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