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