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