blob: 8d27fd19a69cb99f5524d713687332a4a09ff961 [file] [log] [blame]
Harald Welte1733a382017-07-23 17:26:17 +02001module BSSGP_Emulation {
Harald Welte1733a382017-07-23 17:26:17 +02002
Harald Welte34b5a952019-05-27 11:54:11 +02003/* BSSGP Emulation in TTCN-3
Harald Weltef5365242021-01-17 13:46:57 +01004 * (C) 2018-2021 Harald Welte <laforge@gnumonks.org>
Harald Welte34b5a952019-05-27 11:54:11 +02005 * All rights reserved.
6 *
7 * Released under the terms of GNU General Public License, Version 2 or
8 * (at your option) any later version.
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
Harald Welte5339b2e2020-10-04 22:52:56 +020011 *
12 * This code implements BSSGP asa hierarchy of components:
13 * - the main BSSGP_CT which runs on top of a NSE (NS Entity).
14 * - demultiplex based on BVCI. BVCI=0 is handled to a user port
15 * - a per-BVC BSSGP_BVC_CT which runs on top of BSSGP_CT, it handles
16 * one PTP BVCI (i.e. one cell)
17 * - demultiplex based on TLLI
18 * - a per-TLLI BSSGP_Client_CT which runs on top of BSSGP_BVC_CT
Harald Welte34b5a952019-05-27 11:54:11 +020019 */
20
Harald Welte5ac31492018-02-15 20:39:13 +010021import from General_Types all;
22import from Osmocom_Types all;
Harald Welte853c0ad2018-02-15 17:45:29 +010023import from NS_Types all;
24import from NS_Emulation all;
25import from BSSGP_Types all;
26import from Osmocom_Gb_Types all;
27import from IPL4asp_Types all;
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +010028import from Misc_Helpers all;
Harald Welte1733a382017-07-23 17:26:17 +020029
Harald Welte93331e72020-09-12 20:51:05 +020030#ifdef BSSGP_EM_L3
Harald Welte5ac31492018-02-15 20:39:13 +010031import from MobileL3_GMM_SM_Types all;
32import from MobileL3_Types all;
Harald Welte93331e72020-09-12 20:51:05 +020033#endif
Harald Welte5ac31492018-02-15 20:39:13 +010034
35import from LLC_Types all;
36import from LLC_Templates all;
37
Harald Weltea2526a82018-02-18 19:03:36 +010038import from SNDCP_Types all;
39
Harald Welte5ac31492018-02-15 20:39:13 +010040/***********************************************************************
Harald Welte5339b2e2020-10-04 22:52:56 +020041 * Communication between Client Components and per-BVC component
Harald Welte5ac31492018-02-15 20:39:13 +010042 ***********************************************************************/
43
Harald Welte853c0ad2018-02-15 17:45:29 +010044type record BssgpStatusIndication {
Harald Welte5339b2e2020-10-04 22:52:56 +020045 Nsei nsei optional,
Harald Welte853c0ad2018-02-15 17:45:29 +010046 BssgpBvci bvci,
47 BvcState state
Harald Welte5339b2e2020-10-04 22:52:56 +020048};
49
50type record BssgpResetIndication {
51 BssgpBvci bvci
52};
53
54template (value) BssgpStatusIndication
55ts_BssgpStsInd(template (omit) Nsei nsei, template (value) BssgpBvci bvci,
56 template (value) BvcState state) := {
57 nsei := nsei,
58 bvci := bvci,
59 state := state
Harald Welte853c0ad2018-02-15 17:45:29 +010060}
61
Harald Welte5339b2e2020-10-04 22:52:56 +020062template (present) BssgpStatusIndication
63tr_BssgpStsInd(template Nsei nsei, template (present) BssgpBvci bvci,
64 template (present) BvcState state) := {
Harald Welte853c0ad2018-02-15 17:45:29 +010065 nsei := nsei,
66 bvci := bvci,
67 state := state
68}
69
70type enumerated BvcState {
Harald Welte5339b2e2020-10-04 22:52:56 +020071 /* SGSN role: waiting to be reset by the peer */
72 BVC_S_WAIT_RESET,
73 /* PCU role: waiting for NS_ALIVE_UNBLOCKED */
74 BVC_S_WAIT_NS_ALIVE_UNBLOCKED,
75 /* BVC-BLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010076 BVC_S_BLOCKED,
Harald Welte5339b2e2020-10-04 22:52:56 +020077 /* BVC-UNBLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010078 BVC_S_UNBLOCKED
79};
80
81/* port from our (internal) point of view */
82type port BSSGP_SP_PT message {
Harald Welte5ac31492018-02-15 20:39:13 +010083 in PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +020084 PDU_LLC
85#ifdef BSSGP_EM_L3
86 ,
Harald Welte5ac31492018-02-15 20:39:13 +010087 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +020088 PDU_L3_SGSN_MS
89#endif
90 ;
Harald Welte955aa942019-05-03 01:29:29 +020091 out PDU_BSSGP,
92 PDU_LLC,
93 PDU_SN,
Harald Welte853c0ad2018-02-15 17:45:29 +010094 NsStatusIndication,
95 BssgpStatusIndication,
Harald Welte93331e72020-09-12 20:51:05 +020096 ASP_Event
97#ifdef BSSGP_EM_L3
98 ,
Harald Welte5ac31492018-02-15 20:39:13 +010099 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200100 PDU_L3_SGSN_MS
101#endif
102 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100103} with { extension "internal" };
104
105/* port from the user point of view */
106type port BSSGP_PT message {
107 in ASP_Event,
108 NsStatusIndication,
109 BssgpStatusIndication,
Harald Welte955aa942019-05-03 01:29:29 +0200110 PDU_BSSGP,
111 PDU_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200112 PDU_SN
113#ifdef BSSGP_EM_L3
114 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100115 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200116 PDU_L3_SGSN_MS
117#endif
118 ;
Harald Welte5ac31492018-02-15 20:39:13 +0100119 out PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +0200120 PDU_LLC
121#ifdef BSSGP_EM_L3
122 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100123 PDU_L3_SGSN_MS,
Harald Welte93331e72020-09-12 20:51:05 +0200124 PDU_L3_MS_SGSN
125#endif
126 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100127} with { extension "internal" };
128
Harald Welte5339b2e2020-10-04 22:52:56 +0200129signature BSSGP_register_client(hexstring imsi, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100130signature BSSGP_unregister_client(hexstring imsi);
Harald Weltef70997d2018-02-17 10:11:19 +0100131signature BSSGP_llgmm_assign(OCT4 tlli_old, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100132
133type port BSSGP_PROC_PT procedure {
Harald Weltef70997d2018-02-17 10:11:19 +0100134 inout BSSGP_register_client, BSSGP_unregister_client, BSSGP_llgmm_assign;
Harald Welte5ac31492018-02-15 20:39:13 +0100135} with { extension "internal" };
136
137
Harald Welte5339b2e2020-10-04 22:52:56 +0200138
Harald Welte5ac31492018-02-15 20:39:13 +0100139/***********************************************************************
140 * Client Component for a single MS/TLLI
Harald Welte5339b2e2020-10-04 22:52:56 +0200141 ***********************************************************************
142 * This is what most users will want to derive their test cases from. It
143 * provides a set of three different ports (PTP, signaling, procedure)
144 * for (currently up to 3) different Cells. Those ports are all connected
145 * to one or multiple different per-BVC compoennts, depending on whether
146 * they share the same BSS or not.
Harald Welte5ac31492018-02-15 20:39:13 +0100147 ***********************************************************************/
148
149type component BSSGP_Client_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200150 /* one port array for each client; allows talking to up to 3 BVC/Cell (handover, ...) */
151 port BSSGP_PT BSSGP[3]; /* PTP-BVC */
152 port BSSGP_PT BSSGP_SIG[3]; /* Signaling BVC */
Harald Welte9b461a92020-12-10 23:41:14 +0100153 port BSSGP_PT BSSGP_GLOBAL[3]; /* Signaling BVC */
Harald Welte5339b2e2020-10-04 22:52:56 +0200154 port BSSGP_PROC_PT BSSGP_PROC[3]; /* registration / deregistration */
Harald Welte5ac31492018-02-15 20:39:13 +0100155};
156
Harald Welte5339b2e2020-10-04 22:52:56 +0200157function f_bssgp_client_register(hexstring imsi, OCT4 tlli, BSSGP_PROC_PT PT := BSSGP_PROC[0])
158runs on BSSGP_Client_CT {
159 PT.call(BSSGP_register_client:{imsi, tlli}) {
160 [] PT.getreply(BSSGP_register_client:{imsi, tlli}) {};
161 }
162}
163
164function f_bssgp_client_unregister(hexstring imsi, BSSGP_PROC_PT PT := BSSGP_PROC[0])
165runs on BSSGP_Client_CT {
166 PT.call(BSSGP_unregister_client:{imsi}) {
167 [] PT.getreply(BSSGP_unregister_client:{imsi}) {};
168 }
169}
170
171/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
172function f_bssgp_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_PROC_PT PT := BSSGP_PROC[0])
173runs on BSSGP_Client_CT {
174 PT.call(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {
175 [] PT.getreply(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {};
176 }
177}
178
Harald Welte5ac31492018-02-15 20:39:13 +0100179/***********************************************************************
180 * Main Component
Harald Welte5339b2e2020-10-04 22:52:56 +0200181 ***********************************************************************
182 * This component exists once, and it runs on to of the NS_Emulation.
183 * It handles the BVC-RESET for the signaling BVCI, and dispatches messages
184 * to the per-BVC components running above it. Messages are routed by:
185 * - PTP BVCI in NS header (if non-0)
186 * - BVCI IE in messages on BVCI=0 in NS header
187 * Messages for which no unique BVC can be identified (like paging without
188 * a BVCI IE set) are broadcast to all BVC components.
189 * We also broadcast state changes (BssgpStatusIndication) and reset events
190 * (BssgpResetIndication) of the signaling BVC to all per-BVC components.
Harald Welte5ac31492018-02-15 20:39:13 +0100191 ***********************************************************************/
192
Harald Welte5339b2e2020-10-04 22:52:56 +0200193function BssgpStart(BssgpConfig cfg, charstring id) runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100194 g_cfg := cfg;
Harald Welte5339b2e2020-10-04 22:52:56 +0200195
196 /* create the per-BVC components based on the configuration */
197 for (var integer i := 0; i < lengthof(g_cfg.bvc); i := i+1) {
198 var BssgpBvcConfig bvc_cfg := g_cfg.bvc[i];
199 var charstring bvc_id := id & "-BVCI" & int2str(bvc_cfg.bvci);
200 /* create, connect and start the BVC component */
201 var BSSGP_BVC_CT bvc_ct := BSSGP_BVC_CT.create(bvc_id);
202 connect(bvc_ct:BVC, self:BVC);
Harald Weltefba7afd2020-11-24 23:12:31 +0100203 bvc_ct.start(f_bssgp_bvc_main(bvc_cfg, g_cfg.sgsn_role, g_cfg.nsei, bvc_id));
Harald Welte5339b2e2020-10-04 22:52:56 +0200204 /* populate the BVC state table */
205 BvcTable[i] := {
206 bvci := bvc_cfg.bvci,
207 cell_id := bvc_cfg.cell_id,
208 comp_ref := bvc_ct
209 };
210 }
211
212 if (g_cfg.sgsn_role) {
213 /* The SGSN side waits for an inbound BVC-RESET on the signaling BVC */
214 f_sign_change_state(BVC_S_WAIT_RESET);
215 } else {
216 /* The BSS/PCU side waits for an inbound NsStatusIndication to Tx BVC-RESET */
217 f_sign_change_state(BVC_S_WAIT_NS_ALIVE_UNBLOCKED);
218 }
219
220 /* main loop */
221 f_bssgp_ScanEvents();
Harald Welte853c0ad2018-02-15 17:45:29 +0100222}
223
Harald Welte5339b2e2020-10-04 22:52:56 +0200224/* master component, exists once per BSSGP instance (NSE) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100225type component BSSGP_CT {
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200226 /* UDP ports towards the bottom (IUT) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100227 port NS_PT BSCP;
Harald Welte5339b2e2020-10-04 22:52:56 +0200228
229 /* control by the user */
230 port BSSGP_CT_PROC_PT PROC;
231
Harald Welte57de2202020-11-24 18:15:02 +0100232 /* global port for procedures without any relation to a BVC
233 * (currently only) SUSPEND/RESUME */
234 port BSSGP_SP_PT GLOBAL;
235
Harald Welte0083ea62020-12-02 21:30:44 +0100236 /* RAN INFORMATION MGMT */
237 port BSSGP_SP_PT RIM;
238
Harald Weltebdf96d82020-11-27 18:58:46 +0100239 /* port to a management instance */
240 port BSSGP_BVC_MGMT_SP_PT MGMT;
241
Harald Welte5339b2e2020-10-04 22:52:56 +0200242 var BssgpConfig g_cfg;
243
244 /* Signaling BVC (BVCI=0) */
245 var BvcState g_sign_bvc_state := BVC_S_WAIT_RESET;
246 timer g_T2 := 60.0;
247
248 /* port to per-BVC components */
249 port BSSGP_BVC_SP_PT BVC;
250 /* per-BVC state table */
Harald Welte633805b2021-02-03 17:47:18 +0100251 var BvcEntity BvcTable[64];
Harald Welte5339b2e2020-10-04 22:52:56 +0200252};
253
254/* one element in the per-BVC state table */
255type record BvcEntity {
256 /* PTP BVCI of this BVC/Cell */
257 BssgpBvci bvci,
258 /* Cell Identity of this cell */
259 BssgpCellId cell_id,
260 /* reference to the per-BVC/cell component */
261 BSSGP_BVC_CT comp_ref
262};
263
264/* find the per-BVC component for a given BVCI */
265private function f_comp_for_bvci(BssgpBvci bvci) runs on BSSGP_CT return BSSGP_BVC_CT {
266 var integer i;
267
268 for (i := 0; i < lengthof(BvcTable); i := i+1) {
269 if (BvcTable[i].bvci == bvci) {
270 return BvcTable[i].comp_ref;
271 }
272 }
273 return null;
274}
275
276/* We are in BVC_S_WAIT_RSET state (only happens in SGSN role) */
277altstep as_sig_wait_reset() runs on BSSGP_CT {
278 var NsUnitdataIndication udi;
279
280 /* Respond to RESET for signalling BVCI 0 */
281 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
282 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5339b2e2020-10-04 22:52:56 +0200283 f_sign_change_state(BVC_S_UNBLOCKED);
Harald Weltebdf96d82020-11-27 18:58:46 +0100284 if (MGMT.checkstate("Connected")) {
285 MGMT.send(BssgpResetIndication:{0});
286 }
287 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200288 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200289}
290
Harald Welte27a70cc2020-12-09 11:57:41 +0100291private template PDU_BSSGP tr_GLOBAL_SIG := (
Harald Welte57de2202020-11-24 18:15:02 +0100292 {pDU_BSSGP_SUSPEND:=?}, {pDU_BSSGP_SUSPEND_ACK:=?}, {pDU_BSSGP_SUSPEND_NACK:=?},
Harald Weltef8e5c5d2020-11-27 22:37:23 +0100293 {pDU_BSSGP_RESUME:=?}, {pDU_BSSGP_RESUME_ACK:=?}, {pDU_BSSGP_RESUME_NACK:=?},
Harald Weltef5365242021-01-17 13:46:57 +0100294 {pDU_BSSGP_SGSN_INVOKE_TRACE:=?}, {pDU_BSSGP_OVERLOAD:=?}, {pDU_BSSGP_STATUS:=?}
Harald Welte57de2202020-11-24 18:15:02 +0100295);
296
Harald Welte0083ea62020-12-02 21:30:44 +0100297/* BSSGP messages that should arrive on the RIM port */
298private template PDU_BSSGP tr_RIM := (
299 {pDU_BSSGP_RAN_INFORMATION:=?}, {pDU_BSSGP_RAN_INFORMATION_REQUEST:=?},
300 {pDU_BSSGP_RAN_INFORMATION_ACK:=?}, {pDU_BSSGP_RAN_INFORMATION_ERROR:=?},
301 {pDU_BSSGP_RAN_INFORMATION_APPLICATION_ERROR:=?}
302);
303
Harald Welte5339b2e2020-10-04 22:52:56 +0200304/* We are in BVC_S_UNBLOCKED state */
305altstep as_sig_unblocked() runs on BSSGP_CT {
306 var BSSGP_BVC_CT bvc_comp_ref;
307 var BSSGP_Client_CT vc_conn;
308 var NsUnitdataIndication udi;
309 var NsUnitdataRequest udr;
Harald Welte57de2202020-11-24 18:15:02 +0100310 var PDU_BSSGP bssgp;
Harald Welte5339b2e2020-10-04 22:52:56 +0200311
312 /* Messages PTP BVCI in BVCI field of NS: dispatch by that */
313 [] BSCP.receive(f_BnsUdInd(?, (2..65535))) -> value udi {
314 bvc_comp_ref := f_comp_for_bvci(valueof(udi.bvci));
315 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
316 /* dispatch to BVC component */
317 BVC.send(udi) to bvc_comp_ref;
318 } else {
319 setverdict(fail, "Rx BSSGP for unknown PTP BVCI ", udi.bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100320 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), udi.bvci, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200321 }
322 }
323
324 /* Messages with BVCI = 0 (Signaling) in BVCI field of NS */
Harald Welte27a70cc2020-12-09 11:57:41 +0100325 [] BSCP.receive(f_BnsUdInd(tr_GLOBAL_SIG, 0)) -> value udi {
Daniel Willmannc5bbcc62021-09-24 13:17:20 +0200326 if (GLOBAL.checkstate("Connected")) {
327 GLOBAL.send(udi.bssgp);
328 }
Harald Welte57de2202020-11-24 18:15:02 +0100329 }
Harald Welte0083ea62020-12-02 21:30:44 +0100330 [] BSCP.receive(f_BnsUdInd(tr_RIM, 0)) -> value udi {
331 if (RIM.checkstate("Connected")) {
332 RIM.send(udi.bssgp);
333 }
334 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200335
336 /* Route based on PTP BVCI in payload/IE of signaling PDU */
337 [] BSCP.receive(f_BnsUdInd(?, 0)) -> value udi {
338 var template (omit) BssgpBvci ptp_bvci := f_BSSGP_BVCI_get(udi.bssgp);
339 if (istemplatekind(ptp_bvci, "omit")) {
340 log("Rx on SIG BVCI without PTP BVCI: broadcasting");
341 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
342 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
343 BVC.send(udi) to BvcTable[i].comp_ref;
344 }
345 }
346 } else {
347 bvc_comp_ref := f_comp_for_bvci(valueof(ptp_bvci));
348 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
349 /* dispatch to BVC component */
350 BVC.send(udi) to bvc_comp_ref;
351 } else {
352 setverdict(fail, "Rx SIG BSSGP for unknown PTP BVCI ", ptp_bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100353 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Pau Espin Pedrol847a1652021-02-05 12:08:04 +0100354 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200355 }
356 }
357
358 /* Handle PS-PAGING on SIGN BVCI with no conditional BVCI IE */
359 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_PS:=?}, 0)) -> value udi {
360 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
361 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
362 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
363 BVC.send(udi) to BvcTable[i].comp_ref;
364 }
365 }
366 }
367 /* Handle CS-PAGING on SIGN BVCI with no conditional BVCI IE */
368 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_CS:=?}, 0)) -> value udi {
369 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
370 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
371 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
372 BVC.send(udi) to BvcTable[i].comp_ref;
373 }
374 }
375 }
376
377 /* per-BVC component sends us something; forward to NS without any validation */
378 [] BVC.receive(NsUnitdataRequest:?) -> value udr {
379 /* patch in the NSEI, as the per-BVC components have no idea about it */
380 udr.nsei := g_cfg.nsei;
381 BSCP.send(udr);
382 }
Harald Welte57de2202020-11-24 18:15:02 +0100383
Harald Welte27a70cc2020-12-09 11:57:41 +0100384 [] GLOBAL.receive(tr_GLOBAL_SIG) -> value bssgp {
Harald Welte57de2202020-11-24 18:15:02 +0100385 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
386 }
Harald Welte0083ea62020-12-02 21:30:44 +0100387
388 [] RIM.receive(tr_RIM) -> value bssgp {
389 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
390 }
391
Harald Welte5339b2e2020-10-04 22:52:56 +0200392}
393
394/* We are in BVC_S_WAIT_NS_ALIVE_UNBLOCKED (only happens in BSS role) */
395altstep as_sig_wait_ns_alive_unblocked() runs on BSSGP_CT {
396 var NsStatusIndication nsi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100397 [] BSCP.receive(NsStatusIndication:{g_cfg.nsei,?, complement (NSVC_S_ALIVE_UNBLOCKED), NSVC_S_ALIVE_UNBLOCKED, true}) -> value nsi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200398 /* if we just became NS-unblocked, send a BCC-RESET */
399 if (g_cfg.sgsn_role == false) {
Harald Weltec4505522020-11-11 18:55:09 +0100400 BSCP.send(f_BnsUdReq(ts_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, 0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200401 g_T2.start;
402 /* The BVC_RESET_ACK is handled in the as_sig_allstate() below */
403 }
404 /* Idea: We could send BVC-UNBLOCK here like some SGSN do */
405 }
406
407}
408
Harald Weltee5887922020-11-27 19:04:46 +0100409/* handling of events irrespective of BVC state (before state-specific handling) */
410altstep as_sig_allstate_pre() runs on BSSGP_CT {
411 var NsUnitdataIndication udi;
412
413 /* Respond to RESET for signalling BVCI 0 */
414 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
415 log("Rx BVC-RESET for Signaling BVCI=0");
416 if (MGMT.checkstate("Connected")) {
417 MGMT.send(BssgpResetIndication:{0});
418 }
419 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
420 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
421 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
422 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
423 }
424 }
425 }
426
427 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100428 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(0, ?), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100429 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
430 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100431 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100432 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
433 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100434 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK_ACK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100435 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
436 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100437 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100438 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
439 }
440
441 /* BVC-RESET-ACK for BVCI=0 while T2 is running: Answer to a BVC-RESET we sent earlier */
442 [g_T2.running] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(0, omit), 0)) -> value udi {
443 log("BVCI(0) Rx BVC-RESET-ACK");
444 g_T2.stop;
445 f_sign_change_state(BVC_S_UNBLOCKED);
446 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
447 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
448 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
449 }
450 }
451 }
452 [] g_T2.timeout {
453 setverdict(fail, "Timeout waiting for BVC-RESET-ACK on BVCI=0");
454 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
455 g_T2.start;
456 }
457}
458
459/* handling of events irrespective of BVC state (after state-specific handling) */
460altstep as_sig_allstate_post() runs on BSSGP_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200461 var BSSGP_Client_CT vc_conn;
462 var NsUnitdataIndication udi;
463 var NsStatusIndication nsi;
464 var ASP_Event evt;
465 var BSSGP_BVC_CT bvc_comp_ref;
466 var BssgpBvci bvci;
Harald Weltee5887922020-11-27 19:04:46 +0100467 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +0200468
469 /* Respond to RESET for signalling BVCI 0 */
470 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
471 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Weltebdf96d82020-11-27 18:58:46 +0100472 if (MGMT.checkstate("Connected")) {
473 MGMT.send(BssgpResetIndication:{0});
474 }
Harald Weltec4505522020-11-11 18:55:09 +0100475 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200476 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
477 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
478 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
479 }
480 }
481 }
482
483 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100484 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(0, ?), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200485 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
486 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100487 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200488 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
489 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100490 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK_ACK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200491 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
492 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100493 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200494 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
495 }
496
497 /* Respond to BLOCK for wrong BVCI */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100498 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(?, ?), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200499 setverdict(fail, "Rx BVC-BLOCK for unknown BVCI");
Harald Weltec4505522020-11-11 18:55:09 +0100500 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200501 }
502
503 /* Respond to RESET with wrong BVCI */
504 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, ?, ?), 0)) -> value udi {
505 var BssgpBvci ptp_bvci := oct2int(udi.bssgp.pDU_BSSGP_BVC_RESET.bVCI.unstructured_value);
506 setverdict(fail, "Rx BVC-RESET for unknown BVCI ", ptp_bvci);
Harald Weltec4505522020-11-11 18:55:09 +0100507 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(ptp_bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200508 }
509
Harald Welte5339b2e2020-10-04 22:52:56 +0200510 /* Forwarding of ASP_Event to per-BVC components */
511 [] BSCP.receive(ASP_Event:?) -> value evt {
512 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
513 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
514 BVC.send(evt) to BvcTable[i].comp_ref;
515 }
516 }
517 }
518 /* Keep NS Status Indicaitons to us; no need to inform per-BVC components [for now?] */
Harald Weltedc0a0902020-11-10 21:03:29 +0100519 [] BSCP.receive(tr_NsStsInd(g_cfg.nsei)) -> value nsi { }
Harald Welte445fc612020-11-10 19:15:30 +0100520 /* We should never see any different NSEI: There's one BSSGP_CT per NSE */
Harald Weltedc0a0902020-11-10 21:03:29 +0100521 [] BSCP.receive(tr_NsStsInd(?)) -> value nsi {
Harald Welte445fc612020-11-10 19:15:30 +0100522 setverdict(fail, "Rx NsStatusInd for wrong NSEI ", nsi);
523 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200524
525 /* Procedure port request to resolve the per-BVC component for a given ptp BVCI */
526 [] PROC.getcall(BSSGP_get_bvci_ct:{?}) -> param(bvci) sender vc_conn {
527 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
528 if (BvcTable[i].bvci == bvci) {
529 PROC.reply(BSSGP_get_bvci_ct:{bvci} value BvcTable[i].comp_ref) to vc_conn;
530 return;
531 }
532 }
533 }
Harald Weltee5887922020-11-27 19:04:46 +0100534
535 /* default case of handling unknown PDUs */
536 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
537 setverdict(fail, "Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_sign_bvc_state);
538 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp), 0, 0));
539 }
540
541 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
542 BSCP.send(f_BnsUdReq(ts_BVC_RESET(brr.cause, 0, omit), 0, 0));
543 g_T2.start;
544 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200545}
546
547/* send the highest decoded layer of the message through given port */
548private function f_send_bssgp_dec(BssgpDecoded dec, BSSGP_Client_CT vc_conn, BSSGP_SP_PT pt) {
549#ifdef BSSGP_EM_L3
550 if (ispresent(dec.l3_mt)) {
551 pt.send(dec.l3_mt) to vc_conn;
552 } else if (ispresent(dec.l3_mo)) {
553 pt.send(dec.l3_mo) to vc_conn;
554 } else
555#endif
556 if (ispresent(dec.sndcp)) {
557 pt.send(dec.sndcp) to vc_conn;
558 } else if (ispresent(dec.llc)) {
559 pt.send(dec.llc) to vc_conn;
560 } else {
561 pt.send(dec.bssgp) to vc_conn;
562 }
563}
564
565
566function f_llc_get_n_u_tx(inout LLC_Entity llc) return uint9_t {
567 var uint9_t ret := llc.n_u_tx_next;
568 llc.n_u_tx_next := llc.n_u_tx_next + 1;
569 return ret;
570}
571
572#ifdef BSSGP_EM_L3
573function f_llc_sapi_by_l3_mo(PDU_L3_MS_SGSN l3_mo) return BIT4 {
574 if (ischosen(l3_mo.msgs.gprs_mm)) {
575 return c_LLC_SAPI_LLGMM;
576 } else if (ischosen(l3_mo.msgs.gprs_sm)) {
577 return c_LLC_SAPI_LLGMM;
578 } else if (ischosen(l3_mo.msgs.sms)) {
579 return c_LLC_SAPI_LLSMS;
580 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100581 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mo));
582 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200583}
584
585private function f_llc_sapi_by_l3_mt(PDU_L3_SGSN_MS l3_mt) return BIT4 {
586 if (ischosen(l3_mt.msgs.gprs_mm)) {
587 return c_LLC_SAPI_LLGMM;
588 } else if (ischosen(l3_mt.msgs.gprs_sm)) {
589 return c_LLC_SAPI_LLGMM;
590 } else if (ischosen(l3_mt.msgs.sms)) {
591 return c_LLC_SAPI_LLSMS;
592 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100593 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mt));
594 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200595}
596#endif
597
598/* main loop of BSSGP_CT */
599private function f_bssgp_ScanEvents() runs on BSSGP_CT {
600 while (true) {
601 alt {
602 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
Harald Weltee5887922020-11-27 19:04:46 +0100603 [] as_sig_allstate_pre();
Harald Welte5339b2e2020-10-04 22:52:56 +0200604 [g_sign_bvc_state == BVC_S_WAIT_NS_ALIVE_UNBLOCKED] as_sig_wait_ns_alive_unblocked();
605 [g_sign_bvc_state == BVC_S_UNBLOCKED] as_sig_unblocked();
Harald Weltee5887922020-11-27 19:04:46 +0100606 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
607 [] as_sig_allstate_post();
Harald Welte5339b2e2020-10-04 22:52:56 +0200608 }
609 } /* while */
610}
611
612/* generate a send template. Cannot be a real template as we want to use g_cfg.nsei */
Harald Weltec4505522020-11-11 18:55:09 +0100613private function f_BnsUdReq(template PDU_BSSGP pdu, BssgpBvci bvci, integer lsp)
Harald Welte5339b2e2020-10-04 22:52:56 +0200614runs on BSSGP_CT return NsUnitdataRequest {
615 var NsUnitdataRequest udr := {
616 bvci := bvci,
617 nsei := g_cfg.nsei,
Harald Weltec4505522020-11-11 18:55:09 +0100618 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +0200619 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
620 * unbound integer value." when trying to send the reocrd rather than the octetstring */
621 //sdu := omit,
622 //bssgp := valueof(pdu)
623 sdu := enc_PDU_BSSGP(valueof(pdu)),
624 bssgp := omit
625 }
626 return udr;
627}
628
629/* generate a receive template. Cannot be a real template as we want to use g_cfg.nsei */
630private function f_BnsUdInd(template PDU_BSSGP pdu, template BssgpBvci bvci)
631runs on BSSGP_CT return template (present) NsUnitdataIndication {
632 var template (present) NsUnitdataIndication udi := {
633 bvci := bvci,
634 nsei := g_cfg.nsei,
Harald Welte80a249a2020-11-17 19:57:40 +0100635 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +0200636 sdu := *,
637 bssgp := pdu
638 }
639 return udi;
640}
641
642/* change state of signaling BVCI; notify per-BVC components */
643private function f_sign_change_state(BvcState new_state) runs on BSSGP_CT {
644 if (new_state == g_sign_bvc_state) {
645 return;
646 }
647 log("BVCI(0) State Transition: ", g_sign_bvc_state, " -> ", new_state);
648 g_sign_bvc_state := new_state;
649 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
650 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
651 BVC.send(ts_BssgpStsInd(g_cfg.nsei, 0, g_sign_bvc_state)) to BvcTable[i].comp_ref;
652 }
653 }
654}
655
656/* User wants to get per-BVC component reference from BSSGP_CT */
657signature BSSGP_get_bvci_ct(BssgpBvci bvci) return BSSGP_BVC_CT;
658
659type port BSSGP_CT_PROC_PT procedure {
660 inout BSSGP_get_bvci_ct
661} with { extension "internal" };
662
663/* convenience wrapper function for user */
664function f_bssgp_get_bvci_ct(BssgpBvci bvci, BSSGP_CT_PROC_PT PT) return BSSGP_BVC_CT {
665 var BSSGP_BVC_CT res;
666 PT.call(BSSGP_get_bvci_ct:{bvci}) {
667 [] PT.getreply(BSSGP_get_bvci_ct:{bvci})-> value res {
668 return res;
669 }
670 }
671}
672
673/***********************************************************************
674 * per-BVC (Cell) Component
675 ***********************************************************************
676 * Any number of these components runs on top of the BSSGP_CT, each
677 * representing one PTP BVC within this NSE. Users (test cases) can
678 * register with TLLI and/or IMSI via the procedure port.
679 ***********************************************************************/
680
681/* per-BVC component. Exists once per cell within a BSSGP_CT */
682type component BSSGP_BVC_CT {
683 /* port towards the underlying BSSGP_CT */
684 port BSSGP_BVC_PT BVC;
685
Harald Welte199f3862020-11-15 22:37:45 +0100686 /* port to a management instance */
687 port BSSGP_BVC_MGMT_SP_PT MGMT;
688
Harald Welte71449b02020-12-09 11:58:23 +0100689 /* per-BVC global port for e.g. BVC Flow Control */
690 port BSSGP_SP_PT GLOBAL;
691
692 /* BSSGP-User SAP towards the user (per-TLLI, Client) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100693 port BSSGP_SP_PT BSSGP_SP;
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200694 port BSSGP_SP_PT BSSGP_SP_SIG;
Harald Welte5ac31492018-02-15 20:39:13 +0100695 port BSSGP_PROC_PT BSSGP_PROC;
Harald Welte853c0ad2018-02-15 17:45:29 +0100696
Harald Welte5339b2e2020-10-04 22:52:56 +0200697 var BssgpBvcConfig g_cfg;
698 var boolean g_sgsn_role;
Harald Weltefba7afd2020-11-24 23:12:31 +0100699 var Nsei g_nsei;
Harald Welte5ac31492018-02-15 20:39:13 +0100700
Harald Weltec4505522020-11-11 18:55:09 +0100701 /* default Link Selector Parameter for this BVC (for traffic unrelated to a TLLI) */
702 var integer g_bvc_lsp;
703
Harald Welte853c0ad2018-02-15 17:45:29 +0100704 var BvcState g_ptp_bvc_state := BVC_S_BLOCKED;
705 timer g_T1 := 15.0;
706 timer g_T2 := 60.0;
Harald Welte5339b2e2020-10-04 22:52:56 +0200707 var boolean g_t1_waits_for_block_ack := false;
Harald Welte199f3862020-11-15 22:37:45 +0100708 /* for re-transmissions */
709 var BssgpCause g_last_block_cause;
710 var BssgpCause g_last_reset_cause;
Harald Welte5ac31492018-02-15 20:39:13 +0100711
Harald Welte633805b2021-02-03 17:47:18 +0100712 var ClientEntity ClientTable[64];
Harald Welte5339b2e2020-10-04 22:52:56 +0200713};
Harald Welte853c0ad2018-02-15 17:45:29 +0100714
Harald Welte5339b2e2020-10-04 22:52:56 +0200715/* port between global BSSGP_CT and per-BVC BSSGP_BVC_CT */
716type port BSSGP_BVC_SP_PT message {
717 in NsUnitdataRequest;
718 out ASP_Event,
719 BssgpStatusIndication,
720 BssgpResetIndication,
721 NsUnitdataIndication;
722} with { extension "internal" };
723type port BSSGP_BVC_PT message {
724 in ASP_Event,
725 BssgpStatusIndication,
726 BssgpResetIndication,
727 NsUnitdataIndication;
728 out NsUnitdataRequest;
729} with { extension "internal" };
Alexander Couzens6b449fb2018-07-31 14:19:36 +0200730
Harald Welte199f3862020-11-15 22:37:45 +0100731/* port between BSSGP_BVC_CT and a management instance */
732type port BSSGP_BVC_MGMT_SP_PT message {
733 in BssgpResetRequest,
734 BssgpBlockRequest,
735 BssgpUnblockRequest;
736 out BssgpStatusIndication,
737 BssgpResetIndication;
738} with { extension "internal" };
739type port BSSGP_BVC_MGMT_PT message {
740 in BssgpStatusIndication,
741 BssgpResetIndication;
742 out BssgpResetRequest,
743 BssgpBlockRequest,
744 BssgpUnblockRequest;
745} with { extension "internal" };
746
747type record BssgpResetRequest {
748 BssgpCause cause
749};
750type record BssgpBlockRequest {
751 BssgpCause cause
752};
753type record BssgpUnblockRequest {
754};
Harald Welte5339b2e2020-10-04 22:52:56 +0200755
756/* one element in the per-TLLI state table */
Harald Welte5ac31492018-02-15 20:39:13 +0100757type record ClientEntity {
758 OCT4 tlli,
759 OCT4 tlli_old optional,
760 hexstring imsi,
Harald Welte5ac31492018-02-15 20:39:13 +0100761 BSSGP_Client_CT comp_ref,
762 /* LLC entities, one for each SAPI */
763 LLC_Entity llc[16]
Harald Welte853c0ad2018-02-15 17:45:29 +0100764};
Harald Welte5ac31492018-02-15 20:39:13 +0100765type record LLC_Entity {
766 boolean sgsn_role,
767 /* N(U) on transmit side for next PDU */
768 uint9_t n_u_tx_next,
769 /* N(U) on receive side, last PDU */
770 uint9_t n_u_rx_last optional
771};
Harald Welte5339b2e2020-10-04 22:52:56 +0200772type record length(16) of LLC_Entity LLC_Entities;
Harald Welte5ac31492018-02-15 20:39:13 +0100773
Harald Weltef5365242021-01-17 13:46:57 +0100774private template PDU_BSSGP tr_GLOBAL_PTP := (
775 {pDU_BSSGP_STATUS:=?}
776);
777
Alexander Couzenscdfb7512018-07-31 15:37:14 +0200778function f_llc_create(boolean sgsn_role := false) return LLC_Entities {
779 var LLC_Entities llc;
780 for (var integer i := 0; i < 16; i := i+1) {
781 llc[i] := valueof(t_LLC_init(sgsn_role));
782 }
783 return llc;
784}
785
Harald Welte5ac31492018-02-15 20:39:13 +0100786private template LLC_Entity t_LLC_init(boolean sgsn_role := false) := {
787 sgsn_role := sgsn_role,
788 n_u_tx_next := 0,
789 n_u_rx_last := -
790}
791
Harald Welte5339b2e2020-10-04 22:52:56 +0200792/* configuration of BSSGP_CT */
793type record BssgpConfig {
794 Nsvci nsei,
795 boolean sgsn_role,
796 BssgpBvcConfigs bvc
797};
798/* Configuration of one BVC (Cell) BSSGP_BVC_CT */
799type record BssgpBvcConfig {
800 BssgpBvci bvci,
801 BssgpCellId cell_id,
Harald Welte4d112c92020-11-12 19:48:31 +0100802 BssgpDecodeDepth depth,
803 BssgpCreateCallback create_cb
Harald Welte5339b2e2020-10-04 22:52:56 +0200804};
805type record of BssgpBvcConfig BssgpBvcConfigs;
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200806type enumerated BssgpDecodeDepth {
807 BSSGP_DECODE_DEPTH_BSSGP,
808 BSSGP_DECODE_DEPTH_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200809 BSSGP_DECODE_DEPTH_SNDCP
810#ifdef BSSGP_EM_L3
811 ,
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200812 BSSGP_DECODE_DEPTH_L3
Harald Welte93331e72020-09-12 20:51:05 +0200813#endif
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200814};
Harald Welte4d112c92020-11-12 19:48:31 +0100815/* call-back function type: Called for inbound BSSGP for unknown TLLIs; could create BSSGP_ClienT_CT */
816type function BssgpCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT;
817
818/* Default Create Callback function: Fail and terminate */
819function DefaultCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100820 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for TLLI ", tlli));
Harald Welte4d112c92020-11-12 19:48:31 +0100821}
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200822
Harald Welte5339b2e2020-10-04 22:52:56 +0200823/*
824private function f_tbl_init() runs on BSSGP_BVC_CT {
825 var integer i;
826 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
827 ImsiTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100828 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100829
Harald Welte5339b2e2020-10-04 22:52:56 +0200830 for (i := 0; i < sizeof(TlliTable); i := i+1) {
831 TlliTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100832 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100833}
Harald Welte5339b2e2020-10-04 22:52:56 +0200834*/
Harald Welte853c0ad2018-02-15 17:45:29 +0100835
Harald Welte5339b2e2020-10-04 22:52:56 +0200836private function f_tbl_client_add(hexstring imsi, OCT4 tlli, BSSGP_Client_CT vc_conn)
837runs on BSSGP_BVC_CT {
838 var integer i;
839 for (i := 0; i < sizeof(ClientTable); i := i+1) {
840 if (not isvalue(ClientTable[i].comp_ref)) {
841 log("Adding Client=", vc_conn, ", IMSI=", imsi, ", TLLI=", tlli, ", index=", i);
842 ClientTable[i] := {
843 tlli := tlli,
844 tlli_old := omit,
845 imsi := imsi,
846 comp_ref := vc_conn,
847 llc := -
848 };
849 for (var integer j := 0; j < sizeof(ClientTable[i].llc); j := j+1) {
850 ClientTable[i].llc[j] := valueof(t_LLC_init(g_sgsn_role));
851 }
852 return;
Harald Welte5ac31492018-02-15 20:39:13 +0100853 }
854 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200855 testcase.stop("Client Table full");
Harald Welte853c0ad2018-02-15 17:45:29 +0100856}
857
Harald Welte5339b2e2020-10-04 22:52:56 +0200858private function f_tbl_client_del(hexstring imsi, BSSGP_Client_CT vc_conn) runs on BSSGP_BVC_CT {
859 var integer i;
860 for (i := 0; i < sizeof(ClientTable); i := i+1) {
861 if (isvalue(ClientTable[i].imsi) and ClientTable[i].imsi == imsi) {
862 if (ClientTable[i].comp_ref != vc_conn) {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100863 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
864 log2str("Cannot unregister index=", i, " IMSI ",
865 imsi, " registred to ", ClientTable[i].comp_ref,
866 " from ", vc_conn));
Harald Welte5339b2e2020-10-04 22:52:56 +0200867 }
868 log("Removing Client IMSI=", imsi, ", index=", i);
869 ClientTable[i] := {
870 tlli := -,
871 tlli_old := omit,
872 imsi := ''H,
873 comp_ref := null,
874 llc := - };
875 return;
876 }
877 }
878 log("Warning: Could not find client for IMSI ", imsi);
879 return;
880}
881
882/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
883private function f_tbl_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_Client_CT vc_conn)
884runs on BSSGP_BVC_CT {
885 var integer i := f_tbl_idx_by_comp(vc_conn);
886
887 if (tlli_old == 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
888 /* TLLI assignment */
889 ClientTable[i].tlli := tlli_new;
890 ClientTable[i].tlli_old := omit;
891 } else if (tlli_old != 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
892 /* TLLI change: both active */
893 ClientTable[i].tlli := tlli_new;
894 ClientTable[i].tlli_old := tlli_old;
895 } else if (tlli_old != 'FFFFFFFF'O and tlli_new == 'FFFFFFFF'O) {
896 /* TLLI unassignment: old shall be unassigned; new stays */
897 ClientTable[i].tlli_old := omit;
898 }
899}
900
901private function f_tbl_comp_by_imsi(hexstring imsi) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
902 var integer i;
903 for (i := 0; i < sizeof(ClientTable); i := i+1) {
904 if (isvalue(ClientTable[i].imsi) and isvalue(ClientTable[i].comp_ref)
905 and ClientTable[i].imsi == imsi) {
906 return ClientTable[i].comp_ref;
907 }
908 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100909 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for IMSI ", imsi));
910 return ClientTable[0].comp_ref;
Harald Welte5339b2e2020-10-04 22:52:56 +0200911}
912
913private function f_tbl_comp_by_tlli(OCT4 tlli) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
914 var integer i;
915 for (i := 0; i < sizeof(ClientTable); i := i+1) {
916 if (isvalue(ClientTable[i].comp_ref) and
917 (isvalue(ClientTable[i].tlli) and (ClientTable[i].tlli == tlli or
918 isvalue(ClientTable[i].tlli_old) and ClientTable[i].tlli_old == tlli) )) {
919 return ClientTable[i].comp_ref;
920 }
921 }
Harald Welte4d112c92020-11-12 19:48:31 +0100922 return null;
Harald Welte5339b2e2020-10-04 22:52:56 +0200923}
924
925private function f_tbl_idx_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return integer {
926 var integer i;
927 for (i := 0; i < sizeof(ClientTable); i := i+1) {
928 if (isvalue(ClientTable[i].comp_ref) and ClientTable[i].comp_ref == comp_ref) {
929 return i;
930 }
931 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100932 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Client for Component ", comp_ref));
933 return 1;
Harald Welte5339b2e2020-10-04 22:52:56 +0200934}
935
936private function f_tbl_tlli_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return OCT4 {
937 var integer i;
938 for (i := 0; i < sizeof(ClientTable); i := i+1) {
939 if (isvalue(ClientTable[i].tlli) and isvalue(ClientTable[i].comp_ref)
940 and ClientTable[i].comp_ref == comp_ref) {
941 return ClientTable[i].tlli;
942 }
943 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100944 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find TLLI for Component ", comp_ref));
945 return '00000000'O;
Harald Welte5339b2e2020-10-04 22:52:56 +0200946}
947
948/* PDU_BSSGP enhanced with LLC and possibly L3 decoded payloads */
949type record BssgpDecoded {
950 PDU_BSSGP bssgp,
951 PDU_LLC llc optional,
952#ifdef BSSGP_EM_L3
953 PDU_L3_MS_SGSN l3_mo optional,
954 PDU_L3_SGSN_MS l3_mt optional,
955#endif
956 PDU_SN sndcp optional
957}
958
959/* Decode a PDU_BSSGP into a BssgpDecoded (i.e. with LLC/L3 decoded, as applicable) */
960private function f_dec_bssgp(PDU_BSSGP bssgp) runs on BSSGP_BVC_CT return BssgpDecoded {
961 var BssgpDecoded dec := {
962 bssgp := bssgp,
963 llc := omit,
964#ifdef BSSGP_EM_L3
965 l3_mo := omit,
966 l3_mt := omit,
967#endif
968 sndcp := omit
969 };
970
971 /* Decode LLC, if it is a PDU that contains LLC */
972 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_LLC) {
973 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
974 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_DL_UNITDATA.lLC_PDU.lLC_PDU);
975 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
976 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_UL_UNITDATA.lLC_PDU.lLC_PDU);
977 }
978 }
979
980 /* Decode SNDCP, if it is a LLC PDU containing user plane data */
981 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_SNDCP) {
982 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_USER)) {
983 dec.sndcp := dec_PDU_SN(dec.llc.pDU_LLC_UI.information_field_UI);
984 }
985 }
986
987#ifdef BSSGP_EM_L3
988 /* Decode L3, if it is a LLC PDU containing L3 */
989 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_L3) {
990 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_L3)) {
991 if (g_sgsn_role) {
992 dec.l3_mo := dec_PDU_L3_MS_SGSN(dec.llc.pDU_LLC_UI.information_field_UI);
993 } else {
994 dec.l3_mt := dec_PDU_L3_SGSN_MS(dec.llc.pDU_LLC_UI.information_field_UI);
995 }
996 }
997 }
998#endif
999
1000 return dec;
1001}
1002
1003private function f_ptp_sendUnblock() runs on BSSGP_BVC_CT {
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001004 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001005 g_t1_waits_for_block_ack := false;
1006 g_T1.start;
1007}
1008
1009private function f_ptp_sendBlock(BssgpCause cause) runs on BSSGP_BVC_CT {
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001010 BVC.send(ts_ptp_BnsUdReq(ts_BVC_BLOCK(g_cfg.bvci, cause), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001011 g_t1_waits_for_block_ack := true;
1012 g_T1.start;
Harald Welte199f3862020-11-15 22:37:45 +01001013 g_last_block_cause := cause;
Harald Welte5339b2e2020-10-04 22:52:56 +02001014}
1015
1016private function f_ptp_sendStatus(BssgpCause cause, PDU_BSSGP pdu) runs on BSSGP_BVC_CT {
1017 /* FIXME: Make sure correct Signaling or PTP BVCI is used! */
Harald Weltec4505522020-11-11 18:55:09 +01001018 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_STATUS(g_cfg.bvci, cause, pdu), g_cfg.bvci, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001019}
1020
Harald Welte199f3862020-11-15 22:37:45 +01001021private function f_ptp_sendReset(BssgpCause cause := BSSGP_CAUSE_OM_INTERVENTION) runs on BSSGP_BVC_CT {
Harald Weltec967d0e2020-09-14 16:07:26 +02001022 var PDU_BSSGP pdu;
1023
1024 /* The Cell Identifier IE is mandatory in the BVC-RESET PDU sent from BSS to
1025 * SGSN in order to reset a BVC corresponding to a PTP functional entity. The
1026 * Cell Identifier IE shall not be used in any other BVC-RESET PDU. */
Harald Welte5339b2e2020-10-04 22:52:56 +02001027 if (g_sgsn_role) {
Harald Welte199f3862020-11-15 22:37:45 +01001028 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, omit));
Harald Weltec967d0e2020-09-14 16:07:26 +02001029 } else {
Harald Welte199f3862020-11-15 22:37:45 +01001030 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, g_cfg.cell_id));
Harald Weltec967d0e2020-09-14 16:07:26 +02001031 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001032
1033 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
Harald Weltec4505522020-11-11 18:55:09 +01001034 BVC.send(ts_ptp_BnsUdReq(pdu, 0, g_bvc_lsp));
Harald Welte853c0ad2018-02-15 17:45:29 +01001035 g_T2.start;
1036 //f_change_state(BVC_S_WAIT_RESET);
Harald Welte199f3862020-11-15 22:37:45 +01001037 g_last_reset_cause := cause;
Harald Welte853c0ad2018-02-15 17:45:29 +01001038}
1039
Harald Welte5339b2e2020-10-04 22:52:56 +02001040/* PTP-BVC is in BVC_S_BLOCKED state */
1041private altstep as_ptp_blocked() runs on BSSGP_BVC_CT {
1042 var NsUnitdataIndication udi;
1043
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001044 [g_T1.running and not g_t1_waits_for_block_ack] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_UNBLOCK_ACK(g_cfg.bvci), 0)) {
Harald Welte5339b2e2020-10-04 22:52:56 +02001045 g_T1.stop;
1046 f_ptp_change_state(BVC_S_UNBLOCKED);
1047 }
1048
Harald Welteddfc6672020-11-24 23:13:01 +01001049 /* Inbound BVC-UNBLOCK from peer */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001050 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_UNBLOCK(g_cfg.bvci), 0)) {
1051 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welteddfc6672020-11-24 23:13:01 +01001052 f_ptp_change_state(BVC_S_UNBLOCKED);
1053 }
1054
Harald Welte5339b2e2020-10-04 22:52:56 +02001055 /* RESET-ACK before T2 timeout: reset successful. In SGSN role, CellID must be present */
1056 [g_T2.running and g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
1057 g_T2.stop;
1058 f_ptp_change_state(BVC_S_UNBLOCKED);
1059 }
1060 /* RESET-ACK before T2 timeout: reset successful. In BSS role, CellID must be absent */
1061 [g_T2.running and not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, omit), 0)) -> value udi {
1062 g_T2.stop;
1063 f_ptp_change_state(BVC_S_UNBLOCKED);
1064 }
1065
Harald Welteab50cc22020-11-25 17:11:08 +01001066 [] as_ptp_handle_inbound_reset();
1067
Harald Welte5339b2e2020-10-04 22:52:56 +02001068 [] g_T2.timeout {
1069 /* BVC-RESET-ACK PDU was not received within T2: retransmit */
Harald Welte199f3862020-11-15 22:37:45 +01001070 f_ptp_sendReset(g_last_reset_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001071 }
Harald Welte199f3862020-11-15 22:37:45 +01001072
1073 [] MGMT.receive(BssgpUnblockRequest:?) {
1074 f_ptp_sendUnblock();
1075 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001076}
1077
Harald Welte5339b2e2020-10-04 22:52:56 +02001078/* PTP-BVC is in UNBLOCKED state */
1079private altstep as_ptp_unblocked() runs on BSSGP_BVC_CT {
1080 var NsUnitdataIndication udi;
1081 var NsStatusIndication nsi;
Harald Welte199f3862020-11-15 22:37:45 +01001082 var BssgpBlockRequest bbr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001083 var BSSGP_Client_CT vc_conn;
1084 var ASP_Event evt;
1085 var PDU_BSSGP bs_pdu;
1086 var PDU_LLC llc;
1087#ifdef BSSGP_EM_L3
1088 var PDU_L3_MS_SGSN l3_mo;
1089 var PDU_L3_SGSN_MS l3_mt;
1090#endif
1091
1092 /* bogus unblock, just respond with ACK */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001093 [] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_UNBLOCK(g_cfg.bvci), 0)) -> value udi {
1094 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001095 }
1096 /* Respond to BLOCK with BLOCK-ACK + change state */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001097 [] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_BLOCK(g_cfg.bvci, ?), 0)) -> value udi {
1098 BVC.send(ts_ptp_BnsUdReq(ts_BVC_BLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001099 g_T1.stop;
1100 f_ptp_change_state(BVC_S_BLOCKED);
1101 }
1102 /* BLOCK-ACK before T1 timeout: mark as blocked */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001103 [g_T1.running and g_t1_waits_for_block_ack] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_BLOCK_ACK(g_cfg.bvci), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +02001104 g_T1.stop;
1105 f_ptp_change_state(BVC_S_BLOCKED);
1106 }
1107 /* Re-transmit BLOCK / UNBLOCK on T1 timeout */
1108 [not g_t1_waits_for_block_ack] g_T1.timeout {
1109 f_ptp_sendUnblock();
1110 }
1111 [g_t1_waits_for_block_ack] g_T1.timeout {
Harald Welte199f3862020-11-15 22:37:45 +01001112 f_ptp_sendBlock(g_last_block_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001113 }
1114
Harald Welteab50cc22020-11-25 17:11:08 +01001115 [] as_ptp_handle_inbound_reset();
1116
Harald Welte5339b2e2020-10-04 22:52:56 +02001117 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_FC_BVC, g_cfg.bvci)) -> value udi {
Harald Welte71449b02020-12-09 11:58:23 +01001118 if (GLOBAL.checkstate("Connected")) {
1119 GLOBAL.send(udi.bssgp);
1120 } else {
1121 /* simply acknowledge all per-BVC Flow Control Messages */
1122 var OCT1 tag := udi.bssgp.pDU_BSSGP_FLOW_CONTROL_BVC.tag.unstructured_Value;
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001123 BVC.send(ts_ptp_BnsUdReq(ts_BVC_FC_BVC_ACK(tag), g_cfg.bvci, g_bvc_lsp));
Harald Welte71449b02020-12-09 11:58:23 +01001124 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001125 }
Harald Welte71449b02020-12-09 11:58:23 +01001126
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001127 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_FC_BVC_ACK(?), g_cfg.bvci)) -> value udi {
Harald Welte71449b02020-12-09 11:58:23 +01001128 if (GLOBAL.checkstate("Connected")) {
1129 GLOBAL.send(udi.bssgp);
1130 } else {
1131 /* ignore any incoming flow control ACK */
1132 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001133 }
Harald Welte71449b02020-12-09 11:58:23 +01001134
Harald Weltef5365242021-01-17 13:46:57 +01001135 [] BVC.receive(tr_ptp_BnsUdInd(tr_GLOBAL_PTP, g_cfg.bvci)) -> value udi {
1136 if (GLOBAL.checkstate("Connected")) {
1137 GLOBAL.send(udi.bssgp);
1138 } else {
1139 setverdict(fail, "Received BSSGP STATUS ", udi.bssgp);
1140 }
1141 }
1142
Harald Welte5339b2e2020-10-04 22:52:56 +02001143 /* Any other PTP BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1144 [] BVC.receive(tr_ptp_BnsUdInd(?, g_cfg.bvci)) -> value udi {
1145 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1146 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1147 if (isvalue(tlli)) {
1148 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001149 if (vc_conn == null) {
1150 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1151 } else {
1152 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP);
1153 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001154 } else {
1155 log("No TLLI: Broadcasting ", dec);
1156 /* broadcast this message to all components */
1157 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1158 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1159 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1160 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP);
1161 }
1162 }
1163 }
1164 }
1165
1166 /* Any other SIG BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1167 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1168 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1169 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1170 if (isvalue(tlli)) {
1171 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001172 if (vc_conn == null) {
1173 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1174 } else {
1175 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP_SIG);
1176 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001177 } else {
1178 log("No TLLI: Broadcasting ", dec);
1179 /* broadcast this message to all components */
1180 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1181 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1182 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1183 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP_SIG);
1184 }
1185 }
1186 }
1187 }
1188
1189 /* ConnHdlr sends BSSGP on BVCI=0 port: forward it to BSSGP_CT */
1190 [] BSSGP_SP_SIG.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001191 BVC.send(ts_ptp_BnsUdReq(bs_pdu, 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001192 }
1193
1194 /* ConnHdlr sends BSSGP on BVCI=PTP port: forward it to BSSGP_CT */
1195 [] BSSGP_SP.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001196 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001197 }
1198
1199#ifdef BSSGP_EM_L3
1200 /* ConnHdlr sends L3: Encode and send as UL_UD / DL_UD */
1201 [g_sgsn_role] BSSGP_SP.receive(PDU_L3_SGSN_MS:?) -> value l3_mt sender vc_conn {
1202 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001203 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001204 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(l3_mt);
1205 var BIT4 sapi := f_llc_sapi_by_l3_mt(l3_mt);
1206 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1207 var octetstring llc_enc := enc_PDU_LLC(valueof(ts_LLC_UI(l3_enc, sapi, '1'B, n_u)));
Harald Weltec4505522020-11-11 18:55:09 +01001208 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001209 }
1210 [not g_sgsn_role] BSSGP_SP.receive(PDU_L3_MS_SGSN:?) -> value l3_mo sender vc_conn {
1211 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001212 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001213 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(l3_mo);
1214 var BIT4 sapi := f_llc_sapi_by_l3_mo(l3_mo);
1215 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1216 var octetstring llc_enc := enc_PDU_LLC(valueof(ts_LLC_UI(l3_enc, sapi, '1'B, n_u)));
Harald Weltec4505522020-11-11 18:55:09 +01001217 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_UL_UD(tlli, g_cfg.cell_id, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001218 }
1219#endif
1220
1221 /* ConnHdlr sends raw LLC: Encode and send as UL_UD / DL_UD */
1222 [not g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1223 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001224 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001225 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001226 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_UL_UD(tlli, g_cfg.cell_id, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001227 }
1228 [g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1229 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001230 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001231 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001232 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001233 }
Harald Welte199f3862020-11-15 22:37:45 +01001234
Harald Welte71449b02020-12-09 11:58:23 +01001235 /* Testcase sends us BSSGP on global port */
1236 [] GLOBAL.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
1237 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
1238 }
1239
Harald Welte199f3862020-11-15 22:37:45 +01001240 [] MGMT.receive(BssgpBlockRequest:?) -> value bbr {
1241 f_ptp_sendBlock(bbr.cause);
1242 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001243}
1244
Harald Welteab50cc22020-11-25 17:11:08 +01001245private altstep as_ptp_handle_inbound_reset() runs on BSSGP_BVC_CT {
1246 var NsUnitdataIndication udi;
1247 /* we are a SGSN: BSS/PCU-originated RESET must have a cell ID */
1248 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
1249 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001250 if (MGMT.checkstate("Connected")) {
1251 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1252 }
Harald Welteab50cc22020-11-25 17:11:08 +01001253 /* Respond to RESET with correct BVCI but without CellID */
1254 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, omit), 0, g_bvc_lsp));
1255 /* FIXME: reset all state? What about clients? */
1256 f_ptp_change_state(BVC_S_UNBLOCKED);
1257 }
1258 /* we are a BSS/PCU: SGSN-originated RESET must not have a cell ID */
1259 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, omit), 0)) -> value udi {
1260 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001261 if (MGMT.checkstate("Connected")) {
1262 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1263 }
Harald Welteab50cc22020-11-25 17:11:08 +01001264 /* Respond to RESET with correct BVCI with CellID */
1265 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0, g_bvc_lsp));
1266 /* FIXME: reset all state? What about clients? */
1267 f_ptp_change_state(BVC_S_UNBLOCKED);
1268 }
1269}
1270
Harald Welte5339b2e2020-10-04 22:52:56 +02001271/* Events permitted in all states */
1272private altstep as_ptp_allstate() runs on BSSGP_BVC_CT {
1273 var NsUnitdataIndication udi;
Harald Welte199f3862020-11-15 22:37:45 +01001274 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001275 var BSSGP_Client_CT vc_conn;
1276 var OCT4 tlli, tlli_old;
1277 var hexstring imsi;
1278
1279 /* Signaling BVC was reset */
1280 [] BVC.receive(BssgpResetIndication:{0}) {
Harald Welte199f3862020-11-15 22:37:45 +01001281 if (MGMT.checkstate("Connected")) {
1282 MGMT.send(BssgpResetIndication:{0});
1283 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001284 if (not g_sgsn_role) {
1285 f_ptp_change_state(BVC_S_BLOCKED);
1286 /* when the BSS side signaling PTP is RESET, all PTP BVC must start the
1287 * reset procedure */
1288 f_ptp_sendReset();
1289 } else {
1290 f_ptp_change_state(BVC_S_UNBLOCKED);
1291 }
1292 }
1293
Harald Welte5339b2e2020-10-04 22:52:56 +02001294 /* Ignore those for now */
1295 [] BVC.receive(ASP_Event:?) { }
1296 [] BVC.receive(BssgpStatusIndication:?) { }
1297
1298 /* catch-all */
1299 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1300 setverdict(fail, "Received unexpected NS (SIG): ", udi);
1301 }
1302 [] BVC.receive(tr_ptp_BnsUdInd(?, ?)) -> value udi {
1303 setverdict(fail, "Received unexpected NS (PTP): ", udi);
1304 }
1305 [] BVC.receive {
1306 setverdict(fail, "Received unexpected message from BSSGP_CT");
1307 }
1308
1309 /* registration/deregistration of Clients (per-TLLI components) */
1310 [] BSSGP_PROC.getcall(BSSGP_register_client:{?,?}) -> param(imsi, tlli) sender vc_conn {
1311 f_tbl_client_add(imsi, tlli, vc_conn);
1312 BSSGP_PROC.reply(BSSGP_register_client:{imsi, tlli}) to vc_conn;
1313 }
1314 [] BSSGP_PROC.getcall(BSSGP_unregister_client:{?}) -> param(imsi) sender vc_conn {
1315 f_tbl_client_del(imsi, vc_conn);
1316 BSSGP_PROC.reply(BSSGP_unregister_client:{imsi}) to vc_conn;
1317 }
1318 [] BSSGP_PROC.getcall(BSSGP_llgmm_assign:{?,?}) -> param(tlli_old, tlli) sender vc_conn {
1319 f_tbl_client_llgmm_assign(tlli_old, tlli, vc_conn);
1320 BSSGP_PROC.reply(BSSGP_llgmm_assign:{tlli_old, tlli}) to vc_conn;
1321 }
Harald Welte199f3862020-11-15 22:37:45 +01001322
1323 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
Harald Welte52cecbb2020-11-25 21:57:31 +01001324 f_ptp_change_state(BVC_S_BLOCKED);
Harald Welte199f3862020-11-15 22:37:45 +01001325 f_ptp_sendReset(brr.cause);
1326 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001327}
1328
1329/* main loop for per-BVC component */
1330private function f_bssgp_bvc_ScanEvents() runs on BSSGP_BVC_CT {
1331 while (true) {
1332 alt {
1333 [g_ptp_bvc_state == BVC_S_BLOCKED] as_ptp_blocked();
1334 [g_ptp_bvc_state == BVC_S_UNBLOCKED] as_ptp_unblocked();
1335 [] as_ptp_allstate();
1336 }
1337 }
1338}
1339
1340/* main function for per-BVC Component */
Harald Weltefba7afd2020-11-24 23:12:31 +01001341private function f_bssgp_bvc_main(BssgpBvcConfig cfg, boolean sgsn_role, Nsei nsei, charstring id) runs on BSSGP_BVC_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +02001342 g_cfg := cfg;
Harald Weltefba7afd2020-11-24 23:12:31 +01001343 g_nsei := nsei;
Harald Weltec4505522020-11-11 18:55:09 +01001344 g_bvc_lsp := cfg.bvci;
Harald Welte5339b2e2020-10-04 22:52:56 +02001345 g_sgsn_role := sgsn_role;
1346 f_bssgp_bvc_ScanEvents();
1347}
1348
Harald Weltec4505522020-11-11 18:55:09 +01001349template (value) NsUnitdataRequest ts_ptp_BnsUdReq(template (value) PDU_BSSGP pdu, template (value) BssgpBvci bvci,
1350 template (value) integer lsp) := {
Harald Welte5339b2e2020-10-04 22:52:56 +02001351 bvci := bvci,
1352 nsei := 0, // overwritten in BSSGP_CT
Harald Weltec4505522020-11-11 18:55:09 +01001353 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +02001354 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
1355 * unbound integer value." when trying to send the reocrd rather than the octetstring */
1356 //sdu := omit,
1357 //bssgp := valueof(pdu)
1358 sdu := enc_PDU_BSSGP(valueof(pdu)),
1359 bssgp := omit
1360}
1361
1362template (present) NsUnitdataIndication tr_ptp_BnsUdInd(template (present) PDU_BSSGP pdu, template (present) BssgpBvci bvci) := {
1363 bvci := bvci,
1364 nsei := ?,
Harald Welte80a249a2020-11-17 19:57:40 +01001365 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +02001366 sdu := *,
1367 bssgp := pdu
1368}
1369
1370/* change state of PTP BVC; broadcast state change to clients */
1371private function f_ptp_change_state(BvcState new_state) runs on BSSGP_BVC_CT {
1372 if (new_state == g_ptp_bvc_state) {
1373 return;
1374 }
1375 log("BVCI(", g_cfg.bvci, ") State Transition: ", g_ptp_bvc_state, " -> ", new_state);
1376 g_ptp_bvc_state := new_state;
Harald Welte199f3862020-11-15 22:37:45 +01001377 if (MGMT.checkstate("Connected")) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001378 MGMT.send(ts_BssgpStsInd(g_nsei, g_cfg.bvci, g_ptp_bvc_state));
Harald Welte199f3862020-11-15 22:37:45 +01001379 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001380 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1381 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001382 BSSGP_SP.send(ts_BssgpStsInd(g_nsei, g_cfg.bvci, g_ptp_bvc_state)) to ClientTable[i].comp_ref;
Harald Welte5339b2e2020-10-04 22:52:56 +02001383 }
1384 }
Harald Welte5ac31492018-02-15 20:39:13 +01001385}
1386
1387/* attempt to extract the TLLI from a BSSGP PDU */
1388function f_bssgp_get_tlli(PDU_BSSGP bssgp) return template OCT4 {
1389 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
1390 return bssgp.pDU_BSSGP_DL_UNITDATA.tLLI_current;
1391 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
1392 return bssgp.pDU_BSSGP_UL_UNITDATA.tLLI;
1393 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY)) {
1394 return bssgp.pDU_BSSGP_RA_CAPABILITY.tLLI.tLLI_Value;
1395 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE)) {
1396 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE.tLLI.tLLI_Value;
1397 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK)) {
1398 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK.tLLI.tLLI_Value;
Harald Welte650bd722021-01-17 10:58:56 +01001399 } else if (ischosen(bssgp.pDU_BSSGP_RADIO_STATUS) and
1400 ispresent(bssgp.pDU_BSSGP_RADIO_STATUS.tLLI)) {
Harald Welte5ac31492018-02-15 20:39:13 +01001401 return bssgp.pDU_BSSGP_RADIO_STATUS.tLLI.tLLI_Value;
1402 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND)) {
1403 return bssgp.pDU_BSSGP_SUSPEND.tLLI.tLLI_Value;
1404 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_ACK)) {
1405 return bssgp.pDU_BSSGP_SUSPEND_ACK.tLLI.tLLI_Value;
1406 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_NACK)) {
1407 return bssgp.pDU_BSSGP_SUSPEND_NACK.tLLI.tLLI_Value;
1408 } else if (ischosen(bssgp.pDU_BSSGP_RESUME)) {
1409 return bssgp.pDU_BSSGP_RESUME.tLLI.tLLI_Value;
1410 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_ACK)) {
1411 return bssgp.pDU_BSSGP_RESUME_ACK.tLLI.tLLI_Value;
1412 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_NACK)) {
1413 return bssgp.pDU_BSSGP_RESUME_NACK.tLLI.tLLI_Value;
1414 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL)) {
1415 return bssgp.pDU_BSSGP_FLUSH_LL.tLLI.tLLI_Value;
1416 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL_ACK)) {
1417 return bssgp.pDU_BSSGP_FLUSH_LL_ACK.tLLI.tLLI_Value;
1418 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1419 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1420 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1421 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1422 } else if (ischosen(bssgp.pDU_BSSGP_PAGING_CS) and
1423 isvalue(bssgp.pDU_BSSGP_PAGING_CS.tLLI)) {
1424 return bssgp.pDU_BSSGP_PAGING_CS.tLLI.tLLI_Value;
1425 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS)) {
1426 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS.tLLI.tLLI_Value;
1427 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK)) {
1428 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK.tLLI.tLLI_Value;
1429 }
1430 /* TODO: Handover, PFC, LCS */
1431 return omit;
1432}
1433
Harald Weltef70997d2018-02-17 10:11:19 +01001434
Harald Welte5ac31492018-02-15 20:39:13 +01001435
Harald Welte1733a382017-07-23 17:26:17 +02001436}