Home | History | Annotate | Line # | Download | only in ndp
ndp.c revision 1.3
      1  1.3  itojun /*	$NetBSD: ndp.c,v 1.3 1999/09/03 03:54:47 itojun Exp $	*/
      2  1.2  itojun 
      3  1.1  itojun /*
      4  1.1  itojun  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
      5  1.1  itojun  * All rights reserved.
      6  1.1  itojun  *
      7  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1  itojun  * modification, are permitted provided that the following conditions
      9  1.1  itojun  * are met:
     10  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     15  1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.1  itojun  *    may be used to endorse or promote products derived from this software
     17  1.1  itojun  *    without specific prior written permission.
     18  1.1  itojun  *
     19  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  itojun  * SUCH DAMAGE.
     30  1.1  itojun  */
     31  1.1  itojun /*
     32  1.1  itojun  * Copyright (c) 1984, 1993
     33  1.1  itojun  *	The Regents of the University of California.  All rights reserved.
     34  1.1  itojun  *
     35  1.1  itojun  * This code is derived from software contributed to Berkeley by
     36  1.1  itojun  * Sun Microsystems, Inc.
     37  1.1  itojun  *
     38  1.1  itojun  * Redistribution and use in source and binary forms, with or without
     39  1.1  itojun  * modification, are permitted provided that the following conditions
     40  1.1  itojun  * are met:
     41  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     42  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     43  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     45  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     46  1.1  itojun  * 3. All advertising materials mentioning features or use of this software
     47  1.1  itojun  *    must display the following acknowledgement:
     48  1.1  itojun  *	This product includes software developed by the University of
     49  1.1  itojun  *	California, Berkeley and its contributors.
     50  1.1  itojun  * 4. Neither the name of the University nor the names of its contributors
     51  1.1  itojun  *    may be used to endorse or promote products derived from this software
     52  1.1  itojun  *    without specific prior written permission.
     53  1.1  itojun  *
     54  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  1.1  itojun  * SUCH DAMAGE.
     65  1.1  itojun  */
     66  1.1  itojun 
     67  1.1  itojun /*
     68  1.1  itojun  * Based on:
     69  1.1  itojun  * "@(#) Copyright (c) 1984, 1993\n\
     70  1.1  itojun  *	The Regents of the University of California.  All rights reserved.\n";
     71  1.1  itojun  *
     72  1.1  itojun  * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
     73  1.1  itojun  */
     74  1.1  itojun 
     75  1.1  itojun /*
     76  1.1  itojun  * ndp - display, set, delete and flush neighbor cache
     77  1.1  itojun  */
     78  1.1  itojun 
     79  1.1  itojun 
     80  1.1  itojun #include <sys/param.h>
     81  1.1  itojun #include <sys/file.h>
     82  1.1  itojun #include <sys/ioctl.h>
     83  1.1  itojun #include <sys/socket.h>
     84  1.1  itojun #include <sys/sysctl.h>
     85  1.1  itojun #include <sys/time.h>
     86  1.1  itojun 
     87  1.1  itojun #include <net/if.h>
     88  1.1  itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     89  1.1  itojun #include <net/if_var.h>
     90  1.1  itojun #endif /* __FreeBSD__ >= 3 */
     91  1.1  itojun #include <net/if_dl.h>
     92  1.1  itojun #include <net/if_types.h>
     93  1.1  itojun #include <net/route.h>
     94  1.1  itojun 
     95  1.1  itojun #include <netinet/in.h>
     96  1.1  itojun #ifndef __NetBSD__
     97  1.1  itojun #include <netinet/if_ether.h>
     98  1.1  itojun #endif
     99  1.1  itojun 
    100  1.1  itojun #include <netinet/icmp6.h>
    101  1.1  itojun #include <netinet6/in6_var.h>
    102  1.1  itojun #include <netinet6/nd6.h>
    103  1.1  itojun 
    104  1.1  itojun #include <arpa/inet.h>
    105  1.1  itojun 
    106  1.1  itojun #include <netdb.h>
    107  1.1  itojun #include <errno.h>
    108  1.1  itojun #include <nlist.h>
    109  1.1  itojun #include <stdio.h>
    110  1.1  itojun #include <string.h>
    111  1.1  itojun #include <paths.h>
    112  1.1  itojun #include <err.h>
    113  1.1  itojun #include <stdlib.h>
    114  1.1  itojun #include <fcntl.h>
    115  1.1  itojun #include <unistd.h>
    116  1.1  itojun #include "gmt2local.h"
    117  1.1  itojun 
    118  1.3  itojun /* packing rule for routing socket */
    119  1.3  itojun #define ROUNDUP(a) \
    120  1.3  itojun 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    121  1.3  itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    122  1.3  itojun 
    123  1.1  itojun extern int errno;
    124  1.1  itojun static int pid;
    125  1.1  itojun static int fflag;
    126  1.1  itojun static int nflag;
    127  1.1  itojun static int tflag;
    128  1.1  itojun static int32_t thiszone;	/* time difference with gmt */
    129  1.1  itojun static int s = -1;
    130  1.1  itojun static int repeat = 0;
    131  1.1  itojun 
    132  1.1  itojun char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
    133  1.1  itojun char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
    134  1.1  itojun 
    135  1.1  itojun int main __P((int, char **));
    136  1.1  itojun int file __P((char *));
    137  1.1  itojun void getsocket __P((void));
    138  1.1  itojun int set __P((int, char **));
    139  1.1  itojun void get __P((char *));
    140  1.1  itojun int delete __P((char *));
    141  1.1  itojun void dump __P((struct in6_addr *));
    142  1.1  itojun static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr, int ifindex));
    143  1.1  itojun static char *ether_str __P((struct sockaddr_dl *));
    144  1.1  itojun int ndp_ether_aton __P((char *, u_char *));
    145  1.1  itojun void usage __P((void));
    146  1.1  itojun int rtmsg __P((int));
    147  1.1  itojun void ifinfo __P((char *));
    148  1.1  itojun void rtrlist __P((void));
    149  1.1  itojun void plist __P((void));
    150  1.1  itojun void pfx_flush __P((void));
    151  1.1  itojun void rtr_flush __P((void));
    152  1.1  itojun void harmonize_rtr __P((void));
    153  1.1  itojun static char *sec2str __P((time_t t));
    154  1.1  itojun static char *ether_str __P((struct sockaddr_dl *sdl));
    155  1.1  itojun static void ts_print __P((const struct timeval *));
    156  1.1  itojun 
    157  1.1  itojun int
    158  1.1  itojun main(argc, argv)
    159  1.1  itojun 	int argc;
    160  1.1  itojun 	char **argv;
    161  1.1  itojun {
    162  1.1  itojun 	int ch;
    163  1.1  itojun 	int aflag = 0, cflag = 0, dflag = 0, sflag = 0, Hflag = 0,
    164  1.1  itojun 		pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
    165  1.1  itojun 	extern char *optarg;
    166  1.1  itojun 	extern int optind;
    167  1.1  itojun 
    168  1.1  itojun 	pid = getpid();
    169  1.1  itojun 	thiszone = gmt2local(0);
    170  1.1  itojun 	while ((ch = getopt(argc, argv, "acndfiprstA:HPR")) != EOF)
    171  1.1  itojun 		switch ((char)ch) {
    172  1.1  itojun 		case 'a':
    173  1.1  itojun 			aflag = 1;
    174  1.1  itojun 			break;
    175  1.1  itojun 		case 'c':
    176  1.1  itojun 			fflag = 1;
    177  1.1  itojun 			cflag = 1;
    178  1.1  itojun 			break;
    179  1.1  itojun 		case 'd':
    180  1.1  itojun 			dflag = 1;
    181  1.1  itojun 			break;
    182  1.1  itojun 		case 'i' :
    183  1.1  itojun 			if (argc != 3)
    184  1.1  itojun 				usage();
    185  1.1  itojun 			ifinfo(argv[2]);
    186  1.1  itojun 			exit(0);
    187  1.1  itojun 		case 'n':
    188  1.1  itojun 			nflag = 1;
    189  1.1  itojun 			continue;
    190  1.1  itojun 		case 'p':
    191  1.1  itojun 			pflag = 1;
    192  1.1  itojun 			break;
    193  1.1  itojun 		case 'f' :
    194  1.1  itojun 			if (argc != 3)
    195  1.1  itojun 				usage();
    196  1.1  itojun 			file(argv[2]);
    197  1.1  itojun 			exit(0);
    198  1.1  itojun 		case 'r' :
    199  1.1  itojun 			rflag = 1;
    200  1.1  itojun 			break;
    201  1.1  itojun 		case 's':
    202  1.1  itojun 			sflag = 1;
    203  1.1  itojun 			break;
    204  1.1  itojun 		case 't':
    205  1.1  itojun 			tflag = 1;
    206  1.1  itojun 			break;
    207  1.1  itojun 		case 'A':
    208  1.1  itojun 			aflag = 1;
    209  1.1  itojun 			repeat = atoi(optarg);
    210  1.1  itojun 			if (repeat < 0)
    211  1.1  itojun 				usage();
    212  1.1  itojun 			break;
    213  1.1  itojun 		case 'H' :
    214  1.1  itojun 			Hflag = 1;
    215  1.1  itojun 			break;
    216  1.1  itojun 		case 'P':
    217  1.1  itojun 			Pflag = 1;
    218  1.1  itojun 			break;
    219  1.1  itojun 		case 'R':
    220  1.1  itojun 			Rflag = 1;
    221  1.1  itojun 			break;
    222  1.1  itojun 		default:
    223  1.1  itojun 			usage();
    224  1.1  itojun 		}
    225  1.1  itojun 
    226  1.1  itojun 	argc -= optind;
    227  1.1  itojun 	argv += optind;
    228  1.1  itojun 
    229  1.1  itojun 	if (aflag || cflag) {
    230  1.1  itojun 		dump(0);
    231  1.1  itojun 		exit(0);
    232  1.1  itojun 	}
    233  1.1  itojun 	if (dflag) {
    234  1.1  itojun 		if (argc != 1)
    235  1.1  itojun 			usage();
    236  1.1  itojun 		delete(argv[0]);
    237  1.1  itojun 	}
    238  1.1  itojun 	if (pflag) {
    239  1.1  itojun 		plist();
    240  1.1  itojun 		exit(0);
    241  1.1  itojun 	}
    242  1.1  itojun 	if (rflag) {
    243  1.1  itojun 		rtrlist();
    244  1.1  itojun 		exit(0);
    245  1.1  itojun 	}
    246  1.1  itojun 	if (sflag) {
    247  1.1  itojun 		if (argc < 2 || argc > 4)
    248  1.1  itojun 			usage();
    249  1.1  itojun 		exit(set(argc, argv) ? 1 : 0);
    250  1.1  itojun 	}
    251  1.1  itojun 	if (Hflag) {
    252  1.1  itojun 		harmonize_rtr();
    253  1.1  itojun 		exit(0);
    254  1.1  itojun 	}
    255  1.1  itojun 	if (Pflag) {
    256  1.1  itojun 		pfx_flush();
    257  1.1  itojun 		exit(0);
    258  1.1  itojun 	}
    259  1.1  itojun 	if (Rflag) {
    260  1.1  itojun 		rtr_flush();
    261  1.1  itojun 		exit(0);
    262  1.1  itojun 	}
    263  1.1  itojun 
    264  1.1  itojun 	if (argc != 1)
    265  1.1  itojun 		usage();
    266  1.1  itojun 	get(argv[0]);
    267  1.1  itojun 	exit(0);
    268  1.1  itojun }
    269  1.1  itojun 
    270  1.1  itojun /*
    271  1.1  itojun  * Process a file to set standard ndp entries
    272  1.1  itojun  */
    273  1.1  itojun int
    274  1.1  itojun file(name)
    275  1.1  itojun 	char *name;
    276  1.1  itojun {
    277  1.1  itojun 	FILE *fp;
    278  1.1  itojun 	int i, retval;
    279  1.1  itojun 	char line[100], arg[5][50], *args[5];
    280  1.1  itojun 
    281  1.1  itojun 	if ((fp = fopen(name, "r")) == NULL) {
    282  1.1  itojun 		fprintf(stderr, "ndp: cannot open %s\n", name);
    283  1.1  itojun 		exit(1);
    284  1.1  itojun 	}
    285  1.1  itojun 	args[0] = &arg[0][0];
    286  1.1  itojun 	args[1] = &arg[1][0];
    287  1.1  itojun 	args[2] = &arg[2][0];
    288  1.1  itojun 	args[3] = &arg[3][0];
    289  1.1  itojun 	args[4] = &arg[4][0];
    290  1.1  itojun 	retval = 0;
    291  1.1  itojun 	while(fgets(line, 100, fp) != NULL) {
    292  1.1  itojun 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    293  1.1  itojun 		    arg[3], arg[4]);
    294  1.1  itojun 		if (i < 2) {
    295  1.1  itojun 			fprintf(stderr, "ndp: bad line: %s\n", line);
    296  1.1  itojun 			retval = 1;
    297  1.1  itojun 			continue;
    298  1.1  itojun 		}
    299  1.1  itojun 		if (set(i, args))
    300  1.1  itojun 			retval = 1;
    301  1.1  itojun 	}
    302  1.1  itojun 	fclose(fp);
    303  1.1  itojun 	return (retval);
    304  1.1  itojun }
    305  1.1  itojun 
    306  1.1  itojun void
    307  1.1  itojun getsocket()
    308  1.1  itojun {
    309  1.1  itojun 	if (s < 0) {
    310  1.1  itojun 		s = socket(PF_ROUTE, SOCK_RAW, 0);
    311  1.1  itojun 		if (s < 0) {
    312  1.1  itojun 			perror("ndp: socket");
    313  1.1  itojun 			exit(1);
    314  1.1  itojun 		}
    315  1.1  itojun 	}
    316  1.1  itojun }
    317  1.1  itojun 
    318  1.1  itojun struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    319  1.1  itojun struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
    320  1.1  itojun struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    321  1.1  itojun int	expire_time, flags, found_entry;
    322  1.1  itojun struct	{
    323  1.1  itojun 	struct	rt_msghdr m_rtm;
    324  1.1  itojun 	char	m_space[512];
    325  1.1  itojun }	m_rtmsg;
    326  1.1  itojun 
    327  1.1  itojun /*
    328  1.1  itojun  * Set an individual neighbor cache entry
    329  1.1  itojun  */
    330  1.1  itojun int
    331  1.1  itojun set(argc, argv)
    332  1.1  itojun 	int argc;
    333  1.1  itojun 	char **argv;
    334  1.1  itojun {
    335  1.1  itojun 	register struct sockaddr_in6 *sin = &sin_m;
    336  1.1  itojun 	register struct sockaddr_dl *sdl;
    337  1.1  itojun 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
    338  1.1  itojun 	struct addrinfo hints, *res;
    339  1.1  itojun 	int gai_error;
    340  1.1  itojun 	u_char *ea;
    341  1.1  itojun 	char *host = argv[0], *eaddr = argv[1];
    342  1.1  itojun 
    343  1.1  itojun 	getsocket();
    344  1.1  itojun 	argc -= 2;
    345  1.1  itojun 	argv += 2;
    346  1.1  itojun 	sdl_m = blank_sdl;
    347  1.1  itojun 	sin_m = blank_sin;
    348  1.1  itojun 
    349  1.1  itojun 	bzero(&hints, sizeof(hints));
    350  1.1  itojun 	hints.ai_family = AF_INET6;
    351  1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    352  1.1  itojun 	if (gai_error) {
    353  1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    354  1.1  itojun 			gai_strerror(gai_error));
    355  1.1  itojun 		return 1;
    356  1.1  itojun 	}
    357  1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    358  1.1  itojun 	ea = (u_char *)LLADDR(&sdl_m);
    359  1.1  itojun 	if (ndp_ether_aton(eaddr, ea) == 0)
    360  1.1  itojun 		sdl_m.sdl_alen = 6;
    361  1.1  itojun 	flags = expire_time = 0;
    362  1.1  itojun 	while (argc-- > 0) {
    363  1.1  itojun 		if (strncmp(argv[0], "temp", 4) == 0) {
    364  1.1  itojun 			struct timeval time;
    365  1.1  itojun 			gettimeofday(&time, 0);
    366  1.1  itojun 			expire_time = time.tv_sec + 20 * 60;
    367  1.1  itojun 		}
    368  1.1  itojun 		argv++;
    369  1.1  itojun 	}
    370  1.1  itojun tryagain:
    371  1.1  itojun 	if (rtmsg(RTM_GET) < 0) {
    372  1.1  itojun 		perror(host);
    373  1.1  itojun 		return (1);
    374  1.1  itojun 	}
    375  1.1  itojun 	sin = (struct sockaddr_in6 *)(rtm + 1);
    376  1.3  itojun 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
    377  1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
    378  1.1  itojun 		if (sdl->sdl_family == AF_LINK &&
    379  1.1  itojun 		    (rtm->rtm_flags & RTF_LLINFO) &&
    380  1.1  itojun 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    381  1.1  itojun 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    382  1.1  itojun 		case IFT_ISO88024: case IFT_ISO88025:
    383  1.1  itojun 			goto overwrite;
    384  1.1  itojun 		}
    385  1.1  itojun 		goto tryagain;
    386  1.1  itojun 	}
    387  1.1  itojun overwrite:
    388  1.1  itojun 	if (sdl->sdl_family != AF_LINK) {
    389  1.1  itojun 		printf("cannot intuit interface index and type for %s\n", host);
    390  1.1  itojun 		return (1);
    391  1.1  itojun 	}
    392  1.1  itojun 	sdl_m.sdl_type = sdl->sdl_type;
    393  1.1  itojun 	sdl_m.sdl_index = sdl->sdl_index;
    394  1.1  itojun 	return (rtmsg(RTM_ADD));
    395  1.1  itojun }
    396  1.1  itojun 
    397  1.1  itojun /*
    398  1.1  itojun  * Display an individual neighbor cache entry
    399  1.1  itojun  */
    400  1.1  itojun void
    401  1.1  itojun get(host)
    402  1.1  itojun 	char *host;
    403  1.1  itojun {
    404  1.1  itojun 	struct sockaddr_in6 *sin = &sin_m;
    405  1.1  itojun 	struct addrinfo hints, *res;
    406  1.1  itojun 	int gai_error;
    407  1.1  itojun 
    408  1.1  itojun 	sin_m = blank_sin;
    409  1.1  itojun 	bzero(&hints, sizeof(hints));
    410  1.1  itojun 	hints.ai_family = AF_INET6;
    411  1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    412  1.1  itojun 	if (gai_error) {
    413  1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    414  1.1  itojun 			gai_strerror(gai_error));
    415  1.1  itojun 		return;
    416  1.1  itojun 	}
    417  1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    418  1.1  itojun 	dump(&sin->sin6_addr);
    419  1.1  itojun 	if (found_entry == 0) {
    420  1.1  itojun 		printf("%s (%s) -- no entry\n",
    421  1.1  itojun 		    host, inet_ntop(AF_INET6, &sin->sin6_addr, ntop_buf,
    422  1.1  itojun 				sizeof(ntop_buf)));
    423  1.1  itojun 		exit(1);
    424  1.1  itojun 	}
    425  1.1  itojun }
    426  1.1  itojun 
    427  1.1  itojun /*
    428  1.1  itojun  * Delete a neighbor cache entry
    429  1.1  itojun  */
    430  1.1  itojun int
    431  1.1  itojun delete(host)
    432  1.1  itojun 	char *host;
    433  1.1  itojun {
    434  1.1  itojun 	register struct sockaddr_in6 *sin = &sin_m;
    435  1.1  itojun 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
    436  1.1  itojun 	struct sockaddr_dl *sdl;
    437  1.1  itojun 	struct addrinfo hints, *res;
    438  1.1  itojun 	int gai_error;
    439  1.1  itojun 
    440  1.1  itojun 	getsocket();
    441  1.1  itojun 	sin_m = blank_sin;
    442  1.1  itojun 
    443  1.1  itojun 	bzero(&hints, sizeof(hints));
    444  1.1  itojun 	hints.ai_family = AF_INET6;
    445  1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    446  1.1  itojun 	if (gai_error) {
    447  1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    448  1.1  itojun 			gai_strerror(gai_error));
    449  1.1  itojun 		return 1;
    450  1.1  itojun 	}
    451  1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    452  1.1  itojun /*tryagain:*/
    453  1.1  itojun 	if (rtmsg(RTM_GET) < 0) {
    454  1.1  itojun 		perror(host);
    455  1.1  itojun 		return (1);
    456  1.1  itojun 	}
    457  1.1  itojun 	sin = (struct sockaddr_in6 *)(rtm + 1);
    458  1.3  itojun 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
    459  1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
    460  1.1  itojun 		if (sdl->sdl_family == AF_LINK &&
    461  1.1  itojun 		    (rtm->rtm_flags & RTF_LLINFO) &&
    462  1.1  itojun 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    463  1.1  itojun 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    464  1.1  itojun 		case IFT_ISO88024: case IFT_ISO88025:
    465  1.1  itojun 			goto delete;
    466  1.1  itojun 		}
    467  1.1  itojun 	}
    468  1.1  itojun delete:
    469  1.1  itojun 	if (sdl->sdl_family != AF_LINK) {
    470  1.1  itojun 		printf("cannot locate %s\n", host);
    471  1.1  itojun 		return (1);
    472  1.1  itojun 	}
    473  1.1  itojun 	if (rtmsg(RTM_DELETE) == 0)
    474  1.1  itojun 		printf("%s (%s) deleted\n", host,
    475  1.1  itojun 			inet_ntop(AF_INET6, &sin->sin6_addr, ntop_buf,
    476  1.1  itojun 					sizeof(ntop_buf)));
    477  1.1  itojun 	return 0;
    478  1.1  itojun }
    479  1.1  itojun 
    480  1.1  itojun /*
    481  1.1  itojun  * Dump the entire neighbor cache
    482  1.1  itojun  */
    483  1.1  itojun void
    484  1.1  itojun dump(addr)
    485  1.1  itojun 	struct in6_addr *addr;
    486  1.1  itojun {
    487  1.1  itojun 	int mib[6];
    488  1.1  itojun 	size_t needed;
    489  1.1  itojun 	char *host, *lim, *buf, *next;
    490  1.1  itojun 	struct rt_msghdr *rtm;
    491  1.1  itojun 	struct sockaddr_in6 *sin;
    492  1.1  itojun 	struct sockaddr_dl *sdl;
    493  1.1  itojun 	extern int h_errno;
    494  1.1  itojun 	struct hostent *hp;
    495  1.1  itojun 	struct in6_nbrinfo *nbi;
    496  1.1  itojun 	struct timeval time;
    497  1.1  itojun 
    498  1.1  itojun 	/* Print header */
    499  1.1  itojun 	if (!tflag)
    500  1.1  itojun 		printf("%-29.29s %-18.18s %6.6s %-9.9s %2s %4s %4s\n",
    501  1.1  itojun 		       "Neighbor", "Linklayer Address", "Netif", "Expire",
    502  1.1  itojun 		       "St", "Flgs", "Prbs");
    503  1.1  itojun 
    504  1.1  itojun again:;
    505  1.1  itojun 	mib[0] = CTL_NET;
    506  1.1  itojun 	mib[1] = PF_ROUTE;
    507  1.1  itojun 	mib[2] = 0;
    508  1.1  itojun 	mib[3] = AF_INET6;
    509  1.1  itojun 	mib[4] = NET_RT_FLAGS;
    510  1.1  itojun 	mib[5] = RTF_LLINFO;
    511  1.1  itojun 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    512  1.3  itojun 		err(1, "sysctl(PF_ROUTE estimate)");
    513  1.3  itojun 	if (needed > 0) {
    514  1.3  itojun 		if ((buf = malloc(needed)) == NULL)
    515  1.3  itojun 			errx(1, "malloc");
    516  1.3  itojun 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    517  1.3  itojun 			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
    518  1.3  itojun 		lim = buf + needed;
    519  1.3  itojun 	} else
    520  1.3  itojun 		buf = lim = NULL;
    521  1.1  itojun 
    522  1.3  itojun 	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
    523  1.1  itojun 		int isrouter = 0, prbs = 0;
    524  1.1  itojun 
    525  1.1  itojun 		rtm = (struct rt_msghdr *)next;
    526  1.1  itojun 		sin = (struct sockaddr_in6 *)(rtm + 1);
    527  1.3  itojun 		sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
    528  1.1  itojun 		if (addr) {
    529  1.1  itojun 			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
    530  1.1  itojun 				continue;
    531  1.1  itojun 			found_entry = 1;
    532  1.1  itojun 		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
    533  1.1  itojun 			continue;
    534  1.1  itojun 		if (fflag == 1) {
    535  1.1  itojun 			delete((char *)inet_ntop(AF_INET6, &sin->sin6_addr,
    536  1.1  itojun 						ntop_buf, sizeof(ntop_buf)));
    537  1.1  itojun 			continue;
    538  1.1  itojun 		}
    539  1.1  itojun 		host = NULL;
    540  1.1  itojun 		if (nflag == 0) {
    541  1.1  itojun 			hp = gethostbyaddr((char *)&sin->sin6_addr,
    542  1.1  itojun 					   sizeof(struct in6_addr), AF_INET6);
    543  1.1  itojun 			if (hp)
    544  1.1  itojun 				host = hp->h_name;
    545  1.1  itojun 		}
    546  1.1  itojun 		if (host == NULL) {
    547  1.1  itojun 			inet_ntop(AF_INET6, &sin->sin6_addr,
    548  1.1  itojun 				  ntop_buf, sizeof(ntop_buf));
    549  1.1  itojun 			host = ntop_buf;
    550  1.1  itojun 		}
    551  1.1  itojun 
    552  1.1  itojun 		gettimeofday(&time, 0);
    553  1.1  itojun 		if (tflag)
    554  1.1  itojun 			ts_print(&time);
    555  1.1  itojun 
    556  1.1  itojun 		printf("%-29.29s %-18.18s %6.6s", host,
    557  1.1  itojun 		       ether_str(sdl),
    558  1.1  itojun 		       if_indextoname(sdl->sdl_index, ifix_buf));
    559  1.1  itojun 
    560  1.1  itojun 		/* Print neighbor discovery specific informations */
    561  1.1  itojun 		putchar(' ');
    562  1.1  itojun 		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index);
    563  1.1  itojun 		if (nbi) {
    564  1.1  itojun 			if (nbi->expire > time.tv_sec) {
    565  1.1  itojun 				printf(" %-9.9s",
    566  1.1  itojun 				       sec2str(nbi->expire - time.tv_sec));
    567  1.1  itojun 			}
    568  1.1  itojun 			else if (nbi->expire == 0)
    569  1.1  itojun 				printf(" %-9.9s", "permanent");
    570  1.1  itojun 			else
    571  1.1  itojun 				printf(" %-9.9s", "expired");
    572  1.1  itojun 
    573  1.1  itojun 			switch(nbi->state) {
    574  1.1  itojun 			 case ND6_LLINFO_NOSTATE:
    575  1.1  itojun 				 printf(" N");
    576  1.1  itojun 				 break;
    577  1.1  itojun 			 case ND6_LLINFO_WAITDELETE:
    578  1.1  itojun 				 printf(" W");
    579  1.1  itojun 				 break;
    580  1.1  itojun 			 case ND6_LLINFO_INCOMPLETE:
    581  1.1  itojun 				 printf(" I");
    582  1.1  itojun 				 break;
    583  1.1  itojun 			 case ND6_LLINFO_REACHABLE:
    584  1.1  itojun 				 printf(" R");
    585  1.1  itojun 				 break;
    586  1.1  itojun 			 case ND6_LLINFO_STALE:
    587  1.1  itojun 				 printf(" S");
    588  1.1  itojun 				 break;
    589  1.1  itojun 			 case ND6_LLINFO_DELAY:
    590  1.1  itojun 				 printf(" D");
    591  1.1  itojun 				 break;
    592  1.1  itojun 			 case ND6_LLINFO_PROBE:
    593  1.1  itojun 				 printf(" P");
    594  1.1  itojun 				 break;
    595  1.1  itojun 			 default:
    596  1.1  itojun 				 printf(" ?");
    597  1.1  itojun 				 break;
    598  1.1  itojun 			}
    599  1.1  itojun 
    600  1.1  itojun 			isrouter = nbi->isrouter;
    601  1.1  itojun 			prbs = nbi->asked;
    602  1.1  itojun 		}
    603  1.1  itojun 		else {
    604  1.1  itojun 			warnx("failed to get neighbor information");
    605  1.1  itojun 			printf("  ");
    606  1.1  itojun 		}
    607  1.1  itojun 
    608  1.1  itojun 		/* other flags */
    609  1.1  itojun 		putchar(' ');
    610  1.1  itojun 		{
    611  1.1  itojun 			u_char flgbuf[8], *p = flgbuf;
    612  1.1  itojun 
    613  1.1  itojun 			flgbuf[0] = '\0';
    614  1.1  itojun 			if (isrouter)
    615  1.1  itojun 				p += sprintf((char *)p, "R");
    616  1.1  itojun #ifndef RADISH
    617  1.1  itojun 			if (rtm->rtm_addrs & RTA_NETMASK) {
    618  1.1  itojun 				sin = (struct sockaddr_in6 *)
    619  1.1  itojun 					(sdl->sdl_len + (char *)sdl);
    620  1.3  itojun 				if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
    621  1.1  itojun 					p += sprintf((char *)p, "P");
    622  1.1  itojun 				if (sin->sin6_len != sizeof(struct sockaddr_in6))
    623  1.1  itojun 					p += sprintf((char *)p, "W");
    624  1.1  itojun 			}
    625  1.1  itojun #endif /*RADISH*/
    626  1.1  itojun 			printf("%4s", flgbuf);
    627  1.1  itojun 		}
    628  1.1  itojun 
    629  1.1  itojun 		putchar(' ');
    630  1.1  itojun 		if (prbs)
    631  1.1  itojun 			printf("% 4d", prbs);
    632  1.1  itojun 
    633  1.1  itojun 		printf("\n");
    634  1.1  itojun 	}
    635  1.1  itojun 
    636  1.1  itojun 	if (repeat) {
    637  1.1  itojun 		printf("\n");
    638  1.1  itojun 		sleep(repeat);
    639  1.1  itojun 		goto again;
    640  1.1  itojun 	}
    641  1.1  itojun }
    642  1.1  itojun 
    643  1.1  itojun static struct in6_nbrinfo *
    644  1.1  itojun getnbrinfo(addr, ifindex)
    645  1.1  itojun 	struct in6_addr *addr;
    646  1.1  itojun 	int ifindex;
    647  1.1  itojun {
    648  1.1  itojun 	static struct in6_nbrinfo nbi;
    649  1.1  itojun 	int s;
    650  1.1  itojun 
    651  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    652  1.1  itojun 		err(1, "socket");
    653  1.1  itojun 
    654  1.1  itojun 	bzero(&nbi, sizeof(nbi));
    655  1.1  itojun 	if_indextoname(ifindex, nbi.ifname);
    656  1.1  itojun 	nbi.addr = *addr;
    657  1.1  itojun 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
    658  1.1  itojun 		warn("ioctl");
    659  1.1  itojun 		close(s);
    660  1.1  itojun 		return(NULL);
    661  1.1  itojun 	}
    662  1.1  itojun 
    663  1.1  itojun 	close(s);
    664  1.1  itojun 	return(&nbi);
    665  1.1  itojun }
    666  1.1  itojun 
    667  1.1  itojun static char *
    668  1.1  itojun ether_str(sdl)
    669  1.1  itojun 	struct sockaddr_dl *sdl;
    670  1.1  itojun {
    671  1.1  itojun 	static char ebuf[32];
    672  1.1  itojun 	u_char *cp;
    673  1.1  itojun 
    674  1.1  itojun 	if (sdl->sdl_alen) {
    675  1.1  itojun 		cp = (u_char *)LLADDR(sdl);
    676  1.1  itojun 		sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
    677  1.1  itojun 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    678  1.1  itojun 	}
    679  1.1  itojun 	else {
    680  1.1  itojun 		sprintf(ebuf, "(incomplete)");
    681  1.1  itojun 	}
    682  1.1  itojun 
    683  1.1  itojun 	return(ebuf);
    684  1.1  itojun }
    685  1.1  itojun 
    686  1.1  itojun int
    687  1.1  itojun ndp_ether_aton(a, n)
    688  1.1  itojun 	char *a;
    689  1.1  itojun 	u_char *n;
    690  1.1  itojun {
    691  1.1  itojun 	int i, o[6];
    692  1.1  itojun 
    693  1.1  itojun 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
    694  1.1  itojun 					   &o[3], &o[4], &o[5]);
    695  1.1  itojun 	if (i != 6) {
    696  1.1  itojun 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
    697  1.1  itojun 		return (1);
    698  1.1  itojun 	}
    699  1.1  itojun 	for (i=0; i<6; i++)
    700  1.1  itojun 		n[i] = o[i];
    701  1.1  itojun 	return (0);
    702  1.1  itojun }
    703  1.1  itojun 
    704  1.1  itojun void
    705  1.1  itojun usage()
    706  1.1  itojun {
    707  1.1  itojun 	printf("usage: ndp hostname\n");
    708  1.1  itojun 	printf("       ndp -a[nt]\n");
    709  1.1  itojun 	printf("       ndp [-nt] -A wait\n");
    710  1.1  itojun 	printf("       ndp -c[nt]\n");
    711  1.1  itojun 	printf("       ndp -d[nt] hostname\n");
    712  1.1  itojun 	printf("       ndp -f[nt] filename\n");
    713  1.1  itojun 	printf("       ndp -i interface\n");
    714  1.1  itojun 	printf("       ndp -p\n");
    715  1.1  itojun 	printf("       ndp -r\n");
    716  1.1  itojun 	printf("       ndp -s hostname ether_addr [temp]\n");
    717  1.1  itojun 	printf("       ndp -H\n");
    718  1.1  itojun 	printf("       ndp -P\n");
    719  1.1  itojun 	printf("       ndp -R\n");
    720  1.1  itojun 	exit(1);
    721  1.1  itojun }
    722  1.1  itojun 
    723  1.1  itojun int
    724  1.1  itojun rtmsg(cmd)
    725  1.1  itojun 	int cmd;
    726  1.1  itojun {
    727  1.1  itojun 	static int seq;
    728  1.1  itojun 	int rlen;
    729  1.1  itojun 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
    730  1.1  itojun 	register char *cp = m_rtmsg.m_space;
    731  1.1  itojun 	register int l;
    732  1.1  itojun 
    733  1.1  itojun 	errno = 0;
    734  1.1  itojun 	if (cmd == RTM_DELETE)
    735  1.1  itojun 		goto doit;
    736  1.1  itojun 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
    737  1.1  itojun 	rtm->rtm_flags = flags;
    738  1.1  itojun 	rtm->rtm_version = RTM_VERSION;
    739  1.1  itojun 
    740  1.1  itojun 	switch (cmd) {
    741  1.1  itojun 	default:
    742  1.1  itojun 		fprintf(stderr, "ndp: internal wrong cmd\n");
    743  1.1  itojun 		exit(1);
    744  1.1  itojun 	case RTM_ADD:
    745  1.1  itojun 		rtm->rtm_addrs |= RTA_GATEWAY;
    746  1.1  itojun 		rtm->rtm_rmx.rmx_expire = expire_time;
    747  1.1  itojun 		rtm->rtm_inits = RTV_EXPIRE;
    748  1.1  itojun 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    749  1.1  itojun 		/* FALLTHROUGH */
    750  1.1  itojun 	case RTM_GET:
    751  1.1  itojun 		rtm->rtm_addrs |= RTA_DST;
    752  1.1  itojun 	}
    753  1.1  itojun #define NEXTADDR(w, s) \
    754  1.1  itojun 	if (rtm->rtm_addrs & (w)) { \
    755  1.1  itojun 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
    756  1.1  itojun 
    757  1.1  itojun 	NEXTADDR(RTA_DST, sin_m);
    758  1.1  itojun 	NEXTADDR(RTA_GATEWAY, sdl_m);
    759  1.1  itojun 	NEXTADDR(RTA_NETMASK, so_mask);
    760  1.1  itojun 
    761  1.1  itojun 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    762  1.1  itojun doit:
    763  1.1  itojun 	l = rtm->rtm_msglen;
    764  1.1  itojun 	rtm->rtm_seq = ++seq;
    765  1.1  itojun 	rtm->rtm_type = cmd;
    766  1.1  itojun 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    767  1.1  itojun 		if (errno != ESRCH || cmd != RTM_DELETE) {
    768  1.1  itojun 			perror("writing to routing socket");
    769  1.1  itojun 			return (-1);
    770  1.1  itojun 		}
    771  1.1  itojun 	}
    772  1.1  itojun 	do {
    773  1.1  itojun 		l = read(s, (char *)&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.1  itojun 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
    777  1.1  itojun 		    strerror(errno));
    778  1.1  itojun 	return (0);
    779  1.1  itojun }
    780  1.1  itojun 
    781  1.1  itojun void
    782  1.1  itojun ifinfo(ifname)
    783  1.1  itojun 	char *ifname;
    784  1.1  itojun {
    785  1.1  itojun 	struct in6_ndireq nd;
    786  1.1  itojun 	int s;
    787  1.1  itojun 
    788  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    789  1.1  itojun 		perror("ndp: socket");
    790  1.1  itojun 		exit(1);
    791  1.1  itojun 	}
    792  1.1  itojun 	bzero(&nd, sizeof(nd));
    793  1.1  itojun 	strcpy(nd.ifname, ifname);
    794  1.1  itojun 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
    795  1.1  itojun  		perror("ioctl (SIOCGIFINFO_IN6)");
    796  1.1  itojun  		exit(1);
    797  1.1  itojun  	}
    798  1.1  itojun #define ND nd.ndi
    799  1.1  itojun 	printf("linkmtu=%d", ND.linkmtu);
    800  1.1  itojun 	printf(", curhlim=%d", ND.chlim);
    801  1.1  itojun 	printf(", basereachable=%ds%dms",
    802  1.1  itojun 	       ND.basereachable / 1000, ND.basereachable % 1000);
    803  1.1  itojun 	printf(", reachable=%ds", ND.reachable);
    804  1.1  itojun 	printf(", retrans=%ds%dms\n", ND.retrans / 1000, ND.retrans % 1000);
    805  1.1  itojun #undef ND
    806  1.1  itojun 	close(s);
    807  1.1  itojun }
    808  1.1  itojun 
    809  1.1  itojun void
    810  1.1  itojun rtrlist()
    811  1.1  itojun {
    812  1.1  itojun 	struct in6_drlist dr;
    813  1.1  itojun 	int s, i;
    814  1.1  itojun 	struct timeval time;
    815  1.1  itojun 
    816  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    817  1.1  itojun 		perror("ndp: socket");
    818  1.1  itojun 		exit(1);
    819  1.1  itojun 	}
    820  1.1  itojun 	bzero(&dr, sizeof(dr));
    821  1.1  itojun 	strcpy(dr.ifname, "lo0"); /* dummy */
    822  1.1  itojun 	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
    823  1.1  itojun  		perror("ioctl (SIOCGDRLST_IN6)");
    824  1.1  itojun  		exit(1);
    825  1.1  itojun  	}
    826  1.1  itojun #define DR dr.defrouter[i]
    827  1.1  itojun 	for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
    828  1.1  itojun 		printf("%s if=%s", inet_ntop(AF_INET6, &DR.rtaddr,
    829  1.1  itojun 					     ntop_buf, sizeof(ntop_buf)),
    830  1.1  itojun 		       if_indextoname(DR.if_index, ifix_buf));
    831  1.1  itojun 		printf(", flags=%s%s",
    832  1.1  itojun 		       DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
    833  1.1  itojun 		       DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
    834  1.1  itojun 		gettimeofday(&time, 0);
    835  1.1  itojun 		if (DR.expire == 0)
    836  1.1  itojun 			printf(", expire=Never\n");
    837  1.1  itojun 		else
    838  1.1  itojun 			printf(", expire=%s\n",
    839  1.1  itojun 				sec2str(DR.expire - time.tv_sec));
    840  1.1  itojun 	}
    841  1.1  itojun #undef DR
    842  1.1  itojun 	close(s);
    843  1.1  itojun }
    844  1.1  itojun 
    845  1.1  itojun void
    846  1.1  itojun plist()
    847  1.1  itojun {
    848  1.1  itojun 	struct in6_prlist pr;
    849  1.1  itojun 	int s, i;
    850  1.1  itojun 	struct timeval time;
    851  1.1  itojun 
    852  1.1  itojun 	gettimeofday(&time, 0);
    853  1.1  itojun 
    854  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    855  1.1  itojun 		perror("ndp: socket");
    856  1.1  itojun 		exit(1);
    857  1.1  itojun 	}
    858  1.1  itojun 	bzero(&pr, sizeof(pr));
    859  1.1  itojun 	strcpy(pr.ifname, "lo0"); /* dummy */
    860  1.1  itojun 	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
    861  1.1  itojun  		perror("ioctl (SIOCGPRLST_IN6)");
    862  1.1  itojun  		exit(1);
    863  1.1  itojun  	}
    864  1.1  itojun #define PR pr.prefix[i]
    865  1.1  itojun 	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
    866  1.1  itojun 		printf("%s/%d if=%s\n",
    867  1.1  itojun 		       inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
    868  1.1  itojun 				 sizeof(ntop_buf)), PR.prefixlen,
    869  1.1  itojun 		       if_indextoname(PR.if_index, ifix_buf));
    870  1.1  itojun 		gettimeofday(&time, 0);
    871  1.1  itojun 		printf("  flags=%s%s",
    872  1.1  itojun 		       PR.raflags.onlink ? "L" : "",
    873  1.1  itojun 		       PR.raflags.autonomous ? "A" : "");
    874  1.1  itojun 		if (PR.vltime == ND6_INFINITE_LIFETIME)
    875  1.1  itojun 			printf(" vltime=infinity");
    876  1.1  itojun 		else
    877  1.1  itojun 			printf(" vltime=%ld", (long)PR.vltime);
    878  1.1  itojun 		if (PR.pltime == ND6_INFINITE_LIFETIME)
    879  1.1  itojun 			printf(", pltime=infinity");
    880  1.1  itojun 		else
    881  1.1  itojun 			printf(", pltime=%ld", (long)PR.pltime);
    882  1.1  itojun 		if (PR.expire == 0)
    883  1.1  itojun 			printf(", expire=Never\n");
    884  1.1  itojun 		else if (PR.expire >= time.tv_sec)
    885  1.1  itojun 			printf(", expire=%s\n",
    886  1.1  itojun 				sec2str(PR.expire - time.tv_sec));
    887  1.1  itojun 		else
    888  1.1  itojun 			printf(", expired\n");
    889  1.1  itojun 		if (PR.advrtrs) {
    890  1.1  itojun 			int j;
    891  1.1  itojun 			printf("  advertised by\n");
    892  1.1  itojun 			for (j = 0; j < PR.advrtrs; j++) {
    893  1.1  itojun 				printf("    %s\n",
    894  1.1  itojun 				       inet_ntop(AF_INET6, &PR.advrtr[j],
    895  1.1  itojun 						 ntop_buf,
    896  1.1  itojun 						 sizeof(ntop_buf)));
    897  1.1  itojun 			}
    898  1.1  itojun 			if (PR.advrtrs > DRLSTSIZ)
    899  1.1  itojun 				printf("    and %d routers\n",
    900  1.1  itojun 				       PR.advrtrs - DRLSTSIZ);
    901  1.1  itojun 		}
    902  1.1  itojun 		else
    903  1.1  itojun 			printf("  No advertising router\n");
    904  1.1  itojun 	}
    905  1.1  itojun #undef PR
    906  1.1  itojun 	close(s);
    907  1.1  itojun }
    908  1.1  itojun 
    909  1.1  itojun void
    910  1.1  itojun pfx_flush()
    911  1.1  itojun {
    912  1.1  itojun 	char dummyif[IFNAMSIZ+8];
    913  1.1  itojun 	int s;
    914  1.1  itojun 
    915  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    916  1.1  itojun 		err(1, "socket");
    917  1.1  itojun 	strcpy(dummyif, "lo0"); /* dummy */
    918  1.1  itojun 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
    919  1.1  itojun  		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
    920  1.1  itojun }
    921  1.1  itojun 
    922  1.1  itojun void
    923  1.1  itojun rtr_flush()
    924  1.1  itojun {
    925  1.1  itojun 	char dummyif[IFNAMSIZ+8];
    926  1.1  itojun 	int s;
    927  1.1  itojun 
    928  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    929  1.1  itojun 		err(1, "socket");
    930  1.1  itojun 	strcpy(dummyif, "lo0"); /* dummy */
    931  1.1  itojun 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
    932  1.1  itojun  		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
    933  1.1  itojun }
    934  1.1  itojun 
    935  1.1  itojun void
    936  1.1  itojun harmonize_rtr()
    937  1.1  itojun {
    938  1.1  itojun 	char dummyif[IFNAMSIZ+8];
    939  1.1  itojun 	int s;
    940  1.1  itojun 
    941  1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    942  1.1  itojun 		perror("ndp: socket");
    943  1.1  itojun 		exit(1);
    944  1.1  itojun 	}
    945  1.1  itojun 	strcpy(dummyif, "lo0"); /* dummy */
    946  1.1  itojun 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) {
    947  1.1  itojun  		perror("ioctl (SIOCSNDFLUSH_IN6)");
    948  1.1  itojun  		exit(1);
    949  1.1  itojun  	}
    950  1.1  itojun }
    951  1.1  itojun 
    952  1.1  itojun static char *
    953  1.1  itojun sec2str(total)
    954  1.1  itojun 	time_t total;
    955  1.1  itojun {
    956  1.1  itojun 	static char result[256];
    957  1.1  itojun 	int days, hours, mins, secs;
    958  1.1  itojun 	int first = 1;
    959  1.1  itojun 	char *p = result;
    960  1.1  itojun 
    961  1.1  itojun 	days = total / 3600 / 24;
    962  1.1  itojun 	hours = (total / 3600) % 24;
    963  1.1  itojun 	mins = (total / 60) % 60;
    964  1.1  itojun 	secs = total % 60;
    965  1.1  itojun 
    966  1.1  itojun 	if (days) {
    967  1.1  itojun 		first = 0;
    968  1.1  itojun 		p += sprintf(p, "%dd", days);
    969  1.1  itojun 	}
    970  1.1  itojun 	if (!first || hours) {
    971  1.1  itojun 		first = 0;
    972  1.1  itojun 		p += sprintf(p, "%dh", hours);
    973  1.1  itojun 	}
    974  1.1  itojun 	if (!first || mins) {
    975  1.1  itojun 		first = 0;
    976  1.1  itojun 		p += sprintf(p, "%dm", mins);
    977  1.1  itojun 	}
    978  1.1  itojun 	sprintf(p, "%ds", secs);
    979  1.1  itojun 
    980  1.1  itojun 	return(result);
    981  1.1  itojun }
    982  1.1  itojun 
    983  1.1  itojun /*
    984  1.1  itojun  * Print the timestamp
    985  1.1  itojun  * from tcpdump/util.c
    986  1.1  itojun  */
    987  1.1  itojun static void
    988  1.1  itojun ts_print(tvp)
    989  1.1  itojun 	const struct timeval *tvp;
    990  1.1  itojun {
    991  1.1  itojun 	int s;
    992  1.1  itojun 
    993  1.1  itojun 	/* Default */
    994  1.1  itojun 	s = (tvp->tv_sec + thiszone) % 86400;
    995  1.1  itojun 	(void)printf("%02d:%02d:%02d.%06u ",
    996  1.1  itojun 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
    997  1.1  itojun }
    998