From Lars Roland:
[obnox/wireshark/wip.git] / packet-pim.c
1 /* packet-pim.c
2  * Routines for PIM disassembly
3  * (c) Copyright Jun-ichiro itojun Hagino <itojun@itojun.org>
4  *
5  * $Id: packet-pim.c,v 1.46 2003/11/16 23:17:20 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stddef.h>  /* For offsetof */
31 #include <string.h>
32 #include <glib.h>
33
34 #ifdef NEED_SNPRINTF_H
35 # include "snprintf.h"
36 #endif
37
38 #include <epan/packet.h>
39 #include "ipproto.h"
40 #include "afn.h"
41 #include "packet-ipv6.h"
42 #include "in_cksum.h"
43
44 #define PIM_TYPE(x)     ((x) & 0x0f)
45 #define PIM_VER(x)      (((x) & 0xf0) >> 4)
46
47 enum pimv2_addrtype {
48         pimv2_unicast, pimv2_group, pimv2_source
49 };
50
51 static int proto_pim = -1;
52 static int hf_pim_version = -1;
53 static int hf_pim_type = -1;
54 static int hf_pim_code = -1;
55 static int hf_pim_cksum = -1;
56
57 static gint ett_pim = -1;
58
59 static dissector_handle_t ip_handle;
60 static dissector_handle_t ipv6_handle;
61
62 /*
63  * For PIM v1, see the PDF slides at
64  *
65  *      http://www.mbone.de/training/Module3.pdf
66  *
67  * Is it documented anywhere else?
68  */
69 static const char *
70 dissect_pimv1_addr(tvbuff_t *tvb, int offset) {
71     static char buf[512];
72     guint16 flags_masklen;
73
74     flags_masklen = tvb_get_ntohs(tvb, offset);
75     if (flags_masklen & 0x0180) {
76         (void)snprintf(buf, sizeof(buf),
77             "(%s%s%s) ",
78             flags_masklen & 0x0100 ? "S" : "",
79             flags_masklen & 0x0080 ? "W" : "",
80             flags_masklen & 0x0040 ? "R" : "");
81     } else
82         buf[0] = '\0';
83     (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s/%u",
84         ip_to_str(tvb_get_ptr(tvb, offset + 2, 4)), flags_masklen & 0x3f);
85
86     return buf;
87 }
88
89 static const value_string type1vals[] = {
90     { 0, "Query" },
91     { 1, "Register" },
92     { 2, "Register-stop" },
93     { 3, "Join/Prune" },
94     { 4, "RP-Reachable" },
95     { 5, "Assert" },
96     { 6, "Graft" },
97     { 7, "Graft-Ack" },
98     { 8, "Mode" },
99     { 0, NULL },
100 };
101
102 /* This function is only called from the IGMP dissector */
103 int
104 dissect_pimv1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
105               int offset) {
106     guint8 pim_type;
107     guint8 pim_ver;
108     guint length, pim_length;
109     guint16 pim_cksum, computed_cksum;
110     vec_t cksum_vec[1];
111     proto_tree *pim_tree = NULL;
112     proto_item *ti;
113     proto_tree *pimopt_tree = NULL;
114     proto_item *tiopt;
115
116     if (!proto_is_protocol_enabled(find_protocol_by_id(proto_pim))) {
117         /*
118          * We are not enabled; skip entire packet to be nice to the
119          * IGMP layer (so clicking on IGMP will display the data).
120          */
121         return offset+tvb_length_remaining(tvb, offset);
122     }
123
124     if (check_col(pinfo->cinfo, COL_PROTOCOL))
125         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PIMv1");
126     if (check_col(pinfo->cinfo, COL_INFO))
127         col_clear(pinfo->cinfo, COL_INFO);
128
129     if (tree) {
130         ti = proto_tree_add_item(tree, proto_pim, tvb, offset, -1, FALSE);
131         pim_tree = proto_item_add_subtree(ti, ett_pim);
132
133         /* Put IGMP type, 0x14, into the tree */
134         proto_tree_add_text(pim_tree, tvb, offset, 1,
135             "Type: PIM (0x14)");
136     }
137     offset += 1;
138
139     pim_type = tvb_get_guint8(tvb, offset);
140     if (check_col(pinfo->cinfo, COL_INFO))
141         col_add_str(pinfo->cinfo, COL_INFO,
142             val_to_str(pim_type, type1vals, "Unknown (%u)"));
143
144     if (tree) {
145         proto_tree_add_uint(pim_tree, hf_pim_code, tvb, offset, 1, pim_type);
146     }
147     offset += 1;
148
149     pim_cksum = tvb_get_ntohs(tvb, offset);
150     pim_ver = PIM_VER(tvb_get_guint8(tvb, offset + 2));
151     if (pim_ver != 1) {
152         /*
153          * Not PIMv1 - what gives?
154          */
155         if (tree) {
156             proto_tree_add_uint(pim_tree, hf_pim_cksum, tvb,
157                     offset, 2, pim_cksum);
158         }
159         offset += 2;
160         if (tree)
161             proto_tree_add_uint(pim_tree, hf_pim_version, tvb, offset, 1, pim_ver);
162         return offset+tvb_length_remaining(tvb, offset);
163     }
164
165     /*
166      * Well, it's PIM v1, so we can check whether this is a
167      * Register message, and thus can figure out how much to
168      * checksum and whether to make the columns read-only.
169      */
170     length = tvb_length(tvb);
171     if (pim_type == 1) {
172         /*
173          * Register message - the PIM header is 8 bytes long.
174          * Also set the columns non-writable. Otherwise the IPv4 or
175          * IPv6 dissector for the encapsulated packet that caused
176          * this register will overwrite the PIM info in the columns.
177          */
178         pim_length = 8;
179         col_set_writable(pinfo->cinfo, FALSE);
180     } else {
181         /*
182          * Other message - checksum the entire packet.
183          */
184         pim_length = tvb_reported_length(tvb);
185     }
186
187     if (tree) {
188         if (!pinfo->fragmented && length >= pim_length) {
189             /*
190              * The packet isn't part of a fragmented datagram and isn't
191              * truncated, so we can checksum it.
192              */
193             cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, pim_length);
194             cksum_vec[0].len = pim_length;
195             computed_cksum = in_cksum(&cksum_vec[0], 1);
196             if (computed_cksum == 0) {
197                 proto_tree_add_uint_format(pim_tree, hf_pim_cksum, tvb,
198                             offset, 2, pim_cksum,
199                             "Checksum: 0x%04x (correct)",
200                             pim_cksum);
201             } else {
202                 proto_tree_add_uint_format(pim_tree, hf_pim_cksum, tvb,
203                             offset, 2, pim_cksum,
204                             "Checksum: 0x%04x (incorrect, should be 0x%04x)",
205                             pim_cksum, in_cksum_shouldbe(pim_cksum, computed_cksum));
206             }
207         } else {
208             proto_tree_add_uint(pim_tree, hf_pim_cksum, tvb,
209                     offset, 2, pim_cksum);
210         }
211     }
212     offset += 2;
213
214     if (tree)
215         proto_tree_add_uint(pim_tree, hf_pim_version, tvb, offset, 1, pim_ver);
216     offset += 1;
217
218     offset += 3;        /* skip reserved stuff */
219
220     if (tree) {
221         if (tvb_reported_length_remaining(tvb, offset) > 0) {
222             tiopt = proto_tree_add_text(pim_tree, tvb, offset, -1,
223                     "PIM parameters");
224             pimopt_tree = proto_item_add_subtree(tiopt, ett_pim);
225         } else
226             goto done;
227
228         /* version 1 decoder */
229         switch (pim_type) {
230         case 0: /* query */
231           {
232             guint8 mode;
233             guint16 holdtime;
234             static const value_string pimv1_modevals[] = {
235                 { 0, "Dense" },
236                 { 1, "Sparse" },
237                 { 2, "Sparse-Dense" },
238                 { 0, NULL },
239             };
240
241             mode = tvb_get_guint8(tvb, offset) >> 4;
242             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
243                 "Mode: %s",
244                 val_to_str(mode, pimv1_modevals, "Unknown (%u)"));
245             offset += 2;
246             holdtime = tvb_get_ntohs(tvb, offset);
247             proto_tree_add_text(pimopt_tree, tvb, offset, 2,
248                 "Holdtime: %u%s", holdtime,
249                 holdtime == 0xffff ? " (infty)" : "");
250             offset += 2;
251             break;
252           }
253
254         case 1: /* register */
255           {
256             guint8 v_hl;
257             tvbuff_t *next_tvb;
258
259             /*
260              * The rest of the packet is a multicast data packet.
261              */
262             next_tvb = tvb_new_subset(tvb, offset, -1, -1);
263
264             /*
265              * It's an IP packet - determine whether it's IPv4 or IPv6.
266              */
267             v_hl = tvb_get_guint8(tvb, offset);
268             switch((v_hl & 0xf0) >> 4) {
269             case 0:     /* Null-Register dummy header.
270                          * Has the same address family as the encapsulating PIM packet,
271                          * e.g. an IPv6 data packet is encapsulated in IPv6 PIM packet.
272                          */
273                     if (pinfo->src.type == AT_IPv4) {
274                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
275                                                 "IPv4 dummy header");
276                             proto_tree_add_text(pimopt_tree, tvb, offset + 12, 4,
277                                                 "Source: %s",
278                                                 ip_to_str(tvb_get_ptr(tvb, offset + 12, 4)));
279                             proto_tree_add_text(pimopt_tree, tvb, offset + 16, 4,
280                                                 "Group: %s",
281                                                 ip_to_str(tvb_get_ptr(tvb, offset + 16, 4)));
282                     } else if (pinfo->src.type == AT_IPv6) {
283                             struct ip6_hdr ip6_hdr;
284                             tvb_memcpy(tvb, (guint8 *)&ip6_hdr, offset,
285                                        sizeof ip6_hdr);
286                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
287                                                 "IPv6 dummy header");
288                             proto_tree_add_text(pimopt_tree, tvb,
289                                                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
290                                                 "Source: %s",
291                                                 ip6_to_str(&ip6_hdr.ip6_src));
292                             proto_tree_add_text(pimopt_tree, tvb,
293                                                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
294                                                 "Group: %s",
295                                                 ip6_to_str(&ip6_hdr.ip6_dst));
296                     } else
297                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
298                                                 "Dummy header for an unknown protocol");
299                     break;
300             case 4:     /* IPv4 */
301 #if 0
302                     call_dissector(ip_handle, next_tvb, pinfo, tree);
303 #else
304                     call_dissector(ip_handle, next_tvb, pinfo, pimopt_tree);
305 #endif
306                     break;
307             case 6:     /* IPv6 */
308 #if 0
309                     call_dissector(ipv6_handle, next_tvb, pinfo, tree);
310 #else
311                     call_dissector(ipv6_handle, next_tvb, pinfo, pimopt_tree);
312 #endif
313                     break;
314             default:
315                     proto_tree_add_text(pimopt_tree, tvb, offset, -1,
316                         "Unknown IP version %d", (v_hl & 0xf0) >> 4);
317                     break;
318             }
319             break;
320           }
321
322         case 2: /* register-stop */
323           {
324             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
325                 "Group: %s",
326                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
327             offset += 4;
328             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
329                 "Source: %s",
330                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
331             offset += 4;
332             break;
333           }
334
335         case 3: /* join/prune */
336         case 6: /* graft */
337         case 7: /* graft-ack */
338           {
339             int off;
340             const char *s;
341             int ngroup, i, njoin, nprune, j;
342             guint16 holdtime;
343             guint8 mask_len;
344             guint8 adr_len;
345             proto_tree *grouptree = NULL;
346             proto_item *tigroup;
347             proto_tree *subtree = NULL;
348             proto_item *tisub;
349
350             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
351                 "Upstream-neighbor: %s",
352                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
353             offset += 4;
354
355             offset += 2;        /* skip reserved stuff */
356
357             holdtime = tvb_get_ntohs(tvb, offset);
358             proto_tree_add_text(pimopt_tree, tvb, offset, 2,
359                 "Holdtime: %u%s", holdtime,
360                 holdtime == 0xffff ? " (infty)" : "");
361             offset += 2;
362
363             offset += 1;        /* skip reserved stuff */
364
365             mask_len = tvb_get_guint8(tvb, offset);
366             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
367                 "Mask length: %u", mask_len);
368             offset += 1;
369
370             adr_len = tvb_get_guint8(tvb, offset);
371             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
372                 "Address length: %u", adr_len);
373             offset += 1;
374
375             ngroup = tvb_get_guint8(tvb, offset);
376             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
377                 "Groups: %u", ngroup);
378             offset += 1;
379
380             for (i = 0; i < ngroup; i++) {
381                 tigroup = proto_tree_add_text(pimopt_tree, tvb, offset, 4,
382                     "Group %d: %s", i,
383                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
384                 grouptree = proto_item_add_subtree(tigroup, ett_pim);
385                 offset += 4;
386
387                 proto_tree_add_text(grouptree, tvb, offset, 4,
388                     "Group %d Mask: %s", i,
389                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
390                 offset += 4;
391
392                 njoin = tvb_get_ntohs(tvb, offset);
393                 nprune = tvb_get_ntohs(tvb, offset + 2);
394
395                 tisub = proto_tree_add_text(grouptree, tvb, offset, 2,
396                     "Join: %d", njoin);
397                 subtree = proto_item_add_subtree(tisub, ett_pim);
398                 off = offset + 4;
399                 for (j = 0; j < njoin; j++) {
400                     s = dissect_pimv1_addr(tvb, off);
401                     proto_tree_add_text(subtree, tvb, off, 6,
402                         "IP address: %s", s);
403                     off += 6;
404                 }
405
406                 tisub = proto_tree_add_text(grouptree, tvb, offset + 2, 2,
407                     "Prune: %d", nprune);
408                 subtree = proto_item_add_subtree(tisub, ett_pim);
409                 for (j = 0; j < nprune; j++) {
410                     s = dissect_pimv1_addr(tvb, off);
411                     proto_tree_add_text(subtree, tvb, off, 6,
412                         "IP address: %s", s);
413                     off += 6;
414                 }
415                 offset = off;
416             }
417             break;
418           }
419
420         case 4: /* rp-reachability */
421           {
422             guint16 holdtime;
423
424             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
425                 "Group Address: %s",
426                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
427             offset += 4;
428
429             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
430                 "Group Mask: %s",
431                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
432             offset += 4;
433
434             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
435                 "RP Address: %s",
436                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
437             offset += 4;
438
439             offset += 2;        /* skip reserved stuff */
440
441             holdtime = tvb_get_ntohs(tvb, offset);
442             proto_tree_add_text(pimopt_tree, tvb, offset, 2,
443                 "Holdtime: %u%s", holdtime,
444                 holdtime == 0xffff ? " (infty)" : "");
445             offset += 2;
446             break;
447           }
448
449         case 5: /* assert */
450           {
451             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
452                 "Group Address: %s",
453                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
454             offset += 4;
455
456             proto_tree_add_text(pimopt_tree, tvb, offset, 4,
457                 "Group Mask: %s",
458                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
459             offset += 4;
460
461             proto_tree_add_text(pimopt_tree, tvb, offset, 1, "%s",
462                 decode_boolean_bitfield(tvb_get_guint8(tvb, offset), 0x80, 8,
463                     "RP Tree", "Not RP Tree"));
464             proto_tree_add_text(pimopt_tree, tvb, offset, 4, "Preference: %u",
465                 tvb_get_ntohl(tvb, offset) & 0x7fffffff);
466             offset += 4;
467
468             proto_tree_add_text(pimopt_tree, tvb, offset, 4, "Metric: %u",
469                 tvb_get_ntohl(tvb, offset));
470
471             break;
472           }
473
474         default:
475             break;
476         }
477     }
478 done:;
479
480     return offset+tvb_length_remaining(tvb, offset);
481 }
482
483 static const char *
484 dissect_pim_addr(tvbuff_t *tvb, int offset, enum pimv2_addrtype at,
485         int *advance) {
486     static char buf[512];
487     guint8 af;
488     guint8 et;
489     guint8 flags;
490     guint8 mask_len;
491     int len = 0;
492
493     af = tvb_get_guint8(tvb, offset);
494     if (af != AFNUM_INET && af != AFNUM_INET6) {
495         /*
496          * We don't handle the other formats, and addresses don't include
497          * a length field, so we can't even show them as raw bytes.
498          */
499         return NULL;
500     }
501
502     et = tvb_get_guint8(tvb, offset + 1);
503     if (et != 0) {
504         /*
505          * The only defined encoding type is 0, for the native encoding;
506          * again, as addresses don't include a length field, we can't
507          * even show addresses with a different encoding type as raw
508          * bytes.
509          */
510         return NULL;
511     }
512
513     switch (at) {
514     case pimv2_unicast:
515         switch (af) {
516         case AFNUM_INET:
517             len = 4;
518             (void)snprintf(buf, sizeof(buf), "%s",
519                 ip_to_str(tvb_get_ptr(tvb, offset + 2, len)));
520             break;
521
522         case AFNUM_INET6:
523             len = 16;
524             (void)snprintf(buf, sizeof(buf), "%s",
525                 ip6_to_str((const struct e_in6_addr *)tvb_get_ptr(tvb, offset + 2, len)));
526             break;
527         }
528         if (advance)
529             *advance = 2 + len;
530         break;
531
532     case pimv2_group:
533         mask_len = tvb_get_guint8(tvb, offset + 3);
534         switch (af) {
535         case AFNUM_INET:
536             len = 4;
537             (void)snprintf(buf, sizeof(buf), "%s/%u",
538                 ip_to_str(tvb_get_ptr(tvb, offset + 4, len)), mask_len);
539             break;
540
541         case AFNUM_INET6:
542             len = 16;
543             (void)snprintf(buf, sizeof(buf), "%s/%u",
544                 ip6_to_str((const struct e_in6_addr *)tvb_get_ptr(tvb, offset + 4, len)), mask_len);
545             break;
546         }
547         if (advance)
548             *advance = 4 + len;
549         break;
550
551     case pimv2_source:
552         flags = tvb_get_guint8(tvb, offset + 2);
553         mask_len = tvb_get_guint8(tvb, offset + 3);
554         switch (af) {
555         case AFNUM_INET:
556             len = 4;
557             (void)snprintf(buf, sizeof(buf), "%s/%u",
558                 ip_to_str(tvb_get_ptr(tvb, offset + 4, len)), mask_len);
559             break;
560
561         case AFNUM_INET6:
562             len = 16;
563             (void)snprintf(buf, sizeof(buf), "%s/%u",
564                 ip6_to_str((const struct e_in6_addr *)tvb_get_ptr(tvb, offset + 4, len)), mask_len);
565             break;
566         }
567         if (flags) {
568             (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
569                 " (%s%s%s)",
570                 flags & 0x04 ? "S" : "",
571                 flags & 0x02 ? "W" : "",
572                 flags & 0x01 ? "R" : "");
573         }
574         if (advance)
575             *advance = 4 + len;
576         break;
577     default:
578         return NULL;
579     }
580
581     return buf;
582 }
583
584 static const value_string type2vals[] = {
585     { 0, "Hello" },
586     { 1, "Register" },
587     { 2, "Register-stop" },
588     { 3, "Join/Prune" },
589     { 4, "Bootstrap" },
590     { 5, "Assert" },
591     { 6, "Graft" },
592     { 7, "Graft-Ack" },
593     { 8, "Candidate-RP-Advertisement" },
594     { 0, NULL },
595 };
596
597 /*
598  * For PIM v2, see RFC 2362, and draft-ietf-pim-sm-v2-new-03 (when PIM
599  * is run over IPv6, the rules for computing the PIM checksum from the
600  * draft in question, not from RFC 2362, should be used).
601  */
602 static void
603 dissect_pim(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
604     int offset = 0;
605     guint8 pim_typever;
606     guint length, pim_length;
607     guint16 pim_cksum, computed_cksum;
608     vec_t cksum_vec[4];
609     guint32 phdr[2];
610     char *typestr;
611     proto_tree *pim_tree = NULL;
612     proto_item *ti;
613     proto_tree *pimopt_tree = NULL;
614     proto_item *tiopt;
615
616     if (check_col(pinfo->cinfo, COL_PROTOCOL))
617         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PIM");
618     if (check_col(pinfo->cinfo, COL_INFO))
619         col_clear(pinfo->cinfo, COL_INFO);
620
621     pim_typever = tvb_get_guint8(tvb, 0);
622
623     switch (PIM_VER(pim_typever)) {
624     case 2:
625         typestr = val_to_str(PIM_TYPE(pim_typever), type2vals, "Unknown (%u)");
626         break;
627     case 1:     /* PIMv1 - we should never see this */
628     default:
629         typestr = "Unknown";
630         break;
631     }
632
633     if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
634         col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "PIMv%d",
635             PIM_VER(pim_typever));
636     }
637     if (check_col(pinfo->cinfo, COL_INFO))
638         col_add_str(pinfo->cinfo, COL_INFO, typestr);
639
640     if (tree) {
641         ti = proto_tree_add_item(tree, proto_pim, tvb, offset, -1, FALSE);
642         pim_tree = proto_item_add_subtree(ti, ett_pim);
643
644         proto_tree_add_uint(pim_tree, hf_pim_version, tvb, offset, 1,
645             PIM_VER(pim_typever));
646         proto_tree_add_uint(pim_tree, hf_pim_type, tvb, offset, 1,
647             PIM_TYPE(pim_typever));
648
649         pim_cksum = tvb_get_ntohs(tvb, offset + 2);
650         length = tvb_length(tvb);
651         if (PIM_VER(pim_typever) == 2) {
652             /*
653              * Well, it's PIM v2, so we can check whether this is a Register
654              * message, and thus can figure out how much to checksum and
655              * whether to make the columns read-only.
656              */
657             if (PIM_TYPE(pim_typever) == 1) {
658                 /*
659                  * Register message - the PIM header is 8 bytes long.
660                  * Also set the columns non-writable. Otherwise the IPv4 or
661                  * IPv6 dissector for the encapsulated packet that caused
662                  * this register will overwrite the PIM info in the columns.
663                  */
664                 pim_length = 8;
665                 col_set_writable(pinfo->cinfo, FALSE);
666             } else {
667                 /*
668                  * Other message - checksum the entire packet.
669                  */
670                 pim_length = tvb_reported_length(tvb);
671             }
672         } else {
673             /*
674              * We don't know what type of message this is, so say that
675              * the length is 0, to force it not to be checksummed.
676              */
677             pim_length = 0;
678         }
679         if (!pinfo->fragmented && length >= pim_length) {
680             /*
681              * The packet isn't part of a fragmented datagram and isn't
682              * truncated, so we can checksum it.
683              */
684
685             switch (pinfo->src.type) {
686             case AT_IPv4:
687                 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, pim_length);
688                 cksum_vec[0].len = pim_length;
689                 computed_cksum = in_cksum(&cksum_vec[0], 1);
690                 break;
691             case AT_IPv6:
692                 /* Set up the fields of the pseudo-header. */
693                 cksum_vec[0].ptr = pinfo->src.data;
694                 cksum_vec[0].len = pinfo->src.len;
695                 cksum_vec[1].ptr = pinfo->dst.data;
696                 cksum_vec[1].len = pinfo->dst.len;
697                 cksum_vec[2].ptr = (const guint8 *)&phdr;
698                 phdr[0] = g_htonl(pim_length);
699                 phdr[1] = g_htonl(IP_PROTO_PIM);
700                 cksum_vec[2].len = 8;
701                 cksum_vec[3].ptr = tvb_get_ptr(tvb, 0, pim_length);
702                 cksum_vec[3].len = pim_length;
703                 computed_cksum = in_cksum(&cksum_vec[0], 4);
704                 break;
705             default:
706                 /* PIM is available for IPv4 and IPv6 right now */
707                 computed_cksum = 0;     /* squelch GCC complaints */
708                 g_assert_not_reached();
709                 break;
710             }
711
712             if (computed_cksum == 0) {
713                 proto_tree_add_uint_format(pim_tree, hf_pim_cksum, tvb,
714                         offset + 2, 2, pim_cksum,
715                         "Checksum: 0x%04x (correct)",
716                         pim_cksum);
717             } else {
718                 proto_tree_add_uint_format(pim_tree, hf_pim_cksum, tvb,
719                         offset + 2, 2, pim_cksum,
720                         "Checksum: 0x%04x (incorrect, should be 0x%04x)",
721                         pim_cksum, in_cksum_shouldbe(pim_cksum, computed_cksum));
722             }
723         } else {
724             proto_tree_add_uint(pim_tree, hf_pim_cksum, tvb,
725                 offset + 2, 2, pim_cksum);
726         }
727
728         offset += 4;
729
730         if (tvb_reported_length_remaining(tvb, offset) > 0) {
731             tiopt = proto_tree_add_text(pim_tree, tvb, offset, -1,
732                 "PIM parameters");
733             pimopt_tree = proto_item_add_subtree(tiopt, ett_pim);
734         } else
735             goto done;
736
737         if (PIM_VER(pim_typever) != 2)
738             goto done;
739
740         /* version 2 decoder */
741         switch (PIM_TYPE(pim_typever)) {
742         case 0: /*hello*/
743           {
744             while (tvb_reported_length_remaining(tvb, offset) >= 2) {
745                 guint16 hello_opt, opt_len;
746                 guint16 holdtime;
747                 guint16 lan_delay;
748                 guint16 override_interval;
749                 guint32 priority;
750                 guint32 opt_value = 0;
751
752                 hello_opt = tvb_get_ntohs(tvb, offset);
753                 opt_len = tvb_get_ntohs(tvb, offset + 2);
754
755                 if(opt_len == 2)
756                         opt_value = tvb_get_ntohs(tvb, offset + 4);
757                 if(opt_len == 4)
758                         opt_value = tvb_get_ntohl(tvb, offset + 4);
759
760                 switch(hello_opt) {
761                 case 1: /* holdtime */
762                         holdtime = opt_value;
763                         proto_tree_add_text(pimopt_tree, tvb, offset, 4 + opt_len,
764                                             "Holdtime: %u%s", holdtime,
765                                             holdtime == 0xffff ? " (infty)" : "");
766                         break;
767                 case 2: /* LAN prune delay */
768                 {
769                         proto_tree *sub_tree = NULL;
770                         proto_item *landelay_option;
771
772                         landelay_option = proto_tree_add_text(pim_tree, tvb, offset, -1,
773                                                         "LAN Prune Delay");
774                         sub_tree = proto_item_add_subtree(landelay_option, ett_pim);
775
776                         lan_delay = (opt_value & 0x7f00) >> 8;
777                         override_interval = opt_value & 0xff;
778                         proto_tree_add_text(sub_tree, tvb, offset, 4 + opt_len,
779                                             "T bit is %s",
780                                             opt_value & 0x8000 ? "set" : "not set");
781                         proto_tree_add_text(sub_tree, tvb, offset, 4 + opt_len,
782                                             "LAN Delay:  %u", lan_delay);
783                         proto_tree_add_text(sub_tree, tvb, offset, 4 + opt_len,
784                                             "Override Interval:  %u", override_interval);
785                         break;
786                 }
787                 case 19: /* priority */
788                         priority = opt_value;
789                         proto_tree_add_text(pimopt_tree, tvb, offset, 4 + opt_len,
790                                         "DR Priority: %u", priority);
791                         break;
792                 case 20: /* generation ID */
793                         proto_tree_add_text(pimopt_tree, tvb, offset, 4 + opt_len,
794                                             "Generation ID: %d", opt_value);
795                         break;
796
797                 case 24: /* address list */
798                 case 65001: /* address list (old implementations) */
799                 {
800                         int i;
801                         proto_tree_add_text(pimopt_tree, tvb, offset, 4 + opt_len, 
802                                             "%sAddress List",
803                                             hello_opt == 65001 ? "old " : "");
804                         for (i = offset + 4; i < offset + 4 + opt_len; ) {
805                                 int advance;
806                                 const char *s;
807
808                                 s = dissect_pim_addr(tvb, i, pimv2_unicast, &advance);
809                                 if (s == NULL)
810                                         break;
811                                 proto_tree_add_text(pimopt_tree, tvb, offset, 
812                                                     advance, "Address: %s", s);
813                                 i += advance;
814                         }
815                 }
816                 default:
817                         proto_tree_add_text(pimopt_tree, tvb, offset, 4 + opt_len,
818                                             "Unknown option: %d  len: %d  value: 0x%x",
819                                             hello_opt, opt_len, opt_value);
820                         break;
821                 }
822                 offset += 4 + opt_len;
823             }
824             break;
825           }
826
827         case 1: /* register */
828           {
829             guint32 flags;
830             guint8 v_hl;
831             tvbuff_t *next_tvb;
832             proto_tree *flag_tree = NULL;
833             proto_item *tiflag;
834
835             flags = tvb_get_ntohl(tvb, offset);
836             tiflag = proto_tree_add_text(pimopt_tree, tvb, offset, 4,
837                 "Flags: 0x%08x", flags);
838             flag_tree = proto_item_add_subtree(tiflag, ett_pim);
839             proto_tree_add_text(flag_tree, tvb, offset, 1, "%s",
840                 decode_boolean_bitfield(flags, 0x80000000, 32,
841                     "Border", "Not border"));
842             proto_tree_add_text(flag_tree, tvb, offset, 1, "%s",
843                 decode_boolean_bitfield(flags, 0x40000000, 32,
844                     "Null-Register", "Not Null-Register"));
845             offset += 4;
846
847             /*
848              * The rest of the packet is a multicast data packet.
849              */
850             next_tvb = tvb_new_subset(tvb, offset, -1, -1);
851
852             /*
853              * It's an IP packet - determine whether it's IPv4 or IPv6.
854              */
855             v_hl = tvb_get_guint8(tvb, offset);
856             switch((v_hl & 0xf0) >> 4) {
857             case 0:     /* Null-Register dummy header.
858                          * Has the same address family as the encapsulating PIM packet,
859                          * e.g. an IPv6 data packet is encapsulated in IPv6 PIM packet.
860                          */
861                     if (pinfo->src.type == AT_IPv4) {
862                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
863                                                 "IPv4 dummy header");
864                             proto_tree_add_text(pimopt_tree, tvb, offset + 12, 4,
865                                                 "Source: %s",
866                                                 ip_to_str(tvb_get_ptr(tvb, offset + 12, 4)));
867                             proto_tree_add_text(pimopt_tree, tvb, offset + 16, 4,
868                                                 "Group: %s",
869                                                 ip_to_str(tvb_get_ptr(tvb, offset + 16, 4)));
870                     } else if (pinfo->src.type == AT_IPv6) {
871                             struct ip6_hdr ip6_hdr;
872                             tvb_memcpy(tvb, (guint8 *)&ip6_hdr, offset,
873                                        sizeof ip6_hdr);
874                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
875                                                 "IPv6 dummy header");
876                             proto_tree_add_text(pimopt_tree, tvb,
877                                                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
878                                                 "Source: %s",
879                                                 ip6_to_str(&ip6_hdr.ip6_src));
880                             proto_tree_add_text(pimopt_tree, tvb,
881                                                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
882                                                 "Group: %s",
883                                                 ip6_to_str(&ip6_hdr.ip6_dst));
884                     } else
885                             proto_tree_add_text(pimopt_tree, tvb, offset, -1,
886                                                 "Dummy header for an unknown protocol");
887                     break;
888             case 4:     /* IPv4 */
889 #if 0
890                     call_dissector(ip_handle, next_tvb, pinfo, tree);
891 #else
892                     call_dissector(ip_handle, next_tvb, pinfo, pimopt_tree);
893 #endif
894                     break;
895             case 6:     /* IPv6 */
896 #if 0
897                     call_dissector(ipv6_handle, next_tvb, pinfo, tree);
898 #else
899                     call_dissector(ipv6_handle, next_tvb, pinfo, pimopt_tree);
900 #endif
901                     break;
902             default:
903                     proto_tree_add_text(pimopt_tree, tvb, offset, -1,
904                         "Unknown IP version %d", (v_hl & 0xf0) >> 4);
905                     break;
906             }
907             break;
908           }
909
910         case 2: /* register-stop */
911           {
912             int advance;
913             const char *s;
914
915             s = dissect_pim_addr(tvb, offset, pimv2_group, &advance);
916             if (s == NULL)
917                 break;
918             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "Group: %s", s);
919             offset += advance;
920             s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
921             if (s == NULL)
922                 break;
923             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "Source: %s", s);
924             break;
925           }
926
927         case 3: /* join/prune */
928         case 6: /* graft */
929         case 7: /* graft-ack */
930           {
931             int advance;
932             int off;
933             const char *s;
934             int ngroup, i, njoin, nprune, j;
935             guint16 holdtime;
936             proto_tree *grouptree = NULL;
937             proto_item *tigroup;
938             proto_tree *subtree = NULL;
939             proto_item *tisub;
940
941             if (PIM_TYPE(pim_typever) != 7) {
942                 /* not graft-ack */
943                 s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
944                 if (s == NULL)
945                     break;
946                 proto_tree_add_text(pimopt_tree, tvb, offset, advance,
947                     "Upstream-neighbor: %s", s);
948                 offset += advance;
949             }
950
951             offset += 1;        /* skip reserved field */
952
953             ngroup = tvb_get_guint8(tvb, offset);
954             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
955                 "Groups: %u", ngroup);
956             offset += 1;
957
958             if (PIM_TYPE(pim_typever) != 7)     {
959                 /* not graft-ack */
960                 holdtime = tvb_get_ntohs(tvb, offset);
961                 proto_tree_add_text(pimopt_tree, tvb, offset, 2,
962                     "Holdtime: %u%s", holdtime,
963                     holdtime == 0xffff ? " (infty)" : "");
964             }
965             offset += 2;
966
967             for (i = 0; i < ngroup; i++) {
968                 s = dissect_pim_addr(tvb, offset, pimv2_group, &advance);
969                 if (s == NULL)
970                     goto breakbreak3;
971                 tigroup = proto_tree_add_text(pimopt_tree, tvb, offset, advance,
972                     "Group %d: %s", i, s);
973                 grouptree = proto_item_add_subtree(tigroup, ett_pim);
974                 offset += advance;
975
976                 njoin = tvb_get_ntohs(tvb, offset);
977                 nprune = tvb_get_ntohs(tvb, offset + 2);
978
979                 tisub = proto_tree_add_text(grouptree, tvb, offset, 2,
980                     "Join: %d", njoin);
981                 subtree = proto_item_add_subtree(tisub, ett_pim);
982                 off = offset + 4;
983                 for (j = 0; j < njoin; j++) {
984                     s = dissect_pim_addr(tvb, off, pimv2_source,
985                         &advance);
986                     if (s == NULL)
987                         goto breakbreak3;
988                     proto_tree_add_text(subtree, tvb, off, advance,
989                         "IP address: %s", s);
990                     off += advance;
991                 }
992
993                 tisub = proto_tree_add_text(grouptree, tvb, offset + 2, 2,
994                     "Prune: %d", nprune);
995                 subtree = proto_item_add_subtree(tisub, ett_pim);
996                 for (j = 0; j < nprune; j++) {
997                     s = dissect_pim_addr(tvb, off, pimv2_source,
998                         &advance);
999                     if (s == NULL)
1000                         goto breakbreak3;
1001                     proto_tree_add_text(subtree, tvb, off, advance,
1002                         "IP address: %s", s);
1003                     off += advance;
1004                 }
1005                 offset = off;
1006             }
1007     breakbreak3:
1008             break;
1009           }
1010
1011         case 4: /* bootstrap */
1012           {
1013             const char *s;
1014             int advance;
1015             int i, j;
1016             int frpcnt;
1017             guint16 holdtime;
1018             proto_tree *grouptree = NULL;
1019             proto_item *tigroup;
1020
1021             proto_tree_add_text(pimopt_tree, tvb, offset, 2,
1022                 "Fragment tag: 0x%04x", tvb_get_ntohs(tvb, offset));
1023             offset += 2;
1024
1025             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
1026                 "Hash mask len: %u", tvb_get_guint8(tvb, offset));
1027             offset += 1;
1028             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
1029                 "BSR priority: %u", tvb_get_guint8(tvb, offset));
1030             offset += 1;
1031
1032             s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
1033             if (s == NULL)
1034                 break;
1035             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "BSR: %s", s);
1036             offset += advance;
1037
1038             for (i = 0; tvb_reported_length_remaining(tvb, offset) > 0; i++) {
1039                 s = dissect_pim_addr(tvb, offset, pimv2_group, &advance);
1040                 if (s == NULL)
1041                     goto breakbreak4;
1042                 tigroup = proto_tree_add_text(pimopt_tree, tvb, offset, advance,
1043                     "Group %d: %s", i, s);
1044                 grouptree = proto_item_add_subtree(tigroup, ett_pim);
1045                 offset += advance;
1046
1047                 proto_tree_add_text(grouptree, tvb, offset, 1,
1048                     "RP count: %u", tvb_get_guint8(tvb, offset));
1049                 offset += 1;
1050                 frpcnt = tvb_get_guint8(tvb, offset);
1051                 proto_tree_add_text(grouptree, tvb, offset, 1,
1052                     "FRP count: %u", frpcnt);
1053                 offset += 3;
1054
1055                 for (j = 0; j < frpcnt; j++) {
1056                     s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
1057                     if (s == NULL)
1058                         goto breakbreak4;
1059                     proto_tree_add_text(grouptree, tvb, offset, advance,
1060                         "RP %d: %s", j, s);
1061                     offset += advance;
1062
1063                     holdtime = tvb_get_ntohs(tvb, offset);
1064                     proto_tree_add_text(grouptree, tvb, offset, 2,
1065                         "Holdtime: %u%s", holdtime,
1066                         holdtime == 0xffff ? " (infty)" : "");
1067                     offset += 2;
1068                     proto_tree_add_text(grouptree, tvb, offset, 1,
1069                         "Priority: %u", tvb_get_guint8(tvb, offset));
1070                     offset += 2;        /* also skips reserved field */
1071                 }
1072             }
1073
1074     breakbreak4:
1075             break;
1076           }
1077
1078         case 5: /* assert */
1079           {
1080             const char *s;
1081             int advance;
1082
1083             s = dissect_pim_addr(tvb, offset, pimv2_group, &advance);
1084             if (s == NULL)
1085                 break;
1086             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "Group: %s", s);
1087             offset += advance;
1088
1089             s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
1090             if (s == NULL)
1091                 break;
1092             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "Source: %s", s);
1093             offset += advance;
1094
1095             proto_tree_add_text(pimopt_tree, tvb, offset, 1, "%s",
1096                 decode_boolean_bitfield(tvb_get_guint8(tvb, offset), 0x80, 8,
1097                     "RP Tree", "Not RP Tree"));
1098             proto_tree_add_text(pimopt_tree, tvb, offset, 4, "Preference: %u",
1099                 tvb_get_ntohl(tvb, offset) & 0x7fffffff);
1100             offset += 4;
1101
1102             proto_tree_add_text(pimopt_tree, tvb, offset, 4, "Metric: %u",
1103                 tvb_get_ntohl(tvb, offset));
1104
1105             break;
1106           }
1107
1108         case 8: /* Candidate-RP-Advertisement */
1109           {
1110             const char *s;
1111             int advance;
1112             int pfxcnt;
1113             guint16 holdtime;
1114             int i;
1115
1116             pfxcnt = tvb_get_guint8(tvb, offset);
1117             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
1118                 "Prefix-count: %u", pfxcnt);
1119             offset += 1;
1120             proto_tree_add_text(pimopt_tree, tvb, offset, 1,
1121                 "Priority: %u", tvb_get_guint8(tvb, offset));
1122             offset += 1;
1123             holdtime = tvb_get_ntohs(tvb, offset);
1124             proto_tree_add_text(pimopt_tree, tvb, offset, 2,
1125                 "Holdtime: %u%s", holdtime,
1126                 holdtime == 0xffff ? " (infty)" : "");
1127             offset += 2;
1128
1129             s = dissect_pim_addr(tvb, offset, pimv2_unicast, &advance);
1130             if (s == NULL)
1131                 break;
1132             proto_tree_add_text(pimopt_tree, tvb, offset, advance, "RP: %s", s);
1133             offset += advance;
1134
1135             for (i = 0; i < pfxcnt; i++) {
1136                 s = dissect_pim_addr(tvb, offset, pimv2_group, &advance);
1137                 if (s == NULL)
1138                     goto breakbreak8;
1139                 proto_tree_add_text(pimopt_tree, tvb, offset, advance,
1140                     "Group %d: %s", i, s);
1141                 offset += advance;
1142             }
1143     breakbreak8:
1144             break;
1145           }
1146
1147         default:
1148             break;
1149         }
1150     }
1151 done:;
1152 }
1153
1154 void
1155 proto_register_pim(void)
1156 {
1157     static hf_register_info hf[] = {
1158       { &hf_pim_version,
1159         { "Version",            "pim.version",
1160                                 FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
1161       { &hf_pim_type,
1162         { "Type",               "pim.type",
1163                                 FT_UINT8, BASE_DEC, VALS(type2vals), 0x0, "", HFILL }},
1164       { &hf_pim_code,
1165         { "Code",               "pim.code",
1166                                 FT_UINT8, BASE_DEC, VALS(type1vals), 0x0, "", HFILL }},
1167       { &hf_pim_cksum,
1168         { "Checksum",           "pim.cksum",
1169                                 FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }},
1170     };
1171     static gint *ett[] = {
1172         &ett_pim,
1173     };
1174
1175     proto_pim = proto_register_protocol("Protocol Independent Multicast",
1176         "PIM", "pim");
1177     proto_register_field_array(proto_pim, hf, array_length(hf));
1178     proto_register_subtree_array(ett, array_length(ett));
1179 }
1180
1181 void
1182 proto_reg_handoff_pim(void)
1183 {
1184     dissector_handle_t pim_handle;
1185
1186     pim_handle = create_dissector_handle(dissect_pim, proto_pim);
1187     dissector_add("ip.proto", IP_PROTO_PIM, pim_handle);
1188
1189     /*
1190      * Get handles for the IPv4 and IPv6 dissectors.
1191      */
1192     ip_handle = find_dissector("ip");
1193     ipv6_handle = find_dissector("ipv6");
1194 }