xref: /freebsd/usr.sbin/arp/arp.c (revision c34169d468a7813bc9f305b6648335c359197dd4)
1dea673e9SRodney W. Grimes /*
2dea673e9SRodney W. Grimes  * Copyright (c) 1984, 1993
3dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dea673e9SRodney W. Grimes  *
5dea673e9SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6dea673e9SRodney W. Grimes  * Sun Microsystems, Inc.
7dea673e9SRodney W. Grimes  *
8dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
10dea673e9SRodney W. Grimes  * are met:
11dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16dea673e9SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18dea673e9SRodney W. Grimes  *    without specific prior written permission.
19dea673e9SRodney W. Grimes  *
20dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30dea673e9SRodney W. Grimes  * SUCH DAMAGE.
31dea673e9SRodney W. Grimes  */
32dea673e9SRodney W. Grimes 
33b728350eSDavid E. O'Brien #if 0
34dea673e9SRodney W. Grimes #ifndef lint
35a42a667dSPoul-Henning Kamp static char const copyright[] =
36dea673e9SRodney W. Grimes "@(#) Copyright (c) 1984, 1993\n\
37dea673e9SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
38dea673e9SRodney W. Grimes #endif /* not lint */
39dea673e9SRodney W. Grimes 
40dea673e9SRodney W. Grimes #ifndef lint
41a42a667dSPoul-Henning Kamp static char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
42dea673e9SRodney W. Grimes #endif /* not lint */
43b728350eSDavid E. O'Brien #endif
44b728350eSDavid E. O'Brien #include <sys/cdefs.h>
45b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$");
46dea673e9SRodney W. Grimes 
47dea673e9SRodney W. Grimes /*
48dea673e9SRodney W. Grimes  * arp - display, set, and delete arp table entries
49dea673e9SRodney W. Grimes  */
50dea673e9SRodney W. Grimes 
51dea673e9SRodney W. Grimes 
52dea673e9SRodney W. Grimes #include <sys/param.h>
53dea673e9SRodney W. Grimes #include <sys/file.h>
54dea673e9SRodney W. Grimes #include <sys/socket.h>
55a42a667dSPoul-Henning Kamp #include <sys/sockio.h>
56dea673e9SRodney W. Grimes #include <sys/sysctl.h>
57a42a667dSPoul-Henning Kamp #include <sys/ioctl.h>
58628d2ac1SGarrett Wollman #include <sys/time.h>
59dea673e9SRodney W. Grimes 
60dea673e9SRodney W. Grimes #include <net/if.h>
61dea673e9SRodney W. Grimes #include <net/if_dl.h>
62dea673e9SRodney W. Grimes #include <net/if_types.h>
63dea673e9SRodney W. Grimes #include <net/route.h>
642ab778e1SBill Paul #include <net/iso88025.h>
65dea673e9SRodney W. Grimes 
66dea673e9SRodney W. Grimes #include <netinet/in.h>
67dea673e9SRodney W. Grimes #include <netinet/if_ether.h>
68dea673e9SRodney W. Grimes 
69dea673e9SRodney W. Grimes #include <arpa/inet.h>
70dea673e9SRodney W. Grimes 
7144acbc1aSRuslan Ermilov #include <ctype.h>
72c72049e4SPhilippe Charnier #include <err.h>
73dea673e9SRodney W. Grimes #include <errno.h>
74c72049e4SPhilippe Charnier #include <netdb.h>
75dea673e9SRodney W. Grimes #include <nlist.h>
76c72049e4SPhilippe Charnier #include <paths.h>
77dea673e9SRodney W. Grimes #include <stdio.h>
78a42a667dSPoul-Henning Kamp #include <stdlib.h>
79467a0b06SMike Barcroft #include <string.h>
80a42a667dSPoul-Henning Kamp #include <strings.h>
81c72049e4SPhilippe Charnier #include <unistd.h>
82dea673e9SRodney W. Grimes 
83bdf932aeSLuigi Rizzo typedef void (action_fn)(struct sockaddr_dl *sdl,
84bdf932aeSLuigi Rizzo 	struct sockaddr_inarp *s_in, struct rt_msghdr *rtm);
85a42a667dSPoul-Henning Kamp 
86bdf932aeSLuigi Rizzo static int search(u_long addr, action_fn *action);
87bdf932aeSLuigi Rizzo static action_fn print_entry;
88bdf932aeSLuigi Rizzo static action_fn nuke_entry;
89bdf932aeSLuigi Rizzo 
9068839124SLuigi Rizzo static int delete(char *host, int do_proxy);
91bdf932aeSLuigi Rizzo static void usage(void);
92bdf932aeSLuigi Rizzo static int set(int argc, char **argv);
93f3f8b226SRuslan Ermilov static int get(char *host);
94bdf932aeSLuigi Rizzo static int file(char *name);
9568839124SLuigi Rizzo static struct rt_msghdr *rtmsg(int cmd,
9668839124SLuigi Rizzo     struct sockaddr_inarp *dst, struct sockaddr_dl *sdl);
97f3f8b226SRuslan Ermilov static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr);
98f3f8b226SRuslan Ermilov static struct sockaddr_inarp *getaddr(char *host);
99f3f8b226SRuslan Ermilov static int valid_type(int type);
100bdf932aeSLuigi Rizzo 
1018dc4b495SJulian Elischer static int nflag;	/* no reverse dns lookups */
102b9de94e9SYaroslav Tykhiy static char *rifname;
103dea673e9SRodney W. Grimes 
104bdf932aeSLuigi Rizzo static int	expire_time, flags, doing_proxy, proxy_only;
10568839124SLuigi Rizzo 
1068dc4b495SJulian Elischer /* which function we're supposed to do */
1078dc4b495SJulian Elischer #define F_GET		1
1088dc4b495SJulian Elischer #define F_SET		2
1098dc4b495SJulian Elischer #define F_FILESET	3
1108dc4b495SJulian Elischer #define F_REPLACE	4
1118dc4b495SJulian Elischer #define F_DELETE	5
1128dc4b495SJulian Elischer 
1138dc4b495SJulian Elischer #define SETFUNC(f)	{ if (func) usage(); func = (f); }
1148dc4b495SJulian Elischer 
115a42a667dSPoul-Henning Kamp int
1163f844a22SRuslan Ermilov main(int argc, char *argv[])
117dea673e9SRodney W. Grimes {
1188dc4b495SJulian Elischer 	int ch, func = 0;
1198dc4b495SJulian Elischer 	int rtn = 0;
120bdf932aeSLuigi Rizzo 	int aflag = 0;	/* do it for all entries */
121dea673e9SRodney W. Grimes 
122b9de94e9SYaroslav Tykhiy 	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
123eac295c9SRemko Lodder 		switch(ch) {
124dea673e9SRodney W. Grimes 		case 'a':
1258dc4b495SJulian Elischer 			aflag = 1;
1268dc4b495SJulian Elischer 			break;
127dea673e9SRodney W. Grimes 		case 'd':
1288dc4b495SJulian Elischer 			SETFUNC(F_DELETE);
1298dc4b495SJulian Elischer 			break;
130dea673e9SRodney W. Grimes 		case 'n':
131dea673e9SRodney W. Grimes 			nflag = 1;
1328dc4b495SJulian Elischer 			break;
133a42a667dSPoul-Henning Kamp 		case 'S':
1348dc4b495SJulian Elischer 			SETFUNC(F_REPLACE);
1358dc4b495SJulian Elischer 			break;
136dea673e9SRodney W. Grimes 		case 's':
1378dc4b495SJulian Elischer 			SETFUNC(F_SET);
1388dc4b495SJulian Elischer 			break;
13995319e17SJordan K. Hubbard 		case 'f' :
1408dc4b495SJulian Elischer 			SETFUNC(F_FILESET);
1418dc4b495SJulian Elischer 			break;
142b9de94e9SYaroslav Tykhiy 		case 'i':
143b9de94e9SYaroslav Tykhiy 			rifname = optarg;
144b9de94e9SYaroslav Tykhiy 			break;
145dea673e9SRodney W. Grimes 		case '?':
146dea673e9SRodney W. Grimes 		default:
147dea673e9SRodney W. Grimes 			usage();
148dea673e9SRodney W. Grimes 		}
1498dc4b495SJulian Elischer 	argc -= optind;
1508dc4b495SJulian Elischer 	argv += optind;
1518dc4b495SJulian Elischer 
1528dc4b495SJulian Elischer 	if (!func)
1538dc4b495SJulian Elischer 		func = F_GET;
154b9de94e9SYaroslav Tykhiy 	if (rifname) {
15508369852SBrooks Davis 		if (func != F_GET && !(func == F_DELETE && aflag))
156b9de94e9SYaroslav Tykhiy 			errx(1, "-i not applicable to this operation");
157b9de94e9SYaroslav Tykhiy 		if (if_nametoindex(rifname) == 0) {
158b9de94e9SYaroslav Tykhiy 			if (errno == ENXIO)
159b9de94e9SYaroslav Tykhiy 				errx(1, "interface %s does not exist", rifname);
160b9de94e9SYaroslav Tykhiy 			else
161b9de94e9SYaroslav Tykhiy 				err(1, "if_nametoindex(%s)", rifname);
162b9de94e9SYaroslav Tykhiy 		}
163b9de94e9SYaroslav Tykhiy 	}
1648dc4b495SJulian Elischer 	switch (func) {
1658dc4b495SJulian Elischer 	case F_GET:
1668dc4b495SJulian Elischer 		if (aflag) {
1678dc4b495SJulian Elischer 			if (argc != 0)
168dea673e9SRodney W. Grimes 				usage();
1698dc4b495SJulian Elischer 			search(0, print_entry);
1708dc4b495SJulian Elischer 		} else {
1718dc4b495SJulian Elischer 			if (argc != 1)
1728dc4b495SJulian Elischer 				usage();
173f3f8b226SRuslan Ermilov 			rtn = get(argv[0]);
1748dc4b495SJulian Elischer 		}
1758dc4b495SJulian Elischer 		break;
1768dc4b495SJulian Elischer 	case F_SET:
1778dc4b495SJulian Elischer 	case F_REPLACE:
1783f844a22SRuslan Ermilov 		if (argc < 2 || argc > 6)
1798dc4b495SJulian Elischer 			usage();
1808dc4b495SJulian Elischer 		if (func == F_REPLACE)
181f3f8b226SRuslan Ermilov 			(void)delete(argv[0], 0);
1828dc4b495SJulian Elischer 		rtn = set(argc, argv) ? 1 : 0;
1838dc4b495SJulian Elischer 		break;
1848dc4b495SJulian Elischer 	case F_DELETE:
1858dc4b495SJulian Elischer 		if (aflag) {
1868dc4b495SJulian Elischer 			if (argc != 0)
1878dc4b495SJulian Elischer 				usage();
1888dc4b495SJulian Elischer 			search(0, nuke_entry);
1898dc4b495SJulian Elischer 		} else {
19068839124SLuigi Rizzo 			if (argc == 2 && strncmp(argv[1], "pub", 3) == 0)
19168839124SLuigi Rizzo 				ch = SIN_PROXY;
19268839124SLuigi Rizzo 			else if (argc == 1)
19368839124SLuigi Rizzo 				ch = 0;
19468839124SLuigi Rizzo 			else
1958dc4b495SJulian Elischer 				usage();
19668839124SLuigi Rizzo 			rtn = delete(argv[0], ch);
1978dc4b495SJulian Elischer 		}
1988dc4b495SJulian Elischer 		break;
1998dc4b495SJulian Elischer 	case F_FILESET:
2008dc4b495SJulian Elischer 		if (argc != 1)
2018dc4b495SJulian Elischer 			usage();
2028dc4b495SJulian Elischer 		rtn = file(argv[0]);
2038dc4b495SJulian Elischer 		break;
2048dc4b495SJulian Elischer 	}
2058dc4b495SJulian Elischer 
2068dc4b495SJulian Elischer 	return (rtn);
207dea673e9SRodney W. Grimes }
208dea673e9SRodney W. Grimes 
209dea673e9SRodney W. Grimes /*
210dea673e9SRodney W. Grimes  * Process a file to set standard arp entries
211dea673e9SRodney W. Grimes  */
212bdf932aeSLuigi Rizzo static int
213a42a667dSPoul-Henning Kamp file(char *name)
214dea673e9SRodney W. Grimes {
215dea673e9SRodney W. Grimes 	FILE *fp;
216dea673e9SRodney W. Grimes 	int i, retval;
2173adcd042SRuslan Ermilov 	char line[100], arg[5][50], *args[5], *p;
218dea673e9SRodney W. Grimes 
219c72049e4SPhilippe Charnier 	if ((fp = fopen(name, "r")) == NULL)
220e2416749SMaxime Henrion 		err(1, "cannot open %s", name);
221dea673e9SRodney W. Grimes 	args[0] = &arg[0][0];
222dea673e9SRodney W. Grimes 	args[1] = &arg[1][0];
223dea673e9SRodney W. Grimes 	args[2] = &arg[2][0];
224dea673e9SRodney W. Grimes 	args[3] = &arg[3][0];
225dea673e9SRodney W. Grimes 	args[4] = &arg[4][0];
226dea673e9SRodney W. Grimes 	retval = 0;
227d0691403SKevin Lo 	while(fgets(line, sizeof(line), fp) != NULL) {
2283adcd042SRuslan Ermilov 		if ((p = strchr(line, '#')) != NULL)
2293adcd042SRuslan Ermilov 			*p = '\0';
2303adcd042SRuslan Ermilov 		for (p = line; isblank(*p); p++);
2314bfc3624SRuslan Ermilov 		if (*p == '\n' || *p == '\0')
23244acbc1aSRuslan Ermilov 			continue;
2333adcd042SRuslan Ermilov 		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
234135adb1eSJordan K. Hubbard 		    arg[2], arg[3], arg[4]);
235dea673e9SRodney W. Grimes 		if (i < 2) {
236c72049e4SPhilippe Charnier 			warnx("bad line: %s", line);
237dea673e9SRodney W. Grimes 			retval = 1;
238dea673e9SRodney W. Grimes 			continue;
239dea673e9SRodney W. Grimes 		}
240dea673e9SRodney W. Grimes 		if (set(i, args))
241dea673e9SRodney W. Grimes 			retval = 1;
242dea673e9SRodney W. Grimes 	}
243dea673e9SRodney W. Grimes 	fclose(fp);
244dea673e9SRodney W. Grimes 	return (retval);
245dea673e9SRodney W. Grimes }
246dea673e9SRodney W. Grimes 
247dea673e9SRodney W. Grimes /*
24868839124SLuigi Rizzo  * Given a hostname, fills up a (static) struct sockaddr_inarp with
24968839124SLuigi Rizzo  * the address of the host and returns a pointer to the
25068839124SLuigi Rizzo  * structure.
251dea673e9SRodney W. Grimes  */
25268839124SLuigi Rizzo static struct sockaddr_inarp *
25368839124SLuigi Rizzo getaddr(char *host)
254dea673e9SRodney W. Grimes {
255dea673e9SRodney W. Grimes 	struct hostent *hp;
25668839124SLuigi Rizzo 	static struct sockaddr_inarp reply;
25768839124SLuigi Rizzo 
25868839124SLuigi Rizzo 	bzero(&reply, sizeof(reply));
25968839124SLuigi Rizzo 	reply.sin_len = sizeof(reply);
26068839124SLuigi Rizzo 	reply.sin_family = AF_INET;
26168839124SLuigi Rizzo 	reply.sin_addr.s_addr = inet_addr(host);
26268839124SLuigi Rizzo 	if (reply.sin_addr.s_addr == INADDR_NONE) {
26368839124SLuigi Rizzo 		if (!(hp = gethostbyname(host))) {
26468839124SLuigi Rizzo 			warnx("%s: %s", host, hstrerror(h_errno));
265f3f8b226SRuslan Ermilov 			return (NULL);
26668839124SLuigi Rizzo 		}
26768839124SLuigi Rizzo 		bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
26868839124SLuigi Rizzo 			sizeof reply.sin_addr);
26968839124SLuigi Rizzo 	}
270f3f8b226SRuslan Ermilov 	return (&reply);
27168839124SLuigi Rizzo }
27268839124SLuigi Rizzo 
27368839124SLuigi Rizzo /*
274f3f8b226SRuslan Ermilov  * Returns true if the type is a valid one for ARP.
27568839124SLuigi Rizzo  */
27668839124SLuigi Rizzo static int
27768839124SLuigi Rizzo valid_type(int type)
27868839124SLuigi Rizzo {
279f3f8b226SRuslan Ermilov 
28068839124SLuigi Rizzo 	switch (type) {
28168839124SLuigi Rizzo 	case IFT_ETHER:
28268839124SLuigi Rizzo 	case IFT_FDDI:
28368839124SLuigi Rizzo 	case IFT_ISO88023:
28468839124SLuigi Rizzo 	case IFT_ISO88024:
28568839124SLuigi Rizzo 	case IFT_ISO88025:
28668839124SLuigi Rizzo 	case IFT_L2VLAN:
2879af9b983SAndrew Thompson 	case IFT_BRIDGE:
288f3f8b226SRuslan Ermilov 		return (1);
289f3f8b226SRuslan Ermilov 	default:
290f3f8b226SRuslan Ermilov 		return (0);
29168839124SLuigi Rizzo 	}
29268839124SLuigi Rizzo }
29368839124SLuigi Rizzo 
29468839124SLuigi Rizzo /*
29568839124SLuigi Rizzo  * Set an individual arp entry
29668839124SLuigi Rizzo  */
29768839124SLuigi Rizzo static int
29868839124SLuigi Rizzo set(int argc, char **argv)
29968839124SLuigi Rizzo {
30068839124SLuigi Rizzo 	struct sockaddr_inarp *addr;
30168839124SLuigi Rizzo 	struct sockaddr_inarp *dst;	/* what are we looking for */
302e2416749SMaxime Henrion 	struct sockaddr_dl *sdl;
303bdf932aeSLuigi Rizzo 	struct rt_msghdr *rtm;
304a03b1b7cSRuslan Ermilov 	struct ether_addr *ea;
305dea673e9SRodney W. Grimes 	char *host = argv[0], *eaddr = argv[1];
30668839124SLuigi Rizzo 	struct sockaddr_dl sdl_m;
307dea673e9SRodney W. Grimes 
308dea673e9SRodney W. Grimes 	argc -= 2;
309dea673e9SRodney W. Grimes 	argv += 2;
310bdf932aeSLuigi Rizzo 
311bdf932aeSLuigi Rizzo 	bzero(&sdl_m, sizeof(sdl_m));
312bdf932aeSLuigi Rizzo 	sdl_m.sdl_len = sizeof(sdl_m);
313bdf932aeSLuigi Rizzo 	sdl_m.sdl_family = AF_LINK;
314bdf932aeSLuigi Rizzo 
31568839124SLuigi Rizzo 	dst = getaddr(host);
31668839124SLuigi Rizzo 	if (dst == NULL)
317dea673e9SRodney W. Grimes 		return (1);
3183f844a22SRuslan Ermilov 	doing_proxy = flags = proxy_only = expire_time = 0;
319dea673e9SRodney W. Grimes 	while (argc-- > 0) {
320dea673e9SRodney W. Grimes 		if (strncmp(argv[0], "temp", 4) == 0) {
3213f844a22SRuslan Ermilov 			struct timeval tv;
3223f844a22SRuslan Ermilov 			gettimeofday(&tv, 0);
3233f844a22SRuslan Ermilov 			expire_time = tv.tv_sec + 20 * 60;
324e653f1f0SSam Leffler 		} else if (strncmp(argv[0], "pub", 3) == 0) {
325dea673e9SRodney W. Grimes 			flags |= RTF_ANNOUNCE;
3263f844a22SRuslan Ermilov 			doing_proxy = 1;
3273f844a22SRuslan Ermilov 			if (argc && strncmp(argv[1], "only", 3) == 0) {
3283f844a22SRuslan Ermilov 				proxy_only = 1;
32968839124SLuigi Rizzo 				dst->sin_other = SIN_PROXY;
3303f844a22SRuslan Ermilov 				argc--; argv++;
3313f844a22SRuslan Ermilov 			}
332e653f1f0SSam Leffler 		} else if (strncmp(argv[0], "blackhole", 9) == 0) {
3332293dac2STom Rhodes 			if (flags & RTF_REJECT) {
3342293dac2STom Rhodes 				printf("Choose one of blackhole or reject, not both.\n");
3352293dac2STom Rhodes 			}
336e653f1f0SSam Leffler 			flags |= RTF_BLACKHOLE;
337e653f1f0SSam Leffler 		} else if (strncmp(argv[0], "reject", 6) == 0) {
3382293dac2STom Rhodes 			if (flags & RTF_BLACKHOLE) {
3392293dac2STom Rhodes 				printf("Choose one of blackhole or reject, not both.\n");
3402293dac2STom Rhodes 			}
341e653f1f0SSam Leffler 			flags |= RTF_REJECT;
342dea673e9SRodney W. Grimes 		} else if (strncmp(argv[0], "trail", 5) == 0) {
34368839124SLuigi Rizzo 			/* XXX deprecated and undocumented feature */
344dea673e9SRodney W. Grimes 			printf("%s: Sending trailers is no longer supported\n",
345dea673e9SRodney W. Grimes 				host);
346dea673e9SRodney W. Grimes 		}
347dea673e9SRodney W. Grimes 		argv++;
348dea673e9SRodney W. Grimes 	}
349a03b1b7cSRuslan Ermilov 	ea = (struct ether_addr *)LLADDR(&sdl_m);
350a42a667dSPoul-Henning Kamp 	if (doing_proxy && !strcmp(eaddr, "auto")) {
35168839124SLuigi Rizzo 		if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
352eec827b0SRuslan Ermilov 			printf("no interface found for %s\n",
35368839124SLuigi Rizzo 			       inet_ntoa(dst->sin_addr));
354a42a667dSPoul-Henning Kamp 			return (1);
355a42a667dSPoul-Henning Kamp 		}
356a03b1b7cSRuslan Ermilov 		sdl_m.sdl_alen = ETHER_ADDR_LEN;
357a42a667dSPoul-Henning Kamp 	} else {
35868839124SLuigi Rizzo 		struct ether_addr *ea1 = ether_aton(eaddr);
35968839124SLuigi Rizzo 
360a47c388cSGleb Smirnoff 		if (ea1 == NULL) {
36168839124SLuigi Rizzo 			warnx("invalid Ethernet address '%s'", eaddr);
362a47c388cSGleb Smirnoff 			return (1);
363a47c388cSGleb Smirnoff 		} else {
36468839124SLuigi Rizzo 			*ea = *ea1;
365a03b1b7cSRuslan Ermilov 			sdl_m.sdl_alen = ETHER_ADDR_LEN;
366a42a667dSPoul-Henning Kamp 		}
36768839124SLuigi Rizzo 	}
36868839124SLuigi Rizzo 	for (;;) {	/* try at most twice */
36968839124SLuigi Rizzo 		rtm = rtmsg(RTM_GET, dst, &sdl_m);
370bdf932aeSLuigi Rizzo 		if (rtm == NULL) {
371c72049e4SPhilippe Charnier 			warn("%s", host);
372dea673e9SRodney W. Grimes 			return (1);
373dea673e9SRodney W. Grimes 		}
3749d34414bSMike Heffner 		addr = (struct sockaddr_inarp *)(rtm + 1);
3750b46c085SLuigi Rizzo 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
37668839124SLuigi Rizzo 		if (addr->sin_addr.s_addr != dst->sin_addr.s_addr)
37768839124SLuigi Rizzo 			break;
378dea673e9SRodney W. Grimes 		if (sdl->sdl_family == AF_LINK &&
37968839124SLuigi Rizzo 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
38068839124SLuigi Rizzo 		    valid_type(sdl->sdl_type) )
38168839124SLuigi Rizzo 			break;
382dea673e9SRodney W. Grimes 		if (doing_proxy == 0) {
383dea673e9SRodney W. Grimes 			printf("set: can only proxy for %s\n", host);
384dea673e9SRodney W. Grimes 			return (1);
385dea673e9SRodney W. Grimes 		}
38668839124SLuigi Rizzo 		if (dst->sin_other & SIN_PROXY) {
387dea673e9SRodney W. Grimes 			printf("set: proxy entry exists for non 802 device\n");
388dea673e9SRodney W. Grimes 			return (1);
389dea673e9SRodney W. Grimes 		}
39068839124SLuigi Rizzo 		dst->sin_other = SIN_PROXY;
3913f844a22SRuslan Ermilov 		proxy_only = 1;
392dea673e9SRodney W. Grimes 	}
39368839124SLuigi Rizzo 
394dea673e9SRodney W. Grimes 	if (sdl->sdl_family != AF_LINK) {
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 {
40968839124SLuigi Rizzo 	struct sockaddr_inarp *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
42968839124SLuigi Rizzo delete(char *host, int do_proxy)
430dea673e9SRodney W. Grimes {
43168839124SLuigi Rizzo 	struct sockaddr_inarp *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);
43968839124SLuigi Rizzo 	dst->sin_other = do_proxy;
4406e6b3f7cSQing Li 
4416e6b3f7cSQing Li 	/*
4426e6b3f7cSQing Li 	 * setup the data structure to notify the kernel
4436e6b3f7cSQing Li 	 * it is the ARP entry the RTM_GET is interested
4446e6b3f7cSQing Li 	 * in
4456e6b3f7cSQing Li 	 */
4466e6b3f7cSQing Li 	bzero(&sdl_m, sizeof(sdl_m));
4476e6b3f7cSQing Li 	sdl_m.sdl_len = sizeof(sdl_m);
4486e6b3f7cSQing Li 	sdl_m.sdl_family = AF_LINK;
4496e6b3f7cSQing Li 
45068839124SLuigi Rizzo 	for (;;) {	/* try twice */
4516e6b3f7cSQing Li 		rtm = rtmsg(RTM_GET, dst, &sdl_m);
452bdf932aeSLuigi Rizzo 		if (rtm == NULL) {
453c72049e4SPhilippe Charnier 			warn("%s", host);
454dea673e9SRodney W. Grimes 			return (1);
455dea673e9SRodney W. Grimes 		}
4569d34414bSMike Heffner 		addr = (struct sockaddr_inarp *)(rtm + 1);
4570b46c085SLuigi Rizzo 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
4586e6b3f7cSQing Li 
4596e6b3f7cSQing Li 		/*
4606e6b3f7cSQing Li 		 * With the new L2/L3 restructure, the route
4616e6b3f7cSQing Li 		 * returned is a prefix route. The important
4626e6b3f7cSQing Li 		 * piece of information from the previous
4636e6b3f7cSQing Li 		 * RTM_GET is the interface index. In the
4646e6b3f7cSQing Li 		 * case of ECMP, the kernel will traverse
4656e6b3f7cSQing Li 		 * the route group for the given entry.
4666e6b3f7cSQing Li 		 */
4676e6b3f7cSQing Li 		if (sdl->sdl_family == AF_LINK &&
46868839124SLuigi Rizzo 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
4696e6b3f7cSQing Li 		    valid_type(sdl->sdl_type) ) {
4706e6b3f7cSQing Li 			addr->sin_addr.s_addr = dst->sin_addr.s_addr;
4716e6b3f7cSQing Li 			break;
4726e6b3f7cSQing Li 		}
4736e6b3f7cSQing Li 
47468839124SLuigi Rizzo 		if (dst->sin_other & SIN_PROXY) {
47568839124SLuigi Rizzo 			fprintf(stderr, "delete: cannot locate %s\n",host);
476dea673e9SRodney W. Grimes 			return (1);
477dea673e9SRodney W. Grimes 		}
47868839124SLuigi Rizzo 		dst->sin_other = SIN_PROXY;
47968839124SLuigi Rizzo 	}
4808eca593cSQing Li 	rtm->rtm_flags |= RTF_LLDATA;
48168839124SLuigi Rizzo 	if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
4829d34414bSMike Heffner 		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
483a42a667dSPoul-Henning Kamp 		return (0);
484a42a667dSPoul-Henning Kamp 	}
485a42a667dSPoul-Henning Kamp 	return (1);
486dea673e9SRodney W. Grimes }
487dea673e9SRodney W. Grimes 
488dea673e9SRodney W. Grimes /*
4898dc4b495SJulian Elischer  * Search the arp table and do some action on matching entries
490dea673e9SRodney W. Grimes  */
491bdf932aeSLuigi Rizzo static int
492bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action)
493dea673e9SRodney W. Grimes {
494dea673e9SRodney W. Grimes 	int mib[6];
495dea673e9SRodney W. Grimes 	size_t needed;
496c34169d4SJohn Baldwin 	char *lim, *buf, *next;
497dea673e9SRodney W. Grimes 	struct rt_msghdr *rtm;
4989d34414bSMike Heffner 	struct sockaddr_inarp *sin2;
499dea673e9SRodney W. Grimes 	struct sockaddr_dl *sdl;
500b9de94e9SYaroslav Tykhiy 	char ifname[IF_NAMESIZE];
50166658902SMaxim Konovalov 	int st, found_entry = 0;
502dea673e9SRodney W. Grimes 
503dea673e9SRodney W. Grimes 	mib[0] = CTL_NET;
504dea673e9SRodney W. Grimes 	mib[1] = PF_ROUTE;
505dea673e9SRodney W. Grimes 	mib[2] = 0;
506dea673e9SRodney W. Grimes 	mib[3] = AF_INET;
507dea673e9SRodney W. Grimes 	mib[4] = NET_RT_FLAGS;
5086e6b3f7cSQing Li #ifdef RTF_LLINFO
509dea673e9SRodney W. Grimes 	mib[5] = RTF_LLINFO;
5106e6b3f7cSQing Li #else
5116e6b3f7cSQing Li 	mib[5] = 0;
5126e6b3f7cSQing Li #endif
513dea673e9SRodney W. Grimes 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
514e2416749SMaxime Henrion 		err(1, "route-sysctl-estimate");
51568839124SLuigi Rizzo 	if (needed == 0)	/* empty table */
516bdf932aeSLuigi Rizzo 		return 0;
51766658902SMaxim Konovalov 	buf = NULL;
51819beed5eSMaxim Konovalov 	for (;;) {
519c34169d4SJohn Baldwin 		buf = reallocf(buf, needed);
520c34169d4SJohn Baldwin 		if (buf == NULL)
52166658902SMaxim Konovalov 			errx(1, "could not reallocate memory");
52266658902SMaxim Konovalov 		st = sysctl(mib, 6, buf, &needed, NULL, 0);
52319beed5eSMaxim Konovalov 		if (st == 0 || errno != ENOMEM)
52419beed5eSMaxim Konovalov 			break;
52519beed5eSMaxim Konovalov 		needed += needed / 8;
52619beed5eSMaxim Konovalov 	}
52766658902SMaxim Konovalov 	if (st == -1)
528e2416749SMaxime Henrion 		err(1, "actual retrieval of routing table");
529dea673e9SRodney W. Grimes 	lim = buf + needed;
530dea673e9SRodney W. Grimes 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
531dea673e9SRodney W. Grimes 		rtm = (struct rt_msghdr *)next;
5329d34414bSMike Heffner 		sin2 = (struct sockaddr_inarp *)(rtm + 1);
5331a5ff928SStefan Farfeleder 		sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
534b9de94e9SYaroslav Tykhiy 		if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
535b9de94e9SYaroslav Tykhiy 		    strcmp(ifname, rifname))
536b9de94e9SYaroslav Tykhiy 			continue;
537dea673e9SRodney W. Grimes 		if (addr) {
5389d34414bSMike Heffner 			if (addr != sin2->sin_addr.s_addr)
539dea673e9SRodney W. Grimes 				continue;
540dea673e9SRodney W. Grimes 			found_entry = 1;
541dea673e9SRodney W. Grimes 		}
5429d34414bSMike Heffner 		(*action)(sdl, sin2, rtm);
5438dc4b495SJulian Elischer 	}
544ae14be20SYaroslav Tykhiy 	free(buf);
545f3f8b226SRuslan Ermilov 	return (found_entry);
5468dc4b495SJulian Elischer }
5478dc4b495SJulian Elischer 
5488dc4b495SJulian Elischer /*
5498dc4b495SJulian Elischer  * Display an arp entry
5508dc4b495SJulian Elischer  */
551bdf932aeSLuigi Rizzo static void
5528dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl,
5539d34414bSMike Heffner 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
5548dc4b495SJulian Elischer {
5553f844a22SRuslan Ermilov 	const char *host;
5568dc4b495SJulian Elischer 	struct hostent *hp;
55797fe20b4SKelly Yancey 	struct iso88025_sockaddr_dl_data *trld;
558e87a372bSRuslan Ermilov 	char ifname[IF_NAMESIZE];
559fda82fc2SJulian Elischer 	int seg;
5608dc4b495SJulian Elischer 
561dea673e9SRodney W. Grimes 	if (nflag == 0)
5629d34414bSMike Heffner 		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
5639d34414bSMike Heffner 		    sizeof addr->sin_addr, AF_INET);
564dea673e9SRodney W. Grimes 	else
565dea673e9SRodney W. Grimes 		hp = 0;
566dea673e9SRodney W. Grimes 	if (hp)
567dea673e9SRodney W. Grimes 		host = hp->h_name;
568dea673e9SRodney W. Grimes 	else {
569dea673e9SRodney W. Grimes 		host = "?";
570dea673e9SRodney W. Grimes 		if (h_errno == TRY_AGAIN)
571dea673e9SRodney W. Grimes 			nflag = 1;
572dea673e9SRodney W. Grimes 	}
5739d34414bSMike Heffner 	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
57421816de3SDoug Rabson 	if (sdl->sdl_alen) {
575596e374dSRuslan Ermilov 		if ((sdl->sdl_type == IFT_ETHER ||
5769af9b983SAndrew Thompson 		    sdl->sdl_type == IFT_L2VLAN ||
5779af9b983SAndrew Thompson 		    sdl->sdl_type == IFT_BRIDGE) &&
57821816de3SDoug Rabson 		    sdl->sdl_alen == ETHER_ADDR_LEN)
579a03b1b7cSRuslan Ermilov 			printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
58021816de3SDoug Rabson 		else {
58121816de3SDoug Rabson 			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
58221816de3SDoug Rabson 
58321816de3SDoug Rabson 			printf("%s", link_ntoa(sdl) + n);
58421816de3SDoug Rabson 		}
58521816de3SDoug Rabson 	} else
586dea673e9SRodney W. Grimes 		printf("(incomplete)");
587e87a372bSRuslan Ermilov 	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
588e87a372bSRuslan Ermilov 		printf(" on %s", ifname);
589dea673e9SRodney W. Grimes 	if (rtm->rtm_rmx.rmx_expire == 0)
590dea673e9SRodney W. Grimes 		printf(" permanent");
5919d34414bSMike Heffner 	if (addr->sin_other & SIN_PROXY)
592dea673e9SRodney W. Grimes 		printf(" published (proxy only)");
5936e6b3f7cSQing Li 	if (rtm->rtm_flags & RTF_ANNOUNCE)
594dea673e9SRodney W. Grimes 		printf(" published");
595fda82fc2SJulian Elischer 	switch(sdl->sdl_type) {
596fda82fc2SJulian Elischer 	case IFT_ETHER:
597fda82fc2SJulian Elischer                 printf(" [ethernet]");
598fda82fc2SJulian Elischer                 break;
599fda82fc2SJulian Elischer 	case IFT_ISO88025:
600fda82fc2SJulian Elischer                 printf(" [token-ring]");
60197fe20b4SKelly Yancey 		trld = SDL_ISO88025(sdl);
60297fe20b4SKelly Yancey 		if (trld->trld_rcf != 0) {
60397fe20b4SKelly Yancey 			printf(" rt=%x", ntohs(trld->trld_rcf));
60497fe20b4SKelly Yancey 			for (seg = 0;
60597fe20b4SKelly Yancey 			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
60697fe20b4SKelly Yancey 			     seg++)
6072ab778e1SBill Paul 				printf(":%x", ntohs(*(trld->trld_route[seg])));
60897fe20b4SKelly Yancey 		}
609fda82fc2SJulian Elischer                 break;
61004427472SMatthew N. Dodd 	case IFT_FDDI:
61104427472SMatthew N. Dodd                 printf(" [fddi]");
61204427472SMatthew N. Dodd                 break;
61304427472SMatthew N. Dodd 	case IFT_ATM:
61404427472SMatthew N. Dodd                 printf(" [atm]");
61504427472SMatthew N. Dodd                 break;
61688d5b613SYaroslav Tykhiy 	case IFT_L2VLAN:
61788d5b613SYaroslav Tykhiy 		printf(" [vlan]");
61888d5b613SYaroslav Tykhiy 		break;
61921816de3SDoug Rabson 	case IFT_IEEE1394:
62021816de3SDoug Rabson                 printf(" [firewire]");
62121816de3SDoug Rabson                 break;
6229af9b983SAndrew Thompson 	case IFT_BRIDGE:
6239af9b983SAndrew Thompson 		printf(" [bridge]");
6249af9b983SAndrew Thompson 		break;
625fda82fc2SJulian Elischer 	default:
626123b2d4aSMurray Stokely 		break;
627fda82fc2SJulian Elischer         }
628fda82fc2SJulian Elischer 
629dea673e9SRodney W. Grimes 	printf("\n");
630fda82fc2SJulian Elischer 
631dea673e9SRodney W. Grimes }
6328dc4b495SJulian Elischer 
6338dc4b495SJulian Elischer /*
6348dc4b495SJulian Elischer  * Nuke an arp entry
6358dc4b495SJulian Elischer  */
636bdf932aeSLuigi Rizzo static void
6379d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused,
6389d34414bSMike Heffner 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
6398dc4b495SJulian Elischer {
6408dc4b495SJulian Elischer 	char ip[20];
6418dc4b495SJulian Elischer 
6429d34414bSMike Heffner 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
643f3f8b226SRuslan Ermilov 	(void)delete(ip, 0);
644dea673e9SRodney W. Grimes }
645dea673e9SRodney W. Grimes 
646bdf932aeSLuigi Rizzo static void
647a42a667dSPoul-Henning Kamp usage(void)
648dea673e9SRodney W. Grimes {
6498dc4b495SJulian Elischer 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
650b9de94e9SYaroslav Tykhiy 		"usage: arp [-n] [-i interface] hostname",
651b9de94e9SYaroslav Tykhiy 		"       arp [-n] [-i interface] -a",
6523f844a22SRuslan Ermilov 		"       arp -d hostname [pub]",
653582fa422SBrooks Davis 		"       arp -d [-i interface] -a",
6542293dac2STom Rhodes 		"       arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
6552293dac2STom Rhodes 		"       arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
656c72049e4SPhilippe Charnier 		"       arp -f filename");
657dea673e9SRodney W. Grimes 	exit(1);
658dea673e9SRodney W. Grimes }
659dea673e9SRodney W. Grimes 
660bdf932aeSLuigi Rizzo static struct rt_msghdr *
66168839124SLuigi Rizzo rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl)
662dea673e9SRodney W. Grimes {
663dea673e9SRodney W. Grimes 	static int seq;
664dea673e9SRodney W. Grimes 	int rlen;
665bdf932aeSLuigi Rizzo 	int l;
6660c80179cSSam Leffler 	struct sockaddr_in so_mask, *som = &so_mask;
667bdf932aeSLuigi Rizzo 	static int s = -1;
668bdf932aeSLuigi Rizzo 	static pid_t pid;
669bdf932aeSLuigi Rizzo 
670bdf932aeSLuigi Rizzo 	static struct	{
671bdf932aeSLuigi Rizzo 		struct	rt_msghdr m_rtm;
672bdf932aeSLuigi Rizzo 		char	m_space[512];
673bdf932aeSLuigi Rizzo 	}	m_rtmsg;
674bdf932aeSLuigi Rizzo 
675e2416749SMaxime Henrion 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
676e2416749SMaxime Henrion 	char *cp = m_rtmsg.m_space;
677bdf932aeSLuigi Rizzo 
678bdf932aeSLuigi Rizzo 	if (s < 0) {	/* first time: open socket, get pid */
679bdf932aeSLuigi Rizzo 		s = socket(PF_ROUTE, SOCK_RAW, 0);
680bdf932aeSLuigi Rizzo 		if (s < 0)
681bdf932aeSLuigi Rizzo 			err(1, "socket");
682bdf932aeSLuigi Rizzo 		pid = getpid();
683bdf932aeSLuigi Rizzo 	}
684bdf932aeSLuigi Rizzo 	bzero(&so_mask, sizeof(so_mask));
685bdf932aeSLuigi Rizzo 	so_mask.sin_len = 8;
686bdf932aeSLuigi Rizzo 	so_mask.sin_addr.s_addr = 0xffffffff;
687dea673e9SRodney W. Grimes 
688dea673e9SRodney W. Grimes 	errno = 0;
68968839124SLuigi Rizzo 	/*
69068839124SLuigi Rizzo 	 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
69168839124SLuigi Rizzo 	 * appropriately.
69268839124SLuigi Rizzo 	 */
693dea673e9SRodney W. Grimes 	if (cmd == RTM_DELETE)
694dea673e9SRodney W. Grimes 		goto doit;
695dea673e9SRodney W. Grimes 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
696dea673e9SRodney W. Grimes 	rtm->rtm_flags = flags;
697dea673e9SRodney W. Grimes 	rtm->rtm_version = RTM_VERSION;
698dea673e9SRodney W. Grimes 
699dea673e9SRodney W. Grimes 	switch (cmd) {
700dea673e9SRodney W. Grimes 	default:
701c72049e4SPhilippe Charnier 		errx(1, "internal wrong cmd");
702dea673e9SRodney W. Grimes 	case RTM_ADD:
703dea673e9SRodney W. Grimes 		rtm->rtm_addrs |= RTA_GATEWAY;
704dea673e9SRodney W. Grimes 		rtm->rtm_rmx.rmx_expire = expire_time;
705dea673e9SRodney W. Grimes 		rtm->rtm_inits = RTV_EXPIRE;
7068eca593cSQing Li 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
70768839124SLuigi Rizzo 		dst->sin_other = 0;
708dea673e9SRodney W. Grimes 		if (doing_proxy) {
7093f844a22SRuslan Ermilov 			if (proxy_only)
71068839124SLuigi Rizzo 				dst->sin_other = SIN_PROXY;
711dea673e9SRodney W. Grimes 			else {
712dea673e9SRodney W. Grimes 				rtm->rtm_addrs |= RTA_NETMASK;
713dea673e9SRodney W. Grimes 				rtm->rtm_flags &= ~RTF_HOST;
714dea673e9SRodney W. Grimes 			}
715dea673e9SRodney W. Grimes 		}
716dea673e9SRodney W. Grimes 		/* FALLTHROUGH */
717dea673e9SRodney W. Grimes 	case RTM_GET:
718dea673e9SRodney W. Grimes 		rtm->rtm_addrs |= RTA_DST;
719dea673e9SRodney W. Grimes 	}
720dea673e9SRodney W. Grimes #define NEXTADDR(w, s)					   \
721be5d11dcSDag-Erling Smørgrav 	do {						   \
722f3f8b226SRuslan Ermilov 		if ((s) != NULL && rtm->rtm_addrs & (w)) { \
723be5d11dcSDag-Erling Smørgrav 			bcopy((s), cp, sizeof(*(s)));	   \
724be5d11dcSDag-Erling Smørgrav 			cp += SA_SIZE(s);		   \
725be5d11dcSDag-Erling Smørgrav 		}					   \
726be5d11dcSDag-Erling Smørgrav 	} while (0)
727dea673e9SRodney W. Grimes 
72868839124SLuigi Rizzo 	NEXTADDR(RTA_DST, dst);
72968839124SLuigi Rizzo 	NEXTADDR(RTA_GATEWAY, sdl);
7300c80179cSSam Leffler 	NEXTADDR(RTA_NETMASK, som);
731dea673e9SRodney W. Grimes 
732dea673e9SRodney W. Grimes 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
733dea673e9SRodney W. Grimes doit:
734dea673e9SRodney W. Grimes 	l = rtm->rtm_msglen;
735dea673e9SRodney W. Grimes 	rtm->rtm_seq = ++seq;
736dea673e9SRodney W. Grimes 	rtm->rtm_type = cmd;
737dea673e9SRodney W. Grimes 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
738dea673e9SRodney W. Grimes 		if (errno != ESRCH || cmd != RTM_DELETE) {
739c72049e4SPhilippe Charnier 			warn("writing to routing socket");
740f3f8b226SRuslan Ermilov 			return (NULL);
741dea673e9SRodney W. Grimes 		}
742dea673e9SRodney W. Grimes 	}
743dea673e9SRodney W. Grimes 	do {
744dea673e9SRodney W. Grimes 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
745dea673e9SRodney W. Grimes 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
746dea673e9SRodney W. Grimes 	if (l < 0)
747c72049e4SPhilippe Charnier 		warn("read from routing socket");
748f3f8b226SRuslan Ermilov 	return (rtm);
749dea673e9SRodney W. Grimes }
750dea673e9SRodney W. Grimes 
751a42a667dSPoul-Henning Kamp /*
752a42a667dSPoul-Henning Kamp  * get_ether_addr - get the hardware address of an interface on the
753a42a667dSPoul-Henning Kamp  * the same subnet as ipaddr.
754a42a667dSPoul-Henning Kamp  */
755a42a667dSPoul-Henning Kamp #define MAX_IFS		32
756a42a667dSPoul-Henning Kamp 
757bdf932aeSLuigi Rizzo static int
758f3f8b226SRuslan Ermilov get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr)
759a42a667dSPoul-Henning Kamp {
760a42a667dSPoul-Henning Kamp 	struct ifreq *ifr, *ifend, *ifp;
761f3f8b226SRuslan Ermilov 	in_addr_t ina, mask;
762a42a667dSPoul-Henning Kamp 	struct sockaddr_dl *dla;
763a42a667dSPoul-Henning Kamp 	struct ifreq ifreq;
764a42a667dSPoul-Henning Kamp 	struct ifconf ifc;
765a42a667dSPoul-Henning Kamp 	struct ifreq ifs[MAX_IFS];
7669d34414bSMike Heffner 	int sock;
76768839124SLuigi Rizzo 	int retval = 0;
768a42a667dSPoul-Henning Kamp 
7699d34414bSMike Heffner 	sock = socket(AF_INET, SOCK_DGRAM, 0);
7709d34414bSMike Heffner 	if (sock < 0)
771c72049e4SPhilippe Charnier 		err(1, "socket");
772a42a667dSPoul-Henning Kamp 
773a42a667dSPoul-Henning Kamp 	ifc.ifc_len = sizeof(ifs);
774a42a667dSPoul-Henning Kamp 	ifc.ifc_req = ifs;
7759cc7cb58SMike Heffner 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
776c72049e4SPhilippe Charnier 		warnx("ioctl(SIOCGIFCONF)");
77768839124SLuigi Rizzo 		goto done;
778a42a667dSPoul-Henning Kamp 	}
779a42a667dSPoul-Henning Kamp 
78068839124SLuigi Rizzo #define NEXTIFR(i)						\
78168839124SLuigi Rizzo     ((struct ifreq *)((char *)&(i)->ifr_addr			\
78268839124SLuigi Rizzo 	+ MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) )
78368839124SLuigi Rizzo 
784a42a667dSPoul-Henning Kamp 	/*
785a42a667dSPoul-Henning Kamp 	 * Scan through looking for an interface with an Internet
786a42a667dSPoul-Henning Kamp 	 * address on the same subnet as `ipaddr'.
787a42a667dSPoul-Henning Kamp 	 */
788a42a667dSPoul-Henning Kamp 	ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
78968839124SLuigi Rizzo 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) {
79068839124SLuigi Rizzo 		if (ifr->ifr_addr.sa_family != AF_INET)
79168839124SLuigi Rizzo 			continue;
792a42a667dSPoul-Henning Kamp 		strncpy(ifreq.ifr_name, ifr->ifr_name,
793a42a667dSPoul-Henning Kamp 			sizeof(ifreq.ifr_name));
79406274ceeSGleb Smirnoff 		ifreq.ifr_addr = ifr->ifr_addr;
795a42a667dSPoul-Henning Kamp 		/*
796a42a667dSPoul-Henning Kamp 		 * Check that the interface is up,
797a42a667dSPoul-Henning Kamp 		 * and not point-to-point or loopback.
798a42a667dSPoul-Henning Kamp 		 */
7999cc7cb58SMike Heffner 		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
800a42a667dSPoul-Henning Kamp 			continue;
801a42a667dSPoul-Henning Kamp 		if ((ifreq.ifr_flags &
802a42a667dSPoul-Henning Kamp 		     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
803a42a667dSPoul-Henning Kamp 				IFF_LOOPBACK|IFF_NOARP))
804a42a667dSPoul-Henning Kamp 		     != (IFF_UP|IFF_BROADCAST))
80568839124SLuigi Rizzo 			continue;
806a42a667dSPoul-Henning Kamp 		/*
807a42a667dSPoul-Henning Kamp 		 * Get its netmask and check that it's on
808a42a667dSPoul-Henning Kamp 		 * the right subnet.
809a42a667dSPoul-Henning Kamp 		 */
8109cc7cb58SMike Heffner 		if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
811a42a667dSPoul-Henning Kamp 			continue;
812a42a667dSPoul-Henning Kamp 		mask = ((struct sockaddr_in *)
813a42a667dSPoul-Henning Kamp 			&ifreq.ifr_addr)->sin_addr.s_addr;
81468839124SLuigi Rizzo 		ina = ((struct sockaddr_in *)
81568839124SLuigi Rizzo 			&ifr->ifr_addr)->sin_addr.s_addr;
81668839124SLuigi Rizzo 		if ((ipaddr & mask) == (ina & mask))
81768839124SLuigi Rizzo 			break; /* ok, we got it! */
818a42a667dSPoul-Henning Kamp 	}
819a42a667dSPoul-Henning Kamp 
82068839124SLuigi Rizzo 	if (ifr >= ifend)
82168839124SLuigi Rizzo 		goto done;
822a42a667dSPoul-Henning Kamp 
823a42a667dSPoul-Henning Kamp 	/*
824a42a667dSPoul-Henning Kamp 	 * Now scan through again looking for a link-level address
825a42a667dSPoul-Henning Kamp 	 * for this interface.
826a42a667dSPoul-Henning Kamp 	 */
827a42a667dSPoul-Henning Kamp 	ifp = ifr;
82868839124SLuigi Rizzo 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr))
82968839124SLuigi Rizzo 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 &&
83068839124SLuigi Rizzo 		    ifr->ifr_addr.sa_family == AF_LINK)
83168839124SLuigi Rizzo 			break;
83268839124SLuigi Rizzo 	if (ifr >= ifend)
83368839124SLuigi Rizzo 		goto done;
834a42a667dSPoul-Henning Kamp 	/*
835a42a667dSPoul-Henning Kamp 	 * Found the link-level address - copy it out
836a42a667dSPoul-Henning Kamp 	 */
837a42a667dSPoul-Henning Kamp 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
838a42a667dSPoul-Henning Kamp 	memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
839a42a667dSPoul-Henning Kamp 	printf("using interface %s for proxy with address ",
840a42a667dSPoul-Henning Kamp 		ifp->ifr_name);
841a03b1b7cSRuslan Ermilov 	printf("%s\n", ether_ntoa(hwaddr));
84268839124SLuigi Rizzo 	retval = dla->sdl_alen;
84368839124SLuigi Rizzo done:
84468839124SLuigi Rizzo 	close(sock);
845f3f8b226SRuslan Ermilov 	return (retval);
846a42a667dSPoul-Henning Kamp }
847