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