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