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, 849711a168SGleb Smirnoff struct sockaddr_in *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 909711a168SGleb Smirnoff static int delete(char *host); 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, 969711a168SGleb Smirnoff struct sockaddr_in *dst, struct sockaddr_dl *sdl); 97f3f8b226SRuslan Ermilov static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); 989711a168SGleb Smirnoff static struct sockaddr_in *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 10492968305SRuslan Ermilov static time_t expire_time; 1059711a168SGleb Smirnoff static int flags, doing_proxy; 10668839124SLuigi Rizzo 1078dc4b495SJulian Elischer /* which function we're supposed to do */ 1088dc4b495SJulian Elischer #define F_GET 1 1098dc4b495SJulian Elischer #define F_SET 2 1108dc4b495SJulian Elischer #define F_FILESET 3 1118dc4b495SJulian Elischer #define F_REPLACE 4 1128dc4b495SJulian Elischer #define F_DELETE 5 1138dc4b495SJulian Elischer 1148dc4b495SJulian Elischer #define SETFUNC(f) { if (func) usage(); func = (f); } 1158dc4b495SJulian Elischer 116a42a667dSPoul-Henning Kamp int 1173f844a22SRuslan Ermilov main(int argc, char *argv[]) 118dea673e9SRodney W. Grimes { 1198dc4b495SJulian Elischer int ch, func = 0; 1208dc4b495SJulian Elischer int rtn = 0; 121bdf932aeSLuigi Rizzo int aflag = 0; /* do it for all entries */ 122dea673e9SRodney W. Grimes 123b9de94e9SYaroslav Tykhiy while ((ch = getopt(argc, argv, "andfsSi:")) != -1) 124eac295c9SRemko Lodder switch(ch) { 125dea673e9SRodney W. Grimes case 'a': 1268dc4b495SJulian Elischer aflag = 1; 1278dc4b495SJulian Elischer break; 128dea673e9SRodney W. Grimes case 'd': 1298dc4b495SJulian Elischer SETFUNC(F_DELETE); 1308dc4b495SJulian Elischer break; 131dea673e9SRodney W. Grimes case 'n': 132dea673e9SRodney W. Grimes nflag = 1; 1338dc4b495SJulian Elischer break; 134a42a667dSPoul-Henning Kamp case 'S': 1358dc4b495SJulian Elischer SETFUNC(F_REPLACE); 1368dc4b495SJulian Elischer break; 137dea673e9SRodney W. Grimes case 's': 1388dc4b495SJulian Elischer SETFUNC(F_SET); 1398dc4b495SJulian Elischer break; 14095319e17SJordan K. Hubbard case 'f' : 1418dc4b495SJulian Elischer SETFUNC(F_FILESET); 1428dc4b495SJulian Elischer break; 143b9de94e9SYaroslav Tykhiy case 'i': 144b9de94e9SYaroslav Tykhiy rifname = optarg; 145b9de94e9SYaroslav Tykhiy break; 146dea673e9SRodney W. Grimes case '?': 147dea673e9SRodney W. Grimes default: 148dea673e9SRodney W. Grimes usage(); 149dea673e9SRodney W. Grimes } 1508dc4b495SJulian Elischer argc -= optind; 1518dc4b495SJulian Elischer argv += optind; 1528dc4b495SJulian Elischer 1538dc4b495SJulian Elischer if (!func) 1548dc4b495SJulian Elischer func = F_GET; 155b9de94e9SYaroslav Tykhiy if (rifname) { 15608369852SBrooks Davis if (func != F_GET && !(func == F_DELETE && aflag)) 157b9de94e9SYaroslav Tykhiy errx(1, "-i not applicable to this operation"); 158b9de94e9SYaroslav Tykhiy if (if_nametoindex(rifname) == 0) { 159b9de94e9SYaroslav Tykhiy if (errno == ENXIO) 160b9de94e9SYaroslav Tykhiy errx(1, "interface %s does not exist", rifname); 161b9de94e9SYaroslav Tykhiy else 162b9de94e9SYaroslav Tykhiy err(1, "if_nametoindex(%s)", rifname); 163b9de94e9SYaroslav Tykhiy } 164b9de94e9SYaroslav Tykhiy } 1658dc4b495SJulian Elischer switch (func) { 1668dc4b495SJulian Elischer case F_GET: 1678dc4b495SJulian Elischer if (aflag) { 1688dc4b495SJulian Elischer if (argc != 0) 169dea673e9SRodney W. Grimes usage(); 1708dc4b495SJulian Elischer search(0, print_entry); 1718dc4b495SJulian Elischer } else { 1728dc4b495SJulian Elischer if (argc != 1) 1738dc4b495SJulian Elischer usage(); 174f3f8b226SRuslan Ermilov rtn = get(argv[0]); 1758dc4b495SJulian Elischer } 1768dc4b495SJulian Elischer break; 1778dc4b495SJulian Elischer case F_SET: 1788dc4b495SJulian Elischer case F_REPLACE: 1793f844a22SRuslan Ermilov if (argc < 2 || argc > 6) 1808dc4b495SJulian Elischer usage(); 1818dc4b495SJulian Elischer if (func == F_REPLACE) 1829711a168SGleb Smirnoff (void)delete(argv[0]); 1838dc4b495SJulian Elischer rtn = set(argc, argv) ? 1 : 0; 1848dc4b495SJulian Elischer break; 1858dc4b495SJulian Elischer case F_DELETE: 1868dc4b495SJulian Elischer if (aflag) { 1878dc4b495SJulian Elischer if (argc != 0) 1888dc4b495SJulian Elischer usage(); 1898dc4b495SJulian Elischer search(0, nuke_entry); 190*876fc15dSGleb Smirnoff } else { 191*876fc15dSGleb Smirnoff if (argc != 1) 192*876fc15dSGleb Smirnoff usage(); 1939711a168SGleb Smirnoff rtn = delete(argv[0]); 194*876fc15dSGleb Smirnoff } 1958dc4b495SJulian Elischer break; 1968dc4b495SJulian Elischer case F_FILESET: 1978dc4b495SJulian Elischer if (argc != 1) 1988dc4b495SJulian Elischer usage(); 1998dc4b495SJulian Elischer rtn = file(argv[0]); 2008dc4b495SJulian Elischer break; 2018dc4b495SJulian Elischer } 2028dc4b495SJulian Elischer 2038dc4b495SJulian Elischer return (rtn); 204dea673e9SRodney W. Grimes } 205dea673e9SRodney W. Grimes 206dea673e9SRodney W. Grimes /* 207dea673e9SRodney W. Grimes * Process a file to set standard arp entries 208dea673e9SRodney W. Grimes */ 209bdf932aeSLuigi Rizzo static int 210a42a667dSPoul-Henning Kamp file(char *name) 211dea673e9SRodney W. Grimes { 212dea673e9SRodney W. Grimes FILE *fp; 213dea673e9SRodney W. Grimes int i, retval; 2143adcd042SRuslan Ermilov char line[100], arg[5][50], *args[5], *p; 215dea673e9SRodney W. Grimes 216c72049e4SPhilippe Charnier if ((fp = fopen(name, "r")) == NULL) 217e2416749SMaxime Henrion err(1, "cannot open %s", name); 218dea673e9SRodney W. Grimes args[0] = &arg[0][0]; 219dea673e9SRodney W. Grimes args[1] = &arg[1][0]; 220dea673e9SRodney W. Grimes args[2] = &arg[2][0]; 221dea673e9SRodney W. Grimes args[3] = &arg[3][0]; 222dea673e9SRodney W. Grimes args[4] = &arg[4][0]; 223dea673e9SRodney W. Grimes retval = 0; 224d0691403SKevin Lo while(fgets(line, sizeof(line), fp) != NULL) { 2253adcd042SRuslan Ermilov if ((p = strchr(line, '#')) != NULL) 2263adcd042SRuslan Ermilov *p = '\0'; 2273adcd042SRuslan Ermilov for (p = line; isblank(*p); p++); 2284bfc3624SRuslan Ermilov if (*p == '\n' || *p == '\0') 22944acbc1aSRuslan Ermilov continue; 2303adcd042SRuslan Ermilov i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 231135adb1eSJordan K. Hubbard arg[2], arg[3], arg[4]); 232dea673e9SRodney W. Grimes if (i < 2) { 233c72049e4SPhilippe Charnier warnx("bad line: %s", line); 234dea673e9SRodney W. Grimes retval = 1; 235dea673e9SRodney W. Grimes continue; 236dea673e9SRodney W. Grimes } 237dea673e9SRodney W. Grimes if (set(i, args)) 238dea673e9SRodney W. Grimes retval = 1; 239dea673e9SRodney W. Grimes } 240dea673e9SRodney W. Grimes fclose(fp); 241dea673e9SRodney W. Grimes return (retval); 242dea673e9SRodney W. Grimes } 243dea673e9SRodney W. Grimes 244dea673e9SRodney W. Grimes /* 2459711a168SGleb Smirnoff * Given a hostname, fills up a (static) struct sockaddr_in with 24668839124SLuigi Rizzo * the address of the host and returns a pointer to the 24768839124SLuigi Rizzo * structure. 248dea673e9SRodney W. Grimes */ 2499711a168SGleb Smirnoff static struct sockaddr_in * 25068839124SLuigi Rizzo getaddr(char *host) 251dea673e9SRodney W. Grimes { 252dea673e9SRodney W. Grimes struct hostent *hp; 2539711a168SGleb Smirnoff static struct sockaddr_in reply; 25468839124SLuigi Rizzo 25568839124SLuigi Rizzo bzero(&reply, sizeof(reply)); 25668839124SLuigi Rizzo reply.sin_len = sizeof(reply); 25768839124SLuigi Rizzo reply.sin_family = AF_INET; 25868839124SLuigi Rizzo reply.sin_addr.s_addr = inet_addr(host); 25968839124SLuigi Rizzo if (reply.sin_addr.s_addr == INADDR_NONE) { 26068839124SLuigi Rizzo if (!(hp = gethostbyname(host))) { 26168839124SLuigi Rizzo warnx("%s: %s", host, hstrerror(h_errno)); 262f3f8b226SRuslan Ermilov return (NULL); 26368839124SLuigi Rizzo } 26468839124SLuigi Rizzo bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, 26568839124SLuigi Rizzo sizeof reply.sin_addr); 26668839124SLuigi Rizzo } 267f3f8b226SRuslan Ermilov return (&reply); 26868839124SLuigi Rizzo } 26968839124SLuigi Rizzo 27068839124SLuigi Rizzo /* 271f3f8b226SRuslan Ermilov * Returns true if the type is a valid one for ARP. 27268839124SLuigi Rizzo */ 27368839124SLuigi Rizzo static int 27468839124SLuigi Rizzo valid_type(int type) 27568839124SLuigi Rizzo { 276f3f8b226SRuslan Ermilov 27768839124SLuigi Rizzo switch (type) { 27868839124SLuigi Rizzo case IFT_ETHER: 27968839124SLuigi Rizzo case IFT_FDDI: 28068839124SLuigi Rizzo case IFT_ISO88023: 28168839124SLuigi Rizzo case IFT_ISO88024: 28268839124SLuigi Rizzo case IFT_ISO88025: 28368839124SLuigi Rizzo case IFT_L2VLAN: 2849af9b983SAndrew Thompson case IFT_BRIDGE: 285f3f8b226SRuslan Ermilov return (1); 286f3f8b226SRuslan Ermilov default: 287f3f8b226SRuslan Ermilov return (0); 28868839124SLuigi Rizzo } 28968839124SLuigi Rizzo } 29068839124SLuigi Rizzo 29168839124SLuigi Rizzo /* 29268839124SLuigi Rizzo * Set an individual arp entry 29368839124SLuigi Rizzo */ 29468839124SLuigi Rizzo static int 29568839124SLuigi Rizzo set(int argc, char **argv) 29668839124SLuigi Rizzo { 2979711a168SGleb Smirnoff struct sockaddr_in *addr; 2989711a168SGleb Smirnoff struct sockaddr_in *dst; /* what are we looking for */ 299e2416749SMaxime Henrion struct sockaddr_dl *sdl; 300bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 301a03b1b7cSRuslan Ermilov struct ether_addr *ea; 302dea673e9SRodney W. Grimes char *host = argv[0], *eaddr = argv[1]; 30368839124SLuigi Rizzo struct sockaddr_dl sdl_m; 304dea673e9SRodney W. Grimes 305dea673e9SRodney W. Grimes argc -= 2; 306dea673e9SRodney W. Grimes argv += 2; 307bdf932aeSLuigi Rizzo 308bdf932aeSLuigi Rizzo bzero(&sdl_m, sizeof(sdl_m)); 309bdf932aeSLuigi Rizzo sdl_m.sdl_len = sizeof(sdl_m); 310bdf932aeSLuigi Rizzo sdl_m.sdl_family = AF_LINK; 311bdf932aeSLuigi Rizzo 31268839124SLuigi Rizzo dst = getaddr(host); 31368839124SLuigi Rizzo if (dst == NULL) 314dea673e9SRodney W. Grimes return (1); 3159711a168SGleb Smirnoff doing_proxy = flags = expire_time = 0; 316dea673e9SRodney W. Grimes while (argc-- > 0) { 317dea673e9SRodney W. Grimes if (strncmp(argv[0], "temp", 4) == 0) { 3185d067f6cSGleb Smirnoff struct timespec tp; 31984d8f5b8SGleb Smirnoff int max_age; 32084d8f5b8SGleb Smirnoff size_t len = sizeof(max_age); 32184d8f5b8SGleb Smirnoff 3225d067f6cSGleb Smirnoff clock_gettime(CLOCK_MONOTONIC, &tp); 32384d8f5b8SGleb Smirnoff if (sysctlbyname("net.link.ether.inet.max_age", 32484d8f5b8SGleb Smirnoff &max_age, &len, NULL, 0) != 0) 32584d8f5b8SGleb Smirnoff err(1, "sysctlbyname"); 32684d8f5b8SGleb Smirnoff expire_time = tp.tv_sec + max_age; 327e653f1f0SSam Leffler } else if (strncmp(argv[0], "pub", 3) == 0) { 328dea673e9SRodney W. Grimes flags |= RTF_ANNOUNCE; 3293f844a22SRuslan Ermilov doing_proxy = 1; 3303f844a22SRuslan Ermilov if (argc && strncmp(argv[1], "only", 3) == 0) { 3319711a168SGleb Smirnoff /* 3329711a168SGleb Smirnoff * Compatibility: in pre FreeBSD 8 times 3339711a168SGleb Smirnoff * the "only" keyword used to mean that 3349711a168SGleb Smirnoff * an ARP entry should be announced, but 3359711a168SGleb Smirnoff * not installed into routing table. 3369711a168SGleb Smirnoff */ 3373f844a22SRuslan Ermilov argc--; argv++; 3383f844a22SRuslan Ermilov } 339e653f1f0SSam Leffler } else if (strncmp(argv[0], "blackhole", 9) == 0) { 3402293dac2STom Rhodes if (flags & RTF_REJECT) { 3412293dac2STom Rhodes printf("Choose one of blackhole or reject, not both.\n"); 3422293dac2STom Rhodes } 343e653f1f0SSam Leffler flags |= RTF_BLACKHOLE; 344e653f1f0SSam Leffler } else if (strncmp(argv[0], "reject", 6) == 0) { 3452293dac2STom Rhodes if (flags & RTF_BLACKHOLE) { 3462293dac2STom Rhodes printf("Choose one of blackhole or reject, not both.\n"); 3472293dac2STom Rhodes } 348e653f1f0SSam Leffler flags |= RTF_REJECT; 349dea673e9SRodney W. Grimes } else if (strncmp(argv[0], "trail", 5) == 0) { 35068839124SLuigi Rizzo /* XXX deprecated and undocumented feature */ 351dea673e9SRodney W. Grimes printf("%s: Sending trailers is no longer supported\n", 352dea673e9SRodney W. Grimes host); 353dea673e9SRodney W. Grimes } 354dea673e9SRodney W. Grimes argv++; 355dea673e9SRodney W. Grimes } 356a03b1b7cSRuslan Ermilov ea = (struct ether_addr *)LLADDR(&sdl_m); 357a42a667dSPoul-Henning Kamp if (doing_proxy && !strcmp(eaddr, "auto")) { 35868839124SLuigi Rizzo if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { 359eec827b0SRuslan Ermilov printf("no interface found for %s\n", 36068839124SLuigi Rizzo inet_ntoa(dst->sin_addr)); 361a42a667dSPoul-Henning Kamp return (1); 362a42a667dSPoul-Henning Kamp } 363a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 364a42a667dSPoul-Henning Kamp } else { 36568839124SLuigi Rizzo struct ether_addr *ea1 = ether_aton(eaddr); 36668839124SLuigi Rizzo 367a47c388cSGleb Smirnoff if (ea1 == NULL) { 36868839124SLuigi Rizzo warnx("invalid Ethernet address '%s'", eaddr); 369a47c388cSGleb Smirnoff return (1); 370a47c388cSGleb Smirnoff } else { 37168839124SLuigi Rizzo *ea = *ea1; 372a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 373a42a667dSPoul-Henning Kamp } 37468839124SLuigi Rizzo } 375c7ab6602SQing Li 376c7ab6602SQing Li /* 377c7ab6602SQing Li * In the case a proxy-arp entry is being added for 378c7ab6602SQing Li * a remote end point, the RTF_ANNOUNCE flag in the 379c7ab6602SQing Li * RTM_GET command is an indication to the kernel 380c7ab6602SQing Li * routing code that the interface associated with 381c7ab6602SQing Li * the prefix route covering the local end of the 382c7ab6602SQing Li * PPP link should be returned, on which ARP applies. 383c7ab6602SQing Li */ 38468839124SLuigi Rizzo rtm = rtmsg(RTM_GET, dst, &sdl_m); 385bdf932aeSLuigi Rizzo if (rtm == NULL) { 386c72049e4SPhilippe Charnier warn("%s", host); 387dea673e9SRodney W. Grimes return (1); 388dea673e9SRodney W. Grimes } 3899711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1); 3900b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 39168839124SLuigi Rizzo 392c7ab6602SQing Li if ((sdl->sdl_family != AF_LINK) || 393c7ab6602SQing Li (rtm->rtm_flags & RTF_GATEWAY) || 394c7ab6602SQing Li !valid_type(sdl->sdl_type)) { 395dea673e9SRodney W. Grimes printf("cannot intuit interface index and type for %s\n", host); 396dea673e9SRodney W. Grimes return (1); 397dea673e9SRodney W. Grimes } 398dea673e9SRodney W. Grimes sdl_m.sdl_type = sdl->sdl_type; 399dea673e9SRodney W. Grimes sdl_m.sdl_index = sdl->sdl_index; 400cf779589SGleb Smirnoff return (rtmsg(RTM_ADD, dst, &sdl_m) == NULL); 401dea673e9SRodney W. Grimes } 402dea673e9SRodney W. Grimes 403dea673e9SRodney W. Grimes /* 404dea673e9SRodney W. Grimes * Display an individual arp entry 405dea673e9SRodney W. Grimes */ 406f3f8b226SRuslan Ermilov static int 407a42a667dSPoul-Henning Kamp get(char *host) 408dea673e9SRodney W. Grimes { 4099711a168SGleb Smirnoff struct sockaddr_in *addr; 410dea673e9SRodney W. Grimes 41168839124SLuigi Rizzo addr = getaddr(host); 41268839124SLuigi Rizzo if (addr == NULL) 413f3f8b226SRuslan Ermilov return (1); 414bdf932aeSLuigi Rizzo if (0 == search(addr->sin_addr.s_addr, print_entry)) { 415b9de94e9SYaroslav Tykhiy printf("%s (%s) -- no entry", 4169d34414bSMike Heffner host, inet_ntoa(addr->sin_addr)); 417b9de94e9SYaroslav Tykhiy if (rifname) 418b9de94e9SYaroslav Tykhiy printf(" on %s", rifname); 419b9de94e9SYaroslav Tykhiy printf("\n"); 420f3f8b226SRuslan Ermilov return (1); 421dea673e9SRodney W. Grimes } 422f3f8b226SRuslan Ermilov return (0); 423dea673e9SRodney W. Grimes } 424dea673e9SRodney W. Grimes 425dea673e9SRodney W. Grimes /* 426dea673e9SRodney W. Grimes * Delete an arp entry 427dea673e9SRodney W. Grimes */ 428bdf932aeSLuigi Rizzo static int 4299711a168SGleb Smirnoff delete(char *host) 430dea673e9SRodney W. Grimes { 4319711a168SGleb Smirnoff struct sockaddr_in *addr, *dst; 432bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 433dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 4346e6b3f7cSQing Li struct sockaddr_dl sdl_m; 435dea673e9SRodney W. Grimes 43668839124SLuigi Rizzo dst = getaddr(host); 43768839124SLuigi Rizzo if (dst == NULL) 438f3f8b226SRuslan Ermilov return (1); 439c7ab6602SQing Li 440c7ab6602SQing Li /* 441c7ab6602SQing Li * Perform a regular entry delete first. 442c7ab6602SQing Li */ 443c7ab6602SQing Li flags &= ~RTF_ANNOUNCE; 4446e6b3f7cSQing Li 4456e6b3f7cSQing Li /* 4466e6b3f7cSQing Li * setup the data structure to notify the kernel 4476e6b3f7cSQing Li * it is the ARP entry the RTM_GET is interested 4486e6b3f7cSQing Li * in 4496e6b3f7cSQing Li */ 4506e6b3f7cSQing Li bzero(&sdl_m, sizeof(sdl_m)); 4516e6b3f7cSQing Li sdl_m.sdl_len = sizeof(sdl_m); 4526e6b3f7cSQing Li sdl_m.sdl_family = AF_LINK; 4536e6b3f7cSQing Li 45468839124SLuigi Rizzo for (;;) { /* try twice */ 4556e6b3f7cSQing Li rtm = rtmsg(RTM_GET, dst, &sdl_m); 456bdf932aeSLuigi Rizzo if (rtm == NULL) { 457c72049e4SPhilippe Charnier warn("%s", host); 458dea673e9SRodney W. Grimes return (1); 459dea673e9SRodney W. Grimes } 4609711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1); 4610b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 4626e6b3f7cSQing Li 4636e6b3f7cSQing Li /* 4646e6b3f7cSQing Li * With the new L2/L3 restructure, the route 4656e6b3f7cSQing Li * returned is a prefix route. The important 4666e6b3f7cSQing Li * piece of information from the previous 4676e6b3f7cSQing Li * RTM_GET is the interface index. In the 4686e6b3f7cSQing Li * case of ECMP, the kernel will traverse 4696e6b3f7cSQing Li * the route group for the given entry. 4706e6b3f7cSQing Li */ 4716e6b3f7cSQing Li if (sdl->sdl_family == AF_LINK && 47268839124SLuigi Rizzo !(rtm->rtm_flags & RTF_GATEWAY) && 4736e6b3f7cSQing Li valid_type(sdl->sdl_type) ) { 4746e6b3f7cSQing Li addr->sin_addr.s_addr = dst->sin_addr.s_addr; 4756e6b3f7cSQing Li break; 4766e6b3f7cSQing Li } 4776e6b3f7cSQing Li 478c7ab6602SQing Li /* 479c7ab6602SQing Li * Regualar entry delete failed, now check if there 480c7ab6602SQing Li * is a proxy-arp entry to remove. 481c7ab6602SQing Li */ 482c7ab6602SQing Li if (flags & RTF_ANNOUNCE) { 48368839124SLuigi Rizzo fprintf(stderr, "delete: cannot locate %s\n",host); 484dea673e9SRodney W. Grimes return (1); 485dea673e9SRodney W. Grimes } 486c7ab6602SQing Li 487c7ab6602SQing Li flags |= RTF_ANNOUNCE; 48868839124SLuigi Rizzo } 4898eca593cSQing Li rtm->rtm_flags |= RTF_LLDATA; 49068839124SLuigi Rizzo if (rtmsg(RTM_DELETE, dst, NULL) != NULL) { 4919d34414bSMike Heffner printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr)); 492a42a667dSPoul-Henning Kamp return (0); 493a42a667dSPoul-Henning Kamp } 494a42a667dSPoul-Henning Kamp return (1); 495dea673e9SRodney W. Grimes } 496dea673e9SRodney W. Grimes 497c7ab6602SQing Li 498dea673e9SRodney W. Grimes /* 4998dc4b495SJulian Elischer * Search the arp table and do some action on matching entries 500dea673e9SRodney W. Grimes */ 501bdf932aeSLuigi Rizzo static int 502bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action) 503dea673e9SRodney W. Grimes { 504dea673e9SRodney W. Grimes int mib[6]; 505dea673e9SRodney W. Grimes size_t needed; 506c34169d4SJohn Baldwin char *lim, *buf, *next; 507dea673e9SRodney W. Grimes struct rt_msghdr *rtm; 5089711a168SGleb Smirnoff struct sockaddr_in *sin2; 509dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 510b9de94e9SYaroslav Tykhiy char ifname[IF_NAMESIZE]; 51166658902SMaxim Konovalov int st, found_entry = 0; 512dea673e9SRodney W. Grimes 513dea673e9SRodney W. Grimes mib[0] = CTL_NET; 514dea673e9SRodney W. Grimes mib[1] = PF_ROUTE; 515dea673e9SRodney W. Grimes mib[2] = 0; 516dea673e9SRodney W. Grimes mib[3] = AF_INET; 517dea673e9SRodney W. Grimes mib[4] = NET_RT_FLAGS; 5186e6b3f7cSQing Li #ifdef RTF_LLINFO 519dea673e9SRodney W. Grimes mib[5] = RTF_LLINFO; 5206e6b3f7cSQing Li #else 5216e6b3f7cSQing Li mib[5] = 0; 5226e6b3f7cSQing Li #endif 523dea673e9SRodney W. Grimes if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 524e2416749SMaxime Henrion err(1, "route-sysctl-estimate"); 52568839124SLuigi Rizzo if (needed == 0) /* empty table */ 526bdf932aeSLuigi Rizzo return 0; 52766658902SMaxim Konovalov buf = NULL; 52819beed5eSMaxim Konovalov for (;;) { 529c34169d4SJohn Baldwin buf = reallocf(buf, needed); 530c34169d4SJohn Baldwin if (buf == NULL) 53166658902SMaxim Konovalov errx(1, "could not reallocate memory"); 53266658902SMaxim Konovalov st = sysctl(mib, 6, buf, &needed, NULL, 0); 53319beed5eSMaxim Konovalov if (st == 0 || errno != ENOMEM) 53419beed5eSMaxim Konovalov break; 53519beed5eSMaxim Konovalov needed += needed / 8; 53619beed5eSMaxim Konovalov } 53766658902SMaxim Konovalov if (st == -1) 538e2416749SMaxime Henrion err(1, "actual retrieval of routing table"); 539dea673e9SRodney W. Grimes lim = buf + needed; 540dea673e9SRodney W. Grimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 541dea673e9SRodney W. Grimes rtm = (struct rt_msghdr *)next; 5429711a168SGleb Smirnoff sin2 = (struct sockaddr_in *)(rtm + 1); 5431a5ff928SStefan Farfeleder sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); 544b9de94e9SYaroslav Tykhiy if (rifname && if_indextoname(sdl->sdl_index, ifname) && 545b9de94e9SYaroslav Tykhiy strcmp(ifname, rifname)) 546b9de94e9SYaroslav Tykhiy continue; 547dea673e9SRodney W. Grimes if (addr) { 5489d34414bSMike Heffner if (addr != sin2->sin_addr.s_addr) 549dea673e9SRodney W. Grimes continue; 550dea673e9SRodney W. Grimes found_entry = 1; 551dea673e9SRodney W. Grimes } 5529d34414bSMike Heffner (*action)(sdl, sin2, rtm); 5538dc4b495SJulian Elischer } 554ae14be20SYaroslav Tykhiy free(buf); 555f3f8b226SRuslan Ermilov return (found_entry); 5568dc4b495SJulian Elischer } 5578dc4b495SJulian Elischer 5588dc4b495SJulian Elischer /* 5598dc4b495SJulian Elischer * Display an arp entry 5608dc4b495SJulian Elischer */ 561e78c7a0aSMax Laier static char lifname[IF_NAMESIZE]; 562e78c7a0aSMax Laier static int64_t lifindex = -1; 563e78c7a0aSMax Laier 564bdf932aeSLuigi Rizzo static void 5658dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl, 5669711a168SGleb Smirnoff struct sockaddr_in *addr, struct rt_msghdr *rtm) 5678dc4b495SJulian Elischer { 5683f844a22SRuslan Ermilov const char *host; 5698dc4b495SJulian Elischer struct hostent *hp; 57097fe20b4SKelly Yancey struct iso88025_sockaddr_dl_data *trld; 571fda82fc2SJulian Elischer int seg; 5728dc4b495SJulian Elischer 573dea673e9SRodney W. Grimes if (nflag == 0) 5749d34414bSMike Heffner hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 5759d34414bSMike Heffner sizeof addr->sin_addr, AF_INET); 576dea673e9SRodney W. Grimes else 577dea673e9SRodney W. Grimes hp = 0; 578dea673e9SRodney W. Grimes if (hp) 579dea673e9SRodney W. Grimes host = hp->h_name; 580dea673e9SRodney W. Grimes else { 581dea673e9SRodney W. Grimes host = "?"; 582dea673e9SRodney W. Grimes if (h_errno == TRY_AGAIN) 583dea673e9SRodney W. Grimes nflag = 1; 584dea673e9SRodney W. Grimes } 5859d34414bSMike Heffner printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); 58621816de3SDoug Rabson if (sdl->sdl_alen) { 587596e374dSRuslan Ermilov if ((sdl->sdl_type == IFT_ETHER || 5889af9b983SAndrew Thompson sdl->sdl_type == IFT_L2VLAN || 5899af9b983SAndrew Thompson sdl->sdl_type == IFT_BRIDGE) && 59021816de3SDoug Rabson sdl->sdl_alen == ETHER_ADDR_LEN) 591a03b1b7cSRuslan Ermilov printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 59221816de3SDoug Rabson else { 59321816de3SDoug Rabson int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 59421816de3SDoug Rabson 59521816de3SDoug Rabson printf("%s", link_ntoa(sdl) + n); 59621816de3SDoug Rabson } 59721816de3SDoug Rabson } else 598dea673e9SRodney W. Grimes printf("(incomplete)"); 599e78c7a0aSMax Laier if (sdl->sdl_index != lifindex && 600e78c7a0aSMax Laier if_indextoname(sdl->sdl_index, lifname) != NULL) { 601e78c7a0aSMax Laier lifindex = sdl->sdl_index; 602e78c7a0aSMax Laier printf(" on %s", lifname); 603e78c7a0aSMax Laier } else if (sdl->sdl_index == lifindex) 604e78c7a0aSMax Laier printf(" on %s", lifname); 605dea673e9SRodney W. Grimes if (rtm->rtm_rmx.rmx_expire == 0) 606dea673e9SRodney W. Grimes printf(" permanent"); 60792968305SRuslan Ermilov else { 608a98c06f1SGleb Smirnoff static struct timespec tp; 609a98c06f1SGleb Smirnoff if (tp.tv_sec == 0) 610a98c06f1SGleb Smirnoff clock_gettime(CLOCK_MONOTONIC, &tp); 611a98c06f1SGleb Smirnoff if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0) 61292968305SRuslan Ermilov printf(" expires in %d seconds", (int)expire_time); 61392968305SRuslan Ermilov else 61492968305SRuslan Ermilov printf(" expired"); 61592968305SRuslan Ermilov } 6166e6b3f7cSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE) 617dea673e9SRodney W. Grimes printf(" published"); 618fda82fc2SJulian Elischer switch(sdl->sdl_type) { 619fda82fc2SJulian Elischer case IFT_ETHER: 620fda82fc2SJulian Elischer printf(" [ethernet]"); 621fda82fc2SJulian Elischer break; 622fda82fc2SJulian Elischer case IFT_ISO88025: 623fda82fc2SJulian Elischer printf(" [token-ring]"); 62497fe20b4SKelly Yancey trld = SDL_ISO88025(sdl); 62597fe20b4SKelly Yancey if (trld->trld_rcf != 0) { 62697fe20b4SKelly Yancey printf(" rt=%x", ntohs(trld->trld_rcf)); 62797fe20b4SKelly Yancey for (seg = 0; 62897fe20b4SKelly Yancey seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); 62997fe20b4SKelly Yancey seg++) 6302ab778e1SBill Paul printf(":%x", ntohs(*(trld->trld_route[seg]))); 63197fe20b4SKelly Yancey } 632fda82fc2SJulian Elischer break; 63304427472SMatthew N. Dodd case IFT_FDDI: 63404427472SMatthew N. Dodd printf(" [fddi]"); 63504427472SMatthew N. Dodd break; 63604427472SMatthew N. Dodd case IFT_ATM: 63704427472SMatthew N. Dodd printf(" [atm]"); 63804427472SMatthew N. Dodd break; 63988d5b613SYaroslav Tykhiy case IFT_L2VLAN: 64088d5b613SYaroslav Tykhiy printf(" [vlan]"); 64188d5b613SYaroslav Tykhiy break; 64221816de3SDoug Rabson case IFT_IEEE1394: 64321816de3SDoug Rabson printf(" [firewire]"); 64421816de3SDoug Rabson break; 6459af9b983SAndrew Thompson case IFT_BRIDGE: 6469af9b983SAndrew Thompson printf(" [bridge]"); 6479af9b983SAndrew Thompson break; 648fda82fc2SJulian Elischer default: 649123b2d4aSMurray Stokely break; 650fda82fc2SJulian Elischer } 651fda82fc2SJulian Elischer 652dea673e9SRodney W. Grimes printf("\n"); 653fda82fc2SJulian Elischer 654dea673e9SRodney W. Grimes } 6558dc4b495SJulian Elischer 6568dc4b495SJulian Elischer /* 6578dc4b495SJulian Elischer * Nuke an arp entry 6588dc4b495SJulian Elischer */ 659bdf932aeSLuigi Rizzo static void 6609d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused, 6619711a168SGleb Smirnoff struct sockaddr_in *addr, struct rt_msghdr *rtm __unused) 6628dc4b495SJulian Elischer { 6638dc4b495SJulian Elischer char ip[20]; 6648dc4b495SJulian Elischer 6659d34414bSMike Heffner snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 6669711a168SGleb Smirnoff delete(ip); 667dea673e9SRodney W. Grimes } 668dea673e9SRodney W. Grimes 669bdf932aeSLuigi Rizzo static void 670a42a667dSPoul-Henning Kamp usage(void) 671dea673e9SRodney W. Grimes { 6728dc4b495SJulian Elischer fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 673b9de94e9SYaroslav Tykhiy "usage: arp [-n] [-i interface] hostname", 674b9de94e9SYaroslav Tykhiy " arp [-n] [-i interface] -a", 6753f844a22SRuslan Ermilov " arp -d hostname [pub]", 676582fa422SBrooks Davis " arp -d [-i interface] -a", 6772293dac2STom Rhodes " arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 6782293dac2STom Rhodes " arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 679c72049e4SPhilippe Charnier " arp -f filename"); 680dea673e9SRodney W. Grimes exit(1); 681dea673e9SRodney W. Grimes } 682dea673e9SRodney W. Grimes 683bdf932aeSLuigi Rizzo static struct rt_msghdr * 6849711a168SGleb Smirnoff rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl) 685dea673e9SRodney W. Grimes { 686dea673e9SRodney W. Grimes static int seq; 687dea673e9SRodney W. Grimes int rlen; 688bdf932aeSLuigi Rizzo int l; 6890c80179cSSam Leffler struct sockaddr_in so_mask, *som = &so_mask; 690bdf932aeSLuigi Rizzo static int s = -1; 691bdf932aeSLuigi Rizzo static pid_t pid; 692bdf932aeSLuigi Rizzo 693bdf932aeSLuigi Rizzo static struct { 694bdf932aeSLuigi Rizzo struct rt_msghdr m_rtm; 695bdf932aeSLuigi Rizzo char m_space[512]; 696bdf932aeSLuigi Rizzo } m_rtmsg; 697bdf932aeSLuigi Rizzo 698e2416749SMaxime Henrion struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 699e2416749SMaxime Henrion char *cp = m_rtmsg.m_space; 700bdf932aeSLuigi Rizzo 701bdf932aeSLuigi Rizzo if (s < 0) { /* first time: open socket, get pid */ 702bdf932aeSLuigi Rizzo s = socket(PF_ROUTE, SOCK_RAW, 0); 703bdf932aeSLuigi Rizzo if (s < 0) 704bdf932aeSLuigi Rizzo err(1, "socket"); 705bdf932aeSLuigi Rizzo pid = getpid(); 706bdf932aeSLuigi Rizzo } 707bdf932aeSLuigi Rizzo bzero(&so_mask, sizeof(so_mask)); 708bdf932aeSLuigi Rizzo so_mask.sin_len = 8; 709bdf932aeSLuigi Rizzo so_mask.sin_addr.s_addr = 0xffffffff; 710dea673e9SRodney W. Grimes 711dea673e9SRodney W. Grimes errno = 0; 71268839124SLuigi Rizzo /* 71368839124SLuigi Rizzo * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 71468839124SLuigi Rizzo * appropriately. 71568839124SLuigi Rizzo */ 716dea673e9SRodney W. Grimes if (cmd == RTM_DELETE) 717dea673e9SRodney W. Grimes goto doit; 718dea673e9SRodney W. Grimes bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 719dea673e9SRodney W. Grimes rtm->rtm_flags = flags; 720dea673e9SRodney W. Grimes rtm->rtm_version = RTM_VERSION; 721dea673e9SRodney W. Grimes 722dea673e9SRodney W. Grimes switch (cmd) { 723dea673e9SRodney W. Grimes default: 724c72049e4SPhilippe Charnier errx(1, "internal wrong cmd"); 725dea673e9SRodney W. Grimes case RTM_ADD: 726dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_GATEWAY; 727dea673e9SRodney W. Grimes rtm->rtm_rmx.rmx_expire = expire_time; 728dea673e9SRodney W. Grimes rtm->rtm_inits = RTV_EXPIRE; 7298eca593cSQing Li rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 730dea673e9SRodney W. Grimes if (doing_proxy) { 731dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_NETMASK; 732dea673e9SRodney W. Grimes rtm->rtm_flags &= ~RTF_HOST; 733dea673e9SRodney W. Grimes } 734dea673e9SRodney W. Grimes /* FALLTHROUGH */ 735dea673e9SRodney W. Grimes case RTM_GET: 736dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_DST; 737dea673e9SRodney W. Grimes } 738dea673e9SRodney W. Grimes #define NEXTADDR(w, s) \ 739be5d11dcSDag-Erling Smørgrav do { \ 740f3f8b226SRuslan Ermilov if ((s) != NULL && rtm->rtm_addrs & (w)) { \ 741be5d11dcSDag-Erling Smørgrav bcopy((s), cp, sizeof(*(s))); \ 742be5d11dcSDag-Erling Smørgrav cp += SA_SIZE(s); \ 743be5d11dcSDag-Erling Smørgrav } \ 744be5d11dcSDag-Erling Smørgrav } while (0) 745dea673e9SRodney W. Grimes 74668839124SLuigi Rizzo NEXTADDR(RTA_DST, dst); 74768839124SLuigi Rizzo NEXTADDR(RTA_GATEWAY, sdl); 7480c80179cSSam Leffler NEXTADDR(RTA_NETMASK, som); 749dea673e9SRodney W. Grimes 750dea673e9SRodney W. Grimes rtm->rtm_msglen = cp - (char *)&m_rtmsg; 751dea673e9SRodney W. Grimes doit: 752dea673e9SRodney W. Grimes l = rtm->rtm_msglen; 753dea673e9SRodney W. Grimes rtm->rtm_seq = ++seq; 754dea673e9SRodney W. Grimes rtm->rtm_type = cmd; 755dea673e9SRodney W. Grimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 756dea673e9SRodney W. Grimes if (errno != ESRCH || cmd != RTM_DELETE) { 757c72049e4SPhilippe Charnier warn("writing to routing socket"); 758f3f8b226SRuslan Ermilov return (NULL); 759dea673e9SRodney W. Grimes } 760dea673e9SRodney W. Grimes } 761dea673e9SRodney W. Grimes do { 762dea673e9SRodney W. Grimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 763dea673e9SRodney W. Grimes } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 764dea673e9SRodney W. Grimes if (l < 0) 765c72049e4SPhilippe Charnier warn("read from routing socket"); 766f3f8b226SRuslan Ermilov return (rtm); 767dea673e9SRodney W. Grimes } 768dea673e9SRodney W. Grimes 769a42a667dSPoul-Henning Kamp /* 770a42a667dSPoul-Henning Kamp * get_ether_addr - get the hardware address of an interface on the 771a42a667dSPoul-Henning Kamp * the same subnet as ipaddr. 772a42a667dSPoul-Henning Kamp */ 773a42a667dSPoul-Henning Kamp #define MAX_IFS 32 774a42a667dSPoul-Henning Kamp 775bdf932aeSLuigi Rizzo static int 776f3f8b226SRuslan Ermilov get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) 777a42a667dSPoul-Henning Kamp { 778a42a667dSPoul-Henning Kamp struct ifreq *ifr, *ifend, *ifp; 779f3f8b226SRuslan Ermilov in_addr_t ina, mask; 780a42a667dSPoul-Henning Kamp struct sockaddr_dl *dla; 781a42a667dSPoul-Henning Kamp struct ifreq ifreq; 782a42a667dSPoul-Henning Kamp struct ifconf ifc; 783a42a667dSPoul-Henning Kamp struct ifreq ifs[MAX_IFS]; 7849d34414bSMike Heffner int sock; 78568839124SLuigi Rizzo int retval = 0; 786a42a667dSPoul-Henning Kamp 7879d34414bSMike Heffner sock = socket(AF_INET, SOCK_DGRAM, 0); 7889d34414bSMike Heffner if (sock < 0) 789c72049e4SPhilippe Charnier err(1, "socket"); 790a42a667dSPoul-Henning Kamp 791a42a667dSPoul-Henning Kamp ifc.ifc_len = sizeof(ifs); 792a42a667dSPoul-Henning Kamp ifc.ifc_req = ifs; 7939cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 794c72049e4SPhilippe Charnier warnx("ioctl(SIOCGIFCONF)"); 79568839124SLuigi Rizzo goto done; 796a42a667dSPoul-Henning Kamp } 797a42a667dSPoul-Henning Kamp 79868839124SLuigi Rizzo #define NEXTIFR(i) \ 79968839124SLuigi Rizzo ((struct ifreq *)((char *)&(i)->ifr_addr \ 80068839124SLuigi Rizzo + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) 80168839124SLuigi Rizzo 802a42a667dSPoul-Henning Kamp /* 803a42a667dSPoul-Henning Kamp * Scan through looking for an interface with an Internet 804a42a667dSPoul-Henning Kamp * address on the same subnet as `ipaddr'. 805a42a667dSPoul-Henning Kamp */ 806a42a667dSPoul-Henning Kamp ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); 80768839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { 80868839124SLuigi Rizzo if (ifr->ifr_addr.sa_family != AF_INET) 80968839124SLuigi Rizzo continue; 810a42a667dSPoul-Henning Kamp strncpy(ifreq.ifr_name, ifr->ifr_name, 811a42a667dSPoul-Henning Kamp sizeof(ifreq.ifr_name)); 81206274ceeSGleb Smirnoff ifreq.ifr_addr = ifr->ifr_addr; 813a42a667dSPoul-Henning Kamp /* 814a42a667dSPoul-Henning Kamp * Check that the interface is up, 815a42a667dSPoul-Henning Kamp * and not point-to-point or loopback. 816a42a667dSPoul-Henning Kamp */ 8179cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) 818a42a667dSPoul-Henning Kamp continue; 819a42a667dSPoul-Henning Kamp if ((ifreq.ifr_flags & 820a42a667dSPoul-Henning Kamp (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 821a42a667dSPoul-Henning Kamp IFF_LOOPBACK|IFF_NOARP)) 822a42a667dSPoul-Henning Kamp != (IFF_UP|IFF_BROADCAST)) 82368839124SLuigi Rizzo continue; 824a42a667dSPoul-Henning Kamp /* 825a42a667dSPoul-Henning Kamp * Get its netmask and check that it's on 826a42a667dSPoul-Henning Kamp * the right subnet. 827a42a667dSPoul-Henning Kamp */ 8289cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) 829a42a667dSPoul-Henning Kamp continue; 830a42a667dSPoul-Henning Kamp mask = ((struct sockaddr_in *) 831a42a667dSPoul-Henning Kamp &ifreq.ifr_addr)->sin_addr.s_addr; 83268839124SLuigi Rizzo ina = ((struct sockaddr_in *) 83368839124SLuigi Rizzo &ifr->ifr_addr)->sin_addr.s_addr; 83468839124SLuigi Rizzo if ((ipaddr & mask) == (ina & mask)) 83568839124SLuigi Rizzo break; /* ok, we got it! */ 836a42a667dSPoul-Henning Kamp } 837a42a667dSPoul-Henning Kamp 83868839124SLuigi Rizzo if (ifr >= ifend) 83968839124SLuigi Rizzo goto done; 840a42a667dSPoul-Henning Kamp 841a42a667dSPoul-Henning Kamp /* 842a42a667dSPoul-Henning Kamp * Now scan through again looking for a link-level address 843a42a667dSPoul-Henning Kamp * for this interface. 844a42a667dSPoul-Henning Kamp */ 845a42a667dSPoul-Henning Kamp ifp = ifr; 84668839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) 84768839124SLuigi Rizzo if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && 84868839124SLuigi Rizzo ifr->ifr_addr.sa_family == AF_LINK) 84968839124SLuigi Rizzo break; 85068839124SLuigi Rizzo if (ifr >= ifend) 85168839124SLuigi Rizzo goto done; 852a42a667dSPoul-Henning Kamp /* 853a42a667dSPoul-Henning Kamp * Found the link-level address - copy it out 854a42a667dSPoul-Henning Kamp */ 855a42a667dSPoul-Henning Kamp dla = (struct sockaddr_dl *) &ifr->ifr_addr; 856a42a667dSPoul-Henning Kamp memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 857a42a667dSPoul-Henning Kamp printf("using interface %s for proxy with address ", 858a42a667dSPoul-Henning Kamp ifp->ifr_name); 859a03b1b7cSRuslan Ermilov printf("%s\n", ether_ntoa(hwaddr)); 86068839124SLuigi Rizzo retval = dla->sdl_alen; 86168839124SLuigi Rizzo done: 86268839124SLuigi Rizzo close(sock); 863f3f8b226SRuslan Ermilov return (retval); 864a42a667dSPoul-Henning Kamp } 865