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/if_var.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip6.h>
50 #include <netinet/udp.h>
76 #define RT_DUMP_MAXRETRY 15
79 #define INIT_INTERVAL6 6
81 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
84 /* alignment constraint for routing socket */
86 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
87 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
89 struct ifc { /* Configuration of an interface */
90 TAILQ_ENTRY(ifc) ifc_next;
92 char ifc_name[IFNAMSIZ]; /* if name */
93 int ifc_index; /* if index */
94 int ifc_mtu; /* if mtu */
95 int ifc_metric; /* if metric */
96 u_int ifc_flags; /* flags */
97 short ifc_cflags; /* IFC_XXX */
98 struct in6_addr ifc_mylladdr; /* my link-local address */
99 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
100 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
101 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
102 int ifc_joined; /* joined to ff02::9 */
104 TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
106 struct ifac { /* Adddress associated to an interface */
107 TAILQ_ENTRY(ifac) ifac_next;
109 struct ifc *ifac_ifc; /* back pointer */
110 struct in6_addr ifac_addr; /* address */
111 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
112 int ifac_scope_id; /* scope id */
113 int ifac_plen; /* prefix length */
116 struct iff { /* Filters for an interface */
117 TAILQ_ENTRY(iff) iff_next;
120 struct in6_addr iff_addr;
124 struct ifc **index2ifc;
125 unsigned int nindex2ifc;
126 struct ifc *loopifcp = NULL; /* pointing to loopback */
128 struct pollfd set[2];
130 fd_set *sockvecp; /* vector to select() for receiving */
133 int maxfd; /* maximum fd for select() */
135 int rtsock; /* the routing socket */
136 int ripsock; /* socket to send/receive RIP datagram */
138 struct rip6 *ripbuf; /* packet buffer for sending */
141 * Maintain the routes in a linked list. When the number of the routes
142 * grows, somebody would like to introduce a hash based or a radix tree
143 * based structure. I believe the number of routes handled by RIP is
144 * limited and I don't have to manage a complex data structure, however.
146 * One of the major drawbacks of the linear linked list is the difficulty
147 * of representing the relationship between a couple of routes. This may
148 * be a significant problem when we have to support route aggregation with
149 * suppressing the specifics covered by the aggregate.
153 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
155 struct riprt *rrt_same; /* same destination - future use */
156 struct netinfo6 rrt_info; /* network info */
157 struct in6_addr rrt_gw; /* gateway */
158 u_long rrt_flags; /* kernel routing table flags */
159 u_long rrt_rflags; /* route6d routing table flags */
160 time_t rrt_t; /* when the route validated */
161 int rrt_index; /* ifindex from which this route got */
163 TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
165 int dflag = 0; /* debug flag */
166 int qflag = 0; /* quiet flag */
167 int nflag = 0; /* don't update kernel routing table */
168 int aflag = 0; /* age out even the statically defined routes */
169 int hflag = 0; /* don't split horizon */
170 int lflag = 0; /* exchange site local routes */
171 int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
172 int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
173 int sflag = 0; /* announce static routes w/ split horizon */
174 int Sflag = 0; /* announce static routes to every interface */
175 unsigned long routetag = 0; /* route tag attached on originating case */
177 char *filter[MAXFILTER];
178 int filtertype[MAXFILTER];
183 struct sockaddr_storage ripsin;
186 time_t nextalarm = 0;
187 time_t sup_trig_update = 0;
195 volatile sig_atomic_t seenalrm;
196 volatile sig_atomic_t seenquit;
197 volatile sig_atomic_t seenusr1;
199 #define RRTF_AGGREGATE 0x08000000
200 #define RRTF_NOADVERTISE 0x10000000
201 #define RRTF_NH_NOT_LLADDR 0x20000000
202 #define RRTF_SENDANYWAY 0x40000000
203 #define RRTF_CHANGED 0x80000000
205 int main(int, char **);
206 void sighandler(int);
209 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
210 int out_filter(struct riprt *, struct ifc *);
212 void sockopt(struct ifc *);
214 int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
216 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
217 const struct sockaddr_in6 *);
218 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
219 const struct sockaddr_in6 *);
220 void filterconfig(void);
222 const char *rttypes(struct rt_msghdr *);
223 const char *rtflags(struct rt_msghdr *);
224 const char *ifflags(int);
225 int ifrt(struct ifc *, int);
226 void ifrt_p2p(struct ifc *, int);
227 void applymask(struct in6_addr *, struct in6_addr *);
228 void applyplen(struct in6_addr *, int);
231 void ifdump0(FILE *, const struct ifc *);
234 void rt_entry(struct rt_msghdr *, int);
236 void riprequest(struct ifc *, struct netinfo6 *, int,
237 struct sockaddr_in6 *);
238 void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
239 void sendrequest(struct ifc *);
240 int sin6mask2len(const struct sockaddr_in6 *);
241 int mask2len(const struct in6_addr *, int);
242 int sendpacket(struct sockaddr_in6 *, int);
243 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
244 int delroute(struct netinfo6 *, struct in6_addr *);
245 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
247 int tobeadv(struct riprt *, struct ifc *);
248 char *allocopy(char *);
250 const char *inet6_n2p(const struct in6_addr *);
251 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
252 struct in6_addr *plen2mask(int);
253 struct riprt *rtsearch(struct netinfo6 *);
254 int ripinterval(int);
255 time_t ripsuptrig(void);
256 void fatal(const char *, ...)
257 __attribute__((__format__(__printf__, 1, 2)));
258 void trace(int, const char *, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260 void tracet(int, const char *, ...)
261 __attribute__((__format__(__printf__, 2, 3)));
262 unsigned int if_maxindex(void);
263 struct ifc *ifc_find(char *);
264 struct iff *iff_find(struct ifc *, int);
265 void setindex2ifc(int, struct ifc *);
267 #define MALLOC(type) ((type *)malloc(sizeof(type)))
269 #define IFIL_TYPE_ANY 0x0
270 #define IFIL_TYPE_A 'A'
271 #define IFIL_TYPE_N 'N'
272 #define IFIL_TYPE_T 'T'
273 #define IFIL_TYPE_O 'O'
274 #define IFIL_TYPE_L 'L'
277 main(int argc, char *argv[])
283 sigset_t mask, omask;
284 const char *pidfile = ROUTE6D_PID;
289 progname = strrchr(*argv, '/');
296 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
303 if (nfilter >= MAXFILTER) {
304 fatal("Exceeds MAXFILTER");
307 filtertype[nfilter] = ch;
308 filter[nfilter++] = allocopy(optarg);
312 routetag = strtoul(optarg, &ep, 0);
313 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
314 fatal("invalid route tag");
323 proto = strtoul(optarg, &ep, 0);
324 if (!ep || *ep != '\0' || 3 < proto) {
325 fatal("invalid P flag");
339 proto = strtoul(optarg, &ep, 0);
340 if (!ep || *ep != '\0' || 3 < proto) {
341 fatal("invalid Q flag");
354 if ((rtlog = fopen(optarg, "w")) == NULL) {
355 fatal("Can not write to routelog");
359 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
360 FLAG('a', aflag, 1); break;
361 FLAG('d', dflag, 1); break;
362 FLAG('D', dflag, 2); break;
363 FLAG('h', hflag, 1); break;
364 FLAG('l', lflag, 1); break;
365 FLAG('n', nflag, 1); break;
366 FLAG('q', qflag, 1); break;
367 FLAG('s', sflag, 1); break;
368 FLAG('S', Sflag, 1); break;
371 fatal("Invalid option specified, terminating");
378 fatal("bogus extra arguments");
384 fprintf(stderr, "No kernel update is allowed\n");
388 if (daemon(0, 0) < 0) {
394 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
397 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
399 memset(ripbuf, 0, RIP6_MAXMTU);
400 ripbuf->rip6_cmd = RIP6_RESPONSE;
401 ripbuf->rip6_vers = RIP6_VERSION;
402 ripbuf->rip6_res1[0] = 0;
403 ripbuf->rip6_res1[1] = 0;
407 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
408 if (ifcp->ifc_index < 0) {
409 fprintf(stderr, "No ifindex found at %s "
410 "(no link-local address?)\n", ifcp->ifc_name);
416 if (loopifcp == NULL) {
417 fatal("No loopback found");
420 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
429 if ((pidfh = fopen(pidfile, "w")) != NULL) {
430 fprintf(pidfh, "%d\n", pid);
434 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
438 memset(ripbuf, 0, RIP6_MAXMTU);
439 ripbuf->rip6_cmd = RIP6_RESPONSE;
440 ripbuf->rip6_vers = RIP6_VERSION;
441 ripbuf->rip6_res1[0] = 0;
442 ripbuf->rip6_res1[1] = 0;
444 if (signal(SIGALRM, sighandler) == SIG_ERR ||
445 signal(SIGQUIT, sighandler) == SIG_ERR ||
446 signal(SIGTERM, sighandler) == SIG_ERR ||
447 signal(SIGUSR1, sighandler) == SIG_ERR ||
448 signal(SIGHUP, sighandler) == SIG_ERR ||
449 signal(SIGINT, sighandler) == SIG_ERR) {
454 * To avoid rip packet congestion (not on a cable but in this
455 * process), wait for a moment to send the first RIP6_RESPONSE
458 alarm(ripinterval(INIT_INTERVAL6));
460 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
461 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
463 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
467 syslog(LOG_INFO, "**** Started ****");
469 sigaddset(&mask, SIGALRM);
488 switch (poll(set, 2, INFTIM))
490 memcpy(recvecp, sockvecp, fdmasks);
491 switch (select(maxfd + 1, recvecp, 0, 0, 0))
495 if (errno != EINTR) {
504 if (set[0].revents & POLLIN)
506 if (FD_ISSET(ripsock, recvecp))
509 sigprocmask(SIG_BLOCK, &mask, &omask);
511 sigprocmask(SIG_SETMASK, &omask, NULL);
514 if (set[1].revents & POLLIN)
516 if (FD_ISSET(rtsock, recvecp))
519 sigprocmask(SIG_BLOCK, &mask, &omask);
521 sigprocmask(SIG_SETMASK, &omask, NULL);
528 sighandler(int signo)
548 * gracefully exits after resetting sockopts.
557 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
558 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
559 delroute(&rrt->rrt_info, &rrt->rrt_gw);
564 syslog(LOG_INFO, "**** Terminated ****");
570 * Called periodically:
571 * 1. age out the learned route. remove it if necessary.
572 * 2. submit RIP6_RESPONSE packets.
573 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
574 * to invoke this function in every 1 or 5 or 10 seconds only to age the
575 * routes more precisely.
582 struct riprt *rrt, *rrt_tmp;
583 time_t t_lifetime, t_holddown;
585 /* age the RIP routes */
586 t_lifetime = time(NULL) - RIP_LIFETIME;
587 t_holddown = t_lifetime - RIP_HOLDDOWN;
588 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
591 else if (rrt->rrt_t < t_holddown) {
592 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
593 delroute(&rrt->rrt_info, &rrt->rrt_gw);
595 } else if (rrt->rrt_t < t_lifetime)
596 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
599 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
600 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
601 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
603 alarm(ripinterval(SUPPLY_INTERVAL6));
610 const int int0 = 0, int1 = 1, int255 = 255;
611 struct addrinfo hints, *res;
612 char port[NI_MAXSERV];
614 TAILQ_INIT(&ifc_head);
615 nindex2ifc = 0; /*initial guess*/
617 snprintf(port, sizeof(port), "%u", RIP6_PORT);
619 memset(&hints, 0, sizeof(hints));
620 hints.ai_family = PF_INET6;
621 hints.ai_socktype = SOCK_DGRAM;
622 hints.ai_protocol = IPPROTO_UDP;
623 hints.ai_flags = AI_PASSIVE;
624 error = getaddrinfo(NULL, port, &hints, &res);
626 fatal("%s", gai_strerror(error));
630 fatal(":: resolved to multiple address");
634 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
640 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
641 &int1, sizeof(int1)) < 0) {
642 fatal("rip IPV6_V6ONLY");
646 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
650 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
651 &int255, sizeof(int255)) < 0) {
652 fatal("rip IPV6_MULTICAST_HOPS");
655 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
656 &int0, sizeof(int0)) < 0) {
657 fatal("rip IPV6_MULTICAST_LOOP");
661 #ifdef IPV6_RECVPKTINFO
662 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
663 &int1, sizeof(int1)) < 0) {
664 fatal("rip IPV6_RECVPKTINFO");
667 #else /* old adv. API */
668 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
669 &int1, sizeof(int1)) < 0) {
670 fatal("rip IPV6_PKTINFO");
675 #ifdef IPV6_RECVPKTINFO
676 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
677 &int1, sizeof(int1)) < 0) {
678 fatal("rip IPV6_RECVHOPLIMIT");
681 #else /* old adv. API */
682 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
683 &int1, sizeof(int1)) < 0) {
684 fatal("rip IPV6_HOPLIMIT");
689 memset(&hints, 0, sizeof(hints));
690 hints.ai_family = PF_INET6;
691 hints.ai_socktype = SOCK_DGRAM;
692 hints.ai_protocol = IPPROTO_UDP;
693 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
695 fatal("%s", gai_strerror(error));
699 fatal("%s resolved to multiple address", RIP6_DEST);
702 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
706 set[0].events = POLLIN;
712 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
713 fatal("route socket");
718 set[1].events = POLLIN;
727 rtsock = -1; /*just for safety */
732 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
733 if ((sockvecp = malloc(fdmasks)) == NULL) {
737 if ((recvecp = malloc(fdmasks)) == NULL) {
741 memset(sockvecp, 0, fdmasks);
742 FD_SET(ripsock, sockvecp);
744 FD_SET(rtsock, sockvecp);
749 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
752 * ripflush flushes the rip datagram stored in the rip buffer
755 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
761 tracet(1, "Send(%s): info(%d) to %s.%d\n",
763 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
765 tracet(1, "Send: info(%d) to %s.%d\n",
766 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
768 np = ripbuf->rip6_nets;
769 for (i = 0; i < nrt; i++, np++) {
770 if (np->rip6_metric == NEXTHOP_METRIC) {
771 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
772 trace(2, " NextHop reset");
774 trace(2, " NextHop %s",
775 inet6_n2p(&np->rip6_dest));
778 trace(2, " %s/%d[%d]",
779 inet6_n2p(&np->rip6_dest),
780 np->rip6_plen, np->rip6_metric);
783 trace(2, " tag=0x%04x",
784 ntohs(np->rip6_tag) & 0xffff);
789 error = sendpacket(sin6, RIPSIZE(nrt));
790 if (error == EAFNOSUPPORT) {
791 /* Protocol not supported */
792 tracet(1, "Could not send info to %s (%s): "
794 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
795 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
800 * Generate RIP6_RESPONSE packets and send them.
803 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
806 struct in6_addr *nh; /* next hop */
816 * Request from non-link local address is not
817 * a regular route6d update.
819 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
820 sizeof(struct udphdr) -
821 sizeof(struct rip6) + sizeof(struct netinfo6)) /
822 sizeof(struct netinfo6);
825 np = ripbuf->rip6_nets;
826 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
827 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
829 /* Put the route to the buffer */
833 ripflush(NULL, sin6, nrt, np);
836 np = ripbuf->rip6_nets;
839 if (nrt) /* Send last packet */
840 ripflush(NULL, sin6, nrt, np);
844 if ((flag & RRTF_SENDANYWAY) == 0 &&
845 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
849 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
852 /* -T: generate default route only */
853 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
854 struct netinfo6 rrt_info;
855 memset(&rrt_info, 0, sizeof(struct netinfo6));
856 rrt_info.rip6_dest = in6addr_any;
857 rrt_info.rip6_plen = 0;
858 rrt_info.rip6_metric = 1;
859 rrt_info.rip6_metric += ifcp->ifc_metric;
860 rrt_info.rip6_tag = htons(routetag & 0xffff);
861 np = ripbuf->rip6_nets;
864 ripflush(ifcp, sin6, nrt, np);
868 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
869 sizeof(struct udphdr) -
870 sizeof(struct rip6) + sizeof(struct netinfo6)) /
871 sizeof(struct netinfo6);
873 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
874 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
875 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
878 /* Need to check filter here */
879 if (out_filter(rrt, ifcp) == 0)
882 /* Check split horizon and other conditions */
883 if (tobeadv(rrt, ifcp) == 0)
886 /* Only considers the routes with flag if specified */
887 if ((flag & RRTF_CHANGED) &&
888 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
892 if (rrt->rrt_index == ifcp->ifc_index &&
893 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
894 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
895 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
896 if (nrt == maxrte - 2) {
897 ripflush(ifcp, sin6, nrt, np);
900 np = ripbuf->rip6_nets;
903 np->rip6_dest = rrt->rrt_gw;
906 np->rip6_metric = NEXTHOP_METRIC;
910 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
911 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
912 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
914 if (nrt == maxrte - 2) {
915 ripflush(ifcp, sin6, nrt, np);
918 np = ripbuf->rip6_nets;
920 memset(np, 0, sizeof(struct netinfo6));
921 np->rip6_metric = NEXTHOP_METRIC;
926 /* Put the route to the buffer */
930 ripflush(ifcp, sin6, nrt, np);
933 np = ripbuf->rip6_nets;
936 if (nrt) /* Send last packet */
937 ripflush(ifcp, sin6, nrt, np);
941 * outbound filter logic, per-route/interface.
944 out_filter(struct riprt *rrt, struct ifc *ifcp)
951 * -A: filter out less specific routes, if we have aggregated
954 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
955 if (iffp->iff_type != 'A')
957 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
959 ia = rrt->rrt_info.rip6_dest;
960 applyplen(&ia, iffp->iff_plen);
961 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
966 * if it is an aggregated route, advertise it only to the
967 * interfaces specified on -A.
969 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
971 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
972 if (iffp->iff_type != 'A')
974 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
975 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
986 * -O: advertise only if prefix matches the configured prefix.
988 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
990 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
991 if (iffp->iff_type != 'O')
993 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
995 ia = rrt->rrt_info.rip6_dest;
996 applyplen(&ia, iffp->iff_plen);
997 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1006 /* the prefix should be advertised */
1011 * Determine if the route is to be advertised on the specified interface.
1012 * It checks options specified in the arguments and the split horizon rule.
1015 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1018 /* Special care for static routes */
1019 if (rrt->rrt_flags & RTF_STATIC) {
1020 /* XXX don't advertise reject/blackhole routes */
1021 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1024 if (Sflag) /* Yes, advertise it anyway */
1026 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1030 /* Regular split horizon */
1031 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1037 * Send a rip packet actually.
1040 sendpacket(struct sockaddr_in6 *sin6, int len)
1044 struct iovec iov[2];
1045 u_char cmsgbuf[256];
1046 struct in6_pktinfo *pi;
1048 struct sockaddr_in6 sincopy;
1050 /* do not overwrite the given sin */
1054 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1055 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1056 idx = sin6->sin6_scope_id;
1060 m.msg_name = (caddr_t)sin6;
1061 m.msg_namelen = sizeof(*sin6);
1062 iov[0].iov_base = (caddr_t)ripbuf;
1063 iov[0].iov_len = len;
1068 m.msg_control = NULL;
1069 m.msg_controllen = 0;
1071 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1072 cm = (struct cmsghdr *)cmsgbuf;
1073 m.msg_control = (caddr_t)cm;
1074 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1076 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1077 cm->cmsg_level = IPPROTO_IPV6;
1078 cm->cmsg_type = IPV6_PKTINFO;
1079 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1080 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1081 pi->ipi6_ifindex = idx;
1084 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1085 trace(1, "sendmsg: %s\n", strerror(errno));
1093 * Receive and process RIP packets. Update the routes/kernel forwarding
1094 * table if necessary.
1099 struct ifc *ifcp, *ic;
1100 struct sockaddr_in6 fsock;
1101 struct in6_addr nh; /* next hop */
1103 struct netinfo6 *np, *nq;
1106 unsigned int need_trigger, idx;
1107 char buf[4 * RIP6_MAXMTU];
1111 struct iovec iov[2];
1112 u_char cmsgbuf[256];
1113 struct in6_pktinfo *pi = NULL;
1118 time_t t_half_lifetime;
1122 m.msg_name = (caddr_t)&fsock;
1123 m.msg_namelen = sizeof(fsock);
1124 iov[0].iov_base = (caddr_t)buf;
1125 iov[0].iov_len = sizeof(buf);
1128 cm = (struct cmsghdr *)cmsgbuf;
1129 m.msg_control = (caddr_t)cm;
1130 m.msg_controllen = sizeof(cmsgbuf);
1132 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1137 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1139 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1140 if (cm->cmsg_level != IPPROTO_IPV6)
1142 switch (cm->cmsg_type) {
1144 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1146 "invalid cmsg length for IPV6_PKTINFO\n");
1149 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1150 idx = pi->ipi6_ifindex;
1153 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1155 "invalid cmsg length for IPV6_HOPLIMIT\n");
1158 hlimp = (int *)CMSG_DATA(cm);
1163 if ((size_t)len < sizeof(struct rip6)) {
1164 trace(1, "Packet too short\n");
1168 if (pi == NULL || hlimp == NULL) {
1170 * This can happen when the kernel failed to allocate memory
1171 * for the ancillary data. Although we might be able to handle
1172 * some cases without this info, those are minor and not so
1173 * important, so it's better to discard the packet for safer
1176 trace(1, "IPv6 packet information cannot be retrieved\n");
1180 nh = fsock.sin6_addr;
1181 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1182 sizeof(struct netinfo6);
1183 rp = (struct rip6 *)buf;
1186 if (rp->rip6_vers != RIP6_VERSION) {
1187 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1190 if (rp->rip6_cmd == RIP6_REQUEST) {
1191 if (idx && idx < nindex2ifc) {
1192 ifcp = index2ifc[idx];
1193 riprequest(ifcp, np, nn, &fsock);
1195 riprequest(NULL, np, nn, &fsock);
1200 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1201 trace(1, "Response from non-ll addr: %s\n",
1202 inet6_n2p(&fsock.sin6_addr));
1203 return; /* Ignore packets from non-link-local addr */
1205 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1206 trace(1, "Response from non-rip port from %s\n",
1207 inet6_n2p(&fsock.sin6_addr));
1210 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1212 "Response packet with a smaller hop limit (%d) from %s\n",
1213 *hlimp, inet6_n2p(&fsock.sin6_addr));
1217 * Further validation: since this program does not send off-link
1218 * requests, an incoming response must always come from an on-link
1219 * node. Although this is normally ensured by the source address
1220 * check above, it may not 100% be safe because there are router
1221 * implementations that (invalidly) allow a packet with a link-local
1222 * source address to be forwarded to a different link.
1223 * So we also check whether the destination address is a link-local
1224 * address or the hop limit is 255. Note that RFC2080 does not require
1225 * the specific hop limit for a unicast response, so we cannot assume
1228 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1230 "Response packet possibly from an off-link node: "
1231 "from %s to %s hlim=%d\n",
1232 inet6_n2p(&fsock.sin6_addr),
1233 inet6_n2p(&pi->ipi6_addr), *hlimp);
1237 idx = fsock.sin6_scope_id;
1238 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1240 trace(1, "Packets to unknown interface index %d\n", idx);
1241 return; /* Ignore it */
1243 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1244 return; /* The packet is from me; ignore */
1245 if (rp->rip6_cmd != RIP6_RESPONSE) {
1246 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1251 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1254 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1255 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1258 t_half_lifetime = t - (RIP_LIFETIME/2);
1259 for (; nn; nn--, np++) {
1260 if (np->rip6_metric == NEXTHOP_METRIC) {
1261 /* modify neighbor address */
1262 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1264 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1265 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1266 nh = fsock.sin6_addr;
1267 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1269 nh = fsock.sin6_addr;
1270 trace(1, "\tInvalid Nexthop: %s\n",
1271 inet6_n2p(&np->rip6_dest));
1275 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1276 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1277 inet6_n2p(&np->rip6_dest),
1278 np->rip6_plen, np->rip6_metric);
1281 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1282 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1283 inet6_n2p(&np->rip6_dest),
1284 np->rip6_plen, np->rip6_metric);
1287 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1288 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1289 inet6_n2p(&np->rip6_dest),
1290 np->rip6_plen, np->rip6_metric);
1293 /* may need to pass sitelocal prefix in some case, however*/
1294 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1295 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1296 inet6_n2p(&np->rip6_dest),
1297 np->rip6_plen, np->rip6_metric);
1300 trace(2, "\tnetinfo6: %s/%d [%d]",
1301 inet6_n2p(&np->rip6_dest),
1302 np->rip6_plen, np->rip6_metric);
1304 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1307 applyplen(&ia, np->rip6_plen);
1308 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1309 trace(2, " [junk outside prefix]");
1313 * -L: listen only if the prefix matches the configuration
1315 ok = 1; /* if there's no L filter, it is ok */
1316 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1317 if (iffp->iff_type != IFIL_TYPE_L)
1320 if (np->rip6_plen < iffp->iff_plen)
1322 /* special rule: ::/0 means default, not "in /0" */
1323 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1326 applyplen(&ia, iffp->iff_plen);
1327 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1333 trace(2, " (filtered)\n");
1339 np->rip6_metric += ifcp->ifc_metric;
1340 if (np->rip6_metric > HOPCNT_INFINITY6)
1341 np->rip6_metric = HOPCNT_INFINITY6;
1343 applyplen(&np->rip6_dest, np->rip6_plen);
1344 if ((rrt = rtsearch(np)) != NULL) {
1345 if (rrt->rrt_t == 0)
1346 continue; /* Intf route has priority */
1347 nq = &rrt->rrt_info;
1348 if (nq->rip6_metric > np->rip6_metric) {
1349 if (rrt->rrt_index == ifcp->ifc_index &&
1350 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1351 /* Small metric from the same gateway */
1352 nq->rip6_metric = np->rip6_metric;
1354 /* Better route found */
1355 rrt->rrt_index = ifcp->ifc_index;
1356 /* Update routing table */
1357 delroute(nq, &rrt->rrt_gw);
1360 addroute(rrt, &nh, ifcp);
1362 rrt->rrt_rflags |= RRTF_CHANGED;
1365 } else if (nq->rip6_metric < np->rip6_metric &&
1366 rrt->rrt_index == ifcp->ifc_index &&
1367 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1368 /* Got worse route from same gw */
1369 nq->rip6_metric = np->rip6_metric;
1371 rrt->rrt_rflags |= RRTF_CHANGED;
1373 } else if (nq->rip6_metric == np->rip6_metric &&
1374 np->rip6_metric < HOPCNT_INFINITY6) {
1375 if (rrt->rrt_index == ifcp->ifc_index &&
1376 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1377 /* same metric, same route from same gw */
1379 } else if (rrt->rrt_t < t_half_lifetime) {
1380 /* Better route found */
1381 rrt->rrt_index = ifcp->ifc_index;
1382 /* Update routing table */
1383 delroute(nq, &rrt->rrt_gw);
1386 addroute(rrt, &nh, ifcp);
1387 rrt->rrt_rflags |= RRTF_CHANGED;
1392 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1393 * do not update age value. Do nothing.
1395 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1396 /* Got a new valid route */
1397 if ((rrt = MALLOC(struct riprt)) == NULL) {
1398 fatal("malloc: struct riprt");
1401 memset(rrt, 0, sizeof(*rrt));
1402 nq = &rrt->rrt_info;
1404 rrt->rrt_same = NULL;
1405 rrt->rrt_index = ifcp->ifc_index;
1406 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1409 applyplen(&nq->rip6_dest, nq->rip6_plen);
1410 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1411 rrt->rrt_flags |= RTF_HOST;
1413 /* Update routing table */
1414 addroute(rrt, &nh, ifcp);
1415 rrt->rrt_rflags |= RRTF_CHANGED;
1419 /* Put the route to the list */
1420 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1423 /* XXX need to care the interval between triggered updates */
1425 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1426 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1427 if (ifcp->ifc_index == ic->ifc_index)
1429 if (ic->ifc_flags & IFF_UP)
1430 ripsend(ic, &ic->ifc_ripsin,
1434 /* Reset the flag */
1435 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1436 rrt->rrt_rflags &= ~RRTF_CHANGED;
1442 * Send all routes request packet to the specified interface.
1445 sendrequest(struct ifc *ifcp)
1447 struct netinfo6 *np;
1450 if (ifcp->ifc_flags & IFF_LOOPBACK)
1452 ripbuf->rip6_cmd = RIP6_REQUEST;
1453 np = ripbuf->rip6_nets;
1454 memset(np, 0, sizeof(struct netinfo6));
1455 np->rip6_metric = HOPCNT_INFINITY6;
1456 tracet(1, "Send rtdump Request to %s (%s)\n",
1457 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1458 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1459 if (error == EAFNOSUPPORT) {
1460 /* Protocol not supported */
1461 tracet(1, "Could not send rtdump Request to %s (%s): "
1462 "set IFF_UP to 0\n",
1463 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1464 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1466 ripbuf->rip6_cmd = RIP6_RESPONSE;
1470 * Process a RIP6_REQUEST packet.
1473 riprequest(struct ifc *ifcp,
1474 struct netinfo6 *np,
1476 struct sockaddr_in6 *sin6)
1481 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1482 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1483 /* Specific response, don't split-horizon */
1484 trace(1, "\tRIP Request\n");
1485 for (i = 0; i < nn; i++, np++) {
1488 np->rip6_metric = rrt->rrt_info.rip6_metric;
1490 np->rip6_metric = HOPCNT_INFINITY6;
1492 (void)sendpacket(sin6, RIPSIZE(nn));
1495 /* Whole routing table dump */
1496 trace(1, "\tRIP Request -- whole routing table\n");
1497 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1501 * Get information of each interface.
1506 struct ifaddrs *ifap, *ifa;
1508 struct ipv6_mreq mreq;
1511 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1516 if (getifaddrs(&ifap) != 0) {
1517 fatal("getifaddrs");
1521 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1522 if (ifa->ifa_addr->sa_family != AF_INET6)
1524 ifcp = ifc_find(ifa->ifa_name);
1525 /* we are interested in multicast-capable interfaces */
1526 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1530 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1531 fatal("malloc: struct ifc");
1534 memset(ifcp, 0, sizeof(*ifcp));
1536 ifcp->ifc_index = -1;
1537 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1538 sizeof(ifcp->ifc_name));
1539 TAILQ_INIT(&ifcp->ifc_ifac_head);
1540 TAILQ_INIT(&ifcp->ifc_iff_head);
1541 ifcp->ifc_flags = ifa->ifa_flags;
1542 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1543 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1544 ifflags(ifcp->ifc_flags));
1545 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1548 /* update flag, this may be up again */
1549 if (ifcp->ifc_flags != ifa->ifa_flags) {
1550 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1551 ifflags(ifcp->ifc_flags));
1552 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1553 ifcp->ifc_cflags |= IFC_CHANGED;
1555 ifcp->ifc_flags = ifa->ifa_flags;
1557 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1558 /* maybe temporary failure */
1561 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1562 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1563 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1564 mreq.ipv6mr_interface = ifcp->ifc_index;
1565 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1566 &mreq, sizeof(mreq)) < 0) {
1567 fatal("IPV6_JOIN_GROUP");
1570 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1579 ifconfig1(const char *name,
1580 const struct sockaddr *sa,
1584 struct in6_ifreq ifr;
1585 const struct sockaddr_in6 *sin6;
1590 sin6 = (const struct sockaddr_in6 *)sa;
1591 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1593 ifr.ifr_addr = *sin6;
1594 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1595 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1596 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1599 plen = sin6mask2len(&ifr.ifr_addr);
1600 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1601 /* same interface found */
1602 /* need check if something changed */
1603 /* XXX not yet implemented */
1607 * New address is found
1609 if ((ifac = MALLOC(struct ifac)) == NULL) {
1610 fatal("malloc: struct ifac");
1613 memset(ifac, 0, sizeof(*ifac));
1615 ifac->ifac_ifc = ifcp;
1616 ifac->ifac_addr = sin6->sin6_addr;
1617 ifac->ifac_plen = plen;
1618 ifac->ifac_scope_id = sin6->sin6_scope_id;
1619 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1620 ifr.ifr_addr = *sin6;
1621 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1622 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1625 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1626 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1628 trace(1, "found address %s/%d -- %s\n",
1629 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1631 trace(1, "found address %s/%d\n",
1632 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1634 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1635 ifcp->ifc_mylladdr = ifac->ifac_addr;
1636 ifcp->ifc_index = ifac->ifac_scope_id;
1637 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1638 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1639 setindex2ifc(ifcp->ifc_index, ifcp);
1640 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1641 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1642 ifcp->ifc_mtu = RIP6_MAXMTU;
1643 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1644 fatal("ioctl: SIOCGIFMETRIC");
1647 ifcp->ifc_metric = ifr.ifr_metric;
1648 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1649 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1651 ifcp->ifc_cflags |= IFC_CHANGED;
1653 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1659 ifremove(int ifindex)
1664 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1665 if (ifcp->ifc_index == ifindex)
1671 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1672 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1674 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1675 if (rrt->rrt_index == ifcp->ifc_index &&
1676 rrt->rrt_rflags & RRTF_AGGREGATE)
1677 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1683 * Receive and process routing messages.
1684 * Update interface information as necesssary.
1691 struct rt_msghdr *rtm;
1692 struct ifa_msghdr *ifam;
1693 struct if_msghdr *ifm;
1694 struct if_announcemsghdr *ifan;
1696 struct ifc *ifcp, *ic;
1697 int iface = 0, rtable = 0;
1698 struct sockaddr_in6 *rta[RTAX_MAX];
1699 struct sockaddr_in6 mask;
1703 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1704 perror("read from rtsock");
1710 if (len < sizeof(*rtm)) {
1711 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1712 len, (u_long)sizeof(*rtm));
1717 fprintf(stderr, "rtmsg:\n");
1718 for (i = 0; i < len; i++) {
1719 fprintf(stderr, "%02x ", buf[i] & 0xff);
1720 if (i % 16 == 15) fprintf(stderr, "\n");
1722 fprintf(stderr, "\n");
1725 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1726 if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1729 /* safety against bogus message */
1730 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1731 trace(1, "bogus rtmsg: length=%d\n",
1732 ((struct rt_msghdr *)p)->rtm_msglen);
1738 switch (((struct rt_msghdr *)p)->rtm_type) {
1741 ifam = (struct ifa_msghdr *)p;
1742 addrs = ifam->ifam_addrs;
1743 q = (char *)(ifam + 1);
1746 ifm = (struct if_msghdr *)p;
1747 addrs = ifm->ifm_addrs;
1748 q = (char *)(ifm + 1);
1750 case RTM_IFANNOUNCE:
1751 ifan = (struct if_announcemsghdr *)p;
1752 switch (ifan->ifan_what) {
1756 case IFAN_DEPARTURE:
1757 ifremove(ifan->ifan_index);
1763 rtm = (struct rt_msghdr *)p;
1764 addrs = rtm->rtm_addrs;
1765 q = (char *)(rtm + 1);
1766 if (rtm->rtm_version != RTM_VERSION) {
1767 trace(1, "unexpected rtmsg version %d "
1769 rtm->rtm_version, RTM_VERSION);
1772 if (rtm->rtm_pid == pid) {
1774 trace(1, "rtmsg looped back to me, ignored\n");
1780 memset(&rta, 0, sizeof(rta));
1781 for (i = 0; i < RTAX_MAX; i++) {
1782 if (addrs & (1 << i)) {
1783 rta[i] = (struct sockaddr_in6 *)q;
1784 q += ROUNDUP(rta[i]->sin6_len);
1788 trace(1, "rtsock: %s (addrs=%x)\n",
1789 rttypes((struct rt_msghdr *)p), addrs);
1792 i < ((struct rt_msghdr *)p)->rtm_msglen;
1794 fprintf(stderr, "%02x ", p[i] & 0xff);
1795 if (i % 16 == 15) fprintf(stderr, "\n");
1797 fprintf(stderr, "\n");
1803 * We may be able to optimize by using ifm->ifm_index or
1804 * ifam->ifam_index. For simplicity we don't do that here.
1806 switch (((struct rt_msghdr *)p)->rtm_type) {
1818 /* nothing to be done here */
1819 trace(1, "\tnothing to be done, ignored\n");
1824 if (rta[RTAX_DST] == NULL) {
1825 trace(1, "\tno destination, ignored\n");
1828 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1829 trace(1, "\taf mismatch, ignored\n");
1832 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1833 trace(1, "\tlinklocal destination, ignored\n");
1836 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1837 trace(1, "\tloopback destination, ignored\n");
1838 continue; /* Loopback */
1840 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1841 trace(1, "\tmulticast destination, ignored\n");
1847 switch (((struct rt_msghdr *)p)->rtm_type) {
1855 /* should already be handled */
1856 fatal("rtrecv: never reach here");
1859 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1860 trace(1, "\tsome of dst/gw/netamsk are "
1861 "unavailable, ignored\n");
1864 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1865 mask.sin6_len = sizeof(mask);
1866 memset(&mask.sin6_addr, 0xff,
1867 sizeof(mask.sin6_addr));
1868 rta[RTAX_NETMASK] = &mask;
1869 } else if (!rta[RTAX_NETMASK]) {
1870 trace(1, "\tsome of dst/gw/netamsk are "
1871 "unavailable, ignored\n");
1874 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1875 rta[RTAX_NETMASK]) == 0) {
1876 rtable++; /*just to be sure*/
1881 trace(1, "\tnot supported yet, ignored\n");
1884 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1885 trace(1, "\tno netmask or ifa given, ignored\n");
1888 if (ifam->ifam_index < nindex2ifc)
1889 ifcp = index2ifc[ifam->ifam_index];
1893 trace(1, "\tinvalid ifam_index %d, ignored\n",
1897 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1905 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1907 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1908 if (ifcp->ifc_cflags & IFC_CHANGED) {
1909 if (ifrt(ifcp, 1)) {
1910 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1911 if (ifcp->ifc_index == ic->ifc_index)
1913 if (ic->ifc_flags & IFF_UP)
1914 ripsend(ic, &ic->ifc_ripsin,
1917 /* Reset the flag */
1918 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1919 rrt->rrt_rflags &= ~RRTF_CHANGED;
1922 ifcp->ifc_cflags &= ~IFC_CHANGED;
1927 trace(1, "rtsock: read routing table again\n");
1933 * remove specified route from the internal routing table.
1936 rt_del(const struct sockaddr_in6 *sdst,
1937 const struct sockaddr_in6 *sgw,
1938 const struct sockaddr_in6 *smask)
1940 const struct in6_addr *dst = NULL;
1941 const struct in6_addr *gw = NULL;
1943 struct netinfo6 ni6;
1944 struct riprt *rrt = NULL;
1947 if (sdst->sin6_family != AF_INET6) {
1948 trace(1, "\tother AF, ignored\n");
1951 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1952 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1953 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1954 trace(1, "\taddress %s not interesting, ignored\n",
1955 inet6_n2p(&sdst->sin6_addr));
1958 dst = &sdst->sin6_addr;
1959 if (sgw->sin6_family == AF_INET6) {
1961 gw = &sgw->sin6_addr;
1962 prefix = sin6mask2len(smask);
1963 } else if (sgw->sin6_family == AF_LINK) {
1965 * Interface route... a hard case. We need to get the prefix
1966 * length from the kernel, but we now are parsing rtmsg.
1967 * We'll purge matching routes from my list, then get the
1970 struct riprt *longest;
1971 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1974 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1975 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1977 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1979 || longest->rrt_info.rip6_plen <
1980 rrt->rrt_info.rip6_plen) {
1987 trace(1, "\tno matching interface route found\n");
1990 gw = &in6addr_loopback;
1991 prefix = rrt->rrt_info.rip6_plen;
1993 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1997 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1998 trace(1, "gw %s\n", inet6_n2p(gw));
1999 t_lifetime = time(NULL) - RIP_LIFETIME;
2000 /* age route for interface address */
2001 memset(&ni6, 0, sizeof(ni6));
2002 ni6.rip6_dest = *dst;
2003 ni6.rip6_plen = prefix;
2004 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2005 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2007 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2008 trace(1, "\tno route found\n");
2012 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2013 trace(1, "\tyou can delete static routes only\n");
2016 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2017 trace(1, "\tgw mismatch: %s <-> ",
2018 inet6_n2p(&rrt->rrt_gw));
2019 trace(1, "%s\n", inet6_n2p(gw));
2021 trace(1, "\troute found, age it\n");
2022 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2023 rrt->rrt_t = t_lifetime;
2024 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2031 * remove specified address from internal interface/routing table.
2034 rt_deladdr(struct ifc *ifcp,
2035 const struct sockaddr_in6 *sifa,
2036 const struct sockaddr_in6 *smask)
2038 const struct in6_addr *addr = NULL;
2040 struct ifac *ifac = NULL;
2041 struct netinfo6 ni6;
2042 struct riprt *rrt = NULL;
2046 if (sifa->sin6_family != AF_INET6) {
2047 trace(1, "\tother AF, ignored\n");
2050 addr = &sifa->sin6_addr;
2051 prefix = sin6mask2len(smask);
2053 trace(1, "\tdeleting %s/%d from %s\n",
2054 inet6_n2p(addr), prefix, ifcp->ifc_name);
2055 ifac = ifa_match(ifcp, addr, prefix);
2057 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2058 inet6_n2p(addr), prefix, ifcp->ifc_name);
2061 if (ifac->ifac_ifc != ifcp) {
2062 trace(1, "\taddress table corrupt: back pointer does not match "
2064 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2067 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2068 t_lifetime = time(NULL) - RIP_LIFETIME;
2069 /* age route for interface address */
2070 memset(&ni6, 0, sizeof(ni6));
2071 ni6.rip6_dest = ifac->ifac_addr;
2072 ni6.rip6_plen = ifac->ifac_plen;
2073 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2074 trace(1, "\tfind interface route %s/%d on %d\n",
2075 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2076 if ((rrt = rtsearch(&ni6)) != NULL) {
2077 struct in6_addr none;
2078 memset(&none, 0, sizeof(none));
2079 if (rrt->rrt_index == ifcp->ifc_index &&
2080 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2081 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2082 trace(1, "\troute found, age it\n");
2083 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2084 rrt->rrt_t = t_lifetime;
2085 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2089 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2090 inet6_n2p(&rrt->rrt_info.rip6_dest),
2091 rrt->rrt_info.rip6_plen,
2095 trace(1, "\tno interface route found\n");
2096 /* age route for p2p destination */
2097 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2098 memset(&ni6, 0, sizeof(ni6));
2099 ni6.rip6_dest = ifac->ifac_raddr;
2100 ni6.rip6_plen = 128;
2101 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2102 trace(1, "\tfind p2p route %s/%d on %d\n",
2103 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2105 if ((rrt = rtsearch(&ni6)) != NULL) {
2106 if (rrt->rrt_index == ifcp->ifc_index &&
2107 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2108 &ifac->ifac_addr)) {
2109 trace(1, "\troute found, age it\n");
2110 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2111 rrt->rrt_t = t_lifetime;
2112 rrt->rrt_info.rip6_metric =
2117 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2118 inet6_n2p(&rrt->rrt_info.rip6_dest),
2119 rrt->rrt_info.rip6_plen,
2123 trace(1, "\tno p2p route found\n");
2127 return ((updated) ? 0 : -1);
2131 * Get each interface address and put those interface routes to the route
2135 ifrt(struct ifc *ifcp, int again)
2138 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2139 struct netinfo6 *np;
2141 int need_trigger = 0;
2144 if (ifcp->ifc_flags & IFF_LOOPBACK)
2145 return 0; /* ignore loopback */
2148 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2149 ifrt_p2p(ifcp, again);
2153 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2154 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2156 trace(1, "route: %s on %s: "
2157 "skip linklocal interface address\n",
2158 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2162 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2164 trace(1, "route: %s: skip unspec interface address\n",
2169 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2171 trace(1, "route: %s: skip loopback address\n",
2176 if (ifcp->ifc_flags & IFF_UP) {
2177 if ((rrt = MALLOC(struct riprt)) == NULL)
2178 fatal("malloc: struct riprt");
2179 memset(rrt, 0, sizeof(*rrt));
2180 rrt->rrt_same = NULL;
2181 rrt->rrt_index = ifcp->ifc_index;
2182 rrt->rrt_t = 0; /* don't age */
2183 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2184 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2185 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2186 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2187 rrt->rrt_flags = RTF_HOST;
2188 rrt->rrt_rflags |= RRTF_CHANGED;
2189 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2190 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2191 rrt->rrt_gw = ifac->ifac_addr;
2192 np = &rrt->rrt_info;
2193 search_rrt = rtsearch(np);
2194 if (search_rrt != NULL) {
2195 if (search_rrt->rrt_info.rip6_metric <=
2196 rrt->rrt_info.rip6_metric) {
2197 /* Already have better route */
2199 trace(1, "route: %s/%d: "
2200 "already registered (%s)\n",
2201 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2207 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2208 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2210 /* Attach the route to the list */
2211 trace(1, "route: %s/%d: register route (%s)\n",
2212 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2214 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2215 addroute(rrt, &rrt->rrt_gw, ifcp);
2218 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2221 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2222 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2223 t_lifetime = time(NULL) - RIP_LIFETIME;
2224 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2225 loop_rrt->rrt_t = t_lifetime;
2226 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2227 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2237 return need_trigger;
2241 * there are couple of p2p interface routing models. "behavior" lets
2242 * you pick one. it looks that gated behavior fits best with BSDs,
2243 * since BSD kernels do not look at prefix length on p2p interfaces.
2246 ifrt_p2p(struct ifc *ifcp, int again)
2249 struct riprt *rrt, *orrt;
2250 struct netinfo6 *np;
2251 struct in6_addr addr, dest;
2252 int advert, ignore, i;
2253 #define P2PADVERT_NETWORK 1
2254 #define P2PADVERT_ADDR 2
2255 #define P2PADVERT_DEST 4
2256 #define P2PADVERT_MAX 4
2257 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2258 const char *category = "";
2261 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2262 addr = ifac->ifac_addr;
2263 dest = ifac->ifac_raddr;
2264 applyplen(&addr, ifac->ifac_plen);
2265 applyplen(&dest, ifac->ifac_plen);
2266 advert = ignore = 0;
2270 * honor addr/plen, just like normal shared medium
2271 * interface. this may cause trouble if you reuse
2272 * addr/plen on other interfaces.
2274 * advertise addr/plen.
2276 advert |= P2PADVERT_NETWORK;
2280 * prefixlen on p2p interface is meaningless.
2281 * advertise addr/128 and dest/128.
2283 * do not install network route to route6d routing
2284 * table (if we do, it would prevent route installation
2285 * for other p2p interface that shares addr/plen).
2287 * XXX what should we do if dest is ::? it will not
2288 * get announced anyways (see following filter),
2289 * but we need to think.
2291 advert |= P2PADVERT_ADDR;
2292 advert |= P2PADVERT_DEST;
2293 ignore |= P2PADVERT_NETWORK;
2297 * just for testing. actually the code is redundant
2298 * given the current p2p interface address assignment
2299 * rule for kame kernel.
2302 * A/n -> announce A/n
2303 * A B/n, A and B share prefix -> A/n (= B/n)
2304 * A B/n, do not share prefix -> A/128 and B/128
2305 * actually, A/64 and A B/128 are the only cases
2306 * permitted by the kernel:
2308 * A B/128 -> A/128 and B/128
2310 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2311 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2312 advert |= P2PADVERT_NETWORK;
2314 advert |= P2PADVERT_ADDR;
2315 advert |= P2PADVERT_DEST;
2316 ignore |= P2PADVERT_NETWORK;
2319 advert |= P2PADVERT_NETWORK;
2323 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2324 if ((ignore & i) != 0)
2326 if ((rrt = MALLOC(struct riprt)) == NULL) {
2327 fatal("malloc: struct riprt");
2330 memset(rrt, 0, sizeof(*rrt));
2331 rrt->rrt_same = NULL;
2332 rrt->rrt_index = ifcp->ifc_index;
2333 rrt->rrt_t = 0; /* don't age */
2335 case P2PADVERT_NETWORK:
2336 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2337 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2338 applyplen(&rrt->rrt_info.rip6_dest,
2340 category = "network";
2342 case P2PADVERT_ADDR:
2343 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2344 rrt->rrt_info.rip6_plen = 128;
2345 rrt->rrt_gw = in6addr_loopback;
2348 case P2PADVERT_DEST:
2349 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2350 rrt->rrt_info.rip6_plen = 128;
2351 rrt->rrt_gw = ifac->ifac_addr;
2355 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2356 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2358 trace(1, "route: %s: skip unspec/linklocal "
2359 "(%s on %s)\n", category, ifcp->ifc_name);
2364 if ((advert & i) == 0) {
2365 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2369 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2370 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2371 np = &rrt->rrt_info;
2372 orrt = rtsearch(np);
2374 /* Attach the route to the list */
2375 trace(1, "route: %s/%d: register route "
2377 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2378 category, ifcp->ifc_name, noadv);
2379 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2380 } else if (rrt->rrt_index != orrt->rrt_index ||
2381 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2383 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2384 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2387 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2388 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2389 category, ifcp->ifc_name, noadv);
2393 trace(1, "route: %s/%d: "
2394 "already registered (%s on %s%s)\n",
2395 inet6_n2p(&np->rip6_dest),
2396 np->rip6_plen, category,
2397 ifcp->ifc_name, noadv);
2403 #undef P2PADVERT_NETWORK
2404 #undef P2PADVERT_ADDR
2405 #undef P2PADVERT_DEST
2406 #undef P2PADVERT_MAX
2410 getifmtu(int ifindex)
2415 struct if_msghdr *ifm;
2422 mib[4] = NET_RT_IFLIST;
2424 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2425 fatal("sysctl estimate NET_RT_IFLIST");
2428 if ((buf = malloc(msize)) == NULL) {
2432 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2433 fatal("sysctl NET_RT_IFLIST");
2436 ifm = (struct if_msghdr *)buf;
2437 mtu = ifm->ifm_data.ifi_mtu;
2438 if (ifindex != ifm->ifm_index) {
2439 fatal("ifindex does not match with ifm_index");
2447 rttypes(struct rt_msghdr *rtm)
2449 #define RTTYPE(s, f) \
2451 if (rtm->rtm_type == (f)) \
2454 RTTYPE("ADD", RTM_ADD);
2455 RTTYPE("DELETE", RTM_DELETE);
2456 RTTYPE("CHANGE", RTM_CHANGE);
2457 RTTYPE("GET", RTM_GET);
2458 RTTYPE("LOSING", RTM_LOSING);
2459 RTTYPE("REDIRECT", RTM_REDIRECT);
2460 RTTYPE("MISS", RTM_MISS);
2461 RTTYPE("LOCK", RTM_LOCK);
2462 RTTYPE("NEWADDR", RTM_NEWADDR);
2463 RTTYPE("DELADDR", RTM_DELADDR);
2464 RTTYPE("IFINFO", RTM_IFINFO);
2466 RTTYPE("OIFINFO", RTM_OIFINFO);
2468 #ifdef RTM_IFANNOUNCE
2469 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2472 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2475 RTTYPE("DELMADDR", RTM_DELMADDR);
2482 rtflags(struct rt_msghdr *rtm)
2484 static char buf[BUFSIZ];
2487 * letter conflict should be okay. painful when *BSD diverges...
2489 strlcpy(buf, "", sizeof(buf));
2490 #define RTFLAG(s, f) \
2492 if (rtm->rtm_flags & (f)) \
2493 strlcat(buf, (s), sizeof(buf)); \
2495 RTFLAG("U", RTF_UP);
2496 RTFLAG("G", RTF_GATEWAY);
2497 RTFLAG("H", RTF_HOST);
2498 RTFLAG("R", RTF_REJECT);
2499 RTFLAG("D", RTF_DYNAMIC);
2500 RTFLAG("M", RTF_MODIFIED);
2501 RTFLAG("d", RTF_DONE);
2503 RTFLAG("m", RTF_MASK);
2506 RTFLAG("c", RTF_CLONED);
2508 RTFLAG("X", RTF_XRESOLVE);
2510 RTFLAG("L", RTF_LLINFO);
2512 RTFLAG("S", RTF_STATIC);
2513 RTFLAG("B", RTF_BLACKHOLE);
2515 RTFLAG("3", RTF_PROTO3);
2517 RTFLAG("2", RTF_PROTO2);
2518 RTFLAG("1", RTF_PROTO1);
2519 #ifdef RTF_BROADCAST
2520 RTFLAG("b", RTF_BROADCAST);
2523 RTFLAG("d", RTF_DEFAULT);
2525 #ifdef RTF_ISAROUTER
2526 RTFLAG("r", RTF_ISAROUTER);
2529 RTFLAG("T", RTF_TUNNEL);
2532 RTFLAG("A", RTF_AUTH);
2535 RTFLAG("E", RTF_CRYPT);
2544 static char buf[BUFSIZ];
2546 strlcpy(buf, "", sizeof(buf));
2547 #define IFFLAG(s, f) \
2549 if (flags & (f)) { \
2551 strlcat(buf, ",", sizeof(buf)); \
2552 strlcat(buf, (s), sizeof(buf)); \
2555 IFFLAG("UP", IFF_UP);
2556 IFFLAG("BROADCAST", IFF_BROADCAST);
2557 IFFLAG("DEBUG", IFF_DEBUG);
2558 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2559 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2560 #ifdef IFF_NOTRAILERS
2561 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2564 IFFLAG("SMART", IFF_SMART);
2566 IFFLAG("RUNNING", IFF_RUNNING);
2567 IFFLAG("NOARP", IFF_NOARP);
2568 IFFLAG("PROMISC", IFF_PROMISC);
2569 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2570 IFFLAG("OACTIVE", IFF_OACTIVE);
2571 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2572 IFFLAG("LINK0", IFF_LINK0);
2573 IFFLAG("LINK1", IFF_LINK1);
2574 IFFLAG("LINK2", IFF_LINK2);
2575 IFFLAG("MULTICAST", IFF_MULTICAST);
2585 char *buf, *p, *lim;
2586 struct rt_msghdr *rtm;
2595 mib[3] = AF_INET6; /* Address family */
2596 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2597 mib[5] = 0; /* No flags */
2607 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2608 errmsg = "sysctl estimate";
2611 if ((buf = malloc(msize)) == NULL) {
2615 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2616 errmsg = "sysctl NET_RT_DUMP";
2619 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2621 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2624 } else if (1 < retry)
2625 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2628 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2629 rtm = (struct rt_msghdr *)p;
2630 rt_entry(rtm, again);
2636 rt_entry(struct rt_msghdr *rtm, int again)
2638 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2639 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2640 char *rtmp, *ifname = NULL;
2641 struct riprt *rrt, *orrt;
2642 struct netinfo6 *np;
2645 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2646 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2647 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2648 return; /* not interested in the link route */
2650 /* do not look at cloned routes */
2651 #ifdef RTF_WASCLONED
2652 if (rtm->rtm_flags & RTF_WASCLONED)
2656 if (rtm->rtm_flags & RTF_CLONED)
2659 /* XXX: Ignore connected routes. */
2660 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2663 * do not look at dynamic routes.
2664 * netbsd/openbsd cloned routes have UGHD.
2666 if (rtm->rtm_flags & RTF_DYNAMIC)
2668 rtmp = (char *)(rtm + 1);
2670 if ((rtm->rtm_addrs & RTA_DST) == 0)
2671 return; /* ignore routes without destination address */
2672 sin6_dst = (struct sockaddr_in6 *)rtmp;
2673 rtmp += ROUNDUP(sin6_dst->sin6_len);
2674 if (rtm->rtm_addrs & RTA_GATEWAY) {
2675 sin6_gw = (struct sockaddr_in6 *)rtmp;
2676 rtmp += ROUNDUP(sin6_gw->sin6_len);
2678 if (rtm->rtm_addrs & RTA_NETMASK) {
2679 sin6_mask = (struct sockaddr_in6 *)rtmp;
2680 rtmp += ROUNDUP(sin6_mask->sin6_len);
2682 if (rtm->rtm_addrs & RTA_GENMASK) {
2683 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2684 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2686 if (rtm->rtm_addrs & RTA_IFP) {
2687 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2688 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2692 if (sin6_dst->sin6_family != AF_INET6)
2694 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2695 return; /* Link-local */
2696 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2697 return; /* Loopback */
2698 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2701 if ((rrt = MALLOC(struct riprt)) == NULL) {
2702 fatal("malloc: struct riprt");
2705 memset(rrt, 0, sizeof(*rrt));
2706 np = &rrt->rrt_info;
2707 rrt->rrt_same = NULL;
2708 rrt->rrt_t = time(NULL);
2709 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2710 rrt->rrt_t = 0; /* Don't age static routes */
2711 if (rtm->rtm_flags & Pflag)
2712 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2713 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2714 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2716 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2717 if (np->rip6_metric < 1)
2718 np->rip6_metric = 1;
2719 rrt->rrt_flags = rtm->rtm_flags;
2720 np->rip6_dest = sin6_dst->sin6_addr;
2723 if (rtm->rtm_flags & RTF_HOST)
2724 np->rip6_plen = 128; /* Host route */
2726 np->rip6_plen = sin6mask2len(sin6_mask);
2730 orrt = rtsearch(np);
2731 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2734 trace(1, "route: %s/%d flags %s: already registered\n",
2735 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2743 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2745 if (sin6_gw->sin6_family == AF_INET6)
2746 rrt->rrt_gw = sin6_gw->sin6_addr;
2747 else if (sin6_gw->sin6_family == AF_LINK) {
2748 /* XXX in case ppp link? */
2749 rrt->rrt_gw = in6addr_loopback;
2751 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2753 trace(1, "route: %s/%d flags %s",
2754 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2755 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2758 ifindex = rtm->rtm_index;
2759 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2760 ifname = index2ifc[ifindex]->ifc_name;
2762 trace(1, " not configured\n");
2766 trace(1, " if %s sock %d", ifname, ifindex);
2767 rrt->rrt_index = ifindex;
2772 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2773 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2774 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2775 trace(0, "***** Gateway %s is not a link-local address.\n",
2776 inet6_n2p(&rrt->rrt_gw));
2777 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2778 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2779 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2782 /* Put it to the route list */
2783 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2784 /* replace route list */
2785 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2786 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2788 trace(1, "route: %s/%d flags %s: replace new route\n",
2789 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2793 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2797 addroute(struct riprt *rrt,
2798 const struct in6_addr *gw,
2801 struct netinfo6 *np;
2802 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2803 struct rt_msghdr *rtm;
2804 struct sockaddr_in6 *sin6;
2807 np = &rrt->rrt_info;
2808 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2809 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2810 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2811 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2812 np->rip6_metric - 1, buf2);
2814 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2815 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2816 np->rip6_metric - 1, buf2);
2820 memset(buf, 0, sizeof(buf));
2821 rtm = (struct rt_msghdr *)buf;
2822 rtm->rtm_type = RTM_ADD;
2823 rtm->rtm_version = RTM_VERSION;
2824 rtm->rtm_seq = ++seq;
2826 rtm->rtm_flags = rrt->rrt_flags;
2827 rtm->rtm_flags |= Qflag;
2828 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2829 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2830 rtm->rtm_inits = RTV_HOPCOUNT;
2831 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2833 sin6->sin6_len = sizeof(struct sockaddr_in6);
2834 sin6->sin6_family = AF_INET6;
2835 sin6->sin6_addr = np->rip6_dest;
2836 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2838 sin6->sin6_len = sizeof(struct sockaddr_in6);
2839 sin6->sin6_family = AF_INET6;
2840 sin6->sin6_addr = *gw;
2841 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2842 sin6->sin6_scope_id = ifcp->ifc_index;
2843 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2845 sin6->sin6_len = sizeof(struct sockaddr_in6);
2846 sin6->sin6_family = AF_INET6;
2847 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2848 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2850 len = (char *)sin6 - (char *)buf;
2851 rtm->rtm_msglen = len;
2852 if (write(rtsock, buf, len) > 0)
2855 if (errno == EEXIST) {
2856 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2857 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2859 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2860 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2862 trace(0, "Can not write to rtsock (addroute): %s\n",
2865 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2872 delroute(struct netinfo6 *np, struct in6_addr *gw)
2874 u_char buf[BUFSIZ], buf2[BUFSIZ];
2875 struct rt_msghdr *rtm;
2876 struct sockaddr_in6 *sin6;
2879 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2880 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2881 np->rip6_plen, buf2);
2883 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2884 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2888 memset(buf, 0, sizeof(buf));
2889 rtm = (struct rt_msghdr *)buf;
2890 rtm->rtm_type = RTM_DELETE;
2891 rtm->rtm_version = RTM_VERSION;
2892 rtm->rtm_seq = ++seq;
2894 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2895 rtm->rtm_flags |= Qflag;
2896 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2897 rtm->rtm_flags |= RTF_HOST;
2898 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2899 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2901 sin6->sin6_len = sizeof(struct sockaddr_in6);
2902 sin6->sin6_family = AF_INET6;
2903 sin6->sin6_addr = np->rip6_dest;
2904 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2906 sin6->sin6_len = sizeof(struct sockaddr_in6);
2907 sin6->sin6_family = AF_INET6;
2908 sin6->sin6_addr = *gw;
2909 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2911 sin6->sin6_len = sizeof(struct sockaddr_in6);
2912 sin6->sin6_family = AF_INET6;
2913 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2914 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2916 len = (char *)sin6 - (char *)buf;
2917 rtm->rtm_msglen = len;
2918 if (write(rtsock, buf, len) >= 0)
2921 if (errno == ESRCH) {
2922 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2923 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2925 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2926 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2928 trace(0, "Can not write to rtsock (delroute): %s\n",
2931 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2938 getroute(struct netinfo6 *np, struct in6_addr *gw)
2943 struct rt_msghdr *rtm;
2944 struct sockaddr_in6 *sin6;
2946 rtm = (struct rt_msghdr *)buf;
2947 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2948 memset(rtm, 0, len);
2949 rtm->rtm_type = RTM_GET;
2950 rtm->rtm_version = RTM_VERSION;
2952 rtm->rtm_seq = myseq;
2953 rtm->rtm_addrs = RTA_DST;
2954 rtm->rtm_msglen = len;
2955 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2956 sin6->sin6_len = sizeof(struct sockaddr_in6);
2957 sin6->sin6_family = AF_INET6;
2958 sin6->sin6_addr = np->rip6_dest;
2959 if (write(rtsock, buf, len) < 0) {
2960 if (errno == ESRCH) /* No such route found */
2962 perror("write to rtsock");
2966 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2967 perror("read from rtsock");
2970 rtm = (struct rt_msghdr *)buf;
2971 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2972 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2973 if (rtm->rtm_addrs & RTA_DST) {
2974 sin6 = (struct sockaddr_in6 *)
2975 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2977 if (rtm->rtm_addrs & RTA_GATEWAY) {
2978 *gw = sin6->sin6_addr;
2985 inet6_n2p(const struct in6_addr *p)
2987 static char buf[BUFSIZ];
2989 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3010 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3013 fprintf(dump, "%s: Interface Table Dump\n", hms());
3014 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3016 fprintf(dump, " Number of interfaces: %d\n", nifc);
3018 fprintf(dump, " advertising interfaces:\n");
3019 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3020 if ((ifcp->ifc_flags & IFF_UP) == 0)
3022 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3024 ifdump0(dump, ifcp);
3026 fprintf(dump, "\n");
3027 fprintf(dump, " non-advertising interfaces:\n");
3028 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3029 if ((ifcp->ifc_flags & IFF_UP) &&
3030 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3032 ifdump0(dump, ifcp);
3034 fprintf(dump, "\n");
3040 ifdump0(FILE *dump, const struct ifc *ifcp)
3048 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3049 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3050 inet6_n2p(&ifcp->ifc_mylladdr),
3051 ifcp->ifc_mtu, ifcp->ifc_metric);
3052 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3053 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3054 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3056 fprintf(dump, "\t%s/%d -- %s\n",
3057 inet6_n2p(&ifac->ifac_addr),
3058 ifac->ifac_plen, buf);
3060 fprintf(dump, "\t%s/%d\n",
3061 inet6_n2p(&ifac->ifac_addr),
3066 fprintf(dump, "\tFilter:\n");
3067 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3069 switch (iffp->iff_type) {
3071 ft = "Aggregate"; addr++; break;
3073 ft = "No-use"; break;
3075 ft = "Advertise-only"; addr++; break;
3077 ft = "Default-only"; break;
3079 ft = "Listen-only"; addr++; break;
3081 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3086 fprintf(dump, "\t\t%s", ft);
3088 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3090 fprintf(dump, "\n");
3092 fprintf(dump, "\n");
3106 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3110 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3111 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3112 if (rrt->rrt_t == 0)
3115 age = t - rrt->rrt_t;
3116 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3118 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3119 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3120 index2ifc[rrt->rrt_index]->ifc_name,
3121 inet6_n2p(&rrt->rrt_gw),
3122 rrt->rrt_info.rip6_metric, (long)age);
3123 if (rrt->rrt_info.rip6_tag) {
3124 fprintf(dump, " tag(0x%04x)",
3125 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3127 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3128 fprintf(dump, " NOT-LL");
3129 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3130 fprintf(dump, " NO-ADV");
3131 fprintf(dump, "\n");
3133 fprintf(dump, "\n");
3139 * Parse the -A (and -O) options and put corresponding filter object to the
3140 * specified interface structures. Each of the -A/O option has the following
3141 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3142 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3148 char *p, *ap, *iflp, *ifname, *ep;
3149 struct iff iff, *iffp;
3157 for (i = 0; i < nfilter; i++) {
3161 memset(iffp, 0, sizeof(*iffp));
3162 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3166 if ((p = strchr(ap, ',')) != NULL) {
3170 if ((p = strchr(ap, '/')) == NULL) {
3171 fatal("no prefixlen specified for '%s'", ap);
3175 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3176 fatal("invalid prefix specified for '%s'", ap);
3181 plen = strtoul(p, &ep, 10);
3182 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3183 fatal("invalid prefix length specified for '%s'", ap);
3186 iffp->iff_plen = plen;
3187 applyplen(&iffp->iff_addr, iffp->iff_plen);
3189 iffp->iff_type = filtertype[i];
3190 if (iflp == NULL || *iflp == '\0') {
3191 fatal("no interface specified for '%s'", ap);
3194 /* parse the interface listing portion */
3197 if ((iflp = strchr(iflp, ',')) != NULL)
3200 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3201 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3204 iffp = malloc(sizeof(*iffp));
3206 fatal("malloc of iff");
3209 memcpy(iffp, &iff, sizeof(*iffp));
3211 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3213 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3218 * -A: aggregate configuration.
3220 if (filtertype[i] != IFIL_TYPE_A)
3222 /* put the aggregate to the kernel routing table */
3223 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3225 fatal("malloc: rrt");
3228 memset(rrt, 0, sizeof(struct riprt));
3229 rrt->rrt_info.rip6_dest = iff.iff_addr;
3230 rrt->rrt_info.rip6_plen = iff.iff_plen;
3231 rrt->rrt_info.rip6_metric = 1;
3232 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3233 rrt->rrt_gw = in6addr_loopback;
3234 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3235 rrt->rrt_rflags = RRTF_AGGREGATE;
3237 rrt->rrt_index = loopifcp->ifc_index;
3239 if (getroute(&rrt->rrt_info, &gw)) {
3242 * When the address has already been registered in the
3243 * kernel routing table, it should be removed
3245 delroute(&rrt->rrt_info, &gw);
3247 /* it is safer behavior */
3249 fatal("%s/%u already in routing table, "
3251 inet6_n2p(&rrt->rrt_info.rip6_dest),
3252 rrt->rrt_info.rip6_plen);
3257 /* Put the route to the list */
3258 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3259 trace(1, "Aggregate: %s/%d for %s\n",
3260 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3261 loopifcp->ifc_name);
3262 /* Add this route to the kernel */
3263 if (nflag) /* do not modify kernel routing table */
3265 addroute(rrt, &in6addr_loopback, loopifcp);
3269 /***************** utility functions *****************/
3272 * Returns a pointer to ifac whose address and prefix length matches
3273 * with the address and prefix length specified in the arguments.
3276 ifa_match(const struct ifc *ifcp,
3277 const struct in6_addr *ia,
3282 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3283 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3284 ifac->ifac_plen == plen)
3292 * Return a pointer to riprt structure whose address and prefix length
3293 * matches with the address and prefix length found in the argument.
3294 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3297 rtsearch(struct netinfo6 *np)
3301 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3302 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3303 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3312 sin6mask2len(const struct sockaddr_in6 *sin6)
3315 return mask2len(&sin6->sin6_addr,
3316 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3320 mask2len(const struct in6_addr *addr, int lenlim)
3323 const u_char *p = (const u_char *)addr;
3325 for (j = 0; j < lenlim; j++, p++) {
3332 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3333 MASKLEN(0xfe, 7); break;
3334 MASKLEN(0xfc, 6); break;
3335 MASKLEN(0xf8, 5); break;
3336 MASKLEN(0xf0, 4); break;
3337 MASKLEN(0xe0, 3); break;
3338 MASKLEN(0xc0, 2); break;
3339 MASKLEN(0x80, 1); break;
3347 applymask(struct in6_addr *addr, struct in6_addr *mask)
3352 p = (u_long *)addr; q = (u_long *)mask;
3353 for (i = 0; i < 4; i++)
3357 static const u_char plent[8] = {
3358 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3362 applyplen(struct in6_addr *ia, int plen)
3368 for (i = 0; i < 16; i++) {
3377 static const int pl2m[9] = {
3378 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3384 static struct in6_addr ia;
3388 memset(&ia, 0, sizeof(struct in6_addr));
3390 for (i = 0; i < 16; i++, p++, n -= 8) {
3404 int len = strlen(p) + 1;
3405 char *q = (char *)malloc(len);
3419 static char buf[BUFSIZ];
3424 if ((tm = localtime(&t)) == 0) {
3428 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3433 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3436 ripinterval(int timer)
3440 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3441 nextalarm = time(NULL) + interval;
3451 t = (int)(RIP_TRIG_INT6_MIN +
3452 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3453 sup_trig_update = time(NULL) + t;
3459 fatal(const char *fmt, ...)
3461 fatal(fmt, va_alist)
3474 vsnprintf(buf, sizeof(buf), fmt, ap);
3478 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3480 syslog(LOG_ERR, "%s", buf);
3486 tracet(int level, const char *fmt, ...)
3488 tracet(level, fmt, va_alist)
3496 if (level <= dflag) {
3502 fprintf(stderr, "%s: ", hms());
3503 vfprintf(stderr, fmt, ap);
3513 vsyslog(LOG_DEBUG, fmt, ap);
3515 vsyslog(LOG_WARNING, fmt, ap);
3522 trace(int level, const char *fmt, ...)
3524 trace(level, fmt, va_alist)
3532 if (level <= dflag) {
3538 vfprintf(stderr, fmt, ap);
3548 vsyslog(LOG_DEBUG, fmt, ap);
3550 vsyslog(LOG_WARNING, fmt, ap);
3558 struct if_nameindex *p, *p0;
3559 unsigned int max = 0;
3561 p0 = if_nameindex();
3562 for (p = p0; p && p->if_index && p->if_name; p++) {
3563 if (max < p->if_index)
3566 if_freenameindex(p0);
3571 ifc_find(char *name)
3575 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3576 if (strcmp(name, ifcp->ifc_name) == 0)
3583 iff_find(struct ifc *ifcp, int type)
3587 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3588 if (type == IFIL_TYPE_ANY ||
3589 type == iffp->iff_type)
3597 setindex2ifc(int idx, struct ifc *ifcp)
3603 nindex2ifc = 5; /*initial guess*/
3604 index2ifc = (struct ifc **)
3605 malloc(sizeof(*index2ifc) * nindex2ifc);
3606 if (index2ifc == NULL) {
3610 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3613 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3616 p = (struct ifc **)realloc(index2ifc,
3617 sizeof(*index2ifc) * nsize);
3622 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3626 index2ifc[idx] = ifcp;