arp.c revision 1.34 1 /* $NetBSD: arp.c,v 1.34 2002/03/02 03:45:07 tv 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.34 2002/03/02 03:45:07 tv 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 #include <sys/ioctl.h>
66
67 #include <net/if.h>
68 #include <net/if_dl.h>
69 #include <net/if_ether.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/if_inarp.h>
74 #include <arpa/inet.h>
75
76 #include <err.h>
77 #include <errno.h>
78 #include <netdb.h>
79 #include <nlist.h>
80 #include <paths.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85
86 int delete __P((const char *, const char *));
87 void dump __P((u_long));
88 void delete_all __P((void));
89 void sdl_print __P((const struct sockaddr_dl *));
90 int getifname __P((u_int16_t, char *));
91 int atosdl __P((const char *s, struct sockaddr_dl *sdl));
92 int file __P((char *));
93 void get __P((const char *));
94 int getinetaddr __P((const char *, struct in_addr *));
95 void getsocket __P((void));
96 int main __P((int, char **));
97 int rtmsg __P((int));
98 int set __P((int, char **));
99 void usage __P((void));
100
101 static int pid;
102 static int aflag, nflag, vflag;
103 static int s = -1;
104 static int is = -1;
105
106 static struct ifconf ifc;
107 static char ifconfbuf[8192];
108
109 int
110 main(argc, argv)
111 int argc;
112 char **argv;
113 {
114 int ch;
115 int op = 0;
116
117 setprogname(argv[0]);
118
119 pid = getpid();
120
121 while ((ch = getopt(argc, argv, "andsfv")) != -1)
122 switch((char)ch) {
123 case 'a':
124 aflag = 1;
125 break;
126 case 'd':
127 case 's':
128 case 'f':
129 if (op)
130 usage();
131 op = ch;
132 break;
133 case 'n':
134 nflag = 1;
135 break;
136 case 'v':
137 vflag = 1;
138 break;
139 default:
140 usage();
141 }
142 argc -= optind;
143 argv += optind;
144
145 if (!op && aflag)
146 op = 'a';
147
148 switch((char)op) {
149 case 'a':
150 dump(0);
151 break;
152 case 'd':
153 if (aflag && argc == 0)
154 delete_all();
155 else {
156 if (aflag || argc < 1 || argc > 2)
157 usage();
158 (void)delete(argv[0], argv[1]);
159 }
160 break;
161 case 's':
162 if (argc < 2 || argc > 5)
163 usage();
164 return (set(argc, argv) ? 1 : 0);
165 case 'f':
166 if (argc != 1)
167 usage();
168 return (file(argv[0]));
169 default:
170 if (argc != 1)
171 usage();
172 get(argv[0]);
173 break;
174 }
175 return (0);
176 }
177
178 /*
179 * Process a file to set standard arp entries
180 */
181 int
182 file(name)
183 char *name;
184 {
185 char line[100], arg[5][50], *args[5];
186 int i, retval;
187 FILE *fp;
188
189 if ((fp = fopen(name, "r")) == NULL)
190 err(1, "cannot open %s", name);
191 args[0] = &arg[0][0];
192 args[1] = &arg[1][0];
193 args[2] = &arg[2][0];
194 args[3] = &arg[3][0];
195 args[4] = &arg[4][0];
196 retval = 0;
197 while (fgets(line, 100, fp) != NULL) {
198 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
199 arg[3], arg[4]);
200 if (i < 2) {
201 warnx("bad line: %s", line);
202 retval = 1;
203 continue;
204 }
205 if (set(i, args))
206 retval = 1;
207 }
208 fclose(fp);
209 return (retval);
210 }
211
212 void
213 getsocket()
214 {
215 if (s >= 0)
216 return;
217 s = socket(PF_ROUTE, SOCK_RAW, 0);
218 if (s < 0)
219 err(1, "socket");
220 }
221
222 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
223 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
224 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
225 int expire_time, flags, export_only, doing_proxy, found_entry;
226 struct {
227 struct rt_msghdr m_rtm;
228 char m_space[512];
229 } m_rtmsg;
230
231 /*
232 * Set an individual arp entry
233 */
234 int
235 set(argc, argv)
236 int argc;
237 char **argv;
238 {
239 struct sockaddr_inarp *sin;
240 struct sockaddr_dl *sdl;
241 struct rt_msghdr *rtm;
242 char *host = argv[0], *eaddr;
243 int rval;
244
245 sin = &sin_m;
246 rtm = &(m_rtmsg.m_rtm);
247 eaddr = argv[1];
248
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, &sin->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 time;
262 (void)gettimeofday(&time, 0);
263 expire_time = time.tv_sec + 20 * 60;
264 }
265 else if (strncmp(argv[0], "pub", 3) == 0) {
266 flags |= RTF_ANNOUNCE;
267 doing_proxy = SIN_PROXY;
268 } else if (strncmp(argv[0], "trail", 5) == 0) {
269 (void)printf(
270 "%s: Sending trailers is no longer supported\n",
271 host);
272 }
273 argv++;
274 }
275 tryagain:
276 if (rtmsg(RTM_GET) < 0) {
277 warn("%s", host);
278 return (1);
279 }
280 sin = (struct sockaddr_inarp *)(rtm + 1);
281 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
282 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
283 if (sdl->sdl_family == AF_LINK &&
284 (rtm->rtm_flags & RTF_LLINFO) &&
285 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
286 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
287 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
288 goto overwrite;
289 }
290 if (doing_proxy == 0) {
291 (void)printf("set: can only proxy for %s\n", host);
292 return (1);
293 }
294 if (sin_m.sin_other & SIN_PROXY) {
295 (void)printf(
296 "set: proxy entry exists for non 802 device\n");
297 return (1);
298 }
299 sin_m.sin_other = SIN_PROXY;
300 export_only = 1;
301 goto tryagain;
302 }
303 overwrite:
304 if (sdl->sdl_family != AF_LINK) {
305 (void)printf("cannot intuit interface index and type for %s\n",
306 host);
307 return (1);
308 }
309 sdl_m.sdl_type = sdl->sdl_type;
310 sdl_m.sdl_index = sdl->sdl_index;
311 rval = rtmsg(RTM_ADD);
312 if (vflag)
313 (void)printf("%s (%s) added\n", host, eaddr);
314 return (rval);
315 }
316
317 /*
318 * Display an individual arp entry
319 */
320 void
321 get(host)
322 const char *host;
323 {
324 struct sockaddr_inarp *sin;
325
326 sin = &sin_m;
327 sin_m = blank_sin; /* struct copy */
328 if (getinetaddr(host, &sin->sin_addr) == -1)
329 exit(1);
330 dump(sin->sin_addr.s_addr);
331 if (found_entry == 0) {
332 (void)printf("%s (%s) -- no entry\n", host,
333 inet_ntoa(sin->sin_addr));
334 exit(1);
335 }
336 }
337
338 /*
339 * Delete an arp entry
340 */
341 int
342 delete(host, info)
343 const char *host;
344 const char *info;
345 {
346 struct sockaddr_inarp *sin;
347 struct rt_msghdr *rtm;
348 struct sockaddr_dl *sdl;
349
350 sin = &sin_m;
351 rtm = &m_rtmsg.m_rtm;
352
353 if (info && strncmp(info, "pro", 3) )
354 export_only = 1;
355 getsocket();
356 sin_m = blank_sin; /* struct copy */
357 if (getinetaddr(host, &sin->sin_addr) == -1)
358 return (1);
359 tryagain:
360 if (rtmsg(RTM_GET) < 0) {
361 warn("%s", host);
362 return (1);
363 }
364 sin = (struct sockaddr_inarp *)(rtm + 1);
365 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
366 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
367 if (sdl->sdl_family == AF_LINK &&
368 (rtm->rtm_flags & RTF_LLINFO) &&
369 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
370 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
371 case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
372 goto delete;
373 }
374 }
375 if (sin_m.sin_other & SIN_PROXY) {
376 warnx("delete: can't locate %s", host);
377 return (1);
378 } else {
379 sin_m.sin_other = SIN_PROXY;
380 goto tryagain;
381 }
382 delete:
383 if (sdl->sdl_family != AF_LINK) {
384 (void)printf("cannot locate %s\n", host);
385 return (1);
386 }
387 if (rtmsg(RTM_DELETE))
388 return (1);
389 if (vflag)
390 (void)printf("%s (%s) deleted\n", host,
391 inet_ntoa(sin->sin_addr));
392 return (0);
393 }
394
395 /*
396 * Dump the entire arp table
397 */
398 void
399 dump(addr)
400 u_long addr;
401 {
402 int mib[6];
403 size_t needed;
404 char ifname[IFNAMSIZ];
405 char *host, *lim, *buf, *next;
406 struct rt_msghdr *rtm;
407 struct sockaddr_inarp *sin;
408 struct sockaddr_dl *sdl;
409 struct hostent *hp;
410
411 mib[0] = CTL_NET;
412 mib[1] = PF_ROUTE;
413 mib[2] = 0;
414 mib[3] = AF_INET;
415 mib[4] = NET_RT_FLAGS;
416 mib[5] = RTF_LLINFO;
417 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
418 err(1, "route-sysctl-estimate");
419 if (needed == 0)
420 return;
421 if ((buf = malloc(needed)) == NULL)
422 err(1, "malloc");
423 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
424 err(1, "actual retrieval of routing table");
425 lim = buf + needed;
426 for (next = buf; next < lim; next += rtm->rtm_msglen) {
427 rtm = (struct rt_msghdr *)next;
428 sin = (struct sockaddr_inarp *)(rtm + 1);
429 sdl = (struct sockaddr_dl *)
430 (ROUNDUP(sin->sin_len) + (char *)sin);
431 if (addr) {
432 if (addr != sin->sin_addr.s_addr)
433 continue;
434 found_entry = 1;
435 }
436 if (nflag == 0)
437 hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
438 sizeof sin->sin_addr, AF_INET);
439 else
440 hp = NULL;
441
442 host = hp ? hp->h_name : "?";
443
444 (void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
445 if (sdl->sdl_alen)
446 sdl_print(sdl);
447 else
448 (void)printf("(incomplete)");
449
450 if (sdl->sdl_index) {
451 if (getifname(sdl->sdl_index, ifname) == 0)
452 printf(" on %s", ifname);
453 }
454
455 if (rtm->rtm_rmx.rmx_expire == 0)
456 (void)printf(" permanent");
457 if (sin->sin_other & SIN_PROXY)
458 (void)printf(" published (proxy only)");
459 if (rtm->rtm_addrs & RTA_NETMASK) {
460 sin = (struct sockaddr_inarp *)
461 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
462 if (sin->sin_addr.s_addr == 0xffffffff)
463 (void)printf(" published");
464 if (sin->sin_len != 8)
465 (void)printf("(weird)");
466 }
467 (void)printf("\n");
468 }
469 }
470
471 /*
472 * Delete the entire arp table
473 */
474 void
475 delete_all(void)
476 {
477 int mib[6];
478 size_t needed;
479 char addr[sizeof("000.000.000.000\0")];
480 char *lim, *buf, *next;
481 struct rt_msghdr *rtm;
482 struct sockaddr_inarp *sin;
483 struct sockaddr_dl *sdl;
484
485 mib[0] = CTL_NET;
486 mib[1] = PF_ROUTE;
487 mib[2] = 0;
488 mib[3] = AF_INET;
489 mib[4] = NET_RT_FLAGS;
490 mib[5] = RTF_LLINFO;
491 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
492 err(1, "route-sysctl-estimate");
493 if (needed == 0)
494 return;
495 if ((buf = malloc(needed)) == NULL)
496 err(1, "malloc");
497 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
498 err(1, "actual retrieval of routing table");
499 lim = buf + needed;
500 for (next = buf; next < lim; next += rtm->rtm_msglen) {
501 rtm = (struct rt_msghdr *)next;
502 sin = (struct sockaddr_inarp *)(rtm + 1);
503 sdl = (struct sockaddr_dl *)
504 (ROUNDUP(sin->sin_len) + (char *)sin);
505 snprintf(addr, sizeof(addr), "%s", inet_ntoa(sin->sin_addr));
506 delete(addr, NULL);
507 }
508 }
509
510 void
511 sdl_print(sdl)
512 const struct sockaddr_dl *sdl;
513 {
514 char hbuf[NI_MAXHOST];
515
516 if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
517 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
518 printf("<invalid>");
519 else
520 printf("%s", hbuf);
521 }
522
523 int
524 atosdl(s, sdl)
525 const char *s;
526 struct sockaddr_dl *sdl;
527 {
528 int i;
529 long b;
530 caddr_t endp;
531 caddr_t p;
532 char *t, *r;
533
534 p = LLADDR(sdl);
535 endp = ((caddr_t)sdl) + sdl->sdl_len;
536 i = 0;
537
538 b = strtol(s, &t, 16);
539 if (t == s)
540 return 1;
541
542 *p++ = b;
543 ++i;
544 while ((p < endp) && (*t++ == ':')) {
545 b = strtol(t, &r, 16);
546 if (r == t)
547 break;
548 *p++ = b;
549 ++i;
550 t = r;
551 }
552 sdl->sdl_alen = i;
553
554 return 0;
555 }
556
557 void
558 usage()
559 {
560 const char *progname;
561
562 progname = getprogname();
563 (void)fprintf(stderr, "usage: %s [-n] hostname\n", progname);
564 (void)fprintf(stderr, "usage: %s [-n] -a\n", progname);
565 (void)fprintf(stderr, "usage: %s -d [-a|hostname]\n", progname);
566 (void)fprintf(stderr,
567 "usage: %s -s hostname ether_addr [temp] [pub]\n", progname);
568 (void)fprintf(stderr, "usage: %s -f filename\n", progname);
569 exit(1);
570 }
571
572 int
573 rtmsg(cmd)
574 int cmd;
575 {
576 static int seq;
577 int rlen;
578 struct rt_msghdr *rtm;
579 char *cp;
580 int l;
581
582 rtm = &m_rtmsg.m_rtm;
583 cp = m_rtmsg.m_space;
584 errno = 0;
585
586 if (cmd == RTM_DELETE)
587 goto doit;
588 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
589 rtm->rtm_flags = flags;
590 rtm->rtm_version = RTM_VERSION;
591
592 switch (cmd) {
593 default:
594 errx(1, "internal wrong cmd");
595 /*NOTREACHED*/
596 case RTM_ADD:
597 rtm->rtm_addrs |= RTA_GATEWAY;
598 rtm->rtm_rmx.rmx_expire = expire_time;
599 rtm->rtm_inits = RTV_EXPIRE;
600 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
601 sin_m.sin_other = 0;
602 if (doing_proxy) {
603 if (export_only)
604 sin_m.sin_other = SIN_PROXY;
605 else {
606 rtm->rtm_addrs |= RTA_NETMASK;
607 rtm->rtm_flags &= ~RTF_HOST;
608 }
609 }
610 /* FALLTHROUGH */
611 case RTM_GET:
612 rtm->rtm_addrs |= RTA_DST;
613 }
614
615 #define NEXTADDR(w, s) \
616 if (rtm->rtm_addrs & (w)) { \
617 (void)memcpy(cp, &s, ((struct sockaddr *)&s)->sa_len); \
618 cp += ROUNDUP(((struct sockaddr *)&s)->sa_len); \
619 }
620
621 NEXTADDR(RTA_DST, sin_m);
622 NEXTADDR(RTA_GATEWAY, sdl_m);
623 NEXTADDR(RTA_NETMASK, so_mask);
624
625 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
626 doit:
627 l = rtm->rtm_msglen;
628 rtm->rtm_seq = ++seq;
629 rtm->rtm_type = cmd;
630 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
631 if (errno != ESRCH || cmd != RTM_DELETE) {
632 warn("writing to routing socket");
633 return (-1);
634 }
635 }
636 do {
637 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
638 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
639 if (l < 0)
640 warn("read from routing socket");
641 return (0);
642 }
643
644 int
645 getinetaddr(host, inap)
646 const char *host;
647 struct in_addr *inap;
648 {
649 struct hostent *hp;
650
651 if (inet_aton(host, inap) == 1)
652 return (0);
653 if ((hp = gethostbyname(host)) == NULL) {
654 warnx("%s: %s", host, hstrerror(h_errno));
655 return (-1);
656 }
657 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
658 return (0);
659 }
660
661 int
662 getifname(ifindex, ifname)
663 u_int16_t ifindex;
664 char* ifname;
665 {
666 int i, idx, siz;
667 char ifrbuf[8192];
668 struct ifreq ifreq, *ifr;
669 const struct sockaddr_dl *sdl = NULL;
670
671 if (is < 0) {
672 is = socket(PF_INET, SOCK_DGRAM, 0);
673 if (is < 0)
674 err(1, "socket");
675
676 ifc.ifc_len = sizeof(ifconfbuf);
677 ifc.ifc_buf = ifconfbuf;
678
679 if (ioctl(is, SIOCGIFCONF, &ifc) < 0) {
680 close(is);
681 err(1, "SIOCGIFCONF");
682 is = -1;
683 }
684 }
685
686 ifr = ifc.ifc_req;
687 ifreq.ifr_name[0] = '\0';
688 for (i = 0, idx = 0; i < ifc.ifc_len; ) {
689 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
690 siz = sizeof(ifr->ifr_name) +
691 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
692 ? ifr->ifr_addr.sa_len
693 : sizeof(struct sockaddr));
694 i += siz;
695 /* avoid alignment issue */
696 if (sizeof(ifrbuf) < siz)
697 errx(1, "ifr too big");
698
699 memcpy(ifrbuf, ifr, siz);
700 ifr = (struct ifreq *)ifrbuf;
701
702 if (ifr->ifr_addr.sa_family != AF_LINK)
703 continue;
704
705 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
706 if (sdl && sdl->sdl_index == ifindex) {
707 (void) strncpy(ifname, ifr->ifr_name, IFNAMSIZ);
708 return 0;
709 }
710 }
711
712 return -1;
713 }
714