blob: 40929ee4a55c7ba6a2721bea23cbc2492e81c56a [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;
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +020039import from Misc_Helpers all;
Harald Welte2fce7882019-04-15 11:48:05 +020040
41#ifdef RAN_EMULATION_BSSAP
Harald Welte365f4ed2017-11-23 00:00:43 +010042import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010043import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010044import from BSSMAP_Templates all;
Harald Welte2fce7882019-04-15 11:48:05 +020045#endif
46
47#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +010048import from MGCP_Types all;
49import from MGCP_Templates all;
Harald Welte2fce7882019-04-15 11:48:05 +020050#endif
Harald Welte365f4ed2017-11-23 00:00:43 +010051
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +020052#ifdef RAN_EMULATION_CTRL
53import from Osmocom_CTRL_Types all;
54import from Osmocom_CTRL_Adapter all;
55#endif
56
Harald Welte5b027622019-04-14 23:40:17 +020057#ifdef RAN_EMULATION_RANAP
58import from RANAP_CodecPort all;
59import from RANAP_PDU_Descriptions all;
60import from RANAP_Constants all;
61import from RANAP_IEs all;
62import from RANAP_Templates all;
63#endif
64
Harald Welte365f4ed2017-11-23 00:00:43 +010065/* General "base class" component definition, of which specific implementations
66 * derive themselves by means of the "extends" feature */
Harald Welte6811d102019-04-14 22:23:14 +020067type component RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +010068 /* port towards MSC Emulator core / SCCP connection dispatchar */
Harald Welte6811d102019-04-14 22:23:14 +020069 port RAN_Conn_PT BSSAP;
Harald Welteca519982018-01-21 19:28:26 +010070 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +020071 port RAN_PROC_PT BSSAP_PROC;
Harald Welte365f4ed2017-11-23 00:00:43 +010072}
73
74/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +020075type enumerated RAN_Conn_Prim {
Harald Welte365f4ed2017-11-23 00:00:43 +010076 /* SCCP tell us that connection was released */
77 MSC_CONN_PRIM_DISC_IND,
78 /* we tell SCCP to release connection */
Harald Welte71b69332018-01-21 20:43:53 +010079 MSC_CONN_PRIM_DISC_REQ,
80 /* Connection confirmed indication */
81 MSC_CONN_PRIM_CONF_IND
Harald Welte365f4ed2017-11-23 00:00:43 +010082}
83
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +020084type enumerated RAN_Transport {
85 BSSAP_TRANSPORT_AoIP, /* 3GPP AoIP: SCCP over M3UA over SCTP */
86 BSSAP_TRANSPORT_SCCPlite_SERVER, /* SCCPlite: SCCP over IPA over TCP */
87 BSSAP_TRANSPORT_SCCPlite_CLIENT, /* SCCPlite: SCCP over IPA over TCP */
88 RANAP_TRANSPORT_IuCS /* 3GPP IuCS: SCCP over M3UA over SCTP */
89};
90
Harald Welte0b476062018-01-21 19:07:32 +010091/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
92type record PDU_DTAP_MO {
93 OCT1 dlci optional,
Daniel Willmann92f66272018-02-06 15:50:52 +010094 boolean skip_seq_patching optional,
Harald Welte0b476062018-01-21 19:07:32 +010095 PDU_ML3_MS_NW dtap
96}
97
98/* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */
99type record PDU_DTAP_MT {
100 OCT1 dlci optional,
101 PDU_ML3_NW_MS dtap
102}
103
Harald Welte717c7302019-04-23 20:31:55 +0200104type record PDU_DTAP_PS_MO {
105 OCT1 dlci optional,
106 boolean skip_seq_patching optional,
107 PDU_L3_MS_SGSN dtap
108}
109
110type record PDU_DTAP_PS_MT {
111 OCT1 dlci optional,
112 PDU_L3_SGSN_MS dtap
113}
114
Harald Welte0b476062018-01-21 19:07:32 +0100115template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := {
116 dlci := dlci,
117 dtap := dtap
118}
119
Daniel Willmann92f66272018-02-06 15:50:52 +0100120template 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 +0100121 dlci := dlci,
Daniel Willmann92f66272018-02-06 15:50:52 +0100122 skip_seq_patching := skip_seq_patching,
Harald Welte0b476062018-01-21 19:07:32 +0100123 dtap := dtap
124}
125
126template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := {
127 dlci := dlci,
128 dtap := dtap
129}
130
131template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := {
132 dlci := dlci,
Harald Welte930d0a72018-03-22 22:08:40 +0100133 skip_seq_patching := ?,
Harald Welte0b476062018-01-21 19:07:32 +0100134 dtap := dtap
135}
136
Harald Welte717c7302019-04-23 20:31:55 +0200137template (value) PDU_DTAP_PS_MT ts_PDU_DTAP_PS_MT(template (value) PDU_L3_SGSN_MS dtap, template (omit) OCT1 dlci := omit) := {
138 dlci := dlci,
139 dtap := dtap
140}
141
142template (value) PDU_DTAP_PS_MO ts_PDU_DTAP_PS_MO(template (value) PDU_L3_MS_SGSN dtap, template (value) OCT1 dlci := '00'O,
143 boolean skip_seq_patching := false) := {
144 dlci := dlci,
145 skip_seq_patching := skip_seq_patching,
146 dtap := dtap
147}
148
149template PDU_DTAP_PS_MT tr_PDU_DTAP_PS_MT(template PDU_L3_SGSN_MS dtap, template OCT1 dlci := *) := {
150 dlci := dlci,
151 dtap := dtap
152}
153
154template PDU_DTAP_PS_MO tr_PDU_DTAP_PS_MO(template PDU_L3_MS_SGSN dtap, template OCT1 dlci := *) := {
155 dlci := dlci,
156 skip_seq_patching := ?,
157 dtap := dtap
158}
159
Harald Welte365f4ed2017-11-23 00:00:43 +0100160/* port between individual per-connection components and this dispatcher */
Harald Welte6811d102019-04-14 22:23:14 +0200161type port RAN_Conn_PT message {
Harald Welte2fce7882019-04-15 11:48:05 +0200162 inout
163#ifdef RAN_EMULATION_BSSAP
164 PDU_BSSAP,
Neels Hofmeyra8264612020-06-08 20:40:22 +0200165 BSSAP_N_UNITDATA_req,
Harald Weltea4ca4462018-02-09 00:17:14 +0100166 /* Client requests us to create SCCP Connection */
167 BSSAP_Conn_Req,
Harald Welte2fce7882019-04-15 11:48:05 +0200168#endif
Harald Welte5b027622019-04-14 23:40:17 +0200169#ifdef RAN_EMULATION_RANAP
170 RANAP_PDU,
171 /* Client requests us to create SCCP Connection */
172 RANAP_Conn_Req,
173#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200174#ifdef RAN_EMULATION_MGCP
Harald Weltea4ca4462018-02-09 00:17:14 +0100175 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
Harald Welte2fce7882019-04-15 11:48:05 +0200176 MgcpCommand, MgcpResponse,
177#endif
178 /* direct DTAP messages from/to clients */
179 PDU_DTAP_MO, PDU_DTAP_MT,
Harald Welte717c7302019-04-23 20:31:55 +0200180 PDU_DTAP_PS_MO, PDU_DTAP_PS_MT,
Harald Welte2fce7882019-04-15 11:48:05 +0200181 /* misc indications / requests between SCCP and client */
182 RAN_Conn_Prim;
Harald Welte365f4ed2017-11-23 00:00:43 +0100183} with { extension "internal" };
184
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200185type uint2_t N_Sd_Array[4];
Harald Welte365f4ed2017-11-23 00:00:43 +0100186
187/* represents a single BSSAP connection over SCCP */
188type record ConnectionData {
189 /* reference to the instance of the per-connection component */
Harald Welte6811d102019-04-14 22:23:14 +0200190 RAN_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100191 integer sccp_conn_id,
Harald Welte2fce7882019-04-15 11:48:05 +0200192#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100193 /* most recent MGCP transaction ID (Used on MSC side) */
194 MgcpTransId mgcp_trans_id optional,
Harald Welte2fce7882019-04-15 11:48:05 +0200195#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100196 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100197 integer cic optional,
198 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200199 N_Sd_Array n_sd
Harald Welte365f4ed2017-11-23 00:00:43 +0100200}
201
Harald Welte17d21152018-01-27 00:47:11 +0100202type record ImsiMapping {
Harald Welte6811d102019-04-14 22:23:14 +0200203 RAN_ConnHdlr comp_ref,
Harald Welte17d21152018-01-27 00:47:11 +0100204 hexstring imsi optional,
205 OCT4 tmsi
206}
207
Harald Welte6811d102019-04-14 22:23:14 +0200208type component RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200209 /* SCCP ports on the bottom side, using ASP primitives */
210#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100211 port BSSAP_CODEC_PT BSSAP;
Harald Welte2fce7882019-04-15 11:48:05 +0200212#endif
Harald Welte5b027622019-04-14 23:40:17 +0200213#ifdef RAN_EMULATION_RANAP
214 port RANAP_CODEC_PT RANAP;
215#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100216 /* BSSAP port to the per-connection clients */
Harald Welte6811d102019-04-14 22:23:14 +0200217 port RAN_Conn_PT CLIENT;
Harald Welte2fce7882019-04-15 11:48:05 +0200218#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100219 /* MGCP port */
220 port IPA_MGCP_PT MGCP;
Harald Welte2fce7882019-04-15 11:48:05 +0200221#endif
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +0200222#ifdef RAN_EMULATION_CTRL
223 /* CTRL port */
224 port IPA_CTRL_PT CTRL;
225 port IPA_CTRL_PT CTRL_CLIENT;
226#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100227
228 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
229 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100230
Harald Welte624f9632017-12-16 19:26:04 +0100231 /* pending expected incoming connections */
232 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100233
234 /* tables for mapping inbound unitdata (like paging) */
235 var ImsiMapping ImsiTable[16];
236
Harald Welte624f9632017-12-16 19:26:04 +0100237 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200238 port RAN_PROC_PT PROC;
Harald Welte624f9632017-12-16 19:26:04 +0100239
Harald Welte6811d102019-04-14 22:23:14 +0200240 var charstring g_ran_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100241 var integer g_next_e1_ts := 1;
Harald Welte6811d102019-04-14 22:23:14 +0200242 var RanOps g_ran_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100243};
244
Harald Welteb3414b22017-11-23 18:22:10 +0100245private function f_conn_id_known(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200246runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100247 var integer i;
248 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
249 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
250 return true;
251 }
252 }
253 return false;
254}
255
Harald Welte6811d102019-04-14 22:23:14 +0200256private function f_comp_known(RAN_ConnHdlr client)
257runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100258 var integer i;
259 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
260 if (ConnectionTable[i].comp_ref == client) {
261 return true;
262 }
263 }
264 return false;
265}
Harald Welte365f4ed2017-11-23 00:00:43 +0100266
Harald Weltee98bb2e2017-11-29 12:09:48 +0100267private function f_cic_known(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200268runs on RAN_Emulation_CT return boolean {
Harald Weltee98bb2e2017-11-29 12:09:48 +0100269 var integer i;
270 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
271 if (ConnectionTable[i].cic == cic) {
272 return true;
273 }
274 }
275 return false;
276}
277
Harald Welte365f4ed2017-11-23 00:00:43 +0100278/* resolve component reference by connection ID */
279private function f_comp_by_conn_id(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200280runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +0100281 var integer i;
282 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
283 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
284 return ConnectionTable[i].comp_ref;
285 }
286 }
Harald Welte6811d102019-04-14 22:23:14 +0200287 setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200288 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100289}
290
Harald Welte2fce7882019-04-15 11:48:05 +0200291
292#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100293/* resolve component reference by CIC */
294private function f_comp_by_mgcp_tid(MgcpTransId tid)
Harald Welte6811d102019-04-14 22:23:14 +0200295runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100296 var integer i;
297 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
298 if (ConnectionTable[i].mgcp_trans_id == tid) {
299 return ConnectionTable[i].comp_ref;
300 }
301 }
Harald Welte6811d102019-04-14 22:23:14 +0200302 setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200303 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100304}
305
Harald Welte6811d102019-04-14 22:23:14 +0200306private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid)
307runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100308 var integer i;
309 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
310 if (ConnectionTable[i].comp_ref == client) {
311 ConnectionTable[i].mgcp_trans_id := tid;
312 return;
313 }
314 }
Harald Welte6811d102019-04-14 22:23:14 +0200315 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200316 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100317}
Harald Welte2fce7882019-04-15 11:48:05 +0200318#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100319
320private function f_comp_by_cic(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200321runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100322 var integer i;
323 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
324 if (ConnectionTable[i].cic == cic) {
325 return ConnectionTable[i].comp_ref;
326 }
327 }
Harald Welte6811d102019-04-14 22:23:14 +0200328 setverdict(fail, "RAN Connection table not found by CIC ", cic);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200329 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100330}
331
Harald Welte6811d102019-04-14 22:23:14 +0200332private function f_comp_store_cic(RAN_ConnHdlr client, integer cic)
333runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100334 var integer i;
335 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
336 if (ConnectionTable[i].comp_ref == client) {
337 ConnectionTable[i].cic := cic;
338 return;
339 }
340 }
Harald Welte6811d102019-04-14 22:23:14 +0200341 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200342 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100343}
344
Harald Welte365f4ed2017-11-23 00:00:43 +0100345/* resolve connection ID by component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200346private function f_conn_id_by_comp(RAN_ConnHdlr client)
347runs on RAN_Emulation_CT return integer {
Harald Welte365f4ed2017-11-23 00:00:43 +0100348 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
349 if (ConnectionTable[i].comp_ref == client) {
350 return ConnectionTable[i].sccp_conn_id;
351 }
352 }
Harald Welte6811d102019-04-14 22:23:14 +0200353 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200354 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100355}
356
Harald Welte9dadc522018-02-06 13:56:04 +0100357/* resolve ConnectionTable index component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200358private function f_idx_by_comp(RAN_ConnHdlr client)
359runs on RAN_Emulation_CT return integer {
Harald Welte9dadc522018-02-06 13:56:04 +0100360 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
361 if (ConnectionTable[i].comp_ref == client) {
362 return i;
363 }
364 }
Harald Welte6811d102019-04-14 22:23:14 +0200365 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200366 mtc.stop;
Harald Welte9dadc522018-02-06 13:56:04 +0100367}
368
Harald Welteb3414b22017-11-23 18:22:10 +0100369private function f_gen_conn_id()
Harald Welte6811d102019-04-14 22:23:14 +0200370runs on RAN_Emulation_CT return integer {
Harald Welteb3414b22017-11-23 18:22:10 +0100371 var integer conn_id;
372
373 do {
374 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
375 } while (f_conn_id_known(conn_id) == true);
376
377 return conn_id;
378}
379
Harald Welte365f4ed2017-11-23 00:00:43 +0100380private function f_conn_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200381runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100382 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
383 ConnectionTable[i].comp_ref := null;
384 ConnectionTable[i].sccp_conn_id := -1;
Harald Welte2fce7882019-04-15 11:48:05 +0200385#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100386 ConnectionTable[i].mgcp_trans_id := omit;
Harald Welte2fce7882019-04-15 11:48:05 +0200387#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100388 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100389 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100390 }
Harald Welte17d21152018-01-27 00:47:11 +0100391 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
392 ImsiTable[i].comp_ref := null;
393 ImsiTable[i].imsi := omit;
394 ImsiTable[i].tmsi := 'FFFFFFFF'O;
395 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100396}
397
Harald Welte6811d102019-04-14 22:23:14 +0200398private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id)
399runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100400 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
401 if (ConnectionTable[i].sccp_conn_id == -1) {
402 ConnectionTable[i].comp_ref := comp_ref;
403 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100404 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100405 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100406 return;
407 }
408 }
Oliver Smith07ebf962023-07-07 12:11:58 +0200409 setverdict(fail, "RAN Connection table full!");
410 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100411}
412
413private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200414runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100415 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
416 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100417 log("Deleted conn table entry ", i,
418 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100419 ConnectionTable[i].sccp_conn_id := -1;
420 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100421 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100422 }
423 }
Harald Welte6811d102019-04-14 22:23:14 +0200424 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200425 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100426}
427
Harald Welte17d21152018-01-27 00:47:11 +0100428private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200429runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100430 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
431 if (ImsiTable[i].imsi == imsi or
432 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
433 return ImsiTable[i].comp_ref;
434 }
435 }
436 return null;
437}
438
Harald Welte2fce7882019-04-15 11:48:05 +0200439#ifdef RAN_EMULATION_BSSAP
440type record BSSAP_Conn_Req {
441 SCCP_PAR_Address addr_peer,
442 SCCP_PAR_Address addr_own,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100443 PDU_BSSAP bssap optional
Harald Welte2fce7882019-04-15 11:48:05 +0200444}
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100445template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, template (omit) PDU_BSSAP bssap) := {
Harald Welte2fce7882019-04-15 11:48:05 +0200446 addr_peer := peer,
447 addr_own := own,
448 bssap := bssap
449};
450
Harald Welte365f4ed2017-11-23 00:00:43 +0100451/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200452private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
453runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100454 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100455
Harald Welte473676b2018-01-27 20:38:01 +0100456 if (ischosen(bssap.pdu.bssmap)) {
457 /* BSC Side: If this is an assignment command, store CIC */
458 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
459 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
460 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
461 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
Pau Espin Pedrol43021cb2019-06-18 17:32:15 +0200462 var integer cic := f_bssmap_ie_cic_2_int(cic_ie);
Harald Welte473676b2018-01-27 20:38:01 +0100463 f_comp_store_cic(client, cic);
464 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100465 }
466
Harald Welte6811d102019-04-14 22:23:14 +0200467 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
468 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100469 /* we are the MS, so any message to us must be MT */
470 var PDU_DTAP_MT mt := {
471 dlci := bssap.dlci,
472 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
473 };
474 CLIENT.send(mt) to client;
475 } else {
476 /* we are the Network, so any message to us must be MO */
477 var PDU_DTAP_MO mo := {
478 dlci := bssap.dlci,
479 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
480 };
481 CLIENT.send(mo) to client;
482 }
483 } else {
484 CLIENT.send(bssap) to client;
485 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100486}
487
488/* call-back type, to be provided by specific implementation; called when new SCCP connection
489 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100490type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200491runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100492
493type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200494runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100495
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200496private function append_osmux_ie()
497runs on RAN_Emulation_CT return boolean {
498 return g_ran_ops.use_osmux and (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
499}
500
Harald Welte17d21152018-01-27 00:47:11 +0100501/* handle common Unitdata such as Paging */
502private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200503runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100504 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200505 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100506 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
507 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200508 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100509 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
510 client);
511 CLIENT.send(bssap) to client;
512 return omit;
513 }
514 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
515 } else {
516 log("CommonBssmapUnitdataCallback: Not a paging message");
517 }
518 /* ELSE: handle in user callback */
Neels Hofmeyr79896c02023-06-23 03:45:40 +0200519 if (not ispresent(g_ran_ops.unitdata_cb)) {
520 return omit;
521 }
Harald Welte6811d102019-04-14 22:23:14 +0200522 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100523}
524
Harald Welte6811d102019-04-14 22:23:14 +0200525function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100526 timer T := 5.0;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200527 var boolean append_osmux_support := append_osmux_ie();
Eric Wild49888a62022-03-30 03:16:11 +0200528 var integer attempts := g_ran_ops.bssap_reset_retries;
Harald Weltea4ca4462018-02-09 00:17:14 +0100529
Eric Wild49888a62022-03-30 03:16:11 +0200530 while (attempts > 0) {
531 attempts := attempts - 1;
532
533 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0, append_osmux_support)));
534 T.start;
535 alt {
536 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck(append_osmux_support))) {
537 log("BSSMAP: Received RESET-ACK in response to RESET, we're ready to go!");
538 return;
539 }
540 [] as_reset_ack(append_osmux_support);
541 [] BSSAP.receive { repeat };
542 [] T.timeout {
543 continue;
544 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200545 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100546 }
Eric Wild49888a62022-03-30 03:16:11 +0200547
548 setverdict(fail, "BSSMAP: Timeout waiting for RESET-ACK after sending RESET");
549 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100550}
551
Harald Welte2fce7882019-04-15 11:48:05 +0200552private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
553 var template octetstring l3 := f_bssap_extract_l3(bssap);
554 return f_L3_is_rr(l3);
555}
556#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100557
Harald Welte5b027622019-04-14 23:40:17 +0200558#ifdef RAN_EMULATION_RANAP
559type record RANAP_Conn_Req {
560 SCCP_PAR_Address addr_peer,
561 SCCP_PAR_Address addr_own,
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +0200562 RANAP_PDU ranap optional
Harald Welte5b027622019-04-14 23:40:17 +0200563}
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +0200564template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own,
565 template (omit) RANAP_PDU ranap) := {
566 addr_peer := peer,
567 addr_own := own,
568 ranap := ranap
569};
570
571template RANAP_Conn_Req tr_RANAP_Conn_Req(template SCCP_PAR_Address peer := ?,
572 template SCCP_PAR_Address own := ?,
573 template (omit) RANAP_PDU ranap := omit) := {
Harald Welte5b027622019-04-14 23:40:17 +0200574 addr_peer := peer,
575 addr_own := own,
576 ranap := ranap
577};
578
579private function fake_dlci_from_sapi(template (omit) SAPI sapi) return template (omit) OCT1
580{
581 if (istemplatekind(sapi, "omit")) {
582 return omit;
583 } else if (valueof(sapi) == sapi_3) {
584 return '03'O;
585 }
586 return '00'O;
587}
588
589private function f_handle_userData_RANAP(RAN_ConnHdlr client, RANAP_PDU ranap)
590runs on RAN_Emulation_CT {
591 /* decode + send decoded RANAP to client */
592 var template (omit) octetstring l3 := f_ranap_extract_l3(ranap);
Harald Welte0af76152022-01-11 23:02:38 +0100593 if (istemplatekind(l3, "omit") or not g_ran_ops.decode_dtap) {
Harald Welte5b027622019-04-14 23:40:17 +0200594 CLIENT.send(ranap) to client;
595 } else {
596 var template (omit) SAPI sapi := f_ranap_extract_sapi(ranap);
597 var template (omit) OCT1 dlci := fake_dlci_from_sapi(sapi);
598 if (g_ran_ops.role_ms) {
599 /* we are the MS, so any message to us must be MT */
Harald Welte717c7302019-04-23 20:31:55 +0200600 if (g_ran_ops.ps_domain) {
601 var PDU_DTAP_PS_MT mt := {
602 dlci := omit,
603 dtap := dec_PDU_L3_SGSN_MS(valueof(l3))
604 };
605 if (isvalue(dlci)) {
606 mt.dlci := valueof(dlci);
607 }
608 CLIENT.send(mt) to client;
609 } else {
610 var PDU_DTAP_MT mt := {
611 dlci := omit,
612 dtap := dec_PDU_ML3_NW_MS(valueof(l3))
613 };
614 if (isvalue(dlci)) {
615 mt.dlci := valueof(dlci)
616 }
617 CLIENT.send(mt) to client;
Harald Welte5b027622019-04-14 23:40:17 +0200618 }
Harald Welte5b027622019-04-14 23:40:17 +0200619 } else {
620 /* we are the Network, so any message to us must be MO */
Harald Welte717c7302019-04-23 20:31:55 +0200621 if (g_ran_ops.ps_domain) {
622 var PDU_DTAP_PS_MO mo := {
623 dlci := omit,
624 dtap := dec_PDU_L3_MS_SGSN(valueof(l3))
625 };
626 if (isvalue(dlci)) {
627 mo.dlci := valueof(dlci);
628 }
629 CLIENT.send(mo) to client;
630 } else {
631 var PDU_DTAP_MO mo := {
632 dlci := omit,
633 dtap := dec_PDU_ML3_MS_NW(valueof(l3))
634 };
635 if (isvalue(dlci)) {
636 mo.dlci := valueof(dlci)
637 }
638 CLIENT.send(mo) to client;
Harald Welte5b027622019-04-14 23:40:17 +0200639 }
Harald Welte5b027622019-04-14 23:40:17 +0200640 }
641 }
642}
643
644/* call-back type, to be provided by specific implementation; called when new SCCP connection
645 * arrives */
646type function RanapCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
647runs on RAN_Emulation_CT return RAN_ConnHdlr;
648
649type function RanapUnitdataCallback(RANAP_PDU ranap)
650runs on RAN_Emulation_CT return template RANAP_PDU;
651
652private function CommonRanapUnitdataCallback(RANAP_PDU ranap)
653runs on RAN_Emulation_CT return template RANAP_PDU {
654 if (match(ranap, tr_RANAP_Paging(?, ?))) {
655 var RAN_ConnHdlr client := null;
656 /* extract IMSI and (if present) TMSI */
657 var IMSI imsi := ranap.initiatingMessage.value_.paging.protocolIEs[1].value_.permanentNAS_UE_ID.iMSI;
658 var template OCT4 tmsi := omit;
659 if (lengthof(ranap.initiatingMessage.value_.paging.protocolIEs) > 2 and
660 ranap.initiatingMessage.value_.paging.protocolIEs[2].id == id_TemporaryUE_ID) {
661 var TemporaryUE_ID ue_id;
662 ue_id := ranap.initiatingMessage.value_.paging.protocolIEs[2].value_.temporaryUE_ID;
663 if (ischosen(ue_id.tMSI)) {
664 tmsi := ue_id.tMSI;
665 } else {
666 tmsi := ue_id.p_TMSI;
667 }
668 }
669 client := f_imsi_table_find(oct2hex(imsi), tmsi);
Harald Welte03d4e2b2019-05-10 00:42:33 +0200670 if (client != null) {
Harald Welte5b027622019-04-14 23:40:17 +0200671 log("CommonRanapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
672 client);
673 CLIENT.send(ranap) to client;
674 return omit;
675 }
676 log("CommonRanapUnitdataCallback: IMSI/TMSI not found in table");
677 } else {
678 log("CommonRanapUnitdataCallback: Not a paging message");
679 }
680
681 /* ELSE: handle in user callback */
Neels Hofmeyr79896c02023-06-23 03:45:40 +0200682 if (not ispresent(g_ran_ops.ranap_unitdata_cb)) {
683 return omit;
684 }
Harald Welte5b027622019-04-14 23:40:17 +0200685 return g_ran_ops.ranap_unitdata_cb.apply(ranap);
686}
687
688private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean {
689 var template (omit) SAPI sapi;
690 var template octetstring l3 := f_ranap_extract_l3(ranap);
691 return f_L3_is_rr(l3);
692}
693
694function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
695 timer T := 5.0;
696 var CN_DomainIndicator dom;
697 if (g_ran_ops.ps_domain) {
698 dom := ps_domain;
699 } else {
700 dom := cs_domain;
701 }
702
703 RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom)));
704 T.start;
705 alt {
706 [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200707 log("RANAP: Received RESET-ACK in response to RESET, we're ready to go!");
Harald Welte5b027622019-04-14 23:40:17 +0200708 }
709 [] as_reset_ack();
710 [] RANAP.receive { repeat };
711 [] T.timeout {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200712 setverdict(fail, "RANAP: Timeout waiting for RESET-ACK after sending RESET");
Harald Welte5b027622019-04-14 23:40:17 +0200713 mtc.stop;
714 }
715 }
716}
717#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100718
Harald Welte2fce7882019-04-15 11:48:05 +0200719
720type enumerated RanProtocol {
Harald Welte5b027622019-04-14 23:40:17 +0200721 RAN_PROTOCOL_BSSAP,
722 RAN_PROTOCOL_RANAP
Harald Welte2fce7882019-04-15 11:48:05 +0200723}
724
725type record RanOps {
726#ifdef RAN_EMULATION_BSSAP
727 BssmapCreateCallback create_cb optional,
728 BssmapUnitdataCallback unitdata_cb optional,
729#endif
Harald Welte5b027622019-04-14 23:40:17 +0200730#ifdef RAN_EMULATION_RANAP
731 RanapCreateCallback ranap_create_cb optional,
732 RanapUnitdataCallback ranap_unitdata_cb optional,
733 boolean ps_domain,
734#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200735 boolean decode_dtap,
736 boolean role_ms,
737 RanProtocol protocol,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200738 RAN_Transport transport,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200739 boolean use_osmux,
Eric Wild49888a62022-03-30 03:16:11 +0200740 integer bssap_reset_retries,
Harald Welte2fce7882019-04-15 11:48:05 +0200741 /* needed for performing BSSMAP RESET */
742 SCCP_PAR_Address sccp_addr_local optional,
743 SCCP_PAR_Address sccp_addr_peer optional
744}
745
746template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
747
Harald Weltea803b722020-08-21 15:42:26 +0200748function f_L3_is_rr(template octetstring l3) return boolean {
Harald Welte2fce7882019-04-15 11:48:05 +0200749 if (not isvalue(l3)) {
750 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100751 }
Harald Welte2fce7882019-04-15 11:48:05 +0200752 var octetstring l3v := valueof(l3);
753 if (lengthof(l3v) < 1) {
754 return false;
755 }
756 /* lower 4 bits of first octet are protocol discriminator */
757 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
758 return true;
759 }
760 return false;
761}
Harald Weltea4ca4462018-02-09 00:17:14 +0100762
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200763function 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 +0200764 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200765 if (n_sd_idx == 0) {
766 seq_nr := n_sd[0];
767 n_sd[0] := (n_sd[0] + 1) mod 4;
768 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
769 seq_nr := n_sd[n_sd_idx];
770 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200771 } else {
772 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200773 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200774 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200775 return seq_nr;
776}
777
778/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
779function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200780 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
781 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
782 log("patched enc_l3: ", enc_l3);
783}
784
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200785function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
786 var uint2_t seq_nr;
787 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
788 return 0;
789 } else if (ischosen(dtap.msgs.gcc)) {
790 return 1;
791 } else if (ischosen(dtap.msgs.bcc)) {
792 return 2;
793 } else if (ischosen(dtap.msgs.loc)) {
794 return 3;
795 }
796 /* no sequence number to patch */
797 return -1;
798}
799
800/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
Harald Weltea803b722020-08-21 15:42:26 +0200801private function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200802 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
803 if (n_sd_idx < 0) {
804 return;
805 }
806 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
807 f_ML3_patch_seq_nr(seq_nr, enc_l3);
808}
809
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200810private altstep as_reset_ack(boolean append_osmux_support := false) runs on RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200811#ifdef RAN_EMULATION_BSSAP
812 var BSSAP_N_UNITDATA_ind ud_ind;
813#endif
Harald Welte5b027622019-04-14 23:40:17 +0200814#ifdef RAN_EMULATION_RANAP
815 var RANAP_N_UNITDATA_ind rud_ind;
816#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200817#ifdef RAN_EMULATION_BSSAP
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200818 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(append_osmux_support))) -> value ud_ind {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200819 log("BSSMAP: Responding to inbound RESET with RESET-ACK");
Harald Welte2fce7882019-04-15 11:48:05 +0200820 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200821 ts_BSSMAP_ResetAck(append_osmux_support)));
Harald Welte2fce7882019-04-15 11:48:05 +0200822 repeat;
823 }
824#endif
Harald Welte5b027622019-04-14 23:40:17 +0200825#ifdef RAN_EMULATION_RANAP
826 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200827 log("RANAP: Responding to inbound IuRESET with IuRESET-ACK");
Harald Welte5b027622019-04-14 23:40:17 +0200828 var CN_DomainIndicator dom;
829 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
830 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
831 ts_RANAP_ResetAck(dom)));
832 }
833#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200834}
835
836
837private altstep as_main_bssap() runs on RAN_Emulation_CT {
838#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100839 var BSSAP_N_UNITDATA_ind ud_ind;
840 var BSSAP_N_CONNECT_ind conn_ind;
841 var BSSAP_N_CONNECT_cfm conn_cfm;
842 var BSSAP_N_DATA_ind data_ind;
843 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100844 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100845 var PDU_BSSAP bssap;
Neels Hofmeyra8264612020-06-08 20:40:22 +0200846 var BSSAP_N_UNITDATA_req bssap_ud;
Harald Welte2fce7882019-04-15 11:48:05 +0200847 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200848 var integer targetPointCode;
849 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100850
Harald Welte365f4ed2017-11-23 00:00:43 +0100851 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100852 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100853 /* Connectionless Procedures like RESET */
854 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100855 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100856 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100857 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
858 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100859 }
860 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100861 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100862 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200863 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100864 /* store mapping between client components and SCCP connectionId */
865 f_conn_table_add(vc_conn, conn_ind.connectionId);
866 /* handle user payload */
867 f_handle_userData(vc_conn, conn_ind.userData);
868 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100869 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100870 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100871 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100872 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100873 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100874 if (ispresent(data_ind.userData)) {
875 f_handle_userData(vc_conn, data_ind.userData);
876 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100877 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100878 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100879 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100880 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100881 if (ispresent(disc_ind.userData)) {
882 f_handle_userData(vc_conn, disc_ind.userData);
883 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100884 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200885 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100886 CLIENT.send(prim) to vc_conn;
887 f_conn_table_del(disc_ind.connectionId);
888 /* TOOD: return confirm to other side? */
889 }
Harald Welteb3414b22017-11-23 18:22:10 +0100890 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100891 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100892 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200893 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100894 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100895 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100896 if (ispresent(conn_cfm.userData)) {
897 f_handle_userData(vc_conn, conn_cfm.userData);
898 }
Harald Welteb3414b22017-11-23 18:22:10 +0100899 }
Harald Welte2fce7882019-04-15 11:48:05 +0200900 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
901 var integer conn_id := f_conn_id_by_comp(vc_conn);
902 /* send it to dispatcher */
903 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
904 }
Harald Welteb3414b22017-11-23 18:22:10 +0100905
Neels Hofmeyra8264612020-06-08 20:40:22 +0200906 [] CLIENT.receive(BSSAP_N_UNITDATA_req:?) -> value bssap_ud sender vc_conn {
907 BSSAP.send(bssap_ud);
908 }
909
Harald Welte365f4ed2017-11-23 00:00:43 +0100910 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200911 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100912 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100913 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100914 f_conn_table_del(conn_id);
915 }
916
917 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100918 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
919 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100920 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100921
922 if (f_comp_known(vc_conn) == false) {
923 /* unknown client, create new connection */
924 conn_id := f_gen_conn_id();
925
926 /* store mapping between client components and SCCP connectionId */
927 f_conn_table_add(vc_conn, conn_id);
928
Harald Welte004f5fb2017-12-16 17:54:40 +0100929 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
930 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100931 } else {
932 /* known client, send via existing connection */
933 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100934 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100935 }
936
Harald Welte49518bf2018-02-10 11:39:19 +0100937 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
938 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200939 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100940 /* we have just sent the first MM message, increment the counter */
941 var integer idx := f_idx_by_comp(vc_conn);
942 ConnectionTable[idx].n_sd[0] := 1;
943 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
944 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200945 }
Harald Welte9dadc522018-02-06 13:56:04 +0100946
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200947 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
948 var integer idx := f_idx_by_comp(vc_conn);
949 last_n_sd := ConnectionTable[idx].n_sd;
950 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
951 }
952
953 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
954 var integer idx := f_idx_by_comp(vc_conn);
955 ConnectionTable[idx].n_sd := last_n_sd;
956 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100957 }
Harald Welte2fce7882019-04-15 11:48:05 +0200958#else
Harald Welte5b027622019-04-14 23:40:17 +0200959 [false] CLIENT.receive {}
960#endif
961}
962
963private altstep as_main_ranap() runs on RAN_Emulation_CT {
964#ifdef RAN_EMULATION_RANAP
965 var RANAP_N_UNITDATA_ind rud_ind;
966 var RANAP_N_CONNECT_ind rconn_ind;
967 var RANAP_N_CONNECT_cfm rconn_cfm;
968 var RANAP_N_DATA_ind rdata_ind;
969 var RANAP_N_DISCONNECT_ind rdisc_ind;
970 var RANAP_Conn_Req creq;
971 var RANAP_PDU ranap;
972 var RAN_ConnHdlr vc_conn;
Harald Welte717c7302019-04-23 20:31:55 +0200973 var PDU_DTAP_PS_MO ps_mo;
974 var PDU_DTAP_PS_MT ps_mt;
Harald Welte5b027622019-04-14 23:40:17 +0200975
976 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
977 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
978 /* Connectionless Procedures like RESET */
979 var template RANAP_PDU resp;
980 resp := CommonRanapUnitdataCallback(rud_ind.userData);
981 if (isvalue(resp)) {
982 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
983 rud_ind.calledAddress, resp));
984 }
985 }
986 /* SCCP -> Client: new connection from BSC */
987 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
988 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
989 /* store mapping between client components and SCCP connectionId */
990 f_conn_table_add(vc_conn, rconn_ind.connectionId);
991 /* handle user payload */
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +0200992 if (ispresent(rconn_ind.userData)) {
993 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
994 } else {
995 /* Notify client that we received an SCCP CR without user data */
996 CLIENT.send(ts_RANAP_Conn_Req(rconn_ind.callingAddress, rconn_ind.calledAddress, omit));
997 }
Harald Welte5b027622019-04-14 23:40:17 +0200998 /* confirm connection establishment */
999 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
1000 }
1001 /* SCCP -> Client: connection-oriented data in existing connection */
1002 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
1003 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
1004 if (ispresent(rdata_ind.userData)) {
1005 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
1006 }
1007 }
1008 /* SCCP -> Client: disconnect of an existing connection */
1009 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
1010 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
1011 if (ispresent(rdisc_ind.userData)) {
1012 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
1013 }
1014 /* notify client about termination */
1015 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
1016 CLIENT.send(prim) to vc_conn;
1017 f_conn_table_del(rdisc_ind.connectionId);
1018 /* TOOD: return confirm to other side? */
1019 }
1020 /* SCCP -> Client: connection confirm for outbound connection */
1021 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
1022 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
1023 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
1024 CLIENT.send(prim) to vc_conn;
1025 /* handle user payload */
1026 if (ispresent(rconn_cfm.userData)) {
1027 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
1028 }
1029 }
1030
1031 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
1032 var integer conn_id := f_conn_id_by_comp(vc_conn);
1033 /* send it to dispatcher */
1034 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
1035 }
1036
1037 /* Disconnect request client -> SCCP */
1038 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
1039 var integer conn_id := f_conn_id_by_comp(vc_conn);
1040 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
1041 f_conn_table_del(conn_id);
1042 }
1043
1044 /* BSSAP from client -> SCCP */
1045 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
1046 var integer conn_id;
1047 /* send to dispatcher */
1048
1049 if (f_comp_known(vc_conn) == false) {
1050 /* unknown client, create new connection */
1051 conn_id := f_gen_conn_id();
1052
1053 /* store mapping between client components and SCCP connectionId */
1054 f_conn_table_add(vc_conn, conn_id);
1055
1056 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
1057 creq.ranap));
1058 } else {
1059 /* known client, send via existing connection */
1060 conn_id := f_conn_id_by_comp(vc_conn);
1061 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
1062 }
1063
1064 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
1065 * counter only on MM/CC/SS, but not on RR */
1066 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
1067 /* we have just sent the first MM message, increment the counter */
1068 var integer idx := f_idx_by_comp(vc_conn);
1069 ConnectionTable[idx].n_sd[0] := 1;
1070 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
1071 }
1072 }
1073
Harald Welte717c7302019-04-23 20:31:55 +02001074 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MO:?) -> value ps_mo sender vc_conn {
1075 var integer idx := f_idx_by_comp(vc_conn);
1076 /* convert from decoded DTAP to encoded DTAP */
1077 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(ps_mo.dtap);
1078 /* patch correct L3 send sequence number N(SD) into l3_enc */
1079 if (ps_mo.skip_seq_patching == false) {
1080 //f_ML3_patch_seq(ConnectionTable[idx], ps_mo.dtap, l3_enc);
1081 }
1082 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mo.dlci, l3_enc);
1083 }
1084
1085 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MT:?) -> value ps_mt sender vc_conn {
1086 var integer idx := f_idx_by_comp(vc_conn);
1087 /* convert from decoded DTAP to encoded DTAP */
1088 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(ps_mt.dtap);
1089 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mt.dlci, l3_enc);
1090 }
1091
1092
Harald Welte5b027622019-04-14 23:40:17 +02001093#else
1094 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +02001095#endif
1096}
Harald Welteb3414b22017-11-23 18:22:10 +01001097
Harald Welte2fce7882019-04-15 11:48:05 +02001098private altstep as_main_mgcp() runs on RAN_Emulation_CT {
1099#ifdef RAN_EMULATION_MGCP
1100 var MgcpCommand mgcp_req;
1101 var MgcpResponse mgcp_resp;
1102 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +01001103
Harald Weltec82eef42017-11-24 20:40:12 +01001104 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
1105 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
1106 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
1107 * is printed as hex string, e.g. a@mgw for CIC 10 */
1108
1109 /* CLIENT -> MGCP */
1110 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
1111 /* MGCP request from Handler (we're MSC) */
1112 /* store the transaction ID we've seen */
1113 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
1114 /* simply forward any MGCP from the client to the port */
1115 MGCP.send(mgcp_req);
1116 }
1117 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
1118 /* MGCP response from Handler (we're BSC/MGW) */
1119 /* simply forward any MGCP from the client to the port */
1120 MGCP.send(mgcp_resp);
1121 }
1122
1123 /* MGCP -> CLIENT */
1124 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1125 /* MGCP request from network side (we're BSC/MGW) */
1126 /* Extract CIC from local part of endpoint name */
1127 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001128 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1129 /* ignore RSIP for unknown CIC */
1130 } else {
1131 /* Resolve the vc_conn by the CIC */
1132 vc_conn := f_comp_by_cic(cic);
1133 CLIENT.send(mgcp_req) to vc_conn;
1134 }
Harald Weltec82eef42017-11-24 20:40:12 +01001135 }
1136 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1137 /* MGCP response from network side (we're MSC) */
1138 /* Resolve the vc_conn by the transaction ID */
1139 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1140 CLIENT.send(mgcp_resp) to vc_conn;
1141 }
Harald Welte2fce7882019-04-15 11:48:05 +02001142#else
1143 [false] CLIENT.receive {}
1144#endif
1145}
Harald Weltec82eef42017-11-24 20:40:12 +01001146
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001147private altstep as_main_ctrl() runs on RAN_Emulation_CT {
1148#ifdef RAN_EMULATION_CTRL
1149 var CtrlMessage ctrl;
1150 var RAN_ConnHdlr vc_conn;
1151 var ASP_IPA_Event evt;
1152
1153 /* Handling of CTRL in IPA SCCPLite case. This predates 3GPP AoIP
1154 * and uses a CTRL session in parallel to BSSAP. */
1155
1156 /* CTRL_CLIENT -> CTRL */
1157 [] CTRL_CLIENT.receive(CtrlMessage:?) -> value ctrl sender vc_conn {
1158 CTRL.send(ctrl);
1159 }
1160
1161 /* CTRL -> CTRL_CLIENT */
1162 [] CTRL.receive(CtrlMessage:?) -> value ctrl {
1163 CTRL_CLIENT.send(ctrl);
1164 }
1165
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001166 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_UP)) -> value evt {
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001167 CTRL_CLIENT.send(evt);
1168 }
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001169 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN)) {
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001170 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Lost IPA connection!");
1171 }
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001172 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_ID_ACK)) {}
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001173#else
1174 [false] CLIENT.receive {}
1175#endif
1176}
1177
Harald Welte2fce7882019-04-15 11:48:05 +02001178/* send a raw (encoded) L3 message over given SCCP connection */
1179private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1180{
1181 select (g_ran_ops.protocol) {
1182#ifdef RAN_EMULATION_BSSAP
1183 case (RAN_PROTOCOL_BSSAP) {
1184 var PDU_BSSAP bssap;
1185 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1186 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1187 }
1188#endif
Harald Welte5b027622019-04-14 23:40:17 +02001189#ifdef RAN_EMULATION_RANAP
1190 case (RAN_PROTOCOL_RANAP) {
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001191 var template (omit) RANAP_IEs.SAPI sapi := omit;
Harald Welte5b027622019-04-14 23:40:17 +02001192 var RANAP_PDU ranap;
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001193
1194 /* Perform DLCI -> SAPI transformation (x & 0x07) */
1195 if (dlci and4b '07'O == '03'O) {
1196 sapi := sapi_3;
1197 }
1198
1199 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc, sapi := sapi));
Harald Welte5b027622019-04-14 23:40:17 +02001200 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1201 }
1202#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001203 }
1204}
1205
1206function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1207
1208 g_ran_id := id;
1209 g_ran_ops := ops;
1210 f_conn_table_init();
1211 f_expect_table_init();
1212
1213 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1214 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001215 select (g_ran_ops.protocol) {
1216#ifdef RAN_EMULATION_BSSAP
1217 case (RAN_PROTOCOL_BSSAP) {
1218 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1219 }
1220#endif
1221#ifdef RAN_EMULATION_RANAP
1222 case (RAN_PROTOCOL_RANAP) {
1223 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1224 }
1225#endif
1226 }
Harald Welte2fce7882019-04-15 11:48:05 +02001227 }
1228
1229 while (true) {
1230 var RAN_ConnHdlr vc_conn;
1231 var PDU_DTAP_MO dtap_mo;
1232 var PDU_DTAP_MT dtap_mt;
1233 var RAN_ConnHdlr vc_hdlr;
1234 var octetstring l3_info;
1235 var hexstring imsi;
1236 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001237 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001238
1239 alt {
1240 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001241 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001242
1243 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1244 var integer idx := f_idx_by_comp(vc_conn);
1245 /* convert from decoded DTAP to encoded DTAP */
1246 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1247 /* patch correct L3 send sequence number N(SD) into l3_enc */
1248 if (dtap_mo.skip_seq_patching == false) {
1249 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1250 }
1251 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1252 }
1253
1254 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1255 var integer idx := f_idx_by_comp(vc_conn);
1256 /* convert from decoded DTAP to encoded DTAP */
1257 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1258 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1259 }
1260
1261 [] as_main_mgcp();
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001262 [] as_main_ctrl();
Harald Welte624f9632017-12-16 19:26:04 +01001263
Harald Welte6811d102019-04-14 22:23:14 +02001264 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001265 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001266 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001267 }
1268
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001269 [] PROC.getcall(RAN_register_n_connect:{?,?}) -> param(targetPointCode, vc_hdlr) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001270 f_create_expect(omit, vc_hdlr, targetPointCode);
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001271 PROC.reply(RAN_register_n_connect:{targetPointCode, vc_hdlr}) to vc_hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001272 }
1273
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001274 [] PROC.getcall(RAN_register_sccp_cr_without_payload:{?}) -> param(vc_hdlr) {
1275 f_create_expect(omit, vc_hdlr);
1276 PROC.reply(RAN_register_sccp_cr_without_payload:{vc_hdlr}) to vc_hdlr;
1277 }
1278
Harald Welte6811d102019-04-14 22:23:14 +02001279 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001280 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001281 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001282 }
Harald Welteee4d70c2022-05-16 21:29:19 +02001283 [] PROC.getcall(RAN_unregister_imsi:{?,?}) -> param(imsi, vc_hdlr) {
1284 f_destroy_imsi(imsi, vc_hdlr);
1285 PROC.reply(RAN_unregister_imsi:{imsi, vc_hdlr}) to vc_hdlr;
1286 }
Harald Welte17d21152018-01-27 00:47:11 +01001287
Harald Welte365f4ed2017-11-23 00:00:43 +01001288 }
1289 }
1290}
1291
Harald Welte2fce7882019-04-15 11:48:05 +02001292#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001293private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001294 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001295 return hex2int(str2hex(local_part));
1296
1297}
Harald Welte2fce7882019-04-15 11:48:05 +02001298#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001299
Harald Welte624f9632017-12-16 19:26:04 +01001300/***********************************************************************
1301 * "Expect" Handling (mapping for expected incoming SCCP connections)
1302 ***********************************************************************/
1303
1304/* data about an expected future incoming connection */
1305type record ExpectData {
1306 /* L3 payload based on which we can match it */
1307 octetstring l3_payload optional,
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001308 integer n_connectPointCode optional,
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001309 boolean sccp_cr_without_payload,
Harald Welte624f9632017-12-16 19:26:04 +01001310 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001311 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001312}
1313
1314/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001315signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001316signature RAN_register_n_connect(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001317signature RAN_register_sccp_cr_without_payload(in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001318
Harald Welte17d21152018-01-27 00:47:11 +01001319/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001320signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welteee4d70c2022-05-16 21:29:19 +02001321signature RAN_unregister_imsi(in hexstring imsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001322
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001323/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1324signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1325
1326/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1327signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1328
Harald Welte6811d102019-04-14 22:23:14 +02001329type port RAN_PROC_PT procedure {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001330 inout RAN_register, RAN_register_imsi, RAN_unregister_imsi, RAN_register_n_connect,
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001331 RAN_register_sccp_cr_without_payload, RAN_last_n_sd, RAN_continue_after_n_sd;
Harald Welte624f9632017-12-16 19:26:04 +01001332} with { extension "internal" };
1333
Harald Welte5b027622019-04-14 23:40:17 +02001334#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001335/* CreateCallback that can be used as create_cb and will use the expectation table */
1336function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001337runs on RAN_Emulation_CT return RAN_ConnHdlr {
1338 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001339 var octetstring l3_info;
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001340 var boolean n_connect := false;
1341 var integer n_connectPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001342 var integer i;
1343
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001344 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1345 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1346 log("ExpectedCreateCallback completeLayer3Information");
1347 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001348 n_connect := true;
1349 n_connectPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1350 log("ExpectedCreateCallback handoverRequest ", n_connectPointCode);
Andreas Eversbergd9a348c2023-07-27 16:08:23 +02001351 } else if (ischosen(conn_ind.userData.pdu.bssmap.vGCS_VBSSetup)) {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001352 n_connect := true;
1353 n_connectPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1354 log("ExpectedCreateCallback VGCS/VBS Setup ", n_connectPointCode);
Andreas Eversbergd9a348c2023-07-27 16:08:23 +02001355 } else if (ischosen(conn_ind.userData.pdu.bssmap.vGCS_VBSAssignmentRequest)) {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001356 n_connect := true;
1357 n_connectPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1358 log("ExpectedCreateCallback VGCS/VBS Assignment ", n_connectPointCode);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001359 } else {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001360 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a new BSS connection");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001361 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001362 }
Harald Welte624f9632017-12-16 19:26:04 +01001363
1364 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001365 if (n_connect) {
1366 log("ExpectTable[", i, "].n_connectPointCode = ", ExpectTable[i].n_connectPointCode,
1367 " ==? ", n_connectPointCode);
1368 if (ExpectTable[i].n_connectPointCode == n_connectPointCode) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001369 ret := ExpectTable[i].vc_conn;
Andreas Eversbergd9a348c2023-07-27 16:08:23 +02001370 /* release this entry to be used again */
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001371 ExpectTable[i].n_connectPointCode := omit;
Andreas Eversbergd9a348c2023-07-27 16:08:23 +02001372 ExpectTable[i].vc_conn := null;
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001373 log("Found Expect[", i, "] for N-CONNECT handled at ", ret);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001374 return ret;
1375 } else {
1376 continue;
1377 }
1378 }
Harald Welte624f9632017-12-16 19:26:04 +01001379 if (not ispresent(ExpectTable[i].l3_payload)) {
1380 continue;
1381 }
1382 if (l3_info == ExpectTable[i].l3_payload) {
1383 ret := ExpectTable[i].vc_conn;
1384 /* release this entry to be used again */
1385 ExpectTable[i].l3_payload := omit;
1386 ExpectTable[i].vc_conn := null;
1387 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1388 /* return the component reference */
1389 return ret;
1390 }
1391 }
1392 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001393 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001394}
Harald Welte5b027622019-04-14 23:40:17 +02001395#endif
1396
1397#ifdef RAN_EMULATION_RANAP
1398/* CreateCallback that can be used as create_cb and will use the expectation table */
1399function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1400runs on RAN_Emulation_CT return RAN_ConnHdlr {
1401 var RAN_ConnHdlr ret := null;
1402 var template (omit) octetstring l3_info;
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001403 var boolean rx_sccp_cr_without_payload;
Harald Welte5b027622019-04-14 23:40:17 +02001404 var integer i;
1405
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001406 if (ispresent(conn_ind.userData)) {
1407 l3_info := f_ranap_extract_l3(conn_ind.userData);
1408 rx_sccp_cr_without_payload := false;
1409 if (istemplatekind(l3_info, "omit") and not rx_sccp_cr_without_payload) {
1410 setverdict(fail, "N-CONNECT.ind without NAS payload");
1411 mtc.stop;
1412 return ret;
1413 }
1414 } else {
1415 l3_info := omit;
1416 rx_sccp_cr_without_payload := true;
Harald Welte5b027622019-04-14 23:40:17 +02001417 }
1418
1419 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001420 if ((rx_sccp_cr_without_payload and ExpectTable[i].sccp_cr_without_payload)
1421 or (ispresent(ExpectTable[i].l3_payload) and valueof(l3_info) == ExpectTable[i].l3_payload)) {
Harald Welte5b027622019-04-14 23:40:17 +02001422 ret := ExpectTable[i].vc_conn;
1423 /* release this entry to be used again */
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001424 ExpectTable[i].sccp_cr_without_payload := false;
Harald Welte5b027622019-04-14 23:40:17 +02001425 ExpectTable[i].l3_payload := omit;
1426 ExpectTable[i].vc_conn := null;
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001427 log("Found Expect[", i, "] for l3=", l3_info, " handled at ", ret);
Harald Welte5b027622019-04-14 23:40:17 +02001428 /* return the component reference */
1429 return ret;
1430 }
1431 }
1432 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1433 mtc.stop;
1434 return ret;
1435}
1436#endif
Harald Welte624f9632017-12-16 19:26:04 +01001437
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001438private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001439 template integer n_connectPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001440runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001441 var integer i;
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001442 log("f_create_expect(l3 := ", l3, ", n_connectPointCode := ", n_connectPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001443 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001444 if (not ispresent(ExpectTable[i].l3_payload)
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001445 and not ExpectTable[i].sccp_cr_without_payload
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001446 and not ispresent(ExpectTable[i].n_connectPointCode)) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001447 if (ispresent(l3)) {
1448 ExpectTable[i].l3_payload := valueof(l3);
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001449 } else {
1450 ExpectTable[i].sccp_cr_without_payload := true;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001451 }
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001452 if (ispresent(n_connectPointCode)) {
1453 ExpectTable[i].n_connectPointCode := valueof(n_connectPointCode);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001454 }
Harald Welte624f9632017-12-16 19:26:04 +01001455 ExpectTable[i].vc_conn := hdlr;
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001456 if (ispresent(n_connectPointCode)) {
1457 log("Created Expect[", i, "] for N-CONNECT to be handled at ", hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001458 } else {
1459 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1460 }
Harald Welte624f9632017-12-16 19:26:04 +01001461 return;
1462 }
1463 }
Oliver Smith07ebf962023-07-07 12:11:58 +02001464 setverdict(fail, "No space left in ExpectTable");
1465 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001466}
1467
Harald Welte6811d102019-04-14 22:23:14 +02001468private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1469runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001470 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1471 if (not ispresent(ImsiTable[i].imsi)) {
1472 ImsiTable[i].imsi := imsi;
1473 ImsiTable[i].tmsi := tmsi;
1474 ImsiTable[i].comp_ref := hdlr;
1475 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1476 return;
1477 }
1478 }
Oliver Smith07ebf962023-07-07 12:11:58 +02001479 setverdict(fail, "No space left in ImsiTable");
1480 mtc.stop;
Harald Welte17d21152018-01-27 00:47:11 +01001481}
1482
Harald Welteee4d70c2022-05-16 21:29:19 +02001483private function f_destroy_imsi(hexstring imsi, RAN_ConnHdlr hdlr)
1484runs on RAN_Emulation_CT {
1485 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1486 if (ImsiTable[i].imsi == imsi and ImsiTable[i].comp_ref == hdlr) {
1487 ImsiTable[i].comp_ref := null;
1488 ImsiTable[i].imsi := omit;
1489 ImsiTable[i].tmsi := 'FFFFFFFF'O;
1490 log("Removed IMSI[", i, "] for ", imsi, " to be handled at ", hdlr);
1491 return;
1492 }
1493 }
Oliver Smith07ebf962023-07-07 12:11:58 +02001494 setverdict(fail, "Unable to find to-be-destroyed IMSI in ImsiTable");
1495 mtc.stop;
Harald Welteee4d70c2022-05-16 21:29:19 +02001496}
Harald Welte17d21152018-01-27 00:47:11 +01001497
Daniel Willmannd47106b2018-01-17 12:20:56 +01001498private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001499runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001500 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1501 ExpectTable[i].l3_payload := omit;
Andreas Eversberge5a6ef12023-07-28 10:45:20 +02001502 ExpectTable[i].n_connectPointCode := omit;
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001503 ExpectTable[i].sccp_cr_without_payload := false;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001504 }
1505}
Harald Welte624f9632017-12-16 19:26:04 +01001506
Harald Welte17d21152018-01-27 00:47:11 +01001507/* helper function for clients to register their IMSI/TMSI */
Vadim Yanitskiyae747742020-01-10 00:23:10 +01001508function f_ran_register_imsi(hexstring imsi, template (omit) OCT4 tmsi_or_omit)
Harald Welte6811d102019-04-14 22:23:14 +02001509runs on RAN_ConnHdlr {
Vadim Yanitskiyae747742020-01-10 00:23:10 +01001510 var OCT4 tmsi;
1511
1512 /* Resolve omit to a special reserved value */
1513 if (istemplatekind(tmsi_or_omit, "omit")) {
1514 tmsi := 'FFFFFFFF'O;
1515 } else {
1516 tmsi := valueof(tmsi_or_omit);
1517 }
1518
Harald Welte6811d102019-04-14 22:23:14 +02001519 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1520 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001521 }
1522}
1523
Harald Welteee4d70c2022-05-16 21:29:19 +02001524/* helper function for clients to register their IMSI/TMSI */
1525function f_ran_unregister_imsi(hexstring imsi)
1526runs on RAN_ConnHdlr {
1527
1528 BSSAP_PROC.call(RAN_unregister_imsi:{imsi, self}) {
1529 [] BSSAP_PROC.getreply(RAN_unregister_imsi:{?,?}) {};
1530 }
1531}
1532
Harald Welte1e3bbbe2022-01-11 23:04:33 +01001533/* register an expect with the BSSMAP core */
1534function f_ran_register_exp(octetstring l3_enc) runs on RAN_ConnHdlr {
1535 BSSAP_PROC.call(RAN_register:{l3_enc, self}) {
1536 [] BSSAP_PROC.getreply(RAN_register:{?, ?}) {};
1537 }
1538}
1539
Neels Hofmeyrf0b9ed12022-06-07 17:46:32 +02001540function f_ran_register_sccp_cr_without_payload() runs on RAN_ConnHdlr {
1541 BSSAP_PROC.call(RAN_register_sccp_cr_without_payload:{self}) {
1542 [] BSSAP_PROC.getreply(RAN_register_sccp_cr_without_payload:{?}) {};
1543 }
1544}
1545
Harald Welte475a2c12019-05-02 19:05:48 +02001546#ifdef RAN_EMULATION_RANAP
1547/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1548altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1549 var RANAP_PDU ranap;
1550 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1551 BSSAP.send(ts_RANAP_IuReleaseComplete);
1552 alt {
1553 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1554 setverdict(pass);
1555 }
1556 [] BSSAP.receive {
1557 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1558 mtc.stop;
1559 }
1560 }
1561 }
1562 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1563 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1564 mtc.stop;
1565 }
1566}
1567#endif
1568
Harald Welte17d21152018-01-27 00:47:11 +01001569
Harald Welte365f4ed2017-11-23 00:00:43 +01001570}