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