xref: /freebsd/usr.sbin/ndp/ndp.c (revision 7773002178c8dbc52b44e4d705f07706409af8e4)
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 /* packing rule for routing socket */
117 #define ROUNDUP(a) \
118 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
119 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
120 
121 static pid_t pid;
122 static int cflag;
123 static int nflag;
124 static int tflag;
125 static int32_t thiszone;	/* time difference with gmt */
126 static int s = -1;
127 static int repeat = 0;
128 
129 char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
130 char host_buf[NI_MAXHOST];		/* getnameinfo() */
131 char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
132 
133 int main __P((int, char **));
134 int file __P((char *));
135 void getsocket __P((void));
136 int set __P((int, char **));
137 void get __P((char *));
138 int delete __P((char *));
139 void dump __P((struct in6_addr *));
140 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
141 					   int ifindex, int));
142 static char *ether_str __P((struct sockaddr_dl *));
143 int ndp_ether_aton __P((char *, u_char *));
144 void usage __P((void));
145 int rtmsg __P((int));
146 void ifinfo __P((int, char **));
147 void rtrlist __P((void));
148 void plist __P((void));
149 void pfx_flush __P((void));
150 void rtrlist __P((void));
151 void rtr_flush __P((void));
152 void harmonize_rtr __P((void));
153 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
154 static void getdefif __P((void));
155 static void setdefif __P((char *));
156 #endif
157 static char *sec2str __P((time_t t));
158 static char *ether_str __P((struct sockaddr_dl *sdl));
159 static void ts_print __P((const struct timeval *));
160 
161 static char *rtpref_str[] = {
162 	"medium",		/* 00 */
163 	"high",			/* 01 */
164 	"rsv",			/* 10 */
165 	"low"			/* 11 */
166 };
167 
168 int
169 main(argc, argv)
170 	int argc;
171 	char **argv;
172 {
173 	int ch;
174 	int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
175 		pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
176 
177 	pid = getpid();
178 	thiszone = gmt2local(0);
179 	while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
180 		switch (ch) {
181 		case 'a':
182 			aflag = 1;
183 			break;
184 		case 'c':
185 			cflag = 1;
186 			break;
187 		case 'd':
188 			dflag = 1;
189 			break;
190 		case 'I':
191 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
192 			if (argc > 2)
193 				setdefif(argv[2]);
194 			getdefif(); /* always call it to print the result */
195 			exit(0);
196 #else
197 			errx(1, "not supported yet");
198 			/*NOTREACHED*/
199 #endif
200 		case 'i' :
201 			argc -= optind;
202 			argv += optind;
203 			if (argc < 1)
204 				usage();
205 			ifinfo(argc, argv);
206 			exit(0);
207 		case 'n':
208 			nflag = 1;
209 			continue;
210 		case 'p':
211 			pflag = 1;
212 			break;
213 		case 'f' :
214 			if (argc != 3)
215 				usage();
216 			file(argv[2]);
217 			exit(0);
218 		case 'l' :
219 			/* obsolete, ignored */
220 			break;
221 		case 'r' :
222 			rflag = 1;
223 			break;
224 		case 's':
225 			sflag = 1;
226 			break;
227 		case 't':
228 			tflag = 1;
229 			break;
230 		case 'A':
231 			aflag = 1;
232 			repeat = atoi(optarg);
233 			if (repeat < 0)
234 				usage();
235 			break;
236 		case 'H' :
237 			Hflag = 1;
238 			break;
239 		case 'P':
240 			Pflag = 1;
241 			break;
242 		case 'R':
243 			Rflag = 1;
244 			break;
245 		default:
246 			usage();
247 		}
248 
249 	argc -= optind;
250 	argv += optind;
251 
252 	if (aflag || cflag) {
253 		dump(0);
254 		exit(0);
255 	}
256 	if (dflag) {
257 		if (argc != 1)
258 			usage();
259 		delete(argv[0]);
260 		exit(0);
261 	}
262 	if (pflag) {
263 		plist();
264 		exit(0);
265 	}
266 	if (rflag) {
267 		rtrlist();
268 		exit(0);
269 	}
270 	if (sflag) {
271 		if (argc < 2 || argc > 4)
272 			usage();
273 		exit(set(argc, argv) ? 1 : 0);
274 	}
275 	if (Hflag) {
276 		harmonize_rtr();
277 		exit(0);
278 	}
279 	if (Pflag) {
280 		pfx_flush();
281 		exit(0);
282 	}
283 	if (Rflag) {
284 		rtr_flush();
285 		exit(0);
286 	}
287 
288 	if (argc != 1)
289 		usage();
290 	get(argv[0]);
291 	exit(0);
292 }
293 
294 /*
295  * Process a file to set standard ndp entries
296  */
297 int
298 file(name)
299 	char *name;
300 {
301 	FILE *fp;
302 	int i, retval;
303 	char line[100], arg[5][50], *args[5];
304 
305 	if ((fp = fopen(name, "r")) == NULL) {
306 		fprintf(stderr, "ndp: cannot open %s\n", name);
307 		exit(1);
308 	}
309 	args[0] = &arg[0][0];
310 	args[1] = &arg[1][0];
311 	args[2] = &arg[2][0];
312 	args[3] = &arg[3][0];
313 	args[4] = &arg[4][0];
314 	retval = 0;
315 	while (fgets(line, 100, fp) != NULL) {
316 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
317 		    arg[3], arg[4]);
318 		if (i < 2) {
319 			fprintf(stderr, "ndp: bad line: %s\n", line);
320 			retval = 1;
321 			continue;
322 		}
323 		if (set(i, args))
324 			retval = 1;
325 	}
326 	fclose(fp);
327 	return (retval);
328 }
329 
330 void
331 getsocket()
332 {
333 	if (s < 0) {
334 		s = socket(PF_ROUTE, SOCK_RAW, 0);
335 		if (s < 0) {
336 			err(1, "socket");
337 			/* NOTREACHED */
338 		}
339 	}
340 }
341 
342 struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
343 struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
344 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
345 int	expire_time, flags, found_entry;
346 struct	{
347 	struct	rt_msghdr m_rtm;
348 	char	m_space[512];
349 }	m_rtmsg;
350 
351 /*
352  * Set an individual neighbor cache entry
353  */
354 int
355 set(argc, argv)
356 	int argc;
357 	char **argv;
358 {
359 	register struct sockaddr_in6 *sin = &sin_m;
360 	register struct sockaddr_dl *sdl;
361 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
362 	struct addrinfo hints, *res;
363 	int gai_error;
364 	u_char *ea;
365 	char *host = argv[0], *eaddr = argv[1];
366 
367 	getsocket();
368 	argc -= 2;
369 	argv += 2;
370 	sdl_m = blank_sdl;
371 	sin_m = blank_sin;
372 
373 	bzero(&hints, sizeof(hints));
374 	hints.ai_family = AF_INET6;
375 	gai_error = getaddrinfo(host, NULL, &hints, &res);
376 	if (gai_error) {
377 		fprintf(stderr, "ndp: %s: %s\n", host,
378 			gai_strerror(gai_error));
379 		return 1;
380 	}
381 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
382 #ifdef __KAME__
383 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
384 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
385 		    htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
386 	}
387 #endif
388 	ea = (u_char *)LLADDR(&sdl_m);
389 	if (ndp_ether_aton(eaddr, ea) == 0)
390 		sdl_m.sdl_alen = 6;
391 	flags = expire_time = 0;
392 	while (argc-- > 0) {
393 		if (strncmp(argv[0], "temp", 4) == 0) {
394 			struct timeval time;
395 
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	31
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 %2s %4s %4s\n",
573 		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
574 		    W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
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 			err(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 (addr) {
618 			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
619 				continue;
620 			found_entry = 1;
621 		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
622 			continue;
623 		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
624 		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
625 			/* XXX: should scope id be filled in the kernel? */
626 			if (sin->sin6_scope_id == 0)
627 				sin->sin6_scope_id = sdl->sdl_index;
628 #ifdef __KAME__
629 			/* KAME specific hack; removed the embedded id */
630 			*(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
631 #endif
632 		}
633 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
634 		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
635 		if (cflag == 1) {
636 #ifdef RTF_WASCLONED
637 			if (rtm->rtm_flags & RTF_WASCLONED)
638 				delete(host_buf);
639 #else
640 			delete(host_buf);
641 #endif
642 			continue;
643 		}
644 		gettimeofday(&time, 0);
645 		if (tflag)
646 			ts_print(&time);
647 
648 		addrwidth = strlen(host_buf);
649 		if (addrwidth < W_ADDR)
650 			addrwidth = W_ADDR;
651 		llwidth = strlen(ether_str(sdl));
652 		if (W_ADDR + W_LL - addrwidth > llwidth)
653 			llwidth = W_ADDR + W_LL - addrwidth;
654 		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
655 		if (!ifname)
656 			ifname = "?";
657 		ifwidth = strlen(ifname);
658 		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
659 			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
660 
661 		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
662 		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
663 
664 		/* Print neighbor discovery specific informations */
665 		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
666 		if (nbi) {
667 			if (nbi->expire > time.tv_sec) {
668 				printf(" %-9.9s",
669 				    sec2str(nbi->expire - time.tv_sec));
670 			} else if (nbi->expire == 0)
671 				printf(" %-9.9s", "permanent");
672 			else
673 				printf(" %-9.9s", "expired");
674 
675 			switch (nbi->state) {
676 			case ND6_LLINFO_NOSTATE:
677 				 printf(" N");
678 				 break;
679 #ifdef ND6_LLINFO_WAITDELETE
680 			case ND6_LLINFO_WAITDELETE:
681 				 printf(" W");
682 				 break;
683 #endif
684 			case ND6_LLINFO_INCOMPLETE:
685 				 printf(" I");
686 				 break;
687 			case ND6_LLINFO_REACHABLE:
688 				 printf(" R");
689 				 break;
690 			case ND6_LLINFO_STALE:
691 				 printf(" S");
692 				 break;
693 			case ND6_LLINFO_DELAY:
694 				 printf(" D");
695 				 break;
696 			case ND6_LLINFO_PROBE:
697 				 printf(" P");
698 				 break;
699 			default:
700 				 printf(" ?");
701 				 break;
702 			}
703 
704 			isrouter = nbi->isrouter;
705 			prbs = nbi->asked;
706 		} else {
707 			warnx("failed to get neighbor information");
708 			printf("  ");
709 		}
710 		putchar(' ');
711 
712 		/*
713 		 * other flags. R: router, P: proxy, W: ??
714 		 */
715 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
716 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
717 			    isrouter ? "R" : "",
718 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
719 		} else {
720 			sin = (struct sockaddr_in6 *)
721 			    (sdl->sdl_len + (char *)sdl);
722 			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
723 			    isrouter ? "R" : "",
724 			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
725 			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
726 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
727 		}
728 		printf(" %-4.4s", flgbuf);
729 
730 		if (prbs)
731 			printf(" %4d", prbs);
732 
733 		printf("\n");
734 	}
735 	if (buf != NULL)
736 		free(buf);
737 
738 	if (repeat) {
739 		printf("\n");
740 		sleep(repeat);
741 		goto again;
742 	}
743 }
744 
745 static struct in6_nbrinfo *
746 getnbrinfo(addr, ifindex, warning)
747 	struct in6_addr *addr;
748 	int ifindex;
749 	int warning;
750 {
751 	static struct in6_nbrinfo nbi;
752 	int s;
753 
754 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
755 		err(1, "socket");
756 
757 	bzero(&nbi, sizeof(nbi));
758 	if_indextoname(ifindex, nbi.ifname);
759 	nbi.addr = *addr;
760 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
761 		if (warning)
762 			warn("ioctl(SIOCGNBRINFO_IN6)");
763 		close(s);
764 		return(NULL);
765 	}
766 
767 	close(s);
768 	return(&nbi);
769 }
770 
771 static char *
772 ether_str(sdl)
773 	struct sockaddr_dl *sdl;
774 {
775 	static char hbuf[NI_MAXHOST];
776 	u_char *cp;
777 
778 	if (sdl->sdl_alen) {
779 		cp = (u_char *)LLADDR(sdl);
780 		snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
781 		    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
782 	} else
783 		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
784 
785 	return(hbuf);
786 }
787 
788 int
789 ndp_ether_aton(a, n)
790 	char *a;
791 	u_char *n;
792 {
793 	int i, o[6];
794 
795 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
796 	    &o[3], &o[4], &o[5]);
797 	if (i != 6) {
798 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
799 		return (1);
800 	}
801 	for (i = 0; i < 6; i++)
802 		n[i] = o[i];
803 	return (0);
804 }
805 
806 void
807 usage()
808 {
809 	printf("usage: ndp hostname\n");
810 	printf("       ndp -a[nt]\n");
811 	printf("       ndp [-nt] -A wait\n");
812 	printf("       ndp -c[nt]\n");
813 	printf("       ndp -d[nt] hostname\n");
814 	printf("       ndp -f[nt] filename\n");
815 	printf("       ndp -i interface [flags...]\n");
816 #ifdef SIOCSDEFIFACE_IN6
817 	printf("       ndp -I [interface|delete]\n");
818 #endif
819 	printf("       ndp -p\n");
820 	printf("       ndp -r\n");
821 	printf("       ndp -s hostname ether_addr [temp] [proxy]\n");
822 	printf("       ndp -H\n");
823 	printf("       ndp -P\n");
824 	printf("       ndp -R\n");
825 	exit(1);
826 }
827 
828 int
829 rtmsg(cmd)
830 	int cmd;
831 {
832 	static int seq;
833 	int rlen;
834 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
835 	register char *cp = m_rtmsg.m_space;
836 	register int l;
837 
838 	errno = 0;
839 	if (cmd == RTM_DELETE)
840 		goto doit;
841 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
842 	rtm->rtm_flags = flags;
843 	rtm->rtm_version = RTM_VERSION;
844 
845 	switch (cmd) {
846 	default:
847 		fprintf(stderr, "ndp: internal wrong cmd\n");
848 		exit(1);
849 	case RTM_ADD:
850 		rtm->rtm_addrs |= RTA_GATEWAY;
851 		rtm->rtm_rmx.rmx_expire = expire_time;
852 		rtm->rtm_inits = RTV_EXPIRE;
853 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
854 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
855 			rtm->rtm_flags &= ~RTF_HOST;
856 			rtm->rtm_flags |= RTA_NETMASK;
857 		}
858 		/* FALLTHROUGH */
859 	case RTM_GET:
860 		rtm->rtm_addrs |= RTA_DST;
861 	}
862 #define NEXTADDR(w, s) \
863 	if (rtm->rtm_addrs & (w)) { \
864 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
865 
866 	NEXTADDR(RTA_DST, sin_m);
867 	NEXTADDR(RTA_GATEWAY, sdl_m);
868 	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
869 	NEXTADDR(RTA_NETMASK, so_mask);
870 
871 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
872 doit:
873 	l = rtm->rtm_msglen;
874 	rtm->rtm_seq = ++seq;
875 	rtm->rtm_type = cmd;
876 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
877 		if (errno != ESRCH || cmd != RTM_DELETE) {
878 			err(1, "writing to routing socket");
879 			/* NOTREACHED */
880 		}
881 	}
882 	do {
883 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
884 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
885 	if (l < 0)
886 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
887 		    strerror(errno));
888 	return (0);
889 }
890 
891 void
892 ifinfo(argc, argv)
893 	int argc;
894 	char **argv;
895 {
896 	struct in6_ndireq nd;
897 	int i, s;
898 	char *ifname = argv[0];
899 	u_int32_t newflags;
900 #ifdef IPV6CTL_USETEMPADDR
901 	u_int8_t nullbuf[8];
902 #endif
903 
904 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
905 		err(1, "socket");
906 		/* NOTREACHED */
907 	}
908 	bzero(&nd, sizeof(nd));
909 	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
910 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
911 		err(1, "ioctl(SIOCGIFINFO_IN6)");
912 		/* NOTREACHED */
913  	}
914 #define ND nd.ndi
915 	newflags = ND.flags;
916 	for (i = 1; i < argc; i++) {
917 		int clear = 0;
918 		char *cp = argv[i];
919 
920 		if (*cp == '-') {
921 			clear = 1;
922 			cp++;
923 		}
924 
925 #define SETFLAG(s, f) \
926 	do {\
927 		if (strcmp(cp, (s)) == 0) {\
928 			if (clear)\
929 				newflags &= ~(f);\
930 			else\
931 				newflags |= (f);\
932 		}\
933 	} while (0)
934 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
935 #ifdef ND6_IFF_ACCEPT_RTADV
936 		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
937 #endif
938 
939 		ND.flags = newflags;
940 		if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
941 			err(1, "ioctl(SIOCSIFINFO_FLAGS)");
942 			/* NOTREACHED */
943 		}
944 #undef SETFLAG
945 	}
946 
947 	if (!ND.initialized) {
948 		errx(1, "%s: not initialized yet", ifname);
949 		/* NOTREACHED */
950 	}
951 
952 	printf("linkmtu=%d", ND.linkmtu);
953 	printf(", maxmtu=%d", ND.maxmtu);
954 	printf(", curhlim=%d", ND.chlim);
955 	printf(", basereachable=%ds%dms",
956 	    ND.basereachable / 1000, ND.basereachable % 1000);
957 	printf(", reachable=%ds", ND.reachable);
958 	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
959 #ifdef IPV6CTL_USETEMPADDR
960 	memset(nullbuf, 0, sizeof(nullbuf));
961 	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
962 		int j;
963 		u_int8_t *rbuf;
964 
965 		for (i = 0; i < 3; i++) {
966 			switch (i) {
967 			case 0:
968 				printf("\nRandom seed(0): ");
969 				rbuf = ND.randomseed0;
970 				break;
971 			case 1:
972 				printf("\nRandom seed(1): ");
973 				rbuf = ND.randomseed1;
974 				break;
975 			case 2:
976 				printf("\nRandom ID:      ");
977 				rbuf = ND.randomid;
978 				break;
979 			}
980 			for (j = 0; j < 8; j++)
981 				printf("%02x", rbuf[j]);
982 		}
983 	}
984 #endif
985 	if (ND.flags) {
986 		printf("\nFlags: ");
987 		if ((ND.flags & ND6_IFF_PERFORMNUD))
988 			printf("nud ");
989 #ifdef ND6_IFF_ACCEPT_RTADV
990 		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
991 			printf("accept_rtadv ");
992 #endif
993 }
994 	putc('\n', stdout);
995 #undef ND
996 
997 	close(s);
998 }
999 
1000 #ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
1001 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
1002 #endif
1003 
1004 void
1005 rtrlist()
1006 {
1007 #ifdef ICMPV6CTL_ND6_DRLIST
1008 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1009 	char *buf;
1010 	struct in6_defrouter *p, *ep;
1011 	size_t l;
1012 	struct timeval time;
1013 
1014 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1015 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1016 		/*NOTREACHED*/
1017 	}
1018 	buf = malloc(l);
1019 	if (!buf) {
1020 		err(1, "malloc");
1021 		/*NOTREACHED*/
1022 	}
1023 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1024 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1025 		/*NOTREACHED*/
1026 	}
1027 
1028 	ep = (struct in6_defrouter *)(buf + l);
1029 	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1030 		int rtpref;
1031 
1032 		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1033 		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1034 		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1035 			strlcpy(host_buf, "?", sizeof(host_buf));
1036 
1037 		printf("%s if=%s", host_buf,
1038 		    if_indextoname(p->if_index, ifix_buf));
1039 		printf(", flags=%s%s",
1040 		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1041 		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1042 		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1043 		printf(", pref=%s", rtpref_str[rtpref]);
1044 
1045 		gettimeofday(&time, 0);
1046 		if (p->expire == 0)
1047 			printf(", expire=Never\n");
1048 		else
1049 			printf(", expire=%s\n",
1050 			    sec2str(p->expire - time.tv_sec));
1051 	}
1052 	free(buf);
1053 #else
1054 	struct in6_drlist dr;
1055 	int s, i;
1056 	struct timeval time;
1057 
1058 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1059 		err(1, "socket");
1060 		/* NOTREACHED */
1061 	}
1062 	bzero(&dr, sizeof(dr));
1063 	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1064 	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1065 		err(1, "ioctl(SIOCGDRLST_IN6)");
1066 		/* NOTREACHED */
1067 	}
1068 #define DR dr.defrouter[i]
1069 	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1070 		struct sockaddr_in6 sin6;
1071 
1072 		bzero(&sin6, sizeof(sin6));
1073 		sin6.sin6_family = AF_INET6;
1074 		sin6.sin6_len = sizeof(sin6);
1075 		sin6.sin6_addr = DR.rtaddr;
1076 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1077 		    sizeof(host_buf), NULL, 0,
1078 		    (nflag ? NI_NUMERICHOST : 0));
1079 
1080 		printf("%s if=%s", host_buf,
1081 		    if_indextoname(DR.if_index, ifix_buf));
1082 		printf(", flags=%s%s",
1083 		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1084 		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1085 		gettimeofday(&time, 0);
1086 		if (DR.expire == 0)
1087 			printf(", expire=Never\n");
1088 		else
1089 			printf(", expire=%s\n",
1090 			    sec2str(DR.expire - time.tv_sec));
1091 	}
1092 #undef DR
1093 	close(s);
1094 #endif
1095 }
1096 
1097 void
1098 plist()
1099 {
1100 #ifdef ICMPV6CTL_ND6_PRLIST
1101 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1102 	char *buf;
1103 	struct in6_prefix *p, *ep, *n;
1104 	struct sockaddr_in6 *advrtr;
1105 	size_t l;
1106 	struct timeval time;
1107 	const int niflags = NI_NUMERICHOST;
1108 	int ninflags = nflag ? NI_NUMERICHOST : 0;
1109 	char namebuf[NI_MAXHOST];
1110 
1111 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1112 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1113 		/*NOTREACHED*/
1114 	}
1115 	buf = malloc(l);
1116 	if (!buf) {
1117 		err(1, "malloc");
1118 		/*NOTREACHED*/
1119 	}
1120 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1121 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1122 		/*NOTREACHED*/
1123 	}
1124 
1125 	ep = (struct in6_prefix *)(buf + l);
1126 	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1127 		advrtr = (struct sockaddr_in6 *)(p + 1);
1128 		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1129 
1130 		if (getnameinfo((struct sockaddr *)&p->prefix,
1131 		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1132 		    NULL, 0, niflags) != 0)
1133 			strlcpy(namebuf, "?", sizeof(namebuf));
1134 		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1135 		    if_indextoname(p->if_index, ifix_buf));
1136 
1137 		gettimeofday(&time, 0);
1138 		/*
1139 		 * meaning of fields, especially flags, is very different
1140 		 * by origin.  notify the difference to the users.
1141 		 */
1142 		printf("flags=%s%s%s%s%s",
1143 		    p->raflags.onlink ? "L" : "",
1144 		    p->raflags.autonomous ? "A" : "",
1145 		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1146 		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1147 #ifdef NDPRF_HOME
1148 		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1149 #else
1150 		    ""
1151 #endif
1152 		    );
1153 		if (p->vltime == ND6_INFINITE_LIFETIME)
1154 			printf(" vltime=infinity");
1155 		else
1156 			printf(" vltime=%ld", (long)p->vltime);
1157 		if (p->pltime == ND6_INFINITE_LIFETIME)
1158 			printf(", pltime=infinity");
1159 		else
1160 			printf(", pltime=%ld", (long)p->pltime);
1161 		if (p->expire == 0)
1162 			printf(", expire=Never");
1163 		else if (p->expire >= time.tv_sec)
1164 			printf(", expire=%s",
1165 			    sec2str(p->expire - time.tv_sec));
1166 		else
1167 			printf(", expired");
1168 		printf(", ref=%d", p->refcnt);
1169 		printf("\n");
1170 		/*
1171 		 * "advertising router" list is meaningful only if the prefix
1172 		 * information is from RA.
1173 		 */
1174 		if (p->advrtrs) {
1175 			int j;
1176 			struct sockaddr_in6 *sin6;
1177 
1178 			sin6 = (struct sockaddr_in6 *)(p + 1);
1179 			printf("  advertised by\n");
1180 			for (j = 0; j < p->advrtrs; j++) {
1181 				struct in6_nbrinfo *nbi;
1182 
1183 				if (getnameinfo((struct sockaddr *)sin6,
1184 				    sin6->sin6_len, namebuf, sizeof(namebuf),
1185 				    NULL, 0, ninflags) != 0)
1186 					strlcpy(namebuf, "?", sizeof(namebuf));
1187 				printf("    %s", namebuf);
1188 
1189 				nbi = getnbrinfo(&sin6->sin6_addr,
1190 				    p->if_index, 0);
1191 				if (nbi) {
1192 					switch (nbi->state) {
1193 					case ND6_LLINFO_REACHABLE:
1194 					case ND6_LLINFO_STALE:
1195 					case ND6_LLINFO_DELAY:
1196 					case ND6_LLINFO_PROBE:
1197 						printf(" (reachable)\n");
1198 						break;
1199 					default:
1200 						printf(" (unreachable)\n");
1201 					}
1202 				} else
1203 					printf(" (no neighbor state)\n");
1204 				sin6++;
1205 			}
1206 		} else
1207 			printf("  No advertising router\n");
1208 	}
1209 	free(buf);
1210 #else
1211 	struct in6_prlist pr;
1212 	int s, i;
1213 	struct timeval time;
1214 
1215 	gettimeofday(&time, 0);
1216 
1217 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1218 		err(1, "socket");
1219 		/* NOTREACHED */
1220 	}
1221 	bzero(&pr, sizeof(pr));
1222 	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1223 	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1224 		err(1, "ioctl(SIOCGPRLST_IN6)");
1225 		/* NOTREACHED */
1226 	}
1227 #define PR pr.prefix[i]
1228 	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1229 		struct sockaddr_in6 p6;
1230 		char namebuf[NI_MAXHOST];
1231 		int niflags;
1232 
1233 #ifdef NDPRF_ONLINK
1234 		p6 = PR.prefix;
1235 #else
1236 		memset(&p6, 0, sizeof(p6));
1237 		p6.sin6_family = AF_INET6;
1238 		p6.sin6_len = sizeof(p6);
1239 		p6.sin6_addr = PR.prefix;
1240 #endif
1241 
1242 		/*
1243 		 * copy link index to sin6_scope_id field.
1244 		 * XXX: KAME specific.
1245 		 */
1246 		if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1247 			u_int16_t linkid;
1248 
1249 			memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1250 			    sizeof(linkid));
1251 			linkid = ntohs(linkid);
1252 			p6.sin6_scope_id = linkid;
1253 			p6.sin6_addr.s6_addr[2] = 0;
1254 			p6.sin6_addr.s6_addr[3] = 0;
1255 		}
1256 
1257 		niflags = NI_NUMERICHOST;
1258 		if (getnameinfo((struct sockaddr *)&p6,
1259 		    sizeof(p6), namebuf, sizeof(namebuf),
1260 		    NULL, 0, niflags)) {
1261 			warnx("getnameinfo failed");
1262 			continue;
1263 		}
1264 		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1265 		    if_indextoname(PR.if_index, ifix_buf));
1266 
1267 		gettimeofday(&time, 0);
1268 		/*
1269 		 * meaning of fields, especially flags, is very different
1270 		 * by origin.  notify the difference to the users.
1271 		 */
1272 #if 0
1273 		printf("  %s",
1274 		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1275 #endif
1276 #ifdef NDPRF_ONLINK
1277 		printf("flags=%s%s%s%s%s",
1278 		    PR.raflags.onlink ? "L" : "",
1279 		    PR.raflags.autonomous ? "A" : "",
1280 		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1281 		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1282 #ifdef NDPRF_HOME
1283 		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1284 #else
1285 		    ""
1286 #endif
1287 		    );
1288 #else
1289 		printf("flags=%s%s",
1290 		    PR.raflags.onlink ? "L" : "",
1291 		    PR.raflags.autonomous ? "A" : "");
1292 #endif
1293 		if (PR.vltime == ND6_INFINITE_LIFETIME)
1294 			printf(" vltime=infinity");
1295 		else
1296 			printf(" vltime=%ld", (long)PR.vltime);
1297 		if (PR.pltime == ND6_INFINITE_LIFETIME)
1298 			printf(", pltime=infinity");
1299 		else
1300 			printf(", pltime=%ld", (long)PR.pltime);
1301 		if (PR.expire == 0)
1302 			printf(", expire=Never");
1303 		else if (PR.expire >= time.tv_sec)
1304 			printf(", expire=%s",
1305 			    sec2str(PR.expire - time.tv_sec));
1306 		else
1307 			printf(", expired");
1308 #ifdef NDPRF_ONLINK
1309 		printf(", ref=%d", PR.refcnt);
1310 #endif
1311 #if 0
1312 		switch (PR.origin) {
1313 		case PR_ORIG_RA:
1314 			printf(", origin=RA");
1315 			break;
1316 		case PR_ORIG_RR:
1317 			printf(", origin=RR");
1318 			break;
1319 		case PR_ORIG_STATIC:
1320 			printf(", origin=static");
1321 			break;
1322 		case PR_ORIG_KERNEL:
1323 			printf(", origin=kernel");
1324 			break;
1325 		default:
1326 			printf(", origin=?");
1327 			break;
1328 		}
1329 #endif
1330 		printf("\n");
1331 		/*
1332 		 * "advertising router" list is meaningful only if the prefix
1333 		 * information is from RA.
1334 		 */
1335 		if (0 &&	/* prefix origin is almost obsolted */
1336 		    PR.origin != PR_ORIG_RA)
1337 			;
1338 		else if (PR.advrtrs) {
1339 			int j;
1340 			printf("  advertised by\n");
1341 			for (j = 0; j < PR.advrtrs; j++) {
1342 				struct sockaddr_in6 sin6;
1343 				struct in6_nbrinfo *nbi;
1344 
1345 				bzero(&sin6, sizeof(sin6));
1346 				sin6.sin6_family = AF_INET6;
1347 				sin6.sin6_len = sizeof(sin6);
1348 				sin6.sin6_addr = PR.advrtr[j];
1349 				sin6.sin6_scope_id = PR.if_index; /* XXX */
1350 				getnameinfo((struct sockaddr *)&sin6,
1351 				    sin6.sin6_len, host_buf,
1352 				    sizeof(host_buf), NULL, 0,
1353 				    (nflag ? NI_NUMERICHOST : 0));
1354 				printf("    %s", host_buf);
1355 
1356 				nbi = getnbrinfo(&sin6.sin6_addr,
1357 				    PR.if_index, 0);
1358 				if (nbi) {
1359 					switch (nbi->state) {
1360 					case ND6_LLINFO_REACHABLE:
1361 					case ND6_LLINFO_STALE:
1362 					case ND6_LLINFO_DELAY:
1363 					case ND6_LLINFO_PROBE:
1364 						 printf(" (reachable)\n");
1365 						 break;
1366 					default:
1367 						 printf(" (unreachable)\n");
1368 					}
1369 				} else
1370 					printf(" (no neighbor state)\n");
1371 			}
1372 			if (PR.advrtrs > DRLSTSIZ)
1373 				printf("    and %d routers\n",
1374 				    PR.advrtrs - DRLSTSIZ);
1375 		} else
1376 			printf("  No advertising router\n");
1377 	}
1378 #undef PR
1379 	close(s);
1380 #endif
1381 }
1382 
1383 void
1384 pfx_flush()
1385 {
1386 	char dummyif[IFNAMSIZ+8];
1387 	int s;
1388 
1389 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1390 		err(1, "socket");
1391 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1392 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1393 		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1394 }
1395 
1396 void
1397 rtr_flush()
1398 {
1399 	char dummyif[IFNAMSIZ+8];
1400 	int s;
1401 
1402 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1403 		err(1, "socket");
1404 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1405 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1406 		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1407 
1408 	close(s);
1409 }
1410 
1411 void
1412 harmonize_rtr()
1413 {
1414 	char dummyif[IFNAMSIZ+8];
1415 	int s;
1416 
1417 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1418 		err(1, "socket");
1419 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1420 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1421 		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1422 
1423 	close(s);
1424 }
1425 
1426 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1427 static void
1428 setdefif(ifname)
1429 	char *ifname;
1430 {
1431 	struct in6_ndifreq ndifreq;
1432 	unsigned int ifindex;
1433 
1434 	if (strcasecmp(ifname, "delete") == 0)
1435 		ifindex = 0;
1436 	else {
1437 		if ((ifindex = if_nametoindex(ifname)) == 0)
1438 			err(1, "failed to resolve i/f index for %s", ifname);
1439 	}
1440 
1441 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1442 		err(1, "socket");
1443 
1444 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1445 	ndifreq.ifindex = ifindex;
1446 
1447 	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1448 		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1449 
1450 	close(s);
1451 }
1452 
1453 static void
1454 getdefif()
1455 {
1456 	struct in6_ndifreq ndifreq;
1457 	char ifname[IFNAMSIZ+8];
1458 
1459 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1460 		err(1, "socket");
1461 
1462 	memset(&ndifreq, 0, sizeof(ndifreq));
1463 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1464 
1465 	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1466 		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1467 
1468 	if (ndifreq.ifindex == 0)
1469 		printf("No default interface.\n");
1470 	else {
1471 		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1472 			err(1, "failed to resolve ifname for index %lu",
1473 			    ndifreq.ifindex);
1474 		printf("ND default interface = %s\n", ifname);
1475 	}
1476 
1477 	close(s);
1478 }
1479 #endif
1480 
1481 static char *
1482 sec2str(total)
1483 	time_t total;
1484 {
1485 	static char result[256];
1486 	int days, hours, mins, secs;
1487 	int first = 1;
1488 	char *p = result;
1489 	char *ep = &result[sizeof(result)];
1490 	int n;
1491 
1492 	days = total / 3600 / 24;
1493 	hours = (total / 3600) % 24;
1494 	mins = (total / 60) % 60;
1495 	secs = total % 60;
1496 
1497 	if (days) {
1498 		first = 0;
1499 		n = snprintf(p, ep - p, "%dd", days);
1500 		if (n < 0 || n >= ep - p)
1501 			return "?";
1502 		p += n;
1503 	}
1504 	if (!first || hours) {
1505 		first = 0;
1506 		n = snprintf(p, ep - p, "%dh", hours);
1507 		if (n < 0 || n >= ep - p)
1508 			return "?";
1509 		p += n;
1510 	}
1511 	if (!first || mins) {
1512 		first = 0;
1513 		n = snprintf(p, ep - p, "%dm", mins);
1514 		if (n < 0 || n >= ep - p)
1515 			return "?";
1516 		p += n;
1517 	}
1518 	snprintf(p, ep - p, "%ds", secs);
1519 
1520 	return(result);
1521 }
1522 
1523 /*
1524  * Print the timestamp
1525  * from tcpdump/util.c
1526  */
1527 static void
1528 ts_print(tvp)
1529 	const struct timeval *tvp;
1530 {
1531 	int s;
1532 
1533 	/* Default */
1534 	s = (tvp->tv_sec + thiszone) % 86400;
1535 	(void)printf("%02d:%02d:%02d.%06u ",
1536 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1537 }
1538