rarpd.c revision 78163
1/* 2 * Copyright (c) 1990, 1991, 1992, 1993, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 22#ifndef lint 23static const char copyright[] = 24"@(#) Copyright (c) 1990, 1991, 1992, 1993, 1996\n\ 25The Regents of the University of California. All rights reserved.\n"; 26#endif /* not lint */ 27 28#ifndef lint 29static const char rcsid[] = 30 "$FreeBSD: head/usr.sbin/rarpd/rarpd.c 78163 2001-06-13 11:56:00Z bde $"; 31#endif /* not lint */ 32 33/* 34 * rarpd - Reverse ARP Daemon 35 * 36 * Usage: rarpd -a [ -fsv ] [ hostname ] 37 * rarpd [ -fsv ] interface [ hostname ] 38 * 39 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd. 40 * Currently, the argument is ignored. 41 */ 42#include <sys/param.h> 43#include <sys/file.h> 44#include <sys/ioctl.h> 45#include <sys/socket.h> 46#include <sys/time.h> 47 48#include <net/bpf.h> 49#include <net/ethernet.h> 50#include <net/if.h> 51#include <net/if_types.h> 52#include <net/if_dl.h> 53#include <net/route.h> 54 55#include <netinet/in.h> 56#include <netinet/if_ether.h> 57 58#include <arpa/inet.h> 59 60#include <errno.h> 61#include <netdb.h> 62#include <stdio.h> 63#include <string.h> 64#include <syslog.h> 65#include <stdlib.h> 66#include <unistd.h> 67 68#if defined(SUNOS4) || defined(__FreeBSD__) /* XXX */ 69#define HAVE_DIRENT_H 70#endif 71 72#ifdef HAVE_DIRENT_H 73#include <dirent.h> 74#else 75#include <sys/dir.h> 76#endif 77 78/* Cast a struct sockaddr to a structaddr_in */ 79#define SATOSIN(sa) ((struct sockaddr_in *)(sa)) 80 81#ifndef TFTP_DIR 82#define TFTP_DIR "/tftpboot" 83#endif 84 85#if BSD >= 199200 86#define ARPSECS (20 * 60) /* as per code in netinet/if_ether.c */ 87#define REVARP_REQUEST ARPOP_REVREQUEST 88#define REVARP_REPLY ARPOP_REVREPLY 89#endif 90 91#ifndef ETHERTYPE_REVARP 92#define ETHERTYPE_REVARP 0x8035 93#define REVARP_REQUEST 3 94#define REVARP_REPLY 4 95#endif 96 97/* 98 * Map field names in ether_arp struct. What a pain in the neck. 99 */ 100#ifdef SUNOS3 101#undef arp_sha 102#undef arp_spa 103#undef arp_tha 104#undef arp_tpa 105#define arp_sha arp_xsha 106#define arp_spa arp_xspa 107#define arp_tha arp_xtha 108#define arp_tpa arp_xtpa 109#endif 110 111/* 112 * The structure for each interface. 113 */ 114struct if_info { 115 struct if_info *ii_next; 116 int ii_fd; /* BPF file descriptor */ 117 u_long ii_ipaddr; /* IP address of this interface */ 118 u_long ii_netmask; /* subnet or net mask */ 119 u_char ii_eaddr[6]; /* Ethernet address of this interface */ 120 char ii_ifname[sizeof(((struct ifreq *)0)->ifr_name) + 1]; 121}; 122 123/* 124 * The list of all interfaces that are being listened to. rarp_loop() 125 * "selects" on the descriptors in this list. 126 */ 127struct if_info *iflist; 128 129int verbose; /* verbose messages */ 130int s; /* inet datagram socket */ 131const char *tftp_dir = TFTP_DIR; /* tftp directory */ 132 133int sflag; /* ignore /tftpboot */ 134 135static u_char zero[6]; 136 137static int bpf_open(void); 138static u_long choose_ipaddr(u_long **, u_long, u_long); 139static char *eatoa(u_char *); 140static void init(char *); 141static void init_one(struct ifreq *, char *); 142static char *intoa(u_long); 143static u_long ipaddrtonetmask(u_long); 144static int rarp_bootable(u_long); 145static int rarp_check(u_char *, u_int); 146static void rarp_loop(void); 147static int rarp_open(char *); 148static void rarp_process(struct if_info *, u_char *, u_int); 149static void rarp_reply(struct if_info *, struct ether_header *, u_long, u_int); 150static void update_arptab(u_char *, u_long); 151static void usage(void); 152 153int 154main(int argc, char *argv[]) 155{ 156 int op; 157 char *ifname, *hostname, *name; 158 159 int aflag = 0; /* listen on "all" interfaces */ 160 int fflag = 0; /* don't fork */ 161 162 if ((name = strrchr(argv[0], '/')) != NULL) 163 ++name; 164 else 165 name = argv[0]; 166 if (*name == '-') 167 ++name; 168 169 /* 170 * All error reporting is done through syslogs. 171 */ 172 openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON); 173 174 opterr = 0; 175 while ((op = getopt(argc, argv, "afsv")) != -1) { 176 switch (op) { 177 case 'a': 178 ++aflag; 179 break; 180 181 case 'f': 182 ++fflag; 183 break; 184 185 case 's': 186 ++sflag; 187 break; 188 189 case 'v': 190 ++verbose; 191 break; 192 193 default: 194 usage(); 195 /* NOTREACHED */ 196 } 197 } 198 ifname = argv[optind++]; 199 hostname = ifname ? argv[optind] : NULL; 200 if ((aflag && ifname) || (!aflag && ifname == NULL)) 201 usage(); 202 203 if (aflag) 204 init(NULL); 205 else 206 init(ifname); 207 208 if (!fflag) { 209 if (daemon(0,0)) { 210 syslog(LOG_ERR, "cannot fork"); 211 exit(1); 212 } 213 } 214 rarp_loop(); 215 return(0); 216} 217 218/* 219 * Add to the interface list. 220 */ 221void 222init_one(struct ifreq *ifrp, char *target) 223{ 224 struct if_info *ii; 225 struct sockaddr_dl *ll; 226 int family; 227 struct ifreq ifr; 228 229 family = ifrp->ifr_addr.sa_family; 230 switch (family) { 231 232 case AF_INET: 233#if BSD >= 199100 234 case AF_LINK: 235#endif 236 (void)strncpy(ifr.ifr_name, ifrp->ifr_name, 237 sizeof(ifrp->ifr_name)); 238 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) == -1) { 239 syslog(LOG_ERR, 240 "SIOCGIFFLAGS: %.*s: %m", 241 (int)sizeof(ifrp->ifr_name), ifrp->ifr_name); 242 exit(1); 243 } 244 if ((ifr.ifr_flags & IFF_UP) == 0 || 245 (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) 246 return; 247 break; 248 249 250 default: 251 return; 252 } 253 254 /* Don't bother going any further if not the target interface */ 255 if (target != NULL && 256 strncmp(ifrp->ifr_name, target, sizeof(ifrp->ifr_name)) != 0) 257 return; 258 259 /* Look for interface in list */ 260 for (ii = iflist; ii != NULL; ii = ii->ii_next) 261 if (strncmp(ifrp->ifr_name, ii->ii_ifname, 262 sizeof(ifrp->ifr_name)) == 0) 263 break; 264 265 /* Allocate a new one if not found */ 266 if (ii == NULL) { 267 ii = (struct if_info *)malloc(sizeof(*ii)); 268 if (ii == NULL) { 269 syslog(LOG_ERR, "malloc: %m"); 270 exit(1); 271 } 272 bzero(ii, sizeof(*ii)); 273 ii->ii_fd = -1; 274 (void)strncpy(ii->ii_ifname, ifrp->ifr_name, 275 sizeof(ifrp->ifr_name)); 276 ii->ii_ifname[sizeof(ii->ii_ifname) - 1] = '\0'; 277 ii->ii_next = iflist; 278 iflist = ii; 279 } 280 281 switch (family) { 282 283 case AF_INET: 284 if (ioctl(s, SIOCGIFADDR, (char *)&ifr) == -1) { 285 syslog(LOG_ERR, "ipaddr SIOCGIFADDR: %s: %m", 286 ii->ii_ifname); 287 exit(1); 288 } 289 ii->ii_ipaddr = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr; 290 if (ioctl(s, SIOCGIFNETMASK, (char *)&ifr) == -1) { 291 syslog(LOG_ERR, "SIOCGIFNETMASK: %m"); 292 exit(1); 293 } 294 ii->ii_netmask = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr; 295 if (ii->ii_netmask == 0) 296 ii->ii_netmask = ipaddrtonetmask(ii->ii_ipaddr); 297 if (ii->ii_fd < 0) { 298 ii->ii_fd = rarp_open(ii->ii_ifname); 299#if BSD < 199100 300 /* Use BPF descriptor to get ethernet address. */ 301 if (ioctl(ii->ii_fd, SIOCGIFADDR, (char *)&ifr) == -1) { 302 syslog(LOG_ERR, "eaddr SIOCGIFADDR: %s: %m", 303 ii->ii_ifname); 304 exit(1); 305 } 306 bcopy(&ifr.ifr_addr.sa_data[0], ii->ii_eaddr, 6); 307#endif 308 } 309 break; 310 311#if BSD >= 199100 312 case AF_LINK: 313 ll = (struct sockaddr_dl *)&ifrp->ifr_addr; 314 if (ll->sdl_type == IFT_ETHER) 315 bcopy(LLADDR(ll), ii->ii_eaddr, 6); 316 break; 317#endif 318 } 319} 320/* 321 * Initialize all "candidate" interfaces that are in the system 322 * configuration list. A "candidate" is up, not loopback and not 323 * point to point. 324 */ 325void 326init(char *target) 327{ 328 u_int n; 329 struct ifreq *ifrp, *ifend; 330 struct if_info *ii, *nii, *lii; 331 struct ifconf ifc; 332 struct ifreq ibuf[16]; 333 334 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 335 syslog(LOG_ERR, "socket: %m"); 336 exit(1); 337 } 338 ifc.ifc_len = sizeof ibuf; 339 ifc.ifc_buf = (caddr_t)ibuf; 340 if ((ioctl(s, SIOCGIFCONF, (char *)&ifc) == -1) || 341 ((u_int)ifc.ifc_len < sizeof(struct ifreq))) { 342 syslog(LOG_ERR, "SIOCGIFCONF: %m"); 343 exit(1); 344 } 345 ifrp = ibuf; 346 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 347 while (ifrp < ifend) { 348 init_one(ifrp, target); 349 350#if BSD >= 199100 351 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); 352 if (n < sizeof(*ifrp)) 353 n = sizeof(*ifrp); 354 ifrp = (struct ifreq *)((char *)ifrp + n); 355#else 356 ++ifrp; 357#endif 358 } 359 360 /* Throw away incomplete interfaces */ 361 lii = NULL; 362 for (ii = iflist; ii != NULL; ii = nii) { 363 nii = ii->ii_next; 364 if (ii->ii_ipaddr == 0 || 365 bcmp(ii->ii_eaddr, zero, 6) == 0) { 366 if (lii == NULL) 367 iflist = nii; 368 else 369 lii->ii_next = nii; 370 if (ii->ii_fd >= 0) 371 close(ii->ii_fd); 372 free(ii); 373 continue; 374 } 375 lii = ii; 376 } 377 378 /* Verbose stuff */ 379 if (verbose) 380 for (ii = iflist; ii != NULL; ii = ii->ii_next) 381 syslog(LOG_DEBUG, "%s %s 0x%08lx %s", 382 ii->ii_ifname, intoa(ntohl(ii->ii_ipaddr)), 383 (u_long)ntohl(ii->ii_netmask), eatoa(ii->ii_eaddr)); 384} 385 386void 387usage(void) 388{ 389 (void)fprintf(stderr, "usage: rarpd [-afsv] [interface]\n"); 390 exit(1); 391} 392 393int 394bpf_open(void) 395{ 396 int fd; 397 int n = 0; 398 char device[sizeof "/dev/bpf000"]; 399 400 /* 401 * Go through all the minors and find one that isn't in use. 402 */ 403 do { 404 (void)sprintf(device, "/dev/bpf%d", n++); 405 fd = open(device, O_RDWR); 406 } while ((fd == -1) && (errno == EBUSY)); 407 408 if (fd == -1) { 409 syslog(LOG_ERR, "%s: %m", device); 410 exit(1); 411 } 412 return fd; 413} 414 415/* 416 * Open a BPF file and attach it to the interface named 'device'. 417 * Set immediate mode, and set a filter that accepts only RARP requests. 418 */ 419int 420rarp_open(char *device) 421{ 422 int fd; 423 struct ifreq ifr; 424 u_int dlt; 425 int immediate; 426 427 static struct bpf_insn insns[] = { 428 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12), 429 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3), 430 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20), 431 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1), 432 BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) + 433 sizeof(struct ether_header)), 434 BPF_STMT(BPF_RET|BPF_K, 0), 435 }; 436 static struct bpf_program filter = { 437 sizeof insns / sizeof(insns[0]), 438 insns 439 }; 440 441 fd = bpf_open(); 442 /* 443 * Set immediate mode so packets are processed as they arrive. 444 */ 445 immediate = 1; 446 if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) { 447 syslog(LOG_ERR, "BIOCIMMEDIATE: %m"); 448 exit(1); 449 } 450 (void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name); 451 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) { 452 syslog(LOG_ERR, "BIOCSETIF: %m"); 453 exit(1); 454 } 455 /* 456 * Check that the data link layer is an Ethernet; this code won't 457 * work with anything else. 458 */ 459 if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) { 460 syslog(LOG_ERR, "BIOCGDLT: %m"); 461 exit(1); 462 } 463 if (dlt != DLT_EN10MB) { 464 syslog(LOG_ERR, "%s is not an ethernet", device); 465 exit(1); 466 } 467 /* 468 * Set filter program. 469 */ 470 if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) { 471 syslog(LOG_ERR, "BIOCSETF: %m"); 472 exit(1); 473 } 474 return fd; 475} 476 477/* 478 * Perform various sanity checks on the RARP request packet. Return 479 * false on failure and log the reason. 480 */ 481int 482rarp_check(u_char *p, u_int len) 483{ 484 struct ether_header *ep = (struct ether_header *)p; 485 struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep)); 486 487 if (len < sizeof(*ep) + sizeof(*ap)) { 488 syslog(LOG_ERR, "truncated request, got %d, expected %d", 489 len, sizeof(*ep) + sizeof(*ap)); 490 return 0; 491 } 492 /* 493 * XXX This test might be better off broken out... 494 */ 495 if (ntohs(ep->ether_type) != ETHERTYPE_REVARP || 496 ntohs(ap->arp_hrd) != ARPHRD_ETHER || 497 ntohs(ap->arp_op) != REVARP_REQUEST || 498 ntohs(ap->arp_pro) != ETHERTYPE_IP || 499 ap->arp_hln != 6 || ap->arp_pln != 4) { 500 syslog(LOG_DEBUG, "request fails sanity check"); 501 return 0; 502 } 503 if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) { 504 syslog(LOG_DEBUG, "ether/arp sender address mismatch"); 505 return 0; 506 } 507 if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) { 508 syslog(LOG_DEBUG, "ether/arp target address mismatch"); 509 return 0; 510 } 511 return 1; 512} 513 514#ifndef FD_SETSIZE 515#define FD_SET(n, fdp) ((fdp)->fds_bits[0] |= (1 << (n))) 516#define FD_ISSET(n, fdp) ((fdp)->fds_bits[0] & (1 << (n))) 517#define FD_ZERO(fdp) ((fdp)->fds_bits[0] = 0) 518#endif 519 520/* 521 * Loop indefinitely listening for RARP requests on the 522 * interfaces in 'iflist'. 523 */ 524void 525rarp_loop(void) 526{ 527 u_char *buf, *bp, *ep; 528 int cc, fd; 529 fd_set fds, listeners; 530 int bufsize, maxfd = 0; 531 struct if_info *ii; 532 533 if (iflist == NULL) { 534 syslog(LOG_ERR, "no interfaces"); 535 exit(1); 536 } 537 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) { 538 syslog(LOG_ERR, "BIOCGBLEN: %m"); 539 exit(1); 540 } 541 buf = malloc(bufsize); 542 if (buf == NULL) { 543 syslog(LOG_ERR, "malloc: %m"); 544 exit(1); 545 } 546 547 while (1) { 548 /* 549 * Find the highest numbered file descriptor for select(). 550 * Initialize the set of descriptors to listen to. 551 */ 552 FD_ZERO(&fds); 553 for (ii = iflist; ii != NULL; ii = ii->ii_next) { 554 FD_SET(ii->ii_fd, &fds); 555 if (ii->ii_fd > maxfd) 556 maxfd = ii->ii_fd; 557 } 558 listeners = fds; 559 if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) { 560 /* Don't choke when we get ptraced */ 561 if (errno == EINTR) 562 continue; 563 syslog(LOG_ERR, "select: %m"); 564 exit(1); 565 } 566 for (ii = iflist; ii != NULL; ii = ii->ii_next) { 567 fd = ii->ii_fd; 568 if (!FD_ISSET(fd, &listeners)) 569 continue; 570 again: 571 cc = read(fd, (char *)buf, bufsize); 572 /* Don't choke when we get ptraced */ 573 if ((cc == -1) && (errno == EINTR)) 574 goto again; 575#if defined(SUNOS3) || defined(SUNOS4) 576 /* 577 * Due to a SunOS bug, after 2^31 bytes, the 578 * file offset overflows and read fails with 579 * EINVAL. The lseek() to 0 will fix things. 580 */ 581 if (cc == -1) { 582 if (errno == EINVAL && 583 (long)(tell(fd) + bufsize) < 0) { 584 (void)lseek(fd, 0, 0); 585 goto again; 586 } 587 syslog(LOG_ERR, "read: %m"); 588 exit(1); 589 } 590#endif 591 592 /* Loop through the packet(s) */ 593#define bhp ((struct bpf_hdr *)bp) 594 bp = buf; 595 ep = bp + cc; 596 while (bp < ep) { 597 u_int caplen, hdrlen; 598 599 caplen = bhp->bh_caplen; 600 hdrlen = bhp->bh_hdrlen; 601 if (rarp_check(bp + hdrlen, caplen)) 602 rarp_process(ii, bp + hdrlen, caplen); 603 bp += BPF_WORDALIGN(hdrlen + caplen); 604 } 605 } 606 } 607#undef bhp 608} 609 610/* 611 * True if this server can boot the host whose IP address is 'addr'. 612 * This check is made by looking in the tftp directory for the 613 * configuration file. 614 */ 615int 616rarp_bootable(u_long addr) 617{ 618#ifdef HAVE_DIRENT_H 619 struct dirent *dent; 620#else 621 struct direct *dent; 622#endif 623 DIR *d; 624 char ipname[9]; 625 static DIR *dd = NULL; 626 627 (void)sprintf(ipname, "%08lX", (u_long)ntohl(addr)); 628 629 /* 630 * If directory is already open, rewind it. Otherwise, open it. 631 */ 632 if ((d = dd) != NULL) 633 rewinddir(d); 634 else { 635 if (chdir(tftp_dir) == -1) { 636 syslog(LOG_ERR, "chdir: %s: %m", tftp_dir); 637 exit(1); 638 } 639 d = opendir("."); 640 if (d == NULL) { 641 syslog(LOG_ERR, "opendir: %m"); 642 exit(1); 643 } 644 dd = d; 645 } 646 while ((dent = readdir(d)) != NULL) 647 if (strncmp(dent->d_name, ipname, 8) == 0) 648 return 1; 649 return 0; 650} 651 652/* 653 * Given a list of IP addresses, 'alist', return the first address that 654 * is on network 'net'; 'netmask' is a mask indicating the network portion 655 * of the address. 656 */ 657u_long 658choose_ipaddr(u_long **alist, u_long net, u_long netmask) 659{ 660 for (; *alist; ++alist) 661 if ((**alist & netmask) == net) 662 return **alist; 663 return 0; 664} 665 666/* 667 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has 668 * already been checked for validity. The reply is overlaid on the request. 669 */ 670void 671rarp_process(struct if_info *ii, u_char *pkt, u_int len) 672{ 673 struct ether_header *ep; 674 struct hostent *hp; 675 u_long target_ipaddr; 676 char ename[256]; 677 678 ep = (struct ether_header *)pkt; 679 /* should this be arp_tha? */ 680 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) { 681 syslog(LOG_ERR, "cannot map %s to name", 682 eatoa(ep->ether_shost)); 683 return; 684 } 685 686 if ((hp = gethostbyname(ename)) == NULL) { 687 syslog(LOG_ERR, "cannot map %s to IP address", ename); 688 return; 689 } 690 691 /* 692 * Choose correct address from list. 693 */ 694 if (hp->h_addrtype != AF_INET) { 695 syslog(LOG_ERR, "cannot handle non IP addresses for %s", 696 ename); 697 return; 698 } 699 target_ipaddr = choose_ipaddr((u_long **)hp->h_addr_list, 700 ii->ii_ipaddr & ii->ii_netmask, 701 ii->ii_netmask); 702 if (target_ipaddr == 0) { 703 syslog(LOG_ERR, "cannot find %s on net %s", 704 ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask))); 705 return; 706 } 707 if (sflag || rarp_bootable(target_ipaddr)) 708 rarp_reply(ii, ep, target_ipaddr, len); 709 else if (verbose > 1) 710 syslog(LOG_INFO, "%s %s at %s DENIED (not bootable)", 711 ii->ii_ifname, 712 eatoa(ep->ether_shost), 713 intoa(ntohl(target_ipaddr))); 714} 715 716/* 717 * Poke the kernel arp tables with the ethernet/ip address combinataion 718 * given. When processing a reply, we must do this so that the booting 719 * host (i.e. the guy running rarpd), won't try to ARP for the hardware 720 * address of the guy being booted (he cannot answer the ARP). 721 */ 722#if BSD >= 199200 723struct sockaddr_inarp sin_inarp = { 724 sizeof(struct sockaddr_inarp), AF_INET, 0, 725 {0}, 726 {0}, 727 0, 0 728}; 729struct sockaddr_dl sin_dl = { 730 sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6, 731 0, "", 0, {0} 732}; 733struct { 734 struct rt_msghdr rthdr; 735 char rtspace[512]; 736} rtmsg; 737 738void 739update_arptab(u_char *ep, u_long ipaddr) 740{ 741 int cc; 742 struct sockaddr_inarp *ar, *ar2; 743 struct sockaddr_dl *ll, *ll2; 744 struct rt_msghdr *rt; 745 int xtype, xindex; 746 static pid_t pid; 747 int r; 748 static int seq; 749 750 r = socket(PF_ROUTE, SOCK_RAW, 0); 751 if (r == -1) { 752 syslog(LOG_ERR, "raw route socket: %m"); 753 exit(1); 754 } 755 pid = getpid(); 756 757 ar = &sin_inarp; 758 ar->sin_addr.s_addr = ipaddr; 759 ll = &sin_dl; 760 bcopy(ep, LLADDR(ll), 6); 761 762 /* Get the type and interface index */ 763 rt = &rtmsg.rthdr; 764 bzero(rt, sizeof(rtmsg)); 765 rt->rtm_version = RTM_VERSION; 766 rt->rtm_addrs = RTA_DST; 767 rt->rtm_type = RTM_GET; 768 rt->rtm_seq = ++seq; 769 ar2 = (struct sockaddr_inarp *)rtmsg.rtspace; 770 bcopy(ar, ar2, sizeof(*ar)); 771 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar); 772 errno = 0; 773 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) { 774 syslog(LOG_ERR, "rtmsg get write: %m"); 775 close(r); 776 return; 777 } 778 do { 779 cc = read(r, rt, sizeof(rtmsg)); 780 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid)); 781 if (cc == -1) { 782 syslog(LOG_ERR, "rtmsg get read: %m"); 783 close(r); 784 return; 785 } 786 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len); 787 if (ll2->sdl_family != AF_LINK) { 788 /* 789 * XXX I think this means the ip address is not on a 790 * directly connected network (the family is AF_INET in 791 * this case). 792 */ 793 syslog(LOG_ERR, "bogus link family (%d) wrong net for %08lX?\n", 794 ll2->sdl_family, ipaddr); 795 close(r); 796 return; 797 } 798 xtype = ll2->sdl_type; 799 xindex = ll2->sdl_index; 800 801 /* Set the new arp entry */ 802 bzero(rt, sizeof(rtmsg)); 803 rt->rtm_version = RTM_VERSION; 804 rt->rtm_addrs = RTA_DST | RTA_GATEWAY; 805 rt->rtm_inits = RTV_EXPIRE; 806 rt->rtm_rmx.rmx_expire = time(0) + ARPSECS; 807 rt->rtm_flags = RTF_HOST | RTF_STATIC; 808 rt->rtm_type = RTM_ADD; 809 rt->rtm_seq = ++seq; 810 811 bcopy(ar, ar2, sizeof(*ar)); 812 813 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2)); 814 bcopy(ll, ll2, sizeof(*ll)); 815 ll2->sdl_type = xtype; 816 ll2->sdl_index = xindex; 817 818 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2); 819 errno = 0; 820 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) { 821 syslog(LOG_ERR, "rtmsg add write: %m"); 822 close(r); 823 return; 824 } 825 do { 826 cc = read(r, rt, sizeof(rtmsg)); 827 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid)); 828 close(r); 829 if (cc == -1) { 830 syslog(LOG_ERR, "rtmsg add read: %m"); 831 return; 832 } 833} 834#else 835void 836update_arptab(u_char *ep, u_long ipaddr) 837{ 838 struct arpreq request; 839 struct sockaddr_in *sin; 840 841 request.arp_flags = 0; 842 sin = (struct sockaddr_in *)&request.arp_pa; 843 sin->sin_family = AF_INET; 844 sin->sin_addr.s_addr = ipaddr; 845 request.arp_ha.sa_family = AF_UNSPEC; 846 bcopy((char *)ep, (char *)request.arp_ha.sa_data, 6); 847 848 if (ioctl(s, SIOCSARP, (caddr_t)&request) == -1) 849 syslog(LOG_ERR, "SIOCSARP: %m"); 850} 851#endif 852 853/* 854 * Build a reverse ARP packet and sent it out on the interface. 855 * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built 856 * on top of the request, then written to the network. 857 * 858 * RFC 903 defines the ether_arp fields as follows. The following comments 859 * are taken (more or less) straight from this document. 860 * 861 * REVARP_REQUEST 862 * 863 * arp_sha is the hardware address of the sender of the packet. 864 * arp_spa is undefined. 865 * arp_tha is the 'target' hardware address. 866 * In the case where the sender wishes to determine his own 867 * protocol address, this, like arp_sha, will be the hardware 868 * address of the sender. 869 * arp_tpa is undefined. 870 * 871 * REVARP_REPLY 872 * 873 * arp_sha is the hardware address of the responder (the sender of the 874 * reply packet). 875 * arp_spa is the protocol address of the responder (see the note below). 876 * arp_tha is the hardware address of the target, and should be the same as 877 * that which was given in the request. 878 * arp_tpa is the protocol address of the target, that is, the desired address. 879 * 880 * Note that the requirement that arp_spa be filled in with the responder's 881 * protocol is purely for convenience. For instance, if a system were to use 882 * both ARP and RARP, then the inclusion of the valid protocol-hardware 883 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent 884 * ARP request. 885 */ 886void 887rarp_reply(struct if_info *ii, struct ether_header *ep, u_long ipaddr, 888 u_int len) 889{ 890 u_int n; 891 struct ether_arp *ap = (struct ether_arp *)(ep + 1); 892 893 update_arptab((u_char *)&ap->arp_sha, ipaddr); 894 895 /* 896 * Build the rarp reply by modifying the rarp request in place. 897 */ 898 ap->arp_op = htons(REVARP_REPLY); 899 900#ifdef BROKEN_BPF 901 ep->ether_type = ETHERTYPE_REVARP; 902#endif 903 bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6); 904 bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6); 905 bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6); 906 907 bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4); 908 /* Target hardware is unchanged. */ 909 bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4); 910 911 /* Zero possible garbage after packet. */ 912 bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)), 913 len - (sizeof(*ep) + sizeof(*ap))); 914 n = write(ii->ii_fd, (char *)ep, len); 915 if (n != len) 916 syslog(LOG_ERR, "write: only %d of %d bytes written", n, len); 917 if (verbose) 918 syslog(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname, 919 eatoa(ap->arp_tha), 920 intoa(ntohl(ipaddr))); 921} 922 923/* 924 * Get the netmask of an IP address. This routine is used if 925 * SIOCGIFNETMASK doesn't work. 926 */ 927u_long 928ipaddrtonetmask(u_long addr) 929{ 930 addr = ntohl(addr); 931 if (IN_CLASSA(addr)) 932 return htonl(IN_CLASSA_NET); 933 if (IN_CLASSB(addr)) 934 return htonl(IN_CLASSB_NET); 935 if (IN_CLASSC(addr)) 936 return htonl(IN_CLASSC_NET); 937 syslog(LOG_DEBUG, "unknown IP address class: %08lX", addr); 938 return htonl(0xffffffff); 939} 940 941/* 942 * A faster replacement for inet_ntoa(). 943 */ 944char * 945intoa(u_long addr) 946{ 947 char *cp; 948 u_int byte; 949 int n; 950 static char buf[sizeof(".xxx.xxx.xxx.xxx")]; 951 952 cp = &buf[sizeof buf]; 953 *--cp = '\0'; 954 955 n = 4; 956 do { 957 byte = addr & 0xff; 958 *--cp = byte % 10 + '0'; 959 byte /= 10; 960 if (byte > 0) { 961 *--cp = byte % 10 + '0'; 962 byte /= 10; 963 if (byte > 0) 964 *--cp = byte + '0'; 965 } 966 *--cp = '.'; 967 addr >>= 8; 968 } while (--n > 0); 969 970 return cp + 1; 971} 972 973char * 974eatoa(u_char *ea) 975{ 976 static char buf[sizeof("xx:xx:xx:xx:xx:xx")]; 977 978 (void)sprintf(buf, "%x:%x:%x:%x:%x:%x", 979 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]); 980 return (buf); 981} 982