Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.49.6.1
      1  1.49.6.1      yamt /*	$NetBSD: arp.c,v 1.49.6.1 2014/05/22 11:43:01 yamt 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.49.6.1      yamt __RCSID("$NetBSD: arp.c,v 1.49.6.1 2014/05/22 11:43:01 yamt 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.49.6.1      yamt 		if (argc < 2 || argc > 7)
    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.49  dholland 	if (!strcmp(name, "-")) {
    199      1.49  dholland 		fp = stdin;
    200      1.49  dholland 	} else {
    201      1.49  dholland 		fp = fopen(name, "r");
    202      1.49  dholland 		if (fp == NULL) {
    203      1.49  dholland 			err(1, "Cannot open %s", name);
    204      1.49  dholland 		}
    205      1.49  dholland 	}
    206       1.1       cgd 	retval = 0;
    207      1.44  christos 	for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
    208      1.44  christos 		char **ap, *inputstring;
    209      1.44  christos 
    210      1.44  christos 		inputstring = line;
    211      1.44  christos 		for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
    212      1.44  christos 		    (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
    213      1.44  christos 		       if (**ap != '\0')
    214      1.44  christos 				ap++;
    215      1.44  christos 		}
    216      1.44  christos 		i = ap - argv;
    217       1.1       cgd 		if (i < 2) {
    218       1.8    chopps 			warnx("bad line: %s", line);
    219       1.1       cgd 			retval = 1;
    220       1.1       cgd 			continue;
    221       1.1       cgd 		}
    222      1.44  christos 		if (set(i, argv))
    223       1.1       cgd 			retval = 1;
    224       1.1       cgd 	}
    225      1.49  dholland 	if (fp != stdin)
    226      1.49  dholland 		(void)fclose(fp);
    227      1.44  christos 	return retval;
    228       1.1       cgd }
    229       1.1       cgd 
    230      1.44  christos static void
    231      1.41   xtraeme getsocket(void)
    232       1.8    chopps {
    233       1.8    chopps 	if (s >= 0)
    234       1.8    chopps 		return;
    235       1.8    chopps 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    236       1.8    chopps 	if (s < 0)
    237       1.8    chopps 		err(1, "socket");
    238       1.5   mycroft }
    239       1.5   mycroft 
    240  1.49.6.1      yamt static int
    241  1.49.6.1      yamt getlink(const char *name, struct sockaddr_dl *sdl)
    242  1.49.6.1      yamt {
    243  1.49.6.1      yamt 	struct ifaddrs *ifap, *ifa;
    244  1.49.6.1      yamt 
    245  1.49.6.1      yamt 	if (getifaddrs(&ifap) != 0) {
    246  1.49.6.1      yamt 		warn("getifaddrs");
    247  1.49.6.1      yamt 		return 0;
    248  1.49.6.1      yamt 	}
    249  1.49.6.1      yamt 
    250  1.49.6.1      yamt 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    251  1.49.6.1      yamt 		if (ifa->ifa_addr->sa_family != AF_LINK)
    252  1.49.6.1      yamt 			continue;
    253  1.49.6.1      yamt 		if (strcmp(ifa->ifa_name, name) != 0)
    254  1.49.6.1      yamt 			continue;
    255  1.49.6.1      yamt 		memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
    256  1.49.6.1      yamt 		freeifaddrs(ifap);
    257  1.49.6.1      yamt 		return 1;
    258  1.49.6.1      yamt 	}
    259  1.49.6.1      yamt 	freeifaddrs(ifap);
    260  1.49.6.1      yamt 	return 0;
    261  1.49.6.1      yamt }
    262  1.49.6.1      yamt 
    263       1.1       cgd /*
    264       1.1       cgd  * Set an individual arp entry
    265       1.1       cgd  */
    266      1.44  christos static int
    267      1.41   xtraeme set(int argc, char **argv)
    268       1.1       cgd {
    269      1.41   xtraeme 	struct sockaddr_inarp *sina;
    270      1.19     lukem 	struct sockaddr_dl *sdl;
    271      1.19     lukem 	struct rt_msghdr *rtm;
    272       1.8    chopps 	char *host = argv[0], *eaddr;
    273      1.23       mrg 	int rval;
    274       1.8    chopps 
    275      1.41   xtraeme 	sina = &sin_m;
    276       1.8    chopps 	rtm = &(m_rtmsg.m_rtm);
    277       1.8    chopps 	eaddr = argv[1];
    278       1.1       cgd 
    279       1.5   mycroft 	getsocket();
    280       1.1       cgd 	argc -= 2;
    281       1.1       cgd 	argv += 2;
    282       1.8    chopps 	sdl_m = blank_sdl;		/* struct copy */
    283       1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    284      1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    285       1.8    chopps 		return (1);
    286      1.17        is 	if (atosdl(eaddr, &sdl_m))
    287      1.17        is 		warnx("invalid link-level address '%s'", eaddr);
    288       1.5   mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    289  1.49.6.1      yamt 	for (; argc-- > 0; argv++) {
    290       1.5   mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    291      1.41   xtraeme 			struct timeval timev;
    292      1.41   xtraeme 			(void)gettimeofday(&timev, 0);
    293      1.41   xtraeme 			expire_time = timev.tv_sec + 20 * 60;
    294       1.5   mycroft 		}
    295       1.5   mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    296       1.5   mycroft 			flags |= RTF_ANNOUNCE;
    297       1.5   mycroft 			doing_proxy = SIN_PROXY;
    298      1.46     seanb 			if (argc && strncmp(argv[1], "pro", 3) == 0) {
    299      1.46     seanb 			        export_only = 1;
    300      1.46     seanb 			        argc--; argv++;
    301      1.46     seanb 			}
    302       1.5   mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    303      1.44  christos 			warnx("%s: Sending trailers is no longer supported",
    304      1.44  christos 			    host);
    305  1.49.6.1      yamt 		} else if (strcmp(argv[0], "ifscope") == 0) {
    306  1.49.6.1      yamt 			if (argc == 0) {
    307  1.49.6.1      yamt 				warnx("missing interface for ifscope");
    308  1.49.6.1      yamt 				continue;
    309  1.49.6.1      yamt 			}
    310  1.49.6.1      yamt 			argc--;
    311  1.49.6.1      yamt 			argv++;
    312  1.49.6.1      yamt 			if (!getlink(argv[0], &sdl_m))
    313  1.49.6.1      yamt 				warnx("cannot get link address for %s", argv[0]);
    314       1.5   mycroft 		}
    315  1.49.6.1      yamt 
    316       1.1       cgd 	}
    317  1.49.6.1      yamt 	if (memcmp(&sdl_m, &blank_sdl, sizeof(blank_sdl)))
    318  1.49.6.1      yamt 		goto out;
    319       1.5   mycroft tryagain:
    320       1.5   mycroft 	if (rtmsg(RTM_GET) < 0) {
    321       1.8    chopps 		warn("%s", host);
    322       1.5   mycroft 		return (1);
    323       1.1       cgd 	}
    324      1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    325      1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    326      1.44  christos 	    (char *)(void *)sina);
    327      1.41   xtraeme 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    328      1.43    dyoung 		if (is_llinfo(sdl, rtm->rtm_flags))
    329       1.5   mycroft 			goto overwrite;
    330       1.5   mycroft 		if (doing_proxy == 0) {
    331      1.44  christos 			warnx("set: can only proxy for %s", host);
    332       1.5   mycroft 			return (1);
    333       1.5   mycroft 		}
    334       1.5   mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    335      1.44  christos 			warnx("set: proxy entry exists for non 802 device");
    336       1.8    chopps 			return (1);
    337       1.5   mycroft 		}
    338       1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    339       1.5   mycroft 		export_only = 1;
    340       1.5   mycroft 		goto tryagain;
    341       1.5   mycroft 	}
    342       1.5   mycroft overwrite:
    343       1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    344      1.44  christos 		warnx("cannot intuit interface index and type for %s",
    345       1.8    chopps 		    host);
    346       1.5   mycroft 		return (1);
    347       1.1       cgd 	}
    348       1.5   mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    349       1.5   mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    350  1.49.6.1      yamt out:
    351      1.23       mrg 	rval = rtmsg(RTM_ADD);
    352      1.23       mrg 	if (vflag)
    353      1.23       mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    354      1.23       mrg 	return (rval);
    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.41   xtraeme 	struct sockaddr_inarp *sina;
    364       1.1       cgd 
    365      1.41   xtraeme 	sina = &sin_m;
    366       1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    367      1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    368       1.8    chopps 		exit(1);
    369      1.41   xtraeme 	dump(sina->sin_addr.s_addr);
    370      1.44  christos 	if (found_entry == 0)
    371      1.44  christos 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sina->sin_addr));
    372       1.1       cgd }
    373       1.1       cgd 
    374      1.43    dyoung 
    375      1.43    dyoung static int
    376      1.43    dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
    377      1.43    dyoung {
    378      1.43    dyoung 	if (sdl->sdl_family != AF_LINK ||
    379      1.43    dyoung 	    (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
    380      1.43    dyoung 		return 0;
    381      1.43    dyoung 
    382      1.43    dyoung 	switch (sdl->sdl_type) {
    383      1.43    dyoung 	case IFT_ETHER:
    384      1.43    dyoung 	case IFT_FDDI:
    385      1.43    dyoung 	case IFT_ISO88023:
    386      1.43    dyoung 	case IFT_ISO88024:
    387      1.43    dyoung 	case IFT_ISO88025:
    388      1.43    dyoung 	case IFT_ARCNET:
    389      1.43    dyoung 		return 1;
    390      1.43    dyoung 	default:
    391      1.43    dyoung 		return 0;
    392      1.43    dyoung 	}
    393      1.43    dyoung }
    394      1.43    dyoung 
    395       1.1       cgd /*
    396       1.1       cgd  * Delete an arp entry
    397       1.1       cgd  */
    398       1.8    chopps int
    399      1.41   xtraeme delete(const char *host, const char *info)
    400       1.1       cgd {
    401      1.41   xtraeme 	struct sockaddr_inarp *sina;
    402      1.19     lukem 	struct rt_msghdr *rtm;
    403       1.5   mycroft 	struct sockaddr_dl *sdl;
    404       1.1       cgd 
    405      1.41   xtraeme 	sina = &sin_m;
    406       1.8    chopps 	rtm = &m_rtmsg.m_rtm;
    407       1.8    chopps 
    408       1.5   mycroft 	getsocket();
    409       1.8    chopps 	sin_m = blank_sin;		/* struct copy */
    410      1.46     seanb 	if (info && strncmp(info, "pro", 3) == 0)
    411      1.46     seanb 		 sina->sin_other = SIN_PROXY;
    412      1.41   xtraeme 	if (getinetaddr(host, &sina->sin_addr) == -1)
    413       1.8    chopps 		return (1);
    414       1.5   mycroft tryagain:
    415       1.5   mycroft 	if (rtmsg(RTM_GET) < 0) {
    416       1.8    chopps 		warn("%s", host);
    417       1.5   mycroft 		return (1);
    418       1.5   mycroft 	}
    419      1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    420      1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    421      1.44  christos 	    (char *)(void *)sina);
    422      1.43    dyoung 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
    423      1.43    dyoung 	    is_llinfo(sdl, rtm->rtm_flags))
    424      1.43    dyoung 		goto delete;
    425       1.5   mycroft 	if (sin_m.sin_other & SIN_PROXY) {
    426       1.8    chopps 		warnx("delete: can't locate %s", host);
    427       1.5   mycroft 		return (1);
    428       1.5   mycroft 	} else {
    429       1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    430       1.5   mycroft 		goto tryagain;
    431       1.5   mycroft 	}
    432       1.5   mycroft delete:
    433       1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    434      1.44  christos 		(void)warnx("cannot locate %s", host);
    435       1.5   mycroft 		return (1);
    436       1.1       cgd 	}
    437       1.8    chopps 	if (rtmsg(RTM_DELETE))
    438       1.8    chopps 		return (1);
    439      1.23       mrg 	if (vflag)
    440      1.23       mrg 		(void)printf("%s (%s) deleted\n", host,
    441      1.41   xtraeme 		    inet_ntoa(sina->sin_addr));
    442       1.8    chopps 	return (0);
    443       1.1       cgd }
    444       1.1       cgd 
    445       1.1       cgd /*
    446       1.1       cgd  * Dump the entire arp table
    447       1.1       cgd  */
    448       1.8    chopps void
    449      1.44  christos dump(uint32_t addr)
    450       1.1       cgd {
    451       1.5   mycroft 	int mib[6];
    452       1.5   mycroft 	size_t needed;
    453      1.29      fair 	char ifname[IFNAMSIZ];
    454      1.41   xtraeme 	char *lim, *buf, *next;
    455      1.41   xtraeme         const char *host;
    456       1.5   mycroft 	struct rt_msghdr *rtm;
    457      1.41   xtraeme 	struct sockaddr_inarp *sina;
    458       1.5   mycroft 	struct sockaddr_dl *sdl;
    459       1.1       cgd 	struct hostent *hp;
    460       1.1       cgd 
    461       1.5   mycroft 	mib[0] = CTL_NET;
    462       1.5   mycroft 	mib[1] = PF_ROUTE;
    463       1.5   mycroft 	mib[2] = 0;
    464       1.5   mycroft 	mib[3] = AF_INET;
    465       1.5   mycroft 	mib[4] = NET_RT_FLAGS;
    466       1.5   mycroft 	mib[5] = RTF_LLINFO;
    467       1.5   mycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    468       1.8    chopps 		err(1, "route-sysctl-estimate");
    469      1.22      fvdl 	if (needed == 0)
    470      1.22      fvdl 		return;
    471       1.5   mycroft 	if ((buf = malloc(needed)) == NULL)
    472       1.8    chopps 		err(1, "malloc");
    473       1.5   mycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    474       1.8    chopps 		err(1, "actual retrieval of routing table");
    475       1.5   mycroft 	lim = buf + needed;
    476       1.5   mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    477      1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    478      1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    479      1.44  christos 		sdl = (struct sockaddr_dl *)(void *)
    480      1.48  christos 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
    481       1.5   mycroft 		if (addr) {
    482      1.41   xtraeme 			if (addr != sina->sin_addr.s_addr)
    483       1.5   mycroft 				continue;
    484       1.5   mycroft 			found_entry = 1;
    485       1.5   mycroft 		}
    486       1.5   mycroft 		if (nflag == 0)
    487      1.44  christos 			hp = gethostbyaddr((const char *)(void *)
    488      1.44  christos 			    &(sina->sin_addr),
    489      1.41   xtraeme 			    sizeof sina->sin_addr, AF_INET);
    490       1.1       cgd 		else
    491      1.27  christos 			hp = NULL;
    492      1.27  christos 
    493      1.27  christos 		host = hp ? hp->h_name : "?";
    494      1.27  christos 
    495      1.41   xtraeme 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
    496       1.5   mycroft 		if (sdl->sdl_alen)
    497      1.17        is 			sdl_print(sdl);
    498       1.1       cgd 		else
    499       1.8    chopps 			(void)printf("(incomplete)");
    500      1.29      fair 
    501      1.29      fair 		if (sdl->sdl_index) {
    502      1.37    itojun 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
    503      1.44  christos 				(void)printf(" on %s", ifname);
    504      1.29      fair 		}
    505      1.29      fair 
    506       1.5   mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    507       1.8    chopps 			(void)printf(" permanent");
    508      1.41   xtraeme 		if (sina->sin_other & SIN_PROXY)
    509       1.8    chopps 			(void)printf(" published (proxy only)");
    510       1.5   mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    511      1.44  christos 			sina = (struct sockaddr_inarp *)(void *)
    512      1.48  christos 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
    513      1.41   xtraeme 			if (sina->sin_addr.s_addr == 0xffffffff)
    514       1.8    chopps 				(void)printf(" published");
    515      1.41   xtraeme 			if (sina->sin_len != 8)
    516      1.25       erh 				(void)printf("(weird)");
    517       1.5   mycroft 		}
    518       1.8    chopps 		(void)printf("\n");
    519       1.1       cgd 	}
    520      1.39    itojun 	free(buf);
    521       1.1       cgd }
    522       1.1       cgd 
    523      1.30    atatat /*
    524      1.30    atatat  * Delete the entire arp table
    525      1.30    atatat  */
    526      1.30    atatat void
    527      1.30    atatat delete_all(void)
    528      1.30    atatat {
    529      1.30    atatat 	int mib[6];
    530      1.30    atatat 	size_t needed;
    531      1.30    atatat 	char addr[sizeof("000.000.000.000\0")];
    532      1.30    atatat 	char *lim, *buf, *next;
    533      1.30    atatat 	struct rt_msghdr *rtm;
    534      1.41   xtraeme 	struct sockaddr_inarp *sina;
    535      1.30    atatat 
    536      1.30    atatat 	mib[0] = CTL_NET;
    537      1.30    atatat 	mib[1] = PF_ROUTE;
    538      1.30    atatat 	mib[2] = 0;
    539      1.30    atatat 	mib[3] = AF_INET;
    540      1.30    atatat 	mib[4] = NET_RT_FLAGS;
    541      1.30    atatat 	mib[5] = RTF_LLINFO;
    542      1.30    atatat 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    543      1.30    atatat 		err(1, "route-sysctl-estimate");
    544      1.30    atatat 	if (needed == 0)
    545      1.30    atatat 		return;
    546      1.30    atatat 	if ((buf = malloc(needed)) == NULL)
    547      1.30    atatat 		err(1, "malloc");
    548      1.30    atatat 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    549      1.30    atatat 		err(1, "actual retrieval of routing table");
    550      1.30    atatat 	lim = buf + needed;
    551      1.30    atatat 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    552      1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    553      1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    554      1.44  christos 		(void)snprintf(addr, sizeof(addr), "%s",
    555      1.44  christos 		    inet_ntoa(sina->sin_addr));
    556      1.44  christos 		(void)delete(addr, NULL);
    557      1.30    atatat 	}
    558      1.39    itojun 	free(buf);
    559      1.30    atatat }
    560      1.30    atatat 
    561       1.8    chopps void
    562      1.41   xtraeme sdl_print(const struct sockaddr_dl *sdl)
    563      1.17        is {
    564      1.32     bjh21 	char hbuf[NI_MAXHOST];
    565      1.17        is 
    566      1.44  christos 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
    567      1.44  christos 	    (socklen_t)sdl->sdl_len,
    568      1.32     bjh21 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    569      1.44  christos 		(void)printf("<invalid>");
    570      1.32     bjh21 	else
    571      1.44  christos 		(void)printf("%s", hbuf);
    572      1.17        is }
    573      1.17        is 
    574      1.44  christos static int
    575      1.41   xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
    576       1.1       cgd {
    577      1.17        is 	int i;
    578      1.44  christos 	unsigned long b;
    579      1.44  christos 	char *endp;
    580      1.44  christos 	char *p;
    581      1.17        is 	char *t, *r;
    582      1.17        is 
    583      1.17        is 	p = LLADDR(sdl);
    584      1.44  christos 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
    585      1.17        is 	i = 0;
    586      1.17        is 
    587      1.44  christos 	b = strtoul(ss, &t, 16);
    588      1.44  christos 	if (b > 255 || t == ss)
    589      1.17        is 		return 1;
    590      1.17        is 
    591      1.44  christos 	*p++ = (char)b;
    592      1.17        is 	++i;
    593      1.17        is 	while ((p < endp) && (*t++ == ':')) {
    594      1.44  christos 		b = strtoul(t, &r, 16);
    595      1.44  christos 		if (b > 255 || r == t)
    596      1.17        is 			break;
    597      1.44  christos 		*p++ = (char)b;
    598      1.17        is 		++i;
    599      1.17        is 		t = r;
    600      1.17        is 	}
    601      1.17        is 	sdl->sdl_alen = i;
    602      1.17        is 
    603      1.17        is 	return 0;
    604       1.1       cgd }
    605       1.1       cgd 
    606      1.44  christos static void
    607      1.41   xtraeme usage(void)
    608       1.1       cgd {
    609      1.33     pooka 	const char *progname;
    610      1.20     lukem 
    611      1.33     pooka 	progname = getprogname();
    612      1.44  christos 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
    613      1.44  christos 	(void)fprintf(stderr, "	      %s [-nv] -a\n", progname);
    614      1.46     seanb 	(void)fprintf(stderr, "	      %s [-v] -d [-a|hostname [pub [proxy]]]\n",
    615      1.44  christos 	    progname);
    616      1.46     seanb 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
    617      1.44  christos 	    progname);
    618      1.44  christos 	(void)fprintf(stderr, "       %s -f filename\n", progname);
    619       1.5   mycroft 	exit(1);
    620       1.5   mycroft }
    621       1.5   mycroft 
    622      1.44  christos static int
    623      1.41   xtraeme rtmsg(int cmd)
    624       1.5   mycroft {
    625       1.5   mycroft 	static int seq;
    626      1.19     lukem 	struct rt_msghdr *rtm;
    627      1.19     lukem 	char *cp;
    628      1.19     lukem 	int l;
    629       1.5   mycroft 
    630       1.8    chopps 	rtm = &m_rtmsg.m_rtm;
    631       1.8    chopps 	cp = m_rtmsg.m_space;
    632       1.5   mycroft 	errno = 0;
    633       1.8    chopps 
    634       1.5   mycroft 	if (cmd == RTM_DELETE)
    635       1.5   mycroft 		goto doit;
    636       1.8    chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    637       1.5   mycroft 	rtm->rtm_flags = flags;
    638       1.5   mycroft 	rtm->rtm_version = RTM_VERSION;
    639       1.5   mycroft 
    640       1.5   mycroft 	switch (cmd) {
    641       1.5   mycroft 	default:
    642       1.8    chopps 		errx(1, "internal wrong cmd");
    643       1.8    chopps 		/*NOTREACHED*/
    644       1.5   mycroft 	case RTM_ADD:
    645       1.5   mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    646       1.5   mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    647       1.5   mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    648       1.5   mycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    649       1.5   mycroft 		sin_m.sin_other = 0;
    650       1.5   mycroft 		if (doing_proxy) {
    651       1.5   mycroft 			if (export_only)
    652       1.5   mycroft 				sin_m.sin_other = SIN_PROXY;
    653       1.5   mycroft 			else {
    654       1.5   mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    655       1.5   mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    656       1.5   mycroft 			}
    657       1.5   mycroft 		}
    658       1.5   mycroft 		/* FALLTHROUGH */
    659       1.5   mycroft 	case RTM_GET:
    660       1.5   mycroft 		rtm->rtm_addrs |= RTA_DST;
    661       1.5   mycroft 	}
    662      1.25       erh 
    663       1.5   mycroft #define NEXTADDR(w, s) \
    664       1.5   mycroft 	if (rtm->rtm_addrs & (w)) { \
    665      1.44  christos 		(void)memcpy(cp, &s, \
    666      1.44  christos 		(size_t)((struct sockaddr *)(void *)&s)->sa_len); \
    667      1.48  christos 		RT_ADVANCE(cp, ((struct sockaddr *)(void *)&s)); \
    668      1.25       erh 	}
    669       1.5   mycroft 
    670       1.5   mycroft 	NEXTADDR(RTA_DST, sin_m);
    671       1.5   mycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
    672       1.5   mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    673       1.5   mycroft 
    674      1.44  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    675       1.5   mycroft doit:
    676       1.5   mycroft 	l = rtm->rtm_msglen;
    677       1.5   mycroft 	rtm->rtm_seq = ++seq;
    678       1.5   mycroft 	rtm->rtm_type = cmd;
    679      1.44  christos 	if (write(s, &m_rtmsg, (size_t)l) < 0) {
    680       1.5   mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    681       1.8    chopps 			warn("writing to routing socket");
    682       1.5   mycroft 			return (-1);
    683       1.5   mycroft 		}
    684       1.5   mycroft 	}
    685       1.5   mycroft 	do {
    686      1.44  christos 		l = 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.5   mycroft 	return (0);
    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.29      fair 	const struct sockaddr_dl *sdl = NULL;
    714      1.29      fair 
    715      1.35     rafal 	if (ifaddrs == NULL) {
    716      1.35     rafal 		i = getifaddrs(&ifaddrs);
    717      1.35     rafal 		if (i != 0)
    718      1.35     rafal 			err(1, "getifaddrs");
    719      1.29      fair 	}
    720      1.29      fair 
    721      1.35     rafal 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    722      1.35     rafal 		if (addr->ifa_addr == NULL ||
    723      1.35     rafal 		    addr->ifa_addr->sa_family != AF_LINK)
    724      1.29      fair 			continue;
    725      1.29      fair 
    726      1.44  christos 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
    727      1.29      fair 		if (sdl && sdl->sdl_index == ifindex) {
    728      1.37    itojun 			(void) strlcpy(ifname, addr->ifa_name, l);
    729      1.29      fair 			return 0;
    730      1.29      fair 		}
    731      1.29      fair 	}
    732      1.29      fair 
    733      1.29      fair 	return -1;
    734       1.1       cgd }
    735