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 void sighandler(int);
209 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
210 int out_filter(struct riprt *, struct ifc *);
212 void sockopt(struct ifc *);
214 void ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
216 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
217 const struct sockaddr_in6 *);
218 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
219 const struct sockaddr_in6 *);
220 void filterconfig(void);
222 const char *rttypes(struct rt_msghdr *);
223 const char *rtflags(struct rt_msghdr *);
224 const char *ifflags(int);
225 int ifrt(struct ifc *, int);
226 void ifrt_p2p(struct ifc *, int);
227 void applymask(struct in6_addr *, struct in6_addr *);
228 void applyplen(struct in6_addr *, int);
231 void ifdump0(FILE *, const struct ifc *);
233 void rt_entry(struct rt_msghdr *, int);
235 void riprequest(struct ifc *, struct netinfo6 *, int,
236 struct sockaddr_in6 *);
237 void ripflush(struct ifc *, struct sockaddr_in6 *);
238 void sendrequest(struct ifc *);
239 int sin6mask2len(const struct sockaddr_in6 *);
240 int mask2len(const struct in6_addr *, int);
241 int sendpacket(struct sockaddr_in6 *, int);
242 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
243 int delroute(struct netinfo6 *, struct in6_addr *);
244 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
246 int tobeadv(struct riprt *, struct ifc *);
247 char *allocopy(char *);
249 const char *inet6_n2p(const struct in6_addr *);
250 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
251 struct in6_addr *plen2mask(int);
252 struct riprt *rtsearch(struct netinfo6 *, struct riprt **);
253 int ripinterval(int);
254 time_t ripsuptrig(void);
255 void fatal(const char *, ...)
256 __attribute__((__format__(__printf__, 1, 2)));
257 void trace(int, const char *, ...)
258 __attribute__((__format__(__printf__, 2, 3)));
259 void tracet(int, const char *, ...)
260 __attribute__((__format__(__printf__, 2, 3)));
261 unsigned int if_maxindex(void);
262 struct ifc *ifc_find(char *);
263 struct iff *iff_find(struct ifc *, int);
264 void setindex2ifc(int, struct ifc *);
266 #define MALLOC(type) ((type *)malloc(sizeof(type)))
269 main(int argc, char **argv)
274 sigset_t mask, omask;
279 progname = strrchr(*argv, '/');
286 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
293 if (nfilter >= MAXFILTER) {
294 fatal("Exceeds MAXFILTER");
297 filtertype[nfilter] = ch;
298 filter[nfilter++] = allocopy(optarg);
302 routetag = strtoul(optarg, &ep, 0);
303 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
304 fatal("invalid route tag");
309 if ((rtlog = fopen(optarg, "w")) == NULL) {
310 fatal("Can not write to routelog");
314 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
315 FLAG('a', aflag, 1); break;
316 FLAG('d', dflag, 1); break;
317 FLAG('D', dflag, 2); break;
318 FLAG('h', hflag, 1); break;
319 FLAG('l', lflag, 1); break;
320 FLAG('n', nflag, 1); break;
321 FLAG('q', qflag, 1); break;
322 FLAG('s', sflag, 1); break;
323 FLAG('S', Sflag, 1); break;
326 fatal("Invalid option specified, terminating");
333 fatal("bogus extra arguments");
339 fprintf(stderr, "No kernel update is allowed\n");
341 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
344 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
346 memset(ripbuf, 0, RIP6_MAXMTU);
347 ripbuf->rip6_cmd = RIP6_RESPONSE;
348 ripbuf->rip6_vers = RIP6_VERSION;
349 ripbuf->rip6_res1[0] = 0;
350 ripbuf->rip6_res1[1] = 0;
354 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
355 if (ifcp->ifc_index < 0) {
357 "No ifindex found at %s (no link-local address?)\n",
364 if (loopifcp == NULL) {
365 fatal("No loopback found");
368 loopifindex = loopifcp->ifc_index;
369 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
378 if (daemon(0, 0) < 0) {
392 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
393 fprintf(pidfile, "%d\n", pid);
397 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
401 memset(ripbuf, 0, RIP6_MAXMTU);
402 ripbuf->rip6_cmd = RIP6_RESPONSE;
403 ripbuf->rip6_vers = RIP6_VERSION;
404 ripbuf->rip6_res1[0] = 0;
405 ripbuf->rip6_res1[1] = 0;
407 if (signal(SIGALRM, sighandler) == SIG_ERR ||
408 signal(SIGQUIT, sighandler) == SIG_ERR ||
409 signal(SIGTERM, sighandler) == SIG_ERR ||
410 signal(SIGUSR1, sighandler) == SIG_ERR ||
411 signal(SIGHUP, sighandler) == SIG_ERR ||
412 signal(SIGINT, sighandler) == SIG_ERR) {
417 * To avoid rip packet congestion (not on a cable but in this
418 * process), wait for a moment to send the first RIP6_RESPONSE
421 alarm(ripinterval(INIT_INTERVAL6));
423 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
424 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
428 syslog(LOG_INFO, "**** Started ****");
430 sigaddset(&mask, SIGALRM);
450 FD_COPY(&sockvec, &recvec);
452 switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
454 if (errno != EINTR) {
462 if (FD_ISSET(ripsock, &recvec)) {
463 sigprocmask(SIG_BLOCK, &mask, &omask);
465 sigprocmask(SIG_SETMASK, &omask, NULL);
467 if (FD_ISSET(rtsock, &recvec)) {
468 sigprocmask(SIG_BLOCK, &mask, &omask);
470 sigprocmask(SIG_SETMASK, &omask, NULL);
498 * gracefully exits after resetting sockopts.
507 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
508 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
509 delroute(&rrt->rrt_info, &rrt->rrt_gw);
514 syslog(LOG_INFO, "**** Terminated ****");
520 * Called periodically:
521 * 1. age out the learned route. remove it if necessary.
522 * 2. submit RIP6_RESPONSE packets.
523 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
524 * to invoke this function in every 1 or 5 or 10 seconds only to age the
525 * routes more precisely.
532 struct riprt *rrt, *rrt_prev, *rrt_next;
533 time_t t_lifetime, t_holddown;
535 /* age the RIP routes */
537 t_lifetime = time(NULL) - RIP_LIFETIME;
538 t_holddown = t_lifetime - RIP_HOLDDOWN;
539 for (rrt = riprt; rrt; rrt = rrt_next) {
540 rrt_next = rrt->rrt_next;
542 if (rrt->rrt_t == 0) {
546 if (rrt->rrt_t < t_holddown) {
548 rrt_prev->rrt_next = rrt->rrt_next;
550 riprt = rrt->rrt_next;
552 delroute(&rrt->rrt_info, &rrt->rrt_gw);
556 if (rrt->rrt_t < t_lifetime)
557 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
561 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
562 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
563 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
565 alarm(ripinterval(SUPPLY_INTERVAL6));
571 int i, int0, int255, error;
572 struct addrinfo hints, *res;
577 nindex2ifc = 0; /*initial guess*/
579 snprintf(port, sizeof(port), "%d", RIP6_PORT);
581 memset(&hints, 0, sizeof(hints));
582 hints.ai_family = PF_INET6;
583 hints.ai_socktype = SOCK_DGRAM;
584 hints.ai_flags = AI_PASSIVE;
585 error = getaddrinfo(NULL, port, &hints, &res);
587 fatal("%s", gai_strerror(error));
591 fatal(":: resolved to multiple address");
595 int0 = 0; int255 = 255;
596 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
601 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
605 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
606 &int255, sizeof(int255)) < 0) {
607 fatal("rip IPV6_MULTICAST_HOPS");
610 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
611 &int0, sizeof(int0)) < 0) {
612 fatal("rip IPV6_MULTICAST_LOOP");
617 #ifdef IPV6_RECVPKTINFO
618 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
620 fatal("rip IPV6_RECVPKTINFO");
623 #else /* old adv. API */
624 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
626 fatal("rip IPV6_PKTINFO");
631 memset(&hints, 0, sizeof(hints));
632 hints.ai_family = PF_INET6;
633 hints.ai_socktype = SOCK_DGRAM;
634 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
636 fatal("%s", gai_strerror(error));
640 fatal("%s resolved to multiple address", RIP6_DEST);
643 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
648 memset(&sockvec, 0, sizeof(sockvec));
650 FD_SET(ripsock, &sockvec);
653 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
654 fatal("route socket");
657 FD_SET(rtsock, &sockvec);
659 rtsock = -1; /*just for safety */
663 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
666 * ripflush flushes the rip datagram stored in the rip buffer
669 static struct netinfo6 *np;
672 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin)
678 tracet(1, "Send(%s): info(%d) to %s.%d\n",
680 inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
682 tracet(1, "Send: info(%d) to %s.%d\n",
683 nrt, inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
685 np = ripbuf->rip6_nets;
686 for (i = 0; i < nrt; i++, np++) {
687 if (np->rip6_metric == NEXTHOP_METRIC) {
688 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
689 trace(2, " NextHop reset");
691 trace(2, " NextHop %s",
692 inet6_n2p(&np->rip6_dest));
695 trace(2, " %s/%d[%d]",
696 inet6_n2p(&np->rip6_dest),
697 np->rip6_plen, np->rip6_metric);
700 trace(2, " tag=0x%04x",
701 ntohs(np->rip6_tag) & 0xffff);
706 error = sendpacket(sin, RIPSIZE(nrt));
707 if (error == EAFNOSUPPORT) {
708 /* Protocol not supported */
709 tracet(1, "Could not send info to %s (%s): "
711 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
712 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
714 nrt = 0; np = ripbuf->rip6_nets;
718 * Generate RIP6_RESPONSE packets and send them.
721 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin, int flag)
724 struct in6_addr *nh; /* next hop */
729 * Request from non-link local address is not
730 * a regular route6d update.
732 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
733 sizeof(struct udphdr) -
734 sizeof(struct rip6) + sizeof(struct netinfo6)) /
735 sizeof(struct netinfo6);
736 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
737 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
738 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
740 /* Put the route to the buffer */
748 if (nrt) /* Send last packet */
753 if ((flag & RRTF_SENDANYWAY) == 0 &&
754 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
758 if (iff_find(ifcp, 'N') != NULL)
761 /* -T: generate default route only */
762 if (iff_find(ifcp, 'T') != NULL) {
763 struct netinfo6 rrt_info;
764 memset(&rrt_info, 0, sizeof(struct netinfo6));
765 rrt_info.rip6_dest = in6addr_any;
766 rrt_info.rip6_plen = 0;
767 rrt_info.rip6_metric = 1;
768 rrt_info.rip6_metric += ifcp->ifc_metric;
769 rrt_info.rip6_tag = htons(routetag & 0xffff);
770 np = ripbuf->rip6_nets;
777 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
778 sizeof(struct udphdr) -
779 sizeof(struct rip6) + sizeof(struct netinfo6)) /
780 sizeof(struct netinfo6);
782 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
783 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
784 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
787 /* Need to check filter here */
788 if (out_filter(rrt, ifcp) == 0)
791 /* Check split horizon and other conditions */
792 if (tobeadv(rrt, ifcp) == 0)
795 /* Only considers the routes with flag if specified */
796 if ((flag & RRTF_CHANGED) &&
797 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
801 if (rrt->rrt_index == ifcp->ifc_index &&
802 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
803 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
804 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
805 if (nrt == maxrte - 2)
807 np->rip6_dest = rrt->rrt_gw;
808 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
809 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
812 np->rip6_metric = NEXTHOP_METRIC;
816 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
817 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
818 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
820 if (nrt == maxrte - 2)
822 memset(np, 0, sizeof(struct netinfo6));
823 np->rip6_metric = NEXTHOP_METRIC;
828 /* Put the route to the buffer */
836 if (nrt) /* Send last packet */
841 * outbound filter logic, per-route/interface.
844 out_filter(struct riprt *rrt, struct ifc *ifcp)
851 * -A: filter out less specific routes, if we have aggregated
854 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
855 if (iffp->iff_type != 'A')
857 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
859 ia = rrt->rrt_info.rip6_dest;
860 applyplen(&ia, iffp->iff_plen);
861 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
866 * if it is an aggregated route, advertise it only to the
867 * interfaces specified on -A.
869 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
871 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
872 if (iffp->iff_type != 'A')
874 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
875 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
886 * -O: advertise only if prefix matches the configured prefix.
888 if (iff_find(ifcp, 'O')) {
890 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
891 if (iffp->iff_type != 'O')
893 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
895 ia = rrt->rrt_info.rip6_dest;
896 applyplen(&ia, iffp->iff_plen);
897 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
906 /* the prefix should be advertised */
911 * Determine if the route is to be advertised on the specified interface.
912 * It checks options specified in the arguments and the split horizon rule.
915 tobeadv(struct riprt *rrt, struct ifc *ifcp)
918 /* Special care for static routes */
919 if (rrt->rrt_flags & RTF_STATIC) {
920 /* XXX don't advertise reject/blackhole routes */
921 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
924 if (Sflag) /* Yes, advertise it anyway */
926 if (sflag && rrt->rrt_index != ifcp->ifc_index)
930 /* Regular split horizon */
931 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
937 * Send a rip packet actually.
940 sendpacket(struct sockaddr_in6 *sin, int len)
943 * MSG_DONTROUTE should not be specified when it responds with a
944 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
951 struct in6_pktinfo *pi;
953 struct sockaddr_in6 sincopy;
955 /* do not overwrite the given sin */
959 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)
960 || IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) {
961 idx = IN6_LINKLOCAL_IFINDEX(sin->sin6_addr);
962 SET_IN6_LINKLOCAL_IFINDEX(sin->sin6_addr, 0);
966 m.msg_name = (caddr_t)sin;
967 m.msg_namelen = sizeof(*sin);
968 iov[0].iov_base = (caddr_t)ripbuf;
969 iov[0].iov_len = len;
973 m.msg_control = NULL;
974 m.msg_controllen = 0;
976 memset(cmsgbuf, 0, sizeof(cmsgbuf));
977 cm = (struct cmsghdr *)cmsgbuf;
978 m.msg_control = (caddr_t)cm;
979 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
981 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
982 cm->cmsg_level = IPPROTO_IPV6;
983 cm->cmsg_type = IPV6_PKTINFO;
984 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
985 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
986 pi->ipi6_ifindex = idx;
989 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
990 trace(1, "sendmsg: %s\n", strerror(errno));
998 * Receive and process RIP packets. Update the routes/kernel forwarding
999 * table if necessary.
1004 struct ifc *ifcp, *ic;
1005 struct sockaddr_in6 fsock;
1006 struct in6_addr nh; /* next hop */
1008 struct netinfo6 *np, *nq;
1010 int len, nn, need_trigger, idx;
1011 char buf[4 * RIP6_MAXMTU];
1015 struct iovec iov[2];
1016 u_char cmsgbuf[256];
1017 struct in6_pktinfo *pi;
1021 time_t t_half_lifetime;
1025 m.msg_name = (caddr_t)&fsock;
1026 m.msg_namelen = sizeof(fsock);
1027 iov[0].iov_base = (caddr_t)buf;
1028 iov[0].iov_len = sizeof(buf);
1031 cm = (struct cmsghdr *)cmsgbuf;
1032 m.msg_control = (caddr_t)cm;
1033 m.msg_controllen = sizeof(cmsgbuf);
1034 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1039 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1041 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1042 if (cm->cmsg_level == IPPROTO_IPV6 &&
1043 cm->cmsg_type == IPV6_PKTINFO) {
1044 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1045 idx = pi->ipi6_ifindex;
1049 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1050 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1052 nh = fsock.sin6_addr;
1053 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1054 sizeof(struct netinfo6);
1055 rp = (struct rip6 *)buf;
1058 if (rp->rip6_vers != RIP6_VERSION) {
1059 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1062 if (rp->rip6_cmd == RIP6_REQUEST) {
1063 if (idx && idx < nindex2ifc) {
1064 ifcp = index2ifc[idx];
1065 riprequest(ifcp, np, nn, &fsock);
1067 riprequest(NULL, np, nn, &fsock);
1072 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1073 trace(1, "Packets from non-ll addr: %s\n",
1074 inet6_n2p(&fsock.sin6_addr));
1075 return; /* Ignore packets from non-link-local addr */
1077 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1078 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1080 trace(1, "Packets to unknown interface index %d\n", idx);
1081 return; /* Ignore it */
1083 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1084 return; /* The packet is from me; ignore */
1085 if (rp->rip6_cmd != RIP6_RESPONSE) {
1086 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1091 if (iff_find(ifcp, 'N') != NULL)
1094 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1095 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1098 t_half_lifetime = t - (RIP_LIFETIME/2);
1099 for (; nn; nn--, np++) {
1100 if (np->rip6_metric == NEXTHOP_METRIC) {
1101 /* modify neighbor address */
1102 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1104 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1105 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1106 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1107 nh = fsock.sin6_addr;
1108 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1110 nh = fsock.sin6_addr;
1111 trace(1, "\tInvalid Nexthop: %s\n",
1112 inet6_n2p(&np->rip6_dest));
1116 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1117 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1118 inet6_n2p(&np->rip6_dest),
1119 np->rip6_plen, np->rip6_metric);
1122 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1123 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1124 inet6_n2p(&np->rip6_dest),
1125 np->rip6_plen, np->rip6_metric);
1128 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1129 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1130 inet6_n2p(&np->rip6_dest),
1131 np->rip6_plen, np->rip6_metric);
1134 /* may need to pass sitelocal prefix in some case, however*/
1135 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1136 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1137 inet6_n2p(&np->rip6_dest),
1138 np->rip6_plen, np->rip6_metric);
1141 trace(2, "\tnetinfo6: %s/%d [%d]",
1142 inet6_n2p(&np->rip6_dest),
1143 np->rip6_plen, np->rip6_metric);
1145 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1148 applyplen(&ia, np->rip6_plen);
1149 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1150 trace(2, " [junk outside prefix]");
1154 * -L: listen only if the prefix matches the configuration
1156 ok = 1; /* if there's no L filter, it is ok */
1157 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
1158 if (iffp->iff_type != 'L')
1161 if (np->rip6_plen < iffp->iff_plen)
1163 /* special rule: ::/0 means default, not "in /0" */
1164 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1167 applyplen(&ia, iffp->iff_plen);
1168 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1174 trace(2, " (filtered)\n");
1180 np->rip6_metric += ifcp->ifc_metric;
1181 if (np->rip6_metric > HOPCNT_INFINITY6)
1182 np->rip6_metric = HOPCNT_INFINITY6;
1184 applyplen(&np->rip6_dest, np->rip6_plen);
1185 if ((rrt = rtsearch(np, NULL)) != NULL) {
1186 if (rrt->rrt_t == 0)
1187 continue; /* Intf route has priority */
1188 nq = &rrt->rrt_info;
1189 if (nq->rip6_metric > np->rip6_metric) {
1190 if (rrt->rrt_index == ifcp->ifc_index &&
1191 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1192 /* Small metric from the same gateway */
1193 nq->rip6_metric = np->rip6_metric;
1195 /* Better route found */
1196 rrt->rrt_index = ifcp->ifc_index;
1197 /* Update routing table */
1198 delroute(nq, &rrt->rrt_gw);
1201 addroute(rrt, &nh, ifcp);
1203 rrt->rrt_rflags |= RRTF_CHANGED;
1206 } else if (nq->rip6_metric < np->rip6_metric &&
1207 rrt->rrt_index == ifcp->ifc_index &&
1208 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1209 /* Got worse route from same gw */
1210 nq->rip6_metric = np->rip6_metric;
1212 rrt->rrt_rflags |= RRTF_CHANGED;
1214 } else if (nq->rip6_metric == np->rip6_metric &&
1215 np->rip6_metric < HOPCNT_INFINITY6) {
1216 if (rrt->rrt_index == ifcp->ifc_index &&
1217 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1218 /* same metric, same route from same gw */
1220 } else if (rrt->rrt_t < t_half_lifetime) {
1221 /* Better route found */
1222 rrt->rrt_index = ifcp->ifc_index;
1223 /* Update routing table */
1224 delroute(nq, &rrt->rrt_gw);
1227 addroute(rrt, &nh, ifcp);
1228 rrt->rrt_rflags |= RRTF_CHANGED;
1233 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1234 * do not update age value. Do nothing.
1236 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1237 /* Got a new valid route */
1238 if ((rrt = MALLOC(struct riprt)) == NULL) {
1239 fatal("malloc: struct riprt");
1242 memset(rrt, 0, sizeof(*rrt));
1243 nq = &rrt->rrt_info;
1245 rrt->rrt_same = NULL;
1246 rrt->rrt_index = ifcp->ifc_index;
1247 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1250 applyplen(&nq->rip6_dest, nq->rip6_plen);
1251 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1252 rrt->rrt_flags |= RTF_HOST;
1254 /* Put the route to the list */
1255 rrt->rrt_next = riprt;
1257 /* Update routing table */
1258 addroute(rrt, &nh, ifcp);
1259 rrt->rrt_rflags |= RRTF_CHANGED;
1264 /* XXX need to care the interval between triggered updates */
1266 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1267 for (ic = ifc; ic; ic = ic->ifc_next) {
1268 if (ifcp->ifc_index == ic->ifc_index)
1270 if (ic->ifc_flags & IFF_UP)
1271 ripsend(ic, &ic->ifc_ripsin,
1275 /* Reset the flag */
1276 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1277 rrt->rrt_rflags &= ~RRTF_CHANGED;
1282 * Send all routes request packet to the specified interface.
1285 sendrequest(struct ifc *ifcp)
1287 struct netinfo6 *np;
1290 if (ifcp->ifc_flags & IFF_LOOPBACK)
1292 ripbuf->rip6_cmd = RIP6_REQUEST;
1293 np = ripbuf->rip6_nets;
1294 memset(np, 0, sizeof(struct netinfo6));
1295 np->rip6_metric = HOPCNT_INFINITY6;
1296 tracet(1, "Send rtdump Request to %s (%s)\n",
1297 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1298 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1299 if (error == EAFNOSUPPORT) {
1300 /* Protocol not supported */
1301 tracet(1, "Could not send rtdump Request to %s (%s): "
1302 "set IFF_UP to 0\n",
1303 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1304 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1306 ripbuf->rip6_cmd = RIP6_RESPONSE;
1310 * Process a RIP6_REQUEST packet.
1313 riprequest(struct ifc *ifcp, struct netinfo6 *np, int nn,
1314 struct sockaddr_in6 *sin)
1319 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1320 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1321 /* Specific response, don't split-horizon */
1322 trace(1, "\tRIP Request\n");
1323 for (i = 0; i < nn; i++, np++) {
1324 rrt = rtsearch(np, NULL);
1326 np->rip6_metric = rrt->rrt_info.rip6_metric;
1328 np->rip6_metric = HOPCNT_INFINITY6;
1330 sendpacket(sin, RIPSIZE(nn));
1333 /* Whole routing table dump */
1334 trace(1, "\tRIP Request -- whole routing table\n");
1335 ripsend(ifcp, sin, RRTF_SENDANYWAY);
1339 * Get information of each interface.
1344 struct ifaddrs *ifap, *ifa;
1346 struct ipv6_mreq mreq;
1349 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1354 if (getifaddrs(&ifap) != 0) {
1355 fatal("getifaddrs");
1359 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1360 if (ifa->ifa_addr->sa_family != AF_INET6)
1362 ifcp = ifc_find(ifa->ifa_name);
1363 /* we are interested in multicast-capable interfaces */
1364 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1368 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1369 fatal("malloc: struct ifc");
1372 memset(ifcp, 0, sizeof(*ifcp));
1373 ifcp->ifc_index = -1;
1374 ifcp->ifc_next = ifc;
1377 ifcp->ifc_name = allocopy(ifa->ifa_name);
1378 ifcp->ifc_addr = NULL;
1379 ifcp->ifc_filter = NULL;
1380 ifcp->ifc_flags = ifa->ifa_flags;
1381 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1382 ifflags(ifcp->ifc_flags));
1383 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1386 /* update flag, this may be up again */
1387 if (ifcp->ifc_flags != ifa->ifa_flags) {
1388 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1389 ifflags(ifcp->ifc_flags));
1390 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1391 ifcp->ifc_cflags |= IFC_CHANGED;
1393 ifcp->ifc_flags = ifa->ifa_flags;
1395 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
1396 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1397 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1398 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1399 mreq.ipv6mr_interface = ifcp->ifc_index;
1400 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1401 &mreq, sizeof(mreq)) < 0) {
1402 fatal("IPV6_JOIN_GROUP");
1405 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1414 ifconfig1(const char *name, const struct sockaddr *sa, struct ifc *ifcp, int s)
1416 struct in6_ifreq ifr;
1417 const struct sockaddr_in6 *sin;
1422 sin = (const struct sockaddr_in6 *)sa;
1423 ifr.ifr_addr = *sin;
1424 strcpy(ifr.ifr_name, name);
1425 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1426 fatal("ioctl: SIOCGIFNETMASK_IN6");
1429 plen = sin6mask2len(&ifr.ifr_addr);
1430 if ((ifa = ifa_match(ifcp, &sin->sin6_addr, plen)) != NULL) {
1431 /* same interface found */
1432 /* need check if something changed */
1433 /* XXX not yet implemented */
1437 * New address is found
1439 if ((ifa = MALLOC(struct ifac)) == NULL) {
1440 fatal("malloc: struct ifac");
1443 memset(ifa, 0, sizeof(*ifa));
1444 ifa->ifa_conf = ifcp;
1445 ifa->ifa_next = ifcp->ifc_addr;
1446 ifcp->ifc_addr = ifa;
1447 ifa->ifa_addr = sin->sin6_addr;
1448 ifa->ifa_plen = plen;
1449 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1450 ifr.ifr_addr = *sin;
1451 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1452 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1455 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
1456 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
1457 trace(1, "found address %s/%d -- %s\n",
1458 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
1460 trace(1, "found address %s/%d\n",
1461 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
1463 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1464 ifcp->ifc_mylladdr = ifa->ifa_addr;
1465 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
1466 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1467 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1469 setindex2ifc(ifcp->ifc_index, ifcp);
1470 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1471 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1472 ifcp->ifc_mtu = RIP6_MAXMTU;
1473 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1474 fatal("ioctl: SIOCGIFMETRIC");
1477 ifcp->ifc_metric = ifr.ifr_metric;
1478 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1479 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1481 ifcp->ifc_cflags |= IFC_CHANGED;
1485 * Receive and process routing messages.
1486 * Update interface information as necesssary.
1493 struct rt_msghdr *rtm;
1494 struct ifa_msghdr *ifam;
1495 struct if_msghdr *ifm;
1497 struct ifc *ifcp, *ic;
1498 int iface = 0, rtable = 0;
1499 struct sockaddr_in6 *rta[RTAX_MAX];
1500 struct sockaddr_in6 mask;
1504 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1505 perror("read from rtsock");
1508 if (len < sizeof(*rtm)) {
1509 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1510 len, (u_long)sizeof(*rtm));
1514 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1515 /* safety against bogus message */
1516 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1517 trace(1, "bogus rtmsg: length=%d\n",
1518 ((struct rt_msghdr *)p)->rtm_msglen);
1524 switch (((struct rt_msghdr *)p)->rtm_type) {
1527 ifam = (struct ifa_msghdr *)p;
1528 addrs = ifam->ifam_addrs;
1529 q = (char *)(ifam + 1);
1532 ifm = (struct if_msghdr *)p;
1533 addrs = ifm->ifm_addrs;
1534 q = (char *)(ifm + 1);
1537 rtm = (struct rt_msghdr *)p;
1538 addrs = rtm->rtm_addrs;
1539 q = (char *)(rtm + 1);
1540 if (rtm->rtm_version != RTM_VERSION) {
1541 trace(1, "unexpected rtmsg version %d "
1543 rtm->rtm_version, RTM_VERSION);
1546 if (rtm->rtm_pid == pid) {
1548 trace(1, "rtmsg looped back to me, ignored\n");
1554 memset(&rta, 0, sizeof(rta));
1555 for (i = 0; i < RTAX_MAX; i++) {
1556 if (addrs & (1 << i)) {
1557 rta[i] = (struct sockaddr_in6 *)q;
1558 q += ROUNDUP(rta[i]->sin6_len);
1562 trace(1, "rtsock: %s (addrs=%x)\n",
1563 rttypes((struct rt_msghdr *)p), addrs);
1566 i < ((struct rt_msghdr *)p)->rtm_msglen;
1568 fprintf(stderr, "%02x ", p[i] & 0xff);
1569 if (i % 16 == 15) fprintf(stderr, "\n");
1571 fprintf(stderr, "\n");
1577 * We may be able to optimize by using ifm->ifm_index or
1578 * ifam->ifam_index. For simplicity we don't do that here.
1580 switch (((struct rt_msghdr *)p)->rtm_type) {
1593 /* nothing to be done here */
1594 trace(1, "\tnothing to be done, ignored\n");
1599 if (rta[RTAX_DST] == NULL) {
1600 trace(1, "\tno destination, ignored\n");
1603 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1604 trace(1, "\taf mismatch, ignored\n");
1607 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1608 trace(1, "\tlinklocal destination, ignored\n");
1611 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1612 trace(1, "\tloopback destination, ignored\n");
1613 continue; /* Loopback */
1615 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1616 trace(1, "\tmulticast destination, ignored\n");
1622 switch (((struct rt_msghdr *)p)->rtm_type) {
1631 /* should already be handled */
1632 fatal("rtrecv: never reach here");
1635 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1636 trace(1, "\tsome of dst/gw/netamsk are "
1637 "unavailable, ignored\n");
1640 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1641 mask.sin6_len = sizeof(mask);
1642 memset(&mask.sin6_addr, 0xff,
1643 sizeof(mask.sin6_addr));
1644 rta[RTAX_NETMASK] = &mask;
1645 } else if (!rta[RTAX_NETMASK]) {
1646 trace(1, "\tsome of dst/gw/netamsk are "
1647 "unavailable, ignored\n");
1650 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1651 rta[RTAX_NETMASK]) == 0) {
1652 rtable++; /*just to be sure*/
1657 trace(1, "\tnot supported yet, ignored\n");
1660 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1661 trace(1, "\tno netmask or ifa given, ignored\n");
1664 if (ifam->ifam_index < nindex2ifc)
1665 ifcp = index2ifc[ifam->ifam_index];
1669 trace(1, "\tinvalid ifam_index %d, ignored\n",
1673 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1678 trace(1, "\tnot supported yet, ignored\n");
1685 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1687 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
1688 if (ifcp->ifc_cflags & IFC_CHANGED) {
1689 if (ifrt(ifcp, 1)) {
1690 for (ic = ifc; ic; ic = ic->ifc_next) {
1691 if (ifcp->ifc_index == ic->ifc_index)
1693 if (ic->ifc_flags & IFF_UP)
1694 ripsend(ic, &ic->ifc_ripsin,
1697 /* Reset the flag */
1698 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1699 rrt->rrt_rflags &= ~RRTF_CHANGED;
1701 ifcp->ifc_cflags &= ~IFC_CHANGED;
1705 trace(1, "rtsock: read routing table again\n");
1711 * remove specified route from the internal routing table.
1714 rt_del(const struct sockaddr_in6 *sdst, const struct sockaddr_in6 *sgw,
1715 const struct sockaddr_in6 *smask)
1717 const struct in6_addr *dst = NULL;
1718 const struct in6_addr *gw = NULL;
1720 struct netinfo6 ni6;
1721 struct riprt *rrt = NULL;
1724 if (sdst->sin6_family != AF_INET6) {
1725 trace(1, "\tother AF, ignored\n");
1728 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1729 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1730 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1731 trace(1, "\taddress %s not interesting, ignored\n",
1732 inet6_n2p(&sdst->sin6_addr));
1735 dst = &sdst->sin6_addr;
1736 if (sgw->sin6_family == AF_INET6) {
1738 gw = &sgw->sin6_addr;
1739 prefix = sin6mask2len(smask);
1740 } else if (sgw->sin6_family == AF_LINK) {
1742 * Interface route... a hard case. We need to get the prefix
1743 * length from the kernel, but we now are parsing rtmsg.
1744 * We'll purge matching routes from my list, then get the
1747 struct riprt *longest;
1748 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1751 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
1752 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1754 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1756 || longest->rrt_info.rip6_plen <
1757 rrt->rrt_info.rip6_plen) {
1764 trace(1, "\tno matching interface route found\n");
1767 gw = &in6addr_loopback;
1768 prefix = rrt->rrt_info.rip6_plen;
1770 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1774 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1775 trace(1, "gw %s\n", inet6_n2p(gw));
1776 t_lifetime = time(NULL) - RIP_LIFETIME;
1777 /* age route for interface address */
1778 memset(&ni6, 0, sizeof(ni6));
1779 ni6.rip6_dest = *dst;
1780 ni6.rip6_plen = prefix;
1781 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1782 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
1784 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
1785 trace(1, "\tno route found\n");
1789 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
1790 trace(1, "\tyou can delete static routes only\n");
1793 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
1794 trace(1, "\tgw mismatch: %s <-> ",
1795 inet6_n2p(&rrt->rrt_gw));
1796 trace(1, "%s\n", inet6_n2p(gw));
1798 trace(1, "\troute found, age it\n");
1799 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1800 rrt->rrt_t = t_lifetime;
1801 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1808 * remove specified address from internal interface/routing table.
1811 rt_deladdr(struct ifc *ifcp, const struct sockaddr_in6 *sifa,
1812 const struct sockaddr_in6 *smask)
1814 const struct in6_addr *addr = NULL;
1816 struct ifac *ifa = NULL;
1817 struct netinfo6 ni6;
1818 struct riprt *rrt = NULL;
1822 if (sifa->sin6_family != AF_INET6) {
1823 trace(1, "\tother AF, ignored\n");
1826 addr = &sifa->sin6_addr;
1827 prefix = sin6mask2len(smask);
1829 trace(1, "\tdeleting %s/%d from %s\n",
1830 inet6_n2p(addr), prefix, ifcp->ifc_name);
1831 ifa = ifa_match(ifcp, addr, prefix);
1833 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1834 inet6_n2p(addr), prefix, ifcp->ifc_name);
1837 if (ifa->ifa_conf != ifcp) {
1838 trace(1, "\taddress table corrupt: back pointer does not match "
1840 ifcp->ifc_name, ifa->ifa_conf->ifc_name);
1843 /* remove ifa from interface */
1844 if (ifcp->ifc_addr == ifa)
1845 ifcp->ifc_addr = ifa->ifa_next;
1848 for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
1849 if (p->ifa_next == ifa) {
1850 p->ifa_next = ifa->ifa_next;
1855 ifa->ifa_next = NULL;
1856 ifa->ifa_conf = NULL;
1857 t_lifetime = time(NULL) - RIP_LIFETIME;
1858 /* age route for interface address */
1859 memset(&ni6, 0, sizeof(ni6));
1860 ni6.rip6_dest = ifa->ifa_addr;
1861 ni6.rip6_plen = ifa->ifa_plen;
1862 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
1863 trace(1, "\tfind interface route %s/%d on %d\n",
1864 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
1865 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1866 struct in6_addr none;
1867 memset(&none, 0, sizeof(none));
1868 if (rrt->rrt_index == ifcp->ifc_index &&
1869 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
1870 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
1871 trace(1, "\troute found, age it\n");
1872 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1873 rrt->rrt_t = t_lifetime;
1874 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1878 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1879 inet6_n2p(&rrt->rrt_info.rip6_dest),
1880 rrt->rrt_info.rip6_plen,
1884 trace(1, "\tno interface route found\n");
1885 /* age route for p2p destination */
1886 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1887 memset(&ni6, 0, sizeof(ni6));
1888 ni6.rip6_dest = ifa->ifa_raddr;
1889 ni6.rip6_plen = 128;
1890 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1891 trace(1, "\tfind p2p route %s/%d on %d\n",
1892 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
1894 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1895 if (rrt->rrt_index == ifcp->ifc_index &&
1896 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
1897 trace(1, "\troute found, age it\n");
1898 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1899 rrt->rrt_t = t_lifetime;
1900 rrt->rrt_info.rip6_metric =
1905 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1906 inet6_n2p(&rrt->rrt_info.rip6_dest),
1907 rrt->rrt_info.rip6_plen,
1911 trace(1, "\tno p2p route found\n");
1913 return updated ? 0 : -1;
1917 * Get each interface address and put those interface routes to the route
1921 ifrt(struct ifc *ifcp, int again)
1924 struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
1925 struct netinfo6 *np;
1927 int need_trigger = 0;
1929 if (ifcp->ifc_flags & IFF_LOOPBACK)
1930 return 0; /* ignore loopback */
1931 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1932 ifrt_p2p(ifcp, again);
1936 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
1937 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1939 trace(1, "route: %s on %s: "
1940 "skip linklocal interface address\n",
1941 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
1945 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
1947 trace(1, "route: %s: skip unspec interface address\n",
1952 if (ifcp->ifc_flags & IFF_UP) {
1953 if ((rrt = MALLOC(struct riprt)) == NULL)
1954 fatal("malloc: struct riprt");
1955 memset(rrt, 0, sizeof(*rrt));
1956 rrt->rrt_same = NULL;
1957 rrt->rrt_index = ifcp->ifc_index;
1958 rrt->rrt_t = 0; /* don't age */
1959 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
1960 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
1961 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
1962 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
1963 rrt->rrt_flags = RTF_CLONING;
1964 rrt->rrt_rflags |= RRTF_CHANGED;
1965 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
1966 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
1968 /* XXX why gateway address == network adddress? */
1969 rrt->rrt_gw = ifa->ifa_addr;
1971 np = &rrt->rrt_info;
1972 search_rrt = rtsearch(np, &prev_rrt);
1973 if (search_rrt != NULL) {
1974 if (search_rrt->rrt_info.rip6_metric >
1975 rrt->rrt_info.rip6_metric) {
1977 prev_rrt->rrt_next = rrt->rrt_next;
1979 riprt = rrt->rrt_next;
1980 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1983 /* Already have better route */
1985 trace(1, "route: %s/%d: "
1986 "already registered (%s)\n",
1987 inet6_n2p(&np->rip6_dest), np->rip6_plen,
1994 /* Attach the route to the list */
1995 trace(1, "route: %s/%d: register route (%s)\n",
1996 inet6_n2p(&np->rip6_dest), np->rip6_plen,
1998 rrt->rrt_next = riprt;
2000 addroute(rrt, &rrt->rrt_gw, ifcp);
2002 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2005 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
2006 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2007 t_lifetime = time(NULL) - RIP_LIFETIME;
2008 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2009 loop_rrt->rrt_t = t_lifetime;
2010 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2011 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2018 return need_trigger;
2022 * there are couple of p2p interface routing models. "behavior" lets
2023 * you pick one. it looks that gated behavior fits best with BSDs,
2024 * since BSD kernels does not look at prefix length on p2p interfaces.
2027 ifrt_p2p(struct ifc *ifcp, int again)
2030 struct riprt *rrt, *orrt, *prevrrt;
2031 struct netinfo6 *np;
2032 struct in6_addr addr, dest;
2033 int advert, ignore, i;
2034 #define P2PADVERT_NETWORK 1
2035 #define P2PADVERT_ADDR 2
2036 #define P2PADVERT_DEST 4
2037 #define P2PADVERT_MAX 4
2038 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2039 const char *category = "";
2042 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2043 addr = ifa->ifa_addr;
2044 dest = ifa->ifa_raddr;
2045 applyplen(&addr, ifa->ifa_plen);
2046 applyplen(&dest, ifa->ifa_plen);
2047 advert = ignore = 0;
2051 * honor addr/plen, just like normal shared medium
2052 * interface. this may cause trouble if you reuse
2053 * addr/plen on other interfaces.
2055 * advertise addr/plen.
2057 advert |= P2PADVERT_NETWORK;
2061 * prefixlen on p2p interface is meaningless.
2062 * advertise addr/128 and dest/128.
2064 * do not install network route to route6d routing
2065 * table (if we do, it would prevent route installation
2066 * for other p2p interface that shares addr/plen).
2068 * XXX what should we do if dest is ::? it will not
2069 * get announced anyways (see following filter),
2070 * but we need to think.
2072 advert |= P2PADVERT_ADDR;
2073 advert |= P2PADVERT_DEST;
2074 ignore |= P2PADVERT_NETWORK;
2078 * just for testing. actually the code is redundant
2079 * given the current p2p interface address assignment
2080 * rule for kame kernel.
2083 * A/n -> announce A/n
2084 * A B/n, A and B share prefix -> A/n (= B/n)
2085 * A B/n, do not share prefix -> A/128 and B/128
2086 * actually, A/64 and A B/128 are the only cases
2087 * permitted by the kernel:
2089 * A B/128 -> A/128 and B/128
2091 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
2092 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2093 advert |= P2PADVERT_NETWORK;
2095 advert |= P2PADVERT_ADDR;
2096 advert |= P2PADVERT_DEST;
2097 ignore |= P2PADVERT_NETWORK;
2100 advert |= P2PADVERT_NETWORK;
2104 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2105 if ((ignore & i) != 0)
2107 if ((rrt = MALLOC(struct riprt)) == NULL) {
2108 fatal("malloc: struct riprt");
2111 memset(rrt, 0, sizeof(*rrt));
2112 rrt->rrt_same = NULL;
2113 rrt->rrt_index = ifcp->ifc_index;
2114 rrt->rrt_t = 0; /* don't age */
2116 case P2PADVERT_NETWORK:
2117 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2118 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2119 applyplen(&rrt->rrt_info.rip6_dest,
2121 category = "network";
2123 case P2PADVERT_ADDR:
2124 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2125 rrt->rrt_info.rip6_plen = 128;
2126 rrt->rrt_gw = in6addr_loopback;
2129 case P2PADVERT_DEST:
2130 rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
2131 rrt->rrt_info.rip6_plen = 128;
2132 rrt->rrt_gw = ifa->ifa_addr;
2136 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2137 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2139 trace(1, "route: %s: skip unspec/linklocal "
2140 "(%s on %s)\n", category, ifcp->ifc_name);
2145 if ((advert & i) == 0) {
2146 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2150 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2151 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2152 np = &rrt->rrt_info;
2153 orrt = rtsearch(np, &prevrrt);
2155 /* Attach the route to the list */
2156 trace(1, "route: %s/%d: register route "
2158 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2159 category, ifcp->ifc_name, noadv);
2160 rrt->rrt_next = riprt;
2162 } else if (rrt->rrt_index != orrt->rrt_index ||
2163 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2165 rrt->rrt_next = orrt->rrt_next;
2167 prevrrt->rrt_next = rrt;
2172 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2173 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2174 category, ifcp->ifc_name, noadv);
2178 trace(1, "route: %s/%d: "
2179 "already registered (%s on %s%s)\n",
2180 inet6_n2p(&np->rip6_dest),
2181 np->rip6_plen, category,
2182 ifcp->ifc_name, noadv);
2188 #undef P2PADVERT_NETWORK
2189 #undef P2PADVERT_ADDR
2190 #undef P2PADVERT_DEST
2191 #undef P2PADVERT_MAX
2195 getifmtu(int ifindex)
2200 struct if_msghdr *ifm;
2207 mib[4] = NET_RT_IFLIST;
2209 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2210 fatal("sysctl estimate NET_RT_IFLIST");
2213 if ((buf = malloc(msize)) == NULL) {
2217 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2218 fatal("sysctl NET_RT_IFLIST");
2221 ifm = (struct if_msghdr *)buf;
2222 mtu = ifm->ifm_data.ifi_mtu;
2223 #ifdef __DragonFly__
2224 if (ifindex != ifm->ifm_index) {
2225 fatal("ifindex does not match with ifm_index");
2234 rttypes(struct rt_msghdr *rtm)
2236 #define RTTYPE(s, f) \
2238 if (rtm->rtm_type == (f)) \
2241 RTTYPE("ADD", RTM_ADD);
2242 RTTYPE("DELETE", RTM_DELETE);
2243 RTTYPE("CHANGE", RTM_CHANGE);
2244 RTTYPE("GET", RTM_GET);
2245 RTTYPE("LOSING", RTM_LOSING);
2246 RTTYPE("REDIRECT", RTM_REDIRECT);
2247 RTTYPE("MISS", RTM_MISS);
2248 RTTYPE("LOCK", RTM_LOCK);
2249 RTTYPE("OLDADD", RTM_OLDADD);
2250 RTTYPE("OLDDEL", RTM_OLDDEL);
2251 RTTYPE("RESOLVE", RTM_RESOLVE);
2252 RTTYPE("NEWADDR", RTM_NEWADDR);
2253 RTTYPE("DELADDR", RTM_DELADDR);
2254 RTTYPE("IFINFO", RTM_IFINFO);
2256 RTTYPE("OLDADD", RTM_OLDADD);
2259 RTTYPE("OLDDEL", RTM_OLDDEL);
2262 RTTYPE("OIFINFO", RTM_OIFINFO);
2264 #ifdef RTM_IFANNOUNCE
2265 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2268 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2271 RTTYPE("DELMADDR", RTM_DELMADDR);
2278 rtflags(struct rt_msghdr *rtm)
2280 static char buf[BUFSIZ];
2283 * letter conflict should be okay. painful when *BSD diverges...
2285 strlcpy(buf, "", sizeof(buf));
2286 #define RTFLAG(s, f) \
2288 if (rtm->rtm_flags & (f)) \
2289 strlcat(buf, (s), sizeof(buf)); \
2291 RTFLAG("U", RTF_UP);
2292 RTFLAG("G", RTF_GATEWAY);
2293 RTFLAG("H", RTF_HOST);
2294 RTFLAG("R", RTF_REJECT);
2295 RTFLAG("D", RTF_DYNAMIC);
2296 RTFLAG("M", RTF_MODIFIED);
2297 RTFLAG("d", RTF_DONE);
2299 RTFLAG("m", RTF_MASK);
2301 RTFLAG("C", RTF_CLONING);
2303 RTFLAG("c", RTF_CLONED);
2305 #ifdef RTF_PRCLONING
2306 RTFLAG("c", RTF_PRCLONING);
2308 #ifdef RTF_WASCLONED
2309 RTFLAG("W", RTF_WASCLONED);
2311 RTFLAG("X", RTF_XRESOLVE);
2312 RTFLAG("L", RTF_LLINFO);
2313 RTFLAG("S", RTF_STATIC);
2314 RTFLAG("B", RTF_BLACKHOLE);
2316 RTFLAG("3", RTF_PROTO3);
2318 RTFLAG("2", RTF_PROTO2);
2319 RTFLAG("1", RTF_PROTO1);
2320 #ifdef RTF_BROADCAST
2321 RTFLAG("b", RTF_BROADCAST);
2324 RTFLAG("d", RTF_DEFAULT);
2326 #ifdef RTF_ISAROUTER
2327 RTFLAG("r", RTF_ISAROUTER);
2330 RTFLAG("T", RTF_TUNNEL);
2333 RTFLAG("A", RTF_AUTH);
2336 RTFLAG("E", RTF_CRYPT);
2345 static char buf[BUFSIZ];
2347 strlcpy(buf, "", sizeof(buf));
2348 #define IFFLAG(s, f) \
2352 strlcat(buf, ",", sizeof(buf)); \
2353 strlcat(buf, s, sizeof(buf)); \
2356 IFFLAG("UP", IFF_UP);
2357 IFFLAG("BROADCAST", IFF_BROADCAST);
2358 IFFLAG("DEBUG", IFF_DEBUG);
2359 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2360 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2361 #ifdef IFF_NOTRAILERS
2362 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2365 IFFLAG("SMART", IFF_SMART);
2367 IFFLAG("RUNNING", IFF_RUNNING);
2368 IFFLAG("NOARP", IFF_NOARP);
2369 IFFLAG("PROMISC", IFF_PROMISC);
2370 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2371 IFFLAG("OACTIVE", IFF_OACTIVE);
2372 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2373 IFFLAG("LINK0", IFF_LINK0);
2374 IFFLAG("LINK1", IFF_LINK1);
2375 IFFLAG("LINK2", IFF_LINK2);
2376 IFFLAG("MULTICAST", IFF_MULTICAST);
2386 char *buf, *p, *lim;
2387 struct rt_msghdr *rtm;
2396 mib[3] = AF_INET6; /* Address family */
2397 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2398 mib[5] = 0; /* No flags */
2404 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2405 errmsg = "sysctl estimate";
2408 if ((buf = malloc(msize)) == NULL) {
2412 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2413 errmsg = "sysctl NET_RT_DUMP";
2416 } while (retry < 5 && errmsg != NULL);
2418 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2421 } else if (1 < retry)
2422 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2425 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2426 rtm = (struct rt_msghdr *)p;
2427 rt_entry(rtm, again);
2433 rt_entry(struct rt_msghdr *rtm, int again)
2435 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2436 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2437 char *rtmp, *ifname = NULL;
2438 struct riprt *rrt, *orrt;
2439 struct netinfo6 *np;
2442 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = NULL;
2443 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2444 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
2445 return; /* not interested in the link route */
2447 /* do not look at cloned routes */
2448 #ifdef RTF_WASCLONED
2449 if (rtm->rtm_flags & RTF_WASCLONED)
2453 if (rtm->rtm_flags & RTF_CLONED)
2457 * do not look at dynamic routes.
2458 * netbsd/openbsd cloned routes have UGHD.
2460 if (rtm->rtm_flags & RTF_DYNAMIC)
2462 rtmp = (char *)(rtm + 1);
2464 if ((rtm->rtm_addrs & RTA_DST) == 0)
2465 return; /* ignore routes without destination address */
2466 sin6_dst = (struct sockaddr_in6 *)rtmp;
2467 rtmp += ROUNDUP(sin6_dst->sin6_len);
2468 if (rtm->rtm_addrs & RTA_GATEWAY) {
2469 sin6_gw = (struct sockaddr_in6 *)rtmp;
2470 rtmp += ROUNDUP(sin6_gw->sin6_len);
2472 if (rtm->rtm_addrs & RTA_NETMASK) {
2473 sin6_mask = (struct sockaddr_in6 *)rtmp;
2474 rtmp += ROUNDUP(sin6_mask->sin6_len);
2476 if (rtm->rtm_addrs & RTA_GENMASK) {
2477 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2478 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2480 if (rtm->rtm_addrs & RTA_IFP) {
2481 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2482 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2486 if (sin6_dst->sin6_family != AF_INET6)
2488 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2489 return; /* Link-local */
2490 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2491 return; /* Loopback */
2492 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2495 if ((rrt = MALLOC(struct riprt)) == NULL) {
2496 fatal("malloc: struct riprt");
2499 memset(rrt, 0, sizeof(*rrt));
2500 np = &rrt->rrt_info;
2501 rrt->rrt_same = NULL;
2502 rrt->rrt_t = time(NULL);
2503 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2504 rrt->rrt_t = 0; /* Don't age static routes */
2506 np->rip6_tag = htons(routetag & 0xffff);
2510 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2511 if (np->rip6_metric < 1)
2512 np->rip6_metric = 1;
2513 rrt->rrt_flags = rtm->rtm_flags;
2514 np->rip6_dest = sin6_dst->sin6_addr;
2517 if (rtm->rtm_flags & RTF_HOST)
2518 np->rip6_plen = 128; /* Host route */
2520 np->rip6_plen = sin6mask2len(sin6_mask);
2524 orrt = rtsearch(np, NULL);
2525 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2528 trace(1, "route: %s/%d flags %s: already registered\n",
2529 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2537 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2539 if (sin6_gw->sin6_family == AF_INET6)
2540 rrt->rrt_gw = sin6_gw->sin6_addr;
2541 else if (sin6_gw->sin6_family == AF_LINK) {
2542 /* XXX in case ppp link? */
2543 rrt->rrt_gw = in6addr_loopback;
2545 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2547 trace(1, "route: %s/%d flags %s",
2548 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2549 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2553 if (s < nindex2ifc && index2ifc[s])
2554 ifname = index2ifc[s]->ifc_name;
2556 trace(1, " not configured\n");
2560 trace(1, " if %s sock %d", ifname, s);
2566 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2567 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
2568 #ifdef __DragonFly__
2569 && (rrt->rrt_flags & RTF_LOCAL) == 0
2572 trace(0, "***** Gateway %s is not a link-local address.\n",
2573 inet6_n2p(&rrt->rrt_gw));
2574 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2575 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2576 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2579 /* Put it to the route list */
2580 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2581 /* replace route list */
2582 rrt->rrt_next = orrt->rrt_next;
2584 trace(1, "route: %s/%d flags %s: replace new route\n",
2585 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2589 rrt->rrt_next = riprt;
2595 addroute(struct riprt *rrt, const struct in6_addr *gw, struct ifc *ifcp)
2597 struct netinfo6 *np;
2598 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2599 struct rt_msghdr *rtm;
2600 struct sockaddr_in6 *sin;
2603 np = &rrt->rrt_info;
2604 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2605 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2606 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2607 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2608 np->rip6_metric - 1, buf2);
2610 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2611 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2612 np->rip6_metric - 1, buf2);
2616 memset(buf, 0, sizeof(buf));
2617 rtm = (struct rt_msghdr *)buf;
2618 rtm->rtm_type = RTM_ADD;
2619 rtm->rtm_version = RTM_VERSION;
2620 rtm->rtm_seq = ++seq;
2622 rtm->rtm_flags = rrt->rrt_flags;
2623 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2624 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2625 rtm->rtm_inits = RTV_HOPCOUNT;
2626 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2628 sin->sin6_len = sizeof(struct sockaddr_in6);
2629 sin->sin6_family = AF_INET6;
2630 sin->sin6_addr = np->rip6_dest;
2631 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2633 sin->sin6_len = sizeof(struct sockaddr_in6);
2634 sin->sin6_family = AF_INET6;
2635 sin->sin6_addr = *gw;
2636 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2638 sin->sin6_len = sizeof(struct sockaddr_in6);
2639 sin->sin6_family = AF_INET6;
2640 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2641 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2643 len = (char *)sin - (char *)buf;
2644 rtm->rtm_msglen = len;
2645 if (write(rtsock, buf, len) > 0)
2648 if (errno == EEXIST) {
2649 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2650 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2652 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2653 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2655 trace(0, "Can not write to rtsock (addroute): %s\n",
2658 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2665 delroute(struct netinfo6 *np, struct in6_addr *gw)
2667 u_char buf[BUFSIZ], buf2[BUFSIZ];
2668 struct rt_msghdr *rtm;
2669 struct sockaddr_in6 *sin;
2672 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2673 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2674 np->rip6_plen, buf2);
2676 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2677 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2681 memset(buf, 0, sizeof(buf));
2682 rtm = (struct rt_msghdr *)buf;
2683 rtm->rtm_type = RTM_DELETE;
2684 rtm->rtm_version = RTM_VERSION;
2685 rtm->rtm_seq = ++seq;
2687 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2688 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2689 rtm->rtm_flags |= RTF_HOST;
2690 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2691 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2693 sin->sin6_len = sizeof(struct sockaddr_in6);
2694 sin->sin6_family = AF_INET6;
2695 sin->sin6_addr = np->rip6_dest;
2696 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2698 sin->sin6_len = sizeof(struct sockaddr_in6);
2699 sin->sin6_family = AF_INET6;
2700 sin->sin6_addr = *gw;
2701 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2703 sin->sin6_len = sizeof(struct sockaddr_in6);
2704 sin->sin6_family = AF_INET6;
2705 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2706 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2708 len = (char *)sin - (char *)buf;
2709 rtm->rtm_msglen = len;
2710 if (write(rtsock, buf, len) >= 0)
2713 if (errno == ESRCH) {
2714 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2715 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2717 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2718 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2720 trace(0, "Can not write to rtsock (delroute): %s\n",
2723 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2730 getroute(struct netinfo6 *np, struct in6_addr *gw)
2735 struct rt_msghdr *rtm;
2736 struct sockaddr_in6 *sin;
2738 rtm = (struct rt_msghdr *)buf;
2739 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2740 memset(rtm, 0, len);
2741 rtm->rtm_type = RTM_GET;
2742 rtm->rtm_version = RTM_VERSION;
2744 rtm->rtm_seq = myseq;
2745 rtm->rtm_addrs = RTA_DST;
2746 rtm->rtm_msglen = len;
2747 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2748 sin->sin6_len = sizeof(struct sockaddr_in6);
2749 sin->sin6_family = AF_INET6;
2750 sin->sin6_addr = np->rip6_dest;
2751 if (write(rtsock, buf, len) < 0) {
2752 if (errno == ESRCH) /* No such route found */
2754 perror("write to rtsock");
2758 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2759 perror("read from rtsock");
2762 rtm = (struct rt_msghdr *)buf;
2763 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2764 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2765 if (rtm->rtm_addrs & RTA_DST) {
2766 sin = (struct sockaddr_in6 *)
2767 ((char *)sin + ROUNDUP(sin->sin6_len));
2769 if (rtm->rtm_addrs & RTA_GATEWAY) {
2770 *gw = sin->sin6_addr;
2777 inet6_n2p(const struct in6_addr *p)
2779 static char buf[BUFSIZ];
2781 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
2801 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2804 fprintf(dump, "%s: Interface Table Dump\n", hms());
2805 fprintf(dump, " Number of interfaces: %d\n", nifc);
2806 for (i = 0; i < 2; i++) {
2807 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
2808 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
2810 if ((ifcp->ifc_flags & IFF_UP) == 0)
2812 if (iff_find(ifcp, 'N') != NULL)
2815 if (ifcp->ifc_flags & IFF_UP)
2818 ifdump0(dump, ifcp);
2821 fprintf(dump, "\n");
2827 ifdump0(FILE *dump, const struct ifc *ifcp)
2835 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2836 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
2837 inet6_n2p(&ifcp->ifc_mylladdr),
2838 ifcp->ifc_mtu, ifcp->ifc_metric);
2839 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2840 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2841 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
2843 fprintf(dump, "\t%s/%d -- %s\n",
2844 inet6_n2p(&ifa->ifa_addr),
2845 ifa->ifa_plen, buf);
2847 fprintf(dump, "\t%s/%d\n",
2848 inet6_n2p(&ifa->ifa_addr),
2852 if (ifcp->ifc_filter) {
2853 fprintf(dump, "\tFilter:");
2854 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
2856 switch (iffp->iff_type) {
2858 ft = "Aggregate"; addr++; break;
2860 ft = "No-use"; break;
2862 ft = "Advertise-only"; addr++; break;
2864 ft = "Default-only"; break;
2866 ft = "Listen-only"; addr++; break;
2868 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
2873 fprintf(dump, " %s", ft);
2875 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
2879 fprintf(dump, "\n");
2894 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2898 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
2899 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
2900 if (rrt->rrt_t == 0)
2903 age = t - rrt->rrt_t;
2904 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
2906 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2907 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
2908 index2ifc[rrt->rrt_index]->ifc_name,
2909 inet6_n2p(&rrt->rrt_gw),
2910 rrt->rrt_info.rip6_metric, (long)age);
2911 if (rrt->rrt_info.rip6_tag) {
2912 fprintf(dump, " tag(0x%04x)",
2913 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
2915 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
2916 fprintf(dump, " NOT-LL");
2917 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
2918 fprintf(dump, " NO-ADV");
2919 fprintf(dump, "\n");
2921 fprintf(dump, "\n");
2927 * Parse the -A (and -O) options and put corresponding filter object to the
2928 * specified interface structures. Each of the -A/O option has the following
2929 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2930 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2936 char *p, *ap, *iflp, *ifname;
2937 struct iff ftmp, *iff_obj;
2944 for (i = 0; i < nfilter; i++) {
2948 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
2952 if ((p = strchr(ap, ',')) != NULL) {
2956 if ((p = strchr(ap, '/')) == NULL) {
2957 fatal("no prefixlen specified for '%s'", ap);
2961 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
2962 fatal("invalid prefix specified for '%s'", ap);
2965 ftmp.iff_plen = atoi(p);
2966 ftmp.iff_next = NULL;
2967 applyplen(&ftmp.iff_addr, ftmp.iff_plen);
2969 ftmp.iff_type = filtertype[i];
2970 if (iflp == NULL || *iflp == '\0') {
2971 fatal("no interface specified for '%s'", ap);
2974 /* parse the interface listing portion */
2977 if ((iflp = strchr(iflp, ',')) != NULL)
2979 ifcp = ifc_find(ifname);
2981 fatal("no interface %s exists", ifname);
2984 iff_obj = (struct iff *)malloc(sizeof(struct iff));
2985 if (iff_obj == NULL) {
2986 fatal("malloc of iff_obj");
2989 memcpy((void *)iff_obj, (void *)&ftmp,
2990 sizeof(struct iff));
2991 /* link it to the interface filter */
2992 iff_obj->iff_next = ifcp->ifc_filter;
2993 ifcp->ifc_filter = iff_obj;
2997 * -A: aggregate configuration.
2999 if (filtertype[i] != 'A')
3001 /* put the aggregate to the kernel routing table */
3002 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3004 fatal("malloc: rrt");
3007 memset(rrt, 0, sizeof(struct riprt));
3008 rrt->rrt_info.rip6_dest = ftmp.iff_addr;
3009 rrt->rrt_info.rip6_plen = ftmp.iff_plen;
3010 rrt->rrt_info.rip6_metric = 1;
3011 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3012 rrt->rrt_gw = in6addr_loopback;
3013 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3014 rrt->rrt_rflags = RRTF_AGGREGATE;
3016 rrt->rrt_index = loopifindex;
3018 if (getroute(&rrt->rrt_info, &gw)) {
3021 * When the address has already been registered in the
3022 * kernel routing table, it should be removed
3024 delroute(&rrt->rrt_info, &gw);
3026 /* it is safer behavior */
3028 fatal("%s/%u already in routing table, "
3030 inet6_n2p(&rrt->rrt_info.rip6_dest),
3031 rrt->rrt_info.rip6_plen);
3036 /* Put the route to the list */
3037 rrt->rrt_next = riprt;
3039 trace(1, "Aggregate: %s/%d for %s\n",
3040 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
3042 /* Add this route to the kernel */
3043 if (nflag) /* do not modify kernel routing table */
3045 addroute(rrt, &in6addr_loopback, loopifcp);
3049 /***************** utility functions *****************/
3052 * Returns a pointer to ifac whose address and prefix length matches
3053 * with the address and prefix length specified in the arguments.
3056 ifa_match(const struct ifc *ifcp, const struct in6_addr *ia, int plen)
3060 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
3061 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
3062 ifa->ifa_plen == plen)
3069 * Return a pointer to riprt structure whose address and prefix length
3070 * matches with the address and prefix length found in the argument.
3071 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3074 rtsearch(struct netinfo6 *np, struct riprt **prev_rrt)
3080 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
3081 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3082 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3094 sin6mask2len(const struct sockaddr_in6 *sin6)
3096 return mask2len(&sin6->sin6_addr,
3097 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3101 mask2len(const struct in6_addr *addr, int lenlim)
3104 const u_char *p = (const u_char *)addr;
3106 for (j = 0; j < lenlim; j++, p++) {
3113 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3114 MASKLEN(0xfe, 7); break;
3115 MASKLEN(0xfc, 6); break;
3116 MASKLEN(0xf8, 5); break;
3117 MASKLEN(0xf0, 4); break;
3118 MASKLEN(0xe0, 3); break;
3119 MASKLEN(0xc0, 2); break;
3120 MASKLEN(0x80, 1); break;
3128 applymask(struct in6_addr *addr, struct in6_addr *mask)
3133 p = (u_long *)addr; q = (u_long *)mask;
3134 for (i = 0; i < 4; i++)
3138 static const u_char plent[8] = {
3139 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3143 applyplen(struct in6_addr *ia, int plen)
3149 for (i = 0; i < 16; i++) {
3158 static const int pl2m[9] = {
3159 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3165 static struct in6_addr ia;
3169 memset(&ia, 0, sizeof(struct in6_addr));
3171 for (i = 0; i < 16; i++, p++, n -= 8) {
3185 char *q = (char *)malloc(strlen(p) + 1);
3194 static char buf[BUFSIZ];
3199 if ((tm = localtime(&t)) == NULL) {
3203 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3208 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3211 ripinterval(int timer)
3215 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3216 nextalarm = time(NULL) + interval;
3226 t = (int)(RIP_TRIG_INT6_MIN +
3227 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3228 sup_trig_update = time(NULL) + t;
3233 fatal(const char *fmt, ...)
3239 vsnprintf(buf, sizeof(buf), fmt, ap);
3241 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3247 tracet(int level, const char *fmt, ...)
3252 if (level <= dflag) {
3253 fprintf(stderr, "%s: ", hms());
3254 vfprintf(stderr, fmt, ap);
3258 vsyslog(LOG_DEBUG, fmt, ap);
3260 vsyslog(LOG_WARNING, fmt, ap);
3266 trace(int level, const char *fmt, ...)
3272 vfprintf(stderr, fmt, ap);
3275 vsyslog(LOG_DEBUG, fmt, ap);
3277 vsyslog(LOG_WARNING, fmt, ap);
3285 struct if_nameindex *p, *p0;
3286 unsigned int max = 0;
3288 p0 = if_nameindex();
3289 for (p = p0; p && p->if_index && p->if_name; p++) {
3290 if (max < p->if_index)
3293 if_freenameindex(p0);
3298 ifc_find(char *name)
3302 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
3303 if (strcmp(name, ifcp->ifc_name) == 0)
3310 iff_find(struct ifc *ifcp, int type)
3314 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
3315 if (iffp->iff_type == type)
3322 setindex2ifc(int idx, struct ifc *ifcp)
3328 nindex2ifc = 5; /*initial guess*/
3329 index2ifc = (struct ifc **)
3330 malloc(sizeof(*index2ifc) * nindex2ifc);
3331 if (index2ifc == NULL) {
3335 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3338 while (nindex2ifc <= idx)
3340 if (n != nindex2ifc) {
3341 p = (struct ifc **)realloc(index2ifc,
3342 sizeof(*index2ifc) * nindex2ifc);
3347 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3350 index2ifc[idx] = ifcp;