blob: 9b159d432f4aefe713c7b81917e1eaf0f88958c4 [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
Harald Welte35498112019-07-02 14:26:39 +080097};
98
99type component S1AP_Emulation_CT {
100 /* Port facing to the UDP SUT */
101 port S1AP_CODEC_PT S1AP;
102 /* All S1AP_ConnHdlr S1AP ports connect here
103 * S1AP_Emulation_CT.main needs to figure out what messages
104 * to send where with CLIENT.send() to vc_conn */
105 port S1AP_Conn_PT S1AP_CLIENT;
106 /* currently tracked connections */
107 var AssociationData S1apAssociationTable[16];
Philipp Maierf935f0c2023-07-24 17:58:24 +0200108 /* pending expected S1AP Association (UE oriented) */
Harald Welte35498112019-07-02 14:26:39 +0800109 var ExpectData S1apExpectTable[8];
110 /* procedure based port to register for incoming connections */
111 port S1APEM_PROC_PT S1AP_PROC;
112 /* test port for unit data messages */
113 port S1AP_PT S1AP_UNIT;
114
115 var S1AP_conn_parameters g_pars;
116 var charstring g_s1ap_id;
117 var integer g_s1ap_conn_id := -1;
118}
119
120type function S1APCreateCallback(S1AP_PDU msg, template (omit) MME_UE_S1AP_ID mme_id,
121 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
122runs on S1AP_Emulation_CT return S1AP_ConnHdlr;
123
124type function S1APUnitdataCallback(S1AP_PDU msg)
125runs on S1AP_Emulation_CT return template S1AP_PDU;
126
127type record S1APOps {
128 S1APCreateCallback create_cb,
129 S1APUnitdataCallback unitdata_cb
130}
131
132type record S1AP_conn_parameters {
133 HostName remote_ip,
134 PortNumber remote_sctp_port,
135 HostName local_ip,
136 PortNumber local_sctp_port,
137 NAS_Role role
138}
139
140function tr_S1AP_RecvFrom_R(template S1AP_PDU msg)
141runs on S1AP_Emulation_CT return template S1AP_RecvFrom {
142 var template S1AP_RecvFrom mrf := {
143 connId := g_s1ap_conn_id,
144 remName := ?,
145 remPort := ?,
146 locName := ?,
147 locPort := ?,
148 msg := msg
149 }
150 return mrf;
151}
152
153private function f_s1ap_ids_known(template (omit) MME_UE_S1AP_ID mme_id,
154 template (omit) ENB_UE_S1AP_ID enb_id)
155runs on S1AP_Emulation_CT return boolean {
156 var integer i;
157 log("f_s1ap_ids_known(",mme_id,", ",enb_id,")");
158 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
159 log("tbl[",i,"]: mme=", S1apAssociationTable[i].mme_ue_s1ap_id,
160 ", enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
161 /* skip empty records */
162 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit and
163 S1apAssociationTable[i].enb_ue_s1ap_id == omit) {
164 log("skipping empty ", i);
165 continue;
166 }
167 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit) {
168 log("entry ", i, " has no MME ID yet (enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
169 /* Table doesn't yet know the MME side ID, let's look-up only
170 * based on the eNB side ID */
171 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
172 /* update table with MME side ID */
173 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
174 return true;
175 }
176 } else if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id) and
177 match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
178 return true;
179 }
180 }
181 return false;
182}
183
184private function f_comp_known(S1AP_ConnHdlr client)
185runs on S1AP_Emulation_CT return boolean {
186 var integer i;
187 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
188 if (S1apAssociationTable[i].comp_ref == client) {
189 return true;
190 }
191 }
192 return false;
193}
194
195private function f_assoc_id_by_s1ap_ids(template (omit) MME_UE_S1AP_ID mme_id,
196 template (omit) ENB_UE_S1AP_ID enb_id)
197runs on S1AP_Emulation_CT return integer {
198 var integer i;
199 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
200 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
201 if (istemplatekind(mme_id, "omit")) {
202 return i;
203 } else {
204 if (match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
205 return i;
206 }
207 }
208 }
209 }
210 setverdict(fail, "S1AP Association Table not found by ENB-ID=", enb_id, " MME-ID=", mme_id);
211 mtc.stop;
212}
213
214private function f_assoc_id_by_comp(S1AP_ConnHdlr client)
215runs on S1AP_Emulation_CT return integer {
216 var integer i;
217 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
218 if (S1apAssociationTable[i].comp_ref == client) {
219 return i;
220 }
221 }
222 setverdict(fail, "S1AP Association Table not found by component ", client);
223 mtc.stop;
224}
225
226private function f_assoc_by_comp(S1AP_ConnHdlr client)
227runs on S1AP_Emulation_CT return AssociationData {
228 var integer i := f_assoc_id_by_comp(client);
229 return S1apAssociationTable[i];
230}
231
232private function f_s1ap_id_table_add(S1AP_ConnHdlr comp_ref,
233 template (omit) MME_UE_S1AP_ID mme_id, ENB_UE_S1AP_ID enb_id)
234runs on S1AP_Emulation_CT return integer {
235 var integer i;
236 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
237 if (not isvalue(S1apAssociationTable[i].enb_ue_s1ap_id)) {
238 S1apAssociationTable[i].enb_ue_s1ap_id := enb_id;
239 if (istemplatekind(mme_id, "omit")) {
240 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
241 } else {
242 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
243 }
244 S1apAssociationTable[i].comp_ref := comp_ref;
245 return i;
246 }
247 }
248 testcase.stop("S1AP Association Table full!");
249 return -1;
250}
251
252private function f_s1ap_id_table_del(S1AP_ConnHdlr comp_ref, ENB_UE_S1AP_ID enb_id)
253runs on S1AP_Emulation_CT {
254 var integer i;
255 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
256 if (S1apAssociationTable[i].comp_ref == comp_ref and
Philipp Maier0ce67ab2023-07-07 13:02:11 +0200257 S1apAssociationTable[i].enb_ue_s1ap_id == enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800258 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
259 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
260 S1apAssociationTable[i].comp_ref := null;
261 return;
262 }
263 }
264 setverdict(fail, "S1AP Association Table: Couldn't find to-be-deleted entry!");
265 mtc.stop;
266}
267
268
269private function f_s1ap_id_table_init()
270runs on S1AP_Emulation_CT {
271 for (var integer i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
272 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
273 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
274 S1apAssociationTable[i].cgi := omit;
275 S1apAssociationTable[i].tai := omit;
276 S1apAssociationTable[i].nus := valueof(t_NAS_UE_State(g_pars.role));
277 }
278}
279
280private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := 18) := {
281 sinfo_stream := omit,
282 sinfo_ppid := ppid,
283 remSocks := omit,
284 assocId := omit
285};
286
287private template PortEvent tr_SctpAssocChange := {
288 sctpEvent := {
289 sctpAssocChange := ?
290 }
291}
292private template PortEvent tr_SctpPeerAddrChange := {
293 sctpEvent := {
294 sctpPeerAddrChange := ?
295 }
296}
297
298private function f_s1ap_xceive(template (value) S1AP_PDU tx,
299 template S1AP_PDU rx_t := ?)
300runs on S1AP_Emulation_CT return S1AP_PDU {
301 timer T := 10.0;
302 var S1AP_RecvFrom mrf;
303
304 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, tx));
305 alt {
306 [] S1AP.receive(tr_S1AP_RecvFrom_R(rx_t)) -> value mrf { }
307 [] S1AP.receive(tr_SctpAssocChange) { repeat; }
308 [] S1AP.receive(tr_SctpPeerAddrChange) { repeat; }
309 [] T.timeout {
310 setverdict(fail, "Timeout waiting for ", rx_t);
311 mtc.stop;
312 }
313 }
314 return mrf.msg;
315}
316
317/*
318private function f_nas_try_decaps(PDU_NAS_EPS nas) return PDU_NAS_EPS
319{
320 var PDU_NAS_EPS_SecurityProtectedNASMessage secp_nas;
321 if (not match(nas, tr_NAS_EMM_SecurityProtected)) {
322 return nas;
323 }
324 secp_nas := nas.ePS_messages.ePS_MobilityManagement.pDU_NAS_EPS_SecurityProtectedNASMessage;
325 select (secp_nas.securityHeaderType) {
326 case ('0011'B) {
327 var octetstring knas_int := '530ce32318f26264eab26bc116870b86'O;
328 var octetstring data_with_seq := int2oct(secp_nas.sequenceNumber, 1) & secp_nas.nAS_Message;
329 var OCT4 exp_mac := f_snow_3g_f9(knas_int, secp_nas.sequenceNumber, 0,
330 is_downlink:=true, data:=data_with_seq);
331 if (exp_mac != secp_nas.messageAuthenticationCode) {
332 setverdict(fail, "Received NAS MAC ", secp_nas.messageAuthenticationCode,
333 " doesn't match expected MAC ", exp_mac, ": ", nas);
334 mtc.stop;
335 }
336 return dec_PDU_NAS_EPS(secp_nas.nAS_Message);
337 }
338 case else {
339 setverdict(fail, "Implement SecHdrType for ", secp_nas);
340 mtc.stop;
341 }
342 }
343}
344*/
345
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200346function handle_S1AP_UeContextReleaseCmd(template (present) S1AP_PDU rel_cmd) runs on S1AP_Emulation_CT {
347 if (ispresent(rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair)) {
348 var template MME_UE_S1AP_ID mme_ue_id;
349 var template ENB_UE_S1AP_ID enb_ue_id;
350 var integer assoc_id;
351 var S1AP_ConnHdlr vc_conn
352
353 mme_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.mME_UE_S1AP_ID;
354 enb_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.eNB_UE_S1AP_ID;
355
356 assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
357 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
358
359 f_s1ap_id_table_del(vc_conn, valueof(enb_ue_id));
360 } else {
361 /* TODO: The UE CONTEXT RELEASE COMMAND (see also: 3GPP TS 36.413, section 9.1.4.6), may identify the
362 * context by either an uE_S1AP_ID_pair (MME_UE_S1AP_ID and ENB_UE_S1AP_ID) or an MME_UE_S1AP_ID alone.
363 * The latter case is not implemented here yet. */
364 setverdict(fail, "complete implementation of UeContextReleaseCmd handling");
365 mtc.stop;
366 }
367}
368
Harald Welte35498112019-07-02 14:26:39 +0800369function main(S1APOps ops, S1AP_conn_parameters p, charstring id) runs on S1AP_Emulation_CT {
370 var Result res;
371 g_pars := p;
372 g_s1ap_id := id;
373 f_s1ap_id_table_init();
374 f_expect_table_init();
375
376 map(self:S1AP, system:S1AP_CODEC_PT);
377 if (p.remote_sctp_port == -1) {
378 res := S1AP_CodecPort_CtrlFunct.f_IPL4_listen(S1AP, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
379 } else {
380 res := S1AP_CodecPort_CtrlFunct.f_IPL4_connect(S1AP, p.remote_ip, p.remote_sctp_port,
381 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
382 }
383 if (not ispresent(res.connId)) {
384 setverdict(fail, "Could not connect S1AP socket, check your configuration");
385 mtc.stop;
386 }
387 g_s1ap_conn_id := res.connId;
388
389 /* notify user about SCTP establishment */
390 if (p.remote_sctp_port != -1) {
391 S1AP_UNIT.send(S1APEM_Event:{up_down:=S1APEM_EVENT_UP})
392 }
393
394 while (true) {
395 var S1AP_ConnHdlr vc_conn;
396 var PDU_NAS_EPS nas;
Philipp Maier7147c922023-07-07 14:18:32 +0200397 var MME_UE_S1AP_ID mme_id;
398 var ENB_UE_S1AP_ID enb_id;
Harald Welte35498112019-07-02 14:26:39 +0800399 var S1AP_RecvFrom mrf;
400 var S1AP_PDU msg;
401 var S1APEM_Config s1cfg;
402 var charstring vlr_name, mme_name;
403 var integer ai;
404
405 alt {
406 /* Configuration primitive from client */
407 [] S1AP_CLIENT.receive(S1APEM_Config:{set_nas_keys:=?}) -> value s1cfg sender vc_conn {
408 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
409 S1apAssociationTable[assoc_id].nus.k_nas_int := s1cfg.set_nas_keys.k_nas_int;
410 S1apAssociationTable[assoc_id].nus.k_nas_enc := s1cfg.set_nas_keys.k_nas_enc;
411 }
412 /* S1AP from client: InitialUE */
413 [] S1AP_CLIENT.receive(tr_S1AP_InitialUE) -> value msg sender vc_conn {
414 /* create a table entry about this connection */
415 ai := f_s1ap_id_table_add(vc_conn, omit, valueof(f_S1AP_get_ENB_UE_S1AP_ID(msg)));
416 /* Store CGI + TAI so we can use it for generating UlNasTransport from NAS */
417 S1apAssociationTable[ai].tai := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[2].value_.TAI;
418 S1apAssociationTable[ai].cgi := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[3].value_.EUTRAN_CGI;
419 /* Pass message through */
420 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
421 }
422 /* NAS from client: Wrap in S1AP Uplink NAS Transport */
423 [] S1AP_CLIENT.receive(PDU_NAS_EPS:?) -> value nas sender vc_conn {
424 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
425 var AssociationData ad := S1apAssociationTable[assoc_id];
426 nas := f_nas_encaps(S1apAssociationTable[assoc_id].nus, nas, new_ctx := false);
427 var octetstring nas_enc := enc_PDU_NAS_EPS(nas);
428 S1AP.send(t_S1AP_Send(g_s1ap_conn_id,
429 ts_S1AP_UlNasTransport(ad.mme_ue_s1ap_id,
430 ad.enb_ue_s1ap_id,
431 nas_enc, ad.cgi, ad.tai)));
432 }
433 /* S1AP from client: pass on transparently */
434 [] S1AP_CLIENT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
435 /* Pass message through */
436 /* FIXME: validate S1AP_IDs ? */
437 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
438 }
439
440 /* non-UE related S1AP: pass through unmodified/unverified */
441 [] S1AP_UNIT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
442 /* Pass message through */
443 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
444 }
445
446 /* S1AP received from peer (MME) */
447 [] S1AP.receive(tr_S1AP_RecvFrom_R(?)) -> value mrf {
448 if (match(mrf.msg, tr_S1AP_nonUErelated)) {
449 /* non-UE-related S1AP message */
450 var template S1AP_PDU resp := ops.unitdata_cb.apply(mrf.msg);
451 if (isvalue(resp)) {
452 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, valueof(resp)));
453 }
454 } else if (match(mrf.msg, tr_S1AP_UeContextReleaseCmd)) {
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200455 handle_S1AP_UeContextReleaseCmd(mrf.msg);
Harald Welte35498112019-07-02 14:26:39 +0800456 } else {
457 /* Ue-related S1AP message */
458 /* obtain MME + ENB UE S1AP ID */
459 var template (omit) MME_UE_S1AP_ID mme_ue_id := f_S1AP_get_MME_UE_S1AP_ID(mrf.msg);
460 var template (omit) ENB_UE_S1AP_ID enb_ue_id := f_S1AP_get_ENB_UE_S1AP_ID(mrf.msg);
461 /* check if those IDs are known in our table */
462 if (f_s1ap_ids_known(mme_ue_id, enb_ue_id)) {
463 /* if yes, dispatch to the ConnHdlr for this Ue-Connection */
464 var template (omit) octetstring nas_enc;
Philipp Maier7147c922023-07-07 14:18:32 +0200465 var integer assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
Harald Welte35498112019-07-02 14:26:39 +0800466 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
467 nas_enc := f_S1AP_get_NAS_PDU(mrf.msg);
468 if (isvalue(nas_enc)) {
469 nas := dec_PDU_NAS_EPS(valueof(nas_enc));
470 if (match(nas, tr_NAS_EMM_SecurityProtected)) {
471 nas := f_nas_try_decaps(S1apAssociationTable[assoc_id].nus, nas);
472 }
473 /* send decoded NAS */
474 S1AP_CLIENT.send(nas) to vc_conn;
475 } else {
476 /* send raw S1AP */
477 S1AP_CLIENT.send(mrf.msg) to vc_conn;
478 }
479 } else {
480 /* if not, call create_cb so it can create new ConnHdlr */
481 vc_conn := ops.create_cb.apply(mrf.msg, mme_ue_id, enb_ue_id, id);
482 f_s1ap_id_table_add(vc_conn, mme_ue_id, valueof(enb_ue_id));
483 S1AP_CLIENT.send(mrf.msg) to vc_conn;
484 }
485 }
486 }
487 [] S1AP.receive(tr_SctpAssocChange) { }
488 [] S1AP.receive(tr_SctpPeerAddrChange) { }
Philipp Maier7147c922023-07-07 14:18:32 +0200489 [] S1AP_PROC.getcall(S1APEM_register:{?,?,?}) -> param(mme_id, enb_id, vc_conn) {
490 f_create_expect(mme_id, enb_id, vc_conn);
491 S1AP_PROC.reply(S1APEM_register:{mme_id, enb_id, vc_conn}) to vc_conn;
Harald Welte35498112019-07-02 14:26:39 +0800492 }
493 }
494
495 }
496}
497
498/* "Expect" Handling */
499
500type record ExpectData {
Philipp Maier7147c922023-07-07 14:18:32 +0200501 MME_UE_S1AP_ID mme_id optional,
502 ENB_UE_S1AP_ID enb_id optional,
Harald Welte35498112019-07-02 14:26:39 +0800503 S1AP_ConnHdlr vc_conn
504}
505
Philipp Maier7147c922023-07-07 14:18:32 +0200506signature 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 +0800507
508type port S1APEM_PROC_PT procedure {
509 inout S1APEM_register;
510} with { extension "internal" };
511
Oliver Smith23e192e2023-02-13 15:00:46 +0100512/* Function that can be used as create_cb and will use the expect table */
Philipp Maier7147c922023-07-07 14:18:32 +0200513function ExpectedCreateCallback(S1AP_PDU msg,
514 template (omit) MME_UE_S1AP_ID mme_id,
515 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
Harald Welte35498112019-07-02 14:26:39 +0800516runs on S1AP_Emulation_CT return S1AP_ConnHdlr {
517 var S1AP_ConnHdlr ret := null;
518 var integer i;
519
520 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200521 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
Harald Welte35498112019-07-02 14:26:39 +0800522 continue;
523 }
Philipp Maier7147c922023-07-07 14:18:32 +0200524
525 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800526 ret := S1apExpectTable[i].vc_conn;
527 /* Release this entry */
Philipp Maier7147c922023-07-07 14:18:32 +0200528 S1apExpectTable[i].mme_id := omit;
529 S1apExpectTable[i].enb_id := omit;
Harald Welte35498112019-07-02 14:26:39 +0800530 S1apExpectTable[i].vc_conn := null;
531 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
532 return ret;
533 }
534 }
535 setverdict(fail, "Couldn't find Expect for ", msg);
536 mtc.stop;
537}
538
Philipp Maier7147c922023-07-07 14:18:32 +0200539private function f_create_expect(template (omit) MME_UE_S1AP_ID mme_id,
540 template (omit) ENB_UE_S1AP_ID enb_id,
541 S1AP_ConnHdlr hdlr)
Harald Welte35498112019-07-02 14:26:39 +0800542runs on S1AP_Emulation_CT {
543 var integer i;
544
545 /* Check an entry like this is not already presnt */
546 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200547 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
548 continue;
549 }
550 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
551 setverdict(fail, "UE MME id / UE ENB id pair already present", mme_id, enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800552 mtc.stop;
553 }
554 }
555 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200556 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
557 S1apExpectTable[i].mme_id := valueof(mme_id);
558 S1apExpectTable[i].enb_id := valueof(enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800559 S1apExpectTable[i].vc_conn := hdlr;
Philipp Maier7147c922023-07-07 14:18:32 +0200560 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 +0800561 return;
562 }
563 }
564 testcase.stop("No space left in S1apExpectTable")
565}
566
567/* client/conn_hdlr side function to use procedure port to create expect in emulation */
Philipp Maier7147c922023-07-07 14:18:32 +0200568function f_create_s1ap_expect(template (omit) MME_UE_S1AP_ID mme_id,
569 template (omit) ENB_UE_S1AP_ID enb_id) runs on S1AP_ConnHdlr {
570 S1AP_PROC.call(S1APEM_register:{mme_id, enb_id, self}) {
571 [] S1AP_PROC.getreply(S1APEM_register:{?,?,?}) {};
Harald Welte35498112019-07-02 14:26:39 +0800572 }
573}
574
575
576private function f_expect_table_init()
577runs on S1AP_Emulation_CT {
578 var integer i;
579 for (i := 0; i < sizeof(S1apExpectTable); i := i + 1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200580 S1apExpectTable[i].mme_id := omit;
581 S1apExpectTable[i].enb_id := omit;
582 S1apExpectTable[i].vc_conn := null;
Harald Welte35498112019-07-02 14:26:39 +0800583 }
584}
585
586function DummyUnitdataCallback(S1AP_PDU msg)
587runs on S1AP_Emulation_CT return template S1AP_PDU {
588 log("Ignoring S1AP ", msg);
589 return omit;
590}
591
592
593function f_S1AP_get_ENB_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) ENB_UE_S1AP_ID
594{
595 if (ischosen(s1ap.initiatingMessage)) {
596 var InitiatingMessage im := s1ap.initiatingMessage;
597 select (s1ap) {
598 case (tr_S1AP_InitialUE) {
599 return im.value_.InitialUEMessage.protocolIEs[0].value_.ENB_UE_S1AP_ID;
600 }
601 case (tr_S1AP_DlNasTransport) {
602 return im.value_.DownlinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
603 }
604 case (tr_S1AP_UlNasTransport) {
605 return im.value_.UplinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
606 }
607 case (tr_S1AP_IntialCtxSetupReq) {
608 return im.value_.initialContextSetupRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
609 }
610 case (tr_S1AP_UeContextReleaseReq) {
611 return im.value_.UEContextReleaseRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
612 }
613 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
614 case (tr_S1AP_ConnEstInd) {
615 return im.value_.ConnectionEstablishmentIndication.protocolIEs[1].value_.ENB_UE_S1AP_ID;
616 }
617 /* TODO */
618 }
619 } else if (ischosen(s1ap.successfulOutcome)) {
620 var SuccessfulOutcome so := s1ap.successfulOutcome;
621 select (s1ap) {
622 case (tr_S1AP_InitialCtxSetupResp) {
623 return so.value_.initialContextSetupResponse.protocolIEs[1].value_.ENB_UE_S1AP_ID;
624 }
625 case (tr_S1AP_UeContextReleaseCompl) {
626 return so.value_.UEContextReleaseComplete.protocolIEs[1].value_.ENB_UE_S1AP_ID;
627 }
628 /* TODO */
629 }
630 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
631 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
632 select (s1ap) {
633 case (tr_S1AP_InitialCtxSetupFail) {
634 return uo.value_.initialContextSetupFailure.protocolIEs[1].value_.ENB_UE_S1AP_ID;
635 }
636 /* TODO */
637 }
638 }
639 return omit;
640}
641
642function f_S1AP_get_MME_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) MME_UE_S1AP_ID
643{
644 if (ischosen(s1ap.initiatingMessage)) {
645 var InitiatingMessage im := s1ap.initiatingMessage;
646 select (s1ap) {
647 case (tr_S1AP_DlNasTransport) {
648 return im.value_.DownlinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
649 }
650 case (tr_S1AP_UlNasTransport) {
651 return im.value_.UplinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
652 }
653 case (tr_S1AP_IntialCtxSetupReq) {
654 return im.value_.initialContextSetupRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
655 }
656 case (tr_S1AP_UeContextReleaseReq) {
657 return im.value_.UEContextReleaseRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
658 }
659 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
660 case (tr_S1AP_ConnEstInd) {
661 return im.value_.ConnectionEstablishmentIndication.protocolIEs[0].value_.MME_UE_S1AP_ID;
662 }
663 /* TODO */
664 }
665 } else if (ischosen(s1ap.successfulOutcome)) {
666 var SuccessfulOutcome so := s1ap.successfulOutcome;
667 select (s1ap) {
668 case (tr_S1AP_InitialCtxSetupResp) {
669 return so.value_.initialContextSetupResponse.protocolIEs[0].value_.MME_UE_S1AP_ID;
670 }
671 case (tr_S1AP_UeContextReleaseCompl) {
672 return so.value_.UEContextReleaseComplete.protocolIEs[0].value_.MME_UE_S1AP_ID;
673 }
674 /* TODO */
675 }
676 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
677 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
678 select (s1ap) {
679 case (tr_S1AP_InitialCtxSetupFail) {
680 return uo.value_.initialContextSetupFailure.protocolIEs[0].value_.MME_UE_S1AP_ID;
681 }
682 /* TODO */
683 }
684 }
685 return omit;
686}
687
688function f_S1AP_get_NAS_PDU(S1AP_PDU s1ap) return template (omit) NAS_PDU
689{
690 var integer i;
691
692 if (ischosen(s1ap.initiatingMessage)) {
693 var InitiatingMessage im := s1ap.initiatingMessage;
694 select (s1ap) {
695 case (tr_S1AP_DlNasTransport) {
696 var DownlinkNASTransport msg := im.value_.DownlinkNASTransport;
697 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
698 if (msg.protocolIEs[i].id == id_NAS_PDU) {
699 return msg.protocolIEs[i].value_.NAS_PDU;
700 }
701 }
702 }
703 case (tr_S1AP_UlNasTransport) {
704 var UplinkNASTransport msg := im.value_.UplinkNASTransport;
705 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
706 if (msg.protocolIEs[i].id == id_NAS_PDU) {
707 return msg.protocolIEs[i].value_.NAS_PDU;
708 }
709 }
710 }
711 }
712 }
713 return omit;
714}
715
716
717
718}