arp.c revision 1.45 1 /* $NetBSD: arp.c,v 1.45 2007/12/15 19:44:54 perry 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\n\
38 The Regents of the University of California. All rights reserved.\n");
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.45 2007/12/15 19:44:54 perry Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * arp - display, set, and delete arp table entries
51 */
52
53 /* Roundup the same way rt_xaddrs does */
54 #define ROUNDUP(a) \
55 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
56
57 #include <sys/param.h>
58 #include <sys/file.h>
59 #include <sys/socket.h>
60 #include <sys/sysctl.h>
61 #include <sys/ioctl.h>
62
63 #include <net/if.h>
64 #include <net/if_dl.h>
65 #include <net/if_ether.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/if_inarp.h>
70 #include <arpa/inet.h>
71
72 #include <err.h>
73 #include <errno.h>
74 #include <netdb.h>
75 #include <nlist.h>
76 #include <paths.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81 #include <ifaddrs.h>
82
83 static int is_llinfo(const struct sockaddr_dl *, int);
84 static int delete(const char *, const char *);
85 static void dump(uint32_t);
86 static void delete_all(void);
87 static void sdl_print(const struct sockaddr_dl *);
88 static int getifname(u_int16_t, char *, size_t);
89 static int atosdl(const char *s, struct sockaddr_dl *sdl);
90 static int file(const char *);
91 static void get(const char *);
92 static int getinetaddr(const char *, struct in_addr *);
93 static void getsocket(void);
94 static int rtmsg(int);
95 static int set(int, char **);
96 static void usage(void) __dead;
97
98 static pid_t pid;
99 static int aflag, nflag, vflag;
100 static int s = -1;
101 static struct ifaddrs* ifaddrs = NULL;
102 static struct sockaddr_in so_mask = {
103 .sin_len = 8,
104 .sin_addr = {
105 .s_addr = 0xffffffff
106 }
107 };
108 static struct sockaddr_inarp blank_sin = {
109 .sin_len = sizeof(blank_sin),
110 .sin_family = AF_INET
111 };
112 static struct sockaddr_inarp sin_m;
113 static struct sockaddr_dl blank_sdl = {
114 .sdl_len = sizeof(blank_sdl),
115 .sdl_family = AF_LINK
116 };
117 static struct sockaddr_dl sdl_m;
118
119 static int expire_time, flags, export_only, doing_proxy, found_entry;
120 static struct {
121 struct rt_msghdr m_rtm;
122 char m_space[512];
123 } m_rtmsg;
124
125 int
126 main(int argc, char **argv)
127 {
128 int ch;
129 int op = 0;
130
131 setprogname(argv[0]);
132
133 pid = getpid();
134
135 while ((ch = getopt(argc, argv, "andsfv")) != -1)
136 switch((char)ch) {
137 case 'a':
138 aflag = 1;
139 break;
140 case 'd':
141 case 's':
142 case 'f':
143 if (op)
144 usage();
145 op = ch;
146 break;
147 case 'n':
148 nflag = 1;
149 break;
150 case 'v':
151 vflag = 1;
152 break;
153 default:
154 usage();
155 }
156 argc -= optind;
157 argv += optind;
158
159 if (!op && aflag)
160 op = 'a';
161
162 switch((char)op) {
163 case 'a':
164 dump(0);
165 break;
166 case 'd':
167 if (aflag && argc == 0)
168 delete_all();
169 else {
170 if (aflag || argc < 1 || argc > 2)
171 usage();
172 (void)delete(argv[0], argv[1]);
173 }
174 break;
175 case 's':
176 if (argc < 2 || argc > 5)
177 usage();
178 return (set(argc, argv) ? 1 : 0);
179 case 'f':
180 if (argc != 1)
181 usage();
182 return (file(argv[0]));
183 default:
184 if (argc != 1)
185 usage();
186 get(argv[0]);
187 break;
188 }
189 return (0);
190 }
191
192 /*
193 * Process a file to set standard arp entries
194 */
195 static int
196 file(const char *name)
197 {
198 char *line, *argv[5];
199 int i, retval;
200 FILE *fp;
201
202 if ((fp = fopen(name, "r")) == NULL)
203 err(1, "cannot open %s", name);
204 retval = 0;
205 for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
206 char **ap, *inputstring;
207
208 inputstring = line;
209 for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
210 (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
211 if (**ap != '\0')
212 ap++;
213 }
214 i = ap - argv;
215 if (i < 2) {
216 warnx("bad line: %s", line);
217 retval = 1;
218 continue;
219 }
220 if (set(i, argv))
221 retval = 1;
222 }
223 (void)fclose(fp);
224 return retval;
225 }
226
227 static void
228 getsocket(void)
229 {
230 if (s >= 0)
231 return;
232 s = socket(PF_ROUTE, SOCK_RAW, 0);
233 if (s < 0)
234 err(1, "socket");
235 }
236
237 /*
238 * Set an individual arp entry
239 */
240 static int
241 set(int argc, char **argv)
242 {
243 struct sockaddr_inarp *sina;
244 struct sockaddr_dl *sdl;
245 struct rt_msghdr *rtm;
246 char *host = argv[0], *eaddr;
247 int rval;
248
249 sina = &sin_m;
250 rtm = &(m_rtmsg.m_rtm);
251 eaddr = argv[1];
252
253 getsocket();
254 argc -= 2;
255 argv += 2;
256 sdl_m = blank_sdl; /* struct copy */
257 sin_m = blank_sin; /* struct copy */
258 if (getinetaddr(host, &sina->sin_addr) == -1)
259 return (1);
260 if (atosdl(eaddr, &sdl_m))
261 warnx("invalid link-level address '%s'", eaddr);
262 doing_proxy = flags = export_only = expire_time = 0;
263 while (argc-- > 0) {
264 if (strncmp(argv[0], "temp", 4) == 0) {
265 struct timeval timev;
266 (void)gettimeofday(&timev, 0);
267 expire_time = timev.tv_sec + 20 * 60;
268 }
269 else if (strncmp(argv[0], "pub", 3) == 0) {
270 flags |= RTF_ANNOUNCE;
271 doing_proxy = SIN_PROXY;
272 } else if (strncmp(argv[0], "trail", 5) == 0) {
273 warnx("%s: Sending trailers is no longer supported",
274 host);
275 }
276 argv++;
277 }
278 tryagain:
279 if (rtmsg(RTM_GET) < 0) {
280 warn("%s", host);
281 return (1);
282 }
283 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
284 sdl = (struct sockaddr_dl *)(void *)(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);
292 }
293 if (sin_m.sin_other & SIN_PROXY) {
294 warnx("set: proxy entry exists for non 802 device");
295 return (1);
296 }
297 sin_m.sin_other = SIN_PROXY;
298 export_only = 1;
299 goto tryagain;
300 }
301 overwrite:
302 if (sdl->sdl_family != AF_LINK) {
303 warnx("cannot intuit interface index and type for %s",
304 host);
305 return (1);
306 }
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);
313 }
314
315 /*
316 * Display an individual arp entry
317 */
318 static void
319 get(const char *host)
320 {
321 struct sockaddr_inarp *sina;
322
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));
330 }
331
332
333 static int
334 is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
335 {
336 if (sdl->sdl_family != AF_LINK ||
337 (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
338 return 0;
339
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;
350 }
351 }
352
353 /*
354 * Delete an arp entry
355 */
356 int
357 delete(const char *host, const char *info)
358 {
359 struct sockaddr_inarp *sina;
360 struct rt_msghdr *rtm;
361 struct sockaddr_dl *sdl;
362
363 sina = &sin_m;
364 rtm = &m_rtmsg.m_rtm;
365
366 if (info && strncmp(info, "pub", 3) == 0)
367 export_only = 1;
368 getsocket();
369 sin_m = blank_sin; /* struct copy */
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);
376 }
377 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
378 sdl = (struct sockaddr_dl *)(void *)(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;
389 }
390 delete:
391 if (sdl->sdl_family != AF_LINK) {
392 (void)warnx("cannot locate %s", host);
393 return (1);
394 }
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);
401 }
402
403 /*
404 * Dump the entire arp table
405 */
406 void
407 dump(uint32_t addr)
408 {
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;
418
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 (ROUNDUP(sina->sin_len) + (char *)(void *)sina);
439 if (addr) {
440 if (addr != sina->sin_addr.s_addr)
441 continue;
442 found_entry = 1;
443 }
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;
450
451 host = hp ? hp->h_name : "?";
452
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)");
458
459 if (sdl->sdl_index) {
460 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
461 (void)printf(" on %s", ifname);
462 }
463
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 (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)");
475 }
476 (void)printf("\n");
477 }
478 free(buf);
479 }
480
481 /*
482 * Delete the entire arp table
483 */
484 void
485 delete_all(void)
486 {
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;
493
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);
515 }
516 free(buf);
517 }
518
519 void
520 sdl_print(const struct sockaddr_dl *sdl)
521 {
522 char hbuf[NI_MAXHOST];
523
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);
530 }
531
532 static int
533 atosdl(const char *ss, struct sockaddr_dl *sdl)
534 {
535 int i;
536 unsigned long b;
537 char *endp;
538 char *p;
539 char *t, *r;
540
541 p = LLADDR(sdl);
542 endp = ((char *)(void *)sdl) + sdl->sdl_len;
543 i = 0;
544
545 b = strtoul(ss, &t, 16);
546 if (b > 255 || t == ss)
547 return 1;
548
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;
558 }
559 sdl->sdl_alen = i;
560
561 return 0;
562 }
563
564 static void
565 usage(void)
566 {
567 const char *progname;
568
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]]\n",
573 progname);
574 (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub]\n",
575 progname);
576 (void)fprintf(stderr, " %s -f filename\n", progname);
577 exit(1);
578 }
579
580 static int
581 rtmsg(int cmd)
582 {
583 static int seq;
584 struct rt_msghdr *rtm;
585 char *cp;
586 int l;
587
588 rtm = &m_rtmsg.m_rtm;
589 cp = m_rtmsg.m_space;
590 errno = 0;
591
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;
597
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;
614 }
615 }
616 /* FALLTHROUGH */
617 case RTM_GET:
618 rtm->rtm_addrs |= RTA_DST;
619 }
620
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 cp += ROUNDUP(((struct sockaddr *)(void *)&s)->sa_len); \
626 }
627
628 NEXTADDR(RTA_DST, sin_m);
629 NEXTADDR(RTA_GATEWAY, sdl_m);
630 NEXTADDR(RTA_NETMASK, so_mask);
631
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);
641 }
642 }
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);
649 }
650
651 static int
652 getinetaddr(const char *host, struct in_addr *inap)
653 {
654 struct hostent *hp;
655
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);
661 }
662 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
663 return (0);
664 }
665
666 static int
667 getifname(u_int16_t ifindex, char *ifname, size_t l)
668 {
669 int i;
670 struct ifaddrs *addr;
671 const struct sockaddr_dl *sdl = NULL;
672
673 if (ifaddrs == NULL) {
674 i = getifaddrs(&ifaddrs);
675 if (i != 0)
676 err(1, "getifaddrs");
677 }
678
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;
683
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;
688 }
689 }
690
691 return -1;
692 }
693