blob: 5c825d61087817d32828033b9048588a31fd238c [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 Welte5339b2e2020-10-04 22:52:56 +02004 * (C) 2018-2020 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;
Harald Welte1733a382017-07-23 17:26:17 +020028
Harald Welte93331e72020-09-12 20:51:05 +020029#ifdef BSSGP_EM_L3
Harald Welte5ac31492018-02-15 20:39:13 +010030import from MobileL3_GMM_SM_Types all;
31import from MobileL3_Types all;
Harald Welte93331e72020-09-12 20:51:05 +020032#endif
Harald Welte5ac31492018-02-15 20:39:13 +010033
34import from LLC_Types all;
35import from LLC_Templates all;
36
Harald Weltea2526a82018-02-18 19:03:36 +010037import from SNDCP_Types all;
38
Harald Welte12d19b82020-10-09 11:41:06 +020039
40modulepar {
41 /* tolerate CellID absence/presence in BVC-RESET in violation to spec */
42 boolean mp_tolerate_bvc_reset_cellid := false;
43}
44
Harald Welte5ac31492018-02-15 20:39:13 +010045/***********************************************************************
Harald Welte5339b2e2020-10-04 22:52:56 +020046 * Communication between Client Components and per-BVC component
Harald Welte5ac31492018-02-15 20:39:13 +010047 ***********************************************************************/
48
Harald Welte853c0ad2018-02-15 17:45:29 +010049type record BssgpStatusIndication {
Harald Welte5339b2e2020-10-04 22:52:56 +020050 Nsei nsei optional,
Harald Welte853c0ad2018-02-15 17:45:29 +010051 BssgpBvci bvci,
52 BvcState state
Harald Welte5339b2e2020-10-04 22:52:56 +020053};
54
55type record BssgpResetIndication {
56 BssgpBvci bvci
57};
58
59template (value) BssgpStatusIndication
60ts_BssgpStsInd(template (omit) Nsei nsei, template (value) BssgpBvci bvci,
61 template (value) BvcState state) := {
62 nsei := nsei,
63 bvci := bvci,
64 state := state
Harald Welte853c0ad2018-02-15 17:45:29 +010065}
66
Harald Welte5339b2e2020-10-04 22:52:56 +020067template (present) BssgpStatusIndication
68tr_BssgpStsInd(template Nsei nsei, template (present) BssgpBvci bvci,
69 template (present) BvcState state) := {
Harald Welte853c0ad2018-02-15 17:45:29 +010070 nsei := nsei,
71 bvci := bvci,
72 state := state
73}
74
75type enumerated BvcState {
Harald Welte5339b2e2020-10-04 22:52:56 +020076 /* SGSN role: waiting to be reset by the peer */
77 BVC_S_WAIT_RESET,
78 /* PCU role: waiting for NS_ALIVE_UNBLOCKED */
79 BVC_S_WAIT_NS_ALIVE_UNBLOCKED,
80 /* BVC-BLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010081 BVC_S_BLOCKED,
Harald Welte5339b2e2020-10-04 22:52:56 +020082 /* BVC-UNBLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010083 BVC_S_UNBLOCKED
84};
85
86/* port from our (internal) point of view */
87type port BSSGP_SP_PT message {
Harald Welte5ac31492018-02-15 20:39:13 +010088 in PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +020089 PDU_LLC
90#ifdef BSSGP_EM_L3
91 ,
Harald Welte5ac31492018-02-15 20:39:13 +010092 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +020093 PDU_L3_SGSN_MS
94#endif
95 ;
Harald Welte955aa942019-05-03 01:29:29 +020096 out PDU_BSSGP,
97 PDU_LLC,
98 PDU_SN,
Harald Welte853c0ad2018-02-15 17:45:29 +010099 NsStatusIndication,
100 BssgpStatusIndication,
Harald Welte93331e72020-09-12 20:51:05 +0200101 ASP_Event
102#ifdef BSSGP_EM_L3
103 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100104 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200105 PDU_L3_SGSN_MS
106#endif
107 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100108} with { extension "internal" };
109
110/* port from the user point of view */
111type port BSSGP_PT message {
112 in ASP_Event,
113 NsStatusIndication,
114 BssgpStatusIndication,
Harald Welte955aa942019-05-03 01:29:29 +0200115 PDU_BSSGP,
116 PDU_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200117 PDU_SN
118#ifdef BSSGP_EM_L3
119 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100120 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200121 PDU_L3_SGSN_MS
122#endif
123 ;
Harald Welte5ac31492018-02-15 20:39:13 +0100124 out PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +0200125 PDU_LLC
126#ifdef BSSGP_EM_L3
127 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100128 PDU_L3_SGSN_MS,
Harald Welte93331e72020-09-12 20:51:05 +0200129 PDU_L3_MS_SGSN
130#endif
131 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100132} with { extension "internal" };
133
Harald Welte5339b2e2020-10-04 22:52:56 +0200134signature BSSGP_register_client(hexstring imsi, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100135signature BSSGP_unregister_client(hexstring imsi);
Harald Weltef70997d2018-02-17 10:11:19 +0100136signature BSSGP_llgmm_assign(OCT4 tlli_old, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100137
138type port BSSGP_PROC_PT procedure {
Harald Weltef70997d2018-02-17 10:11:19 +0100139 inout BSSGP_register_client, BSSGP_unregister_client, BSSGP_llgmm_assign;
Harald Welte5ac31492018-02-15 20:39:13 +0100140} with { extension "internal" };
141
142
Harald Welte5339b2e2020-10-04 22:52:56 +0200143
Harald Welte5ac31492018-02-15 20:39:13 +0100144/***********************************************************************
145 * Client Component for a single MS/TLLI
Harald Welte5339b2e2020-10-04 22:52:56 +0200146 ***********************************************************************
147 * This is what most users will want to derive their test cases from. It
148 * provides a set of three different ports (PTP, signaling, procedure)
149 * for (currently up to 3) different Cells. Those ports are all connected
150 * to one or multiple different per-BVC compoennts, depending on whether
151 * they share the same BSS or not.
Harald Welte5ac31492018-02-15 20:39:13 +0100152 ***********************************************************************/
153
154type component BSSGP_Client_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200155 /* one port array for each client; allows talking to up to 3 BVC/Cell (handover, ...) */
156 port BSSGP_PT BSSGP[3]; /* PTP-BVC */
157 port BSSGP_PT BSSGP_SIG[3]; /* Signaling BVC */
Harald Welte9b461a92020-12-10 23:41:14 +0100158 port BSSGP_PT BSSGP_GLOBAL[3]; /* Signaling BVC */
Harald Welte5339b2e2020-10-04 22:52:56 +0200159 port BSSGP_PROC_PT BSSGP_PROC[3]; /* registration / deregistration */
Harald Welte5ac31492018-02-15 20:39:13 +0100160};
161
Harald Welte5339b2e2020-10-04 22:52:56 +0200162function f_bssgp_client_register(hexstring imsi, OCT4 tlli, BSSGP_PROC_PT PT := BSSGP_PROC[0])
163runs on BSSGP_Client_CT {
164 PT.call(BSSGP_register_client:{imsi, tlli}) {
165 [] PT.getreply(BSSGP_register_client:{imsi, tlli}) {};
166 }
167}
168
169function f_bssgp_client_unregister(hexstring imsi, BSSGP_PROC_PT PT := BSSGP_PROC[0])
170runs on BSSGP_Client_CT {
171 PT.call(BSSGP_unregister_client:{imsi}) {
172 [] PT.getreply(BSSGP_unregister_client:{imsi}) {};
173 }
174}
175
176/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
177function f_bssgp_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_PROC_PT PT := BSSGP_PROC[0])
178runs on BSSGP_Client_CT {
179 PT.call(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {
180 [] PT.getreply(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {};
181 }
182}
183
Harald Welte5ac31492018-02-15 20:39:13 +0100184/***********************************************************************
185 * Main Component
Harald Welte5339b2e2020-10-04 22:52:56 +0200186 ***********************************************************************
187 * This component exists once, and it runs on to of the NS_Emulation.
188 * It handles the BVC-RESET for the signaling BVCI, and dispatches messages
189 * to the per-BVC components running above it. Messages are routed by:
190 * - PTP BVCI in NS header (if non-0)
191 * - BVCI IE in messages on BVCI=0 in NS header
192 * Messages for which no unique BVC can be identified (like paging without
193 * a BVCI IE set) are broadcast to all BVC components.
194 * We also broadcast state changes (BssgpStatusIndication) and reset events
195 * (BssgpResetIndication) of the signaling BVC to all per-BVC components.
Harald Welte5ac31492018-02-15 20:39:13 +0100196 ***********************************************************************/
197
Harald Welte5339b2e2020-10-04 22:52:56 +0200198function BssgpStart(BssgpConfig cfg, charstring id) runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100199 g_cfg := cfg;
Harald Welte5339b2e2020-10-04 22:52:56 +0200200
201 /* create the per-BVC components based on the configuration */
202 for (var integer i := 0; i < lengthof(g_cfg.bvc); i := i+1) {
203 var BssgpBvcConfig bvc_cfg := g_cfg.bvc[i];
204 var charstring bvc_id := id & "-BVCI" & int2str(bvc_cfg.bvci);
205 /* create, connect and start the BVC component */
206 var BSSGP_BVC_CT bvc_ct := BSSGP_BVC_CT.create(bvc_id);
207 connect(bvc_ct:BVC, self:BVC);
Harald Weltefba7afd2020-11-24 23:12:31 +0100208 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 +0200209 /* populate the BVC state table */
210 BvcTable[i] := {
211 bvci := bvc_cfg.bvci,
212 cell_id := bvc_cfg.cell_id,
213 comp_ref := bvc_ct
214 };
215 }
216
217 if (g_cfg.sgsn_role) {
218 /* The SGSN side waits for an inbound BVC-RESET on the signaling BVC */
219 f_sign_change_state(BVC_S_WAIT_RESET);
220 } else {
221 /* The BSS/PCU side waits for an inbound NsStatusIndication to Tx BVC-RESET */
222 f_sign_change_state(BVC_S_WAIT_NS_ALIVE_UNBLOCKED);
223 }
224
225 /* main loop */
226 f_bssgp_ScanEvents();
Harald Welte853c0ad2018-02-15 17:45:29 +0100227}
228
Harald Welte5339b2e2020-10-04 22:52:56 +0200229/* master component, exists once per BSSGP instance (NSE) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100230type component BSSGP_CT {
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200231 /* UDP ports towards the bottom (IUT) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100232 port NS_PT BSCP;
Harald Welte5339b2e2020-10-04 22:52:56 +0200233
234 /* control by the user */
235 port BSSGP_CT_PROC_PT PROC;
236
Harald Welte57de2202020-11-24 18:15:02 +0100237 /* global port for procedures without any relation to a BVC
238 * (currently only) SUSPEND/RESUME */
239 port BSSGP_SP_PT GLOBAL;
240
Harald Welte0083ea62020-12-02 21:30:44 +0100241 /* RAN INFORMATION MGMT */
242 port BSSGP_SP_PT RIM;
243
Harald Weltebdf96d82020-11-27 18:58:46 +0100244 /* port to a management instance */
245 port BSSGP_BVC_MGMT_SP_PT MGMT;
246
Harald Welte5339b2e2020-10-04 22:52:56 +0200247 var BssgpConfig g_cfg;
248
249 /* Signaling BVC (BVCI=0) */
250 var BvcState g_sign_bvc_state := BVC_S_WAIT_RESET;
251 timer g_T2 := 60.0;
252
253 /* port to per-BVC components */
254 port BSSGP_BVC_SP_PT BVC;
255 /* per-BVC state table */
256 var BvcEntity BvcTable[16];
257};
258
259/* one element in the per-BVC state table */
260type record BvcEntity {
261 /* PTP BVCI of this BVC/Cell */
262 BssgpBvci bvci,
263 /* Cell Identity of this cell */
264 BssgpCellId cell_id,
265 /* reference to the per-BVC/cell component */
266 BSSGP_BVC_CT comp_ref
267};
268
269/* find the per-BVC component for a given BVCI */
270private function f_comp_for_bvci(BssgpBvci bvci) runs on BSSGP_CT return BSSGP_BVC_CT {
271 var integer i;
272
273 for (i := 0; i < lengthof(BvcTable); i := i+1) {
274 if (BvcTable[i].bvci == bvci) {
275 return BvcTable[i].comp_ref;
276 }
277 }
278 return null;
279}
280
281/* We are in BVC_S_WAIT_RSET state (only happens in SGSN role) */
282altstep as_sig_wait_reset() runs on BSSGP_CT {
283 var NsUnitdataIndication udi;
284
285 /* Respond to RESET for signalling BVCI 0 */
286 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
287 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5339b2e2020-10-04 22:52:56 +0200288 f_sign_change_state(BVC_S_UNBLOCKED);
Harald Weltebdf96d82020-11-27 18:58:46 +0100289 if (MGMT.checkstate("Connected")) {
290 MGMT.send(BssgpResetIndication:{0});
291 }
292 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200293 }
294
295 /* work-around for old, buggy libosmogb before Change-Id Ie87820537d6d616da4fd4bbf73eab06e28fda5e1 */
296 [mp_tolerate_bvc_reset_cellid] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, g_cfg.bvc[0].cell_id), 0)) -> value udi {
297 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5339b2e2020-10-04 22:52:56 +0200298 f_sign_change_state(BVC_S_UNBLOCKED);
Harald Weltebdf96d82020-11-27 18:58:46 +0100299 if (MGMT.checkstate("Connected")) {
300 MGMT.send(BssgpResetIndication:{0});
301 }
302 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200303 }
304}
305
Harald Welte27a70cc2020-12-09 11:57:41 +0100306private template PDU_BSSGP tr_GLOBAL_SIG := (
Harald Welte57de2202020-11-24 18:15:02 +0100307 {pDU_BSSGP_SUSPEND:=?}, {pDU_BSSGP_SUSPEND_ACK:=?}, {pDU_BSSGP_SUSPEND_NACK:=?},
Harald Weltef8e5c5d2020-11-27 22:37:23 +0100308 {pDU_BSSGP_RESUME:=?}, {pDU_BSSGP_RESUME_ACK:=?}, {pDU_BSSGP_RESUME_NACK:=?},
Harald Weltef20af412020-11-28 16:11:11 +0100309 {pDU_BSSGP_SGSN_INVOKE_TRACE:=?}, {pDU_BSSGP_OVERLOAD:=?}
Harald Welte57de2202020-11-24 18:15:02 +0100310);
311
Harald Welte0083ea62020-12-02 21:30:44 +0100312/* BSSGP messages that should arrive on the RIM port */
313private template PDU_BSSGP tr_RIM := (
314 {pDU_BSSGP_RAN_INFORMATION:=?}, {pDU_BSSGP_RAN_INFORMATION_REQUEST:=?},
315 {pDU_BSSGP_RAN_INFORMATION_ACK:=?}, {pDU_BSSGP_RAN_INFORMATION_ERROR:=?},
316 {pDU_BSSGP_RAN_INFORMATION_APPLICATION_ERROR:=?}
317);
318
Harald Welte5339b2e2020-10-04 22:52:56 +0200319/* We are in BVC_S_UNBLOCKED state */
320altstep as_sig_unblocked() runs on BSSGP_CT {
321 var BSSGP_BVC_CT bvc_comp_ref;
322 var BSSGP_Client_CT vc_conn;
323 var NsUnitdataIndication udi;
324 var NsUnitdataRequest udr;
Harald Welte57de2202020-11-24 18:15:02 +0100325 var PDU_BSSGP bssgp;
Harald Welte5339b2e2020-10-04 22:52:56 +0200326
327 /* Messages PTP BVCI in BVCI field of NS: dispatch by that */
328 [] BSCP.receive(f_BnsUdInd(?, (2..65535))) -> value udi {
329 bvc_comp_ref := f_comp_for_bvci(valueof(udi.bvci));
330 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
331 /* dispatch to BVC component */
332 BVC.send(udi) to bvc_comp_ref;
333 } else {
334 setverdict(fail, "Rx BSSGP for unknown PTP BVCI ", udi.bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100335 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 +0200336 }
337 }
338
339 /* Messages with BVCI = 0 (Signaling) in BVCI field of NS */
Harald Welte27a70cc2020-12-09 11:57:41 +0100340 [] BSCP.receive(f_BnsUdInd(tr_GLOBAL_SIG, 0)) -> value udi {
Harald Welte57de2202020-11-24 18:15:02 +0100341 GLOBAL.send(udi.bssgp);
342 }
Harald Welte0083ea62020-12-02 21:30:44 +0100343 [] BSCP.receive(f_BnsUdInd(tr_RIM, 0)) -> value udi {
344 if (RIM.checkstate("Connected")) {
345 RIM.send(udi.bssgp);
346 }
347 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200348
349 /* Route based on PTP BVCI in payload/IE of signaling PDU */
350 [] BSCP.receive(f_BnsUdInd(?, 0)) -> value udi {
351 var template (omit) BssgpBvci ptp_bvci := f_BSSGP_BVCI_get(udi.bssgp);
352 if (istemplatekind(ptp_bvci, "omit")) {
353 log("Rx on SIG BVCI without PTP BVCI: broadcasting");
354 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
355 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
356 BVC.send(udi) to BvcTable[i].comp_ref;
357 }
358 }
359 } else {
360 bvc_comp_ref := f_comp_for_bvci(valueof(ptp_bvci));
361 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
362 /* dispatch to BVC component */
363 BVC.send(udi) to bvc_comp_ref;
364 } else {
365 setverdict(fail, "Rx SIG BSSGP for unknown PTP BVCI ", ptp_bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100366 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200367 }
368 }
369 }
370
371 /* Handle PS-PAGING on SIGN BVCI with no conditional BVCI IE */
372 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_PS:=?}, 0)) -> value udi {
373 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
374 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
375 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
376 BVC.send(udi) to BvcTable[i].comp_ref;
377 }
378 }
379 }
380 /* Handle CS-PAGING on SIGN BVCI with no conditional BVCI IE */
381 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_CS:=?}, 0)) -> value udi {
382 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
383 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
384 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
385 BVC.send(udi) to BvcTable[i].comp_ref;
386 }
387 }
388 }
389
390 /* per-BVC component sends us something; forward to NS without any validation */
391 [] BVC.receive(NsUnitdataRequest:?) -> value udr {
392 /* patch in the NSEI, as the per-BVC components have no idea about it */
393 udr.nsei := g_cfg.nsei;
394 BSCP.send(udr);
395 }
Harald Welte57de2202020-11-24 18:15:02 +0100396
Harald Welte27a70cc2020-12-09 11:57:41 +0100397 [] GLOBAL.receive(tr_GLOBAL_SIG) -> value bssgp {
Harald Welte57de2202020-11-24 18:15:02 +0100398 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
399 }
Harald Welte0083ea62020-12-02 21:30:44 +0100400
401 [] RIM.receive(tr_RIM) -> value bssgp {
402 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
403 }
404
Harald Welte5339b2e2020-10-04 22:52:56 +0200405}
406
407/* We are in BVC_S_WAIT_NS_ALIVE_UNBLOCKED (only happens in BSS role) */
408altstep as_sig_wait_ns_alive_unblocked() runs on BSSGP_CT {
409 var NsStatusIndication nsi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100410 [] 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 +0200411 /* if we just became NS-unblocked, send a BCC-RESET */
412 if (g_cfg.sgsn_role == false) {
Harald Weltec4505522020-11-11 18:55:09 +0100413 BSCP.send(f_BnsUdReq(ts_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, 0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200414 g_T2.start;
415 /* The BVC_RESET_ACK is handled in the as_sig_allstate() below */
416 }
417 /* Idea: We could send BVC-UNBLOCK here like some SGSN do */
418 }
419
420}
421
Harald Weltee5887922020-11-27 19:04:46 +0100422/* handling of events irrespective of BVC state (before state-specific handling) */
423altstep as_sig_allstate_pre() runs on BSSGP_CT {
424 var NsUnitdataIndication udi;
425
426 /* Respond to RESET for signalling BVCI 0 */
427 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
428 log("Rx BVC-RESET for Signaling BVCI=0");
429 if (MGMT.checkstate("Connected")) {
430 MGMT.send(BssgpResetIndication:{0});
431 }
432 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
433 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
434 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
435 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
436 }
437 }
438 }
439
440 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
441 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(0, ?), 0)) -> value udi {
442 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
443 }
444 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(0), 0)) -> value udi {
445 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
446 }
447 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(0), 0)) -> value udi {
448 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
449 }
450 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
451 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
452 }
453
454 /* BVC-RESET-ACK for BVCI=0 while T2 is running: Answer to a BVC-RESET we sent earlier */
455 [g_T2.running] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(0, omit), 0)) -> value udi {
456 log("BVCI(0) Rx BVC-RESET-ACK");
457 g_T2.stop;
458 f_sign_change_state(BVC_S_UNBLOCKED);
459 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
460 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
461 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
462 }
463 }
464 }
465 [] g_T2.timeout {
466 setverdict(fail, "Timeout waiting for BVC-RESET-ACK on BVCI=0");
467 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
468 g_T2.start;
469 }
470}
471
472/* handling of events irrespective of BVC state (after state-specific handling) */
473altstep as_sig_allstate_post() runs on BSSGP_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200474 var BSSGP_Client_CT vc_conn;
475 var NsUnitdataIndication udi;
476 var NsStatusIndication nsi;
477 var ASP_Event evt;
478 var BSSGP_BVC_CT bvc_comp_ref;
479 var BssgpBvci bvci;
Harald Weltee5887922020-11-27 19:04:46 +0100480 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +0200481
482 /* Respond to RESET for signalling BVCI 0 */
483 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
484 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Weltebdf96d82020-11-27 18:58:46 +0100485 if (MGMT.checkstate("Connected")) {
486 MGMT.send(BssgpResetIndication:{0});
487 }
Harald Weltec4505522020-11-11 18:55:09 +0100488 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200489 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
490 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
491 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
492 }
493 }
494 }
495
496 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
497 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(0, ?), 0)) -> value udi {
498 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
499 }
500 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(0), 0)) -> value udi {
501 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
502 }
503 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(0), 0)) -> value udi {
504 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
505 }
506 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
507 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
508 }
509
510 /* Respond to BLOCK for wrong BVCI */
511 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(?, ?), 0)) -> value udi {
512 setverdict(fail, "Rx BVC-BLOCK for unknown BVCI");
Harald Weltec4505522020-11-11 18:55:09 +0100513 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200514 }
515
516 /* Respond to RESET with wrong BVCI */
517 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, ?, ?), 0)) -> value udi {
518 var BssgpBvci ptp_bvci := oct2int(udi.bssgp.pDU_BSSGP_BVC_RESET.bVCI.unstructured_value);
519 setverdict(fail, "Rx BVC-RESET for unknown BVCI ", ptp_bvci);
Harald Weltec4505522020-11-11 18:55:09 +0100520 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(ptp_bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200521 }
522
Harald Welte5339b2e2020-10-04 22:52:56 +0200523 /* Forwarding of ASP_Event to per-BVC components */
524 [] BSCP.receive(ASP_Event:?) -> value evt {
525 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
526 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
527 BVC.send(evt) to BvcTable[i].comp_ref;
528 }
529 }
530 }
531 /* Keep NS Status Indicaitons to us; no need to inform per-BVC components [for now?] */
Harald Weltedc0a0902020-11-10 21:03:29 +0100532 [] BSCP.receive(tr_NsStsInd(g_cfg.nsei)) -> value nsi { }
Harald Welte445fc612020-11-10 19:15:30 +0100533 /* We should never see any different NSEI: There's one BSSGP_CT per NSE */
Harald Weltedc0a0902020-11-10 21:03:29 +0100534 [] BSCP.receive(tr_NsStsInd(?)) -> value nsi {
Harald Welte445fc612020-11-10 19:15:30 +0100535 setverdict(fail, "Rx NsStatusInd for wrong NSEI ", nsi);
536 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200537
538 /* Procedure port request to resolve the per-BVC component for a given ptp BVCI */
539 [] PROC.getcall(BSSGP_get_bvci_ct:{?}) -> param(bvci) sender vc_conn {
540 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
541 if (BvcTable[i].bvci == bvci) {
542 PROC.reply(BSSGP_get_bvci_ct:{bvci} value BvcTable[i].comp_ref) to vc_conn;
543 return;
544 }
545 }
546 }
Harald Weltee5887922020-11-27 19:04:46 +0100547
548 /* default case of handling unknown PDUs */
549 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
550 setverdict(fail, "Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_sign_bvc_state);
551 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp), 0, 0));
552 }
553
554 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
555 BSCP.send(f_BnsUdReq(ts_BVC_RESET(brr.cause, 0, omit), 0, 0));
556 g_T2.start;
557 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200558}
559
560/* send the highest decoded layer of the message through given port */
561private function f_send_bssgp_dec(BssgpDecoded dec, BSSGP_Client_CT vc_conn, BSSGP_SP_PT pt) {
562#ifdef BSSGP_EM_L3
563 if (ispresent(dec.l3_mt)) {
564 pt.send(dec.l3_mt) to vc_conn;
565 } else if (ispresent(dec.l3_mo)) {
566 pt.send(dec.l3_mo) to vc_conn;
567 } else
568#endif
569 if (ispresent(dec.sndcp)) {
570 pt.send(dec.sndcp) to vc_conn;
571 } else if (ispresent(dec.llc)) {
572 pt.send(dec.llc) to vc_conn;
573 } else {
574 pt.send(dec.bssgp) to vc_conn;
575 }
576}
577
578
579function f_llc_get_n_u_tx(inout LLC_Entity llc) return uint9_t {
580 var uint9_t ret := llc.n_u_tx_next;
581 llc.n_u_tx_next := llc.n_u_tx_next + 1;
582 return ret;
583}
584
585#ifdef BSSGP_EM_L3
586function f_llc_sapi_by_l3_mo(PDU_L3_MS_SGSN l3_mo) return BIT4 {
587 if (ischosen(l3_mo.msgs.gprs_mm)) {
588 return c_LLC_SAPI_LLGMM;
589 } else if (ischosen(l3_mo.msgs.gprs_sm)) {
590 return c_LLC_SAPI_LLGMM;
591 } else if (ischosen(l3_mo.msgs.sms)) {
592 return c_LLC_SAPI_LLSMS;
593 }
594 setverdict(fail, "No LLC SAPI for ", l3_mo);
595 mtc.stop;
596}
597
598private function f_llc_sapi_by_l3_mt(PDU_L3_SGSN_MS l3_mt) return BIT4 {
599 if (ischosen(l3_mt.msgs.gprs_mm)) {
600 return c_LLC_SAPI_LLGMM;
601 } else if (ischosen(l3_mt.msgs.gprs_sm)) {
602 return c_LLC_SAPI_LLGMM;
603 } else if (ischosen(l3_mt.msgs.sms)) {
604 return c_LLC_SAPI_LLSMS;
605 }
606 setverdict(fail, "No LLC SAPI for ", l3_mt);
607 mtc.stop;
608}
609#endif
610
611/* main loop of BSSGP_CT */
612private function f_bssgp_ScanEvents() runs on BSSGP_CT {
613 while (true) {
614 alt {
615 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
Harald Weltee5887922020-11-27 19:04:46 +0100616 [] as_sig_allstate_pre();
Harald Welte5339b2e2020-10-04 22:52:56 +0200617 [g_sign_bvc_state == BVC_S_WAIT_NS_ALIVE_UNBLOCKED] as_sig_wait_ns_alive_unblocked();
618 [g_sign_bvc_state == BVC_S_UNBLOCKED] as_sig_unblocked();
Harald Weltee5887922020-11-27 19:04:46 +0100619 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
620 [] as_sig_allstate_post();
Harald Welte5339b2e2020-10-04 22:52:56 +0200621 }
622 } /* while */
623}
624
625/* generate a send template. Cannot be a real template as we want to use g_cfg.nsei */
Harald Weltec4505522020-11-11 18:55:09 +0100626private function f_BnsUdReq(template PDU_BSSGP pdu, BssgpBvci bvci, integer lsp)
Harald Welte5339b2e2020-10-04 22:52:56 +0200627runs on BSSGP_CT return NsUnitdataRequest {
628 var NsUnitdataRequest udr := {
629 bvci := bvci,
630 nsei := g_cfg.nsei,
Harald Weltec4505522020-11-11 18:55:09 +0100631 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +0200632 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
633 * unbound integer value." when trying to send the reocrd rather than the octetstring */
634 //sdu := omit,
635 //bssgp := valueof(pdu)
636 sdu := enc_PDU_BSSGP(valueof(pdu)),
637 bssgp := omit
638 }
639 return udr;
640}
641
642/* generate a receive template. Cannot be a real template as we want to use g_cfg.nsei */
643private function f_BnsUdInd(template PDU_BSSGP pdu, template BssgpBvci bvci)
644runs on BSSGP_CT return template (present) NsUnitdataIndication {
645 var template (present) NsUnitdataIndication udi := {
646 bvci := bvci,
647 nsei := g_cfg.nsei,
Harald Welte80a249a2020-11-17 19:57:40 +0100648 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +0200649 sdu := *,
650 bssgp := pdu
651 }
652 return udi;
653}
654
655/* change state of signaling BVCI; notify per-BVC components */
656private function f_sign_change_state(BvcState new_state) runs on BSSGP_CT {
657 if (new_state == g_sign_bvc_state) {
658 return;
659 }
660 log("BVCI(0) State Transition: ", g_sign_bvc_state, " -> ", new_state);
661 g_sign_bvc_state := new_state;
662 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
663 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
664 BVC.send(ts_BssgpStsInd(g_cfg.nsei, 0, g_sign_bvc_state)) to BvcTable[i].comp_ref;
665 }
666 }
667}
668
669/* User wants to get per-BVC component reference from BSSGP_CT */
670signature BSSGP_get_bvci_ct(BssgpBvci bvci) return BSSGP_BVC_CT;
671
672type port BSSGP_CT_PROC_PT procedure {
673 inout BSSGP_get_bvci_ct
674} with { extension "internal" };
675
676/* convenience wrapper function for user */
677function f_bssgp_get_bvci_ct(BssgpBvci bvci, BSSGP_CT_PROC_PT PT) return BSSGP_BVC_CT {
678 var BSSGP_BVC_CT res;
679 PT.call(BSSGP_get_bvci_ct:{bvci}) {
680 [] PT.getreply(BSSGP_get_bvci_ct:{bvci})-> value res {
681 return res;
682 }
683 }
684}
685
686/***********************************************************************
687 * per-BVC (Cell) Component
688 ***********************************************************************
689 * Any number of these components runs on top of the BSSGP_CT, each
690 * representing one PTP BVC within this NSE. Users (test cases) can
691 * register with TLLI and/or IMSI via the procedure port.
692 ***********************************************************************/
693
694/* per-BVC component. Exists once per cell within a BSSGP_CT */
695type component BSSGP_BVC_CT {
696 /* port towards the underlying BSSGP_CT */
697 port BSSGP_BVC_PT BVC;
698
Harald Welte199f3862020-11-15 22:37:45 +0100699 /* port to a management instance */
700 port BSSGP_BVC_MGMT_SP_PT MGMT;
701
Harald Welte71449b02020-12-09 11:58:23 +0100702 /* per-BVC global port for e.g. BVC Flow Control */
703 port BSSGP_SP_PT GLOBAL;
704
705 /* BSSGP-User SAP towards the user (per-TLLI, Client) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100706 port BSSGP_SP_PT BSSGP_SP;
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200707 port BSSGP_SP_PT BSSGP_SP_SIG;
Harald Welte5ac31492018-02-15 20:39:13 +0100708 port BSSGP_PROC_PT BSSGP_PROC;
Harald Welte853c0ad2018-02-15 17:45:29 +0100709
Harald Welte5339b2e2020-10-04 22:52:56 +0200710 var BssgpBvcConfig g_cfg;
711 var boolean g_sgsn_role;
Harald Weltefba7afd2020-11-24 23:12:31 +0100712 var Nsei g_nsei;
Harald Welte5ac31492018-02-15 20:39:13 +0100713
Harald Weltec4505522020-11-11 18:55:09 +0100714 /* default Link Selector Parameter for this BVC (for traffic unrelated to a TLLI) */
715 var integer g_bvc_lsp;
716
Harald Welte853c0ad2018-02-15 17:45:29 +0100717 var BvcState g_ptp_bvc_state := BVC_S_BLOCKED;
718 timer g_T1 := 15.0;
719 timer g_T2 := 60.0;
Harald Welte5339b2e2020-10-04 22:52:56 +0200720 var boolean g_t1_waits_for_block_ack := false;
Harald Welte199f3862020-11-15 22:37:45 +0100721 /* for re-transmissions */
722 var BssgpCause g_last_block_cause;
723 var BssgpCause g_last_reset_cause;
Harald Welte5ac31492018-02-15 20:39:13 +0100724
725 var ClientEntity ClientTable[16];
Harald Welte5339b2e2020-10-04 22:52:56 +0200726};
Harald Welte853c0ad2018-02-15 17:45:29 +0100727
Harald Welte5339b2e2020-10-04 22:52:56 +0200728/* port between global BSSGP_CT and per-BVC BSSGP_BVC_CT */
729type port BSSGP_BVC_SP_PT message {
730 in NsUnitdataRequest;
731 out ASP_Event,
732 BssgpStatusIndication,
733 BssgpResetIndication,
734 NsUnitdataIndication;
735} with { extension "internal" };
736type port BSSGP_BVC_PT message {
737 in ASP_Event,
738 BssgpStatusIndication,
739 BssgpResetIndication,
740 NsUnitdataIndication;
741 out NsUnitdataRequest;
742} with { extension "internal" };
Alexander Couzens6b449fb2018-07-31 14:19:36 +0200743
Harald Welte199f3862020-11-15 22:37:45 +0100744/* port between BSSGP_BVC_CT and a management instance */
745type port BSSGP_BVC_MGMT_SP_PT message {
746 in BssgpResetRequest,
747 BssgpBlockRequest,
748 BssgpUnblockRequest;
749 out BssgpStatusIndication,
750 BssgpResetIndication;
751} with { extension "internal" };
752type port BSSGP_BVC_MGMT_PT message {
753 in BssgpStatusIndication,
754 BssgpResetIndication;
755 out BssgpResetRequest,
756 BssgpBlockRequest,
757 BssgpUnblockRequest;
758} with { extension "internal" };
759
760type record BssgpResetRequest {
761 BssgpCause cause
762};
763type record BssgpBlockRequest {
764 BssgpCause cause
765};
766type record BssgpUnblockRequest {
767};
Harald Welte5339b2e2020-10-04 22:52:56 +0200768
769/* one element in the per-TLLI state table */
Harald Welte5ac31492018-02-15 20:39:13 +0100770type record ClientEntity {
771 OCT4 tlli,
772 OCT4 tlli_old optional,
773 hexstring imsi,
Harald Welte5ac31492018-02-15 20:39:13 +0100774 BSSGP_Client_CT comp_ref,
775 /* LLC entities, one for each SAPI */
776 LLC_Entity llc[16]
Harald Welte853c0ad2018-02-15 17:45:29 +0100777};
Harald Welte5ac31492018-02-15 20:39:13 +0100778type record LLC_Entity {
779 boolean sgsn_role,
780 /* N(U) on transmit side for next PDU */
781 uint9_t n_u_tx_next,
782 /* N(U) on receive side, last PDU */
783 uint9_t n_u_rx_last optional
784};
Harald Welte5339b2e2020-10-04 22:52:56 +0200785type record length(16) of LLC_Entity LLC_Entities;
Harald Welte5ac31492018-02-15 20:39:13 +0100786
Alexander Couzenscdfb7512018-07-31 15:37:14 +0200787function f_llc_create(boolean sgsn_role := false) return LLC_Entities {
788 var LLC_Entities llc;
789 for (var integer i := 0; i < 16; i := i+1) {
790 llc[i] := valueof(t_LLC_init(sgsn_role));
791 }
792 return llc;
793}
794
Harald Welte5ac31492018-02-15 20:39:13 +0100795private template LLC_Entity t_LLC_init(boolean sgsn_role := false) := {
796 sgsn_role := sgsn_role,
797 n_u_tx_next := 0,
798 n_u_rx_last := -
799}
800
Harald Welte5339b2e2020-10-04 22:52:56 +0200801/* configuration of BSSGP_CT */
802type record BssgpConfig {
803 Nsvci nsei,
804 boolean sgsn_role,
805 BssgpBvcConfigs bvc
806};
807/* Configuration of one BVC (Cell) BSSGP_BVC_CT */
808type record BssgpBvcConfig {
809 BssgpBvci bvci,
810 BssgpCellId cell_id,
Harald Welte4d112c92020-11-12 19:48:31 +0100811 BssgpDecodeDepth depth,
812 BssgpCreateCallback create_cb
Harald Welte5339b2e2020-10-04 22:52:56 +0200813};
814type record of BssgpBvcConfig BssgpBvcConfigs;
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200815type enumerated BssgpDecodeDepth {
816 BSSGP_DECODE_DEPTH_BSSGP,
817 BSSGP_DECODE_DEPTH_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200818 BSSGP_DECODE_DEPTH_SNDCP
819#ifdef BSSGP_EM_L3
820 ,
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200821 BSSGP_DECODE_DEPTH_L3
Harald Welte93331e72020-09-12 20:51:05 +0200822#endif
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200823};
Harald Welte4d112c92020-11-12 19:48:31 +0100824/* call-back function type: Called for inbound BSSGP for unknown TLLIs; could create BSSGP_ClienT_CT */
825type function BssgpCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT;
826
827/* Default Create Callback function: Fail and terminate */
828function DefaultCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT {
829 setverdict(fail, "Couldn't find Component for TLLI ", tlli);
830 mtc.stop;
831}
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200832
Harald Welte5339b2e2020-10-04 22:52:56 +0200833/*
834private function f_tbl_init() runs on BSSGP_BVC_CT {
835 var integer i;
836 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
837 ImsiTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100838 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100839
Harald Welte5339b2e2020-10-04 22:52:56 +0200840 for (i := 0; i < sizeof(TlliTable); i := i+1) {
841 TlliTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100842 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100843}
Harald Welte5339b2e2020-10-04 22:52:56 +0200844*/
Harald Welte853c0ad2018-02-15 17:45:29 +0100845
Harald Welte5339b2e2020-10-04 22:52:56 +0200846private function f_tbl_client_add(hexstring imsi, OCT4 tlli, BSSGP_Client_CT vc_conn)
847runs on BSSGP_BVC_CT {
848 var integer i;
849 for (i := 0; i < sizeof(ClientTable); i := i+1) {
850 if (not isvalue(ClientTable[i].comp_ref)) {
851 log("Adding Client=", vc_conn, ", IMSI=", imsi, ", TLLI=", tlli, ", index=", i);
852 ClientTable[i] := {
853 tlli := tlli,
854 tlli_old := omit,
855 imsi := imsi,
856 comp_ref := vc_conn,
857 llc := -
858 };
859 for (var integer j := 0; j < sizeof(ClientTable[i].llc); j := j+1) {
860 ClientTable[i].llc[j] := valueof(t_LLC_init(g_sgsn_role));
861 }
862 return;
Harald Welte5ac31492018-02-15 20:39:13 +0100863 }
864 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200865 testcase.stop("Client Table full");
Harald Welte853c0ad2018-02-15 17:45:29 +0100866}
867
Harald Welte5339b2e2020-10-04 22:52:56 +0200868private function f_tbl_client_del(hexstring imsi, BSSGP_Client_CT vc_conn) runs on BSSGP_BVC_CT {
869 var integer i;
870 for (i := 0; i < sizeof(ClientTable); i := i+1) {
871 if (isvalue(ClientTable[i].imsi) and ClientTable[i].imsi == imsi) {
872 if (ClientTable[i].comp_ref != vc_conn) {
873 setverdict(fail, "Cannot unregister index=", i, " IMSI ", imsi, " registred to ",
874 ClientTable[i].comp_ref, " from ", vc_conn);
875 mtc.stop;
876 }
877 log("Removing Client IMSI=", imsi, ", index=", i);
878 ClientTable[i] := {
879 tlli := -,
880 tlli_old := omit,
881 imsi := ''H,
882 comp_ref := null,
883 llc := - };
884 return;
885 }
886 }
887 log("Warning: Could not find client for IMSI ", imsi);
888 return;
889}
890
891/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
892private function f_tbl_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_Client_CT vc_conn)
893runs on BSSGP_BVC_CT {
894 var integer i := f_tbl_idx_by_comp(vc_conn);
895
896 if (tlli_old == 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
897 /* TLLI assignment */
898 ClientTable[i].tlli := tlli_new;
899 ClientTable[i].tlli_old := omit;
900 } else if (tlli_old != 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
901 /* TLLI change: both active */
902 ClientTable[i].tlli := tlli_new;
903 ClientTable[i].tlli_old := tlli_old;
904 } else if (tlli_old != 'FFFFFFFF'O and tlli_new == 'FFFFFFFF'O) {
905 /* TLLI unassignment: old shall be unassigned; new stays */
906 ClientTable[i].tlli_old := omit;
907 }
908}
909
910private function f_tbl_comp_by_imsi(hexstring imsi) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
911 var integer i;
912 for (i := 0; i < sizeof(ClientTable); i := i+1) {
913 if (isvalue(ClientTable[i].imsi) and isvalue(ClientTable[i].comp_ref)
914 and ClientTable[i].imsi == imsi) {
915 return ClientTable[i].comp_ref;
916 }
917 }
918 setverdict(fail, "Couldn't find Component for IMSI ", imsi);
919 mtc.stop;
920}
921
922private function f_tbl_comp_by_tlli(OCT4 tlli) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
923 var integer i;
924 for (i := 0; i < sizeof(ClientTable); i := i+1) {
925 if (isvalue(ClientTable[i].comp_ref) and
926 (isvalue(ClientTable[i].tlli) and (ClientTable[i].tlli == tlli or
927 isvalue(ClientTable[i].tlli_old) and ClientTable[i].tlli_old == tlli) )) {
928 return ClientTable[i].comp_ref;
929 }
930 }
Harald Welte4d112c92020-11-12 19:48:31 +0100931 return null;
Harald Welte5339b2e2020-10-04 22:52:56 +0200932}
933
934private function f_tbl_idx_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return integer {
935 var integer i;
936 for (i := 0; i < sizeof(ClientTable); i := i+1) {
937 if (isvalue(ClientTable[i].comp_ref) and ClientTable[i].comp_ref == comp_ref) {
938 return i;
939 }
940 }
941 setverdict(fail, "Couldn't find Client for Component ", comp_ref);
942 mtc.stop;
943}
944
945private function f_tbl_tlli_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return OCT4 {
946 var integer i;
947 for (i := 0; i < sizeof(ClientTable); i := i+1) {
948 if (isvalue(ClientTable[i].tlli) and isvalue(ClientTable[i].comp_ref)
949 and ClientTable[i].comp_ref == comp_ref) {
950 return ClientTable[i].tlli;
951 }
952 }
953 setverdict(fail, "Couldn't find TLLI for Component ", comp_ref);
954 mtc.stop;
955}
956
957/* PDU_BSSGP enhanced with LLC and possibly L3 decoded payloads */
958type record BssgpDecoded {
959 PDU_BSSGP bssgp,
960 PDU_LLC llc optional,
961#ifdef BSSGP_EM_L3
962 PDU_L3_MS_SGSN l3_mo optional,
963 PDU_L3_SGSN_MS l3_mt optional,
964#endif
965 PDU_SN sndcp optional
966}
967
968/* Decode a PDU_BSSGP into a BssgpDecoded (i.e. with LLC/L3 decoded, as applicable) */
969private function f_dec_bssgp(PDU_BSSGP bssgp) runs on BSSGP_BVC_CT return BssgpDecoded {
970 var BssgpDecoded dec := {
971 bssgp := bssgp,
972 llc := omit,
973#ifdef BSSGP_EM_L3
974 l3_mo := omit,
975 l3_mt := omit,
976#endif
977 sndcp := omit
978 };
979
980 /* Decode LLC, if it is a PDU that contains LLC */
981 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_LLC) {
982 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
983 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_DL_UNITDATA.lLC_PDU.lLC_PDU);
984 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
985 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_UL_UNITDATA.lLC_PDU.lLC_PDU);
986 }
987 }
988
989 /* Decode SNDCP, if it is a LLC PDU containing user plane data */
990 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_SNDCP) {
991 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_USER)) {
992 dec.sndcp := dec_PDU_SN(dec.llc.pDU_LLC_UI.information_field_UI);
993 }
994 }
995
996#ifdef BSSGP_EM_L3
997 /* Decode L3, if it is a LLC PDU containing L3 */
998 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_L3) {
999 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_L3)) {
1000 if (g_sgsn_role) {
1001 dec.l3_mo := dec_PDU_L3_MS_SGSN(dec.llc.pDU_LLC_UI.information_field_UI);
1002 } else {
1003 dec.l3_mt := dec_PDU_L3_SGSN_MS(dec.llc.pDU_LLC_UI.information_field_UI);
1004 }
1005 }
1006 }
1007#endif
1008
1009 return dec;
1010}
1011
1012private function f_ptp_sendUnblock() runs on BSSGP_BVC_CT {
Harald Weltec4505522020-11-11 18:55:09 +01001013 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001014 g_t1_waits_for_block_ack := false;
1015 g_T1.start;
1016}
1017
1018private function f_ptp_sendBlock(BssgpCause cause) runs on BSSGP_BVC_CT {
Harald Weltec4505522020-11-11 18:55:09 +01001019 BVC.send(ts_ptp_BnsUdReq(t_BVC_BLOCK(g_cfg.bvci, cause), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001020 g_t1_waits_for_block_ack := true;
1021 g_T1.start;
Harald Welte199f3862020-11-15 22:37:45 +01001022 g_last_block_cause := cause;
Harald Welte5339b2e2020-10-04 22:52:56 +02001023}
1024
1025private function f_ptp_sendStatus(BssgpCause cause, PDU_BSSGP pdu) runs on BSSGP_BVC_CT {
1026 /* FIXME: Make sure correct Signaling or PTP BVCI is used! */
Harald Weltec4505522020-11-11 18:55:09 +01001027 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 +02001028}
1029
Harald Welte199f3862020-11-15 22:37:45 +01001030private function f_ptp_sendReset(BssgpCause cause := BSSGP_CAUSE_OM_INTERVENTION) runs on BSSGP_BVC_CT {
Harald Weltec967d0e2020-09-14 16:07:26 +02001031 var PDU_BSSGP pdu;
1032
1033 /* The Cell Identifier IE is mandatory in the BVC-RESET PDU sent from BSS to
1034 * SGSN in order to reset a BVC corresponding to a PTP functional entity. The
1035 * Cell Identifier IE shall not be used in any other BVC-RESET PDU. */
Harald Welte5339b2e2020-10-04 22:52:56 +02001036 if (g_sgsn_role) {
Harald Welte199f3862020-11-15 22:37:45 +01001037 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, omit));
Harald Weltec967d0e2020-09-14 16:07:26 +02001038 } else {
Harald Welte199f3862020-11-15 22:37:45 +01001039 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, g_cfg.cell_id));
Harald Weltec967d0e2020-09-14 16:07:26 +02001040 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001041
1042 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
Harald Weltec4505522020-11-11 18:55:09 +01001043 BVC.send(ts_ptp_BnsUdReq(pdu, 0, g_bvc_lsp));
Harald Welte853c0ad2018-02-15 17:45:29 +01001044 g_T2.start;
1045 //f_change_state(BVC_S_WAIT_RESET);
Harald Welte199f3862020-11-15 22:37:45 +01001046 g_last_reset_cause := cause;
Harald Welte853c0ad2018-02-15 17:45:29 +01001047}
1048
Harald Welte5339b2e2020-10-04 22:52:56 +02001049/* PTP-BVC is in BVC_S_BLOCKED state */
1050private altstep as_ptp_blocked() runs on BSSGP_BVC_CT {
1051 var NsUnitdataIndication udi;
1052
1053 [g_T1.running and not g_t1_waits_for_block_ack] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0)) {
1054 g_T1.stop;
1055 f_ptp_change_state(BVC_S_UNBLOCKED);
1056 }
1057
Harald Welteddfc6672020-11-24 23:13:01 +01001058 /* Inbound BVC-UNBLOCK from peer */
1059 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK(g_cfg.bvci), 0)) {
1060 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
1061 f_ptp_change_state(BVC_S_UNBLOCKED);
1062 }
1063
Harald Welte5339b2e2020-10-04 22:52:56 +02001064 /* RESET-ACK before T2 timeout: reset successful. In SGSN role, CellID must be present */
1065 [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 {
1066 g_T2.stop;
1067 f_ptp_change_state(BVC_S_UNBLOCKED);
1068 }
1069 /* RESET-ACK before T2 timeout: reset successful. In BSS role, CellID must be absent */
1070 [g_T2.running and not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, omit), 0)) -> value udi {
1071 g_T2.stop;
1072 f_ptp_change_state(BVC_S_UNBLOCKED);
1073 }
1074
Harald Welteab50cc22020-11-25 17:11:08 +01001075 [] as_ptp_handle_inbound_reset();
1076
Harald Welte5339b2e2020-10-04 22:52:56 +02001077 [] g_T2.timeout {
1078 /* BVC-RESET-ACK PDU was not received within T2: retransmit */
Harald Welte199f3862020-11-15 22:37:45 +01001079 f_ptp_sendReset(g_last_reset_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001080 }
Harald Welte199f3862020-11-15 22:37:45 +01001081
1082 [] MGMT.receive(BssgpUnblockRequest:?) {
1083 f_ptp_sendUnblock();
1084 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001085}
1086
Harald Welte5339b2e2020-10-04 22:52:56 +02001087/* PTP-BVC is in UNBLOCKED state */
1088private altstep as_ptp_unblocked() runs on BSSGP_BVC_CT {
1089 var NsUnitdataIndication udi;
1090 var NsStatusIndication nsi;
Harald Welte199f3862020-11-15 22:37:45 +01001091 var BssgpBlockRequest bbr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001092 var BSSGP_Client_CT vc_conn;
1093 var ASP_Event evt;
1094 var PDU_BSSGP bs_pdu;
1095 var PDU_LLC llc;
1096#ifdef BSSGP_EM_L3
1097 var PDU_L3_MS_SGSN l3_mo;
1098 var PDU_L3_SGSN_MS l3_mt;
1099#endif
1100
1101 /* bogus unblock, just respond with ACK */
1102 [] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK(g_cfg.bvci), 0)) -> value udi {
Harald Weltec4505522020-11-11 18:55:09 +01001103 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001104 }
1105 /* Respond to BLOCK with BLOCK-ACK + change state */
1106 [] BVC.receive(tr_ptp_BnsUdInd(t_BVC_BLOCK(g_cfg.bvci, ?), 0)) -> value udi {
Harald Weltec4505522020-11-11 18:55:09 +01001107 BVC.send(ts_ptp_BnsUdReq(t_BVC_BLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001108 g_T1.stop;
1109 f_ptp_change_state(BVC_S_BLOCKED);
1110 }
1111 /* BLOCK-ACK before T1 timeout: mark as blocked */
1112 [g_T1.running and g_t1_waits_for_block_ack] BVC.receive(tr_ptp_BnsUdInd(t_BVC_BLOCK_ACK(g_cfg.bvci), 0)) -> value udi {
1113 g_T1.stop;
1114 f_ptp_change_state(BVC_S_BLOCKED);
1115 }
1116 /* Re-transmit BLOCK / UNBLOCK on T1 timeout */
1117 [not g_t1_waits_for_block_ack] g_T1.timeout {
1118 f_ptp_sendUnblock();
1119 }
1120 [g_t1_waits_for_block_ack] g_T1.timeout {
Harald Welte199f3862020-11-15 22:37:45 +01001121 f_ptp_sendBlock(g_last_block_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001122 }
1123
Harald Welteab50cc22020-11-25 17:11:08 +01001124 [] as_ptp_handle_inbound_reset();
1125
Harald Welte5339b2e2020-10-04 22:52:56 +02001126 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_FC_BVC, g_cfg.bvci)) -> value udi {
Harald Welte71449b02020-12-09 11:58:23 +01001127 if (GLOBAL.checkstate("Connected")) {
1128 GLOBAL.send(udi.bssgp);
1129 } else {
1130 /* simply acknowledge all per-BVC Flow Control Messages */
1131 var OCT1 tag := udi.bssgp.pDU_BSSGP_FLOW_CONTROL_BVC.tag.unstructured_Value;
1132 BVC.send(ts_ptp_BnsUdReq(t_BVC_FC_BVC_ACK(tag), g_cfg.bvci, g_bvc_lsp));
1133 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001134 }
Harald Welte71449b02020-12-09 11:58:23 +01001135
1136 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(t_BVC_FC_BVC_ACK(?), g_cfg.bvci)) -> value udi {
1137 if (GLOBAL.checkstate("Connected")) {
1138 GLOBAL.send(udi.bssgp);
1139 } else {
1140 /* ignore any incoming flow control ACK */
1141 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001142 }
Harald Welte71449b02020-12-09 11:58:23 +01001143
Harald Welte5339b2e2020-10-04 22:52:56 +02001144 /* Any other PTP BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1145 [] BVC.receive(tr_ptp_BnsUdInd(?, g_cfg.bvci)) -> value udi {
1146 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1147 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1148 if (isvalue(tlli)) {
1149 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001150 if (vc_conn == null) {
1151 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1152 } else {
1153 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP);
1154 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001155 } else {
1156 log("No TLLI: Broadcasting ", dec);
1157 /* broadcast this message to all components */
1158 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1159 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1160 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1161 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP);
1162 }
1163 }
1164 }
1165 }
1166
1167 /* Any other SIG BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1168 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1169 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1170 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1171 if (isvalue(tlli)) {
1172 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001173 if (vc_conn == null) {
1174 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1175 } else {
1176 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP_SIG);
1177 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001178 } else {
1179 log("No TLLI: Broadcasting ", dec);
1180 /* broadcast this message to all components */
1181 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1182 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1183 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1184 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP_SIG);
1185 }
1186 }
1187 }
1188 }
1189
1190 /* ConnHdlr sends BSSGP on BVCI=0 port: forward it to BSSGP_CT */
1191 [] BSSGP_SP_SIG.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001192 BVC.send(ts_ptp_BnsUdReq(bs_pdu, 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001193 }
1194
1195 /* ConnHdlr sends BSSGP on BVCI=PTP port: forward it to BSSGP_CT */
1196 [] BSSGP_SP.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001197 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001198 }
1199
1200#ifdef BSSGP_EM_L3
1201 /* ConnHdlr sends L3: Encode and send as UL_UD / DL_UD */
1202 [g_sgsn_role] BSSGP_SP.receive(PDU_L3_SGSN_MS:?) -> value l3_mt sender vc_conn {
1203 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001204 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001205 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(l3_mt);
1206 var BIT4 sapi := f_llc_sapi_by_l3_mt(l3_mt);
1207 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1208 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 +01001209 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001210 }
1211 [not g_sgsn_role] BSSGP_SP.receive(PDU_L3_MS_SGSN:?) -> value l3_mo sender vc_conn {
1212 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001213 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001214 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(l3_mo);
1215 var BIT4 sapi := f_llc_sapi_by_l3_mo(l3_mo);
1216 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1217 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 +01001218 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 +02001219 }
1220#endif
1221
1222 /* ConnHdlr sends raw LLC: Encode and send as UL_UD / DL_UD */
1223 [not g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1224 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001225 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001226 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001227 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 +02001228 }
1229 [g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1230 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001231 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001232 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001233 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001234 }
Harald Welte199f3862020-11-15 22:37:45 +01001235
Harald Welte71449b02020-12-09 11:58:23 +01001236 /* Testcase sends us BSSGP on global port */
1237 [] GLOBAL.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
1238 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
1239 }
1240
Harald Welte199f3862020-11-15 22:37:45 +01001241 [] MGMT.receive(BssgpBlockRequest:?) -> value bbr {
1242 f_ptp_sendBlock(bbr.cause);
1243 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001244}
1245
Harald Welteab50cc22020-11-25 17:11:08 +01001246private altstep as_ptp_handle_inbound_reset() runs on BSSGP_BVC_CT {
1247 var NsUnitdataIndication udi;
1248 /* we are a SGSN: BSS/PCU-originated RESET must have a cell ID */
1249 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
1250 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001251 if (MGMT.checkstate("Connected")) {
1252 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1253 }
Harald Welteab50cc22020-11-25 17:11:08 +01001254 /* Respond to RESET with correct BVCI but without CellID */
1255 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, omit), 0, g_bvc_lsp));
1256 /* FIXME: reset all state? What about clients? */
1257 f_ptp_change_state(BVC_S_UNBLOCKED);
1258 }
1259 /* we are a BSS/PCU: SGSN-originated RESET must not have a cell ID */
1260 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, omit), 0)) -> value udi {
1261 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001262 if (MGMT.checkstate("Connected")) {
1263 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1264 }
Harald Welteab50cc22020-11-25 17:11:08 +01001265 /* Respond to RESET with correct BVCI with CellID */
1266 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0, g_bvc_lsp));
1267 /* FIXME: reset all state? What about clients? */
1268 f_ptp_change_state(BVC_S_UNBLOCKED);
1269 }
1270}
1271
Harald Welte5339b2e2020-10-04 22:52:56 +02001272/* Events permitted in all states */
1273private altstep as_ptp_allstate() runs on BSSGP_BVC_CT {
1274 var NsUnitdataIndication udi;
Harald Welte199f3862020-11-15 22:37:45 +01001275 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001276 var BSSGP_Client_CT vc_conn;
1277 var OCT4 tlli, tlli_old;
1278 var hexstring imsi;
1279
1280 /* Signaling BVC was reset */
1281 [] BVC.receive(BssgpResetIndication:{0}) {
Harald Welte199f3862020-11-15 22:37:45 +01001282 if (MGMT.checkstate("Connected")) {
1283 MGMT.send(BssgpResetIndication:{0});
1284 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001285 if (not g_sgsn_role) {
1286 f_ptp_change_state(BVC_S_BLOCKED);
1287 /* when the BSS side signaling PTP is RESET, all PTP BVC must start the
1288 * reset procedure */
1289 f_ptp_sendReset();
1290 } else {
1291 f_ptp_change_state(BVC_S_UNBLOCKED);
1292 }
1293 }
1294
Harald Welte5339b2e2020-10-04 22:52:56 +02001295 /* Ignore those for now */
1296 [] BVC.receive(ASP_Event:?) { }
1297 [] BVC.receive(BssgpStatusIndication:?) { }
1298
1299 /* catch-all */
1300 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1301 setverdict(fail, "Received unexpected NS (SIG): ", udi);
1302 }
1303 [] BVC.receive(tr_ptp_BnsUdInd(?, ?)) -> value udi {
1304 setverdict(fail, "Received unexpected NS (PTP): ", udi);
1305 }
1306 [] BVC.receive {
1307 setverdict(fail, "Received unexpected message from BSSGP_CT");
1308 }
1309
1310 /* registration/deregistration of Clients (per-TLLI components) */
1311 [] BSSGP_PROC.getcall(BSSGP_register_client:{?,?}) -> param(imsi, tlli) sender vc_conn {
1312 f_tbl_client_add(imsi, tlli, vc_conn);
1313 BSSGP_PROC.reply(BSSGP_register_client:{imsi, tlli}) to vc_conn;
1314 }
1315 [] BSSGP_PROC.getcall(BSSGP_unregister_client:{?}) -> param(imsi) sender vc_conn {
1316 f_tbl_client_del(imsi, vc_conn);
1317 BSSGP_PROC.reply(BSSGP_unregister_client:{imsi}) to vc_conn;
1318 }
1319 [] BSSGP_PROC.getcall(BSSGP_llgmm_assign:{?,?}) -> param(tlli_old, tlli) sender vc_conn {
1320 f_tbl_client_llgmm_assign(tlli_old, tlli, vc_conn);
1321 BSSGP_PROC.reply(BSSGP_llgmm_assign:{tlli_old, tlli}) to vc_conn;
1322 }
Harald Welte199f3862020-11-15 22:37:45 +01001323
1324 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
Harald Welte52cecbb2020-11-25 21:57:31 +01001325 f_ptp_change_state(BVC_S_BLOCKED);
Harald Welte199f3862020-11-15 22:37:45 +01001326 f_ptp_sendReset(brr.cause);
1327 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001328}
1329
1330/* main loop for per-BVC component */
1331private function f_bssgp_bvc_ScanEvents() runs on BSSGP_BVC_CT {
1332 while (true) {
1333 alt {
1334 [g_ptp_bvc_state == BVC_S_BLOCKED] as_ptp_blocked();
1335 [g_ptp_bvc_state == BVC_S_UNBLOCKED] as_ptp_unblocked();
1336 [] as_ptp_allstate();
1337 }
1338 }
1339}
1340
1341/* main function for per-BVC Component */
Harald Weltefba7afd2020-11-24 23:12:31 +01001342private 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 +02001343 g_cfg := cfg;
Harald Weltefba7afd2020-11-24 23:12:31 +01001344 g_nsei := nsei;
Harald Weltec4505522020-11-11 18:55:09 +01001345 g_bvc_lsp := cfg.bvci;
Harald Welte5339b2e2020-10-04 22:52:56 +02001346 g_sgsn_role := sgsn_role;
1347 f_bssgp_bvc_ScanEvents();
1348}
1349
Harald Weltec4505522020-11-11 18:55:09 +01001350template (value) NsUnitdataRequest ts_ptp_BnsUdReq(template (value) PDU_BSSGP pdu, template (value) BssgpBvci bvci,
1351 template (value) integer lsp) := {
Harald Welte5339b2e2020-10-04 22:52:56 +02001352 bvci := bvci,
1353 nsei := 0, // overwritten in BSSGP_CT
Harald Weltec4505522020-11-11 18:55:09 +01001354 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +02001355 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
1356 * unbound integer value." when trying to send the reocrd rather than the octetstring */
1357 //sdu := omit,
1358 //bssgp := valueof(pdu)
1359 sdu := enc_PDU_BSSGP(valueof(pdu)),
1360 bssgp := omit
1361}
1362
1363template (present) NsUnitdataIndication tr_ptp_BnsUdInd(template (present) PDU_BSSGP pdu, template (present) BssgpBvci bvci) := {
1364 bvci := bvci,
1365 nsei := ?,
Harald Welte80a249a2020-11-17 19:57:40 +01001366 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +02001367 sdu := *,
1368 bssgp := pdu
1369}
1370
1371/* change state of PTP BVC; broadcast state change to clients */
1372private function f_ptp_change_state(BvcState new_state) runs on BSSGP_BVC_CT {
1373 if (new_state == g_ptp_bvc_state) {
1374 return;
1375 }
1376 log("BVCI(", g_cfg.bvci, ") State Transition: ", g_ptp_bvc_state, " -> ", new_state);
1377 g_ptp_bvc_state := new_state;
Harald Welte199f3862020-11-15 22:37:45 +01001378 if (MGMT.checkstate("Connected")) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001379 MGMT.send(ts_BssgpStsInd(g_nsei, g_cfg.bvci, g_ptp_bvc_state));
Harald Welte199f3862020-11-15 22:37:45 +01001380 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001381 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1382 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001383 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 +02001384 }
1385 }
Harald Welte5ac31492018-02-15 20:39:13 +01001386}
1387
1388/* attempt to extract the TLLI from a BSSGP PDU */
1389function f_bssgp_get_tlli(PDU_BSSGP bssgp) return template OCT4 {
1390 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
1391 return bssgp.pDU_BSSGP_DL_UNITDATA.tLLI_current;
1392 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
1393 return bssgp.pDU_BSSGP_UL_UNITDATA.tLLI;
1394 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY)) {
1395 return bssgp.pDU_BSSGP_RA_CAPABILITY.tLLI.tLLI_Value;
1396 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE)) {
1397 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE.tLLI.tLLI_Value;
1398 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK)) {
1399 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK.tLLI.tLLI_Value;
Harald Welte650bd722021-01-17 10:58:56 +01001400 } else if (ischosen(bssgp.pDU_BSSGP_RADIO_STATUS) and
1401 ispresent(bssgp.pDU_BSSGP_RADIO_STATUS.tLLI)) {
Harald Welte5ac31492018-02-15 20:39:13 +01001402 return bssgp.pDU_BSSGP_RADIO_STATUS.tLLI.tLLI_Value;
1403 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND)) {
1404 return bssgp.pDU_BSSGP_SUSPEND.tLLI.tLLI_Value;
1405 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_ACK)) {
1406 return bssgp.pDU_BSSGP_SUSPEND_ACK.tLLI.tLLI_Value;
1407 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_NACK)) {
1408 return bssgp.pDU_BSSGP_SUSPEND_NACK.tLLI.tLLI_Value;
1409 } else if (ischosen(bssgp.pDU_BSSGP_RESUME)) {
1410 return bssgp.pDU_BSSGP_RESUME.tLLI.tLLI_Value;
1411 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_ACK)) {
1412 return bssgp.pDU_BSSGP_RESUME_ACK.tLLI.tLLI_Value;
1413 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_NACK)) {
1414 return bssgp.pDU_BSSGP_RESUME_NACK.tLLI.tLLI_Value;
1415 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL)) {
1416 return bssgp.pDU_BSSGP_FLUSH_LL.tLLI.tLLI_Value;
1417 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL_ACK)) {
1418 return bssgp.pDU_BSSGP_FLUSH_LL_ACK.tLLI.tLLI_Value;
1419 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1420 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1421 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1422 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1423 } else if (ischosen(bssgp.pDU_BSSGP_PAGING_CS) and
1424 isvalue(bssgp.pDU_BSSGP_PAGING_CS.tLLI)) {
1425 return bssgp.pDU_BSSGP_PAGING_CS.tLLI.tLLI_Value;
1426 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS)) {
1427 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS.tLLI.tLLI_Value;
1428 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK)) {
1429 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK.tLLI.tLLI_Value;
1430 }
1431 /* TODO: Handover, PFC, LCS */
1432 return omit;
1433}
1434
Harald Weltef70997d2018-02-17 10:11:19 +01001435
Harald Welte5ac31492018-02-15 20:39:13 +01001436
Harald Welte1733a382017-07-23 17:26:17 +02001437}