arp.c revision 85123
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
38static 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
45static char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46#endif
47static const char rcsid[] =
48  "$FreeBSD: head/usr.sbin/arp/arp.c 85123 2001-10-19 00:33:26Z mdodd $";
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
84void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
85	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
86void print_entry(struct sockaddr_dl *sdl,
87	struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
88void nuke_entry(struct sockaddr_dl *sdl,
89	struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
90int delete(char *host, char *info);
91void ether_print(u_char *cp);
92void usage(void);
93int set(int argc, char **argv);
94int get(char *host);
95int file(char *name);
96void getsocket(void);
97int my_ether_aton(char *a, u_char *n);
98int rtmsg(int cmd);
99int get_ether_addr(u_int32_t ipaddr, u_char *hwaddr);
100
101static int pid;
102static int nflag;	/* no reverse dns lookups */
103static int aflag;	/* do it for all entries */
104static 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
117int
118main(int argc, 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 > 6)
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 */
197int
198file(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, "%49s %49s %49s %49s %49s", arg[0], arg[1],
214		    arg[2], 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
227void
228getsocket(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
237struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
238struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
239struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
240int	expire_time, flags, doing_proxy, proxy_only, found_entry;
241struct	{
242	struct	rt_msghdr m_rtm;
243	char	m_space[512];
244}	m_rtmsg;
245
246/*
247 * Set an individual arp entry
248 */
249int
250set(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 == INADDR_NONE) {
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 = proxy_only = expire_time = 0;
274	while (argc-- > 0) {
275		if (strncmp(argv[0], "temp", 4) == 0) {
276			struct timeval tv;
277			gettimeofday(&tv, 0);
278			expire_time = tv.tv_sec + 20 * 60;
279		}
280		else if (strncmp(argv[0], "pub", 3) == 0) {
281			flags |= RTF_ANNOUNCE;
282			doing_proxy = 1;
283			if (argc && strncmp(argv[1], "only", 3) == 0) {
284				proxy_only = 1;
285				sin_m.sin_other = SIN_PROXY;
286				argc--; argv++;
287			}
288		} else if (strncmp(argv[0], "trail", 5) == 0) {
289			printf("%s: Sending trailers is no longer supported\n",
290				host);
291		}
292		argv++;
293	}
294	ea = (u_char *)LLADDR(&sdl_m);
295	if (doing_proxy && !strcmp(eaddr, "auto")) {
296		if (!get_ether_addr(sin->sin_addr.s_addr, ea)) {
297			printf("no interface found for %s\n",
298			       inet_ntoa(sin->sin_addr));
299			return (1);
300		}
301		sdl_m.sdl_alen = 6;
302	} else {
303		if (my_ether_aton(eaddr, ea) == 0)
304			sdl_m.sdl_alen = 6;
305	}
306tryagain:
307	if (rtmsg(RTM_GET) < 0) {
308		warn("%s", host);
309		return (1);
310	}
311	sin = (struct sockaddr_inarp *)(rtm + 1);
312	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
313	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
314		if (sdl->sdl_family == AF_LINK &&
315		    (rtm->rtm_flags & RTF_LLINFO) &&
316		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
317		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
318		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
319			goto overwrite;
320		}
321		if (doing_proxy == 0) {
322			printf("set: can only proxy for %s\n", host);
323			return (1);
324		}
325		if (sin_m.sin_other & SIN_PROXY) {
326			printf("set: proxy entry exists for non 802 device\n");
327			return(1);
328		}
329		sin_m.sin_other = SIN_PROXY;
330		proxy_only = 1;
331		goto tryagain;
332	}
333overwrite:
334	if (sdl->sdl_family != AF_LINK) {
335		printf("cannot intuit interface index and type for %s\n", host);
336		return (1);
337	}
338	sdl_m.sdl_type = sdl->sdl_type;
339	sdl_m.sdl_index = sdl->sdl_index;
340	return (rtmsg(RTM_ADD));
341}
342
343/*
344 * Display an individual arp entry
345 */
346int
347get(char *host)
348{
349	struct hostent *hp;
350	struct sockaddr_inarp *sin = &sin_m;
351
352	sin_m = blank_sin;
353	sin->sin_addr.s_addr = inet_addr(host);
354	if (sin->sin_addr.s_addr == INADDR_NONE) {
355		if (!(hp = gethostbyname(host)))
356			errx(1, "%s: %s", host, hstrerror(h_errno));
357		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
358		    sizeof sin->sin_addr);
359	}
360	search(sin->sin_addr.s_addr, print_entry);
361	if (found_entry == 0) {
362		printf("%s (%s) -- no entry\n",
363		    host, inet_ntoa(sin->sin_addr));
364		return(1);
365	}
366	return(0);
367}
368
369/*
370 * Delete an arp entry
371 */
372int
373delete(char *host, char *info)
374{
375	struct hostent *hp;
376	register struct sockaddr_inarp *sin = &sin_m;
377	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
378	struct sockaddr_dl *sdl;
379
380	getsocket();
381	sin_m = blank_sin;
382	if (info) {
383		if (strncmp(info, "pub", 3) == 0)
384			sin_m.sin_other = SIN_PROXY;
385		else
386			usage();
387	}
388	sin->sin_addr.s_addr = inet_addr(host);
389	if (sin->sin_addr.s_addr == INADDR_NONE) {
390		if (!(hp = gethostbyname(host))) {
391			warnx("%s: %s", host, hstrerror(h_errno));
392			return (1);
393		}
394		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
395		    sizeof sin->sin_addr);
396	}
397tryagain:
398	if (rtmsg(RTM_GET) < 0) {
399		warn("%s", host);
400		return (1);
401	}
402	sin = (struct sockaddr_inarp *)(rtm + 1);
403	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
404	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
405		if (sdl->sdl_family == AF_LINK &&
406		    (rtm->rtm_flags & RTF_LLINFO) &&
407		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
408		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
409		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
410			goto delete;
411		}
412	}
413	if (sin_m.sin_other & SIN_PROXY) {
414		fprintf(stderr, "delete: can't locate %s\n",host);
415		return (1);
416	} else {
417		sin_m.sin_other = SIN_PROXY;
418		goto tryagain;
419	}
420delete:
421	if (sdl->sdl_family != AF_LINK) {
422		printf("cannot locate %s\n", host);
423		return (1);
424	}
425	if (rtmsg(RTM_DELETE) == 0) {
426		printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
427		return (0);
428	}
429	return (1);
430}
431
432/*
433 * Search the arp table and do some action on matching entries
434 */
435void
436search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
437	struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
438{
439	int mib[6];
440	size_t needed;
441	char *lim, *buf, *next;
442	struct rt_msghdr *rtm;
443	struct sockaddr_inarp *sin;
444	struct sockaddr_dl *sdl;
445
446	mib[0] = CTL_NET;
447	mib[1] = PF_ROUTE;
448	mib[2] = 0;
449	mib[3] = AF_INET;
450	mib[4] = NET_RT_FLAGS;
451	mib[5] = RTF_LLINFO;
452	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
453		errx(1, "route-sysctl-estimate");
454	if ((buf = malloc(needed)) == NULL)
455		errx(1, "malloc");
456	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
457		errx(1, "actual retrieval of routing table");
458	lim = buf + needed;
459	for (next = buf; next < lim; next += rtm->rtm_msglen) {
460		rtm = (struct rt_msghdr *)next;
461		sin = (struct sockaddr_inarp *)(rtm + 1);
462		(char *)sdl = (char *)sin + ROUNDUP(sin->sin_len);
463		if (addr) {
464			if (addr != sin->sin_addr.s_addr)
465				continue;
466			found_entry = 1;
467		}
468		(*action)(sdl, sin, rtm);
469	}
470	free(buf);
471}
472
473/*
474 * Display an arp entry
475 */
476void
477print_entry(struct sockaddr_dl *sdl,
478	struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
479{
480	const char *host;
481	struct hostent *hp;
482	char ifname[IF_NAMESIZE];
483	int seg;
484
485	if (nflag == 0)
486		hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
487		    sizeof sin->sin_addr, AF_INET);
488	else
489		hp = 0;
490	if (hp)
491		host = hp->h_name;
492	else {
493		host = "?";
494		if (h_errno == TRY_AGAIN)
495			nflag = 1;
496	}
497	printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
498	if (sdl->sdl_alen)
499		ether_print(LLADDR(sdl));
500	else
501		printf("(incomplete)");
502	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
503		printf(" on %s", ifname);
504	if (rtm->rtm_rmx.rmx_expire == 0)
505		printf(" permanent");
506	if (sin->sin_other & SIN_PROXY)
507		printf(" published (proxy only)");
508	if (rtm->rtm_addrs & RTA_NETMASK) {
509		sin = (struct sockaddr_inarp *)
510			(ROUNDUP(sdl->sdl_len) + (char *)sdl);
511		if (sin->sin_addr.s_addr == 0xffffffff)
512			printf(" published");
513		if (sin->sin_len != 8)
514			printf("(weird)");
515	}
516        switch(sdl->sdl_type) {
517            case IFT_ETHER:
518                printf(" [ethernet]");
519                break;
520            case IFT_ISO88025:
521                printf(" [token-ring]");
522                break;
523            case IFT_FDDI:
524                printf(" [fddi]");
525                break;
526            case IFT_ATM:
527                printf(" [atm]");
528                break;
529	    case IFT_L2VLAN:
530		printf(" [vlan]");
531		break;
532            default:
533        }
534	if (sdl->sdl_rcf != NULL) {
535		printf(" rt=%x", ntohs(sdl->sdl_rcf));
536		for (seg = 0; seg < ((((ntohs(sdl->sdl_rcf) & 0x1f00) >> 8) - 2 ) / 2); seg++)
537			printf(":%x", ntohs(sdl->sdl_route[seg]));
538	}
539
540	printf("\n");
541
542}
543
544/*
545 * Nuke an arp entry
546 */
547void
548nuke_entry(struct sockaddr_dl *sdl,
549	struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
550{
551	char ip[20];
552
553	snprintf(ip, sizeof(ip), "%s", inet_ntoa(sin->sin_addr));
554	delete(ip, NULL);
555}
556
557void
558ether_print(u_char *cp)
559{
560	printf("%02x:%02x:%02x:%02x:%02x:%02x", cp[0], cp[1], cp[2], cp[3],
561						cp[4], cp[5]);
562}
563
564int
565my_ether_aton(char *a, u_char *n)
566{
567	int i, o[6];
568
569	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
570					   &o[3], &o[4], &o[5]);
571	if (i != 6) {
572		warnx("invalid Ethernet address '%s'", a);
573		return (1);
574	}
575	for (i=0; i<6; i++)
576		n[i] = o[i];
577	return (0);
578}
579
580void
581usage(void)
582{
583	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
584		"usage: arp [-n] hostname",
585		"       arp [-n] -a",
586		"       arp -d hostname [pub]",
587		"       arp -d -a",
588		"       arp -s hostname ether_addr [temp] [pub]",
589		"       arp -S hostname ether_addr [temp] [pub]",
590		"       arp -f filename");
591	exit(1);
592}
593
594int
595rtmsg(int cmd)
596{
597	static int seq;
598	int rlen;
599	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
600	register char *cp = m_rtmsg.m_space;
601	register int l;
602
603	errno = 0;
604	if (cmd == RTM_DELETE)
605		goto doit;
606	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
607	rtm->rtm_flags = flags;
608	rtm->rtm_version = RTM_VERSION;
609
610	switch (cmd) {
611	default:
612		errx(1, "internal wrong cmd");
613	case RTM_ADD:
614		rtm->rtm_addrs |= RTA_GATEWAY;
615		rtm->rtm_rmx.rmx_expire = expire_time;
616		rtm->rtm_inits = RTV_EXPIRE;
617		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
618		sin_m.sin_other = 0;
619		if (doing_proxy) {
620			if (proxy_only)
621				sin_m.sin_other = SIN_PROXY;
622			else {
623				rtm->rtm_addrs |= RTA_NETMASK;
624				rtm->rtm_flags &= ~RTF_HOST;
625			}
626		}
627		/* FALLTHROUGH */
628	case RTM_GET:
629		rtm->rtm_addrs |= RTA_DST;
630	}
631#define NEXTADDR(w, s) \
632	if (rtm->rtm_addrs & (w)) { \
633		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
634
635	NEXTADDR(RTA_DST, sin_m);
636	NEXTADDR(RTA_GATEWAY, sdl_m);
637	NEXTADDR(RTA_NETMASK, so_mask);
638
639	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
640doit:
641	l = rtm->rtm_msglen;
642	rtm->rtm_seq = ++seq;
643	rtm->rtm_type = cmd;
644	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
645		if (errno != ESRCH || cmd != RTM_DELETE) {
646			warn("writing to routing socket");
647			return (-1);
648		}
649	}
650	do {
651		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
652	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
653	if (l < 0)
654		warn("read from routing socket");
655	return (0);
656}
657
658/*
659 * get_ether_addr - get the hardware address of an interface on the
660 * the same subnet as ipaddr.
661 */
662#define MAX_IFS		32
663
664int
665get_ether_addr(u_int32_t ipaddr, u_char *hwaddr)
666{
667	struct ifreq *ifr, *ifend, *ifp;
668	u_int32_t ina, mask;
669	struct sockaddr_dl *dla;
670	struct ifreq ifreq;
671	struct ifconf ifc;
672	struct ifreq ifs[MAX_IFS];
673	int s;
674
675	s = socket(AF_INET, SOCK_DGRAM, 0);
676	if (s < 0)
677		err(1, "socket");
678
679	ifc.ifc_len = sizeof(ifs);
680	ifc.ifc_req = ifs;
681	if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
682		warnx("ioctl(SIOCGIFCONF)");
683		close(s);
684		return 0;
685	}
686
687	/*
688	* Scan through looking for an interface with an Internet
689	* address on the same subnet as `ipaddr'.
690	*/
691	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
692	for (ifr = ifc.ifc_req; ifr < ifend; ) {
693		if (ifr->ifr_addr.sa_family == AF_INET) {
694			ina = ((struct sockaddr_in *)
695				&ifr->ifr_addr)->sin_addr.s_addr;
696			strncpy(ifreq.ifr_name, ifr->ifr_name,
697				sizeof(ifreq.ifr_name));
698			/*
699			 * Check that the interface is up,
700			 * and not point-to-point or loopback.
701			 */
702			if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
703				continue;
704			if ((ifreq.ifr_flags &
705			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
706					IFF_LOOPBACK|IFF_NOARP))
707			     != (IFF_UP|IFF_BROADCAST))
708				goto nextif;
709			/*
710			 * Get its netmask and check that it's on
711			 * the right subnet.
712			 */
713			if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
714				continue;
715			mask = ((struct sockaddr_in *)
716				&ifreq.ifr_addr)->sin_addr.s_addr;
717			if ((ipaddr & mask) != (ina & mask))
718				goto nextif;
719			break;
720		}
721nextif:
722		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
723		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
724	}
725
726	if (ifr >= ifend) {
727		close(s);
728		return 0;
729	}
730
731	/*
732	* Now scan through again looking for a link-level address
733	* for this interface.
734	*/
735	ifp = ifr;
736	for (ifr = ifc.ifc_req; ifr < ifend; ) {
737		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
738		    && ifr->ifr_addr.sa_family == AF_LINK) {
739			/*
740			 * Found the link-level address - copy it out
741			 */
742		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
743			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
744			close (s);
745			printf("using interface %s for proxy with address ",
746				ifp->ifr_name);
747			ether_print(hwaddr);
748			printf("\n");
749			return dla->sdl_alen;
750		}
751		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
752		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
753	}
754	return 0;
755}
756