blob: 4b93a26063711477acdcffae147d5afdc2c7d47b [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 Welte12d19b82020-10-09 11:41:06 +020040
41modulepar {
42 /* tolerate CellID absence/presence in BVC-RESET in violation to spec */
43 boolean mp_tolerate_bvc_reset_cellid := false;
44}
45
Harald Welte5ac31492018-02-15 20:39:13 +010046/***********************************************************************
Harald Welte5339b2e2020-10-04 22:52:56 +020047 * Communication between Client Components and per-BVC component
Harald Welte5ac31492018-02-15 20:39:13 +010048 ***********************************************************************/
49
Harald Welte853c0ad2018-02-15 17:45:29 +010050type record BssgpStatusIndication {
Harald Welte5339b2e2020-10-04 22:52:56 +020051 Nsei nsei optional,
Harald Welte853c0ad2018-02-15 17:45:29 +010052 BssgpBvci bvci,
53 BvcState state
Harald Welte5339b2e2020-10-04 22:52:56 +020054};
55
56type record BssgpResetIndication {
57 BssgpBvci bvci
58};
59
60template (value) BssgpStatusIndication
61ts_BssgpStsInd(template (omit) Nsei nsei, template (value) BssgpBvci bvci,
62 template (value) BvcState state) := {
63 nsei := nsei,
64 bvci := bvci,
65 state := state
Harald Welte853c0ad2018-02-15 17:45:29 +010066}
67
Harald Welte5339b2e2020-10-04 22:52:56 +020068template (present) BssgpStatusIndication
69tr_BssgpStsInd(template Nsei nsei, template (present) BssgpBvci bvci,
70 template (present) BvcState state) := {
Harald Welte853c0ad2018-02-15 17:45:29 +010071 nsei := nsei,
72 bvci := bvci,
73 state := state
74}
75
76type enumerated BvcState {
Harald Welte5339b2e2020-10-04 22:52:56 +020077 /* SGSN role: waiting to be reset by the peer */
78 BVC_S_WAIT_RESET,
79 /* PCU role: waiting for NS_ALIVE_UNBLOCKED */
80 BVC_S_WAIT_NS_ALIVE_UNBLOCKED,
81 /* BVC-BLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010082 BVC_S_BLOCKED,
Harald Welte5339b2e2020-10-04 22:52:56 +020083 /* BVC-UNBLOCKED state */
Harald Welte853c0ad2018-02-15 17:45:29 +010084 BVC_S_UNBLOCKED
85};
86
87/* port from our (internal) point of view */
88type port BSSGP_SP_PT message {
Harald Welte5ac31492018-02-15 20:39:13 +010089 in PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +020090 PDU_LLC
91#ifdef BSSGP_EM_L3
92 ,
Harald Welte5ac31492018-02-15 20:39:13 +010093 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +020094 PDU_L3_SGSN_MS
95#endif
96 ;
Harald Welte955aa942019-05-03 01:29:29 +020097 out PDU_BSSGP,
98 PDU_LLC,
99 PDU_SN,
Harald Welte853c0ad2018-02-15 17:45:29 +0100100 NsStatusIndication,
101 BssgpStatusIndication,
Harald Welte93331e72020-09-12 20:51:05 +0200102 ASP_Event
103#ifdef BSSGP_EM_L3
104 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100105 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200106 PDU_L3_SGSN_MS
107#endif
108 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100109} with { extension "internal" };
110
111/* port from the user point of view */
112type port BSSGP_PT message {
113 in ASP_Event,
114 NsStatusIndication,
115 BssgpStatusIndication,
Harald Welte955aa942019-05-03 01:29:29 +0200116 PDU_BSSGP,
117 PDU_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200118 PDU_SN
119#ifdef BSSGP_EM_L3
120 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100121 PDU_L3_MS_SGSN,
Harald Welte93331e72020-09-12 20:51:05 +0200122 PDU_L3_SGSN_MS
123#endif
124 ;
Harald Welte5ac31492018-02-15 20:39:13 +0100125 out PDU_BSSGP,
Harald Welte93331e72020-09-12 20:51:05 +0200126 PDU_LLC
127#ifdef BSSGP_EM_L3
128 ,
Harald Welte5ac31492018-02-15 20:39:13 +0100129 PDU_L3_SGSN_MS,
Harald Welte93331e72020-09-12 20:51:05 +0200130 PDU_L3_MS_SGSN
131#endif
132 ;
Harald Welte853c0ad2018-02-15 17:45:29 +0100133} with { extension "internal" };
134
Harald Welte5339b2e2020-10-04 22:52:56 +0200135signature BSSGP_register_client(hexstring imsi, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100136signature BSSGP_unregister_client(hexstring imsi);
Harald Weltef70997d2018-02-17 10:11:19 +0100137signature BSSGP_llgmm_assign(OCT4 tlli_old, OCT4 tlli);
Harald Welte5ac31492018-02-15 20:39:13 +0100138
139type port BSSGP_PROC_PT procedure {
Harald Weltef70997d2018-02-17 10:11:19 +0100140 inout BSSGP_register_client, BSSGP_unregister_client, BSSGP_llgmm_assign;
Harald Welte5ac31492018-02-15 20:39:13 +0100141} with { extension "internal" };
142
143
Harald Welte5339b2e2020-10-04 22:52:56 +0200144
Harald Welte5ac31492018-02-15 20:39:13 +0100145/***********************************************************************
146 * Client Component for a single MS/TLLI
Harald Welte5339b2e2020-10-04 22:52:56 +0200147 ***********************************************************************
148 * This is what most users will want to derive their test cases from. It
149 * provides a set of three different ports (PTP, signaling, procedure)
150 * for (currently up to 3) different Cells. Those ports are all connected
151 * to one or multiple different per-BVC compoennts, depending on whether
152 * they share the same BSS or not.
Harald Welte5ac31492018-02-15 20:39:13 +0100153 ***********************************************************************/
154
155type component BSSGP_Client_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200156 /* one port array for each client; allows talking to up to 3 BVC/Cell (handover, ...) */
157 port BSSGP_PT BSSGP[3]; /* PTP-BVC */
158 port BSSGP_PT BSSGP_SIG[3]; /* Signaling BVC */
Harald Welte9b461a92020-12-10 23:41:14 +0100159 port BSSGP_PT BSSGP_GLOBAL[3]; /* Signaling BVC */
Harald Welte5339b2e2020-10-04 22:52:56 +0200160 port BSSGP_PROC_PT BSSGP_PROC[3]; /* registration / deregistration */
Harald Welte5ac31492018-02-15 20:39:13 +0100161};
162
Harald Welte5339b2e2020-10-04 22:52:56 +0200163function f_bssgp_client_register(hexstring imsi, OCT4 tlli, BSSGP_PROC_PT PT := BSSGP_PROC[0])
164runs on BSSGP_Client_CT {
165 PT.call(BSSGP_register_client:{imsi, tlli}) {
166 [] PT.getreply(BSSGP_register_client:{imsi, tlli}) {};
167 }
168}
169
170function f_bssgp_client_unregister(hexstring imsi, BSSGP_PROC_PT PT := BSSGP_PROC[0])
171runs on BSSGP_Client_CT {
172 PT.call(BSSGP_unregister_client:{imsi}) {
173 [] PT.getreply(BSSGP_unregister_client:{imsi}) {};
174 }
175}
176
177/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
178function f_bssgp_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_PROC_PT PT := BSSGP_PROC[0])
179runs on BSSGP_Client_CT {
180 PT.call(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {
181 [] PT.getreply(BSSGP_llgmm_assign:{tlli_old, tlli_new}) {};
182 }
183}
184
Harald Welte5ac31492018-02-15 20:39:13 +0100185/***********************************************************************
186 * Main Component
Harald Welte5339b2e2020-10-04 22:52:56 +0200187 ***********************************************************************
188 * This component exists once, and it runs on to of the NS_Emulation.
189 * It handles the BVC-RESET for the signaling BVCI, and dispatches messages
190 * to the per-BVC components running above it. Messages are routed by:
191 * - PTP BVCI in NS header (if non-0)
192 * - BVCI IE in messages on BVCI=0 in NS header
193 * Messages for which no unique BVC can be identified (like paging without
194 * a BVCI IE set) are broadcast to all BVC components.
195 * We also broadcast state changes (BssgpStatusIndication) and reset events
196 * (BssgpResetIndication) of the signaling BVC to all per-BVC components.
Harald Welte5ac31492018-02-15 20:39:13 +0100197 ***********************************************************************/
198
Harald Welte5339b2e2020-10-04 22:52:56 +0200199function BssgpStart(BssgpConfig cfg, charstring id) runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100200 g_cfg := cfg;
Harald Welte5339b2e2020-10-04 22:52:56 +0200201
202 /* create the per-BVC components based on the configuration */
203 for (var integer i := 0; i < lengthof(g_cfg.bvc); i := i+1) {
204 var BssgpBvcConfig bvc_cfg := g_cfg.bvc[i];
205 var charstring bvc_id := id & "-BVCI" & int2str(bvc_cfg.bvci);
206 /* create, connect and start the BVC component */
207 var BSSGP_BVC_CT bvc_ct := BSSGP_BVC_CT.create(bvc_id);
208 connect(bvc_ct:BVC, self:BVC);
Harald Weltefba7afd2020-11-24 23:12:31 +0100209 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 +0200210 /* populate the BVC state table */
211 BvcTable[i] := {
212 bvci := bvc_cfg.bvci,
213 cell_id := bvc_cfg.cell_id,
214 comp_ref := bvc_ct
215 };
216 }
217
218 if (g_cfg.sgsn_role) {
219 /* The SGSN side waits for an inbound BVC-RESET on the signaling BVC */
220 f_sign_change_state(BVC_S_WAIT_RESET);
221 } else {
222 /* The BSS/PCU side waits for an inbound NsStatusIndication to Tx BVC-RESET */
223 f_sign_change_state(BVC_S_WAIT_NS_ALIVE_UNBLOCKED);
224 }
225
226 /* main loop */
227 f_bssgp_ScanEvents();
Harald Welte853c0ad2018-02-15 17:45:29 +0100228}
229
Harald Welte5339b2e2020-10-04 22:52:56 +0200230/* master component, exists once per BSSGP instance (NSE) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100231type component BSSGP_CT {
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200232 /* UDP ports towards the bottom (IUT) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100233 port NS_PT BSCP;
Harald Welte5339b2e2020-10-04 22:52:56 +0200234
235 /* control by the user */
236 port BSSGP_CT_PROC_PT PROC;
237
Harald Welte57de2202020-11-24 18:15:02 +0100238 /* global port for procedures without any relation to a BVC
239 * (currently only) SUSPEND/RESUME */
240 port BSSGP_SP_PT GLOBAL;
241
Harald Welte0083ea62020-12-02 21:30:44 +0100242 /* RAN INFORMATION MGMT */
243 port BSSGP_SP_PT RIM;
244
Harald Weltebdf96d82020-11-27 18:58:46 +0100245 /* port to a management instance */
246 port BSSGP_BVC_MGMT_SP_PT MGMT;
247
Harald Welte5339b2e2020-10-04 22:52:56 +0200248 var BssgpConfig g_cfg;
249
250 /* Signaling BVC (BVCI=0) */
251 var BvcState g_sign_bvc_state := BVC_S_WAIT_RESET;
252 timer g_T2 := 60.0;
253
254 /* port to per-BVC components */
255 port BSSGP_BVC_SP_PT BVC;
256 /* per-BVC state table */
Harald Welte633805b2021-02-03 17:47:18 +0100257 var BvcEntity BvcTable[64];
Harald Welte5339b2e2020-10-04 22:52:56 +0200258};
259
260/* one element in the per-BVC state table */
261type record BvcEntity {
262 /* PTP BVCI of this BVC/Cell */
263 BssgpBvci bvci,
264 /* Cell Identity of this cell */
265 BssgpCellId cell_id,
266 /* reference to the per-BVC/cell component */
267 BSSGP_BVC_CT comp_ref
268};
269
270/* find the per-BVC component for a given BVCI */
271private function f_comp_for_bvci(BssgpBvci bvci) runs on BSSGP_CT return BSSGP_BVC_CT {
272 var integer i;
273
274 for (i := 0; i < lengthof(BvcTable); i := i+1) {
275 if (BvcTable[i].bvci == bvci) {
276 return BvcTable[i].comp_ref;
277 }
278 }
279 return null;
280}
281
282/* We are in BVC_S_WAIT_RSET state (only happens in SGSN role) */
283altstep as_sig_wait_reset() runs on BSSGP_CT {
284 var NsUnitdataIndication udi;
285
286 /* Respond to RESET for signalling BVCI 0 */
287 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
288 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5339b2e2020-10-04 22:52:56 +0200289 f_sign_change_state(BVC_S_UNBLOCKED);
Harald Weltebdf96d82020-11-27 18:58:46 +0100290 if (MGMT.checkstate("Connected")) {
291 MGMT.send(BssgpResetIndication:{0});
292 }
293 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200294 }
295
296 /* work-around for old, buggy libosmogb before Change-Id Ie87820537d6d616da4fd4bbf73eab06e28fda5e1 */
297 [mp_tolerate_bvc_reset_cellid] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, g_cfg.bvc[0].cell_id), 0)) -> value udi {
298 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5339b2e2020-10-04 22:52:56 +0200299 f_sign_change_state(BVC_S_UNBLOCKED);
Harald Weltebdf96d82020-11-27 18:58:46 +0100300 if (MGMT.checkstate("Connected")) {
301 MGMT.send(BssgpResetIndication:{0});
302 }
303 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200304 }
305}
306
Harald Welte27a70cc2020-12-09 11:57:41 +0100307private template PDU_BSSGP tr_GLOBAL_SIG := (
Harald Welte57de2202020-11-24 18:15:02 +0100308 {pDU_BSSGP_SUSPEND:=?}, {pDU_BSSGP_SUSPEND_ACK:=?}, {pDU_BSSGP_SUSPEND_NACK:=?},
Harald Weltef8e5c5d2020-11-27 22:37:23 +0100309 {pDU_BSSGP_RESUME:=?}, {pDU_BSSGP_RESUME_ACK:=?}, {pDU_BSSGP_RESUME_NACK:=?},
Harald Weltef5365242021-01-17 13:46:57 +0100310 {pDU_BSSGP_SGSN_INVOKE_TRACE:=?}, {pDU_BSSGP_OVERLOAD:=?}, {pDU_BSSGP_STATUS:=?}
Harald Welte57de2202020-11-24 18:15:02 +0100311);
312
Harald Welte0083ea62020-12-02 21:30:44 +0100313/* BSSGP messages that should arrive on the RIM port */
314private template PDU_BSSGP tr_RIM := (
315 {pDU_BSSGP_RAN_INFORMATION:=?}, {pDU_BSSGP_RAN_INFORMATION_REQUEST:=?},
316 {pDU_BSSGP_RAN_INFORMATION_ACK:=?}, {pDU_BSSGP_RAN_INFORMATION_ERROR:=?},
317 {pDU_BSSGP_RAN_INFORMATION_APPLICATION_ERROR:=?}
318);
319
Harald Welte5339b2e2020-10-04 22:52:56 +0200320/* We are in BVC_S_UNBLOCKED state */
321altstep as_sig_unblocked() runs on BSSGP_CT {
322 var BSSGP_BVC_CT bvc_comp_ref;
323 var BSSGP_Client_CT vc_conn;
324 var NsUnitdataIndication udi;
325 var NsUnitdataRequest udr;
Harald Welte57de2202020-11-24 18:15:02 +0100326 var PDU_BSSGP bssgp;
Harald Welte5339b2e2020-10-04 22:52:56 +0200327
328 /* Messages PTP BVCI in BVCI field of NS: dispatch by that */
329 [] BSCP.receive(f_BnsUdInd(?, (2..65535))) -> value udi {
330 bvc_comp_ref := f_comp_for_bvci(valueof(udi.bvci));
331 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
332 /* dispatch to BVC component */
333 BVC.send(udi) to bvc_comp_ref;
334 } else {
335 setverdict(fail, "Rx BSSGP for unknown PTP BVCI ", udi.bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100336 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 +0200337 }
338 }
339
340 /* Messages with BVCI = 0 (Signaling) in BVCI field of NS */
Harald Welte27a70cc2020-12-09 11:57:41 +0100341 [] BSCP.receive(f_BnsUdInd(tr_GLOBAL_SIG, 0)) -> value udi {
Harald Welte57de2202020-11-24 18:15:02 +0100342 GLOBAL.send(udi.bssgp);
343 }
Harald Welte0083ea62020-12-02 21:30:44 +0100344 [] BSCP.receive(f_BnsUdInd(tr_RIM, 0)) -> value udi {
345 if (RIM.checkstate("Connected")) {
346 RIM.send(udi.bssgp);
347 }
348 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200349
350 /* Route based on PTP BVCI in payload/IE of signaling PDU */
351 [] BSCP.receive(f_BnsUdInd(?, 0)) -> value udi {
352 var template (omit) BssgpBvci ptp_bvci := f_BSSGP_BVCI_get(udi.bssgp);
353 if (istemplatekind(ptp_bvci, "omit")) {
354 log("Rx on SIG BVCI without PTP BVCI: broadcasting");
355 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
356 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
357 BVC.send(udi) to BvcTable[i].comp_ref;
358 }
359 }
360 } else {
361 bvc_comp_ref := f_comp_for_bvci(valueof(ptp_bvci));
362 if (isbound(bvc_comp_ref) and bvc_comp_ref != null) {
363 /* dispatch to BVC component */
364 BVC.send(udi) to bvc_comp_ref;
365 } else {
366 setverdict(fail, "Rx SIG BSSGP for unknown PTP BVCI ", ptp_bvci, ": ", udi.bssgp);
Harald Weltec4505522020-11-11 18:55:09 +0100367 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 +0100368 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200369 }
370 }
371
372 /* Handle PS-PAGING on SIGN BVCI with no conditional BVCI IE */
373 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_PS:=?}, 0)) -> value udi {
374 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
375 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
376 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
377 BVC.send(udi) to BvcTable[i].comp_ref;
378 }
379 }
380 }
381 /* Handle CS-PAGING on SIGN BVCI with no conditional BVCI IE */
382 [] BSCP.receive(f_BnsUdInd(PDU_BSSGP:{pDU_BSSGP_PAGING_CS:=?}, 0)) -> value udi {
383 /* FIXME: use LA, RA or BSS Area to dispatch instead of broadcast */
384 for (var integer i := 0; i < lengthof(BvcTable); i := i+1) {
385 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
386 BVC.send(udi) to BvcTable[i].comp_ref;
387 }
388 }
389 }
390
391 /* per-BVC component sends us something; forward to NS without any validation */
392 [] BVC.receive(NsUnitdataRequest:?) -> value udr {
393 /* patch in the NSEI, as the per-BVC components have no idea about it */
394 udr.nsei := g_cfg.nsei;
395 BSCP.send(udr);
396 }
Harald Welte57de2202020-11-24 18:15:02 +0100397
Harald Welte27a70cc2020-12-09 11:57:41 +0100398 [] GLOBAL.receive(tr_GLOBAL_SIG) -> value bssgp {
Harald Welte57de2202020-11-24 18:15:02 +0100399 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
400 }
Harald Welte0083ea62020-12-02 21:30:44 +0100401
402 [] RIM.receive(tr_RIM) -> value bssgp {
403 BSCP.send(f_BnsUdReq(bssgp, 0, 0));
404 }
405
Harald Welte5339b2e2020-10-04 22:52:56 +0200406}
407
408/* We are in BVC_S_WAIT_NS_ALIVE_UNBLOCKED (only happens in BSS role) */
409altstep as_sig_wait_ns_alive_unblocked() runs on BSSGP_CT {
410 var NsStatusIndication nsi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100411 [] 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 +0200412 /* if we just became NS-unblocked, send a BCC-RESET */
413 if (g_cfg.sgsn_role == false) {
Harald Weltec4505522020-11-11 18:55:09 +0100414 BSCP.send(f_BnsUdReq(ts_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, 0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200415 g_T2.start;
416 /* The BVC_RESET_ACK is handled in the as_sig_allstate() below */
417 }
418 /* Idea: We could send BVC-UNBLOCK here like some SGSN do */
419 }
420
421}
422
Harald Weltee5887922020-11-27 19:04:46 +0100423/* handling of events irrespective of BVC state (before state-specific handling) */
424altstep as_sig_allstate_pre() runs on BSSGP_CT {
425 var NsUnitdataIndication udi;
426
427 /* Respond to RESET for signalling BVCI 0 */
428 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
429 log("Rx BVC-RESET for Signaling BVCI=0");
430 if (MGMT.checkstate("Connected")) {
431 MGMT.send(BssgpResetIndication:{0});
432 }
433 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
434 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
435 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
436 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
437 }
438 }
439 }
440
441 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
442 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(0, ?), 0)) -> value udi {
443 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
444 }
445 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(0), 0)) -> value udi {
446 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
447 }
448 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(0), 0)) -> value udi {
449 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
450 }
451 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
452 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
453 }
454
455 /* BVC-RESET-ACK for BVCI=0 while T2 is running: Answer to a BVC-RESET we sent earlier */
456 [g_T2.running] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(0, omit), 0)) -> value udi {
457 log("BVCI(0) Rx BVC-RESET-ACK");
458 g_T2.stop;
459 f_sign_change_state(BVC_S_UNBLOCKED);
460 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
461 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
462 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
463 }
464 }
465 }
466 [] g_T2.timeout {
467 setverdict(fail, "Timeout waiting for BVC-RESET-ACK on BVCI=0");
468 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(udi.bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
469 g_T2.start;
470 }
471}
472
473/* handling of events irrespective of BVC state (after state-specific handling) */
474altstep as_sig_allstate_post() runs on BSSGP_CT {
Harald Welte5339b2e2020-10-04 22:52:56 +0200475 var BSSGP_Client_CT vc_conn;
476 var NsUnitdataIndication udi;
477 var NsStatusIndication nsi;
478 var ASP_Event evt;
479 var BSSGP_BVC_CT bvc_comp_ref;
480 var BssgpBvci bvci;
Harald Weltee5887922020-11-27 19:04:46 +0100481 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +0200482
483 /* Respond to RESET for signalling BVCI 0 */
484 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, omit), 0)) -> value udi {
485 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Weltebdf96d82020-11-27 18:58:46 +0100486 if (MGMT.checkstate("Connected")) {
487 MGMT.send(BssgpResetIndication:{0});
488 }
Harald Weltec4505522020-11-11 18:55:09 +0100489 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, omit), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200490 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
491 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
492 BVC.send(BssgpResetIndication:{0}) to BvcTable[i].comp_ref;
493 }
494 }
495 }
496
497 /* any BLOCK or UNBLOCK for the SIGNALING BVCI are illegal */
498 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(0, ?), 0)) -> value udi {
499 setverdict(fail, "Rx BVC-BLOCK illegal for BVCI=0: ", udi);
500 }
501 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(0), 0)) -> value udi {
502 setverdict(fail, "Rx BVC-UNBLOCK illegal for BVCI=0: ", udi);
503 }
504 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(0), 0)) -> value udi {
505 setverdict(fail, "Rx BVC-BLOCK-ACK illegal for BVCI=0: ", udi);
506 }
507 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(0), 0)) -> value udi {
508 setverdict(fail, "Rx BVC-UNBLOCK-ACK illegal for BVCI=0: ", udi);
509 }
510
511 /* Respond to BLOCK for wrong BVCI */
512 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(?, ?), 0)) -> value udi {
513 setverdict(fail, "Rx BVC-BLOCK for unknown BVCI");
Harald Weltec4505522020-11-11 18:55:09 +0100514 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200515 }
516
517 /* Respond to RESET with wrong BVCI */
518 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, ?, ?), 0)) -> value udi {
519 var BssgpBvci ptp_bvci := oct2int(udi.bssgp.pDU_BSSGP_BVC_RESET.bVCI.unstructured_value);
520 setverdict(fail, "Rx BVC-RESET for unknown BVCI ", ptp_bvci);
Harald Weltec4505522020-11-11 18:55:09 +0100521 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(ptp_bvci, BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp), 0, 0));
Harald Welte5339b2e2020-10-04 22:52:56 +0200522 }
523
Harald Welte5339b2e2020-10-04 22:52:56 +0200524 /* Forwarding of ASP_Event to per-BVC components */
525 [] BSCP.receive(ASP_Event:?) -> value evt {
526 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
527 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
528 BVC.send(evt) to BvcTable[i].comp_ref;
529 }
530 }
531 }
532 /* Keep NS Status Indicaitons to us; no need to inform per-BVC components [for now?] */
Harald Weltedc0a0902020-11-10 21:03:29 +0100533 [] BSCP.receive(tr_NsStsInd(g_cfg.nsei)) -> value nsi { }
Harald Welte445fc612020-11-10 19:15:30 +0100534 /* We should never see any different NSEI: There's one BSSGP_CT per NSE */
Harald Weltedc0a0902020-11-10 21:03:29 +0100535 [] BSCP.receive(tr_NsStsInd(?)) -> value nsi {
Harald Welte445fc612020-11-10 19:15:30 +0100536 setverdict(fail, "Rx NsStatusInd for wrong NSEI ", nsi);
537 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200538
539 /* Procedure port request to resolve the per-BVC component for a given ptp BVCI */
540 [] PROC.getcall(BSSGP_get_bvci_ct:{?}) -> param(bvci) sender vc_conn {
541 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
542 if (BvcTable[i].bvci == bvci) {
543 PROC.reply(BSSGP_get_bvci_ct:{bvci} value BvcTable[i].comp_ref) to vc_conn;
544 return;
545 }
546 }
547 }
Harald Weltee5887922020-11-27 19:04:46 +0100548
549 /* default case of handling unknown PDUs */
550 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
551 setverdict(fail, "Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_sign_bvc_state);
552 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(0, BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp), 0, 0));
553 }
554
555 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
556 BSCP.send(f_BnsUdReq(ts_BVC_RESET(brr.cause, 0, omit), 0, 0));
557 g_T2.start;
558 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200559}
560
561/* send the highest decoded layer of the message through given port */
562private function f_send_bssgp_dec(BssgpDecoded dec, BSSGP_Client_CT vc_conn, BSSGP_SP_PT pt) {
563#ifdef BSSGP_EM_L3
564 if (ispresent(dec.l3_mt)) {
565 pt.send(dec.l3_mt) to vc_conn;
566 } else if (ispresent(dec.l3_mo)) {
567 pt.send(dec.l3_mo) to vc_conn;
568 } else
569#endif
570 if (ispresent(dec.sndcp)) {
571 pt.send(dec.sndcp) to vc_conn;
572 } else if (ispresent(dec.llc)) {
573 pt.send(dec.llc) to vc_conn;
574 } else {
575 pt.send(dec.bssgp) to vc_conn;
576 }
577}
578
579
580function f_llc_get_n_u_tx(inout LLC_Entity llc) return uint9_t {
581 var uint9_t ret := llc.n_u_tx_next;
582 llc.n_u_tx_next := llc.n_u_tx_next + 1;
583 return ret;
584}
585
586#ifdef BSSGP_EM_L3
587function f_llc_sapi_by_l3_mo(PDU_L3_MS_SGSN l3_mo) return BIT4 {
588 if (ischosen(l3_mo.msgs.gprs_mm)) {
589 return c_LLC_SAPI_LLGMM;
590 } else if (ischosen(l3_mo.msgs.gprs_sm)) {
591 return c_LLC_SAPI_LLGMM;
592 } else if (ischosen(l3_mo.msgs.sms)) {
593 return c_LLC_SAPI_LLSMS;
594 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100595 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mo));
596 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200597}
598
599private function f_llc_sapi_by_l3_mt(PDU_L3_SGSN_MS l3_mt) return BIT4 {
600 if (ischosen(l3_mt.msgs.gprs_mm)) {
601 return c_LLC_SAPI_LLGMM;
602 } else if (ischosen(l3_mt.msgs.gprs_sm)) {
603 return c_LLC_SAPI_LLGMM;
604 } else if (ischosen(l3_mt.msgs.sms)) {
605 return c_LLC_SAPI_LLSMS;
606 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100607 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No LLC SAPI for ", l3_mt));
608 return '0000'B;
Harald Welte5339b2e2020-10-04 22:52:56 +0200609}
610#endif
611
612/* main loop of BSSGP_CT */
613private function f_bssgp_ScanEvents() runs on BSSGP_CT {
614 while (true) {
615 alt {
616 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
Harald Weltee5887922020-11-27 19:04:46 +0100617 [] as_sig_allstate_pre();
Harald Welte5339b2e2020-10-04 22:52:56 +0200618 [g_sign_bvc_state == BVC_S_WAIT_NS_ALIVE_UNBLOCKED] as_sig_wait_ns_alive_unblocked();
619 [g_sign_bvc_state == BVC_S_UNBLOCKED] as_sig_unblocked();
Harald Weltee5887922020-11-27 19:04:46 +0100620 [g_sign_bvc_state == BVC_S_WAIT_RESET] as_sig_wait_reset();
621 [] as_sig_allstate_post();
Harald Welte5339b2e2020-10-04 22:52:56 +0200622 }
623 } /* while */
624}
625
626/* generate a send template. Cannot be a real template as we want to use g_cfg.nsei */
Harald Weltec4505522020-11-11 18:55:09 +0100627private function f_BnsUdReq(template PDU_BSSGP pdu, BssgpBvci bvci, integer lsp)
Harald Welte5339b2e2020-10-04 22:52:56 +0200628runs on BSSGP_CT return NsUnitdataRequest {
629 var NsUnitdataRequest udr := {
630 bvci := bvci,
631 nsei := g_cfg.nsei,
Harald Weltec4505522020-11-11 18:55:09 +0100632 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +0200633 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
634 * unbound integer value." when trying to send the reocrd rather than the octetstring */
635 //sdu := omit,
636 //bssgp := valueof(pdu)
637 sdu := enc_PDU_BSSGP(valueof(pdu)),
638 bssgp := omit
639 }
640 return udr;
641}
642
643/* generate a receive template. Cannot be a real template as we want to use g_cfg.nsei */
644private function f_BnsUdInd(template PDU_BSSGP pdu, template BssgpBvci bvci)
645runs on BSSGP_CT return template (present) NsUnitdataIndication {
646 var template (present) NsUnitdataIndication udi := {
647 bvci := bvci,
648 nsei := g_cfg.nsei,
Harald Welte80a249a2020-11-17 19:57:40 +0100649 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +0200650 sdu := *,
651 bssgp := pdu
652 }
653 return udi;
654}
655
656/* change state of signaling BVCI; notify per-BVC components */
657private function f_sign_change_state(BvcState new_state) runs on BSSGP_CT {
658 if (new_state == g_sign_bvc_state) {
659 return;
660 }
661 log("BVCI(0) State Transition: ", g_sign_bvc_state, " -> ", new_state);
662 g_sign_bvc_state := new_state;
663 for (var integer i := 0; i < sizeof(BvcTable); i := i+1) {
664 if (isbound(BvcTable[i].comp_ref) and BvcTable[i].comp_ref != null) {
665 BVC.send(ts_BssgpStsInd(g_cfg.nsei, 0, g_sign_bvc_state)) to BvcTable[i].comp_ref;
666 }
667 }
668}
669
670/* User wants to get per-BVC component reference from BSSGP_CT */
671signature BSSGP_get_bvci_ct(BssgpBvci bvci) return BSSGP_BVC_CT;
672
673type port BSSGP_CT_PROC_PT procedure {
674 inout BSSGP_get_bvci_ct
675} with { extension "internal" };
676
677/* convenience wrapper function for user */
678function f_bssgp_get_bvci_ct(BssgpBvci bvci, BSSGP_CT_PROC_PT PT) return BSSGP_BVC_CT {
679 var BSSGP_BVC_CT res;
680 PT.call(BSSGP_get_bvci_ct:{bvci}) {
681 [] PT.getreply(BSSGP_get_bvci_ct:{bvci})-> value res {
682 return res;
683 }
684 }
685}
686
687/***********************************************************************
688 * per-BVC (Cell) Component
689 ***********************************************************************
690 * Any number of these components runs on top of the BSSGP_CT, each
691 * representing one PTP BVC within this NSE. Users (test cases) can
692 * register with TLLI and/or IMSI via the procedure port.
693 ***********************************************************************/
694
695/* per-BVC component. Exists once per cell within a BSSGP_CT */
696type component BSSGP_BVC_CT {
697 /* port towards the underlying BSSGP_CT */
698 port BSSGP_BVC_PT BVC;
699
Harald Welte199f3862020-11-15 22:37:45 +0100700 /* port to a management instance */
701 port BSSGP_BVC_MGMT_SP_PT MGMT;
702
Harald Welte71449b02020-12-09 11:58:23 +0100703 /* per-BVC global port for e.g. BVC Flow Control */
704 port BSSGP_SP_PT GLOBAL;
705
706 /* BSSGP-User SAP towards the user (per-TLLI, Client) */
Harald Welte853c0ad2018-02-15 17:45:29 +0100707 port BSSGP_SP_PT BSSGP_SP;
Stefan Sperlingf82bbb62018-05-03 19:14:28 +0200708 port BSSGP_SP_PT BSSGP_SP_SIG;
Harald Welte5ac31492018-02-15 20:39:13 +0100709 port BSSGP_PROC_PT BSSGP_PROC;
Harald Welte853c0ad2018-02-15 17:45:29 +0100710
Harald Welte5339b2e2020-10-04 22:52:56 +0200711 var BssgpBvcConfig g_cfg;
712 var boolean g_sgsn_role;
Harald Weltefba7afd2020-11-24 23:12:31 +0100713 var Nsei g_nsei;
Harald Welte5ac31492018-02-15 20:39:13 +0100714
Harald Weltec4505522020-11-11 18:55:09 +0100715 /* default Link Selector Parameter for this BVC (for traffic unrelated to a TLLI) */
716 var integer g_bvc_lsp;
717
Harald Welte853c0ad2018-02-15 17:45:29 +0100718 var BvcState g_ptp_bvc_state := BVC_S_BLOCKED;
719 timer g_T1 := 15.0;
720 timer g_T2 := 60.0;
Harald Welte5339b2e2020-10-04 22:52:56 +0200721 var boolean g_t1_waits_for_block_ack := false;
Harald Welte199f3862020-11-15 22:37:45 +0100722 /* for re-transmissions */
723 var BssgpCause g_last_block_cause;
724 var BssgpCause g_last_reset_cause;
Harald Welte5ac31492018-02-15 20:39:13 +0100725
Harald Welte633805b2021-02-03 17:47:18 +0100726 var ClientEntity ClientTable[64];
Harald Welte5339b2e2020-10-04 22:52:56 +0200727};
Harald Welte853c0ad2018-02-15 17:45:29 +0100728
Harald Welte5339b2e2020-10-04 22:52:56 +0200729/* port between global BSSGP_CT and per-BVC BSSGP_BVC_CT */
730type port BSSGP_BVC_SP_PT message {
731 in NsUnitdataRequest;
732 out ASP_Event,
733 BssgpStatusIndication,
734 BssgpResetIndication,
735 NsUnitdataIndication;
736} with { extension "internal" };
737type port BSSGP_BVC_PT message {
738 in ASP_Event,
739 BssgpStatusIndication,
740 BssgpResetIndication,
741 NsUnitdataIndication;
742 out NsUnitdataRequest;
743} with { extension "internal" };
Alexander Couzens6b449fb2018-07-31 14:19:36 +0200744
Harald Welte199f3862020-11-15 22:37:45 +0100745/* port between BSSGP_BVC_CT and a management instance */
746type port BSSGP_BVC_MGMT_SP_PT message {
747 in BssgpResetRequest,
748 BssgpBlockRequest,
749 BssgpUnblockRequest;
750 out BssgpStatusIndication,
751 BssgpResetIndication;
752} with { extension "internal" };
753type port BSSGP_BVC_MGMT_PT message {
754 in BssgpStatusIndication,
755 BssgpResetIndication;
756 out BssgpResetRequest,
757 BssgpBlockRequest,
758 BssgpUnblockRequest;
759} with { extension "internal" };
760
761type record BssgpResetRequest {
762 BssgpCause cause
763};
764type record BssgpBlockRequest {
765 BssgpCause cause
766};
767type record BssgpUnblockRequest {
768};
Harald Welte5339b2e2020-10-04 22:52:56 +0200769
770/* one element in the per-TLLI state table */
Harald Welte5ac31492018-02-15 20:39:13 +0100771type record ClientEntity {
772 OCT4 tlli,
773 OCT4 tlli_old optional,
774 hexstring imsi,
Harald Welte5ac31492018-02-15 20:39:13 +0100775 BSSGP_Client_CT comp_ref,
776 /* LLC entities, one for each SAPI */
777 LLC_Entity llc[16]
Harald Welte853c0ad2018-02-15 17:45:29 +0100778};
Harald Welte5ac31492018-02-15 20:39:13 +0100779type record LLC_Entity {
780 boolean sgsn_role,
781 /* N(U) on transmit side for next PDU */
782 uint9_t n_u_tx_next,
783 /* N(U) on receive side, last PDU */
784 uint9_t n_u_rx_last optional
785};
Harald Welte5339b2e2020-10-04 22:52:56 +0200786type record length(16) of LLC_Entity LLC_Entities;
Harald Welte5ac31492018-02-15 20:39:13 +0100787
Harald Weltef5365242021-01-17 13:46:57 +0100788private template PDU_BSSGP tr_GLOBAL_PTP := (
789 {pDU_BSSGP_STATUS:=?}
790);
791
Alexander Couzenscdfb7512018-07-31 15:37:14 +0200792function f_llc_create(boolean sgsn_role := false) return LLC_Entities {
793 var LLC_Entities llc;
794 for (var integer i := 0; i < 16; i := i+1) {
795 llc[i] := valueof(t_LLC_init(sgsn_role));
796 }
797 return llc;
798}
799
Harald Welte5ac31492018-02-15 20:39:13 +0100800private template LLC_Entity t_LLC_init(boolean sgsn_role := false) := {
801 sgsn_role := sgsn_role,
802 n_u_tx_next := 0,
803 n_u_rx_last := -
804}
805
Harald Welte5339b2e2020-10-04 22:52:56 +0200806/* configuration of BSSGP_CT */
807type record BssgpConfig {
808 Nsvci nsei,
809 boolean sgsn_role,
810 BssgpBvcConfigs bvc
811};
812/* Configuration of one BVC (Cell) BSSGP_BVC_CT */
813type record BssgpBvcConfig {
814 BssgpBvci bvci,
815 BssgpCellId cell_id,
Harald Welte4d112c92020-11-12 19:48:31 +0100816 BssgpDecodeDepth depth,
817 BssgpCreateCallback create_cb
Harald Welte5339b2e2020-10-04 22:52:56 +0200818};
819type record of BssgpBvcConfig BssgpBvcConfigs;
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200820type enumerated BssgpDecodeDepth {
821 BSSGP_DECODE_DEPTH_BSSGP,
822 BSSGP_DECODE_DEPTH_LLC,
Harald Welte93331e72020-09-12 20:51:05 +0200823 BSSGP_DECODE_DEPTH_SNDCP
824#ifdef BSSGP_EM_L3
825 ,
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200826 BSSGP_DECODE_DEPTH_L3
Harald Welte93331e72020-09-12 20:51:05 +0200827#endif
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200828};
Harald Welte4d112c92020-11-12 19:48:31 +0100829/* call-back function type: Called for inbound BSSGP for unknown TLLIs; could create BSSGP_ClienT_CT */
830type function BssgpCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT;
831
832/* Default Create Callback function: Fail and terminate */
833function DefaultCreateCallback(BssgpBvci bvci, BssgpCellId cell_id, OCT4 tlli, BssgpDecoded dec) runs on BSSGP_BVC_CT {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100834 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for TLLI ", tlli));
Harald Welte4d112c92020-11-12 19:48:31 +0100835}
Oliver Smithaac9b9c2019-09-02 08:36:36 +0200836
Harald Welte5339b2e2020-10-04 22:52:56 +0200837/*
838private function f_tbl_init() runs on BSSGP_BVC_CT {
839 var integer i;
840 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
841 ImsiTable[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 for (i := 0; i < sizeof(TlliTable); i := i+1) {
845 TlliTable[i] := -;
Harald Welte853c0ad2018-02-15 17:45:29 +0100846 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100847}
Harald Welte5339b2e2020-10-04 22:52:56 +0200848*/
Harald Welte853c0ad2018-02-15 17:45:29 +0100849
Harald Welte5339b2e2020-10-04 22:52:56 +0200850private function f_tbl_client_add(hexstring imsi, OCT4 tlli, BSSGP_Client_CT vc_conn)
851runs on BSSGP_BVC_CT {
852 var integer i;
853 for (i := 0; i < sizeof(ClientTable); i := i+1) {
854 if (not isvalue(ClientTable[i].comp_ref)) {
855 log("Adding Client=", vc_conn, ", IMSI=", imsi, ", TLLI=", tlli, ", index=", i);
856 ClientTable[i] := {
857 tlli := tlli,
858 tlli_old := omit,
859 imsi := imsi,
860 comp_ref := vc_conn,
861 llc := -
862 };
863 for (var integer j := 0; j < sizeof(ClientTable[i].llc); j := j+1) {
864 ClientTable[i].llc[j] := valueof(t_LLC_init(g_sgsn_role));
865 }
866 return;
Harald Welte5ac31492018-02-15 20:39:13 +0100867 }
868 }
Harald Welte5339b2e2020-10-04 22:52:56 +0200869 testcase.stop("Client Table full");
Harald Welte853c0ad2018-02-15 17:45:29 +0100870}
871
Harald Welte5339b2e2020-10-04 22:52:56 +0200872private function f_tbl_client_del(hexstring imsi, BSSGP_Client_CT vc_conn) runs on BSSGP_BVC_CT {
873 var integer i;
874 for (i := 0; i < sizeof(ClientTable); i := i+1) {
875 if (isvalue(ClientTable[i].imsi) and ClientTable[i].imsi == imsi) {
876 if (ClientTable[i].comp_ref != vc_conn) {
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100877 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
878 log2str("Cannot unregister index=", i, " IMSI ",
879 imsi, " registred to ", ClientTable[i].comp_ref,
880 " from ", vc_conn));
Harald Welte5339b2e2020-10-04 22:52:56 +0200881 }
882 log("Removing Client IMSI=", imsi, ", index=", i);
883 ClientTable[i] := {
884 tlli := -,
885 tlli_old := omit,
886 imsi := ''H,
887 comp_ref := null,
888 llc := - };
889 return;
890 }
891 }
892 log("Warning: Could not find client for IMSI ", imsi);
893 return;
894}
895
896/* TS 44.064 7.2.1.1 LLGMM-ASSIGN */
897private function f_tbl_client_llgmm_assign(OCT4 tlli_old, OCT4 tlli_new, BSSGP_Client_CT vc_conn)
898runs on BSSGP_BVC_CT {
899 var integer i := f_tbl_idx_by_comp(vc_conn);
900
901 if (tlli_old == 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
902 /* TLLI assignment */
903 ClientTable[i].tlli := tlli_new;
904 ClientTable[i].tlli_old := omit;
905 } else if (tlli_old != 'FFFFFFFF'O and tlli_new != 'FFFFFFFF'O) {
906 /* TLLI change: both active */
907 ClientTable[i].tlli := tlli_new;
908 ClientTable[i].tlli_old := tlli_old;
909 } else if (tlli_old != 'FFFFFFFF'O and tlli_new == 'FFFFFFFF'O) {
910 /* TLLI unassignment: old shall be unassigned; new stays */
911 ClientTable[i].tlli_old := omit;
912 }
913}
914
915private function f_tbl_comp_by_imsi(hexstring imsi) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
916 var integer i;
917 for (i := 0; i < sizeof(ClientTable); i := i+1) {
918 if (isvalue(ClientTable[i].imsi) and isvalue(ClientTable[i].comp_ref)
919 and ClientTable[i].imsi == imsi) {
920 return ClientTable[i].comp_ref;
921 }
922 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100923 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Component for IMSI ", imsi));
924 return ClientTable[0].comp_ref;
Harald Welte5339b2e2020-10-04 22:52:56 +0200925}
926
927private function f_tbl_comp_by_tlli(OCT4 tlli) runs on BSSGP_BVC_CT return BSSGP_Client_CT {
928 var integer i;
929 for (i := 0; i < sizeof(ClientTable); i := i+1) {
930 if (isvalue(ClientTable[i].comp_ref) and
931 (isvalue(ClientTable[i].tlli) and (ClientTable[i].tlli == tlli or
932 isvalue(ClientTable[i].tlli_old) and ClientTable[i].tlli_old == tlli) )) {
933 return ClientTable[i].comp_ref;
934 }
935 }
Harald Welte4d112c92020-11-12 19:48:31 +0100936 return null;
Harald Welte5339b2e2020-10-04 22:52:56 +0200937}
938
939private function f_tbl_idx_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return integer {
940 var integer i;
941 for (i := 0; i < sizeof(ClientTable); i := i+1) {
942 if (isvalue(ClientTable[i].comp_ref) and ClientTable[i].comp_ref == comp_ref) {
943 return i;
944 }
945 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100946 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find Client for Component ", comp_ref));
947 return 1;
Harald Welte5339b2e2020-10-04 22:52:56 +0200948}
949
950private function f_tbl_tlli_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_BVC_CT return OCT4 {
951 var integer i;
952 for (i := 0; i < sizeof(ClientTable); i := i+1) {
953 if (isvalue(ClientTable[i].tlli) and isvalue(ClientTable[i].comp_ref)
954 and ClientTable[i].comp_ref == comp_ref) {
955 return ClientTable[i].tlli;
956 }
957 }
Pau Espin Pedrol011e32f2021-02-05 12:07:04 +0100958 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Couldn't find TLLI for Component ", comp_ref));
959 return '00000000'O;
Harald Welte5339b2e2020-10-04 22:52:56 +0200960}
961
962/* PDU_BSSGP enhanced with LLC and possibly L3 decoded payloads */
963type record BssgpDecoded {
964 PDU_BSSGP bssgp,
965 PDU_LLC llc optional,
966#ifdef BSSGP_EM_L3
967 PDU_L3_MS_SGSN l3_mo optional,
968 PDU_L3_SGSN_MS l3_mt optional,
969#endif
970 PDU_SN sndcp optional
971}
972
973/* Decode a PDU_BSSGP into a BssgpDecoded (i.e. with LLC/L3 decoded, as applicable) */
974private function f_dec_bssgp(PDU_BSSGP bssgp) runs on BSSGP_BVC_CT return BssgpDecoded {
975 var BssgpDecoded dec := {
976 bssgp := bssgp,
977 llc := omit,
978#ifdef BSSGP_EM_L3
979 l3_mo := omit,
980 l3_mt := omit,
981#endif
982 sndcp := omit
983 };
984
985 /* Decode LLC, if it is a PDU that contains LLC */
986 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_LLC) {
987 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
988 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_DL_UNITDATA.lLC_PDU.lLC_PDU);
989 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
990 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_UL_UNITDATA.lLC_PDU.lLC_PDU);
991 }
992 }
993
994 /* Decode SNDCP, if it is a LLC PDU containing user plane data */
995 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_SNDCP) {
996 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_USER)) {
997 dec.sndcp := dec_PDU_SN(dec.llc.pDU_LLC_UI.information_field_UI);
998 }
999 }
1000
1001#ifdef BSSGP_EM_L3
1002 /* Decode L3, if it is a LLC PDU containing L3 */
1003 if (g_cfg.depth >= BSSGP_DECODE_DEPTH_L3) {
1004 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_L3)) {
1005 if (g_sgsn_role) {
1006 dec.l3_mo := dec_PDU_L3_MS_SGSN(dec.llc.pDU_LLC_UI.information_field_UI);
1007 } else {
1008 dec.l3_mt := dec_PDU_L3_SGSN_MS(dec.llc.pDU_LLC_UI.information_field_UI);
1009 }
1010 }
1011 }
1012#endif
1013
1014 return dec;
1015}
1016
1017private function f_ptp_sendUnblock() runs on BSSGP_BVC_CT {
Harald Weltec4505522020-11-11 18:55:09 +01001018 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001019 g_t1_waits_for_block_ack := false;
1020 g_T1.start;
1021}
1022
1023private function f_ptp_sendBlock(BssgpCause cause) runs on BSSGP_BVC_CT {
Harald Weltec4505522020-11-11 18:55:09 +01001024 BVC.send(ts_ptp_BnsUdReq(t_BVC_BLOCK(g_cfg.bvci, cause), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001025 g_t1_waits_for_block_ack := true;
1026 g_T1.start;
Harald Welte199f3862020-11-15 22:37:45 +01001027 g_last_block_cause := cause;
Harald Welte5339b2e2020-10-04 22:52:56 +02001028}
1029
1030private function f_ptp_sendStatus(BssgpCause cause, PDU_BSSGP pdu) runs on BSSGP_BVC_CT {
1031 /* FIXME: Make sure correct Signaling or PTP BVCI is used! */
Harald Weltec4505522020-11-11 18:55:09 +01001032 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 +02001033}
1034
Harald Welte199f3862020-11-15 22:37:45 +01001035private function f_ptp_sendReset(BssgpCause cause := BSSGP_CAUSE_OM_INTERVENTION) runs on BSSGP_BVC_CT {
Harald Weltec967d0e2020-09-14 16:07:26 +02001036 var PDU_BSSGP pdu;
1037
1038 /* The Cell Identifier IE is mandatory in the BVC-RESET PDU sent from BSS to
1039 * SGSN in order to reset a BVC corresponding to a PTP functional entity. The
1040 * Cell Identifier IE shall not be used in any other BVC-RESET PDU. */
Harald Welte5339b2e2020-10-04 22:52:56 +02001041 if (g_sgsn_role) {
Harald Welte199f3862020-11-15 22:37:45 +01001042 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, omit));
Harald Weltec967d0e2020-09-14 16:07:26 +02001043 } else {
Harald Welte199f3862020-11-15 22:37:45 +01001044 pdu := valueof(ts_BVC_RESET(cause, g_cfg.bvci, g_cfg.cell_id));
Harald Weltec967d0e2020-09-14 16:07:26 +02001045 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001046
1047 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
Harald Weltec4505522020-11-11 18:55:09 +01001048 BVC.send(ts_ptp_BnsUdReq(pdu, 0, g_bvc_lsp));
Harald Welte853c0ad2018-02-15 17:45:29 +01001049 g_T2.start;
1050 //f_change_state(BVC_S_WAIT_RESET);
Harald Welte199f3862020-11-15 22:37:45 +01001051 g_last_reset_cause := cause;
Harald Welte853c0ad2018-02-15 17:45:29 +01001052}
1053
Harald Welte5339b2e2020-10-04 22:52:56 +02001054/* PTP-BVC is in BVC_S_BLOCKED state */
1055private altstep as_ptp_blocked() runs on BSSGP_BVC_CT {
1056 var NsUnitdataIndication udi;
1057
1058 [g_T1.running and not g_t1_waits_for_block_ack] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0)) {
1059 g_T1.stop;
1060 f_ptp_change_state(BVC_S_UNBLOCKED);
1061 }
1062
Harald Welteddfc6672020-11-24 23:13:01 +01001063 /* Inbound BVC-UNBLOCK from peer */
1064 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK(g_cfg.bvci), 0)) {
1065 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
1066 f_ptp_change_state(BVC_S_UNBLOCKED);
1067 }
1068
Harald Welte5339b2e2020-10-04 22:52:56 +02001069 /* RESET-ACK before T2 timeout: reset successful. In SGSN role, CellID must be present */
1070 [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 {
1071 g_T2.stop;
1072 f_ptp_change_state(BVC_S_UNBLOCKED);
1073 }
1074 /* RESET-ACK before T2 timeout: reset successful. In BSS role, CellID must be absent */
1075 [g_T2.running and not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, omit), 0)) -> value udi {
1076 g_T2.stop;
1077 f_ptp_change_state(BVC_S_UNBLOCKED);
1078 }
1079
Harald Welteab50cc22020-11-25 17:11:08 +01001080 [] as_ptp_handle_inbound_reset();
1081
Harald Welte5339b2e2020-10-04 22:52:56 +02001082 [] g_T2.timeout {
1083 /* BVC-RESET-ACK PDU was not received within T2: retransmit */
Harald Welte199f3862020-11-15 22:37:45 +01001084 f_ptp_sendReset(g_last_reset_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001085 }
Harald Welte199f3862020-11-15 22:37:45 +01001086
1087 [] MGMT.receive(BssgpUnblockRequest:?) {
1088 f_ptp_sendUnblock();
1089 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001090}
1091
Harald Welte5339b2e2020-10-04 22:52:56 +02001092/* PTP-BVC is in UNBLOCKED state */
1093private altstep as_ptp_unblocked() runs on BSSGP_BVC_CT {
1094 var NsUnitdataIndication udi;
1095 var NsStatusIndication nsi;
Harald Welte199f3862020-11-15 22:37:45 +01001096 var BssgpBlockRequest bbr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001097 var BSSGP_Client_CT vc_conn;
1098 var ASP_Event evt;
1099 var PDU_BSSGP bs_pdu;
1100 var PDU_LLC llc;
1101#ifdef BSSGP_EM_L3
1102 var PDU_L3_MS_SGSN l3_mo;
1103 var PDU_L3_SGSN_MS l3_mt;
1104#endif
1105
1106 /* bogus unblock, just respond with ACK */
1107 [] BVC.receive(tr_ptp_BnsUdInd(t_BVC_UNBLOCK(g_cfg.bvci), 0)) -> value udi {
Harald Weltec4505522020-11-11 18:55:09 +01001108 BVC.send(ts_ptp_BnsUdReq(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001109 }
1110 /* Respond to BLOCK with BLOCK-ACK + change state */
1111 [] BVC.receive(tr_ptp_BnsUdInd(t_BVC_BLOCK(g_cfg.bvci, ?), 0)) -> value udi {
Harald Weltec4505522020-11-11 18:55:09 +01001112 BVC.send(ts_ptp_BnsUdReq(t_BVC_BLOCK_ACK(g_cfg.bvci), 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001113 g_T1.stop;
1114 f_ptp_change_state(BVC_S_BLOCKED);
1115 }
1116 /* BLOCK-ACK before T1 timeout: mark as blocked */
1117 [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 {
1118 g_T1.stop;
1119 f_ptp_change_state(BVC_S_BLOCKED);
1120 }
1121 /* Re-transmit BLOCK / UNBLOCK on T1 timeout */
1122 [not g_t1_waits_for_block_ack] g_T1.timeout {
1123 f_ptp_sendUnblock();
1124 }
1125 [g_t1_waits_for_block_ack] g_T1.timeout {
Harald Welte199f3862020-11-15 22:37:45 +01001126 f_ptp_sendBlock(g_last_block_cause);
Harald Welte5339b2e2020-10-04 22:52:56 +02001127 }
1128
Harald Welteab50cc22020-11-25 17:11:08 +01001129 [] as_ptp_handle_inbound_reset();
1130
Harald Welte5339b2e2020-10-04 22:52:56 +02001131 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_FC_BVC, g_cfg.bvci)) -> value udi {
Harald Welte71449b02020-12-09 11:58:23 +01001132 if (GLOBAL.checkstate("Connected")) {
1133 GLOBAL.send(udi.bssgp);
1134 } else {
1135 /* simply acknowledge all per-BVC Flow Control Messages */
1136 var OCT1 tag := udi.bssgp.pDU_BSSGP_FLOW_CONTROL_BVC.tag.unstructured_Value;
1137 BVC.send(ts_ptp_BnsUdReq(t_BVC_FC_BVC_ACK(tag), g_cfg.bvci, g_bvc_lsp));
1138 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001139 }
Harald Welte71449b02020-12-09 11:58:23 +01001140
1141 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(t_BVC_FC_BVC_ACK(?), g_cfg.bvci)) -> value udi {
1142 if (GLOBAL.checkstate("Connected")) {
1143 GLOBAL.send(udi.bssgp);
1144 } else {
1145 /* ignore any incoming flow control ACK */
1146 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001147 }
Harald Welte71449b02020-12-09 11:58:23 +01001148
Harald Weltef5365242021-01-17 13:46:57 +01001149 [] BVC.receive(tr_ptp_BnsUdInd(tr_GLOBAL_PTP, g_cfg.bvci)) -> value udi {
1150 if (GLOBAL.checkstate("Connected")) {
1151 GLOBAL.send(udi.bssgp);
1152 } else {
1153 setverdict(fail, "Received BSSGP STATUS ", udi.bssgp);
1154 }
1155 }
1156
Harald Welte5339b2e2020-10-04 22:52:56 +02001157 /* Any other PTP BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1158 [] BVC.receive(tr_ptp_BnsUdInd(?, g_cfg.bvci)) -> value udi {
1159 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1160 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1161 if (isvalue(tlli)) {
1162 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001163 if (vc_conn == null) {
1164 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1165 } else {
1166 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP);
1167 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001168 } else {
1169 log("No TLLI: Broadcasting ", dec);
1170 /* broadcast this message to all components */
1171 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1172 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1173 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1174 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP);
1175 }
1176 }
1177 }
1178 }
1179
1180 /* Any other SIG BSSGP message: If it has TLLI, route to component; otherwise broadcast */
1181 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1182 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
1183 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
1184 if (isvalue(tlli)) {
1185 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
Harald Welte4d112c92020-11-12 19:48:31 +01001186 if (vc_conn == null) {
1187 g_cfg.create_cb.apply(g_cfg.bvci, g_cfg.cell_id, valueof(tlli), dec);
1188 } else {
1189 f_send_bssgp_dec(dec, vc_conn, BSSGP_SP_SIG);
1190 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001191 } else {
1192 log("No TLLI: Broadcasting ", dec);
1193 /* broadcast this message to all components */
1194 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
1195 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1196 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
1197 f_send_bssgp_dec(dec, ClientTable[i].comp_ref, BSSGP_SP_SIG);
1198 }
1199 }
1200 }
1201 }
1202
1203 /* ConnHdlr sends BSSGP on BVCI=0 port: forward it to BSSGP_CT */
1204 [] BSSGP_SP_SIG.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001205 BVC.send(ts_ptp_BnsUdReq(bs_pdu, 0, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001206 }
1207
1208 /* ConnHdlr sends BSSGP on BVCI=PTP port: forward it to BSSGP_CT */
1209 [] BSSGP_SP.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
Harald Weltec4505522020-11-11 18:55:09 +01001210 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
Harald Welte5339b2e2020-10-04 22:52:56 +02001211 }
1212
1213#ifdef BSSGP_EM_L3
1214 /* ConnHdlr sends L3: Encode and send as UL_UD / DL_UD */
1215 [g_sgsn_role] BSSGP_SP.receive(PDU_L3_SGSN_MS:?) -> value l3_mt sender vc_conn {
1216 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001217 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001218 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(l3_mt);
1219 var BIT4 sapi := f_llc_sapi_by_l3_mt(l3_mt);
1220 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1221 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 +01001222 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001223 }
1224 [not g_sgsn_role] BSSGP_SP.receive(PDU_L3_MS_SGSN:?) -> value l3_mo sender vc_conn {
1225 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001226 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001227 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(l3_mo);
1228 var BIT4 sapi := f_llc_sapi_by_l3_mo(l3_mo);
1229 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
1230 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 +01001231 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 +02001232 }
1233#endif
1234
1235 /* ConnHdlr sends raw LLC: Encode and send as UL_UD / DL_UD */
1236 [not g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1237 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001238 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001239 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001240 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 +02001241 }
1242 [g_sgsn_role] BSSGP_SP.receive(PDU_LLC:?) -> value llc sender vc_conn {
1243 var integer idx := f_tbl_idx_by_comp(vc_conn);
Harald Weltec4505522020-11-11 18:55:09 +01001244 var OCT4 tlli := ClientTable[idx].tlli;
Harald Welte5339b2e2020-10-04 22:52:56 +02001245 var octetstring llc_enc := enc_PDU_LLC(llc);
Harald Weltec4505522020-11-11 18:55:09 +01001246 BVC.send(ts_ptp_BnsUdReq(ts_BSSGP_DL_UD(tlli, llc_enc), g_cfg.bvci, oct2int(tlli)));
Harald Welte5339b2e2020-10-04 22:52:56 +02001247 }
Harald Welte199f3862020-11-15 22:37:45 +01001248
Harald Welte71449b02020-12-09 11:58:23 +01001249 /* Testcase sends us BSSGP on global port */
1250 [] GLOBAL.receive(PDU_BSSGP:?)-> value bs_pdu sender vc_conn {
1251 BVC.send(ts_ptp_BnsUdReq(bs_pdu, g_cfg.bvci, g_bvc_lsp));
1252 }
1253
Harald Welte199f3862020-11-15 22:37:45 +01001254 [] MGMT.receive(BssgpBlockRequest:?) -> value bbr {
1255 f_ptp_sendBlock(bbr.cause);
1256 }
Harald Welte853c0ad2018-02-15 17:45:29 +01001257}
1258
Harald Welteab50cc22020-11-25 17:11:08 +01001259private altstep as_ptp_handle_inbound_reset() runs on BSSGP_BVC_CT {
1260 var NsUnitdataIndication udi;
1261 /* we are a SGSN: BSS/PCU-originated RESET must have a cell ID */
1262 [g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
1263 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001264 if (MGMT.checkstate("Connected")) {
1265 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1266 }
Harald Welteab50cc22020-11-25 17:11:08 +01001267 /* Respond to RESET with correct BVCI but without CellID */
1268 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, omit), 0, g_bvc_lsp));
1269 /* FIXME: reset all state? What about clients? */
1270 f_ptp_change_state(BVC_S_UNBLOCKED);
1271 }
1272 /* we are a BSS/PCU: SGSN-originated RESET must not have a cell ID */
1273 [not g_sgsn_role] BVC.receive(tr_ptp_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, omit), 0)) -> value udi {
1274 log("Rx BVC-RESET from BVCI=", g_cfg.bvci);
Harald Welte3c905152020-11-26 20:56:09 +01001275 if (MGMT.checkstate("Connected")) {
1276 MGMT.send(BssgpResetIndication:{g_cfg.bvci});
1277 }
Harald Welteab50cc22020-11-25 17:11:08 +01001278 /* Respond to RESET with correct BVCI with CellID */
1279 BVC.send(ts_ptp_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0, g_bvc_lsp));
1280 /* FIXME: reset all state? What about clients? */
1281 f_ptp_change_state(BVC_S_UNBLOCKED);
1282 }
1283}
1284
Harald Welte5339b2e2020-10-04 22:52:56 +02001285/* Events permitted in all states */
1286private altstep as_ptp_allstate() runs on BSSGP_BVC_CT {
1287 var NsUnitdataIndication udi;
Harald Welte199f3862020-11-15 22:37:45 +01001288 var BssgpResetRequest brr;
Harald Welte5339b2e2020-10-04 22:52:56 +02001289 var BSSGP_Client_CT vc_conn;
1290 var OCT4 tlli, tlli_old;
1291 var hexstring imsi;
1292
1293 /* Signaling BVC was reset */
1294 [] BVC.receive(BssgpResetIndication:{0}) {
Harald Welte199f3862020-11-15 22:37:45 +01001295 if (MGMT.checkstate("Connected")) {
1296 MGMT.send(BssgpResetIndication:{0});
1297 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001298 if (not g_sgsn_role) {
1299 f_ptp_change_state(BVC_S_BLOCKED);
1300 /* when the BSS side signaling PTP is RESET, all PTP BVC must start the
1301 * reset procedure */
1302 f_ptp_sendReset();
1303 } else {
1304 f_ptp_change_state(BVC_S_UNBLOCKED);
1305 }
1306 }
1307
Harald Welte5339b2e2020-10-04 22:52:56 +02001308 /* Ignore those for now */
1309 [] BVC.receive(ASP_Event:?) { }
1310 [] BVC.receive(BssgpStatusIndication:?) { }
1311
1312 /* catch-all */
1313 [] BVC.receive(tr_ptp_BnsUdInd(?, 0)) -> value udi {
1314 setverdict(fail, "Received unexpected NS (SIG): ", udi);
1315 }
1316 [] BVC.receive(tr_ptp_BnsUdInd(?, ?)) -> value udi {
1317 setverdict(fail, "Received unexpected NS (PTP): ", udi);
1318 }
1319 [] BVC.receive {
1320 setverdict(fail, "Received unexpected message from BSSGP_CT");
1321 }
1322
1323 /* registration/deregistration of Clients (per-TLLI components) */
1324 [] BSSGP_PROC.getcall(BSSGP_register_client:{?,?}) -> param(imsi, tlli) sender vc_conn {
1325 f_tbl_client_add(imsi, tlli, vc_conn);
1326 BSSGP_PROC.reply(BSSGP_register_client:{imsi, tlli}) to vc_conn;
1327 }
1328 [] BSSGP_PROC.getcall(BSSGP_unregister_client:{?}) -> param(imsi) sender vc_conn {
1329 f_tbl_client_del(imsi, vc_conn);
1330 BSSGP_PROC.reply(BSSGP_unregister_client:{imsi}) to vc_conn;
1331 }
1332 [] BSSGP_PROC.getcall(BSSGP_llgmm_assign:{?,?}) -> param(tlli_old, tlli) sender vc_conn {
1333 f_tbl_client_llgmm_assign(tlli_old, tlli, vc_conn);
1334 BSSGP_PROC.reply(BSSGP_llgmm_assign:{tlli_old, tlli}) to vc_conn;
1335 }
Harald Welte199f3862020-11-15 22:37:45 +01001336
1337 [] MGMT.receive(BssgpResetRequest:?) -> value brr {
Harald Welte52cecbb2020-11-25 21:57:31 +01001338 f_ptp_change_state(BVC_S_BLOCKED);
Harald Welte199f3862020-11-15 22:37:45 +01001339 f_ptp_sendReset(brr.cause);
1340 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001341}
1342
1343/* main loop for per-BVC component */
1344private function f_bssgp_bvc_ScanEvents() runs on BSSGP_BVC_CT {
1345 while (true) {
1346 alt {
1347 [g_ptp_bvc_state == BVC_S_BLOCKED] as_ptp_blocked();
1348 [g_ptp_bvc_state == BVC_S_UNBLOCKED] as_ptp_unblocked();
1349 [] as_ptp_allstate();
1350 }
1351 }
1352}
1353
1354/* main function for per-BVC Component */
Harald Weltefba7afd2020-11-24 23:12:31 +01001355private 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 +02001356 g_cfg := cfg;
Harald Weltefba7afd2020-11-24 23:12:31 +01001357 g_nsei := nsei;
Harald Weltec4505522020-11-11 18:55:09 +01001358 g_bvc_lsp := cfg.bvci;
Harald Welte5339b2e2020-10-04 22:52:56 +02001359 g_sgsn_role := sgsn_role;
1360 f_bssgp_bvc_ScanEvents();
1361}
1362
Harald Weltec4505522020-11-11 18:55:09 +01001363template (value) NsUnitdataRequest ts_ptp_BnsUdReq(template (value) PDU_BSSGP pdu, template (value) BssgpBvci bvci,
1364 template (value) integer lsp) := {
Harald Welte5339b2e2020-10-04 22:52:56 +02001365 bvci := bvci,
1366 nsei := 0, // overwritten in BSSGP_CT
Harald Weltec4505522020-11-11 18:55:09 +01001367 lsp := lsp,
Harald Welte5339b2e2020-10-04 22:52:56 +02001368 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
1369 * unbound integer value." when trying to send the reocrd rather than the octetstring */
1370 //sdu := omit,
1371 //bssgp := valueof(pdu)
1372 sdu := enc_PDU_BSSGP(valueof(pdu)),
1373 bssgp := omit
1374}
1375
1376template (present) NsUnitdataIndication tr_ptp_BnsUdInd(template (present) PDU_BSSGP pdu, template (present) BssgpBvci bvci) := {
1377 bvci := bvci,
1378 nsei := ?,
Harald Welte80a249a2020-11-17 19:57:40 +01001379 nsvci := ?,
Harald Welte5339b2e2020-10-04 22:52:56 +02001380 sdu := *,
1381 bssgp := pdu
1382}
1383
1384/* change state of PTP BVC; broadcast state change to clients */
1385private function f_ptp_change_state(BvcState new_state) runs on BSSGP_BVC_CT {
1386 if (new_state == g_ptp_bvc_state) {
1387 return;
1388 }
1389 log("BVCI(", g_cfg.bvci, ") State Transition: ", g_ptp_bvc_state, " -> ", new_state);
1390 g_ptp_bvc_state := new_state;
Harald Welte199f3862020-11-15 22:37:45 +01001391 if (MGMT.checkstate("Connected")) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001392 MGMT.send(ts_BssgpStsInd(g_nsei, g_cfg.bvci, g_ptp_bvc_state));
Harald Welte199f3862020-11-15 22:37:45 +01001393 }
Harald Welte5339b2e2020-10-04 22:52:56 +02001394 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
1395 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref != null) {
Harald Weltefba7afd2020-11-24 23:12:31 +01001396 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 +02001397 }
1398 }
Harald Welte5ac31492018-02-15 20:39:13 +01001399}
1400
1401/* attempt to extract the TLLI from a BSSGP PDU */
1402function f_bssgp_get_tlli(PDU_BSSGP bssgp) return template OCT4 {
1403 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
1404 return bssgp.pDU_BSSGP_DL_UNITDATA.tLLI_current;
1405 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
1406 return bssgp.pDU_BSSGP_UL_UNITDATA.tLLI;
1407 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY)) {
1408 return bssgp.pDU_BSSGP_RA_CAPABILITY.tLLI.tLLI_Value;
1409 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE)) {
1410 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE.tLLI.tLLI_Value;
1411 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK)) {
1412 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK.tLLI.tLLI_Value;
Harald Welte650bd722021-01-17 10:58:56 +01001413 } else if (ischosen(bssgp.pDU_BSSGP_RADIO_STATUS) and
1414 ispresent(bssgp.pDU_BSSGP_RADIO_STATUS.tLLI)) {
Harald Welte5ac31492018-02-15 20:39:13 +01001415 return bssgp.pDU_BSSGP_RADIO_STATUS.tLLI.tLLI_Value;
1416 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND)) {
1417 return bssgp.pDU_BSSGP_SUSPEND.tLLI.tLLI_Value;
1418 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_ACK)) {
1419 return bssgp.pDU_BSSGP_SUSPEND_ACK.tLLI.tLLI_Value;
1420 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_NACK)) {
1421 return bssgp.pDU_BSSGP_SUSPEND_NACK.tLLI.tLLI_Value;
1422 } else if (ischosen(bssgp.pDU_BSSGP_RESUME)) {
1423 return bssgp.pDU_BSSGP_RESUME.tLLI.tLLI_Value;
1424 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_ACK)) {
1425 return bssgp.pDU_BSSGP_RESUME_ACK.tLLI.tLLI_Value;
1426 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_NACK)) {
1427 return bssgp.pDU_BSSGP_RESUME_NACK.tLLI.tLLI_Value;
1428 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL)) {
1429 return bssgp.pDU_BSSGP_FLUSH_LL.tLLI.tLLI_Value;
1430 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL_ACK)) {
1431 return bssgp.pDU_BSSGP_FLUSH_LL_ACK.tLLI.tLLI_Value;
1432 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1433 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1434 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
1435 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
1436 } else if (ischosen(bssgp.pDU_BSSGP_PAGING_CS) and
1437 isvalue(bssgp.pDU_BSSGP_PAGING_CS.tLLI)) {
1438 return bssgp.pDU_BSSGP_PAGING_CS.tLLI.tLLI_Value;
1439 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS)) {
1440 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS.tLLI.tLLI_Value;
1441 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK)) {
1442 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK.tLLI.tLLI_Value;
1443 }
1444 /* TODO: Handover, PFC, LCS */
1445 return omit;
1446}
1447
Harald Weltef70997d2018-02-17 10:11:19 +01001448
Harald Welte5ac31492018-02-15 20:39:13 +01001449
Harald Welte1733a382017-07-23 17:26:17 +02001450}