1dea673e9SRodney W. Grimes /* 2dea673e9SRodney W. Grimes * Copyright (c) 1984, 1993 3dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4dea673e9SRodney W. Grimes * 5dea673e9SRodney W. Grimes * This code is derived from software contributed to Berkeley by 6dea673e9SRodney W. Grimes * Sun Microsystems, Inc. 7dea673e9SRodney W. Grimes * 8dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 10dea673e9SRodney W. Grimes * are met: 11dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 17dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 18dea673e9SRodney W. Grimes * without specific prior written permission. 19dea673e9SRodney W. Grimes * 20dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30dea673e9SRodney W. Grimes * SUCH DAMAGE. 31dea673e9SRodney W. Grimes */ 32dea673e9SRodney W. Grimes 33b728350eSDavid E. O'Brien #if 0 34dea673e9SRodney W. Grimes #ifndef lint 35a42a667dSPoul-Henning Kamp static char const copyright[] = 36dea673e9SRodney W. Grimes "@(#) Copyright (c) 1984, 1993\n\ 37dea673e9SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 38dea673e9SRodney W. Grimes #endif /* not lint */ 39dea673e9SRodney W. Grimes 40dea673e9SRodney W. Grimes #ifndef lint 41a42a667dSPoul-Henning Kamp static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94"; 42dea673e9SRodney W. Grimes #endif /* not lint */ 43b728350eSDavid E. O'Brien #endif 44b728350eSDavid E. O'Brien #include <sys/cdefs.h> 45b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 46dea673e9SRodney W. Grimes 47dea673e9SRodney W. Grimes /* 48dea673e9SRodney W. Grimes * arp - display, set, and delete arp table entries 49dea673e9SRodney W. Grimes */ 50dea673e9SRodney W. Grimes 51dea673e9SRodney W. Grimes 52dea673e9SRodney W. Grimes #include <sys/param.h> 53dea673e9SRodney W. Grimes #include <sys/file.h> 54dea673e9SRodney W. Grimes #include <sys/socket.h> 55a42a667dSPoul-Henning Kamp #include <sys/sockio.h> 56dea673e9SRodney W. Grimes #include <sys/sysctl.h> 57a42a667dSPoul-Henning Kamp #include <sys/ioctl.h> 58628d2ac1SGarrett Wollman #include <sys/time.h> 59dea673e9SRodney W. Grimes 60dea673e9SRodney W. Grimes #include <net/if.h> 61dea673e9SRodney W. Grimes #include <net/if_dl.h> 62dea673e9SRodney W. Grimes #include <net/if_types.h> 63dea673e9SRodney W. Grimes #include <net/route.h> 642ab778e1SBill Paul #include <net/iso88025.h> 65dea673e9SRodney W. Grimes 66dea673e9SRodney W. Grimes #include <netinet/in.h> 67dea673e9SRodney W. Grimes #include <netinet/if_ether.h> 68dea673e9SRodney W. Grimes 69dea673e9SRodney W. Grimes #include <arpa/inet.h> 70dea673e9SRodney W. Grimes 7144acbc1aSRuslan Ermilov #include <ctype.h> 72c72049e4SPhilippe Charnier #include <err.h> 73dea673e9SRodney W. Grimes #include <errno.h> 74c72049e4SPhilippe Charnier #include <netdb.h> 75dea673e9SRodney W. Grimes #include <nlist.h> 76c72049e4SPhilippe Charnier #include <paths.h> 77dea673e9SRodney W. Grimes #include <stdio.h> 78a42a667dSPoul-Henning Kamp #include <stdlib.h> 79467a0b06SMike Barcroft #include <string.h> 80a42a667dSPoul-Henning Kamp #include <strings.h> 81c72049e4SPhilippe Charnier #include <unistd.h> 82dea673e9SRodney W. Grimes 83bdf932aeSLuigi Rizzo typedef void (action_fn)(struct sockaddr_dl *sdl, 84bdf932aeSLuigi Rizzo struct sockaddr_inarp *s_in, struct rt_msghdr *rtm); 85a42a667dSPoul-Henning Kamp 86bdf932aeSLuigi Rizzo static int search(u_long addr, action_fn *action); 87bdf932aeSLuigi Rizzo static action_fn print_entry; 88bdf932aeSLuigi Rizzo static action_fn nuke_entry; 89bdf932aeSLuigi Rizzo 9068839124SLuigi Rizzo static int delete(char *host, int do_proxy); 91bdf932aeSLuigi Rizzo static void usage(void); 92bdf932aeSLuigi Rizzo static int set(int argc, char **argv); 93f3f8b226SRuslan Ermilov static int get(char *host); 94bdf932aeSLuigi Rizzo static int file(char *name); 9568839124SLuigi Rizzo static struct rt_msghdr *rtmsg(int cmd, 9668839124SLuigi Rizzo struct sockaddr_inarp *dst, struct sockaddr_dl *sdl); 97f3f8b226SRuslan Ermilov static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); 98f3f8b226SRuslan Ermilov static struct sockaddr_inarp *getaddr(char *host); 99f3f8b226SRuslan Ermilov static int valid_type(int type); 100bdf932aeSLuigi Rizzo 1018dc4b495SJulian Elischer static int nflag; /* no reverse dns lookups */ 102b9de94e9SYaroslav Tykhiy static char *rifname; 103dea673e9SRodney W. Grimes 104bdf932aeSLuigi Rizzo static int expire_time, flags, doing_proxy, proxy_only; 10568839124SLuigi Rizzo 1068dc4b495SJulian Elischer /* which function we're supposed to do */ 1078dc4b495SJulian Elischer #define F_GET 1 1088dc4b495SJulian Elischer #define F_SET 2 1098dc4b495SJulian Elischer #define F_FILESET 3 1108dc4b495SJulian Elischer #define F_REPLACE 4 1118dc4b495SJulian Elischer #define F_DELETE 5 1128dc4b495SJulian Elischer 1138dc4b495SJulian Elischer #define SETFUNC(f) { if (func) usage(); func = (f); } 1148dc4b495SJulian Elischer 115a42a667dSPoul-Henning Kamp int 1163f844a22SRuslan Ermilov main(int argc, char *argv[]) 117dea673e9SRodney W. Grimes { 1188dc4b495SJulian Elischer int ch, func = 0; 1198dc4b495SJulian Elischer int rtn = 0; 120bdf932aeSLuigi Rizzo int aflag = 0; /* do it for all entries */ 121dea673e9SRodney W. Grimes 122b9de94e9SYaroslav Tykhiy while ((ch = getopt(argc, argv, "andfsSi:")) != -1) 123dea673e9SRodney W. Grimes switch((char)ch) { 124dea673e9SRodney W. Grimes case 'a': 1258dc4b495SJulian Elischer aflag = 1; 1268dc4b495SJulian Elischer break; 127dea673e9SRodney W. Grimes case 'd': 1288dc4b495SJulian Elischer SETFUNC(F_DELETE); 1298dc4b495SJulian Elischer break; 130dea673e9SRodney W. Grimes case 'n': 131dea673e9SRodney W. Grimes nflag = 1; 1328dc4b495SJulian Elischer break; 133a42a667dSPoul-Henning Kamp case 'S': 1348dc4b495SJulian Elischer SETFUNC(F_REPLACE); 1358dc4b495SJulian Elischer break; 136dea673e9SRodney W. Grimes case 's': 1378dc4b495SJulian Elischer SETFUNC(F_SET); 1388dc4b495SJulian Elischer break; 13995319e17SJordan K. Hubbard case 'f' : 1408dc4b495SJulian Elischer SETFUNC(F_FILESET); 1418dc4b495SJulian Elischer break; 142b9de94e9SYaroslav Tykhiy case 'i': 143b9de94e9SYaroslav Tykhiy rifname = optarg; 144b9de94e9SYaroslav Tykhiy break; 145dea673e9SRodney W. Grimes case '?': 146dea673e9SRodney W. Grimes default: 147dea673e9SRodney W. Grimes usage(); 148dea673e9SRodney W. Grimes } 1498dc4b495SJulian Elischer argc -= optind; 1508dc4b495SJulian Elischer argv += optind; 1518dc4b495SJulian Elischer 1528dc4b495SJulian Elischer if (!func) 1538dc4b495SJulian Elischer func = F_GET; 154b9de94e9SYaroslav Tykhiy if (rifname) { 15508369852SBrooks Davis if (func != F_GET && !(func == F_DELETE && aflag)) 156b9de94e9SYaroslav Tykhiy errx(1, "-i not applicable to this operation"); 157b9de94e9SYaroslav Tykhiy if (if_nametoindex(rifname) == 0) { 158b9de94e9SYaroslav Tykhiy if (errno == ENXIO) 159b9de94e9SYaroslav Tykhiy errx(1, "interface %s does not exist", rifname); 160b9de94e9SYaroslav Tykhiy else 161b9de94e9SYaroslav Tykhiy err(1, "if_nametoindex(%s)", rifname); 162b9de94e9SYaroslav Tykhiy } 163b9de94e9SYaroslav Tykhiy } 1648dc4b495SJulian Elischer switch (func) { 1658dc4b495SJulian Elischer case F_GET: 1668dc4b495SJulian Elischer if (aflag) { 1678dc4b495SJulian Elischer if (argc != 0) 168dea673e9SRodney W. Grimes usage(); 1698dc4b495SJulian Elischer search(0, print_entry); 1708dc4b495SJulian Elischer } else { 1718dc4b495SJulian Elischer if (argc != 1) 1728dc4b495SJulian Elischer usage(); 173f3f8b226SRuslan Ermilov rtn = get(argv[0]); 1748dc4b495SJulian Elischer } 1758dc4b495SJulian Elischer break; 1768dc4b495SJulian Elischer case F_SET: 1778dc4b495SJulian Elischer case F_REPLACE: 1783f844a22SRuslan Ermilov if (argc < 2 || argc > 6) 1798dc4b495SJulian Elischer usage(); 1808dc4b495SJulian Elischer if (func == F_REPLACE) 181f3f8b226SRuslan Ermilov (void)delete(argv[0], 0); 1828dc4b495SJulian Elischer rtn = set(argc, argv) ? 1 : 0; 1838dc4b495SJulian Elischer break; 1848dc4b495SJulian Elischer case F_DELETE: 1858dc4b495SJulian Elischer if (aflag) { 1868dc4b495SJulian Elischer if (argc != 0) 1878dc4b495SJulian Elischer usage(); 1888dc4b495SJulian Elischer search(0, nuke_entry); 1898dc4b495SJulian Elischer } else { 19068839124SLuigi Rizzo if (argc == 2 && strncmp(argv[1], "pub", 3) == 0) 19168839124SLuigi Rizzo ch = SIN_PROXY; 19268839124SLuigi Rizzo else if (argc == 1) 19368839124SLuigi Rizzo ch = 0; 19468839124SLuigi Rizzo else 1958dc4b495SJulian Elischer usage(); 19668839124SLuigi Rizzo rtn = delete(argv[0], ch); 1978dc4b495SJulian Elischer } 1988dc4b495SJulian Elischer break; 1998dc4b495SJulian Elischer case F_FILESET: 2008dc4b495SJulian Elischer if (argc != 1) 2018dc4b495SJulian Elischer usage(); 2028dc4b495SJulian Elischer rtn = file(argv[0]); 2038dc4b495SJulian Elischer break; 2048dc4b495SJulian Elischer } 2058dc4b495SJulian Elischer 2068dc4b495SJulian Elischer return (rtn); 207dea673e9SRodney W. Grimes } 208dea673e9SRodney W. Grimes 209dea673e9SRodney W. Grimes /* 210dea673e9SRodney W. Grimes * Process a file to set standard arp entries 211dea673e9SRodney W. Grimes */ 212bdf932aeSLuigi Rizzo static int 213a42a667dSPoul-Henning Kamp file(char *name) 214dea673e9SRodney W. Grimes { 215dea673e9SRodney W. Grimes FILE *fp; 216dea673e9SRodney W. Grimes int i, retval; 2173adcd042SRuslan Ermilov char line[100], arg[5][50], *args[5], *p; 218dea673e9SRodney W. Grimes 219c72049e4SPhilippe Charnier if ((fp = fopen(name, "r")) == NULL) 220e2416749SMaxime Henrion err(1, "cannot open %s", name); 221dea673e9SRodney W. Grimes args[0] = &arg[0][0]; 222dea673e9SRodney W. Grimes args[1] = &arg[1][0]; 223dea673e9SRodney W. Grimes args[2] = &arg[2][0]; 224dea673e9SRodney W. Grimes args[3] = &arg[3][0]; 225dea673e9SRodney W. Grimes args[4] = &arg[4][0]; 226dea673e9SRodney W. Grimes retval = 0; 227d0691403SKevin Lo while(fgets(line, sizeof(line), fp) != NULL) { 2283adcd042SRuslan Ermilov if ((p = strchr(line, '#')) != NULL) 2293adcd042SRuslan Ermilov *p = '\0'; 2303adcd042SRuslan Ermilov for (p = line; isblank(*p); p++); 2314bfc3624SRuslan Ermilov if (*p == '\n' || *p == '\0') 23244acbc1aSRuslan Ermilov continue; 2333adcd042SRuslan Ermilov i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 234135adb1eSJordan K. Hubbard arg[2], arg[3], arg[4]); 235dea673e9SRodney W. Grimes if (i < 2) { 236c72049e4SPhilippe Charnier warnx("bad line: %s", line); 237dea673e9SRodney W. Grimes retval = 1; 238dea673e9SRodney W. Grimes continue; 239dea673e9SRodney W. Grimes } 240dea673e9SRodney W. Grimes if (set(i, args)) 241dea673e9SRodney W. Grimes retval = 1; 242dea673e9SRodney W. Grimes } 243dea673e9SRodney W. Grimes fclose(fp); 244dea673e9SRodney W. Grimes return (retval); 245dea673e9SRodney W. Grimes } 246dea673e9SRodney W. Grimes 247dea673e9SRodney W. Grimes /* 24868839124SLuigi Rizzo * Given a hostname, fills up a (static) struct sockaddr_inarp with 24968839124SLuigi Rizzo * the address of the host and returns a pointer to the 25068839124SLuigi Rizzo * structure. 251dea673e9SRodney W. Grimes */ 25268839124SLuigi Rizzo static struct sockaddr_inarp * 25368839124SLuigi Rizzo getaddr(char *host) 254dea673e9SRodney W. Grimes { 255dea673e9SRodney W. Grimes struct hostent *hp; 25668839124SLuigi Rizzo static struct sockaddr_inarp reply; 25768839124SLuigi Rizzo 25868839124SLuigi Rizzo bzero(&reply, sizeof(reply)); 25968839124SLuigi Rizzo reply.sin_len = sizeof(reply); 26068839124SLuigi Rizzo reply.sin_family = AF_INET; 26168839124SLuigi Rizzo reply.sin_addr.s_addr = inet_addr(host); 26268839124SLuigi Rizzo if (reply.sin_addr.s_addr == INADDR_NONE) { 26368839124SLuigi Rizzo if (!(hp = gethostbyname(host))) { 26468839124SLuigi Rizzo warnx("%s: %s", host, hstrerror(h_errno)); 265f3f8b226SRuslan Ermilov return (NULL); 26668839124SLuigi Rizzo } 26768839124SLuigi Rizzo bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, 26868839124SLuigi Rizzo sizeof reply.sin_addr); 26968839124SLuigi Rizzo } 270f3f8b226SRuslan Ermilov return (&reply); 27168839124SLuigi Rizzo } 27268839124SLuigi Rizzo 27368839124SLuigi Rizzo /* 274f3f8b226SRuslan Ermilov * Returns true if the type is a valid one for ARP. 27568839124SLuigi Rizzo */ 27668839124SLuigi Rizzo static int 27768839124SLuigi Rizzo valid_type(int type) 27868839124SLuigi Rizzo { 279f3f8b226SRuslan Ermilov 28068839124SLuigi Rizzo switch (type) { 28168839124SLuigi Rizzo case IFT_ETHER: 28268839124SLuigi Rizzo case IFT_FDDI: 28368839124SLuigi Rizzo case IFT_ISO88023: 28468839124SLuigi Rizzo case IFT_ISO88024: 28568839124SLuigi Rizzo case IFT_ISO88025: 28668839124SLuigi Rizzo case IFT_L2VLAN: 2879af9b983SAndrew Thompson case IFT_BRIDGE: 288f3f8b226SRuslan Ermilov return (1); 289f3f8b226SRuslan Ermilov default: 290f3f8b226SRuslan Ermilov return (0); 29168839124SLuigi Rizzo } 29268839124SLuigi Rizzo } 29368839124SLuigi Rizzo 29468839124SLuigi Rizzo /* 29568839124SLuigi Rizzo * Set an individual arp entry 29668839124SLuigi Rizzo */ 29768839124SLuigi Rizzo static int 29868839124SLuigi Rizzo set(int argc, char **argv) 29968839124SLuigi Rizzo { 30068839124SLuigi Rizzo struct sockaddr_inarp *addr; 30168839124SLuigi Rizzo struct sockaddr_inarp *dst; /* what are we looking for */ 302e2416749SMaxime Henrion struct sockaddr_dl *sdl; 303bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 304a03b1b7cSRuslan Ermilov struct ether_addr *ea; 305dea673e9SRodney W. Grimes char *host = argv[0], *eaddr = argv[1]; 30668839124SLuigi Rizzo struct sockaddr_dl sdl_m; 307dea673e9SRodney W. Grimes 308dea673e9SRodney W. Grimes argc -= 2; 309dea673e9SRodney W. Grimes argv += 2; 310bdf932aeSLuigi Rizzo 311bdf932aeSLuigi Rizzo bzero(&sdl_m, sizeof(sdl_m)); 312bdf932aeSLuigi Rizzo sdl_m.sdl_len = sizeof(sdl_m); 313bdf932aeSLuigi Rizzo sdl_m.sdl_family = AF_LINK; 314bdf932aeSLuigi Rizzo 31568839124SLuigi Rizzo dst = getaddr(host); 31668839124SLuigi Rizzo if (dst == NULL) 317dea673e9SRodney W. Grimes return (1); 3183f844a22SRuslan Ermilov doing_proxy = flags = proxy_only = expire_time = 0; 319dea673e9SRodney W. Grimes while (argc-- > 0) { 320dea673e9SRodney W. Grimes if (strncmp(argv[0], "temp", 4) == 0) { 3213f844a22SRuslan Ermilov struct timeval tv; 3223f844a22SRuslan Ermilov gettimeofday(&tv, 0); 3233f844a22SRuslan Ermilov expire_time = tv.tv_sec + 20 * 60; 324dea673e9SRodney W. Grimes } 325dea673e9SRodney W. Grimes else if (strncmp(argv[0], "pub", 3) == 0) { 326dea673e9SRodney W. Grimes flags |= RTF_ANNOUNCE; 3273f844a22SRuslan Ermilov doing_proxy = 1; 3283f844a22SRuslan Ermilov if (argc && strncmp(argv[1], "only", 3) == 0) { 3293f844a22SRuslan Ermilov proxy_only = 1; 33068839124SLuigi Rizzo dst->sin_other = SIN_PROXY; 3313f844a22SRuslan Ermilov argc--; argv++; 3323f844a22SRuslan Ermilov } 333dea673e9SRodney W. Grimes } else if (strncmp(argv[0], "trail", 5) == 0) { 33468839124SLuigi Rizzo /* XXX deprecated and undocumented feature */ 335dea673e9SRodney W. Grimes printf("%s: Sending trailers is no longer supported\n", 336dea673e9SRodney W. Grimes host); 337dea673e9SRodney W. Grimes } 338dea673e9SRodney W. Grimes argv++; 339dea673e9SRodney W. Grimes } 340a03b1b7cSRuslan Ermilov ea = (struct ether_addr *)LLADDR(&sdl_m); 341a42a667dSPoul-Henning Kamp if (doing_proxy && !strcmp(eaddr, "auto")) { 34268839124SLuigi Rizzo if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { 343eec827b0SRuslan Ermilov printf("no interface found for %s\n", 34468839124SLuigi Rizzo inet_ntoa(dst->sin_addr)); 345a42a667dSPoul-Henning Kamp return (1); 346a42a667dSPoul-Henning Kamp } 347a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 348a42a667dSPoul-Henning Kamp } else { 34968839124SLuigi Rizzo struct ether_addr *ea1 = ether_aton(eaddr); 35068839124SLuigi Rizzo 351a47c388cSGleb Smirnoff if (ea1 == NULL) { 35268839124SLuigi Rizzo warnx("invalid Ethernet address '%s'", eaddr); 353a47c388cSGleb Smirnoff return (1); 354a47c388cSGleb Smirnoff } else { 35568839124SLuigi Rizzo *ea = *ea1; 356a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 357a42a667dSPoul-Henning Kamp } 35868839124SLuigi Rizzo } 35968839124SLuigi Rizzo for (;;) { /* try at most twice */ 36068839124SLuigi Rizzo rtm = rtmsg(RTM_GET, dst, &sdl_m); 361bdf932aeSLuigi Rizzo if (rtm == NULL) { 362c72049e4SPhilippe Charnier warn("%s", host); 363dea673e9SRodney W. Grimes return (1); 364dea673e9SRodney W. Grimes } 3659d34414bSMike Heffner addr = (struct sockaddr_inarp *)(rtm + 1); 3660b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 36768839124SLuigi Rizzo if (addr->sin_addr.s_addr != dst->sin_addr.s_addr) 36868839124SLuigi Rizzo break; 369dea673e9SRodney W. Grimes if (sdl->sdl_family == AF_LINK && 370dea673e9SRodney W. Grimes (rtm->rtm_flags & RTF_LLINFO) && 37168839124SLuigi Rizzo !(rtm->rtm_flags & RTF_GATEWAY) && 37268839124SLuigi Rizzo valid_type(sdl->sdl_type) ) 37368839124SLuigi Rizzo break; 374dea673e9SRodney W. Grimes if (doing_proxy == 0) { 375dea673e9SRodney W. Grimes printf("set: can only proxy for %s\n", host); 376dea673e9SRodney W. Grimes return (1); 377dea673e9SRodney W. Grimes } 37868839124SLuigi Rizzo if (dst->sin_other & SIN_PROXY) { 379dea673e9SRodney W. Grimes printf("set: proxy entry exists for non 802 device\n"); 380dea673e9SRodney W. Grimes return (1); 381dea673e9SRodney W. Grimes } 38268839124SLuigi Rizzo dst->sin_other = SIN_PROXY; 3833f844a22SRuslan Ermilov proxy_only = 1; 384dea673e9SRodney W. Grimes } 38568839124SLuigi Rizzo 386dea673e9SRodney W. Grimes if (sdl->sdl_family != AF_LINK) { 387dea673e9SRodney W. Grimes printf("cannot intuit interface index and type for %s\n", host); 388dea673e9SRodney W. Grimes return (1); 389dea673e9SRodney W. Grimes } 390dea673e9SRodney W. Grimes sdl_m.sdl_type = sdl->sdl_type; 391dea673e9SRodney W. Grimes sdl_m.sdl_index = sdl->sdl_index; 392cf779589SGleb Smirnoff return (rtmsg(RTM_ADD, dst, &sdl_m) == NULL); 393dea673e9SRodney W. Grimes } 394dea673e9SRodney W. Grimes 395dea673e9SRodney W. Grimes /* 396dea673e9SRodney W. Grimes * Display an individual arp entry 397dea673e9SRodney W. Grimes */ 398f3f8b226SRuslan Ermilov static int 399a42a667dSPoul-Henning Kamp get(char *host) 400dea673e9SRodney W. Grimes { 40168839124SLuigi Rizzo struct sockaddr_inarp *addr; 402dea673e9SRodney W. Grimes 40368839124SLuigi Rizzo addr = getaddr(host); 40468839124SLuigi Rizzo if (addr == NULL) 405f3f8b226SRuslan Ermilov return (1); 406bdf932aeSLuigi Rizzo if (0 == search(addr->sin_addr.s_addr, print_entry)) { 407b9de94e9SYaroslav Tykhiy printf("%s (%s) -- no entry", 4089d34414bSMike Heffner host, inet_ntoa(addr->sin_addr)); 409b9de94e9SYaroslav Tykhiy if (rifname) 410b9de94e9SYaroslav Tykhiy printf(" on %s", rifname); 411b9de94e9SYaroslav Tykhiy printf("\n"); 412f3f8b226SRuslan Ermilov return (1); 413dea673e9SRodney W. Grimes } 414f3f8b226SRuslan Ermilov return (0); 415dea673e9SRodney W. Grimes } 416dea673e9SRodney W. Grimes 417dea673e9SRodney W. Grimes /* 418dea673e9SRodney W. Grimes * Delete an arp entry 419dea673e9SRodney W. Grimes */ 420bdf932aeSLuigi Rizzo static int 42168839124SLuigi Rizzo delete(char *host, int do_proxy) 422dea673e9SRodney W. Grimes { 42368839124SLuigi Rizzo struct sockaddr_inarp *addr, *dst; 424bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 425dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 426dea673e9SRodney W. Grimes 42768839124SLuigi Rizzo dst = getaddr(host); 42868839124SLuigi Rizzo if (dst == NULL) 429f3f8b226SRuslan Ermilov return (1); 43068839124SLuigi Rizzo dst->sin_other = do_proxy; 43168839124SLuigi Rizzo for (;;) { /* try twice */ 43268839124SLuigi Rizzo rtm = rtmsg(RTM_GET, dst, NULL); 433bdf932aeSLuigi Rizzo if (rtm == NULL) { 434c72049e4SPhilippe Charnier warn("%s", host); 435dea673e9SRodney W. Grimes return (1); 436dea673e9SRodney W. Grimes } 4379d34414bSMike Heffner addr = (struct sockaddr_inarp *)(rtm + 1); 4380b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 43968839124SLuigi Rizzo if (addr->sin_addr.s_addr == dst->sin_addr.s_addr && 44068839124SLuigi Rizzo sdl->sdl_family == AF_LINK && 441dea673e9SRodney W. Grimes (rtm->rtm_flags & RTF_LLINFO) && 44268839124SLuigi Rizzo !(rtm->rtm_flags & RTF_GATEWAY) && 44368839124SLuigi Rizzo valid_type(sdl->sdl_type) ) 44468839124SLuigi Rizzo break; /* found it */ 44568839124SLuigi Rizzo if (dst->sin_other & SIN_PROXY) { 44668839124SLuigi Rizzo fprintf(stderr, "delete: cannot locate %s\n",host); 447dea673e9SRodney W. Grimes return (1); 448dea673e9SRodney W. Grimes } 44968839124SLuigi Rizzo dst->sin_other = SIN_PROXY; 45068839124SLuigi Rizzo } 45168839124SLuigi Rizzo if (rtmsg(RTM_DELETE, dst, NULL) != NULL) { 4529d34414bSMike Heffner printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr)); 453a42a667dSPoul-Henning Kamp return (0); 454a42a667dSPoul-Henning Kamp } 455a42a667dSPoul-Henning Kamp return (1); 456dea673e9SRodney W. Grimes } 457dea673e9SRodney W. Grimes 458dea673e9SRodney W. Grimes /* 4598dc4b495SJulian Elischer * Search the arp table and do some action on matching entries 460dea673e9SRodney W. Grimes */ 461bdf932aeSLuigi Rizzo static int 462bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action) 463dea673e9SRodney W. Grimes { 464dea673e9SRodney W. Grimes int mib[6]; 465dea673e9SRodney W. Grimes size_t needed; 46666658902SMaxim Konovalov char *lim, *buf, *newbuf, *next; 467dea673e9SRodney W. Grimes struct rt_msghdr *rtm; 4689d34414bSMike Heffner struct sockaddr_inarp *sin2; 469dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 470b9de94e9SYaroslav Tykhiy char ifname[IF_NAMESIZE]; 47166658902SMaxim Konovalov int st, found_entry = 0; 472dea673e9SRodney W. Grimes 473dea673e9SRodney W. Grimes mib[0] = CTL_NET; 474dea673e9SRodney W. Grimes mib[1] = PF_ROUTE; 475dea673e9SRodney W. Grimes mib[2] = 0; 476dea673e9SRodney W. Grimes mib[3] = AF_INET; 477dea673e9SRodney W. Grimes mib[4] = NET_RT_FLAGS; 478dea673e9SRodney W. Grimes mib[5] = RTF_LLINFO; 479dea673e9SRodney W. Grimes if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 480e2416749SMaxime Henrion err(1, "route-sysctl-estimate"); 48168839124SLuigi Rizzo if (needed == 0) /* empty table */ 482bdf932aeSLuigi Rizzo return 0; 48366658902SMaxim Konovalov buf = NULL; 48419beed5eSMaxim Konovalov for (;;) { 48566658902SMaxim Konovalov newbuf = realloc(buf, needed); 48666658902SMaxim Konovalov if (newbuf == NULL) { 48766658902SMaxim Konovalov if (buf != NULL) 48866658902SMaxim Konovalov free(buf); 48966658902SMaxim Konovalov errx(1, "could not reallocate memory"); 49066658902SMaxim Konovalov } 49166658902SMaxim Konovalov buf = newbuf; 49266658902SMaxim Konovalov st = sysctl(mib, 6, buf, &needed, NULL, 0); 49319beed5eSMaxim Konovalov if (st == 0 || errno != ENOMEM) 49419beed5eSMaxim Konovalov break; 49519beed5eSMaxim Konovalov needed += needed / 8; 49619beed5eSMaxim Konovalov } 49766658902SMaxim Konovalov if (st == -1) 498e2416749SMaxime Henrion err(1, "actual retrieval of routing table"); 499dea673e9SRodney W. Grimes lim = buf + needed; 500dea673e9SRodney W. Grimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 501dea673e9SRodney W. Grimes rtm = (struct rt_msghdr *)next; 5029d34414bSMike Heffner sin2 = (struct sockaddr_inarp *)(rtm + 1); 5031a5ff928SStefan Farfeleder sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); 504b9de94e9SYaroslav Tykhiy if (rifname && if_indextoname(sdl->sdl_index, ifname) && 505b9de94e9SYaroslav Tykhiy strcmp(ifname, rifname)) 506b9de94e9SYaroslav Tykhiy continue; 507dea673e9SRodney W. Grimes if (addr) { 5089d34414bSMike Heffner if (addr != sin2->sin_addr.s_addr) 509dea673e9SRodney W. Grimes continue; 510dea673e9SRodney W. Grimes found_entry = 1; 511dea673e9SRodney W. Grimes } 5129d34414bSMike Heffner (*action)(sdl, sin2, rtm); 5138dc4b495SJulian Elischer } 514ae14be20SYaroslav Tykhiy free(buf); 515f3f8b226SRuslan Ermilov return (found_entry); 5168dc4b495SJulian Elischer } 5178dc4b495SJulian Elischer 5188dc4b495SJulian Elischer /* 5198dc4b495SJulian Elischer * Display an arp entry 5208dc4b495SJulian Elischer */ 521bdf932aeSLuigi Rizzo static void 5228dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl, 5239d34414bSMike Heffner struct sockaddr_inarp *addr, struct rt_msghdr *rtm) 5248dc4b495SJulian Elischer { 5253f844a22SRuslan Ermilov const char *host; 5268dc4b495SJulian Elischer struct hostent *hp; 52797fe20b4SKelly Yancey struct iso88025_sockaddr_dl_data *trld; 528e87a372bSRuslan Ermilov char ifname[IF_NAMESIZE]; 529fda82fc2SJulian Elischer int seg; 5308dc4b495SJulian Elischer 531dea673e9SRodney W. Grimes if (nflag == 0) 5329d34414bSMike Heffner hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 5339d34414bSMike Heffner sizeof addr->sin_addr, AF_INET); 534dea673e9SRodney W. Grimes else 535dea673e9SRodney W. Grimes hp = 0; 536dea673e9SRodney W. Grimes if (hp) 537dea673e9SRodney W. Grimes host = hp->h_name; 538dea673e9SRodney W. Grimes else { 539dea673e9SRodney W. Grimes host = "?"; 540dea673e9SRodney W. Grimes if (h_errno == TRY_AGAIN) 541dea673e9SRodney W. Grimes nflag = 1; 542dea673e9SRodney W. Grimes } 5439d34414bSMike Heffner printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); 54421816de3SDoug Rabson if (sdl->sdl_alen) { 545596e374dSRuslan Ermilov if ((sdl->sdl_type == IFT_ETHER || 5469af9b983SAndrew Thompson sdl->sdl_type == IFT_L2VLAN || 5479af9b983SAndrew Thompson sdl->sdl_type == IFT_BRIDGE) && 54821816de3SDoug Rabson sdl->sdl_alen == ETHER_ADDR_LEN) 549a03b1b7cSRuslan Ermilov printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 55021816de3SDoug Rabson else { 55121816de3SDoug Rabson int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 55221816de3SDoug Rabson 55321816de3SDoug Rabson printf("%s", link_ntoa(sdl) + n); 55421816de3SDoug Rabson } 55521816de3SDoug Rabson } else 556dea673e9SRodney W. Grimes printf("(incomplete)"); 557e87a372bSRuslan Ermilov if (if_indextoname(sdl->sdl_index, ifname) != NULL) 558e87a372bSRuslan Ermilov printf(" on %s", ifname); 559dea673e9SRodney W. Grimes if (rtm->rtm_rmx.rmx_expire == 0) 560dea673e9SRodney W. Grimes printf(" permanent"); 5619d34414bSMike Heffner if (addr->sin_other & SIN_PROXY) 562dea673e9SRodney W. Grimes printf(" published (proxy only)"); 563dea673e9SRodney W. Grimes if (rtm->rtm_addrs & RTA_NETMASK) { 5649d34414bSMike Heffner addr = (struct sockaddr_inarp *) 5650b46c085SLuigi Rizzo (SA_SIZE(sdl) + (char *)sdl); 5669d34414bSMike Heffner if (addr->sin_addr.s_addr == 0xffffffff) 567dea673e9SRodney W. Grimes printf(" published"); 5689d34414bSMike Heffner if (addr->sin_len != 8) 5693a6a5ebeSRuslan Ermilov printf("(weird)"); 570dea673e9SRodney W. Grimes } 571fda82fc2SJulian Elischer switch(sdl->sdl_type) { 572fda82fc2SJulian Elischer case IFT_ETHER: 573fda82fc2SJulian Elischer printf(" [ethernet]"); 574fda82fc2SJulian Elischer break; 575fda82fc2SJulian Elischer case IFT_ISO88025: 576fda82fc2SJulian Elischer printf(" [token-ring]"); 57797fe20b4SKelly Yancey trld = SDL_ISO88025(sdl); 57897fe20b4SKelly Yancey if (trld->trld_rcf != 0) { 57997fe20b4SKelly Yancey printf(" rt=%x", ntohs(trld->trld_rcf)); 58097fe20b4SKelly Yancey for (seg = 0; 58197fe20b4SKelly Yancey seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); 58297fe20b4SKelly Yancey seg++) 5832ab778e1SBill Paul printf(":%x", ntohs(*(trld->trld_route[seg]))); 58497fe20b4SKelly Yancey } 585fda82fc2SJulian Elischer break; 58604427472SMatthew N. Dodd case IFT_FDDI: 58704427472SMatthew N. Dodd printf(" [fddi]"); 58804427472SMatthew N. Dodd break; 58904427472SMatthew N. Dodd case IFT_ATM: 59004427472SMatthew N. Dodd printf(" [atm]"); 59104427472SMatthew N. Dodd break; 59288d5b613SYaroslav Tykhiy case IFT_L2VLAN: 59388d5b613SYaroslav Tykhiy printf(" [vlan]"); 59488d5b613SYaroslav Tykhiy break; 59521816de3SDoug Rabson case IFT_IEEE1394: 59621816de3SDoug Rabson printf(" [firewire]"); 59721816de3SDoug Rabson break; 5989af9b983SAndrew Thompson case IFT_BRIDGE: 5999af9b983SAndrew Thompson printf(" [bridge]"); 6009af9b983SAndrew Thompson break; 601fda82fc2SJulian Elischer default: 602123b2d4aSMurray Stokely break; 603fda82fc2SJulian Elischer } 604fda82fc2SJulian Elischer 605dea673e9SRodney W. Grimes printf("\n"); 606fda82fc2SJulian Elischer 607dea673e9SRodney W. Grimes } 6088dc4b495SJulian Elischer 6098dc4b495SJulian Elischer /* 6108dc4b495SJulian Elischer * Nuke an arp entry 6118dc4b495SJulian Elischer */ 612bdf932aeSLuigi Rizzo static void 6139d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused, 6149d34414bSMike Heffner struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused) 6158dc4b495SJulian Elischer { 6168dc4b495SJulian Elischer char ip[20]; 6178dc4b495SJulian Elischer 6189d34414bSMike Heffner snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 619f3f8b226SRuslan Ermilov (void)delete(ip, 0); 620dea673e9SRodney W. Grimes } 621dea673e9SRodney W. Grimes 622bdf932aeSLuigi Rizzo static void 623a42a667dSPoul-Henning Kamp usage(void) 624dea673e9SRodney W. Grimes { 6258dc4b495SJulian Elischer fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 626b9de94e9SYaroslav Tykhiy "usage: arp [-n] [-i interface] hostname", 627b9de94e9SYaroslav Tykhiy " arp [-n] [-i interface] -a", 6283f844a22SRuslan Ermilov " arp -d hostname [pub]", 629582fa422SBrooks Davis " arp -d [-i interface] -a", 630f6385e48SRuslan Ermilov " arp -s hostname ether_addr [temp] [pub [only]]", 631f6385e48SRuslan Ermilov " arp -S hostname ether_addr [temp] [pub [only]]", 632c72049e4SPhilippe Charnier " arp -f filename"); 633dea673e9SRodney W. Grimes exit(1); 634dea673e9SRodney W. Grimes } 635dea673e9SRodney W. Grimes 636bdf932aeSLuigi Rizzo static struct rt_msghdr * 63768839124SLuigi Rizzo rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl) 638dea673e9SRodney W. Grimes { 639dea673e9SRodney W. Grimes static int seq; 640dea673e9SRodney W. Grimes int rlen; 641bdf932aeSLuigi Rizzo int l; 642bdf932aeSLuigi Rizzo struct sockaddr_in so_mask; 643bdf932aeSLuigi Rizzo static int s = -1; 644bdf932aeSLuigi Rizzo static pid_t pid; 645bdf932aeSLuigi Rizzo 646bdf932aeSLuigi Rizzo static struct { 647bdf932aeSLuigi Rizzo struct rt_msghdr m_rtm; 648bdf932aeSLuigi Rizzo char m_space[512]; 649bdf932aeSLuigi Rizzo } m_rtmsg; 650bdf932aeSLuigi Rizzo 651e2416749SMaxime Henrion struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 652e2416749SMaxime Henrion char *cp = m_rtmsg.m_space; 653bdf932aeSLuigi Rizzo 654bdf932aeSLuigi Rizzo if (s < 0) { /* first time: open socket, get pid */ 655bdf932aeSLuigi Rizzo s = socket(PF_ROUTE, SOCK_RAW, 0); 656bdf932aeSLuigi Rizzo if (s < 0) 657bdf932aeSLuigi Rizzo err(1, "socket"); 658bdf932aeSLuigi Rizzo pid = getpid(); 659bdf932aeSLuigi Rizzo } 660bdf932aeSLuigi Rizzo bzero(&so_mask, sizeof(so_mask)); 661bdf932aeSLuigi Rizzo so_mask.sin_len = 8; 662bdf932aeSLuigi Rizzo so_mask.sin_addr.s_addr = 0xffffffff; 663dea673e9SRodney W. Grimes 664dea673e9SRodney W. Grimes errno = 0; 66568839124SLuigi Rizzo /* 66668839124SLuigi Rizzo * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 66768839124SLuigi Rizzo * appropriately. 66868839124SLuigi Rizzo */ 669dea673e9SRodney W. Grimes if (cmd == RTM_DELETE) 670dea673e9SRodney W. Grimes goto doit; 671dea673e9SRodney W. Grimes bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 672dea673e9SRodney W. Grimes rtm->rtm_flags = flags; 673dea673e9SRodney W. Grimes rtm->rtm_version = RTM_VERSION; 674dea673e9SRodney W. Grimes 675dea673e9SRodney W. Grimes switch (cmd) { 676dea673e9SRodney W. Grimes default: 677c72049e4SPhilippe Charnier errx(1, "internal wrong cmd"); 678dea673e9SRodney W. Grimes case RTM_ADD: 679dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_GATEWAY; 680dea673e9SRodney W. Grimes rtm->rtm_rmx.rmx_expire = expire_time; 681dea673e9SRodney W. Grimes rtm->rtm_inits = RTV_EXPIRE; 682dea673e9SRodney W. Grimes rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 68368839124SLuigi Rizzo dst->sin_other = 0; 684dea673e9SRodney W. Grimes if (doing_proxy) { 6853f844a22SRuslan Ermilov if (proxy_only) 68668839124SLuigi Rizzo dst->sin_other = SIN_PROXY; 687dea673e9SRodney W. Grimes else { 688dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_NETMASK; 689dea673e9SRodney W. Grimes rtm->rtm_flags &= ~RTF_HOST; 690dea673e9SRodney W. Grimes } 691dea673e9SRodney W. Grimes } 692dea673e9SRodney W. Grimes /* FALLTHROUGH */ 693dea673e9SRodney W. Grimes case RTM_GET: 694dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_DST; 695dea673e9SRodney W. Grimes } 696dea673e9SRodney W. Grimes #define NEXTADDR(w, s) \ 697f3f8b226SRuslan Ermilov if ((s) != NULL && rtm->rtm_addrs & (w)) { \ 698f3f8b226SRuslan Ermilov bcopy((s), cp, sizeof(*(s))); cp += SA_SIZE(s);} 699dea673e9SRodney W. Grimes 70068839124SLuigi Rizzo NEXTADDR(RTA_DST, dst); 70168839124SLuigi Rizzo NEXTADDR(RTA_GATEWAY, sdl); 70268839124SLuigi Rizzo NEXTADDR(RTA_NETMASK, &so_mask); 703dea673e9SRodney W. Grimes 704dea673e9SRodney W. Grimes rtm->rtm_msglen = cp - (char *)&m_rtmsg; 705dea673e9SRodney W. Grimes doit: 706dea673e9SRodney W. Grimes l = rtm->rtm_msglen; 707dea673e9SRodney W. Grimes rtm->rtm_seq = ++seq; 708dea673e9SRodney W. Grimes rtm->rtm_type = cmd; 709dea673e9SRodney W. Grimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 710dea673e9SRodney W. Grimes if (errno != ESRCH || cmd != RTM_DELETE) { 711c72049e4SPhilippe Charnier warn("writing to routing socket"); 712f3f8b226SRuslan Ermilov return (NULL); 713dea673e9SRodney W. Grimes } 714dea673e9SRodney W. Grimes } 715dea673e9SRodney W. Grimes do { 716dea673e9SRodney W. Grimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 717dea673e9SRodney W. Grimes } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 718dea673e9SRodney W. Grimes if (l < 0) 719c72049e4SPhilippe Charnier warn("read from routing socket"); 720f3f8b226SRuslan Ermilov return (rtm); 721dea673e9SRodney W. Grimes } 722dea673e9SRodney W. Grimes 723a42a667dSPoul-Henning Kamp /* 724a42a667dSPoul-Henning Kamp * get_ether_addr - get the hardware address of an interface on the 725a42a667dSPoul-Henning Kamp * the same subnet as ipaddr. 726a42a667dSPoul-Henning Kamp */ 727a42a667dSPoul-Henning Kamp #define MAX_IFS 32 728a42a667dSPoul-Henning Kamp 729bdf932aeSLuigi Rizzo static int 730f3f8b226SRuslan Ermilov get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) 731a42a667dSPoul-Henning Kamp { 732a42a667dSPoul-Henning Kamp struct ifreq *ifr, *ifend, *ifp; 733f3f8b226SRuslan Ermilov in_addr_t ina, mask; 734a42a667dSPoul-Henning Kamp struct sockaddr_dl *dla; 735a42a667dSPoul-Henning Kamp struct ifreq ifreq; 736a42a667dSPoul-Henning Kamp struct ifconf ifc; 737a42a667dSPoul-Henning Kamp struct ifreq ifs[MAX_IFS]; 7389d34414bSMike Heffner int sock; 73968839124SLuigi Rizzo int retval = 0; 740a42a667dSPoul-Henning Kamp 7419d34414bSMike Heffner sock = socket(AF_INET, SOCK_DGRAM, 0); 7429d34414bSMike Heffner if (sock < 0) 743c72049e4SPhilippe Charnier err(1, "socket"); 744a42a667dSPoul-Henning Kamp 745a42a667dSPoul-Henning Kamp ifc.ifc_len = sizeof(ifs); 746a42a667dSPoul-Henning Kamp ifc.ifc_req = ifs; 7479cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 748c72049e4SPhilippe Charnier warnx("ioctl(SIOCGIFCONF)"); 74968839124SLuigi Rizzo goto done; 750a42a667dSPoul-Henning Kamp } 751a42a667dSPoul-Henning Kamp 75268839124SLuigi Rizzo #define NEXTIFR(i) \ 75368839124SLuigi Rizzo ((struct ifreq *)((char *)&(i)->ifr_addr \ 75468839124SLuigi Rizzo + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) 75568839124SLuigi Rizzo 756a42a667dSPoul-Henning Kamp /* 757a42a667dSPoul-Henning Kamp * Scan through looking for an interface with an Internet 758a42a667dSPoul-Henning Kamp * address on the same subnet as `ipaddr'. 759a42a667dSPoul-Henning Kamp */ 760a42a667dSPoul-Henning Kamp ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); 76168839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { 76268839124SLuigi Rizzo if (ifr->ifr_addr.sa_family != AF_INET) 76368839124SLuigi Rizzo continue; 764a42a667dSPoul-Henning Kamp strncpy(ifreq.ifr_name, ifr->ifr_name, 765a42a667dSPoul-Henning Kamp sizeof(ifreq.ifr_name)); 76606274ceeSGleb Smirnoff ifreq.ifr_addr = ifr->ifr_addr; 767a42a667dSPoul-Henning Kamp /* 768a42a667dSPoul-Henning Kamp * Check that the interface is up, 769a42a667dSPoul-Henning Kamp * and not point-to-point or loopback. 770a42a667dSPoul-Henning Kamp */ 7719cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) 772a42a667dSPoul-Henning Kamp continue; 773a42a667dSPoul-Henning Kamp if ((ifreq.ifr_flags & 774a42a667dSPoul-Henning Kamp (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 775a42a667dSPoul-Henning Kamp IFF_LOOPBACK|IFF_NOARP)) 776a42a667dSPoul-Henning Kamp != (IFF_UP|IFF_BROADCAST)) 77768839124SLuigi Rizzo continue; 778a42a667dSPoul-Henning Kamp /* 779a42a667dSPoul-Henning Kamp * Get its netmask and check that it's on 780a42a667dSPoul-Henning Kamp * the right subnet. 781a42a667dSPoul-Henning Kamp */ 7829cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) 783a42a667dSPoul-Henning Kamp continue; 784a42a667dSPoul-Henning Kamp mask = ((struct sockaddr_in *) 785a42a667dSPoul-Henning Kamp &ifreq.ifr_addr)->sin_addr.s_addr; 78668839124SLuigi Rizzo ina = ((struct sockaddr_in *) 78768839124SLuigi Rizzo &ifr->ifr_addr)->sin_addr.s_addr; 78868839124SLuigi Rizzo if ((ipaddr & mask) == (ina & mask)) 78968839124SLuigi Rizzo break; /* ok, we got it! */ 790a42a667dSPoul-Henning Kamp } 791a42a667dSPoul-Henning Kamp 79268839124SLuigi Rizzo if (ifr >= ifend) 79368839124SLuigi Rizzo goto done; 794a42a667dSPoul-Henning Kamp 795a42a667dSPoul-Henning Kamp /* 796a42a667dSPoul-Henning Kamp * Now scan through again looking for a link-level address 797a42a667dSPoul-Henning Kamp * for this interface. 798a42a667dSPoul-Henning Kamp */ 799a42a667dSPoul-Henning Kamp ifp = ifr; 80068839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) 80168839124SLuigi Rizzo if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && 80268839124SLuigi Rizzo ifr->ifr_addr.sa_family == AF_LINK) 80368839124SLuigi Rizzo break; 80468839124SLuigi Rizzo if (ifr >= ifend) 80568839124SLuigi Rizzo goto done; 806a42a667dSPoul-Henning Kamp /* 807a42a667dSPoul-Henning Kamp * Found the link-level address - copy it out 808a42a667dSPoul-Henning Kamp */ 809a42a667dSPoul-Henning Kamp dla = (struct sockaddr_dl *) &ifr->ifr_addr; 810a42a667dSPoul-Henning Kamp memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 811a42a667dSPoul-Henning Kamp printf("using interface %s for proxy with address ", 812a42a667dSPoul-Henning Kamp ifp->ifr_name); 813a03b1b7cSRuslan Ermilov printf("%s\n", ether_ntoa(hwaddr)); 81468839124SLuigi Rizzo retval = dla->sdl_alen; 81568839124SLuigi Rizzo done: 81668839124SLuigi Rizzo close(sock); 817f3f8b226SRuslan Ermilov return (retval); 818a42a667dSPoul-Henning Kamp } 819