arp.c revision 94075
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 94075 2002-04-07 12:05:05Z murray $";
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 <string.h>
82#include <strings.h>
83#include <unistd.h>
84
85void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
86	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
87void print_entry(struct sockaddr_dl *sdl,
88	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
89void nuke_entry(struct sockaddr_dl *sdl,
90	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
91int delete(char *host, char *info);
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, struct ether_addr *n);
98int rtmsg(int cmd);
99int get_ether_addr(u_int32_t ipaddr, struct ether_addr *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
106struct	sockaddr_in so_mask;
107struct	sockaddr_inarp blank_sin, sin_m;
108struct	sockaddr_dl blank_sdl, sdl_m;
109int	expire_time, flags, doing_proxy, proxy_only, found_entry;
110struct	{
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
126int
127main(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_len = 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 */
216int
217file(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
246void
247getsocket(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 */
259int
260set(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	struct ether_addr *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 = (struct ether_addr *)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 = ETHER_ADDR_LEN;
312	} else {
313		if (my_ether_aton(eaddr, ea) == 0)
314			sdl_m.sdl_alen = ETHER_ADDR_LEN;
315	}
316tryagain:
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	}
343overwrite:
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 */
356int
357get(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 */
382int
383delete(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	}
407tryagain:
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	}
430delete:
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 */
445void
446search(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 */
486void
487print_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		printf("%s", ether_ntoa((struct ether_addr *)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		break;
544        }
545	if (sdl->sdl_rcf != NULL) {
546		printf(" rt=%x", ntohs(sdl->sdl_rcf));
547		for (seg = 0; seg < ((((ntohs(sdl->sdl_rcf) & 0x1f00) >> 8) - 2 ) / 2); seg++)
548			printf(":%x", ntohs(sdl->sdl_route[seg]));
549	}
550
551	printf("\n");
552
553}
554
555/*
556 * Nuke an arp entry
557 */
558void
559nuke_entry(struct sockaddr_dl *sdl __unused,
560	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
561{
562	char ip[20];
563
564	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
565	delete(ip, NULL);
566}
567
568int
569my_ether_aton(char *a, struct ether_addr *n)
570{
571	struct ether_addr *ea;
572
573	if ((ea = ether_aton(a)) == NULL) {
574		warnx("invalid Ethernet address '%s'", a);
575		return (1);
576	}
577	*n = *ea;
578	return (0);
579}
580
581void
582usage(void)
583{
584	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
585		"usage: arp [-n] hostname",
586		"       arp [-n] -a",
587		"       arp -d hostname [pub]",
588		"       arp -d -a",
589		"       arp -s hostname ether_addr [temp] [pub]",
590		"       arp -S hostname ether_addr [temp] [pub]",
591		"       arp -f filename");
592	exit(1);
593}
594
595int
596rtmsg(int cmd)
597{
598	static int seq;
599	int rlen;
600	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
601	register char *cp = m_rtmsg.m_space;
602	register int l;
603
604	errno = 0;
605	if (cmd == RTM_DELETE)
606		goto doit;
607	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
608	rtm->rtm_flags = flags;
609	rtm->rtm_version = RTM_VERSION;
610
611	switch (cmd) {
612	default:
613		errx(1, "internal wrong cmd");
614	case RTM_ADD:
615		rtm->rtm_addrs |= RTA_GATEWAY;
616		rtm->rtm_rmx.rmx_expire = expire_time;
617		rtm->rtm_inits = RTV_EXPIRE;
618		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
619		sin_m.sin_other = 0;
620		if (doing_proxy) {
621			if (proxy_only)
622				sin_m.sin_other = SIN_PROXY;
623			else {
624				rtm->rtm_addrs |= RTA_NETMASK;
625				rtm->rtm_flags &= ~RTF_HOST;
626			}
627		}
628		/* FALLTHROUGH */
629	case RTM_GET:
630		rtm->rtm_addrs |= RTA_DST;
631	}
632#define NEXTADDR(w, s) \
633	if (rtm->rtm_addrs & (w)) { \
634		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
635
636	NEXTADDR(RTA_DST, sin_m);
637	NEXTADDR(RTA_GATEWAY, sdl_m);
638	NEXTADDR(RTA_NETMASK, so_mask);
639
640	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
641doit:
642	l = rtm->rtm_msglen;
643	rtm->rtm_seq = ++seq;
644	rtm->rtm_type = cmd;
645	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
646		if (errno != ESRCH || cmd != RTM_DELETE) {
647			warn("writing to routing socket");
648			return (-1);
649		}
650	}
651	do {
652		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
653	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
654	if (l < 0)
655		warn("read from routing socket");
656	return (0);
657}
658
659/*
660 * get_ether_addr - get the hardware address of an interface on the
661 * the same subnet as ipaddr.
662 */
663#define MAX_IFS		32
664
665int
666get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
667{
668	struct ifreq *ifr, *ifend, *ifp;
669	u_int32_t ina, mask;
670	struct sockaddr_dl *dla;
671	struct ifreq ifreq;
672	struct ifconf ifc;
673	struct ifreq ifs[MAX_IFS];
674	int sock;
675
676	sock = socket(AF_INET, SOCK_DGRAM, 0);
677	if (sock < 0)
678		err(1, "socket");
679
680	ifc.ifc_len = sizeof(ifs);
681	ifc.ifc_req = ifs;
682	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
683		warnx("ioctl(SIOCGIFCONF)");
684		close(sock);
685		return 0;
686	}
687
688	/*
689	* Scan through looking for an interface with an Internet
690	* address on the same subnet as `ipaddr'.
691	*/
692	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
693	for (ifr = ifc.ifc_req; ifr < ifend; ) {
694		if (ifr->ifr_addr.sa_family == AF_INET) {
695			ina = ((struct sockaddr_in *)
696				&ifr->ifr_addr)->sin_addr.s_addr;
697			strncpy(ifreq.ifr_name, ifr->ifr_name,
698				sizeof(ifreq.ifr_name));
699			/*
700			 * Check that the interface is up,
701			 * and not point-to-point or loopback.
702			 */
703			if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
704				continue;
705			if ((ifreq.ifr_flags &
706			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
707					IFF_LOOPBACK|IFF_NOARP))
708			     != (IFF_UP|IFF_BROADCAST))
709				goto nextif;
710			/*
711			 * Get its netmask and check that it's on
712			 * the right subnet.
713			 */
714			if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
715				continue;
716			mask = ((struct sockaddr_in *)
717				&ifreq.ifr_addr)->sin_addr.s_addr;
718			if ((ipaddr & mask) != (ina & mask))
719				goto nextif;
720			break;
721		}
722nextif:
723		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
724		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
725	}
726
727	if (ifr >= ifend) {
728		close(sock);
729		return 0;
730	}
731
732	/*
733	* Now scan through again looking for a link-level address
734	* for this interface.
735	*/
736	ifp = ifr;
737	for (ifr = ifc.ifc_req; ifr < ifend; ) {
738		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
739		    && ifr->ifr_addr.sa_family == AF_LINK) {
740			/*
741			 * Found the link-level address - copy it out
742			 */
743		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
744			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
745			close (sock);
746			printf("using interface %s for proxy with address ",
747				ifp->ifr_name);
748			printf("%s\n", ether_ntoa(hwaddr));
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