xref: /freebsd/usr.sbin/arp/arp.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*
2  * Copyright (c) 1984, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Sun Microsystems, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if 0
38 #ifndef lint
39 static char const copyright[] =
40 "@(#) Copyright (c) 1984, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif /* not lint */
43 
44 #ifndef lint
45 static char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46 #endif /* not lint */
47 #endif
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 /*
52  * arp - display, set, and delete arp table entries
53  */
54 
55 
56 #include <sys/param.h>
57 #include <sys/file.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/sysctl.h>
61 #include <sys/ioctl.h>
62 #include <sys/time.h>
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <net/iso88025.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 
73 #include <arpa/inet.h>
74 
75 #include <ctype.h>
76 #include <err.h>
77 #include <errno.h>
78 #include <netdb.h>
79 #include <nlist.h>
80 #include <paths.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <strings.h>
85 #include <unistd.h>
86 
87 typedef void (action_fn)(struct sockaddr_dl *sdl,
88 	struct sockaddr_inarp *s_in, struct rt_msghdr *rtm);
89 
90 static int search(u_long addr, action_fn *action);
91 static action_fn print_entry;
92 static action_fn nuke_entry;
93 
94 static int delete(char *host, int do_proxy);
95 static void usage(void);
96 static int set(int argc, char **argv);
97 static void get(char *host);
98 static int file(char *name);
99 static struct rt_msghdr *rtmsg(int cmd,
100 	struct sockaddr_inarp *dst, struct sockaddr_dl *sdl);
101 static int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
102 
103 static int nflag;	/* no reverse dns lookups */
104 static char *rifname;
105 
106 static int	expire_time, flags, doing_proxy, proxy_only;
107 
108 /* which function we're supposed to do */
109 #define F_GET		1
110 #define F_SET		2
111 #define F_FILESET	3
112 #define F_REPLACE	4
113 #define F_DELETE	5
114 
115 #define SETFUNC(f)	{ if (func) usage(); func = (f); }
116 
117 int
118 main(int argc, char *argv[])
119 {
120 	int ch, func = 0;
121 	int rtn = 0;
122 	int aflag = 0;	/* do it for all entries */
123 
124 	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
125 		switch((char)ch) {
126 		case 'a':
127 			aflag = 1;
128 			break;
129 		case 'd':
130 			SETFUNC(F_DELETE);
131 			break;
132 		case 'n':
133 			nflag = 1;
134 			break;
135 		case 'S':
136 			SETFUNC(F_REPLACE);
137 			break;
138 		case 's':
139 			SETFUNC(F_SET);
140 			break;
141 		case 'f' :
142 			SETFUNC(F_FILESET);
143 			break;
144 		case 'i':
145 			rifname = optarg;
146 			break;
147 		case '?':
148 		default:
149 			usage();
150 		}
151 	argc -= optind;
152 	argv += optind;
153 
154 	if (!func)
155 		func = F_GET;
156 	if (rifname) {
157 		if (func != F_GET)
158 			errx(1, "-i not applicable to this operation");
159 		if (if_nametoindex(rifname) == 0) {
160 			if (errno == ENXIO)
161 				errx(1, "interface %s does not exist", rifname);
162 			else
163 				err(1, "if_nametoindex(%s)", rifname);
164 		}
165 	}
166 	switch (func) {
167 	case F_GET:
168 		if (aflag) {
169 			if (argc != 0)
170 				usage();
171 			search(0, print_entry);
172 		} else {
173 			if (argc != 1)
174 				usage();
175 			get(argv[0]);
176 		}
177 		break;
178 	case F_SET:
179 	case F_REPLACE:
180 		if (argc < 2 || argc > 6)
181 			usage();
182 		if (func == F_REPLACE)
183 			delete(argv[0], 0);
184 		rtn = set(argc, argv) ? 1 : 0;
185 		break;
186 	case F_DELETE:
187 		if (aflag) {
188 			if (argc != 0)
189 				usage();
190 			search(0, nuke_entry);
191 		} else {
192 			if (argc == 2 && strncmp(argv[1], "pub", 3) == 0)
193 				ch = SIN_PROXY;
194 			else if (argc == 1)
195 				ch = 0;
196 			else
197 				usage();
198 			rtn = delete(argv[0], ch);
199 		}
200 		break;
201 	case F_FILESET:
202 		if (argc != 1)
203 			usage();
204 		rtn = file(argv[0]);
205 		break;
206 	}
207 
208 	return(rtn);
209 }
210 
211 /*
212  * Process a file to set standard arp entries
213  */
214 static int
215 file(char *name)
216 {
217 	FILE *fp;
218 	int i, retval;
219 	char line[100], arg[5][50], *args[5], *p;
220 
221 	if ((fp = fopen(name, "r")) == NULL)
222 		err(1, "cannot open %s", name);
223 	args[0] = &arg[0][0];
224 	args[1] = &arg[1][0];
225 	args[2] = &arg[2][0];
226 	args[3] = &arg[3][0];
227 	args[4] = &arg[4][0];
228 	retval = 0;
229 	while(fgets(line, 100, fp) != NULL) {
230 		if ((p = strchr(line, '#')) != NULL)
231 			*p = '\0';
232 		for (p = line; isblank(*p); p++);
233 		if (*p == '\n' || *p == '\0')
234 			continue;
235 		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
236 		    arg[2], arg[3], arg[4]);
237 		if (i < 2) {
238 			warnx("bad line: %s", line);
239 			retval = 1;
240 			continue;
241 		}
242 		if (set(i, args))
243 			retval = 1;
244 	}
245 	fclose(fp);
246 	return (retval);
247 }
248 
249 /*
250  * Given a hostname, fills up a (static) struct sockaddr_inarp with
251  * the address of the host and returns a pointer to the
252  * structure.
253  */
254 static struct sockaddr_inarp *
255 getaddr(char *host)
256 {
257 	struct hostent *hp;
258 	static struct sockaddr_inarp reply;
259 
260 	bzero(&reply, sizeof(reply));
261 	reply.sin_len = sizeof(reply);
262 	reply.sin_family = AF_INET;
263 	reply.sin_addr.s_addr = inet_addr(host);
264 	if (reply.sin_addr.s_addr == INADDR_NONE) {
265 		if (!(hp = gethostbyname(host))) {
266 			warnx("%s: %s", host, hstrerror(h_errno));
267 			return NULL;
268 		}
269 		bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
270 			sizeof reply.sin_addr);
271 	}
272 	return &reply;
273 }
274 
275 /*
276  * returns true if the type is a valid one for ARP
277  */
278 static int
279 valid_type(int type)
280 {
281 	switch (type) {
282 	default:
283 		return 0;
284 	case IFT_ETHER:
285 	case IFT_FDDI:
286 	case IFT_ISO88023:
287 	case IFT_ISO88024:
288 	case IFT_ISO88025:
289 	case IFT_L2VLAN:
290 		return 1;
291 	}
292 }
293 
294 /*
295  * Set an individual arp entry
296  */
297 static int
298 set(int argc, char **argv)
299 {
300 	struct sockaddr_inarp *addr;
301 	struct sockaddr_inarp *dst;	/* what are we looking for */
302 	struct sockaddr_dl *sdl;
303 	struct rt_msghdr *rtm;
304 	struct ether_addr *ea;
305 	char *host = argv[0], *eaddr = argv[1];
306 	struct	sockaddr_dl sdl_m;
307 
308 	argc -= 2;
309 	argv += 2;
310 
311 	bzero(&sdl_m, sizeof(sdl_m));
312 	sdl_m.sdl_len = sizeof(sdl_m);
313 	sdl_m.sdl_family = AF_LINK;
314 
315 	dst = getaddr(host);
316 	if (dst == NULL)
317 		return (1);
318 	doing_proxy = flags = proxy_only = expire_time = 0;
319 	while (argc-- > 0) {
320 		if (strncmp(argv[0], "temp", 4) == 0) {
321 			struct timeval tv;
322 			gettimeofday(&tv, 0);
323 			expire_time = tv.tv_sec + 20 * 60;
324 		}
325 		else if (strncmp(argv[0], "pub", 3) == 0) {
326 			flags |= RTF_ANNOUNCE;
327 			doing_proxy = 1;
328 			if (argc && strncmp(argv[1], "only", 3) == 0) {
329 				proxy_only = 1;
330 				dst->sin_other = SIN_PROXY;
331 				argc--; argv++;
332 			}
333 		} else if (strncmp(argv[0], "trail", 5) == 0) {
334 			/* XXX deprecated and undocumented feature */
335 			printf("%s: Sending trailers is no longer supported\n",
336 				host);
337 		}
338 		argv++;
339 	}
340 	ea = (struct ether_addr *)LLADDR(&sdl_m);
341 	if (doing_proxy && !strcmp(eaddr, "auto")) {
342 		if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
343 			printf("no interface found for %s\n",
344 			       inet_ntoa(dst->sin_addr));
345 			return (1);
346 		}
347 		sdl_m.sdl_alen = ETHER_ADDR_LEN;
348 	} else {
349 		struct ether_addr *ea1 = ether_aton(eaddr);
350 
351 		if (ea1 == NULL)
352 			warnx("invalid Ethernet address '%s'", eaddr);
353 		else {
354 			*ea = *ea1;
355 			sdl_m.sdl_alen = ETHER_ADDR_LEN;
356 		}
357 	}
358 	for (;;) {	/* try at most twice */
359 		rtm = rtmsg(RTM_GET, dst, &sdl_m);
360 		if (rtm == NULL) {
361 			warn("%s", host);
362 			return (1);
363 		}
364 		addr = (struct sockaddr_inarp *)(rtm + 1);
365 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
366 		if (addr->sin_addr.s_addr != dst->sin_addr.s_addr)
367 			break;
368 		if (sdl->sdl_family == AF_LINK &&
369 		    (rtm->rtm_flags & RTF_LLINFO) &&
370 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
371 		    valid_type(sdl->sdl_type) )
372 			break;
373 		if (doing_proxy == 0) {
374 			printf("set: can only proxy for %s\n", host);
375 			return (1);
376 		}
377 		if (dst->sin_other & SIN_PROXY) {
378 			printf("set: proxy entry exists for non 802 device\n");
379 			return(1);
380 		}
381 		dst->sin_other = SIN_PROXY;
382 		proxy_only = 1;
383 	}
384 
385 	if (sdl->sdl_family != AF_LINK) {
386 		printf("cannot intuit interface index and type for %s\n", host);
387 		return (1);
388 	}
389 	sdl_m.sdl_type = sdl->sdl_type;
390 	sdl_m.sdl_index = sdl->sdl_index;
391 	return (rtmsg(RTM_ADD, dst, &sdl_m) != NULL);
392 }
393 
394 /*
395  * Display an individual arp entry
396  */
397 static void
398 get(char *host)
399 {
400 	struct sockaddr_inarp *addr;
401 
402 	addr = getaddr(host);
403 	if (addr == NULL)
404 		exit(1);
405 	if (0 == search(addr->sin_addr.s_addr, print_entry)) {
406 		printf("%s (%s) -- no entry",
407 		    host, inet_ntoa(addr->sin_addr));
408 		if (rifname)
409 			printf(" on %s", rifname);
410 		printf("\n");
411 	}
412 }
413 
414 /*
415  * Delete an arp entry
416  */
417 static int
418 delete(char *host, int do_proxy)
419 {
420 	struct sockaddr_inarp *addr, *dst;
421 	struct rt_msghdr *rtm;
422 	struct sockaddr_dl *sdl;
423 
424 	dst = getaddr(host);
425 	if (dst == NULL)
426 		return 1;
427 	dst->sin_other = do_proxy;
428 	for (;;) {	/* try twice */
429 		rtm = rtmsg(RTM_GET, dst, NULL);
430 		if (rtm == NULL) {
431 			warn("%s", host);
432 			return (1);
433 		}
434 		addr = (struct sockaddr_inarp *)(rtm + 1);
435 		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
436 		if (addr->sin_addr.s_addr == dst->sin_addr.s_addr &&
437 		    sdl->sdl_family == AF_LINK &&
438 		    (rtm->rtm_flags & RTF_LLINFO) &&
439 		    !(rtm->rtm_flags & RTF_GATEWAY) &&
440 		    valid_type(sdl->sdl_type) )
441 			break;	/* found it */
442 		if (dst->sin_other & SIN_PROXY) {
443 			fprintf(stderr, "delete: cannot locate %s\n",host);
444 			return (1);
445 		}
446 		dst->sin_other = SIN_PROXY;
447 	}
448 	if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
449 		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
450 		return (0);
451 	}
452 	return (1);
453 }
454 
455 /*
456  * Search the arp table and do some action on matching entries
457  */
458 static int
459 search(u_long addr, action_fn *action)
460 {
461 	int mib[6];
462 	size_t needed;
463 	char *lim, *buf, *next;
464 	struct rt_msghdr *rtm;
465 	struct sockaddr_inarp *sin2;
466 	struct sockaddr_dl *sdl;
467 	char ifname[IF_NAMESIZE];
468 	int found_entry = 0;
469 
470 	mib[0] = CTL_NET;
471 	mib[1] = PF_ROUTE;
472 	mib[2] = 0;
473 	mib[3] = AF_INET;
474 	mib[4] = NET_RT_FLAGS;
475 	mib[5] = RTF_LLINFO;
476 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
477 		err(1, "route-sysctl-estimate");
478 	if (needed == 0)	/* empty table */
479 		return 0;
480 	if ((buf = malloc(needed)) == NULL)
481 		err(1, "malloc");
482 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
483 		err(1, "actual retrieval of routing table");
484 	lim = buf + needed;
485 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
486 		rtm = (struct rt_msghdr *)next;
487 		sin2 = (struct sockaddr_inarp *)(rtm + 1);
488 		(char *)sdl = (char *)sin2 + SA_SIZE(sin2);
489 		if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
490 		    strcmp(ifname, rifname))
491 			continue;
492 		if (addr) {
493 			if (addr != sin2->sin_addr.s_addr)
494 				continue;
495 			found_entry = 1;
496 		}
497 		(*action)(sdl, sin2, rtm);
498 	}
499 	free(buf);
500 	return found_entry;
501 }
502 
503 /*
504  * Display an arp entry
505  */
506 static void
507 print_entry(struct sockaddr_dl *sdl,
508 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
509 {
510 	const char *host;
511 	struct hostent *hp;
512 	struct iso88025_sockaddr_dl_data *trld;
513 	char ifname[IF_NAMESIZE];
514 	int seg;
515 
516 	if (nflag == 0)
517 		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
518 		    sizeof addr->sin_addr, AF_INET);
519 	else
520 		hp = 0;
521 	if (hp)
522 		host = hp->h_name;
523 	else {
524 		host = "?";
525 		if (h_errno == TRY_AGAIN)
526 			nflag = 1;
527 	}
528 	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
529 	if (sdl->sdl_alen)
530 		printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
531 	else
532 		printf("(incomplete)");
533 	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
534 		printf(" on %s", ifname);
535 	if (rtm->rtm_rmx.rmx_expire == 0)
536 		printf(" permanent");
537 	if (addr->sin_other & SIN_PROXY)
538 		printf(" published (proxy only)");
539 	if (rtm->rtm_addrs & RTA_NETMASK) {
540 		addr = (struct sockaddr_inarp *)
541 			(SA_SIZE(sdl) + (char *)sdl);
542 		if (addr->sin_addr.s_addr == 0xffffffff)
543 			printf(" published");
544 		if (addr->sin_len != 8)
545 			printf("(weird)");
546 	}
547         switch(sdl->sdl_type) {
548 	case IFT_ETHER:
549                 printf(" [ethernet]");
550                 break;
551 	case IFT_ISO88025:
552                 printf(" [token-ring]");
553 		trld = SDL_ISO88025(sdl);
554 		if (trld->trld_rcf != 0) {
555 			printf(" rt=%x", ntohs(trld->trld_rcf));
556 			for (seg = 0;
557 			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
558 			     seg++)
559 				printf(":%x", ntohs(*(trld->trld_route[seg])));
560 		}
561                 break;
562 	case IFT_FDDI:
563                 printf(" [fddi]");
564                 break;
565 	case IFT_ATM:
566                 printf(" [atm]");
567                 break;
568 	case IFT_L2VLAN:
569 		printf(" [vlan]");
570 		break;
571 	default:
572 		break;
573         }
574 
575 	printf("\n");
576 
577 }
578 
579 /*
580  * Nuke an arp entry
581  */
582 static void
583 nuke_entry(struct sockaddr_dl *sdl __unused,
584 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
585 {
586 	char ip[20];
587 
588 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
589 	delete(ip, 0);
590 }
591 
592 static void
593 usage(void)
594 {
595 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
596 		"usage: arp [-n] [-i interface] hostname",
597 		"       arp [-n] [-i interface] -a",
598 		"       arp -d hostname [pub]",
599 		"       arp -d -a",
600 		"       arp -s hostname ether_addr [temp] [pub]",
601 		"       arp -S hostname ether_addr [temp] [pub]",
602 		"       arp -f filename");
603 	exit(1);
604 }
605 
606 static struct rt_msghdr *
607 rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl)
608 {
609 	static int seq;
610 	int rlen;
611 	int l;
612 	struct sockaddr_in so_mask;
613 	static int s = -1;
614 	static pid_t pid;
615 
616 	static struct	{
617 		struct	rt_msghdr m_rtm;
618 		char	m_space[512];
619 	}	m_rtmsg;
620 
621 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
622 	char *cp = m_rtmsg.m_space;
623 
624 	if (s < 0) {	/* first time: open socket, get pid */
625 		s = socket(PF_ROUTE, SOCK_RAW, 0);
626 		if (s < 0)
627 			err(1, "socket");
628 		pid = getpid();
629 	}
630 	bzero(&so_mask, sizeof(so_mask));
631 	so_mask.sin_len = 8;
632 	so_mask.sin_addr.s_addr = 0xffffffff;
633 
634 	errno = 0;
635 	/*
636 	 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
637 	 * appropriately.
638 	 */
639 	if (cmd == RTM_DELETE)
640 		goto doit;
641 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
642 	rtm->rtm_flags = flags;
643 	rtm->rtm_version = RTM_VERSION;
644 
645 	switch (cmd) {
646 	default:
647 		errx(1, "internal wrong cmd");
648 	case RTM_ADD:
649 		rtm->rtm_addrs |= RTA_GATEWAY;
650 		rtm->rtm_rmx.rmx_expire = expire_time;
651 		rtm->rtm_inits = RTV_EXPIRE;
652 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
653 		dst->sin_other = 0;
654 		if (doing_proxy) {
655 			if (proxy_only)
656 				dst->sin_other = SIN_PROXY;
657 			else {
658 				rtm->rtm_addrs |= RTA_NETMASK;
659 				rtm->rtm_flags &= ~RTF_HOST;
660 			}
661 		}
662 		/* FALLTHROUGH */
663 	case RTM_GET:
664 		rtm->rtm_addrs |= RTA_DST;
665 	}
666 #define NEXTADDR(w, s) \
667 	if (s && rtm->rtm_addrs & (w)) { \
668 		bcopy(s, cp, sizeof(*s)); cp += SA_SIZE(s);}
669 
670 	NEXTADDR(RTA_DST, dst);
671 	NEXTADDR(RTA_GATEWAY, sdl);
672 	NEXTADDR(RTA_NETMASK, &so_mask);
673 
674 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
675 doit:
676 	l = rtm->rtm_msglen;
677 	rtm->rtm_seq = ++seq;
678 	rtm->rtm_type = cmd;
679 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
680 		if (errno != ESRCH || cmd != RTM_DELETE) {
681 			warn("writing to routing socket");
682 			return NULL;
683 		}
684 	}
685 	do {
686 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
687 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
688 	if (l < 0)
689 		warn("read from routing socket");
690 	return rtm;
691 }
692 
693 /*
694  * get_ether_addr - get the hardware address of an interface on the
695  * the same subnet as ipaddr.
696  */
697 #define MAX_IFS		32
698 
699 static int
700 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
701 {
702 	struct ifreq *ifr, *ifend, *ifp;
703 	uint32_t ina, mask;
704 	struct sockaddr_dl *dla;
705 	struct ifreq ifreq;
706 	struct ifconf ifc;
707 	struct ifreq ifs[MAX_IFS];
708 	int sock;
709 	int retval = 0;
710 
711 	sock = socket(AF_INET, SOCK_DGRAM, 0);
712 	if (sock < 0)
713 		err(1, "socket");
714 
715 	ifc.ifc_len = sizeof(ifs);
716 	ifc.ifc_req = ifs;
717 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
718 		warnx("ioctl(SIOCGIFCONF)");
719 		goto done;
720 	}
721 
722 #define NEXTIFR(i)						\
723     ((struct ifreq *)((char *)&(i)->ifr_addr			\
724 	+ MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) )
725 
726 	/*
727 	 * Scan through looking for an interface with an Internet
728 	 * address on the same subnet as `ipaddr'.
729 	 */
730 	ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
731 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) {
732 		if (ifr->ifr_addr.sa_family != AF_INET)
733 			continue;
734 		/* XXX can't we use *ifr instead of ifreq ? */
735 		strncpy(ifreq.ifr_name, ifr->ifr_name,
736 			sizeof(ifreq.ifr_name));
737 		/*
738 		 * Check that the interface is up,
739 		 * and not point-to-point or loopback.
740 		 */
741 		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
742 			continue;
743 		if ((ifreq.ifr_flags &
744 		     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
745 				IFF_LOOPBACK|IFF_NOARP))
746 		     != (IFF_UP|IFF_BROADCAST))
747 			continue;
748 		/*
749 		 * Get its netmask and check that it's on
750 		 * the right subnet.
751 		 */
752 		if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
753 			continue;
754 		mask = ((struct sockaddr_in *)
755 			&ifreq.ifr_addr)->sin_addr.s_addr;
756 		ina = ((struct sockaddr_in *)
757 			&ifr->ifr_addr)->sin_addr.s_addr;
758 		if ((ipaddr & mask) == (ina & mask))
759 			break; /* ok, we got it! */
760 	}
761 
762 	if (ifr >= ifend)
763 		goto done;
764 
765 	/*
766 	 * Now scan through again looking for a link-level address
767 	 * for this interface.
768 	 */
769 	ifp = ifr;
770 	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr))
771 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 &&
772 		    ifr->ifr_addr.sa_family == AF_LINK)
773 			break;
774 	if (ifr >= ifend)
775 		goto done;
776 	/*
777 	 * Found the link-level address - copy it out
778 	 */
779 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
780 	memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
781 	printf("using interface %s for proxy with address ",
782 		ifp->ifr_name);
783 	printf("%s\n", ether_ntoa(hwaddr));
784 	retval = dla->sdl_alen;
785 done:
786 	close(sock);
787 	return retval;
788 }
789