blob: f981adbd5ea4f66afb4a666b3e96cfa97ad4fc5d [file] [log] [blame]
Harald Welte1733a382017-07-23 17:26:17 +02001module BSSGP_Emulation {
Harald Welte1733a382017-07-23 17:26:17 +02002
Harald Welte5ac31492018-02-15 20:39:13 +01003import from General_Types all;
4import from Osmocom_Types all;
Harald Welte853c0ad2018-02-15 17:45:29 +01005import from NS_Types all;
6import from NS_Emulation all;
7import from BSSGP_Types all;
8import from Osmocom_Gb_Types all;
9import from IPL4asp_Types all;
Harald Welte1733a382017-07-23 17:26:17 +020010
Harald Welte5ac31492018-02-15 20:39:13 +010011import from MobileL3_GMM_SM_Types all;
12import from MobileL3_Types all;
13
14import from LLC_Types all;
15import from LLC_Templates all;
16
17/***********************************************************************
18 * Communication between Client Components and Main Component
19 ***********************************************************************/
20
Harald Welte853c0ad2018-02-15 17:45:29 +010021type record BssgpStatusIndication {
22 Nsei nsei,
23 BssgpBvci bvci,
24 BvcState state
25}
26
27template BssgpStatusIndication t_BssgpStsInd(template Nsei nsei, template BssgpBvci bvci, template BvcState state) := {
28 nsei := nsei,
29 bvci := bvci,
30 state := state
31}
32
33type enumerated BvcState {
34 BVC_S_BLOCKED,
35 BVC_S_UNBLOCKED
36};
37
38/* port from our (internal) point of view */
39type port BSSGP_SP_PT message {
Harald Welte5ac31492018-02-15 20:39:13 +010040 in PDU_BSSGP,
41 PDU_L3_MS_SGSN,
42 PDU_L3_SGSN_MS;
43 out BssgpDecoded,
Harald Welte853c0ad2018-02-15 17:45:29 +010044 NsStatusIndication,
45 BssgpStatusIndication,
Harald Welte5ac31492018-02-15 20:39:13 +010046 ASP_Event,
47 PDU_L3_MS_SGSN,
48 PDU_L3_SGSN_MS;
Harald Welte853c0ad2018-02-15 17:45:29 +010049} with { extension "internal" };
50
51/* port from the user point of view */
52type port BSSGP_PT message {
53 in ASP_Event,
54 NsStatusIndication,
55 BssgpStatusIndication,
Harald Welte5ac31492018-02-15 20:39:13 +010056 BssgpDecoded,
57 PDU_L3_MS_SGSN,
58 PDU_L3_SGSN_MS;
59 out PDU_BSSGP,
60 PDU_L3_SGSN_MS,
61 PDU_L3_MS_SGSN;
Harald Welte853c0ad2018-02-15 17:45:29 +010062} with { extension "internal" };
63
Harald Welte5ac31492018-02-15 20:39:13 +010064signature BSSGP_register_client(hexstring imsi, OCT4 tlli, BssgpCellId cell_id);
65signature BSSGP_unregister_client(hexstring imsi);
66
67type port BSSGP_PROC_PT procedure {
68 inout BSSGP_register_client, BSSGP_unregister_client;
69} with { extension "internal" };
70
71
72/***********************************************************************
73 * Client Component for a single MS/TLLI
74 ***********************************************************************/
75
76type component BSSGP_Client_CT {
77 port BSSGP_PT BSSGP;
78 port BSSGP_PROC_PT BSSGP_PROC;
79};
80
81/***********************************************************************
82 * Main Component
83 ***********************************************************************/
84
85function BssgpStart(BssgpConfig cfg) runs on BSSGP_CT {
86 g_cfg := cfg;
Harald Welte853c0ad2018-02-15 17:45:29 +010087 f_init();
88 f_ScanEvents();
89}
90
91private function f_init() runs on BSSGP_CT {
92 /* Connect the UDP socket */
93 f_change_state(BVC_S_BLOCKED);
94}
95
96type component BSSGP_CT {
97 /* UDP port towards the bottom (IUT) */
98 port NS_PT BSCP;
99 /* NS-User SAP towards the user */
100 port BSSGP_SP_PT BSSGP_SP;
Harald Welte5ac31492018-02-15 20:39:13 +0100101 port BSSGP_PROC_PT BSSGP_PROC;
Harald Welte853c0ad2018-02-15 17:45:29 +0100102
Harald Welte5ac31492018-02-15 20:39:13 +0100103 var BssgpConfig g_cfg;
104
Harald Welte853c0ad2018-02-15 17:45:29 +0100105 var BvcState g_ptp_bvc_state := BVC_S_BLOCKED;
106 timer g_T1 := 15.0;
107 timer g_T2 := 60.0;
Harald Welte5ac31492018-02-15 20:39:13 +0100108
109 var ClientEntity ClientTable[16];
Harald Welte853c0ad2018-02-15 17:45:29 +0100110}
111
Harald Welte5ac31492018-02-15 20:39:13 +0100112type record ClientEntity {
113 OCT4 tlli,
114 OCT4 tlli_old optional,
115 hexstring imsi,
116 BssgpCellId cell_id,
117 BSSGP_Client_CT comp_ref,
118 /* LLC entities, one for each SAPI */
119 LLC_Entity llc[16]
Harald Welte853c0ad2018-02-15 17:45:29 +0100120};
121
Harald Welte5ac31492018-02-15 20:39:13 +0100122type record LLC_Entity {
123 boolean sgsn_role,
124 /* N(U) on transmit side for next PDU */
125 uint9_t n_u_tx_next,
126 /* N(U) on receive side, last PDU */
127 uint9_t n_u_rx_last optional
128};
129
130private template LLC_Entity t_LLC_init(boolean sgsn_role := false) := {
131 sgsn_role := sgsn_role,
132 n_u_tx_next := 0,
133 n_u_rx_last := -
134}
135
136type record BssgpConfig {
137 Nsvci nsei,
138 Nsvci bvci,
139 BssgpCellId cell_id,
140 boolean sgsn_role
141};
142
143function f_BnsUdReq(template PDU_BSSGP pdu, BssgpBvci bvci)
144runs on BSSGP_CT return NsUnitdataRequest {
Harald Welte853c0ad2018-02-15 17:45:29 +0100145 var NsUnitdataRequest udr := {
Harald Welte1733a382017-07-23 17:26:17 +0200146 bvci := bvci,
Harald Welte5ac31492018-02-15 20:39:13 +0100147 nsei := g_cfg.nsei,
Harald Welte853c0ad2018-02-15 17:45:29 +0100148 /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an
149 * unbound integer value." when trying to send the reocrd rather than the octetstring */
150 //sdu := omit,
151 //bssgp := valueof(pdu)
152 sdu := enc_PDU_BSSGP(valueof(pdu)),
153 bssgp := omit
154 }
155 return udr;
156}
157
Harald Welte5ac31492018-02-15 20:39:13 +0100158function f_BnsUdInd(template PDU_BSSGP pdu, template BssgpBvci bvci)
159runs on BSSGP_CT return template NsUnitdataIndication {
Harald Welte853c0ad2018-02-15 17:45:29 +0100160 var template NsUnitdataIndication udi := {
161 bvci := bvci,
Harald Welte5ac31492018-02-15 20:39:13 +0100162 nsei := g_cfg.nsei,
Harald Welte853c0ad2018-02-15 17:45:29 +0100163 sdu := *,
164 bssgp := pdu
165 }
166 return udi;
167}
168
169private function f_change_state(BvcState new_state) runs on BSSGP_CT {
170 log("BSSGP State Transition: ", g_ptp_bvc_state, " -> ", new_state);
171 g_ptp_bvc_state := new_state;
Harald Welte5ac31492018-02-15 20:39:13 +0100172 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
173 if (isbound(ClientTable[i].comp_ref)) {
174 BSSGP_SP.send(t_BssgpStsInd(g_cfg.nsei, g_cfg.bvci, g_ptp_bvc_state)) to ClientTable[i].comp_ref;
175 }
176 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100177}
178
179private function f_sendReset() runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100180 var PDU_BSSGP pdu := valueof(ts_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, g_cfg.bvci, g_cfg.cell_id));
Harald Welte853c0ad2018-02-15 17:45:29 +0100181 log("PDU: ", pdu);
182 log("ENC: ", enc_PDU_BSSGP(pdu));
183
184 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
185 BSCP.send(f_BnsUdReq(pdu, 0));
186 g_T2.start;
187 //f_change_state(BVC_S_WAIT_RESET);
188}
189
190private function f_sendUnblock() runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100191 BSCP.send(f_BnsUdReq(t_BVC_UNBLOCK(g_cfg.bvci), 0));
Harald Welte853c0ad2018-02-15 17:45:29 +0100192 g_T1.start;
193}
194
195private function f_sendBlock(BssgpCause cause) runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100196 BSCP.send(f_BnsUdReq(t_BVC_BLOCK(g_cfg.bvci, cause), 0));
Harald Welte853c0ad2018-02-15 17:45:29 +0100197 g_T1.start;
198}
199
200private function f_sendStatus(BssgpCause cause, PDU_BSSGP pdu) runs on BSSGP_CT {
201 /* FIXME: Make sure correct Signaling or PTP BVCI is used! */
Harald Welte5ac31492018-02-15 20:39:13 +0100202 BSCP.send(f_BnsUdReq(ts_BSSGP_STATUS(g_cfg.bvci, cause, pdu), g_cfg.bvci));
203}
204
205/* attempt to extract the TLLI from a BSSGP PDU */
206function f_bssgp_get_tlli(PDU_BSSGP bssgp) return template OCT4 {
207 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
208 return bssgp.pDU_BSSGP_DL_UNITDATA.tLLI_current;
209 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
210 return bssgp.pDU_BSSGP_UL_UNITDATA.tLLI;
211 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY)) {
212 return bssgp.pDU_BSSGP_RA_CAPABILITY.tLLI.tLLI_Value;
213 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE)) {
214 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE.tLLI.tLLI_Value;
215 } else if (ischosen(bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK)) {
216 return bssgp.pDU_BSSGP_RA_CAPABILITY_UPDATE_ACK.tLLI.tLLI_Value;
217 } else if (ischosen(bssgp.pDU_BSSGP_RADIO_STATUS)) {
218 return bssgp.pDU_BSSGP_RADIO_STATUS.tLLI.tLLI_Value;
219 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND)) {
220 return bssgp.pDU_BSSGP_SUSPEND.tLLI.tLLI_Value;
221 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_ACK)) {
222 return bssgp.pDU_BSSGP_SUSPEND_ACK.tLLI.tLLI_Value;
223 } else if (ischosen(bssgp.pDU_BSSGP_SUSPEND_NACK)) {
224 return bssgp.pDU_BSSGP_SUSPEND_NACK.tLLI.tLLI_Value;
225 } else if (ischosen(bssgp.pDU_BSSGP_RESUME)) {
226 return bssgp.pDU_BSSGP_RESUME.tLLI.tLLI_Value;
227 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_ACK)) {
228 return bssgp.pDU_BSSGP_RESUME_ACK.tLLI.tLLI_Value;
229 } else if (ischosen(bssgp.pDU_BSSGP_RESUME_NACK)) {
230 return bssgp.pDU_BSSGP_RESUME_NACK.tLLI.tLLI_Value;
231 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL)) {
232 return bssgp.pDU_BSSGP_FLUSH_LL.tLLI.tLLI_Value;
233 } else if (ischosen(bssgp.pDU_BSSGP_FLUSH_LL_ACK)) {
234 return bssgp.pDU_BSSGP_FLUSH_LL_ACK.tLLI.tLLI_Value;
235 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
236 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
237 } else if (ischosen(bssgp.pDU_BSSGP_LLC_DISCARDED)) {
238 return bssgp.pDU_BSSGP_LLC_DISCARDED.tLLI.tLLI_Value;
239 } else if (ischosen(bssgp.pDU_BSSGP_PAGING_CS) and
240 isvalue(bssgp.pDU_BSSGP_PAGING_CS.tLLI)) {
241 return bssgp.pDU_BSSGP_PAGING_CS.tLLI.tLLI_Value;
242 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS)) {
243 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS.tLLI.tLLI_Value;
244 } else if (ischosen(bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK)) {
245 return bssgp.pDU_BSSGP_FLOW_CONTROL_MS_ACK.tLLI.tLLI_Value;
246 }
247 /* TODO: Handover, PFC, LCS */
248 return omit;
249}
250
251/*
252private function f_tbl_init() runs on BSSGP_CT {
253 var integer i;
254 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
255 ImsiTable[i] := -;
256 }
257
258 for (i := 0; i < sizeof(TlliTable); i := i+1) {
259 TlliTable[i] := -;
260 }
261}
262*/
263
264private function f_tbl_client_add(hexstring imsi, OCT4 tlli, BssgpCellId cell_id, BSSGP_Client_CT vc_conn)
265runs on BSSGP_CT {
266 var integer i;
267 for (i := 0; i < sizeof(ClientTable); i := i+1) {
268 if (not isbound(ClientTable[i].comp_ref)) {
269 log("Adding Client IMSI=", imsi, ", TLLI=", tlli, ", index=", i);
270 ClientTable[i] := {
271 tlli := tlli,
272 tlli_old := omit,
273 imsi := imsi,
274 cell_id := cell_id,
275 comp_ref := vc_conn,
276 llc := -
277 };
278 for (var integer j := 0; j < sizeof(ClientTable[i].llc); j := j+1) {
279 ClientTable[i].llc[j] := valueof(t_LLC_init(g_cfg.sgsn_role));
280 }
281 return;
282 }
283 }
284 setverdict(fail, "Client Table full");
285 self.stop;
286}
287
288private function f_tbl_client_del(hexstring imsi, BSSGP_Client_CT vc_conn) runs on BSSGP_CT {
289 var integer i;
290 for (i := 0; i < sizeof(ClientTable); i := i+1) {
291 if (isbound(ClientTable[i].imsi) and ClientTable[i].imsi == imsi) {
292 if (ClientTable[i].comp_ref != vc_conn) {
293 setverdict(fail, "Cannot unregister IMSI ", imsi, " registred to ",
294 ClientTable[i].comp_ref, " from ", vc_conn);
295 self.stop;
296 }
297 log("Removing Client IMSI=", imsi, ", index=", i);
298 ClientTable[i] := { -, omit, -, -, - };
299 return;
300 }
301 }
302 setverdict(fail, "Could not find client for IMSI ", imsi);
303 self.stop;
304}
305
306private function f_tbl_comp_by_imsi(hexstring imsi) runs on BSSGP_CT return BSSGP_Client_CT {
307 var integer i;
308 for (i := 0; i < sizeof(ClientTable); i := i+1) {
309 if (isbound(ClientTable[i].imsi) and isbound(ClientTable[i].comp_ref)
310 and ClientTable[i].imsi == imsi) {
311 return ClientTable[i].comp_ref;
312 }
313 }
314 setverdict(fail, "Couldn't find Component for IMSI ", imsi);
315 self.stop;
316}
317
318private function f_tbl_comp_by_tlli(OCT4 tlli) runs on BSSGP_CT return BSSGP_Client_CT {
319 var integer i;
320 for (i := 0; i < sizeof(ClientTable); i := i+1) {
321 if (isbound(ClientTable[i].comp_ref) and
322 (isbound(ClientTable[i].tlli) and (ClientTable[i].tlli == tlli or
323 isbound(ClientTable[i].tlli_old) and ClientTable[i].tlli_old == tlli) )) {
324 return ClientTable[i].comp_ref;
325 }
326 }
327 setverdict(fail, "Couldn't find Component for TLLI ", tlli);
328 self.stop;
329}
330
331private function f_tbl_idx_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_CT return integer {
332 var integer i;
333 for (i := 0; i < sizeof(ClientTable); i := i+1) {
334 if (isbound(ClientTable[i].comp_ref) and ClientTable[i].comp_ref == comp_ref) {
335 return i;
336 }
337 }
338 setverdict(fail, "Couldn't find Client for Component ", comp_ref);
339 self.stop;
340}
341
342private function f_tbl_tlli_by_comp(BSSGP_Client_CT comp_ref) runs on BSSGP_CT return OCT4 {
343 var integer i;
344 for (i := 0; i < sizeof(ClientTable); i := i+1) {
345 if (isbound(ClientTable[i].tlli) and isbound(ClientTable[i].comp_ref)
346 and ClientTable[i].comp_ref == comp_ref) {
347 return ClientTable[i].tlli;
348 }
349 }
350 setverdict(fail, "Couldn't find TLLI for Component ", comp_ref);
351 self.stop;
Harald Welte853c0ad2018-02-15 17:45:29 +0100352}
353
354altstep as_allstate() runs on BSSGP_CT {
Harald Welte5ac31492018-02-15 20:39:13 +0100355 var BSSGP_Client_CT vc_conn;
Harald Welte853c0ad2018-02-15 17:45:29 +0100356 var NsUnitdataIndication udi;
357 var NsStatusIndication nsi;
358 var ASP_Event evt;
Harald Welte5ac31492018-02-15 20:39:13 +0100359 var hexstring imsi;
360 var OCT4 tlli;
361 var BssgpCellId cell_id;
Harald Welte853c0ad2018-02-15 17:45:29 +0100362
363 /* Respond to BLOCK for wrong NSVCI */
364 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(?, ?), 0)) -> value udi {
365 log("Rx BVC-BLOCK for unknown BVCI");
366 f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200367 }
368
Harald Welte853c0ad2018-02-15 17:45:29 +0100369 /* Respond to RESET with correct BVCI/CellID */
Harald Welte5ac31492018-02-15 20:39:13 +0100370 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
371 log("Rx BVC-RESET for Our BVCI=", g_cfg.bvci);
372 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0));
Harald Welte853c0ad2018-02-15 17:45:29 +0100373 f_change_state(BVC_S_UNBLOCKED);
Harald Welte1733a382017-07-23 17:26:17 +0200374 }
375
Harald Welte853c0ad2018-02-15 17:45:29 +0100376 /* Respond to RESET for signalling BVCI 0 */
Harald Welte5ac31492018-02-15 20:39:13 +0100377 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, 0, g_cfg.cell_id), 0)) -> value udi {
Harald Welte853c0ad2018-02-15 17:45:29 +0100378 log("Rx BVC-RESET for Signaling BVCI=0");
Harald Welte5ac31492018-02-15 20:39:13 +0100379 BSCP.send(f_BnsUdReq(ts_BVC_RESET_ACK(0, g_cfg.cell_id), 0));
Harald Welte1733a382017-07-23 17:26:17 +0200380 }
381
Harald Welte853c0ad2018-02-15 17:45:29 +0100382 /* Respond to RESET with wrong NSEI/NSVCI */
383 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET(?, ?, ?), 0)) -> value udi {
384 log("Rx BVC-RESET for unknown BVCI");
385 f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200386 }
387
Harald Welte853c0ad2018-02-15 17:45:29 +0100388 /* default case of handling unknown PDUs */
389 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
390 log("Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_ptp_bvc_state);
391 f_sendStatus(BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200392 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100393 /* Forwarding of ASP_Event and NsStatusIndication to user */
Harald Welte5ac31492018-02-15 20:39:13 +0100394 [] BSCP.receive(ASP_Event:?) -> value evt {
395 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
396 if (isbound(ClientTable[i].comp_ref)) {
397 BSSGP_SP.send(evt) to ClientTable[i].comp_ref;
398 }
399 }
400 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100401 [] BSCP.receive(NsStatusIndication:?) -> value nsi {
402 /* if we just became NS-unblocked, send a BCC-RESET */
403 if (nsi.old_state != NSE_S_ALIVE_UNBLOCKED and nsi.new_state == NSE_S_ALIVE_UNBLOCKED) {
Harald Welte5ac31492018-02-15 20:39:13 +0100404 if (g_cfg.sgsn_role == false) {
Harald Welte853c0ad2018-02-15 17:45:29 +0100405 f_sendReset();
Harald Welte1733a382017-07-23 17:26:17 +0200406 }
Harald Welte5ac31492018-02-15 20:39:13 +0100407 /* Idea: We could send BVC-UNBLOCK here like some SGSN do */
Harald Welte1733a382017-07-23 17:26:17 +0200408 }
Harald Welte5ac31492018-02-15 20:39:13 +0100409 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
410 if (isbound(ClientTable[i].comp_ref)) {
411 BSSGP_SP.send(nsi) to ClientTable[i].comp_ref;
412 }
413 }
414 }
415
416 [] BSSGP_PROC.getcall(BSSGP_register_client:{?,?,?}) -> param(imsi, tlli, cell_id) sender vc_conn {
417 f_tbl_client_add(imsi, tlli, cell_id, vc_conn);
418 BSSGP_PROC.reply(BSSGP_register_client:{imsi, tlli, cell_id});
419 }
420 [] BSSGP_PROC.getcall(BSSGP_unregister_client:{?}) -> param(imsi) sender vc_conn {
421 f_tbl_client_del(imsi, vc_conn);
422 BSSGP_PROC.reply(BSSGP_unregister_client:{imsi});
423 }
424}
425
426altstep as_blocked() runs on BSSGP_CT {
427 [] g_T1.timeout {
428 f_sendUnblock();
429 }
430 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0)) {
431 g_T1.stop;
432 f_change_state(BVC_S_UNBLOCKED);
433 }
434 [not g_cfg.sgsn_role] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, omit), 0)) {
435 f_sendUnblock();
Harald Welte1733a382017-07-23 17:26:17 +0200436 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100437}
Harald Welte1733a382017-07-23 17:26:17 +0200438
Harald Welte5ac31492018-02-15 20:39:13 +0100439altstep as_unblocked() runs on BSSGP_CT {
440 var BSSGP_Client_CT vc_conn;
Harald Welte853c0ad2018-02-15 17:45:29 +0100441 var NsUnitdataIndication udi;
442 var PDU_BSSGP bs_pdu;
Harald Welte5ac31492018-02-15 20:39:13 +0100443 var PDU_L3_MS_SGSN l3_mo;
444 var PDU_L3_SGSN_MS l3_mt;
Harald Welte1733a382017-07-23 17:26:17 +0200445
Harald Welte5ac31492018-02-15 20:39:13 +0100446 /* bogus unblock, just respond with ACK */
447 [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(g_cfg.bvci), 0)) -> value udi {
448 BSCP.send(f_BnsUdReq(t_BVC_UNBLOCK_ACK(g_cfg.bvci), 0));
449 }
450 /* Respond to BLOCK with BLOCK-ACK + change state */
451 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(g_cfg.bvci, ?), 0)) -> value udi {
452 BSCP.send(f_BnsUdReq(t_BVC_BLOCK_ACK(g_cfg.bvci), 0));
453 g_T1.stop;
454 f_change_state(BVC_S_BLOCKED);
455 }
456 [] g_T1.timeout {
457 f_sendBlock(BSSGP_CAUSE_OM_INTERVENTION);
458 }
459 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(g_cfg.bvci), 0)) -> value udi {
460 g_T1.stop;
461 f_change_state(BVC_S_BLOCKED);
462 }
463 [] BSCP.receive(f_BnsUdInd(tr_BVC_RESET_ACK(g_cfg.bvci, g_cfg.cell_id), 0)) -> value udi {
464 g_T2.stop;
465 f_change_state(BVC_S_UNBLOCKED);
466 }
Harald Welte13d391e2017-07-23 19:52:33 +0200467
Harald Welte5ac31492018-02-15 20:39:13 +0100468 /* simply acknowledge all Flow Control Messages */
Harald Weltee0abc472018-02-05 09:13:31 +0100469/*
Harald Welte5ac31492018-02-15 20:39:13 +0100470 [g_cfg.sgsn_role] BSCP.receive(f_BnsUdInd(t_BVC_FC_BVC), g_cfg.bvci) {
471 BSCP.send(f_BnsUdReq(t_BVC_FC_BVC_ACK), g_cfg.bvci);
472 }
473 [g_cfg.sgsn_role] BSCP.receive(f_BnsUdInd(t_BVC_FC_MS), g_cfg.bvci) {
474 BSCP.send(f_BnsUdReq(t_BVC_FC_MS_ACK), g_cfg.bvci);
475 }
Harald Weltee0abc472018-02-05 09:13:31 +0100476*/
Harald Welte13d391e2017-07-23 19:52:33 +0200477
Harald Welte5ac31492018-02-15 20:39:13 +0100478 /* FIXME: CS PAGING: dispatch by IMSI */
Harald Welte1733a382017-07-23 17:26:17 +0200479
Harald Welte5ac31492018-02-15 20:39:13 +0100480 /* PS PAGING: dispatch by IMSI */
481 [] BSCP.receive(f_BnsUdInd(tr_BSSGP_PS_PAGING(g_cfg.bvci), g_cfg.bvci)) -> value udi {
482 var hexstring imsi := udi.bssgp.pDU_BSSGP_PAGING_PS.iMSI.digits
483 vc_conn := f_tbl_comp_by_imsi(imsi);
484 BSSGP_SP.send(f_dec_bssgp(udi.bssgp)) to vc_conn;
485 }
486
487 /* Any other BSSGP message: If it has TLLi, route to component; otherwise broadcast */
488 [] BSCP.receive(f_BnsUdInd(?, g_cfg.bvci)) -> value udi {
489 var BssgpDecoded dec := f_dec_bssgp(udi.bssgp);
490 var template OCT4 tlli := f_bssgp_get_tlli(udi.bssgp);
491 if (isvalue(tlli)) {
492 vc_conn := f_tbl_comp_by_tlli(valueof(tlli));
493 BSSGP_SP.send(dec) to vc_conn;
494 } else {
495 log("No TLLI: Broadcasting ", dec);
496 /* broadcast this message to all components */
497 // TITAN DOESN'T DO THIS, *SIGH*: "BSSGP_SP.send(dec) to all component;"
498 for (var integer i := 0; i < sizeof(ClientTable); i := i+1) {
499 if (isbound(ClientTable[i].comp_ref)) {
500 BSSGP_SP.send(dec) to ClientTable[i].comp_ref;
501 }
502 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100503 }
Harald Welte1733a382017-07-23 17:26:17 +0200504 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100505
Harald Welte5ac31492018-02-15 20:39:13 +0100506 /* pass virtually any PDU from user to NS-UNITDATA PDU on network */
507 [] BSSGP_SP.receive(PDU_BSSGP:?) -> value bs_pdu sender vc_conn {
508 BSCP.send(f_BnsUdReq(bs_pdu, g_cfg.bvci));
509 }
510
511 [not g_cfg.sgsn_role] BSSGP_SP.receive(PDU_L3_MS_SGSN:?) -> value l3_mo sender vc_conn {
512 var integer idx := f_tbl_idx_by_comp(vc_conn);
513 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(l3_mo);
514 var BIT4 sapi := f_llc_sapi_by_l3_mo(l3_mo);
515 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
516 var octetstring llc_enc := enc_PDU_LLC(valueof(ts_LLC_UI(l3_enc, sapi, '0'B, n_u)));
517 BSCP.send(f_BnsUdReq(ts_BSSGP_UL_UD(ClientTable[idx].tlli, ClientTable[idx].cell_id, llc_enc), g_cfg.bvci));
518 }
519
520 [g_cfg.sgsn_role] BSSGP_SP.receive(PDU_L3_SGSN_MS:?) -> value l3_mt sender vc_conn {
521 var integer idx := f_tbl_idx_by_comp(vc_conn);
522 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(l3_mt);
523 var BIT4 sapi := f_llc_sapi_by_l3_mt(l3_mt);
524 var integer n_u := f_llc_get_n_u_tx(ClientTable[idx].llc[bit2int(sapi)]);
525 var octetstring llc_enc := enc_PDU_LLC(valueof(ts_LLC_UI(l3_enc, sapi, '1'B, n_u)));
526 //BSCP.send(f_BnsUdReq(ts_BSSGP_DL_UD(ClientTable[idx].tlli, ClientTable[idx].cell_id, llc_enc)), g_cfg.bvci);
527 }
Harald Welte853c0ad2018-02-15 17:45:29 +0100528}
529
Harald Welte5ac31492018-02-15 20:39:13 +0100530private function f_llc_get_n_u_tx(inout LLC_Entity llc) return uint9_t {
531 var uint9_t ret := llc.n_u_tx_next;
532 llc.n_u_tx_next := llc.n_u_tx_next + 1;
533 return ret;
534}
535
536private function f_llc_sapi_by_l3_mo(PDU_L3_MS_SGSN l3_mo) return BIT4 {
537 if (ischosen(l3_mo.msgs.gprs_mm)) {
538 return c_LLC_SAPI_LLGMM;
539 } else if (ischosen(l3_mo.msgs.gprs_sm)) {
540 return c_LLC_SAPI_LLSM;
541 } else if (ischosen(l3_mo.msgs.sms)) {
542 return c_LLC_SAPI_LLSMS;
543 }
544 setverdict(fail, "No LLC SAPI for ", l3_mo);
545 self.stop;
546}
547
548private function f_llc_sapi_by_l3_mt(PDU_L3_SGSN_MS l3_mt) return BIT4 {
549 if (ischosen(l3_mt.msgs.gprs_mm)) {
550 return c_LLC_SAPI_LLGMM;
551 } else if (ischosen(l3_mt.msgs.gprs_sm)) {
552 return c_LLC_SAPI_LLSM;
553 } else if (ischosen(l3_mt.msgs.sms)) {
554 return c_LLC_SAPI_LLSMS;
555 }
556 setverdict(fail, "No LLC SAPI for ", l3_mt);
557 self.stop;
558}
559
560
561
562private function f_ScanEvents() runs on BSSGP_CT {
563 log("matching against ", tr_BVC_RESET(?, g_cfg.bvci, g_cfg.cell_id));
564
565 while (true) {
566 alt {
567 [g_ptp_bvc_state == BVC_S_BLOCKED] as_blocked();
568 [g_ptp_bvc_state == BVC_S_UNBLOCKED] as_unblocked();
569 [] as_allstate();
570 }
571 } /* while */
572}
573
574/* PDU_BSSGP enhanced with LLC and possibly L3 decoded payloads */
575type record BssgpDecoded {
576 PDU_BSSGP bssgp,
577 PDU_LLC llc optional,
578 PDU_L3_MS_SGSN l3_mo optional,
579 PDU_L3_SGSN_MS l3_mt optional
580}
581
582/* Decode a PDU_BSSGP into a BssgpDecoded (i.e. with LLC/L3 decoded, as applicable) */
583private function f_dec_bssgp(PDU_BSSGP bssgp) runs on BSSGP_CT return BssgpDecoded {
584 var BssgpDecoded dec := {
585 bssgp := bssgp,
586 llc := omit,
587 l3_mo := omit,
588 l3_mt := omit
589 };
590
591 /* Decode LLC, if it is a PDU that contains LLC */
592 if (ischosen(bssgp.pDU_BSSGP_DL_UNITDATA)) {
593 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_DL_UNITDATA.lLC_PDU.lLC_PDU);
594 } else if (ischosen(bssgp.pDU_BSSGP_UL_UNITDATA)) {
595 dec.llc := dec_PDU_LLC(bssgp.pDU_BSSGP_UL_UNITDATA.lLC_PDU.lLC_PDU);
596 }
597
598 /* Decode L3, if it is a LLC PDU containing L3 */
599 if (isvalue(dec.llc) and match(dec.llc, tr_LLC_UI_L3)) {
600 if (g_cfg.sgsn_role) {
601 dec.l3_mo := dec_PDU_L3_MS_SGSN(dec.llc.pDU_LLC_UI.information_field_UI);
602 } else {
603 dec.l3_mt := dec_PDU_L3_SGSN_MS(dec.llc.pDU_LLC_UI.information_field_UI);
604 }
605 }
606 return dec;
607}
608
609function f_bssgp_client_register(hexstring imsi, OCT4 tlli, BssgpCellId cell_id, BSSGP_PROC_PT PT := BSSGP_PROC)
610runs on BSSGP_Client_CT {
611 PT.call(BSSGP_register_client:{imsi, tlli, cell_id}) {
612 [] PT.getreply(BSSGP_register_client:{imsi, tlli, cell_id}) {};
613 }
614}
615
616function f_bssgp_client_unregister(hexstring imsi, BSSGP_PROC_PT PT := BSSGP_PROC)
617runs on BSSGP_Client_CT {
618 PT.call(BSSGP_unregister_client:{imsi}) {
619 [] PT.getreply(BSSGP_unregister_client:{imsi}) {};
620 }
621}
622
623template BssgpDecoded tr_BD_BSSGP(template PDU_BSSGP bg) := {
624 bssgp := bg,
625 llc := *,
626 l3_mo := *,
627 l3_mt := *
628}
629
630
631template BssgpDecoded tr_BD_L3_MT(template PDU_L3_SGSN_MS mt) := {
632 bssgp := ?,
633 llc := ?,
634 l3_mo := omit,
635 l3_mt := mt
636}
637
638template BssgpDecoded tr_BD_L3_MO(template PDU_L3_MS_SGSN mo) := {
639 bssgp := ?,
640 llc := ?,
641 l3_mo := mo,
642 l3_mt := omit
643}
644
645
646
Harald Welte853c0ad2018-02-15 17:45:29 +0100647
Harald Welte1733a382017-07-23 17:26:17 +0200648}