summaryrefslogtreecommitdiffstats
path: root/ggsn_tests/GGSN_Tests.ttcn
blob: 2066e0dd1522114da3382de990c26a9d9bf435fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
module GGSN_Tests {

	import from General_Types all;
	import from Osmocom_Types all;
	import from IPL4asp_PortType all;
	import from IPL4asp_Types all;
	import from GTP_CodecPort all;
	import from GTP_CodecPort_CtrlFunct all;
	import from GTPC_Types all;
	import from GTPU_Types all;
	import from IP_Types all;
	import from ICMPv6_Types all;

	const integer GTP0_PORT := 3386;
	const integer GTP1C_PORT := 2123;
	const integer GTP1U_PORT := 2152;
	const charstring g_bind_ip_c := "127.23.42.1";
	const charstring g_bind_ip_u := g_bind_ip_c;

	type set PdpContext {
		hexstring	imsi,
		octetstring	msisdn optional,
		octetstring	apn,
		ProtConfigOptions	pco_req optional,
		ProtConfigOptions	pco_neg optional,
		EndUserAddress	eua,
		OCT16		ip6_prefix optional,
		BIT4		nsapi,
		/* TEI (Data) local side */
		OCT4		teid,
		/* TEI (Control) local side */
		OCT4		teic,
		/* TEI (Data) remote side */
		OCT4		teid_remote,
		/* TEI (Control) remote side */
		OCT4		teic_remote
	}

	type component GT_CT {
		port GTPC_PT GTPC;
		port GTPU_PT GTPU;

		var boolean g_initialized := false;

		var OCT1 g_restart_ctr := '01'O;
		/* FIXME: unify with g_bind_ip + parse from config file */
		var OCT4 g_sgsn_ip_c := '7f172a01'O;
		var OCT4 g_sgsn_ip_u := '7f172a01'O;
		/* FIXME: parse remName from config file */
		var GtpPeer g_peer_c := { connId := 0, remName := "127.0.0.6", remPort := GTP1C_PORT };
		var GtpPeer g_peer_u := { connId := 0, remName := "127.0.0.6", remPort := GTP1U_PORT };
		timer T_default := 3.0;

		/* next to-be-sent GTP-C sequence number */
		var uint16_t g_c_seq_nr;
		/* next to-be-sent GTP-U sequence number */
		var uint16_t g_d_seq_nr;
	}

	function f_init() runs on GT_CT {
		if (g_initialized == true) {
			return;
		}
		g_initialized := true;

		var Result res;
		map(self:GTPC, system:GTPC);
		res := GTP_CodecPort_CtrlFunct.f_IPL4_listen(GTPC, g_bind_ip_c, GTP1C_PORT, {udp:={}});
		log("GTP1C ConnectionID: ", res.connId);
		g_peer_c.connId := res.connId;

		map(self:GTPU, system:GTPU);
		res := GTP_CodecPort_CtrlFunct.f_GTPU_listen(GTPU, g_bind_ip_u, GTP1U_PORT, {udp:={}});
		g_peer_u.connId:= res.connId;

		g_restart_ctr := f_rnd_octstring(1);
		g_c_seq_nr := (oct2int(g_restart_ctr) * 1024) mod 65536;
		g_d_seq_nr := (oct2int(g_restart_ctr) * 2000) mod 65536;
	}

	/* generalized GTP-C receive template */
	template PDU_GTPC tr_GTP1C_PDU(template OCT1 msg_type, template OCT4 teid) := {
		/* N-PDU Number flag (PN) shall be set to '0'. A GTP-C receiver shall not return an
		 * error if this flag is set to '1'. */
		pn_bit := '0'B,
		/* Sequence number flag (S) shall be set to '1'. */
		s_bit := '1'B,
		e_bit := ?,
		spare := ?,
		/* Protocol Type flag (PT) shall be set to '1'.*/
		pt := '1'B,
		/* Version shall be set to decimal 1 ('001'). */
		version := '001'B,
		messageType := msg_type,
		lengthf := ?,
		teid := teid,
		opt_part := *,
		gtpc_pdu := ?
	}

	/* generalized GTP-C send template */
	template PDU_GTPC ts_GTP1C_PDU(OCT1 msg_type, OCT4 teid, GTPC_PDUs pdu, uint16_t seq_nr) := {
		/* N-PDU Number flag (PN) shall be set to '0'. A GTP-C receiver shall not return an
		 * error if this flag is set to '1'. */
		pn_bit := '0'B,
		/* Sequence number flag (S) shall be set to '1'. */
		s_bit := '1'B,
		e_bit := '0'B,
		spare := '0'B,
		/* Protocol Type flag (PT) shall be set to '1'.*/
		pt := '1'B,
		/* Version shall be set to decimal 1 ('001'). */
		version := '001'B,
		messageType := msg_type,
		lengthf := 0,	/* we assume encoder overwrites this */
		teid := teid,
		opt_part := {
			sequenceNumber := int2oct(seq_nr, 2),
			npduNumber := '00'O,
			nextExtHeader := '00'O,
			gTPC_extensionHeader_List := omit
		},
		gtpc_pdu := pdu
	}

	/* recovery IE */
	template Recovery_gtpc ts_Recovery(OCT1 restart_counter) := {
		type_gtpc := '00'O, /* we assume encoder fixes this */
		restartCounter := restart_counter
	}

	/* template matching reception of GTP-C echo-request */
	template Gtp1cUnitdata tr_GTPC_MsgType(template GtpPeer peer, template OCT1 msg_type, template OCT4 teid) := {
		peer := peer,
		gtpc := tr_GTP1C_PDU(msg_type, teid)
	}

	/* template matching reception of GTP-C echo-request */
	template Gtp1cUnitdata tr_GTPC_PING(template GtpPeer peer) := tr_GTPC_MsgType(peer, echoRequest, '00000000'O);

	template GTPC_PDUs ts_EchoRespPDU(OCT1 restart_counter) := {
		echoResponse := {
			recovery := {
				type_gtpc := '00'O, /* we assume encoder fixes? */
				restartCounter := restart_counter
			},
			private_extension_gtpc := omit
		}
	}

	/* master template for senidng a GTP-C echo response */
	template Gtp1cUnitdata ts_GTPC_PONG(GtpPeer peer, uint16_t seq, OCT1 rest_ctr) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(echoResponse, '00000000'O, valueof(ts_EchoRespPDU(rest_ctr)), seq)
	}

	template EndUserAddress t_EuaIPv4(template OCT4 ip_addr) := {
		type_gtpc := '80'O,
		endUserAddress := {
			endUserAddressIPv4 := {
				lengthf := 2,
				pdp_typeorg := '0001'B,
				spare := '1111'B,
				pdp_typenum := '21'O,
				ipv4_address := ip_addr
			}
		}
	}
	template EndUserAddress t_EuaIPv4Dyn := t_EuaIPv4(omit);
	template EndUserAddress t_EuaIPv6(template OCT16 ip_addr) := {
		type_gtpc := '80'O,
		endUserAddress := {
			endUserAddressIPv6 := {
				lengthf := 2,
				pdp_typeorg := '0001'B,
				spare := '1111'B,
				pdp_typenum := '57'O,
				ipv6_address := ip_addr
			}
		}
	}
	template EndUserAddress t_EuaIPv6Dyn := t_EuaIPv6(omit);

	template AccessPointName ts_APN(octetstring apn) := {
		type_gtpc := '83'O,
		lengthf := lengthof(apn),
		apn_value := apn
	}

	template GSN_Address_GTPC ts_GsnAddr(octetstring ip_addr) := {
		type_gtpc := '85'O,
		lengthf := lengthof(ip_addr),
		addressf := ip_addr
	}

	template MSISDN ts_Msisdn(octetstring msisdn) := {
		type_gtpc := '86'O,
		lengthf := lengthof(msisdn),
		msisdn := msisdn
	}

	template QualityOfServiceProfile ts_QosDefault := {
		type_gtpc := '87'O,
		lengthf := 4,
		allocRetensionPrio := '00'O,
		qos_ProfileValue := {
			reliabilityClass := '011'B,
			delayClass := '001'B,
			spare1 := '00'B,
			precedenceClass := '010'B,
			spare2 := '0'B,
			peakThroughput := '1001'B,
			meanThroughput := '11111'B,
			spare3 := '000'B,
			deliverErroneusSDU := omit,
			deliveryOrder := omit,
			trafficClass := omit,
			maxSDUSize := omit,
			maxBitrateUplink := omit,
			maxBitrateDownlink := omit,
			sduErrorRatio := omit,
			residualBER := omit,
			trafficHandlingPriority := omit,
			transferDelay := omit,
			guaranteedBitRateUplink := omit,
			guaranteedBitRateDownlink := omit,
			sourceStatisticsDescriptor := omit,
			signallingIndication := omit,
			spare4 := omit,
			maxBitrateDownlinkExt := omit,
			guaranteedBitRateDownlinkExt := omit,
			maxBitrateUplinkExt := omit,
			guaranteedBitRateUplinkExt := omit
		}
	}

	template IMSI_gtpc ts_Imsi(hexstring digits) := {
		type_gtpc := '02'O,
		digits := digits,
		padding := 'F'H
	}

	template GTPC_PDUs ts_CreatePdpPDU(hexstring imsi, OCT1 restart_ctr, OCT4 teid_data, OCT4 teid_ctrl,
					   BIT4 nsapi, EndUserAddress eua, octetstring apn,
					   octetstring sgsn_ip_sign, octetstring sgsn_ip_data,
					   octetstring msisdn, template ProtConfigOptions pco := omit) := {
		createPDPContextRequest := {
			imsi := ts_Imsi(imsi),
			rai := omit,
			recovery := ts_Recovery(restart_ctr),
			selectionMode := {
				type_gtpc := '0F'O,
				selectModeValue := '00'B,
				spare := '111111'B
			},
			teidDataI := {
				type_gtpc := '00'O,
				teidDataI := teid_data
			},
			teidControlPlane := {
				type_gtpc := '00'O,
				teidControlPlane := teid_ctrl
			},
			nsapi := {
				type_gtpc := '00'O,
				nsapi := nsapi,
				unused := '0000'B
			},
			linked_nsapi := omit,
			charging_char := omit,
			trace_ref := omit,
			trace_type := omit,
			endUserAddress := eua,
			accessPointName := ts_APN(apn),
			protConfigOptions := pco,
			sgsn_addr_signalling := ts_GsnAddr(sgsn_ip_sign),
			sgsn_addr_traffic := ts_GsnAddr(sgsn_ip_data),
			msisdn := ts_Msisdn(msisdn),
			qualityOfServiceProfile := ts_QosDefault,
			tft := omit,
			triggerId := omit,
			omcId := omit,
			commonFlags := omit,
			aPN_Restriction := omit,
			ratType := omit,
			userLocationInformation := omit,
			mS_TimeZone := omit,
			imeisv := omit,
			camelChargingInformationContainer := omit,
			additionalTraceInfo := omit,
			correlationID := omit,
			evolvedAllocationRetentionPriorityI := omit,
			extendedCommonFlags := omit,
			userCSGInformation := omit,
			aPN_AMBR := omit,
			signallingPriorityIndication := omit,
			cN_OperatorSelectionEntity := omit,
			private_extension_gtpc := omit
		}
	}

	template Gtp1cUnitdata ts_GTPC_CreatePDP(GtpPeer peer, uint16_t seq, hexstring imsi,
						 OCT1 restart_ctr, OCT4 teid_data,
						 OCT4 teid_ctrl, BIT4 nsapi, EndUserAddress eua,
						 octetstring apn, octetstring sgsn_ip_sign,
						 octetstring sgsn_ip_data, octetstring msisdn,
						 template ProtConfigOptions pco := omit) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(createPDPContextRequest, '00000000'O,
					valueof(ts_CreatePdpPDU(imsi, restart_ctr, teid_data, teid_ctrl,
								nsapi, eua, apn, sgsn_ip_sign,
								sgsn_ip_data, msisdn, pco)), seq)
	}

	template ProtConfigOptions ts_PCO := {
		type_gtpc := '84'O,
		lengthf := 0,
		configProtocol := '000'B,
		spare := '0000'B,
		extension0 := '1'B,
		protocols := {}
	}


	template ProtConfigOptions ts_PCO_IPv6_DNS modifies ts_PCO := {
		protocols := {
			{ protocolID := '0003'O, lengthProtoID := 0, protoIDContents := ''O }
		}
	}

	template ProtConfigOptions ts_PCO_IPv4_DNS_IPCP modifies ts_PCO := {
		protocols := {
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents := '01000010810600000000830600000000'O }
		}
	}

	template ProtConfigOptions ts_PCO_IPv4_DNS_CONT modifies ts_PCO := {
		protocols := {
			{ protocolID := '000d'O, lengthProtoID := 0, protoIDContents := ''O }
		}
	}

	function f_teardown_ind_IE(in template BIT1 ind) return template TearDownInd {
/*
		if (not isvalue(ind)) {
			return omit;
		}
*/
		var TearDownInd ret := {
			type_gtpc := '13'O,
			tdInd := valueof(ind),
			spare:= '0000000'B
		}
		return ret;
	}

	template GTPC_PDUs ts_DeletePdpPDU(BIT4 nsapi, template BIT1 teardown_ind) := {
		deletePDPContextRequest := {
			cause := omit,
			tearDownIndicator := f_teardown_ind_IE(teardown_ind),
			nsapi := {
				type_gtpc := '14'O,
				nsapi := nsapi,
				unused := '0000'B
			},
			protConfigOptions := omit,
			userLocationInformation := omit,
			mS_TimeZone := omit,
			extendedCommonFlags := omit,
			uLI_Timestamp := omit,
			private_extension_gtpc := omit
		}
	}

	template Gtp1cUnitdata ts_GTPC_DeletePDP(GtpPeer peer, uint16_t seq, OCT4 teid,
						 BIT4 nsapi, template BIT1 teardown_ind) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(deletePDPContextRequest, teid,
					valueof(ts_DeletePdpPDU(nsapi, teardown_ind)), seq)
	}


	/* GTP-U */

	template PDU_GTPU tr_GTP1U_PDU(template OCT1 msg_type, template OCT4 teid, template GTPU_IEs ies := ?) := {
		pn_bit := ?,
		s_bit := ?,
		e_bit := ?,
		spare := ?,
		/* Protocol Type flag (PT) shall be set to '1' in GTP */
		pt := '1'B,
		/* Version shall be set to decimal 1 ('001'). */
		version := '001'B,
		messageType := msg_type,
		lengthf := ?,
		teid := teid,
		opt_part := *,
		gtpu_IEs := ies
	}

	/* generalized GTP-U send template */
	template PDU_GTPU ts_GTP1U_PDU(OCT1 msg_type, uint16_t seq, OCT4 teid, GTPU_IEs ies) := {
		/* N-PDU Number flag (PN): the GTP-U header contains a meaningful N-PDU Number field if the PN
		 * flag is set to 1. */
		pn_bit := '0'B,	/* we assume the encoder overwrites this if an optional part is given */
		/* If the Sequence Number flag (S) is set to '1' the sequence number field is present and
		 * meaningful otherwise it is set to '0'. For GTP-U messages Echo Request, Echo Response,
		 * Error Indication and Supported Extension Headers Notification, the S flag shall be set to '1'. */
		s_bit := '1'B, 	/* we assume the encoder overwrites this if an optional part is given */
		/* Extension header presence */
		e_bit := '0'B,
		spare := '0'B,
		/* Protocol Type flag (PT) shall be set to '1' in GTP */
		pt := '1'B,
		/* Version shall be set to decimal 1 ('001'). */
		version := '001'B,
		messageType := msg_type,
		lengthf := 0,	/* we assume encoder overwrites this */
		teid := teid,
		opt_part :=  {
			sequenceNumber := int2oct(seq, 2),
			npduNumber := '00'O,
			nextExtHeader := '00'O,
			gTPU_extensionHeader_List := omit
		},
		gtpu_IEs := ies
	}

	template Gtp1uUnitdata tr_GTPU_MsgType(template GtpPeer peer, template OCT1 msg_type, template OCT4 teid) := {
		peer := peer,
		gtpu := tr_GTP1U_PDU(msg_type, teid)
	}


	/* template matching reception of GTP-U echo-request */
	template Gtp1uUnitdata tr_GTPU_PING(template GtpPeer peer) := tr_GTPU_MsgType(peer, echoRequest, '00000000'O);

	/* template matching reception of GTP-U GPDU */
	template GTPU_IEs t_GPDU(template octetstring data) := {
		g_PDU_IEs := {
			data := data
		}
	}
	template Gtp1uUnitdata tr_GTPU_GPDU(template GtpPeer peer, template OCT4 teid, template octetstring data := ?) := {
		peer := peer,
		gtpu := tr_GTP1U_PDU('FF'O, teid, t_GPDU(data))
	}

	template GTPU_IEs ts_UEchoRespPDU(OCT1 restart_counter) := {
		echoResponse_IEs := {
			recovery_gtpu := {
				type_gtpu := '00'O, /* we assume encoder fixes? */
				restartCounter := restart_counter
			},
			private_extension_gtpu := omit
		}
	}

	/* master template for sending a GTP-U echo response */
	template Gtp1uUnitdata ts_GTPU_PONG(GtpPeer peer, uint16_t seq, OCT1 rest_ctr) := {
		peer := peer,
		gtpu := ts_GTP1U_PDU(echoResponse, seq, '00000000'O, valueof(ts_UEchoRespPDU(rest_ctr)))
	}

	/* master template for sending a GTP-U user plane data */
	template Gtp1uUnitdata ts_GTP1U_GPDU(GtpPeer peer, uint16_t seq, OCT4 teid, octetstring data) := {
		peer := peer,
		gtpu := ts_GTP1U_PDU('FF'O, seq, teid, { g_PDU_IEs := { data := data }})
	}

	/* Altstep implementing responses to any incoming echo requests */
	altstep pingpong() runs on GT_CT {
		var Gtp1cUnitdata ud;
		var Gtp1uUnitdata udu;
		[] GTPC.receive(tr_GTPC_PING(?)) -> value ud {
			var uint16_t seq := oct2int(ud.gtpc.opt_part.sequenceNumber);
			GTPC.send(ts_GTPC_PONG(ud.peer, seq, '00'O));
			repeat;
		};
		[] GTPU.receive(tr_GTPU_PING(?)) -> value udu {
			var uint16_t seq := oct2int(udu.gtpu.opt_part.sequenceNumber);
			GTPU.send(ts_GTPU_PONG(udu.peer, seq, '00'O));
		};
		[] T_default.timeout { setverdict(fail); };
	}

	/* 'internet' in DNS encoding */
	template octetstring t_ApnInternet := '08696E7465726E6574'O;

	/* return random integer between 0 and max */
	function f_rnd_int(integer max) return integer {
		return float2int(rnd()*int2float(max));
	}

	/* return random NSAPI */
	function f_rnd_nsapi() return BIT4 {
		return int2bit(f_rnd_int(16), 4);
	}

	/* return random TEI[DC] */
	function f_rnd_tei() return OCT4 {
		return int2oct(f_rnd_int(4294967296), 4);
	}

	/* define an (internal) representation of a PDP context */
	template PdpContext t_DefinePDP(hexstring imsi, octetstring msisdn, octetstring apn,
					EndUserAddress eua) := {
		imsi := imsi,
		msisdn := msisdn,
		nsapi := f_rnd_nsapi(),
		apn := apn,
		pco_req := omit,
		eua := eua,
		teid := f_rnd_tei(),
		teic := f_rnd_tei()
	}

	/* send GTP-C for a given context and increment sequence number */
	function f_send_gtpc(inout PdpContext ctx, in template Gtp1cUnitdata data) runs on GT_CT {
		GTPC.send(data);
		g_c_seq_nr := g_c_seq_nr + 1;
	}

	/* send GTP-U for a given context and increment sequence number */
	function f_send_gtpu(inout PdpContext ctx, in octetstring data) runs on GT_CT {
		GTPU.send(ts_GTP1U_GPDU(g_peer_u, g_d_seq_nr, ctx.teid_remote, data));
		g_d_seq_nr := g_d_seq_nr + 1;
	}

	/* send a PDP context activation */
	function f_pdp_ctx_act(inout PdpContext ctx) runs on GT_CT {
		var Gtp1cUnitdata ud;
		var default d;

		log("sending CreatePDP");
		f_send_gtpc(ctx, ts_GTPC_CreatePDP(g_peer_c, g_c_seq_nr, ctx.imsi, g_restart_ctr,
						  ctx.teid, ctx.teic, ctx.nsapi, ctx.eua, ctx.apn,
						  g_sgsn_ip_c, g_sgsn_ip_u, ctx.msisdn, ctx.pco_req));
		T_default.start;
		d := activate(pingpong());
		alt {
			[] GTPC.receive(tr_GTPC_MsgType(g_peer_c, createPDPContextResponse, ctx.teic)) -> value ud {
				var CreatePDPContextResponse cpr := ud.gtpc.gtpc_pdu.createPDPContextResponse;
				if (cpr.cause.causevalue == '80'O) {
					ctx.teid_remote := cpr.teidDataI.teidDataI;
					ctx.teic_remote := cpr.teidControlPlane.teidControlPlane;
					ctx.eua := cpr.endUserAddress;
					ctx.pco_neg := cpr.protConfigOptions;
					setverdict(pass);
				} else {
					setverdict(fail);
				}
			}
		}
		deactivate(d);
		T_default.stop;
	}

	function f_pdp_ctx_del(PdpContext ctx, template BIT1 teardown_ind) runs on GT_CT {
		var Gtp1cUnitdata ud;
		var default d;

		f_send_gtpc(ctx, ts_GTPC_DeletePDP(g_peer_c, g_c_seq_nr, ctx.teic_remote, ctx.nsapi, teardown_ind));
		T_default.start;
		d := activate(pingpong());
		alt {
			[] GTPC.receive(tr_GTPC_MsgType(g_peer_c, deletePDPContextResponse, ctx.teic)) -> value ud {
				if (ud.gtpc.gtpc_pdu.deletePDPContextResponse.cause.causevalue == '80'O) {
					setverdict(pass);
				} else {
					setverdict(fail);
				}
			}
		}
		deactivate(d);
		T_default.stop;
	}
	/* IPv6 router solicitation  fe80::2 -> ff02::2 from 02:88:b5:1f:25:59 */
	const octetstring c_router_solicit := '6000000000103afffe800000000000000000000000000002ff02000000000000000000000000000285009f2b0000000001010288b51f2559'O;
	/* IPv6 neighbor solicitation fe80::2 -> ff02::1:ff00:2 from 02:88:b5:1f:25:59 */
	const octetstring c_neigh_solicit:= '6000000000203afffe800000000000000000000000000002ff0200000000000000000001ff00000287009f9600000000fe80000000000000000000000000000201010288b51f2559'O;

	/* template to generate a 'Prefix Information' ICMPv6 option */
	template OptionField ts_ICMP6_OptPrefix(OCT16 prefix, INT1 prefix_len) := {
		prefixInformation := {
			typeField := 3,
			lengthIndicator := 8,
			prefixLength := prefix_len,
			reserved1 := '000000'B,
			a_Bit := '0'B,
			l_Bit := '0'B,
			validLifetime := oct2int('FFFFFFFF'O),
			preferredLifetime := oct2int('FFFFFFFF'O),
			reserved2 := '00000000'O,
			prefix := prefix
		}
	}

	/* template for an ICMPv6 router solicitation */
	template PDU_ICMPv6 ts_ICMPv6_RS := {
		routerSolicitation := {
			typeField := 133,
			code := 0,
			checksum := '0000'O,
			reserved := '00000000'O,
			/* TODO: do we need 'Source link-layer address' ? */
			options := omit
		}
	}

	/* template for an ICMPv6 router advertisement */
	template PDU_ICMPv6 ts_ICMPv6_RA(OCT16 prefix, INT1 prefix_len) := {
		routerAdvertisement := {
			typeField := 134,
			code := 0,
			checksum := '0000'O,
			curHopLimit := ?,
			reserved := '000000'B,
			o_Bit := '0'B,
			m_Bit := '0'B,
			routerLifetime := oct2int('FFFF'O),
			reachableTime := oct2int('FFFFFFFF'O),
			retransTimer := oct2int('FFFFFFFF'O),
			options := {
				ts_ICMP6_OptPrefix(prefix, prefix_len)
			}
		}
	}

	template PDU_ICMPv6 ts_ICMPv6_NS(OCT16 target_addr) := {
		neighborSolicitation := {
			typeField := 135,
			code := 0,
			checksum := '0000'O,
			reserved := '00000000'O,
			targetAddress := target_addr,
			/* TODO: do we need 'Source link-layer address' ? */
			options := omit
		}
	}

	/* derive ICMPv6 link-local address from lower 64bit of link_id */
	/* template for receiving/matching an ICMPv6 'Prefix Information' option */
	template OptionField tr_ICMP6_OptPrefix(template OCT16 prefix, template INT1 prefix_len) := {
		prefixInformation := {
			typeField := 3,
			lengthIndicator := 4,
			prefixLength := prefix_len,
			reserved1 := ?,
			a_Bit := ?,
			l_Bit := ?,
			validLifetime := ?,
			preferredLifetime := ?,
			reserved2 := ?,
			prefix := prefix
		}
	}

	/* template for receiving/matching an ICMPv6 router advertisement */
	template PDU_ICMPv6 tr_ICMPv6_RA(template OCT16 prefix, template INT1 prefix_len) := {
		routerAdvertisement := {
			typeField := 134,
			code := 0,
			checksum := ?,
			curHopLimit := ?,
			reserved := ?,
			o_Bit := '0'B,
			m_Bit := '0'B,
			routerLifetime := ?,
			reachableTime := ?,
			retransTimer := ?,
			options := {
				tr_ICMP6_OptPrefix(prefix, prefix_len)
			}
		}
	}

	/* template to construct IPv6_packet from input arguments, ready for use in f_IPv6_enc() */
	template IPv6_packet ts_IP6(OCT16 srcaddr, OCT16 dstaddr, LIN1 nexthead, octetstring payload, LIN1 hlim := 255) := {
		header := {
			ver := 6,
			trclass := 0,
			flabel := 0,
			plen := 0,
			nexthead := nexthead,
			hlim := hlim,
			srcaddr := srcaddr,
			dstaddr := dstaddr
		},
		ext_headers := omit,
		payload := payload
	}

	function f_ipv6_link_local(in OCT16 link_id) return OCT16 {
		 return 'FE80000000000000'O & substr(link_id, 8, 8);
	}

	/* Compute solicited-node multicast address as per RFC4291 2.7.1 */
	function f_ipv6_sol_node_mcast(in OCT16 addr) return OCT16 {
		return 'FF0200000000000000000001FF'O & substr(addr, 13, 3);
	}

	/* generate and encode ICMPv6 router solicitation */
	function f_gen_icmpv6_router_solicitation(in OCT16 link_id) return octetstring {
		const OCT16 c_ip6_all_router_mcast := 'FF020000000000000000000000000002'O;
		var OCT16 saddr := f_ipv6_link_local(link_id);

		var octetstring tmp;
		tmp := f_enc_PDU_ICMPv6(valueof(ts_ICMPv6_RS), saddr, c_ip6_all_router_mcast);
		var IPv6_packet ip6 := valueof(ts_IP6(saddr, c_ip6_all_router_mcast, 58, tmp));

		return f_IPv6_enc(ip6);
	}

	/* create ICMPv6 router solicitation deriving link-id from PDP Context EUA */
	function f_icmpv6_rs_for_pdp(in PdpContext ctx) return octetstring {
		var OCT16 interface_id := ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address;
		return f_gen_icmpv6_router_solicitation(interface_id);
	}

	/* generate and encode ICMPv6 neighbor solicitation */
	function f_gen_icmpv6_neigh_solicit(in OCT16 saddr, in OCT16 daddr, in OCT16 tgt_addr) return octetstring {
		var octetstring tmp;
		tmp := f_enc_PDU_ICMPv6(valueof(ts_ICMPv6_NS(tgt_addr)), saddr, daddr);
		var IPv6_packet ip6 := valueof(ts_IP6(saddr, daddr, 58, tmp));
		return f_IPv6_enc(ip6);
	}

	/* generate and encode ICMPv6 neighbor solicitation for PDP Context */
	function f_gen_icmpv6_neigh_solicit_for_pdp(in PdpContext ctx) return octetstring {
		var OCT16 interface_id := ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address;
		var OCT16 link_local := f_ipv6_link_local(interface_id);
		var OCT16 daddr := f_ipv6_sol_node_mcast(link_local);

		return f_gen_icmpv6_neigh_solicit(link_local, daddr, link_local);
	}

	/* wait for GGSN to send us an ICMPv6 router advertisement */
	function f_wait_rtr_adv(PdpContext ctx) runs on GT_CT {
		var Gtp1uUnitdata ud;
		T_default.start;
		alt {
			//'6???????????3aff'O
			[] GTPU.receive(tr_GTPU_GPDU(g_peer_u, ?)) -> value ud {
				var octetstring gpdu := ud.gtpu.gtpu_IEs.g_PDU_IEs.data;
				var IPv6_packet ip6 := f_IPv6_dec(gpdu);
				if (ip6.header.ver != 6 or ip6.header.nexthead != 58 or ip6.header.hlim != 255) {
					repeat;
				}
				var PDU_ICMPv6 icmp6 := f_dec_PDU_ICMPv6(ip6.payload);
				if (not match(icmp6, tr_ICMPv6_RA(?, 64))) {
					repeat;
				}
				ctx.ip6_prefix := icmp6.routerAdvertisement.options[0].prefixInformation.prefix;
				log("RA with /64 prefix ", ctx.ip6_prefix);
			}
			[] GTPU.receive(tr_GTPU_GPDU(?, ?)) { repeat; }
			[] GTPU.receive { setverdict(fail); }
			[] T_default.timeout { setverdict(fail); }
		}
		T_default.stop;
	}

	testcase TC_pdp6_act_deact() runs on GT_CT {
		f_init();

		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv6Dyn)));
		f_pdp_ctx_act(ctx);
		f_pdp_ctx_del(ctx, '1'B);
	}

	testcase TC_pdp6_act_deact_pcodns() runs on GT_CT {
		f_init();

		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv6Dyn)));
		ctx.pco_req := valueof(ts_PCO_IPv6_DNS);
		f_pdp_ctx_act(ctx);
		f_pdp_ctx_del(ctx, '1'B);
	}

	testcase TC_pdp6_act_deact_icmp6() runs on GT_CT {
		f_init();

		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv6Dyn)));
		ctx.pco_req := valueof(ts_PCO_IPv6_DNS);
		f_pdp_ctx_act(ctx);

		//f_send_gtpu(ctx, c_router_solicit);
		//f_send_gtpu(ctx, c_neigh_solicit);

		f_send_gtpu(ctx, f_icmpv6_rs_for_pdp(ctx));
		f_wait_rtr_adv(ctx);
		f_send_gtpu(ctx, f_gen_icmpv6_neigh_solicit_for_pdp(ctx));

		f_pdp_ctx_del(ctx, '1'B);
	}

	testcase TC_pdp4_act_deact() runs on GT_CT {
		f_init();
		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv4Dyn)));
		f_pdp_ctx_act(ctx);
		f_pdp_ctx_del(ctx, '1'B);
	}

	testcase TC_pdp4_act_deact_ipcp() runs on GT_CT {
		f_init();
		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv4Dyn)));
		f_pdp_ctx_act(ctx);
		f_pdp_ctx_del(ctx, '1'B);
	}

	testcase TC_pdp4_act_deact_pcodns() runs on GT_CT {
		f_init();
		var PdpContext ctx := valueof(t_DefinePDP('262420123456789'H, '1234'O, valueof(t_ApnInternet), valueof(t_EuaIPv4Dyn)));
		f_pdp_ctx_act(ctx);
		f_pdp_ctx_del(ctx, '1'B);
	}

	control {
		execute(TC_pdp4_act_deact());
		execute(TC_pdp4_act_deact_ipcp());
		execute(TC_pdp4_act_deact_pcodns());

		execute(TC_pdp6_act_deact());
		execute(TC_pdp6_act_deact_pcodns());
		execute(TC_pdp6_act_deact_icmp6());
	}
}