18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 4dea673e9SRodney W. Grimes * Copyright (c) 1984, 1993 5dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 6dea673e9SRodney W. Grimes * 7dea673e9SRodney W. Grimes * This code is derived from software contributed to Berkeley by 8dea673e9SRodney W. Grimes * Sun Microsystems, Inc. 9dea673e9SRodney W. Grimes * 10dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 12dea673e9SRodney W. Grimes * are met: 13dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 20dea673e9SRodney W. Grimes * without specific prior written permission. 21dea673e9SRodney W. Grimes * 22dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32dea673e9SRodney W. Grimes * SUCH DAMAGE. 33dea673e9SRodney W. Grimes */ 34dea673e9SRodney W. Grimes 35b728350eSDavid E. O'Brien #if 0 36dea673e9SRodney W. Grimes #ifndef lint 37a42a667dSPoul-Henning Kamp static char const copyright[] = 38dea673e9SRodney W. Grimes "@(#) Copyright (c) 1984, 1993\n\ 39dea673e9SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 40dea673e9SRodney W. Grimes #endif /* not lint */ 41dea673e9SRodney W. Grimes 42dea673e9SRodney W. Grimes #ifndef lint 43a42a667dSPoul-Henning Kamp static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94"; 44dea673e9SRodney W. Grimes #endif /* not lint */ 45b728350eSDavid E. O'Brien #endif 46b728350eSDavid E. O'Brien #include <sys/cdefs.h> 47b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 48dea673e9SRodney W. Grimes 49dea673e9SRodney W. Grimes /* 50dea673e9SRodney W. Grimes * arp - display, set, and delete arp table entries 51dea673e9SRodney W. Grimes */ 52dea673e9SRodney W. Grimes 53dea673e9SRodney W. Grimes #include <sys/param.h> 54dea673e9SRodney W. Grimes #include <sys/file.h> 55dea673e9SRodney W. Grimes #include <sys/socket.h> 56a42a667dSPoul-Henning Kamp #include <sys/sockio.h> 57dea673e9SRodney W. Grimes #include <sys/sysctl.h> 58a42a667dSPoul-Henning Kamp #include <sys/ioctl.h> 59628d2ac1SGarrett Wollman #include <sys/time.h> 60dea673e9SRodney W. Grimes 61dea673e9SRodney W. Grimes #include <net/if.h> 62dea673e9SRodney W. Grimes #include <net/if_dl.h> 63dea673e9SRodney W. Grimes #include <net/if_types.h> 64dea673e9SRodney W. Grimes #include <net/route.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> 82a2ac74c1SRenato Botelho #include <libxo/xo.h> 83dea673e9SRodney W. Grimes 8404ae8c64SRenato Botelho typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in, 8504ae8c64SRenato Botelho struct rt_msghdr *rtm); 86a42a667dSPoul-Henning Kamp 87bdf932aeSLuigi Rizzo static int search(u_long addr, action_fn *action); 88bdf932aeSLuigi Rizzo static action_fn print_entry; 89bdf932aeSLuigi Rizzo static action_fn nuke_entry; 90bdf932aeSLuigi Rizzo 919711a168SGleb Smirnoff static int delete(char *host); 92bdf932aeSLuigi Rizzo static void usage(void); 93bdf932aeSLuigi Rizzo static int set(int argc, char **argv); 94f3f8b226SRuslan Ermilov static int get(char *host); 95bdf932aeSLuigi Rizzo static int file(char *name); 9668839124SLuigi Rizzo static struct rt_msghdr *rtmsg(int cmd, 979711a168SGleb Smirnoff struct sockaddr_in *dst, struct sockaddr_dl *sdl); 98f3f8b226SRuslan Ermilov static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); 999711a168SGleb Smirnoff static struct sockaddr_in *getaddr(char *host); 100f3f8b226SRuslan Ermilov static int valid_type(int type); 101bdf932aeSLuigi Rizzo 1028dc4b495SJulian Elischer static int nflag; /* no reverse dns lookups */ 103b9de94e9SYaroslav Tykhiy static char *rifname; 104dea673e9SRodney W. Grimes 10592968305SRuslan Ermilov static time_t expire_time; 1069711a168SGleb Smirnoff static int flags, doing_proxy; 10768839124SLuigi Rizzo 108c1ed96c2SGeorge V. Neville-Neil struct if_nameindex *ifnameindex; 109c1ed96c2SGeorge V. Neville-Neil 1108dc4b495SJulian Elischer /* which function we're supposed to do */ 1118dc4b495SJulian Elischer #define F_GET 1 1128dc4b495SJulian Elischer #define F_SET 2 1138dc4b495SJulian Elischer #define F_FILESET 3 1148dc4b495SJulian Elischer #define F_REPLACE 4 1158dc4b495SJulian Elischer #define F_DELETE 5 1168dc4b495SJulian Elischer 1178dc4b495SJulian Elischer #define SETFUNC(f) { if (func) usage(); func = (f); } 1188dc4b495SJulian Elischer 119a2ac74c1SRenato Botelho #define ARP_XO_VERSION "1" 120a2ac74c1SRenato Botelho 121a42a667dSPoul-Henning Kamp int 1223f844a22SRuslan Ermilov main(int argc, char *argv[]) 123dea673e9SRodney W. Grimes { 1248dc4b495SJulian Elischer int ch, func = 0; 1258dc4b495SJulian Elischer int rtn = 0; 126bdf932aeSLuigi Rizzo int aflag = 0; /* do it for all entries */ 127dea673e9SRodney W. Grimes 128a2ac74c1SRenato Botelho argc = xo_parse_args(argc, argv); 129a2ac74c1SRenato Botelho if (argc < 0) 130a2ac74c1SRenato Botelho exit(1); 131a2ac74c1SRenato Botelho 132b9de94e9SYaroslav Tykhiy while ((ch = getopt(argc, argv, "andfsSi:")) != -1) 133eac295c9SRemko Lodder switch(ch) { 134dea673e9SRodney W. Grimes case 'a': 1358dc4b495SJulian Elischer aflag = 1; 1368dc4b495SJulian Elischer break; 137dea673e9SRodney W. Grimes case 'd': 1388dc4b495SJulian Elischer SETFUNC(F_DELETE); 1398dc4b495SJulian Elischer break; 140dea673e9SRodney W. Grimes case 'n': 141dea673e9SRodney W. Grimes nflag = 1; 1428dc4b495SJulian Elischer break; 143a42a667dSPoul-Henning Kamp case 'S': 1448dc4b495SJulian Elischer SETFUNC(F_REPLACE); 1458dc4b495SJulian Elischer break; 146dea673e9SRodney W. Grimes case 's': 1478dc4b495SJulian Elischer SETFUNC(F_SET); 1488dc4b495SJulian Elischer break; 14995319e17SJordan K. Hubbard case 'f' : 1508dc4b495SJulian Elischer SETFUNC(F_FILESET); 1518dc4b495SJulian Elischer break; 152b9de94e9SYaroslav Tykhiy case 'i': 153b9de94e9SYaroslav Tykhiy rifname = optarg; 154b9de94e9SYaroslav Tykhiy break; 155dea673e9SRodney W. Grimes case '?': 156dea673e9SRodney W. Grimes default: 157dea673e9SRodney W. Grimes usage(); 158dea673e9SRodney W. Grimes } 1598dc4b495SJulian Elischer argc -= optind; 1608dc4b495SJulian Elischer argv += optind; 1618dc4b495SJulian Elischer 1628dc4b495SJulian Elischer if (!func) 1638dc4b495SJulian Elischer func = F_GET; 164b9de94e9SYaroslav Tykhiy if (rifname) { 16508369852SBrooks Davis if (func != F_GET && !(func == F_DELETE && aflag)) 166a2ac74c1SRenato Botelho xo_errx(1, "-i not applicable to this operation"); 167b9de94e9SYaroslav Tykhiy if (if_nametoindex(rifname) == 0) { 168b9de94e9SYaroslav Tykhiy if (errno == ENXIO) 169a2ac74c1SRenato Botelho xo_errx(1, "interface %s does not exist", 170a2ac74c1SRenato Botelho rifname); 171b9de94e9SYaroslav Tykhiy else 172a2ac74c1SRenato Botelho xo_err(1, "if_nametoindex(%s)", rifname); 173b9de94e9SYaroslav Tykhiy } 174b9de94e9SYaroslav Tykhiy } 1758dc4b495SJulian Elischer switch (func) { 1768dc4b495SJulian Elischer case F_GET: 1778dc4b495SJulian Elischer if (aflag) { 1788dc4b495SJulian Elischer if (argc != 0) 179dea673e9SRodney W. Grimes usage(); 180a2ac74c1SRenato Botelho 181a2ac74c1SRenato Botelho xo_set_version(ARP_XO_VERSION); 182a2ac74c1SRenato Botelho xo_open_container("arp"); 183a2ac74c1SRenato Botelho xo_open_list("arp-cache"); 184a2ac74c1SRenato Botelho 1858dc4b495SJulian Elischer search(0, print_entry); 186a2ac74c1SRenato Botelho 187a2ac74c1SRenato Botelho xo_close_list("arp-cache"); 188a2ac74c1SRenato Botelho xo_close_container("arp"); 189a2ac74c1SRenato Botelho xo_finish(); 1908dc4b495SJulian Elischer } else { 1918dc4b495SJulian Elischer if (argc != 1) 1928dc4b495SJulian Elischer usage(); 193f3f8b226SRuslan Ermilov rtn = get(argv[0]); 1948dc4b495SJulian Elischer } 1958dc4b495SJulian Elischer break; 1968dc4b495SJulian Elischer case F_SET: 1978dc4b495SJulian Elischer case F_REPLACE: 1983f844a22SRuslan Ermilov if (argc < 2 || argc > 6) 1998dc4b495SJulian Elischer usage(); 2008dc4b495SJulian Elischer if (func == F_REPLACE) 2019711a168SGleb Smirnoff (void)delete(argv[0]); 2028dc4b495SJulian Elischer rtn = set(argc, argv) ? 1 : 0; 2038dc4b495SJulian Elischer break; 2048dc4b495SJulian Elischer case F_DELETE: 2058dc4b495SJulian Elischer if (aflag) { 2068dc4b495SJulian Elischer if (argc != 0) 2078dc4b495SJulian Elischer usage(); 2088dc4b495SJulian Elischer search(0, nuke_entry); 209876fc15dSGleb Smirnoff } else { 210876fc15dSGleb Smirnoff if (argc != 1) 211876fc15dSGleb Smirnoff usage(); 2129711a168SGleb Smirnoff rtn = delete(argv[0]); 213876fc15dSGleb Smirnoff } 2148dc4b495SJulian Elischer break; 2158dc4b495SJulian Elischer case F_FILESET: 2168dc4b495SJulian Elischer if (argc != 1) 2178dc4b495SJulian Elischer usage(); 2188dc4b495SJulian Elischer rtn = file(argv[0]); 2198dc4b495SJulian Elischer break; 2208dc4b495SJulian Elischer } 2218dc4b495SJulian Elischer 222c1ed96c2SGeorge V. Neville-Neil if (ifnameindex != NULL) 223c1ed96c2SGeorge V. Neville-Neil if_freenameindex(ifnameindex); 224c1ed96c2SGeorge V. Neville-Neil 2258dc4b495SJulian Elischer return (rtn); 226dea673e9SRodney W. Grimes } 227dea673e9SRodney W. Grimes 228dea673e9SRodney W. Grimes /* 229dea673e9SRodney W. Grimes * Process a file to set standard arp entries 230dea673e9SRodney W. Grimes */ 231bdf932aeSLuigi Rizzo static int 232a42a667dSPoul-Henning Kamp file(char *name) 233dea673e9SRodney W. Grimes { 234dea673e9SRodney W. Grimes FILE *fp; 235dea673e9SRodney W. Grimes int i, retval; 2363adcd042SRuslan Ermilov char line[100], arg[5][50], *args[5], *p; 237dea673e9SRodney W. Grimes 238c72049e4SPhilippe Charnier if ((fp = fopen(name, "r")) == NULL) 239a2ac74c1SRenato Botelho xo_err(1, "cannot open %s", name); 240dea673e9SRodney W. Grimes args[0] = &arg[0][0]; 241dea673e9SRodney W. Grimes args[1] = &arg[1][0]; 242dea673e9SRodney W. Grimes args[2] = &arg[2][0]; 243dea673e9SRodney W. Grimes args[3] = &arg[3][0]; 244dea673e9SRodney W. Grimes args[4] = &arg[4][0]; 245dea673e9SRodney W. Grimes retval = 0; 246d0691403SKevin Lo while(fgets(line, sizeof(line), fp) != NULL) { 2473adcd042SRuslan Ermilov if ((p = strchr(line, '#')) != NULL) 2483adcd042SRuslan Ermilov *p = '\0'; 2493adcd042SRuslan Ermilov for (p = line; isblank(*p); p++); 2504bfc3624SRuslan Ermilov if (*p == '\n' || *p == '\0') 25144acbc1aSRuslan Ermilov continue; 2523adcd042SRuslan Ermilov i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 253135adb1eSJordan K. Hubbard arg[2], arg[3], arg[4]); 254dea673e9SRodney W. Grimes if (i < 2) { 255a2ac74c1SRenato Botelho xo_warnx("bad line: %s", line); 256dea673e9SRodney W. Grimes retval = 1; 257dea673e9SRodney W. Grimes continue; 258dea673e9SRodney W. Grimes } 259dea673e9SRodney W. Grimes if (set(i, args)) 260dea673e9SRodney W. Grimes retval = 1; 261dea673e9SRodney W. Grimes } 262dea673e9SRodney W. Grimes fclose(fp); 263dea673e9SRodney W. Grimes return (retval); 264dea673e9SRodney W. Grimes } 265dea673e9SRodney W. Grimes 266dea673e9SRodney W. Grimes /* 2679711a168SGleb Smirnoff * Given a hostname, fills up a (static) struct sockaddr_in with 26868839124SLuigi Rizzo * the address of the host and returns a pointer to the 26968839124SLuigi Rizzo * structure. 270dea673e9SRodney W. Grimes */ 2719711a168SGleb Smirnoff static struct sockaddr_in * 27268839124SLuigi Rizzo getaddr(char *host) 273dea673e9SRodney W. Grimes { 274dea673e9SRodney W. Grimes struct hostent *hp; 2759711a168SGleb Smirnoff static struct sockaddr_in reply; 27668839124SLuigi Rizzo 27768839124SLuigi Rizzo bzero(&reply, sizeof(reply)); 27868839124SLuigi Rizzo reply.sin_len = sizeof(reply); 27968839124SLuigi Rizzo reply.sin_family = AF_INET; 28068839124SLuigi Rizzo reply.sin_addr.s_addr = inet_addr(host); 28168839124SLuigi Rizzo if (reply.sin_addr.s_addr == INADDR_NONE) { 28268839124SLuigi Rizzo if (!(hp = gethostbyname(host))) { 283a2ac74c1SRenato Botelho xo_warnx("%s: %s", host, hstrerror(h_errno)); 284f3f8b226SRuslan Ermilov return (NULL); 28568839124SLuigi Rizzo } 28668839124SLuigi Rizzo bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, 28768839124SLuigi Rizzo sizeof reply.sin_addr); 28868839124SLuigi Rizzo } 289f3f8b226SRuslan Ermilov return (&reply); 29068839124SLuigi Rizzo } 29168839124SLuigi Rizzo 29268839124SLuigi Rizzo /* 293f3f8b226SRuslan Ermilov * Returns true if the type is a valid one for ARP. 29468839124SLuigi Rizzo */ 29568839124SLuigi Rizzo static int 29668839124SLuigi Rizzo valid_type(int type) 29768839124SLuigi Rizzo { 298f3f8b226SRuslan Ermilov 29968839124SLuigi Rizzo switch (type) { 30068839124SLuigi Rizzo case IFT_ETHER: 30168839124SLuigi Rizzo case IFT_FDDI: 302595f03feSMark Johnston case IFT_INFINIBAND: 30368839124SLuigi Rizzo case IFT_ISO88023: 30468839124SLuigi Rizzo case IFT_ISO88024: 30568839124SLuigi Rizzo case IFT_L2VLAN: 3069af9b983SAndrew Thompson case IFT_BRIDGE: 307f3f8b226SRuslan Ermilov return (1); 308f3f8b226SRuslan Ermilov default: 309f3f8b226SRuslan Ermilov return (0); 31068839124SLuigi Rizzo } 31168839124SLuigi Rizzo } 31268839124SLuigi Rizzo 31368839124SLuigi Rizzo /* 31468839124SLuigi Rizzo * Set an individual arp entry 31568839124SLuigi Rizzo */ 31668839124SLuigi Rizzo static int 31768839124SLuigi Rizzo set(int argc, char **argv) 31868839124SLuigi Rizzo { 3199711a168SGleb Smirnoff struct sockaddr_in *addr; 3209711a168SGleb Smirnoff struct sockaddr_in *dst; /* what are we looking for */ 321e2416749SMaxime Henrion struct sockaddr_dl *sdl; 322bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 323a03b1b7cSRuslan Ermilov struct ether_addr *ea; 324dea673e9SRodney W. Grimes char *host = argv[0], *eaddr = argv[1]; 32568839124SLuigi Rizzo struct sockaddr_dl sdl_m; 326dea673e9SRodney W. Grimes 327dea673e9SRodney W. Grimes argc -= 2; 328dea673e9SRodney W. Grimes argv += 2; 329bdf932aeSLuigi Rizzo 330bdf932aeSLuigi Rizzo bzero(&sdl_m, sizeof(sdl_m)); 331bdf932aeSLuigi Rizzo sdl_m.sdl_len = sizeof(sdl_m); 332bdf932aeSLuigi Rizzo sdl_m.sdl_family = AF_LINK; 333bdf932aeSLuigi Rizzo 33468839124SLuigi Rizzo dst = getaddr(host); 33568839124SLuigi Rizzo if (dst == NULL) 336dea673e9SRodney W. Grimes return (1); 3379711a168SGleb Smirnoff doing_proxy = flags = expire_time = 0; 338dea673e9SRodney W. Grimes while (argc-- > 0) { 3397814b3b8SRenato Botelho if (strcmp(argv[0], "temp") == 0) { 3405d067f6cSGleb Smirnoff struct timespec tp; 34184d8f5b8SGleb Smirnoff int max_age; 34284d8f5b8SGleb Smirnoff size_t len = sizeof(max_age); 34384d8f5b8SGleb Smirnoff 3445d067f6cSGleb Smirnoff clock_gettime(CLOCK_MONOTONIC, &tp); 34584d8f5b8SGleb Smirnoff if (sysctlbyname("net.link.ether.inet.max_age", 34684d8f5b8SGleb Smirnoff &max_age, &len, NULL, 0) != 0) 347a2ac74c1SRenato Botelho xo_err(1, "sysctlbyname"); 34884d8f5b8SGleb Smirnoff expire_time = tp.tv_sec + max_age; 3497814b3b8SRenato Botelho } else if (strcmp(argv[0], "pub") == 0) { 350dea673e9SRodney W. Grimes flags |= RTF_ANNOUNCE; 3513f844a22SRuslan Ermilov doing_proxy = 1; 3527814b3b8SRenato Botelho if (argc && strcmp(argv[1], "only") == 0) { 3539711a168SGleb Smirnoff /* 3549711a168SGleb Smirnoff * Compatibility: in pre FreeBSD 8 times 3559711a168SGleb Smirnoff * the "only" keyword used to mean that 3569711a168SGleb Smirnoff * an ARP entry should be announced, but 3579711a168SGleb Smirnoff * not installed into routing table. 3589711a168SGleb Smirnoff */ 3593f844a22SRuslan Ermilov argc--; argv++; 3603f844a22SRuslan Ermilov } 3617814b3b8SRenato Botelho } else if (strcmp(argv[0], "blackhole") == 0) { 3622293dac2STom Rhodes if (flags & RTF_REJECT) { 363a2ac74c1SRenato Botelho xo_errx(1, "Choose one of blackhole or reject, " 36404ae8c64SRenato Botelho "not both."); 3652293dac2STom Rhodes } 366e653f1f0SSam Leffler flags |= RTF_BLACKHOLE; 3677814b3b8SRenato Botelho } else if (strcmp(argv[0], "reject") == 0) { 3682293dac2STom Rhodes if (flags & RTF_BLACKHOLE) { 369a2ac74c1SRenato Botelho xo_errx(1, "Choose one of blackhole or reject, " 37004ae8c64SRenato Botelho "not both."); 3712293dac2STom Rhodes } 372e653f1f0SSam Leffler flags |= RTF_REJECT; 3737814b3b8SRenato Botelho } else { 374a2ac74c1SRenato Botelho xo_warnx("Invalid parameter '%s'", argv[0]); 3757814b3b8SRenato Botelho usage(); 376dea673e9SRodney W. Grimes } 377dea673e9SRodney W. Grimes argv++; 378dea673e9SRodney W. Grimes } 379a03b1b7cSRuslan Ermilov ea = (struct ether_addr *)LLADDR(&sdl_m); 380a42a667dSPoul-Henning Kamp if (doing_proxy && !strcmp(eaddr, "auto")) { 38168839124SLuigi Rizzo if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { 382a2ac74c1SRenato Botelho xo_warnx("no interface found for %s", 38368839124SLuigi Rizzo inet_ntoa(dst->sin_addr)); 384a42a667dSPoul-Henning Kamp return (1); 385a42a667dSPoul-Henning Kamp } 386a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 387a42a667dSPoul-Henning Kamp } else { 38868839124SLuigi Rizzo struct ether_addr *ea1 = ether_aton(eaddr); 38968839124SLuigi Rizzo 390a47c388cSGleb Smirnoff if (ea1 == NULL) { 391a2ac74c1SRenato Botelho xo_warnx("invalid Ethernet address '%s'", eaddr); 392a47c388cSGleb Smirnoff return (1); 393a47c388cSGleb Smirnoff } else { 39468839124SLuigi Rizzo *ea = *ea1; 395a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN; 396a42a667dSPoul-Henning Kamp } 39768839124SLuigi Rizzo } 398c7ab6602SQing Li 399c7ab6602SQing Li /* 400c7ab6602SQing Li * In the case a proxy-arp entry is being added for 401c7ab6602SQing Li * a remote end point, the RTF_ANNOUNCE flag in the 402c7ab6602SQing Li * RTM_GET command is an indication to the kernel 403c7ab6602SQing Li * routing code that the interface associated with 404c7ab6602SQing Li * the prefix route covering the local end of the 405c7ab6602SQing Li * PPP link should be returned, on which ARP applies. 406c7ab6602SQing Li */ 4072f8c6c0aSPatrick Kelsey rtm = rtmsg(RTM_GET, dst, NULL); 408bdf932aeSLuigi Rizzo if (rtm == NULL) { 409a2ac74c1SRenato Botelho xo_warn("%s", host); 410dea673e9SRodney W. Grimes return (1); 411dea673e9SRodney W. Grimes } 4129711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1); 4130b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 41468839124SLuigi Rizzo 415c7ab6602SQing Li if ((sdl->sdl_family != AF_LINK) || 416c7ab6602SQing Li (rtm->rtm_flags & RTF_GATEWAY) || 417c7ab6602SQing Li !valid_type(sdl->sdl_type)) { 418a2ac74c1SRenato Botelho xo_warnx("cannot intuit interface index and type for %s", host); 419dea673e9SRodney W. Grimes return (1); 420dea673e9SRodney W. Grimes } 421dea673e9SRodney W. Grimes sdl_m.sdl_type = sdl->sdl_type; 422dea673e9SRodney W. Grimes sdl_m.sdl_index = sdl->sdl_index; 423cf779589SGleb Smirnoff return (rtmsg(RTM_ADD, dst, &sdl_m) == NULL); 424dea673e9SRodney W. Grimes } 425dea673e9SRodney W. Grimes 426dea673e9SRodney W. Grimes /* 427dea673e9SRodney W. Grimes * Display an individual arp entry 428dea673e9SRodney W. Grimes */ 429f3f8b226SRuslan Ermilov static int 430a42a667dSPoul-Henning Kamp get(char *host) 431dea673e9SRodney W. Grimes { 4329711a168SGleb Smirnoff struct sockaddr_in *addr; 433a2ac74c1SRenato Botelho int found; 434dea673e9SRodney W. Grimes 43568839124SLuigi Rizzo addr = getaddr(host); 43668839124SLuigi Rizzo if (addr == NULL) 437f3f8b226SRuslan Ermilov return (1); 438a2ac74c1SRenato Botelho 439a2ac74c1SRenato Botelho xo_set_version(ARP_XO_VERSION); 440a2ac74c1SRenato Botelho xo_open_container("arp"); 441a2ac74c1SRenato Botelho xo_open_list("arp-cache"); 442a2ac74c1SRenato Botelho 443a2ac74c1SRenato Botelho found = search(addr->sin_addr.s_addr, print_entry); 444a2ac74c1SRenato Botelho 445a2ac74c1SRenato Botelho if (found == 0) { 446a2ac74c1SRenato Botelho xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry", 4479d34414bSMike Heffner host, inet_ntoa(addr->sin_addr)); 448b9de94e9SYaroslav Tykhiy if (rifname) 449a2ac74c1SRenato Botelho xo_emit(" on {d:interface/%s}", rifname); 450a2ac74c1SRenato Botelho xo_emit("\n"); 451dea673e9SRodney W. Grimes } 452a2ac74c1SRenato Botelho 453a2ac74c1SRenato Botelho xo_close_list("arp-cache"); 454a2ac74c1SRenato Botelho xo_close_container("arp"); 455a2ac74c1SRenato Botelho xo_finish(); 456a2ac74c1SRenato Botelho 457a2ac74c1SRenato Botelho return (found == 0); 458dea673e9SRodney W. Grimes } 459dea673e9SRodney W. Grimes 460dea673e9SRodney W. Grimes /* 461dea673e9SRodney W. Grimes * Delete an arp entry 462dea673e9SRodney W. Grimes */ 463bdf932aeSLuigi Rizzo static int 4649711a168SGleb Smirnoff delete(char *host) 465dea673e9SRodney W. Grimes { 4669711a168SGleb Smirnoff struct sockaddr_in *addr, *dst; 467bdf932aeSLuigi Rizzo struct rt_msghdr *rtm; 468dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 469dea673e9SRodney W. Grimes 47068839124SLuigi Rizzo dst = getaddr(host); 47168839124SLuigi Rizzo if (dst == NULL) 472f3f8b226SRuslan Ermilov return (1); 473c7ab6602SQing Li 474c7ab6602SQing Li /* 475c7ab6602SQing Li * Perform a regular entry delete first. 476c7ab6602SQing Li */ 477c7ab6602SQing Li flags &= ~RTF_ANNOUNCE; 4786e6b3f7cSQing Li 47968839124SLuigi Rizzo for (;;) { /* try twice */ 4802f8c6c0aSPatrick Kelsey rtm = rtmsg(RTM_GET, dst, NULL); 481bdf932aeSLuigi Rizzo if (rtm == NULL) { 482a2ac74c1SRenato Botelho xo_warn("%s", host); 483dea673e9SRodney W. Grimes return (1); 484dea673e9SRodney W. Grimes } 4859711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1); 4860b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 4876e6b3f7cSQing Li 4886e6b3f7cSQing Li /* 4896e6b3f7cSQing Li * With the new L2/L3 restructure, the route 4906e6b3f7cSQing Li * returned is a prefix route. The important 4916e6b3f7cSQing Li * piece of information from the previous 4926e6b3f7cSQing Li * RTM_GET is the interface index. In the 4936e6b3f7cSQing Li * case of ECMP, the kernel will traverse 4946e6b3f7cSQing Li * the route group for the given entry. 4956e6b3f7cSQing Li */ 4966e6b3f7cSQing Li if (sdl->sdl_family == AF_LINK && 49768839124SLuigi Rizzo !(rtm->rtm_flags & RTF_GATEWAY) && 4986e6b3f7cSQing Li valid_type(sdl->sdl_type) ) { 4996e6b3f7cSQing Li addr->sin_addr.s_addr = dst->sin_addr.s_addr; 5006e6b3f7cSQing Li break; 5016e6b3f7cSQing Li } 5026e6b3f7cSQing Li 503c7ab6602SQing Li /* 5042f8c6c0aSPatrick Kelsey * Regular entry delete failed, now check if there 505c7ab6602SQing Li * is a proxy-arp entry to remove. 506c7ab6602SQing Li */ 507c7ab6602SQing Li if (flags & RTF_ANNOUNCE) { 508a2ac74c1SRenato Botelho xo_warnx("delete: cannot locate %s", host); 509dea673e9SRodney W. Grimes return (1); 510dea673e9SRodney W. Grimes } 511c7ab6602SQing Li 512c7ab6602SQing Li flags |= RTF_ANNOUNCE; 51368839124SLuigi Rizzo } 5148eca593cSQing Li rtm->rtm_flags |= RTF_LLDATA; 51568839124SLuigi Rizzo if (rtmsg(RTM_DELETE, dst, NULL) != NULL) { 5169d34414bSMike Heffner printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr)); 517a42a667dSPoul-Henning Kamp return (0); 518a42a667dSPoul-Henning Kamp } 519a42a667dSPoul-Henning Kamp return (1); 520dea673e9SRodney W. Grimes } 521dea673e9SRodney W. Grimes 522c7ab6602SQing Li 523dea673e9SRodney W. Grimes /* 5248dc4b495SJulian Elischer * Search the arp table and do some action on matching entries 525dea673e9SRodney W. Grimes */ 526bdf932aeSLuigi Rizzo static int 527bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action) 528dea673e9SRodney W. Grimes { 529dea673e9SRodney W. Grimes int mib[6]; 530dea673e9SRodney W. Grimes size_t needed; 531c34169d4SJohn Baldwin char *lim, *buf, *next; 532dea673e9SRodney W. Grimes struct rt_msghdr *rtm; 5339711a168SGleb Smirnoff struct sockaddr_in *sin2; 534dea673e9SRodney W. Grimes struct sockaddr_dl *sdl; 535b9de94e9SYaroslav Tykhiy char ifname[IF_NAMESIZE]; 53666658902SMaxim Konovalov int st, found_entry = 0; 537dea673e9SRodney W. Grimes 538dea673e9SRodney W. Grimes mib[0] = CTL_NET; 539dea673e9SRodney W. Grimes mib[1] = PF_ROUTE; 540dea673e9SRodney W. Grimes mib[2] = 0; 541dea673e9SRodney W. Grimes mib[3] = AF_INET; 542dea673e9SRodney W. Grimes mib[4] = NET_RT_FLAGS; 5436e6b3f7cSQing Li #ifdef RTF_LLINFO 544dea673e9SRodney W. Grimes mib[5] = RTF_LLINFO; 5456e6b3f7cSQing Li #else 5466e6b3f7cSQing Li mib[5] = 0; 5476e6b3f7cSQing Li #endif 548dea673e9SRodney W. Grimes if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 549a2ac74c1SRenato Botelho xo_err(1, "route-sysctl-estimate"); 55068839124SLuigi Rizzo if (needed == 0) /* empty table */ 551bdf932aeSLuigi Rizzo return 0; 55266658902SMaxim Konovalov buf = NULL; 55319beed5eSMaxim Konovalov for (;;) { 554c34169d4SJohn Baldwin buf = reallocf(buf, needed); 555c34169d4SJohn Baldwin if (buf == NULL) 556a2ac74c1SRenato Botelho xo_errx(1, "could not reallocate memory"); 55766658902SMaxim Konovalov st = sysctl(mib, 6, buf, &needed, NULL, 0); 55819beed5eSMaxim Konovalov if (st == 0 || errno != ENOMEM) 55919beed5eSMaxim Konovalov break; 56019beed5eSMaxim Konovalov needed += needed / 8; 56119beed5eSMaxim Konovalov } 56266658902SMaxim Konovalov if (st == -1) 563a2ac74c1SRenato Botelho xo_err(1, "actual retrieval of routing table"); 564dea673e9SRodney W. Grimes lim = buf + needed; 565dea673e9SRodney W. Grimes for (next = buf; next < lim; next += rtm->rtm_msglen) { 566dea673e9SRodney W. Grimes rtm = (struct rt_msghdr *)next; 5679711a168SGleb Smirnoff sin2 = (struct sockaddr_in *)(rtm + 1); 5681a5ff928SStefan Farfeleder sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); 569b9de94e9SYaroslav Tykhiy if (rifname && if_indextoname(sdl->sdl_index, ifname) && 570b9de94e9SYaroslav Tykhiy strcmp(ifname, rifname)) 571b9de94e9SYaroslav Tykhiy continue; 572dea673e9SRodney W. Grimes if (addr) { 5739d34414bSMike Heffner if (addr != sin2->sin_addr.s_addr) 574dea673e9SRodney W. Grimes continue; 575dea673e9SRodney W. Grimes found_entry = 1; 576dea673e9SRodney W. Grimes } 5779d34414bSMike Heffner (*action)(sdl, sin2, rtm); 5788dc4b495SJulian Elischer } 579ae14be20SYaroslav Tykhiy free(buf); 580f3f8b226SRuslan Ermilov return (found_entry); 5818dc4b495SJulian Elischer } 5828dc4b495SJulian Elischer 5838dc4b495SJulian Elischer /* 5848dc4b495SJulian Elischer * Display an arp entry 5858dc4b495SJulian Elischer */ 586e78c7a0aSMax Laier 587bdf932aeSLuigi Rizzo static void 5888dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl, 5899711a168SGleb Smirnoff struct sockaddr_in *addr, struct rt_msghdr *rtm) 5908dc4b495SJulian Elischer { 5913f844a22SRuslan Ermilov const char *host; 5928dc4b495SJulian Elischer struct hostent *hp; 593c1ed96c2SGeorge V. Neville-Neil struct if_nameindex *p; 5948dc4b495SJulian Elischer 595c1ed96c2SGeorge V. Neville-Neil if (ifnameindex == NULL) 596c1ed96c2SGeorge V. Neville-Neil if ((ifnameindex = if_nameindex()) == NULL) 597a2ac74c1SRenato Botelho xo_err(1, "cannot retrieve interface names"); 598a2ac74c1SRenato Botelho 599a2ac74c1SRenato Botelho xo_open_instance("arp-cache"); 600c1ed96c2SGeorge V. Neville-Neil 601dea673e9SRodney W. Grimes if (nflag == 0) 6029d34414bSMike Heffner hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 6039d34414bSMike Heffner sizeof addr->sin_addr, AF_INET); 604dea673e9SRodney W. Grimes else 605dea673e9SRodney W. Grimes hp = 0; 606dea673e9SRodney W. Grimes if (hp) 607dea673e9SRodney W. Grimes host = hp->h_name; 608dea673e9SRodney W. Grimes else { 609dea673e9SRodney W. Grimes host = "?"; 610dea673e9SRodney W. Grimes if (h_errno == TRY_AGAIN) 611dea673e9SRodney W. Grimes nflag = 1; 612dea673e9SRodney W. Grimes } 613a2ac74c1SRenato Botelho xo_emit("{:hostname/%s} ({:ip-address/%s}) at ", host, 614a2ac74c1SRenato Botelho inet_ntoa(addr->sin_addr)); 61521816de3SDoug Rabson if (sdl->sdl_alen) { 616596e374dSRuslan Ermilov if ((sdl->sdl_type == IFT_ETHER || 6179af9b983SAndrew Thompson sdl->sdl_type == IFT_L2VLAN || 6189af9b983SAndrew Thompson sdl->sdl_type == IFT_BRIDGE) && 61921816de3SDoug Rabson sdl->sdl_alen == ETHER_ADDR_LEN) 620a2ac74c1SRenato Botelho xo_emit("{:mac-address/%s}", 62104ae8c64SRenato Botelho ether_ntoa((struct ether_addr *)LLADDR(sdl))); 62221816de3SDoug Rabson else { 62321816de3SDoug Rabson int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 62421816de3SDoug Rabson 625a2ac74c1SRenato Botelho xo_emit("{:mac-address/%s}", link_ntoa(sdl) + n); 62621816de3SDoug Rabson } 62721816de3SDoug Rabson } else 628a2ac74c1SRenato Botelho xo_emit("{d:/(incomplete)}{en:incomplete/true}"); 629c1ed96c2SGeorge V. Neville-Neil 630*693d3816SEugene Grosbein for (p = ifnameindex; p && p->if_index && p->if_name; p++) { 631c1ed96c2SGeorge V. Neville-Neil if (p->if_index == sdl->sdl_index) { 632a2ac74c1SRenato Botelho xo_emit(" on {:interface/%s}", p->if_name); 633c1ed96c2SGeorge V. Neville-Neil break; 634c1ed96c2SGeorge V. Neville-Neil } 635c1ed96c2SGeorge V. Neville-Neil } 636c1ed96c2SGeorge V. Neville-Neil 637dea673e9SRodney W. Grimes if (rtm->rtm_rmx.rmx_expire == 0) 638a2ac74c1SRenato Botelho xo_emit("{d:/ permanent}{en:permanent/true}"); 63992968305SRuslan Ermilov else { 640a98c06f1SGleb Smirnoff static struct timespec tp; 641a98c06f1SGleb Smirnoff if (tp.tv_sec == 0) 642a98c06f1SGleb Smirnoff clock_gettime(CLOCK_MONOTONIC, &tp); 643a98c06f1SGleb Smirnoff if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0) 644a2ac74c1SRenato Botelho xo_emit(" expires in {:expires/%d} seconds", 645a2ac74c1SRenato Botelho (int)expire_time); 64692968305SRuslan Ermilov else 647a2ac74c1SRenato Botelho xo_emit("{d:/ expired}{en:expired/true}"); 64892968305SRuslan Ermilov } 649a2ac74c1SRenato Botelho 6506e6b3f7cSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE) 651a2ac74c1SRenato Botelho xo_emit("{d:/ published}{en:published/true}"); 652a2ac74c1SRenato Botelho 653fda82fc2SJulian Elischer switch(sdl->sdl_type) { 654fda82fc2SJulian Elischer case IFT_ETHER: 655a2ac74c1SRenato Botelho xo_emit(" [{:type/ethernet}]"); 656fda82fc2SJulian Elischer break; 65704427472SMatthew N. Dodd case IFT_FDDI: 658a2ac74c1SRenato Botelho xo_emit(" [{:type/fddi}]"); 65904427472SMatthew N. Dodd break; 66004427472SMatthew N. Dodd case IFT_ATM: 661a2ac74c1SRenato Botelho xo_emit(" [{:type/atm}]"); 66204427472SMatthew N. Dodd break; 66388d5b613SYaroslav Tykhiy case IFT_L2VLAN: 664a2ac74c1SRenato Botelho xo_emit(" [{:type/vlan}]"); 66588d5b613SYaroslav Tykhiy break; 66621816de3SDoug Rabson case IFT_IEEE1394: 667a2ac74c1SRenato Botelho xo_emit(" [{:type/firewire}]"); 66821816de3SDoug Rabson break; 6699af9b983SAndrew Thompson case IFT_BRIDGE: 670a2ac74c1SRenato Botelho xo_emit(" [{:type/bridge}]"); 6719af9b983SAndrew Thompson break; 672595f03feSMark Johnston case IFT_INFINIBAND: 673a2ac74c1SRenato Botelho xo_emit(" [{:type/infiniband}]"); 674595f03feSMark Johnston break; 675fda82fc2SJulian Elischer default: 676123b2d4aSMurray Stokely break; 677fda82fc2SJulian Elischer } 678fda82fc2SJulian Elischer 679a2ac74c1SRenato Botelho xo_emit("\n"); 680fda82fc2SJulian Elischer 681a2ac74c1SRenato Botelho xo_close_instance("arp-cache"); 682dea673e9SRodney W. Grimes } 6838dc4b495SJulian Elischer 6848dc4b495SJulian Elischer /* 6858dc4b495SJulian Elischer * Nuke an arp entry 6868dc4b495SJulian Elischer */ 687bdf932aeSLuigi Rizzo static void 6889d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused, 6894a336ef4SAlexander V. Chernikov struct sockaddr_in *addr, struct rt_msghdr *rtm) 6908dc4b495SJulian Elischer { 6918dc4b495SJulian Elischer char ip[20]; 6928dc4b495SJulian Elischer 6934a336ef4SAlexander V. Chernikov if (rtm->rtm_flags & RTF_PINNED) 6944a336ef4SAlexander V. Chernikov return; 6954a336ef4SAlexander V. Chernikov 6969d34414bSMike Heffner snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 6979711a168SGleb Smirnoff delete(ip); 698dea673e9SRodney W. Grimes } 699dea673e9SRodney W. Grimes 700bdf932aeSLuigi Rizzo static void 701a42a667dSPoul-Henning Kamp usage(void) 702dea673e9SRodney W. Grimes { 7038dc4b495SJulian Elischer fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 704b9de94e9SYaroslav Tykhiy "usage: arp [-n] [-i interface] hostname", 705b9de94e9SYaroslav Tykhiy " arp [-n] [-i interface] -a", 7063f844a22SRuslan Ermilov " arp -d hostname [pub]", 707582fa422SBrooks Davis " arp -d [-i interface] -a", 7082293dac2STom Rhodes " arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 7092293dac2STom Rhodes " arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 710c72049e4SPhilippe Charnier " arp -f filename"); 711dea673e9SRodney W. Grimes exit(1); 712dea673e9SRodney W. Grimes } 713dea673e9SRodney W. Grimes 714bdf932aeSLuigi Rizzo static struct rt_msghdr * 7159711a168SGleb Smirnoff rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl) 716dea673e9SRodney W. Grimes { 717dea673e9SRodney W. Grimes static int seq; 718dea673e9SRodney W. Grimes int rlen; 719bdf932aeSLuigi Rizzo int l; 7200c80179cSSam Leffler struct sockaddr_in so_mask, *som = &so_mask; 721bdf932aeSLuigi Rizzo static int s = -1; 722bdf932aeSLuigi Rizzo static pid_t pid; 723bdf932aeSLuigi Rizzo 724bdf932aeSLuigi Rizzo static struct { 725bdf932aeSLuigi Rizzo struct rt_msghdr m_rtm; 726bdf932aeSLuigi Rizzo char m_space[512]; 727bdf932aeSLuigi Rizzo } m_rtmsg; 728bdf932aeSLuigi Rizzo 729e2416749SMaxime Henrion struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 730e2416749SMaxime Henrion char *cp = m_rtmsg.m_space; 731bdf932aeSLuigi Rizzo 732bdf932aeSLuigi Rizzo if (s < 0) { /* first time: open socket, get pid */ 733bdf932aeSLuigi Rizzo s = socket(PF_ROUTE, SOCK_RAW, 0); 734bdf932aeSLuigi Rizzo if (s < 0) 735a2ac74c1SRenato Botelho xo_err(1, "socket"); 736bdf932aeSLuigi Rizzo pid = getpid(); 737bdf932aeSLuigi Rizzo } 738bdf932aeSLuigi Rizzo bzero(&so_mask, sizeof(so_mask)); 739bdf932aeSLuigi Rizzo so_mask.sin_len = 8; 740bdf932aeSLuigi Rizzo so_mask.sin_addr.s_addr = 0xffffffff; 741dea673e9SRodney W. Grimes 742dea673e9SRodney W. Grimes errno = 0; 74368839124SLuigi Rizzo /* 74468839124SLuigi Rizzo * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 74568839124SLuigi Rizzo * appropriately. 74668839124SLuigi Rizzo */ 747dea673e9SRodney W. Grimes if (cmd == RTM_DELETE) 748dea673e9SRodney W. Grimes goto doit; 749dea673e9SRodney W. Grimes bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 750dea673e9SRodney W. Grimes rtm->rtm_flags = flags; 751dea673e9SRodney W. Grimes rtm->rtm_version = RTM_VERSION; 752dea673e9SRodney W. Grimes 753dea673e9SRodney W. Grimes switch (cmd) { 754dea673e9SRodney W. Grimes default: 755a2ac74c1SRenato Botelho xo_errx(1, "internal wrong cmd"); 756dea673e9SRodney W. Grimes case RTM_ADD: 757dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_GATEWAY; 758dea673e9SRodney W. Grimes rtm->rtm_rmx.rmx_expire = expire_time; 759dea673e9SRodney W. Grimes rtm->rtm_inits = RTV_EXPIRE; 7608eca593cSQing Li rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 761dea673e9SRodney W. Grimes if (doing_proxy) { 762dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_NETMASK; 763dea673e9SRodney W. Grimes rtm->rtm_flags &= ~RTF_HOST; 764dea673e9SRodney W. Grimes } 765dea673e9SRodney W. Grimes /* FALLTHROUGH */ 766dea673e9SRodney W. Grimes case RTM_GET: 767dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_DST; 768dea673e9SRodney W. Grimes } 769dea673e9SRodney W. Grimes #define NEXTADDR(w, s) \ 770be5d11dcSDag-Erling Smørgrav do { \ 771f3f8b226SRuslan Ermilov if ((s) != NULL && rtm->rtm_addrs & (w)) { \ 772be5d11dcSDag-Erling Smørgrav bcopy((s), cp, sizeof(*(s))); \ 773be5d11dcSDag-Erling Smørgrav cp += SA_SIZE(s); \ 774be5d11dcSDag-Erling Smørgrav } \ 775be5d11dcSDag-Erling Smørgrav } while (0) 776dea673e9SRodney W. Grimes 77768839124SLuigi Rizzo NEXTADDR(RTA_DST, dst); 77868839124SLuigi Rizzo NEXTADDR(RTA_GATEWAY, sdl); 7790c80179cSSam Leffler NEXTADDR(RTA_NETMASK, som); 780dea673e9SRodney W. Grimes 781dea673e9SRodney W. Grimes rtm->rtm_msglen = cp - (char *)&m_rtmsg; 782dea673e9SRodney W. Grimes doit: 783dea673e9SRodney W. Grimes l = rtm->rtm_msglen; 784dea673e9SRodney W. Grimes rtm->rtm_seq = ++seq; 785dea673e9SRodney W. Grimes rtm->rtm_type = cmd; 786dea673e9SRodney W. Grimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 787dea673e9SRodney W. Grimes if (errno != ESRCH || cmd != RTM_DELETE) { 788a2ac74c1SRenato Botelho xo_warn("writing to routing socket"); 789f3f8b226SRuslan Ermilov return (NULL); 790dea673e9SRodney W. Grimes } 791dea673e9SRodney W. Grimes } 792dea673e9SRodney W. Grimes do { 793dea673e9SRodney W. Grimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 7942f8c6c0aSPatrick Kelsey } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || 7952f8c6c0aSPatrick Kelsey rtm->rtm_pid != pid)); 796dea673e9SRodney W. Grimes if (l < 0) 797a2ac74c1SRenato Botelho xo_warn("read from routing socket"); 798f3f8b226SRuslan Ermilov return (rtm); 799dea673e9SRodney W. Grimes } 800dea673e9SRodney W. Grimes 801a42a667dSPoul-Henning Kamp /* 802a42a667dSPoul-Henning Kamp * get_ether_addr - get the hardware address of an interface on the 803a42a667dSPoul-Henning Kamp * the same subnet as ipaddr. 804a42a667dSPoul-Henning Kamp */ 805a42a667dSPoul-Henning Kamp #define MAX_IFS 32 806a42a667dSPoul-Henning Kamp 807bdf932aeSLuigi Rizzo static int 808f3f8b226SRuslan Ermilov get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) 809a42a667dSPoul-Henning Kamp { 810a42a667dSPoul-Henning Kamp struct ifreq *ifr, *ifend, *ifp; 811f3f8b226SRuslan Ermilov in_addr_t ina, mask; 812a42a667dSPoul-Henning Kamp struct sockaddr_dl *dla; 813a42a667dSPoul-Henning Kamp struct ifreq ifreq; 814a42a667dSPoul-Henning Kamp struct ifconf ifc; 815a42a667dSPoul-Henning Kamp struct ifreq ifs[MAX_IFS]; 8169d34414bSMike Heffner int sock; 81768839124SLuigi Rizzo int retval = 0; 818a42a667dSPoul-Henning Kamp 8199d34414bSMike Heffner sock = socket(AF_INET, SOCK_DGRAM, 0); 8209d34414bSMike Heffner if (sock < 0) 821a2ac74c1SRenato Botelho xo_err(1, "socket"); 822a42a667dSPoul-Henning Kamp 823a42a667dSPoul-Henning Kamp ifc.ifc_len = sizeof(ifs); 824a42a667dSPoul-Henning Kamp ifc.ifc_req = ifs; 8259cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 826a2ac74c1SRenato Botelho xo_warnx("ioctl(SIOCGIFCONF)"); 82768839124SLuigi Rizzo goto done; 828a42a667dSPoul-Henning Kamp } 829a42a667dSPoul-Henning Kamp 83068839124SLuigi Rizzo #define NEXTIFR(i) \ 83168839124SLuigi Rizzo ((struct ifreq *)((char *)&(i)->ifr_addr \ 83268839124SLuigi Rizzo + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) 83368839124SLuigi Rizzo 834a42a667dSPoul-Henning Kamp /* 835a42a667dSPoul-Henning Kamp * Scan through looking for an interface with an Internet 836a42a667dSPoul-Henning Kamp * address on the same subnet as `ipaddr'. 837a42a667dSPoul-Henning Kamp */ 838a42a667dSPoul-Henning Kamp ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); 83968839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { 84068839124SLuigi Rizzo if (ifr->ifr_addr.sa_family != AF_INET) 84168839124SLuigi Rizzo continue; 842a42a667dSPoul-Henning Kamp strncpy(ifreq.ifr_name, ifr->ifr_name, 843a42a667dSPoul-Henning Kamp sizeof(ifreq.ifr_name)); 84406274ceeSGleb Smirnoff ifreq.ifr_addr = ifr->ifr_addr; 845a42a667dSPoul-Henning Kamp /* 846a42a667dSPoul-Henning Kamp * Check that the interface is up, 847a42a667dSPoul-Henning Kamp * and not point-to-point or loopback. 848a42a667dSPoul-Henning Kamp */ 8499cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) 850a42a667dSPoul-Henning Kamp continue; 851a42a667dSPoul-Henning Kamp if ((ifreq.ifr_flags & 852a42a667dSPoul-Henning Kamp (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 85304ae8c64SRenato Botelho IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST)) 85468839124SLuigi Rizzo continue; 85504ae8c64SRenato Botelho /* Get its netmask and check that it's on the right subnet. */ 8569cc7cb58SMike Heffner if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) 857a42a667dSPoul-Henning Kamp continue; 858a42a667dSPoul-Henning Kamp mask = ((struct sockaddr_in *) 859a42a667dSPoul-Henning Kamp &ifreq.ifr_addr)->sin_addr.s_addr; 86068839124SLuigi Rizzo ina = ((struct sockaddr_in *) 86168839124SLuigi Rizzo &ifr->ifr_addr)->sin_addr.s_addr; 86268839124SLuigi Rizzo if ((ipaddr & mask) == (ina & mask)) 86368839124SLuigi Rizzo break; /* ok, we got it! */ 864a42a667dSPoul-Henning Kamp } 865a42a667dSPoul-Henning Kamp 86668839124SLuigi Rizzo if (ifr >= ifend) 86768839124SLuigi Rizzo goto done; 868a42a667dSPoul-Henning Kamp 869a42a667dSPoul-Henning Kamp /* 870a42a667dSPoul-Henning Kamp * Now scan through again looking for a link-level address 871a42a667dSPoul-Henning Kamp * for this interface. 872a42a667dSPoul-Henning Kamp */ 873a42a667dSPoul-Henning Kamp ifp = ifr; 87468839124SLuigi Rizzo for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) 87568839124SLuigi Rizzo if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && 87668839124SLuigi Rizzo ifr->ifr_addr.sa_family == AF_LINK) 87768839124SLuigi Rizzo break; 87868839124SLuigi Rizzo if (ifr >= ifend) 87968839124SLuigi Rizzo goto done; 880a42a667dSPoul-Henning Kamp /* 881a42a667dSPoul-Henning Kamp * Found the link-level address - copy it out 882a42a667dSPoul-Henning Kamp */ 883a42a667dSPoul-Henning Kamp dla = (struct sockaddr_dl *) &ifr->ifr_addr; 884a42a667dSPoul-Henning Kamp memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 8857814b3b8SRenato Botelho printf("using interface %s for proxy with address %s\n", ifp->ifr_name, 8867814b3b8SRenato Botelho ether_ntoa(hwaddr)); 88768839124SLuigi Rizzo retval = dla->sdl_alen; 88868839124SLuigi Rizzo done: 88968839124SLuigi Rizzo close(sock); 890f3f8b226SRuslan Ermilov return (retval); 891a42a667dSPoul-Henning Kamp } 892