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