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