blob: e0911331e9d51a1196629147265be2b074b99694 [file] [log] [blame]
Harald Welte6811d102019-04-14 22:23:14 +02001module RAN_Emulation {
Harald Welte365f4ed2017-11-23 00:00:43 +01002
Harald Welte6811d102019-04-14 22:23:14 +02003/* RAN Emulation, runs on top of BSSAP_CodecPort. It multiplexes/demultiplexes
Harald Welte35bb7162018-01-03 21:07:52 +01004 * the individual connections, so there can be separate TTCN-3 components handling
5 * each of the connections.
6 *
Harald Welte6811d102019-04-14 22:23:14 +02007 * The RAN_Emulation.main() function processes SCCP primitives from the SCCP
Harald Welte35bb7162018-01-03 21:07:52 +01008 * 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
Harald Welte6811d102019-04-14 22:23:14 +020011 * to the component running the RAN_Emulation.main() function.
Harald Welte35bb7162018-01-03 21:07:52 +010012 *
Harald Welte6811d102019-04-14 22:23:14 +020013 * For each new inbound connections, the RanOps.create_cb() is called. It can create
Harald Welte35bb7162018-01-03 21:07:52 +010014 * 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 *
Harald Welte6811d102019-04-14 22:23:14 +020022 * Inbound Unit Data messages (such as are dispatched to the RanOps.unitdata_cb() callback,
Harald Welte35bb7162018-01-03 21:07:52 +010023 * which is registered with an argument to the main() function below.
24 *
Harald Welte2fce7882019-04-15 11:48:05 +020025 * (C) 2017-2019 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 Welte9dadc522018-02-06 13:56:04 +010034import from Osmocom_Types all;
Harald Welteb3414b22017-11-23 18:22:10 +010035import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010036import from SCCPasp_Types all;
Harald Welte2fce7882019-04-15 11:48:05 +020037import from IPA_Emulation all;
38import from MobileL3_Types all;
39
40#ifdef RAN_EMULATION_BSSAP
Harald Welte365f4ed2017-11-23 00:00:43 +010041import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010042import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010043import from BSSMAP_Templates all;
Harald Welte2fce7882019-04-15 11:48:05 +020044#endif
45
46#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +010047import from MGCP_Types all;
48import from MGCP_Templates all;
Harald Welte2fce7882019-04-15 11:48:05 +020049#endif
Harald Welte365f4ed2017-11-23 00:00:43 +010050
51/* General "base class" component definition, of which specific implementations
52 * derive themselves by means of the "extends" feature */
Harald Welte6811d102019-04-14 22:23:14 +020053type component RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +010054 /* port towards MSC Emulator core / SCCP connection dispatchar */
Harald Welte6811d102019-04-14 22:23:14 +020055 port RAN_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010056 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +020057 port RAN_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010058}
59
60/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +020061type enumerated RAN_Conn_Prim {
Harald Welte365f4ed2017-11-23 00:00:43 +010062 /* SCCP tell us that connection was released */
63 MSC_CONN_PRIM_DISC_IND,
64 /* we tell SCCP to release connection */
Harald Welte71b69332018-01-21 20:43:53 +010065 MSC_CONN_PRIM_DISC_REQ,
66 /* Connection confirmed indication */
67 MSC_CONN_PRIM_CONF_IND
Harald Welte365f4ed2017-11-23 00:00:43 +010068}
69
Harald Welte0b476062018-01-21 19:07:32 +010070/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
71type record PDU_DTAP_MO {
72 OCT1 dlci optional,
Daniel Willmann92f66272018-02-06 15:50:52 +010073 boolean skip_seq_patching optional,
Harald Welte0b476062018-01-21 19:07:32 +010074 PDU_ML3_MS_NW dtap
75}
76
77/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
78type record PDU_DTAP_MT {
79 OCT1 dlci optional,
80 PDU_ML3_NW_MS dtap
81}
82
83template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
84 dlci := dlci,
85 dtap := dtap
86}
87
Daniel Willmann92f66272018-02-06 15:50:52 +010088template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O, boolean skip_seq_patching := false) := {
Harald Welte0b476062018-01-21 19:07:32 +010089 dlci := dlci,
Daniel Willmann92f66272018-02-06 15:50:52 +010090 skip_seq_patching := skip_seq_patching,
Harald Welte0b476062018-01-21 19:07:32 +010091 dtap := dtap
92}
93
94template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
95 dlci := dlci,
96 dtap := dtap
97}
98
99template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
100 dlci := dlci,
Harald Welte930d0a72018-03-22 22:08:40 +0100101 skip_seq_patching := ?,
Harald Welte0b476062018-01-21 19:07:32 +0100102 dtap := dtap
103}
104
Harald Welte365f4ed2017-11-23 00:00:43 +0100105/* port between individual per-connection components and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +0200106type port RAN_Conn_PT message {
Harald Welte2fce7882019-04-15 11:48:05 +0200107 inout
108#ifdef RAN_EMULATION_BSSAP
109 PDU_BSSAP,
Harald Weltea4ca4462018-02-09 00:17:14 +0100110 /* Client requests us to create SCCP Connection */
111 BSSAP_Conn_Req,
Harald Welte2fce7882019-04-15 11:48:05 +0200112#endif
113#ifdef RAN_EMULATION_MGCP
Harald Weltea4ca4462018-02-09 00:17:14 +0100114 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
Harald Welte2fce7882019-04-15 11:48:05 +0200115 MgcpCommand, MgcpResponse,
116#endif
117 /* direct DTAP messages from/to clients */
118 PDU_DTAP_MO, PDU_DTAP_MT,
119 /* misc indications / requests between SCCP and client */
120 RAN_Conn_Prim;
Harald Welte365f4ed2017-11-23 00:00:43 +0100121} with { extension "internal" };
122
123
124/* represents a single BSSAP connection over SCCP */
125type record ConnectionData {
126 /* reference to the instance of the per-connection component */
Harald Welte6811d102019-04-14 22:23:14 +0200127 RAN_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100128 integer sccp_conn_id,
Harald Welte2fce7882019-04-15 11:48:05 +0200129#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100130 /* most recent MGCP transaction ID (Used on MSC side) */
131 MgcpTransId mgcp_trans_id optional,
Harald Welte2fce7882019-04-15 11:48:05 +0200132#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100133 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100134 integer cic optional,
135 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
136 uint2_t n_sd[4]
Harald Welte365f4ed2017-11-23 00:00:43 +0100137}
138
Harald Welte17d21152018-01-27 00:47:11 +0100139type record ImsiMapping {
Harald Welte6811d102019-04-14 22:23:14 +0200140 RAN_ConnHdlr comp_ref,
Harald Welte17d21152018-01-27 00:47:11 +0100141 hexstring imsi optional,
142 OCT4 tmsi
143}
144
Harald Welte6811d102019-04-14 22:23:14 +0200145type component RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200146 /* SCCP ports on the bottom side, using ASP primitives */
147#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100148 port BSSAP_CODEC_PT BSSAP;
Harald Welte2fce7882019-04-15 11:48:05 +0200149#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100150 /* BSSAP port to the per-connection clients */
Harald Welte6811d102019-04-14 22:23:14 +0200151 port RAN_Conn_PT CLIENT;
Harald Welte2fce7882019-04-15 11:48:05 +0200152#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100153 /* MGCP port */
154 port IPA_MGCP_PT MGCP;
Harald Welte2fce7882019-04-15 11:48:05 +0200155#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100156
157 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
158 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100159
Harald Welte624f9632017-12-16 19:26:04 +0100160 /* pending expected incoming connections */
161 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100162
163 /* tables for mapping inbound unitdata (like paging) */
164 var ImsiMapping ImsiTable[16];
165
Harald Welte624f9632017-12-16 19:26:04 +0100166 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200167 port RAN_PROC_PT PROC;
Harald Welte624f9632017-12-16 19:26:04 +0100168
Harald Welte6811d102019-04-14 22:23:14 +0200169 var charstring g_ran_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100170 var integer g_next_e1_ts := 1;
Harald Welte6811d102019-04-14 22:23:14 +0200171 var RanOps g_ran_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100172};
173
Harald Welteb3414b22017-11-23 18:22:10 +0100174private function f_conn_id_known(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200175runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100176 var integer i;
177 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
178 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
179 return true;
180 }
181 }
182 return false;
183}
184
Harald Welte6811d102019-04-14 22:23:14 +0200185private function f_comp_known(RAN_ConnHdlr client)
186runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100187 var integer i;
188 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
189 if (ConnectionTable[i].comp_ref == client) {
190 return true;
191 }
192 }
193 return false;
194}
Harald Welte365f4ed2017-11-23 00:00:43 +0100195
Harald Weltee98bb2e2017-11-29 12:09:48 +0100196private function f_cic_known(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200197runs on RAN_Emulation_CT return boolean {
Harald Weltee98bb2e2017-11-29 12:09:48 +0100198 var integer i;
199 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
200 if (ConnectionTable[i].cic == cic) {
201 return true;
202 }
203 }
204 return false;
205}
206
Harald Welte365f4ed2017-11-23 00:00:43 +0100207/* resolve component reference by connection ID */
208private function f_comp_by_conn_id(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200209runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +0100210 var integer i;
211 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
212 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
213 return ConnectionTable[i].comp_ref;
214 }
215 }
Harald Welte6811d102019-04-14 22:23:14 +0200216 setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200217 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100218}
219
Harald Welte2fce7882019-04-15 11:48:05 +0200220
221#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100222/* resolve component reference by CIC */
223private function f_comp_by_mgcp_tid(MgcpTransId tid)
Harald Welte6811d102019-04-14 22:23:14 +0200224runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100225 var integer i;
226 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
227 if (ConnectionTable[i].mgcp_trans_id == tid) {
228 return ConnectionTable[i].comp_ref;
229 }
230 }
Harald Welte6811d102019-04-14 22:23:14 +0200231 setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200232 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100233}
234
Harald Welte6811d102019-04-14 22:23:14 +0200235private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid)
236runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100237 var integer i;
238 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
239 if (ConnectionTable[i].comp_ref == client) {
240 ConnectionTable[i].mgcp_trans_id := tid;
241 return;
242 }
243 }
Harald Welte6811d102019-04-14 22:23:14 +0200244 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200245 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100246}
Harald Welte2fce7882019-04-15 11:48:05 +0200247#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100248
249private function f_comp_by_cic(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200250runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100251 var integer i;
252 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
253 if (ConnectionTable[i].cic == cic) {
254 return ConnectionTable[i].comp_ref;
255 }
256 }
Harald Welte6811d102019-04-14 22:23:14 +0200257 setverdict(fail, "RAN Connection table not found by CIC ", cic);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200258 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100259}
260
Harald Welte6811d102019-04-14 22:23:14 +0200261private function f_comp_store_cic(RAN_ConnHdlr client, integer cic)
262runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100263 var integer i;
264 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
265 if (ConnectionTable[i].comp_ref == client) {
266 ConnectionTable[i].cic := cic;
267 return;
268 }
269 }
Harald Welte6811d102019-04-14 22:23:14 +0200270 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200271 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100272}
273
Harald Welte365f4ed2017-11-23 00:00:43 +0100274/* resolve connection ID by component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200275private function f_conn_id_by_comp(RAN_ConnHdlr client)
276runs on RAN_Emulation_CT return integer {
Harald Welte365f4ed2017-11-23 00:00:43 +0100277 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
278 if (ConnectionTable[i].comp_ref == client) {
279 return ConnectionTable[i].sccp_conn_id;
280 }
281 }
Harald Welte6811d102019-04-14 22:23:14 +0200282 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200283 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100284}
285
Harald Welte9dadc522018-02-06 13:56:04 +0100286/* resolve ConnectionTable index component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200287private function f_idx_by_comp(RAN_ConnHdlr client)
288runs on RAN_Emulation_CT return integer {
Harald Welte9dadc522018-02-06 13:56:04 +0100289 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
290 if (ConnectionTable[i].comp_ref == client) {
291 return i;
292 }
293 }
Harald Welte6811d102019-04-14 22:23:14 +0200294 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200295 mtc.stop;
Harald Welte9dadc522018-02-06 13:56:04 +0100296}
297
Harald Welteb3414b22017-11-23 18:22:10 +0100298private function f_gen_conn_id()
Harald Welte6811d102019-04-14 22:23:14 +0200299runs on RAN_Emulation_CT return integer {
Harald Welteb3414b22017-11-23 18:22:10 +0100300 var integer conn_id;
301
302 do {
303 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
304 } while (f_conn_id_known(conn_id) == true);
305
306 return conn_id;
307}
308
Harald Welte365f4ed2017-11-23 00:00:43 +0100309private function f_conn_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200310runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100311 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
312 ConnectionTable[i].comp_ref := null;
313 ConnectionTable[i].sccp_conn_id := -1;
Harald Welte2fce7882019-04-15 11:48:05 +0200314#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100315 ConnectionTable[i].mgcp_trans_id := omit;
Harald Welte2fce7882019-04-15 11:48:05 +0200316#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100317 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100318 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100319 }
Harald Welte17d21152018-01-27 00:47:11 +0100320 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
321 ImsiTable[i].comp_ref := null;
322 ImsiTable[i].imsi := omit;
323 ImsiTable[i].tmsi := 'FFFFFFFF'O;
324 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100325}
326
Harald Welte6811d102019-04-14 22:23:14 +0200327private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id)
328runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100329 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
330 if (ConnectionTable[i].sccp_conn_id == -1) {
331 ConnectionTable[i].comp_ref := comp_ref;
332 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100333 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100334 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100335 return;
336 }
337 }
Harald Welte6811d102019-04-14 22:23:14 +0200338 testcase.stop("RAN Connection table full!");
Harald Welte365f4ed2017-11-23 00:00:43 +0100339}
340
341private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200342runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100343 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
344 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100345 log("Deleted conn table entry ", i,
346 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100347 ConnectionTable[i].sccp_conn_id := -1;
348 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100349 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100350 }
351 }
Harald Welte6811d102019-04-14 22:23:14 +0200352 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200353 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100354}
355
Harald Welte17d21152018-01-27 00:47:11 +0100356private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200357runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100358 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
359 if (ImsiTable[i].imsi == imsi or
360 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
361 return ImsiTable[i].comp_ref;
362 }
363 }
364 return null;
365}
366
Harald Welte2fce7882019-04-15 11:48:05 +0200367#ifdef RAN_EMULATION_BSSAP
368type record BSSAP_Conn_Req {
369 SCCP_PAR_Address addr_peer,
370 SCCP_PAR_Address addr_own,
371 PDU_BSSAP bssap
372}
373template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := {
374 addr_peer := peer,
375 addr_own := own,
376 bssap := bssap
377};
378
Harald Welte365f4ed2017-11-23 00:00:43 +0100379/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200380private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
381runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100382 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100383
Harald Welte473676b2018-01-27 20:38:01 +0100384 if (ischosen(bssap.pdu.bssmap)) {
385 /* BSC Side: If this is an assignment command, store CIC */
386 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
387 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
388 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
389 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
390 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
391 f_comp_store_cic(client, cic);
392 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100393 }
394
Harald Welte6811d102019-04-14 22:23:14 +0200395 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
396 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100397 /* we are the MS, so any message to us must be MT */
398 var PDU_DTAP_MT mt := {
399 dlci := bssap.dlci,
400 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
401 };
402 CLIENT.send(mt) to client;
403 } else {
404 /* we are the Network, so any message to us must be MO */
405 var PDU_DTAP_MO mo := {
406 dlci := bssap.dlci,
407 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
408 };
409 CLIENT.send(mo) to client;
410 }
411 } else {
412 CLIENT.send(bssap) to client;
413 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100414}
415
416/* call-back type, to be provided by specific implementation; called when new SCCP connection
417 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100418type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200419runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100420
421type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200422runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100423
Harald Welte17d21152018-01-27 00:47:11 +0100424/* handle common Unitdata such as Paging */
425private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200426runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100427 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200428 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100429 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
430 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200431 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100432 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
433 client);
434 CLIENT.send(bssap) to client;
435 return omit;
436 }
437 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
438 } else {
439 log("CommonBssmapUnitdataCallback: Not a paging message");
440 }
441 /* ELSE: handle in user callback */
Harald Welte6811d102019-04-14 22:23:14 +0200442 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100443}
444
Harald Welte6811d102019-04-14 22:23:14 +0200445private function f_bssap_wait_for_reset() runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100446 var BSSAP_N_UNITDATA_ind ud_ind;
447 timer T := 20.0;
448
449 T.start;
450 alt {
451 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
452 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
453 ts_BSSMAP_ResetAck));
454 }
455 [] as_reset_ack();
456 [] BSSAP.receive {
457 repeat;
458 }
459 [] T.timeout {
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200460 setverdict(fail, "Timeout waiting for BSSAP RESET");
461 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100462 }
463 }
464}
465
Harald Welte6811d102019-04-14 22:23:14 +0200466function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100467 timer T := 5.0;
468
469 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0)));
470 T.start;
471 alt {
472 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck)) {
473 log("Received RESET-ACK in response to RESET, we're ready to go!");
474 }
475 [] as_reset_ack();
476 [] BSSAP.receive { repeat };
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200477 [] T.timeout {
478 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
479 mtc.stop;
480 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100481 }
482}
483
Harald Welte2fce7882019-04-15 11:48:05 +0200484private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
485 var template octetstring l3 := f_bssap_extract_l3(bssap);
486 return f_L3_is_rr(l3);
487}
488#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100489
Harald Welte365f4ed2017-11-23 00:00:43 +0100490
Harald Welte2fce7882019-04-15 11:48:05 +0200491
492type enumerated RanProtocol {
493 RAN_PROTOCOL_BSSAP
494}
495
496type record RanOps {
497#ifdef RAN_EMULATION_BSSAP
498 BssmapCreateCallback create_cb optional,
499 BssmapUnitdataCallback unitdata_cb optional,
500#endif
501 boolean decode_dtap,
502 boolean role_ms,
503 RanProtocol protocol,
504 /* needed for performing BSSMAP RESET */
505 SCCP_PAR_Address sccp_addr_local optional,
506 SCCP_PAR_Address sccp_addr_peer optional
507}
508
509template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
510
511private function f_L3_is_rr(template octetstring l3) return boolean {
512 if (not isvalue(l3)) {
513 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100514 }
Harald Welte2fce7882019-04-15 11:48:05 +0200515 var octetstring l3v := valueof(l3);
516 if (lengthof(l3v) < 1) {
517 return false;
518 }
519 /* lower 4 bits of first octet are protocol discriminator */
520 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
521 return true;
522 }
523 return false;
524}
Harald Weltea4ca4462018-02-09 00:17:14 +0100525
Harald Welte2fce7882019-04-15 11:48:05 +0200526/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
527function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
528 var uint2_t seq_nr;
529 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
530 seq_nr := cd.n_sd[0];
531 cd.n_sd[0] := (cd.n_sd[0] + 1) mod 4;
532 } else if (ischosen(dtap.msgs.gcc)) {
533 seq_nr := cd.n_sd[1];
534 cd.n_sd[1] := (cd.n_sd[1] + 1) mod 2;
535 } else if (ischosen(dtap.msgs.bcc)) {
536 seq_nr := cd.n_sd[2];
537 cd.n_sd[2] := (cd.n_sd[2] + 1) mod 2;
538 } else if (ischosen(dtap.msgs.loc)) {
539 seq_nr := cd.n_sd[3];
540 cd.n_sd[3] := (cd.n_sd[3] + 1) mod 2;
541 } else {
542 /* no sequence number to patch */
543 return;
544 }
545 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
546 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
547 log("patched enc_l3: ", enc_l3);
548}
549
550private altstep as_reset_ack() runs on RAN_Emulation_CT {
551#ifdef RAN_EMULATION_BSSAP
552 var BSSAP_N_UNITDATA_ind ud_ind;
553#endif
554#ifdef RAN_EMULATION_BSSAP
555 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
556 log("Respoding to inbound RESET with RESET-ACK");
557 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
558 ts_BSSMAP_ResetAck));
559 repeat;
560 }
561#endif
562}
563
564
565private altstep as_main_bssap() runs on RAN_Emulation_CT {
566#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100567 var BSSAP_N_UNITDATA_ind ud_ind;
568 var BSSAP_N_CONNECT_ind conn_ind;
569 var BSSAP_N_CONNECT_cfm conn_cfm;
570 var BSSAP_N_DATA_ind data_ind;
571 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100572 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100573 var PDU_BSSAP bssap;
Harald Welte2fce7882019-04-15 11:48:05 +0200574 var RAN_ConnHdlr vc_conn;
Harald Welte365f4ed2017-11-23 00:00:43 +0100575
Harald Welte365f4ed2017-11-23 00:00:43 +0100576 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100577 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100578 /* Connectionless Procedures like RESET */
579 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100580 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100581 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100582 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
583 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100584 }
585 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100586 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100587 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200588 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100589 /* store mapping between client components and SCCP connectionId */
590 f_conn_table_add(vc_conn, conn_ind.connectionId);
591 /* handle user payload */
592 f_handle_userData(vc_conn, conn_ind.userData);
593 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100594 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100595 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100596 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100597 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100598 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100599 if (ispresent(data_ind.userData)) {
600 f_handle_userData(vc_conn, data_ind.userData);
601 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100602 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100603 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100604 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100605 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100606 if (ispresent(disc_ind.userData)) {
607 f_handle_userData(vc_conn, disc_ind.userData);
608 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100609 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200610 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100611 CLIENT.send(prim) to vc_conn;
612 f_conn_table_del(disc_ind.connectionId);
613 /* TOOD: return confirm to other side? */
614 }
Harald Welteb3414b22017-11-23 18:22:10 +0100615 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100616 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100617 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200618 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100619 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100620 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100621 if (ispresent(conn_cfm.userData)) {
622 f_handle_userData(vc_conn, conn_cfm.userData);
623 }
Harald Welteb3414b22017-11-23 18:22:10 +0100624 }
Harald Welte2fce7882019-04-15 11:48:05 +0200625 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
626 var integer conn_id := f_conn_id_by_comp(vc_conn);
627 /* send it to dispatcher */
628 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
629 }
Harald Welteb3414b22017-11-23 18:22:10 +0100630
Harald Welte365f4ed2017-11-23 00:00:43 +0100631 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200632 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100633 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100634 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100635 f_conn_table_del(conn_id);
636 }
637
638 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100639 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
640 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100641 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100642
643 if (f_comp_known(vc_conn) == false) {
644 /* unknown client, create new connection */
645 conn_id := f_gen_conn_id();
646
647 /* store mapping between client components and SCCP connectionId */
648 f_conn_table_add(vc_conn, conn_id);
649
Harald Welte004f5fb2017-12-16 17:54:40 +0100650 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
651 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100652 } else {
653 /* known client, send via existing connection */
654 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100655 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100656 }
657
Harald Welte49518bf2018-02-10 11:39:19 +0100658 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
659 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200660 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100661 /* we have just sent the first MM message, increment the counter */
662 var integer idx := f_idx_by_comp(vc_conn);
663 ConnectionTable[idx].n_sd[0] := 1;
664 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
665 }
666
Harald Welteb3414b22017-11-23 18:22:10 +0100667 }
Harald Welte2fce7882019-04-15 11:48:05 +0200668#else
669 [false] CLIENT.receive(false) {}
670#endif
671}
Harald Welteb3414b22017-11-23 18:22:10 +0100672
Harald Welte2fce7882019-04-15 11:48:05 +0200673private altstep as_main_mgcp() runs on RAN_Emulation_CT {
674#ifdef RAN_EMULATION_MGCP
675 var MgcpCommand mgcp_req;
676 var MgcpResponse mgcp_resp;
677 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +0100678
Harald Weltec82eef42017-11-24 20:40:12 +0100679 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
680 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
681 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
682 * is printed as hex string, e.g. a@mgw for CIC 10 */
683
684 /* CLIENT -> MGCP */
685 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
686 /* MGCP request from Handler (we're MSC) */
687 /* store the transaction ID we've seen */
688 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
689 /* simply forward any MGCP from the client to the port */
690 MGCP.send(mgcp_req);
691 }
692 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
693 /* MGCP response from Handler (we're BSC/MGW) */
694 /* simply forward any MGCP from the client to the port */
695 MGCP.send(mgcp_resp);
696 }
697
698 /* MGCP -> CLIENT */
699 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
700 /* MGCP request from network side (we're BSC/MGW) */
701 /* Extract CIC from local part of endpoint name */
702 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100703 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
704 /* ignore RSIP for unknown CIC */
705 } else {
706 /* Resolve the vc_conn by the CIC */
707 vc_conn := f_comp_by_cic(cic);
708 CLIENT.send(mgcp_req) to vc_conn;
709 }
Harald Weltec82eef42017-11-24 20:40:12 +0100710 }
711 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
712 /* MGCP response from network side (we're MSC) */
713 /* Resolve the vc_conn by the transaction ID */
714 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
715 CLIENT.send(mgcp_resp) to vc_conn;
716 }
Harald Welte2fce7882019-04-15 11:48:05 +0200717#else
718 [false] CLIENT.receive {}
719#endif
720}
Harald Weltec82eef42017-11-24 20:40:12 +0100721
Harald Welte2fce7882019-04-15 11:48:05 +0200722/* send a raw (encoded) L3 message over given SCCP connection */
723private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
724{
725 select (g_ran_ops.protocol) {
726#ifdef RAN_EMULATION_BSSAP
727 case (RAN_PROTOCOL_BSSAP) {
728 var PDU_BSSAP bssap;
729 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
730 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
731 }
732#endif
733 }
734}
735
736function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
737
738 g_ran_id := id;
739 g_ran_ops := ops;
740 f_conn_table_init();
741 f_expect_table_init();
742
743 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
744 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
745 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
746 }
747
748 while (true) {
749 var RAN_ConnHdlr vc_conn;
750 var PDU_DTAP_MO dtap_mo;
751 var PDU_DTAP_MT dtap_mt;
752 var RAN_ConnHdlr vc_hdlr;
753 var octetstring l3_info;
754 var hexstring imsi;
755 var OCT4 tmsi;
756
757 alt {
758 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
759
760 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
761 var integer idx := f_idx_by_comp(vc_conn);
762 /* convert from decoded DTAP to encoded DTAP */
763 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
764 /* patch correct L3 send sequence number N(SD) into l3_enc */
765 if (dtap_mo.skip_seq_patching == false) {
766 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
767 }
768 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
769 }
770
771 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
772 var integer idx := f_idx_by_comp(vc_conn);
773 /* convert from decoded DTAP to encoded DTAP */
774 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
775 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
776 }
777
778 [] as_main_mgcp();
Harald Welte624f9632017-12-16 19:26:04 +0100779
Harald Welte6811d102019-04-14 22:23:14 +0200780 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +0100781 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +0200782 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +0100783 }
784
Harald Welte6811d102019-04-14 22:23:14 +0200785 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +0100786 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +0200787 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +0100788 }
789
790
Harald Welte365f4ed2017-11-23 00:00:43 +0100791 }
792 }
793}
794
Harald Welte2fce7882019-04-15 11:48:05 +0200795#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100796private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100797 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100798 return hex2int(str2hex(local_part));
799
800}
Harald Welte2fce7882019-04-15 11:48:05 +0200801#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100802
Harald Welte624f9632017-12-16 19:26:04 +0100803/***********************************************************************
804 * "Expect" Handling (mapping for expected incoming SCCP connections)
805 ***********************************************************************/
806
807/* data about an expected future incoming connection */
808type record ExpectData {
809 /* L3 payload based on which we can match it */
810 octetstring l3_payload optional,
811 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +0200812 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +0100813}
814
815/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200816signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +0100817
Harald Welte17d21152018-01-27 00:47:11 +0100818/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +0200819signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +0100820
Harald Welte6811d102019-04-14 22:23:14 +0200821type port RAN_PROC_PT procedure {
822 inout RAN_register, RAN_register_imsi;
Harald Welte624f9632017-12-16 19:26:04 +0100823} with { extension "internal" };
824
825/* CreateCallback that can be used as create_cb and will use the expectation table */
826function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200827runs on RAN_Emulation_CT return RAN_ConnHdlr {
828 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +0100829 var octetstring l3_info;
830 var integer i;
831
832 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
833 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200834 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +0100835 return ret;
836 }
837 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
838
839 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
840 if (not ispresent(ExpectTable[i].l3_payload)) {
841 continue;
842 }
843 if (l3_info == ExpectTable[i].l3_payload) {
844 ret := ExpectTable[i].vc_conn;
845 /* release this entry to be used again */
846 ExpectTable[i].l3_payload := omit;
847 ExpectTable[i].vc_conn := null;
848 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
849 /* return the component reference */
850 return ret;
851 }
852 }
853 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200854 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +0100855 return ret;
856}
857
Harald Welte6811d102019-04-14 22:23:14 +0200858private function f_create_expect(octetstring l3, RAN_ConnHdlr hdlr)
859runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +0100860 var integer i;
861 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
862 if (not ispresent(ExpectTable[i].l3_payload)) {
863 ExpectTable[i].l3_payload := l3;
864 ExpectTable[i].vc_conn := hdlr;
865 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
866 return;
867 }
868 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200869 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +0100870}
871
Harald Welte6811d102019-04-14 22:23:14 +0200872private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
873runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +0100874 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
875 if (not ispresent(ImsiTable[i].imsi)) {
876 ImsiTable[i].imsi := imsi;
877 ImsiTable[i].tmsi := tmsi;
878 ImsiTable[i].comp_ref := hdlr;
879 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
880 return;
881 }
882 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200883 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +0100884}
885
886
Daniel Willmannd47106b2018-01-17 12:20:56 +0100887private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200888runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +0100889 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
890 ExpectTable[i].l3_payload := omit;
891 }
892}
Harald Welte624f9632017-12-16 19:26:04 +0100893
Harald Welte17d21152018-01-27 00:47:11 +0100894/* helper function for clients to register their IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +0200895function f_ran_register_imsi(hexstring imsi, OCT4 tmsi)
896runs on RAN_ConnHdlr {
897 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
898 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +0100899 }
900}
901
902
Harald Welte365f4ed2017-11-23 00:00:43 +0100903}