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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)from: arp.c 8.2 (Berkeley) 1/2/94
33 * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $
37 * arp - display, set, and delete arp table entries
41 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/ioctl.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
57 #include <arpa/inet.h>
70 static void search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
71 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
));
72 static void print_entry(struct sockaddr_dl
*sdl
,
73 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
74 static void nuke_entry(struct sockaddr_dl
*sdl
,
75 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
76 static int delete(char *host
, char *info
);
77 static void usage(void) __dead2
;
78 static int set(int argc
, char **argv
);
79 static int get(char *host
);
80 static int file(char *name
);
81 static void getsocket(void);
82 static int my_ether_aton(char *a
, struct ether_addr
*n
);
83 static int rtmsg(int cmd
);
84 static int get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
);
87 static int nflag
; /* no reverse dns lookups */
88 static int aflag
; /* do it for all entries */
89 static int cpuflag
= -1;
91 static int rifindex
= 0;
92 static const char *rifname
= NULL
;
94 static struct sockaddr_in so_mask
;
95 static struct sockaddr_inarp blank_sin
, sin_m
;
96 static struct sockaddr_dl blank_sdl
, sdl_m
;
97 static int flags
, doing_proxy
, proxy_only
, found_entry
;
98 static time_t expire_time
;
100 struct rt_msghdr m_rtm
;
104 /* which function we're supposed to do */
111 #define SETFUNC(f) { if (func) usage(); func = (f); }
114 main(int argc
, char **argv
)
120 while ((ch
= getopt(argc
, argv
, "ac:i:ndfsS")) != -1)
126 cpuflag
= strtol(optarg
, NULL
, 0);
153 bzero(&so_mask
, sizeof(so_mask
));
155 so_mask
.sin_addr
.s_addr
= 0xffffffff;
156 bzero(&blank_sin
, sizeof(blank_sin
));
157 blank_sin
.sin_len
= sizeof(blank_sin
);
158 blank_sin
.sin_family
= AF_INET
;
159 bzero(&blank_sdl
, sizeof(blank_sdl
));
160 blank_sdl
.sdl_len
= sizeof(blank_sdl
);
161 blank_sdl
.sdl_family
= AF_LINK
;
165 if (rifname
!= NULL
) {
166 if (func
!= F_GET
&& !(func
== F_DELETE
&& aflag
))
167 errx(1, "-i not applicable to this operation");
168 if ((rifindex
= if_nametoindex(rifname
)) == 0) {
170 errx(1, "interface %s does not exist",
173 err(1, "if_nametoindex(%s)", rifname
);
182 search(0, print_entry
);
191 if (argc
< 2 || argc
> 6)
193 if (func
== F_REPLACE
)
194 delete(argv
[0], NULL
);
195 rtn
= set(argc
, argv
) ? 1 : 0;
201 search(0, nuke_entry
);
203 if (argc
< 1 || argc
> 2)
205 rtn
= delete(argv
[0], argv
[1]);
219 * Process a file to set standard arp entries
226 char line
[100], arg
[5][50], *args
[5], *p
;
228 if ((fp
= fopen(name
, "r")) == NULL
)
229 errx(1, "cannot open %s", name
);
230 args
[0] = &arg
[0][0];
231 args
[1] = &arg
[1][0];
232 args
[2] = &arg
[2][0];
233 args
[3] = &arg
[3][0];
234 args
[4] = &arg
[4][0];
236 while(fgets(line
, 100, fp
) != NULL
) {
237 if ((p
= strchr(line
, '#')) != NULL
)
239 for (p
= line
; isblank(*p
); p
++);
240 if (*p
== '\n' || *p
== '\0')
242 i
= sscanf(p
, "%49s %49s %49s %49s %49s", arg
[0], arg
[1],
243 arg
[2], arg
[3], arg
[4]);
245 warnx("bad line: %s", line
);
260 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
267 * Set an individual arp entry
270 set(int argc
, char **argv
)
273 struct sockaddr_inarp
*addr
= &sin_m
;
274 struct sockaddr_dl
*sdl
;
275 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
276 struct ether_addr
*ea
;
277 char *host
= argv
[0], *eaddr
= argv
[1];
284 addr
->sin_addr
.s_addr
= inet_addr(host
);
285 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
286 if (!(hp
= gethostbyname(host
))) {
287 warnx("%s: %s", host
, hstrerror(h_errno
));
290 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
291 sizeof(addr
->sin_addr
));
293 doing_proxy
= flags
= proxy_only
= expire_time
= 0;
295 if (strncmp(argv
[0], "temp", 4) == 0) {
298 clock_gettime(CLOCK_MONOTONIC
, &sp
);
299 expire_time
= sp
.tv_sec
+ 20 * 60;
301 else if (strncmp(argv
[0], "pub", 3) == 0) {
302 flags
|= RTF_ANNOUNCE
;
304 if (argc
&& strncmp(argv
[1], "only", 3) == 0) {
306 sin_m
.sin_other
= SIN_PROXY
;
309 } else if (strncmp(argv
[0], "trail", 5) == 0) {
310 printf("%s: Sending trailers is no longer supported\n",
315 ea
= (struct ether_addr
*)LLADDR(&sdl_m
);
316 if (doing_proxy
&& !strcmp(eaddr
, "auto")) {
317 if (!get_ether_addr(addr
->sin_addr
.s_addr
, ea
)) {
318 printf("no interface found for %s\n",
319 inet_ntoa(addr
->sin_addr
));
322 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
324 if (my_ether_aton(eaddr
, ea
) == 0)
325 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
328 if (rtmsg(RTM_GET
) < 0) {
332 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
333 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(addr
->sin_len
) + (char *)addr
);
334 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
335 if (sdl
->sdl_family
== AF_LINK
&&
336 (rtm
->rtm_flags
& RTF_LLINFO
) &&
337 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
338 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
339 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
343 if (doing_proxy
== 0) {
344 printf("set: can only proxy for %s\n", host
);
347 if (sin_m
.sin_other
& SIN_PROXY
) {
348 printf("set: proxy entry exists for non 802 device\n");
351 sin_m
.sin_other
= SIN_PROXY
;
356 if (sdl
->sdl_family
!= AF_LINK
) {
357 printf("cannot intuit interface index and type for %s\n", host
);
360 sdl_m
.sdl_type
= sdl
->sdl_type
;
361 sdl_m
.sdl_index
= sdl
->sdl_index
;
362 return(rtmsg(RTM_ADD
));
366 * Display an individual arp entry
372 struct sockaddr_inarp
*addr
= &sin_m
;
375 addr
->sin_addr
.s_addr
= inet_addr(host
);
376 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
377 if (!(hp
= gethostbyname(host
)))
378 errx(1, "%s: %s", host
, hstrerror(h_errno
));
379 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
380 sizeof(addr
->sin_addr
));
382 search(addr
->sin_addr
.s_addr
, print_entry
);
383 if (found_entry
== 0) {
384 printf("%s (%s) -- no entry",
385 host
, inet_ntoa(addr
->sin_addr
));
387 printf(" on %s", rifname
);
395 * Delete an arp entry
398 delete(char *host
, char *info
)
401 struct sockaddr_inarp
*addr
= &sin_m
;
402 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
403 struct sockaddr_dl
*sdl
;
408 if (strncmp(info
, "pub", 3) == 0)
409 sin_m
.sin_other
= SIN_PROXY
;
413 addr
->sin_addr
.s_addr
= inet_addr(host
);
414 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
415 if (!(hp
= gethostbyname(host
))) {
416 warnx("%s: %s", host
, hstrerror(h_errno
));
419 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
420 sizeof(addr
->sin_addr
));
423 if (rtmsg(RTM_GET
) < 0) {
427 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
428 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(addr
->sin_len
) + (char *)addr
);
429 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
430 if (sdl
->sdl_family
== AF_LINK
&&
431 (rtm
->rtm_flags
& RTF_LLINFO
) &&
432 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
433 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
434 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
439 if (sin_m
.sin_other
& SIN_PROXY
) {
440 fprintf(stderr
, "delete: can't locate %s\n",host
);
443 sin_m
.sin_other
= SIN_PROXY
;
447 if (sdl
->sdl_family
!= AF_LINK
) {
448 printf("cannot locate %s\n", host
);
451 if (aflag
&& (rtm
->rtm_flags
& RTF_STATIC
)) {
454 if (rtmsg(RTM_CHANGE
) == 0) {
455 printf("%s (%s) cleared\n",
456 host
, inet_ntoa(addr
->sin_addr
));
460 if (rtmsg(RTM_DELETE
) == 0) {
461 printf("%s (%s) deleted\n",
462 host
, inet_ntoa(addr
->sin_addr
));
470 * Search the arp table and do some action on matching entries
473 search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
474 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
))
479 char *lim
, *buf
, *next
;
480 struct rt_msghdr
*rtm
;
481 struct sockaddr_inarp
*sin2
;
482 struct sockaddr_dl
*sdl
;
488 mib
[4] = NET_RT_FLAGS
;
496 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0)
497 errx(1, "route-sysctl-estimate");
498 if ((buf
= malloc(needed
)) == NULL
)
500 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0)
501 errx(1, "actual retrieval of routing table");
503 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
504 rtm
= (struct rt_msghdr
*)next
;
505 sin2
= (struct sockaddr_inarp
*)(rtm
+ 1);
506 sdl
= (struct sockaddr_dl
*)((char *)sin2
+
507 RT_ROUNDUP(sin2
->sin_len
));
508 if (rifindex
> 0 && rifindex
!= sdl
->sdl_index
)
511 if (addr
!= sin2
->sin_addr
.s_addr
)
515 (*action
)(sdl
, sin2
, rtm
);
521 * Display an arp entry
524 print_entry(struct sockaddr_dl
*sdl
,
525 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
)
529 char ifname
[IF_NAMESIZE
];
532 hp
= gethostbyaddr(&(addr
->sin_addr
), sizeof(addr
->sin_addr
),
540 if (h_errno
== TRY_AGAIN
)
543 printf("%s (%s) at ", host
, inet_ntoa(addr
->sin_addr
));
545 printf("%s", ether_ntoa((struct ether_addr
*)LLADDR(sdl
)));
547 printf("(incomplete)");
548 if (if_indextoname(sdl
->sdl_index
, ifname
) != NULL
)
549 printf(" on %s", ifname
);
550 if (rtm
->rtm_rmx
.rmx_expire
== 0)
551 printf(" permanent");
552 if (addr
->sin_other
& SIN_PROXY
)
553 printf(" published (proxy only)");
554 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
555 addr
= (struct sockaddr_inarp
*)
556 (RT_ROUNDUP(sdl
->sdl_len
) + (char *)sdl
);
557 if (addr
->sin_addr
.s_addr
== 0xffffffff)
558 printf(" published");
559 if (addr
->sin_len
!= 8)
562 switch(sdl
->sdl_type
) {
564 printf(" [ethernet]");
583 nuke_entry(struct sockaddr_dl
*sdl __unused
,
584 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm __unused
)
588 snprintf(ip
, sizeof(ip
), "%s", inet_ntoa(addr
->sin_addr
));
593 my_ether_aton(char *a
, struct ether_addr
*n
)
595 struct ether_addr
*ea
;
597 if ((ea
= ether_aton(a
)) == NULL
) {
598 warnx("invalid Ethernet address '%s'", a
);
608 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
609 "usage: arp [-n] [-c cpu] [-i interface] hostname",
610 " arp [-n] [-c cpu] [-i interface] -a",
611 " arp -d hostname [pub]",
612 " arp -d [-i interface] -a",
613 " arp -s hostname ether_addr [temp] [pub [only]]",
614 " arp -S hostname ether_addr [temp] [pub [only]]",
624 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
625 char *cp
= m_rtmsg
.m_space
;
629 if (cmd
== RTM_DELETE
|| cmd
== RTM_CHANGE
)
631 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
632 rtm
->rtm_flags
= flags
;
633 rtm
->rtm_version
= RTM_VERSION
;
637 errx(1, "internal wrong cmd");
639 rtm
->rtm_addrs
|= RTA_GATEWAY
;
640 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
641 rtm
->rtm_inits
= RTV_EXPIRE
;
642 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
646 sin_m
.sin_other
= SIN_PROXY
;
648 rtm
->rtm_addrs
|= RTA_NETMASK
;
649 rtm
->rtm_flags
&= ~RTF_HOST
;
654 rtm
->rtm_addrs
|= RTA_DST
;
657 #define NEXTADDR(w, s) \
658 if (rtm->rtm_addrs & (w)) { \
659 bcopy((char *)&s, cp, sizeof(s)); \
660 cp += RT_ROUNDUP(sizeof(s)); \
663 NEXTADDR(RTA_DST
, sin_m
);
664 NEXTADDR(RTA_GATEWAY
, sdl_m
);
665 NEXTADDR(RTA_NETMASK
, so_mask
);
667 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
670 rtm
->rtm_seq
= ++seq
;
672 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
673 if (errno
!= ESRCH
|| (cmd
!= RTM_DELETE
&& cmd
!= RTM_CHANGE
)) {
674 warn("writing to routing socket");
679 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
680 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
682 warn("read from routing socket");
687 * get_ether_addr - get the hardware address of an interface on the
688 * the same subnet as ipaddr.
693 get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
)
695 struct ifreq
*ifr
, *ifend
, *ifp
;
697 struct sockaddr_dl
*dla
;
700 struct ifreq ifs
[MAX_IFS
];
703 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
707 ifc
.ifc_len
= sizeof(ifs
);
709 if (ioctl(sock
, SIOCGIFCONF
, &ifc
) < 0) {
710 warnx("ioctl(SIOCGIFCONF)");
716 * Scan through looking for an interface with an Internet
717 * address on the same subnet as `ipaddr'.
719 ifend
= (struct ifreq
*) (ifc
.ifc_buf
+ ifc
.ifc_len
);
720 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
721 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
722 ina
= ((struct sockaddr_in
*)
723 &ifr
->ifr_addr
)->sin_addr
.s_addr
;
724 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
,
725 sizeof(ifreq
.ifr_name
));
727 * Check that the interface is up,
728 * and not point-to-point or loopback.
730 if (ioctl(sock
, SIOCGIFFLAGS
, &ifreq
) < 0)
732 if ((ifreq
.ifr_flags
&
733 (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|
734 IFF_LOOPBACK
|IFF_NOARP
))
735 != (IFF_UP
|IFF_BROADCAST
))
738 * Get its netmask and check that it's on
741 if (ioctl(sock
, SIOCGIFNETMASK
, &ifreq
) < 0)
743 mask
= ((struct sockaddr_in
*)
744 &ifreq
.ifr_addr
)->sin_addr
.s_addr
;
745 if ((ipaddr
& mask
) != (ina
& mask
))
750 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
751 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));
760 * Now scan through again looking for a link-level address
761 * for this interface.
764 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
765 if (strcmp(ifp
->ifr_name
, ifr
->ifr_name
) == 0
766 && ifr
->ifr_addr
.sa_family
== AF_LINK
) {
768 * Found the link-level address - copy it out
770 dla
= (struct sockaddr_dl
*) &ifr
->ifr_addr
;
771 memcpy(hwaddr
, LLADDR(dla
), dla
->sdl_alen
);
773 printf("using interface %s for proxy with address ",
775 printf("%s\n", ether_ntoa(hwaddr
));
776 return(dla
->sdl_alen
);
778 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
779 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));