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