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(); |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame^] | 300 | f_expect_table_init(); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 301 | |
| 302 | while (true) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 303 | var BSSAP_N_UNITDATA_ind ud_ind; |
| 304 | var BSSAP_N_CONNECT_ind conn_ind; |
| 305 | var BSSAP_N_CONNECT_cfm conn_cfm; |
| 306 | var BSSAP_N_DATA_ind data_ind; |
| 307 | var BSSAP_N_DISCONNECT_ind disc_ind; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 308 | var BSSAP_Conn_Req creq; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 309 | var BSSAP_ConnHdlr vc_conn; |
| 310 | var PDU_BSSAP bssap; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 311 | var MgcpCommand mgcp_req; |
| 312 | var MgcpResponse mgcp_resp; |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 313 | var BSSAP_ConnHdlr vc_hdlr; |
| 314 | var octetstring l3_info; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 315 | |
| 316 | alt { |
| 317 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 318 | [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 319 | /* Connectionless Procedures like RESET */ |
| 320 | var template PDU_BSSAP resp; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 321 | resp := ops.unitdata_cb.apply(ud_ind.userData); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 322 | if (isvalue(resp)) { |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 323 | BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress, |
| 324 | ud_ind.calledAddress, resp)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /* SCCP -> Client: new connection from BSC */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 329 | [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind { |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 330 | vc_conn := ops.create_cb.apply(conn_ind, id); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 331 | /* store mapping between client components and SCCP connectionId */ |
| 332 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 333 | /* handle user payload */ |
| 334 | f_handle_userData(vc_conn, conn_ind.userData); |
| 335 | /* confirm connection establishment */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 336 | BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* SCCP -> Client: connection-oriented data in existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 340 | [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 341 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 342 | if (ispresent(data_ind.userData)) { |
| 343 | f_handle_userData(vc_conn, data_ind.userData); |
| 344 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* SCCP -> Client: disconnect of an existing connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 348 | [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 349 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 350 | if (ispresent(disc_ind.userData)) { |
| 351 | f_handle_userData(vc_conn, disc_ind.userData); |
| 352 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 353 | /* notify client about termination */ |
| 354 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
| 355 | CLIENT.send(prim) to vc_conn; |
| 356 | f_conn_table_del(disc_ind.connectionId); |
| 357 | /* TOOD: return confirm to other side? */ |
| 358 | } |
| 359 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 360 | /* SCCP -> Client: connection confirm for outbound connection */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 361 | [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 362 | /* handle user payload */ |
Harald Welte | 5cc4aa2 | 2017-11-23 18:51:28 +0100 | [diff] [blame] | 363 | if (ispresent(conn_cfm.userData)) { |
| 364 | f_handle_userData(vc_conn, conn_cfm.userData); |
| 365 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 368 | /* Disconnect request client -> SCCP */ |
| 369 | [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 370 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 371 | BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 372 | f_conn_table_del(conn_id); |
| 373 | } |
| 374 | |
| 375 | /* BSSAP from client -> SCCP */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 376 | [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn { |
| 377 | var integer conn_id; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 378 | /* send to dispatcher */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 379 | |
| 380 | if (f_comp_known(vc_conn) == false) { |
| 381 | /* unknown client, create new connection */ |
| 382 | conn_id := f_gen_conn_id(); |
| 383 | |
| 384 | /* store mapping between client components and SCCP connectionId */ |
| 385 | f_conn_table_add(vc_conn, conn_id); |
| 386 | |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 387 | BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 388 | creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 389 | } else { |
| 390 | /* known client, send via existing connection */ |
| 391 | conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 392 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | } |
| 396 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 397 | [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn { |
| 398 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 399 | /* send it to dispatcher */ |
| 400 | BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 403 | /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP |
| 404 | * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC |
| 405 | * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC |
| 406 | * is printed as hex string, e.g. a@mgw for CIC 10 */ |
| 407 | |
| 408 | /* CLIENT -> MGCP */ |
| 409 | [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn { |
| 410 | /* MGCP request from Handler (we're MSC) */ |
| 411 | /* store the transaction ID we've seen */ |
| 412 | f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id); |
| 413 | /* simply forward any MGCP from the client to the port */ |
| 414 | MGCP.send(mgcp_req); |
| 415 | } |
| 416 | [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn { |
| 417 | /* MGCP response from Handler (we're BSC/MGW) */ |
| 418 | /* simply forward any MGCP from the client to the port */ |
| 419 | MGCP.send(mgcp_resp); |
| 420 | } |
| 421 | |
| 422 | /* MGCP -> CLIENT */ |
| 423 | [] MGCP.receive(MgcpCommand:?) -> value mgcp_req { |
| 424 | /* MGCP request from network side (we're BSC/MGW) */ |
| 425 | /* Extract CIC from local part of endpoint name */ |
| 426 | var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep); |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 427 | if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) { |
| 428 | /* ignore RSIP for unknown CIC */ |
| 429 | } else { |
| 430 | /* Resolve the vc_conn by the CIC */ |
| 431 | vc_conn := f_comp_by_cic(cic); |
| 432 | CLIENT.send(mgcp_req) to vc_conn; |
| 433 | } |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 434 | } |
| 435 | [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp { |
| 436 | /* MGCP response from network side (we're MSC) */ |
| 437 | /* Resolve the vc_conn by the transaction ID */ |
| 438 | vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id); |
| 439 | CLIENT.send(mgcp_resp) to vc_conn; |
| 440 | } |
| 441 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 442 | |
| 443 | [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) { |
| 444 | f_create_expect(l3_info, vc_hdlr); |
| 445 | PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr}); |
| 446 | } |
| 447 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 452 | private function f_mgcp_ep_extract_cic(charstring inp) return integer { |
Harald Welte | 525a9c1 | 2017-11-24 23:40:41 +0100 | [diff] [blame] | 453 | var charstring local_part := regexp(inp, "(*)@*", 0); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 454 | return hex2int(str2hex(local_part)); |
| 455 | |
| 456 | } |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 457 | |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 458 | /*********************************************************************** |
| 459 | * "Expect" Handling (mapping for expected incoming SCCP connections) |
| 460 | ***********************************************************************/ |
| 461 | |
| 462 | /* data about an expected future incoming connection */ |
| 463 | type record ExpectData { |
| 464 | /* L3 payload based on which we can match it */ |
| 465 | octetstring l3_payload optional, |
| 466 | /* component reference for this connection */ |
| 467 | BSSAP_ConnHdlr vc_conn |
| 468 | } |
| 469 | |
| 470 | /* procedure based port to register for incoming connections */ |
| 471 | signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr); |
| 472 | |
| 473 | type port BSSMAPEM_PROC_PT procedure { |
| 474 | inout BSSMAPEM_register; |
| 475 | } with { extension "internal" }; |
| 476 | |
| 477 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 478 | function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
| 479 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 480 | var BSSAP_ConnHdlr ret := null; |
| 481 | var octetstring l3_info; |
| 482 | var integer i; |
| 483 | |
| 484 | if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) { |
| 485 | setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3"); |
| 486 | return ret; |
| 487 | } |
| 488 | l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info; |
| 489 | |
| 490 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
| 491 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 492 | continue; |
| 493 | } |
| 494 | if (l3_info == ExpectTable[i].l3_payload) { |
| 495 | ret := ExpectTable[i].vc_conn; |
| 496 | /* release this entry to be used again */ |
| 497 | ExpectTable[i].l3_payload := omit; |
| 498 | ExpectTable[i].vc_conn := null; |
| 499 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 500 | /* return the component reference */ |
| 501 | return ret; |
| 502 | } |
| 503 | } |
| 504 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr) |
| 509 | runs on BSSMAP_Emulation_CT { |
| 510 | var integer i; |
| 511 | for (i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 512 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 513 | ExpectTable[i].l3_payload := l3; |
| 514 | ExpectTable[i].vc_conn := hdlr; |
| 515 | log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr); |
| 516 | return; |
| 517 | } |
| 518 | } |
| 519 | setverdict(fail, "No space left in ExpectTable"); |
| 520 | } |
| 521 | |
Daniel Willmann | d47106b | 2018-01-17 12:20:56 +0100 | [diff] [blame^] | 522 | private function f_expect_table_init() |
| 523 | runs on BSSMAP_Emulation_CT { |
| 524 | for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 525 | ExpectTable[i].l3_payload := omit; |
| 526 | } |
| 527 | } |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 528 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 529 | } |