]>
git.saurik.com Git - apple/network_cmds.git/blob - arp.tproj/arp.c
e1273c14a2b0faeb51c4449114c140e415c64ec3
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
38 static char const copyright
[] =
39 "@(#) Copyright (c) 1984, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char const sccsid
[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94";
47 static const char rcsid
[] =
48 "$FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.10 2002/06/18 00:16:59 kbyanc Exp $";
52 * arp - display, set, and delete arp table entries
56 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/sysctl.h>
61 #include <sys/ioctl.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
72 #include <arpa/inet.h>
84 #include <sys/types.h>
85 #include <sys/socket.h>
86 #include <net/ethernet.h>
88 void search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
89 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
));
90 void print_entry(struct sockaddr_dl
*sdl
,
91 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
92 void nuke_entry(struct sockaddr_dl
*sdl
,
93 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
);
94 int delete(char *host
, char *info
);
96 int set(int argc
, char **argv
);
100 int my_ether_aton(char *a
, struct ether_addr
*n
);
102 int get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
);
105 static int nflag
; /* no reverse dns lookups */
106 static int aflag
; /* do it for all entries */
109 struct sockaddr_in so_mask
;
110 struct sockaddr_inarp blank_sin
, sin_m
;
111 struct sockaddr_dl blank_sdl
, sdl_m
;
112 int expire_time
, flags
, doing_proxy
, proxy_only
, found_entry
;
114 struct rt_msghdr m_rtm
;
118 /* which function we're supposed to do */
126 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
127 #define SETFUNC(f) { if (func) usage(); func = (f); }
130 main(int argc
, char *argv
[])
136 while ((ch
= getopt(argc
, argv
, "andfsS")) != -1)
163 bzero(&so_mask
, sizeof(so_mask
));
165 so_mask
.sin_addr
.s_addr
= 0xffffffff;
166 bzero(&blank_sin
, sizeof(blank_sin
));
167 blank_sin
.sin_len
= sizeof(blank_sin
);
168 blank_sin
.sin_family
= AF_INET
;
169 bzero(&blank_sdl
, sizeof(blank_sdl
));
170 blank_sdl
.sdl_len
= sizeof(blank_sdl
);
171 blank_sdl
.sdl_family
= AF_LINK
;
180 search(0, print_entry
);
189 if (argc
< 2 || argc
> 6)
191 if (func
== F_REPLACE
)
192 (void) delete(argv
[0], NULL
);
193 rtn
= set(argc
, argv
) ? 1 : 0;
199 search(0, nuke_entry
);
201 if (argc
< 1 || argc
> 2)
203 rtn
= delete(argv
[0], argv
[1]);
217 * Process a file to set standard arp entries
224 char line
[100], arg
[5][50], *args
[5];
226 if ((fp
= fopen(name
, "r")) == NULL
)
227 errx(1, "cannot open %s", name
);
228 args
[0] = &arg
[0][0];
229 args
[1] = &arg
[1][0];
230 args
[2] = &arg
[2][0];
231 args
[3] = &arg
[3][0];
232 args
[4] = &arg
[4][0];
234 while(fgets(line
, 100, fp
) != NULL
) {
235 i
= sscanf(line
, "%49s %49s %49s %49s %49s", arg
[0], arg
[1],
236 arg
[2], arg
[3], arg
[4]);
238 warnx("bad line: %s", line
);
253 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
260 * Set an individual arp entry
263 set(int argc
, char **argv
)
266 register struct sockaddr_inarp
*addr
= &sin_m
;
267 register struct sockaddr_dl
*sdl
;
268 register struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
269 struct ether_addr
*ea
;
270 char *host
= argv
[0], *eaddr
= argv
[1];
277 addr
->sin_addr
.s_addr
= inet_addr(host
);
278 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
279 if (!(hp
= gethostbyname(host
))) {
280 warnx("%s: %s", host
, hstrerror(h_errno
));
283 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
284 sizeof addr
->sin_addr
);
286 doing_proxy
= flags
= proxy_only
= expire_time
= 0;
288 if (strncmp(argv
[0], "temp", 4) == 0) {
290 gettimeofday(&tv
, 0);
291 expire_time
= tv
.tv_sec
+ 20 * 60;
293 else if (strncmp(argv
[0], "pub", 3) == 0) {
294 flags
|= RTF_ANNOUNCE
;
296 if (argc
&& strncmp(argv
[1], "only", 3) == 0) {
298 sin_m
.sin_other
= SIN_PROXY
;
301 } else if (strncmp(argv
[0], "trail", 5) == 0) {
302 printf("%s: Sending trailers is no longer supported\n",
307 ea
= (struct ether_addr
*)LLADDR(&sdl_m
);
308 if (doing_proxy
&& !strcmp(eaddr
, "auto")) {
309 if (!get_ether_addr(addr
->sin_addr
.s_addr
, ea
)) {
310 printf("no interface found for %s\n",
311 inet_ntoa(addr
->sin_addr
));
314 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
316 if (my_ether_aton(eaddr
, ea
) == 0)
317 sdl_m
.sdl_alen
= ETHER_ADDR_LEN
;
320 if (rtmsg(RTM_GET
) < 0) {
324 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
325 sdl
= (struct sockaddr_dl
*)(ROUNDUP(addr
->sin_len
) + (char *)addr
);
326 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
327 if (sdl
->sdl_family
== AF_LINK
&&
328 (rtm
->rtm_flags
& RTF_LLINFO
) &&
329 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
330 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
331 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
334 if (doing_proxy
== 0) {
335 printf("set: can only proxy for %s\n", host
);
338 if (sin_m
.sin_other
& SIN_PROXY
) {
339 printf("set: proxy entry exists for non 802 device\n");
342 sin_m
.sin_other
= SIN_PROXY
;
347 if (sdl
->sdl_family
!= AF_LINK
) {
348 printf("cannot intuit interface index and type for %s\n", host
);
351 sdl_m
.sdl_type
= sdl
->sdl_type
;
352 sdl_m
.sdl_index
= sdl
->sdl_index
;
353 return (rtmsg(RTM_ADD
));
357 * Display an individual arp entry
363 struct sockaddr_inarp
*addr
= &sin_m
;
366 addr
->sin_addr
.s_addr
= inet_addr(host
);
367 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
368 if (!(hp
= gethostbyname(host
)))
369 errx(1, "%s: %s", host
, hstrerror(h_errno
));
370 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
371 sizeof addr
->sin_addr
);
373 search(addr
->sin_addr
.s_addr
, print_entry
);
374 if (found_entry
== 0) {
375 printf("%s (%s) -- no entry\n",
376 host
, inet_ntoa(addr
->sin_addr
));
383 * Delete an arp entry
386 delete(char *host
, char *info
)
389 register struct sockaddr_inarp
*addr
= &sin_m
;
390 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
391 struct sockaddr_dl
*sdl
;
396 if (strncmp(info
, "pub", 3) == 0)
397 sin_m
.sin_other
= SIN_PROXY
;
401 addr
->sin_addr
.s_addr
= inet_addr(host
);
402 if (addr
->sin_addr
.s_addr
== INADDR_NONE
) {
403 if (!(hp
= gethostbyname(host
))) {
404 warnx("%s: %s", host
, hstrerror(h_errno
));
407 bcopy((char *)hp
->h_addr
, (char *)&addr
->sin_addr
,
408 sizeof addr
->sin_addr
);
411 if (rtmsg(RTM_GET
) < 0) {
415 addr
= (struct sockaddr_inarp
*)(rtm
+ 1);
416 sdl
= (struct sockaddr_dl
*)(ROUNDUP(addr
->sin_len
) + (char *)addr
);
417 if (addr
->sin_addr
.s_addr
== sin_m
.sin_addr
.s_addr
) {
418 if (sdl
->sdl_family
== AF_LINK
&&
419 (rtm
->rtm_flags
& RTF_LLINFO
) &&
420 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
421 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
422 case IFT_ISO88024
: case IFT_ISO88025
: case IFT_L2VLAN
:
426 if (sin_m
.sin_other
& SIN_PROXY
) {
427 fprintf(stderr
, "delete: can't locate %s\n",host
);
430 sin_m
.sin_other
= SIN_PROXY
;
434 if (sdl
->sdl_family
!= AF_LINK
) {
435 printf("cannot locate %s\n", host
);
438 if (rtmsg(RTM_DELETE
) == 0) {
439 printf("%s (%s) deleted\n", host
, inet_ntoa(addr
->sin_addr
));
446 * Search the arp table and do some action on matching entries
449 search(u_long addr
, void (*action
)(struct sockaddr_dl
*sdl
,
450 struct sockaddr_inarp
*sin
, struct rt_msghdr
*rtm
))
454 char *lim
, *buf
, *next
;
455 struct rt_msghdr
*rtm
;
456 struct sockaddr_inarp
*sin2
;
457 struct sockaddr_dl
*sdl
;
463 mib
[4] = NET_RT_FLAGS
;
465 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
466 errx(1, "route-sysctl-estimate");
467 if ((buf
= malloc(needed
)) == NULL
)
469 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
470 errx(1, "actual retrieval of routing table");
472 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
473 rtm
= (struct rt_msghdr
*)next
;
474 sin2
= (struct sockaddr_inarp
*)(rtm
+ 1);
475 sdl
= (struct sockaddr_dl
*)((char*)sin2
+ ROUNDUP(sin2
->sin_len
));
477 if (addr
!= sin2
->sin_addr
.s_addr
)
481 (*action
)(sdl
, sin2
, rtm
);
488 * Stolen and adapted from ifconfig
491 print_lladdr(struct sockaddr_dl
*sdl
)
496 cp
= (char *)LLADDR(sdl
);
497 if ((n
= sdl
->sdl_alen
) > 0) {
499 printf("%x%s",*cp
++ & 0xff, n
>0? ":" : "");
505 * Display an arp entry
508 print_entry(struct sockaddr_dl
*sdl
,
509 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
)
513 char ifname
[IF_NAMESIZE
];
516 hp
= gethostbyaddr((caddr_t
)&(addr
->sin_addr
),
517 sizeof addr
->sin_addr
, AF_INET
);
524 if (h_errno
== TRY_AGAIN
)
527 printf("%s (%s) at ", host
, inet_ntoa(addr
->sin_addr
));
531 printf("(incomplete)");
532 if (if_indextoname(sdl
->sdl_index
, ifname
) != NULL
)
533 printf(" on %s", ifname
);
534 if (rtm
->rtm_rmx
.rmx_expire
== 0)
535 printf(" permanent");
536 if (addr
->sin_other
& SIN_PROXY
)
537 printf(" published (proxy only)");
538 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
539 addr
= (struct sockaddr_inarp
*)
540 (ROUNDUP(sdl
->sdl_len
) + (char *)sdl
);
541 if (addr
->sin_addr
.s_addr
== 0xffffffff)
542 printf(" published");
543 if (addr
->sin_len
!= 8)
546 switch(sdl
->sdl_type
) {
548 printf(" [ethernet]");
552 printf(" [token-ring]");
553 trld
= SDL_ISO88025(sdl
);
554 if (trld
->trld_rcf
!= 0) {
555 printf(" rt=%x", ntohs(trld
->trld_rcf
));
557 seg
< ((TR_RCF_RIFLEN(trld
->trld_rcf
) - 2 ) / 2);
559 printf(":%x", ntohs(*(trld
->trld_route
[seg
])));
567 printf(" [firewire]");
581 nuke_entry(struct sockaddr_dl
*sdl
,
582 struct sockaddr_inarp
*addr
, struct rt_msghdr
*rtm
)
586 snprintf(ip
, sizeof(ip
), "%s", inet_ntoa(addr
->sin_addr
));
591 my_ether_aton(char *a
, struct ether_addr
*n
)
593 struct ether_addr
*ea
;
595 if ((ea
= ether_aton(a
)) == NULL
) {
596 warnx("invalid Ethernet address '%s'", a
);
606 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
607 "usage: arp [-n] hostname",
609 " arp -d hostname [pub]",
611 " arp -s hostname ether_addr [temp] [pub]",
612 " arp -S hostname ether_addr [temp] [pub]",
622 register struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
623 register char *cp
= m_rtmsg
.m_space
;
627 if (cmd
== RTM_DELETE
)
629 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
630 rtm
->rtm_flags
= flags
;
631 rtm
->rtm_version
= RTM_VERSION
;
635 errx(1, "internal wrong cmd");
637 rtm
->rtm_addrs
|= RTA_GATEWAY
;
638 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
639 rtm
->rtm_inits
= RTV_EXPIRE
;
640 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
644 sin_m
.sin_other
= SIN_PROXY
;
646 rtm
->rtm_addrs
|= RTA_NETMASK
;
647 rtm
->rtm_flags
&= ~RTF_HOST
;
652 rtm
->rtm_addrs
|= RTA_DST
;
654 #define NEXTADDR(w, s) \
655 if (rtm->rtm_addrs & (w)) { \
656 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
658 NEXTADDR(RTA_DST
, sin_m
);
659 NEXTADDR(RTA_GATEWAY
, sdl_m
);
660 NEXTADDR(RTA_NETMASK
, so_mask
);
662 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
665 rtm
->rtm_seq
= ++seq
;
667 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
668 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
669 warn("writing to routing socket");
674 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
675 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
677 warn("read from routing socket");
682 * get_ether_addr - get the hardware address of an interface on the
683 * the same subnet as ipaddr.
688 get_ether_addr(u_int32_t ipaddr
, struct ether_addr
*hwaddr
)
690 struct ifreq
*ifr
, *ifend
, *ifp
;
692 struct sockaddr_dl
*dla
;
695 struct ifreq ifs
[MAX_IFS
];
698 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
702 ifc
.ifc_len
= sizeof(ifs
);
704 if (ioctl(sock
, SIOCGIFCONF
, &ifc
) < 0) {
705 warnx("ioctl(SIOCGIFCONF)");
711 * Scan through looking for an interface with an Internet
712 * address on the same subnet as `ipaddr'.
714 ifend
= (struct ifreq
*) (ifc
.ifc_buf
+ ifc
.ifc_len
);
715 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
716 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
717 ina
= ((struct sockaddr_in
*)
718 &ifr
->ifr_addr
)->sin_addr
.s_addr
;
719 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
,
720 sizeof(ifreq
.ifr_name
));
722 * Check that the interface is up,
723 * and not point-to-point or loopback.
725 if (ioctl(sock
, SIOCGIFFLAGS
, &ifreq
) < 0)
727 if ((ifreq
.ifr_flags
&
728 (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|
729 IFF_LOOPBACK
|IFF_NOARP
))
730 != (IFF_UP
|IFF_BROADCAST
))
733 * Get its netmask and check that it's on
736 if (ioctl(sock
, SIOCGIFNETMASK
, &ifreq
) < 0)
738 mask
= ((struct sockaddr_in
*)
739 &ifreq
.ifr_addr
)->sin_addr
.s_addr
;
740 if ((ipaddr
& mask
) != (ina
& mask
))
745 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
746 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));
755 * Now scan through again looking for a link-level address
756 * for this interface.
759 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ) {
760 if (strcmp(ifp
->ifr_name
, ifr
->ifr_name
) == 0
761 && ifr
->ifr_addr
.sa_family
== AF_LINK
) {
763 * Found the link-level address - copy it out
765 dla
= (struct sockaddr_dl
*) &ifr
->ifr_addr
;
767 printf("using interface %s for proxy with address ",
771 return dla
->sdl_alen
;
773 ifr
= (struct ifreq
*) ((char *)&ifr
->ifr_addr
774 + MAX(ifr
->ifr_addr
.sa_len
, sizeof(ifr
->ifr_addr
)));