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 $";
58 #include <sys/types.h>
59 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/sysctl.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet/udp.h>
74 #include <arpa/inet.h>
79 #define RT_DUMP_MAXRETRY 15
82 #define INIT_INTERVAL6 6
84 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
87 /* alignment constraint for routing socket */
89 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
90 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
92 struct ifc { /* Configuration of an interface */
93 TAILQ_ENTRY(ifc) ifc_next;
95 char ifc_name[IFNAMSIZ]; /* if name */
96 int ifc_index; /* if index */
97 int ifc_mtu; /* if mtu */
98 int ifc_metric; /* if metric */
99 u_int ifc_flags; /* flags */
100 short ifc_cflags; /* IFC_XXX */
101 struct in6_addr ifc_mylladdr; /* my link-local address */
102 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
103 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
104 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
105 int ifc_joined; /* joined to ff02::9 */
107 TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
109 struct ifac { /* Adddress associated to an interface */
110 TAILQ_ENTRY(ifac) ifac_next;
112 struct ifc *ifac_ifc; /* back pointer */
113 struct in6_addr ifac_addr; /* address */
114 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
115 int ifac_scope_id; /* scope id */
116 int ifac_plen; /* prefix length */
119 struct iff { /* Filters for an interface */
120 TAILQ_ENTRY(iff) iff_next;
123 struct in6_addr iff_addr;
127 struct ifc **index2ifc;
128 unsigned int nindex2ifc;
129 struct ifc *loopifcp = NULL; /* pointing to loopback */
131 struct pollfd set[2];
133 fd_set *sockvecp; /* vector to select() for receiving */
136 int maxfd; /* maximum fd for select() */
138 int rtsock; /* the routing socket */
139 int ripsock; /* socket to send/receive RIP datagram */
141 struct rip6 *ripbuf; /* packet buffer for sending */
144 * Maintain the routes in a linked list. When the number of the routes
145 * grows, somebody would like to introduce a hash based or a radix tree
146 * based structure. I believe the number of routes handled by RIP is
147 * limited and I don't have to manage a complex data structure, however.
149 * One of the major drawbacks of the linear linked list is the difficulty
150 * of representing the relationship between a couple of routes. This may
151 * be a significant problem when we have to support route aggregation with
152 * suppressing the specifics covered by the aggregate.
156 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
158 struct riprt *rrt_same; /* same destination - future use */
159 struct netinfo6 rrt_info; /* network info */
160 struct in6_addr rrt_gw; /* gateway */
161 u_long rrt_flags; /* kernel routing table flags */
162 u_long rrt_rflags; /* route6d routing table flags */
163 time_t rrt_t; /* when the route validated */
164 int rrt_index; /* ifindex from which this route got */
166 TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
168 int dflag = 0; /* debug flag */
169 int qflag = 0; /* quiet flag */
170 int nflag = 0; /* don't update kernel routing table */
171 int aflag = 0; /* age out even the statically defined routes */
172 int hflag = 0; /* don't split horizon */
173 int lflag = 0; /* exchange site local routes */
174 int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
175 int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
176 int sflag = 0; /* announce static routes w/ split horizon */
177 int Sflag = 0; /* announce static routes to every interface */
178 unsigned long routetag = 0; /* route tag attached on originating case */
180 char *filter[MAXFILTER];
181 int filtertype[MAXFILTER];
186 struct sockaddr_storage ripsin;
189 time_t nextalarm = 0;
190 time_t sup_trig_update = 0;
198 volatile sig_atomic_t seenalrm;
199 volatile sig_atomic_t seenquit;
200 volatile sig_atomic_t seenusr1;
202 #define RRTF_AGGREGATE 0x08000000
203 #define RRTF_NOADVERTISE 0x10000000
204 #define RRTF_NH_NOT_LLADDR 0x20000000
205 #define RRTF_SENDANYWAY 0x40000000
206 #define RRTF_CHANGED 0x80000000
208 int main(int, char **);
209 void sighandler(int);
212 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
213 int out_filter(struct riprt *, struct ifc *);
215 void sockopt(struct ifc *);
217 int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
219 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
220 const struct sockaddr_in6 *);
221 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
222 const struct sockaddr_in6 *);
223 void filterconfig(void);
225 const char *rttypes(struct rt_msghdr *);
226 const char *rtflags(struct rt_msghdr *);
227 const char *ifflags(int);
228 int ifrt(struct ifc *, int);
229 void ifrt_p2p(struct ifc *, int);
230 void applymask(struct in6_addr *, struct in6_addr *);
231 void applyplen(struct in6_addr *, int);
234 void ifdump0(FILE *, const struct ifc *);
237 void rt_entry(struct rt_msghdr *, int);
239 void riprequest(struct ifc *, struct netinfo6 *, int,
240 struct sockaddr_in6 *);
241 void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
242 void sendrequest(struct ifc *);
243 int sin6mask2len(const struct sockaddr_in6 *);
244 int mask2len(const struct in6_addr *, int);
245 int sendpacket(struct sockaddr_in6 *, int);
246 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
247 int delroute(struct netinfo6 *, struct in6_addr *);
248 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
250 int tobeadv(struct riprt *, struct ifc *);
251 char *allocopy(char *);
253 const char *inet6_n2p(const struct in6_addr *);
254 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
255 struct in6_addr *plen2mask(int);
256 struct riprt *rtsearch(struct netinfo6 *);
257 int ripinterval(int);
258 time_t ripsuptrig(void);
259 void fatal(const char *, ...)
260 __attribute__((__format__(__printf__, 1, 2)));
261 void trace(int, const char *, ...)
262 __attribute__((__format__(__printf__, 2, 3)));
263 void tracet(int, const char *, ...)
264 __attribute__((__format__(__printf__, 2, 3)));
265 unsigned int if_maxindex(void);
266 struct ifc *ifc_find(char *);
267 struct iff *iff_find(struct ifc *, int);
268 void setindex2ifc(int, struct ifc *);
270 #define MALLOC(type) ((type *)malloc(sizeof(type)))
272 #define IFIL_TYPE_ANY 0x0
273 #define IFIL_TYPE_A 'A'
274 #define IFIL_TYPE_N 'N'
275 #define IFIL_TYPE_T 'T'
276 #define IFIL_TYPE_O 'O'
277 #define IFIL_TYPE_L 'L'
280 main(int argc, char *argv[])
286 sigset_t mask, omask;
287 const char *pidfile = ROUTE6D_PID;
292 progname = strrchr(*argv, '/');
299 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
306 if (nfilter >= MAXFILTER) {
307 fatal("Exceeds MAXFILTER");
310 filtertype[nfilter] = ch;
311 filter[nfilter++] = allocopy(optarg);
315 routetag = strtoul(optarg, &ep, 0);
316 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
317 fatal("invalid route tag");
326 proto = strtoul(optarg, &ep, 0);
327 if (!ep || *ep != '\0' || 3 < proto) {
328 fatal("invalid P flag");
342 proto = strtoul(optarg, &ep, 0);
343 if (!ep || *ep != '\0' || 3 < proto) {
344 fatal("invalid Q flag");
357 if ((rtlog = fopen(optarg, "w")) == NULL) {
358 fatal("Can not write to routelog");
362 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
363 FLAG('a', aflag, 1); break;
364 FLAG('d', dflag, 1); break;
365 FLAG('D', dflag, 2); break;
366 FLAG('h', hflag, 1); break;
367 FLAG('l', lflag, 1); break;
368 FLAG('n', nflag, 1); break;
369 FLAG('q', qflag, 1); break;
370 FLAG('s', sflag, 1); break;
371 FLAG('S', Sflag, 1); break;
374 fatal("Invalid option specified, terminating");
381 fatal("bogus extra arguments");
387 fprintf(stderr, "No kernel update is allowed\n");
391 if (daemon(0, 0) < 0) {
397 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
400 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
402 memset(ripbuf, 0, RIP6_MAXMTU);
403 ripbuf->rip6_cmd = RIP6_RESPONSE;
404 ripbuf->rip6_vers = RIP6_VERSION;
405 ripbuf->rip6_res1[0] = 0;
406 ripbuf->rip6_res1[1] = 0;
410 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
411 if (ifcp->ifc_index < 0) {
412 fprintf(stderr, "No ifindex found at %s "
413 "(no link-local address?)\n", ifcp->ifc_name);
419 if (loopifcp == NULL) {
420 fatal("No loopback found");
423 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
432 if ((pidfh = fopen(pidfile, "w")) != NULL) {
433 fprintf(pidfh, "%d\n", pid);
437 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
441 memset(ripbuf, 0, RIP6_MAXMTU);
442 ripbuf->rip6_cmd = RIP6_RESPONSE;
443 ripbuf->rip6_vers = RIP6_VERSION;
444 ripbuf->rip6_res1[0] = 0;
445 ripbuf->rip6_res1[1] = 0;
447 if (signal(SIGALRM, sighandler) == SIG_ERR ||
448 signal(SIGQUIT, sighandler) == SIG_ERR ||
449 signal(SIGTERM, sighandler) == SIG_ERR ||
450 signal(SIGUSR1, sighandler) == SIG_ERR ||
451 signal(SIGHUP, sighandler) == SIG_ERR ||
452 signal(SIGINT, sighandler) == SIG_ERR) {
457 * To avoid rip packet congestion (not on a cable but in this
458 * process), wait for a moment to send the first RIP6_RESPONSE
461 alarm(ripinterval(INIT_INTERVAL6));
463 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
464 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
466 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
470 syslog(LOG_INFO, "**** Started ****");
472 sigaddset(&mask, SIGALRM);
491 switch (poll(set, 2, INFTIM))
493 memcpy(recvecp, sockvecp, fdmasks);
494 switch (select(maxfd + 1, recvecp, 0, 0, 0))
498 if (errno != EINTR) {
507 if (set[0].revents & POLLIN)
509 if (FD_ISSET(ripsock, recvecp))
512 sigprocmask(SIG_BLOCK, &mask, &omask);
514 sigprocmask(SIG_SETMASK, &omask, NULL);
517 if (set[1].revents & POLLIN)
519 if (FD_ISSET(rtsock, recvecp))
522 sigprocmask(SIG_BLOCK, &mask, &omask);
524 sigprocmask(SIG_SETMASK, &omask, NULL);
531 sighandler(int signo)
551 * gracefully exits after resetting sockopts.
560 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
561 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
562 delroute(&rrt->rrt_info, &rrt->rrt_gw);
567 syslog(LOG_INFO, "**** Terminated ****");
573 * Called periodically:
574 * 1. age out the learned route. remove it if necessary.
575 * 2. submit RIP6_RESPONSE packets.
576 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
577 * to invoke this function in every 1 or 5 or 10 seconds only to age the
578 * routes more precisely.
585 struct riprt *rrt, *rrt_tmp;
586 time_t t_lifetime, t_holddown;
588 /* age the RIP routes */
589 t_lifetime = time(NULL) - RIP_LIFETIME;
590 t_holddown = t_lifetime - RIP_HOLDDOWN;
591 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
594 else if (rrt->rrt_t < t_holddown) {
595 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
596 delroute(&rrt->rrt_info, &rrt->rrt_gw);
598 } else if (rrt->rrt_t < t_lifetime)
599 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
602 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
603 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
604 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
606 alarm(ripinterval(SUPPLY_INTERVAL6));
613 const int int0 = 0, int1 = 1, int255 = 255;
614 struct addrinfo hints, *res;
615 char port[NI_MAXSERV];
617 TAILQ_INIT(&ifc_head);
618 nindex2ifc = 0; /*initial guess*/
620 snprintf(port, sizeof(port), "%u", RIP6_PORT);
622 memset(&hints, 0, sizeof(hints));
623 hints.ai_family = PF_INET6;
624 hints.ai_socktype = SOCK_DGRAM;
625 hints.ai_protocol = IPPROTO_UDP;
626 hints.ai_flags = AI_PASSIVE;
627 error = getaddrinfo(NULL, port, &hints, &res);
629 fatal("%s", gai_strerror(error));
633 fatal(":: resolved to multiple address");
637 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
643 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
644 &int1, sizeof(int1)) < 0) {
645 fatal("rip IPV6_V6ONLY");
649 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
653 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
654 &int255, sizeof(int255)) < 0) {
655 fatal("rip IPV6_MULTICAST_HOPS");
658 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
659 &int0, sizeof(int0)) < 0) {
660 fatal("rip IPV6_MULTICAST_LOOP");
664 #ifdef IPV6_RECVPKTINFO
665 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
666 &int1, sizeof(int1)) < 0) {
667 fatal("rip IPV6_RECVPKTINFO");
670 #else /* old adv. API */
671 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
672 &int1, sizeof(int1)) < 0) {
673 fatal("rip IPV6_PKTINFO");
678 #ifdef IPV6_RECVPKTINFO
679 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
680 &int1, sizeof(int1)) < 0) {
681 fatal("rip IPV6_RECVHOPLIMIT");
684 #else /* old adv. API */
685 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
686 &int1, sizeof(int1)) < 0) {
687 fatal("rip IPV6_HOPLIMIT");
692 memset(&hints, 0, sizeof(hints));
693 hints.ai_family = PF_INET6;
694 hints.ai_socktype = SOCK_DGRAM;
695 hints.ai_protocol = IPPROTO_UDP;
696 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
698 fatal("%s", gai_strerror(error));
702 fatal("%s resolved to multiple address", RIP6_DEST);
705 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
709 set[0].events = POLLIN;
715 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
716 fatal("route socket");
721 set[1].events = POLLIN;
730 rtsock = -1; /*just for safety */
735 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
736 if ((sockvecp = malloc(fdmasks)) == NULL) {
740 if ((recvecp = malloc(fdmasks)) == NULL) {
744 memset(sockvecp, 0, fdmasks);
745 FD_SET(ripsock, sockvecp);
747 FD_SET(rtsock, sockvecp);
752 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
755 * ripflush flushes the rip datagram stored in the rip buffer
758 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
764 tracet(1, "Send(%s): info(%d) to %s.%d\n",
766 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
768 tracet(1, "Send: info(%d) to %s.%d\n",
769 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
771 np = ripbuf->rip6_nets;
772 for (i = 0; i < nrt; i++, np++) {
773 if (np->rip6_metric == NEXTHOP_METRIC) {
774 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
775 trace(2, " NextHop reset");
777 trace(2, " NextHop %s",
778 inet6_n2p(&np->rip6_dest));
781 trace(2, " %s/%d[%d]",
782 inet6_n2p(&np->rip6_dest),
783 np->rip6_plen, np->rip6_metric);
786 trace(2, " tag=0x%04x",
787 ntohs(np->rip6_tag) & 0xffff);
792 error = sendpacket(sin6, RIPSIZE(nrt));
793 if (error == EAFNOSUPPORT) {
794 /* Protocol not supported */
795 tracet(1, "Could not send info to %s (%s): "
797 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
798 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
803 * Generate RIP6_RESPONSE packets and send them.
806 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
809 struct in6_addr *nh; /* next hop */
819 * Request from non-link local address is not
820 * a regular route6d update.
822 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
823 sizeof(struct udphdr) -
824 sizeof(struct rip6) + sizeof(struct netinfo6)) /
825 sizeof(struct netinfo6);
828 np = ripbuf->rip6_nets;
829 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
830 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
832 /* Put the route to the buffer */
836 ripflush(NULL, sin6, nrt, np);
839 np = ripbuf->rip6_nets;
842 if (nrt) /* Send last packet */
843 ripflush(NULL, sin6, nrt, np);
847 if ((flag & RRTF_SENDANYWAY) == 0 &&
848 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
852 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
855 /* -T: generate default route only */
856 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
857 struct netinfo6 rrt_info;
858 memset(&rrt_info, 0, sizeof(struct netinfo6));
859 rrt_info.rip6_dest = in6addr_any;
860 rrt_info.rip6_plen = 0;
861 rrt_info.rip6_metric = 1;
862 rrt_info.rip6_metric += ifcp->ifc_metric;
863 rrt_info.rip6_tag = htons(routetag & 0xffff);
864 np = ripbuf->rip6_nets;
867 ripflush(ifcp, sin6, nrt, np);
871 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
872 sizeof(struct udphdr) -
873 sizeof(struct rip6) + sizeof(struct netinfo6)) /
874 sizeof(struct netinfo6);
876 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
877 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
878 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
881 /* Need to check filter here */
882 if (out_filter(rrt, ifcp) == 0)
885 /* Check split horizon and other conditions */
886 if (tobeadv(rrt, ifcp) == 0)
889 /* Only considers the routes with flag if specified */
890 if ((flag & RRTF_CHANGED) &&
891 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
895 if (rrt->rrt_index == ifcp->ifc_index &&
896 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
897 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
898 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
899 if (nrt == maxrte - 2) {
900 ripflush(ifcp, sin6, nrt, np);
903 np = ripbuf->rip6_nets;
906 np->rip6_dest = rrt->rrt_gw;
909 np->rip6_metric = NEXTHOP_METRIC;
913 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
914 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
915 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
917 if (nrt == maxrte - 2) {
918 ripflush(ifcp, sin6, nrt, np);
921 np = ripbuf->rip6_nets;
923 memset(np, 0, sizeof(struct netinfo6));
924 np->rip6_metric = NEXTHOP_METRIC;
929 /* Put the route to the buffer */
933 ripflush(ifcp, sin6, nrt, np);
936 np = ripbuf->rip6_nets;
939 if (nrt) /* Send last packet */
940 ripflush(ifcp, sin6, nrt, np);
944 * outbound filter logic, per-route/interface.
947 out_filter(struct riprt *rrt, struct ifc *ifcp)
954 * -A: filter out less specific routes, if we have aggregated
957 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
958 if (iffp->iff_type != 'A')
960 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
962 ia = rrt->rrt_info.rip6_dest;
963 applyplen(&ia, iffp->iff_plen);
964 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
969 * if it is an aggregated route, advertise it only to the
970 * interfaces specified on -A.
972 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
974 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
975 if (iffp->iff_type != 'A')
977 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
978 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
989 * -O: advertise only if prefix matches the configured prefix.
991 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
993 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
994 if (iffp->iff_type != 'O')
996 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
998 ia = rrt->rrt_info.rip6_dest;
999 applyplen(&ia, iffp->iff_plen);
1000 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1009 /* the prefix should be advertised */
1014 * Determine if the route is to be advertised on the specified interface.
1015 * It checks options specified in the arguments and the split horizon rule.
1018 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1021 /* Special care for static routes */
1022 if (rrt->rrt_flags & RTF_STATIC) {
1023 /* XXX don't advertise reject/blackhole routes */
1024 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1027 if (Sflag) /* Yes, advertise it anyway */
1029 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1033 /* Regular split horizon */
1034 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1040 * Send a rip packet actually.
1043 sendpacket(struct sockaddr_in6 *sin6, int len)
1047 struct iovec iov[2];
1048 u_char cmsgbuf[256];
1049 struct in6_pktinfo *pi;
1051 struct sockaddr_in6 sincopy;
1053 /* do not overwrite the given sin */
1057 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1058 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1059 idx = sin6->sin6_scope_id;
1063 m.msg_name = (caddr_t)sin6;
1064 m.msg_namelen = sizeof(*sin6);
1065 iov[0].iov_base = (caddr_t)ripbuf;
1066 iov[0].iov_len = len;
1070 m.msg_control = NULL;
1071 m.msg_controllen = 0;
1073 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1074 cm = (struct cmsghdr *)cmsgbuf;
1075 m.msg_control = (caddr_t)cm;
1076 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1078 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1079 cm->cmsg_level = IPPROTO_IPV6;
1080 cm->cmsg_type = IPV6_PKTINFO;
1081 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1082 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1083 pi->ipi6_ifindex = idx;
1086 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1087 trace(1, "sendmsg: %s\n", strerror(errno));
1095 * Receive and process RIP packets. Update the routes/kernel forwarding
1096 * table if necessary.
1101 struct ifc *ifcp, *ic;
1102 struct sockaddr_in6 fsock;
1103 struct in6_addr nh; /* next hop */
1105 struct netinfo6 *np, *nq;
1108 unsigned int need_trigger, idx;
1109 char buf[4 * RIP6_MAXMTU];
1113 struct iovec iov[2];
1114 u_char cmsgbuf[256];
1115 struct in6_pktinfo *pi = NULL;
1120 time_t t_half_lifetime;
1124 m.msg_name = (caddr_t)&fsock;
1125 m.msg_namelen = sizeof(fsock);
1126 iov[0].iov_base = (caddr_t)buf;
1127 iov[0].iov_len = sizeof(buf);
1130 cm = (struct cmsghdr *)cmsgbuf;
1131 m.msg_control = (caddr_t)cm;
1132 m.msg_controllen = sizeof(cmsgbuf);
1133 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1138 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1140 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1141 if (cm->cmsg_level != IPPROTO_IPV6)
1143 switch (cm->cmsg_type) {
1145 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1147 "invalid cmsg length for IPV6_PKTINFO\n");
1150 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1151 idx = pi->ipi6_ifindex;
1154 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1156 "invalid cmsg length for IPV6_HOPLIMIT\n");
1159 hlimp = (int *)CMSG_DATA(cm);
1164 if ((size_t)len < sizeof(struct rip6)) {
1165 trace(1, "Packet too short\n");
1169 if (pi == NULL || hlimp == NULL) {
1171 * This can happen when the kernel failed to allocate memory
1172 * for the ancillary data. Although we might be able to handle
1173 * some cases without this info, those are minor and not so
1174 * important, so it's better to discard the packet for safer
1177 trace(1, "IPv6 packet information cannot be retrieved\n");
1181 nh = fsock.sin6_addr;
1182 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1183 sizeof(struct netinfo6);
1184 rp = (struct rip6 *)buf;
1187 if (rp->rip6_vers != RIP6_VERSION) {
1188 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1191 if (rp->rip6_cmd == RIP6_REQUEST) {
1192 if (idx && idx < nindex2ifc) {
1193 ifcp = index2ifc[idx];
1194 riprequest(ifcp, np, nn, &fsock);
1196 riprequest(NULL, np, nn, &fsock);
1201 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1202 trace(1, "Response from non-ll addr: %s\n",
1203 inet6_n2p(&fsock.sin6_addr));
1204 return; /* Ignore packets from non-link-local addr */
1206 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1207 trace(1, "Response from non-rip port from %s\n",
1208 inet6_n2p(&fsock.sin6_addr));
1211 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1213 "Response packet with a smaller hop limit (%d) from %s\n",
1214 *hlimp, inet6_n2p(&fsock.sin6_addr));
1218 * Further validation: since this program does not send off-link
1219 * requests, an incoming response must always come from an on-link
1220 * node. Although this is normally ensured by the source address
1221 * check above, it may not 100% be safe because there are router
1222 * implementations that (invalidly) allow a packet with a link-local
1223 * source address to be forwarded to a different link.
1224 * So we also check whether the destination address is a link-local
1225 * address or the hop limit is 255. Note that RFC2080 does not require
1226 * the specific hop limit for a unicast response, so we cannot assume
1229 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1231 "Response packet possibly from an off-link node: "
1232 "from %s to %s hlim=%d\n",
1233 inet6_n2p(&fsock.sin6_addr),
1234 inet6_n2p(&pi->ipi6_addr), *hlimp);
1238 idx = fsock.sin6_scope_id;
1239 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1241 trace(1, "Packets to unknown interface index %d\n", idx);
1242 return; /* Ignore it */
1244 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1245 return; /* The packet is from me; ignore */
1246 if (rp->rip6_cmd != RIP6_RESPONSE) {
1247 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1252 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1255 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1256 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1259 t_half_lifetime = t - (RIP_LIFETIME/2);
1260 for (; nn; nn--, np++) {
1261 if (np->rip6_metric == NEXTHOP_METRIC) {
1262 /* modify neighbor address */
1263 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1265 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1266 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1267 nh = fsock.sin6_addr;
1268 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1270 nh = fsock.sin6_addr;
1271 trace(1, "\tInvalid Nexthop: %s\n",
1272 inet6_n2p(&np->rip6_dest));
1276 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1277 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1278 inet6_n2p(&np->rip6_dest),
1279 np->rip6_plen, np->rip6_metric);
1282 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1283 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1284 inet6_n2p(&np->rip6_dest),
1285 np->rip6_plen, np->rip6_metric);
1288 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1289 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1290 inet6_n2p(&np->rip6_dest),
1291 np->rip6_plen, np->rip6_metric);
1294 /* may need to pass sitelocal prefix in some case, however*/
1295 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1296 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1297 inet6_n2p(&np->rip6_dest),
1298 np->rip6_plen, np->rip6_metric);
1301 trace(2, "\tnetinfo6: %s/%d [%d]",
1302 inet6_n2p(&np->rip6_dest),
1303 np->rip6_plen, np->rip6_metric);
1305 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1308 applyplen(&ia, np->rip6_plen);
1309 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1310 trace(2, " [junk outside prefix]");
1314 * -L: listen only if the prefix matches the configuration
1316 ok = 1; /* if there's no L filter, it is ok */
1317 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1318 if (iffp->iff_type != IFIL_TYPE_L)
1321 if (np->rip6_plen < iffp->iff_plen)
1323 /* special rule: ::/0 means default, not "in /0" */
1324 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1327 applyplen(&ia, iffp->iff_plen);
1328 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1334 trace(2, " (filtered)\n");
1340 np->rip6_metric += ifcp->ifc_metric;
1341 if (np->rip6_metric > HOPCNT_INFINITY6)
1342 np->rip6_metric = HOPCNT_INFINITY6;
1344 applyplen(&np->rip6_dest, np->rip6_plen);
1345 if ((rrt = rtsearch(np)) != NULL) {
1346 if (rrt->rrt_t == 0)
1347 continue; /* Intf route has priority */
1348 nq = &rrt->rrt_info;
1349 if (nq->rip6_metric > np->rip6_metric) {
1350 if (rrt->rrt_index == ifcp->ifc_index &&
1351 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1352 /* Small metric from the same gateway */
1353 nq->rip6_metric = np->rip6_metric;
1355 /* Better route found */
1356 rrt->rrt_index = ifcp->ifc_index;
1357 /* Update routing table */
1358 delroute(nq, &rrt->rrt_gw);
1361 addroute(rrt, &nh, ifcp);
1363 rrt->rrt_rflags |= RRTF_CHANGED;
1366 } else if (nq->rip6_metric < np->rip6_metric &&
1367 rrt->rrt_index == ifcp->ifc_index &&
1368 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1369 /* Got worse route from same gw */
1370 nq->rip6_metric = np->rip6_metric;
1372 rrt->rrt_rflags |= RRTF_CHANGED;
1374 } else if (nq->rip6_metric == np->rip6_metric &&
1375 np->rip6_metric < HOPCNT_INFINITY6) {
1376 if (rrt->rrt_index == ifcp->ifc_index &&
1377 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1378 /* same metric, same route from same gw */
1380 } else if (rrt->rrt_t < t_half_lifetime) {
1381 /* Better route found */
1382 rrt->rrt_index = ifcp->ifc_index;
1383 /* Update routing table */
1384 delroute(nq, &rrt->rrt_gw);
1387 addroute(rrt, &nh, ifcp);
1388 rrt->rrt_rflags |= RRTF_CHANGED;
1393 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1394 * do not update age value. Do nothing.
1396 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1397 /* Got a new valid route */
1398 if ((rrt = MALLOC(struct riprt)) == NULL) {
1399 fatal("malloc: struct riprt");
1402 memset(rrt, 0, sizeof(*rrt));
1403 nq = &rrt->rrt_info;
1405 rrt->rrt_same = NULL;
1406 rrt->rrt_index = ifcp->ifc_index;
1407 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1410 applyplen(&nq->rip6_dest, nq->rip6_plen);
1411 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1412 rrt->rrt_flags |= RTF_HOST;
1414 /* Update routing table */
1415 addroute(rrt, &nh, ifcp);
1416 rrt->rrt_rflags |= RRTF_CHANGED;
1420 /* Put the route to the list */
1421 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1424 /* XXX need to care the interval between triggered updates */
1426 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1427 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1428 if (ifcp->ifc_index == ic->ifc_index)
1430 if (ic->ifc_flags & IFF_UP)
1431 ripsend(ic, &ic->ifc_ripsin,
1435 /* Reset the flag */
1436 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1437 rrt->rrt_rflags &= ~RRTF_CHANGED;
1443 * Send all routes request packet to the specified interface.
1446 sendrequest(struct ifc *ifcp)
1448 struct netinfo6 *np;
1451 if (ifcp->ifc_flags & IFF_LOOPBACK)
1453 ripbuf->rip6_cmd = RIP6_REQUEST;
1454 np = ripbuf->rip6_nets;
1455 memset(np, 0, sizeof(struct netinfo6));
1456 np->rip6_metric = HOPCNT_INFINITY6;
1457 tracet(1, "Send rtdump Request to %s (%s)\n",
1458 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1459 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1460 if (error == EAFNOSUPPORT) {
1461 /* Protocol not supported */
1462 tracet(1, "Could not send rtdump Request to %s (%s): "
1463 "set IFF_UP to 0\n",
1464 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1465 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1467 ripbuf->rip6_cmd = RIP6_RESPONSE;
1471 * Process a RIP6_REQUEST packet.
1474 riprequest(struct ifc *ifcp,
1475 struct netinfo6 *np,
1477 struct sockaddr_in6 *sin6)
1482 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1483 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1484 /* Specific response, don't split-horizon */
1485 trace(1, "\tRIP Request\n");
1486 for (i = 0; i < nn; i++, np++) {
1489 np->rip6_metric = rrt->rrt_info.rip6_metric;
1491 np->rip6_metric = HOPCNT_INFINITY6;
1493 (void)sendpacket(sin6, RIPSIZE(nn));
1496 /* Whole routing table dump */
1497 trace(1, "\tRIP Request -- whole routing table\n");
1498 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1502 * Get information of each interface.
1507 struct ifaddrs *ifap, *ifa;
1509 struct ipv6_mreq mreq;
1512 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1517 if (getifaddrs(&ifap) != 0) {
1518 fatal("getifaddrs");
1522 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1523 if (ifa->ifa_addr->sa_family != AF_INET6)
1525 ifcp = ifc_find(ifa->ifa_name);
1526 /* we are interested in multicast-capable interfaces */
1527 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1531 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1532 fatal("malloc: struct ifc");
1535 memset(ifcp, 0, sizeof(*ifcp));
1537 ifcp->ifc_index = -1;
1538 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1539 sizeof(ifcp->ifc_name));
1540 TAILQ_INIT(&ifcp->ifc_ifac_head);
1541 TAILQ_INIT(&ifcp->ifc_iff_head);
1542 ifcp->ifc_flags = ifa->ifa_flags;
1543 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1544 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1545 ifflags(ifcp->ifc_flags));
1546 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1549 /* update flag, this may be up again */
1550 if (ifcp->ifc_flags != ifa->ifa_flags) {
1551 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1552 ifflags(ifcp->ifc_flags));
1553 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1554 ifcp->ifc_cflags |= IFC_CHANGED;
1556 ifcp->ifc_flags = ifa->ifa_flags;
1558 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1559 /* maybe temporary failure */
1562 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1563 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1564 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1565 mreq.ipv6mr_interface = ifcp->ifc_index;
1566 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1567 &mreq, sizeof(mreq)) < 0) {
1568 fatal("IPV6_JOIN_GROUP");
1571 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1580 ifconfig1(const char *name,
1581 const struct sockaddr *sa,
1585 struct in6_ifreq ifr;
1586 const struct sockaddr_in6 *sin6;
1591 sin6 = (const struct sockaddr_in6 *)sa;
1592 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1594 ifr.ifr_addr = *sin6;
1595 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1596 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1597 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1600 plen = sin6mask2len(&ifr.ifr_addr);
1601 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1602 /* same interface found */
1603 /* need check if something changed */
1604 /* XXX not yet implemented */
1608 * New address is found
1610 if ((ifac = MALLOC(struct ifac)) == NULL) {
1611 fatal("malloc: struct ifac");
1614 memset(ifac, 0, sizeof(*ifac));
1616 ifac->ifac_ifc = ifcp;
1617 ifac->ifac_addr = sin6->sin6_addr;
1618 ifac->ifac_plen = plen;
1619 ifac->ifac_scope_id = sin6->sin6_scope_id;
1620 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1621 ifr.ifr_addr = *sin6;
1622 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1623 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1626 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1627 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1629 trace(1, "found address %s/%d -- %s\n",
1630 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1632 trace(1, "found address %s/%d\n",
1633 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1635 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1636 ifcp->ifc_mylladdr = ifac->ifac_addr;
1637 ifcp->ifc_index = ifac->ifac_scope_id;
1638 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1639 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1640 setindex2ifc(ifcp->ifc_index, ifcp);
1641 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1642 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1643 ifcp->ifc_mtu = RIP6_MAXMTU;
1644 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1645 fatal("ioctl: SIOCGIFMETRIC");
1648 ifcp->ifc_metric = ifr.ifr_metric;
1649 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1650 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1652 ifcp->ifc_cflags |= IFC_CHANGED;
1654 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1660 ifremove(int ifindex)
1665 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1666 if (ifcp->ifc_index == ifindex)
1672 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1673 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1675 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1676 if (rrt->rrt_index == ifcp->ifc_index &&
1677 rrt->rrt_rflags & RRTF_AGGREGATE)
1678 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1684 * Receive and process routing messages.
1685 * Update interface information as necesssary.
1692 struct rt_msghdr *rtm;
1693 struct ifa_msghdr *ifam;
1694 struct if_msghdr *ifm;
1695 struct if_announcemsghdr *ifan;
1697 struct ifc *ifcp, *ic;
1698 int iface = 0, rtable = 0;
1699 struct sockaddr_in6 *rta[RTAX_MAX];
1700 struct sockaddr_in6 mask;
1704 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1705 perror("read from rtsock");
1711 if (len < sizeof(*rtm)) {
1712 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1713 len, (u_long)sizeof(*rtm));
1718 fprintf(stderr, "rtmsg:\n");
1719 for (i = 0; i < len; i++) {
1720 fprintf(stderr, "%02x ", buf[i] & 0xff);
1721 if (i % 16 == 15) fprintf(stderr, "\n");
1723 fprintf(stderr, "\n");
1726 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1727 if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1730 /* safety against bogus message */
1731 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1732 trace(1, "bogus rtmsg: length=%d\n",
1733 ((struct rt_msghdr *)p)->rtm_msglen);
1739 switch (((struct rt_msghdr *)p)->rtm_type) {
1742 ifam = (struct ifa_msghdr *)p;
1743 addrs = ifam->ifam_addrs;
1744 q = (char *)(ifam + 1);
1747 ifm = (struct if_msghdr *)p;
1748 addrs = ifm->ifm_addrs;
1749 q = (char *)(ifm + 1);
1751 case RTM_IFANNOUNCE:
1752 ifan = (struct if_announcemsghdr *)p;
1753 switch (ifan->ifan_what) {
1757 case IFAN_DEPARTURE:
1758 ifremove(ifan->ifan_index);
1764 rtm = (struct rt_msghdr *)p;
1765 addrs = rtm->rtm_addrs;
1766 q = (char *)(rtm + 1);
1767 if (rtm->rtm_version != RTM_VERSION) {
1768 trace(1, "unexpected rtmsg version %d "
1770 rtm->rtm_version, RTM_VERSION);
1773 if (rtm->rtm_pid == pid) {
1775 trace(1, "rtmsg looped back to me, ignored\n");
1781 memset(&rta, 0, sizeof(rta));
1782 for (i = 0; i < RTAX_MAX; i++) {
1783 if (addrs & (1 << i)) {
1784 rta[i] = (struct sockaddr_in6 *)q;
1785 q += ROUNDUP(rta[i]->sin6_len);
1789 trace(1, "rtsock: %s (addrs=%x)\n",
1790 rttypes((struct rt_msghdr *)p), addrs);
1793 i < ((struct rt_msghdr *)p)->rtm_msglen;
1795 fprintf(stderr, "%02x ", p[i] & 0xff);
1796 if (i % 16 == 15) fprintf(stderr, "\n");
1798 fprintf(stderr, "\n");
1804 * We may be able to optimize by using ifm->ifm_index or
1805 * ifam->ifam_index. For simplicity we don't do that here.
1807 switch (((struct rt_msghdr *)p)->rtm_type) {
1819 /* nothing to be done here */
1820 trace(1, "\tnothing to be done, ignored\n");
1825 if (rta[RTAX_DST] == NULL) {
1826 trace(1, "\tno destination, ignored\n");
1829 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1830 trace(1, "\taf mismatch, ignored\n");
1833 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1834 trace(1, "\tlinklocal destination, ignored\n");
1837 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1838 trace(1, "\tloopback destination, ignored\n");
1839 continue; /* Loopback */
1841 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1842 trace(1, "\tmulticast destination, ignored\n");
1848 switch (((struct rt_msghdr *)p)->rtm_type) {
1856 /* should already be handled */
1857 fatal("rtrecv: never reach here");
1860 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1861 trace(1, "\tsome of dst/gw/netamsk are "
1862 "unavailable, ignored\n");
1865 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1866 mask.sin6_len = sizeof(mask);
1867 memset(&mask.sin6_addr, 0xff,
1868 sizeof(mask.sin6_addr));
1869 rta[RTAX_NETMASK] = &mask;
1870 } else if (!rta[RTAX_NETMASK]) {
1871 trace(1, "\tsome of dst/gw/netamsk are "
1872 "unavailable, ignored\n");
1875 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1876 rta[RTAX_NETMASK]) == 0) {
1877 rtable++; /*just to be sure*/
1882 trace(1, "\tnot supported yet, ignored\n");
1885 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1886 trace(1, "\tno netmask or ifa given, ignored\n");
1889 if (ifam->ifam_index < nindex2ifc)
1890 ifcp = index2ifc[ifam->ifam_index];
1894 trace(1, "\tinvalid ifam_index %d, ignored\n",
1898 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1906 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1908 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1909 if (ifcp->ifc_cflags & IFC_CHANGED) {
1910 if (ifrt(ifcp, 1)) {
1911 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1912 if (ifcp->ifc_index == ic->ifc_index)
1914 if (ic->ifc_flags & IFF_UP)
1915 ripsend(ic, &ic->ifc_ripsin,
1918 /* Reset the flag */
1919 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1920 rrt->rrt_rflags &= ~RRTF_CHANGED;
1923 ifcp->ifc_cflags &= ~IFC_CHANGED;
1928 trace(1, "rtsock: read routing table again\n");
1934 * remove specified route from the internal routing table.
1937 rt_del(const struct sockaddr_in6 *sdst,
1938 const struct sockaddr_in6 *sgw,
1939 const struct sockaddr_in6 *smask)
1941 const struct in6_addr *dst = NULL;
1942 const struct in6_addr *gw = NULL;
1944 struct netinfo6 ni6;
1945 struct riprt *rrt = NULL;
1948 if (sdst->sin6_family != AF_INET6) {
1949 trace(1, "\tother AF, ignored\n");
1952 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1953 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1954 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1955 trace(1, "\taddress %s not interesting, ignored\n",
1956 inet6_n2p(&sdst->sin6_addr));
1959 dst = &sdst->sin6_addr;
1960 if (sgw->sin6_family == AF_INET6) {
1962 gw = &sgw->sin6_addr;
1963 prefix = sin6mask2len(smask);
1964 } else if (sgw->sin6_family == AF_LINK) {
1966 * Interface route... a hard case. We need to get the prefix
1967 * length from the kernel, but we now are parsing rtmsg.
1968 * We'll purge matching routes from my list, then get the
1971 struct riprt *longest;
1972 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1975 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1976 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1978 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1980 || longest->rrt_info.rip6_plen <
1981 rrt->rrt_info.rip6_plen) {
1988 trace(1, "\tno matching interface route found\n");
1991 gw = &in6addr_loopback;
1992 prefix = rrt->rrt_info.rip6_plen;
1994 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1998 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1999 trace(1, "gw %s\n", inet6_n2p(gw));
2000 t_lifetime = time(NULL) - RIP_LIFETIME;
2001 /* age route for interface address */
2002 memset(&ni6, 0, sizeof(ni6));
2003 ni6.rip6_dest = *dst;
2004 ni6.rip6_plen = prefix;
2005 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2006 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2008 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2009 trace(1, "\tno route found\n");
2013 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2014 trace(1, "\tyou can delete static routes only\n");
2017 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2018 trace(1, "\tgw mismatch: %s <-> ",
2019 inet6_n2p(&rrt->rrt_gw));
2020 trace(1, "%s\n", inet6_n2p(gw));
2022 trace(1, "\troute found, age it\n");
2023 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2024 rrt->rrt_t = t_lifetime;
2025 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2032 * remove specified address from internal interface/routing table.
2035 rt_deladdr(struct ifc *ifcp,
2036 const struct sockaddr_in6 *sifa,
2037 const struct sockaddr_in6 *smask)
2039 const struct in6_addr *addr = NULL;
2041 struct ifac *ifac = NULL;
2042 struct netinfo6 ni6;
2043 struct riprt *rrt = NULL;
2047 if (sifa->sin6_family != AF_INET6) {
2048 trace(1, "\tother AF, ignored\n");
2051 addr = &sifa->sin6_addr;
2052 prefix = sin6mask2len(smask);
2054 trace(1, "\tdeleting %s/%d from %s\n",
2055 inet6_n2p(addr), prefix, ifcp->ifc_name);
2056 ifac = ifa_match(ifcp, addr, prefix);
2058 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2059 inet6_n2p(addr), prefix, ifcp->ifc_name);
2062 if (ifac->ifac_ifc != ifcp) {
2063 trace(1, "\taddress table corrupt: back pointer does not match "
2065 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2068 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2069 t_lifetime = time(NULL) - RIP_LIFETIME;
2070 /* age route for interface address */
2071 memset(&ni6, 0, sizeof(ni6));
2072 ni6.rip6_dest = ifac->ifac_addr;
2073 ni6.rip6_plen = ifac->ifac_plen;
2074 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2075 trace(1, "\tfind interface route %s/%d on %d\n",
2076 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2077 if ((rrt = rtsearch(&ni6)) != NULL) {
2078 struct in6_addr none;
2079 memset(&none, 0, sizeof(none));
2080 if (rrt->rrt_index == ifcp->ifc_index &&
2081 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2082 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2083 trace(1, "\troute found, age it\n");
2084 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2085 rrt->rrt_t = t_lifetime;
2086 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2090 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2091 inet6_n2p(&rrt->rrt_info.rip6_dest),
2092 rrt->rrt_info.rip6_plen,
2096 trace(1, "\tno interface route found\n");
2097 /* age route for p2p destination */
2098 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2099 memset(&ni6, 0, sizeof(ni6));
2100 ni6.rip6_dest = ifac->ifac_raddr;
2101 ni6.rip6_plen = 128;
2102 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2103 trace(1, "\tfind p2p route %s/%d on %d\n",
2104 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2106 if ((rrt = rtsearch(&ni6)) != NULL) {
2107 if (rrt->rrt_index == ifcp->ifc_index &&
2108 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2109 &ifac->ifac_addr)) {
2110 trace(1, "\troute found, age it\n");
2111 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2112 rrt->rrt_t = t_lifetime;
2113 rrt->rrt_info.rip6_metric =
2118 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2119 inet6_n2p(&rrt->rrt_info.rip6_dest),
2120 rrt->rrt_info.rip6_plen,
2124 trace(1, "\tno p2p route found\n");
2128 return ((updated) ? 0 : -1);
2132 * Get each interface address and put those interface routes to the route
2136 ifrt(struct ifc *ifcp, int again)
2139 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2140 struct netinfo6 *np;
2142 int need_trigger = 0;
2145 if (ifcp->ifc_flags & IFF_LOOPBACK)
2146 return 0; /* ignore loopback */
2149 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2150 ifrt_p2p(ifcp, again);
2154 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2155 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2157 trace(1, "route: %s on %s: "
2158 "skip linklocal interface address\n",
2159 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2163 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2165 trace(1, "route: %s: skip unspec interface address\n",
2170 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2172 trace(1, "route: %s: skip loopback address\n",
2177 if (ifcp->ifc_flags & IFF_UP) {
2178 if ((rrt = MALLOC(struct riprt)) == NULL)
2179 fatal("malloc: struct riprt");
2180 memset(rrt, 0, sizeof(*rrt));
2181 rrt->rrt_same = NULL;
2182 rrt->rrt_index = ifcp->ifc_index;
2183 rrt->rrt_t = 0; /* don't age */
2184 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2185 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2186 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2187 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2188 rrt->rrt_flags = RTF_HOST;
2189 rrt->rrt_rflags |= RRTF_CHANGED;
2190 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2191 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2192 rrt->rrt_gw = ifac->ifac_addr;
2193 np = &rrt->rrt_info;
2194 search_rrt = rtsearch(np);
2195 if (search_rrt != NULL) {
2196 if (search_rrt->rrt_info.rip6_metric <=
2197 rrt->rrt_info.rip6_metric) {
2198 /* Already have better route */
2200 trace(1, "route: %s/%d: "
2201 "already registered (%s)\n",
2202 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2208 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2209 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2211 /* Attach the route to the list */
2212 trace(1, "route: %s/%d: register route (%s)\n",
2213 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2215 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2216 addroute(rrt, &rrt->rrt_gw, ifcp);
2219 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2222 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2223 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2224 t_lifetime = time(NULL) - RIP_LIFETIME;
2225 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2226 loop_rrt->rrt_t = t_lifetime;
2227 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2228 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2238 return need_trigger;
2242 * there are couple of p2p interface routing models. "behavior" lets
2243 * you pick one. it looks that gated behavior fits best with BSDs,
2244 * since BSD kernels do not look at prefix length on p2p interfaces.
2247 ifrt_p2p(struct ifc *ifcp, int again)
2250 struct riprt *rrt, *orrt;
2251 struct netinfo6 *np;
2252 struct in6_addr addr, dest;
2253 int advert, ignore, i;
2254 #define P2PADVERT_NETWORK 1
2255 #define P2PADVERT_ADDR 2
2256 #define P2PADVERT_DEST 4
2257 #define P2PADVERT_MAX 4
2258 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2259 const char *category = "";
2262 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2263 addr = ifac->ifac_addr;
2264 dest = ifac->ifac_raddr;
2265 applyplen(&addr, ifac->ifac_plen);
2266 applyplen(&dest, ifac->ifac_plen);
2267 advert = ignore = 0;
2271 * honor addr/plen, just like normal shared medium
2272 * interface. this may cause trouble if you reuse
2273 * addr/plen on other interfaces.
2275 * advertise addr/plen.
2277 advert |= P2PADVERT_NETWORK;
2281 * prefixlen on p2p interface is meaningless.
2282 * advertise addr/128 and dest/128.
2284 * do not install network route to route6d routing
2285 * table (if we do, it would prevent route installation
2286 * for other p2p interface that shares addr/plen).
2288 * XXX what should we do if dest is ::? it will not
2289 * get announced anyways (see following filter),
2290 * but we need to think.
2292 advert |= P2PADVERT_ADDR;
2293 advert |= P2PADVERT_DEST;
2294 ignore |= P2PADVERT_NETWORK;
2298 * just for testing. actually the code is redundant
2299 * given the current p2p interface address assignment
2300 * rule for kame kernel.
2303 * A/n -> announce A/n
2304 * A B/n, A and B share prefix -> A/n (= B/n)
2305 * A B/n, do not share prefix -> A/128 and B/128
2306 * actually, A/64 and A B/128 are the only cases
2307 * permitted by the kernel:
2309 * A B/128 -> A/128 and B/128
2311 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2312 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2313 advert |= P2PADVERT_NETWORK;
2315 advert |= P2PADVERT_ADDR;
2316 advert |= P2PADVERT_DEST;
2317 ignore |= P2PADVERT_NETWORK;
2320 advert |= P2PADVERT_NETWORK;
2324 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2325 if ((ignore & i) != 0)
2327 if ((rrt = MALLOC(struct riprt)) == NULL) {
2328 fatal("malloc: struct riprt");
2331 memset(rrt, 0, sizeof(*rrt));
2332 rrt->rrt_same = NULL;
2333 rrt->rrt_index = ifcp->ifc_index;
2334 rrt->rrt_t = 0; /* don't age */
2336 case P2PADVERT_NETWORK:
2337 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2338 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2339 applyplen(&rrt->rrt_info.rip6_dest,
2341 category = "network";
2343 case P2PADVERT_ADDR:
2344 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2345 rrt->rrt_info.rip6_plen = 128;
2346 rrt->rrt_gw = in6addr_loopback;
2349 case P2PADVERT_DEST:
2350 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2351 rrt->rrt_info.rip6_plen = 128;
2352 rrt->rrt_gw = ifac->ifac_addr;
2356 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2357 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2359 trace(1, "route: %s: skip unspec/linklocal "
2360 "(%s on %s)\n", category, ifcp->ifc_name);
2365 if ((advert & i) == 0) {
2366 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2370 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2371 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2372 np = &rrt->rrt_info;
2373 orrt = rtsearch(np);
2375 /* Attach the route to the list */
2376 trace(1, "route: %s/%d: register route "
2378 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2379 category, ifcp->ifc_name, noadv);
2380 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2381 } else if (rrt->rrt_index != orrt->rrt_index ||
2382 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2384 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2385 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2388 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2389 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2390 category, ifcp->ifc_name, noadv);
2394 trace(1, "route: %s/%d: "
2395 "already registered (%s on %s%s)\n",
2396 inet6_n2p(&np->rip6_dest),
2397 np->rip6_plen, category,
2398 ifcp->ifc_name, noadv);
2404 #undef P2PADVERT_NETWORK
2405 #undef P2PADVERT_ADDR
2406 #undef P2PADVERT_DEST
2407 #undef P2PADVERT_MAX
2411 getifmtu(int ifindex)
2416 struct if_msghdr *ifm;
2423 mib[4] = NET_RT_IFLIST;
2425 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2426 fatal("sysctl estimate NET_RT_IFLIST");
2429 if ((buf = malloc(msize)) == NULL) {
2433 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2434 fatal("sysctl NET_RT_IFLIST");
2437 ifm = (struct if_msghdr *)buf;
2438 mtu = ifm->ifm_data.ifi_mtu;
2439 if (ifindex != ifm->ifm_index) {
2440 fatal("ifindex does not match with ifm_index");
2448 rttypes(struct rt_msghdr *rtm)
2450 #define RTTYPE(s, f) \
2452 if (rtm->rtm_type == (f)) \
2455 RTTYPE("ADD", RTM_ADD);
2456 RTTYPE("DELETE", RTM_DELETE);
2457 RTTYPE("CHANGE", RTM_CHANGE);
2458 RTTYPE("GET", RTM_GET);
2459 RTTYPE("LOSING", RTM_LOSING);
2460 RTTYPE("REDIRECT", RTM_REDIRECT);
2461 RTTYPE("MISS", RTM_MISS);
2462 RTTYPE("LOCK", RTM_LOCK);
2463 RTTYPE("NEWADDR", RTM_NEWADDR);
2464 RTTYPE("DELADDR", RTM_DELADDR);
2465 RTTYPE("IFINFO", RTM_IFINFO);
2467 RTTYPE("OIFINFO", RTM_OIFINFO);
2469 #ifdef RTM_IFANNOUNCE
2470 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2473 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2476 RTTYPE("DELMADDR", RTM_DELMADDR);
2483 rtflags(struct rt_msghdr *rtm)
2485 static char buf[BUFSIZ];
2488 * letter conflict should be okay. painful when *BSD diverges...
2490 strlcpy(buf, "", sizeof(buf));
2491 #define RTFLAG(s, f) \
2493 if (rtm->rtm_flags & (f)) \
2494 strlcat(buf, (s), sizeof(buf)); \
2496 RTFLAG("U", RTF_UP);
2497 RTFLAG("G", RTF_GATEWAY);
2498 RTFLAG("H", RTF_HOST);
2499 RTFLAG("R", RTF_REJECT);
2500 RTFLAG("D", RTF_DYNAMIC);
2501 RTFLAG("M", RTF_MODIFIED);
2502 RTFLAG("d", RTF_DONE);
2504 RTFLAG("m", RTF_MASK);
2507 RTFLAG("c", RTF_CLONED);
2509 RTFLAG("X", RTF_XRESOLVE);
2511 RTFLAG("L", RTF_LLINFO);
2513 RTFLAG("S", RTF_STATIC);
2514 RTFLAG("B", RTF_BLACKHOLE);
2516 RTFLAG("3", RTF_PROTO3);
2518 RTFLAG("2", RTF_PROTO2);
2519 RTFLAG("1", RTF_PROTO1);
2520 #ifdef RTF_BROADCAST
2521 RTFLAG("b", RTF_BROADCAST);
2524 RTFLAG("d", RTF_DEFAULT);
2526 #ifdef RTF_ISAROUTER
2527 RTFLAG("r", RTF_ISAROUTER);
2530 RTFLAG("T", RTF_TUNNEL);
2533 RTFLAG("A", RTF_AUTH);
2536 RTFLAG("E", RTF_CRYPT);
2545 static char buf[BUFSIZ];
2547 strlcpy(buf, "", sizeof(buf));
2548 #define IFFLAG(s, f) \
2550 if (flags & (f)) { \
2552 strlcat(buf, ",", sizeof(buf)); \
2553 strlcat(buf, (s), sizeof(buf)); \
2556 IFFLAG("UP", IFF_UP);
2557 IFFLAG("BROADCAST", IFF_BROADCAST);
2558 IFFLAG("DEBUG", IFF_DEBUG);
2559 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2560 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2561 #ifdef IFF_NOTRAILERS
2562 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2564 IFFLAG("RUNNING", IFF_RUNNING);
2565 IFFLAG("NOARP", IFF_NOARP);
2566 IFFLAG("PROMISC", IFF_PROMISC);
2567 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2568 IFFLAG("OACTIVE", IFF_OACTIVE);
2569 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2570 IFFLAG("LINK0", IFF_LINK0);
2571 IFFLAG("LINK1", IFF_LINK1);
2572 IFFLAG("LINK2", IFF_LINK2);
2573 IFFLAG("MULTICAST", IFF_MULTICAST);
2583 char *buf, *p, *lim;
2584 struct rt_msghdr *rtm;
2593 mib[3] = AF_INET6; /* Address family */
2594 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2595 mib[5] = 0; /* No flags */
2603 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2604 errmsg = "sysctl estimate";
2607 if ((buf = malloc(msize)) == NULL) {
2611 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2612 errmsg = "sysctl NET_RT_DUMP";
2615 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2617 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2620 } else if (1 < retry)
2621 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2624 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2625 rtm = (struct rt_msghdr *)p;
2626 rt_entry(rtm, again);
2632 rt_entry(struct rt_msghdr *rtm, int again)
2634 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2635 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2636 char *rtmp, *ifname = NULL;
2637 struct riprt *rrt, *orrt;
2638 struct netinfo6 *np;
2641 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2642 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2643 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2644 return; /* not interested in the link route */
2646 /* do not look at cloned routes */
2647 #ifdef RTF_WASCLONED
2648 if (rtm->rtm_flags & RTF_WASCLONED)
2652 if (rtm->rtm_flags & RTF_CLONED)
2655 /* XXX: Ignore connected routes. */
2656 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2659 * do not look at dynamic routes.
2660 * netbsd/openbsd cloned routes have UGHD.
2662 if (rtm->rtm_flags & RTF_DYNAMIC)
2664 rtmp = (char *)(rtm + 1);
2666 if ((rtm->rtm_addrs & RTA_DST) == 0)
2667 return; /* ignore routes without destination address */
2668 sin6_dst = (struct sockaddr_in6 *)rtmp;
2669 rtmp += ROUNDUP(sin6_dst->sin6_len);
2670 if (rtm->rtm_addrs & RTA_GATEWAY) {
2671 sin6_gw = (struct sockaddr_in6 *)rtmp;
2672 rtmp += ROUNDUP(sin6_gw->sin6_len);
2674 if (rtm->rtm_addrs & RTA_NETMASK) {
2675 sin6_mask = (struct sockaddr_in6 *)rtmp;
2676 rtmp += ROUNDUP(sin6_mask->sin6_len);
2678 if (rtm->rtm_addrs & RTA_GENMASK) {
2679 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2680 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2682 if (rtm->rtm_addrs & RTA_IFP) {
2683 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2684 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2688 if (sin6_dst->sin6_family != AF_INET6)
2690 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2691 return; /* Link-local */
2692 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2693 return; /* Loopback */
2694 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2697 if ((rrt = MALLOC(struct riprt)) == NULL) {
2698 fatal("malloc: struct riprt");
2701 memset(rrt, 0, sizeof(*rrt));
2702 np = &rrt->rrt_info;
2703 rrt->rrt_same = NULL;
2704 rrt->rrt_t = time(NULL);
2705 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2706 rrt->rrt_t = 0; /* Don't age static routes */
2707 if (rtm->rtm_flags & Pflag)
2708 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2709 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2710 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2712 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2713 if (np->rip6_metric < 1)
2714 np->rip6_metric = 1;
2715 rrt->rrt_flags = rtm->rtm_flags;
2716 np->rip6_dest = sin6_dst->sin6_addr;
2719 if (rtm->rtm_flags & RTF_HOST)
2720 np->rip6_plen = 128; /* Host route */
2722 np->rip6_plen = sin6mask2len(sin6_mask);
2726 orrt = rtsearch(np);
2727 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2730 trace(1, "route: %s/%d flags %s: already registered\n",
2731 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2739 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2741 if (sin6_gw->sin6_family == AF_INET6)
2742 rrt->rrt_gw = sin6_gw->sin6_addr;
2743 else if (sin6_gw->sin6_family == AF_LINK) {
2744 /* XXX in case ppp link? */
2745 rrt->rrt_gw = in6addr_loopback;
2747 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2749 trace(1, "route: %s/%d flags %s",
2750 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2751 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2754 ifindex = rtm->rtm_index;
2755 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2756 ifname = index2ifc[ifindex]->ifc_name;
2758 trace(1, " not configured\n");
2762 trace(1, " if %s sock %d", ifname, ifindex);
2763 rrt->rrt_index = ifindex;
2768 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2769 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2770 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2771 trace(0, "***** Gateway %s is not a link-local address.\n",
2772 inet6_n2p(&rrt->rrt_gw));
2773 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2774 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2775 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2778 /* Put it to the route list */
2779 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2780 /* replace route list */
2781 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2782 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2784 trace(1, "route: %s/%d flags %s: replace new route\n",
2785 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2789 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2793 addroute(struct riprt *rrt,
2794 const struct in6_addr *gw,
2797 struct netinfo6 *np;
2798 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2799 struct rt_msghdr *rtm;
2800 struct sockaddr_in6 *sin6;
2803 np = &rrt->rrt_info;
2804 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2805 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2806 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2807 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2808 np->rip6_metric - 1, buf2);
2810 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2811 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2812 np->rip6_metric - 1, buf2);
2816 memset(buf, 0, sizeof(buf));
2817 rtm = (struct rt_msghdr *)buf;
2818 rtm->rtm_type = RTM_ADD;
2819 rtm->rtm_version = RTM_VERSION;
2820 rtm->rtm_seq = ++seq;
2822 rtm->rtm_flags = rrt->rrt_flags;
2823 rtm->rtm_flags |= Qflag;
2824 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2825 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2826 rtm->rtm_inits = RTV_HOPCOUNT;
2827 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2829 sin6->sin6_len = sizeof(struct sockaddr_in6);
2830 sin6->sin6_family = AF_INET6;
2831 sin6->sin6_addr = np->rip6_dest;
2832 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2834 sin6->sin6_len = sizeof(struct sockaddr_in6);
2835 sin6->sin6_family = AF_INET6;
2836 sin6->sin6_addr = *gw;
2837 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2838 sin6->sin6_scope_id = ifcp->ifc_index;
2839 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2841 sin6->sin6_len = sizeof(struct sockaddr_in6);
2842 sin6->sin6_family = AF_INET6;
2843 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2844 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2846 len = (char *)sin6 - (char *)buf;
2847 rtm->rtm_msglen = len;
2848 if (write(rtsock, buf, len) > 0)
2851 if (errno == EEXIST) {
2852 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2853 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2855 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2856 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2858 trace(0, "Can not write to rtsock (addroute): %s\n",
2861 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2868 delroute(struct netinfo6 *np, struct in6_addr *gw)
2870 u_char buf[BUFSIZ], buf2[BUFSIZ];
2871 struct rt_msghdr *rtm;
2872 struct sockaddr_in6 *sin6;
2875 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2876 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2877 np->rip6_plen, buf2);
2879 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2880 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2884 memset(buf, 0, sizeof(buf));
2885 rtm = (struct rt_msghdr *)buf;
2886 rtm->rtm_type = RTM_DELETE;
2887 rtm->rtm_version = RTM_VERSION;
2888 rtm->rtm_seq = ++seq;
2890 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2891 rtm->rtm_flags |= Qflag;
2892 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2893 rtm->rtm_flags |= RTF_HOST;
2894 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2895 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2897 sin6->sin6_len = sizeof(struct sockaddr_in6);
2898 sin6->sin6_family = AF_INET6;
2899 sin6->sin6_addr = np->rip6_dest;
2900 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2902 sin6->sin6_len = sizeof(struct sockaddr_in6);
2903 sin6->sin6_family = AF_INET6;
2904 sin6->sin6_addr = *gw;
2905 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2907 sin6->sin6_len = sizeof(struct sockaddr_in6);
2908 sin6->sin6_family = AF_INET6;
2909 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2910 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2912 len = (char *)sin6 - (char *)buf;
2913 rtm->rtm_msglen = len;
2914 if (write(rtsock, buf, len) >= 0)
2917 if (errno == ESRCH) {
2918 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2919 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2921 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2922 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2924 trace(0, "Can not write to rtsock (delroute): %s\n",
2927 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2934 getroute(struct netinfo6 *np, struct in6_addr *gw)
2939 struct rt_msghdr *rtm;
2940 struct sockaddr_in6 *sin6;
2942 rtm = (struct rt_msghdr *)buf;
2943 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2944 memset(rtm, 0, len);
2945 rtm->rtm_type = RTM_GET;
2946 rtm->rtm_version = RTM_VERSION;
2948 rtm->rtm_seq = myseq;
2949 rtm->rtm_addrs = RTA_DST;
2950 rtm->rtm_msglen = len;
2951 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2952 sin6->sin6_len = sizeof(struct sockaddr_in6);
2953 sin6->sin6_family = AF_INET6;
2954 sin6->sin6_addr = np->rip6_dest;
2955 if (write(rtsock, buf, len) < 0) {
2956 if (errno == ESRCH) /* No such route found */
2958 perror("write to rtsock");
2962 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2963 perror("read from rtsock");
2966 rtm = (struct rt_msghdr *)buf;
2967 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2968 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2969 if (rtm->rtm_addrs & RTA_DST) {
2970 sin6 = (struct sockaddr_in6 *)
2971 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2973 if (rtm->rtm_addrs & RTA_GATEWAY) {
2974 *gw = sin6->sin6_addr;
2981 inet6_n2p(const struct in6_addr *p)
2983 static char buf[BUFSIZ];
2985 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3006 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3009 fprintf(dump, "%s: Interface Table Dump\n", hms());
3010 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3012 fprintf(dump, " Number of interfaces: %d\n", nifc);
3014 fprintf(dump, " advertising interfaces:\n");
3015 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3016 if ((ifcp->ifc_flags & IFF_UP) == 0)
3018 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3020 ifdump0(dump, ifcp);
3022 fprintf(dump, "\n");
3023 fprintf(dump, " non-advertising interfaces:\n");
3024 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3025 if ((ifcp->ifc_flags & IFF_UP) &&
3026 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3028 ifdump0(dump, ifcp);
3030 fprintf(dump, "\n");
3036 ifdump0(FILE *dump, const struct ifc *ifcp)
3044 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3045 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3046 inet6_n2p(&ifcp->ifc_mylladdr),
3047 ifcp->ifc_mtu, ifcp->ifc_metric);
3048 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3049 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3050 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3052 fprintf(dump, "\t%s/%d -- %s\n",
3053 inet6_n2p(&ifac->ifac_addr),
3054 ifac->ifac_plen, buf);
3056 fprintf(dump, "\t%s/%d\n",
3057 inet6_n2p(&ifac->ifac_addr),
3062 fprintf(dump, "\tFilter:\n");
3063 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3065 switch (iffp->iff_type) {
3067 ft = "Aggregate"; addr++; break;
3069 ft = "No-use"; break;
3071 ft = "Advertise-only"; addr++; break;
3073 ft = "Default-only"; break;
3075 ft = "Listen-only"; addr++; break;
3077 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3082 fprintf(dump, "\t\t%s", ft);
3084 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3086 fprintf(dump, "\n");
3088 fprintf(dump, "\n");
3102 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3106 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3107 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3108 if (rrt->rrt_t == 0)
3111 age = t - rrt->rrt_t;
3112 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3114 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3115 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3116 index2ifc[rrt->rrt_index]->ifc_name,
3117 inet6_n2p(&rrt->rrt_gw),
3118 rrt->rrt_info.rip6_metric, (long)age);
3119 if (rrt->rrt_info.rip6_tag) {
3120 fprintf(dump, " tag(0x%04x)",
3121 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3123 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3124 fprintf(dump, " NOT-LL");
3125 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3126 fprintf(dump, " NO-ADV");
3127 fprintf(dump, "\n");
3129 fprintf(dump, "\n");
3135 * Parse the -A (and -O) options and put corresponding filter object to the
3136 * specified interface structures. Each of the -A/O option has the following
3137 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3138 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3144 char *p, *ap, *iflp, *ifname, *ep;
3145 struct iff iff, *iffp;
3153 for (i = 0; i < nfilter; i++) {
3157 memset(iffp, 0, sizeof(*iffp));
3158 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3162 if ((p = strchr(ap, ',')) != NULL) {
3166 if ((p = strchr(ap, '/')) == NULL) {
3167 fatal("no prefixlen specified for '%s'", ap);
3171 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3172 fatal("invalid prefix specified for '%s'", ap);
3177 plen = strtoul(p, &ep, 10);
3178 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3179 fatal("invalid prefix length specified for '%s'", ap);
3182 iffp->iff_plen = plen;
3183 applyplen(&iffp->iff_addr, iffp->iff_plen);
3185 iffp->iff_type = filtertype[i];
3186 if (iflp == NULL || *iflp == '\0') {
3187 fatal("no interface specified for '%s'", ap);
3190 /* parse the interface listing portion */
3193 if ((iflp = strchr(iflp, ',')) != NULL)
3196 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3197 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3200 iffp = malloc(sizeof(*iffp));
3202 fatal("malloc of iff");
3205 memcpy(iffp, &iff, sizeof(*iffp));
3207 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3209 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3214 * -A: aggregate configuration.
3216 if (filtertype[i] != IFIL_TYPE_A)
3218 /* put the aggregate to the kernel routing table */
3219 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3221 fatal("malloc: rrt");
3224 memset(rrt, 0, sizeof(struct riprt));
3225 rrt->rrt_info.rip6_dest = iff.iff_addr;
3226 rrt->rrt_info.rip6_plen = iff.iff_plen;
3227 rrt->rrt_info.rip6_metric = 1;
3228 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3229 rrt->rrt_gw = in6addr_loopback;
3230 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3231 rrt->rrt_rflags = RRTF_AGGREGATE;
3233 rrt->rrt_index = loopifcp->ifc_index;
3235 if (getroute(&rrt->rrt_info, &gw)) {
3238 * When the address has already been registered in the
3239 * kernel routing table, it should be removed
3241 delroute(&rrt->rrt_info, &gw);
3243 /* it is safer behavior */
3245 fatal("%s/%u already in routing table, "
3247 inet6_n2p(&rrt->rrt_info.rip6_dest),
3248 rrt->rrt_info.rip6_plen);
3253 /* Put the route to the list */
3254 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3255 trace(1, "Aggregate: %s/%d for %s\n",
3256 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3257 loopifcp->ifc_name);
3258 /* Add this route to the kernel */
3259 if (nflag) /* do not modify kernel routing table */
3261 addroute(rrt, &in6addr_loopback, loopifcp);
3265 /***************** utility functions *****************/
3268 * Returns a pointer to ifac whose address and prefix length matches
3269 * with the address and prefix length specified in the arguments.
3272 ifa_match(const struct ifc *ifcp,
3273 const struct in6_addr *ia,
3278 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3279 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3280 ifac->ifac_plen == plen)
3288 * Return a pointer to riprt structure whose address and prefix length
3289 * matches with the address and prefix length found in the argument.
3290 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3293 rtsearch(struct netinfo6 *np)
3297 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3298 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3299 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3308 sin6mask2len(const struct sockaddr_in6 *sin6)
3311 return mask2len(&sin6->sin6_addr,
3312 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3316 mask2len(const struct in6_addr *addr, int lenlim)
3319 const u_char *p = (const u_char *)addr;
3321 for (j = 0; j < lenlim; j++, p++) {
3328 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3329 MASKLEN(0xfe, 7); break;
3330 MASKLEN(0xfc, 6); break;
3331 MASKLEN(0xf8, 5); break;
3332 MASKLEN(0xf0, 4); break;
3333 MASKLEN(0xe0, 3); break;
3334 MASKLEN(0xc0, 2); break;
3335 MASKLEN(0x80, 1); break;
3343 applymask(struct in6_addr *addr, struct in6_addr *mask)
3348 p = (u_long *)addr; q = (u_long *)mask;
3349 for (i = 0; i < 4; i++)
3353 static const u_char plent[8] = {
3354 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3358 applyplen(struct in6_addr *ia, int plen)
3364 for (i = 0; i < 16; i++) {
3373 static const int pl2m[9] = {
3374 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3380 static struct in6_addr ia;
3384 memset(&ia, 0, sizeof(struct in6_addr));
3386 for (i = 0; i < 16; i++, p++, n -= 8) {
3400 int len = strlen(p) + 1;
3401 char *q = (char *)malloc(len);
3415 static char buf[BUFSIZ];
3420 if ((tm = localtime(&t)) == 0) {
3424 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3429 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3432 ripinterval(int timer)
3436 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3437 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, ...)
3457 fatal(fmt, va_alist)
3470 vsnprintf(buf, sizeof(buf), fmt, ap);
3474 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3476 syslog(LOG_ERR, "%s", buf);
3482 tracet(int level, const char *fmt, ...)
3484 tracet(level, fmt, va_alist)
3492 if (level <= dflag) {
3498 fprintf(stderr, "%s: ", hms());
3499 vfprintf(stderr, fmt, ap);
3509 vsyslog(LOG_DEBUG, fmt, ap);
3511 vsyslog(LOG_WARNING, fmt, ap);
3518 trace(int level, const char *fmt, ...)
3520 trace(level, fmt, va_alist)
3528 if (level <= dflag) {
3534 vfprintf(stderr, fmt, ap);
3544 vsyslog(LOG_DEBUG, fmt, ap);
3546 vsyslog(LOG_WARNING, fmt, ap);
3554 struct if_nameindex *p, *p0;
3555 unsigned int max = 0;
3557 p0 = if_nameindex();
3558 for (p = p0; p && p->if_index && p->if_name; p++) {
3559 if (max < p->if_index)
3562 if_freenameindex(p0);
3567 ifc_find(char *name)
3571 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3572 if (strcmp(name, ifcp->ifc_name) == 0)
3579 iff_find(struct ifc *ifcp, int type)
3583 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3584 if (type == IFIL_TYPE_ANY ||
3585 type == iffp->iff_type)
3593 setindex2ifc(int idx, struct ifc *ifcp)
3599 nindex2ifc = 5; /*initial guess*/
3600 index2ifc = (struct ifc **)
3601 malloc(sizeof(*index2ifc) * nindex2ifc);
3602 if (index2ifc == NULL) {
3606 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3609 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3612 p = (struct ifc **)realloc(index2ifc,
3613 sizeof(*index2ifc) * nsize);
3618 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3622 index2ifc[idx] = ifcp;