blob: 5f0409f7d14e9843af98f7f49d21623dcc2f33b0 [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 Welteb3414b22017-11-23 18:22:10 +010034import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010035import from SCCPasp_Types all;
36import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010037import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010038import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +010039import from MGCP_Types all;
40import from MGCP_Templates all;
41import from IPA_Emulation all;
Harald Welte0b476062018-01-21 19:07:32 +010042import from MobileL3_Types all;
Harald Welte365f4ed2017-11-23 00:00:43 +010043
44/* General "base class" component definition, of which specific implementations
45 * derive themselves by means of the "extends" feature */
46type component BSSAP_ConnHdlr {
47 /* port towards MSC Emulator core / SCCP connection dispatchar */
48 port BSSAP_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010049 /* procedure based port to register for incoming connections */
50 port BSSMAPEM_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010051}
52
53/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
54type enumerated BSSAP_Conn_Prim {
55 /* SCCP tell us that connection was released */
56 MSC_CONN_PRIM_DISC_IND,
57 /* we tell SCCP to release connection */
Harald Welte71b69332018-01-21 20:43:53 +010058 MSC_CONN_PRIM_DISC_REQ,
59 /* Connection confirmed indication */
60 MSC_CONN_PRIM_CONF_IND
Harald Welte365f4ed2017-11-23 00:00:43 +010061}
62
Harald Welteb3414b22017-11-23 18:22:10 +010063type record BSSAP_Conn_Req {
64 SCCP_PAR_Address addr_peer,
65 SCCP_PAR_Address addr_own,
66 PDU_BSSAP bssap
67}
68
Harald Welte0b476062018-01-21 19:07:32 +010069/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
70type record PDU_DTAP_MO {
71 OCT1 dlci optional,
72 PDU_ML3_MS_NW dtap
73}
74
75/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
76type record PDU_DTAP_MT {
77 OCT1 dlci optional,
78 PDU_ML3_NW_MS dtap
79}
80
81template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
82 dlci := dlci,
83 dtap := dtap
84}
85
86template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O) := {
87 dlci := dlci,
88 dtap := dtap
89}
90
91template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
92 dlci := dlci,
93 dtap := dtap
94}
95
96template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
97 dlci := dlci,
98 dtap := dtap
99}
100
101
Harald Welte365f4ed2017-11-23 00:00:43 +0100102/* port between individual per-connection components and this dispatcher */
103type port BSSAP_Conn_PT message {
Harald Welte0b476062018-01-21 19:07:32 +0100104 inout PDU_BSSAP, PDU_DTAP_MO, PDU_DTAP_MT, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +0100105} with { extension "internal" };
106
107
108/* represents a single BSSAP connection over SCCP */
109type record ConnectionData {
110 /* reference to the instance of the per-connection component */
111 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100112 integer sccp_conn_id,
113 /* most recent MGCP transaction ID (Used on MSC side) */
114 MgcpTransId mgcp_trans_id optional,
115 /* CIC that has been used for voice of this channel (BSC side) */
116 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +0100117}
118
Harald Welte17d21152018-01-27 00:47:11 +0100119type record ImsiMapping {
120 BSSAP_ConnHdlr comp_ref,
121 hexstring imsi optional,
122 OCT4 tmsi
123}
124
Harald Welte365f4ed2017-11-23 00:00:43 +0100125type component BSSMAP_Emulation_CT {
126 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100127 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100128 /* BSSAP port to the per-connection clients */
129 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +0100130 /* MGCP port */
131 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100132
133 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
134 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100135
Harald Welte624f9632017-12-16 19:26:04 +0100136 /* pending expected incoming connections */
137 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100138
139 /* tables for mapping inbound unitdata (like paging) */
140 var ImsiMapping ImsiTable[16];
141
Harald Welte624f9632017-12-16 19:26:04 +0100142 /* procedure based port to register for incoming connections */
143 port BSSMAPEM_PROC_PT PROC;
144
Harald Weltebe620f62017-11-25 00:23:54 +0100145 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100146 var integer g_next_e1_ts := 1;
Harald Welte0b476062018-01-21 19:07:32 +0100147 var BssmapOps g_bssmap_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100148};
149
Harald Welteb3414b22017-11-23 18:22:10 +0100150private function f_conn_id_known(integer sccp_conn_id)
151runs on BSSMAP_Emulation_CT return boolean {
152 var integer i;
153 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
154 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
155 return true;
156 }
157 }
158 return false;
159}
160
161private function f_comp_known(BSSAP_ConnHdlr client)
162runs on BSSMAP_Emulation_CT return boolean {
163 var integer i;
164 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
165 if (ConnectionTable[i].comp_ref == client) {
166 return true;
167 }
168 }
169 return false;
170}
Harald Welte365f4ed2017-11-23 00:00:43 +0100171
Harald Weltee98bb2e2017-11-29 12:09:48 +0100172private function f_cic_known(integer cic)
173runs on BSSMAP_Emulation_CT return boolean {
174 var integer i;
175 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
176 if (ConnectionTable[i].cic == cic) {
177 return true;
178 }
179 }
180 return false;
181}
182
Harald Welte365f4ed2017-11-23 00:00:43 +0100183/* resolve component reference by connection ID */
184private function f_comp_by_conn_id(integer sccp_conn_id)
185runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
186 var integer i;
187 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
188 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
189 return ConnectionTable[i].comp_ref;
190 }
191 }
192 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100193 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100194 self.stop;
195}
196
Harald Weltec82eef42017-11-24 20:40:12 +0100197/* resolve component reference by CIC */
198private function f_comp_by_mgcp_tid(MgcpTransId tid)
199runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
200 var integer i;
201 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
202 if (ConnectionTable[i].mgcp_trans_id == tid) {
203 return ConnectionTable[i].comp_ref;
204 }
205 }
206 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100207 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100208 self.stop;
209}
210
211private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
212runs on BSSMAP_Emulation_CT {
213 var integer i;
214 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
215 if (ConnectionTable[i].comp_ref == client) {
216 ConnectionTable[i].mgcp_trans_id := tid;
217 return;
218 }
219 }
220 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100221 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100222 self.stop;
223}
224
225private function f_comp_by_cic(integer cic)
226runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
227 var integer i;
228 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
229 if (ConnectionTable[i].cic == cic) {
230 return ConnectionTable[i].comp_ref;
231 }
232 }
233 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100234 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100235 self.stop;
236}
237
238private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
239runs on BSSMAP_Emulation_CT {
240 var integer i;
241 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
242 if (ConnectionTable[i].comp_ref == client) {
243 ConnectionTable[i].cic := cic;
244 return;
245 }
246 }
247 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100248 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100249}
250
Harald Welte365f4ed2017-11-23 00:00:43 +0100251/* resolve connection ID by component reference */
252private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
253runs on BSSMAP_Emulation_CT return integer {
254 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
255 if (ConnectionTable[i].comp_ref == client) {
256 return ConnectionTable[i].sccp_conn_id;
257 }
258 }
259 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100260 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100261 self.stop;
262}
263
Harald Welteb3414b22017-11-23 18:22:10 +0100264private function f_gen_conn_id()
265runs on BSSMAP_Emulation_CT return integer {
266 var integer conn_id;
267
268 do {
269 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
270 } while (f_conn_id_known(conn_id) == true);
271
272 return conn_id;
273}
274
Harald Welte365f4ed2017-11-23 00:00:43 +0100275private function f_conn_table_init()
276runs on BSSMAP_Emulation_CT {
277 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
278 ConnectionTable[i].comp_ref := null;
279 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100280 ConnectionTable[i].mgcp_trans_id := omit;
281 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100282 }
Harald Welte17d21152018-01-27 00:47:11 +0100283 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
284 ImsiTable[i].comp_ref := null;
285 ImsiTable[i].imsi := omit;
286 ImsiTable[i].tmsi := 'FFFFFFFF'O;
287 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100288}
289
290private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
291runs on BSSMAP_Emulation_CT {
292 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
293 if (ConnectionTable[i].sccp_conn_id == -1) {
294 ConnectionTable[i].comp_ref := comp_ref;
295 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100296 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100297 return;
298 }
299 }
300 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100301 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100302 self.stop;
303}
304
305private function f_conn_table_del(integer sccp_conn_id)
306runs on BSSMAP_Emulation_CT {
307 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
308 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100309 log("Deleted conn table entry ", i,
310 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100311 ConnectionTable[i].sccp_conn_id := -1;
312 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100313 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100314 }
315 }
316 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100317 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100318 self.stop;
319}
320
Harald Welte17d21152018-01-27 00:47:11 +0100321private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
322runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
323 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
324 if (ImsiTable[i].imsi == imsi or
325 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
326 return ImsiTable[i].comp_ref;
327 }
328 }
329 return null;
330}
331
Harald Welte365f4ed2017-11-23 00:00:43 +0100332/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100333private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100334runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100335 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100336
Harald Welte473676b2018-01-27 20:38:01 +0100337 if (ischosen(bssap.pdu.bssmap)) {
338 /* BSC Side: If this is an assignment command, store CIC */
339 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
340 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
341 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
342 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
343 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
344 f_comp_store_cic(client, cic);
345 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100346 }
347
Harald Welte0b476062018-01-21 19:07:32 +0100348 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
349 if (g_bssmap_ops.role_ms) {
350 /* we are the MS, so any message to us must be MT */
351 var PDU_DTAP_MT mt := {
352 dlci := bssap.dlci,
353 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
354 };
355 CLIENT.send(mt) to client;
356 } else {
357 /* we are the Network, so any message to us must be MO */
358 var PDU_DTAP_MO mo := {
359 dlci := bssap.dlci,
360 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
361 };
362 CLIENT.send(mo) to client;
363 }
364 } else {
365 CLIENT.send(bssap) to client;
366 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100367}
368
369/* call-back type, to be provided by specific implementation; called when new SCCP connection
370 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100371type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100372runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
373
374type function BssmapUnitdataCallback(PDU_BSSAP bssap)
375runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
376
Harald Welte17d21152018-01-27 00:47:11 +0100377/* handle common Unitdata such as Paging */
378private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
379runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
380 if (match(bssap, tr_BSSMAP_Paging)) {
381 var BSSAP_ConnHdlr client := null;
382 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
383 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
384 if (isvalue(client)) {
385 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
386 client);
387 CLIENT.send(bssap) to client;
388 return omit;
389 }
390 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
391 } else {
392 log("CommonBssmapUnitdataCallback: Not a paging message");
393 }
394 /* ELSE: handle in user callback */
395 return g_bssmap_ops.unitdata_cb.apply(bssap);
396}
397
Harald Welte365f4ed2017-11-23 00:00:43 +0100398type record BssmapOps {
399 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100400 BssmapUnitdataCallback unitdata_cb,
401 boolean decode_dtap,
402 boolean role_ms
Harald Welte365f4ed2017-11-23 00:00:43 +0100403}
404
Harald Weltebe620f62017-11-25 00:23:54 +0100405function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100406
Harald Weltebe620f62017-11-25 00:23:54 +0100407 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100408 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100409 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100410 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100411
412 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100413 var BSSAP_N_UNITDATA_ind ud_ind;
414 var BSSAP_N_CONNECT_ind conn_ind;
415 var BSSAP_N_CONNECT_cfm conn_cfm;
416 var BSSAP_N_DATA_ind data_ind;
417 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100418 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100419 var BSSAP_ConnHdlr vc_conn;
420 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100421 var PDU_DTAP_MO dtap_mo;
422 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100423 var MgcpCommand mgcp_req;
424 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100425 var BSSAP_ConnHdlr vc_hdlr;
426 var octetstring l3_info;
Harald Welte17d21152018-01-27 00:47:11 +0100427 var hexstring imsi;
428 var OCT4 tmsi;
Harald Welte365f4ed2017-11-23 00:00:43 +0100429
430 alt {
431 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100432 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100433 /* Connectionless Procedures like RESET */
434 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100435 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100436 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100437 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
438 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100439 }
440 }
441
442 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100443 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100444 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100445 /* store mapping between client components and SCCP connectionId */
446 f_conn_table_add(vc_conn, conn_ind.connectionId);
447 /* handle user payload */
448 f_handle_userData(vc_conn, conn_ind.userData);
449 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100450 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100451 }
452
453 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100454 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100455 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100456 if (ispresent(data_ind.userData)) {
457 f_handle_userData(vc_conn, data_ind.userData);
458 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100459 }
460
461 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100462 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100463 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100464 if (ispresent(disc_ind.userData)) {
465 f_handle_userData(vc_conn, disc_ind.userData);
466 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100467 /* notify client about termination */
468 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
469 CLIENT.send(prim) to vc_conn;
470 f_conn_table_del(disc_ind.connectionId);
471 /* TOOD: return confirm to other side? */
472 }
473
Harald Welteb3414b22017-11-23 18:22:10 +0100474 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100475 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100476 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
477 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
478 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100479 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100480 if (ispresent(conn_cfm.userData)) {
481 f_handle_userData(vc_conn, conn_cfm.userData);
482 }
Harald Welteb3414b22017-11-23 18:22:10 +0100483 }
484
Harald Welte365f4ed2017-11-23 00:00:43 +0100485 /* Disconnect request client -> SCCP */
486 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
487 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100488 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100489 f_conn_table_del(conn_id);
490 }
491
492 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100493 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
494 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100495 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100496
497 if (f_comp_known(vc_conn) == false) {
498 /* unknown client, create new connection */
499 conn_id := f_gen_conn_id();
500
501 /* store mapping between client components and SCCP connectionId */
502 f_conn_table_add(vc_conn, conn_id);
503
Harald Welte004f5fb2017-12-16 17:54:40 +0100504 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
505 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100506 } else {
507 /* known client, send via existing connection */
508 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100509 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100510 }
511
512 }
513
Harald Welte365f4ed2017-11-23 00:00:43 +0100514 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
515 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100516 /* send it to dispatcher */
517 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100518 }
519
Harald Welte0b476062018-01-21 19:07:32 +0100520 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
521 var integer conn_id := f_conn_id_by_comp(vc_conn);
522 /* convert from decoded DTAP to encoded DTAP */
523 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
524 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
525 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
526 }
527
528 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
529 var integer conn_id := f_conn_id_by_comp(vc_conn);
530 /* convert from decoded DTAP to encoded DTAP */
531 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
532 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
533 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
534 }
535
536
Harald Weltec82eef42017-11-24 20:40:12 +0100537 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
538 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
539 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
540 * is printed as hex string, e.g. a@mgw for CIC 10 */
541
542 /* CLIENT -> MGCP */
543 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
544 /* MGCP request from Handler (we're MSC) */
545 /* store the transaction ID we've seen */
546 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
547 /* simply forward any MGCP from the client to the port */
548 MGCP.send(mgcp_req);
549 }
550 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
551 /* MGCP response from Handler (we're BSC/MGW) */
552 /* simply forward any MGCP from the client to the port */
553 MGCP.send(mgcp_resp);
554 }
555
556 /* MGCP -> CLIENT */
557 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
558 /* MGCP request from network side (we're BSC/MGW) */
559 /* Extract CIC from local part of endpoint name */
560 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100561 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
562 /* ignore RSIP for unknown CIC */
563 } else {
564 /* Resolve the vc_conn by the CIC */
565 vc_conn := f_comp_by_cic(cic);
566 CLIENT.send(mgcp_req) to vc_conn;
567 }
Harald Weltec82eef42017-11-24 20:40:12 +0100568 }
569 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
570 /* MGCP response from network side (we're MSC) */
571 /* Resolve the vc_conn by the transaction ID */
572 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
573 CLIENT.send(mgcp_resp) to vc_conn;
574 }
575
Harald Welte624f9632017-12-16 19:26:04 +0100576
577 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
578 f_create_expect(l3_info, vc_hdlr);
579 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
580 }
581
Harald Welte17d21152018-01-27 00:47:11 +0100582 [] PROC.getcall(BSSMAPEM_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
583 f_create_imsi(imsi, tmsi, vc_hdlr);
584 PROC.reply(BSSMAPEM_register_imsi:{imsi, tmsi, vc_hdlr});
585 }
586
587
Harald Welte365f4ed2017-11-23 00:00:43 +0100588 }
589 }
590}
591
Harald Weltec82eef42017-11-24 20:40:12 +0100592private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100593 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100594 return hex2int(str2hex(local_part));
595
596}
Harald Welte365f4ed2017-11-23 00:00:43 +0100597
Harald Welte624f9632017-12-16 19:26:04 +0100598/***********************************************************************
599 * "Expect" Handling (mapping for expected incoming SCCP connections)
600 ***********************************************************************/
601
602/* data about an expected future incoming connection */
603type record ExpectData {
604 /* L3 payload based on which we can match it */
605 octetstring l3_payload optional,
606 /* component reference for this connection */
607 BSSAP_ConnHdlr vc_conn
608}
609
610/* procedure based port to register for incoming connections */
611signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
612
Harald Welte17d21152018-01-27 00:47:11 +0100613/* procedure based port to register for incoming IMSI/TMSI */
614signature BSSMAPEM_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_ConnHdlr hdlr);
615
Harald Welte624f9632017-12-16 19:26:04 +0100616type port BSSMAPEM_PROC_PT procedure {
Harald Welte17d21152018-01-27 00:47:11 +0100617 inout BSSMAPEM_register, BSSMAPEM_register_imsi;
Harald Welte624f9632017-12-16 19:26:04 +0100618} with { extension "internal" };
619
620/* CreateCallback that can be used as create_cb and will use the expectation table */
621function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
622runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
623 var BSSAP_ConnHdlr ret := null;
624 var octetstring l3_info;
625 var integer i;
626
627 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
628 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
629 return ret;
630 }
631 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
632
633 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
634 if (not ispresent(ExpectTable[i].l3_payload)) {
635 continue;
636 }
637 if (l3_info == ExpectTable[i].l3_payload) {
638 ret := ExpectTable[i].vc_conn;
639 /* release this entry to be used again */
640 ExpectTable[i].l3_payload := omit;
641 ExpectTable[i].vc_conn := null;
642 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
643 /* return the component reference */
644 return ret;
645 }
646 }
647 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
648 return ret;
649}
650
651private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
652runs on BSSMAP_Emulation_CT {
653 var integer i;
654 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
655 if (not ispresent(ExpectTable[i].l3_payload)) {
656 ExpectTable[i].l3_payload := l3;
657 ExpectTable[i].vc_conn := hdlr;
658 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
659 return;
660 }
661 }
662 setverdict(fail, "No space left in ExpectTable");
663}
664
Harald Welte17d21152018-01-27 00:47:11 +0100665private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_ConnHdlr hdlr)
666runs on BSSMAP_Emulation_CT {
667 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
668 if (not ispresent(ImsiTable[i].imsi)) {
669 ImsiTable[i].imsi := imsi;
670 ImsiTable[i].tmsi := tmsi;
671 ImsiTable[i].comp_ref := hdlr;
672 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
673 return;
674 }
675 }
676 setverdict(fail, "No space left in ImsiTable");
677 self.stop;
678}
679
680
Daniel Willmannd47106b2018-01-17 12:20:56 +0100681private function f_expect_table_init()
682runs on BSSMAP_Emulation_CT {
683 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
684 ExpectTable[i].l3_payload := omit;
685 }
686}
Harald Welte624f9632017-12-16 19:26:04 +0100687
Harald Welte17d21152018-01-27 00:47:11 +0100688/* helper function for clients to register their IMSI/TMSI */
689function f_bssmap_register_imsi(hexstring imsi, OCT4 tmsi)
690runs on BSSAP_ConnHdlr {
691 BSSAP_PROC.call(BSSMAPEM_register_imsi:{imsi, tmsi, self}) {
692 [] BSSAP_PROC.getreply(BSSMAPEM_register_imsi:{?,?,?}) {};
693 }
694}
695
696
Harald Welte365f4ed2017-11-23 00:00:43 +0100697}