rtsold.c revision 118914
1/* $KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: head/usr.sbin/rtsold/rtsold.c 118914 2003-08-14 18:13:34Z ume $ 32 */ 33 34#include <sys/types.h> 35#include <sys/time.h> 36#include <sys/socket.h> 37#include <sys/param.h> 38 39#include <net/if.h> 40#include <net/if_dl.h> 41 42#include <netinet/in.h> 43#include <netinet/icmp6.h> 44 45#include <signal.h> 46#include <unistd.h> 47#include <syslog.h> 48#include <string.h> 49#include <stdlib.h> 50#include <stdio.h> 51#include <errno.h> 52#include <err.h> 53#include <stdarg.h> 54#include <ifaddrs.h> 55 56#include "rtsold.h" 57 58struct ifinfo *iflist; 59struct timeval tm_max = {0x7fffffff, 0x7fffffff}; 60static int log_upto = 999; 61static int fflag = 0; 62 63int aflag = 0; 64int dflag = 0; 65 66char *otherconf_script; 67 68/* protocol constatns */ 69#define MAX_RTR_SOLICITATION_DELAY 1 /* second */ 70#define RTR_SOLICITATION_INTERVAL 4 /* seconds */ 71#define MAX_RTR_SOLICITATIONS 3 /* times */ 72 73/* 74 * implementation dependent constants in seconds 75 * XXX: should be configurable 76 */ 77#define PROBE_INTERVAL 60 78 79/* utility macros */ 80/* a < b */ 81#define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\ 82 (((a).tv_sec == (b).tv_sec) && \ 83 ((a).tv_usec < (b).tv_usec))) 84 85/* a <= b */ 86#define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\ 87 (((a).tv_sec == (b).tv_sec) &&\ 88 ((a).tv_usec <= (b).tv_usec))) 89 90/* a == b */ 91#define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec)) 92 93int main __P((int, char **)); 94 95/* static variables and functions */ 96static int mobile_node = 0; 97static int do_dump; 98static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */ 99static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */ 100 101static int ifconfig __P((char *)); 102#if 0 103static int ifreconfig __P((char *)); 104#endif 105static int make_packet __P((struct ifinfo *)); 106static struct timeval *rtsol_check_timer __P((void)); 107static void TIMEVAL_ADD __P((struct timeval *, struct timeval *, 108 struct timeval *)); 109static void TIMEVAL_SUB __P((struct timeval *, struct timeval *, 110 struct timeval *)); 111 112static void rtsold_set_dump_file __P((int)); 113static void usage __P((char *)); 114static char **autoifprobe __P((void)); 115 116int 117main(argc, argv) 118 int argc; 119 char **argv; 120{ 121 int s, ch, once = 0; 122 struct timeval *timeout; 123 char *argv0, *opts; 124 fd_set *fdsetp, *selectfdp; 125 int fdmasks; 126 int maxfd; 127 int rtsock; 128 129 /* 130 * Initialization 131 */ 132 argv0 = argv[0]; 133 134 /* get option */ 135 if (argv0 && argv0[strlen(argv0) - 1] != 'd') { 136 fflag = 1; 137 once = 1; 138 opts = "adDO:"; 139 } else 140 opts = "adDfm1O:"; 141 142 while ((ch = getopt(argc, argv, opts)) != -1) { 143 switch (ch) { 144 case 'a': 145 aflag = 1; 146 break; 147 case 'd': 148 dflag = 1; 149 break; 150 case 'D': 151 dflag = 2; 152 break; 153 case 'f': 154 fflag = 1; 155 break; 156 case 'm': 157 mobile_node = 1; 158 break; 159 case '1': 160 once = 1; 161 break; 162 case 'O': 163 otherconf_script = optarg; 164 break; 165 default: 166 usage(argv0); 167 /*NOTREACHED*/ 168 } 169 } 170 argc -= optind; 171 argv += optind; 172 173 if (aflag) { 174 int i; 175 176 if (argc != 0) { 177 usage(argv0); 178 /*NOTREACHED*/ 179 } 180 181 argv = autoifprobe(); 182 if (!argv) { 183 errx(1, "could not autoprobe interface"); 184 /*NOTREACHED*/ 185 } 186 187 for (i = 0; argv[i]; i++) 188 ; 189 argc = i; 190 } 191 if (argc == 0) { 192 usage(argv0); 193 /*NOTREACHED*/ 194 } 195 196 /* set log level */ 197 if (dflag == 0) 198 log_upto = LOG_NOTICE; 199 if (!fflag) { 200 char *ident; 201 202 ident = strrchr(argv0, '/'); 203 if (!ident) 204 ident = argv0; 205 else 206 ident++; 207 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON); 208 if (log_upto >= 0) 209 setlogmask(LOG_UPTO(log_upto)); 210 } 211 212 if (otherconf_script && *otherconf_script != '/') { 213 errx(1, "configuration script (%s) must be an absolute path", 214 otherconf_script); 215 } 216 217#ifndef HAVE_ARC4RANDOM 218 /* random value initialization */ 219 srandom((u_long)time(NULL)); 220#endif 221 222 /* warn if accept_rtadv is down */ 223 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV)) 224 warnx("kernel is configured not to accept RAs"); 225 /* warn if forwarding is up */ 226 if (getinet6sysctl(IPV6CTL_FORWARDING)) 227 warnx("kernel is configured as a router, not a host"); 228 229 /* initialization to dump internal status to a file */ 230 signal(SIGUSR1, rtsold_set_dump_file); 231 232 if (!fflag) 233 daemon(0, 0); /* act as a daemon */ 234 235 /* 236 * Open a socket for sending RS and receiving RA. 237 * This should be done before calling ifinit(), since the function 238 * uses the socket. 239 */ 240 if ((s = sockopen()) < 0) { 241 warnmsg(LOG_ERR, __func__, "failed to open a socket"); 242 exit(1); 243 /*NOTREACHED*/ 244 } 245 maxfd = s; 246 if ((rtsock = rtsock_open()) < 0) { 247 warnmsg(LOG_ERR, __func__, "failed to open a socket"); 248 exit(1); 249 /*NOTREACHED*/ 250 } 251 if (rtsock > maxfd) 252 maxfd = rtsock; 253 254 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask); 255 if ((fdsetp = malloc(fdmasks)) == NULL) { 256 err(1, "malloc"); 257 /*NOTREACHED*/ 258 } 259 if ((selectfdp = malloc(fdmasks)) == NULL) { 260 err(1, "malloc"); 261 /*NOTREACHED*/ 262 } 263 264 /* configuration per interface */ 265 if (ifinit()) { 266 warnmsg(LOG_ERR, __func__, 267 "failed to initilizatoin interfaces"); 268 exit(1); 269 /*NOTREACHED*/ 270 } 271 while (argc--) { 272 if (ifconfig(*argv)) { 273 warnmsg(LOG_ERR, __func__, 274 "failed to initialize %s", *argv); 275 exit(1); 276 /*NOTREACHED*/ 277 } 278 argv++; 279 } 280 281 /* setup for probing default routers */ 282 if (probe_init()) { 283 warnmsg(LOG_ERR, __func__, 284 "failed to setup for probing routers"); 285 exit(1); 286 /*NOTREACHED*/ 287 } 288 289 /* dump the current pid */ 290 if (!once) { 291 pid_t pid = getpid(); 292 FILE *fp; 293 294 if ((fp = fopen(pidfilename, "w")) == NULL) 295 warnmsg(LOG_ERR, __func__, 296 "failed to open a pid log file(%s): %s", 297 pidfilename, strerror(errno)); 298 else { 299 fprintf(fp, "%d\n", pid); 300 fclose(fp); 301 } 302 } 303 304 memset(fdsetp, 0, fdmasks); 305 FD_SET(s, fdsetp); 306 FD_SET(rtsock, fdsetp); 307 while (1) { /* main loop */ 308 int e; 309 310 memcpy(selectfdp, fdsetp, fdmasks); 311 312 if (do_dump) { /* SIGUSR1 */ 313 do_dump = 0; 314 rtsold_dump_file(dumpfilename); 315 } 316 317 timeout = rtsol_check_timer(); 318 319 if (once) { 320 struct ifinfo *ifi; 321 322 /* if we have no timeout, we are done (or failed) */ 323 if (timeout == NULL) 324 break; 325 326 /* if all interfaces have got RA packet, we are done */ 327 for (ifi = iflist; ifi; ifi = ifi->next) { 328 if (ifi->state != IFS_DOWN && ifi->racnt == 0) 329 break; 330 } 331 if (ifi == NULL) 332 break; 333 } 334 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout); 335 if (e < 1) { 336 if (e < 0 && errno != EINTR) { 337 warnmsg(LOG_ERR, __func__, "select: %s", 338 strerror(errno)); 339 } 340 continue; 341 } 342 343 /* packet reception */ 344 if (FD_ISSET(rtsock, selectfdp)) 345 rtsock_input(rtsock); 346 if (FD_ISSET(s, selectfdp)) 347 rtsol_input(s); 348 } 349 /* NOTREACHED */ 350 351 return 0; 352} 353 354static int 355ifconfig(char *ifname) 356{ 357 struct ifinfo *ifinfo; 358 struct sockaddr_dl *sdl; 359 int flags; 360 361 if ((sdl = if_nametosdl(ifname)) == NULL) { 362 warnmsg(LOG_ERR, __func__, 363 "failed to get link layer information for %s", ifname); 364 return(-1); 365 } 366 if (find_ifinfo(sdl->sdl_index)) { 367 warnmsg(LOG_ERR, __func__, 368 "interface %s was already configured", ifname); 369 free(sdl); 370 return(-1); 371 } 372 373 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) { 374 warnmsg(LOG_ERR, __func__, "memory allocation failed"); 375 free(sdl); 376 return(-1); 377 } 378 memset(ifinfo, 0, sizeof(*ifinfo)); 379 ifinfo->sdl = sdl; 380 381 strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname)); 382 383 /* construct a router solicitation message */ 384 if (make_packet(ifinfo)) 385 goto bad; 386 387 /* 388 * check if the interface is available. 389 * also check if SIOCGIFMEDIA ioctl is OK on the interface. 390 */ 391 ifinfo->mediareqok = 1; 392 ifinfo->active = interface_status(ifinfo); 393 if (!ifinfo->mediareqok) { 394 /* 395 * probe routers periodically even if the link status 396 * does not change. 397 */ 398 ifinfo->probeinterval = PROBE_INTERVAL; 399 } 400 401 /* activate interface: interface_up returns 0 on success */ 402 flags = interface_up(ifinfo->ifname); 403 if (flags == 0) 404 ifinfo->state = IFS_DELAY; 405 else if (flags == IFS_TENTATIVE) 406 ifinfo->state = IFS_TENTATIVE; 407 else 408 ifinfo->state = IFS_DOWN; 409 410 rtsol_timer_update(ifinfo); 411 412 /* link into chain */ 413 if (iflist) 414 ifinfo->next = iflist; 415 iflist = ifinfo; 416 417 return(0); 418 419bad: 420 free(ifinfo->sdl); 421 free(ifinfo); 422 return(-1); 423} 424 425#if 0 426static int 427ifreconfig(char *ifname) 428{ 429 struct ifinfo *ifi, *prev; 430 int rv; 431 432 prev = NULL; 433 for (ifi = iflist; ifi; ifi = ifi->next) { 434 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0) 435 break; 436 prev = ifi; 437 } 438 prev->next = ifi->next; 439 440 rv = ifconfig(ifname); 441 442 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */ 443 if (ifi->rs_data) 444 free(ifi->rs_data); 445 free(ifi->sdl); 446 free(ifi); 447 return rv; 448} 449#endif 450 451struct ifinfo * 452find_ifinfo(int ifindex) 453{ 454 struct ifinfo *ifi; 455 456 for (ifi = iflist; ifi; ifi = ifi->next) 457 if (ifi->sdl->sdl_index == ifindex) 458 return(ifi); 459 return(NULL); 460} 461 462static int 463make_packet(struct ifinfo *ifinfo) 464{ 465 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0; 466 struct nd_router_solicit *rs; 467 char *buf; 468 469 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) { 470 warnmsg(LOG_INFO, __func__, 471 "link-layer address option has null length" 472 " on %s. Treat as not included.", ifinfo->ifname); 473 } 474 packlen += lladdroptlen; 475 ifinfo->rs_datalen = packlen; 476 477 /* allocate buffer */ 478 if ((buf = malloc(packlen)) == NULL) { 479 warnmsg(LOG_ERR, __func__, 480 "memory allocation failed for %s", ifinfo->ifname); 481 return(-1); 482 } 483 ifinfo->rs_data = buf; 484 485 /* fill in the message */ 486 rs = (struct nd_router_solicit *)buf; 487 rs->nd_rs_type = ND_ROUTER_SOLICIT; 488 rs->nd_rs_code = 0; 489 rs->nd_rs_cksum = 0; 490 rs->nd_rs_reserved = 0; 491 buf += sizeof(*rs); 492 493 /* fill in source link-layer address option */ 494 if (lladdroptlen) 495 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf); 496 497 return(0); 498} 499 500static struct timeval * 501rtsol_check_timer() 502{ 503 static struct timeval returnval; 504 struct timeval now, rtsol_timer; 505 struct ifinfo *ifinfo; 506 int flags; 507 508 gettimeofday(&now, NULL); 509 510 rtsol_timer = tm_max; 511 512 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { 513 if (TIMEVAL_LEQ(ifinfo->expire, now)) { 514 if (dflag > 1) 515 warnmsg(LOG_DEBUG, __func__, 516 "timer expiration on %s, " 517 "state = %d", ifinfo->ifname, 518 ifinfo->state); 519 520 switch (ifinfo->state) { 521 case IFS_DOWN: 522 case IFS_TENTATIVE: 523 /* interface_up returns 0 on success */ 524 flags = interface_up(ifinfo->ifname); 525 if (flags == 0) 526 ifinfo->state = IFS_DELAY; 527 else if (flags == IFS_TENTATIVE) 528 ifinfo->state = IFS_TENTATIVE; 529 else 530 ifinfo->state = IFS_DOWN; 531 break; 532 case IFS_IDLE: 533 { 534 int oldstatus = ifinfo->active; 535 int probe = 0; 536 537 ifinfo->active = interface_status(ifinfo); 538 539 if (oldstatus != ifinfo->active) { 540 warnmsg(LOG_DEBUG, __func__, 541 "%s status is changed" 542 " from %d to %d", 543 ifinfo->ifname, 544 oldstatus, ifinfo->active); 545 probe = 1; 546 ifinfo->state = IFS_DELAY; 547 } else if (ifinfo->probeinterval && 548 (ifinfo->probetimer -= 549 ifinfo->timer.tv_sec) <= 0) { 550 /* probe timer expired */ 551 ifinfo->probetimer = 552 ifinfo->probeinterval; 553 probe = 1; 554 ifinfo->state = IFS_PROBE; 555 } 556 557 /* 558 * If we need a probe, clear the previous 559 * status wrt the "other" configuration. 560 */ 561 if (probe) 562 ifinfo->otherconfig = 0; 563 564 if (probe && mobile_node) 565 defrouter_probe(ifinfo->sdl->sdl_index); 566 break; 567 } 568 case IFS_DELAY: 569 ifinfo->state = IFS_PROBE; 570 sendpacket(ifinfo); 571 break; 572 case IFS_PROBE: 573 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 574 sendpacket(ifinfo); 575 else { 576 warnmsg(LOG_INFO, __func__, 577 "No answer after sending %d RSs", 578 ifinfo->probes); 579 ifinfo->probes = 0; 580 ifinfo->state = IFS_IDLE; 581 } 582 break; 583 } 584 rtsol_timer_update(ifinfo); 585 } 586 587 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) 588 rtsol_timer = ifinfo->expire; 589 } 590 591 if (TIMEVAL_EQ(rtsol_timer, tm_max)) { 592 warnmsg(LOG_DEBUG, __func__, "there is no timer"); 593 return(NULL); 594 } else if (TIMEVAL_LT(rtsol_timer, now)) 595 /* this may occur when the interval is too small */ 596 returnval.tv_sec = returnval.tv_usec = 0; 597 else 598 TIMEVAL_SUB(&rtsol_timer, &now, &returnval); 599 600 if (dflag > 1) 601 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld", 602 (long)returnval.tv_sec, (long)returnval.tv_usec); 603 604 return(&returnval); 605} 606 607void 608rtsol_timer_update(struct ifinfo *ifinfo) 609{ 610#define MILLION 1000000 611#define DADRETRY 10 /* XXX: adhoc */ 612 long interval; 613 struct timeval now; 614 615 bzero(&ifinfo->timer, sizeof(ifinfo->timer)); 616 617 switch (ifinfo->state) { 618 case IFS_DOWN: 619 case IFS_TENTATIVE: 620 if (++ifinfo->dadcount > DADRETRY) { 621 ifinfo->dadcount = 0; 622 ifinfo->timer.tv_sec = PROBE_INTERVAL; 623 } else 624 ifinfo->timer.tv_sec = 1; 625 break; 626 case IFS_IDLE: 627 if (mobile_node) { 628 /* XXX should be configurable */ 629 ifinfo->timer.tv_sec = 3; 630 } 631 else 632 ifinfo->timer = tm_max; /* stop timer(valid?) */ 633 break; 634 case IFS_DELAY: 635#ifndef HAVE_ARC4RANDOM 636 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 637#else 638 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 639#endif 640 ifinfo->timer.tv_sec = interval / MILLION; 641 ifinfo->timer.tv_usec = interval % MILLION; 642 break; 643 case IFS_PROBE: 644 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 645 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL; 646 else { 647 /* 648 * After sending MAX_RTR_SOLICITATIONS solicitations, 649 * we're just waiting for possible replies; there 650 * will be no more solicatation. Thus, we change 651 * the timer value to MAX_RTR_SOLICITATION_DELAY based 652 * on RFC 2461, Section 6.3.7. 653 */ 654 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY; 655 } 656 break; 657 default: 658 warnmsg(LOG_ERR, __func__, 659 "illegal interface state(%d) on %s", 660 ifinfo->state, ifinfo->ifname); 661 return; 662 } 663 664 /* reset the timer */ 665 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) { 666 ifinfo->expire = tm_max; 667 warnmsg(LOG_DEBUG, __func__, 668 "stop timer for %s", ifinfo->ifname); 669 } else { 670 gettimeofday(&now, NULL); 671 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire); 672 673 if (dflag > 1) 674 warnmsg(LOG_DEBUG, __func__, 675 "set timer for %s to %d:%d", ifinfo->ifname, 676 (int)ifinfo->timer.tv_sec, 677 (int)ifinfo->timer.tv_usec); 678 } 679 680#undef MILLION 681} 682 683/* timer related utility functions */ 684#define MILLION 1000000 685 686/* result = a + b */ 687static void 688TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) 689{ 690 long l; 691 692 if ((l = a->tv_usec + b->tv_usec) < MILLION) { 693 result->tv_usec = l; 694 result->tv_sec = a->tv_sec + b->tv_sec; 695 } else { 696 result->tv_usec = l - MILLION; 697 result->tv_sec = a->tv_sec + b->tv_sec + 1; 698 } 699} 700 701/* 702 * result = a - b 703 * XXX: this function assumes that a >= b. 704 */ 705void 706TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) 707{ 708 long l; 709 710 if ((l = a->tv_usec - b->tv_usec) >= 0) { 711 result->tv_usec = l; 712 result->tv_sec = a->tv_sec - b->tv_sec; 713 } else { 714 result->tv_usec = MILLION + l; 715 result->tv_sec = a->tv_sec - b->tv_sec - 1; 716 } 717} 718 719static void 720rtsold_set_dump_file(sig) 721 int sig; 722{ 723 do_dump = 1; 724} 725 726static void 727usage(char *progname) 728{ 729 if (progname && progname[strlen(progname) - 1] != 'd') { 730 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n"); 731 fprintf(stderr, "usage: rtsol [-dD] -a\n"); 732 } else { 733 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n"); 734 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n"); 735 } 736 exit(1); 737} 738 739void 740#if __STDC__ 741warnmsg(int priority, const char *func, const char *msg, ...) 742#else 743warnmsg(priority, func, msg, va_alist) 744 int priority; 745 const char *func; 746 const char *msg; 747 va_dcl 748#endif 749{ 750 va_list ap; 751 char buf[BUFSIZ]; 752 753 va_start(ap, msg); 754 if (fflag) { 755 if (priority <= log_upto) { 756 (void)vfprintf(stderr, msg, ap); 757 (void)fprintf(stderr, "\n"); 758 } 759 } else { 760 snprintf(buf, sizeof(buf), "<%s> %s", func, msg); 761 msg = buf; 762 vsyslog(priority, msg, ap); 763 } 764 va_end(ap); 765} 766 767static char ** 768autoifprobe() 769{ 770 static char ifname[IFNAMSIZ + 1]; 771 static char *argv[2]; 772 struct ifaddrs *ifap, *ifa, *target; 773 774 if (getifaddrs(&ifap) != 0) 775 return NULL; 776 777 target = NULL; 778 /* find an ethernet */ 779 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 780 if ((ifa->ifa_flags & IFF_UP) == 0) 781 continue; 782 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 783 continue; 784 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 785 continue; 786 if ((ifa->ifa_flags & IFF_MULTICAST) == 0) 787 continue; 788 789 if (ifa->ifa_addr->sa_family != AF_INET6) 790 continue; 791 792 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0) 793 continue; 794 795 if (!target) 796 target = ifa; 797 else { 798 /* if we find multiple candidates, failure. */ 799 if (dflag > 1) 800 warnx("multiple interfaces found"); 801 target = NULL; 802 break; 803 } 804 } 805 806 if (target) { 807 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1); 808 ifname[sizeof(ifname) - 1] = '\0'; 809 argv[0] = ifname; 810 argv[1] = NULL; 811 812 if (dflag > 0) 813 warnx("probing %s", argv[0]); 814 } 815 freeifaddrs(ifap); 816 if (target) 817 return argv; 818 else 819 return (char **)NULL; 820} 821