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