xref: /freebsd/usr.sbin/ndp/ndp.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1984, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Sun Microsystems, Inc.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *	This product includes software developed by the University of
50  *	California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * Based on:
70  * "@(#) Copyright (c) 1984, 1993\n\
71  *	The Regents of the University of California.  All rights reserved.\n";
72  *
73  * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
74  */
75 
76 /*
77  * ndp - display, set, delete and flush neighbor cache
78  */
79 
80 
81 #include <sys/param.h>
82 #include <sys/file.h>
83 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
86 #include <sys/time.h>
87 #include <sys/queue.h>
88 
89 #include <net/if.h>
90 #include <net/if_var.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
97 
98 #include <netinet/icmp6.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet6/nd6.h>
101 
102 #include <arpa/inet.h>
103 
104 #include <netdb.h>
105 #include <errno.h>
106 #include <nlist.h>
107 #include <stdio.h>
108 #include <string.h>
109 #include <paths.h>
110 #include <err.h>
111 #include <stdlib.h>
112 #include <fcntl.h>
113 #include <unistd.h>
114 #include <err.h>
115 #include "gmt2local.h"
116 
117 /* packing rule for routing socket */
118 #define ROUNDUP(a) \
119 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
120 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
121 
122 static pid_t pid;
123 static int nflag;
124 static int tflag;
125 static int32_t thiszone;	/* time difference with gmt */
126 static int s = -1;
127 static int repeat = 0;
128 
129 char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
130 char host_buf[NI_MAXHOST];		/* getnameinfo() */
131 char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
132 
133 int main __P((int, char **));
134 int file __P((char *));
135 void getsocket __P((void));
136 int set __P((int, char **));
137 void get __P((char *));
138 int delete __P((char *));
139 void dump __P((struct in6_addr *, int));
140 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
141 static char *ether_str __P((struct sockaddr_dl *));
142 int ndp_ether_aton __P((char *, u_char *));
143 void usage __P((void));
144 int rtmsg __P((int));
145 void ifinfo __P((char *, int, char **));
146 void rtrlist __P((void));
147 void plist __P((void));
148 void pfx_flush __P((void));
149 void rtrlist __P((void));
150 void rtr_flush __P((void));
151 void harmonize_rtr __P((void));
152 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
153 static void getdefif __P((void));
154 static void setdefif __P((char *));
155 #endif
156 static char *sec2str __P((time_t));
157 static char *ether_str __P((struct sockaddr_dl *));
158 static void ts_print __P((const struct timeval *));
159 
160 #ifdef ICMPV6CTL_ND6_DRLIST
161 static char *rtpref_str[] = {
162 	"medium",		/* 00 */
163 	"high",			/* 01 */
164 	"rsv",			/* 10 */
165 	"low"			/* 11 */
166 };
167 #endif
168 
169 int mode = 0;
170 char *arg = NULL;
171 
172 int
173 main(argc, argv)
174 	int argc;
175 	char **argv;
176 {
177 	int ch;
178 
179 	pid = getpid();
180 	thiszone = gmt2local(0);
181 	while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
182 		switch (ch) {
183 		case 'a':
184 		case 'c':
185 		case 'p':
186 		case 'r':
187 		case 'H':
188 		case 'P':
189 		case 'R':
190 		case 's':
191 		case 'I':
192 			if (mode) {
193 				usage();
194 				/*NOTREACHED*/
195 			}
196 			mode = ch;
197 			arg = NULL;
198 			break;
199 		case 'd':
200 		case 'f':
201 		case 'i' :
202 			if (mode) {
203 				usage();
204 				/*NOTREACHED*/
205 			}
206 			mode = ch;
207 			arg = optarg;
208 			break;
209 		case 'n':
210 			nflag = 1;
211 			break;
212 		case 't':
213 			tflag = 1;
214 			break;
215 		case 'A':
216 			if (mode) {
217 				usage();
218 				/*NOTREACHED*/
219 			}
220 			mode = 'a';
221 			repeat = atoi(optarg);
222 			if (repeat < 0) {
223 				usage();
224 				/*NOTREACHED*/
225 			}
226 			break;
227 		default:
228 			usage();
229 		}
230 
231 	argc -= optind;
232 	argv += optind;
233 
234 	switch (mode) {
235 	case 'a':
236 	case 'c':
237 		if (argc != 0) {
238 			usage();
239 			/*NOTREACHED*/
240 		}
241 		dump(0, mode == 'c');
242 		break;
243 	case 'd':
244 		if (argc != 0) {
245 			usage();
246 			/*NOTREACHED*/
247 		}
248 		delete(arg);
249 		break;
250 	case 'I':
251 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
252 		if (argc > 1) {
253 			usage();
254 			/*NOTREACHED*/
255 		} else if (argc == 1) {
256 			if (strcmp(*argv, "delete") == 0 ||
257 			    if_nametoindex(*argv))
258 				setdefif(*argv);
259 			else
260 				errx(1, "invalid interface %s", *argv);
261 		}
262 		getdefif(); /* always call it to print the result */
263 		break;
264 #else
265 		errx(1, "not supported yet");
266 		/*NOTREACHED*/
267 #endif
268 	case 'p':
269 		if (argc != 0) {
270 			usage();
271 			/*NOTREACHED*/
272 		}
273 		plist();
274 		break;
275 	case 'i':
276 		ifinfo(arg, argc, argv);
277 		break;
278 	case 'r':
279 		if (argc != 0) {
280 			usage();
281 			/*NOTREACHED*/
282 		}
283 		rtrlist();
284 		break;
285 	case 's':
286 		if (argc < 2 || argc > 4)
287 			usage();
288 		exit(set(argc, argv) ? 1 : 0);
289 	case 'H':
290 		if (argc != 0) {
291 			usage();
292 			/*NOTREACHED*/
293 		}
294 		harmonize_rtr();
295 		break;
296 	case 'P':
297 		if (argc != 0) {
298 			usage();
299 			/*NOTREACHED*/
300 		}
301 		pfx_flush();
302 		break;
303 	case 'R':
304 		if (argc != 0) {
305 			usage();
306 			/*NOTREACHED*/
307 		}
308 		rtr_flush();
309 		break;
310 	case 0:
311 		if (argc != 1) {
312 			usage();
313 			/*NOTREACHED*/
314 		}
315 		get(argv[0]);
316 		break;
317 	}
318 	exit(0);
319 }
320 
321 /*
322  * Process a file to set standard ndp entries
323  */
324 int
325 file(name)
326 	char *name;
327 {
328 	FILE *fp;
329 	int i, retval;
330 	char line[100], arg[5][50], *args[5];
331 
332 	if ((fp = fopen(name, "r")) == NULL) {
333 		fprintf(stderr, "ndp: cannot open %s\n", name);
334 		exit(1);
335 	}
336 	args[0] = &arg[0][0];
337 	args[1] = &arg[1][0];
338 	args[2] = &arg[2][0];
339 	args[3] = &arg[3][0];
340 	args[4] = &arg[4][0];
341 	retval = 0;
342 	while (fgets(line, 100, fp) != NULL) {
343 		i = sscanf(line, "%49s %49s %49s %49s %49s",
344 		    arg[0], arg[1], arg[2], arg[3], arg[4]);
345 		if (i < 2) {
346 			fprintf(stderr, "ndp: bad line: %s\n", line);
347 			retval = 1;
348 			continue;
349 		}
350 		if (set(i, args))
351 			retval = 1;
352 	}
353 	fclose(fp);
354 	return (retval);
355 }
356 
357 void
358 getsocket()
359 {
360 	if (s < 0) {
361 		s = socket(PF_ROUTE, SOCK_RAW, 0);
362 		if (s < 0) {
363 			err(1, "socket");
364 			/* NOTREACHED */
365 		}
366 	}
367 }
368 
369 struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
370 struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
371 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
372 int	expire_time, flags, found_entry;
373 struct	{
374 	struct	rt_msghdr m_rtm;
375 	char	m_space[512];
376 }	m_rtmsg;
377 
378 /*
379  * Set an individual neighbor cache entry
380  */
381 int
382 set(argc, argv)
383 	int argc;
384 	char **argv;
385 {
386 	register struct sockaddr_in6 *sin = &sin_m;
387 	register struct sockaddr_dl *sdl;
388 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
389 	struct addrinfo hints, *res;
390 	int gai_error;
391 	u_char *ea;
392 	char *host = argv[0], *eaddr = argv[1];
393 
394 	getsocket();
395 	argc -= 2;
396 	argv += 2;
397 	sdl_m = blank_sdl;
398 	sin_m = blank_sin;
399 
400 	bzero(&hints, sizeof(hints));
401 	hints.ai_family = AF_INET6;
402 	gai_error = getaddrinfo(host, NULL, &hints, &res);
403 	if (gai_error) {
404 		fprintf(stderr, "ndp: %s: %s\n", host,
405 			gai_strerror(gai_error));
406 		return 1;
407 	}
408 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
409 #ifdef __KAME__
410 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
411 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
412 		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
413 	}
414 #endif
415 	ea = (u_char *)LLADDR(&sdl_m);
416 	if (ndp_ether_aton(eaddr, ea) == 0)
417 		sdl_m.sdl_alen = 6;
418 	flags = expire_time = 0;
419 	while (argc-- > 0) {
420 		if (strncmp(argv[0], "temp", 4) == 0) {
421 			struct timeval time;
422 
423 			gettimeofday(&time, 0);
424 			expire_time = time.tv_sec + 20 * 60;
425 		} else if (strncmp(argv[0], "proxy", 5) == 0)
426 			flags |= RTF_ANNOUNCE;
427 		argv++;
428 	}
429 	if (rtmsg(RTM_GET) < 0) {
430 		errx(1, "RTM_GET(%s) failed", host);
431 		/* NOTREACHED */
432 	}
433 	sin = (struct sockaddr_in6 *)(rtm + 1);
434 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
435 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
436 		if (sdl->sdl_family == AF_LINK &&
437 		    (rtm->rtm_flags & RTF_LLINFO) &&
438 		    !(rtm->rtm_flags & RTF_GATEWAY)) {
439 			switch (sdl->sdl_type) {
440 			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
441 			case IFT_ISO88024: case IFT_ISO88025:
442 				goto overwrite;
443 			}
444 		}
445 		/*
446 		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
447 		 */
448 		fprintf(stderr, "set: cannot configure a new entry\n");
449 		return 1;
450 	}
451 
452 overwrite:
453 	if (sdl->sdl_family != AF_LINK) {
454 		printf("cannot intuit interface index and type for %s\n", host);
455 		return (1);
456 	}
457 	sdl_m.sdl_type = sdl->sdl_type;
458 	sdl_m.sdl_index = sdl->sdl_index;
459 	return (rtmsg(RTM_ADD));
460 }
461 
462 /*
463  * Display an individual neighbor cache entry
464  */
465 void
466 get(host)
467 	char *host;
468 {
469 	struct sockaddr_in6 *sin = &sin_m;
470 	struct addrinfo hints, *res;
471 	int gai_error;
472 
473 	sin_m = blank_sin;
474 	bzero(&hints, sizeof(hints));
475 	hints.ai_family = AF_INET6;
476 	gai_error = getaddrinfo(host, NULL, &hints, &res);
477 	if (gai_error) {
478 		fprintf(stderr, "ndp: %s: %s\n", host,
479 		    gai_strerror(gai_error));
480 		return;
481 	}
482 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
483 #ifdef __KAME__
484 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
485 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
486 		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
487 	}
488 #endif
489 	dump(&sin->sin6_addr, 0);
490 	if (found_entry == 0) {
491 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
492 		    sizeof(host_buf), NULL ,0,
493 		    (nflag ? NI_NUMERICHOST : 0));
494 		printf("%s (%s) -- no entry\n", host, host_buf);
495 		exit(1);
496 	}
497 }
498 
499 /*
500  * Delete a neighbor cache entry
501  */
502 int
503 delete(host)
504 	char *host;
505 {
506 	struct sockaddr_in6 *sin = &sin_m;
507 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
508 	struct sockaddr_dl *sdl;
509 	struct addrinfo hints, *res;
510 	int gai_error;
511 
512 	getsocket();
513 	sin_m = blank_sin;
514 
515 	bzero(&hints, sizeof(hints));
516 	hints.ai_family = AF_INET6;
517 	gai_error = getaddrinfo(host, NULL, &hints, &res);
518 	if (gai_error) {
519 		fprintf(stderr, "ndp: %s: %s\n", host,
520 		    gai_strerror(gai_error));
521 		return 1;
522 	}
523 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
524 #ifdef __KAME__
525 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
526 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
527 		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
528 	}
529 #endif
530 	if (rtmsg(RTM_GET) < 0) {
531 		errx(1, "RTM_GET(%s) failed", host);
532 		/* NOTREACHED */
533 	}
534 	sin = (struct sockaddr_in6 *)(rtm + 1);
535 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
536 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
537 		if (sdl->sdl_family == AF_LINK &&
538 		    (rtm->rtm_flags & RTF_LLINFO) &&
539 		    !(rtm->rtm_flags & RTF_GATEWAY)) {
540 			goto delete;
541 		}
542 		/*
543 		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
544 		 */
545 		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
546 		return 1;
547 	}
548 
549 delete:
550 	if (sdl->sdl_family != AF_LINK) {
551 		printf("cannot locate %s\n", host);
552 		return (1);
553 	}
554 	if (rtmsg(RTM_DELETE) == 0) {
555 		struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
556 
557 #ifdef __KAME__
558 		if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
559 			s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
560 			*(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
561 		}
562 #endif
563 		getnameinfo((struct sockaddr *)&s6,
564 		    s6.sin6_len, host_buf,
565 		    sizeof(host_buf), NULL, 0,
566 		    (nflag ? NI_NUMERICHOST : 0));
567 		printf("%s (%s) deleted\n", host, host_buf);
568 	}
569 
570 	return 0;
571 }
572 
573 #define W_ADDR	36
574 #define W_LL	17
575 #define W_IF	6
576 
577 /*
578  * Dump the entire neighbor cache
579  */
580 void
581 dump(addr, cflag)
582 	struct in6_addr *addr;
583 	int cflag;
584 {
585 	int mib[6];
586 	size_t needed;
587 	char *lim, *buf, *next;
588 	struct rt_msghdr *rtm;
589 	struct sockaddr_in6 *sin;
590 	struct sockaddr_dl *sdl;
591 	extern int h_errno;
592 	struct in6_nbrinfo *nbi;
593 	struct timeval time;
594 	int addrwidth;
595 	int llwidth;
596 	int ifwidth;
597 	char flgbuf[8];
598 	char *ifname;
599 
600 	/* Print header */
601 	if (!tflag && !cflag)
602 		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
603 		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
604 		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
605 
606 again:;
607 	mib[0] = CTL_NET;
608 	mib[1] = PF_ROUTE;
609 	mib[2] = 0;
610 	mib[3] = AF_INET6;
611 	mib[4] = NET_RT_FLAGS;
612 	mib[5] = RTF_LLINFO;
613 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
614 		err(1, "sysctl(PF_ROUTE estimate)");
615 	if (needed > 0) {
616 		if ((buf = malloc(needed)) == NULL)
617 			err(1, "malloc");
618 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
619 			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
620 		lim = buf + needed;
621 	} else
622 		buf = lim = NULL;
623 
624 	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
625 		int isrouter = 0, prbs = 0;
626 
627 		rtm = (struct rt_msghdr *)next;
628 		sin = (struct sockaddr_in6 *)(rtm + 1);
629 		sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
630 
631 		/*
632 		 * Some OSes can produce a route that has the LINK flag but
633 		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
634 		 * and BSD/OS, where xx is not the interface identifier on
635 		 * lo0).  Such routes entry would annoy getnbrinfo() below,
636 		 * so we skip them.
637 		 * XXX: such routes should have the GATEWAY flag, not the
638 		 * LINK flag.  However, there is rotten routing software
639 		 * that advertises all routes that have the GATEWAY flag.
640 		 * Thus, KAME kernel intentionally does not set the LINK flag.
641 		 * What is to be fixed is not ndp, but such routing software
642 		 * (and the kernel workaround)...
643 		 */
644 		if (sdl->sdl_family != AF_LINK)
645 			continue;
646 
647 		if (!(rtm->rtm_flags & RTF_HOST))
648 			continue;
649 
650 		if (addr) {
651 			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
652 				continue;
653 			found_entry = 1;
654 		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
655 			continue;
656 		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
657 		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
658 			/* XXX: should scope id be filled in the kernel? */
659 			if (sin->sin6_scope_id == 0)
660 				sin->sin6_scope_id = sdl->sdl_index;
661 #ifdef __KAME__
662 			/* KAME specific hack; removed the embedded id */
663 			*(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
664 #endif
665 		}
666 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
667 		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
668 		if (cflag) {
669 #ifdef RTF_WASCLONED
670 			if (rtm->rtm_flags & RTF_WASCLONED)
671 				delete(host_buf);
672 #elif defined(RTF_CLONED)
673 			if (rtm->rtm_flags & RTF_CLONED)
674 				delete(host_buf);
675 #else
676 			delete(host_buf);
677 #endif
678 			continue;
679 		}
680 		gettimeofday(&time, 0);
681 		if (tflag)
682 			ts_print(&time);
683 
684 		addrwidth = strlen(host_buf);
685 		if (addrwidth < W_ADDR)
686 			addrwidth = W_ADDR;
687 		llwidth = strlen(ether_str(sdl));
688 		if (W_ADDR + W_LL - addrwidth > llwidth)
689 			llwidth = W_ADDR + W_LL - addrwidth;
690 		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
691 		if (!ifname)
692 			ifname = "?";
693 		ifwidth = strlen(ifname);
694 		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
695 			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
696 
697 		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
698 		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
699 
700 		/* Print neighbor discovery specific informations */
701 		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
702 		if (nbi) {
703 			if (nbi->expire > time.tv_sec) {
704 				printf(" %-9.9s",
705 				    sec2str(nbi->expire - time.tv_sec));
706 			} else if (nbi->expire == 0)
707 				printf(" %-9.9s", "permanent");
708 			else
709 				printf(" %-9.9s", "expired");
710 
711 			switch (nbi->state) {
712 			case ND6_LLINFO_NOSTATE:
713 				 printf(" N");
714 				 break;
715 #ifdef ND6_LLINFO_WAITDELETE
716 			case ND6_LLINFO_WAITDELETE:
717 				 printf(" W");
718 				 break;
719 #endif
720 			case ND6_LLINFO_INCOMPLETE:
721 				 printf(" I");
722 				 break;
723 			case ND6_LLINFO_REACHABLE:
724 				 printf(" R");
725 				 break;
726 			case ND6_LLINFO_STALE:
727 				 printf(" S");
728 				 break;
729 			case ND6_LLINFO_DELAY:
730 				 printf(" D");
731 				 break;
732 			case ND6_LLINFO_PROBE:
733 				 printf(" P");
734 				 break;
735 			default:
736 				 printf(" ?");
737 				 break;
738 			}
739 
740 			isrouter = nbi->isrouter;
741 			prbs = nbi->asked;
742 		} else {
743 			warnx("failed to get neighbor information");
744 			printf("  ");
745 		}
746 
747 		/*
748 		 * other flags. R: router, P: proxy, W: ??
749 		 */
750 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
751 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
752 			    isrouter ? "R" : "",
753 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
754 		} else {
755 			sin = (struct sockaddr_in6 *)
756 			    (sdl->sdl_len + (char *)sdl);
757 #if 0	/* W and P are mystery even for us */
758 			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
759 			    isrouter ? "R" : "",
760 			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
761 			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
762 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
763 #else
764 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
765 			    isrouter ? "R" : "",
766 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
767 #endif
768 		}
769 		printf(" %s", flgbuf);
770 
771 		if (prbs)
772 			printf(" %d", prbs);
773 
774 		printf("\n");
775 	}
776 	if (buf != NULL)
777 		free(buf);
778 
779 	if (repeat) {
780 		printf("\n");
781 		sleep(repeat);
782 		goto again;
783 	}
784 }
785 
786 static struct in6_nbrinfo *
787 getnbrinfo(addr, ifindex, warning)
788 	struct in6_addr *addr;
789 	int ifindex;
790 	int warning;
791 {
792 	static struct in6_nbrinfo nbi;
793 	int s;
794 
795 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
796 		err(1, "socket");
797 
798 	bzero(&nbi, sizeof(nbi));
799 	if_indextoname(ifindex, nbi.ifname);
800 	nbi.addr = *addr;
801 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
802 		if (warning)
803 			warn("ioctl(SIOCGNBRINFO_IN6)");
804 		close(s);
805 		return(NULL);
806 	}
807 
808 	close(s);
809 	return(&nbi);
810 }
811 
812 static char *
813 ether_str(sdl)
814 	struct sockaddr_dl *sdl;
815 {
816 	static char hbuf[NI_MAXHOST];
817 	u_char *cp;
818 
819 	if (sdl->sdl_alen) {
820 		cp = (u_char *)LLADDR(sdl);
821 		snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
822 		    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
823 	} else
824 		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
825 
826 	return(hbuf);
827 }
828 
829 int
830 ndp_ether_aton(a, n)
831 	char *a;
832 	u_char *n;
833 {
834 	int i, o[6];
835 
836 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
837 	    &o[3], &o[4], &o[5]);
838 	if (i != 6) {
839 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
840 		return (1);
841 	}
842 	for (i = 0; i < 6; i++)
843 		n[i] = o[i];
844 	return (0);
845 }
846 
847 void
848 usage()
849 {
850 	printf("usage: ndp [-nt] hostname\n");
851 	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
852 	printf("       ndp [-nt] -A wait\n");
853 	printf("       ndp [-nt] -d hostname\n");
854 	printf("       ndp [-nt] -f filename\n");
855 	printf("       ndp [-nt] -i interface [flags...]\n");
856 #ifdef SIOCSDEFIFACE_IN6
857 	printf("       ndp [-nt] -I [interface|delete]\n");
858 #endif
859 	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
860 	exit(1);
861 }
862 
863 int
864 rtmsg(cmd)
865 	int cmd;
866 {
867 	static int seq;
868 	int rlen;
869 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
870 	register char *cp = m_rtmsg.m_space;
871 	register int l;
872 
873 	errno = 0;
874 	if (cmd == RTM_DELETE)
875 		goto doit;
876 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
877 	rtm->rtm_flags = flags;
878 	rtm->rtm_version = RTM_VERSION;
879 
880 	switch (cmd) {
881 	default:
882 		fprintf(stderr, "ndp: internal wrong cmd\n");
883 		exit(1);
884 	case RTM_ADD:
885 		rtm->rtm_addrs |= RTA_GATEWAY;
886 		if (expire_time) {
887 			rtm->rtm_rmx.rmx_expire = expire_time;
888 			rtm->rtm_inits = RTV_EXPIRE;
889 		}
890 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
891 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
892 			rtm->rtm_flags &= ~RTF_HOST;
893 			rtm->rtm_flags |= RTA_NETMASK;
894 		}
895 		/* FALLTHROUGH */
896 	case RTM_GET:
897 		rtm->rtm_addrs |= RTA_DST;
898 	}
899 #define NEXTADDR(w, s) \
900 	if (rtm->rtm_addrs & (w)) { \
901 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
902 
903 	NEXTADDR(RTA_DST, sin_m);
904 	NEXTADDR(RTA_GATEWAY, sdl_m);
905 	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
906 	NEXTADDR(RTA_NETMASK, so_mask);
907 
908 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
909 doit:
910 	l = rtm->rtm_msglen;
911 	rtm->rtm_seq = ++seq;
912 	rtm->rtm_type = cmd;
913 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
914 		if (errno != ESRCH || cmd != RTM_DELETE) {
915 			err(1, "writing to routing socket");
916 			/* NOTREACHED */
917 		}
918 	}
919 	do {
920 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
921 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
922 	if (l < 0)
923 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
924 		    strerror(errno));
925 	return (0);
926 }
927 
928 void
929 ifinfo(ifname, argc, argv)
930 	char *ifname;
931 	int argc;
932 	char **argv;
933 {
934 	struct in6_ndireq nd;
935 	int i, s;
936 	u_int32_t newflags;
937 #ifdef IPV6CTL_USETEMPADDR
938 	u_int8_t nullbuf[8];
939 #endif
940 
941 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
942 		err(1, "socket");
943 		/* NOTREACHED */
944 	}
945 	bzero(&nd, sizeof(nd));
946 	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
947 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
948 		err(1, "ioctl(SIOCGIFINFO_IN6)");
949 		/* NOTREACHED */
950 	}
951 #define ND nd.ndi
952 	newflags = ND.flags;
953 	for (i = 0; i < argc; i++) {
954 		int clear = 0;
955 		char *cp = argv[i];
956 
957 		if (*cp == '-') {
958 			clear = 1;
959 			cp++;
960 		}
961 
962 #define SETFLAG(s, f) \
963 	do {\
964 		if (strcmp(cp, (s)) == 0) {\
965 			if (clear)\
966 				newflags &= ~(f);\
967 			else\
968 				newflags |= (f);\
969 		}\
970 	} while (0)
971 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
972 #ifdef ND6_IFF_ACCEPT_RTADV
973 		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
974 #endif
975 #ifdef ND6_IFF_PREFER_SOURCE
976 		SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
977 #endif
978 
979 		ND.flags = newflags;
980 		if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
981 			err(1, "ioctl(SIOCSIFINFO_FLAGS)");
982 			/* NOTREACHED */
983 		}
984 #undef SETFLAG
985 	}
986 
987 	if (!ND.initialized) {
988 		errx(1, "%s: not initialized yet", ifname);
989 		/* NOTREACHED */
990 	}
991 
992 	printf("linkmtu=%d", ND.linkmtu);
993 	printf(", maxmtu=%d", ND.maxmtu);
994 	printf(", curhlim=%d", ND.chlim);
995 	printf(", basereachable=%ds%dms",
996 	    ND.basereachable / 1000, ND.basereachable % 1000);
997 	printf(", reachable=%ds", ND.reachable);
998 	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
999 #ifdef IPV6CTL_USETEMPADDR
1000 	memset(nullbuf, 0, sizeof(nullbuf));
1001 	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1002 		int j;
1003 		u_int8_t *rbuf;
1004 
1005 		for (i = 0; i < 3; i++) {
1006 			switch (i) {
1007 			case 0:
1008 				printf("\nRandom seed(0): ");
1009 				rbuf = ND.randomseed0;
1010 				break;
1011 			case 1:
1012 				printf("\nRandom seed(1): ");
1013 				rbuf = ND.randomseed1;
1014 				break;
1015 			case 2:
1016 				printf("\nRandom ID:      ");
1017 				rbuf = ND.randomid;
1018 				break;
1019 			}
1020 			for (j = 0; j < 8; j++)
1021 				printf("%02x", rbuf[j]);
1022 		}
1023 	}
1024 #endif
1025 	if (ND.flags) {
1026 		printf("\nFlags: ");
1027 		if ((ND.flags & ND6_IFF_PERFORMNUD))
1028 			printf("nud ");
1029 #ifdef ND6_IFF_ACCEPT_RTADV
1030 		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1031 			printf("accept_rtadv ");
1032 #endif
1033 #ifdef ND6_IFF_PREFER_SOURCE
1034 		if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1035 			printf("prefer_source ");
1036 #endif
1037 	}
1038 	putc('\n', stdout);
1039 #undef ND
1040 
1041 	close(s);
1042 }
1043 
1044 #ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
1045 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
1046 #endif
1047 
1048 void
1049 rtrlist()
1050 {
1051 #ifdef ICMPV6CTL_ND6_DRLIST
1052 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1053 	char *buf;
1054 	struct in6_defrouter *p, *ep;
1055 	size_t l;
1056 	struct timeval time;
1057 
1058 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1059 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1060 		/*NOTREACHED*/
1061 	}
1062 	buf = malloc(l);
1063 	if (!buf) {
1064 		err(1, "malloc");
1065 		/*NOTREACHED*/
1066 	}
1067 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1068 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1069 		/*NOTREACHED*/
1070 	}
1071 
1072 	ep = (struct in6_defrouter *)(buf + l);
1073 	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1074 		int rtpref;
1075 
1076 		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1077 		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1078 		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1079 			strlcpy(host_buf, "?", sizeof(host_buf));
1080 
1081 		printf("%s if=%s", host_buf,
1082 		    if_indextoname(p->if_index, ifix_buf));
1083 		printf(", flags=%s%s",
1084 		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1085 		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1086 		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1087 		printf(", pref=%s", rtpref_str[rtpref]);
1088 
1089 		gettimeofday(&time, 0);
1090 		if (p->expire == 0)
1091 			printf(", expire=Never\n");
1092 		else
1093 			printf(", expire=%s\n",
1094 			    sec2str(p->expire - time.tv_sec));
1095 	}
1096 	free(buf);
1097 #else
1098 	struct in6_drlist dr;
1099 	int s, i;
1100 	struct timeval time;
1101 
1102 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1103 		err(1, "socket");
1104 		/* NOTREACHED */
1105 	}
1106 	bzero(&dr, sizeof(dr));
1107 	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1108 	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1109 		err(1, "ioctl(SIOCGDRLST_IN6)");
1110 		/* NOTREACHED */
1111 	}
1112 #define DR dr.defrouter[i]
1113 	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1114 		struct sockaddr_in6 sin6;
1115 
1116 		bzero(&sin6, sizeof(sin6));
1117 		sin6.sin6_family = AF_INET6;
1118 		sin6.sin6_len = sizeof(sin6);
1119 		sin6.sin6_addr = DR.rtaddr;
1120 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1121 		    sizeof(host_buf), NULL, 0,
1122 		    (nflag ? NI_NUMERICHOST : 0));
1123 
1124 		printf("%s if=%s", host_buf,
1125 		    if_indextoname(DR.if_index, ifix_buf));
1126 		printf(", flags=%s%s",
1127 		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1128 		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1129 		gettimeofday(&time, 0);
1130 		if (DR.expire == 0)
1131 			printf(", expire=Never\n");
1132 		else
1133 			printf(", expire=%s\n",
1134 			    sec2str(DR.expire - time.tv_sec));
1135 	}
1136 #undef DR
1137 	close(s);
1138 #endif
1139 }
1140 
1141 void
1142 plist()
1143 {
1144 #ifdef ICMPV6CTL_ND6_PRLIST
1145 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1146 	char *buf;
1147 	struct in6_prefix *p, *ep, *n;
1148 	struct sockaddr_in6 *advrtr;
1149 	size_t l;
1150 	struct timeval time;
1151 	const int niflags = NI_NUMERICHOST;
1152 	int ninflags = nflag ? NI_NUMERICHOST : 0;
1153 	char namebuf[NI_MAXHOST];
1154 
1155 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1156 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1157 		/*NOTREACHED*/
1158 	}
1159 	buf = malloc(l);
1160 	if (!buf) {
1161 		err(1, "malloc");
1162 		/*NOTREACHED*/
1163 	}
1164 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1165 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1166 		/*NOTREACHED*/
1167 	}
1168 
1169 	ep = (struct in6_prefix *)(buf + l);
1170 	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1171 		advrtr = (struct sockaddr_in6 *)(p + 1);
1172 		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1173 
1174 		if (getnameinfo((struct sockaddr *)&p->prefix,
1175 		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1176 		    NULL, 0, niflags) != 0)
1177 			strlcpy(namebuf, "?", sizeof(namebuf));
1178 		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1179 		    if_indextoname(p->if_index, ifix_buf));
1180 
1181 		gettimeofday(&time, 0);
1182 		/*
1183 		 * meaning of fields, especially flags, is very different
1184 		 * by origin.  notify the difference to the users.
1185 		 */
1186 		printf("flags=%s%s%s%s%s",
1187 		    p->raflags.onlink ? "L" : "",
1188 		    p->raflags.autonomous ? "A" : "",
1189 		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1190 		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1191 #ifdef NDPRF_HOME
1192 		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1193 #else
1194 		    ""
1195 #endif
1196 		    );
1197 		if (p->vltime == ND6_INFINITE_LIFETIME)
1198 			printf(" vltime=infinity");
1199 		else
1200 			printf(" vltime=%lu", (unsigned long)p->vltime);
1201 		if (p->pltime == ND6_INFINITE_LIFETIME)
1202 			printf(", pltime=infinity");
1203 		else
1204 			printf(", pltime=%lu", (unsigned long)p->pltime);
1205 		if (p->expire == 0)
1206 			printf(", expire=Never");
1207 		else if (p->expire >= time.tv_sec)
1208 			printf(", expire=%s",
1209 			    sec2str(p->expire - time.tv_sec));
1210 		else
1211 			printf(", expired");
1212 		printf(", ref=%d", p->refcnt);
1213 		printf("\n");
1214 		/*
1215 		 * "advertising router" list is meaningful only if the prefix
1216 		 * information is from RA.
1217 		 */
1218 		if (p->advrtrs) {
1219 			int j;
1220 			struct sockaddr_in6 *sin6;
1221 
1222 			sin6 = advrtr;
1223 			printf("  advertised by\n");
1224 			for (j = 0; j < p->advrtrs; j++) {
1225 				struct in6_nbrinfo *nbi;
1226 
1227 				if (getnameinfo((struct sockaddr *)sin6,
1228 				    sin6->sin6_len, namebuf, sizeof(namebuf),
1229 				    NULL, 0, ninflags) != 0)
1230 					strlcpy(namebuf, "?", sizeof(namebuf));
1231 				printf("    %s", namebuf);
1232 
1233 				nbi = getnbrinfo(&sin6->sin6_addr,
1234 				    p->if_index, 0);
1235 				if (nbi) {
1236 					switch (nbi->state) {
1237 					case ND6_LLINFO_REACHABLE:
1238 					case ND6_LLINFO_STALE:
1239 					case ND6_LLINFO_DELAY:
1240 					case ND6_LLINFO_PROBE:
1241 						printf(" (reachable)\n");
1242 						break;
1243 					default:
1244 						printf(" (unreachable)\n");
1245 					}
1246 				} else
1247 					printf(" (no neighbor state)\n");
1248 				sin6++;
1249 			}
1250 		} else
1251 			printf("  No advertising router\n");
1252 	}
1253 	free(buf);
1254 #else
1255 	struct in6_prlist pr;
1256 	int s, i;
1257 	struct timeval time;
1258 
1259 	gettimeofday(&time, 0);
1260 
1261 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1262 		err(1, "socket");
1263 		/* NOTREACHED */
1264 	}
1265 	bzero(&pr, sizeof(pr));
1266 	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1267 	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1268 		err(1, "ioctl(SIOCGPRLST_IN6)");
1269 		/* NOTREACHED */
1270 	}
1271 #define PR pr.prefix[i]
1272 	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1273 		struct sockaddr_in6 p6;
1274 		char namebuf[NI_MAXHOST];
1275 		int niflags;
1276 
1277 #ifdef NDPRF_ONLINK
1278 		p6 = PR.prefix;
1279 #else
1280 		memset(&p6, 0, sizeof(p6));
1281 		p6.sin6_family = AF_INET6;
1282 		p6.sin6_len = sizeof(p6);
1283 		p6.sin6_addr = PR.prefix;
1284 #endif
1285 
1286 		/*
1287 		 * copy link index to sin6_scope_id field.
1288 		 * XXX: KAME specific.
1289 		 */
1290 		if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1291 			u_int16_t linkid;
1292 
1293 			memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1294 			    sizeof(linkid));
1295 			linkid = ntohs(linkid);
1296 			p6.sin6_scope_id = linkid;
1297 			p6.sin6_addr.s6_addr[2] = 0;
1298 			p6.sin6_addr.s6_addr[3] = 0;
1299 		}
1300 
1301 		niflags = NI_NUMERICHOST;
1302 		if (getnameinfo((struct sockaddr *)&p6,
1303 		    sizeof(p6), namebuf, sizeof(namebuf),
1304 		    NULL, 0, niflags)) {
1305 			warnx("getnameinfo failed");
1306 			continue;
1307 		}
1308 		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1309 		    if_indextoname(PR.if_index, ifix_buf));
1310 
1311 		gettimeofday(&time, 0);
1312 		/*
1313 		 * meaning of fields, especially flags, is very different
1314 		 * by origin.  notify the difference to the users.
1315 		 */
1316 #if 0
1317 		printf("  %s",
1318 		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1319 #endif
1320 #ifdef NDPRF_ONLINK
1321 		printf("flags=%s%s%s%s%s",
1322 		    PR.raflags.onlink ? "L" : "",
1323 		    PR.raflags.autonomous ? "A" : "",
1324 		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1325 		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1326 #ifdef NDPRF_HOME
1327 		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1328 #else
1329 		    ""
1330 #endif
1331 		    );
1332 #else
1333 		printf("flags=%s%s",
1334 		    PR.raflags.onlink ? "L" : "",
1335 		    PR.raflags.autonomous ? "A" : "");
1336 #endif
1337 		if (PR.vltime == ND6_INFINITE_LIFETIME)
1338 			printf(" vltime=infinity");
1339 		else
1340 			printf(" vltime=%lu", PR.vltime);
1341 		if (PR.pltime == ND6_INFINITE_LIFETIME)
1342 			printf(", pltime=infinity");
1343 		else
1344 			printf(", pltime=%lu", PR.pltime);
1345 		if (PR.expire == 0)
1346 			printf(", expire=Never");
1347 		else if (PR.expire >= time.tv_sec)
1348 			printf(", expire=%s",
1349 			    sec2str(PR.expire - time.tv_sec));
1350 		else
1351 			printf(", expired");
1352 #ifdef NDPRF_ONLINK
1353 		printf(", ref=%d", PR.refcnt);
1354 #endif
1355 #if 0
1356 		switch (PR.origin) {
1357 		case PR_ORIG_RA:
1358 			printf(", origin=RA");
1359 			break;
1360 		case PR_ORIG_RR:
1361 			printf(", origin=RR");
1362 			break;
1363 		case PR_ORIG_STATIC:
1364 			printf(", origin=static");
1365 			break;
1366 		case PR_ORIG_KERNEL:
1367 			printf(", origin=kernel");
1368 			break;
1369 		default:
1370 			printf(", origin=?");
1371 			break;
1372 		}
1373 #endif
1374 		printf("\n");
1375 		/*
1376 		 * "advertising router" list is meaningful only if the prefix
1377 		 * information is from RA.
1378 		 */
1379 		if (0 &&	/* prefix origin is almost obsolted */
1380 		    PR.origin != PR_ORIG_RA)
1381 			;
1382 		else if (PR.advrtrs) {
1383 			int j;
1384 			printf("  advertised by\n");
1385 			for (j = 0; j < PR.advrtrs; j++) {
1386 				struct sockaddr_in6 sin6;
1387 				struct in6_nbrinfo *nbi;
1388 
1389 				bzero(&sin6, sizeof(sin6));
1390 				sin6.sin6_family = AF_INET6;
1391 				sin6.sin6_len = sizeof(sin6);
1392 				sin6.sin6_addr = PR.advrtr[j];
1393 				sin6.sin6_scope_id = PR.if_index; /* XXX */
1394 				getnameinfo((struct sockaddr *)&sin6,
1395 				    sin6.sin6_len, host_buf,
1396 				    sizeof(host_buf), NULL, 0,
1397 				    (nflag ? NI_NUMERICHOST : 0));
1398 				printf("    %s", host_buf);
1399 
1400 				nbi = getnbrinfo(&sin6.sin6_addr,
1401 				    PR.if_index, 0);
1402 				if (nbi) {
1403 					switch (nbi->state) {
1404 					case ND6_LLINFO_REACHABLE:
1405 					case ND6_LLINFO_STALE:
1406 					case ND6_LLINFO_DELAY:
1407 					case ND6_LLINFO_PROBE:
1408 						 printf(" (reachable)\n");
1409 						 break;
1410 					default:
1411 						 printf(" (unreachable)\n");
1412 					}
1413 				} else
1414 					printf(" (no neighbor state)\n");
1415 			}
1416 			if (PR.advrtrs > DRLSTSIZ)
1417 				printf("    and %d routers\n",
1418 				    PR.advrtrs - DRLSTSIZ);
1419 		} else
1420 			printf("  No advertising router\n");
1421 	}
1422 #undef PR
1423 	close(s);
1424 #endif
1425 }
1426 
1427 void
1428 pfx_flush()
1429 {
1430 	char dummyif[IFNAMSIZ+8];
1431 	int s;
1432 
1433 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1434 		err(1, "socket");
1435 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1436 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1437 		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1438 }
1439 
1440 void
1441 rtr_flush()
1442 {
1443 	char dummyif[IFNAMSIZ+8];
1444 	int s;
1445 
1446 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1447 		err(1, "socket");
1448 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1449 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1450 		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1451 
1452 	close(s);
1453 }
1454 
1455 void
1456 harmonize_rtr()
1457 {
1458 	char dummyif[IFNAMSIZ+8];
1459 	int s;
1460 
1461 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1462 		err(1, "socket");
1463 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1464 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1465 		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1466 
1467 	close(s);
1468 }
1469 
1470 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1471 static void
1472 setdefif(ifname)
1473 	char *ifname;
1474 {
1475 	struct in6_ndifreq ndifreq;
1476 	unsigned int ifindex;
1477 
1478 	if (strcasecmp(ifname, "delete") == 0)
1479 		ifindex = 0;
1480 	else {
1481 		if ((ifindex = if_nametoindex(ifname)) == 0)
1482 			err(1, "failed to resolve i/f index for %s", ifname);
1483 	}
1484 
1485 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1486 		err(1, "socket");
1487 
1488 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1489 	ndifreq.ifindex = ifindex;
1490 
1491 	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1492 		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1493 
1494 	close(s);
1495 }
1496 
1497 static void
1498 getdefif()
1499 {
1500 	struct in6_ndifreq ndifreq;
1501 	char ifname[IFNAMSIZ+8];
1502 
1503 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1504 		err(1, "socket");
1505 
1506 	memset(&ndifreq, 0, sizeof(ndifreq));
1507 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1508 
1509 	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1510 		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1511 
1512 	if (ndifreq.ifindex == 0)
1513 		printf("No default interface.\n");
1514 	else {
1515 		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1516 			err(1, "failed to resolve ifname for index %lu",
1517 			    ndifreq.ifindex);
1518 		printf("ND default interface = %s\n", ifname);
1519 	}
1520 
1521 	close(s);
1522 }
1523 #endif
1524 
1525 static char *
1526 sec2str(total)
1527 	time_t total;
1528 {
1529 	static char result[256];
1530 	int days, hours, mins, secs;
1531 	int first = 1;
1532 	char *p = result;
1533 	char *ep = &result[sizeof(result)];
1534 	int n;
1535 
1536 	days = total / 3600 / 24;
1537 	hours = (total / 3600) % 24;
1538 	mins = (total / 60) % 60;
1539 	secs = total % 60;
1540 
1541 	if (days) {
1542 		first = 0;
1543 		n = snprintf(p, ep - p, "%dd", days);
1544 		if (n < 0 || n >= ep - p)
1545 			return "?";
1546 		p += n;
1547 	}
1548 	if (!first || hours) {
1549 		first = 0;
1550 		n = snprintf(p, ep - p, "%dh", hours);
1551 		if (n < 0 || n >= ep - p)
1552 			return "?";
1553 		p += n;
1554 	}
1555 	if (!first || mins) {
1556 		first = 0;
1557 		n = snprintf(p, ep - p, "%dm", mins);
1558 		if (n < 0 || n >= ep - p)
1559 			return "?";
1560 		p += n;
1561 	}
1562 	snprintf(p, ep - p, "%ds", secs);
1563 
1564 	return(result);
1565 }
1566 
1567 /*
1568  * Print the timestamp
1569  * from tcpdump/util.c
1570  */
1571 static void
1572 ts_print(tvp)
1573 	const struct timeval *tvp;
1574 {
1575 	int s;
1576 
1577 	/* Default */
1578 	s = (tvp->tv_sec + thiszone) % 86400;
1579 	(void)printf("%02d:%02d:%02d.%06u ",
1580 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1581 }
1582