arp.c revision 1.49.12.1 1 /* $NetBSD: arp.c,v 1.49.12.1 2013/06/23 06:29:03 tls 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. 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.
21 *
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.
33 */
34
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 */
40
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.49.12.1 2013/06/23 06:29:03 tls Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * arp - display, set, and delete arp table entries
51 */
52
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>
58
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>
67
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>
78
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;
93
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
102 }
103 };
104 static struct sockaddr_inarp blank_sin = {
105 .sin_len = sizeof(blank_sin),
106 .sin_family = AF_INET
107 };
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
112 };
113 static struct sockaddr_dl sdl_m;
114
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;
120
121 int
122 main(int argc, char **argv)
123 {
124 int ch;
125 int op = 0;
126
127 setprogname(argv[0]);
128
129 pid = getpid();
130
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();
151 }
152 argc -= optind;
153 argv += optind;
154
155 if (!op && aflag)
156 op = 'a';
157
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]);
169 }
170 break;
171 case 's':
172 if (argc < 2 || argc > 7)
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;
184 }
185 return (0);
186 }
187
188 /*
189 * Process a file to set standard arp entries
190 */
191 static int
192 file(const char *name)
193 {
194 char *line, *argv[5];
195 int i, retval;
196 FILE *fp;
197
198 if (!strcmp(name, "-")) {
199 fp = stdin;
200 } else {
201 fp = fopen(name, "r");
202 if (fp == NULL) {
203 err(1, "Cannot open %s", name);
204 }
205 }
206 retval = 0;
207 for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
208 char **ap, *inputstring;
209
210 inputstring = line;
211 for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
212 (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
213 if (**ap != '\0')
214 ap++;
215 }
216 i = ap - argv;
217 if (i < 2) {
218 warnx("bad line: %s", line);
219 retval = 1;
220 continue;
221 }
222 if (set(i, argv))
223 retval = 1;
224 }
225 if (fp != stdin)
226 (void)fclose(fp);
227 return retval;
228 }
229
230 static void
231 getsocket(void)
232 {
233 if (s >= 0)
234 return;
235 s = socket(PF_ROUTE, SOCK_RAW, 0);
236 if (s < 0)
237 err(1, "socket");
238 }
239
240 static int
241 getlink(const char *name, struct sockaddr_dl *sdl)
242 {
243 struct ifaddrs *ifap, *ifa;
244
245 if (getifaddrs(&ifap) != 0) {
246 warn("getifaddrs");
247 return 0;
248 }
249
250 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
251 if (ifa->ifa_addr->sa_family != AF_LINK)
252 continue;
253 if (strcmp(ifa->ifa_name, name) != 0)
254 continue;
255 memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
256 freeifaddrs(ifap);
257 return 1;
258 }
259 freeifaddrs(ifap);
260 return 0;
261 }
262
263 /*
264 * Set an individual arp entry
265 */
266 static int
267 set(int argc, char **argv)
268 {
269 struct sockaddr_inarp *sina;
270 struct sockaddr_dl *sdl;
271 struct rt_msghdr *rtm;
272 char *host = argv[0], *eaddr;
273 int rval;
274
275 sina = &sin_m;
276 rtm = &(m_rtmsg.m_rtm);
277 eaddr = argv[1];
278
279 getsocket();
280 argc -= 2;
281 argv += 2;
282 sdl_m = blank_sdl; /* struct copy */
283 sin_m = blank_sin; /* struct copy */
284 if (getinetaddr(host, &sina->sin_addr) == -1)
285 return (1);
286 if (atosdl(eaddr, &sdl_m))
287 warnx("invalid link-level address '%s'", eaddr);
288 doing_proxy = flags = export_only = expire_time = 0;
289 for (; argc-- > 0; argv++) {
290 if (strncmp(argv[0], "temp", 4) == 0) {
291 struct timeval timev;
292 (void)gettimeofday(&timev, 0);
293 expire_time = timev.tv_sec + 20 * 60;
294 }
295 else if (strncmp(argv[0], "pub", 3) == 0) {
296 flags |= RTF_ANNOUNCE;
297 doing_proxy = SIN_PROXY;
298 if (argc && strncmp(argv[1], "pro", 3) == 0) {
299 export_only = 1;
300 argc--; argv++;
301 }
302 } else if (strncmp(argv[0], "trail", 5) == 0) {
303 warnx("%s: Sending trailers is no longer supported",
304 host);
305 } else if (strcmp(argv[0], "ifscope") == 0) {
306 if (argc == 0) {
307 warnx("missing interface for ifscope");
308 continue;
309 }
310 argc--;
311 argv++;
312 if (!getlink(argv[0], &sdl_m))
313 warnx("cannot get link address for %s", argv[0]);
314 }
315
316 }
317 if (memcmp(&sdl_m, &blank_sdl, sizeof(blank_sdl)))
318 goto out;
319 tryagain:
320 if (rtmsg(RTM_GET) < 0) {
321 warn("%s", host);
322 return (1);
323 }
324 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
325 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
326 (char *)(void *)sina);
327 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
328 if (is_llinfo(sdl, rtm->rtm_flags))
329 goto overwrite;
330 if (doing_proxy == 0) {
331 warnx("set: can only proxy for %s", host);
332 return (1);
333 }
334 if (sin_m.sin_other & SIN_PROXY) {
335 warnx("set: proxy entry exists for non 802 device");
336 return (1);
337 }
338 sin_m.sin_other = SIN_PROXY;
339 export_only = 1;
340 goto tryagain;
341 }
342 overwrite:
343 if (sdl->sdl_family != AF_LINK) {
344 warnx("cannot intuit interface index and type for %s",
345 host);
346 return (1);
347 }
348 sdl_m.sdl_type = sdl->sdl_type;
349 sdl_m.sdl_index = sdl->sdl_index;
350 out:
351 rval = rtmsg(RTM_ADD);
352 if (vflag)
353 (void)printf("%s (%s) added\n", host, eaddr);
354 return (rval);
355 }
356
357 /*
358 * Display an individual arp entry
359 */
360 static void
361 get(const char *host)
362 {
363 struct sockaddr_inarp *sina;
364
365 sina = &sin_m;
366 sin_m = blank_sin; /* struct copy */
367 if (getinetaddr(host, &sina->sin_addr) == -1)
368 exit(1);
369 dump(sina->sin_addr.s_addr);
370 if (found_entry == 0)
371 errx(1, "%s (%s) -- no entry", host, inet_ntoa(sina->sin_addr));
372 }
373
374
375 static int
376 is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
377 {
378 if (sdl->sdl_family != AF_LINK ||
379 (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
380 return 0;
381
382 switch (sdl->sdl_type) {
383 case IFT_ETHER:
384 case IFT_FDDI:
385 case IFT_ISO88023:
386 case IFT_ISO88024:
387 case IFT_ISO88025:
388 case IFT_ARCNET:
389 return 1;
390 default:
391 return 0;
392 }
393 }
394
395 /*
396 * Delete an arp entry
397 */
398 int
399 delete(const char *host, const char *info)
400 {
401 struct sockaddr_inarp *sina;
402 struct rt_msghdr *rtm;
403 struct sockaddr_dl *sdl;
404
405 sina = &sin_m;
406 rtm = &m_rtmsg.m_rtm;
407
408 getsocket();
409 sin_m = blank_sin; /* struct copy */
410 if (info && strncmp(info, "pro", 3) == 0)
411 sina->sin_other = SIN_PROXY;
412 if (getinetaddr(host, &sina->sin_addr) == -1)
413 return (1);
414 tryagain:
415 if (rtmsg(RTM_GET) < 0) {
416 warn("%s", host);
417 return (1);
418 }
419 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
420 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
421 (char *)(void *)sina);
422 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
423 is_llinfo(sdl, rtm->rtm_flags))
424 goto delete;
425 if (sin_m.sin_other & SIN_PROXY) {
426 warnx("delete: can't locate %s", host);
427 return (1);
428 } else {
429 sin_m.sin_other = SIN_PROXY;
430 goto tryagain;
431 }
432 delete:
433 if (sdl->sdl_family != AF_LINK) {
434 (void)warnx("cannot locate %s", host);
435 return (1);
436 }
437 if (rtmsg(RTM_DELETE))
438 return (1);
439 if (vflag)
440 (void)printf("%s (%s) deleted\n", host,
441 inet_ntoa(sina->sin_addr));
442 return (0);
443 }
444
445 /*
446 * Dump the entire arp table
447 */
448 void
449 dump(uint32_t addr)
450 {
451 int mib[6];
452 size_t needed;
453 char ifname[IFNAMSIZ];
454 char *lim, *buf, *next;
455 const char *host;
456 struct rt_msghdr *rtm;
457 struct sockaddr_inarp *sina;
458 struct sockaddr_dl *sdl;
459 struct hostent *hp;
460
461 mib[0] = CTL_NET;
462 mib[1] = PF_ROUTE;
463 mib[2] = 0;
464 mib[3] = AF_INET;
465 mib[4] = NET_RT_FLAGS;
466 mib[5] = RTF_LLINFO;
467 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
468 err(1, "route-sysctl-estimate");
469 if (needed == 0)
470 return;
471 if ((buf = malloc(needed)) == NULL)
472 err(1, "malloc");
473 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
474 err(1, "actual retrieval of routing table");
475 lim = buf + needed;
476 for (next = buf; next < lim; next += rtm->rtm_msglen) {
477 rtm = (struct rt_msghdr *)(void *)next;
478 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
479 sdl = (struct sockaddr_dl *)(void *)
480 (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
481 if (addr) {
482 if (addr != sina->sin_addr.s_addr)
483 continue;
484 found_entry = 1;
485 }
486 if (nflag == 0)
487 hp = gethostbyaddr((const char *)(void *)
488 &(sina->sin_addr),
489 sizeof sina->sin_addr, AF_INET);
490 else
491 hp = NULL;
492
493 host = hp ? hp->h_name : "?";
494
495 (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
496 if (sdl->sdl_alen)
497 sdl_print(sdl);
498 else
499 (void)printf("(incomplete)");
500
501 if (sdl->sdl_index) {
502 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
503 (void)printf(" on %s", ifname);
504 }
505
506 if (rtm->rtm_rmx.rmx_expire == 0)
507 (void)printf(" permanent");
508 if (sina->sin_other & SIN_PROXY)
509 (void)printf(" published (proxy only)");
510 if (rtm->rtm_addrs & RTA_NETMASK) {
511 sina = (struct sockaddr_inarp *)(void *)
512 (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
513 if (sina->sin_addr.s_addr == 0xffffffff)
514 (void)printf(" published");
515 if (sina->sin_len != 8)
516 (void)printf("(weird)");
517 }
518 (void)printf("\n");
519 }
520 free(buf);
521 }
522
523 /*
524 * Delete the entire arp table
525 */
526 void
527 delete_all(void)
528 {
529 int mib[6];
530 size_t needed;
531 char addr[sizeof("000.000.000.000\0")];
532 char *lim, *buf, *next;
533 struct rt_msghdr *rtm;
534 struct sockaddr_inarp *sina;
535
536 mib[0] = CTL_NET;
537 mib[1] = PF_ROUTE;
538 mib[2] = 0;
539 mib[3] = AF_INET;
540 mib[4] = NET_RT_FLAGS;
541 mib[5] = RTF_LLINFO;
542 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
543 err(1, "route-sysctl-estimate");
544 if (needed == 0)
545 return;
546 if ((buf = malloc(needed)) == NULL)
547 err(1, "malloc");
548 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
549 err(1, "actual retrieval of routing table");
550 lim = buf + needed;
551 for (next = buf; next < lim; next += rtm->rtm_msglen) {
552 rtm = (struct rt_msghdr *)(void *)next;
553 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
554 (void)snprintf(addr, sizeof(addr), "%s",
555 inet_ntoa(sina->sin_addr));
556 (void)delete(addr, NULL);
557 }
558 free(buf);
559 }
560
561 void
562 sdl_print(const struct sockaddr_dl *sdl)
563 {
564 char hbuf[NI_MAXHOST];
565
566 if (getnameinfo((const struct sockaddr *)(const void *)sdl,
567 (socklen_t)sdl->sdl_len,
568 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
569 (void)printf("<invalid>");
570 else
571 (void)printf("%s", hbuf);
572 }
573
574 static int
575 atosdl(const char *ss, struct sockaddr_dl *sdl)
576 {
577 int i;
578 unsigned long b;
579 char *endp;
580 char *p;
581 char *t, *r;
582
583 p = LLADDR(sdl);
584 endp = ((char *)(void *)sdl) + sdl->sdl_len;
585 i = 0;
586
587 b = strtoul(ss, &t, 16);
588 if (b > 255 || t == ss)
589 return 1;
590
591 *p++ = (char)b;
592 ++i;
593 while ((p < endp) && (*t++ == ':')) {
594 b = strtoul(t, &r, 16);
595 if (b > 255 || r == t)
596 break;
597 *p++ = (char)b;
598 ++i;
599 t = r;
600 }
601 sdl->sdl_alen = i;
602
603 return 0;
604 }
605
606 static void
607 usage(void)
608 {
609 const char *progname;
610
611 progname = getprogname();
612 (void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
613 (void)fprintf(stderr, " %s [-nv] -a\n", progname);
614 (void)fprintf(stderr, " %s [-v] -d [-a|hostname [pub [proxy]]]\n",
615 progname);
616 (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub [proxy]]\n",
617 progname);
618 (void)fprintf(stderr, " %s -f filename\n", progname);
619 exit(1);
620 }
621
622 static int
623 rtmsg(int cmd)
624 {
625 static int seq;
626 struct rt_msghdr *rtm;
627 char *cp;
628 int l;
629
630 rtm = &m_rtmsg.m_rtm;
631 cp = m_rtmsg.m_space;
632 errno = 0;
633
634 if (cmd == RTM_DELETE)
635 goto doit;
636 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
637 rtm->rtm_flags = flags;
638 rtm->rtm_version = RTM_VERSION;
639
640 switch (cmd) {
641 default:
642 errx(1, "internal wrong cmd");
643 /*NOTREACHED*/
644 case RTM_ADD:
645 rtm->rtm_addrs |= RTA_GATEWAY;
646 rtm->rtm_rmx.rmx_expire = expire_time;
647 rtm->rtm_inits = RTV_EXPIRE;
648 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
649 sin_m.sin_other = 0;
650 if (doing_proxy) {
651 if (export_only)
652 sin_m.sin_other = SIN_PROXY;
653 else {
654 rtm->rtm_addrs |= RTA_NETMASK;
655 rtm->rtm_flags &= ~RTF_HOST;
656 }
657 }
658 /* FALLTHROUGH */
659 case RTM_GET:
660 rtm->rtm_addrs |= RTA_DST;
661 }
662
663 #define NEXTADDR(w, s) \
664 if (rtm->rtm_addrs & (w)) { \
665 (void)memcpy(cp, &s, \
666 (size_t)((struct sockaddr *)(void *)&s)->sa_len); \
667 RT_ADVANCE(cp, ((struct sockaddr *)(void *)&s)); \
668 }
669
670 NEXTADDR(RTA_DST, sin_m);
671 NEXTADDR(RTA_GATEWAY, sdl_m);
672 NEXTADDR(RTA_NETMASK, so_mask);
673
674 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
675 doit:
676 l = rtm->rtm_msglen;
677 rtm->rtm_seq = ++seq;
678 rtm->rtm_type = cmd;
679 if (write(s, &m_rtmsg, (size_t)l) < 0) {
680 if (errno != ESRCH || cmd != RTM_DELETE) {
681 warn("writing to routing socket");
682 return (-1);
683 }
684 }
685 do {
686 l = read(s, &m_rtmsg, sizeof(m_rtmsg));
687 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
688 if (l < 0)
689 warn("read from routing socket");
690 return (0);
691 }
692
693 static int
694 getinetaddr(const char *host, struct in_addr *inap)
695 {
696 struct hostent *hp;
697
698 if (inet_aton(host, inap) == 1)
699 return (0);
700 if ((hp = gethostbyname(host)) == NULL) {
701 warnx("%s: %s", host, hstrerror(h_errno));
702 return (-1);
703 }
704 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
705 return (0);
706 }
707
708 static int
709 getifname(u_int16_t ifindex, char *ifname, size_t l)
710 {
711 int i;
712 struct ifaddrs *addr;
713 const struct sockaddr_dl *sdl = NULL;
714
715 if (ifaddrs == NULL) {
716 i = getifaddrs(&ifaddrs);
717 if (i != 0)
718 err(1, "getifaddrs");
719 }
720
721 for (addr = ifaddrs; addr; addr = addr->ifa_next) {
722 if (addr->ifa_addr == NULL ||
723 addr->ifa_addr->sa_family != AF_LINK)
724 continue;
725
726 sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
727 if (sdl && sdl->sdl_index == ifindex) {
728 (void) strlcpy(ifname, addr->ifa_name, l);
729 return 0;
730 }
731 }
732
733 return -1;
734 }
735