Merge from vendor branch OPENSSL:
[dragonfly.git] / usr.sbin / arp / arp.c
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.10 2007/10/11 21:51:20 thomas 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 <strings.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         int seg;
512
513         if (nflag == 0)
514                 hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
515                     sizeof(addr->sin_addr), AF_INET);
516         else
517                 hp = 0;
518         if (hp)
519                 host = hp->h_name;
520         else {
521                 host = "?";
522                 if (h_errno == TRY_AGAIN)
523                         nflag = 1;
524         }
525         printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
526         if (sdl->sdl_alen)
527                 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
528         else
529                 printf("(incomplete)");
530         if (if_indextoname(sdl->sdl_index, ifname) != NULL)
531                 printf(" on %s", ifname);
532         if (rtm->rtm_rmx.rmx_expire == 0)
533                 printf(" permanent");
534         if (addr->sin_other & SIN_PROXY)
535                 printf(" published (proxy only)");
536         if (rtm->rtm_addrs & RTA_NETMASK) {
537                 addr = (struct sockaddr_inarp *)
538                         (ROUNDUP(sdl->sdl_len) + (char *)sdl);
539                 if (addr->sin_addr.s_addr == 0xffffffff)
540                         printf(" published");
541                 if (addr->sin_len != 8)
542                         printf("(weird)");
543         }
544         switch(sdl->sdl_type) {
545             case IFT_ETHER:
546                 printf(" [ethernet]");
547                 break;
548             case IFT_L2VLAN:
549                 printf(" [vlan]");
550                 break;
551             default:
552                 break;
553         }
554                 
555         printf("\n");
556
557 }
558
559 /*
560  * Nuke an arp entry
561  */
562 void
563 nuke_entry(struct sockaddr_dl *sdl __unused,
564         struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
565 {
566         char ip[20];
567
568         snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
569         delete(ip, NULL);
570 }
571
572 int
573 my_ether_aton(char *a, struct ether_addr *n)
574 {
575         struct ether_addr *ea;
576
577         if ((ea = ether_aton(a)) == NULL) {
578                 warnx("invalid Ethernet address '%s'", a);
579                 return(1);
580         }
581         *n = *ea;
582         return(0);
583 }
584
585 void
586 usage(void)
587 {
588         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
589                 "usage: arp [-n] [-c cpu] hostname",
590                 "       arp [-n] [-c cpu] -a",
591                 "       arp -d hostname [pub]",
592                 "       arp -d -a",
593                 "       arp -s hostname ether_addr [temp] [pub [only]]",
594                 "       arp -S hostname ether_addr [temp] [pub [only]]",
595                 "       arp -f filename");
596         exit(1);
597 }
598
599 int
600 rtmsg(int cmd)
601 {
602         static int seq;
603         int rlen;
604         struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
605         char *cp = m_rtmsg.m_space;
606         int l;
607
608         errno = 0;
609         if (cmd == RTM_DELETE || cmd == RTM_CHANGE)
610                 goto doit;
611         bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
612         rtm->rtm_flags = flags;
613         rtm->rtm_version = RTM_VERSION;
614
615         switch (cmd) {
616         default:
617                 errx(1, "internal wrong cmd");
618         case RTM_ADD:
619                 rtm->rtm_addrs |= RTA_GATEWAY;
620                 rtm->rtm_rmx.rmx_expire = expire_time;
621                 rtm->rtm_inits = RTV_EXPIRE;
622                 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
623                 sin_m.sin_other = 0;
624                 if (doing_proxy) {
625                         if (proxy_only)
626                                 sin_m.sin_other = SIN_PROXY;
627                         else {
628                                 rtm->rtm_addrs |= RTA_NETMASK;
629                                 rtm->rtm_flags &= ~RTF_HOST;
630                         }
631                 }
632                 /* FALLTHROUGH */
633         case RTM_GET:
634                 rtm->rtm_addrs |= RTA_DST;
635         }
636 #define NEXTADDR(w, s) \
637         if (rtm->rtm_addrs & (w)) { \
638                 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
639
640         NEXTADDR(RTA_DST, sin_m);
641         NEXTADDR(RTA_GATEWAY, sdl_m);
642         NEXTADDR(RTA_NETMASK, so_mask);
643
644         rtm->rtm_msglen = cp - (char *)&m_rtmsg;
645 doit:
646         l = rtm->rtm_msglen;
647         rtm->rtm_seq = ++seq;
648         rtm->rtm_type = cmd;
649         if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
650                 if (errno != ESRCH || (cmd != RTM_DELETE && cmd != RTM_CHANGE)) {
651                         warn("writing to routing socket");
652                         return(-1);
653                 }
654         }
655         do {
656                 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
657         } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
658         if (l < 0)
659                 warn("read from routing socket");
660         return(0);
661 }
662
663 /*
664  * get_ether_addr - get the hardware address of an interface on the
665  * the same subnet as ipaddr.
666  */
667 #define MAX_IFS         32
668
669 int
670 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
671 {
672         struct ifreq *ifr, *ifend, *ifp;
673         u_int32_t ina, mask;
674         struct sockaddr_dl *dla;
675         struct ifreq ifreq;
676         struct ifconf ifc;
677         struct ifreq ifs[MAX_IFS];
678         int sock;
679
680         sock = socket(AF_INET, SOCK_DGRAM, 0);
681         if (sock < 0)
682                 err(1, "socket");
683
684         ifc.ifc_len = sizeof(ifs);
685         ifc.ifc_req = ifs;
686         if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
687                 warnx("ioctl(SIOCGIFCONF)");
688                 close(sock);
689                 return(0);
690         }
691
692         /*
693         * Scan through looking for an interface with an Internet
694         * address on the same subnet as `ipaddr'.
695         */
696         ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
697         for (ifr = ifc.ifc_req; ifr < ifend; ) {
698                 if (ifr->ifr_addr.sa_family == AF_INET) {
699                         ina = ((struct sockaddr_in *) 
700                                 &ifr->ifr_addr)->sin_addr.s_addr;
701                         strncpy(ifreq.ifr_name, ifr->ifr_name, 
702                                 sizeof(ifreq.ifr_name));
703                         /*
704                          * Check that the interface is up,
705                          * and not point-to-point or loopback.
706                          */
707                         if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
708                                 continue;
709                         if ((ifreq.ifr_flags &
710                              (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
711                                         IFF_LOOPBACK|IFF_NOARP))
712                              != (IFF_UP|IFF_BROADCAST))
713                                 goto nextif;
714                         /*
715                          * Get its netmask and check that it's on 
716                          * the right subnet.
717                          */
718                         if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
719                                 continue;
720                         mask = ((struct sockaddr_in *)
721                                 &ifreq.ifr_addr)->sin_addr.s_addr;
722                         if ((ipaddr & mask) != (ina & mask))
723                                 goto nextif;
724                         break;
725                 }
726 nextif:
727                 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
728                     + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
729         }
730
731         if (ifr >= ifend) {
732                 close(sock);
733                 return(0);
734         }
735
736         /*
737         * Now scan through again looking for a link-level address
738         * for this interface.
739         */
740         ifp = ifr;
741         for (ifr = ifc.ifc_req; ifr < ifend; ) {
742                 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
743                     && ifr->ifr_addr.sa_family == AF_LINK) {
744                         /*
745                          * Found the link-level address - copy it out
746                          */
747                         dla = (struct sockaddr_dl *) &ifr->ifr_addr;
748                         memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
749                         close (sock);
750                         printf("using interface %s for proxy with address ",
751                                 ifp->ifr_name);
752                         printf("%s\n", ether_ntoa(hwaddr));
753                         return(dla->sdl_alen);
754                 }
755                 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
756                     + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
757         }
758         return(0);
759 }