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