ndp.c revision 259176
162590Sitojun/*	$FreeBSD: head/usr.sbin/ndp/ndp.c 259176 2013-12-10 14:17:07Z 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 *);
134259176Saevoid dump(struct sockaddr_in6 *, 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;
464259176Sae	sin->sin6_scope_id =
465259176Sae	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
466259176Sae	dump(sin, 0);
46755505Sshin	if (found_entry == 0) {
46855505Sshin		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
469121156Sume		    sizeof(host_buf), NULL ,0,
470121156Sume		    (nflag ? NI_NUMERICHOST : 0));
47155505Sshin		printf("%s (%s) -- no entry\n", host, host_buf);
47255505Sshin		exit(1);
47355505Sshin	}
47455505Sshin}
47555505Sshin
47655505Sshin/*
47755505Sshin * Delete a neighbor cache entry
47855505Sshin */
47955505Sshinint
480259169Saedelete(char *host)
48155505Sshin{
48255505Sshin	struct sockaddr_in6 *sin = &sin_m;
48355505Sshin	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
484186119Sqingli	register char *cp = m_rtmsg.m_space;
48555505Sshin	struct sockaddr_dl *sdl;
48655505Sshin	struct addrinfo hints, *res;
48755505Sshin	int gai_error;
48855505Sshin
48955505Sshin	getsocket();
49055505Sshin	sin_m = blank_sin;
49155505Sshin
49255505Sshin	bzero(&hints, sizeof(hints));
49355505Sshin	hints.ai_family = AF_INET6;
49455505Sshin	gai_error = getaddrinfo(host, NULL, &hints, &res);
49555505Sshin	if (gai_error) {
49655505Sshin		fprintf(stderr, "ndp: %s: %s\n", host,
497121156Sume		    gai_strerror(gai_error));
49855505Sshin		return 1;
49955505Sshin	}
50055505Sshin	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
501243903Shrs	sin->sin6_scope_id =
502243903Shrs	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
50355505Sshin	if (rtmsg(RTM_GET) < 0) {
504121156Sume		errx(1, "RTM_GET(%s) failed", host);
505121156Sume		/* NOTREACHED */
50655505Sshin	}
50755505Sshin	sin = (struct sockaddr_in6 *)(rtm + 1);
508259171Sae	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
50955505Sshin	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
51055505Sshin		if (sdl->sdl_family == AF_LINK &&
51162590Sitojun		    !(rtm->rtm_flags & RTF_GATEWAY)) {
51278064Sume			goto delete;
51355505Sshin		}
51462590Sitojun		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
51562590Sitojun		return 1;
51655505Sshin	}
51762590Sitojun
51855505Sshindelete:
51955505Sshin	if (sdl->sdl_family != AF_LINK) {
52055505Sshin		printf("cannot locate %s\n", host);
52155505Sshin		return (1);
52255505Sshin	}
523186119Sqingli        /*
524186119Sqingli         * need to reinit the field because it has rt_key
525186119Sqingli         * but we want the actual address
526186119Sqingli         */
527186119Sqingli	NEXTADDR(RTA_DST, sin_m);
528186500Sqingli	rtm->rtm_flags |= RTF_LLDATA;
52955505Sshin	if (rtmsg(RTM_DELETE) == 0) {
530243903Shrs		getnameinfo((struct sockaddr *)sin,
531243903Shrs		    sin->sin6_len, host_buf,
532121156Sume		    sizeof(host_buf), NULL, 0,
533121156Sume		    (nflag ? NI_NUMERICHOST : 0));
53455505Sshin		printf("%s (%s) deleted\n", host, host_buf);
53555505Sshin	}
53655505Sshin
53755505Sshin	return 0;
53855505Sshin}
53955505Sshin
540122615Sume#define W_ADDR	36
54178064Sume#define W_LL	17
54278064Sume#define W_IF	6
54378064Sume
54455505Sshin/*
54555505Sshin * Dump the entire neighbor cache
54655505Sshin */
54755505Sshinvoid
548259176Saedump(struct sockaddr_in6 *addr, int cflag)
54955505Sshin{
55055505Sshin	int mib[6];
55155505Sshin	size_t needed;
55262590Sitojun	char *lim, *buf, *next;
55355505Sshin	struct rt_msghdr *rtm;
55455505Sshin	struct sockaddr_in6 *sin;
55555505Sshin	struct sockaddr_dl *sdl;
55655505Sshin	extern int h_errno;
55755505Sshin	struct in6_nbrinfo *nbi;
558253999Shrs	struct timeval now;
55955505Sshin	int addrwidth;
56078064Sume	int llwidth;
56178064Sume	int ifwidth;
56262590Sitojun	char flgbuf[8];
56378064Sume	char *ifname;
56455505Sshin
56555505Sshin	/* Print header */
56666865Ssumikawa	if (!tflag && !cflag)
567122615Sume		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
56878064Sume		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
569122615Sume		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
57055505Sshin
57155505Sshinagain:;
57255505Sshin	mib[0] = CTL_NET;
57355505Sshin	mib[1] = PF_ROUTE;
57455505Sshin	mib[2] = 0;
57555505Sshin	mib[3] = AF_INET6;
57655505Sshin	mib[4] = NET_RT_FLAGS;
577186119Sqingli#ifdef RTF_LLINFO
57855505Sshin	mib[5] = RTF_LLINFO;
579186119Sqingli#else
580186119Sqingli	mib[5] = 0;
581186119Sqingli#endif
58255505Sshin	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
58355505Sshin		err(1, "sysctl(PF_ROUTE estimate)");
58455505Sshin	if (needed > 0) {
58555505Sshin		if ((buf = malloc(needed)) == NULL)
586121156Sume			err(1, "malloc");
58755505Sshin		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
58855505Sshin			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
58955505Sshin		lim = buf + needed;
59055505Sshin	} else
59155505Sshin		buf = lim = NULL;
59255505Sshin
59355505Sshin	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
59455505Sshin		int isrouter = 0, prbs = 0;
59555505Sshin
59655505Sshin		rtm = (struct rt_msghdr *)next;
59755505Sshin		sin = (struct sockaddr_in6 *)(rtm + 1);
598259171Sae		sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
59978064Sume
60078064Sume		/*
60178064Sume		 * Some OSes can produce a route that has the LINK flag but
60278064Sume		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
60378064Sume		 * and BSD/OS, where xx is not the interface identifier on
60478064Sume		 * lo0).  Such routes entry would annoy getnbrinfo() below,
60578064Sume		 * so we skip them.
60678064Sume		 * XXX: such routes should have the GATEWAY flag, not the
607121156Sume		 * LINK flag.  However, there is rotten routing software
60878064Sume		 * that advertises all routes that have the GATEWAY flag.
60978064Sume		 * Thus, KAME kernel intentionally does not set the LINK flag.
61078064Sume		 * What is to be fixed is not ndp, but such routing software
61178064Sume		 * (and the kernel workaround)...
61278064Sume		 */
61378064Sume		if (sdl->sdl_family != AF_LINK)
61478064Sume			continue;
61578064Sume
616122615Sume		if (!(rtm->rtm_flags & RTF_HOST))
617122615Sume			continue;
618122615Sume
61955505Sshin		if (addr) {
620259176Sae			if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
621259176Sae			    &sin->sin6_addr) == 0 ||
622259176Sae			    addr->sin6_scope_id != sin->sin6_scope_id)
62355505Sshin				continue;
62455505Sshin			found_entry = 1;
62555505Sshin		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
62655505Sshin			continue;
62755505Sshin		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
62855505Sshin		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
62955505Sshin			/* XXX: should scope id be filled in the kernel? */
63055505Sshin			if (sin->sin6_scope_id == 0)
63155505Sshin				sin->sin6_scope_id = sdl->sdl_index;
63255505Sshin		}
63355505Sshin		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
634121156Sume		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
635122615Sume		if (cflag) {
63681366Ssumikawa#ifdef RTF_WASCLONED
63781366Ssumikawa			if (rtm->rtm_flags & RTF_WASCLONED)
63881366Ssumikawa				delete(host_buf);
639122615Sume#elif defined(RTF_CLONED)
640122615Sume			if (rtm->rtm_flags & RTF_CLONED)
641122615Sume				delete(host_buf);
64281366Ssumikawa#else
64366865Ssumikawa			delete(host_buf);
64481366Ssumikawa#endif
64566865Ssumikawa			continue;
64666865Ssumikawa		}
647253999Shrs		gettimeofday(&now, 0);
64855505Sshin		if (tflag)
649253970Shrs			ts_print(&now);
65055505Sshin
65178064Sume		addrwidth = strlen(host_buf);
65278064Sume		if (addrwidth < W_ADDR)
65378064Sume			addrwidth = W_ADDR;
65478064Sume		llwidth = strlen(ether_str(sdl));
65578064Sume		if (W_ADDR + W_LL - addrwidth > llwidth)
65678064Sume			llwidth = W_ADDR + W_LL - addrwidth;
65778064Sume		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
65878064Sume		if (!ifname)
65978064Sume			ifname = "?";
66078064Sume		ifwidth = strlen(ifname);
66178064Sume		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
66278064Sume			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
66355505Sshin
66478064Sume		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
66578064Sume		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
66655505Sshin
66755505Sshin		/* Print neighbor discovery specific informations */
66862590Sitojun		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
66955505Sshin		if (nbi) {
670253970Shrs			if (nbi->expire > now.tv_sec) {
67155505Sshin				printf(" %-9.9s",
672253970Shrs				    sec2str(nbi->expire - now.tv_sec));
67378064Sume			} else if (nbi->expire == 0)
67455505Sshin				printf(" %-9.9s", "permanent");
67555505Sshin			else
67655505Sshin				printf(" %-9.9s", "expired");
67755505Sshin
678121156Sume			switch (nbi->state) {
679121156Sume			case ND6_LLINFO_NOSTATE:
68055505Sshin				 printf(" N");
68155505Sshin				 break;
68278064Sume#ifdef ND6_LLINFO_WAITDELETE
683121156Sume			case ND6_LLINFO_WAITDELETE:
68455505Sshin				 printf(" W");
68555505Sshin				 break;
68678064Sume#endif
687121156Sume			case ND6_LLINFO_INCOMPLETE:
68855505Sshin				 printf(" I");
68955505Sshin				 break;
690121156Sume			case ND6_LLINFO_REACHABLE:
69155505Sshin				 printf(" R");
69255505Sshin				 break;
693121156Sume			case ND6_LLINFO_STALE:
69455505Sshin				 printf(" S");
69555505Sshin				 break;
696121156Sume			case ND6_LLINFO_DELAY:
69755505Sshin				 printf(" D");
69855505Sshin				 break;
699121156Sume			case ND6_LLINFO_PROBE:
70055505Sshin				 printf(" P");
70155505Sshin				 break;
702121156Sume			default:
70355505Sshin				 printf(" ?");
70455505Sshin				 break;
70555505Sshin			}
70655505Sshin
70755505Sshin			isrouter = nbi->isrouter;
70855505Sshin			prbs = nbi->asked;
70978064Sume		} else {
71055505Sshin			warnx("failed to get neighbor information");
71155505Sshin			printf("  ");
71255505Sshin		}
71355505Sshin
71462590Sitojun		/*
71562590Sitojun		 * other flags. R: router, P: proxy, W: ??
71662590Sitojun		 */
71762590Sitojun		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
71862590Sitojun			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
719121156Sume			    isrouter ? "R" : "",
720121156Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
72162590Sitojun		} else {
72262590Sitojun			sin = (struct sockaddr_in6 *)
723121156Sume			    (sdl->sdl_len + (char *)sdl);
724122615Sume#if 0	/* W and P are mystery even for us */
72562590Sitojun			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
726121156Sume			    isrouter ? "R" : "",
727121156Sume			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
728121156Sume			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
729121156Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
730122615Sume#else
731122615Sume			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
732122615Sume			    isrouter ? "R" : "",
733122615Sume			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
734122615Sume#endif
73555505Sshin		}
736122615Sume		printf(" %s", flgbuf);
73755505Sshin
73855505Sshin		if (prbs)
739122615Sume			printf(" %d", prbs);
74055505Sshin
74155505Sshin		printf("\n");
74255505Sshin	}
74378064Sume	if (buf != NULL)
74478064Sume		free(buf);
74555505Sshin
74655505Sshin	if (repeat) {
74755505Sshin		printf("\n");
748125675Ssumikawa		fflush(stdout);
74955505Sshin		sleep(repeat);
75055505Sshin		goto again;
75155505Sshin	}
75255505Sshin}
75355505Sshin
75455505Sshinstatic struct in6_nbrinfo *
755259169Saegetnbrinfo(struct in6_addr *addr, int ifindex, int warning)
75655505Sshin{
75755505Sshin	static struct in6_nbrinfo nbi;
75855505Sshin	int s;
75955505Sshin
76055505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
76155505Sshin		err(1, "socket");
76255505Sshin
76355505Sshin	bzero(&nbi, sizeof(nbi));
76455505Sshin	if_indextoname(ifindex, nbi.ifname);
76555505Sshin	nbi.addr = *addr;
76655505Sshin	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
76762590Sitojun		if (warning)
76862590Sitojun			warn("ioctl(SIOCGNBRINFO_IN6)");
76955505Sshin		close(s);
77055505Sshin		return(NULL);
77155505Sshin	}
77255505Sshin
77355505Sshin	close(s);
77455505Sshin	return(&nbi);
77555505Sshin}
77655505Sshin
77755505Sshinstatic char *
778217140Sdelphijether_str(struct sockaddr_dl *sdl)
77955505Sshin{
780121156Sume	static char hbuf[NI_MAXHOST];
781219819Sjeff	char *cp;
78255505Sshin
783219819Sjeff	if (sdl->sdl_alen == ETHER_ADDR_LEN) {
784217140Sdelphij		strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
785217140Sdelphij		    sizeof(hbuf));
786219819Sjeff	} else if (sdl->sdl_alen) {
787219819Sjeff		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
788219819Sjeff		snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
789219819Sjeff	} else
790121156Sume		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
79155505Sshin
792121156Sume	return(hbuf);
79355505Sshin}
79455505Sshin
79555505Sshinint
796259169Saendp_ether_aton(char *a, u_char *n)
79755505Sshin{
79855505Sshin	int i, o[6];
79955505Sshin
80055505Sshin	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
801121156Sume	    &o[3], &o[4], &o[5]);
80255505Sshin	if (i != 6) {
80355505Sshin		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
80455505Sshin		return (1);
80555505Sshin	}
806121156Sume	for (i = 0; i < 6; i++)
80755505Sshin		n[i] = o[i];
80855505Sshin	return (0);
80955505Sshin}
81055505Sshin
81155505Sshinvoid
81255505Sshinusage()
81355505Sshin{
814122615Sume	printf("usage: ndp [-nt] hostname\n");
815122615Sume	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
81678064Sume	printf("       ndp [-nt] -A wait\n");
817122615Sume	printf("       ndp [-nt] -d hostname\n");
818122615Sume	printf("       ndp [-nt] -f filename\n");
819122615Sume	printf("       ndp [-nt] -i interface [flags...]\n");
82062590Sitojun#ifdef SIOCSDEFIFACE_IN6
821122615Sume	printf("       ndp [-nt] -I [interface|delete]\n");
82262590Sitojun#endif
823122615Sume	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
82455505Sshin	exit(1);
82555505Sshin}
82655505Sshin
82755505Sshinint
828259169Saertmsg(int cmd)
82955505Sshin{
83055505Sshin	static int seq;
83155505Sshin	int rlen;
83255505Sshin	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
83355505Sshin	register char *cp = m_rtmsg.m_space;
83455505Sshin	register int l;
83555505Sshin
83655505Sshin	errno = 0;
83755505Sshin	if (cmd == RTM_DELETE)
83855505Sshin		goto doit;
83955505Sshin	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
84055505Sshin	rtm->rtm_flags = flags;
84155505Sshin	rtm->rtm_version = RTM_VERSION;
84255505Sshin
84355505Sshin	switch (cmd) {
84455505Sshin	default:
84555505Sshin		fprintf(stderr, "ndp: internal wrong cmd\n");
84655505Sshin		exit(1);
84755505Sshin	case RTM_ADD:
84855505Sshin		rtm->rtm_addrs |= RTA_GATEWAY;
849122615Sume		if (expire_time) {
850122615Sume			rtm->rtm_rmx.rmx_expire = expire_time;
851122615Sume			rtm->rtm_inits = RTV_EXPIRE;
852122615Sume		}
853186500Sqingli		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
854151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */
85562590Sitojun		if (rtm->rtm_flags & RTF_ANNOUNCE) {
85662590Sitojun			rtm->rtm_flags &= ~RTF_HOST;
857124241Ssuz			rtm->rtm_addrs |= RTA_NETMASK;
85862590Sitojun		}
859151473Ssuz#endif
86055505Sshin		/* FALLTHROUGH */
86155505Sshin	case RTM_GET:
86255505Sshin		rtm->rtm_addrs |= RTA_DST;
86355505Sshin	}
86455505Sshin
86555505Sshin	NEXTADDR(RTA_DST, sin_m);
86655505Sshin	NEXTADDR(RTA_GATEWAY, sdl_m);
867151473Ssuz#if 0 /* we don't support ipv6addr/128 type proxying */
86862590Sitojun	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
86955505Sshin	NEXTADDR(RTA_NETMASK, so_mask);
870151473Ssuz#endif
87155505Sshin
87255505Sshin	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
87355505Sshindoit:
87455505Sshin	l = rtm->rtm_msglen;
87555505Sshin	rtm->rtm_seq = ++seq;
87655505Sshin	rtm->rtm_type = cmd;
87755505Sshin	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
87855505Sshin		if (errno != ESRCH || cmd != RTM_DELETE) {
879121156Sume			err(1, "writing to routing socket");
880121156Sume			/* NOTREACHED */
88155505Sshin		}
88255505Sshin	}
88355505Sshin	do {
88455505Sshin		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
88555505Sshin	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
88655505Sshin	if (l < 0)
88755505Sshin		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
88855505Sshin		    strerror(errno));
88955505Sshin	return (0);
89055505Sshin}
89155505Sshin
89255505Sshinvoid
893259169Saeifinfo(char *ifname, int argc, char **argv)
89455505Sshin{
89555505Sshin	struct in6_ndireq nd;
89662590Sitojun	int i, s;
89762590Sitojun	u_int32_t newflags;
89878064Sume#ifdef IPV6CTL_USETEMPADDR
89978064Sume	u_int8_t nullbuf[8];
90078064Sume#endif
90155505Sshin
90255505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
903121156Sume		err(1, "socket");
904121156Sume		/* NOTREACHED */
90555505Sshin	}
90655505Sshin	bzero(&nd, sizeof(nd));
907121156Sume	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
90855505Sshin	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
909121156Sume		err(1, "ioctl(SIOCGIFINFO_IN6)");
910121156Sume		/* NOTREACHED */
911122615Sume	}
91262590Sitojun#define ND nd.ndi
91362590Sitojun	newflags = ND.flags;
914122615Sume	for (i = 0; i < argc; i++) {
91562590Sitojun		int clear = 0;
91662590Sitojun		char *cp = argv[i];
91762590Sitojun
91862590Sitojun		if (*cp == '-') {
91962590Sitojun			clear = 1;
92062590Sitojun			cp++;
92162590Sitojun		}
92262590Sitojun
92362590Sitojun#define SETFLAG(s, f) \
92462590Sitojun	do {\
92562590Sitojun		if (strcmp(cp, (s)) == 0) {\
92662590Sitojun			if (clear)\
92762590Sitojun				newflags &= ~(f);\
92862590Sitojun			else\
92962590Sitojun				newflags |= (f);\
93062590Sitojun		}\
93162590Sitojun	} while (0)
932151468Ssuz/*
933151468Ssuz * XXX: this macro is not 100% correct, in that it matches "nud" against
934151468Ssuz *      "nudbogus".  But we just let it go since this is minor.
935151468Ssuz */
936151468Ssuz#define SETVALUE(f, v) \
937151468Ssuz	do { \
938151468Ssuz		char *valptr; \
939151468Ssuz		unsigned long newval; \
940151468Ssuz		v = 0; /* unspecified */ \
941151468Ssuz		if (strncmp(cp, f, strlen(f)) == 0) { \
942151468Ssuz			valptr = strchr(cp, '='); \
943151468Ssuz			if (valptr == NULL) \
944151468Ssuz				err(1, "syntax error in %s field", (f)); \
945151468Ssuz			errno = 0; \
946151468Ssuz			newval = strtoul(++valptr, NULL, 0); \
947151468Ssuz			if (errno) \
948151468Ssuz				err(1, "syntax error in %s's value", (f)); \
949151468Ssuz			v = newval; \
950151468Ssuz		} \
951151468Ssuz	} while (0)
952151468Ssuz
953151474Ssuz		SETFLAG("disabled", ND6_IFF_IFDISABLED);
95462590Sitojun		SETFLAG("nud", ND6_IFF_PERFORMNUD);
955118498Sume#ifdef ND6_IFF_ACCEPT_RTADV
956118498Sume		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
957118498Sume#endif
958197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL
959197138Shrs		SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
960197138Shrs#endif
961245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE
962245230Sume		SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
963245230Sume#endif
964151468Ssuz		SETVALUE("basereachable", ND.basereachable);
965151468Ssuz		SETVALUE("retrans", ND.retrans);
966151468Ssuz		SETVALUE("curhlim", ND.chlim);
96762590Sitojun
96862590Sitojun		ND.flags = newflags;
969151468Ssuz		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
970151468Ssuz			err(1, "ioctl(SIOCSIFINFO_IN6)");
971121156Sume			/* NOTREACHED */
97262590Sitojun		}
97362590Sitojun#undef SETFLAG
974151468Ssuz#undef SETVALUE
97562590Sitojun	}
97662590Sitojun
977121162Sume	if (!ND.initialized) {
978121162Sume		errx(1, "%s: not initialized yet", ifname);
979121162Sume		/* NOTREACHED */
980121162Sume	}
981121162Sume
982151468Ssuz	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
983151468Ssuz		err(1, "ioctl(SIOCGIFINFO_IN6)");
984151468Ssuz		/* NOTREACHED */
985151468Ssuz	}
98655505Sshin	printf("linkmtu=%d", ND.linkmtu);
987121471Sume	printf(", maxmtu=%d", ND.maxmtu);
98855505Sshin	printf(", curhlim=%d", ND.chlim);
98955505Sshin	printf(", basereachable=%ds%dms",
990121156Sume	    ND.basereachable / 1000, ND.basereachable % 1000);
99155505Sshin	printf(", reachable=%ds", ND.reachable);
99262590Sitojun	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
99378064Sume#ifdef IPV6CTL_USETEMPADDR
99478064Sume	memset(nullbuf, 0, sizeof(nullbuf));
99578064Sume	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
99678064Sume		int j;
99778064Sume		u_int8_t *rbuf;
99878064Sume
99978064Sume		for (i = 0; i < 3; i++) {
1000121156Sume			switch (i) {
100178064Sume			case 0:
100278064Sume				printf("\nRandom seed(0): ");
100378064Sume				rbuf = ND.randomseed0;
100478064Sume				break;
100578064Sume			case 1:
100678064Sume				printf("\nRandom seed(1): ");
100778064Sume				rbuf = ND.randomseed1;
100878064Sume				break;
100978064Sume			case 2:
101078064Sume				printf("\nRandom ID:      ");
101178064Sume				rbuf = ND.randomid;
101278064Sume				break;
1013151472Ssuz			default:
1014151472Ssuz				errx(1, "impossible case for tempaddr display");
101578064Sume			}
101678064Sume			for (j = 0; j < 8; j++)
101778064Sume				printf("%02x", rbuf[j]);
101878064Sume		}
101978064Sume	}
102078064Sume#endif
102162590Sitojun	if (ND.flags) {
102262590Sitojun		printf("\nFlags: ");
1023151474Ssuz#ifdef ND6_IFF_IFDISABLED
1024151474Ssuz		if ((ND.flags & ND6_IFF_IFDISABLED))
1025151474Ssuz			printf("disabled ");
1026151474Ssuz#endif
1027118498Sume		if ((ND.flags & ND6_IFF_PERFORMNUD))
1028118498Sume			printf("nud ");
1029118498Sume#ifdef ND6_IFF_ACCEPT_RTADV
1030118498Sume		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1031118498Sume			printf("accept_rtadv ");
1032118498Sume#endif
1033197138Shrs#ifdef ND6_IFF_AUTO_LINKLOCAL
1034197138Shrs		if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1035197138Shrs			printf("auto_linklocal ");
1036197138Shrs#endif
1037245230Sume#ifdef ND6_IFF_NO_PREFER_IFACE
1038245230Sume		if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1039245230Sume			printf("no_prefer_iface ");
1040245230Sume#endif
1041122615Sume	}
104262590Sitojun	putc('\n', stdout);
104355505Sshin#undef ND
1044121156Sume
104555505Sshin	close(s);
104655505Sshin}
104755505Sshin
104878064Sume#ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
104978064Sume#define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
105078064Sume#endif
105178064Sume
105255505Sshinvoid
105355505Sshinrtrlist()
105455505Sshin{
105578064Sume#ifdef ICMPV6CTL_ND6_DRLIST
105678064Sume	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
105778064Sume	char *buf;
105878064Sume	struct in6_defrouter *p, *ep;
105978064Sume	size_t l;
1060253999Shrs	struct timeval now;
106178064Sume
106278064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
106378064Sume		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
106478064Sume		/*NOTREACHED*/
106578064Sume	}
1066151472Ssuz	if (l == 0)
1067151472Ssuz		return;
106878064Sume	buf = malloc(l);
106978064Sume	if (!buf) {
1070121156Sume		err(1, "malloc");
107178064Sume		/*NOTREACHED*/
107278064Sume	}
107378064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
107478064Sume		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
107578064Sume		/*NOTREACHED*/
107678064Sume	}
107778064Sume
107878064Sume	ep = (struct in6_defrouter *)(buf + l);
107978064Sume	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
108078064Sume		int rtpref;
108178064Sume
108278064Sume		if (getnameinfo((struct sockaddr *)&p->rtaddr,
108378064Sume		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1084121156Sume		    (nflag ? NI_NUMERICHOST : 0)) != 0)
108578064Sume			strlcpy(host_buf, "?", sizeof(host_buf));
1086121156Sume
108778064Sume		printf("%s if=%s", host_buf,
1088121156Sume		    if_indextoname(p->if_index, ifix_buf));
108978064Sume		printf(", flags=%s%s",
1090121156Sume		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1091121156Sume		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
109278064Sume		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
109378064Sume		printf(", pref=%s", rtpref_str[rtpref]);
1094121156Sume
1095253999Shrs		gettimeofday(&now, 0);
109678064Sume		if (p->expire == 0)
109778064Sume			printf(", expire=Never\n");
109878064Sume		else
109978064Sume			printf(", expire=%s\n",
1100253970Shrs			    sec2str(p->expire - now.tv_sec));
110178064Sume	}
110278064Sume	free(buf);
110378064Sume#else
110455505Sshin	struct in6_drlist dr;
110555505Sshin	int s, i;
1106253999Shrs	struct timeval now;
110755505Sshin
110855505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1109121156Sume		err(1, "socket");
1110121156Sume		/* NOTREACHED */
111155505Sshin	}
111255505Sshin	bzero(&dr, sizeof(dr));
1113121156Sume	strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
111455505Sshin	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1115121156Sume		err(1, "ioctl(SIOCGDRLST_IN6)");
1116121156Sume		/* NOTREACHED */
1117121156Sume	}
111862590Sitojun#define DR dr.defrouter[i]
111978064Sume	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
112055505Sshin		struct sockaddr_in6 sin6;
112155505Sshin
112255505Sshin		bzero(&sin6, sizeof(sin6));
112355505Sshin		sin6.sin6_family = AF_INET6;
112455505Sshin		sin6.sin6_len = sizeof(sin6);
112555505Sshin		sin6.sin6_addr = DR.rtaddr;
112655505Sshin		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1127121156Sume		    sizeof(host_buf), NULL, 0,
1128121156Sume		    (nflag ? NI_NUMERICHOST : 0));
1129121156Sume
113055505Sshin		printf("%s if=%s", host_buf,
1131121156Sume		    if_indextoname(DR.if_index, ifix_buf));
113255505Sshin		printf(", flags=%s%s",
1133121156Sume		    DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1134121156Sume		    DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
1135253999Shrs		gettimeofday(&now, 0);
113655505Sshin		if (DR.expire == 0)
113755505Sshin			printf(", expire=Never\n");
113855505Sshin		else
113955505Sshin			printf(", expire=%s\n",
1140253970Shrs			    sec2str(DR.expire - now.tv_sec));
114155505Sshin	}
114255505Sshin#undef DR
114355505Sshin	close(s);
114478064Sume#endif
114555505Sshin}
114655505Sshin
114755505Sshinvoid
114855505Sshinplist()
114955505Sshin{
115078064Sume#ifdef ICMPV6CTL_ND6_PRLIST
115178064Sume	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
115278064Sume	char *buf;
115378064Sume	struct in6_prefix *p, *ep, *n;
115478064Sume	struct sockaddr_in6 *advrtr;
115578064Sume	size_t l;
1156253999Shrs	struct timeval now;
115778064Sume	const int niflags = NI_NUMERICHOST;
115878064Sume	int ninflags = nflag ? NI_NUMERICHOST : 0;
115978064Sume	char namebuf[NI_MAXHOST];
116078064Sume
116178064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
116278064Sume		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
116378064Sume		/*NOTREACHED*/
116478064Sume	}
116578064Sume	buf = malloc(l);
116678064Sume	if (!buf) {
1167121156Sume		err(1, "malloc");
116878064Sume		/*NOTREACHED*/
116978064Sume	}
117078064Sume	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
117178064Sume		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
117278064Sume		/*NOTREACHED*/
117378064Sume	}
117478064Sume
117578064Sume	ep = (struct in6_prefix *)(buf + l);
117678064Sume	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
117778064Sume		advrtr = (struct sockaddr_in6 *)(p + 1);
117878064Sume		n = (struct in6_prefix *)&advrtr[p->advrtrs];
117978064Sume
118078064Sume		if (getnameinfo((struct sockaddr *)&p->prefix,
118178064Sume		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
118278064Sume		    NULL, 0, niflags) != 0)
118378064Sume			strlcpy(namebuf, "?", sizeof(namebuf));
118478064Sume		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1185121156Sume		    if_indextoname(p->if_index, ifix_buf));
118678064Sume
1187253999Shrs		gettimeofday(&now, 0);
118878064Sume		/*
118978064Sume		 * meaning of fields, especially flags, is very different
119078064Sume		 * by origin.  notify the difference to the users.
119178064Sume		 */
119278064Sume		printf("flags=%s%s%s%s%s",
1193121156Sume		    p->raflags.onlink ? "L" : "",
1194121156Sume		    p->raflags.autonomous ? "A" : "",
1195121156Sume		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1196121156Sume		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
119778064Sume#ifdef NDPRF_HOME
1198121156Sume		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
119978064Sume#else
1200121156Sume		    ""
120178064Sume#endif
1202121156Sume		    );
120378064Sume		if (p->vltime == ND6_INFINITE_LIFETIME)
120478064Sume			printf(" vltime=infinity");
120578064Sume		else
1206122615Sume			printf(" vltime=%lu", (unsigned long)p->vltime);
120778064Sume		if (p->pltime == ND6_INFINITE_LIFETIME)
120878064Sume			printf(", pltime=infinity");
120978064Sume		else
1210122615Sume			printf(", pltime=%lu", (unsigned long)p->pltime);
121178064Sume		if (p->expire == 0)
121278064Sume			printf(", expire=Never");
1213253970Shrs		else if (p->expire >= now.tv_sec)
121478064Sume			printf(", expire=%s",
1215253970Shrs			    sec2str(p->expire - now.tv_sec));
121678064Sume		else
121778064Sume			printf(", expired");
121878064Sume		printf(", ref=%d", p->refcnt);
121978064Sume		printf("\n");
122078064Sume		/*
122178064Sume		 * "advertising router" list is meaningful only if the prefix
122278064Sume		 * information is from RA.
122378064Sume		 */
122478064Sume		if (p->advrtrs) {
122578064Sume			int j;
122678064Sume			struct sockaddr_in6 *sin6;
122778064Sume
1228122615Sume			sin6 = advrtr;
122978064Sume			printf("  advertised by\n");
123078064Sume			for (j = 0; j < p->advrtrs; j++) {
123178064Sume				struct in6_nbrinfo *nbi;
123278064Sume
123378064Sume				if (getnameinfo((struct sockaddr *)sin6,
123478064Sume				    sin6->sin6_len, namebuf, sizeof(namebuf),
123578064Sume				    NULL, 0, ninflags) != 0)
123678064Sume					strlcpy(namebuf, "?", sizeof(namebuf));
123778064Sume				printf("    %s", namebuf);
123878064Sume
1239121156Sume				nbi = getnbrinfo(&sin6->sin6_addr,
1240121156Sume				    p->if_index, 0);
124178064Sume				if (nbi) {
1242121156Sume					switch (nbi->state) {
124378064Sume					case ND6_LLINFO_REACHABLE:
124478064Sume					case ND6_LLINFO_STALE:
124578064Sume					case ND6_LLINFO_DELAY:
124678064Sume					case ND6_LLINFO_PROBE:
124778064Sume						printf(" (reachable)\n");
124878064Sume						break;
124978064Sume					default:
125078064Sume						printf(" (unreachable)\n");
125178064Sume					}
125278064Sume				} else
125378064Sume					printf(" (no neighbor state)\n");
125478064Sume				sin6++;
125578064Sume			}
125678064Sume		} else
125778064Sume			printf("  No advertising router\n");
125878064Sume	}
125978064Sume	free(buf);
126078064Sume#else
126155505Sshin	struct in6_prlist pr;
126255505Sshin	int s, i;
1263253999Shrs	struct timeval now;
126455505Sshin
1265253999Shrs	gettimeofday(&now, 0);
126655505Sshin
126755505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1268121156Sume		err(1, "socket");
1269121156Sume		/* NOTREACHED */
127055505Sshin	}
127155505Sshin	bzero(&pr, sizeof(pr));
1272121156Sume	strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
127355505Sshin	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1274121156Sume		err(1, "ioctl(SIOCGPRLST_IN6)");
1275121156Sume		/* NOTREACHED */
1276121156Sume	}
127762590Sitojun#define PR pr.prefix[i]
127855505Sshin	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
127978064Sume		struct sockaddr_in6 p6;
128078064Sume		char namebuf[NI_MAXHOST];
128178064Sume		int niflags;
128278064Sume
128378064Sume#ifdef NDPRF_ONLINK
128478064Sume		p6 = PR.prefix;
128578064Sume#else
128678064Sume		memset(&p6, 0, sizeof(p6));
128778064Sume		p6.sin6_family = AF_INET6;
128878064Sume		p6.sin6_len = sizeof(p6);
128978064Sume		p6.sin6_addr = PR.prefix;
129078064Sume#endif
129178064Sume		niflags = NI_NUMERICHOST;
129278064Sume		if (getnameinfo((struct sockaddr *)&p6,
1293121156Sume		    sizeof(p6), namebuf, sizeof(namebuf),
1294121156Sume		    NULL, 0, niflags)) {
129578064Sume			warnx("getnameinfo failed");
129678064Sume			continue;
129778064Sume		}
129878064Sume		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1299121156Sume		    if_indextoname(PR.if_index, ifix_buf));
130078064Sume
1301253999Shrs		gettimeofday(&now, 0);
130262590Sitojun		/*
130362590Sitojun		 * meaning of fields, especially flags, is very different
130462590Sitojun		 * by origin.  notify the difference to the users.
130562590Sitojun		 */
130678064Sume#if 0
130778064Sume		printf("  %s",
1308121156Sume		    PR.origin == PR_ORIG_RA ? "" : "advertise: ");
130978064Sume#endif
131078064Sume#ifdef NDPRF_ONLINK
131178064Sume		printf("flags=%s%s%s%s%s",
1312121156Sume		    PR.raflags.onlink ? "L" : "",
1313121156Sume		    PR.raflags.autonomous ? "A" : "",
1314121156Sume		    (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1315121156Sume		    (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
131678064Sume#ifdef NDPRF_HOME
1317121156Sume		    (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
131878064Sume#else
1319121156Sume		    ""
132078064Sume#endif
1321121156Sume		    );
132278064Sume#else
132362590Sitojun		printf("flags=%s%s",
1324121156Sume		    PR.raflags.onlink ? "L" : "",
1325121156Sume		    PR.raflags.autonomous ? "A" : "");
132678064Sume#endif
132755505Sshin		if (PR.vltime == ND6_INFINITE_LIFETIME)
132855505Sshin			printf(" vltime=infinity");
132955505Sshin		else
1330122615Sume			printf(" vltime=%lu", PR.vltime);
133155505Sshin		if (PR.pltime == ND6_INFINITE_LIFETIME)
133255505Sshin			printf(", pltime=infinity");
133355505Sshin		else
1334122615Sume			printf(", pltime=%lu", PR.pltime);
133555505Sshin		if (PR.expire == 0)
133662590Sitojun			printf(", expire=Never");
1337253970Shrs		else if (PR.expire >= now.tv_sec)
133862590Sitojun			printf(", expire=%s",
1339253970Shrs			    sec2str(PR.expire - now.tv_sec));
134055505Sshin		else
134162590Sitojun			printf(", expired");
134278064Sume#ifdef NDPRF_ONLINK
134378064Sume		printf(", ref=%d", PR.refcnt);
134478064Sume#endif
134578064Sume#if 0
134662590Sitojun		switch (PR.origin) {
134762590Sitojun		case PR_ORIG_RA:
134862590Sitojun			printf(", origin=RA");
134962590Sitojun			break;
135062590Sitojun		case PR_ORIG_RR:
135162590Sitojun			printf(", origin=RR");
135262590Sitojun			break;
135362590Sitojun		case PR_ORIG_STATIC:
135462590Sitojun			printf(", origin=static");
135562590Sitojun			break;
135662590Sitojun		case PR_ORIG_KERNEL:
135762590Sitojun			printf(", origin=kernel");
135862590Sitojun			break;
135962590Sitojun		default:
136062590Sitojun			printf(", origin=?");
136162590Sitojun			break;
136262590Sitojun		}
136378064Sume#endif
136462590Sitojun		printf("\n");
136562590Sitojun		/*
136662590Sitojun		 * "advertising router" list is meaningful only if the prefix
136762590Sitojun		 * information is from RA.
136862590Sitojun		 */
136978064Sume		if (0 &&	/* prefix origin is almost obsolted */
137078064Sume		    PR.origin != PR_ORIG_RA)
137162590Sitojun			;
137262590Sitojun		else if (PR.advrtrs) {
137355505Sshin			int j;
137455505Sshin			printf("  advertised by\n");
137555505Sshin			for (j = 0; j < PR.advrtrs; j++) {
137655505Sshin				struct sockaddr_in6 sin6;
137762590Sitojun				struct in6_nbrinfo *nbi;
137855505Sshin
137955505Sshin				bzero(&sin6, sizeof(sin6));
138055505Sshin				sin6.sin6_family = AF_INET6;
138155505Sshin				sin6.sin6_len = sizeof(sin6);
138255505Sshin				sin6.sin6_addr = PR.advrtr[j];
138362590Sitojun				sin6.sin6_scope_id = PR.if_index; /* XXX */
138455505Sshin				getnameinfo((struct sockaddr *)&sin6,
1385121156Sume				    sin6.sin6_len, host_buf,
1386121156Sume				    sizeof(host_buf), NULL, 0,
1387121156Sume				    (nflag ? NI_NUMERICHOST : 0));
138862590Sitojun				printf("    %s", host_buf);
138955505Sshin
1390121156Sume				nbi = getnbrinfo(&sin6.sin6_addr,
1391121156Sume				    PR.if_index, 0);
139262590Sitojun				if (nbi) {
1393121156Sume					switch (nbi->state) {
1394121156Sume					case ND6_LLINFO_REACHABLE:
1395121156Sume					case ND6_LLINFO_STALE:
1396121156Sume					case ND6_LLINFO_DELAY:
1397121156Sume					case ND6_LLINFO_PROBE:
139862590Sitojun						 printf(" (reachable)\n");
139962590Sitojun						 break;
1400121156Sume					default:
140162590Sitojun						 printf(" (unreachable)\n");
140262590Sitojun					}
140378064Sume				} else
140462590Sitojun					printf(" (no neighbor state)\n");
140555505Sshin			}
140655505Sshin			if (PR.advrtrs > DRLSTSIZ)
140755505Sshin				printf("    and %d routers\n",
1408121156Sume				    PR.advrtrs - DRLSTSIZ);
140962590Sitojun		} else
141055505Sshin			printf("  No advertising router\n");
141155505Sshin	}
141255505Sshin#undef PR
141355505Sshin	close(s);
141478064Sume#endif
141555505Sshin}
141655505Sshin
141755505Sshinvoid
141855505Sshinpfx_flush()
141955505Sshin{
142055505Sshin	char dummyif[IFNAMSIZ+8];
142155505Sshin	int s;
142255505Sshin
142355505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
142455505Sshin		err(1, "socket");
1425121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
142655505Sshin	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1427121156Sume		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
142855505Sshin}
142955505Sshin
143055505Sshinvoid
143155505Sshinrtr_flush()
143255505Sshin{
143355505Sshin	char dummyif[IFNAMSIZ+8];
143455505Sshin	int s;
143555505Sshin
143655505Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
143755505Sshin		err(1, "socket");
1438121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
143955505Sshin	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1440121156Sume		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
144162590Sitojun
144262590Sitojun	close(s);
144355505Sshin}
144455505Sshin
144555505Sshinvoid
144655505Sshinharmonize_rtr()
144755505Sshin{
144855505Sshin	char dummyif[IFNAMSIZ+8];
144955505Sshin	int s;
145055505Sshin
145162590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
145262590Sitojun		err(1, "socket");
1453121156Sume	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
145462590Sitojun	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1455121156Sume		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
145662590Sitojun
145762590Sitojun	close(s);
145855505Sshin}
145955505Sshin
146062590Sitojun#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
146162590Sitojunstatic void
1462259169Saesetdefif(char *ifname)
146362590Sitojun{
146462590Sitojun	struct in6_ndifreq ndifreq;
146562590Sitojun	unsigned int ifindex;
146662590Sitojun
146762590Sitojun	if (strcasecmp(ifname, "delete") == 0)
146862590Sitojun		ifindex = 0;
146962590Sitojun	else {
147062590Sitojun		if ((ifindex = if_nametoindex(ifname)) == 0)
147162590Sitojun			err(1, "failed to resolve i/f index for %s", ifname);
147262590Sitojun	}
147362590Sitojun
147462590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
147562590Sitojun		err(1, "socket");
147662590Sitojun
1477121156Sume	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
147862590Sitojun	ndifreq.ifindex = ifindex;
147962590Sitojun
148062590Sitojun	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1481121156Sume		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
148262590Sitojun
148362590Sitojun	close(s);
148462590Sitojun}
148562590Sitojun
148662590Sitojunstatic void
148762590Sitojungetdefif()
148862590Sitojun{
148962590Sitojun	struct in6_ndifreq ndifreq;
149062590Sitojun	char ifname[IFNAMSIZ+8];
149162590Sitojun
149262590Sitojun	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
149362590Sitojun		err(1, "socket");
149462590Sitojun
149562590Sitojun	memset(&ndifreq, 0, sizeof(ndifreq));
1496121156Sume	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
149762590Sitojun
149862590Sitojun	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1499121156Sume		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
150062590Sitojun
150162590Sitojun	if (ndifreq.ifindex == 0)
150262590Sitojun		printf("No default interface.\n");
150362590Sitojun	else {
150462590Sitojun		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
150562590Sitojun			err(1, "failed to resolve ifname for index %lu",
150662590Sitojun			    ndifreq.ifindex);
150762590Sitojun		printf("ND default interface = %s\n", ifname);
150862590Sitojun	}
150962590Sitojun
151062590Sitojun	close(s);
151162590Sitojun}
151262590Sitojun#endif
151362590Sitojun
151455505Sshinstatic char *
1515259169Saesec2str(time_t total)
151655505Sshin{
151755505Sshin	static char result[256];
151855505Sshin	int days, hours, mins, secs;
151955505Sshin	int first = 1;
152055505Sshin	char *p = result;
1521121156Sume	char *ep = &result[sizeof(result)];
1522121156Sume	int n;
152355505Sshin
152455505Sshin	days = total / 3600 / 24;
152555505Sshin	hours = (total / 3600) % 24;
152655505Sshin	mins = (total / 60) % 60;
152755505Sshin	secs = total % 60;
152855505Sshin
152955505Sshin	if (days) {
153055505Sshin		first = 0;
1531121156Sume		n = snprintf(p, ep - p, "%dd", days);
1532121156Sume		if (n < 0 || n >= ep - p)
1533121156Sume			return "?";
1534121156Sume		p += n;
153555505Sshin	}
153655505Sshin	if (!first || hours) {
153755505Sshin		first = 0;
1538121156Sume		n = snprintf(p, ep - p, "%dh", hours);
1539121156Sume		if (n < 0 || n >= ep - p)
1540121156Sume			return "?";
1541121156Sume		p += n;
154255505Sshin	}
154355505Sshin	if (!first || mins) {
154455505Sshin		first = 0;
1545121156Sume		n = snprintf(p, ep - p, "%dm", mins);
1546121156Sume		if (n < 0 || n >= ep - p)
1547121156Sume			return "?";
1548121156Sume		p += n;
154955505Sshin	}
1550121156Sume	snprintf(p, ep - p, "%ds", secs);
155155505Sshin
155255505Sshin	return(result);
155355505Sshin}
155455505Sshin
155555505Sshin/*
155655505Sshin * Print the timestamp
155755505Sshin * from tcpdump/util.c
155855505Sshin */
155955505Sshinstatic void
1560259169Saets_print(const struct timeval *tvp)
156155505Sshin{
156255505Sshin	int s;
156355505Sshin
156455505Sshin	/* Default */
1565253999Shrs	s = (tvp->tv_sec + thiszone) % 86400;
156655505Sshin	(void)printf("%02d:%02d:%02d.%06u ",
1567253999Shrs	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
156855505Sshin}
1569186119Sqingli
1570186119Sqingli#undef NEXTADDR
1571