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