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))
95 struct ifc { /* Configuration of an interface */
96 TAILQ_ENTRY(ifc) ifc_next;
98 char ifc_name[IFNAMSIZ]; /* if name */
99 int ifc_index; /* if index */
100 int ifc_mtu; /* if mtu */
101 int ifc_metric; /* if metric */
102 u_int ifc_flags; /* flags */
103 short ifc_cflags; /* IFC_XXX */
104 struct in6_addr ifc_mylladdr; /* my link-local address */
105 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
106 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
107 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
108 int ifc_joined; /* joined to ff02::9 */
110 TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
112 struct ifac { /* Adddress associated to an interface */
113 TAILQ_ENTRY(ifac) ifac_next;
115 struct ifc *ifac_ifc; /* back pointer */
116 struct in6_addr ifac_addr; /* address */
117 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
118 int ifac_scope_id; /* scope id */
119 int ifac_plen; /* prefix length */
122 struct iff { /* Filters for an interface */
123 TAILQ_ENTRY(iff) iff_next;
126 struct in6_addr iff_addr;
130 struct ifc **index2ifc;
131 unsigned int nindex2ifc;
132 struct ifc *loopifcp = NULL; /* pointing to loopback */
134 struct pollfd set[2];
136 fd_set *sockvecp; /* vector to select() for receiving */
139 int maxfd; /* maximum fd for select() */
141 int rtsock; /* the routing socket */
142 int ripsock; /* socket to send/receive RIP datagram */
144 struct rip6 *ripbuf; /* packet buffer for sending */
147 * Maintain the routes in a linked list. When the number of the routes
148 * grows, somebody would like to introduce a hash based or a radix tree
149 * based structure. I believe the number of routes handled by RIP is
150 * limited and I don't have to manage a complex data structure, however.
152 * One of the major drawbacks of the linear linked list is the difficulty
153 * of representing the relationship between a couple of routes. This may
154 * be a significant problem when we have to support route aggregation with
155 * suppressing the specifics covered by the aggregate.
159 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
161 struct riprt *rrt_same; /* same destination - future use */
162 struct netinfo6 rrt_info; /* network info */
163 struct in6_addr rrt_gw; /* gateway */
164 u_long rrt_flags; /* kernel routing table flags */
165 u_long rrt_rflags; /* route6d routing table flags */
166 time_t rrt_t; /* when the route validated */
167 int rrt_index; /* ifindex from which this route got */
169 TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
171 int dflag = 0; /* debug flag */
172 int qflag = 0; /* quiet flag */
173 int nflag = 0; /* don't update kernel routing table */
174 int aflag = 0; /* age out even the statically defined routes */
175 int hflag = 0; /* don't split horizon */
176 int lflag = 0; /* exchange site local routes */
177 int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
178 int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
179 int sflag = 0; /* announce static routes w/ split horizon */
180 int Sflag = 0; /* announce static routes to every interface */
181 unsigned long routetag = 0; /* route tag attached on originating case */
183 char *filter[MAXFILTER];
184 int filtertype[MAXFILTER];
189 struct sockaddr_storage ripsin;
192 time_t nextalarm = 0;
193 time_t sup_trig_update = 0;
201 volatile sig_atomic_t seenalrm;
202 volatile sig_atomic_t seenquit;
203 volatile sig_atomic_t seenusr1;
205 #define RRTF_AGGREGATE 0x08000000
206 #define RRTF_NOADVERTISE 0x10000000
207 #define RRTF_NH_NOT_LLADDR 0x20000000
208 #define RRTF_SENDANYWAY 0x40000000
209 #define RRTF_CHANGED 0x80000000
211 int main(int, char **);
212 void sighandler(int);
215 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
216 int out_filter(struct riprt *, struct ifc *);
218 void sockopt(struct ifc *);
220 int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
222 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
223 const struct sockaddr_in6 *);
224 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
225 const struct sockaddr_in6 *);
226 void filterconfig(void);
228 const char *rttypes(struct rt_msghdr *);
229 const char *rtflags(struct rt_msghdr *);
230 const char *ifflags(int);
231 int ifrt(struct ifc *, int);
232 void ifrt_p2p(struct ifc *, int);
233 void applymask(struct in6_addr *, struct in6_addr *);
234 void applyplen(struct in6_addr *, int);
237 void ifdump0(FILE *, const struct ifc *);
240 void rt_entry(struct rt_msghdr *, int);
242 void riprequest(struct ifc *, struct netinfo6 *, int,
243 struct sockaddr_in6 *);
244 void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
245 void sendrequest(struct ifc *);
246 int sin6mask2len(const struct sockaddr_in6 *);
247 int mask2len(const struct in6_addr *, int);
248 int sendpacket(struct sockaddr_in6 *, int);
249 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
250 int delroute(struct netinfo6 *, struct in6_addr *);
251 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
253 int tobeadv(struct riprt *, struct ifc *);
254 char *allocopy(char *);
256 const char *inet6_n2p(const struct in6_addr *);
257 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
258 struct in6_addr *plen2mask(int);
259 struct riprt *rtsearch(struct netinfo6 *);
260 int ripinterval(int);
261 time_t ripsuptrig(void);
262 void fatal(const char *, ...)
263 __attribute__((__format__(__printf__, 1, 2)));
264 void trace(int, const char *, ...)
265 __attribute__((__format__(__printf__, 2, 3)));
266 void tracet(int, const char *, ...)
267 __attribute__((__format__(__printf__, 2, 3)));
268 unsigned int if_maxindex(void);
269 struct ifc *ifc_find(char *);
270 struct iff *iff_find(struct ifc *, int);
271 void setindex2ifc(int, struct ifc *);
273 #define MALLOC(type) ((type *)malloc(sizeof(type)))
275 #define IFIL_TYPE_ANY 0x0
276 #define IFIL_TYPE_A 'A'
277 #define IFIL_TYPE_N 'N'
278 #define IFIL_TYPE_T 'T'
279 #define IFIL_TYPE_O 'O'
280 #define IFIL_TYPE_L 'L'
283 main(int argc, char *argv[])
289 sigset_t mask, omask;
290 const char *pidfile = ROUTE6D_PID;
295 progname = strrchr(*argv, '/');
302 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
309 if (nfilter >= MAXFILTER) {
310 fatal("Exceeds MAXFILTER");
313 filtertype[nfilter] = ch;
314 filter[nfilter++] = allocopy(optarg);
318 routetag = strtoul(optarg, &ep, 0);
319 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
320 fatal("invalid route tag");
329 proto = strtoul(optarg, &ep, 0);
330 if (!ep || *ep != '\0' || 3 < proto) {
331 fatal("invalid P flag");
345 proto = strtoul(optarg, &ep, 0);
346 if (!ep || *ep != '\0' || 3 < proto) {
347 fatal("invalid Q flag");
360 if ((rtlog = fopen(optarg, "w")) == NULL) {
361 fatal("Can not write to routelog");
365 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
366 FLAG('a', aflag, 1); break;
367 FLAG('d', dflag, 1); break;
368 FLAG('D', dflag, 2); break;
369 FLAG('h', hflag, 1); break;
370 FLAG('l', lflag, 1); break;
371 FLAG('n', nflag, 1); break;
372 FLAG('q', qflag, 1); break;
373 FLAG('s', sflag, 1); break;
374 FLAG('S', Sflag, 1); break;
377 fatal("Invalid option specified, terminating");
384 fatal("bogus extra arguments");
390 fprintf(stderr, "No kernel update is allowed\n");
394 if (daemon(0, 0) < 0) {
400 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
403 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
405 memset(ripbuf, 0, RIP6_MAXMTU);
406 ripbuf->rip6_cmd = RIP6_RESPONSE;
407 ripbuf->rip6_vers = RIP6_VERSION;
408 ripbuf->rip6_res1[0] = 0;
409 ripbuf->rip6_res1[1] = 0;
413 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
414 if (ifcp->ifc_index < 0) {
415 fprintf(stderr, "No ifindex found at %s "
416 "(no link-local address?)\n", ifcp->ifc_name);
422 if (loopifcp == NULL) {
423 fatal("No loopback found");
426 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
435 if ((pidfh = fopen(pidfile, "w")) != NULL) {
436 fprintf(pidfh, "%d\n", pid);
440 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
444 memset(ripbuf, 0, RIP6_MAXMTU);
445 ripbuf->rip6_cmd = RIP6_RESPONSE;
446 ripbuf->rip6_vers = RIP6_VERSION;
447 ripbuf->rip6_res1[0] = 0;
448 ripbuf->rip6_res1[1] = 0;
450 if (signal(SIGALRM, sighandler) == SIG_ERR ||
451 signal(SIGQUIT, sighandler) == SIG_ERR ||
452 signal(SIGTERM, sighandler) == SIG_ERR ||
453 signal(SIGUSR1, sighandler) == SIG_ERR ||
454 signal(SIGHUP, sighandler) == SIG_ERR ||
455 signal(SIGINT, sighandler) == SIG_ERR) {
460 * To avoid rip packet congestion (not on a cable but in this
461 * process), wait for a moment to send the first RIP6_RESPONSE
464 alarm(ripinterval(INIT_INTERVAL6));
466 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
467 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
469 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
473 syslog(LOG_INFO, "**** Started ****");
475 sigaddset(&mask, SIGALRM);
494 switch (poll(set, 2, INFTIM))
496 memcpy(recvecp, sockvecp, fdmasks);
497 switch (select(maxfd + 1, recvecp, 0, 0, 0))
501 if (errno != EINTR) {
510 if (set[0].revents & POLLIN)
512 if (FD_ISSET(ripsock, recvecp))
515 sigprocmask(SIG_BLOCK, &mask, &omask);
517 sigprocmask(SIG_SETMASK, &omask, NULL);
520 if (set[1].revents & POLLIN)
522 if (FD_ISSET(rtsock, recvecp))
525 sigprocmask(SIG_BLOCK, &mask, &omask);
527 sigprocmask(SIG_SETMASK, &omask, NULL);
534 sighandler(int signo)
554 * gracefully exits after resetting sockopts.
563 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
564 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
565 delroute(&rrt->rrt_info, &rrt->rrt_gw);
570 syslog(LOG_INFO, "**** Terminated ****");
576 * Called periodically:
577 * 1. age out the learned route. remove it if necessary.
578 * 2. submit RIP6_RESPONSE packets.
579 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
580 * to invoke this function in every 1 or 5 or 10 seconds only to age the
581 * routes more precisely.
588 struct riprt *rrt, *rrt_tmp;
589 time_t t_lifetime, t_holddown;
591 /* age the RIP routes */
592 t_lifetime = time(NULL) - RIP_LIFETIME;
593 t_holddown = t_lifetime - RIP_HOLDDOWN;
594 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
597 else if (rrt->rrt_t < t_holddown) {
598 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
599 delroute(&rrt->rrt_info, &rrt->rrt_gw);
601 } else if (rrt->rrt_t < t_lifetime)
602 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
605 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
606 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
607 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
609 alarm(ripinterval(SUPPLY_INTERVAL6));
616 const int int0 = 0, int1 = 1, int255 = 255;
617 struct addrinfo hints, *res;
618 char port[NI_MAXSERV];
620 TAILQ_INIT(&ifc_head);
621 nindex2ifc = 0; /*initial guess*/
623 snprintf(port, sizeof(port), "%u", RIP6_PORT);
625 memset(&hints, 0, sizeof(hints));
626 hints.ai_family = PF_INET6;
627 hints.ai_socktype = SOCK_DGRAM;
628 hints.ai_protocol = IPPROTO_UDP;
629 hints.ai_flags = AI_PASSIVE;
630 error = getaddrinfo(NULL, port, &hints, &res);
632 fatal("%s", gai_strerror(error));
636 fatal(":: resolved to multiple address");
640 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
646 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
647 &int1, sizeof(int1)) < 0) {
648 fatal("rip IPV6_V6ONLY");
652 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
656 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
657 &int255, sizeof(int255)) < 0) {
658 fatal("rip IPV6_MULTICAST_HOPS");
661 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
662 &int0, sizeof(int0)) < 0) {
663 fatal("rip IPV6_MULTICAST_LOOP");
667 #ifdef IPV6_RECVPKTINFO
668 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
669 &int1, sizeof(int1)) < 0) {
670 fatal("rip IPV6_RECVPKTINFO");
673 #else /* old adv. API */
674 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
675 &int1, sizeof(int1)) < 0) {
676 fatal("rip IPV6_PKTINFO");
681 #ifdef IPV6_RECVPKTINFO
682 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
683 &int1, sizeof(int1)) < 0) {
684 fatal("rip IPV6_RECVHOPLIMIT");
687 #else /* old adv. API */
688 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
689 &int1, sizeof(int1)) < 0) {
690 fatal("rip IPV6_HOPLIMIT");
695 memset(&hints, 0, sizeof(hints));
696 hints.ai_family = PF_INET6;
697 hints.ai_socktype = SOCK_DGRAM;
698 hints.ai_protocol = IPPROTO_UDP;
699 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
701 fatal("%s", gai_strerror(error));
705 fatal("%s resolved to multiple address", RIP6_DEST);
708 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
712 set[0].events = POLLIN;
718 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
719 fatal("route socket");
724 set[1].events = POLLIN;
733 rtsock = -1; /*just for safety */
738 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
739 if ((sockvecp = malloc(fdmasks)) == NULL) {
743 if ((recvecp = malloc(fdmasks)) == NULL) {
747 memset(sockvecp, 0, fdmasks);
748 FD_SET(ripsock, sockvecp);
750 FD_SET(rtsock, sockvecp);
755 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
758 * ripflush flushes the rip datagram stored in the rip buffer
761 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
767 tracet(1, "Send(%s): info(%d) to %s.%d\n",
769 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
771 tracet(1, "Send: info(%d) to %s.%d\n",
772 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
774 np = ripbuf->rip6_nets;
775 for (i = 0; i < nrt; i++, np++) {
776 if (np->rip6_metric == NEXTHOP_METRIC) {
777 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
778 trace(2, " NextHop reset");
780 trace(2, " NextHop %s",
781 inet6_n2p(&np->rip6_dest));
784 trace(2, " %s/%d[%d]",
785 inet6_n2p(&np->rip6_dest),
786 np->rip6_plen, np->rip6_metric);
789 trace(2, " tag=0x%04x",
790 ntohs(np->rip6_tag) & 0xffff);
795 error = sendpacket(sin6, RIPSIZE(nrt));
796 if (error == EAFNOSUPPORT) {
797 /* Protocol not supported */
798 tracet(1, "Could not send info to %s (%s): "
800 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
801 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
806 * Generate RIP6_RESPONSE packets and send them.
809 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
812 struct in6_addr *nh; /* next hop */
822 * Request from non-link local address is not
823 * a regular route6d update.
825 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
826 sizeof(struct udphdr) -
827 sizeof(struct rip6) + sizeof(struct netinfo6)) /
828 sizeof(struct netinfo6);
831 np = ripbuf->rip6_nets;
832 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
833 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
835 /* Put the route to the buffer */
839 ripflush(NULL, sin6, nrt, np);
842 np = ripbuf->rip6_nets;
845 if (nrt) /* Send last packet */
846 ripflush(NULL, sin6, nrt, np);
850 if ((flag & RRTF_SENDANYWAY) == 0 &&
851 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
855 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
858 /* -T: generate default route only */
859 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
860 struct netinfo6 rrt_info;
861 memset(&rrt_info, 0, sizeof(struct netinfo6));
862 rrt_info.rip6_dest = in6addr_any;
863 rrt_info.rip6_plen = 0;
864 rrt_info.rip6_metric = 1;
865 rrt_info.rip6_metric += ifcp->ifc_metric;
866 rrt_info.rip6_tag = htons(routetag & 0xffff);
867 np = ripbuf->rip6_nets;
870 ripflush(ifcp, sin6, nrt, np);
874 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
875 sizeof(struct udphdr) -
876 sizeof(struct rip6) + sizeof(struct netinfo6)) /
877 sizeof(struct netinfo6);
879 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
880 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
881 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
884 /* Need to check filter here */
885 if (out_filter(rrt, ifcp) == 0)
888 /* Check split horizon and other conditions */
889 if (tobeadv(rrt, ifcp) == 0)
892 /* Only considers the routes with flag if specified */
893 if ((flag & RRTF_CHANGED) &&
894 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
898 if (rrt->rrt_index == ifcp->ifc_index &&
899 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
900 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
901 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
902 if (nrt == maxrte - 2) {
903 ripflush(ifcp, sin6, nrt, np);
906 np = ripbuf->rip6_nets;
909 np->rip6_dest = rrt->rrt_gw;
912 np->rip6_metric = NEXTHOP_METRIC;
916 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
917 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
918 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
920 if (nrt == maxrte - 2) {
921 ripflush(ifcp, sin6, nrt, np);
924 np = ripbuf->rip6_nets;
926 memset(np, 0, sizeof(struct netinfo6));
927 np->rip6_metric = NEXTHOP_METRIC;
932 /* Put the route to the buffer */
936 ripflush(ifcp, sin6, nrt, np);
939 np = ripbuf->rip6_nets;
942 if (nrt) /* Send last packet */
943 ripflush(ifcp, sin6, nrt, np);
947 * outbound filter logic, per-route/interface.
950 out_filter(struct riprt *rrt, struct ifc *ifcp)
957 * -A: filter out less specific routes, if we have aggregated
960 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
961 if (iffp->iff_type != 'A')
963 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
965 ia = rrt->rrt_info.rip6_dest;
966 applyplen(&ia, iffp->iff_plen);
967 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
972 * if it is an aggregated route, advertise it only to the
973 * interfaces specified on -A.
975 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
977 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
978 if (iffp->iff_type != 'A')
980 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
981 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
992 * -O: advertise only if prefix matches the configured prefix.
994 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
996 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
997 if (iffp->iff_type != 'O')
999 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1001 ia = rrt->rrt_info.rip6_dest;
1002 applyplen(&ia, iffp->iff_plen);
1003 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1012 /* the prefix should be advertised */
1017 * Determine if the route is to be advertised on the specified interface.
1018 * It checks options specified in the arguments and the split horizon rule.
1021 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1024 /* Special care for static routes */
1025 if (rrt->rrt_flags & RTF_STATIC) {
1026 /* XXX don't advertise reject/blackhole routes */
1027 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1030 if (Sflag) /* Yes, advertise it anyway */
1032 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1036 /* Regular split horizon */
1037 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1043 * Send a rip packet actually.
1046 sendpacket(struct sockaddr_in6 *sin6, int len)
1050 struct iovec iov[2];
1051 u_char cmsgbuf[256];
1052 struct in6_pktinfo *pi;
1054 struct sockaddr_in6 sincopy;
1056 /* do not overwrite the given sin */
1060 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1061 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1062 idx = sin6->sin6_scope_id;
1066 m.msg_name = (caddr_t)sin6;
1067 m.msg_namelen = sizeof(*sin6);
1068 iov[0].iov_base = (caddr_t)ripbuf;
1069 iov[0].iov_len = len;
1073 m.msg_control = NULL;
1074 m.msg_controllen = 0;
1076 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1077 cm = (struct cmsghdr *)cmsgbuf;
1078 m.msg_control = (caddr_t)cm;
1079 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1081 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1082 cm->cmsg_level = IPPROTO_IPV6;
1083 cm->cmsg_type = IPV6_PKTINFO;
1084 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1085 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1086 pi->ipi6_ifindex = idx;
1089 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1090 trace(1, "sendmsg: %s\n", strerror(errno));
1098 * Receive and process RIP packets. Update the routes/kernel forwarding
1099 * table if necessary.
1104 struct ifc *ifcp, *ic;
1105 struct sockaddr_in6 fsock;
1106 struct in6_addr nh; /* next hop */
1108 struct netinfo6 *np, *nq;
1111 unsigned int need_trigger, idx;
1112 char buf[4 * RIP6_MAXMTU];
1116 struct iovec iov[2];
1117 u_char cmsgbuf[256];
1118 struct in6_pktinfo *pi = NULL;
1123 time_t t_half_lifetime;
1127 m.msg_name = (caddr_t)&fsock;
1128 m.msg_namelen = sizeof(fsock);
1129 iov[0].iov_base = (caddr_t)buf;
1130 iov[0].iov_len = sizeof(buf);
1133 cm = (struct cmsghdr *)cmsgbuf;
1134 m.msg_control = (caddr_t)cm;
1135 m.msg_controllen = sizeof(cmsgbuf);
1136 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1141 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1143 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1144 if (cm->cmsg_level != IPPROTO_IPV6)
1146 switch (cm->cmsg_type) {
1148 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1150 "invalid cmsg length for IPV6_PKTINFO\n");
1153 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1154 idx = pi->ipi6_ifindex;
1157 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1159 "invalid cmsg length for IPV6_HOPLIMIT\n");
1162 hlimp = (int *)CMSG_DATA(cm);
1167 if ((size_t)len < sizeof(struct rip6)) {
1168 trace(1, "Packet too short\n");
1172 if (pi == NULL || hlimp == NULL) {
1174 * This can happen when the kernel failed to allocate memory
1175 * for the ancillary data. Although we might be able to handle
1176 * some cases without this info, those are minor and not so
1177 * important, so it's better to discard the packet for safer
1180 trace(1, "IPv6 packet information cannot be retrieved\n");
1184 nh = fsock.sin6_addr;
1185 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1186 sizeof(struct netinfo6);
1187 rp = (struct rip6 *)buf;
1190 if (rp->rip6_vers != RIP6_VERSION) {
1191 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1194 if (rp->rip6_cmd == RIP6_REQUEST) {
1195 if (idx && idx < nindex2ifc) {
1196 ifcp = index2ifc[idx];
1197 riprequest(ifcp, np, nn, &fsock);
1199 riprequest(NULL, np, nn, &fsock);
1204 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1205 trace(1, "Response from non-ll addr: %s\n",
1206 inet6_n2p(&fsock.sin6_addr));
1207 return; /* Ignore packets from non-link-local addr */
1209 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1210 trace(1, "Response from non-rip port from %s\n",
1211 inet6_n2p(&fsock.sin6_addr));
1214 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1216 "Response packet with a smaller hop limit (%d) from %s\n",
1217 *hlimp, inet6_n2p(&fsock.sin6_addr));
1221 * Further validation: since this program does not send off-link
1222 * requests, an incoming response must always come from an on-link
1223 * node. Although this is normally ensured by the source address
1224 * check above, it may not 100% be safe because there are router
1225 * implementations that (invalidly) allow a packet with a link-local
1226 * source address to be forwarded to a different link.
1227 * So we also check whether the destination address is a link-local
1228 * address or the hop limit is 255. Note that RFC2080 does not require
1229 * the specific hop limit for a unicast response, so we cannot assume
1232 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1234 "Response packet possibly from an off-link node: "
1235 "from %s to %s hlim=%d\n",
1236 inet6_n2p(&fsock.sin6_addr),
1237 inet6_n2p(&pi->ipi6_addr), *hlimp);
1241 idx = fsock.sin6_scope_id;
1242 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1244 trace(1, "Packets to unknown interface index %d\n", idx);
1245 return; /* Ignore it */
1247 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1248 return; /* The packet is from me; ignore */
1249 if (rp->rip6_cmd != RIP6_RESPONSE) {
1250 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1255 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1258 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1259 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1262 t_half_lifetime = t - (RIP_LIFETIME/2);
1263 for (; nn; nn--, np++) {
1264 if (np->rip6_metric == NEXTHOP_METRIC) {
1265 /* modify neighbor address */
1266 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1268 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1269 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1270 nh = fsock.sin6_addr;
1271 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1273 nh = fsock.sin6_addr;
1274 trace(1, "\tInvalid Nexthop: %s\n",
1275 inet6_n2p(&np->rip6_dest));
1279 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1280 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1281 inet6_n2p(&np->rip6_dest),
1282 np->rip6_plen, np->rip6_metric);
1285 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1286 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1287 inet6_n2p(&np->rip6_dest),
1288 np->rip6_plen, np->rip6_metric);
1291 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1292 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1293 inet6_n2p(&np->rip6_dest),
1294 np->rip6_plen, np->rip6_metric);
1297 /* may need to pass sitelocal prefix in some case, however*/
1298 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1299 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1300 inet6_n2p(&np->rip6_dest),
1301 np->rip6_plen, np->rip6_metric);
1304 trace(2, "\tnetinfo6: %s/%d [%d]",
1305 inet6_n2p(&np->rip6_dest),
1306 np->rip6_plen, np->rip6_metric);
1308 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1311 applyplen(&ia, np->rip6_plen);
1312 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1313 trace(2, " [junk outside prefix]");
1317 * -L: listen only if the prefix matches the configuration
1319 ok = 1; /* if there's no L filter, it is ok */
1320 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1321 if (iffp->iff_type != IFIL_TYPE_L)
1324 if (np->rip6_plen < iffp->iff_plen)
1326 /* special rule: ::/0 means default, not "in /0" */
1327 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1330 applyplen(&ia, iffp->iff_plen);
1331 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1337 trace(2, " (filtered)\n");
1343 np->rip6_metric += ifcp->ifc_metric;
1344 if (np->rip6_metric > HOPCNT_INFINITY6)
1345 np->rip6_metric = HOPCNT_INFINITY6;
1347 applyplen(&np->rip6_dest, np->rip6_plen);
1348 if ((rrt = rtsearch(np)) != NULL) {
1349 if (rrt->rrt_t == 0)
1350 continue; /* Intf route has priority */
1351 nq = &rrt->rrt_info;
1352 if (nq->rip6_metric > np->rip6_metric) {
1353 if (rrt->rrt_index == ifcp->ifc_index &&
1354 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1355 /* Small metric from the same gateway */
1356 nq->rip6_metric = np->rip6_metric;
1358 /* Better route found */
1359 rrt->rrt_index = ifcp->ifc_index;
1360 /* Update routing table */
1361 delroute(nq, &rrt->rrt_gw);
1364 addroute(rrt, &nh, ifcp);
1366 rrt->rrt_rflags |= RRTF_CHANGED;
1369 } else if (nq->rip6_metric < np->rip6_metric &&
1370 rrt->rrt_index == ifcp->ifc_index &&
1371 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1372 /* Got worse route from same gw */
1373 nq->rip6_metric = np->rip6_metric;
1375 rrt->rrt_rflags |= RRTF_CHANGED;
1377 } else if (nq->rip6_metric == np->rip6_metric &&
1378 np->rip6_metric < HOPCNT_INFINITY6) {
1379 if (rrt->rrt_index == ifcp->ifc_index &&
1380 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1381 /* same metric, same route from same gw */
1383 } else if (rrt->rrt_t < t_half_lifetime) {
1384 /* Better route found */
1385 rrt->rrt_index = ifcp->ifc_index;
1386 /* Update routing table */
1387 delroute(nq, &rrt->rrt_gw);
1390 addroute(rrt, &nh, ifcp);
1391 rrt->rrt_rflags |= RRTF_CHANGED;
1396 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1397 * do not update age value. Do nothing.
1399 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1400 /* Got a new valid route */
1401 if ((rrt = MALLOC(struct riprt)) == NULL) {
1402 fatal("malloc: struct riprt");
1405 memset(rrt, 0, sizeof(*rrt));
1406 nq = &rrt->rrt_info;
1408 rrt->rrt_same = NULL;
1409 rrt->rrt_index = ifcp->ifc_index;
1410 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1413 applyplen(&nq->rip6_dest, nq->rip6_plen);
1414 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1415 rrt->rrt_flags |= RTF_HOST;
1417 /* Update routing table */
1418 addroute(rrt, &nh, ifcp);
1419 rrt->rrt_rflags |= RRTF_CHANGED;
1423 /* Put the route to the list */
1424 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1427 /* XXX need to care the interval between triggered updates */
1429 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1430 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1431 if (ifcp->ifc_index == ic->ifc_index)
1433 if (ic->ifc_flags & IFF_UP)
1434 ripsend(ic, &ic->ifc_ripsin,
1438 /* Reset the flag */
1439 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1440 rrt->rrt_rflags &= ~RRTF_CHANGED;
1446 * Send all routes request packet to the specified interface.
1449 sendrequest(struct ifc *ifcp)
1451 struct netinfo6 *np;
1454 if (ifcp->ifc_flags & IFF_LOOPBACK)
1456 ripbuf->rip6_cmd = RIP6_REQUEST;
1457 np = ripbuf->rip6_nets;
1458 memset(np, 0, sizeof(struct netinfo6));
1459 np->rip6_metric = HOPCNT_INFINITY6;
1460 tracet(1, "Send rtdump Request to %s (%s)\n",
1461 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1462 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1463 if (error == EAFNOSUPPORT) {
1464 /* Protocol not supported */
1465 tracet(1, "Could not send rtdump Request to %s (%s): "
1466 "set IFF_UP to 0\n",
1467 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1468 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1470 ripbuf->rip6_cmd = RIP6_RESPONSE;
1474 * Process a RIP6_REQUEST packet.
1477 riprequest(struct ifc *ifcp,
1478 struct netinfo6 *np,
1480 struct sockaddr_in6 *sin6)
1485 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1486 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1487 /* Specific response, don't split-horizon */
1488 trace(1, "\tRIP Request\n");
1489 for (i = 0; i < nn; i++, np++) {
1492 np->rip6_metric = rrt->rrt_info.rip6_metric;
1494 np->rip6_metric = HOPCNT_INFINITY6;
1496 (void)sendpacket(sin6, RIPSIZE(nn));
1499 /* Whole routing table dump */
1500 trace(1, "\tRIP Request -- whole routing table\n");
1501 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1505 * Get information of each interface.
1510 struct ifaddrs *ifap, *ifa;
1512 struct ipv6_mreq mreq;
1515 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1520 if (getifaddrs(&ifap) != 0) {
1521 fatal("getifaddrs");
1525 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1526 if (ifa->ifa_addr->sa_family != AF_INET6)
1528 ifcp = ifc_find(ifa->ifa_name);
1529 /* we are interested in multicast-capable interfaces */
1530 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1534 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1535 fatal("malloc: struct ifc");
1538 memset(ifcp, 0, sizeof(*ifcp));
1540 ifcp->ifc_index = -1;
1541 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1542 sizeof(ifcp->ifc_name));
1543 TAILQ_INIT(&ifcp->ifc_ifac_head);
1544 TAILQ_INIT(&ifcp->ifc_iff_head);
1545 ifcp->ifc_flags = ifa->ifa_flags;
1546 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1547 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1548 ifflags(ifcp->ifc_flags));
1549 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1552 /* update flag, this may be up again */
1553 if (ifcp->ifc_flags != ifa->ifa_flags) {
1554 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1555 ifflags(ifcp->ifc_flags));
1556 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1557 ifcp->ifc_cflags |= IFC_CHANGED;
1559 ifcp->ifc_flags = ifa->ifa_flags;
1561 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1562 /* maybe temporary failure */
1565 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1566 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1567 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1568 mreq.ipv6mr_interface = ifcp->ifc_index;
1569 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1570 &mreq, sizeof(mreq)) < 0) {
1571 fatal("IPV6_JOIN_GROUP");
1574 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1583 ifconfig1(const char *name,
1584 const struct sockaddr *sa,
1588 struct in6_ifreq ifr;
1589 const struct sockaddr_in6 *sin6;
1594 sin6 = (const struct sockaddr_in6 *)sa;
1595 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1597 ifr.ifr_addr = *sin6;
1598 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1599 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1600 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1603 plen = sin6mask2len(&ifr.ifr_addr);
1604 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1605 /* same interface found */
1606 /* need check if something changed */
1607 /* XXX not yet implemented */
1611 * New address is found
1613 if ((ifac = MALLOC(struct ifac)) == NULL) {
1614 fatal("malloc: struct ifac");
1617 memset(ifac, 0, sizeof(*ifac));
1619 ifac->ifac_ifc = ifcp;
1620 ifac->ifac_addr = sin6->sin6_addr;
1621 ifac->ifac_plen = plen;
1622 ifac->ifac_scope_id = sin6->sin6_scope_id;
1623 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1624 ifr.ifr_addr = *sin6;
1625 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1626 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1629 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1630 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1632 trace(1, "found address %s/%d -- %s\n",
1633 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1635 trace(1, "found address %s/%d\n",
1636 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1638 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1639 ifcp->ifc_mylladdr = ifac->ifac_addr;
1640 ifcp->ifc_index = ifac->ifac_scope_id;
1641 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1642 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1643 setindex2ifc(ifcp->ifc_index, ifcp);
1644 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1645 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1646 ifcp->ifc_mtu = RIP6_MAXMTU;
1647 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1648 fatal("ioctl: SIOCGIFMETRIC");
1651 ifcp->ifc_metric = ifr.ifr_metric;
1652 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1653 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1655 ifcp->ifc_cflags |= IFC_CHANGED;
1657 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1663 ifremove(int ifindex)
1668 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1669 if (ifcp->ifc_index == ifindex)
1675 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1676 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1678 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1679 if (rrt->rrt_index == ifcp->ifc_index &&
1680 rrt->rrt_rflags & RRTF_AGGREGATE)
1681 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1687 * Receive and process routing messages.
1688 * Update interface information as necesssary.
1695 struct rt_msghdr *rtm;
1696 struct ifa_msghdr *ifam;
1697 struct if_msghdr *ifm;
1698 struct if_announcemsghdr *ifan;
1700 struct ifc *ifcp, *ic;
1701 int iface = 0, rtable = 0;
1702 struct sockaddr_in6 *rta[RTAX_MAX];
1703 struct sockaddr_in6 mask;
1707 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1708 perror("read from rtsock");
1714 if (len < sizeof(*rtm)) {
1715 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1716 len, (u_long)sizeof(*rtm));
1721 fprintf(stderr, "rtmsg:\n");
1722 for (i = 0; i < len; i++) {
1723 fprintf(stderr, "%02x ", buf[i] & 0xff);
1724 if (i % 16 == 15) fprintf(stderr, "\n");
1726 fprintf(stderr, "\n");
1729 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1730 if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1733 /* safety against bogus message */
1734 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1735 trace(1, "bogus rtmsg: length=%d\n",
1736 ((struct rt_msghdr *)p)->rtm_msglen);
1742 switch (((struct rt_msghdr *)p)->rtm_type) {
1745 ifam = (struct ifa_msghdr *)p;
1746 addrs = ifam->ifam_addrs;
1747 q = (char *)(ifam + 1);
1750 ifm = (struct if_msghdr *)p;
1751 addrs = ifm->ifm_addrs;
1752 q = (char *)(ifm + 1);
1754 case RTM_IFANNOUNCE:
1755 ifan = (struct if_announcemsghdr *)p;
1756 switch (ifan->ifan_what) {
1760 case IFAN_DEPARTURE:
1761 ifremove(ifan->ifan_index);
1767 rtm = (struct rt_msghdr *)p;
1768 addrs = rtm->rtm_addrs;
1769 q = (char *)(rtm + 1);
1770 if (rtm->rtm_version != RTM_VERSION) {
1771 trace(1, "unexpected rtmsg version %d "
1773 rtm->rtm_version, RTM_VERSION);
1776 if (rtm->rtm_pid == pid) {
1778 trace(1, "rtmsg looped back to me, ignored\n");
1784 memset(&rta, 0, sizeof(rta));
1785 for (i = 0; i < RTAX_MAX; i++) {
1786 if (addrs & (1 << i)) {
1787 rta[i] = (struct sockaddr_in6 *)q;
1788 q += ROUNDUP(rta[i]->sin6_len);
1792 trace(1, "rtsock: %s (addrs=%x)\n",
1793 rttypes((struct rt_msghdr *)p), addrs);
1796 i < ((struct rt_msghdr *)p)->rtm_msglen;
1798 fprintf(stderr, "%02x ", p[i] & 0xff);
1799 if (i % 16 == 15) fprintf(stderr, "\n");
1801 fprintf(stderr, "\n");
1807 * We may be able to optimize by using ifm->ifm_index or
1808 * ifam->ifam_index. For simplicity we don't do that here.
1810 switch (((struct rt_msghdr *)p)->rtm_type) {
1822 /* nothing to be done here */
1823 trace(1, "\tnothing to be done, ignored\n");
1828 if (rta[RTAX_DST] == NULL) {
1829 trace(1, "\tno destination, ignored\n");
1832 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1833 trace(1, "\taf mismatch, ignored\n");
1836 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1837 trace(1, "\tlinklocal destination, ignored\n");
1840 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1841 trace(1, "\tloopback destination, ignored\n");
1842 continue; /* Loopback */
1844 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1845 trace(1, "\tmulticast destination, ignored\n");
1851 switch (((struct rt_msghdr *)p)->rtm_type) {
1859 /* should already be handled */
1860 fatal("rtrecv: never reach here");
1863 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1864 trace(1, "\tsome of dst/gw/netamsk are "
1865 "unavailable, ignored\n");
1868 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1869 mask.sin6_len = sizeof(mask);
1870 memset(&mask.sin6_addr, 0xff,
1871 sizeof(mask.sin6_addr));
1872 rta[RTAX_NETMASK] = &mask;
1873 } else if (!rta[RTAX_NETMASK]) {
1874 trace(1, "\tsome of dst/gw/netamsk are "
1875 "unavailable, ignored\n");
1878 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1879 rta[RTAX_NETMASK]) == 0) {
1880 rtable++; /*just to be sure*/
1885 trace(1, "\tnot supported yet, ignored\n");
1888 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1889 trace(1, "\tno netmask or ifa given, ignored\n");
1892 if (ifam->ifam_index < nindex2ifc)
1893 ifcp = index2ifc[ifam->ifam_index];
1897 trace(1, "\tinvalid ifam_index %d, ignored\n",
1901 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1906 trace(1, "\tnot supported yet, ignored\n");
1913 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1915 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1916 if (ifcp->ifc_cflags & IFC_CHANGED) {
1917 if (ifrt(ifcp, 1)) {
1918 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1919 if (ifcp->ifc_index == ic->ifc_index)
1921 if (ic->ifc_flags & IFF_UP)
1922 ripsend(ic, &ic->ifc_ripsin,
1925 /* Reset the flag */
1926 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1927 rrt->rrt_rflags &= ~RRTF_CHANGED;
1930 ifcp->ifc_cflags &= ~IFC_CHANGED;
1935 trace(1, "rtsock: read routing table again\n");
1941 * remove specified route from the internal routing table.
1944 rt_del(const struct sockaddr_in6 *sdst,
1945 const struct sockaddr_in6 *sgw,
1946 const struct sockaddr_in6 *smask)
1948 const struct in6_addr *dst = NULL;
1949 const struct in6_addr *gw = NULL;
1951 struct netinfo6 ni6;
1952 struct riprt *rrt = NULL;
1955 if (sdst->sin6_family != AF_INET6) {
1956 trace(1, "\tother AF, ignored\n");
1959 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1960 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1961 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1962 trace(1, "\taddress %s not interesting, ignored\n",
1963 inet6_n2p(&sdst->sin6_addr));
1966 dst = &sdst->sin6_addr;
1967 if (sgw->sin6_family == AF_INET6) {
1969 gw = &sgw->sin6_addr;
1970 prefix = sin6mask2len(smask);
1971 } else if (sgw->sin6_family == AF_LINK) {
1973 * Interface route... a hard case. We need to get the prefix
1974 * length from the kernel, but we now are parsing rtmsg.
1975 * We'll purge matching routes from my list, then get the
1978 struct riprt *longest;
1979 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1982 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1983 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1985 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1987 || longest->rrt_info.rip6_plen <
1988 rrt->rrt_info.rip6_plen) {
1995 trace(1, "\tno matching interface route found\n");
1998 gw = &in6addr_loopback;
1999 prefix = rrt->rrt_info.rip6_plen;
2001 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2005 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2006 trace(1, "gw %s\n", inet6_n2p(gw));
2007 t_lifetime = time(NULL) - RIP_LIFETIME;
2008 /* age route for interface address */
2009 memset(&ni6, 0, sizeof(ni6));
2010 ni6.rip6_dest = *dst;
2011 ni6.rip6_plen = prefix;
2012 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2013 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2015 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2016 trace(1, "\tno route found\n");
2020 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2021 trace(1, "\tyou can delete static routes only\n");
2024 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2025 trace(1, "\tgw mismatch: %s <-> ",
2026 inet6_n2p(&rrt->rrt_gw));
2027 trace(1, "%s\n", inet6_n2p(gw));
2029 trace(1, "\troute found, age it\n");
2030 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2031 rrt->rrt_t = t_lifetime;
2032 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2039 * remove specified address from internal interface/routing table.
2042 rt_deladdr(struct ifc *ifcp,
2043 const struct sockaddr_in6 *sifa,
2044 const struct sockaddr_in6 *smask)
2046 const struct in6_addr *addr = NULL;
2048 struct ifac *ifac = NULL;
2049 struct netinfo6 ni6;
2050 struct riprt *rrt = NULL;
2054 if (sifa->sin6_family != AF_INET6) {
2055 trace(1, "\tother AF, ignored\n");
2058 addr = &sifa->sin6_addr;
2059 prefix = sin6mask2len(smask);
2061 trace(1, "\tdeleting %s/%d from %s\n",
2062 inet6_n2p(addr), prefix, ifcp->ifc_name);
2063 ifac = ifa_match(ifcp, addr, prefix);
2065 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2066 inet6_n2p(addr), prefix, ifcp->ifc_name);
2069 if (ifac->ifac_ifc != ifcp) {
2070 trace(1, "\taddress table corrupt: back pointer does not match "
2072 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2075 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2076 t_lifetime = time(NULL) - RIP_LIFETIME;
2077 /* age route for interface address */
2078 memset(&ni6, 0, sizeof(ni6));
2079 ni6.rip6_dest = ifac->ifac_addr;
2080 ni6.rip6_plen = ifac->ifac_plen;
2081 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2082 trace(1, "\tfind interface route %s/%d on %d\n",
2083 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2084 if ((rrt = rtsearch(&ni6)) != NULL) {
2085 struct in6_addr none;
2086 memset(&none, 0, sizeof(none));
2087 if (rrt->rrt_index == ifcp->ifc_index &&
2088 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2089 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2090 trace(1, "\troute found, age it\n");
2091 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2092 rrt->rrt_t = t_lifetime;
2093 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2097 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2098 inet6_n2p(&rrt->rrt_info.rip6_dest),
2099 rrt->rrt_info.rip6_plen,
2103 trace(1, "\tno interface route found\n");
2104 /* age route for p2p destination */
2105 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2106 memset(&ni6, 0, sizeof(ni6));
2107 ni6.rip6_dest = ifac->ifac_raddr;
2108 ni6.rip6_plen = 128;
2109 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2110 trace(1, "\tfind p2p route %s/%d on %d\n",
2111 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2113 if ((rrt = rtsearch(&ni6)) != NULL) {
2114 if (rrt->rrt_index == ifcp->ifc_index &&
2115 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2116 &ifac->ifac_addr)) {
2117 trace(1, "\troute found, age it\n");
2118 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2119 rrt->rrt_t = t_lifetime;
2120 rrt->rrt_info.rip6_metric =
2125 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2126 inet6_n2p(&rrt->rrt_info.rip6_dest),
2127 rrt->rrt_info.rip6_plen,
2131 trace(1, "\tno p2p route found\n");
2135 return ((updated) ? 0 : -1);
2139 * Get each interface address and put those interface routes to the route
2143 ifrt(struct ifc *ifcp, int again)
2146 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2147 struct netinfo6 *np;
2149 int need_trigger = 0;
2152 if (ifcp->ifc_flags & IFF_LOOPBACK)
2153 return 0; /* ignore loopback */
2156 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2157 ifrt_p2p(ifcp, again);
2161 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2162 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2164 trace(1, "route: %s on %s: "
2165 "skip linklocal interface address\n",
2166 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2170 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2172 trace(1, "route: %s: skip unspec interface address\n",
2177 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2179 trace(1, "route: %s: skip loopback address\n",
2184 if (ifcp->ifc_flags & IFF_UP) {
2185 if ((rrt = MALLOC(struct riprt)) == NULL)
2186 fatal("malloc: struct riprt");
2187 memset(rrt, 0, sizeof(*rrt));
2188 rrt->rrt_same = NULL;
2189 rrt->rrt_index = ifcp->ifc_index;
2190 rrt->rrt_t = 0; /* don't age */
2191 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2192 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2193 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2194 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2195 rrt->rrt_flags = RTF_HOST;
2196 rrt->rrt_rflags |= RRTF_CHANGED;
2197 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2198 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2199 rrt->rrt_gw = ifac->ifac_addr;
2200 np = &rrt->rrt_info;
2201 search_rrt = rtsearch(np);
2202 if (search_rrt != NULL) {
2203 if (search_rrt->rrt_info.rip6_metric <=
2204 rrt->rrt_info.rip6_metric) {
2205 /* Already have better route */
2207 trace(1, "route: %s/%d: "
2208 "already registered (%s)\n",
2209 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2215 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2216 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2218 /* Attach the route to the list */
2219 trace(1, "route: %s/%d: register route (%s)\n",
2220 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2222 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2223 addroute(rrt, &rrt->rrt_gw, ifcp);
2226 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2229 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2230 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2231 t_lifetime = time(NULL) - RIP_LIFETIME;
2232 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2233 loop_rrt->rrt_t = t_lifetime;
2234 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2235 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2245 return need_trigger;
2249 * there are couple of p2p interface routing models. "behavior" lets
2250 * you pick one. it looks that gated behavior fits best with BSDs,
2251 * since BSD kernels do not look at prefix length on p2p interfaces.
2254 ifrt_p2p(struct ifc *ifcp, int again)
2257 struct riprt *rrt, *orrt;
2258 struct netinfo6 *np;
2259 struct in6_addr addr, dest;
2260 int advert, ignore, i;
2261 #define P2PADVERT_NETWORK 1
2262 #define P2PADVERT_ADDR 2
2263 #define P2PADVERT_DEST 4
2264 #define P2PADVERT_MAX 4
2265 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2266 const char *category = "";
2269 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2270 addr = ifac->ifac_addr;
2271 dest = ifac->ifac_raddr;
2272 applyplen(&addr, ifac->ifac_plen);
2273 applyplen(&dest, ifac->ifac_plen);
2274 advert = ignore = 0;
2278 * honor addr/plen, just like normal shared medium
2279 * interface. this may cause trouble if you reuse
2280 * addr/plen on other interfaces.
2282 * advertise addr/plen.
2284 advert |= P2PADVERT_NETWORK;
2288 * prefixlen on p2p interface is meaningless.
2289 * advertise addr/128 and dest/128.
2291 * do not install network route to route6d routing
2292 * table (if we do, it would prevent route installation
2293 * for other p2p interface that shares addr/plen).
2295 * XXX what should we do if dest is ::? it will not
2296 * get announced anyways (see following filter),
2297 * but we need to think.
2299 advert |= P2PADVERT_ADDR;
2300 advert |= P2PADVERT_DEST;
2301 ignore |= P2PADVERT_NETWORK;
2305 * just for testing. actually the code is redundant
2306 * given the current p2p interface address assignment
2307 * rule for kame kernel.
2310 * A/n -> announce A/n
2311 * A B/n, A and B share prefix -> A/n (= B/n)
2312 * A B/n, do not share prefix -> A/128 and B/128
2313 * actually, A/64 and A B/128 are the only cases
2314 * permitted by the kernel:
2316 * A B/128 -> A/128 and B/128
2318 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2319 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2320 advert |= P2PADVERT_NETWORK;
2322 advert |= P2PADVERT_ADDR;
2323 advert |= P2PADVERT_DEST;
2324 ignore |= P2PADVERT_NETWORK;
2327 advert |= P2PADVERT_NETWORK;
2331 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2332 if ((ignore & i) != 0)
2334 if ((rrt = MALLOC(struct riprt)) == NULL) {
2335 fatal("malloc: struct riprt");
2338 memset(rrt, 0, sizeof(*rrt));
2339 rrt->rrt_same = NULL;
2340 rrt->rrt_index = ifcp->ifc_index;
2341 rrt->rrt_t = 0; /* don't age */
2343 case P2PADVERT_NETWORK:
2344 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2345 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2346 applyplen(&rrt->rrt_info.rip6_dest,
2348 category = "network";
2350 case P2PADVERT_ADDR:
2351 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2352 rrt->rrt_info.rip6_plen = 128;
2353 rrt->rrt_gw = in6addr_loopback;
2356 case P2PADVERT_DEST:
2357 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2358 rrt->rrt_info.rip6_plen = 128;
2359 rrt->rrt_gw = ifac->ifac_addr;
2363 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2364 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2366 trace(1, "route: %s: skip unspec/linklocal "
2367 "(%s on %s)\n", category, ifcp->ifc_name);
2372 if ((advert & i) == 0) {
2373 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2377 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2378 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2379 np = &rrt->rrt_info;
2380 orrt = rtsearch(np);
2382 /* Attach the route to the list */
2383 trace(1, "route: %s/%d: register route "
2385 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2386 category, ifcp->ifc_name, noadv);
2387 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2388 } else if (rrt->rrt_index != orrt->rrt_index ||
2389 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2391 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2392 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2395 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2396 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2397 category, ifcp->ifc_name, noadv);
2401 trace(1, "route: %s/%d: "
2402 "already registered (%s on %s%s)\n",
2403 inet6_n2p(&np->rip6_dest),
2404 np->rip6_plen, category,
2405 ifcp->ifc_name, noadv);
2411 #undef P2PADVERT_NETWORK
2412 #undef P2PADVERT_ADDR
2413 #undef P2PADVERT_DEST
2414 #undef P2PADVERT_MAX
2418 getifmtu(int ifindex)
2423 struct if_msghdr *ifm;
2430 mib[4] = NET_RT_IFLIST;
2432 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2433 fatal("sysctl estimate NET_RT_IFLIST");
2436 if ((buf = malloc(msize)) == NULL) {
2440 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2441 fatal("sysctl NET_RT_IFLIST");
2444 ifm = (struct if_msghdr *)buf;
2445 mtu = ifm->ifm_data.ifi_mtu;
2446 if (ifindex != ifm->ifm_index) {
2447 fatal("ifindex does not match with ifm_index");
2455 rttypes(struct rt_msghdr *rtm)
2457 #define RTTYPE(s, f) \
2459 if (rtm->rtm_type == (f)) \
2462 RTTYPE("ADD", RTM_ADD);
2463 RTTYPE("DELETE", RTM_DELETE);
2464 RTTYPE("CHANGE", RTM_CHANGE);
2465 RTTYPE("GET", RTM_GET);
2466 RTTYPE("LOSING", RTM_LOSING);
2467 RTTYPE("REDIRECT", RTM_REDIRECT);
2468 RTTYPE("MISS", RTM_MISS);
2469 RTTYPE("LOCK", RTM_LOCK);
2470 RTTYPE("OLDADD", RTM_OLDADD);
2471 RTTYPE("OLDDEL", RTM_OLDDEL);
2472 RTTYPE("NEWADDR", RTM_NEWADDR);
2473 RTTYPE("DELADDR", RTM_DELADDR);
2474 RTTYPE("IFINFO", RTM_IFINFO);
2476 RTTYPE("OLDADD", RTM_OLDADD);
2479 RTTYPE("OLDDEL", RTM_OLDDEL);
2482 RTTYPE("OIFINFO", RTM_OIFINFO);
2484 #ifdef RTM_IFANNOUNCE
2485 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2488 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2491 RTTYPE("DELMADDR", RTM_DELMADDR);
2498 rtflags(struct rt_msghdr *rtm)
2500 static char buf[BUFSIZ];
2503 * letter conflict should be okay. painful when *BSD diverges...
2505 strlcpy(buf, "", sizeof(buf));
2506 #define RTFLAG(s, f) \
2508 if (rtm->rtm_flags & (f)) \
2509 strlcat(buf, (s), sizeof(buf)); \
2511 RTFLAG("U", RTF_UP);
2512 RTFLAG("G", RTF_GATEWAY);
2513 RTFLAG("H", RTF_HOST);
2514 RTFLAG("R", RTF_REJECT);
2515 RTFLAG("D", RTF_DYNAMIC);
2516 RTFLAG("M", RTF_MODIFIED);
2517 RTFLAG("d", RTF_DONE);
2519 RTFLAG("m", RTF_MASK);
2522 RTFLAG("C", RTF_CLONING);
2525 RTFLAG("c", RTF_CLONED);
2527 #ifdef RTF_PRCLONING
2528 RTFLAG("c", RTF_PRCLONING);
2530 #ifdef RTF_WASCLONED
2531 RTFLAG("W", RTF_WASCLONED);
2533 RTFLAG("X", RTF_XRESOLVE);
2535 RTFLAG("L", RTF_LLINFO);
2537 RTFLAG("S", RTF_STATIC);
2538 RTFLAG("B", RTF_BLACKHOLE);
2540 RTFLAG("3", RTF_PROTO3);
2542 RTFLAG("2", RTF_PROTO2);
2543 RTFLAG("1", RTF_PROTO1);
2544 #ifdef RTF_BROADCAST
2545 RTFLAG("b", RTF_BROADCAST);
2548 RTFLAG("d", RTF_DEFAULT);
2550 #ifdef RTF_ISAROUTER
2551 RTFLAG("r", RTF_ISAROUTER);
2554 RTFLAG("T", RTF_TUNNEL);
2557 RTFLAG("A", RTF_AUTH);
2560 RTFLAG("E", RTF_CRYPT);
2569 static char buf[BUFSIZ];
2571 strlcpy(buf, "", sizeof(buf));
2572 #define IFFLAG(s, f) \
2574 if (flags & (f)) { \
2576 strlcat(buf, ",", sizeof(buf)); \
2577 strlcat(buf, (s), sizeof(buf)); \
2580 IFFLAG("UP", IFF_UP);
2581 IFFLAG("BROADCAST", IFF_BROADCAST);
2582 IFFLAG("DEBUG", IFF_DEBUG);
2583 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2584 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2585 #ifdef IFF_NOTRAILERS
2586 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2589 IFFLAG("SMART", IFF_SMART);
2591 IFFLAG("RUNNING", IFF_RUNNING);
2592 IFFLAG("NOARP", IFF_NOARP);
2593 IFFLAG("PROMISC", IFF_PROMISC);
2594 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2595 IFFLAG("OACTIVE", IFF_OACTIVE);
2596 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2597 IFFLAG("LINK0", IFF_LINK0);
2598 IFFLAG("LINK1", IFF_LINK1);
2599 IFFLAG("LINK2", IFF_LINK2);
2600 IFFLAG("MULTICAST", IFF_MULTICAST);
2610 char *buf, *p, *lim;
2611 struct rt_msghdr *rtm;
2620 mib[3] = AF_INET6; /* Address family */
2621 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2622 mib[5] = 0; /* No flags */
2630 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2631 errmsg = "sysctl estimate";
2634 if ((buf = malloc(msize)) == NULL) {
2638 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2639 errmsg = "sysctl NET_RT_DUMP";
2642 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2644 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2647 } else if (1 < retry)
2648 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2651 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2652 rtm = (struct rt_msghdr *)p;
2653 rt_entry(rtm, again);
2659 rt_entry(struct rt_msghdr *rtm, int again)
2661 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2662 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2663 char *rtmp, *ifname = NULL;
2664 struct riprt *rrt, *orrt;
2665 struct netinfo6 *np;
2668 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2669 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2670 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2671 return; /* not interested in the link route */
2673 /* do not look at cloned routes */
2674 #ifdef RTF_WASCLONED
2675 if (rtm->rtm_flags & RTF_WASCLONED)
2679 if (rtm->rtm_flags & RTF_CLONED)
2682 /* XXX: Ignore connected routes. */
2683 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2686 * do not look at dynamic routes.
2687 * netbsd/openbsd cloned routes have UGHD.
2689 if (rtm->rtm_flags & RTF_DYNAMIC)
2691 rtmp = (char *)(rtm + 1);
2693 if ((rtm->rtm_addrs & RTA_DST) == 0)
2694 return; /* ignore routes without destination address */
2695 sin6_dst = (struct sockaddr_in6 *)rtmp;
2696 rtmp += ROUNDUP(sin6_dst->sin6_len);
2697 if (rtm->rtm_addrs & RTA_GATEWAY) {
2698 sin6_gw = (struct sockaddr_in6 *)rtmp;
2699 rtmp += ROUNDUP(sin6_gw->sin6_len);
2701 if (rtm->rtm_addrs & RTA_NETMASK) {
2702 sin6_mask = (struct sockaddr_in6 *)rtmp;
2703 rtmp += ROUNDUP(sin6_mask->sin6_len);
2705 if (rtm->rtm_addrs & RTA_GENMASK) {
2706 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2707 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2709 if (rtm->rtm_addrs & RTA_IFP) {
2710 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2711 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2715 if (sin6_dst->sin6_family != AF_INET6)
2717 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2718 return; /* Link-local */
2719 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2720 return; /* Loopback */
2721 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2724 if ((rrt = MALLOC(struct riprt)) == NULL) {
2725 fatal("malloc: struct riprt");
2728 memset(rrt, 0, sizeof(*rrt));
2729 np = &rrt->rrt_info;
2730 rrt->rrt_same = NULL;
2731 rrt->rrt_t = time(NULL);
2732 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2733 rrt->rrt_t = 0; /* Don't age static routes */
2734 if (rtm->rtm_flags & Pflag)
2735 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2736 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2737 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2739 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2740 if (np->rip6_metric < 1)
2741 np->rip6_metric = 1;
2742 rrt->rrt_flags = rtm->rtm_flags;
2743 np->rip6_dest = sin6_dst->sin6_addr;
2746 if (rtm->rtm_flags & RTF_HOST)
2747 np->rip6_plen = 128; /* Host route */
2749 np->rip6_plen = sin6mask2len(sin6_mask);
2753 orrt = rtsearch(np);
2754 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2757 trace(1, "route: %s/%d flags %s: already registered\n",
2758 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2766 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2768 if (sin6_gw->sin6_family == AF_INET6)
2769 rrt->rrt_gw = sin6_gw->sin6_addr;
2770 else if (sin6_gw->sin6_family == AF_LINK) {
2771 /* XXX in case ppp link? */
2772 rrt->rrt_gw = in6addr_loopback;
2774 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2776 trace(1, "route: %s/%d flags %s",
2777 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2778 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2781 ifindex = rtm->rtm_index;
2782 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2783 ifname = index2ifc[ifindex]->ifc_name;
2785 trace(1, " not configured\n");
2789 trace(1, " if %s sock %d", ifname, ifindex);
2790 rrt->rrt_index = ifindex;
2795 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2796 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2797 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2798 trace(0, "***** Gateway %s is not a link-local address.\n",
2799 inet6_n2p(&rrt->rrt_gw));
2800 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2801 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2802 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2805 /* Put it to the route list */
2806 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2807 /* replace route list */
2808 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2809 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2811 trace(1, "route: %s/%d flags %s: replace new route\n",
2812 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2816 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2820 addroute(struct riprt *rrt,
2821 const struct in6_addr *gw,
2824 struct netinfo6 *np;
2825 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2826 struct rt_msghdr *rtm;
2827 struct sockaddr_in6 *sin6;
2830 np = &rrt->rrt_info;
2831 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2832 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2833 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2834 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2835 np->rip6_metric - 1, buf2);
2837 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2838 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2839 np->rip6_metric - 1, buf2);
2843 memset(buf, 0, sizeof(buf));
2844 rtm = (struct rt_msghdr *)buf;
2845 rtm->rtm_type = RTM_ADD;
2846 rtm->rtm_version = RTM_VERSION;
2847 rtm->rtm_seq = ++seq;
2849 rtm->rtm_flags = rrt->rrt_flags;
2850 rtm->rtm_flags |= Qflag;
2851 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2852 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2853 rtm->rtm_inits = RTV_HOPCOUNT;
2854 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2856 sin6->sin6_len = sizeof(struct sockaddr_in6);
2857 sin6->sin6_family = AF_INET6;
2858 sin6->sin6_addr = np->rip6_dest;
2859 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2861 sin6->sin6_len = sizeof(struct sockaddr_in6);
2862 sin6->sin6_family = AF_INET6;
2863 sin6->sin6_addr = *gw;
2864 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2866 sin6->sin6_len = sizeof(struct sockaddr_in6);
2867 sin6->sin6_family = AF_INET6;
2868 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2869 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2871 len = (char *)sin6 - (char *)buf;
2872 rtm->rtm_msglen = len;
2873 if (write(rtsock, buf, len) > 0)
2876 if (errno == EEXIST) {
2877 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2878 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2880 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2881 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2883 trace(0, "Can not write to rtsock (addroute): %s\n",
2886 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2893 delroute(struct netinfo6 *np, struct in6_addr *gw)
2895 u_char buf[BUFSIZ], buf2[BUFSIZ];
2896 struct rt_msghdr *rtm;
2897 struct sockaddr_in6 *sin6;
2900 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2901 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2902 np->rip6_plen, buf2);
2904 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2905 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2909 memset(buf, 0, sizeof(buf));
2910 rtm = (struct rt_msghdr *)buf;
2911 rtm->rtm_type = RTM_DELETE;
2912 rtm->rtm_version = RTM_VERSION;
2913 rtm->rtm_seq = ++seq;
2915 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2916 rtm->rtm_flags |= Qflag;
2917 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2918 rtm->rtm_flags |= RTF_HOST;
2919 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2920 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2922 sin6->sin6_len = sizeof(struct sockaddr_in6);
2923 sin6->sin6_family = AF_INET6;
2924 sin6->sin6_addr = np->rip6_dest;
2925 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2927 sin6->sin6_len = sizeof(struct sockaddr_in6);
2928 sin6->sin6_family = AF_INET6;
2929 sin6->sin6_addr = *gw;
2930 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2932 sin6->sin6_len = sizeof(struct sockaddr_in6);
2933 sin6->sin6_family = AF_INET6;
2934 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2935 sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2937 len = (char *)sin6 - (char *)buf;
2938 rtm->rtm_msglen = len;
2939 if (write(rtsock, buf, len) >= 0)
2942 if (errno == ESRCH) {
2943 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2944 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2946 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2947 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2949 trace(0, "Can not write to rtsock (delroute): %s\n",
2952 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2959 getroute(struct netinfo6 *np, struct in6_addr *gw)
2964 struct rt_msghdr *rtm;
2965 struct sockaddr_in6 *sin6;
2967 rtm = (struct rt_msghdr *)buf;
2968 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2969 memset(rtm, 0, len);
2970 rtm->rtm_type = RTM_GET;
2971 rtm->rtm_version = RTM_VERSION;
2973 rtm->rtm_seq = myseq;
2974 rtm->rtm_addrs = RTA_DST;
2975 rtm->rtm_msglen = len;
2976 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2977 sin6->sin6_len = sizeof(struct sockaddr_in6);
2978 sin6->sin6_family = AF_INET6;
2979 sin6->sin6_addr = np->rip6_dest;
2980 if (write(rtsock, buf, len) < 0) {
2981 if (errno == ESRCH) /* No such route found */
2983 perror("write to rtsock");
2987 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2988 perror("read from rtsock");
2991 rtm = (struct rt_msghdr *)buf;
2992 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2993 sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2994 if (rtm->rtm_addrs & RTA_DST) {
2995 sin6 = (struct sockaddr_in6 *)
2996 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2998 if (rtm->rtm_addrs & RTA_GATEWAY) {
2999 *gw = sin6->sin6_addr;
3006 inet6_n2p(const struct in6_addr *p)
3008 static char buf[BUFSIZ];
3010 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3031 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3034 fprintf(dump, "%s: Interface Table Dump\n", hms());
3035 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3037 fprintf(dump, " Number of interfaces: %d\n", nifc);
3039 fprintf(dump, " advertising interfaces:\n");
3040 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3041 if ((ifcp->ifc_flags & IFF_UP) == 0)
3043 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3045 ifdump0(dump, ifcp);
3047 fprintf(dump, "\n");
3048 fprintf(dump, " non-advertising interfaces:\n");
3049 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3050 if ((ifcp->ifc_flags & IFF_UP) &&
3051 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3053 ifdump0(dump, ifcp);
3055 fprintf(dump, "\n");
3061 ifdump0(FILE *dump, const struct ifc *ifcp)
3069 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3070 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3071 inet6_n2p(&ifcp->ifc_mylladdr),
3072 ifcp->ifc_mtu, ifcp->ifc_metric);
3073 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3074 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3075 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3077 fprintf(dump, "\t%s/%d -- %s\n",
3078 inet6_n2p(&ifac->ifac_addr),
3079 ifac->ifac_plen, buf);
3081 fprintf(dump, "\t%s/%d\n",
3082 inet6_n2p(&ifac->ifac_addr),
3087 fprintf(dump, "\tFilter:\n");
3088 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3090 switch (iffp->iff_type) {
3092 ft = "Aggregate"; addr++; break;
3094 ft = "No-use"; break;
3096 ft = "Advertise-only"; addr++; break;
3098 ft = "Default-only"; break;
3100 ft = "Listen-only"; addr++; break;
3102 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3107 fprintf(dump, "\t\t%s", ft);
3109 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3111 fprintf(dump, "\n");
3113 fprintf(dump, "\n");
3127 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3131 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3132 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3133 if (rrt->rrt_t == 0)
3136 age = t - rrt->rrt_t;
3137 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3139 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3140 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3141 index2ifc[rrt->rrt_index]->ifc_name,
3142 inet6_n2p(&rrt->rrt_gw),
3143 rrt->rrt_info.rip6_metric, (long)age);
3144 if (rrt->rrt_info.rip6_tag) {
3145 fprintf(dump, " tag(0x%04x)",
3146 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3148 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3149 fprintf(dump, " NOT-LL");
3150 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3151 fprintf(dump, " NO-ADV");
3152 fprintf(dump, "\n");
3154 fprintf(dump, "\n");
3160 * Parse the -A (and -O) options and put corresponding filter object to the
3161 * specified interface structures. Each of the -A/O option has the following
3162 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3163 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3169 char *p, *ap, *iflp, *ifname, *ep;
3170 struct iff iff, *iffp;
3178 for (i = 0; i < nfilter; i++) {
3182 memset(iffp, 0, sizeof(*iffp));
3183 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3187 if ((p = strchr(ap, ',')) != NULL) {
3191 if ((p = strchr(ap, '/')) == NULL) {
3192 fatal("no prefixlen specified for '%s'", ap);
3196 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3197 fatal("invalid prefix specified for '%s'", ap);
3202 plen = strtoul(p, &ep, 10);
3203 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3204 fatal("invalid prefix length specified for '%s'", ap);
3207 iffp->iff_plen = plen;
3208 applyplen(&iffp->iff_addr, iffp->iff_plen);
3210 iffp->iff_type = filtertype[i];
3211 if (iflp == NULL || *iflp == '\0') {
3212 fatal("no interface specified for '%s'", ap);
3215 /* parse the interface listing portion */
3218 if ((iflp = strchr(iflp, ',')) != NULL)
3221 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3222 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3225 iffp = malloc(sizeof(*iffp));
3227 fatal("malloc of iff");
3230 memcpy(iffp, &iff, sizeof(*iffp));
3232 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3234 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3239 * -A: aggregate configuration.
3241 if (filtertype[i] != IFIL_TYPE_A)
3243 /* put the aggregate to the kernel routing table */
3244 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3246 fatal("malloc: rrt");
3249 memset(rrt, 0, sizeof(struct riprt));
3250 rrt->rrt_info.rip6_dest = iff.iff_addr;
3251 rrt->rrt_info.rip6_plen = iff.iff_plen;
3252 rrt->rrt_info.rip6_metric = 1;
3253 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3254 rrt->rrt_gw = in6addr_loopback;
3255 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3256 rrt->rrt_rflags = RRTF_AGGREGATE;
3258 rrt->rrt_index = loopifcp->ifc_index;
3260 if (getroute(&rrt->rrt_info, &gw)) {
3263 * When the address has already been registered in the
3264 * kernel routing table, it should be removed
3266 delroute(&rrt->rrt_info, &gw);
3268 /* it is safer behavior */
3270 fatal("%s/%u already in routing table, "
3272 inet6_n2p(&rrt->rrt_info.rip6_dest),
3273 rrt->rrt_info.rip6_plen);
3278 /* Put the route to the list */
3279 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3280 trace(1, "Aggregate: %s/%d for %s\n",
3281 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3282 loopifcp->ifc_name);
3283 /* Add this route to the kernel */
3284 if (nflag) /* do not modify kernel routing table */
3286 addroute(rrt, &in6addr_loopback, loopifcp);
3290 /***************** utility functions *****************/
3293 * Returns a pointer to ifac whose address and prefix length matches
3294 * with the address and prefix length specified in the arguments.
3297 ifa_match(const struct ifc *ifcp,
3298 const struct in6_addr *ia,
3303 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3304 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3305 ifac->ifac_plen == plen)
3313 * Return a pointer to riprt structure whose address and prefix length
3314 * matches with the address and prefix length found in the argument.
3315 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3318 rtsearch(struct netinfo6 *np)
3322 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3323 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3324 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3333 sin6mask2len(const struct sockaddr_in6 *sin6)
3336 return mask2len(&sin6->sin6_addr,
3337 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3341 mask2len(const struct in6_addr *addr, int lenlim)
3344 const u_char *p = (const u_char *)addr;
3346 for (j = 0; j < lenlim; j++, p++) {
3353 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3354 MASKLEN(0xfe, 7); break;
3355 MASKLEN(0xfc, 6); break;
3356 MASKLEN(0xf8, 5); break;
3357 MASKLEN(0xf0, 4); break;
3358 MASKLEN(0xe0, 3); break;
3359 MASKLEN(0xc0, 2); break;
3360 MASKLEN(0x80, 1); break;
3368 applymask(struct in6_addr *addr, struct in6_addr *mask)
3373 p = (u_long *)addr; q = (u_long *)mask;
3374 for (i = 0; i < 4; i++)
3378 static const u_char plent[8] = {
3379 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3383 applyplen(struct in6_addr *ia, int plen)
3389 for (i = 0; i < 16; i++) {
3398 static const int pl2m[9] = {
3399 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3405 static struct in6_addr ia;
3409 memset(&ia, 0, sizeof(struct in6_addr));
3411 for (i = 0; i < 16; i++, p++, n -= 8) {
3425 int len = strlen(p) + 1;
3426 char *q = (char *)malloc(len);
3440 static char buf[BUFSIZ];
3445 if ((tm = localtime(&t)) == 0) {
3449 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3454 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3457 ripinterval(int timer)
3461 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3462 nextalarm = time(NULL) + interval;
3472 t = (int)(RIP_TRIG_INT6_MIN +
3473 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3474 sup_trig_update = time(NULL) + t;
3480 fatal(const char *fmt, ...)
3482 fatal(fmt, va_alist)
3495 vsnprintf(buf, sizeof(buf), fmt, ap);
3499 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3501 syslog(LOG_ERR, "%s", buf);
3507 tracet(int level, const char *fmt, ...)
3509 tracet(level, fmt, va_alist)
3517 if (level <= dflag) {
3523 fprintf(stderr, "%s: ", hms());
3524 vfprintf(stderr, fmt, ap);
3534 vsyslog(LOG_DEBUG, fmt, ap);
3536 vsyslog(LOG_WARNING, fmt, ap);
3543 trace(int level, const char *fmt, ...)
3545 trace(level, fmt, va_alist)
3553 if (level <= dflag) {
3559 vfprintf(stderr, fmt, ap);
3569 vsyslog(LOG_DEBUG, fmt, ap);
3571 vsyslog(LOG_WARNING, fmt, ap);
3579 struct if_nameindex *p, *p0;
3580 unsigned int max = 0;
3582 p0 = if_nameindex();
3583 for (p = p0; p && p->if_index && p->if_name; p++) {
3584 if (max < p->if_index)
3587 if_freenameindex(p0);
3592 ifc_find(char *name)
3596 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3597 if (strcmp(name, ifcp->ifc_name) == 0)
3604 iff_find(struct ifc *ifcp, int type)
3608 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3609 if (type == IFIL_TYPE_ANY ||
3610 type == iffp->iff_type)
3618 setindex2ifc(int idx, struct ifc *ifcp)
3624 nindex2ifc = 5; /*initial guess*/
3625 index2ifc = (struct ifc **)
3626 malloc(sizeof(*index2ifc) * nindex2ifc);
3627 if (index2ifc == NULL) {
3631 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3634 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3637 p = (struct ifc **)realloc(index2ifc,
3638 sizeof(*index2ifc) * nsize);
3643 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3647 index2ifc[idx] = ifcp;