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