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