ndp.c revision 259171
162590Sitojun/*	$FreeBSD: head/usr.sbin/ndp/ndp.c 259171 2013-12-10 13:34:28Z ae $	*/
2122615Sume/*	$KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $	*/
362590Sitojun
455505Sshin/*
555505Sshin * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
655505Sshin * All rights reserved.
755505Sshin *
855505Sshin * Redistribution and use in source and binary forms, with or without
955505Sshin * modification, are permitted provided that the following conditions
1055505Sshin * are met:
1155505Sshin * 1. Redistributions of source code must retain the above copyright
1255505Sshin *    notice, this list of conditions and the following disclaimer.
1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright
1455505Sshin *    notice, this list of conditions and the following disclaimer in the
1555505Sshin *    documentation and/or other materials provided with the distribution.
1655505Sshin * 3. Neither the name of the project nor the names of its contributors
1755505Sshin *    may be used to endorse or promote products derived from this software
1855505Sshin *    without specific prior written permission.
1955505Sshin *
2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2355505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3055505Sshin * SUCH DAMAGE.
3155505Sshin */
3255505Sshin/*
3355505Sshin * Copyright (c) 1984, 1993
3455505Sshin *	The Regents of the University of California.  All rights reserved.
3555505Sshin *
3655505Sshin * This code is derived from software contributed to Berkeley by
3755505Sshin * Sun Microsystems, Inc.
3855505Sshin *
3955505Sshin * Redistribution and use in source and binary forms, with or without
4055505Sshin * modification, are permitted provided that the following conditions
4155505Sshin * are met:
4255505Sshin * 1. Redistributions of source code must retain the above copyright
4355505Sshin *    notice, this list of conditions and the following disclaimer.
4455505Sshin * 2. Redistributions in binary form must reproduce the above copyright
4555505Sshin *    notice, this list of conditions and the following disclaimer in the
4655505Sshin *    documentation and/or other materials provided with the distribution.
4755505Sshin * 4. Neither the name of the University nor the names of its contributors
4855505Sshin *    may be used to endorse or promote products derived from this software
4955505Sshin *    without specific prior written permission.
5055505Sshin *
5155505Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5255505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5355505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5455505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5555505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5655505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5755505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5855505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5955505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6055505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6155505Sshin * SUCH DAMAGE.
6255505Sshin */
6355505Sshin
6455505Sshin/*
6555505Sshin * Based on:
6655505Sshin * "@(#) Copyright (c) 1984, 1993\n\
6755505Sshin *	The Regents of the University of California.  All rights reserved.\n";
6855505Sshin *
6955505Sshin * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
7055505Sshin */
7155505Sshin
7255505Sshin/*
7355505Sshin * ndp - display, set, delete and flush neighbor cache
7455505Sshin */
7555505Sshin
7655505Sshin
7755505Sshin#include <sys/param.h>
7855505Sshin#include <sys/file.h>
7955505Sshin#include <sys/ioctl.h>
8055505Sshin#include <sys/socket.h>
8155505Sshin#include <sys/sysctl.h>
82253999Shrs#include <sys/time.h>
8378064Sume#include <sys/queue.h>
8455505Sshin
8555505Sshin#include <net/if.h>
8655505Sshin#include <net/if_var.h>
8755505Sshin#include <net/if_dl.h>
8855505Sshin#include <net/if_types.h>
8955505Sshin#include <net/route.h>
9055505Sshin
9155505Sshin#include <netinet/in.h>
9255505Sshin#include <netinet/if_ether.h>
9355505Sshin
9455505Sshin#include <netinet/icmp6.h>
9555505Sshin#include <netinet6/in6_var.h>
9655505Sshin#include <netinet6/nd6.h>
9755505Sshin
9855505Sshin#include <arpa/inet.h>
9955505Sshin
10055505Sshin#include <netdb.h>
10155505Sshin#include <errno.h>
10255505Sshin#include <nlist.h>
10355505Sshin#include <stdio.h>
10455505Sshin#include <string.h>
10555505Sshin#include <paths.h>
10655505Sshin#include <err.h>
10755505Sshin#include <stdlib.h>
10855505Sshin#include <fcntl.h>
10955505Sshin#include <unistd.h>
11055505Sshin#include "gmt2local.h"
11155505Sshin
112186119Sqingli#define NEXTADDR(w, s) \
113186119Sqingli	if (rtm->rtm_addrs & (w)) { \
114196866Sbz		bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
115186119Sqingli
116186119Sqingli
117100650Sjmallettstatic pid_t pid;
11862590Sitojunstatic int nflag;
11962590Sitojunstatic int tflag;
12062590Sitojunstatic int32_t thiszone;	/* time difference with gmt */
12162590Sitojunstatic int s = -1;
12262590Sitojunstatic int repeat = 0;
12355505Sshin
12462590Sitojunchar ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
12562590Sitojunchar host_buf[NI_MAXHOST];		/* getnameinfo() */
12662590Sitojunchar ifix_buf[IFNAMSIZ];		/* if_indextoname() */
12755505Sshin
128173412Skevloint main(int, char **);
129173412Skevloint file(char *);
130173412Skevlovoid getsocket(void);
131173412Skevloint set(int, char **);
132173412Skevlovoid get(char *);
133173412Skevloint delete(char *);
134173412Skevlovoid dump(struct in6_addr *, int);
135173412Skevlostatic struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
136173412Skevlostatic char *ether_str(struct sockaddr_dl *);
137173412Skevloint ndp_ether_aton(char *, u_char *);
138173412Skevlovoid usage(void);
139173412Skevloint rtmsg(int);
140173412Skevlovoid ifinfo(char *, int, char **);
141173412Skevlovoid rtrlist(void);
142173412Skevlovoid plist(void);
143173412Skevlovoid pfx_flush(void);
144173412Skevlovoid rtr_flush(void);
145173412Skevlovoid harmonize_rtr(void);
14662590Sitojun#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
147173412Skevlostatic void getdefif(void);
148173412Skevlostatic void setdefif(char *);
14962590Sitojun#endif
150173412Skevlostatic char *sec2str(time_t);
151253999Shrsstatic void ts_print(const struct timeval *);
15255505Sshin
153122615Sume#ifdef ICMPV6CTL_ND6_DRLIST
15478064Sumestatic char *rtpref_str[] = {
15578064Sume	"medium",		/* 00 */
15678064Sume	"high",			/* 01 */
15778064Sume	"rsv",			/* 10 */
15878064Sume	"low"			/* 11 */
15978064Sume};
160122615Sume#endif
16178064Sume
162122615Sumeint mode = 0;
163122615Sumechar *arg = NULL;
164122615Sume
16555505Sshinint
166259169Saemain(int argc, char **argv)
16755505Sshin{
16855505Sshin	int ch;
16955505Sshin
17055505Sshin	pid = getpid();
17155505Sshin	thiszone = gmt2local(0);
172122615Sume	while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
173121156Sume		switch (ch) {
17455505Sshin		case 'a':
17555505Sshin		case 'c':
176122615Sume		case 'p':
177122615Sume		case 'r':
178122615Sume		case 'H':
179122615Sume		case 'P':
180122615Sume		case 'R':
181122615Sume		case 's':
182122615Sume		case 'I':
183122615Sume			if (mode) {
184122615Sume				usage();
185122615Sume				/*NOTREACHED*/
186122615Sume			}
187122615Sume			mode = ch;
188122615Sume			arg = NULL;
18955505Sshin			break;
19055505Sshin		case 'd':
191122615Sume		case 'f':
19255505Sshin		case 'i' :
193122615Sume			if (mode) {
19455505Sshin				usage();
195122615Sume				/*NOTREACHED*/
196122615Sume			}
197122615Sume			mode = ch;
198122615Sume			arg = optarg;
199122615Sume			break;
20055505Sshin		case 'n':
20155505Sshin			nflag = 1;
20255505Sshin			break;
20355505Sshin		case 't':
20455505Sshin			tflag = 1;
20555505Sshin			break;
20655505Sshin		case 'A':
207122615Sume			if (mode) {
208122615Sume				usage();
209122615Sume				/*NOTREACHED*/
210122615Sume			}
211122615Sume			mode = 'a';
21255505Sshin			repeat = atoi(optarg);
213122615Sume			if (repeat < 0) {
21455505Sshin				usage();
215122615Sume				/*NOTREACHED*/
216122615Sume			}
21755505Sshin			break;
21855505Sshin		default:
21955505Sshin			usage();
22055505Sshin		}
22155505Sshin
22255505Sshin	argc -= optind;
22355505Sshin	argv += optind;
22455505Sshin
225122615Sume	switch (mode) {
226122615Sume	case 'a':
227122615Sume	case 'c':
228122615Sume		if (argc != 0) {
22955505Sshin			usage();
230122615Sume			/*NOTREACHED*/
231122615Sume		}
232122615Sume		dump(0, mode == 'c');
233122615Sume		break;
234122615Sume	case 'd':
235122615Sume		if (argc != 0) {
236122615Sume			usage();
237122615Sume			/*NOTREACHED*/
238122615Sume		}
239122615Sume		delete(arg);
240122615Sume		break;
241122615Sume	case 'I':
242122615Sume#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
243122615Sume		if (argc > 1) {
244122615Sume			usage();
245122615Sume			/*NOTREACHED*/
246122615Sume		} else if (argc == 1) {
247122615Sume			if (strcmp(*argv, "delete") == 0 ||
248122615Sume			    if_nametoindex(*argv))
249122615Sume				setdefif(*argv);
250122615Sume			else
251122615Sume				errx(1, "invalid interface %s", *argv);
252122615Sume		}
253122615Sume		getdefif(); /* always call it to print the result */
254122615Sume		break;
255122615Sume#else
256122615Sume		errx(1, "not supported yet");
257122615Sume		/*NOTREACHED*/
258122615Sume#endif
259122615Sume	case 'p':
260122615Sume		if (argc != 0) {
261122615Sume			usage();
262122615Sume			/*NOTREACHED*/
263122615Sume		}
26455505Sshin		plist();
265122615Sume		break;
266122615Sume	case 'i':
267122615Sume		ifinfo(arg, argc, argv);
268122615Sume		break;
269122615Sume	case 'r':
270122615Sume		if (argc != 0) {
271122615Sume			usage();
272122615Sume			/*NOTREACHED*/
273122615Sume		}
27455505Sshin		rtrlist();
275122615Sume		break;
276122615Sume	case 's':
27755505Sshin		if (argc < 2 || argc > 4)
27855505Sshin			usage();
27955505Sshin		exit(set(argc, argv) ? 1 : 0);
280122615Sume	case 'H':
281122615Sume		if (argc != 0) {
282122615Sume			usage();
283122615Sume			/*NOTREACHED*/
284122615Sume		}
28555505Sshin		harmonize_rtr();
286122615Sume		break;
287122615Sume	case 'P':
288122615Sume		if (argc != 0) {
289122615Sume			usage();
290122615Sume			/*NOTREACHED*/
291122615Sume		}
29255505Sshin		pfx_flush();
293122615Sume		break;
294122615Sume	case 'R':
295122615Sume		if (argc != 0) {
296122615Sume			usage();
297122615Sume			/*NOTREACHED*/
298122615Sume		}
29955505Sshin		rtr_flush();
300122615Sume		break;
301122615Sume	case 0:
302122615Sume		if (argc != 1) {
303122615Sume			usage();
304122615Sume			/*NOTREACHED*/
305122615Sume		}
306122615Sume		get(argv[0]);
307122615Sume		break;
30855505Sshin	}
30955505Sshin	exit(0);
31055505Sshin}
31155505Sshin
31255505Sshin/*
31355505Sshin * Process a file to set standard ndp entries
31455505Sshin */
31555505Sshinint
316259169Saefile(char *name)
31755505Sshin{
31855505Sshin	FILE *fp;
31955505Sshin	int i, retval;
32055505Sshin	char line[100], arg[5][50], *args[5];
32155505Sshin
32255505Sshin	if ((fp = fopen(name, "r")) == NULL) {
32355505Sshin		fprintf(stderr, "ndp: cannot open %s\n", name);
32455505Sshin		exit(1);
32555505Sshin	}
32655505Sshin	args[0] = &arg[0][0];
32755505Sshin	args[1] = &arg[1][0];
32855505Sshin	args[2] = &arg[2][0];
32955505Sshin	args[3] = &arg[3][0];
33055505Sshin	args[4] = &arg[4][0];
33155505Sshin	retval = 0;
332167260Skevlo	while (fgets(line, sizeof(line), fp) != NULL) {
333122615Sume		i = sscanf(line, "%49s %49s %49s %49s %49s",
334122615Sume		    arg[0], arg[1], arg[2], arg[3], arg[4]);
33555505Sshin		if (i < 2) {
33655505Sshin			fprintf(stderr, "ndp: bad line: %s\n", line);
33755505Sshin			retval = 1;
33855505Sshin			continue;
33955505Sshin		}
34055505Sshin		if (set(i, args))
34155505Sshin			retval = 1;
34255505Sshin	}
34355505Sshin	fclose(fp);
34455505Sshin	return (retval);
34555505Sshin}
34655505Sshin
34755505Sshinvoid
34855505Sshingetsocket()
34955505Sshin{
35055505Sshin	if (s < 0) {
35155505Sshin		s = socket(PF_ROUTE, SOCK_RAW, 0);
35255505Sshin		if (s < 0) {
353121156Sume			err(1, "socket");
354121156Sume			/* NOTREACHED */
35555505Sshin		}
35655505Sshin	}
35755505Sshin}
35855505Sshin
35962590Sitojunstruct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
36055505Sshinstruct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
36155505Sshinstruct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
362253999Shrstime_t	expire_time;
363253999Shrsint	flags, found_entry;
36455505Sshinstruct	{
36555505Sshin	struct	rt_msghdr m_rtm;
36655505Sshin	char	m_space[512];
36755505Sshin}	m_rtmsg;
36855505Sshin
36955505Sshin/*
37055505Sshin * Set an individual neighbor cache entry
37155505Sshin */
37255505Sshinint
373259169Saeset(int argc, char **argv)
37455505Sshin{
37555505Sshin	register struct sockaddr_in6 *sin = &sin_m;
37655505Sshin	register struct sockaddr_dl *sdl;
37755505Sshin	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
37855505Sshin	struct addrinfo hints, *res;
37955505Sshin	int gai_error;
38055505Sshin	u_char *ea;
38155505Sshin	char *host = argv[0], *eaddr = argv[1];
38255505Sshin
38355505Sshin	getsocket();
38455505Sshin	argc -= 2;
38555505Sshin	argv += 2;
38655505Sshin	sdl_m = blank_sdl;
38755505Sshin	sin_m = blank_sin;
38855505Sshin
38955505Sshin	bzero(&hints, sizeof(hints));
39055505Sshin	hints.ai_family = AF_INET6;
39155505Sshin	gai_error = getaddrinfo(host, NULL, &hints, &res);
39255505Sshin	if (gai_error) {
39355505Sshin		fprintf(stderr, "ndp: %s: %s\n", host,
39455505Sshin			gai_strerror(gai_error));
39555505Sshin		return 1;
39655505Sshin	}
39755505Sshin	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
398243903Shrs	sin->sin6_scope_id =
399243903Shrs	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
40055505Sshin	ea = (u_char *)LLADDR(&sdl_m);
40155505Sshin	if (ndp_ether_aton(eaddr, ea) == 0)
40255505Sshin		sdl_m.sdl_alen = 6;
40355505Sshin	flags = expire_time = 0;
40455505Sshin	while (argc-- > 0) {
40555505Sshin		if (strncmp(argv[0], "temp", 4) == 0) {
406253999Shrs			struct timeval now;
407121156Sume
408253999Shrs			gettimeofday(&now, 0);
409253970Shrs			expire_time = now.tv_sec + 20 * 60;
41062590Sitojun		} else if (strncmp(argv[0], "proxy", 5) == 0)
41162590Sitojun			flags |= RTF_ANNOUNCE;
41255505Sshin		argv++;
41355505Sshin	}
41455505Sshin	if (rtmsg(RTM_GET) < 0) {
415121156Sume		errx(1, "RTM_GET(%s) failed", host);
416121156Sume		/* NOTREACHED */
41755505Sshin	}
41855505Sshin	sin = (struct sockaddr_in6 *)(rtm + 1);
419259171Sae	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
42055505Sshin	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
42155505Sshin		if (sdl->sdl_family == AF_LINK &&
422122615Sume		    !(rtm->rtm_flags & RTF_GATEWAY)) {
423122615Sume			switch (sdl->sdl_type) {
424122615Sume			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
425122615Sume			case IFT_ISO88024: case IFT_ISO88025:
426210936Sjhb			case IFT_L2VLAN: case IFT_BRIDGE:
427122615Sume				goto overwrite;
428122615Sume			}
42955505Sshin		}
43062590Sitojun		fprintf(stderr, "set: cannot configure a new entry\n");
43162590Sitojun		return 1;
43255505Sshin	}
43362590Sitojun
43455505Sshinoverwrite:
43555505Sshin	if (sdl->sdl_family != AF_LINK) {
43655505Sshin		printf("cannot intuit interface index and type for %s\n", host);
43755505Sshin		return (1);
43855505Sshin	}
43955505Sshin	sdl_m.sdl_type = sdl->sdl_type;
44055505Sshin	sdl_m.sdl_index = sdl->sdl_index;
44155505Sshin	return (rtmsg(RTM_ADD));
44255505Sshin}
44355505Sshin
44455505Sshin/*
44555505Sshin * Display an individual neighbor cache entry
44655505Sshin */
44755505Sshinvoid
448259169Saeget(char *host)
44955505Sshin{
45055505Sshin	struct sockaddr_in6 *sin = &sin_m;
45155505Sshin	struct addrinfo hints, *res;
45255505Sshin	int gai_error;
45355505Sshin
45455505Sshin	sin_m = blank_sin;
45555505Sshin	bzero(&hints, sizeof(hints));
45655505Sshin	hints.ai_family = AF_INET6;
45755505Sshin	gai_error = getaddrinfo(host, NULL, &hints, &res);
45855505Sshin	if (gai_error) {
45955505Sshin		fprintf(stderr, "ndp: %s: %s\n", host,
460121156Sume		    gai_strerror(gai_error));
46155505Sshin		return;
46255505Sshin	}
46355505Sshin	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
464122615Sume	dump(&sin->sin6_addr, 0);
46555505Sshin	if (found_entry == 0) {
46655505Sshin		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
467121156Sume		    sizeof(host_buf), NULL ,0,
468121156Sume		    (nflag ? NI_NUMERICHOST : 0));
46955505Sshin		printf("%s (%s) -- no entry\n", host, host_buf);
47055505Sshin		exit(1);
47155505Sshin	}
47255505Sshin}
47355505Sshin
47455505Sshin/*
47555505Sshin * Delete a neighbor cache entry
47655505Sshin */
47755505Sshinint
478259169Saedelete(char *host)
47955505Sshin{
48055505Sshin	struct sockaddr_in6 *sin = &sin_m;
48155505Sshin	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
482186119Sqingli	register char *cp = m_rtmsg.m_space;
48355505Sshin	struct sockaddr_dl *sdl;
48455505Sshin	struct addrinfo hints, *res;
48555505Sshin	int gai_error;
48655505Sshin
48755505Sshin	getsocket();
48855505Sshin	sin_m = blank_sin;
48955505Sshin
49055505Sshin	bzero(&hints, sizeof(hints));
49155505Sshin	hints.ai_family = AF_INET6;
49255505Sshin	gai_error = getaddrinfo(host, NULL, &hints, &res);
49355505Sshin	if (gai_error) {
49455505Sshin		fprintf(stderr, "ndp: %s: %s\n", host,
495121156Sume		    gai_strerror(gai_error));
49655505Sshin		return 1;
49755505Sshin	}
49855505Sshin	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
499243903Shrs	sin->sin6_scope_id =
500243903Shrs	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
50155505Sshin	if (rtmsg(RTM_GET) < 0) {
502121156Sume		errx(1, "RTM_GET(%s) failed", host);
503121156Sume		/* NOTREACHED */
50455505Sshin	}
50555505Sshin	sin = (struct sockaddr_in6 *)(rtm + 1);
506259171Sae	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
50755505Sshin	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
50855505Sshin		if (sdl->sdl_family == AF_LINK &&
50962590Sitojun		    !(rtm->rtm_flags & RTF_GATEWAY)) {
51078064Sume			goto delete;
51155505Sshin		}
51262590Sitojun		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
51362590Sitojun		return 1;
51455505Sshin	}
51562590Sitojun
51655505Sshindelete:
51755505Sshin	if (sdl->sdl_family != AF_LINK) {
51855505Sshin		printf("cannot locate %s\n", host);
51955505Sshin		return (1);
52055505Sshin	}
521186119Sqingli        /*
522186119Sqingli         * need to reinit the field because it has rt_key
523186119Sqingli         * but we want the actual address
524186119Sqingli         */
525186119Sqingli	NEXTADDR(RTA_DST, sin_m);
526186500Sqingli	rtm->rtm_flags |= RTF_LLDATA;
52755505Sshin	if (rtmsg(RTM_DELETE) == 0) {
528243903Shrs		getnameinfo((struct sockaddr *)sin,
529243903Shrs		    sin->sin6_len, host_buf,
530121156Sume		    sizeof(host_buf), NULL, 0,
531121156Sume		    (nflag ? NI_NUMERICHOST : 0));
53255505Sshin		printf("%s (%s) deleted\n", host, host_buf);
53355505Sshin	}
53455505Sshin
53555505Sshin	return 0;
53655505Sshin}
53755505Sshin
538122615Sume#define W_ADDR	36
53978064Sume#define W_LL	17
54078064Sume#define W_IF	6
54178064Sume
54255505Sshin/*
54355505Sshin * Dump the entire neighbor cache
54455505Sshin */
54555505Sshinvoid
546259169Saedump(struct in6_addr *addr, int cflag)
54755505Sshin{
54855505Sshin	int mib[6];
54955505Sshin	size_t needed;
55062590Sitojun	char *lim, *buf, *next;
55155505Sshin	struct rt_msghdr *rtm;
55255505Sshin	struct sockaddr_in6 *sin;
55355505Sshin	struct sockaddr_dl *sdl;
55455505Sshin	extern int h_errno;
55555505Sshin	struct in6_nbrinfo *nbi;
556253999Shrs	struct timeval now;
55755505Sshin	int addrwidth;
55878064Sume	int llwidth;
55978064Sume	int ifwidth;
56062590Sitojun	char flgbuf[8];
56178064Sume	char *ifname;
56255505Sshin
56355505Sshin	/* Print header */
56466865Ssumikawa	if (!tflag && !cflag)
565122615Sume		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
56678064Sume		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
567122615Sume		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
56855505Sshin
56955505Sshinagain:;
57055505Sshin	mib[0] = CTL_NET;
57155505Sshin	mib[1] = PF_ROUTE;
57255505Sshin	mib[2] = 0;
57355505Sshin	mib[3] = AF_INET6;
57455505Sshin	mib[4] = NET_RT_FLAGS;
575186119Sqingli#ifdef RTF_LLINFO
57655505Sshin	mib[5] = RTF_LLINFO;
577186119Sqingli#else
578186119Sqingli	mib[5] = 0;
579186119Sqingli#endif
58055505Sshin	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
58155505Sshin		err(1, "sysctl(PF_ROUTE estimate)");
58255505Sshin	if (needed > 0) {
58355505Sshin		if ((buf = malloc(needed)) == NULL)
584121156Sume			err(1, "malloc");
58555505Sshin		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
58655505Sshin			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
58755505Sshin		lim = buf + needed;
58855505Sshin	} else
58955505Sshin		buf = lim = NULL;
59055505Sshin
59155505Sshin	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
59255505Sshin		int isrouter = 0, prbs = 0;
59355505Sshin
59455505Sshin		rtm = (struct rt_msghdr *)next;
59555505Sshin		sin = (struct sockaddr_in6 *)(rtm + 1);
596259171Sae		sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
59778064Sume
59878064Sume		/*
59978064Sume		 * Some OSes can produce a route that has the LINK flag but
60078064Sume		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
60178064Sume		 * and BSD/OS, where xx is not the interface identifier on
60278064Sume		 * lo0).  Such routes entry would annoy getnbrinfo() below,
60378064Sume		 * so we skip them.
60478064Sume		 * XXX: such routes should have the GATEWAY flag, not the
605121156Sume		 * LINK flag.  However, there is rotten routing software
60678064Sume		 * that advertises all routes that have the GATEWAY flag.
60778064Sume		 * Thus, KAME kernel intentionally does not set the LINK flag.
60878064Sume		 * What is to be fixed is not ndp, but such routing software
60978064Sume		 * (and the kernel workaround)...
61078064Sume		 */
61178064Sume		if (sdl->sdl_family != AF_LINK)
61278064Sume			continue;
61378064Sume
614122615Sume		if (!(rtm->rtm_flags & RTF_HOST))
615122615Sume			continue;
616122615Sume
61755505Sshin		if (addr) {
61855505Sshin			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
61955505Sshin				continue;
62055505Sshin			found_entry = 1;
62155505Sshin		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
62255505Sshin			continue;
62355505Sshin		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
62455505Sshin		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
62555505Sshin			/* XXX: should scope id be filled in the kernel? */
62655505Sshin			if (sin->sin6_scope_id == 0)
62755505Sshin				sin->sin6_scope_id = sdl->sdl_index;
62855505Sshin		}
62955505Sshin		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
630121156Sume		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
631122615Sume		if (cflag) {
63281366Ssumikawa#ifdef RTF_WASCLONED
63381366Ssumikawa			if (rtm->rtm_flags & RTF_WASCLONED)
63481366Ssumikawa				delete(host_buf);
635122615Sume#elif defined(RTF_CLONED)
636122615Sume			if (rtm->rtm_flags & RTF_CLONED)
637122615Sume				delete(host_buf);
63881366Ssumikawa#else
63966865Ssumikawa			delete(host_buf);
64081366Ssumikawa#endif
64166865Ssumikawa			continue;
64266865Ssumikawa		}
643253999Shrs		gettimeofday(&now, 0);
64455505Sshin		if (tflag)
645253970Shrs			ts_print(&now);
64655505Sshin
64778064Sume		addrwidth = strlen(host_buf);
64878064Sume		if (addrwidth < W_ADDR)
64978064Sume			addrwidth = W_ADDR;
65078064Sume		llwidth = strlen(ether_str(sdl));
65178064Sume		if (W_ADDR + W_LL - addrwidth > llwidth)
65278064Sume			llwidth = W_ADDR + W_LL - addrwidth;
65378064Sume		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
65478064Sume		if (!ifname)
65578064Sume			ifname = "?";
65678064Sume		ifwidth = strlen(ifname);
65778064Sume		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
65878064Sume			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
65955505Sshin
66078064Sume		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
66178064Sume		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
66255505Sshin
66355505Sshin		/* Print neighbor discovery specific informations */
66462590Sitojun		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
66555505Sshin		if (nbi) {
666253970Shrs			if (nbi->expire > now.tv_sec) {
66755505Sshin				printf(" %-9.9s",
668253970Shrs				    sec2str(nbi->expire - now.tv_sec));
66978064Sume			} else if (nbi->expire == 0)
67055505Sshin				printf(" %-9.9s", "permanent");
67155505Sshin			else
67255505Sshin				printf(" %-9.9s", "expired");
67355505Sshin
674121156Sume			switch (nbi->state) {
675121156Sume			case ND6_LLINFO_NOSTATE:
67655505Sshin				 printf(" N");
67755505Sshin				 break;
67878064Sume#ifdef ND6_LLINFO_WAITDELETE
679121156Sume			case ND6_LLINFO_WAITDELETE:
68055505Sshin				 printf(" W");
68155505Sshin				 break;
68278064Sume#endif
683121156Sume			case ND6_LLINFO_INCOMPLETE:
68455505Sshin				 printf(" I");
68555505Sshin				 break;
686121156Sume			case ND6_LLINFO_REACHABLE:
68755505Sshin				 printf(" R");
68855505Sshin				 break;
689121156Sume			case ND6_LLINFO_STALE:
69055505Sshin				 printf(" S");
69155505Sshin				 break;
692121156Sume			case ND6_LLINFO_DELAY:
69355505Sshin				 printf(" D");
69455505Sshin				 break;
695121156Sume			case ND6_LLINFO_PROBE:
69655505Sshin				 printf(" P");
69755505Sshin				 break;
698121156Sume			default:
69955505Sshin				 printf(" ?");
70055505Sshin				 break;
70155505Sshin			}
70255505Sshin
70355505Sshin			isrouter = nbi->isrouter;
70455505Sshin			prbs = nbi->asked;
70578064Sume		} else {
70655505Sshin			warnx("failed to get neighbor information");
70755505Sshin			printf("  ");
70855505Sshin		}
70955505Sshin
71062590Sitojun		/*
71162590Sitojun		 * other flags. R: router, P: proxy, W: ??
71262590Sitojun		 */
71362590Sitojun		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
71462590Sitojun			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
715121156Sume			    isrouter ? "R" : "",
716121156Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
71762590Sitojun		} else {
71862590Sitojun			sin = (struct sockaddr_in6 *)
719121156Sume			    (sdl->sdl_len + (char *)sdl);
720122615Sume#if 0	/* W and P are mystery even for us */
72162590Sitojun			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
722121156Sume			    isrouter ? "R" : "",
723121156Sume			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
724121156Sume			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
725121156Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
726122615Sume#else
727122615Sume			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
728122615Sume			    isrouter ? "R" : "",
729122615Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
730122615Sume#endif
73155505Sshin		}
732122615Sume		printf(" %s", flgbuf);
73355505Sshin
73455505Sshin		if (prbs)
735122615Sume			printf(" %d", prbs);
73655505Sshin
73755505Sshin		printf("\n");
73855505Sshin	}
73978064Sume	if (buf != NULL)
74078064Sume		free(buf);
74155505Sshin
74255505Sshin	if (repeat) {
74355505Sshin		printf("\n");
744125675Ssumikawa		fflush(stdout);
74555505Sshin		sleep(repeat);
74655505Sshin		goto again;
74755505Sshin	}
74855505Sshin}
74955505Sshin
75055505Sshinstatic struct in6_nbrinfo *
751259169Saegetnbrinfo(struct in6_addr *addr, int ifindex, int warning)
75255505Sshin{
75355505Sshin	static struct in6_nbrinfo nbi;
75455505Sshin	int s;
75555505Sshin
75655505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
75755505Sshin		err(1, "socket");
75855505Sshin
75955505Sshin	bzero(&nbi, sizeof(nbi));
76055505Sshin	if_indextoname(ifindex, nbi.ifname);
76155505Sshin	nbi.addr = *addr;
76255505Sshin	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
76362590Sitojun		if (warning)
76462590Sitojun			warn("ioctl(SIOCGNBRINFO_IN6)");
76555505Sshin		close(s);
76655505Sshin		return(NULL);
76755505Sshin	}
76855505Sshin
76955505Sshin	close(s);
77055505Sshin	return(&nbi);
77155505Sshin}
77255505Sshin
77355505Sshinstatic char *
774217140Sdelphijether_str(struct sockaddr_dl *sdl)
77555505Sshin{
776121156Sume	static char hbuf[NI_MAXHOST];
777219819Sjeff	char *cp;
77855505Sshin
779219819Sjeff	if (sdl->sdl_alen == ETHER_ADDR_LEN) {
780217140Sdelphij		strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
781217140Sdelphij		    sizeof(hbuf));
782219819Sjeff	} else if (sdl->sdl_alen) {
783219819Sjeff		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
784219819Sjeff		snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
785219819Sjeff	} else
786121156Sume		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
78755505Sshin
788121156Sume	return(hbuf);
78955505Sshin}
79055505Sshin
79155505Sshinint
792259169Saendp_ether_aton(char *a, u_char *n)
79355505Sshin{
79455505Sshin	int i, o[6];
79555505Sshin
79655505Sshin	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
797121156Sume	    &o[3], &o[4], &o[5]);
79855505Sshin	if (i != 6) {
79955505Sshin		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
80055505Sshin		return (1);
80155505Sshin	}
802121156Sume	for (i = 0; i < 6; i++)
80355505Sshin		n[i] = o[i];
80455505Sshin	return (0);
80555505Sshin}
80655505Sshin
80755505Sshinvoid
80855505Sshinusage()
80955505Sshin{
810122615Sume	printf("usage: ndp [-nt] hostname\n");
811122615Sume	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
81278064Sume	printf("       ndp [-nt] -A wait\n");
813122615Sume	printf("       ndp [-nt] -d hostname\n");
814122615Sume	printf("       ndp [-nt] -f filename\n");
815122615Sume	printf("       ndp [-nt] -i interface [flags...]\n");
81662590Sitojun#ifdef SIOCSDEFIFACE_IN6
817122615Sume	printf("       ndp [-nt] -I [interface|delete]\n");
81862590Sitojun#endif
819122615Sume	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
82055505Sshin	exit(1);
82155505Sshin}
82255505Sshin
82355505Sshinint
824259169Saertmsg(int cmd)
82555505Sshin{
82655505Sshin	static int seq;
82755505Sshin	int rlen;
82855505Sshin	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
82955505Sshin	register char *cp = m_rtmsg.m_space;
83055505Sshin	register int l;
83155505Sshin
83255505Sshin	errno = 0;
83355505Sshin	if (cmd == RTM_DELETE)
83455505Sshin		goto doit;
83555505Sshin	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
83655505Sshin	rtm->rtm_flags = flags;
83755505Sshin	rtm->rtm_version = RTM_VERSION;
83855505Sshin
83955505Sshin	switch (cmd) {
84055505Sshin	default:
84155505Sshin		fprintf(stderr, "ndp: internal wrong cmd\n");
84255505Sshin		exit(1);
84355505Sshin	case RTM_ADD:
84455505Sshin		rtm->rtm_addrs |= RTA_GATEWAY;
845122615Sume		if (expire_time) {
846122615Sume			rtm->rtm_rmx.rmx_expire = expire_time;
847122615Sume			rtm->rtm_inits = RTV_EXPIRE;
848122615Sume		}
849186500Sqingli		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
850151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */
85162590Sitojun		if (rtm->rtm_flags & RTF_ANNOUNCE) {
85262590Sitojun			rtm->rtm_flags &= ~RTF_HOST;
853124241Ssuz			rtm->rtm_addrs |= RTA_NETMASK;
85462590Sitojun		}
855151473Ssuz#endif
85655505Sshin		/* FALLTHROUGH */
85755505Sshin	case RTM_GET:
85855505Sshin		rtm->rtm_addrs |= RTA_DST;
85955505Sshin	}
86055505Sshin
86155505Sshin	NEXTADDR(RTA_DST, sin_m);
86255505Sshin	NEXTADDR(RTA_GATEWAY, sdl_m);
863151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */
86462590Sitojun	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
86555505Sshin	NEXTADDR(RTA_NETMASK, so_mask);
866151473Ssuz#endif
86755505Sshin
86855505Sshin	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
86955505Sshindoit:
87055505Sshin	l = rtm->rtm_msglen;
87155505Sshin	rtm->rtm_seq = ++seq;
87255505Sshin	rtm->rtm_type = cmd;
87355505Sshin	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
87455505Sshin		if (errno != ESRCH || cmd != RTM_DELETE) {
875121156Sume			err(1, "writing to routing socket");
876121156Sume			/* NOTREACHED */
87755505Sshin		}
87855505Sshin	}
87955505Sshin	do {
88055505Sshin		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
88155505Sshin	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
88255505Sshin	if (l < 0)
88355505Sshin		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
88455505Sshin		    strerror(errno));
88555505Sshin	return (0);
88655505Sshin}
88755505Sshin
88855505Sshinvoid
889259169Saeifinfo(char *ifname, int argc, char **argv)
89055505Sshin{
89155505Sshin	struct in6_ndireq nd;
89262590Sitojun	int i, s;
89362590Sitojun	u_int32_t newflags;
89478064Sume#ifdef IPV6CTL_USETEMPADDR
89578064Sume	u_int8_t nullbuf[8];
89678064Sume#endif
89755505Sshin
89855505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
899121156Sume		err(1, "socket");
900121156Sume		/* NOTREACHED */
90155505Sshin	}
90255505Sshin	bzero(&nd, sizeof(nd));
903121156Sume	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
90455505Sshin	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
905121156Sume		err(1, "ioctl(SIOCGIFINFO_IN6)");
906121156Sume		/* NOTREACHED */
907122615Sume	}
90862590Sitojun#define ND nd.ndi
90962590Sitojun	newflags = ND.flags;
910122615Sume	for (i = 0; i < argc; i++) {
91162590Sitojun		int clear = 0;
91262590Sitojun		char *cp = argv[i];
91362590Sitojun
91462590Sitojun		if (*cp == '-') {
91562590Sitojun			clear = 1;
91662590Sitojun			cp++;
91762590Sitojun		}
91862590Sitojun
91962590Sitojun#define SETFLAG(s, f) \
92062590Sitojun	do {\
92162590Sitojun		if (strcmp(cp, (s)) == 0) {\
92262590Sitojun			if (clear)\
92362590Sitojun				newflags &= ~(f);\
92462590Sitojun			else\
92562590Sitojun				newflags |= (f);\
92662590Sitojun		}\
92762590Sitojun	} while (0)
928151468Ssuz/*
929151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against
930151468Ssuz *      "nudbogus".  But we just let it go since this is minor.
931151468Ssuz */
932151468Ssuz#define SETVALUE(f, v) \
933151468Ssuz	do { \
934151468Ssuz		char *valptr; \
935151468Ssuz		unsigned long newval; \
936151468Ssuz		v = 0; /* unspecified */ \
937151468Ssuz		if (strncmp(cp, f, strlen(f)) == 0) { \
938151468Ssuz			valptr = strchr(cp, '='); \
939151468Ssuz			if (valptr == NULL) \
940151468Ssuz				err(1, "syntax error in %s field", (f)); \
941151468Ssuz			errno = 0; \
942151468Ssuz			newval = strtoul(++valptr, NULL, 0); \
943151468Ssuz			if (errno) \
944151468Ssuz				err(1, "syntax error in %s's value", (f)); \
945151468Ssuz			v = newval; \
946151468Ssuz		} \
947151468Ssuz	} while (0)
948151468Ssuz
949151474Ssuz		SETFLAG("disabled", ND6_IFF_IFDISABLED);
95062590Sitojun		SETFLAG("nud", ND6_IFF_PERFORMNUD);
951118498Sume#ifdef ND6_IFF_ACCEPT_RTADV
952118498Sume		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
953118498Sume#endif
954197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL
955197138Shrs		SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
956197138Shrs#endif
957245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE
958245230Sume		SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
959245230Sume#endif
960151468Ssuz		SETVALUE("basereachable", ND.basereachable);
961151468Ssuz		SETVALUE("retrans", ND.retrans);
962151468Ssuz		SETVALUE("curhlim", ND.chlim);
96362590Sitojun
96462590Sitojun		ND.flags = newflags;
965151468Ssuz		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
966151468Ssuz			err(1, "ioctl(SIOCSIFINFO_IN6)");
967121156Sume			/* NOTREACHED */
96862590Sitojun		}
96962590Sitojun#undef SETFLAG
970151468Ssuz#undef SETVALUE
97162590Sitojun	}
97262590Sitojun
973121162Sume	if (!ND.initialized) {
974121162Sume		errx(1, "%s: not initialized yet", ifname);
975121162Sume		/* NOTREACHED */
976121162Sume	}
977121162Sume
978151468Ssuz	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
979151468Ssuz		err(1, "ioctl(SIOCGIFINFO_IN6)");
980151468Ssuz		/* NOTREACHED */
981151468Ssuz	}
98255505Sshin	printf("linkmtu=%d", ND.linkmtu);
983121471Sume	printf(", maxmtu=%d", ND.maxmtu);
98455505Sshin	printf(", curhlim=%d", ND.chlim);
98555505Sshin	printf(", basereachable=%ds%dms",
986121156Sume	    ND.basereachable / 1000, ND.basereachable % 1000);
98755505Sshin	printf(", reachable=%ds", ND.reachable);
98862590Sitojun	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
98978064Sume#ifdef IPV6CTL_USETEMPADDR
99078064Sume	memset(nullbuf, 0, sizeof(nullbuf));
99178064Sume	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
99278064Sume		int j;
99378064Sume		u_int8_t *rbuf;
99478064Sume
99578064Sume		for (i = 0; i < 3; i++) {
996121156Sume			switch (i) {
99778064Sume			case 0:
99878064Sume				printf("\nRandom seed(0): ");
99978064Sume				rbuf = ND.randomseed0;
100078064Sume				break;
100178064Sume			case 1:
100278064Sume				printf("\nRandom seed(1): ");
100378064Sume				rbuf = ND.randomseed1;
100478064Sume				break;
100578064Sume			case 2:
100678064Sume				printf("\nRandom ID:      ");
100778064Sume				rbuf = ND.randomid;
100878064Sume				break;
1009151472Ssuz			default:
1010151472Ssuz				errx(1, "impossible case for tempaddr display");
101178064Sume			}
101278064Sume			for (j = 0; j < 8; j++)
101378064Sume				printf("%02x", rbuf[j]);
101478064Sume		}
101578064Sume	}
101678064Sume#endif
101762590Sitojun	if (ND.flags) {
101862590Sitojun		printf("\nFlags: ");
1019151474Ssuz#ifdef ND6_IFF_IFDISABLED
1020151474Ssuz		if ((ND.flags & ND6_IFF_IFDISABLED))
1021151474Ssuz			printf("disabled ");
1022151474Ssuz#endif
1023118498Sume		if ((ND.flags & ND6_IFF_PERFORMNUD))
1024118498Sume			printf("nud ");
1025118498Sume#ifdef ND6_IFF_ACCEPT_RTADV
1026118498Sume		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1027118498Sume			printf("accept_rtadv ");
1028118498Sume#endif
1029197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL
1030197138Shrs		if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1031197138Shrs			printf("auto_linklocal ");
1032197138Shrs#endif
1033245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE
1034245230Sume		if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1035245230Sume			printf("no_prefer_iface ");
1036245230Sume#endif
1037122615Sume	}
103862590Sitojun	putc('\n', stdout);
103955505Sshin#undef ND
1040121156Sume
104155505Sshin	close(s);
104255505Sshin}
104355505Sshin
104478064Sume#ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
104578064Sume#define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
104678064Sume#endif
104778064Sume
104855505Sshinvoid
104955505Sshinrtrlist()
105055505Sshin{
105178064Sume#ifdef ICMPV6CTL_ND6_DRLIST
105278064Sume	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
105378064Sume	char *buf;
105478064Sume	struct in6_defrouter *p, *ep;
105578064Sume	size_t l;
1056253999Shrs	struct timeval now;
105778064Sume
105878064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
105978064Sume		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
106078064Sume		/*NOTREACHED*/
106178064Sume	}
1062151472Ssuz	if (l == 0)
1063151472Ssuz		return;
106478064Sume	buf = malloc(l);
106578064Sume	if (!buf) {
1066121156Sume		err(1, "malloc");
106778064Sume		/*NOTREACHED*/
106878064Sume	}
106978064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
107078064Sume		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
107178064Sume		/*NOTREACHED*/
107278064Sume	}
107378064Sume
107478064Sume	ep = (struct in6_defrouter *)(buf + l);
107578064Sume	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
107678064Sume		int rtpref;
107778064Sume
107878064Sume		if (getnameinfo((struct sockaddr *)&p->rtaddr,
107978064Sume		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1080121156Sume		    (nflag ? NI_NUMERICHOST : 0)) != 0)
108178064Sume			strlcpy(host_buf, "?", sizeof(host_buf));
1082121156Sume
108378064Sume		printf("%s if=%s", host_buf,
1084121156Sume		    if_indextoname(p->if_index, ifix_buf));
108578064Sume		printf(", flags=%s%s",
1086121156Sume		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1087121156Sume		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
108878064Sume		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
108978064Sume		printf(", pref=%s", rtpref_str[rtpref]);
1090121156Sume
1091253999Shrs		gettimeofday(&now, 0);
109278064Sume		if (p->expire == 0)
109378064Sume			printf(", expire=Never\n");
109478064Sume		else
109578064Sume			printf(", expire=%s\n",
1096253970Shrs			    sec2str(p->expire - now.tv_sec));
109778064Sume	}
109878064Sume	free(buf);
109978064Sume#else
110055505Sshin	struct in6_drlist dr;
110155505Sshin	int s, i;
1102253999Shrs	struct timeval now;
110355505Sshin
110455505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1105121156Sume		err(1, "socket");
1106121156Sume		/* NOTREACHED */
110755505Sshin	}
110855505Sshin	bzero(&dr, sizeof(dr));
1109121156Sume	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
111055505Sshin	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1111121156Sume		err(1, "ioctl(SIOCGDRLST_IN6)");
1112121156Sume		/* NOTREACHED */
1113121156Sume	}
111462590Sitojun#define DR dr.defrouter[i]
111578064Sume	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
111655505Sshin		struct sockaddr_in6 sin6;
111755505Sshin
111855505Sshin		bzero(&sin6, sizeof(sin6));
111955505Sshin		sin6.sin6_family = AF_INET6;
112055505Sshin		sin6.sin6_len = sizeof(sin6);
112155505Sshin		sin6.sin6_addr = DR.rtaddr;
112255505Sshin		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1123121156Sume		    sizeof(host_buf), NULL, 0,
1124121156Sume		    (nflag ? NI_NUMERICHOST : 0));
1125121156Sume
112655505Sshin		printf("%s if=%s", host_buf,
1127121156Sume		    if_indextoname(DR.if_index, ifix_buf));
112855505Sshin		printf(", flags=%s%s",
1129121156Sume		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1130121156Sume		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1131253999Shrs		gettimeofday(&now, 0);
113255505Sshin		if (DR.expire == 0)
113355505Sshin			printf(", expire=Never\n");
113455505Sshin		else
113555505Sshin			printf(", expire=%s\n",
1136253970Shrs			    sec2str(DR.expire - now.tv_sec));
113755505Sshin	}
113855505Sshin#undef DR
113955505Sshin	close(s);
114078064Sume#endif
114155505Sshin}
114255505Sshin
114355505Sshinvoid
114455505Sshinplist()
114555505Sshin{
114678064Sume#ifdef ICMPV6CTL_ND6_PRLIST
114778064Sume	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
114878064Sume	char *buf;
114978064Sume	struct in6_prefix *p, *ep, *n;
115078064Sume	struct sockaddr_in6 *advrtr;
115178064Sume	size_t l;
1152253999Shrs	struct timeval now;
115378064Sume	const int niflags = NI_NUMERICHOST;
115478064Sume	int ninflags = nflag ? NI_NUMERICHOST : 0;
115578064Sume	char namebuf[NI_MAXHOST];
115678064Sume
115778064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
115878064Sume		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
115978064Sume		/*NOTREACHED*/
116078064Sume	}
116178064Sume	buf = malloc(l);
116278064Sume	if (!buf) {
1163121156Sume		err(1, "malloc");
116478064Sume		/*NOTREACHED*/
116578064Sume	}
116678064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
116778064Sume		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
116878064Sume		/*NOTREACHED*/
116978064Sume	}
117078064Sume
117178064Sume	ep = (struct in6_prefix *)(buf + l);
117278064Sume	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
117378064Sume		advrtr = (struct sockaddr_in6 *)(p + 1);
117478064Sume		n = (struct in6_prefix *)&advrtr[p->advrtrs];
117578064Sume
117678064Sume		if (getnameinfo((struct sockaddr *)&p->prefix,
117778064Sume		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
117878064Sume		    NULL, 0, niflags) != 0)
117978064Sume			strlcpy(namebuf, "?", sizeof(namebuf));
118078064Sume		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1181121156Sume		    if_indextoname(p->if_index, ifix_buf));
118278064Sume
1183253999Shrs		gettimeofday(&now, 0);
118478064Sume		/*
118578064Sume		 * meaning of fields, especially flags, is very different
118678064Sume		 * by origin.  notify the difference to the users.
118778064Sume		 */
118878064Sume		printf("flags=%s%s%s%s%s",
1189121156Sume		    p->raflags.onlink ? "L" : "",
1190121156Sume		    p->raflags.autonomous ? "A" : "",
1191121156Sume		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1192121156Sume		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
119378064Sume#ifdef NDPRF_HOME
1194121156Sume		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
119578064Sume#else
1196121156Sume		    ""
119778064Sume#endif
1198121156Sume		    );
119978064Sume		if (p->vltime == ND6_INFINITE_LIFETIME)
120078064Sume			printf(" vltime=infinity");
120178064Sume		else
1202122615Sume			printf(" vltime=%lu", (unsigned long)p->vltime);
120378064Sume		if (p->pltime == ND6_INFINITE_LIFETIME)
120478064Sume			printf(", pltime=infinity");
120578064Sume		else
1206122615Sume			printf(", pltime=%lu", (unsigned long)p->pltime);
120778064Sume		if (p->expire == 0)
120878064Sume			printf(", expire=Never");
1209253970Shrs		else if (p->expire >= now.tv_sec)
121078064Sume			printf(", expire=%s",
1211253970Shrs			    sec2str(p->expire - now.tv_sec));
121278064Sume		else
121378064Sume			printf(", expired");
121478064Sume		printf(", ref=%d", p->refcnt);
121578064Sume		printf("\n");
121678064Sume		/*
121778064Sume		 * "advertising router" list is meaningful only if the prefix
121878064Sume		 * information is from RA.
121978064Sume		 */
122078064Sume		if (p->advrtrs) {
122178064Sume			int j;
122278064Sume			struct sockaddr_in6 *sin6;
122378064Sume
1224122615Sume			sin6 = advrtr;
122578064Sume			printf("  advertised by\n");
122678064Sume			for (j = 0; j < p->advrtrs; j++) {
122778064Sume				struct in6_nbrinfo *nbi;
122878064Sume
122978064Sume				if (getnameinfo((struct sockaddr *)sin6,
123078064Sume				    sin6->sin6_len, namebuf, sizeof(namebuf),
123178064Sume				    NULL, 0, ninflags) != 0)
123278064Sume					strlcpy(namebuf, "?", sizeof(namebuf));
123378064Sume				printf("    %s", namebuf);
123478064Sume
1235121156Sume				nbi = getnbrinfo(&sin6->sin6_addr,
1236121156Sume				    p->if_index, 0);
123778064Sume				if (nbi) {
1238121156Sume					switch (nbi->state) {
123978064Sume					case ND6_LLINFO_REACHABLE:
124078064Sume					case ND6_LLINFO_STALE:
124178064Sume					case ND6_LLINFO_DELAY:
124278064Sume					case ND6_LLINFO_PROBE:
124378064Sume						printf(" (reachable)\n");
124478064Sume						break;
124578064Sume					default:
124678064Sume						printf(" (unreachable)\n");
124778064Sume					}
124878064Sume				} else
124978064Sume					printf(" (no neighbor state)\n");
125078064Sume				sin6++;
125178064Sume			}
125278064Sume		} else
125378064Sume			printf("  No advertising router\n");
125478064Sume	}
125578064Sume	free(buf);
125678064Sume#else
125755505Sshin	struct in6_prlist pr;
125855505Sshin	int s, i;
1259253999Shrs	struct timeval now;
126055505Sshin
1261253999Shrs	gettimeofday(&now, 0);
126255505Sshin
126355505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1264121156Sume		err(1, "socket");
1265121156Sume		/* NOTREACHED */
126655505Sshin	}
126755505Sshin	bzero(&pr, sizeof(pr));
1268121156Sume	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
126955505Sshin	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1270121156Sume		err(1, "ioctl(SIOCGPRLST_IN6)");
1271121156Sume		/* NOTREACHED */
1272121156Sume	}
127362590Sitojun#define PR pr.prefix[i]
127455505Sshin	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
127578064Sume		struct sockaddr_in6 p6;
127678064Sume		char namebuf[NI_MAXHOST];
127778064Sume		int niflags;
127878064Sume
127978064Sume#ifdef NDPRF_ONLINK
128078064Sume		p6 = PR.prefix;
128178064Sume#else
128278064Sume		memset(&p6, 0, sizeof(p6));
128378064Sume		p6.sin6_family = AF_INET6;
128478064Sume		p6.sin6_len = sizeof(p6);
128578064Sume		p6.sin6_addr = PR.prefix;
128678064Sume#endif
128778064Sume		niflags = NI_NUMERICHOST;
128878064Sume		if (getnameinfo((struct sockaddr *)&p6,
1289121156Sume		    sizeof(p6), namebuf, sizeof(namebuf),
1290121156Sume		    NULL, 0, niflags)) {
129178064Sume			warnx("getnameinfo failed");
129278064Sume			continue;
129378064Sume		}
129478064Sume		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1295121156Sume		    if_indextoname(PR.if_index, ifix_buf));
129678064Sume
1297253999Shrs		gettimeofday(&now, 0);
129862590Sitojun		/*
129962590Sitojun		 * meaning of fields, especially flags, is very different
130062590Sitojun		 * by origin.  notify the difference to the users.
130162590Sitojun		 */
130278064Sume#if 0
130378064Sume		printf("  %s",
1304121156Sume		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
130578064Sume#endif
130678064Sume#ifdef NDPRF_ONLINK
130778064Sume		printf("flags=%s%s%s%s%s",
1308121156Sume		    PR.raflags.onlink ? "L" : "",
1309121156Sume		    PR.raflags.autonomous ? "A" : "",
1310121156Sume		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1311121156Sume		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
131278064Sume#ifdef NDPRF_HOME
1313121156Sume		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
131478064Sume#else
1315121156Sume		    ""
131678064Sume#endif
1317121156Sume		    );
131878064Sume#else
131962590Sitojun		printf("flags=%s%s",
1320121156Sume		    PR.raflags.onlink ? "L" : "",
1321121156Sume		    PR.raflags.autonomous ? "A" : "");
132278064Sume#endif
132355505Sshin		if (PR.vltime == ND6_INFINITE_LIFETIME)
132455505Sshin			printf(" vltime=infinity");
132555505Sshin		else
1326122615Sume			printf(" vltime=%lu", PR.vltime);
132755505Sshin		if (PR.pltime == ND6_INFINITE_LIFETIME)
132855505Sshin			printf(", pltime=infinity");
132955505Sshin		else
1330122615Sume			printf(", pltime=%lu", PR.pltime);
133155505Sshin		if (PR.expire == 0)
133262590Sitojun			printf(", expire=Never");
1333253970Shrs		else if (PR.expire >= now.tv_sec)
133462590Sitojun			printf(", expire=%s",
1335253970Shrs			    sec2str(PR.expire - now.tv_sec));
133655505Sshin		else
133762590Sitojun			printf(", expired");
133878064Sume#ifdef NDPRF_ONLINK
133978064Sume		printf(", ref=%d", PR.refcnt);
134078064Sume#endif
134178064Sume#if 0
134262590Sitojun		switch (PR.origin) {
134362590Sitojun		case PR_ORIG_RA:
134462590Sitojun			printf(", origin=RA");
134562590Sitojun			break;
134662590Sitojun		case PR_ORIG_RR:
134762590Sitojun			printf(", origin=RR");
134862590Sitojun			break;
134962590Sitojun		case PR_ORIG_STATIC:
135062590Sitojun			printf(", origin=static");
135162590Sitojun			break;
135262590Sitojun		case PR_ORIG_KERNEL:
135362590Sitojun			printf(", origin=kernel");
135462590Sitojun			break;
135562590Sitojun		default:
135662590Sitojun			printf(", origin=?");
135762590Sitojun			break;
135862590Sitojun		}
135978064Sume#endif
136062590Sitojun		printf("\n");
136162590Sitojun		/*
136262590Sitojun		 * "advertising router" list is meaningful only if the prefix
136362590Sitojun		 * information is from RA.
136462590Sitojun		 */
136578064Sume		if (0 &&	/* prefix origin is almost obsolted */
136678064Sume		    PR.origin != PR_ORIG_RA)
136762590Sitojun			;
136862590Sitojun		else if (PR.advrtrs) {
136955505Sshin			int j;
137055505Sshin			printf("  advertised by\n");
137155505Sshin			for (j = 0; j < PR.advrtrs; j++) {
137255505Sshin				struct sockaddr_in6 sin6;
137362590Sitojun				struct in6_nbrinfo *nbi;
137455505Sshin
137555505Sshin				bzero(&sin6, sizeof(sin6));
137655505Sshin				sin6.sin6_family = AF_INET6;
137755505Sshin				sin6.sin6_len = sizeof(sin6);
137855505Sshin				sin6.sin6_addr = PR.advrtr[j];
137962590Sitojun				sin6.sin6_scope_id = PR.if_index; /* XXX */
138055505Sshin				getnameinfo((struct sockaddr *)&sin6,
1381121156Sume				    sin6.sin6_len, host_buf,
1382121156Sume				    sizeof(host_buf), NULL, 0,
1383121156Sume				    (nflag ? NI_NUMERICHOST : 0));
138462590Sitojun				printf("    %s", host_buf);
138555505Sshin
1386121156Sume				nbi = getnbrinfo(&sin6.sin6_addr,
1387121156Sume				    PR.if_index, 0);
138862590Sitojun				if (nbi) {
1389121156Sume					switch (nbi->state) {
1390121156Sume					case ND6_LLINFO_REACHABLE:
1391121156Sume					case ND6_LLINFO_STALE:
1392121156Sume					case ND6_LLINFO_DELAY:
1393121156Sume					case ND6_LLINFO_PROBE:
139462590Sitojun						 printf(" (reachable)\n");
139562590Sitojun						 break;
1396121156Sume					default:
139762590Sitojun						 printf(" (unreachable)\n");
139862590Sitojun					}
139978064Sume				} else
140062590Sitojun					printf(" (no neighbor state)\n");
140155505Sshin			}
140255505Sshin			if (PR.advrtrs > DRLSTSIZ)
140355505Sshin				printf("    and %d routers\n",
1404121156Sume				    PR.advrtrs - DRLSTSIZ);
140562590Sitojun		} else
140655505Sshin			printf("  No advertising router\n");
140755505Sshin	}
140855505Sshin#undef PR
140955505Sshin	close(s);
141078064Sume#endif
141155505Sshin}
141255505Sshin
141355505Sshinvoid
141455505Sshinpfx_flush()
141555505Sshin{
141655505Sshin	char dummyif[IFNAMSIZ+8];
141755505Sshin	int s;
141855505Sshin
141955505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
142055505Sshin		err(1, "socket");
1421121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
142255505Sshin	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1423121156Sume		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
142455505Sshin}
142555505Sshin
142655505Sshinvoid
142755505Sshinrtr_flush()
142855505Sshin{
142955505Sshin	char dummyif[IFNAMSIZ+8];
143055505Sshin	int s;
143155505Sshin
143255505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
143355505Sshin		err(1, "socket");
1434121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
143555505Sshin	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1436121156Sume		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
143762590Sitojun
143862590Sitojun	close(s);
143955505Sshin}
144055505Sshin
144155505Sshinvoid
144255505Sshinharmonize_rtr()
144355505Sshin{
144455505Sshin	char dummyif[IFNAMSIZ+8];
144555505Sshin	int s;
144655505Sshin
144762590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
144862590Sitojun		err(1, "socket");
1449121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
145062590Sitojun	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1451121156Sume		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
145262590Sitojun
145362590Sitojun	close(s);
145455505Sshin}
145555505Sshin
145662590Sitojun#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
145762590Sitojunstatic void
1458259169Saesetdefif(char *ifname)
145962590Sitojun{
146062590Sitojun	struct in6_ndifreq ndifreq;
146162590Sitojun	unsigned int ifindex;
146262590Sitojun
146362590Sitojun	if (strcasecmp(ifname, "delete") == 0)
146462590Sitojun		ifindex = 0;
146562590Sitojun	else {
146662590Sitojun		if ((ifindex = if_nametoindex(ifname)) == 0)
146762590Sitojun			err(1, "failed to resolve i/f index for %s", ifname);
146862590Sitojun	}
146962590Sitojun
147062590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
147162590Sitojun		err(1, "socket");
147262590Sitojun
1473121156Sume	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
147462590Sitojun	ndifreq.ifindex = ifindex;
147562590Sitojun
147662590Sitojun	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1477121156Sume		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
147862590Sitojun
147962590Sitojun	close(s);
148062590Sitojun}
148162590Sitojun
148262590Sitojunstatic void
148362590Sitojungetdefif()
148462590Sitojun{
148562590Sitojun	struct in6_ndifreq ndifreq;
148662590Sitojun	char ifname[IFNAMSIZ+8];
148762590Sitojun
148862590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
148962590Sitojun		err(1, "socket");
149062590Sitojun
149162590Sitojun	memset(&ndifreq, 0, sizeof(ndifreq));
1492121156Sume	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
149362590Sitojun
149462590Sitojun	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1495121156Sume		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
149662590Sitojun
149762590Sitojun	if (ndifreq.ifindex == 0)
149862590Sitojun		printf("No default interface.\n");
149962590Sitojun	else {
150062590Sitojun		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
150162590Sitojun			err(1, "failed to resolve ifname for index %lu",
150262590Sitojun			    ndifreq.ifindex);
150362590Sitojun		printf("ND default interface = %s\n", ifname);
150462590Sitojun	}
150562590Sitojun
150662590Sitojun	close(s);
150762590Sitojun}
150862590Sitojun#endif
150962590Sitojun
151055505Sshinstatic char *
1511259169Saesec2str(time_t total)
151255505Sshin{
151355505Sshin	static char result[256];
151455505Sshin	int days, hours, mins, secs;
151555505Sshin	int first = 1;
151655505Sshin	char *p = result;
1517121156Sume	char *ep = &result[sizeof(result)];
1518121156Sume	int n;
151955505Sshin
152055505Sshin	days = total / 3600 / 24;
152155505Sshin	hours = (total / 3600) % 24;
152255505Sshin	mins = (total / 60) % 60;
152355505Sshin	secs = total % 60;
152455505Sshin
152555505Sshin	if (days) {
152655505Sshin		first = 0;
1527121156Sume		n = snprintf(p, ep - p, "%dd", days);
1528121156Sume		if (n < 0 || n >= ep - p)
1529121156Sume			return "?";
1530121156Sume		p += n;
153155505Sshin	}
153255505Sshin	if (!first || hours) {
153355505Sshin		first = 0;
1534121156Sume		n = snprintf(p, ep - p, "%dh", hours);
1535121156Sume		if (n < 0 || n >= ep - p)
1536121156Sume			return "?";
1537121156Sume		p += n;
153855505Sshin	}
153955505Sshin	if (!first || mins) {
154055505Sshin		first = 0;
1541121156Sume		n = snprintf(p, ep - p, "%dm", mins);
1542121156Sume		if (n < 0 || n >= ep - p)
1543121156Sume			return "?";
1544121156Sume		p += n;
154555505Sshin	}
1546121156Sume	snprintf(p, ep - p, "%ds", secs);
154755505Sshin
154855505Sshin	return(result);
154955505Sshin}
155055505Sshin
155155505Sshin/*
155255505Sshin * Print the timestamp
155355505Sshin * from tcpdump/util.c
155455505Sshin */
155555505Sshinstatic void
1556259169Saets_print(const struct timeval *tvp)
155755505Sshin{
155855505Sshin	int s;
155955505Sshin
156055505Sshin	/* Default */
1561253999Shrs	s = (tvp->tv_sec + thiszone) % 86400;
156255505Sshin	(void)printf("%02d:%02d:%02d.%06u ",
1563253999Shrs	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
156455505Sshin}
1565186119Sqingli
1566186119Sqingli#undef NEXTADDR
1567