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