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