1 /* $FreeBSD: src/usr.sbin/route6d/route6d.c,v 1.2.2.5 2001/07/03 11:02:09 ume Exp $ */
2 /* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $
35 #define _KERNEL_STRUCTURES
50 #include <sys/types.h>
51 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/sysctl.h>
58 #if defined(__DragonFly__)
59 #include <net/if_var.h>
60 #endif /* __DragonFly__ */
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet/udp.h>
69 #include <arpa/inet.h>
76 #define INIT_INTERVAL6 6
78 #define INIT_INTERVAL6 10 /* Wait to submit a 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))
87 * Following two macros are highly depending on KAME Release
89 #define IN6_LINKLOCAL_IFINDEX(addr) \
90 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
92 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
94 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
95 (addr).s6_addr[3] = (index) & 0xff; \
98 struct ifc { /* Configuration of an interface */
99 char *ifc_name; /* if name */
100 struct ifc *ifc_next;
101 int ifc_index; /* if index */
102 int ifc_mtu; /* if mtu */
103 int ifc_metric; /* if metric */
104 u_int ifc_flags; /* flags */
105 short ifc_cflags; /* IFC_XXX */
106 struct in6_addr ifc_mylladdr; /* my link-local address */
107 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
108 struct iff *ifc_filter; /* filter structure */
109 struct ifac *ifc_addr; /* list of AF_INET6 addresses */
110 int ifc_joined; /* joined to ff02::9 */
113 struct ifac { /* Adddress associated to an interface */
114 struct ifc *ifa_conf; /* back pointer */
115 struct ifac *ifa_next;
116 struct in6_addr ifa_addr; /* address */
117 struct in6_addr ifa_raddr; /* remote address, valid in p2p */
118 int ifa_plen; /* prefix length */
123 struct in6_addr iff_addr;
125 struct iff *iff_next;
129 int nifc; /* number of valid ifc's */
130 struct ifc **index2ifc;
132 struct ifc *loopifcp = NULL; /* pointing to loopback */
133 int loopifindex = 0; /* ditto */
134 fd_set sockvec; /* vector to select() for receiving */
135 int rtsock; /* the routing socket */
136 int ripsock; /* socket to send/receive RIP datagram */
138 struct rip6 *ripbuf; /* packet buffer for sending */
141 * Maintain the routes in a linked list. When the number of the routes
142 * grows, somebody would like to introduce a hash based or a radix tree
143 * based structure. I believe the number of routes handled by RIP is
144 * limited and I don't have to manage a complex data structure, however.
146 * One of the major drawbacks of the linear linked list is the difficulty
147 * of representing the relationship between a couple of routes. This may
148 * be a significant problem when we have to support route aggregation with
149 * supressing the specifices covered by the aggregate.
153 struct riprt *rrt_next; /* next destination */
154 struct riprt *rrt_same; /* same destination - future use */
155 struct netinfo6 rrt_info; /* network info */
156 struct in6_addr rrt_gw; /* gateway */
157 u_long rrt_flags; /* kernel routing table flags */
158 u_long rrt_rflags; /* route6d routing table flags */
159 time_t rrt_t; /* when the route validated */
160 int rrt_index; /* ifindex from which this route got */
163 struct riprt *riprt = NULL;
165 int dflag = 0; /* debug flag */
166 int qflag = 0; /* quiet flag */
167 int nflag = 0; /* don't update kernel routing table */
168 int aflag = 0; /* age out even the statically defined routes */
169 int hflag = 0; /* don't split horizon */
170 int lflag = 0; /* exchange site local routes */
171 int sflag = 0; /* announce static routes w/ split horizon */
172 int Sflag = 0; /* announce static routes to every interface */
173 unsigned long routetag = 0; /* route tag attached on originating case */
175 char *filter[MAXFILTER];
176 int filtertype[MAXFILTER];
181 struct sockaddr_storage ripsin;
183 struct rtentry rtentry;
186 time_t nextalarm = 0;
187 time_t sup_trig_update = 0;
193 static u_long seq = 0;
196 volatile sig_atomic_t seenalrm;
197 volatile sig_atomic_t seenquit;
198 volatile sig_atomic_t seenusr1;
200 #define RRTF_AGGREGATE 0x08000000
201 #define RRTF_NOADVERTISE 0x10000000
202 #define RRTF_NH_NOT_LLADDR 0x20000000
203 #define RRTF_SENDANYWAY 0x40000000
204 #define RRTF_CHANGED 0x80000000
206 int main(int, char **);
207 void sighandler(int);
210 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
211 int out_filter(struct riprt *, struct ifc *);
213 void sockopt(struct ifc *);
215 void ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
217 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
218 const struct sockaddr_in6 *);
219 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
220 const struct sockaddr_in6 *);
221 void filterconfig(void);
223 const char *rttypes(struct rt_msghdr *);
224 const char *rtflags(struct rt_msghdr *);
225 const char *ifflags(int);
226 int ifrt(struct ifc *, int);
227 void ifrt_p2p(struct ifc *, int);
228 void applymask(struct in6_addr *, struct in6_addr *);
229 void applyplen(struct in6_addr *, int);
232 void ifdump0(FILE *, const struct ifc *);
234 void rt_entry(struct rt_msghdr *, int);
236 void riprequest(struct ifc *, struct netinfo6 *, int,
237 struct sockaddr_in6 *);
238 void ripflush(struct ifc *, struct sockaddr_in6 *);
239 void sendrequest(struct ifc *);
240 int sin6mask2len(const struct sockaddr_in6 *);
241 int mask2len(const struct in6_addr *, int);
242 int sendpacket(struct sockaddr_in6 *, int);
243 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
244 int delroute(struct netinfo6 *, struct in6_addr *);
245 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
247 int tobeadv(struct riprt *, struct ifc *);
248 char *allocopy(char *);
250 const char *inet6_n2p(const struct in6_addr *);
251 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
252 struct in6_addr *plen2mask(int);
253 struct riprt *rtsearch(struct netinfo6 *, struct riprt **);
254 int ripinterval(int);
255 time_t ripsuptrig(void);
256 void fatal(const char *, ...)
257 __attribute__((__format__(__printf__, 1, 2)));
258 void trace(int, const char *, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260 void tracet(int, const char *, ...)
261 __attribute__((__format__(__printf__, 2, 3)));
262 unsigned int if_maxindex(void);
263 struct ifc *ifc_find(char *);
264 struct iff *iff_find(struct ifc *, int);
265 void setindex2ifc(int, struct ifc *);
267 #define MALLOC(type) ((type *)malloc(sizeof(type)))
270 main(int argc, char **argv)
275 sigset_t mask, omask;
280 progname = strrchr(*argv, '/');
287 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
294 if (nfilter >= MAXFILTER) {
295 fatal("Exceeds MAXFILTER");
298 filtertype[nfilter] = ch;
299 filter[nfilter++] = allocopy(optarg);
303 routetag = strtoul(optarg, &ep, 0);
304 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
305 fatal("invalid route tag");
310 if ((rtlog = fopen(optarg, "w")) == NULL) {
311 fatal("Can not write to routelog");
315 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
316 FLAG('a', aflag, 1); break;
317 FLAG('d', dflag, 1); break;
318 FLAG('D', dflag, 2); break;
319 FLAG('h', hflag, 1); break;
320 FLAG('l', lflag, 1); break;
321 FLAG('n', nflag, 1); break;
322 FLAG('q', qflag, 1); break;
323 FLAG('s', sflag, 1); break;
324 FLAG('S', Sflag, 1); break;
327 fatal("Invalid option specified, terminating");
334 fatal("bogus extra arguments");
340 fprintf(stderr, "No kernel update is allowed\n");
342 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
345 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
347 memset(ripbuf, 0, RIP6_MAXMTU);
348 ripbuf->rip6_cmd = RIP6_RESPONSE;
349 ripbuf->rip6_vers = RIP6_VERSION;
350 ripbuf->rip6_res1[0] = 0;
351 ripbuf->rip6_res1[1] = 0;
355 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
356 if (ifcp->ifc_index < 0) {
358 "No ifindex found at %s (no link-local address?)\n",
365 if (loopifcp == NULL) {
366 fatal("No loopback found");
369 loopifindex = loopifcp->ifc_index;
370 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
379 if (daemon(0, 0) < 0) {
393 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
394 fprintf(pidfile, "%d\n", pid);
398 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
402 memset(ripbuf, 0, RIP6_MAXMTU);
403 ripbuf->rip6_cmd = RIP6_RESPONSE;
404 ripbuf->rip6_vers = RIP6_VERSION;
405 ripbuf->rip6_res1[0] = 0;
406 ripbuf->rip6_res1[1] = 0;
408 if (signal(SIGALRM, sighandler) == SIG_ERR ||
409 signal(SIGQUIT, sighandler) == SIG_ERR ||
410 signal(SIGTERM, sighandler) == SIG_ERR ||
411 signal(SIGUSR1, sighandler) == SIG_ERR ||
412 signal(SIGHUP, sighandler) == SIG_ERR ||
413 signal(SIGINT, sighandler) == SIG_ERR) {
418 * To avoid rip packet congestion (not on a cable but in this
419 * process), wait for a moment to send the first RIP6_RESPONSE
422 alarm(ripinterval(INIT_INTERVAL6));
424 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
425 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
429 syslog(LOG_INFO, "**** Started ****");
431 sigaddset(&mask, SIGALRM);
451 FD_COPY(&sockvec, &recvec);
453 switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
455 if (errno != EINTR) {
463 if (FD_ISSET(ripsock, &recvec)) {
464 sigprocmask(SIG_BLOCK, &mask, &omask);
466 sigprocmask(SIG_SETMASK, &omask, NULL);
468 if (FD_ISSET(rtsock, &recvec)) {
469 sigprocmask(SIG_BLOCK, &mask, &omask);
471 sigprocmask(SIG_SETMASK, &omask, NULL);
499 * gracefully exits after resetting sockopts.
508 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
509 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
510 delroute(&rrt->rrt_info, &rrt->rrt_gw);
515 syslog(LOG_INFO, "**** Terminated ****");
521 * Called periodically:
522 * 1. age out the learned route. remove it if necessary.
523 * 2. submit RIP6_RESPONSE packets.
524 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
525 * to invoke this function in every 1 or 5 or 10 seconds only to age the
526 * routes more precisely.
533 struct riprt *rrt, *rrt_prev, *rrt_next;
534 time_t t_lifetime, t_holddown;
536 /* age the RIP routes */
538 t_lifetime = time(NULL) - RIP_LIFETIME;
539 t_holddown = t_lifetime - RIP_HOLDDOWN;
540 for (rrt = riprt; rrt; rrt = rrt_next) {
541 rrt_next = rrt->rrt_next;
543 if (rrt->rrt_t == 0) {
547 if (rrt->rrt_t < t_holddown) {
549 rrt_prev->rrt_next = rrt->rrt_next;
551 riprt = rrt->rrt_next;
553 delroute(&rrt->rrt_info, &rrt->rrt_gw);
557 if (rrt->rrt_t < t_lifetime)
558 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
562 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
563 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
564 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
566 alarm(ripinterval(SUPPLY_INTERVAL6));
572 int i, int0, int255, error;
573 struct addrinfo hints, *res;
578 nindex2ifc = 0; /*initial guess*/
580 snprintf(port, sizeof(port), "%d", RIP6_PORT);
582 memset(&hints, 0, sizeof(hints));
583 hints.ai_family = PF_INET6;
584 hints.ai_socktype = SOCK_DGRAM;
585 hints.ai_flags = AI_PASSIVE;
586 error = getaddrinfo(NULL, port, &hints, &res);
588 fatal("%s", gai_strerror(error));
592 fatal(":: resolved to multiple address");
596 int0 = 0; int255 = 255;
597 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
602 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
606 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
607 &int255, sizeof(int255)) < 0) {
608 fatal("rip IPV6_MULTICAST_HOPS");
611 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
612 &int0, sizeof(int0)) < 0) {
613 fatal("rip IPV6_MULTICAST_LOOP");
618 #ifdef IPV6_RECVPKTINFO
619 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
621 fatal("rip IPV6_RECVPKTINFO");
624 #else /* old adv. API */
625 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
627 fatal("rip IPV6_PKTINFO");
632 memset(&hints, 0, sizeof(hints));
633 hints.ai_family = PF_INET6;
634 hints.ai_socktype = SOCK_DGRAM;
635 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
637 fatal("%s", gai_strerror(error));
641 fatal("%s resolved to multiple address", RIP6_DEST);
644 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
649 memset(&sockvec, 0, sizeof(sockvec));
651 FD_SET(ripsock, &sockvec);
654 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
655 fatal("route socket");
658 FD_SET(rtsock, &sockvec);
660 rtsock = -1; /*just for safety */
664 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
667 * ripflush flushes the rip datagram stored in the rip buffer
670 static struct netinfo6 *np;
673 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin)
679 tracet(1, "Send(%s): info(%d) to %s.%d\n",
681 inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
683 tracet(1, "Send: info(%d) to %s.%d\n",
684 nrt, inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
686 np = ripbuf->rip6_nets;
687 for (i = 0; i < nrt; i++, np++) {
688 if (np->rip6_metric == NEXTHOP_METRIC) {
689 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
690 trace(2, " NextHop reset");
692 trace(2, " NextHop %s",
693 inet6_n2p(&np->rip6_dest));
696 trace(2, " %s/%d[%d]",
697 inet6_n2p(&np->rip6_dest),
698 np->rip6_plen, np->rip6_metric);
701 trace(2, " tag=0x%04x",
702 ntohs(np->rip6_tag) & 0xffff);
707 error = sendpacket(sin, RIPSIZE(nrt));
708 if (error == EAFNOSUPPORT) {
709 /* Protocol not supported */
710 tracet(1, "Could not send info to %s (%s): "
712 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
713 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
715 nrt = 0; np = ripbuf->rip6_nets;
719 * Generate RIP6_RESPONSE packets and send them.
722 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin, int flag)
725 struct in6_addr *nh; /* next hop */
730 * Request from non-link local address is not
731 * a regular route6d update.
733 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
734 sizeof(struct udphdr) -
735 sizeof(struct rip6) + sizeof(struct netinfo6)) /
736 sizeof(struct netinfo6);
737 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
738 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
739 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
741 /* Put the route to the buffer */
749 if (nrt) /* Send last packet */
754 if ((flag & RRTF_SENDANYWAY) == 0 &&
755 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
759 if (iff_find(ifcp, 'N') != NULL)
762 /* -T: generate default route only */
763 if (iff_find(ifcp, 'T') != NULL) {
764 struct netinfo6 rrt_info;
765 memset(&rrt_info, 0, sizeof(struct netinfo6));
766 rrt_info.rip6_dest = in6addr_any;
767 rrt_info.rip6_plen = 0;
768 rrt_info.rip6_metric = 1;
769 rrt_info.rip6_metric += ifcp->ifc_metric;
770 rrt_info.rip6_tag = htons(routetag & 0xffff);
771 np = ripbuf->rip6_nets;
778 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
779 sizeof(struct udphdr) -
780 sizeof(struct rip6) + sizeof(struct netinfo6)) /
781 sizeof(struct netinfo6);
783 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
784 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
785 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
788 /* Need to check filter here */
789 if (out_filter(rrt, ifcp) == 0)
792 /* Check split horizon and other conditions */
793 if (tobeadv(rrt, ifcp) == 0)
796 /* Only considers the routes with flag if specified */
797 if ((flag & RRTF_CHANGED) &&
798 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
802 if (rrt->rrt_index == ifcp->ifc_index &&
803 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
804 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
805 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
806 if (nrt == maxrte - 2)
808 np->rip6_dest = rrt->rrt_gw;
809 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
810 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
813 np->rip6_metric = NEXTHOP_METRIC;
817 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
818 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
819 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
821 if (nrt == maxrte - 2)
823 memset(np, 0, sizeof(struct netinfo6));
824 np->rip6_metric = NEXTHOP_METRIC;
829 /* Put the route to the buffer */
837 if (nrt) /* Send last packet */
842 * outbound filter logic, per-route/interface.
845 out_filter(struct riprt *rrt, struct ifc *ifcp)
852 * -A: filter out less specific routes, if we have aggregated
855 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
856 if (iffp->iff_type != 'A')
858 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
860 ia = rrt->rrt_info.rip6_dest;
861 applyplen(&ia, iffp->iff_plen);
862 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
867 * if it is an aggregated route, advertise it only to the
868 * interfaces specified on -A.
870 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
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 &&
876 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
887 * -O: advertise only if prefix matches the configured prefix.
889 if (iff_find(ifcp, 'O')) {
891 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
892 if (iffp->iff_type != 'O')
894 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
896 ia = rrt->rrt_info.rip6_dest;
897 applyplen(&ia, iffp->iff_plen);
898 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
907 /* the prefix should be advertised */
912 * Determine if the route is to be advertised on the specified interface.
913 * It checks options specified in the arguments and the split horizon rule.
916 tobeadv(struct riprt *rrt, struct ifc *ifcp)
919 /* Special care for static routes */
920 if (rrt->rrt_flags & RTF_STATIC) {
921 /* XXX don't advertise reject/blackhole routes */
922 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
925 if (Sflag) /* Yes, advertise it anyway */
927 if (sflag && rrt->rrt_index != ifcp->ifc_index)
931 /* Regular split horizon */
932 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
938 * Send a rip packet actually.
941 sendpacket(struct sockaddr_in6 *sin, int len)
944 * MSG_DONTROUTE should not be specified when it responds with a
945 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
952 struct in6_pktinfo *pi;
954 struct sockaddr_in6 sincopy;
956 /* do not overwrite the given sin */
960 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)
961 || IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) {
962 idx = IN6_LINKLOCAL_IFINDEX(sin->sin6_addr);
963 SET_IN6_LINKLOCAL_IFINDEX(sin->sin6_addr, 0);
967 m.msg_name = (caddr_t)sin;
968 m.msg_namelen = sizeof(*sin);
969 iov[0].iov_base = (caddr_t)ripbuf;
970 iov[0].iov_len = len;
974 m.msg_control = NULL;
975 m.msg_controllen = 0;
977 memset(cmsgbuf, 0, sizeof(cmsgbuf));
978 cm = (struct cmsghdr *)cmsgbuf;
979 m.msg_control = (caddr_t)cm;
980 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
982 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
983 cm->cmsg_level = IPPROTO_IPV6;
984 cm->cmsg_type = IPV6_PKTINFO;
985 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
986 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
987 pi->ipi6_ifindex = idx;
990 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
991 trace(1, "sendmsg: %s\n", strerror(errno));
999 * Receive and process RIP packets. Update the routes/kernel forwarding
1000 * table if necessary.
1005 struct ifc *ifcp, *ic;
1006 struct sockaddr_in6 fsock;
1007 struct in6_addr nh; /* next hop */
1009 struct netinfo6 *np, *nq;
1011 int len, nn, need_trigger, idx;
1012 char buf[4 * RIP6_MAXMTU];
1016 struct iovec iov[2];
1017 u_char cmsgbuf[256];
1018 struct in6_pktinfo *pi;
1022 time_t t_half_lifetime;
1026 m.msg_name = (caddr_t)&fsock;
1027 m.msg_namelen = sizeof(fsock);
1028 iov[0].iov_base = (caddr_t)buf;
1029 iov[0].iov_len = sizeof(buf);
1032 cm = (struct cmsghdr *)cmsgbuf;
1033 m.msg_control = (caddr_t)cm;
1034 m.msg_controllen = sizeof(cmsgbuf);
1035 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1040 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1042 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1043 if (cm->cmsg_level == IPPROTO_IPV6 &&
1044 cm->cmsg_type == IPV6_PKTINFO) {
1045 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1046 idx = pi->ipi6_ifindex;
1050 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1051 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1053 nh = fsock.sin6_addr;
1054 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1055 sizeof(struct netinfo6);
1056 rp = (struct rip6 *)buf;
1059 if (rp->rip6_vers != RIP6_VERSION) {
1060 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1063 if (rp->rip6_cmd == RIP6_REQUEST) {
1064 if (idx && idx < nindex2ifc) {
1065 ifcp = index2ifc[idx];
1066 riprequest(ifcp, np, nn, &fsock);
1068 riprequest(NULL, np, nn, &fsock);
1073 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1074 trace(1, "Packets from non-ll addr: %s\n",
1075 inet6_n2p(&fsock.sin6_addr));
1076 return; /* Ignore packets from non-link-local addr */
1078 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1079 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1081 trace(1, "Packets to unknown interface index %d\n", idx);
1082 return; /* Ignore it */
1084 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1085 return; /* The packet is from me; ignore */
1086 if (rp->rip6_cmd != RIP6_RESPONSE) {
1087 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1092 if (iff_find(ifcp, 'N') != NULL)
1095 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1096 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1099 t_half_lifetime = t - (RIP_LIFETIME/2);
1100 for (; nn; nn--, np++) {
1101 if (np->rip6_metric == NEXTHOP_METRIC) {
1102 /* modify neighbor address */
1103 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1105 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1106 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1107 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1108 nh = fsock.sin6_addr;
1109 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1111 nh = fsock.sin6_addr;
1112 trace(1, "\tInvalid Nexthop: %s\n",
1113 inet6_n2p(&np->rip6_dest));
1117 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1118 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1119 inet6_n2p(&np->rip6_dest),
1120 np->rip6_plen, np->rip6_metric);
1123 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1124 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1125 inet6_n2p(&np->rip6_dest),
1126 np->rip6_plen, np->rip6_metric);
1129 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1130 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1131 inet6_n2p(&np->rip6_dest),
1132 np->rip6_plen, np->rip6_metric);
1135 /* may need to pass sitelocal prefix in some case, however*/
1136 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1137 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1138 inet6_n2p(&np->rip6_dest),
1139 np->rip6_plen, np->rip6_metric);
1142 trace(2, "\tnetinfo6: %s/%d [%d]",
1143 inet6_n2p(&np->rip6_dest),
1144 np->rip6_plen, np->rip6_metric);
1146 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1149 applyplen(&ia, np->rip6_plen);
1150 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1151 trace(2, " [junk outside prefix]");
1155 * -L: listen only if the prefix matches the configuration
1157 ok = 1; /* if there's no L filter, it is ok */
1158 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
1159 if (iffp->iff_type != 'L')
1162 if (np->rip6_plen < iffp->iff_plen)
1164 /* special rule: ::/0 means default, not "in /0" */
1165 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1168 applyplen(&ia, iffp->iff_plen);
1169 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1175 trace(2, " (filtered)\n");
1181 np->rip6_metric += ifcp->ifc_metric;
1182 if (np->rip6_metric > HOPCNT_INFINITY6)
1183 np->rip6_metric = HOPCNT_INFINITY6;
1185 applyplen(&np->rip6_dest, np->rip6_plen);
1186 if ((rrt = rtsearch(np, NULL)) != NULL) {
1187 if (rrt->rrt_t == 0)
1188 continue; /* Intf route has priority */
1189 nq = &rrt->rrt_info;
1190 if (nq->rip6_metric > np->rip6_metric) {
1191 if (rrt->rrt_index == ifcp->ifc_index &&
1192 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1193 /* Small metric from the same gateway */
1194 nq->rip6_metric = np->rip6_metric;
1196 /* Better route found */
1197 rrt->rrt_index = ifcp->ifc_index;
1198 /* Update routing table */
1199 delroute(nq, &rrt->rrt_gw);
1202 addroute(rrt, &nh, ifcp);
1204 rrt->rrt_rflags |= RRTF_CHANGED;
1207 } else if (nq->rip6_metric < np->rip6_metric &&
1208 rrt->rrt_index == ifcp->ifc_index &&
1209 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1210 /* Got worse route from same gw */
1211 nq->rip6_metric = np->rip6_metric;
1213 rrt->rrt_rflags |= RRTF_CHANGED;
1215 } else if (nq->rip6_metric == np->rip6_metric &&
1216 np->rip6_metric < HOPCNT_INFINITY6) {
1217 if (rrt->rrt_index == ifcp->ifc_index &&
1218 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1219 /* same metric, same route from same gw */
1221 } else if (rrt->rrt_t < t_half_lifetime) {
1222 /* Better route found */
1223 rrt->rrt_index = ifcp->ifc_index;
1224 /* Update routing table */
1225 delroute(nq, &rrt->rrt_gw);
1228 addroute(rrt, &nh, ifcp);
1229 rrt->rrt_rflags |= RRTF_CHANGED;
1234 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1235 * do not update age value. Do nothing.
1237 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1238 /* Got a new valid route */
1239 if ((rrt = MALLOC(struct riprt)) == NULL) {
1240 fatal("malloc: struct riprt");
1243 memset(rrt, 0, sizeof(*rrt));
1244 nq = &rrt->rrt_info;
1246 rrt->rrt_same = NULL;
1247 rrt->rrt_index = ifcp->ifc_index;
1248 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1251 applyplen(&nq->rip6_dest, nq->rip6_plen);
1252 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1253 rrt->rrt_flags |= RTF_HOST;
1255 /* Put the route to the list */
1256 rrt->rrt_next = riprt;
1258 /* Update routing table */
1259 addroute(rrt, &nh, ifcp);
1260 rrt->rrt_rflags |= RRTF_CHANGED;
1265 /* XXX need to care the interval between triggered updates */
1267 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1268 for (ic = ifc; ic; ic = ic->ifc_next) {
1269 if (ifcp->ifc_index == ic->ifc_index)
1271 if (ic->ifc_flags & IFF_UP)
1272 ripsend(ic, &ic->ifc_ripsin,
1276 /* Reset the flag */
1277 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1278 rrt->rrt_rflags &= ~RRTF_CHANGED;
1283 * Send all routes request packet to the specified interface.
1286 sendrequest(struct ifc *ifcp)
1288 struct netinfo6 *np;
1291 if (ifcp->ifc_flags & IFF_LOOPBACK)
1293 ripbuf->rip6_cmd = RIP6_REQUEST;
1294 np = ripbuf->rip6_nets;
1295 memset(np, 0, sizeof(struct netinfo6));
1296 np->rip6_metric = HOPCNT_INFINITY6;
1297 tracet(1, "Send rtdump Request to %s (%s)\n",
1298 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1299 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1300 if (error == EAFNOSUPPORT) {
1301 /* Protocol not supported */
1302 tracet(1, "Could not send rtdump Request to %s (%s): "
1303 "set IFF_UP to 0\n",
1304 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1305 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1307 ripbuf->rip6_cmd = RIP6_RESPONSE;
1311 * Process a RIP6_REQUEST packet.
1314 riprequest(struct ifc *ifcp, struct netinfo6 *np, int nn,
1315 struct sockaddr_in6 *sin)
1320 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1321 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1322 /* Specific response, don't split-horizon */
1323 trace(1, "\tRIP Request\n");
1324 for (i = 0; i < nn; i++, np++) {
1325 rrt = rtsearch(np, NULL);
1327 np->rip6_metric = rrt->rrt_info.rip6_metric;
1329 np->rip6_metric = HOPCNT_INFINITY6;
1331 sendpacket(sin, RIPSIZE(nn));
1334 /* Whole routing table dump */
1335 trace(1, "\tRIP Request -- whole routing table\n");
1336 ripsend(ifcp, sin, RRTF_SENDANYWAY);
1340 * Get information of each interface.
1345 struct ifaddrs *ifap, *ifa;
1347 struct ipv6_mreq mreq;
1350 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1355 if (getifaddrs(&ifap) != 0) {
1356 fatal("getifaddrs");
1360 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1361 if (ifa->ifa_addr->sa_family != AF_INET6)
1363 ifcp = ifc_find(ifa->ifa_name);
1364 /* we are interested in multicast-capable interfaces */
1365 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1369 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1370 fatal("malloc: struct ifc");
1373 memset(ifcp, 0, sizeof(*ifcp));
1374 ifcp->ifc_index = -1;
1375 ifcp->ifc_next = ifc;
1378 ifcp->ifc_name = allocopy(ifa->ifa_name);
1379 ifcp->ifc_addr = NULL;
1380 ifcp->ifc_filter = NULL;
1381 ifcp->ifc_flags = ifa->ifa_flags;
1382 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1383 ifflags(ifcp->ifc_flags));
1384 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1387 /* update flag, this may be up again */
1388 if (ifcp->ifc_flags != ifa->ifa_flags) {
1389 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1390 ifflags(ifcp->ifc_flags));
1391 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1392 ifcp->ifc_cflags |= IFC_CHANGED;
1394 ifcp->ifc_flags = ifa->ifa_flags;
1396 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
1397 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1398 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1399 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1400 mreq.ipv6mr_interface = ifcp->ifc_index;
1401 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1402 &mreq, sizeof(mreq)) < 0) {
1403 fatal("IPV6_JOIN_GROUP");
1406 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1415 ifconfig1(const char *name, const struct sockaddr *sa, struct ifc *ifcp, int s)
1417 struct in6_ifreq ifr;
1418 const struct sockaddr_in6 *sin;
1423 sin = (const struct sockaddr_in6 *)sa;
1424 ifr.ifr_addr = *sin;
1425 strcpy(ifr.ifr_name, name);
1426 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1427 fatal("ioctl: SIOCGIFNETMASK_IN6");
1430 plen = sin6mask2len(&ifr.ifr_addr);
1431 if ((ifa = ifa_match(ifcp, &sin->sin6_addr, plen)) != NULL) {
1432 /* same interface found */
1433 /* need check if something changed */
1434 /* XXX not yet implemented */
1438 * New address is found
1440 if ((ifa = MALLOC(struct ifac)) == NULL) {
1441 fatal("malloc: struct ifac");
1444 memset(ifa, 0, sizeof(*ifa));
1445 ifa->ifa_conf = ifcp;
1446 ifa->ifa_next = ifcp->ifc_addr;
1447 ifcp->ifc_addr = ifa;
1448 ifa->ifa_addr = sin->sin6_addr;
1449 ifa->ifa_plen = plen;
1450 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1451 ifr.ifr_addr = *sin;
1452 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1453 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1456 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
1457 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
1458 trace(1, "found address %s/%d -- %s\n",
1459 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
1461 trace(1, "found address %s/%d\n",
1462 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
1464 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1465 ifcp->ifc_mylladdr = ifa->ifa_addr;
1466 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
1467 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1468 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1470 setindex2ifc(ifcp->ifc_index, ifcp);
1471 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1472 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1473 ifcp->ifc_mtu = RIP6_MAXMTU;
1474 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1475 fatal("ioctl: SIOCGIFMETRIC");
1478 ifcp->ifc_metric = ifr.ifr_metric;
1479 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1480 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1482 ifcp->ifc_cflags |= IFC_CHANGED;
1486 * Receive and process routing messages.
1487 * Update interface information as necesssary.
1494 struct rt_msghdr *rtm;
1495 struct ifa_msghdr *ifam;
1496 struct if_msghdr *ifm;
1498 struct ifc *ifcp, *ic;
1499 int iface = 0, rtable = 0;
1500 struct sockaddr_in6 *rta[RTAX_MAX];
1501 struct sockaddr_in6 mask;
1505 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1506 perror("read from rtsock");
1509 if (len < sizeof(*rtm)) {
1510 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1511 len, (u_long)sizeof(*rtm));
1515 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1516 /* safety against bogus message */
1517 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1518 trace(1, "bogus rtmsg: length=%d\n",
1519 ((struct rt_msghdr *)p)->rtm_msglen);
1525 switch (((struct rt_msghdr *)p)->rtm_type) {
1528 ifam = (struct ifa_msghdr *)p;
1529 addrs = ifam->ifam_addrs;
1530 q = (char *)(ifam + 1);
1533 ifm = (struct if_msghdr *)p;
1534 addrs = ifm->ifm_addrs;
1535 q = (char *)(ifm + 1);
1538 rtm = (struct rt_msghdr *)p;
1539 addrs = rtm->rtm_addrs;
1540 q = (char *)(rtm + 1);
1541 if (rtm->rtm_version != RTM_VERSION) {
1542 trace(1, "unexpected rtmsg version %d "
1544 rtm->rtm_version, RTM_VERSION);
1547 if (rtm->rtm_pid == pid) {
1549 trace(1, "rtmsg looped back to me, ignored\n");
1555 memset(&rta, 0, sizeof(rta));
1556 for (i = 0; i < RTAX_MAX; i++) {
1557 if (addrs & (1 << i)) {
1558 rta[i] = (struct sockaddr_in6 *)q;
1559 q += ROUNDUP(rta[i]->sin6_len);
1563 trace(1, "rtsock: %s (addrs=%x)\n",
1564 rttypes((struct rt_msghdr *)p), addrs);
1567 i < ((struct rt_msghdr *)p)->rtm_msglen;
1569 fprintf(stderr, "%02x ", p[i] & 0xff);
1570 if (i % 16 == 15) fprintf(stderr, "\n");
1572 fprintf(stderr, "\n");
1578 * We may be able to optimize by using ifm->ifm_index or
1579 * ifam->ifam_index. For simplicity we don't do that here.
1581 switch (((struct rt_msghdr *)p)->rtm_type) {
1594 /* nothing to be done here */
1595 trace(1, "\tnothing to be done, ignored\n");
1600 if (rta[RTAX_DST] == NULL) {
1601 trace(1, "\tno destination, ignored\n");
1604 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1605 trace(1, "\taf mismatch, ignored\n");
1608 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1609 trace(1, "\tlinklocal destination, ignored\n");
1612 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1613 trace(1, "\tloopback destination, ignored\n");
1614 continue; /* Loopback */
1616 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1617 trace(1, "\tmulticast destination, ignored\n");
1623 switch (((struct rt_msghdr *)p)->rtm_type) {
1632 /* should already be handled */
1633 fatal("rtrecv: never reach here");
1636 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1637 trace(1, "\tsome of dst/gw/netamsk are "
1638 "unavailable, ignored\n");
1641 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1642 mask.sin6_len = sizeof(mask);
1643 memset(&mask.sin6_addr, 0xff,
1644 sizeof(mask.sin6_addr));
1645 rta[RTAX_NETMASK] = &mask;
1646 } else if (!rta[RTAX_NETMASK]) {
1647 trace(1, "\tsome of dst/gw/netamsk are "
1648 "unavailable, ignored\n");
1651 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1652 rta[RTAX_NETMASK]) == 0) {
1653 rtable++; /*just to be sure*/
1658 trace(1, "\tnot supported yet, ignored\n");
1661 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1662 trace(1, "\tno netmask or ifa given, ignored\n");
1665 if (ifam->ifam_index < nindex2ifc)
1666 ifcp = index2ifc[ifam->ifam_index];
1670 trace(1, "\tinvalid ifam_index %d, ignored\n",
1674 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1679 trace(1, "\tnot supported yet, ignored\n");
1686 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1688 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
1689 if (ifcp->ifc_cflags & IFC_CHANGED) {
1690 if (ifrt(ifcp, 1)) {
1691 for (ic = ifc; ic; ic = ic->ifc_next) {
1692 if (ifcp->ifc_index == ic->ifc_index)
1694 if (ic->ifc_flags & IFF_UP)
1695 ripsend(ic, &ic->ifc_ripsin,
1698 /* Reset the flag */
1699 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1700 rrt->rrt_rflags &= ~RRTF_CHANGED;
1702 ifcp->ifc_cflags &= ~IFC_CHANGED;
1706 trace(1, "rtsock: read routing table again\n");
1712 * remove specified route from the internal routing table.
1715 rt_del(const struct sockaddr_in6 *sdst, const struct sockaddr_in6 *sgw,
1716 const struct sockaddr_in6 *smask)
1718 const struct in6_addr *dst = NULL;
1719 const struct in6_addr *gw = NULL;
1721 struct netinfo6 ni6;
1722 struct riprt *rrt = NULL;
1725 if (sdst->sin6_family != AF_INET6) {
1726 trace(1, "\tother AF, ignored\n");
1729 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1730 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1731 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1732 trace(1, "\taddress %s not interesting, ignored\n",
1733 inet6_n2p(&sdst->sin6_addr));
1736 dst = &sdst->sin6_addr;
1737 if (sgw->sin6_family == AF_INET6) {
1739 gw = &sgw->sin6_addr;
1740 prefix = sin6mask2len(smask);
1741 } else if (sgw->sin6_family == AF_LINK) {
1743 * Interface route... a hard case. We need to get the prefix
1744 * length from the kernel, but we now are parsing rtmsg.
1745 * We'll purge matching routes from my list, then get the
1748 struct riprt *longest;
1749 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1752 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
1753 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1755 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1757 || longest->rrt_info.rip6_plen <
1758 rrt->rrt_info.rip6_plen) {
1765 trace(1, "\tno matching interface route found\n");
1768 gw = &in6addr_loopback;
1769 prefix = rrt->rrt_info.rip6_plen;
1771 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1775 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1776 trace(1, "gw %s\n", inet6_n2p(gw));
1777 t_lifetime = time(NULL) - RIP_LIFETIME;
1778 /* age route for interface address */
1779 memset(&ni6, 0, sizeof(ni6));
1780 ni6.rip6_dest = *dst;
1781 ni6.rip6_plen = prefix;
1782 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1783 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
1785 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
1786 trace(1, "\tno route found\n");
1790 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
1791 trace(1, "\tyou can delete static routes only\n");
1794 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
1795 trace(1, "\tgw mismatch: %s <-> ",
1796 inet6_n2p(&rrt->rrt_gw));
1797 trace(1, "%s\n", inet6_n2p(gw));
1799 trace(1, "\troute found, age it\n");
1800 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1801 rrt->rrt_t = t_lifetime;
1802 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1809 * remove specified address from internal interface/routing table.
1812 rt_deladdr(struct ifc *ifcp, const struct sockaddr_in6 *sifa,
1813 const struct sockaddr_in6 *smask)
1815 const struct in6_addr *addr = NULL;
1817 struct ifac *ifa = NULL;
1818 struct netinfo6 ni6;
1819 struct riprt *rrt = NULL;
1823 if (sifa->sin6_family != AF_INET6) {
1824 trace(1, "\tother AF, ignored\n");
1827 addr = &sifa->sin6_addr;
1828 prefix = sin6mask2len(smask);
1830 trace(1, "\tdeleting %s/%d from %s\n",
1831 inet6_n2p(addr), prefix, ifcp->ifc_name);
1832 ifa = ifa_match(ifcp, addr, prefix);
1834 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1835 inet6_n2p(addr), prefix, ifcp->ifc_name);
1838 if (ifa->ifa_conf != ifcp) {
1839 trace(1, "\taddress table corrupt: back pointer does not match "
1841 ifcp->ifc_name, ifa->ifa_conf->ifc_name);
1844 /* remove ifa from interface */
1845 if (ifcp->ifc_addr == ifa)
1846 ifcp->ifc_addr = ifa->ifa_next;
1849 for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
1850 if (p->ifa_next == ifa) {
1851 p->ifa_next = ifa->ifa_next;
1856 ifa->ifa_next = NULL;
1857 ifa->ifa_conf = NULL;
1858 t_lifetime = time(NULL) - RIP_LIFETIME;
1859 /* age route for interface address */
1860 memset(&ni6, 0, sizeof(ni6));
1861 ni6.rip6_dest = ifa->ifa_addr;
1862 ni6.rip6_plen = ifa->ifa_plen;
1863 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
1864 trace(1, "\tfind interface route %s/%d on %d\n",
1865 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
1866 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1867 struct in6_addr none;
1868 memset(&none, 0, sizeof(none));
1869 if (rrt->rrt_index == ifcp->ifc_index &&
1870 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
1871 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
1872 trace(1, "\troute found, age it\n");
1873 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1874 rrt->rrt_t = t_lifetime;
1875 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1879 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1880 inet6_n2p(&rrt->rrt_info.rip6_dest),
1881 rrt->rrt_info.rip6_plen,
1885 trace(1, "\tno interface route found\n");
1886 /* age route for p2p destination */
1887 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1888 memset(&ni6, 0, sizeof(ni6));
1889 ni6.rip6_dest = ifa->ifa_raddr;
1890 ni6.rip6_plen = 128;
1891 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1892 trace(1, "\tfind p2p route %s/%d on %d\n",
1893 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
1895 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1896 if (rrt->rrt_index == ifcp->ifc_index &&
1897 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
1898 trace(1, "\troute found, age it\n");
1899 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1900 rrt->rrt_t = t_lifetime;
1901 rrt->rrt_info.rip6_metric =
1906 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1907 inet6_n2p(&rrt->rrt_info.rip6_dest),
1908 rrt->rrt_info.rip6_plen,
1912 trace(1, "\tno p2p route found\n");
1914 return updated ? 0 : -1;
1918 * Get each interface address and put those interface routes to the route
1922 ifrt(struct ifc *ifcp, int again)
1925 struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
1926 struct netinfo6 *np;
1928 int need_trigger = 0;
1930 if (ifcp->ifc_flags & IFF_LOOPBACK)
1931 return 0; /* ignore loopback */
1932 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1933 ifrt_p2p(ifcp, again);
1937 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
1938 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1940 trace(1, "route: %s on %s: "
1941 "skip linklocal interface address\n",
1942 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
1946 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
1948 trace(1, "route: %s: skip unspec interface address\n",
1953 if (ifcp->ifc_flags & IFF_UP) {
1954 if ((rrt = MALLOC(struct riprt)) == NULL)
1955 fatal("malloc: struct riprt");
1956 memset(rrt, 0, sizeof(*rrt));
1957 rrt->rrt_same = NULL;
1958 rrt->rrt_index = ifcp->ifc_index;
1959 rrt->rrt_t = 0; /* don't age */
1960 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
1961 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
1962 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
1963 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
1964 rrt->rrt_flags = RTF_CLONING;
1965 rrt->rrt_rflags |= RRTF_CHANGED;
1966 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
1967 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
1969 /* XXX why gateway address == network adddress? */
1970 rrt->rrt_gw = ifa->ifa_addr;
1972 np = &rrt->rrt_info;
1973 search_rrt = rtsearch(np, &prev_rrt);
1974 if (search_rrt != NULL) {
1975 if (search_rrt->rrt_info.rip6_metric >
1976 rrt->rrt_info.rip6_metric) {
1978 prev_rrt->rrt_next = rrt->rrt_next;
1980 riprt = rrt->rrt_next;
1981 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1984 /* Already have better route */
1986 trace(1, "route: %s/%d: "
1987 "already registered (%s)\n",
1988 inet6_n2p(&np->rip6_dest), np->rip6_plen,
1995 /* Attach the route to the list */
1996 trace(1, "route: %s/%d: register route (%s)\n",
1997 inet6_n2p(&np->rip6_dest), np->rip6_plen,
1999 rrt->rrt_next = riprt;
2001 addroute(rrt, &rrt->rrt_gw, ifcp);
2003 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2006 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
2007 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2008 t_lifetime = time(NULL) - RIP_LIFETIME;
2009 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2010 loop_rrt->rrt_t = t_lifetime;
2011 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2012 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2019 return need_trigger;
2023 * there are couple of p2p interface routing models. "behavior" lets
2024 * you pick one. it looks that gated behavior fits best with BSDs,
2025 * since BSD kernels does not look at prefix length on p2p interfaces.
2028 ifrt_p2p(struct ifc *ifcp, int again)
2031 struct riprt *rrt, *orrt, *prevrrt;
2032 struct netinfo6 *np;
2033 struct in6_addr addr, dest;
2034 int advert, ignore, i;
2035 #define P2PADVERT_NETWORK 1
2036 #define P2PADVERT_ADDR 2
2037 #define P2PADVERT_DEST 4
2038 #define P2PADVERT_MAX 4
2039 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2040 const char *category = "";
2043 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2044 addr = ifa->ifa_addr;
2045 dest = ifa->ifa_raddr;
2046 applyplen(&addr, ifa->ifa_plen);
2047 applyplen(&dest, ifa->ifa_plen);
2048 advert = ignore = 0;
2052 * honor addr/plen, just like normal shared medium
2053 * interface. this may cause trouble if you reuse
2054 * addr/plen on other interfaces.
2056 * advertise addr/plen.
2058 advert |= P2PADVERT_NETWORK;
2062 * prefixlen on p2p interface is meaningless.
2063 * advertise addr/128 and dest/128.
2065 * do not install network route to route6d routing
2066 * table (if we do, it would prevent route installation
2067 * for other p2p interface that shares addr/plen).
2069 * XXX what should we do if dest is ::? it will not
2070 * get announced anyways (see following filter),
2071 * but we need to think.
2073 advert |= P2PADVERT_ADDR;
2074 advert |= P2PADVERT_DEST;
2075 ignore |= P2PADVERT_NETWORK;
2079 * just for testing. actually the code is redundant
2080 * given the current p2p interface address assignment
2081 * rule for kame kernel.
2084 * A/n -> announce A/n
2085 * A B/n, A and B share prefix -> A/n (= B/n)
2086 * A B/n, do not share prefix -> A/128 and B/128
2087 * actually, A/64 and A B/128 are the only cases
2088 * permitted by the kernel:
2090 * A B/128 -> A/128 and B/128
2092 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
2093 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2094 advert |= P2PADVERT_NETWORK;
2096 advert |= P2PADVERT_ADDR;
2097 advert |= P2PADVERT_DEST;
2098 ignore |= P2PADVERT_NETWORK;
2101 advert |= P2PADVERT_NETWORK;
2105 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2106 if ((ignore & i) != 0)
2108 if ((rrt = MALLOC(struct riprt)) == NULL) {
2109 fatal("malloc: struct riprt");
2112 memset(rrt, 0, sizeof(*rrt));
2113 rrt->rrt_same = NULL;
2114 rrt->rrt_index = ifcp->ifc_index;
2115 rrt->rrt_t = 0; /* don't age */
2117 case P2PADVERT_NETWORK:
2118 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2119 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2120 applyplen(&rrt->rrt_info.rip6_dest,
2122 category = "network";
2124 case P2PADVERT_ADDR:
2125 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2126 rrt->rrt_info.rip6_plen = 128;
2127 rrt->rrt_gw = in6addr_loopback;
2130 case P2PADVERT_DEST:
2131 rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
2132 rrt->rrt_info.rip6_plen = 128;
2133 rrt->rrt_gw = ifa->ifa_addr;
2137 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2138 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2140 trace(1, "route: %s: skip unspec/linklocal "
2141 "(%s on %s)\n", category, ifcp->ifc_name);
2146 if ((advert & i) == 0) {
2147 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2151 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2152 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2153 np = &rrt->rrt_info;
2154 orrt = rtsearch(np, &prevrrt);
2156 /* Attach the route to the list */
2157 trace(1, "route: %s/%d: register route "
2159 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2160 category, ifcp->ifc_name, noadv);
2161 rrt->rrt_next = riprt;
2163 } else if (rrt->rrt_index != orrt->rrt_index ||
2164 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2166 rrt->rrt_next = orrt->rrt_next;
2168 prevrrt->rrt_next = rrt;
2173 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2174 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2175 category, ifcp->ifc_name, noadv);
2179 trace(1, "route: %s/%d: "
2180 "already registered (%s on %s%s)\n",
2181 inet6_n2p(&np->rip6_dest),
2182 np->rip6_plen, category,
2183 ifcp->ifc_name, noadv);
2189 #undef P2PADVERT_NETWORK
2190 #undef P2PADVERT_ADDR
2191 #undef P2PADVERT_DEST
2192 #undef P2PADVERT_MAX
2196 getifmtu(int ifindex)
2201 struct if_msghdr *ifm;
2208 mib[4] = NET_RT_IFLIST;
2210 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2211 fatal("sysctl estimate NET_RT_IFLIST");
2214 if ((buf = malloc(msize)) == NULL) {
2218 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2219 fatal("sysctl NET_RT_IFLIST");
2222 ifm = (struct if_msghdr *)buf;
2223 mtu = ifm->ifm_data.ifi_mtu;
2224 #ifdef __DragonFly__
2225 if (ifindex != ifm->ifm_index) {
2226 fatal("ifindex does not match with ifm_index");
2235 rttypes(struct rt_msghdr *rtm)
2237 #define RTTYPE(s, f) \
2239 if (rtm->rtm_type == (f)) \
2242 RTTYPE("ADD", RTM_ADD);
2243 RTTYPE("DELETE", RTM_DELETE);
2244 RTTYPE("CHANGE", RTM_CHANGE);
2245 RTTYPE("GET", RTM_GET);
2246 RTTYPE("LOSING", RTM_LOSING);
2247 RTTYPE("REDIRECT", RTM_REDIRECT);
2248 RTTYPE("MISS", RTM_MISS);
2249 RTTYPE("LOCK", RTM_LOCK);
2250 RTTYPE("OLDADD", RTM_OLDADD);
2251 RTTYPE("OLDDEL", RTM_OLDDEL);
2252 RTTYPE("RESOLVE", RTM_RESOLVE);
2253 RTTYPE("NEWADDR", RTM_NEWADDR);
2254 RTTYPE("DELADDR", RTM_DELADDR);
2255 RTTYPE("IFINFO", RTM_IFINFO);
2257 RTTYPE("OLDADD", RTM_OLDADD);
2260 RTTYPE("OLDDEL", RTM_OLDDEL);
2263 RTTYPE("OIFINFO", RTM_OIFINFO);
2265 #ifdef RTM_IFANNOUNCE
2266 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2269 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2272 RTTYPE("DELMADDR", RTM_DELMADDR);
2279 rtflags(struct rt_msghdr *rtm)
2281 static char buf[BUFSIZ];
2284 * letter conflict should be okay. painful when *BSD diverges...
2286 strlcpy(buf, "", sizeof(buf));
2287 #define RTFLAG(s, f) \
2289 if (rtm->rtm_flags & (f)) \
2290 strlcat(buf, (s), sizeof(buf)); \
2292 RTFLAG("U", RTF_UP);
2293 RTFLAG("G", RTF_GATEWAY);
2294 RTFLAG("H", RTF_HOST);
2295 RTFLAG("R", RTF_REJECT);
2296 RTFLAG("D", RTF_DYNAMIC);
2297 RTFLAG("M", RTF_MODIFIED);
2298 RTFLAG("d", RTF_DONE);
2300 RTFLAG("m", RTF_MASK);
2302 RTFLAG("C", RTF_CLONING);
2304 RTFLAG("c", RTF_CLONED);
2306 #ifdef RTF_PRCLONING
2307 RTFLAG("c", RTF_PRCLONING);
2309 #ifdef RTF_WASCLONED
2310 RTFLAG("W", RTF_WASCLONED);
2312 RTFLAG("X", RTF_XRESOLVE);
2313 RTFLAG("L", RTF_LLINFO);
2314 RTFLAG("S", RTF_STATIC);
2315 RTFLAG("B", RTF_BLACKHOLE);
2317 RTFLAG("3", RTF_PROTO3);
2319 RTFLAG("2", RTF_PROTO2);
2320 RTFLAG("1", RTF_PROTO1);
2321 #ifdef RTF_BROADCAST
2322 RTFLAG("b", RTF_BROADCAST);
2325 RTFLAG("d", RTF_DEFAULT);
2327 #ifdef RTF_ISAROUTER
2328 RTFLAG("r", RTF_ISAROUTER);
2331 RTFLAG("T", RTF_TUNNEL);
2334 RTFLAG("A", RTF_AUTH);
2337 RTFLAG("E", RTF_CRYPT);
2346 static char buf[BUFSIZ];
2348 strlcpy(buf, "", sizeof(buf));
2349 #define IFFLAG(s, f) \
2353 strlcat(buf, ",", sizeof(buf)); \
2354 strlcat(buf, s, sizeof(buf)); \
2357 IFFLAG("UP", IFF_UP);
2358 IFFLAG("BROADCAST", IFF_BROADCAST);
2359 IFFLAG("DEBUG", IFF_DEBUG);
2360 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2361 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2362 #ifdef IFF_NOTRAILERS
2363 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2366 IFFLAG("SMART", IFF_SMART);
2368 IFFLAG("RUNNING", IFF_RUNNING);
2369 IFFLAG("NOARP", IFF_NOARP);
2370 IFFLAG("PROMISC", IFF_PROMISC);
2371 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2372 IFFLAG("OACTIVE", IFF_OACTIVE);
2373 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2374 IFFLAG("LINK0", IFF_LINK0);
2375 IFFLAG("LINK1", IFF_LINK1);
2376 IFFLAG("LINK2", IFF_LINK2);
2377 IFFLAG("MULTICAST", IFF_MULTICAST);
2387 char *buf, *p, *lim;
2388 struct rt_msghdr *rtm;
2397 mib[3] = AF_INET6; /* Address family */
2398 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2399 mib[5] = 0; /* No flags */
2405 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2406 errmsg = "sysctl estimate";
2409 if ((buf = malloc(msize)) == NULL) {
2413 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2414 errmsg = "sysctl NET_RT_DUMP";
2417 } while (retry < 5 && errmsg != NULL);
2419 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2422 } else if (1 < retry)
2423 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2426 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2427 rtm = (struct rt_msghdr *)p;
2428 rt_entry(rtm, again);
2434 rt_entry(struct rt_msghdr *rtm, int again)
2436 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2437 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2438 char *rtmp, *ifname = NULL;
2439 struct riprt *rrt, *orrt;
2440 struct netinfo6 *np;
2443 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = NULL;
2444 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2445 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
2446 return; /* not interested in the link route */
2448 /* do not look at cloned routes */
2449 #ifdef RTF_WASCLONED
2450 if (rtm->rtm_flags & RTF_WASCLONED)
2454 if (rtm->rtm_flags & RTF_CLONED)
2458 * do not look at dynamic routes.
2459 * netbsd/openbsd cloned routes have UGHD.
2461 if (rtm->rtm_flags & RTF_DYNAMIC)
2463 rtmp = (char *)(rtm + 1);
2465 if ((rtm->rtm_addrs & RTA_DST) == 0)
2466 return; /* ignore routes without destination address */
2467 sin6_dst = (struct sockaddr_in6 *)rtmp;
2468 rtmp += ROUNDUP(sin6_dst->sin6_len);
2469 if (rtm->rtm_addrs & RTA_GATEWAY) {
2470 sin6_gw = (struct sockaddr_in6 *)rtmp;
2471 rtmp += ROUNDUP(sin6_gw->sin6_len);
2473 if (rtm->rtm_addrs & RTA_NETMASK) {
2474 sin6_mask = (struct sockaddr_in6 *)rtmp;
2475 rtmp += ROUNDUP(sin6_mask->sin6_len);
2477 if (rtm->rtm_addrs & RTA_GENMASK) {
2478 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2479 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2481 if (rtm->rtm_addrs & RTA_IFP) {
2482 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2483 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2487 if (sin6_dst->sin6_family != AF_INET6)
2489 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2490 return; /* Link-local */
2491 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2492 return; /* Loopback */
2493 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2496 if ((rrt = MALLOC(struct riprt)) == NULL) {
2497 fatal("malloc: struct riprt");
2500 memset(rrt, 0, sizeof(*rrt));
2501 np = &rrt->rrt_info;
2502 rrt->rrt_same = NULL;
2503 rrt->rrt_t = time(NULL);
2504 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2505 rrt->rrt_t = 0; /* Don't age static routes */
2507 np->rip6_tag = htons(routetag & 0xffff);
2511 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2512 if (np->rip6_metric < 1)
2513 np->rip6_metric = 1;
2514 rrt->rrt_flags = rtm->rtm_flags;
2515 np->rip6_dest = sin6_dst->sin6_addr;
2518 if (rtm->rtm_flags & RTF_HOST)
2519 np->rip6_plen = 128; /* Host route */
2521 np->rip6_plen = sin6mask2len(sin6_mask);
2525 orrt = rtsearch(np, NULL);
2526 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2529 trace(1, "route: %s/%d flags %s: already registered\n",
2530 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2538 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2540 if (sin6_gw->sin6_family == AF_INET6)
2541 rrt->rrt_gw = sin6_gw->sin6_addr;
2542 else if (sin6_gw->sin6_family == AF_LINK) {
2543 /* XXX in case ppp link? */
2544 rrt->rrt_gw = in6addr_loopback;
2546 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2548 trace(1, "route: %s/%d flags %s",
2549 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2550 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2554 if (s < nindex2ifc && index2ifc[s])
2555 ifname = index2ifc[s]->ifc_name;
2557 trace(1, " not configured\n");
2561 trace(1, " if %s sock %d", ifname, s);
2567 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2568 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
2569 #ifdef __DragonFly__
2570 && (rrt->rrt_flags & RTF_LOCAL) == 0
2573 trace(0, "***** Gateway %s is not a link-local address.\n",
2574 inet6_n2p(&rrt->rrt_gw));
2575 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2576 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2577 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2580 /* Put it to the route list */
2581 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2582 /* replace route list */
2583 rrt->rrt_next = orrt->rrt_next;
2585 trace(1, "route: %s/%d flags %s: replace new route\n",
2586 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2590 rrt->rrt_next = riprt;
2596 addroute(struct riprt *rrt, const struct in6_addr *gw, struct ifc *ifcp)
2598 struct netinfo6 *np;
2599 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2600 struct rt_msghdr *rtm;
2601 struct sockaddr_in6 *sin;
2604 np = &rrt->rrt_info;
2605 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2606 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2607 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2608 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2609 np->rip6_metric - 1, buf2);
2611 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2612 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2613 np->rip6_metric - 1, buf2);
2617 memset(buf, 0, sizeof(buf));
2618 rtm = (struct rt_msghdr *)buf;
2619 rtm->rtm_type = RTM_ADD;
2620 rtm->rtm_version = RTM_VERSION;
2621 rtm->rtm_seq = ++seq;
2623 rtm->rtm_flags = rrt->rrt_flags;
2624 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2625 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2626 rtm->rtm_inits = RTV_HOPCOUNT;
2627 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2629 sin->sin6_len = sizeof(struct sockaddr_in6);
2630 sin->sin6_family = AF_INET6;
2631 sin->sin6_addr = np->rip6_dest;
2632 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2634 sin->sin6_len = sizeof(struct sockaddr_in6);
2635 sin->sin6_family = AF_INET6;
2636 sin->sin6_addr = *gw;
2637 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2639 sin->sin6_len = sizeof(struct sockaddr_in6);
2640 sin->sin6_family = AF_INET6;
2641 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2642 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2644 len = (char *)sin - (char *)buf;
2645 rtm->rtm_msglen = len;
2646 if (write(rtsock, buf, len) > 0)
2649 if (errno == EEXIST) {
2650 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2651 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2653 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2654 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2656 trace(0, "Can not write to rtsock (addroute): %s\n",
2659 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2666 delroute(struct netinfo6 *np, struct in6_addr *gw)
2668 u_char buf[BUFSIZ], buf2[BUFSIZ];
2669 struct rt_msghdr *rtm;
2670 struct sockaddr_in6 *sin;
2673 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2674 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2675 np->rip6_plen, buf2);
2677 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2678 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2682 memset(buf, 0, sizeof(buf));
2683 rtm = (struct rt_msghdr *)buf;
2684 rtm->rtm_type = RTM_DELETE;
2685 rtm->rtm_version = RTM_VERSION;
2686 rtm->rtm_seq = ++seq;
2688 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2689 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2690 rtm->rtm_flags |= RTF_HOST;
2691 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2692 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2694 sin->sin6_len = sizeof(struct sockaddr_in6);
2695 sin->sin6_family = AF_INET6;
2696 sin->sin6_addr = np->rip6_dest;
2697 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2699 sin->sin6_len = sizeof(struct sockaddr_in6);
2700 sin->sin6_family = AF_INET6;
2701 sin->sin6_addr = *gw;
2702 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2704 sin->sin6_len = sizeof(struct sockaddr_in6);
2705 sin->sin6_family = AF_INET6;
2706 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2707 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2709 len = (char *)sin - (char *)buf;
2710 rtm->rtm_msglen = len;
2711 if (write(rtsock, buf, len) >= 0)
2714 if (errno == ESRCH) {
2715 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2716 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2718 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2719 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2721 trace(0, "Can not write to rtsock (delroute): %s\n",
2724 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2731 getroute(struct netinfo6 *np, struct in6_addr *gw)
2736 struct rt_msghdr *rtm;
2737 struct sockaddr_in6 *sin;
2739 rtm = (struct rt_msghdr *)buf;
2740 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2741 memset(rtm, 0, len);
2742 rtm->rtm_type = RTM_GET;
2743 rtm->rtm_version = RTM_VERSION;
2745 rtm->rtm_seq = myseq;
2746 rtm->rtm_addrs = RTA_DST;
2747 rtm->rtm_msglen = len;
2748 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2749 sin->sin6_len = sizeof(struct sockaddr_in6);
2750 sin->sin6_family = AF_INET6;
2751 sin->sin6_addr = np->rip6_dest;
2752 if (write(rtsock, buf, len) < 0) {
2753 if (errno == ESRCH) /* No such route found */
2755 perror("write to rtsock");
2759 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2760 perror("read from rtsock");
2763 rtm = (struct rt_msghdr *)buf;
2764 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2765 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2766 if (rtm->rtm_addrs & RTA_DST) {
2767 sin = (struct sockaddr_in6 *)
2768 ((char *)sin + ROUNDUP(sin->sin6_len));
2770 if (rtm->rtm_addrs & RTA_GATEWAY) {
2771 *gw = sin->sin6_addr;
2778 inet6_n2p(const struct in6_addr *p)
2780 static char buf[BUFSIZ];
2782 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
2802 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2805 fprintf(dump, "%s: Interface Table Dump\n", hms());
2806 fprintf(dump, " Number of interfaces: %d\n", nifc);
2807 for (i = 0; i < 2; i++) {
2808 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
2809 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
2811 if ((ifcp->ifc_flags & IFF_UP) == 0)
2813 if (iff_find(ifcp, 'N') != NULL)
2816 if (ifcp->ifc_flags & IFF_UP)
2819 ifdump0(dump, ifcp);
2822 fprintf(dump, "\n");
2828 ifdump0(FILE *dump, const struct ifc *ifcp)
2836 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2837 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
2838 inet6_n2p(&ifcp->ifc_mylladdr),
2839 ifcp->ifc_mtu, ifcp->ifc_metric);
2840 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2841 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2842 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
2844 fprintf(dump, "\t%s/%d -- %s\n",
2845 inet6_n2p(&ifa->ifa_addr),
2846 ifa->ifa_plen, buf);
2848 fprintf(dump, "\t%s/%d\n",
2849 inet6_n2p(&ifa->ifa_addr),
2853 if (ifcp->ifc_filter) {
2854 fprintf(dump, "\tFilter:");
2855 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
2857 switch (iffp->iff_type) {
2859 ft = "Aggregate"; addr++; break;
2861 ft = "No-use"; break;
2863 ft = "Advertise-only"; addr++; break;
2865 ft = "Default-only"; break;
2867 ft = "Listen-only"; addr++; break;
2869 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
2874 fprintf(dump, " %s", ft);
2876 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
2880 fprintf(dump, "\n");
2895 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2899 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
2900 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
2901 if (rrt->rrt_t == 0)
2904 age = t - rrt->rrt_t;
2905 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
2907 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2908 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
2909 index2ifc[rrt->rrt_index]->ifc_name,
2910 inet6_n2p(&rrt->rrt_gw),
2911 rrt->rrt_info.rip6_metric, (long)age);
2912 if (rrt->rrt_info.rip6_tag) {
2913 fprintf(dump, " tag(0x%04x)",
2914 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
2916 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
2917 fprintf(dump, " NOT-LL");
2918 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
2919 fprintf(dump, " NO-ADV");
2920 fprintf(dump, "\n");
2922 fprintf(dump, "\n");
2928 * Parse the -A (and -O) options and put corresponding filter object to the
2929 * specified interface structures. Each of the -A/O option has the following
2930 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2931 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2937 char *p, *ap, *iflp, *ifname;
2938 struct iff ftmp, *iff_obj;
2945 for (i = 0; i < nfilter; i++) {
2949 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
2953 if ((p = strchr(ap, ',')) != NULL) {
2957 if ((p = strchr(ap, '/')) == NULL) {
2958 fatal("no prefixlen specified for '%s'", ap);
2962 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
2963 fatal("invalid prefix specified for '%s'", ap);
2966 ftmp.iff_plen = atoi(p);
2967 ftmp.iff_next = NULL;
2968 applyplen(&ftmp.iff_addr, ftmp.iff_plen);
2970 ftmp.iff_type = filtertype[i];
2971 if (iflp == NULL || *iflp == '\0') {
2972 fatal("no interface specified for '%s'", ap);
2975 /* parse the interface listing portion */
2978 if ((iflp = strchr(iflp, ',')) != NULL)
2980 ifcp = ifc_find(ifname);
2982 fatal("no interface %s exists", ifname);
2985 iff_obj = (struct iff *)malloc(sizeof(struct iff));
2986 if (iff_obj == NULL) {
2987 fatal("malloc of iff_obj");
2990 memcpy((void *)iff_obj, (void *)&ftmp,
2991 sizeof(struct iff));
2992 /* link it to the interface filter */
2993 iff_obj->iff_next = ifcp->ifc_filter;
2994 ifcp->ifc_filter = iff_obj;
2998 * -A: aggregate configuration.
3000 if (filtertype[i] != 'A')
3002 /* put the aggregate to the kernel routing table */
3003 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3005 fatal("malloc: rrt");
3008 memset(rrt, 0, sizeof(struct riprt));
3009 rrt->rrt_info.rip6_dest = ftmp.iff_addr;
3010 rrt->rrt_info.rip6_plen = ftmp.iff_plen;
3011 rrt->rrt_info.rip6_metric = 1;
3012 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3013 rrt->rrt_gw = in6addr_loopback;
3014 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3015 rrt->rrt_rflags = RRTF_AGGREGATE;
3017 rrt->rrt_index = loopifindex;
3019 if (getroute(&rrt->rrt_info, &gw)) {
3022 * When the address has already been registered in the
3023 * kernel routing table, it should be removed
3025 delroute(&rrt->rrt_info, &gw);
3027 /* it is safer behavior */
3029 fatal("%s/%u already in routing table, "
3031 inet6_n2p(&rrt->rrt_info.rip6_dest),
3032 rrt->rrt_info.rip6_plen);
3037 /* Put the route to the list */
3038 rrt->rrt_next = riprt;
3040 trace(1, "Aggregate: %s/%d for %s\n",
3041 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
3043 /* Add this route to the kernel */
3044 if (nflag) /* do not modify kernel routing table */
3046 addroute(rrt, &in6addr_loopback, loopifcp);
3050 /***************** utility functions *****************/
3053 * Returns a pointer to ifac whose address and prefix length matches
3054 * with the address and prefix length specified in the arguments.
3057 ifa_match(const struct ifc *ifcp, const struct in6_addr *ia, int plen)
3061 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
3062 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
3063 ifa->ifa_plen == plen)
3070 * Return a pointer to riprt structure whose address and prefix length
3071 * matches with the address and prefix length found in the argument.
3072 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3075 rtsearch(struct netinfo6 *np, struct riprt **prev_rrt)
3081 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
3082 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3083 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3095 sin6mask2len(const struct sockaddr_in6 *sin6)
3097 return mask2len(&sin6->sin6_addr,
3098 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3102 mask2len(const struct in6_addr *addr, int lenlim)
3105 const u_char *p = (const u_char *)addr;
3107 for (j = 0; j < lenlim; j++, p++) {
3114 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3115 MASKLEN(0xfe, 7); break;
3116 MASKLEN(0xfc, 6); break;
3117 MASKLEN(0xf8, 5); break;
3118 MASKLEN(0xf0, 4); break;
3119 MASKLEN(0xe0, 3); break;
3120 MASKLEN(0xc0, 2); break;
3121 MASKLEN(0x80, 1); break;
3129 applymask(struct in6_addr *addr, struct in6_addr *mask)
3134 p = (u_long *)addr; q = (u_long *)mask;
3135 for (i = 0; i < 4; i++)
3139 static const u_char plent[8] = {
3140 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3144 applyplen(struct in6_addr *ia, int plen)
3150 for (i = 0; i < 16; i++) {
3159 static const int pl2m[9] = {
3160 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3166 static struct in6_addr ia;
3170 memset(&ia, 0, sizeof(struct in6_addr));
3172 for (i = 0; i < 16; i++, p++, n -= 8) {
3186 char *q = (char *)malloc(strlen(p) + 1);
3195 static char buf[BUFSIZ];
3200 if ((tm = localtime(&t)) == NULL) {
3204 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3209 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3212 ripinterval(int timer)
3216 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3217 nextalarm = time(NULL) + interval;
3227 t = (int)(RIP_TRIG_INT6_MIN +
3228 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3229 sup_trig_update = time(NULL) + t;
3234 fatal(const char *fmt, ...)
3240 vsnprintf(buf, sizeof(buf), fmt, ap);
3242 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3248 tracet(int level, const char *fmt, ...)
3253 if (level <= dflag) {
3254 fprintf(stderr, "%s: ", hms());
3255 vfprintf(stderr, fmt, ap);
3259 vsyslog(LOG_DEBUG, fmt, ap);
3261 vsyslog(LOG_WARNING, fmt, ap);
3267 trace(int level, const char *fmt, ...)
3273 vfprintf(stderr, fmt, ap);
3276 vsyslog(LOG_DEBUG, fmt, ap);
3278 vsyslog(LOG_WARNING, fmt, ap);
3286 struct if_nameindex *p, *p0;
3287 unsigned int max = 0;
3289 p0 = if_nameindex();
3290 for (p = p0; p && p->if_index && p->if_name; p++) {
3291 if (max < p->if_index)
3294 if_freenameindex(p0);
3299 ifc_find(char *name)
3303 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
3304 if (strcmp(name, ifcp->ifc_name) == 0)
3311 iff_find(struct ifc *ifcp, int type)
3315 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
3316 if (iffp->iff_type == type)
3323 setindex2ifc(int idx, struct ifc *ifcp)
3329 nindex2ifc = 5; /*initial guess*/
3330 index2ifc = (struct ifc **)
3331 malloc(sizeof(*index2ifc) * nindex2ifc);
3332 if (index2ifc == NULL) {
3336 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3339 while (nindex2ifc <= idx)
3341 if (n != nindex2ifc) {
3342 p = (struct ifc **)realloc(index2ifc,
3343 sizeof(*index2ifc) * nindex2ifc);
3348 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3351 index2ifc[idx] = ifcp;