blob: d6c3aed566580d4c71e50352e0f6efcd31712bdf [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;
6import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +01007import from MGCP_Types all;
8import from MGCP_Templates all;
9import from IPA_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010010
11/* General "base class" component definition, of which specific implementations
12 * derive themselves by means of the "extends" feature */
13type component BSSAP_ConnHdlr {
14 /* port towards MSC Emulator core / SCCP connection dispatchar */
15 port BSSAP_Conn_PT BSSAP;
16}
17
18/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
19type enumerated BSSAP_Conn_Prim {
20 /* SCCP tell us that connection was released */
21 MSC_CONN_PRIM_DISC_IND,
22 /* we tell SCCP to release connection */
23 MSC_CONN_PRIM_DISC_REQ
24}
25
Harald Welteb3414b22017-11-23 18:22:10 +010026type record BSSAP_Conn_Req {
27 SCCP_PAR_Address addr_peer,
28 SCCP_PAR_Address addr_own,
29 PDU_BSSAP bssap
30}
31
Harald Welte365f4ed2017-11-23 00:00:43 +010032/* port between individual per-connection components and this dispatcher */
33type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010034 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010035} with { extension "internal" };
36
37
38/* represents a single BSSAP connection over SCCP */
39type record ConnectionData {
40 /* reference to the instance of the per-connection component */
41 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010042 integer sccp_conn_id,
43 /* most recent MGCP transaction ID (Used on MSC side) */
44 MgcpTransId mgcp_trans_id optional,
45 /* CIC that has been used for voice of this channel (BSC side) */
46 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010047}
48
49type component BSSMAP_Emulation_CT {
50 /* SCCP port on the bottom side, using ASP primitives */
51 port SCCPasp_PT SCCP;
52 /* BSSAP port to the per-connection clients */
53 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010054 /* MGCP port */
55 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010056
57 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
58 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010059
Harald Weltebe620f62017-11-25 00:23:54 +010060 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010061 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010062};
63
Harald Welteb3414b22017-11-23 18:22:10 +010064private function f_conn_id_known(integer sccp_conn_id)
65runs on BSSMAP_Emulation_CT return boolean {
66 var integer i;
67 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
68 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
69 return true;
70 }
71 }
72 return false;
73}
74
75private function f_comp_known(BSSAP_ConnHdlr client)
76runs on BSSMAP_Emulation_CT return boolean {
77 var integer i;
78 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
79 if (ConnectionTable[i].comp_ref == client) {
80 return true;
81 }
82 }
83 return false;
84}
Harald Welte365f4ed2017-11-23 00:00:43 +010085
86/* resolve component reference by connection ID */
87private function f_comp_by_conn_id(integer sccp_conn_id)
88runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
89 var integer i;
90 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
91 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
92 return ConnectionTable[i].comp_ref;
93 }
94 }
95 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +010096 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +010097 self.stop;
98}
99
Harald Weltec82eef42017-11-24 20:40:12 +0100100/* resolve component reference by CIC */
101private function f_comp_by_mgcp_tid(MgcpTransId tid)
102runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
103 var integer i;
104 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
105 if (ConnectionTable[i].mgcp_trans_id == tid) {
106 return ConnectionTable[i].comp_ref;
107 }
108 }
109 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100110 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100111 self.stop;
112}
113
114private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
115runs on BSSMAP_Emulation_CT {
116 var integer i;
117 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
118 if (ConnectionTable[i].comp_ref == client) {
119 ConnectionTable[i].mgcp_trans_id := tid;
120 return;
121 }
122 }
123 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100124 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100125 self.stop;
126}
127
128private function f_comp_by_cic(integer cic)
129runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
130 var integer i;
131 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
132 if (ConnectionTable[i].cic == cic) {
133 return ConnectionTable[i].comp_ref;
134 }
135 }
136 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100137 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100138 self.stop;
139}
140
141private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
142runs on BSSMAP_Emulation_CT {
143 var integer i;
144 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
145 if (ConnectionTable[i].comp_ref == client) {
146 ConnectionTable[i].cic := cic;
147 return;
148 }
149 }
150 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100151 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100152}
153
Harald Welte365f4ed2017-11-23 00:00:43 +0100154/* resolve connection ID by component reference */
155private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
156runs on BSSMAP_Emulation_CT return integer {
157 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
158 if (ConnectionTable[i].comp_ref == client) {
159 return ConnectionTable[i].sccp_conn_id;
160 }
161 }
162 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100163 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100164 self.stop;
165}
166
Harald Welteb3414b22017-11-23 18:22:10 +0100167private function f_gen_conn_id()
168runs on BSSMAP_Emulation_CT return integer {
169 var integer conn_id;
170
171 do {
172 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
173 } while (f_conn_id_known(conn_id) == true);
174
175 return conn_id;
176}
177
Harald Welte365f4ed2017-11-23 00:00:43 +0100178private function f_conn_table_init()
179runs on BSSMAP_Emulation_CT {
180 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
181 ConnectionTable[i].comp_ref := null;
182 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100183 ConnectionTable[i].mgcp_trans_id := omit;
184 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100185 }
186}
187
188private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
189runs on BSSMAP_Emulation_CT {
190 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
191 if (ConnectionTable[i].sccp_conn_id == -1) {
192 ConnectionTable[i].comp_ref := comp_ref;
193 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100194 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100195 return;
196 }
197 }
198 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100199 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100200 self.stop;
201}
202
203private function f_conn_table_del(integer sccp_conn_id)
204runs on BSSMAP_Emulation_CT {
205 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
206 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100207 log("Deleted conn table entry ", i,
208 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100209 ConnectionTable[i].sccp_conn_id := -1;
210 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100211 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100212 }
213 }
214 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100215 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100216 self.stop;
217}
218
219/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100220private function f_handle_userData(BSSAP_ConnHdlr client, octetstring userdata)
Harald Welte365f4ed2017-11-23 00:00:43 +0100221runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100222 /* decode + send decoded BSSAP to client */
223 var PDU_BSSAP bssap := dec_PDU_BSSAP(valueof(userdata));
Harald Welte1b2748e2017-11-24 23:40:16 +0100224
225 /* BSC Side: If this is an assignment command, store CIC */
226 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
227 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
228 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
229 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
230 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
231 f_comp_store_cic(client, cic);
232 }
233
Harald Welte365f4ed2017-11-23 00:00:43 +0100234 CLIENT.send(bssap) to client;
235}
236
237/* call-back type, to be provided by specific implementation; called when new SCCP connection
238 * arrives */
Harald Weltebe620f62017-11-25 00:23:54 +0100239type function BssmapCreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100240runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
241
242type function BssmapUnitdataCallback(PDU_BSSAP bssap)
243runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
244
245type record BssmapOps {
246 BssmapCreateCallback create_cb,
247 BssmapUnitdataCallback unitdata_cb
248}
249
Harald Weltebe620f62017-11-25 00:23:54 +0100250function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100251
Harald Weltebe620f62017-11-25 00:23:54 +0100252 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100253 f_conn_table_init();
254
255 while (true) {
256 var ASP_SCCP_N_UNITDATA_ind ud_ind;
257 var ASP_SCCP_N_CONNECT_ind conn_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100258 var ASP_SCCP_N_CONNECT_cfm conn_cfm;
Harald Welte365f4ed2017-11-23 00:00:43 +0100259 var ASP_SCCP_N_DATA_ind data_ind;
260 var ASP_SCCP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100261 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100262 var BSSAP_ConnHdlr vc_conn;
263 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100264 var MgcpCommand mgcp_req;
265 var MgcpResponse mgcp_resp;
Harald Welte365f4ed2017-11-23 00:00:43 +0100266
267 alt {
268 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
269 [] SCCP.receive(ASP_SCCP_N_UNITDATA_ind:?) -> value ud_ind {
270 /* Connectionless Procedures like RESET */
271 var template PDU_BSSAP resp;
272 bssap := dec_PDU_BSSAP(ud_ind.userData);
273 resp := ops.unitdata_cb.apply(bssap);
274 if (isvalue(resp)) {
275 var octetstring resp_ud := enc_PDU_BSSAP(valueof(resp));
276 SCCP.send(t_ASP_N_UNITDATA_req(ud_ind.callingAddress,
277 ud_ind.calledAddress, omit,
278 omit, resp_ud, omit));
279 }
280 }
281
282 /* SCCP -> Client: new connection from BSC */
283 [] SCCP.receive(ASP_SCCP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100284 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100285 /* store mapping between client components and SCCP connectionId */
286 f_conn_table_add(vc_conn, conn_ind.connectionId);
287 /* handle user payload */
288 f_handle_userData(vc_conn, conn_ind.userData);
289 /* confirm connection establishment */
290 SCCP.send(t_ASP_N_CONNECT_res(omit, omit, omit, omit, conn_ind.connectionId, omit));
291 }
292
293 /* SCCP -> Client: connection-oriented data in existing connection */
294 [] SCCP.receive(ASP_SCCP_N_DATA_ind:?) -> value data_ind {
295 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100296 if (ispresent(data_ind.userData)) {
297 f_handle_userData(vc_conn, data_ind.userData);
298 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 }
300
301 /* SCCP -> Client: disconnect of an existing connection */
302 [] SCCP.receive(ASP_SCCP_N_DISCONNECT_ind:?) -> value disc_ind {
303 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100304 if (ispresent(disc_ind.userData)) {
305 f_handle_userData(vc_conn, disc_ind.userData);
306 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100307 /* notify client about termination */
308 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
309 CLIENT.send(prim) to vc_conn;
310 f_conn_table_del(disc_ind.connectionId);
311 /* TOOD: return confirm to other side? */
312 }
313
Harald Welteb3414b22017-11-23 18:22:10 +0100314 /* SCCP -> Client: connection confirm for outbound connection */
315 [] SCCP.receive(ASP_SCCP_N_CONNECT_cfm:?) -> value conn_cfm {
316 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100317 if (ispresent(conn_cfm.userData)) {
318 f_handle_userData(vc_conn, conn_cfm.userData);
319 }
Harald Welteb3414b22017-11-23 18:22:10 +0100320 }
321
Harald Welte365f4ed2017-11-23 00:00:43 +0100322 /* Disconnect request client -> SCCP */
323 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
324 var integer conn_id := f_conn_id_by_comp(vc_conn);
325 SCCP.send(t_ASP_N_DISCONNECT_req(omit, 0, omit, conn_id, omit));
326 f_conn_table_del(conn_id);
327 }
328
329 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100330 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
331 var integer conn_id;
332 /* encode + send to dispatcher */
333 var octetstring userdata := enc_PDU_BSSAP(creq.bssap);
334
335 if (f_comp_known(vc_conn) == false) {
336 /* unknown client, create new connection */
337 conn_id := f_gen_conn_id();
338
339 /* store mapping between client components and SCCP connectionId */
340 f_conn_table_add(vc_conn, conn_id);
341
342 SCCP.send(t_ASP_N_CONNECT_req(creq.addr_peer, creq.addr_own, omit, omit,
343 userdata, conn_id, omit));
344 } else {
345 /* known client, send via existing connection */
346 conn_id := f_conn_id_by_comp(vc_conn);
347 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
348 }
349
350 }
351
Harald Welte365f4ed2017-11-23 00:00:43 +0100352 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
353 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welteb3414b22017-11-23 18:22:10 +0100354 /* encode + send it to dispatcher */
Harald Welte365f4ed2017-11-23 00:00:43 +0100355 var octetstring userdata := enc_PDU_BSSAP(bssap);
356 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
357 }
358
Harald Weltec82eef42017-11-24 20:40:12 +0100359 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
360 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
361 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
362 * is printed as hex string, e.g. a@mgw for CIC 10 */
363
364 /* CLIENT -> MGCP */
365 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
366 /* MGCP request from Handler (we're MSC) */
367 /* store the transaction ID we've seen */
368 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
369 /* simply forward any MGCP from the client to the port */
370 MGCP.send(mgcp_req);
371 }
372 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
373 /* MGCP response from Handler (we're BSC/MGW) */
374 /* simply forward any MGCP from the client to the port */
375 MGCP.send(mgcp_resp);
376 }
377
378 /* MGCP -> CLIENT */
379 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
380 /* MGCP request from network side (we're BSC/MGW) */
381 /* Extract CIC from local part of endpoint name */
382 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
383 /* Resolve the vc_conn by the CIC */
384 vc_conn := f_comp_by_cic(cic);
385 CLIENT.send(mgcp_req) to vc_conn;
386 }
387 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
388 /* MGCP response from network side (we're MSC) */
389 /* Resolve the vc_conn by the transaction ID */
390 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
391 CLIENT.send(mgcp_resp) to vc_conn;
392 }
393
Harald Welte365f4ed2017-11-23 00:00:43 +0100394 }
395 }
396}
397
Harald Weltec82eef42017-11-24 20:40:12 +0100398private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100399 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100400 return hex2int(str2hex(local_part));
401
402}
Harald Welte365f4ed2017-11-23 00:00:43 +0100403
404}