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