1/* $FreeBSD: stable/11/usr.sbin/route6d/route6d.c 338279 2018-08-23 21:24:22Z markj $ */ 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, search_rrt, rrt_next); 2207 delroute(&search_rrt->rrt_info, 2208 &search_rrt->rrt_gw); 2209 free(search_rrt); 2210 } 2211 /* Attach the route to the list */ 2212 trace(1, "route: %s/%d: register route (%s)\n", 2213 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2214 ifcp->ifc_name); 2215 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next); 2216 addroute(rrt, &rrt->rrt_gw, ifcp); 2217 rrt = NULL; 2218 sendrequest(ifcp); 2219 ripsend(ifcp, &ifcp->ifc_ripsin, 0); 2220 need_trigger = 1; 2221 } else { 2222 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) { 2223 if (loop_rrt->rrt_index == ifcp->ifc_index) { 2224 t_lifetime = time(NULL) - RIP_LIFETIME; 2225 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) { 2226 loop_rrt->rrt_t = t_lifetime; 2227 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6; 2228 loop_rrt->rrt_rflags |= RRTF_CHANGED; 2229 need_trigger = 1; 2230 } 2231 } 2232 } 2233 } 2234 next: 2235 if (rrt) 2236 free(rrt); 2237 } 2238 return need_trigger; 2239} 2240 2241/* 2242 * there are couple of p2p interface routing models. "behavior" lets 2243 * you pick one. it looks that gated behavior fits best with BSDs, 2244 * since BSD kernels do not look at prefix length on p2p interfaces. 2245 */ 2246void 2247ifrt_p2p(struct ifc *ifcp, int again) 2248{ 2249 struct ifac *ifac; 2250 struct riprt *rrt, *orrt; 2251 struct netinfo6 *np; 2252 struct in6_addr addr, dest; 2253 int advert, ignore, i; 2254#define P2PADVERT_NETWORK 1 2255#define P2PADVERT_ADDR 2 2256#define P2PADVERT_DEST 4 2257#define P2PADVERT_MAX 4 2258 const enum { CISCO, GATED, ROUTE6D } behavior = GATED; 2259 const char *category = ""; 2260 const char *noadv; 2261 2262 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) { 2263 addr = ifac->ifac_addr; 2264 dest = ifac->ifac_raddr; 2265 applyplen(&addr, ifac->ifac_plen); 2266 applyplen(&dest, ifac->ifac_plen); 2267 advert = ignore = 0; 2268 switch (behavior) { 2269 case CISCO: 2270 /* 2271 * honor addr/plen, just like normal shared medium 2272 * interface. this may cause trouble if you reuse 2273 * addr/plen on other interfaces. 2274 * 2275 * advertise addr/plen. 2276 */ 2277 advert |= P2PADVERT_NETWORK; 2278 break; 2279 case GATED: 2280 /* 2281 * prefixlen on p2p interface is meaningless. 2282 * advertise addr/128 and dest/128. 2283 * 2284 * do not install network route to route6d routing 2285 * table (if we do, it would prevent route installation 2286 * for other p2p interface that shares addr/plen). 2287 * 2288 * XXX what should we do if dest is ::? it will not 2289 * get announced anyways (see following filter), 2290 * but we need to think. 2291 */ 2292 advert |= P2PADVERT_ADDR; 2293 advert |= P2PADVERT_DEST; 2294 ignore |= P2PADVERT_NETWORK; 2295 break; 2296 case ROUTE6D: 2297 /* 2298 * just for testing. actually the code is redundant 2299 * given the current p2p interface address assignment 2300 * rule for kame kernel. 2301 * 2302 * intent: 2303 * A/n -> announce A/n 2304 * A B/n, A and B share prefix -> A/n (= B/n) 2305 * A B/n, do not share prefix -> A/128 and B/128 2306 * actually, A/64 and A B/128 are the only cases 2307 * permitted by the kernel: 2308 * A/64 -> A/64 2309 * A B/128 -> A/128 and B/128 2310 */ 2311 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) { 2312 if (IN6_ARE_ADDR_EQUAL(&addr, &dest)) 2313 advert |= P2PADVERT_NETWORK; 2314 else { 2315 advert |= P2PADVERT_ADDR; 2316 advert |= P2PADVERT_DEST; 2317 ignore |= P2PADVERT_NETWORK; 2318 } 2319 } else 2320 advert |= P2PADVERT_NETWORK; 2321 break; 2322 } 2323 2324 for (i = 1; i <= P2PADVERT_MAX; i *= 2) { 2325 if ((ignore & i) != 0) 2326 continue; 2327 if ((rrt = MALLOC(struct riprt)) == NULL) { 2328 fatal("malloc: struct riprt"); 2329 /*NOTREACHED*/ 2330 } 2331 memset(rrt, 0, sizeof(*rrt)); 2332 rrt->rrt_same = NULL; 2333 rrt->rrt_index = ifcp->ifc_index; 2334 rrt->rrt_t = 0; /* don't age */ 2335 switch (i) { 2336 case P2PADVERT_NETWORK: 2337 rrt->rrt_info.rip6_dest = ifac->ifac_addr; 2338 rrt->rrt_info.rip6_plen = ifac->ifac_plen; 2339 applyplen(&rrt->rrt_info.rip6_dest, 2340 ifac->ifac_plen); 2341 category = "network"; 2342 break; 2343 case P2PADVERT_ADDR: 2344 rrt->rrt_info.rip6_dest = ifac->ifac_addr; 2345 rrt->rrt_info.rip6_plen = 128; 2346 rrt->rrt_gw = in6addr_loopback; 2347 category = "addr"; 2348 break; 2349 case P2PADVERT_DEST: 2350 rrt->rrt_info.rip6_dest = ifac->ifac_raddr; 2351 rrt->rrt_info.rip6_plen = 128; 2352 rrt->rrt_gw = ifac->ifac_addr; 2353 category = "dest"; 2354 break; 2355 } 2356 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) || 2357 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) { 2358#if 0 2359 trace(1, "route: %s: skip unspec/linklocal " 2360 "(%s on %s)\n", category, ifcp->ifc_name); 2361#endif 2362 free(rrt); 2363 continue; 2364 } 2365 if ((advert & i) == 0) { 2366 rrt->rrt_rflags |= RRTF_NOADVERTISE; 2367 noadv = ", NO-ADV"; 2368 } else 2369 noadv = ""; 2370 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff); 2371 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric; 2372 np = &rrt->rrt_info; 2373 orrt = rtsearch(np); 2374 if (!orrt) { 2375 /* Attach the route to the list */ 2376 trace(1, "route: %s/%d: register route " 2377 "(%s on %s%s)\n", 2378 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2379 category, ifcp->ifc_name, noadv); 2380 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next); 2381 } else if (rrt->rrt_index != orrt->rrt_index || 2382 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) { 2383 /* replace route */ 2384 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next); 2385 TAILQ_REMOVE(&riprt_head, orrt, rrt_next); 2386 free(orrt); 2387 2388 trace(1, "route: %s/%d: update (%s on %s%s)\n", 2389 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2390 category, ifcp->ifc_name, noadv); 2391 } else { 2392 /* Already found */ 2393 if (!again) { 2394 trace(1, "route: %s/%d: " 2395 "already registered (%s on %s%s)\n", 2396 inet6_n2p(&np->rip6_dest), 2397 np->rip6_plen, category, 2398 ifcp->ifc_name, noadv); 2399 } 2400 free(rrt); 2401 } 2402 } 2403 } 2404#undef P2PADVERT_NETWORK 2405#undef P2PADVERT_ADDR 2406#undef P2PADVERT_DEST 2407#undef P2PADVERT_MAX 2408} 2409 2410int 2411getifmtu(int ifindex) 2412{ 2413 int mib[6]; 2414 char *buf; 2415 size_t msize; 2416 struct if_msghdr *ifm; 2417 int mtu; 2418 2419 mib[0] = CTL_NET; 2420 mib[1] = PF_ROUTE; 2421 mib[2] = 0; 2422 mib[3] = AF_INET6; 2423 mib[4] = NET_RT_IFLIST; 2424 mib[5] = ifindex; 2425 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) { 2426 fatal("sysctl estimate NET_RT_IFLIST"); 2427 /*NOTREACHED*/ 2428 } 2429 if ((buf = malloc(msize)) == NULL) { 2430 fatal("malloc"); 2431 /*NOTREACHED*/ 2432 } 2433 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) { 2434 fatal("sysctl NET_RT_IFLIST"); 2435 /*NOTREACHED*/ 2436 } 2437 ifm = (struct if_msghdr *)buf; 2438 mtu = ifm->ifm_data.ifi_mtu; 2439 if (ifindex != ifm->ifm_index) { 2440 fatal("ifindex does not match with ifm_index"); 2441 /*NOTREACHED*/ 2442 } 2443 free(buf); 2444 return mtu; 2445} 2446 2447const char * 2448rttypes(struct rt_msghdr *rtm) 2449{ 2450#define RTTYPE(s, f) \ 2451do { \ 2452 if (rtm->rtm_type == (f)) \ 2453 return (s); \ 2454} while (0) 2455 RTTYPE("ADD", RTM_ADD); 2456 RTTYPE("DELETE", RTM_DELETE); 2457 RTTYPE("CHANGE", RTM_CHANGE); 2458 RTTYPE("GET", RTM_GET); 2459 RTTYPE("LOSING", RTM_LOSING); 2460 RTTYPE("REDIRECT", RTM_REDIRECT); 2461 RTTYPE("MISS", RTM_MISS); 2462 RTTYPE("LOCK", RTM_LOCK); 2463 RTTYPE("NEWADDR", RTM_NEWADDR); 2464 RTTYPE("DELADDR", RTM_DELADDR); 2465 RTTYPE("IFINFO", RTM_IFINFO); 2466#ifdef RTM_OIFINFO 2467 RTTYPE("OIFINFO", RTM_OIFINFO); 2468#endif 2469#ifdef RTM_IFANNOUNCE 2470 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE); 2471#endif 2472#ifdef RTM_NEWMADDR 2473 RTTYPE("NEWMADDR", RTM_NEWMADDR); 2474#endif 2475#ifdef RTM_DELMADDR 2476 RTTYPE("DELMADDR", RTM_DELMADDR); 2477#endif 2478#undef RTTYPE 2479 return NULL; 2480} 2481 2482const char * 2483rtflags(struct rt_msghdr *rtm) 2484{ 2485 static char buf[BUFSIZ]; 2486 2487 /* 2488 * letter conflict should be okay. painful when *BSD diverges... 2489 */ 2490 strlcpy(buf, "", sizeof(buf)); 2491#define RTFLAG(s, f) \ 2492do { \ 2493 if (rtm->rtm_flags & (f)) \ 2494 strlcat(buf, (s), sizeof(buf)); \ 2495} while (0) 2496 RTFLAG("U", RTF_UP); 2497 RTFLAG("G", RTF_GATEWAY); 2498 RTFLAG("H", RTF_HOST); 2499 RTFLAG("R", RTF_REJECT); 2500 RTFLAG("D", RTF_DYNAMIC); 2501 RTFLAG("M", RTF_MODIFIED); 2502 RTFLAG("d", RTF_DONE); 2503#ifdef RTF_MASK 2504 RTFLAG("m", RTF_MASK); 2505#endif 2506#ifdef RTF_CLONED 2507 RTFLAG("c", RTF_CLONED); 2508#endif 2509 RTFLAG("X", RTF_XRESOLVE); 2510#ifdef RTF_LLINFO 2511 RTFLAG("L", RTF_LLINFO); 2512#endif 2513 RTFLAG("S", RTF_STATIC); 2514 RTFLAG("B", RTF_BLACKHOLE); 2515#ifdef RTF_PROTO3 2516 RTFLAG("3", RTF_PROTO3); 2517#endif 2518 RTFLAG("2", RTF_PROTO2); 2519 RTFLAG("1", RTF_PROTO1); 2520#ifdef RTF_BROADCAST 2521 RTFLAG("b", RTF_BROADCAST); 2522#endif 2523#ifdef RTF_DEFAULT 2524 RTFLAG("d", RTF_DEFAULT); 2525#endif 2526#ifdef RTF_ISAROUTER 2527 RTFLAG("r", RTF_ISAROUTER); 2528#endif 2529#ifdef RTF_TUNNEL 2530 RTFLAG("T", RTF_TUNNEL); 2531#endif 2532#ifdef RTF_AUTH 2533 RTFLAG("A", RTF_AUTH); 2534#endif 2535#ifdef RTF_CRYPT 2536 RTFLAG("E", RTF_CRYPT); 2537#endif 2538#undef RTFLAG 2539 return buf; 2540} 2541 2542const char * 2543ifflags(int flags) 2544{ 2545 static char buf[BUFSIZ]; 2546 2547 strlcpy(buf, "", sizeof(buf)); 2548#define IFFLAG(s, f) \ 2549do { \ 2550 if (flags & (f)) { \ 2551 if (buf[0]) \ 2552 strlcat(buf, ",", sizeof(buf)); \ 2553 strlcat(buf, (s), sizeof(buf)); \ 2554 } \ 2555} while (0) 2556 IFFLAG("UP", IFF_UP); 2557 IFFLAG("BROADCAST", IFF_BROADCAST); 2558 IFFLAG("DEBUG", IFF_DEBUG); 2559 IFFLAG("LOOPBACK", IFF_LOOPBACK); 2560 IFFLAG("POINTOPOINT", IFF_POINTOPOINT); 2561#ifdef IFF_NOTRAILERS 2562 IFFLAG("NOTRAILERS", IFF_NOTRAILERS); 2563#endif 2564 IFFLAG("RUNNING", IFF_RUNNING); 2565 IFFLAG("NOARP", IFF_NOARP); 2566 IFFLAG("PROMISC", IFF_PROMISC); 2567 IFFLAG("ALLMULTI", IFF_ALLMULTI); 2568 IFFLAG("OACTIVE", IFF_OACTIVE); 2569 IFFLAG("SIMPLEX", IFF_SIMPLEX); 2570 IFFLAG("LINK0", IFF_LINK0); 2571 IFFLAG("LINK1", IFF_LINK1); 2572 IFFLAG("LINK2", IFF_LINK2); 2573 IFFLAG("MULTICAST", IFF_MULTICAST); 2574#undef IFFLAG 2575 return buf; 2576} 2577 2578void 2579krtread(int again) 2580{ 2581 int mib[6]; 2582 size_t msize; 2583 char *buf, *p, *lim; 2584 struct rt_msghdr *rtm; 2585 int retry; 2586 const char *errmsg; 2587 2588 retry = 0; 2589 buf = NULL; 2590 mib[0] = CTL_NET; 2591 mib[1] = PF_ROUTE; 2592 mib[2] = 0; 2593 mib[3] = AF_INET6; /* Address family */ 2594 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */ 2595 mib[5] = 0; /* No flags */ 2596 do { 2597 if (retry) 2598 sleep(1); 2599 retry++; 2600 errmsg = NULL; 2601 if (buf) { 2602 free(buf); 2603 buf = NULL; 2604 } 2605 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) { 2606 errmsg = "sysctl estimate"; 2607 continue; 2608 } 2609 if ((buf = malloc(msize)) == NULL) { 2610 errmsg = "malloc"; 2611 continue; 2612 } 2613 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) { 2614 errmsg = "sysctl NET_RT_DUMP"; 2615 continue; 2616 } 2617 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL); 2618 if (errmsg) { 2619 fatal("%s (with %d retries, msize=%lu)", errmsg, retry, 2620 (u_long)msize); 2621 /*NOTREACHED*/ 2622 } else if (1 < retry) 2623 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry); 2624 2625 lim = buf + msize; 2626 for (p = buf; p < lim; p += rtm->rtm_msglen) { 2627 rtm = (struct rt_msghdr *)p; 2628 rt_entry(rtm, again); 2629 } 2630 free(buf); 2631} 2632 2633void 2634rt_entry(struct rt_msghdr *rtm, int again) 2635{ 2636 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask; 2637 struct sockaddr_in6 *sin6_genmask, *sin6_ifp; 2638 char *rtmp, *ifname = NULL; 2639 struct riprt *rrt, *orrt; 2640 struct netinfo6 *np; 2641 int ifindex; 2642 2643 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0; 2644 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags & 2645 (RTF_XRESOLVE|RTF_BLACKHOLE)) { 2646 return; /* not interested in the link route */ 2647 } 2648 /* do not look at cloned routes */ 2649#ifdef RTF_WASCLONED 2650 if (rtm->rtm_flags & RTF_WASCLONED) 2651 return; 2652#endif 2653#ifdef RTF_CLONED 2654 if (rtm->rtm_flags & RTF_CLONED) 2655 return; 2656#endif 2657 /* XXX: Ignore connected routes. */ 2658 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC))) 2659 return; 2660 /* 2661 * do not look at dynamic routes. 2662 * netbsd/openbsd cloned routes have UGHD. 2663 */ 2664 if (rtm->rtm_flags & RTF_DYNAMIC) 2665 return; 2666 rtmp = (char *)(rtm + 1); 2667 /* Destination */ 2668 if ((rtm->rtm_addrs & RTA_DST) == 0) 2669 return; /* ignore routes without destination address */ 2670 sin6_dst = (struct sockaddr_in6 *)rtmp; 2671 rtmp += ROUNDUP(sin6_dst->sin6_len); 2672 if (rtm->rtm_addrs & RTA_GATEWAY) { 2673 sin6_gw = (struct sockaddr_in6 *)rtmp; 2674 rtmp += ROUNDUP(sin6_gw->sin6_len); 2675 } 2676 if (rtm->rtm_addrs & RTA_NETMASK) { 2677 sin6_mask = (struct sockaddr_in6 *)rtmp; 2678 rtmp += ROUNDUP(sin6_mask->sin6_len); 2679 } 2680 if (rtm->rtm_addrs & RTA_GENMASK) { 2681 sin6_genmask = (struct sockaddr_in6 *)rtmp; 2682 rtmp += ROUNDUP(sin6_genmask->sin6_len); 2683 } 2684 if (rtm->rtm_addrs & RTA_IFP) { 2685 sin6_ifp = (struct sockaddr_in6 *)rtmp; 2686 rtmp += ROUNDUP(sin6_ifp->sin6_len); 2687 } 2688 2689 /* Destination */ 2690 if (sin6_dst->sin6_family != AF_INET6) 2691 return; 2692 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr)) 2693 return; /* Link-local */ 2694 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback)) 2695 return; /* Loopback */ 2696 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr)) 2697 return; 2698 2699 if ((rrt = MALLOC(struct riprt)) == NULL) { 2700 fatal("malloc: struct riprt"); 2701 /*NOTREACHED*/ 2702 } 2703 memset(rrt, 0, sizeof(*rrt)); 2704 np = &rrt->rrt_info; 2705 rrt->rrt_same = NULL; 2706 rrt->rrt_t = time(NULL); 2707 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC)) 2708 rrt->rrt_t = 0; /* Don't age static routes */ 2709 if (rtm->rtm_flags & Pflag) 2710 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */ 2711 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST) 2712 rrt->rrt_t = 0; /* Don't age non-gateway host routes */ 2713 np->rip6_tag = 0; 2714 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount; 2715 if (np->rip6_metric < 1) 2716 np->rip6_metric = 1; 2717 rrt->rrt_flags = rtm->rtm_flags; 2718 np->rip6_dest = sin6_dst->sin6_addr; 2719 2720 /* Mask or plen */ 2721 if (rtm->rtm_flags & RTF_HOST) 2722 np->rip6_plen = 128; /* Host route */ 2723 else if (sin6_mask) 2724 np->rip6_plen = sin6mask2len(sin6_mask); 2725 else 2726 np->rip6_plen = 0; 2727 2728 orrt = rtsearch(np); 2729 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) { 2730 /* Already found */ 2731 if (!again) { 2732 trace(1, "route: %s/%d flags %s: already registered\n", 2733 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2734 rtflags(rtm)); 2735 } 2736 free(rrt); 2737 return; 2738 } 2739 /* Gateway */ 2740 if (!sin6_gw) 2741 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr)); 2742 else { 2743 if (sin6_gw->sin6_family == AF_INET6) 2744 rrt->rrt_gw = sin6_gw->sin6_addr; 2745 else if (sin6_gw->sin6_family == AF_LINK) { 2746 /* XXX in case ppp link? */ 2747 rrt->rrt_gw = in6addr_loopback; 2748 } else 2749 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr)); 2750 } 2751 trace(1, "route: %s/%d flags %s", 2752 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm)); 2753 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw)); 2754 2755 /* Interface */ 2756 ifindex = rtm->rtm_index; 2757 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex]) 2758 ifname = index2ifc[ifindex]->ifc_name; 2759 else { 2760 trace(1, " not configured\n"); 2761 free(rrt); 2762 return; 2763 } 2764 trace(1, " if %s sock %d", ifname, ifindex); 2765 rrt->rrt_index = ifindex; 2766 2767 trace(1, "\n"); 2768 2769 /* Check gateway */ 2770 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) && 2771 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) && 2772 (rrt->rrt_flags & RTF_LOCAL) == 0) { 2773 trace(0, "***** Gateway %s is not a link-local address.\n", 2774 inet6_n2p(&rrt->rrt_gw)); 2775 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n", 2776 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname); 2777 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR; 2778 } 2779 2780 /* Put it to the route list */ 2781 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) { 2782 /* replace route list */ 2783 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next); 2784 TAILQ_REMOVE(&riprt_head, orrt, rrt_next); 2785 2786 trace(1, "route: %s/%d flags %s: replace new route\n", 2787 inet6_n2p(&np->rip6_dest), np->rip6_plen, 2788 rtflags(rtm)); 2789 free(orrt); 2790 } else 2791 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next); 2792} 2793 2794int 2795addroute(struct riprt *rrt, 2796 const struct in6_addr *gw, 2797 struct ifc *ifcp) 2798{ 2799 struct netinfo6 *np; 2800 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ]; 2801 struct rt_msghdr *rtm; 2802 struct sockaddr_in6 *sin6; 2803 int len; 2804 2805 np = &rrt->rrt_info; 2806 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1)); 2807 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2)); 2808 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n", 2809 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1, 2810 np->rip6_metric - 1, buf2); 2811 if (rtlog) 2812 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(), 2813 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1, 2814 np->rip6_metric - 1, buf2); 2815 if (nflag) 2816 return 0; 2817 2818 memset(buf, 0, sizeof(buf)); 2819 rtm = (struct rt_msghdr *)buf; 2820 rtm->rtm_type = RTM_ADD; 2821 rtm->rtm_version = RTM_VERSION; 2822 rtm->rtm_seq = ++seq; 2823 rtm->rtm_pid = pid; 2824 rtm->rtm_flags = rrt->rrt_flags; 2825 rtm->rtm_flags |= Qflag; 2826 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2827 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1; 2828 rtm->rtm_inits = RTV_HOPCOUNT; 2829 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2830 /* Destination */ 2831 sin6->sin6_len = sizeof(struct sockaddr_in6); 2832 sin6->sin6_family = AF_INET6; 2833 sin6->sin6_addr = np->rip6_dest; 2834 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2835 /* Gateway */ 2836 sin6->sin6_len = sizeof(struct sockaddr_in6); 2837 sin6->sin6_family = AF_INET6; 2838 sin6->sin6_addr = *gw; 2839 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 2840 sin6->sin6_scope_id = ifcp->ifc_index; 2841 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2842 /* Netmask */ 2843 sin6->sin6_len = sizeof(struct sockaddr_in6); 2844 sin6->sin6_family = AF_INET6; 2845 sin6->sin6_addr = *(plen2mask(np->rip6_plen)); 2846 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2847 2848 len = (char *)sin6 - (char *)buf; 2849 rtm->rtm_msglen = len; 2850 if (write(rtsock, buf, len) > 0) 2851 return 0; 2852 2853 if (errno == EEXIST) { 2854 trace(0, "ADD: Route already exists %s/%d gw %s\n", 2855 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1); 2856 if (rtlog) 2857 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n", 2858 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1); 2859 } else { 2860 trace(0, "Can not write to rtsock (addroute): %s\n", 2861 strerror(errno)); 2862 if (rtlog) 2863 fprintf(rtlog, "\tCan not write to rtsock: %s\n", 2864 strerror(errno)); 2865 } 2866 return -1; 2867} 2868 2869int 2870delroute(struct netinfo6 *np, struct in6_addr *gw) 2871{ 2872 u_char buf[BUFSIZ], buf2[BUFSIZ]; 2873 struct rt_msghdr *rtm; 2874 struct sockaddr_in6 *sin6; 2875 int len; 2876 2877 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2)); 2878 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest), 2879 np->rip6_plen, buf2); 2880 if (rtlog) 2881 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n", 2882 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2883 if (nflag) 2884 return 0; 2885 2886 memset(buf, 0, sizeof(buf)); 2887 rtm = (struct rt_msghdr *)buf; 2888 rtm->rtm_type = RTM_DELETE; 2889 rtm->rtm_version = RTM_VERSION; 2890 rtm->rtm_seq = ++seq; 2891 rtm->rtm_pid = pid; 2892 rtm->rtm_flags = RTF_UP | RTF_GATEWAY; 2893 rtm->rtm_flags |= Qflag; 2894 if (np->rip6_plen == sizeof(struct in6_addr) * 8) 2895 rtm->rtm_flags |= RTF_HOST; 2896 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2897 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2898 /* Destination */ 2899 sin6->sin6_len = sizeof(struct sockaddr_in6); 2900 sin6->sin6_family = AF_INET6; 2901 sin6->sin6_addr = np->rip6_dest; 2902 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2903 /* Gateway */ 2904 sin6->sin6_len = sizeof(struct sockaddr_in6); 2905 sin6->sin6_family = AF_INET6; 2906 sin6->sin6_addr = *gw; 2907 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2908 /* Netmask */ 2909 sin6->sin6_len = sizeof(struct sockaddr_in6); 2910 sin6->sin6_family = AF_INET6; 2911 sin6->sin6_addr = *(plen2mask(np->rip6_plen)); 2912 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2913 2914 len = (char *)sin6 - (char *)buf; 2915 rtm->rtm_msglen = len; 2916 if (write(rtsock, buf, len) >= 0) 2917 return 0; 2918 2919 if (errno == ESRCH) { 2920 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n", 2921 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2922 if (rtlog) 2923 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n", 2924 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2); 2925 } else { 2926 trace(0, "Can not write to rtsock (delroute): %s\n", 2927 strerror(errno)); 2928 if (rtlog) 2929 fprintf(rtlog, "\tCan not write to rtsock: %s\n", 2930 strerror(errno)); 2931 } 2932 return -1; 2933} 2934 2935struct in6_addr * 2936getroute(struct netinfo6 *np, struct in6_addr *gw) 2937{ 2938 u_char buf[BUFSIZ]; 2939 int myseq; 2940 int len; 2941 struct rt_msghdr *rtm; 2942 struct sockaddr_in6 *sin6; 2943 2944 rtm = (struct rt_msghdr *)buf; 2945 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6); 2946 memset(rtm, 0, len); 2947 rtm->rtm_type = RTM_GET; 2948 rtm->rtm_version = RTM_VERSION; 2949 myseq = ++seq; 2950 rtm->rtm_seq = myseq; 2951 rtm->rtm_addrs = RTA_DST; 2952 rtm->rtm_msglen = len; 2953 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2954 sin6->sin6_len = sizeof(struct sockaddr_in6); 2955 sin6->sin6_family = AF_INET6; 2956 sin6->sin6_addr = np->rip6_dest; 2957 if (write(rtsock, buf, len) < 0) { 2958 if (errno == ESRCH) /* No such route found */ 2959 return NULL; 2960 perror("write to rtsock"); 2961 exit(1); 2962 } 2963 do { 2964 if ((len = read(rtsock, buf, sizeof(buf))) < 0) { 2965 perror("read from rtsock"); 2966 exit(1); 2967 } 2968 rtm = (struct rt_msghdr *)buf; 2969 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid); 2970 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)]; 2971 if (rtm->rtm_addrs & RTA_DST) { 2972 sin6 = (struct sockaddr_in6 *) 2973 ((char *)sin6 + ROUNDUP(sin6->sin6_len)); 2974 } 2975 if (rtm->rtm_addrs & RTA_GATEWAY) { 2976 *gw = sin6->sin6_addr; 2977 return gw; 2978 } 2979 return NULL; 2980} 2981 2982const char * 2983inet6_n2p(const struct in6_addr *p) 2984{ 2985 static char buf[BUFSIZ]; 2986 2987 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf)); 2988} 2989 2990void 2991ifrtdump(int sig) 2992{ 2993 2994 ifdump(sig); 2995 rtdump(sig); 2996} 2997 2998void 2999ifdump(int sig) 3000{ 3001 struct ifc *ifcp; 3002 FILE *dump; 3003 int nifc = 0; 3004 3005 if (sig == 0) 3006 dump = stderr; 3007 else 3008 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL) 3009 dump = stderr; 3010 3011 fprintf(dump, "%s: Interface Table Dump\n", hms()); 3012 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) 3013 nifc++; 3014 fprintf(dump, " Number of interfaces: %d\n", nifc); 3015 3016 fprintf(dump, " advertising interfaces:\n"); 3017 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) { 3018 if ((ifcp->ifc_flags & IFF_UP) == 0) 3019 continue; 3020 if (iff_find(ifcp, IFIL_TYPE_N) != NULL) 3021 continue; 3022 ifdump0(dump, ifcp); 3023 } 3024 fprintf(dump, "\n"); 3025 fprintf(dump, " non-advertising interfaces:\n"); 3026 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) { 3027 if ((ifcp->ifc_flags & IFF_UP) && 3028 (iff_find(ifcp, IFIL_TYPE_N) == NULL)) 3029 continue; 3030 ifdump0(dump, ifcp); 3031 } 3032 fprintf(dump, "\n"); 3033 if (dump != stderr) 3034 fclose(dump); 3035} 3036 3037void 3038ifdump0(FILE *dump, const struct ifc *ifcp) 3039{ 3040 struct ifac *ifac; 3041 struct iff *iffp; 3042 char buf[BUFSIZ]; 3043 const char *ft; 3044 int addr; 3045 3046 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n", 3047 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags), 3048 inet6_n2p(&ifcp->ifc_mylladdr), 3049 ifcp->ifc_mtu, ifcp->ifc_metric); 3050 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) { 3051 if (ifcp->ifc_flags & IFF_POINTOPOINT) { 3052 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, 3053 buf, sizeof(buf)); 3054 fprintf(dump, "\t%s/%d -- %s\n", 3055 inet6_n2p(&ifac->ifac_addr), 3056 ifac->ifac_plen, buf); 3057 } else { 3058 fprintf(dump, "\t%s/%d\n", 3059 inet6_n2p(&ifac->ifac_addr), 3060 ifac->ifac_plen); 3061 } 3062 } 3063 3064 fprintf(dump, "\tFilter:\n"); 3065 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) { 3066 addr = 0; 3067 switch (iffp->iff_type) { 3068 case IFIL_TYPE_A: 3069 ft = "Aggregate"; addr++; break; 3070 case IFIL_TYPE_N: 3071 ft = "No-use"; break; 3072 case IFIL_TYPE_O: 3073 ft = "Advertise-only"; addr++; break; 3074 case IFIL_TYPE_T: 3075 ft = "Default-only"; break; 3076 case IFIL_TYPE_L: 3077 ft = "Listen-only"; addr++; break; 3078 default: 3079 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type); 3080 ft = buf; 3081 addr++; 3082 break; 3083 } 3084 fprintf(dump, "\t\t%s", ft); 3085 if (addr) 3086 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr), 3087 iffp->iff_plen); 3088 fprintf(dump, "\n"); 3089 } 3090 fprintf(dump, "\n"); 3091} 3092 3093void 3094rtdump(int sig) 3095{ 3096 struct riprt *rrt; 3097 char buf[BUFSIZ]; 3098 FILE *dump; 3099 time_t t, age; 3100 3101 if (sig == 0) 3102 dump = stderr; 3103 else 3104 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL) 3105 dump = stderr; 3106 3107 t = time(NULL); 3108 fprintf(dump, "\n%s: Routing Table Dump\n", hms()); 3109 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) { 3110 if (rrt->rrt_t == 0) 3111 age = 0; 3112 else 3113 age = t - rrt->rrt_t; 3114 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest, 3115 buf, sizeof(buf)); 3116 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)", 3117 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index, 3118 index2ifc[rrt->rrt_index]->ifc_name, 3119 inet6_n2p(&rrt->rrt_gw), 3120 rrt->rrt_info.rip6_metric, (long)age); 3121 if (rrt->rrt_info.rip6_tag) { 3122 fprintf(dump, " tag(0x%04x)", 3123 ntohs(rrt->rrt_info.rip6_tag) & 0xffff); 3124 } 3125 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) 3126 fprintf(dump, " NOT-LL"); 3127 if (rrt->rrt_rflags & RRTF_NOADVERTISE) 3128 fprintf(dump, " NO-ADV"); 3129 fprintf(dump, "\n"); 3130 } 3131 fprintf(dump, "\n"); 3132 if (dump != stderr) 3133 fclose(dump); 3134} 3135 3136/* 3137 * Parse the -A (and -O) options and put corresponding filter object to the 3138 * specified interface structures. Each of the -A/O option has the following 3139 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate) 3140 * -O 5f09:c400::/32,ef0,ef1 (only when match) 3141 */ 3142void 3143filterconfig(void) 3144{ 3145 int i; 3146 char *p, *ap, *iflp, *ifname, *ep; 3147 struct iff iff, *iffp; 3148 struct ifc *ifcp; 3149 struct riprt *rrt; 3150#if 0 3151 struct in6_addr gw; 3152#endif 3153 u_long plen; 3154 3155 for (i = 0; i < nfilter; i++) { 3156 ap = filter[i]; 3157 iflp = NULL; 3158 iffp = ⇔ 3159 memset(iffp, 0, sizeof(*iffp)); 3160 if (filtertype[i] == 'N' || filtertype[i] == 'T') { 3161 iflp = ap; 3162 goto ifonly; 3163 } 3164 if ((p = strchr(ap, ',')) != NULL) { 3165 *p++ = '\0'; 3166 iflp = p; 3167 } 3168 if ((p = strchr(ap, '/')) == NULL) { 3169 fatal("no prefixlen specified for '%s'", ap); 3170 /*NOTREACHED*/ 3171 } 3172 *p++ = '\0'; 3173 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) { 3174 fatal("invalid prefix specified for '%s'", ap); 3175 /*NOTREACHED*/ 3176 } 3177 errno = 0; 3178 ep = NULL; 3179 plen = strtoul(p, &ep, 10); 3180 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) { 3181 fatal("invalid prefix length specified for '%s'", ap); 3182 /*NOTREACHED*/ 3183 } 3184 iffp->iff_plen = plen; 3185 applyplen(&iffp->iff_addr, iffp->iff_plen); 3186ifonly: 3187 iffp->iff_type = filtertype[i]; 3188 if (iflp == NULL || *iflp == '\0') { 3189 fatal("no interface specified for '%s'", ap); 3190 /*NOTREACHED*/ 3191 } 3192 /* parse the interface listing portion */ 3193 while (iflp) { 3194 ifname = iflp; 3195 if ((iflp = strchr(iflp, ',')) != NULL) 3196 *iflp++ = '\0'; 3197 3198 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) { 3199 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0) 3200 continue; 3201 3202 iffp = malloc(sizeof(*iffp)); 3203 if (iffp == NULL) { 3204 fatal("malloc of iff"); 3205 /*NOTREACHED*/ 3206 } 3207 memcpy(iffp, &iff, sizeof(*iffp)); 3208#if 0 3209 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname); 3210#endif 3211 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next); 3212 } 3213 } 3214 3215 /* 3216 * -A: aggregate configuration. 3217 */ 3218 if (filtertype[i] != IFIL_TYPE_A) 3219 continue; 3220 /* put the aggregate to the kernel routing table */ 3221 rrt = (struct riprt *)malloc(sizeof(struct riprt)); 3222 if (rrt == NULL) { 3223 fatal("malloc: rrt"); 3224 /*NOTREACHED*/ 3225 } 3226 memset(rrt, 0, sizeof(struct riprt)); 3227 rrt->rrt_info.rip6_dest = iff.iff_addr; 3228 rrt->rrt_info.rip6_plen = iff.iff_plen; 3229 rrt->rrt_info.rip6_metric = 1; 3230 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff); 3231 rrt->rrt_gw = in6addr_loopback; 3232 rrt->rrt_flags = RTF_UP | RTF_REJECT; 3233 rrt->rrt_rflags = RRTF_AGGREGATE; 3234 rrt->rrt_t = 0; 3235 rrt->rrt_index = loopifcp->ifc_index; 3236#if 0 3237 if (getroute(&rrt->rrt_info, &gw)) { 3238#if 0 3239 /* 3240 * When the address has already been registered in the 3241 * kernel routing table, it should be removed 3242 */ 3243 delroute(&rrt->rrt_info, &gw); 3244#else 3245 /* it is safer behavior */ 3246 errno = EINVAL; 3247 fatal("%s/%u already in routing table, " 3248 "cannot aggregate", 3249 inet6_n2p(&rrt->rrt_info.rip6_dest), 3250 rrt->rrt_info.rip6_plen); 3251 /*NOTREACHED*/ 3252#endif 3253 } 3254#endif 3255 /* Put the route to the list */ 3256 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next); 3257 trace(1, "Aggregate: %s/%d for %s\n", 3258 inet6_n2p(&iff.iff_addr), iff.iff_plen, 3259 loopifcp->ifc_name); 3260 /* Add this route to the kernel */ 3261 if (nflag) /* do not modify kernel routing table */ 3262 continue; 3263 addroute(rrt, &in6addr_loopback, loopifcp); 3264 } 3265} 3266 3267/***************** utility functions *****************/ 3268 3269/* 3270 * Returns a pointer to ifac whose address and prefix length matches 3271 * with the address and prefix length specified in the arguments. 3272 */ 3273struct ifac * 3274ifa_match(const struct ifc *ifcp, 3275 const struct in6_addr *ia, 3276 int plen) 3277{ 3278 struct ifac *ifac; 3279 3280 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) { 3281 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) && 3282 ifac->ifac_plen == plen) 3283 break; 3284 } 3285 3286 return (ifac); 3287} 3288 3289/* 3290 * Return a pointer to riprt structure whose address and prefix length 3291 * matches with the address and prefix length found in the argument. 3292 * Note: This is not a rtalloc(). Therefore exact match is necessary. 3293 */ 3294struct riprt * 3295rtsearch(struct netinfo6 *np) 3296{ 3297 struct riprt *rrt; 3298 3299 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) { 3300 if (rrt->rrt_info.rip6_plen == np->rip6_plen && 3301 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest, 3302 &np->rip6_dest)) 3303 break; 3304 } 3305 3306 return (rrt); 3307} 3308 3309int 3310sin6mask2len(const struct sockaddr_in6 *sin6) 3311{ 3312 3313 return mask2len(&sin6->sin6_addr, 3314 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr)); 3315} 3316 3317int 3318mask2len(const struct in6_addr *addr, int lenlim) 3319{ 3320 int i = 0, j; 3321 const u_char *p = (const u_char *)addr; 3322 3323 for (j = 0; j < lenlim; j++, p++) { 3324 if (*p != 0xff) 3325 break; 3326 i += 8; 3327 } 3328 if (j < lenlim) { 3329 switch (*p) { 3330#define MASKLEN(m, l) case m: do { i += l; break; } while (0) 3331 MASKLEN(0xfe, 7); break; 3332 MASKLEN(0xfc, 6); break; 3333 MASKLEN(0xf8, 5); break; 3334 MASKLEN(0xf0, 4); break; 3335 MASKLEN(0xe0, 3); break; 3336 MASKLEN(0xc0, 2); break; 3337 MASKLEN(0x80, 1); break; 3338#undef MASKLEN 3339 } 3340 } 3341 return i; 3342} 3343 3344void 3345applymask(struct in6_addr *addr, struct in6_addr *mask) 3346{ 3347 int i; 3348 u_long *p, *q; 3349 3350 p = (u_long *)addr; q = (u_long *)mask; 3351 for (i = 0; i < 4; i++) 3352 *p++ &= *q++; 3353} 3354 3355static const u_char plent[8] = { 3356 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe 3357}; 3358 3359void 3360applyplen(struct in6_addr *ia, int plen) 3361{ 3362 u_char *p; 3363 int i; 3364 3365 p = ia->s6_addr; 3366 for (i = 0; i < 16; i++) { 3367 if (plen <= 0) 3368 *p = 0; 3369 else if (plen < 8) 3370 *p &= plent[plen]; 3371 p++, plen -= 8; 3372 } 3373} 3374 3375static const int pl2m[9] = { 3376 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff 3377}; 3378 3379struct in6_addr * 3380plen2mask(int n) 3381{ 3382 static struct in6_addr ia; 3383 u_char *p; 3384 int i; 3385 3386 memset(&ia, 0, sizeof(struct in6_addr)); 3387 p = (u_char *)&ia; 3388 for (i = 0; i < 16; i++, p++, n -= 8) { 3389 if (n >= 8) { 3390 *p = 0xff; 3391 continue; 3392 } 3393 *p = pl2m[n]; 3394 break; 3395 } 3396 return &ia; 3397} 3398 3399char * 3400allocopy(char *p) 3401{ 3402 int len = strlen(p) + 1; 3403 char *q = (char *)malloc(len); 3404 3405 if (!q) { 3406 fatal("malloc"); 3407 /*NOTREACHED*/ 3408 } 3409 3410 strlcpy(q, p, len); 3411 return q; 3412} 3413 3414char * 3415hms(void) 3416{ 3417 static char buf[BUFSIZ]; 3418 time_t t; 3419 struct tm *tm; 3420 3421 t = time(NULL); 3422 if ((tm = localtime(&t)) == 0) { 3423 fatal("localtime"); 3424 /*NOTREACHED*/ 3425 } 3426 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, 3427 tm->tm_sec); 3428 return buf; 3429} 3430 3431#define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */ 3432 3433int 3434ripinterval(int timer) 3435{ 3436 double r = rand(); 3437 3438 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5)); 3439 nextalarm = time(NULL) + interval; 3440 return interval; 3441} 3442 3443time_t 3444ripsuptrig(void) 3445{ 3446 time_t t; 3447 3448 double r = rand(); 3449 t = (int)(RIP_TRIG_INT6_MIN + 3450 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX)); 3451 sup_trig_update = time(NULL) + t; 3452 return t; 3453} 3454 3455void 3456#ifdef __STDC__ 3457fatal(const char *fmt, ...) 3458#else 3459fatal(fmt, va_alist) 3460 char *fmt; 3461 va_dcl 3462#endif 3463{ 3464 va_list ap; 3465 char buf[1024]; 3466 3467#ifdef __STDC__ 3468 va_start(ap, fmt); 3469#else 3470 va_start(ap); 3471#endif 3472 vsnprintf(buf, sizeof(buf), fmt, ap); 3473 va_end(ap); 3474 perror(buf); 3475 if (errno) 3476 syslog(LOG_ERR, "%s: %s", buf, strerror(errno)); 3477 else 3478 syslog(LOG_ERR, "%s", buf); 3479 rtdexit(); 3480} 3481 3482void 3483#ifdef __STDC__ 3484tracet(int level, const char *fmt, ...) 3485#else 3486tracet(level, fmt, va_alist) 3487 int level; 3488 char *fmt; 3489 va_dcl 3490#endif 3491{ 3492 va_list ap; 3493 3494 if (level <= dflag) { 3495#ifdef __STDC__ 3496 va_start(ap, fmt); 3497#else 3498 va_start(ap); 3499#endif 3500 fprintf(stderr, "%s: ", hms()); 3501 vfprintf(stderr, fmt, ap); 3502 va_end(ap); 3503 } 3504 if (dflag) { 3505#ifdef __STDC__ 3506 va_start(ap, fmt); 3507#else 3508 va_start(ap); 3509#endif 3510 if (level > 0) 3511 vsyslog(LOG_DEBUG, fmt, ap); 3512 else 3513 vsyslog(LOG_WARNING, fmt, ap); 3514 va_end(ap); 3515 } 3516} 3517 3518void 3519#ifdef __STDC__ 3520trace(int level, const char *fmt, ...) 3521#else 3522trace(level, fmt, va_alist) 3523 int level; 3524 char *fmt; 3525 va_dcl 3526#endif 3527{ 3528 va_list ap; 3529 3530 if (level <= dflag) { 3531#ifdef __STDC__ 3532 va_start(ap, fmt); 3533#else 3534 va_start(ap); 3535#endif 3536 vfprintf(stderr, fmt, ap); 3537 va_end(ap); 3538 } 3539 if (dflag) { 3540#ifdef __STDC__ 3541 va_start(ap, fmt); 3542#else 3543 va_start(ap); 3544#endif 3545 if (level > 0) 3546 vsyslog(LOG_DEBUG, fmt, ap); 3547 else 3548 vsyslog(LOG_WARNING, fmt, ap); 3549 va_end(ap); 3550 } 3551} 3552 3553unsigned int 3554if_maxindex(void) 3555{ 3556 struct if_nameindex *p, *p0; 3557 unsigned int max = 0; 3558 3559 p0 = if_nameindex(); 3560 for (p = p0; p && p->if_index && p->if_name; p++) { 3561 if (max < p->if_index) 3562 max = p->if_index; 3563 } 3564 if_freenameindex(p0); 3565 return max; 3566} 3567 3568struct ifc * 3569ifc_find(char *name) 3570{ 3571 struct ifc *ifcp; 3572 3573 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) { 3574 if (strcmp(name, ifcp->ifc_name) == 0) 3575 break; 3576 } 3577 return (ifcp); 3578} 3579 3580struct iff * 3581iff_find(struct ifc *ifcp, int type) 3582{ 3583 struct iff *iffp; 3584 3585 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) { 3586 if (type == IFIL_TYPE_ANY || 3587 type == iffp->iff_type) 3588 break; 3589 } 3590 3591 return (iffp); 3592} 3593 3594void 3595setindex2ifc(int idx, struct ifc *ifcp) 3596{ 3597 int n, nsize; 3598 struct ifc **p; 3599 3600 if (!index2ifc) { 3601 nindex2ifc = 5; /*initial guess*/ 3602 index2ifc = (struct ifc **) 3603 malloc(sizeof(*index2ifc) * nindex2ifc); 3604 if (index2ifc == NULL) { 3605 fatal("malloc"); 3606 /*NOTREACHED*/ 3607 } 3608 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc); 3609 } 3610 n = nindex2ifc; 3611 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2) 3612 ; 3613 if (n != nsize) { 3614 p = (struct ifc **)realloc(index2ifc, 3615 sizeof(*index2ifc) * nsize); 3616 if (p == NULL) { 3617 fatal("realloc"); 3618 /*NOTREACHED*/ 3619 } 3620 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n)); 3621 index2ifc = p; 3622 nindex2ifc = nsize; 3623 } 3624 index2ifc[idx] = ifcp; 3625} 3626