blob: 9a65eb725f3792110ce88f0d059a582286ce3dc9 [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
Harald Welte0b476062018-01-21 19:07:32 +010078/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
79type record PDU_DTAP_MO {
80 OCT1 dlci optional,
Daniel Willmann92f66272018-02-06 15:50:52 +010081 boolean skip_seq_patching optional,
Harald Welte0b476062018-01-21 19:07:32 +010082 PDU_ML3_MS_NW dtap
83}
84
85/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
86type record PDU_DTAP_MT {
87 OCT1 dlci optional,
88 PDU_ML3_NW_MS dtap
89}
90
91template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
92 dlci := dlci,
93 dtap := dtap
94}
95
Daniel Willmann92f66272018-02-06 15:50:52 +010096template 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 +010097 dlci := dlci,
Daniel Willmann92f66272018-02-06 15:50:52 +010098 skip_seq_patching := skip_seq_patching,
Harald Welte0b476062018-01-21 19:07:32 +010099 dtap := dtap
100}
101
102template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
103 dlci := dlci,
104 dtap := dtap
105}
106
107template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
108 dlci := dlci,
Harald Welte930d0a72018-03-22 22:08:40 +0100109 skip_seq_patching := ?,
Harald Welte0b476062018-01-21 19:07:32 +0100110 dtap := dtap
111}
112
Harald Welte365f4ed2017-11-23 00:00:43 +0100113/* port between individual per-connection components and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +0200114type port RAN_Conn_PT message {
Harald Welte2fce7882019-04-15 11:48:05 +0200115 inout
116#ifdef RAN_EMULATION_BSSAP
117 PDU_BSSAP,
Harald Weltea4ca4462018-02-09 00:17:14 +0100118 /* Client requests us to create SCCP Connection */
119 BSSAP_Conn_Req,
Harald Welte2fce7882019-04-15 11:48:05 +0200120#endif
Harald Welte5b027622019-04-14 23:40:17 +0200121#ifdef RAN_EMULATION_RANAP
122 RANAP_PDU,
123 /* Client requests us to create SCCP Connection */
124 RANAP_Conn_Req,
125#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200126#ifdef RAN_EMULATION_MGCP
Harald Weltea4ca4462018-02-09 00:17:14 +0100127 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
Harald Welte2fce7882019-04-15 11:48:05 +0200128 MgcpCommand, MgcpResponse,
129#endif
130 /* direct DTAP messages from/to clients */
131 PDU_DTAP_MO, PDU_DTAP_MT,
132 /* misc indications / requests between SCCP and client */
133 RAN_Conn_Prim;
Harald Welte365f4ed2017-11-23 00:00:43 +0100134} with { extension "internal" };
135
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200136type uint2_t N_Sd_Array[4];
Harald Welte365f4ed2017-11-23 00:00:43 +0100137
138/* represents a single BSSAP connection over SCCP */
139type record ConnectionData {
140 /* reference to the instance of the per-connection component */
Harald Welte6811d102019-04-14 22:23:14 +0200141 RAN_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100142 integer sccp_conn_id,
Harald Welte2fce7882019-04-15 11:48:05 +0200143#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100144 /* most recent MGCP transaction ID (Used on MSC side) */
145 MgcpTransId mgcp_trans_id optional,
Harald Welte2fce7882019-04-15 11:48:05 +0200146#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100147 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100148 integer cic optional,
149 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200150 N_Sd_Array n_sd
Harald Welte365f4ed2017-11-23 00:00:43 +0100151}
152
Harald Welte17d21152018-01-27 00:47:11 +0100153type record ImsiMapping {
Harald Welte6811d102019-04-14 22:23:14 +0200154 RAN_ConnHdlr comp_ref,
Harald Welte17d21152018-01-27 00:47:11 +0100155 hexstring imsi optional,
156 OCT4 tmsi
157}
158
Harald Welte6811d102019-04-14 22:23:14 +0200159type component RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200160 /* SCCP ports on the bottom side, using ASP primitives */
161#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100162 port BSSAP_CODEC_PT BSSAP;
Harald Welte2fce7882019-04-15 11:48:05 +0200163#endif
Harald Welte5b027622019-04-14 23:40:17 +0200164#ifdef RAN_EMULATION_RANAP
165 port RANAP_CODEC_PT RANAP;
166#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100167 /* BSSAP port to the per-connection clients */
Harald Welte6811d102019-04-14 22:23:14 +0200168 port RAN_Conn_PT CLIENT;
Harald Welte2fce7882019-04-15 11:48:05 +0200169#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100170 /* MGCP port */
171 port IPA_MGCP_PT MGCP;
Harald Welte2fce7882019-04-15 11:48:05 +0200172#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100173
174 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
175 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100176
Harald Welte624f9632017-12-16 19:26:04 +0100177 /* pending expected incoming connections */
178 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100179
180 /* tables for mapping inbound unitdata (like paging) */
181 var ImsiMapping ImsiTable[16];
182
Harald Welte624f9632017-12-16 19:26:04 +0100183 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200184 port RAN_PROC_PT PROC;
Harald Welte624f9632017-12-16 19:26:04 +0100185
Harald Welte6811d102019-04-14 22:23:14 +0200186 var charstring g_ran_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100187 var integer g_next_e1_ts := 1;
Harald Welte6811d102019-04-14 22:23:14 +0200188 var RanOps g_ran_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100189};
190
Harald Welteb3414b22017-11-23 18:22:10 +0100191private function f_conn_id_known(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200192runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100193 var integer i;
194 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
195 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
196 return true;
197 }
198 }
199 return false;
200}
201
Harald Welte6811d102019-04-14 22:23:14 +0200202private function f_comp_known(RAN_ConnHdlr client)
203runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100204 var integer i;
205 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
206 if (ConnectionTable[i].comp_ref == client) {
207 return true;
208 }
209 }
210 return false;
211}
Harald Welte365f4ed2017-11-23 00:00:43 +0100212
Harald Weltee98bb2e2017-11-29 12:09:48 +0100213private function f_cic_known(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200214runs on RAN_Emulation_CT return boolean {
Harald Weltee98bb2e2017-11-29 12:09:48 +0100215 var integer i;
216 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
217 if (ConnectionTable[i].cic == cic) {
218 return true;
219 }
220 }
221 return false;
222}
223
Harald Welte365f4ed2017-11-23 00:00:43 +0100224/* resolve component reference by connection ID */
225private function f_comp_by_conn_id(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200226runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +0100227 var integer i;
228 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
229 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
230 return ConnectionTable[i].comp_ref;
231 }
232 }
Harald Welte6811d102019-04-14 22:23:14 +0200233 setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200234 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100235}
236
Harald Welte2fce7882019-04-15 11:48:05 +0200237
238#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100239/* resolve component reference by CIC */
240private function f_comp_by_mgcp_tid(MgcpTransId tid)
Harald Welte6811d102019-04-14 22:23:14 +0200241runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100242 var integer i;
243 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
244 if (ConnectionTable[i].mgcp_trans_id == tid) {
245 return ConnectionTable[i].comp_ref;
246 }
247 }
Harald Welte6811d102019-04-14 22:23:14 +0200248 setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200249 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100250}
251
Harald Welte6811d102019-04-14 22:23:14 +0200252private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid)
253runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100254 var integer i;
255 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
256 if (ConnectionTable[i].comp_ref == client) {
257 ConnectionTable[i].mgcp_trans_id := tid;
258 return;
259 }
260 }
Harald Welte6811d102019-04-14 22:23:14 +0200261 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200262 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100263}
Harald Welte2fce7882019-04-15 11:48:05 +0200264#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100265
266private function f_comp_by_cic(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200267runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100268 var integer i;
269 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
270 if (ConnectionTable[i].cic == cic) {
271 return ConnectionTable[i].comp_ref;
272 }
273 }
Harald Welte6811d102019-04-14 22:23:14 +0200274 setverdict(fail, "RAN Connection table not found by CIC ", cic);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200275 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100276}
277
Harald Welte6811d102019-04-14 22:23:14 +0200278private function f_comp_store_cic(RAN_ConnHdlr client, integer cic)
279runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100280 var integer i;
281 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
282 if (ConnectionTable[i].comp_ref == client) {
283 ConnectionTable[i].cic := cic;
284 return;
285 }
286 }
Harald Welte6811d102019-04-14 22:23:14 +0200287 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200288 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100289}
290
Harald Welte365f4ed2017-11-23 00:00:43 +0100291/* resolve connection ID by component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200292private function f_conn_id_by_comp(RAN_ConnHdlr client)
293runs on RAN_Emulation_CT return integer {
Harald Welte365f4ed2017-11-23 00:00:43 +0100294 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
295 if (ConnectionTable[i].comp_ref == client) {
296 return ConnectionTable[i].sccp_conn_id;
297 }
298 }
Harald Welte6811d102019-04-14 22:23:14 +0200299 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200300 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100301}
302
Harald Welte9dadc522018-02-06 13:56:04 +0100303/* resolve ConnectionTable index component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200304private function f_idx_by_comp(RAN_ConnHdlr client)
305runs on RAN_Emulation_CT return integer {
Harald Welte9dadc522018-02-06 13:56:04 +0100306 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
307 if (ConnectionTable[i].comp_ref == client) {
308 return i;
309 }
310 }
Harald Welte6811d102019-04-14 22:23:14 +0200311 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200312 mtc.stop;
Harald Welte9dadc522018-02-06 13:56:04 +0100313}
314
Harald Welteb3414b22017-11-23 18:22:10 +0100315private function f_gen_conn_id()
Harald Welte6811d102019-04-14 22:23:14 +0200316runs on RAN_Emulation_CT return integer {
Harald Welteb3414b22017-11-23 18:22:10 +0100317 var integer conn_id;
318
319 do {
320 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
321 } while (f_conn_id_known(conn_id) == true);
322
323 return conn_id;
324}
325
Harald Welte365f4ed2017-11-23 00:00:43 +0100326private function f_conn_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200327runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100328 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
329 ConnectionTable[i].comp_ref := null;
330 ConnectionTable[i].sccp_conn_id := -1;
Harald Welte2fce7882019-04-15 11:48:05 +0200331#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100332 ConnectionTable[i].mgcp_trans_id := omit;
Harald Welte2fce7882019-04-15 11:48:05 +0200333#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100334 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100335 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100336 }
Harald Welte17d21152018-01-27 00:47:11 +0100337 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
338 ImsiTable[i].comp_ref := null;
339 ImsiTable[i].imsi := omit;
340 ImsiTable[i].tmsi := 'FFFFFFFF'O;
341 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100342}
343
Harald Welte6811d102019-04-14 22:23:14 +0200344private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id)
345runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100346 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
347 if (ConnectionTable[i].sccp_conn_id == -1) {
348 ConnectionTable[i].comp_ref := comp_ref;
349 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100350 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100351 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100352 return;
353 }
354 }
Harald Welte6811d102019-04-14 22:23:14 +0200355 testcase.stop("RAN Connection table full!");
Harald Welte365f4ed2017-11-23 00:00:43 +0100356}
357
358private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200359runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100360 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
361 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100362 log("Deleted conn table entry ", i,
363 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100364 ConnectionTable[i].sccp_conn_id := -1;
365 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100366 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100367 }
368 }
Harald Welte6811d102019-04-14 22:23:14 +0200369 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200370 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100371}
372
Harald Welte17d21152018-01-27 00:47:11 +0100373private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200374runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100375 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
376 if (ImsiTable[i].imsi == imsi or
377 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
378 return ImsiTable[i].comp_ref;
379 }
380 }
381 return null;
382}
383
Harald Welte2fce7882019-04-15 11:48:05 +0200384#ifdef RAN_EMULATION_BSSAP
385type record BSSAP_Conn_Req {
386 SCCP_PAR_Address addr_peer,
387 SCCP_PAR_Address addr_own,
388 PDU_BSSAP bssap
389}
390template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := {
391 addr_peer := peer,
392 addr_own := own,
393 bssap := bssap
394};
395
Harald Welte365f4ed2017-11-23 00:00:43 +0100396/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200397private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
398runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100399 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100400
Harald Welte473676b2018-01-27 20:38:01 +0100401 if (ischosen(bssap.pdu.bssmap)) {
402 /* BSC Side: If this is an assignment command, store CIC */
403 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
404 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
405 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
406 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
407 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
408 f_comp_store_cic(client, cic);
409 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100410 }
411
Harald Welte6811d102019-04-14 22:23:14 +0200412 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
413 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100414 /* we are the MS, so any message to us must be MT */
415 var PDU_DTAP_MT mt := {
416 dlci := bssap.dlci,
417 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
418 };
419 CLIENT.send(mt) to client;
420 } else {
421 /* we are the Network, so any message to us must be MO */
422 var PDU_DTAP_MO mo := {
423 dlci := bssap.dlci,
424 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
425 };
426 CLIENT.send(mo) to client;
427 }
428 } else {
429 CLIENT.send(bssap) to client;
430 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100431}
432
433/* call-back type, to be provided by specific implementation; called when new SCCP connection
434 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100435type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200436runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100437
438type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200439runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100440
Harald Welte17d21152018-01-27 00:47:11 +0100441/* handle common Unitdata such as Paging */
442private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200443runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100444 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200445 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100446 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
447 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200448 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100449 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
450 client);
451 CLIENT.send(bssap) to client;
452 return omit;
453 }
454 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
455 } else {
456 log("CommonBssmapUnitdataCallback: Not a paging message");
457 }
458 /* ELSE: handle in user callback */
Harald Welte6811d102019-04-14 22:23:14 +0200459 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100460}
461
Harald Welte6811d102019-04-14 22:23:14 +0200462private function f_bssap_wait_for_reset() runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100463 var BSSAP_N_UNITDATA_ind ud_ind;
464 timer T := 20.0;
465
466 T.start;
467 alt {
468 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
469 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
470 ts_BSSMAP_ResetAck));
471 }
472 [] as_reset_ack();
473 [] BSSAP.receive {
474 repeat;
475 }
476 [] T.timeout {
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200477 setverdict(fail, "Timeout waiting for BSSAP RESET");
478 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100479 }
480 }
481}
482
Harald Welte6811d102019-04-14 22:23:14 +0200483function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100484 timer T := 5.0;
485
486 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0)));
487 T.start;
488 alt {
489 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck)) {
490 log("Received RESET-ACK in response to RESET, we're ready to go!");
491 }
492 [] as_reset_ack();
493 [] BSSAP.receive { repeat };
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200494 [] T.timeout {
495 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
496 mtc.stop;
497 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100498 }
499}
500
Harald Welte2fce7882019-04-15 11:48:05 +0200501private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
502 var template octetstring l3 := f_bssap_extract_l3(bssap);
503 return f_L3_is_rr(l3);
504}
505#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100506
Harald Welte5b027622019-04-14 23:40:17 +0200507#ifdef RAN_EMULATION_RANAP
508type record RANAP_Conn_Req {
509 SCCP_PAR_Address addr_peer,
510 SCCP_PAR_Address addr_own,
511 RANAP_PDU ranap
512}
513template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, RANAP_PDU ranap) := {
514 addr_peer := peer,
515 addr_own := own,
516 ranap := ranap
517};
518
519private function fake_dlci_from_sapi(template (omit) SAPI sapi) return template (omit) OCT1
520{
521 if (istemplatekind(sapi, "omit")) {
522 return omit;
523 } else if (valueof(sapi) == sapi_3) {
524 return '03'O;
525 }
526 return '00'O;
527}
528
529private function f_handle_userData_RANAP(RAN_ConnHdlr client, RANAP_PDU ranap)
530runs on RAN_Emulation_CT {
531 /* decode + send decoded RANAP to client */
532 var template (omit) octetstring l3 := f_ranap_extract_l3(ranap);
533 if (istemplatekind(l3, "omit")) {
534 CLIENT.send(ranap) to client;
535 } else {
536 var template (omit) SAPI sapi := f_ranap_extract_sapi(ranap);
537 var template (omit) OCT1 dlci := fake_dlci_from_sapi(sapi);
538 if (g_ran_ops.role_ms) {
539 /* we are the MS, so any message to us must be MT */
540 var PDU_DTAP_MT mt := {
541 dlci := omit,
542 dtap := dec_PDU_ML3_NW_MS(valueof(l3))
543 };
544 if (isvalue(dlci)) {
545 mt.dlci := valueof(dlci)
546 }
547 CLIENT.send(mt) to client;
548 } else {
549 /* we are the Network, so any message to us must be MO */
550 var PDU_DTAP_MO mo := {
551 dlci := omit,
552 dtap := dec_PDU_ML3_MS_NW(valueof(l3))
553 };
554 if (isvalue(dlci)) {
555 mo.dlci := valueof(dlci)
556 }
557 CLIENT.send(mo) to client;
558 }
559 }
560}
561
562/* call-back type, to be provided by specific implementation; called when new SCCP connection
563 * arrives */
564type function RanapCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
565runs on RAN_Emulation_CT return RAN_ConnHdlr;
566
567type function RanapUnitdataCallback(RANAP_PDU ranap)
568runs on RAN_Emulation_CT return template RANAP_PDU;
569
570private function CommonRanapUnitdataCallback(RANAP_PDU ranap)
571runs on RAN_Emulation_CT return template RANAP_PDU {
572 if (match(ranap, tr_RANAP_Paging(?, ?))) {
573 var RAN_ConnHdlr client := null;
574 /* extract IMSI and (if present) TMSI */
575 var IMSI imsi := ranap.initiatingMessage.value_.paging.protocolIEs[1].value_.permanentNAS_UE_ID.iMSI;
576 var template OCT4 tmsi := omit;
577 if (lengthof(ranap.initiatingMessage.value_.paging.protocolIEs) > 2 and
578 ranap.initiatingMessage.value_.paging.protocolIEs[2].id == id_TemporaryUE_ID) {
579 var TemporaryUE_ID ue_id;
580 ue_id := ranap.initiatingMessage.value_.paging.protocolIEs[2].value_.temporaryUE_ID;
581 if (ischosen(ue_id.tMSI)) {
582 tmsi := ue_id.tMSI;
583 } else {
584 tmsi := ue_id.p_TMSI;
585 }
586 }
587 client := f_imsi_table_find(oct2hex(imsi), tmsi);
Harald Welte03d4e2b2019-05-10 00:42:33 +0200588 if (client != null) {
Harald Welte5b027622019-04-14 23:40:17 +0200589 log("CommonRanapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
590 client);
591 CLIENT.send(ranap) to client;
592 return omit;
593 }
594 log("CommonRanapUnitdataCallback: IMSI/TMSI not found in table");
595 } else {
596 log("CommonRanapUnitdataCallback: Not a paging message");
597 }
598
599 /* ELSE: handle in user callback */
600 return g_ran_ops.ranap_unitdata_cb.apply(ranap);
601}
602
603private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean {
604 var template (omit) SAPI sapi;
605 var template octetstring l3 := f_ranap_extract_l3(ranap);
606 return f_L3_is_rr(l3);
607}
608
609function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
610 timer T := 5.0;
611 var CN_DomainIndicator dom;
612 if (g_ran_ops.ps_domain) {
613 dom := ps_domain;
614 } else {
615 dom := cs_domain;
616 }
617
618 RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom)));
619 T.start;
620 alt {
621 [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) {
622 log("Received RESET-ACK in response to RESET, we're ready to go!");
623 }
624 [] as_reset_ack();
625 [] RANAP.receive { repeat };
626 [] T.timeout {
627 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
628 mtc.stop;
629 }
630 }
631}
632#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100633
Harald Welte2fce7882019-04-15 11:48:05 +0200634
635type enumerated RanProtocol {
Harald Welte5b027622019-04-14 23:40:17 +0200636 RAN_PROTOCOL_BSSAP,
637 RAN_PROTOCOL_RANAP
Harald Welte2fce7882019-04-15 11:48:05 +0200638}
639
640type record RanOps {
641#ifdef RAN_EMULATION_BSSAP
642 BssmapCreateCallback create_cb optional,
643 BssmapUnitdataCallback unitdata_cb optional,
644#endif
Harald Welte5b027622019-04-14 23:40:17 +0200645#ifdef RAN_EMULATION_RANAP
646 RanapCreateCallback ranap_create_cb optional,
647 RanapUnitdataCallback ranap_unitdata_cb optional,
648 boolean ps_domain,
649#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200650 boolean decode_dtap,
651 boolean role_ms,
652 RanProtocol protocol,
653 /* needed for performing BSSMAP RESET */
654 SCCP_PAR_Address sccp_addr_local optional,
655 SCCP_PAR_Address sccp_addr_peer optional
656}
657
658template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
659
660private function f_L3_is_rr(template octetstring l3) return boolean {
661 if (not isvalue(l3)) {
662 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100663 }
Harald Welte2fce7882019-04-15 11:48:05 +0200664 var octetstring l3v := valueof(l3);
665 if (lengthof(l3v) < 1) {
666 return false;
667 }
668 /* lower 4 bits of first octet are protocol discriminator */
669 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
670 return true;
671 }
672 return false;
673}
Harald Weltea4ca4462018-02-09 00:17:14 +0100674
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200675function 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 +0200676 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200677 if (n_sd_idx == 0) {
678 seq_nr := n_sd[0];
679 n_sd[0] := (n_sd[0] + 1) mod 4;
680 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
681 seq_nr := n_sd[n_sd_idx];
682 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200683 } else {
684 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200685 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200686 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200687 return seq_nr;
688}
689
690/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
691function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200692 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
693 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
694 log("patched enc_l3: ", enc_l3);
695}
696
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200697function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
698 var uint2_t seq_nr;
699 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
700 return 0;
701 } else if (ischosen(dtap.msgs.gcc)) {
702 return 1;
703 } else if (ischosen(dtap.msgs.bcc)) {
704 return 2;
705 } else if (ischosen(dtap.msgs.loc)) {
706 return 3;
707 }
708 /* no sequence number to patch */
709 return -1;
710}
711
712/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
713function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
714 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
715 if (n_sd_idx < 0) {
716 return;
717 }
718 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
719 f_ML3_patch_seq_nr(seq_nr, enc_l3);
720}
721
Harald Welte2fce7882019-04-15 11:48:05 +0200722private altstep as_reset_ack() runs on RAN_Emulation_CT {
723#ifdef RAN_EMULATION_BSSAP
724 var BSSAP_N_UNITDATA_ind ud_ind;
725#endif
Harald Welte5b027622019-04-14 23:40:17 +0200726#ifdef RAN_EMULATION_RANAP
727 var RANAP_N_UNITDATA_ind rud_ind;
728#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200729#ifdef RAN_EMULATION_BSSAP
730 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind {
731 log("Respoding to inbound RESET with RESET-ACK");
732 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
733 ts_BSSMAP_ResetAck));
734 repeat;
735 }
736#endif
Harald Welte5b027622019-04-14 23:40:17 +0200737#ifdef RAN_EMULATION_RANAP
738 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
739 log("Respoding to inbound IuRESET with IuRESET-ACK");
740 var CN_DomainIndicator dom;
741 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
742 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
743 ts_RANAP_ResetAck(dom)));
744 }
745#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200746}
747
748
749private altstep as_main_bssap() runs on RAN_Emulation_CT {
750#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100751 var BSSAP_N_UNITDATA_ind ud_ind;
752 var BSSAP_N_CONNECT_ind conn_ind;
753 var BSSAP_N_CONNECT_cfm conn_cfm;
754 var BSSAP_N_DATA_ind data_ind;
755 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100756 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100757 var PDU_BSSAP bssap;
Harald Welte2fce7882019-04-15 11:48:05 +0200758 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200759 var integer targetPointCode;
760 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100761
Harald Welte365f4ed2017-11-23 00:00:43 +0100762 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100763 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100764 /* Connectionless Procedures like RESET */
765 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100766 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100767 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100768 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
769 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100770 }
771 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100772 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100773 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200774 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100775 /* store mapping between client components and SCCP connectionId */
776 f_conn_table_add(vc_conn, conn_ind.connectionId);
777 /* handle user payload */
778 f_handle_userData(vc_conn, conn_ind.userData);
779 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100780 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100781 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100782 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100783 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100784 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100785 if (ispresent(data_ind.userData)) {
786 f_handle_userData(vc_conn, data_ind.userData);
787 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100788 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100789 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100790 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100791 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100792 if (ispresent(disc_ind.userData)) {
793 f_handle_userData(vc_conn, disc_ind.userData);
794 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100795 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200796 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100797 CLIENT.send(prim) to vc_conn;
798 f_conn_table_del(disc_ind.connectionId);
799 /* TOOD: return confirm to other side? */
800 }
Harald Welteb3414b22017-11-23 18:22:10 +0100801 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100802 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100803 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200804 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100805 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100806 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100807 if (ispresent(conn_cfm.userData)) {
808 f_handle_userData(vc_conn, conn_cfm.userData);
809 }
Harald Welteb3414b22017-11-23 18:22:10 +0100810 }
Harald Welte2fce7882019-04-15 11:48:05 +0200811 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
812 var integer conn_id := f_conn_id_by_comp(vc_conn);
813 /* send it to dispatcher */
814 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
815 }
Harald Welteb3414b22017-11-23 18:22:10 +0100816
Harald Welte365f4ed2017-11-23 00:00:43 +0100817 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200818 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100819 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100820 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100821 f_conn_table_del(conn_id);
822 }
823
824 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100825 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
826 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100827 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100828
829 if (f_comp_known(vc_conn) == false) {
830 /* unknown client, create new connection */
831 conn_id := f_gen_conn_id();
832
833 /* store mapping between client components and SCCP connectionId */
834 f_conn_table_add(vc_conn, conn_id);
835
Harald Welte004f5fb2017-12-16 17:54:40 +0100836 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
837 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100838 } else {
839 /* known client, send via existing connection */
840 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100841 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100842 }
843
Harald Welte49518bf2018-02-10 11:39:19 +0100844 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
845 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200846 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100847 /* we have just sent the first MM message, increment the counter */
848 var integer idx := f_idx_by_comp(vc_conn);
849 ConnectionTable[idx].n_sd[0] := 1;
850 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
851 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200852 }
Harald Welte9dadc522018-02-06 13:56:04 +0100853
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200854 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
855 var integer idx := f_idx_by_comp(vc_conn);
856 last_n_sd := ConnectionTable[idx].n_sd;
857 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
858 }
859
860 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
861 var integer idx := f_idx_by_comp(vc_conn);
862 ConnectionTable[idx].n_sd := last_n_sd;
863 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100864 }
Harald Welte2fce7882019-04-15 11:48:05 +0200865#else
Harald Welte5b027622019-04-14 23:40:17 +0200866 [false] CLIENT.receive {}
867#endif
868}
869
870private altstep as_main_ranap() runs on RAN_Emulation_CT {
871#ifdef RAN_EMULATION_RANAP
872 var RANAP_N_UNITDATA_ind rud_ind;
873 var RANAP_N_CONNECT_ind rconn_ind;
874 var RANAP_N_CONNECT_cfm rconn_cfm;
875 var RANAP_N_DATA_ind rdata_ind;
876 var RANAP_N_DISCONNECT_ind rdisc_ind;
877 var RANAP_Conn_Req creq;
878 var RANAP_PDU ranap;
879 var RAN_ConnHdlr vc_conn;
880
881 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
882 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
883 /* Connectionless Procedures like RESET */
884 var template RANAP_PDU resp;
885 resp := CommonRanapUnitdataCallback(rud_ind.userData);
886 if (isvalue(resp)) {
887 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
888 rud_ind.calledAddress, resp));
889 }
890 }
891 /* SCCP -> Client: new connection from BSC */
892 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
893 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
894 /* store mapping between client components and SCCP connectionId */
895 f_conn_table_add(vc_conn, rconn_ind.connectionId);
896 /* handle user payload */
897 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
898 /* confirm connection establishment */
899 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
900 }
901 /* SCCP -> Client: connection-oriented data in existing connection */
902 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
903 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
904 if (ispresent(rdata_ind.userData)) {
905 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
906 }
907 }
908 /* SCCP -> Client: disconnect of an existing connection */
909 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
910 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
911 if (ispresent(rdisc_ind.userData)) {
912 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
913 }
914 /* notify client about termination */
915 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
916 CLIENT.send(prim) to vc_conn;
917 f_conn_table_del(rdisc_ind.connectionId);
918 /* TOOD: return confirm to other side? */
919 }
920 /* SCCP -> Client: connection confirm for outbound connection */
921 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
922 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
923 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
924 CLIENT.send(prim) to vc_conn;
925 /* handle user payload */
926 if (ispresent(rconn_cfm.userData)) {
927 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
928 }
929 }
930
931 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
932 var integer conn_id := f_conn_id_by_comp(vc_conn);
933 /* send it to dispatcher */
934 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
935 }
936
937 /* Disconnect request client -> SCCP */
938 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
939 var integer conn_id := f_conn_id_by_comp(vc_conn);
940 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
941 f_conn_table_del(conn_id);
942 }
943
944 /* BSSAP from client -> SCCP */
945 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
946 var integer conn_id;
947 /* send to dispatcher */
948
949 if (f_comp_known(vc_conn) == false) {
950 /* unknown client, create new connection */
951 conn_id := f_gen_conn_id();
952
953 /* store mapping between client components and SCCP connectionId */
954 f_conn_table_add(vc_conn, conn_id);
955
956 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
957 creq.ranap));
958 } else {
959 /* known client, send via existing connection */
960 conn_id := f_conn_id_by_comp(vc_conn);
961 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
962 }
963
964 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
965 * counter only on MM/CC/SS, but not on RR */
966 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
967 /* we have just sent the first MM message, increment the counter */
968 var integer idx := f_idx_by_comp(vc_conn);
969 ConnectionTable[idx].n_sd[0] := 1;
970 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
971 }
972 }
973
974#else
975 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +0200976#endif
977}
Harald Welteb3414b22017-11-23 18:22:10 +0100978
Harald Welte2fce7882019-04-15 11:48:05 +0200979private altstep as_main_mgcp() runs on RAN_Emulation_CT {
980#ifdef RAN_EMULATION_MGCP
981 var MgcpCommand mgcp_req;
982 var MgcpResponse mgcp_resp;
983 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +0100984
Harald Weltec82eef42017-11-24 20:40:12 +0100985 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
986 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
987 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
988 * is printed as hex string, e.g. a@mgw for CIC 10 */
989
990 /* CLIENT -> MGCP */
991 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
992 /* MGCP request from Handler (we're MSC) */
993 /* store the transaction ID we've seen */
994 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
995 /* simply forward any MGCP from the client to the port */
996 MGCP.send(mgcp_req);
997 }
998 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
999 /* MGCP response from Handler (we're BSC/MGW) */
1000 /* simply forward any MGCP from the client to the port */
1001 MGCP.send(mgcp_resp);
1002 }
1003
1004 /* MGCP -> CLIENT */
1005 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1006 /* MGCP request from network side (we're BSC/MGW) */
1007 /* Extract CIC from local part of endpoint name */
1008 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001009 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1010 /* ignore RSIP for unknown CIC */
1011 } else {
1012 /* Resolve the vc_conn by the CIC */
1013 vc_conn := f_comp_by_cic(cic);
1014 CLIENT.send(mgcp_req) to vc_conn;
1015 }
Harald Weltec82eef42017-11-24 20:40:12 +01001016 }
1017 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1018 /* MGCP response from network side (we're MSC) */
1019 /* Resolve the vc_conn by the transaction ID */
1020 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1021 CLIENT.send(mgcp_resp) to vc_conn;
1022 }
Harald Welte2fce7882019-04-15 11:48:05 +02001023#else
1024 [false] CLIENT.receive {}
1025#endif
1026}
Harald Weltec82eef42017-11-24 20:40:12 +01001027
Harald Welte2fce7882019-04-15 11:48:05 +02001028/* send a raw (encoded) L3 message over given SCCP connection */
1029private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1030{
1031 select (g_ran_ops.protocol) {
1032#ifdef RAN_EMULATION_BSSAP
1033 case (RAN_PROTOCOL_BSSAP) {
1034 var PDU_BSSAP bssap;
1035 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1036 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1037 }
1038#endif
Harald Welte5b027622019-04-14 23:40:17 +02001039#ifdef RAN_EMULATION_RANAP
1040 case (RAN_PROTOCOL_RANAP) {
1041 var RANAP_PDU ranap;
1042 if (false /* SAPI */) {
1043 var RANAP_IEs.SAPI sapi := sapi_0;
1044 ranap := valueof(ts_RANAP_DirectTransferSAPI(l3_enc, sapi));
1045 } else {
1046 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc));
1047 }
1048 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1049 }
1050#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001051 }
1052}
1053
1054function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1055
1056 g_ran_id := id;
1057 g_ran_ops := ops;
1058 f_conn_table_init();
1059 f_expect_table_init();
1060
1061 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1062 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001063 select (g_ran_ops.protocol) {
1064#ifdef RAN_EMULATION_BSSAP
1065 case (RAN_PROTOCOL_BSSAP) {
1066 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1067 }
1068#endif
1069#ifdef RAN_EMULATION_RANAP
1070 case (RAN_PROTOCOL_RANAP) {
1071 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1072 }
1073#endif
1074 }
Harald Welte2fce7882019-04-15 11:48:05 +02001075 }
1076
1077 while (true) {
1078 var RAN_ConnHdlr vc_conn;
1079 var PDU_DTAP_MO dtap_mo;
1080 var PDU_DTAP_MT dtap_mt;
1081 var RAN_ConnHdlr vc_hdlr;
1082 var octetstring l3_info;
1083 var hexstring imsi;
1084 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001085 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001086
1087 alt {
1088 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001089 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001090
1091 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1092 var integer idx := f_idx_by_comp(vc_conn);
1093 /* convert from decoded DTAP to encoded DTAP */
1094 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1095 /* patch correct L3 send sequence number N(SD) into l3_enc */
1096 if (dtap_mo.skip_seq_patching == false) {
1097 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1098 }
1099 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1100 }
1101
1102 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1103 var integer idx := f_idx_by_comp(vc_conn);
1104 /* convert from decoded DTAP to encoded DTAP */
1105 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1106 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1107 }
1108
1109 [] as_main_mgcp();
Harald Welte624f9632017-12-16 19:26:04 +01001110
Harald Welte6811d102019-04-14 22:23:14 +02001111 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001112 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001113 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001114 }
1115
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001116 [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
1117 f_create_expect(omit, vc_hdlr, targetPointCode);
1118 PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
1119 }
1120
Harald Welte6811d102019-04-14 22:23:14 +02001121 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001122 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001123 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001124 }
1125
1126
Harald Welte365f4ed2017-11-23 00:00:43 +01001127 }
1128 }
1129}
1130
Harald Welte2fce7882019-04-15 11:48:05 +02001131#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001132private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001133 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001134 return hex2int(str2hex(local_part));
1135
1136}
Harald Welte2fce7882019-04-15 11:48:05 +02001137#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001138
Harald Welte624f9632017-12-16 19:26:04 +01001139/***********************************************************************
1140 * "Expect" Handling (mapping for expected incoming SCCP connections)
1141 ***********************************************************************/
1142
1143/* data about an expected future incoming connection */
1144type record ExpectData {
1145 /* L3 payload based on which we can match it */
1146 octetstring l3_payload optional,
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001147 integer handoverRequestPointCode optional,
Harald Welte624f9632017-12-16 19:26:04 +01001148 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001149 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001150}
1151
1152/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001153signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001154signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001155
Harald Welte17d21152018-01-27 00:47:11 +01001156/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001157signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001158
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001159/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1160signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1161
1162/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1163signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1164
Harald Welte6811d102019-04-14 22:23:14 +02001165type port RAN_PROC_PT procedure {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001166 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 +01001167} with { extension "internal" };
1168
Harald Welte5b027622019-04-14 23:40:17 +02001169#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001170/* CreateCallback that can be used as create_cb and will use the expectation table */
1171function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001172runs on RAN_Emulation_CT return RAN_ConnHdlr {
1173 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001174 var octetstring l3_info;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001175 var boolean handoverRequest := false;
1176 var integer handoverRequestPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001177 var integer i;
1178
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001179 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1180 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1181 log("ExpectedCreateCallback completeLayer3Information");
1182 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
1183 handoverRequest := true;
1184 handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1185 log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode);
1186 } else {
1187 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001188 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001189 return ret;
1190 }
Harald Welte624f9632017-12-16 19:26:04 +01001191
1192 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001193 if (handoverRequest) {
1194 log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode,
1195 " ==? ", handoverRequestPointCode);
1196 if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) {
1197 ret := ExpectTable[i].vc_conn;
1198 log("Found Expect[", i, "] for handoverRequest handled at ", ret);
1199 return ret;
1200 } else {
1201 continue;
1202 }
1203 }
Harald Welte624f9632017-12-16 19:26:04 +01001204 if (not ispresent(ExpectTable[i].l3_payload)) {
1205 continue;
1206 }
1207 if (l3_info == ExpectTable[i].l3_payload) {
1208 ret := ExpectTable[i].vc_conn;
1209 /* release this entry to be used again */
1210 ExpectTable[i].l3_payload := omit;
1211 ExpectTable[i].vc_conn := null;
1212 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1213 /* return the component reference */
1214 return ret;
1215 }
1216 }
1217 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001218 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001219 return ret;
1220}
Harald Welte5b027622019-04-14 23:40:17 +02001221#endif
1222
1223#ifdef RAN_EMULATION_RANAP
1224/* CreateCallback that can be used as create_cb and will use the expectation table */
1225function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1226runs on RAN_Emulation_CT return RAN_ConnHdlr {
1227 var RAN_ConnHdlr ret := null;
1228 var template (omit) octetstring l3_info;
1229 var integer i;
1230
1231 l3_info := f_ranap_extract_l3(conn_ind.userData);
1232 if (istemplatekind(l3_info, "omit")) {
1233 setverdict(fail, "N-CONNECT.ind without NAS payload");
1234 mtc.stop;
1235 return ret;
1236 }
1237
1238 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
1239 if (not ispresent(ExpectTable[i].l3_payload)) {
1240 continue;
1241 }
1242 if (valueof(l3_info) == ExpectTable[i].l3_payload) {
1243 ret := ExpectTable[i].vc_conn;
1244 /* release this entry to be used again */
1245 ExpectTable[i].l3_payload := omit;
1246 ExpectTable[i].vc_conn := null;
1247 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1248 /* return the component reference */
1249 return ret;
1250 }
1251 }
1252 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1253 mtc.stop;
1254 return ret;
1255}
1256#endif
Harald Welte624f9632017-12-16 19:26:04 +01001257
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001258private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
1259 template integer handoverRequestPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001260runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001261 var integer i;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001262 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001263 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001264 if (not ispresent(ExpectTable[i].l3_payload)
1265 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
1266 if (ispresent(l3)) {
1267 ExpectTable[i].l3_payload := valueof(l3);
1268 }
1269 if (ispresent(handoverRequestPointCode)) {
1270 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
1271 }
Harald Welte624f9632017-12-16 19:26:04 +01001272 ExpectTable[i].vc_conn := hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001273 if (ispresent(handoverRequestPointCode)) {
1274 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
1275 } else {
1276 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1277 }
Harald Welte624f9632017-12-16 19:26:04 +01001278 return;
1279 }
1280 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001281 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +01001282}
1283
Harald Welte6811d102019-04-14 22:23:14 +02001284private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1285runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001286 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1287 if (not ispresent(ImsiTable[i].imsi)) {
1288 ImsiTable[i].imsi := imsi;
1289 ImsiTable[i].tmsi := tmsi;
1290 ImsiTable[i].comp_ref := hdlr;
1291 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1292 return;
1293 }
1294 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001295 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +01001296}
1297
1298
Daniel Willmannd47106b2018-01-17 12:20:56 +01001299private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001300runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001301 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1302 ExpectTable[i].l3_payload := omit;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001303 ExpectTable[i].handoverRequestPointCode := omit;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001304 }
1305}
Harald Welte624f9632017-12-16 19:26:04 +01001306
Harald Welte17d21152018-01-27 00:47:11 +01001307/* helper function for clients to register their IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001308function f_ran_register_imsi(hexstring imsi, OCT4 tmsi)
1309runs on RAN_ConnHdlr {
1310 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1311 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001312 }
1313}
1314
Harald Welte475a2c12019-05-02 19:05:48 +02001315#ifdef RAN_EMULATION_RANAP
1316/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1317altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1318 var RANAP_PDU ranap;
1319 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1320 BSSAP.send(ts_RANAP_IuReleaseComplete);
1321 alt {
1322 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1323 setverdict(pass);
1324 }
1325 [] BSSAP.receive {
1326 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1327 mtc.stop;
1328 }
1329 }
1330 }
1331 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1332 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1333 mtc.stop;
1334 }
1335}
1336#endif
1337
Harald Welte17d21152018-01-27 00:47:11 +01001338
Harald Welte365f4ed2017-11-23 00:00:43 +01001339}