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 | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 34 | import from SCCP_Emulation all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 35 | import from SCCPasp_Types all; |
| 36 | import from BSSAP_Types all; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 37 | import from BSSAP_CodecPort all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 38 | import from BSSMAP_Templates all; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 39 | import from MGCP_Types all; |
| 40 | import from MGCP_Templates all; |
| 41 | import from IPA_Emulation all; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 42 | import from MobileL3_Types all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 43 | |
| 44 | /* General "base class" component definition, of which specific implementations |
| 45 | * derive themselves by means of the "extends" feature */ |
| 46 | type component BSSAP_ConnHdlr { |
| 47 | /* port towards MSC Emulator core / SCCP connection dispatchar */ |
| 48 | port BSSAP_Conn_PT BSSAP; |
Harald Welte | ca51998 | 2018-01-21 19:28:26 +0100 | [diff] [blame] | 49 | /* procedure based port to register for incoming connections */ |
| 50 | port BSSMAPEM_PROC_PT BSSAP_PROC; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
| 54 | type enumerated BSSAP_Conn_Prim { |
| 55 | /* SCCP tell us that connection was released */ |
| 56 | MSC_CONN_PRIM_DISC_IND, |
| 57 | /* we tell SCCP to release connection */ |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 58 | MSC_CONN_PRIM_DISC_REQ, |
| 59 | /* Connection confirmed indication */ |
| 60 | MSC_CONN_PRIM_CONF_IND |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 63 | type record BSSAP_Conn_Req { |
| 64 | SCCP_PAR_Address addr_peer, |
| 65 | SCCP_PAR_Address addr_own, |
| 66 | PDU_BSSAP bssap |
| 67 | } |
| 68 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 69 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 70 | type record PDU_DTAP_MO { |
| 71 | OCT1 dlci optional, |
| 72 | PDU_ML3_MS_NW dtap |
| 73 | } |
| 74 | |
| 75 | /* similar to PDU_BSSAP with DTAP, but DTAP is already decoded! */ |
| 76 | type record PDU_DTAP_MT { |
| 77 | OCT1 dlci optional, |
| 78 | PDU_ML3_NW_MS dtap |
| 79 | } |
| 80 | |
| 81 | template PDU_DTAP_MT ts_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := omit) := { |
| 82 | dlci := dlci, |
| 83 | dtap := dtap |
| 84 | } |
| 85 | |
| 86 | template PDU_DTAP_MO ts_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := '00'O) := { |
| 87 | dlci := dlci, |
| 88 | dtap := dtap |
| 89 | } |
| 90 | |
| 91 | template PDU_DTAP_MT tr_PDU_DTAP_MT(template PDU_ML3_NW_MS dtap, template OCT1 dlci := *) := { |
| 92 | dlci := dlci, |
| 93 | dtap := dtap |
| 94 | } |
| 95 | |
| 96 | template PDU_DTAP_MO tr_PDU_DTAP_MO(template PDU_ML3_MS_NW dtap, template OCT1 dlci := *) := { |
| 97 | dlci := dlci, |
| 98 | dtap := dtap |
| 99 | } |
| 100 | |
| 101 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 102 | /* port between individual per-connection components and this dispatcher */ |
| 103 | type port BSSAP_Conn_PT message { |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 104 | 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] | 105 | } with { extension "internal" }; |
| 106 | |
| 107 | |
| 108 | /* represents a single BSSAP connection over SCCP */ |
| 109 | type record ConnectionData { |
| 110 | /* reference to the instance of the per-connection component */ |
| 111 | BSSAP_ConnHdlr comp_ref, |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 112 | integer sccp_conn_id, |
| 113 | /* most recent MGCP transaction ID (Used on MSC side) */ |
| 114 | MgcpTransId mgcp_trans_id optional, |
| 115 | /* CIC that has been used for voice of this channel (BSC side) */ |
| 116 | integer cic optional |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | type component BSSMAP_Emulation_CT { |
| 120 | /* SCCP port on the bottom side, using ASP primitives */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 121 | port BSSAP_CODEC_PT BSSAP; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 122 | /* BSSAP port to the per-connection clients */ |
| 123 | port BSSAP_Conn_PT CLIENT; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 124 | /* MGCP port */ |
| 125 | port IPA_MGCP_PT MGCP; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 126 | |
| 127 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 128 | var ConnectionData ConnectionTable[16]; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 129 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 130 | /* pending expected incoming connections */ |
| 131 | var ExpectData ExpectTable[8]; |
| 132 | /* procedure based port to register for incoming connections */ |
| 133 | port BSSMAPEM_PROC_PT PROC; |
| 134 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 135 | var charstring g_bssmap_id; |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 136 | var integer g_next_e1_ts := 1; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 137 | var BssmapOps g_bssmap_ops; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 140 | private function f_conn_id_known(integer sccp_conn_id) |
| 141 | runs on BSSMAP_Emulation_CT return boolean { |
| 142 | var integer i; |
| 143 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 144 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){ |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | private function f_comp_known(BSSAP_ConnHdlr client) |
| 152 | runs on BSSMAP_Emulation_CT return boolean { |
| 153 | var integer i; |
| 154 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 155 | if (ConnectionTable[i].comp_ref == client) { |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 161 | |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 162 | private function f_cic_known(integer cic) |
| 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].cic == cic) { |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 173 | /* resolve component reference by connection ID */ |
| 174 | private function f_comp_by_conn_id(integer sccp_conn_id) |
| 175 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 176 | var integer i; |
| 177 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 178 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 179 | return ConnectionTable[i].comp_ref; |
| 180 | } |
| 181 | } |
| 182 | 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] | 183 | setverdict(fail); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 184 | self.stop; |
| 185 | } |
| 186 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 187 | /* resolve component reference by CIC */ |
| 188 | private function f_comp_by_mgcp_tid(MgcpTransId tid) |
| 189 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 190 | var integer i; |
| 191 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 192 | if (ConnectionTable[i].mgcp_trans_id == tid) { |
| 193 | return ConnectionTable[i].comp_ref; |
| 194 | } |
| 195 | } |
| 196 | log("BSSMAP Connection table not found by MGCP Transaction ID ", tid); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 197 | setverdict(fail); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 198 | self.stop; |
| 199 | } |
| 200 | |
| 201 | private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid) |
| 202 | runs on BSSMAP_Emulation_CT { |
| 203 | var integer i; |
| 204 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 205 | if (ConnectionTable[i].comp_ref == client) { |
| 206 | ConnectionTable[i].mgcp_trans_id := tid; |
| 207 | return; |
| 208 | } |
| 209 | } |
| 210 | log("BSSMAP Connection table not found by component ", client); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 211 | setverdict(fail); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 212 | self.stop; |
| 213 | } |
| 214 | |
| 215 | private function f_comp_by_cic(integer cic) |
| 216 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 217 | var integer i; |
| 218 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 219 | if (ConnectionTable[i].cic == cic) { |
| 220 | return ConnectionTable[i].comp_ref; |
| 221 | } |
| 222 | } |
| 223 | log("BSSMAP Connection table not found by CIC ", cic); |
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_store_cic(BSSAP_ConnHdlr client, integer cic) |
| 229 | runs on BSSMAP_Emulation_CT { |
| 230 | var integer i; |
| 231 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 232 | if (ConnectionTable[i].comp_ref == client) { |
| 233 | ConnectionTable[i].cic := cic; |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | log("BSSMAP Connection table not found by component ", client); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 238 | setverdict(fail); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 241 | /* resolve connection ID by component reference */ |
| 242 | private function f_conn_id_by_comp(BSSAP_ConnHdlr client) |
| 243 | runs on BSSMAP_Emulation_CT return integer { |
| 244 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 245 | if (ConnectionTable[i].comp_ref == client) { |
| 246 | return ConnectionTable[i].sccp_conn_id; |
| 247 | } |
| 248 | } |
| 249 | log("BSSMAP Connection table not found by component ", client); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 250 | setverdict(fail); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 251 | self.stop; |
| 252 | } |
| 253 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 254 | private function f_gen_conn_id() |
| 255 | runs on BSSMAP_Emulation_CT return integer { |
| 256 | var integer conn_id; |
| 257 | |
| 258 | do { |
| 259 | conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId); |
| 260 | } while (f_conn_id_known(conn_id) == true); |
| 261 | |
| 262 | return conn_id; |
| 263 | } |
| 264 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 265 | private function f_conn_table_init() |
| 266 | runs on BSSMAP_Emulation_CT { |
| 267 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 268 | ConnectionTable[i].comp_ref := null; |
| 269 | ConnectionTable[i].sccp_conn_id := -1; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 270 | ConnectionTable[i].mgcp_trans_id := omit; |
| 271 | ConnectionTable[i].cic := omit; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id) |
| 276 | runs on BSSMAP_Emulation_CT { |
| 277 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 278 | if (ConnectionTable[i].sccp_conn_id == -1) { |
| 279 | ConnectionTable[i].comp_ref := comp_ref; |
| 280 | ConnectionTable[i].sccp_conn_id := sccp_conn_id; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 281 | log("Added conn table entry ", i, comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | } |
| 285 | log("BSSMAP Connection table full!"); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 286 | setverdict(fail); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 287 | self.stop; |
| 288 | } |
| 289 | |
| 290 | private function f_conn_table_del(integer sccp_conn_id) |
| 291 | runs on BSSMAP_Emulation_CT { |
| 292 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 293 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 294 | log("Deleted conn table entry ", i, |
| 295 | ConnectionTable[i].comp_ref, sccp_conn_id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 296 | ConnectionTable[i].sccp_conn_id := -1; |
| 297 | ConnectionTable[i].comp_ref := null; |
Harald Welte | 0a4317a | 2017-11-25 00:32:46 +0100 | [diff] [blame] | 298 | return |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id); |
Harald Welte | 9ca9eb1 | 2017-11-25 00:50:43 +0100 | [diff] [blame] | 302 | setverdict(fail); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 303 | self.stop; |
| 304 | } |
| 305 | |
| 306 | /* 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] | 307 | private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap) |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 308 | runs on BSSMAP_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 309 | /* decode + send decoded BSSAP to client */ |
Harald Welte | 1b2748e | 2017-11-24 23:40:16 +0100 | [diff] [blame] | 310 | |
| 311 | /* BSC Side: If this is an assignment command, store CIC */ |
| 312 | if (ischosen(bssap.pdu.bssmap.assignmentRequest) and |
| 313 | ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) { |
| 314 | var BSSMAP_IE_CircuitIdentityCode cic_ie := |
| 315 | bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode; |
| 316 | var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow); |
| 317 | f_comp_store_cic(client, cic); |
| 318 | } |
| 319 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 320 | if (ischosen(bssap.pdu.dtap) and g_bssmap_ops.decode_dtap) { |
| 321 | if (g_bssmap_ops.role_ms) { |
| 322 | /* we are the MS, so any message to us must be MT */ |
| 323 | var PDU_DTAP_MT mt := { |
| 324 | dlci := bssap.dlci, |
| 325 | dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap) |
| 326 | }; |
| 327 | CLIENT.send(mt) to client; |
| 328 | } else { |
| 329 | /* we are the Network, so any message to us must be MO */ |
| 330 | var PDU_DTAP_MO mo := { |
| 331 | dlci := bssap.dlci, |
| 332 | dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap) |
| 333 | }; |
| 334 | CLIENT.send(mo) to client; |
| 335 | } |
| 336 | } else { |
| 337 | CLIENT.send(bssap) to client; |
| 338 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 342 | * arrives */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 343 | type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 344 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr; |
| 345 | |
| 346 | type function BssmapUnitdataCallback(PDU_BSSAP bssap) |
| 347 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP; |
| 348 | |
| 349 | type record BssmapOps { |
| 350 | BssmapCreateCallback create_cb, |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 351 | BssmapUnitdataCallback unitdata_cb, |
| 352 | boolean decode_dtap, |
| 353 | boolean role_ms |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 356 | function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 357 | |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 358 | g_bssmap_id := id; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 359 | g_bssmap_ops := ops; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 360 | f_conn_table_init(); |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 361 | f_expect_table_init(); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 362 | |
| 363 | while (true) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 364 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 365 | var BSSAP_N_CONNECT_ind conn_ind; |
| 366 | var BSSAP_N_CONNECT_cfm conn_cfm; |
| 367 | var BSSAP_N_DATA_ind data_ind; |
| 368 | var BSSAP_N_DISCONNECT_ind disc_ind; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 369 | var BSSAP_Conn_Req creq; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 370 | var BSSAP_ConnHdlr vc_conn; |
| 371 | var PDU_BSSAP bssap; |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 372 | var PDU_DTAP_MO dtap_mo; |
| 373 | var PDU_DTAP_MT dtap_mt; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 374 | var MgcpCommand mgcp_req; |
| 375 | var MgcpResponse mgcp_resp; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 376 | var BSSAP_ConnHdlr vc_hdlr; |
| 377 | var octetstring l3_info; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 378 | |
| 379 | alt { |
| 380 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 381 | [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 382 | /* Connectionless Procedures like RESET */ |
| 383 | var template PDU_BSSAP resp; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 384 | resp := ops.unitdata_cb.apply(ud_ind.userData); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 385 | if (isvalue(resp)) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 386 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, |
| 387 | ud_ind.calledAddress, resp)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | /* SCCP -> Client: new connection from BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 392 | [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind { |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 393 | vc_conn := ops.create_cb.apply(conn_ind, id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 394 | /* store mapping between client components and SCCP connectionId */ |
| 395 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 396 | /* handle user payload */ |
| 397 | f_handle_userData(vc_conn, conn_ind.userData); |
| 398 | /* confirm connection establishment */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 399 | BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* SCCP -> Client: connection-oriented data in existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 403 | [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 404 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 405 | if (ispresent(data_ind.userData)) { |
| 406 | f_handle_userData(vc_conn, data_ind.userData); |
| 407 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* SCCP -> Client: disconnect of an existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 411 | [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 412 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 413 | if (ispresent(disc_ind.userData)) { |
| 414 | f_handle_userData(vc_conn, disc_ind.userData); |
| 415 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 416 | /* notify client about termination */ |
| 417 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
| 418 | CLIENT.send(prim) to vc_conn; |
| 419 | f_conn_table_del(disc_ind.connectionId); |
| 420 | /* TOOD: return confirm to other side? */ |
| 421 | } |
| 422 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 423 | /* SCCP -> Client: connection confirm for outbound connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 424 | [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm { |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 425 | vc_conn := f_comp_by_conn_id(conn_cfm.connectionId); |
| 426 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_CONF_IND; |
| 427 | CLIENT.send(prim) to vc_conn; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 428 | /* handle user payload */ |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 429 | if (ispresent(conn_cfm.userData)) { |
| 430 | f_handle_userData(vc_conn, conn_cfm.userData); |
| 431 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 434 | /* Disconnect request client -> SCCP */ |
| 435 | [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 436 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 437 | BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 438 | f_conn_table_del(conn_id); |
| 439 | } |
| 440 | |
| 441 | /* BSSAP from client -> SCCP */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 442 | [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn { |
| 443 | var integer conn_id; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 444 | /* send to dispatcher */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 445 | |
| 446 | if (f_comp_known(vc_conn) == false) { |
| 447 | /* unknown client, create new connection */ |
| 448 | conn_id := f_gen_conn_id(); |
| 449 | |
| 450 | /* store mapping between client components and SCCP connectionId */ |
| 451 | f_conn_table_add(vc_conn, conn_id); |
| 452 | |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 453 | BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 454 | creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 455 | } else { |
| 456 | /* known client, send via existing connection */ |
| 457 | conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 458 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | } |
| 462 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 463 | [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn { |
| 464 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 465 | /* send it to dispatcher */ |
| 466 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 469 | [g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn { |
| 470 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 471 | /* convert from decoded DTAP to encoded DTAP */ |
| 472 | var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap); |
| 473 | bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci)); |
| 474 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
| 475 | } |
| 476 | |
| 477 | [not g_bssmap_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn { |
| 478 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 479 | /* convert from decoded DTAP to encoded DTAP */ |
| 480 | var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap); |
| 481 | bssap := valueof(ts_BSSAP_DTAP(l3_enc, dtap_mo.dlci)); |
| 482 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
| 483 | } |
| 484 | |
| 485 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 486 | /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP |
| 487 | * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC |
| 488 | * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC |
| 489 | * is printed as hex string, e.g. a@mgw for CIC 10 */ |
| 490 | |
| 491 | /* CLIENT -> MGCP */ |
| 492 | [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn { |
| 493 | /* MGCP request from Handler (we're MSC) */ |
| 494 | /* store the transaction ID we've seen */ |
| 495 | f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id); |
| 496 | /* simply forward any MGCP from the client to the port */ |
| 497 | MGCP.send(mgcp_req); |
| 498 | } |
| 499 | [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn { |
| 500 | /* MGCP response from Handler (we're BSC/MGW) */ |
| 501 | /* simply forward any MGCP from the client to the port */ |
| 502 | MGCP.send(mgcp_resp); |
| 503 | } |
| 504 | |
| 505 | /* MGCP -> CLIENT */ |
| 506 | [] MGCP.receive(MgcpCommand:?) -> value mgcp_req { |
| 507 | /* MGCP request from network side (we're BSC/MGW) */ |
| 508 | /* Extract CIC from local part of endpoint name */ |
| 509 | var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep); |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 510 | if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) { |
| 511 | /* ignore RSIP for unknown CIC */ |
| 512 | } else { |
| 513 | /* Resolve the vc_conn by the CIC */ |
| 514 | vc_conn := f_comp_by_cic(cic); |
| 515 | CLIENT.send(mgcp_req) to vc_conn; |
| 516 | } |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 517 | } |
| 518 | [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp { |
| 519 | /* MGCP response from network side (we're MSC) */ |
| 520 | /* Resolve the vc_conn by the transaction ID */ |
| 521 | vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id); |
| 522 | CLIENT.send(mgcp_resp) to vc_conn; |
| 523 | } |
| 524 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 525 | |
| 526 | [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) { |
| 527 | f_create_expect(l3_info, vc_hdlr); |
| 528 | PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr}); |
| 529 | } |
| 530 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 535 | private function f_mgcp_ep_extract_cic(charstring inp) return integer { |
Harald Welte | 525a9c1 | 2017-11-24 23:40:41 +0100 | [diff] [blame] | 536 | var charstring local_part := regexp(inp, "(*)@*", 0); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 537 | return hex2int(str2hex(local_part)); |
| 538 | |
| 539 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 540 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 541 | /*********************************************************************** |
| 542 | * "Expect" Handling (mapping for expected incoming SCCP connections) |
| 543 | ***********************************************************************/ |
| 544 | |
| 545 | /* data about an expected future incoming connection */ |
| 546 | type record ExpectData { |
| 547 | /* L3 payload based on which we can match it */ |
| 548 | octetstring l3_payload optional, |
| 549 | /* component reference for this connection */ |
| 550 | BSSAP_ConnHdlr vc_conn |
| 551 | } |
| 552 | |
| 553 | /* procedure based port to register for incoming connections */ |
| 554 | signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr); |
| 555 | |
| 556 | type port BSSMAPEM_PROC_PT procedure { |
| 557 | inout BSSMAPEM_register; |
| 558 | } with { extension "internal" }; |
| 559 | |
| 560 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 561 | function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
| 562 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 563 | var BSSAP_ConnHdlr ret := null; |
| 564 | var octetstring l3_info; |
| 565 | var integer i; |
| 566 | |
| 567 | if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) { |
| 568 | setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3"); |
| 569 | return ret; |
| 570 | } |
| 571 | l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info; |
| 572 | |
| 573 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
| 574 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 575 | continue; |
| 576 | } |
| 577 | if (l3_info == ExpectTable[i].l3_payload) { |
| 578 | ret := ExpectTable[i].vc_conn; |
| 579 | /* release this entry to be used again */ |
| 580 | ExpectTable[i].l3_payload := omit; |
| 581 | ExpectTable[i].vc_conn := null; |
| 582 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 583 | /* return the component reference */ |
| 584 | return ret; |
| 585 | } |
| 586 | } |
| 587 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr) |
| 592 | runs on BSSMAP_Emulation_CT { |
| 593 | var integer i; |
| 594 | for (i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 595 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 596 | ExpectTable[i].l3_payload := l3; |
| 597 | ExpectTable[i].vc_conn := hdlr; |
| 598 | log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr); |
| 599 | return; |
| 600 | } |
| 601 | } |
| 602 | setverdict(fail, "No space left in ExpectTable"); |
| 603 | } |
| 604 | |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame] | 605 | private function f_expect_table_init() |
| 606 | runs on BSSMAP_Emulation_CT { |
| 607 | for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 608 | ExpectTable[i].l3_payload := omit; |
| 609 | } |
| 610 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 611 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 612 | } |