Don't include the header file to get the SNMP version unless we're
[obnox/wireshark/wip.git] / packet-pgm.c
1 /* packet-pgm.c
2  * Routines for pgm packet disassembly
3  *
4  * $Id: packet-pgm.c,v 1.19 2002/08/28 21:00:25 jmayer Exp $
5  *
6  * Copyright (c) 2000 by Talarian Corp
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1999 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <time.h>
35 #include <string.h>
36 #include <epan/packet.h>
37 #include "afn.h"
38 #include "ipproto.h"
39 #include <epan/resolv.h>
40 #include <epan/strutil.h>
41 #include <epan/conversation.h>
42 #include "prefs.h"
43
44 #include <epan/proto.h>
45
46 void proto_reg_handoff_pgm(void);
47 static void proto_rereg_pgm(void);
48
49 typedef guint8 nchar_t;
50 typedef guint16 nshort_t;
51 typedef guint32 nlong_t;
52
53 /* The PGM main header */
54 typedef struct {
55         nshort_t sport;            /* source port */
56         nshort_t dport;            /* destination port */
57         nchar_t type;              /* PGM type */
58         nchar_t opts;              /* options */
59         nshort_t cksum;            /* checksum */
60         nchar_t gsi[6];            /* Global Source ID */
61         nshort_t tsdulen;          /* TSDU length */
62 } pgm_type;
63 #define pgmhdr_ntoh(_p) \
64         (_p)->sport = g_ntohs((_p)->sport); \
65         (_p)->dport = g_ntohs((_p)->dport); \
66         (_p)->type = g_ntohs((_p)->type); \
67         (_p)->opts = g_ntohs((_p)->opts); \
68         (_p)->cksum = g_ntohs((_p)->cksum); \
69         (_p)->tsdulen = g_ntohs((_p)->tsdulen)
70
71 /* The PGM SPM header */
72 typedef struct {
73         nlong_t sqn;              /* SPM's sequence number */
74         nlong_t trail;            /* Trailing edge sequence number */
75         nlong_t lead;             /* Leading edge sequence number */
76         nshort_t path_afi;        /* NLA AFI */
77         nshort_t res;             /* reserved */
78         nlong_t path;             /* Path NLA */
79 } pgm_spm_t;
80 static const size_t PGM_SPM_SZ = sizeof(pgm_type)+sizeof(pgm_spm_t);
81 #define spm_ntoh(_p) \
82         (_p)->sqn = g_ntohl((_p)->sqn); \
83         (_p)->trail = g_ntohl((_p)->trail); \
84         (_p)->lead = g_ntohl((_p)->lead); \
85         (_p)->path_afi = g_ntohs((_p)->path_afi); \
86         (_p)->res = g_ntohs((_p)->res);
87
88 /* The PGM Data (ODATA/RDATA) header */
89 typedef struct {
90         nlong_t sqn;              /* Data Packet sequence number */
91         nlong_t trail;            /* Trailing edge sequence number */
92 } pgm_data_t;
93 #define data_ntoh(_p) \
94         (_p)->sqn = g_ntohl((_p)->sqn); \
95         (_p)->trail = g_ntohl((_p)->trail)
96 static const size_t PGM_DATA_HDR_SZ = sizeof(pgm_type)+sizeof(pgm_data_t);
97
98 /* The PGM NAK (NAK/N-NAK/NCF) header */
99 typedef struct {
100         nlong_t sqn;             /* Requested sequence number */
101         nshort_t src_afi;        /* NLA AFI for source (IPv4 is set to 1) */
102         nshort_t src_res;        /* reserved */
103         nlong_t src;             /* Source NLA  */
104         nshort_t grp_afi;        /* Multicast group AFI (IPv4 is set to 1) */
105         nshort_t grp_res;        /* reserved */
106         nlong_t grp;             /* Multicast group NLA */
107 } pgm_nak_t;
108 static const size_t PGM_NAK_SZ = sizeof(pgm_type)+sizeof(pgm_nak_t);
109 #define nak_ntoh(_p) \
110         (_p)->sqn = g_ntohl((_p)->sqn); \
111         (_p)->src_afi = g_ntohs((_p)->src_afi); \
112         (_p)->src_res = g_ntohs((_p)->src_res); \
113         (_p)->grp_afi = g_ntohs((_p)->grp_afi); \
114         (_p)->grp_res = g_ntohs((_p)->grp_res)
115
116 /* The PGM ACK header (PGMCC) */
117 typedef struct {
118         nlong_t rx_max_sqn;      /* RX_MAX sequence number */
119         nlong_t bitmap;          /* Received Packet Bitmap */
120 } pgm_ack_t;
121 static const size_t PGM_ACK_SZ = sizeof(pgm_type)+sizeof(pgm_ack_t);
122 #define ack_ntoh(_p) \
123         (_p)->rx_max_sqn = g_ntohl((_p)->rx_max_sqn); \
124         (_p)->bitmap = g_ntohl((_p)->bitmap)
125
126 /* constants for hdr types */
127 #if defined(PGM_SPEC_01_PCKTS)
128 /* old spec-01 types */
129 #define PGM_SPM_PCKT  0x00
130 #define PGM_ODATA_PCKT  0x10
131 #define PGM_RDATA_PCKT  0x11
132 #define PGM_NAK_PCKT  0x20
133 #define PGM_NNAK_PCKT  0x21
134 #define PGM_NCF_PCKT 0x30
135 #else
136 /* spec-02 types (as well as spec-04+) */
137 #define PGM_SPM_PCKT  0x00
138 #define PGM_ODATA_PCKT  0x04
139 #define PGM_RDATA_PCKT  0x05
140 #define PGM_NAK_PCKT  0x08
141 #define PGM_NNAK_PCKT  0x09
142 #define PGM_NCF_PCKT 0x0A
143 #define PGM_ACK_PCKT 0x0D
144 #endif /* PGM_SPEC_01_PCKTS */
145
146 /* port swapping on NAK and NNAKs or not (default is to swap) */
147 /* PGM_NO_PORT_SWAP */
148
149 /* option flags (main PGM header) */
150 #define PGM_OPT 0x01
151 #define PGM_OPT_NETSIG 0x02
152 #define PGM_OPT_VAR_PKTLEN 0x40
153 #define PGM_OPT_PARITY 0x80
154
155 /* option types */
156 #define PGM_OPT_LENGTH 0x00
157 #define PGM_OPT_END 0x80
158 #define PGM_OPT_FRAGMENT 0x01
159 #define PGM_OPT_NAK_LIST 0x02
160 #define PGM_OPT_JOIN 0x03
161 #define PGM_OPT_REDIRECT 0x07
162 #define PGM_OPT_SYN 0x0D
163 #define PGM_OPT_FIN 0x0E
164 #define PGM_OPT_RST 0x0F
165 #define PGM_OPT_PARITY_PRM 0x08
166 #define PGM_OPT_PARITY_GRP 0x09
167 #define PGM_OPT_CURR_TGSIZE 0x0A
168 #define PGM_OPT_PGMCC_DATA  0x12
169 #define PGM_OPT_PGMCC_FEEDBACK  0x13
170
171 static const nchar_t PGM_OPT_INVALID = 0x7F;
172
173 /* OPX bit values */
174 #define PGM_OPX_IGNORE  0x00
175 #define PGM_OPX_INVAL   0x01
176 #define PGM_OPX_DISCARD 0x10
177
178 /* option formats */
179 typedef struct {
180         nchar_t type;
181         nchar_t len;
182         nchar_t opx;
183         nchar_t res;
184 } pgm_opt_generic_t;
185
186 typedef struct {
187         nchar_t type;
188         nchar_t len;
189         nshort_t total_len;
190 } pgm_opt_length_t;
191
192 typedef struct {
193         nchar_t type;
194         nchar_t len;
195         nchar_t opx;
196         nchar_t res;
197 } pgm_opt_nak_list_t;
198
199 /*
200  * To squeeze the whole option into 255 bytes, we
201  * can only have 62 in the list
202  */
203 #define PGM_MAX_NAK_LIST_SZ (62)
204
205 typedef struct {
206         nchar_t type;
207         nchar_t len;
208         nchar_t opx;
209         nchar_t res;
210         nlong_t opt_join_min;
211 } pgm_opt_join_t;
212
213 typedef struct {
214         nchar_t type;
215         nchar_t len;
216         nchar_t opx;
217         nchar_t po;
218         nlong_t prm_tgsz;
219 } pgm_opt_parity_prm_t;
220
221 /* OPT_PARITY_PRM P and O bits */
222 static const nchar_t PGM_OPT_PARITY_PRM_PRO = 0x2;
223 static const nchar_t PGM_OPT_PARITY_PRM_OND = 0x1;
224
225 typedef struct {
226         nchar_t type;
227         nchar_t len;
228         nchar_t opx;
229         nchar_t res;
230         nlong_t prm_grp;
231 } pgm_opt_parity_grp_t;
232
233 typedef struct {
234         nchar_t type;
235         nchar_t len;
236         nchar_t opx;
237         nchar_t res;
238         nlong_t prm_atgsz;
239 } pgm_opt_curr_tgsize_t;
240
241 typedef struct {
242         nchar_t type;
243         nchar_t len;
244         nchar_t opx;
245         nchar_t res;
246         nlong_t tsp;
247         nshort_t acker_afi;
248         nshort_t res2;
249         nlong_t acker;
250 } pgm_opt_pgmcc_data_t;
251
252 typedef struct {
253         nchar_t type;
254         nchar_t len;
255         nchar_t opx;
256         nchar_t res;
257         nlong_t tsp;
258         nshort_t acker_afi;
259         nshort_t loss_rate;
260         nlong_t acker;
261 } pgm_opt_pgmcc_feedback_t;
262
263 /*
264  * Udp port for UDP encapsulation
265  */
266 #define DEFAULT_UDP_ENCAP_UCAST_PORT 3055
267 #define DEFAULT_UDP_ENCAP_MCAST_PORT 3056
268
269 static int udp_encap_ucast_port = 0;
270 static int udp_encap_mcast_port = 0;
271 static int old_encap_ucast_port = 0;
272 static int old_encap_mcast_port = 0;
273
274 static int proto_pgm = -1;
275 static int ett_pgm = -1;
276 static int ett_pgm_optbits = -1;
277 static int ett_pgm_opts = -1;
278 static int ett_pgm_spm = -1;
279 static int ett_pgm_data = -1;
280 static int ett_pgm_nak = -1;
281 static int ett_pgm_ack = -1;
282 static int ett_pgm_opts_join = -1;
283 static int ett_pgm_opts_parityprm = -1;
284 static int ett_pgm_opts_paritygrp = -1;
285 static int ett_pgm_opts_naklist = -1;
286 static int ett_pgm_opts_ccdata = -1;
287
288 static int hf_pgm_main_sport = -1;
289 static int hf_pgm_main_dport = -1;
290 static int hf_pgm_main_type = -1;
291 static int hf_pgm_main_opts = -1;
292 static int hf_pgm_main_opts_opt = -1;
293 static int hf_pgm_main_opts_netsig = -1;
294 static int hf_pgm_main_opts_varlen = -1;
295 static int hf_pgm_main_opts_parity = -1;
296 static int hf_pgm_main_cksum = -1;
297 static int hf_pgm_main_gsi = -1;
298 static int hf_pgm_main_tsdulen = -1;
299 static int hf_pgm_spm_sqn = -1;
300 static int hf_pgm_spm_lead = -1;
301 static int hf_pgm_spm_trail = -1;
302 static int hf_pgm_spm_pathafi = -1;
303 static int hf_pgm_spm_res = -1;
304 static int hf_pgm_spm_path = -1;
305 static int hf_pgm_data_sqn = -1;
306 static int hf_pgm_data_trail = -1;
307 static int hf_pgm_nak_sqn = -1;
308 static int hf_pgm_nak_srcafi = -1;
309 static int hf_pgm_nak_srcres = -1;
310 static int hf_pgm_nak_src = -1;
311 static int hf_pgm_nak_grpafi = -1;
312 static int hf_pgm_nak_grpres = -1;
313 static int hf_pgm_nak_grp = -1;
314 static int hf_pgm_ack_sqn = -1;
315 static int hf_pgm_ack_bitmap = -1;
316
317 static int hf_pgm_opt_type = -1;
318 static int hf_pgm_opt_len = -1;
319 static int hf_pgm_opt_tlen = -1;
320
321 static int hf_pgm_genopt_type = -1;
322 static int hf_pgm_genopt_len = -1;
323 static int hf_pgm_genopt_opx = -1;
324
325 static int hf_pgm_opt_join_res = -1;
326 static int hf_pgm_opt_join_minjoin = -1;
327
328 static int hf_pgm_opt_parity_prm_po = -1;
329 static int hf_pgm_opt_parity_prm_prmtgsz = -1;
330
331 static int hf_pgm_opt_parity_grp_res = -1;
332 static int hf_pgm_opt_parity_grp_prmgrp = -1;
333
334 #ifdef PGM_UNUSED_HANDLES
335 static int hf_pgm_opt_curr_tgsize_type = -1;
336 static int hf_pgm_opt_curr_tgsize_len = -1;
337 static int hf_pgm_opt_curr_tgsize_opx = -1;
338 static int hf_pgm_opt_curr_tgsize_res = -1;
339 static int hf_pgm_opt_curr_tgsize_prmatgsz = -1;
340 #endif
341
342 static int hf_pgm_opt_nak_res = -1;
343 static int hf_pgm_opt_nak_list = -1;
344
345 static int hf_pgm_opt_ccdata_res = -1;
346 static int hf_pgm_opt_ccdata_tsp = -1;
347 static int hf_pgm_opt_ccdata_afi = -1;
348 static int hf_pgm_opt_ccdata_res2 = -1;
349 static int hf_pgm_opt_ccdata_acker = -1;
350
351 static int hf_pgm_opt_ccfeedbk_res = -1;
352 static int hf_pgm_opt_ccfeedbk_tsp = -1;
353 static int hf_pgm_opt_ccfeedbk_afi = -1;
354 static int hf_pgm_opt_ccfeedbk_lossrate = -1;
355 static int hf_pgm_opt_ccfeedbk_acker = -1;
356
357 static dissector_table_t subdissector_table;
358 static heur_dissector_list_t heur_subdissector_list;
359 static dissector_handle_t data_handle;
360
361 /*
362  * As of the time this comment was typed
363  *
364  *      http://search.ietf.org/internet-drafts/draft-speakman-pgm-spec-06.txt
365  *
366  * was the URL for the PGM draft.
367  */
368
369 static char *
370 optsstr(nchar_t opts)
371 {
372         static char msg[256];
373         char *p = msg, *str;
374
375         if (opts == 0)
376                 return("");
377
378         if (opts & PGM_OPT){
379                 sprintf(p, "Present");
380                 p += strlen("Present");
381         }
382         if (opts & PGM_OPT_NETSIG){
383                 if (p != msg)
384                         str = ",NetSig";
385                 else
386                         str = "NetSig";
387                 sprintf(p, str);
388                 p += strlen(str);
389         }
390         if (opts & PGM_OPT_VAR_PKTLEN){
391                 if (p != msg)
392                         str = ",VarLen";
393                 else
394                         str = "VarLen";
395                 sprintf(p, str);
396                 p += strlen(str);
397         }
398         if (opts & PGM_OPT_PARITY){
399                 if (p != msg)
400                         str = ",Parity";
401                 else
402                         str = "Parity";
403                 sprintf(p, str);
404                 p += strlen(str);
405         }
406         if (p == msg) {
407                 sprintf(p, "0x%x", opts);
408         }
409         return(msg);
410 }
411 static char *
412 paritystr(nchar_t parity)
413 {
414         static char msg[256];
415         char *p = msg, *str;
416
417         if (parity == 0)
418                 return("");
419
420         if (parity & PGM_OPT_PARITY_PRM_PRO){
421                 sprintf(p, "Pro-active");
422                 p += strlen("Pro-active");
423         }
424         if (parity & PGM_OPT_PARITY_PRM_OND){
425                 if (p != msg)
426                         str = ",On-demand";
427                 else
428                         str = "On-demand";
429                 sprintf(p, str);
430                 p += strlen(str);
431         }
432         if (p == msg) {
433                 sprintf(p, "0x%x", parity);
434         }
435         return(msg);
436 }
437
438 static const value_string opt_vals[] = {
439         { PGM_OPT_LENGTH,      "Length" },
440         { PGM_OPT_END,         "End" },
441         { PGM_OPT_FRAGMENT,    "Fragment" },
442         { PGM_OPT_NAK_LIST,    "NakList" },
443         { PGM_OPT_JOIN,        "Join" },
444         { PGM_OPT_REDIRECT,    "ReDirect" },
445         { PGM_OPT_SYN,         "Syn" },
446         { PGM_OPT_FIN,         "Fin" },
447         { PGM_OPT_RST,         "Rst" },
448         { PGM_OPT_PARITY_PRM,  "ParityPrm" },
449         { PGM_OPT_PARITY_GRP,  "ParityGrp" },
450         { PGM_OPT_CURR_TGSIZE, "CurrTgsiz" },
451         { PGM_OPT_PGMCC_DATA,  "CcData" },
452         { PGM_OPT_PGMCC_FEEDBACK, "CcFeedBack" },
453         { 0,                   NULL }
454 };
455
456 static const value_string opx_vals[] = {
457         { PGM_OPX_IGNORE,  "Ignore" },
458         { PGM_OPX_INVAL,   "Inval" },
459         { PGM_OPX_DISCARD, "DisCard" },
460         { 0,               NULL }
461 };
462
463 static void
464 dissect_pgmopts(tvbuff_t *tvb, int offset, proto_tree *tree,
465     const char *pktname)
466 {
467         proto_item *tf;
468         proto_tree *opts_tree = NULL;
469         proto_tree *opt_tree = NULL;
470         pgm_opt_length_t opts;
471         pgm_opt_generic_t genopts;
472         int theend = 0, firsttime = 1;
473
474         tvb_memcpy(tvb, (guint8 *)&opts, offset, sizeof(opts));
475         opts.total_len = g_ntohs(opts.total_len);
476
477         tf = proto_tree_add_text(tree, tvb, offset,
478                 opts.total_len,
479                 "%s Options (Total Length %d)", pktname, opts.total_len);
480         opts_tree = proto_item_add_subtree(tf, ett_pgm_opts);
481         proto_tree_add_uint(opts_tree, hf_pgm_opt_type, tvb,
482                 offset, 1, opts.type);
483         proto_tree_add_uint(opts_tree, hf_pgm_opt_len, tvb,
484                 offset+1, 1, opts.len);
485         proto_tree_add_uint(opts_tree, hf_pgm_opt_tlen, tvb,
486                 offset+2, 2, opts.total_len);
487
488         offset += 4;
489         for (opts.total_len -= 4; opts.total_len > 0;){
490                 tvb_memcpy(tvb, (guint8 *)&genopts, offset, sizeof(genopts));
491                 if (genopts.type & PGM_OPT_END)  {
492                         genopts.type &= ~PGM_OPT_END;
493                         theend = 1;
494                 }
495                 tf = proto_tree_add_text(opts_tree, tvb, offset, genopts.len,
496                         "Option: %s, Length: %u",
497                         val_to_str(genopts.type, opt_vals, "Unknown (0x%02x)"),
498                         genopts.len);
499                 if (genopts.len == 0)
500                         break;
501
502                 switch(genopts.type) {
503                 case PGM_OPT_JOIN:{
504                         pgm_opt_join_t optdata;
505
506                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
507                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_join);
508
509                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
510                                 tvb, offset, 1, genopts.type);
511
512                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
513                                 offset+1, 1, genopts.len);
514
515                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx, tvb,
516                                 offset+2, 1, genopts.opx);
517
518                         proto_tree_add_uint(opt_tree, hf_pgm_opt_join_res, tvb,
519                                 offset+3, 1, optdata.res);
520
521                         proto_tree_add_uint(opt_tree, hf_pgm_opt_join_minjoin, tvb,
522                                 offset+4, 4, g_ntohl(optdata.opt_join_min));
523
524                         break;
525                 }
526                 case PGM_OPT_PARITY_PRM:{
527                         pgm_opt_parity_prm_t optdata;
528
529                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
530                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_parityprm);
531
532                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
533                                 tvb, offset, 1, genopts.type);
534
535                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
536                                 offset+1, 1, genopts.len);
537
538                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
539                                 tvb, offset+2, 1, genopts.opx);
540
541                         proto_tree_add_uint_format(opt_tree, hf_pgm_opt_parity_prm_po, tvb,
542                                 offset+3, 1, optdata.po, "Parity Parameters: %s (0x%x)",
543                                 paritystr(optdata.po), optdata.po);
544
545                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_prm_prmtgsz,
546                                 tvb, offset+4, 4, g_ntohl(optdata.prm_tgsz));
547
548                         break;
549                 }
550                 case PGM_OPT_PARITY_GRP:{
551                         pgm_opt_parity_grp_t optdata;
552
553                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
554                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_paritygrp);
555
556                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
557                                 tvb, offset, 1, genopts.type);
558
559                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
560                                 offset+1, 1, genopts.len);
561
562                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
563                                 tvb, offset+2, 1, genopts.opx);
564
565                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_grp_res, tvb,
566                                 offset+3, 1, optdata.res);
567
568                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_grp_prmgrp,
569                                 tvb, offset+4, 4, g_ntohl(optdata.prm_grp));
570
571                         break;
572                 }
573                 case PGM_OPT_NAK_LIST:{
574                         pgm_opt_nak_list_t optdata;
575                         nlong_t naklist[PGM_MAX_NAK_LIST_SZ+1];
576                         char nakbuf[8192], *ptr;
577                         int i, j, naks, soffset = 0;
578
579                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
580                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_naklist);
581
582                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type, tvb,
583                                 offset, 1, genopts.type);
584
585                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
586                                 offset+1, 1, genopts.len);
587
588                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
589                                 tvb, offset+2, 1, genopts.opx);
590
591                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_res, tvb,
592                                 offset+3, 1, optdata.res);
593
594                         optdata.len -= sizeof(pgm_opt_nak_list_t);
595                         tvb_memcpy(tvb, (guint8 *)naklist, offset+4, optdata.len);
596                         naks = (optdata.len/sizeof(nlong_t));
597                         ptr = nakbuf;
598                         j = 0;
599                         /*
600                          * Print out 8 per line
601                          */
602                         for (i=0; i < naks; i++) {
603                                 sprintf(nakbuf+soffset, "0x%lx ",
604                                     (unsigned long)g_ntohl(naklist[i]));
605                                 soffset = strlen(nakbuf);
606                                 if ((++j % 8) == 0) {
607                                         if (firsttime) {
608                                                 proto_tree_add_bytes_format(opt_tree,
609                                                         hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
610                                                         nakbuf, "List(%d): %s", naks, nakbuf);
611                                                         soffset = 0;
612                                         } else {
613                                                 proto_tree_add_bytes_format(opt_tree,
614                                                         hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
615                                                         nakbuf, "List: %s", nakbuf);
616                                                         soffset = 0;
617                                         }
618                                         firsttime = 0;
619                                 }
620                         }
621                         if (soffset) {
622                                 if (firsttime) {
623                                         proto_tree_add_bytes_format(opt_tree,
624                                                 hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
625                                                 nakbuf, "List(%d): %s", naks, nakbuf);
626                                                 soffset = 0;
627                                 } else {
628                                         proto_tree_add_bytes_format(opt_tree,
629                                                 hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
630                                                 nakbuf, "List: %s", nakbuf);
631                                                 soffset = 0;
632                                 }
633                         }
634                         break;
635                 }
636                 case PGM_OPT_PGMCC_DATA:{
637                         pgm_opt_pgmcc_data_t optdata;
638
639                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
640                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_ccdata);
641
642                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
643                                 tvb, offset, 1, genopts.type);
644
645                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
646                                 offset+1, 1, genopts.len);
647
648                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
649                                 tvb, offset+2, 1, genopts.opx);
650
651                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_res, tvb,
652                                 offset+3, 1, optdata.res);
653
654                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_tsp, tvb,
655                                 offset+4, 4, optdata.tsp);
656
657                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_afi, tvb,
658                                 offset+8, 2, g_ntohs(optdata.acker_afi));
659
660                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_res2, tvb,
661                                 offset+10, 2, g_ntohs(optdata.res2));
662
663                         switch (g_ntohs(optdata.acker_afi)) {
664
665                         case AFNUM_INET:
666                                 proto_tree_add_ipv4(opt_tree, hf_pgm_opt_ccdata_acker,
667                                     tvb, offset+12, 4, optdata.acker);
668                                 break;
669
670                         default:
671                                 /*
672                                  * XXX - the header is variable-length,
673                                  * as the length of the NLA depends on
674                                  * its AFI.
675                                  *
676                                  * However, our structure for it is
677                                  * fixed-length, and assumes it's a 4-byte
678                                  * IPv4 address.
679                                  */
680                                 break;
681                         }
682
683                         break;
684                 }
685                 case PGM_OPT_PGMCC_FEEDBACK:{
686                         pgm_opt_pgmcc_feedback_t optdata;
687
688                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
689                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_ccdata);
690
691                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
692                                 tvb, offset, 1, genopts.type);
693
694                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
695                                 offset+1, 1, genopts.len);
696
697                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
698                                 tvb, offset+2, 1, genopts.opx);
699
700                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_res, tvb,
701                                 offset+3, 1, optdata.res);
702
703                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_tsp, tvb,
704                                 offset+4, 4, optdata.tsp);
705
706                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_afi, tvb,
707                                 offset+8, 2, g_ntohs(optdata.acker_afi));
708
709                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_lossrate, tvb,
710                                 offset+10, 2, g_ntohs(optdata.loss_rate));
711
712                         switch (g_ntohs(optdata.acker_afi)) {
713
714                         case AFNUM_INET:
715                                 proto_tree_add_ipv4(opt_tree, hf_pgm_opt_ccfeedbk_acker,
716                                     tvb, offset+12, 4, optdata.acker);
717                                 break;
718
719                         default:
720                                 /*
721                                  * XXX - the header is variable-length,
722                                  * as the length of the NLA depends on
723                                  * its AFI.
724                                  *
725                                  * However, our structure for it is
726                                  * fixed-length, and assumes it's a 4-byte
727                                  * IPv4 address.
728                                  */
729                                 break;
730                         }
731
732                         break;
733                 }
734                 }
735                 offset += genopts.len;
736                 opts.total_len -= genopts.len;
737
738         }
739         return ;
740 }
741
742 static const value_string type_vals[] = {
743         { PGM_SPM_PCKT,   "SPM" },
744         { PGM_RDATA_PCKT, "RDATA" },
745         { PGM_ODATA_PCKT, "ODATA" },
746         { PGM_NAK_PCKT,   "NAK" },
747         { PGM_NNAK_PCKT,  "NNAK" },
748         { PGM_NCF_PCKT,   "NCF" },
749         { PGM_ACK_PCKT,   "ACK" },
750         { 0,              NULL }
751 };
752 /* Determine if there is a sub-dissector and call it.  This has been */
753 /* separated into a stand alone routine to other protocol dissectors */
754 /* can call to it, ie. socks    */
755
756 void
757 decode_pgm_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
758         proto_tree *tree, pgm_type *pgmhdr)
759 {
760   tvbuff_t *next_tvb;
761   int found = 0;
762
763   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
764
765   /* do lookup with the subdissector table */
766   found = dissector_try_port(subdissector_table, pgmhdr->sport,
767                         next_tvb, pinfo, tree);
768   if (found)
769         return;
770
771   found = dissector_try_port(subdissector_table, pgmhdr->dport,
772                         next_tvb, pinfo, tree);
773   if (found)
774         return;
775
776   /* do lookup with the heuristic subdissector table */
777   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
778     return;
779
780   /* Oh, well, we don't know this; dissect it as data. */
781   call_dissector(data_handle,next_tvb, pinfo, tree);
782
783 }
784 int
785 total_size(tvbuff_t *tvb, pgm_type *hdr)
786 {
787         int bytes = sizeof(pgm_type);
788         pgm_opt_length_t opts;
789
790         switch(hdr->type) {
791         case PGM_SPM_PCKT:
792                 bytes += sizeof(pgm_spm_t);
793                 break;
794
795         case PGM_RDATA_PCKT:
796         case PGM_ODATA_PCKT:
797                 bytes += sizeof(pgm_data_t);
798                 break;
799
800         case PGM_NAK_PCKT:
801         case PGM_NNAK_PCKT:
802         case PGM_NCF_PCKT:
803                 bytes += sizeof(pgm_nak_t);
804                 break;
805         case PGM_ACK_PCKT:
806                 bytes += sizeof(pgm_ack_t);
807                 break;
808         }
809         if ((hdr->opts & PGM_OPT)) {
810                 tvb_memcpy(tvb, (guint8 *)&opts, bytes, sizeof(opts));
811                 bytes += g_ntohs(opts.total_len);
812         }
813         return(bytes);
814 }
815 /*
816  * dissect_pgm - The dissector for Pragmatic General Multicast
817  */
818 static void
819 dissect_pgm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
820 {
821         proto_tree *pgm_tree = NULL;
822         proto_tree *opt_tree = NULL;
823         proto_tree *type_tree = NULL;
824         proto_item *tf;
825         pgm_type pgmhdr;
826         pgm_spm_t spm;
827         pgm_data_t data;
828         pgm_nak_t nak;
829         pgm_ack_t ack;
830         int offset = 0;
831         guint hlen, plen;
832         proto_item *ti;
833         const char *pktname;
834         char *gsi;
835         int isdata = 0;
836
837         if (check_col(pinfo->cinfo, COL_PROTOCOL))
838                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PGM");
839
840         /* Clear out the Info column. */
841         if (check_col(pinfo->cinfo, COL_INFO))
842                 col_clear(pinfo->cinfo, COL_INFO);
843
844         tvb_memcpy(tvb, (guint8 *)&pgmhdr, offset, sizeof(pgm_type));
845         hlen = sizeof(pgm_type);
846         pgmhdr.sport = g_ntohs(pgmhdr.sport);
847         pgmhdr.dport = g_ntohs(pgmhdr.dport);
848         pgmhdr.tsdulen = g_ntohs(pgmhdr.tsdulen);
849
850         pktname = val_to_str(pgmhdr.type, type_vals, "Unknown (0x%02x)");
851
852         gsi = bytes_to_str(pgmhdr.gsi, 6);
853         switch(pgmhdr.type) {
854         case PGM_SPM_PCKT:
855                 plen = sizeof(pgm_spm_t);
856                 tvb_memcpy(tvb, (guint8 *)&spm, sizeof(pgm_type), plen);
857                 spm_ntoh(&spm);
858                 if (check_col(pinfo->cinfo, COL_INFO)) {
859                         col_add_fstr(pinfo->cinfo, COL_INFO,
860                                 "%-5s sqn 0x%x gsi %s", pktname, spm.sqn, gsi);
861                 }
862                 break;
863
864         case PGM_RDATA_PCKT:
865         case PGM_ODATA_PCKT:
866                 plen = sizeof(pgm_data_t);
867                 tvb_memcpy(tvb, (guint8 *)&data, sizeof(pgm_type), plen);
868                 data_ntoh(&data);
869                 if (check_col(pinfo->cinfo, COL_INFO)) {
870                         col_add_fstr(pinfo->cinfo, COL_INFO,
871                             "%-5s sqn 0x%x gsi %s tsdulen %d", pktname, data.sqn, gsi,
872                             pgmhdr.tsdulen);
873                 }
874                 isdata = 1;
875                 break;
876
877         case PGM_NAK_PCKT:
878         case PGM_NNAK_PCKT:
879         case PGM_NCF_PCKT:
880                 plen = sizeof(pgm_nak_t);
881                 tvb_memcpy(tvb, (guint8 *)&nak, sizeof(pgm_type), plen);
882                 nak_ntoh(&nak);
883                 if (check_col(pinfo->cinfo, COL_INFO)) {
884                         col_add_fstr(pinfo->cinfo, COL_INFO,
885                                 "%-5s sqn 0x%x gsi %s", pktname, nak.sqn, gsi);
886                 }
887                 break;
888         case PGM_ACK_PCKT:
889                 plen = sizeof(pgm_ack_t);
890                 tvb_memcpy(tvb, (guint8 *)&ack, sizeof(pgm_type), plen);
891                 ack_ntoh(&ack);
892                 if (check_col(pinfo->cinfo, COL_INFO)) {
893                         col_add_fstr(pinfo->cinfo, COL_INFO,
894                             "%-5s sqn 0x%x gsi %s", pktname, ack.rx_max_sqn, gsi);
895                 }
896                 break;
897
898         default:
899                 return;
900         }
901
902         if (tree) {
903                 ti = proto_tree_add_protocol_format(tree, proto_pgm,
904                         tvb, offset, total_size(tvb, &pgmhdr),
905                         "Pragmatic General Multicast: Type %s"
906                             " SrcPort %u, DstPort %u, GSI %s", pktname,
907                         pgmhdr.sport, pgmhdr.dport,
908                         bytes_to_str(pgmhdr.gsi, 6));
909
910                 pgm_tree = proto_item_add_subtree(ti, ett_pgm);
911                 proto_tree_add_uint(pgm_tree, hf_pgm_main_sport, tvb, offset, 2,
912                         pgmhdr.sport);
913                 proto_tree_add_uint(pgm_tree, hf_pgm_main_dport, tvb, offset+2,
914                         2, pgmhdr.dport);
915                 proto_tree_add_uint(pgm_tree, hf_pgm_main_type, tvb,
916                         offset+4, 1, pgmhdr.type);
917
918                 tf = proto_tree_add_uint_format(pgm_tree, hf_pgm_main_opts, tvb,
919                         offset+5, 1, pgmhdr.opts, "Options: %s (0x%x)",
920                         optsstr(pgmhdr.opts), pgmhdr.opts);
921                 opt_tree = proto_item_add_subtree(tf, ett_pgm_optbits);
922
923                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_opt, tvb,
924                         offset+5, 1, (pgmhdr.opts & PGM_OPT));
925                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_netsig, tvb,
926                         offset+5, 1, (pgmhdr.opts & PGM_OPT_NETSIG));
927                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_varlen, tvb,
928                         offset+5, 1, (pgmhdr.opts & PGM_OPT_VAR_PKTLEN));
929                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_parity, tvb,
930                         offset+5, 1, (pgmhdr.opts & PGM_OPT_PARITY));
931
932                 proto_tree_add_uint(pgm_tree, hf_pgm_main_cksum, tvb, offset+6,
933                         2, pgmhdr.cksum);
934                 proto_tree_add_bytes(pgm_tree, hf_pgm_main_gsi, tvb, offset+8,
935                         6, pgmhdr.gsi);
936                 proto_tree_add_uint(pgm_tree, hf_pgm_main_tsdulen, tvb,
937                         offset+14, 2, pgmhdr.tsdulen);
938
939                 offset = sizeof(pgm_type);
940                 tf = proto_tree_add_text(pgm_tree, tvb, offset, plen, "%s Packet",
941                         pktname);
942                 switch(pgmhdr.type) {
943                 case PGM_SPM_PCKT:
944                         type_tree = proto_item_add_subtree(tf, ett_pgm_spm);
945
946                         proto_tree_add_uint(type_tree, hf_pgm_spm_sqn, tvb,
947                                 offset, 4, spm.sqn);
948                         proto_tree_add_uint(type_tree, hf_pgm_spm_trail, tvb,
949                                 offset+4, 4, spm.trail);
950                         proto_tree_add_uint(type_tree, hf_pgm_spm_lead, tvb,
951                                 offset+8, 4, spm.lead);
952                         proto_tree_add_uint(type_tree, hf_pgm_spm_pathafi, tvb,
953                                 offset+10, 2, spm.path_afi);
954                         proto_tree_add_uint(type_tree, hf_pgm_spm_res, tvb,
955                                 offset+12, 2, spm.res);
956                         switch (spm.path_afi) {
957
958                         case AFNUM_INET:
959                                 proto_tree_add_ipv4(type_tree, hf_pgm_spm_path,
960                                     tvb, offset+14, 4, spm.path);
961                                 break;
962
963                         default:
964                                 /*
965                                  * XXX - the header is variable-length,
966                                  * as the length of the NLA depends on
967                                  * its AFI.
968                                  *
969                                  * However, our structure for it is
970                                  * fixed-length, and assumes it's a 4-byte
971                                  * IPv4 address.
972                                  */
973                                 return;
974                         }
975
976                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
977                                 break;
978                         offset += plen;
979
980                         dissect_pgmopts(tvb, offset, type_tree, pktname);
981
982                         break;
983
984                 case PGM_RDATA_PCKT:
985                 case PGM_ODATA_PCKT: {
986                         type_tree = proto_item_add_subtree(tf, ett_pgm_data);
987
988                         proto_tree_add_uint(type_tree, hf_pgm_spm_sqn, tvb,
989                                 offset, 4, data.sqn);
990                         proto_tree_add_uint(type_tree, hf_pgm_spm_trail, tvb,
991                                 offset+4, 4, data.trail);
992
993                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
994                                 break;
995                         offset += plen;
996
997                         dissect_pgmopts(tvb, offset, type_tree, pktname);
998
999                         break;
1000                 }
1001
1002
1003                 case PGM_NAK_PCKT:
1004                 case PGM_NNAK_PCKT:
1005                 case PGM_NCF_PCKT:
1006                         type_tree = proto_item_add_subtree(tf, ett_pgm_nak);
1007
1008                         proto_tree_add_uint(type_tree, hf_pgm_nak_sqn, tvb,
1009                                 offset, 4, nak.sqn);
1010                         proto_tree_add_uint(type_tree, hf_pgm_nak_srcafi, tvb,
1011                                 offset+4, 2, nak.src_afi);
1012                         proto_tree_add_uint(type_tree, hf_pgm_nak_srcres, tvb,
1013                                 offset+6, 2, nak.src_res);
1014
1015                         switch (nak.src_afi) {
1016
1017                         case AFNUM_INET:
1018                                 proto_tree_add_ipv4(type_tree, hf_pgm_nak_src,
1019                                     tvb, offset+8, 4, nak.src);
1020                                 break;
1021
1022                         default:
1023                                 /*
1024                                  * XXX - the header is variable-length,
1025                                  * as the length of the NLA depends on
1026                                  * its AFI.
1027                                  *
1028                                  * However, our structure for it is
1029                                  * fixed-length, and assumes it's a 4-byte
1030                                  * IPv4 address.
1031                                  */
1032                                 break;
1033                         }
1034
1035                         proto_tree_add_uint(type_tree, hf_pgm_nak_grpafi, tvb,
1036                                 offset+12, 2, nak.grp_afi);
1037                         proto_tree_add_uint(type_tree, hf_pgm_nak_grpres, tvb,
1038                                 offset+14, 2, nak.grp_res);
1039
1040                         switch (nak.grp_afi) {
1041
1042                         case AFNUM_INET:
1043                                 proto_tree_add_ipv4(type_tree, hf_pgm_nak_grp,
1044                                     tvb, offset+16, 4, nak.grp);
1045                                 break;
1046
1047                         default:
1048                                 /*
1049                                  * XXX - the header is variable-length,
1050                                  * as the length of the NLA depends on
1051                                  * its AFI.
1052                                  *
1053                                  * However, our structure for it is
1054                                  * fixed-length, and assumes it's a 4-byte
1055                                  * IPv4 address.
1056                                  */
1057                                 return;
1058                         }
1059
1060                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1061                                 break;
1062                         offset += plen;
1063
1064                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1065
1066                         break;
1067                 case PGM_ACK_PCKT:
1068                         type_tree = proto_item_add_subtree(tf, ett_pgm_ack);
1069
1070                         proto_tree_add_uint(type_tree, hf_pgm_ack_sqn, tvb,
1071                                 offset, 4, ack.rx_max_sqn);
1072                         proto_tree_add_uint(type_tree, hf_pgm_ack_bitmap, tvb,
1073                                 offset+4, 4, ack.bitmap);
1074
1075                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1076                                 break;
1077                         offset += plen;
1078
1079                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1080
1081                         break;
1082                 }
1083
1084         }
1085         if (isdata) {
1086                 /*
1087                  * Now see if there are any sub-dissectors, if so call them
1088                  */
1089                 offset = total_size(tvb, &pgmhdr);
1090                 decode_pgm_ports(tvb, offset, pinfo, tree, &pgmhdr);
1091         }
1092         pktname = NULL;
1093 }
1094 static const true_false_string opts_present = {
1095         "Present",
1096         "Not Present"
1097 };
1098
1099 /* Register all the bits needed with the filtering engine */
1100 void
1101 proto_register_pgm(void)
1102 {
1103   static hf_register_info hf[] = {
1104     { &hf_pgm_main_sport,
1105       { "Source Port", "pgm.hdr.sport", FT_UINT16, BASE_DEC,
1106           NULL, 0x0, "", HFILL }},
1107     { &hf_pgm_main_dport,
1108       { "Destination Port", "pgm.hdr.dport", FT_UINT16, BASE_DEC,
1109           NULL, 0x0, "", HFILL }},
1110     { &hf_pgm_main_type,
1111       { "Type", "pgm.hdr.type", FT_UINT8, BASE_HEX,
1112           VALS(type_vals), 0x0, "", HFILL }},
1113     { &hf_pgm_main_opts,
1114       { "Options", "pgm.hdr.opts", FT_UINT8, BASE_HEX,
1115           NULL, 0x0, "", HFILL }},
1116     { &hf_pgm_main_opts_opt,
1117       { "Options", "pgm.hdr.opts.opt", FT_BOOLEAN, BASE_NONE,
1118           TFS(&opts_present), PGM_OPT, "", HFILL }},
1119     { &hf_pgm_main_opts_netsig,
1120       { "Network Significant Options", "pgm.hdr.opts.netsig",
1121           FT_BOOLEAN, BASE_NONE,
1122           TFS(&opts_present), PGM_OPT_NETSIG, "", HFILL }},
1123     { &hf_pgm_main_opts_varlen,
1124       { "Variable length Parity Packet Option", "pgm.hdr.opts.varlen",
1125           FT_BOOLEAN, BASE_NONE,
1126           TFS(&opts_present), PGM_OPT_VAR_PKTLEN, "", HFILL }},
1127     { &hf_pgm_main_opts_parity,
1128       { "Parity", "pgm.hdr.opts.parity", FT_BOOLEAN, BASE_NONE,
1129           TFS(&opts_present), PGM_OPT_PARITY, "", HFILL }},
1130     { &hf_pgm_main_cksum,
1131       { "Checksum", "pgm.hdr.cksum", FT_UINT16, BASE_HEX,
1132         NULL, 0x0, "", HFILL }},
1133     { &hf_pgm_main_gsi,
1134       { "Global Source Identifier", "pgm.hdr.gsi", FT_BYTES, BASE_HEX,
1135           NULL, 0x0, "", HFILL }},
1136     { &hf_pgm_main_tsdulen,
1137       { "Transport Service Data Unit Length", "pgm.hdr.tsdulen", FT_UINT16,
1138           BASE_DEC, NULL, 0x0, "", HFILL }},
1139     { &hf_pgm_spm_sqn,
1140       { "Sequence number", "pgm.spm.sqn", FT_UINT32, BASE_HEX,
1141           NULL, 0x0, "", HFILL }},
1142     { &hf_pgm_spm_trail,
1143       { "Trailing Edge Sequence Number", "pgm.spm.trail", FT_UINT32, BASE_HEX,
1144           NULL, 0x0, "", HFILL }},
1145     { &hf_pgm_spm_lead,
1146       { "Leading Edge Sequence Number", "pgm.spm.lead", FT_UINT32, BASE_HEX,
1147           NULL, 0x0, "", HFILL }},
1148     { &hf_pgm_spm_pathafi,
1149       { "Path NLA AFI", "pgm.spm.pathafi", FT_UINT16, BASE_DEC,
1150           VALS(afn_vals), 0x0, "", HFILL }},
1151     { &hf_pgm_spm_res,
1152       { "Reserved", "pgm.spm.res", FT_UINT16, BASE_HEX,
1153           NULL, 0x0, "", HFILL }},
1154     { &hf_pgm_spm_path,
1155       { "Path NLA", "pgm.spm.path", FT_IPv4, BASE_NONE,
1156           NULL, 0x0, "", HFILL }},
1157     { &hf_pgm_data_sqn,
1158       { "Data Packet Sequence Number", "pgm.data.sqn", FT_UINT32, BASE_HEX,
1159           NULL, 0x0, "", HFILL }},
1160     { &hf_pgm_data_trail,
1161       { "Trailing Edge Sequence Number", "pgm.data.trail", FT_UINT32, BASE_HEX,
1162           NULL, 0x0, "", HFILL }},
1163     { &hf_pgm_nak_sqn,
1164       { "Requested Sequence Number", "pgm.nak.sqn", FT_UINT32, BASE_HEX,
1165           NULL, 0x0, "", HFILL }},
1166     { &hf_pgm_nak_srcafi,
1167       { "Source NLA AFI", "pgm.nak.srcafi", FT_UINT16, BASE_DEC,
1168           VALS(afn_vals), 0x0, "", HFILL }},
1169     { &hf_pgm_nak_srcres,
1170       { "Reserved", "pgm.nak.srcres", FT_UINT16, BASE_HEX,
1171           NULL, 0x0, "", HFILL }},
1172     { &hf_pgm_nak_src,
1173       { "Source NLA", "pgm.nak.src", FT_IPv4, BASE_NONE,
1174           NULL, 0x0, "", HFILL }},
1175     { &hf_pgm_nak_grpafi,
1176       { "Multicast Group AFI", "pgm.nak.grpafi", FT_UINT16, BASE_DEC,
1177           VALS(afn_vals), 0x0, "", HFILL }},
1178     { &hf_pgm_nak_grpres,
1179       { "Reserved", "pgm.nak.grpres", FT_UINT16, BASE_HEX,
1180           NULL, 0x0, "", HFILL }},
1181     { &hf_pgm_nak_grp,
1182       { "Multicast Group NLA", "pgm.nak.grp", FT_IPv4, BASE_NONE,
1183           NULL, 0x0, "", HFILL }},
1184     { &hf_pgm_ack_sqn,
1185       { "Maximum Received Sequence Number", "pgm.ack.maxsqn", FT_UINT32,
1186           BASE_HEX, NULL, 0x0, "", HFILL }},
1187     { &hf_pgm_ack_bitmap,
1188       { "Packet Bitmap", "pgm.ack.bitmap", FT_UINT32, BASE_HEX,
1189           NULL, 0x0, "", HFILL }},
1190     { &hf_pgm_opt_type,
1191       { "Type", "pgm.opts.type", FT_UINT8, BASE_HEX,
1192           VALS(opt_vals), 0x0, "", HFILL }},
1193     { &hf_pgm_opt_len,
1194       { "Length", "pgm.opts.len", FT_UINT8, BASE_DEC,
1195           NULL, 0x0, "", HFILL }},
1196     { &hf_pgm_opt_tlen,
1197       { "Total Length", "pgm.opts.tlen", FT_UINT16, BASE_DEC,
1198           NULL, 0x0, "", HFILL }},
1199     { &hf_pgm_genopt_type,
1200       { "Type", "pgm.genopts.type", FT_UINT8, BASE_HEX,
1201           VALS(opt_vals), 0x0, "", HFILL }},
1202     { &hf_pgm_genopt_len,
1203       { "Length", "pgm.genopts.len", FT_UINT8, BASE_DEC,
1204           NULL, 0x0, "", HFILL }},
1205     { &hf_pgm_genopt_opx,
1206       { "Option Extensibility Bits", "pgm.genopts.opx", FT_UINT8, BASE_HEX,
1207           VALS(opx_vals), 0x0, "", HFILL }},
1208     { &hf_pgm_opt_parity_prm_po,
1209       { "Parity Parameters", "pgm.opts.parity_prm.op", FT_UINT8, BASE_HEX,
1210           NULL, 0x0, "", HFILL }},
1211     { &hf_pgm_opt_parity_prm_prmtgsz,
1212       { "Transmission Group Size", "pgm.opts.parity_prm.prm_grp",
1213           FT_UINT32, BASE_HEX,
1214           NULL, 0x0, "", HFILL }},
1215     { &hf_pgm_opt_join_res,
1216       { "Reserved", "pgm.opts.join.res", FT_UINT8, BASE_HEX,
1217           NULL, 0x0, "", HFILL }},
1218     { &hf_pgm_opt_join_minjoin,
1219       { "Minimum Sequence Number", "pgm.opts.join.min_join",
1220           FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
1221     { &hf_pgm_opt_parity_grp_res,
1222       { "Reserved", "pgm.opts.parity_prm.op", FT_UINT8, BASE_HEX,
1223           NULL, 0x0, "", HFILL }},
1224     { &hf_pgm_opt_parity_grp_prmgrp,
1225       { "Transmission Group Size", "pgm.opts.parity_prm.prm_grp",
1226           FT_UINT32, BASE_HEX,
1227           NULL, 0x0, "", HFILL }},
1228     { &hf_pgm_opt_nak_res,
1229       { "Reserved", "pgm.opts.nak.op", FT_UINT8, BASE_HEX,
1230           NULL, 0x0, "", HFILL }},
1231     { &hf_pgm_opt_nak_list,
1232       { "List", "pgm.opts.nak.list", FT_BYTES, BASE_NONE,
1233           NULL, 0x0, "", HFILL }},
1234     { &hf_pgm_opt_ccdata_res,
1235       { "Reserved", "pgm.opts.ccdata.res", FT_UINT8, BASE_DEC,
1236           NULL, 0x0, "", HFILL }},
1237     { &hf_pgm_opt_ccdata_tsp,
1238       { "Time Stamp", "pgm.opts.ccdata.tstamp", FT_UINT16, BASE_HEX,
1239           NULL, 0x0, "", HFILL }},
1240     { &hf_pgm_opt_ccdata_afi,
1241       { "Acker AFI", "pgm.opts.ccdata.afi", FT_UINT16, BASE_DEC,
1242           VALS(afn_vals), 0x0, "", HFILL }},
1243     { &hf_pgm_opt_ccdata_res2,
1244       { "Reserved", "pgm.opts.ccdata.res2", FT_UINT16, BASE_DEC,
1245           NULL, 0x0, "", HFILL }},
1246     { &hf_pgm_opt_ccdata_acker,
1247       { "Acker", "pgm.opts.ccdata.acker", FT_IPv4, BASE_NONE,
1248           NULL, 0x0, "", HFILL }},
1249     { &hf_pgm_opt_ccfeedbk_res,
1250       { "Reserved", "pgm.opts.ccdata.res", FT_UINT8, BASE_DEC,
1251           NULL, 0x0, "", HFILL }},
1252     { &hf_pgm_opt_ccfeedbk_tsp,
1253       { "Time Stamp", "pgm.opts.ccdata.tstamp", FT_UINT16, BASE_HEX,
1254           NULL, 0x0, "", HFILL }},
1255     { &hf_pgm_opt_ccfeedbk_afi,
1256       { "Acker AFI", "pgm.opts.ccdata.afi", FT_UINT16, BASE_DEC,
1257           VALS(afn_vals), 0x0, "", HFILL }},
1258     { &hf_pgm_opt_ccfeedbk_lossrate,
1259       { "Loss Rate", "pgm.opts.ccdata.lossrate", FT_UINT16, BASE_HEX,
1260           NULL, 0x0, "", HFILL }},
1261     { &hf_pgm_opt_ccfeedbk_acker,
1262       { "Acker", "pgm.opts.ccdata.acker", FT_IPv4, BASE_NONE,
1263           NULL, 0x0, "", HFILL }},
1264   };
1265   static gint *ett[] = {
1266     &ett_pgm,
1267         &ett_pgm_optbits,
1268         &ett_pgm_spm,
1269         &ett_pgm_data,
1270         &ett_pgm_nak,
1271         &ett_pgm_ack,
1272         &ett_pgm_opts,
1273         &ett_pgm_opts_join,
1274         &ett_pgm_opts_parityprm,
1275         &ett_pgm_opts_paritygrp,
1276         &ett_pgm_opts_naklist,
1277         &ett_pgm_opts_ccdata,
1278   };
1279   module_t *pgm_module;
1280
1281   proto_pgm = proto_register_protocol("Pragmatic General Multicast",
1282                                        "PGM", "pgm");
1283
1284   proto_register_field_array(proto_pgm, hf, array_length(hf));
1285   proto_register_subtree_array(ett, array_length(ett));
1286
1287         /* subdissector code */
1288   subdissector_table = register_dissector_table("pgm.port",
1289                 "PGM port", FT_UINT16, BASE_DEC);
1290   register_heur_dissector_list("pgm", &heur_subdissector_list);
1291
1292   /*
1293    * Register configuration preferences for UDP encapsulation
1294    * (Note: Initially the ports are set to zero so the
1295    *        dissecting of PGM encapsulated in UPD packets
1296    *        is off by default)
1297    */
1298    pgm_module = prefs_register_protocol(proto_pgm, proto_rereg_pgm);
1299
1300    prefs_register_uint_preference(pgm_module, "udp.encap_ucast_port",
1301                 "PGM Encap Unicast Port (standard is 3055)",
1302                 "PGM Encap is PGM packets encapsulated in UDP packets"
1303                 " (Note: This option is off, i.e. port is 0, by default)",
1304                 10, &udp_encap_ucast_port);
1305    old_encap_ucast_port = udp_encap_ucast_port;
1306
1307    prefs_register_uint_preference(pgm_module, "udp.encap_mcast_port",
1308                 "PGM Encap Multicast Port (standard is 3056)",
1309                 "PGM Encap is PGM packets encapsulated in UDP packets"
1310                 " (Note: This option is off, i.e. port is 0, by default)",
1311                 10, &udp_encap_mcast_port);
1312
1313    old_encap_mcast_port = udp_encap_mcast_port;
1314 }
1315
1316 static dissector_handle_t pgm_handle;
1317
1318 /* The registration hand-off routine */
1319 void
1320 proto_reg_handoff_pgm(void)
1321 {
1322   pgm_handle = create_dissector_handle(dissect_pgm, proto_pgm);
1323
1324   /*
1325    * Set up PGM Encap dissecting, which is off by default
1326    */
1327   dissector_add("udp.port", udp_encap_ucast_port, pgm_handle);
1328   dissector_add("udp.port", udp_encap_mcast_port, pgm_handle);
1329
1330   dissector_add("ip.proto", IP_PROTO_PGM, pgm_handle);
1331
1332   data_handle = find_dissector("data");
1333 }
1334
1335 static void
1336 proto_rereg_pgm(void)
1337 {
1338         /*
1339          * Remove the old ones
1340          */
1341         dissector_delete("udp.port", old_encap_ucast_port, pgm_handle);
1342         dissector_delete("udp.port", old_encap_mcast_port, pgm_handle);
1343
1344         /*
1345          * Set the new ones
1346          */
1347         dissector_add("udp.port", udp_encap_ucast_port, pgm_handle);
1348         dissector_add("udp.port", udp_encap_mcast_port, pgm_handle);
1349 }