Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.18
      1 /*	$NetBSD: arp.c,v 1.18 1997/03/26 01:49:44 thorpej 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 #ifndef lint
     40 static char copyright[] =
     41 "@(#) 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 /* static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95"; */
     47 static char *rcsid = "$NetBSD: arp.c,v 1.18 1997/03/26 01:49:44 thorpej Exp $";
     48 #endif /* not lint */
     49 
     50 /*
     51  * arp - display, set, and delete arp table entries
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/file.h>
     56 #include <sys/socket.h>
     57 #include <sys/sysctl.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_types.h>
     63 #include <net/route.h>
     64 #include <netinet/in.h>
     65 #include <netinet/if_inarp.h>
     66 #include <arpa/inet.h>
     67 
     68 #include <err.h>
     69 #include <errno.h>
     70 #include <netdb.h>
     71 #include <nlist.h>
     72 #include <paths.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <unistd.h>
     77 
     78 int delete __P((const char *, const char *));
     79 void dump __P((u_long));
     80 void sdl_print __P((const struct sockaddr_dl *));
     81 int atosdl __P((const char *s, struct sockaddr_dl *sdl));
     82 int file __P((char *));
     83 void get __P((const char *));
     84 int getinetaddr __P((const char *, struct in_addr *));
     85 void getsocket __P((void));
     86 int rtmsg __P((int));
     87 int set __P((int, char **));
     88 void usage __P((void));
     89 
     90 static int pid;
     91 static int nflag;
     92 static int s = -1;
     93 
     94 int	delete __P((const char *, const char *));
     95 void	dump __P((u_long));
     96 void	ether_print __P((const u_char *));
     97 int	file __P((char *));
     98 void	get __P((const char *));
     99 int	getinetaddr __P((const char *, struct in_addr *));
    100 void	getsocket __P((void));
    101 int	rtmsg __P((int));
    102 int	set __P((int, char **));
    103 void	usage __P((void));
    104 
    105 int
    106 main(argc, argv)
    107 	int argc;
    108 	char **argv;
    109 {
    110 	int ch;
    111 	int op = 0;
    112 
    113 	pid = getpid();
    114 
    115 	while ((ch = getopt(argc, argv, "andsf")) != -1)
    116 		switch((char)ch) {
    117 		case 'a':
    118 		case 'd':
    119 		case 's':
    120 		case 'f':
    121 			if (op)
    122 				usage();
    123 			op = ch;
    124 			break;
    125 		case 'n':
    126 			nflag = 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 	register struct sockaddr_inarp *sin;
    222 	register struct sockaddr_dl *sdl;
    223 	register struct rt_msghdr *rtm;
    224 	struct ether_addr *ea;
    225 	char *host = argv[0], *eaddr;
    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 *)(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:
    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 	return (rtmsg(RTM_ADD));
    294 }
    295 
    296 /*
    297  * Display an individual arp entry
    298  */
    299 void
    300 get(host)
    301 	const char *host;
    302 {
    303 	struct sockaddr_inarp *sin;
    304 
    305 	sin = &sin_m;
    306 	sin_m = blank_sin;		/* struct copy */
    307 	if (getinetaddr(host, &sin->sin_addr) == -1)
    308 		exit(1);
    309 	dump(sin->sin_addr.s_addr);
    310 	if (found_entry == 0) {
    311 		(void)printf("%s (%s) -- no entry\n", host,
    312 		    inet_ntoa(sin->sin_addr));
    313 		exit(1);
    314 	}
    315 }
    316 
    317 /*
    318  * Delete an arp entry
    319  */
    320 int
    321 delete(host, info)
    322 	const char *host;
    323 	const char *info;
    324 {
    325 	register struct sockaddr_inarp *sin;
    326 	register struct rt_msghdr *rtm;
    327 	struct sockaddr_dl *sdl;
    328 
    329 	sin = &sin_m;
    330 	rtm = &m_rtmsg.m_rtm;
    331 
    332 	if (info && strncmp(info, "pro", 3) )
    333 		export_only = 1;
    334 	getsocket();
    335 	sin_m = blank_sin;		/* struct copy */
    336 	if (getinetaddr(host, &sin->sin_addr) == -1)
    337 		return (1);
    338 tryagain:
    339 	if (rtmsg(RTM_GET) < 0) {
    340 		warn("%s", host);
    341 		return (1);
    342 	}
    343 	sin = (struct sockaddr_inarp *)(rtm + 1);
    344 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    345 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    346 		if (sdl->sdl_family == AF_LINK &&
    347 		    (rtm->rtm_flags & RTF_LLINFO) &&
    348 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    349 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    350 		case IFT_ISO88024: case IFT_ISO88025:
    351 			goto delete;
    352 		}
    353 	}
    354 	if (sin_m.sin_other & SIN_PROXY) {
    355 		warnx("delete: can't locate %s", host);
    356 		return (1);
    357 	} else {
    358 		sin_m.sin_other = SIN_PROXY;
    359 		goto tryagain;
    360 	}
    361 delete:
    362 	if (sdl->sdl_family != AF_LINK) {
    363 		(void)printf("cannot locate %s\n", host);
    364 		return (1);
    365 	}
    366 	if (rtmsg(RTM_DELETE))
    367 		return (1);
    368 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
    369 	return (0);
    370 }
    371 
    372 /*
    373  * Dump the entire arp table
    374  */
    375 void
    376 dump(addr)
    377 	u_long addr;
    378 {
    379 	int mib[6];
    380 	size_t needed;
    381 	char *host, *lim, *buf, *next;
    382 	struct rt_msghdr *rtm;
    383 	struct sockaddr_inarp *sin;
    384 	struct sockaddr_dl *sdl;
    385 	extern int h_errno;
    386 	struct hostent *hp;
    387 
    388 	mib[0] = CTL_NET;
    389 	mib[1] = PF_ROUTE;
    390 	mib[2] = 0;
    391 	mib[3] = AF_INET;
    392 	mib[4] = NET_RT_FLAGS;
    393 	mib[5] = RTF_LLINFO;
    394 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    395 		err(1, "route-sysctl-estimate");
    396 	if ((buf = malloc(needed)) == NULL)
    397 		err(1, "malloc");
    398 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    399 		err(1, "actual retrieval of routing table");
    400 	lim = buf + needed;
    401 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    402 		rtm = (struct rt_msghdr *)next;
    403 		sin = (struct sockaddr_inarp *)(rtm + 1);
    404 		sdl = (struct sockaddr_dl *)(sin + 1);
    405 		if (addr) {
    406 			if (addr != sin->sin_addr.s_addr)
    407 				continue;
    408 			found_entry = 1;
    409 		}
    410 		if (nflag == 0)
    411 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    412 			    sizeof sin->sin_addr, AF_INET);
    413 		else
    414 			hp = 0;
    415 		if (hp)
    416 			host = hp->h_name;
    417 		else {
    418 			host = "?";
    419 			if (h_errno == TRY_AGAIN)
    420 				nflag = 1;
    421 		}
    422 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    423 		if (sdl->sdl_alen)
    424 			sdl_print(sdl);
    425 		else
    426 			(void)printf("(incomplete)");
    427 		if (rtm->rtm_rmx.rmx_expire == 0)
    428 			(void)printf(" permanent");
    429 		if (sin->sin_other & SIN_PROXY)
    430 			(void)printf(" published (proxy only)");
    431 		if (rtm->rtm_addrs & RTA_NETMASK) {
    432 			sin = (struct sockaddr_inarp *)
    433 				(sdl->sdl_len + (char *)sdl);
    434 			if (sin->sin_addr.s_addr == 0xffffffff)
    435 				(void)printf(" published");
    436 			if (sin->sin_len != 8)
    437 				(void)printf("(wierd)");
    438 		}
    439 		(void)printf("\n");
    440 	}
    441 }
    442 
    443 void
    444 sdl_print(sdl)
    445 	const struct sockaddr_dl *sdl;
    446 {
    447 	int i;
    448 	u_int8_t *p;
    449 	const char *hexfmt = "%x";
    450 
    451 	if (sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_FDDI)
    452 		hexfmt = "%02x";
    453 
    454 	i = sdl->sdl_alen;
    455 	p = LLADDR(sdl);
    456 
    457 	(void)printf(hexfmt, *p);
    458 	while (--i > 0) {
    459 		putchar(':');
    460 		(void)printf(hexfmt, *++p);
    461 	}
    462 }
    463 
    464 int
    465 atosdl(s, sdl)
    466 	const char *s;
    467 	struct sockaddr_dl *sdl;
    468 {
    469 	int i;
    470 	long b;
    471 	caddr_t endp;
    472 	caddr_t p;
    473 	char *t, *r;
    474 
    475 	p = LLADDR(sdl);
    476 	endp = ((caddr_t)sdl) + sdl->sdl_len;
    477 	i = 0;
    478 
    479 	b = strtol(s, &t, 16);
    480 	if (t == s)
    481 		return 1;
    482 
    483 	*p++ = b;
    484 	++i;
    485 	while ((p < endp) && (*t++ == ':')) {
    486 		b = strtol(t, &r, 16);
    487 		if (r == t)
    488 			break;
    489 		*p++ = b;
    490 		++i;
    491 		t = r;
    492 	}
    493 	sdl->sdl_alen = i;
    494 
    495 	return 0;
    496 }
    497 
    498 void
    499 usage()
    500 {
    501 	(void)fprintf(stderr, "usage: arp [-n] hostname\n");
    502 	(void)fprintf(stderr, "usage: arp [-n] -a\n");
    503 	(void)fprintf(stderr, "usage: arp -d hostname\n");
    504 	(void)fprintf(stderr,
    505 	    "usage: arp -s hostname ether_addr [temp] [pub]\n");
    506 	(void)fprintf(stderr, "usage: arp -f filename\n");
    507 	exit(1);
    508 }
    509 
    510 int
    511 rtmsg(cmd)
    512 	int cmd;
    513 {
    514 	static int seq;
    515 	int rlen;
    516 	register struct rt_msghdr *rtm;
    517 	register char *cp;
    518 	register int l;
    519 
    520 	rtm = &m_rtmsg.m_rtm;
    521 	cp = m_rtmsg.m_space;
    522 	errno = 0;
    523 
    524 	if (cmd == RTM_DELETE)
    525 		goto doit;
    526 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    527 	rtm->rtm_flags = flags;
    528 	rtm->rtm_version = RTM_VERSION;
    529 
    530 	switch (cmd) {
    531 	default:
    532 		errx(1, "internal wrong cmd");
    533 		/*NOTREACHED*/
    534 	case RTM_ADD:
    535 		rtm->rtm_addrs |= RTA_GATEWAY;
    536 		rtm->rtm_rmx.rmx_expire = expire_time;
    537 		rtm->rtm_inits = RTV_EXPIRE;
    538 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    539 		sin_m.sin_other = 0;
    540 		if (doing_proxy) {
    541 			if (export_only)
    542 				sin_m.sin_other = SIN_PROXY;
    543 			else {
    544 				rtm->rtm_addrs |= RTA_NETMASK;
    545 				rtm->rtm_flags &= ~RTF_HOST;
    546 			}
    547 		}
    548 		/* FALLTHROUGH */
    549 	case RTM_GET:
    550 		rtm->rtm_addrs |= RTA_DST;
    551 	}
    552 #define NEXTADDR(w, s) \
    553 	if (rtm->rtm_addrs & (w)) { \
    554 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    555 
    556 	NEXTADDR(RTA_DST, sin_m);
    557 	NEXTADDR(RTA_GATEWAY, sdl_m);
    558 	NEXTADDR(RTA_NETMASK, so_mask);
    559 
    560 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    561 doit:
    562 	l = rtm->rtm_msglen;
    563 	rtm->rtm_seq = ++seq;
    564 	rtm->rtm_type = cmd;
    565 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    566 		if (errno != ESRCH || cmd != RTM_DELETE) {
    567 			warn("writing to routing socket");
    568 			return (-1);
    569 		}
    570 	}
    571 	do {
    572 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    573 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    574 	if (l < 0)
    575 		warn("read from routing socket");
    576 	return (0);
    577 }
    578 
    579 int
    580 getinetaddr(host, inap)
    581 	const char *host;
    582 	struct in_addr *inap;
    583 {
    584 	struct hostent *hp;
    585 
    586 	if (inet_aton(host, inap) == 1)
    587 		return (0);
    588 	if ((hp = gethostbyname(host)) == NULL) {
    589 		warnx("%s: %s\n", host, hstrerror(h_errno));
    590 		return (-1);
    591 	}
    592 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    593 	return (0);
    594 }
    595