rarpd.c revision 117447
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 117446 2003-07-11 14:13:21Z mux $"); 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 */ 84struct 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 */ 97struct if_info *iflist; 98 99int verbose; /* verbose messages */ 100const char *tftp_dir = TFTP_DIR; /* tftp directory */ 101 102int dflag; /* messages to stdout/stderr, not syslog(3) */ 103int sflag; /* ignore /tftpboot */ 104 105static u_char zero[6]; 106 107static int bpf_open(void); 108static in_addr_t choose_ipaddr(in_addr_t **, in_addr_t, in_addr_t); 109static char *eatoa(u_char *); 110static int expand_syslog_m(const char *fmt, char **newfmt); 111static void init(char *); 112static void init_one(struct ifaddrs *, char *); 113static char *intoa(in_addr_t); 114static in_addr_t ipaddrtonetmask(in_addr_t); 115static void logmsg(int, const char *, ...) __printflike(2, 3); 116static int rarp_bootable(in_addr_t); 117static int rarp_check(u_char *, u_int); 118static void rarp_loop(void); 119static int rarp_open(char *); 120static void rarp_process(struct if_info *, u_char *, u_int); 121static void rarp_reply(struct if_info *, struct ether_header *, 122 in_addr_t, u_int); 123static void update_arptab(u_char *, in_addr_t); 124static void usage(void); 125 126int 127main(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 } 179 ifname = argv[optind++]; 180 hostname = ifname ? argv[optind] : NULL; 181 if ((aflag && ifname) || (!aflag && ifname == NULL)) 182 usage(); 183 184 if (aflag) 185 init(NULL); 186 else 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 */ 202static void 203init_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 */ 266static void 267init(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 308static void 309usage(void) 310{ 311 (void)fprintf(stderr, "usage: rarpd [-adfsv] [-t directory] [interface]\n"); 312 exit(1); 313} 314 315static int 316bpf_open(void) 317{ 318 int fd; 319 int n = 0; 320 char device[sizeof "/dev/bpf000"]; 321 322 /* 323 * Go through all the minors and find one that isn't in use. 324 */ 325 do { 326 (void)sprintf(device, "/dev/bpf%d", n++); 327 fd = open(device, O_RDWR); 328 } while ((fd == -1) && (errno == EBUSY)); 329 330 if (fd == -1) { 331 logmsg(LOG_ERR, "%s: %m", device); 332 exit(1); 333 } 334 return fd; 335} 336 337/* 338 * Open a BPF file and attach it to the interface named 'device'. 339 * Set immediate mode, and set a filter that accepts only RARP requests. 340 */ 341static int 342rarp_open(char *device) 343{ 344 int fd; 345 struct ifreq ifr; 346 u_int dlt; 347 int immediate; 348 349 static struct bpf_insn insns[] = { 350 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12), 351 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3), 352 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20), 353 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1), 354 BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) + 355 sizeof(struct ether_header)), 356 BPF_STMT(BPF_RET|BPF_K, 0), 357 }; 358 static struct bpf_program filter = { 359 sizeof insns / sizeof(insns[0]), 360 insns 361 }; 362 363 fd = bpf_open(); 364 /* 365 * Set immediate mode so packets are processed as they arrive. 366 */ 367 immediate = 1; 368 if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) { 369 logmsg(LOG_ERR, "BIOCIMMEDIATE: %m"); 370 exit(1); 371 } 372 strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 373 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) { 374 logmsg(LOG_ERR, "BIOCSETIF: %m"); 375 exit(1); 376 } 377 /* 378 * Check that the data link layer is an Ethernet; this code won't 379 * work with anything else. 380 */ 381 if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) { 382 logmsg(LOG_ERR, "BIOCGDLT: %m"); 383 exit(1); 384 } 385 if (dlt != DLT_EN10MB) { 386 logmsg(LOG_ERR, "%s is not an ethernet", device); 387 exit(1); 388 } 389 /* 390 * Set filter program. 391 */ 392 if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) { 393 logmsg(LOG_ERR, "BIOCSETF: %m"); 394 exit(1); 395 } 396 return fd; 397} 398 399/* 400 * Perform various sanity checks on the RARP request packet. Return 401 * false on failure and log the reason. 402 */ 403static int 404rarp_check(u_char *p, u_int len) 405{ 406 struct ether_header *ep = (struct ether_header *)p; 407 struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep)); 408 409 if (len < sizeof(*ep) + sizeof(*ap)) { 410 logmsg(LOG_ERR, "truncated request, got %u, expected %lu", 411 len, (u_long)(sizeof(*ep) + sizeof(*ap))); 412 return 0; 413 } 414 /* 415 * XXX This test might be better off broken out... 416 */ 417 if (ntohs(ep->ether_type) != ETHERTYPE_REVARP || 418 ntohs(ap->arp_hrd) != ARPHRD_ETHER || 419 ntohs(ap->arp_op) != REVARP_REQUEST || 420 ntohs(ap->arp_pro) != ETHERTYPE_IP || 421 ap->arp_hln != 6 || ap->arp_pln != 4) { 422 logmsg(LOG_DEBUG, "request fails sanity check"); 423 return 0; 424 } 425 if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) { 426 logmsg(LOG_DEBUG, "ether/arp sender address mismatch"); 427 return 0; 428 } 429 if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) { 430 logmsg(LOG_DEBUG, "ether/arp target address mismatch"); 431 return 0; 432 } 433 return 1; 434} 435 436/* 437 * Loop indefinitely listening for RARP requests on the 438 * interfaces in 'iflist'. 439 */ 440static void 441rarp_loop(void) 442{ 443 u_char *buf, *bp, *ep; 444 int cc, fd; 445 fd_set fds, listeners; 446 int bufsize, maxfd = 0; 447 struct if_info *ii; 448 449 if (iflist == NULL) { 450 logmsg(LOG_ERR, "no interfaces"); 451 exit(1); 452 } 453 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) { 454 logmsg(LOG_ERR, "BIOCGBLEN: %m"); 455 exit(1); 456 } 457 buf = malloc(bufsize); 458 if (buf == NULL) { 459 logmsg(LOG_ERR, "malloc: %m"); 460 exit(1); 461 } 462 463 while (1) { 464 /* 465 * Find the highest numbered file descriptor for select(). 466 * Initialize the set of descriptors to listen to. 467 */ 468 FD_ZERO(&fds); 469 for (ii = iflist; ii != NULL; ii = ii->ii_next) { 470 FD_SET(ii->ii_fd, &fds); 471 if (ii->ii_fd > maxfd) 472 maxfd = ii->ii_fd; 473 } 474 listeners = fds; 475 if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) { 476 /* Don't choke when we get ptraced */ 477 if (errno == EINTR) 478 continue; 479 logmsg(LOG_ERR, "select: %m"); 480 exit(1); 481 } 482 for (ii = iflist; ii != NULL; ii = ii->ii_next) { 483 fd = ii->ii_fd; 484 if (!FD_ISSET(fd, &listeners)) 485 continue; 486 again: 487 cc = read(fd, (char *)buf, bufsize); 488 /* Don't choke when we get ptraced */ 489 if ((cc == -1) && (errno == EINTR)) 490 goto again; 491 492 /* Loop through the packet(s) */ 493#define bhp ((struct bpf_hdr *)bp) 494 bp = buf; 495 ep = bp + cc; 496 while (bp < ep) { 497 u_int caplen, hdrlen; 498 499 caplen = bhp->bh_caplen; 500 hdrlen = bhp->bh_hdrlen; 501 if (rarp_check(bp + hdrlen, caplen)) 502 rarp_process(ii, bp + hdrlen, caplen); 503 bp += BPF_WORDALIGN(hdrlen + caplen); 504 } 505 } 506 } 507#undef bhp 508} 509 510/* 511 * True if this server can boot the host whose IP address is 'addr'. 512 * This check is made by looking in the tftp directory for the 513 * configuration file. 514 */ 515static int 516rarp_bootable(in_addr_t addr) 517{ 518 struct dirent *dent; 519 DIR *d; 520 char ipname[9]; 521 static DIR *dd = NULL; 522 523 (void)sprintf(ipname, "%08X", (in_addr_t)ntohl(addr)); 524 525 /* 526 * If directory is already open, rewind it. Otherwise, open it. 527 */ 528 if ((d = dd) != NULL) 529 rewinddir(d); 530 else { 531 if (chdir(tftp_dir) == -1) { 532 logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir); 533 exit(1); 534 } 535 d = opendir("."); 536 if (d == NULL) { 537 logmsg(LOG_ERR, "opendir: %m"); 538 exit(1); 539 } 540 dd = d; 541 } 542 while ((dent = readdir(d)) != NULL) 543 if (strncmp(dent->d_name, ipname, 8) == 0) 544 return 1; 545 return 0; 546} 547 548/* 549 * Given a list of IP addresses, 'alist', return the first address that 550 * is on network 'net'; 'netmask' is a mask indicating the network portion 551 * of the address. 552 */ 553static in_addr_t 554choose_ipaddr(in_addr_t **alist, in_addr_t net, in_addr_t netmask) 555{ 556 for (; *alist; ++alist) 557 if ((**alist & netmask) == net) 558 return **alist; 559 return 0; 560} 561 562/* 563 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has 564 * already been checked for validity. The reply is overlaid on the request. 565 */ 566static void 567rarp_process(struct if_info *ii, u_char *pkt, u_int len) 568{ 569 struct ether_header *ep; 570 struct hostent *hp; 571 in_addr_t target_ipaddr; 572 char ename[256]; 573 574 ep = (struct ether_header *)pkt; 575 /* should this be arp_tha? */ 576 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) { 577 logmsg(LOG_ERR, "cannot map %s to name", 578 eatoa(ep->ether_shost)); 579 return; 580 } 581 582 if ((hp = gethostbyname(ename)) == NULL) { 583 logmsg(LOG_ERR, "cannot map %s to IP address", ename); 584 return; 585 } 586 587 /* 588 * Choose correct address from list. 589 */ 590 if (hp->h_addrtype != AF_INET) { 591 logmsg(LOG_ERR, "cannot handle non IP addresses for %s", 592 ename); 593 return; 594 } 595 target_ipaddr = choose_ipaddr((in_addr_t **)hp->h_addr_list, 596 ii->ii_ipaddr & ii->ii_netmask, 597 ii->ii_netmask); 598 if (target_ipaddr == 0) { 599 logmsg(LOG_ERR, "cannot find %s on net %s", 600 ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask))); 601 return; 602 } 603 if (sflag || rarp_bootable(target_ipaddr)) 604 rarp_reply(ii, ep, target_ipaddr, len); 605 else if (verbose > 1) 606 logmsg(LOG_INFO, "%s %s at %s DENIED (not bootable)", 607 ii->ii_ifname, 608 eatoa(ep->ether_shost), 609 intoa(ntohl(target_ipaddr))); 610} 611 612/* 613 * Poke the kernel arp tables with the ethernet/ip address combinataion 614 * given. When processing a reply, we must do this so that the booting 615 * host (i.e. the guy running rarpd), won't try to ARP for the hardware 616 * address of the guy being booted (he cannot answer the ARP). 617 */ 618struct sockaddr_inarp sin_inarp = { 619 sizeof(struct sockaddr_inarp), AF_INET, 0, 620 {0}, 621 {0}, 622 0, 0 623}; 624struct sockaddr_dl sin_dl = { 625 sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6, 626 0, "" 627}; 628struct { 629 struct rt_msghdr rthdr; 630 char rtspace[512]; 631} rtmsg; 632 633static void 634update_arptab(u_char *ep, in_addr_t ipaddr) 635{ 636 int cc; 637 struct sockaddr_inarp *ar, *ar2; 638 struct sockaddr_dl *ll, *ll2; 639 struct rt_msghdr *rt; 640 int xtype, xindex; 641 static pid_t pid; 642 int r; 643 static int seq; 644 645 r = socket(PF_ROUTE, SOCK_RAW, 0); 646 if (r == -1) { 647 logmsg(LOG_ERR, "raw route socket: %m"); 648 exit(1); 649 } 650 pid = getpid(); 651 652 ar = &sin_inarp; 653 ar->sin_addr.s_addr = ipaddr; 654 ll = &sin_dl; 655 bcopy(ep, LLADDR(ll), 6); 656 657 /* Get the type and interface index */ 658 rt = &rtmsg.rthdr; 659 bzero(rt, sizeof(rtmsg)); 660 rt->rtm_version = RTM_VERSION; 661 rt->rtm_addrs = RTA_DST; 662 rt->rtm_type = RTM_GET; 663 rt->rtm_seq = ++seq; 664 ar2 = (struct sockaddr_inarp *)rtmsg.rtspace; 665 bcopy(ar, ar2, sizeof(*ar)); 666 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar); 667 errno = 0; 668 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) { 669 logmsg(LOG_ERR, "rtmsg get write: %m"); 670 close(r); 671 return; 672 } 673 do { 674 cc = read(r, rt, sizeof(rtmsg)); 675 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid)); 676 if (cc == -1) { 677 logmsg(LOG_ERR, "rtmsg get read: %m"); 678 close(r); 679 return; 680 } 681 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len); 682 if (ll2->sdl_family != AF_LINK) { 683 /* 684 * XXX I think this means the ip address is not on a 685 * directly connected network (the family is AF_INET in 686 * this case). 687 */ 688 logmsg(LOG_ERR, "bogus link family (%d) wrong net for %08X?\n", 689 ll2->sdl_family, ipaddr); 690 close(r); 691 return; 692 } 693 xtype = ll2->sdl_type; 694 xindex = ll2->sdl_index; 695 696 /* Set the new arp entry */ 697 bzero(rt, sizeof(rtmsg)); 698 rt->rtm_version = RTM_VERSION; 699 rt->rtm_addrs = RTA_DST | RTA_GATEWAY; 700 rt->rtm_inits = RTV_EXPIRE; 701 rt->rtm_rmx.rmx_expire = time(0) + ARPSECS; 702 rt->rtm_flags = RTF_HOST | RTF_STATIC; 703 rt->rtm_type = RTM_ADD; 704 rt->rtm_seq = ++seq; 705 706 bcopy(ar, ar2, sizeof(*ar)); 707 708 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2)); 709 bcopy(ll, ll2, sizeof(*ll)); 710 ll2->sdl_type = xtype; 711 ll2->sdl_index = xindex; 712 713 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2); 714 errno = 0; 715 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) { 716 logmsg(LOG_ERR, "rtmsg add write: %m"); 717 close(r); 718 return; 719 } 720 do { 721 cc = read(r, rt, sizeof(rtmsg)); 722 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid)); 723 close(r); 724 if (cc == -1) { 725 logmsg(LOG_ERR, "rtmsg add read: %m"); 726 return; 727 } 728} 729 730/* 731 * Build a reverse ARP packet and sent it out on the interface. 732 * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built 733 * on top of the request, then written to the network. 734 * 735 * RFC 903 defines the ether_arp fields as follows. The following comments 736 * are taken (more or less) straight from this document. 737 * 738 * REVARP_REQUEST 739 * 740 * arp_sha is the hardware address of the sender of the packet. 741 * arp_spa is undefined. 742 * arp_tha is the 'target' hardware address. 743 * In the case where the sender wishes to determine his own 744 * protocol address, this, like arp_sha, will be the hardware 745 * address of the sender. 746 * arp_tpa is undefined. 747 * 748 * REVARP_REPLY 749 * 750 * arp_sha is the hardware address of the responder (the sender of the 751 * reply packet). 752 * arp_spa is the protocol address of the responder (see the note below). 753 * arp_tha is the hardware address of the target, and should be the same as 754 * that which was given in the request. 755 * arp_tpa is the protocol address of the target, that is, the desired address. 756 * 757 * Note that the requirement that arp_spa be filled in with the responder's 758 * protocol is purely for convenience. For instance, if a system were to use 759 * both ARP and RARP, then the inclusion of the valid protocol-hardware 760 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent 761 * ARP request. 762 */ 763static void 764rarp_reply(struct if_info *ii, struct ether_header *ep, in_addr_t ipaddr, 765 u_int len) 766{ 767 u_int n; 768 struct ether_arp *ap = (struct ether_arp *)(ep + 1); 769 770 update_arptab((u_char *)&ap->arp_sha, ipaddr); 771 772 /* 773 * Build the rarp reply by modifying the rarp request in place. 774 */ 775 ap->arp_op = htons(REVARP_REPLY); 776 777#ifdef BROKEN_BPF 778 ep->ether_type = ETHERTYPE_REVARP; 779#endif 780 bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6); 781 bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6); 782 bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6); 783 784 bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4); 785 /* Target hardware is unchanged. */ 786 bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4); 787 788 /* Zero possible garbage after packet. */ 789 bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)), 790 len - (sizeof(*ep) + sizeof(*ap))); 791 n = write(ii->ii_fd, (char *)ep, len); 792 if (n != len) 793 logmsg(LOG_ERR, "write: only %d of %d bytes written", n, len); 794 if (verbose) 795 logmsg(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname, 796 eatoa(ap->arp_tha), 797 intoa(ntohl(ipaddr))); 798} 799 800/* 801 * Get the netmask of an IP address. This routine is used if 802 * SIOCGIFNETMASK doesn't work. 803 */ 804static in_addr_t 805ipaddrtonetmask(in_addr_t addr) 806{ 807 addr = ntohl(addr); 808 if (IN_CLASSA(addr)) 809 return htonl(IN_CLASSA_NET); 810 if (IN_CLASSB(addr)) 811 return htonl(IN_CLASSB_NET); 812 if (IN_CLASSC(addr)) 813 return htonl(IN_CLASSC_NET); 814 logmsg(LOG_DEBUG, "unknown IP address class: %08X", addr); 815 return htonl(0xffffffff); 816} 817 818/* 819 * A faster replacement for inet_ntoa(). 820 */ 821static char * 822intoa(in_addr_t addr) 823{ 824 char *cp; 825 u_int byte; 826 int n; 827 static char buf[sizeof(".xxx.xxx.xxx.xxx")]; 828 829 cp = &buf[sizeof buf]; 830 *--cp = '\0'; 831 832 n = 4; 833 do { 834 byte = addr & 0xff; 835 *--cp = byte % 10 + '0'; 836 byte /= 10; 837 if (byte > 0) { 838 *--cp = byte % 10 + '0'; 839 byte /= 10; 840 if (byte > 0) 841 *--cp = byte + '0'; 842 } 843 *--cp = '.'; 844 addr >>= 8; 845 } while (--n > 0); 846 847 return cp + 1; 848} 849 850static char * 851eatoa(u_char *ea) 852{ 853 static char buf[sizeof("xx:xx:xx:xx:xx:xx")]; 854 855 (void)sprintf(buf, "%x:%x:%x:%x:%x:%x", 856 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]); 857 return (buf); 858} 859 860static void 861logmsg(int pri, const char *fmt, ...) 862{ 863 va_list v; 864 FILE *fp; 865 char *newfmt; 866 867 va_start(v, fmt); 868 if (dflag) { 869 if (pri == LOG_ERR) 870 fp = stderr; 871 else 872 fp = stdout; 873 if (expand_syslog_m(fmt, &newfmt) == -1) { 874 vfprintf(fp, fmt, v); 875 } else { 876 vfprintf(fp, newfmt, v); 877 free(newfmt); 878 } 879 fputs("\n", fp); 880 fflush(fp); 881 } else { 882 vsyslog(pri, fmt, v); 883 } 884 va_end(v); 885} 886 887static int 888expand_syslog_m(const char *fmt, char **newfmt) { 889 const char *str, *m; 890 char *p, *np; 891 892 p = strdup(""); 893 str = fmt; 894 while ((m = strstr(str, "%m")) != NULL) { 895 asprintf(&np, "%s%.*s%s", p, (int)(m - str), 896 str, strerror(errno)); 897 free(p); 898 if (np == NULL) { 899 errno = ENOMEM; 900 return (-1); 901 } 902 p = np; 903 str = m + 2; 904 } 905 906 if (*str != '\0') { 907 asprintf(&np, "%s%s", p, str); 908 free(p); 909 if (np == NULL) { 910 errno = ENOMEM; 911 return (-1); 912 } 913 p = np; 914 } 915 916 *newfmt = p; 917 return (0); 918} 919