Hook up ascii.tmac for the tool build as well.
[netbsd-mini2440.git] / usr.sbin / arp / arp.c
blobae9b2b73164468b7c05ded1ce648f63470903f1a
1 /* $NetBSD: arp.c,v 1.47 2008/07/21 13:36:57 lukem Exp $ */
3 /*
4 * Copyright (c) 1984, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Sun Microsystems, Inc.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: arp.c,v 1.47 2008/07/21 13:36:57 lukem Exp $");
46 #endif
47 #endif /* not lint */
50 * arp - display, set, and delete arp table entries
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/ioctl.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_inarp.h>
66 #include <arpa/inet.h>
68 #include <err.h>
69 #include <errno.h>
70 #include <netdb.h>
71 #include <nlist.h>
72 #include <paths.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include <ifaddrs.h>
79 static int is_llinfo(const struct sockaddr_dl *, int);
80 static int delete(const char *, const char *);
81 static void dump(uint32_t);
82 static void delete_all(void);
83 static void sdl_print(const struct sockaddr_dl *);
84 static int getifname(u_int16_t, char *, size_t);
85 static int atosdl(const char *s, struct sockaddr_dl *sdl);
86 static int file(const char *);
87 static void get(const char *);
88 static int getinetaddr(const char *, struct in_addr *);
89 static void getsocket(void);
90 static int rtmsg(int);
91 static int set(int, char **);
92 static void usage(void) __dead;
94 static pid_t pid;
95 static int aflag, nflag, vflag;
96 static int s = -1;
97 static struct ifaddrs* ifaddrs = NULL;
98 static struct sockaddr_in so_mask = {
99 .sin_len = 8,
100 .sin_addr = {
101 .s_addr = 0xffffffff
104 static struct sockaddr_inarp blank_sin = {
105 .sin_len = sizeof(blank_sin),
106 .sin_family = AF_INET
108 static struct sockaddr_inarp sin_m;
109 static struct sockaddr_dl blank_sdl = {
110 .sdl_len = sizeof(blank_sdl),
111 .sdl_family = AF_LINK
113 static struct sockaddr_dl sdl_m;
115 static int expire_time, flags, export_only, doing_proxy, found_entry;
116 static struct {
117 struct rt_msghdr m_rtm;
118 char m_space[512];
119 } m_rtmsg;
122 main(int argc, char **argv)
124 int ch;
125 int op = 0;
127 setprogname(argv[0]);
129 pid = getpid();
131 while ((ch = getopt(argc, argv, "andsfv")) != -1)
132 switch((char)ch) {
133 case 'a':
134 aflag = 1;
135 break;
136 case 'd':
137 case 's':
138 case 'f':
139 if (op)
140 usage();
141 op = ch;
142 break;
143 case 'n':
144 nflag = 1;
145 break;
146 case 'v':
147 vflag = 1;
148 break;
149 default:
150 usage();
152 argc -= optind;
153 argv += optind;
155 if (!op && aflag)
156 op = 'a';
158 switch((char)op) {
159 case 'a':
160 dump(0);
161 break;
162 case 'd':
163 if (aflag && argc == 0)
164 delete_all();
165 else {
166 if (aflag || argc < 1 || argc > 2)
167 usage();
168 (void)delete(argv[0], argv[1]);
170 break;
171 case 's':
172 if (argc < 2 || argc > 5)
173 usage();
174 return (set(argc, argv) ? 1 : 0);
175 case 'f':
176 if (argc != 1)
177 usage();
178 return (file(argv[0]));
179 default:
180 if (argc != 1)
181 usage();
182 get(argv[0]);
183 break;
185 return (0);
189 * Process a file to set standard arp entries
191 static int
192 file(const char *name)
194 char *line, *argv[5];
195 int i, retval;
196 FILE *fp;
198 if ((fp = fopen(name, "r")) == NULL)
199 err(1, "cannot open %s", name);
200 retval = 0;
201 for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
202 char **ap, *inputstring;
204 inputstring = line;
205 for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
206 (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
207 if (**ap != '\0')
208 ap++;
210 i = ap - argv;
211 if (i < 2) {
212 warnx("bad line: %s", line);
213 retval = 1;
214 continue;
216 if (set(i, argv))
217 retval = 1;
219 (void)fclose(fp);
220 return retval;
223 static void
224 getsocket(void)
226 if (s >= 0)
227 return;
228 s = socket(PF_ROUTE, SOCK_RAW, 0);
229 if (s < 0)
230 err(1, "socket");
234 * Set an individual arp entry
236 static int
237 set(int argc, char **argv)
239 struct sockaddr_inarp *sina;
240 struct sockaddr_dl *sdl;
241 struct rt_msghdr *rtm;
242 char *host = argv[0], *eaddr;
243 int rval;
245 sina = &sin_m;
246 rtm = &(m_rtmsg.m_rtm);
247 eaddr = argv[1];
249 getsocket();
250 argc -= 2;
251 argv += 2;
252 sdl_m = blank_sdl; /* struct copy */
253 sin_m = blank_sin; /* struct copy */
254 if (getinetaddr(host, &sina->sin_addr) == -1)
255 return (1);
256 if (atosdl(eaddr, &sdl_m))
257 warnx("invalid link-level address '%s'", eaddr);
258 doing_proxy = flags = export_only = expire_time = 0;
259 while (argc-- > 0) {
260 if (strncmp(argv[0], "temp", 4) == 0) {
261 struct timeval timev;
262 (void)gettimeofday(&timev, 0);
263 expire_time = timev.tv_sec + 20 * 60;
265 else if (strncmp(argv[0], "pub", 3) == 0) {
266 flags |= RTF_ANNOUNCE;
267 doing_proxy = SIN_PROXY;
268 if (argc && strncmp(argv[1], "pro", 3) == 0) {
269 export_only = 1;
270 argc--; argv++;
272 } else if (strncmp(argv[0], "trail", 5) == 0) {
273 warnx("%s: Sending trailers is no longer supported",
274 host);
276 argv++;
278 tryagain:
279 if (rtmsg(RTM_GET) < 0) {
280 warn("%s", host);
281 return (1);
283 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
284 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
285 (char *)(void *)sina);
286 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
287 if (is_llinfo(sdl, rtm->rtm_flags))
288 goto overwrite;
289 if (doing_proxy == 0) {
290 warnx("set: can only proxy for %s", host);
291 return (1);
293 if (sin_m.sin_other & SIN_PROXY) {
294 warnx("set: proxy entry exists for non 802 device");
295 return (1);
297 sin_m.sin_other = SIN_PROXY;
298 export_only = 1;
299 goto tryagain;
301 overwrite:
302 if (sdl->sdl_family != AF_LINK) {
303 warnx("cannot intuit interface index and type for %s",
304 host);
305 return (1);
307 sdl_m.sdl_type = sdl->sdl_type;
308 sdl_m.sdl_index = sdl->sdl_index;
309 rval = rtmsg(RTM_ADD);
310 if (vflag)
311 (void)printf("%s (%s) added\n", host, eaddr);
312 return (rval);
316 * Display an individual arp entry
318 static void
319 get(const char *host)
321 struct sockaddr_inarp *sina;
323 sina = &sin_m;
324 sin_m = blank_sin; /* struct copy */
325 if (getinetaddr(host, &sina->sin_addr) == -1)
326 exit(1);
327 dump(sina->sin_addr.s_addr);
328 if (found_entry == 0)
329 errx(1, "%s (%s) -- no entry", host, inet_ntoa(sina->sin_addr));
333 static int
334 is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
336 if (sdl->sdl_family != AF_LINK ||
337 (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
338 return 0;
340 switch (sdl->sdl_type) {
341 case IFT_ETHER:
342 case IFT_FDDI:
343 case IFT_ISO88023:
344 case IFT_ISO88024:
345 case IFT_ISO88025:
346 case IFT_ARCNET:
347 return 1;
348 default:
349 return 0;
354 * Delete an arp entry
357 delete(const char *host, const char *info)
359 struct sockaddr_inarp *sina;
360 struct rt_msghdr *rtm;
361 struct sockaddr_dl *sdl;
363 sina = &sin_m;
364 rtm = &m_rtmsg.m_rtm;
366 getsocket();
367 sin_m = blank_sin; /* struct copy */
368 if (info && strncmp(info, "pro", 3) == 0)
369 sina->sin_other = SIN_PROXY;
370 if (getinetaddr(host, &sina->sin_addr) == -1)
371 return (1);
372 tryagain:
373 if (rtmsg(RTM_GET) < 0) {
374 warn("%s", host);
375 return (1);
377 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
378 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
379 (char *)(void *)sina);
380 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
381 is_llinfo(sdl, rtm->rtm_flags))
382 goto delete;
383 if (sin_m.sin_other & SIN_PROXY) {
384 warnx("delete: can't locate %s", host);
385 return (1);
386 } else {
387 sin_m.sin_other = SIN_PROXY;
388 goto tryagain;
390 delete:
391 if (sdl->sdl_family != AF_LINK) {
392 (void)warnx("cannot locate %s", host);
393 return (1);
395 if (rtmsg(RTM_DELETE))
396 return (1);
397 if (vflag)
398 (void)printf("%s (%s) deleted\n", host,
399 inet_ntoa(sina->sin_addr));
400 return (0);
404 * Dump the entire arp table
406 void
407 dump(uint32_t addr)
409 int mib[6];
410 size_t needed;
411 char ifname[IFNAMSIZ];
412 char *lim, *buf, *next;
413 const char *host;
414 struct rt_msghdr *rtm;
415 struct sockaddr_inarp *sina;
416 struct sockaddr_dl *sdl;
417 struct hostent *hp;
419 mib[0] = CTL_NET;
420 mib[1] = PF_ROUTE;
421 mib[2] = 0;
422 mib[3] = AF_INET;
423 mib[4] = NET_RT_FLAGS;
424 mib[5] = RTF_LLINFO;
425 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
426 err(1, "route-sysctl-estimate");
427 if (needed == 0)
428 return;
429 if ((buf = malloc(needed)) == NULL)
430 err(1, "malloc");
431 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
432 err(1, "actual retrieval of routing table");
433 lim = buf + needed;
434 for (next = buf; next < lim; next += rtm->rtm_msglen) {
435 rtm = (struct rt_msghdr *)(void *)next;
436 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
437 sdl = (struct sockaddr_dl *)(void *)
438 (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
439 if (addr) {
440 if (addr != sina->sin_addr.s_addr)
441 continue;
442 found_entry = 1;
444 if (nflag == 0)
445 hp = gethostbyaddr((const char *)(void *)
446 &(sina->sin_addr),
447 sizeof sina->sin_addr, AF_INET);
448 else
449 hp = NULL;
451 host = hp ? hp->h_name : "?";
453 (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
454 if (sdl->sdl_alen)
455 sdl_print(sdl);
456 else
457 (void)printf("(incomplete)");
459 if (sdl->sdl_index) {
460 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
461 (void)printf(" on %s", ifname);
464 if (rtm->rtm_rmx.rmx_expire == 0)
465 (void)printf(" permanent");
466 if (sina->sin_other & SIN_PROXY)
467 (void)printf(" published (proxy only)");
468 if (rtm->rtm_addrs & RTA_NETMASK) {
469 sina = (struct sockaddr_inarp *)(void *)
470 (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
471 if (sina->sin_addr.s_addr == 0xffffffff)
472 (void)printf(" published");
473 if (sina->sin_len != 8)
474 (void)printf("(weird)");
476 (void)printf("\n");
478 free(buf);
482 * Delete the entire arp table
484 void
485 delete_all(void)
487 int mib[6];
488 size_t needed;
489 char addr[sizeof("000.000.000.000\0")];
490 char *lim, *buf, *next;
491 struct rt_msghdr *rtm;
492 struct sockaddr_inarp *sina;
494 mib[0] = CTL_NET;
495 mib[1] = PF_ROUTE;
496 mib[2] = 0;
497 mib[3] = AF_INET;
498 mib[4] = NET_RT_FLAGS;
499 mib[5] = RTF_LLINFO;
500 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
501 err(1, "route-sysctl-estimate");
502 if (needed == 0)
503 return;
504 if ((buf = malloc(needed)) == NULL)
505 err(1, "malloc");
506 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
507 err(1, "actual retrieval of routing table");
508 lim = buf + needed;
509 for (next = buf; next < lim; next += rtm->rtm_msglen) {
510 rtm = (struct rt_msghdr *)(void *)next;
511 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
512 (void)snprintf(addr, sizeof(addr), "%s",
513 inet_ntoa(sina->sin_addr));
514 (void)delete(addr, NULL);
516 free(buf);
519 void
520 sdl_print(const struct sockaddr_dl *sdl)
522 char hbuf[NI_MAXHOST];
524 if (getnameinfo((const struct sockaddr *)(const void *)sdl,
525 (socklen_t)sdl->sdl_len,
526 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
527 (void)printf("<invalid>");
528 else
529 (void)printf("%s", hbuf);
532 static int
533 atosdl(const char *ss, struct sockaddr_dl *sdl)
535 int i;
536 unsigned long b;
537 char *endp;
538 char *p;
539 char *t, *r;
541 p = LLADDR(sdl);
542 endp = ((char *)(void *)sdl) + sdl->sdl_len;
543 i = 0;
545 b = strtoul(ss, &t, 16);
546 if (b > 255 || t == ss)
547 return 1;
549 *p++ = (char)b;
550 ++i;
551 while ((p < endp) && (*t++ == ':')) {
552 b = strtoul(t, &r, 16);
553 if (b > 255 || r == t)
554 break;
555 *p++ = (char)b;
556 ++i;
557 t = r;
559 sdl->sdl_alen = i;
561 return 0;
564 static void
565 usage(void)
567 const char *progname;
569 progname = getprogname();
570 (void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
571 (void)fprintf(stderr, " %s [-nv] -a\n", progname);
572 (void)fprintf(stderr, " %s [-v] -d [-a|hostname [pub [proxy]]]\n",
573 progname);
574 (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub [proxy]]\n",
575 progname);
576 (void)fprintf(stderr, " %s -f filename\n", progname);
577 exit(1);
580 static int
581 rtmsg(int cmd)
583 static int seq;
584 struct rt_msghdr *rtm;
585 char *cp;
586 int l;
588 rtm = &m_rtmsg.m_rtm;
589 cp = m_rtmsg.m_space;
590 errno = 0;
592 if (cmd == RTM_DELETE)
593 goto doit;
594 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
595 rtm->rtm_flags = flags;
596 rtm->rtm_version = RTM_VERSION;
598 switch (cmd) {
599 default:
600 errx(1, "internal wrong cmd");
601 /*NOTREACHED*/
602 case RTM_ADD:
603 rtm->rtm_addrs |= RTA_GATEWAY;
604 rtm->rtm_rmx.rmx_expire = expire_time;
605 rtm->rtm_inits = RTV_EXPIRE;
606 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
607 sin_m.sin_other = 0;
608 if (doing_proxy) {
609 if (export_only)
610 sin_m.sin_other = SIN_PROXY;
611 else {
612 rtm->rtm_addrs |= RTA_NETMASK;
613 rtm->rtm_flags &= ~RTF_HOST;
616 /* FALLTHROUGH */
617 case RTM_GET:
618 rtm->rtm_addrs |= RTA_DST;
621 #define NEXTADDR(w, s) \
622 if (rtm->rtm_addrs & (w)) { \
623 (void)memcpy(cp, &s, \
624 (size_t)((struct sockaddr *)(void *)&s)->sa_len); \
625 RT_ADVANCE(cp, ((struct sockaddr *)(void *)&s)); \
628 NEXTADDR(RTA_DST, sin_m);
629 NEXTADDR(RTA_GATEWAY, sdl_m);
630 NEXTADDR(RTA_NETMASK, so_mask);
632 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
633 doit:
634 l = rtm->rtm_msglen;
635 rtm->rtm_seq = ++seq;
636 rtm->rtm_type = cmd;
637 if (write(s, &m_rtmsg, (size_t)l) < 0) {
638 if (errno != ESRCH || cmd != RTM_DELETE) {
639 warn("writing to routing socket");
640 return (-1);
643 do {
644 l = read(s, &m_rtmsg, sizeof(m_rtmsg));
645 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
646 if (l < 0)
647 warn("read from routing socket");
648 return (0);
651 static int
652 getinetaddr(const char *host, struct in_addr *inap)
654 struct hostent *hp;
656 if (inet_aton(host, inap) == 1)
657 return (0);
658 if ((hp = gethostbyname(host)) == NULL) {
659 warnx("%s: %s", host, hstrerror(h_errno));
660 return (-1);
662 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
663 return (0);
666 static int
667 getifname(u_int16_t ifindex, char *ifname, size_t l)
669 int i;
670 struct ifaddrs *addr;
671 const struct sockaddr_dl *sdl = NULL;
673 if (ifaddrs == NULL) {
674 i = getifaddrs(&ifaddrs);
675 if (i != 0)
676 err(1, "getifaddrs");
679 for (addr = ifaddrs; addr; addr = addr->ifa_next) {
680 if (addr->ifa_addr == NULL ||
681 addr->ifa_addr->sa_family != AF_LINK)
682 continue;
684 sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
685 if (sdl && sdl->sdl_index == ifindex) {
686 (void) strlcpy(ifname, addr->ifa_name, l);
687 return 0;
691 return -1;