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