blob: ca5afb92345b2f8be617e3aefd53271431e91381 [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
337 /* BSC Side: If this is an assignment command, store CIC */
338 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
339 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
340 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
341 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
342 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
343 f_comp_store_cic(client, cic);
344 }
345
Harald Welte0b476062018-01-21 19:07:32 +0100346 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
347 if (g_bssmap_ops.role_ms) {
348 /* we are the MS, so any message to us must be MT */
349 var PDU_DTAP_MT mt := {
350 dlci := bssap.dlci,
351 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
352 };
353 CLIENT.send(mt) to client;
354 } else {
355 /* we are the Network, so any message to us must be MO */
356 var PDU_DTAP_MO mo := {
357 dlci := bssap.dlci,
358 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
359 };
360 CLIENT.send(mo) to client;
361 }
362 } else {
363 CLIENT.send(bssap) to client;
364 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100365}
366
367/* call-back type, to be provided by specific implementation; called when new SCCP connection
368 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100369type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100370runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
371
372type function BssmapUnitdataCallback(PDU_BSSAP bssap)
373runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
374
Harald Welte17d21152018-01-27 00:47:11 +0100375/* handle common Unitdata such as Paging */
376private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
377runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
378 if (match(bssap, tr_BSSMAP_Paging)) {
379 var BSSAP_ConnHdlr client := null;
380 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
381 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
382 if (isvalue(client)) {
383 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
384 client);
385 CLIENT.send(bssap) to client;
386 return omit;
387 }
388 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
389 } else {
390 log("CommonBssmapUnitdataCallback: Not a paging message");
391 }
392 /* ELSE: handle in user callback */
393 return g_bssmap_ops.unitdata_cb.apply(bssap);
394}
395
Harald Welte365f4ed2017-11-23 00:00:43 +0100396type record BssmapOps {
397 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100398 BssmapUnitdataCallback unitdata_cb,
399 boolean decode_dtap,
400 boolean role_ms
Harald Welte365f4ed2017-11-23 00:00:43 +0100401}
402
Harald Weltebe620f62017-11-25 00:23:54 +0100403function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100404
Harald Weltebe620f62017-11-25 00:23:54 +0100405 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100406 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100407 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100408 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100409
410 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100411 var BSSAP_N_UNITDATA_ind ud_ind;
412 var BSSAP_N_CONNECT_ind conn_ind;
413 var BSSAP_N_CONNECT_cfm conn_cfm;
414 var BSSAP_N_DATA_ind data_ind;
415 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100416 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100417 var BSSAP_ConnHdlr vc_conn;
418 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100419 var PDU_DTAP_MO dtap_mo;
420 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100421 var MgcpCommand mgcp_req;
422 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100423 var BSSAP_ConnHdlr vc_hdlr;
424 var octetstring l3_info;
Harald Welte17d21152018-01-27 00:47:11 +0100425 var hexstring imsi;
426 var OCT4 tmsi;
Harald Welte365f4ed2017-11-23 00:00:43 +0100427
428 alt {
429 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100430 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100431 /* Connectionless Procedures like RESET */
432 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100433 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100434 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100435 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
436 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100437 }
438 }
439
440 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100441 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100442 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100443 /* store mapping between client components and SCCP connectionId */
444 f_conn_table_add(vc_conn, conn_ind.connectionId);
445 /* handle user payload */
446 f_handle_userData(vc_conn, conn_ind.userData);
447 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100448 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100449 }
450
451 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100452 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100453 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100454 if (ispresent(data_ind.userData)) {
455 f_handle_userData(vc_conn, data_ind.userData);
456 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100457 }
458
459 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100460 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100461 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100462 if (ispresent(disc_ind.userData)) {
463 f_handle_userData(vc_conn, disc_ind.userData);
464 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100465 /* notify client about termination */
466 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
467 CLIENT.send(prim) to vc_conn;
468 f_conn_table_del(disc_ind.connectionId);
469 /* TOOD: return confirm to other side? */
470 }
471
Harald Welteb3414b22017-11-23 18:22:10 +0100472 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100473 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100474 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
475 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
476 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100477 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100478 if (ispresent(conn_cfm.userData)) {
479 f_handle_userData(vc_conn, conn_cfm.userData);
480 }
Harald Welteb3414b22017-11-23 18:22:10 +0100481 }
482
Harald Welte365f4ed2017-11-23 00:00:43 +0100483 /* Disconnect request client -> SCCP */
484 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
485 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100486 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100487 f_conn_table_del(conn_id);
488 }
489
490 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100491 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
492 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100493 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100494
495 if (f_comp_known(vc_conn) == false) {
496 /* unknown client, create new connection */
497 conn_id := f_gen_conn_id();
498
499 /* store mapping between client components and SCCP connectionId */
500 f_conn_table_add(vc_conn, conn_id);
501
Harald Welte004f5fb2017-12-16 17:54:40 +0100502 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
503 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100504 } else {
505 /* known client, send via existing connection */
506 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100507 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100508 }
509
510 }
511
Harald Welte365f4ed2017-11-23 00:00:43 +0100512 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
513 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100514 /* send it to dispatcher */
515 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100516 }
517
Harald Welte0b476062018-01-21 19:07:32 +0100518 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
519 var integer conn_id := f_conn_id_by_comp(vc_conn);
520 /* convert from decoded DTAP to encoded DTAP */
521 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
522 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
523 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
524 }
525
526 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
527 var integer conn_id := f_conn_id_by_comp(vc_conn);
528 /* convert from decoded DTAP to encoded DTAP */
529 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
530 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
531 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
532 }
533
534
Harald Weltec82eef42017-11-24 20:40:12 +0100535 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
536 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
537 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
538 * is printed as hex string, e.g. a@mgw for CIC 10 */
539
540 /* CLIENT -> MGCP */
541 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
542 /* MGCP request from Handler (we're MSC) */
543 /* store the transaction ID we've seen */
544 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
545 /* simply forward any MGCP from the client to the port */
546 MGCP.send(mgcp_req);
547 }
548 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
549 /* MGCP response from Handler (we're BSC/MGW) */
550 /* simply forward any MGCP from the client to the port */
551 MGCP.send(mgcp_resp);
552 }
553
554 /* MGCP -> CLIENT */
555 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
556 /* MGCP request from network side (we're BSC/MGW) */
557 /* Extract CIC from local part of endpoint name */
558 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100559 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
560 /* ignore RSIP for unknown CIC */
561 } else {
562 /* Resolve the vc_conn by the CIC */
563 vc_conn := f_comp_by_cic(cic);
564 CLIENT.send(mgcp_req) to vc_conn;
565 }
Harald Weltec82eef42017-11-24 20:40:12 +0100566 }
567 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
568 /* MGCP response from network side (we're MSC) */
569 /* Resolve the vc_conn by the transaction ID */
570 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
571 CLIENT.send(mgcp_resp) to vc_conn;
572 }
573
Harald Welte624f9632017-12-16 19:26:04 +0100574
575 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
576 f_create_expect(l3_info, vc_hdlr);
577 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
578 }
579
Harald Welte17d21152018-01-27 00:47:11 +0100580 [] PROC.getcall(BSSMAPEM_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
581 f_create_imsi(imsi, tmsi, vc_hdlr);
582 PROC.reply(BSSMAPEM_register_imsi:{imsi, tmsi, vc_hdlr});
583 }
584
585
Harald Welte365f4ed2017-11-23 00:00:43 +0100586 }
587 }
588}
589
Harald Weltec82eef42017-11-24 20:40:12 +0100590private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100591 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100592 return hex2int(str2hex(local_part));
593
594}
Harald Welte365f4ed2017-11-23 00:00:43 +0100595
Harald Welte624f9632017-12-16 19:26:04 +0100596/***********************************************************************
597 * "Expect" Handling (mapping for expected incoming SCCP connections)
598 ***********************************************************************/
599
600/* data about an expected future incoming connection */
601type record ExpectData {
602 /* L3 payload based on which we can match it */
603 octetstring l3_payload optional,
604 /* component reference for this connection */
605 BSSAP_ConnHdlr vc_conn
606}
607
608/* procedure based port to register for incoming connections */
609signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
610
Harald Welte17d21152018-01-27 00:47:11 +0100611/* procedure based port to register for incoming IMSI/TMSI */
612signature BSSMAPEM_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_ConnHdlr hdlr);
613
Harald Welte624f9632017-12-16 19:26:04 +0100614type port BSSMAPEM_PROC_PT procedure {
Harald Welte17d21152018-01-27 00:47:11 +0100615 inout BSSMAPEM_register, BSSMAPEM_register_imsi;
Harald Welte624f9632017-12-16 19:26:04 +0100616} with { extension "internal" };
617
618/* CreateCallback that can be used as create_cb and will use the expectation table */
619function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
620runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
621 var BSSAP_ConnHdlr ret := null;
622 var octetstring l3_info;
623 var integer i;
624
625 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
626 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
627 return ret;
628 }
629 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
630
631 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
632 if (not ispresent(ExpectTable[i].l3_payload)) {
633 continue;
634 }
635 if (l3_info == ExpectTable[i].l3_payload) {
636 ret := ExpectTable[i].vc_conn;
637 /* release this entry to be used again */
638 ExpectTable[i].l3_payload := omit;
639 ExpectTable[i].vc_conn := null;
640 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
641 /* return the component reference */
642 return ret;
643 }
644 }
645 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
646 return ret;
647}
648
649private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
650runs on BSSMAP_Emulation_CT {
651 var integer i;
652 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
653 if (not ispresent(ExpectTable[i].l3_payload)) {
654 ExpectTable[i].l3_payload := l3;
655 ExpectTable[i].vc_conn := hdlr;
656 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
657 return;
658 }
659 }
660 setverdict(fail, "No space left in ExpectTable");
661}
662
Harald Welte17d21152018-01-27 00:47:11 +0100663private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_ConnHdlr hdlr)
664runs on BSSMAP_Emulation_CT {
665 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
666 if (not ispresent(ImsiTable[i].imsi)) {
667 ImsiTable[i].imsi := imsi;
668 ImsiTable[i].tmsi := tmsi;
669 ImsiTable[i].comp_ref := hdlr;
670 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
671 return;
672 }
673 }
674 setverdict(fail, "No space left in ImsiTable");
675 self.stop;
676}
677
678
Daniel Willmannd47106b2018-01-17 12:20:56 +0100679private function f_expect_table_init()
680runs on BSSMAP_Emulation_CT {
681 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
682 ExpectTable[i].l3_payload := omit;
683 }
684}
Harald Welte624f9632017-12-16 19:26:04 +0100685
Harald Welte17d21152018-01-27 00:47:11 +0100686/* helper function for clients to register their IMSI/TMSI */
687function f_bssmap_register_imsi(hexstring imsi, OCT4 tmsi)
688runs on BSSAP_ConnHdlr {
689 BSSAP_PROC.call(BSSMAPEM_register_imsi:{imsi, tmsi, self}) {
690 [] BSSAP_PROC.getreply(BSSMAPEM_register_imsi:{?,?,?}) {};
691 }
692}
693
694
Harald Welte365f4ed2017-11-23 00:00:43 +0100695}