blob: bb3a2a4f0dbf7dc458f9e21a6854c658dc4c418e [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 }
506 /* send decoded NAS */
507 S1AP_CLIENT.send(nas) to vc_conn;
508 } else {
509 /* send raw S1AP */
510 S1AP_CLIENT.send(mrf.msg) to vc_conn;
511 }
512 } else {
513 /* if not, call create_cb so it can create new ConnHdlr */
514 vc_conn := ops.create_cb.apply(mrf.msg, mme_ue_id, enb_ue_id, id);
515 f_s1ap_id_table_add(vc_conn, mme_ue_id, valueof(enb_ue_id));
516 S1AP_CLIENT.send(mrf.msg) to vc_conn;
517 }
518 }
519 }
520 [] S1AP.receive(tr_SctpAssocChange) { }
521 [] S1AP.receive(tr_SctpPeerAddrChange) { }
Philipp Maier7147c922023-07-07 14:18:32 +0200522 [] S1AP_PROC.getcall(S1APEM_register:{?,?,?}) -> param(mme_id, enb_id, vc_conn) {
523 f_create_expect(mme_id, enb_id, vc_conn);
524 S1AP_PROC.reply(S1APEM_register:{mme_id, enb_id, vc_conn}) to vc_conn;
Harald Welte35498112019-07-02 14:26:39 +0800525 }
Philipp Maierfc7ada22023-07-24 11:32:12 +0200526 [] S1AP_PROC.getcall(S1APEM_register_proc:{?,?}) -> param(procedureCode, vc_conn) {
527 f_create_expect_proc(procedureCode, vc_conn);
528 S1AP_PROC.reply(S1APEM_register_proc:{procedureCode, vc_conn}) to vc_conn;
529 }
Harald Welte35498112019-07-02 14:26:39 +0800530 }
531
532 }
533}
534
535/* "Expect" Handling */
536
537type record ExpectData {
Philipp Maier7147c922023-07-07 14:18:32 +0200538 MME_UE_S1AP_ID mme_id optional,
539 ENB_UE_S1AP_ID enb_id optional,
Harald Welte35498112019-07-02 14:26:39 +0800540 S1AP_ConnHdlr vc_conn
541}
542
Philipp Maierfc7ada22023-07-24 11:32:12 +0200543/* represents a single S1AP PDU that we expect. When a matching PDU is seen, it is forwarded to the registered
544 * component */
545type record ExpectDataProc {
546 integer procedureCode optional,
547 S1AP_ConnHdlr vc_conn /* component handling this UE connection */
548};
549
Philipp Maier7147c922023-07-07 14:18:32 +0200550signature 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 +0200551signature S1APEM_register_proc(in integer procedureCode, in S1AP_ConnHdlr hdlr);
Harald Welte35498112019-07-02 14:26:39 +0800552
553type port S1APEM_PROC_PT procedure {
554 inout S1APEM_register;
Philipp Maierfc7ada22023-07-24 11:32:12 +0200555 inout S1APEM_register_proc;
Harald Welte35498112019-07-02 14:26:39 +0800556} with { extension "internal" };
557
Oliver Smith23e192e2023-02-13 15:00:46 +0100558/* Function that can be used as create_cb and will use the expect table */
Philipp Maier7147c922023-07-07 14:18:32 +0200559function ExpectedCreateCallback(S1AP_PDU msg,
560 template (omit) MME_UE_S1AP_ID mme_id,
561 template (omit) ENB_UE_S1AP_ID enb_id, charstring id)
Harald Welte35498112019-07-02 14:26:39 +0800562runs on S1AP_Emulation_CT return S1AP_ConnHdlr {
563 var S1AP_ConnHdlr ret := null;
564 var integer i;
565
566 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200567 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
Harald Welte35498112019-07-02 14:26:39 +0800568 continue;
569 }
Philipp Maier7147c922023-07-07 14:18:32 +0200570
571 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
Harald Welte35498112019-07-02 14:26:39 +0800572 ret := S1apExpectTable[i].vc_conn;
573 /* Release this entry */
Philipp Maier7147c922023-07-07 14:18:32 +0200574 S1apExpectTable[i].mme_id := omit;
575 S1apExpectTable[i].enb_id := omit;
Harald Welte35498112019-07-02 14:26:39 +0800576 S1apExpectTable[i].vc_conn := null;
577 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
578 return ret;
579 }
580 }
581 setverdict(fail, "Couldn't find Expect for ", msg);
582 mtc.stop;
583}
584
Philipp Maier7147c922023-07-07 14:18:32 +0200585private function f_create_expect(template (omit) MME_UE_S1AP_ID mme_id,
586 template (omit) ENB_UE_S1AP_ID enb_id,
587 S1AP_ConnHdlr hdlr)
Harald Welte35498112019-07-02 14:26:39 +0800588runs on S1AP_Emulation_CT {
589 var integer i;
590
591 /* Check an entry like this is not already presnt */
592 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200593 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
594 continue;
595 }
596 if (valueof(mme_id) == S1apExpectTable[i].mme_id and valueof(enb_id) == S1apExpectTable[i].enb_id) {
597 setverdict(fail, "UE MME id / UE ENB id pair already present", mme_id, enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800598 mtc.stop;
599 }
600 }
601 for (i := 0; i < sizeof(S1apExpectTable); i := i+1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200602 if (not ispresent(S1apExpectTable[i].mme_id) and not ispresent(S1apExpectTable[i].enb_id)) {
603 S1apExpectTable[i].mme_id := valueof(mme_id);
604 S1apExpectTable[i].enb_id := valueof(enb_id);
Harald Welte35498112019-07-02 14:26:39 +0800605 S1apExpectTable[i].vc_conn := hdlr;
Philipp Maier7147c922023-07-07 14:18:32 +0200606 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 +0800607 return;
608 }
609 }
610 testcase.stop("No space left in S1apExpectTable")
611}
612
613/* client/conn_hdlr side function to use procedure port to create expect in emulation */
Philipp Maier7147c922023-07-07 14:18:32 +0200614function f_create_s1ap_expect(template (omit) MME_UE_S1AP_ID mme_id,
615 template (omit) ENB_UE_S1AP_ID enb_id) runs on S1AP_ConnHdlr {
616 S1AP_PROC.call(S1APEM_register:{mme_id, enb_id, self}) {
617 [] S1AP_PROC.getreply(S1APEM_register:{?,?,?}) {};
Harald Welte35498112019-07-02 14:26:39 +0800618 }
619}
620
Philipp Maierfc7ada22023-07-24 11:32:12 +0200621private function f_create_expect_proc(integer procedureCode, S1AP_ConnHdlr hdlr) runs on S1AP_Emulation_CT {
622 var integer i;
623
624 /* Check an entry like this is not already presnt */
625 for (i := 0; i < sizeof(S1apExpectTableProc); i := i+1) {
626 if (S1apExpectTableProc[i].vc_conn == null) {
627 continue;
628 }
629 if (S1apExpectTableProc[i].procedureCode == procedureCode) {
630 setverdict(fail, "procedureCode ", procedureCode, " already present");
631 mtc.stop;
632 }
633 }
634 for (i := 0; i < sizeof(S1apExpectTableProc); i := i+1) {
635 if (S1apExpectTableProc[i].vc_conn == null) {
636 S1apExpectTableProc[i].procedureCode := procedureCode;
637 S1apExpectTableProc[i].vc_conn := hdlr;
638 log("Created Expect[", i, "] for PDU:", procedureCode, " to be handled at ", hdlr);
639 return;
640 }
641 }
642 testcase.stop("No space left in S1apExpectTableProc")
643}
644
645/* client/conn_hdlr side function to use procedure port to create expect (PDU) in emulation */
646function f_create_s1ap_expect_proc(integer procedureCode, S1AP_ConnHdlr hdlr) runs on S1AP_ConnHdlr
647{
648 S1AP_PROC.call(S1APEM_register_proc:{procedureCode, self}) {
649 [] S1AP_PROC.getreply(S1APEM_register_proc:{?,?}) {};
650 }
651
652 log(procedureCode);
653}
Harald Welte35498112019-07-02 14:26:39 +0800654
655private function f_expect_table_init()
656runs on S1AP_Emulation_CT {
657 var integer i;
658 for (i := 0; i < sizeof(S1apExpectTable); i := i + 1) {
Philipp Maier7147c922023-07-07 14:18:32 +0200659 S1apExpectTable[i].mme_id := omit;
660 S1apExpectTable[i].enb_id := omit;
661 S1apExpectTable[i].vc_conn := null;
Harald Welte35498112019-07-02 14:26:39 +0800662 }
Philipp Maierfc7ada22023-07-24 11:32:12 +0200663
664 for (i := 0; i < sizeof(S1apExpectTableProc); i := i + 1) {
665 S1apExpectTableProc[i].procedureCode := omit;
666 S1apExpectTableProc[i].vc_conn := null;
667 }
Harald Welte35498112019-07-02 14:26:39 +0800668}
669
670function DummyUnitdataCallback(S1AP_PDU msg)
671runs on S1AP_Emulation_CT return template S1AP_PDU {
672 log("Ignoring S1AP ", msg);
673 return omit;
674}
675
676
677function f_S1AP_get_ENB_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) ENB_UE_S1AP_ID
678{
679 if (ischosen(s1ap.initiatingMessage)) {
680 var InitiatingMessage im := s1ap.initiatingMessage;
681 select (s1ap) {
682 case (tr_S1AP_InitialUE) {
683 return im.value_.InitialUEMessage.protocolIEs[0].value_.ENB_UE_S1AP_ID;
684 }
685 case (tr_S1AP_DlNasTransport) {
686 return im.value_.DownlinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
687 }
688 case (tr_S1AP_UlNasTransport) {
689 return im.value_.UplinkNASTransport.protocolIEs[1].value_.ENB_UE_S1AP_ID;
690 }
691 case (tr_S1AP_IntialCtxSetupReq) {
692 return im.value_.initialContextSetupRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
693 }
694 case (tr_S1AP_UeContextReleaseReq) {
695 return im.value_.UEContextReleaseRequest.protocolIEs[1].value_.ENB_UE_S1AP_ID;
696 }
697 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
698 case (tr_S1AP_ConnEstInd) {
699 return im.value_.ConnectionEstablishmentIndication.protocolIEs[1].value_.ENB_UE_S1AP_ID;
700 }
701 /* TODO */
702 }
703 } else if (ischosen(s1ap.successfulOutcome)) {
704 var SuccessfulOutcome so := s1ap.successfulOutcome;
705 select (s1ap) {
706 case (tr_S1AP_InitialCtxSetupResp) {
707 return so.value_.initialContextSetupResponse.protocolIEs[1].value_.ENB_UE_S1AP_ID;
708 }
709 case (tr_S1AP_UeContextReleaseCompl) {
710 return so.value_.UEContextReleaseComplete.protocolIEs[1].value_.ENB_UE_S1AP_ID;
711 }
712 /* TODO */
713 }
714 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
715 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
716 select (s1ap) {
717 case (tr_S1AP_InitialCtxSetupFail) {
718 return uo.value_.initialContextSetupFailure.protocolIEs[1].value_.ENB_UE_S1AP_ID;
719 }
720 /* TODO */
721 }
722 }
723 return omit;
724}
725
726function f_S1AP_get_MME_UE_S1AP_ID(S1AP_PDU s1ap) return template (omit) MME_UE_S1AP_ID
727{
728 if (ischosen(s1ap.initiatingMessage)) {
729 var InitiatingMessage im := s1ap.initiatingMessage;
730 select (s1ap) {
731 case (tr_S1AP_DlNasTransport) {
732 return im.value_.DownlinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
733 }
734 case (tr_S1AP_UlNasTransport) {
735 return im.value_.UplinkNASTransport.protocolIEs[0].value_.MME_UE_S1AP_ID;
736 }
737 case (tr_S1AP_IntialCtxSetupReq) {
738 return im.value_.initialContextSetupRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
739 }
740 case (tr_S1AP_UeContextReleaseReq) {
741 return im.value_.UEContextReleaseRequest.protocolIEs[0].value_.MME_UE_S1AP_ID;
742 }
743 /* UeContextReleaseCmd needs special handling; it can contain any number of MME/UE IDs */
744 case (tr_S1AP_ConnEstInd) {
745 return im.value_.ConnectionEstablishmentIndication.protocolIEs[0].value_.MME_UE_S1AP_ID;
746 }
747 /* TODO */
748 }
749 } else if (ischosen(s1ap.successfulOutcome)) {
750 var SuccessfulOutcome so := s1ap.successfulOutcome;
751 select (s1ap) {
752 case (tr_S1AP_InitialCtxSetupResp) {
753 return so.value_.initialContextSetupResponse.protocolIEs[0].value_.MME_UE_S1AP_ID;
754 }
755 case (tr_S1AP_UeContextReleaseCompl) {
756 return so.value_.UEContextReleaseComplete.protocolIEs[0].value_.MME_UE_S1AP_ID;
757 }
758 /* TODO */
759 }
760 } else if (ischosen(s1ap.unsuccessfulOutcome)) {
761 var UnsuccessfulOutcome uo := s1ap.unsuccessfulOutcome;
762 select (s1ap) {
763 case (tr_S1AP_InitialCtxSetupFail) {
764 return uo.value_.initialContextSetupFailure.protocolIEs[0].value_.MME_UE_S1AP_ID;
765 }
766 /* TODO */
767 }
768 }
769 return omit;
770}
771
772function f_S1AP_get_NAS_PDU(S1AP_PDU s1ap) return template (omit) NAS_PDU
773{
774 var integer i;
775
776 if (ischosen(s1ap.initiatingMessage)) {
777 var InitiatingMessage im := s1ap.initiatingMessage;
778 select (s1ap) {
779 case (tr_S1AP_DlNasTransport) {
780 var DownlinkNASTransport msg := im.value_.DownlinkNASTransport;
781 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
782 if (msg.protocolIEs[i].id == id_NAS_PDU) {
783 return msg.protocolIEs[i].value_.NAS_PDU;
784 }
785 }
786 }
787 case (tr_S1AP_UlNasTransport) {
788 var UplinkNASTransport msg := im.value_.UplinkNASTransport;
789 for (i := 0; i < lengthof(msg.protocolIEs); i := i+1) {
790 if (msg.protocolIEs[i].id == id_NAS_PDU) {
791 return msg.protocolIEs[i].value_.NAS_PDU;
792 }
793 }
794 }
795 }
796 }
797 return omit;
798}
799
800
801
802}