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