Home | History | Annotate | Line # | Download | only in ndp
ndp.c revision 1.57
      1  1.57       roy /*	$NetBSD: ndp.c,v 1.57 2020/06/12 21:08:02 roy Exp $	*/
      2  1.34    rpaulo /*	$KAME: ndp.c,v 1.121 2005/07/13 11:30:13 keiichi Exp $	*/
      3   1.9    itojun 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
      6   1.1    itojun  * All rights reserved.
      7   1.9    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19   1.9    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  */
     32   1.1    itojun /*
     33   1.1    itojun  * Copyright (c) 1984, 1993
     34   1.1    itojun  *	The Regents of the University of California.  All rights reserved.
     35   1.1    itojun  *
     36   1.1    itojun  * This code is derived from software contributed to Berkeley by
     37   1.1    itojun  * Sun Microsystems, Inc.
     38   1.1    itojun  *
     39   1.1    itojun  * Redistribution and use in source and binary forms, with or without
     40   1.1    itojun  * modification, are permitted provided that the following conditions
     41   1.1    itojun  * are met:
     42   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     43   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     44   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     46   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     47  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     48   1.1    itojun  *    may be used to endorse or promote products derived from this software
     49   1.1    itojun  *    without specific prior written permission.
     50   1.1    itojun  *
     51   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61   1.1    itojun  * SUCH DAMAGE.
     62   1.1    itojun  */
     63   1.1    itojun 
     64   1.1    itojun /*
     65   1.1    itojun  * Based on:
     66   1.1    itojun  * "@(#) Copyright (c) 1984, 1993\n\
     67   1.1    itojun  *	The Regents of the University of California.  All rights reserved.\n";
     68   1.1    itojun  *
     69   1.1    itojun  * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
     70   1.1    itojun  */
     71   1.1    itojun 
     72   1.1    itojun /*
     73   1.1    itojun  * ndp - display, set, delete and flush neighbor cache
     74   1.1    itojun  */
     75   1.1    itojun 
     76   1.1    itojun 
     77   1.1    itojun #include <sys/param.h>
     78   1.1    itojun #include <sys/file.h>
     79   1.1    itojun #include <sys/ioctl.h>
     80   1.1    itojun #include <sys/socket.h>
     81   1.1    itojun #include <sys/sysctl.h>
     82   1.1    itojun #include <sys/time.h>
     83   1.1    itojun 
     84   1.1    itojun #include <net/if.h>
     85   1.1    itojun #include <net/if_dl.h>
     86   1.1    itojun #include <net/if_types.h>
     87   1.1    itojun #include <net/route.h>
     88   1.1    itojun 
     89   1.1    itojun #include <netinet/in.h>
     90   1.1    itojun 
     91   1.1    itojun #include <netinet/icmp6.h>
     92   1.1    itojun #include <netinet6/in6_var.h>
     93   1.1    itojun #include <netinet6/nd6.h>
     94   1.1    itojun 
     95   1.1    itojun #include <arpa/inet.h>
     96   1.1    itojun 
     97   1.1    itojun #include <netdb.h>
     98   1.1    itojun #include <errno.h>
     99   1.1    itojun #include <nlist.h>
    100   1.1    itojun #include <stdio.h>
    101   1.1    itojun #include <string.h>
    102   1.1    itojun #include <paths.h>
    103   1.1    itojun #include <err.h>
    104   1.1    itojun #include <stdlib.h>
    105   1.1    itojun #include <fcntl.h>
    106   1.1    itojun #include <unistd.h>
    107  1.45     ozaki 
    108   1.1    itojun #include "gmt2local.h"
    109  1.45     ozaki #include "prog_ops.h"
    110   1.1    itojun 
    111  1.19    itojun static pid_t pid;
    112   1.1    itojun static int nflag;
    113   1.1    itojun static int tflag;
    114   1.1    itojun static int32_t thiszone;	/* time difference with gmt */
    115  1.35  christos static int my_s = -1;
    116  1.35  christos static unsigned int repeat = 0;
    117   1.1    itojun 
    118  1.35  christos 
    119  1.35  christos static char host_buf[NI_MAXHOST];		/* getnameinfo() */
    120  1.35  christos static char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
    121  1.35  christos 
    122  1.35  christos static void getsocket(void);
    123  1.35  christos static int set(int, char **);
    124  1.35  christos static void get(char *);
    125  1.50     ozaki static int delete(struct rt_msghdr *, char *);
    126  1.50     ozaki static void delete_one(char *);
    127  1.50     ozaki static void do_foreach(struct in6_addr *, char *, int);
    128  1.35  christos static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, unsigned int, int);
    129  1.35  christos static char *ether_str(struct sockaddr_dl *);
    130  1.35  christos static int ndp_ether_aton(char *, u_char *);
    131  1.40     joerg __dead static void usage(void);
    132  1.50     ozaki static int rtmsg(int, struct rt_msghdr *);
    133  1.35  christos static void ifinfo(char *, int, char **);
    134  1.35  christos static const char *sec2str(time_t);
    135  1.35  christos static char *ether_str(struct sockaddr_dl *);
    136  1.35  christos static void ts_print(const struct timeval *);
    137   1.1    itojun 
    138  1.50     ozaki #define NDP_F_CLEAR	1
    139  1.50     ozaki #define NDP_F_DELETE	2
    140  1.50     ozaki 
    141  1.35  christos static int mode = 0;
    142  1.35  christos static char *arg = NULL;
    143  1.22    itojun 
    144   1.1    itojun int
    145  1.35  christos main(int argc, char **argv)
    146   1.1    itojun {
    147   1.1    itojun 	int ch;
    148   1.1    itojun 
    149  1.56       roy 	while ((ch = getopt(argc, argv, "acd:f:i:nstA:")) != -1)
    150  1.22    itojun 		switch (ch) {
    151   1.1    itojun 		case 'a':
    152   1.1    itojun 		case 'c':
    153  1.22    itojun 		case 's':
    154   1.1    itojun 		case 'd':
    155  1.22    itojun 		case 'f':
    156   1.1    itojun 		case 'i' :
    157  1.22    itojun 			if (mode) {
    158   1.1    itojun 				usage();
    159  1.22    itojun 				/*NOTREACHED*/
    160  1.22    itojun 			}
    161  1.22    itojun 			mode = ch;
    162  1.22    itojun 			arg = optarg;
    163  1.22    itojun 			break;
    164   1.1    itojun 		case 'n':
    165   1.1    itojun 			nflag = 1;
    166   1.1    itojun 			break;
    167   1.1    itojun 		case 't':
    168   1.1    itojun 			tflag = 1;
    169   1.1    itojun 			break;
    170   1.1    itojun 		case 'A':
    171  1.22    itojun 			if (mode) {
    172  1.22    itojun 				usage();
    173  1.22    itojun 				/*NOTREACHED*/
    174  1.22    itojun 			}
    175  1.22    itojun 			mode = 'a';
    176   1.1    itojun 			repeat = atoi(optarg);
    177   1.1    itojun 			break;
    178   1.1    itojun 		default:
    179   1.1    itojun 			usage();
    180   1.1    itojun 		}
    181   1.1    itojun 
    182   1.1    itojun 	argc -= optind;
    183   1.1    itojun 	argv += optind;
    184   1.1    itojun 
    185  1.45     ozaki 	if (prog_init && prog_init() == -1)
    186  1.45     ozaki 		err(1, "init failed");
    187  1.45     ozaki 
    188  1.45     ozaki 	pid = prog_getpid();
    189  1.45     ozaki 	thiszone = gmt2local(0L);
    190  1.45     ozaki 
    191  1.22    itojun 	switch (mode) {
    192  1.22    itojun 	case 'a':
    193  1.22    itojun 	case 'c':
    194  1.22    itojun 		if (argc != 0) {
    195  1.22    itojun 			usage();
    196  1.22    itojun 			/*NOTREACHED*/
    197  1.22    itojun 		}
    198  1.50     ozaki 		do_foreach(0, NULL, mode == 'c' ? NDP_F_CLEAR : 0);
    199  1.22    itojun 		break;
    200  1.22    itojun 	case 'd':
    201  1.22    itojun 		if (argc != 0) {
    202  1.22    itojun 			usage();
    203  1.22    itojun 			/*NOTREACHED*/
    204  1.22    itojun 		}
    205  1.50     ozaki 		delete_one(arg);
    206  1.22    itojun 		break;
    207  1.22    itojun 	case 'i':
    208  1.22    itojun 		ifinfo(arg, argc, argv);
    209  1.22    itojun 		break;
    210  1.22    itojun 	case 's':
    211   1.1    itojun 		if (argc < 2 || argc > 4)
    212   1.1    itojun 			usage();
    213  1.35  christos 		return(set(argc, argv) ? 1 : 0);
    214  1.22    itojun 	case 0:
    215  1.22    itojun 		if (argc != 1) {
    216  1.22    itojun 			usage();
    217  1.22    itojun 			/*NOTREACHED*/
    218  1.22    itojun 		}
    219  1.22    itojun 		get(argv[0]);
    220  1.22    itojun 		break;
    221   1.1    itojun 	}
    222  1.35  christos 	return(0);
    223   1.1    itojun }
    224   1.1    itojun 
    225  1.35  christos static void
    226  1.46  christos makeaddr(struct sockaddr_in6 *mysin, const void *resp)
    227  1.46  christos {
    228  1.46  christos 	const struct sockaddr_in6 *res = resp;
    229  1.46  christos 	mysin->sin6_addr = res->sin6_addr;
    230  1.46  christos 	mysin->sin6_scope_id = res->sin6_scope_id;
    231  1.46  christos 	inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
    232  1.46  christos }
    233  1.46  christos 
    234  1.46  christos static void
    235  1.35  christos getsocket(void)
    236   1.1    itojun {
    237  1.35  christos 	if (my_s < 0) {
    238  1.45     ozaki 		my_s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
    239  1.35  christos 		if (my_s < 0)
    240  1.28    itojun 			err(1, "socket");
    241   1.1    itojun 	}
    242   1.1    itojun }
    243   1.1    itojun 
    244  1.35  christos #ifdef notdef
    245  1.36  christos static struct sockaddr_in6 so_mask = {
    246  1.36  christos 	.sin6_len = sizeof(so_mask),
    247  1.36  christos 	.sin6_family = AF_INET6
    248  1.36  christos };
    249  1.35  christos #endif
    250  1.36  christos static struct sockaddr_in6 blank_sin = {
    251  1.36  christos 	.sin6_len = sizeof(blank_sin),
    252  1.36  christos 	.sin6_family = AF_INET6
    253  1.36  christos };
    254  1.36  christos static struct sockaddr_in6 sin_m;
    255  1.36  christos static struct sockaddr_dl blank_sdl = {
    256  1.36  christos 	.sdl_len = sizeof(blank_sdl),
    257  1.36  christos 	.sdl_family = AF_LINK,
    258  1.36  christos };
    259  1.36  christos static struct sockaddr_dl sdl_m;
    260  1.36  christos static int expire_time, flags, found_entry;
    261  1.36  christos static struct {
    262   1.1    itojun 	struct	rt_msghdr m_rtm;
    263   1.1    itojun 	char	m_space[512];
    264  1.36  christos } m_rtmsg;
    265   1.1    itojun 
    266   1.1    itojun /*
    267   1.1    itojun  * Set an individual neighbor cache entry
    268   1.1    itojun  */
    269  1.35  christos static int
    270  1.35  christos set(int argc, char **argv)
    271   1.1    itojun {
    272  1.35  christos 	register struct sockaddr_in6 *mysin = &sin_m;
    273   1.1    itojun 	register struct sockaddr_dl *sdl;
    274   1.1    itojun 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
    275   1.1    itojun 	struct addrinfo hints, *res;
    276   1.1    itojun 	int gai_error;
    277   1.1    itojun 	u_char *ea;
    278   1.1    itojun 	char *host = argv[0], *eaddr = argv[1];
    279   1.1    itojun 
    280   1.1    itojun 	getsocket();
    281   1.1    itojun 	argc -= 2;
    282   1.1    itojun 	argv += 2;
    283   1.1    itojun 	sdl_m = blank_sdl;
    284   1.1    itojun 	sin_m = blank_sin;
    285   1.1    itojun 
    286  1.35  christos 	(void)memset(&hints, 0, sizeof(hints));
    287   1.1    itojun 	hints.ai_family = AF_INET6;
    288   1.1    itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    289   1.1    itojun 	if (gai_error) {
    290  1.44  christos 		warnx("%s: %s", host, gai_strerror(gai_error));
    291   1.1    itojun 		return 1;
    292   1.1    itojun 	}
    293  1.46  christos 	makeaddr(mysin, res->ai_addr);
    294  1.51  christos 	freeaddrinfo(res);
    295   1.1    itojun 	ea = (u_char *)LLADDR(&sdl_m);
    296   1.1    itojun 	if (ndp_ether_aton(eaddr, ea) == 0)
    297   1.1    itojun 		sdl_m.sdl_alen = 6;
    298   1.1    itojun 	flags = expire_time = 0;
    299   1.1    itojun 	while (argc-- > 0) {
    300   1.1    itojun 		if (strncmp(argv[0], "temp", 4) == 0) {
    301  1.35  christos 			struct timeval tim;
    302  1.21    itojun 
    303  1.35  christos 			(void)gettimeofday(&tim, 0);
    304  1.35  christos 			expire_time = tim.tv_sec + 20 * 60;
    305   1.6    itojun 		} else if (strncmp(argv[0], "proxy", 5) == 0)
    306   1.6    itojun 			flags |= RTF_ANNOUNCE;
    307   1.1    itojun 		argv++;
    308   1.1    itojun 	}
    309  1.50     ozaki 	if (rtmsg(RTM_GET, NULL) < 0) {
    310  1.16    itojun 		errx(1, "RTM_GET(%s) failed", host);
    311  1.16    itojun 		/* NOTREACHED */
    312   1.1    itojun 	}
    313  1.35  christos 	mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
    314  1.39  drochner 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + (char *)(void *)mysin);
    315  1.35  christos 	if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
    316   1.1    itojun 		if (sdl->sdl_family == AF_LINK &&
    317  1.22    itojun 		    !(rtm->rtm_flags & RTF_GATEWAY)) {
    318  1.22    itojun 			switch (sdl->sdl_type) {
    319  1.22    itojun 			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    320  1.22    itojun 			case IFT_ISO88024: case IFT_ISO88025:
    321  1.22    itojun 				goto overwrite;
    322  1.22    itojun 			}
    323   1.1    itojun 		}
    324   1.9    itojun 		/*
    325   1.9    itojun 		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
    326   1.9    itojun 		 */
    327  1.35  christos 		(void)fprintf(stderr, "set: cannot configure a new entry\n");
    328   1.9    itojun 		return 1;
    329   1.1    itojun 	}
    330   1.9    itojun 
    331   1.1    itojun overwrite:
    332   1.1    itojun 	if (sdl->sdl_family != AF_LINK) {
    333  1.35  christos 		warnx("cannot intuit interface index and type for %s", host);
    334   1.1    itojun 		return (1);
    335   1.1    itojun 	}
    336   1.1    itojun 	sdl_m.sdl_type = sdl->sdl_type;
    337   1.1    itojun 	sdl_m.sdl_index = sdl->sdl_index;
    338  1.50     ozaki 	return (rtmsg(RTM_ADD, NULL));
    339   1.1    itojun }
    340   1.1    itojun 
    341   1.1    itojun /*
    342   1.1    itojun  * Display an individual neighbor cache entry
    343   1.1    itojun  */
    344  1.35  christos static void
    345  1.35  christos get(char *host)
    346   1.1    itojun {
    347  1.35  christos 	struct sockaddr_in6 *mysin = &sin_m;
    348   1.1    itojun 	struct addrinfo hints, *res;
    349   1.1    itojun 	int gai_error;
    350   1.1    itojun 
    351   1.1    itojun 	sin_m = blank_sin;
    352  1.35  christos 	(void)memset(&hints, 0, sizeof(hints));
    353   1.1    itojun 	hints.ai_family = AF_INET6;
    354   1.1    itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    355   1.1    itojun 	if (gai_error) {
    356  1.44  christos 		warnx("%s: %s", host, gai_strerror(gai_error));
    357   1.1    itojun 		return;
    358   1.1    itojun 	}
    359  1.46  christos 	makeaddr(mysin, res->ai_addr);
    360  1.51  christos 	freeaddrinfo(res);
    361  1.50     ozaki 	do_foreach(&mysin->sin6_addr, host, 0);
    362   1.1    itojun 	if (found_entry == 0) {
    363  1.35  christos 		(void)getnameinfo((struct sockaddr *)(void *)mysin,
    364  1.35  christos 		    (socklen_t)mysin->sin6_len,
    365  1.35  christos 		    host_buf, sizeof(host_buf), NULL ,0,
    366  1.21    itojun 		    (nflag ? NI_NUMERICHOST : 0));
    367  1.35  christos 		errx(1, "%s (%s) -- no entry", host, host_buf);
    368   1.1    itojun 	}
    369   1.1    itojun }
    370   1.1    itojun 
    371  1.50     ozaki static void
    372  1.50     ozaki delete_one(char *host)
    373   1.1    itojun {
    374  1.35  christos 	struct sockaddr_in6 *mysin = &sin_m;
    375   1.1    itojun 	struct addrinfo hints, *res;
    376   1.1    itojun 	int gai_error;
    377   1.1    itojun 
    378   1.1    itojun 	sin_m = blank_sin;
    379  1.50     ozaki 	(void)memset(&hints, 0, sizeof(hints));
    380   1.1    itojun 	hints.ai_family = AF_INET6;
    381   1.1    itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    382   1.1    itojun 	if (gai_error) {
    383  1.44  christos 		warnx("%s: %s", host, gai_strerror(gai_error));
    384  1.50     ozaki 		return;
    385   1.1    itojun 	}
    386  1.46  christos 	makeaddr(mysin, res->ai_addr);
    387  1.51  christos 	freeaddrinfo(res);
    388  1.50     ozaki 	do_foreach(&mysin->sin6_addr, host, NDP_F_DELETE);
    389  1.50     ozaki }
    390  1.50     ozaki 
    391  1.50     ozaki /*
    392  1.50     ozaki  * Delete a neighbor cache entry
    393  1.50     ozaki  */
    394  1.50     ozaki static int
    395  1.50     ozaki delete(struct rt_msghdr *rtm, char *host)
    396  1.50     ozaki {
    397  1.53    nonaka 	char delete_host_buf[NI_MAXHOST];
    398  1.50     ozaki 	struct sockaddr_in6 *mysin = &sin_m;
    399  1.50     ozaki 	struct sockaddr_dl *sdl;
    400  1.50     ozaki 
    401  1.50     ozaki 	getsocket();
    402  1.35  christos 	mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
    403  1.39  drochner 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) +
    404  1.35  christos 	    (char *)(void *)mysin);
    405   1.8    itojun 
    406   1.1    itojun 	if (sdl->sdl_family != AF_LINK) {
    407  1.35  christos 		(void)printf("cannot locate %s\n", host);
    408   1.1    itojun 		return (1);
    409   1.1    itojun 	}
    410  1.50     ozaki 	if (rtmsg(RTM_DELETE, rtm) == 0) {
    411  1.35  christos 		struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */
    412  1.11    itojun 
    413  1.54    nonaka 		s6.sin6_scope_id = 0;
    414  1.54    nonaka 		inet6_putscopeid(&s6, INET6_IS_ADDR_LINKLOCAL);
    415  1.35  christos 		(void)getnameinfo((struct sockaddr *)(void *)&s6,
    416  1.53    nonaka 		    (socklen_t)s6.sin6_len, delete_host_buf,
    417  1.53    nonaka 		    sizeof(delete_host_buf), NULL, 0,
    418  1.21    itojun 		    (nflag ? NI_NUMERICHOST : 0));
    419  1.53    nonaka 		(void)printf("%s (%s) deleted\n", host, delete_host_buf);
    420   1.4    itojun 	}
    421   1.4    itojun 
    422   1.1    itojun 	return 0;
    423   1.1    itojun }
    424   1.1    itojun 
    425  1.48  christos #define W_ADDR	(8 * 4 + 7)
    426  1.13    itojun #define W_LL	17
    427  1.13    itojun #define W_IF	6
    428  1.13    itojun 
    429   1.1    itojun /*
    430  1.50     ozaki  * Iterate on neighbor caches and do
    431  1.50     ozaki  * - dump all caches,
    432  1.50     ozaki  * - clear all caches (NDP_F_CLEAR) or
    433  1.50     ozaki  * - remove matched caches (NDP_F_DELETE)
    434   1.1    itojun  */
    435  1.35  christos static void
    436  1.50     ozaki do_foreach(struct in6_addr *addr, char *host, int _flags)
    437   1.1    itojun {
    438   1.1    itojun 	int mib[6];
    439   1.1    itojun 	size_t needed;
    440   1.4    itojun 	char *lim, *buf, *next;
    441   1.1    itojun 	struct rt_msghdr *rtm;
    442  1.35  christos 	struct sockaddr_in6 *mysin;
    443   1.1    itojun 	struct sockaddr_dl *sdl;
    444   1.1    itojun 	struct in6_nbrinfo *nbi;
    445  1.35  christos 	struct timeval tim;
    446   1.4    itojun 	int addrwidth;
    447  1.13    itojun 	int llwidth;
    448  1.13    itojun 	int ifwidth;
    449  1.48  christos 	char flgbuf[8], *fl;
    450  1.35  christos 	const char *ifname;
    451  1.50     ozaki 	int cflag = _flags == NDP_F_CLEAR;
    452  1.50     ozaki 	int dflag = _flags == NDP_F_DELETE;
    453   1.1    itojun 
    454   1.1    itojun 	/* Print header */
    455  1.11    itojun 	if (!tflag && !cflag)
    456  1.48  christos 		(void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %2s\n",
    457  1.13    itojun 		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
    458  1.48  christos 		    W_IF, W_IF, "Netif", "Expire", "S", "Fl");
    459   1.1    itojun 
    460   1.1    itojun again:;
    461   1.1    itojun 	mib[0] = CTL_NET;
    462   1.1    itojun 	mib[1] = PF_ROUTE;
    463   1.1    itojun 	mib[2] = 0;
    464   1.1    itojun 	mib[3] = AF_INET6;
    465   1.1    itojun 	mib[4] = NET_RT_FLAGS;
    466  1.50     ozaki 	mib[5] = RTF_LLDATA;
    467  1.45     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    468   1.3    itojun 		err(1, "sysctl(PF_ROUTE estimate)");
    469   1.3    itojun 	if (needed > 0) {
    470   1.3    itojun 		if ((buf = malloc(needed)) == NULL)
    471  1.28    itojun 			err(1, "malloc");
    472  1.50     ozaki 		if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    473  1.50     ozaki 			free(buf);
    474  1.50     ozaki 			if (errno == ENOBUFS)
    475  1.50     ozaki 				goto again;
    476   1.3    itojun 			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
    477  1.50     ozaki 		}
    478   1.3    itojun 		lim = buf + needed;
    479   1.3    itojun 	} else
    480   1.3    itojun 		buf = lim = NULL;
    481   1.1    itojun 
    482   1.3    itojun 	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
    483   1.1    itojun 		int isrouter = 0, prbs = 0;
    484   1.1    itojun 
    485  1.35  christos 		rtm = (struct rt_msghdr *)(void *)next;
    486  1.35  christos 		mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
    487  1.39  drochner 		sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + RT_ROUNDUP(mysin->sin6_len));
    488  1.16    itojun 
    489  1.16    itojun 		/*
    490  1.16    itojun 		 * Some OSes can produce a route that has the LINK flag but
    491  1.16    itojun 		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
    492  1.16    itojun 		 * and BSD/OS, where xx is not the interface identifier on
    493  1.16    itojun 		 * lo0).  Such routes entry would annoy getnbrinfo() below,
    494  1.16    itojun 		 * so we skip them.
    495  1.16    itojun 		 * XXX: such routes should have the GATEWAY flag, not the
    496  1.16    itojun 		 * LINK flag.  However, there is rotten routing software
    497  1.16    itojun 		 * that advertises all routes that have the GATEWAY flag.
    498  1.16    itojun 		 * Thus, KAME kernel intentionally does not set the LINK flag.
    499  1.16    itojun 		 * What is to be fixed is not ndp, but such routing software
    500  1.16    itojun 		 * (and the kernel workaround)...
    501  1.16    itojun 		 */
    502  1.16    itojun 		if (sdl->sdl_family != AF_LINK)
    503  1.16    itojun 			continue;
    504  1.16    itojun 
    505  1.19    itojun 		if (!(rtm->rtm_flags & RTF_HOST))
    506  1.19    itojun 			continue;
    507  1.19    itojun 
    508   1.1    itojun 		if (addr) {
    509  1.35  christos 			if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr))
    510   1.1    itojun 				continue;
    511   1.1    itojun 			found_entry = 1;
    512  1.35  christos 		} else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr))
    513   1.1    itojun 			continue;
    514  1.50     ozaki 		if (dflag) {
    515  1.50     ozaki 			(void)delete(rtm, host_buf);
    516  1.50     ozaki 			continue;
    517  1.50     ozaki 		}
    518  1.35  christos 		if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) ||
    519  1.35  christos 		    IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) {
    520  1.41  christos 			uint16_t scopeid = mysin->sin6_scope_id;
    521  1.41  christos 			inet6_getscopeid(mysin, INET6_IS_ADDR_LINKLOCAL|
    522  1.41  christos 			    INET6_IS_ADDR_MC_LINKLOCAL);
    523  1.41  christos 			if (scopeid == 0)
    524  1.35  christos 				mysin->sin6_scope_id = sdl->sdl_index;
    525   1.1    itojun 		}
    526  1.35  christos 		(void)getnameinfo((struct sockaddr *)(void *)mysin,
    527  1.35  christos 		    (socklen_t)mysin->sin6_len,
    528  1.35  christos 		    host_buf, sizeof(host_buf), NULL, 0,
    529  1.35  christos 		    (nflag ? NI_NUMERICHOST : 0));
    530  1.22    itojun 		if (cflag) {
    531  1.50     ozaki 			/* Restore scopeid */
    532  1.50     ozaki 			if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) ||
    533  1.50     ozaki 			    IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr))
    534  1.50     ozaki 				inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL|
    535  1.50     ozaki 				    INET6_IS_ADDR_MC_LINKLOCAL);
    536  1.47     ozaki 			if ((rtm->rtm_flags & RTF_STATIC) == 0)
    537  1.50     ozaki 				(void)delete(rtm, host_buf);
    538  1.11    itojun 			continue;
    539  1.11    itojun 		}
    540  1.35  christos 		(void)gettimeofday(&tim, 0);
    541   1.1    itojun 		if (tflag)
    542  1.35  christos 			ts_print(&tim);
    543   1.1    itojun 
    544  1.13    itojun 		addrwidth = strlen(host_buf);
    545  1.13    itojun 		if (addrwidth < W_ADDR)
    546  1.13    itojun 			addrwidth = W_ADDR;
    547  1.13    itojun 		llwidth = strlen(ether_str(sdl));
    548  1.13    itojun 		if (W_ADDR + W_LL - addrwidth > llwidth)
    549  1.13    itojun 			llwidth = W_ADDR + W_LL - addrwidth;
    550  1.35  christos 		ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf);
    551  1.14    itojun 		if (!ifname)
    552  1.14    itojun 			ifname = "?";
    553  1.14    itojun 		ifwidth = strlen(ifname);
    554  1.13    itojun 		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
    555  1.13    itojun 			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
    556  1.13    itojun 
    557  1.35  christos 		(void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth,
    558  1.35  christos 		    host_buf, llwidth, llwidth, ether_str(sdl), ifwidth,
    559  1.35  christos 		    ifwidth, ifname);
    560   1.1    itojun 
    561   1.1    itojun 		/* Print neighbor discovery specific informations */
    562  1.35  christos 		nbi = getnbrinfo(&mysin->sin6_addr,
    563  1.35  christos 		    (unsigned int)sdl->sdl_index, 1);
    564   1.1    itojun 		if (nbi) {
    565  1.35  christos 			if (nbi->expire > tim.tv_sec) {
    566  1.35  christos 				(void)printf(" %-9.9s",
    567  1.35  christos 				    sec2str(nbi->expire - tim.tv_sec));
    568  1.11    itojun 			} else if (nbi->expire == 0)
    569  1.35  christos 				(void)printf(" %-9.9s", "permanent");
    570   1.1    itojun 			else
    571  1.35  christos 				(void)printf(" %-9.9s", "expired");
    572   1.1    itojun 
    573  1.21    itojun 			switch (nbi->state) {
    574  1.19    itojun 			case ND6_LLINFO_NOSTATE:
    575  1.35  christos 				 (void)printf(" N");
    576   1.1    itojun 				 break;
    577  1.11    itojun #ifdef ND6_LLINFO_WAITDELETE
    578  1.19    itojun 			case ND6_LLINFO_WAITDELETE:
    579  1.35  christos 				 (void)printf(" W");
    580   1.1    itojun 				 break;
    581  1.11    itojun #endif
    582  1.19    itojun 			case ND6_LLINFO_INCOMPLETE:
    583  1.35  christos 				 (void)printf(" I");
    584   1.1    itojun 				 break;
    585  1.19    itojun 			case ND6_LLINFO_REACHABLE:
    586  1.35  christos 				 (void)printf(" R");
    587   1.1    itojun 				 break;
    588  1.19    itojun 			case ND6_LLINFO_STALE:
    589  1.35  christos 				 (void)printf(" S");
    590   1.1    itojun 				 break;
    591  1.19    itojun 			case ND6_LLINFO_DELAY:
    592  1.35  christos 				 (void)printf(" D");
    593   1.1    itojun 				 break;
    594  1.19    itojun 			case ND6_LLINFO_PROBE:
    595  1.35  christos 				 (void)printf(" P");
    596   1.1    itojun 				 break;
    597  1.19    itojun 			default:
    598  1.35  christos 				 (void)printf(" ?");
    599   1.1    itojun 				 break;
    600   1.1    itojun 			}
    601   1.1    itojun 
    602   1.1    itojun 			isrouter = nbi->isrouter;
    603   1.1    itojun 			prbs = nbi->asked;
    604  1.11    itojun 		} else {
    605   1.1    itojun 			warnx("failed to get neighbor information");
    606  1.35  christos 			(void)printf("  ");
    607   1.1    itojun 		}
    608   1.1    itojun 
    609   1.4    itojun 		/*
    610   1.4    itojun 		 * other flags. R: router, P: proxy, W: ??
    611   1.4    itojun 		 */
    612  1.48  christos 		fl = flgbuf;
    613  1.48  christos 		if (isrouter)
    614  1.48  christos 			*fl++ = 'R';
    615  1.48  christos 		if (rtm->rtm_flags & RTF_ANNOUNCE)
    616  1.48  christos 			*fl++ = 'p';
    617  1.48  christos 		*fl++ = '\0';
    618  1.35  christos 		(void)printf(" %s", flgbuf);
    619   1.1    itojun 
    620   1.1    itojun 		if (prbs)
    621  1.35  christos 			(void)printf(" %d", prbs);
    622   1.1    itojun 
    623  1.35  christos 		(void)printf("\n");
    624   1.1    itojun 	}
    625  1.12    itojun 	if (buf != NULL)
    626  1.12    itojun 		free(buf);
    627   1.1    itojun 
    628   1.1    itojun 	if (repeat) {
    629  1.35  christos 		(void)printf("\n");
    630  1.35  christos 		(void)fflush(stdout);
    631  1.35  christos 		(void)sleep(repeat);
    632   1.1    itojun 		goto again;
    633   1.1    itojun 	}
    634   1.1    itojun }
    635   1.1    itojun 
    636   1.1    itojun static struct in6_nbrinfo *
    637  1.35  christos getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning)
    638   1.1    itojun {
    639   1.1    itojun 	static struct in6_nbrinfo nbi;
    640   1.1    itojun 	int s;
    641   1.1    itojun 
    642  1.45     ozaki 	if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    643   1.1    itojun 		err(1, "socket");
    644   1.1    itojun 
    645  1.35  christos 	(void)memset(&nbi, 0, sizeof(nbi));
    646  1.35  christos 	(void)if_indextoname(ifindex, nbi.ifname);
    647   1.1    itojun 	nbi.addr = *addr;
    648  1.45     ozaki 	if (prog_ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) {
    649   1.4    itojun 		if (warning)
    650   1.8    itojun 			warn("ioctl(SIOCGNBRINFO_IN6)");
    651  1.45     ozaki 		(void)prog_close(s);
    652   1.1    itojun 		return(NULL);
    653   1.1    itojun 	}
    654   1.1    itojun 
    655  1.45     ozaki 	(void)prog_close(s);
    656   1.1    itojun 	return(&nbi);
    657   1.1    itojun }
    658   1.1    itojun 
    659   1.1    itojun static char *
    660  1.35  christos ether_str(struct sockaddr_dl *sdl)
    661   1.1    itojun {
    662  1.17     bjh21 	static char hbuf[NI_MAXHOST];
    663   1.1    itojun 
    664   1.1    itojun 	if (sdl->sdl_alen) {
    665  1.35  christos 		if (getnameinfo((struct sockaddr *)(void *)sdl,
    666  1.35  christos 		    (socklen_t)sdl->sdl_len,
    667  1.17     bjh21 		    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    668  1.35  christos 			(void)snprintf(hbuf, sizeof(hbuf), "<invalid>");
    669  1.20    itojun 	} else
    670  1.35  christos 		(void)snprintf(hbuf, sizeof(hbuf), "(incomplete)");
    671   1.1    itojun 
    672  1.17     bjh21 	return(hbuf);
    673   1.1    itojun }
    674   1.1    itojun 
    675  1.35  christos static int
    676  1.35  christos ndp_ether_aton(char *a, u_char *n)
    677   1.1    itojun {
    678   1.1    itojun 	int i, o[6];
    679   1.1    itojun 
    680   1.1    itojun 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
    681  1.21    itojun 	    &o[3], &o[4], &o[5]);
    682   1.1    itojun 	if (i != 6) {
    683  1.35  christos 		warnx("invalid Ethernet address '%s'", a);
    684   1.1    itojun 		return (1);
    685   1.1    itojun 	}
    686  1.24    itojun 	for (i = 0; i < 6; i++)
    687   1.1    itojun 		n[i] = o[i];
    688   1.1    itojun 	return (0);
    689   1.1    itojun }
    690   1.1    itojun 
    691  1.35  christos static void
    692  1.35  christos usage(void)
    693   1.1    itojun {
    694  1.35  christos 	const char *pn = getprogname();
    695  1.35  christos 
    696  1.35  christos 	(void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn);
    697  1.35  christos 	(void)fprintf(stderr,
    698  1.57       roy 	    "       %s [-nt] -a | -c\n", pn);
    699  1.35  christos 	(void)fprintf(stderr, "       %s [-nt] -A wait\n", pn);
    700  1.35  christos 	(void)fprintf(stderr, "       %s [-nt] -d hostname\n", pn);
    701  1.35  christos 	(void)fprintf(stderr, "       %s [-nt] -f filename\n", pn);
    702  1.35  christos 	(void)fprintf(stderr, "       %s [-nt] -i interface [flags...]\n", pn);
    703  1.35  christos 	(void)fprintf(stderr,
    704  1.35  christos 	    "       %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn);
    705   1.1    itojun 	exit(1);
    706   1.1    itojun }
    707   1.1    itojun 
    708  1.35  christos static int
    709  1.50     ozaki rtmsg(int cmd, struct rt_msghdr *_rtm)
    710   1.1    itojun {
    711   1.1    itojun 	static int seq;
    712  1.50     ozaki 	register struct rt_msghdr *rtm = _rtm;
    713   1.1    itojun 	register char *cp = m_rtmsg.m_space;
    714   1.1    itojun 	register int l;
    715   1.1    itojun 
    716   1.1    itojun 	errno = 0;
    717  1.50     ozaki 	if (rtm != NULL) {
    718  1.50     ozaki 		memcpy(&m_rtmsg, rtm, rtm->rtm_msglen);
    719  1.50     ozaki 		rtm = &m_rtmsg.m_rtm;
    720   1.1    itojun 		goto doit;
    721  1.47     ozaki 	}
    722  1.35  christos 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    723  1.50     ozaki 	rtm = &m_rtmsg.m_rtm;
    724   1.1    itojun 	rtm->rtm_flags = flags;
    725   1.1    itojun 	rtm->rtm_version = RTM_VERSION;
    726   1.1    itojun 
    727   1.1    itojun 	switch (cmd) {
    728   1.1    itojun 	default:
    729  1.35  christos 		errx(1, "internal wrong cmd");
    730  1.35  christos 		/*NOTREACHED*/
    731   1.1    itojun 	case RTM_ADD:
    732   1.1    itojun 		rtm->rtm_addrs |= RTA_GATEWAY;
    733  1.19    itojun 		if (expire_time) {
    734  1.19    itojun 			rtm->rtm_rmx.rmx_expire = expire_time;
    735  1.19    itojun 			rtm->rtm_inits = RTV_EXPIRE;
    736  1.19    itojun 		}
    737  1.47     ozaki 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
    738  1.35  christos #ifdef notdef	/* we don't support ipv6addr/128 type proxying. */
    739   1.6    itojun 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
    740   1.6    itojun 			rtm->rtm_flags &= ~RTF_HOST;
    741  1.30    itojun 			rtm->rtm_addrs |= RTA_NETMASK;
    742   1.6    itojun 		}
    743  1.34    rpaulo #endif
    744  1.47     ozaki 		rtm->rtm_addrs |= RTA_DST;
    745  1.47     ozaki 		break;
    746   1.1    itojun 	case RTM_GET:
    747  1.47     ozaki 		rtm->rtm_flags |= RTF_LLDATA;
    748  1.49     ozaki 		rtm->rtm_addrs |= RTA_DST | RTA_GATEWAY;
    749   1.1    itojun 	}
    750   1.1    itojun #define NEXTADDR(w, s) \
    751   1.1    itojun 	if (rtm->rtm_addrs & (w)) { \
    752  1.35  christos 		(void)memcpy(cp, &s, sizeof(s)); \
    753  1.39  drochner 		RT_ADVANCE(cp, (struct sockaddr *)(void *)&s); \
    754  1.35  christos 	}
    755   1.1    itojun 
    756   1.1    itojun 	NEXTADDR(RTA_DST, sin_m);
    757   1.1    itojun 	NEXTADDR(RTA_GATEWAY, sdl_m);
    758  1.35  christos #ifdef notdef	/* we don't support ipv6addr/128 type proxying. */
    759  1.35  christos 	(void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
    760   1.1    itojun 	NEXTADDR(RTA_NETMASK, so_mask);
    761  1.34    rpaulo #endif
    762   1.1    itojun 
    763  1.35  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    764   1.1    itojun doit:
    765   1.1    itojun 	l = rtm->rtm_msglen;
    766   1.1    itojun 	rtm->rtm_seq = ++seq;
    767   1.1    itojun 	rtm->rtm_type = cmd;
    768  1.45     ozaki 	if (prog_write(my_s, &m_rtmsg, (size_t)l) == -1) {
    769  1.35  christos 		if (errno != ESRCH || cmd != RTM_DELETE)
    770  1.16    itojun 			err(1, "writing to routing socket");
    771   1.1    itojun 	}
    772   1.1    itojun 	do {
    773  1.45     ozaki 		l = prog_read(my_s, &m_rtmsg, sizeof(m_rtmsg));
    774   1.1    itojun 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    775   1.1    itojun 	if (l < 0)
    776  1.35  christos 		warn("read from routing socket");
    777   1.1    itojun 	return (0);
    778   1.1    itojun }
    779   1.1    itojun 
    780  1.35  christos static void
    781  1.35  christos ifinfo(char *ifname, int argc, char **argv)
    782   1.1    itojun {
    783   1.1    itojun 	struct in6_ndireq nd;
    784   1.8    itojun 	int i, s;
    785   1.8    itojun 	u_int32_t newflags;
    786  1.56       roy 	bool valset = false, flagset = false;
    787   1.1    itojun 
    788  1.45     ozaki 	if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    789  1.16    itojun 		err(1, "socket");
    790  1.35  christos 	(void)memset(&nd, 0, sizeof(nd));
    791  1.35  christos 	(void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
    792  1.45     ozaki 	if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
    793  1.21    itojun 		err(1, "ioctl(SIOCGIFINFO_IN6)");
    794   1.1    itojun #define ND nd.ndi
    795   1.8    itojun 	newflags = ND.flags;
    796  1.22    itojun 	for (i = 0; i < argc; i++) {
    797   1.8    itojun 		int clear = 0;
    798   1.8    itojun 		char *cp = argv[i];
    799   1.8    itojun 
    800   1.8    itojun 		if (*cp == '-') {
    801   1.8    itojun 			clear = 1;
    802   1.8    itojun 			cp++;
    803   1.8    itojun 		}
    804   1.8    itojun 
    805   1.8    itojun #define SETFLAG(s, f) \
    806   1.8    itojun 	do {\
    807   1.8    itojun 		if (strcmp(cp, (s)) == 0) {\
    808   1.8    itojun 			if (clear)\
    809   1.8    itojun 				newflags &= ~(f);\
    810   1.8    itojun 			else\
    811   1.8    itojun 				newflags |= (f);\
    812  1.56       roy 			flagset = true; \
    813   1.8    itojun 		}\
    814  1.35  christos 	} while (/*CONSTCOND*/0)
    815  1.34    rpaulo /*
    816  1.34    rpaulo  * XXX: this macro is not 100% correct, in that it matches "nud" against
    817  1.34    rpaulo  *      "nudbogus".  But we just let it go since this is minor.
    818  1.34    rpaulo  */
    819  1.34    rpaulo #define SETVALUE(f, v) \
    820  1.34    rpaulo 	do { \
    821  1.34    rpaulo 		char *valptr; \
    822  1.34    rpaulo 		unsigned long newval; \
    823  1.34    rpaulo 		v = 0; /* unspecified */ \
    824  1.34    rpaulo 		if (strncmp(cp, f, strlen(f)) == 0) { \
    825  1.34    rpaulo 			valptr = strchr(cp, '='); \
    826  1.34    rpaulo 			if (valptr == NULL) \
    827  1.34    rpaulo 				err(1, "syntax error in %s field", (f)); \
    828  1.34    rpaulo 			errno = 0; \
    829  1.34    rpaulo 			newval = strtoul(++valptr, NULL, 0); \
    830  1.34    rpaulo 			if (errno) \
    831  1.34    rpaulo 				err(1, "syntax error in %s's value", (f)); \
    832  1.34    rpaulo 			v = newval; \
    833  1.56       roy 			valset = true; \
    834  1.34    rpaulo 		} \
    835  1.35  christos 	} while (/*CONSTCOND*/0)
    836  1.34    rpaulo 
    837  1.34    rpaulo #ifdef ND6_IFF_IFDISABLED
    838  1.34    rpaulo 		SETFLAG("disabled", ND6_IFF_IFDISABLED);
    839  1.34    rpaulo #endif
    840   1.8    itojun 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
    841  1.19    itojun #ifdef ND6_IFF_ACCEPT_RTADV
    842  1.19    itojun 		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
    843  1.19    itojun #endif
    844  1.38    dyoung #ifdef ND6_IFF_OVERRIDE_RTADV
    845  1.38    dyoung 		SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV);
    846  1.38    dyoung #endif
    847  1.43       roy #ifdef ND6_IFF_AUTO_LINKLOCAL
    848  1.43       roy 		SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
    849  1.43       roy #endif
    850  1.19    itojun #ifdef ND6_IFF_PREFER_SOURCE
    851  1.19    itojun 		SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
    852  1.19    itojun #endif
    853  1.34    rpaulo #ifdef ND6_IFF_DONT_SET_IFROUTE
    854  1.34    rpaulo 		SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE);
    855  1.34    rpaulo #endif
    856  1.34    rpaulo 		SETVALUE("basereachable", ND.basereachable);
    857  1.34    rpaulo 		SETVALUE("retrans", ND.retrans);
    858  1.34    rpaulo 		SETVALUE("curhlim", ND.chlim);
    859   1.8    itojun 
    860   1.8    itojun 		ND.flags = newflags;
    861  1.34    rpaulo #ifdef SIOCSIFINFO_IN6
    862  1.56       roy 		if (valset && prog_ioctl(s, SIOCSIFINFO_IN6, &nd) < 0)
    863  1.34    rpaulo 			err(1, "ioctl(SIOCSIFINFO_IN6)");
    864  1.56       roy #endif
    865  1.56       roy 		if (flagset && prog_ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0)
    866  1.16    itojun 			err(1, "ioctl(SIOCSIFINFO_FLAGS)");
    867   1.8    itojun #undef SETFLAG
    868  1.34    rpaulo #undef SETVALUE
    869   1.8    itojun 	}
    870   1.8    itojun 
    871  1.45     ozaki 	if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
    872  1.34    rpaulo 		err(1, "ioctl(SIOCGIFINFO_IN6)");
    873  1.56       roy 	(void)printf("curhlim=%d", ND.chlim);
    874  1.35  christos 	(void)printf(", basereachable=%ds%dms",
    875  1.21    itojun 	    ND.basereachable / 1000, ND.basereachable % 1000);
    876  1.35  christos 	(void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
    877   1.8    itojun 	if (ND.flags) {
    878  1.35  christos 		(void)printf("\nFlags: ");
    879  1.19    itojun 		if ((ND.flags & ND6_IFF_PERFORMNUD))
    880  1.35  christos 			(void)printf("nud ");
    881  1.34    rpaulo #ifdef ND6_IFF_IFDISABLED
    882  1.34    rpaulo 		if ((ND.flags & ND6_IFF_IFDISABLED))
    883  1.35  christos 			(void)printf("disabled ");
    884  1.34    rpaulo #endif
    885  1.43       roy #ifdef ND6_IFF_AUTO_LINKLOCAL
    886  1.43       roy 		if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
    887  1.43       roy 			(void)printf("auto_linklocal ");
    888  1.43       roy #endif
    889  1.19    itojun #ifdef ND6_IFF_PREFER_SOURCE
    890  1.19    itojun 		if ((ND.flags & ND6_IFF_PREFER_SOURCE))
    891  1.35  christos 			(void)printf("prefer_source ");
    892  1.19    itojun #endif
    893   1.8    itojun 	}
    894  1.35  christos 	(void)putc('\n', stdout);
    895   1.1    itojun #undef ND
    896  1.21    itojun 
    897  1.45     ozaki 	(void)prog_close(s);
    898   1.1    itojun }
    899   1.1    itojun 
    900  1.35  christos static const char *
    901  1.35  christos sec2str(time_t total)
    902   1.1    itojun {
    903   1.1    itojun 	static char result[256];
    904   1.1    itojun 	int days, hours, mins, secs;
    905   1.1    itojun 	int first = 1;
    906   1.1    itojun 	char *p = result;
    907  1.19    itojun 	char *ep = &result[sizeof(result)];
    908  1.19    itojun 	int n;
    909   1.1    itojun 
    910   1.1    itojun 	days = total / 3600 / 24;
    911   1.1    itojun 	hours = (total / 3600) % 24;
    912   1.1    itojun 	mins = (total / 60) % 60;
    913   1.1    itojun 	secs = total % 60;
    914   1.1    itojun 
    915   1.1    itojun 	if (days) {
    916   1.1    itojun 		first = 0;
    917  1.35  christos 		n = snprintf(p, (size_t)(ep - p), "%dd", days);
    918  1.19    itojun 		if (n < 0 || n >= ep - p)
    919  1.19    itojun 			return "?";
    920  1.19    itojun 		p += n;
    921   1.1    itojun 	}
    922   1.1    itojun 	if (!first || hours) {
    923   1.1    itojun 		first = 0;
    924  1.35  christos 		n = snprintf(p, (size_t)(ep - p), "%dh", hours);
    925  1.19    itojun 		if (n < 0 || n >= ep - p)
    926  1.19    itojun 			return "?";
    927  1.19    itojun 		p += n;
    928   1.1    itojun 	}
    929   1.1    itojun 	if (!first || mins) {
    930   1.1    itojun 		first = 0;
    931  1.35  christos 		n = snprintf(p, (size_t)(ep - p), "%dm", mins);
    932  1.19    itojun 		if (n < 0 || n >= ep - p)
    933  1.19    itojun 			return "?";
    934  1.19    itojun 		p += n;
    935   1.1    itojun 	}
    936  1.35  christos 	(void)snprintf(p, (size_t)(ep - p), "%ds", secs);
    937   1.1    itojun 
    938   1.1    itojun 	return(result);
    939   1.1    itojun }
    940   1.1    itojun 
    941   1.1    itojun /*
    942   1.1    itojun  * Print the timestamp
    943   1.1    itojun  * from tcpdump/util.c
    944   1.1    itojun  */
    945   1.1    itojun static void
    946  1.35  christos ts_print(const struct timeval *tvp)
    947   1.1    itojun {
    948   1.1    itojun 	int s;
    949   1.1    itojun 
    950   1.1    itojun 	/* Default */
    951   1.1    itojun 	s = (tvp->tv_sec + thiszone) % 86400;
    952   1.1    itojun 	(void)printf("%02d:%02d:%02d.%06u ",
    953   1.1    itojun 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
    954   1.1    itojun }
    955