xref: /freebsd/usr.sbin/ndp/ndp.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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 		fflush(stdout);
782 		sleep(repeat);
783 		goto again;
784 	}
785 }
786 
787 static struct in6_nbrinfo *
788 getnbrinfo(addr, ifindex, warning)
789 	struct in6_addr *addr;
790 	int ifindex;
791 	int warning;
792 {
793 	static struct in6_nbrinfo nbi;
794 	int s;
795 
796 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
797 		err(1, "socket");
798 
799 	bzero(&nbi, sizeof(nbi));
800 	if_indextoname(ifindex, nbi.ifname);
801 	nbi.addr = *addr;
802 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
803 		if (warning)
804 			warn("ioctl(SIOCGNBRINFO_IN6)");
805 		close(s);
806 		return(NULL);
807 	}
808 
809 	close(s);
810 	return(&nbi);
811 }
812 
813 static char *
814 ether_str(sdl)
815 	struct sockaddr_dl *sdl;
816 {
817 	static char hbuf[NI_MAXHOST];
818 	u_char *cp;
819 
820 	if (sdl->sdl_alen) {
821 		cp = (u_char *)LLADDR(sdl);
822 		snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
823 		    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
824 	} else
825 		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
826 
827 	return(hbuf);
828 }
829 
830 int
831 ndp_ether_aton(a, n)
832 	char *a;
833 	u_char *n;
834 {
835 	int i, o[6];
836 
837 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
838 	    &o[3], &o[4], &o[5]);
839 	if (i != 6) {
840 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
841 		return (1);
842 	}
843 	for (i = 0; i < 6; i++)
844 		n[i] = o[i];
845 	return (0);
846 }
847 
848 void
849 usage()
850 {
851 	printf("usage: ndp [-nt] hostname\n");
852 	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
853 	printf("       ndp [-nt] -A wait\n");
854 	printf("       ndp [-nt] -d hostname\n");
855 	printf("       ndp [-nt] -f filename\n");
856 	printf("       ndp [-nt] -i interface [flags...]\n");
857 #ifdef SIOCSDEFIFACE_IN6
858 	printf("       ndp [-nt] -I [interface|delete]\n");
859 #endif
860 	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
861 	exit(1);
862 }
863 
864 int
865 rtmsg(cmd)
866 	int cmd;
867 {
868 	static int seq;
869 	int rlen;
870 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
871 	register char *cp = m_rtmsg.m_space;
872 	register int l;
873 
874 	errno = 0;
875 	if (cmd == RTM_DELETE)
876 		goto doit;
877 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
878 	rtm->rtm_flags = flags;
879 	rtm->rtm_version = RTM_VERSION;
880 
881 	switch (cmd) {
882 	default:
883 		fprintf(stderr, "ndp: internal wrong cmd\n");
884 		exit(1);
885 	case RTM_ADD:
886 		rtm->rtm_addrs |= RTA_GATEWAY;
887 		if (expire_time) {
888 			rtm->rtm_rmx.rmx_expire = expire_time;
889 			rtm->rtm_inits = RTV_EXPIRE;
890 		}
891 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
892 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
893 			rtm->rtm_flags &= ~RTF_HOST;
894 			rtm->rtm_addrs |= RTA_NETMASK;
895 		}
896 		/* FALLTHROUGH */
897 	case RTM_GET:
898 		rtm->rtm_addrs |= RTA_DST;
899 	}
900 #define NEXTADDR(w, s) \
901 	if (rtm->rtm_addrs & (w)) { \
902 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
903 
904 	NEXTADDR(RTA_DST, sin_m);
905 	NEXTADDR(RTA_GATEWAY, sdl_m);
906 	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
907 	NEXTADDR(RTA_NETMASK, so_mask);
908 
909 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
910 doit:
911 	l = rtm->rtm_msglen;
912 	rtm->rtm_seq = ++seq;
913 	rtm->rtm_type = cmd;
914 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
915 		if (errno != ESRCH || cmd != RTM_DELETE) {
916 			err(1, "writing to routing socket");
917 			/* NOTREACHED */
918 		}
919 	}
920 	do {
921 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
922 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
923 	if (l < 0)
924 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
925 		    strerror(errno));
926 	return (0);
927 }
928 
929 void
930 ifinfo(ifname, argc, argv)
931 	char *ifname;
932 	int argc;
933 	char **argv;
934 {
935 	struct in6_ndireq nd;
936 	int i, s;
937 	u_int32_t newflags;
938 #ifdef IPV6CTL_USETEMPADDR
939 	u_int8_t nullbuf[8];
940 #endif
941 
942 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
943 		err(1, "socket");
944 		/* NOTREACHED */
945 	}
946 	bzero(&nd, sizeof(nd));
947 	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
948 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
949 		err(1, "ioctl(SIOCGIFINFO_IN6)");
950 		/* NOTREACHED */
951 	}
952 #define ND nd.ndi
953 	newflags = ND.flags;
954 	for (i = 0; i < argc; i++) {
955 		int clear = 0;
956 		char *cp = argv[i];
957 
958 		if (*cp == '-') {
959 			clear = 1;
960 			cp++;
961 		}
962 
963 #define SETFLAG(s, f) \
964 	do {\
965 		if (strcmp(cp, (s)) == 0) {\
966 			if (clear)\
967 				newflags &= ~(f);\
968 			else\
969 				newflags |= (f);\
970 		}\
971 	} while (0)
972 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
973 #ifdef ND6_IFF_ACCEPT_RTADV
974 		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
975 #endif
976 #ifdef ND6_IFF_PREFER_SOURCE
977 		SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
978 #endif
979 
980 		ND.flags = newflags;
981 		if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
982 			err(1, "ioctl(SIOCSIFINFO_FLAGS)");
983 			/* NOTREACHED */
984 		}
985 #undef SETFLAG
986 	}
987 
988 	if (!ND.initialized) {
989 		errx(1, "%s: not initialized yet", ifname);
990 		/* NOTREACHED */
991 	}
992 
993 	printf("linkmtu=%d", ND.linkmtu);
994 	printf(", maxmtu=%d", ND.maxmtu);
995 	printf(", curhlim=%d", ND.chlim);
996 	printf(", basereachable=%ds%dms",
997 	    ND.basereachable / 1000, ND.basereachable % 1000);
998 	printf(", reachable=%ds", ND.reachable);
999 	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1000 #ifdef IPV6CTL_USETEMPADDR
1001 	memset(nullbuf, 0, sizeof(nullbuf));
1002 	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1003 		int j;
1004 		u_int8_t *rbuf;
1005 
1006 		for (i = 0; i < 3; i++) {
1007 			switch (i) {
1008 			case 0:
1009 				printf("\nRandom seed(0): ");
1010 				rbuf = ND.randomseed0;
1011 				break;
1012 			case 1:
1013 				printf("\nRandom seed(1): ");
1014 				rbuf = ND.randomseed1;
1015 				break;
1016 			case 2:
1017 				printf("\nRandom ID:      ");
1018 				rbuf = ND.randomid;
1019 				break;
1020 			}
1021 			for (j = 0; j < 8; j++)
1022 				printf("%02x", rbuf[j]);
1023 		}
1024 	}
1025 #endif
1026 	if (ND.flags) {
1027 		printf("\nFlags: ");
1028 		if ((ND.flags & ND6_IFF_PERFORMNUD))
1029 			printf("nud ");
1030 #ifdef ND6_IFF_ACCEPT_RTADV
1031 		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1032 			printf("accept_rtadv ");
1033 #endif
1034 #ifdef ND6_IFF_PREFER_SOURCE
1035 		if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1036 			printf("prefer_source ");
1037 #endif
1038 	}
1039 	putc('\n', stdout);
1040 #undef ND
1041 
1042 	close(s);
1043 }
1044 
1045 #ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
1046 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
1047 #endif
1048 
1049 void
1050 rtrlist()
1051 {
1052 #ifdef ICMPV6CTL_ND6_DRLIST
1053 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1054 	char *buf;
1055 	struct in6_defrouter *p, *ep;
1056 	size_t l;
1057 	struct timeval time;
1058 
1059 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1060 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1061 		/*NOTREACHED*/
1062 	}
1063 	buf = malloc(l);
1064 	if (!buf) {
1065 		err(1, "malloc");
1066 		/*NOTREACHED*/
1067 	}
1068 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1069 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1070 		/*NOTREACHED*/
1071 	}
1072 
1073 	ep = (struct in6_defrouter *)(buf + l);
1074 	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1075 		int rtpref;
1076 
1077 		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1078 		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1079 		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1080 			strlcpy(host_buf, "?", sizeof(host_buf));
1081 
1082 		printf("%s if=%s", host_buf,
1083 		    if_indextoname(p->if_index, ifix_buf));
1084 		printf(", flags=%s%s",
1085 		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1086 		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1087 		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1088 		printf(", pref=%s", rtpref_str[rtpref]);
1089 
1090 		gettimeofday(&time, 0);
1091 		if (p->expire == 0)
1092 			printf(", expire=Never\n");
1093 		else
1094 			printf(", expire=%s\n",
1095 			    sec2str(p->expire - time.tv_sec));
1096 	}
1097 	free(buf);
1098 #else
1099 	struct in6_drlist dr;
1100 	int s, i;
1101 	struct timeval time;
1102 
1103 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1104 		err(1, "socket");
1105 		/* NOTREACHED */
1106 	}
1107 	bzero(&dr, sizeof(dr));
1108 	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1109 	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1110 		err(1, "ioctl(SIOCGDRLST_IN6)");
1111 		/* NOTREACHED */
1112 	}
1113 #define DR dr.defrouter[i]
1114 	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1115 		struct sockaddr_in6 sin6;
1116 
1117 		bzero(&sin6, sizeof(sin6));
1118 		sin6.sin6_family = AF_INET6;
1119 		sin6.sin6_len = sizeof(sin6);
1120 		sin6.sin6_addr = DR.rtaddr;
1121 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1122 		    sizeof(host_buf), NULL, 0,
1123 		    (nflag ? NI_NUMERICHOST : 0));
1124 
1125 		printf("%s if=%s", host_buf,
1126 		    if_indextoname(DR.if_index, ifix_buf));
1127 		printf(", flags=%s%s",
1128 		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1129 		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1130 		gettimeofday(&time, 0);
1131 		if (DR.expire == 0)
1132 			printf(", expire=Never\n");
1133 		else
1134 			printf(", expire=%s\n",
1135 			    sec2str(DR.expire - time.tv_sec));
1136 	}
1137 #undef DR
1138 	close(s);
1139 #endif
1140 }
1141 
1142 void
1143 plist()
1144 {
1145 #ifdef ICMPV6CTL_ND6_PRLIST
1146 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1147 	char *buf;
1148 	struct in6_prefix *p, *ep, *n;
1149 	struct sockaddr_in6 *advrtr;
1150 	size_t l;
1151 	struct timeval time;
1152 	const int niflags = NI_NUMERICHOST;
1153 	int ninflags = nflag ? NI_NUMERICHOST : 0;
1154 	char namebuf[NI_MAXHOST];
1155 
1156 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1157 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1158 		/*NOTREACHED*/
1159 	}
1160 	buf = malloc(l);
1161 	if (!buf) {
1162 		err(1, "malloc");
1163 		/*NOTREACHED*/
1164 	}
1165 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1166 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1167 		/*NOTREACHED*/
1168 	}
1169 
1170 	ep = (struct in6_prefix *)(buf + l);
1171 	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1172 		advrtr = (struct sockaddr_in6 *)(p + 1);
1173 		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1174 
1175 		if (getnameinfo((struct sockaddr *)&p->prefix,
1176 		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1177 		    NULL, 0, niflags) != 0)
1178 			strlcpy(namebuf, "?", sizeof(namebuf));
1179 		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1180 		    if_indextoname(p->if_index, ifix_buf));
1181 
1182 		gettimeofday(&time, 0);
1183 		/*
1184 		 * meaning of fields, especially flags, is very different
1185 		 * by origin.  notify the difference to the users.
1186 		 */
1187 		printf("flags=%s%s%s%s%s",
1188 		    p->raflags.onlink ? "L" : "",
1189 		    p->raflags.autonomous ? "A" : "",
1190 		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1191 		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1192 #ifdef NDPRF_HOME
1193 		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1194 #else
1195 		    ""
1196 #endif
1197 		    );
1198 		if (p->vltime == ND6_INFINITE_LIFETIME)
1199 			printf(" vltime=infinity");
1200 		else
1201 			printf(" vltime=%lu", (unsigned long)p->vltime);
1202 		if (p->pltime == ND6_INFINITE_LIFETIME)
1203 			printf(", pltime=infinity");
1204 		else
1205 			printf(", pltime=%lu", (unsigned long)p->pltime);
1206 		if (p->expire == 0)
1207 			printf(", expire=Never");
1208 		else if (p->expire >= time.tv_sec)
1209 			printf(", expire=%s",
1210 			    sec2str(p->expire - time.tv_sec));
1211 		else
1212 			printf(", expired");
1213 		printf(", ref=%d", p->refcnt);
1214 		printf("\n");
1215 		/*
1216 		 * "advertising router" list is meaningful only if the prefix
1217 		 * information is from RA.
1218 		 */
1219 		if (p->advrtrs) {
1220 			int j;
1221 			struct sockaddr_in6 *sin6;
1222 
1223 			sin6 = advrtr;
1224 			printf("  advertised by\n");
1225 			for (j = 0; j < p->advrtrs; j++) {
1226 				struct in6_nbrinfo *nbi;
1227 
1228 				if (getnameinfo((struct sockaddr *)sin6,
1229 				    sin6->sin6_len, namebuf, sizeof(namebuf),
1230 				    NULL, 0, ninflags) != 0)
1231 					strlcpy(namebuf, "?", sizeof(namebuf));
1232 				printf("    %s", namebuf);
1233 
1234 				nbi = getnbrinfo(&sin6->sin6_addr,
1235 				    p->if_index, 0);
1236 				if (nbi) {
1237 					switch (nbi->state) {
1238 					case ND6_LLINFO_REACHABLE:
1239 					case ND6_LLINFO_STALE:
1240 					case ND6_LLINFO_DELAY:
1241 					case ND6_LLINFO_PROBE:
1242 						printf(" (reachable)\n");
1243 						break;
1244 					default:
1245 						printf(" (unreachable)\n");
1246 					}
1247 				} else
1248 					printf(" (no neighbor state)\n");
1249 				sin6++;
1250 			}
1251 		} else
1252 			printf("  No advertising router\n");
1253 	}
1254 	free(buf);
1255 #else
1256 	struct in6_prlist pr;
1257 	int s, i;
1258 	struct timeval time;
1259 
1260 	gettimeofday(&time, 0);
1261 
1262 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1263 		err(1, "socket");
1264 		/* NOTREACHED */
1265 	}
1266 	bzero(&pr, sizeof(pr));
1267 	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1268 	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1269 		err(1, "ioctl(SIOCGPRLST_IN6)");
1270 		/* NOTREACHED */
1271 	}
1272 #define PR pr.prefix[i]
1273 	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1274 		struct sockaddr_in6 p6;
1275 		char namebuf[NI_MAXHOST];
1276 		int niflags;
1277 
1278 #ifdef NDPRF_ONLINK
1279 		p6 = PR.prefix;
1280 #else
1281 		memset(&p6, 0, sizeof(p6));
1282 		p6.sin6_family = AF_INET6;
1283 		p6.sin6_len = sizeof(p6);
1284 		p6.sin6_addr = PR.prefix;
1285 #endif
1286 
1287 		/*
1288 		 * copy link index to sin6_scope_id field.
1289 		 * XXX: KAME specific.
1290 		 */
1291 		if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1292 			u_int16_t linkid;
1293 
1294 			memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1295 			    sizeof(linkid));
1296 			linkid = ntohs(linkid);
1297 			p6.sin6_scope_id = linkid;
1298 			p6.sin6_addr.s6_addr[2] = 0;
1299 			p6.sin6_addr.s6_addr[3] = 0;
1300 		}
1301 
1302 		niflags = NI_NUMERICHOST;
1303 		if (getnameinfo((struct sockaddr *)&p6,
1304 		    sizeof(p6), namebuf, sizeof(namebuf),
1305 		    NULL, 0, niflags)) {
1306 			warnx("getnameinfo failed");
1307 			continue;
1308 		}
1309 		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1310 		    if_indextoname(PR.if_index, ifix_buf));
1311 
1312 		gettimeofday(&time, 0);
1313 		/*
1314 		 * meaning of fields, especially flags, is very different
1315 		 * by origin.  notify the difference to the users.
1316 		 */
1317 #if 0
1318 		printf("  %s",
1319 		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1320 #endif
1321 #ifdef NDPRF_ONLINK
1322 		printf("flags=%s%s%s%s%s",
1323 		    PR.raflags.onlink ? "L" : "",
1324 		    PR.raflags.autonomous ? "A" : "",
1325 		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1326 		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1327 #ifdef NDPRF_HOME
1328 		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1329 #else
1330 		    ""
1331 #endif
1332 		    );
1333 #else
1334 		printf("flags=%s%s",
1335 		    PR.raflags.onlink ? "L" : "",
1336 		    PR.raflags.autonomous ? "A" : "");
1337 #endif
1338 		if (PR.vltime == ND6_INFINITE_LIFETIME)
1339 			printf(" vltime=infinity");
1340 		else
1341 			printf(" vltime=%lu", PR.vltime);
1342 		if (PR.pltime == ND6_INFINITE_LIFETIME)
1343 			printf(", pltime=infinity");
1344 		else
1345 			printf(", pltime=%lu", PR.pltime);
1346 		if (PR.expire == 0)
1347 			printf(", expire=Never");
1348 		else if (PR.expire >= time.tv_sec)
1349 			printf(", expire=%s",
1350 			    sec2str(PR.expire - time.tv_sec));
1351 		else
1352 			printf(", expired");
1353 #ifdef NDPRF_ONLINK
1354 		printf(", ref=%d", PR.refcnt);
1355 #endif
1356 #if 0
1357 		switch (PR.origin) {
1358 		case PR_ORIG_RA:
1359 			printf(", origin=RA");
1360 			break;
1361 		case PR_ORIG_RR:
1362 			printf(", origin=RR");
1363 			break;
1364 		case PR_ORIG_STATIC:
1365 			printf(", origin=static");
1366 			break;
1367 		case PR_ORIG_KERNEL:
1368 			printf(", origin=kernel");
1369 			break;
1370 		default:
1371 			printf(", origin=?");
1372 			break;
1373 		}
1374 #endif
1375 		printf("\n");
1376 		/*
1377 		 * "advertising router" list is meaningful only if the prefix
1378 		 * information is from RA.
1379 		 */
1380 		if (0 &&	/* prefix origin is almost obsolted */
1381 		    PR.origin != PR_ORIG_RA)
1382 			;
1383 		else if (PR.advrtrs) {
1384 			int j;
1385 			printf("  advertised by\n");
1386 			for (j = 0; j < PR.advrtrs; j++) {
1387 				struct sockaddr_in6 sin6;
1388 				struct in6_nbrinfo *nbi;
1389 
1390 				bzero(&sin6, sizeof(sin6));
1391 				sin6.sin6_family = AF_INET6;
1392 				sin6.sin6_len = sizeof(sin6);
1393 				sin6.sin6_addr = PR.advrtr[j];
1394 				sin6.sin6_scope_id = PR.if_index; /* XXX */
1395 				getnameinfo((struct sockaddr *)&sin6,
1396 				    sin6.sin6_len, host_buf,
1397 				    sizeof(host_buf), NULL, 0,
1398 				    (nflag ? NI_NUMERICHOST : 0));
1399 				printf("    %s", host_buf);
1400 
1401 				nbi = getnbrinfo(&sin6.sin6_addr,
1402 				    PR.if_index, 0);
1403 				if (nbi) {
1404 					switch (nbi->state) {
1405 					case ND6_LLINFO_REACHABLE:
1406 					case ND6_LLINFO_STALE:
1407 					case ND6_LLINFO_DELAY:
1408 					case ND6_LLINFO_PROBE:
1409 						 printf(" (reachable)\n");
1410 						 break;
1411 					default:
1412 						 printf(" (unreachable)\n");
1413 					}
1414 				} else
1415 					printf(" (no neighbor state)\n");
1416 			}
1417 			if (PR.advrtrs > DRLSTSIZ)
1418 				printf("    and %d routers\n",
1419 				    PR.advrtrs - DRLSTSIZ);
1420 		} else
1421 			printf("  No advertising router\n");
1422 	}
1423 #undef PR
1424 	close(s);
1425 #endif
1426 }
1427 
1428 void
1429 pfx_flush()
1430 {
1431 	char dummyif[IFNAMSIZ+8];
1432 	int s;
1433 
1434 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1435 		err(1, "socket");
1436 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1437 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1438 		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1439 }
1440 
1441 void
1442 rtr_flush()
1443 {
1444 	char dummyif[IFNAMSIZ+8];
1445 	int s;
1446 
1447 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1448 		err(1, "socket");
1449 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1450 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1451 		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1452 
1453 	close(s);
1454 }
1455 
1456 void
1457 harmonize_rtr()
1458 {
1459 	char dummyif[IFNAMSIZ+8];
1460 	int s;
1461 
1462 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1463 		err(1, "socket");
1464 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1465 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1466 		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1467 
1468 	close(s);
1469 }
1470 
1471 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1472 static void
1473 setdefif(ifname)
1474 	char *ifname;
1475 {
1476 	struct in6_ndifreq ndifreq;
1477 	unsigned int ifindex;
1478 
1479 	if (strcasecmp(ifname, "delete") == 0)
1480 		ifindex = 0;
1481 	else {
1482 		if ((ifindex = if_nametoindex(ifname)) == 0)
1483 			err(1, "failed to resolve i/f index for %s", ifname);
1484 	}
1485 
1486 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1487 		err(1, "socket");
1488 
1489 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1490 	ndifreq.ifindex = ifindex;
1491 
1492 	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1493 		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1494 
1495 	close(s);
1496 }
1497 
1498 static void
1499 getdefif()
1500 {
1501 	struct in6_ndifreq ndifreq;
1502 	char ifname[IFNAMSIZ+8];
1503 
1504 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1505 		err(1, "socket");
1506 
1507 	memset(&ndifreq, 0, sizeof(ndifreq));
1508 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1509 
1510 	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1511 		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1512 
1513 	if (ndifreq.ifindex == 0)
1514 		printf("No default interface.\n");
1515 	else {
1516 		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1517 			err(1, "failed to resolve ifname for index %lu",
1518 			    ndifreq.ifindex);
1519 		printf("ND default interface = %s\n", ifname);
1520 	}
1521 
1522 	close(s);
1523 }
1524 #endif
1525 
1526 static char *
1527 sec2str(total)
1528 	time_t total;
1529 {
1530 	static char result[256];
1531 	int days, hours, mins, secs;
1532 	int first = 1;
1533 	char *p = result;
1534 	char *ep = &result[sizeof(result)];
1535 	int n;
1536 
1537 	days = total / 3600 / 24;
1538 	hours = (total / 3600) % 24;
1539 	mins = (total / 60) % 60;
1540 	secs = total % 60;
1541 
1542 	if (days) {
1543 		first = 0;
1544 		n = snprintf(p, ep - p, "%dd", days);
1545 		if (n < 0 || n >= ep - p)
1546 			return "?";
1547 		p += n;
1548 	}
1549 	if (!first || hours) {
1550 		first = 0;
1551 		n = snprintf(p, ep - p, "%dh", hours);
1552 		if (n < 0 || n >= ep - p)
1553 			return "?";
1554 		p += n;
1555 	}
1556 	if (!first || mins) {
1557 		first = 0;
1558 		n = snprintf(p, ep - p, "%dm", mins);
1559 		if (n < 0 || n >= ep - p)
1560 			return "?";
1561 		p += n;
1562 	}
1563 	snprintf(p, ep - p, "%ds", secs);
1564 
1565 	return(result);
1566 }
1567 
1568 /*
1569  * Print the timestamp
1570  * from tcpdump/util.c
1571  */
1572 static void
1573 ts_print(tvp)
1574 	const struct timeval *tvp;
1575 {
1576 	int s;
1577 
1578 	/* Default */
1579 	s = (tvp->tv_sec + thiszone) % 86400;
1580 	(void)printf("%02d:%02d:%02d.%06u ",
1581 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1582 }
1583