xref: /freebsd/usr.sbin/arp/arp.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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 const 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 const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
45 static char const freebsdid[] = "$Id$";
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/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/ioctl.h>
59 
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 
68 #include <arpa/inet.h>
69 
70 #include <netdb.h>
71 #include <errno.h>
72 #include <nlist.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <unistd.h>
76 #include <strings.h>
77 #include <paths.h>
78 
79 void dump(u_long addr);
80 int delete(char *host, char *info);
81 void ether_print(u_char *cp);
82 void usage(void);
83 int set(int argc, char **argv);
84 void get(char *host);
85 int file(char *name);
86 void getsocket(void);
87 int ether_aton(char *a, u_char *n);
88 int rtmsg(int cmd);
89 void quit(char *msg);
90 int get_ether_addr(u_long ipaddr, u_char *hwaddr);
91 
92 extern int errno;
93 static int pid;
94 static int nflag;
95 static int s = -1;
96 
97 int
98 main(argc, argv)
99 	int argc;
100 	char **argv;
101 {
102 	int ch;
103 
104 	pid = getpid();
105 	while ((ch = getopt(argc, argv, "andfsS")) != EOF)
106 		switch((char)ch) {
107 		case 'a':
108 			dump(0);
109 			exit(0);
110 		case 'd':
111 			if (argc < 3 || argc > 4)
112 				usage();
113 			delete(argv[2], argv[3]);
114 			exit(0);
115 		case 'n':
116 			nflag = 1;
117 			continue;
118 		case 'S':
119 			delete(argv[2], NULL);
120 			/* FALL THROUGH */
121 		case 's':
122 			if (argc < 4 || argc > 7)
123 				usage();
124 			exit(set(argc-2, &argv[2]) ? 1 : 0);
125 		case 'f' :
126 			if (argc != 3)
127 				usage();
128 			return (file(argv[2]));
129 		case '?':
130 		default:
131 			usage();
132 		}
133 	if (argc != 2)
134 		usage();
135 	get(argv[1]);
136 	exit(0);
137 }
138 
139 /*
140  * Process a file to set standard arp entries
141  */
142 int
143 file(char *name)
144 {
145 	FILE *fp;
146 	int i, retval;
147 	char line[100], arg[5][50], *args[5];
148 
149 	if ((fp = fopen(name, "r")) == NULL) {
150 		fprintf(stderr, "arp: cannot open %s\n", name);
151 		exit(1);
152 	}
153 	args[0] = &arg[0][0];
154 	args[1] = &arg[1][0];
155 	args[2] = &arg[2][0];
156 	args[3] = &arg[3][0];
157 	args[4] = &arg[4][0];
158 	retval = 0;
159 	while(fgets(line, 100, fp) != NULL) {
160 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
161 		    arg[3], arg[4]);
162 		if (i < 2) {
163 			fprintf(stderr, "arp: bad line: %s\n", line);
164 			retval = 1;
165 			continue;
166 		}
167 		if (set(i, args))
168 			retval = 1;
169 	}
170 	fclose(fp);
171 	return (retval);
172 }
173 
174 void
175 getsocket(void)
176 {
177 	if (s < 0) {
178 		s = socket(PF_ROUTE, SOCK_RAW, 0);
179 		if (s < 0) {
180 			perror("arp: socket");
181 			exit(1);
182 		}
183 	}
184 }
185 
186 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
187 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
188 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
189 int	expire_time, flags, export_only, doing_proxy, found_entry;
190 struct	{
191 	struct	rt_msghdr m_rtm;
192 	char	m_space[512];
193 }	m_rtmsg;
194 
195 /*
196  * Set an individual arp entry
197  */
198 int
199 set(int argc, char **argv)
200 {
201 	struct hostent *hp;
202 	register struct sockaddr_inarp *sin = &sin_m;
203 	register struct sockaddr_dl *sdl;
204 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
205 	u_char *ea;
206 	char *host = argv[0], *eaddr = argv[1];
207 
208 	getsocket();
209 	argc -= 2;
210 	argv += 2;
211 	sdl_m = blank_sdl;
212 	sin_m = blank_sin;
213 	sin->sin_addr.s_addr = inet_addr(host);
214 	if (sin->sin_addr.s_addr == -1) {
215 		if (!(hp = gethostbyname(host))) {
216 			fprintf(stderr, "arp: %s: ", host);
217 			herror((char *)NULL);
218 			return (1);
219 		}
220 		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
221 		    sizeof sin->sin_addr);
222 	}
223 	doing_proxy = flags = export_only = expire_time = 0;
224 	while (argc-- > 0) {
225 		if (strncmp(argv[0], "temp", 4) == 0) {
226 			struct timeval time;
227 			gettimeofday(&time, 0);
228 			expire_time = time.tv_sec + 20 * 60;
229 		}
230 		else if (strncmp(argv[0], "pub", 3) == 0) {
231 			flags |= RTF_ANNOUNCE;
232 			doing_proxy = SIN_PROXY;
233 		} else if (strncmp(argv[0], "trail", 5) == 0) {
234 			printf("%s: Sending trailers is no longer supported\n",
235 				host);
236 		}
237 		argv++;
238 	}
239 	ea = (u_char *)LLADDR(&sdl_m);
240 	if (doing_proxy && !strcmp(eaddr, "auto")) {
241 		if (!get_ether_addr(sin->sin_addr.s_addr, ea)) {
242 			return (1);
243 		}
244 		sdl_m.sdl_alen = 6;
245 	} else {
246 		if (ether_aton(eaddr, ea) == 0)
247 			sdl_m.sdl_alen = 6;
248 	}
249 tryagain:
250 	if (rtmsg(RTM_GET) < 0) {
251 		perror(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 			printf("set: can only proxy for %s\n", host);
266 			return (1);
267 		}
268 		if (sin_m.sin_other & SIN_PROXY) {
269 			printf("set: proxy entry exists for non 802 device\n");
270 			return(1);
271 		}
272 		sin_m.sin_other = SIN_PROXY;
273 		export_only = 1;
274 		goto tryagain;
275 	}
276 overwrite:
277 	if (sdl->sdl_family != AF_LINK) {
278 		printf("cannot intuit interface index and type for %s\n", host);
279 		return (1);
280 	}
281 	sdl_m.sdl_type = sdl->sdl_type;
282 	sdl_m.sdl_index = sdl->sdl_index;
283 	return (rtmsg(RTM_ADD));
284 }
285 
286 /*
287  * Display an individual arp entry
288  */
289 void
290 get(char *host)
291 {
292 	struct hostent *hp;
293 	struct sockaddr_inarp *sin = &sin_m;
294 
295 	sin_m = blank_sin;
296 	sin->sin_addr.s_addr = inet_addr(host);
297 	if (sin->sin_addr.s_addr == -1) {
298 		if (!(hp = gethostbyname(host))) {
299 			fprintf(stderr, "arp: %s: ", host);
300 			herror((char *)NULL);
301 			exit(1);
302 		}
303 		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
304 		    sizeof sin->sin_addr);
305 	}
306 	dump(sin->sin_addr.s_addr);
307 	if (found_entry == 0) {
308 		printf("%s (%s) -- no entry\n",
309 		    host, inet_ntoa(sin->sin_addr));
310 		exit(1);
311 	}
312 }
313 
314 /*
315  * Delete an arp entry
316  */
317 int
318 delete(char *host, char *info)
319 {
320 	struct hostent *hp;
321 	register struct sockaddr_inarp *sin = &sin_m;
322 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
323 	struct sockaddr_dl *sdl;
324 
325 	if (info && strncmp(info, "pro", 3) )
326 		export_only = 1;
327 	getsocket();
328 	sin_m = blank_sin;
329 	sin->sin_addr.s_addr = inet_addr(host);
330 	if (sin->sin_addr.s_addr == -1) {
331 		if (!(hp = gethostbyname(host))) {
332 			fprintf(stderr, "arp: %s: ", host);
333 			herror((char *)NULL);
334 			return (1);
335 		}
336 		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
337 		    sizeof sin->sin_addr);
338 	}
339 tryagain:
340 	if (rtmsg(RTM_GET) < 0) {
341 		perror(host);
342 		return (1);
343 	}
344 	sin = (struct sockaddr_inarp *)(rtm + 1);
345 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
346 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
347 		if (sdl->sdl_family == AF_LINK &&
348 		    (rtm->rtm_flags & RTF_LLINFO) &&
349 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
350 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
351 		case IFT_ISO88024: case IFT_ISO88025:
352 			goto delete;
353 		}
354 	}
355 	if (sin_m.sin_other & SIN_PROXY) {
356 		fprintf(stderr, "delete: can't locate %s\n",host);
357 		return (1);
358 	} else {
359 		sin_m.sin_other = SIN_PROXY;
360 		goto tryagain;
361 	}
362 delete:
363 	if (sdl->sdl_family != AF_LINK) {
364 		printf("cannot locate %s\n", host);
365 		return (1);
366 	}
367 	if (rtmsg(RTM_DELETE) == 0) {
368 		printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
369 		return (0);
370 	}
371 	return (1);
372 }
373 
374 /*
375  * Dump the entire arp table
376  */
377 void
378 dump(u_long addr)
379 {
380 	int mib[6];
381 	size_t needed;
382 	char *host, *malloc(), *lim, *buf, *next;
383 	struct rt_msghdr *rtm;
384 	struct sockaddr_inarp *sin;
385 	struct sockaddr_dl *sdl;
386 	extern int h_errno;
387 	struct hostent *hp;
388 
389 	mib[0] = CTL_NET;
390 	mib[1] = PF_ROUTE;
391 	mib[2] = 0;
392 	mib[3] = AF_INET;
393 	mib[4] = NET_RT_FLAGS;
394 	mib[5] = RTF_LLINFO;
395 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
396 		quit("route-sysctl-estimate");
397 	if ((buf = malloc(needed)) == NULL)
398 		quit("malloc");
399 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
400 		quit("actual retrieval of routing table");
401 	lim = buf + needed;
402 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
403 		rtm = (struct rt_msghdr *)next;
404 		sin = (struct sockaddr_inarp *)(rtm + 1);
405 		sdl = (struct sockaddr_dl *)(sin + 1);
406 		if (addr) {
407 			if (addr != sin->sin_addr.s_addr)
408 				continue;
409 			found_entry = 1;
410 		}
411 		if (nflag == 0)
412 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
413 			    sizeof sin->sin_addr, AF_INET);
414 		else
415 			hp = 0;
416 		if (hp)
417 			host = hp->h_name;
418 		else {
419 			host = "?";
420 			if (h_errno == TRY_AGAIN)
421 				nflag = 1;
422 		}
423 		printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
424 		if (sdl->sdl_alen)
425 			ether_print(LLADDR(sdl));
426 		else
427 			printf("(incomplete)");
428 		if (rtm->rtm_rmx.rmx_expire == 0)
429 			printf(" permanent");
430 		if (sin->sin_other & SIN_PROXY)
431 			printf(" published (proxy only)");
432 		if (rtm->rtm_addrs & RTA_NETMASK) {
433 			sin = (struct sockaddr_inarp *)
434 				(sdl->sdl_len + (char *)sdl);
435 			if (sin->sin_addr.s_addr == 0xffffffff)
436 				printf(" published");
437 			if (sin->sin_len != 8)
438 				printf("(wierd)");
439 		}
440 		printf("\n");
441 	}
442 }
443 
444 void
445 ether_print(u_char *cp)
446 {
447 	printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
448 }
449 
450 int
451 ether_aton(char *a, u_char *n)
452 {
453 	int i, o[6];
454 
455 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
456 					   &o[3], &o[4], &o[5]);
457 	if (i != 6) {
458 		fprintf(stderr, "arp: invalid Ethernet address '%s'\n", a);
459 		return (1);
460 	}
461 	for (i=0; i<6; i++)
462 		n[i] = o[i];
463 	return (0);
464 }
465 
466 void
467 usage(void)
468 {
469 	printf("usage: arp hostname\n");
470 	printf("       arp -a [kernel] [kernel_memory]\n");
471 	printf("       arp -d hostname\n");
472 	printf("       arp -s hostname ether_addr [temp] [pub]\n");
473 	printf("       arp -S hostname ether_addr [temp] [pub]\n");
474 	printf("       arp -f filename\n");
475 	exit(1);
476 }
477 
478 int
479 rtmsg(int cmd)
480 {
481 	static int seq;
482 	int rlen;
483 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
484 	register char *cp = m_rtmsg.m_space;
485 	register int l;
486 
487 	errno = 0;
488 	if (cmd == RTM_DELETE)
489 		goto doit;
490 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
491 	rtm->rtm_flags = flags;
492 	rtm->rtm_version = RTM_VERSION;
493 
494 	switch (cmd) {
495 	default:
496 		fprintf(stderr, "arp: internal wrong cmd\n");
497 		exit(1);
498 	case RTM_ADD:
499 		rtm->rtm_addrs |= RTA_GATEWAY;
500 		rtm->rtm_rmx.rmx_expire = expire_time;
501 		rtm->rtm_inits = RTV_EXPIRE;
502 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
503 		sin_m.sin_other = 0;
504 		if (doing_proxy) {
505 			if (export_only)
506 				sin_m.sin_other = SIN_PROXY;
507 			else {
508 				rtm->rtm_addrs |= RTA_NETMASK;
509 				rtm->rtm_flags &= ~RTF_HOST;
510 			}
511 		}
512 		/* FALLTHROUGH */
513 	case RTM_GET:
514 		rtm->rtm_addrs |= RTA_DST;
515 	}
516 #define NEXTADDR(w, s) \
517 	if (rtm->rtm_addrs & (w)) { \
518 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
519 
520 	NEXTADDR(RTA_DST, sin_m);
521 	NEXTADDR(RTA_GATEWAY, sdl_m);
522 	NEXTADDR(RTA_NETMASK, so_mask);
523 
524 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
525 doit:
526 	l = rtm->rtm_msglen;
527 	rtm->rtm_seq = ++seq;
528 	rtm->rtm_type = cmd;
529 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
530 		if (errno != ESRCH || cmd != RTM_DELETE) {
531 			perror("writing to routing socket");
532 			return (-1);
533 		}
534 	}
535 	do {
536 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
537 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
538 	if (l < 0)
539 		(void) fprintf(stderr, "arp: read from routing socket: %s\n",
540 		    strerror(errno));
541 	return (0);
542 }
543 
544 void
545 quit(char *msg)
546 {
547 	fprintf(stderr, "%s\n", msg);
548 	exit(1);
549 }
550 
551 /*
552  * get_ether_addr - get the hardware address of an interface on the
553  * the same subnet as ipaddr.
554  */
555 #define MAX_IFS		32
556 
557 int
558 get_ether_addr(u_long ipaddr, u_char *hwaddr)
559 {
560 	struct ifreq *ifr, *ifend, *ifp;
561 	u_long ina, mask;
562 	struct sockaddr_dl *dla;
563 	struct ifreq ifreq;
564 	struct ifconf ifc;
565 	struct ifreq ifs[MAX_IFS];
566 	int s;
567 
568 	s = socket(AF_INET, SOCK_DGRAM, 0);
569 	if (s < 0) {
570 		perror("socket");
571 		exit(1);
572 	}
573 
574 	ifc.ifc_len = sizeof(ifs);
575 	ifc.ifc_req = ifs;
576 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
577 		fprintf(stderr, "ioctl(SIOCGIFCONF): \n");
578 		close(s);
579 		return 0;
580 	}
581 
582 	/*
583 	* Scan through looking for an interface with an Internet
584 	* address on the same subnet as `ipaddr'.
585 	*/
586 	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
587 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
588 		if (ifr->ifr_addr.sa_family == AF_INET) {
589 			ina = ((struct sockaddr_in *)
590 				&ifr->ifr_addr)->sin_addr.s_addr;
591 			strncpy(ifreq.ifr_name, ifr->ifr_name,
592 				sizeof(ifreq.ifr_name));
593 			/*
594 			 * Check that the interface is up,
595 			 * and not point-to-point or loopback.
596 			 */
597 			if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
598 				continue;
599 			if ((ifreq.ifr_flags &
600 			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
601 					IFF_LOOPBACK|IFF_NOARP))
602 			     != (IFF_UP|IFF_BROADCAST))
603 				goto nextif;
604 			/*
605 			 * Get its netmask and check that it's on
606 			 * the right subnet.
607 			 */
608 			if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
609 				continue;
610 			mask = ((struct sockaddr_in *)
611 				&ifreq.ifr_addr)->sin_addr.s_addr;
612 			if ((ipaddr & mask) != (ina & mask))
613 				goto nextif;
614 			break;
615 		}
616 nextif:
617 		ifr = (struct ifreq *)
618 		    ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
619 	}
620 
621 	if (ifr >= ifend) {
622 		close(s);
623 		return 0;
624 	}
625 
626 	/*
627 	* Now scan through again looking for a link-level address
628 	* for this interface.
629 	*/
630 	ifp = ifr;
631 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
632 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
633 		    && ifr->ifr_addr.sa_family == AF_LINK) {
634 			/*
635 			 * Found the link-level address - copy it out
636 			 */
637 		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
638 			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
639 			close (s);
640 			printf("using interface %s for proxy with address ",
641 				ifp->ifr_name);
642 			ether_print(hwaddr);
643 			printf("\n");
644 			return dla->sdl_alen;
645 		}
646 		ifr = (struct ifreq *)
647 			((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
648 	}
649 	return 0;
650 }
651