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
35dea673e9SRodney W. Grimes /*
36dea673e9SRodney W. Grimes * arp - display, set, and delete arp table entries
37dea673e9SRodney W. Grimes */
38dea673e9SRodney W. Grimes
39dea673e9SRodney W. Grimes #include <sys/param.h>
40dea673e9SRodney W. Grimes #include <sys/file.h>
41dea673e9SRodney W. Grimes #include <sys/socket.h>
42a42a667dSPoul-Henning Kamp #include <sys/sockio.h>
43dea673e9SRodney W. Grimes #include <sys/sysctl.h>
44a42a667dSPoul-Henning Kamp #include <sys/ioctl.h>
45628d2ac1SGarrett Wollman #include <sys/time.h>
46dea673e9SRodney W. Grimes
47dea673e9SRodney W. Grimes #include <net/if.h>
48dea673e9SRodney W. Grimes #include <net/if_dl.h>
49dea673e9SRodney W. Grimes #include <net/if_types.h>
50dea673e9SRodney W. Grimes #include <net/route.h>
51dea673e9SRodney W. Grimes
52dea673e9SRodney W. Grimes #include <netinet/in.h>
53dea673e9SRodney W. Grimes #include <netinet/if_ether.h>
54dea673e9SRodney W. Grimes
55dea673e9SRodney W. Grimes #include <arpa/inet.h>
56dea673e9SRodney W. Grimes
5744acbc1aSRuslan Ermilov #include <ctype.h>
58dea673e9SRodney W. Grimes #include <errno.h>
59c72049e4SPhilippe Charnier #include <netdb.h>
60dea673e9SRodney W. Grimes #include <nlist.h>
61c72049e4SPhilippe Charnier #include <paths.h>
626ad73dbfSAlexander V. Chernikov #include <stdbool.h>
63dea673e9SRodney W. Grimes #include <stdio.h>
64a42a667dSPoul-Henning Kamp #include <stdlib.h>
65467a0b06SMike Barcroft #include <string.h>
66a42a667dSPoul-Henning Kamp #include <strings.h>
67c72049e4SPhilippe Charnier #include <unistd.h>
68d80d7349SKUROSAWA Takahiro #include <ifaddrs.h>
69a2ac74c1SRenato Botelho #include <libxo/xo.h>
706ad73dbfSAlexander V. Chernikov #include "arp.h"
71dea673e9SRodney W. Grimes
7204ae8c64SRenato Botelho typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in,
7304ae8c64SRenato Botelho struct rt_msghdr *rtm);
746ad73dbfSAlexander V. Chernikov static void nuke_entries(uint32_t ifindex, struct in_addr addr);
756ad73dbfSAlexander V. Chernikov static int print_entries(uint32_t ifindex, struct in_addr addr);
76bdf932aeSLuigi Rizzo
779711a168SGleb Smirnoff static int delete(char *host);
7872e1ea2fSAlfonso Gregory static void usage(void) __dead2;
79bdf932aeSLuigi Rizzo static int set(int argc, char **argv);
80f3f8b226SRuslan Ermilov static int get(char *host);
81bdf932aeSLuigi Rizzo static int file(char *name);
8268839124SLuigi Rizzo static struct rt_msghdr *rtmsg(int cmd,
839711a168SGleb Smirnoff struct sockaddr_in *dst, struct sockaddr_dl *sdl);
84f3f8b226SRuslan Ermilov static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr);
856ad73dbfSAlexander V. Chernikov static int set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m,
866ad73dbfSAlexander V. Chernikov char *host);
87bdf932aeSLuigi Rizzo
88c1ed96c2SGeorge V. Neville-Neil struct if_nameindex *ifnameindex;
89c1ed96c2SGeorge V. Neville-Neil
906ad73dbfSAlexander V. Chernikov struct arp_opts opts = {};
916ad73dbfSAlexander V. Chernikov
928dc4b495SJulian Elischer /* which function we're supposed to do */
938dc4b495SJulian Elischer #define F_GET 1
948dc4b495SJulian Elischer #define F_SET 2
958dc4b495SJulian Elischer #define F_FILESET 3
968dc4b495SJulian Elischer #define F_REPLACE 4
978dc4b495SJulian Elischer #define F_DELETE 5
988dc4b495SJulian Elischer
998dc4b495SJulian Elischer #define SETFUNC(f) { if (func) usage(); func = (f); }
1008dc4b495SJulian Elischer
101a2ac74c1SRenato Botelho #define ARP_XO_VERSION "1"
102a2ac74c1SRenato Botelho
103a42a667dSPoul-Henning Kamp int
main(int argc,char * argv[])1043f844a22SRuslan Ermilov main(int argc, char *argv[])
105dea673e9SRodney W. Grimes {
1068dc4b495SJulian Elischer int ch, func = 0;
1078dc4b495SJulian Elischer int rtn = 0;
108dea673e9SRodney W. Grimes
109a2ac74c1SRenato Botelho argc = xo_parse_args(argc, argv);
110a2ac74c1SRenato Botelho if (argc < 0)
111a2ac74c1SRenato Botelho exit(1);
112a2ac74c1SRenato Botelho
113b9de94e9SYaroslav Tykhiy while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
114eac295c9SRemko Lodder switch(ch) {
115dea673e9SRodney W. Grimes case 'a':
1166ad73dbfSAlexander V. Chernikov opts.aflag = true;
1178dc4b495SJulian Elischer break;
118dea673e9SRodney W. Grimes case 'd':
1198dc4b495SJulian Elischer SETFUNC(F_DELETE);
1208dc4b495SJulian Elischer break;
121dea673e9SRodney W. Grimes case 'n':
1226ad73dbfSAlexander V. Chernikov opts.nflag = true;
1238dc4b495SJulian Elischer break;
124a42a667dSPoul-Henning Kamp case 'S':
1258dc4b495SJulian Elischer SETFUNC(F_REPLACE);
1268dc4b495SJulian Elischer break;
127dea673e9SRodney W. Grimes case 's':
1288dc4b495SJulian Elischer SETFUNC(F_SET);
1298dc4b495SJulian Elischer break;
13095319e17SJordan K. Hubbard case 'f' :
1318dc4b495SJulian Elischer SETFUNC(F_FILESET);
1328dc4b495SJulian Elischer break;
133b9de94e9SYaroslav Tykhiy case 'i':
13479278872SR. Christian McDonald opts.rifname = optarg;
135b9de94e9SYaroslav Tykhiy break;
136dea673e9SRodney W. Grimes case '?':
137dea673e9SRodney W. Grimes default:
138dea673e9SRodney W. Grimes usage();
139dea673e9SRodney W. Grimes }
1408dc4b495SJulian Elischer argc -= optind;
1418dc4b495SJulian Elischer argv += optind;
1428dc4b495SJulian Elischer
1438dc4b495SJulian Elischer if (!func)
1448dc4b495SJulian Elischer func = F_GET;
14579278872SR. Christian McDonald if (opts.rifname) {
1462356b60bSLexi Winter if (func != F_GET && func != F_SET && !(func == F_DELETE && opts.aflag))
147a2ac74c1SRenato Botelho xo_errx(1, "-i not applicable to this operation");
14879278872SR. Christian McDonald if ((opts.rifindex = if_nametoindex(opts.rifname)) == 0) {
149b9de94e9SYaroslav Tykhiy if (errno == ENXIO)
150a2ac74c1SRenato Botelho xo_errx(1, "interface %s does not exist",
15179278872SR. Christian McDonald opts.rifname);
152b9de94e9SYaroslav Tykhiy else
15379278872SR. Christian McDonald xo_err(1, "if_nametoindex(%s)", opts.rifname);
154b9de94e9SYaroslav Tykhiy }
155b9de94e9SYaroslav Tykhiy }
1568dc4b495SJulian Elischer switch (func) {
1578dc4b495SJulian Elischer case F_GET:
1586ad73dbfSAlexander V. Chernikov if (opts.aflag) {
1598dc4b495SJulian Elischer if (argc != 0)
160dea673e9SRodney W. Grimes usage();
161a2ac74c1SRenato Botelho
162a2ac74c1SRenato Botelho xo_set_version(ARP_XO_VERSION);
163a2ac74c1SRenato Botelho xo_open_container("arp");
164a2ac74c1SRenato Botelho xo_open_list("arp-cache");
165a2ac74c1SRenato Botelho
1666ad73dbfSAlexander V. Chernikov struct in_addr all_addrs = {};
16779278872SR. Christian McDonald print_entries(opts.rifindex, all_addrs);
168a2ac74c1SRenato Botelho
169a2ac74c1SRenato Botelho xo_close_list("arp-cache");
170a2ac74c1SRenato Botelho xo_close_container("arp");
171*417842f9SYan-Hao Wang if (xo_finish() < 0)
172*417842f9SYan-Hao Wang xo_err(1, "stdout");
1738dc4b495SJulian Elischer } else {
1748dc4b495SJulian Elischer if (argc != 1)
1758dc4b495SJulian Elischer usage();
176f3f8b226SRuslan Ermilov rtn = get(argv[0]);
1778dc4b495SJulian Elischer }
1788dc4b495SJulian Elischer break;
1798dc4b495SJulian Elischer case F_SET:
1808dc4b495SJulian Elischer case F_REPLACE:
1813f844a22SRuslan Ermilov if (argc < 2 || argc > 6)
1828dc4b495SJulian Elischer usage();
1838dc4b495SJulian Elischer if (func == F_REPLACE)
1849711a168SGleb Smirnoff (void)delete(argv[0]);
1858dc4b495SJulian Elischer rtn = set(argc, argv) ? 1 : 0;
1868dc4b495SJulian Elischer break;
1878dc4b495SJulian Elischer case F_DELETE:
1886ad73dbfSAlexander V. Chernikov if (opts.aflag) {
1898dc4b495SJulian Elischer if (argc != 0)
1908dc4b495SJulian Elischer usage();
1916ad73dbfSAlexander V. Chernikov struct in_addr all_addrs = {};
1926ad73dbfSAlexander V. Chernikov nuke_entries(0, all_addrs);
193876fc15dSGleb Smirnoff } else {
194876fc15dSGleb Smirnoff if (argc != 1)
195876fc15dSGleb Smirnoff usage();
1969711a168SGleb Smirnoff rtn = delete(argv[0]);
197876fc15dSGleb Smirnoff }
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
206c1ed96c2SGeorge V. Neville-Neil if (ifnameindex != NULL)
207c1ed96c2SGeorge V. Neville-Neil if_freenameindex(ifnameindex);
208c1ed96c2SGeorge V. Neville-Neil
209*417842f9SYan-Hao Wang exit(rtn);
210dea673e9SRodney W. Grimes }
211dea673e9SRodney W. Grimes
212dea673e9SRodney W. Grimes /*
213dea673e9SRodney W. Grimes * Process a file to set standard arp entries
214dea673e9SRodney W. Grimes */
215bdf932aeSLuigi Rizzo static int
file(char * name)216a42a667dSPoul-Henning Kamp file(char *name)
217dea673e9SRodney W. Grimes {
218dea673e9SRodney W. Grimes FILE *fp;
219dea673e9SRodney W. Grimes int i, retval;
2203adcd042SRuslan Ermilov char line[100], arg[5][50], *args[5], *p;
221dea673e9SRodney W. Grimes
222c72049e4SPhilippe Charnier if ((fp = fopen(name, "r")) == NULL)
223a2ac74c1SRenato Botelho xo_err(1, "cannot open %s", name);
224dea673e9SRodney W. Grimes args[0] = &arg[0][0];
225dea673e9SRodney W. Grimes args[1] = &arg[1][0];
226dea673e9SRodney W. Grimes args[2] = &arg[2][0];
227dea673e9SRodney W. Grimes args[3] = &arg[3][0];
228dea673e9SRodney W. Grimes args[4] = &arg[4][0];
229dea673e9SRodney W. Grimes retval = 0;
230d0691403SKevin Lo while(fgets(line, sizeof(line), fp) != NULL) {
2313adcd042SRuslan Ermilov if ((p = strchr(line, '#')) != NULL)
2323adcd042SRuslan Ermilov *p = '\0';
2333adcd042SRuslan Ermilov for (p = line; isblank(*p); p++);
2344bfc3624SRuslan Ermilov if (*p == '\n' || *p == '\0')
23544acbc1aSRuslan Ermilov continue;
2363adcd042SRuslan Ermilov i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
237135adb1eSJordan K. Hubbard arg[2], arg[3], arg[4]);
238dea673e9SRodney W. Grimes if (i < 2) {
239a2ac74c1SRenato Botelho xo_warnx("bad line: %s", line);
240dea673e9SRodney W. Grimes retval = 1;
241dea673e9SRodney W. Grimes continue;
242dea673e9SRodney W. Grimes }
243dea673e9SRodney W. Grimes if (set(i, args))
244dea673e9SRodney W. Grimes retval = 1;
245dea673e9SRodney W. Grimes }
246dea673e9SRodney W. Grimes fclose(fp);
247dea673e9SRodney W. Grimes return (retval);
248dea673e9SRodney W. Grimes }
249dea673e9SRodney W. Grimes
250dea673e9SRodney W. Grimes /*
2519711a168SGleb Smirnoff * Given a hostname, fills up a (static) struct sockaddr_in with
25268839124SLuigi Rizzo * the address of the host and returns a pointer to the
25368839124SLuigi Rizzo * structure.
254dea673e9SRodney W. Grimes */
2556ad73dbfSAlexander V. Chernikov struct sockaddr_in *
getaddr(char * host)25668839124SLuigi Rizzo getaddr(char *host)
257dea673e9SRodney W. Grimes {
258dea673e9SRodney W. Grimes struct hostent *hp;
2599711a168SGleb Smirnoff static struct sockaddr_in reply;
26068839124SLuigi Rizzo
26168839124SLuigi Rizzo bzero(&reply, sizeof(reply));
26268839124SLuigi Rizzo reply.sin_len = sizeof(reply);
26368839124SLuigi Rizzo reply.sin_family = AF_INET;
26468839124SLuigi Rizzo reply.sin_addr.s_addr = inet_addr(host);
26568839124SLuigi Rizzo if (reply.sin_addr.s_addr == INADDR_NONE) {
26668839124SLuigi Rizzo if (!(hp = gethostbyname(host))) {
267a2ac74c1SRenato Botelho xo_warnx("%s: %s", host, hstrerror(h_errno));
268f3f8b226SRuslan Ermilov return (NULL);
26968839124SLuigi Rizzo }
27068839124SLuigi Rizzo bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
27168839124SLuigi Rizzo sizeof reply.sin_addr);
27268839124SLuigi Rizzo }
273f3f8b226SRuslan Ermilov return (&reply);
27468839124SLuigi Rizzo }
27568839124SLuigi Rizzo
2766ad73dbfSAlexander V. Chernikov int valid_type(int type);
27768839124SLuigi Rizzo /*
278f3f8b226SRuslan Ermilov * Returns true if the type is a valid one for ARP.
27968839124SLuigi Rizzo */
2806ad73dbfSAlexander V. Chernikov int
valid_type(int type)28168839124SLuigi Rizzo valid_type(int type)
28268839124SLuigi Rizzo {
283f3f8b226SRuslan Ermilov
28468839124SLuigi Rizzo switch (type) {
28568839124SLuigi Rizzo case IFT_ETHER:
28668839124SLuigi Rizzo case IFT_FDDI:
287a0d6d0d0STai-hwa Liang case IFT_IEEE1394:
288595f03feSMark Johnston case IFT_INFINIBAND:
28968839124SLuigi Rizzo case IFT_ISO88023:
29068839124SLuigi Rizzo case IFT_ISO88024:
29168839124SLuigi Rizzo case IFT_L2VLAN:
2929af9b983SAndrew Thompson case IFT_BRIDGE:
293f3f8b226SRuslan Ermilov return (1);
294f3f8b226SRuslan Ermilov default:
295f3f8b226SRuslan Ermilov return (0);
29668839124SLuigi Rizzo }
29768839124SLuigi Rizzo }
29868839124SLuigi Rizzo
29968839124SLuigi Rizzo /*
30068839124SLuigi Rizzo * Set an individual arp entry
30168839124SLuigi Rizzo */
30268839124SLuigi Rizzo static int
set(int argc,char ** argv)30368839124SLuigi Rizzo set(int argc, char **argv)
30468839124SLuigi Rizzo {
3059711a168SGleb Smirnoff struct sockaddr_in *dst; /* what are we looking for */
306a03b1b7cSRuslan Ermilov struct ether_addr *ea;
307dea673e9SRodney W. Grimes char *host = argv[0], *eaddr = argv[1];
30868839124SLuigi Rizzo struct sockaddr_dl sdl_m;
309dea673e9SRodney W. Grimes
310dea673e9SRodney W. Grimes argc -= 2;
311dea673e9SRodney W. Grimes argv += 2;
312bdf932aeSLuigi Rizzo
313bdf932aeSLuigi Rizzo bzero(&sdl_m, sizeof(sdl_m));
314bdf932aeSLuigi Rizzo sdl_m.sdl_len = sizeof(sdl_m);
315bdf932aeSLuigi Rizzo sdl_m.sdl_family = AF_LINK;
316bdf932aeSLuigi Rizzo
31768839124SLuigi Rizzo dst = getaddr(host);
31868839124SLuigi Rizzo if (dst == NULL)
319dea673e9SRodney W. Grimes return (1);
320dea673e9SRodney W. Grimes while (argc-- > 0) {
3217814b3b8SRenato Botelho if (strcmp(argv[0], "temp") == 0) {
32284d8f5b8SGleb Smirnoff int max_age;
32384d8f5b8SGleb Smirnoff size_t len = sizeof(max_age);
32484d8f5b8SGleb Smirnoff
32584d8f5b8SGleb Smirnoff if (sysctlbyname("net.link.ether.inet.max_age",
32684d8f5b8SGleb Smirnoff &max_age, &len, NULL, 0) != 0)
327a2ac74c1SRenato Botelho xo_err(1, "sysctlbyname");
3286ad73dbfSAlexander V. Chernikov opts.expire_time = max_age;
3297814b3b8SRenato Botelho } else if (strcmp(argv[0], "pub") == 0) {
3306ad73dbfSAlexander V. Chernikov opts.flags |= RTF_ANNOUNCE;
3317814b3b8SRenato Botelho if (argc && strcmp(argv[1], "only") == 0) {
3329711a168SGleb Smirnoff /*
3339711a168SGleb Smirnoff * Compatibility: in pre FreeBSD 8 times
3349711a168SGleb Smirnoff * the "only" keyword used to mean that
3359711a168SGleb Smirnoff * an ARP entry should be announced, but
3369711a168SGleb Smirnoff * not installed into routing table.
3379711a168SGleb Smirnoff */
3383f844a22SRuslan Ermilov argc--; argv++;
3393f844a22SRuslan Ermilov }
3407814b3b8SRenato Botelho } else if (strcmp(argv[0], "blackhole") == 0) {
3416ad73dbfSAlexander V. Chernikov if (opts.flags & RTF_REJECT) {
342a2ac74c1SRenato Botelho xo_errx(1, "Choose one of blackhole or reject, "
34304ae8c64SRenato Botelho "not both.");
3442293dac2STom Rhodes }
3456ad73dbfSAlexander V. Chernikov opts.flags |= RTF_BLACKHOLE;
3467814b3b8SRenato Botelho } else if (strcmp(argv[0], "reject") == 0) {
3476ad73dbfSAlexander V. Chernikov if (opts.flags & RTF_BLACKHOLE) {
348a2ac74c1SRenato Botelho xo_errx(1, "Choose one of blackhole or reject, "
34904ae8c64SRenato Botelho "not both.");
3502293dac2STom Rhodes }
3516ad73dbfSAlexander V. Chernikov opts.flags |= RTF_REJECT;
3527814b3b8SRenato Botelho } else {
353a2ac74c1SRenato Botelho xo_warnx("Invalid parameter '%s'", argv[0]);
3547814b3b8SRenato Botelho usage();
355dea673e9SRodney W. Grimes }
356dea673e9SRodney W. Grimes argv++;
357dea673e9SRodney W. Grimes }
358a03b1b7cSRuslan Ermilov ea = (struct ether_addr *)LLADDR(&sdl_m);
3596ad73dbfSAlexander V. Chernikov if ((opts.flags & RTF_ANNOUNCE) && !strcmp(eaddr, "auto")) {
36068839124SLuigi Rizzo if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
361a2ac74c1SRenato Botelho xo_warnx("no interface found for %s",
36268839124SLuigi Rizzo inet_ntoa(dst->sin_addr));
363a42a667dSPoul-Henning Kamp return (1);
364a42a667dSPoul-Henning Kamp }
365a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN;
366a42a667dSPoul-Henning Kamp } else {
36768839124SLuigi Rizzo struct ether_addr *ea1 = ether_aton(eaddr);
36868839124SLuigi Rizzo
369a47c388cSGleb Smirnoff if (ea1 == NULL) {
370a2ac74c1SRenato Botelho xo_warnx("invalid Ethernet address '%s'", eaddr);
371a47c388cSGleb Smirnoff return (1);
372a47c388cSGleb Smirnoff } else {
37368839124SLuigi Rizzo *ea = *ea1;
374a03b1b7cSRuslan Ermilov sdl_m.sdl_alen = ETHER_ADDR_LEN;
375a42a667dSPoul-Henning Kamp }
37668839124SLuigi Rizzo }
3776ad73dbfSAlexander V. Chernikov #ifndef WITHOUT_NETLINK
3782356b60bSLexi Winter return (set_nl(opts.rifindex, dst, &sdl_m, host));
3796ad73dbfSAlexander V. Chernikov #else
3806ad73dbfSAlexander V. Chernikov return (set_rtsock(dst, &sdl_m, host));
3816ad73dbfSAlexander V. Chernikov #endif
3826ad73dbfSAlexander V. Chernikov }
3836ad73dbfSAlexander V. Chernikov
3846ad73dbfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
3856ad73dbfSAlexander V. Chernikov static int
set_rtsock(struct sockaddr_in * dst,struct sockaddr_dl * sdl_m,char * host)3866ad73dbfSAlexander V. Chernikov set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m, char *host)
3876ad73dbfSAlexander V. Chernikov {
3886ad73dbfSAlexander V. Chernikov struct sockaddr_in *addr;
3896ad73dbfSAlexander V. Chernikov struct sockaddr_dl *sdl;
3906ad73dbfSAlexander V. Chernikov struct rt_msghdr *rtm;
391c7ab6602SQing Li
392c7ab6602SQing Li /*
393c7ab6602SQing Li * In the case a proxy-arp entry is being added for
394c7ab6602SQing Li * a remote end point, the RTF_ANNOUNCE flag in the
395c7ab6602SQing Li * RTM_GET command is an indication to the kernel
396c7ab6602SQing Li * routing code that the interface associated with
397c7ab6602SQing Li * the prefix route covering the local end of the
398c7ab6602SQing Li * PPP link should be returned, on which ARP applies.
399c7ab6602SQing Li */
4002f8c6c0aSPatrick Kelsey rtm = rtmsg(RTM_GET, dst, NULL);
401bdf932aeSLuigi Rizzo if (rtm == NULL) {
402a2ac74c1SRenato Botelho xo_warn("%s", host);
403dea673e9SRodney W. Grimes return (1);
404dea673e9SRodney W. Grimes }
4059711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1);
4060b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
40768839124SLuigi Rizzo
408c7ab6602SQing Li if ((sdl->sdl_family != AF_LINK) ||
409c7ab6602SQing Li (rtm->rtm_flags & RTF_GATEWAY) ||
410c7ab6602SQing Li !valid_type(sdl->sdl_type)) {
411a2ac74c1SRenato Botelho xo_warnx("cannot intuit interface index and type for %s", host);
412dea673e9SRodney W. Grimes return (1);
413dea673e9SRodney W. Grimes }
4146ad73dbfSAlexander V. Chernikov sdl_m->sdl_type = sdl->sdl_type;
4156ad73dbfSAlexander V. Chernikov sdl_m->sdl_index = sdl->sdl_index;
4166ad73dbfSAlexander V. Chernikov return (rtmsg(RTM_ADD, dst, sdl_m) == NULL);
417dea673e9SRodney W. Grimes }
4186ad73dbfSAlexander V. Chernikov #endif
419dea673e9SRodney W. Grimes
420dea673e9SRodney W. Grimes /*
421dea673e9SRodney W. Grimes * Display an individual arp entry
422dea673e9SRodney W. Grimes */
423f3f8b226SRuslan Ermilov static int
get(char * host)424a42a667dSPoul-Henning Kamp get(char *host)
425dea673e9SRodney W. Grimes {
4269711a168SGleb Smirnoff struct sockaddr_in *addr;
427a2ac74c1SRenato Botelho int found;
428dea673e9SRodney W. Grimes
42968839124SLuigi Rizzo addr = getaddr(host);
43068839124SLuigi Rizzo if (addr == NULL)
431f3f8b226SRuslan Ermilov return (1);
432a2ac74c1SRenato Botelho
433a2ac74c1SRenato Botelho xo_set_version(ARP_XO_VERSION);
434a2ac74c1SRenato Botelho xo_open_container("arp");
435a2ac74c1SRenato Botelho xo_open_list("arp-cache");
436a2ac74c1SRenato Botelho
43779278872SR. Christian McDonald found = print_entries(opts.rifindex, addr->sin_addr);
438a2ac74c1SRenato Botelho
439a2ac74c1SRenato Botelho if (found == 0) {
440a2ac74c1SRenato Botelho xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry",
4419d34414bSMike Heffner host, inet_ntoa(addr->sin_addr));
44279278872SR. Christian McDonald if (opts.rifname)
44379278872SR. Christian McDonald xo_emit(" on {d:interface/%s}", opts.rifname);
444a2ac74c1SRenato Botelho xo_emit("\n");
445dea673e9SRodney W. Grimes }
446a2ac74c1SRenato Botelho
447a2ac74c1SRenato Botelho xo_close_list("arp-cache");
448a2ac74c1SRenato Botelho xo_close_container("arp");
449*417842f9SYan-Hao Wang if (xo_finish() < 0)
450*417842f9SYan-Hao Wang xo_err(1, "stdout");
451a2ac74c1SRenato Botelho
452a2ac74c1SRenato Botelho return (found == 0);
453dea673e9SRodney W. Grimes }
454dea673e9SRodney W. Grimes
455dea673e9SRodney W. Grimes /*
456dea673e9SRodney W. Grimes * Delete an arp entry
457dea673e9SRodney W. Grimes */
4586ad73dbfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
459bdf932aeSLuigi Rizzo static int
delete_rtsock(char * host)4606ad73dbfSAlexander V. Chernikov delete_rtsock(char *host)
461dea673e9SRodney W. Grimes {
4629711a168SGleb Smirnoff struct sockaddr_in *addr, *dst;
463bdf932aeSLuigi Rizzo struct rt_msghdr *rtm;
464dea673e9SRodney W. Grimes struct sockaddr_dl *sdl;
465dea673e9SRodney W. Grimes
46668839124SLuigi Rizzo dst = getaddr(host);
46768839124SLuigi Rizzo if (dst == NULL)
468f3f8b226SRuslan Ermilov return (1);
469c7ab6602SQing Li
470c7ab6602SQing Li /*
471c7ab6602SQing Li * Perform a regular entry delete first.
472c7ab6602SQing Li */
4736ad73dbfSAlexander V. Chernikov opts.flags &= ~RTF_ANNOUNCE;
4746e6b3f7cSQing Li
47568839124SLuigi Rizzo for (;;) { /* try twice */
4762f8c6c0aSPatrick Kelsey rtm = rtmsg(RTM_GET, dst, NULL);
477bdf932aeSLuigi Rizzo if (rtm == NULL) {
478a2ac74c1SRenato Botelho xo_warn("%s", host);
479dea673e9SRodney W. Grimes return (1);
480dea673e9SRodney W. Grimes }
4819711a168SGleb Smirnoff addr = (struct sockaddr_in *)(rtm + 1);
4820b46c085SLuigi Rizzo sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
4836e6b3f7cSQing Li
4846e6b3f7cSQing Li /*
4856e6b3f7cSQing Li * With the new L2/L3 restructure, the route
4866e6b3f7cSQing Li * returned is a prefix route. The important
4876e6b3f7cSQing Li * piece of information from the previous
4886e6b3f7cSQing Li * RTM_GET is the interface index. In the
4896e6b3f7cSQing Li * case of ECMP, the kernel will traverse
4906e6b3f7cSQing Li * the route group for the given entry.
4916e6b3f7cSQing Li */
4926e6b3f7cSQing Li if (sdl->sdl_family == AF_LINK &&
49368839124SLuigi Rizzo !(rtm->rtm_flags & RTF_GATEWAY) &&
4946e6b3f7cSQing Li valid_type(sdl->sdl_type) ) {
4956e6b3f7cSQing Li addr->sin_addr.s_addr = dst->sin_addr.s_addr;
4966e6b3f7cSQing Li break;
4976e6b3f7cSQing Li }
4986e6b3f7cSQing Li
499c7ab6602SQing Li /*
5002f8c6c0aSPatrick Kelsey * Regular entry delete failed, now check if there
501c7ab6602SQing Li * is a proxy-arp entry to remove.
502c7ab6602SQing Li */
5036ad73dbfSAlexander V. Chernikov if (opts.flags & RTF_ANNOUNCE) {
504a2ac74c1SRenato Botelho xo_warnx("delete: cannot locate %s", host);
505dea673e9SRodney W. Grimes return (1);
506dea673e9SRodney W. Grimes }
507c7ab6602SQing Li
5086ad73dbfSAlexander V. Chernikov opts.flags |= RTF_ANNOUNCE;
50968839124SLuigi Rizzo }
5108eca593cSQing Li rtm->rtm_flags |= RTF_LLDATA;
51168839124SLuigi Rizzo if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
5129d34414bSMike Heffner printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
513a42a667dSPoul-Henning Kamp return (0);
514a42a667dSPoul-Henning Kamp }
515a42a667dSPoul-Henning Kamp return (1);
516dea673e9SRodney W. Grimes }
5176ad73dbfSAlexander V. Chernikov #endif
5186ad73dbfSAlexander V. Chernikov
5196ad73dbfSAlexander V. Chernikov static int
delete(char * host)5206ad73dbfSAlexander V. Chernikov delete(char *host)
5216ad73dbfSAlexander V. Chernikov {
5226ad73dbfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
5236ad73dbfSAlexander V. Chernikov return (delete_rtsock(host));
5246ad73dbfSAlexander V. Chernikov #else
5256ad73dbfSAlexander V. Chernikov return (delete_nl(0, host));
5266ad73dbfSAlexander V. Chernikov #endif
5276ad73dbfSAlexander V. Chernikov }
528dea673e9SRodney W. Grimes
529c7ab6602SQing Li
530dea673e9SRodney W. Grimes /*
5318dc4b495SJulian Elischer * Search the arp table and do some action on matching entries
532dea673e9SRodney W. Grimes */
533bdf932aeSLuigi Rizzo static int
search(u_long addr,action_fn * action)534bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action)
535dea673e9SRodney W. Grimes {
536dea673e9SRodney W. Grimes int mib[6];
537dea673e9SRodney W. Grimes size_t needed;
538c34169d4SJohn Baldwin char *lim, *buf, *next;
539dea673e9SRodney W. Grimes struct rt_msghdr *rtm;
5409711a168SGleb Smirnoff struct sockaddr_in *sin2;
541dea673e9SRodney W. Grimes struct sockaddr_dl *sdl;
54266658902SMaxim Konovalov int st, found_entry = 0;
543dea673e9SRodney W. Grimes
544dea673e9SRodney W. Grimes mib[0] = CTL_NET;
545dea673e9SRodney W. Grimes mib[1] = PF_ROUTE;
546dea673e9SRodney W. Grimes mib[2] = 0;
547dea673e9SRodney W. Grimes mib[3] = AF_INET;
548dea673e9SRodney W. Grimes mib[4] = NET_RT_FLAGS;
5496e6b3f7cSQing Li #ifdef RTF_LLINFO
550dea673e9SRodney W. Grimes mib[5] = RTF_LLINFO;
5516e6b3f7cSQing Li #else
5526e6b3f7cSQing Li mib[5] = 0;
5536e6b3f7cSQing Li #endif
554dea673e9SRodney W. Grimes if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
555a2ac74c1SRenato Botelho xo_err(1, "route-sysctl-estimate");
55668839124SLuigi Rizzo if (needed == 0) /* empty table */
557bdf932aeSLuigi Rizzo return 0;
55866658902SMaxim Konovalov buf = NULL;
55919beed5eSMaxim Konovalov for (;;) {
560c34169d4SJohn Baldwin buf = reallocf(buf, needed);
561c34169d4SJohn Baldwin if (buf == NULL)
562a2ac74c1SRenato Botelho xo_errx(1, "could not reallocate memory");
56366658902SMaxim Konovalov st = sysctl(mib, 6, buf, &needed, NULL, 0);
56419beed5eSMaxim Konovalov if (st == 0 || errno != ENOMEM)
56519beed5eSMaxim Konovalov break;
56619beed5eSMaxim Konovalov needed += needed / 8;
56719beed5eSMaxim Konovalov }
56866658902SMaxim Konovalov if (st == -1)
569a2ac74c1SRenato Botelho xo_err(1, "actual retrieval of routing table");
570dea673e9SRodney W. Grimes lim = buf + needed;
571dea673e9SRodney W. Grimes for (next = buf; next < lim; next += rtm->rtm_msglen) {
572dea673e9SRodney W. Grimes rtm = (struct rt_msghdr *)next;
5739711a168SGleb Smirnoff sin2 = (struct sockaddr_in *)(rtm + 1);
5741a5ff928SStefan Farfeleder sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
57579278872SR. Christian McDonald if (opts.rifindex &&
57679278872SR. Christian McDonald (opts.rifindex != sdl->sdl_index))
577b9de94e9SYaroslav Tykhiy continue;
57879278872SR. Christian McDonald if (addr &&
57979278872SR. Christian McDonald (addr != sin2->sin_addr.s_addr))
580dea673e9SRodney W. Grimes continue;
581dea673e9SRodney W. Grimes found_entry = 1;
5829d34414bSMike Heffner (*action)(sdl, sin2, rtm);
5838dc4b495SJulian Elischer }
584ae14be20SYaroslav Tykhiy free(buf);
585f3f8b226SRuslan Ermilov return (found_entry);
5868dc4b495SJulian Elischer }
5878dc4b495SJulian Elischer
5888dc4b495SJulian Elischer /*
5898dc4b495SJulian Elischer * Display an arp entry
5908dc4b495SJulian Elischer */
591e78c7a0aSMax Laier
592bdf932aeSLuigi Rizzo static void
print_entry(struct sockaddr_dl * sdl,struct sockaddr_in * addr,struct rt_msghdr * rtm)5938dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl,
5949711a168SGleb Smirnoff struct sockaddr_in *addr, struct rt_msghdr *rtm)
5958dc4b495SJulian Elischer {
5963f844a22SRuslan Ermilov const char *host;
5978dc4b495SJulian Elischer struct hostent *hp;
598c1ed96c2SGeorge V. Neville-Neil struct if_nameindex *p;
5998dc4b495SJulian Elischer
600c1ed96c2SGeorge V. Neville-Neil if (ifnameindex == NULL)
601c1ed96c2SGeorge V. Neville-Neil if ((ifnameindex = if_nameindex()) == NULL)
602a2ac74c1SRenato Botelho xo_err(1, "cannot retrieve interface names");
603a2ac74c1SRenato Botelho
604a2ac74c1SRenato Botelho xo_open_instance("arp-cache");
605c1ed96c2SGeorge V. Neville-Neil
6066ad73dbfSAlexander V. Chernikov if (!opts.nflag)
6079d34414bSMike Heffner hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
6089d34414bSMike Heffner sizeof addr->sin_addr, AF_INET);
609dea673e9SRodney W. Grimes else
610dea673e9SRodney W. Grimes hp = 0;
611dea673e9SRodney W. Grimes if (hp)
612dea673e9SRodney W. Grimes host = hp->h_name;
613dea673e9SRodney W. Grimes else {
614dea673e9SRodney W. Grimes host = "?";
615dea673e9SRodney W. Grimes if (h_errno == TRY_AGAIN)
6166ad73dbfSAlexander V. Chernikov opts.nflag = true;
617dea673e9SRodney W. Grimes }
618a2ac74c1SRenato Botelho xo_emit("{:hostname/%s} ({:ip-address/%s}) at ", host,
619a2ac74c1SRenato Botelho inet_ntoa(addr->sin_addr));
62021816de3SDoug Rabson if (sdl->sdl_alen) {
621596e374dSRuslan Ermilov if ((sdl->sdl_type == IFT_ETHER ||
6229af9b983SAndrew Thompson sdl->sdl_type == IFT_L2VLAN ||
6239af9b983SAndrew Thompson sdl->sdl_type == IFT_BRIDGE) &&
62421816de3SDoug Rabson sdl->sdl_alen == ETHER_ADDR_LEN)
625a2ac74c1SRenato Botelho xo_emit("{:mac-address/%s}",
62604ae8c64SRenato Botelho ether_ntoa((struct ether_addr *)LLADDR(sdl)));
62721816de3SDoug Rabson else {
62821816de3SDoug Rabson int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
62921816de3SDoug Rabson
630a2ac74c1SRenato Botelho xo_emit("{:mac-address/%s}", link_ntoa(sdl) + n);
63121816de3SDoug Rabson }
63221816de3SDoug Rabson } else
633a2ac74c1SRenato Botelho xo_emit("{d:/(incomplete)}{en:incomplete/true}");
634c1ed96c2SGeorge V. Neville-Neil
635693d3816SEugene Grosbein for (p = ifnameindex; p && p->if_index && p->if_name; p++) {
636c1ed96c2SGeorge V. Neville-Neil if (p->if_index == sdl->sdl_index) {
637a2ac74c1SRenato Botelho xo_emit(" on {:interface/%s}", p->if_name);
638c1ed96c2SGeorge V. Neville-Neil break;
639c1ed96c2SGeorge V. Neville-Neil }
640c1ed96c2SGeorge V. Neville-Neil }
641c1ed96c2SGeorge V. Neville-Neil
642dea673e9SRodney W. Grimes if (rtm->rtm_rmx.rmx_expire == 0)
643a2ac74c1SRenato Botelho xo_emit("{d:/ permanent}{en:permanent/true}");
64492968305SRuslan Ermilov else {
645a98c06f1SGleb Smirnoff static struct timespec tp;
6466ad73dbfSAlexander V. Chernikov time_t expire_time = 0;
6476ad73dbfSAlexander V. Chernikov
648a98c06f1SGleb Smirnoff if (tp.tv_sec == 0)
649a98c06f1SGleb Smirnoff clock_gettime(CLOCK_MONOTONIC, &tp);
650a98c06f1SGleb Smirnoff if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0)
651a2ac74c1SRenato Botelho xo_emit(" expires in {:expires/%d} seconds",
652a2ac74c1SRenato Botelho (int)expire_time);
65392968305SRuslan Ermilov else
654a2ac74c1SRenato Botelho xo_emit("{d:/ expired}{en:expired/true}");
65592968305SRuslan Ermilov }
656a2ac74c1SRenato Botelho
6576e6b3f7cSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE)
658a2ac74c1SRenato Botelho xo_emit("{d:/ published}{en:published/true}");
659a2ac74c1SRenato Botelho
660fda82fc2SJulian Elischer switch(sdl->sdl_type) {
661fda82fc2SJulian Elischer case IFT_ETHER:
662a2ac74c1SRenato Botelho xo_emit(" [{:type/ethernet}]");
663fda82fc2SJulian Elischer break;
66404427472SMatthew N. Dodd case IFT_FDDI:
665a2ac74c1SRenato Botelho xo_emit(" [{:type/fddi}]");
66604427472SMatthew N. Dodd break;
66704427472SMatthew N. Dodd case IFT_ATM:
668a2ac74c1SRenato Botelho xo_emit(" [{:type/atm}]");
66904427472SMatthew N. Dodd break;
67088d5b613SYaroslav Tykhiy case IFT_L2VLAN:
671a2ac74c1SRenato Botelho xo_emit(" [{:type/vlan}]");
67288d5b613SYaroslav Tykhiy break;
67321816de3SDoug Rabson case IFT_IEEE1394:
674a2ac74c1SRenato Botelho xo_emit(" [{:type/firewire}]");
67521816de3SDoug Rabson break;
6769af9b983SAndrew Thompson case IFT_BRIDGE:
677a2ac74c1SRenato Botelho xo_emit(" [{:type/bridge}]");
6789af9b983SAndrew Thompson break;
679595f03feSMark Johnston case IFT_INFINIBAND:
680a2ac74c1SRenato Botelho xo_emit(" [{:type/infiniband}]");
681595f03feSMark Johnston break;
682fda82fc2SJulian Elischer default:
683123b2d4aSMurray Stokely break;
684fda82fc2SJulian Elischer }
685fda82fc2SJulian Elischer
686a2ac74c1SRenato Botelho xo_emit("\n");
687fda82fc2SJulian Elischer
688a2ac74c1SRenato Botelho xo_close_instance("arp-cache");
689dea673e9SRodney W. Grimes }
6908dc4b495SJulian Elischer
6916ad73dbfSAlexander V. Chernikov static int
print_entries(uint32_t ifindex,struct in_addr addr)6926ad73dbfSAlexander V. Chernikov print_entries(uint32_t ifindex, struct in_addr addr)
6936ad73dbfSAlexander V. Chernikov {
6946ad73dbfSAlexander V. Chernikov #ifndef WITHOUT_NETLINK
6956ad73dbfSAlexander V. Chernikov return (print_entries_nl(ifindex, addr));
6966ad73dbfSAlexander V. Chernikov #else
6976ad73dbfSAlexander V. Chernikov return (search(addr.s_addr, print_entry));
6986ad73dbfSAlexander V. Chernikov #endif
6996ad73dbfSAlexander V. Chernikov }
7006ad73dbfSAlexander V. Chernikov
7016ad73dbfSAlexander V. Chernikov
7028dc4b495SJulian Elischer /*
7038dc4b495SJulian Elischer * Nuke an arp entry
7048dc4b495SJulian Elischer */
705bdf932aeSLuigi Rizzo static void
nuke_entry(struct sockaddr_dl * sdl __unused,struct sockaddr_in * addr,struct rt_msghdr * rtm)7069d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused,
7074a336ef4SAlexander V. Chernikov struct sockaddr_in *addr, struct rt_msghdr *rtm)
7088dc4b495SJulian Elischer {
7098dc4b495SJulian Elischer char ip[20];
7108dc4b495SJulian Elischer
7114a336ef4SAlexander V. Chernikov if (rtm->rtm_flags & RTF_PINNED)
7124a336ef4SAlexander V. Chernikov return;
7134a336ef4SAlexander V. Chernikov
7149d34414bSMike Heffner snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
7159711a168SGleb Smirnoff delete(ip);
716dea673e9SRodney W. Grimes }
717dea673e9SRodney W. Grimes
718bdf932aeSLuigi Rizzo static void
nuke_entries(uint32_t ifindex,struct in_addr addr)7196ad73dbfSAlexander V. Chernikov nuke_entries(uint32_t ifindex, struct in_addr addr)
7206ad73dbfSAlexander V. Chernikov {
7216ad73dbfSAlexander V. Chernikov search(addr.s_addr, nuke_entry);
7226ad73dbfSAlexander V. Chernikov }
7236ad73dbfSAlexander V. Chernikov
7246ad73dbfSAlexander V. Chernikov static void
usage(void)725a42a667dSPoul-Henning Kamp usage(void)
726dea673e9SRodney W. Grimes {
727*417842f9SYan-Hao Wang xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
728b9de94e9SYaroslav Tykhiy "usage: arp [-n] [-i interface] hostname",
729b9de94e9SYaroslav Tykhiy " arp [-n] [-i interface] -a",
7303f844a22SRuslan Ermilov " arp -d hostname [pub]",
731582fa422SBrooks Davis " arp -d [-i interface] -a",
7322293dac2STom Rhodes " arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
7332293dac2STom Rhodes " arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
734c72049e4SPhilippe Charnier " arp -f filename");
735dea673e9SRodney W. Grimes exit(1);
736dea673e9SRodney W. Grimes }
737dea673e9SRodney W. Grimes
738bdf932aeSLuigi Rizzo static struct rt_msghdr *
rtmsg(int cmd,struct sockaddr_in * dst,struct sockaddr_dl * sdl)7399711a168SGleb Smirnoff rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl)
740dea673e9SRodney W. Grimes {
741dea673e9SRodney W. Grimes static int seq;
742dea673e9SRodney W. Grimes int rlen;
743bdf932aeSLuigi Rizzo int l;
744bdf932aeSLuigi Rizzo static int s = -1;
745bdf932aeSLuigi Rizzo static pid_t pid;
746bdf932aeSLuigi Rizzo
747bdf932aeSLuigi Rizzo static struct {
748bdf932aeSLuigi Rizzo struct rt_msghdr m_rtm;
749bdf932aeSLuigi Rizzo char m_space[512];
750bdf932aeSLuigi Rizzo } m_rtmsg;
751bdf932aeSLuigi Rizzo
752e2416749SMaxime Henrion struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
753e2416749SMaxime Henrion char *cp = m_rtmsg.m_space;
754bdf932aeSLuigi Rizzo
755bdf932aeSLuigi Rizzo if (s < 0) { /* first time: open socket, get pid */
756bdf932aeSLuigi Rizzo s = socket(PF_ROUTE, SOCK_RAW, 0);
757bdf932aeSLuigi Rizzo if (s < 0)
758a2ac74c1SRenato Botelho xo_err(1, "socket");
759bdf932aeSLuigi Rizzo pid = getpid();
760bdf932aeSLuigi Rizzo }
761dea673e9SRodney W. Grimes
762dea673e9SRodney W. Grimes errno = 0;
76368839124SLuigi Rizzo /*
76468839124SLuigi Rizzo * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
76568839124SLuigi Rizzo * appropriately.
76668839124SLuigi Rizzo */
767dea673e9SRodney W. Grimes if (cmd == RTM_DELETE)
768dea673e9SRodney W. Grimes goto doit;
769dea673e9SRodney W. Grimes bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
7706ad73dbfSAlexander V. Chernikov rtm->rtm_flags = opts.flags;
771dea673e9SRodney W. Grimes rtm->rtm_version = RTM_VERSION;
772dea673e9SRodney W. Grimes
773dea673e9SRodney W. Grimes switch (cmd) {
774dea673e9SRodney W. Grimes default:
775a2ac74c1SRenato Botelho xo_errx(1, "internal wrong cmd");
776dea673e9SRodney W. Grimes case RTM_ADD:
777dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_GATEWAY;
7786ad73dbfSAlexander V. Chernikov if (opts.expire_time != 0) {
7796ad73dbfSAlexander V. Chernikov struct timespec tp;
7806ad73dbfSAlexander V. Chernikov
7816ad73dbfSAlexander V. Chernikov clock_gettime(CLOCK_MONOTONIC, &tp);
7826ad73dbfSAlexander V. Chernikov rtm->rtm_rmx.rmx_expire = opts.expire_time + tp.tv_sec;
7836ad73dbfSAlexander V. Chernikov }
784dea673e9SRodney W. Grimes rtm->rtm_inits = RTV_EXPIRE;
7858eca593cSQing Li rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
786dea673e9SRodney W. Grimes /* FALLTHROUGH */
787dea673e9SRodney W. Grimes case RTM_GET:
788dea673e9SRodney W. Grimes rtm->rtm_addrs |= RTA_DST;
789dea673e9SRodney W. Grimes }
790dea673e9SRodney W. Grimes #define NEXTADDR(w, s) \
791be5d11dcSDag-Erling Smørgrav do { \
792f3f8b226SRuslan Ermilov if ((s) != NULL && rtm->rtm_addrs & (w)) { \
793be5d11dcSDag-Erling Smørgrav bcopy((s), cp, sizeof(*(s))); \
794be5d11dcSDag-Erling Smørgrav cp += SA_SIZE(s); \
795be5d11dcSDag-Erling Smørgrav } \
796be5d11dcSDag-Erling Smørgrav } while (0)
797dea673e9SRodney W. Grimes
79868839124SLuigi Rizzo NEXTADDR(RTA_DST, dst);
79968839124SLuigi Rizzo NEXTADDR(RTA_GATEWAY, sdl);
800dea673e9SRodney W. Grimes
801dea673e9SRodney W. Grimes rtm->rtm_msglen = cp - (char *)&m_rtmsg;
802dea673e9SRodney W. Grimes doit:
803dea673e9SRodney W. Grimes l = rtm->rtm_msglen;
804dea673e9SRodney W. Grimes rtm->rtm_seq = ++seq;
805dea673e9SRodney W. Grimes rtm->rtm_type = cmd;
806dea673e9SRodney W. Grimes if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
807dea673e9SRodney W. Grimes if (errno != ESRCH || cmd != RTM_DELETE) {
808a2ac74c1SRenato Botelho xo_warn("writing to routing socket");
809f3f8b226SRuslan Ermilov return (NULL);
810dea673e9SRodney W. Grimes }
811dea673e9SRodney W. Grimes }
812dea673e9SRodney W. Grimes do {
813dea673e9SRodney W. Grimes l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
8142f8c6c0aSPatrick Kelsey } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq ||
8152f8c6c0aSPatrick Kelsey rtm->rtm_pid != pid));
816dea673e9SRodney W. Grimes if (l < 0)
817a2ac74c1SRenato Botelho xo_warn("read from routing socket");
818f3f8b226SRuslan Ermilov return (rtm);
819dea673e9SRodney W. Grimes }
820dea673e9SRodney W. Grimes
821a42a667dSPoul-Henning Kamp /*
822a42a667dSPoul-Henning Kamp * get_ether_addr - get the hardware address of an interface on the
823ec8a394dSElyes Haouas * same subnet as ipaddr.
824a42a667dSPoul-Henning Kamp */
825bdf932aeSLuigi Rizzo static int
get_ether_addr(in_addr_t ipaddr,struct ether_addr * hwaddr)826f3f8b226SRuslan Ermilov get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr)
827a42a667dSPoul-Henning Kamp {
828d80d7349SKUROSAWA Takahiro struct ifaddrs *ifa, *ifd, *ifas = NULL;
829f3f8b226SRuslan Ermilov in_addr_t ina, mask;
830a42a667dSPoul-Henning Kamp struct sockaddr_dl *dla;
83168839124SLuigi Rizzo int retval = 0;
832a42a667dSPoul-Henning Kamp
833a42a667dSPoul-Henning Kamp /*
834a42a667dSPoul-Henning Kamp * Scan through looking for an interface with an Internet
835a42a667dSPoul-Henning Kamp * address on the same subnet as `ipaddr'.
836a42a667dSPoul-Henning Kamp */
837d80d7349SKUROSAWA Takahiro if (getifaddrs(&ifas) < 0) {
838d80d7349SKUROSAWA Takahiro xo_warnx("getifaddrs");
839d80d7349SKUROSAWA Takahiro goto done;
840d80d7349SKUROSAWA Takahiro }
841d80d7349SKUROSAWA Takahiro
842d80d7349SKUROSAWA Takahiro for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
843d80d7349SKUROSAWA Takahiro if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
84468839124SLuigi Rizzo continue;
845d80d7349SKUROSAWA Takahiro if (ifa->ifa_addr->sa_family != AF_INET)
846d80d7349SKUROSAWA Takahiro continue;
847a42a667dSPoul-Henning Kamp /*
848a42a667dSPoul-Henning Kamp * Check that the interface is up,
849a42a667dSPoul-Henning Kamp * and not point-to-point or loopback.
850a42a667dSPoul-Henning Kamp */
851d80d7349SKUROSAWA Takahiro if ((ifa->ifa_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. */
856a42a667dSPoul-Henning Kamp mask = ((struct sockaddr_in *)
857d80d7349SKUROSAWA Takahiro ifa->ifa_netmask)->sin_addr.s_addr;
85868839124SLuigi Rizzo ina = ((struct sockaddr_in *)
859d80d7349SKUROSAWA Takahiro ifa->ifa_addr)->sin_addr.s_addr;
86068839124SLuigi Rizzo if ((ipaddr & mask) == (ina & mask))
86168839124SLuigi Rizzo break; /* ok, we got it! */
862a42a667dSPoul-Henning Kamp }
863d80d7349SKUROSAWA Takahiro if (ifa == NULL)
86468839124SLuigi Rizzo goto done;
865a42a667dSPoul-Henning Kamp
866a42a667dSPoul-Henning Kamp /*
867a42a667dSPoul-Henning Kamp * Now scan through again looking for a link-level address
868a42a667dSPoul-Henning Kamp * for this interface.
869a42a667dSPoul-Henning Kamp */
870d80d7349SKUROSAWA Takahiro for (ifd = ifas; ifd != NULL; ifd = ifd->ifa_next) {
871d80d7349SKUROSAWA Takahiro if (ifd->ifa_addr == NULL)
872d80d7349SKUROSAWA Takahiro continue;
873d80d7349SKUROSAWA Takahiro if (strcmp(ifa->ifa_name, ifd->ifa_name) == 0 &&
874d80d7349SKUROSAWA Takahiro ifd->ifa_addr->sa_family == AF_LINK)
87568839124SLuigi Rizzo break;
876d80d7349SKUROSAWA Takahiro }
877d80d7349SKUROSAWA Takahiro if (ifd == NULL)
87868839124SLuigi Rizzo goto done;
879a42a667dSPoul-Henning Kamp /*
880a42a667dSPoul-Henning Kamp * Found the link-level address - copy it out
881a42a667dSPoul-Henning Kamp */
882d80d7349SKUROSAWA Takahiro dla = (struct sockaddr_dl *)ifd->ifa_addr;
883a42a667dSPoul-Henning Kamp memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
884d80d7349SKUROSAWA Takahiro printf("using interface %s for proxy with address %s\n", ifa->ifa_name,
8857814b3b8SRenato Botelho ether_ntoa(hwaddr));
88668839124SLuigi Rizzo retval = dla->sdl_alen;
88768839124SLuigi Rizzo done:
888d80d7349SKUROSAWA Takahiro if (ifas != NULL)
889d80d7349SKUROSAWA Takahiro freeifaddrs(ifas);
890f3f8b226SRuslan Ermilov return (retval);
891a42a667dSPoul-Henning Kamp }
892