1 /* 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 #ifndef lint 22 char copyright[] = 23 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ 24 All rights reserved.\n"; 25 #endif /* not lint */ 26 27 #ifndef lint 28 static char rcsid[] = 29 "@(#) $Header: /home/ncvs/src/usr.sbin/rarpd/rarpd.c,v 1.6 1996/08/24 23:05:08 wpaul Exp $ (LBL)"; 30 #endif 31 32 33 /* 34 * rarpd - Reverse ARP Daemon 35 * 36 * Usage: rarpd -a [ -f ] [ hostname ] 37 * rarpd [ -f ] interface [ hostname ] 38 * 39 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd. 40 * Currently, the argument is ignored. 41 */ 42 43 #include <stdio.h> 44 #include <syslog.h> 45 #include <string.h> 46 #include <strings.h> 47 #include <sys/types.h> 48 /* SunOS 4.x defines this while 3.x does not. */ 49 #ifdef __sys_types_h 50 #define SUNOS4 51 #endif 52 #include <sys/time.h> 53 #include <net/bpf.h> 54 #include <sys/socket.h> 55 #include <sys/ioctl.h> 56 #include <net/if.h> 57 #include <netinet/in.h> 58 #include <netinet/if_ether.h> 59 #include <sys/errno.h> 60 #include <sys/file.h> 61 #include <netdb.h> 62 63 #if defined(SUNOS4) || defined(__FreeBSD__) /* XXX */ 64 #define HAVE_DIRENT_H 65 #endif 66 67 #ifdef HAVE_DIRENT_H 68 #include <dirent.h> 69 #else 70 #include <sys/dir.h> 71 #endif 72 73 /* 74 * Map field names in ether_arp struct. What a pain in the neck. 75 */ 76 #if !defined(SUNOS4) && !defined(__FreeBSD__) 77 #undef arp_sha 78 #undef arp_spa 79 #undef arp_tha 80 #undef arp_tpa 81 #define arp_sha arp_xsha 82 #define arp_spa arp_xspa 83 #define arp_tha arp_xtha 84 #define arp_tpa arp_xtpa 85 #endif 86 87 #ifndef __GNUC__ 88 #define inline 89 #endif 90 91 extern int errno; 92 extern int ether_ntohost __P((char *, struct ether_addr *)); 93 94 /* 95 * The structure for each interface. 96 */ 97 struct if_info { 98 int ii_fd; /* BPF file descriptor */ 99 u_char ii_eaddr[6]; /* Ethernet address of this interface */ 100 u_long ii_ipaddr; /* IP address of this interface */ 101 u_long ii_netmask; /* subnet or net mask */ 102 struct if_info *ii_next; 103 }; 104 105 /* 106 * The list of all interfaces that are being listened to. rarp_loop() 107 * "selects" on the descriptors in this list. 108 */ 109 struct if_info *iflist; 110 111 extern char *malloc(); 112 extern void exit(); 113 114 u_long ipaddrtonetmask(); 115 void init_one(); 116 void init_all(); 117 void rarp_loop(); 118 void lookup_eaddr(); 119 void lookup_ipaddr(); 120 121 void 122 main(argc, argv) 123 int argc; 124 char **argv; 125 { 126 int op, pid; 127 char *ifname, *hostname, *name; 128 129 int aflag = 0; /* listen on "all" interfaces */ 130 int fflag = 0; /* don't fork */ 131 132 extern char *optarg; 133 extern int optind, opterr; 134 135 if (name = strrchr(argv[0], '/')) 136 ++name; 137 else 138 name = argv[0]; 139 if (*name == '-') 140 ++name; 141 142 /* 143 * All error reporting is done through syslogs. 144 */ 145 openlog(name, LOG_PID, LOG_DAEMON); 146 147 opterr = 0; 148 while ((op = getopt(argc, argv, "af")) != EOF) { 149 switch (op) { 150 case 'a': 151 ++aflag; 152 break; 153 154 case 'f': 155 ++fflag; 156 break; 157 158 default: 159 usage(); 160 /* NOTREACHED */ 161 } 162 } 163 ifname = argv[optind++]; 164 hostname = ifname ? argv[optind] : 0; 165 if ((aflag && ifname) || (!aflag && ifname == 0)) 166 usage(); 167 168 if (aflag) 169 init_all(); 170 else 171 init_one(ifname); 172 173 if (!fflag) 174 if (daemon(0,0)) { 175 perror("fork"); 176 exit(0); 177 } 178 rarp_loop(); 179 } 180 181 /* 182 * Add 'ifname' to the interface list. Lookup its IP address and network 183 * mask and Ethernet address, and open a BPF file for it. 184 */ 185 void 186 init_one(ifname) 187 char *ifname; 188 { 189 struct if_info *p; 190 191 192 p = (struct if_info *)malloc(sizeof(*p)); 193 p->ii_next = iflist; 194 iflist = p; 195 196 p->ii_fd = rarp_open(ifname); 197 lookup_eaddr(p->ii_fd, p->ii_eaddr); 198 lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask); 199 } 200 201 /* 202 * Initialize all "candidate" interfaces that are in the system 203 * configuration list. A "candidate" is up, not loopback and not 204 * point to point. 205 */ 206 void 207 init_all() 208 { 209 int fd; 210 int ifflags; 211 struct ifreq ibuf[8], tmp_ibuf, *ifptr, *n; 212 struct ifconf ifc; 213 214 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 215 syslog(LOG_ERR, "socket: %m"); 216 exit(1); 217 } 218 ifc.ifc_len = sizeof ibuf; 219 ifc.ifc_buf = (caddr_t)ibuf; 220 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 221 ifc.ifc_len < sizeof(struct ifreq)) { 222 syslog(LOG_ERR, "SIOCGIFCONF: %m"); 223 exit(1); 224 } 225 ifptr = ifc.ifc_req; 226 ifflags = ifptr->ifr_flags; 227 n = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); 228 while (ifptr < n) { 229 bcopy((char *)ifptr, (char *)&tmp_ibuf, sizeof(struct ifreq)); 230 if (ioctl(fd, SIOCGIFFLAGS, (char *)&tmp_ibuf) < 0) { 231 syslog(LOG_ERR, "SIOCGIFFLAGS: %m"); 232 exit(1); 233 } 234 if (ifptr->ifr_flags == ifflags && (tmp_ibuf.ifr_flags & 235 (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) == IFF_UP) 236 init_one(ifptr->ifr_name); 237 if(ifptr->ifr_addr.sa_len) /* Dohw! */ 238 ifptr = (struct ifreq *) ((caddr_t) ifptr + 239 ifptr->ifr_addr.sa_len - 240 sizeof(struct sockaddr)); 241 ifptr++; 242 } 243 (void)close(fd); 244 } 245 246 usage() 247 { 248 (void)fprintf(stderr, "usage: rarpd [ -af ] [ interface ]\n"); 249 exit(1); 250 } 251 252 static int 253 bpf_open() 254 { 255 int fd; 256 int n = 0; 257 char device[sizeof "/dev/bpf000"]; 258 259 /* 260 * Go through all the minors and find one that isn't in use. 261 */ 262 do { 263 (void)sprintf(device, "/dev/bpf%d", n++); 264 fd = open(device, O_RDWR); 265 } while (fd < 0 && errno == EBUSY); 266 267 if (fd < 0) { 268 syslog(LOG_ERR, "%s: %m", device); 269 exit(-1); 270 } 271 return fd; 272 } 273 274 /* 275 * Open a BPF file and attach it to the interface named 'device'. 276 * Set immediate mode, and set a filter that accepts only RARP requests. 277 */ 278 int 279 rarp_open(device) 280 char *device; 281 { 282 int fd; 283 struct ifreq ifr; 284 int immediate, link_type; 285 286 static struct bpf_insn insns[] = { 287 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12), 288 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3), 289 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20), 290 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARPOP_REVREQUEST, 0, 1), 291 BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) + 292 sizeof(struct ether_header)), 293 BPF_STMT(BPF_RET+BPF_K, 0), 294 }; 295 296 static struct bpf_program filter = { 297 sizeof insns / sizeof(insns[0]), 298 (struct bpf_insn *)&insns 299 }; 300 301 fd = bpf_open(); 302 /* 303 * Set immediate mode so packets are processed as they arrive. 304 */ 305 immediate = 1; 306 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) { 307 syslog(LOG_ERR, "BIOCIMMEDIATE: %m"); 308 exit(1); 309 } 310 (void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name); 311 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 312 syslog(LOG_ERR, "BIOCSETIF: %m"); 313 exit(1); 314 } 315 /* 316 * Check that the data link layer is an Ethernet; this code won't 317 * work with anything else. 318 */ 319 if (ioctl(fd, BIOCGDLT, &link_type) < 0) { 320 syslog(LOG_ERR, "BIOCGDLP: %m"); 321 exit(1); 322 } 323 if (link_type != DLT_EN10MB) { 324 syslog(LOG_ERR, "%s not on ethernet", device); 325 exit(1); 326 } 327 /* 328 * Set filter program. 329 */ 330 if (ioctl(fd, BIOCSETF, (caddr_t)&filter) < 0) { 331 syslog(LOG_ERR, "BIOCSETF: %m"); 332 exit(1); 333 } 334 return fd; 335 } 336 337 /* 338 * Perform various sanity checks on the RARP request packet. Return 339 * false on failure and log the reason. 340 */ 341 static int 342 rarp_check(p, len) 343 u_char *p; 344 int len; 345 { 346 struct ether_header *ep = (struct ether_header *)p; 347 struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep)); 348 349 if (len < sizeof(*ep) + sizeof(*ap)) { 350 syslog(LOG_ERR, "truncated request"); 351 return 0; 352 } 353 /* 354 * XXX This test might be better off broken out... 355 */ 356 if (ep->ether_type != htons(ETHERTYPE_REVARP) || 357 ap->arp_hrd != htons(ARPHRD_ETHER) || 358 ap->arp_op != htons(ARPOP_REVREQUEST) || 359 ap->arp_pro != htons(ETHERTYPE_IP) || 360 ap->arp_hln != 6 || ap->arp_pln != 4) { 361 syslog(LOG_DEBUG, "request fails sanity check"); 362 return 0; 363 } 364 if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) { 365 syslog(LOG_DEBUG, "ether/arp sender address mismatch"); 366 return 0; 367 } 368 if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) { 369 syslog(LOG_DEBUG, "ether/arp target address mismatch"); 370 return 0; 371 } 372 return 1; 373 } 374 375 #ifndef FD_SETSIZE 376 #define FD_SET(n, fdp) ((fdp)->fds_bits[0] |= (1 << (n))) 377 #define FD_ISSET(n, fdp) ((fdp)->fds_bits[0] & (1 << (n))) 378 #define FD_ZERO(fdp) ((fdp)->fds_bits[0] = 0) 379 #endif 380 381 /* 382 * Loop indefinitely listening for RARP requests on the 383 * interfaces in 'iflist'. 384 */ 385 void 386 rarp_loop() 387 { 388 struct bpf_hdr *bhp; 389 u_char *pkt; 390 int cc, fd; 391 fd_set fds, listeners; 392 int bufsize, maxfd = 0; 393 struct if_info *ii; 394 395 if (iflist == 0) { 396 syslog(LOG_ERR, "no interfaces"); 397 exit(1); 398 } 399 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) < 0) { 400 syslog(LOG_ERR, "BIOCGBLEN: %m"); 401 exit(1); 402 } 403 bhp = (struct bpf_hdr *)malloc((unsigned)bufsize); 404 405 /* 406 * Find the highest numbered file descriptor for select(). 407 * Initialize the set of descriptors to listen to. 408 */ 409 FD_ZERO(&fds); 410 for (ii = iflist; ii; ii = ii->ii_next) { 411 FD_SET(ii->ii_fd, &fds); 412 if (ii->ii_fd > maxfd) 413 maxfd = ii->ii_fd; 414 } 415 while (1) { 416 listeners = fds; 417 if (select(maxfd + 1, &listeners, (struct fd_set *)0, 418 (struct fd_set *)0, (struct timeval *)0) < 0) { 419 syslog(LOG_ERR, "select: %m"); 420 exit(1); 421 } 422 for (ii = iflist; ii; ii = ii->ii_next) { 423 fd = ii->ii_fd; 424 if (FD_ISSET(fd, &listeners)) { 425 again: 426 cc = read(fd, (char *)bhp, bufsize); 427 /* 428 * Due to a SunOS bug, after 2^31 bytes, the 429 * file offset overflows and read fails with 430 * EINVAL. The lseek() to 0 will fix things. 431 */ 432 if (cc < 0) { 433 if (errno == EINVAL && 434 (long)(lseek(fd, 0L, SEEK_CUR) + bufsize) < 0) { 435 (void)lseek(fd, 0, 0); 436 goto again; 437 } 438 syslog(LOG_ERR, "read: %m"); 439 exit(1); 440 } 441 pkt = (u_char *)bhp + bhp->bh_hdrlen; 442 443 if (rarp_check(pkt, (int)bhp->bh_datalen)) 444 rarp_process(ii, pkt); 445 } 446 } 447 } 448 } 449 450 #ifndef TFTP_DIR 451 #define TFTP_DIR "/tftpboot" 452 #endif 453 454 /* 455 * True if this server can boot the host whose IP address is 'addr'. 456 * This check is made by looking in the tftp directory for the 457 * configuration file. 458 */ 459 rarp_bootable(addr) 460 u_long addr; 461 { 462 463 #ifdef HAVE_DIRENT_H 464 register struct dirent *dent; 465 #else 466 register struct direct *dent; 467 #endif 468 register DIR *d; 469 char ipname[9]; 470 static DIR *dd = 0; 471 472 /* 473 * XXX Need to htonl() the IP address or it'll 474 * come out backwards. 475 */ 476 (void)sprintf(ipname, "%08X", htonl(addr)); 477 /* 478 * If directory is already open, rewind it. Otherwise, open it. 479 */ 480 if (d = dd) 481 rewinddir(d); 482 else { 483 if (chdir(TFTP_DIR) == -1) { 484 syslog(LOG_ERR, "chdir: %m"); 485 exit(1); 486 } 487 d = opendir("."); 488 if (d == 0) { 489 syslog(LOG_ERR, "opendir: %m"); 490 exit(1); 491 } 492 dd = d; 493 } 494 while (dent = readdir(d)) 495 if (strncmp(dent->d_name, ipname, 8) == 0) 496 return 1; 497 return 0; 498 499 } 500 501 /* 502 * Given a list of IP addresses, 'alist', return the first address that 503 * is on network 'net'; 'netmask' is a mask indicating the network portion 504 * of the address. 505 */ 506 u_long 507 choose_ipaddr(alist, net, netmask) 508 u_long **alist; 509 u_long net; 510 u_long netmask; 511 { 512 for (; *alist; ++alist) { 513 if ((**alist & netmask) == net) 514 return **alist; 515 } 516 return 0; 517 } 518 519 /* 520 * A one entry ip/ethernet address cache. 521 */ 522 static u_long cache_ipaddr; 523 static u_char cache_eaddr[6]; 524 525 /* 526 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has 527 * already been checked for validity. The reply is overlaid on the request. 528 */ 529 rarp_process(ii, pkt) 530 struct if_info *ii; 531 u_char *pkt; 532 { 533 struct ether_header *ep; 534 struct hostent *hp; 535 u_long target_ipaddr; 536 char ename[256]; 537 538 ep = (struct ether_header *)pkt; 539 /* 540 * If the address in the one element cache, don't bother 541 * looking up names. 542 */ 543 if (bcmp((char *)cache_eaddr, (char *)&ep->ether_shost, 6) == 0) 544 target_ipaddr = cache_ipaddr; 545 else { 546 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0 || 547 (hp = gethostbyname(ename)) == 0) 548 return; 549 /* 550 * Choose correct address from list. 551 */ 552 if (hp->h_addrtype != AF_INET) { 553 syslog(LOG_ERR, "cannot handle non IP addresses"); 554 exit(1); 555 } 556 target_ipaddr = choose_ipaddr((u_long **)hp->h_addr_list, 557 ii->ii_ipaddr & ii->ii_netmask, 558 ii->ii_netmask); 559 if (target_ipaddr == 0) { 560 syslog(LOG_ERR, "cannot find %s on %08x", 561 ename, ii->ii_ipaddr & ii->ii_netmask); 562 return; 563 } 564 bcopy((char *)&ep->ether_shost, (char *)cache_eaddr, 6); 565 cache_ipaddr = target_ipaddr; 566 } 567 if (rarp_bootable(target_ipaddr)) 568 rarp_reply(ii, ep, target_ipaddr); 569 } 570 571 /* 572 * Lookup the ethernet address of the interface attached to the BPF 573 * file descriptor 'fd'; return it in 'eaddr'. 574 */ 575 void 576 lookup_eaddr(fd, eaddr) 577 int fd; 578 u_char *eaddr; 579 { 580 struct ifreq ifr; 581 582 /* Use BPF descriptor to get ethernet address. */ 583 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 584 syslog(LOG_ERR, "SIOCGIFADDR: %m"); 585 exit(1); 586 } 587 bcopy((char *)&ifr.ifr_addr.sa_data[0], (char *)eaddr, 6); 588 } 589 590 /* 591 * Lookup the IP address and network mask of the interface named 'ifname'. 592 */ 593 void 594 lookup_ipaddr(ifname, addrp, netmaskp) 595 char *ifname; 596 u_long *addrp; 597 u_long *netmaskp; 598 { 599 int fd; 600 struct ifreq ifr; 601 602 /* Use data gram socket to get IP address. */ 603 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 604 syslog(LOG_ERR, "socket: %m"); 605 exit(1); 606 } 607 (void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name); 608 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 609 syslog(LOG_ERR, "SIOCGIFADDR: %m"); 610 exit(1); 611 } 612 *addrp = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; 613 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 614 perror("SIOCGIFNETMASK"); 615 exit(1); 616 } 617 *netmaskp = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; 618 /* 619 * If SIOCGIFNETMASK didn't work, figure out a mask from 620 * the IP address class. 621 */ 622 if (*netmaskp == 0) 623 *netmaskp = ipaddrtonetmask(*addrp); 624 625 (void)close(fd); 626 } 627 628 /* 629 * Poke the kernel arp tables with the ethernet/ip address combinataion 630 * given. When processing a reply, we must do this so that the booting 631 * host (i.e. the guy running rarpd), won't try to ARP for the hardware 632 * address of the guy being booted (he cannot answer the ARP). 633 */ 634 update_arptab(ep, ipaddr) 635 u_char *ep; 636 u_long ipaddr; 637 { 638 #ifdef SIOCSARP 639 int s; 640 struct arpreq request; 641 struct sockaddr_in *sin; 642 643 request.arp_flags = 0; 644 sin = (struct sockaddr_in *)&request.arp_pa; 645 sin->sin_family = AF_INET; 646 sin->sin_addr.s_addr = ipaddr; 647 request.arp_ha.sa_family = AF_UNSPEC; 648 bcopy((char *)ep, (char *)request.arp_ha.sa_data, 6); 649 650 s = socket(AF_INET, SOCK_DGRAM, 0); 651 if (ioctl(s, SIOCSARP, (caddr_t)&request) < 0) 652 syslog(LOG_ERR, "SIOCSARP: %m"); 653 (void)close(s); 654 #else 655 if (arptab_set(ep, ipaddr) > 0) 656 syslog(LOG_ERR, "couldn't update arp table"); 657 #endif 658 } 659 660 /* 661 * Build a reverse ARP packet and sent it out on the interface. 662 * 'ep' points to a valid ARPOP_REVREQUEST. The ARPOP_REVREPLY is built 663 * on top of the request, then written to the network. 664 * 665 * RFC 903 defines the ether_arp fields as follows. The following comments 666 * are taken (more or less) straight from this document. 667 * 668 * ARPOP_REVREQUEST 669 * 670 * arp_sha is the hardware address of the sender of the packet. 671 * arp_spa is undefined. 672 * arp_tha is the 'target' hardware address. 673 * In the case where the sender wishes to determine his own 674 * protocol address, this, like arp_sha, will be the hardware 675 * address of the sender. 676 * arp_tpa is undefined. 677 * 678 * ARPOP_REVREPLY 679 * 680 * arp_sha is the hardware address of the responder (the sender of the 681 * reply packet). 682 * arp_spa is the protocol address of the responder (see the note below). 683 * arp_tha is the hardware address of the target, and should be the same as 684 * that which was given in the request. 685 * arp_tpa is the protocol address of the target, that is, the desired address. 686 * 687 * Note that the requirement that arp_spa be filled in with the responder's 688 * protocol is purely for convenience. For instance, if a system were to use 689 * both ARP and RARP, then the inclusion of the valid protocol-hardware 690 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent 691 * ARP request. 692 */ 693 rarp_reply(ii, ep, ipaddr) 694 struct if_info *ii; 695 struct ether_header *ep; 696 u_long ipaddr; 697 { 698 int n; 699 struct ether_arp *ap = (struct ether_arp *)(ep + 1); 700 int len, raw_sock; 701 702 update_arptab((u_char *)&ap->arp_sha, ipaddr); 703 704 /* 705 * Build the rarp reply by modifying the rarp request in place. 706 */ 707 ap->arp_op = htons(ARPOP_REVREPLY); 708 709 /* 710 * XXX Using htons(ETHERTYPE_REVARP) doesn't work: you wind 711 * up transmitting 0x3580 instead of the correct value of 712 * 0x8035. What makes no sense is that the NetBSD people 713 * do in fact use htons(ETHERTYPE_REVARP) in their rarpd. 714 * (Thank god for tcpdump or I would never have figured this 715 * out.) 716 */ 717 ep->ether_type = htons(ETHERTYPE_REVARP); 718 719 bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6); 720 bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6); 721 bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6); 722 723 bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4); 724 /* Target hardware is unchanged. */ 725 bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4); 726 727 len = sizeof(*ep) + sizeof(*ap); 728 n = write(ii->ii_fd, (char *)ep, len); 729 if (n != len) { 730 syslog(LOG_ERR, "write: only %d of %d bytes written", n, len); 731 } 732 } 733 734 /* 735 * Get the netmask of an IP address. This routine is used if 736 * SIOCGIFNETMASK doesn't work. 737 */ 738 u_long 739 ipaddrtonetmask(addr) 740 u_long addr; 741 { 742 if (IN_CLASSA(addr)) 743 return IN_CLASSA_NET; 744 if (IN_CLASSB(addr)) 745 return IN_CLASSB_NET; 746 if (IN_CLASSC(addr)) 747 return IN_CLASSC_NET; 748 syslog(LOG_DEBUG, "unknown IP address class: %08X", addr); 749 exit(1); 750 /* NOTREACHED */ 751 } 752