blob: 736dd0303912a0ef107b6dbcccc86c3a7274d10a [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
343private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := {
344 sinfo_stream := omit,
345 sinfo_ppid := ppid,
346 remSocks := omit,
347 assocId := omit
348};
349
350function tr_UECUPS_RecvFrom_R(template PDU_UECUPS msg)
351runs on GTPv2_Emulation_CT return template UECUPS_RecvFrom {
352 var template UECUPS_RecvFrom mrf := {
353 connId := g_uecups_conn_id,
354 remName := ?,
355 remPort := ?,
356 locName := ?,
357 locPort := ?,
358 msg := msg
359 }
360 return mrf;
361}
362
363
364private template PortEvent tr_SctpAssocChange := {
365 sctpEvent := {
366 sctpAssocChange := ?
367 }
368}
369private template PortEvent tr_SctpPeerAddrChange := {
370 sctpEvent := {
371 sctpPeerAddrChange := ?
372 }
373}
374
375private function f_uecups_xceive(template (value) PDU_UECUPS tx,
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200376 template PDU_UECUPS rx_t := ?, float time_out := 10.0)
Harald Welte88b3ccb2020-03-12 21:36:32 +0100377runs on GTPv2_Emulation_CT return PDU_UECUPS {
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200378 timer T := time_out;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100379 var UECUPS_RecvFrom mrf;
380
381 UECUPS.send(t_UECUPS_Send(g_uecups_conn_id, tx));
Vadim Yanitskiy5313af92022-01-20 18:53:15 +0600382 T.start;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100383 alt {
384 [] UECUPS.receive(tr_UECUPS_RecvFrom_R(rx_t)) -> value mrf { }
385 [] UECUPS.receive(tr_SctpAssocChange) { repeat; }
386 [] UECUPS.receive(tr_SctpPeerAddrChange) { repeat; }
387 [] T.timeout {
388 setverdict(fail, "Timeout waiting for ", rx_t);
389 mtc.stop;
390 }
391 }
392 return mrf.msg;
393}
394
395private function f_init(Gtp2EmulationCfg cfg) runs on GTPv2_Emulation_CT {
396 var Result res;
397
398 map(self:GTP2C, system:GTP2C);
399 res := GTPv2_CodecPort_CtrlFunct.f_IPL4_listen(GTP2C, cfg.gtpc_bind_ip,
400 cfg.gtpc_bind_port, {udp:={}});
401 g_gtp2c_id := res.connId;
402
403 g_restart_ctr := f_rnd_octstring(1);
404 g_c_seq_nr := f_rnd_int(65535);
405 g_gtp2_cfg := cfg;
406 g_peer := {
407 connId := g_gtp2c_id,
408 remName := g_gtp2_cfg.gtpc_remote_ip,
409 remPort := g_gtp2_cfg.gtpc_remote_port
410 }
411
Philipp Maierb11ee502023-08-31 16:48:19 +0200412 g_uecups_conn_id := res.connId;
413
Harald Welte88b3ccb2020-03-12 21:36:32 +0100414 if (g_gtp2_cfg.use_gtpu_daemon) {
415 map(self:UECUPS, system:UECUPS);
416 res := UECUPS_CodecPort_CtrlFunct.f_IPL4_connect(UECUPS, mp_uecups_host, mp_uecups_port, "", -1, -1, { sctp := valueof(ts_SCTP) });
417 if (not ispresent(res.connId)) {
418 setverdict(fail, "Could not connect UECUPS socket, check your configuration");
419 testcase.stop;
420 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100421
422 /* clear all tunnel state in the daemon at start */
Pau Espin Pedrol12c4aa82022-04-11 19:55:55 +0200423 f_uecups_xceive({reset_all_state := {}}, {reset_all_state_res:=?}, 30.0);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100424 }
425
426 /* make sure we always pass incoming UECUPS indications whenever receiving fom the UECUPS port */
427 activate(as_uecups_ind());
428}
429
430private altstep as_uecups_ind() runs on GTPv2_Emulation_CT {
431var UECUPS_RecvFrom rx;
432var GTP2_ConnHdlr vc_conn;
433/* handle incoming program_term_ind; dispatch to whatever component started the process */
434[] UECUPS.receive(tr_UECUPS_RecvFrom_R({program_term_ind:=?})) -> value rx {
435 vc_conn := f_comp_by_pid(rx.msg.program_term_ind.pid);
436 CLIENT.send(rx.msg.program_term_ind) to vc_conn;
437 /* FIXME: remove from table */
438 repeat;
439 }
440}
441
Philipp Maierac791562023-09-01 17:10:24 +0200442private function SendToUdMsgTable(Gtp2cUnitdata g2c_ud) runs on GTPv2_Emulation_CT {
443 var GTP2_ConnHdlr vc_conn;
444
445 for (var integer i := 0; i < sizeof(UdMsgTable); i := i + 1) {
446 if (isbound(UdMsgTable[i].messageType)) {
447 if (UdMsgTable[i].messageType == g2c_ud.gtpc.messageType) {
448 vc_conn := UdMsgTable[i].vc_conn;
449 CLIENT.send(g2c_ud.gtpc) to vc_conn;
450 }
451 }
452 }
453
454 return;
455}
456
Harald Welte88b3ccb2020-03-12 21:36:32 +0100457function main(Gtp2EmulationCfg cfg) runs on GTPv2_Emulation_CT {
458 var Gtp2cUnitdata g2c_ud;
459 var PDU_GTPCv2 g2c;
460 var GTP2_ConnHdlr vc_conn;
461 var hexstring imsi;
Philipp Maierac791562023-09-01 17:10:24 +0200462 var OCT1 messageType;
Harald Welte88b3ccb2020-03-12 21:36:32 +0100463 var OCT4 teid;
464 var PDU_UECUPS rx_uecups;
465 var UECUPS_CreateTun gtc;
466 var UECUPS_DestroyTun gtd;
467 var UECUPS_StartProgram sprog;
468
469 f_init(cfg);
470
471 while (true) {
472 alt {
473 /* route inbound GTP2-C based on TEID, SEQ or IMSI */
474 [] GTP2C.receive(Gtp2cUnitdata:?) -> value g2c_ud {
475 var template hexstring imsi_t := f_gtp2c_extract_imsi(g2c_ud.gtpc);
476 if (not ispresent(g2c_ud.gtpc.tEID) or g2c_ud.gtpc.tEID == int2oct(0, 4)) {
477 /* if this is a response, route by SEQ */
478 if (match(g2c_ud.gtpc, tr_PDU_GTP2C_msgtypes(gtp2_responses))
479 and f_seq_known(g2c_ud.gtpc.sequenceNumber)) {
480 vc_conn := f_comp_by_seq(g2c_ud.gtpc.sequenceNumber);
481 CLIENT.send(g2c_ud.gtpc) to vc_conn;
482 } else {
Philipp Maierac791562023-09-01 17:10:24 +0200483 SendToUdMsgTable(g2c_ud);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100484 TEID0.send(g2c_ud.gtpc);
485 }
486 } else if (ispresent(g2c_ud.gtpc.tEID) and g2c_ud.gtpc.tEID != int2oct(0, 4)) {
487 vc_conn := f_comp_by_teid(g2c_ud.gtpc.tEID);
488 CLIENT.send(g2c_ud.gtpc) to vc_conn;
489 } else if (isvalue(imsi_t)) {
490 vc_conn := f_comp_by_imsi(valueof(imsi_t));
491 CLIENT.send(g2c_ud.gtpc) to vc_conn;
492 } else {
493 /* Send to all clients */
494 var integer i;
495 for (i := 0; i < sizeof(TidTable); i := i+1) {
496 if (isbound(TidTable[i].teid) and TidTable[i].teid == teid) {
497 CLIENT.send(g2c_ud.gtpc) to TidTable[i].vc_conn;
498 }
499 }
500 }
501
502 /* remove sequence number if response was received */
503 if (match(g2c_ud.gtpc, tr_PDU_GTP2C_msgtypes(gtp2_responses))) {
504 f_seq_tbl_del(g2c_ud.gtpc.sequenceNumber);
505 }
506
507 }
508
509 [] TEID0.receive(PDU_GTPCv2:?) -> value g2c sender vc_conn {
510 /* patch in the next sequence number */
511 /* FIXME: do this only for outbound requests */
512 g2c.sequenceNumber := int2oct(g_c_seq_nr, 3);
513 g_c_seq_nr := g_c_seq_nr + 1;
514 /* build Gtp2cUnitdata */
515 g2c_ud := { peer := g_peer, gtpc := g2c };
516 GTP2C.send(g2c_ud);
517 if (match(g2c, tr_PDU_GTP2C_msgtypes(gtp2_requests))) {
518 f_seq_tbl_add(g2c.sequenceNumber, vc_conn);
519 }
520 }
521
522 [] CLIENT.receive(PDU_GTPCv2:?) -> value g2c sender vc_conn {
523 /* patch in the next sequence number */
524 /* FIXME: do this only for outbound requests */
525 g2c.sequenceNumber := int2oct(g_c_seq_nr, 3);
526 g_c_seq_nr := g_c_seq_nr + 1;
527 /* build Gtp2cUnitdata */
528 g2c_ud := { peer := g_peer, gtpc := g2c };
529 GTP2C.send(g2c_ud);
530 if (match(g2c, tr_PDU_GTP2C_msgtypes(gtp2_requests))) {
531 f_seq_tbl_add(g2c.sequenceNumber, vc_conn);
532 }
533 }
534
535 [] CLIENT_PROC.getcall(GTP2EM_register_imsi:{?}) -> param(imsi) sender vc_conn {
536 f_imsi_tbl_add(imsi, vc_conn);
537 CLIENT_PROC.reply(GTP2EM_register_imsi:{imsi}) to vc_conn;
538 }
Philipp Maierac791562023-09-01 17:10:24 +0200539 [] CLIENT_PROC.getcall(GTP2EM_register_udmsg:{?}) -> param(messageType) sender vc_conn {
540 f_udmsg_tbl_add(messageType, vc_conn);
541 CLIENT_PROC.reply(GTP2EM_register_udmsg:{messageType}) to vc_conn;
542 }
Harald Welte88b3ccb2020-03-12 21:36:32 +0100543
544 [] CLIENT_PROC.getcall(GTP2EM_register_teid:{?}) -> param(teid) sender vc_conn {
545 f_tid_tbl_add(teid, vc_conn);
546 CLIENT_PROC.reply(GTP2EM_register_teid:{teid}) to vc_conn;
547 }
548 [] CLIENT_PROC.getcall(GTP2EM_allocate_teid:{}) -> sender vc_conn {
549 var OCT4 t := f_alloc_teid();
550 f_tid_tbl_add(t, vc_conn);
551 CLIENT_PROC.reply(GTP2EM_allocate_teid:{} value t) to vc_conn;
552 }
553 [] CLIENT_PROC.getcall(GTP2EM_create_tunnel:{?}) -> param(gtc) sender vc_conn {
554 rx_uecups := f_uecups_xceive({create_tun := gtc}, {create_tun_res:={result:=OK}});
555 CLIENT_PROC.reply(GTP2EM_create_tunnel:{gtc}) to vc_conn;
556 }
557 [] CLIENT_PROC.getcall(GTP2EM_destroy_tunnel:{?}) -> param(gtd) sender vc_conn {
558 rx_uecups := f_uecups_xceive({destroy_tun := gtd}, {destroy_tun_res:={result:=OK}});
559 CLIENT_PROC.reply(GTP2EM_destroy_tunnel:{gtd}) to vc_conn;
560 }
561 [] CLIENT_PROC.getcall(GTP2EM_start_program:{?}) -> param(sprog) sender vc_conn {
562 rx_uecups := f_uecups_xceive({start_program := sprog}, {start_program_res:=?});
563 /* if successful: store (pid, vc_conn) tuple so we can route program_term_ind */
564 if (rx_uecups.start_program_res.result == OK) {
565 f_pid_tbl_add(rx_uecups.start_program_res.pid, vc_conn);
566 }
567 CLIENT_PROC.reply(GTP2EM_start_program:{sprog} value rx_uecups.start_program_res) to vc_conn;
568 }
569
570 }
571 }
572}
573
574
575/***********************************************************************
576 * Interaction between Main and Client Components
577 ***********************************************************************/
578type port GTP2EM_PT message {
579 inout PDU_GTPCv2, UECUPS_ProgramTermInd;
580} with { extension "internal" };
581
582signature GTP2EM_register_imsi(hexstring imsi);
Philipp Maierac791562023-09-01 17:10:24 +0200583signature GTP2EM_register_udmsg(OCT1 messageType);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100584signature GTP2EM_register_teid(OCT4 teid);
585signature GTP2EM_allocate_teid() return OCT4;
586signature GTP2EM_create_tunnel(UECUPS_CreateTun gtc);
587signature GTP2EM_destroy_tunnel(UECUPS_DestroyTun gtd);
588signature GTP2EM_start_program(UECUPS_StartProgram sprog) return UECUPS_StartProgramRes;
589
590type port GTP2EM_PROC_PT procedure {
Philipp Maierac791562023-09-01 17:10:24 +0200591 inout GTP2EM_register_imsi, GTP2EM_register_udmsg, GTP2EM_register_teid, GTP2EM_allocate_teid,
Harald Welte88b3ccb2020-03-12 21:36:32 +0100592 GTP2EM_create_tunnel, GTP2EM_destroy_tunnel, GTP2EM_start_program;
593} with { extension "internal" };
594
595/***********************************************************************
596 * Client Component
597 ***********************************************************************/
598
599type component GTP2_ConnHdlr {
600 port GTP2EM_PT GTP2;
601 port GTP2EM_PROC_PT GTP2_PROC;
602};
603
604function f_gtp2_register_imsi(hexstring imsi) runs on GTP2_ConnHdlr {
Pau Espin Pedrol5c18a0c2023-10-19 13:41:41 +0200605 /* 15-digit IMSIs are len(imsi)=15, but decoded messages are
606 * octet-aligned, hence the hexstring in messages is len(imsi)=16, where
607 * the last hex char is a padding 'F'H */
608 imsi := f_pad_bcd_number(imsi);
Harald Welte88b3ccb2020-03-12 21:36:32 +0100609 GTP2_PROC.call(GTP2EM_register_imsi:{imsi}) {
610 [] GTP2_PROC.getreply(GTP2EM_register_imsi:{imsi});
611 }
612}
613
Philipp Maierac791562023-09-01 17:10:24 +0200614function f_gtp2_register_udmsg(OCT1 messageType) runs on GTP2_ConnHdlr {
615 GTP2_PROC.call(GTP2EM_register_udmsg:{messageType}) {
616 [] GTP2_PROC.getreply(GTP2EM_register_udmsg:{messageType});
617 }
618}
619
Harald Welte88b3ccb2020-03-12 21:36:32 +0100620function f_gtp2_register_teid(OCT4 teid) runs on GTP2_ConnHdlr {
621 GTP2_PROC.call(GTP2EM_register_teid:{teid}) {
622 [] GTP2_PROC.getreply(GTP2EM_register_teid:{teid});
623 }
624}
625
626function f_gtp2_allocate_teid() runs on GTP2_ConnHdlr return OCT4 {
627 var OCT4 t;
628 GTP2_PROC.call(GTP2EM_allocate_teid:{}) {
629 [] GTP2_PROC.getreply(GTP2EM_allocate_teid:{}) -> value t {
630 return t;
631 }
632 }
633}
634
635function f_gtp2_create_tunnel(template (value) UECUPS_CreateTun gtc)
636runs on GTP2_ConnHdlr {
637 GTP2_PROC.call(GTP2EM_create_tunnel:{valueof(gtc)}) {
638 [] GTP2_PROC.getreply(GTP2EM_create_tunnel:{gtc});
639 }
640}
641
642function f_gtp2_destroy_tunnel(template (value) UECUPS_DestroyTun gtd)
643runs on GTP2_ConnHdlr {
644 GTP2_PROC.call(GTP2EM_destroy_tunnel:{valueof(gtd)}) {
645 [] GTP2_PROC.getreply(GTP2EM_destroy_tunnel:{gtd});
646 }
647}
648
649function f_gtp2_start_program(template (value) UECUPS_StartProgram sprog)
650runs on GTP2_ConnHdlr return UECUPS_StartProgramRes {
651 var UECUPS_StartProgramRes res;
652 GTP2_PROC.call(GTP2EM_start_program:{valueof(sprog)}) {
653 [] GTP2_PROC.getreply(GTP2EM_start_program:{sprog}) -> value res;
654 }
655 return res;
656}
657
658
659
660}