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