1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/time.h> 34 35 #include <net/if_dl.h> 36 37 #include <netinet/in.h> 38 #include <netinet/icmp6.h> 39 40 #include <signal.h> 41 #include <unistd.h> 42 #include <syslog.h> 43 #include <string.h> 44 #include <stdlib.h> 45 #include <stdio.h> 46 #include <errno.h> 47 #include <err.h> 48 #include <stdarg.h> 49 #include "rtsold.h" 50 51 struct ifinfo *iflist; 52 struct timeval tm_max = {0x7fffffff, 0x7fffffff}; 53 int dflag; 54 static int log_upto = 999; 55 static int fflag = 0; 56 57 /* protocol constatns */ 58 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */ 59 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */ 60 #define MAX_RTR_SOLICITATIONS 3 /* times */ 61 62 /* implementation dependent constants */ 63 #define PROBE_INTERVAL 60 /* secondes XXX: should be configurable */ 64 65 /* utility macros */ 66 /* a < b */ 67 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\ 68 (((a).tv_sec == (b).tv_sec) && \ 69 ((a).tv_usec < (b).tv_usec))) 70 71 /* a <= b */ 72 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\ 73 (((a).tv_sec == (b).tv_sec) &&\ 74 ((a).tv_usec <= (b).tv_usec))) 75 76 /* a == b */ 77 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec)) 78 79 int main __P((int argc, char *argv[])); 80 81 /* static variables and functions */ 82 static int mobile_node = 0; 83 static int do_dump; 84 static char *dumpfilename = "/var/tmp/rtsold.dump"; /* XXX: should be configurable */ 85 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */ 86 87 static int ifconfig __P((char *ifname)); 88 static int make_packet __P((struct ifinfo *ifinfo)); 89 static struct timeval *rtsol_check_timer __P((void)); 90 static void TIMEVAL_ADD __P((struct timeval *a, struct timeval *b, 91 struct timeval *result)); 92 static void TIMEVAL_SUB __P((struct timeval *a, struct timeval *b, 93 struct timeval *result)); 94 95 static void rtsold_set_dump_file __P(()); 96 static void usage __P((char *progname)); 97 98 int 99 main(argc, argv) 100 int argc; 101 char *argv[]; 102 { 103 int s, ch; 104 int once = 0; 105 struct timeval *timeout; 106 struct fd_set fdset; 107 char *argv0; 108 char *opts; 109 110 /* 111 * Initialization 112 */ 113 argv0 = argv[0]; 114 115 /* get option */ 116 if (argv0 && argv0[strlen(argv0) - 1] != 'd') { 117 fflag = 1; 118 once = 1; 119 opts = "dD"; 120 } else 121 opts = "dDfm1"; 122 123 while ((ch = getopt(argc, argv, opts)) != -1) { 124 switch(ch) { 125 case 'd': 126 dflag = 1; 127 break; 128 case 'D': 129 dflag = 2; 130 break; 131 case 'f': 132 fflag = 1; 133 break; 134 case 'm': 135 mobile_node = 1; 136 break; 137 case '1': 138 once = 1; 139 break; 140 default: 141 usage(argv0); 142 } 143 } 144 argc -= optind; 145 argv += optind; 146 if (argc == 0) 147 usage(argv0); 148 149 /* set log level */ 150 if (dflag == 0) 151 log_upto = LOG_NOTICE; 152 if (!fflag) { 153 char *ident; 154 ident = strrchr(argv0, '/'); 155 if (!ident) 156 ident = argv0; 157 else 158 ident++; 159 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON); 160 if (log_upto >= 0) 161 setlogmask(LOG_UPTO(log_upto)); 162 } 163 164 /* random value initilization */ 165 srandom((u_long)time(NULL)); 166 167 /* warn if accept_rtadv is down */ 168 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV)) 169 warnx("kernel is configured not to accept RAs"); 170 171 /* initialization to dump internal status to a file */ 172 if (signal(SIGUSR1, (void *)rtsold_set_dump_file) < 0) 173 errx(1, "failed to set signal for dump status"); 174 175 /* configuration per interface */ 176 if (ifinit()) 177 errx(1, "failed to initilizatoin interfaces"); 178 while (argc--) { 179 if (ifconfig(*argv)) 180 errx(1, "failed to initilize %s", *argv); 181 argv++; 182 } 183 184 /* open a socket for sending RS and receiving RA */ 185 if ((s = sockopen()) < 0) 186 errx(1, "failed to open a socket"); 187 188 /* setup for probing default routers */ 189 if (probe_init()) 190 errx(1, "failed to setup for probing routers"); 191 192 if (!fflag) 193 daemon(0, 0); /* act as a daemon */ 194 195 /* dump the current pid */ 196 if (!once) { 197 pid_t pid = getpid(); 198 FILE *fp; 199 200 if ((fp = fopen(pidfilename, "w")) == NULL) 201 warnmsg(LOG_ERR, __FUNCTION__, 202 "failed to open a log file(%s)", 203 pidfilename, strerror(errno)); 204 else { 205 fprintf(fp, "%d\n", pid); 206 fclose(fp); 207 } 208 } 209 210 FD_ZERO(&fdset); 211 FD_SET(s, &fdset); 212 while (1) { /* main loop */ 213 extern int errno; 214 int e; 215 struct fd_set select_fd = fdset; 216 217 if (do_dump) { /* SIGUSR1 */ 218 do_dump = 0; 219 rtsold_dump_file(dumpfilename); 220 } 221 222 timeout = rtsol_check_timer(); 223 224 if (once) { 225 struct ifinfo *ifi; 226 227 /* if we have no timeout, we are done (or failed) */ 228 if (timeout == NULL) 229 break; 230 231 /* if all interfaces have got RA packet, we are done */ 232 for (ifi = iflist; ifi; ifi = ifi->next) { 233 if (ifi->state != IFS_DOWN && ifi->racnt == 0) 234 break; 235 } 236 if (ifi == NULL) 237 break; 238 } 239 240 if ((e = select(s + 1, &select_fd, NULL, NULL, timeout)) < 1) { 241 if (e < 0 && errno != EINTR) { 242 warnmsg(LOG_ERR, __FUNCTION__, "select: %s", 243 strerror(errno)); 244 } 245 continue; 246 } 247 248 /* packet reception */ 249 if (FD_ISSET(s, &fdset)) 250 rtsol_input(s); 251 } 252 /* NOTREACHED */ 253 254 return 0; 255 } 256 257 static int 258 ifconfig(char *ifname) 259 { 260 struct ifinfo *ifinfo; 261 struct sockaddr_dl *sdl; 262 int flags; 263 264 if ((sdl = if_nametosdl(ifname)) == NULL) { 265 warnmsg(LOG_ERR, __FUNCTION__, 266 "failed to get link layer information for %s", ifname); 267 return(-1); 268 } 269 if (find_ifinfo(sdl->sdl_index)) { 270 warnmsg(LOG_ERR, __FUNCTION__, 271 "interface %s was already cofigured", ifname); 272 return(-1); 273 } 274 275 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) { 276 warnmsg(LOG_ERR, __FUNCTION__, "memory allocation failed"); 277 return(-1); 278 } 279 memset(ifinfo, 0, sizeof(*ifinfo)); 280 ifinfo->sdl = sdl; 281 282 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname)); 283 284 /* construct a router solicitation message */ 285 if (make_packet(ifinfo)) 286 goto bad; 287 288 /* 289 * check if the interface is available. 290 * also check if SIOCGIFMEDIA ioctl is OK on the interface. 291 */ 292 ifinfo->mediareqok = 1; 293 ifinfo->active = interface_status(ifinfo); 294 if (!ifinfo->mediareqok) { 295 /* 296 * probe routers periodically even if the link status 297 * does not change. 298 */ 299 ifinfo->probeinterval = PROBE_INTERVAL; 300 } 301 302 /* activate interface: interface_up returns 0 on success */ 303 flags = interface_up(ifinfo->ifname); 304 if (flags == 0) 305 ifinfo->state = IFS_DELAY; 306 else if (flags == IFS_TENTATIVE) 307 ifinfo->state = IFS_TENTATIVE; 308 else 309 ifinfo->state = IFS_DOWN; 310 311 rtsol_timer_update(ifinfo); 312 313 /* link into chain */ 314 if (iflist) 315 ifinfo->next = iflist; 316 iflist = ifinfo; 317 318 return(0); 319 320 bad: 321 free(ifinfo); 322 free(ifinfo->sdl); 323 return(-1); 324 } 325 326 struct ifinfo * 327 find_ifinfo(int ifindex) 328 { 329 struct ifinfo *ifi; 330 331 for (ifi = iflist; ifi; ifi = ifi->next) 332 if (ifi->sdl->sdl_index == ifindex) 333 return(ifi); 334 335 return(NULL); 336 } 337 338 static int 339 make_packet(struct ifinfo *ifinfo) 340 { 341 char *buf; 342 struct nd_router_solicit *rs; 343 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0; 344 345 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) { 346 warnmsg(LOG_INFO, __FUNCTION__, 347 "link-layer address option has null length" 348 " on %s. Treat as not included.", ifinfo->ifname); 349 } 350 packlen += lladdroptlen; 351 ifinfo->rs_datalen = packlen; 352 353 /* allocate buffer */ 354 if ((buf = malloc(packlen)) == NULL) { 355 warnmsg(LOG_ERR, __FUNCTION__, 356 "memory allocation failed for %s", ifinfo->ifname); 357 return(-1); 358 } 359 ifinfo->rs_data = buf; 360 361 /* fill in the message */ 362 rs = (struct nd_router_solicit *)buf; 363 rs->nd_rs_type = ND_ROUTER_SOLICIT; 364 rs->nd_rs_code = 0; 365 rs->nd_rs_cksum = 0; 366 rs->nd_rs_reserved = 0; 367 buf += sizeof(*rs); 368 369 /* fill in source link-layer address option */ 370 if (lladdroptlen) 371 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf); 372 373 return(0); 374 } 375 376 static struct timeval * 377 rtsol_check_timer() 378 { 379 static struct timeval returnval; 380 struct timeval now, rtsol_timer; 381 struct ifinfo *ifinfo; 382 int flags; 383 384 gettimeofday(&now, NULL); 385 386 rtsol_timer = tm_max; 387 388 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { 389 if (TIMEVAL_LEQ(ifinfo->expire, now)) { 390 if (dflag > 1) 391 warnmsg(LOG_DEBUG, __FUNCTION__, 392 "timer expiration on %s, " 393 "state = %d", ifinfo->ifname, 394 ifinfo->state); 395 396 switch(ifinfo->state) { 397 case IFS_DOWN: 398 case IFS_TENTATIVE: 399 /* interface_up returns 0 on success */ 400 flags = interface_up(ifinfo->ifname); 401 if (flags == 0) 402 ifinfo->state = IFS_DELAY; 403 else if (flags == IFS_TENTATIVE) 404 ifinfo->state = IFS_TENTATIVE; 405 else 406 ifinfo->state = IFS_DOWN; 407 break; 408 case IFS_IDLE: 409 { 410 int oldstatus = ifinfo->active; 411 int probe = 0; 412 413 ifinfo->active = 414 interface_status(ifinfo); 415 416 if (oldstatus != ifinfo->active) { 417 warnmsg(LOG_DEBUG, __FUNCTION__, 418 "%s status is changed" 419 " from %d to %d", 420 ifinfo->ifname, 421 oldstatus, ifinfo->active); 422 probe = 1; 423 ifinfo->state = IFS_DELAY; 424 } 425 else if (ifinfo->probeinterval && 426 (ifinfo->probetimer -= 427 ifinfo->timer.tv_sec) <= 0) { 428 /* probe timer expired */ 429 ifinfo->probetimer = 430 ifinfo->probeinterval; 431 probe = 1; 432 ifinfo->state = IFS_PROBE; 433 } 434 435 if (probe && mobile_node) 436 defrouter_probe(ifinfo->sdl->sdl_index); 437 break; 438 } 439 case IFS_DELAY: 440 ifinfo->state = IFS_PROBE; 441 sendpacket(ifinfo); 442 break; 443 case IFS_PROBE: 444 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 445 sendpacket(ifinfo); 446 else { 447 warnmsg(LOG_INFO, __FUNCTION__, 448 "No answer " 449 "after sending %d RSs", 450 ifinfo->probes); 451 ifinfo->probes = 0; 452 ifinfo->state = IFS_IDLE; 453 } 454 break; 455 } 456 rtsol_timer_update(ifinfo); 457 } 458 459 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) 460 rtsol_timer = ifinfo->expire; 461 } 462 463 if (TIMEVAL_EQ(rtsol_timer, tm_max)) { 464 warnmsg(LOG_DEBUG, __FUNCTION__, "there is no timer"); 465 return(NULL); 466 } 467 else if (TIMEVAL_LT(rtsol_timer, now)) 468 /* this may occur when the interval is too small */ 469 returnval.tv_sec = returnval.tv_usec = 0; 470 else 471 TIMEVAL_SUB(&rtsol_timer, &now, &returnval); 472 473 if (dflag > 1) 474 warnmsg(LOG_DEBUG, __FUNCTION__, "New timer is %d:%08d", 475 returnval.tv_sec, returnval.tv_usec); 476 477 return(&returnval); 478 } 479 480 void 481 rtsol_timer_update(struct ifinfo *ifinfo) 482 { 483 #define MILLION 1000000 484 #define DADRETRY 10 /* XXX: adhoc */ 485 long interval; 486 struct timeval now; 487 488 bzero(&ifinfo->timer, sizeof(ifinfo->timer)); 489 490 switch (ifinfo->state) { 491 case IFS_DOWN: 492 case IFS_TENTATIVE: 493 if (++ifinfo->dadcount > DADRETRY) { 494 ifinfo->dadcount = 0; 495 ifinfo->timer.tv_sec = PROBE_INTERVAL; 496 } 497 else 498 ifinfo->timer.tv_sec = 1; 499 break; 500 case IFS_IDLE: 501 if (mobile_node) { 502 /* XXX should be configurable */ 503 ifinfo->timer.tv_sec = 3; 504 } 505 else 506 ifinfo->timer = tm_max; /* stop timer(valid?) */ 507 break; 508 case IFS_DELAY: 509 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 510 ifinfo->timer.tv_sec = interval / MILLION; 511 ifinfo->timer.tv_usec = interval % MILLION; 512 break; 513 case IFS_PROBE: 514 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL; 515 break; 516 default: 517 warnmsg(LOG_ERR, __FUNCTION__, 518 "illegal interface state(%d) on %s", 519 ifinfo->state, ifinfo->ifname); 520 return; 521 } 522 523 /* reset the timer */ 524 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) { 525 ifinfo->expire = tm_max; 526 warnmsg(LOG_DEBUG, __FUNCTION__, 527 "stop timer for %s", ifinfo->ifname); 528 } 529 else { 530 gettimeofday(&now, NULL); 531 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire); 532 533 if (dflag > 1) 534 warnmsg(LOG_DEBUG, __FUNCTION__, 535 "set timer for %s to %d:%d", ifinfo->ifname, 536 (int)ifinfo->timer.tv_sec, 537 (int)ifinfo->timer.tv_usec); 538 } 539 540 #undef MILLION 541 } 542 543 /* timer related utility functions */ 544 #define MILLION 1000000 545 546 /* result = a + b */ 547 static void 548 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) 549 { 550 long l; 551 552 if ((l = a->tv_usec + b->tv_usec) < MILLION) { 553 result->tv_usec = l; 554 result->tv_sec = a->tv_sec + b->tv_sec; 555 } 556 else { 557 result->tv_usec = l - MILLION; 558 result->tv_sec = a->tv_sec + b->tv_sec + 1; 559 } 560 } 561 562 /* 563 * result = a - b 564 * XXX: this function assumes that a >= b. 565 */ 566 void 567 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) 568 { 569 long l; 570 571 if ((l = a->tv_usec - b->tv_usec) >= 0) { 572 result->tv_usec = l; 573 result->tv_sec = a->tv_sec - b->tv_sec; 574 } 575 else { 576 result->tv_usec = MILLION + l; 577 result->tv_sec = a->tv_sec - b->tv_sec - 1; 578 } 579 } 580 581 static void 582 rtsold_set_dump_file() 583 { 584 do_dump = 1; 585 } 586 587 static void 588 usage(char *progname) 589 { 590 if (progname && progname[strlen(progname) - 1] != 'd') 591 fprintf(stderr, "usage: rtsol [-dD] interfaces\n"); 592 else 593 fprintf(stderr, "usage: rtsold [-dDfm1] interfaces\n"); 594 exit(1); 595 } 596 597 void 598 #if __STDC__ 599 warnmsg(int priority, const char *func, const char *msg, ...) 600 #else 601 warnmsg(priority, func, msg, va_alist) 602 int priority; 603 const char *func; 604 const char *msg; 605 va_dcl 606 #endif 607 { 608 va_list ap; 609 char buf[BUFSIZ]; 610 611 va_start(ap, msg); 612 if (fflag) { 613 if (priority <= log_upto) { 614 (void)vfprintf(stderr, msg, ap); 615 (void)fprintf(stderr, "\n"); 616 } 617 } else { 618 snprintf(buf, sizeof(buf), "<%s> %s", func, msg); 619 vsyslog(priority, buf, ap); 620 } 621 va_end(ap); 622 } 623