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