summaryrefslogtreecommitdiffstats
path: root/contrib/tcpdump/print-pim.c
blob: 209c552f76b65e72635bb014f03784f5530d63b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/*
 * Copyright (c) 1995, 1996
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $FreeBSD$
 */

#ifndef lint
static const char rcsid[] =
    "@(#) $Header: /tcpdump/master/tcpdump/print-pim.c,v 1.29 2001/07/04 21:36:15 fenner Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>

#include <netinet/in.h>

/*
 * XXX: We consider a case where IPv6 is not ready yet for portability,
 * but PIM dependent defintions should be independent of IPv6...
 */

struct pim {
	u_int8_t pim_typever;
			/* upper 4bit: PIM version number; 2 for PIMv2 */
			/* lower 4bit: the PIM message type, currently they are:
			 * Hello, Register, Register-Stop, Join/Prune,
			 * Bootstrap, Assert, Graft (PIM-DM only),
			 * Graft-Ack (PIM-DM only), C-RP-Adv
			 */
#define PIM_VER(x)	(((x) & 0xf0) >> 4)
#define PIM_TYPE(x)	((x) & 0x0f)
	u_char  pim_rsv;	/* Reserved */
	u_short	pim_cksum;	/* IP style check sum */
};


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "interface.h"
#include "addrtoname.h"
#include "extract.h"

#include "ip.h"

static void pimv2_print(register const u_char *bp, register u_int len);

static void
pimv1_join_prune_print(register const u_char *bp, register u_int len)
{
	int maddrlen, addrlen, ngroups, njoin, nprune;
	int njp;

	/* If it's a single group and a single source, use 1-line output. */
	if (TTEST2(bp[0], 30) && bp[11] == 1 &&
	    ((njoin = EXTRACT_16BITS(&bp[20])) + EXTRACT_16BITS(&bp[22])) == 1) {
		int hold;

		(void)printf(" RPF %s ", ipaddr_string(bp));
		hold = EXTRACT_16BITS(&bp[6]);
		if (hold != 180) {
			(void)printf("Hold ");
			relts_print(hold);
		}
		(void)printf("%s (%s/%d, %s", njoin ? "Join" : "Prune",
		ipaddr_string(&bp[26]), bp[25] & 0x3f,
		ipaddr_string(&bp[12]));
		if (EXTRACT_32BITS(&bp[16]) != 0xffffffff)
			(void)printf("/%s", ipaddr_string(&bp[16]));
		(void)printf(") %s%s %s",
		    (bp[24] & 0x01) ? "Sparse" : "Dense",
		    (bp[25] & 0x80) ? " WC" : "",
		    (bp[25] & 0x40) ? "RP" : "SPT");
		return;
	}

	TCHECK2(bp[0], 4);
	if (vflag > 1)
		(void)printf("\n");
	(void)printf(" Upstream Nbr: %s", ipaddr_string(bp));
	TCHECK2(bp[6], 2);
	if (vflag > 1)
		(void)printf("\n");
	(void)printf(" Hold time: ");
	relts_print(EXTRACT_16BITS(&bp[6]));
	if (vflag < 2)
		return;
	bp += 8;
	len -= 8;

	TCHECK2(bp[0], 4);
	maddrlen = bp[1];
	addrlen = bp[2];
	ngroups = bp[3];
	bp += 4;
	len -= 4;
	while (ngroups--) {
		TCHECK2(bp[0], 4);
		(void)printf("\n\tGroup: %s", ipaddr_string(bp));
		if (EXTRACT_32BITS(&bp[4]) != 0xffffffff)
			(void)printf("/%s", ipaddr_string(&bp[4]));
		TCHECK2(bp[8], 4);
		njoin = EXTRACT_16BITS(&bp[8]);
		nprune = EXTRACT_16BITS(&bp[10]);
		(void)printf(" joined: %d pruned: %d", njoin, nprune);
		bp += 12;
		len -= 12;
		for (njp = 0; njp < (njoin + nprune); njp++) {
			char *type;

			if (njp < njoin)
				type = "Join ";
			else
				type = "Prune";
			TCHECK2(bp[0], 6);
			(void)printf("\n\t%s %s%s%s%s/%d", type,
			    (bp[0] & 0x01) ? "Sparse " : "Dense ",
			    (bp[1] & 0x80) ? "WC " : "",
			    (bp[1] & 0x40) ? "RP " : "SPT ",
			ipaddr_string(&bp[2]), bp[1] & 0x3f);
			bp += 6;
			len -= 6;
		}
	}
	return;
trunc:
	(void)printf("[|pim]");
	return;
}

void
pimv1_print(register const u_char *bp, register u_int len)
{
	register const u_char *ep;
	register u_char type;

	ep = (const u_char *)snapend;
	if (bp >= ep)
		return;

	type = bp[1];

	switch (type) {
	case 0:
		(void)printf(" Query");
		if (TTEST(bp[8])) {
			switch (bp[8] >> 4) {
			case 0:
				(void)printf(" Dense-mode");
				break;
			case 1:
				(void)printf(" Sparse-mode");
				break;
			case 2:
				(void)printf(" Sparse-Dense-mode");
				break;
			default:
				(void)printf(" mode-%d", bp[8] >> 4);
				break;
			}
		}
		if (vflag) {
			TCHECK2(bp[10],2);
			(void)printf(" (Hold-time ");
			relts_print(EXTRACT_16BITS(&bp[10]));
			(void)printf(")");
		}
		break;

	case 1:
		(void)printf(" Register");
		TCHECK2(bp[8], 20);			/* ip header */
		(void)printf(" for %s > %s", ipaddr_string(&bp[20]),
		    ipaddr_string(&bp[24]));
		break;
	case 2:
		(void)printf(" Register-Stop");
		TCHECK2(bp[12], 4);
		(void)printf(" for %s > %s", ipaddr_string(&bp[8]),
		    ipaddr_string(&bp[12]));
		break;
	case 3:
		(void)printf(" Join/Prune");
		if (vflag)
			pimv1_join_prune_print(&bp[8], len - 8);
		break;
	case 4:
		(void)printf(" RP-reachable");
		if (vflag) {
			TCHECK2(bp[22], 2);
			(void)printf(" group %s",
			ipaddr_string(&bp[8]));
			if (EXTRACT_32BITS(&bp[12]) != 0xffffffff)
				(void)printf("/%s", ipaddr_string(&bp[12]));
			(void)printf(" RP %s hold ", ipaddr_string(&bp[16]));
			relts_print(EXTRACT_16BITS(&bp[22]));
		}
		break;
	case 5:
		(void)printf(" Assert");
		TCHECK2(bp[16], 4);
		(void)printf(" for %s > %s", ipaddr_string(&bp[16]),
		    ipaddr_string(&bp[8]));
		if (EXTRACT_32BITS(&bp[12]) != 0xffffffff)
			(void)printf("/%s", ipaddr_string(&bp[12]));
		TCHECK2(bp[24], 4);
		(void)printf(" %s pref %d metric %d",
		    (bp[20] & 0x80) ? "RP-tree" : "SPT",
		EXTRACT_32BITS(&bp[20]) & 0x7fffffff,
		EXTRACT_32BITS(&bp[24]));
		break;
	case 6:
		(void)printf(" Graft");
		if (vflag)
			pimv1_join_prune_print(&bp[8], len - 8);
		break;
	case 7:
		(void)printf(" Graft-ACK");
		if (vflag)
			pimv1_join_prune_print(&bp[8], len - 8);
		break;
	case 8:
		(void)printf(" Mode");
		break;
	default:
		(void)printf(" [type %d]", type);
		break;
	}
	if ((bp[4] >> 4) != 1)
		(void)printf(" [v%d]", bp[4] >> 4);
	return;

trunc:
	(void)printf("[|pim]");
	return;
}

/*
 * auto-RP is a cisco protocol, documented at
 * ftp://ftpeng.cisco.com/ipmulticast/specs/pim-autorp-spec01.txt
 *
 * This implements version 1+, dated Sept 9, 1998.
 */
void
cisco_autorp_print(register const u_char *bp, register u_int len)
{
	int type;
	int numrps;
	int hold;

	TCHECK(bp[0]);
	(void)printf(" auto-rp ");
	type = bp[0];
	switch (type) {
	case 0x11:
		(void)printf("candidate-advert");
		break;
	case 0x12:
		(void)printf("mapping");
		break;
	default:
		(void)printf("type-0x%02x", type);
		break;
	}

	TCHECK(bp[1]);
	numrps = bp[1];

	TCHECK2(bp[2], 2);
	(void)printf(" Hold ");
	hold = EXTRACT_16BITS(&bp[2]);
	if (hold)
		relts_print(EXTRACT_16BITS(&bp[2]));
	else
		printf("FOREVER");

	/* Next 4 bytes are reserved. */

	bp += 8; len -= 8;

	/*XXX skip unless -v? */

	/*
	 * Rest of packet:
	 * numrps entries of the form:
	 * 32 bits: RP
	 * 6 bits: reserved
	 * 2 bits: PIM version supported, bit 0 is "supports v1", 1 is "v2".
	 * 8 bits: # of entries for this RP
	 * each entry: 7 bits: reserved, 1 bit: negative,
	 *	       8 bits: mask 32 bits: source
	 * lather, rinse, repeat.
	 */
	while (numrps--) {
		int nentries;
		char s;

		TCHECK2(bp[0], 4);
		(void)printf(" RP %s", ipaddr_string(bp));
		TCHECK(bp[4]);
		switch (bp[4] & 0x3) {
		case 0: printf(" PIMv?");
			break;
		case 1:	printf(" PIMv1");
			break;
		case 2:	printf(" PIMv2");
			break;
		case 3:	printf(" PIMv1+2");
			break;
		}
		if (bp[4] & 0xfc)
			(void)printf(" [rsvd=0x%02x]", bp[4] & 0xfc);
		TCHECK(bp[5]);
		nentries = bp[5];
		bp += 6; len -= 6;
		s = ' ';
		for (; nentries; nentries--) {
			TCHECK2(bp[0], 6);
			(void)printf("%c%s%s/%d", s, bp[0] & 1 ? "!" : "",
			    ipaddr_string(&bp[2]), bp[1]);
			if (bp[0] & 0xfe)
				(void)printf("[rsvd=0x%02x]", bp[0] & 0xfe);
			s = ',';
			bp += 6; len -= 6;
		}
	}
	return;

trunc:
	(void)printf("[|autorp]");
	return;
}

void
pim_print(register const u_char *bp, register u_int len)
{
	register const u_char *ep;
	register struct pim *pim = (struct pim *)bp;

	ep = (const u_char *)snapend;
	if (bp >= ep)
		return;
#ifdef notyet			/* currently we see only version and type */
	TCHECK(pim->pim_rsv);
#endif

	switch (PIM_VER(pim->pim_typever)) {
	case 2:		/* avoid hardcoding? */
		(void)printf("pim v2");
		pimv2_print(bp, len);
		break;
	default:
		(void)printf("pim v%d", PIM_VER(pim->pim_typever));
		break;
	}
	return;
}

/*
 * PIMv2 uses encoded address representations.
 *
 * The last PIM-SM I-D before RFC2117 was published specified the
 * following representation for unicast addresses.  However, RFC2117
 * specified no encoding for unicast addresses with the unicast
 * address length specified in the header.  Therefore, we have to
 * guess which encoding is being used (Cisco's PIMv2 implementation
 * uses the non-RFC encoding).  RFC2117 turns a previously "Reserved"
 * field into a 'unicast-address-length-in-bytes' field.  We guess
 * that it's the draft encoding if this reserved field is zero.
 *
 * RFC2362 goes back to the encoded format, and calls the addr length
 * field "reserved" again.
 *
 * The first byte is the address family, from:
 *
 *    0    Reserved
 *    1    IP (IP version 4)
 *    2    IP6 (IP version 6)
 *    3    NSAP
 *    4    HDLC (8-bit multidrop)
 *    5    BBN 1822
 *    6    802 (includes all 802 media plus Ethernet "canonical format")
 *    7    E.163
 *    8    E.164 (SMDS, Frame Relay, ATM)
 *    9    F.69 (Telex)
 *   10    X.121 (X.25, Frame Relay)
 *   11    IPX
 *   12    Appletalk
 *   13    Decnet IV
 *   14    Banyan Vines
 *   15    E.164 with NSAP format subaddress
 *
 * In addition, the second byte is an "Encoding".  0 is the default
 * encoding for the address family, and no other encodings are currently
 * specified.
 *
 */

static int pimv2_addr_len;

enum pimv2_addrtype {
	pimv2_unicast, pimv2_group, pimv2_source
};

/*  0                   1                   2                   3
 *  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
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * | Addr Family   | Encoding Type |     Unicast Address           |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+++++++
 *  0                   1                   2                   3
 *  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
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * | Addr Family   | Encoding Type |   Reserved    |  Mask Len     |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                Group multicast Address                        |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  0                   1                   2                   3
 *  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
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * | Addr Family   | Encoding Type | Rsrvd   |S|W|R|  Mask Len     |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                        Source Address                         |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static int
pimv2_addr_print(const u_char *bp, enum pimv2_addrtype at, int silent)
{
	int af;
	char *afstr;
	int len, hdrlen;

	TCHECK(bp[0]);

	if (pimv2_addr_len == 0) {
		TCHECK(bp[1]);
		switch (bp[0]) {
		case 1:
			af = AF_INET;
			afstr = "IPv4";
			len = 4;
			break;
#ifdef INET6
		case 2:
			af = AF_INET6;
			afstr = "IPv6";
			len = 16;
			break;
#endif
		default:
			return -1;
		}
		if (bp[1] != 0)
			return -1;
		hdrlen = 2;
	} else {
		switch (pimv2_addr_len) {
		case 4:
			af = AF_INET;
			afstr = "IPv4";
			break;
#ifdef INET6
		case 16:
			af = AF_INET6;
			afstr = "IPv6";
			break;
#endif
		default:
			return -1;
			break;
		}
		len = pimv2_addr_len;
		hdrlen = 0;
	}

	bp += hdrlen;
	switch (at) {
	case pimv2_unicast:
		TCHECK2(bp[0], len);
		if (af == AF_INET) {
			if (!silent)
				(void)printf("%s", ipaddr_string(bp));
		}
#ifdef INET6
		else if (af == AF_INET6) {
			if (!silent)
				(void)printf("%s", ip6addr_string(bp));
		}
#endif
		return hdrlen + len;
	case pimv2_group:
	case pimv2_source:
		TCHECK2(bp[0], len + 2);
		if (af == AF_INET) {
			if (!silent) {
				(void)printf("%s", ipaddr_string(bp + 2));
				if (bp[1] != 32)
					(void)printf("/%u", bp[1]);
			}
		}
#ifdef INET6
		else if (af == AF_INET6) {
			if (!silent) {
				(void)printf("%s", ip6addr_string(bp + 2));
				if (bp[1] != 128)
					(void)printf("/%u", bp[1]);
			}
		}
#endif
		if (bp[0] && !silent) {
			if (at == pimv2_group) {
				(void)printf("(0x%02x)", bp[0]);
			} else {
				(void)printf("(%s%s%s",
					bp[0] & 0x04 ? "S" : "",
					bp[0] & 0x02 ? "W" : "",
					bp[0] & 0x01 ? "R" : "");
				if (bp[0] & 0xf8) {
					(void) printf("+0x%02x", bp[0] & 0xf8);
				}
				(void)printf(")");
			}
		}
		return hdrlen + 2 + len;
	default:
		return -1;
	}
trunc:
	return -1;
}

static void
pimv2_print(register const u_char *bp, register u_int len)
{
	register const u_char *ep;
	register struct pim *pim = (struct pim *)bp;
	int advance;

	ep = (const u_char *)snapend;
	if (bp >= ep)
		return;
	if (ep > bp + len)
		ep = bp + len;
	TCHECK(pim->pim_rsv);
	pimv2_addr_len = pim->pim_rsv;
	if (pimv2_addr_len != 0)
		(void)printf("[RFC2117-encoding] ");

	switch (PIM_TYPE(pim->pim_typever)) {
	case 0:
	    {
		u_int16_t otype, olen;
		(void)printf(" Hello");
		bp += 4;
		while (bp < ep) {
			TCHECK2(bp[0], 4);
			otype = EXTRACT_16BITS(&bp[0]);
			olen = EXTRACT_16BITS(&bp[2]);
			TCHECK2(bp[0], 4 + olen);
			switch (otype) {
			case 1:		/* Hold time */
				(void)printf(" (Hold-time ");
				relts_print(EXTRACT_16BITS(&bp[4]));
				(void)printf(")");
				break;

			case 19:	/* DR-Priority */
				(void)printf(" (DR-Priority: ");
				if (olen != 4) {
					(void)printf("!olen=%d!)", olen);
				} else {
					(void)printf("%d)", EXTRACT_32BITS(&bp[4]));
				}
				break;

			case 20:
				(void)printf(" (Genid: 0x%08x)", EXTRACT_32BITS(&bp[4]));
				break;

			case 21:
				(void)printf(" (State Refresh Capable; v%d", bp[4]);
				if (bp[5] != 0) {
					(void)printf(" interval ");
					relts_print(bp[5]);
				}
				if (EXTRACT_16BITS(&bp[6]) != 0) {
					(void)printf(" ?0x%04x?", EXTRACT_16BITS(&bp[6]));
				}
				(void)printf(")");
				break;

			case 22:	/* Bidir-Capable */
				(void)printf(" (bidir-capable)");
				break;

			default:
				if (vflag)
					(void)printf(" [Hello option %d]", otype);
			}
			bp += 4 + olen;
		}
		break;
	    }

	case 1:
	{
		struct ip *ip;

		(void)printf(" Register");
		if (vflag && bp + 8 <= ep) {
			(void)printf(" %s%s", bp[4] & 0x80 ? "B" : "",
				bp[4] & 0x40 ? "N" : "");
		}
		bp += 8; len -= 8;

		/* encapsulated multicast packet */
		if (bp >= ep)
			break;
		ip = (struct ip *)bp;
		switch (IP_V(ip)) {
		case 4:	/* IPv4 */
			printf(" ");
			ip_print(bp, len);
			break;
#ifdef INET6
		case 6:	/* IPv6 */
			printf(" ");
			ip6_print(bp, len);
			break;
#endif
		default:
			(void)printf(" IP ver %d", IP_V(ip));
			break;
		}
		break;
	}

	case 2:
		(void)printf(" Register-Stop");
		bp += 4; len -= 4;
		if (bp >= ep)
			break;
		(void)printf(" group=");
		if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance; len -= advance;
		if (bp >= ep)
			break;
		(void)printf(" source=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance; len -= advance;
		break;

	case 3:
	case 6:
	case 7:
	    {
		u_int8_t ngroup;
		u_int16_t holdtime;
		u_int16_t njoin;
		u_int16_t nprune;
		int i, j;

		switch (PIM_TYPE(pim->pim_typever)) {
		case 3:
			(void)printf(" Join/Prune");
			break;
		case 6:
			(void)printf(" Graft");
			break;
		case 7:
			(void)printf(" Graft-ACK");
			break;
		}
		bp += 4; len -= 4;
		if (PIM_TYPE(pim->pim_typever) != 7) {	/*not for Graft-ACK*/
			if (bp >= ep)
				break;
			(void)printf(" upstream-neighbor=");
			if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
				(void)printf("...");
				break;
			}
			bp += advance; len -= advance;
		}
		if (bp + 4 > ep)
			break;
		ngroup = bp[1];
		holdtime = EXTRACT_16BITS(&bp[2]);
		(void)printf(" groups=%u", ngroup);
		if (PIM_TYPE(pim->pim_typever) != 7) {	/*not for Graft-ACK*/
			(void)printf(" holdtime=");
			if (holdtime == 0xffff)
				(void)printf("infty");
			else
				relts_print(holdtime);
		}
		bp += 4; len -= 4;
		for (i = 0; i < ngroup; i++) {
			if (bp >= ep)
				goto jp_done;
			(void)printf(" (group%d: ", i);
			if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
				(void)printf("...)");
				goto jp_done;
			}
			bp += advance; len -= advance;
			if (bp + 4 > ep) {
				(void)printf("...)");
				goto jp_done;
			}
			njoin = EXTRACT_16BITS(&bp[0]);
			nprune = EXTRACT_16BITS(&bp[2]);
			(void)printf(" join=%u", njoin);
			bp += 4; len -= 4;
			for (j = 0; j < njoin; j++) {
				(void)printf(" ");
				if ((advance = pimv2_addr_print(bp, pimv2_source, 0)) < 0) {
					(void)printf("...)");
					goto jp_done;
				}
				bp += advance; len -= advance;
			}
			(void)printf(" prune=%u", nprune);
			for (j = 0; j < nprune; j++) {
				(void)printf(" ");
				if ((advance = pimv2_addr_print(bp, pimv2_source, 0)) < 0) {
					(void)printf("...)");
					goto jp_done;
				}
				bp += advance; len -= advance;
			}
			(void)printf(")");
		}
	jp_done:
		break;
	    }

	case 4:
	{
		int i, j, frpcnt;

		(void)printf(" Bootstrap");
		bp += 4;

		/* Fragment Tag, Hash Mask len, and BSR-priority */
		if (bp + sizeof(u_int16_t) >= ep) break;
		(void)printf(" tag=%x", EXTRACT_16BITS(bp));
		bp += sizeof(u_int16_t);
		if (bp >= ep) break;
		(void)printf(" hashmlen=%d", bp[0]);
		if (bp + 1 >= ep) break;
		(void)printf(" BSRprio=%d", bp[1]);
		bp += 2;

		/* Encoded-Unicast-BSR-Address */
		if (bp >= ep) break;
		(void)printf(" BSR=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance;

		for (i = 0; bp < ep; i++) {
			/* Encoded-Group Address */
			(void)printf(" (group%d: ", i);
			if ((advance = pimv2_addr_print(bp, pimv2_group, 0))
			    < 0) {
				(void)printf("...)");
				goto bs_done;
			}
			bp += advance;

			/* RP-Count, Frag RP-Cnt, and rsvd */
			if (bp >= ep) {
				(void)printf("...)");
				goto bs_done;
			}
			(void)printf(" RPcnt=%d", bp[0]);
			if (bp + 1 >= ep) {
				(void)printf("...)");
				goto bs_done;
			}
			(void)printf(" FRPcnt=%d", frpcnt = bp[1]);
			bp += 4;

			for (j = 0; j < frpcnt && bp < ep; j++) {
				/* each RP info */
				(void)printf(" RP%d=", j);
				if ((advance = pimv2_addr_print(bp,
								pimv2_unicast,
								0)) < 0) {
					(void)printf("...)");
					goto bs_done;
				}
				bp += advance;

				if (bp + 1 >= ep) {
					(void)printf("...)");
					goto bs_done;
				}
				(void)printf(",holdtime=");
				relts_print(EXTRACT_16BITS(bp));
				if (bp + 2 >= ep) {
					(void)printf("...)");
					goto bs_done;
				}
				(void)printf(",prio=%d", bp[2]);
				bp += 4;
			}
			(void)printf(")");
		}
	   bs_done:
		break;
	}
	case 5:
		(void)printf(" Assert");
		bp += 4; len -= 4;
		if (bp >= ep)
			break;
		(void)printf(" group=");
		if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance; len -= advance;
		if (bp >= ep)
			break;
		(void)printf(" src=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance; len -= advance;
		if (bp + 8 > ep)
			break;
		if (bp[0] & 0x80)
			(void)printf(" RPT");
		(void)printf(" pref=%u", EXTRACT_32BITS(&bp[0]) & 0x7fffffff);
		(void)printf(" metric=%u", EXTRACT_32BITS(&bp[4]));
		break;

	case 8:
	{
		int i, pfxcnt;

		(void)printf(" Candidate-RP-Advertisement");
		bp += 4;

		/* Prefix-Cnt, Priority, and Holdtime */
		if (bp >= ep) break;
		(void)printf(" prefix-cnt=%d", bp[0]);
		pfxcnt = bp[0];
		if (bp + 1 >= ep) break;
		(void)printf(" prio=%d", bp[1]);
		if (bp + 3 >= ep) break;
		(void)printf(" holdtime=");
		relts_print(EXTRACT_16BITS(&bp[2]));
		bp += 4;

		/* Encoded-Unicast-RP-Address */
		if (bp >= ep) break;
		(void)printf(" RP=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance;

		/* Encoded-Group Addresses */
		for (i = 0; i < pfxcnt && bp < ep; i++) {
			(void)printf(" Group%d=", i);
			if ((advance = pimv2_addr_print(bp, pimv2_group, 0))
			    < 0) {
				(void)printf("...");
				break;
			}
			bp += advance;
		}
		break;
	}

	case 9:
		(void)printf(" Prune-Refresh");
		(void)printf(" src=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance;
		(void)printf(" grp=");
		if ((advance = pimv2_addr_print(bp, pimv2_group, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance;
		(void)printf(" forwarder=");
		if ((advance = pimv2_addr_print(bp, pimv2_unicast, 0)) < 0) {
			(void)printf("...");
			break;
		}
		bp += advance;
		TCHECK2(bp[0], 2);
		(void)printf(" TUNR ");
		relts_print(EXTRACT_16BITS(bp));
		break;


	 default:
		(void)printf(" [type %d]", PIM_TYPE(pim->pim_typever));
		break;
	}

	return;

trunc:
	(void)printf("[|pim]");
}
OpenPOWER on IntegriCloud