blob: 8475d552cfe626cb5106e80cc5076ca2ba0666cf [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;
47}
48
49/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
50type enumerated BSSAP_Conn_Prim {
51 /* SCCP tell us that connection was released */
52 MSC_CONN_PRIM_DISC_IND,
53 /* we tell SCCP to release connection */
54 MSC_CONN_PRIM_DISC_REQ
55}
56
Harald Welteb3414b22017-11-23 18:22:10 +010057type record BSSAP_Conn_Req {
58 SCCP_PAR_Address addr_peer,
59 SCCP_PAR_Address addr_own,
60 PDU_BSSAP bssap
61}
62
Harald Welte365f4ed2017-11-23 00:00:43 +010063/* port between individual per-connection components and this dispatcher */
64type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010065 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010066} with { extension "internal" };
67
68
69/* represents a single BSSAP connection over SCCP */
70type record ConnectionData {
71 /* reference to the instance of the per-connection component */
72 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010073 integer sccp_conn_id,
74 /* most recent MGCP transaction ID (Used on MSC side) */
75 MgcpTransId mgcp_trans_id optional,
76 /* CIC that has been used for voice of this channel (BSC side) */
77 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010078}
79
80type component BSSMAP_Emulation_CT {
81 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +010082 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +010083 /* BSSAP port to the per-connection clients */
84 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010085 /* MGCP port */
86 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010087
88 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
89 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010090
Harald Welte624f9632017-12-16 19:26:04 +010091 /* pending expected incoming connections */
92 var ExpectData ExpectTable[8];
93 /* procedure based port to register for incoming connections */
94 port BSSMAPEM_PROC_PT PROC;
95
Harald Weltebe620f62017-11-25 00:23:54 +010096 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010097 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010098};
99
Harald Welteb3414b22017-11-23 18:22:10 +0100100private function f_conn_id_known(integer sccp_conn_id)
101runs on BSSMAP_Emulation_CT return boolean {
102 var integer i;
103 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
104 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
105 return true;
106 }
107 }
108 return false;
109}
110
111private function f_comp_known(BSSAP_ConnHdlr client)
112runs on BSSMAP_Emulation_CT return boolean {
113 var integer i;
114 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
115 if (ConnectionTable[i].comp_ref == client) {
116 return true;
117 }
118 }
119 return false;
120}
Harald Welte365f4ed2017-11-23 00:00:43 +0100121
Harald Weltee98bb2e2017-11-29 12:09:48 +0100122private function f_cic_known(integer cic)
123runs on BSSMAP_Emulation_CT return boolean {
124 var integer i;
125 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
126 if (ConnectionTable[i].cic == cic) {
127 return true;
128 }
129 }
130 return false;
131}
132
Harald Welte365f4ed2017-11-23 00:00:43 +0100133/* resolve component reference by connection ID */
134private function f_comp_by_conn_id(integer sccp_conn_id)
135runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
136 var integer i;
137 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
138 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
139 return ConnectionTable[i].comp_ref;
140 }
141 }
142 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100143 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100144 self.stop;
145}
146
Harald Weltec82eef42017-11-24 20:40:12 +0100147/* resolve component reference by CIC */
148private function f_comp_by_mgcp_tid(MgcpTransId tid)
149runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
150 var integer i;
151 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
152 if (ConnectionTable[i].mgcp_trans_id == tid) {
153 return ConnectionTable[i].comp_ref;
154 }
155 }
156 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100157 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100158 self.stop;
159}
160
161private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
162runs on BSSMAP_Emulation_CT {
163 var integer i;
164 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
165 if (ConnectionTable[i].comp_ref == client) {
166 ConnectionTable[i].mgcp_trans_id := tid;
167 return;
168 }
169 }
170 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100171 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100172 self.stop;
173}
174
175private function f_comp_by_cic(integer cic)
176runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
177 var integer i;
178 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
179 if (ConnectionTable[i].cic == cic) {
180 return ConnectionTable[i].comp_ref;
181 }
182 }
183 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100184 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100185 self.stop;
186}
187
188private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
189runs on BSSMAP_Emulation_CT {
190 var integer i;
191 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
192 if (ConnectionTable[i].comp_ref == client) {
193 ConnectionTable[i].cic := cic;
194 return;
195 }
196 }
197 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100198 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100199}
200
Harald Welte365f4ed2017-11-23 00:00:43 +0100201/* resolve connection ID by component reference */
202private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
203runs on BSSMAP_Emulation_CT return integer {
204 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
205 if (ConnectionTable[i].comp_ref == client) {
206 return ConnectionTable[i].sccp_conn_id;
207 }
208 }
209 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100210 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100211 self.stop;
212}
213
Harald Welteb3414b22017-11-23 18:22:10 +0100214private function f_gen_conn_id()
215runs on BSSMAP_Emulation_CT return integer {
216 var integer conn_id;
217
218 do {
219 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
220 } while (f_conn_id_known(conn_id) == true);
221
222 return conn_id;
223}
224
Harald Welte365f4ed2017-11-23 00:00:43 +0100225private function f_conn_table_init()
226runs on BSSMAP_Emulation_CT {
227 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
228 ConnectionTable[i].comp_ref := null;
229 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100230 ConnectionTable[i].mgcp_trans_id := omit;
231 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100232 }
233}
234
235private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
236runs on BSSMAP_Emulation_CT {
237 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
238 if (ConnectionTable[i].sccp_conn_id == -1) {
239 ConnectionTable[i].comp_ref := comp_ref;
240 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100241 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100242 return;
243 }
244 }
245 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100246 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100247 self.stop;
248}
249
250private function f_conn_table_del(integer sccp_conn_id)
251runs on BSSMAP_Emulation_CT {
252 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
253 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100254 log("Deleted conn table entry ", i,
255 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100256 ConnectionTable[i].sccp_conn_id := -1;
257 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100258 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100259 }
260 }
261 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100262 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100263 self.stop;
264}
265
266/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100267private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100268runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100269 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100270
271 /* BSC Side: If this is an assignment command, store CIC */
272 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
273 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
274 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
275 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
276 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
277 f_comp_store_cic(client, cic);
278 }
279
Harald Welte365f4ed2017-11-23 00:00:43 +0100280 CLIENT.send(bssap) to client;
281}
282
283/* call-back type, to be provided by specific implementation; called when new SCCP connection
284 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100285type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100286runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
287
288type function BssmapUnitdataCallback(PDU_BSSAP bssap)
289runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
290
291type record BssmapOps {
292 BssmapCreateCallback create_cb,
293 BssmapUnitdataCallback unitdata_cb
294}
295
Harald Weltebe620f62017-11-25 00:23:54 +0100296function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100297
Harald Weltebe620f62017-11-25 00:23:54 +0100298 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 f_conn_table_init();
Daniel Willmannd47106b2018-01-17 12:20:56 +0100300 f_expect_table_init();
Harald Welte365f4ed2017-11-23 00:00:43 +0100301
302 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100303 var BSSAP_N_UNITDATA_ind ud_ind;
304 var BSSAP_N_CONNECT_ind conn_ind;
305 var BSSAP_N_CONNECT_cfm conn_cfm;
306 var BSSAP_N_DATA_ind data_ind;
307 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100308 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100309 var BSSAP_ConnHdlr vc_conn;
310 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100311 var MgcpCommand mgcp_req;
312 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100313 var BSSAP_ConnHdlr vc_hdlr;
314 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100315
316 alt {
317 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100318 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100319 /* Connectionless Procedures like RESET */
320 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100321 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100322 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100323 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
324 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100325 }
326 }
327
328 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100329 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100330 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100331 /* store mapping between client components and SCCP connectionId */
332 f_conn_table_add(vc_conn, conn_ind.connectionId);
333 /* handle user payload */
334 f_handle_userData(vc_conn, conn_ind.userData);
335 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100336 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100337 }
338
339 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100340 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100341 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100342 if (ispresent(data_ind.userData)) {
343 f_handle_userData(vc_conn, data_ind.userData);
344 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100345 }
346
347 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100348 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100349 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100350 if (ispresent(disc_ind.userData)) {
351 f_handle_userData(vc_conn, disc_ind.userData);
352 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100353 /* notify client about termination */
354 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
355 CLIENT.send(prim) to vc_conn;
356 f_conn_table_del(disc_ind.connectionId);
357 /* TOOD: return confirm to other side? */
358 }
359
Harald Welteb3414b22017-11-23 18:22:10 +0100360 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100361 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100362 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100363 if (ispresent(conn_cfm.userData)) {
364 f_handle_userData(vc_conn, conn_cfm.userData);
365 }
Harald Welteb3414b22017-11-23 18:22:10 +0100366 }
367
Harald Welte365f4ed2017-11-23 00:00:43 +0100368 /* Disconnect request client -> SCCP */
369 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
370 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100371 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100372 f_conn_table_del(conn_id);
373 }
374
375 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100376 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
377 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100378 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100379
380 if (f_comp_known(vc_conn) == false) {
381 /* unknown client, create new connection */
382 conn_id := f_gen_conn_id();
383
384 /* store mapping between client components and SCCP connectionId */
385 f_conn_table_add(vc_conn, conn_id);
386
Harald Welte004f5fb2017-12-16 17:54:40 +0100387 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
388 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100389 } else {
390 /* known client, send via existing connection */
391 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100392 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100393 }
394
395 }
396
Harald Welte365f4ed2017-11-23 00:00:43 +0100397 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
398 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100399 /* send it to dispatcher */
400 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100401 }
402
Harald Weltec82eef42017-11-24 20:40:12 +0100403 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
404 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
405 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
406 * is printed as hex string, e.g. a@mgw for CIC 10 */
407
408 /* CLIENT -> MGCP */
409 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
410 /* MGCP request from Handler (we're MSC) */
411 /* store the transaction ID we've seen */
412 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
413 /* simply forward any MGCP from the client to the port */
414 MGCP.send(mgcp_req);
415 }
416 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
417 /* MGCP response from Handler (we're BSC/MGW) */
418 /* simply forward any MGCP from the client to the port */
419 MGCP.send(mgcp_resp);
420 }
421
422 /* MGCP -> CLIENT */
423 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
424 /* MGCP request from network side (we're BSC/MGW) */
425 /* Extract CIC from local part of endpoint name */
426 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100427 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
428 /* ignore RSIP for unknown CIC */
429 } else {
430 /* Resolve the vc_conn by the CIC */
431 vc_conn := f_comp_by_cic(cic);
432 CLIENT.send(mgcp_req) to vc_conn;
433 }
Harald Weltec82eef42017-11-24 20:40:12 +0100434 }
435 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
436 /* MGCP response from network side (we're MSC) */
437 /* Resolve the vc_conn by the transaction ID */
438 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
439 CLIENT.send(mgcp_resp) to vc_conn;
440 }
441
Harald Welte624f9632017-12-16 19:26:04 +0100442
443 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
444 f_create_expect(l3_info, vc_hdlr);
445 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
446 }
447
Harald Welte365f4ed2017-11-23 00:00:43 +0100448 }
449 }
450}
451
Harald Weltec82eef42017-11-24 20:40:12 +0100452private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100453 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100454 return hex2int(str2hex(local_part));
455
456}
Harald Welte365f4ed2017-11-23 00:00:43 +0100457
Harald Welte624f9632017-12-16 19:26:04 +0100458/***********************************************************************
459 * "Expect" Handling (mapping for expected incoming SCCP connections)
460 ***********************************************************************/
461
462/* data about an expected future incoming connection */
463type record ExpectData {
464 /* L3 payload based on which we can match it */
465 octetstring l3_payload optional,
466 /* component reference for this connection */
467 BSSAP_ConnHdlr vc_conn
468}
469
470/* procedure based port to register for incoming connections */
471signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
472
473type port BSSMAPEM_PROC_PT procedure {
474 inout BSSMAPEM_register;
475} with { extension "internal" };
476
477/* CreateCallback that can be used as create_cb and will use the expectation table */
478function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
479runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
480 var BSSAP_ConnHdlr ret := null;
481 var octetstring l3_info;
482 var integer i;
483
484 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
485 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
486 return ret;
487 }
488 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
489
490 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
491 if (not ispresent(ExpectTable[i].l3_payload)) {
492 continue;
493 }
494 if (l3_info == ExpectTable[i].l3_payload) {
495 ret := ExpectTable[i].vc_conn;
496 /* release this entry to be used again */
497 ExpectTable[i].l3_payload := omit;
498 ExpectTable[i].vc_conn := null;
499 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
500 /* return the component reference */
501 return ret;
502 }
503 }
504 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
505 return ret;
506}
507
508private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
509runs on BSSMAP_Emulation_CT {
510 var integer i;
511 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
512 if (not ispresent(ExpectTable[i].l3_payload)) {
513 ExpectTable[i].l3_payload := l3;
514 ExpectTable[i].vc_conn := hdlr;
515 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
516 return;
517 }
518 }
519 setverdict(fail, "No space left in ExpectTable");
520}
521
Daniel Willmannd47106b2018-01-17 12:20:56 +0100522private function f_expect_table_init()
523runs on BSSMAP_Emulation_CT {
524 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
525 ExpectTable[i].l3_payload := omit;
526 }
527}
Harald Welte624f9632017-12-16 19:26:04 +0100528
Harald Welte365f4ed2017-11-23 00:00:43 +0100529}