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