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