blob: 6221bf8852753b1a1345560b1bf0980fde8a8939 [file] [log] [blame]
Harald Welte35498112019-07-02 14:26:39 +08001module S1AP_Emulation {
2
Philipp Maier7147c922023-07-07 14:18:32 +02003/* S1AP Emulation, runs on top of S1AP_CodecPort. It multiplexes/demultiplexes
4 * the individual subscribers by their UE association (MME_UE_S1AP_ID/
5 * ENB_UE_S1AP_ID identifiers), so there can be separate TTCN-3 components
6 * handling each of them.
Harald Welte35498112019-07-02 14:26:39 +08007 *
8 * The S1AP_Emulation.main() function processes S1AP primitives from the S1AP
Philipp Maier7147c922023-07-07 14:18:32 +02009 * socket via the S1AP_CodecPort, and dispatches them to the per-subscriber
10 * components.
Harald Welte35498112019-07-02 14:26:39 +080011 *
Philipp Maier7147c922023-07-07 14:18:32 +020012 * For each new subscruber, the S1apOps.create_cb() is called. It can create
13 * or resolve a TTCN-3 component, and returns a component reference to which
14 * that subscriber traffic is routed/dispatched.
Harald Welte35498112019-07-02 14:26:39 +080015 *
Philipp Maier7147c922023-07-07 14:18:32 +020016 * If a pre-existing component wants to register to handle a future inbound UE
17 * association, it can do so by registering an "expect" with the expected
18 * MME_UE_S1AP_ID/ENB_UE_S1AP_ID identifiers.
Harald Welte35498112019-07-02 14:26:39 +080019 *
Philipp Maier7147c922023-07-07 14:18:32 +020020 * Inbound non-UE related S1AP messages (such as RESET, SETUP, OVERLOAD) are
21 * dispatched to the S1apOps.unitdata_cb() callback, which is registered with
22 * an argument to the main() function below.
Harald Welte35498112019-07-02 14:26:39 +080023 *
24 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
25 * All rights reserved.
26 *
27 * Released under the terms of GNU General Public License, Version 2 or
28 * (at your option) any later version.
29 *
30 * SPDX-License-Identifier: GPL-2.0-or-later
31 */
32
33import from S1AP_CodecPort all;
34import from S1AP_CodecPort_CtrlFunct all;
35import from S1AP_Types all;
36import from S1AP_Constants all;
37import from S1AP_PDU_Contents all;
38import from S1AP_PDU_Descriptions all;
39import from S1AP_IEs all;
40import from S1AP_Templates all;
41
42import from NAS_EPS_Types all;
43import from NAS_Templates all;
44
45import from LTE_CryptoFunctions all;
46
47import from General_Types all;
48import from Osmocom_Types all;
49import from IPL4asp_Types all;
50import from DNS_Helpers all;
51
52
53type component S1AP_ConnHdlr {
54 port S1AP_Conn_PT S1AP;
55 /* procedure based port to register for incoming connections */
56 port S1APEM_PROC_PT S1AP_PROC;
57}
58
59/* port between individual per-connection components and this dispatcher */
60type port S1AP_Conn_PT message {
61 inout S1AP_PDU, PDU_NAS_EPS, S1APEM_Config;
62} with { extension "internal" };
63
64type record NAS_Keys {
65 octetstring k_nas_int,
66 octetstring k_nas_enc
67};
68type union S1APEM_Config {
69 NAS_Keys set_nas_keys
70};
71
72type enumerated S1APEM_EventUpDown {
73 S1APEM_EVENT_DOWN,
74 S1APEM_EVENT_UP
75}
76
77/* an event indicating us whether or not a connection is physically up or down,
78 * and whether we have received an ID_ACK */
79type union S1APEM_Event {
80 S1APEM_EventUpDown up_down
81}
82
83/* global test port e.g. for non-imsi/conn specific messages */
84type port S1AP_PT message {
85 inout S1AP_PDU, S1APEM_Event;
86} with { extension "internal" };
87
88
89/* represents a single S1AP Association */
90type record AssociationData {
91 S1AP_ConnHdlr comp_ref, /* component handling this UE connection */
92 uint24_t enb_ue_s1ap_id optional, /* eNB side S1AP ID */
93 uint32_t mme_ue_s1ap_id optional, /* MME side S1AP ID */
94 EUTRAN_CGI cgi optional,
95 TAI tai optional,
96 NAS_UE_State nus
97
98 //hexstring imsi optional
99};
100
101type component S1AP_Emulation_CT {
102 /* Port facing to the UDP SUT */
103 port S1AP_CODEC_PT S1AP;
104 /* All S1AP_ConnHdlr S1AP ports connect here
105 * S1AP_Emulation_CT.main needs to figure out what messages
106 * to send where with CLIENT.send() to vc_conn */
107 port S1AP_Conn_PT S1AP_CLIENT;
108 /* currently tracked connections */
109 var AssociationData S1apAssociationTable[16];
110 /* pending expected CRCX */
111 var ExpectData S1apExpectTable[8];
112 /* procedure based port to register for incoming connections */
113 port S1APEM_PROC_PT S1AP_PROC;
114 /* test port for unit data messages */
115 port S1AP_PT S1AP_UNIT;
116
117 var S1AP_conn_parameters g_pars;
118 var charstring g_s1ap_id;
119 var integer g_s1ap_conn_id := -1;
120}
121
122type function S1APCreateCallback(S1AP_PDU msg, template (omit) MME_UE_S1AP_ID mme_id,
123 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
124runs on S1AP_Emulation_CT return S1AP_ConnHdlr;
125
126type function S1APUnitdataCallback(S1AP_PDU msg)
127runs on S1AP_Emulation_CT return template S1AP_PDU;
128
129type record S1APOps {
130 S1APCreateCallback create_cb,
131 S1APUnitdataCallback unitdata_cb
132}
133
134type record S1AP_conn_parameters {
135 HostName remote_ip,
136 PortNumber remote_sctp_port,
137 HostName local_ip,
138 PortNumber local_sctp_port,
139 NAS_Role role
140}
141
142function tr_S1AP_RecvFrom_R(template S1AP_PDU msg)
143runs on S1AP_Emulation_CT return template S1AP_RecvFrom {
144 var template S1AP_RecvFrom mrf := {
145 connId := g_s1ap_conn_id,
146 remName := ?,
147 remPort := ?,
148 locName := ?,
149 locPort := ?,
150 msg := msg
151 }
152 return mrf;
153}
154
155private function f_s1ap_ids_known(template (omit) MME_UE_S1AP_ID mme_id,
156 template (omit) ENB_UE_S1AP_ID enb_id)
157runs on S1AP_Emulation_CT return boolean {
158 var integer i;
159 log("f_s1ap_ids_known(",mme_id,", ",enb_id,")");
160 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
161 log("tbl[",i,"]: mme=", S1apAssociationTable[i].mme_ue_s1ap_id,
162 ", enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
163 /* skip empty records */
164 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit and
165 S1apAssociationTable[i].enb_ue_s1ap_id == omit) {
166 log("skipping empty ", i);
167 continue;
168 }
169 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit) {
170 log("entry ", i, " has no MME ID yet (enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
171 /* Table doesn't yet know the MME side ID, let's look-up only
172 * based on the eNB side ID */
173 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
174 /* update table with MME side ID */
175 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
176 return true;
177 }
178 } else if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id) and
179 match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
180 return true;
181 }
182 }
183 return false;
184}
185
186private function f_comp_known(S1AP_ConnHdlr client)
187runs on S1AP_Emulation_CT return boolean {
188 var integer i;
189 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
190 if (S1apAssociationTable[i].comp_ref == client) {
191 return true;
192 }
193 }
194 return false;
195}
196
197private function f_assoc_id_by_s1ap_ids(template (omit) MME_UE_S1AP_ID mme_id,
198 template (omit) ENB_UE_S1AP_ID enb_id)
199runs on S1AP_Emulation_CT return integer {
200 var integer i;
201 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
202 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
203 if (istemplatekind(mme_id, "omit")) {
204 return i;
205 } else {
206 if (match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
207 return i;
208 }
209 }
210 }
211 }
212 setverdict(fail, "S1AP Association Table not found by ENB-ID=", enb_id, " MME-ID=", mme_id);
213 mtc.stop;
214}
215
216private function f_assoc_id_by_comp(S1AP_ConnHdlr client)
217runs on S1AP_Emulation_CT return integer {
218 var integer i;
219 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
220 if (S1apAssociationTable[i].comp_ref == client) {
221 return i;
222 }
223 }
224 setverdict(fail, "S1AP Association Table not found by component ", client);
225 mtc.stop;
226}
227
228private function f_assoc_by_comp(S1AP_ConnHdlr client)
229runs on S1AP_Emulation_CT return AssociationData {
230 var integer i := f_assoc_id_by_comp(client);
231 return S1apAssociationTable[i];
232}
233
234private function f_s1ap_id_table_add(S1AP_ConnHdlr comp_ref,
235 template (omit) MME_UE_S1AP_ID mme_id, ENB_UE_S1AP_ID enb_id)
236runs on S1AP_Emulation_CT return integer {
237 var integer i;
238 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
239 if (not isvalue(S1apAssociationTable[i].enb_ue_s1ap_id)) {
240 S1apAssociationTable[i].enb_ue_s1ap_id := enb_id;
241 if (istemplatekind(mme_id, "omit")) {
242 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
243 } else {
244 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
245 }
246 S1apAssociationTable[i].comp_ref := comp_ref;
247 return i;
248 }
249 }
250 testcase.stop("S1AP Association Table full!");
251 return -1;
252}
253
254private function f_s1ap_id_table_del(S1AP_ConnHdlr comp_ref, ENB_UE_S1AP_ID enb_id)
255runs on S1AP_Emulation_CT {
256 var integer i;
257 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
258 if (S1apAssociationTable[i].comp_ref == comp_ref and
Philipp Maier0ce67ab2023-07-07 13:02:11 +0200259 S1apAssociationTable[i].enb_ue_s1ap_id == enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800260 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
261 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
262 S1apAssociationTable[i].comp_ref := null;
263 return;
264 }
265 }
266 setverdict(fail, "S1AP Association Table: Couldn't find to-be-deleted entry!");
267 mtc.stop;
268}
269
270
271private function f_s1ap_id_table_init()
272runs on S1AP_Emulation_CT {
273 for (var integer i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
274 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
275 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
276 S1apAssociationTable[i].cgi := omit;
277 S1apAssociationTable[i].tai := omit;
278 S1apAssociationTable[i].nus := valueof(t_NAS_UE_State(g_pars.role));
279 }
280}
281
282private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := 18) := {
283 sinfo_stream := omit,
284 sinfo_ppid := ppid,
285 remSocks := omit,
286 assocId := omit
287};
288
289private template PortEvent tr_SctpAssocChange := {
290 sctpEvent := {
291 sctpAssocChange := ?
292 }
293}
294private template PortEvent tr_SctpPeerAddrChange := {
295 sctpEvent := {
296 sctpPeerAddrChange := ?
297 }
298}
299
300private function f_s1ap_xceive(template (value) S1AP_PDU tx,
301 template S1AP_PDU rx_t := ?)
302runs on S1AP_Emulation_CT return S1AP_PDU {
303 timer T := 10.0;
304 var S1AP_RecvFrom mrf;
305
306 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, tx));
307 alt {
308 [] S1AP.receive(tr_S1AP_RecvFrom_R(rx_t)) -> value mrf { }
309 [] S1AP.receive(tr_SctpAssocChange) { repeat; }
310 [] S1AP.receive(tr_SctpPeerAddrChange) { repeat; }
311 [] T.timeout {
312 setverdict(fail, "Timeout waiting for ", rx_t);
313 mtc.stop;
314 }
315 }
316 return mrf.msg;
317}
318
319/*
320private function f_nas_try_decaps(PDU_NAS_EPS nas) return PDU_NAS_EPS
321{
322 var PDU_NAS_EPS_SecurityProtectedNASMessage secp_nas;
323 if (not match(nas, tr_NAS_EMM_SecurityProtected)) {
324 return nas;
325 }
326 secp_nas := nas.ePS_messages.ePS_MobilityManagement.pDU_NAS_EPS_SecurityProtectedNASMessage;
327 select (secp_nas.securityHeaderType) {
328 case ('0011'B) {
329 var octetstring knas_int := '530ce32318f26264eab26bc116870b86'O;
330 var octetstring data_with_seq := int2oct(secp_nas.sequenceNumber, 1) & secp_nas.nAS_Message;
331 var OCT4 exp_mac := f_snow_3g_f9(knas_int, secp_nas.sequenceNumber, 0,
332 is_downlink:=true, data:=data_with_seq);
333 if (exp_mac != secp_nas.messageAuthenticationCode) {
334 setverdict(fail, "Received NAS MAC ", secp_nas.messageAuthenticationCode,
335 " doesn't match expected MAC ", exp_mac, ": ", nas);
336 mtc.stop;
337 }
338 return dec_PDU_NAS_EPS(secp_nas.nAS_Message);
339 }
340 case else {
341 setverdict(fail, "Implement SecHdrType for ", secp_nas);
342 mtc.stop;
343 }
344 }
345}
346*/
347
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200348function handle_S1AP_UeContextReleaseCmd(template (present) S1AP_PDU rel_cmd) runs on S1AP_Emulation_CT {
349 if (ispresent(rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair)) {
350 var template MME_UE_S1AP_ID mme_ue_id;
351 var template ENB_UE_S1AP_ID enb_ue_id;
352 var integer assoc_id;
353 var S1AP_ConnHdlr vc_conn
354
355 mme_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.mME_UE_S1AP_ID;
356 enb_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.eNB_UE_S1AP_ID;
357
358 assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
359 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
360
361 f_s1ap_id_table_del(vc_conn, valueof(enb_ue_id));
362 } else {
363 /* TODO: The UE CONTEXT RELEASE COMMAND (see also: 3GPP TS 36.413, section 9.1.4.6), may identify the
364 * context by either an uE_S1AP_ID_pair (MME_UE_S1AP_ID and ENB_UE_S1AP_ID) or an MME_UE_S1AP_ID alone.
365 * The latter case is not implemented here yet. */
366 setverdict(fail, "complete implementation of UeContextReleaseCmd handling");
367 mtc.stop;
368 }
369}
370
Harald Welte35498112019-07-02 14:26:39 +0800371function main(S1APOps ops, S1AP_conn_parameters p, charstring id) runs on S1AP_Emulation_CT {
372 var Result res;
373 g_pars := p;
374 g_s1ap_id := id;
375 f_s1ap_id_table_init();
376 f_expect_table_init();
377
378 map(self:S1AP, system:S1AP_CODEC_PT);
379 if (p.remote_sctp_port == -1) {
380 res := S1AP_CodecPort_CtrlFunct.f_IPL4_listen(S1AP, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
381 } else {
382 res := S1AP_CodecPort_CtrlFunct.f_IPL4_connect(S1AP, p.remote_ip, p.remote_sctp_port,
383 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
384 }
385 if (not ispresent(res.connId)) {
386 setverdict(fail, "Could not connect S1AP socket, check your configuration");
387 mtc.stop;
388 }
389 g_s1ap_conn_id := res.connId;
390
391 /* notify user about SCTP establishment */
392 if (p.remote_sctp_port != -1) {
393 S1AP_UNIT.send(S1APEM_Event:{up_down:=S1APEM_EVENT_UP})
394 }
395
396 while (true) {
397 var S1AP_ConnHdlr vc_conn;
398 var PDU_NAS_EPS nas;
Philipp Maier7147c922023-07-07 14:18:32 +0200399 var MME_UE_S1AP_ID mme_id;
400 var ENB_UE_S1AP_ID enb_id;
Harald Welte35498112019-07-02 14:26:39 +0800401 var S1AP_RecvFrom mrf;
402 var S1AP_PDU msg;
403 var S1APEM_Config s1cfg;
404 var charstring vlr_name, mme_name;
405 var integer ai;
406
407 alt {
408 /* Configuration primitive from client */
409 [] S1AP_CLIENT.receive(S1APEM_Config:{set_nas_keys:=?}) -> value s1cfg sender vc_conn {
410 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
411 S1apAssociationTable[assoc_id].nus.k_nas_int := s1cfg.set_nas_keys.k_nas_int;
412 S1apAssociationTable[assoc_id].nus.k_nas_enc := s1cfg.set_nas_keys.k_nas_enc;
413 }
414 /* S1AP from client: InitialUE */
415 [] S1AP_CLIENT.receive(tr_S1AP_InitialUE) -> value msg sender vc_conn {
416 /* create a table entry about this connection */
417 ai := f_s1ap_id_table_add(vc_conn, omit, valueof(f_S1AP_get_ENB_UE_S1AP_ID(msg)));
418 /* Store CGI + TAI so we can use it for generating UlNasTransport from NAS */
419 S1apAssociationTable[ai].tai := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[2].value_.TAI;
420 S1apAssociationTable[ai].cgi := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[3].value_.EUTRAN_CGI;
421 /* Pass message through */
422 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
423 }
424 /* NAS from client: Wrap in S1AP Uplink NAS Transport */
425 [] S1AP_CLIENT.receive(PDU_NAS_EPS:?) -> value nas sender vc_conn {
426 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
427 var AssociationData ad := S1apAssociationTable[assoc_id];
428 nas := f_nas_encaps(S1apAssociationTable[assoc_id].nus, nas, new_ctx := false);
429 var octetstring nas_enc := enc_PDU_NAS_EPS(nas);
430 S1AP.send(t_S1AP_Send(g_s1ap_conn_id,
431 ts_S1AP_UlNasTransport(ad.mme_ue_s1ap_id,
432 ad.enb_ue_s1ap_id,
433 nas_enc, ad.cgi, ad.tai)));
434 }
435 /* S1AP from client: pass on transparently */
436 [] S1AP_CLIENT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
437 /* Pass message through */
438 /* FIXME: validate S1AP_IDs ? */
439 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
440 }
441
442 /* non-UE related S1AP: pass through unmodified/unverified */
443 [] S1AP_UNIT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
444 /* Pass message through */
445 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
446 }
447
448 /* S1AP received from peer (MME) */
449 [] S1AP.receive(tr_S1AP_RecvFrom_R(?)) -> value mrf {
450 if (match(mrf.msg, tr_S1AP_nonUErelated)) {
451 /* non-UE-related S1AP message */
452 var template S1AP_PDU resp := ops.unitdata_cb.apply(mrf.msg);
453 if (isvalue(resp)) {
454 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, valueof(resp)));
455 }
456 } else if (match(mrf.msg, tr_S1AP_UeContextReleaseCmd)) {
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200457 handle_S1AP_UeContextReleaseCmd(mrf.msg);
Harald Welte35498112019-07-02 14:26:39 +0800458 } else {
459 /* Ue-related S1AP message */
460 /* obtain MME + ENB UE S1AP ID */
461 var template (omit) MME_UE_S1AP_ID mme_ue_id := f_S1AP_get_MME_UE_S1AP_ID(mrf.msg);
462 var template (omit) ENB_UE_S1AP_ID enb_ue_id := f_S1AP_get_ENB_UE_S1AP_ID(mrf.msg);
463 /* check if those IDs are known in our table */
464 if (f_s1ap_ids_known(mme_ue_id, enb_ue_id)) {
465 /* if yes, dispatch to the ConnHdlr for this Ue-Connection */
466 var template (omit) octetstring nas_enc;
Philipp Maier7147c922023-07-07 14:18:32 +0200467 var integer assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
Harald Welte35498112019-07-02 14:26:39 +0800468 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
469 nas_enc := f_S1AP_get_NAS_PDU(mrf.msg);
470 if (isvalue(nas_enc)) {
471 nas := dec_PDU_NAS_EPS(valueof(nas_enc));
472 if (match(nas, tr_NAS_EMM_SecurityProtected)) {
473 nas := f_nas_try_decaps(S1apAssociationTable[assoc_id].nus, nas);
474 }
475 /* send decoded NAS */
476 S1AP_CLIENT.send(nas) to vc_conn;
477 } else {
478 /* send raw S1AP */
479 S1AP_CLIENT.send(mrf.msg) to vc_conn;
480 }
481 } else {
482 /* if not, call create_cb so it can create new ConnHdlr */
483 vc_conn := ops.create_cb.apply(mrf.msg, mme_ue_id, enb_ue_id, id);
484 f_s1ap_id_table_add(vc_conn, mme_ue_id, valueof(enb_ue_id));
485 S1AP_CLIENT.send(mrf.msg) to vc_conn;
486 }
487 }
488 }
489 [] S1AP.receive(tr_SctpAssocChange) { }
490 [] S1AP.receive(tr_SctpPeerAddrChange) { }
Philipp Maier7147c922023-07-07 14:18:32 +0200491 [] S1AP_PROC.getcall(S1APEM_register:{?,?,?}) -> param(mme_id, enb_id, vc_conn) {
492 f_create_expect(mme_id, enb_id, vc_conn);
493 S1AP_PROC.reply(S1APEM_register:{mme_id, enb_id, vc_conn}) to vc_conn;
Harald Welte35498112019-07-02 14:26:39 +0800494 }
495 }
496
497 }
498}
499
500/* "Expect" Handling */
501
502type record ExpectData {
Philipp Maier7147c922023-07-07 14:18:32 +0200503 MME_UE_S1AP_ID mme_id optional,
504 ENB_UE_S1AP_ID enb_id optional,
Harald Welte35498112019-07-02 14:26:39 +0800505 S1AP_ConnHdlr vc_conn
506}
507
Philipp Maier7147c922023-07-07 14:18:32 +0200508signature S1APEM_register(in MME_UE_S1AP_ID mme_id, in ENB_UE_S1AP_ID enb_id, in S1AP_ConnHdlr hdlr);
Harald Welte35498112019-07-02 14:26:39 +0800509
510type port S1APEM_PROC_PT procedure {
511 inout S1APEM_register;
512} with { extension "internal" };
513
Oliver Smith23e192e2023-02-13 15:00:46 +0100514/* Function that can be used as create_cb and will use the expect table */
Philipp Maier7147c922023-07-07 14:18:32 +0200515function ExpectedCreateCallback(S1AP_PDU msg,
516 template (omit) MME_UE_S1AP_ID mme_id,
517 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
Harald Welte35498112019-07-02 14:26:39 +0800518runs on S1AP_Emulation_CT return S1AP_ConnHdlr {
519 var S1AP_ConnHdlr ret := null;
520 var integer i;
521
522 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200523 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
Harald Welte35498112019-07-02 14:26:39 +0800524 continue;
525 }
Philipp Maier7147c922023-07-07 14:18:32 +0200526
527 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800528 ret := S1apExpectTable[i].vc_conn;
529 /* Release this entry */
Philipp Maier7147c922023-07-07 14:18:32 +0200530 S1apExpectTable[i].mme_id := omit;
531 S1apExpectTable[i].enb_id := omit;
Harald Welte35498112019-07-02 14:26:39 +0800532 S1apExpectTable[i].vc_conn := null;
533 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
534 return ret;
535 }
536 }
537 setverdict(fail, "Couldn't find Expect for ", msg);
538 mtc.stop;
539}
540
Philipp Maier7147c922023-07-07 14:18:32 +0200541private function f_create_expect(template (omit) MME_UE_S1AP_ID mme_id,
542 template (omit) ENB_UE_S1AP_ID enb_id,
543 S1AP_ConnHdlr hdlr)
Harald Welte35498112019-07-02 14:26:39 +0800544runs on S1AP_Emulation_CT {
545 var integer i;
546
547 /* Check an entry like this is not already presnt */
548 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200549 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
550 continue;
551 }
552 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
553 setverdict(fail, "UE MME id / UE ENB id pair already present", mme_id, enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800554 mtc.stop;
555 }
556 }
557 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200558 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
559 S1apExpectTable[i].mme_id := valueof(mme_id);
560 S1apExpectTable[i].enb_id := valueof(enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800561 S1apExpectTable[i].vc_conn := hdlr;
Philipp Maier7147c922023-07-07 14:18:32 +0200562 log("Created Expect[", i, "] for UE MME id:", mme_id, ", UE ENB id:", enb_id, " to be handled at ", hdlr);
Harald Welte35498112019-07-02 14:26:39 +0800563 return;
564 }
565 }
566 testcase.stop("No space left in S1apExpectTable")
567}
568
569/* client/conn_hdlr side function to use procedure port to create expect in emulation */
Philipp Maier7147c922023-07-07 14:18:32 +0200570function f_create_s1ap_expect(template (omit) MME_UE_S1AP_ID mme_id,
571 template (omit) ENB_UE_S1AP_ID enb_id) runs on S1AP_ConnHdlr {
572 S1AP_PROC.call(S1APEM_register:{mme_id, enb_id, self}) {
573 [] S1AP_PROC.getreply(S1APEM_register:{?,?,?}) {};
Harald Welte35498112019-07-02 14:26:39 +0800574 }
575}
576
577
578private function f_expect_table_init()
579runs on S1AP_Emulation_CT {
580 var integer i;
581 for (i := 0; i < sizeof(S1apExpectTable); i := i + 1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200582 S1apExpectTable[i].mme_id := omit;
583 S1apExpectTable[i].enb_id := omit;
584 S1apExpectTable[i].vc_conn := null;
Harald Welte35498112019-07-02 14:26:39 +0800585 }
586}
587
588function DummyUnitdataCallback(S1AP_PDU msg)
589runs on S1AP_Emulation_CT return template S1AP_PDU {
590 log("Ignoring S1AP ", msg);
591 return omit;
592}
593
594
595function f_S1AP_get_ENB_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) ENB_UE_S1AP_ID
596{
597 if (ischosen(s1ap.initiatingMessage)) {
598 var InitiatingMessage im := s1ap.initiatingMessage;
599 select (s1ap) {
600 case (tr_S1AP_InitialUE) {
601 return im.value_.InitialUEMessage.protocolIEs[0].value_.ENB_UE_S1AP_ID;
602 }
603 case (tr_S1AP_DlNasTransport) {
604 return im.value_.DownlinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
605 }
606 case (tr_S1AP_UlNasTransport) {
607 return im.value_.UplinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
608 }
609 case (tr_S1AP_IntialCtxSetupReq) {
610 return im.value_.initialContextSetupRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
611 }
612 case (tr_S1AP_UeContextReleaseReq) {
613 return im.value_.UEContextReleaseRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
614 }
615 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
616 case (tr_S1AP_ConnEstInd) {
617 return im.value_.ConnectionEstablishmentIndication.protocolIEs[1].value_.ENB_UE_S1AP_ID;
618 }
619 /* TODO */
620 }
621 } else if (ischosen(s1ap.successfulOutcome)) {
622 var SuccessfulOutcome so := s1ap.successfulOutcome;
623 select (s1ap) {
624 case (tr_S1AP_InitialCtxSetupResp) {
625 return so.value_.initialContextSetupResponse.protocolIEs[1].value_.ENB_UE_S1AP_ID;
626 }
627 case (tr_S1AP_UeContextReleaseCompl) {
628 return so.value_.UEContextReleaseComplete.protocolIEs[1].value_.ENB_UE_S1AP_ID;
629 }
630 /* TODO */
631 }
632 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
633 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
634 select (s1ap) {
635 case (tr_S1AP_InitialCtxSetupFail) {
636 return uo.value_.initialContextSetupFailure.protocolIEs[1].value_.ENB_UE_S1AP_ID;
637 }
638 /* TODO */
639 }
640 }
641 return omit;
642}
643
644function f_S1AP_get_MME_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) MME_UE_S1AP_ID
645{
646 if (ischosen(s1ap.initiatingMessage)) {
647 var InitiatingMessage im := s1ap.initiatingMessage;
648 select (s1ap) {
649 case (tr_S1AP_DlNasTransport) {
650 return im.value_.DownlinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
651 }
652 case (tr_S1AP_UlNasTransport) {
653 return im.value_.UplinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
654 }
655 case (tr_S1AP_IntialCtxSetupReq) {
656 return im.value_.initialContextSetupRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
657 }
658 case (tr_S1AP_UeContextReleaseReq) {
659 return im.value_.UEContextReleaseRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
660 }
661 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
662 case (tr_S1AP_ConnEstInd) {
663 return im.value_.ConnectionEstablishmentIndication.protocolIEs[0].value_.MME_UE_S1AP_ID;
664 }
665 /* TODO */
666 }
667 } else if (ischosen(s1ap.successfulOutcome)) {
668 var SuccessfulOutcome so := s1ap.successfulOutcome;
669 select (s1ap) {
670 case (tr_S1AP_InitialCtxSetupResp) {
671 return so.value_.initialContextSetupResponse.protocolIEs[0].value_.MME_UE_S1AP_ID;
672 }
673 case (tr_S1AP_UeContextReleaseCompl) {
674 return so.value_.UEContextReleaseComplete.protocolIEs[0].value_.MME_UE_S1AP_ID;
675 }
676 /* TODO */
677 }
678 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
679 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
680 select (s1ap) {
681 case (tr_S1AP_InitialCtxSetupFail) {
682 return uo.value_.initialContextSetupFailure.protocolIEs[0].value_.MME_UE_S1AP_ID;
683 }
684 /* TODO */
685 }
686 }
687 return omit;
688}
689
690function f_S1AP_get_NAS_PDU(S1AP_PDU s1ap) return template (omit) NAS_PDU
691{
692 var integer i;
693
694 if (ischosen(s1ap.initiatingMessage)) {
695 var InitiatingMessage im := s1ap.initiatingMessage;
696 select (s1ap) {
697 case (tr_S1AP_DlNasTransport) {
698 var DownlinkNASTransport msg := im.value_.DownlinkNASTransport;
699 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
700 if (msg.protocolIEs[i].id == id_NAS_PDU) {
701 return msg.protocolIEs[i].value_.NAS_PDU;
702 }
703 }
704 }
705 case (tr_S1AP_UlNasTransport) {
706 var UplinkNASTransport msg := im.value_.UplinkNASTransport;
707 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
708 if (msg.protocolIEs[i].id == id_NAS_PDU) {
709 return msg.protocolIEs[i].value_.NAS_PDU;
710 }
711 }
712 }
713 }
714 }
715 return omit;
716}
717
718
719
720}