blob: 593abd8da84b6b77a34faf21d81c1bb623b97030 [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 {
Harald Welte57de2202020-11-24 18:15:02 +0100326 GLOBAL.send(udi.bssgp);
327 }
Harald Welte0083ea62020-12-02 21:30:44 +0100328 [] BSCP.receive(f_BnsUdInd(tr_RIM, 0)) -> value udi {
329 if (RIM.checkstate("Connected")) {
330 RIM.send(udi.bssgp);
331 }
332 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200333
334 /* Route based on PTP BVCI in payload/IE of signaling PDU */
335 [] BSCP.receive(f_BnsUdInd(?, 0)) -> value udi {
336 var template (omit) BssgpBvci ptp_bvci := f_BSSGP_BVCI_get(udi.bssgp);
337 if (istemplatekind(ptp_bvci, "omit")) {
338 log("Rx on SIG BVCI without PTP BVCI: broadcasting");
339 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
340 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
341 BVC.send(udi) to BvcTable[i].comp_ref;
342 }
343 }
344 } else {
345 bvc_comp_ref := f_comp_for_bvci(valueof(ptp_bvci));
346 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
347 /* dispatch to BVC component */
348 BVC.send(udi) to bvc_comp_ref;
349 } else {
350 setverdict(fail, "Rx SIG BSSGP for unknown PTP BVCI ", ptp_bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100351 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 +0100352 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200353 }
354 }
355
356 /* Handle PS-PAGING on SIGN BVCI with no conditional BVCI IE */
357 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_PS:=?}, 0)) -> value udi {
358 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
359 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
360 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
361 BVC.send(udi) to BvcTable[i].comp_ref;
362 }
363 }
364 }
365 /* Handle CS-PAGING on SIGN BVCI with no conditional BVCI IE */
366 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_CS:=?}, 0)) -> value udi {
367 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
368 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
369 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
370 BVC.send(udi) to BvcTable[i].comp_ref;
371 }
372 }
373 }
374
375 /* per-BVC component sends us something; forward to NS without any validation */
376 [] BVC.receive(NsUnitdataRequest:?) -> value udr {
377 /* patch in the NSEI, as the per-BVC components have no idea about it */
378 udr.nsei := g_cfg.nsei;
379 BSCP.send(udr);
380 }
Harald Welte57de2202020-11-24 18:15:02 +0100381
Harald Welte27a70cc2020-12-09 11:57:41 +0100382 [] GLOBAL.receive(tr_GLOBAL_SIG) -> value bssgp {
Harald Welte57de2202020-11-24 18:15:02 +0100383 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
384 }
Harald Welte0083ea62020-12-02 21:30:44 +0100385
386 [] RIM.receive(tr_RIM) -> value bssgp {
387 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
388 }
389
Harald Welte5339b2e2020-10-04 22:52:56 +0200390}
391
392/* We are in BVC_S_WAIT_NS_ALIVE_UNBLOCKED (only happens in BSS role) */
393altstep as_sig_wait_ns_alive_unblocked() runs on BSSGP_CT {
394 var NsStatusIndication nsi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100395 [] 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 +0200396 /* if we just became NS-unblocked, send a BCC-RESET */
397 if (g_cfg.sgsn_role == false) {
Harald Weltec4505522020-11-11 18:55:09 +0100398 BSCP.send(f_BnsUdReq(ts_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, 0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200399 g_T2.start;
400 /* The BVC_RESET_ACK is handled in the as_sig_allstate() below */
401 }
402 /* Idea: We could send BVC-UNBLOCK here like some SGSN do */
403 }
404
405}
406
Harald Weltee5887922020-11-27 19:04:46 +0100407/* handling of events irrespective of BVC state (before state-specific handling) */
408altstep as_sig_allstate_pre() runs on BSSGP_CT {
409 var NsUnitdataIndication udi;
410
411 /* Respond to RESET for signalling BVCI 0 */
412 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
413 log("Rx BVC-RESET for Signaling BVCI=0");
414 if (MGMT.checkstate("Connected")) {
415 MGMT.send(BssgpResetIndication:{0});
416 }
417 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
418 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
419 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
420 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
421 }
422 }
423 }
424
425 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100426 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(0, ?), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100427 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
428 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100429 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100430 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
431 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100432 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK_ACK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100433 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
434 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100435 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
Harald Weltee5887922020-11-27 19:04:46 +0100436 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
437 }
438
439 /* BVC-RESET-ACK for BVCI=0 while T2 is running: Answer to a BVC-RESET we sent earlier */
440 [g_T2.running] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(0, omit), 0)) -> value udi {
441 log("BVCI(0) Rx BVC-RESET-ACK");
442 g_T2.stop;
443 f_sign_change_state(BVC_S_UNBLOCKED);
444 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
445 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
446 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
447 }
448 }
449 }
450 [] g_T2.timeout {
451 setverdict(fail, "Timeout waiting for BVC-RESET-ACK on BVCI=0");
452 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
453 g_T2.start;
454 }
455}
456
457/* handling of events irrespective of BVC state (after state-specific handling) */
458altstep as_sig_allstate_post() runs on BSSGP_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200459 var BSSGP_Client_CT vc_conn;
460 var NsUnitdataIndication udi;
461 var NsStatusIndication nsi;
462 var ASP_Event evt;
463 var BSSGP_BVC_CT bvc_comp_ref;
464 var BssgpBvci bvci;
Harald Weltee5887922020-11-27 19:04:46 +0100465 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +0200466
467 /* Respond to RESET for signalling BVCI 0 */
468 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
469 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Weltebdf96d82020-11-27 18:58:46 +0100470 if (MGMT.checkstate("Connected")) {
471 MGMT.send(BssgpResetIndication:{0});
472 }
Harald Weltec4505522020-11-11 18:55:09 +0100473 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200474 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
475 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
476 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
477 }
478 }
479 }
480
481 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100482 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(0, ?), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200483 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
484 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100485 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200486 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
487 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100488 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK_ACK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200489 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
490 }
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100491 [] BSCP.receive(f_BnsUdInd(tr_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200492 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
493 }
494
495 /* Respond to BLOCK for wrong BVCI */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +0100496 [] BSCP.receive(f_BnsUdInd(tr_BVC_BLOCK(?, ?), 0)) -> value udi {
Harald Welte5339b2e2020-10-04 22:52:56 +0200497 setverdict(fail, "Rx BVC-BLOCK for unknown BVCI");
Harald Weltec4505522020-11-11 18:55:09 +0100498 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200499 }
500
501 /* Respond to RESET with wrong BVCI */
502 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, ?, ?), 0)) -> value udi {
503 var BssgpBvci ptp_bvci := oct2int(udi.bssgp.pDU_BSSGP_BVC_RESET.bVCI.unstructured_value);
504 setverdict(fail, "Rx BVC-RESET for unknown BVCI ", ptp_bvci);
Harald Weltec4505522020-11-11 18:55:09 +0100505 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(ptp_bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200506 }
507
Harald Welte5339b2e2020-10-04 22:52:56 +0200508 /* Forwarding of ASP_Event to per-BVC components */
509 [] BSCP.receive(ASP_Event:?) -> value evt {
510 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
511 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
512 BVC.send(evt) to BvcTable[i].comp_ref;
513 }
514 }
515 }
516 /* Keep NS Status Indicaitons to us; no need to inform per-BVC components [for now?] */
Harald Weltedc0a0902020-11-10 21:03:29 +0100517 [] BSCP.receive(tr_NsStsInd(g_cfg.nsei)) -> value nsi { }
Harald Welte445fc612020-11-10 19:15:30 +0100518 /* We should never see any different NSEI: There's one BSSGP_CT per NSE */
Harald Weltedc0a0902020-11-10 21:03:29 +0100519 [] BSCP.receive(tr_NsStsInd(?)) -> value nsi {
Harald Welte445fc612020-11-10 19:15:30 +0100520 setverdict(fail, "Rx NsStatusInd for wrong NSEI ", nsi);
521 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200522
523 /* Procedure port request to resolve the per-BVC component for a given ptp BVCI */
524 [] PROC.getcall(BSSGP_get_bvci_ct:{?}) -> param(bvci) sender vc_conn {
525 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
526 if (BvcTable[i].bvci == bvci) {
527 PROC.reply(BSSGP_get_bvci_ct:{bvci} value BvcTable[i].comp_ref) to vc_conn;
528 return;
529 }
530 }
531 }
Harald Weltee5887922020-11-27 19:04:46 +0100532
533 /* default case of handling unknown PDUs */
534 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
535 setverdict(fail, "Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_sign_bvc_state);
536 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp), 0, 0));
537 }
538
539 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
540 BSCP.send(f_BnsUdReq(ts_BVC_RESET(brr.cause, 0, omit), 0, 0));
541 g_T2.start;
542 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200543}
544
545/* send the highest decoded layer of the message through given port */
546private function f_send_bssgp_dec(BssgpDecoded dec, BSSGP_Client_CT vc_conn, BSSGP_SP_PT pt) {
547#ifdef BSSGP_EM_L3
548 if (ispresent(dec.l3_mt)) {
549 pt.send(dec.l3_mt) to vc_conn;
550 } else if (ispresent(dec.l3_mo)) {
551 pt.send(dec.l3_mo) to vc_conn;
552 } else
553#endif
554 if (ispresent(dec.sndcp)) {
555 pt.send(dec.sndcp) to vc_conn;
556 } else if (ispresent(dec.llc)) {
557 pt.send(dec.llc) to vc_conn;
558 } else {
559 pt.send(dec.bssgp) to vc_conn;
560 }
561}
562
563
564function f_llc_get_n_u_tx(inout LLC_Entity llc) return uint9_t {
565 var uint9_t ret := llc.n_u_tx_next;
566 llc.n_u_tx_next := llc.n_u_tx_next + 1;
567 return ret;
568}
569
570#ifdef BSSGP_EM_L3
571function f_llc_sapi_by_l3_mo(PDU_L3_MS_SGSN l3_mo) return BIT4 {
572 if (ischosen(l3_mo.msgs.gprs_mm)) {
573 return c_LLC_SAPI_LLGMM;
574 } else if (ischosen(l3_mo.msgs.gprs_sm)) {
575 return c_LLC_SAPI_LLGMM;
576 } else if (ischosen(l3_mo.msgs.sms)) {
577 return c_LLC_SAPI_LLSMS;
578 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100579 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mo));
580 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200581}
582
583private function f_llc_sapi_by_l3_mt(PDU_L3_SGSN_MS l3_mt) return BIT4 {
584 if (ischosen(l3_mt.msgs.gprs_mm)) {
585 return c_LLC_SAPI_LLGMM;
586 } else if (ischosen(l3_mt.msgs.gprs_sm)) {
587 return c_LLC_SAPI_LLGMM;
588 } else if (ischosen(l3_mt.msgs.sms)) {
589 return c_LLC_SAPI_LLSMS;
590 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100591 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mt));
592 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200593}
594#endif
595
596/* main loop of BSSGP_CT */
597private function f_bssgp_ScanEvents() runs on BSSGP_CT {
598 while (true) {
599 alt {
600 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
Harald Weltee5887922020-11-27 19:04:46 +0100601 [] as_sig_allstate_pre();
Harald Welte5339b2e2020-10-04 22:52:56 +0200602 [g_sign_bvc_state == BVC_S_WAIT_NS_ALIVE_UNBLOCKED] as_sig_wait_ns_alive_unblocked();
603 [g_sign_bvc_state == BVC_S_UNBLOCKED] as_sig_unblocked();
Harald Weltee5887922020-11-27 19:04:46 +0100604 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
605 [] as_sig_allstate_post();
Harald Welte5339b2e2020-10-04 22:52:56 +0200606 }
607 } /* while */
608}
609
610/* generate a send template. Cannot be a real template as we want to use g_cfg.nsei */
Harald Weltec4505522020-11-11 18:55:09 +0100611private function f_BnsUdReq(template PDU_BSSGP pdu, BssgpBvci bvci, integer lsp)
Harald Welte5339b2e2020-10-04 22:52:56 +0200612runs on BSSGP_CT return NsUnitdataRequest {
613 var NsUnitdataRequest udr := {
614 bvci := bvci,
615 nsei := g_cfg.nsei,
Harald Weltec4505522020-11-11 18:55:09 +0100616 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +0200617 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
618 * unbound integer value." when trying to send the reocrd rather than the octetstring */
619 //sdu := omit,
620 //bssgp := valueof(pdu)
621 sdu := enc_PDU_BSSGP(valueof(pdu)),
622 bssgp := omit
623 }
624 return udr;
625}
626
627/* generate a receive template. Cannot be a real template as we want to use g_cfg.nsei */
628private function f_BnsUdInd(template PDU_BSSGP pdu, template BssgpBvci bvci)
629runs on BSSGP_CT return template (present) NsUnitdataIndication {
630 var template (present) NsUnitdataIndication udi := {
631 bvci := bvci,
632 nsei := g_cfg.nsei,
Harald Welte80a249a2020-11-17 19:57:40 +0100633 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +0200634 sdu := *,
635 bssgp := pdu
636 }
637 return udi;
638}
639
640/* change state of signaling BVCI; notify per-BVC components */
641private function f_sign_change_state(BvcState new_state) runs on BSSGP_CT {
642 if (new_state == g_sign_bvc_state) {
643 return;
644 }
645 log("BVCI(0) State Transition: ", g_sign_bvc_state, " -> ", new_state);
646 g_sign_bvc_state := new_state;
647 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
648 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
649 BVC.send(ts_BssgpStsInd(g_cfg.nsei, 0, g_sign_bvc_state)) to BvcTable[i].comp_ref;
650 }
651 }
652}
653
654/* User wants to get per-BVC component reference from BSSGP_CT */
655signature BSSGP_get_bvci_ct(BssgpBvci bvci) return BSSGP_BVC_CT;
656
657type port BSSGP_CT_PROC_PT procedure {
658 inout BSSGP_get_bvci_ct
659} with { extension "internal" };
660
661/* convenience wrapper function for user */
662function f_bssgp_get_bvci_ct(BssgpBvci bvci, BSSGP_CT_PROC_PT PT) return BSSGP_BVC_CT {
663 var BSSGP_BVC_CT res;
664 PT.call(BSSGP_get_bvci_ct:{bvci}) {
665 [] PT.getreply(BSSGP_get_bvci_ct:{bvci})-> value res {
666 return res;
667 }
668 }
669}
670
671/***********************************************************************
672 * per-BVC (Cell) Component
673 ***********************************************************************
674 * Any number of these components runs on top of the BSSGP_CT, each
675 * representing one PTP BVC within this NSE. Users (test cases) can
676 * register with TLLI and/or IMSI via the procedure port.
677 ***********************************************************************/
678
679/* per-BVC component. Exists once per cell within a BSSGP_CT */
680type component BSSGP_BVC_CT {
681 /* port towards the underlying BSSGP_CT */
682 port BSSGP_BVC_PT BVC;
683
Harald Welte199f3862020-11-15 22:37:45 +0100684 /* port to a management instance */
685 port BSSGP_BVC_MGMT_SP_PT MGMT;
686
Harald Welte71449b02020-12-09 11:58:23 +0100687 /* per-BVC global port for e.g. BVC Flow Control */
688 port BSSGP_SP_PT GLOBAL;
689
690 /* BSSGP-User SAP towards the user (per-TLLI, Client) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100691 port BSSGP_SP_PT BSSGP_SP;
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200692 port BSSGP_SP_PT BSSGP_SP_SIG;
Harald Welte5ac31492018-02-15 20:39:13 +0100693 port BSSGP_PROC_PT BSSGP_PROC;
Harald Welte853c0ad2018-02-15 17:45:29 +0100694
Harald Welte5339b2e2020-10-04 22:52:56 +0200695 var BssgpBvcConfig g_cfg;
696 var boolean g_sgsn_role;
Harald Weltefba7afd2020-11-24 23:12:31 +0100697 var Nsei g_nsei;
Harald Welte5ac31492018-02-15 20:39:13 +0100698
Harald Weltec4505522020-11-11 18:55:09 +0100699 /* default Link Selector Parameter for this BVC (for traffic unrelated to a TLLI) */
700 var integer g_bvc_lsp;
701
Harald Welte853c0ad2018-02-15 17:45:29 +0100702 var BvcState g_ptp_bvc_state := BVC_S_BLOCKED;
703 timer g_T1 := 15.0;
704 timer g_T2 := 60.0;
Harald Welte5339b2e2020-10-04 22:52:56 +0200705 var boolean g_t1_waits_for_block_ack := false;
Harald Welte199f3862020-11-15 22:37:45 +0100706 /* for re-transmissions */
707 var BssgpCause g_last_block_cause;
708 var BssgpCause g_last_reset_cause;
Harald Welte5ac31492018-02-15 20:39:13 +0100709
Harald Welte633805b2021-02-03 17:47:18 +0100710 var ClientEntity ClientTable[64];
Harald Welte5339b2e2020-10-04 22:52:56 +0200711};
Harald Welte853c0ad2018-02-15 17:45:29 +0100712
Harald Welte5339b2e2020-10-04 22:52:56 +0200713/* port between global BSSGP_CT and per-BVC BSSGP_BVC_CT */
714type port BSSGP_BVC_SP_PT message {
715 in NsUnitdataRequest;
716 out ASP_Event,
717 BssgpStatusIndication,
718 BssgpResetIndication,
719 NsUnitdataIndication;
720} with { extension "internal" };
721type port BSSGP_BVC_PT message {
722 in ASP_Event,
723 BssgpStatusIndication,
724 BssgpResetIndication,
725 NsUnitdataIndication;
726 out NsUnitdataRequest;
727} with { extension "internal" };
Alexander Couzens6b449fb2018-07-31 14:19:36 +0200728
Harald Welte199f3862020-11-15 22:37:45 +0100729/* port between BSSGP_BVC_CT and a management instance */
730type port BSSGP_BVC_MGMT_SP_PT message {
731 in BssgpResetRequest,
732 BssgpBlockRequest,
733 BssgpUnblockRequest;
734 out BssgpStatusIndication,
735 BssgpResetIndication;
736} with { extension "internal" };
737type port BSSGP_BVC_MGMT_PT message {
738 in BssgpStatusIndication,
739 BssgpResetIndication;
740 out BssgpResetRequest,
741 BssgpBlockRequest,
742 BssgpUnblockRequest;
743} with { extension "internal" };
744
745type record BssgpResetRequest {
746 BssgpCause cause
747};
748type record BssgpBlockRequest {
749 BssgpCause cause
750};
751type record BssgpUnblockRequest {
752};
Harald Welte5339b2e2020-10-04 22:52:56 +0200753
754/* one element in the per-TLLI state table */
Harald Welte5ac31492018-02-15 20:39:13 +0100755type record ClientEntity {
756 OCT4 tlli,
757 OCT4 tlli_old optional,
758 hexstring imsi,
Harald Welte5ac31492018-02-15 20:39:13 +0100759 BSSGP_Client_CT comp_ref,
760 /* LLC entities, one for each SAPI */
761 LLC_Entity llc[16]
Harald Welte853c0ad2018-02-15 17:45:29 +0100762};
Harald Welte5ac31492018-02-15 20:39:13 +0100763type record LLC_Entity {
764 boolean sgsn_role,
765 /* N(U) on transmit side for next PDU */
766 uint9_t n_u_tx_next,
767 /* N(U) on receive side, last PDU */
768 uint9_t n_u_rx_last optional
769};
Harald Welte5339b2e2020-10-04 22:52:56 +0200770type record length(16) of LLC_Entity LLC_Entities;
Harald Welte5ac31492018-02-15 20:39:13 +0100771
Harald Weltef5365242021-01-17 13:46:57 +0100772private template PDU_BSSGP tr_GLOBAL_PTP := (
773 {pDU_BSSGP_STATUS:=?}
774);
775
Alexander Couzenscdfb7512018-07-31 15:37:14 +0200776function f_llc_create(boolean sgsn_role := false) return LLC_Entities {
777 var LLC_Entities llc;
778 for (var integer i := 0; i < 16; i := i+1) {
779 llc[i] := valueof(t_LLC_init(sgsn_role));
780 }
781 return llc;
782}
783
Harald Welte5ac31492018-02-15 20:39:13 +0100784private template LLC_Entity t_LLC_init(boolean sgsn_role := false) := {
785 sgsn_role := sgsn_role,
786 n_u_tx_next := 0,
787 n_u_rx_last := -
788}
789
Harald Welte5339b2e2020-10-04 22:52:56 +0200790/* configuration of BSSGP_CT */
791type record BssgpConfig {
792 Nsvci nsei,
793 boolean sgsn_role,
794 BssgpBvcConfigs bvc
795};
796/* Configuration of one BVC (Cell) BSSGP_BVC_CT */
797type record BssgpBvcConfig {
798 BssgpBvci bvci,
799 BssgpCellId cell_id,
Harald Welte4d112c92020-11-12 19:48:31 +0100800 BssgpDecodeDepth depth,
801 BssgpCreateCallback create_cb
Harald Welte5339b2e2020-10-04 22:52:56 +0200802};
803type record of BssgpBvcConfig BssgpBvcConfigs;
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200804type enumerated BssgpDecodeDepth {
805 BSSGP_DECODE_DEPTH_BSSGP,
806 BSSGP_DECODE_DEPTH_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200807 BSSGP_DECODE_DEPTH_SNDCP
808#ifdef BSSGP_EM_L3
809 ,
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200810 BSSGP_DECODE_DEPTH_L3
Harald Welte93331e72020-09-12 20:51:05 +0200811#endif
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200812};
Harald Welte4d112c92020-11-12 19:48:31 +0100813/* call-back function type: Called for inbound BSSGP for unknown TLLIs; could create BSSGP_ClienT_CT */
814type function BssgpCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT;
815
816/* Default Create Callback function: Fail and terminate */
817function DefaultCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100818 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for TLLI ", tlli));
Harald Welte4d112c92020-11-12 19:48:31 +0100819}
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200820
Harald Welte5339b2e2020-10-04 22:52:56 +0200821/*
822private function f_tbl_init() runs on BSSGP_BVC_CT {
823 var integer i;
824 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
825 ImsiTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100826 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100827
Harald Welte5339b2e2020-10-04 22:52:56 +0200828 for (i := 0; i < sizeof(TlliTable); i := i+1) {
829 TlliTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100830 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100831}
Harald Welte5339b2e2020-10-04 22:52:56 +0200832*/
Harald Welte853c0ad2018-02-15 17:45:29 +0100833
Harald Welte5339b2e2020-10-04 22:52:56 +0200834private function f_tbl_client_add(hexstring imsi, OCT4 tlli, BSSGP_Client_CT vc_conn)
835runs on BSSGP_BVC_CT {
836 var integer i;
837 for (i := 0; i < sizeof(ClientTable); i := i+1) {
838 if (not isvalue(ClientTable[i].comp_ref)) {
839 log("Adding Client=", vc_conn, ", IMSI=", imsi, ", TLLI=", tlli, ", index=", i);
840 ClientTable[i] := {
841 tlli := tlli,
842 tlli_old := omit,
843 imsi := imsi,
844 comp_ref := vc_conn,
845 llc := -
846 };
847 for (var integer j := 0; j < sizeof(ClientTable[i].llc); j := j+1) {
848 ClientTable[i].llc[j] := valueof(t_LLC_init(g_sgsn_role));
849 }
850 return;
Harald Welte5ac31492018-02-15 20:39:13 +0100851 }
852 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200853 testcase.stop("Client Table full");
Harald Welte853c0ad2018-02-15 17:45:29 +0100854}
855
Harald Welte5339b2e2020-10-04 22:52:56 +0200856private function f_tbl_client_del(hexstring imsi, BSSGP_Client_CT vc_conn) runs on BSSGP_BVC_CT {
857 var integer i;
858 for (i := 0; i < sizeof(ClientTable); i := i+1) {
859 if (isvalue(ClientTable[i].imsi) and ClientTable[i].imsi == imsi) {
860 if (ClientTable[i].comp_ref != vc_conn) {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100861 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
862 log2str("Cannot unregister index=", i, " IMSI ",
863 imsi, " registred to ", ClientTable[i].comp_ref,
864 " from ", vc_conn));
Harald Welte5339b2e2020-10-04 22:52:56 +0200865 }
866 log("Removing Client IMSI=", imsi, ", index=", i);
867 ClientTable[i] := {
868 tlli := -,
869 tlli_old := omit,
870 imsi := ''H,
871 comp_ref := null,
872 llc := - };
873 return;
874 }
875 }
876 log("Warning: Could not find client for IMSI ", imsi);
877 return;
878}
879
880/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
881private function f_tbl_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_Client_CT vc_conn)
882runs on BSSGP_BVC_CT {
883 var integer i := f_tbl_idx_by_comp(vc_conn);
884
885 if (tlli_old == 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
886 /* TLLI assignment */
887 ClientTable[i].tlli := tlli_new;
888 ClientTable[i].tlli_old := omit;
889 } else if (tlli_old != 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
890 /* TLLI change: both active */
891 ClientTable[i].tlli := tlli_new;
892 ClientTable[i].tlli_old := tlli_old;
893 } else if (tlli_old != 'FFFFFFFF'O and tlli_new == 'FFFFFFFF'O) {
894 /* TLLI unassignment: old shall be unassigned; new stays */
895 ClientTable[i].tlli_old := omit;
896 }
897}
898
899private function f_tbl_comp_by_imsi(hexstring imsi) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
900 var integer i;
901 for (i := 0; i < sizeof(ClientTable); i := i+1) {
902 if (isvalue(ClientTable[i].imsi) and isvalue(ClientTable[i].comp_ref)
903 and ClientTable[i].imsi == imsi) {
904 return ClientTable[i].comp_ref;
905 }
906 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100907 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for IMSI ", imsi));
908 return ClientTable[0].comp_ref;
Harald Welte5339b2e2020-10-04 22:52:56 +0200909}
910
911private function f_tbl_comp_by_tlli(OCT4 tlli) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
912 var integer i;
913 for (i := 0; i < sizeof(ClientTable); i := i+1) {
914 if (isvalue(ClientTable[i].comp_ref) and
915 (isvalue(ClientTable[i].tlli) and (ClientTable[i].tlli == tlli or
916 isvalue(ClientTable[i].tlli_old) and ClientTable[i].tlli_old == tlli) )) {
917 return ClientTable[i].comp_ref;
918 }
919 }
Harald Welte4d112c92020-11-12 19:48:31 +0100920 return null;
Harald Welte5339b2e2020-10-04 22:52:56 +0200921}
922
923private function f_tbl_idx_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return integer {
924 var integer i;
925 for (i := 0; i < sizeof(ClientTable); i := i+1) {
926 if (isvalue(ClientTable[i].comp_ref) and ClientTable[i].comp_ref == comp_ref) {
927 return i;
928 }
929 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100930 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Client for Component ", comp_ref));
931 return 1;
Harald Welte5339b2e2020-10-04 22:52:56 +0200932}
933
934private function f_tbl_tlli_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return OCT4 {
935 var integer i;
936 for (i := 0; i < sizeof(ClientTable); i := i+1) {
937 if (isvalue(ClientTable[i].tlli) and isvalue(ClientTable[i].comp_ref)
938 and ClientTable[i].comp_ref == comp_ref) {
939 return ClientTable[i].tlli;
940 }
941 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100942 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find TLLI for Component ", comp_ref));
943 return '00000000'O;
Harald Welte5339b2e2020-10-04 22:52:56 +0200944}
945
946/* PDU_BSSGP enhanced with LLC and possibly L3 decoded payloads */
947type record BssgpDecoded {
948 PDU_BSSGP bssgp,
949 PDU_LLC llc optional,
950#ifdef BSSGP_EM_L3
951 PDU_L3_MS_SGSN l3_mo optional,
952 PDU_L3_SGSN_MS l3_mt optional,
953#endif
954 PDU_SN sndcp optional
955}
956
957/* Decode a PDU_BSSGP into a BssgpDecoded (i.e. with LLC/L3 decoded, as applicable) */
958private function f_dec_bssgp(PDU_BSSGP bssgp) runs on BSSGP_BVC_CT return BssgpDecoded {
959 var BssgpDecoded dec := {
960 bssgp := bssgp,
961 llc := omit,
962#ifdef BSSGP_EM_L3
963 l3_mo := omit,
964 l3_mt := omit,
965#endif
966 sndcp := omit
967 };
968
969 /* Decode LLC, if it is a PDU that contains LLC */
970 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_LLC) {
971 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
972 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_DL_UNITDATA.lLC_PDU.lLC_PDU);
973 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
974 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_UL_UNITDATA.lLC_PDU.lLC_PDU);
975 }
976 }
977
978 /* Decode SNDCP, if it is a LLC PDU containing user plane data */
979 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_SNDCP) {
980 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_USER)) {
981 dec.sndcp := dec_PDU_SN(dec.llc.pDU_LLC_UI.information_field_UI);
982 }
983 }
984
985#ifdef BSSGP_EM_L3
986 /* Decode L3, if it is a LLC PDU containing L3 */
987 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_L3) {
988 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_L3)) {
989 if (g_sgsn_role) {
990 dec.l3_mo := dec_PDU_L3_MS_SGSN(dec.llc.pDU_LLC_UI.information_field_UI);
991 } else {
992 dec.l3_mt := dec_PDU_L3_SGSN_MS(dec.llc.pDU_LLC_UI.information_field_UI);
993 }
994 }
995 }
996#endif
997
998 return dec;
999}
1000
1001private function f_ptp_sendUnblock() runs on BSSGP_BVC_CT {
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001002 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001003 g_t1_waits_for_block_ack := false;
1004 g_T1.start;
1005}
1006
1007private function f_ptp_sendBlock(BssgpCause cause) runs on BSSGP_BVC_CT {
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001008 BVC.send(ts_ptp_BnsUdReq(ts_BVC_BLOCK(g_cfg.bvci, cause), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001009 g_t1_waits_for_block_ack := true;
1010 g_T1.start;
Harald Welte199f3862020-11-15 22:37:45 +01001011 g_last_block_cause := cause;
Harald Welte5339b2e2020-10-04 22:52:56 +02001012}
1013
1014private function f_ptp_sendStatus(BssgpCause cause, PDU_BSSGP pdu) runs on BSSGP_BVC_CT {
1015 /* FIXME: Make sure correct Signaling or PTP BVCI is used! */
Harald Weltec4505522020-11-11 18:55:09 +01001016 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 +02001017}
1018
Harald Welte199f3862020-11-15 22:37:45 +01001019private function f_ptp_sendReset(BssgpCause cause := BSSGP_CAUSE_OM_INTERVENTION) runs on BSSGP_BVC_CT {
Harald Weltec967d0e2020-09-14 16:07:26 +02001020 var PDU_BSSGP pdu;
1021
1022 /* The Cell Identifier IE is mandatory in the BVC-RESET PDU sent from BSS to
1023 * SGSN in order to reset a BVC corresponding to a PTP functional entity. The
1024 * Cell Identifier IE shall not be used in any other BVC-RESET PDU. */
Harald Welte5339b2e2020-10-04 22:52:56 +02001025 if (g_sgsn_role) {
Harald Welte199f3862020-11-15 22:37:45 +01001026 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, omit));
Harald Weltec967d0e2020-09-14 16:07:26 +02001027 } else {
Harald Welte199f3862020-11-15 22:37:45 +01001028 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, g_cfg.cell_id));
Harald Weltec967d0e2020-09-14 16:07:26 +02001029 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001030
1031 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
Harald Weltec4505522020-11-11 18:55:09 +01001032 BVC.send(ts_ptp_BnsUdReq(pdu, 0, g_bvc_lsp));
Harald Welte853c0ad2018-02-15 17:45:29 +01001033 g_T2.start;
1034 //f_change_state(BVC_S_WAIT_RESET);
Harald Welte199f3862020-11-15 22:37:45 +01001035 g_last_reset_cause := cause;
Harald Welte853c0ad2018-02-15 17:45:29 +01001036}
1037
Harald Welte5339b2e2020-10-04 22:52:56 +02001038/* PTP-BVC is in BVC_S_BLOCKED state */
1039private altstep as_ptp_blocked() runs on BSSGP_BVC_CT {
1040 var NsUnitdataIndication udi;
1041
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001042 [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 +02001043 g_T1.stop;
1044 f_ptp_change_state(BVC_S_UNBLOCKED);
1045 }
1046
Harald Welteddfc6672020-11-24 23:13:01 +01001047 /* Inbound BVC-UNBLOCK from peer */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001048 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_UNBLOCK(g_cfg.bvci), 0)) {
1049 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welteddfc6672020-11-24 23:13:01 +01001050 f_ptp_change_state(BVC_S_UNBLOCKED);
1051 }
1052
Harald Welte5339b2e2020-10-04 22:52:56 +02001053 /* RESET-ACK before T2 timeout: reset successful. In SGSN role, CellID must be present */
1054 [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 {
1055 g_T2.stop;
1056 f_ptp_change_state(BVC_S_UNBLOCKED);
1057 }
1058 /* RESET-ACK before T2 timeout: reset successful. In BSS role, CellID must be absent */
1059 [g_T2.running and not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, omit), 0)) -> value udi {
1060 g_T2.stop;
1061 f_ptp_change_state(BVC_S_UNBLOCKED);
1062 }
1063
Harald Welteab50cc22020-11-25 17:11:08 +01001064 [] as_ptp_handle_inbound_reset();
1065
Harald Welte5339b2e2020-10-04 22:52:56 +02001066 [] g_T2.timeout {
1067 /* BVC-RESET-ACK PDU was not received within T2: retransmit */
Harald Welte199f3862020-11-15 22:37:45 +01001068 f_ptp_sendReset(g_last_reset_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001069 }
Harald Welte199f3862020-11-15 22:37:45 +01001070
1071 [] MGMT.receive(BssgpUnblockRequest:?) {
1072 f_ptp_sendUnblock();
1073 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001074}
1075
Harald Welte5339b2e2020-10-04 22:52:56 +02001076/* PTP-BVC is in UNBLOCKED state */
1077private altstep as_ptp_unblocked() runs on BSSGP_BVC_CT {
1078 var NsUnitdataIndication udi;
1079 var NsStatusIndication nsi;
Harald Welte199f3862020-11-15 22:37:45 +01001080 var BssgpBlockRequest bbr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001081 var BSSGP_Client_CT vc_conn;
1082 var ASP_Event evt;
1083 var PDU_BSSGP bs_pdu;
1084 var PDU_LLC llc;
1085#ifdef BSSGP_EM_L3
1086 var PDU_L3_MS_SGSN l3_mo;
1087 var PDU_L3_SGSN_MS l3_mt;
1088#endif
1089
1090 /* bogus unblock, just respond with ACK */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001091 [] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_UNBLOCK(g_cfg.bvci), 0)) -> value udi {
1092 BVC.send(ts_ptp_BnsUdReq(ts_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001093 }
1094 /* Respond to BLOCK with BLOCK-ACK + change state */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001095 [] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_BLOCK(g_cfg.bvci, ?), 0)) -> value udi {
1096 BVC.send(ts_ptp_BnsUdReq(ts_BVC_BLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001097 g_T1.stop;
1098 f_ptp_change_state(BVC_S_BLOCKED);
1099 }
1100 /* BLOCK-ACK before T1 timeout: mark as blocked */
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001101 [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 +02001102 g_T1.stop;
1103 f_ptp_change_state(BVC_S_BLOCKED);
1104 }
1105 /* Re-transmit BLOCK / UNBLOCK on T1 timeout */
1106 [not g_t1_waits_for_block_ack] g_T1.timeout {
1107 f_ptp_sendUnblock();
1108 }
1109 [g_t1_waits_for_block_ack] g_T1.timeout {
Harald Welte199f3862020-11-15 22:37:45 +01001110 f_ptp_sendBlock(g_last_block_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001111 }
1112
Harald Welteab50cc22020-11-25 17:11:08 +01001113 [] as_ptp_handle_inbound_reset();
1114
Harald Welte5339b2e2020-10-04 22:52:56 +02001115 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_FC_BVC, g_cfg.bvci)) -> value udi {
Harald Welte71449b02020-12-09 11:58:23 +01001116 if (GLOBAL.checkstate("Connected")) {
1117 GLOBAL.send(udi.bssgp);
1118 } else {
1119 /* simply acknowledge all per-BVC Flow Control Messages */
1120 var OCT1 tag := udi.bssgp.pDU_BSSGP_FLOW_CONTROL_BVC.tag.unstructured_Value;
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001121 BVC.send(ts_ptp_BnsUdReq(ts_BVC_FC_BVC_ACK(tag), g_cfg.bvci, g_bvc_lsp));
Harald Welte71449b02020-12-09 11:58:23 +01001122 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001123 }
Harald Welte71449b02020-12-09 11:58:23 +01001124
Pau Espin Pedrol6ee01262021-02-05 13:05:06 +01001125 [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 +01001126 if (GLOBAL.checkstate("Connected")) {
1127 GLOBAL.send(udi.bssgp);
1128 } else {
1129 /* ignore any incoming flow control ACK */
1130 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001131 }
Harald Welte71449b02020-12-09 11:58:23 +01001132
Harald Weltef5365242021-01-17 13:46:57 +01001133 [] BVC.receive(tr_ptp_BnsUdInd(tr_GLOBAL_PTP, g_cfg.bvci)) -> value udi {
1134 if (GLOBAL.checkstate("Connected")) {
1135 GLOBAL.send(udi.bssgp);
1136 } else {
1137 setverdict(fail, "Received BSSGP STATUS ", udi.bssgp);
1138 }
1139 }
1140
Harald Welte5339b2e2020-10-04 22:52:56 +02001141 /* Any other PTP BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1142 [] BVC.receive(tr_ptp_BnsUdInd(?, g_cfg.bvci)) -> value udi {
1143 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1144 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1145 if (isvalue(tlli)) {
1146 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001147 if (vc_conn == null) {
1148 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1149 } else {
1150 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP);
1151 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001152 } else {
1153 log("No TLLI: Broadcasting ", dec);
1154 /* broadcast this message to all components */
1155 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1156 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1157 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1158 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP);
1159 }
1160 }
1161 }
1162 }
1163
1164 /* Any other SIG BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1165 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1166 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1167 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1168 if (isvalue(tlli)) {
1169 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001170 if (vc_conn == null) {
1171 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1172 } else {
1173 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP_SIG);
1174 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001175 } else {
1176 log("No TLLI: Broadcasting ", dec);
1177 /* broadcast this message to all components */
1178 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1179 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1180 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1181 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP_SIG);
1182 }
1183 }
1184 }
1185 }
1186
1187 /* ConnHdlr sends BSSGP on BVCI=0 port: forward it to BSSGP_CT */
1188 [] BSSGP_SP_SIG.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001189 BVC.send(ts_ptp_BnsUdReq(bs_pdu, 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001190 }
1191
1192 /* ConnHdlr sends BSSGP on BVCI=PTP port: forward it to BSSGP_CT */
1193 [] BSSGP_SP.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001194 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001195 }
1196
1197#ifdef BSSGP_EM_L3
1198 /* ConnHdlr sends L3: Encode and send as UL_UD / DL_UD */
1199 [g_sgsn_role] BSSGP_SP.receive(PDU_L3_SGSN_MS:?) -> value l3_mt sender vc_conn {
1200 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001201 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001202 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(l3_mt);
1203 var BIT4 sapi := f_llc_sapi_by_l3_mt(l3_mt);
1204 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1205 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 +01001206 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001207 }
1208 [not g_sgsn_role] BSSGP_SP.receive(PDU_L3_MS_SGSN:?) -> value l3_mo sender vc_conn {
1209 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001210 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001211 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(l3_mo);
1212 var BIT4 sapi := f_llc_sapi_by_l3_mo(l3_mo);
1213 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1214 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 +01001215 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 +02001216 }
1217#endif
1218
1219 /* ConnHdlr sends raw LLC: Encode and send as UL_UD / DL_UD */
1220 [not g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1221 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001222 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001223 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001224 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 +02001225 }
1226 [g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1227 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001228 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001229 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001230 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001231 }
Harald Welte199f3862020-11-15 22:37:45 +01001232
Harald Welte71449b02020-12-09 11:58:23 +01001233 /* Testcase sends us BSSGP on global port */
1234 [] GLOBAL.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
1235 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
1236 }
1237
Harald Welte199f3862020-11-15 22:37:45 +01001238 [] MGMT.receive(BssgpBlockRequest:?) -> value bbr {
1239 f_ptp_sendBlock(bbr.cause);
1240 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001241}
1242
Harald Welteab50cc22020-11-25 17:11:08 +01001243private altstep as_ptp_handle_inbound_reset() runs on BSSGP_BVC_CT {
1244 var NsUnitdataIndication udi;
1245 /* we are a SGSN: BSS/PCU-originated RESET must have a cell ID */
1246 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
1247 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001248 if (MGMT.checkstate("Connected")) {
1249 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1250 }
Harald Welteab50cc22020-11-25 17:11:08 +01001251 /* Respond to RESET with correct BVCI but without CellID */
1252 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, omit), 0, g_bvc_lsp));
1253 /* FIXME: reset all state? What about clients? */
1254 f_ptp_change_state(BVC_S_UNBLOCKED);
1255 }
1256 /* we are a BSS/PCU: SGSN-originated RESET must not have a cell ID */
1257 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, omit), 0)) -> value udi {
1258 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001259 if (MGMT.checkstate("Connected")) {
1260 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1261 }
Harald Welteab50cc22020-11-25 17:11:08 +01001262 /* Respond to RESET with correct BVCI with CellID */
1263 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0, g_bvc_lsp));
1264 /* FIXME: reset all state? What about clients? */
1265 f_ptp_change_state(BVC_S_UNBLOCKED);
1266 }
1267}
1268
Harald Welte5339b2e2020-10-04 22:52:56 +02001269/* Events permitted in all states */
1270private altstep as_ptp_allstate() runs on BSSGP_BVC_CT {
1271 var NsUnitdataIndication udi;
Harald Welte199f3862020-11-15 22:37:45 +01001272 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001273 var BSSGP_Client_CT vc_conn;
1274 var OCT4 tlli, tlli_old;
1275 var hexstring imsi;
1276
1277 /* Signaling BVC was reset */
1278 [] BVC.receive(BssgpResetIndication:{0}) {
Harald Welte199f3862020-11-15 22:37:45 +01001279 if (MGMT.checkstate("Connected")) {
1280 MGMT.send(BssgpResetIndication:{0});
1281 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001282 if (not g_sgsn_role) {
1283 f_ptp_change_state(BVC_S_BLOCKED);
1284 /* when the BSS side signaling PTP is RESET, all PTP BVC must start the
1285 * reset procedure */
1286 f_ptp_sendReset();
1287 } else {
1288 f_ptp_change_state(BVC_S_UNBLOCKED);
1289 }
1290 }
1291
Harald Welte5339b2e2020-10-04 22:52:56 +02001292 /* Ignore those for now */
1293 [] BVC.receive(ASP_Event:?) { }
1294 [] BVC.receive(BssgpStatusIndication:?) { }
1295
1296 /* catch-all */
1297 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1298 setverdict(fail, "Received unexpected NS (SIG): ", udi);
1299 }
1300 [] BVC.receive(tr_ptp_BnsUdInd(?, ?)) -> value udi {
1301 setverdict(fail, "Received unexpected NS (PTP): ", udi);
1302 }
1303 [] BVC.receive {
1304 setverdict(fail, "Received unexpected message from BSSGP_CT");
1305 }
1306
1307 /* registration/deregistration of Clients (per-TLLI components) */
1308 [] BSSGP_PROC.getcall(BSSGP_register_client:{?,?}) -> param(imsi, tlli) sender vc_conn {
1309 f_tbl_client_add(imsi, tlli, vc_conn);
1310 BSSGP_PROC.reply(BSSGP_register_client:{imsi, tlli}) to vc_conn;
1311 }
1312 [] BSSGP_PROC.getcall(BSSGP_unregister_client:{?}) -> param(imsi) sender vc_conn {
1313 f_tbl_client_del(imsi, vc_conn);
1314 BSSGP_PROC.reply(BSSGP_unregister_client:{imsi}) to vc_conn;
1315 }
1316 [] BSSGP_PROC.getcall(BSSGP_llgmm_assign:{?,?}) -> param(tlli_old, tlli) sender vc_conn {
1317 f_tbl_client_llgmm_assign(tlli_old, tlli, vc_conn);
1318 BSSGP_PROC.reply(BSSGP_llgmm_assign:{tlli_old, tlli}) to vc_conn;
1319 }
Harald Welte199f3862020-11-15 22:37:45 +01001320
1321 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
Harald Welte52cecbb2020-11-25 21:57:31 +01001322 f_ptp_change_state(BVC_S_BLOCKED);
Harald Welte199f3862020-11-15 22:37:45 +01001323 f_ptp_sendReset(brr.cause);
1324 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001325}
1326
1327/* main loop for per-BVC component */
1328private function f_bssgp_bvc_ScanEvents() runs on BSSGP_BVC_CT {
1329 while (true) {
1330 alt {
1331 [g_ptp_bvc_state == BVC_S_BLOCKED] as_ptp_blocked();
1332 [g_ptp_bvc_state == BVC_S_UNBLOCKED] as_ptp_unblocked();
1333 [] as_ptp_allstate();
1334 }
1335 }
1336}
1337
1338/* main function for per-BVC Component */
Harald Weltefba7afd2020-11-24 23:12:31 +01001339private 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 +02001340 g_cfg := cfg;
Harald Weltefba7afd2020-11-24 23:12:31 +01001341 g_nsei := nsei;
Harald Weltec4505522020-11-11 18:55:09 +01001342 g_bvc_lsp := cfg.bvci;
Harald Welte5339b2e2020-10-04 22:52:56 +02001343 g_sgsn_role := sgsn_role;
1344 f_bssgp_bvc_ScanEvents();
1345}
1346
Harald Weltec4505522020-11-11 18:55:09 +01001347template (value) NsUnitdataRequest ts_ptp_BnsUdReq(template (value) PDU_BSSGP pdu, template (value) BssgpBvci bvci,
1348 template (value) integer lsp) := {
Harald Welte5339b2e2020-10-04 22:52:56 +02001349 bvci := bvci,
1350 nsei := 0, // overwritten in BSSGP_CT
Harald Weltec4505522020-11-11 18:55:09 +01001351 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +02001352 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
1353 * unbound integer value." when trying to send the reocrd rather than the octetstring */
1354 //sdu := omit,
1355 //bssgp := valueof(pdu)
1356 sdu := enc_PDU_BSSGP(valueof(pdu)),
1357 bssgp := omit
1358}
1359
1360template (present) NsUnitdataIndication tr_ptp_BnsUdInd(template (present) PDU_BSSGP pdu, template (present) BssgpBvci bvci) := {
1361 bvci := bvci,
1362 nsei := ?,
Harald Welte80a249a2020-11-17 19:57:40 +01001363 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +02001364 sdu := *,
1365 bssgp := pdu
1366}
1367
1368/* change state of PTP BVC; broadcast state change to clients */
1369private function f_ptp_change_state(BvcState new_state) runs on BSSGP_BVC_CT {
1370 if (new_state == g_ptp_bvc_state) {
1371 return;
1372 }
1373 log("BVCI(", g_cfg.bvci, ") State Transition: ", g_ptp_bvc_state, " -> ", new_state);
1374 g_ptp_bvc_state := new_state;
Harald Welte199f3862020-11-15 22:37:45 +01001375 if (MGMT.checkstate("Connected")) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001376 MGMT.send(ts_BssgpStsInd(g_nsei, g_cfg.bvci, g_ptp_bvc_state));
Harald Welte199f3862020-11-15 22:37:45 +01001377 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001378 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1379 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001380 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 +02001381 }
1382 }
Harald Welte5ac31492018-02-15 20:39:13 +01001383}
1384
1385/* attempt to extract the TLLI from a BSSGP PDU */
1386function f_bssgp_get_tlli(PDU_BSSGP bssgp) return template OCT4 {
1387 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
1388 return bssgp.pDU_BSSGP_DL_UNITDATA.tLLI_current;
1389 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
1390 return bssgp.pDU_BSSGP_UL_UNITDATA.tLLI;
1391 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY)) {
1392 return bssgp.pDU_BSSGP_RA_CAPABILITY.tLLI.tLLI_Value;
1393 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE)) {
1394 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE.tLLI.tLLI_Value;
1395 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK)) {
1396 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK.tLLI.tLLI_Value;
Harald Welte650bd722021-01-17 10:58:56 +01001397 } else if (ischosen(bssgp.pDU_BSSGP_RADIO_STATUS) and
1398 ispresent(bssgp.pDU_BSSGP_RADIO_STATUS.tLLI)) {
Harald Welte5ac31492018-02-15 20:39:13 +01001399 return bssgp.pDU_BSSGP_RADIO_STATUS.tLLI.tLLI_Value;
1400 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND)) {
1401 return bssgp.pDU_BSSGP_SUSPEND.tLLI.tLLI_Value;
1402 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_ACK)) {
1403 return bssgp.pDU_BSSGP_SUSPEND_ACK.tLLI.tLLI_Value;
1404 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_NACK)) {
1405 return bssgp.pDU_BSSGP_SUSPEND_NACK.tLLI.tLLI_Value;
1406 } else if (ischosen(bssgp.pDU_BSSGP_RESUME)) {
1407 return bssgp.pDU_BSSGP_RESUME.tLLI.tLLI_Value;
1408 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_ACK)) {
1409 return bssgp.pDU_BSSGP_RESUME_ACK.tLLI.tLLI_Value;
1410 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_NACK)) {
1411 return bssgp.pDU_BSSGP_RESUME_NACK.tLLI.tLLI_Value;
1412 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL)) {
1413 return bssgp.pDU_BSSGP_FLUSH_LL.tLLI.tLLI_Value;
1414 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL_ACK)) {
1415 return bssgp.pDU_BSSGP_FLUSH_LL_ACK.tLLI.tLLI_Value;
1416 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1417 return bssgp.pDU_BSSGP_LLC_DISCARDED.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_PAGING_CS) and
1421 isvalue(bssgp.pDU_BSSGP_PAGING_CS.tLLI)) {
1422 return bssgp.pDU_BSSGP_PAGING_CS.tLLI.tLLI_Value;
1423 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS)) {
1424 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS.tLLI.tLLI_Value;
1425 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK)) {
1426 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK.tLLI.tLLI_Value;
1427 }
1428 /* TODO: Handover, PFC, LCS */
1429 return omit;
1430}
1431
Harald Weltef70997d2018-02-17 10:11:19 +01001432
Harald Welte5ac31492018-02-15 20:39:13 +01001433
Harald Welte1733a382017-07-23 17:26:17 +02001434}