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