aboutsummaryrefslogtreecommitdiffstats
path: root/library/GTP_Templates.ttcn
blob: 493da8f5c0c9f15cea2e5c4a7e5500655759de0b (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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
/* GTP Templates in TTCN-3
 * (C) 2018 Harald Welte <laforge@gnumonks.org>
 * contributions by sysmocom - s.f.m.c. GmbH
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

module GTP_Templates {

	import from General_Types all;
	import from Osmocom_Types all;
	import from GTPC_Types all;
	import from GTPU_Types all;
	import from GTP_CodecPort all;
	import from IPCP_Types all;
	import from GSM_Types all; // RoutingAreaIdentification, CellIdentity

	type record GTP_CellId {
		RoutingAreaIdentification	ra_id,
		CellIdentity			cell_id
	} with { encode "RAW" };

	template (value) GTP_CellId ts_GTP_CellId(template (value) RoutingAreaIdentification rai, CellIdentity cell_id) := {
		ra_id := rai,
		cell_id := cell_id
	};

	/* Table 38 of 3GPP TS 29.060 */
	type enumerated GTP_Cause {
		GTP_CAUSE_REQUEST_IMEI			(1),
		GTP_CAUSE_REQUEST_IMSI_AND_IMEI		(2),
		GTP_CAUSE_NO_IDENTITY_NEDED		(3),
		GTP_CAUSE_MS_REFUSES			(4),
		GTP_CAUSE_MS_IS_NOT_GPRS_RESPONDING	(5),
		/* reserved */
		GTP_CAUSE_REQUEST_ACCEPTED		(128)
		/* FIXME */
	};

	private function f_oct_or_wc(template integer inp, integer len) return template octetstring {
		if (istemplatekind(inp, "omit")) {
			return omit;
		} else if (istemplatekind(inp, "*")) {
			return *;
		} else if (istemplatekind(inp, "?")) {
			return ?;
		}
		return int2oct(valueof(inp), len);
	}

	private function f_hex_or_wc(template integer inp, integer len) return template hexstring {
		if (istemplatekind(inp, "omit")) {
			return omit;
		} else if (istemplatekind(inp, "*")) {
			return *;
		} else if (istemplatekind(inp, "?")) {
			return ?;
		}
		return int2hex(valueof(inp), len);
	}

	/* generalized GTP-C receive template */
	template PDU_GTPC tr_GTP1C_PDU(template OCT1 msg_type, template OCT4 teid, template GTPC_PDUs pdu := ?) := {
		/* 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 := 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 := '0E'O,
		restartCounter := restart_counter
	}

	template Recovery_gtpc tr_Recovery(template OCT1 restart_counter) := {
		type_gtpc := '0E'O,
		restartCounter := restart_counter
	}

	/* IMEI(SV) IE TS 29.060 7.7.53 */
	template IMEISV_gtpc ts_IMEISV(template (value) OCT8 imeisv) := {
		type_gtpc := '9A'O,
		lengthf := 8,
		imeisv := imeisv
	}
	private function f_ts_IMEISV(template (omit) OCT8 imeisv)
	return template (omit) IMEISV_gtpc {
		if (istemplatekind(imeisv, "omit")) {
			return omit;
		}
		return ts_IMEISV(imeisv);
	}

	template IMEISV_gtpc tr_IMEISV(template (present) OCT8 imeisv) := {
		type_gtpc := '9A'O,
		lengthf := 8,
		imeisv := imeisv
	}

	// MS Time Zone  -  7.7.52
	template (value) MS_TimeZone ts_MS_TimeZone(template (value) OCT1 timeZone := '00'O,
						    template (value) BIT2 daylightSavingTime := '00'B) := {
		type_gtpc := '99'O,
		lengthf := 2,
		timeZone := timeZone,
		daylightSavingTime := daylightSavingTime,
		spare1 := '000'B,
		sgsnAttempsToUpdateMS := '0'B, /* propietary, use it as spare */
		spare2 := '00'B
	}
	function f_ts_MS_TimeZone(template (omit) OCT1 timeZone, template (omit) BIT2 daylightSavingTime)
	return template (omit) MS_TimeZone {
		if (istemplatekind(timeZone, "omit") and istemplatekind(daylightSavingTime, "omit")) {
			return omit;
		}
		if (istemplatekind(timeZone, "omit")) {
			return ts_MS_TimeZone(daylightSavingTime := daylightSavingTime);
		}
		if (istemplatekind(daylightSavingTime, "omit")) {
			return ts_MS_TimeZone(timeZone);
		}
		return ts_MS_TimeZone(timeZone, daylightSavingTime);
	}

	template (present) MS_TimeZone tr_MS_TimeZone(template (present) OCT1 timeZone := ?,
						      template (present) BIT2 daylightSavingTime := ?) := {
		type_gtpc := '99'O,
		lengthf := 2,
		timeZone := timeZone,
		daylightSavingTime := daylightSavingTime,
		spare1 := '000'B,
		sgsnAttempsToUpdateMS := '0'B, /* propietary, use it as spare */
		spare2 := '00'B
	}

	/* Charging Characteristics IE TS 29.060 7.7.23 */
	template ChargingCharacteristics_GTPC ts_ChargingCharacteristics(template (value) OCT2 chargingChar) := {
		type_gtpc := '1A'O,
		chargingChar := chargingChar
	}
	private function f_ts_ChargingCharacteristics(template (omit) OCT2 chargingChar)
		return template (omit) ChargingCharacteristics_GTPC {
		if (istemplatekind(chargingChar, "omit")) {
			return omit;
		}
		return ts_ChargingCharacteristics(chargingChar);
	}

	template ChargingCharacteristics_GTPC tr_ChargingCharacteristics(template (present) OCT2 chargingChar) := {
		type_gtpc := '1A'O,
		chargingChar := chargingChar
	}


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

	/* 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 tr_EchoRespPDU(template OCT1 restart_counter) := {
		echoResponse := {
			recovery := tr_Recovery(restart_counter),
			private_extension_gtpc := *
		}
	}

	/* template matching reception of GTP-C echo-response */
	template Gtp1cUnitdata tr_GTPC_PONG(template GtpPeer peer) := tr_GTPC_MsgType(peer, echoResponse, '00000000'O, tr_EchoRespPDU(?));

	template GTPC_PDUs ts_EchoRespPDU(OCT1 restart_counter) := {
		echoResponse := {
			recovery := ts_Recovery(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 GTPC_PDUs ts_EchoReqPDU := {
		echoRequest := {
			private_extension_gtpc := omit
		}
	}

	/* master template for sending a GTP-C echo request */
	template Gtp1cUnitdata ts_GTPC_PING(GtpPeer peer, uint16_t seq) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(echoRequest, '00000000'O, valueof(ts_EchoReqPDU), seq)
	}

	private function f_eua_ipv4_len(template OCT4 ip_addr) return template integer {
		if (istemplatekind(ip_addr, "omit")) {
			return 2;
		} else if (istemplatekind(ip_addr, "*")) {
			return ?;
		} else if (istemplatekind(ip_addr, "?")) {
			return 6;
		}
		return 6;
	}

	private function f_eua_ipv6_len(template OCT16 ip_addr) return template integer {
		if (istemplatekind(ip_addr, "omit")) {
			return 2;
		} else if (istemplatekind(ip_addr, "*")) {
			return ?;
		} else if (istemplatekind(ip_addr, "?")) {
			return 18;
		}
		return 18;
	}

	private function f_eua_ipv4v6_len(template OCT4 ip_addr4, template OCT16 ip_addr6) return template integer {
		var integer len := 2;
		if (istemplatekind(ip_addr4, "*") or
		    istemplatekind(ip_addr6, "*")) {
			return ?;
		}
		if (not istemplatekind(ip_addr4, "omit")) {
			len := len + 4;
		}
		if (not istemplatekind(ip_addr6, "omit")) {
			len := len + 16;
		}
		return len;
	}

	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 tr_EuaIPv4(template OCT4 ip_addr) modifies t_EuaIPv4 := {
		endUserAddress := {
			endUserAddressIPv4 := {
				lengthf := f_eua_ipv4_len(ip_addr)
			}
		}
	}

	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 EndUserAddress tr_EuaIPv6(template OCT16 ip_addr) modifies t_EuaIPv6 := {
		endUserAddress := {
			endUserAddressIPv6 := {
				lengthf := f_eua_ipv6_len(ip_addr)
			}
		}
	}

	/* 3GPP TS 29.060 Figure 37A: End User Address Information Element for IPv4v6 (both static) */
	template EndUserAddress t_EuaIPv4v6(template OCT4 ip_addr4, template OCT16 ip_addr6) := {
		type_gtpc := '80'O,
		endUserAddress := {
			endUserAddressIPv4andIPv6 := {
				lengthf := 2,
				pdp_typeorg := '0001'B,
				spare := '1111'B,
				pdp_typenum := '8D'O,
				ipv4_address := ip_addr4,
				ipv6_address := ip_addr6
			}
		}
	}
	template EndUserAddress t_EuaIPv4Dynv6Dyn := t_EuaIPv4v6(omit, omit);
	template EndUserAddress tr_EuaIPv4v6(template OCT4 ip_addr4, template OCT16 ip_addr6) modifies t_EuaIPv4v6 := {
		endUserAddress := {
			endUserAddressIPv4andIPv6 := {
				lengthf := f_eua_ipv4v6_len(ip_addr4, ip_addr6)
			}
		}
	}

	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
	}

	function f_ts_RATType(template (omit) OCT1 ratType := omit) return template (omit) RATType {
		var template (omit) RATType rt;
		if (istemplatekind(ratType, "omit")) {
			rt := omit;
		} else {
			rt := {
				type_gtpc := '97'O,
				lengthf := 1,
				ratTypeValue := ratType
			};
		}
		return rt;
	}

	template (value) GeographicLocationCGI
	ts_GeographicLocationCGI(template (value) hexstring mcc,
				template (value) hexstring mnc,
				template (value) OCT2 lac,
				template (value) OCT2 cI_value) :=
	{
		mccDigit1 := mcc[0],
		mccDigit2 := mcc[1],
		mccDigit3 := mcc[2],
		mncDigit3 := mnc[2], /* 'F'H for 2 digit MNC */
		mncDigit1 := mnc[0],
		mncDigit2 := mnc[1],
		lac := lac,
		cI_value := cI_value
	}

	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,
					   template (omit) OCT1 ratType := omit,
					   template (omit) UserLocationInformation uli := omit,
					   template (omit) OCT2 charging_char := omit,
					   template (omit) OCT8 imeisv := omit,
					   template(omit) MS_TimeZone ms_tz := 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 := f_ts_ChargingCharacteristics(charging_char),
			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 := f_ts_RATType(ratType),
			userLocationInformation := uli,
			mS_TimeZone := ms_tz,
			imeisv := f_ts_IMEISV(imeisv),
			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,
						 template (omit) OCT1 ratType := omit,
						 template (omit) UserLocationInformation uli := omit,
						 template (omit) OCT2 charging_char := omit,
						 template (omit) OCT8 imeisv := omit,
						 template(omit) MS_TimeZone ms_tz := 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, ratType, uli,
								charging_char, imeisv, ms_tz)), seq)
	}


	template GTPC_PDUs ts_UpdatePdpPDU(hexstring imsi, OCT1 restart_ctr, OCT4 teid_data, OCT4 teid_ctrl,
					   BIT4 nsapi,
					   octetstring sgsn_ip_sign, octetstring sgsn_ip_data,
					   template ProtConfigOptions pco := omit,
					   template (omit) OCT1 ratType := omit,
					   template (omit) UserLocationInformation uli := omit) := {
		updatePDPContextRequest := {
			updatePDPContextRequestSGSN := {
				imsi := ts_Imsi(imsi),
				rai := omit,
				recovery := ts_Recovery(restart_ctr),
				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
				},
				trace_ref := omit,
				trace_type := omit,
				protConfigOptions := pco,
				sgsn_addr_controlPlane := ts_GsnAddr(sgsn_ip_sign),
				sgsn_addr_traffic := ts_GsnAddr(sgsn_ip_data),
				alt_ggsn_addr_controlPane := omit,
				alt_ggsn_addr_traffic := omit,
				qualityOfServiceProfile := ts_QosDefault,
				tft := omit,
				triggerId := omit,
				omcId := omit,
				commonFlags := omit,
				ratType := f_ts_RATType(ratType),
				userLocationInformation := uli,
				mS_TimeZone := omit,
				additionalTraceInfo := omit,
				directTunnelFlags := omit,
				evolvedAllocationRetentionPriorityI := omit,
				extendedCommonFlags := omit,
				userCSGInformation := omit,
				aPN_AMBR := omit,
				signallingPriorityIndication := omit,
				cN_OperatorSelectionEntity := omit,
				private_extension_gtpc := omit
			}
		}
	}

	template Gtp1cUnitdata ts_GTPC_UpdatePDP(GtpPeer peer, OCT4 teid, uint16_t seq, hexstring imsi,
						 OCT1 restart_ctr, OCT4 teid_data,
						 OCT4 teid_ctrl, BIT4 nsapi, octetstring sgsn_ip_sign,
						 octetstring sgsn_ip_data,
						 template ProtConfigOptions pco := omit,
						 template (omit) OCT1 ratType := omit,
						 template (omit) UserLocationInformation uli := omit) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(updatePDPContextRequest, teid,
					valueof(ts_UpdatePdpPDU(imsi, restart_ctr, teid_data, teid_ctrl,
								nsapi, sgsn_ip_sign,
								sgsn_ip_data, pco, ratType, uli)), seq)
	}


	template NSAPI_GTPC ts_NSAPI(BIT4 nsapi) := {
		type_gtpc := '14'O,
		nsapi := nsapi,
		unused := '0000'B
	}

	template ReorderingRequired ts_ReorderReq(boolean req := false) := {
		type_gtpc := '08'O,
		reordreq := bool2bit(req),
		spare := '0000000'B
	}

	template GTPC_PDUs ts_CreatePdpRespPDU(OCT1 cause, OCT4 teid_data, OCT4 teid_ctrl, BIT4 nsapi,
						octetstring ggsn_ip_sign, octetstring ggsn_ip_data,
						OCT4 chg_id, template EndUserAddress eua := omit,
						template Recovery_gtpc recovery := omit,
						template ProtConfigOptions pco := omit) := {
		createPDPContextResponse := {
			cause := { '00'O, cause },
			reorderingRequired := ts_ReorderReq(false),
			recovery := recovery,
			teidDataI := {
				type_gtpc := '00'O,
				teidDataI := teid_data
			},
			teidControlPlane := {
				type_gtpc := '00'O,
				teidControlPlane := teid_ctrl
			},
			nsapi := ts_NSAPI(nsapi),
			chargingID := {
				type_gtpc := '7F'O,
				chargingID := chg_id
			},
			endUserAddress := eua,
			protConfigOptions := pco,
			ggsn_addr_controlPlane := ts_GsnAddr(ggsn_ip_sign),
			ggsn_addr_traffic := ts_GsnAddr(ggsn_ip_data),
			alt_ggsn_addr_controlPane := omit,
			alt_ggsn_addr_traffic := omit,
			qualityOfServiceProfile := ts_QosDefault,
			commonFlags := omit,
			aPN_Restriction := omit,
			mS_InfoChangeReportingAction := omit,
			bearerControlMode := omit,
			evolvedAllocationRetentionPriorityI := omit,
			extendedCommonFlag := omit,
			csg_information_reporting_action := omit,
			aPN_AMBR := omit,
			gGSN_BackOffTime := omit,
			private_extension_gtpc := omit
		}
	}

	template Gtp1cUnitdata ts_GTPC_CreatePdpResp(GtpPeer peer, uint16_t seq, OCT4 teid,
						 OCT1 cause,
						 OCT4 teid_ctrl, OCT4 teid_data,
						 BIT4 nsapi, octetstring ggsn_ip_sign,
						 octetstring ggsn_ip_data, OCT4 chg_id,
						 template EndUserAddress eua := omit,
						 template Recovery_gtpc recovery := omit,
						 template ProtConfigOptions pco := omit) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(createPDPContextResponse, teid,
					valueof(ts_CreatePdpRespPDU(cause, teid_data, teid_ctrl, nsapi,
								ggsn_ip_sign, ggsn_ip_data, chg_id,
								eua, recovery, pco)), seq)
	}

	/* PCO send base template */
	template ProtConfigOptions ts_PCO := {
		type_gtpc := '84'O,
		lengthf := 0,
		configProtocol := '000'B,
		spare := '0000'B,
		extension0 := '1'B,
		protocols := {}
	}
	/* PCO receive base template */
	template ProtConfigOptions tr_PCO := {
		type_gtpc := '84'O,
		lengthf := ?,
		configProtocol := '000'B,
		spare := ?,
		extension0 := '1'B,
		protocols := {}
	}

	template ProtConfigOptions ts_PCO_IPv6_DNS modifies ts_PCO := {
		protocols := {
			{ protocolID := '0003'O, lengthProtoID := 0, protoIDContents := ''O }
		}
	}
	template ProtConfigOptions tr_PCO_IPv6_DNS_resp(template OCT16 contents) modifies tr_PCO := {
		protocols := {
			*, { protocolID := '0003'O, lengthProtoID := 16, protoIDContents := contents }, *
		}
	}

	template ProtConfigOptions ts_PCO_IPv4_DNS_IPCP modifies ts_PCO := {
		protocols := {
			/* dummy PAP entry to check if our parser in the GGSN can properly iterate over
			 * the list of protocols, see Change-Id Icc2e6716c33d78d3c3e000f529806228d8aa155e */
			{ protocolID := 'C023'O, lengthProtoID := 0, protoIDContents := ''O },
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
								enc_IpcpPacket(valueof(ts_IPCP_ReqDNS)) }
		}
	}

	template ProtConfigOptions ts_PCO_IPv4_PRI_DNS_IPCP modifies ts_PCO := {
		protocols := {
			/* dummy PAP entry to check if our parser can cope with a single primary DNS entry
			 * see Change-Id Icffde89f9bc5d8fcadf6e2dd6c0b4de03440edd5 and OS#3288 */
			{ protocolID := 'C023'O, lengthProtoID := 0, protoIDContents := ''O },
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
								enc_IpcpPacket(valueof(ts_IPCP_ReqDNS_Primary)) }
		}
	}
	template ProtConfigOptions ts_PCO_IPv4_SEC_DNS_IPCP modifies ts_PCO := {
		protocols := {
			/* dummy PAP entry to check if our parser can cope with a single secondary DNS entry
			 * see Change-Id Icffde89f9bc5d8fcadf6e2dd6c0b4de03440edd5 and OS#3288 */
			{ protocolID := 'C023'O, lengthProtoID := 0, protoIDContents := ''O },
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
								enc_IpcpPacket(valueof(ts_IPCP_ReqDNS_Secondary)) }
		}
	}
	template ProtConfigOptions ts_PCO_IPv4_SEPARATE_DNS_IPCP modifies ts_PCO := {
		protocols := {
			/* dummy PAP entry to check if our parser can cope with a primary and secondary DNS
			 * in separate IPCP containers OS#3381 */
			{ protocolID := 'C023'O, lengthProtoID := 0, protoIDContents := ''O },
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
								enc_IpcpPacket(valueof(ts_IPCP_ReqDNS_Primary)) },
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
								enc_IpcpPacket(valueof(ts_IPCP_ReqDNS_Secondary)) }
		}
	}

	template ProtocolElement tr_PCO_Proto(OCT2 prot_id) := {
		protocolID := prot_id,
		lengthProtoID := ?,
		protoIDContents := ?
	}
	template ProtocolElement ts_PCOelem_PAP_broken := {
		protocolID := 'C023'O,
		lengthProtoID := 60,
		/* PPP Password Authentication Protocol containing incorrect Peer-Id-Length set to 4 (6-7 should be the valid one), see OS#3914. */
		protoIDContents := '0100003c'O & '0444435338323700bc1c08087c1508083e00790000150808fd06000001000000000000000000000000000000000000000000000000000000'O
	}
	template ProtConfigOptions ts_PCO_PAP_IPv4_DNS modifies ts_PCO := {
		protocols := {
			ts_PCOelem_PAP_broken,
			{ protocolID := '8021'O, lengthProtoID := 16, protoIDContents := enc_IpcpPacket(valueof(ts_IPCP_ReqDNS)) }
		}
	}
	template ProtConfigOptions tr_PCO_Contains(OCT2 prot_id) modifies tr_PCO := {
		protocols := { *, tr_PCO_Proto(prot_id), * }
	}

	template ProtConfigOptions ts_PCO_IPv4_DNS_CONT modifies ts_PCO := {
		protocols := {
			{ protocolID := '000d'O, lengthProtoID := 0, protoIDContents := ''O }
		}
	}
	template ProtConfigOptions tr_PCO_IPv4_DNS_CONT_resp(template OCT4 contents) modifies tr_PCO := {
		protocols := {
			*, { protocolID := '000d'O, lengthProtoID := 4, protoIDContents := contents }, *
		}
	}

	/* extract a given protocol payload from PCO */
	function f_PCO_extract_proto(ProtConfigOptions pco, OCT2 protocol, integer nth_match := 1) return octetstring {
		var integer i;
		var integer num_matches := 0;
		for (i := 0; i < lengthof(pco.protocols); i := i + 1) {
			if (pco.protocols[i].protocolID == protocol) {
				num_matches := num_matches + 1;
				if (num_matches == nth_match) {
					return pco.protocols[i].protoIDContents;
				}
			}
		}
		setverdict(fail, "Could not extract protocol payload from protocol ", protocol);
		mtc.stop;
		return ''O;
	}

	template IpcpPacket tr_IPCP(template LcpCode code, template uint8_t identifier,
				    template IpcpOptionList opts) := {
		code := code,
		identifier := identifier,
		len := ?,
		options := opts
	}
	template IpcpOption tr_IPCP_PrimaryDns(template OCT4 addr) := {
		code := IPCP_OPT_PrimaryDNS,
		len := 6,
		data := addr
	}
	template IpcpOption tr_IPCP_SecondaryDns(template OCT4 addr) := {
		code := IPCP_OPT_SecondaryDNS,
		len := 6,
		data := addr
	}
	template IpcpPacket tr_IPCP_Ack_DNS(template uint8_t identifier := ?, template OCT4 dns1 := ?,
					    template OCT4 dns2 := ?) :=
		tr_IPCP(LCP_Configure_Ack, identifier,
				{ *, tr_IPCP_PrimaryDns(dns1), *, tr_IPCP_SecondaryDns(dns2), * });

	template IpcpPacket ts_IPCP(LcpCode code, uint8_t identifier, template IpcpOptionList opts) := {
		code := code,
		identifier := identifier,
		len := 0,	/* overwritten */
		options := opts
	}
	template IpcpPacket ts_IPCP_ReqDNS(uint8_t identifier := 0) :=
		ts_IPCP(LCP_Configure_Request, identifier,
			{ tr_IPCP_PrimaryDns('00000000'O), tr_IPCP_SecondaryDns('00000000'O) });

	template IpcpPacket ts_IPCP_ReqDNS_Primary(uint8_t identifier := 0) :=
		ts_IPCP(LCP_Configure_Request, identifier,
			{ tr_IPCP_PrimaryDns('00000000'O) });
	template IpcpPacket ts_IPCP_ReqDNS_Secondary(uint8_t identifier := 0) :=
		ts_IPCP(LCP_Configure_Request, identifier,
			{ tr_IPCP_SecondaryDns('00000000'O) });

	function f_teardown_ind_IE(in template (omit) BIT1 ind) return template (omit) TearDownInd {
		if (istemplatekind(ind, "omit")) {
			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 (omit) 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 (omit) BIT1 teardown_ind) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(deletePDPContextRequest, teid,
					valueof(ts_DeletePdpPDU(nsapi, teardown_ind)), seq)
	}

	template GTPC_PDUs ts_DeletePdpRespPDU(OCT1 cause,
						template ProtConfigOptions pco := omit) := {
		deletePDPContextResponse := {
			cause := { '00'O, cause },
			protConfigOptions := pco,
			userLocationInformation := omit,
			mS_TimeZone := omit,
			uLI_Timestamp := omit,
			private_extension_gtpc := omit
		}
	}

	template Gtp1cUnitdata ts_GTPC_DeletePdpResp(GtpPeer peer, uint16_t seq, OCT4 teid,
						OCT1 cause,
						template ProtConfigOptions pco := omit) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(deletePDPContextResponse, teid,
					valueof(ts_DeletePdpRespPDU(cause, pco)), seq)
	}


	/* GTP-C RIM */

	template (value) RIM_Application_Identity_GTPC ts_GTPC_RIM_Application_Identity(OCT1 app_id) := {
		 iEI := '4B'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := 1
		 },
		 rIMApplicationIdentity := app_id
	}
	/* 3GPP TS 48.018 11.3.62 */
	template (value) RIM_Sequence_Number_GTPC ts_GTPC_RIM_Sequence_Number(integer seq) := {
		 iEI := '4C'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := 4
		 },
		 rIMSequenceNumber := int2oct(seq, 4)
	}
	template (value) RIM_PDU_Indications_GTPC ts_GTPC_RIM_PDU_Indications(boolean ack, BIT3 type_ext) := {
		 iEI := '4F'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := 1
		 },
		 ack := bool2bit(ack),
		 pDU_Type_Extension := type_ext,
		 reserved := '0000'B
	}
	/* 3GPP TS 48.018 11.3.67 */
	template (value) RIM_Protocol_Version_Number_GTPC ts_GTPC_RIM_Protocol_Version_Number(integer ver) := {
		 iEI := '55'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := 1
		 },
		 rIMProtocolVersionNumber := int2oct(ver, 1)
	}
	function tr_GTPC_Cell_Identifier_V(template GTP_CellId cid) return template Cell_Identifier_V_GTPC {
		var template Cell_Identifier_V_GTPC ret := {
			mccDigit1 := ?,
			mccDigit2 := ?,
			mccDigit3 := ?,
			mncDigit3 := ?,
			mncDigit1 := ?,
			mncDigit2 := ?,
			lac := ?,
			rac := ?,
			cI_value := ?
		}
		if (istemplatekind(cid, "omit")) {
			return omit;
		} else if (istemplatekind(cid, "*")) {
			return *;
		} else if (istemplatekind(cid, "?")) {
			return ?;
		}
		if (isvalue(cid) and isvalue(cid.ra_id) and isvalue(cid.ra_id.lai)) {
			if (isvalue(cid.ra_id.lai.mcc_mnc)) {
				ret.mccDigit1 := cid.ra_id.lai.mcc_mnc[0];
				ret.mccDigit2 := cid.ra_id.lai.mcc_mnc[1];
				ret.mccDigit3 := cid.ra_id.lai.mcc_mnc[2];
				ret.mncDigit3 := cid.ra_id.lai.mcc_mnc[3];
				ret.mncDigit1 := cid.ra_id.lai.mcc_mnc[4];
				ret.mncDigit2 := cid.ra_id.lai.mcc_mnc[5];
			}
			if (isvalue(cid.ra_id.lai.lac)) {
				ret.lac := f_oct_or_wc(cid.ra_id.lai.lac, 2);
			}
		}
		if (isvalue(cid) and isvalue(cid.ra_id)) {
			ret.rac := f_oct_or_wc(cid.ra_id.rac, 1);
		}
		if (isvalue(cid)) {
			ret.cI_value := f_oct_or_wc(cid.cell_id, 2);
		}
		return ret;
	}
	template (value) Cell_Identifier_V_GTPC ts_GTPC_Cell_Identifier_V(GTP_CellId cid) := {
		mccDigit1 := cid.ra_id.lai.mcc_mnc[0],
		mccDigit2 := cid.ra_id.lai.mcc_mnc[1],
		mccDigit3 := cid.ra_id.lai.mcc_mnc[2],
		mncDigit3 := cid.ra_id.lai.mcc_mnc[3],
		mncDigit1 := cid.ra_id.lai.mcc_mnc[4],
		mncDigit2 := cid.ra_id.lai.mcc_mnc[5],
		lac := int2oct(cid.ra_id.lai.lac, 2),
		rac := int2oct(cid.ra_id.rac, 1),
		cI_value := int2oct(cid.cell_id, 2)
	}
	template RIM_Routing_Address_GTPC t_GTPC_RIM_Routing_Address_cid(GTP_CellId cid) := {
		 cell_Identifier := ts_GTPC_Cell_Identifier_V(cid)
	}
	function tr_GTPC_ENB_Identifier(template GTP_CellId cid, template integer tac, template octetstring gnbid) return template ENB_Identifier {
		var template ENB_Identifier ret := {
			mccDigit1 := ?,
			mccDigit2 := ?,
			mccDigit3 := ?,
			mncDigit3 := ?,
			mncDigit1 := ?,
			mncDigit2 := ?,
			tAC := ?,
			globalENB_ID := ?
		}
		if (istemplatekind(cid, "omit") and istemplatekind(tac, "omit") and istemplatekind(gnbid, "omit")) {
			return omit;
		} else if (istemplatekind(cid, "*") and istemplatekind(tac, "*") and istemplatekind(gnbid, "*")) {
			return *;
		} else if (istemplatekind(cid, "?") and istemplatekind(tac, "?") and istemplatekind(gnbid, "?")) {
			return ?;
		}
		if (isvalue(cid) and isvalue(cid.ra_id) and isvalue(cid.ra_id.lai)) {
			if (isvalue(cid.ra_id.lai.mcc_mnc)) {
				ret.mccDigit1 := cid.ra_id.lai.mcc_mnc[0];
				ret.mccDigit2 := cid.ra_id.lai.mcc_mnc[1];
				ret.mccDigit3 := cid.ra_id.lai.mcc_mnc[2];
				ret.mncDigit3 := cid.ra_id.lai.mcc_mnc[3];
				ret.mncDigit1 := cid.ra_id.lai.mcc_mnc[4];
				ret.mncDigit2 := cid.ra_id.lai.mcc_mnc[5];
			}
		}
		if (isvalue(tac)) {
			ret.tAC := int2oct(valueof(tac), 2);
		}
		if (isvalue(gnbid)) {
			ret.globalENB_ID := gnbid;
		}

		return ret;
	}
	template (value) ENB_Identifier ts_GTPC_ENB_Identifier(GTP_CellId cid, integer tac, octetstring gnbid) := {
		mccDigit1 := cid.ra_id.lai.mcc_mnc[0],
		mccDigit2 := cid.ra_id.lai.mcc_mnc[1],
		mccDigit3 := cid.ra_id.lai.mcc_mnc[2],
		mncDigit3 := cid.ra_id.lai.mcc_mnc[3],
		mncDigit1 := cid.ra_id.lai.mcc_mnc[4],
		mncDigit2 := cid.ra_id.lai.mcc_mnc[5],
		tAC := int2oct(tac, 2),
		globalENB_ID := gnbid
	}
	template RIM_Routing_Address_GTPC t_GTPC_RIM_Routing_Address_enbid(GTP_CellId cid, integer tac, octetstring gnbid) := {
		 eNB_Identifier := ts_GTPC_ENB_Identifier(cid, tac, gnbid)
	}
	template RIM_Routing_Information_GTPC
	tr_GTPC_RIM_Routing_Information(HEX1 addr_discr, template RIM_Routing_Address_GTPC addr) := {
		iEI := '54'O,
		ext := '1'B,
			lengthIndicator := {
				length1 := ?
		},
		rIMRoutingAddressDiscriminator := addr_discr,
		spare := '0'H,
		rIM_Routing_Address := addr
	}
	template (value) RIM_Routing_Information_GTPC
	ts_GTPC_RIM_Routing_Information(HEX1 addr_discr, template (value) RIM_Routing_Address_GTPC addr) := {
		iEI := '54'O,
		ext := '1'B,
			lengthIndicator := {
				length1 := 0 /* overwritten */
		},
		rIMRoutingAddressDiscriminator := addr_discr,
		spare := '0'H,
		rIM_Routing_Address := addr
	}
	/* 3GPP TS 48.018 11.3.63.1.1 */
	template RAN_Information_Request_Application_Container_NACC_GTPC
	tr_GTPC_RAN_Information_Request_Application_Container_NACC(template GTP_CellId cid) := {
		iEI := '4D'O,
		ext := '1'B,
		lengthIndicator := {
			length1 := ?
		},
		reporting_Cell_Identifier := tr_GTPC_Cell_Identifier_V(cid)
	}
	template (value) RAN_Information_Request_Application_Container_NACC_GTPC
	ts_GTPC_RAN_Information_Request_Application_Container_NACC(GTP_CellId cid) := {
		iEI := '4D'O,
		ext := '1'B,
		lengthIndicator := {
			length1 := 0 /* overwritten */
		},
		reporting_Cell_Identifier := ts_GTPC_Cell_Identifier_V(cid)
	}
	/* 3GPP TS 48.018 11.3.63.1 */
	template RAN_Information_Request_Application_Container_GTPC
	tru_GTPC_RAN_Information_Request_Application_Container_NACC(template GTP_CellId cid) := {
		nacc := tr_GTPC_RAN_Information_Request_Application_Container_NACC(cid)
	}
	template (value) RAN_Information_Request_Application_Container_GTPC
	tsu_GTPC_RAN_Information_Request_Application_Container_NACC(GTP_CellId cid) := {
		nacc := ts_GTPC_RAN_Information_Request_Application_Container_NACC(cid)
	}
	/* 3GPP TS 48.018 11.3.63.2.1 */
	template RAN_Information_Application_Container_NACC_GTPC
	tr_GTPC_RAN_Information_Application_Container_NACC(template GTP_CellId cid, boolean psi_type, integer si_psi_num, octetstring si_psi) := {
		 iEI := '4E'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := ?
		 },
		 reporting_Cell_Identifier := tr_GTPC_Cell_Identifier_V(cid),
		 typeBit := bool2bit(psi_type),
		 number_of_SI_PSI := int2bit(si_psi_num, 7),
		 sI_PSI := si_psi
	}
	template (value) RAN_Information_Application_Container_NACC_GTPC
	ts_GTPC_RAN_Information_Application_Container_NACC(GTP_CellId cid, boolean psi_type, integer si_psi_num, octetstring si_psi) := {
		 iEI := '4E'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := 0 /* overwritten */
		 },
		 reporting_Cell_Identifier := ts_GTPC_Cell_Identifier_V(cid),
		 typeBit := bool2bit(psi_type),
		 number_of_SI_PSI := int2bit(si_psi_num, 7),
		 sI_PSI := si_psi
	}

	/* RAN_Information_Request */
	template (value) RAN_Information_Request_RIM_Container_GTPC
	ts_GTPC_RAN_Information_Request_RIM_Container(template (value) RIM_Application_Identity_GTPC app_id,
						      template (value) RIM_Sequence_Number_GTPC seq,
						      template (value) RIM_PDU_Indications_GTPC ind,
						      template (omit) RIM_Protocol_Version_Number_GTPC ver := omit,
						      template (omit) RAN_Information_Request_Application_Container_GTPC app_cont := omit,
						      template (omit) SON_TransferApplicationIdentity son_app_id := omit) := {
		iEI := '57'O,
		ext := '1'B,
		lengthIndicator := {
			length1 := 0 /* overwritten */
		},
		rIM_Application_Identity := app_id,
		rIM_Sequence_Number := seq,
		rIM_PDU_Indications := ind,
		rIM_Protocol_Version_Number := ver,
		application_Container := app_cont,
		sON_TransferApplicationIdentity := son_app_id
	}
	template (value) PDU_BSSGP_RAN_INFORMATION_REQUEST_GTPC
	ts_GTPC_RAN_Information_Request(template (value) RIM_Routing_Information_GTPC dest,
					template (value) RIM_Routing_Information_GTPC src,
					template (value) RAN_Information_Request_RIM_Container_GTPC  cont) := {
		bssgpPduType := '71'O,
		destination_Cell_Identifier := dest,
		source_Cell_Identifier := src,
		rIM_Container := cont
	}
	template (value) RANTransparentContainer ts_RANTransparentContainer_RAN_INFO_REQ(template (value) PDU_BSSGP_RAN_INFORMATION_REQUEST_GTPC pdu) := {
		type_gtpc := '90'O,
		lengthf := 0, /* FIXME */
		rANTransparentContainerField := {
			pDU_BSSGP_RAN_INFORMATION_REQUEST := pdu
		}
	}

	/* RAN_Information */
	template ApplContainer_or_ApplErrContainer_NACC_GTPC
	tru_GTPC_ApplContainer_NACC(GTP_CellId cid, boolean psi_type, integer si_psi_num, octetstring si_psi) := {
		application_Container := tr_GTPC_RAN_Information_Application_Container_NACC(cid, psi_type, si_psi_num, si_psi)
	}
	template (value) ApplContainer_or_ApplErrContainer_NACC_GTPC
	tsu_GTPC_ApplContainer_NACC(GTP_CellId cid, boolean psi_type, integer si_psi_num, octetstring si_psi) := {
		application_Container := ts_GTPC_RAN_Information_Application_Container_NACC(cid, psi_type, si_psi_num, si_psi)
	}
	template ApplContainer_or_ApplErrContainer_GTPC
	tru_GTPC_ApplContainer_or_ApplErrContainer_NACC(template ApplContainer_or_ApplErrContainer_NACC_GTPC cont) := {
		nacc := cont
	}
	template (value) ApplContainer_or_ApplErrContainer_GTPC
	tsu_GTPC_ApplContainer_or_ApplErrContainer_NACC(template (value) ApplContainer_or_ApplErrContainer_NACC_GTPC cont) := {
		nacc := cont
	}
	template RAN_Information_RIM_Container_GTPC
	tr_GTPC_RAN_Information_RIM_Container(template RIM_Application_Identity_GTPC app_id,
					      template RIM_Sequence_Number_GTPC seq,
					      template RIM_PDU_Indications_GTPC ind,
					      template RIM_Protocol_Version_Number_GTPC ver := omit,
					      template ApplContainer_or_ApplErrContainer_GTPC app_cont := omit,
					      template SON_TransferApplicationIdentity son_app_id := omit) := {
		iEI := '58'O,
		ext := '1'B,
		lengthIndicator := {
			length1 := ?
		},
		rIM_Application_Identity := app_id,
		rIM_Sequence_Number := seq,
		rIM_PDU_Indications := ind,
		rIM_Protocol_Version_Number := ver,
		applContainer_or_ApplErrContainer := app_cont,
		sON_TransferApplicationIdentity := son_app_id
	}
	template (value) RAN_Information_RIM_Container_GTPC
	ts_GTPC_RAN_Information_RIM_Container(template (value) RIM_Application_Identity_GTPC app_id,
						      template (value) RIM_Sequence_Number_GTPC seq,
						      template (value) RIM_PDU_Indications_GTPC ind,
						      template (omit) RIM_Protocol_Version_Number_GTPC ver := omit,
						      template (omit) ApplContainer_or_ApplErrContainer_GTPC app_cont := omit,
						      template (omit) SON_TransferApplicationIdentity son_app_id := omit) := {
		iEI := '58'O,
		ext := '1'B,
		lengthIndicator := {
			length1 := 0 /* overwritten */
		},
		rIM_Application_Identity := app_id,
		rIM_Sequence_Number := seq,
		rIM_PDU_Indications := ind,
		rIM_Protocol_Version_Number := ver,
		applContainer_or_ApplErrContainer := app_cont,
		sON_TransferApplicationIdentity := son_app_id
	}
	template PDU_BSSGP_RAN_INFORMATION_GTPC
	tr_GTPC_RAN_Information(template RIM_Routing_Information_GTPC dest,
				template RIM_Routing_Information_GTPC src,
				template RAN_Information_RIM_Container_GTPC cont) := {
		bssgpPduType := '70'O,
		destination_Cell_Identifier := dest,
		source_Cell_Identifier := src,
		rIM_Container := cont
	}
	template (value) PDU_BSSGP_RAN_INFORMATION_GTPC
	ts_GTPC_RAN_Information(template (value) RIM_Routing_Information_GTPC dest,
				template (value) RIM_Routing_Information_GTPC src,
				template (value) RAN_Information_RIM_Container_GTPC cont) := {
		bssgpPduType := '70'O,
		destination_Cell_Identifier := dest,
		source_Cell_Identifier := src,
		rIM_Container := cont
	}
	template RANTransparentContainer tr_RANTransparentContainer_RAN_INFO(template PDU_BSSGP_RAN_INFORMATION_GTPC pdu) := {
		type_gtpc := '90'O,
		lengthf := ?,
		rANTransparentContainerField := {
			pDU_BSSGP_RAN_INFORMATION := pdu
		}
	}
	template (value) RANTransparentContainer ts_RANTransparentContainer_RAN_INFO(template (value) PDU_BSSGP_RAN_INFORMATION_GTPC pdu) := {
		type_gtpc := '90'O,
		lengthf := 0, /* overwritten */
		rANTransparentContainerField := {
			pDU_BSSGP_RAN_INFORMATION := pdu
		}
	}

	template RANTransparentContainer tr_RANTransparentContainer(template RANTransparentContainerField rANTransparentContainerField) := {
		type_gtpc := '90'O,
		lengthf := ?,
		rANTransparentContainerField := rANTransparentContainerField
	}
	template (value) RANTransparentContainer ts_RANTransparentContainer(template (value) RANTransparentContainerField rANTransparentContainerField) := {
		type_gtpc := '90'O,
		lengthf := 0, /* overwritten */
		rANTransparentContainerField := rANTransparentContainerField
	}
	template GTPC_PDUs tr_RANInfoRelay(template RANTransparentContainer transparentContainer) := {
		ranInformationRelay := {
			transparentContainer := transparentContainer,
			rIM_RoutingAddress := *,
			rIM_RoutingAddress_Discriminator := *,
			private_extension_gtpc := *
		}
	}
	template (value) GTPC_PDUs ts_RANInfoRelay(template (value) RANTransparentContainer transparentContainer) := {
		ranInformationRelay := {
			transparentContainer := transparentContainer,
			rIM_RoutingAddress := omit,
			rIM_RoutingAddress_Discriminator := omit,
			private_extension_gtpc := omit
		}
	}
	template Gtp1cUnitdata
	tr_GTPC_RANInfoRelay(template GtpPeer peer,
			     template RANTransparentContainer transparentContainer) := {
		peer := peer,
		gtpc := tr_GTP1C_PDU(rANInformationRelay, '00000000'O, tr_RANInfoRelay(transparentContainer))
	}
	template (value) Gtp1cUnitdata
	ts_GTPC_RANInfoRelay(template (value) GtpPeer peer,
			     template (value) RANTransparentContainer transparentContainer) := {
		peer := peer,
		gtpc := ts_GTP1C_PDU(rANInformationRelay, '00000000'O, valueof(ts_RANInfoRelay(transparentContainer)), 0)
	}


	template RAN_Information_Request_RIM_Container_GTPC
	tr_GTPC_RAN_Information_Request_RIM_Container(template RIM_Application_Identity_GTPC app_id := ?,
						      template RIM_Sequence_Number_GTPC seq := ?,
						      template RIM_PDU_Indications_GTPC ind := ?,
						      template RIM_Protocol_Version_Number_GTPC ver := *,
						      template RAN_Information_Request_Application_Container_GTPC app_cont := *,
						      template SON_TransferApplicationIdentity son_app_id := *) := {
		 iEI := '57'O,
		 ext := '1'B,
		 lengthIndicator := {
			length1 := ?
		 },
		 rIM_Application_Identity := app_id,
		 rIM_Sequence_Number := seq,
		 rIM_PDU_Indications := ind,
		 rIM_Protocol_Version_Number := ver,
		 application_Container := app_cont,
		 sON_TransferApplicationIdentity := son_app_id
	}

	/* 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
	}

	function f_GTPU_s_bit(template (omit) uint16_t seq) return BIT1 {
		if (istemplatekind(seq, "omit")) {
			return '0'B;
		}
		return '1'B;
	}

	function f_GTPU_opt_part(template (omit) uint16_t seq) return template (omit) GTPU_Header_optional_part {
		if (istemplatekind(seq, "omit")) {
			return omit;
		}
		var GTPU_Header_optional_part ret := {
			sequenceNumber := int2oct(valueof(seq), 2),
			npduNumber := '00'O,
			nextExtHeader := '00'O,
			gTPU_extensionHeader_List := omit
		};
		return ret;
	}

	/* generalized GTP-U send template */
	template (value) PDU_GTPU ts_GTP1U_PDU(OCT1 msg_type, template (omit) 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'.
		 *
		 * Note that the caller must ensure that these conditions hold.
		 * The caller can either pass a sequence number (we set s_bit to '1'B) when appropriate,
		 * or may omit the sequence number (we set s_bit to '0'B). */
		s_bit := f_GTPU_s_bit(seq),
		/* 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 := f_GTPU_opt_part(seq),
		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/response */
	template Gtp1uUnitdata tr_GTPU_PING(template GtpPeer peer) := tr_GTPU_MsgType(peer, echoRequest, '00000000'O);
	template Gtp1uUnitdata tr_GTPU_PONG(template GtpPeer peer) := tr_GTPU_MsgType(peer, echoResponse, '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_UEchoReqPDU := {
		echoRequest_IEs := {
			private_extension_gtpu := omit
		}
	}

	/* master template for sending a GTP-C echo request */
	template (value) Gtp1uUnitdata ts_GTPU_PING(GtpPeer peer, uint16_t seq) := {
		peer := peer,
		gtpu := ts_GTP1U_PDU(echoRequest, seq, '00000000'O, valueof(ts_UEchoReqPDU))
	}

	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)))
	}

	template GSNAddress_gtpu ts_UGsnAddr(octetstring ip_addr) := {
		type_gtpu := '85'O,
		lengthf := lengthof(ip_addr),
		gSNAddressValue := ip_addr
	}

	template TeidDataI_gtpu ts_UteidDataI(OCT4 teid) := {
		type_gtpu := '10'O,
		teidDataI := teid
	}

	template GTPU_IEs ts_UErrorIndication(OCT4 teid, octetstring gsn_addr) := {
		errorIndication_IEs := {
			teidDataI_gtpu := ts_UteidDataI(teid),
			gSNAddress_gtpu := ts_UGsnAddr(gsn_addr),
			private_extension_gtpu := omit
		}
	}

	/* master template for sending a GTP-U Error indication */
	template Gtp1uUnitdata ts_GTPU_ErrorIndication(GtpPeer peer, uint16_t seq, OCT4 teid, octetstring gsn_addr) := {
		peer := peer,
		gtpu := ts_GTP1U_PDU('1A'O, seq, '00000000'O, valueof(ts_UErrorIndication(teid, gsn_addr)))
	}

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