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