2 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
34 static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
37 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
43 #include <arpa/inet.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet/ip6.h>
49 #include <netinet/udp.h>
71 #define RT_DUMP_MAXRETRY 15
74 #define INIT_INTERVAL6 6
76 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
79 /* alignment constraint for routing socket */
81 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
82 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
84 struct ifc { /* Configuration of an interface */
85 TAILQ_ENTRY(ifc) ifc_next;
87 char ifc_name[IFNAMSIZ]; /* if name */
88 int ifc_index; /* if index */
89 int ifc_mtu; /* if mtu */
90 int ifc_metric; /* if metric */
91 u_int ifc_flags; /* flags */
92 short ifc_cflags; /* IFC_XXX */
93 struct in6_addr ifc_mylladdr; /* my link-local address */
94 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
95 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
96 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
97 int ifc_joined; /* joined to ff02::9 */
99 static TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
101 struct ifac { /* Adddress associated to an interface */
102 TAILQ_ENTRY(ifac) ifac_next;
104 struct ifc *ifac_ifc; /* back pointer */
105 struct in6_addr ifac_addr; /* address */
106 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
107 int ifac_scope_id; /* scope id */
108 int ifac_plen; /* prefix length */
111 struct iff { /* Filters for an interface */
112 TAILQ_ENTRY(iff) iff_next;
115 struct in6_addr iff_addr;
119 static struct ifc **index2ifc;
120 static unsigned int nindex2ifc;
121 static struct ifc *loopifcp = NULL; /* pointing to loopback */
123 static struct pollfd set[2];
125 static fd_set *sockvecp; /* vector to select() for receiving */
126 static fd_set *recvecp;
128 static int maxfd; /* maximum fd for select() */
130 static int rtsock; /* the routing socket */
131 static int ripsock; /* socket to send/receive RIP datagram */
133 static struct rip6 *ripbuf; /* packet buffer for sending */
136 * Maintain the routes in a linked list. When the number of the routes
137 * grows, somebody would like to introduce a hash based or a radix tree
138 * based structure. I believe the number of routes handled by RIP is
139 * limited and I don't have to manage a complex data structure, however.
141 * One of the major drawbacks of the linear linked list is the difficulty
142 * of representing the relationship between a couple of routes. This may
143 * be a significant problem when we have to support route aggregation with
144 * suppressing the specifics covered by the aggregate.
148 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
150 struct riprt *rrt_same; /* same destination - future use */
151 struct netinfo6 rrt_info; /* network info */
152 struct in6_addr rrt_gw; /* gateway */
153 u_long rrt_flags; /* kernel routing table flags */
154 u_long rrt_rflags; /* route6d routing table flags */
155 time_t rrt_t; /* when the route validated */
156 int rrt_index; /* ifindex from which this route got */
158 static TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
160 static int dflag = 0; /* debug flag */
161 static int qflag = 0; /* quiet flag */
162 static int nflag = 0; /* don't update kernel routing table */
163 static int aflag = 0; /* age out even the statically defined routes */
164 static int hflag = 0; /* don't split horizon */
165 static int lflag = 0; /* exchange site local routes */
166 static int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
167 static int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
168 static int sflag = 0; /* announce static routes w/ split horizon */
169 static int Sflag = 0; /* announce static routes to every interface */
170 static unsigned long routetag = 0; /* route tag attached on originating case */
172 static char *filter[MAXFILTER];
173 static int filtertype[MAXFILTER];
174 static int nfilter = 0;
178 static struct sockaddr_storage ripsin;
180 static int interval = 1;
181 static time_t nextalarm = 0;
183 static time_t sup_trig_update = 0;
186 static FILE *rtlog = NULL;
188 static int logopened = 0;
192 static volatile sig_atomic_t seenalrm;
193 static volatile sig_atomic_t seenquit;
194 static volatile sig_atomic_t seenusr1;
196 #define RRTF_AGGREGATE 0x08000000
197 #define RRTF_NOADVERTISE 0x10000000
198 #define RRTF_NH_NOT_LLADDR 0x20000000
199 #define RRTF_SENDANYWAY 0x40000000
200 #define RRTF_CHANGED 0x80000000
202 static void sighandler(int);
203 static void ripalarm(void);
204 static void riprecv(void);
205 static void ripsend(struct ifc *, struct sockaddr_in6 *, int);
206 static int out_filter(struct riprt *, struct ifc *);
207 static void init(void);
208 static void ifconfig(void);
209 static int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
210 static void rtrecv(void);
211 static int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
212 const struct sockaddr_in6 *);
213 static int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
214 const struct sockaddr_in6 *);
215 static void filterconfig(void);
216 static int getifmtu(int);
217 static const char *rttypes(struct rt_msghdr *);
218 static const char *rtflags(struct rt_msghdr *);
219 static const char *ifflags(int);
220 static int ifrt(struct ifc *, int);
221 static void ifrt_p2p(struct ifc *, int);
222 static void applyplen(struct in6_addr *, int);
223 static void ifrtdump(int);
224 static void ifdump(int);
225 static void ifdump0(FILE *, const struct ifc *);
226 static void ifremove(int);
227 static void rtdump(int);
228 static void rt_entry(struct rt_msghdr *, int);
229 static void rtdexit(void);
230 static void riprequest(struct ifc *, struct netinfo6 *, int,
231 struct sockaddr_in6 *);
232 static void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
233 static void sendrequest(struct ifc *);
234 static int sin6mask2len(const struct sockaddr_in6 *);
235 static int mask2len(const struct in6_addr *, int);
236 static int sendpacket(struct sockaddr_in6 *, int);
237 static int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
238 static int delroute(struct netinfo6 *, struct in6_addr *);
240 static struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
242 static void krtread(int);
243 static int tobeadv(struct riprt *, struct ifc *);
244 static char *allocopy(char *);
245 static char *hms(void);
246 static const char *inet6_n2p(const struct in6_addr *);
247 static struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
248 static struct in6_addr *plen2mask(int);
249 static struct riprt *rtsearch(struct netinfo6 *);
250 static int ripinterval(int);
252 static time_t ripsuptrig(void);
254 static void fatal(const char *, ...)
255 __attribute__((__format__(__printf__, 1, 2)));
256 static void trace(int, const char *, ...)
257 __attribute__((__format__(__printf__, 2, 3)));
258 static void tracet(int, const char *, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260 static struct ifc *ifc_find(char *);
261 static struct iff *iff_find(struct ifc *, int);
262 static void setindex2ifc(int, struct ifc *);
264 #define MALLOC(type) ((type *)malloc(sizeof(type)))
266 #define IFIL_TYPE_ANY 0x0
267 #define IFIL_TYPE_A 'A'
268 #define IFIL_TYPE_N 'N'
269 #define IFIL_TYPE_T 'T'
270 #define IFIL_TYPE_O 'O'
271 #define IFIL_TYPE_L 'L'
274 main(int argc, char *argv[])
280 sigset_t mask, omask;
281 const char *pidfile = ROUTE6D_PID;
286 progname = strrchr(*argv, '/');
293 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
300 if (nfilter >= MAXFILTER) {
301 fatal("Exceeds MAXFILTER");
304 filtertype[nfilter] = ch;
305 filter[nfilter++] = allocopy(optarg);
309 routetag = strtoul(optarg, &ep, 0);
310 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
311 fatal("invalid route tag");
320 proto = strtoul(optarg, &ep, 0);
321 if (!ep || *ep != '\0' || 3 < proto) {
322 fatal("invalid P flag");
336 proto = strtoul(optarg, &ep, 0);
337 if (!ep || *ep != '\0' || 3 < proto) {
338 fatal("invalid Q flag");
351 if ((rtlog = fopen(optarg, "w")) == NULL) {
352 fatal("Can not write to routelog");
356 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
357 FLAG('a', aflag, 1); break;
358 FLAG('d', dflag, 1); break;
359 FLAG('D', dflag, 2); break;
360 FLAG('h', hflag, 1); break;
361 FLAG('l', lflag, 1); break;
362 FLAG('n', nflag, 1); break;
363 FLAG('q', qflag, 1); break;
364 FLAG('s', sflag, 1); break;
365 FLAG('S', Sflag, 1); break;
368 fatal("Invalid option specified, terminating");
375 fatal("bogus extra arguments");
381 fprintf(stderr, "No kernel update is allowed\n");
385 if (daemon(0, 0) < 0) {
391 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
394 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
396 memset(ripbuf, 0, RIP6_MAXMTU);
397 ripbuf->rip6_cmd = RIP6_RESPONSE;
398 ripbuf->rip6_vers = RIP6_VERSION;
399 ripbuf->rip6_res1[0] = 0;
400 ripbuf->rip6_res1[1] = 0;
404 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
405 if (ifcp->ifc_index < 0) {
406 fprintf(stderr, "No ifindex found at %s "
407 "(no link-local address?)\n", ifcp->ifc_name);
413 if (loopifcp == NULL) {
414 fatal("No loopback found");
417 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
426 if ((pidfh = fopen(pidfile, "w")) != NULL) {
427 fprintf(pidfh, "%d\n", pid);
431 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
435 memset(ripbuf, 0, RIP6_MAXMTU);
436 ripbuf->rip6_cmd = RIP6_RESPONSE;
437 ripbuf->rip6_vers = RIP6_VERSION;
438 ripbuf->rip6_res1[0] = 0;
439 ripbuf->rip6_res1[1] = 0;
441 if (signal(SIGALRM, sighandler) == SIG_ERR ||
442 signal(SIGQUIT, sighandler) == SIG_ERR ||
443 signal(SIGTERM, sighandler) == SIG_ERR ||
444 signal(SIGUSR1, sighandler) == SIG_ERR ||
445 signal(SIGHUP, sighandler) == SIG_ERR ||
446 signal(SIGINT, sighandler) == SIG_ERR) {
451 * To avoid rip packet congestion (not on a cable but in this
452 * process), wait for a moment to send the first RIP6_RESPONSE
455 alarm(ripinterval(INIT_INTERVAL6));
457 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
458 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
460 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
464 syslog(LOG_INFO, "**** Started ****");
466 sigaddset(&mask, SIGALRM);
485 switch (poll(set, 2, INFTIM))
487 memcpy(recvecp, sockvecp, fdmasks);
488 switch (select(maxfd + 1, recvecp, 0, 0, 0))
492 if (errno != EINTR) {
501 if (set[0].revents & POLLIN)
503 if (FD_ISSET(ripsock, recvecp))
506 sigprocmask(SIG_BLOCK, &mask, &omask);
508 sigprocmask(SIG_SETMASK, &omask, NULL);
511 if (set[1].revents & POLLIN)
513 if (FD_ISSET(rtsock, recvecp))
516 sigprocmask(SIG_BLOCK, &mask, &omask);
518 sigprocmask(SIG_SETMASK, &omask, NULL);
525 sighandler(int signo)
545 * gracefully exits after resetting sockopts.
554 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
555 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
556 delroute(&rrt->rrt_info, &rrt->rrt_gw);
561 syslog(LOG_INFO, "**** Terminated ****");
567 * Called periodically:
568 * 1. age out the learned route. remove it if necessary.
569 * 2. submit RIP6_RESPONSE packets.
570 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
571 * to invoke this function in every 1 or 5 or 10 seconds only to age the
572 * routes more precisely.
579 struct riprt *rrt, *rrt_tmp;
580 time_t t_lifetime, t_holddown;
582 /* age the RIP routes */
583 t_lifetime = time(NULL) - RIP_LIFETIME;
584 t_holddown = t_lifetime - RIP_HOLDDOWN;
585 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
588 else if (rrt->rrt_t < t_holddown) {
589 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
590 delroute(&rrt->rrt_info, &rrt->rrt_gw);
592 } else if (rrt->rrt_t < t_lifetime)
593 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
596 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
597 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
598 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
600 alarm(ripinterval(SUPPLY_INTERVAL6));
607 const int int0 = 0, int1 = 1, int255 = 255;
608 struct addrinfo hints, *res;
609 char port[NI_MAXSERV];
611 TAILQ_INIT(&ifc_head);
612 nindex2ifc = 0; /*initial guess*/
614 snprintf(port, sizeof(port), "%u", RIP6_PORT);
616 memset(&hints, 0, sizeof(hints));
617 hints.ai_family = PF_INET6;
618 hints.ai_socktype = SOCK_DGRAM;
619 hints.ai_protocol = IPPROTO_UDP;
620 hints.ai_flags = AI_PASSIVE;
621 error = getaddrinfo(NULL, port, &hints, &res);
623 fatal("%s", gai_strerror(error));
627 fatal(":: resolved to multiple address");
631 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
637 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
638 &int1, sizeof(int1)) < 0) {
639 fatal("rip IPV6_V6ONLY");
643 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
647 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
648 &int255, sizeof(int255)) < 0) {
649 fatal("rip IPV6_MULTICAST_HOPS");
652 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
653 &int0, sizeof(int0)) < 0) {
654 fatal("rip IPV6_MULTICAST_LOOP");
658 #ifdef IPV6_RECVPKTINFO
659 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
660 &int1, sizeof(int1)) < 0) {
661 fatal("rip IPV6_RECVPKTINFO");
664 #else /* old adv. API */
665 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
666 &int1, sizeof(int1)) < 0) {
667 fatal("rip IPV6_PKTINFO");
672 #ifdef IPV6_RECVPKTINFO
673 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
674 &int1, sizeof(int1)) < 0) {
675 fatal("rip IPV6_RECVHOPLIMIT");
678 #else /* old adv. API */
679 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
680 &int1, sizeof(int1)) < 0) {
681 fatal("rip IPV6_HOPLIMIT");
687 memset(&hints, 0, sizeof(hints));
688 hints.ai_family = PF_INET6;
689 hints.ai_socktype = SOCK_DGRAM;
690 hints.ai_protocol = IPPROTO_UDP;
691 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
693 fatal("%s", gai_strerror(error));
697 fatal("%s resolved to multiple address", RIP6_DEST);
700 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
705 set[0].events = POLLIN;
711 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
712 fatal("route socket");
717 set[1].events = POLLIN;
726 rtsock = -1; /*just for safety */
731 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
732 if ((sockvecp = malloc(fdmasks)) == NULL) {
736 if ((recvecp = malloc(fdmasks)) == NULL) {
740 memset(sockvecp, 0, fdmasks);
741 FD_SET(ripsock, sockvecp);
743 FD_SET(rtsock, sockvecp);
748 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
751 * ripflush flushes the rip datagram stored in the rip buffer
754 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
760 tracet(1, "Send(%s): info(%d) to %s.%d\n",
762 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
764 tracet(1, "Send: info(%d) to %s.%d\n",
765 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
767 np = ripbuf->rip6_nets;
768 for (i = 0; i < nrt; i++, np++) {
769 if (np->rip6_metric == NEXTHOP_METRIC) {
770 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
771 trace(2, " NextHop reset");
773 trace(2, " NextHop %s",
774 inet6_n2p(&np->rip6_dest));
777 trace(2, " %s/%d[%d]",
778 inet6_n2p(&np->rip6_dest),
779 np->rip6_plen, np->rip6_metric);
782 trace(2, " tag=0x%04x",
783 ntohs(np->rip6_tag) & 0xffff);
788 error = sendpacket(sin6, RIPSIZE(nrt));
789 if (error == EAFNOSUPPORT) {
790 /* Protocol not supported */
792 tracet(1, "Could not send info to %s (%s): "
795 inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
796 /* As if down for AF_INET6 */
797 ifcp->ifc_flags &= ~IFF_UP;
799 tracet(1, "Could not send info to %s\n",
800 inet6_n2p(&sin6->sin6_addr));
806 * Generate RIP6_RESPONSE packets and send them.
809 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
812 struct in6_addr *nh; /* next hop */
822 * Request from non-link local address is not
823 * a regular route6d update.
825 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
826 sizeof(struct udphdr) -
827 sizeof(struct rip6) + sizeof(struct netinfo6)) /
828 sizeof(struct netinfo6);
831 np = ripbuf->rip6_nets;
832 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
833 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
835 /* Put the route to the buffer */
839 ripflush(NULL, sin6, nrt, np);
842 np = ripbuf->rip6_nets;
845 if (nrt) /* Send last packet */
846 ripflush(NULL, sin6, nrt, np);
850 if ((flag & RRTF_SENDANYWAY) == 0 &&
851 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
855 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
858 /* -T: generate default route only */
859 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
860 struct netinfo6 rrt_info;
861 memset(&rrt_info, 0, sizeof(struct netinfo6));
862 rrt_info.rip6_dest = in6addr_any;
863 rrt_info.rip6_plen = 0;
864 rrt_info.rip6_metric = 1;
865 rrt_info.rip6_metric += ifcp->ifc_metric;
866 rrt_info.rip6_tag = htons(routetag & 0xffff);
867 np = ripbuf->rip6_nets;
870 ripflush(ifcp, sin6, nrt, np);
874 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
875 sizeof(struct udphdr) -
876 sizeof(struct rip6) + sizeof(struct netinfo6)) /
877 sizeof(struct netinfo6);
879 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
880 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
881 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
884 /* Need to check filter here */
885 if (out_filter(rrt, ifcp) == 0)
888 /* Check split horizon and other conditions */
889 if (tobeadv(rrt, ifcp) == 0)
892 /* Only considers the routes with flag if specified */
893 if ((flag & RRTF_CHANGED) &&
894 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
898 if (rrt->rrt_index == ifcp->ifc_index &&
899 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
900 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
901 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
902 if (nrt == maxrte - 2) {
903 ripflush(ifcp, sin6, nrt, np);
906 np = ripbuf->rip6_nets;
909 np->rip6_dest = rrt->rrt_gw;
912 np->rip6_metric = NEXTHOP_METRIC;
916 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
917 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
918 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
920 if (nrt == maxrte - 2) {
921 ripflush(ifcp, sin6, nrt, np);
924 np = ripbuf->rip6_nets;
926 memset(np, 0, sizeof(struct netinfo6));
927 np->rip6_metric = NEXTHOP_METRIC;
932 /* Put the route to the buffer */
936 ripflush(ifcp, sin6, nrt, np);
939 np = ripbuf->rip6_nets;
942 if (nrt) /* Send last packet */
943 ripflush(ifcp, sin6, nrt, np);
947 * outbound filter logic, per-route/interface.
950 out_filter(struct riprt *rrt, struct ifc *ifcp)
957 * -A: filter out less specific routes, if we have aggregated
960 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
961 if (iffp->iff_type != 'A')
963 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
965 ia = rrt->rrt_info.rip6_dest;
966 applyplen(&ia, iffp->iff_plen);
967 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
972 * if it is an aggregated route, advertise it only to the
973 * interfaces specified on -A.
975 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
977 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
978 if (iffp->iff_type != 'A')
980 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
981 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
992 * -O: advertise only if prefix matches the configured prefix.
994 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
996 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
997 if (iffp->iff_type != 'O')
999 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1001 ia = rrt->rrt_info.rip6_dest;
1002 applyplen(&ia, iffp->iff_plen);
1003 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1012 /* the prefix should be advertised */
1017 * Determine if the route is to be advertised on the specified interface.
1018 * It checks options specified in the arguments and the split horizon rule.
1021 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1024 /* Special care for static routes */
1025 if (rrt->rrt_flags & RTF_STATIC) {
1026 /* XXX don't advertise reject/blackhole routes */
1027 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1030 if (Sflag) /* Yes, advertise it anyway */
1032 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1036 /* Regular split horizon */
1037 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1043 * Send a rip packet actually.
1046 sendpacket(struct sockaddr_in6 *sin6, int len)
1050 struct iovec iov[2];
1051 struct in6_pktinfo *pi;
1052 u_char cmsgbuf[256];
1054 struct sockaddr_in6 sincopy;
1056 /* do not overwrite the given sin */
1060 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1061 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1062 idx = sin6->sin6_scope_id;
1066 m.msg_name = (caddr_t)sin6;
1067 m.msg_namelen = sizeof(*sin6);
1068 iov[0].iov_base = (caddr_t)ripbuf;
1069 iov[0].iov_len = len;
1074 m.msg_control = NULL;
1075 m.msg_controllen = 0;
1077 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1078 cm = (struct cmsghdr *)(void *)cmsgbuf;
1079 m.msg_control = (caddr_t)cm;
1080 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1082 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1083 cm->cmsg_level = IPPROTO_IPV6;
1084 cm->cmsg_type = IPV6_PKTINFO;
1085 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1086 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1087 pi->ipi6_ifindex = idx;
1090 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1091 trace(1, "sendmsg: %s\n", strerror(errno));
1099 * Receive and process RIP packets. Update the routes/kernel forwarding
1100 * table if necessary.
1105 struct ifc *ifcp, *ic;
1106 struct sockaddr_in6 fsock;
1107 struct in6_addr nh; /* next hop */
1109 struct netinfo6 *np, *nq;
1112 unsigned int need_trigger, idx;
1113 char buf[4 * RIP6_MAXMTU];
1117 struct iovec iov[2];
1118 u_char cmsgbuf[256];
1119 struct in6_pktinfo *pi = NULL;
1124 time_t t_half_lifetime;
1128 m.msg_name = (caddr_t)&fsock;
1129 m.msg_namelen = sizeof(fsock);
1130 iov[0].iov_base = (caddr_t)buf;
1131 iov[0].iov_len = sizeof(buf);
1134 cm = (struct cmsghdr *)(void *)cmsgbuf;
1135 m.msg_control = (caddr_t)cm;
1136 m.msg_controllen = sizeof(cmsgbuf);
1138 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1143 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1145 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1146 if (cm->cmsg_level != IPPROTO_IPV6)
1148 switch (cm->cmsg_type) {
1150 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1152 "invalid cmsg length for IPV6_PKTINFO\n");
1155 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1156 idx = pi->ipi6_ifindex;
1159 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1161 "invalid cmsg length for IPV6_HOPLIMIT\n");
1164 hlimp = (int *)(void *)CMSG_DATA(cm);
1169 if ((size_t)len < sizeof(struct rip6)) {
1170 trace(1, "Packet too short\n");
1174 if (pi == NULL || hlimp == NULL) {
1176 * This can happen when the kernel failed to allocate memory
1177 * for the ancillary data. Although we might be able to handle
1178 * some cases without this info, those are minor and not so
1179 * important, so it's better to discard the packet for safer
1182 trace(1, "IPv6 packet information cannot be retrieved\n");
1186 nh = fsock.sin6_addr;
1187 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1188 sizeof(struct netinfo6);
1189 rp = (struct rip6 *)(void *)buf;
1192 if (rp->rip6_vers != RIP6_VERSION) {
1193 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1196 if (rp->rip6_cmd == RIP6_REQUEST) {
1197 if (idx && idx < nindex2ifc) {
1198 ifcp = index2ifc[idx];
1199 riprequest(ifcp, np, nn, &fsock);
1201 riprequest(NULL, np, nn, &fsock);
1206 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1207 trace(1, "Response from non-ll addr: %s\n",
1208 inet6_n2p(&fsock.sin6_addr));
1209 return; /* Ignore packets from non-link-local addr */
1211 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1212 trace(1, "Response from non-rip port from %s\n",
1213 inet6_n2p(&fsock.sin6_addr));
1216 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1218 "Response packet with a smaller hop limit (%d) from %s\n",
1219 *hlimp, inet6_n2p(&fsock.sin6_addr));
1223 * Further validation: since this program does not send off-link
1224 * requests, an incoming response must always come from an on-link
1225 * node. Although this is normally ensured by the source address
1226 * check above, it may not 100% be safe because there are router
1227 * implementations that (invalidly) allow a packet with a link-local
1228 * source address to be forwarded to a different link.
1229 * So we also check whether the destination address is a link-local
1230 * address or the hop limit is 255. Note that RFC2080 does not require
1231 * the specific hop limit for a unicast response, so we cannot assume
1234 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1236 "Response packet possibly from an off-link node: "
1237 "from %s to %s hlim=%d\n",
1238 inet6_n2p(&fsock.sin6_addr),
1239 inet6_n2p(&pi->ipi6_addr), *hlimp);
1243 idx = fsock.sin6_scope_id;
1244 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1246 trace(1, "Packets to unknown interface index %d\n", idx);
1247 return; /* Ignore it */
1249 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1250 return; /* The packet is from me; ignore */
1251 if (rp->rip6_cmd != RIP6_RESPONSE) {
1252 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1257 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1260 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1261 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1264 t_half_lifetime = t - (RIP_LIFETIME/2);
1265 for (; nn; nn--, np++) {
1266 if (np->rip6_metric == NEXTHOP_METRIC) {
1267 /* modify neighbor address */
1268 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1270 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1271 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1272 nh = fsock.sin6_addr;
1273 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1275 nh = fsock.sin6_addr;
1276 trace(1, "\tInvalid Nexthop: %s\n",
1277 inet6_n2p(&np->rip6_dest));
1281 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1282 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1283 inet6_n2p(&np->rip6_dest),
1284 np->rip6_plen, np->rip6_metric);
1287 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1288 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1289 inet6_n2p(&np->rip6_dest),
1290 np->rip6_plen, np->rip6_metric);
1293 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1294 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1295 inet6_n2p(&np->rip6_dest),
1296 np->rip6_plen, np->rip6_metric);
1299 /* may need to pass sitelocal prefix in some case, however*/
1300 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1301 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1302 inet6_n2p(&np->rip6_dest),
1303 np->rip6_plen, np->rip6_metric);
1306 trace(2, "\tnetinfo6: %s/%d [%d]",
1307 inet6_n2p(&np->rip6_dest),
1308 np->rip6_plen, np->rip6_metric);
1310 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1313 applyplen(&ia, np->rip6_plen);
1314 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1315 trace(2, " [junk outside prefix]");
1319 * -L: listen only if the prefix matches the configuration
1321 ok = 1; /* if there's no L filter, it is ok */
1322 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1323 if (iffp->iff_type != IFIL_TYPE_L)
1326 if (np->rip6_plen < iffp->iff_plen)
1328 /* special rule: ::/0 means default, not "in /0" */
1329 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1332 applyplen(&ia, iffp->iff_plen);
1333 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1339 trace(2, " (filtered)\n");
1345 np->rip6_metric += ifcp->ifc_metric;
1346 if (np->rip6_metric > HOPCNT_INFINITY6)
1347 np->rip6_metric = HOPCNT_INFINITY6;
1349 applyplen(&np->rip6_dest, np->rip6_plen);
1350 if ((rrt = rtsearch(np)) != NULL) {
1351 if (rrt->rrt_t == 0)
1352 continue; /* Intf route has priority */
1353 nq = &rrt->rrt_info;
1354 if (nq->rip6_metric > np->rip6_metric) {
1355 if (rrt->rrt_index == ifcp->ifc_index &&
1356 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1357 /* Small metric from the same gateway */
1358 nq->rip6_metric = np->rip6_metric;
1360 /* Better route found */
1361 rrt->rrt_index = ifcp->ifc_index;
1362 /* Update routing table */
1363 delroute(nq, &rrt->rrt_gw);
1366 addroute(rrt, &nh, ifcp);
1368 rrt->rrt_rflags |= RRTF_CHANGED;
1371 } else if (nq->rip6_metric < np->rip6_metric &&
1372 rrt->rrt_index == ifcp->ifc_index &&
1373 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1374 /* Got worse route from same gw */
1375 nq->rip6_metric = np->rip6_metric;
1377 rrt->rrt_rflags |= RRTF_CHANGED;
1379 } else if (nq->rip6_metric == np->rip6_metric &&
1380 np->rip6_metric < HOPCNT_INFINITY6) {
1381 if (rrt->rrt_index == ifcp->ifc_index &&
1382 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1383 /* same metric, same route from same gw */
1385 } else if (rrt->rrt_t < t_half_lifetime) {
1386 /* Better route found */
1387 rrt->rrt_index = ifcp->ifc_index;
1388 /* Update routing table */
1389 delroute(nq, &rrt->rrt_gw);
1392 addroute(rrt, &nh, ifcp);
1393 rrt->rrt_rflags |= RRTF_CHANGED;
1398 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1399 * do not update age value. Do nothing.
1401 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1402 /* Got a new valid route */
1403 if ((rrt = MALLOC(struct riprt)) == NULL) {
1404 fatal("malloc: struct riprt");
1407 memset(rrt, 0, sizeof(*rrt));
1408 nq = &rrt->rrt_info;
1410 rrt->rrt_same = NULL;
1411 rrt->rrt_index = ifcp->ifc_index;
1412 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1415 applyplen(&nq->rip6_dest, nq->rip6_plen);
1416 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1417 rrt->rrt_flags |= RTF_HOST;
1419 /* Update routing table */
1420 addroute(rrt, &nh, ifcp);
1421 rrt->rrt_rflags |= RRTF_CHANGED;
1425 /* Put the route to the list */
1426 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1429 /* XXX need to care the interval between triggered updates */
1431 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1432 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1433 if (ifcp->ifc_index == ic->ifc_index)
1435 if (ic->ifc_flags & IFF_UP)
1436 ripsend(ic, &ic->ifc_ripsin,
1440 /* Reset the flag */
1441 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1442 rrt->rrt_rflags &= ~RRTF_CHANGED;
1448 * Send all routes request packet to the specified interface.
1451 sendrequest(struct ifc *ifcp)
1453 struct netinfo6 *np;
1456 if (ifcp->ifc_flags & IFF_LOOPBACK)
1458 ripbuf->rip6_cmd = RIP6_REQUEST;
1459 np = ripbuf->rip6_nets;
1460 memset(np, 0, sizeof(struct netinfo6));
1461 np->rip6_metric = HOPCNT_INFINITY6;
1462 tracet(1, "Send rtdump Request to %s (%s)\n",
1463 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1464 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1465 if (error == EAFNOSUPPORT) {
1466 /* Protocol not supported */
1467 tracet(1, "Could not send rtdump Request to %s (%s): "
1468 "set IFF_UP to 0\n",
1469 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1470 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1472 ripbuf->rip6_cmd = RIP6_RESPONSE;
1476 * Process a RIP6_REQUEST packet.
1479 riprequest(struct ifc *ifcp,
1480 struct netinfo6 *np,
1482 struct sockaddr_in6 *sin6)
1487 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1488 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1489 /* Specific response, don't split-horizon */
1490 trace(1, "\tRIP Request\n");
1491 for (i = 0; i < nn; i++, np++) {
1494 np->rip6_metric = rrt->rrt_info.rip6_metric;
1496 np->rip6_metric = HOPCNT_INFINITY6;
1498 (void)sendpacket(sin6, RIPSIZE(nn));
1501 /* Whole routing table dump */
1502 trace(1, "\tRIP Request -- whole routing table\n");
1503 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1507 * Get information of each interface.
1512 struct ifaddrs *ifap, *ifa;
1514 struct ipv6_mreq mreq;
1517 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1522 if (getifaddrs(&ifap) != 0) {
1523 fatal("getifaddrs");
1527 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1528 if (ifa->ifa_addr->sa_family != AF_INET6)
1530 ifcp = ifc_find(ifa->ifa_name);
1531 /* we are interested in multicast-capable interfaces */
1532 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1536 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1537 fatal("malloc: struct ifc");
1540 memset(ifcp, 0, sizeof(*ifcp));
1542 ifcp->ifc_index = -1;
1543 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1544 sizeof(ifcp->ifc_name));
1545 TAILQ_INIT(&ifcp->ifc_ifac_head);
1546 TAILQ_INIT(&ifcp->ifc_iff_head);
1547 ifcp->ifc_flags = ifa->ifa_flags;
1548 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1549 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1550 ifflags(ifcp->ifc_flags));
1551 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1554 /* update flag, this may be up again */
1555 if (ifcp->ifc_flags != ifa->ifa_flags) {
1556 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1557 ifflags(ifcp->ifc_flags));
1558 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1559 ifcp->ifc_cflags |= IFC_CHANGED;
1561 ifcp->ifc_flags = ifa->ifa_flags;
1563 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1564 /* maybe temporary failure */
1567 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1568 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1569 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1570 mreq.ipv6mr_interface = ifcp->ifc_index;
1571 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1572 &mreq, sizeof(mreq)) < 0) {
1573 fatal("IPV6_JOIN_GROUP");
1576 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1585 ifconfig1(const char *name,
1586 const struct sockaddr *sa,
1590 struct in6_ifreq ifr;
1591 const struct sockaddr_in6 *sin6;
1596 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
1597 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1599 ifr.ifr_addr = *sin6;
1600 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1601 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1602 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1605 plen = sin6mask2len(&ifr.ifr_addr);
1606 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1607 /* same interface found */
1608 /* need check if something changed */
1609 /* XXX not yet implemented */
1613 * New address is found
1615 if ((ifac = MALLOC(struct ifac)) == NULL) {
1616 fatal("malloc: struct ifac");
1619 memset(ifac, 0, sizeof(*ifac));
1621 ifac->ifac_ifc = ifcp;
1622 ifac->ifac_addr = sin6->sin6_addr;
1623 ifac->ifac_plen = plen;
1624 ifac->ifac_scope_id = sin6->sin6_scope_id;
1625 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1626 ifr.ifr_addr = *sin6;
1627 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1628 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1631 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1632 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1634 trace(1, "found address %s/%d -- %s\n",
1635 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1637 trace(1, "found address %s/%d\n",
1638 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1640 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1641 ifcp->ifc_mylladdr = ifac->ifac_addr;
1642 ifcp->ifc_index = ifac->ifac_scope_id;
1643 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1644 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1645 setindex2ifc(ifcp->ifc_index, ifcp);
1646 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1647 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1648 ifcp->ifc_mtu = RIP6_MAXMTU;
1649 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1650 fatal("ioctl: SIOCGIFMETRIC");
1653 ifcp->ifc_metric = ifr.ifr_metric;
1654 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1655 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1657 ifcp->ifc_cflags |= IFC_CHANGED;
1659 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1665 ifremove(int ifindex)
1670 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1671 if (ifcp->ifc_index == ifindex)
1677 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1678 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1680 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1681 if (rrt->rrt_index == ifcp->ifc_index &&
1682 rrt->rrt_rflags & RRTF_AGGREGATE)
1683 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1689 * Receive and process routing messages.
1690 * Update interface information as necesssary.
1697 struct rt_msghdr *rtm;
1698 struct ifa_msghdr *ifam;
1699 struct if_msghdr *ifm;
1700 struct if_announcemsghdr *ifan;
1702 struct ifc *ifcp, *ic;
1703 int iface = 0, rtable = 0;
1704 struct sockaddr_in6 *rta[RTAX_MAX];
1705 struct sockaddr_in6 mask;
1709 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1710 perror("read from rtsock");
1716 if (len < sizeof(*rtm)) {
1717 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1718 len, (u_long)sizeof(*rtm));
1723 fprintf(stderr, "rtmsg:\n");
1724 for (i = 0; i < len; i++) {
1725 fprintf(stderr, "%02x ", buf[i] & 0xff);
1726 if (i % 16 == 15) fprintf(stderr, "\n");
1728 fprintf(stderr, "\n");
1731 for (p = buf; p - buf < len; p +=
1732 ((struct rt_msghdr *)(void *)p)->rtm_msglen) {
1733 if (((struct rt_msghdr *)(void *)p)->rtm_version != RTM_VERSION)
1736 /* safety against bogus message */
1737 if (((struct rt_msghdr *)(void *)p)->rtm_msglen <= 0) {
1738 trace(1, "bogus rtmsg: length=%d\n",
1739 ((struct rt_msghdr *)(void *)p)->rtm_msglen);
1745 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1748 ifam = (struct ifa_msghdr *)(void *)p;
1749 addrs = ifam->ifam_addrs;
1750 q = (char *)(ifam + 1);
1753 ifm = (struct if_msghdr *)(void *)p;
1754 addrs = ifm->ifm_addrs;
1755 q = (char *)(ifm + 1);
1757 case RTM_IFANNOUNCE:
1758 ifan = (struct if_announcemsghdr *)(void *)p;
1759 switch (ifan->ifan_what) {
1763 case IFAN_DEPARTURE:
1764 ifremove(ifan->ifan_index);
1770 rtm = (struct rt_msghdr *)(void *)p;
1771 addrs = rtm->rtm_addrs;
1772 q = (char *)(rtm + 1);
1773 if (rtm->rtm_version != RTM_VERSION) {
1774 trace(1, "unexpected rtmsg version %d "
1776 rtm->rtm_version, RTM_VERSION);
1779 if (rtm->rtm_pid == pid) {
1781 trace(1, "rtmsg looped back to me, ignored\n");
1787 memset(&rta, 0, sizeof(rta));
1788 for (i = 0; i < RTAX_MAX; i++) {
1789 if (addrs & (1 << i)) {
1790 rta[i] = (struct sockaddr_in6 *)(void *)q;
1791 q += ROUNDUP(rta[i]->sin6_len);
1795 trace(1, "rtsock: %s (addrs=%x)\n",
1796 rttypes((struct rt_msghdr *)(void *)p), addrs);
1799 i < ((struct rt_msghdr *)(void *)p)->rtm_msglen;
1801 fprintf(stderr, "%02x ", p[i] & 0xff);
1802 if (i % 16 == 15) fprintf(stderr, "\n");
1804 fprintf(stderr, "\n");
1810 * We may be able to optimize by using ifm->ifm_index or
1811 * ifam->ifam_index. For simplicity we don't do that here.
1813 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1825 /* nothing to be done here */
1826 trace(1, "\tnothing to be done, ignored\n");
1831 if (rta[RTAX_DST] == NULL) {
1832 trace(1, "\tno destination, ignored\n");
1835 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1836 trace(1, "\taf mismatch, ignored\n");
1839 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1840 trace(1, "\tlinklocal destination, ignored\n");
1843 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1844 trace(1, "\tloopback destination, ignored\n");
1845 continue; /* Loopback */
1847 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1848 trace(1, "\tmulticast destination, ignored\n");
1854 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1862 /* should already be handled */
1863 fatal("rtrecv: never reach here");
1866 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1867 trace(1, "\tsome of dst/gw/netamsk are "
1868 "unavailable, ignored\n");
1871 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1872 mask.sin6_len = sizeof(mask);
1873 memset(&mask.sin6_addr, 0xff,
1874 sizeof(mask.sin6_addr));
1875 rta[RTAX_NETMASK] = &mask;
1876 } else if (!rta[RTAX_NETMASK]) {
1877 trace(1, "\tsome of dst/gw/netamsk are "
1878 "unavailable, ignored\n");
1881 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1882 rta[RTAX_NETMASK]) == 0) {
1883 rtable++; /*just to be sure*/
1888 trace(1, "\tnot supported yet, ignored\n");
1891 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1892 trace(1, "\tno netmask or ifa given, ignored\n");
1895 if (ifam->ifam_index < nindex2ifc)
1896 ifcp = index2ifc[ifam->ifam_index];
1900 trace(1, "\tinvalid ifam_index %d, ignored\n",
1904 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1912 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1914 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1915 if (ifcp->ifc_cflags & IFC_CHANGED) {
1916 if (ifrt(ifcp, 1)) {
1917 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1918 if (ifcp->ifc_index == ic->ifc_index)
1920 if (ic->ifc_flags & IFF_UP)
1921 ripsend(ic, &ic->ifc_ripsin,
1924 /* Reset the flag */
1925 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1926 rrt->rrt_rflags &= ~RRTF_CHANGED;
1929 ifcp->ifc_cflags &= ~IFC_CHANGED;
1934 trace(1, "rtsock: read routing table again\n");
1940 * remove specified route from the internal routing table.
1943 rt_del(const struct sockaddr_in6 *sdst,
1944 const struct sockaddr_in6 *sgw,
1945 const struct sockaddr_in6 *smask)
1947 const struct in6_addr *dst = NULL;
1948 const struct in6_addr *gw = NULL;
1950 struct netinfo6 ni6;
1951 struct riprt *rrt = NULL;
1954 if (sdst->sin6_family != AF_INET6) {
1955 trace(1, "\tother AF, ignored\n");
1958 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1959 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1960 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1961 trace(1, "\taddress %s not interesting, ignored\n",
1962 inet6_n2p(&sdst->sin6_addr));
1965 dst = &sdst->sin6_addr;
1966 if (sgw->sin6_family == AF_INET6) {
1968 gw = &sgw->sin6_addr;
1969 prefix = sin6mask2len(smask);
1970 } else if (sgw->sin6_family == AF_LINK) {
1972 * Interface route... a hard case. We need to get the prefix
1973 * length from the kernel, but we now are parsing rtmsg.
1974 * We'll purge matching routes from my list, then get the
1977 struct riprt *longest;
1978 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1981 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1982 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1984 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1986 || longest->rrt_info.rip6_plen <
1987 rrt->rrt_info.rip6_plen) {
1994 trace(1, "\tno matching interface route found\n");
1997 gw = &in6addr_loopback;
1998 prefix = rrt->rrt_info.rip6_plen;
2000 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2004 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2005 trace(1, "gw %s\n", inet6_n2p(gw));
2006 t_lifetime = time(NULL) - RIP_LIFETIME;
2007 /* age route for interface address */
2008 memset(&ni6, 0, sizeof(ni6));
2009 ni6.rip6_dest = *dst;
2010 ni6.rip6_plen = prefix;
2011 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2012 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2014 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2015 trace(1, "\tno route found\n");
2019 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2020 trace(1, "\tyou can delete static routes only\n");
2023 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2024 trace(1, "\tgw mismatch: %s <-> ",
2025 inet6_n2p(&rrt->rrt_gw));
2026 trace(1, "%s\n", inet6_n2p(gw));
2028 trace(1, "\troute found, age it\n");
2029 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2030 rrt->rrt_t = t_lifetime;
2031 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2038 * remove specified address from internal interface/routing table.
2041 rt_deladdr(struct ifc *ifcp,
2042 const struct sockaddr_in6 *sifa,
2043 const struct sockaddr_in6 *smask)
2045 const struct in6_addr *addr = NULL;
2047 struct ifac *ifac = NULL;
2048 struct netinfo6 ni6;
2049 struct riprt *rrt = NULL;
2053 if (sifa->sin6_family != AF_INET6) {
2054 trace(1, "\tother AF, ignored\n");
2057 addr = &sifa->sin6_addr;
2058 prefix = sin6mask2len(smask);
2060 trace(1, "\tdeleting %s/%d from %s\n",
2061 inet6_n2p(addr), prefix, ifcp->ifc_name);
2062 ifac = ifa_match(ifcp, addr, prefix);
2064 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2065 inet6_n2p(addr), prefix, ifcp->ifc_name);
2068 if (ifac->ifac_ifc != ifcp) {
2069 trace(1, "\taddress table corrupt: back pointer does not match "
2071 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2074 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2075 t_lifetime = time(NULL) - RIP_LIFETIME;
2076 /* age route for interface address */
2077 memset(&ni6, 0, sizeof(ni6));
2078 ni6.rip6_dest = ifac->ifac_addr;
2079 ni6.rip6_plen = ifac->ifac_plen;
2080 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2081 trace(1, "\tfind interface route %s/%d on %d\n",
2082 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2083 if ((rrt = rtsearch(&ni6)) != NULL) {
2084 struct in6_addr none;
2085 memset(&none, 0, sizeof(none));
2086 if (rrt->rrt_index == ifcp->ifc_index &&
2087 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2088 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2089 trace(1, "\troute found, age it\n");
2090 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2091 rrt->rrt_t = t_lifetime;
2092 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2096 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2097 inet6_n2p(&rrt->rrt_info.rip6_dest),
2098 rrt->rrt_info.rip6_plen,
2102 trace(1, "\tno interface route found\n");
2103 /* age route for p2p destination */
2104 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2105 memset(&ni6, 0, sizeof(ni6));
2106 ni6.rip6_dest = ifac->ifac_raddr;
2107 ni6.rip6_plen = 128;
2108 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2109 trace(1, "\tfind p2p route %s/%d on %d\n",
2110 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2112 if ((rrt = rtsearch(&ni6)) != NULL) {
2113 if (rrt->rrt_index == ifcp->ifc_index &&
2114 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2115 &ifac->ifac_addr)) {
2116 trace(1, "\troute found, age it\n");
2117 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2118 rrt->rrt_t = t_lifetime;
2119 rrt->rrt_info.rip6_metric =
2124 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2125 inet6_n2p(&rrt->rrt_info.rip6_dest),
2126 rrt->rrt_info.rip6_plen,
2130 trace(1, "\tno p2p route found\n");
2134 return ((updated) ? 0 : -1);
2138 * Get each interface address and put those interface routes to the route
2142 ifrt(struct ifc *ifcp, int again)
2145 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2146 struct netinfo6 *np;
2148 int need_trigger = 0;
2151 if (ifcp->ifc_flags & IFF_LOOPBACK)
2152 return 0; /* ignore loopback */
2155 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2156 ifrt_p2p(ifcp, again);
2160 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2161 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2163 trace(1, "route: %s on %s: "
2164 "skip linklocal interface address\n",
2165 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2169 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2171 trace(1, "route: %s: skip unspec interface address\n",
2176 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2178 trace(1, "route: %s: skip loopback address\n",
2183 if (ifcp->ifc_flags & IFF_UP) {
2184 if ((rrt = MALLOC(struct riprt)) == NULL)
2185 fatal("malloc: struct riprt");
2186 memset(rrt, 0, sizeof(*rrt));
2187 rrt->rrt_same = NULL;
2188 rrt->rrt_index = ifcp->ifc_index;
2189 rrt->rrt_t = 0; /* don't age */
2190 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2191 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2192 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2193 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2194 rrt->rrt_flags = RTF_HOST;
2195 rrt->rrt_rflags |= RRTF_CHANGED;
2196 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2197 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2198 rrt->rrt_gw = ifac->ifac_addr;
2199 np = &rrt->rrt_info;
2200 search_rrt = rtsearch(np);
2201 if (search_rrt != NULL) {
2202 if (search_rrt->rrt_info.rip6_metric <=
2203 rrt->rrt_info.rip6_metric) {
2204 /* Already have better route */
2206 trace(1, "route: %s/%d: "
2207 "already registered (%s)\n",
2208 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2214 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2215 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2217 /* Attach the route to the list */
2218 trace(1, "route: %s/%d: register route (%s)\n",
2219 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2221 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2222 addroute(rrt, &rrt->rrt_gw, ifcp);
2225 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2228 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2229 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2230 t_lifetime = time(NULL) - RIP_LIFETIME;
2231 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2232 loop_rrt->rrt_t = t_lifetime;
2233 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2234 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2244 return need_trigger;
2248 * there are couple of p2p interface routing models. "behavior" lets
2249 * you pick one. it looks that gated behavior fits best with BSDs,
2250 * since BSD kernels do not look at prefix length on p2p interfaces.
2253 ifrt_p2p(struct ifc *ifcp, int again)
2256 struct riprt *rrt, *orrt;
2257 struct netinfo6 *np;
2258 struct in6_addr addr, dest;
2259 int advert, ignore, i;
2260 #define P2PADVERT_NETWORK 1
2261 #define P2PADVERT_ADDR 2
2262 #define P2PADVERT_DEST 4
2263 #define P2PADVERT_MAX 4
2264 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2265 const char *category = "";
2268 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2269 addr = ifac->ifac_addr;
2270 dest = ifac->ifac_raddr;
2271 applyplen(&addr, ifac->ifac_plen);
2272 applyplen(&dest, ifac->ifac_plen);
2273 advert = ignore = 0;
2277 * honor addr/plen, just like normal shared medium
2278 * interface. this may cause trouble if you reuse
2279 * addr/plen on other interfaces.
2281 * advertise addr/plen.
2283 advert |= P2PADVERT_NETWORK;
2287 * prefixlen on p2p interface is meaningless.
2288 * advertise addr/128 and dest/128.
2290 * do not install network route to route6d routing
2291 * table (if we do, it would prevent route installation
2292 * for other p2p interface that shares addr/plen).
2294 * XXX what should we do if dest is ::? it will not
2295 * get announced anyways (see following filter),
2296 * but we need to think.
2298 advert |= P2PADVERT_ADDR;
2299 advert |= P2PADVERT_DEST;
2300 ignore |= P2PADVERT_NETWORK;
2304 * just for testing. actually the code is redundant
2305 * given the current p2p interface address assignment
2306 * rule for kame kernel.
2309 * A/n -> announce A/n
2310 * A B/n, A and B share prefix -> A/n (= B/n)
2311 * A B/n, do not share prefix -> A/128 and B/128
2312 * actually, A/64 and A B/128 are the only cases
2313 * permitted by the kernel:
2315 * A B/128 -> A/128 and B/128
2317 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2318 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2319 advert |= P2PADVERT_NETWORK;
2321 advert |= P2PADVERT_ADDR;
2322 advert |= P2PADVERT_DEST;
2323 ignore |= P2PADVERT_NETWORK;
2326 advert |= P2PADVERT_NETWORK;
2330 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2331 if ((ignore & i) != 0)
2333 if ((rrt = MALLOC(struct riprt)) == NULL) {
2334 fatal("malloc: struct riprt");
2337 memset(rrt, 0, sizeof(*rrt));
2338 rrt->rrt_same = NULL;
2339 rrt->rrt_index = ifcp->ifc_index;
2340 rrt->rrt_t = 0; /* don't age */
2342 case P2PADVERT_NETWORK:
2343 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2344 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2345 applyplen(&rrt->rrt_info.rip6_dest,
2347 category = "network";
2349 case P2PADVERT_ADDR:
2350 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2351 rrt->rrt_info.rip6_plen = 128;
2352 rrt->rrt_gw = in6addr_loopback;
2355 case P2PADVERT_DEST:
2356 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2357 rrt->rrt_info.rip6_plen = 128;
2358 rrt->rrt_gw = ifac->ifac_addr;
2362 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2363 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2365 trace(1, "route: %s: skip unspec/linklocal "
2366 "(%s on %s)\n", category, ifcp->ifc_name);
2371 if ((advert & i) == 0) {
2372 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2376 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2377 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2378 np = &rrt->rrt_info;
2379 orrt = rtsearch(np);
2381 /* Attach the route to the list */
2382 trace(1, "route: %s/%d: register route "
2384 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2385 category, ifcp->ifc_name, noadv);
2386 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2387 } else if (rrt->rrt_index != orrt->rrt_index ||
2388 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2390 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2391 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2394 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2395 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2396 category, ifcp->ifc_name, noadv);
2400 trace(1, "route: %s/%d: "
2401 "already registered (%s on %s%s)\n",
2402 inet6_n2p(&np->rip6_dest),
2403 np->rip6_plen, category,
2404 ifcp->ifc_name, noadv);
2410 #undef P2PADVERT_NETWORK
2411 #undef P2PADVERT_ADDR
2412 #undef P2PADVERT_DEST
2413 #undef P2PADVERT_MAX
2417 getifmtu(int ifindex)
2422 struct if_msghdr *ifm;
2429 mib[4] = NET_RT_IFLIST;
2431 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2432 fatal("sysctl estimate NET_RT_IFLIST");
2435 if ((buf = malloc(msize)) == NULL) {
2439 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2440 fatal("sysctl NET_RT_IFLIST");
2443 ifm = (struct if_msghdr *)(void *)buf;
2444 mtu = ifm->ifm_data.ifi_mtu;
2445 if (ifindex != ifm->ifm_index) {
2446 fatal("ifindex does not match with ifm_index");
2454 rttypes(struct rt_msghdr *rtm)
2456 #define RTTYPE(s, f) \
2458 if (rtm->rtm_type == (f)) \
2461 RTTYPE("ADD", RTM_ADD);
2462 RTTYPE("DELETE", RTM_DELETE);
2463 RTTYPE("CHANGE", RTM_CHANGE);
2464 RTTYPE("GET", RTM_GET);
2465 RTTYPE("LOSING", RTM_LOSING);
2466 RTTYPE("REDIRECT", RTM_REDIRECT);
2467 RTTYPE("MISS", RTM_MISS);
2468 RTTYPE("LOCK", RTM_LOCK);
2469 RTTYPE("NEWADDR", RTM_NEWADDR);
2470 RTTYPE("DELADDR", RTM_DELADDR);
2471 RTTYPE("IFINFO", RTM_IFINFO);
2473 RTTYPE("OIFINFO", RTM_OIFINFO);
2475 #ifdef RTM_IFANNOUNCE
2476 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2479 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2482 RTTYPE("DELMADDR", RTM_DELMADDR);
2489 rtflags(struct rt_msghdr *rtm)
2491 static char buf[BUFSIZ];
2494 * letter conflict should be okay. painful when *BSD diverges...
2496 strlcpy(buf, "", sizeof(buf));
2497 #define RTFLAG(s, f) \
2499 if (rtm->rtm_flags & (f)) \
2500 strlcat(buf, (s), sizeof(buf)); \
2502 RTFLAG("U", RTF_UP);
2503 RTFLAG("G", RTF_GATEWAY);
2504 RTFLAG("H", RTF_HOST);
2505 RTFLAG("R", RTF_REJECT);
2506 RTFLAG("D", RTF_DYNAMIC);
2507 RTFLAG("M", RTF_MODIFIED);
2508 RTFLAG("d", RTF_DONE);
2510 RTFLAG("m", RTF_MASK);
2513 RTFLAG("c", RTF_CLONED);
2515 RTFLAG("X", RTF_XRESOLVE);
2517 RTFLAG("L", RTF_LLINFO);
2519 RTFLAG("S", RTF_STATIC);
2520 RTFLAG("B", RTF_BLACKHOLE);
2522 RTFLAG("3", RTF_PROTO3);
2524 RTFLAG("2", RTF_PROTO2);
2525 RTFLAG("1", RTF_PROTO1);
2526 #ifdef RTF_BROADCAST
2527 RTFLAG("b", RTF_BROADCAST);
2530 RTFLAG("d", RTF_DEFAULT);
2532 #ifdef RTF_ISAROUTER
2533 RTFLAG("r", RTF_ISAROUTER);
2536 RTFLAG("T", RTF_TUNNEL);
2539 RTFLAG("A", RTF_AUTH);
2542 RTFLAG("E", RTF_CRYPT);
2551 static char buf[BUFSIZ];
2553 strlcpy(buf, "", sizeof(buf));
2554 #define IFFLAG(s, f) \
2556 if (flags & (f)) { \
2558 strlcat(buf, ",", sizeof(buf)); \
2559 strlcat(buf, (s), sizeof(buf)); \
2562 IFFLAG("UP", IFF_UP);
2563 IFFLAG("BROADCAST", IFF_BROADCAST);
2564 IFFLAG("DEBUG", IFF_DEBUG);
2565 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2566 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2567 #ifdef IFF_NOTRAILERS
2568 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2570 IFFLAG("RUNNING", IFF_RUNNING);
2571 IFFLAG("NOARP", IFF_NOARP);
2572 IFFLAG("PROMISC", IFF_PROMISC);
2573 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2574 IFFLAG("OACTIVE", IFF_OACTIVE);
2575 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2576 IFFLAG("LINK0", IFF_LINK0);
2577 IFFLAG("LINK1", IFF_LINK1);
2578 IFFLAG("LINK2", IFF_LINK2);
2579 IFFLAG("MULTICAST", IFF_MULTICAST);
2589 char *buf, *p, *lim;
2590 struct rt_msghdr *rtm;
2599 mib[3] = AF_INET6; /* Address family */
2600 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2601 mib[5] = 0; /* No flags */
2611 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2612 errmsg = "sysctl estimate";
2615 if ((buf = malloc(msize)) == NULL) {
2619 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2620 errmsg = "sysctl NET_RT_DUMP";
2623 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2625 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2628 } else if (1 < retry)
2629 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2632 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2633 rtm = (struct rt_msghdr *)(void *)p;
2634 rt_entry(rtm, again);
2640 rt_entry(struct rt_msghdr *rtm, int again)
2642 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2643 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2644 char *rtmp, *ifname = NULL;
2645 struct riprt *rrt, *orrt;
2646 struct netinfo6 *np;
2649 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2650 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2651 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2652 return; /* not interested in the link route */
2654 /* do not look at cloned routes */
2655 #ifdef RTF_WASCLONED
2656 if (rtm->rtm_flags & RTF_WASCLONED)
2660 if (rtm->rtm_flags & RTF_CLONED)
2663 /* XXX: Ignore connected routes. */
2664 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2667 * do not look at dynamic routes.
2668 * netbsd/openbsd cloned routes have UGHD.
2670 if (rtm->rtm_flags & RTF_DYNAMIC)
2672 rtmp = (char *)(rtm + 1);
2674 if ((rtm->rtm_addrs & RTA_DST) == 0)
2675 return; /* ignore routes without destination address */
2676 sin6_dst = (struct sockaddr_in6 *)(void *)rtmp;
2677 rtmp += ROUNDUP(sin6_dst->sin6_len);
2678 if (rtm->rtm_addrs & RTA_GATEWAY) {
2679 sin6_gw = (struct sockaddr_in6 *)(void *)rtmp;
2680 rtmp += ROUNDUP(sin6_gw->sin6_len);
2682 if (rtm->rtm_addrs & RTA_NETMASK) {
2683 sin6_mask = (struct sockaddr_in6 *)(void *)rtmp;
2684 rtmp += ROUNDUP(sin6_mask->sin6_len);
2686 if (rtm->rtm_addrs & RTA_GENMASK) {
2687 sin6_genmask = (struct sockaddr_in6 *)(void *)rtmp;
2688 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2690 if (rtm->rtm_addrs & RTA_IFP) {
2691 sin6_ifp = (struct sockaddr_in6 *)(void *)rtmp;
2692 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2696 if (sin6_dst->sin6_family != AF_INET6)
2698 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2699 return; /* Link-local */
2700 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2701 return; /* Loopback */
2702 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2705 if ((rrt = MALLOC(struct riprt)) == NULL) {
2706 fatal("malloc: struct riprt");
2709 memset(rrt, 0, sizeof(*rrt));
2710 np = &rrt->rrt_info;
2711 rrt->rrt_same = NULL;
2712 rrt->rrt_t = time(NULL);
2713 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2714 rrt->rrt_t = 0; /* Don't age static routes */
2715 if (rtm->rtm_flags & Pflag)
2716 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2717 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2718 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2720 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2721 if (np->rip6_metric < 1)
2722 np->rip6_metric = 1;
2723 rrt->rrt_flags = rtm->rtm_flags;
2724 np->rip6_dest = sin6_dst->sin6_addr;
2727 if (rtm->rtm_flags & RTF_HOST)
2728 np->rip6_plen = 128; /* Host route */
2730 np->rip6_plen = sin6mask2len(sin6_mask);
2734 orrt = rtsearch(np);
2735 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2738 trace(1, "route: %s/%d flags %s: already registered\n",
2739 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2747 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2749 if (sin6_gw->sin6_family == AF_INET6)
2750 rrt->rrt_gw = sin6_gw->sin6_addr;
2751 else if (sin6_gw->sin6_family == AF_LINK) {
2752 /* XXX in case ppp link? */
2753 rrt->rrt_gw = in6addr_loopback;
2755 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2757 trace(1, "route: %s/%d flags %s",
2758 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2759 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2762 ifindex = rtm->rtm_index;
2763 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2764 ifname = index2ifc[ifindex]->ifc_name;
2766 trace(1, " not configured\n");
2770 trace(1, " if %s sock %d", ifname, ifindex);
2771 rrt->rrt_index = ifindex;
2776 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2777 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2778 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2779 trace(0, "***** Gateway %s is not a link-local address.\n",
2780 inet6_n2p(&rrt->rrt_gw));
2781 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2782 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2783 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2786 /* Put it to the route list */
2787 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2788 /* replace route list */
2789 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2790 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2792 trace(1, "route: %s/%d flags %s: replace new route\n",
2793 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2797 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2801 addroute(struct riprt *rrt,
2802 const struct in6_addr *gw,
2805 struct netinfo6 *np;
2806 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2807 struct rt_msghdr *rtm;
2808 struct sockaddr_in6 *sin6;
2811 np = &rrt->rrt_info;
2812 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2813 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2814 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2815 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2816 np->rip6_metric - 1, buf2);
2818 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2819 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2820 np->rip6_metric - 1, buf2);
2824 memset(buf, 0, sizeof(buf));
2825 rtm = (struct rt_msghdr *)(void *)buf;
2826 rtm->rtm_type = RTM_ADD;
2827 rtm->rtm_version = RTM_VERSION;
2828 rtm->rtm_seq = ++seq;
2830 rtm->rtm_flags = rrt->rrt_flags;
2831 rtm->rtm_flags |= Qflag;
2832 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2833 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2834 rtm->rtm_inits = RTV_HOPCOUNT;
2835 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2837 sin6->sin6_len = sizeof(struct sockaddr_in6);
2838 sin6->sin6_family = AF_INET6;
2839 sin6->sin6_addr = np->rip6_dest;
2840 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2842 sin6->sin6_len = sizeof(struct sockaddr_in6);
2843 sin6->sin6_family = AF_INET6;
2844 sin6->sin6_addr = *gw;
2845 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2846 sin6->sin6_scope_id = ifcp->ifc_index;
2847 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2849 sin6->sin6_len = sizeof(struct sockaddr_in6);
2850 sin6->sin6_family = AF_INET6;
2851 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2852 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2854 len = (char *)sin6 - (char *)buf;
2855 rtm->rtm_msglen = len;
2856 if (write(rtsock, buf, len) > 0)
2859 if (errno == EEXIST) {
2860 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2861 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2863 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2864 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2866 trace(0, "Can not write to rtsock (addroute): %s\n",
2869 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2876 delroute(struct netinfo6 *np, struct in6_addr *gw)
2878 u_char buf[BUFSIZ], buf2[BUFSIZ];
2879 struct rt_msghdr *rtm;
2880 struct sockaddr_in6 *sin6;
2883 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2884 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2885 np->rip6_plen, buf2);
2887 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2888 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2892 memset(buf, 0, sizeof(buf));
2893 rtm = (struct rt_msghdr *)(void *)buf;
2894 rtm->rtm_type = RTM_DELETE;
2895 rtm->rtm_version = RTM_VERSION;
2896 rtm->rtm_seq = ++seq;
2898 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2899 rtm->rtm_flags |= Qflag;
2900 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2901 rtm->rtm_flags |= RTF_HOST;
2902 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2903 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2905 sin6->sin6_len = sizeof(struct sockaddr_in6);
2906 sin6->sin6_family = AF_INET6;
2907 sin6->sin6_addr = np->rip6_dest;
2908 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2910 sin6->sin6_len = sizeof(struct sockaddr_in6);
2911 sin6->sin6_family = AF_INET6;
2912 sin6->sin6_addr = *gw;
2913 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2915 sin6->sin6_len = sizeof(struct sockaddr_in6);
2916 sin6->sin6_family = AF_INET6;
2917 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2918 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2920 len = (char *)sin6 - (char *)buf;
2921 rtm->rtm_msglen = len;
2922 if (write(rtsock, buf, len) >= 0)
2925 if (errno == ESRCH) {
2926 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2927 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2929 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2930 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2932 trace(0, "Can not write to rtsock (delroute): %s\n",
2935 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2942 static struct in6_addr *
2943 getroute(struct netinfo6 *np, struct in6_addr *gw)
2948 struct rt_msghdr *rtm;
2949 struct sockaddr_in6 *sin6;
2951 rtm = (struct rt_msghdr *)(void *)buf;
2952 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2953 memset(rtm, 0, len);
2954 rtm->rtm_type = RTM_GET;
2955 rtm->rtm_version = RTM_VERSION;
2957 rtm->rtm_seq = myseq;
2958 rtm->rtm_addrs = RTA_DST;
2959 rtm->rtm_msglen = len;
2960 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2961 sin6->sin6_len = sizeof(struct sockaddr_in6);
2962 sin6->sin6_family = AF_INET6;
2963 sin6->sin6_addr = np->rip6_dest;
2964 if (write(rtsock, buf, len) < 0) {
2965 if (errno == ESRCH) /* No such route found */
2967 perror("write to rtsock");
2971 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2972 perror("read from rtsock");
2975 rtm = (struct rt_msghdr *)(void *)buf;
2976 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2977 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2978 if (rtm->rtm_addrs & RTA_DST) {
2979 sin6 = (struct sockaddr_in6 *)(void *)
2980 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2982 if (rtm->rtm_addrs & RTA_GATEWAY) {
2983 *gw = sin6->sin6_addr;
2991 inet6_n2p(const struct in6_addr *p)
2993 static char buf[BUFSIZ];
2995 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3016 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3019 fprintf(dump, "%s: Interface Table Dump\n", hms());
3020 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3022 fprintf(dump, " Number of interfaces: %d\n", nifc);
3024 fprintf(dump, " advertising interfaces:\n");
3025 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3026 if ((ifcp->ifc_flags & IFF_UP) == 0)
3028 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3030 ifdump0(dump, ifcp);
3032 fprintf(dump, "\n");
3033 fprintf(dump, " non-advertising interfaces:\n");
3034 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3035 if ((ifcp->ifc_flags & IFF_UP) &&
3036 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3038 ifdump0(dump, ifcp);
3040 fprintf(dump, "\n");
3046 ifdump0(FILE *dump, const struct ifc *ifcp)
3054 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3055 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3056 inet6_n2p(&ifcp->ifc_mylladdr),
3057 ifcp->ifc_mtu, ifcp->ifc_metric);
3058 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3059 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3060 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3062 fprintf(dump, "\t%s/%d -- %s\n",
3063 inet6_n2p(&ifac->ifac_addr),
3064 ifac->ifac_plen, buf);
3066 fprintf(dump, "\t%s/%d\n",
3067 inet6_n2p(&ifac->ifac_addr),
3072 fprintf(dump, "\tFilter:\n");
3073 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3075 switch (iffp->iff_type) {
3077 ft = "Aggregate"; addr++; break;
3079 ft = "No-use"; break;
3081 ft = "Advertise-only"; addr++; break;
3083 ft = "Default-only"; break;
3085 ft = "Listen-only"; addr++; break;
3087 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3092 fprintf(dump, "\t\t%s", ft);
3094 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3096 fprintf(dump, "\n");
3098 fprintf(dump, "\n");
3112 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3116 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3117 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3118 if (rrt->rrt_t == 0)
3121 age = t - rrt->rrt_t;
3122 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3124 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3125 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3126 index2ifc[rrt->rrt_index]->ifc_name,
3127 inet6_n2p(&rrt->rrt_gw),
3128 rrt->rrt_info.rip6_metric, (long)age);
3129 if (rrt->rrt_info.rip6_tag) {
3130 fprintf(dump, " tag(0x%04x)",
3131 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3133 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3134 fprintf(dump, " NOT-LL");
3135 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3136 fprintf(dump, " NO-ADV");
3137 fprintf(dump, "\n");
3139 fprintf(dump, "\n");
3145 * Parse the -A (and -O) options and put corresponding filter object to the
3146 * specified interface structures. Each of the -A/O option has the following
3147 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3148 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3154 char *p, *ap, *iflp, *ifname, *ep;
3155 struct iff iff, *iffp;
3163 for (i = 0; i < nfilter; i++) {
3167 memset(iffp, 0, sizeof(*iffp));
3168 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3172 if ((p = strchr(ap, ',')) != NULL) {
3176 if ((p = strchr(ap, '/')) == NULL) {
3177 fatal("no prefixlen specified for '%s'", ap);
3181 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3182 fatal("invalid prefix specified for '%s'", ap);
3187 plen = strtoul(p, &ep, 10);
3188 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3189 fatal("invalid prefix length specified for '%s'", ap);
3192 iffp->iff_plen = plen;
3193 applyplen(&iffp->iff_addr, iffp->iff_plen);
3195 iffp->iff_type = filtertype[i];
3196 if (iflp == NULL || *iflp == '\0') {
3197 fatal("no interface specified for '%s'", ap);
3200 /* parse the interface listing portion */
3203 if ((iflp = strchr(iflp, ',')) != NULL)
3206 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3207 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3210 iffp = malloc(sizeof(*iffp));
3212 fatal("malloc of iff");
3215 memcpy(iffp, &iff, sizeof(*iffp));
3217 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3219 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3224 * -A: aggregate configuration.
3226 if (filtertype[i] != IFIL_TYPE_A)
3228 /* put the aggregate to the kernel routing table */
3229 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3231 fatal("malloc: rrt");
3234 memset(rrt, 0, sizeof(struct riprt));
3235 rrt->rrt_info.rip6_dest = iff.iff_addr;
3236 rrt->rrt_info.rip6_plen = iff.iff_plen;
3237 rrt->rrt_info.rip6_metric = 1;
3238 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3239 rrt->rrt_gw = in6addr_loopback;
3240 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3241 rrt->rrt_rflags = RRTF_AGGREGATE;
3243 rrt->rrt_index = loopifcp->ifc_index;
3245 if (getroute(&rrt->rrt_info, &gw)) {
3248 * When the address has already been registered in the
3249 * kernel routing table, it should be removed
3251 delroute(&rrt->rrt_info, &gw);
3253 /* it is safer behavior */
3255 fatal("%s/%u already in routing table, "
3257 inet6_n2p(&rrt->rrt_info.rip6_dest),
3258 rrt->rrt_info.rip6_plen);
3263 /* Put the route to the list */
3264 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3265 trace(1, "Aggregate: %s/%d for %s\n",
3266 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3267 loopifcp->ifc_name);
3268 /* Add this route to the kernel */
3269 if (nflag) /* do not modify kernel routing table */
3271 addroute(rrt, &in6addr_loopback, loopifcp);
3275 /***************** utility functions *****************/
3278 * Returns a pointer to ifac whose address and prefix length matches
3279 * with the address and prefix length specified in the arguments.
3281 static struct ifac *
3282 ifa_match(const struct ifc *ifcp,
3283 const struct in6_addr *ia,
3288 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3289 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3290 ifac->ifac_plen == plen)
3298 * Return a pointer to riprt structure whose address and prefix length
3299 * matches with the address and prefix length found in the argument.
3300 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3302 static struct riprt *
3303 rtsearch(struct netinfo6 *np)
3307 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3308 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3309 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3318 sin6mask2len(const struct sockaddr_in6 *sin6)
3321 return mask2len(&sin6->sin6_addr,
3322 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3326 mask2len(const struct in6_addr *addr, int lenlim)
3329 const u_char *p = (const u_char *)addr;
3331 for (j = 0; j < lenlim; j++, p++) {
3338 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3339 MASKLEN(0xfe, 7); break;
3340 MASKLEN(0xfc, 6); break;
3341 MASKLEN(0xf8, 5); break;
3342 MASKLEN(0xf0, 4); break;
3343 MASKLEN(0xe0, 3); break;
3344 MASKLEN(0xc0, 2); break;
3345 MASKLEN(0x80, 1); break;
3352 static const u_char plent[8] = {
3353 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3357 applyplen(struct in6_addr *ia, int plen)
3363 for (i = 0; i < 16; i++) {
3372 static const int pl2m[9] = {
3373 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3376 static struct in6_addr *
3379 static struct in6_addr ia;
3383 memset(&ia, 0, sizeof(struct in6_addr));
3385 for (i = 0; i < 16; i++, p++, n -= 8) {
3399 int len = strlen(p) + 1;
3400 char *q = (char *)malloc(len);
3414 static char buf[BUFSIZ];
3419 if ((tm = localtime(&t)) == 0) {
3423 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3428 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3431 ripinterval(int timer)
3435 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3436 nextalarm = time(NULL) + interval;
3447 t = (int)(RIP_TRIG_INT6_MIN +
3448 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3449 sup_trig_update = time(NULL) + t;
3455 fatal(const char *fmt, ...)
3461 vsnprintf(buf, sizeof(buf), fmt, ap);
3465 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3467 syslog(LOG_ERR, "%s", buf);
3472 tracet(int level, const char *fmt, ...)
3476 if (level <= dflag) {
3478 fprintf(stderr, "%s: ", hms());
3479 vfprintf(stderr, fmt, ap);
3485 vsyslog(LOG_DEBUG, fmt, ap);
3487 vsyslog(LOG_WARNING, fmt, ap);
3493 trace(int level, const char *fmt, ...)
3497 if (level <= dflag) {
3499 vfprintf(stderr, fmt, ap);
3505 vsyslog(LOG_DEBUG, fmt, ap);
3507 vsyslog(LOG_WARNING, fmt, ap);
3513 ifc_find(char *name)
3517 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3518 if (strcmp(name, ifcp->ifc_name) == 0)
3525 iff_find(struct ifc *ifcp, int type)
3529 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3530 if (type == IFIL_TYPE_ANY ||
3531 type == iffp->iff_type)
3539 setindex2ifc(int idx, struct ifc *ifcp)
3545 nindex2ifc = 5; /*initial guess*/
3546 index2ifc = (struct ifc **)
3547 malloc(sizeof(*index2ifc) * nindex2ifc);
3548 if (index2ifc == NULL) {
3552 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3555 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3558 p = (struct ifc **)realloc(index2ifc,
3559 sizeof(*index2ifc) * nsize);
3564 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3568 index2ifc[idx] = ifcp;