Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 1 | module BSSAP_LE_Emulation { |
| 2 | |
| 3 | /* BSSAP_LE Emulation, runs on top of BSSAP_LE_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 BSSAP_LE_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_LE_Conn_Req primitive |
| 11 | * to the component running the BSSAP_LE_Emulation.main() function. |
| 12 | * |
| 13 | * For each new inbound connections, the BssapLeOps.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 BssapLeOps.unitdata_cb() callback, |
| 23 | * which is registered with an argument to the main() function below. |
| 24 | * |
| 25 | * (C) 2017-2020 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 | |
| 33 | import from General_Types all; |
| 34 | import from Osmocom_Types all; |
| 35 | import from SCCP_Emulation all; |
| 36 | import from SCCPasp_Types all; |
| 37 | import from IPA_Emulation all; |
| 38 | import from MobileL3_Types all; |
| 39 | import from Misc_Helpers all; |
| 40 | |
| 41 | import from BSSAP_LE_Types all; |
| 42 | import from BSSAP_LE_CodecPort all; |
| 43 | import from BSSMAP_LE_Templates all; |
| 44 | import from BSSMAP_Templates all; |
| 45 | import from RAN_Emulation all; |
| 46 | |
| 47 | /* General "base class" component definition, of which specific implementations |
| 48 | * derive themselves by means of the "extends" feature */ |
| 49 | type component BSSAP_LE_ConnHdlr { |
| 50 | /* port towards MSC Emulator core / SCCP connection dispatchar */ |
| 51 | port BSSAP_LE_Conn_PT BSSAP_LE; |
| 52 | /* procedure based port to register for incoming connections */ |
| 53 | port BSSAP_LE_PROC_PT BSSAP_LE_PROC; |
| 54 | } |
| 55 | |
| 56 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
| 57 | type enumerated BSSAP_LE_Conn_Prim { |
| 58 | /* SCCP tell us that connection was released */ |
| 59 | CONN_PRIM_DISC_IND, |
| 60 | /* we tell SCCP to release connection */ |
| 61 | CONN_PRIM_DISC_REQ, |
| 62 | /* Connection confirmed indication */ |
| 63 | CONN_PRIM_CONF_IND |
| 64 | } |
| 65 | |
| 66 | /* port between individual per-connection components and this dispatcher */ |
| 67 | type port BSSAP_LE_Conn_PT message { |
| 68 | inout |
| 69 | PDU_BSSAP_LE, |
| 70 | BSSAP_LE_N_UNITDATA_req, |
| 71 | /* Client requests us to create SCCP Connection */ |
| 72 | BSSAP_LE_Conn_Req, |
| 73 | /* direct DTAP messages from/to clients */ |
| 74 | PDU_DTAP_MO, PDU_DTAP_MT, |
| 75 | PDU_DTAP_PS_MO, PDU_DTAP_PS_MT, |
| 76 | /* misc indications / requests between SCCP and client */ |
| 77 | BSSAP_LE_Conn_Prim; |
| 78 | } with { extension "internal" }; |
| 79 | |
| 80 | type uint2_t N_Sd_Array[4]; |
| 81 | |
| 82 | /* represents a single BSSAP connection over SCCP */ |
| 83 | type record ConnectionData { |
| 84 | /* reference to the instance of the per-connection component */ |
| 85 | BSSAP_LE_ConnHdlr comp_ref, |
| 86 | integer sccp_conn_id, |
| 87 | /* array of N(SD) values for MO DTAP messages, indexed by discriminator */ |
| 88 | N_Sd_Array n_sd |
| 89 | } |
| 90 | |
| 91 | type record ImsiMapping { |
| 92 | BSSAP_LE_ConnHdlr comp_ref, |
| 93 | hexstring imsi optional, |
| 94 | OCT4 tmsi |
| 95 | } |
| 96 | |
| 97 | type component BSSAP_LE_Emulation_CT { |
| 98 | /* SCCP ports on the bottom side, using ASP primitives */ |
| 99 | port BSSAP_LE_CODEC_PT BSSAP_LE; |
| 100 | /* BSSAP port to the per-connection clients */ |
| 101 | port BSSAP_LE_Conn_PT CLIENT; |
| 102 | |
| 103 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 104 | var ConnectionData ConnectionTable[16]; |
| 105 | |
| 106 | /* pending expected incoming connections */ |
| 107 | var ExpectData ExpectTable[8]; |
| 108 | |
| 109 | /* tables for mapping inbound unitdata (like paging) */ |
| 110 | var ImsiMapping ImsiTable[16]; |
| 111 | |
| 112 | /* procedure based port to register for incoming connections */ |
| 113 | port BSSAP_LE_PROC_PT PROC; |
| 114 | |
| 115 | var charstring g_ran_id; |
| 116 | var integer g_next_e1_ts := 1; |
| 117 | var BssapLeOps g_ran_ops; |
| 118 | }; |
| 119 | |
| 120 | private function f_conn_id_known(integer sccp_conn_id) |
| 121 | runs on BSSAP_LE_Emulation_CT return boolean { |
| 122 | var integer i; |
| 123 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 124 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){ |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | private function f_comp_known(BSSAP_LE_ConnHdlr client) |
| 132 | runs on BSSAP_LE_Emulation_CT return boolean { |
| 133 | var integer i; |
| 134 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 135 | if (ConnectionTable[i].comp_ref == client) { |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | /* resolve component reference by connection ID */ |
| 143 | private function f_comp_by_conn_id(integer sccp_conn_id) |
| 144 | runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr { |
| 145 | var integer i; |
| 146 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 147 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 148 | return ConnectionTable[i].comp_ref; |
| 149 | } |
| 150 | } |
| 151 | setverdict(fail, "BSSAP-LE Connection table not found by SCCP Connection ID ", sccp_conn_id); |
| 152 | mtc.stop; |
| 153 | } |
| 154 | |
| 155 | /* resolve connection ID by component reference */ |
| 156 | private function f_conn_id_by_comp(BSSAP_LE_ConnHdlr client) |
| 157 | runs on BSSAP_LE_Emulation_CT return integer { |
| 158 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 159 | if (ConnectionTable[i].comp_ref == client) { |
| 160 | return ConnectionTable[i].sccp_conn_id; |
| 161 | } |
| 162 | } |
| 163 | setverdict(fail, "BSSAP-LE Connection table not found by component ", client); |
| 164 | mtc.stop; |
| 165 | } |
| 166 | |
| 167 | /* resolve ConnectionTable index component reference */ |
| 168 | private function f_idx_by_comp(BSSAP_LE_ConnHdlr client) |
| 169 | runs on BSSAP_LE_Emulation_CT return integer { |
| 170 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 171 | if (ConnectionTable[i].comp_ref == client) { |
| 172 | return i; |
| 173 | } |
| 174 | } |
| 175 | setverdict(fail, "BSSAP-LE Connection table not found by component ", client); |
| 176 | mtc.stop; |
| 177 | } |
| 178 | |
| 179 | private function f_gen_conn_id() |
| 180 | runs on BSSAP_LE_Emulation_CT return integer { |
| 181 | var integer conn_id; |
| 182 | |
| 183 | do { |
| 184 | conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId); |
| 185 | } while (f_conn_id_known(conn_id) == true); |
| 186 | |
| 187 | return conn_id; |
| 188 | } |
| 189 | |
| 190 | private function f_conn_table_init() |
| 191 | runs on BSSAP_LE_Emulation_CT { |
| 192 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 193 | ConnectionTable[i].comp_ref := null; |
| 194 | ConnectionTable[i].sccp_conn_id := -1; |
| 195 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
| 196 | } |
| 197 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 198 | ImsiTable[i].comp_ref := null; |
| 199 | ImsiTable[i].imsi := omit; |
| 200 | ImsiTable[i].tmsi := 'FFFFFFFF'O; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private function f_conn_table_add(BSSAP_LE_ConnHdlr comp_ref, integer sccp_conn_id) |
| 205 | runs on BSSAP_LE_Emulation_CT { |
| 206 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 207 | if (ConnectionTable[i].sccp_conn_id == -1) { |
| 208 | ConnectionTable[i].comp_ref := comp_ref; |
| 209 | ConnectionTable[i].sccp_conn_id := sccp_conn_id; |
| 210 | ConnectionTable[i].n_sd := { 0, 0, 0, 0 }; |
| 211 | log("Added conn table entry ", i, comp_ref, sccp_conn_id); |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | testcase.stop("BSSAP-LE Connection table full!"); |
| 216 | } |
| 217 | |
| 218 | private function f_conn_table_del(integer sccp_conn_id) |
| 219 | runs on BSSAP_LE_Emulation_CT { |
| 220 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 221 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 222 | log("Deleted conn table entry ", i, |
| 223 | ConnectionTable[i].comp_ref, sccp_conn_id); |
| 224 | ConnectionTable[i].sccp_conn_id := -1; |
| 225 | ConnectionTable[i].comp_ref := null; |
| 226 | return |
| 227 | } |
| 228 | } |
| 229 | setverdict(fail, "BSSAP-LE Connection table attempt to delete non-existant ", sccp_conn_id); |
| 230 | mtc.stop; |
| 231 | } |
| 232 | |
| 233 | private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi) |
| 234 | runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr { |
| 235 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 236 | if (ImsiTable[i].imsi == imsi or |
| 237 | isvalue(tmsi) and match(ImsiTable[i].tmsi, tmsi)) { |
| 238 | return ImsiTable[i].comp_ref; |
| 239 | } |
| 240 | } |
| 241 | return null; |
| 242 | } |
| 243 | |
| 244 | type record BSSAP_LE_Conn_Req { |
| 245 | SCCP_PAR_Address addr_peer, |
| 246 | SCCP_PAR_Address addr_own, |
| 247 | PDU_BSSAP_LE bssap |
| 248 | } |
| 249 | template BSSAP_LE_Conn_Req ts_BSSAP_LE_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP_LE bssap) := { |
| 250 | addr_peer := peer, |
| 251 | addr_own := own, |
| 252 | bssap := bssap |
| 253 | }; |
| 254 | |
| 255 | /* handle (optional) userData portion of various primitives and dispatch it to the client */ |
| 256 | private function f_handle_userData(BSSAP_LE_ConnHdlr client, PDU_BSSAP_LE bssap) |
| 257 | runs on BSSAP_LE_Emulation_CT { |
| 258 | /* decode + send decoded BSSAP to client */ |
| 259 | |
| 260 | if (ischosen(bssap.pdu.dtap) and g_ran_ops.decode_dtap) { |
| 261 | if (g_ran_ops.role_ms) { |
| 262 | /* we are the MS, so any message to us must be MT */ |
| 263 | var PDU_DTAP_MT mt := { |
| 264 | dlci := bssap.dlci, |
| 265 | dtap := dec_PDU_ML3_NW_MS(bssap.pdu.dtap) |
| 266 | }; |
| 267 | CLIENT.send(mt) to client; |
| 268 | } else { |
| 269 | /* we are the Network, so any message to us must be MO */ |
| 270 | var PDU_DTAP_MO mo := { |
| 271 | dlci := bssap.dlci, |
| 272 | dtap := dec_PDU_ML3_MS_NW(bssap.pdu.dtap) |
| 273 | }; |
| 274 | CLIENT.send(mo) to client; |
| 275 | } |
| 276 | } else { |
| 277 | CLIENT.send(bssap) to client; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 282 | * arrives */ |
| 283 | type function BssmapLeCreateCallback(BSSAP_LE_N_CONNECT_ind conn_ind, charstring id) |
| 284 | runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr; |
| 285 | |
| 286 | type function BssmapLeUnitdataCallback(PDU_BSSAP_LE bssap) |
| 287 | runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE; |
| 288 | |
| 289 | /* handle common Unitdata such as Paging */ |
| 290 | private function CommonBssmapUnitdataCallback(PDU_BSSAP_LE bssap) |
| 291 | runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE { |
| 292 | if (match(bssap, tr_BSSMAP_LE_Paging)) { |
| 293 | var BSSAP_LE_ConnHdlr client := null; |
| 294 | client := f_imsi_table_find(bssap.pdu.bssmap.paging.iMSI.digits, |
| 295 | bssap.pdu.bssmap.paging.tMSI.tmsiOctets); |
| 296 | if (client != null) { |
| 297 | log("CommonBssmapUnitdataCallback: IMSI/TMSI found in table, dispatching to ", |
| 298 | client); |
| 299 | CLIENT.send(bssap) to client; |
| 300 | return omit; |
| 301 | } |
| 302 | log("CommonBssmapUnitdataCallback: IMSI/TMSI not found in table"); |
| 303 | } else { |
| 304 | log("CommonBssmapUnitdataCallback: Not a paging message"); |
| 305 | } |
| 306 | /* ELSE: handle in user callback */ |
| 307 | return g_ran_ops.unitdata_cb.apply(bssap); |
| 308 | } |
| 309 | |
| 310 | private function f_bssap_le_reset(SCCP_PAR_Address peer, SCCP_PAR_Address own) runs on BSSAP_LE_Emulation_CT { |
| 311 | timer T := 5.0; |
| 312 | |
| 313 | BSSAP_LE.send(ts_BSSAP_LE_UNITDATA_req(peer, own, ts_BSSMAP_LE_Reset(GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE))); |
| 314 | T.start; |
| 315 | alt { |
| 316 | [] BSSAP_LE.receive(tr_BSSAP_LE_UNITDATA_ind(own, peer, tr_BSSMAP_LE_ResetAck)) { |
| 317 | log("Received RESET-ACK in response to RESET, we're ready to go!"); |
| 318 | } |
| 319 | [] as_reset_ack(); |
| 320 | [] BSSAP_LE.receive { repeat }; |
| 321 | [] T.timeout { |
| 322 | setverdict(fail, "Timeout waiting for RESET-ACK after sending RESET"); |
| 323 | mtc.stop; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | private function f_bssap_l3_is_rr(PDU_BSSAP_LE bssap) return boolean { |
| 329 | var template octetstring l3 := f_bssap_le_extract_l3(bssap); |
| 330 | return f_L3_is_rr(l3); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | |
| 335 | type record BssapLeOps { |
| 336 | BssmapLeCreateCallback create_cb optional, |
| 337 | BssmapLeUnitdataCallback unitdata_cb optional, |
| 338 | boolean decode_dtap, |
| 339 | boolean role_ms, |
| 340 | /* needed for performing BSSMAP RESET */ |
| 341 | SCCP_PAR_Address sccp_addr_local optional, |
Neels Hofmeyr | b2b3704 | 2020-10-12 18:04:12 +0000 | [diff] [blame^] | 342 | SCCP_PAR_Address sccp_addr_peer optional, |
| 343 | boolean send_reset |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | private altstep as_reset_ack() runs on BSSAP_LE_Emulation_CT { |
| 347 | var BSSAP_LE_N_UNITDATA_ind ud_ind; |
| 348 | [] BSSAP_LE.receive(tr_BSSAP_LE_UNITDATA_ind(?, ?, tr_BSSMAP_LE_Reset)) -> value ud_ind { |
Neels Hofmeyr | 67de937 | 2020-10-01 06:35:49 +0200 | [diff] [blame] | 349 | log("Responding to inbound RESET with RESET-ACK"); |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 350 | BSSAP_LE.send(ts_BSSAP_LE_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress, |
| 351 | ts_BSSMAP_LE_ResetAck)); |
| 352 | repeat; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | |
| 357 | private altstep as_main_bssap_le() runs on BSSAP_LE_Emulation_CT { |
| 358 | var BSSAP_LE_N_UNITDATA_ind ud_ind; |
| 359 | var BSSAP_LE_N_CONNECT_ind conn_ind; |
| 360 | var BSSAP_LE_N_CONNECT_cfm conn_cfm; |
| 361 | var BSSAP_LE_N_DATA_ind data_ind; |
| 362 | var BSSAP_LE_N_DISCONNECT_ind disc_ind; |
| 363 | var BSSAP_LE_Conn_Req creq; |
| 364 | var PDU_BSSAP_LE bssap; |
| 365 | var BSSAP_LE_N_UNITDATA_req bssap_ud; |
| 366 | var BSSAP_LE_ConnHdlr vc_conn; |
| 367 | var integer targetPointCode; |
| 368 | var N_Sd_Array last_n_sd; |
| 369 | |
| 370 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
| 371 | [] BSSAP_LE.receive(BSSAP_LE_N_UNITDATA_ind:?) -> value ud_ind { |
| 372 | /* Connectionless Procedures like RESET */ |
| 373 | var template PDU_BSSAP_LE resp; |
| 374 | resp := CommonBssmapUnitdataCallback(ud_ind.userData); |
| 375 | if (isvalue(resp)) { |
| 376 | BSSAP_LE.send(ts_BSSAP_LE_UNITDATA_req(ud_ind.callingAddress, |
| 377 | ud_ind.calledAddress, resp)); |
| 378 | } |
| 379 | } |
| 380 | /* SCCP -> Client: new connection from BSC */ |
| 381 | [] BSSAP_LE.receive(BSSAP_LE_N_CONNECT_ind:?) -> value conn_ind { |
| 382 | vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id); |
| 383 | /* store mapping between client components and SCCP connectionId */ |
| 384 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 385 | /* handle user payload */ |
| 386 | f_handle_userData(vc_conn, conn_ind.userData); |
| 387 | /* confirm connection establishment */ |
| 388 | BSSAP_LE.send(ts_BSSAP_LE_CONNECT_res(conn_ind.connectionId, omit)); |
| 389 | } |
| 390 | /* SCCP -> Client: connection-oriented data in existing connection */ |
| 391 | [] BSSAP_LE.receive(BSSAP_LE_N_DATA_ind:?) -> value data_ind { |
| 392 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
| 393 | if (ispresent(data_ind.userData)) { |
| 394 | f_handle_userData(vc_conn, data_ind.userData); |
| 395 | } |
| 396 | } |
| 397 | /* SCCP -> Client: disconnect of an existing connection */ |
| 398 | [] BSSAP_LE.receive(BSSAP_LE_N_DISCONNECT_ind:?) -> value disc_ind { |
| 399 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
| 400 | if (ispresent(disc_ind.userData)) { |
| 401 | f_handle_userData(vc_conn, disc_ind.userData); |
| 402 | } |
| 403 | /* notify client about termination */ |
| 404 | var BSSAP_LE_Conn_Prim prim := CONN_PRIM_DISC_IND; |
| 405 | CLIENT.send(prim) to vc_conn; |
| 406 | f_conn_table_del(disc_ind.connectionId); |
| 407 | /* TOOD: return confirm to other side? */ |
| 408 | } |
| 409 | /* SCCP -> Client: connection confirm for outbound connection */ |
| 410 | [] BSSAP_LE.receive(BSSAP_LE_N_CONNECT_cfm:?) -> value conn_cfm { |
| 411 | vc_conn := f_comp_by_conn_id(conn_cfm.connectionId); |
| 412 | var BSSAP_LE_Conn_Prim prim := CONN_PRIM_CONF_IND; |
| 413 | CLIENT.send(prim) to vc_conn; |
| 414 | /* handle user payload */ |
| 415 | if (ispresent(conn_cfm.userData)) { |
| 416 | f_handle_userData(vc_conn, conn_cfm.userData); |
| 417 | } |
| 418 | } |
| 419 | [] CLIENT.receive(PDU_BSSAP_LE:?) -> value bssap sender vc_conn { |
| 420 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 421 | /* send it to dispatcher */ |
| 422 | BSSAP_LE.send(ts_BSSAP_LE_DATA_req(conn_id, bssap)); |
| 423 | } |
| 424 | |
| 425 | [] CLIENT.receive(BSSAP_LE_N_UNITDATA_req:?) -> value bssap_ud sender vc_conn { |
| 426 | BSSAP_LE.send(bssap_ud); |
| 427 | } |
| 428 | |
| 429 | /* Disconnect request client -> SCCP */ |
| 430 | [] CLIENT.receive(BSSAP_LE_Conn_Prim:CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 431 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 432 | BSSAP_LE.send(ts_BSSAP_LE_DISC_req(conn_id, 0)); |
| 433 | f_conn_table_del(conn_id); |
| 434 | } |
| 435 | |
| 436 | /* BSSAP from client -> SCCP */ |
| 437 | [] CLIENT.receive(BSSAP_LE_Conn_Req:?) -> value creq sender vc_conn { |
| 438 | var integer conn_id; |
| 439 | /* send to dispatcher */ |
| 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 | |
| 448 | BSSAP_LE.send(ts_BSSAP_LE_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id, |
| 449 | creq.bssap)); |
| 450 | } else { |
| 451 | /* known client, send via existing connection */ |
| 452 | conn_id := f_conn_id_by_comp(vc_conn); |
| 453 | BSSAP_LE.send(ts_BSSAP_LE_DATA_req(conn_id, creq.bssap)); |
| 454 | } |
| 455 | |
| 456 | /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment |
| 457 | * counter only on MM/CC/SS, but not on RR */ |
| 458 | if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) { |
| 459 | /* we have just sent the first MM message, increment the counter */ |
| 460 | var integer idx := f_idx_by_comp(vc_conn); |
| 461 | ConnectionTable[idx].n_sd[0] := 1; |
| 462 | log("patch: N(SD) for ConnIdx ", idx, " set to 1"); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | [] PROC.getcall(BSSAP_LE_last_n_sd:{?,-}) -> param(vc_conn) { |
| 467 | var integer idx := f_idx_by_comp(vc_conn); |
| 468 | last_n_sd := ConnectionTable[idx].n_sd; |
| 469 | PROC.reply(BSSAP_LE_last_n_sd:{vc_conn, last_n_sd}) to vc_conn; |
| 470 | } |
| 471 | |
| 472 | [] PROC.getcall(BSSAP_LE_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) { |
| 473 | var integer idx := f_idx_by_comp(vc_conn); |
| 474 | ConnectionTable[idx].n_sd := last_n_sd; |
| 475 | PROC.reply(BSSAP_LE_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* send a raw (encoded) L3 message over given SCCP connection */ |
| 481 | private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on BSSAP_LE_Emulation_CT |
| 482 | { |
| 483 | var PDU_BSSAP_LE bssap; |
| 484 | bssap := valueof(ts_BSSAP_LE_DTAP(l3_enc, dlci)); |
| 485 | BSSAP_LE.send(ts_BSSAP_LE_DATA_req(sccp_conn_id, bssap)); |
| 486 | } |
| 487 | |
| 488 | /* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */ |
| 489 | private function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) { |
| 490 | var integer n_sd_idx := f_ML3_n_sd_idx(dtap); |
| 491 | if (n_sd_idx < 0) { |
| 492 | return; |
| 493 | } |
| 494 | var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx); |
| 495 | f_ML3_patch_seq_nr(seq_nr, enc_l3); |
| 496 | } |
| 497 | |
| 498 | function main(BssapLeOps ops, charstring id) runs on BSSAP_LE_Emulation_CT { |
| 499 | |
| 500 | g_ran_id := id; |
| 501 | g_ran_ops := ops; |
| 502 | f_conn_table_init(); |
| 503 | f_expect_table_init(); |
| 504 | |
Neels Hofmeyr | b2b3704 | 2020-10-12 18:04:12 +0000 | [diff] [blame^] | 505 | if (g_ran_ops.send_reset |
| 506 | and isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) { |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 507 | f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */ |
| 508 | f_bssap_le_reset(ops.sccp_addr_peer, ops.sccp_addr_local); |
| 509 | } |
| 510 | |
| 511 | while (true) { |
| 512 | var BSSAP_LE_ConnHdlr vc_conn; |
| 513 | var PDU_DTAP_MO dtap_mo; |
| 514 | var PDU_DTAP_MT dtap_mt; |
| 515 | var BSSAP_LE_ConnHdlr vc_hdlr; |
| 516 | var octetstring l3_info; |
| 517 | var hexstring imsi; |
| 518 | var OCT4 tmsi; |
| 519 | var integer targetPointCode; |
| 520 | |
| 521 | alt { |
Neels Hofmeyr | b2b3704 | 2020-10-12 18:04:12 +0000 | [diff] [blame^] | 522 | [not g_ran_ops.send_reset] as_reset_ack(); |
| 523 | |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 524 | [] as_main_bssap_le(); |
| 525 | |
| 526 | [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn { |
| 527 | var integer idx := f_idx_by_comp(vc_conn); |
| 528 | /* convert from decoded DTAP to encoded DTAP */ |
| 529 | var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap); |
| 530 | /* patch correct L3 send sequence number N(SD) into l3_enc */ |
| 531 | if (dtap_mo.skip_seq_patching == false) { |
| 532 | f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc); |
| 533 | } |
| 534 | f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc); |
| 535 | } |
| 536 | |
| 537 | [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn { |
| 538 | var integer idx := f_idx_by_comp(vc_conn); |
| 539 | /* convert from decoded DTAP to encoded DTAP */ |
| 540 | var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap); |
| 541 | f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc); |
| 542 | } |
| 543 | |
| 544 | [] PROC.getcall(BSSAP_LE_register:{?,?}) -> param(l3_info, vc_hdlr) { |
| 545 | f_create_expect(l3_info, vc_hdlr); |
| 546 | PROC.reply(BSSAP_LE_register:{l3_info, vc_hdlr}) to vc_hdlr; |
| 547 | } |
| 548 | |
| 549 | [] PROC.getcall(BSSAP_LE_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) { |
| 550 | f_create_expect(omit, vc_hdlr, targetPointCode); |
| 551 | PROC.reply(BSSAP_LE_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr; |
| 552 | } |
| 553 | |
| 554 | [] PROC.getcall(BSSAP_LE_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) { |
| 555 | f_create_imsi(imsi, tmsi, vc_hdlr); |
| 556 | PROC.reply(BSSAP_LE_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /*********************************************************************** |
| 565 | * "Expect" Handling (mapping for expected incoming SCCP connections) |
| 566 | ***********************************************************************/ |
| 567 | |
| 568 | /* data about an expected future incoming connection */ |
| 569 | type record ExpectData { |
| 570 | /* L3 payload based on which we can match it */ |
| 571 | octetstring l3_payload optional, |
| 572 | integer handoverRequestPointCode optional, |
| 573 | /* component reference for this connection */ |
| 574 | BSSAP_LE_ConnHdlr vc_conn |
| 575 | } |
| 576 | |
| 577 | /* procedure based port to register for incoming connections */ |
| 578 | signature BSSAP_LE_register(in octetstring l3, in BSSAP_LE_ConnHdlr hdlr); |
| 579 | signature BSSAP_LE_register_handoverRequest(in integer targetPointCode, in BSSAP_LE_ConnHdlr hdlr); |
| 580 | |
| 581 | /* procedure based port to register for incoming IMSI/TMSI */ |
| 582 | signature BSSAP_LE_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_LE_ConnHdlr hdlr); |
| 583 | |
| 584 | /* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */ |
| 585 | signature BSSAP_LE_last_n_sd(in BSSAP_LE_ConnHdlr hdlr, out N_Sd_Array last_n_sd); |
| 586 | |
| 587 | /* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */ |
| 588 | signature BSSAP_LE_continue_after_n_sd(N_Sd_Array last_n_sd, in BSSAP_LE_ConnHdlr hdlr); |
| 589 | |
| 590 | type port BSSAP_LE_PROC_PT procedure { |
| 591 | inout BSSAP_LE_register, BSSAP_LE_register_imsi, BSSAP_LE_register_handoverRequest, BSSAP_LE_last_n_sd, BSSAP_LE_continue_after_n_sd; |
| 592 | } with { extension "internal" }; |
| 593 | |
| 594 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 595 | function ExpectedCreateCallback(BSSAP_LE_N_CONNECT_ind conn_ind, charstring id) |
| 596 | runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr { |
| 597 | var BSSAP_LE_ConnHdlr ret := null; |
| 598 | var octetstring l3_info; |
| 599 | var integer i; |
| 600 | |
| 601 | if (ischosen(conn_ind.userData.pdu.bssmap.perf_loc_req)) { |
| 602 | var BSSMAP_LE_PerfLocReq plr := conn_ind.userData.pdu.bssmap.perf_loc_req; |
| 603 | if (not ispresent(plr.imsi)) { |
| 604 | log("ExpectedCreateCallback: Cannot handle PerformLocReq without IMSI!"); |
| 605 | mtc.stop; |
| 606 | } |
| 607 | var hexstring imsi := plr.imsi.imsi.oddEvenInd_identity.imsi.digits; |
| 608 | /* Find the component by the IMSI [usually] contained in this message */ |
| 609 | ret := f_imsi_table_find(imsi, omit); |
| 610 | if (ret != null) { |
| 611 | log("ExpectedCreateCallback: Found ConnHdlr ", ret, " for IMSI ", imsi); |
| 612 | return ret; |
| 613 | } |
| 614 | } else if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) { |
| 615 | l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info; |
| 616 | log("ExpectedCreateCallback completeLayer3Information"); |
| 617 | } else { |
Neels Hofmeyr | 67de937 | 2020-10-01 06:35:49 +0200 | [diff] [blame] | 618 | setverdict(fail, "N-CONNECT.ind with L3 != { PerformLocReq | COMPLETE L3 }"); |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 619 | mtc.stop; |
| 620 | } |
| 621 | |
| 622 | for (i := 0; i < sizeof(ExpectTable); i:= i+1) { |
| 623 | if (not ispresent(ExpectTable[i].l3_payload)) { |
| 624 | continue; |
| 625 | } |
| 626 | if (l3_info == ExpectTable[i].l3_payload) { |
| 627 | ret := ExpectTable[i].vc_conn; |
| 628 | /* release this entry to be used again */ |
| 629 | ExpectTable[i].l3_payload := omit; |
| 630 | ExpectTable[i].vc_conn := null; |
| 631 | log("Found Expect[", i, "] for ", l3_info, " handled at ", ret); |
| 632 | /* return the component reference */ |
| 633 | return ret; |
| 634 | } |
| 635 | } |
| 636 | setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind); |
| 637 | mtc.stop; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | private function f_create_expect(template octetstring l3, BSSAP_LE_ConnHdlr hdlr, |
| 642 | template integer handoverRequestPointCode := omit) |
| 643 | runs on BSSAP_LE_Emulation_CT { |
| 644 | var integer i; |
| 645 | log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode); |
| 646 | for (i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 647 | if (not ispresent(ExpectTable[i].l3_payload) |
| 648 | and not ispresent(ExpectTable[i].handoverRequestPointCode)) { |
| 649 | if (ispresent(l3)) { |
| 650 | ExpectTable[i].l3_payload := valueof(l3); |
| 651 | } |
| 652 | if (ispresent(handoverRequestPointCode)) { |
| 653 | ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode); |
| 654 | } |
| 655 | ExpectTable[i].vc_conn := hdlr; |
| 656 | if (ispresent(handoverRequestPointCode)) { |
| 657 | log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr); |
| 658 | } else { |
| 659 | log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr); |
| 660 | } |
| 661 | return; |
| 662 | } |
| 663 | } |
| 664 | testcase.stop("No space left in ExpectTable"); |
| 665 | } |
| 666 | |
| 667 | private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_LE_ConnHdlr hdlr) |
| 668 | runs on BSSAP_LE_Emulation_CT { |
| 669 | for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) { |
| 670 | if (not ispresent(ImsiTable[i].imsi)) { |
| 671 | ImsiTable[i].imsi := imsi; |
| 672 | ImsiTable[i].tmsi := tmsi; |
| 673 | ImsiTable[i].comp_ref := hdlr; |
| 674 | log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr); |
| 675 | return; |
| 676 | } |
| 677 | } |
| 678 | testcase.stop("No space left in ImsiTable"); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | private function f_expect_table_init() |
| 683 | runs on BSSAP_LE_Emulation_CT { |
| 684 | for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) { |
| 685 | ExpectTable[i].l3_payload := omit; |
| 686 | ExpectTable[i].handoverRequestPointCode := omit; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /* helper function for clients to register their IMSI/TMSI */ |
Neels Hofmeyr | 67de937 | 2020-10-01 06:35:49 +0200 | [diff] [blame] | 691 | /* FIXME: there is no TMSI in BSSMAP-LE */ |
Harald Welte | a803b72 | 2020-08-21 15:42:26 +0200 | [diff] [blame] | 692 | function f_bssap_le_register_imsi(hexstring imsi, template (omit) OCT4 tmsi_or_omit) |
| 693 | runs on BSSAP_LE_ConnHdlr { |
| 694 | var OCT4 tmsi; |
| 695 | |
| 696 | /* Resolve omit to a special reserved value */ |
| 697 | if (istemplatekind(tmsi_or_omit, "omit")) { |
| 698 | tmsi := 'FFFFFFFF'O; |
| 699 | } else { |
| 700 | tmsi := valueof(tmsi_or_omit); |
| 701 | } |
| 702 | |
| 703 | BSSAP_LE_PROC.call(BSSAP_LE_register_imsi:{imsi, tmsi, self}) { |
| 704 | [] BSSAP_LE_PROC.getreply(BSSAP_LE_register_imsi:{?,?,?}) {}; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | |
| 709 | } |