2 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
37 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
43 #include <arpa/inet.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet/ip6.h>
49 #include <netinet/udp.h>
75 #define RT_DUMP_MAXRETRY 15
78 #define INIT_INTERVAL6 6
80 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
83 /* alignment constraint for routing socket */
85 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
86 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
88 struct ifc { /* Configuration of an interface */
89 TAILQ_ENTRY(ifc) ifc_next;
91 char ifc_name[IFNAMSIZ]; /* if name */
92 int ifc_index; /* if index */
93 int ifc_mtu; /* if mtu */
94 int ifc_metric; /* if metric */
95 u_int ifc_flags; /* flags */
96 short ifc_cflags; /* IFC_XXX */
97 struct in6_addr ifc_mylladdr; /* my link-local address */
98 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
99 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
100 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
101 int ifc_joined; /* joined to ff02::9 */
103 TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
105 struct ifac { /* Adddress associated to an interface */
106 TAILQ_ENTRY(ifac) ifac_next;
108 struct ifc *ifac_ifc; /* back pointer */
109 struct in6_addr ifac_addr; /* address */
110 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
111 int ifac_scope_id; /* scope id */
112 int ifac_plen; /* prefix length */
115 struct iff { /* Filters for an interface */
116 TAILQ_ENTRY(iff) iff_next;
119 struct in6_addr iff_addr;
123 struct ifc **index2ifc;
124 unsigned int nindex2ifc;
125 struct ifc *loopifcp = NULL; /* pointing to loopback */
127 struct pollfd set[2];
129 fd_set *sockvecp; /* vector to select() for receiving */
132 int maxfd; /* maximum fd for select() */
134 int rtsock; /* the routing socket */
135 int ripsock; /* socket to send/receive RIP datagram */
137 struct rip6 *ripbuf; /* packet buffer for sending */
140 * Maintain the routes in a linked list. When the number of the routes
141 * grows, somebody would like to introduce a hash based or a radix tree
142 * based structure. I believe the number of routes handled by RIP is
143 * limited and I don't have to manage a complex data structure, however.
145 * One of the major drawbacks of the linear linked list is the difficulty
146 * of representing the relationship between a couple of routes. This may
147 * be a significant problem when we have to support route aggregation with
148 * suppressing the specifics covered by the aggregate.
152 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
154 struct riprt *rrt_same; /* same destination - future use */
155 struct netinfo6 rrt_info; /* network info */
156 struct in6_addr rrt_gw; /* gateway */
157 u_long rrt_flags; /* kernel routing table flags */
158 u_long rrt_rflags; /* route6d routing table flags */
159 time_t rrt_t; /* when the route validated */
160 int rrt_index; /* ifindex from which this route got */
162 TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
164 int dflag = 0; /* debug flag */
165 int qflag = 0; /* quiet flag */
166 int nflag = 0; /* don't update kernel routing table */
167 int aflag = 0; /* age out even the statically defined routes */
168 int hflag = 0; /* don't split horizon */
169 int lflag = 0; /* exchange site local routes */
170 int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
171 int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
172 int sflag = 0; /* announce static routes w/ split horizon */
173 int Sflag = 0; /* announce static routes to every interface */
174 unsigned long routetag = 0; /* route tag attached on originating case */
176 char *filter[MAXFILTER];
177 int filtertype[MAXFILTER];
182 struct sockaddr_storage ripsin;
185 time_t nextalarm = 0;
186 time_t sup_trig_update = 0;
194 volatile sig_atomic_t seenalrm;
195 volatile sig_atomic_t seenquit;
196 volatile sig_atomic_t seenusr1;
198 #define RRTF_AGGREGATE 0x08000000
199 #define RRTF_NOADVERTISE 0x10000000
200 #define RRTF_NH_NOT_LLADDR 0x20000000
201 #define RRTF_SENDANYWAY 0x40000000
202 #define RRTF_CHANGED 0x80000000
204 int main(int, char **);
205 void sighandler(int);
208 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
209 int out_filter(struct riprt *, struct ifc *);
211 void sockopt(struct ifc *);
213 int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
215 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
216 const struct sockaddr_in6 *);
217 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
218 const struct sockaddr_in6 *);
219 void filterconfig(void);
221 const char *rttypes(struct rt_msghdr *);
222 const char *rtflags(struct rt_msghdr *);
223 const char *ifflags(int);
224 int ifrt(struct ifc *, int);
225 void ifrt_p2p(struct ifc *, int);
226 void applymask(struct in6_addr *, struct in6_addr *);
227 void applyplen(struct in6_addr *, int);
230 void ifdump0(FILE *, const struct ifc *);
233 void rt_entry(struct rt_msghdr *, int);
235 void riprequest(struct ifc *, struct netinfo6 *, int,
236 struct sockaddr_in6 *);
237 void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
238 void sendrequest(struct ifc *);
239 int sin6mask2len(const struct sockaddr_in6 *);
240 int mask2len(const struct in6_addr *, int);
241 int sendpacket(struct sockaddr_in6 *, int);
242 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
243 int delroute(struct netinfo6 *, struct in6_addr *);
244 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
246 int tobeadv(struct riprt *, struct ifc *);
247 char *allocopy(char *);
249 const char *inet6_n2p(const struct in6_addr *);
250 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
251 struct in6_addr *plen2mask(int);
252 struct riprt *rtsearch(struct netinfo6 *);
253 int ripinterval(int);
254 time_t ripsuptrig(void);
255 void fatal(const char *, ...)
256 __attribute__((__format__(__printf__, 1, 2)));
257 void trace(int, const char *, ...)
258 __attribute__((__format__(__printf__, 2, 3)));
259 void tracet(int, const char *, ...)
260 __attribute__((__format__(__printf__, 2, 3)));
261 unsigned int if_maxindex(void);
262 struct ifc *ifc_find(char *);
263 struct iff *iff_find(struct ifc *, int);
264 void setindex2ifc(int, struct ifc *);
266 #define MALLOC(type) ((type *)malloc(sizeof(type)))
268 #define IFIL_TYPE_ANY 0x0
269 #define IFIL_TYPE_A 'A'
270 #define IFIL_TYPE_N 'N'
271 #define IFIL_TYPE_T 'T'
272 #define IFIL_TYPE_O 'O'
273 #define IFIL_TYPE_L 'L'
276 main(int argc, char *argv[])
282 sigset_t mask, omask;
283 const char *pidfile = ROUTE6D_PID;
288 progname = strrchr(*argv, '/');
295 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
302 if (nfilter >= MAXFILTER) {
303 fatal("Exceeds MAXFILTER");
306 filtertype[nfilter] = ch;
307 filter[nfilter++] = allocopy(optarg);
311 routetag = strtoul(optarg, &ep, 0);
312 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
313 fatal("invalid route tag");
322 proto = strtoul(optarg, &ep, 0);
323 if (!ep || *ep != '\0' || 3 < proto) {
324 fatal("invalid P flag");
338 proto = strtoul(optarg, &ep, 0);
339 if (!ep || *ep != '\0' || 3 < proto) {
340 fatal("invalid Q flag");
353 if ((rtlog = fopen(optarg, "w")) == NULL) {
354 fatal("Can not write to routelog");
358 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
359 FLAG('a', aflag, 1); break;
360 FLAG('d', dflag, 1); break;
361 FLAG('D', dflag, 2); break;
362 FLAG('h', hflag, 1); break;
363 FLAG('l', lflag, 1); break;
364 FLAG('n', nflag, 1); break;
365 FLAG('q', qflag, 1); break;
366 FLAG('s', sflag, 1); break;
367 FLAG('S', Sflag, 1); break;
370 fatal("Invalid option specified, terminating");
377 fatal("bogus extra arguments");
383 fprintf(stderr, "No kernel update is allowed\n");
387 if (daemon(0, 0) < 0) {
393 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
396 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
398 memset(ripbuf, 0, RIP6_MAXMTU);
399 ripbuf->rip6_cmd = RIP6_RESPONSE;
400 ripbuf->rip6_vers = RIP6_VERSION;
401 ripbuf->rip6_res1[0] = 0;
402 ripbuf->rip6_res1[1] = 0;
406 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
407 if (ifcp->ifc_index < 0) {
408 fprintf(stderr, "No ifindex found at %s "
409 "(no link-local address?)\n", ifcp->ifc_name);
415 if (loopifcp == NULL) {
416 fatal("No loopback found");
419 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
428 if ((pidfh = fopen(pidfile, "w")) != NULL) {
429 fprintf(pidfh, "%d\n", pid);
433 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
437 memset(ripbuf, 0, RIP6_MAXMTU);
438 ripbuf->rip6_cmd = RIP6_RESPONSE;
439 ripbuf->rip6_vers = RIP6_VERSION;
440 ripbuf->rip6_res1[0] = 0;
441 ripbuf->rip6_res1[1] = 0;
443 if (signal(SIGALRM, sighandler) == SIG_ERR ||
444 signal(SIGQUIT, sighandler) == SIG_ERR ||
445 signal(SIGTERM, sighandler) == SIG_ERR ||
446 signal(SIGUSR1, sighandler) == SIG_ERR ||
447 signal(SIGHUP, sighandler) == SIG_ERR ||
448 signal(SIGINT, sighandler) == SIG_ERR) {
453 * To avoid rip packet congestion (not on a cable but in this
454 * process), wait for a moment to send the first RIP6_RESPONSE
457 alarm(ripinterval(INIT_INTERVAL6));
459 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
460 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
462 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
466 syslog(LOG_INFO, "**** Started ****");
468 sigaddset(&mask, SIGALRM);
487 switch (poll(set, 2, INFTIM))
489 memcpy(recvecp, sockvecp, fdmasks);
490 switch (select(maxfd + 1, recvecp, 0, 0, 0))
494 if (errno != EINTR) {
503 if (set[0].revents & POLLIN)
505 if (FD_ISSET(ripsock, recvecp))
508 sigprocmask(SIG_BLOCK, &mask, &omask);
510 sigprocmask(SIG_SETMASK, &omask, NULL);
513 if (set[1].revents & POLLIN)
515 if (FD_ISSET(rtsock, recvecp))
518 sigprocmask(SIG_BLOCK, &mask, &omask);
520 sigprocmask(SIG_SETMASK, &omask, NULL);
527 sighandler(int signo)
547 * gracefully exits after resetting sockopts.
556 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
557 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
558 delroute(&rrt->rrt_info, &rrt->rrt_gw);
563 syslog(LOG_INFO, "**** Terminated ****");
569 * Called periodically:
570 * 1. age out the learned route. remove it if necessary.
571 * 2. submit RIP6_RESPONSE packets.
572 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
573 * to invoke this function in every 1 or 5 or 10 seconds only to age the
574 * routes more precisely.
581 struct riprt *rrt, *rrt_tmp;
582 time_t t_lifetime, t_holddown;
584 /* age the RIP routes */
585 t_lifetime = time(NULL) - RIP_LIFETIME;
586 t_holddown = t_lifetime - RIP_HOLDDOWN;
587 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
590 else if (rrt->rrt_t < t_holddown) {
591 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
592 delroute(&rrt->rrt_info, &rrt->rrt_gw);
594 } else if (rrt->rrt_t < t_lifetime)
595 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
598 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
599 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
600 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
602 alarm(ripinterval(SUPPLY_INTERVAL6));
609 const int int0 = 0, int1 = 1, int255 = 255;
610 struct addrinfo hints, *res;
611 char port[NI_MAXSERV];
613 TAILQ_INIT(&ifc_head);
614 nindex2ifc = 0; /*initial guess*/
616 snprintf(port, sizeof(port), "%u", RIP6_PORT);
618 memset(&hints, 0, sizeof(hints));
619 hints.ai_family = PF_INET6;
620 hints.ai_socktype = SOCK_DGRAM;
621 hints.ai_protocol = IPPROTO_UDP;
622 hints.ai_flags = AI_PASSIVE;
623 error = getaddrinfo(NULL, port, &hints, &res);
625 fatal("%s", gai_strerror(error));
629 fatal(":: resolved to multiple address");
633 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
639 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
640 &int1, sizeof(int1)) < 0) {
641 fatal("rip IPV6_V6ONLY");
645 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
649 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
650 &int255, sizeof(int255)) < 0) {
651 fatal("rip IPV6_MULTICAST_HOPS");
654 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
655 &int0, sizeof(int0)) < 0) {
656 fatal("rip IPV6_MULTICAST_LOOP");
660 #ifdef IPV6_RECVPKTINFO
661 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
662 &int1, sizeof(int1)) < 0) {
663 fatal("rip IPV6_RECVPKTINFO");
666 #else /* old adv. API */
667 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
668 &int1, sizeof(int1)) < 0) {
669 fatal("rip IPV6_PKTINFO");
674 #ifdef IPV6_RECVPKTINFO
675 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
676 &int1, sizeof(int1)) < 0) {
677 fatal("rip IPV6_RECVHOPLIMIT");
680 #else /* old adv. API */
681 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
682 &int1, sizeof(int1)) < 0) {
683 fatal("rip IPV6_HOPLIMIT");
688 memset(&hints, 0, sizeof(hints));
689 hints.ai_family = PF_INET6;
690 hints.ai_socktype = SOCK_DGRAM;
691 hints.ai_protocol = IPPROTO_UDP;
692 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
694 fatal("%s", gai_strerror(error));
698 fatal("%s resolved to multiple address", RIP6_DEST);
701 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
705 set[0].events = POLLIN;
711 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
712 fatal("route socket");
717 set[1].events = POLLIN;
726 rtsock = -1; /*just for safety */
731 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
732 if ((sockvecp = malloc(fdmasks)) == NULL) {
736 if ((recvecp = malloc(fdmasks)) == NULL) {
740 memset(sockvecp, 0, fdmasks);
741 FD_SET(ripsock, sockvecp);
743 FD_SET(rtsock, sockvecp);
748 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
751 * ripflush flushes the rip datagram stored in the rip buffer
754 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
760 tracet(1, "Send(%s): info(%d) to %s.%d\n",
762 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
764 tracet(1, "Send: info(%d) to %s.%d\n",
765 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
767 np = ripbuf->rip6_nets;
768 for (i = 0; i < nrt; i++, np++) {
769 if (np->rip6_metric == NEXTHOP_METRIC) {
770 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
771 trace(2, " NextHop reset");
773 trace(2, " NextHop %s",
774 inet6_n2p(&np->rip6_dest));
777 trace(2, " %s/%d[%d]",
778 inet6_n2p(&np->rip6_dest),
779 np->rip6_plen, np->rip6_metric);
782 trace(2, " tag=0x%04x",
783 ntohs(np->rip6_tag) & 0xffff);
788 error = sendpacket(sin6, RIPSIZE(nrt));
789 if (error == EAFNOSUPPORT) {
790 /* Protocol not supported */
791 tracet(1, "Could not send info to %s (%s): "
793 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
794 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
799 * Generate RIP6_RESPONSE packets and send them.
802 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
805 struct in6_addr *nh; /* next hop */
815 * Request from non-link local address is not
816 * a regular route6d update.
818 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
819 sizeof(struct udphdr) -
820 sizeof(struct rip6) + sizeof(struct netinfo6)) /
821 sizeof(struct netinfo6);
824 np = ripbuf->rip6_nets;
825 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
826 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
828 /* Put the route to the buffer */
832 ripflush(NULL, sin6, nrt, np);
835 np = ripbuf->rip6_nets;
838 if (nrt) /* Send last packet */
839 ripflush(NULL, sin6, nrt, np);
843 if ((flag & RRTF_SENDANYWAY) == 0 &&
844 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
848 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
851 /* -T: generate default route only */
852 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
853 struct netinfo6 rrt_info;
854 memset(&rrt_info, 0, sizeof(struct netinfo6));
855 rrt_info.rip6_dest = in6addr_any;
856 rrt_info.rip6_plen = 0;
857 rrt_info.rip6_metric = 1;
858 rrt_info.rip6_metric += ifcp->ifc_metric;
859 rrt_info.rip6_tag = htons(routetag & 0xffff);
860 np = ripbuf->rip6_nets;
863 ripflush(ifcp, sin6, nrt, np);
867 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
868 sizeof(struct udphdr) -
869 sizeof(struct rip6) + sizeof(struct netinfo6)) /
870 sizeof(struct netinfo6);
872 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
873 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
874 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
877 /* Need to check filter here */
878 if (out_filter(rrt, ifcp) == 0)
881 /* Check split horizon and other conditions */
882 if (tobeadv(rrt, ifcp) == 0)
885 /* Only considers the routes with flag if specified */
886 if ((flag & RRTF_CHANGED) &&
887 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
891 if (rrt->rrt_index == ifcp->ifc_index &&
892 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
893 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
894 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
895 if (nrt == maxrte - 2) {
896 ripflush(ifcp, sin6, nrt, np);
899 np = ripbuf->rip6_nets;
902 np->rip6_dest = rrt->rrt_gw;
905 np->rip6_metric = NEXTHOP_METRIC;
909 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
910 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
911 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
913 if (nrt == maxrte - 2) {
914 ripflush(ifcp, sin6, nrt, np);
917 np = ripbuf->rip6_nets;
919 memset(np, 0, sizeof(struct netinfo6));
920 np->rip6_metric = NEXTHOP_METRIC;
925 /* Put the route to the buffer */
929 ripflush(ifcp, sin6, nrt, np);
932 np = ripbuf->rip6_nets;
935 if (nrt) /* Send last packet */
936 ripflush(ifcp, sin6, nrt, np);
940 * outbound filter logic, per-route/interface.
943 out_filter(struct riprt *rrt, struct ifc *ifcp)
950 * -A: filter out less specific routes, if we have aggregated
953 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
954 if (iffp->iff_type != 'A')
956 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
958 ia = rrt->rrt_info.rip6_dest;
959 applyplen(&ia, iffp->iff_plen);
960 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
965 * if it is an aggregated route, advertise it only to the
966 * interfaces specified on -A.
968 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
970 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
971 if (iffp->iff_type != 'A')
973 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
974 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
985 * -O: advertise only if prefix matches the configured prefix.
987 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
989 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
990 if (iffp->iff_type != 'O')
992 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
994 ia = rrt->rrt_info.rip6_dest;
995 applyplen(&ia, iffp->iff_plen);
996 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1005 /* the prefix should be advertised */
1010 * Determine if the route is to be advertised on the specified interface.
1011 * It checks options specified in the arguments and the split horizon rule.
1014 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1017 /* Special care for static routes */
1018 if (rrt->rrt_flags & RTF_STATIC) {
1019 /* XXX don't advertise reject/blackhole routes */
1020 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1023 if (Sflag) /* Yes, advertise it anyway */
1025 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1029 /* Regular split horizon */
1030 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1036 * Send a rip packet actually.
1039 sendpacket(struct sockaddr_in6 *sin6, int len)
1043 struct iovec iov[2];
1044 u_char cmsgbuf[256];
1045 struct in6_pktinfo *pi;
1047 struct sockaddr_in6 sincopy;
1049 /* do not overwrite the given sin */
1053 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1054 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1055 idx = sin6->sin6_scope_id;
1059 m.msg_name = (caddr_t)sin6;
1060 m.msg_namelen = sizeof(*sin6);
1061 iov[0].iov_base = (caddr_t)ripbuf;
1062 iov[0].iov_len = len;
1067 m.msg_control = NULL;
1068 m.msg_controllen = 0;
1070 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1071 cm = (struct cmsghdr *)cmsgbuf;
1072 m.msg_control = (caddr_t)cm;
1073 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1075 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1076 cm->cmsg_level = IPPROTO_IPV6;
1077 cm->cmsg_type = IPV6_PKTINFO;
1078 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1079 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1080 pi->ipi6_ifindex = idx;
1083 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1084 trace(1, "sendmsg: %s\n", strerror(errno));
1092 * Receive and process RIP packets. Update the routes/kernel forwarding
1093 * table if necessary.
1098 struct ifc *ifcp, *ic;
1099 struct sockaddr_in6 fsock;
1100 struct in6_addr nh; /* next hop */
1102 struct netinfo6 *np, *nq;
1105 unsigned int need_trigger, idx;
1106 char buf[4 * RIP6_MAXMTU];
1110 struct iovec iov[2];
1111 u_char cmsgbuf[256];
1112 struct in6_pktinfo *pi = NULL;
1117 time_t t_half_lifetime;
1121 m.msg_name = (caddr_t)&fsock;
1122 m.msg_namelen = sizeof(fsock);
1123 iov[0].iov_base = (caddr_t)buf;
1124 iov[0].iov_len = sizeof(buf);
1127 cm = (struct cmsghdr *)cmsgbuf;
1128 m.msg_control = (caddr_t)cm;
1129 m.msg_controllen = sizeof(cmsgbuf);
1131 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1136 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1138 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1139 if (cm->cmsg_level != IPPROTO_IPV6)
1141 switch (cm->cmsg_type) {
1143 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1145 "invalid cmsg length for IPV6_PKTINFO\n");
1148 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1149 idx = pi->ipi6_ifindex;
1152 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1154 "invalid cmsg length for IPV6_HOPLIMIT\n");
1157 hlimp = (int *)CMSG_DATA(cm);
1162 if ((size_t)len < sizeof(struct rip6)) {
1163 trace(1, "Packet too short\n");
1167 if (pi == NULL || hlimp == NULL) {
1169 * This can happen when the kernel failed to allocate memory
1170 * for the ancillary data. Although we might be able to handle
1171 * some cases without this info, those are minor and not so
1172 * important, so it's better to discard the packet for safer
1175 trace(1, "IPv6 packet information cannot be retrieved\n");
1179 nh = fsock.sin6_addr;
1180 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1181 sizeof(struct netinfo6);
1182 rp = (struct rip6 *)buf;
1185 if (rp->rip6_vers != RIP6_VERSION) {
1186 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1189 if (rp->rip6_cmd == RIP6_REQUEST) {
1190 if (idx && idx < nindex2ifc) {
1191 ifcp = index2ifc[idx];
1192 riprequest(ifcp, np, nn, &fsock);
1194 riprequest(NULL, np, nn, &fsock);
1199 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1200 trace(1, "Response from non-ll addr: %s\n",
1201 inet6_n2p(&fsock.sin6_addr));
1202 return; /* Ignore packets from non-link-local addr */
1204 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1205 trace(1, "Response from non-rip port from %s\n",
1206 inet6_n2p(&fsock.sin6_addr));
1209 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1211 "Response packet with a smaller hop limit (%d) from %s\n",
1212 *hlimp, inet6_n2p(&fsock.sin6_addr));
1216 * Further validation: since this program does not send off-link
1217 * requests, an incoming response must always come from an on-link
1218 * node. Although this is normally ensured by the source address
1219 * check above, it may not 100% be safe because there are router
1220 * implementations that (invalidly) allow a packet with a link-local
1221 * source address to be forwarded to a different link.
1222 * So we also check whether the destination address is a link-local
1223 * address or the hop limit is 255. Note that RFC2080 does not require
1224 * the specific hop limit for a unicast response, so we cannot assume
1227 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1229 "Response packet possibly from an off-link node: "
1230 "from %s to %s hlim=%d\n",
1231 inet6_n2p(&fsock.sin6_addr),
1232 inet6_n2p(&pi->ipi6_addr), *hlimp);
1236 idx = fsock.sin6_scope_id;
1237 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1239 trace(1, "Packets to unknown interface index %d\n", idx);
1240 return; /* Ignore it */
1242 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1243 return; /* The packet is from me; ignore */
1244 if (rp->rip6_cmd != RIP6_RESPONSE) {
1245 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1250 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1253 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1254 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1257 t_half_lifetime = t - (RIP_LIFETIME/2);
1258 for (; nn; nn--, np++) {
1259 if (np->rip6_metric == NEXTHOP_METRIC) {
1260 /* modify neighbor address */
1261 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1263 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1264 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1265 nh = fsock.sin6_addr;
1266 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1268 nh = fsock.sin6_addr;
1269 trace(1, "\tInvalid Nexthop: %s\n",
1270 inet6_n2p(&np->rip6_dest));
1274 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1275 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1276 inet6_n2p(&np->rip6_dest),
1277 np->rip6_plen, np->rip6_metric);
1280 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1281 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1282 inet6_n2p(&np->rip6_dest),
1283 np->rip6_plen, np->rip6_metric);
1286 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1287 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1288 inet6_n2p(&np->rip6_dest),
1289 np->rip6_plen, np->rip6_metric);
1292 /* may need to pass sitelocal prefix in some case, however*/
1293 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1294 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1295 inet6_n2p(&np->rip6_dest),
1296 np->rip6_plen, np->rip6_metric);
1299 trace(2, "\tnetinfo6: %s/%d [%d]",
1300 inet6_n2p(&np->rip6_dest),
1301 np->rip6_plen, np->rip6_metric);
1303 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1306 applyplen(&ia, np->rip6_plen);
1307 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1308 trace(2, " [junk outside prefix]");
1312 * -L: listen only if the prefix matches the configuration
1314 ok = 1; /* if there's no L filter, it is ok */
1315 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1316 if (iffp->iff_type != IFIL_TYPE_L)
1319 if (np->rip6_plen < iffp->iff_plen)
1321 /* special rule: ::/0 means default, not "in /0" */
1322 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1325 applyplen(&ia, iffp->iff_plen);
1326 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1332 trace(2, " (filtered)\n");
1338 np->rip6_metric += ifcp->ifc_metric;
1339 if (np->rip6_metric > HOPCNT_INFINITY6)
1340 np->rip6_metric = HOPCNT_INFINITY6;
1342 applyplen(&np->rip6_dest, np->rip6_plen);
1343 if ((rrt = rtsearch(np)) != NULL) {
1344 if (rrt->rrt_t == 0)
1345 continue; /* Intf route has priority */
1346 nq = &rrt->rrt_info;
1347 if (nq->rip6_metric > np->rip6_metric) {
1348 if (rrt->rrt_index == ifcp->ifc_index &&
1349 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1350 /* Small metric from the same gateway */
1351 nq->rip6_metric = np->rip6_metric;
1353 /* Better route found */
1354 rrt->rrt_index = ifcp->ifc_index;
1355 /* Update routing table */
1356 delroute(nq, &rrt->rrt_gw);
1359 addroute(rrt, &nh, ifcp);
1361 rrt->rrt_rflags |= RRTF_CHANGED;
1364 } else if (nq->rip6_metric < np->rip6_metric &&
1365 rrt->rrt_index == ifcp->ifc_index &&
1366 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1367 /* Got worse route from same gw */
1368 nq->rip6_metric = np->rip6_metric;
1370 rrt->rrt_rflags |= RRTF_CHANGED;
1372 } else if (nq->rip6_metric == np->rip6_metric &&
1373 np->rip6_metric < HOPCNT_INFINITY6) {
1374 if (rrt->rrt_index == ifcp->ifc_index &&
1375 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1376 /* same metric, same route from same gw */
1378 } else if (rrt->rrt_t < t_half_lifetime) {
1379 /* Better route found */
1380 rrt->rrt_index = ifcp->ifc_index;
1381 /* Update routing table */
1382 delroute(nq, &rrt->rrt_gw);
1385 addroute(rrt, &nh, ifcp);
1386 rrt->rrt_rflags |= RRTF_CHANGED;
1391 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1392 * do not update age value. Do nothing.
1394 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1395 /* Got a new valid route */
1396 if ((rrt = MALLOC(struct riprt)) == NULL) {
1397 fatal("malloc: struct riprt");
1400 memset(rrt, 0, sizeof(*rrt));
1401 nq = &rrt->rrt_info;
1403 rrt->rrt_same = NULL;
1404 rrt->rrt_index = ifcp->ifc_index;
1405 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1408 applyplen(&nq->rip6_dest, nq->rip6_plen);
1409 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1410 rrt->rrt_flags |= RTF_HOST;
1412 /* Update routing table */
1413 addroute(rrt, &nh, ifcp);
1414 rrt->rrt_rflags |= RRTF_CHANGED;
1418 /* Put the route to the list */
1419 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1422 /* XXX need to care the interval between triggered updates */
1424 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1425 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1426 if (ifcp->ifc_index == ic->ifc_index)
1428 if (ic->ifc_flags & IFF_UP)
1429 ripsend(ic, &ic->ifc_ripsin,
1433 /* Reset the flag */
1434 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1435 rrt->rrt_rflags &= ~RRTF_CHANGED;
1441 * Send all routes request packet to the specified interface.
1444 sendrequest(struct ifc *ifcp)
1446 struct netinfo6 *np;
1449 if (ifcp->ifc_flags & IFF_LOOPBACK)
1451 ripbuf->rip6_cmd = RIP6_REQUEST;
1452 np = ripbuf->rip6_nets;
1453 memset(np, 0, sizeof(struct netinfo6));
1454 np->rip6_metric = HOPCNT_INFINITY6;
1455 tracet(1, "Send rtdump Request to %s (%s)\n",
1456 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1457 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1458 if (error == EAFNOSUPPORT) {
1459 /* Protocol not supported */
1460 tracet(1, "Could not send rtdump Request to %s (%s): "
1461 "set IFF_UP to 0\n",
1462 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1463 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1465 ripbuf->rip6_cmd = RIP6_RESPONSE;
1469 * Process a RIP6_REQUEST packet.
1472 riprequest(struct ifc *ifcp,
1473 struct netinfo6 *np,
1475 struct sockaddr_in6 *sin6)
1480 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1481 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1482 /* Specific response, don't split-horizon */
1483 trace(1, "\tRIP Request\n");
1484 for (i = 0; i < nn; i++, np++) {
1487 np->rip6_metric = rrt->rrt_info.rip6_metric;
1489 np->rip6_metric = HOPCNT_INFINITY6;
1491 (void)sendpacket(sin6, RIPSIZE(nn));
1494 /* Whole routing table dump */
1495 trace(1, "\tRIP Request -- whole routing table\n");
1496 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1500 * Get information of each interface.
1505 struct ifaddrs *ifap, *ifa;
1507 struct ipv6_mreq mreq;
1510 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1515 if (getifaddrs(&ifap) != 0) {
1516 fatal("getifaddrs");
1520 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1521 if (ifa->ifa_addr->sa_family != AF_INET6)
1523 ifcp = ifc_find(ifa->ifa_name);
1524 /* we are interested in multicast-capable interfaces */
1525 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1529 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1530 fatal("malloc: struct ifc");
1533 memset(ifcp, 0, sizeof(*ifcp));
1535 ifcp->ifc_index = -1;
1536 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1537 sizeof(ifcp->ifc_name));
1538 TAILQ_INIT(&ifcp->ifc_ifac_head);
1539 TAILQ_INIT(&ifcp->ifc_iff_head);
1540 ifcp->ifc_flags = ifa->ifa_flags;
1541 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1542 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1543 ifflags(ifcp->ifc_flags));
1544 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1547 /* update flag, this may be up again */
1548 if (ifcp->ifc_flags != ifa->ifa_flags) {
1549 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1550 ifflags(ifcp->ifc_flags));
1551 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1552 ifcp->ifc_cflags |= IFC_CHANGED;
1554 ifcp->ifc_flags = ifa->ifa_flags;
1556 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1557 /* maybe temporary failure */
1560 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1561 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1562 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1563 mreq.ipv6mr_interface = ifcp->ifc_index;
1564 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1565 &mreq, sizeof(mreq)) < 0) {
1566 fatal("IPV6_JOIN_GROUP");
1569 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1578 ifconfig1(const char *name,
1579 const struct sockaddr *sa,
1583 struct in6_ifreq ifr;
1584 const struct sockaddr_in6 *sin6;
1589 sin6 = (const struct sockaddr_in6 *)sa;
1590 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1592 ifr.ifr_addr = *sin6;
1593 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1594 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1595 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1598 plen = sin6mask2len(&ifr.ifr_addr);
1599 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1600 /* same interface found */
1601 /* need check if something changed */
1602 /* XXX not yet implemented */
1606 * New address is found
1608 if ((ifac = MALLOC(struct ifac)) == NULL) {
1609 fatal("malloc: struct ifac");
1612 memset(ifac, 0, sizeof(*ifac));
1614 ifac->ifac_ifc = ifcp;
1615 ifac->ifac_addr = sin6->sin6_addr;
1616 ifac->ifac_plen = plen;
1617 ifac->ifac_scope_id = sin6->sin6_scope_id;
1618 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1619 ifr.ifr_addr = *sin6;
1620 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1621 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1624 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1625 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1627 trace(1, "found address %s/%d -- %s\n",
1628 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1630 trace(1, "found address %s/%d\n",
1631 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1633 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1634 ifcp->ifc_mylladdr = ifac->ifac_addr;
1635 ifcp->ifc_index = ifac->ifac_scope_id;
1636 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1637 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1638 setindex2ifc(ifcp->ifc_index, ifcp);
1639 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1640 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1641 ifcp->ifc_mtu = RIP6_MAXMTU;
1642 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1643 fatal("ioctl: SIOCGIFMETRIC");
1646 ifcp->ifc_metric = ifr.ifr_metric;
1647 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1648 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1650 ifcp->ifc_cflags |= IFC_CHANGED;
1652 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1658 ifremove(int ifindex)
1663 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1664 if (ifcp->ifc_index == ifindex)
1670 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1671 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1673 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1674 if (rrt->rrt_index == ifcp->ifc_index &&
1675 rrt->rrt_rflags & RRTF_AGGREGATE)
1676 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1682 * Receive and process routing messages.
1683 * Update interface information as necesssary.
1690 struct rt_msghdr *rtm;
1691 struct ifa_msghdr *ifam;
1692 struct if_msghdr *ifm;
1693 struct if_announcemsghdr *ifan;
1695 struct ifc *ifcp, *ic;
1696 int iface = 0, rtable = 0;
1697 struct sockaddr_in6 *rta[RTAX_MAX];
1698 struct sockaddr_in6 mask;
1702 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1703 perror("read from rtsock");
1709 if (len < sizeof(*rtm)) {
1710 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1711 len, (u_long)sizeof(*rtm));
1716 fprintf(stderr, "rtmsg:\n");
1717 for (i = 0; i < len; i++) {
1718 fprintf(stderr, "%02x ", buf[i] & 0xff);
1719 if (i % 16 == 15) fprintf(stderr, "\n");
1721 fprintf(stderr, "\n");
1724 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1725 if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1728 /* safety against bogus message */
1729 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1730 trace(1, "bogus rtmsg: length=%d\n",
1731 ((struct rt_msghdr *)p)->rtm_msglen);
1737 switch (((struct rt_msghdr *)p)->rtm_type) {
1740 ifam = (struct ifa_msghdr *)p;
1741 addrs = ifam->ifam_addrs;
1742 q = (char *)(ifam + 1);
1745 ifm = (struct if_msghdr *)p;
1746 addrs = ifm->ifm_addrs;
1747 q = (char *)(ifm + 1);
1749 case RTM_IFANNOUNCE:
1750 ifan = (struct if_announcemsghdr *)p;
1751 switch (ifan->ifan_what) {
1755 case IFAN_DEPARTURE:
1756 ifremove(ifan->ifan_index);
1762 rtm = (struct rt_msghdr *)p;
1763 addrs = rtm->rtm_addrs;
1764 q = (char *)(rtm + 1);
1765 if (rtm->rtm_version != RTM_VERSION) {
1766 trace(1, "unexpected rtmsg version %d "
1768 rtm->rtm_version, RTM_VERSION);
1771 if (rtm->rtm_pid == pid) {
1773 trace(1, "rtmsg looped back to me, ignored\n");
1779 memset(&rta, 0, sizeof(rta));
1780 for (i = 0; i < RTAX_MAX; i++) {
1781 if (addrs & (1 << i)) {
1782 rta[i] = (struct sockaddr_in6 *)q;
1783 q += ROUNDUP(rta[i]->sin6_len);
1787 trace(1, "rtsock: %s (addrs=%x)\n",
1788 rttypes((struct rt_msghdr *)p), addrs);
1791 i < ((struct rt_msghdr *)p)->rtm_msglen;
1793 fprintf(stderr, "%02x ", p[i] & 0xff);
1794 if (i % 16 == 15) fprintf(stderr, "\n");
1796 fprintf(stderr, "\n");
1802 * We may be able to optimize by using ifm->ifm_index or
1803 * ifam->ifam_index. For simplicity we don't do that here.
1805 switch (((struct rt_msghdr *)p)->rtm_type) {
1817 /* nothing to be done here */
1818 trace(1, "\tnothing to be done, ignored\n");
1823 if (rta[RTAX_DST] == NULL) {
1824 trace(1, "\tno destination, ignored\n");
1827 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1828 trace(1, "\taf mismatch, ignored\n");
1831 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1832 trace(1, "\tlinklocal destination, ignored\n");
1835 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1836 trace(1, "\tloopback destination, ignored\n");
1837 continue; /* Loopback */
1839 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1840 trace(1, "\tmulticast destination, ignored\n");
1846 switch (((struct rt_msghdr *)p)->rtm_type) {
1854 /* should already be handled */
1855 fatal("rtrecv: never reach here");
1858 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1859 trace(1, "\tsome of dst/gw/netamsk are "
1860 "unavailable, ignored\n");
1863 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1864 mask.sin6_len = sizeof(mask);
1865 memset(&mask.sin6_addr, 0xff,
1866 sizeof(mask.sin6_addr));
1867 rta[RTAX_NETMASK] = &mask;
1868 } else if (!rta[RTAX_NETMASK]) {
1869 trace(1, "\tsome of dst/gw/netamsk are "
1870 "unavailable, ignored\n");
1873 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1874 rta[RTAX_NETMASK]) == 0) {
1875 rtable++; /*just to be sure*/
1880 trace(1, "\tnot supported yet, ignored\n");
1883 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1884 trace(1, "\tno netmask or ifa given, ignored\n");
1887 if (ifam->ifam_index < nindex2ifc)
1888 ifcp = index2ifc[ifam->ifam_index];
1892 trace(1, "\tinvalid ifam_index %d, ignored\n",
1896 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1904 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1906 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1907 if (ifcp->ifc_cflags & IFC_CHANGED) {
1908 if (ifrt(ifcp, 1)) {
1909 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1910 if (ifcp->ifc_index == ic->ifc_index)
1912 if (ic->ifc_flags & IFF_UP)
1913 ripsend(ic, &ic->ifc_ripsin,
1916 /* Reset the flag */
1917 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1918 rrt->rrt_rflags &= ~RRTF_CHANGED;
1921 ifcp->ifc_cflags &= ~IFC_CHANGED;
1926 trace(1, "rtsock: read routing table again\n");
1932 * remove specified route from the internal routing table.
1935 rt_del(const struct sockaddr_in6 *sdst,
1936 const struct sockaddr_in6 *sgw,
1937 const struct sockaddr_in6 *smask)
1939 const struct in6_addr *dst = NULL;
1940 const struct in6_addr *gw = NULL;
1942 struct netinfo6 ni6;
1943 struct riprt *rrt = NULL;
1946 if (sdst->sin6_family != AF_INET6) {
1947 trace(1, "\tother AF, ignored\n");
1950 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1951 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1952 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1953 trace(1, "\taddress %s not interesting, ignored\n",
1954 inet6_n2p(&sdst->sin6_addr));
1957 dst = &sdst->sin6_addr;
1958 if (sgw->sin6_family == AF_INET6) {
1960 gw = &sgw->sin6_addr;
1961 prefix = sin6mask2len(smask);
1962 } else if (sgw->sin6_family == AF_LINK) {
1964 * Interface route... a hard case. We need to get the prefix
1965 * length from the kernel, but we now are parsing rtmsg.
1966 * We'll purge matching routes from my list, then get the
1969 struct riprt *longest;
1970 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1973 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1974 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1976 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1978 || longest->rrt_info.rip6_plen <
1979 rrt->rrt_info.rip6_plen) {
1986 trace(1, "\tno matching interface route found\n");
1989 gw = &in6addr_loopback;
1990 prefix = rrt->rrt_info.rip6_plen;
1992 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1996 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1997 trace(1, "gw %s\n", inet6_n2p(gw));
1998 t_lifetime = time(NULL) - RIP_LIFETIME;
1999 /* age route for interface address */
2000 memset(&ni6, 0, sizeof(ni6));
2001 ni6.rip6_dest = *dst;
2002 ni6.rip6_plen = prefix;
2003 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2004 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2006 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2007 trace(1, "\tno route found\n");
2011 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2012 trace(1, "\tyou can delete static routes only\n");
2015 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2016 trace(1, "\tgw mismatch: %s <-> ",
2017 inet6_n2p(&rrt->rrt_gw));
2018 trace(1, "%s\n", inet6_n2p(gw));
2020 trace(1, "\troute found, age it\n");
2021 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2022 rrt->rrt_t = t_lifetime;
2023 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2030 * remove specified address from internal interface/routing table.
2033 rt_deladdr(struct ifc *ifcp,
2034 const struct sockaddr_in6 *sifa,
2035 const struct sockaddr_in6 *smask)
2037 const struct in6_addr *addr = NULL;
2039 struct ifac *ifac = NULL;
2040 struct netinfo6 ni6;
2041 struct riprt *rrt = NULL;
2045 if (sifa->sin6_family != AF_INET6) {
2046 trace(1, "\tother AF, ignored\n");
2049 addr = &sifa->sin6_addr;
2050 prefix = sin6mask2len(smask);
2052 trace(1, "\tdeleting %s/%d from %s\n",
2053 inet6_n2p(addr), prefix, ifcp->ifc_name);
2054 ifac = ifa_match(ifcp, addr, prefix);
2056 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2057 inet6_n2p(addr), prefix, ifcp->ifc_name);
2060 if (ifac->ifac_ifc != ifcp) {
2061 trace(1, "\taddress table corrupt: back pointer does not match "
2063 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2066 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2067 t_lifetime = time(NULL) - RIP_LIFETIME;
2068 /* age route for interface address */
2069 memset(&ni6, 0, sizeof(ni6));
2070 ni6.rip6_dest = ifac->ifac_addr;
2071 ni6.rip6_plen = ifac->ifac_plen;
2072 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2073 trace(1, "\tfind interface route %s/%d on %d\n",
2074 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2075 if ((rrt = rtsearch(&ni6)) != NULL) {
2076 struct in6_addr none;
2077 memset(&none, 0, sizeof(none));
2078 if (rrt->rrt_index == ifcp->ifc_index &&
2079 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2080 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2081 trace(1, "\troute found, age it\n");
2082 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2083 rrt->rrt_t = t_lifetime;
2084 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2088 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2089 inet6_n2p(&rrt->rrt_info.rip6_dest),
2090 rrt->rrt_info.rip6_plen,
2094 trace(1, "\tno interface route found\n");
2095 /* age route for p2p destination */
2096 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2097 memset(&ni6, 0, sizeof(ni6));
2098 ni6.rip6_dest = ifac->ifac_raddr;
2099 ni6.rip6_plen = 128;
2100 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2101 trace(1, "\tfind p2p route %s/%d on %d\n",
2102 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2104 if ((rrt = rtsearch(&ni6)) != NULL) {
2105 if (rrt->rrt_index == ifcp->ifc_index &&
2106 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2107 &ifac->ifac_addr)) {
2108 trace(1, "\troute found, age it\n");
2109 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2110 rrt->rrt_t = t_lifetime;
2111 rrt->rrt_info.rip6_metric =
2116 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2117 inet6_n2p(&rrt->rrt_info.rip6_dest),
2118 rrt->rrt_info.rip6_plen,
2122 trace(1, "\tno p2p route found\n");
2126 return ((updated) ? 0 : -1);
2130 * Get each interface address and put those interface routes to the route
2134 ifrt(struct ifc *ifcp, int again)
2137 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2138 struct netinfo6 *np;
2140 int need_trigger = 0;
2143 if (ifcp->ifc_flags & IFF_LOOPBACK)
2144 return 0; /* ignore loopback */
2147 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2148 ifrt_p2p(ifcp, again);
2152 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2153 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2155 trace(1, "route: %s on %s: "
2156 "skip linklocal interface address\n",
2157 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2161 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2163 trace(1, "route: %s: skip unspec interface address\n",
2168 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2170 trace(1, "route: %s: skip loopback address\n",
2175 if (ifcp->ifc_flags & IFF_UP) {
2176 if ((rrt = MALLOC(struct riprt)) == NULL)
2177 fatal("malloc: struct riprt");
2178 memset(rrt, 0, sizeof(*rrt));
2179 rrt->rrt_same = NULL;
2180 rrt->rrt_index = ifcp->ifc_index;
2181 rrt->rrt_t = 0; /* don't age */
2182 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2183 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2184 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2185 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2186 rrt->rrt_flags = RTF_HOST;
2187 rrt->rrt_rflags |= RRTF_CHANGED;
2188 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2189 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2190 rrt->rrt_gw = ifac->ifac_addr;
2191 np = &rrt->rrt_info;
2192 search_rrt = rtsearch(np);
2193 if (search_rrt != NULL) {
2194 if (search_rrt->rrt_info.rip6_metric <=
2195 rrt->rrt_info.rip6_metric) {
2196 /* Already have better route */
2198 trace(1, "route: %s/%d: "
2199 "already registered (%s)\n",
2200 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2206 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2207 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2209 /* Attach the route to the list */
2210 trace(1, "route: %s/%d: register route (%s)\n",
2211 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2213 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2214 addroute(rrt, &rrt->rrt_gw, ifcp);
2217 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2220 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2221 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2222 t_lifetime = time(NULL) - RIP_LIFETIME;
2223 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2224 loop_rrt->rrt_t = t_lifetime;
2225 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2226 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2236 return need_trigger;
2240 * there are couple of p2p interface routing models. "behavior" lets
2241 * you pick one. it looks that gated behavior fits best with BSDs,
2242 * since BSD kernels do not look at prefix length on p2p interfaces.
2245 ifrt_p2p(struct ifc *ifcp, int again)
2248 struct riprt *rrt, *orrt;
2249 struct netinfo6 *np;
2250 struct in6_addr addr, dest;
2251 int advert, ignore, i;
2252 #define P2PADVERT_NETWORK 1
2253 #define P2PADVERT_ADDR 2
2254 #define P2PADVERT_DEST 4
2255 #define P2PADVERT_MAX 4
2256 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2257 const char *category = "";
2260 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2261 addr = ifac->ifac_addr;
2262 dest = ifac->ifac_raddr;
2263 applyplen(&addr, ifac->ifac_plen);
2264 applyplen(&dest, ifac->ifac_plen);
2265 advert = ignore = 0;
2269 * honor addr/plen, just like normal shared medium
2270 * interface. this may cause trouble if you reuse
2271 * addr/plen on other interfaces.
2273 * advertise addr/plen.
2275 advert |= P2PADVERT_NETWORK;
2279 * prefixlen on p2p interface is meaningless.
2280 * advertise addr/128 and dest/128.
2282 * do not install network route to route6d routing
2283 * table (if we do, it would prevent route installation
2284 * for other p2p interface that shares addr/plen).
2286 * XXX what should we do if dest is ::? it will not
2287 * get announced anyways (see following filter),
2288 * but we need to think.
2290 advert |= P2PADVERT_ADDR;
2291 advert |= P2PADVERT_DEST;
2292 ignore |= P2PADVERT_NETWORK;
2296 * just for testing. actually the code is redundant
2297 * given the current p2p interface address assignment
2298 * rule for kame kernel.
2301 * A/n -> announce A/n
2302 * A B/n, A and B share prefix -> A/n (= B/n)
2303 * A B/n, do not share prefix -> A/128 and B/128
2304 * actually, A/64 and A B/128 are the only cases
2305 * permitted by the kernel:
2307 * A B/128 -> A/128 and B/128
2309 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2310 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2311 advert |= P2PADVERT_NETWORK;
2313 advert |= P2PADVERT_ADDR;
2314 advert |= P2PADVERT_DEST;
2315 ignore |= P2PADVERT_NETWORK;
2318 advert |= P2PADVERT_NETWORK;
2322 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2323 if ((ignore & i) != 0)
2325 if ((rrt = MALLOC(struct riprt)) == NULL) {
2326 fatal("malloc: struct riprt");
2329 memset(rrt, 0, sizeof(*rrt));
2330 rrt->rrt_same = NULL;
2331 rrt->rrt_index = ifcp->ifc_index;
2332 rrt->rrt_t = 0; /* don't age */
2334 case P2PADVERT_NETWORK:
2335 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2336 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2337 applyplen(&rrt->rrt_info.rip6_dest,
2339 category = "network";
2341 case P2PADVERT_ADDR:
2342 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2343 rrt->rrt_info.rip6_plen = 128;
2344 rrt->rrt_gw = in6addr_loopback;
2347 case P2PADVERT_DEST:
2348 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2349 rrt->rrt_info.rip6_plen = 128;
2350 rrt->rrt_gw = ifac->ifac_addr;
2354 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2355 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2357 trace(1, "route: %s: skip unspec/linklocal "
2358 "(%s on %s)\n", category, ifcp->ifc_name);
2363 if ((advert & i) == 0) {
2364 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2368 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2369 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2370 np = &rrt->rrt_info;
2371 orrt = rtsearch(np);
2373 /* Attach the route to the list */
2374 trace(1, "route: %s/%d: register route "
2376 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2377 category, ifcp->ifc_name, noadv);
2378 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2379 } else if (rrt->rrt_index != orrt->rrt_index ||
2380 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2382 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2383 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2386 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2387 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2388 category, ifcp->ifc_name, noadv);
2392 trace(1, "route: %s/%d: "
2393 "already registered (%s on %s%s)\n",
2394 inet6_n2p(&np->rip6_dest),
2395 np->rip6_plen, category,
2396 ifcp->ifc_name, noadv);
2402 #undef P2PADVERT_NETWORK
2403 #undef P2PADVERT_ADDR
2404 #undef P2PADVERT_DEST
2405 #undef P2PADVERT_MAX
2409 getifmtu(int ifindex)
2414 struct if_msghdr *ifm;
2421 mib[4] = NET_RT_IFLIST;
2423 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2424 fatal("sysctl estimate NET_RT_IFLIST");
2427 if ((buf = malloc(msize)) == NULL) {
2431 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2432 fatal("sysctl NET_RT_IFLIST");
2435 ifm = (struct if_msghdr *)buf;
2436 mtu = ifm->ifm_data.ifi_mtu;
2437 if (ifindex != ifm->ifm_index) {
2438 fatal("ifindex does not match with ifm_index");
2446 rttypes(struct rt_msghdr *rtm)
2448 #define RTTYPE(s, f) \
2450 if (rtm->rtm_type == (f)) \
2453 RTTYPE("ADD", RTM_ADD);
2454 RTTYPE("DELETE", RTM_DELETE);
2455 RTTYPE("CHANGE", RTM_CHANGE);
2456 RTTYPE("GET", RTM_GET);
2457 RTTYPE("LOSING", RTM_LOSING);
2458 RTTYPE("REDIRECT", RTM_REDIRECT);
2459 RTTYPE("MISS", RTM_MISS);
2460 RTTYPE("LOCK", RTM_LOCK);
2461 RTTYPE("NEWADDR", RTM_NEWADDR);
2462 RTTYPE("DELADDR", RTM_DELADDR);
2463 RTTYPE("IFINFO", RTM_IFINFO);
2465 RTTYPE("OIFINFO", RTM_OIFINFO);
2467 #ifdef RTM_IFANNOUNCE
2468 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2471 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2474 RTTYPE("DELMADDR", RTM_DELMADDR);
2481 rtflags(struct rt_msghdr *rtm)
2483 static char buf[BUFSIZ];
2486 * letter conflict should be okay. painful when *BSD diverges...
2488 strlcpy(buf, "", sizeof(buf));
2489 #define RTFLAG(s, f) \
2491 if (rtm->rtm_flags & (f)) \
2492 strlcat(buf, (s), sizeof(buf)); \
2494 RTFLAG("U", RTF_UP);
2495 RTFLAG("G", RTF_GATEWAY);
2496 RTFLAG("H", RTF_HOST);
2497 RTFLAG("R", RTF_REJECT);
2498 RTFLAG("D", RTF_DYNAMIC);
2499 RTFLAG("M", RTF_MODIFIED);
2500 RTFLAG("d", RTF_DONE);
2502 RTFLAG("m", RTF_MASK);
2505 RTFLAG("c", RTF_CLONED);
2507 RTFLAG("X", RTF_XRESOLVE);
2509 RTFLAG("L", RTF_LLINFO);
2511 RTFLAG("S", RTF_STATIC);
2512 RTFLAG("B", RTF_BLACKHOLE);
2514 RTFLAG("3", RTF_PROTO3);
2516 RTFLAG("2", RTF_PROTO2);
2517 RTFLAG("1", RTF_PROTO1);
2518 #ifdef RTF_BROADCAST
2519 RTFLAG("b", RTF_BROADCAST);
2522 RTFLAG("d", RTF_DEFAULT);
2524 #ifdef RTF_ISAROUTER
2525 RTFLAG("r", RTF_ISAROUTER);
2528 RTFLAG("T", RTF_TUNNEL);
2531 RTFLAG("A", RTF_AUTH);
2534 RTFLAG("E", RTF_CRYPT);
2543 static char buf[BUFSIZ];
2545 strlcpy(buf, "", sizeof(buf));
2546 #define IFFLAG(s, f) \
2548 if (flags & (f)) { \
2550 strlcat(buf, ",", sizeof(buf)); \
2551 strlcat(buf, (s), sizeof(buf)); \
2554 IFFLAG("UP", IFF_UP);
2555 IFFLAG("BROADCAST", IFF_BROADCAST);
2556 IFFLAG("DEBUG", IFF_DEBUG);
2557 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2558 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2559 #ifdef IFF_NOTRAILERS
2560 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2562 IFFLAG("RUNNING", IFF_RUNNING);
2563 IFFLAG("NOARP", IFF_NOARP);
2564 IFFLAG("PROMISC", IFF_PROMISC);
2565 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2566 IFFLAG("OACTIVE", IFF_OACTIVE);
2567 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2568 IFFLAG("LINK0", IFF_LINK0);
2569 IFFLAG("LINK1", IFF_LINK1);
2570 IFFLAG("LINK2", IFF_LINK2);
2571 IFFLAG("MULTICAST", IFF_MULTICAST);
2581 char *buf, *p, *lim;
2582 struct rt_msghdr *rtm;
2591 mib[3] = AF_INET6; /* Address family */
2592 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2593 mib[5] = 0; /* No flags */
2603 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2604 errmsg = "sysctl estimate";
2607 if ((buf = malloc(msize)) == NULL) {
2611 if (sysctl(mib, nitems(mib), 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;