arp.c revision 1.34.2.1 1 /* $NetBSD: arp.c,v 1.34.2.1 2004/03/29 05:38:54 jmc 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.2.1 2004/03/29 05:38:54 jmc 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 free(buf);
470 }
471
472 /*
473 * Delete the entire arp table
474 */
475 void
476 delete_all(void)
477 {
478 int mib[6];
479 size_t needed;
480 char addr[sizeof("000.000.000.000\0")];
481 char *lim, *buf, *next;
482 struct rt_msghdr *rtm;
483 struct sockaddr_inarp *sin;
484 struct sockaddr_dl *sdl;
485
486 mib[0] = CTL_NET;
487 mib[1] = PF_ROUTE;
488 mib[2] = 0;
489 mib[3] = AF_INET;
490 mib[4] = NET_RT_FLAGS;
491 mib[5] = RTF_LLINFO;
492 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
493 err(1, "route-sysctl-estimate");
494 if (needed == 0)
495 return;
496 if ((buf = malloc(needed)) == NULL)
497 err(1, "malloc");
498 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
499 err(1, "actual retrieval of routing table");
500 lim = buf + needed;
501 for (next = buf; next < lim; next += rtm->rtm_msglen) {
502 rtm = (struct rt_msghdr *)next;
503 sin = (struct sockaddr_inarp *)(rtm + 1);
504 sdl = (struct sockaddr_dl *)
505 (ROUNDUP(sin->sin_len) + (char *)sin);
506 snprintf(addr, sizeof(addr), "%s", inet_ntoa(sin->sin_addr));
507 delete(addr, NULL);
508 }
509 free(buf);
510 }
511
512 void
513 sdl_print(sdl)
514 const struct sockaddr_dl *sdl;
515 {
516 char hbuf[NI_MAXHOST];
517
518 if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
519 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
520 printf("<invalid>");
521 else
522 printf("%s", hbuf);
523 }
524
525 int
526 atosdl(s, sdl)
527 const char *s;
528 struct sockaddr_dl *sdl;
529 {
530 int i;
531 long b;
532 caddr_t endp;
533 caddr_t p;
534 char *t, *r;
535
536 p = LLADDR(sdl);
537 endp = ((caddr_t)sdl) + sdl->sdl_len;
538 i = 0;
539
540 b = strtol(s, &t, 16);
541 if (t == s)
542 return 1;
543
544 *p++ = b;
545 ++i;
546 while ((p < endp) && (*t++ == ':')) {
547 b = strtol(t, &r, 16);
548 if (r == t)
549 break;
550 *p++ = b;
551 ++i;
552 t = r;
553 }
554 sdl->sdl_alen = i;
555
556 return 0;
557 }
558
559 void
560 usage()
561 {
562 const char *progname;
563
564 progname = getprogname();
565 (void)fprintf(stderr, "usage: %s [-n] hostname\n", progname);
566 (void)fprintf(stderr, "usage: %s [-n] -a\n", progname);
567 (void)fprintf(stderr, "usage: %s -d [-a|hostname]\n", progname);
568 (void)fprintf(stderr,
569 "usage: %s -s hostname ether_addr [temp] [pub]\n", progname);
570 (void)fprintf(stderr, "usage: %s -f filename\n", progname);
571 exit(1);
572 }
573
574 int
575 rtmsg(cmd)
576 int cmd;
577 {
578 static int seq;
579 int rlen;
580 struct rt_msghdr *rtm;
581 char *cp;
582 int l;
583
584 rtm = &m_rtmsg.m_rtm;
585 cp = m_rtmsg.m_space;
586 errno = 0;
587
588 if (cmd == RTM_DELETE)
589 goto doit;
590 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
591 rtm->rtm_flags = flags;
592 rtm->rtm_version = RTM_VERSION;
593
594 switch (cmd) {
595 default:
596 errx(1, "internal wrong cmd");
597 /*NOTREACHED*/
598 case RTM_ADD:
599 rtm->rtm_addrs |= RTA_GATEWAY;
600 rtm->rtm_rmx.rmx_expire = expire_time;
601 rtm->rtm_inits = RTV_EXPIRE;
602 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
603 sin_m.sin_other = 0;
604 if (doing_proxy) {
605 if (export_only)
606 sin_m.sin_other = SIN_PROXY;
607 else {
608 rtm->rtm_addrs |= RTA_NETMASK;
609 rtm->rtm_flags &= ~RTF_HOST;
610 }
611 }
612 /* FALLTHROUGH */
613 case RTM_GET:
614 rtm->rtm_addrs |= RTA_DST;
615 }
616
617 #define NEXTADDR(w, s) \
618 if (rtm->rtm_addrs & (w)) { \
619 (void)memcpy(cp, &s, ((struct sockaddr *)&s)->sa_len); \
620 cp += ROUNDUP(((struct sockaddr *)&s)->sa_len); \
621 }
622
623 NEXTADDR(RTA_DST, sin_m);
624 NEXTADDR(RTA_GATEWAY, sdl_m);
625 NEXTADDR(RTA_NETMASK, so_mask);
626
627 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
628 doit:
629 l = rtm->rtm_msglen;
630 rtm->rtm_seq = ++seq;
631 rtm->rtm_type = cmd;
632 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
633 if (errno != ESRCH || cmd != RTM_DELETE) {
634 warn("writing to routing socket");
635 return (-1);
636 }
637 }
638 do {
639 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
640 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
641 if (l < 0)
642 warn("read from routing socket");
643 return (0);
644 }
645
646 int
647 getinetaddr(host, inap)
648 const char *host;
649 struct in_addr *inap;
650 {
651 struct hostent *hp;
652
653 if (inet_aton(host, inap) == 1)
654 return (0);
655 if ((hp = gethostbyname(host)) == NULL) {
656 warnx("%s: %s", host, hstrerror(h_errno));
657 return (-1);
658 }
659 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
660 return (0);
661 }
662
663 int
664 getifname(ifindex, ifname)
665 u_int16_t ifindex;
666 char* ifname;
667 {
668 int i, idx, siz;
669 char ifrbuf[8192];
670 struct ifreq ifreq, *ifr;
671 const struct sockaddr_dl *sdl = NULL;
672
673 if (is < 0) {
674 is = socket(PF_INET, SOCK_DGRAM, 0);
675 if (is < 0)
676 err(1, "socket");
677
678 ifc.ifc_len = sizeof(ifconfbuf);
679 ifc.ifc_buf = ifconfbuf;
680
681 if (ioctl(is, SIOCGIFCONF, &ifc) < 0) {
682 close(is);
683 err(1, "SIOCGIFCONF");
684 is = -1;
685 }
686 }
687
688 ifr = ifc.ifc_req;
689 ifreq.ifr_name[0] = '\0';
690 for (i = 0, idx = 0; i < ifc.ifc_len; ) {
691 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
692 siz = sizeof(ifr->ifr_name) +
693 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
694 ? ifr->ifr_addr.sa_len
695 : sizeof(struct sockaddr));
696 i += siz;
697 /* avoid alignment issue */
698 if (sizeof(ifrbuf) < siz)
699 errx(1, "ifr too big");
700
701 memcpy(ifrbuf, ifr, siz);
702 ifr = (struct ifreq *)ifrbuf;
703
704 if (ifr->ifr_addr.sa_family != AF_LINK)
705 continue;
706
707 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
708 if (sdl && sdl->sdl_index == ifindex) {
709 (void) strncpy(ifname, ifr->ifr_name, IFNAMSIZ);
710 return 0;
711 }
712 }
713
714 return -1;
715 }
716