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