xref: /dragonfly/usr.sbin/arp/arp.c (revision 984263bc)
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 #ifndef lint
38 static char const copyright[] =
39 "@(#) Copyright (c) 1984, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46 #endif
47 static const char rcsid[] =
48   "$FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $";
49 #endif /* not lint */
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 <strings.h>
84 #include <unistd.h>
85 
86 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
87 	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
88 void print_entry(struct sockaddr_dl *sdl,
89 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
90 void nuke_entry(struct sockaddr_dl *sdl,
91 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
92 int delete(char *host, char *info);
93 void usage(void);
94 int set(int argc, char **argv);
95 int get(char *host);
96 int file(char *name);
97 void getsocket(void);
98 int my_ether_aton(char *a, struct ether_addr *n);
99 int rtmsg(int cmd);
100 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
101 
102 static int pid;
103 static int nflag;	/* no reverse dns lookups */
104 static int aflag;	/* do it for all entries */
105 static int s = -1;
106 
107 struct	sockaddr_in so_mask;
108 struct	sockaddr_inarp blank_sin, sin_m;
109 struct	sockaddr_dl blank_sdl, sdl_m;
110 int	expire_time, flags, doing_proxy, proxy_only, found_entry;
111 struct	{
112 	struct	rt_msghdr m_rtm;
113 	char	m_space[512];
114 }	m_rtmsg;
115 
116 /* which function we're supposed to do */
117 #define F_GET		1
118 #define F_SET		2
119 #define F_FILESET	3
120 #define F_REPLACE	4
121 #define F_DELETE	5
122 
123 #define ROUNDUP(a) \
124 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
125 #define SETFUNC(f)	{ if (func) usage(); func = (f); }
126 
127 int
128 main(int argc, char *argv[])
129 {
130 	int ch, func = 0;
131 	int rtn = 0;
132 
133 	pid = getpid();
134 	while ((ch = getopt(argc, argv, "andfsS")) != -1)
135 		switch((char)ch) {
136 		case 'a':
137 			aflag = 1;
138 			break;
139 		case 'd':
140 			SETFUNC(F_DELETE);
141 			break;
142 		case 'n':
143 			nflag = 1;
144 			break;
145 		case 'S':
146 			SETFUNC(F_REPLACE);
147 			break;
148 		case 's':
149 			SETFUNC(F_SET);
150 			break;
151 		case 'f' :
152 			SETFUNC(F_FILESET);
153 			break;
154 		case '?':
155 		default:
156 			usage();
157 		}
158 	argc -= optind;
159 	argv += optind;
160 
161 	bzero(&so_mask, sizeof(so_mask));
162 	so_mask.sin_len = 8;
163 	so_mask.sin_addr.s_addr = 0xffffffff;
164 	bzero(&blank_sin, sizeof(blank_sin));
165 	blank_sin.sin_len = sizeof(blank_sin);
166 	blank_sin.sin_family = AF_INET;
167 	bzero(&blank_sdl, sizeof(blank_sdl));
168 	blank_sdl.sdl_len = sizeof(blank_sdl);
169 	blank_sdl.sdl_family = AF_LINK;
170 
171 	if (!func)
172 		func = F_GET;
173 	switch (func) {
174 	case F_GET:
175 		if (aflag) {
176 			if (argc != 0)
177 				usage();
178 			search(0, print_entry);
179 		} else {
180 			if (argc != 1)
181 				usage();
182 			get(argv[0]);
183 		}
184 		break;
185 	case F_SET:
186 	case F_REPLACE:
187 		if (argc < 2 || argc > 6)
188 			usage();
189 		if (func == F_REPLACE)
190 			(void) delete(argv[0], NULL);
191 		rtn = set(argc, argv) ? 1 : 0;
192 		break;
193 	case F_DELETE:
194 		if (aflag) {
195 			if (argc != 0)
196 				usage();
197 			search(0, nuke_entry);
198 		} else {
199 			if (argc < 1 || argc > 2)
200 				usage();
201 			rtn = delete(argv[0], argv[1]);
202 		}
203 		break;
204 	case F_FILESET:
205 		if (argc != 1)
206 			usage();
207 		rtn = file(argv[0]);
208 		break;
209 	}
210 
211 	return(rtn);
212 }
213 
214 /*
215  * Process a file to set standard arp entries
216  */
217 int
218 file(char *name)
219 {
220 	FILE *fp;
221 	int i, retval;
222 	char line[100], arg[5][50], *args[5], *p;
223 
224 	if ((fp = fopen(name, "r")) == NULL)
225 		errx(1, "cannot open %s", name);
226 	args[0] = &arg[0][0];
227 	args[1] = &arg[1][0];
228 	args[2] = &arg[2][0];
229 	args[3] = &arg[3][0];
230 	args[4] = &arg[4][0];
231 	retval = 0;
232 	while(fgets(line, 100, fp) != NULL) {
233 		if ((p = strchr(line, '#')) != NULL)
234 			*p = '\0';
235 		for (p = line; isblank(*p); p++);
236 		if (*p == '\n' || *p == '\0')
237 			continue;
238 		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
239 		    arg[2], arg[3], arg[4]);
240 		if (i < 2) {
241 			warnx("bad line: %s", line);
242 			retval = 1;
243 			continue;
244 		}
245 		if (set(i, args))
246 			retval = 1;
247 	}
248 	fclose(fp);
249 	return (retval);
250 }
251 
252 void
253 getsocket(void)
254 {
255 	if (s < 0) {
256 		s = socket(PF_ROUTE, SOCK_RAW, 0);
257 		if (s < 0)
258 			err(1, "socket");
259 	}
260 }
261 
262 /*
263  * Set an individual arp entry
264  */
265 int
266 set(int argc, char **argv)
267 {
268 	struct hostent *hp;
269 	register struct sockaddr_inarp *addr = &sin_m;
270 	register struct sockaddr_dl *sdl;
271 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
272 	struct ether_addr *ea;
273 	char *host = argv[0], *eaddr = argv[1];
274 
275 	getsocket();
276 	argc -= 2;
277 	argv += 2;
278 	sdl_m = blank_sdl;
279 	sin_m = blank_sin;
280 	addr->sin_addr.s_addr = inet_addr(host);
281 	if (addr->sin_addr.s_addr == INADDR_NONE) {
282 		if (!(hp = gethostbyname(host))) {
283 			warnx("%s: %s", host, hstrerror(h_errno));
284 			return (1);
285 		}
286 		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
287 		    sizeof addr->sin_addr);
288 	}
289 	doing_proxy = flags = proxy_only = expire_time = 0;
290 	while (argc-- > 0) {
291 		if (strncmp(argv[0], "temp", 4) == 0) {
292 			struct timeval tv;
293 			gettimeofday(&tv, 0);
294 			expire_time = tv.tv_sec + 20 * 60;
295 		}
296 		else if (strncmp(argv[0], "pub", 3) == 0) {
297 			flags |= RTF_ANNOUNCE;
298 			doing_proxy = 1;
299 			if (argc && strncmp(argv[1], "only", 3) == 0) {
300 				proxy_only = 1;
301 				sin_m.sin_other = SIN_PROXY;
302 				argc--; argv++;
303 			}
304 		} else if (strncmp(argv[0], "trail", 5) == 0) {
305 			printf("%s: Sending trailers is no longer supported\n",
306 				host);
307 		}
308 		argv++;
309 	}
310 	ea = (struct ether_addr *)LLADDR(&sdl_m);
311 	if (doing_proxy && !strcmp(eaddr, "auto")) {
312 		if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
313 			printf("no interface found for %s\n",
314 			       inet_ntoa(addr->sin_addr));
315 			return (1);
316 		}
317 		sdl_m.sdl_alen = ETHER_ADDR_LEN;
318 	} else {
319 		if (my_ether_aton(eaddr, ea) == 0)
320 			sdl_m.sdl_alen = ETHER_ADDR_LEN;
321 	}
322 tryagain:
323 	if (rtmsg(RTM_GET) < 0) {
324 		warn("%s", host);
325 		return (1);
326 	}
327 	addr = (struct sockaddr_inarp *)(rtm + 1);
328 	sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
329 	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
330 		if (sdl->sdl_family == AF_LINK &&
331 		    (rtm->rtm_flags & RTF_LLINFO) &&
332 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
333 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
334 		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
335 			goto overwrite;
336 		}
337 		if (doing_proxy == 0) {
338 			printf("set: can only proxy for %s\n", host);
339 			return (1);
340 		}
341 		if (sin_m.sin_other & SIN_PROXY) {
342 			printf("set: proxy entry exists for non 802 device\n");
343 			return(1);
344 		}
345 		sin_m.sin_other = SIN_PROXY;
346 		proxy_only = 1;
347 		goto tryagain;
348 	}
349 overwrite:
350 	if (sdl->sdl_family != AF_LINK) {
351 		printf("cannot intuit interface index and type for %s\n", host);
352 		return (1);
353 	}
354 	sdl_m.sdl_type = sdl->sdl_type;
355 	sdl_m.sdl_index = sdl->sdl_index;
356 	return (rtmsg(RTM_ADD));
357 }
358 
359 /*
360  * Display an individual arp entry
361  */
362 int
363 get(char *host)
364 {
365 	struct hostent *hp;
366 	struct sockaddr_inarp *addr = &sin_m;
367 
368 	sin_m = blank_sin;
369 	addr->sin_addr.s_addr = inet_addr(host);
370 	if (addr->sin_addr.s_addr == INADDR_NONE) {
371 		if (!(hp = gethostbyname(host)))
372 			errx(1, "%s: %s", host, hstrerror(h_errno));
373 		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
374 		    sizeof addr->sin_addr);
375 	}
376 	search(addr->sin_addr.s_addr, print_entry);
377 	if (found_entry == 0) {
378 		printf("%s (%s) -- no entry\n",
379 		    host, inet_ntoa(addr->sin_addr));
380 		return(1);
381 	}
382 	return(0);
383 }
384 
385 /*
386  * Delete an arp entry
387  */
388 int
389 delete(char *host, char *info)
390 {
391 	struct hostent *hp;
392 	register struct sockaddr_inarp *addr = &sin_m;
393 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
394 	struct sockaddr_dl *sdl;
395 
396 	getsocket();
397 	sin_m = blank_sin;
398 	if (info) {
399 		if (strncmp(info, "pub", 3) == 0)
400 			sin_m.sin_other = SIN_PROXY;
401 		else
402 			usage();
403 	}
404 	addr->sin_addr.s_addr = inet_addr(host);
405 	if (addr->sin_addr.s_addr == INADDR_NONE) {
406 		if (!(hp = gethostbyname(host))) {
407 			warnx("%s: %s", host, hstrerror(h_errno));
408 			return (1);
409 		}
410 		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
411 		    sizeof addr->sin_addr);
412 	}
413 tryagain:
414 	if (rtmsg(RTM_GET) < 0) {
415 		warn("%s", host);
416 		return (1);
417 	}
418 	addr = (struct sockaddr_inarp *)(rtm + 1);
419 	sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
420 	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
421 		if (sdl->sdl_family == AF_LINK &&
422 		    (rtm->rtm_flags & RTF_LLINFO) &&
423 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
424 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
425 		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
426 			goto delete;
427 		}
428 	}
429 	if (sin_m.sin_other & SIN_PROXY) {
430 		fprintf(stderr, "delete: can't locate %s\n",host);
431 		return (1);
432 	} else {
433 		sin_m.sin_other = SIN_PROXY;
434 		goto tryagain;
435 	}
436 delete:
437 	if (sdl->sdl_family != AF_LINK) {
438 		printf("cannot locate %s\n", host);
439 		return (1);
440 	}
441 	if (rtmsg(RTM_DELETE) == 0) {
442 		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
443 		return (0);
444 	}
445 	return (1);
446 }
447 
448 /*
449  * Search the arp table and do some action on matching entries
450  */
451 void
452 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
453 	struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
454 {
455 	int mib[6];
456 	size_t needed;
457 	char *lim, *buf, *next;
458 	struct rt_msghdr *rtm;
459 	struct sockaddr_inarp *sin2;
460 	struct sockaddr_dl *sdl;
461 
462 	mib[0] = CTL_NET;
463 	mib[1] = PF_ROUTE;
464 	mib[2] = 0;
465 	mib[3] = AF_INET;
466 	mib[4] = NET_RT_FLAGS;
467 	mib[5] = RTF_LLINFO;
468 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
469 		errx(1, "route-sysctl-estimate");
470 	if ((buf = malloc(needed)) == NULL)
471 		errx(1, "malloc");
472 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
473 		errx(1, "actual retrieval of routing table");
474 	lim = buf + needed;
475 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
476 		rtm = (struct rt_msghdr *)next;
477 		sin2 = (struct sockaddr_inarp *)(rtm + 1);
478 		(char *)sdl = (char *)sin2 + ROUNDUP(sin2->sin_len);
479 		if (addr) {
480 			if (addr != sin2->sin_addr.s_addr)
481 				continue;
482 			found_entry = 1;
483 		}
484 		(*action)(sdl, sin2, rtm);
485 	}
486 	free(buf);
487 }
488 
489 /*
490  * Display an arp entry
491  */
492 void
493 print_entry(struct sockaddr_dl *sdl,
494 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
495 {
496 	const char *host;
497 	struct hostent *hp;
498 	struct iso88025_sockaddr_dl_data *trld;
499 	char ifname[IF_NAMESIZE];
500 	int seg;
501 
502 	if (nflag == 0)
503 		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
504 		    sizeof addr->sin_addr, AF_INET);
505 	else
506 		hp = 0;
507 	if (hp)
508 		host = hp->h_name;
509 	else {
510 		host = "?";
511 		if (h_errno == TRY_AGAIN)
512 			nflag = 1;
513 	}
514 	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
515 	if (sdl->sdl_alen)
516 		printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
517 	else
518 		printf("(incomplete)");
519 	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
520 		printf(" on %s", ifname);
521 	if (rtm->rtm_rmx.rmx_expire == 0)
522 		printf(" permanent");
523 	if (addr->sin_other & SIN_PROXY)
524 		printf(" published (proxy only)");
525 	if (rtm->rtm_addrs & RTA_NETMASK) {
526 		addr = (struct sockaddr_inarp *)
527 			(ROUNDUP(sdl->sdl_len) + (char *)sdl);
528 		if (addr->sin_addr.s_addr == 0xffffffff)
529 			printf(" published");
530 		if (addr->sin_len != 8)
531 			printf("(weird)");
532 	}
533         switch(sdl->sdl_type) {
534             case IFT_ETHER:
535                 printf(" [ethernet]");
536                 break;
537             case IFT_ISO88025:
538                 printf(" [token-ring]");
539 		trld = SDL_ISO88025(sdl);
540 		if (trld->trld_rcf != 0) {
541 			printf(" rt=%x", ntohs(trld->trld_rcf));
542 			for (seg = 0;
543 			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
544 			     seg++)
545 				printf(":%x", ntohs(*(trld->trld_route[seg])));
546 		}
547                 break;
548 	    case IFT_L2VLAN:
549 		printf(" [vlan]");
550 		break;
551             default:
552         }
553 
554 	printf("\n");
555 
556 }
557 
558 /*
559  * Nuke an arp entry
560  */
561 void
562 nuke_entry(struct sockaddr_dl *sdl __unused,
563 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
564 {
565 	char ip[20];
566 
567 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
568 	delete(ip, NULL);
569 }
570 
571 int
572 my_ether_aton(char *a, struct ether_addr *n)
573 {
574 	struct ether_addr *ea;
575 
576 	if ((ea = ether_aton(a)) == NULL) {
577 		warnx("invalid Ethernet address '%s'", a);
578 		return (1);
579 	}
580 	*n = *ea;
581 	return (0);
582 }
583 
584 void
585 usage(void)
586 {
587 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
588 		"usage: arp [-n] hostname",
589 		"       arp [-n] -a",
590 		"       arp -d hostname [pub]",
591 		"       arp -d -a",
592 		"       arp -s hostname ether_addr [temp] [pub]",
593 		"       arp -S hostname ether_addr [temp] [pub]",
594 		"       arp -f filename");
595 	exit(1);
596 }
597 
598 int
599 rtmsg(int cmd)
600 {
601 	static int seq;
602 	int rlen;
603 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
604 	register char *cp = m_rtmsg.m_space;
605 	register int l;
606 
607 	errno = 0;
608 	if (cmd == RTM_DELETE)
609 		goto doit;
610 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
611 	rtm->rtm_flags = flags;
612 	rtm->rtm_version = RTM_VERSION;
613 
614 	switch (cmd) {
615 	default:
616 		errx(1, "internal wrong cmd");
617 	case RTM_ADD:
618 		rtm->rtm_addrs |= RTA_GATEWAY;
619 		rtm->rtm_rmx.rmx_expire = expire_time;
620 		rtm->rtm_inits = RTV_EXPIRE;
621 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
622 		sin_m.sin_other = 0;
623 		if (doing_proxy) {
624 			if (proxy_only)
625 				sin_m.sin_other = SIN_PROXY;
626 			else {
627 				rtm->rtm_addrs |= RTA_NETMASK;
628 				rtm->rtm_flags &= ~RTF_HOST;
629 			}
630 		}
631 		/* FALLTHROUGH */
632 	case RTM_GET:
633 		rtm->rtm_addrs |= RTA_DST;
634 	}
635 #define NEXTADDR(w, s) \
636 	if (rtm->rtm_addrs & (w)) { \
637 		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
638 
639 	NEXTADDR(RTA_DST, sin_m);
640 	NEXTADDR(RTA_GATEWAY, sdl_m);
641 	NEXTADDR(RTA_NETMASK, so_mask);
642 
643 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
644 doit:
645 	l = rtm->rtm_msglen;
646 	rtm->rtm_seq = ++seq;
647 	rtm->rtm_type = cmd;
648 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
649 		if (errno != ESRCH || cmd != RTM_DELETE) {
650 			warn("writing to routing socket");
651 			return (-1);
652 		}
653 	}
654 	do {
655 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
656 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
657 	if (l < 0)
658 		warn("read from routing socket");
659 	return (0);
660 }
661 
662 /*
663  * get_ether_addr - get the hardware address of an interface on the
664  * the same subnet as ipaddr.
665  */
666 #define MAX_IFS		32
667 
668 int
669 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
670 {
671 	struct ifreq *ifr, *ifend, *ifp;
672 	u_int32_t ina, mask;
673 	struct sockaddr_dl *dla;
674 	struct ifreq ifreq;
675 	struct ifconf ifc;
676 	struct ifreq ifs[MAX_IFS];
677 	int sock;
678 
679 	sock = socket(AF_INET, SOCK_DGRAM, 0);
680 	if (sock < 0)
681 		err(1, "socket");
682 
683 	ifc.ifc_len = sizeof(ifs);
684 	ifc.ifc_req = ifs;
685 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
686 		warnx("ioctl(SIOCGIFCONF)");
687 		close(sock);
688 		return 0;
689 	}
690 
691 	/*
692 	* Scan through looking for an interface with an Internet
693 	* address on the same subnet as `ipaddr'.
694 	*/
695 	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
696 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
697 		if (ifr->ifr_addr.sa_family == AF_INET) {
698 			ina = ((struct sockaddr_in *)
699 				&ifr->ifr_addr)->sin_addr.s_addr;
700 			strncpy(ifreq.ifr_name, ifr->ifr_name,
701 				sizeof(ifreq.ifr_name));
702 			/*
703 			 * Check that the interface is up,
704 			 * and not point-to-point or loopback.
705 			 */
706 			if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
707 				continue;
708 			if ((ifreq.ifr_flags &
709 			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
710 					IFF_LOOPBACK|IFF_NOARP))
711 			     != (IFF_UP|IFF_BROADCAST))
712 				goto nextif;
713 			/*
714 			 * Get its netmask and check that it's on
715 			 * the right subnet.
716 			 */
717 			if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
718 				continue;
719 			mask = ((struct sockaddr_in *)
720 				&ifreq.ifr_addr)->sin_addr.s_addr;
721 			if ((ipaddr & mask) != (ina & mask))
722 				goto nextif;
723 			break;
724 		}
725 nextif:
726 		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
727 		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
728 	}
729 
730 	if (ifr >= ifend) {
731 		close(sock);
732 		return 0;
733 	}
734 
735 	/*
736 	* Now scan through again looking for a link-level address
737 	* for this interface.
738 	*/
739 	ifp = ifr;
740 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
741 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
742 		    && ifr->ifr_addr.sa_family == AF_LINK) {
743 			/*
744 			 * Found the link-level address - copy it out
745 			 */
746 		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
747 			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
748 			close (sock);
749 			printf("using interface %s for proxy with address ",
750 				ifp->ifr_name);
751 			printf("%s\n", ether_ntoa(hwaddr));
752 			return dla->sdl_alen;
753 		}
754 		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
755 		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
756 	}
757 	return 0;
758 }
759