blob: 81e3552a5c2e982de8f33c4dadd5539aacf3c3bb [file] [log] [blame]
Harald Welte4526da92020-03-05 23:08:10 +01001module PGW_Tests {
2
3import from General_Types all;
4import from Osmocom_Types all;
5import from Native_Functions all;
6
7import from GTPv2_Types all;
8import from GTPv2_Templates all;
9import from GTPv2_Emulation all;
10
11import from UECUPS_Types all;
12
13import from DNS_Helpers all;
14
Harald Weltef4001512020-04-26 21:48:34 +020015
16import from DIAMETER_Types all;
17import from DIAMETER_Templates all;
18import from DIAMETER_Emulation all;
19
20
Harald Welte4526da92020-03-05 23:08:10 +010021modulepar {
22 charstring mp_pgw_hostname := "127.0.0.3";
23 charstring mp_local_hostname_c := "127.0.0.1";
24 charstring mp_local_hostname_u := "127.0.0.1";
Harald Weltef4001512020-04-26 21:48:34 +020025
Harald Welte4526da92020-03-05 23:08:10 +010026 charstring mp_run_prog_as_user := "laforge";
27 charstring mp_ping_hostname := "10.45.0.1";
Harald Weltef4001512020-04-26 21:48:34 +020028
29 charstring mp_pcrf_local_ip := "127.0.0.5";
30 integer mp_pcrf_local_port := 3868;
Harald Welte4526da92020-03-05 23:08:10 +010031}
32
33/* main component, we typically have one per testcase */
34type component PGW_Test_CT {
35 var GTPv2_Emulation_CT vc_GTP2;
36 port GTP2EM_PT TEID0;
Harald Weltef4001512020-04-26 21:48:34 +020037
38 /* emulated PCRF */
39 var DIAMETER_Emulation_CT vc_DIAMETER;
40 port DIAMETER_PT DIAMETER_UNIT;
41 port DIAMETEREM_PROC_PT DIAMETER_PROC;
Harald Welte4526da92020-03-05 23:08:10 +010042}
43
44/* per-session component; we typically have 1..N per testcase */
Harald Weltef4001512020-04-26 21:48:34 +020045type component PGW_Session_CT extends GTP2_ConnHdlr, DIAMETER_ConnHdlr {
Harald Welte4526da92020-03-05 23:08:10 +010046 var SessionPars g_pars;
47
48 /* TEI (Data) local side */
49 var OCT4 g_teid;
50 /* TEI (Control) local side */
51 var OCT4 g_teic;
52 /* TEI (Data) remote side */
53 var OCT4 g_teid_remote;
54 /* TEI (Control) remote side */
55 var OCT4 g_teic_remote;
56 /* GTP-U IPv4 address remote sie */
57 var OCT4 g_gtpu4_remote;
58 var OCT16 g_gtpu6_remote;
59
60 /* Address allocation */
61 var OCT4 g_ip4_addr;
62 var OCT16 g_ip6_addr;
63 var integer g_ip6_plen;
64}
65
66/* configuration data for a given Session */
67type record SessionPars {
68 hexstring imsi,
69 octetstring msisdn optional,
70 // serving network
71 integer rat_type,
72 // flags?
73 charstring apn,
74 /* Apn subscribed or non-subscribed */
75 boolean selection_mode,
76 BIT3 pdn_type,
77 /* PAA */
78 /* Max APN Restriction */
79 /* APN-AMBR */
80 octetstring pco optional,
81 octetstring epco optional,
82 /* Bearer Contexts to be created */
83
84 charstring tun_dev_name,
85 charstring tun_netns_name optional
86}
87
88template (value) SessionPars
89t_SessionPars(hexstring imsi, charstring tundev, integer rat_type := 6, charstring apn := "internet",
90 boolean selection_mode := false, BIT3 pdn_type := '001'B) := {
91 imsi := imsi,
92 msisdn := omit,
93 rat_type := rat_type,
94 apn := apn,
95 selection_mode := selection_mode,
96 pdn_type := pdn_type,
97 pco := omit,
98 epco := omit,
99 tun_dev_name := tundev,
100 tun_netns_name := tundev
101}
102
103type record BearerConfig {
104 integer eps_bearer_id
105}
106
107type function void_fn() runs on PGW_Session_CT;
108
Harald Weltef4001512020-04-26 21:48:34 +0200109friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg)
110runs on DIAMETER_Emulation_CT return template PDU_DIAMETER {
111 DIAMETER_UNIT.send(msg);
112 return omit;
113}
114
115friend function f_init_diameter(charstring id) runs on PGW_Test_CT {
116 var DIAMETEROps ops := {
117 create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback),
118 unitdata_cb := refers(DiameterForwardUnitdataCallback)
119 };
120 var DIAMETER_conn_parameters pars := {
121 remote_ip := mp_pgw_hostname,
122 remote_sctp_port := -1,
123 local_ip := mp_pcrf_local_ip,
124 local_sctp_port := mp_pcrf_local_port,
125 origin_host := "pcrf.localdomain",
126 origin_realm := "localdomain",
127 vendor_app_id := c_DIAMETER_3GPP_Gx_AID
128 };
129 vc_DIAMETER := DIAMETER_Emulation_CT.create(id);
130 map(vc_DIAMETER:DIAMETER, system:DIAMETER_CODEC_PT);
131 connect(vc_DIAMETER:DIAMETER_UNIT, self:DIAMETER_UNIT);
132 connect(vc_DIAMETER:DIAMETER_PROC, self:DIAMETER_PROC);
133 vc_DIAMETER.start(DIAMETER_Emulation.main(ops, pars, id));
134
135 f_diameter_wait_capability(DIAMETER_UNIT);
136}
137
Harald Welte4526da92020-03-05 23:08:10 +0100138private function f_init() runs on PGW_Test_CT {
139 var Gtp2EmulationCfg cfg := {
140 gtpc_bind_ip := mp_local_hostname_c,
141 gtpc_bind_port := GTP2C_PORT,
142 gtpc_remote_ip := mp_pgw_hostname,
143 gtpc_remote_port := GTP2C_PORT,
144 sgw_role := true,
145 use_gtpu_daemon := true
146 };
147
148 vc_GTP2 := GTPv2_Emulation_CT.create("GTP2_EM");
149 map(vc_GTP2:GTP2C, system:GTP2C);
150 connect(vc_GTP2:TEID0, self:TEID0);
151 vc_GTP2.start(GTPv2_Emulation.main(cfg));
Harald Weltef4001512020-04-26 21:48:34 +0200152
153 if (mp_pcrf_local_ip != "") {
154 f_init_diameter(testcasename());
155 }
Harald Welte4526da92020-03-05 23:08:10 +0100156}
157
158function f_start_handler(void_fn fn, template (omit) SessionPars pars := omit)
159runs on PGW_Test_CT return PGW_Session_CT {
160 var charstring id := testcasename();
161 var PGW_Session_CT vc_conn;
162 vc_conn := PGW_Session_CT.create(id);
163 connect(vc_conn:GTP2, vc_GTP2:CLIENT);
164 connect(vc_conn:GTP2_PROC, vc_GTP2:CLIENT_PROC);
Harald Weltef4001512020-04-26 21:48:34 +0200165
166 if (isbound(vc_DIAMETER)) {
167 connect(vc_conn:DIAMETER, vc_DIAMETER:DIAMETER_CLIENT);
168 connect(vc_conn:DIAMETER_PROC, vc_DIAMETER:DIAMETER_PROC);
169 }
170
Harald Welte4526da92020-03-05 23:08:10 +0100171 vc_conn.start(f_handler_init(fn, pars));
172 return vc_conn;
173}
174
175private function f_handler_init(void_fn fn, template (omit) SessionPars pars := omit)
176runs on PGW_Session_CT {
177 if (isvalue(pars)) {
178 g_pars := valueof(pars);
179 }
Harald Weltef4001512020-04-26 21:48:34 +0200180 if (DIAMETER_PROC.checkstate("Connected")) {
181 f_diameter_expect(g_pars.imsi);
182 }
Harald Welte4526da92020-03-05 23:08:10 +0100183 fn.apply();
184}
185
Harald Weltefe595e42020-04-21 22:56:47 +0200186private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring {
187 var integer suffix_len := tot_len - lengthof(prefix);
188 var charstring suffix_ch := int2str(suffix);
189 var integer pad_len := suffix_len - lengthof(suffix_ch);
190
191 return prefix & int2hex(0, pad_len) & str2hex(suffix_ch);
192}
193
194function f_gen_imei(integer suffix) return hexstring {
195 return f_concat_pad(14, '49999'H, suffix);
196}
197
198function f_gen_imsi(integer suffix) return hexstring {
199 return f_concat_pad(15, '26242'H, suffix);
200}
201
202function f_gen_msisdn(integer suffix) return hexstring {
203 return f_concat_pad(12, '49123'H, suffix);
204}
205
Harald Weltef4001512020-04-26 21:48:34 +0200206private altstep as_DIA_CCR(DCC_NONE_CC_Request_Type req_type) runs on PGW_Session_CT {
207 var PDU_DIAMETER rx_dia;
208 [] DIAMETER.receive(tr_DIA_CCR(req_type := req_type)) -> value rx_dia {
209 var template (omit) AVP avp;
210 var octetstring sess_id;
211 var AVP_Unsigned32 req_num;
212
213 avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id);
214 sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id);
215
216 avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number);
217 req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number);
218
219 DIAMETER.send(ts_DIA_CCA(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id,
220 req_type, req_num));
221 }
222 [] DIAMETER.receive(PDU_DIAMETER:?) -> value rx_dia {
223 setverdict(fail, "Received unexpected DIAMETER ", rx_dia);
224 self.stop;
225 }
226}
227
Harald Welte4526da92020-03-05 23:08:10 +0100228
229/* find TEID of given interface type (and optionally instance) */
230private function f_find_teid(FullyQualifiedTEID_List list,
231 template (present) integer if_type,
232 template (present) BIT4 instance := ?)
233return template (omit) FullyQualifiedTEID
234{
235 var integer i;
236 for (i := 0; i < lengthof(list); i := i+1) {
237 if (match(list[i].interfaceType, if_type) and
238 match(list[i].instance, instance)) {
239 return list[i];
240 }
241 }
242 return omit;
243}
244
245/* process one to-be-created bearer context */
246private function process_bctx_create(BearerContextGrouped bctx) runs on PGW_Session_CT
247{
248 /* FIXME: EPS Bearer ID */
249 /* FIXME: Cause */
250
251 /* find F-TEID of the P-GW U side */
252 var FullyQualifiedTEID rx_fteid;
253 rx_fteid := valueof(f_find_teid(bctx.bearerContextIEs.fullyQualifiedTEID, 5, '0010'B));
254 g_teid_remote := rx_fteid.tEID_GRE_Key;
255 if (rx_fteid.v4_Flag == '1'B) {
256 g_gtpu4_remote := rx_fteid.iPv4_Address;
257 }
258 if (rx_fteid.v6_Flag == '1'B) {
259 g_gtpu6_remote := rx_fteid.iPv6_Address;
260 }
261
262 var UECUPS_CreateTun uecups_create := {
263 tx_teid := oct2int(g_teid_remote),
264 rx_teid := oct2int(g_teid),
265 user_addr_type := IPV4,
266 user_addr := '00000000'O,
267 local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))),
268 remote_gtp_ep := valueof(ts_UECUPS_SockAddr(g_gtpu4_remote)),
269 tun_dev_name := g_pars.tun_dev_name,
270 tun_netns_name := g_pars.tun_netns_name
271 };
272
273 /* create tunnel in daemon */
274 if (isbound(g_ip4_addr)) {
275 uecups_create.user_addr := g_ip4_addr;
276 f_gtp2_create_tunnel(uecups_create);
277 }
278 if (isbound(g_ip6_addr)) {
279 uecups_create.user_addr_type := IPV6;
280 uecups_create.user_addr := g_ip6_addr;
281 f_gtp2_create_tunnel(uecups_create);
282 }
283}
284
285/* create a session on the PGW */
286private function f_create_session() runs on PGW_Session_CT {
287 var PDU_GTPCv2 rx;
288
289 /* allocate + register TEID-C on local side */
290 g_teic := f_gtp2_allocate_teid();
291 g_teid := g_teic;
292
293 var template (value) FullyQualifiedTEID fteid_c_ie, fteid_u_ie;
294 fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0,
295 f_inet_addr(mp_local_hostname_c), omit);
296 fteid_u_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPU, g_teid, 2,
297 f_inet_addr(mp_local_hostname_u), omit);
298 var template (value) PDU_GTPCv2 g2c :=
299 ts_GTP2C_CreateSessionReq(imsi := g_pars.imsi, msisdn := omit, rat_type := 6,
300 sender_fteid := fteid_c_ie,
301 apn := f_enc_dns_hostname(g_pars.apn),
302 pdn_type := g_pars.pdn_type, teid_list := { fteid_u_ie },
303 chg_car := '0000'O, bearer_id := 1);
304 /* open5gs up to 1.2.3 won't accept it without ULI, despite not mandatory */
305 var template (value) TAI tai := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0001'O };
306 var template (value) ECGI ecgi := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0'H, 23 };
307 g2c.gtpcv2_pdu.createSessionRequest.userLocationInfo := ts_GTP2C_UserLocInfo(tai := tai, ecgi := ecgi);
308
309 GTP2.send(g2c);
Harald Weltef4001512020-04-26 21:48:34 +0200310 if (DIAMETER_PROC.checkstate("Connected")) {
311 as_DIA_CCR(INITIAL_REQUEST);
312 }
Harald Welte4526da92020-03-05 23:08:10 +0100313 alt {
314 [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:='10'O)) -> value rx {
315 /* extract TEIDs */
316 var CreateSessionResponse resp := rx.gtpcv2_pdu.createSessionResponse;
317 g_teic_remote := resp.fullyQualifiedTEID[0].tEID_GRE_Key;
318
319 /* extract allocated address[es] */
320 var PDN_Address_and_Prefix paa := resp.pDN_AddressAllocation.pDN_Address_and_Prefix;
321 if (ischosen(paa.iPv4_Address)) {
322 g_ip4_addr := paa.iPv4_Address;
323 } else if (ischosen(paa.iPv6_Address)) {
324 g_ip6_addr := paa.iPv6_Address.iPv6_Address;
325 g_ip6_plen := paa.iPv6_Address.prefixLength;
326 } else if (ischosen(paa.iPv4_IPv6)) {
327 g_ip4_addr := paa.iPv4_IPv6.iPv4_Address;
328 g_ip6_addr := paa.iPv4_IPv6.iPv6_Address;
329 g_ip6_plen := paa.iPv4_IPv6.prefixLength;
330 }
331 var integer i;
332 for (i := 0; i < lengthof(resp.bearerContextGrouped); i := i+1) {
333 var BearerContextGrouped bctx := resp.bearerContextGrouped[i];
334 select (bctx.instance) {
335 case ('0000'B) { // created
336 process_bctx_create(bctx);
337 }
338 case ('0001'B) { // removed
339 setverdict(fail, "We don't expect removed bearer contexts yet");
340 }
341 }
342 }
343 }
344 [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=?)) -> value rx {
345 setverdict(fail, "Unexpected CreateSessionResp(cause=",
346 rx.gtpcv2_pdu.createSessionResponse.cause.causeValue, ")");
347 }
348 [] GTP2.receive {
349 setverdict(fail, "Unexpected GTPv2 while waiting for CreateSessionResp");
350 }
351 }
352
353}
354
355/* delete the session from the PGW */
356private function f_delete_session(template (omit) OCT1 tx_cause := omit,
357 template (present) OCT4 exp_teid,
358 template (present) OCT1 exp_cause) runs on PGW_Session_CT {
359 var template (value) FullyQualifiedTEID fteid_c_ie
360 fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0,
361 f_inet_addr(mp_local_hostname_c), omit);
362 var template PDU_GTPCv2 g2c :=
363 ts_GTP2C_DeleteSessionReq(d_teid := g_teic_remote, cause := tx_cause,
364 sender_fteid := fteid_c_ie,
365 teid_list := {}, bearer_id := 1);
366
367 GTP2.send(g2c);
Harald Weltef4001512020-04-26 21:48:34 +0200368 if (DIAMETER_PROC.checkstate("Connected")) {
369 as_DIA_CCR(TERMINATION_REQUEST);
370 }
Harald Welte4526da92020-03-05 23:08:10 +0100371 alt {
372 [] GTP2.receive(tr_GTP2C_DeleteSessionResp(d_teid := exp_teid, cause := exp_cause)) {
373 setverdict(pass);
374 }
375 [] GTP2.receive(tr_GTP2C_DeleteSessionResp(?, ?)) {
376 setverdict(fail, "Unexpected DeleteSessionResp");
377 }
378 [] GTP2.receive {
379 setverdict(fail, "Unexpected GTPv2 while waiting for DeleteSessionResp");
380 }
381 }
382
383 /* destroy tunnel in daemon */
384 if (isbound(g_teid)) {
385 var UECUPS_DestroyTun uecups_destroy := {
386 local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))),
387 rx_teid := oct2int(g_teid)
388 };
389 /* FIXME: what about IPv4/IPv6 differentiation? */
390 f_gtp2_destroy_tunnel(uecups_destroy);
391 }
392}
393
394/* start a program on the user plane side; return its PID */
395private function f_start_prog(charstring command) runs on PGW_Session_CT return integer
396{
397 var UECUPS_StartProgram sprog := {
398 command := command,
399 environment := {},
400 run_as_user := mp_run_prog_as_user,
401 tun_netns_name := g_pars.tun_netns_name
402 };
403 var UECUPS_StartProgramRes res := f_gtp2_start_program(sprog);
404 if (res.result != OK) {
405 setverdict(fail, "Unable to start program '", command, "'");
406 }
407 return res.pid;
408}
409
410/* wait for termination of a given PID with specified exit_code */
411private function f_wait_term(integer pid, template (present) integer exit_code := 0,
412 float tout := 10.0) runs on PGW_Session_CT
413{
414 timer T := tout;
415
416 T.start;
417 alt {
418 [] GTP2.receive(UECUPS_ProgramTermInd:{pid := pid, exit_code := exit_code}) {
419 setverdict(pass);
420 }
421 [] GTP2.receive(UECUPS_ProgramTermInd:?) {
422 setverdict(fail, "Received unexpected ProgramTermInd");
423 }
424 [] T.timeout {
425 setverdict(fail, "timeout waiting for user-plane program termination");
426 }
427 }
428}
429
430/* execute a program and wait for result */
431private function f_start_prog_wait(charstring command, template integer exit_code := 0, float tout := 10.0) runs on PGW_Session_CT
432{
433 var integer pid := f_start_prog(command);
434 f_wait_term(pid, exit_code, tout);
435}
436
437/* execute ping command and wait for result */
438private function f_ping4(charstring host, integer interval := 1, integer count := 10) runs on PGW_Session_CT
439{
440 var charstring ping :="ping -c " & int2str(count) & " -i " & int2str(interval);
441 ping := ping & " -I " & f_inet_ntoa(g_ip4_addr);
442 ping := ping & " " & host;
Harald Welte8cfdc7c2020-04-21 22:48:34 +0200443 f_start_prog_wait(ping, tout := int2float(5 + interval*count));
Harald Welte4526da92020-03-05 23:08:10 +0100444}
445
446
447
448
449/* send echo request; expect response */
450testcase TC_tx_echo() runs on PGW_Test_CT {
451 timer T := 5.0;
452
453 f_init();
454
455 TEID0.send(ts_GTP2C_EchoReq(0));
456 T.start;
457 alt {
458 [] TEID0.receive(tr_GTP2C_EchoResp) {
459 setverdict(pass);
460 }
461 [] T.timeout {
462 setverdict(fail, "timeout waiting for Echo Response");
463 }
464 }
465}
466
467/* create a session, expect it to succeed */
468private function f_TC_createSession() runs on PGW_Session_CT {
469 f_create_session();
470 setverdict(pass);
471}
472testcase TC_createSession() runs on PGW_Test_CT {
473 var PGW_Session_CT vc_conn;
474 var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun22"));
475 f_init();
476 vc_conn := f_start_handler(refers(f_TC_createSession), pars);
477 vc_conn.done;
478}
479
480/* create a session, then execute a ping command on the user plane */
481private function f_TC_createSession_ping4() runs on PGW_Session_CT {
482 f_create_session();
483 f_ping4(mp_ping_hostname);
484 setverdict(pass);
485}
486testcase TC_createSession_ping4() runs on PGW_Test_CT {
487 var PGW_Session_CT vc_conn;
488 var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23"));
489 f_init();
490 vc_conn := f_start_handler(refers(f_TC_createSession_ping4), pars);
491 vc_conn.done;
492}
Harald Weltefe595e42020-04-21 22:56:47 +0200493testcase TC_createSession_ping4_256() runs on PGW_Test_CT {
494 var PGW_Session_CT vc_conn[256];
495 var integer i;
496
497 f_init();
498
499 for (i := 0; i < sizeof(vc_conn); i:=i+1) {
500 var charstring tundev := "ping" & int2str(i);
501 var SessionPars pars := valueof(t_SessionPars(f_gen_imsi(i), tundev));
502 vc_conn[i] := f_start_handler(refers(f_TC_createSession_ping4), pars);
503 }
504
505 for (i := 0; i < lengthof(vc_conn); i:=i+1) {
506 vc_conn[i].done;
507 }
508}
509
Harald Welte4526da92020-03-05 23:08:10 +0100510
511/* create a session, then delete it again */
512private function f_TC_createSession_deleteSession() runs on PGW_Session_CT {
513 f_create_session();
514 f_delete_session(omit, g_teic, '10'O);
515 setverdict(pass);
516}
517testcase TC_createSession_deleteSession() runs on PGW_Test_CT {
518 var PGW_Session_CT vc_conn;
519 var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23"));
520 f_init();
521 vc_conn := f_start_handler(refers(f_TC_createSession_deleteSession), pars);
522 vc_conn.done;
523}
524
525/* send a DeleteSessionReq for an unknown/invalid TEID */
526private function f_TC_deleteSession_unknown() runs on PGW_Session_CT {
527 g_teic := f_gtp2_allocate_teid();
528 g_teic_remote := f_rnd_octstring(4);
529 f_delete_session(omit, '00000000'O, '40'O /* Context Unknown */);
530 setverdict(pass);
531}
532testcase TC_deleteSession_unknown() runs on PGW_Test_CT {
533 var PGW_Session_CT vc_conn;
534 var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23"));
535 f_init();
536 vc_conn := f_start_handler(refers(f_TC_deleteSession_unknown), pars);
537 vc_conn.done;
538}
539
540
541
542
543control {
544 execute( TC_tx_echo() );
545 execute( TC_createSession() );
546 execute( TC_createSession_ping4() );
Harald Weltefe595e42020-04-21 22:56:47 +0200547 execute( TC_createSession_ping4_256() );
Harald Welte4526da92020-03-05 23:08:10 +0100548 execute( TC_createSession_deleteSession() );
549 execute( TC_deleteSession_unknown() );
550}
551
552
553}