Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.16
      1 /*	$NetBSD: arp.c,v 1.16 1997/03/07 07:04:43 mikel 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.16 1997/03/07 07:04:43 mikel 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_types.h>
     62 #include <net/route.h>
     63 #include <netinet/in.h>
     64 #include <netinet/if_ether.h>
     65 #include <arpa/inet.h>
     66 
     67 #include <err.h>
     68 #include <errno.h>
     69 #include <netdb.h>
     70 #include <nlist.h>
     71 #include <paths.h>
     72 #include <stdio.h>
     73 #include <stdlib.h>
     74 #include <string.h>
     75 #include <unistd.h>
     76 
     77 static int pid;
     78 static int nflag;
     79 static int s = -1;
     80 
     81 int	delete __P((const char *, const char *));
     82 void	dump __P((u_long));
     83 void	ether_print __P((const u_char *));
     84 int	file __P((char *));
     85 void	get __P((const char *));
     86 int	getinetaddr __P((const char *, struct in_addr *));
     87 void	getsocket __P((void));
     88 int	rtmsg __P((int));
     89 int	set __P((int, char **));
     90 void	usage __P((void));
     91 
     92 int
     93 main(argc, argv)
     94 	int argc;
     95 	char **argv;
     96 {
     97 	int ch;
     98 	int op = 0;
     99 
    100 	pid = getpid();
    101 
    102 	while ((ch = getopt(argc, argv, "andsf")) != -1)
    103 		switch((char)ch) {
    104 		case 'a':
    105 		case 'd':
    106 		case 's':
    107 		case 'f':
    108 			if (op)
    109 				usage();
    110 			op = ch;
    111 			break;
    112 		case 'n':
    113 			nflag = 1;
    114 			break;
    115 		default:
    116 			usage();
    117 		}
    118 	argc -= optind;
    119 	argv += optind;
    120 
    121 	switch((char)op) {
    122 	case 'a':
    123 		dump(0);
    124 		break;
    125 	case 'd':
    126 		if (argc < 1 || argc > 2)
    127 			usage();
    128 		(void)delete(argv[0], argv[1]);
    129 		break;
    130 	case 's':
    131 		if (argc < 2 || argc > 5)
    132 			usage();
    133 		return (set(argc, argv) ? 1 : 0);
    134 	case 'f':
    135 		if (argc != 1)
    136 			usage();
    137 		return (file(argv[0]));
    138 	default:
    139 		if (argc != 1)
    140 			usage();
    141 		get(argv[0]);
    142 		break;
    143 	}
    144 	return (0);
    145 }
    146 
    147 /*
    148  * Process a file to set standard arp entries
    149  */
    150 int
    151 file(name)
    152 	char *name;
    153 {
    154 	char line[100], arg[5][50], *args[5];
    155 	int i, retval;
    156 	FILE *fp;
    157 
    158 	if ((fp = fopen(name, "r")) == NULL)
    159 		err(1, "cannot open %s", name);
    160 	args[0] = &arg[0][0];
    161 	args[1] = &arg[1][0];
    162 	args[2] = &arg[2][0];
    163 	args[3] = &arg[3][0];
    164 	args[4] = &arg[4][0];
    165 	retval = 0;
    166 	while (fgets(line, 100, fp) != NULL) {
    167 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    168 		    arg[3], arg[4]);
    169 		if (i < 2) {
    170 			warnx("bad line: %s", line);
    171 			retval = 1;
    172 			continue;
    173 		}
    174 		if (set(i, args))
    175 			retval = 1;
    176 	}
    177 	fclose(fp);
    178 	return (retval);
    179 }
    180 
    181 void
    182 getsocket()
    183 {
    184 	if (s >= 0)
    185 		return;
    186 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    187 	if (s < 0)
    188 		err(1, "socket");
    189 }
    190 
    191 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    192 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    193 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    194 int	expire_time, flags, export_only, doing_proxy, found_entry;
    195 struct	{
    196 	struct	rt_msghdr m_rtm;
    197 	char	m_space[512];
    198 }	m_rtmsg;
    199 
    200 /*
    201  * Set an individual arp entry
    202  */
    203 int
    204 set(argc, argv)
    205 	int argc;
    206 	char **argv;
    207 {
    208 	register struct sockaddr_inarp *sin;
    209 	register struct sockaddr_dl *sdl;
    210 	register struct rt_msghdr *rtm;
    211 	struct ether_addr *ea;
    212 	char *host = argv[0], *eaddr;
    213 
    214 	sin = &sin_m;
    215 	rtm = &(m_rtmsg.m_rtm);
    216 	eaddr = argv[1];
    217 
    218 	getsocket();
    219 	argc -= 2;
    220 	argv += 2;
    221 	sdl_m = blank_sdl;		/* struct copy */
    222 	sin_m = blank_sin;		/* struct copy */
    223 	if (getinetaddr(host, &sin->sin_addr) == -1)
    224 		return (1);
    225 	ea = ether_aton(eaddr);
    226 	if (ea != NULL) {
    227 		(void)memcpy(LLADDR(&sdl_m), ea, sizeof(struct ether_addr));
    228 		sdl_m.sdl_alen = sizeof(struct ether_addr);
    229 	}
    230 	else
    231 		warnx("invalid Ethernet address '%s'", eaddr);
    232 	doing_proxy = flags = export_only = expire_time = 0;
    233 	while (argc-- > 0) {
    234 		if (strncmp(argv[0], "temp", 4) == 0) {
    235 			struct timeval time;
    236 			(void)gettimeofday(&time, 0);
    237 			expire_time = time.tv_sec + 20 * 60;
    238 		}
    239 		else if (strncmp(argv[0], "pub", 3) == 0) {
    240 			flags |= RTF_ANNOUNCE;
    241 			doing_proxy = SIN_PROXY;
    242 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    243 			(void)printf(
    244 			    "%s: Sending trailers is no longer supported\n",
    245 			     host);
    246 		}
    247 		argv++;
    248 	}
    249 tryagain:
    250 	if (rtmsg(RTM_GET) < 0) {
    251 		warn("%s", host);
    252 		return (1);
    253 	}
    254 	sin = (struct sockaddr_inarp *)(rtm + 1);
    255 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    256 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    257 		if (sdl->sdl_family == AF_LINK &&
    258 		    (rtm->rtm_flags & RTF_LLINFO) &&
    259 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    260 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    261 		case IFT_ISO88024: case IFT_ISO88025:
    262 			goto overwrite;
    263 		}
    264 		if (doing_proxy == 0) {
    265 			(void)printf("set: can only proxy for %s\n", host);
    266 			return (1);
    267 		}
    268 		if (sin_m.sin_other & SIN_PROXY) {
    269 			(void)printf(
    270 			    "set: proxy entry exists for non 802 device\n");
    271 			return (1);
    272 		}
    273 		sin_m.sin_other = SIN_PROXY;
    274 		export_only = 1;
    275 		goto tryagain;
    276 	}
    277 overwrite:
    278 	if (sdl->sdl_family != AF_LINK) {
    279 		(void)printf("cannot intuit interface index and type for %s\n",
    280 		    host);
    281 		return (1);
    282 	}
    283 	sdl_m.sdl_type = sdl->sdl_type;
    284 	sdl_m.sdl_index = sdl->sdl_index;
    285 	return (rtmsg(RTM_ADD));
    286 }
    287 
    288 /*
    289  * Display an individual arp entry
    290  */
    291 void
    292 get(host)
    293 	const char *host;
    294 {
    295 	struct sockaddr_inarp *sin;
    296 
    297 	sin = &sin_m;
    298 	sin_m = blank_sin;		/* struct copy */
    299 	if (getinetaddr(host, &sin->sin_addr) == -1)
    300 		exit(1);
    301 	dump(sin->sin_addr.s_addr);
    302 	if (found_entry == 0) {
    303 		(void)printf("%s (%s) -- no entry\n", host,
    304 		    inet_ntoa(sin->sin_addr));
    305 		exit(1);
    306 	}
    307 }
    308 
    309 /*
    310  * Delete an arp entry
    311  */
    312 int
    313 delete(host, info)
    314 	const char *host;
    315 	const char *info;
    316 {
    317 	register struct sockaddr_inarp *sin;
    318 	register struct rt_msghdr *rtm;
    319 	struct sockaddr_dl *sdl;
    320 
    321 	sin = &sin_m;
    322 	rtm = &m_rtmsg.m_rtm;
    323 
    324 	if (info && strncmp(info, "pro", 3) )
    325 		export_only = 1;
    326 	getsocket();
    327 	sin_m = blank_sin;		/* struct copy */
    328 	if (getinetaddr(host, &sin->sin_addr) == -1)
    329 		return (1);
    330 tryagain:
    331 	if (rtmsg(RTM_GET) < 0) {
    332 		warn("%s", host);
    333 		return (1);
    334 	}
    335 	sin = (struct sockaddr_inarp *)(rtm + 1);
    336 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    337 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    338 		if (sdl->sdl_family == AF_LINK &&
    339 		    (rtm->rtm_flags & RTF_LLINFO) &&
    340 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    341 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    342 		case IFT_ISO88024: case IFT_ISO88025:
    343 			goto delete;
    344 		}
    345 	}
    346 	if (sin_m.sin_other & SIN_PROXY) {
    347 		warnx("delete: can't locate %s", host);
    348 		return (1);
    349 	} else {
    350 		sin_m.sin_other = SIN_PROXY;
    351 		goto tryagain;
    352 	}
    353 delete:
    354 	if (sdl->sdl_family != AF_LINK) {
    355 		(void)printf("cannot locate %s\n", host);
    356 		return (1);
    357 	}
    358 	if (rtmsg(RTM_DELETE))
    359 		return (1);
    360 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
    361 	return (0);
    362 }
    363 
    364 /*
    365  * Dump the entire arp table
    366  */
    367 void
    368 dump(addr)
    369 	u_long addr;
    370 {
    371 	int mib[6];
    372 	size_t needed;
    373 	char *host, *lim, *buf, *next;
    374 	struct rt_msghdr *rtm;
    375 	struct sockaddr_inarp *sin;
    376 	struct sockaddr_dl *sdl;
    377 	extern int h_errno;
    378 	struct hostent *hp;
    379 
    380 	mib[0] = CTL_NET;
    381 	mib[1] = PF_ROUTE;
    382 	mib[2] = 0;
    383 	mib[3] = AF_INET;
    384 	mib[4] = NET_RT_FLAGS;
    385 	mib[5] = RTF_LLINFO;
    386 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    387 		err(1, "route-sysctl-estimate");
    388 	if ((buf = malloc(needed)) == NULL)
    389 		err(1, "malloc");
    390 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    391 		err(1, "actual retrieval of routing table");
    392 	lim = buf + needed;
    393 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    394 		rtm = (struct rt_msghdr *)next;
    395 		sin = (struct sockaddr_inarp *)(rtm + 1);
    396 		sdl = (struct sockaddr_dl *)(sin + 1);
    397 		if (addr) {
    398 			if (addr != sin->sin_addr.s_addr)
    399 				continue;
    400 			found_entry = 1;
    401 		}
    402 		if (nflag == 0)
    403 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    404 			    sizeof sin->sin_addr, AF_INET);
    405 		else
    406 			hp = 0;
    407 		if (hp)
    408 			host = hp->h_name;
    409 		else {
    410 			host = "?";
    411 			if (h_errno == TRY_AGAIN)
    412 				nflag = 1;
    413 		}
    414 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    415 		if (sdl->sdl_alen)
    416 			ether_print((u_char *)LLADDR(sdl));
    417 		else
    418 			(void)printf("(incomplete)");
    419 		if (rtm->rtm_rmx.rmx_expire == 0)
    420 			(void)printf(" permanent");
    421 		if (sin->sin_other & SIN_PROXY)
    422 			(void)printf(" published (proxy only)");
    423 		if (rtm->rtm_addrs & RTA_NETMASK) {
    424 			sin = (struct sockaddr_inarp *)
    425 				(sdl->sdl_len + (char *)sdl);
    426 			if (sin->sin_addr.s_addr == 0xffffffff)
    427 				(void)printf(" published");
    428 			if (sin->sin_len != 8)
    429 				(void)printf("(wierd)");
    430 		}
    431 		(void)printf("\n");
    432 	}
    433 }
    434 
    435 void
    436 ether_print(cp)
    437 	const u_char *cp;
    438 {
    439 	(void)printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4],
    440 	    cp[5]);
    441 }
    442 
    443 void
    444 usage()
    445 {
    446 	(void)fprintf(stderr, "usage: arp [-n] hostname\n");
    447 	(void)fprintf(stderr, "usage: arp [-n] -a\n");
    448 	(void)fprintf(stderr, "usage: arp -d hostname\n");
    449 	(void)fprintf(stderr,
    450 	    "usage: arp -s hostname ether_addr [temp] [pub]\n");
    451 	(void)fprintf(stderr, "usage: arp -f filename\n");
    452 	exit(1);
    453 }
    454 
    455 int
    456 rtmsg(cmd)
    457 	int cmd;
    458 {
    459 	static int seq;
    460 	int rlen;
    461 	register struct rt_msghdr *rtm;
    462 	register char *cp;
    463 	register int l;
    464 
    465 	rtm = &m_rtmsg.m_rtm;
    466 	cp = m_rtmsg.m_space;
    467 	errno = 0;
    468 
    469 	if (cmd == RTM_DELETE)
    470 		goto doit;
    471 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    472 	rtm->rtm_flags = flags;
    473 	rtm->rtm_version = RTM_VERSION;
    474 
    475 	switch (cmd) {
    476 	default:
    477 		errx(1, "internal wrong cmd");
    478 		/*NOTREACHED*/
    479 	case RTM_ADD:
    480 		rtm->rtm_addrs |= RTA_GATEWAY;
    481 		rtm->rtm_rmx.rmx_expire = expire_time;
    482 		rtm->rtm_inits = RTV_EXPIRE;
    483 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    484 		sin_m.sin_other = 0;
    485 		if (doing_proxy) {
    486 			if (export_only)
    487 				sin_m.sin_other = SIN_PROXY;
    488 			else {
    489 				rtm->rtm_addrs |= RTA_NETMASK;
    490 				rtm->rtm_flags &= ~RTF_HOST;
    491 			}
    492 		}
    493 		/* FALLTHROUGH */
    494 	case RTM_GET:
    495 		rtm->rtm_addrs |= RTA_DST;
    496 	}
    497 #define NEXTADDR(w, s) \
    498 	if (rtm->rtm_addrs & (w)) { \
    499 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    500 
    501 	NEXTADDR(RTA_DST, sin_m);
    502 	NEXTADDR(RTA_GATEWAY, sdl_m);
    503 	NEXTADDR(RTA_NETMASK, so_mask);
    504 
    505 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    506 doit:
    507 	l = rtm->rtm_msglen;
    508 	rtm->rtm_seq = ++seq;
    509 	rtm->rtm_type = cmd;
    510 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    511 		if (errno != ESRCH || cmd != RTM_DELETE) {
    512 			warn("writing to routing socket");
    513 			return (-1);
    514 		}
    515 	}
    516 	do {
    517 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    518 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    519 	if (l < 0)
    520 		warn("read from routing socket");
    521 	return (0);
    522 }
    523 
    524 int
    525 getinetaddr(host, inap)
    526 	const char *host;
    527 	struct in_addr *inap;
    528 {
    529 	struct hostent *hp;
    530 
    531 	if (inet_aton(host, inap) == 1)
    532 		return (0);
    533 	if ((hp = gethostbyname(host)) == NULL) {
    534 		warnx("%s: %s\n", host, hstrerror(h_errno));
    535 		return (-1);
    536 	}
    537 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    538 	return (0);
    539 }
    540