"read_string()" can return a null pointer for the string, so check for
[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.20 2003/03/12 04:04:13 gerald 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 "in_cksum.h"
40 #include <epan/resolv.h>
41 #include <epan/strutil.h>
42 #include <epan/conversation.h>
43 #include "prefs.h"
44
45 #include <epan/proto.h>
46
47 /*
48  * Flag to control whether to check the PGM checksum.
49  */
50 static gboolean pgm_check_checksum = TRUE;
51
52 void proto_reg_handoff_pgm(void);
53 static void proto_rereg_pgm(void);
54
55 typedef guint8 nchar_t;
56 typedef guint16 nshort_t;
57 typedef guint32 nlong_t;
58
59 /* The PGM main header */
60 typedef struct {
61         nshort_t sport;            /* source port */
62         nshort_t dport;            /* destination port */
63         nchar_t type;              /* PGM type */
64         nchar_t opts;              /* options */
65         nshort_t cksum;            /* checksum */
66         nchar_t gsi[6];            /* Global Source ID */
67         nshort_t tsdulen;          /* TSDU length */
68 } pgm_type;
69 #define pgmhdr_ntoh(_p) \
70         (_p)->sport = g_ntohs((_p)->sport); \
71         (_p)->dport = g_ntohs((_p)->dport); \
72         (_p)->type = g_ntohs((_p)->type); \
73         (_p)->opts = g_ntohs((_p)->opts); \
74         (_p)->cksum = g_ntohs((_p)->cksum); \
75         (_p)->tsdulen = g_ntohs((_p)->tsdulen)
76
77 /* The PGM SPM header */
78 typedef struct {
79         nlong_t sqn;              /* SPM's sequence number */
80         nlong_t trail;            /* Trailing edge sequence number */
81         nlong_t lead;             /* Leading edge sequence number */
82         nshort_t path_afi;        /* NLA AFI */
83         nshort_t res;             /* reserved */
84         nlong_t path;             /* Path NLA */
85 } pgm_spm_t;
86 static const size_t PGM_SPM_SZ = sizeof(pgm_type)+sizeof(pgm_spm_t);
87 #define spm_ntoh(_p) \
88         (_p)->sqn = g_ntohl((_p)->sqn); \
89         (_p)->trail = g_ntohl((_p)->trail); \
90         (_p)->lead = g_ntohl((_p)->lead); \
91         (_p)->path_afi = g_ntohs((_p)->path_afi); \
92         (_p)->res = g_ntohs((_p)->res);
93
94 /* The PGM Data (ODATA/RDATA) header */
95 typedef struct {
96         nlong_t sqn;              /* Data Packet sequence number */
97         nlong_t trail;            /* Trailing edge sequence number */
98 } pgm_data_t;
99 #define data_ntoh(_p) \
100         (_p)->sqn = g_ntohl((_p)->sqn); \
101         (_p)->trail = g_ntohl((_p)->trail)
102 static const size_t PGM_DATA_HDR_SZ = sizeof(pgm_type)+sizeof(pgm_data_t);
103
104 /* The PGM NAK (NAK/N-NAK/NCF) header */
105 typedef struct {
106         nlong_t sqn;             /* Requested sequence number */
107         nshort_t src_afi;        /* NLA AFI for source (IPv4 is set to 1) */
108         nshort_t src_res;        /* reserved */
109         nlong_t src;             /* Source NLA  */
110         nshort_t grp_afi;        /* Multicast group AFI (IPv4 is set to 1) */
111         nshort_t grp_res;        /* reserved */
112         nlong_t grp;             /* Multicast group NLA */
113 } pgm_nak_t;
114 static const size_t PGM_NAK_SZ = sizeof(pgm_type)+sizeof(pgm_nak_t);
115 #define nak_ntoh(_p) \
116         (_p)->sqn = g_ntohl((_p)->sqn); \
117         (_p)->src_afi = g_ntohs((_p)->src_afi); \
118         (_p)->src_res = g_ntohs((_p)->src_res); \
119         (_p)->grp_afi = g_ntohs((_p)->grp_afi); \
120         (_p)->grp_res = g_ntohs((_p)->grp_res)
121
122 /* The PGM POLL header */
123 typedef struct {
124         nlong_t sqn;             /* POLL sequence number */
125         nshort_t round;          /* POLL Round */
126         nshort_t subtype;        /* POLL subtype */
127         nshort_t path_afi;       /* NLA AFI for last hop router (IPv4 is set to 1) */
128         nshort_t res;            /* reserved */
129         nlong_t path;            /* Last hop router NLA  */
130         nlong_t backoff_ivl;     /* POLL backoff interval */
131         nlong_t rand_str;        /* POLL random string */
132         nlong_t matching_bmask;  /* POLL matching bitmask */
133 } pgm_poll_t;
134 static const size_t PGM_POLL_SZ = sizeof(pgm_type)+sizeof(pgm_poll_t);
135 #define poll_ntoh(_p) \
136         (_p)->sqn = g_ntohl((_p)->sqn); \
137         (_p)->round = g_ntohs((_p)->round); \
138         (_p)->subtype = g_ntohs((_p)->subtype); \
139         (_p)->path_afi = g_ntohs((_p)->path_afi); \
140         (_p)->res = g_ntohs((_p)->res); \
141         (_p)->backoff_ivl = g_ntohl((_p)->backoff_ivl); \
142         (_p)->rand_str = g_ntohl((_p)->rand_str); \
143         (_p)->matching_bmask = g_ntohl((_p)->matching_bmask)
144
145 /* The PGM POLR header */
146 typedef struct {
147         nlong_t sqn;             /* POLR sequence number */
148         nshort_t round;          /* POLR Round */
149         nshort_t res;            /* reserved */
150 } pgm_polr_t;
151 static const size_t PGM_POLR_SZ = sizeof(pgm_type)+sizeof(pgm_polr_t);
152 #define polr_ntoh(_p) \
153         (_p)->sqn = g_ntohl((_p)->sqn); \
154         (_p)->round = g_ntohs((_p)->round); \
155         (_p)->res = g_ntohs((_p)->res)
156
157 /* The PGM ACK header (PGMCC) */
158 typedef struct {
159         nlong_t rx_max_sqn;      /* RX_MAX sequence number */
160         nlong_t bitmap;          /* Received Packet Bitmap */
161 } pgm_ack_t;
162 static const size_t PGM_ACK_SZ = sizeof(pgm_type)+sizeof(pgm_ack_t);
163 #define ack_ntoh(_p) \
164         (_p)->rx_max_sqn = g_ntohl((_p)->rx_max_sqn); \
165         (_p)->bitmap = g_ntohl((_p)->bitmap)
166
167 /* constants for hdr types */
168 #if defined(PGM_SPEC_01_PCKTS)
169 /* old spec-01 types */
170 #define PGM_SPM_PCKT  0x00
171 #define PGM_ODATA_PCKT  0x10
172 #define PGM_RDATA_PCKT  0x11
173 #define PGM_NAK_PCKT  0x20
174 #define PGM_NNAK_PCKT  0x21
175 #define PGM_NCF_PCKT 0x30
176 #else
177 /* spec-02 types (as well as spec-04+) */
178 #define PGM_SPM_PCKT  0x00
179 #define PGM_ODATA_PCKT  0x04
180 #define PGM_RDATA_PCKT  0x05
181 #define PGM_NAK_PCKT  0x08
182 #define PGM_NNAK_PCKT  0x09
183 #define PGM_NCF_PCKT 0x0A
184 #define PGM_POLL_PCKT 0x01
185 #define PGM_POLR_PCKT 0x02
186 #define PGM_ACK_PCKT 0x0D
187 #endif /* PGM_SPEC_01_PCKTS */
188
189 /* port swapping on NAK and NNAKs or not (default is to swap) */
190 /* PGM_NO_PORT_SWAP */
191
192 /* option flags (main PGM header) */
193 #define PGM_OPT 0x01
194 #define PGM_OPT_NETSIG 0x02
195 #define PGM_OPT_VAR_PKTLEN 0x40
196 #define PGM_OPT_PARITY 0x80
197
198 /* option types */
199 #define PGM_OPT_LENGTH 0x00
200 #define PGM_OPT_END 0x80
201 #define PGM_OPT_FRAGMENT 0x01
202 #define PGM_OPT_NAK_LIST 0x02
203 #define PGM_OPT_JOIN 0x03
204 #define PGM_OPT_REDIRECT 0x07
205 #define PGM_OPT_SYN 0x0D
206 #define PGM_OPT_FIN 0x0E
207 #define PGM_OPT_RST 0x0F
208 #define PGM_OPT_PARITY_PRM 0x08
209 #define PGM_OPT_PARITY_GRP 0x09
210 #define PGM_OPT_CURR_TGSIZE 0x0A
211 #define PGM_OPT_PGMCC_DATA  0x12
212 #define PGM_OPT_PGMCC_FEEDBACK  0x13
213 #define PGM_OPT_NAK_BO_IVL 0x04
214 #define PGM_OPT_NAK_BO_RNG 0x05
215
216 /* POLL subtypes */
217 #define PGM_POLL_GENERAL 0x0
218 #define PGM_POLL_DLR 0x1
219
220 static const nchar_t PGM_OPT_INVALID = 0x7F;
221
222 /* OPX bit values */
223 #define PGM_OPX_IGNORE  0x00
224 #define PGM_OPX_INVAL   0x01
225 #define PGM_OPX_DISCARD 0x10
226
227 /* option formats */
228 typedef struct {
229         nchar_t type;
230         nchar_t len;
231         nchar_t opx;
232         nchar_t res;
233 } pgm_opt_generic_t;
234
235 typedef struct {
236         nchar_t type;
237         nchar_t len;
238         nshort_t total_len;
239 } pgm_opt_length_t;
240
241 typedef struct {
242         nchar_t type;
243         nchar_t len;
244         nchar_t opx;
245         nchar_t res;
246 } pgm_opt_nak_list_t;
247
248 /*
249  * To squeeze the whole option into 255 bytes, we
250  * can only have 62 in the list
251  */
252 #define PGM_MAX_NAK_LIST_SZ (62)
253
254 typedef struct {
255         nchar_t type;
256         nchar_t len;
257         nchar_t opx;
258         nchar_t res;
259         nlong_t opt_join_min;
260 } pgm_opt_join_t;
261
262 typedef struct {
263         nchar_t type;
264         nchar_t len;
265         nchar_t opx;
266         nchar_t po;
267         nlong_t prm_tgsz;
268 } pgm_opt_parity_prm_t;
269
270 /* OPT_PARITY_PRM P and O bits */
271 static const nchar_t PGM_OPT_PARITY_PRM_PRO = 0x2;
272 static const nchar_t PGM_OPT_PARITY_PRM_OND = 0x1;
273
274 typedef struct {
275         nchar_t type;
276         nchar_t len;
277         nchar_t opx;
278         nchar_t res;
279         nlong_t prm_grp;
280 } pgm_opt_parity_grp_t;
281
282 typedef struct {
283         nchar_t type;
284         nchar_t len;
285         nchar_t opx;
286         nchar_t res;
287         nlong_t prm_atgsz;
288 } pgm_opt_curr_tgsize_t;
289
290 typedef struct {
291         nchar_t type;
292         nchar_t len;
293         nchar_t opx;
294         nchar_t res;
295         nlong_t tsp;
296         nshort_t acker_afi;
297         nshort_t res2;
298         nlong_t acker;
299 } pgm_opt_pgmcc_data_t;
300
301 typedef struct {
302         nchar_t type;
303         nchar_t len;
304         nchar_t opx;
305         nchar_t res;
306         nlong_t tsp;
307         nshort_t acker_afi;
308         nshort_t loss_rate;
309         nlong_t acker;
310 } pgm_opt_pgmcc_feedback_t;
311
312 typedef struct {
313         nchar_t type;
314         nchar_t len;
315         nchar_t opx;
316         nchar_t res;
317         nlong_t bo_ivl;
318         nlong_t bo_ivl_sqn;
319 } pgm_opt_nak_bo_ivl_t;
320
321 typedef struct {
322         nchar_t type;
323         nchar_t len;
324         nchar_t opx;
325         nchar_t res;
326         nlong_t min_bo_ivl;
327         nlong_t max_bo_ivl;
328 } pgm_opt_nak_bo_rng_t;
329
330 typedef struct {
331         nchar_t type;
332         nchar_t len;
333         nchar_t opx;
334         nchar_t res;
335         nshort_t afi;
336         nshort_t res2;
337         nlong_t dlr;
338 } pgm_opt_redirect_t;
339
340 typedef struct {
341         nchar_t type;
342         nchar_t len;
343         nchar_t opx;
344         nchar_t res;
345         nlong_t first_sqn;
346         nlong_t offset;
347         nlong_t total_length;
348 } pgm_opt_fragment_t;
349
350 /*
351  * Udp port for UDP encapsulation
352  */
353 #define DEFAULT_UDP_ENCAP_UCAST_PORT 3055
354 #define DEFAULT_UDP_ENCAP_MCAST_PORT 3056
355
356 static int udp_encap_ucast_port = 0;
357 static int udp_encap_mcast_port = 0;
358 static int old_encap_ucast_port = 0;
359 static int old_encap_mcast_port = 0;
360
361 static int proto_pgm = -1;
362 static int ett_pgm = -1;
363 static int ett_pgm_optbits = -1;
364 static int ett_pgm_opts = -1;
365 static int ett_pgm_spm = -1;
366 static int ett_pgm_data = -1;
367 static int ett_pgm_nak = -1;
368 static int ett_pgm_poll = -1;
369 static int ett_pgm_polr = -1;
370 static int ett_pgm_ack = -1;
371 static int ett_pgm_opts_join = -1;
372 static int ett_pgm_opts_parityprm = -1;
373 static int ett_pgm_opts_paritygrp = -1;
374 static int ett_pgm_opts_naklist = -1;
375 static int ett_pgm_opts_ccdata = -1;
376 static int ett_pgm_opts_nak_bo_ivl = -1;
377 static int ett_pgm_opts_nak_bo_rng = -1;
378 static int ett_pgm_opts_redirect = -1;
379 static int ett_pgm_opts_fragment = -1;
380
381 static int hf_pgm_main_sport = -1;
382 static int hf_pgm_main_dport = -1;
383 static int hf_pgm_main_type = -1;
384 static int hf_pgm_main_opts = -1;
385 static int hf_pgm_main_opts_opt = -1;
386 static int hf_pgm_main_opts_netsig = -1;
387 static int hf_pgm_main_opts_varlen = -1;
388 static int hf_pgm_main_opts_parity = -1;
389 static int hf_pgm_main_cksum = -1;
390 static int hf_pgm_main_cksum_bad = -1;
391 static int hf_pgm_main_gsi = -1;
392 static int hf_pgm_main_tsdulen = -1;
393 static int hf_pgm_spm_sqn = -1;
394 static int hf_pgm_spm_lead = -1;
395 static int hf_pgm_spm_trail = -1;
396 static int hf_pgm_spm_pathafi = -1;
397 static int hf_pgm_spm_res = -1;
398 static int hf_pgm_spm_path = -1;
399 static int hf_pgm_data_sqn = -1;
400 static int hf_pgm_data_trail = -1;
401 static int hf_pgm_nak_sqn = -1;
402 static int hf_pgm_nak_srcafi = -1;
403 static int hf_pgm_nak_srcres = -1;
404 static int hf_pgm_nak_src = -1;
405 static int hf_pgm_nak_grpafi = -1;
406 static int hf_pgm_nak_grpres = -1;
407 static int hf_pgm_nak_grp = -1;
408 static int hf_pgm_poll_sqn = -1;
409 static int hf_pgm_poll_round = -1;
410 static int hf_pgm_poll_subtype = -1;
411 static int hf_pgm_poll_pathafi = -1;
412 static int hf_pgm_poll_res = -1;
413 static int hf_pgm_poll_path = -1;
414 static int hf_pgm_poll_backoff_ivl = -1;
415 static int hf_pgm_poll_rand_str = -1;
416 static int hf_pgm_poll_matching_bmask = -1;
417 static int hf_pgm_polr_sqn = -1;
418 static int hf_pgm_polr_round = -1;
419 static int hf_pgm_polr_res = -1;
420 static int hf_pgm_ack_sqn = -1;
421 static int hf_pgm_ack_bitmap = -1;
422
423 static int hf_pgm_opt_type = -1;
424 static int hf_pgm_opt_len = -1;
425 static int hf_pgm_opt_tlen = -1;
426
427 static int hf_pgm_genopt_type = -1;
428 static int hf_pgm_genopt_len = -1;
429 static int hf_pgm_genopt_opx = -1;
430
431 static int hf_pgm_opt_join_res = -1;
432 static int hf_pgm_opt_join_minjoin = -1;
433
434 static int hf_pgm_opt_parity_prm_po = -1;
435 static int hf_pgm_opt_parity_prm_prmtgsz = -1;
436
437 static int hf_pgm_opt_parity_grp_res = -1;
438 static int hf_pgm_opt_parity_grp_prmgrp = -1;
439
440 #ifdef PGM_UNUSED_HANDLES
441 static int hf_pgm_opt_curr_tgsize_type = -1;
442 static int hf_pgm_opt_curr_tgsize_len = -1;
443 static int hf_pgm_opt_curr_tgsize_opx = -1;
444 static int hf_pgm_opt_curr_tgsize_res = -1;
445 static int hf_pgm_opt_curr_tgsize_prmatgsz = -1;
446 #endif
447
448 static int hf_pgm_opt_nak_res = -1;
449 static int hf_pgm_opt_nak_list = -1;
450
451 static int hf_pgm_opt_ccdata_res = -1;
452 static int hf_pgm_opt_ccdata_tsp = -1;
453 static int hf_pgm_opt_ccdata_afi = -1;
454 static int hf_pgm_opt_ccdata_res2 = -1;
455 static int hf_pgm_opt_ccdata_acker = -1;
456
457 static int hf_pgm_opt_ccfeedbk_res = -1;
458 static int hf_pgm_opt_ccfeedbk_tsp = -1;
459 static int hf_pgm_opt_ccfeedbk_afi = -1;
460 static int hf_pgm_opt_ccfeedbk_lossrate = -1;
461 static int hf_pgm_opt_ccfeedbk_acker = -1;
462
463 static int hf_pgm_opt_nak_bo_ivl_res = -1;
464 static int hf_pgm_opt_nak_bo_ivl_bo_ivl = -1;
465 static int hf_pgm_opt_nak_bo_ivl_bo_ivl_sqn = -1;
466
467 static int hf_pgm_opt_nak_bo_rng_res = -1;
468 static int hf_pgm_opt_nak_bo_rng_min_bo_ivl = -1;
469 static int hf_pgm_opt_nak_bo_rng_max_bo_ivl = -1;
470
471 static int hf_pgm_opt_redirect_res = -1;
472 static int hf_pgm_opt_redirect_afi = -1;
473 static int hf_pgm_opt_redirect_res2 = -1;
474 static int hf_pgm_opt_redirect_dlr = -1;
475
476 static int hf_pgm_opt_fragment_res = -1;
477 static int hf_pgm_opt_fragment_first_sqn = -1;
478 static int hf_pgm_opt_fragment_offset = -1;
479 static int hf_pgm_opt_fragment_total_length = -1;
480
481 static dissector_table_t subdissector_table;
482 static heur_dissector_list_t heur_subdissector_list;
483 static dissector_handle_t data_handle;
484
485 /*
486  * As of the time this comment was typed
487  *
488  *      http://search.ietf.org/internet-drafts/draft-speakman-pgm-spec-06.txt
489  *
490  * was the URL for the PGM draft.
491  */
492
493 static char *
494 optsstr(nchar_t opts)
495 {
496         static char msg[256];
497         char *p = msg, *str;
498
499         if (opts == 0)
500                 return("");
501
502         if (opts & PGM_OPT){
503                 sprintf(p, "Present");
504                 p += strlen("Present");
505         }
506         if (opts & PGM_OPT_NETSIG){
507                 if (p != msg)
508                         str = ",NetSig";
509                 else
510                         str = "NetSig";
511                 sprintf(p, str);
512                 p += strlen(str);
513         }
514         if (opts & PGM_OPT_VAR_PKTLEN){
515                 if (p != msg)
516                         str = ",VarLen";
517                 else
518                         str = "VarLen";
519                 sprintf(p, str);
520                 p += strlen(str);
521         }
522         if (opts & PGM_OPT_PARITY){
523                 if (p != msg)
524                         str = ",Parity";
525                 else
526                         str = "Parity";
527                 sprintf(p, str);
528                 p += strlen(str);
529         }
530         if (p == msg) {
531                 sprintf(p, "0x%x", opts);
532         }
533         return(msg);
534 }
535 static char *
536 paritystr(nchar_t parity)
537 {
538         static char msg[256];
539         char *p = msg, *str;
540
541         if (parity == 0)
542                 return("");
543
544         if (parity & PGM_OPT_PARITY_PRM_PRO){
545                 sprintf(p, "Pro-active");
546                 p += strlen("Pro-active");
547         }
548         if (parity & PGM_OPT_PARITY_PRM_OND){
549                 if (p != msg)
550                         str = ",On-demand";
551                 else
552                         str = "On-demand";
553                 sprintf(p, str);
554                 p += strlen(str);
555         }
556         if (p == msg) {
557                 sprintf(p, "0x%x", parity);
558         }
559         return(msg);
560 }
561
562 static const value_string opt_vals[] = {
563         { PGM_OPT_LENGTH,      "Length" },
564         { PGM_OPT_END,         "End" },
565         { PGM_OPT_FRAGMENT,    "Fragment" },
566         { PGM_OPT_NAK_LIST,    "NakList" },
567         { PGM_OPT_JOIN,        "Join" },
568         { PGM_OPT_REDIRECT,    "ReDirect" },
569         { PGM_OPT_SYN,         "Syn" },
570         { PGM_OPT_FIN,         "Fin" },
571         { PGM_OPT_RST,         "Rst" },
572         { PGM_OPT_PARITY_PRM,  "ParityPrm" },
573         { PGM_OPT_PARITY_GRP,  "ParityGrp" },
574         { PGM_OPT_CURR_TGSIZE, "CurrTgsiz" },
575         { PGM_OPT_PGMCC_DATA,  "CcData" },
576         { PGM_OPT_PGMCC_FEEDBACK, "CcFeedBack" },
577         { PGM_OPT_NAK_BO_IVL,  "NakBackOffIvl" },
578         { PGM_OPT_NAK_BO_RNG,  "NakBackOffRng" },
579         { PGM_OPT_FRAGMENT,    "Fragment" },
580         { 0,                   NULL }
581 };
582
583 static const value_string opx_vals[] = {
584         { PGM_OPX_IGNORE,  "Ignore" },
585         { PGM_OPX_INVAL,   "Inval" },
586         { PGM_OPX_DISCARD, "DisCard" },
587         { 0,               NULL }
588 };
589
590 static void
591 dissect_pgmopts(tvbuff_t *tvb, int offset, proto_tree *tree,
592     const char *pktname)
593 {
594         proto_item *tf;
595         proto_tree *opts_tree = NULL;
596         proto_tree *opt_tree = NULL;
597         pgm_opt_length_t opts;
598         pgm_opt_generic_t genopts;
599         int theend = 0, firsttime = 1;
600
601         tvb_memcpy(tvb, (guint8 *)&opts, offset, sizeof(opts));
602         opts.total_len = g_ntohs(opts.total_len);
603
604         tf = proto_tree_add_text(tree, tvb, offset,
605                 opts.total_len,
606                 "%s Options (Total Length %d)", pktname, opts.total_len);
607         opts_tree = proto_item_add_subtree(tf, ett_pgm_opts);
608         proto_tree_add_uint(opts_tree, hf_pgm_opt_type, tvb,
609                 offset, 1, opts.type);
610         proto_tree_add_uint(opts_tree, hf_pgm_opt_len, tvb,
611                 offset+1, 1, opts.len);
612         proto_tree_add_uint(opts_tree, hf_pgm_opt_tlen, tvb,
613                 offset+2, 2, opts.total_len);
614
615         offset += 4;
616         for (opts.total_len -= 4; opts.total_len > 0;){
617                 tvb_memcpy(tvb, (guint8 *)&genopts, offset, sizeof(genopts));
618                 if (genopts.type & PGM_OPT_END)  {
619                         genopts.type &= ~PGM_OPT_END;
620                         theend = 1;
621                 }
622                 tf = proto_tree_add_text(opts_tree, tvb, offset, genopts.len,
623                         "Option: %s, Length: %u",
624                         val_to_str(genopts.type, opt_vals, "Unknown (0x%02x)"),
625                         genopts.len);
626                 if (genopts.len == 0)
627                         break;
628
629                 switch(genopts.type) {
630                 case PGM_OPT_JOIN:{
631                         pgm_opt_join_t optdata;
632
633                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
634                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_join);
635
636                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
637                                 tvb, offset, 1, genopts.type);
638
639                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
640                                 offset+1, 1, genopts.len);
641
642                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx, tvb,
643                                 offset+2, 1, genopts.opx);
644
645                         proto_tree_add_uint(opt_tree, hf_pgm_opt_join_res, tvb,
646                                 offset+3, 1, optdata.res);
647
648                         proto_tree_add_uint(opt_tree, hf_pgm_opt_join_minjoin, tvb,
649                                 offset+4, 4, g_ntohl(optdata.opt_join_min));
650
651                         break;
652                 }
653                 case PGM_OPT_PARITY_PRM:{
654                         pgm_opt_parity_prm_t optdata;
655
656                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
657                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_parityprm);
658
659                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
660                                 tvb, offset, 1, genopts.type);
661
662                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
663                                 offset+1, 1, genopts.len);
664
665                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
666                                 tvb, offset+2, 1, genopts.opx);
667
668                         proto_tree_add_uint_format(opt_tree, hf_pgm_opt_parity_prm_po, tvb,
669                                 offset+3, 1, optdata.po, "Parity Parameters: %s (0x%x)",
670                                 paritystr(optdata.po), optdata.po);
671
672                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_prm_prmtgsz,
673                                 tvb, offset+4, 4, g_ntohl(optdata.prm_tgsz));
674
675                         break;
676                 }
677                 case PGM_OPT_PARITY_GRP:{
678                         pgm_opt_parity_grp_t optdata;
679
680                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
681                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_paritygrp);
682
683                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
684                                 tvb, offset, 1, genopts.type);
685
686                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
687                                 offset+1, 1, genopts.len);
688
689                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
690                                 tvb, offset+2, 1, genopts.opx);
691
692                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_grp_res, tvb,
693                                 offset+3, 1, optdata.res);
694
695                         proto_tree_add_uint(opt_tree, hf_pgm_opt_parity_grp_prmgrp,
696                                 tvb, offset+4, 4, g_ntohl(optdata.prm_grp));
697
698                         break;
699                 }
700                 case PGM_OPT_NAK_LIST:{
701                         pgm_opt_nak_list_t optdata;
702                         nlong_t naklist[PGM_MAX_NAK_LIST_SZ+1];
703                         char nakbuf[8192], *ptr;
704                         int i, j, naks, soffset = 0;
705
706                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
707                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_naklist);
708
709                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type, tvb,
710                                 offset, 1, genopts.type);
711
712                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
713                                 offset+1, 1, genopts.len);
714
715                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
716                                 tvb, offset+2, 1, genopts.opx);
717
718                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_res, tvb,
719                                 offset+3, 1, optdata.res);
720
721                         optdata.len -= sizeof(pgm_opt_nak_list_t);
722                         tvb_memcpy(tvb, (guint8 *)naklist, offset+4, optdata.len);
723                         naks = (optdata.len/sizeof(nlong_t));
724                         ptr = nakbuf;
725                         j = 0;
726                         /*
727                          * Print out 8 per line
728                          */
729                         for (i=0; i < naks; i++) {
730                                 sprintf(nakbuf+soffset, "0x%lx ",
731                                     (unsigned long)g_ntohl(naklist[i]));
732                                 soffset = strlen(nakbuf);
733                                 if ((++j % 8) == 0) {
734                                         if (firsttime) {
735                                                 proto_tree_add_bytes_format(opt_tree,
736                                                         hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
737                                                         nakbuf, "List(%d): %s", naks, nakbuf);
738                                                         soffset = 0;
739                                         } else {
740                                                 proto_tree_add_bytes_format(opt_tree,
741                                                         hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
742                                                         nakbuf, "List: %s", nakbuf);
743                                                         soffset = 0;
744                                         }
745                                         firsttime = 0;
746                                 }
747                         }
748                         if (soffset) {
749                                 if (firsttime) {
750                                         proto_tree_add_bytes_format(opt_tree,
751                                                 hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
752                                                 nakbuf, "List(%d): %s", naks, nakbuf);
753                                                 soffset = 0;
754                                 } else {
755                                         proto_tree_add_bytes_format(opt_tree,
756                                                 hf_pgm_opt_nak_list, tvb, offset+4, optdata.len,
757                                                 nakbuf, "List: %s", nakbuf);
758                                                 soffset = 0;
759                                 }
760                         }
761                         break;
762                 }
763                 case PGM_OPT_PGMCC_DATA:{
764                         pgm_opt_pgmcc_data_t optdata;
765
766                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
767                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_ccdata);
768
769                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
770                                 tvb, offset, 1, genopts.type);
771
772                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
773                                 offset+1, 1, genopts.len);
774
775                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
776                                 tvb, offset+2, 1, genopts.opx);
777
778                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_res, tvb,
779                                 offset+3, 1, optdata.res);
780
781                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_tsp, tvb,
782                                 offset+4, 4, optdata.tsp);
783
784                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_afi, tvb,
785                                 offset+8, 2, g_ntohs(optdata.acker_afi));
786
787                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccdata_res2, tvb,
788                                 offset+10, 2, g_ntohs(optdata.res2));
789
790                         switch (g_ntohs(optdata.acker_afi)) {
791
792                         case AFNUM_INET:
793                                 proto_tree_add_ipv4(opt_tree, hf_pgm_opt_ccdata_acker,
794                                     tvb, offset+12, 4, optdata.acker);
795                                 break;
796
797                         default:
798                                 /*
799                                  * XXX - the header is variable-length,
800                                  * as the length of the NLA depends on
801                                  * its AFI.
802                                  *
803                                  * However, our structure for it is
804                                  * fixed-length, and assumes it's a 4-byte
805                                  * IPv4 address.
806                                  */
807                                 break;
808                         }
809
810                         break;
811                 }
812                 case PGM_OPT_PGMCC_FEEDBACK:{
813                         pgm_opt_pgmcc_feedback_t optdata;
814
815                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
816                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_ccdata);
817
818                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
819                                 tvb, offset, 1, genopts.type);
820
821                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
822                                 offset+1, 1, genopts.len);
823
824                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
825                                 tvb, offset+2, 1, genopts.opx);
826
827                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_res, tvb,
828                                 offset+3, 1, optdata.res);
829
830                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_tsp, tvb,
831                                 offset+4, 4, optdata.tsp);
832
833                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_afi, tvb,
834                                 offset+8, 2, g_ntohs(optdata.acker_afi));
835
836                         proto_tree_add_uint(opt_tree, hf_pgm_opt_ccfeedbk_lossrate, tvb,
837                                 offset+10, 2, g_ntohs(optdata.loss_rate));
838
839                         switch (g_ntohs(optdata.acker_afi)) {
840
841                         case AFNUM_INET:
842                                 proto_tree_add_ipv4(opt_tree, hf_pgm_opt_ccfeedbk_acker,
843                                     tvb, offset+12, 4, optdata.acker);
844                                 break;
845
846                         default:
847                                 /*
848                                  * XXX - the header is variable-length,
849                                  * as the length of the NLA depends on
850                                  * its AFI.
851                                  *
852                                  * However, our structure for it is
853                                  * fixed-length, and assumes it's a 4-byte
854                                  * IPv4 address.
855                                  */
856                                 break;
857                         }
858
859                         break;
860                 }
861                 case PGM_OPT_NAK_BO_IVL:{
862                         pgm_opt_nak_bo_ivl_t optdata;
863
864                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
865                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_nak_bo_ivl);
866
867                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
868                                 tvb, offset, 1, genopts.type);
869
870                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
871                                 offset+1, 1, genopts.len);
872
873                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx, tvb,
874                                 offset+2, 1, genopts.opx);
875
876                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_ivl_res, tvb,
877                                 offset+3, 1, optdata.res);
878
879                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_ivl_bo_ivl, tvb,
880                                 offset+4, 4, g_ntohl(optdata.bo_ivl));
881
882                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_ivl_bo_ivl_sqn, tvb,
883                                 offset+8, 4, g_ntohl(optdata.bo_ivl_sqn));
884
885                         break;
886                 }
887                 case PGM_OPT_NAK_BO_RNG:{
888                         pgm_opt_nak_bo_rng_t optdata;
889
890                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
891                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_nak_bo_rng);
892
893                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
894                                 tvb, offset, 1, genopts.type);
895
896                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
897                                 offset+1, 1, genopts.len);
898
899                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx, tvb,
900                                 offset+2, 1, genopts.opx);
901
902                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_rng_res, tvb,
903                                 offset+3, 1, optdata.res);
904
905                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_rng_min_bo_ivl, tvb,
906                                 offset+4, 4, g_ntohl(optdata.min_bo_ivl));
907
908                         proto_tree_add_uint(opt_tree, hf_pgm_opt_nak_bo_rng_max_bo_ivl, tvb,
909                                 offset+8, 4, g_ntohl(optdata.max_bo_ivl));
910
911                         break;
912                 }
913                 case PGM_OPT_REDIRECT:{
914                         pgm_opt_redirect_t optdata;
915
916                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
917                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_redirect);
918
919                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
920                                 tvb, offset, 1, genopts.type);
921
922                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
923                                 offset+1, 1, genopts.len);
924
925                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx,
926                                 tvb, offset+2, 1, genopts.opx);
927
928                         proto_tree_add_uint(opt_tree, hf_pgm_opt_redirect_res, tvb,
929                                 offset+3, 1, optdata.res);
930
931                         proto_tree_add_uint(opt_tree, hf_pgm_opt_redirect_afi, tvb,
932                                 offset+4, 2, g_ntohs(optdata.afi));
933
934                         proto_tree_add_uint(opt_tree, hf_pgm_opt_redirect_res2, tvb,
935                                 offset+6, 2, g_ntohs(optdata.res2));
936
937                         switch (g_ntohs(optdata.afi)) {
938
939                         case AFNUM_INET:
940                                 proto_tree_add_ipv4(opt_tree, hf_pgm_opt_redirect_dlr,
941                                     tvb, offset+8, 4, optdata.dlr);
942                                 break;
943
944                         default:
945                                 /*
946                                  * XXX - the header is variable-length,
947                                  * as the length of the NLA depends on
948                                  * its AFI.
949                                  *
950                                  * However, our structure for it is
951                                  * fixed-length, and assumes it's a 4-byte
952                                  * IPv4 address.
953                                  */
954                                 break;
955                         }
956
957                         break;
958                 }
959                 case PGM_OPT_FRAGMENT:{
960                         pgm_opt_fragment_t optdata;
961
962                         tvb_memcpy(tvb, (guint8 *)&optdata, offset, sizeof(optdata));
963                         opt_tree = proto_item_add_subtree(tf, ett_pgm_opts_fragment);
964
965                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_type,
966                                 tvb, offset, 1, genopts.type);
967
968                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_len, tvb,
969                                 offset+1, 1, genopts.len);
970
971                         proto_tree_add_uint(opt_tree, hf_pgm_genopt_opx, tvb,
972                                 offset+2, 1, genopts.opx);
973
974                         proto_tree_add_uint(opt_tree, hf_pgm_opt_fragment_res, tvb,
975                                 offset+3, 1, optdata.res);
976
977                         proto_tree_add_uint(opt_tree, hf_pgm_opt_fragment_first_sqn, tvb,
978                                 offset+4, 4, g_ntohl(optdata.first_sqn));
979
980                         proto_tree_add_uint(opt_tree, hf_pgm_opt_fragment_offset, tvb,
981                                 offset+8, 4, g_ntohl(optdata.offset));
982
983                         proto_tree_add_uint(opt_tree, hf_pgm_opt_fragment_total_length, tvb,
984                                 offset+12, 4, g_ntohl(optdata.total_length));
985
986                         break;
987                 }
988                 }
989                 offset += genopts.len;
990                 opts.total_len -= genopts.len;
991
992         }
993         return ;
994 }
995
996 static const value_string type_vals[] = {
997         { PGM_SPM_PCKT,   "SPM" },
998         { PGM_RDATA_PCKT, "RDATA" },
999         { PGM_ODATA_PCKT, "ODATA" },
1000         { PGM_NAK_PCKT,   "NAK" },
1001         { PGM_NNAK_PCKT,  "NNAK" },
1002         { PGM_NCF_PCKT,   "NCF" },
1003         { PGM_POLL_PCKT,  "POLL" },
1004         { PGM_POLR_PCKT,  "POLR" },
1005         { PGM_ACK_PCKT,   "ACK" },
1006         { 0,              NULL }
1007 };
1008
1009 static const value_string poll_subtype_vals[] = {
1010         { PGM_POLL_GENERAL,   "General" },
1011         { PGM_POLL_DLR,       "DLR" },
1012         { 0,                  NULL }
1013 };
1014 /* Determine if there is a sub-dissector and call it.  This has been */
1015 /* separated into a stand alone routine to other protocol dissectors */
1016 /* can call to it, ie. socks    */
1017
1018 void
1019 decode_pgm_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
1020         proto_tree *tree, pgm_type *pgmhdr)
1021 {
1022   tvbuff_t *next_tvb;
1023   int found = 0;
1024
1025   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1026
1027   /* do lookup with the subdissector table */
1028   found = dissector_try_port(subdissector_table, pgmhdr->sport,
1029                         next_tvb, pinfo, tree);
1030   if (found)
1031         return;
1032
1033   found = dissector_try_port(subdissector_table, pgmhdr->dport,
1034                         next_tvb, pinfo, tree);
1035   if (found)
1036         return;
1037
1038   /* do lookup with the heuristic subdissector table */
1039   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
1040     return;
1041
1042   /* Oh, well, we don't know this; dissect it as data. */
1043   call_dissector(data_handle,next_tvb, pinfo, tree);
1044
1045 }
1046 int
1047 total_size(tvbuff_t *tvb, pgm_type *hdr)
1048 {
1049         int bytes = sizeof(pgm_type);
1050         pgm_opt_length_t opts;
1051
1052         switch(hdr->type) {
1053         case PGM_SPM_PCKT:
1054                 bytes += sizeof(pgm_spm_t);
1055                 break;
1056
1057         case PGM_RDATA_PCKT:
1058         case PGM_ODATA_PCKT:
1059                 bytes += sizeof(pgm_data_t);
1060                 break;
1061
1062         case PGM_NAK_PCKT:
1063         case PGM_NNAK_PCKT:
1064         case PGM_NCF_PCKT:
1065                 bytes += sizeof(pgm_nak_t);
1066                 break;
1067         case PGM_POLL_PCKT:
1068                 bytes += sizeof(pgm_poll_t);
1069                 break;
1070         case PGM_POLR_PCKT:
1071                 bytes += sizeof(pgm_polr_t);
1072                 break;
1073         case PGM_ACK_PCKT:
1074                 bytes += sizeof(pgm_ack_t);
1075                 break;
1076         }
1077         if ((hdr->opts & PGM_OPT)) {
1078                 tvb_memcpy(tvb, (guint8 *)&opts, bytes, sizeof(opts));
1079                 bytes += g_ntohs(opts.total_len);
1080         }
1081         return(bytes);
1082 }
1083 /*
1084  * dissect_pgm - The dissector for Pragmatic General Multicast
1085  */
1086 static void
1087 dissect_pgm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1088 {
1089         proto_tree *pgm_tree = NULL;
1090         proto_tree *opt_tree = NULL;
1091         proto_tree *type_tree = NULL;
1092         proto_item *tf;
1093         pgm_type pgmhdr;
1094         pgm_spm_t spm;
1095         pgm_data_t data;
1096         pgm_nak_t nak;
1097         pgm_poll_t poll;
1098         pgm_polr_t polr;
1099         pgm_ack_t ack;
1100         int offset = 0;
1101         guint hlen, plen;
1102         proto_item *ti;
1103         const char *pktname;
1104         const char *pollstname;
1105         char *gsi;
1106         int isdata = 0;
1107         guint pgmlen, reportedlen;
1108
1109         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1110                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PGM");
1111
1112         /* Clear out the Info column. */
1113         if (check_col(pinfo->cinfo, COL_INFO))
1114                 col_clear(pinfo->cinfo, COL_INFO);
1115
1116         tvb_memcpy(tvb, (guint8 *)&pgmhdr, offset, sizeof(pgm_type));
1117         hlen = sizeof(pgm_type);
1118         pgmhdr.sport = g_ntohs(pgmhdr.sport);
1119         pgmhdr.dport = g_ntohs(pgmhdr.dport);
1120         pgmhdr.tsdulen = g_ntohs(pgmhdr.tsdulen);
1121         pgmhdr.cksum = g_ntohs(pgmhdr.cksum);
1122
1123         pktname = val_to_str(pgmhdr.type, type_vals, "Unknown (0x%02x)");
1124
1125         gsi = bytes_to_str(pgmhdr.gsi, 6);
1126         switch(pgmhdr.type) {
1127         case PGM_SPM_PCKT:
1128                 plen = sizeof(pgm_spm_t);
1129                 tvb_memcpy(tvb, (guint8 *)&spm, sizeof(pgm_type), plen);
1130                 spm_ntoh(&spm);
1131                 if (check_col(pinfo->cinfo, COL_INFO)) {
1132                         col_add_fstr(pinfo->cinfo, COL_INFO,
1133                                 "%-5s sqn 0x%x gsi %s", pktname, spm.sqn, gsi);
1134                 }
1135                 break;
1136
1137         case PGM_RDATA_PCKT:
1138         case PGM_ODATA_PCKT:
1139                 plen = sizeof(pgm_data_t);
1140                 tvb_memcpy(tvb, (guint8 *)&data, sizeof(pgm_type), plen);
1141                 data_ntoh(&data);
1142                 if (check_col(pinfo->cinfo, COL_INFO)) {
1143                         col_add_fstr(pinfo->cinfo, COL_INFO,
1144                             "%-5s sqn 0x%x gsi %s tsdulen %d", pktname, data.sqn, gsi,
1145                             pgmhdr.tsdulen);
1146                 }
1147                 isdata = 1;
1148                 break;
1149
1150         case PGM_NAK_PCKT:
1151         case PGM_NNAK_PCKT:
1152         case PGM_NCF_PCKT:
1153                 plen = sizeof(pgm_nak_t);
1154                 tvb_memcpy(tvb, (guint8 *)&nak, sizeof(pgm_type), plen);
1155                 nak_ntoh(&nak);
1156                 if (check_col(pinfo->cinfo, COL_INFO)) {
1157                         col_add_fstr(pinfo->cinfo, COL_INFO,
1158                                 "%-5s sqn 0x%x gsi %s", pktname, nak.sqn, gsi);
1159                 }
1160                 break;
1161         case PGM_POLL_PCKT:
1162                 plen = sizeof(pgm_poll_t);
1163                 tvb_memcpy(tvb, (guint8 *)&poll, sizeof(pgm_type), plen);
1164                 poll_ntoh(&poll);
1165                 pollstname = val_to_str(poll.subtype, poll_subtype_vals, "Unknown (0x%02x)");
1166                 if (check_col(pinfo->cinfo, COL_INFO)) {
1167                         col_add_fstr(pinfo->cinfo, COL_INFO,
1168                                 "%-5s sqn 0x%x gsi %s subtype %s", pktname, poll.sqn, gsi, pollstname);
1169                 }
1170                 break;
1171         case PGM_POLR_PCKT:
1172                 plen = sizeof(pgm_polr_t);
1173                 tvb_memcpy(tvb, (guint8 *)&polr, sizeof(pgm_type), plen);
1174                 polr_ntoh(&polr);
1175                 if (check_col(pinfo->cinfo, COL_INFO)) {
1176                         col_add_fstr(pinfo->cinfo, COL_INFO,
1177                                 "%-5s sqn 0x%x gsi %s", pktname, polr.sqn, gsi);
1178                 }
1179                 break;
1180         case PGM_ACK_PCKT:
1181                 plen = sizeof(pgm_ack_t);
1182                 tvb_memcpy(tvb, (guint8 *)&ack, sizeof(pgm_type), plen);
1183                 ack_ntoh(&ack);
1184                 if (check_col(pinfo->cinfo, COL_INFO)) {
1185                         col_add_fstr(pinfo->cinfo, COL_INFO,
1186                             "%-5s sqn 0x%x gsi %s", pktname, ack.rx_max_sqn, gsi);
1187                 }
1188                 break;
1189
1190         default:
1191                 return;
1192         }
1193
1194         if (tree) {
1195                 ti = proto_tree_add_protocol_format(tree, proto_pgm,
1196                         tvb, offset, total_size(tvb, &pgmhdr),
1197                         "Pragmatic General Multicast: Type %s"
1198                             " SrcPort %u, DstPort %u, GSI %s", pktname,
1199                         pgmhdr.sport, pgmhdr.dport,
1200                         bytes_to_str(pgmhdr.gsi, 6));
1201
1202                 pgm_tree = proto_item_add_subtree(ti, ett_pgm);
1203                 proto_tree_add_uint(pgm_tree, hf_pgm_main_sport, tvb, offset, 2,
1204                         pgmhdr.sport);
1205                 proto_tree_add_uint(pgm_tree, hf_pgm_main_dport, tvb, offset+2,
1206                         2, pgmhdr.dport);
1207                 proto_tree_add_uint(pgm_tree, hf_pgm_main_type, tvb,
1208                         offset+4, 1, pgmhdr.type);
1209
1210                 tf = proto_tree_add_uint_format(pgm_tree, hf_pgm_main_opts, tvb,
1211                         offset+5, 1, pgmhdr.opts, "Options: %s (0x%x)",
1212                         optsstr(pgmhdr.opts), pgmhdr.opts);
1213                 opt_tree = proto_item_add_subtree(tf, ett_pgm_optbits);
1214
1215                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_opt, tvb,
1216                         offset+5, 1, (pgmhdr.opts & PGM_OPT));
1217                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_netsig, tvb,
1218                         offset+5, 1, (pgmhdr.opts & PGM_OPT_NETSIG));
1219                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_varlen, tvb,
1220                         offset+5, 1, (pgmhdr.opts & PGM_OPT_VAR_PKTLEN));
1221                 proto_tree_add_boolean(opt_tree, hf_pgm_main_opts_parity, tvb,
1222                         offset+5, 1, (pgmhdr.opts & PGM_OPT_PARITY));
1223
1224                 reportedlen = tvb_reported_length(tvb);
1225                 pgmlen = tvb_length(tvb);
1226                 if (pgm_check_checksum && pgmlen >= reportedlen) {
1227                         vec_t cksum_vec[1];
1228                         guint16 computed_cksum;
1229
1230                         cksum_vec[0].ptr = tvb_get_ptr(tvb, offset, pgmlen);
1231                         cksum_vec[0].len = pgmlen;
1232                         computed_cksum = in_cksum(&cksum_vec[0], 1);
1233                         if (computed_cksum == 0) {
1234                                 proto_tree_add_uint_format(pgm_tree, hf_pgm_main_cksum, tvb,
1235                                         offset+6, 2, pgmhdr.cksum, "Checksum: 0x%04x (correct)", pgmhdr.cksum);
1236                         } else {
1237                                 proto_tree_add_boolean_hidden(pgm_tree, hf_pgm_main_cksum_bad, tvb,
1238                                     offset+6, 2, TRUE);
1239                                 proto_tree_add_uint_format(pgm_tree, hf_pgm_main_cksum, tvb,
1240                                     offset+6, 2, pgmhdr.cksum, "Checksum: 0x%04x (incorrect, should be 0x%04x)",
1241                                         pgmhdr.cksum, in_cksum_shouldbe(pgmhdr.cksum, computed_cksum));
1242                         }
1243                 } else {
1244                         proto_tree_add_uint(pgm_tree, hf_pgm_main_cksum, tvb, offset+6,
1245                                 2, pgmhdr.cksum);
1246                 }
1247
1248                 proto_tree_add_bytes(pgm_tree, hf_pgm_main_gsi, tvb, offset+8,
1249                         6, pgmhdr.gsi);
1250                 proto_tree_add_uint(pgm_tree, hf_pgm_main_tsdulen, tvb,
1251                         offset+14, 2, pgmhdr.tsdulen);
1252
1253                 offset = sizeof(pgm_type);
1254                 tf = proto_tree_add_text(pgm_tree, tvb, offset, plen, "%s Packet",
1255                         pktname);
1256                 switch(pgmhdr.type) {
1257                 case PGM_SPM_PCKT:
1258                         type_tree = proto_item_add_subtree(tf, ett_pgm_spm);
1259
1260                         proto_tree_add_uint(type_tree, hf_pgm_spm_sqn, tvb,
1261                                 offset, 4, spm.sqn);
1262                         proto_tree_add_uint(type_tree, hf_pgm_spm_trail, tvb,
1263                                 offset+4, 4, spm.trail);
1264                         proto_tree_add_uint(type_tree, hf_pgm_spm_lead, tvb,
1265                                 offset+8, 4, spm.lead);
1266                         proto_tree_add_uint(type_tree, hf_pgm_spm_pathafi, tvb,
1267                                 offset+12, 2, spm.path_afi);
1268                         proto_tree_add_uint(type_tree, hf_pgm_spm_res, tvb,
1269                                 offset+14, 2, spm.res);
1270                         switch (spm.path_afi) {
1271
1272                         case AFNUM_INET:
1273                                 proto_tree_add_ipv4(type_tree, hf_pgm_spm_path,
1274                                     tvb, offset+16, 4, spm.path);
1275                                 break;
1276
1277                         default:
1278                                 /*
1279                                  * XXX - the header is variable-length,
1280                                  * as the length of the NLA depends on
1281                                  * its AFI.
1282                                  *
1283                                  * However, our structure for it is
1284                                  * fixed-length, and assumes it's a 4-byte
1285                                  * IPv4 address.
1286                                  */
1287                                 return;
1288                         }
1289
1290                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1291                                 break;
1292                         offset += plen;
1293
1294                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1295
1296                         break;
1297
1298                 case PGM_RDATA_PCKT:
1299                 case PGM_ODATA_PCKT: {
1300                         type_tree = proto_item_add_subtree(tf, ett_pgm_data);
1301
1302                         proto_tree_add_uint(type_tree, hf_pgm_spm_sqn, tvb,
1303                                 offset, 4, data.sqn);
1304                         proto_tree_add_uint(type_tree, hf_pgm_spm_trail, tvb,
1305                                 offset+4, 4, data.trail);
1306
1307                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1308                                 break;
1309                         offset += plen;
1310
1311                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1312
1313                         break;
1314                 }
1315
1316
1317                 case PGM_NAK_PCKT:
1318                 case PGM_NNAK_PCKT:
1319                 case PGM_NCF_PCKT:
1320                         type_tree = proto_item_add_subtree(tf, ett_pgm_nak);
1321
1322                         proto_tree_add_uint(type_tree, hf_pgm_nak_sqn, tvb,
1323                                 offset, 4, nak.sqn);
1324                         proto_tree_add_uint(type_tree, hf_pgm_nak_srcafi, tvb,
1325                                 offset+4, 2, nak.src_afi);
1326                         proto_tree_add_uint(type_tree, hf_pgm_nak_srcres, tvb,
1327                                 offset+6, 2, nak.src_res);
1328
1329                         switch (nak.src_afi) {
1330
1331                         case AFNUM_INET:
1332                                 proto_tree_add_ipv4(type_tree, hf_pgm_nak_src,
1333                                     tvb, offset+8, 4, nak.src);
1334                                 break;
1335
1336                         default:
1337                                 /*
1338                                  * XXX - the header is variable-length,
1339                                  * as the length of the NLA depends on
1340                                  * its AFI.
1341                                  *
1342                                  * However, our structure for it is
1343                                  * fixed-length, and assumes it's a 4-byte
1344                                  * IPv4 address.
1345                                  */
1346                                 break;
1347                         }
1348
1349                         proto_tree_add_uint(type_tree, hf_pgm_nak_grpafi, tvb,
1350                                 offset+12, 2, nak.grp_afi);
1351                         proto_tree_add_uint(type_tree, hf_pgm_nak_grpres, tvb,
1352                                 offset+14, 2, nak.grp_res);
1353
1354                         switch (nak.grp_afi) {
1355
1356                         case AFNUM_INET:
1357                                 proto_tree_add_ipv4(type_tree, hf_pgm_nak_grp,
1358                                     tvb, offset+16, 4, nak.grp);
1359                                 break;
1360
1361                         default:
1362                                 /*
1363                                  * XXX - the header is variable-length,
1364                                  * as the length of the NLA depends on
1365                                  * its AFI.
1366                                  *
1367                                  * However, our structure for it is
1368                                  * fixed-length, and assumes it's a 4-byte
1369                                  * IPv4 address.
1370                                  */
1371                                 return;
1372                         }
1373
1374                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1375                                 break;
1376                         offset += plen;
1377
1378                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1379
1380                         break;
1381                 case PGM_POLL_PCKT:
1382                         type_tree = proto_item_add_subtree(tf, ett_pgm_poll);
1383
1384                         proto_tree_add_uint(type_tree, hf_pgm_poll_sqn, tvb,
1385                                 offset, 4, poll.sqn);
1386                         proto_tree_add_uint(type_tree, hf_pgm_poll_round, tvb,
1387                                 offset+4, 2, poll.round);
1388                         proto_tree_add_uint(type_tree, hf_pgm_poll_subtype, tvb,
1389                                 offset+6, 2, poll.subtype);
1390                         proto_tree_add_uint(type_tree, hf_pgm_poll_pathafi, tvb,
1391                                 offset+8, 2, poll.path_afi);
1392                         proto_tree_add_uint(type_tree, hf_pgm_poll_res, tvb,
1393                                 offset+10, 2, poll.res);
1394
1395                         switch (poll.path_afi) {
1396
1397                         case AFNUM_INET:
1398                                 proto_tree_add_ipv4(type_tree, hf_pgm_poll_path,
1399                                     tvb, offset+12, 4, poll.path);
1400                                 break;
1401
1402                         default:
1403                                 /*
1404                                  * XXX - the header is variable-length,
1405                                  * as the length of the NLA depends on
1406                                  * its AFI.
1407                                  *
1408                                  * However, our structure for it is
1409                                  * fixed-length, and assumes it's a 4-byte
1410                                  * IPv4 address.
1411                                  */
1412                                 break;
1413                         }
1414
1415                         proto_tree_add_uint(type_tree, hf_pgm_poll_backoff_ivl, tvb,
1416                                 offset+16, 4, poll.backoff_ivl);
1417                         proto_tree_add_uint(type_tree, hf_pgm_poll_rand_str, tvb,
1418                                 offset+20, 4, poll.rand_str);
1419                         proto_tree_add_uint(type_tree, hf_pgm_poll_matching_bmask, tvb,
1420                                 offset+24, 4, poll.matching_bmask);
1421
1422                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1423                                 break;
1424                         offset += plen;
1425
1426                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1427
1428                         break;
1429                 case PGM_POLR_PCKT:
1430                         type_tree = proto_item_add_subtree(tf, ett_pgm_polr);
1431
1432                         proto_tree_add_uint(type_tree, hf_pgm_polr_sqn, tvb,
1433                                 offset, 4, polr.sqn);
1434                         proto_tree_add_uint(type_tree, hf_pgm_polr_round, tvb,
1435                                 offset+4, 2, polr.round);
1436                         proto_tree_add_uint(type_tree, hf_pgm_polr_res, tvb,
1437                                 offset+6, 2, polr.res);
1438
1439                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1440                                 break;
1441                         offset += plen;
1442
1443                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1444
1445                         break;
1446                 case PGM_ACK_PCKT:
1447                         type_tree = proto_item_add_subtree(tf, ett_pgm_ack);
1448
1449                         proto_tree_add_uint(type_tree, hf_pgm_ack_sqn, tvb,
1450                                 offset, 4, ack.rx_max_sqn);
1451                         proto_tree_add_uint(type_tree, hf_pgm_ack_bitmap, tvb,
1452                                 offset+4, 4, ack.bitmap);
1453
1454                         if ((pgmhdr.opts & PGM_OPT) == FALSE)
1455                                 break;
1456                         offset += plen;
1457
1458                         dissect_pgmopts(tvb, offset, type_tree, pktname);
1459
1460                         break;
1461                 }
1462
1463         }
1464         if (isdata) {
1465                 /*
1466                  * Now see if there are any sub-dissectors, if so call them
1467                  */
1468                 offset = total_size(tvb, &pgmhdr);
1469                 decode_pgm_ports(tvb, offset, pinfo, tree, &pgmhdr);
1470         }
1471         pktname = NULL;
1472 }
1473 static const true_false_string opts_present = {
1474         "Present",
1475         "Not Present"
1476 };
1477
1478 /* Register all the bits needed with the filtering engine */
1479 void
1480 proto_register_pgm(void)
1481 {
1482   static hf_register_info hf[] = {
1483     { &hf_pgm_main_sport,
1484       { "Source Port", "pgm.hdr.sport", FT_UINT16, BASE_DEC,
1485           NULL, 0x0, "", HFILL }},
1486     { &hf_pgm_main_dport,
1487       { "Destination Port", "pgm.hdr.dport", FT_UINT16, BASE_DEC,
1488           NULL, 0x0, "", HFILL }},
1489     { &hf_pgm_main_type,
1490       { "Type", "pgm.hdr.type", FT_UINT8, BASE_HEX,
1491           VALS(type_vals), 0x0, "", HFILL }},
1492     { &hf_pgm_main_opts,
1493       { "Options", "pgm.hdr.opts", FT_UINT8, BASE_HEX,
1494           NULL, 0x0, "", HFILL }},
1495     { &hf_pgm_main_opts_opt,
1496       { "Options", "pgm.hdr.opts.opt", FT_BOOLEAN, BASE_NONE,
1497           TFS(&opts_present), PGM_OPT, "", HFILL }},
1498     { &hf_pgm_main_opts_netsig,
1499       { "Network Significant Options", "pgm.hdr.opts.netsig",
1500           FT_BOOLEAN, BASE_NONE,
1501           TFS(&opts_present), PGM_OPT_NETSIG, "", HFILL }},
1502     { &hf_pgm_main_opts_varlen,
1503       { "Variable length Parity Packet Option", "pgm.hdr.opts.varlen",
1504           FT_BOOLEAN, BASE_NONE,
1505           TFS(&opts_present), PGM_OPT_VAR_PKTLEN, "", HFILL }},
1506     { &hf_pgm_main_opts_parity,
1507       { "Parity", "pgm.hdr.opts.parity", FT_BOOLEAN, BASE_NONE,
1508           TFS(&opts_present), PGM_OPT_PARITY, "", HFILL }},
1509     { &hf_pgm_main_cksum,
1510       { "Checksum", "pgm.hdr.cksum", FT_UINT16, BASE_HEX,
1511         NULL, 0x0, "", HFILL }},
1512     { &hf_pgm_main_cksum_bad,
1513       { "Bad Checksum", "pgm.hdr.cksum_bad", FT_BOOLEAN, BASE_NONE,
1514         NULL, 0x0, "", HFILL }},
1515     { &hf_pgm_main_gsi,
1516       { "Global Source Identifier", "pgm.hdr.gsi", FT_BYTES, BASE_HEX,
1517           NULL, 0x0, "", HFILL }},
1518     { &hf_pgm_main_tsdulen,
1519       { "Transport Service Data Unit Length", "pgm.hdr.tsdulen", FT_UINT16,
1520           BASE_DEC, NULL, 0x0, "", HFILL }},
1521     { &hf_pgm_spm_sqn,
1522       { "Sequence number", "pgm.spm.sqn", FT_UINT32, BASE_HEX,
1523           NULL, 0x0, "", HFILL }},
1524     { &hf_pgm_spm_trail,
1525       { "Trailing Edge Sequence Number", "pgm.spm.trail", FT_UINT32, BASE_HEX,
1526           NULL, 0x0, "", HFILL }},
1527     { &hf_pgm_spm_lead,
1528       { "Leading Edge Sequence Number", "pgm.spm.lead", FT_UINT32, BASE_HEX,
1529           NULL, 0x0, "", HFILL }},
1530     { &hf_pgm_spm_pathafi,
1531       { "Path NLA AFI", "pgm.spm.pathafi", FT_UINT16, BASE_DEC,
1532           VALS(afn_vals), 0x0, "", HFILL }},
1533     { &hf_pgm_spm_res,
1534       { "Reserved", "pgm.spm.res", FT_UINT16, BASE_HEX,
1535           NULL, 0x0, "", HFILL }},
1536     { &hf_pgm_spm_path,
1537       { "Path NLA", "pgm.spm.path", FT_IPv4, BASE_NONE,
1538           NULL, 0x0, "", HFILL }},
1539     { &hf_pgm_data_sqn,
1540       { "Data Packet Sequence Number", "pgm.data.sqn", FT_UINT32, BASE_HEX,
1541           NULL, 0x0, "", HFILL }},
1542     { &hf_pgm_data_trail,
1543       { "Trailing Edge Sequence Number", "pgm.data.trail", FT_UINT32, BASE_HEX,
1544           NULL, 0x0, "", HFILL }},
1545     { &hf_pgm_nak_sqn,
1546       { "Requested Sequence Number", "pgm.nak.sqn", FT_UINT32, BASE_HEX,
1547           NULL, 0x0, "", HFILL }},
1548     { &hf_pgm_nak_srcafi,
1549       { "Source NLA AFI", "pgm.nak.srcafi", FT_UINT16, BASE_DEC,
1550           VALS(afn_vals), 0x0, "", HFILL }},
1551     { &hf_pgm_nak_srcres,
1552       { "Reserved", "pgm.nak.srcres", FT_UINT16, BASE_HEX,
1553           NULL, 0x0, "", HFILL }},
1554     { &hf_pgm_nak_src,
1555       { "Source NLA", "pgm.nak.src", FT_IPv4, BASE_NONE,
1556           NULL, 0x0, "", HFILL }},
1557     { &hf_pgm_nak_grpafi,
1558       { "Multicast Group AFI", "pgm.nak.grpafi", FT_UINT16, BASE_DEC,
1559           VALS(afn_vals), 0x0, "", HFILL }},
1560     { &hf_pgm_nak_grpres,
1561       { "Reserved", "pgm.nak.grpres", FT_UINT16, BASE_HEX,
1562           NULL, 0x0, "", HFILL }},
1563     { &hf_pgm_nak_grp,
1564       { "Multicast Group NLA", "pgm.nak.grp", FT_IPv4, BASE_NONE,
1565           NULL, 0x0, "", HFILL }},
1566     { &hf_pgm_poll_sqn,
1567       { "Sequence Number", "pgm.poll.sqn", FT_UINT32, BASE_HEX,
1568           NULL, 0x0, "", HFILL }},
1569     { &hf_pgm_poll_round,
1570       { "Round", "pgm.poll.round", FT_UINT16, BASE_DEC,
1571           NULL, 0x0, "", HFILL }},
1572     { &hf_pgm_poll_subtype,
1573       { "Subtype", "pgm.poll.subtype", FT_UINT16, BASE_HEX,
1574           VALS(poll_subtype_vals), 0x0, "", HFILL }},
1575     { &hf_pgm_poll_pathafi,
1576       { "Path NLA AFI", "pgm.poll.pathafi", FT_UINT16, BASE_DEC,
1577           VALS(afn_vals), 0x0, "", HFILL }},
1578     { &hf_pgm_poll_res,
1579       { "Reserved", "pgm.poll.res", FT_UINT16, BASE_HEX,
1580           NULL, 0x0, "", HFILL }},
1581     { &hf_pgm_poll_path,
1582       { "Path NLA", "pgm.poll.path", FT_IPv4, BASE_NONE,
1583           NULL, 0x0, "", HFILL }},
1584     { &hf_pgm_poll_backoff_ivl,
1585       { "Back-off Interval", "pgm.poll.backoff_ivl", FT_UINT32, BASE_DEC,
1586           NULL, 0x0, "", HFILL }},
1587     { &hf_pgm_poll_rand_str,
1588       { "Random String", "pgm.poll.rand_str", FT_UINT32, BASE_HEX,
1589           NULL, 0x0, "", HFILL }},
1590     { &hf_pgm_poll_matching_bmask,
1591       { "Matching Bitmask", "pgm.poll.matching_bmask", FT_UINT32, BASE_HEX,
1592           NULL, 0x0, "", HFILL }},
1593     { &hf_pgm_polr_sqn,
1594       { "Sequence Number", "pgm.polr.sqn", FT_UINT32, BASE_HEX,
1595           NULL, 0x0, "", HFILL }},
1596     { &hf_pgm_polr_round,
1597       { "Round", "pgm.polr.round", FT_UINT16, BASE_DEC,
1598           NULL, 0x0, "", HFILL }},
1599     { &hf_pgm_polr_res,
1600       { "Reserved", "pgm.polr.res", FT_UINT16, BASE_HEX,
1601           NULL, 0x0, "", HFILL }},
1602     { &hf_pgm_ack_sqn,
1603       { "Maximum Received Sequence Number", "pgm.ack.maxsqn", FT_UINT32,
1604           BASE_HEX, NULL, 0x0, "", HFILL }},
1605     { &hf_pgm_ack_bitmap,
1606       { "Packet Bitmap", "pgm.ack.bitmap", FT_UINT32, BASE_HEX,
1607           NULL, 0x0, "", HFILL }},
1608     { &hf_pgm_opt_type,
1609       { "Type", "pgm.opts.type", FT_UINT8, BASE_HEX,
1610           VALS(opt_vals), 0x0, "", HFILL }},
1611     { &hf_pgm_opt_len,
1612       { "Length", "pgm.opts.len", FT_UINT8, BASE_DEC,
1613           NULL, 0x0, "", HFILL }},
1614     { &hf_pgm_opt_tlen,
1615       { "Total Length", "pgm.opts.tlen", FT_UINT16, BASE_DEC,
1616           NULL, 0x0, "", HFILL }},
1617     { &hf_pgm_genopt_type,
1618       { "Type", "pgm.genopts.type", FT_UINT8, BASE_HEX,
1619           VALS(opt_vals), 0x0, "", HFILL }},
1620     { &hf_pgm_genopt_len,
1621       { "Length", "pgm.genopts.len", FT_UINT8, BASE_DEC,
1622           NULL, 0x0, "", HFILL }},
1623     { &hf_pgm_genopt_opx,
1624       { "Option Extensibility Bits", "pgm.genopts.opx", FT_UINT8, BASE_HEX,
1625           VALS(opx_vals), 0x0, "", HFILL }},
1626     { &hf_pgm_opt_parity_prm_po,
1627       { "Parity Parameters", "pgm.opts.parity_prm.op", FT_UINT8, BASE_HEX,
1628           NULL, 0x0, "", HFILL }},
1629     { &hf_pgm_opt_parity_prm_prmtgsz,
1630       { "Transmission Group Size", "pgm.opts.parity_prm.prm_grp",
1631           FT_UINT32, BASE_HEX,
1632           NULL, 0x0, "", HFILL }},
1633     { &hf_pgm_opt_join_res,
1634       { "Reserved", "pgm.opts.join.res", FT_UINT8, BASE_HEX,
1635           NULL, 0x0, "", HFILL }},
1636     { &hf_pgm_opt_join_minjoin,
1637       { "Minimum Sequence Number", "pgm.opts.join.min_join",
1638           FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
1639     { &hf_pgm_opt_parity_grp_res,
1640       { "Reserved", "pgm.opts.parity_prm.op", FT_UINT8, BASE_HEX,
1641           NULL, 0x0, "", HFILL }},
1642     { &hf_pgm_opt_parity_grp_prmgrp,
1643       { "Transmission Group Size", "pgm.opts.parity_prm.prm_grp",
1644           FT_UINT32, BASE_HEX,
1645           NULL, 0x0, "", HFILL }},
1646     { &hf_pgm_opt_nak_res,
1647       { "Reserved", "pgm.opts.nak.op", FT_UINT8, BASE_HEX,
1648           NULL, 0x0, "", HFILL }},
1649     { &hf_pgm_opt_nak_list,
1650       { "List", "pgm.opts.nak.list", FT_BYTES, BASE_NONE,
1651           NULL, 0x0, "", HFILL }},
1652     { &hf_pgm_opt_ccdata_res,
1653       { "Reserved", "pgm.opts.ccdata.res", FT_UINT8, BASE_DEC,
1654           NULL, 0x0, "", HFILL }},
1655     { &hf_pgm_opt_ccdata_tsp,
1656       { "Time Stamp", "pgm.opts.ccdata.tstamp", FT_UINT16, BASE_HEX,
1657           NULL, 0x0, "", HFILL }},
1658     { &hf_pgm_opt_ccdata_afi,
1659       { "Acker AFI", "pgm.opts.ccdata.afi", FT_UINT16, BASE_DEC,
1660           VALS(afn_vals), 0x0, "", HFILL }},
1661     { &hf_pgm_opt_ccdata_res2,
1662       { "Reserved", "pgm.opts.ccdata.res2", FT_UINT16, BASE_DEC,
1663           NULL, 0x0, "", HFILL }},
1664     { &hf_pgm_opt_ccdata_acker,
1665       { "Acker", "pgm.opts.ccdata.acker", FT_IPv4, BASE_NONE,
1666           NULL, 0x0, "", HFILL }},
1667     { &hf_pgm_opt_ccfeedbk_res,
1668       { "Reserved", "pgm.opts.ccdata.res", FT_UINT8, BASE_DEC,
1669           NULL, 0x0, "", HFILL }},
1670     { &hf_pgm_opt_ccfeedbk_tsp,
1671       { "Time Stamp", "pgm.opts.ccdata.tstamp", FT_UINT16, BASE_HEX,
1672           NULL, 0x0, "", HFILL }},
1673     { &hf_pgm_opt_ccfeedbk_afi,
1674       { "Acker AFI", "pgm.opts.ccdata.afi", FT_UINT16, BASE_DEC,
1675           VALS(afn_vals), 0x0, "", HFILL }},
1676     { &hf_pgm_opt_ccfeedbk_lossrate,
1677       { "Loss Rate", "pgm.opts.ccdata.lossrate", FT_UINT16, BASE_HEX,
1678           NULL, 0x0, "", HFILL }},
1679     { &hf_pgm_opt_ccfeedbk_acker,
1680       { "Acker", "pgm.opts.ccdata.acker", FT_IPv4, BASE_NONE,
1681           NULL, 0x0, "", HFILL }},
1682     { &hf_pgm_opt_nak_bo_ivl_res,
1683       { "Reserved", "pgm.opts.nak_bo_ivl.res", FT_UINT8, BASE_HEX,
1684           NULL, 0x0, "", HFILL }},
1685     { &hf_pgm_opt_nak_bo_ivl_bo_ivl,
1686       { "Back-off Interval", "pgm.opts.nak_bo_ivl.bo_ivl", FT_UINT32, BASE_DEC,
1687           NULL, 0x0, "", HFILL }},
1688     { &hf_pgm_opt_nak_bo_ivl_bo_ivl_sqn,
1689       { "Back-off Interval Sequence Number", "pgm.opts.nak_bo_ivl.bo_ivl_sqn", FT_UINT32, BASE_HEX,
1690           NULL, 0x0, "", HFILL }},
1691     { &hf_pgm_opt_nak_bo_rng_res,
1692       { "Reserved", "pgm.opts.nak_bo_rng.res", FT_UINT8, BASE_HEX,
1693           NULL, 0x0, "", HFILL }},
1694     { &hf_pgm_opt_nak_bo_rng_min_bo_ivl,
1695       { "Min Back-off Interval", "pgm.opts.nak_bo_rng.min_bo_ivl", FT_UINT32, BASE_DEC,
1696           NULL, 0x0, "", HFILL }},
1697     { &hf_pgm_opt_nak_bo_rng_max_bo_ivl,
1698       { "Max Back-off Interval", "pgm.opts.nak_bo_rng.max_bo_ivl", FT_UINT32, BASE_DEC,
1699           NULL, 0x0, "", HFILL }},
1700     { &hf_pgm_opt_redirect_res,
1701       { "Reserved", "pgm.opts.redirect.res", FT_UINT8, BASE_DEC,
1702           NULL, 0x0, "", HFILL }},
1703     { &hf_pgm_opt_redirect_afi,
1704       { "DLR AFI", "pgm.opts.redirect.afi", FT_UINT16, BASE_DEC,
1705           VALS(afn_vals), 0x0, "", HFILL }},
1706     { &hf_pgm_opt_redirect_res2,
1707       { "Reserved", "pgm.opts.redirect.res2", FT_UINT16, BASE_HEX,
1708           NULL, 0x0, "", HFILL }},
1709     { &hf_pgm_opt_redirect_dlr,
1710       { "DLR", "pgm.opts.redirect.dlr", FT_IPv4, BASE_NONE,
1711           NULL, 0x0, "", HFILL }},
1712     { &hf_pgm_opt_fragment_res,
1713       { "Reserved", "pgm.opts.fragment.res", FT_UINT8, BASE_HEX,
1714           NULL, 0x0, "", HFILL }},
1715     { &hf_pgm_opt_fragment_first_sqn,
1716       { "First Sequence Number", "pgm.opts.fragment.first_sqn", FT_UINT32, BASE_HEX,
1717           NULL, 0x0, "", HFILL }},
1718     { &hf_pgm_opt_fragment_total_length,
1719       { "Total Length", "pgm.opts.fragment.total_length", FT_UINT32, BASE_DEC,
1720           NULL, 0x0, "", HFILL }},
1721   };
1722   static gint *ett[] = {
1723     &ett_pgm,
1724         &ett_pgm_optbits,
1725         &ett_pgm_spm,
1726         &ett_pgm_data,
1727         &ett_pgm_nak,
1728         &ett_pgm_poll,
1729         &ett_pgm_polr,
1730         &ett_pgm_ack,
1731         &ett_pgm_opts,
1732         &ett_pgm_opts_join,
1733         &ett_pgm_opts_parityprm,
1734         &ett_pgm_opts_paritygrp,
1735         &ett_pgm_opts_naklist,
1736         &ett_pgm_opts_ccdata,
1737         &ett_pgm_opts_nak_bo_ivl,
1738         &ett_pgm_opts_nak_bo_rng,
1739         &ett_pgm_opts_redirect,
1740         &ett_pgm_opts_fragment,
1741   };
1742   module_t *pgm_module;
1743
1744   proto_pgm = proto_register_protocol("Pragmatic General Multicast",
1745                                        "PGM", "pgm");
1746
1747   proto_register_field_array(proto_pgm, hf, array_length(hf));
1748   proto_register_subtree_array(ett, array_length(ett));
1749
1750         /* subdissector code */
1751   subdissector_table = register_dissector_table("pgm.port",
1752                 "PGM port", FT_UINT16, BASE_DEC);
1753   register_heur_dissector_list("pgm", &heur_subdissector_list);
1754
1755   /*
1756    * Register configuration preferences for UDP encapsulation
1757    * (Note: Initially the ports are set to zero so the
1758    *        dissecting of PGM encapsulated in UPD packets
1759    *        is off by default)
1760    */
1761    pgm_module = prefs_register_protocol(proto_pgm, proto_rereg_pgm);
1762
1763    prefs_register_bool_preference(pgm_module, "check_checksum",
1764             "Check the validity of the PGM checksum when possible",
1765                 "Whether to check the validity of the PGM checksum",
1766             &pgm_check_checksum);
1767
1768    prefs_register_uint_preference(pgm_module, "udp.encap_ucast_port",
1769                 "PGM Encap Unicast Port (standard is 3055)",
1770                 "PGM Encap is PGM packets encapsulated in UDP packets"
1771                 " (Note: This option is off, i.e. port is 0, by default)",
1772                 10, &udp_encap_ucast_port);
1773    old_encap_ucast_port = udp_encap_ucast_port;
1774
1775    prefs_register_uint_preference(pgm_module, "udp.encap_mcast_port",
1776                 "PGM Encap Multicast Port (standard is 3056)",
1777                 "PGM Encap is PGM packets encapsulated in UDP packets"
1778                 " (Note: This option is off, i.e. port is 0, by default)",
1779                 10, &udp_encap_mcast_port);
1780
1781    old_encap_mcast_port = udp_encap_mcast_port;
1782 }
1783
1784 static dissector_handle_t pgm_handle;
1785
1786 /* The registration hand-off routine */
1787 void
1788 proto_reg_handoff_pgm(void)
1789 {
1790   pgm_handle = create_dissector_handle(dissect_pgm, proto_pgm);
1791
1792   /*
1793    * Set up PGM Encap dissecting, which is off by default
1794    */
1795   dissector_add("udp.port", udp_encap_ucast_port, pgm_handle);
1796   dissector_add("udp.port", udp_encap_mcast_port, pgm_handle);
1797
1798   dissector_add("ip.proto", IP_PROTO_PGM, pgm_handle);
1799
1800   data_handle = find_dissector("data");
1801 }
1802
1803 static void
1804 proto_rereg_pgm(void)
1805 {
1806         /*
1807          * Remove the old ones
1808          */
1809         dissector_delete("udp.port", old_encap_ucast_port, pgm_handle);
1810         dissector_delete("udp.port", old_encap_mcast_port, pgm_handle);
1811
1812         /*
1813          * Set the new ones
1814          */
1815         dissector_add("udp.port", udp_encap_ucast_port, pgm_handle);
1816         dissector_add("udp.port", udp_encap_mcast_port, pgm_handle);
1817 }