make pim.{version,type,cksum} available for filters.
[metze/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.5 1999/10/15 13:14:43 itojun Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  * 
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 #include "config.h"
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36
37 #ifdef NEED_SNPRINTF_H
38 # ifdef HAVE_STDARG_H
39 #  include <stdarg.h>
40 # else
41 #  include <varargs.h>
42 # endif
43 # include "snprintf.h"
44 #endif
45
46 #include <glib.h>
47 #include "packet.h"
48 #include "packet-ipv6.h"
49
50 #ifndef offsetof
51 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
52 #endif
53
54 struct pim {
55         guint8  pim_typever;
56 #define PIM_TYPE(x)     ((x) & 0x0f)
57 #define PIM_VER(x)      (((x) & 0xf0) >> 4)
58         guint8  pim_rsv;        /* Reserved */
59         guint16 pim_cksum;      /* IP style check sum */
60 };
61
62 enum pimv2_addrtype {
63         pimv2_unicast, pimv2_group, pimv2_source
64 };
65
66 static int proto_pim = -1;
67 static int hf_pim_version = -1;
68 static int hf_pim_type = -1;
69 static int hf_pim_cksum = -1;
70
71 static const char *
72 dissect_pim_addr(const u_char *bp, const u_char *ep, enum pimv2_addrtype at,
73         int *advance) {
74     static char buf[512];
75     int af;
76     int len = 0;
77
78     if (bp >= ep)
79         return NULL;
80
81     switch (bp[0]) {
82     case 1:
83         af = 4;
84         break;
85     case 2:
86         af = 6;
87         break;
88     default:
89         return NULL;
90     }
91
92     if (bp[1] != 0)
93         return NULL;
94
95     switch (at) {
96     case pimv2_unicast:
97         if (af == 4) {
98             len = 4;
99             if (bp + 2 + len > ep)
100                 return NULL;
101             (void)snprintf(buf, sizeof(buf), "%s", ip_to_str(bp + 2));
102         }
103         else if (af == 6) {
104             len = 16;
105             if (bp + 2 + len > ep)
106                 return NULL;
107             (void)snprintf(buf, sizeof(buf), "%s",
108                 ip6_to_str((struct e_in6_addr *)(bp + 2)));
109         }
110         if (advance)
111             *advance = 2 + len;
112         break;
113
114     case pimv2_group:
115         if (af == 4) {
116             len = 4;
117             if (bp + 4 + len > ep)
118                 return NULL;
119             (void)snprintf(buf, sizeof(buf), "%s/%u", ip_to_str(bp + 4), bp[3]);
120         }
121         else if (af == 6) {
122             len = 16;
123             if (bp + 4 + len > ep)
124                 return NULL;
125             (void)snprintf(buf, sizeof(buf), "%s/%u",
126                 ip6_to_str((struct e_in6_addr *)(bp + 4)), bp[3]);
127         }
128         if (advance)
129             *advance = 4 + len;
130         break;
131     case pimv2_source:
132         if (af == 4) {
133             len = 4;
134             if (bp + 4 + len > ep)
135                 return NULL;
136             (void)snprintf(buf, sizeof(buf), "%s/%u", ip_to_str(bp + 4), bp[3]);
137         }
138         else if (af == 6) {
139             len = 16;
140             if (bp + 4 + len > ep)
141                 return NULL;
142             (void)snprintf(buf, sizeof(buf), "%s/%u",
143                 ip6_to_str((struct e_in6_addr *)(bp + 4)), bp[3]);
144         }
145         if (bp[2]) {
146             (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
147                 " (%s%s%s)",
148                 bp[2] & 0x04 ? "S" : "",
149                 bp[2] & 0x02 ? "W" : "",
150                 bp[2] & 0x01 ? "R" : "");
151         }
152         if (advance)
153             *advance = 4 + len;
154         break;
155     default:
156         return NULL;
157     }
158
159     return buf;
160 }
161
162 void 
163 dissect_pim(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
164     struct pim pim;
165     static const value_string type1vals[] = {
166         { 0, "Query" },
167         { 1, "Register" },
168         { 2, "Register-stop" },
169         { 3, "Join/Prune" },
170         { 4, "RP-Reachable" },
171         { 5, "Assert" },
172         { 6, "Graft" },
173         { 7, "Graft-Ack" },
174         { 8, "Mode" },
175         { 0, NULL },
176     };
177     static const value_string type2vals[] = {
178         { 0, "Hello" },
179         { 1, "Register" },
180         { 2, "Register-stop" },
181         { 3, "Join/Prune" },
182         { 4, "Bootstrap" },
183         { 5, "Assert" },
184         { 6, "Graft" },
185         { 7, "Graft-Ack" },
186         { 8, "Candidate-RP-Advertisement" },
187         { 0, NULL },
188     };
189     char *typestr;
190     proto_tree *pim_tree = NULL;
191         proto_item *ti; 
192     proto_tree *pimopt_tree = NULL;
193         proto_item *tiopt; 
194
195     /* avoid alignment problem */
196     memcpy(&pim, &pd[offset], sizeof(pim));
197
198     switch (PIM_VER(pim.pim_typever)) {
199     case 1:
200         typestr = val_to_str(PIM_TYPE(pim.pim_typever), type1vals, "Unknown");
201         break;
202     case 2:
203         typestr = val_to_str(PIM_TYPE(pim.pim_typever), type2vals, "Unknown");
204         break;
205     default:
206         typestr = "Unknown";
207         break;
208     }
209
210     if (check_col(fd, COL_PROTOCOL)) {
211         col_add_fstr(fd, COL_PROTOCOL, "PIM version %d",
212             PIM_VER(pim.pim_typever));
213     }
214     if (check_col(fd, COL_INFO))
215         col_add_fstr(fd, COL_INFO, "%s", typestr); 
216
217     if (tree) {
218         ti = proto_tree_add_item(tree, proto_pim, offset, END_OF_FRAME, NULL);
219         pim_tree = proto_item_add_subtree(ti, ETT_PIM);
220
221         proto_tree_add_item(pim_tree, hf_pim_version, offset, 1,
222             PIM_VER(pim.pim_typever)); 
223         proto_tree_add_item_format(pim_tree, hf_pim_type, offset, 1,
224             PIM_TYPE(pim.pim_typever),
225             "Type: %s (%u)", typestr, PIM_TYPE(pim.pim_typever)); 
226
227         proto_tree_add_item(pim_tree, hf_pim_cksum,
228             offset + offsetof(struct pim, pim_cksum), sizeof(pim.pim_cksum),
229             ntohs(pim.pim_cksum));
230
231         if (sizeof(struct pim) < END_OF_FRAME) {
232             tiopt = proto_tree_add_text(pim_tree,
233                 offset + sizeof(struct pim), END_OF_FRAME,
234                 "PIM parameters");
235             pimopt_tree = proto_item_add_subtree(tiopt, ETT_PIM);
236         } else
237             goto done;
238
239         if (PIM_VER(pim.pim_typever) != 2)
240             goto done;
241
242         /* version 2 decoder */
243         switch (PIM_TYPE(pim.pim_typever)) {
244         case 0: /*hello*/
245           {
246             guint16 *w;
247             w = (guint16 *)&pd[offset + sizeof(struct pim)];
248             while ((guint8 *)w < &pd[offset + END_OF_FRAME]) {
249                 if (ntohs(w[0]) == 1 && ntohs(w[1]) == 2
250                  && (guint8 *)&w[3] <= &pd[offset + END_OF_FRAME]) {
251                     proto_tree_add_text(pimopt_tree, (guint8 *)w - pd, 6,
252                         "Holdtime: %u%s", ntohs(w[2]),
253                         ntohs(w[2]) == 0xffff ? " (infty)" : "");
254                     w += 3;
255                 } else
256                     break;
257             }
258             break;
259           }
260
261         case 1: /* register */
262           {
263             const guint8 *ip;
264             proto_tree *flag_tree = NULL;
265                 proto_item *tiflag; 
266             int flagoff;
267
268             flagoff = offset + sizeof(struct pim);
269             tiflag = proto_tree_add_text(pimopt_tree, flagoff, 4,
270                 "Flags: 0x%08x", ntohl(*(guint32 *)&pd[flagoff]));
271             flag_tree = proto_item_add_subtree(tiflag, ETT_PIM);
272             proto_tree_add_text(flag_tree, flagoff, 1, "%s",
273                 decode_boolean_bitfield(pd[flagoff], 0x80000000, 32,
274                     "Border", "Not border"));
275             proto_tree_add_text(flag_tree, flagoff, 1, "%s",
276                 decode_boolean_bitfield(pd[flagoff], 0x40000000, 32,
277                     "Null-Register", "Not Null-Register"));
278
279             ip = &pd[flagoff + sizeof(guint32)];
280             switch((*ip & 0xf0) >> 4) {
281             case 4:     /* IPv4 */
282 #if 0
283                     dissect_ip(pd, ip - pd, fd, pimopt_tree);
284 #else
285                     dissect_ip(pd, ip - pd, fd, tree);
286 #endif
287                     break;
288             case 6:     /* IPv6 */
289 #if 0
290                     dissect_ipv6(pd, ip - pd, fd, pimopt_tree);
291 #else
292                     dissect_ipv6(pd, ip - pd, fd, tree);
293 #endif
294                     break;
295             default:
296                     proto_tree_add_text(pimopt_tree,
297                         ip - pd, END_OF_FRAME,
298                         "Unknown IP version %d", (*ip & 0xf0) >> 4);
299                     break;
300             }
301             break;
302           }
303
304         case 2: /* register-stop */
305           {
306             int advance;
307             const guint8 *ep;
308             const char *s;
309
310             ep = &pd[offset + END_OF_FRAME];
311             offset += sizeof(struct pim);
312             s = dissect_pim_addr(&pd[offset], ep, pimv2_group, &advance);
313             if (s == NULL)
314                 break;
315             proto_tree_add_text(pimopt_tree, offset, advance, "Group: %s", s);
316             offset += advance;
317             s = dissect_pim_addr(&pd[offset], ep, pimv2_unicast, &advance);
318             if (s == NULL)
319                 break;
320             proto_tree_add_text(pimopt_tree, offset, advance, "Source: %s", s);
321             break;
322           }
323
324         case 3: /* join/prune */
325         case 6: /* graft */
326         case 7: /* graft-ack */
327           {
328             int advance;
329             const guint8 *ep;
330             int off;
331             const char *s;
332             int ngroup, i, njoin, nprune, j;
333             proto_tree *grouptree = NULL;
334                 proto_item *tigroup; 
335             proto_tree *subtree = NULL;
336                 proto_item *tisub; 
337
338             ep = &pd[offset + END_OF_FRAME];
339             offset += sizeof(struct pim);
340             if (PIM_TYPE(pim.pim_typever) != 7) {
341                 /* not graft-ack */
342                 s = dissect_pim_addr(&pd[offset], ep, pimv2_unicast, &advance);
343                 if (s == NULL)
344                     break;
345                 proto_tree_add_text(pimopt_tree, offset, advance,
346                     "Upstream-neighbor: %s", s);
347                 offset += advance;
348             }
349
350             if (&pd[offset + 2] > ep)
351                 break;
352             ngroup = pd[offset + 1];
353             proto_tree_add_text(pimopt_tree, offset + 1, 1,
354                 "Groups: %u", pd[offset + 1]);
355             offset += 2;
356
357             if (&pd[offset + 2] > ep)
358                 break;
359             if (PIM_TYPE(pim.pim_typever) != 7) {
360                 /* not graft-ack */
361                 proto_tree_add_text(pimopt_tree, offset, 2,
362                     "Holdtime: %u%s", ntohs(*(guint16 *)&pd[offset]),
363                     ntohs(*(guint16 *)&pd[offset]) == 0xffff ? " (infty)" : "");
364             }
365             offset += 2;
366
367             for (i = 0; i < ngroup; i++) {
368                 if (&pd[offset] >= ep)
369                     goto breakbreak3;
370
371                 s = dissect_pim_addr(&pd[offset], ep, pimv2_group, &advance);
372                 if (s == NULL)
373                     goto breakbreak3;
374                 tigroup = proto_tree_add_text(pimopt_tree, offset, advance,
375                     "Group %d: %s", i, s);
376                 grouptree = proto_item_add_subtree(tigroup, ETT_PIM);
377                 offset += advance;
378
379                 if (&pd[offset + 4] > ep)
380                     goto breakbreak3;
381                 njoin = ntohs(*(guint16 *)&pd[offset]);
382                 nprune = ntohs(*(guint16 *)&pd[offset + 2]);
383
384                 tisub = proto_tree_add_text(grouptree, offset, 2,
385                     "Join: %d", njoin);
386                 subtree = proto_item_add_subtree(tisub, ETT_PIM);
387                 off = offset + 4;
388                 for (j = 0; j < nprune; j++) {
389                     s = dissect_pim_addr(&pd[off], ep, pimv2_source,
390                         &advance);
391                     if (s == NULL)
392                         goto breakbreak3;
393                     proto_tree_add_text(subtree, off, advance,
394                         "IP address: %s", s);
395                     off += advance;
396                 }
397
398                 tisub = proto_tree_add_text(grouptree, offset + 2, 2,
399                     "Prune: %d", nprune);
400                 subtree = proto_item_add_subtree(tisub, ETT_PIM);
401                 for (j = 0; j < nprune; j++) {
402                     s = dissect_pim_addr(&pd[off], ep, pimv2_source,
403                         &advance);
404                     if (s == NULL)
405                         goto breakbreak3;
406                     proto_tree_add_text(subtree, off, advance,
407                         "IP address: %s", s);
408                     off += advance;
409                 }
410             }
411     breakbreak3:
412             break;
413           }
414
415         case 4: /* bootstrap */
416           {
417             const char *s;
418             int advance;
419             int i, j;
420             int frpcnt;
421             proto_tree *grouptree = NULL;
422                 proto_item *tigroup; 
423
424             offset += sizeof(struct pim);
425
426             if (END_OF_FRAME < 2)
427                 break;
428             proto_tree_add_text(pimopt_tree, offset, 2,
429                 "Fragment tag: 0x%04x", ntohs(*(guint16 *)&pd[offset]));
430             offset += 2;
431
432             if (END_OF_FRAME < 2)
433                 break;
434             proto_tree_add_text(pimopt_tree, offset, 1,
435                 "Hash mask len: %u", pd[offset]);
436             proto_tree_add_text(pimopt_tree, offset + 1, 1,
437                 "BSR priority: %u", pd[offset + 1]);
438             offset += 2;
439
440             s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
441                 pimv2_unicast, &advance);
442             if (s == NULL)
443                 break;
444             proto_tree_add_text(pimopt_tree, offset, advance, "BSR: %s", s);
445             offset += advance;
446
447             for (i = 0; END_OF_FRAME > 0; i++) {
448                 s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
449                     pimv2_group, &advance);
450                 if (s == NULL)
451                     goto breakbreak4;
452                 tigroup = proto_tree_add_text(pimopt_tree, offset, advance,
453                     "Group %d: %s", i, s);
454                 grouptree = proto_item_add_subtree(tigroup, ETT_PIM);
455                 offset += advance;
456
457                 if (END_OF_FRAME < 2)
458                     goto breakbreak4;
459                 proto_tree_add_text(grouptree, offset, 1,
460                     "RP count: %u", pd[offset]);
461                 proto_tree_add_text(grouptree, offset + 1, 1,
462                     "FRP count: %u", pd[offset + 1]);
463                 frpcnt = pd[offset + 1];
464                 offset += 4;
465
466                 for (j = 0; j < frpcnt && END_OF_FRAME > 0; j++) {
467                     s = dissect_pim_addr(&pd[offset],
468                         &pd[offset + END_OF_FRAME], pimv2_unicast, &advance);
469                     if (s == NULL)
470                         goto breakbreak4;
471                     proto_tree_add_text(grouptree, offset, advance,
472                         "RP %d: %s", j, s);
473                     offset += advance;
474
475                     if (END_OF_FRAME < 4)
476                         goto breakbreak4;
477                     proto_tree_add_text(grouptree, offset, 2,
478                         "Holdtime: %u%s", ntohs(*(guint16 *)&pd[offset]),
479                         ntohs(*(guint16 *)&pd[offset]) == 0xffff ? " (infty)" : "");
480                     proto_tree_add_text(grouptree, offset + 3, 1,
481                         "Priority: %u", pd[offset + 3]);
482
483                     offset += 4;
484                 }
485             }
486
487     breakbreak4:
488             break;
489           }
490
491         case 5: /* assert */
492           {
493             const char *s;
494             int advance;
495
496             offset += sizeof(struct pim);
497
498             s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
499                 pimv2_group, &advance);
500             if (s == NULL)
501                 break;
502             proto_tree_add_text(pimopt_tree, offset, advance, "Group: %s", s);
503             offset += advance;
504
505             s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
506                 pimv2_unicast, &advance);
507             if (s == NULL)
508                 break;
509             proto_tree_add_text(pimopt_tree, offset, advance, "Source: %s", s);
510             offset += advance;
511
512             if (END_OF_FRAME < 4)
513                 break;
514             proto_tree_add_text(pimopt_tree, offset, 1, "%s",
515                 decode_boolean_bitfield(pd[offset], 0x80, 8,
516                     "RP Tree", "Not RP Tree"));
517             proto_tree_add_text(pimopt_tree, offset, 4, "Preference: %u",
518                 ntohl(*(guint32 *)&pd[offset] & 0x7fffffff));
519             offset += 4;
520
521             if (END_OF_FRAME < 4)
522                 break;
523             proto_tree_add_text(pimopt_tree, offset, 4, "Metric: %u",
524                 ntohl(*(guint32 *)&pd[offset]));
525
526             break;
527           }
528
529         case 8: /* Candidate-RP-Advertisement */
530           {
531             const char *s;
532             int advance;
533             int pfxcnt;
534             int i;
535
536             offset += sizeof(struct pim);
537             if (END_OF_FRAME < 4)
538                 break;
539             pfxcnt = pd[offset];
540             proto_tree_add_text(pimopt_tree, offset, 1,
541                 "Prefix-count: %u", pd[offset]);
542             proto_tree_add_text(pimopt_tree, offset + 1, 1,
543                 "Priority: %u", pd[offset + 1]);
544             proto_tree_add_text(pimopt_tree, offset + 2, 2,
545                 "Holdtime: %u%s", ntohs(*(guint16 *)&pd[offset + 2]),
546                 ntohs(*(guint16 *)&pd[offset + 2]) == 0xffff ? " (infty)" : "");
547             offset += 4;
548
549             if (END_OF_FRAME <= 0)
550                 break;
551             s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
552                 pimv2_unicast, &advance);
553             if (s == NULL)
554                 break;
555             proto_tree_add_text(pimopt_tree, offset, advance, "RP: %s", s);
556             offset += advance;
557
558             for (i = 0; i < pfxcnt && END_OF_FRAME > 0; i++) {
559                 s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
560                     pimv2_group, &advance);
561                 if (s == NULL)
562                     goto breakbreak8;
563                 proto_tree_add_text(pimopt_tree, offset, advance,
564                     "Group %d: %s", i, s);
565                 offset += advance;
566             }
567     breakbreak8:
568             break;
569           }
570
571         default:
572             break;
573         }
574     }
575 done:;
576 }
577
578 void
579 proto_register_pim(void)
580 {
581     static hf_register_info hf[] = {
582       { &hf_pim_version,
583         { "Version",            "pim.version",
584                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
585       { &hf_pim_type,
586         { "Type",                       "pim.type",
587                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
588       { &hf_pim_cksum,
589         { "Checksum",           "pim.cksum",
590                                 FT_UINT16, BASE_HEX, NULL, 0x0, "" }},
591     };
592
593     proto_pim = proto_register_protocol("Protocol Independent Multicast",
594         "pim");
595     proto_register_field_array(proto_pim, hf, array_length(hf));
596 }