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/socket.h>
43 #include <sys/sysctl.h>
45 #include <arpa/inet.h>
47 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet/udp.h>
73 #define RT_DUMP_MAXRETRY 15
76 #define INIT_INTERVAL6 6
78 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
81 /* alignment constraint for routing socket */
83 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
84 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
86 struct ifc { /* Configuration of an interface */
87 TAILQ_ENTRY(ifc) ifc_next;
89 char ifc_name[IFNAMSIZ]; /* if name */
90 int ifc_index; /* if index */
91 int ifc_mtu; /* if mtu */
92 int ifc_metric; /* if metric */
93 u_int ifc_flags; /* flags */
94 short ifc_cflags; /* IFC_XXX */
95 struct in6_addr ifc_mylladdr; /* my link-local address */
96 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
97 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
98 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
99 int ifc_joined; /* joined to ff02::9 */
101 static TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
103 struct ifac { /* Adddress associated to an interface */
104 TAILQ_ENTRY(ifac) ifac_next;
106 struct ifc *ifac_ifc; /* back pointer */
107 struct in6_addr ifac_addr; /* address */
108 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
109 int ifac_scope_id; /* scope id */
110 int ifac_plen; /* prefix length */
113 struct iff { /* Filters for an interface */
114 TAILQ_ENTRY(iff) iff_next;
117 struct in6_addr iff_addr;
121 static struct ifc **index2ifc;
122 static unsigned int nindex2ifc;
123 static struct ifc *loopifcp = NULL; /* pointing to loopback */
125 static struct pollfd set[2];
127 static fd_set *sockvecp; /* vector to select() for receiving */
128 static fd_set *recvecp;
130 static int maxfd; /* maximum fd for select() */
132 static int rtsock; /* the routing socket */
133 static int ripsock; /* socket to send/receive RIP datagram */
135 static struct rip6 *ripbuf; /* packet buffer for sending */
138 * Maintain the routes in a linked list. When the number of the routes
139 * grows, somebody would like to introduce a hash based or a radix tree
140 * based structure. I believe the number of routes handled by RIP is
141 * limited and I don't have to manage a complex data structure, however.
143 * One of the major drawbacks of the linear linked list is the difficulty
144 * of representing the relationship between a couple of routes. This may
145 * be a significant problem when we have to support route aggregation with
146 * suppressing the specifics covered by the aggregate.
150 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
152 struct riprt *rrt_same; /* same destination - future use */
153 struct netinfo6 rrt_info; /* network info */
154 struct in6_addr rrt_gw; /* gateway */
155 u_long rrt_flags; /* kernel routing table flags */
156 u_long rrt_rflags; /* route6d routing table flags */
157 time_t rrt_t; /* when the route validated */
158 int rrt_index; /* ifindex from which this route got */
160 static TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
162 static int dflag = 0; /* debug flag */
163 static int qflag = 0; /* quiet flag */
164 static int nflag = 0; /* don't update kernel routing table */
165 static int aflag = 0; /* age out even the statically defined routes */
166 static int hflag = 0; /* don't split horizon */
167 static int lflag = 0; /* exchange site local routes */
168 static int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
169 static int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
170 static int sflag = 0; /* announce static routes w/ split horizon */
171 static int Sflag = 0; /* announce static routes to every interface */
172 static unsigned long routetag = 0; /* route tag attached on originating case */
174 static char *filter[MAXFILTER];
175 static int filtertype[MAXFILTER];
176 static int nfilter = 0;
180 static struct sockaddr_storage ripsin;
182 static int interval = 1;
183 static time_t nextalarm = 0;
185 static time_t sup_trig_update = 0;
188 static FILE *rtlog = NULL;
190 static int logopened = 0;
194 static volatile sig_atomic_t seenalrm;
195 static volatile sig_atomic_t seenquit;
196 static volatile sig_atomic_t seenusr1;
198 #define RRTF_AGGREGATE 0x08000000
199 #define RRTF_NOADVERTISE 0x10000000
200 #define RRTF_NH_NOT_LLADDR 0x20000000
201 #define RRTF_SENDANYWAY 0x40000000
202 #define RRTF_CHANGED 0x80000000
204 static void sighandler(int);
205 static void ripalarm(void);
206 static void riprecv(void);
207 static void ripsend(struct ifc *, struct sockaddr_in6 *, int);
208 static int out_filter(struct riprt *, struct ifc *);
209 static void init(void);
210 static void ifconfig(void);
211 static int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
212 static void rtrecv(void);
213 static int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
214 const struct sockaddr_in6 *);
215 static int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
216 const struct sockaddr_in6 *);
217 static void filterconfig(void);
218 static int getifmtu(int);
219 static const char *rttypes(struct rt_msghdr *);
220 static const char *rtflags(struct rt_msghdr *);
221 static const char *ifflags(int);
222 static int ifrt(struct ifc *, int);
223 static void ifrt_p2p(struct ifc *, int);
224 static void applyplen(struct in6_addr *, int);
225 static void ifrtdump(int);
226 static void ifdump(int);
227 static void ifdump0(FILE *, const struct ifc *);
228 static void ifremove(int);
229 static void rtdump(int);
230 static void rt_entry(struct rt_msghdr *, int);
231 static void rtdexit(void);
232 static void riprequest(struct ifc *, struct netinfo6 *, int,
233 struct sockaddr_in6 *);
234 static void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
235 static void sendrequest(struct ifc *);
236 static int sin6mask2len(const struct sockaddr_in6 *);
237 static int mask2len(const struct in6_addr *, int);
238 static int sendpacket(struct sockaddr_in6 *, int);
239 static int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
240 static int delroute(struct netinfo6 *, struct in6_addr *);
242 static struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
244 static void krtread(int);
245 static int tobeadv(struct riprt *, struct ifc *);
246 static char *allocopy(char *);
247 static char *hms(void);
248 static const char *inet6_n2p(const struct in6_addr *);
249 static struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
250 static struct in6_addr *plen2mask(int);
251 static struct riprt *rtsearch(struct netinfo6 *);
252 static int ripinterval(int);
254 static time_t ripsuptrig(void);
256 static void fatal(const char *, ...)
257 __attribute__((__format__(__printf__, 1, 2)));
258 static void trace(int, const char *, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260 static void tracet(int, const char *, ...)
261 __attribute__((__format__(__printf__, 2, 3)));
262 static struct ifc *ifc_find(char *);
263 static struct iff *iff_find(struct ifc *, int);
264 static void setindex2ifc(int, struct ifc *);
266 #define MALLOC(type) ((type *)malloc(sizeof(type)))
268 #define IFIL_TYPE_ANY 0x0
269 #define IFIL_TYPE_A 'A'
270 #define IFIL_TYPE_N 'N'
271 #define IFIL_TYPE_T 'T'
272 #define IFIL_TYPE_O 'O'
273 #define IFIL_TYPE_L 'L'
276 main(int argc, char *argv[])
282 sigset_t mask, omask;
283 const char *pidfile = ROUTE6D_PID;
288 progname = strrchr(*argv, '/');
295 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
302 if (nfilter >= MAXFILTER) {
303 fatal("Exceeds MAXFILTER");
306 filtertype[nfilter] = ch;
307 filter[nfilter++] = allocopy(optarg);
311 routetag = strtoul(optarg, &ep, 0);
312 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
313 fatal("invalid route tag");
322 proto = strtoul(optarg, &ep, 0);
323 if (!ep || *ep != '\0' || 3 < proto) {
324 fatal("invalid P flag");
338 proto = strtoul(optarg, &ep, 0);
339 if (!ep || *ep != '\0' || 3 < proto) {
340 fatal("invalid Q flag");
353 if ((rtlog = fopen(optarg, "w")) == NULL) {
354 fatal("Can not write to routelog");
358 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
359 FLAG('a', aflag, 1); break;
360 FLAG('d', dflag, 1); break;
361 FLAG('D', dflag, 2); break;
362 FLAG('h', hflag, 1); break;
363 FLAG('l', lflag, 1); break;
364 FLAG('n', nflag, 1); break;
365 FLAG('q', qflag, 1); break;
366 FLAG('s', sflag, 1); break;
367 FLAG('S', Sflag, 1); break;
370 fatal("Invalid option specified, terminating");
377 fatal("bogus extra arguments");
383 fprintf(stderr, "No kernel update is allowed\n");
387 if (daemon(0, 0) < 0) {
393 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
396 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
398 memset(ripbuf, 0, RIP6_MAXMTU);
399 ripbuf->rip6_cmd = RIP6_RESPONSE;
400 ripbuf->rip6_vers = RIP6_VERSION;
401 ripbuf->rip6_res1[0] = 0;
402 ripbuf->rip6_res1[1] = 0;
406 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
407 if (ifcp->ifc_index < 0) {
408 fprintf(stderr, "No ifindex found at %s "
409 "(no link-local address?)\n", ifcp->ifc_name);
415 if (loopifcp == NULL) {
416 fatal("No loopback found");
419 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
428 if ((pidfh = fopen(pidfile, "w")) != NULL) {
429 fprintf(pidfh, "%d\n", pid);
433 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
437 memset(ripbuf, 0, RIP6_MAXMTU);
438 ripbuf->rip6_cmd = RIP6_RESPONSE;
439 ripbuf->rip6_vers = RIP6_VERSION;
440 ripbuf->rip6_res1[0] = 0;
441 ripbuf->rip6_res1[1] = 0;
443 if (signal(SIGALRM, sighandler) == SIG_ERR ||
444 signal(SIGQUIT, sighandler) == SIG_ERR ||
445 signal(SIGTERM, sighandler) == SIG_ERR ||
446 signal(SIGUSR1, sighandler) == SIG_ERR ||
447 signal(SIGHUP, sighandler) == SIG_ERR ||
448 signal(SIGINT, sighandler) == SIG_ERR) {
453 * To avoid rip packet congestion (not on a cable but in this
454 * process), wait for a moment to send the first RIP6_RESPONSE
457 alarm(ripinterval(INIT_INTERVAL6));
459 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
460 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
462 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
466 syslog(LOG_INFO, "**** Started ****");
468 sigaddset(&mask, SIGALRM);
487 switch (poll(set, 2, INFTIM))
489 memcpy(recvecp, sockvecp, fdmasks);
490 switch (select(maxfd + 1, recvecp, 0, 0, 0))
494 if (errno != EINTR) {
503 if (set[0].revents & POLLIN)
505 if (FD_ISSET(ripsock, recvecp))
508 sigprocmask(SIG_BLOCK, &mask, &omask);
510 sigprocmask(SIG_SETMASK, &omask, NULL);
513 if (set[1].revents & POLLIN)
515 if (FD_ISSET(rtsock, recvecp))
518 sigprocmask(SIG_BLOCK, &mask, &omask);
520 sigprocmask(SIG_SETMASK, &omask, NULL);
527 sighandler(int signo)
547 * gracefully exits after resetting sockopts.
556 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
557 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
558 delroute(&rrt->rrt_info, &rrt->rrt_gw);
563 syslog(LOG_INFO, "**** Terminated ****");
569 * Called periodically:
570 * 1. age out the learned route. remove it if necessary.
571 * 2. submit RIP6_RESPONSE packets.
572 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
573 * to invoke this function in every 1 or 5 or 10 seconds only to age the
574 * routes more precisely.
581 struct riprt *rrt, *rrt_tmp;
582 time_t t_lifetime, t_holddown;
584 /* age the RIP routes */
585 t_lifetime = time(NULL) - RIP_LIFETIME;
586 t_holddown = t_lifetime - RIP_HOLDDOWN;
587 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
590 else if (rrt->rrt_t < t_holddown) {
591 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
592 delroute(&rrt->rrt_info, &rrt->rrt_gw);
594 } else if (rrt->rrt_t < t_lifetime)
595 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
598 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
599 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
600 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
602 alarm(ripinterval(SUPPLY_INTERVAL6));
609 const int int0 = 0, int1 = 1, int255 = 255;
610 struct addrinfo hints, *res;
611 char port[NI_MAXSERV];
613 TAILQ_INIT(&ifc_head);
614 nindex2ifc = 0; /*initial guess*/
616 snprintf(port, sizeof(port), "%u", RIP6_PORT);
618 memset(&hints, 0, sizeof(hints));
619 hints.ai_family = PF_INET6;
620 hints.ai_socktype = SOCK_DGRAM;
621 hints.ai_protocol = IPPROTO_UDP;
622 hints.ai_flags = AI_PASSIVE;
623 error = getaddrinfo(NULL, port, &hints, &res);
625 fatal("%s", gai_strerror(error));
629 fatal(":: resolved to multiple address");
633 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
639 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
640 &int1, sizeof(int1)) < 0) {
641 fatal("rip IPV6_V6ONLY");
645 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
649 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
650 &int255, sizeof(int255)) < 0) {
651 fatal("rip IPV6_MULTICAST_HOPS");
654 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
655 &int0, sizeof(int0)) < 0) {
656 fatal("rip IPV6_MULTICAST_LOOP");
660 #ifdef IPV6_RECVPKTINFO
661 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
662 &int1, sizeof(int1)) < 0) {
663 fatal("rip IPV6_RECVPKTINFO");
666 #else /* old adv. API */
667 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
668 &int1, sizeof(int1)) < 0) {
669 fatal("rip IPV6_PKTINFO");
674 #ifdef IPV6_RECVPKTINFO
675 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
676 &int1, sizeof(int1)) < 0) {
677 fatal("rip IPV6_RECVHOPLIMIT");
680 #else /* old adv. API */
681 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
682 &int1, sizeof(int1)) < 0) {
683 fatal("rip IPV6_HOPLIMIT");
689 memset(&hints, 0, sizeof(hints));
690 hints.ai_family = PF_INET6;
691 hints.ai_socktype = SOCK_DGRAM;
692 hints.ai_protocol = IPPROTO_UDP;
693 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
695 fatal("%s", gai_strerror(error));
699 fatal("%s resolved to multiple address", RIP6_DEST);
702 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
707 set[0].events = POLLIN;
713 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
714 fatal("route socket");
719 set[1].events = POLLIN;
728 rtsock = -1; /*just for safety */
733 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
734 if ((sockvecp = malloc(fdmasks)) == NULL) {
738 if ((recvecp = malloc(fdmasks)) == NULL) {
742 memset(sockvecp, 0, fdmasks);
743 FD_SET(ripsock, sockvecp);
745 FD_SET(rtsock, sockvecp);
750 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
753 * ripflush flushes the rip datagram stored in the rip buffer
756 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
762 tracet(1, "Send(%s): info(%d) to %s.%d\n",
764 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
766 tracet(1, "Send: info(%d) to %s.%d\n",
767 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
769 np = ripbuf->rip6_nets;
770 for (i = 0; i < nrt; i++, np++) {
771 if (np->rip6_metric == NEXTHOP_METRIC) {
772 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
773 trace(2, " NextHop reset");
775 trace(2, " NextHop %s",
776 inet6_n2p(&np->rip6_dest));
779 trace(2, " %s/%d[%d]",
780 inet6_n2p(&np->rip6_dest),
781 np->rip6_plen, np->rip6_metric);
784 trace(2, " tag=0x%04x",
785 ntohs(np->rip6_tag) & 0xffff);
790 error = sendpacket(sin6, RIPSIZE(nrt));
791 if (error == EAFNOSUPPORT) {
792 /* Protocol not supported */
794 tracet(1, "Could not send info to %s (%s): "
797 inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
798 /* As if down for AF_INET6 */
799 ifcp->ifc_flags &= ~IFF_UP;
801 tracet(1, "Could not send info to %s\n",
802 inet6_n2p(&sin6->sin6_addr));
808 * Generate RIP6_RESPONSE packets and send them.
811 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
814 struct in6_addr *nh; /* next hop */
824 * Request from non-link local address is not
825 * a regular route6d update.
827 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
828 sizeof(struct udphdr) -
829 sizeof(struct rip6) + sizeof(struct netinfo6)) /
830 sizeof(struct netinfo6);
833 np = ripbuf->rip6_nets;
834 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
835 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
837 /* Put the route to the buffer */
841 ripflush(NULL, sin6, nrt, np);
844 np = ripbuf->rip6_nets;
847 if (nrt) /* Send last packet */
848 ripflush(NULL, sin6, nrt, np);
852 if ((flag & RRTF_SENDANYWAY) == 0 &&
853 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
857 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
860 /* -T: generate default route only */
861 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
862 struct netinfo6 rrt_info;
863 memset(&rrt_info, 0, sizeof(struct netinfo6));
864 rrt_info.rip6_dest = in6addr_any;
865 rrt_info.rip6_plen = 0;
866 rrt_info.rip6_metric = 1;
867 rrt_info.rip6_metric += ifcp->ifc_metric;
868 rrt_info.rip6_tag = htons(routetag & 0xffff);
869 np = ripbuf->rip6_nets;
872 ripflush(ifcp, sin6, nrt, np);
876 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
877 sizeof(struct udphdr) -
878 sizeof(struct rip6) + sizeof(struct netinfo6)) /
879 sizeof(struct netinfo6);
881 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
882 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
883 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
886 /* Need to check filter here */
887 if (out_filter(rrt, ifcp) == 0)
890 /* Check split horizon and other conditions */
891 if (tobeadv(rrt, ifcp) == 0)
894 /* Only considers the routes with flag if specified */
895 if ((flag & RRTF_CHANGED) &&
896 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
900 if (rrt->rrt_index == ifcp->ifc_index &&
901 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
902 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
903 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
904 if (nrt == maxrte - 2) {
905 ripflush(ifcp, sin6, nrt, np);
908 np = ripbuf->rip6_nets;
911 np->rip6_dest = rrt->rrt_gw;
914 np->rip6_metric = NEXTHOP_METRIC;
918 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
919 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
920 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
922 if (nrt == maxrte - 2) {
923 ripflush(ifcp, sin6, nrt, np);
926 np = ripbuf->rip6_nets;
928 memset(np, 0, sizeof(struct netinfo6));
929 np->rip6_metric = NEXTHOP_METRIC;
934 /* Put the route to the buffer */
938 ripflush(ifcp, sin6, nrt, np);
941 np = ripbuf->rip6_nets;
944 if (nrt) /* Send last packet */
945 ripflush(ifcp, sin6, nrt, np);
949 * outbound filter logic, per-route/interface.
952 out_filter(struct riprt *rrt, struct ifc *ifcp)
959 * -A: filter out less specific routes, if we have aggregated
962 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
963 if (iffp->iff_type != 'A')
965 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
967 ia = rrt->rrt_info.rip6_dest;
968 applyplen(&ia, iffp->iff_plen);
969 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
974 * if it is an aggregated route, advertise it only to the
975 * interfaces specified on -A.
977 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
979 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
980 if (iffp->iff_type != 'A')
982 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
983 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
994 * -O: advertise only if prefix matches the configured prefix.
996 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
998 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
999 if (iffp->iff_type != 'O')
1001 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1003 ia = rrt->rrt_info.rip6_dest;
1004 applyplen(&ia, iffp->iff_plen);
1005 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1014 /* the prefix should be advertised */
1019 * Determine if the route is to be advertised on the specified interface.
1020 * It checks options specified in the arguments and the split horizon rule.
1023 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1026 /* Special care for static routes */
1027 if (rrt->rrt_flags & RTF_STATIC) {
1028 /* XXX don't advertise reject/blackhole routes */
1029 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1032 if (Sflag) /* Yes, advertise it anyway */
1034 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1038 /* Regular split horizon */
1039 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1045 * Send a rip packet actually.
1048 sendpacket(struct sockaddr_in6 *sin6, int len)
1052 struct iovec iov[2];
1053 struct in6_pktinfo *pi;
1054 u_char cmsgbuf[256];
1056 struct sockaddr_in6 sincopy;
1058 /* do not overwrite the given sin */
1062 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1063 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1064 idx = sin6->sin6_scope_id;
1068 m.msg_name = (caddr_t)sin6;
1069 m.msg_namelen = sizeof(*sin6);
1070 iov[0].iov_base = (caddr_t)ripbuf;
1071 iov[0].iov_len = len;
1076 m.msg_control = NULL;
1077 m.msg_controllen = 0;
1079 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1080 cm = (struct cmsghdr *)(void *)cmsgbuf;
1081 m.msg_control = (caddr_t)cm;
1082 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1084 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1085 cm->cmsg_level = IPPROTO_IPV6;
1086 cm->cmsg_type = IPV6_PKTINFO;
1087 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1088 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1089 pi->ipi6_ifindex = idx;
1092 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1093 trace(1, "sendmsg: %s\n", strerror(errno));
1101 * Receive and process RIP packets. Update the routes/kernel forwarding
1102 * table if necessary.
1107 struct ifc *ifcp, *ic;
1108 struct sockaddr_in6 fsock;
1109 struct in6_addr nh; /* next hop */
1111 struct netinfo6 *np, *nq;
1114 unsigned int need_trigger, idx;
1115 char buf[4 * RIP6_MAXMTU];
1119 struct iovec iov[2];
1120 u_char cmsgbuf[256];
1121 struct in6_pktinfo *pi = NULL;
1126 time_t t_half_lifetime;
1130 m.msg_name = (caddr_t)&fsock;
1131 m.msg_namelen = sizeof(fsock);
1132 iov[0].iov_base = (caddr_t)buf;
1133 iov[0].iov_len = sizeof(buf);
1136 cm = (struct cmsghdr *)(void *)cmsgbuf;
1137 m.msg_control = (caddr_t)cm;
1138 m.msg_controllen = sizeof(cmsgbuf);
1140 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1145 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1147 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1148 if (cm->cmsg_level != IPPROTO_IPV6)
1150 switch (cm->cmsg_type) {
1152 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1154 "invalid cmsg length for IPV6_PKTINFO\n");
1157 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1158 idx = pi->ipi6_ifindex;
1161 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1163 "invalid cmsg length for IPV6_HOPLIMIT\n");
1166 hlimp = (int *)(void *)CMSG_DATA(cm);
1171 if ((size_t)len < sizeof(struct rip6)) {
1172 trace(1, "Packet too short\n");
1176 if (pi == NULL || hlimp == NULL) {
1178 * This can happen when the kernel failed to allocate memory
1179 * for the ancillary data. Although we might be able to handle
1180 * some cases without this info, those are minor and not so
1181 * important, so it's better to discard the packet for safer
1184 trace(1, "IPv6 packet information cannot be retrieved\n");
1188 nh = fsock.sin6_addr;
1189 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1190 sizeof(struct netinfo6);
1191 rp = (struct rip6 *)(void *)buf;
1194 if (rp->rip6_vers != RIP6_VERSION) {
1195 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1198 if (rp->rip6_cmd == RIP6_REQUEST) {
1199 if (idx && idx < nindex2ifc) {
1200 ifcp = index2ifc[idx];
1201 riprequest(ifcp, np, nn, &fsock);
1203 riprequest(NULL, np, nn, &fsock);
1208 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1209 trace(1, "Response from non-ll addr: %s\n",
1210 inet6_n2p(&fsock.sin6_addr));
1211 return; /* Ignore packets from non-link-local addr */
1213 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1214 trace(1, "Response from non-rip port from %s\n",
1215 inet6_n2p(&fsock.sin6_addr));
1218 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1220 "Response packet with a smaller hop limit (%d) from %s\n",
1221 *hlimp, inet6_n2p(&fsock.sin6_addr));
1225 * Further validation: since this program does not send off-link
1226 * requests, an incoming response must always come from an on-link
1227 * node. Although this is normally ensured by the source address
1228 * check above, it may not 100% be safe because there are router
1229 * implementations that (invalidly) allow a packet with a link-local
1230 * source address to be forwarded to a different link.
1231 * So we also check whether the destination address is a link-local
1232 * address or the hop limit is 255. Note that RFC2080 does not require
1233 * the specific hop limit for a unicast response, so we cannot assume
1236 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1238 "Response packet possibly from an off-link node: "
1239 "from %s to %s hlim=%d\n",
1240 inet6_n2p(&fsock.sin6_addr),
1241 inet6_n2p(&pi->ipi6_addr), *hlimp);
1245 idx = fsock.sin6_scope_id;
1246 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1248 trace(1, "Packets to unknown interface index %d\n", idx);
1249 return; /* Ignore it */
1251 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1252 return; /* The packet is from me; ignore */
1253 if (rp->rip6_cmd != RIP6_RESPONSE) {
1254 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1259 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1262 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1263 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1266 t_half_lifetime = t - (RIP_LIFETIME/2);
1267 for (; nn; nn--, np++) {
1268 if (np->rip6_metric == NEXTHOP_METRIC) {
1269 /* modify neighbor address */
1270 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1272 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1273 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1274 nh = fsock.sin6_addr;
1275 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1277 nh = fsock.sin6_addr;
1278 trace(1, "\tInvalid Nexthop: %s\n",
1279 inet6_n2p(&np->rip6_dest));
1283 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1284 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1285 inet6_n2p(&np->rip6_dest),
1286 np->rip6_plen, np->rip6_metric);
1289 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1290 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1291 inet6_n2p(&np->rip6_dest),
1292 np->rip6_plen, np->rip6_metric);
1295 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1296 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1297 inet6_n2p(&np->rip6_dest),
1298 np->rip6_plen, np->rip6_metric);
1301 /* may need to pass sitelocal prefix in some case, however*/
1302 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1303 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1304 inet6_n2p(&np->rip6_dest),
1305 np->rip6_plen, np->rip6_metric);
1308 trace(2, "\tnetinfo6: %s/%d [%d]",
1309 inet6_n2p(&np->rip6_dest),
1310 np->rip6_plen, np->rip6_metric);
1312 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1315 applyplen(&ia, np->rip6_plen);
1316 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1317 trace(2, " [junk outside prefix]");
1321 * -L: listen only if the prefix matches the configuration
1323 ok = 1; /* if there's no L filter, it is ok */
1324 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1325 if (iffp->iff_type != IFIL_TYPE_L)
1328 if (np->rip6_plen < iffp->iff_plen)
1330 /* special rule: ::/0 means default, not "in /0" */
1331 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1334 applyplen(&ia, iffp->iff_plen);
1335 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1341 trace(2, " (filtered)\n");
1347 np->rip6_metric += ifcp->ifc_metric;
1348 if (np->rip6_metric > HOPCNT_INFINITY6)
1349 np->rip6_metric = HOPCNT_INFINITY6;
1351 applyplen(&np->rip6_dest, np->rip6_plen);
1352 if ((rrt = rtsearch(np)) != NULL) {
1353 if (rrt->rrt_t == 0)
1354 continue; /* Intf route has priority */
1355 nq = &rrt->rrt_info;
1356 if (nq->rip6_metric > np->rip6_metric) {
1357 if (rrt->rrt_index == ifcp->ifc_index &&
1358 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1359 /* Small metric from the same gateway */
1360 nq->rip6_metric = np->rip6_metric;
1362 /* Better route found */
1363 rrt->rrt_index = ifcp->ifc_index;
1364 /* Update routing table */
1365 delroute(nq, &rrt->rrt_gw);
1368 addroute(rrt, &nh, ifcp);
1370 rrt->rrt_rflags |= RRTF_CHANGED;
1373 } else if (nq->rip6_metric < np->rip6_metric &&
1374 rrt->rrt_index == ifcp->ifc_index &&
1375 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1376 /* Got worse route from same gw */
1377 nq->rip6_metric = np->rip6_metric;
1379 rrt->rrt_rflags |= RRTF_CHANGED;
1381 } else if (nq->rip6_metric == np->rip6_metric &&
1382 np->rip6_metric < HOPCNT_INFINITY6) {
1383 if (rrt->rrt_index == ifcp->ifc_index &&
1384 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1385 /* same metric, same route from same gw */
1387 } else if (rrt->rrt_t < t_half_lifetime) {
1388 /* Better route found */
1389 rrt->rrt_index = ifcp->ifc_index;
1390 /* Update routing table */
1391 delroute(nq, &rrt->rrt_gw);
1394 addroute(rrt, &nh, ifcp);
1395 rrt->rrt_rflags |= RRTF_CHANGED;
1400 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1401 * do not update age value. Do nothing.
1403 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1404 /* Got a new valid route */
1405 if ((rrt = MALLOC(struct riprt)) == NULL) {
1406 fatal("malloc: struct riprt");
1409 memset(rrt, 0, sizeof(*rrt));
1410 nq = &rrt->rrt_info;
1412 rrt->rrt_same = NULL;
1413 rrt->rrt_index = ifcp->ifc_index;
1414 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1417 applyplen(&nq->rip6_dest, nq->rip6_plen);
1418 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1419 rrt->rrt_flags |= RTF_HOST;
1421 /* Update routing table */
1422 addroute(rrt, &nh, ifcp);
1423 rrt->rrt_rflags |= RRTF_CHANGED;
1427 /* Put the route to the list */
1428 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1431 /* XXX need to care the interval between triggered updates */
1433 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1434 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1435 if (ifcp->ifc_index == ic->ifc_index)
1437 if (ic->ifc_flags & IFF_UP)
1438 ripsend(ic, &ic->ifc_ripsin,
1442 /* Reset the flag */
1443 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1444 rrt->rrt_rflags &= ~RRTF_CHANGED;
1450 * Send all routes request packet to the specified interface.
1453 sendrequest(struct ifc *ifcp)
1455 struct netinfo6 *np;
1458 if (ifcp->ifc_flags & IFF_LOOPBACK)
1460 ripbuf->rip6_cmd = RIP6_REQUEST;
1461 np = ripbuf->rip6_nets;
1462 memset(np, 0, sizeof(struct netinfo6));
1463 np->rip6_metric = HOPCNT_INFINITY6;
1464 tracet(1, "Send rtdump Request to %s (%s)\n",
1465 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1466 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1467 if (error == EAFNOSUPPORT) {
1468 /* Protocol not supported */
1469 tracet(1, "Could not send rtdump Request to %s (%s): "
1470 "set IFF_UP to 0\n",
1471 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1472 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1474 ripbuf->rip6_cmd = RIP6_RESPONSE;
1478 * Process a RIP6_REQUEST packet.
1481 riprequest(struct ifc *ifcp,
1482 struct netinfo6 *np,
1484 struct sockaddr_in6 *sin6)
1489 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1490 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1491 /* Specific response, don't split-horizon */
1492 trace(1, "\tRIP Request\n");
1493 for (i = 0; i < nn; i++, np++) {
1496 np->rip6_metric = rrt->rrt_info.rip6_metric;
1498 np->rip6_metric = HOPCNT_INFINITY6;
1500 (void)sendpacket(sin6, RIPSIZE(nn));
1503 /* Whole routing table dump */
1504 trace(1, "\tRIP Request -- whole routing table\n");
1505 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1509 * Get information of each interface.
1514 struct ifaddrs *ifap, *ifa;
1516 struct ipv6_mreq mreq;
1519 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1524 if (getifaddrs(&ifap) != 0) {
1525 fatal("getifaddrs");
1529 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1530 if (ifa->ifa_addr->sa_family != AF_INET6)
1532 ifcp = ifc_find(ifa->ifa_name);
1533 /* we are interested in multicast-capable interfaces */
1534 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1538 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1539 fatal("malloc: struct ifc");
1542 memset(ifcp, 0, sizeof(*ifcp));
1544 ifcp->ifc_index = -1;
1545 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1546 sizeof(ifcp->ifc_name));
1547 TAILQ_INIT(&ifcp->ifc_ifac_head);
1548 TAILQ_INIT(&ifcp->ifc_iff_head);
1549 ifcp->ifc_flags = ifa->ifa_flags;
1550 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1551 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1552 ifflags(ifcp->ifc_flags));
1553 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1556 /* update flag, this may be up again */
1557 if (ifcp->ifc_flags != ifa->ifa_flags) {
1558 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1559 ifflags(ifcp->ifc_flags));
1560 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1561 ifcp->ifc_cflags |= IFC_CHANGED;
1563 ifcp->ifc_flags = ifa->ifa_flags;
1565 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1566 /* maybe temporary failure */
1569 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1570 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1571 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1572 mreq.ipv6mr_interface = ifcp->ifc_index;
1573 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1574 &mreq, sizeof(mreq)) < 0) {
1575 fatal("IPV6_JOIN_GROUP");
1578 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1587 ifconfig1(const char *name,
1588 const struct sockaddr *sa,
1592 struct in6_ifreq ifr;
1593 const struct sockaddr_in6 *sin6;
1598 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
1599 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1601 ifr.ifr_addr = *sin6;
1602 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1603 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1604 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1607 plen = sin6mask2len(&ifr.ifr_addr);
1608 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1609 /* same interface found */
1610 /* need check if something changed */
1611 /* XXX not yet implemented */
1615 * New address is found
1617 if ((ifac = MALLOC(struct ifac)) == NULL) {
1618 fatal("malloc: struct ifac");
1621 memset(ifac, 0, sizeof(*ifac));
1623 ifac->ifac_ifc = ifcp;
1624 ifac->ifac_addr = sin6->sin6_addr;
1625 ifac->ifac_plen = plen;
1626 ifac->ifac_scope_id = sin6->sin6_scope_id;
1627 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1628 ifr.ifr_addr = *sin6;
1629 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1630 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1633 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1634 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1636 trace(1, "found address %s/%d -- %s\n",
1637 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1639 trace(1, "found address %s/%d\n",
1640 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1642 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1643 ifcp->ifc_mylladdr = ifac->ifac_addr;
1644 ifcp->ifc_index = ifac->ifac_scope_id;
1645 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1646 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1647 setindex2ifc(ifcp->ifc_index, ifcp);
1648 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1649 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1650 ifcp->ifc_mtu = RIP6_MAXMTU;
1651 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1652 fatal("ioctl: SIOCGIFMETRIC");
1655 ifcp->ifc_metric = ifr.ifr_metric;
1656 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1657 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1659 ifcp->ifc_cflags |= IFC_CHANGED;
1661 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1667 ifremove(int ifindex)
1672 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1673 if (ifcp->ifc_index == ifindex)
1679 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1680 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1682 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1683 if (rrt->rrt_index == ifcp->ifc_index &&
1684 rrt->rrt_rflags & RRTF_AGGREGATE)
1685 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1691 * Receive and process routing messages.
1692 * Update interface information as necesssary.
1699 struct rt_msghdr *rtm;
1700 struct ifa_msghdr *ifam;
1701 struct if_msghdr *ifm;
1702 struct if_announcemsghdr *ifan;
1704 struct ifc *ifcp, *ic;
1705 int iface = 0, rtable = 0;
1706 struct sockaddr_in6 *rta[RTAX_MAX];
1707 struct sockaddr_in6 mask;
1711 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1712 perror("read from rtsock");
1718 if (len < sizeof(*rtm)) {
1719 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1720 len, (u_long)sizeof(*rtm));
1725 fprintf(stderr, "rtmsg:\n");
1726 for (i = 0; i < len; i++) {
1727 fprintf(stderr, "%02x ", buf[i] & 0xff);
1728 if (i % 16 == 15) fprintf(stderr, "\n");
1730 fprintf(stderr, "\n");
1733 for (p = buf; p - buf < len; p +=
1734 ((struct rt_msghdr *)(void *)p)->rtm_msglen) {
1735 if (((struct rt_msghdr *)(void *)p)->rtm_version != RTM_VERSION)
1738 /* safety against bogus message */
1739 if (((struct rt_msghdr *)(void *)p)->rtm_msglen <= 0) {
1740 trace(1, "bogus rtmsg: length=%d\n",
1741 ((struct rt_msghdr *)(void *)p)->rtm_msglen);
1747 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1750 ifam = (struct ifa_msghdr *)(void *)p;
1751 addrs = ifam->ifam_addrs;
1752 q = (char *)(ifam + 1);
1755 ifm = (struct if_msghdr *)(void *)p;
1756 addrs = ifm->ifm_addrs;
1757 q = (char *)(ifm + 1);
1759 case RTM_IFANNOUNCE:
1760 ifan = (struct if_announcemsghdr *)(void *)p;
1761 switch (ifan->ifan_what) {
1765 case IFAN_DEPARTURE:
1766 ifremove(ifan->ifan_index);
1772 rtm = (struct rt_msghdr *)(void *)p;
1773 if (rtm->rtm_version != RTM_VERSION) {
1774 trace(1, "unexpected rtmsg version %d "
1776 rtm->rtm_version, RTM_VERSION);
1780 * Only messages that use the struct rt_msghdr
1781 * format are allowed beyond this point.
1783 if (rtm->rtm_type > RTM_RESOLVE) {
1784 trace(1, "rtmsg type %d ignored\n",
1788 addrs = rtm->rtm_addrs;
1789 q = (char *)(rtm + 1);
1790 if (rtm->rtm_pid == pid) {
1792 trace(1, "rtmsg looped back to me, ignored\n");
1798 memset(&rta, 0, sizeof(rta));
1799 for (i = 0; i < RTAX_MAX; i++) {
1800 if (addrs & (1 << i)) {
1801 rta[i] = (struct sockaddr_in6 *)(void *)q;
1802 q += ROUNDUP(rta[i]->sin6_len);
1806 trace(1, "rtsock: %s (addrs=%x)\n",
1807 rttypes((struct rt_msghdr *)(void *)p), addrs);
1810 i < ((struct rt_msghdr *)(void *)p)->rtm_msglen;
1812 fprintf(stderr, "%02x ", p[i] & 0xff);
1813 if (i % 16 == 15) fprintf(stderr, "\n");
1815 fprintf(stderr, "\n");
1821 * We may be able to optimize by using ifm->ifm_index or
1822 * ifam->ifam_index. For simplicity we don't do that here.
1824 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1836 /* nothing to be done here */
1837 trace(1, "\tnothing to be done, ignored\n");
1842 if (rta[RTAX_DST] == NULL) {
1843 trace(1, "\tno destination, ignored\n");
1846 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1847 trace(1, "\taf mismatch, ignored\n");
1850 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1851 trace(1, "\tlinklocal destination, ignored\n");
1854 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1855 trace(1, "\tloopback destination, ignored\n");
1856 continue; /* Loopback */
1858 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1859 trace(1, "\tmulticast destination, ignored\n");
1865 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1873 /* should already be handled */
1874 fatal("rtrecv: never reach here");
1877 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1878 trace(1, "\tsome of dst/gw/netamsk are "
1879 "unavailable, ignored\n");
1882 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1883 mask.sin6_len = sizeof(mask);
1884 memset(&mask.sin6_addr, 0xff,
1885 sizeof(mask.sin6_addr));
1886 rta[RTAX_NETMASK] = &mask;
1887 } else if (!rta[RTAX_NETMASK]) {
1888 trace(1, "\tsome of dst/gw/netamsk are "
1889 "unavailable, ignored\n");
1892 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1893 rta[RTAX_NETMASK]) == 0) {
1894 rtable++; /*just to be sure*/
1899 trace(1, "\tnot supported yet, ignored\n");
1902 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1903 trace(1, "\tno netmask or ifa given, ignored\n");
1906 if (ifam->ifam_index < nindex2ifc)
1907 ifcp = index2ifc[ifam->ifam_index];
1911 trace(1, "\tinvalid ifam_index %d, ignored\n",
1915 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1923 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1925 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1926 if (ifcp->ifc_cflags & IFC_CHANGED) {
1927 if (ifrt(ifcp, 1)) {
1928 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1929 if (ifcp->ifc_index == ic->ifc_index)
1931 if (ic->ifc_flags & IFF_UP)
1932 ripsend(ic, &ic->ifc_ripsin,
1935 /* Reset the flag */
1936 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1937 rrt->rrt_rflags &= ~RRTF_CHANGED;
1940 ifcp->ifc_cflags &= ~IFC_CHANGED;
1945 trace(1, "rtsock: read routing table again\n");
1951 * remove specified route from the internal routing table.
1954 rt_del(const struct sockaddr_in6 *sdst,
1955 const struct sockaddr_in6 *sgw,
1956 const struct sockaddr_in6 *smask)
1958 const struct in6_addr *dst = NULL;
1959 const struct in6_addr *gw = NULL;
1961 struct netinfo6 ni6;
1962 struct riprt *rrt = NULL;
1965 if (sdst->sin6_family != AF_INET6) {
1966 trace(1, "\tother AF, ignored\n");
1969 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1970 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1971 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1972 trace(1, "\taddress %s not interesting, ignored\n",
1973 inet6_n2p(&sdst->sin6_addr));
1976 dst = &sdst->sin6_addr;
1977 if (sgw->sin6_family == AF_INET6) {
1979 gw = &sgw->sin6_addr;
1980 prefix = sin6mask2len(smask);
1981 } else if (sgw->sin6_family == AF_LINK) {
1983 * Interface route... a hard case. We need to get the prefix
1984 * length from the kernel, but we now are parsing rtmsg.
1985 * We'll purge matching routes from my list, then get the
1988 struct riprt *longest;
1989 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1992 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1993 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1995 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1997 || longest->rrt_info.rip6_plen <
1998 rrt->rrt_info.rip6_plen) {
2005 trace(1, "\tno matching interface route found\n");
2008 gw = &in6addr_loopback;
2009 prefix = rrt->rrt_info.rip6_plen;
2011 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2015 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2016 trace(1, "gw %s\n", inet6_n2p(gw));
2017 t_lifetime = time(NULL) - RIP_LIFETIME;
2018 /* age route for interface address */
2019 memset(&ni6, 0, sizeof(ni6));
2020 ni6.rip6_dest = *dst;
2021 ni6.rip6_plen = prefix;
2022 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2023 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2025 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2026 trace(1, "\tno route found\n");
2030 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2031 trace(1, "\tyou can delete static routes only\n");
2034 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2035 trace(1, "\tgw mismatch: %s <-> ",
2036 inet6_n2p(&rrt->rrt_gw));
2037 trace(1, "%s\n", inet6_n2p(gw));
2039 trace(1, "\troute found, age it\n");
2040 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2041 rrt->rrt_t = t_lifetime;
2042 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2049 * remove specified address from internal interface/routing table.
2052 rt_deladdr(struct ifc *ifcp,
2053 const struct sockaddr_in6 *sifa,
2054 const struct sockaddr_in6 *smask)
2056 const struct in6_addr *addr = NULL;
2058 struct ifac *ifac = NULL;
2059 struct netinfo6 ni6;
2060 struct riprt *rrt = NULL;
2064 if (sifa->sin6_family != AF_INET6) {
2065 trace(1, "\tother AF, ignored\n");
2068 addr = &sifa->sin6_addr;
2069 prefix = sin6mask2len(smask);
2071 trace(1, "\tdeleting %s/%d from %s\n",
2072 inet6_n2p(addr), prefix, ifcp->ifc_name);
2073 ifac = ifa_match(ifcp, addr, prefix);
2075 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2076 inet6_n2p(addr), prefix, ifcp->ifc_name);
2079 if (ifac->ifac_ifc != ifcp) {
2080 trace(1, "\taddress table corrupt: back pointer does not match "
2082 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2085 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2086 t_lifetime = time(NULL) - RIP_LIFETIME;
2087 /* age route for interface address */
2088 memset(&ni6, 0, sizeof(ni6));
2089 ni6.rip6_dest = ifac->ifac_addr;
2090 ni6.rip6_plen = ifac->ifac_plen;
2091 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2092 trace(1, "\tfind interface route %s/%d on %d\n",
2093 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2094 if ((rrt = rtsearch(&ni6)) != NULL) {
2095 struct in6_addr none;
2096 memset(&none, 0, sizeof(none));
2097 if (rrt->rrt_index == ifcp->ifc_index &&
2098 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2099 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2100 trace(1, "\troute found, age it\n");
2101 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2102 rrt->rrt_t = t_lifetime;
2103 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2107 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2108 inet6_n2p(&rrt->rrt_info.rip6_dest),
2109 rrt->rrt_info.rip6_plen,
2113 trace(1, "\tno interface route found\n");
2114 /* age route for p2p destination */
2115 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2116 memset(&ni6, 0, sizeof(ni6));
2117 ni6.rip6_dest = ifac->ifac_raddr;
2118 ni6.rip6_plen = 128;
2119 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2120 trace(1, "\tfind p2p route %s/%d on %d\n",
2121 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2123 if ((rrt = rtsearch(&ni6)) != NULL) {
2124 if (rrt->rrt_index == ifcp->ifc_index &&
2125 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2126 &ifac->ifac_addr)) {
2127 trace(1, "\troute found, age it\n");
2128 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2129 rrt->rrt_t = t_lifetime;
2130 rrt->rrt_info.rip6_metric =
2135 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2136 inet6_n2p(&rrt->rrt_info.rip6_dest),
2137 rrt->rrt_info.rip6_plen,
2141 trace(1, "\tno p2p route found\n");
2145 return ((updated) ? 0 : -1);
2149 * Get each interface address and put those interface routes to the route
2153 ifrt(struct ifc *ifcp, int again)
2156 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2157 struct netinfo6 *np;
2159 int need_trigger = 0;
2162 if (ifcp->ifc_flags & IFF_LOOPBACK)
2163 return 0; /* ignore loopback */
2166 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2167 ifrt_p2p(ifcp, again);
2171 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2172 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2174 trace(1, "route: %s on %s: "
2175 "skip linklocal interface address\n",
2176 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2180 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2182 trace(1, "route: %s: skip unspec interface address\n",
2187 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2189 trace(1, "route: %s: skip loopback address\n",
2194 if (ifcp->ifc_flags & IFF_UP) {
2195 if ((rrt = MALLOC(struct riprt)) == NULL)
2196 fatal("malloc: struct riprt");
2197 memset(rrt, 0, sizeof(*rrt));
2198 rrt->rrt_same = NULL;
2199 rrt->rrt_index = ifcp->ifc_index;
2200 rrt->rrt_t = 0; /* don't age */
2201 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2202 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2203 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2204 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2205 rrt->rrt_flags = RTF_HOST;
2206 rrt->rrt_rflags |= RRTF_CHANGED;
2207 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2208 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2209 rrt->rrt_gw = ifac->ifac_addr;
2210 np = &rrt->rrt_info;
2211 search_rrt = rtsearch(np);
2212 if (search_rrt != NULL) {
2213 if (search_rrt->rrt_info.rip6_metric <=
2214 rrt->rrt_info.rip6_metric) {
2215 /* Already have better route */
2217 trace(1, "route: %s/%d: "
2218 "already registered (%s)\n",
2219 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2225 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2226 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2228 /* Attach the route to the list */
2229 trace(1, "route: %s/%d: register route (%s)\n",
2230 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2232 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2233 addroute(rrt, &rrt->rrt_gw, ifcp);
2236 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2239 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2240 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2241 t_lifetime = time(NULL) - RIP_LIFETIME;
2242 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2243 loop_rrt->rrt_t = t_lifetime;
2244 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2245 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2255 return need_trigger;
2259 * there are couple of p2p interface routing models. "behavior" lets
2260 * you pick one. it looks that gated behavior fits best with BSDs,
2261 * since BSD kernels do not look at prefix length on p2p interfaces.
2264 ifrt_p2p(struct ifc *ifcp, int again)
2267 struct riprt *rrt, *orrt;
2268 struct netinfo6 *np;
2269 struct in6_addr addr, dest;
2270 int advert, ignore, i;
2271 #define P2PADVERT_NETWORK 1
2272 #define P2PADVERT_ADDR 2
2273 #define P2PADVERT_DEST 4
2274 #define P2PADVERT_MAX 4
2275 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2276 const char *category = "";
2279 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2280 addr = ifac->ifac_addr;
2281 dest = ifac->ifac_raddr;
2282 applyplen(&addr, ifac->ifac_plen);
2283 applyplen(&dest, ifac->ifac_plen);
2284 advert = ignore = 0;
2288 * honor addr/plen, just like normal shared medium
2289 * interface. this may cause trouble if you reuse
2290 * addr/plen on other interfaces.
2292 * advertise addr/plen.
2294 advert |= P2PADVERT_NETWORK;
2298 * prefixlen on p2p interface is meaningless.
2299 * advertise addr/128 and dest/128.
2301 * do not install network route to route6d routing
2302 * table (if we do, it would prevent route installation
2303 * for other p2p interface that shares addr/plen).
2305 * XXX what should we do if dest is ::? it will not
2306 * get announced anyways (see following filter),
2307 * but we need to think.
2309 advert |= P2PADVERT_ADDR;
2310 advert |= P2PADVERT_DEST;
2311 ignore |= P2PADVERT_NETWORK;
2315 * just for testing. actually the code is redundant
2316 * given the current p2p interface address assignment
2317 * rule for kame kernel.
2320 * A/n -> announce A/n
2321 * A B/n, A and B share prefix -> A/n (= B/n)
2322 * A B/n, do not share prefix -> A/128 and B/128
2323 * actually, A/64 and A B/128 are the only cases
2324 * permitted by the kernel:
2326 * A B/128 -> A/128 and B/128
2328 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2329 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2330 advert |= P2PADVERT_NETWORK;
2332 advert |= P2PADVERT_ADDR;
2333 advert |= P2PADVERT_DEST;
2334 ignore |= P2PADVERT_NETWORK;
2337 advert |= P2PADVERT_NETWORK;
2341 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2342 if ((ignore & i) != 0)
2344 if ((rrt = MALLOC(struct riprt)) == NULL) {
2345 fatal("malloc: struct riprt");
2348 memset(rrt, 0, sizeof(*rrt));
2349 rrt->rrt_same = NULL;
2350 rrt->rrt_index = ifcp->ifc_index;
2351 rrt->rrt_t = 0; /* don't age */
2353 case P2PADVERT_NETWORK:
2354 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2355 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2356 applyplen(&rrt->rrt_info.rip6_dest,
2358 category = "network";
2360 case P2PADVERT_ADDR:
2361 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2362 rrt->rrt_info.rip6_plen = 128;
2363 rrt->rrt_gw = in6addr_loopback;
2366 case P2PADVERT_DEST:
2367 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2368 rrt->rrt_info.rip6_plen = 128;
2369 rrt->rrt_gw = ifac->ifac_addr;
2373 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2374 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2376 trace(1, "route: %s: skip unspec/linklocal "
2377 "(%s on %s)\n", category, ifcp->ifc_name);
2382 if ((advert & i) == 0) {
2383 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2387 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2388 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2389 np = &rrt->rrt_info;
2390 orrt = rtsearch(np);
2392 /* Attach the route to the list */
2393 trace(1, "route: %s/%d: register route "
2395 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2396 category, ifcp->ifc_name, noadv);
2397 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2398 } else if (rrt->rrt_index != orrt->rrt_index ||
2399 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2401 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2402 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2405 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2406 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2407 category, ifcp->ifc_name, noadv);
2411 trace(1, "route: %s/%d: "
2412 "already registered (%s on %s%s)\n",
2413 inet6_n2p(&np->rip6_dest),
2414 np->rip6_plen, category,
2415 ifcp->ifc_name, noadv);
2421 #undef P2PADVERT_NETWORK
2422 #undef P2PADVERT_ADDR
2423 #undef P2PADVERT_DEST
2424 #undef P2PADVERT_MAX
2428 getifmtu(int ifindex)
2433 struct if_msghdr *ifm;
2440 mib[4] = NET_RT_IFLIST;
2442 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2443 fatal("sysctl estimate NET_RT_IFLIST");
2446 if ((buf = malloc(msize)) == NULL) {
2450 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2451 fatal("sysctl NET_RT_IFLIST");
2454 ifm = (struct if_msghdr *)(void *)buf;
2455 mtu = ifm->ifm_data.ifi_mtu;
2456 if (ifindex != ifm->ifm_index) {
2457 fatal("ifindex does not match with ifm_index");
2465 rttypes(struct rt_msghdr *rtm)
2467 #define RTTYPE(s, f) \
2469 if (rtm->rtm_type == (f)) \
2472 RTTYPE("ADD", RTM_ADD);
2473 RTTYPE("DELETE", RTM_DELETE);
2474 RTTYPE("CHANGE", RTM_CHANGE);
2475 RTTYPE("GET", RTM_GET);
2476 RTTYPE("LOSING", RTM_LOSING);
2477 RTTYPE("REDIRECT", RTM_REDIRECT);
2478 RTTYPE("MISS", RTM_MISS);
2479 RTTYPE("LOCK", RTM_LOCK);
2480 RTTYPE("NEWADDR", RTM_NEWADDR);
2481 RTTYPE("DELADDR", RTM_DELADDR);
2482 RTTYPE("IFINFO", RTM_IFINFO);
2484 RTTYPE("OIFINFO", RTM_OIFINFO);
2486 #ifdef RTM_IFANNOUNCE
2487 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2490 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2493 RTTYPE("DELMADDR", RTM_DELMADDR);
2500 rtflags(struct rt_msghdr *rtm)
2502 static char buf[BUFSIZ];
2505 * letter conflict should be okay. painful when *BSD diverges...
2507 strlcpy(buf, "", sizeof(buf));
2508 #define RTFLAG(s, f) \
2510 if (rtm->rtm_flags & (f)) \
2511 strlcat(buf, (s), sizeof(buf)); \
2513 RTFLAG("U", RTF_UP);
2514 RTFLAG("G", RTF_GATEWAY);
2515 RTFLAG("H", RTF_HOST);
2516 RTFLAG("R", RTF_REJECT);
2517 RTFLAG("D", RTF_DYNAMIC);
2518 RTFLAG("M", RTF_MODIFIED);
2519 RTFLAG("d", RTF_DONE);
2521 RTFLAG("m", RTF_MASK);
2524 RTFLAG("c", RTF_CLONED);
2526 RTFLAG("X", RTF_XRESOLVE);
2528 RTFLAG("L", RTF_LLINFO);
2530 RTFLAG("S", RTF_STATIC);
2531 RTFLAG("B", RTF_BLACKHOLE);
2533 RTFLAG("3", RTF_PROTO3);
2535 RTFLAG("2", RTF_PROTO2);
2536 RTFLAG("1", RTF_PROTO1);
2537 #ifdef RTF_BROADCAST
2538 RTFLAG("b", RTF_BROADCAST);
2541 RTFLAG("d", RTF_DEFAULT);
2543 #ifdef RTF_ISAROUTER
2544 RTFLAG("r", RTF_ISAROUTER);
2547 RTFLAG("T", RTF_TUNNEL);
2550 RTFLAG("A", RTF_AUTH);
2553 RTFLAG("E", RTF_CRYPT);
2562 static char buf[BUFSIZ];
2564 strlcpy(buf, "", sizeof(buf));
2565 #define IFFLAG(s, f) \
2567 if (flags & (f)) { \
2569 strlcat(buf, ",", sizeof(buf)); \
2570 strlcat(buf, (s), sizeof(buf)); \
2573 IFFLAG("UP", IFF_UP);
2574 IFFLAG("BROADCAST", IFF_BROADCAST);
2575 IFFLAG("DEBUG", IFF_DEBUG);
2576 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2577 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2578 #ifdef IFF_NOTRAILERS
2579 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2581 IFFLAG("RUNNING", IFF_RUNNING);
2582 IFFLAG("NOARP", IFF_NOARP);
2583 IFFLAG("PROMISC", IFF_PROMISC);
2584 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2585 IFFLAG("OACTIVE", IFF_OACTIVE);
2586 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2587 IFFLAG("LINK0", IFF_LINK0);
2588 IFFLAG("LINK1", IFF_LINK1);
2589 IFFLAG("LINK2", IFF_LINK2);
2590 IFFLAG("MULTICAST", IFF_MULTICAST);
2600 char *buf, *p, *lim;
2601 struct rt_msghdr *rtm;
2610 mib[3] = AF_INET6; /* Address family */
2611 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2612 mib[5] = 0; /* No flags */
2622 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2623 errmsg = "sysctl estimate";
2626 if ((buf = malloc(msize)) == NULL) {
2630 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2631 errmsg = "sysctl NET_RT_DUMP";
2634 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2636 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2639 } else if (1 < retry)
2640 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2643 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2644 rtm = (struct rt_msghdr *)(void *)p;
2645 rt_entry(rtm, again);
2651 rt_entry(struct rt_msghdr *rtm, int again)
2653 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2654 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2655 char *rtmp, *ifname = NULL;
2656 struct riprt *rrt, *orrt;
2657 struct netinfo6 *np;
2660 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2661 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2662 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2663 return; /* not interested in the link route */
2665 /* do not look at cloned routes */
2666 #ifdef RTF_WASCLONED
2667 if (rtm->rtm_flags & RTF_WASCLONED)
2671 if (rtm->rtm_flags & RTF_CLONED)
2674 /* XXX: Ignore connected routes. */
2675 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2678 * do not look at dynamic routes.
2679 * netbsd/openbsd cloned routes have UGHD.
2681 if (rtm->rtm_flags & RTF_DYNAMIC)
2683 rtmp = (char *)(rtm + 1);
2685 if ((rtm->rtm_addrs & RTA_DST) == 0)
2686 return; /* ignore routes without destination address */
2687 sin6_dst = (struct sockaddr_in6 *)(void *)rtmp;
2688 rtmp += ROUNDUP(sin6_dst->sin6_len);
2689 if (rtm->rtm_addrs & RTA_GATEWAY) {
2690 sin6_gw = (struct sockaddr_in6 *)(void *)rtmp;
2691 rtmp += ROUNDUP(sin6_gw->sin6_len);
2693 if (rtm->rtm_addrs & RTA_NETMASK) {
2694 sin6_mask = (struct sockaddr_in6 *)(void *)rtmp;
2695 rtmp += ROUNDUP(sin6_mask->sin6_len);
2697 if (rtm->rtm_addrs & RTA_GENMASK) {
2698 sin6_genmask = (struct sockaddr_in6 *)(void *)rtmp;
2699 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2701 if (rtm->rtm_addrs & RTA_IFP) {
2702 sin6_ifp = (struct sockaddr_in6 *)(void *)rtmp;
2703 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2707 if (sin6_dst->sin6_family != AF_INET6)
2709 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2710 return; /* Link-local */
2711 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2712 return; /* Loopback */
2713 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2716 if ((rrt = MALLOC(struct riprt)) == NULL) {
2717 fatal("malloc: struct riprt");
2720 memset(rrt, 0, sizeof(*rrt));
2721 np = &rrt->rrt_info;
2722 rrt->rrt_same = NULL;
2723 rrt->rrt_t = time(NULL);
2724 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2725 rrt->rrt_t = 0; /* Don't age static routes */
2726 if (rtm->rtm_flags & Pflag)
2727 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2728 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2729 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2731 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2732 if (np->rip6_metric < 1)
2733 np->rip6_metric = 1;
2734 rrt->rrt_flags = rtm->rtm_flags;
2735 np->rip6_dest = sin6_dst->sin6_addr;
2738 if (rtm->rtm_flags & RTF_HOST)
2739 np->rip6_plen = 128; /* Host route */
2741 np->rip6_plen = sin6mask2len(sin6_mask);
2745 orrt = rtsearch(np);
2746 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2749 trace(1, "route: %s/%d flags %s: already registered\n",
2750 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2758 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2760 if (sin6_gw->sin6_family == AF_INET6)
2761 rrt->rrt_gw = sin6_gw->sin6_addr;
2762 else if (sin6_gw->sin6_family == AF_LINK) {
2763 /* XXX in case ppp link? */
2764 rrt->rrt_gw = in6addr_loopback;
2766 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2768 trace(1, "route: %s/%d flags %s",
2769 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2770 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2773 ifindex = rtm->rtm_index;
2774 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2775 ifname = index2ifc[ifindex]->ifc_name;
2777 trace(1, " not configured\n");
2781 trace(1, " if %s sock %d", ifname, ifindex);
2782 rrt->rrt_index = ifindex;
2787 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2788 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2789 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2790 trace(0, "***** Gateway %s is not a link-local address.\n",
2791 inet6_n2p(&rrt->rrt_gw));
2792 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2793 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2794 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2797 /* Put it to the route list */
2798 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2799 /* replace route list */
2800 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2801 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2803 trace(1, "route: %s/%d flags %s: replace new route\n",
2804 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2808 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2812 addroute(struct riprt *rrt,
2813 const struct in6_addr *gw,
2816 struct netinfo6 *np;
2817 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2818 struct rt_msghdr *rtm;
2819 struct sockaddr_in6 *sin6;
2822 np = &rrt->rrt_info;
2823 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2824 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2825 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2826 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2827 np->rip6_metric - 1, buf2);
2829 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2830 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2831 np->rip6_metric - 1, buf2);
2835 memset(buf, 0, sizeof(buf));
2836 rtm = (struct rt_msghdr *)(void *)buf;
2837 rtm->rtm_type = RTM_ADD;
2838 rtm->rtm_version = RTM_VERSION;
2839 rtm->rtm_seq = ++seq;
2841 rtm->rtm_flags = rrt->rrt_flags;
2842 rtm->rtm_flags |= Qflag;
2843 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2844 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2845 rtm->rtm_inits = RTV_HOPCOUNT;
2846 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2848 sin6->sin6_len = sizeof(struct sockaddr_in6);
2849 sin6->sin6_family = AF_INET6;
2850 sin6->sin6_addr = np->rip6_dest;
2851 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2853 sin6->sin6_len = sizeof(struct sockaddr_in6);
2854 sin6->sin6_family = AF_INET6;
2855 sin6->sin6_addr = *gw;
2856 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2857 sin6->sin6_scope_id = ifcp->ifc_index;
2858 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2860 sin6->sin6_len = sizeof(struct sockaddr_in6);
2861 sin6->sin6_family = AF_INET6;
2862 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2863 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2865 len = (char *)sin6 - (char *)buf;
2866 rtm->rtm_msglen = len;
2867 if (write(rtsock, buf, len) > 0)
2870 if (errno == EEXIST) {
2871 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2872 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2874 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2875 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2877 trace(0, "Can not write to rtsock (addroute): %s\n",
2880 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2887 delroute(struct netinfo6 *np, struct in6_addr *gw)
2889 u_char buf[BUFSIZ], buf2[BUFSIZ];
2890 struct rt_msghdr *rtm;
2891 struct sockaddr_in6 *sin6;
2894 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2895 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2896 np->rip6_plen, buf2);
2898 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2899 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2903 memset(buf, 0, sizeof(buf));
2904 rtm = (struct rt_msghdr *)(void *)buf;
2905 rtm->rtm_type = RTM_DELETE;
2906 rtm->rtm_version = RTM_VERSION;
2907 rtm->rtm_seq = ++seq;
2909 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2910 rtm->rtm_flags |= Qflag;
2911 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2912 rtm->rtm_flags |= RTF_HOST;
2913 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2914 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2916 sin6->sin6_len = sizeof(struct sockaddr_in6);
2917 sin6->sin6_family = AF_INET6;
2918 sin6->sin6_addr = np->rip6_dest;
2919 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2921 sin6->sin6_len = sizeof(struct sockaddr_in6);
2922 sin6->sin6_family = AF_INET6;
2923 sin6->sin6_addr = *gw;
2924 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2926 sin6->sin6_len = sizeof(struct sockaddr_in6);
2927 sin6->sin6_family = AF_INET6;
2928 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2929 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2931 len = (char *)sin6 - (char *)buf;
2932 rtm->rtm_msglen = len;
2933 if (write(rtsock, buf, len) >= 0)
2936 if (errno == ESRCH) {
2937 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2938 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2940 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2941 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2943 trace(0, "Can not write to rtsock (delroute): %s\n",
2946 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2953 static struct in6_addr *
2954 getroute(struct netinfo6 *np, struct in6_addr *gw)
2959 struct rt_msghdr *rtm;
2960 struct sockaddr_in6 *sin6;
2962 rtm = (struct rt_msghdr *)(void *)buf;
2963 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2964 memset(rtm, 0, len);
2965 rtm->rtm_type = RTM_GET;
2966 rtm->rtm_version = RTM_VERSION;
2968 rtm->rtm_seq = myseq;
2969 rtm->rtm_addrs = RTA_DST;
2970 rtm->rtm_msglen = len;
2971 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2972 sin6->sin6_len = sizeof(struct sockaddr_in6);
2973 sin6->sin6_family = AF_INET6;
2974 sin6->sin6_addr = np->rip6_dest;
2975 if (write(rtsock, buf, len) < 0) {
2976 if (errno == ESRCH) /* No such route found */
2978 perror("write to rtsock");
2982 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2983 perror("read from rtsock");
2986 rtm = (struct rt_msghdr *)(void *)buf;
2987 } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != myseq ||
2988 rtm->rtm_pid != pid);
2989 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2990 if (rtm->rtm_addrs & RTA_DST) {
2991 sin6 = (struct sockaddr_in6 *)(void *)
2992 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2994 if (rtm->rtm_addrs & RTA_GATEWAY) {
2995 *gw = sin6->sin6_addr;
3003 inet6_n2p(const struct in6_addr *p)
3005 static char buf[BUFSIZ];
3007 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3028 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3031 fprintf(dump, "%s: Interface Table Dump\n", hms());
3032 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3034 fprintf(dump, " Number of interfaces: %d\n", nifc);
3036 fprintf(dump, " advertising interfaces:\n");
3037 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3038 if ((ifcp->ifc_flags & IFF_UP) == 0)
3040 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3042 ifdump0(dump, ifcp);
3044 fprintf(dump, "\n");
3045 fprintf(dump, " non-advertising interfaces:\n");
3046 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3047 if ((ifcp->ifc_flags & IFF_UP) &&
3048 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3050 ifdump0(dump, ifcp);
3052 fprintf(dump, "\n");
3058 ifdump0(FILE *dump, const struct ifc *ifcp)
3066 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3067 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3068 inet6_n2p(&ifcp->ifc_mylladdr),
3069 ifcp->ifc_mtu, ifcp->ifc_metric);
3070 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3071 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3072 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3074 fprintf(dump, "\t%s/%d -- %s\n",
3075 inet6_n2p(&ifac->ifac_addr),
3076 ifac->ifac_plen, buf);
3078 fprintf(dump, "\t%s/%d\n",
3079 inet6_n2p(&ifac->ifac_addr),
3084 fprintf(dump, "\tFilter:\n");
3085 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3087 switch (iffp->iff_type) {
3089 ft = "Aggregate"; addr++; break;
3091 ft = "No-use"; break;
3093 ft = "Advertise-only"; addr++; break;
3095 ft = "Default-only"; break;
3097 ft = "Listen-only"; addr++; break;
3099 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3104 fprintf(dump, "\t\t%s", ft);
3106 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3108 fprintf(dump, "\n");
3110 fprintf(dump, "\n");
3124 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3128 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3129 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3130 if (rrt->rrt_t == 0)
3133 age = t - rrt->rrt_t;
3134 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3136 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3137 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3138 index2ifc[rrt->rrt_index]->ifc_name,
3139 inet6_n2p(&rrt->rrt_gw),
3140 rrt->rrt_info.rip6_metric, (long)age);
3141 if (rrt->rrt_info.rip6_tag) {
3142 fprintf(dump, " tag(0x%04x)",
3143 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3145 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3146 fprintf(dump, " NOT-LL");
3147 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3148 fprintf(dump, " NO-ADV");
3149 fprintf(dump, "\n");
3151 fprintf(dump, "\n");
3157 * Parse the -A (and -O) options and put corresponding filter object to the
3158 * specified interface structures. Each of the -A/O option has the following
3159 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3160 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3166 char *p, *ap, *iflp, *ifname, *ep;
3167 struct iff iff, *iffp;
3175 for (i = 0; i < nfilter; i++) {
3179 memset(iffp, 0, sizeof(*iffp));
3180 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3184 if ((p = strchr(ap, ',')) != NULL) {
3188 if ((p = strchr(ap, '/')) == NULL) {
3189 fatal("no prefixlen specified for '%s'", ap);
3193 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3194 fatal("invalid prefix specified for '%s'", ap);
3199 plen = strtoul(p, &ep, 10);
3200 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3201 fatal("invalid prefix length specified for '%s'", ap);
3204 iffp->iff_plen = plen;
3205 applyplen(&iffp->iff_addr, iffp->iff_plen);
3207 iffp->iff_type = filtertype[i];
3208 if (iflp == NULL || *iflp == '\0') {
3209 fatal("no interface specified for '%s'", ap);
3212 /* parse the interface listing portion */
3215 if ((iflp = strchr(iflp, ',')) != NULL)
3218 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3219 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3222 iffp = malloc(sizeof(*iffp));
3224 fatal("malloc of iff");
3227 memcpy(iffp, &iff, sizeof(*iffp));
3229 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3231 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3236 * -A: aggregate configuration.
3238 if (filtertype[i] != IFIL_TYPE_A)
3240 /* put the aggregate to the kernel routing table */
3241 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3243 fatal("malloc: rrt");
3246 memset(rrt, 0, sizeof(struct riprt));
3247 rrt->rrt_info.rip6_dest = iff.iff_addr;
3248 rrt->rrt_info.rip6_plen = iff.iff_plen;
3249 rrt->rrt_info.rip6_metric = 1;
3250 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3251 rrt->rrt_gw = in6addr_loopback;
3252 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3253 rrt->rrt_rflags = RRTF_AGGREGATE;
3255 rrt->rrt_index = loopifcp->ifc_index;
3257 if (getroute(&rrt->rrt_info, &gw)) {
3260 * When the address has already been registered in the
3261 * kernel routing table, it should be removed
3263 delroute(&rrt->rrt_info, &gw);
3265 /* it is safer behavior */
3267 fatal("%s/%u already in routing table, "
3269 inet6_n2p(&rrt->rrt_info.rip6_dest),
3270 rrt->rrt_info.rip6_plen);
3275 /* Put the route to the list */
3276 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3277 trace(1, "Aggregate: %s/%d for %s\n",
3278 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3279 loopifcp->ifc_name);
3280 /* Add this route to the kernel */
3281 if (nflag) /* do not modify kernel routing table */
3283 addroute(rrt, &in6addr_loopback, loopifcp);
3287 /***************** utility functions *****************/
3290 * Returns a pointer to ifac whose address and prefix length matches
3291 * with the address and prefix length specified in the arguments.
3293 static struct ifac *
3294 ifa_match(const struct ifc *ifcp,
3295 const struct in6_addr *ia,
3300 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3301 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3302 ifac->ifac_plen == plen)
3310 * Return a pointer to riprt structure whose address and prefix length
3311 * matches with the address and prefix length found in the argument.
3312 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3314 static struct riprt *
3315 rtsearch(struct netinfo6 *np)
3319 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3320 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3321 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3330 sin6mask2len(const struct sockaddr_in6 *sin6)
3333 return mask2len(&sin6->sin6_addr,
3334 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3338 mask2len(const struct in6_addr *addr, int lenlim)
3341 const u_char *p = (const u_char *)addr;
3343 for (j = 0; j < lenlim; j++, p++) {
3350 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3351 MASKLEN(0xfe, 7); break;
3352 MASKLEN(0xfc, 6); break;
3353 MASKLEN(0xf8, 5); break;
3354 MASKLEN(0xf0, 4); break;
3355 MASKLEN(0xe0, 3); break;
3356 MASKLEN(0xc0, 2); break;
3357 MASKLEN(0x80, 1); break;
3364 static const u_char plent[8] = {
3365 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3369 applyplen(struct in6_addr *ia, int plen)
3375 for (i = 0; i < 16; i++) {
3384 static const int pl2m[9] = {
3385 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3388 static struct in6_addr *
3391 static struct in6_addr ia;
3395 memset(&ia, 0, sizeof(struct in6_addr));
3397 for (i = 0; i < 16; i++, p++, n -= 8) {
3411 int len = strlen(p) + 1;
3412 char *q = (char *)malloc(len);
3426 static char buf[BUFSIZ];
3431 if ((tm = localtime(&t)) == 0) {
3435 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3440 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3443 ripinterval(int timer)
3447 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3448 nextalarm = time(NULL) + interval;
3459 t = (int)(RIP_TRIG_INT6_MIN +
3460 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3461 sup_trig_update = time(NULL) + t;
3467 fatal(const char *fmt, ...)
3473 vsnprintf(buf, sizeof(buf), fmt, ap);
3477 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3479 syslog(LOG_ERR, "%s", buf);
3484 tracet(int level, const char *fmt, ...)
3488 if (level <= dflag) {
3490 fprintf(stderr, "%s: ", hms());
3491 vfprintf(stderr, fmt, ap);
3497 vsyslog(LOG_DEBUG, fmt, ap);
3499 vsyslog(LOG_WARNING, fmt, ap);
3505 trace(int level, const char *fmt, ...)
3509 if (level <= dflag) {
3511 vfprintf(stderr, fmt, ap);
3517 vsyslog(LOG_DEBUG, fmt, ap);
3519 vsyslog(LOG_WARNING, fmt, ap);
3525 ifc_find(char *name)
3529 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3530 if (strcmp(name, ifcp->ifc_name) == 0)
3537 iff_find(struct ifc *ifcp, int type)
3541 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3542 if (type == IFIL_TYPE_ANY ||
3543 type == iffp->iff_type)
3551 setindex2ifc(int idx, struct ifc *ifcp)
3557 nindex2ifc = 5; /*initial guess*/
3558 index2ifc = (struct ifc **)
3559 malloc(sizeof(*index2ifc) * nindex2ifc);
3560 if (index2ifc == NULL) {
3564 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3567 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3570 p = (struct ifc **)realloc(index2ifc,
3571 sizeof(*index2ifc) * nsize);
3576 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3580 index2ifc[idx] = ifcp;