Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.26
      1 /*	$NetBSD: arp.c,v 1.26 2000/10/11 20:23:48 is Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1984, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Sun Microsystems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\n\
     42 	The Regents of the University of California.  All rights reserved.\n");
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     48 #else
     49 __RCSID("$NetBSD: arp.c,v 1.26 2000/10/11 20:23:48 is Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 /*
     54  * arp - display, set, and delete arp table entries
     55  */
     56 
     57 /* Roundup the same way rt_xaddrs does */
     58 #define ROUNDUP(a) \
     59 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     60 
     61 #include <sys/param.h>
     62 #include <sys/file.h>
     63 #include <sys/socket.h>
     64 #include <sys/sysctl.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_ether.h>
     69 #include <net/if_types.h>
     70 #include <net/route.h>
     71 #include <netinet/in.h>
     72 #include <netinet/if_inarp.h>
     73 #include <arpa/inet.h>
     74 
     75 #include <err.h>
     76 #include <errno.h>
     77 #include <netdb.h>
     78 #include <nlist.h>
     79 #include <paths.h>
     80 #include <stdio.h>
     81 #include <stdlib.h>
     82 #include <string.h>
     83 #include <unistd.h>
     84 
     85 int	delete __P((const char *, const char *));
     86 void	dump __P((u_long));
     87 void	sdl_print __P((const struct sockaddr_dl *));
     88 int	atosdl __P((const char *s, struct sockaddr_dl *sdl));
     89 int	file __P((char *));
     90 void	get __P((const char *));
     91 int	getinetaddr __P((const char *, struct in_addr *));
     92 void	getsocket __P((void));
     93 int	main __P((int, char **));
     94 int	rtmsg __P((int));
     95 int	set __P((int, char **));
     96 void	usage __P((void));
     97 
     98 static int pid;
     99 static int nflag, vflag;
    100 static int s = -1;
    101 
    102 int
    103 main(argc, argv)
    104 	int argc;
    105 	char **argv;
    106 {
    107 	int ch;
    108 	int op = 0;
    109 
    110 	pid = getpid();
    111 
    112 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    113 		switch((char)ch) {
    114 		case 'a':
    115 		case 'd':
    116 		case 's':
    117 		case 'f':
    118 			if (op)
    119 				usage();
    120 			op = ch;
    121 			break;
    122 		case 'n':
    123 			nflag = 1;
    124 			break;
    125 		case 'v':
    126 			vflag = 1;
    127 			break;
    128 		default:
    129 			usage();
    130 		}
    131 	argc -= optind;
    132 	argv += optind;
    133 
    134 	switch((char)op) {
    135 	case 'a':
    136 		dump(0);
    137 		break;
    138 	case 'd':
    139 		if (argc < 1 || argc > 2)
    140 			usage();
    141 		(void)delete(argv[0], argv[1]);
    142 		break;
    143 	case 's':
    144 		if (argc < 2 || argc > 5)
    145 			usage();
    146 		return (set(argc, argv) ? 1 : 0);
    147 	case 'f':
    148 		if (argc != 1)
    149 			usage();
    150 		return (file(argv[0]));
    151 	default:
    152 		if (argc != 1)
    153 			usage();
    154 		get(argv[0]);
    155 		break;
    156 	}
    157 	return (0);
    158 }
    159 
    160 /*
    161  * Process a file to set standard arp entries
    162  */
    163 int
    164 file(name)
    165 	char *name;
    166 {
    167 	char line[100], arg[5][50], *args[5];
    168 	int i, retval;
    169 	FILE *fp;
    170 
    171 	if ((fp = fopen(name, "r")) == NULL)
    172 		err(1, "cannot open %s", name);
    173 	args[0] = &arg[0][0];
    174 	args[1] = &arg[1][0];
    175 	args[2] = &arg[2][0];
    176 	args[3] = &arg[3][0];
    177 	args[4] = &arg[4][0];
    178 	retval = 0;
    179 	while (fgets(line, 100, fp) != NULL) {
    180 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    181 		    arg[3], arg[4]);
    182 		if (i < 2) {
    183 			warnx("bad line: %s", line);
    184 			retval = 1;
    185 			continue;
    186 		}
    187 		if (set(i, args))
    188 			retval = 1;
    189 	}
    190 	fclose(fp);
    191 	return (retval);
    192 }
    193 
    194 void
    195 getsocket()
    196 {
    197 	if (s >= 0)
    198 		return;
    199 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    200 	if (s < 0)
    201 		err(1, "socket");
    202 }
    203 
    204 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    205 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    206 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    207 int	expire_time, flags, export_only, doing_proxy, found_entry;
    208 struct	{
    209 	struct	rt_msghdr m_rtm;
    210 	char	m_space[512];
    211 }	m_rtmsg;
    212 
    213 /*
    214  * Set an individual arp entry
    215  */
    216 int
    217 set(argc, argv)
    218 	int argc;
    219 	char **argv;
    220 {
    221 	struct sockaddr_inarp *sin;
    222 	struct sockaddr_dl *sdl;
    223 	struct rt_msghdr *rtm;
    224 	char *host = argv[0], *eaddr;
    225 	int rval;
    226 
    227 	sin = &sin_m;
    228 	rtm = &(m_rtmsg.m_rtm);
    229 	eaddr = argv[1];
    230 
    231 	getsocket();
    232 	argc -= 2;
    233 	argv += 2;
    234 	sdl_m = blank_sdl;		/* struct copy */
    235 	sin_m = blank_sin;		/* struct copy */
    236 	if (getinetaddr(host, &sin->sin_addr) == -1)
    237 		return (1);
    238 	if (atosdl(eaddr, &sdl_m))
    239 		warnx("invalid link-level address '%s'", eaddr);
    240 	doing_proxy = flags = export_only = expire_time = 0;
    241 	while (argc-- > 0) {
    242 		if (strncmp(argv[0], "temp", 4) == 0) {
    243 			struct timeval time;
    244 			(void)gettimeofday(&time, 0);
    245 			expire_time = time.tv_sec + 20 * 60;
    246 		}
    247 		else if (strncmp(argv[0], "pub", 3) == 0) {
    248 			flags |= RTF_ANNOUNCE;
    249 			doing_proxy = SIN_PROXY;
    250 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    251 			(void)printf(
    252 			    "%s: Sending trailers is no longer supported\n",
    253 			     host);
    254 		}
    255 		argv++;
    256 	}
    257 tryagain:
    258 	if (rtmsg(RTM_GET) < 0) {
    259 		warn("%s", host);
    260 		return (1);
    261 	}
    262 	sin = (struct sockaddr_inarp *)(rtm + 1);
    263 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
    264 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    265 		if (sdl->sdl_family == AF_LINK &&
    266 		    (rtm->rtm_flags & RTF_LLINFO) &&
    267 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    268 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    269 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    270 			goto overwrite;
    271 		}
    272 		if (doing_proxy == 0) {
    273 			(void)printf("set: can only proxy for %s\n", host);
    274 			return (1);
    275 		}
    276 		if (sin_m.sin_other & SIN_PROXY) {
    277 			(void)printf(
    278 			    "set: proxy entry exists for non 802 device\n");
    279 			return (1);
    280 		}
    281 		sin_m.sin_other = SIN_PROXY;
    282 		export_only = 1;
    283 		goto tryagain;
    284 	}
    285 overwrite:
    286 	if (sdl->sdl_family != AF_LINK) {
    287 		(void)printf("cannot intuit interface index and type for %s\n",
    288 		    host);
    289 		return (1);
    290 	}
    291 	sdl_m.sdl_type = sdl->sdl_type;
    292 	sdl_m.sdl_index = sdl->sdl_index;
    293 	rval = rtmsg(RTM_ADD);
    294 	if (vflag)
    295 		(void)printf("%s (%s) added\n", host, eaddr);
    296 	return (rval);
    297 }
    298 
    299 /*
    300  * Display an individual arp entry
    301  */
    302 void
    303 get(host)
    304 	const char *host;
    305 {
    306 	struct sockaddr_inarp *sin;
    307 
    308 	sin = &sin_m;
    309 	sin_m = blank_sin;		/* struct copy */
    310 	if (getinetaddr(host, &sin->sin_addr) == -1)
    311 		exit(1);
    312 	dump(sin->sin_addr.s_addr);
    313 	if (found_entry == 0) {
    314 		(void)printf("%s (%s) -- no entry\n", host,
    315 		    inet_ntoa(sin->sin_addr));
    316 		exit(1);
    317 	}
    318 }
    319 
    320 /*
    321  * Delete an arp entry
    322  */
    323 int
    324 delete(host, info)
    325 	const char *host;
    326 	const char *info;
    327 {
    328 	struct sockaddr_inarp *sin;
    329 	struct rt_msghdr *rtm;
    330 	struct sockaddr_dl *sdl;
    331 
    332 	sin = &sin_m;
    333 	rtm = &m_rtmsg.m_rtm;
    334 
    335 	if (info && strncmp(info, "pro", 3) )
    336 		export_only = 1;
    337 	getsocket();
    338 	sin_m = blank_sin;		/* struct copy */
    339 	if (getinetaddr(host, &sin->sin_addr) == -1)
    340 		return (1);
    341 tryagain:
    342 	if (rtmsg(RTM_GET) < 0) {
    343 		warn("%s", host);
    344 		return (1);
    345 	}
    346 	sin = (struct sockaddr_inarp *)(rtm + 1);
    347 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
    348 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    349 		if (sdl->sdl_family == AF_LINK &&
    350 		    (rtm->rtm_flags & RTF_LLINFO) &&
    351 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    352 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    353 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    354 			goto delete;
    355 		}
    356 	}
    357 	if (sin_m.sin_other & SIN_PROXY) {
    358 		warnx("delete: can't locate %s", host);
    359 		return (1);
    360 	} else {
    361 		sin_m.sin_other = SIN_PROXY;
    362 		goto tryagain;
    363 	}
    364 delete:
    365 	if (sdl->sdl_family != AF_LINK) {
    366 		(void)printf("cannot locate %s\n", host);
    367 		return (1);
    368 	}
    369 	if (rtmsg(RTM_DELETE))
    370 		return (1);
    371 	if (vflag)
    372 		(void)printf("%s (%s) deleted\n", host,
    373 		    inet_ntoa(sin->sin_addr));
    374 	return (0);
    375 }
    376 
    377 /*
    378  * Dump the entire arp table
    379  */
    380 void
    381 dump(addr)
    382 	u_long addr;
    383 {
    384 	int mib[6];
    385 	size_t needed;
    386 	char *host, *lim, *buf, *next;
    387 	struct rt_msghdr *rtm;
    388 	struct sockaddr_inarp *sin;
    389 	struct sockaddr_dl *sdl;
    390 	extern int h_errno;
    391 	struct hostent *hp;
    392 
    393 	mib[0] = CTL_NET;
    394 	mib[1] = PF_ROUTE;
    395 	mib[2] = 0;
    396 	mib[3] = AF_INET;
    397 	mib[4] = NET_RT_FLAGS;
    398 	mib[5] = RTF_LLINFO;
    399 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    400 		err(1, "route-sysctl-estimate");
    401 	if (needed == 0)
    402 		return;
    403 	if ((buf = malloc(needed)) == NULL)
    404 		err(1, "malloc");
    405 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    406 		err(1, "actual retrieval of routing table");
    407 	lim = buf + needed;
    408 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    409 		rtm = (struct rt_msghdr *)next;
    410 		sin = (struct sockaddr_inarp *)(rtm + 1);
    411 		sdl = (struct sockaddr_dl *)
    412 		    (ROUNDUP(sin->sin_len) + (char *)sin);
    413 		if (addr) {
    414 			if (addr != sin->sin_addr.s_addr)
    415 				continue;
    416 			found_entry = 1;
    417 		}
    418 		if (nflag == 0)
    419 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    420 			    sizeof sin->sin_addr, AF_INET);
    421 		else
    422 			hp = 0;
    423 		if (hp)
    424 			host = hp->h_name;
    425 		else {
    426 			host = "?";
    427 			if (h_errno == TRY_AGAIN)
    428 				nflag = 1;
    429 		}
    430 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    431 		if (sdl->sdl_alen)
    432 			sdl_print(sdl);
    433 		else
    434 			(void)printf("(incomplete)");
    435 		if (rtm->rtm_rmx.rmx_expire == 0)
    436 			(void)printf(" permanent");
    437 		if (sin->sin_other & SIN_PROXY)
    438 			(void)printf(" published (proxy only)");
    439 		if (rtm->rtm_addrs & RTA_NETMASK) {
    440 			sin = (struct sockaddr_inarp *)
    441 				(ROUNDUP(sdl->sdl_len) + (char *)sdl);
    442 			if (sin->sin_addr.s_addr == 0xffffffff)
    443 				(void)printf(" published");
    444 			if (sin->sin_len != 8)
    445 				(void)printf("(weird)");
    446 		}
    447 		(void)printf("\n");
    448 	}
    449 }
    450 
    451 void
    452 sdl_print(sdl)
    453 	const struct sockaddr_dl *sdl;
    454 {
    455 	int i;
    456 	u_int8_t *p;
    457 
    458 	i = sdl->sdl_alen;
    459 	p = LLADDR(sdl);
    460 
    461 	(void)printf("%02x", *p);
    462 	while (--i > 0) {
    463 		putchar(':');
    464 		(void)printf("%02x", *++p);
    465 	}
    466 }
    467 
    468 int
    469 atosdl(s, sdl)
    470 	const char *s;
    471 	struct sockaddr_dl *sdl;
    472 {
    473 	int i;
    474 	long b;
    475 	caddr_t endp;
    476 	caddr_t p;
    477 	char *t, *r;
    478 
    479 	p = LLADDR(sdl);
    480 	endp = ((caddr_t)sdl) + sdl->sdl_len;
    481 	i = 0;
    482 
    483 	b = strtol(s, &t, 16);
    484 	if (t == s)
    485 		return 1;
    486 
    487 	*p++ = b;
    488 	++i;
    489 	while ((p < endp) && (*t++ == ':')) {
    490 		b = strtol(t, &r, 16);
    491 		if (r == t)
    492 			break;
    493 		*p++ = b;
    494 		++i;
    495 		t = r;
    496 	}
    497 	sdl->sdl_alen = i;
    498 
    499 	return 0;
    500 }
    501 
    502 void
    503 usage()
    504 {
    505 	extern char *__progname;
    506 
    507 	(void)fprintf(stderr, "usage: %s [-n] hostname\n", __progname);
    508 	(void)fprintf(stderr, "usage: %s [-n] -a\n", __progname);
    509 	(void)fprintf(stderr, "usage: %s -d hostname\n", __progname);
    510 	(void)fprintf(stderr,
    511 	    "usage: %s -s hostname ether_addr [temp] [pub]\n", __progname);
    512 	(void)fprintf(stderr, "usage: %s -f filename\n", __progname);
    513 	exit(1);
    514 }
    515 
    516 int
    517 rtmsg(cmd)
    518 	int cmd;
    519 {
    520 	static int seq;
    521 	int rlen;
    522 	struct rt_msghdr *rtm;
    523 	char *cp;
    524 	int l;
    525 
    526 	rtm = &m_rtmsg.m_rtm;
    527 	cp = m_rtmsg.m_space;
    528 	errno = 0;
    529 
    530 	if (cmd == RTM_DELETE)
    531 		goto doit;
    532 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    533 	rtm->rtm_flags = flags;
    534 	rtm->rtm_version = RTM_VERSION;
    535 
    536 	switch (cmd) {
    537 	default:
    538 		errx(1, "internal wrong cmd");
    539 		/*NOTREACHED*/
    540 	case RTM_ADD:
    541 		rtm->rtm_addrs |= RTA_GATEWAY;
    542 		rtm->rtm_rmx.rmx_expire = expire_time;
    543 		rtm->rtm_inits = RTV_EXPIRE;
    544 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    545 		sin_m.sin_other = 0;
    546 		if (doing_proxy) {
    547 			if (export_only)
    548 				sin_m.sin_other = SIN_PROXY;
    549 			else {
    550 				rtm->rtm_addrs |= RTA_NETMASK;
    551 				rtm->rtm_flags &= ~RTF_HOST;
    552 			}
    553 		}
    554 		/* FALLTHROUGH */
    555 	case RTM_GET:
    556 		rtm->rtm_addrs |= RTA_DST;
    557 	}
    558 
    559 #define NEXTADDR(w, s) \
    560 	if (rtm->rtm_addrs & (w)) { \
    561 		(void)memcpy(cp, &s, ((struct sockaddr *)&s)->sa_len); \
    562 		cp += ROUNDUP(((struct sockaddr *)&s)->sa_len); \
    563 	}
    564 
    565 	NEXTADDR(RTA_DST, sin_m);
    566 	NEXTADDR(RTA_GATEWAY, sdl_m);
    567 	NEXTADDR(RTA_NETMASK, so_mask);
    568 
    569 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    570 doit:
    571 	l = rtm->rtm_msglen;
    572 	rtm->rtm_seq = ++seq;
    573 	rtm->rtm_type = cmd;
    574 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    575 		if (errno != ESRCH || cmd != RTM_DELETE) {
    576 			warn("writing to routing socket");
    577 			return (-1);
    578 		}
    579 	}
    580 	do {
    581 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    582 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    583 	if (l < 0)
    584 		warn("read from routing socket");
    585 	return (0);
    586 }
    587 
    588 int
    589 getinetaddr(host, inap)
    590 	const char *host;
    591 	struct in_addr *inap;
    592 {
    593 	struct hostent *hp;
    594 
    595 	if (inet_aton(host, inap) == 1)
    596 		return (0);
    597 	if ((hp = gethostbyname(host)) == NULL) {
    598 		warnx("%s: %s\n", host, hstrerror(h_errno));
    599 		return (-1);
    600 	}
    601 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    602 	return (0);
    603 }
    604