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