blob: 14174f8c8fc04558aa30086b451b759c5ec1b870 [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 *
25 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
26 * 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 Welteb3414b22017-11-23 18:22:10 +010033import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010034import from SCCPasp_Types all;
35import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010036import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010037import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +010038import from MGCP_Types all;
39import from MGCP_Templates all;
40import from IPA_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010041
42/* General "base class" component definition, of which specific implementations
43 * derive themselves by means of the "extends" feature */
44type component BSSAP_ConnHdlr {
45 /* port towards MSC Emulator core / SCCP connection dispatchar */
46 port BSSAP_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010047 /* procedure based port to register for incoming connections */
48 port BSSMAPEM_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010049}
50
51/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
52type enumerated BSSAP_Conn_Prim {
53 /* SCCP tell us that connection was released */
54 MSC_CONN_PRIM_DISC_IND,
55 /* we tell SCCP to release connection */
56 MSC_CONN_PRIM_DISC_REQ
57}
58
Harald Welteb3414b22017-11-23 18:22:10 +010059type record BSSAP_Conn_Req {
60 SCCP_PAR_Address addr_peer,
61 SCCP_PAR_Address addr_own,
62 PDU_BSSAP bssap
63}
64
Harald Welte365f4ed2017-11-23 00:00:43 +010065/* port between individual per-connection components and this dispatcher */
66type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010067 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010068} with { extension "internal" };
69
70
71/* represents a single BSSAP connection over SCCP */
72type record ConnectionData {
73 /* reference to the instance of the per-connection component */
74 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010075 integer sccp_conn_id,
76 /* most recent MGCP transaction ID (Used on MSC side) */
77 MgcpTransId mgcp_trans_id optional,
78 /* CIC that has been used for voice of this channel (BSC side) */
79 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010080}
81
82type component BSSMAP_Emulation_CT {
83 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +010084 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +010085 /* BSSAP port to the per-connection clients */
86 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010087 /* MGCP port */
88 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010089
90 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
91 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010092
Harald Welte624f9632017-12-16 19:26:04 +010093 /* pending expected incoming connections */
94 var ExpectData ExpectTable[8];
95 /* procedure based port to register for incoming connections */
96 port BSSMAPEM_PROC_PT PROC;
97
Harald Weltebe620f62017-11-25 00:23:54 +010098 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010099 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +0100100};
101
Harald Welteb3414b22017-11-23 18:22:10 +0100102private function f_conn_id_known(integer sccp_conn_id)
103runs on BSSMAP_Emulation_CT return boolean {
104 var integer i;
105 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
106 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
107 return true;
108 }
109 }
110 return false;
111}
112
113private function f_comp_known(BSSAP_ConnHdlr client)
114runs on BSSMAP_Emulation_CT return boolean {
115 var integer i;
116 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
117 if (ConnectionTable[i].comp_ref == client) {
118 return true;
119 }
120 }
121 return false;
122}
Harald Welte365f4ed2017-11-23 00:00:43 +0100123
Harald Weltee98bb2e2017-11-29 12:09:48 +0100124private function f_cic_known(integer cic)
125runs on BSSMAP_Emulation_CT return boolean {
126 var integer i;
127 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
128 if (ConnectionTable[i].cic == cic) {
129 return true;
130 }
131 }
132 return false;
133}
134
Harald Welte365f4ed2017-11-23 00:00:43 +0100135/* resolve component reference by connection ID */
136private function f_comp_by_conn_id(integer sccp_conn_id)
137runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
138 var integer i;
139 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
140 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
141 return ConnectionTable[i].comp_ref;
142 }
143 }
144 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100145 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100146 self.stop;
147}
148
Harald Weltec82eef42017-11-24 20:40:12 +0100149/* resolve component reference by CIC */
150private function f_comp_by_mgcp_tid(MgcpTransId tid)
151runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
152 var integer i;
153 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
154 if (ConnectionTable[i].mgcp_trans_id == tid) {
155 return ConnectionTable[i].comp_ref;
156 }
157 }
158 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100159 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100160 self.stop;
161}
162
163private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
164runs on BSSMAP_Emulation_CT {
165 var integer i;
166 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
167 if (ConnectionTable[i].comp_ref == client) {
168 ConnectionTable[i].mgcp_trans_id := tid;
169 return;
170 }
171 }
172 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100173 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100174 self.stop;
175}
176
177private function f_comp_by_cic(integer cic)
178runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
179 var integer i;
180 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
181 if (ConnectionTable[i].cic == cic) {
182 return ConnectionTable[i].comp_ref;
183 }
184 }
185 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100186 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100187 self.stop;
188}
189
190private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
191runs on BSSMAP_Emulation_CT {
192 var integer i;
193 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
194 if (ConnectionTable[i].comp_ref == client) {
195 ConnectionTable[i].cic := cic;
196 return;
197 }
198 }
199 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100200 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100201}
202
Harald Welte365f4ed2017-11-23 00:00:43 +0100203/* resolve connection ID by component reference */
204private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
205runs on BSSMAP_Emulation_CT return integer {
206 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
207 if (ConnectionTable[i].comp_ref == client) {
208 return ConnectionTable[i].sccp_conn_id;
209 }
210 }
211 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100212 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100213 self.stop;
214}
215
Harald Welteb3414b22017-11-23 18:22:10 +0100216private function f_gen_conn_id()
217runs on BSSMAP_Emulation_CT return integer {
218 var integer conn_id;
219
220 do {
221 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
222 } while (f_conn_id_known(conn_id) == true);
223
224 return conn_id;
225}
226
Harald Welte365f4ed2017-11-23 00:00:43 +0100227private function f_conn_table_init()
228runs on BSSMAP_Emulation_CT {
229 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
230 ConnectionTable[i].comp_ref := null;
231 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100232 ConnectionTable[i].mgcp_trans_id := omit;
233 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100234 }
235}
236
237private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
238runs on BSSMAP_Emulation_CT {
239 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
240 if (ConnectionTable[i].sccp_conn_id == -1) {
241 ConnectionTable[i].comp_ref := comp_ref;
242 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100243 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100244 return;
245 }
246 }
247 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100248 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100249 self.stop;
250}
251
252private function f_conn_table_del(integer sccp_conn_id)
253runs on BSSMAP_Emulation_CT {
254 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
255 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100256 log("Deleted conn table entry ", i,
257 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100258 ConnectionTable[i].sccp_conn_id := -1;
259 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100260 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100261 }
262 }
263 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100264 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100265 self.stop;
266}
267
268/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100269private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100270runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100271 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100272
273 /* BSC Side: If this is an assignment command, store CIC */
274 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
275 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
276 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
277 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
278 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
279 f_comp_store_cic(client, cic);
280 }
281
Harald Welte365f4ed2017-11-23 00:00:43 +0100282 CLIENT.send(bssap) to client;
283}
284
285/* call-back type, to be provided by specific implementation; called when new SCCP connection
286 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100287type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100288runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
289
290type function BssmapUnitdataCallback(PDU_BSSAP bssap)
291runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
292
293type record BssmapOps {
294 BssmapCreateCallback create_cb,
295 BssmapUnitdataCallback unitdata_cb
296}
297
Harald Weltebe620f62017-11-25 00:23:54 +0100298function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100299
Harald Weltebe620f62017-11-25 00:23:54 +0100300 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100301 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100302 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100303
304 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100305 var BSSAP_N_UNITDATA_ind ud_ind;
306 var BSSAP_N_CONNECT_ind conn_ind;
307 var BSSAP_N_CONNECT_cfm conn_cfm;
308 var BSSAP_N_DATA_ind data_ind;
309 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100310 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100311 var BSSAP_ConnHdlr vc_conn;
312 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100313 var MgcpCommand mgcp_req;
314 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100315 var BSSAP_ConnHdlr vc_hdlr;
316 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100317
318 alt {
319 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100320 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100321 /* Connectionless Procedures like RESET */
322 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100323 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100324 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100325 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
326 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100327 }
328 }
329
330 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100331 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100332 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100333 /* store mapping between client components and SCCP connectionId */
334 f_conn_table_add(vc_conn, conn_ind.connectionId);
335 /* handle user payload */
336 f_handle_userData(vc_conn, conn_ind.userData);
337 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100338 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100339 }
340
341 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100342 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100343 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100344 if (ispresent(data_ind.userData)) {
345 f_handle_userData(vc_conn, data_ind.userData);
346 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100347 }
348
349 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100350 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100351 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100352 if (ispresent(disc_ind.userData)) {
353 f_handle_userData(vc_conn, disc_ind.userData);
354 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100355 /* notify client about termination */
356 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
357 CLIENT.send(prim) to vc_conn;
358 f_conn_table_del(disc_ind.connectionId);
359 /* TOOD: return confirm to other side? */
360 }
361
Harald Welteb3414b22017-11-23 18:22:10 +0100362 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100363 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100364 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100365 if (ispresent(conn_cfm.userData)) {
366 f_handle_userData(vc_conn, conn_cfm.userData);
367 }
Harald Welteb3414b22017-11-23 18:22:10 +0100368 }
369
Harald Welte365f4ed2017-11-23 00:00:43 +0100370 /* Disconnect request client -> SCCP */
371 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
372 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100373 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100374 f_conn_table_del(conn_id);
375 }
376
377 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100378 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
379 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100380 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100381
382 if (f_comp_known(vc_conn) == false) {
383 /* unknown client, create new connection */
384 conn_id := f_gen_conn_id();
385
386 /* store mapping between client components and SCCP connectionId */
387 f_conn_table_add(vc_conn, conn_id);
388
Harald Welte004f5fb2017-12-16 17:54:40 +0100389 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
390 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100391 } else {
392 /* known client, send via existing connection */
393 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100394 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100395 }
396
397 }
398
Harald Welte365f4ed2017-11-23 00:00:43 +0100399 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
400 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100401 /* send it to dispatcher */
402 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100403 }
404
Harald Weltec82eef42017-11-24 20:40:12 +0100405 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
406 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
407 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
408 * is printed as hex string, e.g. a@mgw for CIC 10 */
409
410 /* CLIENT -> MGCP */
411 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
412 /* MGCP request from Handler (we're MSC) */
413 /* store the transaction ID we've seen */
414 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
415 /* simply forward any MGCP from the client to the port */
416 MGCP.send(mgcp_req);
417 }
418 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
419 /* MGCP response from Handler (we're BSC/MGW) */
420 /* simply forward any MGCP from the client to the port */
421 MGCP.send(mgcp_resp);
422 }
423
424 /* MGCP -> CLIENT */
425 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
426 /* MGCP request from network side (we're BSC/MGW) */
427 /* Extract CIC from local part of endpoint name */
428 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100429 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
430 /* ignore RSIP for unknown CIC */
431 } else {
432 /* Resolve the vc_conn by the CIC */
433 vc_conn := f_comp_by_cic(cic);
434 CLIENT.send(mgcp_req) to vc_conn;
435 }
Harald Weltec82eef42017-11-24 20:40:12 +0100436 }
437 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
438 /* MGCP response from network side (we're MSC) */
439 /* Resolve the vc_conn by the transaction ID */
440 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
441 CLIENT.send(mgcp_resp) to vc_conn;
442 }
443
Harald Welte624f9632017-12-16 19:26:04 +0100444
445 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
446 f_create_expect(l3_info, vc_hdlr);
447 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
448 }
449
Harald Welte365f4ed2017-11-23 00:00:43 +0100450 }
451 }
452}
453
Harald Weltec82eef42017-11-24 20:40:12 +0100454private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100455 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100456 return hex2int(str2hex(local_part));
457
458}
Harald Welte365f4ed2017-11-23 00:00:43 +0100459
Harald Welte624f9632017-12-16 19:26:04 +0100460/***********************************************************************
461 * "Expect" Handling (mapping for expected incoming SCCP connections)
462 ***********************************************************************/
463
464/* data about an expected future incoming connection */
465type record ExpectData {
466 /* L3 payload based on which we can match it */
467 octetstring l3_payload optional,
468 /* component reference for this connection */
469 BSSAP_ConnHdlr vc_conn
470}
471
472/* procedure based port to register for incoming connections */
473signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
474
475type port BSSMAPEM_PROC_PT procedure {
476 inout BSSMAPEM_register;
477} with { extension "internal" };
478
479/* CreateCallback that can be used as create_cb and will use the expectation table */
480function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
481runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
482 var BSSAP_ConnHdlr ret := null;
483 var octetstring l3_info;
484 var integer i;
485
486 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
487 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
488 return ret;
489 }
490 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
491
492 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
493 if (not ispresent(ExpectTable[i].l3_payload)) {
494 continue;
495 }
496 if (l3_info == ExpectTable[i].l3_payload) {
497 ret := ExpectTable[i].vc_conn;
498 /* release this entry to be used again */
499 ExpectTable[i].l3_payload := omit;
500 ExpectTable[i].vc_conn := null;
501 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
502 /* return the component reference */
503 return ret;
504 }
505 }
506 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
507 return ret;
508}
509
510private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
511runs on BSSMAP_Emulation_CT {
512 var integer i;
513 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
514 if (not ispresent(ExpectTable[i].l3_payload)) {
515 ExpectTable[i].l3_payload := l3;
516 ExpectTable[i].vc_conn := hdlr;
517 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
518 return;
519 }
520 }
521 setverdict(fail, "No space left in ExpectTable");
522}
523
Daniel Willmannd47106b2018-01-17 12:20:56 +0100524private function f_expect_table_init()
525runs on BSSMAP_Emulation_CT {
526 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
527 ExpectTable[i].l3_payload := omit;
528 }
529}
Harald Welte624f9632017-12-16 19:26:04 +0100530
Harald Welte365f4ed2017-11-23 00:00:43 +0100531}