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