blob: 85ae08400ecf72726236abe9d27786beff27dee0 [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
Harald Welte5b027622019-04-14 23:40:17 +020051#ifdef RAN_EMULATION_RANAP
52import from RANAP_CodecPort all;
53import from RANAP_PDU_Descriptions all;
54import from RANAP_Constants all;
55import from RANAP_IEs all;
56import from RANAP_Templates all;
57#endif
58
Harald Welte365f4ed2017-11-23 00:00:43 +010059/* General "base class" component definition, of which specific implementations
60 * derive themselves by means of the "extends" feature */
Harald Welte6811d102019-04-14 22:23:14 +020061type component RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +010062 /* port towards MSC Emulator core / SCCP connection dispatchar */
Harald Welte6811d102019-04-14 22:23:14 +020063 port RAN_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010064 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +020065 port RAN_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010066}
67
68/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +020069type enumerated RAN_Conn_Prim {
Harald Welte365f4ed2017-11-23 00:00:43 +010070 /* SCCP tell us that connection was released */
71 MSC_CONN_PRIM_DISC_IND,
72 /* we tell SCCP to release connection */
Harald Welte71b69332018-01-21 20:43:53 +010073 MSC_CONN_PRIM_DISC_REQ,
74 /* Connection confirmed indication */
75 MSC_CONN_PRIM_CONF_IND
Harald Welte365f4ed2017-11-23 00:00:43 +010076}
77
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +020078type enumerated RAN_Transport {
79 BSSAP_TRANSPORT_AoIP, /* 3GPP AoIP: SCCP over M3UA over SCTP */
80 BSSAP_TRANSPORT_SCCPlite_SERVER, /* SCCPlite: SCCP over IPA over TCP */
81 BSSAP_TRANSPORT_SCCPlite_CLIENT, /* SCCPlite: SCCP over IPA over TCP */
82 RANAP_TRANSPORT_IuCS /* 3GPP IuCS: SCCP over M3UA over SCTP */
83};
84
Harald Welte0b476062018-01-21 19:07:32 +010085/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
86type record PDU_DTAP_MO {
87 OCT1 dlci optional,
Daniel Willmann92f66272018-02-06 15:50:52 +010088 boolean skip_seq_patching optional,
Harald Welte0b476062018-01-21 19:07:32 +010089 PDU_ML3_MS_NW dtap
90}
91
92/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
93type record PDU_DTAP_MT {
94 OCT1 dlci optional,
95 PDU_ML3_NW_MS dtap
96}
97
98template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
99 dlci := dlci,
100 dtap := dtap
101}
102
Daniel Willmann92f66272018-02-06 15:50:52 +0100103template 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 +0100104 dlci := dlci,
Daniel Willmann92f66272018-02-06 15:50:52 +0100105 skip_seq_patching := skip_seq_patching,
Harald Welte0b476062018-01-21 19:07:32 +0100106 dtap := dtap
107}
108
109template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
110 dlci := dlci,
111 dtap := dtap
112}
113
114template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
115 dlci := dlci,
Harald Welte930d0a72018-03-22 22:08:40 +0100116 skip_seq_patching := ?,
Harald Welte0b476062018-01-21 19:07:32 +0100117 dtap := dtap
118}
119
Harald Welte365f4ed2017-11-23 00:00:43 +0100120/* port between individual per-connection components and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +0200121type port RAN_Conn_PT message {
Harald Welte2fce7882019-04-15 11:48:05 +0200122 inout
123#ifdef RAN_EMULATION_BSSAP
124 PDU_BSSAP,
Harald Weltea4ca4462018-02-09 00:17:14 +0100125 /* Client requests us to create SCCP Connection */
126 BSSAP_Conn_Req,
Harald Welte2fce7882019-04-15 11:48:05 +0200127#endif
Harald Welte5b027622019-04-14 23:40:17 +0200128#ifdef RAN_EMULATION_RANAP
129 RANAP_PDU,
130 /* Client requests us to create SCCP Connection */
131 RANAP_Conn_Req,
132#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200133#ifdef RAN_EMULATION_MGCP
Harald Weltea4ca4462018-02-09 00:17:14 +0100134 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
Harald Welte2fce7882019-04-15 11:48:05 +0200135 MgcpCommand, MgcpResponse,
136#endif
137 /* direct DTAP messages from/to clients */
138 PDU_DTAP_MO, PDU_DTAP_MT,
139 /* misc indications / requests between SCCP and client */
140 RAN_Conn_Prim;
Harald Welte365f4ed2017-11-23 00:00:43 +0100141} with { extension "internal" };
142
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200143type uint2_t N_Sd_Array[4];
Harald Welte365f4ed2017-11-23 00:00:43 +0100144
145/* represents a single BSSAP connection over SCCP */
146type record ConnectionData {
147 /* reference to the instance of the per-connection component */
Harald Welte6811d102019-04-14 22:23:14 +0200148 RAN_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100149 integer sccp_conn_id,
Harald Welte2fce7882019-04-15 11:48:05 +0200150#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100151 /* most recent MGCP transaction ID (Used on MSC side) */
152 MgcpTransId mgcp_trans_id optional,
Harald Welte2fce7882019-04-15 11:48:05 +0200153#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100154 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100155 integer cic optional,
156 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200157 N_Sd_Array n_sd
Harald Welte365f4ed2017-11-23 00:00:43 +0100158}
159
Harald Welte17d21152018-01-27 00:47:11 +0100160type record ImsiMapping {
Harald Welte6811d102019-04-14 22:23:14 +0200161 RAN_ConnHdlr comp_ref,
Harald Welte17d21152018-01-27 00:47:11 +0100162 hexstring imsi optional,
163 OCT4 tmsi
164}
165
Harald Welte6811d102019-04-14 22:23:14 +0200166type component RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200167 /* SCCP ports on the bottom side, using ASP primitives */
168#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100169 port BSSAP_CODEC_PT BSSAP;
Harald Welte2fce7882019-04-15 11:48:05 +0200170#endif
Harald Welte5b027622019-04-14 23:40:17 +0200171#ifdef RAN_EMULATION_RANAP
172 port RANAP_CODEC_PT RANAP;
173#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100174 /* BSSAP port to the per-connection clients */
Harald Welte6811d102019-04-14 22:23:14 +0200175 port RAN_Conn_PT CLIENT;
Harald Welte2fce7882019-04-15 11:48:05 +0200176#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100177 /* MGCP port */
178 port IPA_MGCP_PT MGCP;
Harald Welte2fce7882019-04-15 11:48:05 +0200179#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100180
181 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
182 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100183
Harald Welte624f9632017-12-16 19:26:04 +0100184 /* pending expected incoming connections */
185 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100186
187 /* tables for mapping inbound unitdata (like paging) */
188 var ImsiMapping ImsiTable[16];
189
Harald Welte624f9632017-12-16 19:26:04 +0100190 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200191 port RAN_PROC_PT PROC;
Harald Welte624f9632017-12-16 19:26:04 +0100192
Harald Welte6811d102019-04-14 22:23:14 +0200193 var charstring g_ran_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100194 var integer g_next_e1_ts := 1;
Harald Welte6811d102019-04-14 22:23:14 +0200195 var RanOps g_ran_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100196};
197
Harald Welteb3414b22017-11-23 18:22:10 +0100198private function f_conn_id_known(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200199runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100200 var integer i;
201 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
202 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
203 return true;
204 }
205 }
206 return false;
207}
208
Harald Welte6811d102019-04-14 22:23:14 +0200209private function f_comp_known(RAN_ConnHdlr client)
210runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100211 var integer i;
212 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
213 if (ConnectionTable[i].comp_ref == client) {
214 return true;
215 }
216 }
217 return false;
218}
Harald Welte365f4ed2017-11-23 00:00:43 +0100219
Harald Weltee98bb2e2017-11-29 12:09:48 +0100220private function f_cic_known(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200221runs on RAN_Emulation_CT return boolean {
Harald Weltee98bb2e2017-11-29 12:09:48 +0100222 var integer i;
223 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
224 if (ConnectionTable[i].cic == cic) {
225 return true;
226 }
227 }
228 return false;
229}
230
Harald Welte365f4ed2017-11-23 00:00:43 +0100231/* resolve component reference by connection ID */
232private function f_comp_by_conn_id(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200233runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +0100234 var integer i;
235 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
236 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
237 return ConnectionTable[i].comp_ref;
238 }
239 }
Harald Welte6811d102019-04-14 22:23:14 +0200240 setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200241 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100242}
243
Harald Welte2fce7882019-04-15 11:48:05 +0200244
245#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100246/* resolve component reference by CIC */
247private function f_comp_by_mgcp_tid(MgcpTransId tid)
Harald Welte6811d102019-04-14 22:23:14 +0200248runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100249 var integer i;
250 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
251 if (ConnectionTable[i].mgcp_trans_id == tid) {
252 return ConnectionTable[i].comp_ref;
253 }
254 }
Harald Welte6811d102019-04-14 22:23:14 +0200255 setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200256 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100257}
258
Harald Welte6811d102019-04-14 22:23:14 +0200259private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid)
260runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100261 var integer i;
262 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
263 if (ConnectionTable[i].comp_ref == client) {
264 ConnectionTable[i].mgcp_trans_id := tid;
265 return;
266 }
267 }
Harald Welte6811d102019-04-14 22:23:14 +0200268 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200269 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100270}
Harald Welte2fce7882019-04-15 11:48:05 +0200271#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100272
273private function f_comp_by_cic(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200274runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100275 var integer i;
276 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
277 if (ConnectionTable[i].cic == cic) {
278 return ConnectionTable[i].comp_ref;
279 }
280 }
Harald Welte6811d102019-04-14 22:23:14 +0200281 setverdict(fail, "RAN Connection table not found by CIC ", cic);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200282 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100283}
284
Harald Welte6811d102019-04-14 22:23:14 +0200285private function f_comp_store_cic(RAN_ConnHdlr client, integer cic)
286runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100287 var integer i;
288 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
289 if (ConnectionTable[i].comp_ref == client) {
290 ConnectionTable[i].cic := cic;
291 return;
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 Weltec82eef42017-11-24 20:40:12 +0100296}
297
Harald Welte365f4ed2017-11-23 00:00:43 +0100298/* resolve connection ID by component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200299private function f_conn_id_by_comp(RAN_ConnHdlr client)
300runs on RAN_Emulation_CT return integer {
Harald Welte365f4ed2017-11-23 00:00:43 +0100301 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
302 if (ConnectionTable[i].comp_ref == client) {
303 return ConnectionTable[i].sccp_conn_id;
304 }
305 }
Harald Welte6811d102019-04-14 22:23:14 +0200306 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200307 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100308}
309
Harald Welte9dadc522018-02-06 13:56:04 +0100310/* resolve ConnectionTable index component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200311private function f_idx_by_comp(RAN_ConnHdlr client)
312runs on RAN_Emulation_CT return integer {
Harald Welte9dadc522018-02-06 13:56:04 +0100313 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
314 if (ConnectionTable[i].comp_ref == client) {
315 return i;
316 }
317 }
Harald Welte6811d102019-04-14 22:23:14 +0200318 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200319 mtc.stop;
Harald Welte9dadc522018-02-06 13:56:04 +0100320}
321
Harald Welteb3414b22017-11-23 18:22:10 +0100322private function f_gen_conn_id()
Harald Welte6811d102019-04-14 22:23:14 +0200323runs on RAN_Emulation_CT return integer {
Harald Welteb3414b22017-11-23 18:22:10 +0100324 var integer conn_id;
325
326 do {
327 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
328 } while (f_conn_id_known(conn_id) == true);
329
330 return conn_id;
331}
332
Harald Welte365f4ed2017-11-23 00:00:43 +0100333private function f_conn_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200334runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100335 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
336 ConnectionTable[i].comp_ref := null;
337 ConnectionTable[i].sccp_conn_id := -1;
Harald Welte2fce7882019-04-15 11:48:05 +0200338#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100339 ConnectionTable[i].mgcp_trans_id := omit;
Harald Welte2fce7882019-04-15 11:48:05 +0200340#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100341 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100342 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100343 }
Harald Welte17d21152018-01-27 00:47:11 +0100344 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
345 ImsiTable[i].comp_ref := null;
346 ImsiTable[i].imsi := omit;
347 ImsiTable[i].tmsi := 'FFFFFFFF'O;
348 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100349}
350
Harald Welte6811d102019-04-14 22:23:14 +0200351private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id)
352runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100353 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
354 if (ConnectionTable[i].sccp_conn_id == -1) {
355 ConnectionTable[i].comp_ref := comp_ref;
356 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100357 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100358 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100359 return;
360 }
361 }
Harald Welte6811d102019-04-14 22:23:14 +0200362 testcase.stop("RAN Connection table full!");
Harald Welte365f4ed2017-11-23 00:00:43 +0100363}
364
365private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200366runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100367 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
368 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100369 log("Deleted conn table entry ", i,
370 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100371 ConnectionTable[i].sccp_conn_id := -1;
372 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100373 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100374 }
375 }
Harald Welte6811d102019-04-14 22:23:14 +0200376 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200377 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100378}
379
Harald Welte17d21152018-01-27 00:47:11 +0100380private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200381runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100382 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
383 if (ImsiTable[i].imsi == imsi or
384 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
385 return ImsiTable[i].comp_ref;
386 }
387 }
388 return null;
389}
390
Harald Welte2fce7882019-04-15 11:48:05 +0200391#ifdef RAN_EMULATION_BSSAP
392type record BSSAP_Conn_Req {
393 SCCP_PAR_Address addr_peer,
394 SCCP_PAR_Address addr_own,
395 PDU_BSSAP bssap
396}
397template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := {
398 addr_peer := peer,
399 addr_own := own,
400 bssap := bssap
401};
402
Harald Welte365f4ed2017-11-23 00:00:43 +0100403/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200404private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
405runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100406 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100407
Harald Welte473676b2018-01-27 20:38:01 +0100408 if (ischosen(bssap.pdu.bssmap)) {
409 /* BSC Side: If this is an assignment command, store CIC */
410 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
411 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
412 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
413 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
414 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
415 f_comp_store_cic(client, cic);
416 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100417 }
418
Harald Welte6811d102019-04-14 22:23:14 +0200419 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
420 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100421 /* we are the MS, so any message to us must be MT */
422 var PDU_DTAP_MT mt := {
423 dlci := bssap.dlci,
424 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
425 };
426 CLIENT.send(mt) to client;
427 } else {
428 /* we are the Network, so any message to us must be MO */
429 var PDU_DTAP_MO mo := {
430 dlci := bssap.dlci,
431 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
432 };
433 CLIENT.send(mo) to client;
434 }
435 } else {
436 CLIENT.send(bssap) to client;
437 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100438}
439
440/* call-back type, to be provided by specific implementation; called when new SCCP connection
441 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100442type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200443runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100444
445type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200446runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100447
Harald Welte17d21152018-01-27 00:47:11 +0100448/* handle common Unitdata such as Paging */
449private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200450runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100451 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200452 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100453 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
454 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200455 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100456 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
457 client);
458 CLIENT.send(bssap) to client;
459 return omit;
460 }
461 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
462 } else {
463 log("CommonBssmapUnitdataCallback: Not a paging message");
464 }
465 /* ELSE: handle in user callback */
Harald Welte6811d102019-04-14 22:23:14 +0200466 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100467}
468
Harald Welte6811d102019-04-14 22:23:14 +0200469private function f_bssap_wait_for_reset() runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100470 var BSSAP_N_UNITDATA_ind ud_ind;
471 timer T := 20.0;
472
473 T.start;
474 alt {
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200475 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(g_ran_ops.use_osmux))) -> value ud_ind {
Harald Weltea4ca4462018-02-09 00:17:14 +0100476 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200477 ts_BSSMAP_ResetAck(g_ran_ops.use_osmux)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100478 }
479 [] as_reset_ack();
480 [] BSSAP.receive {
481 repeat;
482 }
483 [] T.timeout {
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200484 setverdict(fail, "Timeout waiting for BSSAP RESET");
485 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100486 }
487 }
488}
489
Harald Welte6811d102019-04-14 22:23:14 +0200490function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100491 timer T := 5.0;
492
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200493 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0, g_ran_ops.use_osmux)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100494 T.start;
495 alt {
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200496 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck(g_ran_ops.use_osmux))) {
Harald Weltea4ca4462018-02-09 00:17:14 +0100497 log("Received RESET-ACK in response to RESET, we're ready to go!");
498 }
499 [] as_reset_ack();
500 [] BSSAP.receive { repeat };
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200501 [] T.timeout {
502 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
503 mtc.stop;
504 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100505 }
506}
507
Harald Welte2fce7882019-04-15 11:48:05 +0200508private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
509 var template octetstring l3 := f_bssap_extract_l3(bssap);
510 return f_L3_is_rr(l3);
511}
512#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100513
Harald Welte5b027622019-04-14 23:40:17 +0200514#ifdef RAN_EMULATION_RANAP
515type record RANAP_Conn_Req {
516 SCCP_PAR_Address addr_peer,
517 SCCP_PAR_Address addr_own,
518 RANAP_PDU ranap
519}
520template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, RANAP_PDU ranap) := {
521 addr_peer := peer,
522 addr_own := own,
523 ranap := ranap
524};
525
526private function fake_dlci_from_sapi(template (omit) SAPI sapi) return template (omit) OCT1
527{
528 if (istemplatekind(sapi, "omit")) {
529 return omit;
530 } else if (valueof(sapi) == sapi_3) {
531 return '03'O;
532 }
533 return '00'O;
534}
535
536private function f_handle_userData_RANAP(RAN_ConnHdlr client, RANAP_PDU ranap)
537runs on RAN_Emulation_CT {
538 /* decode + send decoded RANAP to client */
539 var template (omit) octetstring l3 := f_ranap_extract_l3(ranap);
540 if (istemplatekind(l3, "omit")) {
541 CLIENT.send(ranap) to client;
542 } else {
543 var template (omit) SAPI sapi := f_ranap_extract_sapi(ranap);
544 var template (omit) OCT1 dlci := fake_dlci_from_sapi(sapi);
545 if (g_ran_ops.role_ms) {
546 /* we are the MS, so any message to us must be MT */
547 var PDU_DTAP_MT mt := {
548 dlci := omit,
549 dtap := dec_PDU_ML3_NW_MS(valueof(l3))
550 };
551 if (isvalue(dlci)) {
552 mt.dlci := valueof(dlci)
553 }
554 CLIENT.send(mt) to client;
555 } else {
556 /* we are the Network, so any message to us must be MO */
557 var PDU_DTAP_MO mo := {
558 dlci := omit,
559 dtap := dec_PDU_ML3_MS_NW(valueof(l3))
560 };
561 if (isvalue(dlci)) {
562 mo.dlci := valueof(dlci)
563 }
564 CLIENT.send(mo) to client;
565 }
566 }
567}
568
569/* call-back type, to be provided by specific implementation; called when new SCCP connection
570 * arrives */
571type function RanapCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
572runs on RAN_Emulation_CT return RAN_ConnHdlr;
573
574type function RanapUnitdataCallback(RANAP_PDU ranap)
575runs on RAN_Emulation_CT return template RANAP_PDU;
576
577private function CommonRanapUnitdataCallback(RANAP_PDU ranap)
578runs on RAN_Emulation_CT return template RANAP_PDU {
579 if (match(ranap, tr_RANAP_Paging(?, ?))) {
580 var RAN_ConnHdlr client := null;
581 /* extract IMSI and (if present) TMSI */
582 var IMSI imsi := ranap.initiatingMessage.value_.paging.protocolIEs[1].value_.permanentNAS_UE_ID.iMSI;
583 var template OCT4 tmsi := omit;
584 if (lengthof(ranap.initiatingMessage.value_.paging.protocolIEs) > 2 and
585 ranap.initiatingMessage.value_.paging.protocolIEs[2].id == id_TemporaryUE_ID) {
586 var TemporaryUE_ID ue_id;
587 ue_id := ranap.initiatingMessage.value_.paging.protocolIEs[2].value_.temporaryUE_ID;
588 if (ischosen(ue_id.tMSI)) {
589 tmsi := ue_id.tMSI;
590 } else {
591 tmsi := ue_id.p_TMSI;
592 }
593 }
594 client := f_imsi_table_find(oct2hex(imsi), tmsi);
Harald Welte03d4e2b2019-05-10 00:42:33 +0200595 if (client != null) {
Harald Welte5b027622019-04-14 23:40:17 +0200596 log("CommonRanapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
597 client);
598 CLIENT.send(ranap) to client;
599 return omit;
600 }
601 log("CommonRanapUnitdataCallback: IMSI/TMSI not found in table");
602 } else {
603 log("CommonRanapUnitdataCallback: Not a paging message");
604 }
605
606 /* ELSE: handle in user callback */
607 return g_ran_ops.ranap_unitdata_cb.apply(ranap);
608}
609
610private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean {
611 var template (omit) SAPI sapi;
612 var template octetstring l3 := f_ranap_extract_l3(ranap);
613 return f_L3_is_rr(l3);
614}
615
616function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
617 timer T := 5.0;
618 var CN_DomainIndicator dom;
619 if (g_ran_ops.ps_domain) {
620 dom := ps_domain;
621 } else {
622 dom := cs_domain;
623 }
624
625 RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom)));
626 T.start;
627 alt {
628 [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) {
629 log("Received RESET-ACK in response to RESET, we're ready to go!");
630 }
631 [] as_reset_ack();
632 [] RANAP.receive { repeat };
633 [] T.timeout {
634 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
635 mtc.stop;
636 }
637 }
638}
639#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100640
Harald Welte2fce7882019-04-15 11:48:05 +0200641
642type enumerated RanProtocol {
Harald Welte5b027622019-04-14 23:40:17 +0200643 RAN_PROTOCOL_BSSAP,
644 RAN_PROTOCOL_RANAP
Harald Welte2fce7882019-04-15 11:48:05 +0200645}
646
647type record RanOps {
648#ifdef RAN_EMULATION_BSSAP
649 BssmapCreateCallback create_cb optional,
650 BssmapUnitdataCallback unitdata_cb optional,
651#endif
Harald Welte5b027622019-04-14 23:40:17 +0200652#ifdef RAN_EMULATION_RANAP
653 RanapCreateCallback ranap_create_cb optional,
654 RanapUnitdataCallback ranap_unitdata_cb optional,
655 boolean ps_domain,
656#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200657 boolean decode_dtap,
658 boolean role_ms,
659 RanProtocol protocol,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200660 RAN_Transport transport,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200661 boolean use_osmux,
Harald Welte2fce7882019-04-15 11:48:05 +0200662 /* needed for performing BSSMAP RESET */
663 SCCP_PAR_Address sccp_addr_local optional,
664 SCCP_PAR_Address sccp_addr_peer optional
665}
666
667template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
668
669private function f_L3_is_rr(template octetstring l3) return boolean {
670 if (not isvalue(l3)) {
671 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100672 }
Harald Welte2fce7882019-04-15 11:48:05 +0200673 var octetstring l3v := valueof(l3);
674 if (lengthof(l3v) < 1) {
675 return false;
676 }
677 /* lower 4 bits of first octet are protocol discriminator */
678 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
679 return true;
680 }
681 return false;
682}
Harald Weltea4ca4462018-02-09 00:17:14 +0100683
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200684function f_next_n_sd(inout N_Sd_Array n_sd, in integer n_sd_idx) return uint2_t {
Harald Welte2fce7882019-04-15 11:48:05 +0200685 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200686 if (n_sd_idx == 0) {
687 seq_nr := n_sd[0];
688 n_sd[0] := (n_sd[0] + 1) mod 4;
689 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
690 seq_nr := n_sd[n_sd_idx];
691 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200692 } else {
693 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200694 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200695 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200696 return seq_nr;
697}
698
699/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
700function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200701 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
702 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
703 log("patched enc_l3: ", enc_l3);
704}
705
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200706function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
707 var uint2_t seq_nr;
708 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
709 return 0;
710 } else if (ischosen(dtap.msgs.gcc)) {
711 return 1;
712 } else if (ischosen(dtap.msgs.bcc)) {
713 return 2;
714 } else if (ischosen(dtap.msgs.loc)) {
715 return 3;
716 }
717 /* no sequence number to patch */
718 return -1;
719}
720
721/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
722function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
723 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
724 if (n_sd_idx < 0) {
725 return;
726 }
727 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
728 f_ML3_patch_seq_nr(seq_nr, enc_l3);
729}
730
Harald Welte2fce7882019-04-15 11:48:05 +0200731private altstep as_reset_ack() runs on RAN_Emulation_CT {
732#ifdef RAN_EMULATION_BSSAP
733 var BSSAP_N_UNITDATA_ind ud_ind;
734#endif
Harald Welte5b027622019-04-14 23:40:17 +0200735#ifdef RAN_EMULATION_RANAP
736 var RANAP_N_UNITDATA_ind rud_ind;
737#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200738#ifdef RAN_EMULATION_BSSAP
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200739 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(g_ran_ops.use_osmux))) -> value ud_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200740 log("Respoding to inbound RESET with RESET-ACK");
741 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200742 ts_BSSMAP_ResetAck(g_ran_ops.use_osmux)));
Harald Welte2fce7882019-04-15 11:48:05 +0200743 repeat;
744 }
745#endif
Harald Welte5b027622019-04-14 23:40:17 +0200746#ifdef RAN_EMULATION_RANAP
747 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
748 log("Respoding to inbound IuRESET with IuRESET-ACK");
749 var CN_DomainIndicator dom;
750 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
751 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
752 ts_RANAP_ResetAck(dom)));
753 }
754#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200755}
756
757
758private altstep as_main_bssap() runs on RAN_Emulation_CT {
759#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100760 var BSSAP_N_UNITDATA_ind ud_ind;
761 var BSSAP_N_CONNECT_ind conn_ind;
762 var BSSAP_N_CONNECT_cfm conn_cfm;
763 var BSSAP_N_DATA_ind data_ind;
764 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100765 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100766 var PDU_BSSAP bssap;
Harald Welte2fce7882019-04-15 11:48:05 +0200767 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200768 var integer targetPointCode;
769 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100770
Harald Welte365f4ed2017-11-23 00:00:43 +0100771 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100772 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100773 /* Connectionless Procedures like RESET */
774 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100775 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100776 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100777 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
778 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100779 }
780 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100781 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100782 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200783 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100784 /* store mapping between client components and SCCP connectionId */
785 f_conn_table_add(vc_conn, conn_ind.connectionId);
786 /* handle user payload */
787 f_handle_userData(vc_conn, conn_ind.userData);
788 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100789 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100790 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100791 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100792 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100793 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100794 if (ispresent(data_ind.userData)) {
795 f_handle_userData(vc_conn, data_ind.userData);
796 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100797 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100798 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100799 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100800 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100801 if (ispresent(disc_ind.userData)) {
802 f_handle_userData(vc_conn, disc_ind.userData);
803 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100804 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200805 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100806 CLIENT.send(prim) to vc_conn;
807 f_conn_table_del(disc_ind.connectionId);
808 /* TOOD: return confirm to other side? */
809 }
Harald Welteb3414b22017-11-23 18:22:10 +0100810 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100811 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100812 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200813 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100814 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100815 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100816 if (ispresent(conn_cfm.userData)) {
817 f_handle_userData(vc_conn, conn_cfm.userData);
818 }
Harald Welteb3414b22017-11-23 18:22:10 +0100819 }
Harald Welte2fce7882019-04-15 11:48:05 +0200820 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
821 var integer conn_id := f_conn_id_by_comp(vc_conn);
822 /* send it to dispatcher */
823 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
824 }
Harald Welteb3414b22017-11-23 18:22:10 +0100825
Harald Welte365f4ed2017-11-23 00:00:43 +0100826 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200827 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100828 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100829 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100830 f_conn_table_del(conn_id);
831 }
832
833 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100834 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
835 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100836 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100837
838 if (f_comp_known(vc_conn) == false) {
839 /* unknown client, create new connection */
840 conn_id := f_gen_conn_id();
841
842 /* store mapping between client components and SCCP connectionId */
843 f_conn_table_add(vc_conn, conn_id);
844
Harald Welte004f5fb2017-12-16 17:54:40 +0100845 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
846 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100847 } else {
848 /* known client, send via existing connection */
849 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100850 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100851 }
852
Harald Welte49518bf2018-02-10 11:39:19 +0100853 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
854 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200855 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100856 /* we have just sent the first MM message, increment the counter */
857 var integer idx := f_idx_by_comp(vc_conn);
858 ConnectionTable[idx].n_sd[0] := 1;
859 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
860 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200861 }
Harald Welte9dadc522018-02-06 13:56:04 +0100862
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200863 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
864 var integer idx := f_idx_by_comp(vc_conn);
865 last_n_sd := ConnectionTable[idx].n_sd;
866 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
867 }
868
869 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
870 var integer idx := f_idx_by_comp(vc_conn);
871 ConnectionTable[idx].n_sd := last_n_sd;
872 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100873 }
Harald Welte2fce7882019-04-15 11:48:05 +0200874#else
Harald Welte5b027622019-04-14 23:40:17 +0200875 [false] CLIENT.receive {}
876#endif
877}
878
879private altstep as_main_ranap() runs on RAN_Emulation_CT {
880#ifdef RAN_EMULATION_RANAP
881 var RANAP_N_UNITDATA_ind rud_ind;
882 var RANAP_N_CONNECT_ind rconn_ind;
883 var RANAP_N_CONNECT_cfm rconn_cfm;
884 var RANAP_N_DATA_ind rdata_ind;
885 var RANAP_N_DISCONNECT_ind rdisc_ind;
886 var RANAP_Conn_Req creq;
887 var RANAP_PDU ranap;
888 var RAN_ConnHdlr vc_conn;
889
890 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
891 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
892 /* Connectionless Procedures like RESET */
893 var template RANAP_PDU resp;
894 resp := CommonRanapUnitdataCallback(rud_ind.userData);
895 if (isvalue(resp)) {
896 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
897 rud_ind.calledAddress, resp));
898 }
899 }
900 /* SCCP -> Client: new connection from BSC */
901 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
902 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
903 /* store mapping between client components and SCCP connectionId */
904 f_conn_table_add(vc_conn, rconn_ind.connectionId);
905 /* handle user payload */
906 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
907 /* confirm connection establishment */
908 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
909 }
910 /* SCCP -> Client: connection-oriented data in existing connection */
911 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
912 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
913 if (ispresent(rdata_ind.userData)) {
914 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
915 }
916 }
917 /* SCCP -> Client: disconnect of an existing connection */
918 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
919 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
920 if (ispresent(rdisc_ind.userData)) {
921 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
922 }
923 /* notify client about termination */
924 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
925 CLIENT.send(prim) to vc_conn;
926 f_conn_table_del(rdisc_ind.connectionId);
927 /* TOOD: return confirm to other side? */
928 }
929 /* SCCP -> Client: connection confirm for outbound connection */
930 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
931 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
932 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
933 CLIENT.send(prim) to vc_conn;
934 /* handle user payload */
935 if (ispresent(rconn_cfm.userData)) {
936 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
937 }
938 }
939
940 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
941 var integer conn_id := f_conn_id_by_comp(vc_conn);
942 /* send it to dispatcher */
943 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
944 }
945
946 /* Disconnect request client -> SCCP */
947 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
948 var integer conn_id := f_conn_id_by_comp(vc_conn);
949 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
950 f_conn_table_del(conn_id);
951 }
952
953 /* BSSAP from client -> SCCP */
954 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
955 var integer conn_id;
956 /* send to dispatcher */
957
958 if (f_comp_known(vc_conn) == false) {
959 /* unknown client, create new connection */
960 conn_id := f_gen_conn_id();
961
962 /* store mapping between client components and SCCP connectionId */
963 f_conn_table_add(vc_conn, conn_id);
964
965 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
966 creq.ranap));
967 } else {
968 /* known client, send via existing connection */
969 conn_id := f_conn_id_by_comp(vc_conn);
970 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
971 }
972
973 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
974 * counter only on MM/CC/SS, but not on RR */
975 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
976 /* we have just sent the first MM message, increment the counter */
977 var integer idx := f_idx_by_comp(vc_conn);
978 ConnectionTable[idx].n_sd[0] := 1;
979 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
980 }
981 }
982
983#else
984 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +0200985#endif
986}
Harald Welteb3414b22017-11-23 18:22:10 +0100987
Harald Welte2fce7882019-04-15 11:48:05 +0200988private altstep as_main_mgcp() runs on RAN_Emulation_CT {
989#ifdef RAN_EMULATION_MGCP
990 var MgcpCommand mgcp_req;
991 var MgcpResponse mgcp_resp;
992 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +0100993
Harald Weltec82eef42017-11-24 20:40:12 +0100994 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
995 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
996 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
997 * is printed as hex string, e.g. a@mgw for CIC 10 */
998
999 /* CLIENT -> MGCP */
1000 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
1001 /* MGCP request from Handler (we're MSC) */
1002 /* store the transaction ID we've seen */
1003 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
1004 /* simply forward any MGCP from the client to the port */
1005 MGCP.send(mgcp_req);
1006 }
1007 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
1008 /* MGCP response from Handler (we're BSC/MGW) */
1009 /* simply forward any MGCP from the client to the port */
1010 MGCP.send(mgcp_resp);
1011 }
1012
1013 /* MGCP -> CLIENT */
1014 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1015 /* MGCP request from network side (we're BSC/MGW) */
1016 /* Extract CIC from local part of endpoint name */
1017 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001018 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1019 /* ignore RSIP for unknown CIC */
1020 } else {
1021 /* Resolve the vc_conn by the CIC */
1022 vc_conn := f_comp_by_cic(cic);
1023 CLIENT.send(mgcp_req) to vc_conn;
1024 }
Harald Weltec82eef42017-11-24 20:40:12 +01001025 }
1026 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1027 /* MGCP response from network side (we're MSC) */
1028 /* Resolve the vc_conn by the transaction ID */
1029 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1030 CLIENT.send(mgcp_resp) to vc_conn;
1031 }
Harald Welte2fce7882019-04-15 11:48:05 +02001032#else
1033 [false] CLIENT.receive {}
1034#endif
1035}
Harald Weltec82eef42017-11-24 20:40:12 +01001036
Harald Welte2fce7882019-04-15 11:48:05 +02001037/* send a raw (encoded) L3 message over given SCCP connection */
1038private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1039{
1040 select (g_ran_ops.protocol) {
1041#ifdef RAN_EMULATION_BSSAP
1042 case (RAN_PROTOCOL_BSSAP) {
1043 var PDU_BSSAP bssap;
1044 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1045 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1046 }
1047#endif
Harald Welte5b027622019-04-14 23:40:17 +02001048#ifdef RAN_EMULATION_RANAP
1049 case (RAN_PROTOCOL_RANAP) {
1050 var RANAP_PDU ranap;
1051 if (false /* SAPI */) {
1052 var RANAP_IEs.SAPI sapi := sapi_0;
1053 ranap := valueof(ts_RANAP_DirectTransferSAPI(l3_enc, sapi));
1054 } else {
1055 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc));
1056 }
1057 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1058 }
1059#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001060 }
1061}
1062
1063function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1064
1065 g_ran_id := id;
1066 g_ran_ops := ops;
1067 f_conn_table_init();
1068 f_expect_table_init();
1069
1070 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1071 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001072 select (g_ran_ops.protocol) {
1073#ifdef RAN_EMULATION_BSSAP
1074 case (RAN_PROTOCOL_BSSAP) {
1075 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1076 }
1077#endif
1078#ifdef RAN_EMULATION_RANAP
1079 case (RAN_PROTOCOL_RANAP) {
1080 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1081 }
1082#endif
1083 }
Harald Welte2fce7882019-04-15 11:48:05 +02001084 }
1085
1086 while (true) {
1087 var RAN_ConnHdlr vc_conn;
1088 var PDU_DTAP_MO dtap_mo;
1089 var PDU_DTAP_MT dtap_mt;
1090 var RAN_ConnHdlr vc_hdlr;
1091 var octetstring l3_info;
1092 var hexstring imsi;
1093 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001094 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001095
1096 alt {
1097 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001098 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001099
1100 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1101 var integer idx := f_idx_by_comp(vc_conn);
1102 /* convert from decoded DTAP to encoded DTAP */
1103 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1104 /* patch correct L3 send sequence number N(SD) into l3_enc */
1105 if (dtap_mo.skip_seq_patching == false) {
1106 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1107 }
1108 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1109 }
1110
1111 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1112 var integer idx := f_idx_by_comp(vc_conn);
1113 /* convert from decoded DTAP to encoded DTAP */
1114 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1115 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1116 }
1117
1118 [] as_main_mgcp();
Harald Welte624f9632017-12-16 19:26:04 +01001119
Harald Welte6811d102019-04-14 22:23:14 +02001120 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001121 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001122 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001123 }
1124
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001125 [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
1126 f_create_expect(omit, vc_hdlr, targetPointCode);
1127 PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
1128 }
1129
Harald Welte6811d102019-04-14 22:23:14 +02001130 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001131 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001132 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001133 }
1134
1135
Harald Welte365f4ed2017-11-23 00:00:43 +01001136 }
1137 }
1138}
1139
Harald Welte2fce7882019-04-15 11:48:05 +02001140#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001141private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001142 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001143 return hex2int(str2hex(local_part));
1144
1145}
Harald Welte2fce7882019-04-15 11:48:05 +02001146#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001147
Harald Welte624f9632017-12-16 19:26:04 +01001148/***********************************************************************
1149 * "Expect" Handling (mapping for expected incoming SCCP connections)
1150 ***********************************************************************/
1151
1152/* data about an expected future incoming connection */
1153type record ExpectData {
1154 /* L3 payload based on which we can match it */
1155 octetstring l3_payload optional,
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001156 integer handoverRequestPointCode optional,
Harald Welte624f9632017-12-16 19:26:04 +01001157 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001158 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001159}
1160
1161/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001162signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001163signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001164
Harald Welte17d21152018-01-27 00:47:11 +01001165/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001166signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001167
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001168/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1169signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1170
1171/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1172signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1173
Harald Welte6811d102019-04-14 22:23:14 +02001174type port RAN_PROC_PT procedure {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001175 inout RAN_register, RAN_register_imsi, RAN_register_handoverRequest, RAN_last_n_sd, RAN_continue_after_n_sd;
Harald Welte624f9632017-12-16 19:26:04 +01001176} with { extension "internal" };
1177
Harald Welte5b027622019-04-14 23:40:17 +02001178#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001179/* CreateCallback that can be used as create_cb and will use the expectation table */
1180function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001181runs on RAN_Emulation_CT return RAN_ConnHdlr {
1182 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001183 var octetstring l3_info;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001184 var boolean handoverRequest := false;
1185 var integer handoverRequestPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001186 var integer i;
1187
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001188 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1189 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1190 log("ExpectedCreateCallback completeLayer3Information");
1191 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
1192 handoverRequest := true;
1193 handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1194 log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode);
1195 } else {
1196 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001197 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001198 return ret;
1199 }
Harald Welte624f9632017-12-16 19:26:04 +01001200
1201 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001202 if (handoverRequest) {
1203 log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode,
1204 " ==? ", handoverRequestPointCode);
1205 if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) {
1206 ret := ExpectTable[i].vc_conn;
1207 log("Found Expect[", i, "] for handoverRequest handled at ", ret);
1208 return ret;
1209 } else {
1210 continue;
1211 }
1212 }
Harald Welte624f9632017-12-16 19:26:04 +01001213 if (not ispresent(ExpectTable[i].l3_payload)) {
1214 continue;
1215 }
1216 if (l3_info == ExpectTable[i].l3_payload) {
1217 ret := ExpectTable[i].vc_conn;
1218 /* release this entry to be used again */
1219 ExpectTable[i].l3_payload := omit;
1220 ExpectTable[i].vc_conn := null;
1221 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1222 /* return the component reference */
1223 return ret;
1224 }
1225 }
1226 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001227 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001228 return ret;
1229}
Harald Welte5b027622019-04-14 23:40:17 +02001230#endif
1231
1232#ifdef RAN_EMULATION_RANAP
1233/* CreateCallback that can be used as create_cb and will use the expectation table */
1234function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1235runs on RAN_Emulation_CT return RAN_ConnHdlr {
1236 var RAN_ConnHdlr ret := null;
1237 var template (omit) octetstring l3_info;
1238 var integer i;
1239
1240 l3_info := f_ranap_extract_l3(conn_ind.userData);
1241 if (istemplatekind(l3_info, "omit")) {
1242 setverdict(fail, "N-CONNECT.ind without NAS payload");
1243 mtc.stop;
1244 return ret;
1245 }
1246
1247 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
1248 if (not ispresent(ExpectTable[i].l3_payload)) {
1249 continue;
1250 }
1251 if (valueof(l3_info) == ExpectTable[i].l3_payload) {
1252 ret := ExpectTable[i].vc_conn;
1253 /* release this entry to be used again */
1254 ExpectTable[i].l3_payload := omit;
1255 ExpectTable[i].vc_conn := null;
1256 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1257 /* return the component reference */
1258 return ret;
1259 }
1260 }
1261 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1262 mtc.stop;
1263 return ret;
1264}
1265#endif
Harald Welte624f9632017-12-16 19:26:04 +01001266
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001267private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
1268 template integer handoverRequestPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001269runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001270 var integer i;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001271 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001272 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001273 if (not ispresent(ExpectTable[i].l3_payload)
1274 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
1275 if (ispresent(l3)) {
1276 ExpectTable[i].l3_payload := valueof(l3);
1277 }
1278 if (ispresent(handoverRequestPointCode)) {
1279 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
1280 }
Harald Welte624f9632017-12-16 19:26:04 +01001281 ExpectTable[i].vc_conn := hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001282 if (ispresent(handoverRequestPointCode)) {
1283 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
1284 } else {
1285 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1286 }
Harald Welte624f9632017-12-16 19:26:04 +01001287 return;
1288 }
1289 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001290 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +01001291}
1292
Harald Welte6811d102019-04-14 22:23:14 +02001293private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1294runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001295 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1296 if (not ispresent(ImsiTable[i].imsi)) {
1297 ImsiTable[i].imsi := imsi;
1298 ImsiTable[i].tmsi := tmsi;
1299 ImsiTable[i].comp_ref := hdlr;
1300 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1301 return;
1302 }
1303 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001304 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +01001305}
1306
1307
Daniel Willmannd47106b2018-01-17 12:20:56 +01001308private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001309runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001310 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1311 ExpectTable[i].l3_payload := omit;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001312 ExpectTable[i].handoverRequestPointCode := omit;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001313 }
1314}
Harald Welte624f9632017-12-16 19:26:04 +01001315
Harald Welte17d21152018-01-27 00:47:11 +01001316/* helper function for clients to register their IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001317function f_ran_register_imsi(hexstring imsi, OCT4 tmsi)
1318runs on RAN_ConnHdlr {
1319 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1320 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001321 }
1322}
1323
Harald Welte475a2c12019-05-02 19:05:48 +02001324#ifdef RAN_EMULATION_RANAP
1325/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1326altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1327 var RANAP_PDU ranap;
1328 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1329 BSSAP.send(ts_RANAP_IuReleaseComplete);
1330 alt {
1331 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1332 setverdict(pass);
1333 }
1334 [] BSSAP.receive {
1335 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1336 mtc.stop;
1337 }
1338 }
1339 }
1340 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1341 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1342 mtc.stop;
1343 }
1344}
1345#endif
1346
Harald Welte17d21152018-01-27 00:47:11 +01001347
Harald Welte365f4ed2017-11-23 00:00:43 +01001348}