blob: 14bf238b8f7cf7523e7605caf025496df4ae8f5b [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module BSSMAP_Emulation {
2
Harald Welteb3414b22017-11-23 18:22:10 +01003import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +01004import from SCCPasp_Types all;
5import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +01006import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +01007import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +01008import from MGCP_Types all;
9import from MGCP_Templates all;
10import from IPA_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010011
12/* General "base class" component definition, of which specific implementations
13 * derive themselves by means of the "extends" feature */
14type component BSSAP_ConnHdlr {
15 /* port towards MSC Emulator core / SCCP connection dispatchar */
16 port BSSAP_Conn_PT BSSAP;
17}
18
19/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
20type enumerated BSSAP_Conn_Prim {
21 /* SCCP tell us that connection was released */
22 MSC_CONN_PRIM_DISC_IND,
23 /* we tell SCCP to release connection */
24 MSC_CONN_PRIM_DISC_REQ
25}
26
Harald Welteb3414b22017-11-23 18:22:10 +010027type record BSSAP_Conn_Req {
28 SCCP_PAR_Address addr_peer,
29 SCCP_PAR_Address addr_own,
30 PDU_BSSAP bssap
31}
32
Harald Welte365f4ed2017-11-23 00:00:43 +010033/* port between individual per-connection components and this dispatcher */
34type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010035 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010036} with { extension "internal" };
37
38
39/* represents a single BSSAP connection over SCCP */
40type record ConnectionData {
41 /* reference to the instance of the per-connection component */
42 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010043 integer sccp_conn_id,
44 /* most recent MGCP transaction ID (Used on MSC side) */
45 MgcpTransId mgcp_trans_id optional,
46 /* CIC that has been used for voice of this channel (BSC side) */
47 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010048}
49
50type component BSSMAP_Emulation_CT {
51 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +010052 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +010053 /* BSSAP port to the per-connection clients */
54 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010055 /* MGCP port */
56 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010057
58 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
59 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010060
Harald Weltebe620f62017-11-25 00:23:54 +010061 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010062 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010063};
64
Harald Welteb3414b22017-11-23 18:22:10 +010065private function f_conn_id_known(integer sccp_conn_id)
66runs on BSSMAP_Emulation_CT return boolean {
67 var integer i;
68 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
69 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
70 return true;
71 }
72 }
73 return false;
74}
75
76private function f_comp_known(BSSAP_ConnHdlr client)
77runs on BSSMAP_Emulation_CT return boolean {
78 var integer i;
79 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
80 if (ConnectionTable[i].comp_ref == client) {
81 return true;
82 }
83 }
84 return false;
85}
Harald Welte365f4ed2017-11-23 00:00:43 +010086
Harald Weltee98bb2e2017-11-29 12:09:48 +010087private function f_cic_known(integer cic)
88runs on BSSMAP_Emulation_CT return boolean {
89 var integer i;
90 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
91 if (ConnectionTable[i].cic == cic) {
92 return true;
93 }
94 }
95 return false;
96}
97
Harald Welte365f4ed2017-11-23 00:00:43 +010098/* resolve component reference by connection ID */
99private function f_comp_by_conn_id(integer sccp_conn_id)
100runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
101 var integer i;
102 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
103 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
104 return ConnectionTable[i].comp_ref;
105 }
106 }
107 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100108 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100109 self.stop;
110}
111
Harald Weltec82eef42017-11-24 20:40:12 +0100112/* resolve component reference by CIC */
113private function f_comp_by_mgcp_tid(MgcpTransId tid)
114runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
115 var integer i;
116 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
117 if (ConnectionTable[i].mgcp_trans_id == tid) {
118 return ConnectionTable[i].comp_ref;
119 }
120 }
121 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100122 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100123 self.stop;
124}
125
126private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
127runs on BSSMAP_Emulation_CT {
128 var integer i;
129 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
130 if (ConnectionTable[i].comp_ref == client) {
131 ConnectionTable[i].mgcp_trans_id := tid;
132 return;
133 }
134 }
135 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100136 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100137 self.stop;
138}
139
140private function f_comp_by_cic(integer cic)
141runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
142 var integer i;
143 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
144 if (ConnectionTable[i].cic == cic) {
145 return ConnectionTable[i].comp_ref;
146 }
147 }
148 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100149 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100150 self.stop;
151}
152
153private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
154runs on BSSMAP_Emulation_CT {
155 var integer i;
156 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
157 if (ConnectionTable[i].comp_ref == client) {
158 ConnectionTable[i].cic := cic;
159 return;
160 }
161 }
162 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100163 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100164}
165
Harald Welte365f4ed2017-11-23 00:00:43 +0100166/* resolve connection ID by component reference */
167private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
168runs on BSSMAP_Emulation_CT return integer {
169 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
170 if (ConnectionTable[i].comp_ref == client) {
171 return ConnectionTable[i].sccp_conn_id;
172 }
173 }
174 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100175 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100176 self.stop;
177}
178
Harald Welteb3414b22017-11-23 18:22:10 +0100179private function f_gen_conn_id()
180runs on BSSMAP_Emulation_CT return integer {
181 var integer conn_id;
182
183 do {
184 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
185 } while (f_conn_id_known(conn_id) == true);
186
187 return conn_id;
188}
189
Harald Welte365f4ed2017-11-23 00:00:43 +0100190private function f_conn_table_init()
191runs on BSSMAP_Emulation_CT {
192 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
193 ConnectionTable[i].comp_ref := null;
194 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100195 ConnectionTable[i].mgcp_trans_id := omit;
196 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100197 }
198}
199
200private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
201runs on BSSMAP_Emulation_CT {
202 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
203 if (ConnectionTable[i].sccp_conn_id == -1) {
204 ConnectionTable[i].comp_ref := comp_ref;
205 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100206 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100207 return;
208 }
209 }
210 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100211 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100212 self.stop;
213}
214
215private function f_conn_table_del(integer sccp_conn_id)
216runs on BSSMAP_Emulation_CT {
217 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
218 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100219 log("Deleted conn table entry ", i,
220 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100221 ConnectionTable[i].sccp_conn_id := -1;
222 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100223 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100224 }
225 }
226 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100227 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100228 self.stop;
229}
230
231/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100232private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100233runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100234 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100235
236 /* BSC Side: If this is an assignment command, store CIC */
237 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
238 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
239 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
240 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
241 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
242 f_comp_store_cic(client, cic);
243 }
244
Harald Welte365f4ed2017-11-23 00:00:43 +0100245 CLIENT.send(bssap) to client;
246}
247
248/* call-back type, to be provided by specific implementation; called when new SCCP connection
249 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100250type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100251runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
252
253type function BssmapUnitdataCallback(PDU_BSSAP bssap)
254runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
255
256type record BssmapOps {
257 BssmapCreateCallback create_cb,
258 BssmapUnitdataCallback unitdata_cb
259}
260
Harald Weltebe620f62017-11-25 00:23:54 +0100261function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100262
Harald Weltebe620f62017-11-25 00:23:54 +0100263 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100264 f_conn_table_init();
265
266 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100267 var BSSAP_N_UNITDATA_ind ud_ind;
268 var BSSAP_N_CONNECT_ind conn_ind;
269 var BSSAP_N_CONNECT_cfm conn_cfm;
270 var BSSAP_N_DATA_ind data_ind;
271 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100272 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100273 var BSSAP_ConnHdlr vc_conn;
274 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100275 var MgcpCommand mgcp_req;
276 var MgcpResponse mgcp_resp;
Harald Welte365f4ed2017-11-23 00:00:43 +0100277
278 alt {
279 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100280 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100281 /* Connectionless Procedures like RESET */
282 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100283 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100284 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100285 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
286 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100287 }
288 }
289
290 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100291 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100292 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100293 /* store mapping between client components and SCCP connectionId */
294 f_conn_table_add(vc_conn, conn_ind.connectionId);
295 /* handle user payload */
296 f_handle_userData(vc_conn, conn_ind.userData);
297 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100298 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 }
300
301 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100302 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100303 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100304 if (ispresent(data_ind.userData)) {
305 f_handle_userData(vc_conn, data_ind.userData);
306 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100307 }
308
309 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100310 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100311 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100312 if (ispresent(disc_ind.userData)) {
313 f_handle_userData(vc_conn, disc_ind.userData);
314 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100315 /* notify client about termination */
316 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
317 CLIENT.send(prim) to vc_conn;
318 f_conn_table_del(disc_ind.connectionId);
319 /* TOOD: return confirm to other side? */
320 }
321
Harald Welteb3414b22017-11-23 18:22:10 +0100322 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100323 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100324 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100325 if (ispresent(conn_cfm.userData)) {
326 f_handle_userData(vc_conn, conn_cfm.userData);
327 }
Harald Welteb3414b22017-11-23 18:22:10 +0100328 }
329
Harald Welte365f4ed2017-11-23 00:00:43 +0100330 /* Disconnect request client -> SCCP */
331 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
332 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100333 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100334 f_conn_table_del(conn_id);
335 }
336
337 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100338 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
339 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100340 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100341
342 if (f_comp_known(vc_conn) == false) {
343 /* unknown client, create new connection */
344 conn_id := f_gen_conn_id();
345
346 /* store mapping between client components and SCCP connectionId */
347 f_conn_table_add(vc_conn, conn_id);
348
Harald Welte004f5fb2017-12-16 17:54:40 +0100349 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
350 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100351 } else {
352 /* known client, send via existing connection */
353 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100354 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100355 }
356
357 }
358
Harald Welte365f4ed2017-11-23 00:00:43 +0100359 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
360 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100361 /* send it to dispatcher */
362 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100363 }
364
Harald Weltec82eef42017-11-24 20:40:12 +0100365 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
366 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
367 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
368 * is printed as hex string, e.g. a@mgw for CIC 10 */
369
370 /* CLIENT -> MGCP */
371 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
372 /* MGCP request from Handler (we're MSC) */
373 /* store the transaction ID we've seen */
374 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
375 /* simply forward any MGCP from the client to the port */
376 MGCP.send(mgcp_req);
377 }
378 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
379 /* MGCP response from Handler (we're BSC/MGW) */
380 /* simply forward any MGCP from the client to the port */
381 MGCP.send(mgcp_resp);
382 }
383
384 /* MGCP -> CLIENT */
385 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
386 /* MGCP request from network side (we're BSC/MGW) */
387 /* Extract CIC from local part of endpoint name */
388 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100389 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
390 /* ignore RSIP for unknown CIC */
391 } else {
392 /* Resolve the vc_conn by the CIC */
393 vc_conn := f_comp_by_cic(cic);
394 CLIENT.send(mgcp_req) to vc_conn;
395 }
Harald Weltec82eef42017-11-24 20:40:12 +0100396 }
397 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
398 /* MGCP response from network side (we're MSC) */
399 /* Resolve the vc_conn by the transaction ID */
400 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
401 CLIENT.send(mgcp_resp) to vc_conn;
402 }
403
Harald Welte365f4ed2017-11-23 00:00:43 +0100404 }
405 }
406}
407
Harald Weltec82eef42017-11-24 20:40:12 +0100408private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100409 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100410 return hex2int(str2hex(local_part));
411
412}
Harald Welte365f4ed2017-11-23 00:00:43 +0100413
414}