blob: 891de09d6b7bae447a7b40811ae9fcc6ae0138be [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,
Harald Weltea4ca4462018-02-09 00:17:14 +0100165 /* Client requests us to create SCCP Connection */
166 BSSAP_Conn_Req,
Harald Welte2fce7882019-04-15 11:48:05 +0200167#endif
Harald Welte5b027622019-04-14 23:40:17 +0200168#ifdef RAN_EMULATION_RANAP
169 RANAP_PDU,
170 /* Client requests us to create SCCP Connection */
171 RANAP_Conn_Req,
172#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200173#ifdef RAN_EMULATION_MGCP
Harald Weltea4ca4462018-02-09 00:17:14 +0100174 /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */
Harald Welte2fce7882019-04-15 11:48:05 +0200175 MgcpCommand, MgcpResponse,
176#endif
177 /* direct DTAP messages from/to clients */
178 PDU_DTAP_MO, PDU_DTAP_MT,
Harald Welte717c7302019-04-23 20:31:55 +0200179 PDU_DTAP_PS_MO, PDU_DTAP_PS_MT,
Harald Welte2fce7882019-04-15 11:48:05 +0200180 /* misc indications / requests between SCCP and client */
181 RAN_Conn_Prim;
Harald Welte365f4ed2017-11-23 00:00:43 +0100182} with { extension "internal" };
183
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200184type uint2_t N_Sd_Array[4];
Harald Welte365f4ed2017-11-23 00:00:43 +0100185
186/* represents a single BSSAP connection over SCCP */
187type record ConnectionData {
188 /* reference to the instance of the per-connection component */
Harald Welte6811d102019-04-14 22:23:14 +0200189 RAN_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +0100190 integer sccp_conn_id,
Harald Welte2fce7882019-04-15 11:48:05 +0200191#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100192 /* most recent MGCP transaction ID (Used on MSC side) */
193 MgcpTransId mgcp_trans_id optional,
Harald Welte2fce7882019-04-15 11:48:05 +0200194#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100195 /* CIC that has been used for voice of this channel (BSC side) */
Harald Welte9dadc522018-02-06 13:56:04 +0100196 integer cic optional,
197 /* array of N(SD) values for MO DTAP messages, indexed by discriminator */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200198 N_Sd_Array n_sd
Harald Welte365f4ed2017-11-23 00:00:43 +0100199}
200
Harald Welte17d21152018-01-27 00:47:11 +0100201type record ImsiMapping {
Harald Welte6811d102019-04-14 22:23:14 +0200202 RAN_ConnHdlr comp_ref,
Harald Welte17d21152018-01-27 00:47:11 +0100203 hexstring imsi optional,
204 OCT4 tmsi
205}
206
Harald Welte6811d102019-04-14 22:23:14 +0200207type component RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200208 /* SCCP ports on the bottom side, using ASP primitives */
209#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100210 port BSSAP_CODEC_PT BSSAP;
Harald Welte2fce7882019-04-15 11:48:05 +0200211#endif
Harald Welte5b027622019-04-14 23:40:17 +0200212#ifdef RAN_EMULATION_RANAP
213 port RANAP_CODEC_PT RANAP;
214#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100215 /* BSSAP port to the per-connection clients */
Harald Welte6811d102019-04-14 22:23:14 +0200216 port RAN_Conn_PT CLIENT;
Harald Welte2fce7882019-04-15 11:48:05 +0200217#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100218 /* MGCP port */
219 port IPA_MGCP_PT MGCP;
Harald Welte2fce7882019-04-15 11:48:05 +0200220#endif
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +0200221#ifdef RAN_EMULATION_CTRL
222 /* CTRL port */
223 port IPA_CTRL_PT CTRL;
224 port IPA_CTRL_PT CTRL_CLIENT;
225#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100226
227 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
228 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +0100229
Harald Welte624f9632017-12-16 19:26:04 +0100230 /* pending expected incoming connections */
231 var ExpectData ExpectTable[8];
Harald Welte17d21152018-01-27 00:47:11 +0100232
233 /* tables for mapping inbound unitdata (like paging) */
234 var ImsiMapping ImsiTable[16];
235
Harald Welte624f9632017-12-16 19:26:04 +0100236 /* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +0200237 port RAN_PROC_PT PROC;
Harald Welte624f9632017-12-16 19:26:04 +0100238
Harald Welte6811d102019-04-14 22:23:14 +0200239 var charstring g_ran_id;
Harald Welte66fecd42017-11-24 23:53:23 +0100240 var integer g_next_e1_ts := 1;
Harald Welte6811d102019-04-14 22:23:14 +0200241 var RanOps g_ran_ops;
Harald Welte365f4ed2017-11-23 00:00:43 +0100242};
243
Harald Welteb3414b22017-11-23 18:22:10 +0100244private function f_conn_id_known(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200245runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100246 var integer i;
247 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
248 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
249 return true;
250 }
251 }
252 return false;
253}
254
Harald Welte6811d102019-04-14 22:23:14 +0200255private function f_comp_known(RAN_ConnHdlr client)
256runs on RAN_Emulation_CT return boolean {
Harald Welteb3414b22017-11-23 18:22:10 +0100257 var integer i;
258 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
259 if (ConnectionTable[i].comp_ref == client) {
260 return true;
261 }
262 }
263 return false;
264}
Harald Welte365f4ed2017-11-23 00:00:43 +0100265
Harald Weltee98bb2e2017-11-29 12:09:48 +0100266private function f_cic_known(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200267runs on RAN_Emulation_CT return boolean {
Harald Weltee98bb2e2017-11-29 12:09:48 +0100268 var integer i;
269 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
270 if (ConnectionTable[i].cic == cic) {
271 return true;
272 }
273 }
274 return false;
275}
276
Harald Welte365f4ed2017-11-23 00:00:43 +0100277/* resolve component reference by connection ID */
278private function f_comp_by_conn_id(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200279runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +0100280 var integer i;
281 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
282 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
283 return ConnectionTable[i].comp_ref;
284 }
285 }
Harald Welte6811d102019-04-14 22:23:14 +0200286 setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200287 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100288}
289
Harald Welte2fce7882019-04-15 11:48:05 +0200290
291#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100292/* resolve component reference by CIC */
293private function f_comp_by_mgcp_tid(MgcpTransId tid)
Harald Welte6811d102019-04-14 22:23:14 +0200294runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100295 var integer i;
296 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
297 if (ConnectionTable[i].mgcp_trans_id == tid) {
298 return ConnectionTable[i].comp_ref;
299 }
300 }
Harald Welte6811d102019-04-14 22:23:14 +0200301 setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200302 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100303}
304
Harald Welte6811d102019-04-14 22:23:14 +0200305private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid)
306runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100307 var integer i;
308 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
309 if (ConnectionTable[i].comp_ref == client) {
310 ConnectionTable[i].mgcp_trans_id := tid;
311 return;
312 }
313 }
Harald Welte6811d102019-04-14 22:23:14 +0200314 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200315 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100316}
Harald Welte2fce7882019-04-15 11:48:05 +0200317#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100318
319private function f_comp_by_cic(integer cic)
Harald Welte6811d102019-04-14 22:23:14 +0200320runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +0100321 var integer i;
322 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
323 if (ConnectionTable[i].cic == cic) {
324 return ConnectionTable[i].comp_ref;
325 }
326 }
Harald Welte6811d102019-04-14 22:23:14 +0200327 setverdict(fail, "RAN Connection table not found by CIC ", cic);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200328 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100329}
330
Harald Welte6811d102019-04-14 22:23:14 +0200331private function f_comp_store_cic(RAN_ConnHdlr client, integer cic)
332runs on RAN_Emulation_CT {
Harald Weltec82eef42017-11-24 20:40:12 +0100333 var integer i;
334 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
335 if (ConnectionTable[i].comp_ref == client) {
336 ConnectionTable[i].cic := cic;
337 return;
338 }
339 }
Harald Welte6811d102019-04-14 22:23:14 +0200340 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200341 mtc.stop;
Harald Weltec82eef42017-11-24 20:40:12 +0100342}
343
Harald Welte365f4ed2017-11-23 00:00:43 +0100344/* resolve connection ID by component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200345private function f_conn_id_by_comp(RAN_ConnHdlr client)
346runs on RAN_Emulation_CT return integer {
Harald Welte365f4ed2017-11-23 00:00:43 +0100347 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
348 if (ConnectionTable[i].comp_ref == client) {
349 return ConnectionTable[i].sccp_conn_id;
350 }
351 }
Harald Welte6811d102019-04-14 22:23:14 +0200352 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200353 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100354}
355
Harald Welte9dadc522018-02-06 13:56:04 +0100356/* resolve ConnectionTable index component reference */
Harald Welte6811d102019-04-14 22:23:14 +0200357private function f_idx_by_comp(RAN_ConnHdlr client)
358runs on RAN_Emulation_CT return integer {
Harald Welte9dadc522018-02-06 13:56:04 +0100359 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
360 if (ConnectionTable[i].comp_ref == client) {
361 return i;
362 }
363 }
Harald Welte6811d102019-04-14 22:23:14 +0200364 setverdict(fail, "RAN Connection table not found by component ", client);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200365 mtc.stop;
Harald Welte9dadc522018-02-06 13:56:04 +0100366}
367
Harald Welteb3414b22017-11-23 18:22:10 +0100368private function f_gen_conn_id()
Harald Welte6811d102019-04-14 22:23:14 +0200369runs on RAN_Emulation_CT return integer {
Harald Welteb3414b22017-11-23 18:22:10 +0100370 var integer conn_id;
371
372 do {
373 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
374 } while (f_conn_id_known(conn_id) == true);
375
376 return conn_id;
377}
378
Harald Welte365f4ed2017-11-23 00:00:43 +0100379private function f_conn_table_init()
Harald Welte6811d102019-04-14 22:23:14 +0200380runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100381 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
382 ConnectionTable[i].comp_ref := null;
383 ConnectionTable[i].sccp_conn_id := -1;
Harald Welte2fce7882019-04-15 11:48:05 +0200384#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +0100385 ConnectionTable[i].mgcp_trans_id := omit;
Harald Welte2fce7882019-04-15 11:48:05 +0200386#endif
Harald Weltec82eef42017-11-24 20:40:12 +0100387 ConnectionTable[i].cic := omit;
Harald Welte9dadc522018-02-06 13:56:04 +0100388 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welte365f4ed2017-11-23 00:00:43 +0100389 }
Harald Welte17d21152018-01-27 00:47:11 +0100390 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
391 ImsiTable[i].comp_ref := null;
392 ImsiTable[i].imsi := omit;
393 ImsiTable[i].tmsi := 'FFFFFFFF'O;
394 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100395}
396
Harald Welte6811d102019-04-14 22:23:14 +0200397private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id)
398runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100399 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
400 if (ConnectionTable[i].sccp_conn_id == -1) {
401 ConnectionTable[i].comp_ref := comp_ref;
402 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welte9dadc522018-02-06 13:56:04 +0100403 ConnectionTable[i].n_sd := { 0, 0, 0, 0 };
Harald Welteb3414b22017-11-23 18:22:10 +0100404 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100405 return;
406 }
407 }
Harald Welte6811d102019-04-14 22:23:14 +0200408 testcase.stop("RAN Connection table full!");
Harald Welte365f4ed2017-11-23 00:00:43 +0100409}
410
411private function f_conn_table_del(integer sccp_conn_id)
Harald Welte6811d102019-04-14 22:23:14 +0200412runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100413 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
414 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100415 log("Deleted conn table entry ", i,
416 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100417 ConnectionTable[i].sccp_conn_id := -1;
418 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100419 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100420 }
421 }
Harald Welte6811d102019-04-14 22:23:14 +0200422 setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200423 mtc.stop;
Harald Welte365f4ed2017-11-23 00:00:43 +0100424}
425
Harald Welte17d21152018-01-27 00:47:11 +0100426private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
Harald Welte6811d102019-04-14 22:23:14 +0200427runs on RAN_Emulation_CT return RAN_ConnHdlr {
Harald Welte17d21152018-01-27 00:47:11 +0100428 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
429 if (ImsiTable[i].imsi == imsi or
430 isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) {
431 return ImsiTable[i].comp_ref;
432 }
433 }
434 return null;
435}
436
Harald Welte2fce7882019-04-15 11:48:05 +0200437#ifdef RAN_EMULATION_BSSAP
438type record BSSAP_Conn_Req {
439 SCCP_PAR_Address addr_peer,
440 SCCP_PAR_Address addr_own,
441 PDU_BSSAP bssap
442}
443template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := {
444 addr_peer := peer,
445 addr_own := own,
446 bssap := bssap
447};
448
Harald Welte365f4ed2017-11-23 00:00:43 +0100449/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte6811d102019-04-14 22:23:14 +0200450private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap)
451runs on RAN_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100452 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100453
Harald Welte473676b2018-01-27 20:38:01 +0100454 if (ischosen(bssap.pdu.bssmap)) {
455 /* BSC Side: If this is an assignment command, store CIC */
456 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
457 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
458 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
459 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
Pau Espin Pedrol43021cb2019-06-18 17:32:15 +0200460 var integer cic := f_bssmap_ie_cic_2_int(cic_ie);
Harald Welte473676b2018-01-27 20:38:01 +0100461 f_comp_store_cic(client, cic);
462 }
Harald Welte1b2748e2017-11-24 23:40:16 +0100463 }
464
Harald Welte6811d102019-04-14 22:23:14 +0200465 if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) {
466 if (g_ran_ops.role_ms) {
Harald Welte0b476062018-01-21 19:07:32 +0100467 /* we are the MS, so any message to us must be MT */
468 var PDU_DTAP_MT mt := {
469 dlci := bssap.dlci,
470 dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap)
471 };
472 CLIENT.send(mt) to client;
473 } else {
474 /* we are the Network, so any message to us must be MO */
475 var PDU_DTAP_MO mo := {
476 dlci := bssap.dlci,
477 dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap)
478 };
479 CLIENT.send(mo) to client;
480 }
481 } else {
482 CLIENT.send(bssap) to client;
483 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100484}
485
486/* call-back type, to be provided by specific implementation; called when new SCCP connection
487 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100488type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +0200489runs on RAN_Emulation_CT return RAN_ConnHdlr;
Harald Welte365f4ed2017-11-23 00:00:43 +0100490
491type function BssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200492runs on RAN_Emulation_CT return template PDU_BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +0100493
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200494private function append_osmux_ie()
495runs on RAN_Emulation_CT return boolean {
496 return g_ran_ops.use_osmux and (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
497}
498
Harald Welte17d21152018-01-27 00:47:11 +0100499/* handle common Unitdata such as Paging */
500private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200501runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte17d21152018-01-27 00:47:11 +0100502 if (match(bssap, tr_BSSMAP_Paging)) {
Harald Welte6811d102019-04-14 22:23:14 +0200503 var RAN_ConnHdlr client := null;
Harald Welte17d21152018-01-27 00:47:11 +0100504 client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits,
505 bssap.pdu.bssmap.paging.tMSI.tmsiOctets);
Daniel Willmannabc73e12019-04-15 17:25:56 +0200506 if (client != null) {
Harald Welte17d21152018-01-27 00:47:11 +0100507 log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ",
508 client);
509 CLIENT.send(bssap) to client;
510 return omit;
511 }
512 log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table");
513 } else {
514 log("CommonBssmapUnitdataCallback: Not a paging message");
515 }
516 /* ELSE: handle in user callback */
Harald Welte6811d102019-04-14 22:23:14 +0200517 return g_ran_ops.unitdata_cb.apply(bssap);
Harald Welte17d21152018-01-27 00:47:11 +0100518}
519
Harald Welte6811d102019-04-14 22:23:14 +0200520private function f_bssap_wait_for_reset() runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100521 var BSSAP_N_UNITDATA_ind ud_ind;
522 timer T := 20.0;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200523 var boolean append_osmux_support := append_osmux_ie();
Harald Weltea4ca4462018-02-09 00:17:14 +0100524
525 T.start;
526 alt {
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200527 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(append_osmux_support))) -> value ud_ind {
Harald Weltea4ca4462018-02-09 00:17:14 +0100528 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200529 ts_BSSMAP_ResetAck(append_osmux_support)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100530 }
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200531 [] as_reset_ack(append_osmux_support);
Harald Weltea4ca4462018-02-09 00:17:14 +0100532 [] BSSAP.receive {
533 repeat;
534 }
535 [] T.timeout {
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200536 setverdict(fail, "Timeout waiting for BSSAP RESET");
537 mtc.stop;
Harald Weltea4ca4462018-02-09 00:17:14 +0100538 }
539 }
540}
541
Harald Welte6811d102019-04-14 22:23:14 +0200542function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
Harald Weltea4ca4462018-02-09 00:17:14 +0100543 timer T := 5.0;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200544 var boolean append_osmux_support := append_osmux_ie();
Harald Weltea4ca4462018-02-09 00:17:14 +0100545
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200546 BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0, append_osmux_support)));
Harald Weltea4ca4462018-02-09 00:17:14 +0100547 T.start;
548 alt {
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200549 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck(append_osmux_support))) {
Harald Weltea4ca4462018-02-09 00:17:14 +0100550 log("Received RESET-ACK in response to RESET, we're ready to go!");
551 }
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200552 [] as_reset_ack(append_osmux_support);
Harald Weltea4ca4462018-02-09 00:17:14 +0100553 [] BSSAP.receive { repeat };
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200554 [] T.timeout {
555 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
556 mtc.stop;
557 }
Harald Weltea4ca4462018-02-09 00:17:14 +0100558 }
559}
560
Harald Welte2fce7882019-04-15 11:48:05 +0200561private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean {
562 var template octetstring l3 := f_bssap_extract_l3(bssap);
563 return f_L3_is_rr(l3);
564}
565#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100566
Harald Welte5b027622019-04-14 23:40:17 +0200567#ifdef RAN_EMULATION_RANAP
568type record RANAP_Conn_Req {
569 SCCP_PAR_Address addr_peer,
570 SCCP_PAR_Address addr_own,
571 RANAP_PDU ranap
572}
573template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, RANAP_PDU ranap) := {
574 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);
593 if (istemplatekind(l3, "omit")) {
594 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 */
682 return g_ran_ops.ranap_unitdata_cb.apply(ranap);
683}
684
685private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean {
686 var template (omit) SAPI sapi;
687 var template octetstring l3 := f_ranap_extract_l3(ranap);
688 return f_L3_is_rr(l3);
689}
690
691function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT {
692 timer T := 5.0;
693 var CN_DomainIndicator dom;
694 if (g_ran_ops.ps_domain) {
695 dom := ps_domain;
696 } else {
697 dom := cs_domain;
698 }
699
700 RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom)));
701 T.start;
702 alt {
703 [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) {
704 log("Received RESET-ACK in response to RESET, we're ready to go!");
705 }
706 [] as_reset_ack();
707 [] RANAP.receive { repeat };
708 [] T.timeout {
709 setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET");
710 mtc.stop;
711 }
712 }
713}
714#endif
Harald Welte365f4ed2017-11-23 00:00:43 +0100715
Harald Welte2fce7882019-04-15 11:48:05 +0200716
717type enumerated RanProtocol {
Harald Welte5b027622019-04-14 23:40:17 +0200718 RAN_PROTOCOL_BSSAP,
719 RAN_PROTOCOL_RANAP
Harald Welte2fce7882019-04-15 11:48:05 +0200720}
721
722type record RanOps {
723#ifdef RAN_EMULATION_BSSAP
724 BssmapCreateCallback create_cb optional,
725 BssmapUnitdataCallback unitdata_cb optional,
726#endif
Harald Welte5b027622019-04-14 23:40:17 +0200727#ifdef RAN_EMULATION_RANAP
728 RanapCreateCallback ranap_create_cb optional,
729 RanapUnitdataCallback ranap_unitdata_cb optional,
730 boolean ps_domain,
731#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200732 boolean decode_dtap,
733 boolean role_ms,
734 RanProtocol protocol,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200735 RAN_Transport transport,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200736 boolean use_osmux,
Harald Welte2fce7882019-04-15 11:48:05 +0200737 /* needed for performing BSSMAP RESET */
738 SCCP_PAR_Address sccp_addr_local optional,
739 SCCP_PAR_Address sccp_addr_peer optional
740}
741
742template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B);
743
744private function f_L3_is_rr(template octetstring l3) return boolean {
745 if (not isvalue(l3)) {
746 return false;
Harald Weltea4ca4462018-02-09 00:17:14 +0100747 }
Harald Welte2fce7882019-04-15 11:48:05 +0200748 var octetstring l3v := valueof(l3);
749 if (lengthof(l3v) < 1) {
750 return false;
751 }
752 /* lower 4 bits of first octet are protocol discriminator */
753 if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) {
754 return true;
755 }
756 return false;
757}
Harald Weltea4ca4462018-02-09 00:17:14 +0100758
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200759function 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 +0200760 var uint2_t seq_nr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200761 if (n_sd_idx == 0) {
762 seq_nr := n_sd[0];
763 n_sd[0] := (n_sd[0] + 1) mod 4;
764 } else if (n_sd_idx >= 1 and n_sd_idx <= 3) {
765 seq_nr := n_sd[n_sd_idx];
766 n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2;
Harald Welte2fce7882019-04-15 11:48:05 +0200767 } else {
768 /* no sequence number to patch */
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200769 seq_nr := 0;
Harald Welte2fce7882019-04-15 11:48:05 +0200770 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200771 return seq_nr;
772}
773
774/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
775function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) {
Harald Welte2fce7882019-04-15 11:48:05 +0200776 log("patching N(SD)=", seq_nr, " into dtap ", enc_l3);
777 enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6);
778 log("patched enc_l3: ", enc_l3);
779}
780
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200781function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer {
782 var uint2_t seq_nr;
783 if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) {
784 return 0;
785 } else if (ischosen(dtap.msgs.gcc)) {
786 return 1;
787 } else if (ischosen(dtap.msgs.bcc)) {
788 return 2;
789 } else if (ischosen(dtap.msgs.loc)) {
790 return 3;
791 }
792 /* no sequence number to patch */
793 return -1;
794}
795
796/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
797function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
798 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
799 if (n_sd_idx < 0) {
800 return;
801 }
802 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
803 f_ML3_patch_seq_nr(seq_nr, enc_l3);
804}
805
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200806private altstep as_reset_ack(boolean append_osmux_support := false) runs on RAN_Emulation_CT {
Harald Welte2fce7882019-04-15 11:48:05 +0200807#ifdef RAN_EMULATION_BSSAP
808 var BSSAP_N_UNITDATA_ind ud_ind;
809#endif
Harald Welte5b027622019-04-14 23:40:17 +0200810#ifdef RAN_EMULATION_RANAP
811 var RANAP_N_UNITDATA_ind rud_ind;
812#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200813#ifdef RAN_EMULATION_BSSAP
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200814 [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset(append_osmux_support))) -> value ud_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200815 log("Respoding to inbound RESET with RESET-ACK");
816 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200817 ts_BSSMAP_ResetAck(append_osmux_support)));
Harald Welte2fce7882019-04-15 11:48:05 +0200818 repeat;
819 }
820#endif
Harald Welte5b027622019-04-14 23:40:17 +0200821#ifdef RAN_EMULATION_RANAP
822 [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind {
823 log("Respoding to inbound IuRESET with IuRESET-ACK");
824 var CN_DomainIndicator dom;
825 dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator;
826 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress,
827 ts_RANAP_ResetAck(dom)));
828 }
829#endif
Harald Welte2fce7882019-04-15 11:48:05 +0200830}
831
832
833private altstep as_main_bssap() runs on RAN_Emulation_CT {
834#ifdef RAN_EMULATION_BSSAP
Harald Welte004f5fb2017-12-16 17:54:40 +0100835 var BSSAP_N_UNITDATA_ind ud_ind;
836 var BSSAP_N_CONNECT_ind conn_ind;
837 var BSSAP_N_CONNECT_cfm conn_cfm;
838 var BSSAP_N_DATA_ind data_ind;
839 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100840 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100841 var PDU_BSSAP bssap;
Harald Welte2fce7882019-04-15 11:48:05 +0200842 var RAN_ConnHdlr vc_conn;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200843 var integer targetPointCode;
844 var N_Sd_Array last_n_sd;
Harald Welte365f4ed2017-11-23 00:00:43 +0100845
Harald Welte365f4ed2017-11-23 00:00:43 +0100846 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100847 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100848 /* Connectionless Procedures like RESET */
849 var template PDU_BSSAP resp;
Harald Welte17d21152018-01-27 00:47:11 +0100850 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100851 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100852 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
853 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100854 }
855 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100856 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100857 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Welte2fce7882019-04-15 11:48:05 +0200858 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100859 /* store mapping between client components and SCCP connectionId */
860 f_conn_table_add(vc_conn, conn_ind.connectionId);
861 /* handle user payload */
862 f_handle_userData(vc_conn, conn_ind.userData);
863 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100864 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100865 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100866 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100867 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100868 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100869 if (ispresent(data_ind.userData)) {
870 f_handle_userData(vc_conn, data_ind.userData);
871 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100872 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100873 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100874 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100875 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100876 if (ispresent(disc_ind.userData)) {
877 f_handle_userData(vc_conn, disc_ind.userData);
878 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100879 /* notify client about termination */
Harald Welte6811d102019-04-14 22:23:14 +0200880 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
Harald Welte365f4ed2017-11-23 00:00:43 +0100881 CLIENT.send(prim) to vc_conn;
882 f_conn_table_del(disc_ind.connectionId);
883 /* TOOD: return confirm to other side? */
884 }
Harald Welteb3414b22017-11-23 18:22:10 +0100885 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100886 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welte71b69332018-01-21 20:43:53 +0100887 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
Harald Welte6811d102019-04-14 22:23:14 +0200888 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
Harald Welte71b69332018-01-21 20:43:53 +0100889 CLIENT.send(prim) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100890 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100891 if (ispresent(conn_cfm.userData)) {
892 f_handle_userData(vc_conn, conn_cfm.userData);
893 }
Harald Welteb3414b22017-11-23 18:22:10 +0100894 }
Harald Welte2fce7882019-04-15 11:48:05 +0200895 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
896 var integer conn_id := f_conn_id_by_comp(vc_conn);
897 /* send it to dispatcher */
898 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
899 }
Harald Welteb3414b22017-11-23 18:22:10 +0100900
Harald Welte365f4ed2017-11-23 00:00:43 +0100901 /* Disconnect request client -> SCCP */
Harald Welte6811d102019-04-14 22:23:14 +0200902 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
Harald Welte365f4ed2017-11-23 00:00:43 +0100903 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100904 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100905 f_conn_table_del(conn_id);
906 }
907
908 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100909 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
910 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100911 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100912
913 if (f_comp_known(vc_conn) == false) {
914 /* unknown client, create new connection */
915 conn_id := f_gen_conn_id();
916
917 /* store mapping between client components and SCCP connectionId */
918 f_conn_table_add(vc_conn, conn_id);
919
Harald Welte004f5fb2017-12-16 17:54:40 +0100920 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
921 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100922 } else {
923 /* known client, send via existing connection */
924 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100925 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100926 }
927
Harald Welte49518bf2018-02-10 11:39:19 +0100928 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
929 * counter only on MM/CC/SS, but not on RR */
Harald Welte6811d102019-04-14 22:23:14 +0200930 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
Harald Welte9dadc522018-02-06 13:56:04 +0100931 /* we have just sent the first MM message, increment the counter */
932 var integer idx := f_idx_by_comp(vc_conn);
933 ConnectionTable[idx].n_sd[0] := 1;
934 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
935 }
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200936 }
Harald Welte9dadc522018-02-06 13:56:04 +0100937
Neels Hofmeyr0ac63152019-05-07 01:20:17 +0200938 [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) {
939 var integer idx := f_idx_by_comp(vc_conn);
940 last_n_sd := ConnectionTable[idx].n_sd;
941 PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
942 }
943
944 [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
945 var integer idx := f_idx_by_comp(vc_conn);
946 ConnectionTable[idx].n_sd := last_n_sd;
947 PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
Harald Welteb3414b22017-11-23 18:22:10 +0100948 }
Harald Welte2fce7882019-04-15 11:48:05 +0200949#else
Harald Welte5b027622019-04-14 23:40:17 +0200950 [false] CLIENT.receive {}
951#endif
952}
953
954private altstep as_main_ranap() runs on RAN_Emulation_CT {
955#ifdef RAN_EMULATION_RANAP
956 var RANAP_N_UNITDATA_ind rud_ind;
957 var RANAP_N_CONNECT_ind rconn_ind;
958 var RANAP_N_CONNECT_cfm rconn_cfm;
959 var RANAP_N_DATA_ind rdata_ind;
960 var RANAP_N_DISCONNECT_ind rdisc_ind;
961 var RANAP_Conn_Req creq;
962 var RANAP_PDU ranap;
963 var RAN_ConnHdlr vc_conn;
Harald Welte717c7302019-04-23 20:31:55 +0200964 var PDU_DTAP_PS_MO ps_mo;
965 var PDU_DTAP_PS_MT ps_mt;
Harald Welte5b027622019-04-14 23:40:17 +0200966
967 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
968 [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind {
969 /* Connectionless Procedures like RESET */
970 var template RANAP_PDU resp;
971 resp := CommonRanapUnitdataCallback(rud_ind.userData);
972 if (isvalue(resp)) {
973 RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress,
974 rud_ind.calledAddress, resp));
975 }
976 }
977 /* SCCP -> Client: new connection from BSC */
978 [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind {
979 vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id);
980 /* store mapping between client components and SCCP connectionId */
981 f_conn_table_add(vc_conn, rconn_ind.connectionId);
982 /* handle user payload */
983 f_handle_userData_RANAP(vc_conn, rconn_ind.userData);
984 /* confirm connection establishment */
985 RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit));
986 }
987 /* SCCP -> Client: connection-oriented data in existing connection */
988 [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind {
989 vc_conn := f_comp_by_conn_id(rdata_ind.connectionId);
990 if (ispresent(rdata_ind.userData)) {
991 f_handle_userData_RANAP(vc_conn, rdata_ind.userData);
992 }
993 }
994 /* SCCP -> Client: disconnect of an existing connection */
995 [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind {
996 vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId);
997 if (ispresent(rdisc_ind.userData)) {
998 f_handle_userData_RANAP(vc_conn, rdisc_ind.userData);
999 }
1000 /* notify client about termination */
1001 var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
1002 CLIENT.send(prim) to vc_conn;
1003 f_conn_table_del(rdisc_ind.connectionId);
1004 /* TOOD: return confirm to other side? */
1005 }
1006 /* SCCP -> Client: connection confirm for outbound connection */
1007 [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm {
1008 vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId);
1009 var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND;
1010 CLIENT.send(prim) to vc_conn;
1011 /* handle user payload */
1012 if (ispresent(rconn_cfm.userData)) {
1013 f_handle_userData_RANAP(vc_conn, rconn_cfm.userData);
1014 }
1015 }
1016
1017 [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn {
1018 var integer conn_id := f_conn_id_by_comp(vc_conn);
1019 /* send it to dispatcher */
1020 RANAP.send(ts_RANAP_DATA_req(conn_id, ranap));
1021 }
1022
1023 /* Disconnect request client -> SCCP */
1024 [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
1025 var integer conn_id := f_conn_id_by_comp(vc_conn);
1026 RANAP.send(ts_RANAP_DISC_req(conn_id, 0));
1027 f_conn_table_del(conn_id);
1028 }
1029
1030 /* BSSAP from client -> SCCP */
1031 [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn {
1032 var integer conn_id;
1033 /* send to dispatcher */
1034
1035 if (f_comp_known(vc_conn) == false) {
1036 /* unknown client, create new connection */
1037 conn_id := f_gen_conn_id();
1038
1039 /* store mapping between client components and SCCP connectionId */
1040 f_conn_table_add(vc_conn, conn_id);
1041
1042 RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
1043 creq.ranap));
1044 } else {
1045 /* known client, send via existing connection */
1046 conn_id := f_conn_id_by_comp(vc_conn);
1047 RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap));
1048 }
1049
1050 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
1051 * counter only on MM/CC/SS, but not on RR */
1052 if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) {
1053 /* we have just sent the first MM message, increment the counter */
1054 var integer idx := f_idx_by_comp(vc_conn);
1055 ConnectionTable[idx].n_sd[0] := 1;
1056 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
1057 }
1058 }
1059
Harald Welte717c7302019-04-23 20:31:55 +02001060 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MO:?) -> value ps_mo sender vc_conn {
1061 var integer idx := f_idx_by_comp(vc_conn);
1062 /* convert from decoded DTAP to encoded DTAP */
1063 var octetstring l3_enc := enc_PDU_L3_MS_SGSN(ps_mo.dtap);
1064 /* patch correct L3 send sequence number N(SD) into l3_enc */
1065 if (ps_mo.skip_seq_patching == false) {
1066 //f_ML3_patch_seq(ConnectionTable[idx], ps_mo.dtap, l3_enc);
1067 }
1068 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mo.dlci, l3_enc);
1069 }
1070
1071 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_PS_MT:?) -> value ps_mt sender vc_conn {
1072 var integer idx := f_idx_by_comp(vc_conn);
1073 /* convert from decoded DTAP to encoded DTAP */
1074 var octetstring l3_enc := enc_PDU_L3_SGSN_MS(ps_mt.dtap);
1075 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, ps_mt.dlci, l3_enc);
1076 }
1077
1078
Harald Welte5b027622019-04-14 23:40:17 +02001079#else
1080 [false] CLIENT.receive {}
Harald Welte2fce7882019-04-15 11:48:05 +02001081#endif
1082}
Harald Welteb3414b22017-11-23 18:22:10 +01001083
Harald Welte2fce7882019-04-15 11:48:05 +02001084private altstep as_main_mgcp() runs on RAN_Emulation_CT {
1085#ifdef RAN_EMULATION_MGCP
1086 var MgcpCommand mgcp_req;
1087 var MgcpResponse mgcp_resp;
1088 var RAN_ConnHdlr vc_conn;
Harald Welte0b476062018-01-21 19:07:32 +01001089
Harald Weltec82eef42017-11-24 20:40:12 +01001090 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
1091 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
1092 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
1093 * is printed as hex string, e.g. a@mgw for CIC 10 */
1094
1095 /* CLIENT -> MGCP */
1096 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
1097 /* MGCP request from Handler (we're MSC) */
1098 /* store the transaction ID we've seen */
1099 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
1100 /* simply forward any MGCP from the client to the port */
1101 MGCP.send(mgcp_req);
1102 }
1103 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
1104 /* MGCP response from Handler (we're BSC/MGW) */
1105 /* simply forward any MGCP from the client to the port */
1106 MGCP.send(mgcp_resp);
1107 }
1108
1109 /* MGCP -> CLIENT */
1110 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
1111 /* MGCP request from network side (we're BSC/MGW) */
1112 /* Extract CIC from local part of endpoint name */
1113 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +01001114 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
1115 /* ignore RSIP for unknown CIC */
1116 } else {
1117 /* Resolve the vc_conn by the CIC */
1118 vc_conn := f_comp_by_cic(cic);
1119 CLIENT.send(mgcp_req) to vc_conn;
1120 }
Harald Weltec82eef42017-11-24 20:40:12 +01001121 }
1122 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
1123 /* MGCP response from network side (we're MSC) */
1124 /* Resolve the vc_conn by the transaction ID */
1125 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
1126 CLIENT.send(mgcp_resp) to vc_conn;
1127 }
Harald Welte2fce7882019-04-15 11:48:05 +02001128#else
1129 [false] CLIENT.receive {}
1130#endif
1131}
Harald Weltec82eef42017-11-24 20:40:12 +01001132
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001133private altstep as_main_ctrl() runs on RAN_Emulation_CT {
1134#ifdef RAN_EMULATION_CTRL
1135 var CtrlMessage ctrl;
1136 var RAN_ConnHdlr vc_conn;
1137 var ASP_IPA_Event evt;
1138
1139 /* Handling of CTRL in IPA SCCPLite case. This predates 3GPP AoIP
1140 * and uses a CTRL session in parallel to BSSAP. */
1141
1142 /* CTRL_CLIENT -> CTRL */
1143 [] CTRL_CLIENT.receive(CtrlMessage:?) -> value ctrl sender vc_conn {
1144 CTRL.send(ctrl);
1145 }
1146
1147 /* CTRL -> CTRL_CLIENT */
1148 [] CTRL.receive(CtrlMessage:?) -> value ctrl {
1149 CTRL_CLIENT.send(ctrl);
1150 }
1151
1152 [] CTRL.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_UP}) -> value evt {
1153 CTRL_CLIENT.send(evt);
1154 }
1155 [] CTRL.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_DOWN}) {
1156 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Lost IPA connection!");
1157 }
1158 [] CTRL.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_ID_ACK}) {}
1159#else
1160 [false] CLIENT.receive {}
1161#endif
1162}
1163
Harald Welte2fce7882019-04-15 11:48:05 +02001164/* send a raw (encoded) L3 message over given SCCP connection */
1165private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT
1166{
1167 select (g_ran_ops.protocol) {
1168#ifdef RAN_EMULATION_BSSAP
1169 case (RAN_PROTOCOL_BSSAP) {
1170 var PDU_BSSAP bssap;
1171 bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci));
1172 BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap));
1173 }
1174#endif
Harald Welte5b027622019-04-14 23:40:17 +02001175#ifdef RAN_EMULATION_RANAP
1176 case (RAN_PROTOCOL_RANAP) {
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001177 var template (omit) RANAP_IEs.SAPI sapi := omit;
Harald Welte5b027622019-04-14 23:40:17 +02001178 var RANAP_PDU ranap;
Vadim Yanitskiyf2c3fa92019-06-20 05:18:15 +07001179
1180 /* Perform DLCI -> SAPI transformation (x & 0x07) */
1181 if (dlci and4b '07'O == '03'O) {
1182 sapi := sapi_3;
1183 }
1184
1185 ranap := valueof(ts_RANAP_DirectTransfer(l3_enc, sapi := sapi));
Harald Welte5b027622019-04-14 23:40:17 +02001186 RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap));
1187 }
1188#endif
Harald Welte2fce7882019-04-15 11:48:05 +02001189 }
1190}
1191
1192function main(RanOps ops, charstring id) runs on RAN_Emulation_CT {
1193
1194 g_ran_id := id;
1195 g_ran_ops := ops;
1196 f_conn_table_init();
1197 f_expect_table_init();
1198
1199 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
1200 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
Harald Welte5b027622019-04-14 23:40:17 +02001201 select (g_ran_ops.protocol) {
1202#ifdef RAN_EMULATION_BSSAP
1203 case (RAN_PROTOCOL_BSSAP) {
1204 f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1205 }
1206#endif
1207#ifdef RAN_EMULATION_RANAP
1208 case (RAN_PROTOCOL_RANAP) {
1209 f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
1210 }
1211#endif
1212 }
Harald Welte2fce7882019-04-15 11:48:05 +02001213 }
1214
1215 while (true) {
1216 var RAN_ConnHdlr vc_conn;
1217 var PDU_DTAP_MO dtap_mo;
1218 var PDU_DTAP_MT dtap_mt;
1219 var RAN_ConnHdlr vc_hdlr;
1220 var octetstring l3_info;
1221 var hexstring imsi;
1222 var OCT4 tmsi;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001223 var integer targetPointCode;
Harald Welte2fce7882019-04-15 11:48:05 +02001224
1225 alt {
1226 [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap();
Harald Welte5b027622019-04-14 23:40:17 +02001227 [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap();
Harald Welte2fce7882019-04-15 11:48:05 +02001228
1229 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
1230 var integer idx := f_idx_by_comp(vc_conn);
1231 /* convert from decoded DTAP to encoded DTAP */
1232 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
1233 /* patch correct L3 send sequence number N(SD) into l3_enc */
1234 if (dtap_mo.skip_seq_patching == false) {
1235 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
1236 }
1237 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
1238 }
1239
1240 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
1241 var integer idx := f_idx_by_comp(vc_conn);
1242 /* convert from decoded DTAP to encoded DTAP */
1243 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
1244 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
1245 }
1246
1247 [] as_main_mgcp();
Pau Espin Pedrol4d0e5e52019-06-07 19:38:28 +02001248 [] as_main_ctrl();
Harald Welte624f9632017-12-16 19:26:04 +01001249
Harald Welte6811d102019-04-14 22:23:14 +02001250 [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) {
Harald Welte624f9632017-12-16 19:26:04 +01001251 f_create_expect(l3_info, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001252 PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr;
Harald Welte624f9632017-12-16 19:26:04 +01001253 }
1254
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001255 [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
1256 f_create_expect(omit, vc_hdlr, targetPointCode);
1257 PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
1258 }
1259
Harald Welte6811d102019-04-14 22:23:14 +02001260 [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
Harald Welte17d21152018-01-27 00:47:11 +01001261 f_create_imsi(imsi, tmsi, vc_hdlr);
Harald Welte6811d102019-04-14 22:23:14 +02001262 PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
Harald Welte17d21152018-01-27 00:47:11 +01001263 }
1264
1265
Harald Welte365f4ed2017-11-23 00:00:43 +01001266 }
1267 }
1268}
1269
Harald Welte2fce7882019-04-15 11:48:05 +02001270#ifdef RAN_EMULATION_MGCP
Harald Weltec82eef42017-11-24 20:40:12 +01001271private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +01001272 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +01001273 return hex2int(str2hex(local_part));
1274
1275}
Harald Welte2fce7882019-04-15 11:48:05 +02001276#endif
Harald Welte365f4ed2017-11-23 00:00:43 +01001277
Harald Welte624f9632017-12-16 19:26:04 +01001278/***********************************************************************
1279 * "Expect" Handling (mapping for expected incoming SCCP connections)
1280 ***********************************************************************/
1281
1282/* data about an expected future incoming connection */
1283type record ExpectData {
1284 /* L3 payload based on which we can match it */
1285 octetstring l3_payload optional,
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001286 integer handoverRequestPointCode optional,
Harald Welte624f9632017-12-16 19:26:04 +01001287 /* component reference for this connection */
Harald Welte6811d102019-04-14 22:23:14 +02001288 RAN_ConnHdlr vc_conn
Harald Welte624f9632017-12-16 19:26:04 +01001289}
1290
1291/* procedure based port to register for incoming connections */
Harald Welte6811d102019-04-14 22:23:14 +02001292signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr);
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001293signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr);
Harald Welte624f9632017-12-16 19:26:04 +01001294
Harald Welte17d21152018-01-27 00:47:11 +01001295/* procedure based port to register for incoming IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001296signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr);
Harald Welte17d21152018-01-27 00:47:11 +01001297
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001298/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
1299signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
1300
1301/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
1302signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr);
1303
Harald Welte6811d102019-04-14 22:23:14 +02001304type port RAN_PROC_PT procedure {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001305 inout RAN_register, RAN_register_imsi, RAN_register_handoverRequest, RAN_last_n_sd, RAN_continue_after_n_sd;
Harald Welte624f9632017-12-16 19:26:04 +01001306} with { extension "internal" };
1307
Harald Welte5b027622019-04-14 23:40:17 +02001308#ifdef RAN_EMULATION_BSSAP
Harald Welte624f9632017-12-16 19:26:04 +01001309/* CreateCallback that can be used as create_cb and will use the expectation table */
1310function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte6811d102019-04-14 22:23:14 +02001311runs on RAN_Emulation_CT return RAN_ConnHdlr {
1312 var RAN_ConnHdlr ret := null;
Harald Welte624f9632017-12-16 19:26:04 +01001313 var octetstring l3_info;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001314 var boolean handoverRequest := false;
1315 var integer handoverRequestPointCode;
Harald Welte624f9632017-12-16 19:26:04 +01001316 var integer i;
1317
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001318 if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
1319 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
1320 log("ExpectedCreateCallback completeLayer3Information");
1321 } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) {
1322 handoverRequest := true;
1323 handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode);
1324 log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode);
1325 } else {
1326 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request");
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001327 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001328 return ret;
1329 }
Harald Welte624f9632017-12-16 19:26:04 +01001330
1331 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001332 if (handoverRequest) {
1333 log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode,
1334 " ==? ", handoverRequestPointCode);
1335 if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) {
1336 ret := ExpectTable[i].vc_conn;
1337 log("Found Expect[", i, "] for handoverRequest handled at ", ret);
1338 return ret;
1339 } else {
1340 continue;
1341 }
1342 }
Harald Welte624f9632017-12-16 19:26:04 +01001343 if (not ispresent(ExpectTable[i].l3_payload)) {
1344 continue;
1345 }
1346 if (l3_info == ExpectTable[i].l3_payload) {
1347 ret := ExpectTable[i].vc_conn;
1348 /* release this entry to be used again */
1349 ExpectTable[i].l3_payload := omit;
1350 ExpectTable[i].vc_conn := null;
1351 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1352 /* return the component reference */
1353 return ret;
1354 }
1355 }
1356 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001357 mtc.stop;
Harald Welte624f9632017-12-16 19:26:04 +01001358 return ret;
1359}
Harald Welte5b027622019-04-14 23:40:17 +02001360#endif
1361
1362#ifdef RAN_EMULATION_RANAP
1363/* CreateCallback that can be used as create_cb and will use the expectation table */
1364function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id)
1365runs on RAN_Emulation_CT return RAN_ConnHdlr {
1366 var RAN_ConnHdlr ret := null;
1367 var template (omit) octetstring l3_info;
1368 var integer i;
1369
1370 l3_info := f_ranap_extract_l3(conn_ind.userData);
1371 if (istemplatekind(l3_info, "omit")) {
1372 setverdict(fail, "N-CONNECT.ind without NAS payload");
1373 mtc.stop;
1374 return ret;
1375 }
1376
1377 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
1378 if (not ispresent(ExpectTable[i].l3_payload)) {
1379 continue;
1380 }
1381 if (valueof(l3_info) == ExpectTable[i].l3_payload) {
1382 ret := ExpectTable[i].vc_conn;
1383 /* release this entry to be used again */
1384 ExpectTable[i].l3_payload := omit;
1385 ExpectTable[i].vc_conn := null;
1386 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
1387 /* return the component reference */
1388 return ret;
1389 }
1390 }
1391 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
1392 mtc.stop;
1393 return ret;
1394}
1395#endif
Harald Welte624f9632017-12-16 19:26:04 +01001396
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001397private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr,
1398 template integer handoverRequestPointCode := omit)
Harald Welte6811d102019-04-14 22:23:14 +02001399runs on RAN_Emulation_CT {
Harald Welte624f9632017-12-16 19:26:04 +01001400 var integer i;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001401 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
Harald Welte624f9632017-12-16 19:26:04 +01001402 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001403 if (not ispresent(ExpectTable[i].l3_payload)
1404 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
1405 if (ispresent(l3)) {
1406 ExpectTable[i].l3_payload := valueof(l3);
1407 }
1408 if (ispresent(handoverRequestPointCode)) {
1409 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
1410 }
Harald Welte624f9632017-12-16 19:26:04 +01001411 ExpectTable[i].vc_conn := hdlr;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001412 if (ispresent(handoverRequestPointCode)) {
1413 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
1414 } else {
1415 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
1416 }
Harald Welte624f9632017-12-16 19:26:04 +01001417 return;
1418 }
1419 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001420 testcase.stop("No space left in ExpectTable");
Harald Welte624f9632017-12-16 19:26:04 +01001421}
1422
Harald Welte6811d102019-04-14 22:23:14 +02001423private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr)
1424runs on RAN_Emulation_CT {
Harald Welte17d21152018-01-27 00:47:11 +01001425 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
1426 if (not ispresent(ImsiTable[i].imsi)) {
1427 ImsiTable[i].imsi := imsi;
1428 ImsiTable[i].tmsi := tmsi;
1429 ImsiTable[i].comp_ref := hdlr;
1430 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
1431 return;
1432 }
1433 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +02001434 testcase.stop("No space left in ImsiTable");
Harald Welte17d21152018-01-27 00:47:11 +01001435}
1436
1437
Daniel Willmannd47106b2018-01-17 12:20:56 +01001438private function f_expect_table_init()
Harald Welte6811d102019-04-14 22:23:14 +02001439runs on RAN_Emulation_CT {
Daniel Willmannd47106b2018-01-17 12:20:56 +01001440 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
1441 ExpectTable[i].l3_payload := omit;
Neels Hofmeyr0ac63152019-05-07 01:20:17 +02001442 ExpectTable[i].handoverRequestPointCode := omit;
Daniel Willmannd47106b2018-01-17 12:20:56 +01001443 }
1444}
Harald Welte624f9632017-12-16 19:26:04 +01001445
Harald Welte17d21152018-01-27 00:47:11 +01001446/* helper function for clients to register their IMSI/TMSI */
Harald Welte6811d102019-04-14 22:23:14 +02001447function f_ran_register_imsi(hexstring imsi, OCT4 tmsi)
1448runs on RAN_ConnHdlr {
1449 BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) {
1450 [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {};
Harald Welte17d21152018-01-27 00:47:11 +01001451 }
1452}
1453
Harald Welte475a2c12019-05-02 19:05:48 +02001454#ifdef RAN_EMULATION_RANAP
1455/* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */
1456altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr {
1457 var RANAP_PDU ranap;
1458 [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) {
1459 BSSAP.send(ts_RANAP_IuReleaseComplete);
1460 alt {
1461 [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
1462 setverdict(pass);
1463 }
1464 [] BSSAP.receive {
1465 setverdict(fail, "Unexpected RANAP while waiting for SCCP Release ");
1466 mtc.stop;
1467 }
1468 }
1469 }
1470 [] BSSAP.receive(RANAP_PDU:?) -> value ranap{
1471 setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap);
1472 mtc.stop;
1473 }
1474}
1475#endif
1476
Harald Welte17d21152018-01-27 00:47:11 +01001477
Harald Welte365f4ed2017-11-23 00:00:43 +01001478}