aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-nsip.c
blob: 5d3456c1db8c46f9682572d24bcfdcbf6121af5d (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
/* packet-nsip.c
 * Routines for Network Service Over IP dissection
 * Copyright 2000, Susanne Edlund <susanne.edlund@ericsson.com>
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* 3GPP TS 48.016 V 5.3.0 (2004-07) Release 6 + CR013 */

#include "config.h"

#include <epan/packet.h>

#include <prefs.h>
#include <epan/to_str.h>

void proto_register_nsip(void);
void proto_reg_handoff_nsip(void);

#define NSIP_DEBUG 0
#define NSIP_SEP ", " /* Separator string */

static range_t *global_nsip_udp_port_range;
#define DEFAULT_NSIP_PORT_RANGE "2157,19999"

/* Initialize the protocol and registered fields */
static int proto_nsip = -1;

static int hf_nsip_cause = -1;
static int hf_nsip_ns_vci = -1;
static int hf_nsip_pdu_type = -1;
static int hf_nsip_bvci = -1;
static int hf_nsip_nsei = -1;
/* static int hf_nsip_ip4_elements = -1; */
/* static int hf_nsip_ip6_elements = -1; */
static int hf_nsip_max_num_ns_vc = -1;
static int hf_nsip_num_ip4_endpoints = -1;
static int hf_nsip_num_ip6_endpoints = -1;
static int hf_nsip_reset_flag = -1;
static int hf_nsip_reset_flag_spare = -1;
static int hf_nsip_ip_address_type = -1;
static int hf_nsip_ip_address_ipv4 = -1;
static int hf_nsip_ip_address_ipv6 = -1;
static int hf_nsip_end_flag = -1;
static int hf_nsip_end_flag_spare = -1;
static int hf_nsip_control_bits_r = -1;
static int hf_nsip_control_bits_c = -1;
static int hf_nsip_control_bits_spare = -1;
static int hf_nsip_transaction_id = -1;
/* static int hf_nsip_ip_element_ip_address_ipv4 = -1; */
/* static int hf_nsip_ip_element_ip_address_ipv6 = -1; */
static int hf_nsip_ip_element_udp_port = -1;
static int hf_nsip_ip_element_signalling_weight = -1;
static int hf_nsip_ip_element_data_weight = -1;


/* Initialize the subtree pointers */
static gint ett_nsip = -1;
static gint ett_nsip_control_bits = -1;
static gint ett_nsip_reset_flag = -1;
static gint ett_nsip_end_flag = -1;
static gint ett_nsip_ip_element = -1;
static gint ett_nsip_ip_element_list = -1;

/* PDU type coding, v5.3.0, table 10.3.7.1, p 51 */
#define NSIP_PDU_NS_UNITDATA        0x00
#define NSIP_PDU_NS_RESET           0x02
#define NSIP_PDU_NS_RESET_ACK       0x03
#define NSIP_PDU_NS_BLOCK           0x04
#define NSIP_PDU_NS_BLOCK_ACK       0x05
#define NSIP_PDU_NS_UNBLOCK         0x06
#define NSIP_PDU_NS_UNBLOCK_ACK     0x07
#define NSIP_PDU_NS_STATUS          0x08
#define NSIP_PDU_NS_ALIVE           0x0a
#define NSIP_PDU_NS_ALIVE_ACK       0x0b
#define NSIP_PDU_SNS_ACK            0x0c
#define NSIP_PDU_SNS_ADD            0x0d
#define NSIP_PDU_SNS_CHANGEWEIGHT   0x0e
#define NSIP_PDU_SNS_CONFIG         0x0f
#define NSIP_PDU_SNS_CONFIG_ACK     0x10
#define NSIP_PDU_SNS_DELETE         0x11
#define NSIP_PDU_SNS_SIZE           0x12
#define NSIP_PDU_SNS_SIZE_ACK       0x13

static const value_string tab_nsip_pdu_types[] = {
  { NSIP_PDU_NS_UNITDATA,        "NS_UNITDATA" },
  { NSIP_PDU_NS_RESET,           "NS_RESET" },
  { NSIP_PDU_NS_RESET_ACK,       "NS_RESET_ACK" },
  { NSIP_PDU_NS_BLOCK,           "NS_BLOCK" },
  { NSIP_PDU_NS_BLOCK_ACK,       "NS_BLOCK_ACK" },
  { NSIP_PDU_NS_UNBLOCK,         "NS_UNBLOCK" },
  { NSIP_PDU_NS_UNBLOCK_ACK,     "NS_UNBLOCK_ACK" },
  { NSIP_PDU_NS_STATUS,          "NS_STATUS" },
  { NSIP_PDU_NS_ALIVE,           "NS_ALIVE" },
  { NSIP_PDU_NS_ALIVE_ACK,       "NS_ALIVE_ACK" },
  { NSIP_PDU_SNS_ACK,            "SNS_ACK" },
  { NSIP_PDU_SNS_ADD,            "SNS_ADD" },
  { NSIP_PDU_SNS_CHANGEWEIGHT,   "SNS_CHANGEWEIGHT" },
  { NSIP_PDU_SNS_CONFIG,         "SNS_CONFIG" },
  { NSIP_PDU_SNS_CONFIG_ACK,     "SNS_CONFIG_ACK" },
  { NSIP_PDU_SNS_DELETE,         "SNS_DELETE" },
  { NSIP_PDU_SNS_SIZE,           "SNS_SIZE" },
  { NSIP_PDU_SNS_SIZE_ACK,       "SNS_SIZE_ACK" },
  { 0,                            NULL },
};

/* Information element coding, v 5.3.0, table 10.3.1, p 46 */
#define NSIP_IE_CAUSE              0x00
#define NSIP_IE_NS_VCI             0x01
#define NSIP_IE_NS_PDU             0x02
#define NSIP_IE_BVCI               0x03
#define NSIP_IE_NSEI               0x04
#define NSIP_IE_IP4_ELEMENTS       0x05
#define NSIP_IE_IP6_ELEMENTS       0x06
#define NSIP_IE_MAX_NUM_NS_VC      0x07
#define NSIP_IE_NUM_IP4_ENDPOINTS  0x08
#define NSIP_IE_NUM_IP6_ENDPOINTS  0x09
#define NSIP_IE_RESET_FLAG         0x0a
#define NSIP_IE_IP_ADDRESS         0x0b

#if NSIP_DEBUG
static const value_string tab_nsip_ieis[] = {
  { NSIP_IE_CAUSE,               "Cause" },
  { NSIP_IE_NS_VCI,              "NS-VCI" },
  { NSIP_IE_NS_PDU,              "NS PDU" },
  { NSIP_IE_BVCI,                "BVCI" },
  { NSIP_IE_NSEI,                "NSEI" },
  { NSIP_IE_IP4_ELEMENTS,        "List of IP4 Elements" },
  { NSIP_IE_IP6_ELEMENTS,        "List of IP6 Elements" },
  { NSIP_IE_MAX_NUM_NS_VC,       "Maximum Number of NC-VCs" },
  { NSIP_IE_NUM_IP4_ENDPOINTS,   "Number of IP4 Endpoints" },
  { NSIP_IE_NUM_IP6_ENDPOINTS,   "Number of IP6 Endpoints"},
  { NSIP_IE_RESET_FLAG,          "Reset Flag" },
  { NSIP_IE_IP_ADDRESS,          "IP Address" },
  { 0,                            NULL },
};
#endif

/* Cause values, v 5.3.0, table 10.3.2.1, p 47 */
#define NSIP_CAUSE_TRANSIT_NETWORK_FAILURE      0x00
#define NSIP_CAUSE_OM_INTERVENTION              0x01
#define NSIP_CAUSE_EQUIPMENT_FAILURE            0x02
#define NSIP_CAUSE_NS_VC_BLOCKED                0x03
#define NSIP_CAUSE_NS_VC_UNKNOWN                0x04
#define NSIP_CAUSE_BVCI_UNKNOWN                 0x05
#define NSIP_CAUSE_SEMANTICALLY_INCORRECT_PDU   0x08
#define NSIP_CAUSE_NSIP_PDU_NOT_COMPATIBLE      0x0a
#define NSIP_CAUSE_PROTOCOL_ERROR               0x0b
#define NSIP_CAUSE_INVALID_ESSENTIAL_IE         0x0c
#define NSIP_CAUSE_MISSING_ESSENTIAL_IE         0x0d
#define NSIP_CAUSE_INVALID_NUM_IP4_ENDPOINTS    0x0e
#define NSIP_CAUSE_INVALID_NUM_IP6_ENDPOINTS    0x0f
#define NSIP_CAUSE_INVALID_NUM_NS_VC            0x10
#define NSIP_CAUSE_INVALID_WEIGHTS              0x11
#define NSIP_CAUSE_UNKNOWN_IP_ENDPOINT          0x12
#define NSIP_CAUSE_UNKNOWN_IP_ADDRESS           0x13
#define NSIP_CAUSE_IP_TEST_FAILED               0x14

static const value_string tab_nsip_cause_values[] = {
  { NSIP_CAUSE_TRANSIT_NETWORK_FAILURE,   "Transit network failure" },
  { NSIP_CAUSE_OM_INTERVENTION,           "O&M intervention" },
  { NSIP_CAUSE_EQUIPMENT_FAILURE,         "Equipment failure" },
  { NSIP_CAUSE_NS_VC_BLOCKED,             "NS-VC blocked" },
  { NSIP_CAUSE_NS_VC_UNKNOWN,             "NS-VC unknown" },
  { NSIP_CAUSE_BVCI_UNKNOWN,              "BVCI unknown on that NSE" },
  { NSIP_CAUSE_SEMANTICALLY_INCORRECT_PDU, "Semantically incorrect PDU" },
  { NSIP_CAUSE_NSIP_PDU_NOT_COMPATIBLE,   "PDU not compatible with the protocol state" },
  { NSIP_CAUSE_PROTOCOL_ERROR,            "Protocol error - unspecified" },
  { NSIP_CAUSE_INVALID_ESSENTIAL_IE,      "Invalid essential IE" },
  { NSIP_CAUSE_MISSING_ESSENTIAL_IE,      "Missing essential IE" },
  { NSIP_CAUSE_INVALID_NUM_IP4_ENDPOINTS, "Invalid number of IP4 endpoints" },
  { NSIP_CAUSE_INVALID_NUM_IP6_ENDPOINTS, "Invalid number of IP6 endpoints" },
  { NSIP_CAUSE_INVALID_NUM_NS_VC,         "Invalid number of NS-VCs" },
  { NSIP_CAUSE_INVALID_WEIGHTS,           "Invalid weights" },
  { NSIP_CAUSE_UNKNOWN_IP_ENDPOINT,       "Unknown IP endpoint" },
  { NSIP_CAUSE_UNKNOWN_IP_ADDRESS,        "Unknown IP address" },
  { NSIP_CAUSE_IP_TEST_FAILED,            "IP test failed" },
  { 0,                                     NULL },
};

/* Presence requirements of Information Elements
   v 5.3.0, chapter 8.1.1, p. 35 */
#define NSIP_IE_PRESENCE_M 1   /* Mandatory */
#define NSIP_IE_PRESENCE_O 2   /* Conditional */
#define NSIP_IE_PRESENCE_C 3   /* Optional */

/* Format options */
#define NSIP_IE_FORMAT_V 1
#define NSIP_IE_FORMAT_TV 2
#define NSIP_IE_FORMAT_TLV 3

/* IP address types, v 5.3.0, chapter 10.3.2b, p. 48 */
#define NSIP_IP_ADDRESS_TYPE_IPV4 1
#define NSIP_IP_ADDRESS_TYPE_IPV6 2
#define NSIP_IP_VERSION_4 4
#define NSIP_IP_VERSION_6 6

static const value_string ip_address_type_vals[] = {
  { 0,                         "Reserved" },
  { NSIP_IP_ADDRESS_TYPE_IPV4, "IPv4 Address" },
  { NSIP_IP_ADDRESS_TYPE_IPV6, "IPv6 Address" },
  { 0,                            NULL },
};


#define NSIP_MASK_CONTROL_BITS_R 0x01
#define NSIP_MASK_CONTROL_BITS_C 0x02
#define NSIP_MASK_CONTROL_BITS_SPARE 0xFC
#define NSIP_MASK_END_FLAG 0x01
#define NSIP_MASK_END_FLAG_SPARE 0xFE
#define NSIP_MASK_RESET_FLAG 0x01
#define NSIP_MASK_RESET_FLAG_SPARE 0xFE

static dissector_handle_t bssgp_handle;
static dissector_handle_t nsip_handle;

static gboolean nsip_is_recursive = FALSE;

typedef struct {
  guint8        iei;
  guint8        presence_req;
  int           format;
  guint16       value_length; /* in bytes */
  guint16       total_length; /* as specified, or 0 if unspecified */
} nsip_ie_t;

typedef struct {
  tvbuff_t     *tvb;
  int           offset;
  packet_info  *pinfo;
  proto_tree   *nsip_tree;
  proto_tree   *parent_tree;
  proto_item   *ti;
} build_info_t;

typedef struct {
  int version;
  int address_length;
  int total_length;
} nsip_ip_element_info_t;

static nsip_ip_element_info_t ipv4_element = { NSIP_IP_VERSION_4, 4, 8 };
static nsip_ip_element_info_t ipv6_element = { NSIP_IP_VERSION_6, 16, 20 };

static void
get_value_length(nsip_ie_t *ie, build_info_t *bi) {
  /* length indicator in bit 8, 0 => two bytes, 1 => one byte */
  const guint8 MASK_LENGTH_INDICATOR = 0x80;
  const guint8 MASK_ONE_BYTE_LENGTH = 0x7f;
  guint8 length_len;
  guint16 length;

  length = tvb_get_guint8(bi->tvb, bi->offset);
  length_len = 1;

  if (length & MASK_LENGTH_INDICATOR) {
    length &= MASK_ONE_BYTE_LENGTH;
  }
  else {
    length_len++;
    length <<= 8;
    length |= tvb_get_guint8(bi->tvb, bi->offset+1);
  }
  ie->value_length = length;
  ie->total_length += length_len + length;
  bi->offset += length_len;
}

static int
check_correct_iei(nsip_ie_t *ie, build_info_t *bi) {
  guint8 fetched_iei = tvb_get_guint8(bi->tvb, bi->offset);

#if NSIP_DEBUG
  if (fetched_iei != ie->iei) {
    proto_tree_add_text(bi->nsip_tree, bi->tvb, bi->offset, 1,
                        "Tried IEI %s (%#02x), found IEI %s (%#02x)",
                        val_to_str_const(ie->iei, tab_nsip_ieis, "Unknown"),
                        ie->iei,
                        val_to_str_const(fetched_iei, tab_nsip_ieis, "Unknown"),
                        fetched_iei);
  }
#endif
  return (fetched_iei == ie->iei);
}

static void
decode_iei_cause(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 cause;

  cause = tvb_get_guint8(bi->tvb, bi->offset);
  proto_tree_add_uint(bi->nsip_tree, hf_nsip_cause,
      bi->tvb, ie_start_offset, ie->total_length,
      cause);
  col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
      "Cause: %s",
      val_to_str(cause, tab_nsip_cause_values, "Unknown (0x%02x)"));

  proto_item_append_text(bi->ti, ", Cause: %s",
            val_to_str(cause, tab_nsip_cause_values, "Unknown (0x%02x)"));

  bi->offset += ie->value_length;
}

static void
decode_iei_ns_vci(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint16 ns_vci;

  ns_vci = tvb_get_ntohs(bi->tvb, bi->offset);

  proto_tree_add_uint(bi->nsip_tree, hf_nsip_ns_vci,
      bi->tvb, ie_start_offset, ie->total_length,
      ns_vci);
  col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
      "NS VCI: %#04x", ns_vci);
  proto_item_append_text(bi->ti, ", NS VCI: %#04x", ns_vci);

  bi->offset += ie->value_length;
}

static void
decode_iei_ns_pdu(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  tvbuff_t * next_tvb;

  proto_tree_add_text(bi->nsip_tree, bi->tvb, ie_start_offset,
                      ie->total_length,
                      "NS PDU (%u bytes)", ie->value_length);
  next_tvb = tvb_new_subset(bi->tvb, bi->offset, ie->value_length, -1);
  if (nsip_handle) {
    gboolean was_recursive;
    was_recursive = nsip_is_recursive;
    nsip_is_recursive = TRUE;
    call_dissector(nsip_handle, next_tvb, bi->pinfo, bi->nsip_tree);
    nsip_is_recursive = was_recursive;
  }
  bi->offset += ie->value_length;
}

static void
decode_iei_nsei(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint16 nsei = tvb_get_ntohs(bi->tvb, bi->offset);

  proto_tree_add_uint(bi->nsip_tree, hf_nsip_nsei, bi->tvb,
                      ie_start_offset, ie->total_length, nsei);
  bi->offset += ie->value_length;

  col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
                        "NSEI %u", nsei);

  proto_item_append_text(bi->ti, ", NSEI %u", nsei);
}

static void
decode_iei_bvci(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint16 bvci = tvb_get_ntohs(bi->tvb, bi->offset);

  proto_tree_add_uint(bi->nsip_tree, hf_nsip_bvci, bi->tvb,
                      ie_start_offset, ie->total_length, bvci);
  bi->offset += ie->value_length;

  col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
                        "BVCI %u", bvci);
  proto_item_append_text(bi->ti, ", BVCI %u", bvci);
}

static proto_item *
decode_ip_element(nsip_ip_element_info_t *element, build_info_t *bi, proto_tree * element_tree) {
  guint16 udp_port;
  guint32 ip4_addr;
  struct e_in6_addr ip6_addr;
  proto_item *tf = NULL;
  proto_tree *field_tree = NULL;

  if (bi->nsip_tree) {
    tf = proto_tree_add_text(element_tree, bi->tvb, bi->offset,
                             element->total_length, "IP Element");
    field_tree = proto_item_add_subtree(tf, ett_nsip_ip_element);

    /* IP address */
    switch (element->version) {
    case NSIP_IP_VERSION_4:
      ip4_addr = tvb_get_ipv4(bi->tvb, bi->offset);
      proto_tree_add_item(field_tree, hf_nsip_ip_address_ipv4,
                          bi->tvb, bi->offset, element->address_length,
                          ENC_BIG_ENDIAN);
      proto_item_append_text(tf, ": IP address: %s",
                             ip_to_str((guint8 *)&ip4_addr));

      break;
    case NSIP_IP_VERSION_6:
      tvb_get_ipv6(bi->tvb, bi->offset, &ip6_addr);
      proto_tree_add_item(field_tree, hf_nsip_ip_address_ipv6, bi->tvb,
                          bi->offset, element->address_length,
                          ENC_NA);
      proto_item_append_text(tf, ": IP address: %s",
                             ip6_to_str((struct e_in6_addr *)&ip6_addr));
      break;
    default:
      ;
    }
  }
  bi->offset += element->address_length;

  if (bi->nsip_tree) {
    /* UDP port value */
    udp_port = tvb_get_ntohs(bi->tvb, bi->offset);
    proto_tree_add_item(field_tree, hf_nsip_ip_element_udp_port,
                               bi->tvb, bi->offset, 2, ENC_BIG_ENDIAN);
    proto_item_append_text(tf, ", UDP Port: %u", udp_port);
  }
  bi->offset += 2;

  if (bi->nsip_tree) {
    /* Signalling weight */
    proto_tree_add_item(field_tree, hf_nsip_ip_element_signalling_weight,
                        bi->tvb, bi->offset, 1, ENC_BIG_ENDIAN);
  }
  bi->offset++;

  if (bi->nsip_tree) {
    /* Data weight */
    proto_tree_add_item(field_tree, hf_nsip_ip_element_data_weight,
                        bi->tvb, bi->offset, 1, ENC_BIG_ENDIAN);
  }
  bi->offset++;
  return tf;
}

static proto_item *
decode_ip_elements(nsip_ip_element_info_t *element, nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  int i;
  int num_elements = ie->value_length / element->total_length;
  proto_item *tf;
  proto_tree *field_tree;

  tf = proto_tree_add_text(bi->nsip_tree, bi->tvb, ie_start_offset,
                           ie->total_length,
                           "List of IP%u Elements (%u Elements)",
                           element->version, num_elements);
  field_tree = proto_item_add_subtree(tf, ett_nsip_ip_element_list);

  for (i = 0; i < num_elements; i++) {
    decode_ip_element(element, bi, field_tree);
  }
  return tf;
}

static void
decode_iei_max_num_ns_vc(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
 guint16 num_ns_vc;

 if (bi->nsip_tree) {
   num_ns_vc = tvb_get_ntohs(bi->tvb, bi->offset);

   proto_tree_add_uint(bi->nsip_tree, hf_nsip_max_num_ns_vc,
                              bi->tvb, ie_start_offset, ie->total_length,
                              num_ns_vc);
 }
 bi->offset += 2;
}

static void
decode_iei_num_ip4_endpoints(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint16 num_endpoints;

  if (bi->nsip_tree) {
    num_endpoints = tvb_get_ntohs(bi->tvb, bi->offset);

    proto_tree_add_uint(bi->nsip_tree, hf_nsip_num_ip4_endpoints,
                               bi->tvb, ie_start_offset, ie->total_length,
                               num_endpoints);
  }
  bi->offset += 2;
}

static void
decode_iei_num_ip6_endpoints(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint16 num_endpoints;

  if (bi->nsip_tree) {
    num_endpoints = tvb_get_ntohs(bi->tvb, bi->offset);

    proto_tree_add_uint(bi->nsip_tree, hf_nsip_num_ip6_endpoints,
                               bi->tvb, ie_start_offset, ie->total_length,
                               num_endpoints);
  }
  bi->offset += 2;
}

static void
decode_iei_reset_flag(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 flag;
  proto_item *tf;
  proto_tree *field_tree;

  flag = tvb_get_guint8(bi->tvb, bi->offset);
  if (bi->nsip_tree) {

     tf = proto_tree_add_text(bi->nsip_tree, bi->tvb, ie_start_offset,
                 ie->total_length,
                 "Reset Flag: %#02x", flag);

     field_tree = proto_item_add_subtree(tf, ett_nsip_reset_flag);
     proto_tree_add_boolean(field_tree, hf_nsip_reset_flag, bi->tvb,
                           bi->offset, 1,
                           flag & NSIP_MASK_RESET_FLAG);
     if (flag & NSIP_MASK_RESET_FLAG) {
         col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
                   "Reset");
         proto_item_append_text(bi->ti, ", Reset");
     }
     proto_tree_add_uint(field_tree, hf_nsip_reset_flag_spare,
                           bi->tvb, bi->offset, 1,
                           flag & NSIP_MASK_RESET_FLAG_SPARE);
  }
  bi->offset += 1;
}

static void
decode_iei_ip_address(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 addr_type;
  guint32 ip4_addr;
  struct e_in6_addr ip6_addr;

  addr_type = tvb_get_guint8(bi->tvb, bi->offset);
  proto_tree_add_item(bi->nsip_tree, hf_nsip_ip_address_type,
                          bi->tvb, bi->offset, 1, ENC_BIG_ENDIAN);
  switch (addr_type) {
  case NSIP_IP_ADDRESS_TYPE_IPV4:
    ie->total_length = 2 + ipv4_element.address_length;
    ip4_addr = tvb_get_ipv4(bi->tvb, bi->offset+1);
    proto_tree_add_ipv4(bi->nsip_tree, hf_nsip_ip_address_ipv4,
        bi->tvb, ie_start_offset, ie->total_length,
        ip4_addr);
    break;
  case NSIP_IP_ADDRESS_TYPE_IPV6:
    ie->total_length = 2 + ipv6_element.address_length;
    tvb_get_ipv6(bi->tvb, bi->offset+1, &ip6_addr);
    proto_tree_add_ipv6(bi->nsip_tree, hf_nsip_ip_address_ipv4,
        bi->tvb, ie_start_offset, ie->total_length,
        (guint8 *)&ip6_addr);
    break;
  default:
    return; /* error */
  }
  bi->offset += ie->value_length;
}

static void
decode_iei_transaction_id(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 id;
  id = tvb_get_guint8(bi->tvb, bi->offset);
  proto_tree_add_uint(bi->nsip_tree, hf_nsip_transaction_id,
                      bi->tvb, ie_start_offset, ie->total_length, id);
  col_append_sep_fstr(bi->pinfo->cinfo, COL_INFO, NSIP_SEP,
            "Transaction Id: %d", id);
  bi->offset += 1;
}

static void
decode_iei_end_flag(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 flag;
  proto_item *tf;
  proto_tree *field_tree;

  if (bi->nsip_tree) {
      flag = tvb_get_guint8(bi->tvb, bi->offset);

      tf = proto_tree_add_text(bi->nsip_tree, bi->tvb, ie_start_offset,
                     ie->total_length,
                     "End Flag: %#02x", flag);

      field_tree = proto_item_add_subtree(tf, ett_nsip_end_flag);
      proto_tree_add_boolean(field_tree, hf_nsip_end_flag, bi->tvb,
                           bi->offset, 1,
                           flag & NSIP_MASK_END_FLAG);
      if (flag & NSIP_MASK_END_FLAG) {
          proto_item_append_text(bi->ti, ", End");
      }
      proto_tree_add_uint(field_tree, hf_nsip_end_flag_spare,
                           bi->tvb, bi->offset, 1,
                           flag & NSIP_MASK_END_FLAG_SPARE);
  }
  bi->offset += 1;
}

static void
decode_iei_control_bits(nsip_ie_t *ie, build_info_t *bi, int ie_start_offset) {
  guint8 control_bits;
  proto_item *tf;
  proto_tree *field_tree;

  control_bits = tvb_get_guint8(bi->tvb, bi->offset);

  if (bi->nsip_tree) {
    tf = proto_tree_add_text(bi->nsip_tree, bi->tvb, ie_start_offset,
                             ie->total_length,
                             "NS SDU Control bits: %#02x", control_bits);

    field_tree = proto_item_add_subtree(tf, ett_nsip_control_bits);
    proto_tree_add_boolean(field_tree, hf_nsip_control_bits_r, bi->tvb,
                           bi->offset, 1,
                           control_bits & NSIP_MASK_CONTROL_BITS_R);
    proto_tree_add_boolean(field_tree, hf_nsip_control_bits_c, bi->tvb,
                           bi->offset, 1,
                           control_bits & NSIP_MASK_CONTROL_BITS_C);
    proto_tree_add_uint(field_tree, hf_nsip_control_bits_spare,
                           bi->tvb, bi->offset, 1,
                           control_bits & NSIP_MASK_CONTROL_BITS_SPARE);
  }
  bi->offset++;

  if (control_bits & NSIP_MASK_CONTROL_BITS_R) {
    col_append_sep_str(bi->pinfo->cinfo, COL_INFO, NSIP_SEP, "Req CF");
    proto_item_append_text(bi->ti, ", Request Change Flow");
  }

  if (control_bits & NSIP_MASK_CONTROL_BITS_C) {
    col_append_sep_str(bi->pinfo->cinfo, COL_INFO, NSIP_SEP, "Conf CF");
    proto_item_append_text(bi->ti, ", Confirm Change Flow");
  }
}


static void
decode_ie(nsip_ie_t *ie, build_info_t *bi) {

  int org_offset = bi->offset;

  if (tvb_length_remaining(bi->tvb, bi->offset) < 1) {
    return;
  }
  switch (ie->format) {
  case NSIP_IE_FORMAT_TLV:
    if (!check_correct_iei(ie, bi)) {
      return;
    }
    bi->offset++; /* Account for type */
    ie->total_length = 1;
    get_value_length(ie, bi);
    break;
  case NSIP_IE_FORMAT_TV:
    if (!check_correct_iei(ie, bi)) {
      return;
    }
    bi->offset++; /* Account for type */
    ie->value_length = ie->total_length - 1;
    break;
  case NSIP_IE_FORMAT_V:
    ie->value_length = ie->total_length;
  default:
    ;
  }
  switch (ie->iei) {
  case NSIP_IE_CAUSE:
    decode_iei_cause(ie, bi, org_offset);
    break;
  case NSIP_IE_NS_VCI:
    decode_iei_ns_vci(ie, bi, org_offset);
    break;
  case NSIP_IE_NS_PDU:
    decode_iei_ns_pdu(ie, bi, org_offset);
    break;
  case NSIP_IE_NSEI:
    decode_iei_nsei(ie, bi, org_offset);
    break;
  case NSIP_IE_BVCI:
    decode_iei_bvci(ie, bi, org_offset);
    break;
  case NSIP_IE_IP4_ELEMENTS:
    decode_ip_elements(&ipv4_element, ie, bi, org_offset);
    break;
  case NSIP_IE_IP6_ELEMENTS:
    decode_ip_elements(&ipv6_element, ie, bi, org_offset);
    break;
  case NSIP_IE_MAX_NUM_NS_VC:
    decode_iei_max_num_ns_vc(ie, bi, org_offset);
    break;
  case NSIP_IE_NUM_IP4_ENDPOINTS:
    decode_iei_num_ip4_endpoints(ie, bi, org_offset);
    break;
  case NSIP_IE_NUM_IP6_ENDPOINTS:
    decode_iei_num_ip6_endpoints(ie, bi, org_offset);
    break;
  case NSIP_IE_RESET_FLAG:
    decode_iei_reset_flag(ie, bi, org_offset);
    break;
  case NSIP_IE_IP_ADDRESS:
    decode_iei_ip_address(ie, bi, org_offset);
    break;
  default:
    ;
  }
}

static void
decode_pdu_general(nsip_ie_t *ies, int num_ies, build_info_t *bi) {
  int i;
  for (i = 0; i < num_ies; i++) {
    decode_ie(&ies[i], bi);
  }
}

static void
decode_pdu_ns_unitdata(build_info_t *bi) {
  tvbuff_t *next_tvb;

  nsip_ie_t ies[] = {
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* Control bits */
    { NSIP_IE_BVCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 2 },
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 0 },
    /* NS SDU, length unknown */
  };
  gint sdu_length;

  decode_iei_control_bits(ies, bi, bi->offset);
  decode_pdu_general(&ies[1], 1, bi);

  next_tvb = tvb_new_subset_remaining(bi->tvb, bi->offset);
  if (bssgp_handle) {
    call_dissector(bssgp_handle, next_tvb, bi->pinfo, bi->parent_tree);
  }
  else {
    sdu_length = tvb_length_remaining(bi->tvb, bi->offset);
    proto_tree_add_text(bi->nsip_tree, bi->tvb, bi->offset, sdu_length,
                        "NS SDU (%u bytes)", sdu_length);
  }
}

static void
decode_pdu_ns_reset(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 3 },
    { NSIP_IE_NS_VCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
  };
  decode_pdu_general(ies, 3, bi);
}

static void
decode_pdu_ns_reset_ack(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NS_VCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
  };
  decode_pdu_general(ies, 2, bi);
}

static void
decode_pdu_ns_block(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 3 },
    { NSIP_IE_NS_VCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
  };
  decode_pdu_general(ies, 2, bi);
}

static void
decode_pdu_ns_block_ack(build_info_t *bi) {
  nsip_ie_t ies[] = { { NSIP_IE_NS_VCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV,
                        0, 1 }, };
  decode_pdu_general(ies, 1, bi);
}

static void
decode_pdu_ns_status(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 3 },
    { NSIP_IE_NS_VCI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_NS_PDU, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_BVCI, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
  };
  decode_pdu_general(ies, 6, bi);
}

static void
decode_pdu_sns_ack(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* Transaction id */
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 3 },
    { NSIP_IE_IP_ADDRESS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
  };
  decode_pdu_general(ies, 1, bi);
  decode_iei_transaction_id(&ies[1], bi, bi->offset);
  decode_pdu_general(&ies[2], 4, bi);
}

static void
decode_pdu_sns_add(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* Transaction id */
    { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
  };
  decode_pdu_general(ies, 1, bi);
  decode_iei_transaction_id(&ies[1], bi, bi->offset);
  decode_pdu_general(&ies[2], 2, bi);
}

static void
decode_pdu_sns_changeweight(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* Transaction id */
    { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    /* Unknown length */
  };
  decode_pdu_general(ies, 1, bi);
  decode_iei_transaction_id(&ies[1], bi, bi->offset);
  decode_pdu_general(&ies[2], 2, bi);
}

static void
decode_pdu_sns_config(build_info_t *bi) {

    nsip_ie_t ies[] = {
      { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* End flag */
      { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
      { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
      /* Unknown length */
      { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
      /* Unknown length */
    };
    decode_iei_end_flag(ies, bi, bi->offset);
    decode_pdu_general(&ies[1], 3, bi);
}

static void
decode_pdu_sns_config_ack(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 3 },
  };
  decode_pdu_general(ies, 2, bi);
}

static void
decode_pdu_sns_delete(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4}, /* CR013 */
    { 0, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_V, 0, 1 }, /* Transaction id */
    { NSIP_IE_IP_ADDRESS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TV, 0, 0 },
    /* Unknown length */
    { NSIP_IE_IP4_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
    { NSIP_IE_IP6_ELEMENTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 0 },
  };
  decode_pdu_general(ies, 1, bi);
  decode_iei_transaction_id(&ies[1], bi, bi->offset);
  decode_pdu_general(&ies[2], 3, bi);
}

static void
decode_pdu_sns_size(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_RESET_FLAG, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TV, 0, 2 },
    { NSIP_IE_MAX_NUM_NS_VC, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TV, 0, 3 },
    { NSIP_IE_NUM_IP4_ENDPOINTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TV,
      0, 3 },
    { NSIP_IE_NUM_IP6_ENDPOINTS, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TV,
      0, 3 },
  };
  decode_pdu_general(ies, 5, bi);
}

static void
decode_pdu_sns_size_ack(build_info_t *bi) {
  nsip_ie_t ies[] = {
    { NSIP_IE_NSEI, NSIP_IE_PRESENCE_M, NSIP_IE_FORMAT_TLV, 0, 4 },
    { NSIP_IE_CAUSE, NSIP_IE_PRESENCE_C, NSIP_IE_FORMAT_TLV, 0, 3 },
  };
  decode_pdu_general(ies, 2, bi);
}

static void
decode_pdu(guint8 pdu_type, build_info_t *bi) {
  switch (pdu_type) {
  case NSIP_PDU_NS_UNITDATA:
    decode_pdu_ns_unitdata(bi);
    break;
  case NSIP_PDU_NS_RESET:
    decode_pdu_ns_reset(bi);
    break;
  case NSIP_PDU_NS_RESET_ACK:
    decode_pdu_ns_reset_ack(bi);
    break;
  case NSIP_PDU_NS_BLOCK:
    decode_pdu_ns_block(bi);
    break;
  case NSIP_PDU_NS_BLOCK_ACK:
    decode_pdu_ns_block_ack(bi);
    break;
  case NSIP_PDU_NS_STATUS:
    decode_pdu_ns_status(bi);
    break;
  case NSIP_PDU_SNS_ACK:
    decode_pdu_sns_ack(bi);
    break;
  case NSIP_PDU_SNS_ADD:
    decode_pdu_sns_add(bi);
    break;
  case NSIP_PDU_SNS_CHANGEWEIGHT:
    decode_pdu_sns_changeweight(bi);
    break;
  case NSIP_PDU_SNS_CONFIG:
    decode_pdu_sns_config(bi);
    break;
  case NSIP_PDU_SNS_CONFIG_ACK:
    decode_pdu_sns_config_ack(bi);
    break;
  case NSIP_PDU_SNS_DELETE:
    decode_pdu_sns_delete(bi);
    break;
  case NSIP_PDU_SNS_SIZE:
    decode_pdu_sns_size(bi);
    break;
  case NSIP_PDU_SNS_SIZE_ACK:
    decode_pdu_sns_size_ack(bi);
    break;
  case NSIP_PDU_NS_ALIVE:
  case NSIP_PDU_NS_ALIVE_ACK:
  case NSIP_PDU_NS_UNBLOCK:
  case NSIP_PDU_NS_UNBLOCK_ACK:
    /* Only contains PDU type, which has already been decoded */
  default: ;
  }
}

static void
dissect_nsip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
  guint8 pdu_type;
  build_info_t bi = { NULL, 0, NULL, NULL, NULL, NULL };
  proto_tree *nsip_tree;

  bi.tvb = tvb;
  bi.pinfo = pinfo;
  bi.parent_tree = tree;

  pinfo->current_proto = "GPRS-NS";

  if (!nsip_is_recursive) {
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "GPRS-NS");
    col_clear(pinfo->cinfo, COL_INFO);
  }

  pdu_type = tvb_get_guint8(tvb, 0);
  bi.offset++;

  if (tree) {
    bi.ti = proto_tree_add_item(tree, proto_nsip, tvb, 0, -1,
                             ENC_NA);
    nsip_tree = proto_item_add_subtree(bi.ti, ett_nsip);
    proto_tree_add_item(nsip_tree, hf_nsip_pdu_type, tvb, 0, 1, ENC_NA);
    proto_item_append_text(bi.ti, ", PDU type: %s",
                               val_to_str_const(pdu_type, tab_nsip_pdu_types, "Unknown"));
    bi.nsip_tree = nsip_tree;
  }

  if (!nsip_is_recursive) {
    col_set_str(pinfo->cinfo, COL_INFO,
                val_to_str_const(pdu_type, tab_nsip_pdu_types, "Unknown PDU type"));
  } else {
    col_append_sep_fstr(pinfo->cinfo, COL_INFO, NSIP_SEP, "%s",
                val_to_str_const(pdu_type, tab_nsip_pdu_types, "Unknown PDU type"));
  }
  decode_pdu(pdu_type, &bi);
}

void
proto_register_nsip(void)
{
  static hf_register_info hf[] = {
    { &hf_nsip_cause,
      { "Cause", "nsip.cause",
        FT_UINT8, BASE_OCT, VALS(tab_nsip_cause_values), 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_ns_vci,
      { "NS-VCI", "nsip.ns_vci",
        FT_UINT16, BASE_HEX, NULL, 0x0,
        "Network Service Virtual Link Identifier", HFILL }
    },
    { &hf_nsip_pdu_type,
      { "PDU type", "nsip.pdu_type",
        FT_UINT8, BASE_OCT, VALS(tab_nsip_pdu_types), 0x0,
        "PDU type information element", HFILL }
    },
    { &hf_nsip_bvci,
      { "BVCI", "nsip.bvci",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "BSSGP Virtual Connection Identifier", HFILL }
    },
    { &hf_nsip_nsei,
      { "NSEI", "nsip.nsei",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "Network Service Entity Identifier", HFILL }
    },
#if 0
    { &hf_nsip_ip4_elements,
      { "IP4 elements", "nsip.ip4_elements",
        FT_NONE, BASE_NONE, NULL, 0x0,
        "List of IP4 elements", HFILL }
    },
#endif
#if 0
    { &hf_nsip_ip6_elements,
      { "IP6 elements", "nsip.ip6_elements",
        FT_NONE, BASE_NONE, NULL, 0x0,
        "List of IP6 elements", HFILL }
    },
#endif
    { &hf_nsip_max_num_ns_vc,
      { "Maximum number of NS-VCs", "nsip.max_num_ns_vc",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_num_ip4_endpoints,
      { "Number of IP4 endpoints", "nsip.num_ip4_endpoints",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_num_ip6_endpoints,
      { "Number of IP6 endpoints", "nsip.num_ip6_endpoints",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_reset_flag,
      { "Reset flag", "nsip.reset_flag.flag",
        FT_BOOLEAN, 8, TFS(&tfs_set_notset), NSIP_MASK_RESET_FLAG,
        NULL, HFILL }
    },
    { &hf_nsip_reset_flag_spare,
      { "Reset flag spare bits", "nsip.reset_flag.spare",
        FT_UINT8, BASE_HEX, NULL, NSIP_MASK_RESET_FLAG_SPARE,
        NULL, HFILL }
    },
    { &hf_nsip_ip_address_type,
      { "IP Address Type", "nsip.ip_address_type",
        FT_UINT8, BASE_DEC, VALS(ip_address_type_vals), 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_ip_address_ipv4,
      { "IP Address", "nsip.ipv4_address",
        FT_IPv4, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_ip_address_ipv6,
      { "IP Address", "nsip.ipv6_address",
        FT_IPv6, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_end_flag,
      { "End flag", "nsip.end_flag.flag",
        FT_BOOLEAN, 8, TFS(&tfs_set_notset), NSIP_MASK_END_FLAG,
        NULL, HFILL }
    },
    { &hf_nsip_end_flag_spare,
      { "End flag spare bits", "nsip.end_flag.spare",
        FT_UINT8, BASE_HEX, NULL, NSIP_MASK_END_FLAG_SPARE,
        NULL, HFILL }
    },
    { &hf_nsip_control_bits_r,
      { "Request change flow", "nsip.control_bits.r",
        FT_BOOLEAN, 8, TFS(&tfs_set_notset), NSIP_MASK_CONTROL_BITS_R,
        NULL, HFILL }
    },
    { &hf_nsip_control_bits_c,
      { "Confirm change flow", "nsip.control_bits.c",
        FT_BOOLEAN, 8, TFS(&tfs_set_notset), NSIP_MASK_CONTROL_BITS_C,
        NULL, HFILL }
    },
    { &hf_nsip_control_bits_spare,
      { "Spare bits", "nsip.control_bits.spare",
        FT_UINT8, BASE_HEX, NULL, NSIP_MASK_CONTROL_BITS_SPARE,
        NULL, HFILL }
    },
    { &hf_nsip_transaction_id,
      { "Transaction ID", "nsip.transaction_id",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      NULL, HFILL }
    },
#if 0
    { &hf_nsip_ip_element_ip_address_ipv4,
      { "IP Address", "nsip.ip_element.ipv4_address",
        FT_IPv4, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
#endif
#if 0
    { &hf_nsip_ip_element_ip_address_ipv6,
      { "IP Address", "nsip.ip_element.ipv6_address",
        FT_IPv6, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
#endif
    { &hf_nsip_ip_element_udp_port,
      { "UDP Port", "nsip.ip_element.udp_port",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_ip_element_signalling_weight,
      { "Signalling Weight", "nsip.ip_element.signalling_weight",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_nsip_ip_element_data_weight,
      { "Data Weight", "nsip.ip_element.data_weight",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
  };

  /* Setup protocol subtree array */
  static gint *ett[] = {
    &ett_nsip,
    &ett_nsip_control_bits,
    &ett_nsip_reset_flag,
    &ett_nsip_end_flag,
    &ett_nsip_ip_element,
    &ett_nsip_ip_element_list,
  };

  module_t *nsip_module;

  /* Register the protocol name and description */
  proto_nsip = proto_register_protocol("GPRS Network Service",
                                       "GPRS-NS", "gprs-ns");

  /* Required function calls to register the header fields and
     subtrees used */
  proto_register_field_array(proto_nsip, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

  register_dissector("gprs_ns", dissect_nsip, proto_nsip);

  /* Set default UDP ports */
  range_convert_str(&global_nsip_udp_port_range, DEFAULT_NSIP_PORT_RANGE, MAX_UDP_PORT);

  /* Register configuration options */
  nsip_module = prefs_register_protocol(proto_nsip, proto_reg_handoff_nsip);
  prefs_register_obsolete_preference(nsip_module, "udp.port1");
  prefs_register_obsolete_preference(nsip_module, "udp.port2");
  prefs_register_range_preference(nsip_module, "udp.ports", "GPRS-NS UDP ports",
                                  "UDP ports to be decoded as GPRS-NS (default: "
                                  DEFAULT_NSIP_PORT_RANGE ")",
                                  &global_nsip_udp_port_range, MAX_UDP_PORT);
}

void
proto_reg_handoff_nsip(void) {
  static gboolean nsip_prefs_initialized = FALSE;
  static range_t *nsip_udp_port_range;

  if (!nsip_prefs_initialized) {
    nsip_handle = find_dissector("gprs_ns");
    bssgp_handle = find_dissector("bssgp");
    nsip_prefs_initialized = TRUE;
  } else {
    dissector_delete_uint_range("udp.port", nsip_udp_port_range, nsip_handle);
    g_free(nsip_udp_port_range);
  }

  nsip_udp_port_range = range_copy(global_nsip_udp_port_range);

  dissector_add_uint_range("udp.port", nsip_udp_port_range, nsip_handle);

}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */