blob: 9f36225124eb0773a192cdcc2327e054ad04de66 [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
Philipp Maierfc7ada22023-07-24 11:32:12 +020018 * MME_UE_S1AP_ID/ENB_UE_S1AP_ID identifiers. It is also possible to register
19 * an expect for a specific procedureCode, in case the expected message is non
20 * UE related (unit-data).
Harald Welte35498112019-07-02 14:26:39 +080021 *
Philipp Maier7147c922023-07-07 14:18:32 +020022 * Inbound non-UE related S1AP messages (such as RESET, SETUP, OVERLOAD) are
23 * dispatched to the S1apOps.unitdata_cb() callback, which is registered with
24 * an argument to the main() function below.
Harald Welte35498112019-07-02 14:26:39 +080025 *
26 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
27 * All rights reserved.
28 *
29 * Released under the terms of GNU General Public License, Version 2 or
30 * (at your option) any later version.
31 *
32 * SPDX-License-Identifier: GPL-2.0-or-later
33 */
34
35import from S1AP_CodecPort all;
36import from S1AP_CodecPort_CtrlFunct all;
37import from S1AP_Types all;
38import from S1AP_Constants all;
39import from S1AP_PDU_Contents all;
40import from S1AP_PDU_Descriptions all;
41import from S1AP_IEs all;
42import from S1AP_Templates all;
43
44import from NAS_EPS_Types all;
45import from NAS_Templates all;
46
47import from LTE_CryptoFunctions all;
48
49import from General_Types all;
50import from Osmocom_Types all;
51import from IPL4asp_Types all;
52import from DNS_Helpers all;
53
54
55type component S1AP_ConnHdlr {
56 port S1AP_Conn_PT S1AP;
57 /* procedure based port to register for incoming connections */
58 port S1APEM_PROC_PT S1AP_PROC;
59}
60
61/* port between individual per-connection components and this dispatcher */
62type port S1AP_Conn_PT message {
63 inout S1AP_PDU, PDU_NAS_EPS, S1APEM_Config;
64} with { extension "internal" };
65
66type record NAS_Keys {
67 octetstring k_nas_int,
68 octetstring k_nas_enc
69};
70type union S1APEM_Config {
71 NAS_Keys set_nas_keys
72};
73
74type enumerated S1APEM_EventUpDown {
75 S1APEM_EVENT_DOWN,
76 S1APEM_EVENT_UP
77}
78
79/* an event indicating us whether or not a connection is physically up or down,
80 * and whether we have received an ID_ACK */
81type union S1APEM_Event {
82 S1APEM_EventUpDown up_down
83}
84
85/* global test port e.g. for non-imsi/conn specific messages */
86type port S1AP_PT message {
87 inout S1AP_PDU, S1APEM_Event;
88} with { extension "internal" };
89
90
91/* represents a single S1AP Association */
92type record AssociationData {
93 S1AP_ConnHdlr comp_ref, /* component handling this UE connection */
94 uint24_t enb_ue_s1ap_id optional, /* eNB side S1AP ID */
95 uint32_t mme_ue_s1ap_id optional, /* MME side S1AP ID */
96 EUTRAN_CGI cgi optional,
97 TAI tai optional,
98 NAS_UE_State nus
Harald Welte35498112019-07-02 14:26:39 +080099};
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];
Philipp Maierf935f0c2023-07-24 17:58:24 +0200110 /* pending expected S1AP Association (UE oriented) */
Harald Welte35498112019-07-02 14:26:39 +0800111 var ExpectData S1apExpectTable[8];
Philipp Maierfc7ada22023-07-24 11:32:12 +0200112 /* pending expected S1AP PDU */
113 var ExpectDataProc S1apExpectTableProc[8];
Harald Welte35498112019-07-02 14:26:39 +0800114 /* procedure based port to register for incoming connections */
115 port S1APEM_PROC_PT S1AP_PROC;
116 /* test port for unit data messages */
117 port S1AP_PT S1AP_UNIT;
118
119 var S1AP_conn_parameters g_pars;
120 var charstring g_s1ap_id;
121 var integer g_s1ap_conn_id := -1;
122}
123
124type function S1APCreateCallback(S1AP_PDU msg, template (omit) MME_UE_S1AP_ID mme_id,
125 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
126runs on S1AP_Emulation_CT return S1AP_ConnHdlr;
127
128type function S1APUnitdataCallback(S1AP_PDU msg)
129runs on S1AP_Emulation_CT return template S1AP_PDU;
130
131type record S1APOps {
132 S1APCreateCallback create_cb,
133 S1APUnitdataCallback unitdata_cb
134}
135
136type record S1AP_conn_parameters {
137 HostName remote_ip,
138 PortNumber remote_sctp_port,
139 HostName local_ip,
140 PortNumber local_sctp_port,
141 NAS_Role role
142}
143
144function tr_S1AP_RecvFrom_R(template S1AP_PDU msg)
145runs on S1AP_Emulation_CT return template S1AP_RecvFrom {
146 var template S1AP_RecvFrom mrf := {
147 connId := g_s1ap_conn_id,
148 remName := ?,
149 remPort := ?,
150 locName := ?,
151 locPort := ?,
152 msg := msg
153 }
154 return mrf;
155}
156
157private function f_s1ap_ids_known(template (omit) MME_UE_S1AP_ID mme_id,
158 template (omit) ENB_UE_S1AP_ID enb_id)
159runs on S1AP_Emulation_CT return boolean {
160 var integer i;
161 log("f_s1ap_ids_known(",mme_id,", ",enb_id,")");
162 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
163 log("tbl[",i,"]: mme=", S1apAssociationTable[i].mme_ue_s1ap_id,
164 ", enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
165 /* skip empty records */
166 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit and
167 S1apAssociationTable[i].enb_ue_s1ap_id == omit) {
168 log("skipping empty ", i);
169 continue;
170 }
171 if (S1apAssociationTable[i].mme_ue_s1ap_id == omit) {
172 log("entry ", i, " has no MME ID yet (enb=", S1apAssociationTable[i].enb_ue_s1ap_id);
173 /* Table doesn't yet know the MME side ID, let's look-up only
174 * based on the eNB side ID */
175 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
176 /* update table with MME side ID */
177 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
178 return true;
179 }
180 } else if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id) and
181 match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
182 return true;
183 }
184 }
185 return false;
186}
187
188private function f_comp_known(S1AP_ConnHdlr client)
189runs on S1AP_Emulation_CT return boolean {
190 var integer i;
191 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
192 if (S1apAssociationTable[i].comp_ref == client) {
193 return true;
194 }
195 }
196 return false;
197}
198
199private function f_assoc_id_by_s1ap_ids(template (omit) MME_UE_S1AP_ID mme_id,
200 template (omit) ENB_UE_S1AP_ID enb_id)
201runs on S1AP_Emulation_CT return integer {
202 var integer i;
203 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
204 if (match(S1apAssociationTable[i].enb_ue_s1ap_id, enb_id)) {
205 if (istemplatekind(mme_id, "omit")) {
206 return i;
207 } else {
208 if (match(S1apAssociationTable[i].mme_ue_s1ap_id, mme_id)) {
209 return i;
210 }
211 }
212 }
213 }
214 setverdict(fail, "S1AP Association Table not found by ENB-ID=", enb_id, " MME-ID=", mme_id);
215 mtc.stop;
216}
217
218private function f_assoc_id_by_comp(S1AP_ConnHdlr client)
219runs on S1AP_Emulation_CT return integer {
220 var integer i;
221 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
222 if (S1apAssociationTable[i].comp_ref == client) {
223 return i;
224 }
225 }
226 setverdict(fail, "S1AP Association Table not found by component ", client);
227 mtc.stop;
228}
229
230private function f_assoc_by_comp(S1AP_ConnHdlr client)
231runs on S1AP_Emulation_CT return AssociationData {
232 var integer i := f_assoc_id_by_comp(client);
233 return S1apAssociationTable[i];
234}
235
236private function f_s1ap_id_table_add(S1AP_ConnHdlr comp_ref,
237 template (omit) MME_UE_S1AP_ID mme_id, ENB_UE_S1AP_ID enb_id)
238runs on S1AP_Emulation_CT return integer {
239 var integer i;
240 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
241 if (not isvalue(S1apAssociationTable[i].enb_ue_s1ap_id)) {
242 S1apAssociationTable[i].enb_ue_s1ap_id := enb_id;
243 if (istemplatekind(mme_id, "omit")) {
244 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
245 } else {
246 S1apAssociationTable[i].mme_ue_s1ap_id := valueof(mme_id);
247 }
248 S1apAssociationTable[i].comp_ref := comp_ref;
249 return i;
250 }
251 }
252 testcase.stop("S1AP Association Table full!");
253 return -1;
254}
255
256private function f_s1ap_id_table_del(S1AP_ConnHdlr comp_ref, ENB_UE_S1AP_ID enb_id)
257runs on S1AP_Emulation_CT {
258 var integer i;
259 for (i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
260 if (S1apAssociationTable[i].comp_ref == comp_ref and
Philipp Maier0ce67ab2023-07-07 13:02:11 +0200261 S1apAssociationTable[i].enb_ue_s1ap_id == enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800262 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
263 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
264 S1apAssociationTable[i].comp_ref := null;
265 return;
266 }
267 }
268 setverdict(fail, "S1AP Association Table: Couldn't find to-be-deleted entry!");
269 mtc.stop;
270}
271
272
273private function f_s1ap_id_table_init()
274runs on S1AP_Emulation_CT {
275 for (var integer i := 0; i < sizeof(S1apAssociationTable); i := i+1) {
276 S1apAssociationTable[i].mme_ue_s1ap_id := omit;
277 S1apAssociationTable[i].enb_ue_s1ap_id := omit;
278 S1apAssociationTable[i].cgi := omit;
279 S1apAssociationTable[i].tai := omit;
280 S1apAssociationTable[i].nus := valueof(t_NAS_UE_State(g_pars.role));
Philipp Maiereb930fd2023-07-24 17:59:40 +0200281 S1apAssociationTable[i].comp_ref := null;
Harald Welte35498112019-07-02 14:26:39 +0800282 }
283}
284
285private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := 18) := {
286 sinfo_stream := omit,
287 sinfo_ppid := ppid,
288 remSocks := omit,
289 assocId := omit
290};
291
292private template PortEvent tr_SctpAssocChange := {
293 sctpEvent := {
294 sctpAssocChange := ?
295 }
296}
297private template PortEvent tr_SctpPeerAddrChange := {
298 sctpEvent := {
299 sctpPeerAddrChange := ?
300 }
301}
302
303private function f_s1ap_xceive(template (value) S1AP_PDU tx,
304 template S1AP_PDU rx_t := ?)
305runs on S1AP_Emulation_CT return S1AP_PDU {
306 timer T := 10.0;
307 var S1AP_RecvFrom mrf;
308
309 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, tx));
310 alt {
311 [] S1AP.receive(tr_S1AP_RecvFrom_R(rx_t)) -> value mrf { }
312 [] S1AP.receive(tr_SctpAssocChange) { repeat; }
313 [] S1AP.receive(tr_SctpPeerAddrChange) { repeat; }
314 [] T.timeout {
315 setverdict(fail, "Timeout waiting for ", rx_t);
316 mtc.stop;
317 }
318 }
319 return mrf.msg;
320}
321
322/*
323private function f_nas_try_decaps(PDU_NAS_EPS nas) return PDU_NAS_EPS
324{
325 var PDU_NAS_EPS_SecurityProtectedNASMessage secp_nas;
326 if (not match(nas, tr_NAS_EMM_SecurityProtected)) {
327 return nas;
328 }
329 secp_nas := nas.ePS_messages.ePS_MobilityManagement.pDU_NAS_EPS_SecurityProtectedNASMessage;
330 select (secp_nas.securityHeaderType) {
331 case ('0011'B) {
332 var octetstring knas_int := '530ce32318f26264eab26bc116870b86'O;
333 var octetstring data_with_seq := int2oct(secp_nas.sequenceNumber, 1) & secp_nas.nAS_Message;
334 var OCT4 exp_mac := f_snow_3g_f9(knas_int, secp_nas.sequenceNumber, 0,
335 is_downlink:=true, data:=data_with_seq);
336 if (exp_mac != secp_nas.messageAuthenticationCode) {
337 setverdict(fail, "Received NAS MAC ", secp_nas.messageAuthenticationCode,
338 " doesn't match expected MAC ", exp_mac, ": ", nas);
339 mtc.stop;
340 }
341 return dec_PDU_NAS_EPS(secp_nas.nAS_Message);
342 }
343 case else {
344 setverdict(fail, "Implement SecHdrType for ", secp_nas);
345 mtc.stop;
346 }
347 }
348}
349*/
350
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200351function handle_S1AP_UeContextReleaseCmd(template (present) S1AP_PDU rel_cmd) runs on S1AP_Emulation_CT {
352 if (ispresent(rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair)) {
353 var template MME_UE_S1AP_ID mme_ue_id;
354 var template ENB_UE_S1AP_ID enb_ue_id;
355 var integer assoc_id;
356 var S1AP_ConnHdlr vc_conn
357
358 mme_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.mME_UE_S1AP_ID;
359 enb_ue_id := rel_cmd.initiatingMessage.value_.uEContextReleaseCommand.protocolIEs[0].value_.uE_S1AP_IDs.uE_S1AP_ID_pair.eNB_UE_S1AP_ID;
360
361 assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
362 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
363
364 f_s1ap_id_table_del(vc_conn, valueof(enb_ue_id));
365 } else {
366 /* TODO: The UE CONTEXT RELEASE COMMAND (see also: 3GPP TS 36.413, section 9.1.4.6), may identify the
367 * context by either an uE_S1AP_ID_pair (MME_UE_S1AP_ID and ENB_UE_S1AP_ID) or an MME_UE_S1AP_ID alone.
368 * The latter case is not implemented here yet. */
369 setverdict(fail, "complete implementation of UeContextReleaseCmd handling");
370 mtc.stop;
371 }
372}
373
Philipp Maierfc7ada22023-07-24 11:32:12 +0200374private function SendToS1apExpectTableProc(S1AP_PDU msg) runs on S1AP_Emulation_CT {
375 var integer procedureCode;
376 var S1AP_ConnHdlr vc_conn;
377
378 if (ispresent(msg.initiatingMessage.procedureCode)) {
379 procedureCode := msg.initiatingMessage.procedureCode;
380 } else if (ispresent(msg.unsuccessfulOutcome.procedureCode)) {
381 procedureCode := msg.unsuccessfulOutcome.procedureCode;
382 } else if (ispresent(msg.successfulOutcome.procedureCode)) {
383 procedureCode := msg.successfulOutcome.procedureCode;
384 } else {
385 return;
386 }
387
388 for (var integer i := 0; i < sizeof(S1apExpectTableProc); i := i+1) {
389 if (S1apExpectTableProc[i].procedureCode == procedureCode) {
390 vc_conn := S1apExpectTableProc[i].vc_conn;
391 if (vc_conn != null) {
392 S1AP_CLIENT.send(msg) to vc_conn;
393 }
394 }
395 }
396
397 return;
398}
399
Harald Welte35498112019-07-02 14:26:39 +0800400function main(S1APOps ops, S1AP_conn_parameters p, charstring id) runs on S1AP_Emulation_CT {
401 var Result res;
402 g_pars := p;
403 g_s1ap_id := id;
404 f_s1ap_id_table_init();
405 f_expect_table_init();
406
407 map(self:S1AP, system:S1AP_CODEC_PT);
408 if (p.remote_sctp_port == -1) {
409 res := S1AP_CodecPort_CtrlFunct.f_IPL4_listen(S1AP, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
410 } else {
411 res := S1AP_CodecPort_CtrlFunct.f_IPL4_connect(S1AP, p.remote_ip, p.remote_sctp_port,
412 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
413 }
414 if (not ispresent(res.connId)) {
415 setverdict(fail, "Could not connect S1AP socket, check your configuration");
416 mtc.stop;
417 }
418 g_s1ap_conn_id := res.connId;
419
420 /* notify user about SCTP establishment */
421 if (p.remote_sctp_port != -1) {
422 S1AP_UNIT.send(S1APEM_Event:{up_down:=S1APEM_EVENT_UP})
423 }
424
425 while (true) {
426 var S1AP_ConnHdlr vc_conn;
427 var PDU_NAS_EPS nas;
Philipp Maier7147c922023-07-07 14:18:32 +0200428 var MME_UE_S1AP_ID mme_id;
429 var ENB_UE_S1AP_ID enb_id;
Philipp Maierfc7ada22023-07-24 11:32:12 +0200430 var integer procedureCode;
Harald Welte35498112019-07-02 14:26:39 +0800431 var S1AP_RecvFrom mrf;
432 var S1AP_PDU msg;
433 var S1APEM_Config s1cfg;
434 var charstring vlr_name, mme_name;
435 var integer ai;
436
437 alt {
438 /* Configuration primitive from client */
439 [] S1AP_CLIENT.receive(S1APEM_Config:{set_nas_keys:=?}) -> value s1cfg sender vc_conn {
440 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
441 S1apAssociationTable[assoc_id].nus.k_nas_int := s1cfg.set_nas_keys.k_nas_int;
442 S1apAssociationTable[assoc_id].nus.k_nas_enc := s1cfg.set_nas_keys.k_nas_enc;
443 }
444 /* S1AP from client: InitialUE */
445 [] S1AP_CLIENT.receive(tr_S1AP_InitialUE) -> value msg sender vc_conn {
446 /* create a table entry about this connection */
447 ai := f_s1ap_id_table_add(vc_conn, omit, valueof(f_S1AP_get_ENB_UE_S1AP_ID(msg)));
448 /* Store CGI + TAI so we can use it for generating UlNasTransport from NAS */
449 S1apAssociationTable[ai].tai := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[2].value_.TAI;
450 S1apAssociationTable[ai].cgi := msg.initiatingMessage.value_.InitialUEMessage.protocolIEs[3].value_.EUTRAN_CGI;
451 /* Pass message through */
452 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
453 }
454 /* NAS from client: Wrap in S1AP Uplink NAS Transport */
455 [] S1AP_CLIENT.receive(PDU_NAS_EPS:?) -> value nas sender vc_conn {
456 var integer assoc_id := f_assoc_id_by_comp(vc_conn);
457 var AssociationData ad := S1apAssociationTable[assoc_id];
458 nas := f_nas_encaps(S1apAssociationTable[assoc_id].nus, nas, new_ctx := false);
459 var octetstring nas_enc := enc_PDU_NAS_EPS(nas);
460 S1AP.send(t_S1AP_Send(g_s1ap_conn_id,
461 ts_S1AP_UlNasTransport(ad.mme_ue_s1ap_id,
462 ad.enb_ue_s1ap_id,
463 nas_enc, ad.cgi, ad.tai)));
464 }
465 /* S1AP from client: pass on transparently */
466 [] S1AP_CLIENT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
467 /* Pass message through */
468 /* FIXME: validate S1AP_IDs ? */
469 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
470 }
471
472 /* non-UE related S1AP: pass through unmodified/unverified */
473 [] S1AP_UNIT.receive(S1AP_PDU:?) -> value msg sender vc_conn {
474 /* Pass message through */
475 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, msg));
476 }
477
478 /* S1AP received from peer (MME) */
479 [] S1AP.receive(tr_S1AP_RecvFrom_R(?)) -> value mrf {
480 if (match(mrf.msg, tr_S1AP_nonUErelated)) {
481 /* non-UE-related S1AP message */
Philipp Maierfc7ada22023-07-24 11:32:12 +0200482 SendToS1apExpectTableProc(mrf.msg);
Harald Welte35498112019-07-02 14:26:39 +0800483 var template S1AP_PDU resp := ops.unitdata_cb.apply(mrf.msg);
484 if (isvalue(resp)) {
485 S1AP.send(t_S1AP_Send(g_s1ap_conn_id, valueof(resp)));
486 }
487 } else if (match(mrf.msg, tr_S1AP_UeContextReleaseCmd)) {
Philipp Maierbb8f05d2023-07-07 14:16:08 +0200488 handle_S1AP_UeContextReleaseCmd(mrf.msg);
Harald Welte35498112019-07-02 14:26:39 +0800489 } else {
490 /* Ue-related S1AP message */
491 /* obtain MME + ENB UE S1AP ID */
492 var template (omit) MME_UE_S1AP_ID mme_ue_id := f_S1AP_get_MME_UE_S1AP_ID(mrf.msg);
493 var template (omit) ENB_UE_S1AP_ID enb_ue_id := f_S1AP_get_ENB_UE_S1AP_ID(mrf.msg);
494 /* check if those IDs are known in our table */
495 if (f_s1ap_ids_known(mme_ue_id, enb_ue_id)) {
496 /* if yes, dispatch to the ConnHdlr for this Ue-Connection */
497 var template (omit) octetstring nas_enc;
Philipp Maier7147c922023-07-07 14:18:32 +0200498 var integer assoc_id := f_assoc_id_by_s1ap_ids(mme_ue_id, enb_ue_id);
Harald Welte35498112019-07-02 14:26:39 +0800499 vc_conn := S1apAssociationTable[assoc_id].comp_ref;
500 nas_enc := f_S1AP_get_NAS_PDU(mrf.msg);
501 if (isvalue(nas_enc)) {
502 nas := dec_PDU_NAS_EPS(valueof(nas_enc));
503 if (match(nas, tr_NAS_EMM_SecurityProtected)) {
504 nas := f_nas_try_decaps(S1apAssociationTable[assoc_id].nus, nas);
505 }
Pau Espin Pedrol2edec462023-12-15 19:16:02 +0100506 /* DL/UlNasTransport are not interesting, don't send them */
507 if (not match(mrf.msg, (tr_S1AP_DlNasTransport, tr_S1AP_UlNasTransport))) {
508 /* send raw S1AP */
509 S1AP_CLIENT.send(mrf.msg) to vc_conn;
510 }
Harald Welte35498112019-07-02 14:26:39 +0800511 /* send decoded NAS */
512 S1AP_CLIENT.send(nas) to vc_conn;
513 } else {
514 /* send raw S1AP */
515 S1AP_CLIENT.send(mrf.msg) to vc_conn;
516 }
517 } else {
518 /* if not, call create_cb so it can create new ConnHdlr */
519 vc_conn := ops.create_cb.apply(mrf.msg, mme_ue_id, enb_ue_id, id);
520 f_s1ap_id_table_add(vc_conn, mme_ue_id, valueof(enb_ue_id));
521 S1AP_CLIENT.send(mrf.msg) to vc_conn;
522 }
523 }
524 }
525 [] S1AP.receive(tr_SctpAssocChange) { }
526 [] S1AP.receive(tr_SctpPeerAddrChange) { }
Philipp Maier7147c922023-07-07 14:18:32 +0200527 [] S1AP_PROC.getcall(S1APEM_register:{?,?,?}) -> param(mme_id, enb_id, vc_conn) {
528 f_create_expect(mme_id, enb_id, vc_conn);
529 S1AP_PROC.reply(S1APEM_register:{mme_id, enb_id, vc_conn}) to vc_conn;
Harald Welte35498112019-07-02 14:26:39 +0800530 }
Philipp Maierfc7ada22023-07-24 11:32:12 +0200531 [] S1AP_PROC.getcall(S1APEM_register_proc:{?,?}) -> param(procedureCode, vc_conn) {
532 f_create_expect_proc(procedureCode, vc_conn);
533 S1AP_PROC.reply(S1APEM_register_proc:{procedureCode, vc_conn}) to vc_conn;
534 }
Harald Welte35498112019-07-02 14:26:39 +0800535 }
536
537 }
538}
539
540/* "Expect" Handling */
541
542type record ExpectData {
Philipp Maier7147c922023-07-07 14:18:32 +0200543 MME_UE_S1AP_ID mme_id optional,
544 ENB_UE_S1AP_ID enb_id optional,
Harald Welte35498112019-07-02 14:26:39 +0800545 S1AP_ConnHdlr vc_conn
546}
547
Philipp Maierfc7ada22023-07-24 11:32:12 +0200548/* represents a single S1AP PDU that we expect. When a matching PDU is seen, it is forwarded to the registered
549 * component */
550type record ExpectDataProc {
551 integer procedureCode optional,
552 S1AP_ConnHdlr vc_conn /* component handling this UE connection */
553};
554
Philipp Maier7147c922023-07-07 14:18:32 +0200555signature S1APEM_register(in MME_UE_S1AP_ID mme_id, in ENB_UE_S1AP_ID enb_id, in S1AP_ConnHdlr hdlr);
Philipp Maierfc7ada22023-07-24 11:32:12 +0200556signature S1APEM_register_proc(in integer procedureCode, in S1AP_ConnHdlr hdlr);
Harald Welte35498112019-07-02 14:26:39 +0800557
558type port S1APEM_PROC_PT procedure {
559 inout S1APEM_register;
Philipp Maierfc7ada22023-07-24 11:32:12 +0200560 inout S1APEM_register_proc;
Harald Welte35498112019-07-02 14:26:39 +0800561} with { extension "internal" };
562
Oliver Smith23e192e2023-02-13 15:00:46 +0100563/* Function that can be used as create_cb and will use the expect table */
Philipp Maier7147c922023-07-07 14:18:32 +0200564function ExpectedCreateCallback(S1AP_PDU msg,
565 template (omit) MME_UE_S1AP_ID mme_id,
566 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
Harald Welte35498112019-07-02 14:26:39 +0800567runs on S1AP_Emulation_CT return S1AP_ConnHdlr {
568 var S1AP_ConnHdlr ret := null;
569 var integer i;
570
571 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200572 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
Harald Welte35498112019-07-02 14:26:39 +0800573 continue;
574 }
Philipp Maier7147c922023-07-07 14:18:32 +0200575
576 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800577 ret := S1apExpectTable[i].vc_conn;
578 /* Release this entry */
Philipp Maier7147c922023-07-07 14:18:32 +0200579 S1apExpectTable[i].mme_id := omit;
580 S1apExpectTable[i].enb_id := omit;
Harald Welte35498112019-07-02 14:26:39 +0800581 S1apExpectTable[i].vc_conn := null;
582 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
583 return ret;
584 }
585 }
586 setverdict(fail, "Couldn't find Expect for ", msg);
587 mtc.stop;
588}
589
Philipp Maier7147c922023-07-07 14:18:32 +0200590private function f_create_expect(template (omit) MME_UE_S1AP_ID mme_id,
591 template (omit) ENB_UE_S1AP_ID enb_id,
592 S1AP_ConnHdlr hdlr)
Harald Welte35498112019-07-02 14:26:39 +0800593runs on S1AP_Emulation_CT {
594 var integer i;
595
596 /* Check an entry like this is not already presnt */
597 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200598 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
599 continue;
600 }
601 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
602 setverdict(fail, "UE MME id / UE ENB id pair already present", mme_id, enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800603 mtc.stop;
604 }
605 }
606 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200607 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
608 S1apExpectTable[i].mme_id := valueof(mme_id);
609 S1apExpectTable[i].enb_id := valueof(enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800610 S1apExpectTable[i].vc_conn := hdlr;
Philipp Maier7147c922023-07-07 14:18:32 +0200611 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 +0800612 return;
613 }
614 }
615 testcase.stop("No space left in S1apExpectTable")
616}
617
618/* client/conn_hdlr side function to use procedure port to create expect in emulation */
Philipp Maier7147c922023-07-07 14:18:32 +0200619function f_create_s1ap_expect(template (omit) MME_UE_S1AP_ID mme_id,
620 template (omit) ENB_UE_S1AP_ID enb_id) runs on S1AP_ConnHdlr {
621 S1AP_PROC.call(S1APEM_register:{mme_id, enb_id, self}) {
622 [] S1AP_PROC.getreply(S1APEM_register:{?,?,?}) {};
Harald Welte35498112019-07-02 14:26:39 +0800623 }
624}
625
Philipp Maierfc7ada22023-07-24 11:32:12 +0200626private function f_create_expect_proc(integer procedureCode, S1AP_ConnHdlr hdlr) runs on S1AP_Emulation_CT {
627 var integer i;
628
629 /* Check an entry like this is not already presnt */
630 for (i := 0; i < sizeof(S1apExpectTableProc); i := i+1) {
631 if (S1apExpectTableProc[i].vc_conn == null) {
632 continue;
633 }
634 if (S1apExpectTableProc[i].procedureCode == procedureCode) {
635 setverdict(fail, "procedureCode ", procedureCode, " already present");
636 mtc.stop;
637 }
638 }
639 for (i := 0; i < sizeof(S1apExpectTableProc); i := i+1) {
640 if (S1apExpectTableProc[i].vc_conn == null) {
641 S1apExpectTableProc[i].procedureCode := procedureCode;
642 S1apExpectTableProc[i].vc_conn := hdlr;
643 log("Created Expect[", i, "] for PDU:", procedureCode, " to be handled at ", hdlr);
644 return;
645 }
646 }
647 testcase.stop("No space left in S1apExpectTableProc")
648}
649
650/* client/conn_hdlr side function to use procedure port to create expect (PDU) in emulation */
651function f_create_s1ap_expect_proc(integer procedureCode, S1AP_ConnHdlr hdlr) runs on S1AP_ConnHdlr
652{
653 S1AP_PROC.call(S1APEM_register_proc:{procedureCode, self}) {
654 [] S1AP_PROC.getreply(S1APEM_register_proc:{?,?}) {};
655 }
656
657 log(procedureCode);
658}
Harald Welte35498112019-07-02 14:26:39 +0800659
660private function f_expect_table_init()
661runs on S1AP_Emulation_CT {
662 var integer i;
663 for (i := 0; i < sizeof(S1apExpectTable); i := i + 1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200664 S1apExpectTable[i].mme_id := omit;
665 S1apExpectTable[i].enb_id := omit;
666 S1apExpectTable[i].vc_conn := null;
Harald Welte35498112019-07-02 14:26:39 +0800667 }
Philipp Maierfc7ada22023-07-24 11:32:12 +0200668
669 for (i := 0; i < sizeof(S1apExpectTableProc); i := i + 1) {
670 S1apExpectTableProc[i].procedureCode := omit;
671 S1apExpectTableProc[i].vc_conn := null;
672 }
Harald Welte35498112019-07-02 14:26:39 +0800673}
674
675function DummyUnitdataCallback(S1AP_PDU msg)
676runs on S1AP_Emulation_CT return template S1AP_PDU {
677 log("Ignoring S1AP ", msg);
678 return omit;
679}
680
681
682function f_S1AP_get_ENB_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) ENB_UE_S1AP_ID
683{
684 if (ischosen(s1ap.initiatingMessage)) {
685 var InitiatingMessage im := s1ap.initiatingMessage;
686 select (s1ap) {
687 case (tr_S1AP_InitialUE) {
688 return im.value_.InitialUEMessage.protocolIEs[0].value_.ENB_UE_S1AP_ID;
689 }
690 case (tr_S1AP_DlNasTransport) {
691 return im.value_.DownlinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
692 }
693 case (tr_S1AP_UlNasTransport) {
694 return im.value_.UplinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
695 }
696 case (tr_S1AP_IntialCtxSetupReq) {
697 return im.value_.initialContextSetupRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
698 }
699 case (tr_S1AP_UeContextReleaseReq) {
700 return im.value_.UEContextReleaseRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
701 }
702 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
703 case (tr_S1AP_ConnEstInd) {
704 return im.value_.ConnectionEstablishmentIndication.protocolIEs[1].value_.ENB_UE_S1AP_ID;
705 }
706 /* TODO */
707 }
708 } else if (ischosen(s1ap.successfulOutcome)) {
709 var SuccessfulOutcome so := s1ap.successfulOutcome;
710 select (s1ap) {
711 case (tr_S1AP_InitialCtxSetupResp) {
712 return so.value_.initialContextSetupResponse.protocolIEs[1].value_.ENB_UE_S1AP_ID;
713 }
714 case (tr_S1AP_UeContextReleaseCompl) {
715 return so.value_.UEContextReleaseComplete.protocolIEs[1].value_.ENB_UE_S1AP_ID;
716 }
717 /* TODO */
718 }
719 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
720 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
721 select (s1ap) {
722 case (tr_S1AP_InitialCtxSetupFail) {
723 return uo.value_.initialContextSetupFailure.protocolIEs[1].value_.ENB_UE_S1AP_ID;
724 }
725 /* TODO */
726 }
727 }
728 return omit;
729}
730
731function f_S1AP_get_MME_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) MME_UE_S1AP_ID
732{
733 if (ischosen(s1ap.initiatingMessage)) {
734 var InitiatingMessage im := s1ap.initiatingMessage;
735 select (s1ap) {
736 case (tr_S1AP_DlNasTransport) {
737 return im.value_.DownlinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
738 }
739 case (tr_S1AP_UlNasTransport) {
740 return im.value_.UplinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
741 }
742 case (tr_S1AP_IntialCtxSetupReq) {
743 return im.value_.initialContextSetupRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
744 }
745 case (tr_S1AP_UeContextReleaseReq) {
746 return im.value_.UEContextReleaseRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
747 }
748 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
749 case (tr_S1AP_ConnEstInd) {
750 return im.value_.ConnectionEstablishmentIndication.protocolIEs[0].value_.MME_UE_S1AP_ID;
751 }
752 /* TODO */
753 }
754 } else if (ischosen(s1ap.successfulOutcome)) {
755 var SuccessfulOutcome so := s1ap.successfulOutcome;
756 select (s1ap) {
757 case (tr_S1AP_InitialCtxSetupResp) {
758 return so.value_.initialContextSetupResponse.protocolIEs[0].value_.MME_UE_S1AP_ID;
759 }
760 case (tr_S1AP_UeContextReleaseCompl) {
761 return so.value_.UEContextReleaseComplete.protocolIEs[0].value_.MME_UE_S1AP_ID;
762 }
763 /* TODO */
764 }
765 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
766 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
767 select (s1ap) {
768 case (tr_S1AP_InitialCtxSetupFail) {
769 return uo.value_.initialContextSetupFailure.protocolIEs[0].value_.MME_UE_S1AP_ID;
770 }
771 /* TODO */
772 }
773 }
774 return omit;
775}
776
777function f_S1AP_get_NAS_PDU(S1AP_PDU s1ap) return template (omit) NAS_PDU
778{
Pau Espin Pedrol2edec462023-12-15 19:16:02 +0100779 var integer i, j;
Harald Welte35498112019-07-02 14:26:39 +0800780
781 if (ischosen(s1ap.initiatingMessage)) {
782 var InitiatingMessage im := s1ap.initiatingMessage;
783 select (s1ap) {
784 case (tr_S1AP_DlNasTransport) {
785 var DownlinkNASTransport msg := im.value_.DownlinkNASTransport;
786 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
787 if (msg.protocolIEs[i].id == id_NAS_PDU) {
788 return msg.protocolIEs[i].value_.NAS_PDU;
789 }
790 }
791 }
792 case (tr_S1AP_UlNasTransport) {
793 var UplinkNASTransport msg := im.value_.UplinkNASTransport;
794 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
795 if (msg.protocolIEs[i].id == id_NAS_PDU) {
796 return msg.protocolIEs[i].value_.NAS_PDU;
797 }
798 }
799 }
Pau Espin Pedrol2edec462023-12-15 19:16:02 +0100800 case (tr_S1AP_IntialCtxSetupReq) {
801 var InitialContextSetupRequest msg := im.value_.initialContextSetupRequest;
802 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
803 if (msg.protocolIEs[i].id == id_E_RABToBeSetupListCtxtSUReq) {
804 var E_RABToBeSetupListCtxtSUReq rab_req := msg.protocolIEs[i].value_.E_RABToBeSetupListCtxtSUReq;
805 for (j := 0; j < lengthof(rab_req); j := j+1) {
806 var E_RABToBeSetupItemCtxtSUReq it := rab_req[j].value_.E_RABToBeSetupItemCtxtSUReq;
807 return it.nAS_PDU;
808 }
809 }
810 }
811 return omit;
812 }
Harald Welte35498112019-07-02 14:26:39 +0800813 }
814 }
815 return omit;
816}
817
818
819
820}