Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.61
      1  1.61  christos /*	$NetBSD: arp.c,v 1.61 2018/07/07 15:44:27 christos Exp $ */
      2  1.10    chopps 
      3   1.1       cgd /*
      4   1.5   mycroft  * Copyright (c) 1984, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Sun Microsystems, Inc.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.38       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19     lukem #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.47     lukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
     38  1.47     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.19     lukem #if 0
     43  1.19     lukem static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     44  1.19     lukem #else
     45  1.61  christos __RCSID("$NetBSD: arp.c,v 1.61 2018/07/07 15:44:27 christos Exp $");
     46  1.19     lukem #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * arp - display, set, and delete arp table entries
     51   1.1       cgd  */
     52   1.1       cgd 
     53   1.1       cgd #include <sys/param.h>
     54   1.1       cgd #include <sys/file.h>
     55   1.1       cgd #include <sys/socket.h>
     56   1.5   mycroft #include <sys/sysctl.h>
     57  1.29      fair #include <sys/ioctl.h>
     58   1.5   mycroft 
     59   1.5   mycroft #include <net/if.h>
     60   1.5   mycroft #include <net/if_dl.h>
     61  1.17        is #include <net/if_ether.h>
     62   1.5   mycroft #include <net/if_types.h>
     63   1.5   mycroft #include <net/route.h>
     64   1.1       cgd #include <netinet/in.h>
     65  1.17        is #include <netinet/if_inarp.h>
     66   1.2   mycroft #include <arpa/inet.h>
     67   1.1       cgd 
     68  1.15     mikel #include <err.h>
     69  1.15     mikel #include <errno.h>
     70   1.5   mycroft #include <netdb.h>
     71   1.1       cgd #include <nlist.h>
     72  1.15     mikel #include <paths.h>
     73   1.1       cgd #include <stdio.h>
     74   1.8    chopps #include <stdlib.h>
     75  1.12       cgd #include <string.h>
     76  1.15     mikel #include <unistd.h>
     77  1.35     rafal #include <ifaddrs.h>
     78   1.1       cgd 
     79  1.52     ozaki #include "prog_ops.h"
     80  1.52     ozaki 
     81  1.43    dyoung static int is_llinfo(const struct sockaddr_dl *, int);
     82  1.58     ozaki static int delete_one(struct rt_msghdr *);
     83  1.44  christos static void dump(uint32_t);
     84  1.58     ozaki static void delete(const char *, const char *);
     85  1.44  christos static void sdl_print(const struct sockaddr_dl *);
     86  1.44  christos static int getifname(u_int16_t, char *, size_t);
     87  1.60  christos static int atosdl(const char *, struct sockaddr_dl *);
     88  1.44  christos static int file(const char *);
     89  1.44  christos static void get(const char *);
     90  1.44  christos static int getinetaddr(const char *, struct in_addr *);
     91  1.53     ozaki static int getsocket(void);
     92  1.60  christos static int getetheraddr(struct in_addr, struct sockaddr_dl *);
     93  1.60  christos static struct rt_msghdr * rtmsg(const int, const int,  struct rt_msghdr *,
     94  1.60  christos 	const struct sockaddr_inarp *, const struct sockaddr_dl *);
     95  1.44  christos static int set(int, char **);
     96  1.45     perry static void usage(void) __dead;
     97  1.17        is 
     98  1.30    atatat static int aflag, nflag, vflag;
     99  1.56       ryo static struct sockaddr_in so_mask = {
    100  1.44  christos 	.sin_len = 8,
    101  1.44  christos 	.sin_addr = {
    102  1.44  christos 		.s_addr = 0xffffffff
    103  1.44  christos 	}
    104  1.44  christos };
    105  1.44  christos static struct sockaddr_inarp blank_sin = {
    106  1.44  christos 	.sin_len = sizeof(blank_sin),
    107  1.44  christos 	.sin_family = AF_INET
    108  1.44  christos };
    109  1.44  christos static struct sockaddr_dl blank_sdl = {
    110  1.44  christos 	.sdl_len = sizeof(blank_sdl),
    111  1.44  christos 	.sdl_family = AF_LINK
    112  1.44  christos };
    113  1.44  christos 
    114  1.44  christos static int expire_time, flags, export_only, doing_proxy, found_entry;
    115  1.16     mikel 
    116   1.8    chopps int
    117  1.41   xtraeme main(int argc, char **argv)
    118   1.1       cgd {
    119   1.1       cgd 	int ch;
    120  1.15     mikel 	int op = 0;
    121  1.34        tv 
    122  1.34        tv 	setprogname(argv[0]);
    123   1.1       cgd 
    124  1.23       mrg 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    125   1.1       cgd 		switch((char)ch) {
    126   1.5   mycroft 		case 'a':
    127  1.30    atatat 			aflag = 1;
    128  1.30    atatat 			break;
    129   1.1       cgd 		case 'd':
    130  1.15     mikel 		case 's':
    131  1.15     mikel 		case 'f':
    132  1.15     mikel 			if (op)
    133   1.1       cgd 				usage();
    134  1.15     mikel 			op = ch;
    135  1.15     mikel 			break;
    136   1.5   mycroft 		case 'n':
    137   1.5   mycroft 			nflag = 1;
    138   1.8    chopps 			break;
    139  1.23       mrg 		case 'v':
    140  1.23       mrg 			vflag = 1;
    141  1.23       mrg 			break;
    142   1.1       cgd 		default:
    143   1.1       cgd 			usage();
    144   1.1       cgd 		}
    145  1.15     mikel 	argc -= optind;
    146  1.15     mikel 	argv += optind;
    147  1.15     mikel 
    148  1.30    atatat 	if (!op && aflag)
    149  1.30    atatat 		op = 'a';
    150  1.30    atatat 
    151  1.52     ozaki 	if (prog_init && prog_init() == -1)
    152  1.52     ozaki 		err(1, "init failed");
    153  1.52     ozaki 
    154  1.15     mikel 	switch((char)op) {
    155  1.15     mikel 	case 'a':
    156  1.15     mikel 		dump(0);
    157  1.15     mikel 		break;
    158  1.15     mikel 	case 'd':
    159  1.30    atatat 		if (aflag && argc == 0)
    160  1.58     ozaki 			delete(NULL, NULL);
    161  1.30    atatat 		else {
    162  1.30    atatat 			if (aflag || argc < 1 || argc > 2)
    163  1.30    atatat 				usage();
    164  1.58     ozaki 			delete(argv[0], argv[1]);
    165  1.30    atatat 		}
    166  1.15     mikel 		break;
    167  1.15     mikel 	case 's':
    168  1.51  christos 		if (argc < 2 || argc > 7)
    169  1.15     mikel 			usage();
    170  1.15     mikel 		return (set(argc, argv) ? 1 : 0);
    171  1.15     mikel 	case 'f':
    172  1.15     mikel 		if (argc != 1)
    173  1.15     mikel 			usage();
    174  1.15     mikel 		return (file(argv[0]));
    175  1.15     mikel 	default:
    176  1.15     mikel 		if (argc != 1)
    177  1.15     mikel 			usage();
    178  1.15     mikel 		get(argv[0]);
    179  1.15     mikel 		break;
    180  1.15     mikel 	}
    181   1.8    chopps 	return (0);
    182   1.1       cgd }
    183   1.1       cgd 
    184   1.1       cgd /*
    185   1.1       cgd  * Process a file to set standard arp entries
    186   1.1       cgd  */
    187  1.44  christos static int
    188  1.44  christos file(const char *name)
    189   1.1       cgd {
    190  1.44  christos 	char *line, *argv[5];
    191   1.8    chopps 	int i, retval;
    192   1.1       cgd 	FILE *fp;
    193   1.1       cgd 
    194  1.49  dholland 	if (!strcmp(name, "-")) {
    195  1.49  dholland 		fp = stdin;
    196  1.49  dholland 	} else {
    197  1.49  dholland 		fp = fopen(name, "r");
    198  1.49  dholland 		if (fp == NULL) {
    199  1.49  dholland 			err(1, "Cannot open %s", name);
    200  1.49  dholland 		}
    201  1.49  dholland 	}
    202   1.1       cgd 	retval = 0;
    203  1.44  christos 	for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
    204  1.44  christos 		char **ap, *inputstring;
    205  1.44  christos 
    206  1.44  christos 		inputstring = line;
    207  1.44  christos 		for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
    208  1.44  christos 		    (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
    209  1.56       ryo 			if (**ap != '\0')
    210  1.44  christos 				ap++;
    211  1.44  christos 		}
    212  1.44  christos 		i = ap - argv;
    213   1.1       cgd 		if (i < 2) {
    214   1.8    chopps 			warnx("bad line: %s", line);
    215   1.1       cgd 			retval = 1;
    216   1.1       cgd 			continue;
    217   1.1       cgd 		}
    218  1.44  christos 		if (set(i, argv))
    219   1.1       cgd 			retval = 1;
    220   1.1       cgd 	}
    221  1.49  dholland 	if (fp != stdin)
    222  1.49  dholland 		(void)fclose(fp);
    223  1.44  christos 	return retval;
    224   1.1       cgd }
    225   1.1       cgd 
    226  1.53     ozaki static int
    227  1.41   xtraeme getsocket(void)
    228   1.8    chopps {
    229  1.53     ozaki 	int s;
    230  1.52     ozaki 	s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
    231   1.8    chopps 	if (s < 0)
    232   1.8    chopps 		err(1, "socket");
    233  1.53     ozaki 	return s;
    234   1.5   mycroft }
    235   1.5   mycroft 
    236  1.50  christos static int
    237  1.50  christos getlink(const char *name, struct sockaddr_dl *sdl)
    238  1.50  christos {
    239  1.50  christos 	struct ifaddrs *ifap, *ifa;
    240  1.50  christos 
    241  1.50  christos 	if (getifaddrs(&ifap) != 0) {
    242  1.50  christos 		warn("getifaddrs");
    243  1.50  christos 		return 0;
    244  1.50  christos 	}
    245  1.50  christos 
    246  1.50  christos 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    247  1.50  christos 		if (ifa->ifa_addr->sa_family != AF_LINK)
    248  1.50  christos 			continue;
    249  1.50  christos 		if (strcmp(ifa->ifa_name, name) != 0)
    250  1.50  christos 			continue;
    251  1.50  christos 		memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
    252  1.50  christos 		freeifaddrs(ifap);
    253  1.50  christos 		return 1;
    254  1.50  christos 	}
    255  1.50  christos 	freeifaddrs(ifap);
    256  1.50  christos 	return 0;
    257  1.50  christos }
    258  1.50  christos 
    259   1.1       cgd /*
    260  1.56       ryo  * Set an individual arp entry
    261   1.1       cgd  */
    262  1.44  christos static int
    263  1.41   xtraeme set(int argc, char **argv)
    264   1.1       cgd {
    265  1.41   xtraeme 	struct sockaddr_inarp *sina;
    266  1.55     ozaki 	struct sockaddr_dl *sdl = NULL;
    267  1.19     lukem 	struct rt_msghdr *rtm;
    268   1.8    chopps 	char *host = argv[0], *eaddr;
    269  1.53     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    270  1.53     ozaki 	struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */
    271  1.53     ozaki 	int s;
    272   1.8    chopps 
    273   1.8    chopps 	eaddr = argv[1];
    274   1.1       cgd 
    275  1.53     ozaki 	s = getsocket();
    276   1.1       cgd 	argc -= 2;
    277   1.1       cgd 	argv += 2;
    278  1.53     ozaki 
    279  1.53     ozaki 	if (getinetaddr(host, &sin_m.sin_addr) == -1)
    280   1.8    chopps 		return (1);
    281  1.61  christos 	if (strcmp(eaddr, "auto") != 0 && atosdl(eaddr, &sdl_m))
    282  1.17        is 		warnx("invalid link-level address '%s'", eaddr);
    283   1.5   mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    284  1.50  christos 	for (; argc-- > 0; argv++) {
    285   1.5   mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    286  1.41   xtraeme 			struct timeval timev;
    287  1.41   xtraeme 			(void)gettimeofday(&timev, 0);
    288  1.41   xtraeme 			expire_time = timev.tv_sec + 20 * 60;
    289   1.5   mycroft 		}
    290   1.5   mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    291   1.5   mycroft 			flags |= RTF_ANNOUNCE;
    292  1.54     ozaki 			doing_proxy = 1;
    293  1.46     seanb 			if (argc && strncmp(argv[1], "pro", 3) == 0) {
    294  1.56       ryo 				export_only = 1;
    295  1.56       ryo 				argc--; argv++;
    296  1.46     seanb 			}
    297   1.5   mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    298  1.44  christos 			warnx("%s: Sending trailers is no longer supported",
    299  1.44  christos 			    host);
    300  1.50  christos 		} else if (strcmp(argv[0], "ifscope") == 0) {
    301  1.50  christos 			if (argc == 0) {
    302  1.50  christos 				warnx("missing interface for ifscope");
    303  1.50  christos 				continue;
    304  1.50  christos 			}
    305  1.50  christos 			argc--;
    306  1.50  christos 			argv++;
    307  1.50  christos 			if (!getlink(argv[0], &sdl_m))
    308  1.50  christos 				warnx("cannot get link address for %s", argv[0]);
    309   1.5   mycroft 		}
    310  1.50  christos 
    311   1.1       cgd 	}
    312  1.61  christos 	if (doing_proxy && strcmp(eaddr, "auto") == 0) {
    313  1.60  christos 		if (getetheraddr(sin_m.sin_addr, &sdl_m) == -1)
    314  1.60  christos 			return 1;
    315  1.60  christos 	}
    316   1.5   mycroft tryagain:
    317  1.58     ozaki 	rtm = rtmsg(s, RTM_GET, NULL, &sin_m, &sdl_m);
    318  1.53     ozaki 	if (rtm == NULL) {
    319   1.8    chopps 		warn("%s", host);
    320   1.5   mycroft 		return (1);
    321   1.1       cgd 	}
    322  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    323  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    324  1.44  christos 	    (char *)(void *)sina);
    325  1.41   xtraeme 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    326  1.43    dyoung 		if (is_llinfo(sdl, rtm->rtm_flags))
    327   1.5   mycroft 			goto overwrite;
    328   1.5   mycroft 		if (doing_proxy == 0) {
    329  1.44  christos 			warnx("set: can only proxy for %s", host);
    330   1.5   mycroft 			return (1);
    331   1.5   mycroft 		}
    332   1.5   mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    333  1.44  christos 			warnx("set: proxy entry exists for non 802 device");
    334   1.8    chopps 			return (1);
    335   1.5   mycroft 		}
    336   1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    337   1.5   mycroft 		export_only = 1;
    338   1.5   mycroft 		goto tryagain;
    339   1.5   mycroft 	}
    340   1.5   mycroft overwrite:
    341   1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    342  1.44  christos 		warnx("cannot intuit interface index and type for %s",
    343   1.8    chopps 		    host);
    344   1.5   mycroft 		return (1);
    345   1.1       cgd 	}
    346   1.5   mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    347   1.5   mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    348  1.53     ozaki 	sin_m.sin_other = 0;
    349  1.53     ozaki 	if (doing_proxy && export_only)
    350  1.53     ozaki 		sin_m.sin_other = SIN_PROXY;
    351  1.58     ozaki 	rtm = rtmsg(s, RTM_ADD, NULL, &sin_m, &sdl_m);
    352  1.23       mrg 	if (vflag)
    353  1.23       mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    354  1.53     ozaki 	return (rtm == NULL) ? 1 : 0;
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd /*
    358   1.1       cgd  * Display an individual arp entry
    359   1.1       cgd  */
    360  1.44  christos static void
    361  1.41   xtraeme get(const char *host)
    362   1.1       cgd {
    363  1.53     ozaki 	struct sockaddr_inarp sin = blank_sin; /* struct copy */
    364   1.1       cgd 
    365  1.53     ozaki 	if (getinetaddr(host, &sin.sin_addr) == -1)
    366   1.8    chopps 		exit(1);
    367  1.53     ozaki 	dump(sin.sin_addr.s_addr);
    368  1.44  christos 	if (found_entry == 0)
    369  1.53     ozaki 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr));
    370   1.1       cgd }
    371   1.1       cgd 
    372  1.43    dyoung 
    373  1.43    dyoung static int
    374  1.43    dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
    375  1.43    dyoung {
    376  1.43    dyoung 	if (sdl->sdl_family != AF_LINK ||
    377  1.55     ozaki 	    (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
    378  1.43    dyoung 		return 0;
    379  1.43    dyoung 
    380  1.43    dyoung 	switch (sdl->sdl_type) {
    381  1.43    dyoung 	case IFT_ETHER:
    382  1.43    dyoung 	case IFT_FDDI:
    383  1.43    dyoung 	case IFT_ISO88023:
    384  1.43    dyoung 	case IFT_ISO88024:
    385  1.43    dyoung 	case IFT_ISO88025:
    386  1.43    dyoung 	case IFT_ARCNET:
    387  1.43    dyoung 		return 1;
    388  1.43    dyoung 	default:
    389  1.43    dyoung 		return 0;
    390  1.43    dyoung 	}
    391  1.43    dyoung }
    392  1.43    dyoung 
    393   1.1       cgd /*
    394  1.56       ryo  * Delete an arp entry
    395   1.1       cgd  */
    396   1.8    chopps int
    397  1.58     ozaki delete_one(struct rt_msghdr *rtm)
    398   1.1       cgd {
    399  1.41   xtraeme 	struct sockaddr_inarp *sina;
    400  1.53     ozaki 	struct sockaddr_dl *sdl;
    401  1.53     ozaki 	int s;
    402   1.8    chopps 
    403  1.53     ozaki 	s = getsocket();
    404  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    405  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    406  1.44  christos 	    (char *)(void *)sina);
    407  1.58     ozaki 	if (sdl->sdl_family != AF_LINK)
    408   1.5   mycroft 		return (1);
    409  1.58     ozaki 	rtm = rtmsg(s, RTM_DELETE, rtm, sina, sdl);
    410  1.53     ozaki 	if (rtm == NULL)
    411   1.8    chopps 		return (1);
    412   1.8    chopps 	return (0);
    413   1.1       cgd }
    414   1.1       cgd 
    415   1.1       cgd /*
    416   1.1       cgd  * Dump the entire arp table
    417   1.1       cgd  */
    418   1.8    chopps void
    419  1.44  christos dump(uint32_t addr)
    420   1.1       cgd {
    421   1.5   mycroft 	int mib[6];
    422   1.5   mycroft 	size_t needed;
    423  1.29      fair 	char ifname[IFNAMSIZ];
    424  1.41   xtraeme 	char *lim, *buf, *next;
    425  1.41   xtraeme         const char *host;
    426   1.5   mycroft 	struct rt_msghdr *rtm;
    427  1.41   xtraeme 	struct sockaddr_inarp *sina;
    428   1.5   mycroft 	struct sockaddr_dl *sdl;
    429   1.1       cgd 	struct hostent *hp;
    430   1.1       cgd 
    431   1.5   mycroft 	mib[0] = CTL_NET;
    432   1.5   mycroft 	mib[1] = PF_ROUTE;
    433   1.5   mycroft 	mib[2] = 0;
    434   1.5   mycroft 	mib[3] = AF_INET;
    435   1.5   mycroft 	mib[4] = NET_RT_FLAGS;
    436  1.55     ozaki 	mib[5] = 0;
    437  1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    438   1.8    chopps 		err(1, "route-sysctl-estimate");
    439  1.22      fvdl 	if (needed == 0)
    440  1.22      fvdl 		return;
    441   1.5   mycroft 	if ((buf = malloc(needed)) == NULL)
    442   1.8    chopps 		err(1, "malloc");
    443  1.52     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    444   1.8    chopps 		err(1, "actual retrieval of routing table");
    445   1.5   mycroft 	lim = buf + needed;
    446   1.5   mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    447  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    448  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    449  1.44  christos 		sdl = (struct sockaddr_dl *)(void *)
    450  1.48  christos 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
    451   1.5   mycroft 		if (addr) {
    452  1.41   xtraeme 			if (addr != sina->sin_addr.s_addr)
    453   1.5   mycroft 				continue;
    454   1.5   mycroft 			found_entry = 1;
    455   1.5   mycroft 		}
    456   1.5   mycroft 		if (nflag == 0)
    457  1.44  christos 			hp = gethostbyaddr((const char *)(void *)
    458  1.44  christos 			    &(sina->sin_addr),
    459  1.41   xtraeme 			    sizeof sina->sin_addr, AF_INET);
    460   1.1       cgd 		else
    461  1.27  christos 			hp = NULL;
    462  1.27  christos 
    463  1.27  christos 		host = hp ? hp->h_name : "?";
    464  1.27  christos 
    465  1.41   xtraeme 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
    466   1.5   mycroft 		if (sdl->sdl_alen)
    467  1.17        is 			sdl_print(sdl);
    468   1.1       cgd 		else
    469   1.8    chopps 			(void)printf("(incomplete)");
    470  1.29      fair 
    471  1.29      fair 		if (sdl->sdl_index) {
    472  1.37    itojun 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
    473  1.44  christos 				(void)printf(" on %s", ifname);
    474  1.29      fair 		}
    475  1.29      fair 
    476   1.5   mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    477   1.8    chopps 			(void)printf(" permanent");
    478  1.41   xtraeme 		if (sina->sin_other & SIN_PROXY)
    479   1.8    chopps 			(void)printf(" published (proxy only)");
    480   1.5   mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    481  1.44  christos 			sina = (struct sockaddr_inarp *)(void *)
    482  1.48  christos 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
    483  1.41   xtraeme 			if (sina->sin_addr.s_addr == 0xffffffff)
    484   1.8    chopps 				(void)printf(" published");
    485  1.41   xtraeme 			if (sina->sin_len != 8)
    486  1.25       erh 				(void)printf("(weird)");
    487   1.5   mycroft 		}
    488   1.8    chopps 		(void)printf("\n");
    489   1.1       cgd 	}
    490  1.39    itojun 	free(buf);
    491   1.1       cgd }
    492   1.1       cgd 
    493  1.30    atatat /*
    494  1.30    atatat  * Delete the entire arp table
    495  1.30    atatat  */
    496  1.30    atatat void
    497  1.58     ozaki delete(const char *host, const char *info)
    498  1.30    atatat {
    499  1.30    atatat 	int mib[6];
    500  1.30    atatat 	size_t needed;
    501  1.30    atatat 	char *lim, *buf, *next;
    502  1.30    atatat 	struct rt_msghdr *rtm;
    503  1.41   xtraeme 	struct sockaddr_inarp *sina;
    504  1.58     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    505  1.58     ozaki 
    506  1.58     ozaki 	if (host != NULL) {
    507  1.58     ozaki 		int ret = getinetaddr(host, &sin_m.sin_addr);
    508  1.58     ozaki 		if (ret == -1)
    509  1.58     ozaki 			return;
    510  1.58     ozaki 	}
    511  1.58     ozaki 	if (info && strncmp(info, "pro", 3) == 0)
    512  1.58     ozaki 		sin_m.sin_other = SIN_PROXY;
    513  1.30    atatat 
    514  1.58     ozaki retry:
    515  1.30    atatat 	mib[0] = CTL_NET;
    516  1.30    atatat 	mib[1] = PF_ROUTE;
    517  1.30    atatat 	mib[2] = 0;
    518  1.30    atatat 	mib[3] = AF_INET;
    519  1.30    atatat 	mib[4] = NET_RT_FLAGS;
    520  1.58     ozaki 	mib[5] = RTF_LLDATA;
    521  1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    522  1.30    atatat 		err(1, "route-sysctl-estimate");
    523  1.30    atatat 	if (needed == 0)
    524  1.30    atatat 		return;
    525  1.30    atatat 	if ((buf = malloc(needed)) == NULL)
    526  1.30    atatat 		err(1, "malloc");
    527  1.58     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    528  1.58     ozaki 		free(buf);
    529  1.58     ozaki 		if (errno == ENOBUFS)
    530  1.58     ozaki 			goto retry;
    531  1.30    atatat 		err(1, "actual retrieval of routing table");
    532  1.58     ozaki 	}
    533  1.30    atatat 	lim = buf + needed;
    534  1.58     ozaki 
    535  1.30    atatat 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    536  1.58     ozaki 		int ret;
    537  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    538  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    539  1.58     ozaki 		if (host != NULL &&
    540  1.58     ozaki 		    sina->sin_addr.s_addr != sin_m.sin_addr.s_addr)
    541  1.58     ozaki 			continue;
    542  1.58     ozaki 		ret = delete_one(rtm);
    543  1.58     ozaki 		if (vflag && ret == 0) {
    544  1.58     ozaki 			(void)printf("%s (%s) deleted\n", host,
    545  1.58     ozaki 			    inet_ntoa(sina->sin_addr));
    546  1.58     ozaki 		}
    547  1.30    atatat 	}
    548  1.39    itojun 	free(buf);
    549  1.30    atatat }
    550  1.30    atatat 
    551   1.8    chopps void
    552  1.41   xtraeme sdl_print(const struct sockaddr_dl *sdl)
    553  1.17        is {
    554  1.32     bjh21 	char hbuf[NI_MAXHOST];
    555  1.17        is 
    556  1.44  christos 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
    557  1.44  christos 	    (socklen_t)sdl->sdl_len,
    558  1.32     bjh21 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    559  1.44  christos 		(void)printf("<invalid>");
    560  1.32     bjh21 	else
    561  1.44  christos 		(void)printf("%s", hbuf);
    562  1.17        is }
    563  1.17        is 
    564  1.44  christos static int
    565  1.41   xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
    566   1.1       cgd {
    567  1.17        is 	int i;
    568  1.44  christos 	unsigned long b;
    569  1.44  christos 	char *endp;
    570  1.56       ryo 	char *p;
    571  1.17        is 	char *t, *r;
    572  1.17        is 
    573  1.17        is 	p = LLADDR(sdl);
    574  1.44  christos 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
    575  1.17        is 	i = 0;
    576  1.56       ryo 
    577  1.44  christos 	b = strtoul(ss, &t, 16);
    578  1.44  christos 	if (b > 255 || t == ss)
    579  1.17        is 		return 1;
    580  1.17        is 
    581  1.44  christos 	*p++ = (char)b;
    582  1.17        is 	++i;
    583  1.17        is 	while ((p < endp) && (*t++ == ':')) {
    584  1.44  christos 		b = strtoul(t, &r, 16);
    585  1.44  christos 		if (b > 255 || r == t)
    586  1.17        is 			break;
    587  1.44  christos 		*p++ = (char)b;
    588  1.17        is 		++i;
    589  1.17        is 		t = r;
    590  1.17        is 	}
    591  1.17        is 	sdl->sdl_alen = i;
    592  1.17        is 
    593  1.17        is 	return 0;
    594   1.1       cgd }
    595   1.1       cgd 
    596  1.44  christos static void
    597  1.41   xtraeme usage(void)
    598   1.1       cgd {
    599  1.33     pooka 	const char *progname;
    600  1.20     lukem 
    601  1.33     pooka 	progname = getprogname();
    602  1.44  christos 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
    603  1.56       ryo 	(void)fprintf(stderr, "       %s [-nv] -a\n", progname);
    604  1.59    nonaka 	(void)fprintf(stderr, "       %s [-v] -d [-a|hostname [proxy]]\n",
    605  1.44  christos 	    progname);
    606  1.46     seanb 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
    607  1.44  christos 	    progname);
    608  1.44  christos 	(void)fprintf(stderr, "       %s -f filename\n", progname);
    609   1.5   mycroft 	exit(1);
    610   1.5   mycroft }
    611   1.5   mycroft 
    612  1.53     ozaki static struct rt_msghdr *
    613  1.58     ozaki rtmsg(const int s, const int cmd, struct rt_msghdr *_rtm,
    614  1.58     ozaki     const struct sockaddr_inarp *sin, const struct sockaddr_dl *sdl)
    615   1.5   mycroft {
    616   1.5   mycroft 	static int seq;
    617  1.58     ozaki 	struct rt_msghdr *rtm = _rtm;
    618  1.19     lukem 	char *cp;
    619  1.19     lukem 	int l;
    620  1.53     ozaki 	static struct {
    621  1.53     ozaki 		struct	rt_msghdr m_rtm;
    622  1.53     ozaki 		char	m_space[512];
    623  1.53     ozaki 	} m_rtmsg;
    624  1.53     ozaki 	pid_t pid;
    625   1.5   mycroft 
    626   1.5   mycroft 	errno = 0;
    627  1.58     ozaki 	if (rtm != NULL) {
    628  1.58     ozaki 		memcpy(&m_rtmsg, rtm, rtm->rtm_msglen);
    629  1.58     ozaki 		rtm = &m_rtmsg.m_rtm;
    630   1.5   mycroft 		goto doit;
    631  1.55     ozaki 	}
    632   1.8    chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    633  1.58     ozaki 	rtm = &m_rtmsg.m_rtm;
    634  1.58     ozaki 	cp = m_rtmsg.m_space;
    635  1.58     ozaki 
    636   1.5   mycroft 	rtm->rtm_flags = flags;
    637   1.5   mycroft 	rtm->rtm_version = RTM_VERSION;
    638   1.5   mycroft 
    639   1.5   mycroft 	switch (cmd) {
    640   1.5   mycroft 	default:
    641   1.8    chopps 		errx(1, "internal wrong cmd");
    642   1.8    chopps 		/*NOTREACHED*/
    643   1.5   mycroft 	case RTM_ADD:
    644   1.5   mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    645   1.5   mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    646   1.5   mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    647  1.55     ozaki 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
    648   1.5   mycroft 		if (doing_proxy) {
    649  1.53     ozaki 			if (!export_only) {
    650   1.5   mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    651   1.5   mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    652   1.5   mycroft 			}
    653   1.5   mycroft 		}
    654  1.55     ozaki 		rtm->rtm_addrs |= RTA_DST;
    655  1.55     ozaki 		break;
    656   1.5   mycroft 	case RTM_GET:
    657  1.55     ozaki 		rtm->rtm_flags |= RTF_LLDATA;
    658  1.57     ozaki 		rtm->rtm_addrs |= RTA_DST | RTA_GATEWAY;
    659   1.5   mycroft 	}
    660  1.25       erh 
    661   1.5   mycroft #define NEXTADDR(w, s) \
    662   1.5   mycroft 	if (rtm->rtm_addrs & (w)) { \
    663  1.44  christos 		(void)memcpy(cp, &s, \
    664  1.53     ozaki 		    (size_t)((const struct sockaddr *)&s)->sa_len); \
    665  1.53     ozaki 		RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \
    666  1.25       erh 	}
    667   1.5   mycroft 
    668  1.53     ozaki 	NEXTADDR(RTA_DST, *sin);
    669  1.53     ozaki 	NEXTADDR(RTA_GATEWAY, *sdl);
    670   1.5   mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    671   1.5   mycroft 
    672  1.44  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    673   1.5   mycroft doit:
    674   1.5   mycroft 	l = rtm->rtm_msglen;
    675   1.5   mycroft 	rtm->rtm_seq = ++seq;
    676   1.5   mycroft 	rtm->rtm_type = cmd;
    677  1.52     ozaki 	if (prog_write(s, &m_rtmsg, (size_t)l) < 0) {
    678   1.5   mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    679   1.8    chopps 			warn("writing to routing socket");
    680  1.53     ozaki 			return NULL;
    681   1.5   mycroft 		}
    682   1.5   mycroft 	}
    683  1.53     ozaki 
    684  1.53     ozaki 	pid = prog_getpid();
    685   1.5   mycroft 	do {
    686  1.52     ozaki 		l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg));
    687   1.5   mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    688   1.5   mycroft 	if (l < 0)
    689   1.8    chopps 		warn("read from routing socket");
    690  1.53     ozaki 	return rtm;
    691   1.5   mycroft }
    692   1.5   mycroft 
    693  1.44  christos static int
    694  1.41   xtraeme getinetaddr(const char *host, struct in_addr *inap)
    695   1.5   mycroft {
    696   1.8    chopps 	struct hostent *hp;
    697   1.8    chopps 
    698   1.9    chopps 	if (inet_aton(host, inap) == 1)
    699   1.8    chopps 		return (0);
    700   1.8    chopps 	if ((hp = gethostbyname(host)) == NULL) {
    701  1.31       chs 		warnx("%s: %s", host, hstrerror(h_errno));
    702   1.8    chopps 		return (-1);
    703   1.8    chopps 	}
    704   1.8    chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    705   1.8    chopps 	return (0);
    706  1.29      fair }
    707  1.29      fair 
    708  1.44  christos static int
    709  1.41   xtraeme getifname(u_int16_t ifindex, char *ifname, size_t l)
    710  1.29      fair {
    711  1.35     rafal 	int i;
    712  1.35     rafal 	struct ifaddrs *addr;
    713  1.60  christos 	const struct sockaddr_dl *sdl;
    714  1.53     ozaki 	static struct ifaddrs* ifaddrs = NULL;
    715  1.29      fair 
    716  1.35     rafal 	if (ifaddrs == NULL) {
    717  1.35     rafal 		i = getifaddrs(&ifaddrs);
    718  1.35     rafal 		if (i != 0)
    719  1.35     rafal 			err(1, "getifaddrs");
    720  1.29      fair 	}
    721  1.29      fair 
    722  1.35     rafal 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    723  1.56       ryo 		if (addr->ifa_addr == NULL ||
    724  1.35     rafal 		    addr->ifa_addr->sa_family != AF_LINK)
    725  1.29      fair 			continue;
    726  1.29      fair 
    727  1.44  christos 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
    728  1.29      fair 		if (sdl && sdl->sdl_index == ifindex) {
    729  1.37    itojun 			(void) strlcpy(ifname, addr->ifa_name, l);
    730  1.29      fair 			return 0;
    731  1.29      fair 		}
    732  1.29      fair 	}
    733  1.29      fair 
    734  1.29      fair 	return -1;
    735   1.1       cgd }
    736  1.60  christos 
    737  1.60  christos static int
    738  1.60  christos getetheraddr(struct in_addr ipaddr, struct sockaddr_dl *sdl)
    739  1.60  christos {
    740  1.60  christos 	struct ifaddrs *ifaddrs, *addr;
    741  1.60  christos 	in_addr_t ina, mask;
    742  1.60  christos 	char ifname[IFNAMSIZ];
    743  1.60  christos 
    744  1.60  christos 	if (getifaddrs(&ifaddrs) != 0) {
    745  1.60  christos 		warn("getifaddrs");
    746  1.60  christos 		return -1;
    747  1.60  christos 	}
    748  1.60  christos 
    749  1.60  christos 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    750  1.60  christos 		if (addr->ifa_addr == NULL ||
    751  1.60  christos 		    addr->ifa_addr->sa_family != AF_INET)
    752  1.60  christos 			continue;
    753  1.60  christos 		if ((addr->ifa_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
    754  1.60  christos 		    IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST))
    755  1.60  christos 			continue;
    756  1.60  christos 
    757  1.60  christos 		mask = ((struct sockaddr_in *)(void *)addr->ifa_netmask)->sin_addr.s_addr;
    758  1.60  christos 		ina = ((struct sockaddr_in *)(void *)addr->ifa_addr)->sin_addr.s_addr;
    759  1.60  christos 		if ((ipaddr.s_addr & mask) != (ina & mask))
    760  1.60  christos 			continue;
    761  1.60  christos 		strlcpy(ifname, addr->ifa_name, sizeof(ifname));
    762  1.60  christos 		break;
    763  1.60  christos 	}
    764  1.60  christos 	if (addr == NULL) {
    765  1.60  christos 		warnx("No interface matched %s", inet_ntoa(ipaddr));
    766  1.60  christos 		freeifaddrs(ifaddrs);
    767  1.60  christos 		return -1;
    768  1.60  christos 	}
    769  1.60  christos 
    770  1.60  christos 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    771  1.60  christos 		if (addr->ifa_addr == NULL ||
    772  1.60  christos 		    addr->ifa_addr->sa_family != AF_LINK)
    773  1.60  christos 			continue;
    774  1.60  christos 		if (strcmp(ifname, addr->ifa_name) != 0)
    775  1.60  christos 			continue;
    776  1.60  christos 		memcpy(sdl, addr->ifa_addr, sizeof(*sdl));
    777  1.60  christos 		freeifaddrs(ifaddrs);
    778  1.60  christos 		return 0;
    779  1.60  christos 	}
    780  1.60  christos 	warnx("No link address for interface %s", ifname);
    781  1.60  christos 	freeifaddrs(ifaddrs);
    782  1.60  christos 	return -1;
    783  1.60  christos }
    784