2 * Copyright (c) 1984, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Sun Microsystems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1984, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)from: arp.c 8.2 (Berkeley) 1/2/94
38 * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $
39 * $DragonFly: src/usr.sbin/arp/arp.c,v 1.11 2008/02/03 00:41:44 swildner Exp $
43 * arp - display, set, and delete arp table entries
47 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/ioctl.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
63 #include <arpa/inet.h>
76 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
77 struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
78 void print_entry(struct sockaddr_dl *sdl,
79 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
80 void nuke_entry(struct sockaddr_dl *sdl,
81 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
82 int delete(char *host, char *info);
84 int set(int argc, char **argv);
88 int my_ether_aton(char *a, struct ether_addr *n);
90 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
93 static int nflag; /* no reverse dns lookups */
94 static int aflag; /* do it for all entries */
95 static int cpuflag = -1;
98 struct sockaddr_in so_mask;
99 struct sockaddr_inarp blank_sin, sin_m;
100 struct sockaddr_dl blank_sdl, sdl_m;
101 int expire_time, flags, doing_proxy, proxy_only, found_entry;
103 struct rt_msghdr m_rtm;
107 /* which function we're supposed to do */
115 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
116 #define SETFUNC(f) { if (func) usage(); func = (f); }
119 main(int argc, char **argv)
125 while ((ch = getopt(argc, argv, "ac:ndfsS")) != -1)
131 cpuflag = strtol(optarg, NULL, 0);
155 bzero(&so_mask, sizeof(so_mask));
157 so_mask.sin_addr.s_addr = 0xffffffff;
158 bzero(&blank_sin, sizeof(blank_sin));
159 blank_sin.sin_len = sizeof(blank_sin);
160 blank_sin.sin_family = AF_INET;
161 bzero(&blank_sdl, sizeof(blank_sdl));
162 blank_sdl.sdl_len = sizeof(blank_sdl);
163 blank_sdl.sdl_family = AF_LINK;
172 search(0, print_entry);
181 if (argc < 2 || argc > 6)
183 if (func == F_REPLACE)
184 delete(argv[0], NULL);
185 rtn = set(argc, argv) ? 1 : 0;
191 search(0, nuke_entry);
193 if (argc < 1 || argc > 2)
195 rtn = delete(argv[0], argv[1]);
209 * Process a file to set standard arp entries
216 char line[100], arg[5][50], *args[5], *p;
218 if ((fp = fopen(name, "r")) == NULL)
219 errx(1, "cannot open %s", name);
220 args[0] = &arg[0][0];
221 args[1] = &arg[1][0];
222 args[2] = &arg[2][0];
223 args[3] = &arg[3][0];
224 args[4] = &arg[4][0];
226 while(fgets(line, 100, fp) != NULL) {
227 if ((p = strchr(line, '#')) != NULL)
229 for (p = line; isblank(*p); p++);
230 if (*p == '\n' || *p == '\0')
232 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
233 arg[2], arg[3], arg[4]);
235 warnx("bad line: %s", line);
250 s = socket(PF_ROUTE, SOCK_RAW, 0);
257 * Set an individual arp entry
260 set(int argc, char **argv)
263 struct sockaddr_inarp *addr = &sin_m;
264 struct sockaddr_dl *sdl;
265 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
266 struct ether_addr *ea;
267 char *host = argv[0], *eaddr = argv[1];
274 addr->sin_addr.s_addr = inet_addr(host);
275 if (addr->sin_addr.s_addr == INADDR_NONE) {
276 if (!(hp = gethostbyname(host))) {
277 warnx("%s: %s", host, hstrerror(h_errno));
280 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
281 sizeof(addr->sin_addr));
283 doing_proxy = flags = proxy_only = expire_time = 0;
285 if (strncmp(argv[0], "temp", 4) == 0) {
287 gettimeofday(&tv, 0);
288 expire_time = tv.tv_sec + 20 * 60;
290 else if (strncmp(argv[0], "pub", 3) == 0) {
291 flags |= RTF_ANNOUNCE;
293 if (argc && strncmp(argv[1], "only", 3) == 0) {
295 sin_m.sin_other = SIN_PROXY;
298 } else if (strncmp(argv[0], "trail", 5) == 0) {
299 printf("%s: Sending trailers is no longer supported\n",
304 ea = (struct ether_addr *)LLADDR(&sdl_m);
305 if (doing_proxy && !strcmp(eaddr, "auto")) {
306 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
307 printf("no interface found for %s\n",
308 inet_ntoa(addr->sin_addr));
311 sdl_m.sdl_alen = ETHER_ADDR_LEN;
313 if (my_ether_aton(eaddr, ea) == 0)
314 sdl_m.sdl_alen = ETHER_ADDR_LEN;
317 if (rtmsg(RTM_GET) < 0) {
321 addr = (struct sockaddr_inarp *)(rtm + 1);
322 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
323 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
324 if (sdl->sdl_family == AF_LINK &&
325 (rtm->rtm_flags & RTF_LLINFO) &&
326 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
327 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
328 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
332 if (doing_proxy == 0) {
333 printf("set: can only proxy for %s\n", host);
336 if (sin_m.sin_other & SIN_PROXY) {
337 printf("set: proxy entry exists for non 802 device\n");
340 sin_m.sin_other = SIN_PROXY;
345 if (sdl->sdl_family != AF_LINK) {
346 printf("cannot intuit interface index and type for %s\n", host);
349 sdl_m.sdl_type = sdl->sdl_type;
350 sdl_m.sdl_index = sdl->sdl_index;
351 return(rtmsg(RTM_ADD));
355 * Display an individual arp entry
361 struct sockaddr_inarp *addr = &sin_m;
364 addr->sin_addr.s_addr = inet_addr(host);
365 if (addr->sin_addr.s_addr == INADDR_NONE) {
366 if (!(hp = gethostbyname(host)))
367 errx(1, "%s: %s", host, hstrerror(h_errno));
368 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
369 sizeof(addr->sin_addr));
371 search(addr->sin_addr.s_addr, print_entry);
372 if (found_entry == 0) {
373 printf("%s (%s) -- no entry\n",
374 host, inet_ntoa(addr->sin_addr));
381 * Delete an arp entry
384 delete(char *host, char *info)
387 struct sockaddr_inarp *addr = &sin_m;
388 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
389 struct sockaddr_dl *sdl;
394 if (strncmp(info, "pub", 3) == 0)
395 sin_m.sin_other = SIN_PROXY;
399 addr->sin_addr.s_addr = inet_addr(host);
400 if (addr->sin_addr.s_addr == INADDR_NONE) {
401 if (!(hp = gethostbyname(host))) {
402 warnx("%s: %s", host, hstrerror(h_errno));
405 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
406 sizeof(addr->sin_addr));
409 if (rtmsg(RTM_GET) < 0) {
413 addr = (struct sockaddr_inarp *)(rtm + 1);
414 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
415 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
416 if (sdl->sdl_family == AF_LINK &&
417 (rtm->rtm_flags & RTF_LLINFO) &&
418 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
419 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
420 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
425 if (sin_m.sin_other & SIN_PROXY) {
426 fprintf(stderr, "delete: can't locate %s\n",host);
429 sin_m.sin_other = SIN_PROXY;
433 if (sdl->sdl_family != AF_LINK) {
434 printf("cannot locate %s\n", host);
437 if (aflag && (rtm->rtm_flags & RTF_STATIC)) {
440 if (rtmsg(RTM_CHANGE) == 0) {
441 printf("%s (%s) cleared\n",
442 host, inet_ntoa(addr->sin_addr));
446 if (rtmsg(RTM_DELETE) == 0) {
447 printf("%s (%s) deleted\n",
448 host, inet_ntoa(addr->sin_addr));
456 * Search the arp table and do some action on matching entries
459 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
460 struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
465 char *lim, *buf, *next;
466 struct rt_msghdr *rtm;
467 struct sockaddr_inarp *sin2;
468 struct sockaddr_dl *sdl;
474 mib[4] = NET_RT_FLAGS;
482 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
483 errx(1, "route-sysctl-estimate");
484 if ((buf = malloc(needed)) == NULL)
486 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
487 errx(1, "actual retrieval of routing table");
489 for (next = buf; next < lim; next += rtm->rtm_msglen) {
490 rtm = (struct rt_msghdr *)next;
491 sin2 = (struct sockaddr_inarp *)(rtm + 1);
492 sdl = (struct sockaddr_dl *)((char *)sin2 + ROUNDUP(sin2->sin_len));
494 if (addr != sin2->sin_addr.s_addr)
498 (*action)(sdl, sin2, rtm);
504 * Display an arp entry
507 print_entry(struct sockaddr_dl *sdl,
508 struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
512 char ifname[IF_NAMESIZE];
515 hp = gethostbyaddr(&(addr->sin_addr), sizeof(addr->sin_addr),
523 if (h_errno == TRY_AGAIN)
526 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
528 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
530 printf("(incomplete)");
531 if (if_indextoname(sdl->sdl_index, ifname) != NULL)
532 printf(" on %s", ifname);
533 if (rtm->rtm_rmx.rmx_expire == 0)
534 printf(" permanent");
535 if (addr->sin_other & SIN_PROXY)
536 printf(" published (proxy only)");
537 if (rtm->rtm_addrs & RTA_NETMASK) {
538 addr = (struct sockaddr_inarp *)
539 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
540 if (addr->sin_addr.s_addr == 0xffffffff)
541 printf(" published");
542 if (addr->sin_len != 8)
545 switch(sdl->sdl_type) {
547 printf(" [ethernet]");
566 nuke_entry(struct sockaddr_dl *sdl __unused,
567 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
571 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
576 my_ether_aton(char *a, struct ether_addr *n)
578 struct ether_addr *ea;
580 if ((ea = ether_aton(a)) == NULL) {
581 warnx("invalid Ethernet address '%s'", a);
591 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
592 "usage: arp [-n] [-c cpu] hostname",
593 " arp [-n] [-c cpu] -a",
594 " arp -d hostname [pub]",
596 " arp -s hostname ether_addr [temp] [pub [only]]",
597 " arp -S hostname ether_addr [temp] [pub [only]]",
607 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
608 char *cp = m_rtmsg.m_space;
612 if (cmd == RTM_DELETE || cmd == RTM_CHANGE)
614 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
615 rtm->rtm_flags = flags;
616 rtm->rtm_version = RTM_VERSION;
620 errx(1, "internal wrong cmd");
622 rtm->rtm_addrs |= RTA_GATEWAY;
623 rtm->rtm_rmx.rmx_expire = expire_time;
624 rtm->rtm_inits = RTV_EXPIRE;
625 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
629 sin_m.sin_other = SIN_PROXY;
631 rtm->rtm_addrs |= RTA_NETMASK;
632 rtm->rtm_flags &= ~RTF_HOST;
637 rtm->rtm_addrs |= RTA_DST;
639 #define NEXTADDR(w, s) \
640 if (rtm->rtm_addrs & (w)) { \
641 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
643 NEXTADDR(RTA_DST, sin_m);
644 NEXTADDR(RTA_GATEWAY, sdl_m);
645 NEXTADDR(RTA_NETMASK, so_mask);
647 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
650 rtm->rtm_seq = ++seq;
652 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
653 if (errno != ESRCH || (cmd != RTM_DELETE && cmd != RTM_CHANGE)) {
654 warn("writing to routing socket");
659 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
660 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
662 warn("read from routing socket");
667 * get_ether_addr - get the hardware address of an interface on the
668 * the same subnet as ipaddr.
673 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
675 struct ifreq *ifr, *ifend, *ifp;
677 struct sockaddr_dl *dla;
680 struct ifreq ifs[MAX_IFS];
683 sock = socket(AF_INET, SOCK_DGRAM, 0);
687 ifc.ifc_len = sizeof(ifs);
689 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
690 warnx("ioctl(SIOCGIFCONF)");
696 * Scan through looking for an interface with an Internet
697 * address on the same subnet as `ipaddr'.
699 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
700 for (ifr = ifc.ifc_req; ifr < ifend; ) {
701 if (ifr->ifr_addr.sa_family == AF_INET) {
702 ina = ((struct sockaddr_in *)
703 &ifr->ifr_addr)->sin_addr.s_addr;
704 strncpy(ifreq.ifr_name, ifr->ifr_name,
705 sizeof(ifreq.ifr_name));
707 * Check that the interface is up,
708 * and not point-to-point or loopback.
710 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
712 if ((ifreq.ifr_flags &
713 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
714 IFF_LOOPBACK|IFF_NOARP))
715 != (IFF_UP|IFF_BROADCAST))
718 * Get its netmask and check that it's on
721 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
723 mask = ((struct sockaddr_in *)
724 &ifreq.ifr_addr)->sin_addr.s_addr;
725 if ((ipaddr & mask) != (ina & mask))
730 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
731 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
740 * Now scan through again looking for a link-level address
741 * for this interface.
744 for (ifr = ifc.ifc_req; ifr < ifend; ) {
745 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
746 && ifr->ifr_addr.sa_family == AF_LINK) {
748 * Found the link-level address - copy it out
750 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
751 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
753 printf("using interface %s for proxy with address ",
755 printf("%s\n", ether_ntoa(hwaddr));
756 return(dla->sdl_alen);
758 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
759 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));