2 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
39 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
46 #include <arpa/inet.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip6.h>
52 #include <netinet/udp.h>
74 #define RT_DUMP_MAXRETRY 15
77 #define INIT_INTERVAL6 6
79 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
82 /* alignment constraint for routing socket */
84 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
85 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
87 struct ifc { /* Configuration of an interface */
88 TAILQ_ENTRY(ifc) ifc_next;
90 char ifc_name[IFNAMSIZ]; /* if name */
91 int ifc_index; /* if index */
92 int ifc_mtu; /* if mtu */
93 int ifc_metric; /* if metric */
94 u_int ifc_flags; /* flags */
95 short ifc_cflags; /* IFC_XXX */
96 struct in6_addr ifc_mylladdr; /* my link-local address */
97 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
98 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
99 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
100 int ifc_joined; /* joined to ff02::9 */
102 static TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
104 struct ifac { /* Adddress associated to an interface */
105 TAILQ_ENTRY(ifac) ifac_next;
107 struct ifc *ifac_ifc; /* back pointer */
108 struct in6_addr ifac_addr; /* address */
109 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
110 int ifac_scope_id; /* scope id */
111 int ifac_plen; /* prefix length */
114 struct iff { /* Filters for an interface */
115 TAILQ_ENTRY(iff) iff_next;
118 struct in6_addr iff_addr;
122 static struct ifc **index2ifc;
123 static unsigned int nindex2ifc;
124 static struct ifc *loopifcp = NULL; /* pointing to loopback */
126 static struct pollfd set[2];
128 static fd_set *sockvecp; /* vector to select() for receiving */
129 static fd_set *recvecp;
131 static int maxfd; /* maximum fd for select() */
133 static int rtsock; /* the routing socket */
134 static int ripsock; /* socket to send/receive RIP datagram */
136 static struct rip6 *ripbuf; /* packet buffer for sending */
139 * Maintain the routes in a linked list. When the number of the routes
140 * grows, somebody would like to introduce a hash based or a radix tree
141 * based structure. I believe the number of routes handled by RIP is
142 * limited and I don't have to manage a complex data structure, however.
144 * One of the major drawbacks of the linear linked list is the difficulty
145 * of representing the relationship between a couple of routes. This may
146 * be a significant problem when we have to support route aggregation with
147 * suppressing the specifics covered by the aggregate.
151 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
153 struct riprt *rrt_same; /* same destination - future use */
154 struct netinfo6 rrt_info; /* network info */
155 struct in6_addr rrt_gw; /* gateway */
156 u_long rrt_flags; /* kernel routing table flags */
157 u_long rrt_rflags; /* route6d routing table flags */
158 time_t rrt_t; /* when the route validated */
159 int rrt_index; /* ifindex from which this route got */
161 static TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
163 static int dflag = 0; /* debug flag */
164 static int qflag = 0; /* quiet flag */
165 static int nflag = 0; /* don't update kernel routing table */
166 static int aflag = 0; /* age out even the statically defined routes */
167 static int hflag = 0; /* don't split horizon */
168 static int lflag = 0; /* exchange site local routes */
169 static int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
170 static int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
171 static int sflag = 0; /* announce static routes w/ split horizon */
172 static int Sflag = 0; /* announce static routes to every interface */
173 static unsigned long routetag = 0; /* route tag attached on originating case */
175 static char *filter[MAXFILTER];
176 static int filtertype[MAXFILTER];
177 static int nfilter = 0;
181 static struct sockaddr_storage ripsin;
183 static int interval = 1;
184 static time_t nextalarm = 0;
186 static time_t sup_trig_update = 0;
189 static FILE *rtlog = NULL;
191 static int logopened = 0;
195 static volatile sig_atomic_t seenalrm;
196 static volatile sig_atomic_t seenquit;
197 static volatile sig_atomic_t seenusr1;
199 #define RRTF_AGGREGATE 0x08000000
200 #define RRTF_NOADVERTISE 0x10000000
201 #define RRTF_NH_NOT_LLADDR 0x20000000
202 #define RRTF_SENDANYWAY 0x40000000
203 #define RRTF_CHANGED 0x80000000
205 static void sighandler(int);
206 static void ripalarm(void);
207 static void riprecv(void);
208 static void ripsend(struct ifc *, struct sockaddr_in6 *, int);
209 static int out_filter(struct riprt *, struct ifc *);
210 static void init(void);
211 static void ifconfig(void);
212 static int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
213 static void rtrecv(void);
214 static int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
215 const struct sockaddr_in6 *);
216 static int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
217 const struct sockaddr_in6 *);
218 static void filterconfig(void);
219 static int getifmtu(int);
220 static const char *rttypes(struct rt_msghdr *);
221 static const char *rtflags(struct rt_msghdr *);
222 static const char *ifflags(int);
223 static int ifrt(struct ifc *, int);
224 static void ifrt_p2p(struct ifc *, int);
225 static void applyplen(struct in6_addr *, int);
226 static void ifrtdump(int);
227 static void ifdump(int);
228 static void ifdump0(FILE *, const struct ifc *);
229 static void ifremove(int);
230 static void rtdump(int);
231 static void rt_entry(struct rt_msghdr *, int);
232 static void rtdexit(void);
233 static void riprequest(struct ifc *, struct netinfo6 *, int,
234 struct sockaddr_in6 *);
235 static void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
236 static void sendrequest(struct ifc *);
237 static int sin6mask2len(const struct sockaddr_in6 *);
238 static int mask2len(const struct in6_addr *, int);
239 static int sendpacket(struct sockaddr_in6 *, int);
240 static int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
241 static int delroute(struct netinfo6 *, struct in6_addr *);
243 static struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
245 static void krtread(int);
246 static int tobeadv(struct riprt *, struct ifc *);
247 static char *allocopy(char *);
248 static char *hms(void);
249 static const char *inet6_n2p(const struct in6_addr *);
250 static struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
251 static struct in6_addr *plen2mask(int);
252 static struct riprt *rtsearch(struct netinfo6 *);
253 static int ripinterval(int);
255 static time_t ripsuptrig(void);
257 static void fatal(const char *, ...)
258 __attribute__((__format__(__printf__, 1, 2)));
259 static void trace(int, const char *, ...)
260 __attribute__((__format__(__printf__, 2, 3)));
261 static void tracet(int, const char *, ...)
262 __attribute__((__format__(__printf__, 2, 3)));
263 static struct ifc *ifc_find(char *);
264 static struct iff *iff_find(struct ifc *, int);
265 static void setindex2ifc(int, struct ifc *);
267 #define MALLOC(type) ((type *)malloc(sizeof(type)))
269 #define IFIL_TYPE_ANY 0x0
270 #define IFIL_TYPE_A 'A'
271 #define IFIL_TYPE_N 'N'
272 #define IFIL_TYPE_T 'T'
273 #define IFIL_TYPE_O 'O'
274 #define IFIL_TYPE_L 'L'
277 main(int argc, char *argv[])
283 sigset_t mask, omask;
284 const char *pidfile = ROUTE6D_PID;
289 progname = strrchr(*argv, '/');
296 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
303 if (nfilter >= MAXFILTER) {
304 fatal("Exceeds MAXFILTER");
307 filtertype[nfilter] = ch;
308 filter[nfilter++] = allocopy(optarg);
312 routetag = strtoul(optarg, &ep, 0);
313 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
314 fatal("invalid route tag");
323 proto = strtoul(optarg, &ep, 0);
324 if (!ep || *ep != '\0' || 3 < proto) {
325 fatal("invalid P flag");
339 proto = strtoul(optarg, &ep, 0);
340 if (!ep || *ep != '\0' || 3 < proto) {
341 fatal("invalid Q flag");
354 if ((rtlog = fopen(optarg, "w")) == NULL) {
355 fatal("Can not write to routelog");
359 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
360 FLAG('a', aflag, 1); break;
361 FLAG('d', dflag, 1); break;
362 FLAG('D', dflag, 2); break;
363 FLAG('h', hflag, 1); break;
364 FLAG('l', lflag, 1); break;
365 FLAG('n', nflag, 1); break;
366 FLAG('q', qflag, 1); break;
367 FLAG('s', sflag, 1); break;
368 FLAG('S', Sflag, 1); break;
371 fatal("Invalid option specified, terminating");
378 fatal("bogus extra arguments");
384 fprintf(stderr, "No kernel update is allowed\n");
388 if (daemon(0, 0) < 0) {
394 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
397 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
399 memset(ripbuf, 0, RIP6_MAXMTU);
400 ripbuf->rip6_cmd = RIP6_RESPONSE;
401 ripbuf->rip6_vers = RIP6_VERSION;
402 ripbuf->rip6_res1[0] = 0;
403 ripbuf->rip6_res1[1] = 0;
407 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
408 if (ifcp->ifc_index < 0) {
409 fprintf(stderr, "No ifindex found at %s "
410 "(no link-local address?)\n", ifcp->ifc_name);
416 if (loopifcp == NULL) {
417 fatal("No loopback found");
420 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
429 if ((pidfh = fopen(pidfile, "w")) != NULL) {
430 fprintf(pidfh, "%d\n", pid);
434 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
438 memset(ripbuf, 0, RIP6_MAXMTU);
439 ripbuf->rip6_cmd = RIP6_RESPONSE;
440 ripbuf->rip6_vers = RIP6_VERSION;
441 ripbuf->rip6_res1[0] = 0;
442 ripbuf->rip6_res1[1] = 0;
444 if (signal(SIGALRM, sighandler) == SIG_ERR ||
445 signal(SIGQUIT, sighandler) == SIG_ERR ||
446 signal(SIGTERM, sighandler) == SIG_ERR ||
447 signal(SIGUSR1, sighandler) == SIG_ERR ||
448 signal(SIGHUP, sighandler) == SIG_ERR ||
449 signal(SIGINT, sighandler) == SIG_ERR) {
454 * To avoid rip packet congestion (not on a cable but in this
455 * process), wait for a moment to send the first RIP6_RESPONSE
458 alarm(ripinterval(INIT_INTERVAL6));
460 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
461 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
463 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
467 syslog(LOG_INFO, "**** Started ****");
469 sigaddset(&mask, SIGALRM);
488 switch (poll(set, 2, INFTIM))
490 memcpy(recvecp, sockvecp, fdmasks);
491 switch (select(maxfd + 1, recvecp, 0, 0, 0))
495 if (errno != EINTR) {
504 if (set[0].revents & POLLIN)
506 if (FD_ISSET(ripsock, recvecp))
509 sigprocmask(SIG_BLOCK, &mask, &omask);
511 sigprocmask(SIG_SETMASK, &omask, NULL);
514 if (set[1].revents & POLLIN)
516 if (FD_ISSET(rtsock, recvecp))
519 sigprocmask(SIG_BLOCK, &mask, &omask);
521 sigprocmask(SIG_SETMASK, &omask, NULL);
528 sighandler(int signo)
548 * gracefully exits after resetting sockopts.
557 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
558 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
559 delroute(&rrt->rrt_info, &rrt->rrt_gw);
564 syslog(LOG_INFO, "**** Terminated ****");
570 * Called periodically:
571 * 1. age out the learned route. remove it if necessary.
572 * 2. submit RIP6_RESPONSE packets.
573 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
574 * to invoke this function in every 1 or 5 or 10 seconds only to age the
575 * routes more precisely.
582 struct riprt *rrt, *rrt_tmp;
583 time_t t_lifetime, t_holddown;
585 /* age the RIP routes */
586 t_lifetime = time(NULL) - RIP_LIFETIME;
587 t_holddown = t_lifetime - RIP_HOLDDOWN;
588 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
591 else if (rrt->rrt_t < t_holddown) {
592 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
593 delroute(&rrt->rrt_info, &rrt->rrt_gw);
595 } else if (rrt->rrt_t < t_lifetime)
596 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
599 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
600 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
601 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
603 alarm(ripinterval(SUPPLY_INTERVAL6));
610 const int int0 = 0, int1 = 1, int255 = 255;
611 struct addrinfo hints, *res;
612 char port[NI_MAXSERV];
614 TAILQ_INIT(&ifc_head);
615 nindex2ifc = 0; /*initial guess*/
617 snprintf(port, sizeof(port), "%u", RIP6_PORT);
619 memset(&hints, 0, sizeof(hints));
620 hints.ai_family = PF_INET6;
621 hints.ai_socktype = SOCK_DGRAM;
622 hints.ai_protocol = IPPROTO_UDP;
623 hints.ai_flags = AI_PASSIVE;
624 error = getaddrinfo(NULL, port, &hints, &res);
626 fatal("%s", gai_strerror(error));
630 fatal(":: resolved to multiple address");
634 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
640 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
641 &int1, sizeof(int1)) < 0) {
642 fatal("rip IPV6_V6ONLY");
646 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
650 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
651 &int255, sizeof(int255)) < 0) {
652 fatal("rip IPV6_MULTICAST_HOPS");
655 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
656 &int0, sizeof(int0)) < 0) {
657 fatal("rip IPV6_MULTICAST_LOOP");
661 #ifdef IPV6_RECVPKTINFO
662 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
663 &int1, sizeof(int1)) < 0) {
664 fatal("rip IPV6_RECVPKTINFO");
667 #else /* old adv. API */
668 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
669 &int1, sizeof(int1)) < 0) {
670 fatal("rip IPV6_PKTINFO");
675 #ifdef IPV6_RECVPKTINFO
676 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
677 &int1, sizeof(int1)) < 0) {
678 fatal("rip IPV6_RECVHOPLIMIT");
681 #else /* old adv. API */
682 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
683 &int1, sizeof(int1)) < 0) {
684 fatal("rip IPV6_HOPLIMIT");
690 memset(&hints, 0, sizeof(hints));
691 hints.ai_family = PF_INET6;
692 hints.ai_socktype = SOCK_DGRAM;
693 hints.ai_protocol = IPPROTO_UDP;
694 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
696 fatal("%s", gai_strerror(error));
700 fatal("%s resolved to multiple address", RIP6_DEST);
703 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
708 set[0].events = POLLIN;
714 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
715 fatal("route socket");
720 set[1].events = POLLIN;
729 rtsock = -1; /*just for safety */
734 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
735 if ((sockvecp = malloc(fdmasks)) == NULL) {
739 if ((recvecp = malloc(fdmasks)) == NULL) {
743 memset(sockvecp, 0, fdmasks);
744 FD_SET(ripsock, sockvecp);
746 FD_SET(rtsock, sockvecp);
751 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
754 * ripflush flushes the rip datagram stored in the rip buffer
757 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
763 tracet(1, "Send(%s): info(%d) to %s.%d\n",
765 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
767 tracet(1, "Send: info(%d) to %s.%d\n",
768 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
770 np = ripbuf->rip6_nets;
771 for (i = 0; i < nrt; i++, np++) {
772 if (np->rip6_metric == NEXTHOP_METRIC) {
773 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
774 trace(2, " NextHop reset");
776 trace(2, " NextHop %s",
777 inet6_n2p(&np->rip6_dest));
780 trace(2, " %s/%d[%d]",
781 inet6_n2p(&np->rip6_dest),
782 np->rip6_plen, np->rip6_metric);
785 trace(2, " tag=0x%04x",
786 ntohs(np->rip6_tag) & 0xffff);
791 error = sendpacket(sin6, RIPSIZE(nrt));
792 if (error == EAFNOSUPPORT) {
793 /* Protocol not supported */
795 tracet(1, "Could not send info to %s (%s): "
798 inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
799 /* As if down for AF_INET6 */
800 ifcp->ifc_flags &= ~IFF_UP;
802 tracet(1, "Could not send info to %s\n",
803 inet6_n2p(&sin6->sin6_addr));
809 * Generate RIP6_RESPONSE packets and send them.
812 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
815 struct in6_addr *nh; /* next hop */
825 * Request from non-link local address is not
826 * a regular route6d update.
828 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
829 sizeof(struct udphdr) -
830 sizeof(struct rip6) + sizeof(struct netinfo6)) /
831 sizeof(struct netinfo6);
834 np = ripbuf->rip6_nets;
835 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
836 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
838 /* Put the route to the buffer */
842 ripflush(NULL, sin6, nrt, np);
845 np = ripbuf->rip6_nets;
848 if (nrt) /* Send last packet */
849 ripflush(NULL, sin6, nrt, np);
853 if ((flag & RRTF_SENDANYWAY) == 0 &&
854 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
858 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
861 /* -T: generate default route only */
862 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
863 struct netinfo6 rrt_info;
864 memset(&rrt_info, 0, sizeof(struct netinfo6));
865 rrt_info.rip6_dest = in6addr_any;
866 rrt_info.rip6_plen = 0;
867 rrt_info.rip6_metric = 1;
868 rrt_info.rip6_metric += ifcp->ifc_metric;
869 rrt_info.rip6_tag = htons(routetag & 0xffff);
870 np = ripbuf->rip6_nets;
873 ripflush(ifcp, sin6, nrt, np);
877 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
878 sizeof(struct udphdr) -
879 sizeof(struct rip6) + sizeof(struct netinfo6)) /
880 sizeof(struct netinfo6);
882 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
883 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
884 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
887 /* Need to check filter here */
888 if (out_filter(rrt, ifcp) == 0)
891 /* Check split horizon and other conditions */
892 if (tobeadv(rrt, ifcp) == 0)
895 /* Only considers the routes with flag if specified */
896 if ((flag & RRTF_CHANGED) &&
897 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
901 if (rrt->rrt_index == ifcp->ifc_index &&
902 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
903 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
904 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
905 if (nrt == maxrte - 2) {
906 ripflush(ifcp, sin6, nrt, np);
909 np = ripbuf->rip6_nets;
912 np->rip6_dest = rrt->rrt_gw;
915 np->rip6_metric = NEXTHOP_METRIC;
919 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
920 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
921 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
923 if (nrt == maxrte - 2) {
924 ripflush(ifcp, sin6, nrt, np);
927 np = ripbuf->rip6_nets;
929 memset(np, 0, sizeof(struct netinfo6));
930 np->rip6_metric = NEXTHOP_METRIC;
935 /* Put the route to the buffer */
939 ripflush(ifcp, sin6, nrt, np);
942 np = ripbuf->rip6_nets;
945 if (nrt) /* Send last packet */
946 ripflush(ifcp, sin6, nrt, np);
950 * outbound filter logic, per-route/interface.
953 out_filter(struct riprt *rrt, struct ifc *ifcp)
960 * -A: filter out less specific routes, if we have aggregated
963 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
964 if (iffp->iff_type != 'A')
966 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
968 ia = rrt->rrt_info.rip6_dest;
969 applyplen(&ia, iffp->iff_plen);
970 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
975 * if it is an aggregated route, advertise it only to the
976 * interfaces specified on -A.
978 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
980 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
981 if (iffp->iff_type != 'A')
983 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
984 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
995 * -O: advertise only if prefix matches the configured prefix.
997 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
999 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1000 if (iffp->iff_type != 'O')
1002 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1004 ia = rrt->rrt_info.rip6_dest;
1005 applyplen(&ia, iffp->iff_plen);
1006 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1015 /* the prefix should be advertised */
1020 * Determine if the route is to be advertised on the specified interface.
1021 * It checks options specified in the arguments and the split horizon rule.
1024 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1027 /* Special care for static routes */
1028 if (rrt->rrt_flags & RTF_STATIC) {
1029 /* XXX don't advertise reject/blackhole routes */
1030 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1033 if (Sflag) /* Yes, advertise it anyway */
1035 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1039 /* Regular split horizon */
1040 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1046 * Send a rip packet actually.
1049 sendpacket(struct sockaddr_in6 *sin6, int len)
1053 struct iovec iov[2];
1054 struct in6_pktinfo *pi;
1055 u_char cmsgbuf[256];
1057 struct sockaddr_in6 sincopy;
1059 /* do not overwrite the given sin */
1063 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1064 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1065 idx = sin6->sin6_scope_id;
1069 m.msg_name = (caddr_t)sin6;
1070 m.msg_namelen = sizeof(*sin6);
1071 iov[0].iov_base = (caddr_t)ripbuf;
1072 iov[0].iov_len = len;
1077 m.msg_control = NULL;
1078 m.msg_controllen = 0;
1080 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1081 cm = (struct cmsghdr *)(void *)cmsgbuf;
1082 m.msg_control = (caddr_t)cm;
1083 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1085 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1086 cm->cmsg_level = IPPROTO_IPV6;
1087 cm->cmsg_type = IPV6_PKTINFO;
1088 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1089 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1090 pi->ipi6_ifindex = idx;
1093 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1094 trace(1, "sendmsg: %s\n", strerror(errno));
1102 * Receive and process RIP packets. Update the routes/kernel forwarding
1103 * table if necessary.
1108 struct ifc *ifcp, *ic;
1109 struct sockaddr_in6 fsock;
1110 struct in6_addr nh; /* next hop */
1112 struct netinfo6 *np, *nq;
1115 unsigned int need_trigger, idx;
1116 char buf[4 * RIP6_MAXMTU];
1120 struct iovec iov[2];
1121 u_char cmsgbuf[256];
1122 struct in6_pktinfo *pi = NULL;
1127 time_t t_half_lifetime;
1131 m.msg_name = (caddr_t)&fsock;
1132 m.msg_namelen = sizeof(fsock);
1133 iov[0].iov_base = (caddr_t)buf;
1134 iov[0].iov_len = sizeof(buf);
1137 cm = (struct cmsghdr *)(void *)cmsgbuf;
1138 m.msg_control = (caddr_t)cm;
1139 m.msg_controllen = sizeof(cmsgbuf);
1141 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1146 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1148 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1149 if (cm->cmsg_level != IPPROTO_IPV6)
1151 switch (cm->cmsg_type) {
1153 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1155 "invalid cmsg length for IPV6_PKTINFO\n");
1158 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1159 idx = pi->ipi6_ifindex;
1162 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1164 "invalid cmsg length for IPV6_HOPLIMIT\n");
1167 hlimp = (int *)(void *)CMSG_DATA(cm);
1172 if ((size_t)len < sizeof(struct rip6)) {
1173 trace(1, "Packet too short\n");
1177 if (pi == NULL || hlimp == NULL) {
1179 * This can happen when the kernel failed to allocate memory
1180 * for the ancillary data. Although we might be able to handle
1181 * some cases without this info, those are minor and not so
1182 * important, so it's better to discard the packet for safer
1185 trace(1, "IPv6 packet information cannot be retrieved\n");
1189 nh = fsock.sin6_addr;
1190 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1191 sizeof(struct netinfo6);
1192 rp = (struct rip6 *)(void *)buf;
1195 if (rp->rip6_vers != RIP6_VERSION) {
1196 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1199 if (rp->rip6_cmd == RIP6_REQUEST) {
1200 if (idx && idx < nindex2ifc) {
1201 ifcp = index2ifc[idx];
1202 riprequest(ifcp, np, nn, &fsock);
1204 riprequest(NULL, np, nn, &fsock);
1209 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1210 trace(1, "Response from non-ll addr: %s\n",
1211 inet6_n2p(&fsock.sin6_addr));
1212 return; /* Ignore packets from non-link-local addr */
1214 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1215 trace(1, "Response from non-rip port from %s\n",
1216 inet6_n2p(&fsock.sin6_addr));
1219 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1221 "Response packet with a smaller hop limit (%d) from %s\n",
1222 *hlimp, inet6_n2p(&fsock.sin6_addr));
1226 * Further validation: since this program does not send off-link
1227 * requests, an incoming response must always come from an on-link
1228 * node. Although this is normally ensured by the source address
1229 * check above, it may not 100% be safe because there are router
1230 * implementations that (invalidly) allow a packet with a link-local
1231 * source address to be forwarded to a different link.
1232 * So we also check whether the destination address is a link-local
1233 * address or the hop limit is 255. Note that RFC2080 does not require
1234 * the specific hop limit for a unicast response, so we cannot assume
1237 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1239 "Response packet possibly from an off-link node: "
1240 "from %s to %s hlim=%d\n",
1241 inet6_n2p(&fsock.sin6_addr),
1242 inet6_n2p(&pi->ipi6_addr), *hlimp);
1246 idx = fsock.sin6_scope_id;
1247 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1249 trace(1, "Packets to unknown interface index %d\n", idx);
1250 return; /* Ignore it */
1252 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1253 return; /* The packet is from me; ignore */
1254 if (rp->rip6_cmd != RIP6_RESPONSE) {
1255 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1260 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1263 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1264 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1267 t_half_lifetime = t - (RIP_LIFETIME/2);
1268 for (; nn; nn--, np++) {
1269 if (np->rip6_metric == NEXTHOP_METRIC) {
1270 /* modify neighbor address */
1271 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1273 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1274 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1275 nh = fsock.sin6_addr;
1276 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1278 nh = fsock.sin6_addr;
1279 trace(1, "\tInvalid Nexthop: %s\n",
1280 inet6_n2p(&np->rip6_dest));
1284 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1285 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1286 inet6_n2p(&np->rip6_dest),
1287 np->rip6_plen, np->rip6_metric);
1290 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1291 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1292 inet6_n2p(&np->rip6_dest),
1293 np->rip6_plen, np->rip6_metric);
1296 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1297 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1298 inet6_n2p(&np->rip6_dest),
1299 np->rip6_plen, np->rip6_metric);
1302 /* may need to pass sitelocal prefix in some case, however*/
1303 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1304 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1305 inet6_n2p(&np->rip6_dest),
1306 np->rip6_plen, np->rip6_metric);
1309 trace(2, "\tnetinfo6: %s/%d [%d]",
1310 inet6_n2p(&np->rip6_dest),
1311 np->rip6_plen, np->rip6_metric);
1313 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1316 applyplen(&ia, np->rip6_plen);
1317 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1318 trace(2, " [junk outside prefix]");
1322 * -L: listen only if the prefix matches the configuration
1324 ok = 1; /* if there's no L filter, it is ok */
1325 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1326 if (iffp->iff_type != IFIL_TYPE_L)
1329 if (np->rip6_plen < iffp->iff_plen)
1331 /* special rule: ::/0 means default, not "in /0" */
1332 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1335 applyplen(&ia, iffp->iff_plen);
1336 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1342 trace(2, " (filtered)\n");
1348 np->rip6_metric += ifcp->ifc_metric;
1349 if (np->rip6_metric > HOPCNT_INFINITY6)
1350 np->rip6_metric = HOPCNT_INFINITY6;
1352 applyplen(&np->rip6_dest, np->rip6_plen);
1353 if ((rrt = rtsearch(np)) != NULL) {
1354 if (rrt->rrt_t == 0)
1355 continue; /* Intf route has priority */
1356 nq = &rrt->rrt_info;
1357 if (nq->rip6_metric > np->rip6_metric) {
1358 if (rrt->rrt_index == ifcp->ifc_index &&
1359 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1360 /* Small metric from the same gateway */
1361 nq->rip6_metric = np->rip6_metric;
1363 /* Better route found */
1364 rrt->rrt_index = ifcp->ifc_index;
1365 /* Update routing table */
1366 delroute(nq, &rrt->rrt_gw);
1369 addroute(rrt, &nh, ifcp);
1371 rrt->rrt_rflags |= RRTF_CHANGED;
1374 } else if (nq->rip6_metric < np->rip6_metric &&
1375 rrt->rrt_index == ifcp->ifc_index &&
1376 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1377 /* Got worse route from same gw */
1378 nq->rip6_metric = np->rip6_metric;
1380 rrt->rrt_rflags |= RRTF_CHANGED;
1382 } else if (nq->rip6_metric == np->rip6_metric &&
1383 np->rip6_metric < HOPCNT_INFINITY6) {
1384 if (rrt->rrt_index == ifcp->ifc_index &&
1385 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1386 /* same metric, same route from same gw */
1388 } else if (rrt->rrt_t < t_half_lifetime) {
1389 /* Better route found */
1390 rrt->rrt_index = ifcp->ifc_index;
1391 /* Update routing table */
1392 delroute(nq, &rrt->rrt_gw);
1395 addroute(rrt, &nh, ifcp);
1396 rrt->rrt_rflags |= RRTF_CHANGED;
1401 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1402 * do not update age value. Do nothing.
1404 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1405 /* Got a new valid route */
1406 if ((rrt = MALLOC(struct riprt)) == NULL) {
1407 fatal("malloc: struct riprt");
1410 memset(rrt, 0, sizeof(*rrt));
1411 nq = &rrt->rrt_info;
1413 rrt->rrt_same = NULL;
1414 rrt->rrt_index = ifcp->ifc_index;
1415 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1418 applyplen(&nq->rip6_dest, nq->rip6_plen);
1419 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1420 rrt->rrt_flags |= RTF_HOST;
1422 /* Update routing table */
1423 addroute(rrt, &nh, ifcp);
1424 rrt->rrt_rflags |= RRTF_CHANGED;
1428 /* Put the route to the list */
1429 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1432 /* XXX need to care the interval between triggered updates */
1434 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1435 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1436 if (ifcp->ifc_index == ic->ifc_index)
1438 if (ic->ifc_flags & IFF_UP)
1439 ripsend(ic, &ic->ifc_ripsin,
1443 /* Reset the flag */
1444 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1445 rrt->rrt_rflags &= ~RRTF_CHANGED;
1451 * Send all routes request packet to the specified interface.
1454 sendrequest(struct ifc *ifcp)
1456 struct netinfo6 *np;
1459 if (ifcp->ifc_flags & IFF_LOOPBACK)
1461 ripbuf->rip6_cmd = RIP6_REQUEST;
1462 np = ripbuf->rip6_nets;
1463 memset(np, 0, sizeof(struct netinfo6));
1464 np->rip6_metric = HOPCNT_INFINITY6;
1465 tracet(1, "Send rtdump Request to %s (%s)\n",
1466 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1467 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1468 if (error == EAFNOSUPPORT) {
1469 /* Protocol not supported */
1470 tracet(1, "Could not send rtdump Request to %s (%s): "
1471 "set IFF_UP to 0\n",
1472 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1473 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1475 ripbuf->rip6_cmd = RIP6_RESPONSE;
1479 * Process a RIP6_REQUEST packet.
1482 riprequest(struct ifc *ifcp,
1483 struct netinfo6 *np,
1485 struct sockaddr_in6 *sin6)
1490 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1491 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1492 /* Specific response, don't split-horizon */
1493 trace(1, "\tRIP Request\n");
1494 for (i = 0; i < nn; i++, np++) {
1497 np->rip6_metric = rrt->rrt_info.rip6_metric;
1499 np->rip6_metric = HOPCNT_INFINITY6;
1501 (void)sendpacket(sin6, RIPSIZE(nn));
1504 /* Whole routing table dump */
1505 trace(1, "\tRIP Request -- whole routing table\n");
1506 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1510 * Get information of each interface.
1515 struct ifaddrs *ifap, *ifa;
1517 struct ipv6_mreq mreq;
1520 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1525 if (getifaddrs(&ifap) != 0) {
1526 fatal("getifaddrs");
1530 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1531 if (ifa->ifa_addr->sa_family != AF_INET6)
1533 ifcp = ifc_find(ifa->ifa_name);
1534 /* we are interested in multicast-capable interfaces */
1535 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1539 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1540 fatal("malloc: struct ifc");
1543 memset(ifcp, 0, sizeof(*ifcp));
1545 ifcp->ifc_index = -1;
1546 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1547 sizeof(ifcp->ifc_name));
1548 TAILQ_INIT(&ifcp->ifc_ifac_head);
1549 TAILQ_INIT(&ifcp->ifc_iff_head);
1550 ifcp->ifc_flags = ifa->ifa_flags;
1551 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1552 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1553 ifflags(ifcp->ifc_flags));
1554 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1557 /* update flag, this may be up again */
1558 if (ifcp->ifc_flags != ifa->ifa_flags) {
1559 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1560 ifflags(ifcp->ifc_flags));
1561 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1562 ifcp->ifc_cflags |= IFC_CHANGED;
1564 ifcp->ifc_flags = ifa->ifa_flags;
1566 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1567 /* maybe temporary failure */
1570 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1571 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1572 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1573 mreq.ipv6mr_interface = ifcp->ifc_index;
1574 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1575 &mreq, sizeof(mreq)) < 0) {
1576 fatal("IPV6_JOIN_GROUP");
1579 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1588 ifconfig1(const char *name,
1589 const struct sockaddr *sa,
1593 struct in6_ifreq ifr;
1594 const struct sockaddr_in6 *sin6;
1599 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
1600 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1602 ifr.ifr_addr = *sin6;
1603 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1604 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1605 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1608 plen = sin6mask2len(&ifr.ifr_addr);
1609 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1610 /* same interface found */
1611 /* need check if something changed */
1612 /* XXX not yet implemented */
1616 * New address is found
1618 if ((ifac = MALLOC(struct ifac)) == NULL) {
1619 fatal("malloc: struct ifac");
1622 memset(ifac, 0, sizeof(*ifac));
1624 ifac->ifac_ifc = ifcp;
1625 ifac->ifac_addr = sin6->sin6_addr;
1626 ifac->ifac_plen = plen;
1627 ifac->ifac_scope_id = sin6->sin6_scope_id;
1628 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1629 ifr.ifr_addr = *sin6;
1630 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1631 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1634 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1635 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1637 trace(1, "found address %s/%d -- %s\n",
1638 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1640 trace(1, "found address %s/%d\n",
1641 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1643 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1644 ifcp->ifc_mylladdr = ifac->ifac_addr;
1645 ifcp->ifc_index = ifac->ifac_scope_id;
1646 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1647 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1648 setindex2ifc(ifcp->ifc_index, ifcp);
1649 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1650 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1651 ifcp->ifc_mtu = RIP6_MAXMTU;
1652 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1653 fatal("ioctl: SIOCGIFMETRIC");
1656 ifcp->ifc_metric = ifr.ifr_metric;
1657 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1658 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1660 ifcp->ifc_cflags |= IFC_CHANGED;
1662 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1668 ifremove(int ifindex)
1673 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1674 if (ifcp->ifc_index == ifindex)
1680 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1681 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1683 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1684 if (rrt->rrt_index == ifcp->ifc_index &&
1685 rrt->rrt_rflags & RRTF_AGGREGATE)
1686 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1692 * Receive and process routing messages.
1693 * Update interface information as necesssary.
1700 struct rt_msghdr *rtm;
1701 struct ifa_msghdr *ifam;
1702 struct if_msghdr *ifm;
1703 struct if_announcemsghdr *ifan;
1705 struct ifc *ifcp, *ic;
1706 int iface = 0, rtable = 0;
1707 struct sockaddr_in6 *rta[RTAX_MAX];
1708 struct sockaddr_in6 mask;
1712 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1713 perror("read from rtsock");
1719 if (len < sizeof(*rtm)) {
1720 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1721 len, (u_long)sizeof(*rtm));
1726 fprintf(stderr, "rtmsg:\n");
1727 for (i = 0; i < len; i++) {
1728 fprintf(stderr, "%02x ", buf[i] & 0xff);
1729 if (i % 16 == 15) fprintf(stderr, "\n");
1731 fprintf(stderr, "\n");
1734 for (p = buf; p - buf < len; p +=
1735 ((struct rt_msghdr *)(void *)p)->rtm_msglen) {
1736 if (((struct rt_msghdr *)(void *)p)->rtm_version != RTM_VERSION)
1739 /* safety against bogus message */
1740 if (((struct rt_msghdr *)(void *)p)->rtm_msglen <= 0) {
1741 trace(1, "bogus rtmsg: length=%d\n",
1742 ((struct rt_msghdr *)(void *)p)->rtm_msglen);
1748 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1751 ifam = (struct ifa_msghdr *)(void *)p;
1752 addrs = ifam->ifam_addrs;
1753 q = (char *)(ifam + 1);
1756 ifm = (struct if_msghdr *)(void *)p;
1757 addrs = ifm->ifm_addrs;
1758 q = (char *)(ifm + 1);
1760 case RTM_IFANNOUNCE:
1761 ifan = (struct if_announcemsghdr *)(void *)p;
1762 switch (ifan->ifan_what) {
1766 case IFAN_DEPARTURE:
1767 ifremove(ifan->ifan_index);
1773 rtm = (struct rt_msghdr *)(void *)p;
1774 if (rtm->rtm_version != RTM_VERSION) {
1775 trace(1, "unexpected rtmsg version %d "
1777 rtm->rtm_version, RTM_VERSION);
1781 * Only messages that use the struct rt_msghdr
1782 * format are allowed beyond this point.
1784 if (rtm->rtm_type > RTM_RESOLVE) {
1785 trace(1, "rtmsg type %d ignored\n",
1789 addrs = rtm->rtm_addrs;
1790 q = (char *)(rtm + 1);
1791 if (rtm->rtm_pid == pid) {
1793 trace(1, "rtmsg looped back to me, ignored\n");
1799 memset(&rta, 0, sizeof(rta));
1800 for (i = 0; i < RTAX_MAX; i++) {
1801 if (addrs & (1 << i)) {
1802 rta[i] = (struct sockaddr_in6 *)(void *)q;
1803 q += ROUNDUP(rta[i]->sin6_len);
1807 trace(1, "rtsock: %s (addrs=%x)\n",
1808 rttypes((struct rt_msghdr *)(void *)p), addrs);
1811 i < ((struct rt_msghdr *)(void *)p)->rtm_msglen;
1813 fprintf(stderr, "%02x ", p[i] & 0xff);
1814 if (i % 16 == 15) fprintf(stderr, "\n");
1816 fprintf(stderr, "\n");
1822 * We may be able to optimize by using ifm->ifm_index or
1823 * ifam->ifam_index. For simplicity we don't do that here.
1825 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1837 /* nothing to be done here */
1838 trace(1, "\tnothing to be done, ignored\n");
1843 if (rta[RTAX_DST] == NULL) {
1844 trace(1, "\tno destination, ignored\n");
1847 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1848 trace(1, "\taf mismatch, ignored\n");
1851 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1852 trace(1, "\tlinklocal destination, ignored\n");
1855 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1856 trace(1, "\tloopback destination, ignored\n");
1857 continue; /* Loopback */
1859 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1860 trace(1, "\tmulticast destination, ignored\n");
1866 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1874 /* should already be handled */
1875 fatal("rtrecv: never reach here");
1878 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1879 trace(1, "\tsome of dst/gw/netamsk are "
1880 "unavailable, ignored\n");
1883 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1884 mask.sin6_len = sizeof(mask);
1885 memset(&mask.sin6_addr, 0xff,
1886 sizeof(mask.sin6_addr));
1887 rta[RTAX_NETMASK] = &mask;
1888 } else if (!rta[RTAX_NETMASK]) {
1889 trace(1, "\tsome of dst/gw/netamsk are "
1890 "unavailable, ignored\n");
1893 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1894 rta[RTAX_NETMASK]) == 0) {
1895 rtable++; /*just to be sure*/
1900 trace(1, "\tnot supported yet, ignored\n");
1903 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1904 trace(1, "\tno netmask or ifa given, ignored\n");
1907 if (ifam->ifam_index < nindex2ifc)
1908 ifcp = index2ifc[ifam->ifam_index];
1912 trace(1, "\tinvalid ifam_index %d, ignored\n",
1916 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1924 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1926 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1927 if (ifcp->ifc_cflags & IFC_CHANGED) {
1928 if (ifrt(ifcp, 1)) {
1929 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1930 if (ifcp->ifc_index == ic->ifc_index)
1932 if (ic->ifc_flags & IFF_UP)
1933 ripsend(ic, &ic->ifc_ripsin,
1936 /* Reset the flag */
1937 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1938 rrt->rrt_rflags &= ~RRTF_CHANGED;
1941 ifcp->ifc_cflags &= ~IFC_CHANGED;
1946 trace(1, "rtsock: read routing table again\n");
1952 * remove specified route from the internal routing table.
1955 rt_del(const struct sockaddr_in6 *sdst,
1956 const struct sockaddr_in6 *sgw,
1957 const struct sockaddr_in6 *smask)
1959 const struct in6_addr *dst = NULL;
1960 const struct in6_addr *gw = NULL;
1962 struct netinfo6 ni6;
1963 struct riprt *rrt = NULL;
1966 if (sdst->sin6_family != AF_INET6) {
1967 trace(1, "\tother AF, ignored\n");
1970 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1971 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1972 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1973 trace(1, "\taddress %s not interesting, ignored\n",
1974 inet6_n2p(&sdst->sin6_addr));
1977 dst = &sdst->sin6_addr;
1978 if (sgw->sin6_family == AF_INET6) {
1980 gw = &sgw->sin6_addr;
1981 prefix = sin6mask2len(smask);
1982 } else if (sgw->sin6_family == AF_LINK) {
1984 * Interface route... a hard case. We need to get the prefix
1985 * length from the kernel, but we now are parsing rtmsg.
1986 * We'll purge matching routes from my list, then get the
1989 struct riprt *longest;
1990 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1993 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1994 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1996 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1998 || longest->rrt_info.rip6_plen <
1999 rrt->rrt_info.rip6_plen) {
2006 trace(1, "\tno matching interface route found\n");
2009 gw = &in6addr_loopback;
2010 prefix = rrt->rrt_info.rip6_plen;
2012 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2016 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2017 trace(1, "gw %s\n", inet6_n2p(gw));
2018 t_lifetime = time(NULL) - RIP_LIFETIME;
2019 /* age route for interface address */
2020 memset(&ni6, 0, sizeof(ni6));
2021 ni6.rip6_dest = *dst;
2022 ni6.rip6_plen = prefix;
2023 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2024 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2026 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2027 trace(1, "\tno route found\n");
2031 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2032 trace(1, "\tyou can delete static routes only\n");
2035 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2036 trace(1, "\tgw mismatch: %s <-> ",
2037 inet6_n2p(&rrt->rrt_gw));
2038 trace(1, "%s\n", inet6_n2p(gw));
2040 trace(1, "\troute found, age it\n");
2041 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2042 rrt->rrt_t = t_lifetime;
2043 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2050 * remove specified address from internal interface/routing table.
2053 rt_deladdr(struct ifc *ifcp,
2054 const struct sockaddr_in6 *sifa,
2055 const struct sockaddr_in6 *smask)
2057 const struct in6_addr *addr = NULL;
2059 struct ifac *ifac = NULL;
2060 struct netinfo6 ni6;
2061 struct riprt *rrt = NULL;
2065 if (sifa->sin6_family != AF_INET6) {
2066 trace(1, "\tother AF, ignored\n");
2069 addr = &sifa->sin6_addr;
2070 prefix = sin6mask2len(smask);
2072 trace(1, "\tdeleting %s/%d from %s\n",
2073 inet6_n2p(addr), prefix, ifcp->ifc_name);
2074 ifac = ifa_match(ifcp, addr, prefix);
2076 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2077 inet6_n2p(addr), prefix, ifcp->ifc_name);
2080 if (ifac->ifac_ifc != ifcp) {
2081 trace(1, "\taddress table corrupt: back pointer does not match "
2083 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2086 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2087 t_lifetime = time(NULL) - RIP_LIFETIME;
2088 /* age route for interface address */
2089 memset(&ni6, 0, sizeof(ni6));
2090 ni6.rip6_dest = ifac->ifac_addr;
2091 ni6.rip6_plen = ifac->ifac_plen;
2092 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2093 trace(1, "\tfind interface route %s/%d on %d\n",
2094 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2095 if ((rrt = rtsearch(&ni6)) != NULL) {
2096 struct in6_addr none;
2097 memset(&none, 0, sizeof(none));
2098 if (rrt->rrt_index == ifcp->ifc_index &&
2099 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2100 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2101 trace(1, "\troute found, age it\n");
2102 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2103 rrt->rrt_t = t_lifetime;
2104 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2108 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2109 inet6_n2p(&rrt->rrt_info.rip6_dest),
2110 rrt->rrt_info.rip6_plen,
2114 trace(1, "\tno interface route found\n");
2115 /* age route for p2p destination */
2116 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2117 memset(&ni6, 0, sizeof(ni6));
2118 ni6.rip6_dest = ifac->ifac_raddr;
2119 ni6.rip6_plen = 128;
2120 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2121 trace(1, "\tfind p2p route %s/%d on %d\n",
2122 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2124 if ((rrt = rtsearch(&ni6)) != NULL) {
2125 if (rrt->rrt_index == ifcp->ifc_index &&
2126 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2127 &ifac->ifac_addr)) {
2128 trace(1, "\troute found, age it\n");
2129 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2130 rrt->rrt_t = t_lifetime;
2131 rrt->rrt_info.rip6_metric =
2136 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2137 inet6_n2p(&rrt->rrt_info.rip6_dest),
2138 rrt->rrt_info.rip6_plen,
2142 trace(1, "\tno p2p route found\n");
2146 return ((updated) ? 0 : -1);
2150 * Get each interface address and put those interface routes to the route
2154 ifrt(struct ifc *ifcp, int again)
2157 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2158 struct netinfo6 *np;
2160 int need_trigger = 0;
2163 if (ifcp->ifc_flags & IFF_LOOPBACK)
2164 return 0; /* ignore loopback */
2167 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2168 ifrt_p2p(ifcp, again);
2172 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2173 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2175 trace(1, "route: %s on %s: "
2176 "skip linklocal interface address\n",
2177 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2181 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2183 trace(1, "route: %s: skip unspec interface address\n",
2188 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2190 trace(1, "route: %s: skip loopback address\n",
2195 if (ifcp->ifc_flags & IFF_UP) {
2196 if ((rrt = MALLOC(struct riprt)) == NULL)
2197 fatal("malloc: struct riprt");
2198 memset(rrt, 0, sizeof(*rrt));
2199 rrt->rrt_same = NULL;
2200 rrt->rrt_index = ifcp->ifc_index;
2201 rrt->rrt_t = 0; /* don't age */
2202 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2203 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2204 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2205 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2206 rrt->rrt_flags = RTF_HOST;
2207 rrt->rrt_rflags |= RRTF_CHANGED;
2208 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2209 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2210 rrt->rrt_gw = ifac->ifac_addr;
2211 np = &rrt->rrt_info;
2212 search_rrt = rtsearch(np);
2213 if (search_rrt != NULL) {
2214 if (search_rrt->rrt_info.rip6_metric <=
2215 rrt->rrt_info.rip6_metric) {
2216 /* Already have better route */
2218 trace(1, "route: %s/%d: "
2219 "already registered (%s)\n",
2220 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2226 TAILQ_REMOVE(&riprt_head, search_rrt, rrt_next);
2227 delroute(&search_rrt->rrt_info,
2228 &search_rrt->rrt_gw);
2231 /* Attach the route to the list */
2232 trace(1, "route: %s/%d: register route (%s)\n",
2233 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2235 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2236 addroute(rrt, &rrt->rrt_gw, ifcp);
2239 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2242 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2243 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2244 t_lifetime = time(NULL) - RIP_LIFETIME;
2245 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2246 loop_rrt->rrt_t = t_lifetime;
2247 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2248 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2258 return need_trigger;
2262 * there are couple of p2p interface routing models. "behavior" lets
2263 * you pick one. it looks that gated behavior fits best with BSDs,
2264 * since BSD kernels do not look at prefix length on p2p interfaces.
2267 ifrt_p2p(struct ifc *ifcp, int again)
2270 struct riprt *rrt, *orrt;
2271 struct netinfo6 *np;
2272 struct in6_addr addr, dest;
2273 int advert, ignore, i;
2274 #define P2PADVERT_NETWORK 1
2275 #define P2PADVERT_ADDR 2
2276 #define P2PADVERT_DEST 4
2277 #define P2PADVERT_MAX 4
2278 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2279 const char *category = "";
2282 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2283 addr = ifac->ifac_addr;
2284 dest = ifac->ifac_raddr;
2285 applyplen(&addr, ifac->ifac_plen);
2286 applyplen(&dest, ifac->ifac_plen);
2287 advert = ignore = 0;
2291 * honor addr/plen, just like normal shared medium
2292 * interface. this may cause trouble if you reuse
2293 * addr/plen on other interfaces.
2295 * advertise addr/plen.
2297 advert |= P2PADVERT_NETWORK;
2301 * prefixlen on p2p interface is meaningless.
2302 * advertise addr/128 and dest/128.
2304 * do not install network route to route6d routing
2305 * table (if we do, it would prevent route installation
2306 * for other p2p interface that shares addr/plen).
2308 * XXX what should we do if dest is ::? it will not
2309 * get announced anyways (see following filter),
2310 * but we need to think.
2312 advert |= P2PADVERT_ADDR;
2313 advert |= P2PADVERT_DEST;
2314 ignore |= P2PADVERT_NETWORK;
2318 * just for testing. actually the code is redundant
2319 * given the current p2p interface address assignment
2320 * rule for kame kernel.
2323 * A/n -> announce A/n
2324 * A B/n, A and B share prefix -> A/n (= B/n)
2325 * A B/n, do not share prefix -> A/128 and B/128
2326 * actually, A/64 and A B/128 are the only cases
2327 * permitted by the kernel:
2329 * A B/128 -> A/128 and B/128
2331 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2332 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2333 advert |= P2PADVERT_NETWORK;
2335 advert |= P2PADVERT_ADDR;
2336 advert |= P2PADVERT_DEST;
2337 ignore |= P2PADVERT_NETWORK;
2340 advert |= P2PADVERT_NETWORK;
2344 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2345 if ((ignore & i) != 0)
2347 if ((rrt = MALLOC(struct riprt)) == NULL) {
2348 fatal("malloc: struct riprt");
2351 memset(rrt, 0, sizeof(*rrt));
2352 rrt->rrt_same = NULL;
2353 rrt->rrt_index = ifcp->ifc_index;
2354 rrt->rrt_t = 0; /* don't age */
2356 case P2PADVERT_NETWORK:
2357 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2358 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2359 applyplen(&rrt->rrt_info.rip6_dest,
2361 category = "network";
2363 case P2PADVERT_ADDR:
2364 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2365 rrt->rrt_info.rip6_plen = 128;
2366 rrt->rrt_gw = in6addr_loopback;
2369 case P2PADVERT_DEST:
2370 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2371 rrt->rrt_info.rip6_plen = 128;
2372 rrt->rrt_gw = ifac->ifac_addr;
2376 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2377 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2379 trace(1, "route: %s: skip unspec/linklocal "
2380 "(%s on %s)\n", category, ifcp->ifc_name);
2385 if ((advert & i) == 0) {
2386 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2390 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2391 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2392 np = &rrt->rrt_info;
2393 orrt = rtsearch(np);
2395 /* Attach the route to the list */
2396 trace(1, "route: %s/%d: register route "
2398 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2399 category, ifcp->ifc_name, noadv);
2400 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2401 } else if (rrt->rrt_index != orrt->rrt_index ||
2402 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2404 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2405 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2408 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2409 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2410 category, ifcp->ifc_name, noadv);
2414 trace(1, "route: %s/%d: "
2415 "already registered (%s on %s%s)\n",
2416 inet6_n2p(&np->rip6_dest),
2417 np->rip6_plen, category,
2418 ifcp->ifc_name, noadv);
2424 #undef P2PADVERT_NETWORK
2425 #undef P2PADVERT_ADDR
2426 #undef P2PADVERT_DEST
2427 #undef P2PADVERT_MAX
2431 getifmtu(int ifindex)
2436 struct if_msghdr *ifm;
2443 mib[4] = NET_RT_IFLIST;
2445 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2446 fatal("sysctl estimate NET_RT_IFLIST");
2449 if ((buf = malloc(msize)) == NULL) {
2453 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2454 fatal("sysctl NET_RT_IFLIST");
2457 ifm = (struct if_msghdr *)(void *)buf;
2458 mtu = ifm->ifm_data.ifi_mtu;
2459 if (ifindex != ifm->ifm_index) {
2460 fatal("ifindex does not match with ifm_index");
2468 rttypes(struct rt_msghdr *rtm)
2470 #define RTTYPE(s, f) \
2472 if (rtm->rtm_type == (f)) \
2475 RTTYPE("ADD", RTM_ADD);
2476 RTTYPE("DELETE", RTM_DELETE);
2477 RTTYPE("CHANGE", RTM_CHANGE);
2478 RTTYPE("GET", RTM_GET);
2479 RTTYPE("LOSING", RTM_LOSING);
2480 RTTYPE("REDIRECT", RTM_REDIRECT);
2481 RTTYPE("MISS", RTM_MISS);
2482 RTTYPE("LOCK", RTM_LOCK);
2483 RTTYPE("NEWADDR", RTM_NEWADDR);
2484 RTTYPE("DELADDR", RTM_DELADDR);
2485 RTTYPE("IFINFO", RTM_IFINFO);
2487 RTTYPE("OIFINFO", RTM_OIFINFO);
2489 #ifdef RTM_IFANNOUNCE
2490 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2493 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2496 RTTYPE("DELMADDR", RTM_DELMADDR);
2503 rtflags(struct rt_msghdr *rtm)
2505 static char buf[BUFSIZ];
2508 * letter conflict should be okay. painful when *BSD diverges...
2510 strlcpy(buf, "", sizeof(buf));
2511 #define RTFLAG(s, f) \
2513 if (rtm->rtm_flags & (f)) \
2514 strlcat(buf, (s), sizeof(buf)); \
2516 RTFLAG("U", RTF_UP);
2517 RTFLAG("G", RTF_GATEWAY);
2518 RTFLAG("H", RTF_HOST);
2519 RTFLAG("R", RTF_REJECT);
2520 RTFLAG("D", RTF_DYNAMIC);
2521 RTFLAG("M", RTF_MODIFIED);
2522 RTFLAG("d", RTF_DONE);
2524 RTFLAG("m", RTF_MASK);
2527 RTFLAG("c", RTF_CLONED);
2529 RTFLAG("X", RTF_XRESOLVE);
2531 RTFLAG("L", RTF_LLINFO);
2533 RTFLAG("S", RTF_STATIC);
2534 RTFLAG("B", RTF_BLACKHOLE);
2536 RTFLAG("3", RTF_PROTO3);
2538 RTFLAG("2", RTF_PROTO2);
2539 RTFLAG("1", RTF_PROTO1);
2540 #ifdef RTF_BROADCAST
2541 RTFLAG("b", RTF_BROADCAST);
2544 RTFLAG("d", RTF_DEFAULT);
2546 #ifdef RTF_ISAROUTER
2547 RTFLAG("r", RTF_ISAROUTER);
2550 RTFLAG("T", RTF_TUNNEL);
2553 RTFLAG("A", RTF_AUTH);
2556 RTFLAG("E", RTF_CRYPT);
2565 static char buf[BUFSIZ];
2567 strlcpy(buf, "", sizeof(buf));
2568 #define IFFLAG(s, f) \
2570 if (flags & (f)) { \
2572 strlcat(buf, ",", sizeof(buf)); \
2573 strlcat(buf, (s), sizeof(buf)); \
2576 IFFLAG("UP", IFF_UP);
2577 IFFLAG("BROADCAST", IFF_BROADCAST);
2578 IFFLAG("DEBUG", IFF_DEBUG);
2579 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2580 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2581 #ifdef IFF_NOTRAILERS
2582 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2584 IFFLAG("RUNNING", IFF_RUNNING);
2585 IFFLAG("NOARP", IFF_NOARP);
2586 IFFLAG("PROMISC", IFF_PROMISC);
2587 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2588 IFFLAG("OACTIVE", IFF_OACTIVE);
2589 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2590 IFFLAG("LINK0", IFF_LINK0);
2591 IFFLAG("LINK1", IFF_LINK1);
2592 IFFLAG("LINK2", IFF_LINK2);
2593 IFFLAG("MULTICAST", IFF_MULTICAST);
2603 char *buf, *p, *lim;
2604 struct rt_msghdr *rtm;
2613 mib[3] = AF_INET6; /* Address family */
2614 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2615 mib[5] = 0; /* No flags */
2625 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2626 errmsg = "sysctl estimate";
2629 if ((buf = malloc(msize)) == NULL) {
2633 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2634 errmsg = "sysctl NET_RT_DUMP";
2637 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2639 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2642 } else if (1 < retry)
2643 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2646 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2647 rtm = (struct rt_msghdr *)(void *)p;
2648 rt_entry(rtm, again);
2654 rt_entry(struct rt_msghdr *rtm, int again)
2656 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2657 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2658 char *rtmp, *ifname = NULL;
2659 struct riprt *rrt, *orrt;
2660 struct netinfo6 *np;
2663 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2664 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2665 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2666 return; /* not interested in the link route */
2668 /* do not look at cloned routes */
2669 #ifdef RTF_WASCLONED
2670 if (rtm->rtm_flags & RTF_WASCLONED)
2674 if (rtm->rtm_flags & RTF_CLONED)
2677 /* XXX: Ignore connected routes. */
2678 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2681 * do not look at dynamic routes.
2682 * netbsd/openbsd cloned routes have UGHD.
2684 if (rtm->rtm_flags & RTF_DYNAMIC)
2686 rtmp = (char *)(rtm + 1);
2688 if ((rtm->rtm_addrs & RTA_DST) == 0)
2689 return; /* ignore routes without destination address */
2690 sin6_dst = (struct sockaddr_in6 *)(void *)rtmp;
2691 rtmp += ROUNDUP(sin6_dst->sin6_len);
2692 if (rtm->rtm_addrs & RTA_GATEWAY) {
2693 sin6_gw = (struct sockaddr_in6 *)(void *)rtmp;
2694 rtmp += ROUNDUP(sin6_gw->sin6_len);
2696 if (rtm->rtm_addrs & RTA_NETMASK) {
2697 sin6_mask = (struct sockaddr_in6 *)(void *)rtmp;
2698 rtmp += ROUNDUP(sin6_mask->sin6_len);
2700 if (rtm->rtm_addrs & RTA_GENMASK) {
2701 sin6_genmask = (struct sockaddr_in6 *)(void *)rtmp;
2702 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2704 if (rtm->rtm_addrs & RTA_IFP) {
2705 sin6_ifp = (struct sockaddr_in6 *)(void *)rtmp;
2706 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2710 if (sin6_dst->sin6_family != AF_INET6)
2712 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2713 return; /* Link-local */
2714 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2715 return; /* Loopback */
2716 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2719 if ((rrt = MALLOC(struct riprt)) == NULL) {
2720 fatal("malloc: struct riprt");
2723 memset(rrt, 0, sizeof(*rrt));
2724 np = &rrt->rrt_info;
2725 rrt->rrt_same = NULL;
2726 rrt->rrt_t = time(NULL);
2727 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2728 rrt->rrt_t = 0; /* Don't age static routes */
2729 if (rtm->rtm_flags & Pflag)
2730 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2731 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2732 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2734 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2735 if (np->rip6_metric < 1)
2736 np->rip6_metric = 1;
2737 rrt->rrt_flags = rtm->rtm_flags;
2738 np->rip6_dest = sin6_dst->sin6_addr;
2741 if (rtm->rtm_flags & RTF_HOST)
2742 np->rip6_plen = 128; /* Host route */
2744 np->rip6_plen = sin6mask2len(sin6_mask);
2748 orrt = rtsearch(np);
2749 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2752 trace(1, "route: %s/%d flags %s: already registered\n",
2753 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2761 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2763 if (sin6_gw->sin6_family == AF_INET6)
2764 rrt->rrt_gw = sin6_gw->sin6_addr;
2765 else if (sin6_gw->sin6_family == AF_LINK) {
2766 /* XXX in case ppp link? */
2767 rrt->rrt_gw = in6addr_loopback;
2769 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2771 trace(1, "route: %s/%d flags %s",
2772 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2773 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2776 ifindex = rtm->rtm_index;
2777 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2778 ifname = index2ifc[ifindex]->ifc_name;
2780 trace(1, " not configured\n");
2784 trace(1, " if %s sock %d", ifname, ifindex);
2785 rrt->rrt_index = ifindex;
2790 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2791 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2792 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2793 trace(0, "***** Gateway %s is not a link-local address.\n",
2794 inet6_n2p(&rrt->rrt_gw));
2795 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2796 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2797 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2800 /* Put it to the route list */
2801 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2802 /* replace route list */
2803 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2804 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2806 trace(1, "route: %s/%d flags %s: replace new route\n",
2807 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2811 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2815 addroute(struct riprt *rrt,
2816 const struct in6_addr *gw,
2819 struct netinfo6 *np;
2820 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2821 struct rt_msghdr *rtm;
2822 struct sockaddr_in6 *sin6;
2825 np = &rrt->rrt_info;
2826 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2827 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2828 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2829 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2830 np->rip6_metric - 1, buf2);
2832 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2833 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2834 np->rip6_metric - 1, buf2);
2838 memset(buf, 0, sizeof(buf));
2839 rtm = (struct rt_msghdr *)(void *)buf;
2840 rtm->rtm_type = RTM_ADD;
2841 rtm->rtm_version = RTM_VERSION;
2842 rtm->rtm_seq = ++seq;
2844 rtm->rtm_flags = rrt->rrt_flags;
2845 rtm->rtm_flags |= Qflag;
2846 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2847 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2848 rtm->rtm_inits = RTV_HOPCOUNT;
2849 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2851 sin6->sin6_len = sizeof(struct sockaddr_in6);
2852 sin6->sin6_family = AF_INET6;
2853 sin6->sin6_addr = np->rip6_dest;
2854 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2856 sin6->sin6_len = sizeof(struct sockaddr_in6);
2857 sin6->sin6_family = AF_INET6;
2858 sin6->sin6_addr = *gw;
2859 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2860 sin6->sin6_scope_id = ifcp->ifc_index;
2861 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2863 sin6->sin6_len = sizeof(struct sockaddr_in6);
2864 sin6->sin6_family = AF_INET6;
2865 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2866 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2868 len = (char *)sin6 - (char *)buf;
2869 rtm->rtm_msglen = len;
2870 if (write(rtsock, buf, len) > 0)
2873 if (errno == EEXIST) {
2874 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2875 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2877 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2878 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2880 trace(0, "Can not write to rtsock (addroute): %s\n",
2883 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2890 delroute(struct netinfo6 *np, struct in6_addr *gw)
2892 u_char buf[BUFSIZ], buf2[BUFSIZ];
2893 struct rt_msghdr *rtm;
2894 struct sockaddr_in6 *sin6;
2897 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2898 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2899 np->rip6_plen, buf2);
2901 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2902 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2906 memset(buf, 0, sizeof(buf));
2907 rtm = (struct rt_msghdr *)(void *)buf;
2908 rtm->rtm_type = RTM_DELETE;
2909 rtm->rtm_version = RTM_VERSION;
2910 rtm->rtm_seq = ++seq;
2912 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2913 rtm->rtm_flags |= Qflag;
2914 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2915 rtm->rtm_flags |= RTF_HOST;
2916 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2917 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2919 sin6->sin6_len = sizeof(struct sockaddr_in6);
2920 sin6->sin6_family = AF_INET6;
2921 sin6->sin6_addr = np->rip6_dest;
2922 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2924 sin6->sin6_len = sizeof(struct sockaddr_in6);
2925 sin6->sin6_family = AF_INET6;
2926 sin6->sin6_addr = *gw;
2927 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2929 sin6->sin6_len = sizeof(struct sockaddr_in6);
2930 sin6->sin6_family = AF_INET6;
2931 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2932 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2934 len = (char *)sin6 - (char *)buf;
2935 rtm->rtm_msglen = len;
2936 if (write(rtsock, buf, len) >= 0)
2939 if (errno == ESRCH) {
2940 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2941 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2943 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2944 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2946 trace(0, "Can not write to rtsock (delroute): %s\n",
2949 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2956 static struct in6_addr *
2957 getroute(struct netinfo6 *np, struct in6_addr *gw)
2962 struct rt_msghdr *rtm;
2963 struct sockaddr_in6 *sin6;
2965 rtm = (struct rt_msghdr *)(void *)buf;
2966 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2967 memset(rtm, 0, len);
2968 rtm->rtm_type = RTM_GET;
2969 rtm->rtm_version = RTM_VERSION;
2971 rtm->rtm_seq = myseq;
2972 rtm->rtm_addrs = RTA_DST;
2973 rtm->rtm_msglen = len;
2974 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2975 sin6->sin6_len = sizeof(struct sockaddr_in6);
2976 sin6->sin6_family = AF_INET6;
2977 sin6->sin6_addr = np->rip6_dest;
2978 if (write(rtsock, buf, len) < 0) {
2979 if (errno == ESRCH) /* No such route found */
2981 perror("write to rtsock");
2985 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2986 perror("read from rtsock");
2989 rtm = (struct rt_msghdr *)(void *)buf;
2990 } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != myseq ||
2991 rtm->rtm_pid != pid);
2992 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2993 if (rtm->rtm_addrs & RTA_DST) {
2994 sin6 = (struct sockaddr_in6 *)(void *)
2995 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2997 if (rtm->rtm_addrs & RTA_GATEWAY) {
2998 *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.
3296 static struct ifac *
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.
3317 static struct riprt *
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;
3367 static const u_char plent[8] = {
3368 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3372 applyplen(struct in6_addr *ia, int plen)
3378 for (i = 0; i < 16; i++) {
3387 static const int pl2m[9] = {
3388 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3391 static struct in6_addr *
3394 static struct in6_addr ia;
3398 memset(&ia, 0, sizeof(struct in6_addr));
3400 for (i = 0; i < 16; i++, p++, n -= 8) {
3414 int len = strlen(p) + 1;
3415 char *q = (char *)malloc(len);
3429 static char buf[BUFSIZ];
3434 if ((tm = localtime(&t)) == 0) {
3438 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3443 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3446 ripinterval(int timer)
3450 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3451 nextalarm = time(NULL) + interval;
3462 t = (int)(RIP_TRIG_INT6_MIN +
3463 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3464 sup_trig_update = time(NULL) + t;
3470 fatal(const char *fmt, ...)
3476 vsnprintf(buf, sizeof(buf), fmt, ap);
3480 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3482 syslog(LOG_ERR, "%s", buf);
3487 tracet(int level, const char *fmt, ...)
3491 if (level <= dflag) {
3493 fprintf(stderr, "%s: ", hms());
3494 vfprintf(stderr, fmt, ap);
3500 vsyslog(LOG_DEBUG, fmt, ap);
3502 vsyslog(LOG_WARNING, fmt, ap);
3508 trace(int level, const char *fmt, ...)
3512 if (level <= dflag) {
3514 vfprintf(stderr, fmt, ap);
3520 vsyslog(LOG_DEBUG, fmt, ap);
3522 vsyslog(LOG_WARNING, fmt, ap);
3528 ifc_find(char *name)
3532 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3533 if (strcmp(name, ifcp->ifc_name) == 0)
3540 iff_find(struct ifc *ifcp, int type)
3544 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3545 if (type == IFIL_TYPE_ANY ||
3546 type == iffp->iff_type)
3554 setindex2ifc(int idx, struct ifc *ifcp)
3560 nindex2ifc = 5; /*initial guess*/
3561 index2ifc = (struct ifc **)
3562 malloc(sizeof(*index2ifc) * nindex2ifc);
3563 if (index2ifc == NULL) {
3567 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3570 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3573 p = (struct ifc **)realloc(index2ifc,
3574 sizeof(*index2ifc) * nsize);
3579 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3583 index2ifc[idx] = ifcp;