Import of tcpdump 3.8.3
[dragonfly.git] / contrib / tcpdump-3.8.3 / print-pim.c
1 /*
2  * Copyright (c) 1995, 1996
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21
22 #ifndef lint
23 static const char rcsid[] _U_ =
24     "@(#) $Header: /tcpdump/master/tcpdump/print-pim.c,v 1.37.2.4 2004/03/24 02:52:37 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 /*
34  * XXX: We consider a case where IPv6 is not ready yet for portability,
35  * but PIM dependent defintions should be independent of IPv6...
36  */
37
38 struct pim {
39         u_int8_t pim_typever;
40                         /* upper 4bit: PIM version number; 2 for PIMv2 */
41                         /* lower 4bit: the PIM message type, currently they are:
42                          * Hello, Register, Register-Stop, Join/Prune,
43                          * Bootstrap, Assert, Graft (PIM-DM only),
44                          * Graft-Ack (PIM-DM only), C-RP-Adv
45                          */
46 #define PIM_VER(x)      (((x) & 0xf0) >> 4)
47 #define PIM_TYPE(x)     ((x) & 0x0f)
48         u_char  pim_rsv;        /* Reserved */
49         u_short pim_cksum;      /* IP style check sum */
50 };
51
52
53 #include <stdio.h>
54 #include <stdlib.h>
55
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "extract.h"
59
60 #include "ip.h"
61
62 static void pimv2_print(register const u_char *bp, register u_int len);
63
64 static void
65 pimv1_join_prune_print(register const u_char *bp, register u_int len)
66 {
67         int maddrlen, addrlen, ngroups, njoin, nprune;
68         int njp;
69
70         /* If it's a single group and a single source, use 1-line output. */
71         if (TTEST2(bp[0], 30) && bp[11] == 1 &&
72             ((njoin = EXTRACT_16BITS(&bp[20])) + EXTRACT_16BITS(&bp[22])) == 1) {
73                 int hold;
74
75                 (void)printf(" RPF %s ", ipaddr_string(bp));
76                 hold = EXTRACT_16BITS(&bp[6]);
77                 if (hold != 180) {
78                         (void)printf("Hold ");
79                         relts_print(hold);
80                 }
81                 (void)printf("%s (%s/%d, %s", njoin ? "Join" : "Prune",
82                 ipaddr_string(&bp[26]), bp[25] & 0x3f,
83                 ipaddr_string(&bp[12]));
84                 if (EXTRACT_32BITS(&bp[16]) != 0xffffffff)
85                         (void)printf("/%s", ipaddr_string(&bp[16]));
86                 (void)printf(") %s%s %s",
87                     (bp[24] & 0x01) ? "Sparse" : "Dense",
88                     (bp[25] & 0x80) ? " WC" : "",
89                     (bp[25] & 0x40) ? "RP" : "SPT");
90                 return;
91         }
92
93         TCHECK2(bp[0], 4);
94         if (vflag > 1)
95                 (void)printf("\n");
96         (void)printf(" Upstream Nbr: %s", ipaddr_string(bp));
97         TCHECK2(bp[6], 2);
98         if (vflag > 1)
99                 (void)printf("\n");
100         (void)printf(" Hold time: ");
101         relts_print(EXTRACT_16BITS(&bp[6]));
102         if (vflag < 2)
103                 return;
104         bp += 8;
105         len -= 8;
106
107         TCHECK2(bp[0], 4);
108         maddrlen = bp[1];
109         addrlen = bp[2];
110         ngroups = bp[3];
111         bp += 4;
112         len -= 4;
113         while (ngroups--) {
114                 TCHECK2(bp[0], 4);
115                 (void)printf("\n\tGroup: %s", ipaddr_string(bp));
116                 TCHECK2(bp[4], 4);
117                 if (EXTRACT_32BITS(&bp[4]) != 0xffffffff)
118                         (void)printf("/%s", ipaddr_string(&bp[4]));
119                 TCHECK2(bp[8], 4);
120                 njoin = EXTRACT_16BITS(&bp[8]);
121                 nprune = EXTRACT_16BITS(&bp[10]);
122                 (void)printf(" joined: %d pruned: %d", njoin, nprune);
123                 bp += 12;
124                 len -= 12;
125                 for (njp = 0; njp < (njoin + nprune); njp++) {
126                         const char *type;
127
128                         if (njp < njoin)
129                                 type = "Join ";
130                         else
131                                 type = "Prune";
132                         TCHECK2(bp[0], 6);
133                         (void)printf("\n\t%s %s%s%s%s/%d", type,
134                             (bp[0] & 0x01) ? "Sparse " : "Dense ",
135                             (bp[1] & 0x80) ? "WC " : "",
136                             (bp[1] & 0x40) ? "RP " : "SPT ",
137                         ipaddr_string(&bp[2]), bp[1] & 0x3f);
138                         bp += 6;
139                         len -= 6;
140                 }
141         }
142         return;
143 trunc:
144         (void)printf("[|pim]");
145         return;
146 }
147
148 void
149 pimv1_print(register const u_char *bp, register u_int len)
150 {
151         register const u_char *ep;
152         register u_char type;
153
154         ep = (const u_char *)snapend;
155         if (bp >= ep)
156                 return;
157
158         TCHECK(bp[1]);
159         type = bp[1];
160
161         switch (type) {
162         case 0:
163                 (void)printf(" Query");
164                 if (TTEST(bp[8])) {
165                         switch (bp[8] >> 4) {
166                         case 0:
167                                 (void)printf(" Dense-mode");
168                                 break;
169                         case 1:
170                                 (void)printf(" Sparse-mode");
171                                 break;
172                         case 2:
173                                 (void)printf(" Sparse-Dense-mode");
174                                 break;
175                         default:
176                                 (void)printf(" mode-%d", bp[8] >> 4);
177                                 break;
178                         }
179                 }
180                 if (vflag) {
181                         TCHECK2(bp[10],2);
182                         (void)printf(" (Hold-time ");
183                         relts_print(EXTRACT_16BITS(&bp[10]));
184                         (void)printf(")");
185                 }
186                 break;
187
188         case 1:
189                 (void)printf(" Register");
190                 TCHECK2(bp[8], 20);                     /* ip header */
191                 (void)printf(" for %s > %s", ipaddr_string(&bp[20]),
192                     ipaddr_string(&bp[24]));
193                 break;
194         case 2:
195                 (void)printf(" Register-Stop");
196                 TCHECK2(bp[12], 4);
197                 (void)printf(" for %s > %s", ipaddr_string(&bp[8]),
198                     ipaddr_string(&bp[12]));
199                 break;
200         case 3:
201                 (void)printf(" Join/Prune");
202                 if (vflag)
203                         pimv1_join_prune_print(&bp[8], len - 8);
204                 break;
205         case 4:
206                 (void)printf(" RP-reachable");
207                 if (vflag) {
208                         TCHECK2(bp[22], 2);
209                         (void)printf(" group %s",
210                         ipaddr_string(&bp[8]));
211                         if (EXTRACT_32BITS(&bp[12]) != 0xffffffff)
212                                 (void)printf("/%s", ipaddr_string(&bp[12]));
213                         (void)printf(" RP %s hold ", ipaddr_string(&bp[16]));
214                         relts_print(EXTRACT_16BITS(&bp[22]));
215                 }
216                 break;
217         case 5:
218                 (void)printf(" Assert");
219                 TCHECK2(bp[16], 4);
220                 (void)printf(" for %s > %s", ipaddr_string(&bp[16]),
221                     ipaddr_string(&bp[8]));
222                 if (EXTRACT_32BITS(&bp[12]) != 0xffffffff)
223                         (void)printf("/%s", ipaddr_string(&bp[12]));
224                 TCHECK2(bp[24], 4);
225                 (void)printf(" %s pref %d metric %d",
226                     (bp[20] & 0x80) ? "RP-tree" : "SPT",
227                 EXTRACT_32BITS(&bp[20]) & 0x7fffffff,
228                 EXTRACT_32BITS(&bp[24]));
229                 break;
230         case 6:
231                 (void)printf(" Graft");
232                 if (vflag)
233                         pimv1_join_prune_print(&bp[8], len - 8);
234                 break;
235         case 7:
236                 (void)printf(" Graft-ACK");
237                 if (vflag)
238                         pimv1_join_prune_print(&bp[8], len - 8);
239                 break;
240         case 8:
241                 (void)printf(" Mode");
242                 break;
243         default:
244                 (void)printf(" [type %d]", type);
245                 break;
246         }
247         if ((bp[4] >> 4) != 1)
248                 (void)printf(" [v%d]", bp[4] >> 4);
249         return;
250
251 trunc:
252         (void)printf("[|pim]");
253         return;
254 }
255
256 /*
257  * auto-RP is a cisco protocol, documented at
258  * ftp://ftpeng.cisco.com/ipmulticast/specs/pim-autorp-spec01.txt
259  *
260  * This implements version 1+, dated Sept 9, 1998.
261  */
262 void
263 cisco_autorp_print(register const u_char *bp, register u_int len)
264 {
265         int type;
266         int numrps;
267         int hold;
268
269         TCHECK(bp[0]);
270         (void)printf(" auto-rp ");
271         type = bp[0];
272         switch (type) {
273         case 0x11:
274                 (void)printf("candidate-advert");
275                 break;
276         case 0x12:
277                 (void)printf("mapping");
278                 break;
279         default:
280                 (void)printf("type-0x%02x", type);
281                 break;
282         }
283
284         TCHECK(bp[1]);
285         numrps = bp[1];
286
287         TCHECK2(bp[2], 2);
288         (void)printf(" Hold ");
289         hold = EXTRACT_16BITS(&bp[2]);
290         if (hold)
291                 relts_print(EXTRACT_16BITS(&bp[2]));
292         else
293                 printf("FOREVER");
294
295         /* Next 4 bytes are reserved. */
296
297         bp += 8; len -= 8;
298
299         /*XXX skip unless -v? */
300
301         /*
302          * Rest of packet:
303          * numrps entries of the form:
304          * 32 bits: RP
305          * 6 bits: reserved
306          * 2 bits: PIM version supported, bit 0 is "supports v1", 1 is "v2".
307          * 8 bits: # of entries for this RP
308          * each entry: 7 bits: reserved, 1 bit: negative,
309          *             8 bits: mask 32 bits: source
310          * lather, rinse, repeat.
311          */
312         while (numrps--) {
313                 int nentries;
314                 char s;
315
316                 TCHECK2(bp[0], 4);
317                 (void)printf(" RP %s", ipaddr_string(bp));
318                 TCHECK(bp[4]);
319                 switch (bp[4] & 0x3) {
320                 case 0: printf(" PIMv?");
321                         break;
322                 case 1: printf(" PIMv1");
323                         break;
324                 case 2: printf(" PIMv2");
325                         break;
326                 case 3: printf(" PIMv1+2");
327                         break;
328                 }
329                 if (bp[4] & 0xfc)
330                         (void)printf(" [rsvd=0x%02x]", bp[4] & 0xfc);
331                 TCHECK(bp[5]);
332                 nentries = bp[5];
333                 bp += 6; len -= 6;
334                 s = ' ';
335                 for (; nentries; nentries--) {
336                         TCHECK2(bp[0], 6);
337                         (void)printf("%c%s%s/%d", s, bp[0] & 1 ? "!" : "",
338                             ipaddr_string(&bp[2]), bp[1]);
339                         if (bp[0] & 0xfe)
340                                 (void)printf("[rsvd=0x%02x]", bp[0] & 0xfe);
341                         s = ',';
342                         bp += 6; len -= 6;
343                 }
344         }
345         return;
346
347 trunc:
348         (void)printf("[|autorp]");
349         return;
350 }
351
352 void
353 pim_print(register const u_char *bp, register u_int len)
354 {
355         register const u_char *ep;
356         register struct pim *pim = (struct pim *)bp;
357
358         ep = (const u_char *)snapend;
359         if (bp >= ep)
360                 return;
361 #ifdef notyet                   /* currently we see only version and type */
362         TCHECK(pim->pim_rsv);
363 #endif
364
365         switch (PIM_VER(pim->pim_typever)) {
366         case 2:         /* avoid hardcoding? */
367                 (void)printf("pim v2");
368                 pimv2_print(bp, len);
369                 break;
370         default:
371                 (void)printf("pim v%d", PIM_VER(pim->pim_typever));
372                 break;
373         }
374         return;
375 }
376
377 /*
378  * PIMv2 uses encoded address representations.
379  *
380  * The last PIM-SM I-D before RFC2117 was published specified the
381  * following representation for unicast addresses.  However, RFC2117
382  * specified no encoding for unicast addresses with the unicast
383  * address length specified in the header.  Therefore, we have to
384  * guess which encoding is being used (Cisco's PIMv2 implementation
385  * uses the non-RFC encoding).  RFC2117 turns a previously "Reserved"
386  * field into a 'unicast-address-length-in-bytes' field.  We guess
387  * that it's the draft encoding if this reserved field is zero.
388  *
389  * RFC2362 goes back to the encoded format, and calls the addr length
390  * field "reserved" again.
391  *
392  * The first byte is the address family, from:
393  *
394  *    0    Reserved
395  *    1    IP (IP version 4)
396  *    2    IP6 (IP version 6)
397  *    3    NSAP
398  *    4    HDLC (8-bit multidrop)
399  *    5    BBN 1822
400  *    6    802 (includes all 802 media plus Ethernet "canonical format")
401  *    7    E.163
402  *    8    E.164 (SMDS, Frame Relay, ATM)
403  *    9    F.69 (Telex)
404  *   10    X.121 (X.25, Frame Relay)
405  *   11    IPX
406  *   12    Appletalk
407  *   13    Decnet IV
408  *   14    Banyan Vines
409  *   15    E.164 with NSAP format subaddress
410  *
411  * In addition, the second byte is an "Encoding".  0 is the default
412  * encoding for the address family, and no other encodings are currently
413  * specified.
414  *
415  */
416
417 static int pimv2_addr_len;
418
419 enum pimv2_addrtype {
420         pimv2_unicast, pimv2_group, pimv2_source
421 };
422
423 /*  0                   1                   2                   3
424  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
425  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
426  * | Addr Family   | Encoding Type |     Unicast Address           |
427  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+++++++
428  *  0                   1                   2                   3
429  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
430  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
431  * | Addr Family   | Encoding Type |   Reserved    |  Mask Len     |
432  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433  * |                Group multicast Address                        |
434  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435  *  0                   1                   2                   3
436  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
437  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
438  * | Addr Family   | Encoding Type | Rsrvd   |S|W|R|  Mask Len     |
439  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
440  * |                        Source Address                         |
441  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
442  */
443 static int
444 pimv2_addr_print(const u_char *bp, enum pimv2_addrtype at, int silent)
445 {
446         int af;
447         const char *afstr;
448         int len, hdrlen;
449
450         TCHECK(bp[0]);
451
452         if (pimv2_addr_len == 0) {
453                 TCHECK(bp[1]);
454                 switch (bp[0]) {
455                 case 1:
456                         af = AF_INET;
457                         afstr = "IPv4";
458                         len = 4;
459                         break;
460 #ifdef INET6
461                 case 2:
462                         af = AF_INET6;
463                         afstr = "IPv6";
464                         len = 16;
465                         break;
466 #endif
467                 default:
468                         return -1;
469                 }
470                 if (bp[1] != 0)
471                         return -1;
472                 hdrlen = 2;
473         } else {
474                 switch (pimv2_addr_len) {
475                 case 4:
476                         af = AF_INET;
477                         afstr = "IPv4";
478                         break;
479 #ifdef INET6
480                 case 16:
481                         af = AF_INET6;
482                         afstr = "IPv6";
483                         break;
484 #endif
485                 default:
486                         return -1;
487                         break;
488                 }
489                 len = pimv2_addr_len;
490                 hdrlen = 0;
491         }
492
493         bp += hdrlen;
494         switch (at) {
495         case pimv2_unicast:
496                 TCHECK2(bp[0], len);
497                 if (af == AF_INET) {
498                         if (!silent)
499                                 (void)printf("%s", ipaddr_string(bp));
500                 }
501 #ifdef INET6
502                 else if (af == AF_INET6) {
503                         if (!silent)
504                                 (void)printf("%s", ip6addr_string(bp));
505                 }
506 #endif
507                 return hdrlen + len;
508         case pimv2_group:
509         case pimv2_source:
510                 TCHECK2(bp[0], len + 2);
511                 if (af == AF_INET) {
512                         if (!silent) {
513                                 (void)printf("%s", ipaddr_string(bp + 2));
514                                 if (bp[1] != 32)
515                                         (void)printf("/%u", bp[1]);
516                         }
517                 }
518 #ifdef INET6
519                 else if (af == AF_INET6) {
520                         if (!silent) {
521                                 (void)printf("%s", ip6addr_string(bp + 2));
522                                 if (bp[1] != 128)
523                                         (void)printf("/%u", bp[1]);
524                         }
525                 }
526 #endif
527                 if (bp[0] && !silent) {
528                         if (at == pimv2_group) {
529                                 (void)printf("(0x%02x)", bp[0]);
530                         } else {
531                                 (void)printf("(%s%s%s",
532                                         bp[0] & 0x04 ? "S" : "",
533                                         bp[0] & 0x02 ? "W" : "",
534                                         bp[0] & 0x01 ? "R" : "");
535                                 if (bp[0] & 0xf8) {
536                                         (void) printf("+0x%02x", bp[0] & 0xf8);
537                                 }
538                                 (void)printf(")");
539                         }
540                 }
541                 return hdrlen + 2 + len;
542         default:
543                 return -1;
544         }
545 trunc:
546         return -1;
547 }
548
549 static void
550 pimv2_print(register const u_char *bp, register u_int len)
551 {
552         register const u_char *ep;
553         register struct pim *pim = (struct pim *)bp;
554         int advance;
555
556         ep = (const u_char *)snapend;
557         if (bp >= ep)
558                 return;
559         if (ep > bp + len)
560                 ep = bp + len;
561         TCHECK(pim->pim_rsv);
562         pimv2_addr_len = pim->pim_rsv;
563         if (pimv2_addr_len != 0)
564                 (void)printf("[RFC2117-encoding] ");
565
566         switch (PIM_TYPE(pim->pim_typever)) {
567         case 0:
568             {
569                 u_int16_t otype, olen;
570                 (void)printf(" Hello");
571                 bp += 4;
572                 while (bp < ep) {
573                         TCHECK2(bp[0], 4);
574                         otype = EXTRACT_16BITS(&bp[0]);
575                         olen = EXTRACT_16BITS(&bp[2]);
576                         TCHECK2(bp[0], 4 + olen);
577                         switch (otype) {
578                         case 1:         /* Hold time */
579                                 (void)printf(" (Hold-time ");
580                                 relts_print(EXTRACT_16BITS(&bp[4]));
581                                 (void)printf(")");
582                                 break;
583
584                         case 2:         /* LAN Prune Delay */
585                                 (void)printf(" (LAN-Prune-Delay: ");
586                                 if (olen != 4) {
587                                         (void)printf("!olen=%d!)", olen);
588                                 } else {
589                                         char t_bit;
590                                         u_int16_t lan_delay, override_interval;
591                                         lan_delay = EXTRACT_16BITS(&bp[4]);
592                                         override_interval = EXTRACT_16BITS(&bp[6]);
593                                         t_bit = (lan_delay & 0x8000)? 1 : 0;
594                                         lan_delay &= ~0x8000;
595                                         (void)printf("T-bit=%d lan-delay=%dms override-interval=%dms)",
596                                         t_bit, lan_delay, override_interval);
597                                 }
598                                 break;
599
600                         case 18:        /* Old DR-Priority */
601                                 if (olen == 4)
602                                         (void)printf(" (OLD-DR-Priority: %d)",
603                                                         EXTRACT_32BITS(&bp[4]));
604                                 else
605                                         goto unknown;
606                                 break;
607
608
609                         case 19:        /* DR-Priority */
610                                 if (olen == 0) {
611                                         (void)printf(" (OLD-bidir-capable)");
612                                         break;
613                                 }
614                                 (void)printf(" (DR-Priority: ");
615                                 if (olen != 4) {
616                                         (void)printf("!olen=%d!)", olen);
617                                 } else {
618                                         (void)printf("%d)", EXTRACT_32BITS(&bp[4]));
619                                 }
620                                 break;
621
622                         case 20:
623                                 (void)printf(" (Genid: 0x%08x)", EXTRACT_32BITS(&bp[4]));
624                                 break;
625
626                         case 21:
627                                 (void)printf(" (State Refresh Capable; v%d", bp[4]);
628                                 if (bp[5] != 0) {
629                                         (void)printf(" interval ");
630                                         relts_print(bp[5]);
631                                 }
632                                 if (EXTRACT_16BITS(&bp[6]) != 0) {
633                                         (void)printf(" ?0x%04x?", EXTRACT_16BITS(&bp[6]));
634                                 }
635                                 (void)printf(")");
636                                 break;
637
638                         case 22:        /* Bidir-Capable */
639                                 (void)printf(" (bidir-capable)");
640                                 break;
641
642                         case 24:        /* Address List */
643                         case 65001:     /* Address List (old implementations) */
644                                 (void)printf(" (%saddr-list",
645                                              otype == 65001 ? "old" : "");
646                                 if (vflag > 1) {
647                                         const u_char *ptr = &bp[4];
648                                         while (ptr < &bp[4 + olen]) {
649                                                 int advance;
650
651                                                 printf(" ");
652                                                 advance = pimv2_addr_print(ptr, pimv2_unicast, 0);
653                                                 if (advance < 0) {
654                                                         printf("...");
655                                                         break;
656                                                 }
657                                                 ptr += advance;
658                                         }
659                                 }
660                                 (void)printf(")");
661                                 break;
662                         default:
663                         unknown:
664                                 if (vflag)
665                                         (void)printf(" [Hello option %d]", otype);
666                         }
667                         bp += 4 + olen;
668                 }
669                 break;
670             }
671
672         case 1:
673         {
674                 struct ip *ip;
675
676                 (void)printf(" Register");
677                 if (vflag && bp + 8 <= ep) {
678                         (void)printf(" %s%s", bp[4] & 0x80 ? "B" : "",
679                                 bp[4] & 0x40 ? "N" : "");
680                 }
681                 bp += 8; len -= 8;
682
683                 /* encapsulated multicast packet */
684                 if (bp >= ep)
685                         break;
686                 ip = (struct ip *)bp;
687                 switch (IP_V(ip)) {
688                 case 4: /* IPv4 */
689                         printf(" ");
690                         ip_print(bp, len);
691                         break;
692 #ifdef INET6
693                 case 6: /* IPv6 */
694                         printf(" ");
695                         ip6_print(bp, len);
696                         break;
697 #endif
698                 default:
699                         (void)printf(" IP ver %d", IP_V(ip));
700                         break;
701                 }
702                 break;
703         }
704
705         case 2:
706                 (void)printf(" Register-Stop");
707                 bp += 4; len -= 4;
708                 if (bp >= ep)
709                         break;
710                 (void)printf(" group=");
711                 if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
712                         (void)printf("...");
713                         break;
714                 }
715                 bp += advance; len -= advance;
716                 if (bp >= ep)
717                         break;
718                 (void)printf(" source=");
719                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
720                         (void)printf("...");
721                         break;
722                 }
723                 bp += advance; len -= advance;
724                 break;
725
726         case 3:
727         case 6:
728         case 7:
729             {
730                 u_int8_t ngroup;
731                 u_int16_t holdtime;
732                 u_int16_t njoin;
733                 u_int16_t nprune;
734                 int i, j;
735
736                 switch (PIM_TYPE(pim->pim_typever)) {
737                 case 3:
738                         (void)printf(" Join/Prune");
739                         break;
740                 case 6:
741                         (void)printf(" Graft");
742                         break;
743                 case 7:
744                         (void)printf(" Graft-ACK");
745                         break;
746                 }
747                 bp += 4; len -= 4;
748                 if (PIM_TYPE(pim->pim_typever) != 7) {  /*not for Graft-ACK*/
749                         if (bp >= ep)
750                                 break;
751                         (void)printf(" upstream-neighbor=");
752                         if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
753                                 (void)printf("...");
754                                 break;
755                         }
756                         bp += advance; len -= advance;
757                 }
758                 if (bp + 4 > ep)
759                         break;
760                 ngroup = bp[1];
761                 holdtime = EXTRACT_16BITS(&bp[2]);
762                 (void)printf(" groups=%u", ngroup);
763                 if (PIM_TYPE(pim->pim_typever) != 7) {  /*not for Graft-ACK*/
764                         (void)printf(" holdtime=");
765                         if (holdtime == 0xffff)
766                                 (void)printf("infty");
767                         else
768                                 relts_print(holdtime);
769                 }
770                 bp += 4; len -= 4;
771                 for (i = 0; i < ngroup; i++) {
772                         if (bp >= ep)
773                                 goto jp_done;
774                         (void)printf(" (group%d: ", i);
775                         if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
776                                 (void)printf("...)");
777                                 goto jp_done;
778                         }
779                         bp += advance; len -= advance;
780                         if (bp + 4 > ep) {
781                                 (void)printf("...)");
782                                 goto jp_done;
783                         }
784                         njoin = EXTRACT_16BITS(&bp[0]);
785                         nprune = EXTRACT_16BITS(&bp[2]);
786                         (void)printf(" join=%u", njoin);
787                         bp += 4; len -= 4;
788                         for (j = 0; j < njoin; j++) {
789                                 (void)printf(" ");
790                                 if ((advance = pimv2_addr_print(bp, pimv2_source, 0)) < 0) {
791                                         (void)printf("...)");
792                                         goto jp_done;
793                                 }
794                                 bp += advance; len -= advance;
795                         }
796                         (void)printf(" prune=%u", nprune);
797                         for (j = 0; j < nprune; j++) {
798                                 (void)printf(" ");
799                                 if ((advance = pimv2_addr_print(bp, pimv2_source, 0)) < 0) {
800                                         (void)printf("...)");
801                                         goto jp_done;
802                                 }
803                                 bp += advance; len -= advance;
804                         }
805                         (void)printf(")");
806                 }
807         jp_done:
808                 break;
809             }
810
811         case 4:
812         {
813                 int i, j, frpcnt;
814
815                 (void)printf(" Bootstrap");
816                 bp += 4;
817
818                 /* Fragment Tag, Hash Mask len, and BSR-priority */
819                 if (bp + sizeof(u_int16_t) >= ep) break;
820                 (void)printf(" tag=%x", EXTRACT_16BITS(bp));
821                 bp += sizeof(u_int16_t);
822                 if (bp >= ep) break;
823                 (void)printf(" hashmlen=%d", bp[0]);
824                 if (bp + 1 >= ep) break;
825                 (void)printf(" BSRprio=%d", bp[1]);
826                 bp += 2;
827
828                 /* Encoded-Unicast-BSR-Address */
829                 if (bp >= ep) break;
830                 (void)printf(" BSR=");
831                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
832                         (void)printf("...");
833                         break;
834                 }
835                 bp += advance;
836
837                 for (i = 0; bp < ep; i++) {
838                         /* Encoded-Group Address */
839                         (void)printf(" (group%d: ", i);
840                         if ((advance = pimv2_addr_print(bp, pimv2_group, 0))
841                             < 0) {
842                                 (void)printf("...)");
843                                 goto bs_done;
844                         }
845                         bp += advance;
846
847                         /* RP-Count, Frag RP-Cnt, and rsvd */
848                         if (bp >= ep) {
849                                 (void)printf("...)");
850                                 goto bs_done;
851                         }
852                         (void)printf(" RPcnt=%d", bp[0]);
853                         if (bp + 1 >= ep) {
854                                 (void)printf("...)");
855                                 goto bs_done;
856                         }
857                         (void)printf(" FRPcnt=%d", frpcnt = bp[1]);
858                         bp += 4;
859
860                         for (j = 0; j < frpcnt && bp < ep; j++) {
861                                 /* each RP info */
862                                 (void)printf(" RP%d=", j);
863                                 if ((advance = pimv2_addr_print(bp,
864                                                                 pimv2_unicast,
865                                                                 0)) < 0) {
866                                         (void)printf("...)");
867                                         goto bs_done;
868                                 }
869                                 bp += advance;
870
871                                 if (bp + 1 >= ep) {
872                                         (void)printf("...)");
873                                         goto bs_done;
874                                 }
875                                 (void)printf(",holdtime=");
876                                 relts_print(EXTRACT_16BITS(bp));
877                                 if (bp + 2 >= ep) {
878                                         (void)printf("...)");
879                                         goto bs_done;
880                                 }
881                                 (void)printf(",prio=%d", bp[2]);
882                                 bp += 4;
883                         }
884                         (void)printf(")");
885                 }
886            bs_done:
887                 break;
888         }
889         case 5:
890                 (void)printf(" Assert");
891                 bp += 4; len -= 4;
892                 if (bp >= ep)
893                         break;
894                 (void)printf(" group=");
895                 if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
896                         (void)printf("...");
897                         break;
898                 }
899                 bp += advance; len -= advance;
900                 if (bp >= ep)
901                         break;
902                 (void)printf(" src=");
903                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
904                         (void)printf("...");
905                         break;
906                 }
907                 bp += advance; len -= advance;
908                 if (bp + 8 > ep)
909                         break;
910                 if (bp[0] & 0x80)
911                         (void)printf(" RPT");
912                 (void)printf(" pref=%u", EXTRACT_32BITS(&bp[0]) & 0x7fffffff);
913                 (void)printf(" metric=%u", EXTRACT_32BITS(&bp[4]));
914                 break;
915
916         case 8:
917         {
918                 int i, pfxcnt;
919
920                 (void)printf(" Candidate-RP-Advertisement");
921                 bp += 4;
922
923                 /* Prefix-Cnt, Priority, and Holdtime */
924                 if (bp >= ep) break;
925                 (void)printf(" prefix-cnt=%d", bp[0]);
926                 pfxcnt = bp[0];
927                 if (bp + 1 >= ep) break;
928                 (void)printf(" prio=%d", bp[1]);
929                 if (bp + 3 >= ep) break;
930                 (void)printf(" holdtime=");
931                 relts_print(EXTRACT_16BITS(&bp[2]));
932                 bp += 4;
933
934                 /* Encoded-Unicast-RP-Address */
935                 if (bp >= ep) break;
936                 (void)printf(" RP=");
937                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
938                         (void)printf("...");
939                         break;
940                 }
941                 bp += advance;
942
943                 /* Encoded-Group Addresses */
944                 for (i = 0; i < pfxcnt && bp < ep; i++) {
945                         (void)printf(" Group%d=", i);
946                         if ((advance = pimv2_addr_print(bp, pimv2_group, 0))
947                             < 0) {
948                                 (void)printf("...");
949                                 break;
950                         }
951                         bp += advance;
952                 }
953                 break;
954         }
955
956         case 9:
957                 (void)printf(" Prune-Refresh");
958                 (void)printf(" src=");
959                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
960                         (void)printf("...");
961                         break;
962                 }
963                 bp += advance;
964                 (void)printf(" grp=");
965                 if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
966                         (void)printf("...");
967                         break;
968                 }
969                 bp += advance;
970                 (void)printf(" forwarder=");
971                 if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
972                         (void)printf("...");
973                         break;
974                 }
975                 bp += advance;
976                 TCHECK2(bp[0], 2);
977                 (void)printf(" TUNR ");
978                 relts_print(EXTRACT_16BITS(bp));
979                 break;
980
981
982          default:
983                 (void)printf(" [type %d]", PIM_TYPE(pim->pim_typever));
984                 break;
985         }
986
987         return;
988
989 trunc:
990         (void)printf("[|pim]");
991 }