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