1 /* $FreeBSD: src/usr.sbin/route6d/route6d.c,v 1.2.2.5 2001/07/03 11:02:09 ume Exp $ */
2 /* $DragonFly: src/usr.sbin/route6d/route6d.c,v 1.2 2003/06/17 04:30:02 dillon Exp $ */
3 /* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $
53 #include <sys/types.h>
54 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
58 #include <sys/sysctl.h>
61 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
62 #include <net/if_var.h>
63 #endif /* __FreeBSD__ >= 3 */
66 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet/udp.h>
76 #include <arpa/inet.h>
83 #define INIT_INTERVAL6 6
85 #define INIT_INTERVAL6 10 /* Wait to submit a initial riprequest */
88 /* alignment constraint for routing socket */
90 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
91 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
94 * Following two macros are highly depending on KAME Release
96 #define IN6_LINKLOCAL_IFINDEX(addr) \
97 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
99 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
101 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
102 (addr).s6_addr[3] = (index) & 0xff; \
105 struct ifc { /* Configuration of an interface */
106 char *ifc_name; /* if name */
107 struct ifc *ifc_next;
108 int ifc_index; /* if index */
109 int ifc_mtu; /* if mtu */
110 int ifc_metric; /* if metric */
111 u_int ifc_flags; /* flags */
112 short ifc_cflags; /* IFC_XXX */
113 struct in6_addr ifc_mylladdr; /* my link-local address */
114 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
115 struct iff *ifc_filter; /* filter structure */
116 struct ifac *ifc_addr; /* list of AF_INET6 addresses */
117 int ifc_joined; /* joined to ff02::9 */
120 struct ifac { /* Adddress associated to an interface */
121 struct ifc *ifa_conf; /* back pointer */
122 struct ifac *ifa_next;
123 struct in6_addr ifa_addr; /* address */
124 struct in6_addr ifa_raddr; /* remote address, valid in p2p */
125 int ifa_plen; /* prefix length */
130 struct in6_addr iff_addr;
132 struct iff *iff_next;
136 int nifc; /* number of valid ifc's */
137 struct ifc **index2ifc;
139 struct ifc *loopifcp = NULL; /* pointing to loopback */
140 int loopifindex = 0; /* ditto */
141 fd_set sockvec; /* vector to select() for receiving */
142 int rtsock; /* the routing socket */
143 int ripsock; /* socket to send/receive RIP datagram */
145 struct rip6 *ripbuf; /* packet buffer for sending */
148 * Maintain the routes in a linked list. When the number of the routes
149 * grows, somebody would like to introduce a hash based or a radix tree
150 * based structure. I believe the number of routes handled by RIP is
151 * limited and I don't have to manage a complex data structure, however.
153 * One of the major drawbacks of the linear linked list is the difficulty
154 * of representing the relationship between a couple of routes. This may
155 * be a significant problem when we have to support route aggregation with
156 * supressing the specifices covered by the aggregate.
160 struct riprt *rrt_next; /* next destination */
161 struct riprt *rrt_same; /* same destination - future use */
162 struct netinfo6 rrt_info; /* network info */
163 struct in6_addr rrt_gw; /* gateway */
164 u_long rrt_flags; /* kernel routing table flags */
165 u_long rrt_rflags; /* route6d routing table flags */
166 time_t rrt_t; /* when the route validated */
167 int rrt_index; /* ifindex from which this route got */
170 struct riprt *riprt = 0;
172 int dflag = 0; /* debug flag */
173 int qflag = 0; /* quiet flag */
174 int nflag = 0; /* don't update kernel routing table */
175 int aflag = 0; /* age out even the statically defined routes */
176 int hflag = 0; /* don't split horizon */
177 int lflag = 0; /* exchange site local routes */
178 int sflag = 0; /* announce static routes w/ split horizon */
179 int Sflag = 0; /* announce static routes to every interface */
180 unsigned long routetag = 0; /* route tag attached on originating case */
182 char *filter[MAXFILTER];
183 int filtertype[MAXFILTER];
188 struct sockaddr_storage ripsin;
190 struct rtentry rtentry;
193 time_t nextalarm = 0;
194 time_t sup_trig_update = 0;
200 static u_long seq = 0;
203 volatile sig_atomic_t seenalrm;
204 volatile sig_atomic_t seenquit;
205 volatile sig_atomic_t seenusr1;
207 #define RRTF_AGGREGATE 0x08000000
208 #define RRTF_NOADVERTISE 0x10000000
209 #define RRTF_NH_NOT_LLADDR 0x20000000
210 #define RRTF_SENDANYWAY 0x40000000
211 #define RRTF_CHANGED 0x80000000
213 int main __P((int, char **));
214 void sighandler __P((int));
215 void ripalarm __P((void));
216 void riprecv __P((void));
217 void ripsend __P((struct ifc *, struct sockaddr_in6 *, int));
218 int out_filter __P((struct riprt *, struct ifc *));
219 void init __P((void));
220 void sockopt __P((struct ifc *));
221 void ifconfig __P((void));
222 void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int));
223 void rtrecv __P((void));
224 int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *,
225 const struct sockaddr_in6 *));
226 int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *,
227 const struct sockaddr_in6 *));
228 void filterconfig __P((void));
229 int getifmtu __P((int));
230 const char *rttypes __P((struct rt_msghdr *));
231 const char *rtflags __P((struct rt_msghdr *));
232 const char *ifflags __P((int));
233 int ifrt __P((struct ifc *, int));
234 void ifrt_p2p __P((struct ifc *, int));
235 void applymask __P((struct in6_addr *, struct in6_addr *));
236 void applyplen __P((struct in6_addr *, int));
237 void ifrtdump __P((int));
238 void ifdump __P((int));
239 void ifdump0 __P((FILE *, const struct ifc *));
240 void rtdump __P((int));
241 void rt_entry __P((struct rt_msghdr *, int));
242 void rtdexit __P((void));
243 void riprequest __P((struct ifc *, struct netinfo6 *, int,
244 struct sockaddr_in6 *));
245 void ripflush __P((struct ifc *, struct sockaddr_in6 *));
246 void sendrequest __P((struct ifc *));
247 int sin6mask2len __P((const struct sockaddr_in6 *));
248 int mask2len __P((const struct in6_addr *, int));
249 int sendpacket __P((struct sockaddr_in6 *, int));
250 int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *));
251 int delroute __P((struct netinfo6 *, struct in6_addr *));
252 struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *));
253 void krtread __P((int));
254 int tobeadv __P((struct riprt *, struct ifc *));
255 char *allocopy __P((char *));
256 char *hms __P((void));
257 const char *inet6_n2p __P((const struct in6_addr *));
258 struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int));
259 struct in6_addr *plen2mask __P((int));
260 struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **));
261 int ripinterval __P((int));
262 time_t ripsuptrig __P((void));
263 void fatal __P((const char *, ...))
264 __attribute__((__format__(__printf__, 1, 2)));
265 void trace __P((int, const char *, ...))
266 __attribute__((__format__(__printf__, 2, 3)));
267 void tracet __P((int, const char *, ...))
268 __attribute__((__format__(__printf__, 2, 3)));
269 unsigned int if_maxindex __P((void));
270 struct ifc *ifc_find __P((char *));
271 struct iff *iff_find __P((struct ifc *, int));
272 void setindex2ifc __P((int, struct ifc *));
274 #define MALLOC(type) ((type *)malloc(sizeof(type)))
284 sigset_t mask, omask;
289 progname = strrchr(*argv, '/');
296 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -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");
319 if ((rtlog = fopen(optarg, "w")) == NULL) {
320 fatal("Can not write to routelog");
324 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
325 FLAG('a', aflag, 1); break;
326 FLAG('d', dflag, 1); break;
327 FLAG('D', dflag, 2); break;
328 FLAG('h', hflag, 1); break;
329 FLAG('l', lflag, 1); break;
330 FLAG('n', nflag, 1); break;
331 FLAG('q', qflag, 1); break;
332 FLAG('s', sflag, 1); break;
333 FLAG('S', Sflag, 1); break;
336 fatal("Invalid option specified, terminating");
343 fatal("bogus extra arguments");
349 fprintf(stderr, "No kernel update is allowed\n");
351 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
354 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
356 memset(ripbuf, 0, RIP6_MAXMTU);
357 ripbuf->rip6_cmd = RIP6_RESPONSE;
358 ripbuf->rip6_vers = RIP6_VERSION;
359 ripbuf->rip6_res1[0] = 0;
360 ripbuf->rip6_res1[1] = 0;
364 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
365 if (ifcp->ifc_index < 0) {
367 "No ifindex found at %s (no link-local address?)\n",
374 if (loopifcp == NULL) {
375 fatal("No loopback found");
378 loopifindex = loopifcp->ifc_index;
379 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
388 if (daemon(0, 0) < 0) {
402 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
403 fprintf(pidfile, "%d\n", pid);
407 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
411 memset(ripbuf, 0, RIP6_MAXMTU);
412 ripbuf->rip6_cmd = RIP6_RESPONSE;
413 ripbuf->rip6_vers = RIP6_VERSION;
414 ripbuf->rip6_res1[0] = 0;
415 ripbuf->rip6_res1[1] = 0;
417 if (signal(SIGALRM, sighandler) == SIG_ERR ||
418 signal(SIGQUIT, sighandler) == SIG_ERR ||
419 signal(SIGTERM, sighandler) == SIG_ERR ||
420 signal(SIGUSR1, sighandler) == SIG_ERR ||
421 signal(SIGHUP, sighandler) == SIG_ERR ||
422 signal(SIGINT, sighandler) == SIG_ERR) {
427 * To avoid rip packet congestion (not on a cable but in this
428 * process), wait for a moment to send the first RIP6_RESPONSE
431 alarm(ripinterval(INIT_INTERVAL6));
433 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
434 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
438 syslog(LOG_INFO, "**** Started ****");
440 sigaddset(&mask, SIGALRM);
460 FD_COPY(&sockvec, &recvec);
462 switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
464 if (errno != EINTR) {
472 if (FD_ISSET(ripsock, &recvec)) {
473 sigprocmask(SIG_BLOCK, &mask, &omask);
475 sigprocmask(SIG_SETMASK, &omask, NULL);
477 if (FD_ISSET(rtsock, &recvec)) {
478 sigprocmask(SIG_BLOCK, &mask, &omask);
480 sigprocmask(SIG_SETMASK, &omask, NULL);
509 * gracefully exits after resetting sockopts.
518 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
519 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
520 delroute(&rrt->rrt_info, &rrt->rrt_gw);
525 syslog(LOG_INFO, "**** Terminated ****");
531 * Called periodically:
532 * 1. age out the learned route. remove it if necessary.
533 * 2. submit RIP6_RESPONSE packets.
534 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
535 * to invoke this function in every 1 or 5 or 10 seconds only to age the
536 * routes more precisely.
543 struct riprt *rrt, *rrt_prev, *rrt_next;
544 time_t t_lifetime, t_holddown;
546 /* age the RIP routes */
548 t_lifetime = time(NULL) - RIP_LIFETIME;
549 t_holddown = t_lifetime - RIP_HOLDDOWN;
550 for (rrt = riprt; rrt; rrt = rrt_next) {
551 rrt_next = rrt->rrt_next;
553 if (rrt->rrt_t == 0) {
557 if (rrt->rrt_t < t_holddown) {
559 rrt_prev->rrt_next = rrt->rrt_next;
561 riprt = rrt->rrt_next;
563 delroute(&rrt->rrt_info, &rrt->rrt_gw);
567 if (rrt->rrt_t < t_lifetime)
568 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
572 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
573 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
574 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
576 alarm(ripinterval(SUPPLY_INTERVAL6));
582 int i, int0, int255, error;
583 struct addrinfo hints, *res;
586 ifc = (struct ifc *)NULL;
588 nindex2ifc = 0; /*initial guess*/
590 snprintf(port, sizeof(port), "%d", RIP6_PORT);
592 memset(&hints, 0, sizeof(hints));
593 hints.ai_family = PF_INET6;
594 hints.ai_socktype = SOCK_DGRAM;
595 hints.ai_flags = AI_PASSIVE;
596 error = getaddrinfo(NULL, port, &hints, &res);
598 fatal("%s", gai_strerror(error));
602 fatal(":: resolved to multiple address");
606 int0 = 0; int255 = 255;
607 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
612 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
616 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
617 &int255, sizeof(int255)) < 0) {
618 fatal("rip IPV6_MULTICAST_HOPS");
621 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
622 &int0, sizeof(int0)) < 0) {
623 fatal("rip IPV6_MULTICAST_LOOP");
628 #ifdef IPV6_RECVPKTINFO
629 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
631 fatal("rip IPV6_RECVPKTINFO");
634 #else /* old adv. API */
635 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
637 fatal("rip IPV6_PKTINFO");
642 memset(&hints, 0, sizeof(hints));
643 hints.ai_family = PF_INET6;
644 hints.ai_socktype = SOCK_DGRAM;
645 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
647 fatal("%s", gai_strerror(error));
651 fatal("%s resolved to multiple address", RIP6_DEST);
654 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
659 memset(&sockvec, 0, sizeof(sockvec));
661 FD_SET(ripsock, &sockvec);
664 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
665 fatal("route socket");
668 FD_SET(rtsock, &sockvec);
670 rtsock = -1; /*just for safety */
674 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
677 * ripflush flushes the rip datagram stored in the rip buffer
680 static struct netinfo6 *np;
685 struct sockaddr_in6 *sin;
691 tracet(1, "Send(%s): info(%d) to %s.%d\n",
693 inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
695 tracet(1, "Send: info(%d) to %s.%d\n",
696 nrt, inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
698 np = ripbuf->rip6_nets;
699 for (i = 0; i < nrt; i++, np++) {
700 if (np->rip6_metric == NEXTHOP_METRIC) {
701 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
702 trace(2, " NextHop reset");
704 trace(2, " NextHop %s",
705 inet6_n2p(&np->rip6_dest));
708 trace(2, " %s/%d[%d]",
709 inet6_n2p(&np->rip6_dest),
710 np->rip6_plen, np->rip6_metric);
713 trace(2, " tag=0x%04x",
714 ntohs(np->rip6_tag) & 0xffff);
719 error = sendpacket(sin, RIPSIZE(nrt));
720 if (error == EAFNOSUPPORT) {
721 /* Protocol not supported */
722 tracet(1, "Could not send info to %s (%s): "
724 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
725 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
727 nrt = 0; np = ripbuf->rip6_nets;
731 * Generate RIP6_RESPONSE packets and send them.
734 ripsend(ifcp, sin, flag)
736 struct sockaddr_in6 *sin;
740 struct in6_addr *nh; /* next hop */
745 * Request from non-link local address is not
746 * a regular route6d update.
748 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
749 sizeof(struct udphdr) -
750 sizeof(struct rip6) + sizeof(struct netinfo6)) /
751 sizeof(struct netinfo6);
752 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
753 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
754 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
756 /* Put the route to the buffer */
764 if (nrt) /* Send last packet */
769 if ((flag & RRTF_SENDANYWAY) == 0 &&
770 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
774 if (iff_find(ifcp, 'N') != NULL)
777 /* -T: generate default route only */
778 if (iff_find(ifcp, 'T') != NULL) {
779 struct netinfo6 rrt_info;
780 memset(&rrt_info, 0, sizeof(struct netinfo6));
781 rrt_info.rip6_dest = in6addr_any;
782 rrt_info.rip6_plen = 0;
783 rrt_info.rip6_metric = 1;
784 rrt_info.rip6_metric += ifcp->ifc_metric;
785 rrt_info.rip6_tag = htons(routetag & 0xffff);
786 np = ripbuf->rip6_nets;
793 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
794 sizeof(struct udphdr) -
795 sizeof(struct rip6) + sizeof(struct netinfo6)) /
796 sizeof(struct netinfo6);
798 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
799 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
800 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
803 /* Need to check filter here */
804 if (out_filter(rrt, ifcp) == 0)
807 /* Check split horizon and other conditions */
808 if (tobeadv(rrt, ifcp) == 0)
811 /* Only considers the routes with flag if specified */
812 if ((flag & RRTF_CHANGED) &&
813 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
817 if (rrt->rrt_index == ifcp->ifc_index &&
818 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
819 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
820 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
821 if (nrt == maxrte - 2)
823 np->rip6_dest = rrt->rrt_gw;
824 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
825 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
828 np->rip6_metric = NEXTHOP_METRIC;
832 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
833 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
834 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
836 if (nrt == maxrte - 2)
838 memset(np, 0, sizeof(struct netinfo6));
839 np->rip6_metric = NEXTHOP_METRIC;
844 /* Put the route to the buffer */
852 if (nrt) /* Send last packet */
857 * outbound filter logic, per-route/interface.
860 out_filter(rrt, ifcp)
869 * -A: filter out less specific routes, if we have aggregated
872 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
873 if (iffp->iff_type != 'A')
875 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
877 ia = rrt->rrt_info.rip6_dest;
878 applyplen(&ia, iffp->iff_plen);
879 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
884 * if it is an aggregated route, advertise it only to the
885 * interfaces specified on -A.
887 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
889 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
890 if (iffp->iff_type != 'A')
892 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
893 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
904 * -O: advertise only if prefix matches the configured prefix.
906 if (iff_find(ifcp, 'O')) {
908 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
909 if (iffp->iff_type != 'O')
911 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
913 ia = rrt->rrt_info.rip6_dest;
914 applyplen(&ia, iffp->iff_plen);
915 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
924 /* the prefix should be advertised */
929 * Determine if the route is to be advertised on the specified interface.
930 * It checks options specified in the arguments and the split horizon rule.
938 /* Special care for static routes */
939 if (rrt->rrt_flags & RTF_STATIC) {
940 /* XXX don't advertise reject/blackhole routes */
941 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
944 if (Sflag) /* Yes, advertise it anyway */
946 if (sflag && rrt->rrt_index != ifcp->ifc_index)
950 /* Regular split horizon */
951 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
957 * Send a rip packet actually.
961 struct sockaddr_in6 *sin;
965 * MSG_DONTROUTE should not be specified when it responds with a
966 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
973 struct in6_pktinfo *pi;
975 struct sockaddr_in6 sincopy;
977 /* do not overwrite the given sin */
981 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)
982 || IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) {
983 idx = IN6_LINKLOCAL_IFINDEX(sin->sin6_addr);
984 SET_IN6_LINKLOCAL_IFINDEX(sin->sin6_addr, 0);
988 m.msg_name = (caddr_t)sin;
989 m.msg_namelen = sizeof(*sin);
990 iov[0].iov_base = (caddr_t)ripbuf;
991 iov[0].iov_len = len;
995 m.msg_control = NULL;
996 m.msg_controllen = 0;
998 memset(cmsgbuf, 0, sizeof(cmsgbuf));
999 cm = (struct cmsghdr *)cmsgbuf;
1000 m.msg_control = (caddr_t)cm;
1001 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1003 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1004 cm->cmsg_level = IPPROTO_IPV6;
1005 cm->cmsg_type = IPV6_PKTINFO;
1006 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1007 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1008 pi->ipi6_ifindex = idx;
1011 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1012 trace(1, "sendmsg: %s\n", strerror(errno));
1020 * Receive and process RIP packets. Update the routes/kernel forwarding
1021 * table if necessary.
1026 struct ifc *ifcp, *ic;
1027 struct sockaddr_in6 fsock;
1028 struct in6_addr nh; /* next hop */
1030 struct netinfo6 *np, *nq;
1032 int len, nn, need_trigger, idx;
1033 char buf[4 * RIP6_MAXMTU];
1037 struct iovec iov[2];
1038 u_char cmsgbuf[256];
1039 struct in6_pktinfo *pi;
1043 time_t t_half_lifetime;
1047 m.msg_name = (caddr_t)&fsock;
1048 m.msg_namelen = sizeof(fsock);
1049 iov[0].iov_base = (caddr_t)buf;
1050 iov[0].iov_len = sizeof(buf);
1053 cm = (struct cmsghdr *)cmsgbuf;
1054 m.msg_control = (caddr_t)cm;
1055 m.msg_controllen = sizeof(cmsgbuf);
1056 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1061 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1063 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1064 if (cm->cmsg_level == IPPROTO_IPV6 &&
1065 cm->cmsg_type == IPV6_PKTINFO) {
1066 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1067 idx = pi->ipi6_ifindex;
1071 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1072 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1074 nh = fsock.sin6_addr;
1075 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1076 sizeof(struct netinfo6);
1077 rp = (struct rip6 *)buf;
1080 if (rp->rip6_vers != RIP6_VERSION) {
1081 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1084 if (rp->rip6_cmd == RIP6_REQUEST) {
1085 if (idx && idx < nindex2ifc) {
1086 ifcp = index2ifc[idx];
1087 riprequest(ifcp, np, nn, &fsock);
1089 riprequest(NULL, np, nn, &fsock);
1094 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1095 trace(1, "Packets from non-ll addr: %s\n",
1096 inet6_n2p(&fsock.sin6_addr));
1097 return; /* Ignore packets from non-link-local addr */
1099 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1100 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1102 trace(1, "Packets to unknown interface index %d\n", idx);
1103 return; /* Ignore it */
1105 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1106 return; /* The packet is from me; ignore */
1107 if (rp->rip6_cmd != RIP6_RESPONSE) {
1108 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1113 if (iff_find(ifcp, 'N') != NULL)
1116 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1117 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1120 t_half_lifetime = t - (RIP_LIFETIME/2);
1121 for (; nn; nn--, np++) {
1122 if (np->rip6_metric == NEXTHOP_METRIC) {
1123 /* modify neighbor address */
1124 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1126 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1127 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1128 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1129 nh = fsock.sin6_addr;
1130 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1132 nh = fsock.sin6_addr;
1133 trace(1, "\tInvalid Nexthop: %s\n",
1134 inet6_n2p(&np->rip6_dest));
1138 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1139 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1140 inet6_n2p(&np->rip6_dest),
1141 np->rip6_plen, np->rip6_metric);
1144 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1145 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1146 inet6_n2p(&np->rip6_dest),
1147 np->rip6_plen, np->rip6_metric);
1150 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1151 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1152 inet6_n2p(&np->rip6_dest),
1153 np->rip6_plen, np->rip6_metric);
1156 /* may need to pass sitelocal prefix in some case, however*/
1157 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1158 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1159 inet6_n2p(&np->rip6_dest),
1160 np->rip6_plen, np->rip6_metric);
1163 trace(2, "\tnetinfo6: %s/%d [%d]",
1164 inet6_n2p(&np->rip6_dest),
1165 np->rip6_plen, np->rip6_metric);
1167 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1170 applyplen(&ia, np->rip6_plen);
1171 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1172 trace(2, " [junk outside prefix]");
1176 * -L: listen only if the prefix matches the configuration
1178 ok = 1; /* if there's no L filter, it is ok */
1179 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
1180 if (iffp->iff_type != 'L')
1183 if (np->rip6_plen < iffp->iff_plen)
1185 /* special rule: ::/0 means default, not "in /0" */
1186 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1189 applyplen(&ia, iffp->iff_plen);
1190 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1196 trace(2, " (filtered)\n");
1202 np->rip6_metric += ifcp->ifc_metric;
1203 if (np->rip6_metric > HOPCNT_INFINITY6)
1204 np->rip6_metric = HOPCNT_INFINITY6;
1206 applyplen(&np->rip6_dest, np->rip6_plen);
1207 if ((rrt = rtsearch(np, NULL)) != NULL) {
1208 if (rrt->rrt_t == 0)
1209 continue; /* Intf route has priority */
1210 nq = &rrt->rrt_info;
1211 if (nq->rip6_metric > np->rip6_metric) {
1212 if (rrt->rrt_index == ifcp->ifc_index &&
1213 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1214 /* Small metric from the same gateway */
1215 nq->rip6_metric = np->rip6_metric;
1217 /* Better route found */
1218 rrt->rrt_index = ifcp->ifc_index;
1219 /* Update routing table */
1220 delroute(nq, &rrt->rrt_gw);
1223 addroute(rrt, &nh, ifcp);
1225 rrt->rrt_rflags |= RRTF_CHANGED;
1228 } else if (nq->rip6_metric < np->rip6_metric &&
1229 rrt->rrt_index == ifcp->ifc_index &&
1230 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1231 /* Got worse route from same gw */
1232 nq->rip6_metric = np->rip6_metric;
1234 rrt->rrt_rflags |= RRTF_CHANGED;
1236 } else if (nq->rip6_metric == np->rip6_metric &&
1237 np->rip6_metric < HOPCNT_INFINITY6) {
1238 if (rrt->rrt_index == ifcp->ifc_index &&
1239 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1240 /* same metric, same route from same gw */
1242 } else if (rrt->rrt_t < t_half_lifetime) {
1243 /* Better route found */
1244 rrt->rrt_index = ifcp->ifc_index;
1245 /* Update routing table */
1246 delroute(nq, &rrt->rrt_gw);
1249 addroute(rrt, &nh, ifcp);
1250 rrt->rrt_rflags |= RRTF_CHANGED;
1255 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1256 * do not update age value. Do nothing.
1258 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1259 /* Got a new valid route */
1260 if ((rrt = MALLOC(struct riprt)) == NULL) {
1261 fatal("malloc: struct riprt");
1264 memset(rrt, 0, sizeof(*rrt));
1265 nq = &rrt->rrt_info;
1267 rrt->rrt_same = NULL;
1268 rrt->rrt_index = ifcp->ifc_index;
1269 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1272 applyplen(&nq->rip6_dest, nq->rip6_plen);
1273 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1274 rrt->rrt_flags |= RTF_HOST;
1276 /* Put the route to the list */
1277 rrt->rrt_next = riprt;
1279 /* Update routing table */
1280 addroute(rrt, &nh, ifcp);
1281 rrt->rrt_rflags |= RRTF_CHANGED;
1286 /* XXX need to care the interval between triggered updates */
1288 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1289 for (ic = ifc; ic; ic = ic->ifc_next) {
1290 if (ifcp->ifc_index == ic->ifc_index)
1292 if (ic->ifc_flags & IFF_UP)
1293 ripsend(ic, &ic->ifc_ripsin,
1297 /* Reset the flag */
1298 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1299 rrt->rrt_rflags &= ~RRTF_CHANGED;
1304 * Send all routes request packet to the specified interface.
1310 struct netinfo6 *np;
1313 if (ifcp->ifc_flags & IFF_LOOPBACK)
1315 ripbuf->rip6_cmd = RIP6_REQUEST;
1316 np = ripbuf->rip6_nets;
1317 memset(np, 0, sizeof(struct netinfo6));
1318 np->rip6_metric = HOPCNT_INFINITY6;
1319 tracet(1, "Send rtdump Request to %s (%s)\n",
1320 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1321 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1322 if (error == EAFNOSUPPORT) {
1323 /* Protocol not supported */
1324 tracet(1, "Could not send rtdump Request to %s (%s): "
1325 "set IFF_UP to 0\n",
1326 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1327 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1329 ripbuf->rip6_cmd = RIP6_RESPONSE;
1333 * Process a RIP6_REQUEST packet.
1336 riprequest(ifcp, np, nn, sin)
1338 struct netinfo6 *np;
1340 struct sockaddr_in6 *sin;
1345 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1346 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1347 /* Specific response, don't split-horizon */
1348 trace(1, "\tRIP Request\n");
1349 for (i = 0; i < nn; i++, np++) {
1350 rrt = rtsearch(np, NULL);
1352 np->rip6_metric = rrt->rrt_info.rip6_metric;
1354 np->rip6_metric = HOPCNT_INFINITY6;
1356 (void)sendpacket(sin, RIPSIZE(nn));
1359 /* Whole routing table dump */
1360 trace(1, "\tRIP Request -- whole routing table\n");
1361 ripsend(ifcp, sin, RRTF_SENDANYWAY);
1365 * Get information of each interface.
1370 struct ifaddrs *ifap, *ifa;
1372 struct ipv6_mreq mreq;
1375 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1380 if (getifaddrs(&ifap) != 0) {
1381 fatal("getifaddrs");
1385 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1386 if (ifa->ifa_addr->sa_family != AF_INET6)
1388 ifcp = ifc_find(ifa->ifa_name);
1389 /* we are interested in multicast-capable interfaces */
1390 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1394 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1395 fatal("malloc: struct ifc");
1398 memset(ifcp, 0, sizeof(*ifcp));
1399 ifcp->ifc_index = -1;
1400 ifcp->ifc_next = ifc;
1403 ifcp->ifc_name = allocopy(ifa->ifa_name);
1405 ifcp->ifc_filter = 0;
1406 ifcp->ifc_flags = ifa->ifa_flags;
1407 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1408 ifflags(ifcp->ifc_flags));
1409 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1412 /* update flag, this may be up again */
1413 if (ifcp->ifc_flags != ifa->ifa_flags) {
1414 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1415 ifflags(ifcp->ifc_flags));
1416 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1417 ifcp->ifc_cflags |= IFC_CHANGED;
1419 ifcp->ifc_flags = ifa->ifa_flags;
1421 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
1422 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1423 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1424 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1425 mreq.ipv6mr_interface = ifcp->ifc_index;
1426 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1427 &mreq, sizeof(mreq)) < 0) {
1428 fatal("IPV6_JOIN_GROUP");
1431 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1440 ifconfig1(name, sa, ifcp, s)
1442 const struct sockaddr *sa;
1446 struct in6_ifreq ifr;
1447 const struct sockaddr_in6 *sin;
1452 sin = (const struct sockaddr_in6 *)sa;
1453 ifr.ifr_addr = *sin;
1454 strcpy(ifr.ifr_name, name);
1455 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1456 fatal("ioctl: SIOCGIFNETMASK_IN6");
1459 plen = sin6mask2len(&ifr.ifr_addr);
1460 if ((ifa = ifa_match(ifcp, &sin->sin6_addr, plen)) != NULL) {
1461 /* same interface found */
1462 /* need check if something changed */
1463 /* XXX not yet implemented */
1467 * New address is found
1469 if ((ifa = MALLOC(struct ifac)) == NULL) {
1470 fatal("malloc: struct ifac");
1473 memset(ifa, 0, sizeof(*ifa));
1474 ifa->ifa_conf = ifcp;
1475 ifa->ifa_next = ifcp->ifc_addr;
1476 ifcp->ifc_addr = ifa;
1477 ifa->ifa_addr = sin->sin6_addr;
1478 ifa->ifa_plen = plen;
1479 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1480 ifr.ifr_addr = *sin;
1481 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1482 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1485 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
1486 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
1487 trace(1, "found address %s/%d -- %s\n",
1488 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
1490 trace(1, "found address %s/%d\n",
1491 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
1493 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1494 ifcp->ifc_mylladdr = ifa->ifa_addr;
1495 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
1496 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1497 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1499 setindex2ifc(ifcp->ifc_index, ifcp);
1500 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1501 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1502 ifcp->ifc_mtu = RIP6_MAXMTU;
1503 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1504 fatal("ioctl: SIOCGIFMETRIC");
1507 ifcp->ifc_metric = ifr.ifr_metric;
1508 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1509 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1511 ifcp->ifc_cflags |= IFC_CHANGED;
1515 * Receive and process routing messages.
1516 * Update interface information as necesssary.
1523 struct rt_msghdr *rtm;
1524 struct ifa_msghdr *ifam;
1525 struct if_msghdr *ifm;
1527 struct ifc *ifcp, *ic;
1528 int iface = 0, rtable = 0;
1529 struct sockaddr_in6 *rta[RTAX_MAX];
1530 struct sockaddr_in6 mask;
1534 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1535 perror("read from rtsock");
1538 if (len < sizeof(*rtm)) {
1539 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1540 len, (u_long)sizeof(*rtm));
1544 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1545 /* safety against bogus message */
1546 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1547 trace(1, "bogus rtmsg: length=%d\n",
1548 ((struct rt_msghdr *)p)->rtm_msglen);
1554 switch (((struct rt_msghdr *)p)->rtm_type) {
1557 ifam = (struct ifa_msghdr *)p;
1558 addrs = ifam->ifam_addrs;
1559 q = (char *)(ifam + 1);
1562 ifm = (struct if_msghdr *)p;
1563 addrs = ifm->ifm_addrs;
1564 q = (char *)(ifm + 1);
1567 rtm = (struct rt_msghdr *)p;
1568 addrs = rtm->rtm_addrs;
1569 q = (char *)(rtm + 1);
1570 if (rtm->rtm_version != RTM_VERSION) {
1571 trace(1, "unexpected rtmsg version %d "
1573 rtm->rtm_version, RTM_VERSION);
1576 if (rtm->rtm_pid == pid) {
1578 trace(1, "rtmsg looped back to me, ignored\n");
1584 memset(&rta, 0, sizeof(rta));
1585 for (i = 0; i < RTAX_MAX; i++) {
1586 if (addrs & (1 << i)) {
1587 rta[i] = (struct sockaddr_in6 *)q;
1588 q += ROUNDUP(rta[i]->sin6_len);
1592 trace(1, "rtsock: %s (addrs=%x)\n",
1593 rttypes((struct rt_msghdr *)p), addrs);
1596 i < ((struct rt_msghdr *)p)->rtm_msglen;
1598 fprintf(stderr, "%02x ", p[i] & 0xff);
1599 if (i % 16 == 15) fprintf(stderr, "\n");
1601 fprintf(stderr, "\n");
1607 * We may be able to optimize by using ifm->ifm_index or
1608 * ifam->ifam_index. For simplicity we don't do that here.
1610 switch (((struct rt_msghdr *)p)->rtm_type) {
1623 /* nothing to be done here */
1624 trace(1, "\tnothing to be done, ignored\n");
1629 if (rta[RTAX_DST] == NULL) {
1630 trace(1, "\tno destination, ignored\n");
1633 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1634 trace(1, "\taf mismatch, ignored\n");
1637 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1638 trace(1, "\tlinklocal destination, ignored\n");
1641 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1642 trace(1, "\tloopback destination, ignored\n");
1643 continue; /* Loopback */
1645 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1646 trace(1, "\tmulticast destination, ignored\n");
1652 switch (((struct rt_msghdr *)p)->rtm_type) {
1661 /* should already be handled */
1662 fatal("rtrecv: never reach here");
1665 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1666 trace(1, "\tsome of dst/gw/netamsk are "
1667 "unavailable, ignored\n");
1670 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1671 mask.sin6_len = sizeof(mask);
1672 memset(&mask.sin6_addr, 0xff,
1673 sizeof(mask.sin6_addr));
1674 rta[RTAX_NETMASK] = &mask;
1675 } else if (!rta[RTAX_NETMASK]) {
1676 trace(1, "\tsome of dst/gw/netamsk are "
1677 "unavailable, ignored\n");
1680 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1681 rta[RTAX_NETMASK]) == 0) {
1682 rtable++; /*just to be sure*/
1687 trace(1, "\tnot supported yet, ignored\n");
1690 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1691 trace(1, "\tno netmask or ifa given, ignored\n");
1694 if (ifam->ifam_index < nindex2ifc)
1695 ifcp = index2ifc[ifam->ifam_index];
1699 trace(1, "\tinvalid ifam_index %d, ignored\n",
1703 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1708 trace(1, "\tnot supported yet, ignored\n");
1715 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1717 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
1718 if (ifcp->ifc_cflags & IFC_CHANGED) {
1719 if (ifrt(ifcp, 1)) {
1720 for (ic = ifc; ic; ic = ic->ifc_next) {
1721 if (ifcp->ifc_index == ic->ifc_index)
1723 if (ic->ifc_flags & IFF_UP)
1724 ripsend(ic, &ic->ifc_ripsin,
1727 /* Reset the flag */
1728 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1729 rrt->rrt_rflags &= ~RRTF_CHANGED;
1731 ifcp->ifc_cflags &= ~IFC_CHANGED;
1735 trace(1, "rtsock: read routing table again\n");
1741 * remove specified route from the internal routing table.
1744 rt_del(sdst, sgw, smask)
1745 const struct sockaddr_in6 *sdst;
1746 const struct sockaddr_in6 *sgw;
1747 const struct sockaddr_in6 *smask;
1749 const struct in6_addr *dst = NULL;
1750 const struct in6_addr *gw = NULL;
1752 struct netinfo6 ni6;
1753 struct riprt *rrt = NULL;
1756 if (sdst->sin6_family != AF_INET6) {
1757 trace(1, "\tother AF, ignored\n");
1760 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1761 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1762 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1763 trace(1, "\taddress %s not interesting, ignored\n",
1764 inet6_n2p(&sdst->sin6_addr));
1767 dst = &sdst->sin6_addr;
1768 if (sgw->sin6_family == AF_INET6) {
1770 gw = &sgw->sin6_addr;
1771 prefix = sin6mask2len(smask);
1772 } else if (sgw->sin6_family == AF_LINK) {
1774 * Interface route... a hard case. We need to get the prefix
1775 * length from the kernel, but we now are parsing rtmsg.
1776 * We'll purge matching routes from my list, then get the
1779 struct riprt *longest;
1780 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1783 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
1784 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1786 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1788 || longest->rrt_info.rip6_plen <
1789 rrt->rrt_info.rip6_plen) {
1796 trace(1, "\tno matching interface route found\n");
1799 gw = &in6addr_loopback;
1800 prefix = rrt->rrt_info.rip6_plen;
1802 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1806 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1807 trace(1, "gw %s\n", inet6_n2p(gw));
1808 t_lifetime = time(NULL) - RIP_LIFETIME;
1809 /* age route for interface address */
1810 memset(&ni6, 0, sizeof(ni6));
1811 ni6.rip6_dest = *dst;
1812 ni6.rip6_plen = prefix;
1813 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1814 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
1816 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
1817 trace(1, "\tno route found\n");
1821 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
1822 trace(1, "\tyou can delete static routes only\n");
1825 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
1826 trace(1, "\tgw mismatch: %s <-> ",
1827 inet6_n2p(&rrt->rrt_gw));
1828 trace(1, "%s\n", inet6_n2p(gw));
1830 trace(1, "\troute found, age it\n");
1831 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1832 rrt->rrt_t = t_lifetime;
1833 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1840 * remove specified address from internal interface/routing table.
1843 rt_deladdr(ifcp, sifa, smask)
1845 const struct sockaddr_in6 *sifa;
1846 const struct sockaddr_in6 *smask;
1848 const struct in6_addr *addr = NULL;
1850 struct ifac *ifa = NULL;
1851 struct netinfo6 ni6;
1852 struct riprt *rrt = NULL;
1856 if (sifa->sin6_family != AF_INET6) {
1857 trace(1, "\tother AF, ignored\n");
1860 addr = &sifa->sin6_addr;
1861 prefix = sin6mask2len(smask);
1863 trace(1, "\tdeleting %s/%d from %s\n",
1864 inet6_n2p(addr), prefix, ifcp->ifc_name);
1865 ifa = ifa_match(ifcp, addr, prefix);
1867 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1868 inet6_n2p(addr), prefix, ifcp->ifc_name);
1871 if (ifa->ifa_conf != ifcp) {
1872 trace(1, "\taddress table corrupt: back pointer does not match "
1874 ifcp->ifc_name, ifa->ifa_conf->ifc_name);
1877 /* remove ifa from interface */
1878 if (ifcp->ifc_addr == ifa)
1879 ifcp->ifc_addr = ifa->ifa_next;
1882 for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
1883 if (p->ifa_next == ifa) {
1884 p->ifa_next = ifa->ifa_next;
1889 ifa->ifa_next = NULL;
1890 ifa->ifa_conf = NULL;
1891 t_lifetime = time(NULL) - RIP_LIFETIME;
1892 /* age route for interface address */
1893 memset(&ni6, 0, sizeof(ni6));
1894 ni6.rip6_dest = ifa->ifa_addr;
1895 ni6.rip6_plen = ifa->ifa_plen;
1896 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
1897 trace(1, "\tfind interface route %s/%d on %d\n",
1898 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
1899 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1900 struct in6_addr none;
1901 memset(&none, 0, sizeof(none));
1902 if (rrt->rrt_index == ifcp->ifc_index &&
1903 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
1904 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
1905 trace(1, "\troute found, age it\n");
1906 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1907 rrt->rrt_t = t_lifetime;
1908 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1912 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1913 inet6_n2p(&rrt->rrt_info.rip6_dest),
1914 rrt->rrt_info.rip6_plen,
1918 trace(1, "\tno interface route found\n");
1919 /* age route for p2p destination */
1920 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1921 memset(&ni6, 0, sizeof(ni6));
1922 ni6.rip6_dest = ifa->ifa_raddr;
1923 ni6.rip6_plen = 128;
1924 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1925 trace(1, "\tfind p2p route %s/%d on %d\n",
1926 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
1928 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1929 if (rrt->rrt_index == ifcp->ifc_index &&
1930 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
1931 trace(1, "\troute found, age it\n");
1932 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1933 rrt->rrt_t = t_lifetime;
1934 rrt->rrt_info.rip6_metric =
1939 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1940 inet6_n2p(&rrt->rrt_info.rip6_dest),
1941 rrt->rrt_info.rip6_plen,
1945 trace(1, "\tno p2p route found\n");
1947 return updated ? 0 : -1;
1951 * Get each interface address and put those interface routes to the route
1960 struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
1961 struct netinfo6 *np;
1963 int need_trigger = 0;
1965 if (ifcp->ifc_flags & IFF_LOOPBACK)
1966 return 0; /* ignore loopback */
1967 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1968 ifrt_p2p(ifcp, again);
1972 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
1973 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1975 trace(1, "route: %s on %s: "
1976 "skip linklocal interface address\n",
1977 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
1981 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
1983 trace(1, "route: %s: skip unspec interface address\n",
1988 if (ifcp->ifc_flags & IFF_UP) {
1989 if ((rrt = MALLOC(struct riprt)) == NULL)
1990 fatal("malloc: struct riprt");
1991 memset(rrt, 0, sizeof(*rrt));
1992 rrt->rrt_same = NULL;
1993 rrt->rrt_index = ifcp->ifc_index;
1994 rrt->rrt_t = 0; /* don't age */
1995 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
1996 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
1997 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
1998 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
1999 rrt->rrt_flags = RTF_CLONING;
2000 rrt->rrt_rflags |= RRTF_CHANGED;
2001 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
2002 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2004 /* XXX why gateway address == network adddress? */
2005 rrt->rrt_gw = ifa->ifa_addr;
2007 np = &rrt->rrt_info;
2008 search_rrt = rtsearch(np, &prev_rrt);
2009 if (search_rrt != NULL) {
2010 if (search_rrt->rrt_info.rip6_metric >
2011 rrt->rrt_info.rip6_metric) {
2013 prev_rrt->rrt_next = rrt->rrt_next;
2015 riprt = rrt->rrt_next;
2016 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2019 /* Already have better route */
2021 trace(1, "route: %s/%d: "
2022 "already registered (%s)\n",
2023 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2030 /* Attach the route to the list */
2031 trace(1, "route: %s/%d: register route (%s)\n",
2032 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2034 rrt->rrt_next = riprt;
2036 addroute(rrt, &rrt->rrt_gw, ifcp);
2038 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2041 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
2042 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2043 t_lifetime = time(NULL) - RIP_LIFETIME;
2044 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2045 loop_rrt->rrt_t = t_lifetime;
2046 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2047 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2054 return need_trigger;
2058 * there are couple of p2p interface routing models. "behavior" lets
2059 * you pick one. it looks that gated behavior fits best with BSDs,
2060 * since BSD kernels does not look at prefix length on p2p interfaces.
2063 ifrt_p2p(ifcp, again)
2068 struct riprt *rrt, *orrt, *prevrrt;
2069 struct netinfo6 *np;
2070 struct in6_addr addr, dest;
2071 int advert, ignore, i;
2072 #define P2PADVERT_NETWORK 1
2073 #define P2PADVERT_ADDR 2
2074 #define P2PADVERT_DEST 4
2075 #define P2PADVERT_MAX 4
2076 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2077 const char *category = "";
2080 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2081 addr = ifa->ifa_addr;
2082 dest = ifa->ifa_raddr;
2083 applyplen(&addr, ifa->ifa_plen);
2084 applyplen(&dest, ifa->ifa_plen);
2085 advert = ignore = 0;
2089 * honor addr/plen, just like normal shared medium
2090 * interface. this may cause trouble if you reuse
2091 * addr/plen on other interfaces.
2093 * advertise addr/plen.
2095 advert |= P2PADVERT_NETWORK;
2099 * prefixlen on p2p interface is meaningless.
2100 * advertise addr/128 and dest/128.
2102 * do not install network route to route6d routing
2103 * table (if we do, it would prevent route installation
2104 * for other p2p interface that shares addr/plen).
2106 * XXX what should we do if dest is ::? it will not
2107 * get announced anyways (see following filter),
2108 * but we need to think.
2110 advert |= P2PADVERT_ADDR;
2111 advert |= P2PADVERT_DEST;
2112 ignore |= P2PADVERT_NETWORK;
2116 * just for testing. actually the code is redundant
2117 * given the current p2p interface address assignment
2118 * rule for kame kernel.
2121 * A/n -> announce A/n
2122 * A B/n, A and B share prefix -> A/n (= B/n)
2123 * A B/n, do not share prefix -> A/128 and B/128
2124 * actually, A/64 and A B/128 are the only cases
2125 * permitted by the kernel:
2127 * A B/128 -> A/128 and B/128
2129 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
2130 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2131 advert |= P2PADVERT_NETWORK;
2133 advert |= P2PADVERT_ADDR;
2134 advert |= P2PADVERT_DEST;
2135 ignore |= P2PADVERT_NETWORK;
2138 advert |= P2PADVERT_NETWORK;
2142 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2143 if ((ignore & i) != 0)
2145 if ((rrt = MALLOC(struct riprt)) == NULL) {
2146 fatal("malloc: struct riprt");
2149 memset(rrt, 0, sizeof(*rrt));
2150 rrt->rrt_same = NULL;
2151 rrt->rrt_index = ifcp->ifc_index;
2152 rrt->rrt_t = 0; /* don't age */
2154 case P2PADVERT_NETWORK:
2155 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2156 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2157 applyplen(&rrt->rrt_info.rip6_dest,
2159 category = "network";
2161 case P2PADVERT_ADDR:
2162 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2163 rrt->rrt_info.rip6_plen = 128;
2164 rrt->rrt_gw = in6addr_loopback;
2167 case P2PADVERT_DEST:
2168 rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
2169 rrt->rrt_info.rip6_plen = 128;
2170 rrt->rrt_gw = ifa->ifa_addr;
2174 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2175 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2177 trace(1, "route: %s: skip unspec/linklocal "
2178 "(%s on %s)\n", category, ifcp->ifc_name);
2183 if ((advert & i) == 0) {
2184 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2188 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2189 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2190 np = &rrt->rrt_info;
2191 orrt = rtsearch(np, &prevrrt);
2193 /* Attach the route to the list */
2194 trace(1, "route: %s/%d: register route "
2196 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2197 category, ifcp->ifc_name, noadv);
2198 rrt->rrt_next = riprt;
2200 } else if (rrt->rrt_index != orrt->rrt_index ||
2201 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2203 rrt->rrt_next = orrt->rrt_next;
2205 prevrrt->rrt_next = rrt;
2210 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2211 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2212 category, ifcp->ifc_name, noadv);
2216 trace(1, "route: %s/%d: "
2217 "already registered (%s on %s%s)\n",
2218 inet6_n2p(&np->rip6_dest),
2219 np->rip6_plen, category,
2220 ifcp->ifc_name, noadv);
2226 #undef P2PADVERT_NETWORK
2227 #undef P2PADVERT_ADDR
2228 #undef P2PADVERT_DEST
2229 #undef P2PADVERT_MAX
2239 struct if_msghdr *ifm;
2246 mib[4] = NET_RT_IFLIST;
2248 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2249 fatal("sysctl estimate NET_RT_IFLIST");
2252 if ((buf = malloc(msize)) == NULL) {
2256 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2257 fatal("sysctl NET_RT_IFLIST");
2260 ifm = (struct if_msghdr *)buf;
2261 mtu = ifm->ifm_data.ifi_mtu;
2263 if (ifindex != ifm->ifm_index) {
2264 fatal("ifindex does not match with ifm_index");
2274 struct rt_msghdr *rtm;
2276 #define RTTYPE(s, f) \
2278 if (rtm->rtm_type == (f)) \
2281 RTTYPE("ADD", RTM_ADD);
2282 RTTYPE("DELETE", RTM_DELETE);
2283 RTTYPE("CHANGE", RTM_CHANGE);
2284 RTTYPE("GET", RTM_GET);
2285 RTTYPE("LOSING", RTM_LOSING);
2286 RTTYPE("REDIRECT", RTM_REDIRECT);
2287 RTTYPE("MISS", RTM_MISS);
2288 RTTYPE("LOCK", RTM_LOCK);
2289 RTTYPE("OLDADD", RTM_OLDADD);
2290 RTTYPE("OLDDEL", RTM_OLDDEL);
2291 RTTYPE("RESOLVE", RTM_RESOLVE);
2292 RTTYPE("NEWADDR", RTM_NEWADDR);
2293 RTTYPE("DELADDR", RTM_DELADDR);
2294 RTTYPE("IFINFO", RTM_IFINFO);
2296 RTTYPE("OLDADD", RTM_OLDADD);
2299 RTTYPE("OLDDEL", RTM_OLDDEL);
2302 RTTYPE("OIFINFO", RTM_OIFINFO);
2304 #ifdef RTM_IFANNOUNCE
2305 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2308 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2311 RTTYPE("DELMADDR", RTM_DELMADDR);
2319 struct rt_msghdr *rtm;
2321 static char buf[BUFSIZ];
2324 * letter conflict should be okay. painful when *BSD diverges...
2326 strlcpy(buf, "", sizeof(buf));
2327 #define RTFLAG(s, f) \
2329 if (rtm->rtm_flags & (f)) \
2330 strlcat(buf, (s), sizeof(buf)); \
2332 RTFLAG("U", RTF_UP);
2333 RTFLAG("G", RTF_GATEWAY);
2334 RTFLAG("H", RTF_HOST);
2335 RTFLAG("R", RTF_REJECT);
2336 RTFLAG("D", RTF_DYNAMIC);
2337 RTFLAG("M", RTF_MODIFIED);
2338 RTFLAG("d", RTF_DONE);
2340 RTFLAG("m", RTF_MASK);
2342 RTFLAG("C", RTF_CLONING);
2344 RTFLAG("c", RTF_CLONED);
2346 #ifdef RTF_PRCLONING
2347 RTFLAG("c", RTF_PRCLONING);
2349 #ifdef RTF_WASCLONED
2350 RTFLAG("W", RTF_WASCLONED);
2352 RTFLAG("X", RTF_XRESOLVE);
2353 RTFLAG("L", RTF_LLINFO);
2354 RTFLAG("S", RTF_STATIC);
2355 RTFLAG("B", RTF_BLACKHOLE);
2357 RTFLAG("3", RTF_PROTO3);
2359 RTFLAG("2", RTF_PROTO2);
2360 RTFLAG("1", RTF_PROTO1);
2361 #ifdef RTF_BROADCAST
2362 RTFLAG("b", RTF_BROADCAST);
2365 RTFLAG("d", RTF_DEFAULT);
2367 #ifdef RTF_ISAROUTER
2368 RTFLAG("r", RTF_ISAROUTER);
2371 RTFLAG("T", RTF_TUNNEL);
2374 RTFLAG("A", RTF_AUTH);
2377 RTFLAG("E", RTF_CRYPT);
2387 static char buf[BUFSIZ];
2389 strlcpy(buf, "", sizeof(buf));
2390 #define IFFLAG(s, f) \
2394 strlcat(buf, ",", sizeof(buf)); \
2395 strlcat(buf, s, sizeof(buf)); \
2398 IFFLAG("UP", IFF_UP);
2399 IFFLAG("BROADCAST", IFF_BROADCAST);
2400 IFFLAG("DEBUG", IFF_DEBUG);
2401 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2402 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2403 #ifdef IFF_NOTRAILERS
2404 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2407 IFFLAG("SMART", IFF_SMART);
2409 IFFLAG("RUNNING", IFF_RUNNING);
2410 IFFLAG("NOARP", IFF_NOARP);
2411 IFFLAG("PROMISC", IFF_PROMISC);
2412 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2413 IFFLAG("OACTIVE", IFF_OACTIVE);
2414 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2415 IFFLAG("LINK0", IFF_LINK0);
2416 IFFLAG("LINK1", IFF_LINK1);
2417 IFFLAG("LINK2", IFF_LINK2);
2418 IFFLAG("MULTICAST", IFF_MULTICAST);
2429 char *buf, *p, *lim;
2430 struct rt_msghdr *rtm;
2439 mib[3] = AF_INET6; /* Address family */
2440 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2441 mib[5] = 0; /* No flags */
2447 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2448 errmsg = "sysctl estimate";
2451 if ((buf = malloc(msize)) == NULL) {
2455 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2456 errmsg = "sysctl NET_RT_DUMP";
2459 } while (retry < 5 && errmsg != NULL);
2461 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2464 } else if (1 < retry)
2465 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2468 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2469 rtm = (struct rt_msghdr *)p;
2470 rt_entry(rtm, again);
2476 rt_entry(rtm, again)
2477 struct rt_msghdr *rtm;
2480 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2481 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2482 char *rtmp, *ifname = NULL;
2483 struct riprt *rrt, *orrt;
2484 struct netinfo6 *np;
2487 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2488 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2489 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
2490 return; /* not interested in the link route */
2492 /* do not look at cloned routes */
2493 #ifdef RTF_WASCLONED
2494 if (rtm->rtm_flags & RTF_WASCLONED)
2498 if (rtm->rtm_flags & RTF_CLONED)
2502 * do not look at dynamic routes.
2503 * netbsd/openbsd cloned routes have UGHD.
2505 if (rtm->rtm_flags & RTF_DYNAMIC)
2507 rtmp = (char *)(rtm + 1);
2509 if ((rtm->rtm_addrs & RTA_DST) == 0)
2510 return; /* ignore routes without destination address */
2511 sin6_dst = (struct sockaddr_in6 *)rtmp;
2512 rtmp += ROUNDUP(sin6_dst->sin6_len);
2513 if (rtm->rtm_addrs & RTA_GATEWAY) {
2514 sin6_gw = (struct sockaddr_in6 *)rtmp;
2515 rtmp += ROUNDUP(sin6_gw->sin6_len);
2517 if (rtm->rtm_addrs & RTA_NETMASK) {
2518 sin6_mask = (struct sockaddr_in6 *)rtmp;
2519 rtmp += ROUNDUP(sin6_mask->sin6_len);
2521 if (rtm->rtm_addrs & RTA_GENMASK) {
2522 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2523 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2525 if (rtm->rtm_addrs & RTA_IFP) {
2526 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2527 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2531 if (sin6_dst->sin6_family != AF_INET6)
2533 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2534 return; /* Link-local */
2535 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2536 return; /* Loopback */
2537 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2540 if ((rrt = MALLOC(struct riprt)) == NULL) {
2541 fatal("malloc: struct riprt");
2544 memset(rrt, 0, sizeof(*rrt));
2545 np = &rrt->rrt_info;
2546 rrt->rrt_same = NULL;
2547 rrt->rrt_t = time(NULL);
2548 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2549 rrt->rrt_t = 0; /* Don't age static routes */
2551 np->rip6_tag = htons(routetag & 0xffff);
2555 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2556 if (np->rip6_metric < 1)
2557 np->rip6_metric = 1;
2558 rrt->rrt_flags = rtm->rtm_flags;
2559 np->rip6_dest = sin6_dst->sin6_addr;
2562 if (rtm->rtm_flags & RTF_HOST)
2563 np->rip6_plen = 128; /* Host route */
2565 np->rip6_plen = sin6mask2len(sin6_mask);
2569 orrt = rtsearch(np, NULL);
2570 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2573 trace(1, "route: %s/%d flags %s: already registered\n",
2574 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2582 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2584 if (sin6_gw->sin6_family == AF_INET6)
2585 rrt->rrt_gw = sin6_gw->sin6_addr;
2586 else if (sin6_gw->sin6_family == AF_LINK) {
2587 /* XXX in case ppp link? */
2588 rrt->rrt_gw = in6addr_loopback;
2590 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2592 trace(1, "route: %s/%d flags %s",
2593 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2594 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2598 if (s < nindex2ifc && index2ifc[s])
2599 ifname = index2ifc[s]->ifc_name;
2601 trace(1, " not configured\n");
2605 trace(1, " if %s sock %d", ifname, s);
2611 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2612 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
2614 && (rrt->rrt_flags & RTF_LOCAL) == 0
2617 trace(0, "***** Gateway %s is not a link-local address.\n",
2618 inet6_n2p(&rrt->rrt_gw));
2619 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2620 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2621 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2624 /* Put it to the route list */
2625 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2626 /* replace route list */
2627 rrt->rrt_next = orrt->rrt_next;
2629 trace(1, "route: %s/%d flags %s: replace new route\n",
2630 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2634 rrt->rrt_next = riprt;
2640 addroute(rrt, gw, ifcp)
2642 const struct in6_addr *gw;
2645 struct netinfo6 *np;
2646 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2647 struct rt_msghdr *rtm;
2648 struct sockaddr_in6 *sin;
2651 np = &rrt->rrt_info;
2652 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2653 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2654 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2655 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2656 np->rip6_metric - 1, buf2);
2658 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2659 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2660 np->rip6_metric - 1, buf2);
2664 memset(buf, 0, sizeof(buf));
2665 rtm = (struct rt_msghdr *)buf;
2666 rtm->rtm_type = RTM_ADD;
2667 rtm->rtm_version = RTM_VERSION;
2668 rtm->rtm_seq = ++seq;
2670 rtm->rtm_flags = rrt->rrt_flags;
2671 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2672 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2673 rtm->rtm_inits = RTV_HOPCOUNT;
2674 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2676 sin->sin6_len = sizeof(struct sockaddr_in6);
2677 sin->sin6_family = AF_INET6;
2678 sin->sin6_addr = np->rip6_dest;
2679 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2681 sin->sin6_len = sizeof(struct sockaddr_in6);
2682 sin->sin6_family = AF_INET6;
2683 sin->sin6_addr = *gw;
2684 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2686 sin->sin6_len = sizeof(struct sockaddr_in6);
2687 sin->sin6_family = AF_INET6;
2688 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2689 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2691 len = (char *)sin - (char *)buf;
2692 rtm->rtm_msglen = len;
2693 if (write(rtsock, buf, len) > 0)
2696 if (errno == EEXIST) {
2697 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2698 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2700 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2701 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2703 trace(0, "Can not write to rtsock (addroute): %s\n",
2706 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2714 struct netinfo6 *np;
2715 struct in6_addr *gw;
2717 u_char buf[BUFSIZ], buf2[BUFSIZ];
2718 struct rt_msghdr *rtm;
2719 struct sockaddr_in6 *sin;
2722 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2723 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2724 np->rip6_plen, buf2);
2726 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2727 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2731 memset(buf, 0, sizeof(buf));
2732 rtm = (struct rt_msghdr *)buf;
2733 rtm->rtm_type = RTM_DELETE;
2734 rtm->rtm_version = RTM_VERSION;
2735 rtm->rtm_seq = ++seq;
2737 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2738 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2739 rtm->rtm_flags |= RTF_HOST;
2740 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2741 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2743 sin->sin6_len = sizeof(struct sockaddr_in6);
2744 sin->sin6_family = AF_INET6;
2745 sin->sin6_addr = np->rip6_dest;
2746 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2748 sin->sin6_len = sizeof(struct sockaddr_in6);
2749 sin->sin6_family = AF_INET6;
2750 sin->sin6_addr = *gw;
2751 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2753 sin->sin6_len = sizeof(struct sockaddr_in6);
2754 sin->sin6_family = AF_INET6;
2755 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2756 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2758 len = (char *)sin - (char *)buf;
2759 rtm->rtm_msglen = len;
2760 if (write(rtsock, buf, len) >= 0)
2763 if (errno == ESRCH) {
2764 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2765 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2767 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2768 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2770 trace(0, "Can not write to rtsock (delroute): %s\n",
2773 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2781 struct netinfo6 *np;
2782 struct in6_addr *gw;
2787 struct rt_msghdr *rtm;
2788 struct sockaddr_in6 *sin;
2790 rtm = (struct rt_msghdr *)buf;
2791 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2792 memset(rtm, 0, len);
2793 rtm->rtm_type = RTM_GET;
2794 rtm->rtm_version = RTM_VERSION;
2796 rtm->rtm_seq = myseq;
2797 rtm->rtm_addrs = RTA_DST;
2798 rtm->rtm_msglen = len;
2799 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2800 sin->sin6_len = sizeof(struct sockaddr_in6);
2801 sin->sin6_family = AF_INET6;
2802 sin->sin6_addr = np->rip6_dest;
2803 if (write(rtsock, buf, len) < 0) {
2804 if (errno == ESRCH) /* No such route found */
2806 perror("write to rtsock");
2810 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2811 perror("read from rtsock");
2814 rtm = (struct rt_msghdr *)buf;
2815 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2816 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2817 if (rtm->rtm_addrs & RTA_DST) {
2818 sin = (struct sockaddr_in6 *)
2819 ((char *)sin + ROUNDUP(sin->sin6_len));
2821 if (rtm->rtm_addrs & RTA_GATEWAY) {
2822 *gw = sin->sin6_addr;
2830 const struct in6_addr *p;
2832 static char buf[BUFSIZ];
2834 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
2857 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2860 fprintf(dump, "%s: Interface Table Dump\n", hms());
2861 fprintf(dump, " Number of interfaces: %d\n", nifc);
2862 for (i = 0; i < 2; i++) {
2863 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
2864 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
2866 if ((ifcp->ifc_flags & IFF_UP) == 0)
2868 if (iff_find(ifcp, 'N') != NULL)
2871 if (ifcp->ifc_flags & IFF_UP)
2874 ifdump0(dump, ifcp);
2877 fprintf(dump, "\n");
2885 const struct ifc *ifcp;
2893 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2894 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
2895 inet6_n2p(&ifcp->ifc_mylladdr),
2896 ifcp->ifc_mtu, ifcp->ifc_metric);
2897 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2898 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2899 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
2901 fprintf(dump, "\t%s/%d -- %s\n",
2902 inet6_n2p(&ifa->ifa_addr),
2903 ifa->ifa_plen, buf);
2905 fprintf(dump, "\t%s/%d\n",
2906 inet6_n2p(&ifa->ifa_addr),
2910 if (ifcp->ifc_filter) {
2911 fprintf(dump, "\tFilter:");
2912 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
2914 switch (iffp->iff_type) {
2916 ft = "Aggregate"; addr++; break;
2918 ft = "No-use"; break;
2920 ft = "Advertise-only"; addr++; break;
2922 ft = "Default-only"; break;
2924 ft = "Listen-only"; addr++; break;
2926 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
2931 fprintf(dump, " %s", ft);
2933 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
2937 fprintf(dump, "\n");
2953 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2957 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
2958 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
2959 if (rrt->rrt_t == 0)
2962 age = t - rrt->rrt_t;
2963 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
2965 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2966 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
2967 index2ifc[rrt->rrt_index]->ifc_name,
2968 inet6_n2p(&rrt->rrt_gw),
2969 rrt->rrt_info.rip6_metric, (long)age);
2970 if (rrt->rrt_info.rip6_tag) {
2971 fprintf(dump, " tag(0x%04x)",
2972 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
2974 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
2975 fprintf(dump, " NOT-LL");
2976 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
2977 fprintf(dump, " NO-ADV");
2978 fprintf(dump, "\n");
2980 fprintf(dump, "\n");
2986 * Parse the -A (and -O) options and put corresponding filter object to the
2987 * specified interface structures. Each of the -A/O option has the following
2988 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2989 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2995 char *p, *ap, *iflp, *ifname;
2996 struct iff ftmp, *iff_obj;
3003 for (i = 0; i < nfilter; i++) {
3007 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3011 if ((p = index(ap, ',')) != NULL) {
3015 if ((p = index(ap, '/')) == NULL) {
3016 fatal("no prefixlen specified for '%s'", ap);
3020 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
3021 fatal("invalid prefix specified for '%s'", ap);
3024 ftmp.iff_plen = atoi(p);
3025 ftmp.iff_next = NULL;
3026 applyplen(&ftmp.iff_addr, ftmp.iff_plen);
3028 ftmp.iff_type = filtertype[i];
3029 if (iflp == NULL || *iflp == '\0') {
3030 fatal("no interface specified for '%s'", ap);
3033 /* parse the interface listing portion */
3036 if ((iflp = index(iflp, ',')) != NULL)
3038 ifcp = ifc_find(ifname);
3040 fatal("no interface %s exists", ifname);
3043 iff_obj = (struct iff *)malloc(sizeof(struct iff));
3044 if (iff_obj == NULL) {
3045 fatal("malloc of iff_obj");
3048 memcpy((void *)iff_obj, (void *)&ftmp,
3049 sizeof(struct iff));
3050 /* link it to the interface filter */
3051 iff_obj->iff_next = ifcp->ifc_filter;
3052 ifcp->ifc_filter = iff_obj;
3056 * -A: aggregate configuration.
3058 if (filtertype[i] != 'A')
3060 /* put the aggregate to the kernel routing table */
3061 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3063 fatal("malloc: rrt");
3066 memset(rrt, 0, sizeof(struct riprt));
3067 rrt->rrt_info.rip6_dest = ftmp.iff_addr;
3068 rrt->rrt_info.rip6_plen = ftmp.iff_plen;
3069 rrt->rrt_info.rip6_metric = 1;
3070 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3071 rrt->rrt_gw = in6addr_loopback;
3072 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3073 rrt->rrt_rflags = RRTF_AGGREGATE;
3075 rrt->rrt_index = loopifindex;
3077 if (getroute(&rrt->rrt_info, &gw)) {
3080 * When the address has already been registered in the
3081 * kernel routing table, it should be removed
3083 delroute(&rrt->rrt_info, &gw);
3085 /* it is safer behavior */
3087 fatal("%s/%u already in routing table, "
3089 inet6_n2p(&rrt->rrt_info.rip6_dest),
3090 rrt->rrt_info.rip6_plen);
3095 /* Put the route to the list */
3096 rrt->rrt_next = riprt;
3098 trace(1, "Aggregate: %s/%d for %s\n",
3099 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
3101 /* Add this route to the kernel */
3102 if (nflag) /* do not modify kernel routing table */
3104 addroute(rrt, &in6addr_loopback, loopifcp);
3108 /***************** utility functions *****************/
3111 * Returns a pointer to ifac whose address and prefix length matches
3112 * with the address and prefix length specified in the arguments.
3115 ifa_match(ifcp, ia, plen)
3116 const struct ifc *ifcp;
3117 const struct in6_addr *ia;
3122 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
3123 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
3124 ifa->ifa_plen == plen)
3131 * Return a pointer to riprt structure whose address and prefix length
3132 * matches with the address and prefix length found in the argument.
3133 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3136 rtsearch(np, prev_rrt)
3137 struct netinfo6 *np;
3138 struct riprt **prev_rrt;
3144 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
3145 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3146 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3159 const struct sockaddr_in6 *sin6;
3162 return mask2len(&sin6->sin6_addr,
3163 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3167 mask2len(addr, lenlim)
3168 const struct in6_addr *addr;
3172 const u_char *p = (const u_char *)addr;
3174 for (j = 0; j < lenlim; j++, p++) {
3181 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3182 MASKLEN(0xfe, 7); break;
3183 MASKLEN(0xfc, 6); break;
3184 MASKLEN(0xf8, 5); break;
3185 MASKLEN(0xf0, 4); break;
3186 MASKLEN(0xe0, 3); break;
3187 MASKLEN(0xc0, 2); break;
3188 MASKLEN(0x80, 1); break;
3196 applymask(addr, mask)
3197 struct in6_addr *addr, *mask;
3202 p = (u_long *)addr; q = (u_long *)mask;
3203 for (i = 0; i < 4; i++)
3207 static const u_char plent[8] = {
3208 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3213 struct in6_addr *ia;
3220 for (i = 0; i < 16; i++) {
3229 static const int pl2m[9] = {
3230 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3237 static struct in6_addr ia;
3241 memset(&ia, 0, sizeof(struct in6_addr));
3243 for (i = 0; i < 16; i++, p++, n -= 8) {
3258 char *q = (char *)malloc(strlen(p) + 1);
3267 static char buf[BUFSIZ];
3272 if ((tm = localtime(&t)) == 0) {
3276 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3281 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3289 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3290 nextalarm = time(NULL) + interval;
3300 t = (int)(RIP_TRIG_INT6_MIN +
3301 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3302 sup_trig_update = time(NULL) + t;
3308 fatal(const char *fmt, ...)
3310 fatal(fmt, va_alist)
3323 vsnprintf(buf, sizeof(buf), fmt, ap);
3325 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3332 tracet(int level, const char *fmt, ...)
3334 tracet(level, fmt, va_alist)
3347 if (level <= dflag) {
3348 fprintf(stderr, "%s: ", hms());
3349 vfprintf(stderr, fmt, ap);
3353 vsyslog(LOG_DEBUG, fmt, ap);
3355 vsyslog(LOG_WARNING, fmt, ap);
3362 trace(int level, const char *fmt, ...)
3364 trace(level, fmt, va_alist)
3378 vfprintf(stderr, fmt, ap);
3381 vsyslog(LOG_DEBUG, fmt, ap);
3383 vsyslog(LOG_WARNING, fmt, ap);
3391 struct if_nameindex *p, *p0;
3392 unsigned int max = 0;
3394 p0 = if_nameindex();
3395 for (p = p0; p && p->if_index && p->if_name; p++) {
3396 if (max < p->if_index)
3399 if_freenameindex(p0);
3409 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
3410 if (strcmp(name, ifcp->ifc_name) == 0)
3413 return (struct ifc *)NULL;
3417 iff_find(ifcp, type)
3423 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
3424 if (iffp->iff_type == type)
3431 setindex2ifc(idx, ifcp)
3439 nindex2ifc = 5; /*initial guess*/
3440 index2ifc = (struct ifc **)
3441 malloc(sizeof(*index2ifc) * nindex2ifc);
3442 if (index2ifc == NULL) {
3446 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3449 while (nindex2ifc <= idx)
3451 if (n != nindex2ifc) {
3452 p = (struct ifc **)realloc(index2ifc,
3453 sizeof(*index2ifc) * nindex2ifc);
3458 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3461 index2ifc[idx] = ifcp;