blob: 0b2cb4ffd4c12c5247bb79d391b585216df486c4 [file] [log] [blame]
Harald Welte88b3ccb2020-03-12 21:36:32 +01001/* GTPv2 Emulation in TTCN-3
2 *
3 * (C) 2018-2020 Harald Welte <laforge@gnumonks.org>
4 * All rights reserved.
5 *
6 * Released under the terms of GNU General Public License, Version 2 or
7 * (at your option) any later version.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12module GTPv2_Emulation {
13
14import from IPL4asp_Types all;
15import from General_Types all;
16import from Osmocom_Types all;
17import from GTPv2_Types all;
18import from GTPv2_Templates all;
19import from GTPv2_CodecPort all;
20import from GTPv2_CodecPort_CtrlFunct all;
21
22import from UECUPS_Types all;
23import from UECUPS_CodecPort all;
24import from UECUPS_CodecPort_CtrlFunct all;
25
26/***********************************************************************
27 * Main Emulation Component
28 ***********************************************************************/
29
30modulepar {
31 charstring mp_uecups_host := "127.0.0.1";
32 integer mp_uecups_port := UECUPS_SCTP_PORT;
33};
34
35const integer GTP2C_PORT := 2123;
36const integer GTP1U_PORT := 2152;
37
38type record Gtp2EmulationCfg {
39 HostName gtpc_bind_ip,
40 IPL4asp_Types.PortNumber gtpc_bind_port,
41 HostName gtpc_remote_ip,
42 IPL4asp_Types.PortNumber gtpc_remote_port,
43 //HostName gtpu_bind_ip,
44 //PortNumber gtpu_bind_port,
45 boolean sgw_role,
46 boolean use_gtpu_daemon
47};
48
49type component GTPv2_Emulation_CT {
50 /* Communication with underlying GTP CodecPort */
51 port GTPv2C_PT GTP2C;
52
53 /* Control port to GTP-U Daemon */
54 port UECUPS_CODEC_PT UECUPS;
55
56 /* Communication with Clients */
57 port GTP2EM_PT TEID0;
58 port GTP2EM_PT CLIENT;
59 port GTP2EM_PROC_PT CLIENT_PROC;
60
61 /* Configuration by the user */
62 var Gtp2EmulationCfg g_gtp2_cfg;
63
64 /* State */
65 var GtpPeer g_peer;
66 var integer g_gtp2c_id;
67 var OCT1 g_restart_ctr;
68 var uint16_t g_c_seq_nr;
69 var TidTableRec TidTable[256];
70 var SeqTableRec SeqTable[256];
71 var ImsiTableRec ImsiTable[256];
Philipp Maierac791562023-09-01 17:10:24 +020072 var UdMsgTableRec UdMsgTable[256];
Harald Welte88b3ccb2020-03-12 21:36:32 +010073 var PidTableRec PidTable[256];
Philipp Maierac791562023-09-01 17:10:24 +020074
Harald Welte88b3ccb2020-03-12 21:36:32 +010075 var integer g_uecups_conn_id;
76};
77
78/* local TEID <-> ConnHdlr mapping */
79type record TidTableRec {
80 OCT4 teid,
81 GTP2_ConnHdlr vc_conn
82};
83
84/* local SeqNr <-> ConnHdlr mapping (until a response is received */
85type record SeqTableRec {
86 OCT3 seq,
87 GTP2_ConnHdlr vc_conn
88};
89
90/* IMSI <-> ConnHdlr mapping */
91type record ImsiTableRec {
92 hexstring imsi,
93 GTP2_ConnHdlr vc_conn
94};
95
Philipp Maierac791562023-09-01 17:10:24 +020096/* Unit data message type <-> ConnHdlr mapping */
97type record UdMsgTableRec {
98 OCT1 messageType,
99 GTP2_ConnHdlr vc_conn
100};
101
Harald Welte88b3ccb2020-03-12 21:36:32 +0100102/* pid <-> ConnHdlr mapping (for UECUPS process termination indication) */
103type record PidTableRec {
104 /* process ID of the running process */
105 integer pid,
106 /* component that started it */
107 GTP2_ConnHdlr vc_conn
108};
109
110private function f_comp_by_teid(OCT4 teid) runs on GTPv2_Emulation_CT return GTP2_ConnHdlr {
111 var integer i;
112 for (i := 0; i < sizeof(TidTable); i := i+1) {
113 if (isbound(TidTable[i].teid) and TidTable[i].teid == teid) {
114 return TidTable[i].vc_conn;
115 }
116 }
117 setverdict(fail, "No Component for TEID ", teid);
118 mtc.stop;
119}
120
121private function f_seq_known(OCT3 seq) runs on GTPv2_Emulation_CT return boolean {
122 var integer i;
123 for (i := 0; i < sizeof(SeqTable); i := i+1) {
124 if (isbound(SeqTable[i].seq) and SeqTable[i].seq == seq) {
125 return true;
126 }
127 }
128 return false;
129}
130
131private function f_comp_by_seq(OCT3 seq) runs on GTPv2_Emulation_CT return GTP2_ConnHdlr {
132 var integer i;
133 for (i := 0; i < sizeof(SeqTable); i := i+1) {
134 if (isbound(SeqTable[i].seq) and SeqTable[i].seq == seq) {
135 return SeqTable[i].vc_conn;
136 }
137 }
138 setverdict(fail, "No Component for SEQ ", seq);
139 mtc.stop;
140}
141
142private function f_comp_by_imsi(hexstring imsi) runs on GTPv2_Emulation_CT return GTP2_ConnHdlr {
143 var integer i;
144 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
145 if (isbound(ImsiTable[i].imsi) and ImsiTable[i].imsi == imsi) {
146 return ImsiTable[i].vc_conn;
147 }
148 }
149 setverdict(fail, "No Component for IMSI ", imsi);
150 mtc.stop;
151}
152
153private function f_comp_by_pid(integer pid) runs on GTPv2_Emulation_CT return GTP2_ConnHdlr {
154 var integer i;
155 for (i := 0; i < sizeof(PidTable); i := i+1) {
156 if (isbound(PidTable[i].pid) and PidTable[i].pid == pid) {
157 /* fixme: remove */
158 return PidTable[i].vc_conn;
159 }
160 }
161 setverdict(fail, "No Component for PID ", pid);
162 mtc.stop;
163}
164
165private function f_tid_tbl_add(OCT4 teid, GTP2_ConnHdlr vc_conn) runs on GTPv2_Emulation_CT {
166 var integer i;
167 for (i := 0; i < sizeof(TidTable); i := i+1) {
168 if (not isbound(TidTable[i].teid)) {
169 TidTable[i].teid := teid;
170 TidTable[i].vc_conn := vc_conn;
171 return;
172 }
173 }
174 testcase.stop("No Space in TidTable for ", teid);
175}
176
177private function f_seq_tbl_add(OCT3 seq, GTP2_ConnHdlr vc_conn) runs on GTPv2_Emulation_CT {
178 var integer i;
179 for (i := 0; i < sizeof(SeqTable); i := i+1) {
180 if (not isbound(SeqTable[i].seq)) {
181 SeqTable[i].seq := seq;
182 SeqTable[i].vc_conn := vc_conn;
183 return;
184 }
185 }
186 testcase.stop("No Space in SeqTable for ", seq);
187}
188
189private function f_seq_tbl_del(OCT3 seq) runs on GTPv2_Emulation_CT {
190 var integer i;
191 for (i := 0; i < sizeof(SeqTable); i := i+1) {
192 if (isbound(SeqTable[i].seq) and SeqTable[i].seq == seq) {
193 SeqTable[i] := {
194 seq := -,
195 vc_conn := null
196 }
197 }
198 }
199}
200
201private function f_imsi_tbl_add(hexstring imsi, GTP2_ConnHdlr vc_conn) runs on GTPv2_Emulation_CT {
202 var integer i;
203 for (i := 0; i < sizeof(ImsiTable); i := i+1) {
204 if (not isbound(ImsiTable[i].imsi)) {
205 ImsiTable[i].imsi := imsi;
206 ImsiTable[i].vc_conn := vc_conn;
207 return;
208 }
209 }
210 testcase.stop("No Space in IMSI Table for ", imsi);
211}
212
Philipp Maierac791562023-09-01 17:10:24 +0200213private function f_udmsg_tbl_add(OCT1 messageType, GTP2_ConnHdlr vc_conn) runs on GTPv2_Emulation_CT {
214 var integer i;
215 for (i := 0; i < sizeof(UdMsgTable); i := i+1) {
216 if (not isbound(UdMsgTable[i].messageType)) {
217 UdMsgTable[i].messageType := messageType;
218 UdMsgTable[i].vc_conn := vc_conn;
219 return;
220 }
221 }
222 testcase.stop("No Space in UdMsg Table for messateType ", messageType);
223}
224
Harald Welte88b3ccb2020-03-12 21:36:32 +0100225private function f_pid_tbl_add(integer pid, GTP2_ConnHdlr vc_conn) runs on GTPv2_Emulation_CT {
226 var integer i;
227 for (i := 0; i < sizeof(PidTable); i := i+1) {
228 if (not isbound(PidTable[i].pid)) {
229 PidTable[i].pid := pid;
230 PidTable[i].vc_conn := vc_conn;
231 return;
232 }
233 }
234 testcase.stop("No Space in PID Table for ", pid);
235}
236
237
238/* allocate an unused local teid */
239private function f_alloc_teid() runs on GTPv2_Emulation_CT return OCT4 {
240 var OCT4 teid;
241 var integer i, j;
242 for (i := 0; i < 100; i := i+1) {
243 teid := f_rnd_octstring(4);
244 for (j := 0; j < sizeof(TidTable); j := j+1) {
245 if (isbound(TidTable) and TidTable[i].teid == teid) {
246 continue;
247 }
248 }
249 /* we iterated over all entries and found no match: great! */
250 return teid;
251 }
252 testcase.stop("Cannot find unused TEID after ", i, " attempts");
253}
254
255/* obtain the IMSI from a GTPv2C PDU, if there is any IMSI contained. The way how the TITAN
256 * GTPv2 decoders are structured (explict IE members rather than a list/set of generic IE structures)
257 * doesn't make this easy, but requires lots of boilerplate code. Oh well.. */
258function f_gtp2c_extract_imsi(PDU_GTPCv2 gtp) return template (omit) hexstring {
259 if (ischosen(gtp.gtpcv2_pdu.createSessionRequest)) {
260 if (ispresent(gtp.gtpcv2_pdu.createSessionRequest.iMSI)) {
261 return gtp.gtpcv2_pdu.createSessionRequest.iMSI.iMSI_Value;
262 }
263 } else if (ischosen(gtp.gtpcv2_pdu.downlinkDataNotification)) {
264 if (ispresent(gtp.gtpcv2_pdu.downlinkDataNotification.iMSI)) {
265 return gtp.gtpcv2_pdu.downlinkDataNotification.iMSI.iMSI_Value;
266 }
267 } else if (ischosen(gtp.gtpcv2_pdu.downlinkDataNotificationAcknowledgement)) {
268 if (ispresent(gtp.gtpcv2_pdu.downlinkDataNotificationAcknowledgement.iMSI)) {
269 return gtp.gtpcv2_pdu.downlinkDataNotificationAcknowledgement.iMSI.iMSI_Value;
270 }
271 } else if (ischosen(gtp.gtpcv2_pdu.downlinkDataNotificationFailureIndication)) {
272 if (ispresent(gtp.gtpcv2_pdu.downlinkDataNotificationFailureIndication.iMSI)) {
273 return gtp.gtpcv2_pdu.downlinkDataNotificationFailureIndication.iMSI.iMSI_Value;
274 }
275 } else if (ischosen(gtp.gtpcv2_pdu.createIndirectDataForwardingTunnelRequest)) {
276 if (ispresent(gtp.gtpcv2_pdu.createIndirectDataForwardingTunnelRequest.iMSI)) {
277 return gtp.gtpcv2_pdu.createIndirectDataForwardingTunnelRequest.iMSI.iMSI_Value;
278 }
279 } else if (ischosen(gtp.gtpcv2_pdu.stopPagingIndication)) {
280 if (ispresent(gtp.gtpcv2_pdu.stopPagingIndication.iMSI)) {
281 return gtp.gtpcv2_pdu.stopPagingIndication.iMSI.iMSI_Value;
282 }
283 } else if (ischosen(gtp.gtpcv2_pdu.forwardRelocationRequest)) {
284 if (ispresent(gtp.gtpcv2_pdu.forwardRelocationRequest.iMSI)) {
285 return gtp.gtpcv2_pdu.forwardRelocationRequest.iMSI.iMSI_Value;
286 }
287 } else if (ischosen(gtp.gtpcv2_pdu.contextRequest)) {
288 if (ispresent(gtp.gtpcv2_pdu.contextRequest.iMSI)) {
289 return gtp.gtpcv2_pdu.contextRequest.iMSI.iMSI_Value;
290 }
291 } else if (ischosen(gtp.gtpcv2_pdu.identificationResponse)) {
292 if (ispresent(gtp.gtpcv2_pdu.identificationResponse.iMSI)) {
293 return gtp.gtpcv2_pdu.identificationResponse.iMSI.iMSI_Value;
294 }
295 } else if (ischosen(gtp.gtpcv2_pdu.changeNotificationRequest)) {
296 if (ispresent(gtp.gtpcv2_pdu.changeNotificationRequest)) {
297 return gtp.gtpcv2_pdu.changeNotificationRequest.iMSI.iMSI_Value;
298 }
299 } else if (ischosen(gtp.gtpcv2_pdu.changeNotificationResponse)) {
300 if (ispresent(gtp.gtpcv2_pdu.changeNotificationResponse.iMSI)) {
301 return gtp.gtpcv2_pdu.changeNotificationResponse.iMSI.iMSI_Value;
302 }
303 } else if (ischosen(gtp.gtpcv2_pdu.relocationCancelRequest)) {
304 if (ispresent(gtp.gtpcv2_pdu.relocationCancelRequest.iMSI)) {
305 return gtp.gtpcv2_pdu.relocationCancelRequest.iMSI.iMSI_Value;
306 }
307 } else if (ischosen(gtp.gtpcv2_pdu.uE_RegistrationQueryRequest)) {
308 if (ispresent(gtp.gtpcv2_pdu.uE_RegistrationQueryRequest.iMSI)) {
309 return gtp.gtpcv2_pdu.uE_RegistrationQueryRequest.iMSI.iMSI_Value;
310 }
311 } else if (ischosen(gtp.gtpcv2_pdu.uE_RegistrationQueryResponse)) {
312 if (ispresent(gtp.gtpcv2_pdu.uE_RegistrationQueryResponse.iMSI)) {
313 return gtp.gtpcv2_pdu.uE_RegistrationQueryResponse.iMSI.iMSI_Value;
314 }
315 } else if (ischosen(gtp.gtpcv2_pdu.suspendNotification)) {
316 if (ispresent(gtp.gtpcv2_pdu.suspendNotification.iMSI)) {
317 return gtp.gtpcv2_pdu.suspendNotification.iMSI.iMSI_Value;
318 }
319 } else if (ischosen(gtp.gtpcv2_pdu.resumeNotification)) {
320 if (ispresent(gtp.gtpcv2_pdu.resumeNotification.iMSI)) {
321 return gtp.gtpcv2_pdu.resumeNotification.iMSI.iMSI_Value;
322 }
323 } else if (ischosen(gtp.gtpcv2_pdu.cSPagingIndication)) {
324 if (ispresent(gtp.gtpcv2_pdu.cSPagingIndication.iMSI)) {
325 return gtp.gtpcv2_pdu.cSPagingIndication.iMSI.iMSI_Value;
326 }
327 } else if (ischosen(gtp.gtpcv2_pdu.pGW_DownlinkTriggeringNotification)) {
328 if (ispresent(gtp.gtpcv2_pdu.pGW_DownlinkTriggeringNotification.iMSI)) {
329 return gtp.gtpcv2_pdu.pGW_DownlinkTriggeringNotification.iMSI.iMSI_Value;
330 }
331 } else if (ischosen(gtp.gtpcv2_pdu.pGW_DownlinkTriggeringAcknowledge)) {
332 if (ispresent(gtp.gtpcv2_pdu.pGW_DownlinkTriggeringAcknowledge.iMSI)) {
333 return gtp.gtpcv2_pdu.pGW_DownlinkTriggeringAcknowledge.iMSI.iMSI_Value;
334 }
335 } else if (ischosen(gtp.gtpcv2_pdu.traceSessionActivation)) {
336 if (ispresent(gtp.gtpcv2_pdu.traceSessionActivation.iMSI)) {
337 return gtp.gtpcv2_pdu.traceSessionActivation.iMSI.iMSI_Value;
338 }
339 }
340 return omit;
341}
342
Pau Espin Pedrold7ae2c42023-12-13 19:11:13 +0100343private function f_gtp2c_is_initial_msg(PDU_GTPCv2 msg) return boolean
344{
345 if (ischosen(msg.gtpcv2_pdu.echoRequest) or
346 ischosen(msg.gtpcv2_pdu.versionNotSupported) or
347 ischosen(msg.gtpcv2_pdu.createSessionRequest) or
348 ischosen(msg.gtpcv2_pdu.createBearerRequest) or
349 ischosen(msg.gtpcv2_pdu.bearerResourceCommand) or
350 ischosen(msg.gtpcv2_pdu.bearerResourceFailureIndication) or
351 ischosen(msg.gtpcv2_pdu.modifyBearerRequest) or
352 ischosen(msg.gtpcv2_pdu.deleteSessionRequest) or
353 ischosen(msg.gtpcv2_pdu.deleteBearerRequest) or
354 ischosen(msg.gtpcv2_pdu.downlinkDataNotification) or
355 ischosen(msg.gtpcv2_pdu.downlinkDataNotificationAcknowledgement) or
356 ischosen(msg.gtpcv2_pdu.downlinkDataNotificationFailureIndication) or
357 ischosen(msg.gtpcv2_pdu.deleteIndirectDataForwardingTunnelRequest) or
358 ischosen(msg.gtpcv2_pdu.modifyBearerCommand) or
359 ischosen(msg.gtpcv2_pdu.modifyBearerFailureIndication) or
360 ischosen(msg.gtpcv2_pdu.updateBearerRequest) or
361 ischosen(msg.gtpcv2_pdu.deleteBearerCommand) or
362 ischosen(msg.gtpcv2_pdu.createIndirectDataForwardingTunnelRequest) or
363 ischosen(msg.gtpcv2_pdu.releaseAccessBearersRequest) or
364 ischosen(msg.gtpcv2_pdu.stopPagingIndication) or
365 ischosen(msg.gtpcv2_pdu.modifyAccessBearersRequest) or
366 ischosen(msg.gtpcv2_pdu.remoteUEReportNotification) or
367 ischosen(msg.gtpcv2_pdu.remoteUEReportAcknowledge) or
368 ischosen(msg.gtpcv2_pdu.forwardRelocationRequest) or
369 ischosen(msg.gtpcv2_pdu.forwardRelocationCompleteNotification) or
370 ischosen(msg.gtpcv2_pdu.forwardRelocationCompleteAcknowledge) or
371 ischosen(msg.gtpcv2_pdu.contextRequest) or
372 ischosen(msg.gtpcv2_pdu.contextAcknowledge) or
373 ischosen(msg.gtpcv2_pdu.identificationRequest) or
374 ischosen(msg.gtpcv2_pdu.forwardAccessContextNotification) or
375 ischosen(msg.gtpcv2_pdu.forwardAccessContextAcknowledge) or
376 ischosen(msg.gtpcv2_pdu.detachNotification) or
377 ischosen(msg.gtpcv2_pdu.detachAcknowledge) or
378 ischosen(msg.gtpcv2_pdu.changeNotificationRequest) or
379 ischosen(msg.gtpcv2_pdu.relocationCancelRequest) or
380 ischosen(msg.gtpcv2_pdu.configurationTransferTunnel) or
381 ischosen(msg.gtpcv2_pdu.rAN_InformationRelay) or
382 ischosen(msg.gtpcv2_pdu.suspendNotification) or
383 ischosen(msg.gtpcv2_pdu.suspendAcknowledge) or
384 ischosen(msg.gtpcv2_pdu.resumeNotification) or
385 ischosen(msg.gtpcv2_pdu.resumeAcknowledge) or
386 ischosen(msg.gtpcv2_pdu.cSPagingIndication) or
387 ischosen(msg.gtpcv2_pdu.createForwardingTunnelRequest) or
388 ischosen(msg.gtpcv2_pdu.deletePDN_ConnectionSetRequest) or
389 ischosen(msg.gtpcv2_pdu.traceSessionActivation) or
390 ischosen(msg.gtpcv2_pdu.traceSessionDeactivation) or
391 ischosen(msg.gtpcv2_pdu.updatePDN_ConnectionSetRequest) or
392 ischosen(msg.gtpcv2_pdu.pGW_RestartNotification) or
393 ischosen(msg.gtpcv2_pdu.pGW_RestartNotificationAcknowledge) or
394 ischosen(msg.gtpcv2_pdu.pGW_DownlinkTriggeringNotification) or
395 ischosen(msg.gtpcv2_pdu.pGW_DownlinkTriggeringAcknowledge) or
396 ischosen(msg.gtpcv2_pdu.alertMMENotification) or
397 ischosen(msg.gtpcv2_pdu.alertMMEAcknowledge) or
398 ischosen(msg.gtpcv2_pdu.uEActivityNotification) or
399 ischosen(msg.gtpcv2_pdu.uEActivityAcknowledge) or
400 ischosen(msg.gtpcv2_pdu.mBMSSessionStartRequest) or
401 ischosen(msg.gtpcv2_pdu.mBMSSessionUpdateRequest) or
402 ischosen(msg.gtpcv2_pdu.mBMSSessionStopRequest) or
403 ischosen(msg.gtpcv2_pdu.iSR_StatusIndication) or
404 ischosen(msg.gtpcv2_pdu.uE_RegistrationQueryRequest)) {
405 return true;
406 }
407 return false;
408}
409
Harald Welte88b3ccb2020-03-12 21:36:32 +0100410private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := {
411 sinfo_stream := omit,
412 sinfo_ppid := ppid,
413 remSocks := omit,
414 assocId := omit
415};
416
417function tr_UECUPS_RecvFrom_R(template PDU_UECUPS msg)
418runs on GTPv2_Emulation_CT return template UECUPS_RecvFrom {
419 var template UECUPS_RecvFrom mrf := {
420 connId := g_uecups_conn_id,
421 remName := ?,
422 remPort := ?,
423 locName := ?,
424 locPort := ?,
425 msg := msg
426 }
427 return mrf;
428}
429
430
431private template PortEvent tr_SctpAssocChange := {
432 sctpEvent := {
433 sctpAssocChange := ?
434 }
435}
436private template PortEvent tr_SctpPeerAddrChange := {
437 sctpEvent := {
438 sctpPeerAddrChange := ?
439 }
440}
441
442private function f_uecups_xceive(template (value) PDU_UECUPS tx,
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200443 template PDU_UECUPS rx_t := ?, float time_out := 10.0)
Harald Welte88b3ccb2020-03-12 21:36:32 +0100444runs on GTPv2_Emulation_CT return PDU_UECUPS {
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200445 timer T := time_out;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100446 var UECUPS_RecvFrom mrf;
447
448 UECUPS.send(t_UECUPS_Send(g_uecups_conn_id, tx));
Vadim Yanitskiy5313af92022-01-20 18:53:15 +0600449 T.start;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100450 alt {
451 [] UECUPS.receive(tr_UECUPS_RecvFrom_R(rx_t)) -> value mrf { }
452 [] UECUPS.receive(tr_SctpAssocChange) { repeat; }
453 [] UECUPS.receive(tr_SctpPeerAddrChange) { repeat; }
454 [] T.timeout {
455 setverdict(fail, "Timeout waiting for ", rx_t);
456 mtc.stop;
457 }
458 }
459 return mrf.msg;
460}
461
462private function f_init(Gtp2EmulationCfg cfg) runs on GTPv2_Emulation_CT {
463 var Result res;
464
465 map(self:GTP2C, system:GTP2C);
466 res := GTPv2_CodecPort_CtrlFunct.f_IPL4_listen(GTP2C, cfg.gtpc_bind_ip,
467 cfg.gtpc_bind_port, {udp:={}});
468 g_gtp2c_id := res.connId;
469
470 g_restart_ctr := f_rnd_octstring(1);
471 g_c_seq_nr := f_rnd_int(65535);
472 g_gtp2_cfg := cfg;
473 g_peer := {
474 connId := g_gtp2c_id,
475 remName := g_gtp2_cfg.gtpc_remote_ip,
476 remPort := g_gtp2_cfg.gtpc_remote_port
477 }
478
Philipp Maierb11ee502023-08-31 16:48:19 +0200479 g_uecups_conn_id := res.connId;
480
Harald Welte88b3ccb2020-03-12 21:36:32 +0100481 if (g_gtp2_cfg.use_gtpu_daemon) {
482 map(self:UECUPS, system:UECUPS);
483 res := UECUPS_CodecPort_CtrlFunct.f_IPL4_connect(UECUPS, mp_uecups_host, mp_uecups_port, "", -1, -1, { sctp := valueof(ts_SCTP) });
484 if (not ispresent(res.connId)) {
485 setverdict(fail, "Could not connect UECUPS socket, check your configuration");
486 testcase.stop;
487 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100488
489 /* clear all tunnel state in the daemon at start */
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200490 f_uecups_xceive({reset_all_state := {}}, {reset_all_state_res:=?}, 30.0);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100491 }
492
493 /* make sure we always pass incoming UECUPS indications whenever receiving fom the UECUPS port */
494 activate(as_uecups_ind());
495}
496
497private altstep as_uecups_ind() runs on GTPv2_Emulation_CT {
498var UECUPS_RecvFrom rx;
499var GTP2_ConnHdlr vc_conn;
500/* handle incoming program_term_ind; dispatch to whatever component started the process */
501[] UECUPS.receive(tr_UECUPS_RecvFrom_R({program_term_ind:=?})) -> value rx {
502 vc_conn := f_comp_by_pid(rx.msg.program_term_ind.pid);
503 CLIENT.send(rx.msg.program_term_ind) to vc_conn;
504 /* FIXME: remove from table */
505 repeat;
506 }
507}
508
Philipp Maierac791562023-09-01 17:10:24 +0200509private function SendToUdMsgTable(Gtp2cUnitdata g2c_ud) runs on GTPv2_Emulation_CT {
510 var GTP2_ConnHdlr vc_conn;
511
512 for (var integer i := 0; i < sizeof(UdMsgTable); i := i + 1) {
513 if (isbound(UdMsgTable[i].messageType)) {
514 if (UdMsgTable[i].messageType == g2c_ud.gtpc.messageType) {
515 vc_conn := UdMsgTable[i].vc_conn;
516 CLIENT.send(g2c_ud.gtpc) to vc_conn;
517 }
518 }
519 }
520
521 return;
522}
523
Harald Welte88b3ccb2020-03-12 21:36:32 +0100524function main(Gtp2EmulationCfg cfg) runs on GTPv2_Emulation_CT {
525 var Gtp2cUnitdata g2c_ud;
526 var PDU_GTPCv2 g2c;
527 var GTP2_ConnHdlr vc_conn;
528 var hexstring imsi;
Philipp Maierac791562023-09-01 17:10:24 +0200529 var OCT1 messageType;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100530 var OCT4 teid;
531 var PDU_UECUPS rx_uecups;
532 var UECUPS_CreateTun gtc;
533 var UECUPS_DestroyTun gtd;
534 var UECUPS_StartProgram sprog;
535
536 f_init(cfg);
537
538 while (true) {
539 alt {
540 /* route inbound GTP2-C based on TEID, SEQ or IMSI */
541 [] GTP2C.receive(Gtp2cUnitdata:?) -> value g2c_ud {
542 var template hexstring imsi_t := f_gtp2c_extract_imsi(g2c_ud.gtpc);
543 if (not ispresent(g2c_ud.gtpc.tEID) or g2c_ud.gtpc.tEID == int2oct(0, 4)) {
544 /* if this is a response, route by SEQ */
545 if (match(g2c_ud.gtpc, tr_PDU_GTP2C_msgtypes(gtp2_responses))
546 and f_seq_known(g2c_ud.gtpc.sequenceNumber)) {
547 vc_conn := f_comp_by_seq(g2c_ud.gtpc.sequenceNumber);
548 CLIENT.send(g2c_ud.gtpc) to vc_conn;
549 } else {
Philipp Maierac791562023-09-01 17:10:24 +0200550 SendToUdMsgTable(g2c_ud);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100551 TEID0.send(g2c_ud.gtpc);
552 }
553 } else if (ispresent(g2c_ud.gtpc.tEID) and g2c_ud.gtpc.tEID != int2oct(0, 4)) {
554 vc_conn := f_comp_by_teid(g2c_ud.gtpc.tEID);
555 CLIENT.send(g2c_ud.gtpc) to vc_conn;
556 } else if (isvalue(imsi_t)) {
557 vc_conn := f_comp_by_imsi(valueof(imsi_t));
558 CLIENT.send(g2c_ud.gtpc) to vc_conn;
559 } else {
560 /* Send to all clients */
561 var integer i;
562 for (i := 0; i < sizeof(TidTable); i := i+1) {
563 if (isbound(TidTable[i].teid) and TidTable[i].teid == teid) {
564 CLIENT.send(g2c_ud.gtpc) to TidTable[i].vc_conn;
565 }
566 }
567 }
568
569 /* remove sequence number if response was received */
570 if (match(g2c_ud.gtpc, tr_PDU_GTP2C_msgtypes(gtp2_responses))) {
571 f_seq_tbl_del(g2c_ud.gtpc.sequenceNumber);
572 }
573
574 }
575
576 [] TEID0.receive(PDU_GTPCv2:?) -> value g2c sender vc_conn {
Pau Espin Pedrold7ae2c42023-12-13 19:11:13 +0100577 /* patch in the next sequence number on outbound Initial message */
578 if (f_gtp2c_is_initial_msg(g2c)) {
579 g2c.sequenceNumber := int2oct(g_c_seq_nr, 3);
580 g_c_seq_nr := g_c_seq_nr + 1;
581 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100582 /* build Gtp2cUnitdata */
583 g2c_ud := { peer := g_peer, gtpc := g2c };
584 GTP2C.send(g2c_ud);
585 if (match(g2c, tr_PDU_GTP2C_msgtypes(gtp2_requests))) {
586 f_seq_tbl_add(g2c.sequenceNumber, vc_conn);
587 }
588 }
589
590 [] CLIENT.receive(PDU_GTPCv2:?) -> value g2c sender vc_conn {
Pau Espin Pedrold7ae2c42023-12-13 19:11:13 +0100591 /* patch in the next sequence number on outbound Initial message */
592 if (f_gtp2c_is_initial_msg(g2c)) {
593 g2c.sequenceNumber := int2oct(g_c_seq_nr, 3);
594 g_c_seq_nr := g_c_seq_nr + 1;
595 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100596 /* build Gtp2cUnitdata */
597 g2c_ud := { peer := g_peer, gtpc := g2c };
598 GTP2C.send(g2c_ud);
599 if (match(g2c, tr_PDU_GTP2C_msgtypes(gtp2_requests))) {
600 f_seq_tbl_add(g2c.sequenceNumber, vc_conn);
601 }
602 }
603
604 [] CLIENT_PROC.getcall(GTP2EM_register_imsi:{?}) -> param(imsi) sender vc_conn {
605 f_imsi_tbl_add(imsi, vc_conn);
606 CLIENT_PROC.reply(GTP2EM_register_imsi:{imsi}) to vc_conn;
607 }
Philipp Maierac791562023-09-01 17:10:24 +0200608 [] CLIENT_PROC.getcall(GTP2EM_register_udmsg:{?}) -> param(messageType) sender vc_conn {
609 f_udmsg_tbl_add(messageType, vc_conn);
610 CLIENT_PROC.reply(GTP2EM_register_udmsg:{messageType}) to vc_conn;
611 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100612
613 [] CLIENT_PROC.getcall(GTP2EM_register_teid:{?}) -> param(teid) sender vc_conn {
614 f_tid_tbl_add(teid, vc_conn);
615 CLIENT_PROC.reply(GTP2EM_register_teid:{teid}) to vc_conn;
616 }
617 [] CLIENT_PROC.getcall(GTP2EM_allocate_teid:{}) -> sender vc_conn {
618 var OCT4 t := f_alloc_teid();
619 f_tid_tbl_add(t, vc_conn);
620 CLIENT_PROC.reply(GTP2EM_allocate_teid:{} value t) to vc_conn;
621 }
622 [] CLIENT_PROC.getcall(GTP2EM_create_tunnel:{?}) -> param(gtc) sender vc_conn {
623 rx_uecups := f_uecups_xceive({create_tun := gtc}, {create_tun_res:={result:=OK}});
624 CLIENT_PROC.reply(GTP2EM_create_tunnel:{gtc}) to vc_conn;
625 }
626 [] CLIENT_PROC.getcall(GTP2EM_destroy_tunnel:{?}) -> param(gtd) sender vc_conn {
627 rx_uecups := f_uecups_xceive({destroy_tun := gtd}, {destroy_tun_res:={result:=OK}});
628 CLIENT_PROC.reply(GTP2EM_destroy_tunnel:{gtd}) to vc_conn;
629 }
630 [] CLIENT_PROC.getcall(GTP2EM_start_program:{?}) -> param(sprog) sender vc_conn {
631 rx_uecups := f_uecups_xceive({start_program := sprog}, {start_program_res:=?});
632 /* if successful: store (pid, vc_conn) tuple so we can route program_term_ind */
633 if (rx_uecups.start_program_res.result == OK) {
634 f_pid_tbl_add(rx_uecups.start_program_res.pid, vc_conn);
635 }
636 CLIENT_PROC.reply(GTP2EM_start_program:{sprog} value rx_uecups.start_program_res) to vc_conn;
637 }
638
639 }
640 }
641}
642
643
644/***********************************************************************
645 * Interaction between Main and Client Components
646 ***********************************************************************/
647type port GTP2EM_PT message {
648 inout PDU_GTPCv2, UECUPS_ProgramTermInd;
649} with { extension "internal" };
650
651signature GTP2EM_register_imsi(hexstring imsi);
Philipp Maierac791562023-09-01 17:10:24 +0200652signature GTP2EM_register_udmsg(OCT1 messageType);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100653signature GTP2EM_register_teid(OCT4 teid);
654signature GTP2EM_allocate_teid() return OCT4;
655signature GTP2EM_create_tunnel(UECUPS_CreateTun gtc);
656signature GTP2EM_destroy_tunnel(UECUPS_DestroyTun gtd);
657signature GTP2EM_start_program(UECUPS_StartProgram sprog) return UECUPS_StartProgramRes;
658
659type port GTP2EM_PROC_PT procedure {
Philipp Maierac791562023-09-01 17:10:24 +0200660 inout GTP2EM_register_imsi, GTP2EM_register_udmsg, GTP2EM_register_teid, GTP2EM_allocate_teid,
Harald Welte88b3ccb2020-03-12 21:36:32 +0100661 GTP2EM_create_tunnel, GTP2EM_destroy_tunnel, GTP2EM_start_program;
662} with { extension "internal" };
663
664/***********************************************************************
665 * Client Component
666 ***********************************************************************/
667
668type component GTP2_ConnHdlr {
669 port GTP2EM_PT GTP2;
670 port GTP2EM_PROC_PT GTP2_PROC;
671};
672
673function f_gtp2_register_imsi(hexstring imsi) runs on GTP2_ConnHdlr {
Pau Espin Pedrol5c18a0c2023-10-19 13:41:41 +0200674 /* 15-digit IMSIs are len(imsi)=15, but decoded messages are
675 * octet-aligned, hence the hexstring in messages is len(imsi)=16, where
676 * the last hex char is a padding 'F'H */
677 imsi := f_pad_bcd_number(imsi);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100678 GTP2_PROC.call(GTP2EM_register_imsi:{imsi}) {
679 [] GTP2_PROC.getreply(GTP2EM_register_imsi:{imsi});
680 }
681}
682
Philipp Maierac791562023-09-01 17:10:24 +0200683function f_gtp2_register_udmsg(OCT1 messageType) runs on GTP2_ConnHdlr {
684 GTP2_PROC.call(GTP2EM_register_udmsg:{messageType}) {
685 [] GTP2_PROC.getreply(GTP2EM_register_udmsg:{messageType});
686 }
687}
688
Harald Welte88b3ccb2020-03-12 21:36:32 +0100689function f_gtp2_register_teid(OCT4 teid) runs on GTP2_ConnHdlr {
690 GTP2_PROC.call(GTP2EM_register_teid:{teid}) {
691 [] GTP2_PROC.getreply(GTP2EM_register_teid:{teid});
692 }
693}
694
695function f_gtp2_allocate_teid() runs on GTP2_ConnHdlr return OCT4 {
696 var OCT4 t;
697 GTP2_PROC.call(GTP2EM_allocate_teid:{}) {
698 [] GTP2_PROC.getreply(GTP2EM_allocate_teid:{}) -> value t {
699 return t;
700 }
701 }
702}
703
704function f_gtp2_create_tunnel(template (value) UECUPS_CreateTun gtc)
705runs on GTP2_ConnHdlr {
706 GTP2_PROC.call(GTP2EM_create_tunnel:{valueof(gtc)}) {
707 [] GTP2_PROC.getreply(GTP2EM_create_tunnel:{gtc});
708 }
709}
710
711function f_gtp2_destroy_tunnel(template (value) UECUPS_DestroyTun gtd)
712runs on GTP2_ConnHdlr {
713 GTP2_PROC.call(GTP2EM_destroy_tunnel:{valueof(gtd)}) {
714 [] GTP2_PROC.getreply(GTP2EM_destroy_tunnel:{gtd});
715 }
716}
717
718function f_gtp2_start_program(template (value) UECUPS_StartProgram sprog)
719runs on GTP2_ConnHdlr return UECUPS_StartProgramRes {
720 var UECUPS_StartProgramRes res;
721 GTP2_PROC.call(GTP2EM_start_program:{valueof(sprog)}) {
722 [] GTP2_PROC.getreply(GTP2EM_start_program:{sprog}) -> value res;
723 }
724 return res;
725}
726
727
728
729}