rarpd.c revision 1.45 1 /* $NetBSD: rarpd.c,v 1.45 2002/07/14 00:58:30 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23 #include <sys/cdefs.h>
24 #ifndef lint
25 __COPYRIGHT(
26 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
27 All rights reserved.\n");
28 #endif /* not lint */
29
30 #ifndef lint
31 __RCSID("$NetBSD: rarpd.c,v 1.45 2002/07/14 00:58:30 wiz Exp $");
32 #endif
33
34
35 /*
36 * rarpd - Reverse ARP Daemon
37 *
38 * Usage: rarpd -a [-d|-f] [-l]
39 * rarpd [-d|-f] [-l] interface
40 */
41
42 #include <sys/param.h>
43 #include <sys/file.h>
44 #include <sys/time.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47
48 #include <net/bpf.h>
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #ifdef __NetBSD__
52 #include <net/if_ether.h>
53 #endif
54 #include <net/if_types.h>
55 #include <netinet/in.h>
56 #ifdef __NetBSD__
57 #include <netinet/if_inarp.h>
58 #else
59 #include <netinet/if_ether.h>
60 #endif
61
62 #include <arpa/inet.h>
63
64 #include <errno.h>
65 #include <dirent.h>
66 #include <netdb.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <syslog.h>
71 #include <unistd.h>
72 #include <util.h>
73 #ifdef HAVE_IFADDRS_H
74 #include <ifaddrs.h>
75 #endif
76
77 #define FATAL 1 /* fatal error occurred */
78 #define NONFATAL 0 /* non fatal error occurred */
79
80 /*
81 * The structure for each interface.
82 */
83 struct if_info {
84 int ii_fd; /* BPF file descriptor */
85 u_char ii_eaddr[6]; /* Ethernet address of this interface */
86 u_int32_t ii_ipaddr; /* IP address of this interface */
87 u_int32_t ii_netmask; /* subnet or net mask */
88 char *ii_name; /* interface name */
89 struct if_info *ii_alias;
90 struct if_info *ii_next;
91 };
92 /*
93 * The list of all interfaces that are being listened to. rarp_loop()
94 * "selects" on the descriptors in this list.
95 */
96 struct if_info *iflist;
97
98 u_int32_t choose_ipaddr(u_int32_t **, u_int32_t, u_int32_t);
99 void debug(const char *,...)
100 __attribute__((__format__(__printf__, 1, 2)));
101 void init_all(void);
102 void init_one(char *, u_int32_t);
103 u_int32_t ipaddrtonetmask(u_int32_t);
104 void lookup_eaddr(char *, u_char *);
105 void lookup_ipaddr(char *, u_int32_t *, u_int32_t *);
106 int main(int, char **);
107 void rarp_loop(void);
108 int rarp_open(char *);
109 void rarp_process(struct if_info *, u_char *);
110 void rarp_reply(struct if_info *, struct ether_header *, u_int32_t,
111 struct hostent *);
112 void rarperr(int, const char *,...)
113 __attribute__((__format__(__printf__, 2, 3)));
114
115 #if defined(__NetBSD__)
116 #include "mkarp.h"
117 #else
118 void update_arptab(u_char *, u_int32_t);
119 #endif
120
121 void usage(void);
122
123 static int bpf_open(void);
124 static int rarp_check(u_char *, int);
125
126 #ifdef REQUIRE_TFTPBOOT
127 int rarp_bootable(u_int32_t);
128 #endif
129
130 int aflag = 0; /* listen on "all" interfaces */
131 int dflag = 0; /* print debugging messages */
132 int fflag = 0; /* don't fork */
133 int lflag = 0; /* log all replies */
134
135 int
136 main(int argc, char **argv)
137 {
138 int op;
139 char *ifname, *hostname;
140
141 /* All error reporting is done through syslogs. */
142 openlog("rarpd", LOG_PID, LOG_DAEMON);
143
144 opterr = 0;
145 while ((op = getopt(argc, argv, "adfl")) != -1) {
146 switch (op) {
147 case 'a':
148 ++aflag;
149 break;
150
151 case 'd':
152 ++dflag;
153 break;
154
155 case 'f':
156 ++fflag;
157 break;
158
159 case 'l':
160 ++lflag;
161 break;
162
163 default:
164 usage();
165 /* NOTREACHED */
166 }
167 }
168 ifname = argv[optind++];
169 hostname = ifname ? argv[optind] : 0;
170 if ((aflag && ifname) || (!aflag && ifname == 0))
171 usage();
172
173 if ((!fflag) && (!dflag)) {
174 if (daemon(0, 0))
175 rarperr(FATAL, "daemon");
176 pidfile(NULL);
177 }
178
179 if (aflag)
180 init_all();
181 else
182 init_one(ifname, INADDR_ANY);
183
184 rarp_loop();
185 /* NOTREACHED */
186 return (0);
187 }
188
189 /*
190 * Add 'ifname' to the interface list. Lookup its IP address and network
191 * mask and Ethernet address, and open a BPF file for it.
192 */
193 void
194 init_one(char *ifname, u_int32_t ipaddr)
195 {
196 struct if_info *h;
197 struct if_info *p;
198 int fd;
199
200 for (h = iflist; h != NULL; h = h->ii_next) {
201 if (!strcmp(h->ii_name, ifname))
202 break;
203 }
204 if (h == NULL) {
205 fd = rarp_open(ifname);
206 if (fd < 0)
207 return;
208 } else {
209 fd = h->ii_fd;
210 }
211
212 p = (struct if_info *)malloc(sizeof(*p));
213 if (p == 0) {
214 rarperr(FATAL, "malloc: %s", strerror(errno));
215 /* NOTREACHED */
216 }
217 p->ii_name = strdup(ifname);
218 if (p->ii_name == 0) {
219 rarperr(FATAL, "malloc: %s", strerror(errno));
220 /* NOTREACHED */
221 }
222 if (h != NULL) {
223 p->ii_alias = h->ii_alias;
224 h->ii_alias = p;
225 } else {
226 p->ii_next = iflist;
227 iflist = p;
228 }
229
230 p->ii_fd = fd;
231 p->ii_ipaddr = ipaddr;
232 lookup_eaddr(ifname, p->ii_eaddr);
233 lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask);
234 }
235
236 /*
237 * Initialize all "candidate" interfaces that are in the system
238 * configuration list. A "candidate" is up, not loopback and not
239 * point to point.
240 */
241 void
242 init_all(void)
243 {
244 #ifdef HAVE_IFADDRS_H
245 struct ifaddrs *ifap, *ifa, *p;
246
247 if (getifaddrs(&ifap) != 0) {
248 rarperr(FATAL, "getifaddrs: %s", strerror(errno));
249 /* NOTREACHED */
250 }
251
252 p = NULL;
253 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
254 #define SIN(s) ((struct sockaddr_in *) (s))
255 if (ifa->ifa_addr->sa_family != AF_INET)
256 continue;
257 if (p && !strcmp(p->ifa_name, ifa->ifa_name) &&
258 SIN(p->ifa_addr)->sin_addr.s_addr == SIN(ifa->ifa_addr)->sin_addr.s_addr)
259 continue;
260 p = ifa;
261 if ((ifa->ifa_flags &
262 (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
263 continue;
264 init_one(ifa->ifa_name, SIN(ifa->ifa_addr)->sin_addr.s_addr);
265 #undef SIN
266 }
267 freeifaddrs(ifap);
268 #else
269 char inbuf[8192*2];
270 struct ifconf ifc;
271 struct ifreq ifreq, ifrd, *ifr, *ifrp;
272 int fd;
273 int i, len;
274
275 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
276 rarperr(FATAL, "socket: %s", strerror(errno));
277 /* NOTREACHED */
278 }
279
280 ifc.ifc_len = sizeof(inbuf);
281 ifc.ifc_buf = inbuf;
282 if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
283 ifc.ifc_len < sizeof(struct ifreq)) {
284 rarperr(FATAL, "init_all: SIOCGIFCONF: %s", strerror(errno));
285 /* NOTREACHED */
286 }
287 ifr = &ifrd;
288 ifrp = ifc.ifc_req;
289 ifreq.ifr_name[0] = '\0';
290 for (i = 0; i < ifc.ifc_len;
291 i += len, ifrp = (struct ifreq *)((caddr_t)ifrp + len)) {
292 #define SIN(s) ((struct sockaddr_in *) (s))
293 memcpy(&ifrd, ifrp, sizeof (ifrd));
294 len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
295 if (ifr->ifr_addr.sa_family != AF_INET)
296 continue;
297 if (!strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))
298 && SIN(&ifreq.ifr_addr)->sin_addr.s_addr == SIN(&ifr->ifr_addr)->sin_addr.s_addr)
299 continue;
300 ifreq = *ifr;
301 if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
302 rarperr(FATAL, "init_all: SIOCGIFFLAGS: %s",
303 strerror(errno));
304 /* NOTREACHED */
305 }
306 if ((ifr->ifr_flags &
307 (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
308 continue;
309 init_one(ifr->ifr_name, SIN(&ifreq.ifr_addr)->sin_addr.s_addr);
310 #undef SIN
311 }
312 (void)close(fd);
313 #endif
314 }
315
316 void
317 usage(void)
318 {
319 (void) fprintf(stderr, "usage: rarpd -a [-d|-f] [-l]\n");
320 (void) fprintf(stderr, " rarpd [-d|-f] [-l] interface\n");
321 exit(1);
322 }
323
324 static int
325 bpf_open(void)
326 {
327 int fd;
328 int n = 0;
329 char device[sizeof "/dev/bpf000"];
330
331 /* Go through all the minors and find one that isn't in use. */
332 do {
333 (void)sprintf(device, "/dev/bpf%d", n++);
334 fd = open(device, O_RDWR);
335 } while (fd < 0 && errno == EBUSY);
336
337 if (fd < 0) {
338 rarperr(FATAL, "%s: %s", device, strerror(errno));
339 /* NOTREACHED */
340 }
341 return fd;
342 }
343 /*
344 * Open a BPF file and attach it to the interface named 'device'.
345 * Set immediate mode, and set a filter that accepts only RARP requests.
346 */
347 int
348 rarp_open(char *device)
349 {
350 int fd;
351 struct ifreq ifr;
352 u_int dlt;
353 int immediate;
354
355 static struct bpf_insn insns[] = {
356 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
357 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_REVARP, 0, 3),
358 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
359 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ARPOP_REVREQUEST, 0, 1),
360 BPF_STMT(BPF_RET | BPF_K,
361 sizeof(struct arphdr) +
362 2 * ETHER_ADDR_LEN + 2 * sizeof(struct in_addr) +
363 sizeof(struct ether_header)),
364 BPF_STMT(BPF_RET | BPF_K, 0),
365 };
366 static struct bpf_program filter = {
367 sizeof insns / sizeof(insns[0]),
368 insns
369 };
370
371 fd = bpf_open();
372
373 /* Set immediate mode so packets are processed as they arrive. */
374 immediate = 1;
375 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
376 rarperr(FATAL, "BIOCIMMEDIATE: %s", strerror(errno));
377 /* NOTREACHED */
378 }
379 (void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name - 1);
380 ifr.ifr_name[sizeof ifr.ifr_name - 1] = '\0';
381 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
382 if (aflag) { /* for -a skip non-ethernet interfaces */
383 close(fd);
384 return(-1);
385 }
386 rarperr(FATAL, "BIOCSETIF: %s", strerror(errno));
387 /* NOTREACHED */
388 }
389 /* Check that the data link layer is an Ethernet; this code won't work
390 * with anything else. */
391 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
392 rarperr(FATAL, "BIOCGDLT: %s", strerror(errno));
393 /* NOTREACHED */
394 }
395 if (dlt != DLT_EN10MB) {
396 if (aflag) { /* for -a skip non-ethernet interfaces */
397 close(fd);
398 return(-1);
399 }
400 rarperr(FATAL, "%s is not an ethernet", device);
401 /* NOTREACHED */
402 }
403 /* Set filter program. */
404 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
405 rarperr(FATAL, "BIOCSETF: %s", strerror(errno));
406 /* NOTREACHED */
407 }
408 return fd;
409 }
410 /*
411 * Perform various sanity checks on the RARP request packet. Return
412 * false on failure and log the reason.
413 */
414 static int
415 rarp_check(u_char *p, int len)
416 {
417 struct ether_header *ep = (struct ether_header *) p;
418 #ifdef __NetBSD__
419 struct arphdr *ap = (struct arphdr *) (p + sizeof(*ep));
420 #else
421 struct ether_arp *ap = (struct ether_arp *) (p + sizeof(*ep));
422 #endif
423
424 if (len < sizeof(*ep) + sizeof(*ap)) {
425 rarperr(NONFATAL, "truncated request");
426 return 0;
427 }
428 #ifdef __NetBSD__
429 /* now that we know the fixed part of the ARP hdr is there: */
430 if (len < sizeof(*ap) + 2 * ap->ar_hln + 2 * ap->ar_pln) {
431 rarperr(NONFATAL, "truncated request");
432 return 0;
433 }
434 #endif
435 /* XXX This test might be better off broken out... */
436 #ifdef __FreeBSD__
437 /* BPF (incorrectly) returns this in host order. */
438 if (ep->ether_type != ETHERTYPE_REVARP ||
439 #else
440 if (ntohs (ep->ether_type) != ETHERTYPE_REVARP ||
441 #endif
442 #ifdef __NetBSD__
443 ntohs (ap->ar_hrd) != ARPHRD_ETHER ||
444 ntohs (ap->ar_op) != ARPOP_REVREQUEST ||
445 ntohs (ap->ar_pro) != ETHERTYPE_IP ||
446 ap->ar_hln != 6 || ap->ar_pln != 4) {
447 #else
448 ntohs (ap->arp_hrd) != ARPHRD_ETHER ||
449 ntohs (ap->arp_op) != ARPOP_REVREQUEST ||
450 ntohs (ap->arp_pro) != ETHERTYPE_IP ||
451 ap->arp_hln != 6 || ap->arp_pln != 4) {
452 #endif
453 rarperr(NONFATAL, "request fails sanity check");
454 return 0;
455 }
456 #ifdef __NetBSD__
457 if (memcmp((char *) &ep->ether_shost, ar_sha(ap), 6) != 0) {
458 #else
459 if (memcmp((char *) &ep->ether_shost, ap->arp_sha, 6) != 0) {
460 #endif
461 rarperr(NONFATAL, "ether/arp sender address mismatch");
462 return 0;
463 }
464 #ifdef __NetBSD__
465 if (memcmp(ar_sha(ap), ar_tha(ap), 6) != 0) {
466 #else
467 if (memcmp((char *) &ap->arp_sha, (char *) &ap->arp_tha, 6) != 0) {
468 #endif
469 rarperr(NONFATAL, "ether/arp target address mismatch");
470 return 0;
471 }
472 return 1;
473 }
474
475 /*
476 * Loop indefinitely listening for RARP requests on the
477 * interfaces in 'iflist'.
478 */
479 void
480 rarp_loop(void)
481 {
482 u_char *buf, *bp, *ep;
483 int cc, fd;
484 fd_set fds, listeners;
485 int bufsize, maxfd = 0;
486 struct if_info *ii;
487
488 if (iflist == 0) {
489 rarperr(FATAL, "no interfaces");
490 /* NOTREACHED */
491 }
492 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
493 rarperr(FATAL, "BIOCGBLEN: %s", strerror(errno));
494 /* NOTREACHED */
495 }
496 buf = (u_char *) malloc((unsigned) bufsize);
497 if (buf == 0) {
498 rarperr(FATAL, "malloc: %s", strerror(errno));
499 /* NOTREACHED */
500 }
501 /*
502 * Find the highest numbered file descriptor for select().
503 * Initialize the set of descriptors to listen to.
504 */
505 FD_ZERO(&fds);
506 for (ii = iflist; ii; ii = ii->ii_next) {
507 FD_SET(ii->ii_fd, &fds);
508 if (ii->ii_fd > maxfd)
509 maxfd = ii->ii_fd;
510 }
511 while (1) {
512 listeners = fds;
513 if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
514 (struct fd_set *) 0, (struct timeval *) 0) < 0) {
515 rarperr(FATAL, "select: %s", strerror(errno));
516 /* NOTREACHED */
517 }
518 for (ii = iflist; ii; ii = ii->ii_next) {
519 fd = ii->ii_fd;
520 if (!FD_ISSET(fd, &listeners))
521 continue;
522 again:
523 cc = read(fd, (char *) buf, bufsize);
524 /* Don't choke when we get ptraced */
525 if (cc < 0 && errno == EINTR)
526 goto again;
527 /* Due to a SunOS bug, after 2^31 bytes, the file
528 * offset overflows and read fails with EINVAL. The
529 * lseek() to 0 will fix things. */
530 if (cc < 0) {
531 if (errno == EINVAL &&
532 (lseek(fd, 0, SEEK_CUR) + bufsize) < 0) {
533 (void)lseek(fd, 0, 0);
534 goto again;
535 }
536 rarperr(FATAL, "read: %s", strerror(errno));
537 /* NOTREACHED */
538 }
539 /* Loop through the packet(s) */
540 #define bhp ((struct bpf_hdr *)bp)
541 bp = buf;
542 ep = bp + cc;
543 while (bp < ep) {
544 int caplen, hdrlen;
545
546 caplen = bhp->bh_caplen;
547 hdrlen = bhp->bh_hdrlen;
548 debug("received packet on %s", ii->ii_name);
549
550 if (rarp_check(bp + hdrlen, caplen))
551 rarp_process(ii, bp + hdrlen);
552 bp += BPF_WORDALIGN(hdrlen + caplen);
553 }
554 }
555 }
556 }
557
558 #ifdef REQUIRE_TFTPBOOT
559
560 #ifndef TFTP_DIR
561 #define TFTP_DIR "/tftpboot"
562 #endif
563
564 /*
565 * True if this server can boot the host whose IP address is 'addr'.
566 * This check is made by looking in the tftp directory for the
567 * configuration file.
568 */
569 int
570 rarp_bootable(u_int32_t addr)
571 {
572 struct dirent *dent;
573 DIR *d;
574 char ipname[9];
575 static DIR *dd = 0;
576
577 (void)sprintf(ipname, "%08X", addr);
578 /* If directory is already open, rewind it. Otherwise, open it. */
579 if (d = dd)
580 rewinddir(d);
581 else {
582 if (chdir(TFTP_DIR) == -1) {
583 rarperr(FATAL, "chdir: %s", strerror(errno));
584 /* NOTREACHED */
585 }
586 d = opendir(".");
587 if (d == 0) {
588 rarperr(FATAL, "opendir: %s", strerror(errno));
589 /* NOTREACHED */
590 }
591 dd = d;
592 }
593 while (dent = readdir(d))
594 if (strncmp(dent->d_name, ipname, 8) == 0)
595 return 1;
596 return 0;
597 }
598 #endif /* REQUIRE_TFTPBOOT */
599
600 /*
601 * Given a list of IP addresses, 'alist', return the first address that
602 * is on network 'net'; 'netmask' is a mask indicating the network portion
603 * of the address.
604 */
605 u_int32_t
606 choose_ipaddr(u_int32_t **alist, u_int32_t net, u_int32_t netmask)
607 {
608
609 for (; *alist; ++alist) {
610 if ((**alist & netmask) == net)
611 return **alist;
612 }
613 return 0;
614 }
615 /*
616 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
617 * already been checked for validity. The reply is overlaid on the request.
618 */
619 void
620 rarp_process(struct if_info *ii, u_char *pkt)
621 {
622 struct ether_header *ep;
623 struct hostent *hp;
624 u_int32_t target_ipaddr = 0;
625 char ename[MAXHOSTNAMELEN + 1];
626 struct in_addr in;
627
628 ep = (struct ether_header *) pkt;
629
630 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
631 debug("no IP address for %s",
632 ether_ntoa((struct ether_addr *)&ep->ether_shost));
633 return;
634 }
635 ename[sizeof(ename)-1] = '\0';
636
637 if ((hp = gethostbyname(ename)) == 0) {
638 debug("gethostbyname(%s) failed: %s", ename,
639 hstrerror(h_errno));
640 return;
641 }
642
643 /* Choose correct address from list. */
644 if (hp->h_addrtype != AF_INET) {
645 rarperr(FATAL, "cannot handle non IP addresses");
646 /* NOTREACHED */
647 }
648 for (;; ii = ii->ii_alias) {
649 target_ipaddr = choose_ipaddr((u_int32_t **) hp->h_addr_list,
650 ii->ii_ipaddr & ii->ii_netmask, ii->ii_netmask);
651 if (target_ipaddr != 0)
652 break;
653 if (ii->ii_alias == NULL)
654 break;
655 }
656
657 if (target_ipaddr == 0) {
658 in.s_addr = ii->ii_ipaddr & ii->ii_netmask;
659 rarperr(NONFATAL, "cannot find %s on net %s",
660 ename, inet_ntoa(in));
661 return;
662 }
663 #ifdef REQUIRE_TFTPBOOT
664 if (rarp_bootable(htonl(target_ipaddr)))
665 #endif
666 rarp_reply(ii, ep, target_ipaddr, hp);
667 #ifdef REQUIRE_TFTPBOOT
668 else
669 debug("%08X not bootable", htonl(target_ipaddr));
670 #endif
671 }
672 /*
673 * Lookup the ethernet address of the interface attached to the BPF
674 * file descriptor 'fd'; return it in 'eaddr'.
675 */
676 void
677 lookup_eaddr(char *ifname, u_char *eaddr)
678 {
679 #ifdef HAVE_IFADDRS_H
680 struct ifaddrs *ifap, *ifa;
681 struct sockaddr_dl *sdl;
682
683 if (getifaddrs(&ifap) != 0) {
684 rarperr(FATAL, "getifaddrs: %s", strerror(errno));
685 /* NOTREACHED */
686 }
687
688 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
689 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
690 if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
691 sdl->sdl_alen != 6)
692 continue;
693 if (!strcmp(ifa->ifa_name, ifname)) {
694 memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
695 debug("%s: %x:%x:%x:%x:%x:%x",
696 ifa->ifa_name, eaddr[0], eaddr[1],
697 eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
698 freeifaddrs(ifap);
699 return;
700 }
701 }
702 rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
703 freeifaddrs(ifap);
704 #else
705 char inbuf[8192*2];
706 struct ifconf ifc;
707 struct ifreq *ifr;
708 struct sockaddr_dl *sdl;
709 int fd;
710 int i, len;
711
712 /* We cannot use SIOCGIFADDR on the BPF descriptor.
713 We must instead get all the interfaces with SIOCGIFCONF
714 and find the right one. */
715
716 /* Use datagram socket to get Ethernet address. */
717 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
718 rarperr(FATAL, "socket: %s", strerror(errno));
719 /* NOTREACHED */
720 }
721
722 ifc.ifc_len = sizeof(inbuf);
723 ifc.ifc_buf = inbuf;
724 if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
725 ifc.ifc_len < sizeof(struct ifreq)) {
726 rarperr(FATAL, "lookup_eaddr: SIOGIFCONF: %s", strerror(errno));
727 /* NOTREACHED */
728 }
729 ifr = ifc.ifc_req;
730 for (i = 0; i < ifc.ifc_len;
731 i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
732 len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
733 sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
734 if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
735 sdl->sdl_alen != 6)
736 continue;
737 if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
738 memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
739 debug("%s: %x:%x:%x:%x:%x:%x",
740 ifr->ifr_name, eaddr[0], eaddr[1],
741 eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
742 return;
743 }
744 }
745
746 rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
747 #endif
748 }
749 /*
750 * Lookup the IP address and network mask of the interface named 'ifname'.
751 */
752 void
753 lookup_ipaddr(char *ifname, u_int32_t *addrp, u_int32_t *netmaskp)
754 {
755 int fd;
756
757 /* Use datagram socket to get IP address. */
758 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
759 rarperr(FATAL, "socket: %s", strerror(errno));
760 /* NOTREACHED */
761 }
762 if (*addrp == INADDR_ANY) {
763 struct ifreq ifr;
764 memset(&ifr, 0, sizeof(ifr));
765 (void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
766 if (ioctl(fd, SIOCGIFADDR, (char *) &ifr) < 0) {
767 rarperr(FATAL, "SIOCGIFADDR: %s", strerror(errno));
768 /* NOTREACHED */
769 }
770 *addrp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
771 if (ioctl(fd, SIOCGIFNETMASK, (char *) &ifr) < 0) {
772 perror("SIOCGIFNETMASK");
773 exit(1);
774 }
775 *netmaskp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
776 } else {
777 struct ifaliasreq ifra;
778 memset(&ifra, 0, sizeof(ifra));
779 (void)strncpy(ifra.ifra_name, ifname, sizeof ifra.ifra_name);
780 ((struct sockaddr_in *) & ifra.ifra_addr)->sin_family = AF_INET;
781 ((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr = *addrp;
782 if (ioctl(fd, SIOCGIFALIAS, (char *) &ifra) < 0) {
783 rarperr(FATAL, "SIOCGIFALIAS: %s", strerror(errno));
784 /* NOTREACHED */
785 }
786 *addrp = ((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr;
787 *netmaskp = ((struct sockaddr_in *) & ifra.ifra_mask)->sin_addr.s_addr;
788 }
789 /* If SIOCGIFNETMASK didn't work, figure out a mask from the IP
790 * address class. */
791 if (*netmaskp == 0)
792 *netmaskp = ipaddrtonetmask(*addrp);
793
794 (void)close(fd);
795 }
796 /*
797 * Poke the kernel arp tables with the ethernet/ip address combinataion
798 * given. When processing a reply, we must do this so that the booting
799 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
800 * address of the guy being booted (he cannot answer the ARP).
801 */
802 #ifndef __NetBSD__
803 void
804 update_arptab(u_char *ep, u_int32_t ipaddr)
805 {
806 struct arpreq request;
807 struct sockaddr_in *sin;
808
809 request.arp_flags = 0;
810 sin = (struct sockaddr_in *) & request.arp_pa;
811 sin->sin_family = AF_INET;
812 sin->sin_addr.s_addr = ipaddr;
813 request.arp_ha.sa_family = AF_UNSPEC;
814 /* This is needed #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN,
815 because AF_UNSPEC is zero and the kernel assumes that a zero
816 sa_family means that the real sa_family value is in sa_len. */
817 request.arp_ha.sa_len = 16; /* XXX */
818 memmove((char *) request.arp_ha.sa_data, (char *)ep, 6);
819
820 #if 0
821 s = socket(AF_INET, SOCK_DGRAM, 0);
822 if (ioctl(s, SIOCSARP, (caddr_t) & request) < 0) {
823 rarperr(NONFATAL, "SIOCSARP: %s", strerror(errno));
824 }
825 (void)close(s);
826 #endif
827 }
828 #endif
829
830 /*
831 * Build a reverse ARP packet and sent it out on the interface.
832 * 'ep' points to a valid ARPOP_REVREQUEST. The ARPOP_REVREPLY is built
833 * on top of the request, then written to the network.
834 *
835 * RFC 903 defines the ether_arp fields as follows. The following comments
836 * are taken (more or less) straight from this document.
837 *
838 * ARPOP_REVREQUEST
839 *
840 * arp_sha is the hardware address of the sender of the packet.
841 * arp_spa is undefined.
842 * arp_tha is the 'target' hardware address.
843 * In the case where the sender wishes to determine his own
844 * protocol address, this, like arp_sha, will be the hardware
845 * address of the sender.
846 * arp_tpa is undefined.
847 *
848 * ARPOP_REVREPLY
849 *
850 * arp_sha is the hardware address of the responder (the sender of the
851 * reply packet).
852 * arp_spa is the protocol address of the responder (see the note below).
853 * arp_tha is the hardware address of the target, and should be the same as
854 * that which was given in the request.
855 * arp_tpa is the protocol address of the target, that is, the desired address.
856 *
857 * Note that the requirement that arp_spa be filled in with the responder's
858 * protocol is purely for convenience. For instance, if a system were to use
859 * both ARP and RARP, then the inclusion of the valid protocol-hardware
860 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
861 * ARP request.
862 */
863 void
864 rarp_reply(struct if_info *ii, struct ether_header *ep, u_int32_t ipaddr,
865 struct hostent *hp)
866 {
867 int n;
868 #ifdef __NetBSD__
869 struct arphdr *ap = (struct arphdr *) (ep + 1);
870 #else
871 struct ether_arp *ap = (struct ether_arp *) (ep + 1);
872 #endif
873
874 int len;
875
876 #ifdef __NetBSD__
877 (void)mkarp(ar_sha(ap), ipaddr);
878 #else
879 update_arptab((u_char *) & ap->arp_sha, ipaddr);
880 #endif
881
882 /* Build the rarp reply by modifying the rarp request in place. */
883 #ifdef __FreeBSD__
884 /* BPF (incorrectly) wants this in host order. */
885 ep->ether_type = ETHERTYPE_REVARP;
886 #else
887 ep->ether_type = htons(ETHERTYPE_REVARP);
888 #endif
889 #ifdef __NetBSD__
890 ap->ar_hrd = htons(ARPHRD_ETHER);
891 ap->ar_pro = htons(ETHERTYPE_IP);
892 ap->ar_op = htons(ARPOP_REVREPLY);
893
894 memmove((char *) &ep->ether_dhost, ar_sha(ap), 6);
895 memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
896 memmove(ar_sha(ap), (char *) ii->ii_eaddr, 6);
897
898 memmove(ar_tpa(ap), (char *) &ipaddr, 4);
899 /* Target hardware is unchanged. */
900 memmove(ar_spa(ap), (char *) &ii->ii_ipaddr, 4);
901
902 len = sizeof(*ep) + sizeof(*ap) +
903 2 * ap->ar_pln + 2 * ap->ar_hln;
904 #else
905 ap->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
906 ap->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
907 ap->arp_op = htons(ARPOP_REVREPLY);
908
909 memmove((char *) &ep->ether_dhost, (char *) &ap->arp_sha, 6);
910 memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
911 memmove((char *) &ap->arp_sha, (char *) ii->ii_eaddr, 6);
912
913 memmove((char *) ap->arp_tpa, (char *) &ipaddr, 4);
914 /* Target hardware is unchanged. */
915 memmove((char *) ap->arp_spa, (char *) &ii->ii_ipaddr, 4);
916
917 len = sizeof(*ep) + sizeof(*ap);
918 #endif
919
920 debug("%s asked; %s replied",
921 ether_ntoa((struct ether_addr *)ar_tha(ap)), hp->h_name);
922 if (lflag)
923 syslog(LOG_INFO, "%s asked; %s replied",
924 ether_ntoa((struct ether_addr *)ar_tha(ap)), hp->h_name);
925 n = write(ii->ii_fd, (char *) ep, len);
926 if (n != len) {
927 rarperr(NONFATAL, "write: only %d of %d bytes written", n, len);
928 }
929 }
930 /*
931 * Get the netmask of an IP address. This routine is used if
932 * SIOCGIFNETMASK doesn't work.
933 */
934 u_int32_t
935 ipaddrtonetmask(u_int32_t addr)
936 {
937
938 if (IN_CLASSA(addr))
939 return IN_CLASSA_NET;
940 if (IN_CLASSB(addr))
941 return IN_CLASSB_NET;
942 if (IN_CLASSC(addr))
943 return IN_CLASSC_NET;
944 rarperr(FATAL, "unknown IP address class: %08X", addr);
945 /* NOTREACHED */
946 return(-1);
947 }
948
949 #include <stdarg.h>
950
951 void
952 rarperr(int fatal, const char *fmt,...)
953 {
954 va_list ap;
955
956 va_start(ap, fmt);
957 if (dflag) {
958 if (fatal)
959 (void)fprintf(stderr, "rarpd: error: ");
960 else
961 (void)fprintf(stderr, "rarpd: warning: ");
962 (void)vfprintf(stderr, fmt, ap);
963 va_end(ap);
964 va_start(ap, fmt);
965 (void)fprintf(stderr, "\n");
966 }
967 vsyslog(LOG_ERR, fmt, ap);
968 va_end(ap);
969 if (fatal)
970 exit(1);
971 /* NOTREACHED */
972 }
973
974 void
975 debug(const char *fmt,...)
976 {
977 va_list ap;
978
979 va_start(ap, fmt);
980 if (dflag) {
981 (void)fprintf(stderr, "rarpd: ");
982 (void)vfprintf(stderr, fmt, ap);
983 va_end(ap);
984 va_start(ap, fmt);
985 (void)fprintf(stderr, "\n");
986 }
987 vsyslog(LOG_WARNING, fmt, ap);
988 va_end(ap);
989 }
990