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