1 /* $FreeBSD$ */ 2 /* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char _rcsid[] = "$KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $"; 35 #endif 36 37 #include <stdio.h> 38 39 #include <time.h> 40 #include <unistd.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <signal.h> 44 #ifdef __STDC__ 45 #include <stdarg.h> 46 #else 47 #include <varargs.h> 48 #endif 49 #include <syslog.h> 50 #include <stddef.h> 51 #include <errno.h> 52 #include <err.h> 53 #ifdef HAVE_POLL_H 54 #include <poll.h> 55 #endif 56 57 #include <sys/types.h> 58 #include <sys/param.h> 59 #include <sys/file.h> 60 #include <sys/socket.h> 61 #include <sys/ioctl.h> 62 #include <sys/sysctl.h> 63 #include <sys/uio.h> 64 #include <net/if.h> 65 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 66 #include <net/if_var.h> 67 #endif /* __FreeBSD__ >= 3 */ 68 #define KERNEL 1 69 #define _KERNEL 1 70 #include <net/route.h> 71 #undef KERNEL 72 #undef _KERNEL 73 #include <netinet/in.h> 74 #include <netinet/in_var.h> 75 #include <netinet/ip6.h> 76 #include <netinet/udp.h> 77 #include <netdb.h> 78 #include <ifaddrs.h> 79 80 #include <arpa/inet.h> 81 82 #include "route6d.h" 83 84 #define MAXFILTER 40 85 86 #ifdef DEBUG 87 #define INIT_INTERVAL6 6 88 #else 89 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */ 90 #endif 91 92 /* alignment constraint for routing socket */ 93 #define ROUNDUP(a) \ 94 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 95 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 96 97 /* 98 * Following two macros are highly depending on KAME Release 99 */ 100 #define IN6_LINKLOCAL_IFINDEX(addr) \ 101 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3]) 102 103 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \ 104 do { \ 105 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \ 106 (addr).s6_addr[3] = (index) & 0xff; \ 107 } while (0) 108 109 struct ifc { /* Configuration of an interface */ 110 char *ifc_name; /* if name */ 111 struct ifc *ifc_next; 112 int ifc_index; /* if index */ 113 int ifc_mtu; /* if mtu */ 114 int ifc_metric; /* if metric */ 115 u_int ifc_flags; /* flags */ 116 short ifc_cflags; /* IFC_XXX */ 117 struct in6_addr ifc_mylladdr; /* my link-local address */ 118 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */ 119 struct iff *ifc_filter; /* filter structure */ 120 struct ifac *ifc_addr; /* list of AF_INET6 addresses */ 121 int ifc_joined; /* joined to ff02::9 */ 122 }; 123 124 struct ifac { /* Adddress associated to an interface */ 125 struct ifc *ifa_conf; /* back pointer */ 126 struct ifac *ifa_next; 127 struct in6_addr ifa_addr; /* address */ 128 struct in6_addr ifa_raddr; /* remote address, valid in p2p */ 129 int ifa_plen; /* prefix length */ 130 }; 131 132 struct iff { 133 int iff_type; 134 struct in6_addr iff_addr; 135 int iff_plen; 136 struct iff *iff_next; 137 }; 138 139 struct ifc *ifc; 140 int nifc; /* number of valid ifc's */ 141 struct ifc **index2ifc; 142 int nindex2ifc; 143 struct ifc *loopifcp = NULL; /* pointing to loopback */ 144 #ifdef HAVE_POLL_H 145 struct pollfd set[2]; 146 #else 147 fd_set *sockvecp; /* vector to select() for receiving */ 148 fd_set *recvecp; 149 int fdmasks; 150 int maxfd; /* maximum fd for select() */ 151 #endif 152 int rtsock; /* the routing socket */ 153 int ripsock; /* socket to send/receive RIP datagram */ 154 155 struct rip6 *ripbuf; /* packet buffer for sending */ 156 157 /* 158 * Maintain the routes in a linked list. When the number of the routes 159 * grows, somebody would like to introduce a hash based or a radix tree 160 * based structure. I believe the number of routes handled by RIP is 161 * limited and I don't have to manage a complex data structure, however. 162 * 163 * One of the major drawbacks of the linear linked list is the difficulty 164 * of representing the relationship between a couple of routes. This may 165 * be a significant problem when we have to support route aggregation with 166 * supressing the specifices covered by the aggregate. 167 */ 168 169 struct riprt { 170 struct riprt *rrt_next; /* next destination */ 171 struct riprt *rrt_same; /* same destination - future use */ 172 struct netinfo6 rrt_info; /* network info */ 173 struct in6_addr rrt_gw; /* gateway */ 174 u_long rrt_flags; /* kernel routing table flags */ 175 u_long rrt_rflags; /* route6d routing table flags */ 176 time_t rrt_t; /* when the route validated */ 177 int rrt_index; /* ifindex from which this route got */ 178 }; 179 180 struct riprt *riprt = 0; 181 182 int dflag = 0; /* debug flag */ 183 int qflag = 0; /* quiet flag */ 184 int nflag = 0; /* don't update kernel routing table */ 185 int aflag = 0; /* age out even the statically defined routes */ 186 int hflag = 0; /* don't split horizon */ 187 int lflag = 0; /* exchange site local routes */ 188 int sflag = 0; /* announce static routes w/ split horizon */ 189 int Sflag = 0; /* announce static routes to every interface */ 190 unsigned long routetag = 0; /* route tag attached on originating case */ 191 192 char *filter[MAXFILTER]; 193 int filtertype[MAXFILTER]; 194 int nfilter = 0; 195 196 pid_t pid; 197 198 struct sockaddr_storage ripsin; 199 200 struct rtentry rtentry; 201 202 int interval = 1; 203 time_t nextalarm = 0; 204 time_t sup_trig_update = 0; 205 206 FILE *rtlog = NULL; 207 208 int logopened = 0; 209 210 static u_long seq = 0; 211 212 volatile sig_atomic_t seenalrm; 213 volatile sig_atomic_t seenquit; 214 volatile sig_atomic_t seenusr1; 215 216 #define RRTF_AGGREGATE 0x08000000 217 #define RRTF_NOADVERTISE 0x10000000 218 #define RRTF_NH_NOT_LLADDR 0x20000000 219 #define RRTF_SENDANYWAY 0x40000000 220 #define RRTF_CHANGED 0x80000000 221 222 int main __P((int, char **)); 223 void sighandler __P((int)); 224 void ripalarm __P((void)); 225 void riprecv __P((void)); 226 void ripsend __P((struct ifc *, struct sockaddr_in6 *, int)); 227 int out_filter __P((struct riprt *, struct ifc *)); 228 void init __P((void)); 229 void sockopt __P((struct ifc *)); 230 void ifconfig __P((void)); 231 void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int)); 232 void rtrecv __P((void)); 233 int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *, 234 const struct sockaddr_in6 *)); 235 int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *, 236 const struct sockaddr_in6 *)); 237 void filterconfig __P((void)); 238 int getifmtu __P((int)); 239 const char *rttypes __P((struct rt_msghdr *)); 240 const char *rtflags __P((struct rt_msghdr *)); 241 const char *ifflags __P((int)); 242 int ifrt __P((struct ifc *, int)); 243 void ifrt_p2p __P((struct ifc *, int)); 244 void applymask __P((struct in6_addr *, struct in6_addr *)); 245 void applyplen __P((struct in6_addr *, int)); 246 void ifrtdump __P((int)); 247 void ifdump __P((int)); 248 void ifdump0 __P((FILE *, const struct ifc *)); 249 void rtdump __P((int)); 250 void rt_entry __P((struct rt_msghdr *, int)); 251 void rtdexit __P((void)); 252 void riprequest __P((struct ifc *, struct netinfo6 *, int, 253 struct sockaddr_in6 *)); 254 void ripflush __P((struct ifc *, struct sockaddr_in6 *)); 255 void sendrequest __P((struct ifc *)); 256 int sin6mask2len __P((const struct sockaddr_in6 *)); 257 int mask2len __P((const struct in6_addr *, int)); 258 int sendpacket __P((struct sockaddr_in6 *, int)); 259 int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *)); 260 int delroute __P((struct netinfo6 *, struct in6_addr *)); 261 struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *)); 262 void krtread __P((int)); 263 int tobeadv __P((struct riprt *, struct ifc *)); 264 char *allocopy __P((char *)); 265 char *hms __P((void)); 266 const char *inet6_n2p __P((const struct in6_addr *)); 267 struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int)); 268 struct in6_addr *plen2mask __P((int)); 269 struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **)); 270 int ripinterval __P((int)); 271 time_t ripsuptrig __P((void)); 272 void fatal __P((const char *, ...)) 273 __attribute__((__format__(__printf__, 1, 2))); 274 void trace __P((int, const char *, ...)) 275 __attribute__((__format__(__printf__, 2, 3))); 276 void tracet __P((int, const char *, ...)) 277 __attribute__((__format__(__printf__, 2, 3))); 278 unsigned int if_maxindex __P((void)); 279 struct ifc *ifc_find __P((char *)); 280 struct iff *iff_find __P((struct ifc *, int)); 281 void setindex2ifc __P((int, struct ifc *)); 282 283 #define MALLOC(type) ((type *)malloc(sizeof(type))) 284 285 int 286 main(argc, argv) 287 int argc; 288 char **argv; 289 { 290 int ch; 291 int error = 0; 292 struct ifc *ifcp; 293 sigset_t mask, omask; 294 FILE *pidfile; 295 char *progname; 296 char *ep; 297 298 progname = strrchr(*argv, '/'); 299 if (progname) 300 progname++; 301 else 302 progname = *argv; 303 304 pid = getpid(); 305 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) { 306 switch (ch) { 307 case 'A': 308 case 'N': 309 case 'O': 310 case 'T': 311 case 'L': 312 if (nfilter >= MAXFILTER) { 313 fatal("Exceeds MAXFILTER"); 314 /*NOTREACHED*/ 315 } 316 filtertype[nfilter] = ch; 317 filter[nfilter++] = allocopy(optarg); 318 break; 319 case 't': 320 ep = NULL; 321 routetag = strtoul(optarg, &ep, 0); 322 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) { 323 fatal("invalid route tag"); 324 /*NOTREACHED*/ 325 } 326 break; 327 case 'R': 328 if ((rtlog = fopen(optarg, "w")) == NULL) { 329 fatal("Can not write to routelog"); 330 /*NOTREACHED*/ 331 } 332 break; 333 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0) 334 FLAG('a', aflag, 1); break; 335 FLAG('d', dflag, 1); break; 336 FLAG('D', dflag, 2); break; 337 FLAG('h', hflag, 1); break; 338 FLAG('l', lflag, 1); break; 339 FLAG('n', nflag, 1); break; 340 FLAG('q', qflag, 1); break; 341 FLAG('s', sflag, 1); break; 342 FLAG('S', Sflag, 1); break; 343 #undef FLAG 344 default: 345 fatal("Invalid option specified, terminating"); 346 /*NOTREACHED*/ 347 } 348 } 349 argc -= optind; 350 argv += optind; 351 if (argc > 0) { 352 fatal("bogus extra arguments"); 353 /*NOTREACHED*/ 354 } 355 356 if (geteuid()) { 357 nflag = 1; 358 fprintf(stderr, "No kernel update is allowed\n"); 359 } 360 361 if (dflag == 0) { 362 if (daemon(0, 0) < 0) { 363 fatal("daemon"); 364 /*NOTREACHED*/ 365 } 366 } 367 368 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON); 369 logopened++; 370 371 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) 372 fatal("malloc"); 373 memset(ripbuf, 0, RIP6_MAXMTU); 374 ripbuf->rip6_cmd = RIP6_RESPONSE; 375 ripbuf->rip6_vers = RIP6_VERSION; 376 ripbuf->rip6_res1[0] = 0; 377 ripbuf->rip6_res1[1] = 0; 378 379 init(); 380 ifconfig(); 381 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) { 382 if (ifcp->ifc_index < 0) { 383 fprintf(stderr, 384 "No ifindex found at %s (no link-local address?)\n", 385 ifcp->ifc_name); 386 error++; 387 } 388 } 389 if (error) 390 exit(1); 391 if (loopifcp == NULL) { 392 fatal("No loopback found"); 393 /*NOTREACHED*/ 394 } 395 #ifdef __FreeBSD__ 396 sranddev(); 397 #else 398 srand((unsigned)(time(NULL)^(pid<<16))); 399 #endif 400 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) 401 ifrt(ifcp, 0); 402 filterconfig(); 403 krtread(0); 404 if (dflag) 405 ifrtdump(0); 406 407 pid = getpid(); 408 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) { 409 fprintf(pidfile, "%d\n", pid); 410 fclose(pidfile); 411 } 412 413 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) { 414 fatal("malloc"); 415 /*NOTREACHED*/ 416 } 417 memset(ripbuf, 0, RIP6_MAXMTU); 418 ripbuf->rip6_cmd = RIP6_RESPONSE; 419 ripbuf->rip6_vers = RIP6_VERSION; 420 ripbuf->rip6_res1[0] = 0; 421 ripbuf->rip6_res1[1] = 0; 422 423 if (signal(SIGALRM, sighandler) == SIG_ERR || 424 signal(SIGQUIT, sighandler) == SIG_ERR || 425 signal(SIGTERM, sighandler) == SIG_ERR || 426 signal(SIGUSR1, sighandler) == SIG_ERR || 427 signal(SIGHUP, sighandler) == SIG_ERR || 428 signal(SIGINT, sighandler) == SIG_ERR) { 429 fatal("signal"); 430 /*NOTREACHED*/ 431 } 432 /* 433 * To avoid rip packet congestion (not on a cable but in this 434 * process), wait for a moment to send the first RIP6_RESPONSE 435 * packets. 436 */ 437 alarm(ripinterval(INIT_INTERVAL6)); 438 439 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) { 440 if (iff_find(ifcp, 'N')) 441 continue; 442 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP)) 443 sendrequest(ifcp); 444 } 445 446 syslog(LOG_INFO, "**** Started ****"); 447 sigemptyset(&mask); 448 sigaddset(&mask, SIGALRM); 449 while (1) { 450 if (seenalrm) { 451 ripalarm(); 452 seenalrm = 0; 453 continue; 454 } 455 if (seenquit) { 456 rtdexit(); 457 seenquit = 0; 458 continue; 459 } 460 if (seenusr1) { 461 ifrtdump(SIGUSR1); 462 seenusr1 = 0; 463 continue; 464 } 465 466 #ifdef HAVE_POLL_H 467 switch (poll(set, 2, INFTIM)) 468 #else 469 memcpy(recvecp, sockvecp, fdmasks); 470 switch (select(maxfd + 1, recvecp, 0, 0, 0)) 471 #endif 472 { 473 case -1: 474 if (errno != EINTR) { 475 fatal("select"); 476 /*NOTREACHED*/ 477 } 478 continue; 479 case 0: 480 continue; 481 default: 482 #ifdef HAVE_POLL_H 483 if (set[0].revents & POLLIN) 484 #else 485 if (FD_ISSET(ripsock, recvecp)) 486 #endif 487 { 488 sigprocmask(SIG_BLOCK, &mask, &omask); 489 riprecv(); 490 sigprocmask(SIG_SETMASK, &omask, NULL); 491 } 492 #ifdef HAVE_POLL_H 493 if (set[1].revents & POLLIN) 494 #else 495 if (FD_ISSET(rtsock, recvecp)) 496 #endif 497 { 498 sigprocmask(SIG_BLOCK, &mask, &omask); 499 rtrecv(); 500 sigprocmask(SIG_SETMASK, &omask, NULL); 501 } 502 } 503 } 504 } 505 506 void 507 sighandler(signo) 508 int signo; 509 { 510 511 switch (signo) { 512 case SIGALRM: 513 seenalrm++; 514 break; 515 case SIGQUIT: 516 case SIGTERM: 517 seenquit++; 518 break; 519 case SIGUSR1: 520 case SIGHUP: 521 case SIGINT: 522 seenusr1++; 523 break; 524 } 525 } 526 527 /* 528 * gracefully exits after resetting sockopts. 529 */ 530 /* ARGSUSED */ 531 void 532 rtdexit() 533 { 534 struct riprt *rrt; 535 536 alarm(0); 537 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 538 if (rrt->rrt_rflags & RRTF_AGGREGATE) { 539 delroute(&rrt->rrt_info, &rrt->rrt_gw); 540 } 541 } 542 close(ripsock); 543 close(rtsock); 544 syslog(LOG_INFO, "**** Terminated ****"); 545 closelog(); 546 exit(1); 547 } 548 549 /* 550 * Called periodically: 551 * 1. age out the learned route. remove it if necessary. 552 * 2. submit RIP6_RESPONSE packets. 553 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have 554 * to invoke this function in every 1 or 5 or 10 seconds only to age the 555 * routes more precisely. 556 */ 557 /* ARGSUSED */ 558 void 559 ripalarm() 560 { 561 struct ifc *ifcp; 562 struct riprt *rrt, *rrt_prev, *rrt_next; 563 time_t t_lifetime, t_holddown; 564 565 /* age the RIP routes */ 566 rrt_prev = 0; 567 t_lifetime = time(NULL) - RIP_LIFETIME; 568 t_holddown = t_lifetime - RIP_HOLDDOWN; 569 for (rrt = riprt; rrt; rrt = rrt_next) { 570 rrt_next = rrt->rrt_next; 571 572 if (rrt->rrt_t == 0) { 573 rrt_prev = rrt; 574 continue; 575 } 576 if (rrt->rrt_t < t_holddown) { 577 if (rrt_prev) { 578 rrt_prev->rrt_next = rrt->rrt_next; 579 } else { 580 riprt = rrt->rrt_next; 581 } 582 delroute(&rrt->rrt_info, &rrt->rrt_gw); 583 free(rrt); 584 continue; 585 } 586 if (rrt->rrt_t < t_lifetime) 587 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6; 588 rrt_prev = rrt; 589 } 590 /* Supply updates */ 591 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) { 592 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP)) 593 ripsend(ifcp, &ifcp->ifc_ripsin, 0); 594 } 595 alarm(ripinterval(SUPPLY_INTERVAL6)); 596 } 597 598 void 599 init() 600 { 601 int error; 602 const int int0 = 0, int1 = 1, int255 = 255; 603 struct addrinfo hints, *res; 604 char port[10]; 605 606 ifc = (struct ifc *)NULL; 607 nifc = 0; 608 nindex2ifc = 0; /*initial guess*/ 609 index2ifc = NULL; 610 snprintf(port, sizeof(port), "%d", RIP6_PORT); 611 612 memset(&hints, 0, sizeof(hints)); 613 hints.ai_family = PF_INET6; 614 hints.ai_socktype = SOCK_DGRAM; 615 hints.ai_flags = AI_PASSIVE; 616 error = getaddrinfo(NULL, port, &hints, &res); 617 if (error) { 618 fatal("%s", gai_strerror(error)); 619 /*NOTREACHED*/ 620 } 621 if (res->ai_next) { 622 fatal(":: resolved to multiple address"); 623 /*NOTREACHED*/ 624 } 625 626 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 627 if (ripsock < 0) { 628 fatal("rip socket"); 629 /*NOTREACHED*/ 630 } 631 #ifdef IPV6_V6ONLY 632 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY, 633 &int1, sizeof(int1)) < 0) { 634 fatal("rip IPV6_V6ONLY"); 635 /*NOTREACHED*/ 636 } 637 #endif 638 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) { 639 fatal("rip bind"); 640 /*NOTREACHED*/ 641 } 642 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 643 &int255, sizeof(int255)) < 0) { 644 fatal("rip IPV6_MULTICAST_HOPS"); 645 /*NOTREACHED*/ 646 } 647 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 648 &int0, sizeof(int0)) < 0) { 649 fatal("rip IPV6_MULTICAST_LOOP"); 650 /*NOTREACHED*/ 651 } 652 653 #ifdef IPV6_RECVPKTINFO 654 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, 655 &int1, sizeof(int1)) < 0) { 656 fatal("rip IPV6_RECVPKTINFO"); 657 /*NOTREACHED*/ 658 } 659 #else /* old adv. API */ 660 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, 661 &int1, sizeof(int1)) < 0) { 662 fatal("rip IPV6_PKTINFO"); 663 /*NOTREACHED*/ 664 } 665 #endif 666 667 memset(&hints, 0, sizeof(hints)); 668 hints.ai_family = PF_INET6; 669 hints.ai_socktype = SOCK_DGRAM; 670 error = getaddrinfo(RIP6_DEST, port, &hints, &res); 671 if (error) { 672 fatal("%s", gai_strerror(error)); 673 /*NOTREACHED*/ 674 } 675 if (res->ai_next) { 676 fatal("%s resolved to multiple address", RIP6_DEST); 677 /*NOTREACHED*/ 678 } 679 memcpy(&ripsin, res->ai_addr, res->ai_addrlen); 680 681 #ifdef HAVE_POLL_H 682 set[0].fd = ripsock; 683 set[0].events = POLLIN; 684 #else 685 maxfd = ripsock; 686 #endif 687 688 if (nflag == 0) { 689 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 690 fatal("route socket"); 691 /*NOTREACHED*/ 692 } 693 #ifdef HAVE_POLL_H 694 set[1].fd = rtsock; 695 set[1].events = POLLIN; 696 #else 697 if (rtsock > maxfd) 698 maxfd = rtsock; 699 #endif 700 } else { 701 #ifdef HAVE_POLL_H 702 set[1].fd = -1; 703 #else 704 rtsock = -1; /*just for safety */ 705 #endif 706 } 707 708 #ifndef HAVE_POLL_H 709 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask); 710 if ((sockvecp = malloc(fdmasks)) == NULL) { 711 fatal("malloc"); 712 /*NOTREACHED*/ 713 } 714 if ((recvecp = malloc(fdmasks)) == NULL) { 715 fatal("malloc"); 716 /*NOTREACHED*/ 717 } 718 memset(sockvecp, 0, fdmasks); 719 FD_SET(ripsock, sockvecp); 720 if (rtsock >= 0) 721 FD_SET(rtsock, sockvecp); 722 #endif 723 } 724 725 #define RIPSIZE(n) \ 726 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6)) 727 728 /* 729 * ripflush flushes the rip datagram stored in the rip buffer 730 */ 731 static int nrt; 732 static struct netinfo6 *np; 733 734 void 735 ripflush(ifcp, sin6) 736 struct ifc *ifcp; 737 struct sockaddr_in6 *sin6; 738 { 739 int i; 740 int error; 741 742 if (ifcp) 743 tracet(1, "Send(%s): info(%d) to %s.%d\n", 744 ifcp->ifc_name, nrt, 745 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port)); 746 else 747 tracet(1, "Send: info(%d) to %s.%d\n", 748 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port)); 749 if (dflag >= 2) { 750 np = ripbuf->rip6_nets; 751 for (i = 0; i < nrt; i++, np++) { 752 if (np->rip6_metric == NEXTHOP_METRIC) { 753 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) 754 trace(2, " NextHop reset"); 755 else { 756 trace(2, " NextHop %s", 757 inet6_n2p(&np->rip6_dest)); 758 } 759 } else { 760 trace(2, " %s/%d[%d]", 761 inet6_n2p(&np->rip6_dest), 762 np->rip6_plen, np->rip6_metric); 763 } 764 if (np->rip6_tag) { 765 trace(2, " tag=0x%04x", 766 ntohs(np->rip6_tag) & 0xffff); 767 } 768 trace(2, "\n"); 769 } 770 } 771 error = sendpacket(sin6, RIPSIZE(nrt)); 772 if (error == EAFNOSUPPORT) { 773 /* Protocol not supported */ 774 tracet(1, "Could not send info to %s (%s): " 775 "set IFF_UP to 0\n", 776 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr)); 777 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */ 778 } 779 nrt = 0; np = ripbuf->rip6_nets; 780 } 781 782 /* 783 * Generate RIP6_RESPONSE packets and send them. 784 */ 785 void 786 ripsend(ifcp, sin6, flag) 787 struct ifc *ifcp; 788 struct sockaddr_in6 *sin6; 789 int flag; 790 { 791 struct riprt *rrt; 792 struct in6_addr *nh; /* next hop */ 793 int maxrte; 794 795 if (ifcp == NULL) { 796 /* 797 * Request from non-link local address is not 798 * a regular route6d update. 799 */ 800 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) - 801 sizeof(struct udphdr) - 802 sizeof(struct rip6) + sizeof(struct netinfo6)) / 803 sizeof(struct netinfo6); 804 nrt = 0; np = ripbuf->rip6_nets; nh = NULL; 805 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 806 if (rrt->rrt_rflags & RRTF_NOADVERTISE) 807 continue; 808 /* Put the route to the buffer */ 809 *np = rrt->rrt_info; 810 np++; nrt++; 811 if (nrt == maxrte) { 812 ripflush(NULL, sin6); 813 nh = NULL; 814 } 815 } 816 if (nrt) /* Send last packet */ 817 ripflush(NULL, sin6); 818 return; 819 } 820 821 if ((flag & RRTF_SENDANYWAY) == 0 && 822 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK))) 823 return; 824 825 /* -N: no use */ 826 if (iff_find(ifcp, 'N') != NULL) 827 return; 828 829 /* -T: generate default route only */ 830 if (iff_find(ifcp, 'T') != NULL) { 831 struct netinfo6 rrt_info; 832 memset(&rrt_info, 0, sizeof(struct netinfo6)); 833 rrt_info.rip6_dest = in6addr_any; 834 rrt_info.rip6_plen = 0; 835 rrt_info.rip6_metric = 1; 836 rrt_info.rip6_metric += ifcp->ifc_metric; 837 rrt_info.rip6_tag = htons(routetag & 0xffff); 838 np = ripbuf->rip6_nets; 839 *np = rrt_info; 840 nrt = 1; 841 ripflush(ifcp, sin6); 842 return; 843 } 844 845 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) - 846 sizeof(struct udphdr) - 847 sizeof(struct rip6) + sizeof(struct netinfo6)) / 848 sizeof(struct netinfo6); 849 850 nrt = 0; np = ripbuf->rip6_nets; nh = NULL; 851 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 852 if (rrt->rrt_rflags & RRTF_NOADVERTISE) 853 continue; 854 855 /* Need to check filter here */ 856 if (out_filter(rrt, ifcp) == 0) 857 continue; 858 859 /* Check split horizon and other conditions */ 860 if (tobeadv(rrt, ifcp) == 0) 861 continue; 862 863 /* Only considers the routes with flag if specified */ 864 if ((flag & RRTF_CHANGED) && 865 (rrt->rrt_rflags & RRTF_CHANGED) == 0) 866 continue; 867 868 /* Check nexthop */ 869 if (rrt->rrt_index == ifcp->ifc_index && 870 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) && 871 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) { 872 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) { 873 if (nrt == maxrte - 2) 874 ripflush(ifcp, sin6); 875 np->rip6_dest = rrt->rrt_gw; 876 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) 877 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0); 878 np->rip6_plen = 0; 879 np->rip6_tag = 0; 880 np->rip6_metric = NEXTHOP_METRIC; 881 nh = &rrt->rrt_gw; 882 np++; nrt++; 883 } 884 } else if (nh && (rrt->rrt_index != ifcp->ifc_index || 885 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) || 886 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) { 887 /* Reset nexthop */ 888 if (nrt == maxrte - 2) 889 ripflush(ifcp, sin6); 890 memset(np, 0, sizeof(struct netinfo6)); 891 np->rip6_metric = NEXTHOP_METRIC; 892 nh = NULL; 893 np++; nrt++; 894 } 895 896 /* Put the route to the buffer */ 897 *np = rrt->rrt_info; 898 np++; nrt++; 899 if (nrt == maxrte) { 900 ripflush(ifcp, sin6); 901 nh = NULL; 902 } 903 } 904 if (nrt) /* Send last packet */ 905 ripflush(ifcp, sin6); 906 } 907 908 /* 909 * outbound filter logic, per-route/interface. 910 */ 911 int 912 out_filter(rrt, ifcp) 913 struct riprt *rrt; 914 struct ifc *ifcp; 915 { 916 struct iff *iffp; 917 struct in6_addr ia; 918 int ok; 919 920 /* 921 * -A: filter out less specific routes, if we have aggregated 922 * route configured. 923 */ 924 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 925 if (iffp->iff_type != 'A') 926 continue; 927 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen) 928 continue; 929 ia = rrt->rrt_info.rip6_dest; 930 applyplen(&ia, iffp->iff_plen); 931 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) 932 return 0; 933 } 934 935 /* 936 * if it is an aggregated route, advertise it only to the 937 * interfaces specified on -A. 938 */ 939 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) { 940 ok = 0; 941 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 942 if (iffp->iff_type != 'A') 943 continue; 944 if (rrt->rrt_info.rip6_plen == iffp->iff_plen && 945 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest, 946 &iffp->iff_addr)) { 947 ok = 1; 948 break; 949 } 950 } 951 if (!ok) 952 return 0; 953 } 954 955 /* 956 * -O: advertise only if prefix matches the configured prefix. 957 */ 958 if (iff_find(ifcp, 'O')) { 959 ok = 0; 960 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 961 if (iffp->iff_type != 'O') 962 continue; 963 if (rrt->rrt_info.rip6_plen < iffp->iff_plen) 964 continue; 965 ia = rrt->rrt_info.rip6_dest; 966 applyplen(&ia, iffp->iff_plen); 967 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) { 968 ok = 1; 969 break; 970 } 971 } 972 if (!ok) 973 return 0; 974 } 975 976 /* the prefix should be advertised */ 977 return 1; 978 } 979 980 /* 981 * Determine if the route is to be advertised on the specified interface. 982 * It checks options specified in the arguments and the split horizon rule. 983 */ 984 int 985 tobeadv(rrt, ifcp) 986 struct riprt *rrt; 987 struct ifc *ifcp; 988 { 989 990 /* Special care for static routes */ 991 if (rrt->rrt_flags & RTF_STATIC) { 992 /* XXX don't advertise reject/blackhole routes */ 993 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE)) 994 return 0; 995 996 if (Sflag) /* Yes, advertise it anyway */ 997 return 1; 998 if (sflag && rrt->rrt_index != ifcp->ifc_index) 999 return 1; 1000 return 0; 1001 } 1002 /* Regular split horizon */ 1003 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index) 1004 return 0; 1005 return 1; 1006 } 1007 1008 /* 1009 * Send a rip packet actually. 1010 */ 1011 int 1012 sendpacket(sin6, len) 1013 struct sockaddr_in6 *sin6; 1014 int len; 1015 { 1016 /* 1017 * MSG_DONTROUTE should not be specified when it responds with a 1018 * RIP6_REQUEST message. SO_DONTROUTE has been specified to 1019 * other sockets. 1020 */ 1021 struct msghdr m; 1022 struct cmsghdr *cm; 1023 struct iovec iov[2]; 1024 u_char cmsgbuf[256]; 1025 struct in6_pktinfo *pi; 1026 int idx; 1027 struct sockaddr_in6 sincopy; 1028 1029 /* do not overwrite the given sin */ 1030 sincopy = *sin6; 1031 sin6 = &sincopy; 1032 1033 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) || 1034 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1035 idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr); 1036 SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0); 1037 } else 1038 idx = 0; 1039 1040 m.msg_name = (caddr_t)sin6; 1041 m.msg_namelen = sizeof(*sin6); 1042 iov[0].iov_base = (caddr_t)ripbuf; 1043 iov[0].iov_len = len; 1044 m.msg_iov = iov; 1045 m.msg_iovlen = 1; 1046 if (!idx) { 1047 m.msg_control = NULL; 1048 m.msg_controllen = 0; 1049 } else { 1050 memset(cmsgbuf, 0, sizeof(cmsgbuf)); 1051 cm = (struct cmsghdr *)cmsgbuf; 1052 m.msg_control = (caddr_t)cm; 1053 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); 1054 1055 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 1056 cm->cmsg_level = IPPROTO_IPV6; 1057 cm->cmsg_type = IPV6_PKTINFO; 1058 pi = (struct in6_pktinfo *)CMSG_DATA(cm); 1059 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/ 1060 pi->ipi6_ifindex = idx; 1061 } 1062 1063 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) { 1064 trace(1, "sendmsg: %s\n", strerror(errno)); 1065 return errno; 1066 } 1067 1068 return 0; 1069 } 1070 1071 /* 1072 * Receive and process RIP packets. Update the routes/kernel forwarding 1073 * table if necessary. 1074 */ 1075 void 1076 riprecv() 1077 { 1078 struct ifc *ifcp, *ic; 1079 struct sockaddr_in6 fsock; 1080 struct in6_addr nh; /* next hop */ 1081 struct rip6 *rp; 1082 struct netinfo6 *np, *nq; 1083 struct riprt *rrt; 1084 int len, nn, need_trigger, idx; 1085 char buf[4 * RIP6_MAXMTU]; 1086 time_t t; 1087 struct msghdr m; 1088 struct cmsghdr *cm; 1089 struct iovec iov[2]; 1090 u_char cmsgbuf[256]; 1091 struct in6_pktinfo *pi; 1092 struct iff *iffp; 1093 struct in6_addr ia; 1094 int ok; 1095 time_t t_half_lifetime; 1096 1097 need_trigger = 0; 1098 1099 m.msg_name = (caddr_t)&fsock; 1100 m.msg_namelen = sizeof(fsock); 1101 iov[0].iov_base = (caddr_t)buf; 1102 iov[0].iov_len = sizeof(buf); 1103 m.msg_iov = iov; 1104 m.msg_iovlen = 1; 1105 cm = (struct cmsghdr *)cmsgbuf; 1106 m.msg_control = (caddr_t)cm; 1107 m.msg_controllen = sizeof(cmsgbuf); 1108 if ((len = recvmsg(ripsock, &m, 0)) < 0) { 1109 fatal("recvmsg"); 1110 /*NOTREACHED*/ 1111 } 1112 idx = 0; 1113 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m); 1114 cm; 1115 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) { 1116 if (cm->cmsg_level == IPPROTO_IPV6 && 1117 cm->cmsg_type == IPV6_PKTINFO) { 1118 pi = (struct in6_pktinfo *)(CMSG_DATA(cm)); 1119 idx = pi->ipi6_ifindex; 1120 break; 1121 } 1122 } 1123 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) 1124 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx); 1125 1126 nh = fsock.sin6_addr; 1127 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) / 1128 sizeof(struct netinfo6); 1129 rp = (struct rip6 *)buf; 1130 np = rp->rip6_nets; 1131 1132 if (rp->rip6_vers != RIP6_VERSION) { 1133 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers); 1134 return; 1135 } 1136 if (rp->rip6_cmd == RIP6_REQUEST) { 1137 if (idx && idx < nindex2ifc) { 1138 ifcp = index2ifc[idx]; 1139 riprequest(ifcp, np, nn, &fsock); 1140 } else { 1141 riprequest(NULL, np, nn, &fsock); 1142 } 1143 return; 1144 } 1145 1146 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) { 1147 trace(1, "Packets from non-ll addr: %s\n", 1148 inet6_n2p(&fsock.sin6_addr)); 1149 return; /* Ignore packets from non-link-local addr */ 1150 } 1151 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr); 1152 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL; 1153 if (!ifcp) { 1154 trace(1, "Packets to unknown interface index %d\n", idx); 1155 return; /* Ignore it */ 1156 } 1157 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr)) 1158 return; /* The packet is from me; ignore */ 1159 if (rp->rip6_cmd != RIP6_RESPONSE) { 1160 trace(1, "Invalid command %d\n", rp->rip6_cmd); 1161 return; 1162 } 1163 1164 /* -N: no use */ 1165 if (iff_find(ifcp, 'N') != NULL) 1166 return; 1167 1168 tracet(1, "Recv(%s): from %s.%d info(%d)\n", 1169 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn); 1170 1171 t = time(NULL); 1172 t_half_lifetime = t - (RIP_LIFETIME/2); 1173 for (; nn; nn--, np++) { 1174 if (np->rip6_metric == NEXTHOP_METRIC) { 1175 /* modify neighbor address */ 1176 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) { 1177 nh = np->rip6_dest; 1178 SET_IN6_LINKLOCAL_IFINDEX(nh, idx); 1179 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh)); 1180 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) { 1181 nh = fsock.sin6_addr; 1182 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh)); 1183 } else { 1184 nh = fsock.sin6_addr; 1185 trace(1, "\tInvalid Nexthop: %s\n", 1186 inet6_n2p(&np->rip6_dest)); 1187 } 1188 continue; 1189 } 1190 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) { 1191 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n", 1192 inet6_n2p(&np->rip6_dest), 1193 np->rip6_plen, np->rip6_metric); 1194 continue; 1195 } 1196 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) { 1197 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n", 1198 inet6_n2p(&np->rip6_dest), 1199 np->rip6_plen, np->rip6_metric); 1200 continue; 1201 } 1202 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) { 1203 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n", 1204 inet6_n2p(&np->rip6_dest), 1205 np->rip6_plen, np->rip6_metric); 1206 continue; 1207 } 1208 /* may need to pass sitelocal prefix in some case, however*/ 1209 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) { 1210 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n", 1211 inet6_n2p(&np->rip6_dest), 1212 np->rip6_plen, np->rip6_metric); 1213 continue; 1214 } 1215 trace(2, "\tnetinfo6: %s/%d [%d]", 1216 inet6_n2p(&np->rip6_dest), 1217 np->rip6_plen, np->rip6_metric); 1218 if (np->rip6_tag) 1219 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff); 1220 if (dflag >= 2) { 1221 ia = np->rip6_dest; 1222 applyplen(&ia, np->rip6_plen); 1223 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest)) 1224 trace(2, " [junk outside prefix]"); 1225 } 1226 1227 /* 1228 * -L: listen only if the prefix matches the configuration 1229 */ 1230 ok = 1; /* if there's no L filter, it is ok */ 1231 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 1232 if (iffp->iff_type != 'L') 1233 continue; 1234 ok = 0; 1235 if (np->rip6_plen < iffp->iff_plen) 1236 continue; 1237 /* special rule: ::/0 means default, not "in /0" */ 1238 if (iffp->iff_plen == 0 && np->rip6_plen > 0) 1239 continue; 1240 ia = np->rip6_dest; 1241 applyplen(&ia, iffp->iff_plen); 1242 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) { 1243 ok = 1; 1244 break; 1245 } 1246 } 1247 if (!ok) { 1248 trace(2, " (filtered)\n"); 1249 continue; 1250 } 1251 1252 trace(2, "\n"); 1253 np->rip6_metric++; 1254 np->rip6_metric += ifcp->ifc_metric; 1255 if (np->rip6_metric > HOPCNT_INFINITY6) 1256 np->rip6_metric = HOPCNT_INFINITY6; 1257 1258 applyplen(&np->rip6_dest, np->rip6_plen); 1259 if ((rrt = rtsearch(np, NULL)) != NULL) { 1260 if (rrt->rrt_t == 0) 1261 continue; /* Intf route has priority */ 1262 nq = &rrt->rrt_info; 1263 if (nq->rip6_metric > np->rip6_metric) { 1264 if (rrt->rrt_index == ifcp->ifc_index && 1265 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) { 1266 /* Small metric from the same gateway */ 1267 nq->rip6_metric = np->rip6_metric; 1268 } else { 1269 /* Better route found */ 1270 rrt->rrt_index = ifcp->ifc_index; 1271 /* Update routing table */ 1272 delroute(nq, &rrt->rrt_gw); 1273 rrt->rrt_gw = nh; 1274 *nq = *np; 1275 addroute(rrt, &nh, ifcp); 1276 } 1277 rrt->rrt_rflags |= RRTF_CHANGED; 1278 rrt->rrt_t = t; 1279 need_trigger = 1; 1280 } else if (nq->rip6_metric < np->rip6_metric && 1281 rrt->rrt_index == ifcp->ifc_index && 1282 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) { 1283 /* Got worse route from same gw */ 1284 nq->rip6_metric = np->rip6_metric; 1285 rrt->rrt_t = t; 1286 rrt->rrt_rflags |= RRTF_CHANGED; 1287 need_trigger = 1; 1288 } else if (nq->rip6_metric == np->rip6_metric && 1289 np->rip6_metric < HOPCNT_INFINITY6) { 1290 if (rrt->rrt_index == ifcp->ifc_index && 1291 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) { 1292 /* same metric, same route from same gw */ 1293 rrt->rrt_t = t; 1294 } else if (rrt->rrt_t < t_half_lifetime) { 1295 /* Better route found */ 1296 rrt->rrt_index = ifcp->ifc_index; 1297 /* Update routing table */ 1298 delroute(nq, &rrt->rrt_gw); 1299 rrt->rrt_gw = nh; 1300 *nq = *np; 1301 addroute(rrt, &nh, ifcp); 1302 rrt->rrt_rflags |= RRTF_CHANGED; 1303 rrt->rrt_t = t; 1304 } 1305 } 1306 /* 1307 * if nq->rip6_metric == HOPCNT_INFINITY6 then 1308 * do not update age value. Do nothing. 1309 */ 1310 } else if (np->rip6_metric < HOPCNT_INFINITY6) { 1311 /* Got a new valid route */ 1312 if ((rrt = MALLOC(struct riprt)) == NULL) { 1313 fatal("malloc: struct riprt"); 1314 /*NOTREACHED*/ 1315 } 1316 memset(rrt, 0, sizeof(*rrt)); 1317 nq = &rrt->rrt_info; 1318 1319 rrt->rrt_same = NULL; 1320 rrt->rrt_index = ifcp->ifc_index; 1321 rrt->rrt_flags = RTF_UP|RTF_GATEWAY; 1322 rrt->rrt_gw = nh; 1323 *nq = *np; 1324 applyplen(&nq->rip6_dest, nq->rip6_plen); 1325 if (nq->rip6_plen == sizeof(struct in6_addr) * 8) 1326 rrt->rrt_flags |= RTF_HOST; 1327 1328 /* Put the route to the list */ 1329 rrt->rrt_next = riprt; 1330 riprt = rrt; 1331 /* Update routing table */ 1332 addroute(rrt, &nh, ifcp); 1333 rrt->rrt_rflags |= RRTF_CHANGED; 1334 need_trigger = 1; 1335 rrt->rrt_t = t; 1336 } 1337 } 1338 /* XXX need to care the interval between triggered updates */ 1339 if (need_trigger) { 1340 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) { 1341 for (ic = ifc; ic; ic = ic->ifc_next) { 1342 if (ifcp->ifc_index == ic->ifc_index) 1343 continue; 1344 if (ic->ifc_flags & IFF_UP) 1345 ripsend(ic, &ic->ifc_ripsin, 1346 RRTF_CHANGED); 1347 } 1348 } 1349 /* Reset the flag */ 1350 for (rrt = riprt; rrt; rrt = rrt->rrt_next) 1351 rrt->rrt_rflags &= ~RRTF_CHANGED; 1352 } 1353 } 1354 1355 /* 1356 * Send all routes request packet to the specified interface. 1357 */ 1358 void 1359 sendrequest(ifcp) 1360 struct ifc *ifcp; 1361 { 1362 struct netinfo6 *np; 1363 int error; 1364 1365 if (ifcp->ifc_flags & IFF_LOOPBACK) 1366 return; 1367 ripbuf->rip6_cmd = RIP6_REQUEST; 1368 np = ripbuf->rip6_nets; 1369 memset(np, 0, sizeof(struct netinfo6)); 1370 np->rip6_metric = HOPCNT_INFINITY6; 1371 tracet(1, "Send rtdump Request to %s (%s)\n", 1372 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr)); 1373 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1)); 1374 if (error == EAFNOSUPPORT) { 1375 /* Protocol not supported */ 1376 tracet(1, "Could not send rtdump Request to %s (%s): " 1377 "set IFF_UP to 0\n", 1378 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr)); 1379 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */ 1380 } 1381 ripbuf->rip6_cmd = RIP6_RESPONSE; 1382 } 1383 1384 /* 1385 * Process a RIP6_REQUEST packet. 1386 */ 1387 void 1388 riprequest(ifcp, np, nn, sin6) 1389 struct ifc *ifcp; 1390 struct netinfo6 *np; 1391 int nn; 1392 struct sockaddr_in6 *sin6; 1393 { 1394 int i; 1395 struct riprt *rrt; 1396 1397 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) && 1398 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) { 1399 /* Specific response, don't split-horizon */ 1400 trace(1, "\tRIP Request\n"); 1401 for (i = 0; i < nn; i++, np++) { 1402 rrt = rtsearch(np, NULL); 1403 if (rrt) 1404 np->rip6_metric = rrt->rrt_info.rip6_metric; 1405 else 1406 np->rip6_metric = HOPCNT_INFINITY6; 1407 } 1408 (void)sendpacket(sin6, RIPSIZE(nn)); 1409 return; 1410 } 1411 /* Whole routing table dump */ 1412 trace(1, "\tRIP Request -- whole routing table\n"); 1413 ripsend(ifcp, sin6, RRTF_SENDANYWAY); 1414 } 1415 1416 /* 1417 * Get information of each interface. 1418 */ 1419 void 1420 ifconfig() 1421 { 1422 struct ifaddrs *ifap, *ifa; 1423 struct ifc *ifcp; 1424 struct ipv6_mreq mreq; 1425 int s; 1426 1427 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1428 fatal("socket"); 1429 /*NOTREACHED*/ 1430 } 1431 1432 if (getifaddrs(&ifap) != 0) { 1433 fatal("getifaddrs"); 1434 /*NOTREACHED*/ 1435 } 1436 1437 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1438 if (ifa->ifa_addr->sa_family != AF_INET6) 1439 continue; 1440 ifcp = ifc_find(ifa->ifa_name); 1441 /* we are interested in multicast-capable interfaces */ 1442 if ((ifa->ifa_flags & IFF_MULTICAST) == 0) 1443 continue; 1444 if (!ifcp) { 1445 /* new interface */ 1446 if ((ifcp = MALLOC(struct ifc)) == NULL) { 1447 fatal("malloc: struct ifc"); 1448 /*NOTREACHED*/ 1449 } 1450 memset(ifcp, 0, sizeof(*ifcp)); 1451 ifcp->ifc_index = -1; 1452 ifcp->ifc_next = ifc; 1453 ifc = ifcp; 1454 nifc++; 1455 ifcp->ifc_name = allocopy(ifa->ifa_name); 1456 ifcp->ifc_addr = 0; 1457 ifcp->ifc_filter = 0; 1458 ifcp->ifc_flags = ifa->ifa_flags; 1459 trace(1, "newif %s <%s>\n", ifcp->ifc_name, 1460 ifflags(ifcp->ifc_flags)); 1461 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF)) 1462 loopifcp = ifcp; 1463 } else { 1464 /* update flag, this may be up again */ 1465 if (ifcp->ifc_flags != ifa->ifa_flags) { 1466 trace(1, "%s: <%s> -> ", ifcp->ifc_name, 1467 ifflags(ifcp->ifc_flags)); 1468 trace(1, "<%s>\n", ifflags(ifa->ifa_flags)); 1469 ifcp->ifc_cflags |= IFC_CHANGED; 1470 } 1471 ifcp->ifc_flags = ifa->ifa_flags; 1472 } 1473 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s); 1474 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP 1475 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) { 1476 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr; 1477 mreq.ipv6mr_interface = ifcp->ifc_index; 1478 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP, 1479 &mreq, sizeof(mreq)) < 0) { 1480 fatal("IPV6_JOIN_GROUP"); 1481 /*NOTREACHED*/ 1482 } 1483 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST); 1484 ifcp->ifc_joined++; 1485 } 1486 } 1487 close(s); 1488 freeifaddrs(ifap); 1489 } 1490 1491 void 1492 ifconfig1(name, sa, ifcp, s) 1493 const char *name; 1494 const struct sockaddr *sa; 1495 struct ifc *ifcp; 1496 int s; 1497 { 1498 struct in6_ifreq ifr; 1499 const struct sockaddr_in6 *sin6; 1500 struct ifac *ifa; 1501 int plen; 1502 char buf[BUFSIZ]; 1503 1504 sin6 = (const struct sockaddr_in6 *)sa; 1505 ifr.ifr_addr = *sin6; 1506 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1507 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) { 1508 fatal("ioctl: SIOCGIFNETMASK_IN6"); 1509 /*NOTREACHED*/ 1510 } 1511 plen = sin6mask2len(&ifr.ifr_addr); 1512 if ((ifa = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) { 1513 /* same interface found */ 1514 /* need check if something changed */ 1515 /* XXX not yet implemented */ 1516 return; 1517 } 1518 /* 1519 * New address is found 1520 */ 1521 if ((ifa = MALLOC(struct ifac)) == NULL) { 1522 fatal("malloc: struct ifac"); 1523 /*NOTREACHED*/ 1524 } 1525 memset(ifa, 0, sizeof(*ifa)); 1526 ifa->ifa_conf = ifcp; 1527 ifa->ifa_next = ifcp->ifc_addr; 1528 ifcp->ifc_addr = ifa; 1529 ifa->ifa_addr = sin6->sin6_addr; 1530 ifa->ifa_plen = plen; 1531 if (ifcp->ifc_flags & IFF_POINTOPOINT) { 1532 ifr.ifr_addr = *sin6; 1533 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) { 1534 fatal("ioctl: SIOCGIFDSTADDR_IN6"); 1535 /*NOTREACHED*/ 1536 } 1537 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr; 1538 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf)); 1539 trace(1, "found address %s/%d -- %s\n", 1540 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf); 1541 } else { 1542 trace(1, "found address %s/%d\n", 1543 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen); 1544 } 1545 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) { 1546 ifcp->ifc_mylladdr = ifa->ifa_addr; 1547 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr); 1548 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len); 1549 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr, 1550 ifcp->ifc_index); 1551 setindex2ifc(ifcp->ifc_index, ifcp); 1552 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index); 1553 if (ifcp->ifc_mtu > RIP6_MAXMTU) 1554 ifcp->ifc_mtu = RIP6_MAXMTU; 1555 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) { 1556 fatal("ioctl: SIOCGIFMETRIC"); 1557 /*NOTREACHED*/ 1558 } 1559 ifcp->ifc_metric = ifr.ifr_metric; 1560 trace(1, "\tindex: %d, mtu: %d, metric: %d\n", 1561 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric); 1562 } else 1563 ifcp->ifc_cflags |= IFC_CHANGED; 1564 } 1565 1566 /* 1567 * Receive and process routing messages. 1568 * Update interface information as necesssary. 1569 */ 1570 void 1571 rtrecv() 1572 { 1573 char buf[BUFSIZ]; 1574 char *p, *q; 1575 struct rt_msghdr *rtm; 1576 struct ifa_msghdr *ifam; 1577 struct if_msghdr *ifm; 1578 int len; 1579 struct ifc *ifcp, *ic; 1580 int iface = 0, rtable = 0; 1581 struct sockaddr_in6 *rta[RTAX_MAX]; 1582 struct sockaddr_in6 mask; 1583 int i, addrs; 1584 struct riprt *rrt; 1585 1586 if ((len = read(rtsock, buf, sizeof(buf))) < 0) { 1587 perror("read from rtsock"); 1588 exit(1); 1589 } 1590 if (len < sizeof(*rtm)) { 1591 trace(1, "short read from rtsock: %d (should be > %lu)\n", 1592 len, (u_long)sizeof(*rtm)); 1593 return; 1594 } 1595 if (dflag >= 2) { 1596 fprintf(stderr, "rtmsg:\n"); 1597 for (i = 0; i < len; i++) { 1598 fprintf(stderr, "%02x ", buf[i] & 0xff); 1599 if (i % 16 == 15) fprintf(stderr, "\n"); 1600 } 1601 fprintf(stderr, "\n"); 1602 } 1603 1604 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) { 1605 /* safety against bogus message */ 1606 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) { 1607 trace(1, "bogus rtmsg: length=%d\n", 1608 ((struct rt_msghdr *)p)->rtm_msglen); 1609 break; 1610 } 1611 rtm = NULL; 1612 ifam = NULL; 1613 ifm = NULL; 1614 switch (((struct rt_msghdr *)p)->rtm_type) { 1615 case RTM_NEWADDR: 1616 case RTM_DELADDR: 1617 ifam = (struct ifa_msghdr *)p; 1618 addrs = ifam->ifam_addrs; 1619 q = (char *)(ifam + 1); 1620 break; 1621 case RTM_IFINFO: 1622 ifm = (struct if_msghdr *)p; 1623 addrs = ifm->ifm_addrs; 1624 q = (char *)(ifm + 1); 1625 break; 1626 default: 1627 rtm = (struct rt_msghdr *)p; 1628 addrs = rtm->rtm_addrs; 1629 q = (char *)(rtm + 1); 1630 if (rtm->rtm_version != RTM_VERSION) { 1631 trace(1, "unexpected rtmsg version %d " 1632 "(should be %d)\n", 1633 rtm->rtm_version, RTM_VERSION); 1634 continue; 1635 } 1636 if (rtm->rtm_pid == pid) { 1637 #if 0 1638 trace(1, "rtmsg looped back to me, ignored\n"); 1639 #endif 1640 continue; 1641 } 1642 break; 1643 } 1644 memset(&rta, 0, sizeof(rta)); 1645 for (i = 0; i < RTAX_MAX; i++) { 1646 if (addrs & (1 << i)) { 1647 rta[i] = (struct sockaddr_in6 *)q; 1648 q += ROUNDUP(rta[i]->sin6_len); 1649 } 1650 } 1651 1652 trace(1, "rtsock: %s (addrs=%x)\n", 1653 rttypes((struct rt_msghdr *)p), addrs); 1654 if (dflag >= 2) { 1655 for (i = 0; 1656 i < ((struct rt_msghdr *)p)->rtm_msglen; 1657 i++) { 1658 fprintf(stderr, "%02x ", p[i] & 0xff); 1659 if (i % 16 == 15) fprintf(stderr, "\n"); 1660 } 1661 fprintf(stderr, "\n"); 1662 } 1663 1664 /* 1665 * Easy ones first. 1666 * 1667 * We may be able to optimize by using ifm->ifm_index or 1668 * ifam->ifam_index. For simplicity we don't do that here. 1669 */ 1670 switch (((struct rt_msghdr *)p)->rtm_type) { 1671 case RTM_NEWADDR: 1672 case RTM_IFINFO: 1673 iface++; 1674 continue; 1675 case RTM_ADD: 1676 rtable++; 1677 continue; 1678 case RTM_LOSING: 1679 case RTM_MISS: 1680 case RTM_RESOLVE: 1681 case RTM_GET: 1682 case RTM_LOCK: 1683 /* nothing to be done here */ 1684 trace(1, "\tnothing to be done, ignored\n"); 1685 continue; 1686 } 1687 1688 #if 0 1689 if (rta[RTAX_DST] == NULL) { 1690 trace(1, "\tno destination, ignored\n"); 1691 continue; 1692 } 1693 if (rta[RTAX_DST]->sin6_family != AF_INET6) { 1694 trace(1, "\taf mismatch, ignored\n"); 1695 continue; 1696 } 1697 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) { 1698 trace(1, "\tlinklocal destination, ignored\n"); 1699 continue; 1700 } 1701 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) { 1702 trace(1, "\tloopback destination, ignored\n"); 1703 continue; /* Loopback */ 1704 } 1705 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) { 1706 trace(1, "\tmulticast destination, ignored\n"); 1707 continue; 1708 } 1709 #endif 1710 1711 /* hard ones */ 1712 switch (((struct rt_msghdr *)p)->rtm_type) { 1713 case RTM_NEWADDR: 1714 case RTM_IFINFO: 1715 case RTM_ADD: 1716 case RTM_LOSING: 1717 case RTM_MISS: 1718 case RTM_RESOLVE: 1719 case RTM_GET: 1720 case RTM_LOCK: 1721 /* should already be handled */ 1722 fatal("rtrecv: never reach here"); 1723 /*NOTREACHED*/ 1724 case RTM_DELETE: 1725 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) { 1726 trace(1, "\tsome of dst/gw/netamsk are " 1727 "unavailable, ignored\n"); 1728 break; 1729 } 1730 if ((rtm->rtm_flags & RTF_HOST) != 0) { 1731 mask.sin6_len = sizeof(mask); 1732 memset(&mask.sin6_addr, 0xff, 1733 sizeof(mask.sin6_addr)); 1734 rta[RTAX_NETMASK] = &mask; 1735 } else if (!rta[RTAX_NETMASK]) { 1736 trace(1, "\tsome of dst/gw/netamsk are " 1737 "unavailable, ignored\n"); 1738 break; 1739 } 1740 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY], 1741 rta[RTAX_NETMASK]) == 0) { 1742 rtable++; /*just to be sure*/ 1743 } 1744 break; 1745 case RTM_CHANGE: 1746 case RTM_REDIRECT: 1747 trace(1, "\tnot supported yet, ignored\n"); 1748 break; 1749 case RTM_DELADDR: 1750 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) { 1751 trace(1, "\tno netmask or ifa given, ignored\n"); 1752 break; 1753 } 1754 if (ifam->ifam_index < nindex2ifc) 1755 ifcp = index2ifc[ifam->ifam_index]; 1756 else 1757 ifcp = NULL; 1758 if (!ifcp) { 1759 trace(1, "\tinvalid ifam_index %d, ignored\n", 1760 ifam->ifam_index); 1761 break; 1762 } 1763 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK])) 1764 iface++; 1765 break; 1766 case RTM_OLDADD: 1767 case RTM_OLDDEL: 1768 trace(1, "\tnot supported yet, ignored\n"); 1769 break; 1770 } 1771 1772 } 1773 1774 if (iface) { 1775 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n"); 1776 ifconfig(); 1777 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) 1778 if (ifcp->ifc_cflags & IFC_CHANGED) { 1779 if (ifrt(ifcp, 1)) { 1780 for (ic = ifc; ic; ic = ic->ifc_next) { 1781 if (ifcp->ifc_index == ic->ifc_index) 1782 continue; 1783 if (ic->ifc_flags & IFF_UP) 1784 ripsend(ic, &ic->ifc_ripsin, 1785 RRTF_CHANGED); 1786 } 1787 /* Reset the flag */ 1788 for (rrt = riprt; rrt; rrt = rrt->rrt_next) 1789 rrt->rrt_rflags &= ~RRTF_CHANGED; 1790 } 1791 ifcp->ifc_cflags &= ~IFC_CHANGED; 1792 } 1793 } 1794 if (rtable) { 1795 trace(1, "rtsock: read routing table again\n"); 1796 krtread(1); 1797 } 1798 } 1799 1800 /* 1801 * remove specified route from the internal routing table. 1802 */ 1803 int 1804 rt_del(sdst, sgw, smask) 1805 const struct sockaddr_in6 *sdst; 1806 const struct sockaddr_in6 *sgw; 1807 const struct sockaddr_in6 *smask; 1808 { 1809 const struct in6_addr *dst = NULL; 1810 const struct in6_addr *gw = NULL; 1811 int prefix; 1812 struct netinfo6 ni6; 1813 struct riprt *rrt = NULL; 1814 time_t t_lifetime; 1815 1816 if (sdst->sin6_family != AF_INET6) { 1817 trace(1, "\tother AF, ignored\n"); 1818 return -1; 1819 } 1820 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr) 1821 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback) 1822 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) { 1823 trace(1, "\taddress %s not interesting, ignored\n", 1824 inet6_n2p(&sdst->sin6_addr)); 1825 return -1; 1826 } 1827 dst = &sdst->sin6_addr; 1828 if (sgw->sin6_family == AF_INET6) { 1829 /* easy case */ 1830 gw = &sgw->sin6_addr; 1831 prefix = sin6mask2len(smask); 1832 } else if (sgw->sin6_family == AF_LINK) { 1833 /* 1834 * Interface route... a hard case. We need to get the prefix 1835 * length from the kernel, but we now are parsing rtmsg. 1836 * We'll purge matching routes from my list, then get the 1837 * fresh list. 1838 */ 1839 struct riprt *longest; 1840 trace(1, "\t%s is an interface route, guessing prefixlen\n", 1841 inet6_n2p(dst)); 1842 longest = NULL; 1843 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 1844 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest, 1845 &sdst->sin6_addr) 1846 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) { 1847 if (!longest 1848 || longest->rrt_info.rip6_plen < 1849 rrt->rrt_info.rip6_plen) { 1850 longest = rrt; 1851 } 1852 } 1853 } 1854 rrt = longest; 1855 if (!rrt) { 1856 trace(1, "\tno matching interface route found\n"); 1857 return -1; 1858 } 1859 gw = &in6addr_loopback; 1860 prefix = rrt->rrt_info.rip6_plen; 1861 } else { 1862 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family); 1863 return -1; 1864 } 1865 1866 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix); 1867 trace(1, "gw %s\n", inet6_n2p(gw)); 1868 t_lifetime = time(NULL) - RIP_LIFETIME; 1869 /* age route for interface address */ 1870 memset(&ni6, 0, sizeof(ni6)); 1871 ni6.rip6_dest = *dst; 1872 ni6.rip6_plen = prefix; 1873 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/ 1874 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest), 1875 ni6.rip6_plen); 1876 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) { 1877 trace(1, "\tno route found\n"); 1878 return -1; 1879 } 1880 #if 0 1881 if ((rrt->rrt_flags & RTF_STATIC) == 0) { 1882 trace(1, "\tyou can delete static routes only\n"); 1883 } else 1884 #endif 1885 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) { 1886 trace(1, "\tgw mismatch: %s <-> ", 1887 inet6_n2p(&rrt->rrt_gw)); 1888 trace(1, "%s\n", inet6_n2p(gw)); 1889 } else { 1890 trace(1, "\troute found, age it\n"); 1891 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) { 1892 rrt->rrt_t = t_lifetime; 1893 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6; 1894 } 1895 } 1896 return 0; 1897 } 1898 1899 /* 1900 * remove specified address from internal interface/routing table. 1901 */ 1902 int 1903 rt_deladdr(ifcp, sifa, smask) 1904 struct ifc *ifcp; 1905 const struct sockaddr_in6 *sifa; 1906 const struct sockaddr_in6 *smask; 1907 { 1908 const struct in6_addr *addr = NULL; 1909 int prefix; 1910 struct ifac *ifa = NULL; 1911 struct netinfo6 ni6; 1912 struct riprt *rrt = NULL; 1913 time_t t_lifetime; 1914 int updated = 0; 1915 1916 if (sifa->sin6_family != AF_INET6) { 1917 trace(1, "\tother AF, ignored\n"); 1918 return -1; 1919 } 1920 addr = &sifa->sin6_addr; 1921 prefix = sin6mask2len(smask); 1922 1923 trace(1, "\tdeleting %s/%d from %s\n", 1924 inet6_n2p(addr), prefix, ifcp->ifc_name); 1925 ifa = ifa_match(ifcp, addr, prefix); 1926 if (!ifa) { 1927 trace(1, "\tno matching ifa found for %s/%d on %s\n", 1928 inet6_n2p(addr), prefix, ifcp->ifc_name); 1929 return -1; 1930 } 1931 if (ifa->ifa_conf != ifcp) { 1932 trace(1, "\taddress table corrupt: back pointer does not match " 1933 "(%s != %s)\n", 1934 ifcp->ifc_name, ifa->ifa_conf->ifc_name); 1935 return -1; 1936 } 1937 /* remove ifa from interface */ 1938 if (ifcp->ifc_addr == ifa) 1939 ifcp->ifc_addr = ifa->ifa_next; 1940 else { 1941 struct ifac *p; 1942 for (p = ifcp->ifc_addr; p; p = p->ifa_next) { 1943 if (p->ifa_next == ifa) { 1944 p->ifa_next = ifa->ifa_next; 1945 break; 1946 } 1947 } 1948 } 1949 ifa->ifa_next = NULL; 1950 ifa->ifa_conf = NULL; 1951 t_lifetime = time(NULL) - RIP_LIFETIME; 1952 /* age route for interface address */ 1953 memset(&ni6, 0, sizeof(ni6)); 1954 ni6.rip6_dest = ifa->ifa_addr; 1955 ni6.rip6_plen = ifa->ifa_plen; 1956 applyplen(&ni6.rip6_dest, ni6.rip6_plen); 1957 trace(1, "\tfind interface route %s/%d on %d\n", 1958 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index); 1959 if ((rrt = rtsearch(&ni6, NULL)) != NULL) { 1960 struct in6_addr none; 1961 memset(&none, 0, sizeof(none)); 1962 if (rrt->rrt_index == ifcp->ifc_index && 1963 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) || 1964 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) { 1965 trace(1, "\troute found, age it\n"); 1966 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) { 1967 rrt->rrt_t = t_lifetime; 1968 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6; 1969 } 1970 updated++; 1971 } else { 1972 trace(1, "\tnon-interface route found: %s/%d on %d\n", 1973 inet6_n2p(&rrt->rrt_info.rip6_dest), 1974 rrt->rrt_info.rip6_plen, 1975 rrt->rrt_index); 1976 } 1977 } else 1978 trace(1, "\tno interface route found\n"); 1979 /* age route for p2p destination */ 1980 if (ifcp->ifc_flags & IFF_POINTOPOINT) { 1981 memset(&ni6, 0, sizeof(ni6)); 1982 ni6.rip6_dest = ifa->ifa_raddr; 1983 ni6.rip6_plen = 128; 1984 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/ 1985 trace(1, "\tfind p2p route %s/%d on %d\n", 1986 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, 1987 ifcp->ifc_index); 1988 if ((rrt = rtsearch(&ni6, NULL)) != NULL) { 1989 if (rrt->rrt_index == ifcp->ifc_index && 1990 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) { 1991 trace(1, "\troute found, age it\n"); 1992 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) { 1993 rrt->rrt_t = t_lifetime; 1994 rrt->rrt_info.rip6_metric = 1995 HOPCNT_INFINITY6; 1996 updated++; 1997 } 1998 } else { 1999 trace(1, "\tnon-p2p route found: %s/%d on %d\n", 2000 inet6_n2p(&rrt->rrt_info.rip6_dest), 2001 rrt->rrt_info.rip6_plen, 2002 rrt->rrt_index); 2003 } 2004 } else 2005 trace(1, "\tno p2p route found\n"); 2006 } 2007 return updated ? 0 : -1; 2008 } 2009 2010 /* 2011 * Get each interface address and put those interface routes to the route 2012 * list. 2013 */ 2014 int 2015 ifrt(ifcp, again) 2016 struct ifc *ifcp; 2017 int again; 2018 { 2019 struct ifac *ifa; 2020 struct riprt *rrt = NULL, *search_rrt, *prev_rrt, *loop_rrt; 2021 struct netinfo6 *np; 2022 time_t t_lifetime; 2023 int need_trigger = 0; 2024 2025 if (ifcp->ifc_flags & IFF_LOOPBACK) 2026 return 0; /* ignore loopback */ 2027 if (ifcp->ifc_flags & IFF_POINTOPOINT) { 2028 ifrt_p2p(ifcp, again); 2029 return 0; 2030 } 2031 2032 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) { 2033 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) { 2034 #if 0 2035 trace(1, "route: %s on %s: " 2036 "skip linklocal interface address\n", 2037 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name); 2038 #endif 2039 continue; 2040 } 2041 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) { 2042 #if 0 2043 trace(1, "route: %s: skip unspec interface address\n", 2044 ifcp->ifc_name); 2045 #endif 2046 continue; 2047 } 2048 if (ifcp->ifc_flags & IFF_UP) { 2049 if ((rrt = MALLOC(struct riprt)) == NULL) 2050 fatal("malloc: struct riprt"); 2051 memset(rrt, 0, sizeof(*rrt)); 2052 rrt->rrt_same = NULL; 2053 rrt->rrt_index = ifcp->ifc_index; 2054 rrt->rrt_t = 0; /* don't age */ 2055 rrt->rrt_info.rip6_dest = ifa->ifa_addr; 2056 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff); 2057 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric; 2058 rrt->rrt_info.rip6_plen = ifa->ifa_plen; 2059 rrt->rrt_flags = RTF_CLONING; 2060 rrt->rrt_rflags |= RRTF_CHANGED; 2061 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen); 2062 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr)); 2063 rrt->rrt_gw = ifa->ifa_addr; 2064 np = &rrt->rrt_info; 2065 search_rrt = rtsearch(np, &prev_rrt); 2066 if (search_rrt != NULL) { 2067 if (search_rrt->rrt_info.rip6_metric <= 2068 rrt->rrt_info.rip6_metric) { 2069 /* Already have better route */ 2070 if (!again) { 2071 trace(1, "route: %s/%d: " 2072 "already registered (%s)\n", 2073 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2074 ifcp->ifc_name); 2075 } 2076 goto next; 2077 } 2078 2079 if (prev_rrt) 2080 prev_rrt->rrt_next = rrt->rrt_next; 2081 else 2082 riprt = rrt->rrt_next; 2083 delroute(&rrt->rrt_info, &rrt->rrt_gw); 2084 } 2085 /* Attach the route to the list */ 2086 trace(1, "route: %s/%d: register route (%s)\n", 2087 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2088 ifcp->ifc_name); 2089 rrt->rrt_next = riprt; 2090 riprt = rrt; 2091 addroute(rrt, &rrt->rrt_gw, ifcp); 2092 rrt = NULL; 2093 sendrequest(ifcp); 2094 ripsend(ifcp, &ifcp->ifc_ripsin, 0); 2095 need_trigger = 1; 2096 } else { 2097 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) { 2098 if (loop_rrt->rrt_index == ifcp->ifc_index) { 2099 t_lifetime = time(NULL) - RIP_LIFETIME; 2100 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) { 2101 loop_rrt->rrt_t = t_lifetime; 2102 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6; 2103 loop_rrt->rrt_rflags |= RRTF_CHANGED; 2104 need_trigger = 1; 2105 } 2106 } 2107 } 2108 } 2109 next: 2110 if (rrt) 2111 free(rrt); 2112 } 2113 return need_trigger; 2114 } 2115 2116 /* 2117 * there are couple of p2p interface routing models. "behavior" lets 2118 * you pick one. it looks that gated behavior fits best with BSDs, 2119 * since BSD kernels do not look at prefix length on p2p interfaces. 2120 */ 2121 void 2122 ifrt_p2p(ifcp, again) 2123 struct ifc *ifcp; 2124 int again; 2125 { 2126 struct ifac *ifa; 2127 struct riprt *rrt, *orrt, *prevrrt; 2128 struct netinfo6 *np; 2129 struct in6_addr addr, dest; 2130 int advert, ignore, i; 2131 #define P2PADVERT_NETWORK 1 2132 #define P2PADVERT_ADDR 2 2133 #define P2PADVERT_DEST 4 2134 #define P2PADVERT_MAX 4 2135 const enum { CISCO, GATED, ROUTE6D } behavior = GATED; 2136 const char *category = ""; 2137 const char *noadv; 2138 2139 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) { 2140 addr = ifa->ifa_addr; 2141 dest = ifa->ifa_raddr; 2142 applyplen(&addr, ifa->ifa_plen); 2143 applyplen(&dest, ifa->ifa_plen); 2144 advert = ignore = 0; 2145 switch (behavior) { 2146 case CISCO: 2147 /* 2148 * honor addr/plen, just like normal shared medium 2149 * interface. this may cause trouble if you reuse 2150 * addr/plen on other interfaces. 2151 * 2152 * advertise addr/plen. 2153 */ 2154 advert |= P2PADVERT_NETWORK; 2155 break; 2156 case GATED: 2157 /* 2158 * prefixlen on p2p interface is meaningless. 2159 * advertise addr/128 and dest/128. 2160 * 2161 * do not install network route to route6d routing 2162 * table (if we do, it would prevent route installation 2163 * for other p2p interface that shares addr/plen). 2164 * 2165 * XXX what should we do if dest is ::? it will not 2166 * get announced anyways (see following filter), 2167 * but we need to think. 2168 */ 2169 advert |= P2PADVERT_ADDR; 2170 advert |= P2PADVERT_DEST; 2171 ignore |= P2PADVERT_NETWORK; 2172 break; 2173 case ROUTE6D: 2174 /* 2175 * just for testing. actually the code is redundant 2176 * given the current p2p interface address assignment 2177 * rule for kame kernel. 2178 * 2179 * intent: 2180 * A/n -> announce A/n 2181 * A B/n, A and B share prefix -> A/n (= B/n) 2182 * A B/n, do not share prefix -> A/128 and B/128 2183 * actually, A/64 and A B/128 are the only cases 2184 * permitted by the kernel: 2185 * A/64 -> A/64 2186 * A B/128 -> A/128 and B/128 2187 */ 2188 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) { 2189 if (IN6_ARE_ADDR_EQUAL(&addr, &dest)) 2190 advert |= P2PADVERT_NETWORK; 2191 else { 2192 advert |= P2PADVERT_ADDR; 2193 advert |= P2PADVERT_DEST; 2194 ignore |= P2PADVERT_NETWORK; 2195 } 2196 } else 2197 advert |= P2PADVERT_NETWORK; 2198 break; 2199 } 2200 2201 for (i = 1; i <= P2PADVERT_MAX; i *= 2) { 2202 if ((ignore & i) != 0) 2203 continue; 2204 if ((rrt = MALLOC(struct riprt)) == NULL) { 2205 fatal("malloc: struct riprt"); 2206 /*NOTREACHED*/ 2207 } 2208 memset(rrt, 0, sizeof(*rrt)); 2209 rrt->rrt_same = NULL; 2210 rrt->rrt_index = ifcp->ifc_index; 2211 rrt->rrt_t = 0; /* don't age */ 2212 switch (i) { 2213 case P2PADVERT_NETWORK: 2214 rrt->rrt_info.rip6_dest = ifa->ifa_addr; 2215 rrt->rrt_info.rip6_plen = ifa->ifa_plen; 2216 applyplen(&rrt->rrt_info.rip6_dest, 2217 ifa->ifa_plen); 2218 category = "network"; 2219 break; 2220 case P2PADVERT_ADDR: 2221 rrt->rrt_info.rip6_dest = ifa->ifa_addr; 2222 rrt->rrt_info.rip6_plen = 128; 2223 rrt->rrt_gw = in6addr_loopback; 2224 category = "addr"; 2225 break; 2226 case P2PADVERT_DEST: 2227 rrt->rrt_info.rip6_dest = ifa->ifa_raddr; 2228 rrt->rrt_info.rip6_plen = 128; 2229 rrt->rrt_gw = ifa->ifa_addr; 2230 category = "dest"; 2231 break; 2232 } 2233 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) || 2234 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) { 2235 #if 0 2236 trace(1, "route: %s: skip unspec/linklocal " 2237 "(%s on %s)\n", category, ifcp->ifc_name); 2238 #endif 2239 free(rrt); 2240 continue; 2241 } 2242 if ((advert & i) == 0) { 2243 rrt->rrt_rflags |= RRTF_NOADVERTISE; 2244 noadv = ", NO-ADV"; 2245 } else 2246 noadv = ""; 2247 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff); 2248 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric; 2249 np = &rrt->rrt_info; 2250 orrt = rtsearch(np, &prevrrt); 2251 if (!orrt) { 2252 /* Attach the route to the list */ 2253 trace(1, "route: %s/%d: register route " 2254 "(%s on %s%s)\n", 2255 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2256 category, ifcp->ifc_name, noadv); 2257 rrt->rrt_next = riprt; 2258 riprt = rrt; 2259 } else if (rrt->rrt_index != orrt->rrt_index || 2260 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) { 2261 /* swap route */ 2262 rrt->rrt_next = orrt->rrt_next; 2263 if (prevrrt) 2264 prevrrt->rrt_next = rrt; 2265 else 2266 riprt = rrt; 2267 free(orrt); 2268 2269 trace(1, "route: %s/%d: update (%s on %s%s)\n", 2270 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2271 category, ifcp->ifc_name, noadv); 2272 } else { 2273 /* Already found */ 2274 if (!again) { 2275 trace(1, "route: %s/%d: " 2276 "already registered (%s on %s%s)\n", 2277 inet6_n2p(&np->rip6_dest), 2278 np->rip6_plen, category, 2279 ifcp->ifc_name, noadv); 2280 } 2281 free(rrt); 2282 } 2283 } 2284 } 2285 #undef P2PADVERT_NETWORK 2286 #undef P2PADVERT_ADDR 2287 #undef P2PADVERT_DEST 2288 #undef P2PADVERT_MAX 2289 } 2290 2291 int 2292 getifmtu(ifindex) 2293 int ifindex; 2294 { 2295 int mib[6]; 2296 char *buf; 2297 size_t msize; 2298 struct if_msghdr *ifm; 2299 int mtu; 2300 2301 mib[0] = CTL_NET; 2302 mib[1] = PF_ROUTE; 2303 mib[2] = 0; 2304 mib[3] = AF_INET6; 2305 mib[4] = NET_RT_IFLIST; 2306 mib[5] = ifindex; 2307 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) { 2308 fatal("sysctl estimate NET_RT_IFLIST"); 2309 /*NOTREACHED*/ 2310 } 2311 if ((buf = malloc(msize)) == NULL) { 2312 fatal("malloc"); 2313 /*NOTREACHED*/ 2314 } 2315 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) { 2316 fatal("sysctl NET_RT_IFLIST"); 2317 /*NOTREACHED*/ 2318 } 2319 ifm = (struct if_msghdr *)buf; 2320 mtu = ifm->ifm_data.ifi_mtu; 2321 #ifdef __FreeBSD__ 2322 if (ifindex != ifm->ifm_index) { 2323 fatal("ifindex does not match with ifm_index"); 2324 /*NOTREACHED*/ 2325 } 2326 #endif 2327 free(buf); 2328 return mtu; 2329 } 2330 2331 const char * 2332 rttypes(rtm) 2333 struct rt_msghdr *rtm; 2334 { 2335 #define RTTYPE(s, f) \ 2336 do { \ 2337 if (rtm->rtm_type == (f)) \ 2338 return (s); \ 2339 } while (0) 2340 RTTYPE("ADD", RTM_ADD); 2341 RTTYPE("DELETE", RTM_DELETE); 2342 RTTYPE("CHANGE", RTM_CHANGE); 2343 RTTYPE("GET", RTM_GET); 2344 RTTYPE("LOSING", RTM_LOSING); 2345 RTTYPE("REDIRECT", RTM_REDIRECT); 2346 RTTYPE("MISS", RTM_MISS); 2347 RTTYPE("LOCK", RTM_LOCK); 2348 RTTYPE("OLDADD", RTM_OLDADD); 2349 RTTYPE("OLDDEL", RTM_OLDDEL); 2350 RTTYPE("RESOLVE", RTM_RESOLVE); 2351 RTTYPE("NEWADDR", RTM_NEWADDR); 2352 RTTYPE("DELADDR", RTM_DELADDR); 2353 RTTYPE("IFINFO", RTM_IFINFO); 2354 #ifdef RTM_OLDADD 2355 RTTYPE("OLDADD", RTM_OLDADD); 2356 #endif 2357 #ifdef RTM_OLDDEL 2358 RTTYPE("OLDDEL", RTM_OLDDEL); 2359 #endif 2360 #ifdef RTM_OIFINFO 2361 RTTYPE("OIFINFO", RTM_OIFINFO); 2362 #endif 2363 #ifdef RTM_IFANNOUNCE 2364 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE); 2365 #endif 2366 #ifdef RTM_NEWMADDR 2367 RTTYPE("NEWMADDR", RTM_NEWMADDR); 2368 #endif 2369 #ifdef RTM_DELMADDR 2370 RTTYPE("DELMADDR", RTM_DELMADDR); 2371 #endif 2372 #undef RTTYPE 2373 return NULL; 2374 } 2375 2376 const char * 2377 rtflags(rtm) 2378 struct rt_msghdr *rtm; 2379 { 2380 static char buf[BUFSIZ]; 2381 2382 /* 2383 * letter conflict should be okay. painful when *BSD diverges... 2384 */ 2385 strlcpy(buf, "", sizeof(buf)); 2386 #define RTFLAG(s, f) \ 2387 do { \ 2388 if (rtm->rtm_flags & (f)) \ 2389 strlcat(buf, (s), sizeof(buf)); \ 2390 } while (0) 2391 RTFLAG("U", RTF_UP); 2392 RTFLAG("G", RTF_GATEWAY); 2393 RTFLAG("H", RTF_HOST); 2394 RTFLAG("R", RTF_REJECT); 2395 RTFLAG("D", RTF_DYNAMIC); 2396 RTFLAG("M", RTF_MODIFIED); 2397 RTFLAG("d", RTF_DONE); 2398 #ifdef RTF_MASK 2399 RTFLAG("m", RTF_MASK); 2400 #endif 2401 RTFLAG("C", RTF_CLONING); 2402 #ifdef RTF_CLONED 2403 RTFLAG("c", RTF_CLONED); 2404 #endif 2405 #ifdef RTF_PRCLONING 2406 RTFLAG("c", RTF_PRCLONING); 2407 #endif 2408 #ifdef RTF_WASCLONED 2409 RTFLAG("W", RTF_WASCLONED); 2410 #endif 2411 RTFLAG("X", RTF_XRESOLVE); 2412 RTFLAG("L", RTF_LLINFO); 2413 RTFLAG("S", RTF_STATIC); 2414 RTFLAG("B", RTF_BLACKHOLE); 2415 #ifdef RTF_PROTO3 2416 RTFLAG("3", RTF_PROTO3); 2417 #endif 2418 RTFLAG("2", RTF_PROTO2); 2419 RTFLAG("1", RTF_PROTO1); 2420 #ifdef RTF_BROADCAST 2421 RTFLAG("b", RTF_BROADCAST); 2422 #endif 2423 #ifdef RTF_DEFAULT 2424 RTFLAG("d", RTF_DEFAULT); 2425 #endif 2426 #ifdef RTF_ISAROUTER 2427 RTFLAG("r", RTF_ISAROUTER); 2428 #endif 2429 #ifdef RTF_TUNNEL 2430 RTFLAG("T", RTF_TUNNEL); 2431 #endif 2432 #ifdef RTF_AUTH 2433 RTFLAG("A", RTF_AUTH); 2434 #endif 2435 #ifdef RTF_CRYPT 2436 RTFLAG("E", RTF_CRYPT); 2437 #endif 2438 #undef RTFLAG 2439 return buf; 2440 } 2441 2442 const char * 2443 ifflags(flags) 2444 int flags; 2445 { 2446 static char buf[BUFSIZ]; 2447 2448 strlcpy(buf, "", sizeof(buf)); 2449 #define IFFLAG(s, f) \ 2450 do { \ 2451 if (flags & (f)) { \ 2452 if (buf[0]) \ 2453 strlcat(buf, ",", sizeof(buf)); \ 2454 strlcat(buf, (s), sizeof(buf)); \ 2455 } \ 2456 } while (0) 2457 IFFLAG("UP", IFF_UP); 2458 IFFLAG("BROADCAST", IFF_BROADCAST); 2459 IFFLAG("DEBUG", IFF_DEBUG); 2460 IFFLAG("LOOPBACK", IFF_LOOPBACK); 2461 IFFLAG("POINTOPOINT", IFF_POINTOPOINT); 2462 #ifdef IFF_NOTRAILERS 2463 IFFLAG("NOTRAILERS", IFF_NOTRAILERS); 2464 #endif 2465 #ifdef IFF_SMART 2466 IFFLAG("SMART", IFF_SMART); 2467 #endif 2468 IFFLAG("RUNNING", IFF_RUNNING); 2469 IFFLAG("NOARP", IFF_NOARP); 2470 IFFLAG("PROMISC", IFF_PROMISC); 2471 IFFLAG("ALLMULTI", IFF_ALLMULTI); 2472 IFFLAG("OACTIVE", IFF_OACTIVE); 2473 IFFLAG("SIMPLEX", IFF_SIMPLEX); 2474 IFFLAG("LINK0", IFF_LINK0); 2475 IFFLAG("LINK1", IFF_LINK1); 2476 IFFLAG("LINK2", IFF_LINK2); 2477 IFFLAG("MULTICAST", IFF_MULTICAST); 2478 #undef IFFLAG 2479 return buf; 2480 } 2481 2482 void 2483 krtread(again) 2484 int again; 2485 { 2486 int mib[6]; 2487 size_t msize; 2488 char *buf, *p, *lim; 2489 struct rt_msghdr *rtm; 2490 int retry; 2491 const char *errmsg; 2492 2493 retry = 0; 2494 buf = NULL; 2495 mib[0] = CTL_NET; 2496 mib[1] = PF_ROUTE; 2497 mib[2] = 0; 2498 mib[3] = AF_INET6; /* Address family */ 2499 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */ 2500 mib[5] = 0; /* No flags */ 2501 do { 2502 retry++; 2503 errmsg = NULL; 2504 if (buf) 2505 free(buf); 2506 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) { 2507 errmsg = "sysctl estimate"; 2508 continue; 2509 } 2510 if ((buf = malloc(msize)) == NULL) { 2511 errmsg = "malloc"; 2512 continue; 2513 } 2514 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) { 2515 errmsg = "sysctl NET_RT_DUMP"; 2516 continue; 2517 } 2518 } while (retry < 5 && errmsg != NULL); 2519 if (errmsg) { 2520 fatal("%s (with %d retries, msize=%lu)", errmsg, retry, 2521 (u_long)msize); 2522 /*NOTREACHED*/ 2523 } else if (1 < retry) 2524 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry); 2525 2526 lim = buf + msize; 2527 for (p = buf; p < lim; p += rtm->rtm_msglen) { 2528 rtm = (struct rt_msghdr *)p; 2529 rt_entry(rtm, again); 2530 } 2531 free(buf); 2532 } 2533 2534 void 2535 rt_entry(rtm, again) 2536 struct rt_msghdr *rtm; 2537 int again; 2538 { 2539 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask; 2540 struct sockaddr_in6 *sin6_genmask, *sin6_ifp; 2541 char *rtmp, *ifname = NULL; 2542 struct riprt *rrt, *orrt; 2543 struct netinfo6 *np; 2544 int s; 2545 2546 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0; 2547 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags & 2548 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) { 2549 return; /* not interested in the link route */ 2550 } 2551 /* do not look at cloned routes */ 2552 #ifdef RTF_WASCLONED 2553 if (rtm->rtm_flags & RTF_WASCLONED) 2554 return; 2555 #endif 2556 #ifdef RTF_CLONED 2557 if (rtm->rtm_flags & RTF_CLONED) 2558 return; 2559 #endif 2560 /* 2561 * do not look at dynamic routes. 2562 * netbsd/openbsd cloned routes have UGHD. 2563 */ 2564 if (rtm->rtm_flags & RTF_DYNAMIC) 2565 return; 2566 rtmp = (char *)(rtm + 1); 2567 /* Destination */ 2568 if ((rtm->rtm_addrs & RTA_DST) == 0) 2569 return; /* ignore routes without destination address */ 2570 sin6_dst = (struct sockaddr_in6 *)rtmp; 2571 rtmp += ROUNDUP(sin6_dst->sin6_len); 2572 if (rtm->rtm_addrs & RTA_GATEWAY) { 2573 sin6_gw = (struct sockaddr_in6 *)rtmp; 2574 rtmp += ROUNDUP(sin6_gw->sin6_len); 2575 } 2576 if (rtm->rtm_addrs & RTA_NETMASK) { 2577 sin6_mask = (struct sockaddr_in6 *)rtmp; 2578 rtmp += ROUNDUP(sin6_mask->sin6_len); 2579 } 2580 if (rtm->rtm_addrs & RTA_GENMASK) { 2581 sin6_genmask = (struct sockaddr_in6 *)rtmp; 2582 rtmp += ROUNDUP(sin6_genmask->sin6_len); 2583 } 2584 if (rtm->rtm_addrs & RTA_IFP) { 2585 sin6_ifp = (struct sockaddr_in6 *)rtmp; 2586 rtmp += ROUNDUP(sin6_ifp->sin6_len); 2587 } 2588 2589 /* Destination */ 2590 if (sin6_dst->sin6_family != AF_INET6) 2591 return; 2592 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr)) 2593 return; /* Link-local */ 2594 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback)) 2595 return; /* Loopback */ 2596 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr)) 2597 return; 2598 2599 if ((rrt = MALLOC(struct riprt)) == NULL) { 2600 fatal("malloc: struct riprt"); 2601 /*NOTREACHED*/ 2602 } 2603 memset(rrt, 0, sizeof(*rrt)); 2604 np = &rrt->rrt_info; 2605 rrt->rrt_same = NULL; 2606 rrt->rrt_t = time(NULL); 2607 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC)) 2608 rrt->rrt_t = 0; /* Don't age static routes */ 2609 np->rip6_tag = 0; 2610 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount; 2611 if (np->rip6_metric < 1) 2612 np->rip6_metric = 1; 2613 rrt->rrt_flags = rtm->rtm_flags; 2614 np->rip6_dest = sin6_dst->sin6_addr; 2615 2616 /* Mask or plen */ 2617 if (rtm->rtm_flags & RTF_HOST) 2618 np->rip6_plen = 128; /* Host route */ 2619 else if (sin6_mask) 2620 np->rip6_plen = sin6mask2len(sin6_mask); 2621 else 2622 np->rip6_plen = 0; 2623 2624 orrt = rtsearch(np, NULL); 2625 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) { 2626 /* Already found */ 2627 if (!again) { 2628 trace(1, "route: %s/%d flags %s: already registered\n", 2629 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2630 rtflags(rtm)); 2631 } 2632 free(rrt); 2633 return; 2634 } 2635 /* Gateway */ 2636 if (!sin6_gw) 2637 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr)); 2638 else { 2639 if (sin6_gw->sin6_family == AF_INET6) 2640 rrt->rrt_gw = sin6_gw->sin6_addr; 2641 else if (sin6_gw->sin6_family == AF_LINK) { 2642 /* XXX in case ppp link? */ 2643 rrt->rrt_gw = in6addr_loopback; 2644 } else 2645 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr)); 2646 } 2647 trace(1, "route: %s/%d flags %s", 2648 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm)); 2649 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw)); 2650 2651 /* Interface */ 2652 s = rtm->rtm_index; 2653 if (s < nindex2ifc && index2ifc[s]) 2654 ifname = index2ifc[s]->ifc_name; 2655 else { 2656 trace(1, " not configured\n"); 2657 free(rrt); 2658 return; 2659 } 2660 trace(1, " if %s sock %d", ifname, s); 2661 rrt->rrt_index = s; 2662 2663 trace(1, "\n"); 2664 2665 /* Check gateway */ 2666 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) && 2667 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) 2668 #ifdef __FreeBSD__ 2669 && (rrt->rrt_flags & RTF_LOCAL) == 0 2670 #endif 2671 ) { 2672 trace(0, "***** Gateway %s is not a link-local address.\n", 2673 inet6_n2p(&rrt->rrt_gw)); 2674 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n", 2675 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname); 2676 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR; 2677 } 2678 2679 /* Put it to the route list */ 2680 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) { 2681 /* replace route list */ 2682 rrt->rrt_next = orrt->rrt_next; 2683 *orrt = *rrt; 2684 trace(1, "route: %s/%d flags %s: replace new route\n", 2685 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2686 rtflags(rtm)); 2687 free(rrt); 2688 } else { 2689 rrt->rrt_next = riprt; 2690 riprt = rrt; 2691 } 2692 } 2693 2694 int 2695 addroute(rrt, gw, ifcp) 2696 struct riprt *rrt; 2697 const struct in6_addr *gw; 2698 struct ifc *ifcp; 2699 { 2700 struct netinfo6 *np; 2701 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ]; 2702 struct rt_msghdr *rtm; 2703 struct sockaddr_in6 *sin6; 2704 int len; 2705 2706 np = &rrt->rrt_info; 2707 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1)); 2708 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2)); 2709 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n", 2710 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1, 2711 np->rip6_metric - 1, buf2); 2712 if (rtlog) 2713 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(), 2714 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1, 2715 np->rip6_metric - 1, buf2); 2716 if (nflag) 2717 return 0; 2718 2719 memset(buf, 0, sizeof(buf)); 2720 rtm = (struct rt_msghdr *)buf; 2721 rtm->rtm_type = RTM_ADD; 2722 rtm->rtm_version = RTM_VERSION; 2723 rtm->rtm_seq = ++seq; 2724 rtm->rtm_pid = pid; 2725 rtm->rtm_flags = rrt->rrt_flags; 2726 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2727 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1; 2728 rtm->rtm_inits = RTV_HOPCOUNT; 2729 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2730 /* Destination */ 2731 sin6->sin6_len = sizeof(struct sockaddr_in6); 2732 sin6->sin6_family = AF_INET6; 2733 sin6->sin6_addr = np->rip6_dest; 2734 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2735 /* Gateway */ 2736 sin6->sin6_len = sizeof(struct sockaddr_in6); 2737 sin6->sin6_family = AF_INET6; 2738 sin6->sin6_addr = *gw; 2739 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2740 /* Netmask */ 2741 sin6->sin6_len = sizeof(struct sockaddr_in6); 2742 sin6->sin6_family = AF_INET6; 2743 sin6->sin6_addr = *(plen2mask(np->rip6_plen)); 2744 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2745 2746 len = (char *)sin6 - (char *)buf; 2747 rtm->rtm_msglen = len; 2748 if (write(rtsock, buf, len) > 0) 2749 return 0; 2750 2751 if (errno == EEXIST) { 2752 trace(0, "ADD: Route already exists %s/%d gw %s\n", 2753 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1); 2754 if (rtlog) 2755 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n", 2756 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1); 2757 } else { 2758 trace(0, "Can not write to rtsock (addroute): %s\n", 2759 strerror(errno)); 2760 if (rtlog) 2761 fprintf(rtlog, "\tCan not write to rtsock: %s\n", 2762 strerror(errno)); 2763 } 2764 return -1; 2765 } 2766 2767 int 2768 delroute(np, gw) 2769 struct netinfo6 *np; 2770 struct in6_addr *gw; 2771 { 2772 u_char buf[BUFSIZ], buf2[BUFSIZ]; 2773 struct rt_msghdr *rtm; 2774 struct sockaddr_in6 *sin6; 2775 int len; 2776 2777 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2)); 2778 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest), 2779 np->rip6_plen, buf2); 2780 if (rtlog) 2781 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n", 2782 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2783 if (nflag) 2784 return 0; 2785 2786 memset(buf, 0, sizeof(buf)); 2787 rtm = (struct rt_msghdr *)buf; 2788 rtm->rtm_type = RTM_DELETE; 2789 rtm->rtm_version = RTM_VERSION; 2790 rtm->rtm_seq = ++seq; 2791 rtm->rtm_pid = pid; 2792 rtm->rtm_flags = RTF_UP | RTF_GATEWAY; 2793 if (np->rip6_plen == sizeof(struct in6_addr) * 8) 2794 rtm->rtm_flags |= RTF_HOST; 2795 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2796 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2797 /* Destination */ 2798 sin6->sin6_len = sizeof(struct sockaddr_in6); 2799 sin6->sin6_family = AF_INET6; 2800 sin6->sin6_addr = np->rip6_dest; 2801 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2802 /* Gateway */ 2803 sin6->sin6_len = sizeof(struct sockaddr_in6); 2804 sin6->sin6_family = AF_INET6; 2805 sin6->sin6_addr = *gw; 2806 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2807 /* Netmask */ 2808 sin6->sin6_len = sizeof(struct sockaddr_in6); 2809 sin6->sin6_family = AF_INET6; 2810 sin6->sin6_addr = *(plen2mask(np->rip6_plen)); 2811 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2812 2813 len = (char *)sin6 - (char *)buf; 2814 rtm->rtm_msglen = len; 2815 if (write(rtsock, buf, len) >= 0) 2816 return 0; 2817 2818 if (errno == ESRCH) { 2819 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n", 2820 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2821 if (rtlog) 2822 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n", 2823 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2824 } else { 2825 trace(0, "Can not write to rtsock (delroute): %s\n", 2826 strerror(errno)); 2827 if (rtlog) 2828 fprintf(rtlog, "\tCan not write to rtsock: %s\n", 2829 strerror(errno)); 2830 } 2831 return -1; 2832 } 2833 2834 struct in6_addr * 2835 getroute(np, gw) 2836 struct netinfo6 *np; 2837 struct in6_addr *gw; 2838 { 2839 u_char buf[BUFSIZ]; 2840 u_long myseq; 2841 int len; 2842 struct rt_msghdr *rtm; 2843 struct sockaddr_in6 *sin6; 2844 2845 rtm = (struct rt_msghdr *)buf; 2846 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6); 2847 memset(rtm, 0, len); 2848 rtm->rtm_type = RTM_GET; 2849 rtm->rtm_version = RTM_VERSION; 2850 myseq = ++seq; 2851 rtm->rtm_seq = myseq; 2852 rtm->rtm_addrs = RTA_DST; 2853 rtm->rtm_msglen = len; 2854 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2855 sin6->sin6_len = sizeof(struct sockaddr_in6); 2856 sin6->sin6_family = AF_INET6; 2857 sin6->sin6_addr = np->rip6_dest; 2858 if (write(rtsock, buf, len) < 0) { 2859 if (errno == ESRCH) /* No such route found */ 2860 return NULL; 2861 perror("write to rtsock"); 2862 exit(1); 2863 } 2864 do { 2865 if ((len = read(rtsock, buf, sizeof(buf))) < 0) { 2866 perror("read from rtsock"); 2867 exit(1); 2868 } 2869 rtm = (struct rt_msghdr *)buf; 2870 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid); 2871 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2872 if (rtm->rtm_addrs & RTA_DST) { 2873 sin6 = (struct sockaddr_in6 *) 2874 ((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2875 } 2876 if (rtm->rtm_addrs & RTA_GATEWAY) { 2877 *gw = sin6->sin6_addr; 2878 return gw; 2879 } 2880 return NULL; 2881 } 2882 2883 const char * 2884 inet6_n2p(p) 2885 const struct in6_addr *p; 2886 { 2887 static char buf[BUFSIZ]; 2888 2889 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf)); 2890 } 2891 2892 void 2893 ifrtdump(sig) 2894 int sig; 2895 { 2896 2897 ifdump(sig); 2898 rtdump(sig); 2899 } 2900 2901 void 2902 ifdump(sig) 2903 int sig; 2904 { 2905 struct ifc *ifcp; 2906 FILE *dump; 2907 int i; 2908 2909 if (sig == 0) 2910 dump = stderr; 2911 else 2912 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL) 2913 dump = stderr; 2914 2915 fprintf(dump, "%s: Interface Table Dump\n", hms()); 2916 fprintf(dump, " Number of interfaces: %d\n", nifc); 2917 for (i = 0; i < 2; i++) { 2918 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : ""); 2919 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) { 2920 if (i == 0) { 2921 if ((ifcp->ifc_flags & IFF_UP) == 0) 2922 continue; 2923 if (iff_find(ifcp, 'N') != NULL) 2924 continue; 2925 } else { 2926 if (ifcp->ifc_flags & IFF_UP) 2927 continue; 2928 } 2929 ifdump0(dump, ifcp); 2930 } 2931 } 2932 fprintf(dump, "\n"); 2933 if (dump != stderr) 2934 fclose(dump); 2935 } 2936 2937 void 2938 ifdump0(dump, ifcp) 2939 FILE *dump; 2940 const struct ifc *ifcp; 2941 { 2942 struct ifac *ifa; 2943 struct iff *iffp; 2944 char buf[BUFSIZ]; 2945 const char *ft; 2946 int addr; 2947 2948 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n", 2949 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags), 2950 inet6_n2p(&ifcp->ifc_mylladdr), 2951 ifcp->ifc_mtu, ifcp->ifc_metric); 2952 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) { 2953 if (ifcp->ifc_flags & IFF_POINTOPOINT) { 2954 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, 2955 buf, sizeof(buf)); 2956 fprintf(dump, "\t%s/%d -- %s\n", 2957 inet6_n2p(&ifa->ifa_addr), 2958 ifa->ifa_plen, buf); 2959 } else { 2960 fprintf(dump, "\t%s/%d\n", 2961 inet6_n2p(&ifa->ifa_addr), 2962 ifa->ifa_plen); 2963 } 2964 } 2965 if (ifcp->ifc_filter) { 2966 fprintf(dump, "\tFilter:"); 2967 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 2968 addr = 0; 2969 switch (iffp->iff_type) { 2970 case 'A': 2971 ft = "Aggregate"; addr++; break; 2972 case 'N': 2973 ft = "No-use"; break; 2974 case 'O': 2975 ft = "Advertise-only"; addr++; break; 2976 case 'T': 2977 ft = "Default-only"; break; 2978 case 'L': 2979 ft = "Listen-only"; addr++; break; 2980 default: 2981 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type); 2982 ft = buf; 2983 addr++; 2984 break; 2985 } 2986 fprintf(dump, " %s", ft); 2987 if (addr) { 2988 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr), 2989 iffp->iff_plen); 2990 } 2991 } 2992 fprintf(dump, "\n"); 2993 } 2994 } 2995 2996 void 2997 rtdump(sig) 2998 int sig; 2999 { 3000 struct riprt *rrt; 3001 char buf[BUFSIZ]; 3002 FILE *dump; 3003 time_t t, age; 3004 3005 if (sig == 0) 3006 dump = stderr; 3007 else 3008 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL) 3009 dump = stderr; 3010 3011 t = time(NULL); 3012 fprintf(dump, "\n%s: Routing Table Dump\n", hms()); 3013 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 3014 if (rrt->rrt_t == 0) 3015 age = 0; 3016 else 3017 age = t - rrt->rrt_t; 3018 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest, 3019 buf, sizeof(buf)); 3020 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)", 3021 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index, 3022 index2ifc[rrt->rrt_index]->ifc_name, 3023 inet6_n2p(&rrt->rrt_gw), 3024 rrt->rrt_info.rip6_metric, (long)age); 3025 if (rrt->rrt_info.rip6_tag) { 3026 fprintf(dump, " tag(0x%04x)", 3027 ntohs(rrt->rrt_info.rip6_tag) & 0xffff); 3028 } 3029 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) 3030 fprintf(dump, " NOT-LL"); 3031 if (rrt->rrt_rflags & RRTF_NOADVERTISE) 3032 fprintf(dump, " NO-ADV"); 3033 fprintf(dump, "\n"); 3034 } 3035 fprintf(dump, "\n"); 3036 if (dump != stderr) 3037 fclose(dump); 3038 } 3039 3040 /* 3041 * Parse the -A (and -O) options and put corresponding filter object to the 3042 * specified interface structures. Each of the -A/O option has the following 3043 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate) 3044 * -O 5f09:c400::/32,ef0,ef1 (only when match) 3045 */ 3046 void 3047 filterconfig() 3048 { 3049 int i; 3050 char *p, *ap, *iflp, *ifname; 3051 struct iff ftmp, *iff_obj; 3052 struct ifc *ifcp; 3053 struct riprt *rrt; 3054 #if 0 3055 struct in6_addr gw; 3056 #endif 3057 3058 for (i = 0; i < nfilter; i++) { 3059 ap = filter[i]; 3060 iflp = NULL; 3061 ifcp = NULL; 3062 if (filtertype[i] == 'N' || filtertype[i] == 'T') { 3063 iflp = ap; 3064 goto ifonly; 3065 } 3066 if ((p = strchr(ap, ',')) != NULL) { 3067 *p++ = '\0'; 3068 iflp = p; 3069 } 3070 if ((p = strchr(ap, '/')) == NULL) { 3071 fatal("no prefixlen specified for '%s'", ap); 3072 /*NOTREACHED*/ 3073 } 3074 *p++ = '\0'; 3075 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) { 3076 fatal("invalid prefix specified for '%s'", ap); 3077 /*NOTREACHED*/ 3078 } 3079 ftmp.iff_plen = atoi(p); 3080 ftmp.iff_next = NULL; 3081 applyplen(&ftmp.iff_addr, ftmp.iff_plen); 3082 ifonly: 3083 ftmp.iff_type = filtertype[i]; 3084 if (iflp == NULL || *iflp == '\0') { 3085 fatal("no interface specified for '%s'", ap); 3086 /*NOTREACHED*/ 3087 } 3088 /* parse the interface listing portion */ 3089 while (iflp) { 3090 ifname = iflp; 3091 if ((iflp = strchr(iflp, ',')) != NULL) 3092 *iflp++ = '\0'; 3093 ifcp = ifc_find(ifname); 3094 if (ifcp == NULL) { 3095 fatal("no interface %s exists", ifname); 3096 /*NOTREACHED*/ 3097 } 3098 iff_obj = (struct iff *)malloc(sizeof(struct iff)); 3099 if (iff_obj == NULL) { 3100 fatal("malloc of iff_obj"); 3101 /*NOTREACHED*/ 3102 } 3103 memcpy((void *)iff_obj, (void *)&ftmp, 3104 sizeof(struct iff)); 3105 /* link it to the interface filter */ 3106 iff_obj->iff_next = ifcp->ifc_filter; 3107 ifcp->ifc_filter = iff_obj; 3108 } 3109 3110 /* 3111 * -A: aggregate configuration. 3112 */ 3113 if (filtertype[i] != 'A') 3114 continue; 3115 /* put the aggregate to the kernel routing table */ 3116 rrt = (struct riprt *)malloc(sizeof(struct riprt)); 3117 if (rrt == NULL) { 3118 fatal("malloc: rrt"); 3119 /*NOTREACHED*/ 3120 } 3121 memset(rrt, 0, sizeof(struct riprt)); 3122 rrt->rrt_info.rip6_dest = ftmp.iff_addr; 3123 rrt->rrt_info.rip6_plen = ftmp.iff_plen; 3124 rrt->rrt_info.rip6_metric = 1; 3125 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff); 3126 rrt->rrt_gw = in6addr_loopback; 3127 rrt->rrt_flags = RTF_UP | RTF_REJECT; 3128 rrt->rrt_rflags = RRTF_AGGREGATE; 3129 rrt->rrt_t = 0; 3130 rrt->rrt_index = loopifcp->ifc_index; 3131 #if 0 3132 if (getroute(&rrt->rrt_info, &gw)) { 3133 #if 0 3134 /* 3135 * When the address has already been registered in the 3136 * kernel routing table, it should be removed 3137 */ 3138 delroute(&rrt->rrt_info, &gw); 3139 #else 3140 /* it is safer behavior */ 3141 errno = EINVAL; 3142 fatal("%s/%u already in routing table, " 3143 "cannot aggregate", 3144 inet6_n2p(&rrt->rrt_info.rip6_dest), 3145 rrt->rrt_info.rip6_plen); 3146 /*NOTREACHED*/ 3147 #endif 3148 } 3149 #endif 3150 /* Put the route to the list */ 3151 rrt->rrt_next = riprt; 3152 riprt = rrt; 3153 trace(1, "Aggregate: %s/%d for %s\n", 3154 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen, 3155 ifcp->ifc_name); 3156 /* Add this route to the kernel */ 3157 if (nflag) /* do not modify kernel routing table */ 3158 continue; 3159 addroute(rrt, &in6addr_loopback, loopifcp); 3160 } 3161 } 3162 3163 /***************** utility functions *****************/ 3164 3165 /* 3166 * Returns a pointer to ifac whose address and prefix length matches 3167 * with the address and prefix length specified in the arguments. 3168 */ 3169 struct ifac * 3170 ifa_match(ifcp, ia, plen) 3171 const struct ifc *ifcp; 3172 const struct in6_addr *ia; 3173 int plen; 3174 { 3175 struct ifac *ifa; 3176 3177 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) { 3178 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) && 3179 ifa->ifa_plen == plen) 3180 break; 3181 } 3182 return ifa; 3183 } 3184 3185 /* 3186 * Return a pointer to riprt structure whose address and prefix length 3187 * matches with the address and prefix length found in the argument. 3188 * Note: This is not a rtalloc(). Therefore exact match is necessary. 3189 */ 3190 struct riprt * 3191 rtsearch(np, prev_rrt) 3192 struct netinfo6 *np; 3193 struct riprt **prev_rrt; 3194 { 3195 struct riprt *rrt; 3196 3197 if (prev_rrt) 3198 *prev_rrt = NULL; 3199 for (rrt = riprt; rrt; rrt = rrt->rrt_next) { 3200 if (rrt->rrt_info.rip6_plen == np->rip6_plen && 3201 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest, 3202 &np->rip6_dest)) 3203 return rrt; 3204 if (prev_rrt) 3205 *prev_rrt = rrt; 3206 } 3207 if (prev_rrt) 3208 *prev_rrt = NULL; 3209 return 0; 3210 } 3211 3212 int 3213 sin6mask2len(sin6) 3214 const struct sockaddr_in6 *sin6; 3215 { 3216 3217 return mask2len(&sin6->sin6_addr, 3218 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr)); 3219 } 3220 3221 int 3222 mask2len(addr, lenlim) 3223 const struct in6_addr *addr; 3224 int lenlim; 3225 { 3226 int i = 0, j; 3227 const u_char *p = (const u_char *)addr; 3228 3229 for (j = 0; j < lenlim; j++, p++) { 3230 if (*p != 0xff) 3231 break; 3232 i += 8; 3233 } 3234 if (j < lenlim) { 3235 switch (*p) { 3236 #define MASKLEN(m, l) case m: do { i += l; break; } while (0) 3237 MASKLEN(0xfe, 7); break; 3238 MASKLEN(0xfc, 6); break; 3239 MASKLEN(0xf8, 5); break; 3240 MASKLEN(0xf0, 4); break; 3241 MASKLEN(0xe0, 3); break; 3242 MASKLEN(0xc0, 2); break; 3243 MASKLEN(0x80, 1); break; 3244 #undef MASKLEN 3245 } 3246 } 3247 return i; 3248 } 3249 3250 void 3251 applymask(addr, mask) 3252 struct in6_addr *addr, *mask; 3253 { 3254 int i; 3255 u_long *p, *q; 3256 3257 p = (u_long *)addr; q = (u_long *)mask; 3258 for (i = 0; i < 4; i++) 3259 *p++ &= *q++; 3260 } 3261 3262 static const u_char plent[8] = { 3263 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe 3264 }; 3265 3266 void 3267 applyplen(ia, plen) 3268 struct in6_addr *ia; 3269 int plen; 3270 { 3271 u_char *p; 3272 int i; 3273 3274 p = ia->s6_addr; 3275 for (i = 0; i < 16; i++) { 3276 if (plen <= 0) 3277 *p = 0; 3278 else if (plen < 8) 3279 *p &= plent[plen]; 3280 p++, plen -= 8; 3281 } 3282 } 3283 3284 static const int pl2m[9] = { 3285 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff 3286 }; 3287 3288 struct in6_addr * 3289 plen2mask(n) 3290 int n; 3291 { 3292 static struct in6_addr ia; 3293 u_char *p; 3294 int i; 3295 3296 memset(&ia, 0, sizeof(struct in6_addr)); 3297 p = (u_char *)&ia; 3298 for (i = 0; i < 16; i++, p++, n -= 8) { 3299 if (n >= 8) { 3300 *p = 0xff; 3301 continue; 3302 } 3303 *p = pl2m[n]; 3304 break; 3305 } 3306 return &ia; 3307 } 3308 3309 char * 3310 allocopy(p) 3311 char *p; 3312 { 3313 int len = strlen(p) + 1; 3314 char *q = (char *)malloc(len); 3315 3316 if (!q) { 3317 fatal("malloc"); 3318 /*NOTREACHED*/ 3319 } 3320 3321 strlcpy(q, p, len); 3322 return q; 3323 } 3324 3325 char * 3326 hms() 3327 { 3328 static char buf[BUFSIZ]; 3329 time_t t; 3330 struct tm *tm; 3331 3332 t = time(NULL); 3333 if ((tm = localtime(&t)) == 0) { 3334 fatal("localtime"); 3335 /*NOTREACHED*/ 3336 } 3337 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, 3338 tm->tm_sec); 3339 return buf; 3340 } 3341 3342 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */ 3343 3344 int 3345 ripinterval(timer) 3346 int timer; 3347 { 3348 double r = rand(); 3349 3350 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5)); 3351 nextalarm = time(NULL) + interval; 3352 return interval; 3353 } 3354 3355 time_t 3356 ripsuptrig() 3357 { 3358 time_t t; 3359 3360 double r = rand(); 3361 t = (int)(RIP_TRIG_INT6_MIN + 3362 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX)); 3363 sup_trig_update = time(NULL) + t; 3364 return t; 3365 } 3366 3367 void 3368 #ifdef __STDC__ 3369 fatal(const char *fmt, ...) 3370 #else 3371 fatal(fmt, va_alist) 3372 char *fmt; 3373 va_dcl 3374 #endif 3375 { 3376 va_list ap; 3377 char buf[1024]; 3378 3379 #ifdef __STDC__ 3380 va_start(ap, fmt); 3381 #else 3382 va_start(ap); 3383 #endif 3384 vsnprintf(buf, sizeof(buf), fmt, ap); 3385 va_end(ap); 3386 perror(buf); 3387 if (errno) 3388 syslog(LOG_ERR, "%s: %s", buf, strerror(errno)); 3389 else 3390 syslog(LOG_ERR, "%s", buf); 3391 rtdexit(); 3392 } 3393 3394 void 3395 #ifdef __STDC__ 3396 tracet(int level, const char *fmt, ...) 3397 #else 3398 tracet(level, fmt, va_alist) 3399 int level; 3400 char *fmt; 3401 va_dcl 3402 #endif 3403 { 3404 va_list ap; 3405 3406 if (level <= dflag) { 3407 #ifdef __STDC__ 3408 va_start(ap, fmt); 3409 #else 3410 va_start(ap); 3411 #endif 3412 fprintf(stderr, "%s: ", hms()); 3413 vfprintf(stderr, fmt, ap); 3414 va_end(ap); 3415 } 3416 if (dflag) { 3417 #ifdef __STDC__ 3418 va_start(ap, fmt); 3419 #else 3420 va_start(ap); 3421 #endif 3422 if (level > 0) 3423 vsyslog(LOG_DEBUG, fmt, ap); 3424 else 3425 vsyslog(LOG_WARNING, fmt, ap); 3426 va_end(ap); 3427 } 3428 } 3429 3430 void 3431 #ifdef __STDC__ 3432 trace(int level, const char *fmt, ...) 3433 #else 3434 trace(level, fmt, va_alist) 3435 int level; 3436 char *fmt; 3437 va_dcl 3438 #endif 3439 { 3440 va_list ap; 3441 3442 if (level <= dflag) { 3443 #ifdef __STDC__ 3444 va_start(ap, fmt); 3445 #else 3446 va_start(ap); 3447 #endif 3448 vfprintf(stderr, fmt, ap); 3449 va_end(ap); 3450 } 3451 if (dflag) { 3452 #ifdef __STDC__ 3453 va_start(ap, fmt); 3454 #else 3455 va_start(ap); 3456 #endif 3457 if (level > 0) 3458 vsyslog(LOG_DEBUG, fmt, ap); 3459 else 3460 vsyslog(LOG_WARNING, fmt, ap); 3461 va_end(ap); 3462 } 3463 } 3464 3465 unsigned int 3466 if_maxindex() 3467 { 3468 struct if_nameindex *p, *p0; 3469 unsigned int max = 0; 3470 3471 p0 = if_nameindex(); 3472 for (p = p0; p && p->if_index && p->if_name; p++) { 3473 if (max < p->if_index) 3474 max = p->if_index; 3475 } 3476 if_freenameindex(p0); 3477 return max; 3478 } 3479 3480 struct ifc * 3481 ifc_find(name) 3482 char *name; 3483 { 3484 struct ifc *ifcp; 3485 3486 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) { 3487 if (strcmp(name, ifcp->ifc_name) == 0) 3488 return ifcp; 3489 } 3490 return (struct ifc *)NULL; 3491 } 3492 3493 struct iff * 3494 iff_find(ifcp, type) 3495 struct ifc *ifcp; 3496 int type; 3497 { 3498 struct iff *iffp; 3499 3500 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) { 3501 if (iffp->iff_type == type) 3502 return iffp; 3503 } 3504 return NULL; 3505 } 3506 3507 void 3508 setindex2ifc(idx, ifcp) 3509 int idx; 3510 struct ifc *ifcp; 3511 { 3512 int n; 3513 struct ifc **p; 3514 3515 if (!index2ifc) { 3516 nindex2ifc = 5; /*initial guess*/ 3517 index2ifc = (struct ifc **) 3518 malloc(sizeof(*index2ifc) * nindex2ifc); 3519 if (index2ifc == NULL) { 3520 fatal("malloc"); 3521 /*NOTREACHED*/ 3522 } 3523 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc); 3524 } 3525 n = nindex2ifc; 3526 while (nindex2ifc <= idx) 3527 nindex2ifc *= 2; 3528 if (n != nindex2ifc) { 3529 p = (struct ifc **)realloc(index2ifc, 3530 sizeof(*index2ifc) * nindex2ifc); 3531 if (p == NULL) { 3532 fatal("realloc"); 3533 /*NOTREACHED*/ 3534 } 3535 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n)); 3536 index2ifc = p; 3537 } 3538 index2ifc[idx] = ifcp; 3539 } 3540