blob: b1c6b81418b3761a7368e14af3f439de9e1ece67 [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module BSSMAP_Emulation {
2
Harald Welte35bb7162018-01-03 21:07:52 +01003/* BSSMAP Emulation, runs on top of BSSAP_CodecPort. It multiplexes/demultiplexes
4 * the individual connections, so there can be separate TTCN-3 components handling
5 * each of the connections.
6 *
7 * The BSSMAP_Emulation.main() function processes SCCP primitives from the SCCP
8 * stack via the BSSAP_CodecPort, and dispatches them to the per-connection components.
9 *
10 * Outbound BSSAP/SCCP connections are initiated by sending a BSSAP_Conn_Req primitive
11 * to the component running the BSSMAP_Emulation.main() function.
12 *
13 * For each new inbound connections, the BssmapOps.create_cb() is called. It can create
14 * or resolve a TTCN-3 component, and returns a component reference to which that inbound
15 * connection is routed/dispatched.
16 *
17 * If a pre-existing component wants to register to handle a future inbound connection, it can
18 * do so by registering an "expect" with the expected Layer 3 (DTAP) payload. This is e.g. useful
19 * if you are simulating BTS + MSC, and first trigger a connection from BTS/RSL side in a
20 * component which then subsequently should also handle the MSC emulation.
21 *
22 * Inbound Unit Data messages (such as are dispatched to the BssmapOps.unitdata_cb() callback,
23 * which is registered with an argument to the main() function below.
24 *
Harald Welte0b476062018-01-21 19:07:32 +010025 * (C) 2017-2018 by Harald Welte <laforge@gnumonks.org>
Harald Welte35bb7162018-01-03 21:07:52 +010026 * All rights reserved.
27 *
28 * Released under the terms of GNU General Public License, Version 2 or
29 * (at your option) any later version.
30 */
31
32
Harald Welte0b476062018-01-21 19:07:32 +010033import from General_Types all;
Harald Welte9dadc522018-02-06 13:56:04 +010034import from Osmocom_Types all;
Harald Welteb3414b22017-11-23 18:22:10 +010035import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010036import from SCCPasp_Types all;
37import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010038import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010039import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +010040import from MGCP_Types all;
41import from MGCP_Templates all;
42import from IPA_Emulation all;
Harald Welte0b476062018-01-21 19:07:32 +010043import from MobileL3_Types all;
Harald Welte365f4ed2017-11-23 00:00:43 +010044
45/* General "base class" component definition, of which specific implementations
46 * derive themselves by means of the "extends" feature */
47type component BSSAP_ConnHdlr {
48 /* port towards MSC Emulator core / SCCP connection dispatchar */
49 port BSSAP_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010050 /* procedure based port to register for incoming connections */
51 port BSSMAPEM_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010052}
53
54/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
55type enumerated BSSAP_Conn_Prim {
56 /* SCCP tell us that connection was released */
57 MSC_CONN_PRIM_DISC_IND,
58 /* we tell SCCP to release connection */
Harald Welte71b69332018-01-21 20:43:53 +010059 MSC_CONN_PRIM_DISC_REQ,
60 /* Connection confirmed indication */
61 MSC_CONN_PRIM_CONF_IND
Harald Welte365f4ed2017-11-23 00:00:43 +010062}
63
Harald Welteb3414b22017-11-23 18:22:10 +010064type record BSSAP_Conn_Req {
65 SCCP_PAR_Address addr_peer,
66 SCCP_PAR_Address addr_own,
67 PDU_BSSAP bssap
68}
69
Harald Welte0b476062018-01-21 19:07:32 +010070/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
71type record PDU_DTAP_MO {
72 OCT1 dlci optional,
73 PDU_ML3_MS_NW dtap
74}
75
76/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
77type record PDU_DTAP_MT {
78 OCT1 dlci optional,
79 PDU_ML3_NW_MS dtap
80}
81
82template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
83 dlci := dlci,
84 dtap := dtap
85}
86
87template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O) := {
88 dlci := dlci,
89 dtap := dtap
90}
91
92template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
93 dlci := dlci,
94 dtap := dtap
95}
96
97template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
98 dlci := dlci,
99 dtap := dtap
100}
101
102
Harald Welte365f4ed2017-11-23 00:00:43 +0100103/* port between individual per-connection components and this dispatcher */
104type port BSSAP_Conn_PT message {
Harald Weltea4ca4462018-02-09 00:17:14 +0100105 /* BSSAP or direct DTAP messages from/to clients */
106 inout PDU_BSSAP, PDU_DTAP_MO, PDU_DTAP_MT,
107 /* misc indications / requests between SCCP and client */
108 BSSAP_Conn_Prim,
109 /* Client requests us to create SCCP Connection */
110 BSSAP_Conn_Req,
111 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
112 MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +0100113} with { extension "internal" };
114
115
116/* represents a single BSSAP connection over SCCP */
117type record ConnectionData {
118 /* reference to the instance of the per-connection component */
119 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100120 integer sccp_conn_id,
121 /* most recent MGCP transaction ID (Used on MSC side) */
122 MgcpTransId mgcp_trans_id optional,
123 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100124 integer cic optional,
125 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
126 uint2_t n_sd[4]
Harald Welte365f4ed2017-11-23 00:00:43 +0100127}
128
Harald Welte17d21152018-01-27 00:47:11 +0100129type record ImsiMapping {
130 BSSAP_ConnHdlr comp_ref,
131 hexstring imsi optional,
132 OCT4 tmsi
133}
134
Harald Welte365f4ed2017-11-23 00:00:43 +0100135type component BSSMAP_Emulation_CT {
136 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100137 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100138 /* BSSAP port to the per-connection clients */
139 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +0100140 /* MGCP port */
141 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100142
143 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
144 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100145
Harald Welte624f9632017-12-16 19:26:04 +0100146 /* pending expected incoming connections */
147 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100148
149 /* tables for mapping inbound unitdata (like paging) */
150 var ImsiMapping ImsiTable[16];
151
Harald Welte624f9632017-12-16 19:26:04 +0100152 /* procedure based port to register for incoming connections */
153 port BSSMAPEM_PROC_PT PROC;
154
Harald Weltebe620f62017-11-25 00:23:54 +0100155 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100156 var integer g_next_e1_ts := 1;
Harald Welte0b476062018-01-21 19:07:32 +0100157 var BssmapOps g_bssmap_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100158};
159
Harald Welteb3414b22017-11-23 18:22:10 +0100160private function f_conn_id_known(integer sccp_conn_id)
161runs on BSSMAP_Emulation_CT return boolean {
162 var integer i;
163 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
164 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
165 return true;
166 }
167 }
168 return false;
169}
170
171private function f_comp_known(BSSAP_ConnHdlr client)
172runs on BSSMAP_Emulation_CT return boolean {
173 var integer i;
174 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
175 if (ConnectionTable[i].comp_ref == client) {
176 return true;
177 }
178 }
179 return false;
180}
Harald Welte365f4ed2017-11-23 00:00:43 +0100181
Harald Weltee98bb2e2017-11-29 12:09:48 +0100182private function f_cic_known(integer cic)
183runs on BSSMAP_Emulation_CT return boolean {
184 var integer i;
185 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
186 if (ConnectionTable[i].cic == cic) {
187 return true;
188 }
189 }
190 return false;
191}
192
Harald Welte365f4ed2017-11-23 00:00:43 +0100193/* resolve component reference by connection ID */
194private function f_comp_by_conn_id(integer sccp_conn_id)
195runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
196 var integer i;
197 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
198 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
199 return ConnectionTable[i].comp_ref;
200 }
201 }
202 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100203 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100204 self.stop;
205}
206
Harald Weltec82eef42017-11-24 20:40:12 +0100207/* resolve component reference by CIC */
208private function f_comp_by_mgcp_tid(MgcpTransId tid)
209runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
210 var integer i;
211 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
212 if (ConnectionTable[i].mgcp_trans_id == tid) {
213 return ConnectionTable[i].comp_ref;
214 }
215 }
216 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100217 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100218 self.stop;
219}
220
221private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
222runs on BSSMAP_Emulation_CT {
223 var integer i;
224 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
225 if (ConnectionTable[i].comp_ref == client) {
226 ConnectionTable[i].mgcp_trans_id := tid;
227 return;
228 }
229 }
230 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100231 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100232 self.stop;
233}
234
235private function f_comp_by_cic(integer cic)
236runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
237 var integer i;
238 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
239 if (ConnectionTable[i].cic == cic) {
240 return ConnectionTable[i].comp_ref;
241 }
242 }
243 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100244 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100245 self.stop;
246}
247
248private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
249runs on BSSMAP_Emulation_CT {
250 var integer i;
251 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
252 if (ConnectionTable[i].comp_ref == client) {
253 ConnectionTable[i].cic := cic;
254 return;
255 }
256 }
257 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100258 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100259}
260
Harald Welte365f4ed2017-11-23 00:00:43 +0100261/* resolve connection ID by component reference */
262private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
263runs on BSSMAP_Emulation_CT return integer {
264 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
265 if (ConnectionTable[i].comp_ref == client) {
266 return ConnectionTable[i].sccp_conn_id;
267 }
268 }
269 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100270 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100271 self.stop;
272}
273
Harald Welte9dadc522018-02-06 13:56:04 +0100274/* resolve ConnectionTable index component reference */
275private function f_idx_by_comp(BSSAP_ConnHdlr client)
276runs on BSSMAP_Emulation_CT return integer {
277 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
278 if (ConnectionTable[i].comp_ref == client) {
279 return i;
280 }
281 }
282 log("BSSMAP Connection table not found by component ", client);
283 setverdict(fail);
284 self.stop;
285}
286
Harald Welteb3414b22017-11-23 18:22:10 +0100287private function f_gen_conn_id()
288runs on BSSMAP_Emulation_CT return integer {
289 var integer conn_id;
290
291 do {
292 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
293 } while (f_conn_id_known(conn_id) == true);
294
295 return conn_id;
296}
297
Harald Welte365f4ed2017-11-23 00:00:43 +0100298private function f_conn_table_init()
299runs on BSSMAP_Emulation_CT {
300 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
301 ConnectionTable[i].comp_ref := null;
302 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100303 ConnectionTable[i].mgcp_trans_id := omit;
304 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100305 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100306 }
Harald Welte17d21152018-01-27 00:47:11 +0100307 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
308 ImsiTable[i].comp_ref := null;
309 ImsiTable[i].imsi := omit;
310 ImsiTable[i].tmsi := 'FFFFFFFF'O;
311 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100312}
313
314private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
315runs on BSSMAP_Emulation_CT {
316 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
317 if (ConnectionTable[i].sccp_conn_id == -1) {
318 ConnectionTable[i].comp_ref := comp_ref;
319 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100320 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100321 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100322 return;
323 }
324 }
325 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100326 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100327 self.stop;
328}
329
330private function f_conn_table_del(integer sccp_conn_id)
331runs on BSSMAP_Emulation_CT {
332 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
333 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100334 log("Deleted conn table entry ", i,
335 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100336 ConnectionTable[i].sccp_conn_id := -1;
337 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100338 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100339 }
340 }
341 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100342 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100343 self.stop;
344}
345
Harald Welte17d21152018-01-27 00:47:11 +0100346private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
347runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
348 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
349 if (ImsiTable[i].imsi == imsi or
350 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
351 return ImsiTable[i].comp_ref;
352 }
353 }
354 return null;
355}
356
Harald Welte365f4ed2017-11-23 00:00:43 +0100357/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100358private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100359runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100360 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100361
Harald Welte473676b2018-01-27 20:38:01 +0100362 if (ischosen(bssap.pdu.bssmap)) {
363 /* BSC Side: If this is an assignment command, store CIC */
364 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
365 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
366 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
367 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
368 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
369 f_comp_store_cic(client, cic);
370 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100371 }
372
Harald Welte0b476062018-01-21 19:07:32 +0100373 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
374 if (g_bssmap_ops.role_ms) {
375 /* we are the MS, so any message to us must be MT */
376 var PDU_DTAP_MT mt := {
377 dlci := bssap.dlci,
378 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
379 };
380 CLIENT.send(mt) to client;
381 } else {
382 /* we are the Network, so any message to us must be MO */
383 var PDU_DTAP_MO mo := {
384 dlci := bssap.dlci,
385 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
386 };
387 CLIENT.send(mo) to client;
388 }
389 } else {
390 CLIENT.send(bssap) to client;
391 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100392}
393
394/* call-back type, to be provided by specific implementation; called when new SCCP connection
395 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100396type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100397runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
398
399type function BssmapUnitdataCallback(PDU_BSSAP bssap)
400runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
401
Harald Welte17d21152018-01-27 00:47:11 +0100402/* handle common Unitdata such as Paging */
403private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
404runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
405 if (match(bssap, tr_BSSMAP_Paging)) {
406 var BSSAP_ConnHdlr client := null;
407 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
408 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
409 if (isvalue(client)) {
410 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
411 client);
412 CLIENT.send(bssap) to client;
413 return omit;
414 }
415 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
416 } else {
417 log("CommonBssmapUnitdataCallback: Not a paging message");
418 }
419 /* ELSE: handle in user callback */
420 return g_bssmap_ops.unitdata_cb.apply(bssap);
421}
422
Harald Welte365f4ed2017-11-23 00:00:43 +0100423type record BssmapOps {
424 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100425 BssmapUnitdataCallback unitdata_cb,
426 boolean decode_dtap,
Harald Weltea4ca4462018-02-09 00:17:14 +0100427 boolean role_ms,
428 /* needed for performing BSSMAP RESET */
429 SCCP_PAR_Address sccp_addr_local optional,
430 SCCP_PAR_Address sccp_addr_peer optional
Harald Welte365f4ed2017-11-23 00:00:43 +0100431}
432
Harald Welte9dadc522018-02-06 13:56:04 +0100433template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
434
435/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
436function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
437 var uint2_t seq_nr;
438 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
439 seq_nr := cd.n_sd[0];
440 cd.n_sd[0] := (cd.n_sd[0] + 1) mod 4;
441 } else if (ischosen(dtap.msgs.gcc)) {
442 seq_nr := cd.n_sd[1];
443 cd.n_sd[1] := (cd.n_sd[1] + 1) mod 2;
444 } else if (ischosen(dtap.msgs.bcc)) {
445 seq_nr := cd.n_sd[2];
446 cd.n_sd[2] := (cd.n_sd[2] + 1) mod 2;
447 } else if (ischosen(dtap.msgs.loc)) {
448 seq_nr := cd.n_sd[3];
449 cd.n_sd[3] := (cd.n_sd[3] + 1) mod 2;
450 } else {
451 /* no sequence number to patch */
452 return;
453 }
454 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
455 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
456 log("patched enc_l3: ", enc_l3);
457}
458
Harald Welte49518bf2018-02-10 11:39:19 +0100459private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
460 var template octetstring l3 := f_bssap_extract_l3(bssap);
461 if (not isvalue(l3)) {
462 return false;
463 }
464 var octetstring l3v := valueof(l3);
465 /* lower 4 bits of first octet are protocol discriminator */
466 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
467 return true;
468 }
469 return false;
470}
471
Harald Weltea4ca4462018-02-09 00:17:14 +0100472private altstep as_reset_ack() runs on BSSMAP_Emulation_CT {
473 var BSSAP_N_UNITDATA_ind ud_ind;
474 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
475 log("Respoding to inbound RESET with RESET-ACK");
476 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
477 ts_BSSMAP_ResetAck));
478 repeat;
479 }
480}
481
482
483private function f_bssap_wait_for_reset() runs on BSSMAP_Emulation_CT {
484 var BSSAP_N_UNITDATA_ind ud_ind;
485 timer T := 20.0;
486
487 T.start;
488 alt {
489 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
490 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
491 ts_BSSMAP_ResetAck));
492 }
493 [] as_reset_ack();
494 [] BSSAP.receive {
495 repeat;
496 }
497 [] T.timeout {
498 setverdict(fail);
499 }
500 }
501}
502
503function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on BSSMAP_Emulation_CT {
504 timer T := 5.0;
505
506 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0)));
507 T.start;
508 alt {
509 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck)) {
510 log("Received RESET-ACK in response to RESET, we're ready to go!");
511 }
512 [] as_reset_ack();
513 [] BSSAP.receive { repeat };
514 [] T.timeout { setverdict(fail, "Waiting for RESET-ACK after sending RESET"); }
515 }
516}
517
Harald Weltebe620f62017-11-25 00:23:54 +0100518function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100519
Harald Weltebe620f62017-11-25 00:23:54 +0100520 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100521 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100522 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100523 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100524
Harald Weltea4ca4462018-02-09 00:17:14 +0100525 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
526 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
527 }
528
Harald Welte365f4ed2017-11-23 00:00:43 +0100529 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100530 var BSSAP_N_UNITDATA_ind ud_ind;
531 var BSSAP_N_CONNECT_ind conn_ind;
532 var BSSAP_N_CONNECT_cfm conn_cfm;
533 var BSSAP_N_DATA_ind data_ind;
534 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100535 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100536 var BSSAP_ConnHdlr vc_conn;
537 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100538 var PDU_DTAP_MO dtap_mo;
539 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100540 var MgcpCommand mgcp_req;
541 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100542 var BSSAP_ConnHdlr vc_hdlr;
543 var octetstring l3_info;
Harald Welte17d21152018-01-27 00:47:11 +0100544 var hexstring imsi;
545 var OCT4 tmsi;
Harald Welte365f4ed2017-11-23 00:00:43 +0100546
547 alt {
548 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100549 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100550 /* Connectionless Procedures like RESET */
551 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100552 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100553 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100554 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
555 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100556 }
557 }
558
559 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100560 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100561 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100562 /* store mapping between client components and SCCP connectionId */
563 f_conn_table_add(vc_conn, conn_ind.connectionId);
564 /* handle user payload */
565 f_handle_userData(vc_conn, conn_ind.userData);
566 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100567 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100568 }
569
570 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100571 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100572 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100573 if (ispresent(data_ind.userData)) {
574 f_handle_userData(vc_conn, data_ind.userData);
575 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100576 }
577
578 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100579 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100580 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100581 if (ispresent(disc_ind.userData)) {
582 f_handle_userData(vc_conn, disc_ind.userData);
583 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100584 /* notify client about termination */
585 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
586 CLIENT.send(prim) to vc_conn;
587 f_conn_table_del(disc_ind.connectionId);
588 /* TOOD: return confirm to other side? */
589 }
590
Harald Welteb3414b22017-11-23 18:22:10 +0100591 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100592 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100593 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
594 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
595 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100596 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100597 if (ispresent(conn_cfm.userData)) {
598 f_handle_userData(vc_conn, conn_cfm.userData);
599 }
Harald Welteb3414b22017-11-23 18:22:10 +0100600 }
601
Harald Welte365f4ed2017-11-23 00:00:43 +0100602 /* Disconnect request client -> SCCP */
603 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
604 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100605 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100606 f_conn_table_del(conn_id);
607 }
608
609 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100610 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
611 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100612 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100613
614 if (f_comp_known(vc_conn) == false) {
615 /* unknown client, create new connection */
616 conn_id := f_gen_conn_id();
617
618 /* store mapping between client components and SCCP connectionId */
619 f_conn_table_add(vc_conn, conn_id);
620
Harald Welte004f5fb2017-12-16 17:54:40 +0100621 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
622 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100623 } else {
624 /* known client, send via existing connection */
625 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100626 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100627 }
628
Harald Welte49518bf2018-02-10 11:39:19 +0100629 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
630 * counter only on MM/CC/SS, but not on RR */
631 if (g_bssmap_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100632 /* we have just sent the first MM message, increment the counter */
633 var integer idx := f_idx_by_comp(vc_conn);
634 ConnectionTable[idx].n_sd[0] := 1;
635 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
636 }
637
Harald Welteb3414b22017-11-23 18:22:10 +0100638 }
639
Harald Welte365f4ed2017-11-23 00:00:43 +0100640 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
641 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100642 /* send it to dispatcher */
643 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100644 }
645
Harald Welte0b476062018-01-21 19:07:32 +0100646 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
Harald Welte9dadc522018-02-06 13:56:04 +0100647 var integer idx := f_idx_by_comp(vc_conn);
Harald Welte0b476062018-01-21 19:07:32 +0100648 /* convert from decoded DTAP to encoded DTAP */
649 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
Harald Welte9dadc522018-02-06 13:56:04 +0100650 /* patch correct L3 send sequence number N(SD) into l3_enc */
651 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
Harald Welte0b476062018-01-21 19:07:32 +0100652 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
Harald Welte9dadc522018-02-06 13:56:04 +0100653 BSSAP.send(ts_BSSAP_DATA_req(ConnectionTable[idx].sccp_conn_id, bssap));
Harald Welte0b476062018-01-21 19:07:32 +0100654 }
655
656 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
657 var integer conn_id := f_conn_id_by_comp(vc_conn);
658 /* convert from decoded DTAP to encoded DTAP */
659 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
660 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
661 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
662 }
663
664
Harald Weltec82eef42017-11-24 20:40:12 +0100665 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
666 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
667 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
668 * is printed as hex string, e.g. a@mgw for CIC 10 */
669
670 /* CLIENT -> MGCP */
671 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
672 /* MGCP request from Handler (we're MSC) */
673 /* store the transaction ID we've seen */
674 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
675 /* simply forward any MGCP from the client to the port */
676 MGCP.send(mgcp_req);
677 }
678 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
679 /* MGCP response from Handler (we're BSC/MGW) */
680 /* simply forward any MGCP from the client to the port */
681 MGCP.send(mgcp_resp);
682 }
683
684 /* MGCP -> CLIENT */
685 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
686 /* MGCP request from network side (we're BSC/MGW) */
687 /* Extract CIC from local part of endpoint name */
688 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100689 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
690 /* ignore RSIP for unknown CIC */
691 } else {
692 /* Resolve the vc_conn by the CIC */
693 vc_conn := f_comp_by_cic(cic);
694 CLIENT.send(mgcp_req) to vc_conn;
695 }
Harald Weltec82eef42017-11-24 20:40:12 +0100696 }
697 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
698 /* MGCP response from network side (we're MSC) */
699 /* Resolve the vc_conn by the transaction ID */
700 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
701 CLIENT.send(mgcp_resp) to vc_conn;
702 }
703
Harald Welte624f9632017-12-16 19:26:04 +0100704
705 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
706 f_create_expect(l3_info, vc_hdlr);
707 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
708 }
709
Harald Welte17d21152018-01-27 00:47:11 +0100710 [] PROC.getcall(BSSMAPEM_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
711 f_create_imsi(imsi, tmsi, vc_hdlr);
712 PROC.reply(BSSMAPEM_register_imsi:{imsi, tmsi, vc_hdlr});
713 }
714
715
Harald Welte365f4ed2017-11-23 00:00:43 +0100716 }
717 }
718}
719
Harald Weltec82eef42017-11-24 20:40:12 +0100720private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100721 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100722 return hex2int(str2hex(local_part));
723
724}
Harald Welte365f4ed2017-11-23 00:00:43 +0100725
Harald Welte624f9632017-12-16 19:26:04 +0100726/***********************************************************************
727 * "Expect" Handling (mapping for expected incoming SCCP connections)
728 ***********************************************************************/
729
730/* data about an expected future incoming connection */
731type record ExpectData {
732 /* L3 payload based on which we can match it */
733 octetstring l3_payload optional,
734 /* component reference for this connection */
735 BSSAP_ConnHdlr vc_conn
736}
737
738/* procedure based port to register for incoming connections */
739signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
740
Harald Welte17d21152018-01-27 00:47:11 +0100741/* procedure based port to register for incoming IMSI/TMSI */
742signature BSSMAPEM_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_ConnHdlr hdlr);
743
Harald Welte624f9632017-12-16 19:26:04 +0100744type port BSSMAPEM_PROC_PT procedure {
Harald Welte17d21152018-01-27 00:47:11 +0100745 inout BSSMAPEM_register, BSSMAPEM_register_imsi;
Harald Welte624f9632017-12-16 19:26:04 +0100746} with { extension "internal" };
747
748/* CreateCallback that can be used as create_cb and will use the expectation table */
749function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
750runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
751 var BSSAP_ConnHdlr ret := null;
752 var octetstring l3_info;
753 var integer i;
754
755 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
756 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
757 return ret;
758 }
759 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
760
761 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
762 if (not ispresent(ExpectTable[i].l3_payload)) {
763 continue;
764 }
765 if (l3_info == ExpectTable[i].l3_payload) {
766 ret := ExpectTable[i].vc_conn;
767 /* release this entry to be used again */
768 ExpectTable[i].l3_payload := omit;
769 ExpectTable[i].vc_conn := null;
770 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
771 /* return the component reference */
772 return ret;
773 }
774 }
775 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
776 return ret;
777}
778
779private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
780runs on BSSMAP_Emulation_CT {
781 var integer i;
782 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
783 if (not ispresent(ExpectTable[i].l3_payload)) {
784 ExpectTable[i].l3_payload := l3;
785 ExpectTable[i].vc_conn := hdlr;
786 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
787 return;
788 }
789 }
790 setverdict(fail, "No space left in ExpectTable");
791}
792
Harald Welte17d21152018-01-27 00:47:11 +0100793private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_ConnHdlr hdlr)
794runs on BSSMAP_Emulation_CT {
795 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
796 if (not ispresent(ImsiTable[i].imsi)) {
797 ImsiTable[i].imsi := imsi;
798 ImsiTable[i].tmsi := tmsi;
799 ImsiTable[i].comp_ref := hdlr;
800 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
801 return;
802 }
803 }
804 setverdict(fail, "No space left in ImsiTable");
805 self.stop;
806}
807
808
Daniel Willmannd47106b2018-01-17 12:20:56 +0100809private function f_expect_table_init()
810runs on BSSMAP_Emulation_CT {
811 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
812 ExpectTable[i].l3_payload := omit;
813 }
814}
Harald Welte624f9632017-12-16 19:26:04 +0100815
Harald Welte17d21152018-01-27 00:47:11 +0100816/* helper function for clients to register their IMSI/TMSI */
817function f_bssmap_register_imsi(hexstring imsi, OCT4 tmsi)
818runs on BSSAP_ConnHdlr {
819 BSSAP_PROC.call(BSSMAPEM_register_imsi:{imsi, tmsi, self}) {
820 [] BSSAP_PROC.getreply(BSSMAPEM_register_imsi:{?,?,?}) {};
821 }
822}
823
824
Harald Welte365f4ed2017-11-23 00:00:43 +0100825}