xref: /freebsd/usr.sbin/arp/arp.c (revision 596e374dac03cbfa29cc70e81d77bde8c888d0e4)
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);
9368839124SLuigi Rizzo static void 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);
97bdf932aeSLuigi Rizzo static int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
98bdf932aeSLuigi Rizzo 
998dc4b495SJulian Elischer static int nflag;	/* no reverse dns lookups */
100b9de94e9SYaroslav Tykhiy static char *rifname;
101dea673e9SRodney W. Grimes 
102bdf932aeSLuigi Rizzo static int	expire_time, flags, doing_proxy, proxy_only;
10368839124SLuigi Rizzo 
1048dc4b495SJulian Elischer /* which function we're supposed to do */
1058dc4b495SJulian Elischer #define F_GET		1
1068dc4b495SJulian Elischer #define F_SET		2
1078dc4b495SJulian Elischer #define F_FILESET	3
1088dc4b495SJulian Elischer #define F_REPLACE	4
1098dc4b495SJulian Elischer #define F_DELETE	5
1108dc4b495SJulian Elischer 
1118dc4b495SJulian Elischer #define SETFUNC(f)	{ if (func) usage(); func = (f); }
1128dc4b495SJulian Elischer 
113a42a667dSPoul-Henning Kamp int
1143f844a22SRuslan Ermilov main(int argc, char *argv[])
115dea673e9SRodney W. Grimes {
1168dc4b495SJulian Elischer 	int ch, func = 0;
1178dc4b495SJulian Elischer 	int rtn = 0;
118bdf932aeSLuigi Rizzo 	int aflag = 0;	/* do it for all entries */
119dea673e9SRodney W. Grimes 
120b9de94e9SYaroslav Tykhiy 	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
121dea673e9SRodney W. Grimes 		switch((char)ch) {
122dea673e9SRodney W. Grimes 		case 'a':
1238dc4b495SJulian Elischer 			aflag = 1;
1248dc4b495SJulian Elischer 			break;
125dea673e9SRodney W. Grimes 		case 'd':
1268dc4b495SJulian Elischer 			SETFUNC(F_DELETE);
1278dc4b495SJulian Elischer 			break;
128dea673e9SRodney W. Grimes 		case 'n':
129dea673e9SRodney W. Grimes 			nflag = 1;
1308dc4b495SJulian Elischer 			break;
131a42a667dSPoul-Henning Kamp 		case 'S':
1328dc4b495SJulian Elischer 			SETFUNC(F_REPLACE);
1338dc4b495SJulian Elischer 			break;
134dea673e9SRodney W. Grimes 		case 's':
1358dc4b495SJulian Elischer 			SETFUNC(F_SET);
1368dc4b495SJulian Elischer 			break;
13795319e17SJordan K. Hubbard 		case 'f' :
1388dc4b495SJulian Elischer 			SETFUNC(F_FILESET);
1398dc4b495SJulian Elischer 			break;
140b9de94e9SYaroslav Tykhiy 		case 'i':
141b9de94e9SYaroslav Tykhiy 			rifname = optarg;
142b9de94e9SYaroslav Tykhiy 			break;
143dea673e9SRodney W. Grimes 		case '?':
144dea673e9SRodney W. Grimes 		default:
145dea673e9SRodney W. Grimes 			usage();
146dea673e9SRodney W. Grimes 		}
1478dc4b495SJulian Elischer 	argc -= optind;
1488dc4b495SJulian Elischer 	argv += optind;
1498dc4b495SJulian Elischer 
1508dc4b495SJulian Elischer 	if (!func)
1518dc4b495SJulian Elischer 		func = F_GET;
152b9de94e9SYaroslav Tykhiy 	if (rifname) {
153b9de94e9SYaroslav Tykhiy 		if (func != F_GET)
154b9de94e9SYaroslav Tykhiy 			errx(1, "-i not applicable to this operation");
155b9de94e9SYaroslav Tykhiy 		if (if_nametoindex(rifname) == 0) {
156b9de94e9SYaroslav Tykhiy 			if (errno == ENXIO)
157b9de94e9SYaroslav Tykhiy 				errx(1, "interface %s does not exist", rifname);
158b9de94e9SYaroslav Tykhiy 			else
159b9de94e9SYaroslav Tykhiy 				err(1, "if_nametoindex(%s)", rifname);
160b9de94e9SYaroslav Tykhiy 		}
161b9de94e9SYaroslav Tykhiy 	}
1628dc4b495SJulian Elischer 	switch (func) {
1638dc4b495SJulian Elischer 	case F_GET:
1648dc4b495SJulian Elischer 		if (aflag) {
1658dc4b495SJulian Elischer 			if (argc != 0)
166dea673e9SRodney W. Grimes 				usage();
1678dc4b495SJulian Elischer 			search(0, print_entry);
1688dc4b495SJulian Elischer 		} else {
1698dc4b495SJulian Elischer 			if (argc != 1)
1708dc4b495SJulian Elischer 				usage();
1718dc4b495SJulian Elischer 			get(argv[0]);
1728dc4b495SJulian Elischer 		}
1738dc4b495SJulian Elischer 		break;
1748dc4b495SJulian Elischer 	case F_SET:
1758dc4b495SJulian Elischer 	case F_REPLACE:
1763f844a22SRuslan Ermilov 		if (argc < 2 || argc > 6)
1778dc4b495SJulian Elischer 			usage();
1788dc4b495SJulian Elischer 		if (func == F_REPLACE)
17968839124SLuigi Rizzo 			delete(argv[0], 0);
1808dc4b495SJulian Elischer 		rtn = set(argc, argv) ? 1 : 0;
1818dc4b495SJulian Elischer 		break;
1828dc4b495SJulian Elischer 	case F_DELETE:
1838dc4b495SJulian Elischer 		if (aflag) {
1848dc4b495SJulian Elischer 			if (argc != 0)
1858dc4b495SJulian Elischer 				usage();
1868dc4b495SJulian Elischer 			search(0, nuke_entry);
1878dc4b495SJulian Elischer 		} else {
18868839124SLuigi Rizzo 			if (argc == 2 && strncmp(argv[1], "pub", 3) == 0)
18968839124SLuigi Rizzo 				ch = SIN_PROXY;
19068839124SLuigi Rizzo 			else if (argc == 1)
19168839124SLuigi Rizzo 				ch = 0;
19268839124SLuigi Rizzo 			else
1938dc4b495SJulian Elischer 				usage();
19468839124SLuigi Rizzo 			rtn = delete(argv[0], ch);
1958dc4b495SJulian Elischer 		}
1968dc4b495SJulian Elischer 		break;
1978dc4b495SJulian Elischer 	case F_FILESET:
1988dc4b495SJulian Elischer 		if (argc != 1)
1998dc4b495SJulian Elischer 			usage();
2008dc4b495SJulian Elischer 		rtn = file(argv[0]);
2018dc4b495SJulian Elischer 		break;
2028dc4b495SJulian Elischer 	}
2038dc4b495SJulian Elischer 
2048dc4b495SJulian Elischer 	return(rtn);
205dea673e9SRodney W. Grimes }
206dea673e9SRodney W. Grimes 
207dea673e9SRodney W. Grimes /*
208dea673e9SRodney W. Grimes  * Process a file to set standard arp entries
209dea673e9SRodney W. Grimes  */
210bdf932aeSLuigi Rizzo static int
211a42a667dSPoul-Henning Kamp file(char *name)
212dea673e9SRodney W. Grimes {
213dea673e9SRodney W. Grimes 	FILE *fp;
214dea673e9SRodney W. Grimes 	int i, retval;
2153adcd042SRuslan Ermilov 	char line[100], arg[5][50], *args[5], *p;
216dea673e9SRodney W. Grimes 
217c72049e4SPhilippe Charnier 	if ((fp = fopen(name, "r")) == NULL)
218e2416749SMaxime Henrion 		err(1, "cannot open %s", name);
219dea673e9SRodney W. Grimes 	args[0] = &arg[0][0];
220dea673e9SRodney W. Grimes 	args[1] = &arg[1][0];
221dea673e9SRodney W. Grimes 	args[2] = &arg[2][0];
222dea673e9SRodney W. Grimes 	args[3] = &arg[3][0];
223dea673e9SRodney W. Grimes 	args[4] = &arg[4][0];
224dea673e9SRodney W. Grimes 	retval = 0;
225dea673e9SRodney W. Grimes 	while(fgets(line, 100, fp) != NULL) {
2263adcd042SRuslan Ermilov 		if ((p = strchr(line, '#')) != NULL)
2273adcd042SRuslan Ermilov 			*p = '\0';
2283adcd042SRuslan Ermilov 		for (p = line; isblank(*p); p++);
2294bfc3624SRuslan Ermilov 		if (*p == '\n' || *p == '\0')
23044acbc1aSRuslan Ermilov 			continue;
2313adcd042SRuslan Ermilov 		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
232135adb1eSJordan K. Hubbard 		    arg[2], arg[3], arg[4]);
233dea673e9SRodney W. Grimes 		if (i < 2) {
234c72049e4SPhilippe Charnier 			warnx("bad line: %s", line);
235dea673e9SRodney W. Grimes 			retval = 1;
236dea673e9SRodney W. Grimes 			continue;
237dea673e9SRodney W. Grimes 		}
238dea673e9SRodney W. Grimes 		if (set(i, args))
239dea673e9SRodney W. Grimes 			retval = 1;
240dea673e9SRodney W. Grimes 	}
241dea673e9SRodney W. Grimes 	fclose(fp);
242dea673e9SRodney W. Grimes 	return (retval);
243dea673e9SRodney W. Grimes }
244dea673e9SRodney W. Grimes 
245dea673e9SRodney W. Grimes /*
24668839124SLuigi Rizzo  * Given a hostname, fills up a (static) struct sockaddr_inarp with
24768839124SLuigi Rizzo  * the address of the host and returns a pointer to the
24868839124SLuigi Rizzo  * structure.
249dea673e9SRodney W. Grimes  */
25068839124SLuigi Rizzo static struct sockaddr_inarp *
25168839124SLuigi Rizzo getaddr(char *host)
252dea673e9SRodney W. Grimes {
253dea673e9SRodney W. Grimes 	struct hostent *hp;
25468839124SLuigi Rizzo 	static struct sockaddr_inarp reply;
25568839124SLuigi Rizzo 
25668839124SLuigi Rizzo 	bzero(&reply, sizeof(reply));
25768839124SLuigi Rizzo 	reply.sin_len = sizeof(reply);
25868839124SLuigi Rizzo 	reply.sin_family = AF_INET;
25968839124SLuigi Rizzo 	reply.sin_addr.s_addr = inet_addr(host);
26068839124SLuigi Rizzo 	if (reply.sin_addr.s_addr == INADDR_NONE) {
26168839124SLuigi Rizzo 		if (!(hp = gethostbyname(host))) {
26268839124SLuigi Rizzo 			warnx("%s: %s", host, hstrerror(h_errno));
26368839124SLuigi Rizzo 			return NULL;
26468839124SLuigi Rizzo 		}
26568839124SLuigi Rizzo 		bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
26668839124SLuigi Rizzo 			sizeof reply.sin_addr);
26768839124SLuigi Rizzo 	}
26868839124SLuigi Rizzo 	return &reply;
26968839124SLuigi Rizzo }
27068839124SLuigi Rizzo 
27168839124SLuigi Rizzo /*
27268839124SLuigi Rizzo  * returns true if the type is a valid one for ARP
27368839124SLuigi Rizzo  */
27468839124SLuigi Rizzo static int
27568839124SLuigi Rizzo valid_type(int type)
27668839124SLuigi Rizzo {
27768839124SLuigi Rizzo 	switch (type) {
27868839124SLuigi Rizzo 	default:
27968839124SLuigi Rizzo 		return 0;
28068839124SLuigi Rizzo 	case IFT_ETHER:
28168839124SLuigi Rizzo 	case IFT_FDDI:
28268839124SLuigi Rizzo 	case IFT_ISO88023:
28368839124SLuigi Rizzo 	case IFT_ISO88024:
28468839124SLuigi Rizzo 	case IFT_ISO88025:
28568839124SLuigi Rizzo 	case IFT_L2VLAN:
28668839124SLuigi Rizzo 		return 1;
28768839124SLuigi Rizzo 	}
28868839124SLuigi Rizzo }
28968839124SLuigi Rizzo 
29068839124SLuigi Rizzo /*
29168839124SLuigi Rizzo  * Set an individual arp entry
29268839124SLuigi Rizzo  */
29368839124SLuigi Rizzo static int
29468839124SLuigi Rizzo set(int argc, char **argv)
29568839124SLuigi Rizzo {
29668839124SLuigi Rizzo 	struct sockaddr_inarp *addr;
29768839124SLuigi Rizzo 	struct sockaddr_inarp *dst;	/* what are we looking for */
298e2416749SMaxime Henrion 	struct sockaddr_dl *sdl;
299bdf932aeSLuigi Rizzo 	struct rt_msghdr *rtm;
300a03b1b7cSRuslan Ermilov 	struct ether_addr *ea;
301dea673e9SRodney W. Grimes 	char *host = argv[0], *eaddr = argv[1];
30268839124SLuigi Rizzo 	struct	sockaddr_dl sdl_m;
303dea673e9SRodney W. Grimes 
304dea673e9SRodney W. Grimes 	argc -= 2;
305dea673e9SRodney W. Grimes 	argv += 2;
306bdf932aeSLuigi Rizzo 
307bdf932aeSLuigi Rizzo 	bzero(&sdl_m, sizeof(sdl_m));
308bdf932aeSLuigi Rizzo 	sdl_m.sdl_len = sizeof(sdl_m);
309bdf932aeSLuigi Rizzo 	sdl_m.sdl_family = AF_LINK;
310bdf932aeSLuigi Rizzo 
31168839124SLuigi Rizzo 	dst = getaddr(host);
31268839124SLuigi Rizzo 	if (dst == NULL)
313dea673e9SRodney W. Grimes 		return (1);
3143f844a22SRuslan Ermilov 	doing_proxy = flags = proxy_only = expire_time = 0;
315dea673e9SRodney W. Grimes 	while (argc-- > 0) {
316dea673e9SRodney W. Grimes 		if (strncmp(argv[0], "temp", 4) == 0) {
3173f844a22SRuslan Ermilov 			struct timeval tv;
3183f844a22SRuslan Ermilov 			gettimeofday(&tv, 0);
3193f844a22SRuslan Ermilov 			expire_time = tv.tv_sec + 20 * 60;
320dea673e9SRodney W. Grimes 		}
321dea673e9SRodney W. Grimes 		else if (strncmp(argv[0], "pub", 3) == 0) {
322dea673e9SRodney W. Grimes 			flags |= RTF_ANNOUNCE;
3233f844a22SRuslan Ermilov 			doing_proxy = 1;
3243f844a22SRuslan Ermilov 			if (argc && strncmp(argv[1], "only", 3) == 0) {
3253f844a22SRuslan Ermilov 				proxy_only = 1;
32668839124SLuigi Rizzo 				dst->sin_other = SIN_PROXY;
3273f844a22SRuslan Ermilov 				argc--; argv++;
3283f844a22SRuslan Ermilov 			}
329dea673e9SRodney W. Grimes 		} else if (strncmp(argv[0], "trail", 5) == 0) {
33068839124SLuigi Rizzo 			/* XXX deprecated and undocumented feature */
331dea673e9SRodney W. Grimes 			printf("%s: Sending trailers is no longer supported\n",
332dea673e9SRodney W. Grimes 				host);
333dea673e9SRodney W. Grimes 		}
334dea673e9SRodney W. Grimes 		argv++;
335dea673e9SRodney W. Grimes 	}
336a03b1b7cSRuslan Ermilov 	ea = (struct ether_addr *)LLADDR(&sdl_m);
337a42a667dSPoul-Henning Kamp 	if (doing_proxy && !strcmp(eaddr, "auto")) {
33868839124SLuigi Rizzo 		if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
339eec827b0SRuslan Ermilov 			printf("no interface found for %s\n",
34068839124SLuigi Rizzo 			       inet_ntoa(dst->sin_addr));
341a42a667dSPoul-Henning Kamp 			return (1);
342a42a667dSPoul-Henning Kamp 		}
343a03b1b7cSRuslan Ermilov 		sdl_m.sdl_alen = ETHER_ADDR_LEN;
344a42a667dSPoul-Henning Kamp 	} else {
34568839124SLuigi Rizzo 		struct ether_addr *ea1 = ether_aton(eaddr);
34668839124SLuigi Rizzo 
34768839124SLuigi Rizzo 		if (ea1 == NULL)
34868839124SLuigi Rizzo 			warnx("invalid Ethernet address '%s'", eaddr);
34968839124SLuigi Rizzo 		else {
35068839124SLuigi Rizzo 			*ea = *ea1;
351a03b1b7cSRuslan Ermilov 			sdl_m.sdl_alen = ETHER_ADDR_LEN;
352a42a667dSPoul-Henning Kamp 		}
35368839124SLuigi Rizzo 	}
35468839124SLuigi Rizzo 	for (;;) {	/* try at most twice */
35568839124SLuigi Rizzo 		rtm = rtmsg(RTM_GET, dst, &sdl_m);
356bdf932aeSLuigi Rizzo 		if (rtm == NULL) {
357c72049e4SPhilippe Charnier 			warn("%s", host);
358dea673e9SRodney W. Grimes 			return (1);
359dea673e9SRodney W. Grimes 		}
3609d34414bSMike Heffner 		addr = (struct sockaddr_inarp *)(rtm + 1);
3610b46c085SLuigi Rizzo 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
36268839124SLuigi Rizzo 		if (addr->sin_addr.s_addr != dst->sin_addr.s_addr)
36368839124SLuigi Rizzo 			break;
364dea673e9SRodney W. Grimes 		if (sdl->sdl_family == AF_LINK &&
365dea673e9SRodney W. Grimes 		    (rtm->rtm_flags & RTF_LLINFO) &&
36668839124SLuigi Rizzo 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
36768839124SLuigi Rizzo 		    valid_type(sdl->sdl_type) )
36868839124SLuigi Rizzo 			break;
369dea673e9SRodney W. Grimes 		if (doing_proxy == 0) {
370dea673e9SRodney W. Grimes 			printf("set: can only proxy for %s\n", host);
371dea673e9SRodney W. Grimes 			return (1);
372dea673e9SRodney W. Grimes 		}
37368839124SLuigi Rizzo 		if (dst->sin_other & SIN_PROXY) {
374dea673e9SRodney W. Grimes 			printf("set: proxy entry exists for non 802 device\n");
375dea673e9SRodney W. Grimes 			return(1);
376dea673e9SRodney W. Grimes 		}
37768839124SLuigi Rizzo 		dst->sin_other = SIN_PROXY;
3783f844a22SRuslan Ermilov 		proxy_only = 1;
379dea673e9SRodney W. Grimes 	}
38068839124SLuigi Rizzo 
381dea673e9SRodney W. Grimes 	if (sdl->sdl_family != AF_LINK) {
382dea673e9SRodney W. Grimes 		printf("cannot intuit interface index and type for %s\n", host);
383dea673e9SRodney W. Grimes 		return (1);
384dea673e9SRodney W. Grimes 	}
385dea673e9SRodney W. Grimes 	sdl_m.sdl_type = sdl->sdl_type;
386dea673e9SRodney W. Grimes 	sdl_m.sdl_index = sdl->sdl_index;
38768839124SLuigi Rizzo 	return (rtmsg(RTM_ADD, dst, &sdl_m) != NULL);
388dea673e9SRodney W. Grimes }
389dea673e9SRodney W. Grimes 
390dea673e9SRodney W. Grimes /*
391dea673e9SRodney W. Grimes  * Display an individual arp entry
392dea673e9SRodney W. Grimes  */
39368839124SLuigi Rizzo static void
394a42a667dSPoul-Henning Kamp get(char *host)
395dea673e9SRodney W. Grimes {
39668839124SLuigi Rizzo 	struct sockaddr_inarp *addr;
397dea673e9SRodney W. Grimes 
39868839124SLuigi Rizzo 	addr = getaddr(host);
39968839124SLuigi Rizzo 	if (addr == NULL)
40068839124SLuigi Rizzo 		exit(1);
401bdf932aeSLuigi Rizzo 	if (0 == search(addr->sin_addr.s_addr, print_entry)) {
402b9de94e9SYaroslav Tykhiy 		printf("%s (%s) -- no entry",
4039d34414bSMike Heffner 		    host, inet_ntoa(addr->sin_addr));
404b9de94e9SYaroslav Tykhiy 		if (rifname)
405b9de94e9SYaroslav Tykhiy 			printf(" on %s", rifname);
406b9de94e9SYaroslav Tykhiy 		printf("\n");
407dea673e9SRodney W. Grimes 	}
408dea673e9SRodney W. Grimes }
409dea673e9SRodney W. Grimes 
410dea673e9SRodney W. Grimes /*
411dea673e9SRodney W. Grimes  * Delete an arp entry
412dea673e9SRodney W. Grimes  */
413bdf932aeSLuigi Rizzo static int
41468839124SLuigi Rizzo delete(char *host, int do_proxy)
415dea673e9SRodney W. Grimes {
41668839124SLuigi Rizzo 	struct sockaddr_inarp *addr, *dst;
417bdf932aeSLuigi Rizzo 	struct rt_msghdr *rtm;
418dea673e9SRodney W. Grimes 	struct sockaddr_dl *sdl;
419dea673e9SRodney W. Grimes 
42068839124SLuigi Rizzo 	dst = getaddr(host);
42168839124SLuigi Rizzo 	if (dst == NULL)
42268839124SLuigi Rizzo 		return 1;
42368839124SLuigi Rizzo 	dst->sin_other = do_proxy;
42468839124SLuigi Rizzo 	for (;;) {	/* try twice */
42568839124SLuigi Rizzo 		rtm = rtmsg(RTM_GET, dst, NULL);
426bdf932aeSLuigi Rizzo 		if (rtm == NULL) {
427c72049e4SPhilippe Charnier 			warn("%s", host);
428dea673e9SRodney W. Grimes 			return (1);
429dea673e9SRodney W. Grimes 		}
4309d34414bSMike Heffner 		addr = (struct sockaddr_inarp *)(rtm + 1);
4310b46c085SLuigi Rizzo 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
43268839124SLuigi Rizzo 		if (addr->sin_addr.s_addr == dst->sin_addr.s_addr &&
43368839124SLuigi Rizzo 		    sdl->sdl_family == AF_LINK &&
434dea673e9SRodney W. Grimes 		    (rtm->rtm_flags & RTF_LLINFO) &&
43568839124SLuigi Rizzo 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
43668839124SLuigi Rizzo 		    valid_type(sdl->sdl_type) )
43768839124SLuigi Rizzo 			break;	/* found it */
43868839124SLuigi Rizzo 		if (dst->sin_other & SIN_PROXY) {
43968839124SLuigi Rizzo 			fprintf(stderr, "delete: cannot locate %s\n",host);
440dea673e9SRodney W. Grimes 			return (1);
441dea673e9SRodney W. Grimes 		}
44268839124SLuigi Rizzo 		dst->sin_other = SIN_PROXY;
44368839124SLuigi Rizzo 	}
44468839124SLuigi Rizzo 	if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
4459d34414bSMike Heffner 		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
446a42a667dSPoul-Henning Kamp 		return (0);
447a42a667dSPoul-Henning Kamp 	}
448a42a667dSPoul-Henning Kamp 	return (1);
449dea673e9SRodney W. Grimes }
450dea673e9SRodney W. Grimes 
451dea673e9SRodney W. Grimes /*
4528dc4b495SJulian Elischer  * Search the arp table and do some action on matching entries
453dea673e9SRodney W. Grimes  */
454bdf932aeSLuigi Rizzo static int
455bdf932aeSLuigi Rizzo search(u_long addr, action_fn *action)
456dea673e9SRodney W. Grimes {
457dea673e9SRodney W. Grimes 	int mib[6];
458dea673e9SRodney W. Grimes 	size_t needed;
4598dc4b495SJulian Elischer 	char *lim, *buf, *next;
460dea673e9SRodney W. Grimes 	struct rt_msghdr *rtm;
4619d34414bSMike Heffner 	struct sockaddr_inarp *sin2;
462dea673e9SRodney W. Grimes 	struct sockaddr_dl *sdl;
463b9de94e9SYaroslav Tykhiy 	char ifname[IF_NAMESIZE];
464bdf932aeSLuigi Rizzo 	int found_entry = 0;
465dea673e9SRodney W. Grimes 
466dea673e9SRodney W. Grimes 	mib[0] = CTL_NET;
467dea673e9SRodney W. Grimes 	mib[1] = PF_ROUTE;
468dea673e9SRodney W. Grimes 	mib[2] = 0;
469dea673e9SRodney W. Grimes 	mib[3] = AF_INET;
470dea673e9SRodney W. Grimes 	mib[4] = NET_RT_FLAGS;
471dea673e9SRodney W. Grimes 	mib[5] = RTF_LLINFO;
472dea673e9SRodney W. Grimes 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
473e2416749SMaxime Henrion 		err(1, "route-sysctl-estimate");
47468839124SLuigi Rizzo 	if (needed == 0)	/* empty table */
475bdf932aeSLuigi Rizzo 		return 0;
476dea673e9SRodney W. Grimes 	if ((buf = malloc(needed)) == NULL)
477e2416749SMaxime Henrion 		err(1, "malloc");
478dea673e9SRodney W. Grimes 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
479e2416749SMaxime Henrion 		err(1, "actual retrieval of routing table");
480dea673e9SRodney W. Grimes 	lim = buf + needed;
481dea673e9SRodney W. Grimes 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
482dea673e9SRodney W. Grimes 		rtm = (struct rt_msghdr *)next;
4839d34414bSMike Heffner 		sin2 = (struct sockaddr_inarp *)(rtm + 1);
4841a5ff928SStefan Farfeleder 		sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
485b9de94e9SYaroslav Tykhiy 		if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
486b9de94e9SYaroslav Tykhiy 		    strcmp(ifname, rifname))
487b9de94e9SYaroslav Tykhiy 			continue;
488dea673e9SRodney W. Grimes 		if (addr) {
4899d34414bSMike Heffner 			if (addr != sin2->sin_addr.s_addr)
490dea673e9SRodney W. Grimes 				continue;
491dea673e9SRodney W. Grimes 			found_entry = 1;
492dea673e9SRodney W. Grimes 		}
4939d34414bSMike Heffner 		(*action)(sdl, sin2, rtm);
4948dc4b495SJulian Elischer 	}
495ae14be20SYaroslav Tykhiy 	free(buf);
496bdf932aeSLuigi Rizzo 	return found_entry;
4978dc4b495SJulian Elischer }
4988dc4b495SJulian Elischer 
4998dc4b495SJulian Elischer /*
5008dc4b495SJulian Elischer  * Display an arp entry
5018dc4b495SJulian Elischer  */
502bdf932aeSLuigi Rizzo static void
5038dc4b495SJulian Elischer print_entry(struct sockaddr_dl *sdl,
5049d34414bSMike Heffner 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
5058dc4b495SJulian Elischer {
5063f844a22SRuslan Ermilov 	const char *host;
5078dc4b495SJulian Elischer 	struct hostent *hp;
50897fe20b4SKelly Yancey 	struct iso88025_sockaddr_dl_data *trld;
509e87a372bSRuslan Ermilov 	char ifname[IF_NAMESIZE];
510fda82fc2SJulian Elischer 	int seg;
5118dc4b495SJulian Elischer 
512dea673e9SRodney W. Grimes 	if (nflag == 0)
5139d34414bSMike Heffner 		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
5149d34414bSMike Heffner 		    sizeof addr->sin_addr, AF_INET);
515dea673e9SRodney W. Grimes 	else
516dea673e9SRodney W. Grimes 		hp = 0;
517dea673e9SRodney W. Grimes 	if (hp)
518dea673e9SRodney W. Grimes 		host = hp->h_name;
519dea673e9SRodney W. Grimes 	else {
520dea673e9SRodney W. Grimes 		host = "?";
521dea673e9SRodney W. Grimes 		if (h_errno == TRY_AGAIN)
522dea673e9SRodney W. Grimes 			nflag = 1;
523dea673e9SRodney W. Grimes 	}
5249d34414bSMike Heffner 	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
52521816de3SDoug Rabson 	if (sdl->sdl_alen) {
526596e374dSRuslan Ermilov 		if ((sdl->sdl_type == IFT_ETHER ||
527596e374dSRuslan Ermilov 		    sdl->sdl_type == IFT_L2VLAN) &&
52821816de3SDoug Rabson 		    sdl->sdl_alen == ETHER_ADDR_LEN)
529a03b1b7cSRuslan Ermilov 			printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
53021816de3SDoug Rabson 		else {
53121816de3SDoug Rabson 			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
53221816de3SDoug Rabson 
53321816de3SDoug Rabson 			printf("%s", link_ntoa(sdl) + n);
53421816de3SDoug Rabson 		}
53521816de3SDoug Rabson 	} else
536dea673e9SRodney W. Grimes 		printf("(incomplete)");
537e87a372bSRuslan Ermilov 	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
538e87a372bSRuslan Ermilov 		printf(" on %s", ifname);
539dea673e9SRodney W. Grimes 	if (rtm->rtm_rmx.rmx_expire == 0)
540dea673e9SRodney W. Grimes 		printf(" permanent");
5419d34414bSMike Heffner 	if (addr->sin_other & SIN_PROXY)
542dea673e9SRodney W. Grimes 		printf(" published (proxy only)");
543dea673e9SRodney W. Grimes 	if (rtm->rtm_addrs & RTA_NETMASK) {
5449d34414bSMike Heffner 		addr = (struct sockaddr_inarp *)
5450b46c085SLuigi Rizzo 			(SA_SIZE(sdl) + (char *)sdl);
5469d34414bSMike Heffner 		if (addr->sin_addr.s_addr == 0xffffffff)
547dea673e9SRodney W. Grimes 			printf(" published");
5489d34414bSMike Heffner 		if (addr->sin_len != 8)
5493a6a5ebeSRuslan Ermilov 			printf("(weird)");
550dea673e9SRodney W. Grimes 	}
551fda82fc2SJulian Elischer         switch(sdl->sdl_type) {
552fda82fc2SJulian Elischer 	case IFT_ETHER:
553fda82fc2SJulian Elischer                 printf(" [ethernet]");
554fda82fc2SJulian Elischer                 break;
555fda82fc2SJulian Elischer 	case IFT_ISO88025:
556fda82fc2SJulian Elischer                 printf(" [token-ring]");
55797fe20b4SKelly Yancey 		trld = SDL_ISO88025(sdl);
55897fe20b4SKelly Yancey 		if (trld->trld_rcf != 0) {
55997fe20b4SKelly Yancey 			printf(" rt=%x", ntohs(trld->trld_rcf));
56097fe20b4SKelly Yancey 			for (seg = 0;
56197fe20b4SKelly Yancey 			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
56297fe20b4SKelly Yancey 			     seg++)
5632ab778e1SBill Paul 				printf(":%x", ntohs(*(trld->trld_route[seg])));
56497fe20b4SKelly Yancey 		}
565fda82fc2SJulian Elischer                 break;
56604427472SMatthew N. Dodd 	case IFT_FDDI:
56704427472SMatthew N. Dodd                 printf(" [fddi]");
56804427472SMatthew N. Dodd                 break;
56904427472SMatthew N. Dodd 	case IFT_ATM:
57004427472SMatthew N. Dodd                 printf(" [atm]");
57104427472SMatthew N. Dodd                 break;
57288d5b613SYaroslav Tykhiy 	case IFT_L2VLAN:
57388d5b613SYaroslav Tykhiy 		printf(" [vlan]");
57488d5b613SYaroslav Tykhiy 		break;
57521816de3SDoug Rabson 	case IFT_IEEE1394:
57621816de3SDoug Rabson                 printf(" [firewire]");
57721816de3SDoug Rabson                 break;
578fda82fc2SJulian Elischer 	default:
579123b2d4aSMurray Stokely 		break;
580fda82fc2SJulian Elischer         }
581fda82fc2SJulian Elischer 
582dea673e9SRodney W. Grimes 	printf("\n");
583fda82fc2SJulian Elischer 
584dea673e9SRodney W. Grimes }
5858dc4b495SJulian Elischer 
5868dc4b495SJulian Elischer /*
5878dc4b495SJulian Elischer  * Nuke an arp entry
5888dc4b495SJulian Elischer  */
589bdf932aeSLuigi Rizzo static void
5909d34414bSMike Heffner nuke_entry(struct sockaddr_dl *sdl __unused,
5919d34414bSMike Heffner 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
5928dc4b495SJulian Elischer {
5938dc4b495SJulian Elischer 	char ip[20];
5948dc4b495SJulian Elischer 
5959d34414bSMike Heffner 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
59668839124SLuigi Rizzo 	delete(ip, 0);
597dea673e9SRodney W. Grimes }
598dea673e9SRodney W. Grimes 
599bdf932aeSLuigi Rizzo static void
600a42a667dSPoul-Henning Kamp usage(void)
601dea673e9SRodney W. Grimes {
6028dc4b495SJulian Elischer 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
603b9de94e9SYaroslav Tykhiy 		"usage: arp [-n] [-i interface] hostname",
604b9de94e9SYaroslav Tykhiy 		"       arp [-n] [-i interface] -a",
6053f844a22SRuslan Ermilov 		"       arp -d hostname [pub]",
6068dc4b495SJulian Elischer 		"       arp -d -a",
607c72049e4SPhilippe Charnier 		"       arp -s hostname ether_addr [temp] [pub]",
608c72049e4SPhilippe Charnier 		"       arp -S hostname ether_addr [temp] [pub]",
609c72049e4SPhilippe Charnier 		"       arp -f filename");
610dea673e9SRodney W. Grimes 	exit(1);
611dea673e9SRodney W. Grimes }
612dea673e9SRodney W. Grimes 
613bdf932aeSLuigi Rizzo static struct rt_msghdr *
61468839124SLuigi Rizzo rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl)
615dea673e9SRodney W. Grimes {
616dea673e9SRodney W. Grimes 	static int seq;
617dea673e9SRodney W. Grimes 	int rlen;
618bdf932aeSLuigi Rizzo 	int l;
619bdf932aeSLuigi Rizzo 	struct sockaddr_in so_mask;
620bdf932aeSLuigi Rizzo 	static int s = -1;
621bdf932aeSLuigi Rizzo 	static pid_t pid;
622bdf932aeSLuigi Rizzo 
623bdf932aeSLuigi Rizzo 	static struct	{
624bdf932aeSLuigi Rizzo 		struct	rt_msghdr m_rtm;
625bdf932aeSLuigi Rizzo 		char	m_space[512];
626bdf932aeSLuigi Rizzo 	}	m_rtmsg;
627bdf932aeSLuigi Rizzo 
628e2416749SMaxime Henrion 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
629e2416749SMaxime Henrion 	char *cp = m_rtmsg.m_space;
630bdf932aeSLuigi Rizzo 
631bdf932aeSLuigi Rizzo 	if (s < 0) {	/* first time: open socket, get pid */
632bdf932aeSLuigi Rizzo 		s = socket(PF_ROUTE, SOCK_RAW, 0);
633bdf932aeSLuigi Rizzo 		if (s < 0)
634bdf932aeSLuigi Rizzo 			err(1, "socket");
635bdf932aeSLuigi Rizzo 		pid = getpid();
636bdf932aeSLuigi Rizzo 	}
637bdf932aeSLuigi Rizzo 	bzero(&so_mask, sizeof(so_mask));
638bdf932aeSLuigi Rizzo 	so_mask.sin_len = 8;
639bdf932aeSLuigi Rizzo 	so_mask.sin_addr.s_addr = 0xffffffff;
640dea673e9SRodney W. Grimes 
641dea673e9SRodney W. Grimes 	errno = 0;
64268839124SLuigi Rizzo 	/*
64368839124SLuigi Rizzo 	 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
64468839124SLuigi Rizzo 	 * appropriately.
64568839124SLuigi Rizzo 	 */
646dea673e9SRodney W. Grimes 	if (cmd == RTM_DELETE)
647dea673e9SRodney W. Grimes 		goto doit;
648dea673e9SRodney W. Grimes 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
649dea673e9SRodney W. Grimes 	rtm->rtm_flags = flags;
650dea673e9SRodney W. Grimes 	rtm->rtm_version = RTM_VERSION;
651dea673e9SRodney W. Grimes 
652dea673e9SRodney W. Grimes 	switch (cmd) {
653dea673e9SRodney W. Grimes 	default:
654c72049e4SPhilippe Charnier 		errx(1, "internal wrong cmd");
655dea673e9SRodney W. Grimes 	case RTM_ADD:
656dea673e9SRodney W. Grimes 		rtm->rtm_addrs |= RTA_GATEWAY;
657dea673e9SRodney W. Grimes 		rtm->rtm_rmx.rmx_expire = expire_time;
658dea673e9SRodney W. Grimes 		rtm->rtm_inits = RTV_EXPIRE;
659dea673e9SRodney W. Grimes 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
66068839124SLuigi Rizzo 		dst->sin_other = 0;
661dea673e9SRodney W. Grimes 		if (doing_proxy) {
6623f844a22SRuslan Ermilov 			if (proxy_only)
66368839124SLuigi Rizzo 				dst->sin_other = SIN_PROXY;
664dea673e9SRodney W. Grimes 			else {
665dea673e9SRodney W. Grimes 				rtm->rtm_addrs |= RTA_NETMASK;
666dea673e9SRodney W. Grimes 				rtm->rtm_flags &= ~RTF_HOST;
667dea673e9SRodney W. Grimes 			}
668dea673e9SRodney W. Grimes 		}
669dea673e9SRodney W. Grimes 		/* FALLTHROUGH */
670dea673e9SRodney W. Grimes 	case RTM_GET:
671dea673e9SRodney W. Grimes 		rtm->rtm_addrs |= RTA_DST;
672dea673e9SRodney W. Grimes 	}
673dea673e9SRodney W. Grimes #define NEXTADDR(w, s) \
67468839124SLuigi Rizzo 	if (s && rtm->rtm_addrs & (w)) { \
67568839124SLuigi Rizzo 		bcopy(s, cp, sizeof(*s)); cp += SA_SIZE(s);}
676dea673e9SRodney W. Grimes 
67768839124SLuigi Rizzo 	NEXTADDR(RTA_DST, dst);
67868839124SLuigi Rizzo 	NEXTADDR(RTA_GATEWAY, sdl);
67968839124SLuigi Rizzo 	NEXTADDR(RTA_NETMASK, &so_mask);
680dea673e9SRodney W. Grimes 
681dea673e9SRodney W. Grimes 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
682dea673e9SRodney W. Grimes doit:
683dea673e9SRodney W. Grimes 	l = rtm->rtm_msglen;
684dea673e9SRodney W. Grimes 	rtm->rtm_seq = ++seq;
685dea673e9SRodney W. Grimes 	rtm->rtm_type = cmd;
686dea673e9SRodney W. Grimes 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
687dea673e9SRodney W. Grimes 		if (errno != ESRCH || cmd != RTM_DELETE) {
688c72049e4SPhilippe Charnier 			warn("writing to routing socket");
689bdf932aeSLuigi Rizzo 			return NULL;
690dea673e9SRodney W. Grimes 		}
691dea673e9SRodney W. Grimes 	}
692dea673e9SRodney W. Grimes 	do {
693dea673e9SRodney W. Grimes 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
694dea673e9SRodney W. Grimes 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
695dea673e9SRodney W. Grimes 	if (l < 0)
696c72049e4SPhilippe Charnier 		warn("read from routing socket");
697bdf932aeSLuigi Rizzo 	return rtm;
698dea673e9SRodney W. Grimes }
699dea673e9SRodney W. Grimes 
700a42a667dSPoul-Henning Kamp /*
701a42a667dSPoul-Henning Kamp  * get_ether_addr - get the hardware address of an interface on the
702a42a667dSPoul-Henning Kamp  * the same subnet as ipaddr.
703a42a667dSPoul-Henning Kamp  */
704a42a667dSPoul-Henning Kamp #define MAX_IFS		32
705a42a667dSPoul-Henning Kamp 
706bdf932aeSLuigi Rizzo static int
707a03b1b7cSRuslan Ermilov get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
708a42a667dSPoul-Henning Kamp {
709a42a667dSPoul-Henning Kamp 	struct ifreq *ifr, *ifend, *ifp;
71068839124SLuigi Rizzo 	uint32_t ina, mask;
711a42a667dSPoul-Henning Kamp 	struct sockaddr_dl *dla;
712a42a667dSPoul-Henning Kamp 	struct ifreq ifreq;
713a42a667dSPoul-Henning Kamp 	struct ifconf ifc;
714a42a667dSPoul-Henning Kamp 	struct ifreq ifs[MAX_IFS];
7159d34414bSMike Heffner 	int sock;
71668839124SLuigi Rizzo 	int retval = 0;
717a42a667dSPoul-Henning Kamp 
7189d34414bSMike Heffner 	sock = socket(AF_INET, SOCK_DGRAM, 0);
7199d34414bSMike Heffner 	if (sock < 0)
720c72049e4SPhilippe Charnier 		err(1, "socket");
721a42a667dSPoul-Henning Kamp 
722a42a667dSPoul-Henning Kamp 	ifc.ifc_len = sizeof(ifs);
723a42a667dSPoul-Henning Kamp 	ifc.ifc_req = ifs;
7249cc7cb58SMike Heffner 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
725c72049e4SPhilippe Charnier 		warnx("ioctl(SIOCGIFCONF)");
72668839124SLuigi Rizzo 		goto done;
727a42a667dSPoul-Henning Kamp 	}
728a42a667dSPoul-Henning Kamp 
72968839124SLuigi Rizzo #define NEXTIFR(i)						\
73068839124SLuigi Rizzo     ((struct ifreq *)((char *)&(i)->ifr_addr			\
73168839124SLuigi Rizzo 	+ MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) )
73268839124SLuigi Rizzo 
733a42a667dSPoul-Henning Kamp 	/*
734a42a667dSPoul-Henning Kamp 	 * Scan through looking for an interface with an Internet
735a42a667dSPoul-Henning Kamp 	 * address on the same subnet as `ipaddr'.
736a42a667dSPoul-Henning Kamp 	 */
737a42a667dSPoul-Henning Kamp 	ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
73868839124SLuigi Rizzo 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) {
73968839124SLuigi Rizzo 		if (ifr->ifr_addr.sa_family != AF_INET)
74068839124SLuigi Rizzo 			continue;
74168839124SLuigi Rizzo 		/* XXX can't we use *ifr instead of ifreq ? */
742a42a667dSPoul-Henning Kamp 		strncpy(ifreq.ifr_name, ifr->ifr_name,
743a42a667dSPoul-Henning Kamp 			sizeof(ifreq.ifr_name));
744a42a667dSPoul-Henning Kamp 		/*
745a42a667dSPoul-Henning Kamp 		 * Check that the interface is up,
746a42a667dSPoul-Henning Kamp 		 * and not point-to-point or loopback.
747a42a667dSPoul-Henning Kamp 		 */
7489cc7cb58SMike Heffner 		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
749a42a667dSPoul-Henning Kamp 			continue;
750a42a667dSPoul-Henning Kamp 		if ((ifreq.ifr_flags &
751a42a667dSPoul-Henning Kamp 		     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
752a42a667dSPoul-Henning Kamp 				IFF_LOOPBACK|IFF_NOARP))
753a42a667dSPoul-Henning Kamp 		     != (IFF_UP|IFF_BROADCAST))
75468839124SLuigi Rizzo 			continue;
755a42a667dSPoul-Henning Kamp 		/*
756a42a667dSPoul-Henning Kamp 		 * Get its netmask and check that it's on
757a42a667dSPoul-Henning Kamp 		 * the right subnet.
758a42a667dSPoul-Henning Kamp 		 */
7599cc7cb58SMike Heffner 		if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
760a42a667dSPoul-Henning Kamp 			continue;
761a42a667dSPoul-Henning Kamp 		mask = ((struct sockaddr_in *)
762a42a667dSPoul-Henning Kamp 			&ifreq.ifr_addr)->sin_addr.s_addr;
76368839124SLuigi Rizzo 		ina = ((struct sockaddr_in *)
76468839124SLuigi Rizzo 			&ifr->ifr_addr)->sin_addr.s_addr;
76568839124SLuigi Rizzo 		if ((ipaddr & mask) == (ina & mask))
76668839124SLuigi Rizzo 			break; /* ok, we got it! */
767a42a667dSPoul-Henning Kamp 	}
768a42a667dSPoul-Henning Kamp 
76968839124SLuigi Rizzo 	if (ifr >= ifend)
77068839124SLuigi Rizzo 		goto done;
771a42a667dSPoul-Henning Kamp 
772a42a667dSPoul-Henning Kamp 	/*
773a42a667dSPoul-Henning Kamp 	 * Now scan through again looking for a link-level address
774a42a667dSPoul-Henning Kamp 	 * for this interface.
775a42a667dSPoul-Henning Kamp 	 */
776a42a667dSPoul-Henning Kamp 	ifp = ifr;
77768839124SLuigi Rizzo 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr))
77868839124SLuigi Rizzo 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 &&
77968839124SLuigi Rizzo 		    ifr->ifr_addr.sa_family == AF_LINK)
78068839124SLuigi Rizzo 			break;
78168839124SLuigi Rizzo 	if (ifr >= ifend)
78268839124SLuigi Rizzo 		goto done;
783a42a667dSPoul-Henning Kamp 	/*
784a42a667dSPoul-Henning Kamp 	 * Found the link-level address - copy it out
785a42a667dSPoul-Henning Kamp 	 */
786a42a667dSPoul-Henning Kamp 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
787a42a667dSPoul-Henning Kamp 	memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
788a42a667dSPoul-Henning Kamp 	printf("using interface %s for proxy with address ",
789a42a667dSPoul-Henning Kamp 		ifp->ifr_name);
790a03b1b7cSRuslan Ermilov 	printf("%s\n", ether_ntoa(hwaddr));
79168839124SLuigi Rizzo 	retval = dla->sdl_alen;
79268839124SLuigi Rizzo done:
79368839124SLuigi Rizzo 	close(sock);
79468839124SLuigi Rizzo 	return retval;
795a42a667dSPoul-Henning Kamp }
796