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