blob: 0b9fe162f0100ade3c1f1810a3d82c2974a7da29 [file] [log] [blame]
Harald Weltea803b722020-08-21 15:42:26 +02001module 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
33import from General_Types all;
34import from Osmocom_Types all;
35import from SCCP_Emulation all;
36import from SCCPasp_Types all;
37import from IPA_Emulation all;
38import from MobileL3_Types all;
39import from Misc_Helpers all;
40
41import from BSSAP_LE_Types all;
42import from BSSAP_LE_CodecPort all;
43import from BSSMAP_LE_Templates all;
44import from BSSMAP_Templates all;
45import from RAN_Emulation all;
46
47/* General "base class" component definition, of which specific implementations
48 * derive themselves by means of the "extends" feature */
49type 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 */
57type 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 */
67type 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
80type uint2_t N_Sd_Array[4];
81
82/* represents a single BSSAP connection over SCCP */
83type 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
91type record ImsiMapping {
92 BSSAP_LE_ConnHdlr comp_ref,
93 hexstring imsi optional,
94 OCT4 tmsi
95}
96
97type 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
120private function f_conn_id_known(integer sccp_conn_id)
121runs 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
131private function f_comp_known(BSSAP_LE_ConnHdlr client)
132runs 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 */
143private function f_comp_by_conn_id(integer sccp_conn_id)
144runs 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 */
156private function f_conn_id_by_comp(BSSAP_LE_ConnHdlr client)
157runs 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 */
168private function f_idx_by_comp(BSSAP_LE_ConnHdlr client)
169runs 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
179private function f_gen_conn_id()
180runs 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
190private function f_conn_table_init()
191runs 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
204private function f_conn_table_add(BSSAP_LE_ConnHdlr comp_ref, integer sccp_conn_id)
205runs 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
218private function f_conn_table_del(integer sccp_conn_id)
219runs 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
233private function f_imsi_table_find(hexstring imsi, template OCT4 tmsi)
234runs 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
244type record BSSAP_LE_Conn_Req {
245 SCCP_PAR_Address addr_peer,
246 SCCP_PAR_Address addr_own,
247 PDU_BSSAP_LE bssap
248}
249template 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 */
256private function f_handle_userData(BSSAP_LE_ConnHdlr client, PDU_BSSAP_LE bssap)
257runs 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 */
283type function BssmapLeCreateCallback(BSSAP_LE_N_CONNECT_ind conn_ind, charstring id)
284runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr;
285
286type function BssmapLeUnitdataCallback(PDU_BSSAP_LE bssap)
287runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE;
288
289/* handle common Unitdata such as Paging */
290private function CommonBssmapUnitdataCallback(PDU_BSSAP_LE bssap)
291runs 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
310private 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
328private 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
335type 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,
342 SCCP_PAR_Address sccp_addr_peer optional
343}
344
345private altstep as_reset_ack() runs on BSSAP_LE_Emulation_CT {
346 var BSSAP_LE_N_UNITDATA_ind ud_ind;
347 [] BSSAP_LE.receive(tr_BSSAP_LE_UNITDATA_ind(?, ?, tr_BSSMAP_LE_Reset)) -> value ud_ind {
348 log("Respoding to inbound RESET with RESET-ACK");
349 BSSAP_LE.send(ts_BSSAP_LE_UNITDATA_req(ud_ind.callingAddress, ud_ind.calledAddress,
350 ts_BSSMAP_LE_ResetAck));
351 repeat;
352 }
353}
354
355
356private altstep as_main_bssap_le() runs on BSSAP_LE_Emulation_CT {
357 var BSSAP_LE_N_UNITDATA_ind ud_ind;
358 var BSSAP_LE_N_CONNECT_ind conn_ind;
359 var BSSAP_LE_N_CONNECT_cfm conn_cfm;
360 var BSSAP_LE_N_DATA_ind data_ind;
361 var BSSAP_LE_N_DISCONNECT_ind disc_ind;
362 var BSSAP_LE_Conn_Req creq;
363 var PDU_BSSAP_LE bssap;
364 var BSSAP_LE_N_UNITDATA_req bssap_ud;
365 var BSSAP_LE_ConnHdlr vc_conn;
366 var integer targetPointCode;
367 var N_Sd_Array last_n_sd;
368
369 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
370 [] BSSAP_LE.receive(BSSAP_LE_N_UNITDATA_ind:?) -> value ud_ind {
371 /* Connectionless Procedures like RESET */
372 var template PDU_BSSAP_LE resp;
373 resp := CommonBssmapUnitdataCallback(ud_ind.userData);
374 if (isvalue(resp)) {
375 BSSAP_LE.send(ts_BSSAP_LE_UNITDATA_req(ud_ind.callingAddress,
376 ud_ind.calledAddress, resp));
377 }
378 }
379 /* SCCP -> Client: new connection from BSC */
380 [] BSSAP_LE.receive(BSSAP_LE_N_CONNECT_ind:?) -> value conn_ind {
381 vc_conn := g_ran_ops.create_cb.apply(conn_ind, g_ran_id);
382 /* store mapping between client components and SCCP connectionId */
383 f_conn_table_add(vc_conn, conn_ind.connectionId);
384 /* handle user payload */
385 f_handle_userData(vc_conn, conn_ind.userData);
386 /* confirm connection establishment */
387 BSSAP_LE.send(ts_BSSAP_LE_CONNECT_res(conn_ind.connectionId, omit));
388 }
389 /* SCCP -> Client: connection-oriented data in existing connection */
390 [] BSSAP_LE.receive(BSSAP_LE_N_DATA_ind:?) -> value data_ind {
391 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
392 if (ispresent(data_ind.userData)) {
393 f_handle_userData(vc_conn, data_ind.userData);
394 }
395 }
396 /* SCCP -> Client: disconnect of an existing connection */
397 [] BSSAP_LE.receive(BSSAP_LE_N_DISCONNECT_ind:?) -> value disc_ind {
398 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
399 if (ispresent(disc_ind.userData)) {
400 f_handle_userData(vc_conn, disc_ind.userData);
401 }
402 /* notify client about termination */
403 var BSSAP_LE_Conn_Prim prim := CONN_PRIM_DISC_IND;
404 CLIENT.send(prim) to vc_conn;
405 f_conn_table_del(disc_ind.connectionId);
406 /* TOOD: return confirm to other side? */
407 }
408 /* SCCP -> Client: connection confirm for outbound connection */
409 [] BSSAP_LE.receive(BSSAP_LE_N_CONNECT_cfm:?) -> value conn_cfm {
410 vc_conn := f_comp_by_conn_id(conn_cfm.connectionId);
411 var BSSAP_LE_Conn_Prim prim := CONN_PRIM_CONF_IND;
412 CLIENT.send(prim) to vc_conn;
413 /* handle user payload */
414 if (ispresent(conn_cfm.userData)) {
415 f_handle_userData(vc_conn, conn_cfm.userData);
416 }
417 }
418 [] CLIENT.receive(PDU_BSSAP_LE:?) -> value bssap sender vc_conn {
419 var integer conn_id := f_conn_id_by_comp(vc_conn);
420 /* send it to dispatcher */
421 BSSAP_LE.send(ts_BSSAP_LE_DATA_req(conn_id, bssap));
422 }
423
424 [] CLIENT.receive(BSSAP_LE_N_UNITDATA_req:?) -> value bssap_ud sender vc_conn {
425 BSSAP_LE.send(bssap_ud);
426 }
427
428 /* Disconnect request client -> SCCP */
429 [] CLIENT.receive(BSSAP_LE_Conn_Prim:CONN_PRIM_DISC_REQ) -> sender vc_conn {
430 var integer conn_id := f_conn_id_by_comp(vc_conn);
431 BSSAP_LE.send(ts_BSSAP_LE_DISC_req(conn_id, 0));
432 f_conn_table_del(conn_id);
433 }
434
435 /* BSSAP from client -> SCCP */
436 [] CLIENT.receive(BSSAP_LE_Conn_Req:?) -> value creq sender vc_conn {
437 var integer conn_id;
438 /* send to dispatcher */
439
440 if (f_comp_known(vc_conn) == false) {
441 /* unknown client, create new connection */
442 conn_id := f_gen_conn_id();
443
444 /* store mapping between client components and SCCP connectionId */
445 f_conn_table_add(vc_conn, conn_id);
446
447 BSSAP_LE.send(ts_BSSAP_LE_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
448 creq.bssap));
449 } else {
450 /* known client, send via existing connection */
451 conn_id := f_conn_id_by_comp(vc_conn);
452 BSSAP_LE.send(ts_BSSAP_LE_DATA_req(conn_id, creq.bssap));
453 }
454
455 /* InitialL3 contains RR (PAG RESP) or MM (CM SRV REQ), we must increment
456 * counter only on MM/CC/SS, but not on RR */
457 if (g_ran_ops.role_ms and not f_bssap_l3_is_rr(creq.bssap)) {
458 /* we have just sent the first MM message, increment the counter */
459 var integer idx := f_idx_by_comp(vc_conn);
460 ConnectionTable[idx].n_sd[0] := 1;
461 log("patch: N(SD) for ConnIdx ", idx, " set to 1");
462 }
463 }
464
465 [] PROC.getcall(BSSAP_LE_last_n_sd:{?,-}) -> param(vc_conn) {
466 var integer idx := f_idx_by_comp(vc_conn);
467 last_n_sd := ConnectionTable[idx].n_sd;
468 PROC.reply(BSSAP_LE_last_n_sd:{vc_conn, last_n_sd}) to vc_conn;
469 }
470
471 [] PROC.getcall(BSSAP_LE_continue_after_n_sd:{?,?}) -> param(last_n_sd, vc_conn) {
472 var integer idx := f_idx_by_comp(vc_conn);
473 ConnectionTable[idx].n_sd := last_n_sd;
474 PROC.reply(BSSAP_LE_continue_after_n_sd:{last_n_sd, vc_conn}) to vc_conn;
475 }
476}
477
478
479/* send a raw (encoded) L3 message over given SCCP connection */
480private function f_xmit_raw_l3(integer sccp_conn_id, OCT1 dlci, octetstring l3_enc) runs on BSSAP_LE_Emulation_CT
481{
482 var PDU_BSSAP_LE bssap;
483 bssap := valueof(ts_BSSAP_LE_DTAP(l3_enc, dlci));
484 BSSAP_LE.send(ts_BSSAP_LE_DATA_req(sccp_conn_id, bssap));
485}
486
487/* patch N(SD) into enc_l3, according to 24.007 11.2.3.2 */
488private function f_ML3_patch_seq(inout ConnectionData cd, in PDU_ML3_MS_NW dtap, inout octetstring enc_l3) {
489 var integer n_sd_idx := f_ML3_n_sd_idx(dtap);
490 if (n_sd_idx < 0) {
491 return;
492 }
493 var uint2_t seq_nr := f_next_n_sd(cd.n_sd, n_sd_idx);
494 f_ML3_patch_seq_nr(seq_nr, enc_l3);
495}
496
497function main(BssapLeOps ops, charstring id) runs on BSSAP_LE_Emulation_CT {
498
499 g_ran_id := id;
500 g_ran_ops := ops;
501 f_conn_table_init();
502 f_expect_table_init();
503
504 if (isvalue(ops.sccp_addr_peer) and isvalue(ops.sccp_addr_local)) {
505 f_sleep(1.0); /* HACK to wait for M3UA/ASP to be ACTIVE */
506 f_bssap_le_reset(ops.sccp_addr_peer, ops.sccp_addr_local);
507 }
508
509 while (true) {
510 var BSSAP_LE_ConnHdlr vc_conn;
511 var PDU_DTAP_MO dtap_mo;
512 var PDU_DTAP_MT dtap_mt;
513 var BSSAP_LE_ConnHdlr vc_hdlr;
514 var octetstring l3_info;
515 var hexstring imsi;
516 var OCT4 tmsi;
517 var integer targetPointCode;
518
519 alt {
520 [] as_main_bssap_le();
521
522 [g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MO:?) -> value dtap_mo sender vc_conn {
523 var integer idx := f_idx_by_comp(vc_conn);
524 /* convert from decoded DTAP to encoded DTAP */
525 var octetstring l3_enc := enc_PDU_ML3_MS_NW(dtap_mo.dtap);
526 /* patch correct L3 send sequence number N(SD) into l3_enc */
527 if (dtap_mo.skip_seq_patching == false) {
528 f_ML3_patch_seq(ConnectionTable[idx], dtap_mo.dtap, l3_enc);
529 }
530 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mo.dlci, l3_enc);
531 }
532
533 [not g_ran_ops.role_ms] CLIENT.receive(PDU_DTAP_MT:?) -> value dtap_mt sender vc_conn {
534 var integer idx := f_idx_by_comp(vc_conn);
535 /* convert from decoded DTAP to encoded DTAP */
536 var octetstring l3_enc := enc_PDU_ML3_NW_MS(dtap_mt.dtap);
537 f_xmit_raw_l3(ConnectionTable[idx].sccp_conn_id, dtap_mt.dlci, l3_enc);
538 }
539
540 [] PROC.getcall(BSSAP_LE_register:{?,?}) -> param(l3_info, vc_hdlr) {
541 f_create_expect(l3_info, vc_hdlr);
542 PROC.reply(BSSAP_LE_register:{l3_info, vc_hdlr}) to vc_hdlr;
543 }
544
545 [] PROC.getcall(BSSAP_LE_register_handoverRequest:{?,?}) -> param(targetPointCode, vc_hdlr) {
546 f_create_expect(omit, vc_hdlr, targetPointCode);
547 PROC.reply(BSSAP_LE_register_handoverRequest:{targetPointCode, vc_hdlr}) to vc_hdlr;
548 }
549
550 [] PROC.getcall(BSSAP_LE_register_imsi:{?,?,?}) -> param(imsi, tmsi, vc_hdlr) {
551 f_create_imsi(imsi, tmsi, vc_hdlr);
552 PROC.reply(BSSAP_LE_register_imsi:{imsi, tmsi, vc_hdlr}) to vc_hdlr;
553 }
554
555
556 }
557 }
558}
559
560/***********************************************************************
561 * "Expect" Handling (mapping for expected incoming SCCP connections)
562 ***********************************************************************/
563
564/* data about an expected future incoming connection */
565type record ExpectData {
566 /* L3 payload based on which we can match it */
567 octetstring l3_payload optional,
568 integer handoverRequestPointCode optional,
569 /* component reference for this connection */
570 BSSAP_LE_ConnHdlr vc_conn
571}
572
573/* procedure based port to register for incoming connections */
574signature BSSAP_LE_register(in octetstring l3, in BSSAP_LE_ConnHdlr hdlr);
575signature BSSAP_LE_register_handoverRequest(in integer targetPointCode, in BSSAP_LE_ConnHdlr hdlr);
576
577/* procedure based port to register for incoming IMSI/TMSI */
578signature BSSAP_LE_register_imsi(in hexstring imsi, in OCT4 tmsi, in BSSAP_LE_ConnHdlr hdlr);
579
580/* If DTAP happens across other channels (e.g. GSUP), provide manual advancing of the n_sd sequence number */
581signature BSSAP_LE_last_n_sd(in BSSAP_LE_ConnHdlr hdlr, out N_Sd_Array last_n_sd);
582
583/* Update conn's n_sd sequence nr after the connection was taken over from elsewhere */
584signature BSSAP_LE_continue_after_n_sd(N_Sd_Array last_n_sd, in BSSAP_LE_ConnHdlr hdlr);
585
586type port BSSAP_LE_PROC_PT procedure {
587 inout BSSAP_LE_register, BSSAP_LE_register_imsi, BSSAP_LE_register_handoverRequest, BSSAP_LE_last_n_sd, BSSAP_LE_continue_after_n_sd;
588} with { extension "internal" };
589
590/* CreateCallback that can be used as create_cb and will use the expectation table */
591function ExpectedCreateCallback(BSSAP_LE_N_CONNECT_ind conn_ind, charstring id)
592runs on BSSAP_LE_Emulation_CT return BSSAP_LE_ConnHdlr {
593 var BSSAP_LE_ConnHdlr ret := null;
594 var octetstring l3_info;
595 var integer i;
596
597 if (ischosen(conn_ind.userData.pdu.bssmap.perf_loc_req)) {
598 var BSSMAP_LE_PerfLocReq plr := conn_ind.userData.pdu.bssmap.perf_loc_req;
599 if (not ispresent(plr.imsi)) {
600 log("ExpectedCreateCallback: Cannot handle PerformLocReq without IMSI!");
601 mtc.stop;
602 }
603 var hexstring imsi := plr.imsi.imsi.oddEvenInd_identity.imsi.digits;
604 /* Find the component by the IMSI [usually] contained in this message */
605 ret := f_imsi_table_find(imsi, omit);
606 if (ret != null) {
607 log("ExpectedCreateCallback: Found ConnHdlr ", ret, " for IMSI ", imsi);
608 return ret;
609 }
610 } else if (ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
611 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
612 log("ExpectedCreateCallback completeLayer3Information");
613 } else {
614 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
615 mtc.stop;
616 }
617
618 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
619 if (not ispresent(ExpectTable[i].l3_payload)) {
620 continue;
621 }
622 if (l3_info == ExpectTable[i].l3_payload) {
623 ret := ExpectTable[i].vc_conn;
624 /* release this entry to be used again */
625 ExpectTable[i].l3_payload := omit;
626 ExpectTable[i].vc_conn := null;
627 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
628 /* return the component reference */
629 return ret;
630 }
631 }
632 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
633 mtc.stop;
634}
635
636
637private function f_create_expect(template octetstring l3, BSSAP_LE_ConnHdlr hdlr,
638 template integer handoverRequestPointCode := omit)
639runs on BSSAP_LE_Emulation_CT {
640 var integer i;
641 log("f_create_expect(l3 := ", l3, ", handoverRequest := ", handoverRequestPointCode);
642 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
643 if (not ispresent(ExpectTable[i].l3_payload)
644 and not ispresent(ExpectTable[i].handoverRequestPointCode)) {
645 if (ispresent(l3)) {
646 ExpectTable[i].l3_payload := valueof(l3);
647 }
648 if (ispresent(handoverRequestPointCode)) {
649 ExpectTable[i].handoverRequestPointCode := valueof(handoverRequestPointCode);
650 }
651 ExpectTable[i].vc_conn := hdlr;
652 if (ispresent(handoverRequestPointCode)) {
653 log("Created Expect[", i, "] for handoverRequest to be handled at ", hdlr);
654 } else {
655 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
656 }
657 return;
658 }
659 }
660 testcase.stop("No space left in ExpectTable");
661}
662
663private function f_create_imsi(hexstring imsi, OCT4 tmsi, BSSAP_LE_ConnHdlr hdlr)
664runs on BSSAP_LE_Emulation_CT {
665 for (var integer i := 0; i < sizeof(ImsiTable); i := i+1) {
666 if (not ispresent(ImsiTable[i].imsi)) {
667 ImsiTable[i].imsi := imsi;
668 ImsiTable[i].tmsi := tmsi;
669 ImsiTable[i].comp_ref := hdlr;
670 log("Created IMSI[", i, "] for ", imsi, tmsi, " to be handled at ", hdlr);
671 return;
672 }
673 }
674 testcase.stop("No space left in ImsiTable");
675}
676
677
678private function f_expect_table_init()
679runs on BSSAP_LE_Emulation_CT {
680 for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
681 ExpectTable[i].l3_payload := omit;
682 ExpectTable[i].handoverRequestPointCode := omit;
683 }
684}
685
686/* helper function for clients to register their IMSI/TMSI */
687function f_bssap_le_register_imsi(hexstring imsi, template (omit) OCT4 tmsi_or_omit)
688runs on BSSAP_LE_ConnHdlr {
689 var OCT4 tmsi;
690
691 /* Resolve omit to a special reserved value */
692 if (istemplatekind(tmsi_or_omit, "omit")) {
693 tmsi := 'FFFFFFFF'O;
694 } else {
695 tmsi := valueof(tmsi_or_omit);
696 }
697
698 BSSAP_LE_PROC.call(BSSAP_LE_register_imsi:{imsi, tmsi, self}) {
699 [] BSSAP_LE_PROC.getreply(BSSAP_LE_register_imsi:{?,?,?}) {};
700 }
701}
702
703
704}