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