ndp.c revision 121156
1/*	$FreeBSD: head/usr.sbin/ndp/ndp.c 121156 2003-10-17 11:46:40Z ume $	*/
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
121static pid_t pid;
122static int cflag;
123static int nflag;
124static int tflag;
125static int32_t thiszone;	/* time difference with gmt */
126static int s = -1;
127static int repeat = 0;
128
129char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
130char host_buf[NI_MAXHOST];		/* getnameinfo() */
131char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
132
133int main __P((int, char **));
134int file __P((char *));
135void getsocket __P((void));
136int set __P((int, char **));
137void get __P((char *));
138int delete __P((char *));
139void dump __P((struct in6_addr *));
140static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
141					   int ifindex, int));
142static char *ether_str __P((struct sockaddr_dl *));
143int ndp_ether_aton __P((char *, u_char *));
144void usage __P((void));
145int rtmsg __P((int));
146void ifinfo __P((int, char **));
147void rtrlist __P((void));
148void plist __P((void));
149void pfx_flush __P((void));
150void rtrlist __P((void));
151void rtr_flush __P((void));
152void harmonize_rtr __P((void));
153#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
154static void getdefif __P((void));
155static void setdefif __P((char *));
156#endif
157static char *sec2str __P((time_t t));
158static char *ether_str __P((struct sockaddr_dl *sdl));
159static void ts_print __P((const struct timeval *));
160
161static char *rtpref_str[] = {
162	"medium",		/* 00 */
163	"high",			/* 01 */
164	"rsv",			/* 10 */
165	"low"			/* 11 */
166};
167
168int
169main(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 */
297int
298file(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
330void
331getsocket()
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
342struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
343struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
344struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
345int	expire_time, flags, found_entry;
346struct	{
347	struct	rt_msghdr m_rtm;
348	char	m_space[512];
349}	m_rtmsg;
350
351/*
352 * Set an individual neighbor cache entry
353 */
354int
355set(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
423overwrite:
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 */
436void
437get(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 */
473int
474delete(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
520delete:
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 */
551void
552dump(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
576again:;
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
745static struct in6_nbrinfo *
746getnbrinfo(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
771static char *
772ether_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
788int
789ndp_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
806void
807usage()
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
828int
829rtmsg(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;
872doit:
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
891void
892ifinfo(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	printf("linkmtu=%d", ND.linkmtu);
948	printf(", curhlim=%d", ND.chlim);
949	printf(", basereachable=%ds%dms",
950	    ND.basereachable / 1000, ND.basereachable % 1000);
951	printf(", reachable=%ds", ND.reachable);
952	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
953#ifdef IPV6CTL_USETEMPADDR
954	memset(nullbuf, 0, sizeof(nullbuf));
955	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
956		int j;
957		u_int8_t *rbuf;
958
959		for (i = 0; i < 3; i++) {
960			switch (i) {
961			case 0:
962				printf("\nRandom seed(0): ");
963				rbuf = ND.randomseed0;
964				break;
965			case 1:
966				printf("\nRandom seed(1): ");
967				rbuf = ND.randomseed1;
968				break;
969			case 2:
970				printf("\nRandom ID:      ");
971				rbuf = ND.randomid;
972				break;
973			}
974			for (j = 0; j < 8; j++)
975				printf("%02x", rbuf[j]);
976		}
977	}
978#endif
979	if (ND.flags) {
980		printf("\nFlags: ");
981		if ((ND.flags & ND6_IFF_PERFORMNUD))
982			printf("nud ");
983#ifdef ND6_IFF_ACCEPT_RTADV
984		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
985			printf("accept_rtadv ");
986#endif
987}
988	putc('\n', stdout);
989#undef ND
990
991	close(s);
992}
993
994#ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
995#define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
996#endif
997
998void
999rtrlist()
1000{
1001#ifdef ICMPV6CTL_ND6_DRLIST
1002	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1003	char *buf;
1004	struct in6_defrouter *p, *ep;
1005	size_t l;
1006	struct timeval time;
1007
1008	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1009		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1010		/*NOTREACHED*/
1011	}
1012	buf = malloc(l);
1013	if (!buf) {
1014		err(1, "malloc");
1015		/*NOTREACHED*/
1016	}
1017	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1018		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1019		/*NOTREACHED*/
1020	}
1021
1022	ep = (struct in6_defrouter *)(buf + l);
1023	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1024		int rtpref;
1025
1026		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1027		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1028		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1029			strlcpy(host_buf, "?", sizeof(host_buf));
1030
1031		printf("%s if=%s", host_buf,
1032		    if_indextoname(p->if_index, ifix_buf));
1033		printf(", flags=%s%s",
1034		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1035		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1036		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1037		printf(", pref=%s", rtpref_str[rtpref]);
1038
1039		gettimeofday(&time, 0);
1040		if (p->expire == 0)
1041			printf(", expire=Never\n");
1042		else
1043			printf(", expire=%s\n",
1044			    sec2str(p->expire - time.tv_sec));
1045	}
1046	free(buf);
1047#else
1048	struct in6_drlist dr;
1049	int s, i;
1050	struct timeval time;
1051
1052	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1053		err(1, "socket");
1054		/* NOTREACHED */
1055	}
1056	bzero(&dr, sizeof(dr));
1057	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1058	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1059		err(1, "ioctl(SIOCGDRLST_IN6)");
1060		/* NOTREACHED */
1061	}
1062#define DR dr.defrouter[i]
1063	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1064		struct sockaddr_in6 sin6;
1065
1066		bzero(&sin6, sizeof(sin6));
1067		sin6.sin6_family = AF_INET6;
1068		sin6.sin6_len = sizeof(sin6);
1069		sin6.sin6_addr = DR.rtaddr;
1070		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1071		    sizeof(host_buf), NULL, 0,
1072		    (nflag ? NI_NUMERICHOST : 0));
1073
1074		printf("%s if=%s", host_buf,
1075		    if_indextoname(DR.if_index, ifix_buf));
1076		printf(", flags=%s%s",
1077		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1078		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1079		gettimeofday(&time, 0);
1080		if (DR.expire == 0)
1081			printf(", expire=Never\n");
1082		else
1083			printf(", expire=%s\n",
1084			    sec2str(DR.expire - time.tv_sec));
1085	}
1086#undef DR
1087	close(s);
1088#endif
1089}
1090
1091void
1092plist()
1093{
1094#ifdef ICMPV6CTL_ND6_PRLIST
1095	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1096	char *buf;
1097	struct in6_prefix *p, *ep, *n;
1098	struct sockaddr_in6 *advrtr;
1099	size_t l;
1100	struct timeval time;
1101	const int niflags = NI_NUMERICHOST;
1102	int ninflags = nflag ? NI_NUMERICHOST : 0;
1103	char namebuf[NI_MAXHOST];
1104
1105	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1106		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1107		/*NOTREACHED*/
1108	}
1109	buf = malloc(l);
1110	if (!buf) {
1111		err(1, "malloc");
1112		/*NOTREACHED*/
1113	}
1114	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1115		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1116		/*NOTREACHED*/
1117	}
1118
1119	ep = (struct in6_prefix *)(buf + l);
1120	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1121		advrtr = (struct sockaddr_in6 *)(p + 1);
1122		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1123
1124		if (getnameinfo((struct sockaddr *)&p->prefix,
1125		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1126		    NULL, 0, niflags) != 0)
1127			strlcpy(namebuf, "?", sizeof(namebuf));
1128		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1129		    if_indextoname(p->if_index, ifix_buf));
1130
1131		gettimeofday(&time, 0);
1132		/*
1133		 * meaning of fields, especially flags, is very different
1134		 * by origin.  notify the difference to the users.
1135		 */
1136		printf("flags=%s%s%s%s%s",
1137		    p->raflags.onlink ? "L" : "",
1138		    p->raflags.autonomous ? "A" : "",
1139		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1140		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1141#ifdef NDPRF_HOME
1142		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1143#else
1144		    ""
1145#endif
1146		    );
1147		if (p->vltime == ND6_INFINITE_LIFETIME)
1148			printf(" vltime=infinity");
1149		else
1150			printf(" vltime=%ld", (long)p->vltime);
1151		if (p->pltime == ND6_INFINITE_LIFETIME)
1152			printf(", pltime=infinity");
1153		else
1154			printf(", pltime=%ld", (long)p->pltime);
1155		if (p->expire == 0)
1156			printf(", expire=Never");
1157		else if (p->expire >= time.tv_sec)
1158			printf(", expire=%s",
1159			    sec2str(p->expire - time.tv_sec));
1160		else
1161			printf(", expired");
1162		printf(", ref=%d", p->refcnt);
1163		printf("\n");
1164		/*
1165		 * "advertising router" list is meaningful only if the prefix
1166		 * information is from RA.
1167		 */
1168		if (p->advrtrs) {
1169			int j;
1170			struct sockaddr_in6 *sin6;
1171
1172			sin6 = (struct sockaddr_in6 *)(p + 1);
1173			printf("  advertised by\n");
1174			for (j = 0; j < p->advrtrs; j++) {
1175				struct in6_nbrinfo *nbi;
1176
1177				if (getnameinfo((struct sockaddr *)sin6,
1178				    sin6->sin6_len, namebuf, sizeof(namebuf),
1179				    NULL, 0, ninflags) != 0)
1180					strlcpy(namebuf, "?", sizeof(namebuf));
1181				printf("    %s", namebuf);
1182
1183				nbi = getnbrinfo(&sin6->sin6_addr,
1184				    p->if_index, 0);
1185				if (nbi) {
1186					switch (nbi->state) {
1187					case ND6_LLINFO_REACHABLE:
1188					case ND6_LLINFO_STALE:
1189					case ND6_LLINFO_DELAY:
1190					case ND6_LLINFO_PROBE:
1191						printf(" (reachable)\n");
1192						break;
1193					default:
1194						printf(" (unreachable)\n");
1195					}
1196				} else
1197					printf(" (no neighbor state)\n");
1198				sin6++;
1199			}
1200		} else
1201			printf("  No advertising router\n");
1202	}
1203	free(buf);
1204#else
1205	struct in6_prlist pr;
1206	int s, i;
1207	struct timeval time;
1208
1209	gettimeofday(&time, 0);
1210
1211	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1212		err(1, "socket");
1213		/* NOTREACHED */
1214	}
1215	bzero(&pr, sizeof(pr));
1216	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1217	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1218		err(1, "ioctl(SIOCGPRLST_IN6)");
1219		/* NOTREACHED */
1220	}
1221#define PR pr.prefix[i]
1222	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1223		struct sockaddr_in6 p6;
1224		char namebuf[NI_MAXHOST];
1225		int niflags;
1226
1227#ifdef NDPRF_ONLINK
1228		p6 = PR.prefix;
1229#else
1230		memset(&p6, 0, sizeof(p6));
1231		p6.sin6_family = AF_INET6;
1232		p6.sin6_len = sizeof(p6);
1233		p6.sin6_addr = PR.prefix;
1234#endif
1235
1236		/*
1237		 * copy link index to sin6_scope_id field.
1238		 * XXX: KAME specific.
1239		 */
1240		if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1241			u_int16_t linkid;
1242
1243			memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1244			    sizeof(linkid));
1245			linkid = ntohs(linkid);
1246			p6.sin6_scope_id = linkid;
1247			p6.sin6_addr.s6_addr[2] = 0;
1248			p6.sin6_addr.s6_addr[3] = 0;
1249		}
1250
1251		niflags = NI_NUMERICHOST;
1252		if (getnameinfo((struct sockaddr *)&p6,
1253		    sizeof(p6), namebuf, sizeof(namebuf),
1254		    NULL, 0, niflags)) {
1255			warnx("getnameinfo failed");
1256			continue;
1257		}
1258		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1259		    if_indextoname(PR.if_index, ifix_buf));
1260
1261		gettimeofday(&time, 0);
1262		/*
1263		 * meaning of fields, especially flags, is very different
1264		 * by origin.  notify the difference to the users.
1265		 */
1266#if 0
1267		printf("  %s",
1268		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1269#endif
1270#ifdef NDPRF_ONLINK
1271		printf("flags=%s%s%s%s%s",
1272		    PR.raflags.onlink ? "L" : "",
1273		    PR.raflags.autonomous ? "A" : "",
1274		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1275		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1276#ifdef NDPRF_HOME
1277		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1278#else
1279		    ""
1280#endif
1281		    );
1282#else
1283		printf("flags=%s%s",
1284		    PR.raflags.onlink ? "L" : "",
1285		    PR.raflags.autonomous ? "A" : "");
1286#endif
1287		if (PR.vltime == ND6_INFINITE_LIFETIME)
1288			printf(" vltime=infinity");
1289		else
1290			printf(" vltime=%ld", (long)PR.vltime);
1291		if (PR.pltime == ND6_INFINITE_LIFETIME)
1292			printf(", pltime=infinity");
1293		else
1294			printf(", pltime=%ld", (long)PR.pltime);
1295		if (PR.expire == 0)
1296			printf(", expire=Never");
1297		else if (PR.expire >= time.tv_sec)
1298			printf(", expire=%s",
1299			    sec2str(PR.expire - time.tv_sec));
1300		else
1301			printf(", expired");
1302#ifdef NDPRF_ONLINK
1303		printf(", ref=%d", PR.refcnt);
1304#endif
1305#if 0
1306		switch (PR.origin) {
1307		case PR_ORIG_RA:
1308			printf(", origin=RA");
1309			break;
1310		case PR_ORIG_RR:
1311			printf(", origin=RR");
1312			break;
1313		case PR_ORIG_STATIC:
1314			printf(", origin=static");
1315			break;
1316		case PR_ORIG_KERNEL:
1317			printf(", origin=kernel");
1318			break;
1319		default:
1320			printf(", origin=?");
1321			break;
1322		}
1323#endif
1324		printf("\n");
1325		/*
1326		 * "advertising router" list is meaningful only if the prefix
1327		 * information is from RA.
1328		 */
1329		if (0 &&	/* prefix origin is almost obsolted */
1330		    PR.origin != PR_ORIG_RA)
1331			;
1332		else if (PR.advrtrs) {
1333			int j;
1334			printf("  advertised by\n");
1335			for (j = 0; j < PR.advrtrs; j++) {
1336				struct sockaddr_in6 sin6;
1337				struct in6_nbrinfo *nbi;
1338
1339				bzero(&sin6, sizeof(sin6));
1340				sin6.sin6_family = AF_INET6;
1341				sin6.sin6_len = sizeof(sin6);
1342				sin6.sin6_addr = PR.advrtr[j];
1343				sin6.sin6_scope_id = PR.if_index; /* XXX */
1344				getnameinfo((struct sockaddr *)&sin6,
1345				    sin6.sin6_len, host_buf,
1346				    sizeof(host_buf), NULL, 0,
1347				    (nflag ? NI_NUMERICHOST : 0));
1348				printf("    %s", host_buf);
1349
1350				nbi = getnbrinfo(&sin6.sin6_addr,
1351				    PR.if_index, 0);
1352				if (nbi) {
1353					switch (nbi->state) {
1354					case ND6_LLINFO_REACHABLE:
1355					case ND6_LLINFO_STALE:
1356					case ND6_LLINFO_DELAY:
1357					case ND6_LLINFO_PROBE:
1358						 printf(" (reachable)\n");
1359						 break;
1360					default:
1361						 printf(" (unreachable)\n");
1362					}
1363				} else
1364					printf(" (no neighbor state)\n");
1365			}
1366			if (PR.advrtrs > DRLSTSIZ)
1367				printf("    and %d routers\n",
1368				    PR.advrtrs - DRLSTSIZ);
1369		} else
1370			printf("  No advertising router\n");
1371	}
1372#undef PR
1373	close(s);
1374#endif
1375}
1376
1377void
1378pfx_flush()
1379{
1380	char dummyif[IFNAMSIZ+8];
1381	int s;
1382
1383	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1384		err(1, "socket");
1385	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1386	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1387		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1388}
1389
1390void
1391rtr_flush()
1392{
1393	char dummyif[IFNAMSIZ+8];
1394	int s;
1395
1396	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1397		err(1, "socket");
1398	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1399	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1400		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1401
1402	close(s);
1403}
1404
1405void
1406harmonize_rtr()
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	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1414	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1415		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1416
1417	close(s);
1418}
1419
1420#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1421static void
1422setdefif(ifname)
1423	char *ifname;
1424{
1425	struct in6_ndifreq ndifreq;
1426	unsigned int ifindex;
1427
1428	if (strcasecmp(ifname, "delete") == 0)
1429		ifindex = 0;
1430	else {
1431		if ((ifindex = if_nametoindex(ifname)) == 0)
1432			err(1, "failed to resolve i/f index for %s", ifname);
1433	}
1434
1435	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1436		err(1, "socket");
1437
1438	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1439	ndifreq.ifindex = ifindex;
1440
1441	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1442		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1443
1444	close(s);
1445}
1446
1447static void
1448getdefif()
1449{
1450	struct in6_ndifreq ndifreq;
1451	char ifname[IFNAMSIZ+8];
1452
1453	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1454		err(1, "socket");
1455
1456	memset(&ndifreq, 0, sizeof(ndifreq));
1457	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1458
1459	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1460		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1461
1462	if (ndifreq.ifindex == 0)
1463		printf("No default interface.\n");
1464	else {
1465		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1466			err(1, "failed to resolve ifname for index %lu",
1467			    ndifreq.ifindex);
1468		printf("ND default interface = %s\n", ifname);
1469	}
1470
1471	close(s);
1472}
1473#endif
1474
1475static char *
1476sec2str(total)
1477	time_t total;
1478{
1479	static char result[256];
1480	int days, hours, mins, secs;
1481	int first = 1;
1482	char *p = result;
1483	char *ep = &result[sizeof(result)];
1484	int n;
1485
1486	days = total / 3600 / 24;
1487	hours = (total / 3600) % 24;
1488	mins = (total / 60) % 60;
1489	secs = total % 60;
1490
1491	if (days) {
1492		first = 0;
1493		n = snprintf(p, ep - p, "%dd", days);
1494		if (n < 0 || n >= ep - p)
1495			return "?";
1496		p += n;
1497	}
1498	if (!first || hours) {
1499		first = 0;
1500		n = snprintf(p, ep - p, "%dh", hours);
1501		if (n < 0 || n >= ep - p)
1502			return "?";
1503		p += n;
1504	}
1505	if (!first || mins) {
1506		first = 0;
1507		n = snprintf(p, ep - p, "%dm", mins);
1508		if (n < 0 || n >= ep - p)
1509			return "?";
1510		p += n;
1511	}
1512	snprintf(p, ep - p, "%ds", secs);
1513
1514	return(result);
1515}
1516
1517/*
1518 * Print the timestamp
1519 * from tcpdump/util.c
1520 */
1521static void
1522ts_print(tvp)
1523	const struct timeval *tvp;
1524{
1525	int s;
1526
1527	/* Default */
1528	s = (tvp->tv_sec + thiszone) % 86400;
1529	(void)printf("%02d:%02d:%02d.%06u ",
1530	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1531}
1532