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