blob: dce8eb0e66892aff8fff22abe9953b1c862c1d59 [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 */
58 MSC_CONN_PRIM_DISC_REQ
59}
60
Harald Welteb3414b22017-11-23 18:22:10 +010061type record BSSAP_Conn_Req {
62 SCCP_PAR_Address addr_peer,
63 SCCP_PAR_Address addr_own,
64 PDU_BSSAP bssap
65}
66
Harald Welte0b476062018-01-21 19:07:32 +010067/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
68type record PDU_DTAP_MO {
69 OCT1 dlci optional,
70 PDU_ML3_MS_NW dtap
71}
72
73/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
74type record PDU_DTAP_MT {
75 OCT1 dlci optional,
76 PDU_ML3_NW_MS dtap
77}
78
79template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
80 dlci := dlci,
81 dtap := dtap
82}
83
84template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O) := {
85 dlci := dlci,
86 dtap := dtap
87}
88
89template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
90 dlci := dlci,
91 dtap := dtap
92}
93
94template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
95 dlci := dlci,
96 dtap := dtap
97}
98
99
Harald Welte365f4ed2017-11-23 00:00:43 +0100100/* port between individual per-connection components and this dispatcher */
101type port BSSAP_Conn_PT message {
Harald Welte0b476062018-01-21 19:07:32 +0100102 inout PDU_BSSAP, PDU_DTAP_MO, PDU_DTAP_MT, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +0100103} with { extension "internal" };
104
105
106/* represents a single BSSAP connection over SCCP */
107type record ConnectionData {
108 /* reference to the instance of the per-connection component */
109 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100110 integer sccp_conn_id,
111 /* most recent MGCP transaction ID (Used on MSC side) */
112 MgcpTransId mgcp_trans_id optional,
113 /* CIC that has been used for voice of this channel (BSC side) */
114 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +0100115}
116
117type component BSSMAP_Emulation_CT {
118 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100119 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100120 /* BSSAP port to the per-connection clients */
121 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +0100122 /* MGCP port */
123 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100124
125 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
126 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100127
Harald Welte624f9632017-12-16 19:26:04 +0100128 /* pending expected incoming connections */
129 var ExpectData ExpectTable[8];
130 /* procedure based port to register for incoming connections */
131 port BSSMAPEM_PROC_PT PROC;
132
Harald Weltebe620f62017-11-25 00:23:54 +0100133 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100134 var integer g_next_e1_ts := 1;
Harald Welte0b476062018-01-21 19:07:32 +0100135 var BssmapOps g_bssmap_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100136};
137
Harald Welteb3414b22017-11-23 18:22:10 +0100138private function f_conn_id_known(integer sccp_conn_id)
139runs on BSSMAP_Emulation_CT return boolean {
140 var integer i;
141 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
142 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
143 return true;
144 }
145 }
146 return false;
147}
148
149private function f_comp_known(BSSAP_ConnHdlr client)
150runs on BSSMAP_Emulation_CT return boolean {
151 var integer i;
152 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
153 if (ConnectionTable[i].comp_ref == client) {
154 return true;
155 }
156 }
157 return false;
158}
Harald Welte365f4ed2017-11-23 00:00:43 +0100159
Harald Weltee98bb2e2017-11-29 12:09:48 +0100160private function f_cic_known(integer cic)
161runs on BSSMAP_Emulation_CT return boolean {
162 var integer i;
163 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
164 if (ConnectionTable[i].cic == cic) {
165 return true;
166 }
167 }
168 return false;
169}
170
Harald Welte365f4ed2017-11-23 00:00:43 +0100171/* resolve component reference by connection ID */
172private function f_comp_by_conn_id(integer sccp_conn_id)
173runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
174 var integer i;
175 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
176 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
177 return ConnectionTable[i].comp_ref;
178 }
179 }
180 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100181 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100182 self.stop;
183}
184
Harald Weltec82eef42017-11-24 20:40:12 +0100185/* resolve component reference by CIC */
186private function f_comp_by_mgcp_tid(MgcpTransId tid)
187runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
188 var integer i;
189 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
190 if (ConnectionTable[i].mgcp_trans_id == tid) {
191 return ConnectionTable[i].comp_ref;
192 }
193 }
194 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100195 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100196 self.stop;
197}
198
199private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
200runs on BSSMAP_Emulation_CT {
201 var integer i;
202 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
203 if (ConnectionTable[i].comp_ref == client) {
204 ConnectionTable[i].mgcp_trans_id := tid;
205 return;
206 }
207 }
208 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100209 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100210 self.stop;
211}
212
213private function f_comp_by_cic(integer cic)
214runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
215 var integer i;
216 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
217 if (ConnectionTable[i].cic == cic) {
218 return ConnectionTable[i].comp_ref;
219 }
220 }
221 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100222 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100223 self.stop;
224}
225
226private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
227runs on BSSMAP_Emulation_CT {
228 var integer i;
229 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
230 if (ConnectionTable[i].comp_ref == client) {
231 ConnectionTable[i].cic := cic;
232 return;
233 }
234 }
235 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100236 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100237}
238
Harald Welte365f4ed2017-11-23 00:00:43 +0100239/* resolve connection ID by component reference */
240private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
241runs on BSSMAP_Emulation_CT return integer {
242 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
243 if (ConnectionTable[i].comp_ref == client) {
244 return ConnectionTable[i].sccp_conn_id;
245 }
246 }
247 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100248 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100249 self.stop;
250}
251
Harald Welteb3414b22017-11-23 18:22:10 +0100252private function f_gen_conn_id()
253runs on BSSMAP_Emulation_CT return integer {
254 var integer conn_id;
255
256 do {
257 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
258 } while (f_conn_id_known(conn_id) == true);
259
260 return conn_id;
261}
262
Harald Welte365f4ed2017-11-23 00:00:43 +0100263private function f_conn_table_init()
264runs on BSSMAP_Emulation_CT {
265 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
266 ConnectionTable[i].comp_ref := null;
267 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100268 ConnectionTable[i].mgcp_trans_id := omit;
269 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100270 }
271}
272
273private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
274runs on BSSMAP_Emulation_CT {
275 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
276 if (ConnectionTable[i].sccp_conn_id == -1) {
277 ConnectionTable[i].comp_ref := comp_ref;
278 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100279 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100280 return;
281 }
282 }
283 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100284 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100285 self.stop;
286}
287
288private function f_conn_table_del(integer sccp_conn_id)
289runs on BSSMAP_Emulation_CT {
290 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
291 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100292 log("Deleted conn table entry ", i,
293 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100294 ConnectionTable[i].sccp_conn_id := -1;
295 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100296 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100297 }
298 }
299 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100300 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100301 self.stop;
302}
303
304/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100305private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100306runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100307 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100308
309 /* BSC Side: If this is an assignment command, store CIC */
310 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
311 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
312 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
313 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
314 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
315 f_comp_store_cic(client, cic);
316 }
317
Harald Welte0b476062018-01-21 19:07:32 +0100318 if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) {
319 if (g_bssmap_ops.role_ms) {
320 /* we are the MS, so any message to us must be MT */
321 var PDU_DTAP_MT mt := {
322 dlci := bssap.dlci,
323 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
324 };
325 CLIENT.send(mt) to client;
326 } else {
327 /* we are the Network, so any message to us must be MO */
328 var PDU_DTAP_MO mo := {
329 dlci := bssap.dlci,
330 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
331 };
332 CLIENT.send(mo) to client;
333 }
334 } else {
335 CLIENT.send(bssap) to client;
336 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100337}
338
339/* call-back type, to be provided by specific implementation; called when new SCCP connection
340 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100341type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100342runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
343
344type function BssmapUnitdataCallback(PDU_BSSAP bssap)
345runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
346
347type record BssmapOps {
348 BssmapCreateCallback create_cb,
Harald Welte0b476062018-01-21 19:07:32 +0100349 BssmapUnitdataCallback unitdata_cb,
350 boolean decode_dtap,
351 boolean role_ms
Harald Welte365f4ed2017-11-23 00:00:43 +0100352}
353
Harald Weltebe620f62017-11-25 00:23:54 +0100354function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100355
Harald Weltebe620f62017-11-25 00:23:54 +0100356 g_bssmap_id := id;
Harald Welte0b476062018-01-21 19:07:32 +0100357 g_bssmap_ops := ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100358 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100359 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100360
361 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100362 var BSSAP_N_UNITDATA_ind ud_ind;
363 var BSSAP_N_CONNECT_ind conn_ind;
364 var BSSAP_N_CONNECT_cfm conn_cfm;
365 var BSSAP_N_DATA_ind data_ind;
366 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100367 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100368 var BSSAP_ConnHdlr vc_conn;
369 var PDU_BSSAP bssap;
Harald Welte0b476062018-01-21 19:07:32 +0100370 var PDU_DTAP_MO dtap_mo;
371 var PDU_DTAP_MT dtap_mt;
Harald Weltec82eef42017-11-24 20:40:12 +0100372 var MgcpCommand mgcp_req;
373 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100374 var BSSAP_ConnHdlr vc_hdlr;
375 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100376
377 alt {
378 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100379 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100380 /* Connectionless Procedures like RESET */
381 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100382 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100383 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100384 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
385 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100386 }
387 }
388
389 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100390 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100391 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100392 /* store mapping between client components and SCCP connectionId */
393 f_conn_table_add(vc_conn, conn_ind.connectionId);
394 /* handle user payload */
395 f_handle_userData(vc_conn, conn_ind.userData);
396 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100397 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100398 }
399
400 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100401 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100402 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100403 if (ispresent(data_ind.userData)) {
404 f_handle_userData(vc_conn, data_ind.userData);
405 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100406 }
407
408 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100409 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100410 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100411 if (ispresent(disc_ind.userData)) {
412 f_handle_userData(vc_conn, disc_ind.userData);
413 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100414 /* notify client about termination */
415 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
416 CLIENT.send(prim) to vc_conn;
417 f_conn_table_del(disc_ind.connectionId);
418 /* TOOD: return confirm to other side? */
419 }
420
Harald Welteb3414b22017-11-23 18:22:10 +0100421 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100422 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100423 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100424 if (ispresent(conn_cfm.userData)) {
425 f_handle_userData(vc_conn, conn_cfm.userData);
426 }
Harald Welteb3414b22017-11-23 18:22:10 +0100427 }
428
Harald Welte365f4ed2017-11-23 00:00:43 +0100429 /* Disconnect request client -> SCCP */
430 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
431 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100432 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100433 f_conn_table_del(conn_id);
434 }
435
436 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100437 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
438 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100439 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100440
441 if (f_comp_known(vc_conn) == false) {
442 /* unknown client, create new connection */
443 conn_id := f_gen_conn_id();
444
445 /* store mapping between client components and SCCP connectionId */
446 f_conn_table_add(vc_conn, conn_id);
447
Harald Welte004f5fb2017-12-16 17:54:40 +0100448 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
449 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100450 } else {
451 /* known client, send via existing connection */
452 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100453 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100454 }
455
456 }
457
Harald Welte365f4ed2017-11-23 00:00:43 +0100458 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
459 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100460 /* send it to dispatcher */
461 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100462 }
463
Harald Welte0b476062018-01-21 19:07:32 +0100464 [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
465 var integer conn_id := f_conn_id_by_comp(vc_conn);
466 /* convert from decoded DTAP to encoded DTAP */
467 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
468 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
469 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
470 }
471
472 [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
473 var integer conn_id := f_conn_id_by_comp(vc_conn);
474 /* convert from decoded DTAP to encoded DTAP */
475 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
476 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci));
477 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
478 }
479
480
Harald Weltec82eef42017-11-24 20:40:12 +0100481 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
482 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
483 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
484 * is printed as hex string, e.g. a@mgw for CIC 10 */
485
486 /* CLIENT -> MGCP */
487 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
488 /* MGCP request from Handler (we're MSC) */
489 /* store the transaction ID we've seen */
490 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
491 /* simply forward any MGCP from the client to the port */
492 MGCP.send(mgcp_req);
493 }
494 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
495 /* MGCP response from Handler (we're BSC/MGW) */
496 /* simply forward any MGCP from the client to the port */
497 MGCP.send(mgcp_resp);
498 }
499
500 /* MGCP -> CLIENT */
501 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
502 /* MGCP request from network side (we're BSC/MGW) */
503 /* Extract CIC from local part of endpoint name */
504 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100505 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
506 /* ignore RSIP for unknown CIC */
507 } else {
508 /* Resolve the vc_conn by the CIC */
509 vc_conn := f_comp_by_cic(cic);
510 CLIENT.send(mgcp_req) to vc_conn;
511 }
Harald Weltec82eef42017-11-24 20:40:12 +0100512 }
513 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
514 /* MGCP response from network side (we're MSC) */
515 /* Resolve the vc_conn by the transaction ID */
516 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
517 CLIENT.send(mgcp_resp) to vc_conn;
518 }
519
Harald Welte624f9632017-12-16 19:26:04 +0100520
521 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
522 f_create_expect(l3_info, vc_hdlr);
523 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
524 }
525
Harald Welte365f4ed2017-11-23 00:00:43 +0100526 }
527 }
528}
529
Harald Weltec82eef42017-11-24 20:40:12 +0100530private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100531 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100532 return hex2int(str2hex(local_part));
533
534}
Harald Welte365f4ed2017-11-23 00:00:43 +0100535
Harald Welte624f9632017-12-16 19:26:04 +0100536/***********************************************************************
537 * "Expect" Handling (mapping for expected incoming SCCP connections)
538 ***********************************************************************/
539
540/* data about an expected future incoming connection */
541type record ExpectData {
542 /* L3 payload based on which we can match it */
543 octetstring l3_payload optional,
544 /* component reference for this connection */
545 BSSAP_ConnHdlr vc_conn
546}
547
548/* procedure based port to register for incoming connections */
549signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
550
551type port BSSMAPEM_PROC_PT procedure {
552 inout BSSMAPEM_register;
553} with { extension "internal" };
554
555/* CreateCallback that can be used as create_cb and will use the expectation table */
556function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
557runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
558 var BSSAP_ConnHdlr ret := null;
559 var octetstring l3_info;
560 var integer i;
561
562 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
563 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
564 return ret;
565 }
566 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
567
568 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
569 if (not ispresent(ExpectTable[i].l3_payload)) {
570 continue;
571 }
572 if (l3_info == ExpectTable[i].l3_payload) {
573 ret := ExpectTable[i].vc_conn;
574 /* release this entry to be used again */
575 ExpectTable[i].l3_payload := omit;
576 ExpectTable[i].vc_conn := null;
577 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
578 /* return the component reference */
579 return ret;
580 }
581 }
582 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
583 return ret;
584}
585
586private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
587runs on BSSMAP_Emulation_CT {
588 var integer i;
589 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
590 if (not ispresent(ExpectTable[i].l3_payload)) {
591 ExpectTable[i].l3_payload := l3;
592 ExpectTable[i].vc_conn := hdlr;
593 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
594 return;
595 }
596 }
597 setverdict(fail, "No space left in ExpectTable");
598}
599
Daniel Willmannd47106b2018-01-17 12:20:56 +0100600private function f_expect_table_init()
601runs on BSSMAP_Emulation_CT {
602 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
603 ExpectTable[i].l3_payload := omit;
604 }
605}
Harald Welte624f9632017-12-16 19:26:04 +0100606
Harald Welte365f4ed2017-11-23 00:00:43 +0100607}