Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.21
      1 /*	$NetBSD: arp.c,v 1.21 1997/11/16 11:25:08 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.21 1997/11/16 11:25:08 is Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 /*
     54  * arp - display, set, and delete arp table entries
     55  */
     56 
     57 #include <sys/param.h>
     58 #include <sys/file.h>
     59 #include <sys/socket.h>
     60 #include <sys/sysctl.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_dl.h>
     64 #include <net/if_ether.h>
     65 #include <net/if_types.h>
     66 #include <net/route.h>
     67 #include <netinet/in.h>
     68 #include <netinet/if_inarp.h>
     69 #include <arpa/inet.h>
     70 
     71 #include <err.h>
     72 #include <errno.h>
     73 #include <netdb.h>
     74 #include <nlist.h>
     75 #include <paths.h>
     76 #include <stdio.h>
     77 #include <stdlib.h>
     78 #include <string.h>
     79 #include <unistd.h>
     80 
     81 int	delete __P((const char *, const char *));
     82 void	dump __P((u_long));
     83 void	sdl_print __P((const struct sockaddr_dl *));
     84 int	atosdl __P((const char *s, struct sockaddr_dl *sdl));
     85 int	file __P((char *));
     86 void	get __P((const char *));
     87 int	getinetaddr __P((const char *, struct in_addr *));
     88 void	getsocket __P((void));
     89 int	main __P((int, char **));
     90 int	rtmsg __P((int));
     91 int	set __P((int, char **));
     92 void	usage __P((void));
     93 
     94 static int pid;
     95 static int nflag;
     96 static int s = -1;
     97 
     98 int	delete __P((const char *, const char *));
     99 void	dump __P((u_long));
    100 void	ether_print __P((const u_char *));
    101 int	file __P((char *));
    102 void	get __P((const char *));
    103 int	getinetaddr __P((const char *, struct in_addr *));
    104 void	getsocket __P((void));
    105 int	rtmsg __P((int));
    106 int	set __P((int, char **));
    107 void	usage __P((void));
    108 
    109 int
    110 main(argc, argv)
    111 	int argc;
    112 	char **argv;
    113 {
    114 	int ch;
    115 	int op = 0;
    116 
    117 	pid = getpid();
    118 
    119 	while ((ch = getopt(argc, argv, "andsf")) != -1)
    120 		switch((char)ch) {
    121 		case 'a':
    122 		case 'd':
    123 		case 's':
    124 		case 'f':
    125 			if (op)
    126 				usage();
    127 			op = ch;
    128 			break;
    129 		case 'n':
    130 			nflag = 1;
    131 			break;
    132 		default:
    133 			usage();
    134 		}
    135 	argc -= optind;
    136 	argv += optind;
    137 
    138 	switch((char)op) {
    139 	case 'a':
    140 		dump(0);
    141 		break;
    142 	case 'd':
    143 		if (argc < 1 || argc > 2)
    144 			usage();
    145 		(void)delete(argv[0], argv[1]);
    146 		break;
    147 	case 's':
    148 		if (argc < 2 || argc > 5)
    149 			usage();
    150 		return (set(argc, argv) ? 1 : 0);
    151 	case 'f':
    152 		if (argc != 1)
    153 			usage();
    154 		return (file(argv[0]));
    155 	default:
    156 		if (argc != 1)
    157 			usage();
    158 		get(argv[0]);
    159 		break;
    160 	}
    161 	return (0);
    162 }
    163 
    164 /*
    165  * Process a file to set standard arp entries
    166  */
    167 int
    168 file(name)
    169 	char *name;
    170 {
    171 	char line[100], arg[5][50], *args[5];
    172 	int i, retval;
    173 	FILE *fp;
    174 
    175 	if ((fp = fopen(name, "r")) == NULL)
    176 		err(1, "cannot open %s", name);
    177 	args[0] = &arg[0][0];
    178 	args[1] = &arg[1][0];
    179 	args[2] = &arg[2][0];
    180 	args[3] = &arg[3][0];
    181 	args[4] = &arg[4][0];
    182 	retval = 0;
    183 	while (fgets(line, 100, fp) != NULL) {
    184 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    185 		    arg[3], arg[4]);
    186 		if (i < 2) {
    187 			warnx("bad line: %s", line);
    188 			retval = 1;
    189 			continue;
    190 		}
    191 		if (set(i, args))
    192 			retval = 1;
    193 	}
    194 	fclose(fp);
    195 	return (retval);
    196 }
    197 
    198 void
    199 getsocket()
    200 {
    201 	if (s >= 0)
    202 		return;
    203 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    204 	if (s < 0)
    205 		err(1, "socket");
    206 }
    207 
    208 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    209 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    210 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    211 int	expire_time, flags, export_only, doing_proxy, found_entry;
    212 struct	{
    213 	struct	rt_msghdr m_rtm;
    214 	char	m_space[512];
    215 }	m_rtmsg;
    216 
    217 /*
    218  * Set an individual arp entry
    219  */
    220 int
    221 set(argc, argv)
    222 	int argc;
    223 	char **argv;
    224 {
    225 	struct sockaddr_inarp *sin;
    226 	struct sockaddr_dl *sdl;
    227 	struct rt_msghdr *rtm;
    228 	char *host = argv[0], *eaddr;
    229 
    230 	sin = &sin_m;
    231 	rtm = &(m_rtmsg.m_rtm);
    232 	eaddr = argv[1];
    233 
    234 	getsocket();
    235 	argc -= 2;
    236 	argv += 2;
    237 	sdl_m = blank_sdl;		/* struct copy */
    238 	sin_m = blank_sin;		/* struct copy */
    239 	if (getinetaddr(host, &sin->sin_addr) == -1)
    240 		return (1);
    241 	if (atosdl(eaddr, &sdl_m))
    242 		warnx("invalid link-level address '%s'", eaddr);
    243 	doing_proxy = flags = export_only = expire_time = 0;
    244 	while (argc-- > 0) {
    245 		if (strncmp(argv[0], "temp", 4) == 0) {
    246 			struct timeval time;
    247 			(void)gettimeofday(&time, 0);
    248 			expire_time = time.tv_sec + 20 * 60;
    249 		}
    250 		else if (strncmp(argv[0], "pub", 3) == 0) {
    251 			flags |= RTF_ANNOUNCE;
    252 			doing_proxy = SIN_PROXY;
    253 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    254 			(void)printf(
    255 			    "%s: Sending trailers is no longer supported\n",
    256 			     host);
    257 		}
    258 		argv++;
    259 	}
    260 tryagain:
    261 	if (rtmsg(RTM_GET) < 0) {
    262 		warn("%s", host);
    263 		return (1);
    264 	}
    265 	sin = (struct sockaddr_inarp *)(rtm + 1);
    266 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    267 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    268 		if (sdl->sdl_family == AF_LINK &&
    269 		    (rtm->rtm_flags & RTF_LLINFO) &&
    270 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    271 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    272 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    273 			goto overwrite;
    274 		}
    275 		if (doing_proxy == 0) {
    276 			(void)printf("set: can only proxy for %s\n", host);
    277 			return (1);
    278 		}
    279 		if (sin_m.sin_other & SIN_PROXY) {
    280 			(void)printf(
    281 			    "set: proxy entry exists for non 802 device\n");
    282 			return (1);
    283 		}
    284 		sin_m.sin_other = SIN_PROXY;
    285 		export_only = 1;
    286 		goto tryagain;
    287 	}
    288 overwrite:
    289 	if (sdl->sdl_family != AF_LINK) {
    290 		(void)printf("cannot intuit interface index and type for %s\n",
    291 		    host);
    292 		return (1);
    293 	}
    294 	sdl_m.sdl_type = sdl->sdl_type;
    295 	sdl_m.sdl_index = sdl->sdl_index;
    296 	return (rtmsg(RTM_ADD));
    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 *)(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 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
    372 	return (0);
    373 }
    374 
    375 /*
    376  * Dump the entire arp table
    377  */
    378 void
    379 dump(addr)
    380 	u_long addr;
    381 {
    382 	int mib[6];
    383 	size_t needed;
    384 	char *host, *lim, *buf, *next;
    385 	struct rt_msghdr *rtm;
    386 	struct sockaddr_inarp *sin;
    387 	struct sockaddr_dl *sdl;
    388 	extern int h_errno;
    389 	struct hostent *hp;
    390 
    391 	mib[0] = CTL_NET;
    392 	mib[1] = PF_ROUTE;
    393 	mib[2] = 0;
    394 	mib[3] = AF_INET;
    395 	mib[4] = NET_RT_FLAGS;
    396 	mib[5] = RTF_LLINFO;
    397 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    398 		err(1, "route-sysctl-estimate");
    399 	if ((buf = malloc(needed)) == NULL)
    400 		err(1, "malloc");
    401 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    402 		err(1, "actual retrieval of routing table");
    403 	lim = buf + needed;
    404 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    405 		rtm = (struct rt_msghdr *)next;
    406 		sin = (struct sockaddr_inarp *)(rtm + 1);
    407 		sdl = (struct sockaddr_dl *)(sin + 1);
    408 		if (addr) {
    409 			if (addr != sin->sin_addr.s_addr)
    410 				continue;
    411 			found_entry = 1;
    412 		}
    413 		if (nflag == 0)
    414 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    415 			    sizeof sin->sin_addr, AF_INET);
    416 		else
    417 			hp = 0;
    418 		if (hp)
    419 			host = hp->h_name;
    420 		else {
    421 			host = "?";
    422 			if (h_errno == TRY_AGAIN)
    423 				nflag = 1;
    424 		}
    425 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    426 		if (sdl->sdl_alen)
    427 			sdl_print(sdl);
    428 		else
    429 			(void)printf("(incomplete)");
    430 		if (rtm->rtm_rmx.rmx_expire == 0)
    431 			(void)printf(" permanent");
    432 		if (sin->sin_other & SIN_PROXY)
    433 			(void)printf(" published (proxy only)");
    434 		if (rtm->rtm_addrs & RTA_NETMASK) {
    435 			sin = (struct sockaddr_inarp *)
    436 				(sdl->sdl_len + (char *)sdl);
    437 			if (sin->sin_addr.s_addr == 0xffffffff)
    438 				(void)printf(" published");
    439 			if (sin->sin_len != 8)
    440 				(void)printf("(wierd)");
    441 		}
    442 		(void)printf("\n");
    443 	}
    444 }
    445 
    446 void
    447 sdl_print(sdl)
    448 	const struct sockaddr_dl *sdl;
    449 {
    450 	int i;
    451 	u_int8_t *p;
    452 	const char *hexfmt = "%x";
    453 
    454 	if (sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_FDDI)
    455 		hexfmt = "%02x";
    456 
    457 	i = sdl->sdl_alen;
    458 	p = LLADDR(sdl);
    459 
    460 	(void)printf(hexfmt, *p);
    461 	while (--i > 0) {
    462 		putchar(':');
    463 		(void)printf(hexfmt, *++p);
    464 	}
    465 }
    466 
    467 int
    468 atosdl(s, sdl)
    469 	const char *s;
    470 	struct sockaddr_dl *sdl;
    471 {
    472 	int i;
    473 	long b;
    474 	caddr_t endp;
    475 	caddr_t p;
    476 	char *t, *r;
    477 
    478 	p = LLADDR(sdl);
    479 	endp = ((caddr_t)sdl) + sdl->sdl_len;
    480 	i = 0;
    481 
    482 	b = strtol(s, &t, 16);
    483 	if (t == s)
    484 		return 1;
    485 
    486 	*p++ = b;
    487 	++i;
    488 	while ((p < endp) && (*t++ == ':')) {
    489 		b = strtol(t, &r, 16);
    490 		if (r == t)
    491 			break;
    492 		*p++ = b;
    493 		++i;
    494 		t = r;
    495 	}
    496 	sdl->sdl_alen = i;
    497 
    498 	return 0;
    499 }
    500 
    501 void
    502 usage()
    503 {
    504 	extern char *__progname;
    505 
    506 	(void)fprintf(stderr, "usage: %s [-n] hostname\n", __progname);
    507 	(void)fprintf(stderr, "usage: %s [-n] -a\n", __progname);
    508 	(void)fprintf(stderr, "usage: %s -d hostname\n", __progname);
    509 	(void)fprintf(stderr,
    510 	    "usage: %s -s hostname ether_addr [temp] [pub]\n", __progname);
    511 	(void)fprintf(stderr, "usage: %s -f filename\n", __progname);
    512 	exit(1);
    513 }
    514 
    515 int
    516 rtmsg(cmd)
    517 	int cmd;
    518 {
    519 	static int seq;
    520 	int rlen;
    521 	struct rt_msghdr *rtm;
    522 	char *cp;
    523 	int l;
    524 
    525 	rtm = &m_rtmsg.m_rtm;
    526 	cp = m_rtmsg.m_space;
    527 	errno = 0;
    528 
    529 	if (cmd == RTM_DELETE)
    530 		goto doit;
    531 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    532 	rtm->rtm_flags = flags;
    533 	rtm->rtm_version = RTM_VERSION;
    534 
    535 	switch (cmd) {
    536 	default:
    537 		errx(1, "internal wrong cmd");
    538 		/*NOTREACHED*/
    539 	case RTM_ADD:
    540 		rtm->rtm_addrs |= RTA_GATEWAY;
    541 		rtm->rtm_rmx.rmx_expire = expire_time;
    542 		rtm->rtm_inits = RTV_EXPIRE;
    543 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    544 		sin_m.sin_other = 0;
    545 		if (doing_proxy) {
    546 			if (export_only)
    547 				sin_m.sin_other = SIN_PROXY;
    548 			else {
    549 				rtm->rtm_addrs |= RTA_NETMASK;
    550 				rtm->rtm_flags &= ~RTF_HOST;
    551 			}
    552 		}
    553 		/* FALLTHROUGH */
    554 	case RTM_GET:
    555 		rtm->rtm_addrs |= RTA_DST;
    556 	}
    557 #define NEXTADDR(w, s) \
    558 	if (rtm->rtm_addrs & (w)) { \
    559 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    560 
    561 	NEXTADDR(RTA_DST, sin_m);
    562 	NEXTADDR(RTA_GATEWAY, sdl_m);
    563 	NEXTADDR(RTA_NETMASK, so_mask);
    564 
    565 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    566 doit:
    567 	l = rtm->rtm_msglen;
    568 	rtm->rtm_seq = ++seq;
    569 	rtm->rtm_type = cmd;
    570 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    571 		if (errno != ESRCH || cmd != RTM_DELETE) {
    572 			warn("writing to routing socket");
    573 			return (-1);
    574 		}
    575 	}
    576 	do {
    577 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    578 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    579 	if (l < 0)
    580 		warn("read from routing socket");
    581 	return (0);
    582 }
    583 
    584 int
    585 getinetaddr(host, inap)
    586 	const char *host;
    587 	struct in_addr *inap;
    588 {
    589 	struct hostent *hp;
    590 
    591 	if (inet_aton(host, inap) == 1)
    592 		return (0);
    593 	if ((hp = gethostbyname(host)) == NULL) {
    594 		warnx("%s: %s\n", host, hstrerror(h_errno));
    595 		return (-1);
    596 	}
    597 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    598 	return (0);
    599 }
    600