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 * @(#) Copyright (c) 1984, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)from: arp.c 8.2 (Berkeley) 1/2/94
34 * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $
38 * arp - display, set, and delete arp table entries
42 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
47 #include <sys/ioctl.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
58 #include <arpa/inet.h>
71 void search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
72 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
));
73 void print_entry(struct sockaddr_dl
*sdl
,
74 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
75 void nuke_entry(struct sockaddr_dl
*sdl
,
76 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
77 int delete(char *host
, char *info
);
79 int set(int argc
, char **argv
);
83 int my_ether_aton(char *a
, struct ether_addr
*n
);
85 int get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
);
88 static int nflag
; /* no reverse dns lookups */
89 static int aflag
; /* do it for all entries */
90 static int cpuflag
= -1;
93 struct sockaddr_in so_mask
;
94 struct sockaddr_inarp blank_sin
, sin_m
;
95 struct sockaddr_dl blank_sdl
, sdl_m
;
96 int flags
, doing_proxy
, proxy_only
, found_entry
;
99 struct rt_msghdr m_rtm
;
103 /* which function we're supposed to do */
110 #define SETFUNC(f) { if (func) usage(); func = (f); }
113 main(int argc
, char **argv
)
119 while ((ch
= getopt(argc
, argv
, "ac:ndfsS")) != -1)
125 cpuflag
= strtol(optarg
, NULL
, 0);
149 bzero(&so_mask
, sizeof(so_mask
));
151 so_mask
.sin_addr
.s_addr
= 0xffffffff;
152 bzero(&blank_sin
, sizeof(blank_sin
));
153 blank_sin
.sin_len
= sizeof(blank_sin
);
154 blank_sin
.sin_family
= AF_INET
;
155 bzero(&blank_sdl
, sizeof(blank_sdl
));
156 blank_sdl
.sdl_len
= sizeof(blank_sdl
);
157 blank_sdl
.sdl_family
= AF_LINK
;
166 search(0, print_entry
);
175 if (argc
< 2 || argc
> 6)
177 if (func
== F_REPLACE
)
178 delete(argv
[0], NULL
);
179 rtn
= set(argc
, argv
) ? 1 : 0;
185 search(0, nuke_entry
);
187 if (argc
< 1 || argc
> 2)
189 rtn
= delete(argv
[0], argv
[1]);
203 * Process a file to set standard arp entries
210 char line
[100], arg
[5][50], *args
[5], *p
;
212 if ((fp
= fopen(name
, "r")) == NULL
)
213 errx(1, "cannot open %s", name
);
214 args
[0] = &arg
[0][0];
215 args
[1] = &arg
[1][0];
216 args
[2] = &arg
[2][0];
217 args
[3] = &arg
[3][0];
218 args
[4] = &arg
[4][0];
220 while(fgets(line
, 100, fp
) != NULL
) {
221 if ((p
= strchr(line
, '#')) != NULL
)
223 for (p
= line
; isblank(*p
); p
++);
224 if (*p
== '\n' || *p
== '\0')
226 i
= sscanf(p
, "%49s %49s %49s %49s %49s", arg
[0], arg
[1],
227 arg
[2], arg
[3], arg
[4]);
229 warnx("bad line: %s", line
);
244 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
251 * Set an individual arp entry
254 set(int argc
, char **argv
)
257 struct sockaddr_inarp
*addr
= &sin_m
;
258 struct sockaddr_dl
*sdl
;
259 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
260 struct ether_addr
*ea
;
261 char *host
= argv
[0], *eaddr
= argv
[1];
268 addr
->sin_addr
.s_addr
= inet_addr(host
);
269 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
270 if (!(hp
= gethostbyname(host
))) {
271 warnx("%s: %s", host
, hstrerror(h_errno
));
274 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
275 sizeof(addr
->sin_addr
));
277 doing_proxy
= flags
= proxy_only
= expire_time
= 0;
279 if (strncmp(argv
[0], "temp", 4) == 0) {
282 clock_gettime(CLOCK_MONOTONIC
, &sp
);
283 expire_time
= sp
.tv_sec
+ 20 * 60;
285 else if (strncmp(argv
[0], "pub", 3) == 0) {
286 flags
|= RTF_ANNOUNCE
;
288 if (argc
&& strncmp(argv
[1], "only", 3) == 0) {
290 sin_m
.sin_other
= SIN_PROXY
;
293 } else if (strncmp(argv
[0], "trail", 5) == 0) {
294 printf("%s: Sending trailers is no longer supported\n",
299 ea
= (struct ether_addr
*)LLADDR(&sdl_m
);
300 if (doing_proxy
&& !strcmp(eaddr
, "auto")) {
301 if (!get_ether_addr(addr
->sin_addr
.s_addr
, ea
)) {
302 printf("no interface found for %s\n",
303 inet_ntoa(addr
->sin_addr
));
306 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
308 if (my_ether_aton(eaddr
, ea
) == 0)
309 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
312 if (rtmsg(RTM_GET
) < 0) {
316 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
317 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(addr
->sin_len
) + (char *)addr
);
318 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
319 if (sdl
->sdl_family
== AF_LINK
&&
320 (rtm
->rtm_flags
& RTF_LLINFO
) &&
321 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
322 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
323 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
327 if (doing_proxy
== 0) {
328 printf("set: can only proxy for %s\n", host
);
331 if (sin_m
.sin_other
& SIN_PROXY
) {
332 printf("set: proxy entry exists for non 802 device\n");
335 sin_m
.sin_other
= SIN_PROXY
;
340 if (sdl
->sdl_family
!= AF_LINK
) {
341 printf("cannot intuit interface index and type for %s\n", host
);
344 sdl_m
.sdl_type
= sdl
->sdl_type
;
345 sdl_m
.sdl_index
= sdl
->sdl_index
;
346 return(rtmsg(RTM_ADD
));
350 * Display an individual arp entry
356 struct sockaddr_inarp
*addr
= &sin_m
;
359 addr
->sin_addr
.s_addr
= inet_addr(host
);
360 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
361 if (!(hp
= gethostbyname(host
)))
362 errx(1, "%s: %s", host
, hstrerror(h_errno
));
363 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
364 sizeof(addr
->sin_addr
));
366 search(addr
->sin_addr
.s_addr
, print_entry
);
367 if (found_entry
== 0) {
368 printf("%s (%s) -- no entry\n",
369 host
, inet_ntoa(addr
->sin_addr
));
376 * Delete an arp entry
379 delete(char *host
, char *info
)
382 struct sockaddr_inarp
*addr
= &sin_m
;
383 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
384 struct sockaddr_dl
*sdl
;
389 if (strncmp(info
, "pub", 3) == 0)
390 sin_m
.sin_other
= SIN_PROXY
;
394 addr
->sin_addr
.s_addr
= inet_addr(host
);
395 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
396 if (!(hp
= gethostbyname(host
))) {
397 warnx("%s: %s", host
, hstrerror(h_errno
));
400 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
401 sizeof(addr
->sin_addr
));
404 if (rtmsg(RTM_GET
) < 0) {
408 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
409 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(addr
->sin_len
) + (char *)addr
);
410 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
411 if (sdl
->sdl_family
== AF_LINK
&&
412 (rtm
->rtm_flags
& RTF_LLINFO
) &&
413 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
414 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
415 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
420 if (sin_m
.sin_other
& SIN_PROXY
) {
421 fprintf(stderr
, "delete: can't locate %s\n",host
);
424 sin_m
.sin_other
= SIN_PROXY
;
428 if (sdl
->sdl_family
!= AF_LINK
) {
429 printf("cannot locate %s\n", host
);
432 if (aflag
&& (rtm
->rtm_flags
& RTF_STATIC
)) {
435 if (rtmsg(RTM_CHANGE
) == 0) {
436 printf("%s (%s) cleared\n",
437 host
, inet_ntoa(addr
->sin_addr
));
441 if (rtmsg(RTM_DELETE
) == 0) {
442 printf("%s (%s) deleted\n",
443 host
, inet_ntoa(addr
->sin_addr
));
451 * Search the arp table and do some action on matching entries
454 search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
455 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
))
460 char *lim
, *buf
, *next
;
461 struct rt_msghdr
*rtm
;
462 struct sockaddr_inarp
*sin2
;
463 struct sockaddr_dl
*sdl
;
469 mib
[4] = NET_RT_FLAGS
;
477 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0)
478 errx(1, "route-sysctl-estimate");
479 if ((buf
= malloc(needed
)) == NULL
)
481 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0)
482 errx(1, "actual retrieval of routing table");
484 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
485 rtm
= (struct rt_msghdr
*)next
;
486 sin2
= (struct sockaddr_inarp
*)(rtm
+ 1);
487 sdl
= (struct sockaddr_dl
*)((char *)sin2
+
488 RT_ROUNDUP(sin2
->sin_len
));
490 if (addr
!= sin2
->sin_addr
.s_addr
)
494 (*action
)(sdl
, sin2
, rtm
);
500 * Display an arp entry
503 print_entry(struct sockaddr_dl
*sdl
,
504 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
)
508 char ifname
[IF_NAMESIZE
];
511 hp
= gethostbyaddr(&(addr
->sin_addr
), sizeof(addr
->sin_addr
),
519 if (h_errno
== TRY_AGAIN
)
522 printf("%s (%s) at ", host
, inet_ntoa(addr
->sin_addr
));
524 printf("%s", ether_ntoa((struct ether_addr
*)LLADDR(sdl
)));
526 printf("(incomplete)");
527 if (if_indextoname(sdl
->sdl_index
, ifname
) != NULL
)
528 printf(" on %s", ifname
);
529 if (rtm
->rtm_rmx
.rmx_expire
== 0)
530 printf(" permanent");
531 if (addr
->sin_other
& SIN_PROXY
)
532 printf(" published (proxy only)");
533 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
534 addr
= (struct sockaddr_inarp
*)
535 (RT_ROUNDUP(sdl
->sdl_len
) + (char *)sdl
);
536 if (addr
->sin_addr
.s_addr
== 0xffffffff)
537 printf(" published");
538 if (addr
->sin_len
!= 8)
541 switch(sdl
->sdl_type
) {
543 printf(" [ethernet]");
562 nuke_entry(struct sockaddr_dl
*sdl __unused
,
563 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm __unused
)
567 snprintf(ip
, sizeof(ip
), "%s", inet_ntoa(addr
->sin_addr
));
572 my_ether_aton(char *a
, struct ether_addr
*n
)
574 struct ether_addr
*ea
;
576 if ((ea
= ether_aton(a
)) == NULL
) {
577 warnx("invalid Ethernet address '%s'", a
);
587 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
588 "usage: arp [-n] [-c cpu] hostname",
589 " arp [-n] [-c cpu] -a",
590 " arp -d hostname [pub]",
592 " arp -s hostname ether_addr [temp] [pub [only]]",
593 " arp -S hostname ether_addr [temp] [pub [only]]",
603 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
604 char *cp
= m_rtmsg
.m_space
;
608 if (cmd
== RTM_DELETE
|| cmd
== RTM_CHANGE
)
610 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
611 rtm
->rtm_flags
= flags
;
612 rtm
->rtm_version
= RTM_VERSION
;
616 errx(1, "internal wrong cmd");
618 rtm
->rtm_addrs
|= RTA_GATEWAY
;
619 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
620 rtm
->rtm_inits
= RTV_EXPIRE
;
621 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
625 sin_m
.sin_other
= SIN_PROXY
;
627 rtm
->rtm_addrs
|= RTA_NETMASK
;
628 rtm
->rtm_flags
&= ~RTF_HOST
;
633 rtm
->rtm_addrs
|= RTA_DST
;
636 #define NEXTADDR(w, s) \
637 if (rtm->rtm_addrs & (w)) { \
638 bcopy((char *)&s, cp, sizeof(s)); \
639 cp += RT_ROUNDUP(sizeof(s)); \
642 NEXTADDR(RTA_DST
, sin_m
);
643 NEXTADDR(RTA_GATEWAY
, sdl_m
);
644 NEXTADDR(RTA_NETMASK
, so_mask
);
646 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
649 rtm
->rtm_seq
= ++seq
;
651 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
652 if (errno
!= ESRCH
|| (cmd
!= RTM_DELETE
&& cmd
!= RTM_CHANGE
)) {
653 warn("writing to routing socket");
658 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
659 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
661 warn("read from routing socket");
666 * get_ether_addr - get the hardware address of an interface on the
667 * the same subnet as ipaddr.
672 get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
)
674 struct ifreq
*ifr
, *ifend
, *ifp
;
676 struct sockaddr_dl
*dla
;
679 struct ifreq ifs
[MAX_IFS
];
682 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
686 ifc
.ifc_len
= sizeof(ifs
);
688 if (ioctl(sock
, SIOCGIFCONF
, &ifc
) < 0) {
689 warnx("ioctl(SIOCGIFCONF)");
695 * Scan through looking for an interface with an Internet
696 * address on the same subnet as `ipaddr'.
698 ifend
= (struct ifreq
*) (ifc
.ifc_buf
+ ifc
.ifc_len
);
699 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
700 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
701 ina
= ((struct sockaddr_in
*)
702 &ifr
->ifr_addr
)->sin_addr
.s_addr
;
703 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
,
704 sizeof(ifreq
.ifr_name
));
706 * Check that the interface is up,
707 * and not point-to-point or loopback.
709 if (ioctl(sock
, SIOCGIFFLAGS
, &ifreq
) < 0)
711 if ((ifreq
.ifr_flags
&
712 (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|
713 IFF_LOOPBACK
|IFF_NOARP
))
714 != (IFF_UP
|IFF_BROADCAST
))
717 * Get its netmask and check that it's on
720 if (ioctl(sock
, SIOCGIFNETMASK
, &ifreq
) < 0)
722 mask
= ((struct sockaddr_in
*)
723 &ifreq
.ifr_addr
)->sin_addr
.s_addr
;
724 if ((ipaddr
& mask
) != (ina
& mask
))
729 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
730 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));
739 * Now scan through again looking for a link-level address
740 * for this interface.
743 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
744 if (strcmp(ifp
->ifr_name
, ifr
->ifr_name
) == 0
745 && ifr
->ifr_addr
.sa_family
== AF_LINK
) {
747 * Found the link-level address - copy it out
749 dla
= (struct sockaddr_dl
*) &ifr
->ifr_addr
;
750 memcpy(hwaddr
, LLADDR(dla
), dla
->sdl_alen
);
752 printf("using interface %s for proxy with address ",
754 printf("%s\n", ether_ntoa(hwaddr
));
755 return(dla
->sdl_alen
);
757 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
758 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));