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