2 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
58 #include <sys/types.h>
59 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/sysctl.h>
66 #include <net/if_var.h>
68 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet/udp.h>
77 #include <arpa/inet.h>
82 #define RT_DUMP_MAXRETRY 15
85 #define INIT_INTERVAL6 6
87 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
90 /* alignment constraint for routing socket */
92 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
93 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
96 * Following two macros are highly depending on KAME Release
98 #define IN6_LINKLOCAL_IFINDEX(addr) \
99 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
101 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
103 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
104 (addr).s6_addr[3] = (index) & 0xff; \
107 struct ifc { /* Configuration of an interface */
108 TAILQ_ENTRY(ifc) ifc_next;
110 char ifc_name[IFNAMSIZ]; /* if name */
111 int ifc_index; /* if index */
112 int ifc_mtu; /* if mtu */
113 int ifc_metric; /* if metric */
114 u_int ifc_flags; /* flags */
115 short ifc_cflags; /* IFC_XXX */
116 struct in6_addr ifc_mylladdr; /* my link-local address */
117 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
118 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
119 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
120 int ifc_joined; /* joined to ff02::9 */
122 TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
124 struct ifac { /* Adddress associated to an interface */
125 TAILQ_ENTRY(ifac) ifac_next;
127 struct ifc *ifac_ifc; /* back pointer */
128 struct in6_addr ifac_addr; /* address */
129 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
130 int ifac_scope_id; /* scope id */
131 int ifac_plen; /* prefix length */
134 struct iff { /* Filters for an interface */
135 TAILQ_ENTRY(iff) iff_next;
138 struct in6_addr iff_addr;
142 struct ifc **index2ifc;
143 unsigned int nindex2ifc;
144 struct ifc *loopifcp = NULL; /* pointing to loopback */
146 struct pollfd set[2];
148 fd_set *sockvecp; /* vector to select() for receiving */
151 int maxfd; /* maximum fd for select() */
153 int rtsock; /* the routing socket */
154 int ripsock; /* socket to send/receive RIP datagram */
156 struct rip6 *ripbuf; /* packet buffer for sending */
159 * Maintain the routes in a linked list. When the number of the routes
160 * grows, somebody would like to introduce a hash based or a radix tree
161 * based structure. I believe the number of routes handled by RIP is
162 * limited and I don't have to manage a complex data structure, however.
164 * One of the major drawbacks of the linear linked list is the difficulty
165 * of representing the relationship between a couple of routes. This may
166 * be a significant problem when we have to support route aggregation with
167 * suppressing the specifics covered by the aggregate.
171 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
173 struct riprt *rrt_same; /* same destination - future use */
174 struct netinfo6 rrt_info; /* network info */
175 struct in6_addr rrt_gw; /* gateway */
176 u_long rrt_flags; /* kernel routing table flags */
177 u_long rrt_rflags; /* route6d routing table flags */
178 time_t rrt_t; /* when the route validated */
179 int rrt_index; /* ifindex from which this route got */
181 TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
183 int dflag = 0; /* debug flag */
184 int qflag = 0; /* quiet flag */
185 int nflag = 0; /* don't update kernel routing table */
186 int aflag = 0; /* age out even the statically defined routes */
187 int hflag = 0; /* don't split horizon */
188 int lflag = 0; /* exchange site local routes */
189 int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
190 int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
191 int sflag = 0; /* announce static routes w/ split horizon */
192 int Sflag = 0; /* announce static routes to every interface */
193 unsigned long routetag = 0; /* route tag attached on originating case */
195 char *filter[MAXFILTER];
196 int filtertype[MAXFILTER];
201 struct sockaddr_storage ripsin;
204 time_t nextalarm = 0;
205 time_t sup_trig_update = 0;
213 volatile sig_atomic_t seenalrm;
214 volatile sig_atomic_t seenquit;
215 volatile sig_atomic_t seenusr1;
217 #define RRTF_AGGREGATE 0x08000000
218 #define RRTF_NOADVERTISE 0x10000000
219 #define RRTF_NH_NOT_LLADDR 0x20000000
220 #define RRTF_SENDANYWAY 0x40000000
221 #define RRTF_CHANGED 0x80000000
223 int main(int, char **);
224 void sighandler(int);
227 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
228 int out_filter(struct riprt *, struct ifc *);
230 void sockopt(struct ifc *);
232 int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
234 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
235 const struct sockaddr_in6 *);
236 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
237 const struct sockaddr_in6 *);
238 void filterconfig(void);
240 const char *rttypes(struct rt_msghdr *);
241 const char *rtflags(struct rt_msghdr *);
242 const char *ifflags(int);
243 int ifrt(struct ifc *, int);
244 void ifrt_p2p(struct ifc *, int);
245 void applymask(struct in6_addr *, struct in6_addr *);
246 void applyplen(struct in6_addr *, int);
249 void ifdump0(FILE *, const struct ifc *);
252 void rt_entry(struct rt_msghdr *, int);
254 void riprequest(struct ifc *, struct netinfo6 *, int,
255 struct sockaddr_in6 *);
256 void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
257 void sendrequest(struct ifc *);
258 int sin6mask2len(const struct sockaddr_in6 *);
259 int mask2len(const struct in6_addr *, int);
260 int sendpacket(struct sockaddr_in6 *, int);
261 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
262 int delroute(struct netinfo6 *, struct in6_addr *);
263 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
265 int tobeadv(struct riprt *, struct ifc *);
266 char *allocopy(char *);
268 const char *inet6_n2p(const struct in6_addr *);
269 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
270 struct in6_addr *plen2mask(int);
271 struct riprt *rtsearch(struct netinfo6 *);
272 int ripinterval(int);
273 time_t ripsuptrig(void);
274 void fatal(const char *, ...)
275 __attribute__((__format__(__printf__, 1, 2)));
276 void trace(int, const char *, ...)
277 __attribute__((__format__(__printf__, 2, 3)));
278 void tracet(int, const char *, ...)
279 __attribute__((__format__(__printf__, 2, 3)));
280 unsigned int if_maxindex(void);
281 struct ifc *ifc_find(char *);
282 struct iff *iff_find(struct ifc *, int);
283 void setindex2ifc(int, struct ifc *);
285 #define MALLOC(type) ((type *)malloc(sizeof(type)))
287 #define IFIL_TYPE_ANY 0x0
288 #define IFIL_TYPE_A 'A'
289 #define IFIL_TYPE_N 'N'
290 #define IFIL_TYPE_T 'T'
291 #define IFIL_TYPE_O 'O'
292 #define IFIL_TYPE_L 'L'
295 main(int argc, char *argv[])
301 sigset_t mask, omask;
302 const char *pidfile = ROUTE6D_PID;
307 progname = strrchr(*argv, '/');
314 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
321 if (nfilter >= MAXFILTER) {
322 fatal("Exceeds MAXFILTER");
325 filtertype[nfilter] = ch;
326 filter[nfilter++] = allocopy(optarg);
330 routetag = strtoul(optarg, &ep, 0);
331 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
332 fatal("invalid route tag");
341 proto = strtoul(optarg, &ep, 0);
342 if (!ep || *ep != '\0' || 3 < proto) {
343 fatal("invalid P flag");
357 proto = strtoul(optarg, &ep, 0);
358 if (!ep || *ep != '\0' || 3 < proto) {
359 fatal("invalid Q flag");
372 if ((rtlog = fopen(optarg, "w")) == NULL) {
373 fatal("Can not write to routelog");
377 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
378 FLAG('a', aflag, 1); break;
379 FLAG('d', dflag, 1); break;
380 FLAG('D', dflag, 2); break;
381 FLAG('h', hflag, 1); break;
382 FLAG('l', lflag, 1); break;
383 FLAG('n', nflag, 1); break;
384 FLAG('q', qflag, 1); break;
385 FLAG('s', sflag, 1); break;
386 FLAG('S', Sflag, 1); break;
389 fatal("Invalid option specified, terminating");
396 fatal("bogus extra arguments");
402 fprintf(stderr, "No kernel update is allowed\n");
406 if (daemon(0, 0) < 0) {
412 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
415 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
417 memset(ripbuf, 0, RIP6_MAXMTU);
418 ripbuf->rip6_cmd = RIP6_RESPONSE;
419 ripbuf->rip6_vers = RIP6_VERSION;
420 ripbuf->rip6_res1[0] = 0;
421 ripbuf->rip6_res1[1] = 0;
425 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
426 if (ifcp->ifc_index < 0) {
427 fprintf(stderr, "No ifindex found at %s "
428 "(no link-local address?)\n", ifcp->ifc_name);
434 if (loopifcp == NULL) {
435 fatal("No loopback found");
438 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
447 if ((pidfh = fopen(pidfile, "w")) != NULL) {
448 fprintf(pidfh, "%d\n", pid);
452 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
456 memset(ripbuf, 0, RIP6_MAXMTU);
457 ripbuf->rip6_cmd = RIP6_RESPONSE;
458 ripbuf->rip6_vers = RIP6_VERSION;
459 ripbuf->rip6_res1[0] = 0;
460 ripbuf->rip6_res1[1] = 0;
462 if (signal(SIGALRM, sighandler) == SIG_ERR ||
463 signal(SIGQUIT, sighandler) == SIG_ERR ||
464 signal(SIGTERM, sighandler) == SIG_ERR ||
465 signal(SIGUSR1, sighandler) == SIG_ERR ||
466 signal(SIGHUP, sighandler) == SIG_ERR ||
467 signal(SIGINT, sighandler) == SIG_ERR) {
472 * To avoid rip packet congestion (not on a cable but in this
473 * process), wait for a moment to send the first RIP6_RESPONSE
476 alarm(ripinterval(INIT_INTERVAL6));
478 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
479 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
481 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
485 syslog(LOG_INFO, "**** Started ****");
487 sigaddset(&mask, SIGALRM);
506 switch (poll(set, 2, INFTIM))
508 memcpy(recvecp, sockvecp, fdmasks);
509 switch (select(maxfd + 1, recvecp, 0, 0, 0))
513 if (errno != EINTR) {
522 if (set[0].revents & POLLIN)
524 if (FD_ISSET(ripsock, recvecp))
527 sigprocmask(SIG_BLOCK, &mask, &omask);
529 sigprocmask(SIG_SETMASK, &omask, NULL);
532 if (set[1].revents & POLLIN)
534 if (FD_ISSET(rtsock, recvecp))
537 sigprocmask(SIG_BLOCK, &mask, &omask);
539 sigprocmask(SIG_SETMASK, &omask, NULL);
546 sighandler(int signo)
566 * gracefully exits after resetting sockopts.
575 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
576 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
577 delroute(&rrt->rrt_info, &rrt->rrt_gw);
582 syslog(LOG_INFO, "**** Terminated ****");
588 * Called periodically:
589 * 1. age out the learned route. remove it if necessary.
590 * 2. submit RIP6_RESPONSE packets.
591 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
592 * to invoke this function in every 1 or 5 or 10 seconds only to age the
593 * routes more precisely.
600 struct riprt *rrt, *rrt_tmp;
601 time_t t_lifetime, t_holddown;
603 /* age the RIP routes */
604 t_lifetime = time(NULL) - RIP_LIFETIME;
605 t_holddown = t_lifetime - RIP_HOLDDOWN;
606 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
609 else if (rrt->rrt_t < t_holddown) {
610 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
611 delroute(&rrt->rrt_info, &rrt->rrt_gw);
613 } else if (rrt->rrt_t < t_lifetime)
614 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
617 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
618 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
619 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
621 alarm(ripinterval(SUPPLY_INTERVAL6));
628 const int int0 = 0, int1 = 1, int255 = 255;
629 struct addrinfo hints, *res;
630 char port[NI_MAXSERV];
632 TAILQ_INIT(&ifc_head);
633 nindex2ifc = 0; /*initial guess*/
635 snprintf(port, sizeof(port), "%u", RIP6_PORT);
637 memset(&hints, 0, sizeof(hints));
638 hints.ai_family = PF_INET6;
639 hints.ai_socktype = SOCK_DGRAM;
640 hints.ai_protocol = IPPROTO_UDP;
641 hints.ai_flags = AI_PASSIVE;
642 error = getaddrinfo(NULL, port, &hints, &res);
644 fatal("%s", gai_strerror(error));
648 fatal(":: resolved to multiple address");
652 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
658 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
659 &int1, sizeof(int1)) < 0) {
660 fatal("rip IPV6_V6ONLY");
664 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
668 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
669 &int255, sizeof(int255)) < 0) {
670 fatal("rip IPV6_MULTICAST_HOPS");
673 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
674 &int0, sizeof(int0)) < 0) {
675 fatal("rip IPV6_MULTICAST_LOOP");
679 #ifdef IPV6_RECVPKTINFO
680 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
681 &int1, sizeof(int1)) < 0) {
682 fatal("rip IPV6_RECVPKTINFO");
685 #else /* old adv. API */
686 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
687 &int1, sizeof(int1)) < 0) {
688 fatal("rip IPV6_PKTINFO");
693 #ifdef IPV6_RECVPKTINFO
694 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
695 &int1, sizeof(int1)) < 0) {
696 fatal("rip IPV6_RECVHOPLIMIT");
699 #else /* old adv. API */
700 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
701 &int1, sizeof(int1)) < 0) {
702 fatal("rip IPV6_HOPLIMIT");
707 memset(&hints, 0, sizeof(hints));
708 hints.ai_family = PF_INET6;
709 hints.ai_socktype = SOCK_DGRAM;
710 hints.ai_protocol = IPPROTO_UDP;
711 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
713 fatal("%s", gai_strerror(error));
717 fatal("%s resolved to multiple address", RIP6_DEST);
720 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
724 set[0].events = POLLIN;
730 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
731 fatal("route socket");
736 set[1].events = POLLIN;
745 rtsock = -1; /*just for safety */
750 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
751 if ((sockvecp = malloc(fdmasks)) == NULL) {
755 if ((recvecp = malloc(fdmasks)) == NULL) {
759 memset(sockvecp, 0, fdmasks);
760 FD_SET(ripsock, sockvecp);
762 FD_SET(rtsock, sockvecp);
767 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
770 * ripflush flushes the rip datagram stored in the rip buffer
773 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
779 tracet(1, "Send(%s): info(%d) to %s.%d\n",
781 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
783 tracet(1, "Send: info(%d) to %s.%d\n",
784 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
786 np = ripbuf->rip6_nets;
787 for (i = 0; i < nrt; i++, np++) {
788 if (np->rip6_metric == NEXTHOP_METRIC) {
789 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
790 trace(2, " NextHop reset");
792 trace(2, " NextHop %s",
793 inet6_n2p(&np->rip6_dest));
796 trace(2, " %s/%d[%d]",
797 inet6_n2p(&np->rip6_dest),
798 np->rip6_plen, np->rip6_metric);
801 trace(2, " tag=0x%04x",
802 ntohs(np->rip6_tag) & 0xffff);
807 error = sendpacket(sin6, RIPSIZE(nrt));
808 if (error == EAFNOSUPPORT) {
809 /* Protocol not supported */
810 tracet(1, "Could not send info to %s (%s): "
812 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
813 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
818 * Generate RIP6_RESPONSE packets and send them.
821 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
824 struct in6_addr *nh; /* next hop */
834 * Request from non-link local address is not
835 * a regular route6d update.
837 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
838 sizeof(struct udphdr) -
839 sizeof(struct rip6) + sizeof(struct netinfo6)) /
840 sizeof(struct netinfo6);
843 np = ripbuf->rip6_nets;
844 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
845 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
847 /* Put the route to the buffer */
851 ripflush(NULL, sin6, nrt, np);
854 np = ripbuf->rip6_nets;
857 if (nrt) /* Send last packet */
858 ripflush(NULL, sin6, nrt, np);
862 if ((flag & RRTF_SENDANYWAY) == 0 &&
863 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
867 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
870 /* -T: generate default route only */
871 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
872 struct netinfo6 rrt_info;
873 memset(&rrt_info, 0, sizeof(struct netinfo6));
874 rrt_info.rip6_dest = in6addr_any;
875 rrt_info.rip6_plen = 0;
876 rrt_info.rip6_metric = 1;
877 rrt_info.rip6_metric += ifcp->ifc_metric;
878 rrt_info.rip6_tag = htons(routetag & 0xffff);
879 np = ripbuf->rip6_nets;
882 ripflush(ifcp, sin6, nrt, np);
886 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
887 sizeof(struct udphdr) -
888 sizeof(struct rip6) + sizeof(struct netinfo6)) /
889 sizeof(struct netinfo6);
891 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
892 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
893 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
896 /* Need to check filter here */
897 if (out_filter(rrt, ifcp) == 0)
900 /* Check split horizon and other conditions */
901 if (tobeadv(rrt, ifcp) == 0)
904 /* Only considers the routes with flag if specified */
905 if ((flag & RRTF_CHANGED) &&
906 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
910 if (rrt->rrt_index == ifcp->ifc_index &&
911 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
912 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
913 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
914 if (nrt == maxrte - 2) {
915 ripflush(ifcp, sin6, nrt, np);
918 np = ripbuf->rip6_nets;
921 np->rip6_dest = rrt->rrt_gw;
922 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
923 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
926 np->rip6_metric = NEXTHOP_METRIC;
930 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
931 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
932 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
934 if (nrt == maxrte - 2) {
935 ripflush(ifcp, sin6, nrt, np);
938 np = ripbuf->rip6_nets;
940 memset(np, 0, sizeof(struct netinfo6));
941 np->rip6_metric = NEXTHOP_METRIC;
946 /* Put the route to the buffer */
950 ripflush(ifcp, sin6, nrt, np);
953 np = ripbuf->rip6_nets;
956 if (nrt) /* Send last packet */
957 ripflush(ifcp, sin6, nrt, np);
961 * outbound filter logic, per-route/interface.
964 out_filter(struct riprt *rrt, struct ifc *ifcp)
971 * -A: filter out less specific routes, if we have aggregated
974 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
975 if (iffp->iff_type != 'A')
977 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
979 ia = rrt->rrt_info.rip6_dest;
980 applyplen(&ia, iffp->iff_plen);
981 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
986 * if it is an aggregated route, advertise it only to the
987 * interfaces specified on -A.
989 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
991 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
992 if (iffp->iff_type != 'A')
994 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
995 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1006 * -O: advertise only if prefix matches the configured prefix.
1008 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
1010 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1011 if (iffp->iff_type != 'O')
1013 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1015 ia = rrt->rrt_info.rip6_dest;
1016 applyplen(&ia, iffp->iff_plen);
1017 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1026 /* the prefix should be advertised */
1031 * Determine if the route is to be advertised on the specified interface.
1032 * It checks options specified in the arguments and the split horizon rule.
1035 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1038 /* Special care for static routes */
1039 if (rrt->rrt_flags & RTF_STATIC) {
1040 /* XXX don't advertise reject/blackhole routes */
1041 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1044 if (Sflag) /* Yes, advertise it anyway */
1046 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1050 /* Regular split horizon */
1051 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1057 * Send a rip packet actually.
1060 sendpacket(struct sockaddr_in6 *sin6, int len)
1064 struct iovec iov[2];
1065 u_char cmsgbuf[256];
1066 struct in6_pktinfo *pi;
1068 struct sockaddr_in6 sincopy;
1070 /* do not overwrite the given sin */
1074 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1075 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1076 /* XXX: do not mix the interface index and link index */
1077 idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
1078 SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
1079 sin6->sin6_scope_id = idx;
1083 m.msg_name = (caddr_t)sin6;
1084 m.msg_namelen = sizeof(*sin6);
1085 iov[0].iov_base = (caddr_t)ripbuf;
1086 iov[0].iov_len = len;
1090 m.msg_control = NULL;
1091 m.msg_controllen = 0;
1093 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1094 cm = (struct cmsghdr *)cmsgbuf;
1095 m.msg_control = (caddr_t)cm;
1096 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1098 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1099 cm->cmsg_level = IPPROTO_IPV6;
1100 cm->cmsg_type = IPV6_PKTINFO;
1101 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1102 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1103 pi->ipi6_ifindex = idx;
1106 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1107 trace(1, "sendmsg: %s\n", strerror(errno));
1115 * Receive and process RIP packets. Update the routes/kernel forwarding
1116 * table if necessary.
1121 struct ifc *ifcp, *ic;
1122 struct sockaddr_in6 fsock;
1123 struct in6_addr nh; /* next hop */
1125 struct netinfo6 *np, *nq;
1128 unsigned int need_trigger, idx;
1129 char buf[4 * RIP6_MAXMTU];
1133 struct iovec iov[2];
1134 u_char cmsgbuf[256];
1135 struct in6_pktinfo *pi = NULL;
1140 time_t t_half_lifetime;
1144 m.msg_name = (caddr_t)&fsock;
1145 m.msg_namelen = sizeof(fsock);
1146 iov[0].iov_base = (caddr_t)buf;
1147 iov[0].iov_len = sizeof(buf);
1150 cm = (struct cmsghdr *)cmsgbuf;
1151 m.msg_control = (caddr_t)cm;
1152 m.msg_controllen = sizeof(cmsgbuf);
1153 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1158 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1160 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1161 if (cm->cmsg_level != IPPROTO_IPV6)
1163 switch (cm->cmsg_type) {
1165 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1167 "invalid cmsg length for IPV6_PKTINFO\n");
1170 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1171 idx = pi->ipi6_ifindex;
1174 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1176 "invalid cmsg length for IPV6_HOPLIMIT\n");
1179 hlimp = (int *)CMSG_DATA(cm);
1183 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1184 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1186 if ((size_t)len < sizeof(struct rip6)) {
1187 trace(1, "Packet too short\n");
1191 if (pi == NULL || hlimp == NULL) {
1193 * This can happen when the kernel failed to allocate memory
1194 * for the ancillary data. Although we might be able to handle
1195 * some cases without this info, those are minor and not so
1196 * important, so it's better to discard the packet for safer
1199 trace(1, "IPv6 packet information cannot be retrieved\n");
1203 nh = fsock.sin6_addr;
1204 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1205 sizeof(struct netinfo6);
1206 rp = (struct rip6 *)buf;
1209 if (rp->rip6_vers != RIP6_VERSION) {
1210 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1213 if (rp->rip6_cmd == RIP6_REQUEST) {
1214 if (idx && idx < nindex2ifc) {
1215 ifcp = index2ifc[idx];
1216 riprequest(ifcp, np, nn, &fsock);
1218 riprequest(NULL, np, nn, &fsock);
1223 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1224 trace(1, "Response from non-ll addr: %s\n",
1225 inet6_n2p(&fsock.sin6_addr));
1226 return; /* Ignore packets from non-link-local addr */
1228 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1229 trace(1, "Response from non-rip port from %s\n",
1230 inet6_n2p(&fsock.sin6_addr));
1233 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1235 "Response packet with a smaller hop limit (%d) from %s\n",
1236 *hlimp, inet6_n2p(&fsock.sin6_addr));
1240 * Further validation: since this program does not send off-link
1241 * requests, an incoming response must always come from an on-link
1242 * node. Although this is normally ensured by the source address
1243 * check above, it may not 100% be safe because there are router
1244 * implementations that (invalidly) allow a packet with a link-local
1245 * source address to be forwarded to a different link.
1246 * So we also check whether the destination address is a link-local
1247 * address or the hop limit is 255. Note that RFC2080 does not require
1248 * the specific hop limit for a unicast response, so we cannot assume
1251 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1253 "Response packet possibly from an off-link node: "
1254 "from %s to %s hlim=%d\n",
1255 inet6_n2p(&fsock.sin6_addr),
1256 inet6_n2p(&pi->ipi6_addr), *hlimp);
1260 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1261 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1263 trace(1, "Packets to unknown interface index %d\n", idx);
1264 return; /* Ignore it */
1266 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1267 return; /* The packet is from me; ignore */
1268 if (rp->rip6_cmd != RIP6_RESPONSE) {
1269 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1274 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1277 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1278 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1281 t_half_lifetime = t - (RIP_LIFETIME/2);
1282 for (; nn; nn--, np++) {
1283 if (np->rip6_metric == NEXTHOP_METRIC) {
1284 /* modify neighbor address */
1285 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1287 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1288 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1289 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1290 nh = fsock.sin6_addr;
1291 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1293 nh = fsock.sin6_addr;
1294 trace(1, "\tInvalid Nexthop: %s\n",
1295 inet6_n2p(&np->rip6_dest));
1299 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1300 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1301 inet6_n2p(&np->rip6_dest),
1302 np->rip6_plen, np->rip6_metric);
1305 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1306 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1307 inet6_n2p(&np->rip6_dest),
1308 np->rip6_plen, np->rip6_metric);
1311 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1312 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1313 inet6_n2p(&np->rip6_dest),
1314 np->rip6_plen, np->rip6_metric);
1317 /* may need to pass sitelocal prefix in some case, however*/
1318 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1319 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1320 inet6_n2p(&np->rip6_dest),
1321 np->rip6_plen, np->rip6_metric);
1324 trace(2, "\tnetinfo6: %s/%d [%d]",
1325 inet6_n2p(&np->rip6_dest),
1326 np->rip6_plen, np->rip6_metric);
1328 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1331 applyplen(&ia, np->rip6_plen);
1332 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1333 trace(2, " [junk outside prefix]");
1337 * -L: listen only if the prefix matches the configuration
1339 ok = 1; /* if there's no L filter, it is ok */
1340 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1341 if (iffp->iff_type != IFIL_TYPE_L)
1344 if (np->rip6_plen < iffp->iff_plen)
1346 /* special rule: ::/0 means default, not "in /0" */
1347 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1350 applyplen(&ia, iffp->iff_plen);
1351 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1357 trace(2, " (filtered)\n");
1363 np->rip6_metric += ifcp->ifc_metric;
1364 if (np->rip6_metric > HOPCNT_INFINITY6)
1365 np->rip6_metric = HOPCNT_INFINITY6;
1367 applyplen(&np->rip6_dest, np->rip6_plen);
1368 if ((rrt = rtsearch(np)) != NULL) {
1369 if (rrt->rrt_t == 0)
1370 continue; /* Intf route has priority */
1371 nq = &rrt->rrt_info;
1372 if (nq->rip6_metric > np->rip6_metric) {
1373 if (rrt->rrt_index == ifcp->ifc_index &&
1374 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1375 /* Small metric from the same gateway */
1376 nq->rip6_metric = np->rip6_metric;
1378 /* Better route found */
1379 rrt->rrt_index = ifcp->ifc_index;
1380 /* Update routing table */
1381 delroute(nq, &rrt->rrt_gw);
1384 addroute(rrt, &nh, ifcp);
1386 rrt->rrt_rflags |= RRTF_CHANGED;
1389 } else if (nq->rip6_metric < np->rip6_metric &&
1390 rrt->rrt_index == ifcp->ifc_index &&
1391 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1392 /* Got worse route from same gw */
1393 nq->rip6_metric = np->rip6_metric;
1395 rrt->rrt_rflags |= RRTF_CHANGED;
1397 } else if (nq->rip6_metric == np->rip6_metric &&
1398 np->rip6_metric < HOPCNT_INFINITY6) {
1399 if (rrt->rrt_index == ifcp->ifc_index &&
1400 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1401 /* same metric, same route from same gw */
1403 } else if (rrt->rrt_t < t_half_lifetime) {
1404 /* Better route found */
1405 rrt->rrt_index = ifcp->ifc_index;
1406 /* Update routing table */
1407 delroute(nq, &rrt->rrt_gw);
1410 addroute(rrt, &nh, ifcp);
1411 rrt->rrt_rflags |= RRTF_CHANGED;
1416 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1417 * do not update age value. Do nothing.
1419 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1420 /* Got a new valid route */
1421 if ((rrt = MALLOC(struct riprt)) == NULL) {
1422 fatal("malloc: struct riprt");
1425 memset(rrt, 0, sizeof(*rrt));
1426 nq = &rrt->rrt_info;
1428 rrt->rrt_same = NULL;
1429 rrt->rrt_index = ifcp->ifc_index;
1430 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1433 applyplen(&nq->rip6_dest, nq->rip6_plen);
1434 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1435 rrt->rrt_flags |= RTF_HOST;
1437 /* Update routing table */
1438 addroute(rrt, &nh, ifcp);
1439 rrt->rrt_rflags |= RRTF_CHANGED;
1443 /* Put the route to the list */
1444 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1447 /* XXX need to care the interval between triggered updates */
1449 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1450 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1451 if (ifcp->ifc_index == ic->ifc_index)
1453 if (ic->ifc_flags & IFF_UP)
1454 ripsend(ic, &ic->ifc_ripsin,
1458 /* Reset the flag */
1459 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1460 rrt->rrt_rflags &= ~RRTF_CHANGED;
1466 * Send all routes request packet to the specified interface.
1469 sendrequest(struct ifc *ifcp)
1471 struct netinfo6 *np;
1474 if (ifcp->ifc_flags & IFF_LOOPBACK)
1476 ripbuf->rip6_cmd = RIP6_REQUEST;
1477 np = ripbuf->rip6_nets;
1478 memset(np, 0, sizeof(struct netinfo6));
1479 np->rip6_metric = HOPCNT_INFINITY6;
1480 tracet(1, "Send rtdump Request to %s (%s)\n",
1481 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1482 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1483 if (error == EAFNOSUPPORT) {
1484 /* Protocol not supported */
1485 tracet(1, "Could not send rtdump Request to %s (%s): "
1486 "set IFF_UP to 0\n",
1487 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1488 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1490 ripbuf->rip6_cmd = RIP6_RESPONSE;
1494 * Process a RIP6_REQUEST packet.
1497 riprequest(struct ifc *ifcp,
1498 struct netinfo6 *np,
1500 struct sockaddr_in6 *sin6)
1505 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1506 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1507 /* Specific response, don't split-horizon */
1508 trace(1, "\tRIP Request\n");
1509 for (i = 0; i < nn; i++, np++) {
1512 np->rip6_metric = rrt->rrt_info.rip6_metric;
1514 np->rip6_metric = HOPCNT_INFINITY6;
1516 (void)sendpacket(sin6, RIPSIZE(nn));
1519 /* Whole routing table dump */
1520 trace(1, "\tRIP Request -- whole routing table\n");
1521 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1525 * Get information of each interface.
1530 struct ifaddrs *ifap, *ifa;
1532 struct ipv6_mreq mreq;
1535 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1540 if (getifaddrs(&ifap) != 0) {
1541 fatal("getifaddrs");
1545 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1546 if (ifa->ifa_addr->sa_family != AF_INET6)
1548 ifcp = ifc_find(ifa->ifa_name);
1549 /* we are interested in multicast-capable interfaces */
1550 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1554 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1555 fatal("malloc: struct ifc");
1558 memset(ifcp, 0, sizeof(*ifcp));
1560 ifcp->ifc_index = -1;
1561 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1562 sizeof(ifcp->ifc_name));
1563 TAILQ_INIT(&ifcp->ifc_ifac_head);
1564 TAILQ_INIT(&ifcp->ifc_iff_head);
1565 ifcp->ifc_flags = ifa->ifa_flags;
1566 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1567 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1568 ifflags(ifcp->ifc_flags));
1569 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1572 /* update flag, this may be up again */
1573 if (ifcp->ifc_flags != ifa->ifa_flags) {
1574 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1575 ifflags(ifcp->ifc_flags));
1576 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1577 ifcp->ifc_cflags |= IFC_CHANGED;
1579 ifcp->ifc_flags = ifa->ifa_flags;
1581 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1582 /* maybe temporary failure */
1585 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1586 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1587 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1588 mreq.ipv6mr_interface = ifcp->ifc_index;
1589 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1590 &mreq, sizeof(mreq)) < 0) {
1591 fatal("IPV6_JOIN_GROUP");
1594 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1603 ifconfig1(const char *name,
1604 const struct sockaddr *sa,
1608 struct in6_ifreq ifr;
1609 const struct sockaddr_in6 *sin6;
1614 sin6 = (const struct sockaddr_in6 *)sa;
1615 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1617 ifr.ifr_addr = *sin6;
1618 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1619 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1620 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1623 plen = sin6mask2len(&ifr.ifr_addr);
1624 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1625 /* same interface found */
1626 /* need check if something changed */
1627 /* XXX not yet implemented */
1631 * New address is found
1633 if ((ifac = MALLOC(struct ifac)) == NULL) {
1634 fatal("malloc: struct ifac");
1637 memset(ifac, 0, sizeof(*ifac));
1639 ifac->ifac_ifc = ifcp;
1640 ifac->ifac_addr = sin6->sin6_addr;
1641 ifac->ifac_plen = plen;
1642 ifac->ifac_scope_id = sin6->sin6_scope_id;
1643 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1644 ifr.ifr_addr = *sin6;
1645 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1646 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1649 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1650 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1652 trace(1, "found address %s/%d -- %s\n",
1653 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1655 trace(1, "found address %s/%d\n",
1656 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1658 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1659 ifcp->ifc_mylladdr = ifac->ifac_addr;
1660 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifac->ifac_addr);
1661 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1662 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1664 setindex2ifc(ifcp->ifc_index, ifcp);
1665 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1666 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1667 ifcp->ifc_mtu = RIP6_MAXMTU;
1668 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1669 fatal("ioctl: SIOCGIFMETRIC");
1672 ifcp->ifc_metric = ifr.ifr_metric;
1673 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1674 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1676 ifcp->ifc_cflags |= IFC_CHANGED;
1678 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1684 ifremove(int ifindex)
1689 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1690 if (ifcp->ifc_index == ifindex)
1696 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1697 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1699 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1700 if (rrt->rrt_index == ifcp->ifc_index &&
1701 rrt->rrt_rflags & RRTF_AGGREGATE)
1702 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1708 * Receive and process routing messages.
1709 * Update interface information as necesssary.
1716 struct rt_msghdr *rtm;
1717 struct ifa_msghdr *ifam;
1718 struct if_msghdr *ifm;
1719 struct if_announcemsghdr *ifan;
1721 struct ifc *ifcp, *ic;
1722 int iface = 0, rtable = 0;
1723 struct sockaddr_in6 *rta[RTAX_MAX];
1724 struct sockaddr_in6 mask;
1728 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1729 perror("read from rtsock");
1735 if (len < sizeof(*rtm)) {
1736 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1737 len, (u_long)sizeof(*rtm));
1742 fprintf(stderr, "rtmsg:\n");
1743 for (i = 0; i < len; i++) {
1744 fprintf(stderr, "%02x ", buf[i] & 0xff);
1745 if (i % 16 == 15) fprintf(stderr, "\n");
1747 fprintf(stderr, "\n");
1750 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1751 if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1754 /* safety against bogus message */
1755 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1756 trace(1, "bogus rtmsg: length=%d\n",
1757 ((struct rt_msghdr *)p)->rtm_msglen);
1763 switch (((struct rt_msghdr *)p)->rtm_type) {
1766 ifam = (struct ifa_msghdr *)p;
1767 addrs = ifam->ifam_addrs;
1768 q = (char *)(ifam + 1);
1771 ifm = (struct if_msghdr *)p;
1772 addrs = ifm->ifm_addrs;
1773 q = (char *)(ifm + 1);
1775 case RTM_IFANNOUNCE:
1776 ifan = (struct if_announcemsghdr *)p;
1777 switch (ifan->ifan_what) {
1781 case IFAN_DEPARTURE:
1782 ifremove(ifan->ifan_index);
1788 rtm = (struct rt_msghdr *)p;
1789 addrs = rtm->rtm_addrs;
1790 q = (char *)(rtm + 1);
1791 if (rtm->rtm_version != RTM_VERSION) {
1792 trace(1, "unexpected rtmsg version %d "
1794 rtm->rtm_version, RTM_VERSION);
1797 if (rtm->rtm_pid == pid) {
1799 trace(1, "rtmsg looped back to me, ignored\n");
1805 memset(&rta, 0, sizeof(rta));
1806 for (i = 0; i < RTAX_MAX; i++) {
1807 if (addrs & (1 << i)) {
1808 rta[i] = (struct sockaddr_in6 *)q;
1809 q += ROUNDUP(rta[i]->sin6_len);
1813 trace(1, "rtsock: %s (addrs=%x)\n",
1814 rttypes((struct rt_msghdr *)p), addrs);
1817 i < ((struct rt_msghdr *)p)->rtm_msglen;
1819 fprintf(stderr, "%02x ", p[i] & 0xff);
1820 if (i % 16 == 15) fprintf(stderr, "\n");
1822 fprintf(stderr, "\n");
1828 * We may be able to optimize by using ifm->ifm_index or
1829 * ifam->ifam_index. For simplicity we don't do that here.
1831 switch (((struct rt_msghdr *)p)->rtm_type) {
1843 /* nothing to be done here */
1844 trace(1, "\tnothing to be done, ignored\n");
1849 if (rta[RTAX_DST] == NULL) {
1850 trace(1, "\tno destination, ignored\n");
1853 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1854 trace(1, "\taf mismatch, ignored\n");
1857 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1858 trace(1, "\tlinklocal destination, ignored\n");
1861 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1862 trace(1, "\tloopback destination, ignored\n");
1863 continue; /* Loopback */
1865 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1866 trace(1, "\tmulticast destination, ignored\n");
1872 switch (((struct rt_msghdr *)p)->rtm_type) {
1880 /* should already be handled */
1881 fatal("rtrecv: never reach here");
1884 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1885 trace(1, "\tsome of dst/gw/netamsk are "
1886 "unavailable, ignored\n");
1889 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1890 mask.sin6_len = sizeof(mask);
1891 memset(&mask.sin6_addr, 0xff,
1892 sizeof(mask.sin6_addr));
1893 rta[RTAX_NETMASK] = &mask;
1894 } else if (!rta[RTAX_NETMASK]) {
1895 trace(1, "\tsome of dst/gw/netamsk are "
1896 "unavailable, ignored\n");
1899 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1900 rta[RTAX_NETMASK]) == 0) {
1901 rtable++; /*just to be sure*/
1906 trace(1, "\tnot supported yet, ignored\n");
1909 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1910 trace(1, "\tno netmask or ifa given, ignored\n");
1913 if (ifam->ifam_index < nindex2ifc)
1914 ifcp = index2ifc[ifam->ifam_index];
1918 trace(1, "\tinvalid ifam_index %d, ignored\n",
1922 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1927 trace(1, "\tnot supported yet, ignored\n");
1934 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1936 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1937 if (ifcp->ifc_cflags & IFC_CHANGED) {
1938 if (ifrt(ifcp, 1)) {
1939 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1940 if (ifcp->ifc_index == ic->ifc_index)
1942 if (ic->ifc_flags & IFF_UP)
1943 ripsend(ic, &ic->ifc_ripsin,
1946 /* Reset the flag */
1947 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1948 rrt->rrt_rflags &= ~RRTF_CHANGED;
1951 ifcp->ifc_cflags &= ~IFC_CHANGED;
1956 trace(1, "rtsock: read routing table again\n");
1962 * remove specified route from the internal routing table.
1965 rt_del(const struct sockaddr_in6 *sdst,
1966 const struct sockaddr_in6 *sgw,
1967 const struct sockaddr_in6 *smask)
1969 const struct in6_addr *dst = NULL;
1970 const struct in6_addr *gw = NULL;
1972 struct netinfo6 ni6;
1973 struct riprt *rrt = NULL;
1976 if (sdst->sin6_family != AF_INET6) {
1977 trace(1, "\tother AF, ignored\n");
1980 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1981 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1982 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1983 trace(1, "\taddress %s not interesting, ignored\n",
1984 inet6_n2p(&sdst->sin6_addr));
1987 dst = &sdst->sin6_addr;
1988 if (sgw->sin6_family == AF_INET6) {
1990 gw = &sgw->sin6_addr;
1991 prefix = sin6mask2len(smask);
1992 } else if (sgw->sin6_family == AF_LINK) {
1994 * Interface route... a hard case. We need to get the prefix
1995 * length from the kernel, but we now are parsing rtmsg.
1996 * We'll purge matching routes from my list, then get the
1999 struct riprt *longest;
2000 trace(1, "\t%s is an interface route, guessing prefixlen\n",
2003 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
2004 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
2006 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
2008 || longest->rrt_info.rip6_plen <
2009 rrt->rrt_info.rip6_plen) {
2016 trace(1, "\tno matching interface route found\n");
2019 gw = &in6addr_loopback;
2020 prefix = rrt->rrt_info.rip6_plen;
2022 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2026 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2027 trace(1, "gw %s\n", inet6_n2p(gw));
2028 t_lifetime = time(NULL) - RIP_LIFETIME;
2029 /* age route for interface address */
2030 memset(&ni6, 0, sizeof(ni6));
2031 ni6.rip6_dest = *dst;
2032 ni6.rip6_plen = prefix;
2033 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2034 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2036 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2037 trace(1, "\tno route found\n");
2041 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2042 trace(1, "\tyou can delete static routes only\n");
2045 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2046 trace(1, "\tgw mismatch: %s <-> ",
2047 inet6_n2p(&rrt->rrt_gw));
2048 trace(1, "%s\n", inet6_n2p(gw));
2050 trace(1, "\troute found, age it\n");
2051 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2052 rrt->rrt_t = t_lifetime;
2053 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2060 * remove specified address from internal interface/routing table.
2063 rt_deladdr(struct ifc *ifcp,
2064 const struct sockaddr_in6 *sifa,
2065 const struct sockaddr_in6 *smask)
2067 const struct in6_addr *addr = NULL;
2069 struct ifac *ifac = NULL;
2070 struct netinfo6 ni6;
2071 struct riprt *rrt = NULL;
2075 if (sifa->sin6_family != AF_INET6) {
2076 trace(1, "\tother AF, ignored\n");
2079 addr = &sifa->sin6_addr;
2080 prefix = sin6mask2len(smask);
2082 trace(1, "\tdeleting %s/%d from %s\n",
2083 inet6_n2p(addr), prefix, ifcp->ifc_name);
2084 ifac = ifa_match(ifcp, addr, prefix);
2086 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2087 inet6_n2p(addr), prefix, ifcp->ifc_name);
2090 if (ifac->ifac_ifc != ifcp) {
2091 trace(1, "\taddress table corrupt: back pointer does not match "
2093 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2096 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2097 t_lifetime = time(NULL) - RIP_LIFETIME;
2098 /* age route for interface address */
2099 memset(&ni6, 0, sizeof(ni6));
2100 ni6.rip6_dest = ifac->ifac_addr;
2101 ni6.rip6_plen = ifac->ifac_plen;
2102 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2103 trace(1, "\tfind interface route %s/%d on %d\n",
2104 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2105 if ((rrt = rtsearch(&ni6)) != NULL) {
2106 struct in6_addr none;
2107 memset(&none, 0, sizeof(none));
2108 if (rrt->rrt_index == ifcp->ifc_index &&
2109 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2110 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2111 trace(1, "\troute found, age it\n");
2112 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2113 rrt->rrt_t = t_lifetime;
2114 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2118 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2119 inet6_n2p(&rrt->rrt_info.rip6_dest),
2120 rrt->rrt_info.rip6_plen,
2124 trace(1, "\tno interface route found\n");
2125 /* age route for p2p destination */
2126 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2127 memset(&ni6, 0, sizeof(ni6));
2128 ni6.rip6_dest = ifac->ifac_raddr;
2129 ni6.rip6_plen = 128;
2130 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2131 trace(1, "\tfind p2p route %s/%d on %d\n",
2132 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2134 if ((rrt = rtsearch(&ni6)) != NULL) {
2135 if (rrt->rrt_index == ifcp->ifc_index &&
2136 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2137 &ifac->ifac_addr)) {
2138 trace(1, "\troute found, age it\n");
2139 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2140 rrt->rrt_t = t_lifetime;
2141 rrt->rrt_info.rip6_metric =
2146 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2147 inet6_n2p(&rrt->rrt_info.rip6_dest),
2148 rrt->rrt_info.rip6_plen,
2152 trace(1, "\tno p2p route found\n");
2156 return ((updated) ? 0 : -1);
2160 * Get each interface address and put those interface routes to the route
2164 ifrt(struct ifc *ifcp, int again)
2167 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2168 struct netinfo6 *np;
2170 int need_trigger = 0;
2173 if (ifcp->ifc_flags & IFF_LOOPBACK)
2174 return 0; /* ignore loopback */
2177 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2178 ifrt_p2p(ifcp, again);
2182 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2183 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2185 trace(1, "route: %s on %s: "
2186 "skip linklocal interface address\n",
2187 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2191 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2193 trace(1, "route: %s: skip unspec interface address\n",
2198 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2200 trace(1, "route: %s: skip loopback address\n",
2205 if (ifcp->ifc_flags & IFF_UP) {
2206 if ((rrt = MALLOC(struct riprt)) == NULL)
2207 fatal("malloc: struct riprt");
2208 memset(rrt, 0, sizeof(*rrt));
2209 rrt->rrt_same = NULL;
2210 rrt->rrt_index = ifcp->ifc_index;
2211 rrt->rrt_t = 0; /* don't age */
2212 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2213 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2214 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2215 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2216 rrt->rrt_flags = RTF_HOST;
2217 rrt->rrt_rflags |= RRTF_CHANGED;
2218 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2219 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2220 rrt->rrt_gw = ifac->ifac_addr;
2221 np = &rrt->rrt_info;
2222 search_rrt = rtsearch(np);
2223 if (search_rrt != NULL) {
2224 if (search_rrt->rrt_info.rip6_metric <=
2225 rrt->rrt_info.rip6_metric) {
2226 /* Already have better route */
2228 trace(1, "route: %s/%d: "
2229 "already registered (%s)\n",
2230 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2236 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2237 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2239 /* Attach the route to the list */
2240 trace(1, "route: %s/%d: register route (%s)\n",
2241 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2243 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2244 addroute(rrt, &rrt->rrt_gw, ifcp);
2247 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2250 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2251 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2252 t_lifetime = time(NULL) - RIP_LIFETIME;
2253 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2254 loop_rrt->rrt_t = t_lifetime;
2255 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2256 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2266 return need_trigger;
2270 * there are couple of p2p interface routing models. "behavior" lets
2271 * you pick one. it looks that gated behavior fits best with BSDs,
2272 * since BSD kernels do not look at prefix length on p2p interfaces.
2275 ifrt_p2p(struct ifc *ifcp, int again)
2278 struct riprt *rrt, *orrt;
2279 struct netinfo6 *np;
2280 struct in6_addr addr, dest;
2281 int advert, ignore, i;
2282 #define P2PADVERT_NETWORK 1
2283 #define P2PADVERT_ADDR 2
2284 #define P2PADVERT_DEST 4
2285 #define P2PADVERT_MAX 4
2286 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2287 const char *category = "";
2290 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2291 addr = ifac->ifac_addr;
2292 dest = ifac->ifac_raddr;
2293 applyplen(&addr, ifac->ifac_plen);
2294 applyplen(&dest, ifac->ifac_plen);
2295 advert = ignore = 0;
2299 * honor addr/plen, just like normal shared medium
2300 * interface. this may cause trouble if you reuse
2301 * addr/plen on other interfaces.
2303 * advertise addr/plen.
2305 advert |= P2PADVERT_NETWORK;
2309 * prefixlen on p2p interface is meaningless.
2310 * advertise addr/128 and dest/128.
2312 * do not install network route to route6d routing
2313 * table (if we do, it would prevent route installation
2314 * for other p2p interface that shares addr/plen).
2316 * XXX what should we do if dest is ::? it will not
2317 * get announced anyways (see following filter),
2318 * but we need to think.
2320 advert |= P2PADVERT_ADDR;
2321 advert |= P2PADVERT_DEST;
2322 ignore |= P2PADVERT_NETWORK;
2326 * just for testing. actually the code is redundant
2327 * given the current p2p interface address assignment
2328 * rule for kame kernel.
2331 * A/n -> announce A/n
2332 * A B/n, A and B share prefix -> A/n (= B/n)
2333 * A B/n, do not share prefix -> A/128 and B/128
2334 * actually, A/64 and A B/128 are the only cases
2335 * permitted by the kernel:
2337 * A B/128 -> A/128 and B/128
2339 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2340 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2341 advert |= P2PADVERT_NETWORK;
2343 advert |= P2PADVERT_ADDR;
2344 advert |= P2PADVERT_DEST;
2345 ignore |= P2PADVERT_NETWORK;
2348 advert |= P2PADVERT_NETWORK;
2352 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2353 if ((ignore & i) != 0)
2355 if ((rrt = MALLOC(struct riprt)) == NULL) {
2356 fatal("malloc: struct riprt");
2359 memset(rrt, 0, sizeof(*rrt));
2360 rrt->rrt_same = NULL;
2361 rrt->rrt_index = ifcp->ifc_index;
2362 rrt->rrt_t = 0; /* don't age */
2364 case P2PADVERT_NETWORK:
2365 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2366 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2367 applyplen(&rrt->rrt_info.rip6_dest,
2369 category = "network";
2371 case P2PADVERT_ADDR:
2372 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2373 rrt->rrt_info.rip6_plen = 128;
2374 rrt->rrt_gw = in6addr_loopback;
2377 case P2PADVERT_DEST:
2378 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2379 rrt->rrt_info.rip6_plen = 128;
2380 rrt->rrt_gw = ifac->ifac_addr;
2384 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2385 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2387 trace(1, "route: %s: skip unspec/linklocal "
2388 "(%s on %s)\n", category, ifcp->ifc_name);
2393 if ((advert & i) == 0) {
2394 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2398 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2399 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2400 np = &rrt->rrt_info;
2401 orrt = rtsearch(np);
2403 /* Attach the route to the list */
2404 trace(1, "route: %s/%d: register route "
2406 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2407 category, ifcp->ifc_name, noadv);
2408 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2409 } else if (rrt->rrt_index != orrt->rrt_index ||
2410 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2412 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2413 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2416 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2417 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2418 category, ifcp->ifc_name, noadv);
2422 trace(1, "route: %s/%d: "
2423 "already registered (%s on %s%s)\n",
2424 inet6_n2p(&np->rip6_dest),
2425 np->rip6_plen, category,
2426 ifcp->ifc_name, noadv);
2432 #undef P2PADVERT_NETWORK
2433 #undef P2PADVERT_ADDR
2434 #undef P2PADVERT_DEST
2435 #undef P2PADVERT_MAX
2439 getifmtu(int ifindex)
2444 struct if_msghdr *ifm;
2451 mib[4] = NET_RT_IFLIST;
2453 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2454 fatal("sysctl estimate NET_RT_IFLIST");
2457 if ((buf = malloc(msize)) == NULL) {
2461 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2462 fatal("sysctl NET_RT_IFLIST");
2465 ifm = (struct if_msghdr *)buf;
2466 mtu = ifm->ifm_data.ifi_mtu;
2467 if (ifindex != ifm->ifm_index) {
2468 fatal("ifindex does not match with ifm_index");
2476 rttypes(struct rt_msghdr *rtm)
2478 #define RTTYPE(s, f) \
2480 if (rtm->rtm_type == (f)) \
2483 RTTYPE("ADD", RTM_ADD);
2484 RTTYPE("DELETE", RTM_DELETE);
2485 RTTYPE("CHANGE", RTM_CHANGE);
2486 RTTYPE("GET", RTM_GET);
2487 RTTYPE("LOSING", RTM_LOSING);
2488 RTTYPE("REDIRECT", RTM_REDIRECT);
2489 RTTYPE("MISS", RTM_MISS);
2490 RTTYPE("LOCK", RTM_LOCK);
2491 RTTYPE("OLDADD", RTM_OLDADD);
2492 RTTYPE("OLDDEL", RTM_OLDDEL);
2493 RTTYPE("NEWADDR", RTM_NEWADDR);
2494 RTTYPE("DELADDR", RTM_DELADDR);
2495 RTTYPE("IFINFO", RTM_IFINFO);
2497 RTTYPE("OLDADD", RTM_OLDADD);
2500 RTTYPE("OLDDEL", RTM_OLDDEL);
2503 RTTYPE("OIFINFO", RTM_OIFINFO);
2505 #ifdef RTM_IFANNOUNCE
2506 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2509 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2512 RTTYPE("DELMADDR", RTM_DELMADDR);
2519 rtflags(struct rt_msghdr *rtm)
2521 static char buf[BUFSIZ];
2524 * letter conflict should be okay. painful when *BSD diverges...
2526 strlcpy(buf, "", sizeof(buf));
2527 #define RTFLAG(s, f) \
2529 if (rtm->rtm_flags & (f)) \
2530 strlcat(buf, (s), sizeof(buf)); \
2532 RTFLAG("U", RTF_UP);
2533 RTFLAG("G", RTF_GATEWAY);
2534 RTFLAG("H", RTF_HOST);
2535 RTFLAG("R", RTF_REJECT);
2536 RTFLAG("D", RTF_DYNAMIC);
2537 RTFLAG("M", RTF_MODIFIED);
2538 RTFLAG("d", RTF_DONE);
2540 RTFLAG("m", RTF_MASK);
2543 RTFLAG("C", RTF_CLONING);
2546 RTFLAG("c", RTF_CLONED);
2548 #ifdef RTF_PRCLONING
2549 RTFLAG("c", RTF_PRCLONING);
2551 #ifdef RTF_WASCLONED
2552 RTFLAG("W", RTF_WASCLONED);
2554 RTFLAG("X", RTF_XRESOLVE);
2556 RTFLAG("L", RTF_LLINFO);
2558 RTFLAG("S", RTF_STATIC);
2559 RTFLAG("B", RTF_BLACKHOLE);
2561 RTFLAG("3", RTF_PROTO3);
2563 RTFLAG("2", RTF_PROTO2);
2564 RTFLAG("1", RTF_PROTO1);
2565 #ifdef RTF_BROADCAST
2566 RTFLAG("b", RTF_BROADCAST);
2569 RTFLAG("d", RTF_DEFAULT);
2571 #ifdef RTF_ISAROUTER
2572 RTFLAG("r", RTF_ISAROUTER);
2575 RTFLAG("T", RTF_TUNNEL);
2578 RTFLAG("A", RTF_AUTH);
2581 RTFLAG("E", RTF_CRYPT);
2590 static char buf[BUFSIZ];
2592 strlcpy(buf, "", sizeof(buf));
2593 #define IFFLAG(s, f) \
2595 if (flags & (f)) { \
2597 strlcat(buf, ",", sizeof(buf)); \
2598 strlcat(buf, (s), sizeof(buf)); \
2601 IFFLAG("UP", IFF_UP);
2602 IFFLAG("BROADCAST", IFF_BROADCAST);
2603 IFFLAG("DEBUG", IFF_DEBUG);
2604 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2605 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2606 #ifdef IFF_NOTRAILERS
2607 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2610 IFFLAG("SMART", IFF_SMART);
2612 IFFLAG("RUNNING", IFF_RUNNING);
2613 IFFLAG("NOARP", IFF_NOARP);
2614 IFFLAG("PROMISC", IFF_PROMISC);
2615 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2616 IFFLAG("OACTIVE", IFF_OACTIVE);
2617 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2618 IFFLAG("LINK0", IFF_LINK0);
2619 IFFLAG("LINK1", IFF_LINK1);
2620 IFFLAG("LINK2", IFF_LINK2);
2621 IFFLAG("MULTICAST", IFF_MULTICAST);
2631 char *buf, *p, *lim;
2632 struct rt_msghdr *rtm;
2641 mib[3] = AF_INET6; /* Address family */
2642 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2643 mib[5] = 0; /* No flags */
2651 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2652 errmsg = "sysctl estimate";
2655 if ((buf = malloc(msize)) == NULL) {
2659 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2660 errmsg = "sysctl NET_RT_DUMP";
2663 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2665 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2668 } else if (1 < retry)
2669 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2672 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2673 rtm = (struct rt_msghdr *)p;
2674 rt_entry(rtm, again);
2680 rt_entry(struct rt_msghdr *rtm, int again)
2682 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2683 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2684 char *rtmp, *ifname = NULL;
2685 struct riprt *rrt, *orrt;
2686 struct netinfo6 *np;
2689 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2690 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2691 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2692 return; /* not interested in the link route */
2694 /* do not look at cloned routes */
2695 #ifdef RTF_WASCLONED
2696 if (rtm->rtm_flags & RTF_WASCLONED)
2700 if (rtm->rtm_flags & RTF_CLONED)
2703 /* XXX: Ignore connected routes. */
2704 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2707 * do not look at dynamic routes.
2708 * netbsd/openbsd cloned routes have UGHD.
2710 if (rtm->rtm_flags & RTF_DYNAMIC)
2712 rtmp = (char *)(rtm + 1);
2714 if ((rtm->rtm_addrs & RTA_DST) == 0)
2715 return; /* ignore routes without destination address */
2716 sin6_dst = (struct sockaddr_in6 *)rtmp;
2717 rtmp += ROUNDUP(sin6_dst->sin6_len);
2718 if (rtm->rtm_addrs & RTA_GATEWAY) {
2719 sin6_gw = (struct sockaddr_in6 *)rtmp;
2720 rtmp += ROUNDUP(sin6_gw->sin6_len);
2722 if (rtm->rtm_addrs & RTA_NETMASK) {
2723 sin6_mask = (struct sockaddr_in6 *)rtmp;
2724 rtmp += ROUNDUP(sin6_mask->sin6_len);
2726 if (rtm->rtm_addrs & RTA_GENMASK) {
2727 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2728 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2730 if (rtm->rtm_addrs & RTA_IFP) {
2731 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2732 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2736 if (sin6_dst->sin6_family != AF_INET6)
2738 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2739 return; /* Link-local */
2740 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2741 return; /* Loopback */
2742 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2745 if ((rrt = MALLOC(struct riprt)) == NULL) {
2746 fatal("malloc: struct riprt");
2749 memset(rrt, 0, sizeof(*rrt));
2750 np = &rrt->rrt_info;
2751 rrt->rrt_same = NULL;
2752 rrt->rrt_t = time(NULL);
2753 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2754 rrt->rrt_t = 0; /* Don't age static routes */
2755 if (rtm->rtm_flags & Pflag)
2756 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2757 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2758 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2760 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2761 if (np->rip6_metric < 1)
2762 np->rip6_metric = 1;
2763 rrt->rrt_flags = rtm->rtm_flags;
2764 np->rip6_dest = sin6_dst->sin6_addr;
2767 if (rtm->rtm_flags & RTF_HOST)
2768 np->rip6_plen = 128; /* Host route */
2770 np->rip6_plen = sin6mask2len(sin6_mask);
2774 orrt = rtsearch(np);
2775 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2778 trace(1, "route: %s/%d flags %s: already registered\n",
2779 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2787 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2789 if (sin6_gw->sin6_family == AF_INET6)
2790 rrt->rrt_gw = sin6_gw->sin6_addr;
2791 else if (sin6_gw->sin6_family == AF_LINK) {
2792 /* XXX in case ppp link? */
2793 rrt->rrt_gw = in6addr_loopback;
2795 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2797 trace(1, "route: %s/%d flags %s",
2798 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2799 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2802 ifindex = rtm->rtm_index;
2803 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2804 ifname = index2ifc[ifindex]->ifc_name;
2806 trace(1, " not configured\n");
2810 trace(1, " if %s sock %d", ifname, ifindex);
2811 rrt->rrt_index = ifindex;
2816 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2817 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2818 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2819 trace(0, "***** Gateway %s is not a link-local address.\n",
2820 inet6_n2p(&rrt->rrt_gw));
2821 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2822 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2823 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2826 /* Put it to the route list */
2827 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2828 /* replace route list */
2829 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2830 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2832 trace(1, "route: %s/%d flags %s: replace new route\n",
2833 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2837 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2841 addroute(struct riprt *rrt,
2842 const struct in6_addr *gw,
2845 struct netinfo6 *np;
2846 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2847 struct rt_msghdr *rtm;
2848 struct sockaddr_in6 *sin6;
2851 np = &rrt->rrt_info;
2852 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2853 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2854 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2855 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2856 np->rip6_metric - 1, buf2);
2858 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2859 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2860 np->rip6_metric - 1, buf2);
2864 memset(buf, 0, sizeof(buf));
2865 rtm = (struct rt_msghdr *)buf;
2866 rtm->rtm_type = RTM_ADD;
2867 rtm->rtm_version = RTM_VERSION;
2868 rtm->rtm_seq = ++seq;
2870 rtm->rtm_flags = rrt->rrt_flags;
2871 rtm->rtm_flags |= Qflag;
2872 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2873 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2874 rtm->rtm_inits = RTV_HOPCOUNT;
2875 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2877 sin6->sin6_len = sizeof(struct sockaddr_in6);
2878 sin6->sin6_family = AF_INET6;
2879 sin6->sin6_addr = np->rip6_dest;
2880 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2882 sin6->sin6_len = sizeof(struct sockaddr_in6);
2883 sin6->sin6_family = AF_INET6;
2884 sin6->sin6_addr = *gw;
2885 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2887 sin6->sin6_len = sizeof(struct sockaddr_in6);
2888 sin6->sin6_family = AF_INET6;
2889 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2890 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2892 len = (char *)sin6 - (char *)buf;
2893 rtm->rtm_msglen = len;
2894 if (write(rtsock, buf, len) > 0)
2897 if (errno == EEXIST) {
2898 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2899 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2901 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2902 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2904 trace(0, "Can not write to rtsock (addroute): %s\n",
2907 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2914 delroute(struct netinfo6 *np, struct in6_addr *gw)
2916 u_char buf[BUFSIZ], buf2[BUFSIZ];
2917 struct rt_msghdr *rtm;
2918 struct sockaddr_in6 *sin6;
2921 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2922 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2923 np->rip6_plen, buf2);
2925 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2926 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2930 memset(buf, 0, sizeof(buf));
2931 rtm = (struct rt_msghdr *)buf;
2932 rtm->rtm_type = RTM_DELETE;
2933 rtm->rtm_version = RTM_VERSION;
2934 rtm->rtm_seq = ++seq;
2936 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2937 rtm->rtm_flags |= Qflag;
2938 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2939 rtm->rtm_flags |= RTF_HOST;
2940 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2941 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2943 sin6->sin6_len = sizeof(struct sockaddr_in6);
2944 sin6->sin6_family = AF_INET6;
2945 sin6->sin6_addr = np->rip6_dest;
2946 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2948 sin6->sin6_len = sizeof(struct sockaddr_in6);
2949 sin6->sin6_family = AF_INET6;
2950 sin6->sin6_addr = *gw;
2951 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2953 sin6->sin6_len = sizeof(struct sockaddr_in6);
2954 sin6->sin6_family = AF_INET6;
2955 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2956 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2958 len = (char *)sin6 - (char *)buf;
2959 rtm->rtm_msglen = len;
2960 if (write(rtsock, buf, len) >= 0)
2963 if (errno == ESRCH) {
2964 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2965 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2967 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2968 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2970 trace(0, "Can not write to rtsock (delroute): %s\n",
2973 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2980 getroute(struct netinfo6 *np, struct in6_addr *gw)
2985 struct rt_msghdr *rtm;
2986 struct sockaddr_in6 *sin6;
2988 rtm = (struct rt_msghdr *)buf;
2989 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2990 memset(rtm, 0, len);
2991 rtm->rtm_type = RTM_GET;
2992 rtm->rtm_version = RTM_VERSION;
2994 rtm->rtm_seq = myseq;
2995 rtm->rtm_addrs = RTA_DST;
2996 rtm->rtm_msglen = len;
2997 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2998 sin6->sin6_len = sizeof(struct sockaddr_in6);
2999 sin6->sin6_family = AF_INET6;
3000 sin6->sin6_addr = np->rip6_dest;
3001 if (write(rtsock, buf, len) < 0) {
3002 if (errno == ESRCH) /* No such route found */
3004 perror("write to rtsock");
3008 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
3009 perror("read from rtsock");
3012 rtm = (struct rt_msghdr *)buf;
3013 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
3014 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
3015 if (rtm->rtm_addrs & RTA_DST) {
3016 sin6 = (struct sockaddr_in6 *)
3017 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
3019 if (rtm->rtm_addrs & RTA_GATEWAY) {
3020 *gw = sin6->sin6_addr;
3027 inet6_n2p(const struct in6_addr *p)
3029 static char buf[BUFSIZ];
3031 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3052 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3055 fprintf(dump, "%s: Interface Table Dump\n", hms());
3056 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3058 fprintf(dump, " Number of interfaces: %d\n", nifc);
3060 fprintf(dump, " advertising interfaces:\n");
3061 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3062 if ((ifcp->ifc_flags & IFF_UP) == 0)
3064 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3066 ifdump0(dump, ifcp);
3068 fprintf(dump, "\n");
3069 fprintf(dump, " non-advertising interfaces:\n");
3070 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3071 if ((ifcp->ifc_flags & IFF_UP) &&
3072 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3074 ifdump0(dump, ifcp);
3076 fprintf(dump, "\n");
3082 ifdump0(FILE *dump, const struct ifc *ifcp)
3090 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3091 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3092 inet6_n2p(&ifcp->ifc_mylladdr),
3093 ifcp->ifc_mtu, ifcp->ifc_metric);
3094 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3095 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3096 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3098 fprintf(dump, "\t%s/%d -- %s\n",
3099 inet6_n2p(&ifac->ifac_addr),
3100 ifac->ifac_plen, buf);
3102 fprintf(dump, "\t%s/%d\n",
3103 inet6_n2p(&ifac->ifac_addr),
3108 fprintf(dump, "\tFilter:\n");
3109 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3111 switch (iffp->iff_type) {
3113 ft = "Aggregate"; addr++; break;
3115 ft = "No-use"; break;
3117 ft = "Advertise-only"; addr++; break;
3119 ft = "Default-only"; break;
3121 ft = "Listen-only"; addr++; break;
3123 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3128 fprintf(dump, "\t\t%s", ft);
3130 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3132 fprintf(dump, "\n");
3134 fprintf(dump, "\n");
3148 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3152 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3153 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3154 if (rrt->rrt_t == 0)
3157 age = t - rrt->rrt_t;
3158 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3160 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3161 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3162 index2ifc[rrt->rrt_index]->ifc_name,
3163 inet6_n2p(&rrt->rrt_gw),
3164 rrt->rrt_info.rip6_metric, (long)age);
3165 if (rrt->rrt_info.rip6_tag) {
3166 fprintf(dump, " tag(0x%04x)",
3167 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3169 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3170 fprintf(dump, " NOT-LL");
3171 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3172 fprintf(dump, " NO-ADV");
3173 fprintf(dump, "\n");
3175 fprintf(dump, "\n");
3181 * Parse the -A (and -O) options and put corresponding filter object to the
3182 * specified interface structures. Each of the -A/O option has the following
3183 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3184 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3190 char *p, *ap, *iflp, *ifname, *ep;
3191 struct iff iff, *iffp;
3199 for (i = 0; i < nfilter; i++) {
3203 memset(iffp, 0, sizeof(*iffp));
3204 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3208 if ((p = strchr(ap, ',')) != NULL) {
3212 if ((p = strchr(ap, '/')) == NULL) {
3213 fatal("no prefixlen specified for '%s'", ap);
3217 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3218 fatal("invalid prefix specified for '%s'", ap);
3223 plen = strtoul(p, &ep, 10);
3224 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3225 fatal("invalid prefix length specified for '%s'", ap);
3228 iffp->iff_plen = plen;
3229 applyplen(&iffp->iff_addr, iffp->iff_plen);
3231 iffp->iff_type = filtertype[i];
3232 if (iflp == NULL || *iflp == '\0') {
3233 fatal("no interface specified for '%s'", ap);
3236 /* parse the interface listing portion */
3239 if ((iflp = strchr(iflp, ',')) != NULL)
3242 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3243 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3246 iffp = malloc(sizeof(*iffp));
3248 fatal("malloc of iff");
3251 memcpy(iffp, &iff, sizeof(*iffp));
3253 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3255 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3260 * -A: aggregate configuration.
3262 if (filtertype[i] != IFIL_TYPE_A)
3264 /* put the aggregate to the kernel routing table */
3265 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3267 fatal("malloc: rrt");
3270 memset(rrt, 0, sizeof(struct riprt));
3271 rrt->rrt_info.rip6_dest = iff.iff_addr;
3272 rrt->rrt_info.rip6_plen = iff.iff_plen;
3273 rrt->rrt_info.rip6_metric = 1;
3274 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3275 rrt->rrt_gw = in6addr_loopback;
3276 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3277 rrt->rrt_rflags = RRTF_AGGREGATE;
3279 rrt->rrt_index = loopifcp->ifc_index;
3281 if (getroute(&rrt->rrt_info, &gw)) {
3284 * When the address has already been registered in the
3285 * kernel routing table, it should be removed
3287 delroute(&rrt->rrt_info, &gw);
3289 /* it is safer behavior */
3291 fatal("%s/%u already in routing table, "
3293 inet6_n2p(&rrt->rrt_info.rip6_dest),
3294 rrt->rrt_info.rip6_plen);
3299 /* Put the route to the list */
3300 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3301 trace(1, "Aggregate: %s/%d for %s\n",
3302 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3303 loopifcp->ifc_name);
3304 /* Add this route to the kernel */
3305 if (nflag) /* do not modify kernel routing table */
3307 addroute(rrt, &in6addr_loopback, loopifcp);
3311 /***************** utility functions *****************/
3314 * Returns a pointer to ifac whose address and prefix length matches
3315 * with the address and prefix length specified in the arguments.
3318 ifa_match(const struct ifc *ifcp,
3319 const struct in6_addr *ia,
3324 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3325 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3326 ifac->ifac_plen == plen)
3334 * Return a pointer to riprt structure whose address and prefix length
3335 * matches with the address and prefix length found in the argument.
3336 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3339 rtsearch(struct netinfo6 *np)
3343 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3344 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3345 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3354 sin6mask2len(const struct sockaddr_in6 *sin6)
3357 return mask2len(&sin6->sin6_addr,
3358 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3362 mask2len(const struct in6_addr *addr, int lenlim)
3365 const u_char *p = (const u_char *)addr;
3367 for (j = 0; j < lenlim; j++, p++) {
3374 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3375 MASKLEN(0xfe, 7); break;
3376 MASKLEN(0xfc, 6); break;
3377 MASKLEN(0xf8, 5); break;
3378 MASKLEN(0xf0, 4); break;
3379 MASKLEN(0xe0, 3); break;
3380 MASKLEN(0xc0, 2); break;
3381 MASKLEN(0x80, 1); break;
3389 applymask(struct in6_addr *addr, struct in6_addr *mask)
3394 p = (u_long *)addr; q = (u_long *)mask;
3395 for (i = 0; i < 4; i++)
3399 static const u_char plent[8] = {
3400 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3404 applyplen(struct in6_addr *ia, int plen)
3410 for (i = 0; i < 16; i++) {
3419 static const int pl2m[9] = {
3420 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3426 static struct in6_addr ia;
3430 memset(&ia, 0, sizeof(struct in6_addr));
3432 for (i = 0; i < 16; i++, p++, n -= 8) {
3446 int len = strlen(p) + 1;
3447 char *q = (char *)malloc(len);
3461 static char buf[BUFSIZ];
3466 if ((tm = localtime(&t)) == 0) {
3470 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3475 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3478 ripinterval(int timer)
3482 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3483 nextalarm = time(NULL) + interval;
3493 t = (int)(RIP_TRIG_INT6_MIN +
3494 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3495 sup_trig_update = time(NULL) + t;
3501 fatal(const char *fmt, ...)
3503 fatal(fmt, va_alist)
3516 vsnprintf(buf, sizeof(buf), fmt, ap);
3520 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3522 syslog(LOG_ERR, "%s", buf);
3528 tracet(int level, const char *fmt, ...)
3530 tracet(level, fmt, va_alist)
3538 if (level <= dflag) {
3544 fprintf(stderr, "%s: ", hms());
3545 vfprintf(stderr, fmt, ap);
3555 vsyslog(LOG_DEBUG, fmt, ap);
3557 vsyslog(LOG_WARNING, fmt, ap);
3564 trace(int level, const char *fmt, ...)
3566 trace(level, fmt, va_alist)
3574 if (level <= dflag) {
3580 vfprintf(stderr, fmt, ap);
3590 vsyslog(LOG_DEBUG, fmt, ap);
3592 vsyslog(LOG_WARNING, fmt, ap);
3600 struct if_nameindex *p, *p0;
3601 unsigned int max = 0;
3603 p0 = if_nameindex();
3604 for (p = p0; p && p->if_index && p->if_name; p++) {
3605 if (max < p->if_index)
3608 if_freenameindex(p0);
3613 ifc_find(char *name)
3617 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3618 if (strcmp(name, ifcp->ifc_name) == 0)
3625 iff_find(struct ifc *ifcp, int type)
3629 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3630 if (type == IFIL_TYPE_ANY ||
3631 type == iffp->iff_type)
3639 setindex2ifc(int idx, struct ifc *ifcp)
3645 nindex2ifc = 5; /*initial guess*/
3646 index2ifc = (struct ifc **)
3647 malloc(sizeof(*index2ifc) * nindex2ifc);
3648 if (index2ifc == NULL) {
3652 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3655 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3658 p = (struct ifc **)realloc(index2ifc,
3659 sizeof(*index2ifc) * nsize);
3664 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3668 index2ifc[idx] = ifcp;