rarpd.c revision 1.28
1/* $NetBSD: rarpd.c,v 1.28 1998/09/29 09:21:35 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.28 1998/09/29 09:21:35 mrg 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/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#include "pathnames.h" 74 75#define FATAL 1 /* fatal error occurred */ 76#define NONFATAL 0 /* non fatal error occurred */ 77 78/* 79 * The structure for each interface. 80 */ 81struct if_info { 82 int ii_fd; /* BPF file descriptor */ 83 u_char ii_eaddr[6]; /* Ethernet address of this interface */ 84 u_long ii_ipaddr; /* IP address of this interface */ 85 u_long ii_netmask; /* subnet or net mask */ 86 struct if_info *ii_next; 87}; 88/* 89 * The list of all interfaces that are being listened to. rarp_loop() 90 * "selects" on the descriptors in this list. 91 */ 92struct if_info *iflist; 93 94u_int32_t choose_ipaddr __P((u_int32_t **, u_int32_t, u_int32_t)); 95void debug __P((const char *,...)); 96void init_all __P((void)); 97void init_one __P((char *)); 98u_long ipaddrtonetmask __P((u_long)); 99void lookup_eaddr __P((char *, u_char *)); 100void lookup_ipaddr __P((char *, u_long *, u_long *)); 101int main __P((int, char **)); 102void rarp_loop __P((void)); 103int rarp_open __P((char *)); 104void rarp_process __P((struct if_info *, u_char *)); 105void rarp_reply __P((struct if_info *, struct ether_header *, u_long, 106 struct hostent *)); 107void rarperr __P((int, const char *,...)); 108 109#if defined(__NetBSD__) 110#include "mkarp.h" 111#else 112void update_arptab __P((u_char *, u_long)); 113#endif 114 115void usage __P((void)); 116 117static int bpf_open __P((void)); 118static int rarp_check __P((u_char *, int)); 119 120#ifdef REQUIRE_TFTPBOOT 121int rarp_bootable __P((u_long)); 122#endif 123 124int aflag = 0; /* listen on "all" interfaces */ 125int dflag = 0; /* print debugging messages */ 126int fflag = 0; /* don't fork */ 127int lflag = 0; /* log all replies */ 128 129int 130main(argc, argv) 131 int argc; 132 char **argv; 133{ 134 extern char *__progname; 135 136 int op; 137 char *ifname, *hostname; 138 139 /* All error reporting is done through syslogs. */ 140 openlog(__progname, LOG_PID, LOG_DAEMON); 141 142 opterr = 0; 143 while ((op = getopt(argc, argv, "adfl")) != -1) { 144 switch (op) { 145 case 'a': 146 ++aflag; 147 break; 148 149 case 'd': 150 ++dflag; 151 break; 152 153 case 'f': 154 ++fflag; 155 break; 156 157 case 'l': 158 ++lflag; 159 break; 160 161 default: 162 usage(); 163 /* NOTREACHED */ 164 } 165 } 166 ifname = argv[optind++]; 167 hostname = ifname ? argv[optind] : 0; 168 if ((aflag && ifname) || (!aflag && ifname == 0)) 169 usage(); 170 171 if (aflag) 172 init_all(); 173 else 174 init_one(ifname); 175 176 if ((!fflag) && (!dflag)) { 177 FILE *fp; 178 179 if (daemon(0, 0)) 180 rarperr(FATAL, "daemon"); 181 182 /* write pid file */ 183 if ((fp = fopen(_PATH_RARPDPID, "w")) != NULL) { 184 fprintf(fp, "%u\n", getpid()); 185 (void)fclose(fp); 186 } 187 } 188 rarp_loop(); 189 /* NOTREACHED */ 190 return (0); 191} 192 193/* 194 * Add 'ifname' to the interface list. Lookup its IP address and network 195 * mask and Ethernet address, and open a BPF file for it. 196 */ 197void 198init_one(ifname) 199 char *ifname; 200{ 201 struct if_info *p; 202 int fd; 203 204 fd = rarp_open(ifname); 205 if (fd < 0) 206 return; 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 = fd; 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 */ 226void 227init_all() 228{ 229 char inbuf[8192*2]; 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 268void 269usage() 270{ 271 (void) fprintf(stderr, "usage: rarpd -a [-d|-f] [-l]\n"); 272 (void) fprintf(stderr, " rarpd [-d|-f] [-l] interface\n"); 273 exit(1); 274} 275 276static int 277bpf_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 */ 299int 300rarp_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 - 1); 333 ifr.ifr_name[sizeof ifr.ifr_name - 1] = '\0'; 334 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) { 335 if (aflag) { /* for -a skip non-ethernet interfaces */ 336 close(fd); 337 return(-1); 338 } 339 rarperr(FATAL, "BIOCSETIF: %s", strerror(errno)); 340 /* NOTREACHED */ 341 } 342 /* Check that the data link layer is an Ethernet; this code won't work 343 * with anything else. */ 344 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) { 345 rarperr(FATAL, "BIOCGDLT: %s", strerror(errno)); 346 /* NOTREACHED */ 347 } 348 if (dlt != DLT_EN10MB) { 349 if (aflag) { /* for -a skip non-ethernet interfaces */ 350 close(fd); 351 return(-1); 352 } 353 rarperr(FATAL, "%s is not an ethernet", device); 354 /* NOTREACHED */ 355 } 356 /* Set filter program. */ 357 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) { 358 rarperr(FATAL, "BIOCSETF: %s", strerror(errno)); 359 /* NOTREACHED */ 360 } 361 return fd; 362} 363/* 364 * Perform various sanity checks on the RARP request packet. Return 365 * false on failure and log the reason. 366 */ 367static int 368rarp_check(p, len) 369 u_char *p; 370 int len; 371{ 372 struct ether_header *ep = (struct ether_header *) p; 373#ifdef __NetBSD__ 374 struct arphdr *ap = (struct arphdr *) (p + sizeof(*ep)); 375#else 376 struct ether_arp *ap = (struct ether_arp *) (p + sizeof(*ep)); 377#endif 378 379 if (dflag) 380 fprintf(stderr, "got a packet\n"); 381 382 if (len < sizeof(*ep) + sizeof(*ap)) { 383 rarperr(NONFATAL, "truncated request"); 384 return 0; 385 } 386#ifdef __NetBSD__ 387 /* now that we know the fixed part of the ARP hdr is there: */ 388 if (len < sizeof(*ap) + 2 * ap->ar_hln + 2 * ap->ar_pln) { 389 rarperr(NONFATAL, "truncated request"); 390 return 0; 391 } 392#endif 393 /* XXX This test might be better off broken out... */ 394#ifdef __FreeBSD__ 395 /* BPF (incorrectly) returns this in host order. */ 396 if (ep->ether_type != ETHERTYPE_REVARP || 397#else 398 if (ntohs (ep->ether_type) != ETHERTYPE_REVARP || 399#endif 400#ifdef __NetBSD__ 401 ntohs (ap->ar_hrd) != ARPHRD_ETHER || 402 ntohs (ap->ar_op) != ARPOP_REVREQUEST || 403 ntohs (ap->ar_pro) != ETHERTYPE_IP || 404 ap->ar_hln != 6 || ap->ar_pln != 4) { 405#else 406 ntohs (ap->arp_hrd) != ARPHRD_ETHER || 407 ntohs (ap->arp_op) != ARPOP_REVREQUEST || 408 ntohs (ap->arp_pro) != ETHERTYPE_IP || 409 ap->arp_hln != 6 || ap->arp_pln != 4) { 410#endif 411 rarperr(NONFATAL, "request fails sanity check"); 412 return 0; 413 } 414#ifdef __NetBSD__ 415 if (memcmp((char *) &ep->ether_shost, ar_sha(ap), 6) != 0) { 416#else 417 if (memcmp((char *) &ep->ether_shost, ap->arp_sha, 6) != 0) { 418#endif 419 rarperr(NONFATAL, "ether/arp sender address mismatch"); 420 return 0; 421 } 422#ifdef __NetBSD__ 423 if (memcmp(ar_sha(ap), ar_tha(ap), 6) != 0) { 424#else 425 if (memcmp((char *) &ap->arp_sha, (char *) &ap->arp_tha, 6) != 0) { 426#endif 427 rarperr(NONFATAL, "ether/arp target address mismatch"); 428 return 0; 429 } 430 return 1; 431} 432 433/* 434 * Loop indefinitely listening for RARP requests on the 435 * interfaces in 'iflist'. 436 */ 437void 438rarp_loop() 439{ 440 u_char *buf, *bp, *ep; 441 int cc, fd; 442 fd_set fds, listeners; 443 int bufsize, maxfd = 0; 444 struct if_info *ii; 445 446 if (iflist == 0) { 447 rarperr(FATAL, "no interfaces"); 448 /* NOTREACHED */ 449 } 450 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) { 451 rarperr(FATAL, "BIOCGBLEN: %s", strerror(errno)); 452 /* NOTREACHED */ 453 } 454 buf = (u_char *) malloc((unsigned) bufsize); 455 if (buf == 0) { 456 rarperr(FATAL, "malloc: %s", strerror(errno)); 457 /* NOTREACHED */ 458 } 459 /* 460 * Find the highest numbered file descriptor for select(). 461 * Initialize the set of descriptors to listen to. 462 */ 463 FD_ZERO(&fds); 464 for (ii = iflist; ii; ii = ii->ii_next) { 465 FD_SET(ii->ii_fd, &fds); 466 if (ii->ii_fd > maxfd) 467 maxfd = ii->ii_fd; 468 } 469 while (1) { 470 listeners = fds; 471 if (select(maxfd + 1, &listeners, (struct fd_set *) 0, 472 (struct fd_set *) 0, (struct timeval *) 0) < 0) { 473 rarperr(FATAL, "select: %s", strerror(errno)); 474 /* NOTREACHED */ 475 } 476 for (ii = iflist; ii; ii = ii->ii_next) { 477 fd = ii->ii_fd; 478 if (!FD_ISSET(fd, &listeners)) 479 continue; 480 again: 481 cc = read(fd, (char *) buf, bufsize); 482 /* Don't choke when we get ptraced */ 483 if (cc < 0 && errno == EINTR) 484 goto again; 485 /* Due to a SunOS bug, after 2^31 bytes, the file 486 * offset overflows and read fails with EINVAL. The 487 * lseek() to 0 will fix things. */ 488 if (cc < 0) { 489 if (errno == EINVAL && 490 (lseek(fd, 0, SEEK_CUR) + bufsize) < 0) { 491 (void)lseek(fd, 0, 0); 492 goto again; 493 } 494 rarperr(FATAL, "read: %s", strerror(errno)); 495 /* NOTREACHED */ 496 } 497 /* Loop through the packet(s) */ 498#define bhp ((struct bpf_hdr *)bp) 499 bp = buf; 500 ep = bp + cc; 501 while (bp < ep) { 502 register int caplen, hdrlen; 503 504 caplen = bhp->bh_caplen; 505 hdrlen = bhp->bh_hdrlen; 506 if (rarp_check(bp + hdrlen, caplen)) 507 rarp_process(ii, bp + hdrlen); 508 bp += BPF_WORDALIGN(hdrlen + caplen); 509 } 510 } 511 } 512} 513 514#ifdef REQUIRE_TFTPBOOT 515 516#ifndef TFTP_DIR 517#define TFTP_DIR "/tftpboot" 518#endif 519 520/* 521 * True if this server can boot the host whose IP address is 'addr'. 522 * This check is made by looking in the tftp directory for the 523 * configuration file. 524 */ 525int 526rarp_bootable(addr) 527 u_long addr; 528{ 529 register struct dirent *dent; 530 register DIR *d; 531 char ipname[9]; 532 static DIR *dd = 0; 533 534 (void)sprintf(ipname, "%08X", addr); 535 /* If directory is already open, rewind it. Otherwise, open it. */ 536 if (d = dd) 537 rewinddir(d); 538 else { 539 if (chdir(TFTP_DIR) == -1) { 540 rarperr(FATAL, "chdir: %s", strerror(errno)); 541 /* NOTREACHED */ 542 } 543 d = opendir("."); 544 if (d == 0) { 545 rarperr(FATAL, "opendir: %s", strerror(errno)); 546 /* NOTREACHED */ 547 } 548 dd = d; 549 } 550 while (dent = readdir(d)) 551 if (strncmp(dent->d_name, ipname, 8) == 0) 552 return 1; 553 return 0; 554} 555#endif /* REQUIRE_TFTPBOOT */ 556 557/* 558 * Given a list of IP addresses, 'alist', return the first address that 559 * is on network 'net'; 'netmask' is a mask indicating the network portion 560 * of the address. 561 */ 562u_int32_t 563choose_ipaddr(alist, net, netmask) 564 u_int32_t **alist; 565 u_int32_t net; 566 u_int32_t netmask; 567{ 568 569 for (; *alist; ++alist) { 570 if ((**alist & netmask) == net) 571 return **alist; 572 } 573 return 0; 574} 575/* 576 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has 577 * already been checked for validity. The reply is overlaid on the request. 578 */ 579void 580rarp_process(ii, pkt) 581 struct if_info *ii; 582 u_char *pkt; 583{ 584 struct ether_header *ep; 585 struct hostent *hp; 586 u_long target_ipaddr; 587 char ename[MAXHOSTNAMELEN + 1]; 588 struct in_addr in; 589 590 ep = (struct ether_header *) pkt; 591 592 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) { 593 debug("no IP address for %s", 594 ether_ntoa((struct ether_addr *)&ep->ether_shost)); 595 return; 596 } 597 ename[sizeof(ename)-1] = '\0'; 598 599 if ((hp = gethostbyname(ename)) == 0) { 600 debug("gethostbyname(%s) failed: %s", ename, 601 hstrerror(h_errno)); 602 return; 603 } 604 605 /* Choose correct address from list. */ 606 if (hp->h_addrtype != AF_INET) { 607 rarperr(FATAL, "cannot handle non IP addresses"); 608 /* NOTREACHED */ 609 } 610 target_ipaddr = choose_ipaddr((u_int32_t **) hp->h_addr_list, 611 ii->ii_ipaddr & ii->ii_netmask, ii->ii_netmask); 612 613 if (target_ipaddr == 0) { 614 615 in.s_addr = ii->ii_ipaddr & ii->ii_netmask; 616 rarperr(NONFATAL, "cannot find %s on net %s", 617 ename, inet_ntoa(in)); 618 return; 619 } 620#ifdef REQUIRE_TFTPBOOT 621 if (rarp_bootable(htonl(target_ipaddr))) 622#endif 623 rarp_reply(ii, ep, target_ipaddr, hp); 624#ifdef REQUIRE_TFTPBOOT 625 else 626 debug("%08X not bootable", htonl(target_ipaddr)); 627#endif 628} 629/* 630 * Lookup the ethernet address of the interface attached to the BPF 631 * file descriptor 'fd'; return it in 'eaddr'. 632 */ 633void 634lookup_eaddr(ifname, eaddr) 635 char *ifname; 636 u_char *eaddr; 637{ 638 char inbuf[8192*2]; 639 struct ifconf ifc; 640 struct ifreq *ifr; 641 struct sockaddr_dl *sdl; 642 int fd; 643 int i, len; 644 645 /* We cannot use SIOCGIFADDR on the BPF descriptor. 646 We must instead get all the interfaces with SIOCGIFCONF 647 and find the right one. */ 648 649 /* Use datagram socket to get Ethernet address. */ 650 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 651 rarperr(FATAL, "socket: %s", strerror(errno)); 652 /* NOTREACHED */ 653 } 654 655 ifc.ifc_len = sizeof(inbuf); 656 ifc.ifc_buf = inbuf; 657 if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || 658 ifc.ifc_len < sizeof(struct ifreq)) { 659 rarperr(FATAL, "lookup_eaddr: SIOGIFCONF: %s", strerror(errno)); 660 /* NOTREACHED */ 661 } 662 ifr = ifc.ifc_req; 663 for (i = 0; i < ifc.ifc_len; 664 i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) { 665 len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len; 666 sdl = (struct sockaddr_dl *)&ifr->ifr_addr; 667 if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || 668 sdl->sdl_alen != 6) 669 continue; 670 if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) { 671 memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6); 672 debug("%s: %x:%x:%x:%x:%x:%x", 673 ifr->ifr_name, eaddr[0], eaddr[1], 674 eaddr[2], eaddr[3], eaddr[4], eaddr[5]); 675 return; 676 } 677 } 678 679 rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname); 680} 681/* 682 * Lookup the IP address and network mask of the interface named 'ifname'. 683 */ 684void 685lookup_ipaddr(ifname, addrp, netmaskp) 686 char *ifname; 687 u_long *addrp; 688 u_long *netmaskp; 689{ 690 int fd; 691 struct ifreq ifr; 692 693 /* Use datagram socket to get IP address. */ 694 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 695 rarperr(FATAL, "socket: %s", strerror(errno)); 696 /* NOTREACHED */ 697 } 698 (void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name); 699 if (ioctl(fd, SIOCGIFADDR, (char *) &ifr) < 0) { 700 rarperr(FATAL, "SIOCGIFADDR: %s", strerror(errno)); 701 /* NOTREACHED */ 702 } 703 *addrp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr; 704 if (ioctl(fd, SIOCGIFNETMASK, (char *) &ifr) < 0) { 705 perror("SIOCGIFNETMASK"); 706 unlink(_PATH_RARPDPID); 707 exit(1); 708 } 709 *netmaskp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr; 710 /* If SIOCGIFNETMASK didn't work, figure out a mask from the IP 711 * address class. */ 712 if (*netmaskp == 0) 713 *netmaskp = ipaddrtonetmask(*addrp); 714 715 (void)close(fd); 716} 717/* 718 * Poke the kernel arp tables with the ethernet/ip address combinataion 719 * given. When processing a reply, we must do this so that the booting 720 * host (i.e. the guy running rarpd), won't try to ARP for the hardware 721 * address of the guy being booted (he cannot answer the ARP). 722 */ 723#ifndef __NetBSD__ 724void 725update_arptab(ep, ipaddr) 726 u_char *ep; 727 u_long 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 */ 786void 787rarp_reply(ii, ep, ipaddr, hp) 788 struct if_info *ii; 789 struct ether_header *ep; 790 u_long ipaddr; 791 struct hostent *hp; 792{ 793 int n; 794#ifdef __NetBSD__ 795 struct arphdr *ap = (struct arphdr *) (ep + 1); 796#else 797 struct ether_arp *ap = (struct ether_arp *) (ep + 1); 798#endif 799 800 int len; 801 802#ifdef __NetBSD__ 803 (void)mkarp(ar_sha(ap), ipaddr); 804#else 805 update_arptab((u_char *) & ap->arp_sha, ipaddr); 806#endif 807 808 /* Build the rarp reply by modifying the rarp request in place. */ 809#ifdef __FreeBSD__ 810 /* BPF (incorrectly) wants this in host order. */ 811 ep->ether_type = ETHERTYPE_REVARP; 812#else 813 ep->ether_type = htons(ETHERTYPE_REVARP); 814#endif 815#ifdef __NetBSD__ 816 ap->ar_hrd = htons(ARPHRD_ETHER); 817 ap->ar_pro = htons(ETHERTYPE_IP); 818 ap->ar_op = htons(ARPOP_REVREPLY); 819 820 memmove((char *) &ep->ether_dhost, ar_sha(ap), 6); 821 memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6); 822 memmove(ar_sha(ap), (char *) ii->ii_eaddr, 6); 823 824 memmove(ar_tpa(ap), (char *) &ipaddr, 4); 825 /* Target hardware is unchanged. */ 826 memmove(ar_spa(ap), (char *) &ii->ii_ipaddr, 4); 827 828 len = sizeof(*ep) + sizeof(*ap) + 829 2 * ap->ar_pln + 2 * ap->ar_hln; 830#else 831 ap->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); 832 ap->ea_hdr.ar_pro = htons(ETHERTYPE_IP); 833 ap->arp_op = htons(ARPOP_REVREPLY); 834 835 memmove((char *) &ep->ether_dhost, (char *) &ap->arp_sha, 6); 836 memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6); 837 memmove((char *) &ap->arp_sha, (char *) ii->ii_eaddr, 6); 838 839 memmove((char *) ap->arp_tpa, (char *) &ipaddr, 4); 840 /* Target hardware is unchanged. */ 841 memmove((char *) ap->arp_spa, (char *) &ii->ii_ipaddr, 4); 842 843 len = sizeof(*ep) + sizeof(*ap); 844#endif 845 846 if (lflag) 847 syslog(LOG_INFO, "%s asked; %s replied", hp->h_name, 848 ether_ntoa((struct ether_addr *)ar_tha(ap))); 849 n = write(ii->ii_fd, (char *) ep, len); 850 if (n != len) { 851 rarperr(NONFATAL, "write: only %d of %d bytes written", n, len); 852 } 853} 854/* 855 * Get the netmask of an IP address. This routine is used if 856 * SIOCGIFNETMASK doesn't work. 857 */ 858u_long 859ipaddrtonetmask(addr) 860 u_long addr; 861{ 862 863 if (IN_CLASSA(addr)) 864 return IN_CLASSA_NET; 865 if (IN_CLASSB(addr)) 866 return IN_CLASSB_NET; 867 if (IN_CLASSC(addr)) 868 return IN_CLASSC_NET; 869 rarperr(FATAL, "unknown IP address class: %08X", addr); 870 /* NOTREACHED */ 871 return(-1); 872} 873 874#if __STDC__ 875#include <stdarg.h> 876#else 877#include <varargs.h> 878#endif 879 880void 881#if __STDC__ 882rarperr(int fatal, const char *fmt,...) 883#else 884rarperr(fmt, va_alist) 885 int fatal; 886 char *fmt; 887va_dcl 888#endif 889{ 890 va_list ap; 891 892#if __STDC__ 893 va_start(ap, fmt); 894#else 895 va_start(ap); 896#endif 897 if (dflag) { 898 if (fatal) 899 (void)fprintf(stderr, "rarpd: error: "); 900 else 901 (void)fprintf(stderr, "rarpd: warning: "); 902 (void)vfprintf(stderr, fmt, ap); 903 (void)fprintf(stderr, "\n"); 904 } 905 vsyslog(LOG_ERR, fmt, ap); 906 va_end(ap); 907 if (fatal) { 908 unlink(_PATH_RARPDPID); 909 exit(1); 910 } 911 /* NOTREACHED */ 912} 913 914void 915#if __STDC__ 916debug(const char *fmt,...) 917#else 918debug(fmt, va_alist) 919 char *fmt; 920va_dcl 921#endif 922{ 923 va_list ap; 924 925#if __STDC__ 926 va_start(ap, fmt); 927#else 928 va_start(ap); 929#endif 930 if (dflag) { 931 (void)fprintf(stderr, "rarpd: "); 932 (void)vfprintf(stderr, fmt, ap); 933 (void)fprintf(stderr, "\n"); 934 } 935 vsyslog(LOG_WARNING, fmt, ap); 936 va_end(ap); 937} 938