Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1 | module RAN_Emulation { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 2 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 3 | /* RAN Emulation, runs on top of BSSAP_CodecPort. It multiplexes/demultiplexes |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 4 | * the individual connections, so there can be separate TTCN-3 components handling |
| 5 | * each of the connections. |
| 6 | * |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 7 | * The RAN_Emulation.main() function processes SCCP primitives from the SCCP |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 8 | * 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 Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 11 | * to the component running the RAN_Emulation.main() function. |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 12 | * |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 13 | * For each new inbound connections, the RanOps.create_cb() is called. It can create |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 14 | * 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 Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 22 | * Inbound Unit Data messages (such as are dispatched to the RanOps.unitdata_cb() callback, |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 23 | * which is registered with an argument to the main() function below. |
| 24 | * |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 25 | * (C) 2017-2019 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 26 | * 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 Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 33 | import from General_Types all; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 34 | import from Osmocom_Types all; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 35 | import from SCCP_Emulation all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 36 | import from SCCPasp_Types all; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 37 | import from IPA_Emulation all; |
| 38 | import from MobileL3_Types all; |
| 39 | |
| 40 | #ifdef RAN_EMULATION_BSSAP |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 41 | import from BSSAP_Types all; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 42 | import from BSSAP_CodecPort all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 43 | import from BSSMAP_Templates all; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 47 | import from MGCP_Types all; |
| 48 | import from MGCP_Templates all; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 49 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 50 | |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 51 | #ifdef RAN_EMULATION_RANAP |
| 52 | import from RANAP_CodecPort all; |
| 53 | import from RANAP_PDU_Descriptions all; |
| 54 | import from RANAP_Constants all; |
| 55 | import from RANAP_IEs all; |
| 56 | import from RANAP_Templates all; |
| 57 | #endif |
| 58 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 59 | /* General "base class" component definition, of which specific implementations |
| 60 | * derive themselves by means of the "extends" feature */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 61 | type component RAN_ConnHdlr { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 62 | /* port towards MSC Emulator core / SCCP connection dispatchar */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 63 | port RAN_Conn_PT BSSAP; |
Harald Welte | ca51998 | 2018-01-21 19:28:26 +0100 | [diff] [blame] | 64 | /* procedure based port to register for incoming connections */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 65 | port RAN_PROC_PT BSSAP_PROC; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 69 | type enumerated RAN_Conn_Prim { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 70 | /* SCCP tell us that connection was released */ |
| 71 | MSC_CONN_PRIM_DISC_IND, |
| 72 | /* we tell SCCP to release connection */ |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 73 | MSC_CONN_PRIM_DISC_REQ, |
| 74 | /* Connection confirmed indication */ |
| 75 | MSC_CONN_PRIM_CONF_IND |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 78 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 79 | type record PDU_DTAP_MO { |
| 80 | OCT1 dlci optional, |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 81 | boolean skip_seq_patching optional, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 82 | PDU_ML3_MS_NW dtap |
| 83 | } |
| 84 | |
| 85 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 86 | type record PDU_DTAP_MT { |
| 87 | OCT1 dlci optional, |
| 88 | PDU_ML3_NW_MS dtap |
| 89 | } |
| 90 | |
| 91 | template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := { |
| 92 | dlci := dlci, |
| 93 | dtap := dtap |
| 94 | } |
| 95 | |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 96 | template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O, boolean skip_seq_patching := false) := { |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 97 | dlci := dlci, |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 98 | skip_seq_patching := skip_seq_patching, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 99 | dtap := dtap |
| 100 | } |
| 101 | |
| 102 | template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := { |
| 103 | dlci := dlci, |
| 104 | dtap := dtap |
| 105 | } |
| 106 | |
| 107 | template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := { |
| 108 | dlci := dlci, |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 109 | skip_seq_patching := ?, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 110 | dtap := dtap |
| 111 | } |
| 112 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 113 | /* port between individual per-connection components and this dispatcher */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 114 | type port RAN_Conn_PT message { |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 115 | inout |
| 116 | #ifdef RAN_EMULATION_BSSAP |
| 117 | PDU_BSSAP, |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 118 | /* Client requests us to create SCCP Connection */ |
| 119 | BSSAP_Conn_Req, |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 120 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 121 | #ifdef RAN_EMULATION_RANAP |
| 122 | RANAP_PDU, |
| 123 | /* Client requests us to create SCCP Connection */ |
| 124 | RANAP_Conn_Req, |
| 125 | #endif |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 126 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 127 | /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */ |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 128 | MgcpCommand, MgcpResponse, |
| 129 | #endif |
| 130 | /* direct DTAP messages from/to clients */ |
| 131 | PDU_DTAP_MO, PDU_DTAP_MT, |
| 132 | /* misc indications / requests between SCCP and client */ |
| 133 | RAN_Conn_Prim; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 134 | } with { extension "internal" }; |
| 135 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 136 | type uint2_t N_Sd_Array[4]; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 137 | |
| 138 | /* represents a single BSSAP connection over SCCP */ |
| 139 | type record ConnectionData { |
| 140 | /* reference to the instance of the per-connection component */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 141 | RAN_ConnHdlr comp_ref, |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 142 | integer sccp_conn_id, |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 143 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 144 | /* most recent MGCP transaction ID (Used on MSC side) */ |
| 145 | MgcpTransId mgcp_trans_id optional, |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 146 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 147 | /* CIC that has been used for voice of this channel (BSC side) */ |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 148 | integer cic optional, |
| 149 | /* array of N(SD) values for MO DTAP messages, indexed by discriminator */ |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 150 | N_Sd_Array n_sd |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 153 | type record ImsiMapping { |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 154 | RAN_ConnHdlr comp_ref, |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 155 | hexstring imsi optional, |
| 156 | OCT4 tmsi |
| 157 | } |
| 158 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 159 | type component RAN_Emulation_CT { |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 160 | /* SCCP ports on the bottom side, using ASP primitives */ |
| 161 | #ifdef RAN_EMULATION_BSSAP |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 162 | port BSSAP_CODEC_PT BSSAP; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 163 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 164 | #ifdef RAN_EMULATION_RANAP |
| 165 | port RANAP_CODEC_PT RANAP; |
| 166 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 167 | /* BSSAP port to the per-connection clients */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 168 | port RAN_Conn_PT CLIENT; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 169 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 170 | /* MGCP port */ |
| 171 | port IPA_MGCP_PT MGCP; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 172 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 173 | |
| 174 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 175 | var ConnectionData ConnectionTable[16]; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 176 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 177 | /* pending expected incoming connections */ |
| 178 | var ExpectData ExpectTable[8]; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 179 | |
| 180 | /* tables for mapping inbound unitdata (like paging) */ |
| 181 | var ImsiMapping ImsiTable[16]; |
| 182 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 183 | /* procedure based port to register for incoming connections */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 184 | port RAN_PROC_PT PROC; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 185 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 186 | var charstring g_ran_id; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 187 | var integer g_next_e1_ts := 1; |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 188 | var RanOps g_ran_ops; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 191 | private function f_conn_id_known(integer sccp_conn_id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 192 | runs on RAN_Emulation_CT return boolean { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 193 | var integer i; |
| 194 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 195 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){ |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 202 | private function f_comp_known(RAN_ConnHdlr client) |
| 203 | runs on RAN_Emulation_CT return boolean { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 204 | var integer i; |
| 205 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 206 | if (ConnectionTable[i].comp_ref == client) { |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | return false; |
| 211 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 212 | |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 213 | private function f_cic_known(integer cic) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 214 | runs on RAN_Emulation_CT return boolean { |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 215 | var integer i; |
| 216 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 217 | if (ConnectionTable[i].cic == cic) { |
| 218 | return true; |
| 219 | } |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 224 | /* resolve component reference by connection ID */ |
| 225 | private function f_comp_by_conn_id(integer sccp_conn_id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 226 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 227 | var integer i; |
| 228 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 229 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 230 | return ConnectionTable[i].comp_ref; |
| 231 | } |
| 232 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 233 | setverdict(fail, "RAN Connection table not found by SCCP Connection ID ", sccp_conn_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 234 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 237 | |
| 238 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 239 | /* resolve component reference by CIC */ |
| 240 | private function f_comp_by_mgcp_tid(MgcpTransId tid) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 241 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 242 | var integer i; |
| 243 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 244 | if (ConnectionTable[i].mgcp_trans_id == tid) { |
| 245 | return ConnectionTable[i].comp_ref; |
| 246 | } |
| 247 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 248 | setverdict(fail, "RAN Connection table not found by MGCP Transaction ID ", tid); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 249 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 252 | private function f_comp_store_mgcp_tid(RAN_ConnHdlr client, MgcpTransId tid) |
| 253 | runs on RAN_Emulation_CT { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 254 | var integer i; |
| 255 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 256 | if (ConnectionTable[i].comp_ref == client) { |
| 257 | ConnectionTable[i].mgcp_trans_id := tid; |
| 258 | return; |
| 259 | } |
| 260 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 261 | setverdict(fail, "RAN Connection table not found by component ", client); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 262 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 263 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 264 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 265 | |
| 266 | private function f_comp_by_cic(integer cic) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 267 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 268 | var integer i; |
| 269 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 270 | if (ConnectionTable[i].cic == cic) { |
| 271 | return ConnectionTable[i].comp_ref; |
| 272 | } |
| 273 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 274 | setverdict(fail, "RAN Connection table not found by CIC ", cic); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 275 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 278 | private function f_comp_store_cic(RAN_ConnHdlr client, integer cic) |
| 279 | runs on RAN_Emulation_CT { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 280 | var integer i; |
| 281 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 282 | if (ConnectionTable[i].comp_ref == client) { |
| 283 | ConnectionTable[i].cic := cic; |
| 284 | return; |
| 285 | } |
| 286 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 287 | setverdict(fail, "RAN Connection table not found by component ", client); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 288 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 291 | /* resolve connection ID by component reference */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 292 | private function f_conn_id_by_comp(RAN_ConnHdlr client) |
| 293 | runs on RAN_Emulation_CT return integer { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 294 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 295 | if (ConnectionTable[i].comp_ref == client) { |
| 296 | return ConnectionTable[i].sccp_conn_id; |
| 297 | } |
| 298 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 299 | setverdict(fail, "RAN Connection table not found by component ", client); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 300 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 303 | /* resolve ConnectionTable index component reference */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 304 | private function f_idx_by_comp(RAN_ConnHdlr client) |
| 305 | runs on RAN_Emulation_CT return integer { |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 306 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 307 | if (ConnectionTable[i].comp_ref == client) { |
| 308 | return i; |
| 309 | } |
| 310 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 311 | setverdict(fail, "RAN Connection table not found by component ", client); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 312 | mtc.stop; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 315 | private function f_gen_conn_id() |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 316 | runs on RAN_Emulation_CT return integer { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 317 | var integer conn_id; |
| 318 | |
| 319 | do { |
| 320 | conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId); |
| 321 | } while (f_conn_id_known(conn_id) == true); |
| 322 | |
| 323 | return conn_id; |
| 324 | } |
| 325 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 326 | private function f_conn_table_init() |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 327 | runs on RAN_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 328 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 329 | ConnectionTable[i].comp_ref := null; |
| 330 | ConnectionTable[i].sccp_conn_id := -1; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 331 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 332 | ConnectionTable[i].mgcp_trans_id := omit; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 333 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 334 | ConnectionTable[i].cic := omit; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 335 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 336 | } |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 337 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 338 | ImsiTable[i].comp_ref := null; |
| 339 | ImsiTable[i].imsi := omit; |
| 340 | ImsiTable[i].tmsi := 'FFFFFFFF'O; |
| 341 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 344 | private function f_conn_table_add(RAN_ConnHdlr comp_ref, integer sccp_conn_id) |
| 345 | runs on RAN_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 346 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 347 | if (ConnectionTable[i].sccp_conn_id == -1) { |
| 348 | ConnectionTable[i].comp_ref := comp_ref; |
| 349 | ConnectionTable[i].sccp_conn_id := sccp_conn_id; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 350 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 351 | log("Added conn table entry ", i, comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 355 | testcase.stop("RAN Connection table full!"); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | private function f_conn_table_del(integer sccp_conn_id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 359 | runs on RAN_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 360 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 361 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 362 | log("Deleted conn table entry ", i, |
| 363 | ConnectionTable[i].comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 364 | ConnectionTable[i].sccp_conn_id := -1; |
| 365 | ConnectionTable[i].comp_ref := null; |
Harald Welte | 0a4317a | 2017-11-25 00:32:46 +0100 | [diff] [blame] | 366 | return |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 367 | } |
| 368 | } |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 369 | setverdict(fail, "RAN Connection table attempt to delete non-existant ", sccp_conn_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 370 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 373 | private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 374 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 375 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 376 | if (ImsiTable[i].imsi == imsi or |
| 377 | isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) { |
| 378 | return ImsiTable[i].comp_ref; |
| 379 | } |
| 380 | } |
| 381 | return null; |
| 382 | } |
| 383 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 384 | #ifdef RAN_EMULATION_BSSAP |
| 385 | type record BSSAP_Conn_Req { |
| 386 | SCCP_PAR_Address addr_peer, |
| 387 | SCCP_PAR_Address addr_own, |
| 388 | PDU_BSSAP bssap |
| 389 | } |
| 390 | template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := { |
| 391 | addr_peer := peer, |
| 392 | addr_own := own, |
| 393 | bssap := bssap |
| 394 | }; |
| 395 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 396 | /* handle (optional) userData portion of various primitives and dispatch it to the client */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 397 | private function f_handle_userData(RAN_ConnHdlr client, PDU_BSSAP bssap) |
| 398 | runs on RAN_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 399 | /* decode + send decoded BSSAP to client */ |
Harald Welte | 1b2748e | 2017-11-24 23:40:16 +0100 | [diff] [blame] | 400 | |
Harald Welte | 473676b | 2018-01-27 20:38:01 +0100 | [diff] [blame] | 401 | if (ischosen(bssap.pdu.bssmap)) { |
| 402 | /* BSC Side: If this is an assignment command, store CIC */ |
| 403 | if (ischosen(bssap.pdu.bssmap.assignmentRequest) and |
| 404 | ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) { |
| 405 | var BSSMAP_IE_CircuitIdentityCode cic_ie := |
| 406 | bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode; |
| 407 | var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow); |
| 408 | f_comp_store_cic(client, cic); |
| 409 | } |
Harald Welte | 1b2748e | 2017-11-24 23:40:16 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 412 | if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) { |
| 413 | if (g_ran_ops.role_ms) { |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 414 | /* we are the MS, so any message to us must be MT */ |
| 415 | var PDU_DTAP_MT mt := { |
| 416 | dlci := bssap.dlci, |
| 417 | dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap) |
| 418 | }; |
| 419 | CLIENT.send(mt) to client; |
| 420 | } else { |
| 421 | /* we are the Network, so any message to us must be MO */ |
| 422 | var PDU_DTAP_MO mo := { |
| 423 | dlci := bssap.dlci, |
| 424 | dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap) |
| 425 | }; |
| 426 | CLIENT.send(mo) to client; |
| 427 | } |
| 428 | } else { |
| 429 | CLIENT.send(bssap) to client; |
| 430 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 434 | * arrives */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 435 | type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 436 | runs on RAN_Emulation_CT return RAN_ConnHdlr; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 437 | |
| 438 | type function BssmapUnitdataCallback(PDU_BSSAP bssap) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 439 | runs on RAN_Emulation_CT return template PDU_BSSAP; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 440 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 441 | /* handle common Unitdata such as Paging */ |
| 442 | private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 443 | runs on RAN_Emulation_CT return template PDU_BSSAP { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 444 | if (match(bssap, tr_BSSMAP_Paging)) { |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 445 | var RAN_ConnHdlr client := null; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 446 | client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits, |
| 447 | bssap.pdu.bssmap.paging.tMSI.tmsiOctets); |
Daniel Willmann | abc73e1 | 2019-04-15 17:25:56 +0200 | [diff] [blame] | 448 | if (client != null) { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 449 | log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ", |
| 450 | client); |
| 451 | CLIENT.send(bssap) to client; |
| 452 | return omit; |
| 453 | } |
| 454 | log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table"); |
| 455 | } else { |
| 456 | log("CommonBssmapUnitdataCallback: Not a paging message"); |
| 457 | } |
| 458 | /* ELSE: handle in user callback */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 459 | return g_ran_ops.unitdata_cb.apply(bssap); |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 462 | private function f_bssap_wait_for_reset() runs on RAN_Emulation_CT { |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 463 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 464 | timer T := 20.0; |
| 465 | |
| 466 | T.start; |
| 467 | alt { |
| 468 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind { |
| 469 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress, |
| 470 | ts_BSSMAP_ResetAck)); |
| 471 | } |
| 472 | [] as_reset_ack(); |
| 473 | [] BSSAP.receive { |
| 474 | repeat; |
| 475 | } |
| 476 | [] T.timeout { |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 477 | setverdict(fail, "Timeout waiting for BSSAP RESET"); |
| 478 | mtc.stop; |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 483 | function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT { |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 484 | timer T := 5.0; |
| 485 | |
| 486 | BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0))); |
| 487 | T.start; |
| 488 | alt { |
| 489 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck)) { |
| 490 | log("Received RESET-ACK in response to RESET, we're ready to go!"); |
| 491 | } |
| 492 | [] as_reset_ack(); |
| 493 | [] BSSAP.receive { repeat }; |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 494 | [] T.timeout { |
| 495 | setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET"); |
| 496 | mtc.stop; |
| 497 | } |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 501 | private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean { |
| 502 | var template octetstring l3 := f_bssap_extract_l3(bssap); |
| 503 | return f_L3_is_rr(l3); |
| 504 | } |
| 505 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 506 | |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 507 | #ifdef RAN_EMULATION_RANAP |
| 508 | type record RANAP_Conn_Req { |
| 509 | SCCP_PAR_Address addr_peer, |
| 510 | SCCP_PAR_Address addr_own, |
| 511 | RANAP_PDU ranap |
| 512 | } |
| 513 | template (value) RANAP_Conn_Req ts_RANAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, RANAP_PDU ranap) := { |
| 514 | addr_peer := peer, |
| 515 | addr_own := own, |
| 516 | ranap := ranap |
| 517 | }; |
| 518 | |
| 519 | private function fake_dlci_from_sapi(template (omit) SAPI sapi) return template (omit) OCT1 |
| 520 | { |
| 521 | if (istemplatekind(sapi, "omit")) { |
| 522 | return omit; |
| 523 | } else if (valueof(sapi) == sapi_3) { |
| 524 | return '03'O; |
| 525 | } |
| 526 | return '00'O; |
| 527 | } |
| 528 | |
| 529 | private function f_handle_userData_RANAP(RAN_ConnHdlr client, RANAP_PDU ranap) |
| 530 | runs on RAN_Emulation_CT { |
| 531 | /* decode + send decoded RANAP to client */ |
| 532 | var template (omit) octetstring l3 := f_ranap_extract_l3(ranap); |
| 533 | if (istemplatekind(l3, "omit")) { |
| 534 | CLIENT.send(ranap) to client; |
| 535 | } else { |
| 536 | var template (omit) SAPI sapi := f_ranap_extract_sapi(ranap); |
| 537 | var template (omit) OCT1 dlci := fake_dlci_from_sapi(sapi); |
| 538 | if (g_ran_ops.role_ms) { |
| 539 | /* we are the MS, so any message to us must be MT */ |
| 540 | var PDU_DTAP_MT mt := { |
| 541 | dlci := omit, |
| 542 | dtap := dec_PDU_ML3_NW_MS(valueof(l3)) |
| 543 | }; |
| 544 | if (isvalue(dlci)) { |
| 545 | mt.dlci := valueof(dlci) |
| 546 | } |
| 547 | CLIENT.send(mt) to client; |
| 548 | } else { |
| 549 | /* we are the Network, so any message to us must be MO */ |
| 550 | var PDU_DTAP_MO mo := { |
| 551 | dlci := omit, |
| 552 | dtap := dec_PDU_ML3_MS_NW(valueof(l3)) |
| 553 | }; |
| 554 | if (isvalue(dlci)) { |
| 555 | mo.dlci := valueof(dlci) |
| 556 | } |
| 557 | CLIENT.send(mo) to client; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 563 | * arrives */ |
| 564 | type function RanapCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id) |
| 565 | runs on RAN_Emulation_CT return RAN_ConnHdlr; |
| 566 | |
| 567 | type function RanapUnitdataCallback(RANAP_PDU ranap) |
| 568 | runs on RAN_Emulation_CT return template RANAP_PDU; |
| 569 | |
| 570 | private function CommonRanapUnitdataCallback(RANAP_PDU ranap) |
| 571 | runs on RAN_Emulation_CT return template RANAP_PDU { |
| 572 | if (match(ranap, tr_RANAP_Paging(?, ?))) { |
| 573 | var RAN_ConnHdlr client := null; |
| 574 | /* extract IMSI and (if present) TMSI */ |
| 575 | var IMSI imsi := ranap.initiatingMessage.value_.paging.protocolIEs[1].value_.permanentNAS_UE_ID.iMSI; |
| 576 | var template OCT4 tmsi := omit; |
| 577 | if (lengthof(ranap.initiatingMessage.value_.paging.protocolIEs) > 2 and |
| 578 | ranap.initiatingMessage.value_.paging.protocolIEs[2].id == id_TemporaryUE_ID) { |
| 579 | var TemporaryUE_ID ue_id; |
| 580 | ue_id := ranap.initiatingMessage.value_.paging.protocolIEs[2].value_.temporaryUE_ID; |
| 581 | if (ischosen(ue_id.tMSI)) { |
| 582 | tmsi := ue_id.tMSI; |
| 583 | } else { |
| 584 | tmsi := ue_id.p_TMSI; |
| 585 | } |
| 586 | } |
| 587 | client := f_imsi_table_find(oct2hex(imsi), tmsi); |
| 588 | if (isvalue(client)) { |
| 589 | log("CommonRanapUnitdataCallback: IMSI/TMSI found in table, dispatching to ", |
| 590 | client); |
| 591 | CLIENT.send(ranap) to client; |
| 592 | return omit; |
| 593 | } |
| 594 | log("CommonRanapUnitdataCallback: IMSI/TMSI not found in table"); |
| 595 | } else { |
| 596 | log("CommonRanapUnitdataCallback: Not a paging message"); |
| 597 | } |
| 598 | |
| 599 | /* ELSE: handle in user callback */ |
| 600 | return g_ran_ops.ranap_unitdata_cb.apply(ranap); |
| 601 | } |
| 602 | |
| 603 | private function f_ranap_l3_is_rr(RANAP_PDU ranap) return boolean { |
| 604 | var template (omit) SAPI sapi; |
| 605 | var template octetstring l3 := f_ranap_extract_l3(ranap); |
| 606 | return f_L3_is_rr(l3); |
| 607 | } |
| 608 | |
| 609 | function f_ranap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on RAN_Emulation_CT { |
| 610 | timer T := 5.0; |
| 611 | var CN_DomainIndicator dom; |
| 612 | if (g_ran_ops.ps_domain) { |
| 613 | dom := ps_domain; |
| 614 | } else { |
| 615 | dom := cs_domain; |
| 616 | } |
| 617 | |
| 618 | RANAP.send(ts_RANAP_UNITDATA_req(peer, own, ts_RANAP_Reset(ts_RanapCause_om_intervention, dom))); |
| 619 | T.start; |
| 620 | alt { |
| 621 | [] RANAP.receive(tr_RANAP_UNITDATA_ind(own, peer, tr_RANAP_ResetAck)) { |
| 622 | log("Received RESET-ACK in response to RESET, we're ready to go!"); |
| 623 | } |
| 624 | [] as_reset_ack(); |
| 625 | [] RANAP.receive { repeat }; |
| 626 | [] T.timeout { |
| 627 | setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET"); |
| 628 | mtc.stop; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 633 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 634 | |
| 635 | type enumerated RanProtocol { |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 636 | RAN_PROTOCOL_BSSAP, |
| 637 | RAN_PROTOCOL_RANAP |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | type record RanOps { |
| 641 | #ifdef RAN_EMULATION_BSSAP |
| 642 | BssmapCreateCallback create_cb optional, |
| 643 | BssmapUnitdataCallback unitdata_cb optional, |
| 644 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 645 | #ifdef RAN_EMULATION_RANAP |
| 646 | RanapCreateCallback ranap_create_cb optional, |
| 647 | RanapUnitdataCallback ranap_unitdata_cb optional, |
| 648 | boolean ps_domain, |
| 649 | #endif |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 650 | boolean decode_dtap, |
| 651 | boolean role_ms, |
| 652 | RanProtocol protocol, |
| 653 | /* needed for performing BSSMAP RESET */ |
| 654 | SCCP_PAR_Address sccp_addr_local optional, |
| 655 | SCCP_PAR_Address sccp_addr_peer optional |
| 656 | } |
| 657 | |
| 658 | template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B); |
| 659 | |
| 660 | private function f_L3_is_rr(template octetstring l3) return boolean { |
| 661 | if (not isvalue(l3)) { |
| 662 | return false; |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 663 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 664 | var octetstring l3v := valueof(l3); |
| 665 | if (lengthof(l3v) < 1) { |
| 666 | return false; |
| 667 | } |
| 668 | /* lower 4 bits of first octet are protocol discriminator */ |
| 669 | if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) { |
| 670 | return true; |
| 671 | } |
| 672 | return false; |
| 673 | } |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 674 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 675 | function f_next_n_sd(inout N_Sd_Array n_sd, in integer n_sd_idx) return uint2_t { |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 676 | var uint2_t seq_nr; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 677 | if (n_sd_idx == 0) { |
| 678 | seq_nr := n_sd[0]; |
| 679 | n_sd[0] := (n_sd[0] + 1) mod 4; |
| 680 | } else if (n_sd_idx >= 1 and n_sd_idx <= 3) { |
| 681 | seq_nr := n_sd[n_sd_idx]; |
| 682 | n_sd[n_sd_idx] := (n_sd[n_sd_idx] + 1) mod 2; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 683 | } else { |
| 684 | /* no sequence number to patch */ |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 685 | seq_nr := 0; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 686 | } |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 687 | return seq_nr; |
| 688 | } |
| 689 | |
| 690 | /* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */ |
| 691 | function f_ML3_patch_seq_nr(in uint2_t seq_nr, inout octetstring enc_l3) { |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 692 | log("patching N(SD)=", seq_nr, " into dtap ", enc_l3); |
| 693 | enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6); |
| 694 | log("patched enc_l3: ", enc_l3); |
| 695 | } |
| 696 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 697 | function f_ML3_n_sd_idx(in PDU_ML3_MS_NW dtap) return integer { |
| 698 | var uint2_t seq_nr; |
| 699 | if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) { |
| 700 | return 0; |
| 701 | } else if (ischosen(dtap.msgs.gcc)) { |
| 702 | return 1; |
| 703 | } else if (ischosen(dtap.msgs.bcc)) { |
| 704 | return 2; |
| 705 | } else if (ischosen(dtap.msgs.loc)) { |
| 706 | return 3; |
| 707 | } |
| 708 | /* no sequence number to patch */ |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | /* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */ |
| 713 | function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) { |
| 714 | var integer n_sd_idx := f_ML3_n_sd_idx(dtap); |
| 715 | if (n_sd_idx < 0) { |
| 716 | return; |
| 717 | } |
| 718 | var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx); |
| 719 | f_ML3_patch_seq_nr(seq_nr, enc_l3); |
| 720 | } |
| 721 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 722 | private altstep as_reset_ack() runs on RAN_Emulation_CT { |
| 723 | #ifdef RAN_EMULATION_BSSAP |
| 724 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 725 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 726 | #ifdef RAN_EMULATION_RANAP |
| 727 | var RANAP_N_UNITDATA_ind rud_ind; |
| 728 | #endif |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 729 | #ifdef RAN_EMULATION_BSSAP |
| 730 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind { |
| 731 | log("Respoding to inbound RESET with RESET-ACK"); |
| 732 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress, |
| 733 | ts_BSSMAP_ResetAck)); |
| 734 | repeat; |
| 735 | } |
| 736 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 737 | #ifdef RAN_EMULATION_RANAP |
| 738 | [] RANAP.receive(tr_RANAP_UNITDATA_ind(?, ?, tr_RANAP_Reset)) -> value rud_ind { |
| 739 | log("Respoding to inbound IuRESET with IuRESET-ACK"); |
| 740 | var CN_DomainIndicator dom; |
| 741 | dom := rud_ind.userData.initiatingMessage.value_.Reset.protocolIEs[1].value_.cN_DomainIndicator; |
| 742 | RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, rud_ind.calledAddress, |
| 743 | ts_RANAP_ResetAck(dom))); |
| 744 | } |
| 745 | #endif |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | |
| 749 | private altstep as_main_bssap() runs on RAN_Emulation_CT { |
| 750 | #ifdef RAN_EMULATION_BSSAP |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 751 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 752 | var BSSAP_N_CONNECT_ind conn_ind; |
| 753 | var BSSAP_N_CONNECT_cfm conn_cfm; |
| 754 | var BSSAP_N_DATA_ind data_ind; |
| 755 | var BSSAP_N_DISCONNECT_ind disc_ind; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 756 | var BSSAP_Conn_Req creq; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 757 | var PDU_BSSAP bssap; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 758 | var RAN_ConnHdlr vc_conn; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 759 | var integer targetPointCode; |
| 760 | var N_Sd_Array last_n_sd; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 761 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 762 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 763 | [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 764 | /* Connectionless Procedures like RESET */ |
| 765 | var template PDU_BSSAP resp; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 766 | resp := CommonBssmapUnitdataCallback(ud_ind.userData); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 767 | if (isvalue(resp)) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 768 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, |
| 769 | ud_ind.calledAddress, resp)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 770 | } |
| 771 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 772 | /* SCCP -> Client: new connection from BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 773 | [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind { |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 774 | vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 775 | /* store mapping between client components and SCCP connectionId */ |
| 776 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 777 | /* handle user payload */ |
| 778 | f_handle_userData(vc_conn, conn_ind.userData); |
| 779 | /* confirm connection establishment */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 780 | BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 781 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 782 | /* SCCP -> Client: connection-oriented data in existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 783 | [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 784 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 785 | if (ispresent(data_ind.userData)) { |
| 786 | f_handle_userData(vc_conn, data_ind.userData); |
| 787 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 788 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 789 | /* SCCP -> Client: disconnect of an existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 790 | [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 791 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 792 | if (ispresent(disc_ind.userData)) { |
| 793 | f_handle_userData(vc_conn, disc_ind.userData); |
| 794 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 795 | /* notify client about termination */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 796 | var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 797 | CLIENT.send(prim) to vc_conn; |
| 798 | f_conn_table_del(disc_ind.connectionId); |
| 799 | /* TOOD: return confirm to other side? */ |
| 800 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 801 | /* SCCP -> Client: connection confirm for outbound connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 802 | [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm { |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 803 | vc_conn := f_comp_by_conn_id(conn_cfm.connectionId); |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 804 | var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND; |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 805 | CLIENT.send(prim) to vc_conn; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 806 | /* handle user payload */ |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 807 | if (ispresent(conn_cfm.userData)) { |
| 808 | f_handle_userData(vc_conn, conn_cfm.userData); |
| 809 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 810 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 811 | [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn { |
| 812 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 813 | /* send it to dispatcher */ |
| 814 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
| 815 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 816 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 817 | /* Disconnect request client -> SCCP */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 818 | [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 819 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 820 | BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 821 | f_conn_table_del(conn_id); |
| 822 | } |
| 823 | |
| 824 | /* BSSAP from client -> SCCP */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 825 | [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn { |
| 826 | var integer conn_id; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 827 | /* send to dispatcher */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 828 | |
| 829 | if (f_comp_known(vc_conn) == false) { |
| 830 | /* unknown client, create new connection */ |
| 831 | conn_id := f_gen_conn_id(); |
| 832 | |
| 833 | /* store mapping between client components and SCCP connectionId */ |
| 834 | f_conn_table_add(vc_conn, conn_id); |
| 835 | |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 836 | BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 837 | creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 838 | } else { |
| 839 | /* known client, send via existing connection */ |
| 840 | conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 841 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Harald Welte | 49518bf | 2018-02-10 11:39:19 +0100 | [diff] [blame] | 844 | /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment |
| 845 | * counter only on MM/CC/SS, but not on RR */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 846 | if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) { |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 847 | /* we have just sent the first MM message, increment the counter */ |
| 848 | var integer idx := f_idx_by_comp(vc_conn); |
| 849 | ConnectionTable[idx].n_sd[0] := 1; |
| 850 | log("patch: N(SD) for ConnIdx ", idx, " set to 1"); |
| 851 | } |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 852 | } |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 853 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 854 | [] PROC.getcall(RAN_last_n_sd:{?,-}) -> param(vc_conn) { |
| 855 | var integer idx := f_idx_by_comp(vc_conn); |
| 856 | last_n_sd := ConnectionTable[idx].n_sd; |
| 857 | PROC.reply(RAN_last_n_sd:{vc_conn, last_n_sd}) to vc_conn; |
| 858 | } |
| 859 | |
| 860 | [] PROC.getcall(RAN_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) { |
| 861 | var integer idx := f_idx_by_comp(vc_conn); |
| 862 | ConnectionTable[idx].n_sd := last_n_sd; |
| 863 | PROC.reply(RAN_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 864 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 865 | #else |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 866 | [false] CLIENT.receive {} |
| 867 | #endif |
| 868 | } |
| 869 | |
| 870 | private altstep as_main_ranap() runs on RAN_Emulation_CT { |
| 871 | #ifdef RAN_EMULATION_RANAP |
| 872 | var RANAP_N_UNITDATA_ind rud_ind; |
| 873 | var RANAP_N_CONNECT_ind rconn_ind; |
| 874 | var RANAP_N_CONNECT_cfm rconn_cfm; |
| 875 | var RANAP_N_DATA_ind rdata_ind; |
| 876 | var RANAP_N_DISCONNECT_ind rdisc_ind; |
| 877 | var RANAP_Conn_Req creq; |
| 878 | var RANAP_PDU ranap; |
| 879 | var RAN_ConnHdlr vc_conn; |
| 880 | |
| 881 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
| 882 | [] RANAP.receive(RANAP_N_UNITDATA_ind:?) -> value rud_ind { |
| 883 | /* Connectionless Procedures like RESET */ |
| 884 | var template RANAP_PDU resp; |
| 885 | resp := CommonRanapUnitdataCallback(rud_ind.userData); |
| 886 | if (isvalue(resp)) { |
| 887 | RANAP.send(ts_RANAP_UNITDATA_req(rud_ind.callingAddress, |
| 888 | rud_ind.calledAddress, resp)); |
| 889 | } |
| 890 | } |
| 891 | /* SCCP -> Client: new connection from BSC */ |
| 892 | [] RANAP.receive(RANAP_N_CONNECT_ind:?) -> value rconn_ind { |
| 893 | vc_conn := g_ran_ops.ranap_create_cb.apply(rconn_ind, g_ran_id); |
| 894 | /* store mapping between client components and SCCP connectionId */ |
| 895 | f_conn_table_add(vc_conn, rconn_ind.connectionId); |
| 896 | /* handle user payload */ |
| 897 | f_handle_userData_RANAP(vc_conn, rconn_ind.userData); |
| 898 | /* confirm connection establishment */ |
| 899 | RANAP.send(ts_RANAP_CONNECT_res(rconn_ind.connectionId, omit)); |
| 900 | } |
| 901 | /* SCCP -> Client: connection-oriented data in existing connection */ |
| 902 | [] RANAP.receive(RANAP_N_DATA_ind:?) -> value rdata_ind { |
| 903 | vc_conn := f_comp_by_conn_id(rdata_ind.connectionId); |
| 904 | if (ispresent(rdata_ind.userData)) { |
| 905 | f_handle_userData_RANAP(vc_conn, rdata_ind.userData); |
| 906 | } |
| 907 | } |
| 908 | /* SCCP -> Client: disconnect of an existing connection */ |
| 909 | [] RANAP.receive(RANAP_N_DISCONNECT_ind:?) -> value rdisc_ind { |
| 910 | vc_conn := f_comp_by_conn_id(rdisc_ind.connectionId); |
| 911 | if (ispresent(rdisc_ind.userData)) { |
| 912 | f_handle_userData_RANAP(vc_conn, rdisc_ind.userData); |
| 913 | } |
| 914 | /* notify client about termination */ |
| 915 | var RAN_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
| 916 | CLIENT.send(prim) to vc_conn; |
| 917 | f_conn_table_del(rdisc_ind.connectionId); |
| 918 | /* TOOD: return confirm to other side? */ |
| 919 | } |
| 920 | /* SCCP -> Client: connection confirm for outbound connection */ |
| 921 | [] RANAP.receive(RANAP_N_CONNECT_cfm:?) -> value rconn_cfm { |
| 922 | vc_conn := f_comp_by_conn_id(rconn_cfm.connectionId); |
| 923 | var RAN_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND; |
| 924 | CLIENT.send(prim) to vc_conn; |
| 925 | /* handle user payload */ |
| 926 | if (ispresent(rconn_cfm.userData)) { |
| 927 | f_handle_userData_RANAP(vc_conn, rconn_cfm.userData); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | [] CLIENT.receive(RANAP_PDU:?) -> value ranap sender vc_conn { |
| 932 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 933 | /* send it to dispatcher */ |
| 934 | RANAP.send(ts_RANAP_DATA_req(conn_id, ranap)); |
| 935 | } |
| 936 | |
| 937 | /* Disconnect request client -> SCCP */ |
| 938 | [] CLIENT.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 939 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 940 | RANAP.send(ts_RANAP_DISC_req(conn_id, 0)); |
| 941 | f_conn_table_del(conn_id); |
| 942 | } |
| 943 | |
| 944 | /* BSSAP from client -> SCCP */ |
| 945 | [] CLIENT.receive(RANAP_Conn_Req:?) -> value creq sender vc_conn { |
| 946 | var integer conn_id; |
| 947 | /* send to dispatcher */ |
| 948 | |
| 949 | if (f_comp_known(vc_conn) == false) { |
| 950 | /* unknown client, create new connection */ |
| 951 | conn_id := f_gen_conn_id(); |
| 952 | |
| 953 | /* store mapping between client components and SCCP connectionId */ |
| 954 | f_conn_table_add(vc_conn, conn_id); |
| 955 | |
| 956 | RANAP.send(ts_RANAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 957 | creq.ranap)); |
| 958 | } else { |
| 959 | /* known client, send via existing connection */ |
| 960 | conn_id := f_conn_id_by_comp(vc_conn); |
| 961 | RANAP.send(ts_RANAP_DATA_req(conn_id, creq.ranap)); |
| 962 | } |
| 963 | |
| 964 | /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment |
| 965 | * counter only on MM/CC/SS, but not on RR */ |
| 966 | if (g_ran_ops.role_ms and not f_ranap_l3_is_rr(creq.ranap)) { |
| 967 | /* we have just sent the first MM message, increment the counter */ |
| 968 | var integer idx := f_idx_by_comp(vc_conn); |
| 969 | ConnectionTable[idx].n_sd[0] := 1; |
| 970 | log("patch: N(SD) for ConnIdx ", idx, " set to 1"); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | #else |
| 975 | [false] CLIENT.receive {} |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 976 | #endif |
| 977 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 978 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 979 | private altstep as_main_mgcp() runs on RAN_Emulation_CT { |
| 980 | #ifdef RAN_EMULATION_MGCP |
| 981 | var MgcpCommand mgcp_req; |
| 982 | var MgcpResponse mgcp_resp; |
| 983 | var RAN_ConnHdlr vc_conn; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 984 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 985 | /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP |
| 986 | * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC |
| 987 | * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC |
| 988 | * is printed as hex string, e.g. a@mgw for CIC 10 */ |
| 989 | |
| 990 | /* CLIENT -> MGCP */ |
| 991 | [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn { |
| 992 | /* MGCP request from Handler (we're MSC) */ |
| 993 | /* store the transaction ID we've seen */ |
| 994 | f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id); |
| 995 | /* simply forward any MGCP from the client to the port */ |
| 996 | MGCP.send(mgcp_req); |
| 997 | } |
| 998 | [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn { |
| 999 | /* MGCP response from Handler (we're BSC/MGW) */ |
| 1000 | /* simply forward any MGCP from the client to the port */ |
| 1001 | MGCP.send(mgcp_resp); |
| 1002 | } |
| 1003 | |
| 1004 | /* MGCP -> CLIENT */ |
| 1005 | [] MGCP.receive(MgcpCommand:?) -> value mgcp_req { |
| 1006 | /* MGCP request from network side (we're BSC/MGW) */ |
| 1007 | /* Extract CIC from local part of endpoint name */ |
| 1008 | var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep); |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 1009 | if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) { |
| 1010 | /* ignore RSIP for unknown CIC */ |
| 1011 | } else { |
| 1012 | /* Resolve the vc_conn by the CIC */ |
| 1013 | vc_conn := f_comp_by_cic(cic); |
| 1014 | CLIENT.send(mgcp_req) to vc_conn; |
| 1015 | } |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 1016 | } |
| 1017 | [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp { |
| 1018 | /* MGCP response from network side (we're MSC) */ |
| 1019 | /* Resolve the vc_conn by the transaction ID */ |
| 1020 | vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id); |
| 1021 | CLIENT.send(mgcp_resp) to vc_conn; |
| 1022 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1023 | #else |
| 1024 | [false] CLIENT.receive {} |
| 1025 | #endif |
| 1026 | } |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 1027 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1028 | /* send a raw (encoded) L3 message over given SCCP connection */ |
| 1029 | private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on RAN_Emulation_CT |
| 1030 | { |
| 1031 | select (g_ran_ops.protocol) { |
| 1032 | #ifdef RAN_EMULATION_BSSAP |
| 1033 | case (RAN_PROTOCOL_BSSAP) { |
| 1034 | var PDU_BSSAP bssap; |
| 1035 | bssap := valueof(ts_BSSAP_DTAP(l3_enc, dlci)); |
| 1036 | BSSAP.send(ts_BSSAP_DATA_req(sccp_conn_id, bssap)); |
| 1037 | } |
| 1038 | #endif |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 1039 | #ifdef RAN_EMULATION_RANAP |
| 1040 | case (RAN_PROTOCOL_RANAP) { |
| 1041 | var RANAP_PDU ranap; |
| 1042 | if (false /* SAPI */) { |
| 1043 | var RANAP_IEs.SAPI sapi := sapi_0; |
| 1044 | ranap := valueof(ts_RANAP_DirectTransferSAPI(l3_enc, sapi)); |
| 1045 | } else { |
| 1046 | ranap := valueof(ts_RANAP_DirectTransfer(l3_enc)); |
| 1047 | } |
| 1048 | RANAP.send(ts_RANAP_DATA_req(sccp_conn_id, ranap)); |
| 1049 | } |
| 1050 | #endif |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | function main(RanOps ops, charstring id) runs on RAN_Emulation_CT { |
| 1055 | |
| 1056 | g_ran_id := id; |
| 1057 | g_ran_ops := ops; |
| 1058 | f_conn_table_init(); |
| 1059 | f_expect_table_init(); |
| 1060 | |
| 1061 | if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) { |
| 1062 | f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */ |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 1063 | select (g_ran_ops.protocol) { |
| 1064 | #ifdef RAN_EMULATION_BSSAP |
| 1065 | case (RAN_PROTOCOL_BSSAP) { |
| 1066 | f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local); |
| 1067 | } |
| 1068 | #endif |
| 1069 | #ifdef RAN_EMULATION_RANAP |
| 1070 | case (RAN_PROTOCOL_RANAP) { |
| 1071 | f_ranap_reset(ops.sccp_addr_peer, ops.sccp_addr_local); |
| 1072 | } |
| 1073 | #endif |
| 1074 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | while (true) { |
| 1078 | var RAN_ConnHdlr vc_conn; |
| 1079 | var PDU_DTAP_MO dtap_mo; |
| 1080 | var PDU_DTAP_MT dtap_mt; |
| 1081 | var RAN_ConnHdlr vc_hdlr; |
| 1082 | var octetstring l3_info; |
| 1083 | var hexstring imsi; |
| 1084 | var OCT4 tmsi; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1085 | var integer targetPointCode; |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1086 | |
| 1087 | alt { |
| 1088 | [g_ran_ops.protocol == RAN_PROTOCOL_BSSAP] as_main_bssap(); |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 1089 | [g_ran_ops.protocol == RAN_PROTOCOL_RANAP] as_main_ranap(); |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1090 | |
| 1091 | [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn { |
| 1092 | var integer idx := f_idx_by_comp(vc_conn); |
| 1093 | /* convert from decoded DTAP to encoded DTAP */ |
| 1094 | var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap); |
| 1095 | /* patch correct L3 send sequence number N(SD) into l3_enc */ |
| 1096 | if (dtap_mo.skip_seq_patching == false) { |
| 1097 | f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc); |
| 1098 | } |
| 1099 | f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc); |
| 1100 | } |
| 1101 | |
| 1102 | [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn { |
| 1103 | var integer idx := f_idx_by_comp(vc_conn); |
| 1104 | /* convert from decoded DTAP to encoded DTAP */ |
| 1105 | var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap); |
| 1106 | f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc); |
| 1107 | } |
| 1108 | |
| 1109 | [] as_main_mgcp(); |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1110 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1111 | [] PROC.getcall(RAN_register:{?,?}) -> param(l3_info, vc_hdlr) { |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1112 | f_create_expect(l3_info, vc_hdlr); |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1113 | PROC.reply(RAN_register:{l3_info, vc_hdlr}) to vc_hdlr; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1116 | [] PROC.getcall(RAN_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) { |
| 1117 | f_create_expect(omit, vc_hdlr, targetPointCode); |
| 1118 | PROC.reply(RAN_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr; |
| 1119 | } |
| 1120 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1121 | [] PROC.getcall(RAN_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1122 | f_create_imsi(imsi, tmsi, vc_hdlr); |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1123 | PROC.reply(RAN_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1131 | #ifdef RAN_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 1132 | private function f_mgcp_ep_extract_cic(charstring inp) return integer { |
Harald Welte | 525a9c1 | 2017-11-24 23:40:41 +0100 | [diff] [blame] | 1133 | var charstring local_part := regexp(inp, "(*)@*", 0); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 1134 | return hex2int(str2hex(local_part)); |
| 1135 | |
| 1136 | } |
Harald Welte | 2fce788 | 2019-04-15 11:48:05 +0200 | [diff] [blame] | 1137 | #endif |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 1138 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1139 | /*********************************************************************** |
| 1140 | * "Expect" Handling (mapping for expected incoming SCCP connections) |
| 1141 | ***********************************************************************/ |
| 1142 | |
| 1143 | /* data about an expected future incoming connection */ |
| 1144 | type record ExpectData { |
| 1145 | /* L3 payload based on which we can match it */ |
| 1146 | octetstring l3_payload optional, |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1147 | integer handoverRequestPointCode optional, |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1148 | /* component reference for this connection */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1149 | RAN_ConnHdlr vc_conn |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* procedure based port to register for incoming connections */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1153 | signature RAN_register(in octetstring l3, in RAN_ConnHdlr hdlr); |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1154 | signature RAN_register_handoverRequest(in integer targetPointCode, in RAN_ConnHdlr hdlr); |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1155 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1156 | /* procedure based port to register for incoming IMSI/TMSI */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1157 | signature RAN_register_imsi(in hexstring imsi, in OCT4 tmsi, in RAN_ConnHdlr hdlr); |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1158 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1159 | /* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */ |
| 1160 | signature RAN_last_n_sd(in RAN_ConnHdlr hdlr, out N_Sd_Array last_n_sd); |
| 1161 | |
| 1162 | /* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */ |
| 1163 | signature RAN_continue_after_n_sd(N_Sd_Array last_n_sd, in RAN_ConnHdlr hdlr); |
| 1164 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1165 | type port RAN_PROC_PT procedure { |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1166 | inout RAN_register, RAN_register_imsi, RAN_register_handoverRequest, RAN_last_n_sd, RAN_continue_after_n_sd; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1167 | } with { extension "internal" }; |
| 1168 | |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 1169 | #ifdef RAN_EMULATION_BSSAP |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1170 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 1171 | function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1172 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
| 1173 | var RAN_ConnHdlr ret := null; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1174 | var octetstring l3_info; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1175 | var boolean handoverRequest := false; |
| 1176 | var integer handoverRequestPointCode; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1177 | var integer i; |
| 1178 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1179 | if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) { |
| 1180 | l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info; |
| 1181 | log("ExpectedCreateCallback completeLayer3Information"); |
| 1182 | } else if (ischosen(conn_ind.userData.pdu.bssmap.handoverRequest)) { |
| 1183 | handoverRequest := true; |
| 1184 | handoverRequestPointCode := bit2int(conn_ind.calledAddress.signPointCode); |
| 1185 | log("ExpectedCreateCallback handoverRequest ", handoverRequestPointCode); |
| 1186 | } else { |
| 1187 | setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3 nor a Handover Request"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 1188 | mtc.stop; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1189 | return ret; |
| 1190 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1191 | |
| 1192 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1193 | if (handoverRequest) { |
| 1194 | log("ExpectTable[", i, "].handoverRequestPointCode = ", ExpectTable[i].handoverRequestPointCode, |
| 1195 | " ==? ", handoverRequestPointCode); |
| 1196 | if (ExpectTable[i].handoverRequestPointCode == handoverRequestPointCode) { |
| 1197 | ret := ExpectTable[i].vc_conn; |
| 1198 | log("Found Expect[", i, "] for handoverRequest handled at ", ret); |
| 1199 | return ret; |
| 1200 | } else { |
| 1201 | continue; |
| 1202 | } |
| 1203 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1204 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 1205 | continue; |
| 1206 | } |
| 1207 | if (l3_info == ExpectTable[i].l3_payload) { |
| 1208 | ret := ExpectTable[i].vc_conn; |
| 1209 | /* release this entry to be used again */ |
| 1210 | ExpectTable[i].l3_payload := omit; |
| 1211 | ExpectTable[i].vc_conn := null; |
| 1212 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 1213 | /* return the component reference */ |
| 1214 | return ret; |
| 1215 | } |
| 1216 | } |
| 1217 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 1218 | mtc.stop; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1219 | return ret; |
| 1220 | } |
Harald Welte | 5b02762 | 2019-04-14 23:40:17 +0200 | [diff] [blame] | 1221 | #endif |
| 1222 | |
| 1223 | #ifdef RAN_EMULATION_RANAP |
| 1224 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 1225 | function RanapExpectedCreateCallback(RANAP_N_CONNECT_ind conn_ind, charstring id) |
| 1226 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
| 1227 | var RAN_ConnHdlr ret := null; |
| 1228 | var template (omit) octetstring l3_info; |
| 1229 | var integer i; |
| 1230 | |
| 1231 | l3_info := f_ranap_extract_l3(conn_ind.userData); |
| 1232 | if (istemplatekind(l3_info, "omit")) { |
| 1233 | setverdict(fail, "N-CONNECT.ind without NAS payload"); |
| 1234 | mtc.stop; |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
| 1239 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 1240 | continue; |
| 1241 | } |
| 1242 | if (valueof(l3_info) == ExpectTable[i].l3_payload) { |
| 1243 | ret := ExpectTable[i].vc_conn; |
| 1244 | /* release this entry to be used again */ |
| 1245 | ExpectTable[i].l3_payload := omit; |
| 1246 | ExpectTable[i].vc_conn := null; |
| 1247 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 1248 | /* return the component reference */ |
| 1249 | return ret; |
| 1250 | } |
| 1251 | } |
| 1252 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
| 1253 | mtc.stop; |
| 1254 | return ret; |
| 1255 | } |
| 1256 | #endif |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1257 | |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1258 | private function f_create_expect(template octetstring l3, RAN_ConnHdlr hdlr, |
| 1259 | template integer handoverRequestPointCode := omit) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1260 | runs on RAN_Emulation_CT { |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1261 | var integer i; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1262 | log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode); |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1263 | for (i := 0; i < sizeof(ExpectTable); i := i+1) { |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1264 | if (not ispresent(ExpectTable[i].l3_payload) |
| 1265 | and not ispresent(ExpectTable[i].handoverRequestPointCode)) { |
| 1266 | if (ispresent(l3)) { |
| 1267 | ExpectTable[i].l3_payload := valueof(l3); |
| 1268 | } |
| 1269 | if (ispresent(handoverRequestPointCode)) { |
| 1270 | ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode); |
| 1271 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1272 | ExpectTable[i].vc_conn := hdlr; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1273 | if (ispresent(handoverRequestPointCode)) { |
| 1274 | log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr); |
| 1275 | } else { |
| 1276 | log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr); |
| 1277 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1278 | return; |
| 1279 | } |
| 1280 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 1281 | testcase.stop("No space left in ExpectTable"); |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1284 | private function f_create_imsi(hexstring imsi, OCT4 tmsi, RAN_ConnHdlr hdlr) |
| 1285 | runs on RAN_Emulation_CT { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1286 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 1287 | if (not ispresent(ImsiTable[i].imsi)) { |
| 1288 | ImsiTable[i].imsi := imsi; |
| 1289 | ImsiTable[i].tmsi := tmsi; |
| 1290 | ImsiTable[i].comp_ref := hdlr; |
| 1291 | log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr); |
| 1292 | return; |
| 1293 | } |
| 1294 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 1295 | testcase.stop("No space left in ImsiTable"); |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 1299 | private function f_expect_table_init() |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1300 | runs on RAN_Emulation_CT { |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 1301 | for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 1302 | ExpectTable[i].l3_payload := omit; |
Neels Hofmeyr | 0ac6315 | 2019-05-07 01:20:17 +0200 | [diff] [blame^] | 1303 | ExpectTable[i].handoverRequestPointCode := omit; |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 1304 | } |
| 1305 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 1306 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1307 | /* helper function for clients to register their IMSI/TMSI */ |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 1308 | function f_ran_register_imsi(hexstring imsi, OCT4 tmsi) |
| 1309 | runs on RAN_ConnHdlr { |
| 1310 | BSSAP_PROC.call(RAN_register_imsi:{imsi, tmsi, self}) { |
| 1311 | [] BSSAP_PROC.getreply(RAN_register_imsi:{?,?,?}) {}; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | |
Harald Welte | 475a2c1 | 2019-05-02 19:05:48 +0200 | [diff] [blame] | 1315 | #ifdef RAN_EMULATION_RANAP |
| 1316 | /* expect a IuReleaseCommand; Confirm that; expect SCCP-level N-DISCONNET.ind */ |
| 1317 | altstep as_iu_release_compl_disc(float t := 5.0) runs on RAN_ConnHdlr { |
| 1318 | var RANAP_PDU ranap; |
| 1319 | [] BSSAP.receive(tr_RANAP_IuReleaseCommand(?)) { |
| 1320 | BSSAP.send(ts_RANAP_IuReleaseComplete); |
| 1321 | alt { |
| 1322 | [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 1323 | setverdict(pass); |
| 1324 | } |
| 1325 | [] BSSAP.receive { |
| 1326 | setverdict(fail, "Unexpected RANAP while waiting for SCCP Release "); |
| 1327 | mtc.stop; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | [] BSSAP.receive(RANAP_PDU:?) -> value ranap{ |
| 1332 | setverdict(fail, "Unexpected RANAP while waiting for IuReleaseCommand", ranap); |
| 1333 | mtc.stop; |
| 1334 | } |
| 1335 | } |
| 1336 | #endif |
| 1337 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 1338 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 1339 | } |