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