Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 1 | module BSSMAP_Emulation { |
| 2 | |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 3 | /* BSSMAP Emulation, runs on top of BSSAP_CodecPort. It multiplexes/demultiplexes |
| 4 | * the individual connections, so there can be separate TTCN-3 components handling |
| 5 | * each of the connections. |
| 6 | * |
| 7 | * The BSSMAP_Emulation.main() function processes SCCP primitives from the SCCP |
| 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 |
| 11 | * to the component running the BSSMAP_Emulation.main() function. |
| 12 | * |
| 13 | * For each new inbound connections, the BssmapOps.create_cb() is called. It can create |
| 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 | * |
| 22 | * Inbound Unit Data messages (such as are dispatched to the BssmapOps.unitdata_cb() callback, |
| 23 | * which is registered with an argument to the main() function below. |
| 24 | * |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 25 | * (C) 2017-2018 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; |
| 37 | import from BSSAP_Types all; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 38 | import from BSSAP_CodecPort all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 39 | import from BSSMAP_Templates all; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 40 | import from MGCP_Types all; |
| 41 | import from MGCP_Templates all; |
| 42 | import from IPA_Emulation all; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 43 | import from MobileL3_Types all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 44 | |
| 45 | /* General "base class" component definition, of which specific implementations |
| 46 | * derive themselves by means of the "extends" feature */ |
| 47 | type component BSSAP_ConnHdlr { |
| 48 | /* port towards MSC Emulator core / SCCP connection dispatchar */ |
| 49 | port BSSAP_Conn_PT BSSAP; |
Harald Welte | ca51998 | 2018-01-21 19:28:26 +0100 | [diff] [blame] | 50 | /* procedure based port to register for incoming connections */ |
| 51 | port BSSMAPEM_PROC_PT BSSAP_PROC; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
| 55 | type enumerated BSSAP_Conn_Prim { |
| 56 | /* SCCP tell us that connection was released */ |
| 57 | MSC_CONN_PRIM_DISC_IND, |
| 58 | /* we tell SCCP to release connection */ |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 59 | MSC_CONN_PRIM_DISC_REQ, |
| 60 | /* Connection confirmed indication */ |
| 61 | MSC_CONN_PRIM_CONF_IND |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 64 | type record BSSAP_Conn_Req { |
| 65 | SCCP_PAR_Address addr_peer, |
| 66 | SCCP_PAR_Address addr_own, |
| 67 | PDU_BSSAP bssap |
| 68 | } |
| 69 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 70 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 71 | type record PDU_DTAP_MO { |
| 72 | OCT1 dlci optional, |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 73 | boolean skip_seq_patching optional, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 74 | PDU_ML3_MS_NW dtap |
| 75 | } |
| 76 | |
| 77 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 78 | type record PDU_DTAP_MT { |
| 79 | OCT1 dlci optional, |
| 80 | PDU_ML3_NW_MS dtap |
| 81 | } |
| 82 | |
| 83 | template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := { |
| 84 | dlci := dlci, |
| 85 | dtap := dtap |
| 86 | } |
| 87 | |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 88 | 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] | 89 | dlci := dlci, |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 90 | skip_seq_patching := skip_seq_patching, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 91 | dtap := dtap |
| 92 | } |
| 93 | |
| 94 | template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := { |
| 95 | dlci := dlci, |
| 96 | dtap := dtap |
| 97 | } |
| 98 | |
| 99 | template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := { |
| 100 | dlci := dlci, |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 101 | skip_seq_patching := ?, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 102 | dtap := dtap |
| 103 | } |
| 104 | |
Neels Hofmeyr | bd0ef93 | 2018-03-19 14:58:46 +0100 | [diff] [blame] | 105 | template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := { |
| 106 | addr_peer := peer, |
| 107 | addr_own := own, |
| 108 | bssap := bssap |
| 109 | }; |
| 110 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 111 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 112 | /* port between individual per-connection components and this dispatcher */ |
| 113 | type port BSSAP_Conn_PT message { |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 114 | /* BSSAP or direct DTAP messages from/to clients */ |
| 115 | inout PDU_BSSAP, PDU_DTAP_MO, PDU_DTAP_MT, |
| 116 | /* misc indications / requests between SCCP and client */ |
| 117 | BSSAP_Conn_Prim, |
| 118 | /* Client requests us to create SCCP Connection */ |
| 119 | BSSAP_Conn_Req, |
| 120 | /* MGCP, only used for IPA SCCPlite (MGCP in IPA mux) */ |
| 121 | MgcpCommand, MgcpResponse; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 122 | } with { extension "internal" }; |
| 123 | |
| 124 | |
| 125 | /* represents a single BSSAP connection over SCCP */ |
| 126 | type record ConnectionData { |
| 127 | /* reference to the instance of the per-connection component */ |
| 128 | BSSAP_ConnHdlr comp_ref, |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 129 | integer sccp_conn_id, |
| 130 | /* most recent MGCP transaction ID (Used on MSC side) */ |
| 131 | MgcpTransId mgcp_trans_id optional, |
| 132 | /* CIC that has been used for voice of this channel (BSC side) */ |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 133 | integer cic optional, |
| 134 | /* array of N(SD) values for MO DTAP messages, indexed by discriminator */ |
| 135 | uint2_t n_sd[4] |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 138 | type record ImsiMapping { |
| 139 | BSSAP_ConnHdlr comp_ref, |
| 140 | hexstring imsi optional, |
| 141 | OCT4 tmsi |
| 142 | } |
| 143 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 144 | type component BSSMAP_Emulation_CT { |
| 145 | /* SCCP port on the bottom side, using ASP primitives */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 146 | port BSSAP_CODEC_PT BSSAP; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 147 | /* BSSAP port to the per-connection clients */ |
| 148 | port BSSAP_Conn_PT CLIENT; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 149 | /* MGCP port */ |
| 150 | port IPA_MGCP_PT MGCP; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 151 | |
| 152 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 153 | var ConnectionData ConnectionTable[16]; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 154 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 155 | /* pending expected incoming connections */ |
| 156 | var ExpectData ExpectTable[8]; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 157 | |
| 158 | /* tables for mapping inbound unitdata (like paging) */ |
| 159 | var ImsiMapping ImsiTable[16]; |
| 160 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 161 | /* procedure based port to register for incoming connections */ |
| 162 | port BSSMAPEM_PROC_PT PROC; |
| 163 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 164 | var charstring g_bssmap_id; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 165 | var integer g_next_e1_ts := 1; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 166 | var BssmapOps g_bssmap_ops; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 169 | private function f_conn_id_known(integer sccp_conn_id) |
| 170 | runs on BSSMAP_Emulation_CT return boolean { |
| 171 | var integer i; |
| 172 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 173 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){ |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | private function f_comp_known(BSSAP_ConnHdlr client) |
| 181 | runs on BSSMAP_Emulation_CT return boolean { |
| 182 | var integer i; |
| 183 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 184 | if (ConnectionTable[i].comp_ref == client) { |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | return false; |
| 189 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 190 | |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 191 | private function f_cic_known(integer cic) |
| 192 | runs on BSSMAP_Emulation_CT return boolean { |
| 193 | var integer i; |
| 194 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 195 | if (ConnectionTable[i].cic == cic) { |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 202 | /* resolve component reference by connection ID */ |
| 203 | private function f_comp_by_conn_id(integer sccp_conn_id) |
| 204 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 205 | var integer i; |
| 206 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 207 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 208 | return ConnectionTable[i].comp_ref; |
| 209 | } |
| 210 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 211 | setverdict(fail, "BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id); |
| 212 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 215 | /* resolve component reference by CIC */ |
| 216 | private function f_comp_by_mgcp_tid(MgcpTransId tid) |
| 217 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 218 | var integer i; |
| 219 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 220 | if (ConnectionTable[i].mgcp_trans_id == tid) { |
| 221 | return ConnectionTable[i].comp_ref; |
| 222 | } |
| 223 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 224 | setverdict(fail, "BSSMAP Connection table not found by MGCP Transaction ID ", tid); |
| 225 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid) |
| 229 | runs on BSSMAP_Emulation_CT { |
| 230 | var integer i; |
| 231 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 232 | if (ConnectionTable[i].comp_ref == client) { |
| 233 | ConnectionTable[i].mgcp_trans_id := tid; |
| 234 | return; |
| 235 | } |
| 236 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 237 | setverdict(fail, "BSSMAP Connection table not found by component ", client); |
| 238 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private function f_comp_by_cic(integer cic) |
| 242 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 243 | var integer i; |
| 244 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 245 | if (ConnectionTable[i].cic == cic) { |
| 246 | return ConnectionTable[i].comp_ref; |
| 247 | } |
| 248 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 249 | setverdict(fail, "BSSMAP Connection table not found by CIC ", cic); |
| 250 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic) |
| 254 | runs on BSSMAP_Emulation_CT { |
| 255 | var integer i; |
| 256 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 257 | if (ConnectionTable[i].comp_ref == client) { |
| 258 | ConnectionTable[i].cic := cic; |
| 259 | return; |
| 260 | } |
| 261 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 262 | setverdict(fail, "BSSMAP Connection table not found by component ", client); |
| 263 | mtc.stop; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 266 | /* resolve connection ID by component reference */ |
| 267 | private function f_conn_id_by_comp(BSSAP_ConnHdlr client) |
| 268 | runs on BSSMAP_Emulation_CT return integer { |
| 269 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 270 | if (ConnectionTable[i].comp_ref == client) { |
| 271 | return ConnectionTable[i].sccp_conn_id; |
| 272 | } |
| 273 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 274 | setverdict(fail, "BSSMAP Connection table not found by component ", client); |
| 275 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 278 | /* resolve ConnectionTable index component reference */ |
| 279 | private function f_idx_by_comp(BSSAP_ConnHdlr client) |
| 280 | runs on BSSMAP_Emulation_CT return integer { |
| 281 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 282 | if (ConnectionTable[i].comp_ref == client) { |
| 283 | return i; |
| 284 | } |
| 285 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 286 | setverdict(fail, "BSSMAP Connection table not found by component ", client); |
| 287 | mtc.stop; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 290 | private function f_gen_conn_id() |
| 291 | runs on BSSMAP_Emulation_CT return integer { |
| 292 | var integer conn_id; |
| 293 | |
| 294 | do { |
| 295 | conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId); |
| 296 | } while (f_conn_id_known(conn_id) == true); |
| 297 | |
| 298 | return conn_id; |
| 299 | } |
| 300 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 301 | private function f_conn_table_init() |
| 302 | runs on BSSMAP_Emulation_CT { |
| 303 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 304 | ConnectionTable[i].comp_ref := null; |
| 305 | ConnectionTable[i].sccp_conn_id := -1; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 306 | ConnectionTable[i].mgcp_trans_id := omit; |
| 307 | ConnectionTable[i].cic := omit; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 308 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 309 | } |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 310 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 311 | ImsiTable[i].comp_ref := null; |
| 312 | ImsiTable[i].imsi := omit; |
| 313 | ImsiTable[i].tmsi := 'FFFFFFFF'O; |
| 314 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id) |
| 318 | runs on BSSMAP_Emulation_CT { |
| 319 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 320 | if (ConnectionTable[i].sccp_conn_id == -1) { |
| 321 | ConnectionTable[i].comp_ref := comp_ref; |
| 322 | ConnectionTable[i].sccp_conn_id := sccp_conn_id; |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 323 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 324 | log("Added conn table entry ", i, comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 328 | testcase.stop("BSSMAP Connection table full!"); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | private function f_conn_table_del(integer sccp_conn_id) |
| 332 | runs on BSSMAP_Emulation_CT { |
| 333 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 334 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 335 | log("Deleted conn table entry ", i, |
| 336 | ConnectionTable[i].comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 337 | ConnectionTable[i].sccp_conn_id := -1; |
| 338 | ConnectionTable[i].comp_ref := null; |
Harald Welte | 0a4317a | 2017-11-25 00:32:46 +0100 | [diff] [blame] | 339 | return |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 340 | } |
| 341 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 342 | setverdict(fail, "BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id); |
| 343 | mtc.stop; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 346 | private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi) |
| 347 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 348 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 349 | if (ImsiTable[i].imsi == imsi or |
| 350 | isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) { |
| 351 | return ImsiTable[i].comp_ref; |
| 352 | } |
| 353 | } |
| 354 | return null; |
| 355 | } |
| 356 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 357 | /* handle (optional) userData portion of various primitives and dispatch it to the client */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 358 | private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap) |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 359 | runs on BSSMAP_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 360 | /* decode + send decoded BSSAP to client */ |
Harald Welte | 1b2748e | 2017-11-24 23:40:16 +0100 | [diff] [blame] | 361 | |
Harald Welte | 473676b | 2018-01-27 20:38:01 +0100 | [diff] [blame] | 362 | if (ischosen(bssap.pdu.bssmap)) { |
| 363 | /* BSC Side: If this is an assignment command, store CIC */ |
| 364 | if (ischosen(bssap.pdu.bssmap.assignmentRequest) and |
| 365 | ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) { |
| 366 | var BSSMAP_IE_CircuitIdentityCode cic_ie := |
| 367 | bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode; |
| 368 | var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow); |
| 369 | f_comp_store_cic(client, cic); |
| 370 | } |
Harald Welte | 1b2748e | 2017-11-24 23:40:16 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 373 | if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) { |
| 374 | if (g_bssmap_ops.role_ms) { |
| 375 | /* we are the MS, so any message to us must be MT */ |
| 376 | var PDU_DTAP_MT mt := { |
| 377 | dlci := bssap.dlci, |
| 378 | dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap) |
| 379 | }; |
| 380 | CLIENT.send(mt) to client; |
| 381 | } else { |
| 382 | /* we are the Network, so any message to us must be MO */ |
| 383 | var PDU_DTAP_MO mo := { |
| 384 | dlci := bssap.dlci, |
| 385 | dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap) |
| 386 | }; |
| 387 | CLIENT.send(mo) to client; |
| 388 | } |
| 389 | } else { |
| 390 | CLIENT.send(bssap) to client; |
| 391 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 395 | * arrives */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 396 | type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 397 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr; |
| 398 | |
| 399 | type function BssmapUnitdataCallback(PDU_BSSAP bssap) |
| 400 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP; |
| 401 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 402 | /* handle common Unitdata such as Paging */ |
| 403 | private function CommonBssmapUnitdataCallback(PDU_BSSAP bssap) |
| 404 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP { |
| 405 | if (match(bssap, tr_BSSMAP_Paging)) { |
| 406 | var BSSAP_ConnHdlr client := null; |
| 407 | client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits, |
| 408 | bssap.pdu.bssmap.paging.tMSI.tmsiOctets); |
| 409 | if (isvalue(client)) { |
| 410 | log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ", |
| 411 | client); |
| 412 | CLIENT.send(bssap) to client; |
| 413 | return omit; |
| 414 | } |
| 415 | log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table"); |
| 416 | } else { |
| 417 | log("CommonBssmapUnitdataCallback: Not a paging message"); |
| 418 | } |
| 419 | /* ELSE: handle in user callback */ |
| 420 | return g_bssmap_ops.unitdata_cb.apply(bssap); |
| 421 | } |
| 422 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 423 | type record BssmapOps { |
| 424 | BssmapCreateCallback create_cb, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 425 | BssmapUnitdataCallback unitdata_cb, |
| 426 | boolean decode_dtap, |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 427 | boolean role_ms, |
| 428 | /* needed for performing BSSMAP RESET */ |
| 429 | SCCP_PAR_Address sccp_addr_local optional, |
| 430 | SCCP_PAR_Address sccp_addr_peer optional |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 433 | template BIT4 t_ML3_DISC_CC_MM_SS := ('0011'B, '0101'B, '1011'B); |
| 434 | |
| 435 | /* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */ |
| 436 | function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) { |
| 437 | var uint2_t seq_nr; |
| 438 | if (ischosen(dtap.msgs.cc) or ischosen(dtap.msgs.mm) or ischosen(dtap.msgs.ss)) { |
| 439 | seq_nr := cd.n_sd[0]; |
| 440 | cd.n_sd[0] := (cd.n_sd[0] + 1) mod 4; |
| 441 | } else if (ischosen(dtap.msgs.gcc)) { |
| 442 | seq_nr := cd.n_sd[1]; |
| 443 | cd.n_sd[1] := (cd.n_sd[1] + 1) mod 2; |
| 444 | } else if (ischosen(dtap.msgs.bcc)) { |
| 445 | seq_nr := cd.n_sd[2]; |
| 446 | cd.n_sd[2] := (cd.n_sd[2] + 1) mod 2; |
| 447 | } else if (ischosen(dtap.msgs.loc)) { |
| 448 | seq_nr := cd.n_sd[3]; |
| 449 | cd.n_sd[3] := (cd.n_sd[3] + 1) mod 2; |
| 450 | } else { |
| 451 | /* no sequence number to patch */ |
| 452 | return; |
| 453 | } |
| 454 | log("patching N(SD)=", seq_nr, " into dtap ", enc_l3); |
| 455 | enc_l3[1] := (enc_l3[1] and4b '3f'O) or4b bit2oct(int2bit(seq_nr, 8) << 6); |
| 456 | log("patched enc_l3: ", enc_l3); |
| 457 | } |
| 458 | |
Harald Welte | 49518bf | 2018-02-10 11:39:19 +0100 | [diff] [blame] | 459 | private function f_bssap_l3_is_rr(PDU_BSSAP bssap) return boolean { |
| 460 | var template octetstring l3 := f_bssap_extract_l3(bssap); |
| 461 | if (not isvalue(l3)) { |
| 462 | return false; |
| 463 | } |
| 464 | var octetstring l3v := valueof(l3); |
Harald Welte | 96505f0 | 2018-03-18 21:31:07 +0100 | [diff] [blame] | 465 | if (lengthof(l3v) < 1) { |
| 466 | return false; |
| 467 | } |
Harald Welte | 49518bf | 2018-02-10 11:39:19 +0100 | [diff] [blame] | 468 | /* lower 4 bits of first octet are protocol discriminator */ |
| 469 | if ((oct2bit(l3v[0]) and4b '00001111'B) == '00000110'B) { |
| 470 | return true; |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 475 | private altstep as_reset_ack() runs on BSSMAP_Emulation_CT { |
| 476 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 477 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind { |
| 478 | log("Respoding to inbound RESET with RESET-ACK"); |
| 479 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress, |
| 480 | ts_BSSMAP_ResetAck)); |
| 481 | repeat; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
| 486 | private function f_bssap_wait_for_reset() runs on BSSMAP_Emulation_CT { |
| 487 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 488 | timer T := 20.0; |
| 489 | |
| 490 | T.start; |
| 491 | alt { |
| 492 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(?, ?, tr_BSSMAP_Reset)) -> value ud_ind { |
| 493 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress, |
| 494 | ts_BSSMAP_ResetAck)); |
| 495 | } |
| 496 | [] as_reset_ack(); |
| 497 | [] BSSAP.receive { |
| 498 | repeat; |
| 499 | } |
| 500 | [] T.timeout { |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 501 | setverdict(fail, "Timeout waiting for BSSAP RESET"); |
| 502 | mtc.stop; |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | function f_bssap_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on BSSMAP_Emulation_CT { |
| 508 | timer T := 5.0; |
| 509 | |
| 510 | BSSAP.send(ts_BSSAP_UNITDATA_req(peer, own, ts_BSSMAP_Reset(0))); |
| 511 | T.start; |
| 512 | alt { |
| 513 | [] BSSAP.receive(tr_BSSAP_UNITDATA_ind(own, peer, tr_BSSMAP_ResetAck)) { |
| 514 | log("Received RESET-ACK in response to RESET, we're ready to go!"); |
| 515 | } |
| 516 | [] as_reset_ack(); |
| 517 | [] BSSAP.receive { repeat }; |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 518 | [] T.timeout { |
| 519 | setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET"); |
| 520 | mtc.stop; |
| 521 | } |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 525 | function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 526 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 527 | g_bssmap_id := id; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 528 | g_bssmap_ops := ops; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 529 | f_conn_table_init(); |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 530 | f_expect_table_init(); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 531 | |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 532 | if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) { |
Harald Welte | 7f58258 | 2018-02-14 20:20:11 +0100 | [diff] [blame] | 533 | f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */ |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 534 | f_bssap_reset(ops.sccp_addr_peer, ops.sccp_addr_local); |
| 535 | } |
| 536 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 537 | while (true) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 538 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 539 | var BSSAP_N_CONNECT_ind conn_ind; |
| 540 | var BSSAP_N_CONNECT_cfm conn_cfm; |
| 541 | var BSSAP_N_DATA_ind data_ind; |
| 542 | var BSSAP_N_DISCONNECT_ind disc_ind; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 543 | var BSSAP_Conn_Req creq; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 544 | var BSSAP_ConnHdlr vc_conn; |
| 545 | var PDU_BSSAP bssap; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 546 | var PDU_DTAP_MO dtap_mo; |
| 547 | var PDU_DTAP_MT dtap_mt; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 548 | var MgcpCommand mgcp_req; |
| 549 | var MgcpResponse mgcp_resp; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 550 | var BSSAP_ConnHdlr vc_hdlr; |
| 551 | var octetstring l3_info; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 552 | var hexstring imsi; |
| 553 | var OCT4 tmsi; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 554 | |
| 555 | alt { |
| 556 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 557 | [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 558 | /* Connectionless Procedures like RESET */ |
| 559 | var template PDU_BSSAP resp; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 560 | resp := CommonBssmapUnitdataCallback(ud_ind.userData); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 561 | if (isvalue(resp)) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 562 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, |
| 563 | ud_ind.calledAddress, resp)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | /* SCCP -> Client: new connection from BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 568 | [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind { |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 569 | vc_conn := ops.create_cb.apply(conn_ind, id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 570 | /* store mapping between client components and SCCP connectionId */ |
| 571 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 572 | /* handle user payload */ |
| 573 | f_handle_userData(vc_conn, conn_ind.userData); |
| 574 | /* confirm connection establishment */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 575 | BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* SCCP -> Client: connection-oriented data in existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 579 | [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 580 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 581 | if (ispresent(data_ind.userData)) { |
| 582 | f_handle_userData(vc_conn, data_ind.userData); |
| 583 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* SCCP -> Client: disconnect of an existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 587 | [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 588 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 589 | if (ispresent(disc_ind.userData)) { |
| 590 | f_handle_userData(vc_conn, disc_ind.userData); |
| 591 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 592 | /* notify client about termination */ |
| 593 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
| 594 | CLIENT.send(prim) to vc_conn; |
| 595 | f_conn_table_del(disc_ind.connectionId); |
| 596 | /* TOOD: return confirm to other side? */ |
| 597 | } |
| 598 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 599 | /* SCCP -> Client: connection confirm for outbound connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 600 | [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm { |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 601 | vc_conn := f_comp_by_conn_id(conn_cfm.connectionId); |
| 602 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND; |
| 603 | CLIENT.send(prim) to vc_conn; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 604 | /* handle user payload */ |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 605 | if (ispresent(conn_cfm.userData)) { |
| 606 | f_handle_userData(vc_conn, conn_cfm.userData); |
| 607 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 610 | /* Disconnect request client -> SCCP */ |
| 611 | [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 612 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 613 | BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 614 | f_conn_table_del(conn_id); |
| 615 | } |
| 616 | |
| 617 | /* BSSAP from client -> SCCP */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 618 | [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn { |
| 619 | var integer conn_id; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 620 | /* send to dispatcher */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 621 | |
| 622 | if (f_comp_known(vc_conn) == false) { |
| 623 | /* unknown client, create new connection */ |
| 624 | conn_id := f_gen_conn_id(); |
| 625 | |
| 626 | /* store mapping between client components and SCCP connectionId */ |
| 627 | f_conn_table_add(vc_conn, conn_id); |
| 628 | |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 629 | BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 630 | creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 631 | } else { |
| 632 | /* known client, send via existing connection */ |
| 633 | conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 634 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Harald Welte | 49518bf | 2018-02-10 11:39:19 +0100 | [diff] [blame] | 637 | /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment |
| 638 | * counter only on MM/CC/SS, but not on RR */ |
| 639 | if (g_bssmap_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) { |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 640 | /* we have just sent the first MM message, increment the counter */ |
| 641 | var integer idx := f_idx_by_comp(vc_conn); |
| 642 | ConnectionTable[idx].n_sd[0] := 1; |
| 643 | log("patch: N(SD) for ConnIdx ", idx, " set to 1"); |
| 644 | } |
| 645 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 648 | [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn { |
| 649 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 650 | /* send it to dispatcher */ |
| 651 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 654 | [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn { |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 655 | var integer idx := f_idx_by_comp(vc_conn); |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 656 | /* convert from decoded DTAP to encoded DTAP */ |
| 657 | var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap); |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 658 | /* patch correct L3 send sequence number N(SD) into l3_enc */ |
Daniel Willmann | 92f6627 | 2018-02-06 15:50:52 +0100 | [diff] [blame] | 659 | if (dtap_mo.skip_seq_patching == false) { |
| 660 | f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc); |
| 661 | } |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 662 | bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci)); |
Harald Welte | 9dadc52 | 2018-02-06 13:56:04 +0100 | [diff] [blame] | 663 | BSSAP.send(ts_BSSAP_DATA_req(ConnectionTable[idx].sccp_conn_id, bssap)); |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn { |
| 667 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 668 | /* convert from decoded DTAP to encoded DTAP */ |
| 669 | var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap); |
| 670 | bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci)); |
| 671 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
| 672 | } |
| 673 | |
| 674 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 675 | /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP |
| 676 | * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC |
| 677 | * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC |
| 678 | * is printed as hex string, e.g. a@mgw for CIC 10 */ |
| 679 | |
| 680 | /* CLIENT -> MGCP */ |
| 681 | [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn { |
| 682 | /* MGCP request from Handler (we're MSC) */ |
| 683 | /* store the transaction ID we've seen */ |
| 684 | f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id); |
| 685 | /* simply forward any MGCP from the client to the port */ |
| 686 | MGCP.send(mgcp_req); |
| 687 | } |
| 688 | [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn { |
| 689 | /* MGCP response from Handler (we're BSC/MGW) */ |
| 690 | /* simply forward any MGCP from the client to the port */ |
| 691 | MGCP.send(mgcp_resp); |
| 692 | } |
| 693 | |
| 694 | /* MGCP -> CLIENT */ |
| 695 | [] MGCP.receive(MgcpCommand:?) -> value mgcp_req { |
| 696 | /* MGCP request from network side (we're BSC/MGW) */ |
| 697 | /* Extract CIC from local part of endpoint name */ |
| 698 | var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep); |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 699 | if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) { |
| 700 | /* ignore RSIP for unknown CIC */ |
| 701 | } else { |
| 702 | /* Resolve the vc_conn by the CIC */ |
| 703 | vc_conn := f_comp_by_cic(cic); |
| 704 | CLIENT.send(mgcp_req) to vc_conn; |
| 705 | } |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 706 | } |
| 707 | [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp { |
| 708 | /* MGCP response from network side (we're MSC) */ |
| 709 | /* Resolve the vc_conn by the transaction ID */ |
| 710 | vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id); |
| 711 | CLIENT.send(mgcp_resp) to vc_conn; |
| 712 | } |
| 713 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 714 | |
| 715 | [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) { |
| 716 | f_create_expect(l3_info, vc_hdlr); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 717 | PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr}) to vc_hdlr; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 720 | [] PROC.getcall(BSSMAPEM_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) { |
| 721 | f_create_imsi(imsi, tmsi, vc_hdlr); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 722 | PROC.reply(BSSMAPEM_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr; |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 730 | private function f_mgcp_ep_extract_cic(charstring inp) return integer { |
Harald Welte | 525a9c1 | 2017-11-24 23:40:41 +0100 | [diff] [blame] | 731 | var charstring local_part := regexp(inp, "(*)@*", 0); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 732 | return hex2int(str2hex(local_part)); |
| 733 | |
| 734 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 735 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 736 | /*********************************************************************** |
| 737 | * "Expect" Handling (mapping for expected incoming SCCP connections) |
| 738 | ***********************************************************************/ |
| 739 | |
| 740 | /* data about an expected future incoming connection */ |
| 741 | type record ExpectData { |
| 742 | /* L3 payload based on which we can match it */ |
| 743 | octetstring l3_payload optional, |
| 744 | /* component reference for this connection */ |
| 745 | BSSAP_ConnHdlr vc_conn |
| 746 | } |
| 747 | |
| 748 | /* procedure based port to register for incoming connections */ |
| 749 | signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr); |
| 750 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 751 | /* procedure based port to register for incoming IMSI/TMSI */ |
| 752 | signature BSSMAPEM_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_ConnHdlr hdlr); |
| 753 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 754 | type port BSSMAPEM_PROC_PT procedure { |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 755 | inout BSSMAPEM_register, BSSMAPEM_register_imsi; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 756 | } with { extension "internal" }; |
| 757 | |
| 758 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 759 | function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
| 760 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 761 | var BSSAP_ConnHdlr ret := null; |
| 762 | var octetstring l3_info; |
| 763 | var integer i; |
| 764 | |
| 765 | if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) { |
| 766 | setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 767 | mtc.stop; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 768 | return ret; |
| 769 | } |
| 770 | l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info; |
| 771 | |
| 772 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
| 773 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 774 | continue; |
| 775 | } |
| 776 | if (l3_info == ExpectTable[i].l3_payload) { |
| 777 | ret := ExpectTable[i].vc_conn; |
| 778 | /* release this entry to be used again */ |
| 779 | ExpectTable[i].l3_payload := omit; |
| 780 | ExpectTable[i].vc_conn := null; |
| 781 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 782 | /* return the component reference */ |
| 783 | return ret; |
| 784 | } |
| 785 | } |
| 786 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 787 | mtc.stop; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr) |
| 792 | runs on BSSMAP_Emulation_CT { |
| 793 | var integer i; |
| 794 | for (i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 795 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 796 | ExpectTable[i].l3_payload := l3; |
| 797 | ExpectTable[i].vc_conn := hdlr; |
| 798 | log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr); |
| 799 | return; |
| 800 | } |
| 801 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 802 | testcase.stop("No space left in ExpectTable"); |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 805 | private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_ConnHdlr hdlr) |
| 806 | runs on BSSMAP_Emulation_CT { |
| 807 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 808 | if (not ispresent(ImsiTable[i].imsi)) { |
| 809 | ImsiTable[i].imsi := imsi; |
| 810 | ImsiTable[i].tmsi := tmsi; |
| 811 | ImsiTable[i].comp_ref := hdlr; |
| 812 | log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr); |
| 813 | return; |
| 814 | } |
| 815 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 816 | testcase.stop("No space left in ImsiTable"); |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 820 | private function f_expect_table_init() |
| 821 | runs on BSSMAP_Emulation_CT { |
| 822 | for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 823 | ExpectTable[i].l3_payload := omit; |
| 824 | } |
| 825 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 826 | |
Harald Welte | 17d2115 | 2018-01-27 00:47:11 +0100 | [diff] [blame] | 827 | /* helper function for clients to register their IMSI/TMSI */ |
| 828 | function f_bssmap_register_imsi(hexstring imsi, OCT4 tmsi) |
| 829 | runs on BSSAP_ConnHdlr { |
| 830 | BSSAP_PROC.call(BSSMAPEM_register_imsi:{imsi, tmsi, self}) { |
| 831 | [] BSSAP_PROC.getreply(BSSMAPEM_register_imsi:{?,?,?}) {}; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 836 | } |