blob: 3198ee1352f354bdde082b1039e3c2f7f67e1e2e [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 }
Harald Welte6811d102019-04-14 22:23:14 +0200409 testcase.stop("RAN Connection table full!");
Harald Welte365f4ed2017-11-23 00:00:43 +0100410}
411
412private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200413runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100414 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
415 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100416 log("Deleted conn table entry ", i,
417 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100418 ConnectionTable[i].sccp_conn_id := -1;
419 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100420 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100421 }
422 }
Harald Welte6811d102019-04-14 22:23:14 +0200423 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200424 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100425}
426
Harald Welte17d21152018-01-27 00:47:11 +0100427private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200428runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100429 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
430 if (ImsiTable[i].imsi == imsi or
431 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
432 return ImsiTable[i].comp_ref;
433 }
434 }
435 return null;
436}
437
Harald Welte2fce7882019-04-15 11:48:05 +0200438#ifdef RAN_EMULATION_BSSAP
439type record BSSAP_Conn_Req {
440 SCCP_PAR_Address addr_peer,
441 SCCP_PAR_Address addr_own,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100442 PDU_BSSAP bssap optional
Harald Welte2fce7882019-04-15 11:48:05 +0200443}
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100444template 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 +0200445 addr_peer := peer,
446 addr_own := own,
447 bssap := bssap
448};
449
Harald Welte365f4ed2017-11-23 00:00:43 +0100450/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200451private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
452runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100453 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100454
Harald Welte473676b2018-01-27 20:38:01 +0100455 if (ischosen(bssap.pdu.bssmap)) {
456 /* BSC Side: If this is an assignment command, store CIC */
457 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
458 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
459 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
460 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
Pau Espin Pedrol43021cb2019-06-18 17:32:15 +0200461 var integer cic := f_bssmap_ie_cic_2_int(cic_ie);
Harald Welte473676b2018-01-27 20:38:01 +0100462 f_comp_store_cic(client, cic);
463 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100464 }
465
Harald Welte6811d102019-04-14 22:23:14 +0200466 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
467 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100468 /* we are the MS, so any message to us must be MT */
469 var PDU_DTAP_MT mt := {
470 dlci := bssap.dlci,
471 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
472 };
473 CLIENT.send(mt) to client;
474 } else {
475 /* we are the Network, so any message to us must be MO */
476 var PDU_DTAP_MO mo := {
477 dlci := bssap.dlci,
478 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
479 };
480 CLIENT.send(mo) to client;
481 }
482 } else {
483 CLIENT.send(bssap) to client;
484 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100485}
486
487/* call-back type, to be provided by specific implementation; called when new SCCP connection
488 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100489type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200490runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100491
492type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200493runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100494
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200495private function append_osmux_ie()
496runs on RAN_Emulation_CT return boolean {
497 return g_ran_ops.use_osmux and (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
498}
499
Harald Welte17d21152018-01-27 00:47:11 +0100500/* handle common Unitdata such as Paging */
501private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200502runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100503 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200504 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100505 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
506 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200507 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100508 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
509 client);
510 CLIENT.send(bssap) to client;
511 return omit;
512 }
513 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
514 } else {
515 log("CommonBssmapUnitdataCallback: Not a paging message");
516 }
517 /* ELSE: handle in user callback */
Harald Welte6811d102019-04-14 22:23:14 +0200518 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100519}
520
Harald Welte6811d102019-04-14 22:23:14 +0200521function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100522 timer T := 5.0;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200523 var boolean append_osmux_support := append_osmux_ie();
Eric Wild49888a62022-03-30 03:16:11 +0200524 var integer attempts := g_ran_ops.bssap_reset_retries;
Harald Weltea4ca4462018-02-09 00:17:14 +0100525
Eric Wild49888a62022-03-30 03:16:11 +0200526 while (attempts > 0) {
527 attempts := attempts - 1;
528
529 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0, append_osmux_support)));
530 T.start;
531 alt {
532 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck(append_osmux_support))) {
533 log("BSSMAP: Received RESET-ACK in response to RESET, we're ready to go!");
534 return;
535 }
536 [] as_reset_ack(append_osmux_support);
537 [] BSSAP.receive { repeat };
538 [] T.timeout {
539 continue;
540 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200541 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100542 }
Eric Wild49888a62022-03-30 03:16:11 +0200543
544 setverdict(fail, "BSSMAP: Timeout waiting for RESET-ACK after sending RESET");
545 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100546}
547
Harald Welte2fce7882019-04-15 11:48:05 +0200548private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
549 var template octetstring l3 := f_bssap_extract_l3(bssap);
550 return f_L3_is_rr(l3);
551}
552#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100553
Harald Welte5b027622019-04-14 23:40:17 +0200554#ifdef RAN_EMULATION_RANAP
555type record RANAP_Conn_Req {
556 SCCP_PAR_Address addr_peer,
557 SCCP_PAR_Address addr_own,
558 RANAP_PDU ranap
559}
560template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, RANAP_PDU ranap) := {
561 addr_peer := peer,
562 addr_own := own,
563 ranap := ranap
564};
565
566private function fake_dlci_from_sapi(template (omit) SAPI sapi) return template (omit) OCT1
567{
568 if (istemplatekind(sapi, "omit")) {
569 return omit;
570 } else if (valueof(sapi) == sapi_3) {
571 return '03'O;
572 }
573 return '00'O;
574}
575
576private function f_handle_userData_RANAP(RAN_ConnHdlr client, RANAP_PDU ranap)
577runs on RAN_Emulation_CT {
578 /* decode + send decoded RANAP to client */
579 var template (omit) octetstring l3 := f_ranap_extract_l3(ranap);
Harald Welte0af76152022-01-11 23:02:38 +0100580 if (istemplatekind(l3, "omit") or not g_ran_ops.decode_dtap) {
Harald Welte5b027622019-04-14 23:40:17 +0200581 CLIENT.send(ranap) to client;
582 } else {
583 var template (omit) SAPI sapi := f_ranap_extract_sapi(ranap);
584 var template (omit) OCT1 dlci := fake_dlci_from_sapi(sapi);
585 if (g_ran_ops.role_ms) {
586 /* we are the MS, so any message to us must be MT */
Harald Welte717c7302019-04-23 20:31:55 +0200587 if (g_ran_ops.ps_domain) {
588 var PDU_DTAP_PS_MT mt := {
589 dlci := omit,
590 dtap := dec_PDU_L3_SGSN_MS(valueof(l3))
591 };
592 if (isvalue(dlci)) {
593 mt.dlci := valueof(dlci);
594 }
595 CLIENT.send(mt) to client;
596 } else {
597 var PDU_DTAP_MT mt := {
598 dlci := omit,
599 dtap := dec_PDU_ML3_NW_MS(valueof(l3))
600 };
601 if (isvalue(dlci)) {
602 mt.dlci := valueof(dlci)
603 }
604 CLIENT.send(mt) to client;
Harald Welte5b027622019-04-14 23:40:17 +0200605 }
Harald Welte5b027622019-04-14 23:40:17 +0200606 } else {
607 /* we are the Network, so any message to us must be MO */
Harald Welte717c7302019-04-23 20:31:55 +0200608 if (g_ran_ops.ps_domain) {
609 var PDU_DTAP_PS_MO mo := {
610 dlci := omit,
611 dtap := dec_PDU_L3_MS_SGSN(valueof(l3))
612 };
613 if (isvalue(dlci)) {
614 mo.dlci := valueof(dlci);
615 }
616 CLIENT.send(mo) to client;
617 } else {
618 var PDU_DTAP_MO mo := {
619 dlci := omit,
620 dtap := dec_PDU_ML3_MS_NW(valueof(l3))
621 };
622 if (isvalue(dlci)) {
623 mo.dlci := valueof(dlci)
624 }
625 CLIENT.send(mo) to client;
Harald Welte5b027622019-04-14 23:40:17 +0200626 }
Harald Welte5b027622019-04-14 23:40:17 +0200627 }
628 }
629}
630
631/* call-back type, to be provided by specific implementation; called when new SCCP connection
632 * arrives */
633type function RanapCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
634runs on RAN_Emulation_CT return RAN_ConnHdlr;
635
636type function RanapUnitdataCallback(RANAP_PDU ranap)
637runs on RAN_Emulation_CT return template RANAP_PDU;
638
639private function CommonRanapUnitdataCallback(RANAP_PDU ranap)
640runs on RAN_Emulation_CT return template RANAP_PDU {
641 if (match(ranap, tr_RANAP_Paging(?, ?))) {
642 var RAN_ConnHdlr client := null;
643 /* extract IMSI and (if present) TMSI */
644 var IMSI imsi := ranap.initiatingMessage.value_.paging.protocolIEs[1].value_.permanentNAS_UE_ID.iMSI;
645 var template OCT4 tmsi := omit;
646 if (lengthof(ranap.initiatingMessage.value_.paging.protocolIEs) > 2 and
647 ranap.initiatingMessage.value_.paging.protocolIEs[2].id == id_TemporaryUE_ID) {
648 var TemporaryUE_ID ue_id;
649 ue_id := ranap.initiatingMessage.value_.paging.protocolIEs[2].value_.temporaryUE_ID;
650 if (ischosen(ue_id.tMSI)) {
651 tmsi := ue_id.tMSI;
652 } else {
653 tmsi := ue_id.p_TMSI;
654 }
655 }
656 client := f_imsi_table_find(oct2hex(imsi), tmsi);
Harald Welte03d4e2b2019-05-10 00:42:33 +0200657 if (client != null) {
Harald Welte5b027622019-04-14 23:40:17 +0200658 log("CommonRanapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
659 client);
660 CLIENT.send(ranap) to client;
661 return omit;
662 }
663 log("CommonRanapUnitdataCallback: IMSI/TMSI not found in table");
664 } else {
665 log("CommonRanapUnitdataCallback: Not a paging message");
666 }
667
668 /* ELSE: handle in user callback */
669 return g_ran_ops.ranap_unitdata_cb.apply(ranap);
670}
671
672private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean {
673 var template (omit) SAPI sapi;
674 var template octetstring l3 := f_ranap_extract_l3(ranap);
675 return f_L3_is_rr(l3);
676}
677
678function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
679 timer T := 5.0;
680 var CN_DomainIndicator dom;
681 if (g_ran_ops.ps_domain) {
682 dom := ps_domain;
683 } else {
684 dom := cs_domain;
685 }
686
687 RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom)));
688 T.start;
689 alt {
690 [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200691 log("RANAP: Received RESET-ACK in response to RESET, we're ready to go!");
Harald Welte5b027622019-04-14 23:40:17 +0200692 }
693 [] as_reset_ack();
694 [] RANAP.receive { repeat };
695 [] T.timeout {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200696 setverdict(fail, "RANAP: Timeout waiting for RESET-ACK after sending RESET");
Harald Welte5b027622019-04-14 23:40:17 +0200697 mtc.stop;
698 }
699 }
700}
701#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100702
Harald Welte2fce7882019-04-15 11:48:05 +0200703
704type enumerated RanProtocol {
Harald Welte5b027622019-04-14 23:40:17 +0200705 RAN_PROTOCOL_BSSAP,
706 RAN_PROTOCOL_RANAP
Harald Welte2fce7882019-04-15 11:48:05 +0200707}
708
709type record RanOps {
710#ifdef RAN_EMULATION_BSSAP
711 BssmapCreateCallback create_cb optional,
712 BssmapUnitdataCallback unitdata_cb optional,
713#endif
Harald Welte5b027622019-04-14 23:40:17 +0200714#ifdef RAN_EMULATION_RANAP
715 RanapCreateCallback ranap_create_cb optional,
716 RanapUnitdataCallback ranap_unitdata_cb optional,
717 boolean ps_domain,
718#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200719 boolean decode_dtap,
720 boolean role_ms,
721 RanProtocol protocol,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200722 RAN_Transport transport,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200723 boolean use_osmux,
Eric Wild49888a62022-03-30 03:16:11 +0200724 integer bssap_reset_retries,
Harald Welte2fce7882019-04-15 11:48:05 +0200725 /* needed for performing BSSMAP RESET */
726 SCCP_PAR_Address sccp_addr_local optional,
727 SCCP_PAR_Address sccp_addr_peer optional
728}
729
730template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
731
Harald Weltea803b722020-08-21 15:42:26 +0200732function f_L3_is_rr(template octetstring l3) return boolean {
Harald Welte2fce7882019-04-15 11:48:05 +0200733 if (not isvalue(l3)) {
734 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100735 }
Harald Welte2fce7882019-04-15 11:48:05 +0200736 var octetstring l3v := valueof(l3);
737 if (lengthof(l3v) < 1) {
738 return false;
739 }
740 /* lower 4 bits of first octet are protocol discriminator */
741 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
742 return true;
743 }
744 return false;
745}
Harald Weltea4ca4462018-02-09 00:17:14 +0100746
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200747function 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 +0200748 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200749 if (n_sd_idx == 0) {
750 seq_nr := n_sd[0];
751 n_sd[0] := (n_sd[0] + 1) mod 4;
752 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
753 seq_nr := n_sd[n_sd_idx];
754 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200755 } else {
756 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200757 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200758 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200759 return seq_nr;
760}
761
762/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
763function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200764 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
765 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
766 log("patched enc_l3: ", enc_l3);
767}
768
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200769function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
770 var uint2_t seq_nr;
771 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
772 return 0;
773 } else if (ischosen(dtap.msgs.gcc)) {
774 return 1;
775 } else if (ischosen(dtap.msgs.bcc)) {
776 return 2;
777 } else if (ischosen(dtap.msgs.loc)) {
778 return 3;
779 }
780 /* no sequence number to patch */
781 return -1;
782}
783
784/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
Harald Weltea803b722020-08-21 15:42:26 +0200785private 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 +0200786 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
787 if (n_sd_idx < 0) {
788 return;
789 }
790 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
791 f_ML3_patch_seq_nr(seq_nr, enc_l3);
792}
793
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200794private altstep as_reset_ack(boolean append_osmux_support := false) runs on RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200795#ifdef RAN_EMULATION_BSSAP
796 var BSSAP_N_UNITDATA_ind ud_ind;
797#endif
Harald Welte5b027622019-04-14 23:40:17 +0200798#ifdef RAN_EMULATION_RANAP
799 var RANAP_N_UNITDATA_ind rud_ind;
800#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200801#ifdef RAN_EMULATION_BSSAP
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200802 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(append_osmux_support))) -> value ud_ind {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200803 log("BSSMAP: Responding to inbound RESET with RESET-ACK");
Harald Welte2fce7882019-04-15 11:48:05 +0200804 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200805 ts_BSSMAP_ResetAck(append_osmux_support)));
Harald Welte2fce7882019-04-15 11:48:05 +0200806 repeat;
807 }
808#endif
Harald Welte5b027622019-04-14 23:40:17 +0200809#ifdef RAN_EMULATION_RANAP
810 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
Neels Hofmeyr4f5d7be2020-10-16 16:28:16 +0200811 log("RANAP: Responding to inbound IuRESET with IuRESET-ACK");
Harald Welte5b027622019-04-14 23:40:17 +0200812 var CN_DomainIndicator dom;
813 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
814 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
815 ts_RANAP_ResetAck(dom)));
816 }
817#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200818}
819
820
821private altstep as_main_bssap() runs on RAN_Emulation_CT {
822#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100823 var BSSAP_N_UNITDATA_ind ud_ind;
824 var BSSAP_N_CONNECT_ind conn_ind;
825 var BSSAP_N_CONNECT_cfm conn_cfm;
826 var BSSAP_N_DATA_ind data_ind;
827 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100828 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100829 var PDU_BSSAP bssap;
Neels Hofmeyra8264612020-06-08 20:40:22 +0200830 var BSSAP_N_UNITDATA_req bssap_ud;
Harald Welte2fce7882019-04-15 11:48:05 +0200831 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200832 var integer targetPointCode;
833 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100834
Harald Welte365f4ed2017-11-23 00:00:43 +0100835 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100836 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100837 /* Connectionless Procedures like RESET */
838 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100839 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100840 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100841 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
842 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100843 }
844 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100845 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100846 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200847 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100848 /* store mapping between client components and SCCP connectionId */
849 f_conn_table_add(vc_conn, conn_ind.connectionId);
850 /* handle user payload */
851 f_handle_userData(vc_conn, conn_ind.userData);
852 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100853 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100854 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100855 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100856 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100857 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100858 if (ispresent(data_ind.userData)) {
859 f_handle_userData(vc_conn, data_ind.userData);
860 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100861 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100862 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100863 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100864 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100865 if (ispresent(disc_ind.userData)) {
866 f_handle_userData(vc_conn, disc_ind.userData);
867 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100868 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200869 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100870 CLIENT.send(prim) to vc_conn;
871 f_conn_table_del(disc_ind.connectionId);
872 /* TOOD: return confirm to other side? */
873 }
Harald Welteb3414b22017-11-23 18:22:10 +0100874 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100875 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100876 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200877 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100878 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100879 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100880 if (ispresent(conn_cfm.userData)) {
881 f_handle_userData(vc_conn, conn_cfm.userData);
882 }
Harald Welteb3414b22017-11-23 18:22:10 +0100883 }
Harald Welte2fce7882019-04-15 11:48:05 +0200884 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
885 var integer conn_id := f_conn_id_by_comp(vc_conn);
886 /* send it to dispatcher */
887 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
888 }
Harald Welteb3414b22017-11-23 18:22:10 +0100889
Neels Hofmeyra8264612020-06-08 20:40:22 +0200890 [] CLIENT.receive(BSSAP_N_UNITDATA_req:?) -> value bssap_ud sender vc_conn {
891 BSSAP.send(bssap_ud);
892 }
893
Harald Welte365f4ed2017-11-23 00:00:43 +0100894 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200895 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100896 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100897 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100898 f_conn_table_del(conn_id);
899 }
900
901 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100902 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
903 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100904 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100905
906 if (f_comp_known(vc_conn) == false) {
907 /* unknown client, create new connection */
908 conn_id := f_gen_conn_id();
909
910 /* store mapping between client components and SCCP connectionId */
911 f_conn_table_add(vc_conn, conn_id);
912
Harald Welte004f5fb2017-12-16 17:54:40 +0100913 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
914 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100915 } else {
916 /* known client, send via existing connection */
917 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100918 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100919 }
920
Harald Welte49518bf2018-02-10 11:39:19 +0100921 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
922 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200923 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100924 /* we have just sent the first MM message, increment the counter */
925 var integer idx := f_idx_by_comp(vc_conn);
926 ConnectionTable[idx].n_sd[0] := 1;
927 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
928 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200929 }
Harald Welte9dadc522018-02-06 13:56:04 +0100930
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200931 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
932 var integer idx := f_idx_by_comp(vc_conn);
933 last_n_sd := ConnectionTable[idx].n_sd;
934 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
935 }
936
937 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
938 var integer idx := f_idx_by_comp(vc_conn);
939 ConnectionTable[idx].n_sd := last_n_sd;
940 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100941 }
Harald Welte2fce7882019-04-15 11:48:05 +0200942#else
Harald Welte5b027622019-04-14 23:40:17 +0200943 [false] CLIENT.receive {}
944#endif
945}
946
947private altstep as_main_ranap() runs on RAN_Emulation_CT {
948#ifdef RAN_EMULATION_RANAP
949 var RANAP_N_UNITDATA_ind rud_ind;
950 var RANAP_N_CONNECT_ind rconn_ind;
951 var RANAP_N_CONNECT_cfm rconn_cfm;
952 var RANAP_N_DATA_ind rdata_ind;
953 var RANAP_N_DISCONNECT_ind rdisc_ind;
954 var RANAP_Conn_Req creq;
955 var RANAP_PDU ranap;
956 var RAN_ConnHdlr vc_conn;
Harald Welte717c7302019-04-23 20:31:55 +0200957 var PDU_DTAP_PS_MO ps_mo;
958 var PDU_DTAP_PS_MT ps_mt;
Harald Welte5b027622019-04-14 23:40:17 +0200959
960 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
961 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
962 /* Connectionless Procedures like RESET */
963 var template RANAP_PDU resp;
964 resp := CommonRanapUnitdataCallback(rud_ind.userData);
965 if (isvalue(resp)) {
966 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
967 rud_ind.calledAddress, resp));
968 }
969 }
970 /* SCCP -> Client: new connection from BSC */
971 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
972 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
973 /* store mapping between client components and SCCP connectionId */
974 f_conn_table_add(vc_conn, rconn_ind.connectionId);
975 /* handle user payload */
976 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
977 /* confirm connection establishment */
978 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
979 }
980 /* SCCP -> Client: connection-oriented data in existing connection */
981 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
982 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
983 if (ispresent(rdata_ind.userData)) {
984 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
985 }
986 }
987 /* SCCP -> Client: disconnect of an existing connection */
988 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
989 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
990 if (ispresent(rdisc_ind.userData)) {
991 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
992 }
993 /* notify client about termination */
994 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
995 CLIENT.send(prim) to vc_conn;
996 f_conn_table_del(rdisc_ind.connectionId);
997 /* TOOD: return confirm to other side? */
998 }
999 /* SCCP -> Client: connection confirm for outbound connection */
1000 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
1001 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
1002 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
1003 CLIENT.send(prim) to vc_conn;
1004 /* handle user payload */
1005 if (ispresent(rconn_cfm.userData)) {
1006 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
1007 }
1008 }
1009
1010 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
1011 var integer conn_id := f_conn_id_by_comp(vc_conn);
1012 /* send it to dispatcher */
1013 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
1014 }
1015
1016 /* Disconnect request client -> SCCP */
1017 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
1018 var integer conn_id := f_conn_id_by_comp(vc_conn);
1019 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
1020 f_conn_table_del(conn_id);
1021 }
1022
1023 /* BSSAP from client -> SCCP */
1024 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
1025 var integer conn_id;
1026 /* send to dispatcher */
1027
1028 if (f_comp_known(vc_conn) == false) {
1029 /* unknown client, create new connection */
1030 conn_id := f_gen_conn_id();
1031
1032 /* store mapping between client components and SCCP connectionId */
1033 f_conn_table_add(vc_conn, conn_id);
1034
1035 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
1036 creq.ranap));
1037 } else {
1038 /* known client, send via existing connection */
1039 conn_id := f_conn_id_by_comp(vc_conn);
1040 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
1041 }
1042
1043 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
1044 * counter only on MM/CC/SS, but not on RR */
1045 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
1046 /* we have just sent the first MM message, increment the counter */
1047 var integer idx := f_idx_by_comp(vc_conn);
1048 ConnectionTable[idx].n_sd[0] := 1;
1049 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
1050 }
1051 }
1052
Harald Welte717c7302019-04-23 20:31:55 +02001053 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MO:?) -> value ps_mo sender vc_conn {
1054 var integer idx := f_idx_by_comp(vc_conn);
1055 /* convert from decoded DTAP to encoded DTAP */
1056 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(ps_mo.dtap);
1057 /* patch correct L3 send sequence number N(SD) into l3_enc */
1058 if (ps_mo.skip_seq_patching == false) {
1059 //f_ML3_patch_seq(ConnectionTable[idx], ps_mo.dtap, l3_enc);
1060 }
1061 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mo.dlci, l3_enc);
1062 }
1063
1064 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MT:?) -> value ps_mt sender vc_conn {
1065 var integer idx := f_idx_by_comp(vc_conn);
1066 /* convert from decoded DTAP to encoded DTAP */
1067 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(ps_mt.dtap);
1068 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mt.dlci, l3_enc);
1069 }
1070
1071
Harald Welte5b027622019-04-14 23:40:17 +02001072#else
1073 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +02001074#endif
1075}
Harald Welteb3414b22017-11-23 18:22:10 +01001076
Harald Welte2fce7882019-04-15 11:48:05 +02001077private altstep as_main_mgcp() runs on RAN_Emulation_CT {
1078#ifdef RAN_EMULATION_MGCP
1079 var MgcpCommand mgcp_req;
1080 var MgcpResponse mgcp_resp;
1081 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +01001082
Harald Weltec82eef42017-11-24 20:40:12 +01001083 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
1084 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
1085 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
1086 * is printed as hex string, e.g. a@mgw for CIC 10 */
1087
1088 /* CLIENT -> MGCP */
1089 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
1090 /* MGCP request from Handler (we're MSC) */
1091 /* store the transaction ID we've seen */
1092 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
1093 /* simply forward any MGCP from the client to the port */
1094 MGCP.send(mgcp_req);
1095 }
1096 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
1097 /* MGCP response from Handler (we're BSC/MGW) */
1098 /* simply forward any MGCP from the client to the port */
1099 MGCP.send(mgcp_resp);
1100 }
1101
1102 /* MGCP -> CLIENT */
1103 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1104 /* MGCP request from network side (we're BSC/MGW) */
1105 /* Extract CIC from local part of endpoint name */
1106 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001107 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1108 /* ignore RSIP for unknown CIC */
1109 } else {
1110 /* Resolve the vc_conn by the CIC */
1111 vc_conn := f_comp_by_cic(cic);
1112 CLIENT.send(mgcp_req) to vc_conn;
1113 }
Harald Weltec82eef42017-11-24 20:40:12 +01001114 }
1115 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1116 /* MGCP response from network side (we're MSC) */
1117 /* Resolve the vc_conn by the transaction ID */
1118 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1119 CLIENT.send(mgcp_resp) to vc_conn;
1120 }
Harald Welte2fce7882019-04-15 11:48:05 +02001121#else
1122 [false] CLIENT.receive {}
1123#endif
1124}
Harald Weltec82eef42017-11-24 20:40:12 +01001125
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001126private altstep as_main_ctrl() runs on RAN_Emulation_CT {
1127#ifdef RAN_EMULATION_CTRL
1128 var CtrlMessage ctrl;
1129 var RAN_ConnHdlr vc_conn;
1130 var ASP_IPA_Event evt;
1131
1132 /* Handling of CTRL in IPA SCCPLite case. This predates 3GPP AoIP
1133 * and uses a CTRL session in parallel to BSSAP. */
1134
1135 /* CTRL_CLIENT -> CTRL */
1136 [] CTRL_CLIENT.receive(CtrlMessage:?) -> value ctrl sender vc_conn {
1137 CTRL.send(ctrl);
1138 }
1139
1140 /* CTRL -> CTRL_CLIENT */
1141 [] CTRL.receive(CtrlMessage:?) -> value ctrl {
1142 CTRL_CLIENT.send(ctrl);
1143 }
1144
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001145 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_UP)) -> value evt {
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001146 CTRL_CLIENT.send(evt);
1147 }
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001148 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN)) {
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001149 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Lost IPA connection!");
1150 }
Vadim Yanitskiya2afacc2020-05-18 21:16:19 +07001151 [] CTRL.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_ID_ACK)) {}
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001152#else
1153 [false] CLIENT.receive {}
1154#endif
1155}
1156
Harald Welte2fce7882019-04-15 11:48:05 +02001157/* send a raw (encoded) L3 message over given SCCP connection */
1158private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1159{
1160 select (g_ran_ops.protocol) {
1161#ifdef RAN_EMULATION_BSSAP
1162 case (RAN_PROTOCOL_BSSAP) {
1163 var PDU_BSSAP bssap;
1164 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1165 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1166 }
1167#endif
Harald Welte5b027622019-04-14 23:40:17 +02001168#ifdef RAN_EMULATION_RANAP
1169 case (RAN_PROTOCOL_RANAP) {
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001170 var template (omit) RANAP_IEs.SAPI sapi := omit;
Harald Welte5b027622019-04-14 23:40:17 +02001171 var RANAP_PDU ranap;
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001172
1173 /* Perform DLCI -> SAPI transformation (x & 0x07) */
1174 if (dlci and4b '07'O == '03'O) {
1175 sapi := sapi_3;
1176 }
1177
1178 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc, sapi := sapi));
Harald Welte5b027622019-04-14 23:40:17 +02001179 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1180 }
1181#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001182 }
1183}
1184
1185function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1186
1187 g_ran_id := id;
1188 g_ran_ops := ops;
1189 f_conn_table_init();
1190 f_expect_table_init();
1191
1192 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1193 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001194 select (g_ran_ops.protocol) {
1195#ifdef RAN_EMULATION_BSSAP
1196 case (RAN_PROTOCOL_BSSAP) {
1197 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1198 }
1199#endif
1200#ifdef RAN_EMULATION_RANAP
1201 case (RAN_PROTOCOL_RANAP) {
1202 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1203 }
1204#endif
1205 }
Harald Welte2fce7882019-04-15 11:48:05 +02001206 }
1207
1208 while (true) {
1209 var RAN_ConnHdlr vc_conn;
1210 var PDU_DTAP_MO dtap_mo;
1211 var PDU_DTAP_MT dtap_mt;
1212 var RAN_ConnHdlr vc_hdlr;
1213 var octetstring l3_info;
1214 var hexstring imsi;
1215 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001216 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001217
1218 alt {
1219 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001220 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001221
1222 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1223 var integer idx := f_idx_by_comp(vc_conn);
1224 /* convert from decoded DTAP to encoded DTAP */
1225 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1226 /* patch correct L3 send sequence number N(SD) into l3_enc */
1227 if (dtap_mo.skip_seq_patching == false) {
1228 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1229 }
1230 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1231 }
1232
1233 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1234 var integer idx := f_idx_by_comp(vc_conn);
1235 /* convert from decoded DTAP to encoded DTAP */
1236 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1237 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1238 }
1239
1240 [] as_main_mgcp();
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001241 [] as_main_ctrl();
Harald Welte624f9632017-12-16 19:26:04 +01001242
Harald Welte6811d102019-04-14 22:23:14 +02001243 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001244 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001245 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001246 }
1247
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001248 [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
1249 f_create_expect(omit, vc_hdlr, targetPointCode);
1250 PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
1251 }
1252
Harald Welte6811d102019-04-14 22:23:14 +02001253 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001254 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001255 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001256 }
Harald Welteee4d70c2022-05-16 21:29:19 +02001257 [] PROC.getcall(RAN_unregister_imsi:{?,?}) -> param(imsi, vc_hdlr) {
1258 f_destroy_imsi(imsi, vc_hdlr);
1259 PROC.reply(RAN_unregister_imsi:{imsi, vc_hdlr}) to vc_hdlr;
1260 }
Harald Welte17d21152018-01-27 00:47:11 +01001261
Harald Welte365f4ed2017-11-23 00:00:43 +01001262 }
1263 }
1264}
1265
Harald Welte2fce7882019-04-15 11:48:05 +02001266#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001267private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001268 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001269 return hex2int(str2hex(local_part));
1270
1271}
Harald Welte2fce7882019-04-15 11:48:05 +02001272#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001273
Harald Welte624f9632017-12-16 19:26:04 +01001274/***********************************************************************
1275 * "Expect" Handling (mapping for expected incoming SCCP connections)
1276 ***********************************************************************/
1277
1278/* data about an expected future incoming connection */
1279type record ExpectData {
1280 /* L3 payload based on which we can match it */
1281 octetstring l3_payload optional,
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001282 integer handoverRequestPointCode optional,
Harald Welte624f9632017-12-16 19:26:04 +01001283 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001284 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001285}
1286
1287/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001288signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001289signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001290
Harald Welte17d21152018-01-27 00:47:11 +01001291/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001292signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welteee4d70c2022-05-16 21:29:19 +02001293signature RAN_unregister_imsi(in hexstring imsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001294
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001295/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1296signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1297
1298/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1299signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1300
Harald Welte6811d102019-04-14 22:23:14 +02001301type port RAN_PROC_PT procedure {
Harald Welteee4d70c2022-05-16 21:29:19 +02001302 inout RAN_register, RAN_register_imsi, RAN_unregister_imsi, RAN_register_handoverRequest, RAN_last_n_sd, RAN_continue_after_n_sd;
Harald Welte624f9632017-12-16 19:26:04 +01001303} with { extension "internal" };
1304
Harald Welte5b027622019-04-14 23:40:17 +02001305#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001306/* CreateCallback that can be used as create_cb and will use the expectation table */
1307function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001308runs on RAN_Emulation_CT return RAN_ConnHdlr {
1309 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001310 var octetstring l3_info;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001311 var boolean handoverRequest := false;
1312 var integer handoverRequestPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001313 var integer i;
1314
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001315 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1316 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1317 log("ExpectedCreateCallback completeLayer3Information");
1318 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
1319 handoverRequest := true;
1320 handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1321 log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode);
1322 } else {
1323 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001324 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001325 }
Harald Welte624f9632017-12-16 19:26:04 +01001326
1327 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001328 if (handoverRequest) {
1329 log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode,
1330 " ==? ", handoverRequestPointCode);
1331 if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) {
1332 ret := ExpectTable[i].vc_conn;
1333 log("Found Expect[", i, "] for handoverRequest handled at ", ret);
1334 return ret;
1335 } else {
1336 continue;
1337 }
1338 }
Harald Welte624f9632017-12-16 19:26:04 +01001339 if (not ispresent(ExpectTable[i].l3_payload)) {
1340 continue;
1341 }
1342 if (l3_info == ExpectTable[i].l3_payload) {
1343 ret := ExpectTable[i].vc_conn;
1344 /* release this entry to be used again */
1345 ExpectTable[i].l3_payload := omit;
1346 ExpectTable[i].vc_conn := null;
1347 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1348 /* return the component reference */
1349 return ret;
1350 }
1351 }
1352 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001353 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001354}
Harald Welte5b027622019-04-14 23:40:17 +02001355#endif
1356
1357#ifdef RAN_EMULATION_RANAP
1358/* CreateCallback that can be used as create_cb and will use the expectation table */
1359function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1360runs on RAN_Emulation_CT return RAN_ConnHdlr {
1361 var RAN_ConnHdlr ret := null;
1362 var template (omit) octetstring l3_info;
1363 var integer i;
1364
1365 l3_info := f_ranap_extract_l3(conn_ind.userData);
1366 if (istemplatekind(l3_info, "omit")) {
1367 setverdict(fail, "N-CONNECT.ind without NAS payload");
1368 mtc.stop;
1369 return ret;
1370 }
1371
1372 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
1373 if (not ispresent(ExpectTable[i].l3_payload)) {
1374 continue;
1375 }
1376 if (valueof(l3_info) == ExpectTable[i].l3_payload) {
1377 ret := ExpectTable[i].vc_conn;
1378 /* release this entry to be used again */
1379 ExpectTable[i].l3_payload := omit;
1380 ExpectTable[i].vc_conn := null;
1381 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1382 /* return the component reference */
1383 return ret;
1384 }
1385 }
1386 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1387 mtc.stop;
1388 return ret;
1389}
1390#endif
Harald Welte624f9632017-12-16 19:26:04 +01001391
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001392private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
1393 template integer handoverRequestPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001394runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001395 var integer i;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001396 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001397 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001398 if (not ispresent(ExpectTable[i].l3_payload)
1399 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
1400 if (ispresent(l3)) {
1401 ExpectTable[i].l3_payload := valueof(l3);
1402 }
1403 if (ispresent(handoverRequestPointCode)) {
1404 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
1405 }
Harald Welte624f9632017-12-16 19:26:04 +01001406 ExpectTable[i].vc_conn := hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001407 if (ispresent(handoverRequestPointCode)) {
1408 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
1409 } else {
1410 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1411 }
Harald Welte624f9632017-12-16 19:26:04 +01001412 return;
1413 }
1414 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001415 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +01001416}
1417
Harald Welte6811d102019-04-14 22:23:14 +02001418private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1419runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001420 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1421 if (not ispresent(ImsiTable[i].imsi)) {
1422 ImsiTable[i].imsi := imsi;
1423 ImsiTable[i].tmsi := tmsi;
1424 ImsiTable[i].comp_ref := hdlr;
1425 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1426 return;
1427 }
1428 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001429 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +01001430}
1431
Harald Welteee4d70c2022-05-16 21:29:19 +02001432private function f_destroy_imsi(hexstring imsi, RAN_ConnHdlr hdlr)
1433runs on RAN_Emulation_CT {
1434 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1435 if (ImsiTable[i].imsi == imsi and ImsiTable[i].comp_ref == hdlr) {
1436 ImsiTable[i].comp_ref := null;
1437 ImsiTable[i].imsi := omit;
1438 ImsiTable[i].tmsi := 'FFFFFFFF'O;
1439 log("Removed IMSI[", i, "] for ", imsi, " to be handled at ", hdlr);
1440 return;
1441 }
1442 }
1443 testcase.stop("Unable to find to-be-destroyed IMSI in ImsiTable");
1444}
Harald Welte17d21152018-01-27 00:47:11 +01001445
Daniel Willmannd47106b2018-01-17 12:20:56 +01001446private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001447runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001448 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1449 ExpectTable[i].l3_payload := omit;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001450 ExpectTable[i].handoverRequestPointCode := omit;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001451 }
1452}
Harald Welte624f9632017-12-16 19:26:04 +01001453
Harald Welte17d21152018-01-27 00:47:11 +01001454/* helper function for clients to register their IMSI/TMSI */
Vadim Yanitskiyae747742020-01-10 00:23:10 +01001455function f_ran_register_imsi(hexstring imsi, template (omit) OCT4 tmsi_or_omit)
Harald Welte6811d102019-04-14 22:23:14 +02001456runs on RAN_ConnHdlr {
Vadim Yanitskiyae747742020-01-10 00:23:10 +01001457 var OCT4 tmsi;
1458
1459 /* Resolve omit to a special reserved value */
1460 if (istemplatekind(tmsi_or_omit, "omit")) {
1461 tmsi := 'FFFFFFFF'O;
1462 } else {
1463 tmsi := valueof(tmsi_or_omit);
1464 }
1465
Harald Welte6811d102019-04-14 22:23:14 +02001466 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1467 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001468 }
1469}
1470
Harald Welteee4d70c2022-05-16 21:29:19 +02001471/* helper function for clients to register their IMSI/TMSI */
1472function f_ran_unregister_imsi(hexstring imsi)
1473runs on RAN_ConnHdlr {
1474
1475 BSSAP_PROC.call(RAN_unregister_imsi:{imsi, self}) {
1476 [] BSSAP_PROC.getreply(RAN_unregister_imsi:{?,?}) {};
1477 }
1478}
1479
Harald Welte1e3bbbe2022-01-11 23:04:33 +01001480/* register an expect with the BSSMAP core */
1481function f_ran_register_exp(octetstring l3_enc) runs on RAN_ConnHdlr {
1482 BSSAP_PROC.call(RAN_register:{l3_enc, self}) {
1483 [] BSSAP_PROC.getreply(RAN_register:{?, ?}) {};
1484 }
1485}
1486
Harald Welte475a2c12019-05-02 19:05:48 +02001487#ifdef RAN_EMULATION_RANAP
1488/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1489altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1490 var RANAP_PDU ranap;
1491 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1492 BSSAP.send(ts_RANAP_IuReleaseComplete);
1493 alt {
1494 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1495 setverdict(pass);
1496 }
1497 [] BSSAP.receive {
1498 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1499 mtc.stop;
1500 }
1501 }
1502 }
1503 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1504 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1505 mtc.stop;
1506 }
1507}
1508#endif
1509
Harald Welte17d21152018-01-27 00:47:11 +01001510
Harald Welte365f4ed2017-11-23 00:00:43 +01001511}