aboutsummaryrefslogtreecommitdiffstats
path: root/gtp/gtpie.c
blob: c4eabf81c971b67aad8ae1f8ddcc3a4dc7888383 (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
/* 
 *  OsmoGGSN - Gateway GPRS Support Node
 *  Copyright (C) 2002 Mondru AB.
 * 
 *  The contents of this file may be used under the terms of the GNU
 *  General Public License Version 2, provided that the above copyright
 *  notice and this permission notice is included in all copies or
 *  substantial portions of the software.
 * 
 */

/*
 * gtpie.c: Contains functions to encapsulate and decapsulate GTP 
 * information elements 
 *
 *
 * Encapsulation
 * - gtpie_tlv, gtpie_tv0, gtpie_tv1, gtpie_tv2 ... Adds information
 * elements to a buffer.
 *
 * Decapsulation
 *  - gtpie_decaps: Returns array with pointers to information elements.
 *  - getie_getie: Returns the pointer of a particular element.
 *  - gtpie_gettlv: Copies tlv information element. Return 0 on success.
 *  - gtpie_gettv: Copies tv information element. Return 0 on success.
 *
 */

#include <../config.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>

#include "gtpie.h"

/*! Encode a TLV type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] l Length of value \a v in bytes
 *  \param[in] v Pointer to input value
 *  \returns 0 on success, 1 on error */
int gtpie_tlv(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      int l, const void *v)
{
	if ((*length + 3 + l) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tlv.t = hton8(t);
	((union gtpie_member *)(p + *length))->tlv.l = hton16(l);
	memcpy((void *)(p + *length + 3), v, l);
	*length += 3 + l;
	return 0;
}

/*! Encode a TV0 (Tag + value) type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] l Length of value \a v in bytes
 *  \param[in] v Pointer to input value
 *  \returns 0 on success, 1 on error */
int gtpie_tv0(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      int l, const uint8_t * v)
{
	if ((*length + 1 + l) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tv0.t = hton8(t);
	memcpy((void *)(p + *length + 1), v, l);
	*length += 1 + l;
	return 0;
}

/*! Encode a TV1 (Tag + 8bit value) type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] v Input value
 *  \returns 0 on success, 1 on error */
int gtpie_tv1(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      uint8_t v)
{
	if ((*length + 2) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tv1.t = hton8(t);
	((union gtpie_member *)(p + *length))->tv1.v = hton8(v);
	*length += 2;
	return 0;
}

/*! Encode a TV2 (Tag + 16bit value) type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] v Input value
 *  \returns 0 on success, 1 on error */
int gtpie_tv2(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      uint16_t v)
{
	if ((*length + 3) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tv2.t = hton8(t);
	((union gtpie_member *)(p + *length))->tv2.v = hton16(v);
	*length += 3;
	return 0;
}

/*! Encode a TV4 (Tag + 32bit value) type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] v Input value
 *  \returns 0 on success, 1 on error */
int gtpie_tv4(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      uint32_t v)
{
	if ((*length + 5) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tv4.t = hton8(t);
	((union gtpie_member *)(p + *length))->tv4.v = hton32(v);
	*length += 5;
	return 0;
}

/*! Encode a TV8 (Tag + 64bit value) type Information Element.
 *  \param[inout] p Pointer to output packet to which IE is appended
 *  \param[inout] length Up to which byte length is \a p used/filled
 *  \param[in] size Total size of \a p in bytes
 *  \param[in] t Tag / Information Element Identifier
 *  \param[in] v Input value
 *  \returns 0 on success, 1 on error */
int gtpie_tv8(void *p, unsigned int *length, unsigned int size, uint8_t t,
	      uint64_t v)
{
	if ((*length + 9) >= size)
		return 1;
	((union gtpie_member *)(p + *length))->tv8.t = hton8(t);
	((union gtpie_member *)(p + *length))->tv8.v = hton64(v);
	*length += 9;
	return 0;
}

/*! Obtain a GTP IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \returns index into \a ie on success; -1 if not found */
int gtpie_getie(union gtpie_member *ie[], int type, int instance)
{
	int j;
	for (j = 0; j < GTPIE_SIZE; j++) {
		if ((ie[j] != 0) && (ie[j]->t == type)) {
			if (instance-- == 0)
				return j;
		}
	}
	return -1;
}

/*! Determine if IE for a given tag/IEI exists in a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \returns 1 if IEI instance present in \a ie; 0 if not */
int gtpie_exist(union gtpie_member *ie[], int type, int instance)
{
	int j;
	for (j = 0; j < GTPIE_SIZE; j++) {
		if ((ie[j] != 0) && (ie[j]->t == type)) {
			if (instance-- == 0)
				return 1;
		}
	}
	return 0;
}

/*! Obtain Value of TLV-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[out] length Length of IE
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \param[in] size Size of \a dst in bytes
 *  \returns 0 on sucess; EOF in case value is larger than \a size */
int gtpie_gettlv(union gtpie_member *ie[], int type, int instance,
		 unsigned int *length, void *dst, unsigned int size)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0) {
		*length = ntoh16(ie[ien]->tlv.l);
		if (*length <= size)
			memcpy(dst, ie[ien]->tlv.v, *length);
		else
			return EOF;
	}
	return 0;
}

/*! Obtain Value of TV0-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \param[in] size Size of value in bytes
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_gettv0(union gtpie_member *ie[], int type, int instance,
		 void *dst, unsigned int size)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0)
		memcpy(dst, ie[ien]->tv0.v, size);
	else
		return EOF;
	return 0;
}

/*! Obtain Value of TV1-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_gettv1(union gtpie_member *ie[], int type, int instance,
		 uint8_t * dst)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0)
		*dst = ntoh8(ie[ien]->tv1.v);
	else
		return EOF;
	return 0;
}

/*! Obtain Value of TV2-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_gettv2(union gtpie_member *ie[], int type, int instance,
		 uint16_t * dst)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0)
		*dst = ntoh16(ie[ien]->tv2.v);
	else
		return EOF;
	return 0;
}

/*! Obtain Value of TV4-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_gettv4(union gtpie_member *ie[], int type, int instance,
		 uint32_t * dst)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0)
		*dst = ntoh32(ie[ien]->tv4.v);
	else
		return EOF;
	return 0;
}

/*! Obtain Value of TV8-type IE for a given tag/IEI from a list/array.
 *  \param[in] ie Array of GTPIE
 *  \param[in] type Tag/IEI for which we're looking
 *  \param[in] instance Instance (number of occurence) of this IEI
 *  \param[inout] dst Caller-allocated buffer where to store value
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_gettv8(union gtpie_member *ie[], int type, int instance,
		 uint64_t * dst)
{
	int ien;
	ien = gtpie_getie(ie, type, instance);
	if (ien >= 0)
		*dst = ntoh64(ie[ien]->tv8.v);
	else
		return EOF;
	return 0;
}

/*! Parse an incoming GTP packet into its Information Elements.
 *  \param[out] ie Caller-allocated Array of GTPIE
 *  \param[in] version GTP protocol version
 *  \param[in] pack Pointer to raw GTP packet (payload part)
 *  \param[in] len Length of \a pack in bytes
 *  \returns 0 on sucess; EOF in case IE not found */
int gtpie_decaps(union gtpie_member *ie[], int version, const void *pack,
		 unsigned len)
{
	int i;
	int j = 0;
	const unsigned char *p;
	const unsigned char *end;

	end = (unsigned char *)pack + len;
	p = pack;

	memset(ie, 0, sizeof(union gtpie_member *) * GTPIE_SIZE);

	while ((p < end) && (j < GTPIE_SIZE)) {
		if (GTPIE_DEBUG) {
			printf("The packet looks like this:\n");
			for (i = 0; i < (end - p); i++) {
				printf("%02x ",
				       (unsigned char)*(char *)(p + i));
				if (!((i + 1) % 16))
					printf("\n");
			};
			printf("\n");
		}

		switch (*p) {
		case GTPIE_CAUSE:	/* TV GTPIE types with value length 1 */
		case GTPIE_REORDER:
		case GTPIE_MAP_CAUSE:
		case GTPIE_MS_VALIDATED:
		case GTPIE_RECOVERY:
		case GTPIE_SELECTION_MODE:
		case GTPIE_TEARDOWN:
		case GTPIE_NSAPI:
		case GTPIE_RANAP_CAUSE:
		case GTPIE_RP_SMS:
		case GTPIE_RP:
		case GTPIE_MS_NOT_REACH:
		case GTPIE_BCM:
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE TV1 found. Type %d, value %d\n",
					     ie[j]->tv1.t, ie[j]->tv1.v);
				p += 1 + 1;
				j++;
			}
			break;
		case GTPIE_FL_DI:	/* TV GTPIE types with value length 2 or 4 */
		case GTPIE_FL_C:
			if (version != 0) {
				if (j < GTPIE_SIZE) {	/* GTPIE_TEI_DI & GTPIE_TEI_C with length 4 */
					/* case GTPIE_TEI_DI: gtp1 */
					/* case GTPIE_TEI_C:  gtp1 */
					ie[j] = (union gtpie_member *)p;
					if (GTPIE_DEBUG)
						printf
						    ("GTPIE TV 4 found. Type %d, value %d\n",
						     ie[j]->tv4.t,
						     ie[j]->tv4.v);
					p += 1 + 4;
					j++;
				}
				break;
			}
		case GTPIE_PFI:	/* TV GTPIE types with value length 2 */
		case GTPIE_CHARGING_C:
		case GTPIE_TRACE_REF:
		case GTPIE_TRACE_TYPE:
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE TV2 found. Type %d, value %d\n",
					     ie[j]->tv2.t, ie[j]->tv2.v);
				p += 1 + 2;
				j++;
			}
			break;
		case GTPIE_QOS_PROFILE0:	/* TV GTPIE types with value length 3 */
		case GTPIE_P_TMSI_S:
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE TV 3 found. Type %d, value %d, %d, %d\n",
					     ie[j]->tv0.t, ie[j]->tv0.v[0],
					     ie[j]->tv0.v[1], ie[j]->tv0.v[2]);
				p += 1 + 3;
				j++;
			}
			break;
		case GTPIE_TLLI:	/* TV GTPIE types with value length 4 */
		case GTPIE_P_TMSI:
		case GTPIE_CHARGING_ID:
			/* case GTPIE_TEI_DI: Handled by GTPIE_FL_DI */
			/* case GTPIE_TEI_C:  Handled by GTPIE_FL_DI */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE TV 4 found. Type %d, value %d\n",
					     ie[j]->tv4.t, ie[j]->tv4.v);
				p += 1 + 4;
				j++;
			}
			break;
		case GTPIE_TEI_DII:	/* TV GTPIE types with value length 5 */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf("GTPIE TV 5 found. Type %d\n",
					       ie[j]->tv0.t);
				p += 1 + 5;
				j++;
			}
			break;
		case GTPIE_RAB_CONTEXT:	/* TV GTPIE types with value length 7 */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf("GTPIE TV 7 found. Type %d\n",
					       ie[j]->tv0.t);
				p += 1 + 7;
				j++;
			}
			break;
		case GTPIE_IMSI:	/* TV GTPIE types with value length 8 */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE_IMSI - GTPIE TV 8 found. Type %d, value 0x%llx\n",
					     ie[j]->tv0.t, ie[j]->tv8.v);
				p += 1 + 8;
				j++;
			}
			break;
		case GTPIE_RAI:	/* TV GTPIE types with value length 6 */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE_RAI - GTPIE TV 6 found. Type %d, value 0x%llx\n",
					     ie[j]->tv0.t, ie[j]->tv8.v);
				p += 1 + 6;
				j++;
			}
			break;
		case GTPIE_AUTH_TRIPLET:	/* TV GTPIE types with value length 28 */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf("GTPIE TV 28 found. Type %d\n",
					       ie[j]->tv0.t);
				p += 1 + 28;
				j++;
			}
			break;
		case GTPIE_EXT_HEADER_T:	/* GTP extension header */
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf
					    ("GTPIE GTP extension header found. Type %d\n",
					     ie[j]->ext.t);
				p += 2 + ntoh8(ie[j]->ext.l);
				j++;
			}
			break;
		case GTPIE_EUA:	/* TLV GTPIE types with variable length */
		case GTPIE_MM_CONTEXT:
		case GTPIE_PDP_CONTEXT:
		case GTPIE_APN:
		case GTPIE_PCO:
		case GTPIE_GSN_ADDR:
		case GTPIE_MSISDN:
		case GTPIE_QOS_PROFILE:
		case GTPIE_AUTH_QUINTUP:
		case GTPIE_TFT:
		case GTPIE_TARGET_INF:
		case GTPIE_UTRAN_TRANS:
		case GTPIE_RAB_SETUP:
		case GTPIE_TRIGGER_ID:
		case GTPIE_OMC_ID:
		case GTPIE_RAN_T_CONTAIN:
		case GTPIE_PDP_CTX_PRIO:
		case GTPIE_ADDL_RAB_S_I:
		case GTPIE_SGSN_NUMBER:
		case GTPIE_COMMON_FLAGS:
		case GTPIE_APN_RESTR:
		case GTPIE_R_PRIO_LCS:
		case GTPIE_RAT_TYPE:
		case GTPIE_USER_LOC:
		case GTPIE_MS_TZ:
		case GTPIE_IMEI_SV:
		case GTPIE_CML_CHG_I_CT:
		case GTPIE_MBMS_UE_CTX:
		case GTPIE_TMGI:
		case GTPIE_RIM_ROUT_ADDR:
		case GTPIE_MBMS_PCO:
		case GTPIE_MBMS_SA:
		case GTPIE_SRNC_PDCP_CTX:
		case GTPIE_ADDL_TRACE:
		case GTPIE_HOP_CTR:
		case GTPIE_SEL_PLMN_ID:
		case GTPIE_MBMS_SESS_ID:
		case GTPIE_MBMS_2_3G_IND:
		case GTPIE_ENH_NSAPI:
		case GTPIE_MBMS_SESS_DUR:
		case GTPIE_A_MBMS_TRAC_I:
		case GTPIE_MBMS_S_REP_N:
		case GTPIE_MBMS_TTDT:
		case GTPIE_PS_HO_REQ_CTX:
		case GTPIE_BSS_CONTAINER:
		case GTPIE_CELL_ID:
		case GTPIE_PDU_NUMBERS:
		case GTPIE_BSSGP_CAUSE:
		case GTPIE_RQD_MBMS_BCAP:
		case GTPIE_RIM_RA_DISCR:
		case GTPIE_L_SETUP_PFCS:
		case GTPIE_PS_HO_XID_PAR:
		case GTPIE_MS_CHG_REP_A:
		case GTPIE_DIR_TUN_FLAGS:
		case GTPIE_CORREL_ID:
		case GTPIE_MBMS_FLOWI:
		case GTPIE_MBMS_MC_DIST:
		case GTPIE_MBMS_DIST_ACK:
		case GTPIE_R_IRAT_HO_INF:
		case GTPIE_RFSP_IDX:
		case GTPIE_FQDN:
		case GTPIE_E_ALL_PRIO_1:
		case GTPIE_E_ALL_PRIO_2:
		case GTPIE_E_CMN_FLAGS:
		case GTPIE_U_CSG_INFO:
		case GTPIE_CSG_I_REP_ACT:
		case GTPIE_CSG_ID:
		case GTPIE_CSG_MEMB_IND:
		case GTPIE_AMBR:
		case GTPIE_UE_NET_CAPA:
		case GTPIE_UE_AMBR:
		case GTPIE_APN_AMBR_NS:
		case GTPIE_GGSN_BACKOFF:
		case GTPIE_S_PRIO_IND:
		case GTPIE_S_PRIO_IND_NS:
		case GTPIE_H_BR_16MBPS_F:
		case GTPIE_A_MMCTX_SRVCC:
		case GTPIE_A_FLAGS_SRVCC:
		case GTPIE_STN_SR:
		case GTPIE_C_MSISDN:
		case GTPIE_E_RANAP_CAUSE:
		case GTPIE_ENODEB_ID:
		case GTPIE_SEL_MODE_NS:
		case GTPIE_ULI_TIMESTAMP:
		case GTPIE_CHARGING_ADDR:
		case GTPIE_PRIVATE:
			if (j < GTPIE_SIZE) {
				ie[j] = (union gtpie_member *)p;
				if (GTPIE_DEBUG)
					printf("GTPIE TLV found. Type %d\n",
					       ie[j]->tlv.t);
				p += 3 + ntoh16(ie[j]->tlv.l);
				j++;
			}
			break;
		default:
			if (GTPIE_DEBUG)
				printf("GTPIE something unknown. Type %d\n",
				       *p);
			return EOF;	/* We received something unknown */
		}
	}
	if (p == end) {
		if (GTPIE_DEBUG)
			printf("GTPIE normal return. %lx %lx\n",
			       (unsigned long)p, (unsigned long)end);
		return 0;	/* We landed at the end of the packet: OK */
	} else if (!(j < GTPIE_SIZE)) {
		if (GTPIE_DEBUG)
			printf("GTPIE too many elements.\n");
		return EOF;	/* We received too many information elements */
	} else {
		if (GTPIE_DEBUG)
			printf("GTPIE exceeded end of packet. %lx %lx\n",
			       (unsigned long)p, (unsigned long)end);
		return EOF;	/* We exceeded the end of the packet: Error */
	}
}

/*! Encode GTP packet payload from Array of Information Elements.
 *  \param[out] ie Input Array of GTPIE
 *  \param[out] pack Pointer to caller-allocated buffer for raw GTP packet (GTPIE_MAX length)
 *  \param[out] len Encoded length of \a pack in bytes
 *  \returns 0 on sucess; 2 for out-of-space */
int gtpie_encaps(union gtpie_member *ie[], void *pack, unsigned *len)
{
	int i;
	unsigned char *p;
	unsigned char *end;
	int iesize;

	p = pack;

	memset(pack, 0, GTPIE_MAX);
	end = p + GTPIE_MAX;
	for (i = 1; i < GTPIE_SIZE; i++)
		if (ie[i] != 0) {
			if (GTPIE_DEBUG)
				printf("gtpie_encaps. Type %d\n", i);
			switch (i) {
			case GTPIE_CAUSE:	/* TV GTPIE types with value length 1 */
			case GTPIE_REORDER:
			case GTPIE_MAP_CAUSE:
			case GTPIE_MS_VALIDATED:
			case GTPIE_RECOVERY:
			case GTPIE_SELECTION_MODE:
			case GTPIE_TEARDOWN:
			case GTPIE_NSAPI:
			case GTPIE_RANAP_CAUSE:
			case GTPIE_RP_SMS:
			case GTPIE_RP:
			case GTPIE_MS_NOT_REACH:
			case GTPIE_BCM:
				iesize = 2;
				break;
			case GTPIE_FL_DI:	/* TV GTPIE types with value length 2 */
			case GTPIE_FL_C:
			case GTPIE_PFI:
			case GTPIE_CHARGING_C:
			case GTPIE_TRACE_REF:
			case GTPIE_TRACE_TYPE:
				iesize = 3;
				break;
			case GTPIE_QOS_PROFILE0:	/* TV GTPIE types with value length 3 */
			case GTPIE_P_TMSI_S:
				iesize = 4;
				break;
			case GTPIE_TLLI:	/* TV GTPIE types with value length 4 */
			case GTPIE_P_TMSI:
				/* case GTPIE_TEI_DI: only in gtp1 */
				/* case GTPIE_TEI_C: only in gtp1 */
			case GTPIE_CHARGING_ID:
				iesize = 5;
				break;
			case GTPIE_TEI_DII:	/* TV GTPIE types with value length 5 */
				iesize = 6;
				break;
			case GTPIE_RAB_CONTEXT:	/* TV GTPIE types with value length 7 */
				iesize = 8;
				break;
			case GTPIE_IMSI:	/* TV GTPIE types with value length 8 */
			case GTPIE_RAI:
				iesize = 9;
				break;
			case GTPIE_AUTH_TRIPLET:	/* TV GTPIE types with value length 28 */
				iesize = 29;
				break;
			case GTPIE_EXT_HEADER_T:	/* GTP extension header */
				iesize = 2 + hton8(ie[i]->ext.l);
				break;
			case GTPIE_EUA:	/* TLV GTPIE types with length length 2 */
			case GTPIE_MM_CONTEXT:
			case GTPIE_PDP_CONTEXT:
			case GTPIE_APN:
			case GTPIE_PCO:
			case GTPIE_GSN_ADDR:
			case GTPIE_MSISDN:
			case GTPIE_QOS_PROFILE:
			case GTPIE_AUTH_QUINTUP:
			case GTPIE_TFT:
			case GTPIE_TARGET_INF:
			case GTPIE_UTRAN_TRANS:
			case GTPIE_RAB_SETUP:
			case GTPIE_TRIGGER_ID:
			case GTPIE_OMC_ID:
			case GTPIE_RAN_T_CONTAIN:
			case GTPIE_PDP_CTX_PRIO:
			case GTPIE_ADDL_RAB_S_I:
			case GTPIE_SGSN_NUMBER:
			case GTPIE_COMMON_FLAGS:
			case GTPIE_APN_RESTR:
			case GTPIE_R_PRIO_LCS:
			case GTPIE_RAT_TYPE:
			case GTPIE_USER_LOC:
			case GTPIE_MS_TZ:
			case GTPIE_IMEI_SV:
			case GTPIE_CML_CHG_I_CT:
			case GTPIE_MBMS_UE_CTX:
			case GTPIE_TMGI:
			case GTPIE_RIM_ROUT_ADDR:
			case GTPIE_MBMS_PCO:
			case GTPIE_MBMS_SA:
			case GTPIE_SRNC_PDCP_CTX:
			case GTPIE_ADDL_TRACE:
			case GTPIE_HOP_CTR:
			case GTPIE_SEL_PLMN_ID:
			case GTPIE_MBMS_SESS_ID:
			case GTPIE_MBMS_2_3G_IND:
			case GTPIE_ENH_NSAPI:
			case GTPIE_MBMS_SESS_DUR:
			case GTPIE_A_MBMS_TRAC_I:
			case GTPIE_MBMS_S_REP_N:
			case GTPIE_MBMS_TTDT:
			case GTPIE_PS_HO_REQ_CTX:
			case GTPIE_BSS_CONTAINER:
			case GTPIE_CELL_ID:
			case GTPIE_PDU_NUMBERS:
			case GTPIE_BSSGP_CAUSE:
			case GTPIE_RQD_MBMS_BCAP:
			case GTPIE_RIM_RA_DISCR:
			case GTPIE_L_SETUP_PFCS:
			case GTPIE_PS_HO_XID_PAR:
			case GTPIE_MS_CHG_REP_A:
			case GTPIE_DIR_TUN_FLAGS:
			case GTPIE_CORREL_ID:
			case GTPIE_MBMS_FLOWI:
			case GTPIE_MBMS_MC_DIST:
			case GTPIE_MBMS_DIST_ACK:
			case GTPIE_R_IRAT_HO_INF:
			case GTPIE_RFSP_IDX:
			case GTPIE_FQDN:
			case GTPIE_E_ALL_PRIO_1:
			case GTPIE_E_ALL_PRIO_2:
			case GTPIE_E_CMN_FLAGS:
			case GTPIE_U_CSG_INFO:
			case GTPIE_CSG_I_REP_ACT:
			case GTPIE_CSG_ID:
			case GTPIE_CSG_MEMB_IND:
			case GTPIE_AMBR:
			case GTPIE_UE_NET_CAPA:
			case GTPIE_UE_AMBR:
			case GTPIE_APN_AMBR_NS:
			case GTPIE_GGSN_BACKOFF:
			case GTPIE_S_PRIO_IND:
			case GTPIE_S_PRIO_IND_NS:
			case GTPIE_H_BR_16MBPS_F:
			case GTPIE_A_MMCTX_SRVCC:
			case GTPIE_A_FLAGS_SRVCC:
			case GTPIE_STN_SR:
			case GTPIE_C_MSISDN:
			case GTPIE_E_RANAP_CAUSE:
			case GTPIE_ENODEB_ID:
			case GTPIE_SEL_MODE_NS:
			case GTPIE_ULI_TIMESTAMP:
			case GTPIE_CHARGING_ADDR:
			case GTPIE_PRIVATE:
				iesize = 3 + hton16(ie[i]->tlv.l);
				break;
			default:
				return 2;	/* We received something unknown */
			}
			if (p + iesize < end) {
				memcpy(p, ie[i], iesize);
				p += iesize;
				*len += iesize;
			} else
				return 2;	/* Out of space */
		}
	return 0;
}

/*! Encode GTP packet payload from Array of Information Elements.
 *  \param[out] ie Input Array of GTPIE
 *  \param[in] size Size of ?
 *  \param[out] pack Pointer to caller-allocated buffer for raw GTP packet (GTPIE_MAX length)
 *  \param[out] len Encoded length of \a pack in bytes
 *  \returns 0 on sucess; 2 for out-of-space */
int gtpie_encaps2(union gtpie_member ie[], unsigned int size,
		  void *pack, unsigned *len)
{
	unsigned int i, j;
	unsigned char *p;
	unsigned char *end;
	int iesize;

	p = pack;

	memset(pack, 0, GTPIE_MAX);
	end = p + GTPIE_MAX;
	for (j = 0; j < GTPIE_SIZE; j++)
		for (i = 0; i < size; i++)
			if (ie[i].t == j) {
				if (GTPIE_DEBUG)
					printf
					    ("gtpie_encaps. Number %d, Type %d\n",
					     i, ie[i].t);
				switch (ie[i].t) {
				case GTPIE_CAUSE:	/* TV GTPIE types with value length 1 */
				case GTPIE_REORDER:
				case GTPIE_MAP_CAUSE:
				case GTPIE_MS_VALIDATED:
				case GTPIE_RECOVERY:
				case GTPIE_SELECTION_MODE:
				case GTPIE_TEARDOWN:
				case GTPIE_NSAPI:
				case GTPIE_RANAP_CAUSE:
				case GTPIE_RP_SMS:
				case GTPIE_RP:
				case GTPIE_MS_NOT_REACH:
				case GTPIE_BCM:
					iesize = 2;
					break;
				case GTPIE_PFI:	/* TV GTPIE types with value length 2 */
				case GTPIE_CHARGING_C:
				case GTPIE_TRACE_REF:
				case GTPIE_TRACE_TYPE:
					iesize = 3;
					break;
				case GTPIE_QOS_PROFILE0:	/* TV GTPIE types with value length 3 */
				case GTPIE_P_TMSI_S:
					iesize = 4;
					break;
				case GTPIE_TLLI:	/* TV GTPIE types with value length 4 */
				case GTPIE_P_TMSI:
				case GTPIE_TEI_DI:
				case GTPIE_TEI_C:
				case GTPIE_CHARGING_ID:
					iesize = 5;
					break;
				case GTPIE_TEI_DII:	/* TV GTPIE types with value length 5 */
					iesize = 6;
					break;
				case GTPIE_RAB_CONTEXT:	/* TV GTPIE types with value length 7 */
					iesize = 8;
					break;
				case GTPIE_IMSI:	/* TV GTPIE types with value length 8 */
				case GTPIE_RAI:
					iesize = 9;
					break;
				case GTPIE_AUTH_TRIPLET:	/* TV GTPIE types with value length 28 */
					iesize = 29;
					break;
				case GTPIE_EXT_HEADER_T:	/* GTP extension header */
					iesize = 2 + hton8(ie[i].ext.l);
					break;
				case GTPIE_EUA:	/* TLV GTPIE types with length length 2 */
				case GTPIE_MM_CONTEXT:
				case GTPIE_PDP_CONTEXT:
				case GTPIE_APN:
				case GTPIE_PCO:
				case GTPIE_GSN_ADDR:
				case GTPIE_MSISDN:
				case GTPIE_QOS_PROFILE:
				case GTPIE_AUTH_QUINTUP:
				case GTPIE_TFT:
				case GTPIE_TARGET_INF:
				case GTPIE_UTRAN_TRANS:
				case GTPIE_RAB_SETUP:
				case GTPIE_TRIGGER_ID:
				case GTPIE_OMC_ID:
				case GTPIE_RAN_T_CONTAIN:
				case GTPIE_PDP_CTX_PRIO:
				case GTPIE_ADDL_RAB_S_I:
				case GTPIE_SGSN_NUMBER:
				case GTPIE_COMMON_FLAGS:
				case GTPIE_APN_RESTR:
				case GTPIE_R_PRIO_LCS:
				case GTPIE_RAT_TYPE:
				case GTPIE_USER_LOC:
				case GTPIE_MS_TZ:
				case GTPIE_IMEI_SV:
				case GTPIE_CML_CHG_I_CT:
				case GTPIE_MBMS_UE_CTX:
				case GTPIE_TMGI:
				case GTPIE_RIM_ROUT_ADDR:
				case GTPIE_MBMS_PCO:
				case GTPIE_MBMS_SA:
				case GTPIE_SRNC_PDCP_CTX:
				case GTPIE_ADDL_TRACE:
				case GTPIE_HOP_CTR:
				case GTPIE_SEL_PLMN_ID:
				case GTPIE_MBMS_SESS_ID:
				case GTPIE_MBMS_2_3G_IND:
				case GTPIE_ENH_NSAPI:
				case GTPIE_MBMS_SESS_DUR:
				case GTPIE_A_MBMS_TRAC_I:
				case GTPIE_MBMS_S_REP_N:
				case GTPIE_MBMS_TTDT:
				case GTPIE_PS_HO_REQ_CTX:
				case GTPIE_BSS_CONTAINER:
				case GTPIE_CELL_ID:
				case GTPIE_PDU_NUMBERS:
				case GTPIE_BSSGP_CAUSE:
				case GTPIE_RQD_MBMS_BCAP:
				case GTPIE_RIM_RA_DISCR:
				case GTPIE_L_SETUP_PFCS:
				case GTPIE_PS_HO_XID_PAR:
				case GTPIE_MS_CHG_REP_A:
				case GTPIE_DIR_TUN_FLAGS:
				case GTPIE_CORREL_ID:
				case GTPIE_MBMS_FLOWI:
				case GTPIE_MBMS_MC_DIST:
				case GTPIE_MBMS_DIST_ACK:
				case GTPIE_R_IRAT_HO_INF:
				case GTPIE_RFSP_IDX:
				case GTPIE_FQDN:
				case GTPIE_E_ALL_PRIO_1:
				case GTPIE_E_ALL_PRIO_2:
				case GTPIE_E_CMN_FLAGS:
				case GTPIE_U_CSG_INFO:
				case GTPIE_CSG_I_REP_ACT:
				case GTPIE_CSG_ID:
				case GTPIE_CSG_MEMB_IND:
				case GTPIE_AMBR:
				case GTPIE_UE_NET_CAPA:
				case GTPIE_UE_AMBR:
				case GTPIE_APN_AMBR_NS:
				case GTPIE_GGSN_BACKOFF:
				case GTPIE_S_PRIO_IND:
				case GTPIE_S_PRIO_IND_NS:
				case GTPIE_H_BR_16MBPS_F:
				case GTPIE_A_MMCTX_SRVCC:
				case GTPIE_A_FLAGS_SRVCC:
				case GTPIE_STN_SR:
				case GTPIE_C_MSISDN:
				case GTPIE_E_RANAP_CAUSE:
				case GTPIE_ENODEB_ID:
				case GTPIE_SEL_MODE_NS:
				case GTPIE_ULI_TIMESTAMP:
				case GTPIE_CHARGING_ADDR:
				case GTPIE_PRIVATE:
					iesize = 3 + hton16(ie[i].tlv.l);
					break;
				default:
					return 2;	/* We received something unknown */
				}
				if (p + iesize < end) {
					memcpy(p, &ie[i], iesize);
					p += iesize;
					*len += iesize;
				} else
					return 2;	/* Out of space */
			}
	return 0;
}