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