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