blob: 4aacdf27ba65955020d27f0630b609fcabc622d3 [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 Welte0b476062018-01-21 19:07:32 +0100105 inout PDU_BSSAP, PDU_DTAP_MO, PDU_DTAP_MT, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +0100106} with { extension "internal" };
107
108
109/* represents a single BSSAP connection over SCCP */
110type record ConnectionData {
111 /* reference to the instance of the per-connection component */
112 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100113 integer sccp_conn_id,
114 /* most recent MGCP transaction ID (Used on MSC side) */
115 MgcpTransId mgcp_trans_id optional,
116 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100117 integer cic optional,
118 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
119 uint2_t n_sd[4]
Harald Welte365f4ed2017-11-23 00:00:43 +0100120}
121
Harald Welte17d21152018-01-27 00:47:11 +0100122type record ImsiMapping {
123 BSSAP_ConnHdlr comp_ref,
124 hexstring imsi optional,
125 OCT4 tmsi
126}
127
Harald Welte365f4ed2017-11-23 00:00:43 +0100128type component BSSMAP_Emulation_CT {
129 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100130 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100131 /* BSSAP port to the per-connection clients */
132 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +0100133 /* MGCP port */
134 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100135
136 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
137 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100138
Harald Welte624f9632017-12-16 19:26:04 +0100139 /* pending expected incoming connections */
140 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100141
142 /* tables for mapping inbound unitdata (like paging) */
143 var ImsiMapping ImsiTable[16];
144
Harald Welte624f9632017-12-16 19:26:04 +0100145 /* procedure based port to register for incoming connections */
146 port BSSMAPEM_PROC_PT PROC;
147
Harald Weltebe620f62017-11-25 00:23:54 +0100148 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100149 var integer g_next_e1_ts := 1;
Harald Welte0b476062018-01-21 19:07:32 +0100150 var BssmapOps g_bssmap_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100151};
152
Harald Welteb3414b22017-11-23 18:22:10 +0100153private function f_conn_id_known(integer sccp_conn_id)
154runs on BSSMAP_Emulation_CT return boolean {
155 var integer i;
156 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
157 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
158 return true;
159 }
160 }
161 return false;
162}
163
164private function f_comp_known(BSSAP_ConnHdlr client)
165runs on BSSMAP_Emulation_CT return boolean {
166 var integer i;
167 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
168 if (ConnectionTable[i].comp_ref == client) {
169 return true;
170 }
171 }
172 return false;
173}
Harald Welte365f4ed2017-11-23 00:00:43 +0100174
Harald Weltee98bb2e2017-11-29 12:09:48 +0100175private function f_cic_known(integer cic)
176runs on BSSMAP_Emulation_CT return boolean {
177 var integer i;
178 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
179 if (ConnectionTable[i].cic == cic) {
180 return true;
181 }
182 }
183 return false;
184}
185
Harald Welte365f4ed2017-11-23 00:00:43 +0100186/* resolve component reference by connection ID */
187private function f_comp_by_conn_id(integer sccp_conn_id)
188runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
189 var integer i;
190 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
191 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
192 return ConnectionTable[i].comp_ref;
193 }
194 }
195 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100196 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100197 self.stop;
198}
199
Harald Weltec82eef42017-11-24 20:40:12 +0100200/* resolve component reference by CIC */
201private function f_comp_by_mgcp_tid(MgcpTransId tid)
202runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
203 var integer i;
204 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
205 if (ConnectionTable[i].mgcp_trans_id == tid) {
206 return ConnectionTable[i].comp_ref;
207 }
208 }
209 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100210 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100211 self.stop;
212}
213
214private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
215runs on BSSMAP_Emulation_CT {
216 var integer i;
217 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
218 if (ConnectionTable[i].comp_ref == client) {
219 ConnectionTable[i].mgcp_trans_id := tid;
220 return;
221 }
222 }
223 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100224 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100225 self.stop;
226}
227
228private function f_comp_by_cic(integer cic)
229runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
230 var integer i;
231 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
232 if (ConnectionTable[i].cic == cic) {
233 return ConnectionTable[i].comp_ref;
234 }
235 }
236 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100237 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100238 self.stop;
239}
240
241private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
242runs on BSSMAP_Emulation_CT {
243 var integer i;
244 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
245 if (ConnectionTable[i].comp_ref == client) {
246 ConnectionTable[i].cic := cic;
247 return;
248 }
249 }
250 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100251 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100252}
253
Harald Welte365f4ed2017-11-23 00:00:43 +0100254/* resolve connection ID by component reference */
255private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
256runs on BSSMAP_Emulation_CT return integer {
257 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
258 if (ConnectionTable[i].comp_ref == client) {
259 return ConnectionTable[i].sccp_conn_id;
260 }
261 }
262 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100263 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100264 self.stop;
265}
266
Harald Welte9dadc522018-02-06 13:56:04 +0100267/* resolve ConnectionTable index component reference */
268private function f_idx_by_comp(BSSAP_ConnHdlr client)
269runs on BSSMAP_Emulation_CT return integer {
270 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
271 if (ConnectionTable[i].comp_ref == client) {
272 return i;
273 }
274 }
275 log("BSSMAP Connection table not found by component ", client);
276 setverdict(fail);
277 self.stop;
278}
279
Harald Welteb3414b22017-11-23 18:22:10 +0100280private function f_gen_conn_id()
281runs on BSSMAP_Emulation_CT return integer {
282 var integer conn_id;
283
284 do {
285 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
286 } while (f_conn_id_known(conn_id) == true);
287
288 return conn_id;
289}
290
Harald Welte365f4ed2017-11-23 00:00:43 +0100291private function f_conn_table_init()
292runs on BSSMAP_Emulation_CT {
293 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
294 ConnectionTable[i].comp_ref := null;
295 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100296 ConnectionTable[i].mgcp_trans_id := omit;
297 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100298 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 }
Harald Welte17d21152018-01-27 00:47:11 +0100300 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
301 ImsiTable[i].comp_ref := null;
302 ImsiTable[i].imsi := omit;
303 ImsiTable[i].tmsi := 'FFFFFFFF'O;
304 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100305}
306
307private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
308runs on BSSMAP_Emulation_CT {
309 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
310 if (ConnectionTable[i].sccp_conn_id == -1) {
311 ConnectionTable[i].comp_ref := comp_ref;
312 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100313 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100314 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100315 return;
316 }
317 }
318 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100319 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100320 self.stop;
321}
322
323private function f_conn_table_del(integer sccp_conn_id)
324runs on BSSMAP_Emulation_CT {
325 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
326 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100327 log("Deleted conn table entry ", i,
328 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100329 ConnectionTable[i].sccp_conn_id := -1;
330 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100331 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100332 }
333 }
334 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100335 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100336 self.stop;
337}
338
Harald Welte17d21152018-01-27 00:47:11 +0100339private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
340runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
341 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
342 if (ImsiTable[i].imsi == imsi or
343 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
344 return ImsiTable[i].comp_ref;
345 }
346 }
347 return null;
348}
349
Harald Welte365f4ed2017-11-23 00:00:43 +0100350/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100351private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100352runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100353 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100354
Harald Welte473676b2018-01-27 20:38:01 +0100355 if (ischosen(bssap.pdu.bssmap)) {
356 /* BSC Side: If this is an assignment command, store CIC */
357 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
358 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
359 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
360 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
361 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
362 f_comp_store_cic(client, cic);
363 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100364 }
365
Harald Welte0b476062018-01-21 19:07:32 +0100366 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
367 if (g_bssmap_ops.role_ms) {
368 /* we are the MS, so any message to us must be MT */
369 var PDU_DTAP_MT mt := {
370 dlci := bssap.dlci,
371 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
372 };
373 CLIENT.send(mt) to client;
374 } else {
375 /* we are the Network, so any message to us must be MO */
376 var PDU_DTAP_MO mo := {
377 dlci := bssap.dlci,
378 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
379 };
380 CLIENT.send(mo) to client;
381 }
382 } else {
383 CLIENT.send(bssap) to client;
384 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100385}
386
387/* call-back type, to be provided by specific implementation; called when new SCCP connection
388 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100389type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100390runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
391
392type function BssmapUnitdataCallback(PDU_BSSAP bssap)
393runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
394
Harald Welte17d21152018-01-27 00:47:11 +0100395/* handle common Unitdata such as Paging */
396private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
397runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
398 if (match(bssap, tr_BSSMAP_Paging)) {
399 var BSSAP_ConnHdlr client := null;
400 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
401 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
402 if (isvalue(client)) {
403 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
404 client);
405 CLIENT.send(bssap) to client;
406 return omit;
407 }
408 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
409 } else {
410 log("CommonBssmapUnitdataCallback: Not a paging message");
411 }
412 /* ELSE: handle in user callback */
413 return g_bssmap_ops.unitdata_cb.apply(bssap);
414}
415
Harald Welte365f4ed2017-11-23 00:00:43 +0100416type record BssmapOps {
417 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100418 BssmapUnitdataCallback unitdata_cb,
419 boolean decode_dtap,
420 boolean role_ms
Harald Welte365f4ed2017-11-23 00:00:43 +0100421}
422
Harald Welte9dadc522018-02-06 13:56:04 +0100423template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
424
425/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
426function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
427 var uint2_t seq_nr;
428 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
429 seq_nr := cd.n_sd[0];
430 cd.n_sd[0] := (cd.n_sd[0] + 1) mod 4;
431 } else if (ischosen(dtap.msgs.gcc)) {
432 seq_nr := cd.n_sd[1];
433 cd.n_sd[1] := (cd.n_sd[1] + 1) mod 2;
434 } else if (ischosen(dtap.msgs.bcc)) {
435 seq_nr := cd.n_sd[2];
436 cd.n_sd[2] := (cd.n_sd[2] + 1) mod 2;
437 } else if (ischosen(dtap.msgs.loc)) {
438 seq_nr := cd.n_sd[3];
439 cd.n_sd[3] := (cd.n_sd[3] + 1) mod 2;
440 } else {
441 /* no sequence number to patch */
442 return;
443 }
444 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
445 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
446 log("patched enc_l3: ", enc_l3);
447}
448
Harald Welte49518bf2018-02-10 11:39:19 +0100449private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
450 var template octetstring l3 := f_bssap_extract_l3(bssap);
451 if (not isvalue(l3)) {
452 return false;
453 }
454 var octetstring l3v := valueof(l3);
455 /* lower 4 bits of first octet are protocol discriminator */
456 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
457 return true;
458 }
459 return false;
460}
461
Harald Weltebe620f62017-11-25 00:23:54 +0100462function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100463
Harald Weltebe620f62017-11-25 00:23:54 +0100464 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100465 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100466 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100467 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100468
469 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100470 var BSSAP_N_UNITDATA_ind ud_ind;
471 var BSSAP_N_CONNECT_ind conn_ind;
472 var BSSAP_N_CONNECT_cfm conn_cfm;
473 var BSSAP_N_DATA_ind data_ind;
474 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100475 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100476 var BSSAP_ConnHdlr vc_conn;
477 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100478 var PDU_DTAP_MO dtap_mo;
479 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100480 var MgcpCommand mgcp_req;
481 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100482 var BSSAP_ConnHdlr vc_hdlr;
483 var octetstring l3_info;
Harald Welte17d21152018-01-27 00:47:11 +0100484 var hexstring imsi;
485 var OCT4 tmsi;
Harald Welte365f4ed2017-11-23 00:00:43 +0100486
487 alt {
488 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100489 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100490 /* Connectionless Procedures like RESET */
491 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100492 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100493 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100494 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
495 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100496 }
497 }
498
499 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100500 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100501 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100502 /* store mapping between client components and SCCP connectionId */
503 f_conn_table_add(vc_conn, conn_ind.connectionId);
504 /* handle user payload */
505 f_handle_userData(vc_conn, conn_ind.userData);
506 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100507 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100508 }
509
510 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100511 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100512 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100513 if (ispresent(data_ind.userData)) {
514 f_handle_userData(vc_conn, data_ind.userData);
515 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100516 }
517
518 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100519 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100520 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100521 if (ispresent(disc_ind.userData)) {
522 f_handle_userData(vc_conn, disc_ind.userData);
523 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100524 /* notify client about termination */
525 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
526 CLIENT.send(prim) to vc_conn;
527 f_conn_table_del(disc_ind.connectionId);
528 /* TOOD: return confirm to other side? */
529 }
530
Harald Welteb3414b22017-11-23 18:22:10 +0100531 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100532 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100533 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
534 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
535 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100536 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100537 if (ispresent(conn_cfm.userData)) {
538 f_handle_userData(vc_conn, conn_cfm.userData);
539 }
Harald Welteb3414b22017-11-23 18:22:10 +0100540 }
541
Harald Welte365f4ed2017-11-23 00:00:43 +0100542 /* Disconnect request client -> SCCP */
543 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
544 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100545 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100546 f_conn_table_del(conn_id);
547 }
548
549 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100550 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
551 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100552 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100553
554 if (f_comp_known(vc_conn) == false) {
555 /* unknown client, create new connection */
556 conn_id := f_gen_conn_id();
557
558 /* store mapping between client components and SCCP connectionId */
559 f_conn_table_add(vc_conn, conn_id);
560
Harald Welte004f5fb2017-12-16 17:54:40 +0100561 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
562 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100563 } else {
564 /* known client, send via existing connection */
565 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100566 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100567 }
568
Harald Welte49518bf2018-02-10 11:39:19 +0100569 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
570 * counter only on MM/CC/SS, but not on RR */
571 if (g_bssmap_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100572 /* we have just sent the first MM message, increment the counter */
573 var integer idx := f_idx_by_comp(vc_conn);
574 ConnectionTable[idx].n_sd[0] := 1;
575 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
576 }
577
Harald Welteb3414b22017-11-23 18:22:10 +0100578 }
579
Harald Welte365f4ed2017-11-23 00:00:43 +0100580 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
581 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100582 /* send it to dispatcher */
583 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100584 }
585
Harald Welte0b476062018-01-21 19:07:32 +0100586 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
Harald Welte9dadc522018-02-06 13:56:04 +0100587 var integer idx := f_idx_by_comp(vc_conn);
Harald Welte0b476062018-01-21 19:07:32 +0100588 /* convert from decoded DTAP to encoded DTAP */
589 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
Harald Welte9dadc522018-02-06 13:56:04 +0100590 /* patch correct L3 send sequence number N(SD) into l3_enc */
591 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
Harald Welte0b476062018-01-21 19:07:32 +0100592 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
Harald Welte9dadc522018-02-06 13:56:04 +0100593 BSSAP.send(ts_BSSAP_DATA_req(ConnectionTable[idx].sccp_conn_id, bssap));
Harald Welte0b476062018-01-21 19:07:32 +0100594 }
595
596 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
597 var integer conn_id := f_conn_id_by_comp(vc_conn);
598 /* convert from decoded DTAP to encoded DTAP */
599 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
600 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
601 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
602 }
603
604
Harald Weltec82eef42017-11-24 20:40:12 +0100605 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
606 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
607 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
608 * is printed as hex string, e.g. a@mgw for CIC 10 */
609
610 /* CLIENT -> MGCP */
611 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
612 /* MGCP request from Handler (we're MSC) */
613 /* store the transaction ID we've seen */
614 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
615 /* simply forward any MGCP from the client to the port */
616 MGCP.send(mgcp_req);
617 }
618 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
619 /* MGCP response from Handler (we're BSC/MGW) */
620 /* simply forward any MGCP from the client to the port */
621 MGCP.send(mgcp_resp);
622 }
623
624 /* MGCP -> CLIENT */
625 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
626 /* MGCP request from network side (we're BSC/MGW) */
627 /* Extract CIC from local part of endpoint name */
628 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100629 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
630 /* ignore RSIP for unknown CIC */
631 } else {
632 /* Resolve the vc_conn by the CIC */
633 vc_conn := f_comp_by_cic(cic);
634 CLIENT.send(mgcp_req) to vc_conn;
635 }
Harald Weltec82eef42017-11-24 20:40:12 +0100636 }
637 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
638 /* MGCP response from network side (we're MSC) */
639 /* Resolve the vc_conn by the transaction ID */
640 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
641 CLIENT.send(mgcp_resp) to vc_conn;
642 }
643
Harald Welte624f9632017-12-16 19:26:04 +0100644
645 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
646 f_create_expect(l3_info, vc_hdlr);
647 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
648 }
649
Harald Welte17d21152018-01-27 00:47:11 +0100650 [] PROC.getcall(BSSMAPEM_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
651 f_create_imsi(imsi, tmsi, vc_hdlr);
652 PROC.reply(BSSMAPEM_register_imsi:{imsi, tmsi, vc_hdlr});
653 }
654
655
Harald Welte365f4ed2017-11-23 00:00:43 +0100656 }
657 }
658}
659
Harald Weltec82eef42017-11-24 20:40:12 +0100660private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100661 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100662 return hex2int(str2hex(local_part));
663
664}
Harald Welte365f4ed2017-11-23 00:00:43 +0100665
Harald Welte624f9632017-12-16 19:26:04 +0100666/***********************************************************************
667 * "Expect" Handling (mapping for expected incoming SCCP connections)
668 ***********************************************************************/
669
670/* data about an expected future incoming connection */
671type record ExpectData {
672 /* L3 payload based on which we can match it */
673 octetstring l3_payload optional,
674 /* component reference for this connection */
675 BSSAP_ConnHdlr vc_conn
676}
677
678/* procedure based port to register for incoming connections */
679signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
680
Harald Welte17d21152018-01-27 00:47:11 +0100681/* procedure based port to register for incoming IMSI/TMSI */
682signature BSSMAPEM_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_ConnHdlr hdlr);
683
Harald Welte624f9632017-12-16 19:26:04 +0100684type port BSSMAPEM_PROC_PT procedure {
Harald Welte17d21152018-01-27 00:47:11 +0100685 inout BSSMAPEM_register, BSSMAPEM_register_imsi;
Harald Welte624f9632017-12-16 19:26:04 +0100686} with { extension "internal" };
687
688/* CreateCallback that can be used as create_cb and will use the expectation table */
689function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
690runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
691 var BSSAP_ConnHdlr ret := null;
692 var octetstring l3_info;
693 var integer i;
694
695 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
696 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
697 return ret;
698 }
699 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
700
701 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
702 if (not ispresent(ExpectTable[i].l3_payload)) {
703 continue;
704 }
705 if (l3_info == ExpectTable[i].l3_payload) {
706 ret := ExpectTable[i].vc_conn;
707 /* release this entry to be used again */
708 ExpectTable[i].l3_payload := omit;
709 ExpectTable[i].vc_conn := null;
710 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
711 /* return the component reference */
712 return ret;
713 }
714 }
715 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
716 return ret;
717}
718
719private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
720runs on BSSMAP_Emulation_CT {
721 var integer i;
722 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
723 if (not ispresent(ExpectTable[i].l3_payload)) {
724 ExpectTable[i].l3_payload := l3;
725 ExpectTable[i].vc_conn := hdlr;
726 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
727 return;
728 }
729 }
730 setverdict(fail, "No space left in ExpectTable");
731}
732
Harald Welte17d21152018-01-27 00:47:11 +0100733private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_ConnHdlr hdlr)
734runs on BSSMAP_Emulation_CT {
735 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
736 if (not ispresent(ImsiTable[i].imsi)) {
737 ImsiTable[i].imsi := imsi;
738 ImsiTable[i].tmsi := tmsi;
739 ImsiTable[i].comp_ref := hdlr;
740 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
741 return;
742 }
743 }
744 setverdict(fail, "No space left in ImsiTable");
745 self.stop;
746}
747
748
Daniel Willmannd47106b2018-01-17 12:20:56 +0100749private function f_expect_table_init()
750runs on BSSMAP_Emulation_CT {
751 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
752 ExpectTable[i].l3_payload := omit;
753 }
754}
Harald Welte624f9632017-12-16 19:26:04 +0100755
Harald Welte17d21152018-01-27 00:47:11 +0100756/* helper function for clients to register their IMSI/TMSI */
757function f_bssmap_register_imsi(hexstring imsi, OCT4 tmsi)
758runs on BSSAP_ConnHdlr {
759 BSSAP_PROC.call(BSSMAPEM_register_imsi:{imsi, tmsi, self}) {
760 [] BSSAP_PROC.getreply(BSSMAPEM_register_imsi:{?,?,?}) {};
761 }
762}
763
764
Harald Welte365f4ed2017-11-23 00:00:43 +0100765}