blob: e7a432c3adaaa621a5cad73c4a24e396e0431cd1 [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
119type component BSSMAP_Emulation_CT {
120 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100121 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100122 /* BSSAP port to the per-connection clients */
123 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +0100124 /* MGCP port */
125 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100126
127 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
128 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100129
Harald Welte624f9632017-12-16 19:26:04 +0100130 /* pending expected incoming connections */
131 var ExpectData ExpectTable[8];
132 /* procedure based port to register for incoming connections */
133 port BSSMAPEM_PROC_PT PROC;
134
Harald Weltebe620f62017-11-25 00:23:54 +0100135 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100136 var integer g_next_e1_ts := 1;
Harald Welte0b476062018-01-21 19:07:32 +0100137 var BssmapOps g_bssmap_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100138};
139
Harald Welteb3414b22017-11-23 18:22:10 +0100140private function f_conn_id_known(integer sccp_conn_id)
141runs on BSSMAP_Emulation_CT return boolean {
142 var integer i;
143 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
144 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
145 return true;
146 }
147 }
148 return false;
149}
150
151private function f_comp_known(BSSAP_ConnHdlr client)
152runs on BSSMAP_Emulation_CT return boolean {
153 var integer i;
154 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
155 if (ConnectionTable[i].comp_ref == client) {
156 return true;
157 }
158 }
159 return false;
160}
Harald Welte365f4ed2017-11-23 00:00:43 +0100161
Harald Weltee98bb2e2017-11-29 12:09:48 +0100162private function f_cic_known(integer cic)
163runs on BSSMAP_Emulation_CT return boolean {
164 var integer i;
165 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
166 if (ConnectionTable[i].cic == cic) {
167 return true;
168 }
169 }
170 return false;
171}
172
Harald Welte365f4ed2017-11-23 00:00:43 +0100173/* resolve component reference by connection ID */
174private function f_comp_by_conn_id(integer sccp_conn_id)
175runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
176 var integer i;
177 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
178 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
179 return ConnectionTable[i].comp_ref;
180 }
181 }
182 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100183 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100184 self.stop;
185}
186
Harald Weltec82eef42017-11-24 20:40:12 +0100187/* resolve component reference by CIC */
188private function f_comp_by_mgcp_tid(MgcpTransId tid)
189runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
190 var integer i;
191 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
192 if (ConnectionTable[i].mgcp_trans_id == tid) {
193 return ConnectionTable[i].comp_ref;
194 }
195 }
196 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100197 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100198 self.stop;
199}
200
201private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
202runs on BSSMAP_Emulation_CT {
203 var integer i;
204 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
205 if (ConnectionTable[i].comp_ref == client) {
206 ConnectionTable[i].mgcp_trans_id := tid;
207 return;
208 }
209 }
210 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100211 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100212 self.stop;
213}
214
215private function f_comp_by_cic(integer cic)
216runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
217 var integer i;
218 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
219 if (ConnectionTable[i].cic == cic) {
220 return ConnectionTable[i].comp_ref;
221 }
222 }
223 log("BSSMAP Connection table not found by CIC ", cic);
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_store_cic(BSSAP_ConnHdlr client, integer cic)
229runs on BSSMAP_Emulation_CT {
230 var integer i;
231 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
232 if (ConnectionTable[i].comp_ref == client) {
233 ConnectionTable[i].cic := cic;
234 return;
235 }
236 }
237 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100238 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100239}
240
Harald Welte365f4ed2017-11-23 00:00:43 +0100241/* resolve connection ID by component reference */
242private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
243runs on BSSMAP_Emulation_CT return integer {
244 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
245 if (ConnectionTable[i].comp_ref == client) {
246 return ConnectionTable[i].sccp_conn_id;
247 }
248 }
249 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100250 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100251 self.stop;
252}
253
Harald Welteb3414b22017-11-23 18:22:10 +0100254private function f_gen_conn_id()
255runs on BSSMAP_Emulation_CT return integer {
256 var integer conn_id;
257
258 do {
259 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
260 } while (f_conn_id_known(conn_id) == true);
261
262 return conn_id;
263}
264
Harald Welte365f4ed2017-11-23 00:00:43 +0100265private function f_conn_table_init()
266runs on BSSMAP_Emulation_CT {
267 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
268 ConnectionTable[i].comp_ref := null;
269 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100270 ConnectionTable[i].mgcp_trans_id := omit;
271 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100272 }
273}
274
275private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
276runs on BSSMAP_Emulation_CT {
277 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
278 if (ConnectionTable[i].sccp_conn_id == -1) {
279 ConnectionTable[i].comp_ref := comp_ref;
280 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100281 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100282 return;
283 }
284 }
285 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100286 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100287 self.stop;
288}
289
290private function f_conn_table_del(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 == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100294 log("Deleted conn table entry ", i,
295 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100296 ConnectionTable[i].sccp_conn_id := -1;
297 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100298 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 }
300 }
301 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100302 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100303 self.stop;
304}
305
306/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100307private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100308runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100309 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100310
311 /* BSC Side: If this is an assignment command, store CIC */
312 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
313 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
314 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
315 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
316 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
317 f_comp_store_cic(client, cic);
318 }
319
Harald Welte0b476062018-01-21 19:07:32 +0100320 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
321 if (g_bssmap_ops.role_ms) {
322 /* we are the MS, so any message to us must be MT */
323 var PDU_DTAP_MT mt := {
324 dlci := bssap.dlci,
325 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
326 };
327 CLIENT.send(mt) to client;
328 } else {
329 /* we are the Network, so any message to us must be MO */
330 var PDU_DTAP_MO mo := {
331 dlci := bssap.dlci,
332 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
333 };
334 CLIENT.send(mo) to client;
335 }
336 } else {
337 CLIENT.send(bssap) to client;
338 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100339}
340
341/* call-back type, to be provided by specific implementation; called when new SCCP connection
342 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100343type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100344runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
345
346type function BssmapUnitdataCallback(PDU_BSSAP bssap)
347runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
348
349type record BssmapOps {
350 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100351 BssmapUnitdataCallback unitdata_cb,
352 boolean decode_dtap,
353 boolean role_ms
Harald Welte365f4ed2017-11-23 00:00:43 +0100354}
355
Harald Weltebe620f62017-11-25 00:23:54 +0100356function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100357
Harald Weltebe620f62017-11-25 00:23:54 +0100358 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100359 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100360 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100361 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100362
363 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100364 var BSSAP_N_UNITDATA_ind ud_ind;
365 var BSSAP_N_CONNECT_ind conn_ind;
366 var BSSAP_N_CONNECT_cfm conn_cfm;
367 var BSSAP_N_DATA_ind data_ind;
368 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100369 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100370 var BSSAP_ConnHdlr vc_conn;
371 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100372 var PDU_DTAP_MO dtap_mo;
373 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100374 var MgcpCommand mgcp_req;
375 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100376 var BSSAP_ConnHdlr vc_hdlr;
377 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100378
379 alt {
380 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100381 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100382 /* Connectionless Procedures like RESET */
383 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100384 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100385 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100386 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
387 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100388 }
389 }
390
391 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100392 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100393 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100394 /* store mapping between client components and SCCP connectionId */
395 f_conn_table_add(vc_conn, conn_ind.connectionId);
396 /* handle user payload */
397 f_handle_userData(vc_conn, conn_ind.userData);
398 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100399 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100400 }
401
402 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100403 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100404 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100405 if (ispresent(data_ind.userData)) {
406 f_handle_userData(vc_conn, data_ind.userData);
407 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100408 }
409
410 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100411 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100412 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100413 if (ispresent(disc_ind.userData)) {
414 f_handle_userData(vc_conn, disc_ind.userData);
415 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100416 /* notify client about termination */
417 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
418 CLIENT.send(prim) to vc_conn;
419 f_conn_table_del(disc_ind.connectionId);
420 /* TOOD: return confirm to other side? */
421 }
422
Harald Welteb3414b22017-11-23 18:22:10 +0100423 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100424 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100425 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
426 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
427 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100428 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100429 if (ispresent(conn_cfm.userData)) {
430 f_handle_userData(vc_conn, conn_cfm.userData);
431 }
Harald Welteb3414b22017-11-23 18:22:10 +0100432 }
433
Harald Welte365f4ed2017-11-23 00:00:43 +0100434 /* Disconnect request client -> SCCP */
435 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
436 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100437 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100438 f_conn_table_del(conn_id);
439 }
440
441 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100442 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
443 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100444 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100445
446 if (f_comp_known(vc_conn) == false) {
447 /* unknown client, create new connection */
448 conn_id := f_gen_conn_id();
449
450 /* store mapping between client components and SCCP connectionId */
451 f_conn_table_add(vc_conn, conn_id);
452
Harald Welte004f5fb2017-12-16 17:54:40 +0100453 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
454 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100455 } else {
456 /* known client, send via existing connection */
457 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100458 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100459 }
460
461 }
462
Harald Welte365f4ed2017-11-23 00:00:43 +0100463 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
464 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100465 /* send it to dispatcher */
466 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100467 }
468
Harald Welte0b476062018-01-21 19:07:32 +0100469 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
470 var integer conn_id := f_conn_id_by_comp(vc_conn);
471 /* convert from decoded DTAP to encoded DTAP */
472 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
473 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
474 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
475 }
476
477 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
478 var integer conn_id := f_conn_id_by_comp(vc_conn);
479 /* convert from decoded DTAP to encoded DTAP */
480 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
481 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
482 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
483 }
484
485
Harald Weltec82eef42017-11-24 20:40:12 +0100486 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
487 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
488 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
489 * is printed as hex string, e.g. a@mgw for CIC 10 */
490
491 /* CLIENT -> MGCP */
492 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
493 /* MGCP request from Handler (we're MSC) */
494 /* store the transaction ID we've seen */
495 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
496 /* simply forward any MGCP from the client to the port */
497 MGCP.send(mgcp_req);
498 }
499 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
500 /* MGCP response from Handler (we're BSC/MGW) */
501 /* simply forward any MGCP from the client to the port */
502 MGCP.send(mgcp_resp);
503 }
504
505 /* MGCP -> CLIENT */
506 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
507 /* MGCP request from network side (we're BSC/MGW) */
508 /* Extract CIC from local part of endpoint name */
509 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100510 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
511 /* ignore RSIP for unknown CIC */
512 } else {
513 /* Resolve the vc_conn by the CIC */
514 vc_conn := f_comp_by_cic(cic);
515 CLIENT.send(mgcp_req) to vc_conn;
516 }
Harald Weltec82eef42017-11-24 20:40:12 +0100517 }
518 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
519 /* MGCP response from network side (we're MSC) */
520 /* Resolve the vc_conn by the transaction ID */
521 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
522 CLIENT.send(mgcp_resp) to vc_conn;
523 }
524
Harald Welte624f9632017-12-16 19:26:04 +0100525
526 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
527 f_create_expect(l3_info, vc_hdlr);
528 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
529 }
530
Harald Welte365f4ed2017-11-23 00:00:43 +0100531 }
532 }
533}
534
Harald Weltec82eef42017-11-24 20:40:12 +0100535private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100536 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100537 return hex2int(str2hex(local_part));
538
539}
Harald Welte365f4ed2017-11-23 00:00:43 +0100540
Harald Welte624f9632017-12-16 19:26:04 +0100541/***********************************************************************
542 * "Expect" Handling (mapping for expected incoming SCCP connections)
543 ***********************************************************************/
544
545/* data about an expected future incoming connection */
546type record ExpectData {
547 /* L3 payload based on which we can match it */
548 octetstring l3_payload optional,
549 /* component reference for this connection */
550 BSSAP_ConnHdlr vc_conn
551}
552
553/* procedure based port to register for incoming connections */
554signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
555
556type port BSSMAPEM_PROC_PT procedure {
557 inout BSSMAPEM_register;
558} with { extension "internal" };
559
560/* CreateCallback that can be used as create_cb and will use the expectation table */
561function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
562runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
563 var BSSAP_ConnHdlr ret := null;
564 var octetstring l3_info;
565 var integer i;
566
567 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
568 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
569 return ret;
570 }
571 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
572
573 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
574 if (not ispresent(ExpectTable[i].l3_payload)) {
575 continue;
576 }
577 if (l3_info == ExpectTable[i].l3_payload) {
578 ret := ExpectTable[i].vc_conn;
579 /* release this entry to be used again */
580 ExpectTable[i].l3_payload := omit;
581 ExpectTable[i].vc_conn := null;
582 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
583 /* return the component reference */
584 return ret;
585 }
586 }
587 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
588 return ret;
589}
590
591private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
592runs on BSSMAP_Emulation_CT {
593 var integer i;
594 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
595 if (not ispresent(ExpectTable[i].l3_payload)) {
596 ExpectTable[i].l3_payload := l3;
597 ExpectTable[i].vc_conn := hdlr;
598 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
599 return;
600 }
601 }
602 setverdict(fail, "No space left in ExpectTable");
603}
604
Daniel Willmannd47106b2018-01-17 12:20:56 +0100605private function f_expect_table_init()
606runs on BSSMAP_Emulation_CT {
607 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
608 ExpectTable[i].l3_payload := omit;
609 }
610}
Harald Welte624f9632017-12-16 19:26:04 +0100611
Harald Welte365f4ed2017-11-23 00:00:43 +0100612}