xref: /dragonfly/usr.sbin/arp/arp.c (revision f746689a)
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  * @(#) Copyright (c) 1984, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)from: arp.c	8.2 (Berkeley) 1/2/94
38  * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $
39  * $DragonFly: src/usr.sbin/arp/arp.c,v 1.11 2008/02/03 00:41:44 swildner Exp $
40  */
41 
42 /*
43  * arp - display, set, and delete arp table entries
44  */
45 
46 
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/ioctl.h>
53 #include <sys/time.h>
54 
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62 
63 #include <arpa/inet.h>
64 
65 #include <ctype.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <netdb.h>
69 #include <nlist.h>
70 #include <paths.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 
76 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
77 	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
78 void print_entry(struct sockaddr_dl *sdl,
79 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
80 void nuke_entry(struct sockaddr_dl *sdl,
81 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
82 int delete(char *host, char *info);
83 void usage(void);
84 int set(int argc, char **argv);
85 int get(char *host);
86 int file(char *name);
87 void getsocket(void);
88 int my_ether_aton(char *a, struct ether_addr *n);
89 int rtmsg(int cmd);
90 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
91 
92 static int pid;
93 static int nflag;	/* no reverse dns lookups */
94 static int aflag;	/* do it for all entries */
95 static int cpuflag = -1;
96 static int s = -1;
97 
98 struct	sockaddr_in so_mask;
99 struct	sockaddr_inarp blank_sin, sin_m;
100 struct	sockaddr_dl blank_sdl, sdl_m;
101 int	expire_time, flags, doing_proxy, proxy_only, found_entry;
102 struct	{
103 	struct	rt_msghdr m_rtm;
104 	char	m_space[512];
105 }	m_rtmsg;
106 
107 /* which function we're supposed to do */
108 #define F_GET		1
109 #define F_SET		2
110 #define F_FILESET	3
111 #define F_REPLACE	4
112 #define F_DELETE	5
113 
114 #define ROUNDUP(a) \
115 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
116 #define SETFUNC(f)	{ if (func) usage(); func = (f); }
117 
118 int
119 main(int argc, char **argv)
120 {
121 	int ch, func = 0;
122 	int rtn = 0;
123 
124 	pid = getpid();
125 	while ((ch = getopt(argc, argv, "ac:ndfsS")) != -1)
126 		switch((char)ch) {
127 		case 'a':
128 			aflag = 1;
129 			break;
130 		case 'c':
131 			cpuflag = strtol(optarg, NULL, 0);
132 			break;
133 		case 'd':
134 			SETFUNC(F_DELETE);
135 			break;
136 		case 'n':
137 			nflag = 1;
138 			break;
139 		case 'S':
140 			SETFUNC(F_REPLACE);
141 			break;
142 		case 's':
143 			SETFUNC(F_SET);
144 			break;
145 		case 'f' :
146 			SETFUNC(F_FILESET);
147 			break;
148 		case '?':
149 		default:
150 			usage();
151 		}
152 	argc -= optind;
153 	argv += optind;
154 
155 	bzero(&so_mask, sizeof(so_mask));
156 	so_mask.sin_len = 8;
157 	so_mask.sin_addr.s_addr = 0xffffffff;
158 	bzero(&blank_sin, sizeof(blank_sin));
159 	blank_sin.sin_len = sizeof(blank_sin);
160 	blank_sin.sin_family = AF_INET;
161 	bzero(&blank_sdl, sizeof(blank_sdl));
162 	blank_sdl.sdl_len = sizeof(blank_sdl);
163 	blank_sdl.sdl_family = AF_LINK;
164 
165 	if (!func)
166 		func = F_GET;
167 	switch (func) {
168 	case F_GET:
169 		if (aflag) {
170 			if (argc != 0)
171 				usage();
172 			search(0, print_entry);
173 		} else {
174 			if (argc != 1)
175 				usage();
176 			get(argv[0]);
177 		}
178 		break;
179 	case F_SET:
180 	case F_REPLACE:
181 		if (argc < 2 || argc > 6)
182 			usage();
183 		if (func == F_REPLACE)
184 			delete(argv[0], NULL);
185 		rtn = set(argc, argv) ? 1 : 0;
186 		break;
187 	case F_DELETE:
188 		if (aflag) {
189 			if (argc != 0)
190 				usage();
191 			search(0, nuke_entry);
192 		} else {
193 			if (argc < 1 || argc > 2)
194 				usage();
195 			rtn = delete(argv[0], argv[1]);
196 		}
197 		break;
198 	case F_FILESET:
199 		if (argc != 1)
200 			usage();
201 		rtn = file(argv[0]);
202 		break;
203 	}
204 
205 	return(rtn);
206 }
207 
208 /*
209  * Process a file to set standard arp entries
210  */
211 int
212 file(char *name)
213 {
214 	FILE *fp;
215 	int i, retval;
216 	char line[100], arg[5][50], *args[5], *p;
217 
218 	if ((fp = fopen(name, "r")) == NULL)
219 		errx(1, "cannot open %s", name);
220 	args[0] = &arg[0][0];
221 	args[1] = &arg[1][0];
222 	args[2] = &arg[2][0];
223 	args[3] = &arg[3][0];
224 	args[4] = &arg[4][0];
225 	retval = 0;
226 	while(fgets(line, 100, fp) != NULL) {
227 		if ((p = strchr(line, '#')) != NULL)
228 			*p = '\0';
229 		for (p = line; isblank(*p); p++);
230 		if (*p == '\n' || *p == '\0')
231 			continue;
232 		i = sscanf(p, "%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 
246 void
247 getsocket(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  */
259 int
260 set(int argc, char **argv)
261 {
262 	struct hostent *hp;
263 	struct sockaddr_inarp *addr = &sin_m;
264 	struct sockaddr_dl *sdl;
265 	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 	}
316 tryagain:
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 	}
343 overwrite:
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  */
356 int
357 get(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  */
382 int
383 delete(char *host, char *info)
384 {
385 	struct hostent *hp;
386 	struct sockaddr_inarp *addr = &sin_m;
387 	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 	}
407 tryagain:
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 	}
430 delete:
431 	if (sdl->sdl_family != AF_LINK) {
432 		printf("cannot locate %s\n", host);
433 		return(1);
434 	}
435 	if (aflag && (rtm->rtm_flags & RTF_STATIC)) {
436 		sdl->sdl_alen = 0;
437 		sdl->sdl_slen = 0;
438 		if (rtmsg(RTM_CHANGE) == 0) {
439 			printf("%s (%s) cleared\n",
440 				host, inet_ntoa(addr->sin_addr));
441 			return(0);
442 		}
443 	} else {
444 		if (rtmsg(RTM_DELETE) == 0) {
445 			printf("%s (%s) deleted\n",
446 				host, inet_ntoa(addr->sin_addr));
447 			return(0);
448 		}
449 	}
450 	return(1);
451 }
452 
453 /*
454  * Search the arp table and do some action on matching entries
455  */
456 void
457 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
458 	struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
459 {
460 	int mib[7];
461 	int miblen;
462 	size_t needed;
463 	char *lim, *buf, *next;
464 	struct rt_msghdr *rtm;
465 	struct sockaddr_inarp *sin2;
466 	struct sockaddr_dl *sdl;
467 
468 	mib[0] = CTL_NET;
469 	mib[1] = PF_ROUTE;
470 	mib[2] = 0;
471 	mib[3] = AF_INET;
472 	mib[4] = NET_RT_FLAGS;
473 	mib[5] = RTF_LLINFO;
474 	if (cpuflag >= 0) {
475 	    mib[6] = cpuflag;
476 	    miblen = 7;
477 	} else {
478 	    miblen = 6;
479 	}
480 	if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
481 		errx(1, "route-sysctl-estimate");
482 	if ((buf = malloc(needed)) == NULL)
483 		errx(1, "malloc");
484 	if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
485 		errx(1, "actual retrieval of routing table");
486 	lim = buf + needed;
487 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
488 		rtm = (struct rt_msghdr *)next;
489 		sin2 = (struct sockaddr_inarp *)(rtm + 1);
490 		sdl = (struct sockaddr_dl *)((char *)sin2 + ROUNDUP(sin2->sin_len));
491 		if (addr) {
492 			if (addr != sin2->sin_addr.s_addr)
493 				continue;
494 			found_entry = 1;
495 		}
496 		(*action)(sdl, sin2, rtm);
497 	}
498 	free(buf);
499 }
500 
501 /*
502  * Display an arp entry
503  */
504 void
505 print_entry(struct sockaddr_dl *sdl,
506 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
507 {
508 	const char *host;
509 	struct hostent *hp;
510 	char ifname[IF_NAMESIZE];
511 
512 	if (nflag == 0)
513 		hp = gethostbyaddr(&(addr->sin_addr), sizeof(addr->sin_addr),
514 		    AF_INET);
515 	else
516 		hp = 0;
517 	if (hp)
518 		host = hp->h_name;
519 	else {
520 		host = "?";
521 		if (h_errno == TRY_AGAIN)
522 			nflag = 1;
523 	}
524 	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
525 	if (sdl->sdl_alen)
526 		printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
527 	else
528 		printf("(incomplete)");
529 	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
530 		printf(" on %s", ifname);
531 	if (rtm->rtm_rmx.rmx_expire == 0)
532 		printf(" permanent");
533 	if (addr->sin_other & SIN_PROXY)
534 		printf(" published (proxy only)");
535 	if (rtm->rtm_addrs & RTA_NETMASK) {
536 		addr = (struct sockaddr_inarp *)
537 			(ROUNDUP(sdl->sdl_len) + (char *)sdl);
538 		if (addr->sin_addr.s_addr == 0xffffffff)
539 			printf(" published");
540 		if (addr->sin_len != 8)
541 			printf("(weird)");
542 	}
543         switch(sdl->sdl_type) {
544             case IFT_ETHER:
545                 printf(" [ethernet]");
546                 break;
547 	    case IFT_L2VLAN:
548 		printf(" [vlan]");
549 		break;
550             default:
551 		break;
552         }
553 
554 	printf("\n");
555 
556 }
557 
558 /*
559  * Nuke an arp entry
560  */
561 void
562 nuke_entry(struct sockaddr_dl *sdl __unused,
563 	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
564 {
565 	char ip[20];
566 
567 	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
568 	delete(ip, NULL);
569 }
570 
571 int
572 my_ether_aton(char *a, struct ether_addr *n)
573 {
574 	struct ether_addr *ea;
575 
576 	if ((ea = ether_aton(a)) == NULL) {
577 		warnx("invalid Ethernet address '%s'", a);
578 		return(1);
579 	}
580 	*n = *ea;
581 	return(0);
582 }
583 
584 void
585 usage(void)
586 {
587 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
588 		"usage: arp [-n] [-c cpu] hostname",
589 		"       arp [-n] [-c cpu] -a",
590 		"       arp -d hostname [pub]",
591 		"       arp -d -a",
592 		"       arp -s hostname ether_addr [temp] [pub [only]]",
593 		"       arp -S hostname ether_addr [temp] [pub [only]]",
594 		"       arp -f filename");
595 	exit(1);
596 }
597 
598 int
599 rtmsg(int cmd)
600 {
601 	static int seq;
602 	int rlen;
603 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
604 	char *cp = m_rtmsg.m_space;
605 	int l;
606 
607 	errno = 0;
608 	if (cmd == RTM_DELETE || cmd == RTM_CHANGE)
609 		goto doit;
610 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
611 	rtm->rtm_flags = flags;
612 	rtm->rtm_version = RTM_VERSION;
613 
614 	switch (cmd) {
615 	default:
616 		errx(1, "internal wrong cmd");
617 	case RTM_ADD:
618 		rtm->rtm_addrs |= RTA_GATEWAY;
619 		rtm->rtm_rmx.rmx_expire = expire_time;
620 		rtm->rtm_inits = RTV_EXPIRE;
621 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
622 		sin_m.sin_other = 0;
623 		if (doing_proxy) {
624 			if (proxy_only)
625 				sin_m.sin_other = SIN_PROXY;
626 			else {
627 				rtm->rtm_addrs |= RTA_NETMASK;
628 				rtm->rtm_flags &= ~RTF_HOST;
629 			}
630 		}
631 		/* FALLTHROUGH */
632 	case RTM_GET:
633 		rtm->rtm_addrs |= RTA_DST;
634 	}
635 #define NEXTADDR(w, s) \
636 	if (rtm->rtm_addrs & (w)) { \
637 		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
638 
639 	NEXTADDR(RTA_DST, sin_m);
640 	NEXTADDR(RTA_GATEWAY, sdl_m);
641 	NEXTADDR(RTA_NETMASK, so_mask);
642 
643 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
644 doit:
645 	l = rtm->rtm_msglen;
646 	rtm->rtm_seq = ++seq;
647 	rtm->rtm_type = cmd;
648 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
649 		if (errno != ESRCH || (cmd != RTM_DELETE && cmd != RTM_CHANGE)) {
650 			warn("writing to routing socket");
651 			return(-1);
652 		}
653 	}
654 	do {
655 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
656 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
657 	if (l < 0)
658 		warn("read from routing socket");
659 	return(0);
660 }
661 
662 /*
663  * get_ether_addr - get the hardware address of an interface on the
664  * the same subnet as ipaddr.
665  */
666 #define MAX_IFS		32
667 
668 int
669 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
670 {
671 	struct ifreq *ifr, *ifend, *ifp;
672 	u_int32_t ina, mask;
673 	struct sockaddr_dl *dla;
674 	struct ifreq ifreq;
675 	struct ifconf ifc;
676 	struct ifreq ifs[MAX_IFS];
677 	int sock;
678 
679 	sock = socket(AF_INET, SOCK_DGRAM, 0);
680 	if (sock < 0)
681 		err(1, "socket");
682 
683 	ifc.ifc_len = sizeof(ifs);
684 	ifc.ifc_req = ifs;
685 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
686 		warnx("ioctl(SIOCGIFCONF)");
687 		close(sock);
688 		return(0);
689 	}
690 
691 	/*
692 	* Scan through looking for an interface with an Internet
693 	* address on the same subnet as `ipaddr'.
694 	*/
695 	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
696 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
697 		if (ifr->ifr_addr.sa_family == AF_INET) {
698 			ina = ((struct sockaddr_in *)
699 				&ifr->ifr_addr)->sin_addr.s_addr;
700 			strncpy(ifreq.ifr_name, ifr->ifr_name,
701 				sizeof(ifreq.ifr_name));
702 			/*
703 			 * Check that the interface is up,
704 			 * and not point-to-point or loopback.
705 			 */
706 			if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
707 				continue;
708 			if ((ifreq.ifr_flags &
709 			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
710 					IFF_LOOPBACK|IFF_NOARP))
711 			     != (IFF_UP|IFF_BROADCAST))
712 				goto nextif;
713 			/*
714 			 * Get its netmask and check that it's on
715 			 * the right subnet.
716 			 */
717 			if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
718 				continue;
719 			mask = ((struct sockaddr_in *)
720 				&ifreq.ifr_addr)->sin_addr.s_addr;
721 			if ((ipaddr & mask) != (ina & mask))
722 				goto nextif;
723 			break;
724 		}
725 nextif:
726 		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
727 		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
728 	}
729 
730 	if (ifr >= ifend) {
731 		close(sock);
732 		return(0);
733 	}
734 
735 	/*
736 	* Now scan through again looking for a link-level address
737 	* for this interface.
738 	*/
739 	ifp = ifr;
740 	for (ifr = ifc.ifc_req; ifr < ifend; ) {
741 		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
742 		    && ifr->ifr_addr.sa_family == AF_LINK) {
743 			/*
744 			 * Found the link-level address - copy it out
745 			 */
746 		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
747 			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
748 			close (sock);
749 			printf("using interface %s for proxy with address ",
750 				ifp->ifr_name);
751 			printf("%s\n", ether_ntoa(hwaddr));
752 			return(dla->sdl_alen);
753 		}
754 		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
755 		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
756 	}
757 	return(0);
758 }
759