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