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