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