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