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