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