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