blob: 41aec4781333c535033d549af8842e7ecafbcb76 [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 {
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200468 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(g_ran_ops.use_osmux))) -> value ud_ind {
Harald Weltea4ca4462018-02-09 00:17:14 +0100469 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200470 ts_BSSMAP_ResetAck(g_ran_ops.use_osmux)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100471 }
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
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200486 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0, g_ran_ops.use_osmux)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100487 T.start;
488 alt {
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200489 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck(g_ran_ops.use_osmux))) {
Harald Weltea4ca4462018-02-09 00:17:14 +0100490 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,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200653 boolean use_osmux,
Harald Welte2fce7882019-04-15 11:48:05 +0200654 /* needed for performing BSSMAP RESET */
655 SCCP_PAR_Address sccp_addr_local optional,
656 SCCP_PAR_Address sccp_addr_peer optional
657}
658
659template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
660
661private function f_L3_is_rr(template octetstring l3) return boolean {
662 if (not isvalue(l3)) {
663 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100664 }
Harald Welte2fce7882019-04-15 11:48:05 +0200665 var octetstring l3v := valueof(l3);
666 if (lengthof(l3v) < 1) {
667 return false;
668 }
669 /* lower 4 bits of first octet are protocol discriminator */
670 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
671 return true;
672 }
673 return false;
674}
Harald Weltea4ca4462018-02-09 00:17:14 +0100675
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200676function 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 +0200677 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200678 if (n_sd_idx == 0) {
679 seq_nr := n_sd[0];
680 n_sd[0] := (n_sd[0] + 1) mod 4;
681 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
682 seq_nr := n_sd[n_sd_idx];
683 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200684 } else {
685 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200686 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200687 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200688 return seq_nr;
689}
690
691/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
692function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200693 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
694 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
695 log("patched enc_l3: ", enc_l3);
696}
697
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200698function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
699 var uint2_t seq_nr;
700 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
701 return 0;
702 } else if (ischosen(dtap.msgs.gcc)) {
703 return 1;
704 } else if (ischosen(dtap.msgs.bcc)) {
705 return 2;
706 } else if (ischosen(dtap.msgs.loc)) {
707 return 3;
708 }
709 /* no sequence number to patch */
710 return -1;
711}
712
713/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
714function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
715 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
716 if (n_sd_idx < 0) {
717 return;
718 }
719 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
720 f_ML3_patch_seq_nr(seq_nr, enc_l3);
721}
722
Harald Welte2fce7882019-04-15 11:48:05 +0200723private altstep as_reset_ack() runs on RAN_Emulation_CT {
724#ifdef RAN_EMULATION_BSSAP
725 var BSSAP_N_UNITDATA_ind ud_ind;
726#endif
Harald Welte5b027622019-04-14 23:40:17 +0200727#ifdef RAN_EMULATION_RANAP
728 var RANAP_N_UNITDATA_ind rud_ind;
729#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200730#ifdef RAN_EMULATION_BSSAP
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200731 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(g_ran_ops.use_osmux))) -> value ud_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200732 log("Respoding to inbound RESET with RESET-ACK");
733 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200734 ts_BSSMAP_ResetAck(g_ran_ops.use_osmux)));
Harald Welte2fce7882019-04-15 11:48:05 +0200735 repeat;
736 }
737#endif
Harald Welte5b027622019-04-14 23:40:17 +0200738#ifdef RAN_EMULATION_RANAP
739 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
740 log("Respoding to inbound IuRESET with IuRESET-ACK");
741 var CN_DomainIndicator dom;
742 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
743 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
744 ts_RANAP_ResetAck(dom)));
745 }
746#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200747}
748
749
750private altstep as_main_bssap() runs on RAN_Emulation_CT {
751#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100752 var BSSAP_N_UNITDATA_ind ud_ind;
753 var BSSAP_N_CONNECT_ind conn_ind;
754 var BSSAP_N_CONNECT_cfm conn_cfm;
755 var BSSAP_N_DATA_ind data_ind;
756 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100757 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100758 var PDU_BSSAP bssap;
Harald Welte2fce7882019-04-15 11:48:05 +0200759 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200760 var integer targetPointCode;
761 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100762
Harald Welte365f4ed2017-11-23 00:00:43 +0100763 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100764 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100765 /* Connectionless Procedures like RESET */
766 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100767 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100768 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100769 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
770 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100771 }
772 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100773 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100774 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200775 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100776 /* store mapping between client components and SCCP connectionId */
777 f_conn_table_add(vc_conn, conn_ind.connectionId);
778 /* handle user payload */
779 f_handle_userData(vc_conn, conn_ind.userData);
780 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100781 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100782 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100783 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100784 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100785 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100786 if (ispresent(data_ind.userData)) {
787 f_handle_userData(vc_conn, data_ind.userData);
788 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100789 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100790 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100791 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100792 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100793 if (ispresent(disc_ind.userData)) {
794 f_handle_userData(vc_conn, disc_ind.userData);
795 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100796 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200797 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100798 CLIENT.send(prim) to vc_conn;
799 f_conn_table_del(disc_ind.connectionId);
800 /* TOOD: return confirm to other side? */
801 }
Harald Welteb3414b22017-11-23 18:22:10 +0100802 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100803 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100804 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200805 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100806 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100807 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100808 if (ispresent(conn_cfm.userData)) {
809 f_handle_userData(vc_conn, conn_cfm.userData);
810 }
Harald Welteb3414b22017-11-23 18:22:10 +0100811 }
Harald Welte2fce7882019-04-15 11:48:05 +0200812 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
813 var integer conn_id := f_conn_id_by_comp(vc_conn);
814 /* send it to dispatcher */
815 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
816 }
Harald Welteb3414b22017-11-23 18:22:10 +0100817
Harald Welte365f4ed2017-11-23 00:00:43 +0100818 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200819 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100820 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100821 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100822 f_conn_table_del(conn_id);
823 }
824
825 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100826 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
827 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100828 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100829
830 if (f_comp_known(vc_conn) == false) {
831 /* unknown client, create new connection */
832 conn_id := f_gen_conn_id();
833
834 /* store mapping between client components and SCCP connectionId */
835 f_conn_table_add(vc_conn, conn_id);
836
Harald Welte004f5fb2017-12-16 17:54:40 +0100837 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
838 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100839 } else {
840 /* known client, send via existing connection */
841 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100842 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100843 }
844
Harald Welte49518bf2018-02-10 11:39:19 +0100845 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
846 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200847 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100848 /* we have just sent the first MM message, increment the counter */
849 var integer idx := f_idx_by_comp(vc_conn);
850 ConnectionTable[idx].n_sd[0] := 1;
851 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
852 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200853 }
Harald Welte9dadc522018-02-06 13:56:04 +0100854
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200855 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
856 var integer idx := f_idx_by_comp(vc_conn);
857 last_n_sd := ConnectionTable[idx].n_sd;
858 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
859 }
860
861 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
862 var integer idx := f_idx_by_comp(vc_conn);
863 ConnectionTable[idx].n_sd := last_n_sd;
864 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100865 }
Harald Welte2fce7882019-04-15 11:48:05 +0200866#else
Harald Welte5b027622019-04-14 23:40:17 +0200867 [false] CLIENT.receive {}
868#endif
869}
870
871private altstep as_main_ranap() runs on RAN_Emulation_CT {
872#ifdef RAN_EMULATION_RANAP
873 var RANAP_N_UNITDATA_ind rud_ind;
874 var RANAP_N_CONNECT_ind rconn_ind;
875 var RANAP_N_CONNECT_cfm rconn_cfm;
876 var RANAP_N_DATA_ind rdata_ind;
877 var RANAP_N_DISCONNECT_ind rdisc_ind;
878 var RANAP_Conn_Req creq;
879 var RANAP_PDU ranap;
880 var RAN_ConnHdlr vc_conn;
881
882 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
883 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
884 /* Connectionless Procedures like RESET */
885 var template RANAP_PDU resp;
886 resp := CommonRanapUnitdataCallback(rud_ind.userData);
887 if (isvalue(resp)) {
888 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
889 rud_ind.calledAddress, resp));
890 }
891 }
892 /* SCCP -> Client: new connection from BSC */
893 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
894 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
895 /* store mapping between client components and SCCP connectionId */
896 f_conn_table_add(vc_conn, rconn_ind.connectionId);
897 /* handle user payload */
898 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
899 /* confirm connection establishment */
900 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
901 }
902 /* SCCP -> Client: connection-oriented data in existing connection */
903 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
904 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
905 if (ispresent(rdata_ind.userData)) {
906 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
907 }
908 }
909 /* SCCP -> Client: disconnect of an existing connection */
910 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
911 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
912 if (ispresent(rdisc_ind.userData)) {
913 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
914 }
915 /* notify client about termination */
916 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
917 CLIENT.send(prim) to vc_conn;
918 f_conn_table_del(rdisc_ind.connectionId);
919 /* TOOD: return confirm to other side? */
920 }
921 /* SCCP -> Client: connection confirm for outbound connection */
922 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
923 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
924 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
925 CLIENT.send(prim) to vc_conn;
926 /* handle user payload */
927 if (ispresent(rconn_cfm.userData)) {
928 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
929 }
930 }
931
932 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
933 var integer conn_id := f_conn_id_by_comp(vc_conn);
934 /* send it to dispatcher */
935 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
936 }
937
938 /* Disconnect request client -> SCCP */
939 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
940 var integer conn_id := f_conn_id_by_comp(vc_conn);
941 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
942 f_conn_table_del(conn_id);
943 }
944
945 /* BSSAP from client -> SCCP */
946 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
947 var integer conn_id;
948 /* send to dispatcher */
949
950 if (f_comp_known(vc_conn) == false) {
951 /* unknown client, create new connection */
952 conn_id := f_gen_conn_id();
953
954 /* store mapping between client components and SCCP connectionId */
955 f_conn_table_add(vc_conn, conn_id);
956
957 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
958 creq.ranap));
959 } else {
960 /* known client, send via existing connection */
961 conn_id := f_conn_id_by_comp(vc_conn);
962 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
963 }
964
965 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
966 * counter only on MM/CC/SS, but not on RR */
967 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
968 /* we have just sent the first MM message, increment the counter */
969 var integer idx := f_idx_by_comp(vc_conn);
970 ConnectionTable[idx].n_sd[0] := 1;
971 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
972 }
973 }
974
975#else
976 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +0200977#endif
978}
Harald Welteb3414b22017-11-23 18:22:10 +0100979
Harald Welte2fce7882019-04-15 11:48:05 +0200980private altstep as_main_mgcp() runs on RAN_Emulation_CT {
981#ifdef RAN_EMULATION_MGCP
982 var MgcpCommand mgcp_req;
983 var MgcpResponse mgcp_resp;
984 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +0100985
Harald Weltec82eef42017-11-24 20:40:12 +0100986 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
987 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
988 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
989 * is printed as hex string, e.g. a@mgw for CIC 10 */
990
991 /* CLIENT -> MGCP */
992 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
993 /* MGCP request from Handler (we're MSC) */
994 /* store the transaction ID we've seen */
995 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
996 /* simply forward any MGCP from the client to the port */
997 MGCP.send(mgcp_req);
998 }
999 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
1000 /* MGCP response from Handler (we're BSC/MGW) */
1001 /* simply forward any MGCP from the client to the port */
1002 MGCP.send(mgcp_resp);
1003 }
1004
1005 /* MGCP -> CLIENT */
1006 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1007 /* MGCP request from network side (we're BSC/MGW) */
1008 /* Extract CIC from local part of endpoint name */
1009 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001010 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1011 /* ignore RSIP for unknown CIC */
1012 } else {
1013 /* Resolve the vc_conn by the CIC */
1014 vc_conn := f_comp_by_cic(cic);
1015 CLIENT.send(mgcp_req) to vc_conn;
1016 }
Harald Weltec82eef42017-11-24 20:40:12 +01001017 }
1018 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1019 /* MGCP response from network side (we're MSC) */
1020 /* Resolve the vc_conn by the transaction ID */
1021 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1022 CLIENT.send(mgcp_resp) to vc_conn;
1023 }
Harald Welte2fce7882019-04-15 11:48:05 +02001024#else
1025 [false] CLIENT.receive {}
1026#endif
1027}
Harald Weltec82eef42017-11-24 20:40:12 +01001028
Harald Welte2fce7882019-04-15 11:48:05 +02001029/* send a raw (encoded) L3 message over given SCCP connection */
1030private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1031{
1032 select (g_ran_ops.protocol) {
1033#ifdef RAN_EMULATION_BSSAP
1034 case (RAN_PROTOCOL_BSSAP) {
1035 var PDU_BSSAP bssap;
1036 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1037 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1038 }
1039#endif
Harald Welte5b027622019-04-14 23:40:17 +02001040#ifdef RAN_EMULATION_RANAP
1041 case (RAN_PROTOCOL_RANAP) {
1042 var RANAP_PDU ranap;
1043 if (false /* SAPI */) {
1044 var RANAP_IEs.SAPI sapi := sapi_0;
1045 ranap := valueof(ts_RANAP_DirectTransferSAPI(l3_enc, sapi));
1046 } else {
1047 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc));
1048 }
1049 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1050 }
1051#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001052 }
1053}
1054
1055function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1056
1057 g_ran_id := id;
1058 g_ran_ops := ops;
1059 f_conn_table_init();
1060 f_expect_table_init();
1061
1062 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1063 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001064 select (g_ran_ops.protocol) {
1065#ifdef RAN_EMULATION_BSSAP
1066 case (RAN_PROTOCOL_BSSAP) {
1067 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1068 }
1069#endif
1070#ifdef RAN_EMULATION_RANAP
1071 case (RAN_PROTOCOL_RANAP) {
1072 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1073 }
1074#endif
1075 }
Harald Welte2fce7882019-04-15 11:48:05 +02001076 }
1077
1078 while (true) {
1079 var RAN_ConnHdlr vc_conn;
1080 var PDU_DTAP_MO dtap_mo;
1081 var PDU_DTAP_MT dtap_mt;
1082 var RAN_ConnHdlr vc_hdlr;
1083 var octetstring l3_info;
1084 var hexstring imsi;
1085 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001086 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001087
1088 alt {
1089 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001090 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001091
1092 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1093 var integer idx := f_idx_by_comp(vc_conn);
1094 /* convert from decoded DTAP to encoded DTAP */
1095 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1096 /* patch correct L3 send sequence number N(SD) into l3_enc */
1097 if (dtap_mo.skip_seq_patching == false) {
1098 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1099 }
1100 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1101 }
1102
1103 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1104 var integer idx := f_idx_by_comp(vc_conn);
1105 /* convert from decoded DTAP to encoded DTAP */
1106 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1107 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1108 }
1109
1110 [] as_main_mgcp();
Harald Welte624f9632017-12-16 19:26:04 +01001111
Harald Welte6811d102019-04-14 22:23:14 +02001112 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001113 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001114 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001115 }
1116
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001117 [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
1118 f_create_expect(omit, vc_hdlr, targetPointCode);
1119 PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
1120 }
1121
Harald Welte6811d102019-04-14 22:23:14 +02001122 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001123 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001124 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001125 }
1126
1127
Harald Welte365f4ed2017-11-23 00:00:43 +01001128 }
1129 }
1130}
1131
Harald Welte2fce7882019-04-15 11:48:05 +02001132#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001133private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001134 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001135 return hex2int(str2hex(local_part));
1136
1137}
Harald Welte2fce7882019-04-15 11:48:05 +02001138#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001139
Harald Welte624f9632017-12-16 19:26:04 +01001140/***********************************************************************
1141 * "Expect" Handling (mapping for expected incoming SCCP connections)
1142 ***********************************************************************/
1143
1144/* data about an expected future incoming connection */
1145type record ExpectData {
1146 /* L3 payload based on which we can match it */
1147 octetstring l3_payload optional,
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001148 integer handoverRequestPointCode optional,
Harald Welte624f9632017-12-16 19:26:04 +01001149 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001150 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001151}
1152
1153/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001154signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001155signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001156
Harald Welte17d21152018-01-27 00:47:11 +01001157/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001158signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001159
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001160/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1161signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1162
1163/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1164signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1165
Harald Welte6811d102019-04-14 22:23:14 +02001166type port RAN_PROC_PT procedure {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001167 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 +01001168} with { extension "internal" };
1169
Harald Welte5b027622019-04-14 23:40:17 +02001170#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001171/* CreateCallback that can be used as create_cb and will use the expectation table */
1172function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001173runs on RAN_Emulation_CT return RAN_ConnHdlr {
1174 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001175 var octetstring l3_info;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001176 var boolean handoverRequest := false;
1177 var integer handoverRequestPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001178 var integer i;
1179
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001180 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1181 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1182 log("ExpectedCreateCallback completeLayer3Information");
1183 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
1184 handoverRequest := true;
1185 handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1186 log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode);
1187 } else {
1188 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001189 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001190 return ret;
1191 }
Harald Welte624f9632017-12-16 19:26:04 +01001192
1193 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001194 if (handoverRequest) {
1195 log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode,
1196 " ==? ", handoverRequestPointCode);
1197 if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) {
1198 ret := ExpectTable[i].vc_conn;
1199 log("Found Expect[", i, "] for handoverRequest handled at ", ret);
1200 return ret;
1201 } else {
1202 continue;
1203 }
1204 }
Harald Welte624f9632017-12-16 19:26:04 +01001205 if (not ispresent(ExpectTable[i].l3_payload)) {
1206 continue;
1207 }
1208 if (l3_info == ExpectTable[i].l3_payload) {
1209 ret := ExpectTable[i].vc_conn;
1210 /* release this entry to be used again */
1211 ExpectTable[i].l3_payload := omit;
1212 ExpectTable[i].vc_conn := null;
1213 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1214 /* return the component reference */
1215 return ret;
1216 }
1217 }
1218 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001219 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001220 return ret;
1221}
Harald Welte5b027622019-04-14 23:40:17 +02001222#endif
1223
1224#ifdef RAN_EMULATION_RANAP
1225/* CreateCallback that can be used as create_cb and will use the expectation table */
1226function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1227runs on RAN_Emulation_CT return RAN_ConnHdlr {
1228 var RAN_ConnHdlr ret := null;
1229 var template (omit) octetstring l3_info;
1230 var integer i;
1231
1232 l3_info := f_ranap_extract_l3(conn_ind.userData);
1233 if (istemplatekind(l3_info, "omit")) {
1234 setverdict(fail, "N-CONNECT.ind without NAS payload");
1235 mtc.stop;
1236 return ret;
1237 }
1238
1239 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
1240 if (not ispresent(ExpectTable[i].l3_payload)) {
1241 continue;
1242 }
1243 if (valueof(l3_info) == ExpectTable[i].l3_payload) {
1244 ret := ExpectTable[i].vc_conn;
1245 /* release this entry to be used again */
1246 ExpectTable[i].l3_payload := omit;
1247 ExpectTable[i].vc_conn := null;
1248 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1249 /* return the component reference */
1250 return ret;
1251 }
1252 }
1253 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1254 mtc.stop;
1255 return ret;
1256}
1257#endif
Harald Welte624f9632017-12-16 19:26:04 +01001258
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001259private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
1260 template integer handoverRequestPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001261runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001262 var integer i;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001263 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001264 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001265 if (not ispresent(ExpectTable[i].l3_payload)
1266 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
1267 if (ispresent(l3)) {
1268 ExpectTable[i].l3_payload := valueof(l3);
1269 }
1270 if (ispresent(handoverRequestPointCode)) {
1271 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
1272 }
Harald Welte624f9632017-12-16 19:26:04 +01001273 ExpectTable[i].vc_conn := hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001274 if (ispresent(handoverRequestPointCode)) {
1275 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
1276 } else {
1277 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1278 }
Harald Welte624f9632017-12-16 19:26:04 +01001279 return;
1280 }
1281 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001282 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +01001283}
1284
Harald Welte6811d102019-04-14 22:23:14 +02001285private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1286runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001287 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1288 if (not ispresent(ImsiTable[i].imsi)) {
1289 ImsiTable[i].imsi := imsi;
1290 ImsiTable[i].tmsi := tmsi;
1291 ImsiTable[i].comp_ref := hdlr;
1292 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1293 return;
1294 }
1295 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001296 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +01001297}
1298
1299
Daniel Willmannd47106b2018-01-17 12:20:56 +01001300private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001301runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001302 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1303 ExpectTable[i].l3_payload := omit;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001304 ExpectTable[i].handoverRequestPointCode := omit;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001305 }
1306}
Harald Welte624f9632017-12-16 19:26:04 +01001307
Harald Welte17d21152018-01-27 00:47:11 +01001308/* helper function for clients to register their IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001309function f_ran_register_imsi(hexstring imsi, OCT4 tmsi)
1310runs on RAN_ConnHdlr {
1311 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1312 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001313 }
1314}
1315
Harald Welte475a2c12019-05-02 19:05:48 +02001316#ifdef RAN_EMULATION_RANAP
1317/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1318altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1319 var RANAP_PDU ranap;
1320 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1321 BSSAP.send(ts_RANAP_IuReleaseComplete);
1322 alt {
1323 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1324 setverdict(pass);
1325 }
1326 [] BSSAP.receive {
1327 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1328 mtc.stop;
1329 }
1330 }
1331 }
1332 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1333 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1334 mtc.stop;
1335 }
1336}
1337#endif
1338
Harald Welte17d21152018-01-27 00:47:11 +01001339
Harald Welte365f4ed2017-11-23 00:00:43 +01001340}