xref: /freebsd/usr.sbin/arp/arp.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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.13 1997/11/13 01:16:57 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 
473 	if (nflag == 0)
474 		hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
475 		    sizeof sin->sin_addr, AF_INET);
476 	else
477 		hp = 0;
478 	if (hp)
479 		host = hp->h_name;
480 	else {
481 		host = "?";
482 		if (h_errno == TRY_AGAIN)
483 			nflag = 1;
484 	}
485 	printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
486 	if (sdl->sdl_alen)
487 		ether_print(LLADDR(sdl));
488 	else
489 		printf("(incomplete)");
490 	if (rtm->rtm_rmx.rmx_expire == 0)
491 		printf(" permanent");
492 	if (sin->sin_other & SIN_PROXY)
493 		printf(" published (proxy only)");
494 	if (rtm->rtm_addrs & RTA_NETMASK) {
495 		sin = (struct sockaddr_inarp *)
496 			(sdl->sdl_len + (char *)sdl);
497 		if (sin->sin_addr.s_addr == 0xffffffff)
498 			printf(" published");
499 		if (sin->sin_len != 8)
500 			printf("(wierd)");
501 	}
502 	printf("\n");
503 }
504 
505 /*
506  * Nuke an arp entry
507  */
508 void
509 nuke_entry(struct sockaddr_dl *sdl,
510 	struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
511 {
512 	char ip[20];
513 
514 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(sin->sin_addr));
515 	delete(ip, NULL);
516 }
517 
518 void
519 ether_print(u_char *cp)
520 {
521 	printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
522 }
523 
524 int
525 my_ether_aton(char *a, u_char *n)
526 {
527 	int i, o[6];
528 
529 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
530 					   &o[3], &o[4], &o[5]);
531 	if (i != 6) {
532 		warnx("invalid Ethernet address '%s'", a);
533 		return (1);
534 	}
535 	for (i=0; i<6; i++)
536 		n[i] = o[i];
537 	return (0);
538 }
539 
540 void
541 usage(void)
542 {
543 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
544 		"usage: arp [-n] hostname",
545 		"       arp [-n] -a",
546 		"       arp -d hostname [proxy]",
547 		"       arp -d -a",
548 		"       arp -s hostname ether_addr [temp] [pub]",
549 		"       arp -S hostname ether_addr [temp] [pub]",
550 		"       arp -f filename");
551 	exit(1);
552 }
553 
554 int
555 rtmsg(int cmd)
556 {
557 	static int seq;
558 	int rlen;
559 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
560 	register char *cp = m_rtmsg.m_space;
561 	register int l;
562 
563 	errno = 0;
564 	if (cmd == RTM_DELETE)
565 		goto doit;
566 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
567 	rtm->rtm_flags = flags;
568 	rtm->rtm_version = RTM_VERSION;
569 
570 	switch (cmd) {
571 	default:
572 		errx(1, "internal wrong cmd");
573 	case RTM_ADD:
574 		rtm->rtm_addrs |= RTA_GATEWAY;
575 		rtm->rtm_rmx.rmx_expire = expire_time;
576 		rtm->rtm_inits = RTV_EXPIRE;
577 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
578 		sin_m.sin_other = 0;
579 		if (doing_proxy) {
580 			if (export_only)
581 				sin_m.sin_other = SIN_PROXY;
582 			else {
583 				rtm->rtm_addrs |= RTA_NETMASK;
584 				rtm->rtm_flags &= ~RTF_HOST;
585 			}
586 		}
587 		/* FALLTHROUGH */
588 	case RTM_GET:
589 		rtm->rtm_addrs |= RTA_DST;
590 	}
591 #define NEXTADDR(w, s) \
592 	if (rtm->rtm_addrs & (w)) { \
593 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
594 
595 	NEXTADDR(RTA_DST, sin_m);
596 	NEXTADDR(RTA_GATEWAY, sdl_m);
597 	NEXTADDR(RTA_NETMASK, so_mask);
598 
599 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
600 doit:
601 	l = rtm->rtm_msglen;
602 	rtm->rtm_seq = ++seq;
603 	rtm->rtm_type = cmd;
604 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
605 		if (errno != ESRCH || cmd != RTM_DELETE) {
606 			warn("writing to routing socket");
607 			return (-1);
608 		}
609 	}
610 	do {
611 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
612 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
613 	if (l < 0)
614 		warn("read from routing socket");
615 	return (0);
616 }
617 
618 /*
619  * get_ether_addr - get the hardware address of an interface on the
620  * the same subnet as ipaddr.
621  */
622 #define MAX_IFS		32
623 
624 int
625 get_ether_addr(u_long ipaddr, u_char *hwaddr)
626 {
627 	struct ifreq *ifr, *ifend, *ifp;
628 	u_long ina, mask;
629 	struct sockaddr_dl *dla;
630 	struct ifreq ifreq;
631 	struct ifconf ifc;
632 	struct ifreq ifs[MAX_IFS];
633 	int s;
634 
635 	s = socket(AF_INET, SOCK_DGRAM, 0);
636 	if (s < 0)
637 		err(1, "socket");
638 
639 	ifc.ifc_len = sizeof(ifs);
640 	ifc.ifc_req = ifs;
641 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
642 		warnx("ioctl(SIOCGIFCONF)");
643 		close(s);
644 		return 0;
645 	}
646 
647 	/*
648 	* Scan through looking for an interface with an Internet
649 	* address on the same subnet as `ipaddr'.
650 	*/
651 	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
652 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
653 		if (ifr->ifr_addr.sa_family == AF_INET) {
654 			ina = ((struct sockaddr_in *)
655 				&ifr->ifr_addr)->sin_addr.s_addr;
656 			strncpy(ifreq.ifr_name, ifr->ifr_name,
657 				sizeof(ifreq.ifr_name));
658 			/*
659 			 * Check that the interface is up,
660 			 * and not point-to-point or loopback.
661 			 */
662 			if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
663 				continue;
664 			if ((ifreq.ifr_flags &
665 			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
666 					IFF_LOOPBACK|IFF_NOARP))
667 			     != (IFF_UP|IFF_BROADCAST))
668 				goto nextif;
669 			/*
670 			 * Get its netmask and check that it's on
671 			 * the right subnet.
672 			 */
673 			if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
674 				continue;
675 			mask = ((struct sockaddr_in *)
676 				&ifreq.ifr_addr)->sin_addr.s_addr;
677 			if ((ipaddr & mask) != (ina & mask))
678 				goto nextif;
679 			break;
680 		}
681 nextif:
682 		ifr = (struct ifreq *)
683 		    ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
684 	}
685 
686 	if (ifr >= ifend) {
687 		close(s);
688 		return 0;
689 	}
690 
691 	/*
692 	* Now scan through again looking for a link-level address
693 	* for this interface.
694 	*/
695 	ifp = ifr;
696 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
697 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
698 		    && ifr->ifr_addr.sa_family == AF_LINK) {
699 			/*
700 			 * Found the link-level address - copy it out
701 			 */
702 		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
703 			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
704 			close (s);
705 			printf("using interface %s for proxy with address ",
706 				ifp->ifr_name);
707 			ether_print(hwaddr);
708 			printf("\n");
709 			return dla->sdl_alen;
710 		}
711 		ifr = (struct ifreq *)
712 			((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
713 	}
714 	return 0;
715 }
716