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