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