xref: /dragonfly/contrib/tcpdump/print-icmp6.c (revision da0d35cf)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
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 /* \summary: IPv6 Internet Control Message Protocol (ICMPv6) printer */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <netdissect-stdinc.h>
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "addrtostr.h"
36 #include "extract.h"
37 
38 #include "ip6.h"
39 #include "ipproto.h"
40 
41 #include "udp.h"
42 #include "ah.h"
43 
44 static const char icmp6_tstr[] = " [|icmp6]";
45 static const char rpl_tstr[] = " [|rpl]";
46 static const char mldv2_tstr[] = " [|mldv2]";
47 
48 /*	NetBSD: icmp6.h,v 1.13 2000/08/03 16:30:37 itojun Exp 	*/
49 /*	$KAME: icmp6.h,v 1.22 2000/08/03 15:25:16 jinmei Exp $	*/
50 
51 /*
52  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
53  * All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. Neither the name of the project nor the names of its contributors
64  *    may be used to endorse or promote products derived from this software
65  *    without specific prior written permission.
66  *
67  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77  * SUCH DAMAGE.
78  */
79 
80 struct icmp6_hdr {
81 	uint8_t		icmp6_type;	/* type field */
82 	uint8_t		icmp6_code;	/* code field */
83 	uint16_t	icmp6_cksum;	/* checksum field */
84 	union {
85 		uint32_t	icmp6_un_data32[1]; /* type-specific field */
86 		uint16_t	icmp6_un_data16[2]; /* type-specific field */
87 		uint8_t		icmp6_un_data8[4];  /* type-specific field */
88 	} icmp6_dataun;
89 };
90 
91 #define icmp6_data32	icmp6_dataun.icmp6_un_data32
92 #define icmp6_data16	icmp6_dataun.icmp6_un_data16
93 #define icmp6_data8	icmp6_dataun.icmp6_un_data8
94 #define icmp6_pptr	icmp6_data32[0]		/* parameter prob */
95 #define icmp6_mtu	icmp6_data32[0]		/* packet too big */
96 #define icmp6_id	icmp6_data16[0]		/* echo request/reply */
97 #define icmp6_seq	icmp6_data16[1]		/* echo request/reply */
98 #define icmp6_maxdelay	icmp6_data16[0]		/* mcast group membership */
99 
100 #define ICMP6_DST_UNREACH		1	/* dest unreachable, codes: */
101 #define ICMP6_PACKET_TOO_BIG		2	/* packet too big */
102 #define ICMP6_TIME_EXCEEDED		3	/* time exceeded, code: */
103 #define ICMP6_PARAM_PROB		4	/* ip6 header bad */
104 
105 #define ICMP6_ECHO_REQUEST		128	/* echo service */
106 #define ICMP6_ECHO_REPLY		129	/* echo reply */
107 #define ICMP6_MEMBERSHIP_QUERY		130	/* group membership query */
108 #define MLD6_LISTENER_QUERY		130 	/* multicast listener query */
109 #define ICMP6_MEMBERSHIP_REPORT		131	/* group membership report */
110 #define MLD6_LISTENER_REPORT		131	/* multicast listener report */
111 #define ICMP6_MEMBERSHIP_REDUCTION	132	/* group membership termination */
112 #define MLD6_LISTENER_DONE		132	/* multicast listener done */
113 
114 #define ND_ROUTER_SOLICIT		133	/* router solicitation */
115 #define ND_ROUTER_ADVERT		134	/* router advertisement */
116 #define ND_NEIGHBOR_SOLICIT		135	/* neighbor solicitation */
117 #define ND_NEIGHBOR_ADVERT		136	/* neighbor advertisement */
118 #define ND_REDIRECT			137	/* redirect */
119 
120 #define ICMP6_ROUTER_RENUMBERING	138	/* router renumbering */
121 
122 #define ICMP6_WRUREQUEST		139	/* who are you request */
123 #define ICMP6_WRUREPLY			140	/* who are you reply */
124 #define ICMP6_FQDN_QUERY		139	/* FQDN query */
125 #define ICMP6_FQDN_REPLY		140	/* FQDN reply */
126 #define ICMP6_NI_QUERY			139	/* node information request */
127 #define ICMP6_NI_REPLY			140	/* node information reply */
128 #define IND_SOLICIT			141	/* inverse neighbor solicitation */
129 #define IND_ADVERT			142	/* inverse neighbor advertisement */
130 
131 #define ICMP6_V2_MEMBERSHIP_REPORT	143	/* v2 membership report */
132 #define MLDV2_LISTENER_REPORT		143	/* v2 multicast listener report */
133 #define ICMP6_HADISCOV_REQUEST		144
134 #define ICMP6_HADISCOV_REPLY		145
135 #define ICMP6_MOBILEPREFIX_SOLICIT	146
136 #define ICMP6_MOBILEPREFIX_ADVERT	147
137 
138 #define MLD6_MTRACE_RESP		200	/* mtrace response(to sender) */
139 #define MLD6_MTRACE			201	/* mtrace messages */
140 
141 #define ICMP6_MAXTYPE			201
142 
143 #define ICMP6_DST_UNREACH_NOROUTE	0	/* no route to destination */
144 #define ICMP6_DST_UNREACH_ADMIN	 	1	/* administratively prohibited */
145 #define ICMP6_DST_UNREACH_NOTNEIGHBOR	2	/* not a neighbor(obsolete) */
146 #define ICMP6_DST_UNREACH_BEYONDSCOPE	2	/* beyond scope of source address */
147 #define ICMP6_DST_UNREACH_ADDR		3	/* address unreachable */
148 #define ICMP6_DST_UNREACH_NOPORT	4	/* port unreachable */
149 
150 #define ICMP6_TIME_EXCEED_TRANSIT 	0	/* ttl==0 in transit */
151 #define ICMP6_TIME_EXCEED_REASSEMBLY	1	/* ttl==0 in reass */
152 
153 #define ICMP6_PARAMPROB_HEADER 	 	0	/* erroneous header field */
154 #define ICMP6_PARAMPROB_NEXTHEADER	1	/* unrecognized next header */
155 #define ICMP6_PARAMPROB_OPTION		2	/* unrecognized option */
156 
157 #define ICMP6_INFOMSG_MASK		0x80	/* all informational messages */
158 
159 #define ICMP6_NI_SUBJ_IPV6	0	/* Query Subject is an IPv6 address */
160 #define ICMP6_NI_SUBJ_FQDN	1	/* Query Subject is a Domain name */
161 #define ICMP6_NI_SUBJ_IPV4	2	/* Query Subject is an IPv4 address */
162 
163 #define ICMP6_NI_SUCCESS	0	/* node information successful reply */
164 #define ICMP6_NI_REFUSED	1	/* node information request is refused */
165 #define ICMP6_NI_UNKNOWN	2	/* unknown Qtype */
166 
167 #define ICMP6_ROUTER_RENUMBERING_COMMAND  0	/* rr command */
168 #define ICMP6_ROUTER_RENUMBERING_RESULT   1	/* rr result */
169 #define ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET   255	/* rr seq num reset */
170 
171 /* Used in kernel only */
172 #define ND_REDIRECT_ONLINK	0	/* redirect to an on-link node */
173 #define ND_REDIRECT_ROUTER	1	/* redirect to a better router */
174 
175 /*
176  * Multicast Listener Discovery
177  */
178 struct mld6_hdr {
179 	struct icmp6_hdr	mld6_hdr;
180 	struct in6_addr		mld6_addr; /* multicast address */
181 };
182 
183 #define mld6_type	mld6_hdr.icmp6_type
184 #define mld6_code	mld6_hdr.icmp6_code
185 #define mld6_cksum	mld6_hdr.icmp6_cksum
186 #define mld6_maxdelay	mld6_hdr.icmp6_data16[0]
187 #define mld6_reserved	mld6_hdr.icmp6_data16[1]
188 
189 #define MLD_MINLEN	24
190 #define MLDV2_MINLEN	28
191 
192 /*
193  * Neighbor Discovery
194  */
195 
196 struct nd_router_solicit {	/* router solicitation */
197 	struct icmp6_hdr 	nd_rs_hdr;
198 	/* could be followed by options */
199 };
200 
201 #define nd_rs_type	nd_rs_hdr.icmp6_type
202 #define nd_rs_code	nd_rs_hdr.icmp6_code
203 #define nd_rs_cksum	nd_rs_hdr.icmp6_cksum
204 #define nd_rs_reserved	nd_rs_hdr.icmp6_data32[0]
205 
206 struct nd_router_advert {	/* router advertisement */
207 	struct icmp6_hdr	nd_ra_hdr;
208 	uint32_t		nd_ra_reachable;	/* reachable time */
209 	uint32_t		nd_ra_retransmit;	/* retransmit timer */
210 	/* could be followed by options */
211 };
212 
213 #define nd_ra_type		nd_ra_hdr.icmp6_type
214 #define nd_ra_code		nd_ra_hdr.icmp6_code
215 #define nd_ra_cksum		nd_ra_hdr.icmp6_cksum
216 #define nd_ra_curhoplimit	nd_ra_hdr.icmp6_data8[0]
217 #define nd_ra_flags_reserved	nd_ra_hdr.icmp6_data8[1]
218 #define ND_RA_FLAG_MANAGED	0x80
219 #define ND_RA_FLAG_OTHER	0x40
220 #define ND_RA_FLAG_HOME_AGENT	0x20
221 
222 /*
223  * Router preference values based on draft-draves-ipngwg-router-selection-01.
224  * These are non-standard definitions.
225  */
226 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
227 
228 #define ND_RA_FLAG_RTPREF_HIGH	0x08 /* 00001000 */
229 #define ND_RA_FLAG_RTPREF_MEDIUM	0x00 /* 00000000 */
230 #define ND_RA_FLAG_RTPREF_LOW	0x18 /* 00011000 */
231 #define ND_RA_FLAG_RTPREF_RSV	0x10 /* 00010000 */
232 
233 #define nd_ra_router_lifetime	nd_ra_hdr.icmp6_data16[1]
234 
235 struct nd_neighbor_solicit {	/* neighbor solicitation */
236 	struct icmp6_hdr	nd_ns_hdr;
237 	struct in6_addr		nd_ns_target;	/*target address */
238 	/* could be followed by options */
239 };
240 
241 #define nd_ns_type		nd_ns_hdr.icmp6_type
242 #define nd_ns_code		nd_ns_hdr.icmp6_code
243 #define nd_ns_cksum		nd_ns_hdr.icmp6_cksum
244 #define nd_ns_reserved		nd_ns_hdr.icmp6_data32[0]
245 
246 struct nd_neighbor_advert {	/* neighbor advertisement */
247 	struct icmp6_hdr	nd_na_hdr;
248 	struct in6_addr		nd_na_target;	/* target address */
249 	/* could be followed by options */
250 };
251 
252 #define nd_na_type		nd_na_hdr.icmp6_type
253 #define nd_na_code		nd_na_hdr.icmp6_code
254 #define nd_na_cksum		nd_na_hdr.icmp6_cksum
255 #define nd_na_flags_reserved	nd_na_hdr.icmp6_data32[0]
256 
257 #define ND_NA_FLAG_ROUTER		0x80000000
258 #define ND_NA_FLAG_SOLICITED		0x40000000
259 #define ND_NA_FLAG_OVERRIDE		0x20000000
260 
261 struct nd_redirect {		/* redirect */
262 	struct icmp6_hdr	nd_rd_hdr;
263 	struct in6_addr		nd_rd_target;	/* target address */
264 	struct in6_addr		nd_rd_dst;	/* destination address */
265 	/* could be followed by options */
266 };
267 
268 #define nd_rd_type		nd_rd_hdr.icmp6_type
269 #define nd_rd_code		nd_rd_hdr.icmp6_code
270 #define nd_rd_cksum		nd_rd_hdr.icmp6_cksum
271 #define nd_rd_reserved		nd_rd_hdr.icmp6_data32[0]
272 
273 struct nd_opt_hdr {		/* Neighbor discovery option header */
274 	uint8_t		nd_opt_type;
275 	uint8_t		nd_opt_len;
276 	/* followed by option specific data*/
277 };
278 
279 #define ND_OPT_SOURCE_LINKADDR		1
280 #define ND_OPT_TARGET_LINKADDR		2
281 #define ND_OPT_PREFIX_INFORMATION	3
282 #define ND_OPT_REDIRECTED_HEADER	4
283 #define ND_OPT_MTU			5
284 #define ND_OPT_ADVINTERVAL		7
285 #define ND_OPT_HOMEAGENT_INFO		8
286 #define ND_OPT_ROUTE_INFO		24	/* RFC4191 */
287 #define ND_OPT_RDNSS			25
288 #define ND_OPT_DNSSL			31
289 
290 struct nd_opt_prefix_info {	/* prefix information */
291 	nd_uint8_t		nd_opt_pi_type;
292 	nd_uint8_t		nd_opt_pi_len;
293 	nd_uint8_t		nd_opt_pi_prefix_len;
294 	nd_uint8_t		nd_opt_pi_flags_reserved;
295 	nd_uint32_t		nd_opt_pi_valid_time;
296 	nd_uint32_t		nd_opt_pi_preferred_time;
297 	nd_uint32_t		nd_opt_pi_reserved2;
298 	struct in6_addr	nd_opt_pi_prefix;
299 };
300 
301 #define ND_OPT_PI_FLAG_ONLINK		0x80
302 #define ND_OPT_PI_FLAG_AUTO		0x40
303 #define ND_OPT_PI_FLAG_ROUTER		0x20	/*2292bis*/
304 
305 struct nd_opt_rd_hdr {         /* redirected header */
306 	uint8_t		nd_opt_rh_type;
307 	uint8_t		nd_opt_rh_len;
308 	uint16_t	nd_opt_rh_reserved1;
309 	uint32_t	nd_opt_rh_reserved2;
310 	/* followed by IP header and data */
311 };
312 
313 struct nd_opt_mtu {		/* MTU option */
314 	uint8_t		nd_opt_mtu_type;
315 	uint8_t		nd_opt_mtu_len;
316 	uint16_t	nd_opt_mtu_reserved;
317 	uint32_t	nd_opt_mtu_mtu;
318 };
319 
320 struct nd_opt_rdnss {		/* RDNSS RFC 6106 5.1 */
321 	uint8_t		nd_opt_rdnss_type;
322 	uint8_t		nd_opt_rdnss_len;
323 	uint16_t	nd_opt_rdnss_reserved;
324 	uint32_t	nd_opt_rdnss_lifetime;
325 	struct in6_addr nd_opt_rdnss_addr[1];	/* variable-length */
326 };
327 
328 struct nd_opt_dnssl {		/* DNSSL RFC 6106 5.2 */
329 	uint8_t  nd_opt_dnssl_type;
330 	uint8_t  nd_opt_dnssl_len;
331 	uint16_t nd_opt_dnssl_reserved;
332 	uint32_t nd_opt_dnssl_lifetime;
333 	/* followed by list of DNS search domains, variable-length */
334 };
335 
336 struct nd_opt_advinterval {	/* Advertisement interval option */
337 	uint8_t		nd_opt_adv_type;
338 	uint8_t		nd_opt_adv_len;
339 	uint16_t	nd_opt_adv_reserved;
340 	uint32_t	nd_opt_adv_interval;
341 };
342 
343 struct nd_opt_homeagent_info {	/* Home Agent info */
344 	uint8_t		nd_opt_hai_type;
345 	uint8_t		nd_opt_hai_len;
346 	uint16_t	nd_opt_hai_reserved;
347 	int16_t		nd_opt_hai_preference;
348 	uint16_t	nd_opt_hai_lifetime;
349 };
350 
351 struct nd_opt_route_info {	/* route info */
352 	uint8_t		nd_opt_rti_type;
353 	uint8_t		nd_opt_rti_len;
354 	uint8_t		nd_opt_rti_prefixlen;
355 	uint8_t		nd_opt_rti_flags;
356 	uint32_t	nd_opt_rti_lifetime;
357 	/* prefix follows */
358 };
359 
360 /*
361  * icmp6 namelookup
362  */
363 
364 struct icmp6_namelookup {
365 	struct icmp6_hdr 	icmp6_nl_hdr;
366 	uint8_t		icmp6_nl_nonce[8];
367 	int32_t		icmp6_nl_ttl;
368 #if 0
369 	uint8_t		icmp6_nl_len;
370 	uint8_t		icmp6_nl_name[3];
371 #endif
372 	/* could be followed by options */
373 };
374 
375 /*
376  * icmp6 node information
377  */
378 struct icmp6_nodeinfo {
379 	struct icmp6_hdr icmp6_ni_hdr;
380 	uint8_t icmp6_ni_nonce[8];
381 	/* could be followed by reply data */
382 };
383 
384 #define ni_type		icmp6_ni_hdr.icmp6_type
385 #define ni_code		icmp6_ni_hdr.icmp6_code
386 #define ni_cksum	icmp6_ni_hdr.icmp6_cksum
387 #define ni_qtype	icmp6_ni_hdr.icmp6_data16[0]
388 #define ni_flags	icmp6_ni_hdr.icmp6_data16[1]
389 
390 #define NI_QTYPE_NOOP		0 /* NOOP  */
391 #define NI_QTYPE_SUPTYPES	1 /* Supported Qtypes */
392 #define NI_QTYPE_FQDN		2 /* FQDN (draft 04) */
393 #define NI_QTYPE_DNSNAME	2 /* DNS Name */
394 #define NI_QTYPE_NODEADDR	3 /* Node Addresses */
395 #define NI_QTYPE_IPV4ADDR	4 /* IPv4 Addresses */
396 
397 /* network endian */
398 #define NI_SUPTYPE_FLAG_COMPRESS	((uint16_t)htons(0x1))
399 #define NI_FQDN_FLAG_VALIDTTL		((uint16_t)htons(0x1))
400 
401 /* network endian */
402 #define NI_NODEADDR_FLAG_TRUNCATE	((uint16_t)htons(0x1))
403 #define NI_NODEADDR_FLAG_ALL		((uint16_t)htons(0x2))
404 #define NI_NODEADDR_FLAG_COMPAT		((uint16_t)htons(0x4))
405 #define NI_NODEADDR_FLAG_LINKLOCAL	((uint16_t)htons(0x8))
406 #define NI_NODEADDR_FLAG_SITELOCAL	((uint16_t)htons(0x10))
407 #define NI_NODEADDR_FLAG_GLOBAL		((uint16_t)htons(0x20))
408 #define NI_NODEADDR_FLAG_ANYCAST	((uint16_t)htons(0x40)) /* just experimental. not in spec */
409 
410 struct ni_reply_fqdn {
411 	uint32_t ni_fqdn_ttl;	/* TTL */
412 	uint8_t ni_fqdn_namelen; /* length in octets of the FQDN */
413 	uint8_t ni_fqdn_name[3]; /* XXX: alignment */
414 };
415 
416 /*
417  * Router Renumbering. as router-renum-08.txt
418  */
419 struct icmp6_router_renum {	/* router renumbering header */
420 	struct icmp6_hdr	rr_hdr;
421 	uint8_t		rr_segnum;
422 	uint8_t		rr_flags;
423 	uint16_t	rr_maxdelay;
424 	uint32_t	rr_reserved;
425 };
426 #define ICMP6_RR_FLAGS_TEST		0x80
427 #define ICMP6_RR_FLAGS_REQRESULT	0x40
428 #define ICMP6_RR_FLAGS_FORCEAPPLY	0x20
429 #define ICMP6_RR_FLAGS_SPECSITE		0x10
430 #define ICMP6_RR_FLAGS_PREVDONE		0x08
431 
432 #define rr_type		rr_hdr.icmp6_type
433 #define rr_code		rr_hdr.icmp6_code
434 #define rr_cksum	rr_hdr.icmp6_cksum
435 #define rr_seqnum 	rr_hdr.icmp6_data32[0]
436 
437 struct rr_pco_match {		/* match prefix part */
438 	uint8_t		rpm_code;
439 	uint8_t		rpm_len;
440 	uint8_t		rpm_ordinal;
441 	uint8_t		rpm_matchlen;
442 	uint8_t		rpm_minlen;
443 	uint8_t		rpm_maxlen;
444 	uint16_t	rpm_reserved;
445 	struct	in6_addr	rpm_prefix;
446 };
447 
448 #define RPM_PCO_ADD		1
449 #define RPM_PCO_CHANGE		2
450 #define RPM_PCO_SETGLOBAL	3
451 #define RPM_PCO_MAX		4
452 
453 struct rr_pco_use {		/* use prefix part */
454 	uint8_t		rpu_uselen;
455 	uint8_t		rpu_keeplen;
456 	uint8_t		rpu_ramask;
457 	uint8_t		rpu_raflags;
458 	uint32_t	rpu_vltime;
459 	uint32_t	rpu_pltime;
460 	uint32_t	rpu_flags;
461 	struct	in6_addr rpu_prefix;
462 };
463 #define ICMP6_RR_PCOUSE_RAFLAGS_ONLINK	0x80
464 #define ICMP6_RR_PCOUSE_RAFLAGS_AUTO	0x40
465 
466 /* network endian */
467 #define ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME     ((uint32_t)htonl(0x80000000))
468 #define ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME     ((uint32_t)htonl(0x40000000))
469 
470 struct rr_result {		/* router renumbering result message */
471 	uint16_t	rrr_flags;
472 	uint8_t		rrr_ordinal;
473 	uint8_t		rrr_matchedlen;
474 	uint32_t	rrr_ifid;
475 	struct	in6_addr rrr_prefix;
476 };
477 /* network endian */
478 #define ICMP6_RR_RESULT_FLAGS_OOB		((uint16_t)htons(0x0002))
479 #define ICMP6_RR_RESULT_FLAGS_FORBIDDEN		((uint16_t)htons(0x0001))
480 
481 static const char *get_rtpref(u_int);
482 static const char *get_lifetime(uint32_t);
483 static void print_lladdr(netdissect_options *ndo, const u_char *, size_t);
484 static void icmp6_opt_print(netdissect_options *ndo, const u_char *, int);
485 static void mld6_print(netdissect_options *ndo, const u_char *);
486 static void mldv2_report_print(netdissect_options *ndo, const u_char *, u_int);
487 static void mldv2_query_print(netdissect_options *ndo, const u_char *, u_int);
488 static const struct udphdr *get_upperlayer(netdissect_options *ndo, const u_char *, u_int *);
489 static void dnsname_print(netdissect_options *ndo, const u_char *, const u_char *);
490 static void icmp6_nodeinfo_print(netdissect_options *ndo, u_int, const u_char *, const u_char *);
491 static void icmp6_rrenum_print(netdissect_options *ndo, const u_char *, const u_char *);
492 
493 #ifndef abs
494 #define abs(a)	((0 < (a)) ? (a) : -(a))
495 #endif
496 
497 #include "rpl.h"
498 
499 static const struct tok icmp6_type_values[] = {
500     { ICMP6_DST_UNREACH, "destination unreachable"},
501     { ICMP6_PACKET_TOO_BIG, "packet too big"},
502     { ICMP6_TIME_EXCEEDED, "time exceeded in-transit"},
503     { ICMP6_PARAM_PROB, "parameter problem"},
504     { ICMP6_ECHO_REQUEST, "echo request"},
505     { ICMP6_ECHO_REPLY, "echo reply"},
506     { MLD6_LISTENER_QUERY, "multicast listener query"},
507     { MLD6_LISTENER_REPORT, "multicast listener report"},
508     { MLD6_LISTENER_DONE, "multicast listener done"},
509     { ND_ROUTER_SOLICIT, "router solicitation"},
510     { ND_ROUTER_ADVERT, "router advertisement"},
511     { ND_NEIGHBOR_SOLICIT, "neighbor solicitation"},
512     { ND_NEIGHBOR_ADVERT, "neighbor advertisement"},
513     { ND_REDIRECT, "redirect"},
514     { ICMP6_ROUTER_RENUMBERING, "router renumbering"},
515     { IND_SOLICIT, "inverse neighbor solicitation"},
516     { IND_ADVERT, "inverse neighbor advertisement"},
517     { MLDV2_LISTENER_REPORT, "multicast listener report v2"},
518     { ICMP6_HADISCOV_REQUEST, "ha discovery request"},
519     { ICMP6_HADISCOV_REPLY, "ha discovery reply"},
520     { ICMP6_MOBILEPREFIX_SOLICIT, "mobile router solicitation"},
521     { ICMP6_MOBILEPREFIX_ADVERT, "mobile router advertisement"},
522     { ICMP6_WRUREQUEST, "who-are-you request"},
523     { ICMP6_WRUREPLY, "who-are-you reply"},
524     { ICMP6_NI_QUERY, "node information query"},
525     { ICMP6_NI_REPLY, "node information reply"},
526     { MLD6_MTRACE, "mtrace message"},
527     { MLD6_MTRACE_RESP, "mtrace response"},
528     { ND_RPL_MESSAGE,   "RPL"},
529     { 0,	NULL }
530 };
531 
532 static const struct tok icmp6_dst_unreach_code_values[] = {
533     { ICMP6_DST_UNREACH_NOROUTE, "unreachable route" },
534     { ICMP6_DST_UNREACH_ADMIN, " unreachable prohibited"},
535     { ICMP6_DST_UNREACH_BEYONDSCOPE, "beyond scope"},
536     { ICMP6_DST_UNREACH_ADDR, "unreachable address"},
537     { ICMP6_DST_UNREACH_NOPORT, "unreachable port"},
538     { 0,	NULL }
539 };
540 
541 static const struct tok icmp6_opt_pi_flag_values[] = {
542     { ND_OPT_PI_FLAG_ONLINK, "onlink" },
543     { ND_OPT_PI_FLAG_AUTO, "auto" },
544     { ND_OPT_PI_FLAG_ROUTER, "router" },
545     { 0,	NULL }
546 };
547 
548 static const struct tok icmp6_opt_ra_flag_values[] = {
549     { ND_RA_FLAG_MANAGED, "managed" },
550     { ND_RA_FLAG_OTHER, "other stateful"},
551     { ND_RA_FLAG_HOME_AGENT, "home agent"},
552     { 0,	NULL }
553 };
554 
555 static const struct tok icmp6_nd_na_flag_values[] = {
556     { ND_NA_FLAG_ROUTER, "router" },
557     { ND_NA_FLAG_SOLICITED, "solicited" },
558     { ND_NA_FLAG_OVERRIDE, "override" },
559     { 0,	NULL }
560 };
561 
562 
563 static const struct tok icmp6_opt_values[] = {
564    { ND_OPT_SOURCE_LINKADDR, "source link-address"},
565    { ND_OPT_TARGET_LINKADDR, "destination link-address"},
566    { ND_OPT_PREFIX_INFORMATION, "prefix info"},
567    { ND_OPT_REDIRECTED_HEADER, "redirected header"},
568    { ND_OPT_MTU, "mtu"},
569    { ND_OPT_RDNSS, "rdnss"},
570    { ND_OPT_DNSSL, "dnssl"},
571    { ND_OPT_ADVINTERVAL, "advertisement interval"},
572    { ND_OPT_HOMEAGENT_INFO, "homeagent information"},
573    { ND_OPT_ROUTE_INFO, "route info"},
574    { 0,	NULL }
575 };
576 
577 /* mldv2 report types */
578 static const struct tok mldv2report2str[] = {
579 	{ 1,	"is_in" },
580 	{ 2,	"is_ex" },
581 	{ 3,	"to_in" },
582 	{ 4,	"to_ex" },
583 	{ 5,	"allow" },
584 	{ 6,	"block" },
585 	{ 0,	NULL }
586 };
587 
588 static const char *
589 get_rtpref(u_int v)
590 {
591 	static const char *rtpref_str[] = {
592 		"medium",		/* 00 */
593 		"high",			/* 01 */
594 		"rsv",			/* 10 */
595 		"low"			/* 11 */
596 	};
597 
598 	return rtpref_str[((v & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff];
599 }
600 
601 static const char *
602 get_lifetime(uint32_t v)
603 {
604 	static char buf[20];
605 
606 	if (v == (uint32_t)~0UL)
607 		return "infinity";
608 	else {
609 		snprintf(buf, sizeof(buf), "%us", v);
610 		return buf;
611 	}
612 }
613 
614 static void
615 print_lladdr(netdissect_options *ndo, const uint8_t *p, size_t l)
616 {
617 	const uint8_t *ep, *q;
618 
619 	q = p;
620 	ep = p + l;
621 	while (l > 0 && q < ep) {
622 		if (q > p)
623                         ND_PRINT((ndo,":"));
624 		ND_PRINT((ndo,"%02x", *q++));
625 		l--;
626 	}
627 }
628 
629 static int icmp6_cksum(netdissect_options *ndo, const struct ip6_hdr *ip6,
630 	const struct icmp6_hdr *icp, u_int len)
631 {
632 	return nextproto6_cksum(ndo, ip6, (const uint8_t *)(const void *)icp, len, len,
633 				IPPROTO_ICMPV6);
634 }
635 
636 static const struct tok rpl_mop_values[] = {
637         { RPL_DIO_NONSTORING,         "nonstoring"},
638         { RPL_DIO_STORING,            "storing"},
639         { RPL_DIO_NONSTORING_MULTICAST, "nonstoring-multicast"},
640         { RPL_DIO_STORING_MULTICAST,  "storing-multicast"},
641         { 0, NULL},
642 };
643 
644 static const struct tok rpl_subopt_values[] = {
645         { RPL_OPT_PAD0, "pad0"},
646         { RPL_OPT_PADN, "padN"},
647         { RPL_DIO_METRICS, "metrics"},
648         { RPL_DIO_ROUTINGINFO, "routinginfo"},
649         { RPL_DIO_CONFIG,    "config"},
650         { RPL_DAO_RPLTARGET, "rpltarget"},
651         { RPL_DAO_TRANSITINFO, "transitinfo"},
652         { RPL_DIO_DESTPREFIX, "destprefix"},
653         { RPL_DAO_RPLTARGET_DESC, "rpltargetdesc"},
654         { 0, NULL},
655 };
656 
657 static void
658 rpl_dio_printopt(netdissect_options *ndo,
659                  const struct rpl_dio_genoption *opt,
660                  u_int length)
661 {
662         if(length < RPL_DIO_GENOPTION_LEN) return;
663         length -= RPL_DIO_GENOPTION_LEN;
664 
665         ND_TCHECK(opt->rpl_dio_len);
666 
667         while((opt->rpl_dio_type == RPL_OPT_PAD0 &&
668                (const u_char *)opt < ndo->ndo_snapend) ||
669               ND_TTEST2(*opt,(opt->rpl_dio_len+2))) {
670 
671                 unsigned int optlen = opt->rpl_dio_len+2;
672                 if(opt->rpl_dio_type == RPL_OPT_PAD0) {
673                         optlen = 1;
674                         ND_PRINT((ndo, " opt:pad0"));
675                 } else {
676                         ND_PRINT((ndo, " opt:%s len:%u ",
677                                   tok2str(rpl_subopt_values, "subopt:%u", opt->rpl_dio_type),
678                                   optlen));
679                         if(ndo->ndo_vflag > 2) {
680                                 unsigned int paylen = opt->rpl_dio_len;
681                                 if(paylen > length) paylen = length;
682                                 hex_print(ndo,
683                                           " ",
684                                           ((const uint8_t *)opt) + RPL_DIO_GENOPTION_LEN,  /* content of DIO option */
685                                           paylen);
686                         }
687                 }
688                 opt = (const struct rpl_dio_genoption *)(((const char *)opt) + optlen);
689                 length -= optlen;
690                 ND_TCHECK(opt->rpl_dio_len);
691         }
692         return;
693 trunc:
694 	ND_PRINT((ndo, "%s", rpl_tstr));
695 	return;
696 }
697 
698 static void
699 rpl_dio_print(netdissect_options *ndo,
700               const u_char *bp, u_int length)
701 {
702         const struct nd_rpl_dio *dio = (const struct nd_rpl_dio *)bp;
703         const char *dagid_str;
704 
705         ND_TCHECK(*dio);
706         dagid_str = ip6addr_string (ndo, dio->rpl_dagid);
707 
708         ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u,rank:%u,%smop:%s,prf:%u]",
709                   dagid_str,
710                   dio->rpl_dtsn,
711                   dio->rpl_instanceid,
712                   EXTRACT_16BITS(&dio->rpl_dagrank),
713                   RPL_DIO_GROUNDED(dio->rpl_mopprf) ? "grounded,":"",
714                   tok2str(rpl_mop_values, "mop%u", RPL_DIO_MOP(dio->rpl_mopprf)),
715                   RPL_DIO_PRF(dio->rpl_mopprf)));
716 
717         if(ndo->ndo_vflag > 1) {
718                 const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)&dio[1];
719                 rpl_dio_printopt(ndo, opt, length);
720         }
721 	return;
722 trunc:
723 	ND_PRINT((ndo, "%s", rpl_tstr));
724 	return;
725 }
726 
727 static void
728 rpl_dao_print(netdissect_options *ndo,
729               const u_char *bp, u_int length)
730 {
731         const struct nd_rpl_dao *dao = (const struct nd_rpl_dao *)bp;
732         const char *dagid_str = "<elided>";
733 
734         ND_TCHECK(*dao);
735         if (length < ND_RPL_DAO_MIN_LEN)
736         	goto tooshort;
737 
738         bp += ND_RPL_DAO_MIN_LEN;
739         length -= ND_RPL_DAO_MIN_LEN;
740         if(RPL_DAO_D(dao->rpl_flags)) {
741                 ND_TCHECK2(dao->rpl_dagid, DAGID_LEN);
742                 if (length < DAGID_LEN)
743                 	goto tooshort;
744                 dagid_str = ip6addr_string (ndo, dao->rpl_dagid);
745                 bp += DAGID_LEN;
746                 length -= DAGID_LEN;
747         }
748 
749         ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u%s%s,%02x]",
750                   dagid_str,
751                   dao->rpl_daoseq,
752                   dao->rpl_instanceid,
753                   RPL_DAO_K(dao->rpl_flags) ? ",acK":"",
754                   RPL_DAO_D(dao->rpl_flags) ? ",Dagid":"",
755                   dao->rpl_flags));
756 
757         if(ndo->ndo_vflag > 1) {
758                 const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)bp;
759                 rpl_dio_printopt(ndo, opt, length);
760         }
761 	return;
762 
763 trunc:
764 	ND_PRINT((ndo, "%s", rpl_tstr));
765 	return;
766 
767 tooshort:
768 	ND_PRINT((ndo," [|length too short]"));
769 	return;
770 }
771 
772 static void
773 rpl_daoack_print(netdissect_options *ndo,
774                  const u_char *bp, u_int length)
775 {
776         const struct nd_rpl_daoack *daoack = (const struct nd_rpl_daoack *)bp;
777         const char *dagid_str = "<elided>";
778 
779         ND_TCHECK2(*daoack, ND_RPL_DAOACK_MIN_LEN);
780         if (length < ND_RPL_DAOACK_MIN_LEN)
781         	goto tooshort;
782 
783         bp += ND_RPL_DAOACK_MIN_LEN;
784         length -= ND_RPL_DAOACK_MIN_LEN;
785         if(RPL_DAOACK_D(daoack->rpl_flags)) {
786                 ND_TCHECK2(daoack->rpl_dagid, DAGID_LEN);
787                 if (length < DAGID_LEN)
788                 	goto tooshort;
789                 dagid_str = ip6addr_string (ndo, daoack->rpl_dagid);
790                 bp += DAGID_LEN;
791                 length -= DAGID_LEN;
792         }
793 
794         ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u,status:%u]",
795                   dagid_str,
796                   daoack->rpl_daoseq,
797                   daoack->rpl_instanceid,
798                   daoack->rpl_status));
799 
800         /* no officially defined options for DAOACK, but print any we find */
801         if(ndo->ndo_vflag > 1) {
802                 const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)bp;
803                 rpl_dio_printopt(ndo, opt, length);
804         }
805 	return;
806 
807 trunc:
808 	ND_PRINT((ndo, "%s", rpl_tstr));
809 	return;
810 
811 tooshort:
812 	ND_PRINT((ndo," [|dao-length too short]"));
813 	return;
814 }
815 
816 static void
817 rpl_print(netdissect_options *ndo,
818           const struct icmp6_hdr *hdr,
819           const u_char *bp, u_int length)
820 {
821         int secured = hdr->icmp6_code & 0x80;
822         int basecode= hdr->icmp6_code & 0x7f;
823 
824         if(secured) {
825                 ND_PRINT((ndo, ", (SEC) [worktodo]"));
826                 /* XXX
827                  * the next header pointer needs to move forward to
828                  * skip the secure part.
829                  */
830                 return;
831         } else {
832                 ND_PRINT((ndo, ", (CLR)"));
833         }
834 
835         switch(basecode) {
836         case ND_RPL_DAG_IS:
837                 ND_PRINT((ndo, "DODAG Information Solicitation"));
838                 if(ndo->ndo_vflag) {
839                 }
840                 break;
841         case ND_RPL_DAG_IO:
842                 ND_PRINT((ndo, "DODAG Information Object"));
843                 if(ndo->ndo_vflag) {
844                         rpl_dio_print(ndo, bp, length);
845                 }
846                 break;
847         case ND_RPL_DAO:
848                 ND_PRINT((ndo, "Destination Advertisement Object"));
849                 if(ndo->ndo_vflag) {
850                         rpl_dao_print(ndo, bp, length);
851                 }
852                 break;
853         case ND_RPL_DAO_ACK:
854                 ND_PRINT((ndo, "Destination Advertisement Object Ack"));
855                 if(ndo->ndo_vflag) {
856                         rpl_daoack_print(ndo, bp, length);
857                 }
858                 break;
859         default:
860                 ND_PRINT((ndo, "RPL message, unknown code %u",hdr->icmp6_code));
861                 break;
862         }
863 	return;
864 
865 #if 0
866 trunc:
867 	ND_PRINT((ndo, "%s", rpl_tstr));
868 	return;
869 #endif
870 
871 }
872 
873 
874 void
875 icmp6_print(netdissect_options *ndo,
876             const u_char *bp, u_int length, const u_char *bp2, int fragmented)
877 {
878 	const struct icmp6_hdr *dp;
879 	const struct ip6_hdr *ip;
880 	const struct ip6_hdr *oip;
881 	const struct udphdr *ouh;
882 	int dport;
883 	const u_char *ep;
884 	u_int prot;
885 
886 	dp = (const struct icmp6_hdr *)bp;
887 	ip = (const struct ip6_hdr *)bp2;
888 	oip = (const struct ip6_hdr *)(dp + 1);
889 	/* 'ep' points to the end of available data. */
890 	ep = ndo->ndo_snapend;
891 
892 	ND_TCHECK(dp->icmp6_cksum);
893 
894 	if (ndo->ndo_vflag && !fragmented) {
895 		uint16_t sum, udp_sum;
896 
897 		if (ND_TTEST2(bp[0], length)) {
898 			udp_sum = EXTRACT_16BITS(&dp->icmp6_cksum);
899 			sum = icmp6_cksum(ndo, ip, dp, length);
900 			if (sum != 0)
901 				ND_PRINT((ndo,"[bad icmp6 cksum 0x%04x -> 0x%04x!] ",
902                                                 udp_sum,
903                                                 in_cksum_shouldbe(udp_sum, sum)));
904 			else
905 				ND_PRINT((ndo,"[icmp6 sum ok] "));
906 		}
907 	}
908 
909         ND_PRINT((ndo,"ICMP6, %s", tok2str(icmp6_type_values,"unknown icmp6 type (%u)",dp->icmp6_type)));
910 
911         /* display cosmetics: print the packet length for printer that use the vflag now */
912         if (ndo->ndo_vflag && (dp->icmp6_type == ND_ROUTER_SOLICIT ||
913                       dp->icmp6_type == ND_ROUTER_ADVERT ||
914                       dp->icmp6_type == ND_NEIGHBOR_ADVERT ||
915                       dp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
916                       dp->icmp6_type == ND_REDIRECT ||
917                       dp->icmp6_type == ICMP6_HADISCOV_REPLY ||
918                       dp->icmp6_type == ICMP6_MOBILEPREFIX_ADVERT ))
919                 ND_PRINT((ndo,", length %u", length));
920 
921 	switch (dp->icmp6_type) {
922 	case ICMP6_DST_UNREACH:
923 		ND_TCHECK(oip->ip6_dst);
924                 ND_PRINT((ndo,", %s", tok2str(icmp6_dst_unreach_code_values,"unknown unreach code (%u)",dp->icmp6_code)));
925 		switch (dp->icmp6_code) {
926 
927 		case ICMP6_DST_UNREACH_NOROUTE: /* fall through */
928 		case ICMP6_DST_UNREACH_ADMIN:
929 		case ICMP6_DST_UNREACH_ADDR:
930                         ND_PRINT((ndo," %s",ip6addr_string(ndo, &oip->ip6_dst)));
931                         break;
932 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
933 			ND_PRINT((ndo," %s, source address %s",
934 			       ip6addr_string(ndo, &oip->ip6_dst),
935                                   ip6addr_string(ndo, &oip->ip6_src)));
936 			break;
937 		case ICMP6_DST_UNREACH_NOPORT:
938 			if ((ouh = get_upperlayer(ndo, (const u_char *)oip, &prot))
939 			    == NULL)
940 				goto trunc;
941 
942 			dport = EXTRACT_16BITS(&ouh->uh_dport);
943 			switch (prot) {
944 			case IPPROTO_TCP:
945 				ND_PRINT((ndo,", %s tcp port %s",
946 					ip6addr_string(ndo, &oip->ip6_dst),
947                                           tcpport_string(ndo, dport)));
948 				break;
949 			case IPPROTO_UDP:
950 				ND_PRINT((ndo,", %s udp port %s",
951 					ip6addr_string(ndo, &oip->ip6_dst),
952                                           udpport_string(ndo, dport)));
953 				break;
954 			default:
955 				ND_PRINT((ndo,", %s protocol %d port %d unreachable",
956 					ip6addr_string(ndo, &oip->ip6_dst),
957                                           oip->ip6_nxt, dport));
958 				break;
959 			}
960 			break;
961 		default:
962                   if (ndo->ndo_vflag <= 1) {
963                     print_unknown_data(ndo, bp,"\n\t",length);
964                     return;
965                   }
966                     break;
967 		}
968 		break;
969 	case ICMP6_PACKET_TOO_BIG:
970 		ND_TCHECK(dp->icmp6_mtu);
971 		ND_PRINT((ndo,", mtu %u", EXTRACT_32BITS(&dp->icmp6_mtu)));
972 		break;
973 	case ICMP6_TIME_EXCEEDED:
974 		ND_TCHECK(oip->ip6_dst);
975 		switch (dp->icmp6_code) {
976 		case ICMP6_TIME_EXCEED_TRANSIT:
977 			ND_PRINT((ndo," for %s",
978                                   ip6addr_string(ndo, &oip->ip6_dst)));
979 			break;
980 		case ICMP6_TIME_EXCEED_REASSEMBLY:
981 			ND_PRINT((ndo," (reassembly)"));
982 			break;
983 		default:
984                         ND_PRINT((ndo,", unknown code (%u)", dp->icmp6_code));
985 			break;
986 		}
987 		break;
988 	case ICMP6_PARAM_PROB:
989 		ND_TCHECK(oip->ip6_dst);
990 		switch (dp->icmp6_code) {
991 		case ICMP6_PARAMPROB_HEADER:
992                         ND_PRINT((ndo,", erroneous - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr)));
993                         break;
994 		case ICMP6_PARAMPROB_NEXTHEADER:
995                         ND_PRINT((ndo,", next header - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr)));
996                         break;
997 		case ICMP6_PARAMPROB_OPTION:
998                         ND_PRINT((ndo,", option - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr)));
999                         break;
1000 		default:
1001                         ND_PRINT((ndo,", code-#%d",
1002                                   dp->icmp6_code));
1003                         break;
1004 		}
1005 		break;
1006 	case ICMP6_ECHO_REQUEST:
1007 	case ICMP6_ECHO_REPLY:
1008                 ND_TCHECK(dp->icmp6_seq);
1009                 ND_PRINT((ndo,", seq %u", EXTRACT_16BITS(&dp->icmp6_seq)));
1010 		break;
1011 	case ICMP6_MEMBERSHIP_QUERY:
1012 		if (length == MLD_MINLEN) {
1013 			mld6_print(ndo, (const u_char *)dp);
1014 		} else if (length >= MLDV2_MINLEN) {
1015 			ND_PRINT((ndo," v2"));
1016 			mldv2_query_print(ndo, (const u_char *)dp, length);
1017 		} else {
1018                         ND_PRINT((ndo," unknown-version (len %u) ", length));
1019 		}
1020 		break;
1021 	case ICMP6_MEMBERSHIP_REPORT:
1022 		mld6_print(ndo, (const u_char *)dp);
1023 		break;
1024 	case ICMP6_MEMBERSHIP_REDUCTION:
1025 		mld6_print(ndo, (const u_char *)dp);
1026 		break;
1027 	case ND_ROUTER_SOLICIT:
1028 #define RTSOLLEN 8
1029 		if (ndo->ndo_vflag) {
1030 			icmp6_opt_print(ndo, (const u_char *)dp + RTSOLLEN,
1031 					length - RTSOLLEN);
1032 		}
1033 		break;
1034 	case ND_ROUTER_ADVERT:
1035 #define RTADVLEN 16
1036 		if (ndo->ndo_vflag) {
1037 			const struct nd_router_advert *p;
1038 
1039 			p = (const struct nd_router_advert *)dp;
1040 			ND_TCHECK(p->nd_ra_retransmit);
1041 			ND_PRINT((ndo,"\n\thop limit %u, Flags [%s]" \
1042                                   ", pref %s, router lifetime %us, reachable time %ums, retrans timer %ums",
1043                                   (u_int)p->nd_ra_curhoplimit,
1044                                   bittok2str(icmp6_opt_ra_flag_values,"none",(p->nd_ra_flags_reserved)),
1045                                   get_rtpref(p->nd_ra_flags_reserved),
1046                                   EXTRACT_16BITS(&p->nd_ra_router_lifetime),
1047                                   EXTRACT_32BITS(&p->nd_ra_reachable),
1048                                   EXTRACT_32BITS(&p->nd_ra_retransmit)));
1049 
1050 			icmp6_opt_print(ndo, (const u_char *)dp + RTADVLEN,
1051 					length - RTADVLEN);
1052 		}
1053 		break;
1054 	case ND_NEIGHBOR_SOLICIT:
1055 	    {
1056 		const struct nd_neighbor_solicit *p;
1057 		p = (const struct nd_neighbor_solicit *)dp;
1058 		ND_TCHECK(p->nd_ns_target);
1059 		ND_PRINT((ndo,", who has %s", ip6addr_string(ndo, &p->nd_ns_target)));
1060 		if (ndo->ndo_vflag) {
1061 #define NDSOLLEN 24
1062 			icmp6_opt_print(ndo, (const u_char *)dp + NDSOLLEN,
1063 					length - NDSOLLEN);
1064 		}
1065 	    }
1066 		break;
1067 	case ND_NEIGHBOR_ADVERT:
1068 	    {
1069 		const struct nd_neighbor_advert *p;
1070 
1071 		p = (const struct nd_neighbor_advert *)dp;
1072 		ND_TCHECK(p->nd_na_target);
1073 		ND_PRINT((ndo,", tgt is %s",
1074                           ip6addr_string(ndo, &p->nd_na_target)));
1075 		if (ndo->ndo_vflag) {
1076                         ND_PRINT((ndo,", Flags [%s]",
1077                                   bittok2str(icmp6_nd_na_flag_values,
1078                                              "none",
1079                                              EXTRACT_32BITS(&p->nd_na_flags_reserved))));
1080 #define NDADVLEN 24
1081 			icmp6_opt_print(ndo, (const u_char *)dp + NDADVLEN,
1082 					length - NDADVLEN);
1083 #undef NDADVLEN
1084 		}
1085 	    }
1086 		break;
1087 	case ND_REDIRECT:
1088 #define RDR(i) ((const struct nd_redirect *)(i))
1089                          ND_TCHECK(RDR(dp)->nd_rd_dst);
1090                          ND_PRINT((ndo,", %s", ip6addr_string(ndo, &RDR(dp)->nd_rd_dst)));
1091 		ND_TCHECK(RDR(dp)->nd_rd_target);
1092 		ND_PRINT((ndo," to %s",
1093                           ip6addr_string(ndo, &RDR(dp)->nd_rd_target)));
1094 #define REDIRECTLEN 40
1095 		if (ndo->ndo_vflag) {
1096 			icmp6_opt_print(ndo, (const u_char *)dp + REDIRECTLEN,
1097 					length - REDIRECTLEN);
1098 		}
1099 		break;
1100 #undef REDIRECTLEN
1101 #undef RDR
1102 	case ICMP6_ROUTER_RENUMBERING:
1103 		icmp6_rrenum_print(ndo, bp, ep);
1104 		break;
1105 	case ICMP6_NI_QUERY:
1106 	case ICMP6_NI_REPLY:
1107 		icmp6_nodeinfo_print(ndo, length, bp, ep);
1108 		break;
1109 	case IND_SOLICIT:
1110 	case IND_ADVERT:
1111 		break;
1112 	case ICMP6_V2_MEMBERSHIP_REPORT:
1113 		mldv2_report_print(ndo, (const u_char *) dp, length);
1114 		break;
1115 	case ICMP6_MOBILEPREFIX_SOLICIT: /* fall through */
1116 	case ICMP6_HADISCOV_REQUEST:
1117                 ND_TCHECK(dp->icmp6_data16[0]);
1118                 ND_PRINT((ndo,", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0])));
1119                 break;
1120 	case ICMP6_HADISCOV_REPLY:
1121 		if (ndo->ndo_vflag) {
1122 			const struct in6_addr *in6;
1123 			const u_char *cp;
1124 
1125 			ND_TCHECK(dp->icmp6_data16[0]);
1126 			ND_PRINT((ndo,", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0])));
1127 			cp = (const u_char *)dp + length;
1128 			in6 = (const struct in6_addr *)(dp + 1);
1129 			for (; (const u_char *)in6 < cp; in6++) {
1130 				ND_TCHECK(*in6);
1131 				ND_PRINT((ndo,", %s", ip6addr_string(ndo, in6)));
1132 			}
1133 		}
1134 		break;
1135 	case ICMP6_MOBILEPREFIX_ADVERT:
1136 		if (ndo->ndo_vflag) {
1137 			ND_TCHECK(dp->icmp6_data16[0]);
1138 			ND_PRINT((ndo,", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0])));
1139 			ND_TCHECK(dp->icmp6_data16[1]);
1140 			if (dp->icmp6_data16[1] & 0xc0)
1141 				ND_PRINT((ndo," "));
1142 			if (dp->icmp6_data16[1] & 0x80)
1143 				ND_PRINT((ndo,"M"));
1144 			if (dp->icmp6_data16[1] & 0x40)
1145 				ND_PRINT((ndo,"O"));
1146 #define MPADVLEN 8
1147 			icmp6_opt_print(ndo, (const u_char *)dp + MPADVLEN,
1148 					length - MPADVLEN);
1149 		}
1150 		break;
1151         case ND_RPL_MESSAGE:
1152                 /* plus 4, because struct icmp6_hdr contains 4 bytes of icmp payload */
1153                 rpl_print(ndo, dp, &dp->icmp6_data8[0], length-sizeof(struct icmp6_hdr)+4);
1154                 break;
1155 	default:
1156                 ND_PRINT((ndo,", length %u", length));
1157                 if (ndo->ndo_vflag <= 1)
1158                         print_unknown_data(ndo, bp,"\n\t", length);
1159                 return;
1160         }
1161         if (!ndo->ndo_vflag)
1162                 ND_PRINT((ndo,", length %u", length));
1163 	return;
1164 trunc:
1165 	ND_PRINT((ndo, "%s", icmp6_tstr));
1166 }
1167 
1168 static const struct udphdr *
1169 get_upperlayer(netdissect_options *ndo, const u_char *bp, u_int *prot)
1170 {
1171 	const u_char *ep;
1172 	const struct ip6_hdr *ip6 = (const struct ip6_hdr *)bp;
1173 	const struct udphdr *uh;
1174 	const struct ip6_hbh *hbh;
1175 	const struct ip6_frag *fragh;
1176 	const struct ah *ah;
1177 	u_int nh;
1178 	int hlen;
1179 
1180 	/* 'ep' points to the end of available data. */
1181 	ep = ndo->ndo_snapend;
1182 
1183 	if (!ND_TTEST(ip6->ip6_nxt))
1184 		return NULL;
1185 
1186 	nh = ip6->ip6_nxt;
1187 	hlen = sizeof(struct ip6_hdr);
1188 
1189 	while (bp < ep) {
1190 		bp += hlen;
1191 
1192 		switch(nh) {
1193 		case IPPROTO_UDP:
1194 		case IPPROTO_TCP:
1195 			uh = (const struct udphdr *)bp;
1196 			if (ND_TTEST(uh->uh_dport)) {
1197 				*prot = nh;
1198 				return(uh);
1199 			}
1200 			else
1201 				return(NULL);
1202 			/* NOTREACHED */
1203 
1204 		case IPPROTO_HOPOPTS:
1205 		case IPPROTO_DSTOPTS:
1206 		case IPPROTO_ROUTING:
1207 			hbh = (const struct ip6_hbh *)bp;
1208 			if (!ND_TTEST(hbh->ip6h_len))
1209 				return(NULL);
1210 			nh = hbh->ip6h_nxt;
1211 			hlen = (hbh->ip6h_len + 1) << 3;
1212 			break;
1213 
1214 		case IPPROTO_FRAGMENT: /* this should be odd, but try anyway */
1215 			fragh = (const struct ip6_frag *)bp;
1216 			if (!ND_TTEST(fragh->ip6f_offlg))
1217 				return(NULL);
1218 			/* fragments with non-zero offset are meaningless */
1219 			if ((EXTRACT_16BITS(&fragh->ip6f_offlg) & IP6F_OFF_MASK) != 0)
1220 				return(NULL);
1221 			nh = fragh->ip6f_nxt;
1222 			hlen = sizeof(struct ip6_frag);
1223 			break;
1224 
1225 		case IPPROTO_AH:
1226 			ah = (const struct ah *)bp;
1227 			if (!ND_TTEST(ah->ah_len))
1228 				return(NULL);
1229 			nh = ah->ah_nxt;
1230 			hlen = (ah->ah_len + 2) << 2;
1231 			break;
1232 
1233 		default:	/* unknown or undecodable header */
1234 			*prot = nh; /* meaningless, but set here anyway */
1235 			return(NULL);
1236 		}
1237 	}
1238 
1239 	return(NULL);		/* should be notreached, though */
1240 }
1241 
1242 static void
1243 icmp6_opt_print(netdissect_options *ndo, const u_char *bp, int resid)
1244 {
1245 	const struct nd_opt_hdr *op;
1246 	const struct nd_opt_prefix_info *opp;
1247 	const struct nd_opt_mtu *opm;
1248 	const struct nd_opt_rdnss *oprd;
1249 	const struct nd_opt_dnssl *opds;
1250 	const struct nd_opt_advinterval *opa;
1251 	const struct nd_opt_homeagent_info *oph;
1252 	const struct nd_opt_route_info *opri;
1253 	const u_char *cp, *ep, *domp;
1254 	struct in6_addr in6;
1255 	const struct in6_addr *in6p;
1256 	size_t l;
1257 	u_int i;
1258 
1259 #define ECHECK(var) if ((const u_char *)&(var) > ep - sizeof(var)) return
1260 
1261 	cp = bp;
1262 	/* 'ep' points to the end of available data. */
1263 	ep = ndo->ndo_snapend;
1264 
1265 	while (cp < ep) {
1266 		op = (const struct nd_opt_hdr *)cp;
1267 
1268 		ECHECK(op->nd_opt_len);
1269 		if (resid <= 0)
1270 			return;
1271 		if (op->nd_opt_len == 0)
1272 			goto trunc;
1273 		if (cp + (op->nd_opt_len << 3) > ep)
1274 			goto trunc;
1275 
1276                 ND_PRINT((ndo,"\n\t  %s option (%u), length %u (%u): ",
1277                           tok2str(icmp6_opt_values, "unknown", op->nd_opt_type),
1278                           op->nd_opt_type,
1279                           op->nd_opt_len << 3,
1280                           op->nd_opt_len));
1281 
1282 		switch (op->nd_opt_type) {
1283 		case ND_OPT_SOURCE_LINKADDR:
1284 			l = (op->nd_opt_len << 3) - 2;
1285 			print_lladdr(ndo, cp + 2, l);
1286 			break;
1287 		case ND_OPT_TARGET_LINKADDR:
1288 			l = (op->nd_opt_len << 3) - 2;
1289 			print_lladdr(ndo, cp + 2, l);
1290 			break;
1291 		case ND_OPT_PREFIX_INFORMATION:
1292 			opp = (const struct nd_opt_prefix_info *)op;
1293 			ND_TCHECK(opp->nd_opt_pi_prefix);
1294                         ND_PRINT((ndo,"%s/%u%s, Flags [%s], valid time %s",
1295                                   ip6addr_string(ndo, &opp->nd_opt_pi_prefix),
1296                                   opp->nd_opt_pi_prefix_len,
1297                                   (op->nd_opt_len != 4) ? "badlen" : "",
1298                                   bittok2str(icmp6_opt_pi_flag_values, "none", opp->nd_opt_pi_flags_reserved),
1299                                   get_lifetime(EXTRACT_32BITS(&opp->nd_opt_pi_valid_time))));
1300                         ND_PRINT((ndo,", pref. time %s", get_lifetime(EXTRACT_32BITS(&opp->nd_opt_pi_preferred_time))));
1301 			break;
1302 		case ND_OPT_REDIRECTED_HEADER:
1303                         print_unknown_data(ndo, bp,"\n\t    ",op->nd_opt_len<<3);
1304 			/* xxx */
1305 			break;
1306 		case ND_OPT_MTU:
1307 			opm = (const struct nd_opt_mtu *)op;
1308 			ND_TCHECK(opm->nd_opt_mtu_mtu);
1309 			ND_PRINT((ndo," %u%s",
1310                                EXTRACT_32BITS(&opm->nd_opt_mtu_mtu),
1311                                   (op->nd_opt_len != 1) ? "bad option length" : "" ));
1312                         break;
1313 		case ND_OPT_RDNSS:
1314 			oprd = (const struct nd_opt_rdnss *)op;
1315 			l = (op->nd_opt_len - 1) / 2;
1316 			ND_PRINT((ndo," lifetime %us,",
1317                                   EXTRACT_32BITS(&oprd->nd_opt_rdnss_lifetime)));
1318 			for (i = 0; i < l; i++) {
1319 				ND_TCHECK(oprd->nd_opt_rdnss_addr[i]);
1320 				ND_PRINT((ndo," addr: %s",
1321                                           ip6addr_string(ndo, &oprd->nd_opt_rdnss_addr[i])));
1322 			}
1323 			break;
1324 		case ND_OPT_DNSSL:
1325 			opds = (const struct nd_opt_dnssl *)op;
1326 			ND_PRINT((ndo," lifetime %us, domain(s):",
1327                                   EXTRACT_32BITS(&opds->nd_opt_dnssl_lifetime)));
1328 			domp = cp + 8; /* domain names, variable-sized, RFC1035-encoded */
1329 			while (domp < cp + (op->nd_opt_len << 3) && *domp != '\0')
1330 			{
1331 				ND_PRINT((ndo, " "));
1332 				if ((domp = ns_nprint (ndo, domp, bp)) == NULL)
1333 					goto trunc;
1334 			}
1335 			break;
1336 		case ND_OPT_ADVINTERVAL:
1337 			opa = (const struct nd_opt_advinterval *)op;
1338 			ND_TCHECK(opa->nd_opt_adv_interval);
1339 			ND_PRINT((ndo," %ums", EXTRACT_32BITS(&opa->nd_opt_adv_interval)));
1340 			break;
1341                 case ND_OPT_HOMEAGENT_INFO:
1342 			oph = (const struct nd_opt_homeagent_info *)op;
1343 			ND_TCHECK(oph->nd_opt_hai_lifetime);
1344 			ND_PRINT((ndo," preference %u, lifetime %u",
1345                                   EXTRACT_16BITS(&oph->nd_opt_hai_preference),
1346                                   EXTRACT_16BITS(&oph->nd_opt_hai_lifetime)));
1347 			break;
1348 		case ND_OPT_ROUTE_INFO:
1349 			opri = (const struct nd_opt_route_info *)op;
1350 			ND_TCHECK(opri->nd_opt_rti_lifetime);
1351 			memset(&in6, 0, sizeof(in6));
1352 			in6p = (const struct in6_addr *)(opri + 1);
1353 			switch (op->nd_opt_len) {
1354 			case 1:
1355 				break;
1356 			case 2:
1357 				ND_TCHECK2(*in6p, 8);
1358 				memcpy(&in6, opri + 1, 8);
1359 				break;
1360 			case 3:
1361 				ND_TCHECK(*in6p);
1362 				memcpy(&in6, opri + 1, sizeof(in6));
1363 				break;
1364 			default:
1365 				goto trunc;
1366 			}
1367 			ND_PRINT((ndo," %s/%u", ip6addr_string(ndo, &in6),
1368                                   opri->nd_opt_rti_prefixlen));
1369 			ND_PRINT((ndo,", pref=%s", get_rtpref(opri->nd_opt_rti_flags)));
1370 			ND_PRINT((ndo,", lifetime=%s",
1371                                   get_lifetime(EXTRACT_32BITS(&opri->nd_opt_rti_lifetime))));
1372 			break;
1373 		default:
1374                         if (ndo->ndo_vflag <= 1) {
1375                                 print_unknown_data(ndo,cp+2,"\n\t  ", (op->nd_opt_len << 3) - 2); /* skip option header */
1376                             return;
1377                         }
1378                         break;
1379 		}
1380                 /* do we want to see an additional hexdump ? */
1381                 if (ndo->ndo_vflag> 1)
1382                         print_unknown_data(ndo, cp+2,"\n\t    ", (op->nd_opt_len << 3) - 2); /* skip option header */
1383 
1384 		cp += op->nd_opt_len << 3;
1385 		resid -= op->nd_opt_len << 3;
1386 	}
1387 	return;
1388 
1389 trunc:
1390 	ND_PRINT((ndo, "%s", icmp6_tstr));
1391 	return;
1392 #undef ECHECK
1393 }
1394 
1395 static void
1396 mld6_print(netdissect_options *ndo, const u_char *bp)
1397 {
1398 	const struct mld6_hdr *mp = (const struct mld6_hdr *)bp;
1399 	const u_char *ep;
1400 
1401 	/* 'ep' points to the end of available data. */
1402 	ep = ndo->ndo_snapend;
1403 
1404 	if ((const u_char *)mp + sizeof(*mp) > ep)
1405 		return;
1406 
1407 	ND_PRINT((ndo,"max resp delay: %d ", EXTRACT_16BITS(&mp->mld6_maxdelay)));
1408 	ND_PRINT((ndo,"addr: %s", ip6addr_string(ndo, &mp->mld6_addr)));
1409 }
1410 
1411 static void
1412 mldv2_report_print(netdissect_options *ndo, const u_char *bp, u_int len)
1413 {
1414     const struct icmp6_hdr *icp = (const struct icmp6_hdr *) bp;
1415     u_int group, nsrcs, ngroups;
1416     u_int i, j;
1417 
1418     /* Minimum len is 8 */
1419     if (len < 8) {
1420             ND_PRINT((ndo," [invalid len %d]", len));
1421             return;
1422     }
1423 
1424     ND_TCHECK(icp->icmp6_data16[1]);
1425     ngroups = EXTRACT_16BITS(&icp->icmp6_data16[1]);
1426     ND_PRINT((ndo,", %d group record(s)", ngroups));
1427     if (ndo->ndo_vflag > 0) {
1428 	/* Print the group records */
1429 	group = 8;
1430         for (i = 0; i < ngroups; i++) {
1431 	    /* type(1) + auxlen(1) + numsrc(2) + grp(16) */
1432 	    if (len < group + 20) {
1433                     ND_PRINT((ndo," [invalid number of groups]"));
1434                     return;
1435 	    }
1436             ND_TCHECK2(bp[group + 4], sizeof(struct in6_addr));
1437             ND_PRINT((ndo," [gaddr %s", ip6addr_string(ndo, &bp[group + 4])));
1438 	    ND_PRINT((ndo," %s", tok2str(mldv2report2str, " [v2-report-#%d]",
1439                                          bp[group])));
1440             nsrcs = (bp[group + 2] << 8) + bp[group + 3];
1441 	    /* Check the number of sources and print them */
1442 	    if (len < group + 20 + (nsrcs * sizeof(struct in6_addr))) {
1443                     ND_PRINT((ndo," [invalid number of sources %d]", nsrcs));
1444                     return;
1445 	    }
1446             if (ndo->ndo_vflag == 1)
1447                     ND_PRINT((ndo,", %d source(s)", nsrcs));
1448             else {
1449 		/* Print the sources */
1450                     ND_PRINT((ndo," {"));
1451                 for (j = 0; j < nsrcs; j++) {
1452                     ND_TCHECK2(bp[group + 20 + j * sizeof(struct in6_addr)],
1453                             sizeof(struct in6_addr));
1454 		    ND_PRINT((ndo," %s", ip6addr_string(ndo, &bp[group + 20 + j * sizeof(struct in6_addr)])));
1455 		}
1456                 ND_PRINT((ndo," }"));
1457             }
1458 	    /* Next group record */
1459             group += 20 + nsrcs * sizeof(struct in6_addr);
1460 	    ND_PRINT((ndo,"]"));
1461         }
1462     }
1463     return;
1464 trunc:
1465     ND_PRINT((ndo, "%s", mldv2_tstr));
1466     return;
1467 }
1468 
1469 static void
1470 mldv2_query_print(netdissect_options *ndo, const u_char *bp, u_int len)
1471 {
1472     const struct icmp6_hdr *icp = (const struct icmp6_hdr *) bp;
1473     u_int mrc;
1474     int mrt, qqi;
1475     u_int nsrcs;
1476     register u_int i;
1477 
1478     /* Minimum len is 28 */
1479     if (len < 28) {
1480             ND_PRINT((ndo," [invalid len %d]", len));
1481 	return;
1482     }
1483     ND_TCHECK(icp->icmp6_data16[0]);
1484     mrc = EXTRACT_16BITS(&icp->icmp6_data16[0]);
1485     if (mrc < 32768) {
1486 	mrt = mrc;
1487     } else {
1488         mrt = ((mrc & 0x0fff) | 0x1000) << (((mrc & 0x7000) >> 12) + 3);
1489     }
1490     if (ndo->ndo_vflag) {
1491             ND_PRINT((ndo," [max resp delay=%d]", mrt));
1492     }
1493     ND_TCHECK2(bp[8], sizeof(struct in6_addr));
1494     ND_PRINT((ndo," [gaddr %s", ip6addr_string(ndo, &bp[8])));
1495 
1496     if (ndo->ndo_vflag) {
1497         ND_TCHECK(bp[25]);
1498 	if (bp[24] & 0x08) {
1499 		ND_PRINT((ndo," sflag"));
1500 	}
1501 	if (bp[24] & 0x07) {
1502 		ND_PRINT((ndo," robustness=%d", bp[24] & 0x07));
1503 	}
1504 	if (bp[25] < 128) {
1505 		qqi = bp[25];
1506 	} else {
1507 		qqi = ((bp[25] & 0x0f) | 0x10) << (((bp[25] & 0x70) >> 4) + 3);
1508 	}
1509 	ND_PRINT((ndo," qqi=%d", qqi));
1510     }
1511 
1512     ND_TCHECK2(bp[26], 2);
1513     nsrcs = EXTRACT_16BITS(&bp[26]);
1514     if (nsrcs > 0) {
1515 	if (len < 28 + nsrcs * sizeof(struct in6_addr))
1516 	    ND_PRINT((ndo," [invalid number of sources]"));
1517 	else if (ndo->ndo_vflag > 1) {
1518 	    ND_PRINT((ndo," {"));
1519 	    for (i = 0; i < nsrcs; i++) {
1520 		ND_TCHECK2(bp[28 + i * sizeof(struct in6_addr)],
1521                         sizeof(struct in6_addr));
1522 		ND_PRINT((ndo," %s", ip6addr_string(ndo, &bp[28 + i * sizeof(struct in6_addr)])));
1523 	    }
1524 	    ND_PRINT((ndo," }"));
1525 	} else
1526                 ND_PRINT((ndo,", %d source(s)", nsrcs));
1527     }
1528     ND_PRINT((ndo,"]"));
1529     return;
1530 trunc:
1531     ND_PRINT((ndo, "%s", mldv2_tstr));
1532     return;
1533 }
1534 
1535 static void
1536 dnsname_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
1537 {
1538 	int i;
1539 
1540 	/* DNS name decoding - no decompression */
1541 	ND_PRINT((ndo,", \""));
1542 	while (cp < ep) {
1543 		i = *cp++;
1544 		if (i) {
1545 			if (i > ep - cp) {
1546 				ND_PRINT((ndo,"???"));
1547 				break;
1548 			}
1549 			while (i-- && cp < ep) {
1550 				safeputchar(ndo, *cp);
1551 				cp++;
1552 			}
1553 			if (cp + 1 < ep && *cp)
1554 				ND_PRINT((ndo,"."));
1555 		} else {
1556 			if (cp == ep) {
1557 				/* FQDN */
1558 				ND_PRINT((ndo,"."));
1559 			} else if (cp + 1 == ep && *cp == '\0') {
1560 				/* truncated */
1561 			} else {
1562 				/* invalid */
1563 				ND_PRINT((ndo,"???"));
1564 			}
1565 			break;
1566 		}
1567 	}
1568 	ND_PRINT((ndo,"\""));
1569 }
1570 
1571 static void
1572 icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp, const u_char *ep)
1573 {
1574 	const struct icmp6_nodeinfo *ni6;
1575 	const struct icmp6_hdr *dp;
1576 	const u_char *cp;
1577 	size_t siz, i;
1578 	int needcomma;
1579 
1580 	if (ep < bp)
1581 		return;
1582 	dp = (const struct icmp6_hdr *)bp;
1583 	ni6 = (const struct icmp6_nodeinfo *)bp;
1584 	siz = ep - bp;
1585 
1586 	switch (ni6->ni_type) {
1587 	case ICMP6_NI_QUERY:
1588 		if (siz == sizeof(*dp) + 4) {
1589 			/* KAME who-are-you */
1590 			ND_PRINT((ndo," who-are-you request"));
1591 			break;
1592 		}
1593 		ND_PRINT((ndo," node information query"));
1594 
1595 		ND_TCHECK2(*dp, sizeof(*ni6));
1596 		ni6 = (const struct icmp6_nodeinfo *)dp;
1597 		ND_PRINT((ndo," ("));	/*)*/
1598 		switch (EXTRACT_16BITS(&ni6->ni_qtype)) {
1599 		case NI_QTYPE_NOOP:
1600 			ND_PRINT((ndo,"noop"));
1601 			break;
1602 		case NI_QTYPE_SUPTYPES:
1603 			ND_PRINT((ndo,"supported qtypes"));
1604 			i = EXTRACT_16BITS(&ni6->ni_flags);
1605 			if (i)
1606 				ND_PRINT((ndo," [%s]", (i & 0x01) ? "C" : ""));
1607 			break;
1608 		case NI_QTYPE_FQDN:
1609 			ND_PRINT((ndo,"DNS name"));
1610 			break;
1611 		case NI_QTYPE_NODEADDR:
1612 			ND_PRINT((ndo,"node addresses"));
1613 			i = ni6->ni_flags;
1614 			if (!i)
1615 				break;
1616 			/* NI_NODEADDR_FLAG_TRUNCATE undefined for query */
1617 			ND_PRINT((ndo," [%s%s%s%s%s%s]",
1618 			    (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
1619 			    (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
1620 			    (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
1621 			    (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
1622 			    (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
1623 			    (i & NI_NODEADDR_FLAG_ALL) ? "A" : ""));
1624 			break;
1625 		default:
1626 			ND_PRINT((ndo,"unknown"));
1627 			break;
1628 		}
1629 
1630 		if (ni6->ni_qtype == NI_QTYPE_NOOP ||
1631 		    ni6->ni_qtype == NI_QTYPE_SUPTYPES) {
1632 			if (siz != sizeof(*ni6))
1633 				if (ndo->ndo_vflag)
1634 					ND_PRINT((ndo,", invalid len"));
1635 			/*(*/
1636 			ND_PRINT((ndo,")"));
1637 			break;
1638 		}
1639 
1640 
1641 		/* XXX backward compat, icmp-name-lookup-03 */
1642 		if (siz == sizeof(*ni6)) {
1643 			ND_PRINT((ndo,", 03 draft"));
1644 			/*(*/
1645 			ND_PRINT((ndo,")"));
1646 			break;
1647 		}
1648 
1649 		switch (ni6->ni_code) {
1650 		case ICMP6_NI_SUBJ_IPV6:
1651 			if (!ND_TTEST2(*dp,
1652 			    sizeof(*ni6) + sizeof(struct in6_addr)))
1653 				break;
1654 			if (siz != sizeof(*ni6) + sizeof(struct in6_addr)) {
1655 				if (ndo->ndo_vflag)
1656 					ND_PRINT((ndo,", invalid subject len"));
1657 				break;
1658 			}
1659 			ND_PRINT((ndo,", subject=%s",
1660                                   ip6addr_string(ndo, ni6 + 1)));
1661 			break;
1662 		case ICMP6_NI_SUBJ_FQDN:
1663 			ND_PRINT((ndo,", subject=DNS name"));
1664 			cp = (const u_char *)(ni6 + 1);
1665 			if (cp[0] == ep - cp - 1) {
1666 				/* icmp-name-lookup-03, pascal string */
1667 				if (ndo->ndo_vflag)
1668 					ND_PRINT((ndo,", 03 draft"));
1669 				cp++;
1670 				ND_PRINT((ndo,", \""));
1671 				while (cp < ep) {
1672 					safeputchar(ndo, *cp);
1673 					cp++;
1674 				}
1675 				ND_PRINT((ndo,"\""));
1676 			} else
1677 				dnsname_print(ndo, cp, ep);
1678 			break;
1679 		case ICMP6_NI_SUBJ_IPV4:
1680 			if (!ND_TTEST2(*dp, sizeof(*ni6) + sizeof(struct in_addr)))
1681 				break;
1682 			if (siz != sizeof(*ni6) + sizeof(struct in_addr)) {
1683 				if (ndo->ndo_vflag)
1684 					ND_PRINT((ndo,", invalid subject len"));
1685 				break;
1686 			}
1687 			ND_PRINT((ndo,", subject=%s",
1688                                   ipaddr_string(ndo, ni6 + 1)));
1689 			break;
1690 		default:
1691 			ND_PRINT((ndo,", unknown subject"));
1692 			break;
1693 		}
1694 
1695 		/*(*/
1696 		ND_PRINT((ndo,")"));
1697 		break;
1698 
1699 	case ICMP6_NI_REPLY:
1700 		if (icmp6len > siz) {
1701 			ND_PRINT((ndo,"[|icmp6: node information reply]"));
1702 			break;
1703 		}
1704 
1705 		needcomma = 0;
1706 
1707 		ND_TCHECK2(*dp, sizeof(*ni6));
1708 		ni6 = (const struct icmp6_nodeinfo *)dp;
1709 		ND_PRINT((ndo," node information reply"));
1710 		ND_PRINT((ndo," ("));	/*)*/
1711 		switch (ni6->ni_code) {
1712 		case ICMP6_NI_SUCCESS:
1713 			if (ndo->ndo_vflag) {
1714 				ND_PRINT((ndo,"success"));
1715 				needcomma++;
1716 			}
1717 			break;
1718 		case ICMP6_NI_REFUSED:
1719 			ND_PRINT((ndo,"refused"));
1720 			needcomma++;
1721 			if (siz != sizeof(*ni6))
1722 				if (ndo->ndo_vflag)
1723 					ND_PRINT((ndo,", invalid length"));
1724 			break;
1725 		case ICMP6_NI_UNKNOWN:
1726 			ND_PRINT((ndo,"unknown"));
1727 			needcomma++;
1728 			if (siz != sizeof(*ni6))
1729 				if (ndo->ndo_vflag)
1730 					ND_PRINT((ndo,", invalid length"));
1731 			break;
1732 		}
1733 
1734 		if (ni6->ni_code != ICMP6_NI_SUCCESS) {
1735 			/*(*/
1736 			ND_PRINT((ndo,")"));
1737 			break;
1738 		}
1739 
1740 		switch (EXTRACT_16BITS(&ni6->ni_qtype)) {
1741 		case NI_QTYPE_NOOP:
1742 			if (needcomma)
1743 				ND_PRINT((ndo,", "));
1744 			ND_PRINT((ndo,"noop"));
1745 			if (siz != sizeof(*ni6))
1746 				if (ndo->ndo_vflag)
1747 					ND_PRINT((ndo,", invalid length"));
1748 			break;
1749 		case NI_QTYPE_SUPTYPES:
1750 			if (needcomma)
1751 				ND_PRINT((ndo,", "));
1752 			ND_PRINT((ndo,"supported qtypes"));
1753 			i = EXTRACT_16BITS(&ni6->ni_flags);
1754 			if (i)
1755 				ND_PRINT((ndo," [%s]", (i & 0x01) ? "C" : ""));
1756 			break;
1757 		case NI_QTYPE_FQDN:
1758 			if (needcomma)
1759 				ND_PRINT((ndo,", "));
1760 			ND_PRINT((ndo,"DNS name"));
1761 			cp = (const u_char *)(ni6 + 1) + 4;
1762 			ND_TCHECK(cp[0]);
1763 			if (cp[0] == ep - cp - 1) {
1764 				/* icmp-name-lookup-03, pascal string */
1765 				if (ndo->ndo_vflag)
1766 					ND_PRINT((ndo,", 03 draft"));
1767 				cp++;
1768 				ND_PRINT((ndo,", \""));
1769 				while (cp < ep) {
1770 					safeputchar(ndo, *cp);
1771 					cp++;
1772 				}
1773 				ND_PRINT((ndo,"\""));
1774 			} else
1775 				dnsname_print(ndo, cp, ep);
1776 			if ((EXTRACT_16BITS(&ni6->ni_flags) & 0x01) != 0)
1777 				ND_PRINT((ndo," [TTL=%u]", EXTRACT_32BITS(ni6 + 1)));
1778 			break;
1779 		case NI_QTYPE_NODEADDR:
1780 			if (needcomma)
1781 				ND_PRINT((ndo,", "));
1782 			ND_PRINT((ndo,"node addresses"));
1783 			i = sizeof(*ni6);
1784 			while (i < siz) {
1785 				if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
1786 					break;
1787 				ND_PRINT((ndo," %s", ip6addr_string(ndo, bp + i)));
1788 				i += sizeof(struct in6_addr);
1789 				ND_PRINT((ndo,"(%d)", (int32_t)EXTRACT_32BITS(bp + i)));
1790 				i += sizeof(int32_t);
1791 			}
1792 			i = ni6->ni_flags;
1793 			if (!i)
1794 				break;
1795 			ND_PRINT((ndo," [%s%s%s%s%s%s%s]",
1796                                   (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
1797                                   (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
1798                                   (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
1799                                   (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
1800                                   (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
1801                                   (i & NI_NODEADDR_FLAG_ALL) ? "A" : "",
1802                                   (i & NI_NODEADDR_FLAG_TRUNCATE) ? "T" : ""));
1803 			break;
1804 		default:
1805 			if (needcomma)
1806 				ND_PRINT((ndo,", "));
1807 			ND_PRINT((ndo,"unknown"));
1808 			break;
1809 		}
1810 
1811 		/*(*/
1812 		ND_PRINT((ndo,")"));
1813 		break;
1814 	}
1815 	return;
1816 
1817 trunc:
1818 	ND_PRINT((ndo, "%s", icmp6_tstr));
1819 }
1820 
1821 static void
1822 icmp6_rrenum_print(netdissect_options *ndo, const u_char *bp, const u_char *ep)
1823 {
1824 	const struct icmp6_router_renum *rr6;
1825 	const char *cp;
1826 	const struct rr_pco_match *match;
1827 	const struct rr_pco_use *use;
1828 	char hbuf[NI_MAXHOST];
1829 	int n;
1830 
1831 	if (ep < bp)
1832 		return;
1833 	rr6 = (const struct icmp6_router_renum *)bp;
1834 	cp = (const char *)(rr6 + 1);
1835 
1836 	ND_TCHECK(rr6->rr_reserved);
1837 	switch (rr6->rr_code) {
1838 	case ICMP6_ROUTER_RENUMBERING_COMMAND:
1839 		ND_PRINT((ndo,"router renum: command"));
1840 		break;
1841 	case ICMP6_ROUTER_RENUMBERING_RESULT:
1842 		ND_PRINT((ndo,"router renum: result"));
1843 		break;
1844 	case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
1845 		ND_PRINT((ndo,"router renum: sequence number reset"));
1846 		break;
1847 	default:
1848 		ND_PRINT((ndo,"router renum: code-#%d", rr6->rr_code));
1849 		break;
1850 	}
1851 
1852         ND_PRINT((ndo,", seq=%u", EXTRACT_32BITS(&rr6->rr_seqnum)));
1853 
1854 	if (ndo->ndo_vflag) {
1855 #define F(x, y)	((rr6->rr_flags) & (x) ? (y) : "")
1856 		ND_PRINT((ndo,"["));	/*]*/
1857 		if (rr6->rr_flags) {
1858 			ND_PRINT((ndo,"%s%s%s%s%s,", F(ICMP6_RR_FLAGS_TEST, "T"),
1859                                   F(ICMP6_RR_FLAGS_REQRESULT, "R"),
1860                                   F(ICMP6_RR_FLAGS_FORCEAPPLY, "A"),
1861                                   F(ICMP6_RR_FLAGS_SPECSITE, "S"),
1862                                   F(ICMP6_RR_FLAGS_PREVDONE, "P")));
1863 		}
1864                 ND_PRINT((ndo,"seg=%u,", rr6->rr_segnum));
1865                 ND_PRINT((ndo,"maxdelay=%u", EXTRACT_16BITS(&rr6->rr_maxdelay)));
1866 		if (rr6->rr_reserved)
1867 			ND_PRINT((ndo,"rsvd=0x%x", EXTRACT_32BITS(&rr6->rr_reserved)));
1868 		/*[*/
1869 		ND_PRINT((ndo,"]"));
1870 #undef F
1871 	}
1872 
1873 	if (rr6->rr_code == ICMP6_ROUTER_RENUMBERING_COMMAND) {
1874 		match = (const struct rr_pco_match *)cp;
1875 		cp = (const char *)(match + 1);
1876 
1877 		ND_TCHECK(match->rpm_prefix);
1878 
1879 		if (ndo->ndo_vflag > 1)
1880 			ND_PRINT((ndo,"\n\t"));
1881 		else
1882 			ND_PRINT((ndo," "));
1883 		ND_PRINT((ndo,"match("));	/*)*/
1884 		switch (match->rpm_code) {
1885 		case RPM_PCO_ADD:	ND_PRINT((ndo,"add")); break;
1886 		case RPM_PCO_CHANGE:	ND_PRINT((ndo,"change")); break;
1887 		case RPM_PCO_SETGLOBAL:	ND_PRINT((ndo,"setglobal")); break;
1888 		default:		ND_PRINT((ndo,"#%u", match->rpm_code)); break;
1889 		}
1890 
1891 		if (ndo->ndo_vflag) {
1892 			ND_PRINT((ndo,",ord=%u", match->rpm_ordinal));
1893 			ND_PRINT((ndo,",min=%u", match->rpm_minlen));
1894 			ND_PRINT((ndo,",max=%u", match->rpm_maxlen));
1895 		}
1896 		if (addrtostr6(&match->rpm_prefix, hbuf, sizeof(hbuf)))
1897 			ND_PRINT((ndo,",%s/%u", hbuf, match->rpm_matchlen));
1898 		else
1899 			ND_PRINT((ndo,",?/%u", match->rpm_matchlen));
1900 		/*(*/
1901 		ND_PRINT((ndo,")"));
1902 
1903 		n = match->rpm_len - 3;
1904 		if (n % 4)
1905 			goto trunc;
1906 		n /= 4;
1907 		while (n-- > 0) {
1908 			use = (const struct rr_pco_use *)cp;
1909 			cp = (const char *)(use + 1);
1910 
1911 			ND_TCHECK(use->rpu_prefix);
1912 
1913 			if (ndo->ndo_vflag > 1)
1914 				ND_PRINT((ndo,"\n\t"));
1915 			else
1916 				ND_PRINT((ndo," "));
1917 			ND_PRINT((ndo,"use("));	/*)*/
1918 			if (use->rpu_flags) {
1919 #define F(x, y)	((use->rpu_flags) & (x) ? (y) : "")
1920 				ND_PRINT((ndo,"%s%s,",
1921                                           F(ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME, "V"),
1922                                           F(ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME, "P")));
1923 #undef F
1924 			}
1925 			if (ndo->ndo_vflag) {
1926 				ND_PRINT((ndo,"mask=0x%x,", use->rpu_ramask));
1927 				ND_PRINT((ndo,"raflags=0x%x,", use->rpu_raflags));
1928 				if (~use->rpu_vltime == 0)
1929 					ND_PRINT((ndo,"vltime=infty,"));
1930 				else
1931 					ND_PRINT((ndo,"vltime=%u,",
1932                                                   EXTRACT_32BITS(&use->rpu_vltime)));
1933 				if (~use->rpu_pltime == 0)
1934 					ND_PRINT((ndo,"pltime=infty,"));
1935 				else
1936 					ND_PRINT((ndo,"pltime=%u,",
1937                                                   EXTRACT_32BITS(&use->rpu_pltime)));
1938 			}
1939 			if (addrtostr6(&use->rpu_prefix, hbuf, sizeof(hbuf)))
1940 				ND_PRINT((ndo,"%s/%u/%u", hbuf, use->rpu_uselen,
1941                                           use->rpu_keeplen));
1942 			else
1943 				ND_PRINT((ndo,"?/%u/%u", use->rpu_uselen,
1944                                           use->rpu_keeplen));
1945 			/*(*/
1946                         ND_PRINT((ndo,")"));
1947 		}
1948 	}
1949 
1950 	return;
1951 
1952 trunc:
1953 	ND_PRINT((ndo, "%s", icmp6_tstr));
1954 }
1955 
1956 /*
1957  * Local Variables:
1958  * c-style: whitesmith
1959  * c-basic-offset: 8
1960  * End:
1961  */
1962