Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.48
      1  1.48  christos /*	$NetBSD: arp.c,v 1.48 2009/04/02 21:02:06 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.48  christos __RCSID("$NetBSD: arp.c,v 1.48 2009/04/02 21:02:06 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.43    dyoung static int is_llinfo(const struct sockaddr_dl *, int);
     80  1.44  christos static int delete(const char *, const char *);
     81  1.44  christos static void dump(uint32_t);
     82  1.44  christos static void delete_all(void);
     83  1.44  christos static void sdl_print(const struct sockaddr_dl *);
     84  1.44  christos static int getifname(u_int16_t, char *, size_t);
     85  1.44  christos static int atosdl(const char *s, struct sockaddr_dl *sdl);
     86  1.44  christos static int file(const char *);
     87  1.44  christos static void get(const char *);
     88  1.44  christos static int getinetaddr(const char *, struct in_addr *);
     89  1.44  christos static void getsocket(void);
     90  1.44  christos static int rtmsg(int);
     91  1.44  christos static int set(int, char **);
     92  1.45     perry static void usage(void) __dead;
     93  1.17        is 
     94  1.44  christos static pid_t pid;
     95  1.30    atatat static int aflag, nflag, vflag;
     96   1.5   mycroft static int s = -1;
     97  1.35     rafal static struct ifaddrs* ifaddrs = NULL;
     98  1.44  christos static struct sockaddr_in so_mask = {
     99  1.44  christos 	.sin_len = 8,
    100  1.44  christos 	.sin_addr = {
    101  1.44  christos 		.s_addr = 0xffffffff
    102  1.44  christos 	}
    103  1.44  christos };
    104  1.44  christos static struct sockaddr_inarp blank_sin = {
    105  1.44  christos 	.sin_len = sizeof(blank_sin),
    106  1.44  christos 	.sin_family = AF_INET
    107  1.44  christos };
    108  1.44  christos static struct sockaddr_inarp sin_m;
    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 static struct sockaddr_dl sdl_m;
    114  1.44  christos 
    115  1.44  christos static int expire_time, flags, export_only, doing_proxy, found_entry;
    116  1.44  christos static struct {
    117  1.44  christos 	struct	rt_msghdr m_rtm;
    118  1.44  christos 	char	m_space[512];
    119  1.44  christos } m_rtmsg;
    120  1.16     mikel 
    121   1.8    chopps int
    122  1.41   xtraeme main(int argc, char **argv)
    123   1.1       cgd {
    124   1.1       cgd 	int ch;
    125  1.15     mikel 	int op = 0;
    126  1.34        tv 
    127  1.34        tv 	setprogname(argv[0]);
    128   1.1       cgd 
    129   1.5   mycroft 	pid = getpid();
    130  1.15     mikel 
    131  1.23       mrg 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    132   1.1       cgd 		switch((char)ch) {
    133   1.5   mycroft 		case 'a':
    134  1.30    atatat 			aflag = 1;
    135  1.30    atatat 			break;
    136   1.1       cgd 		case 'd':
    137  1.15     mikel 		case 's':
    138  1.15     mikel 		case 'f':
    139  1.15     mikel 			if (op)
    140   1.1       cgd 				usage();
    141  1.15     mikel 			op = ch;
    142  1.15     mikel 			break;
    143   1.5   mycroft 		case 'n':
    144   1.5   mycroft 			nflag = 1;
    145   1.8    chopps 			break;
    146  1.23       mrg 		case 'v':
    147  1.23       mrg 			vflag = 1;
    148  1.23       mrg 			break;
    149   1.1       cgd 		default:
    150   1.1       cgd 			usage();
    151   1.1       cgd 		}
    152  1.15     mikel 	argc -= optind;
    153  1.15     mikel 	argv += optind;
    154  1.15     mikel 
    155  1.30    atatat 	if (!op && aflag)
    156  1.30    atatat 		op = 'a';
    157  1.30    atatat 
    158  1.15     mikel 	switch((char)op) {
    159  1.15     mikel 	case 'a':
    160  1.15     mikel 		dump(0);
    161  1.15     mikel 		break;
    162  1.15     mikel 	case 'd':
    163  1.30    atatat 		if (aflag && argc == 0)
    164  1.30    atatat 			delete_all();
    165  1.30    atatat 		else {
    166  1.30    atatat 			if (aflag || argc < 1 || argc > 2)
    167  1.30    atatat 				usage();
    168  1.30    atatat 			(void)delete(argv[0], argv[1]);
    169  1.30    atatat 		}
    170  1.15     mikel 		break;
    171  1.15     mikel 	case 's':
    172  1.15     mikel 		if (argc < 2 || argc > 5)
    173  1.15     mikel 			usage();
    174  1.15     mikel 		return (set(argc, argv) ? 1 : 0);
    175  1.15     mikel 	case 'f':
    176  1.15     mikel 		if (argc != 1)
    177  1.15     mikel 			usage();
    178  1.15     mikel 		return (file(argv[0]));
    179  1.15     mikel 	default:
    180  1.15     mikel 		if (argc != 1)
    181  1.15     mikel 			usage();
    182  1.15     mikel 		get(argv[0]);
    183  1.15     mikel 		break;
    184  1.15     mikel 	}
    185   1.8    chopps 	return (0);
    186   1.1       cgd }
    187   1.1       cgd 
    188   1.1       cgd /*
    189   1.1       cgd  * Process a file to set standard arp entries
    190   1.1       cgd  */
    191  1.44  christos static int
    192  1.44  christos file(const char *name)
    193   1.1       cgd {
    194  1.44  christos 	char *line, *argv[5];
    195   1.8    chopps 	int i, retval;
    196   1.1       cgd 	FILE *fp;
    197   1.1       cgd 
    198   1.8    chopps 	if ((fp = fopen(name, "r")) == NULL)
    199   1.8    chopps 		err(1, "cannot open %s", name);
    200   1.1       cgd 	retval = 0;
    201  1.44  christos 	for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
    202  1.44  christos 		char **ap, *inputstring;
    203  1.44  christos 
    204  1.44  christos 		inputstring = line;
    205  1.44  christos 		for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
    206  1.44  christos 		    (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
    207  1.44  christos 		       if (**ap != '\0')
    208  1.44  christos 				ap++;
    209  1.44  christos 		}
    210  1.44  christos 		i = ap - argv;
    211   1.1       cgd 		if (i < 2) {
    212   1.8    chopps 			warnx("bad line: %s", line);
    213   1.1       cgd 			retval = 1;
    214   1.1       cgd 			continue;
    215   1.1       cgd 		}
    216  1.44  christos 		if (set(i, argv))
    217   1.1       cgd 			retval = 1;
    218   1.1       cgd 	}
    219  1.44  christos 	(void)fclose(fp);
    220  1.44  christos 	return retval;
    221   1.1       cgd }
    222   1.1       cgd 
    223  1.44  christos static void
    224  1.41   xtraeme getsocket(void)
    225   1.8    chopps {
    226   1.8    chopps 	if (s >= 0)
    227   1.8    chopps 		return;
    228   1.8    chopps 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    229   1.8    chopps 	if (s < 0)
    230   1.8    chopps 		err(1, "socket");
    231   1.5   mycroft }
    232   1.5   mycroft 
    233   1.1       cgd /*
    234   1.1       cgd  * Set an individual arp entry
    235   1.1       cgd  */
    236  1.44  christos static int
    237  1.41   xtraeme set(int argc, char **argv)
    238   1.1       cgd {
    239  1.41   xtraeme 	struct sockaddr_inarp *sina;
    240  1.19     lukem 	struct sockaddr_dl *sdl;
    241  1.19     lukem 	struct rt_msghdr *rtm;
    242   1.8    chopps 	char *host = argv[0], *eaddr;
    243  1.23       mrg 	int rval;
    244   1.8    chopps 
    245  1.41   xtraeme 	sina = &sin_m;
    246   1.8    chopps 	rtm = &(m_rtmsg.m_rtm);
    247   1.8    chopps 	eaddr = argv[1];
    248   1.1       cgd 
    249   1.5   mycroft 	getsocket();
    250   1.1       cgd 	argc -= 2;
    251   1.1       cgd 	argv += 2;
    252   1.8    chopps 	sdl_m = blank_sdl;		/* struct copy */
    253   1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    254  1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    255   1.8    chopps 		return (1);
    256  1.17        is 	if (atosdl(eaddr, &sdl_m))
    257  1.17        is 		warnx("invalid link-level address '%s'", eaddr);
    258   1.5   mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    259   1.1       cgd 	while (argc-- > 0) {
    260   1.5   mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    261  1.41   xtraeme 			struct timeval timev;
    262  1.41   xtraeme 			(void)gettimeofday(&timev, 0);
    263  1.41   xtraeme 			expire_time = timev.tv_sec + 20 * 60;
    264   1.5   mycroft 		}
    265   1.5   mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    266   1.5   mycroft 			flags |= RTF_ANNOUNCE;
    267   1.5   mycroft 			doing_proxy = SIN_PROXY;
    268  1.46     seanb 			if (argc && strncmp(argv[1], "pro", 3) == 0) {
    269  1.46     seanb 			        export_only = 1;
    270  1.46     seanb 			        argc--; argv++;
    271  1.46     seanb 			}
    272   1.5   mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    273  1.44  christos 			warnx("%s: Sending trailers is no longer supported",
    274  1.44  christos 			    host);
    275   1.5   mycroft 		}
    276   1.1       cgd 		argv++;
    277   1.1       cgd 	}
    278   1.5   mycroft tryagain:
    279   1.5   mycroft 	if (rtmsg(RTM_GET) < 0) {
    280   1.8    chopps 		warn("%s", host);
    281   1.5   mycroft 		return (1);
    282   1.1       cgd 	}
    283  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    284  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    285  1.44  christos 	    (char *)(void *)sina);
    286  1.41   xtraeme 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    287  1.43    dyoung 		if (is_llinfo(sdl, rtm->rtm_flags))
    288   1.5   mycroft 			goto overwrite;
    289   1.5   mycroft 		if (doing_proxy == 0) {
    290  1.44  christos 			warnx("set: can only proxy for %s", host);
    291   1.5   mycroft 			return (1);
    292   1.5   mycroft 		}
    293   1.5   mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    294  1.44  christos 			warnx("set: proxy entry exists for non 802 device");
    295   1.8    chopps 			return (1);
    296   1.5   mycroft 		}
    297   1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    298   1.5   mycroft 		export_only = 1;
    299   1.5   mycroft 		goto tryagain;
    300   1.5   mycroft 	}
    301   1.5   mycroft overwrite:
    302   1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    303  1.44  christos 		warnx("cannot intuit interface index and type for %s",
    304   1.8    chopps 		    host);
    305   1.5   mycroft 		return (1);
    306   1.1       cgd 	}
    307   1.5   mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    308   1.5   mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    309  1.23       mrg 	rval = rtmsg(RTM_ADD);
    310  1.23       mrg 	if (vflag)
    311  1.23       mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    312  1.23       mrg 	return (rval);
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.1       cgd /*
    316   1.1       cgd  * Display an individual arp entry
    317   1.1       cgd  */
    318  1.44  christos static void
    319  1.41   xtraeme get(const char *host)
    320   1.1       cgd {
    321  1.41   xtraeme 	struct sockaddr_inarp *sina;
    322   1.1       cgd 
    323  1.41   xtraeme 	sina = &sin_m;
    324   1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    325  1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    326   1.8    chopps 		exit(1);
    327  1.41   xtraeme 	dump(sina->sin_addr.s_addr);
    328  1.44  christos 	if (found_entry == 0)
    329  1.44  christos 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sina->sin_addr));
    330   1.1       cgd }
    331   1.1       cgd 
    332  1.43    dyoung 
    333  1.43    dyoung static int
    334  1.43    dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
    335  1.43    dyoung {
    336  1.43    dyoung 	if (sdl->sdl_family != AF_LINK ||
    337  1.43    dyoung 	    (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
    338  1.43    dyoung 		return 0;
    339  1.43    dyoung 
    340  1.43    dyoung 	switch (sdl->sdl_type) {
    341  1.43    dyoung 	case IFT_ETHER:
    342  1.43    dyoung 	case IFT_FDDI:
    343  1.43    dyoung 	case IFT_ISO88023:
    344  1.43    dyoung 	case IFT_ISO88024:
    345  1.43    dyoung 	case IFT_ISO88025:
    346  1.43    dyoung 	case IFT_ARCNET:
    347  1.43    dyoung 		return 1;
    348  1.43    dyoung 	default:
    349  1.43    dyoung 		return 0;
    350  1.43    dyoung 	}
    351  1.43    dyoung }
    352  1.43    dyoung 
    353   1.1       cgd /*
    354   1.1       cgd  * Delete an arp entry
    355   1.1       cgd  */
    356   1.8    chopps int
    357  1.41   xtraeme delete(const char *host, const char *info)
    358   1.1       cgd {
    359  1.41   xtraeme 	struct sockaddr_inarp *sina;
    360  1.19     lukem 	struct rt_msghdr *rtm;
    361   1.5   mycroft 	struct sockaddr_dl *sdl;
    362   1.1       cgd 
    363  1.41   xtraeme 	sina = &sin_m;
    364   1.8    chopps 	rtm = &m_rtmsg.m_rtm;
    365   1.8    chopps 
    366   1.5   mycroft 	getsocket();
    367   1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    368  1.46     seanb 	if (info && strncmp(info, "pro", 3) == 0)
    369  1.46     seanb 		 sina->sin_other = SIN_PROXY;
    370  1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    371   1.8    chopps 		return (1);
    372   1.5   mycroft tryagain:
    373   1.5   mycroft 	if (rtmsg(RTM_GET) < 0) {
    374   1.8    chopps 		warn("%s", host);
    375   1.5   mycroft 		return (1);
    376   1.5   mycroft 	}
    377  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    378  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    379  1.44  christos 	    (char *)(void *)sina);
    380  1.43    dyoung 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
    381  1.43    dyoung 	    is_llinfo(sdl, rtm->rtm_flags))
    382  1.43    dyoung 		goto delete;
    383   1.5   mycroft 	if (sin_m.sin_other & SIN_PROXY) {
    384   1.8    chopps 		warnx("delete: can't locate %s", host);
    385   1.5   mycroft 		return (1);
    386   1.5   mycroft 	} else {
    387   1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    388   1.5   mycroft 		goto tryagain;
    389   1.5   mycroft 	}
    390   1.5   mycroft delete:
    391   1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    392  1.44  christos 		(void)warnx("cannot locate %s", host);
    393   1.5   mycroft 		return (1);
    394   1.1       cgd 	}
    395   1.8    chopps 	if (rtmsg(RTM_DELETE))
    396   1.8    chopps 		return (1);
    397  1.23       mrg 	if (vflag)
    398  1.23       mrg 		(void)printf("%s (%s) deleted\n", host,
    399  1.41   xtraeme 		    inet_ntoa(sina->sin_addr));
    400   1.8    chopps 	return (0);
    401   1.1       cgd }
    402   1.1       cgd 
    403   1.1       cgd /*
    404   1.1       cgd  * Dump the entire arp table
    405   1.1       cgd  */
    406   1.8    chopps void
    407  1.44  christos dump(uint32_t addr)
    408   1.1       cgd {
    409   1.5   mycroft 	int mib[6];
    410   1.5   mycroft 	size_t needed;
    411  1.29      fair 	char ifname[IFNAMSIZ];
    412  1.41   xtraeme 	char *lim, *buf, *next;
    413  1.41   xtraeme         const char *host;
    414   1.5   mycroft 	struct rt_msghdr *rtm;
    415  1.41   xtraeme 	struct sockaddr_inarp *sina;
    416   1.5   mycroft 	struct sockaddr_dl *sdl;
    417   1.1       cgd 	struct hostent *hp;
    418   1.1       cgd 
    419   1.5   mycroft 	mib[0] = CTL_NET;
    420   1.5   mycroft 	mib[1] = PF_ROUTE;
    421   1.5   mycroft 	mib[2] = 0;
    422   1.5   mycroft 	mib[3] = AF_INET;
    423   1.5   mycroft 	mib[4] = NET_RT_FLAGS;
    424   1.5   mycroft 	mib[5] = RTF_LLINFO;
    425   1.5   mycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    426   1.8    chopps 		err(1, "route-sysctl-estimate");
    427  1.22      fvdl 	if (needed == 0)
    428  1.22      fvdl 		return;
    429   1.5   mycroft 	if ((buf = malloc(needed)) == NULL)
    430   1.8    chopps 		err(1, "malloc");
    431   1.5   mycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    432   1.8    chopps 		err(1, "actual retrieval of routing table");
    433   1.5   mycroft 	lim = buf + needed;
    434   1.5   mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    435  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    436  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    437  1.44  christos 		sdl = (struct sockaddr_dl *)(void *)
    438  1.48  christos 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
    439   1.5   mycroft 		if (addr) {
    440  1.41   xtraeme 			if (addr != sina->sin_addr.s_addr)
    441   1.5   mycroft 				continue;
    442   1.5   mycroft 			found_entry = 1;
    443   1.5   mycroft 		}
    444   1.5   mycroft 		if (nflag == 0)
    445  1.44  christos 			hp = gethostbyaddr((const char *)(void *)
    446  1.44  christos 			    &(sina->sin_addr),
    447  1.41   xtraeme 			    sizeof sina->sin_addr, AF_INET);
    448   1.1       cgd 		else
    449  1.27  christos 			hp = NULL;
    450  1.27  christos 
    451  1.27  christos 		host = hp ? hp->h_name : "?";
    452  1.27  christos 
    453  1.41   xtraeme 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
    454   1.5   mycroft 		if (sdl->sdl_alen)
    455  1.17        is 			sdl_print(sdl);
    456   1.1       cgd 		else
    457   1.8    chopps 			(void)printf("(incomplete)");
    458  1.29      fair 
    459  1.29      fair 		if (sdl->sdl_index) {
    460  1.37    itojun 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
    461  1.44  christos 				(void)printf(" on %s", ifname);
    462  1.29      fair 		}
    463  1.29      fair 
    464   1.5   mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    465   1.8    chopps 			(void)printf(" permanent");
    466  1.41   xtraeme 		if (sina->sin_other & SIN_PROXY)
    467   1.8    chopps 			(void)printf(" published (proxy only)");
    468   1.5   mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    469  1.44  christos 			sina = (struct sockaddr_inarp *)(void *)
    470  1.48  christos 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
    471  1.41   xtraeme 			if (sina->sin_addr.s_addr == 0xffffffff)
    472   1.8    chopps 				(void)printf(" published");
    473  1.41   xtraeme 			if (sina->sin_len != 8)
    474  1.25       erh 				(void)printf("(weird)");
    475   1.5   mycroft 		}
    476   1.8    chopps 		(void)printf("\n");
    477   1.1       cgd 	}
    478  1.39    itojun 	free(buf);
    479   1.1       cgd }
    480   1.1       cgd 
    481  1.30    atatat /*
    482  1.30    atatat  * Delete the entire arp table
    483  1.30    atatat  */
    484  1.30    atatat void
    485  1.30    atatat delete_all(void)
    486  1.30    atatat {
    487  1.30    atatat 	int mib[6];
    488  1.30    atatat 	size_t needed;
    489  1.30    atatat 	char addr[sizeof("000.000.000.000\0")];
    490  1.30    atatat 	char *lim, *buf, *next;
    491  1.30    atatat 	struct rt_msghdr *rtm;
    492  1.41   xtraeme 	struct sockaddr_inarp *sina;
    493  1.30    atatat 
    494  1.30    atatat 	mib[0] = CTL_NET;
    495  1.30    atatat 	mib[1] = PF_ROUTE;
    496  1.30    atatat 	mib[2] = 0;
    497  1.30    atatat 	mib[3] = AF_INET;
    498  1.30    atatat 	mib[4] = NET_RT_FLAGS;
    499  1.30    atatat 	mib[5] = RTF_LLINFO;
    500  1.30    atatat 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    501  1.30    atatat 		err(1, "route-sysctl-estimate");
    502  1.30    atatat 	if (needed == 0)
    503  1.30    atatat 		return;
    504  1.30    atatat 	if ((buf = malloc(needed)) == NULL)
    505  1.30    atatat 		err(1, "malloc");
    506  1.30    atatat 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    507  1.30    atatat 		err(1, "actual retrieval of routing table");
    508  1.30    atatat 	lim = buf + needed;
    509  1.30    atatat 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    510  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    511  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    512  1.44  christos 		(void)snprintf(addr, sizeof(addr), "%s",
    513  1.44  christos 		    inet_ntoa(sina->sin_addr));
    514  1.44  christos 		(void)delete(addr, NULL);
    515  1.30    atatat 	}
    516  1.39    itojun 	free(buf);
    517  1.30    atatat }
    518  1.30    atatat 
    519   1.8    chopps void
    520  1.41   xtraeme sdl_print(const struct sockaddr_dl *sdl)
    521  1.17        is {
    522  1.32     bjh21 	char hbuf[NI_MAXHOST];
    523  1.17        is 
    524  1.44  christos 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
    525  1.44  christos 	    (socklen_t)sdl->sdl_len,
    526  1.32     bjh21 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    527  1.44  christos 		(void)printf("<invalid>");
    528  1.32     bjh21 	else
    529  1.44  christos 		(void)printf("%s", hbuf);
    530  1.17        is }
    531  1.17        is 
    532  1.44  christos static int
    533  1.41   xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
    534   1.1       cgd {
    535  1.17        is 	int i;
    536  1.44  christos 	unsigned long b;
    537  1.44  christos 	char *endp;
    538  1.44  christos 	char *p;
    539  1.17        is 	char *t, *r;
    540  1.17        is 
    541  1.17        is 	p = LLADDR(sdl);
    542  1.44  christos 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
    543  1.17        is 	i = 0;
    544  1.17        is 
    545  1.44  christos 	b = strtoul(ss, &t, 16);
    546  1.44  christos 	if (b > 255 || t == ss)
    547  1.17        is 		return 1;
    548  1.17        is 
    549  1.44  christos 	*p++ = (char)b;
    550  1.17        is 	++i;
    551  1.17        is 	while ((p < endp) && (*t++ == ':')) {
    552  1.44  christos 		b = strtoul(t, &r, 16);
    553  1.44  christos 		if (b > 255 || r == t)
    554  1.17        is 			break;
    555  1.44  christos 		*p++ = (char)b;
    556  1.17        is 		++i;
    557  1.17        is 		t = r;
    558  1.17        is 	}
    559  1.17        is 	sdl->sdl_alen = i;
    560  1.17        is 
    561  1.17        is 	return 0;
    562   1.1       cgd }
    563   1.1       cgd 
    564  1.44  christos static void
    565  1.41   xtraeme usage(void)
    566   1.1       cgd {
    567  1.33     pooka 	const char *progname;
    568  1.20     lukem 
    569  1.33     pooka 	progname = getprogname();
    570  1.44  christos 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
    571  1.44  christos 	(void)fprintf(stderr, "	      %s [-nv] -a\n", progname);
    572  1.46     seanb 	(void)fprintf(stderr, "	      %s [-v] -d [-a|hostname [pub [proxy]]]\n",
    573  1.44  christos 	    progname);
    574  1.46     seanb 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
    575  1.44  christos 	    progname);
    576  1.44  christos 	(void)fprintf(stderr, "       %s -f filename\n", progname);
    577   1.5   mycroft 	exit(1);
    578   1.5   mycroft }
    579   1.5   mycroft 
    580  1.44  christos static int
    581  1.41   xtraeme rtmsg(int cmd)
    582   1.5   mycroft {
    583   1.5   mycroft 	static int seq;
    584  1.19     lukem 	struct rt_msghdr *rtm;
    585  1.19     lukem 	char *cp;
    586  1.19     lukem 	int l;
    587   1.5   mycroft 
    588   1.8    chopps 	rtm = &m_rtmsg.m_rtm;
    589   1.8    chopps 	cp = m_rtmsg.m_space;
    590   1.5   mycroft 	errno = 0;
    591   1.8    chopps 
    592   1.5   mycroft 	if (cmd == RTM_DELETE)
    593   1.5   mycroft 		goto doit;
    594   1.8    chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    595   1.5   mycroft 	rtm->rtm_flags = flags;
    596   1.5   mycroft 	rtm->rtm_version = RTM_VERSION;
    597   1.5   mycroft 
    598   1.5   mycroft 	switch (cmd) {
    599   1.5   mycroft 	default:
    600   1.8    chopps 		errx(1, "internal wrong cmd");
    601   1.8    chopps 		/*NOTREACHED*/
    602   1.5   mycroft 	case RTM_ADD:
    603   1.5   mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    604   1.5   mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    605   1.5   mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    606   1.5   mycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    607   1.5   mycroft 		sin_m.sin_other = 0;
    608   1.5   mycroft 		if (doing_proxy) {
    609   1.5   mycroft 			if (export_only)
    610   1.5   mycroft 				sin_m.sin_other = SIN_PROXY;
    611   1.5   mycroft 			else {
    612   1.5   mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    613   1.5   mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    614   1.5   mycroft 			}
    615   1.5   mycroft 		}
    616   1.5   mycroft 		/* FALLTHROUGH */
    617   1.5   mycroft 	case RTM_GET:
    618   1.5   mycroft 		rtm->rtm_addrs |= RTA_DST;
    619   1.5   mycroft 	}
    620  1.25       erh 
    621   1.5   mycroft #define NEXTADDR(w, s) \
    622   1.5   mycroft 	if (rtm->rtm_addrs & (w)) { \
    623  1.44  christos 		(void)memcpy(cp, &s, \
    624  1.44  christos 		(size_t)((struct sockaddr *)(void *)&s)->sa_len); \
    625  1.48  christos 		RT_ADVANCE(cp, ((struct sockaddr *)(void *)&s)); \
    626  1.25       erh 	}
    627   1.5   mycroft 
    628   1.5   mycroft 	NEXTADDR(RTA_DST, sin_m);
    629   1.5   mycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
    630   1.5   mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    631   1.5   mycroft 
    632  1.44  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    633   1.5   mycroft doit:
    634   1.5   mycroft 	l = rtm->rtm_msglen;
    635   1.5   mycroft 	rtm->rtm_seq = ++seq;
    636   1.5   mycroft 	rtm->rtm_type = cmd;
    637  1.44  christos 	if (write(s, &m_rtmsg, (size_t)l) < 0) {
    638   1.5   mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    639   1.8    chopps 			warn("writing to routing socket");
    640   1.5   mycroft 			return (-1);
    641   1.5   mycroft 		}
    642   1.5   mycroft 	}
    643   1.5   mycroft 	do {
    644  1.44  christos 		l = read(s, &m_rtmsg, sizeof(m_rtmsg));
    645   1.5   mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    646   1.5   mycroft 	if (l < 0)
    647   1.8    chopps 		warn("read from routing socket");
    648   1.5   mycroft 	return (0);
    649   1.5   mycroft }
    650   1.5   mycroft 
    651  1.44  christos static int
    652  1.41   xtraeme getinetaddr(const char *host, struct in_addr *inap)
    653   1.5   mycroft {
    654   1.8    chopps 	struct hostent *hp;
    655   1.8    chopps 
    656   1.9    chopps 	if (inet_aton(host, inap) == 1)
    657   1.8    chopps 		return (0);
    658   1.8    chopps 	if ((hp = gethostbyname(host)) == NULL) {
    659  1.31       chs 		warnx("%s: %s", host, hstrerror(h_errno));
    660   1.8    chopps 		return (-1);
    661   1.8    chopps 	}
    662   1.8    chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    663   1.8    chopps 	return (0);
    664  1.29      fair }
    665  1.29      fair 
    666  1.44  christos static int
    667  1.41   xtraeme getifname(u_int16_t ifindex, char *ifname, size_t l)
    668  1.29      fair {
    669  1.35     rafal 	int i;
    670  1.35     rafal 	struct ifaddrs *addr;
    671  1.29      fair 	const struct sockaddr_dl *sdl = NULL;
    672  1.29      fair 
    673  1.35     rafal 	if (ifaddrs == NULL) {
    674  1.35     rafal 		i = getifaddrs(&ifaddrs);
    675  1.35     rafal 		if (i != 0)
    676  1.35     rafal 			err(1, "getifaddrs");
    677  1.29      fair 	}
    678  1.29      fair 
    679  1.35     rafal 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    680  1.35     rafal 		if (addr->ifa_addr == NULL ||
    681  1.35     rafal 		    addr->ifa_addr->sa_family != AF_LINK)
    682  1.29      fair 			continue;
    683  1.29      fair 
    684  1.44  christos 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
    685  1.29      fair 		if (sdl && sdl->sdl_index == ifindex) {
    686  1.37    itojun 			(void) strlcpy(ifname, addr->ifa_name, l);
    687  1.29      fair 			return 0;
    688  1.29      fair 		}
    689  1.29      fair 	}
    690  1.29      fair 
    691  1.29      fair 	return -1;
    692   1.1       cgd }
    693