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
34 static char _rcsid[] = "$KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $";
54 #include <sys/types.h>
55 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <sys/sysctl.h>
62 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
63 #include <net/if_var.h>
64 #endif /* __FreeBSD__ >= 3 */
67 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet/udp.h>
77 #include <arpa/inet.h>
84 #define INIT_INTERVAL6 6
86 #define INIT_INTERVAL6 10 /* Wait to submit a initial riprequest */
89 /* alignment constraint for routing socket */
91 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
92 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
95 * Following two macros are highly depending on KAME Release
97 #define IN6_LINKLOCAL_IFINDEX(addr) \
98 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
100 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
102 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
103 (addr).s6_addr[3] = (index) & 0xff; \
106 struct ifc { /* Configuration of an interface */
107 char *ifc_name; /* if name */
108 struct ifc *ifc_next;
109 int ifc_index; /* if index */
110 int ifc_mtu; /* if mtu */
111 int ifc_metric; /* if metric */
112 u_int ifc_flags; /* flags */
113 short ifc_cflags; /* IFC_XXX */
114 struct in6_addr ifc_mylladdr; /* my link-local address */
115 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
116 struct iff *ifc_filter; /* filter structure */
117 struct ifac *ifc_addr; /* list of AF_INET6 addresses */
118 int ifc_joined; /* joined to ff02::9 */
121 struct ifac { /* Adddress associated to an interface */
122 struct ifc *ifa_conf; /* back pointer */
123 struct ifac *ifa_next;
124 struct in6_addr ifa_addr; /* address */
125 struct in6_addr ifa_raddr; /* remote address, valid in p2p */
126 int ifa_plen; /* prefix length */
131 struct in6_addr iff_addr;
133 struct iff *iff_next;
137 int nifc; /* number of valid ifc's */
138 struct ifc **index2ifc;
140 struct ifc *loopifcp = NULL; /* pointing to loopback */
141 int loopifindex = 0; /* ditto */
142 fd_set sockvec; /* vector to select() for receiving */
143 int rtsock; /* the routing socket */
144 int ripsock; /* socket to send/receive RIP datagram */
146 struct rip6 *ripbuf; /* packet buffer for sending */
149 * Maintain the routes in a linked list. When the number of the routes
150 * grows, somebody would like to introduce a hash based or a radix tree
151 * based structure. I believe the number of routes handled by RIP is
152 * limited and I don't have to manage a complex data structure, however.
154 * One of the major drawbacks of the linear linked list is the difficulty
155 * of representing the relationship between a couple of routes. This may
156 * be a significant problem when we have to support route aggregation with
157 * supressing the specifices covered by the aggregate.
161 struct riprt *rrt_next; /* next destination */
162 struct riprt *rrt_same; /* same destination - future use */
163 struct netinfo6 rrt_info; /* network info */
164 struct in6_addr rrt_gw; /* gateway */
165 u_long rrt_flags; /* kernel routing table flags */
166 u_long rrt_rflags; /* route6d routing table flags */
167 time_t rrt_t; /* when the route validated */
168 int rrt_index; /* ifindex from which this route got */
171 struct riprt *riprt = 0;
173 int dflag = 0; /* debug flag */
174 int qflag = 0; /* quiet flag */
175 int nflag = 0; /* don't update kernel routing table */
176 int aflag = 0; /* age out even the statically defined routes */
177 int hflag = 0; /* don't split horizon */
178 int lflag = 0; /* exchange site local routes */
179 int sflag = 0; /* announce static routes w/ split horizon */
180 int Sflag = 0; /* announce static routes to every interface */
181 unsigned long routetag = 0; /* route tag attached on originating case */
183 char *filter[MAXFILTER];
184 int filtertype[MAXFILTER];
189 struct sockaddr_storage ripsin;
191 struct rtentry rtentry;
194 time_t nextalarm = 0;
195 time_t sup_trig_update = 0;
201 static u_long seq = 0;
204 volatile sig_atomic_t seenalrm;
205 volatile sig_atomic_t seenquit;
206 volatile sig_atomic_t seenusr1;
208 #define RRTF_AGGREGATE 0x08000000
209 #define RRTF_NOADVERTISE 0x10000000
210 #define RRTF_NH_NOT_LLADDR 0x20000000
211 #define RRTF_SENDANYWAY 0x40000000
212 #define RRTF_CHANGED 0x80000000
214 int main __P((int, char **));
215 void sighandler __P((int));
216 void ripalarm __P((void));
217 void riprecv __P((void));
218 void ripsend __P((struct ifc *, struct sockaddr_in6 *, int));
219 int out_filter __P((struct riprt *, struct ifc *));
220 void init __P((void));
221 void sockopt __P((struct ifc *));
222 void ifconfig __P((void));
223 void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int));
224 void rtrecv __P((void));
225 int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *,
226 const struct sockaddr_in6 *));
227 int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *,
228 const struct sockaddr_in6 *));
229 void filterconfig __P((void));
230 int getifmtu __P((int));
231 const char *rttypes __P((struct rt_msghdr *));
232 const char *rtflags __P((struct rt_msghdr *));
233 const char *ifflags __P((int));
234 int ifrt __P((struct ifc *, int));
235 void ifrt_p2p __P((struct ifc *, int));
236 void applymask __P((struct in6_addr *, struct in6_addr *));
237 void applyplen __P((struct in6_addr *, int));
238 void ifrtdump __P((int));
239 void ifdump __P((int));
240 void ifdump0 __P((FILE *, const struct ifc *));
241 void rtdump __P((int));
242 void rt_entry __P((struct rt_msghdr *, int));
243 void rtdexit __P((void));
244 void riprequest __P((struct ifc *, struct netinfo6 *, int,
245 struct sockaddr_in6 *));
246 void ripflush __P((struct ifc *, struct sockaddr_in6 *));
247 void sendrequest __P((struct ifc *));
248 int sin6mask2len __P((const struct sockaddr_in6 *));
249 int mask2len __P((const struct in6_addr *, int));
250 int sendpacket __P((struct sockaddr_in6 *, int));
251 int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *));
252 int delroute __P((struct netinfo6 *, struct in6_addr *));
253 struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *));
254 void krtread __P((int));
255 int tobeadv __P((struct riprt *, struct ifc *));
256 char *allocopy __P((char *));
257 char *hms __P((void));
258 const char *inet6_n2p __P((const struct in6_addr *));
259 struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int));
260 struct in6_addr *plen2mask __P((int));
261 struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **));
262 int ripinterval __P((int));
263 time_t ripsuptrig __P((void));
264 void fatal __P((const char *, ...))
265 __attribute__((__format__(__printf__, 1, 2)));
266 void trace __P((int, const char *, ...))
267 __attribute__((__format__(__printf__, 2, 3)));
268 void tracet __P((int, const char *, ...))
269 __attribute__((__format__(__printf__, 2, 3)));
270 unsigned int if_maxindex __P((void));
271 struct ifc *ifc_find __P((char *));
272 struct iff *iff_find __P((struct ifc *, int));
273 void setindex2ifc __P((int, struct ifc *));
275 #define MALLOC(type) ((type *)malloc(sizeof(type)))
285 sigset_t mask, omask;
290 progname = strrchr(*argv, '/');
297 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
304 if (nfilter >= MAXFILTER) {
305 fatal("Exceeds MAXFILTER");
308 filtertype[nfilter] = ch;
309 filter[nfilter++] = allocopy(optarg);
313 routetag = strtoul(optarg, &ep, 0);
314 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
315 fatal("invalid route tag");
320 if ((rtlog = fopen(optarg, "w")) == NULL) {
321 fatal("Can not write to routelog");
325 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
326 FLAG('a', aflag, 1); break;
327 FLAG('d', dflag, 1); break;
328 FLAG('D', dflag, 2); break;
329 FLAG('h', hflag, 1); break;
330 FLAG('l', lflag, 1); break;
331 FLAG('n', nflag, 1); break;
332 FLAG('q', qflag, 1); break;
333 FLAG('s', sflag, 1); break;
334 FLAG('S', Sflag, 1); break;
337 fatal("Invalid option specified, terminating");
344 fatal("bogus extra arguments");
350 fprintf(stderr, "No kernel update is allowed\n");
352 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
355 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
357 memset(ripbuf, 0, RIP6_MAXMTU);
358 ripbuf->rip6_cmd = RIP6_RESPONSE;
359 ripbuf->rip6_vers = RIP6_VERSION;
360 ripbuf->rip6_res1[0] = 0;
361 ripbuf->rip6_res1[1] = 0;
365 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
366 if (ifcp->ifc_index < 0) {
368 "No ifindex found at %s (no link-local address?)\n",
375 if (loopifcp == NULL) {
376 fatal("No loopback found");
379 loopifindex = loopifcp->ifc_index;
380 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
389 if (daemon(0, 0) < 0) {
403 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
404 fprintf(pidfile, "%d\n", pid);
408 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
412 memset(ripbuf, 0, RIP6_MAXMTU);
413 ripbuf->rip6_cmd = RIP6_RESPONSE;
414 ripbuf->rip6_vers = RIP6_VERSION;
415 ripbuf->rip6_res1[0] = 0;
416 ripbuf->rip6_res1[1] = 0;
418 if (signal(SIGALRM, sighandler) == SIG_ERR ||
419 signal(SIGQUIT, sighandler) == SIG_ERR ||
420 signal(SIGTERM, sighandler) == SIG_ERR ||
421 signal(SIGUSR1, sighandler) == SIG_ERR ||
422 signal(SIGHUP, sighandler) == SIG_ERR ||
423 signal(SIGINT, sighandler) == SIG_ERR) {
428 * To avoid rip packet congestion (not on a cable but in this
429 * process), wait for a moment to send the first RIP6_RESPONSE
432 alarm(ripinterval(INIT_INTERVAL6));
434 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
435 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
439 syslog(LOG_INFO, "**** Started ****");
441 sigaddset(&mask, SIGALRM);
461 FD_COPY(&sockvec, &recvec);
463 switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
465 if (errno != EINTR) {
473 if (FD_ISSET(ripsock, &recvec)) {
474 sigprocmask(SIG_BLOCK, &mask, &omask);
476 sigprocmask(SIG_SETMASK, &omask, NULL);
478 if (FD_ISSET(rtsock, &recvec)) {
479 sigprocmask(SIG_BLOCK, &mask, &omask);
481 sigprocmask(SIG_SETMASK, &omask, NULL);
510 * gracefully exits after resetting sockopts.
519 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
520 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
521 delroute(&rrt->rrt_info, &rrt->rrt_gw);
526 syslog(LOG_INFO, "**** Terminated ****");
532 * Called periodically:
533 * 1. age out the learned route. remove it if necessary.
534 * 2. submit RIP6_RESPONSE packets.
535 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
536 * to invoke this function in every 1 or 5 or 10 seconds only to age the
537 * routes more precisely.
544 struct riprt *rrt, *rrt_prev, *rrt_next;
545 time_t t_lifetime, t_holddown;
547 /* age the RIP routes */
549 t_lifetime = time(NULL) - RIP_LIFETIME;
550 t_holddown = t_lifetime - RIP_HOLDDOWN;
551 for (rrt = riprt; rrt; rrt = rrt_next) {
552 rrt_next = rrt->rrt_next;
554 if (rrt->rrt_t == 0) {
558 if (rrt->rrt_t < t_holddown) {
560 rrt_prev->rrt_next = rrt->rrt_next;
562 riprt = rrt->rrt_next;
564 delroute(&rrt->rrt_info, &rrt->rrt_gw);
568 if (rrt->rrt_t < t_lifetime)
569 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
573 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
574 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
575 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
577 alarm(ripinterval(SUPPLY_INTERVAL6));
583 int i, int0, int255, error;
584 struct addrinfo hints, *res;
587 ifc = (struct ifc *)NULL;
589 nindex2ifc = 0; /*initial guess*/
591 snprintf(port, sizeof(port), "%d", RIP6_PORT);
593 memset(&hints, 0, sizeof(hints));
594 hints.ai_family = PF_INET6;
595 hints.ai_socktype = SOCK_DGRAM;
596 hints.ai_flags = AI_PASSIVE;
597 error = getaddrinfo(NULL, port, &hints, &res);
599 fatal("%s", gai_strerror(error));
603 fatal(":: resolved to multiple address");
607 int0 = 0; int255 = 255;
608 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
613 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
617 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
618 &int255, sizeof(int255)) < 0) {
619 fatal("rip IPV6_MULTICAST_HOPS");
622 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
623 &int0, sizeof(int0)) < 0) {
624 fatal("rip IPV6_MULTICAST_LOOP");
629 #ifdef IPV6_RECVPKTINFO
630 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
632 fatal("rip IPV6_RECVPKTINFO");
635 #else /* old adv. API */
636 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
638 fatal("rip IPV6_PKTINFO");
643 memset(&hints, 0, sizeof(hints));
644 hints.ai_family = PF_INET6;
645 hints.ai_socktype = SOCK_DGRAM;
646 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
648 fatal("%s", gai_strerror(error));
652 fatal("%s resolved to multiple address", RIP6_DEST);
655 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
660 memset(&sockvec, 0, sizeof(sockvec));
662 FD_SET(ripsock, &sockvec);
665 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
666 fatal("route socket");
669 FD_SET(rtsock, &sockvec);
671 rtsock = -1; /*just for safety */
675 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
678 * ripflush flushes the rip datagram stored in the rip buffer
681 static struct netinfo6 *np;
686 struct sockaddr_in6 *sin;
692 tracet(1, "Send(%s): info(%d) to %s.%d\n",
694 inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
696 tracet(1, "Send: info(%d) to %s.%d\n",
697 nrt, inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
699 np = ripbuf->rip6_nets;
700 for (i = 0; i < nrt; i++, np++) {
701 if (np->rip6_metric == NEXTHOP_METRIC) {
702 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
703 trace(2, " NextHop reset");
705 trace(2, " NextHop %s",
706 inet6_n2p(&np->rip6_dest));
709 trace(2, " %s/%d[%d]",
710 inet6_n2p(&np->rip6_dest),
711 np->rip6_plen, np->rip6_metric);
714 trace(2, " tag=0x%04x",
715 ntohs(np->rip6_tag) & 0xffff);
720 error = sendpacket(sin, RIPSIZE(nrt));
721 if (error == EAFNOSUPPORT) {
722 /* Protocol not supported */
723 tracet(1, "Could not send info to %s (%s): "
725 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
726 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
728 nrt = 0; np = ripbuf->rip6_nets;
732 * Generate RIP6_RESPONSE packets and send them.
735 ripsend(ifcp, sin, flag)
737 struct sockaddr_in6 *sin;
741 struct in6_addr *nh; /* next hop */
746 * Request from non-link local address is not
747 * a regular route6d update.
749 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
750 sizeof(struct udphdr) -
751 sizeof(struct rip6) + sizeof(struct netinfo6)) /
752 sizeof(struct netinfo6);
753 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
754 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
755 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
757 /* Put the route to the buffer */
765 if (nrt) /* Send last packet */
770 if ((flag & RRTF_SENDANYWAY) == 0 &&
771 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
775 if (iff_find(ifcp, 'N') != NULL)
778 /* -T: generate default route only */
779 if (iff_find(ifcp, 'T') != NULL) {
780 struct netinfo6 rrt_info;
781 memset(&rrt_info, 0, sizeof(struct netinfo6));
782 rrt_info.rip6_dest = in6addr_any;
783 rrt_info.rip6_plen = 0;
784 rrt_info.rip6_metric = 1;
785 rrt_info.rip6_metric += ifcp->ifc_metric;
786 rrt_info.rip6_tag = htons(routetag & 0xffff);
787 np = ripbuf->rip6_nets;
794 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
795 sizeof(struct udphdr) -
796 sizeof(struct rip6) + sizeof(struct netinfo6)) /
797 sizeof(struct netinfo6);
799 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
800 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
801 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
804 /* Need to check filter here */
805 if (out_filter(rrt, ifcp) == 0)
808 /* Check split horizon and other conditions */
809 if (tobeadv(rrt, ifcp) == 0)
812 /* Only considers the routes with flag if specified */
813 if ((flag & RRTF_CHANGED) &&
814 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
818 if (rrt->rrt_index == ifcp->ifc_index &&
819 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
820 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
821 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
822 if (nrt == maxrte - 2)
824 np->rip6_dest = rrt->rrt_gw;
825 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
826 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
829 np->rip6_metric = NEXTHOP_METRIC;
833 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
834 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
835 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
837 if (nrt == maxrte - 2)
839 memset(np, 0, sizeof(struct netinfo6));
840 np->rip6_metric = NEXTHOP_METRIC;
845 /* Put the route to the buffer */
853 if (nrt) /* Send last packet */
858 * outbound filter logic, per-route/interface.
861 out_filter(rrt, ifcp)
870 * -A: filter out less specific routes, if we have aggregated
873 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
874 if (iffp->iff_type != 'A')
876 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
878 ia = rrt->rrt_info.rip6_dest;
879 applyplen(&ia, iffp->iff_plen);
880 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
885 * if it is an aggregated route, advertise it only to the
886 * interfaces specified on -A.
888 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
890 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
891 if (iffp->iff_type != 'A')
893 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
894 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
905 * -O: advertise only if prefix matches the configured prefix.
907 if (iff_find(ifcp, 'O')) {
909 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
910 if (iffp->iff_type != 'O')
912 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
914 ia = rrt->rrt_info.rip6_dest;
915 applyplen(&ia, iffp->iff_plen);
916 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
925 /* the prefix should be advertised */
930 * Determine if the route is to be advertised on the specified interface.
931 * It checks options specified in the arguments and the split horizon rule.
939 /* Special care for static routes */
940 if (rrt->rrt_flags & RTF_STATIC) {
941 /* XXX don't advertise reject/blackhole routes */
942 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
945 if (Sflag) /* Yes, advertise it anyway */
947 if (sflag && rrt->rrt_index != ifcp->ifc_index)
951 /* Regular split horizon */
952 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
958 * Send a rip packet actually.
962 struct sockaddr_in6 *sin;
966 * MSG_DONTROUTE should not be specified when it responds with a
967 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
974 struct in6_pktinfo *pi;
976 struct sockaddr_in6 sincopy;
978 /* do not overwrite the given sin */
982 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)
983 || IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) {
984 idx = IN6_LINKLOCAL_IFINDEX(sin->sin6_addr);
985 SET_IN6_LINKLOCAL_IFINDEX(sin->sin6_addr, 0);
989 m.msg_name = (caddr_t)sin;
990 m.msg_namelen = sizeof(*sin);
991 iov[0].iov_base = (caddr_t)ripbuf;
992 iov[0].iov_len = len;
996 m.msg_control = NULL;
997 m.msg_controllen = 0;
999 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1000 cm = (struct cmsghdr *)cmsgbuf;
1001 m.msg_control = (caddr_t)cm;
1002 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1004 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1005 cm->cmsg_level = IPPROTO_IPV6;
1006 cm->cmsg_type = IPV6_PKTINFO;
1007 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1008 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1009 pi->ipi6_ifindex = idx;
1012 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1013 trace(1, "sendmsg: %s\n", strerror(errno));
1021 * Receive and process RIP packets. Update the routes/kernel forwarding
1022 * table if necessary.
1027 struct ifc *ifcp, *ic;
1028 struct sockaddr_in6 fsock;
1029 struct in6_addr nh; /* next hop */
1031 struct netinfo6 *np, *nq;
1033 int len, nn, need_trigger, idx;
1034 char buf[4 * RIP6_MAXMTU];
1038 struct iovec iov[2];
1039 u_char cmsgbuf[256];
1040 struct in6_pktinfo *pi;
1044 time_t t_half_lifetime;
1048 m.msg_name = (caddr_t)&fsock;
1049 m.msg_namelen = sizeof(fsock);
1050 iov[0].iov_base = (caddr_t)buf;
1051 iov[0].iov_len = sizeof(buf);
1054 cm = (struct cmsghdr *)cmsgbuf;
1055 m.msg_control = (caddr_t)cm;
1056 m.msg_controllen = sizeof(cmsgbuf);
1057 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1062 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1064 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1065 if (cm->cmsg_level == IPPROTO_IPV6 &&
1066 cm->cmsg_type == IPV6_PKTINFO) {
1067 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1068 idx = pi->ipi6_ifindex;
1072 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1073 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1075 nh = fsock.sin6_addr;
1076 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1077 sizeof(struct netinfo6);
1078 rp = (struct rip6 *)buf;
1081 if (rp->rip6_vers != RIP6_VERSION) {
1082 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1085 if (rp->rip6_cmd == RIP6_REQUEST) {
1086 if (idx && idx < nindex2ifc) {
1087 ifcp = index2ifc[idx];
1088 riprequest(ifcp, np, nn, &fsock);
1090 riprequest(NULL, np, nn, &fsock);
1095 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1096 trace(1, "Packets from non-ll addr: %s\n",
1097 inet6_n2p(&fsock.sin6_addr));
1098 return; /* Ignore packets from non-link-local addr */
1100 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1101 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1103 trace(1, "Packets to unknown interface index %d\n", idx);
1104 return; /* Ignore it */
1106 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1107 return; /* The packet is from me; ignore */
1108 if (rp->rip6_cmd != RIP6_RESPONSE) {
1109 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1114 if (iff_find(ifcp, 'N') != NULL)
1117 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1118 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1121 t_half_lifetime = t - (RIP_LIFETIME/2);
1122 for (; nn; nn--, np++) {
1123 if (np->rip6_metric == NEXTHOP_METRIC) {
1124 /* modify neighbor address */
1125 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1127 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1128 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1129 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1130 nh = fsock.sin6_addr;
1131 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1133 nh = fsock.sin6_addr;
1134 trace(1, "\tInvalid Nexthop: %s\n",
1135 inet6_n2p(&np->rip6_dest));
1139 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1140 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1141 inet6_n2p(&np->rip6_dest),
1142 np->rip6_plen, np->rip6_metric);
1145 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1146 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1147 inet6_n2p(&np->rip6_dest),
1148 np->rip6_plen, np->rip6_metric);
1151 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1152 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1153 inet6_n2p(&np->rip6_dest),
1154 np->rip6_plen, np->rip6_metric);
1157 /* may need to pass sitelocal prefix in some case, however*/
1158 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1159 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1160 inet6_n2p(&np->rip6_dest),
1161 np->rip6_plen, np->rip6_metric);
1164 trace(2, "\tnetinfo6: %s/%d [%d]",
1165 inet6_n2p(&np->rip6_dest),
1166 np->rip6_plen, np->rip6_metric);
1168 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1171 applyplen(&ia, np->rip6_plen);
1172 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1173 trace(2, " [junk outside prefix]");
1177 * -L: listen only if the prefix matches the configuration
1179 ok = 1; /* if there's no L filter, it is ok */
1180 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
1181 if (iffp->iff_type != 'L')
1184 if (np->rip6_plen < iffp->iff_plen)
1186 /* special rule: ::/0 means default, not "in /0" */
1187 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1190 applyplen(&ia, iffp->iff_plen);
1191 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1197 trace(2, " (filtered)\n");
1203 np->rip6_metric += ifcp->ifc_metric;
1204 if (np->rip6_metric > HOPCNT_INFINITY6)
1205 np->rip6_metric = HOPCNT_INFINITY6;
1207 applyplen(&np->rip6_dest, np->rip6_plen);
1208 if ((rrt = rtsearch(np, NULL)) != NULL) {
1209 if (rrt->rrt_t == 0)
1210 continue; /* Intf route has priority */
1211 nq = &rrt->rrt_info;
1212 if (nq->rip6_metric > np->rip6_metric) {
1213 if (rrt->rrt_index == ifcp->ifc_index &&
1214 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1215 /* Small metric from the same gateway */
1216 nq->rip6_metric = np->rip6_metric;
1218 /* Better route found */
1219 rrt->rrt_index = ifcp->ifc_index;
1220 /* Update routing table */
1221 delroute(nq, &rrt->rrt_gw);
1224 addroute(rrt, &nh, ifcp);
1226 rrt->rrt_rflags |= RRTF_CHANGED;
1229 } else if (nq->rip6_metric < np->rip6_metric &&
1230 rrt->rrt_index == ifcp->ifc_index &&
1231 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1232 /* Got worse route from same gw */
1233 nq->rip6_metric = np->rip6_metric;
1235 rrt->rrt_rflags |= RRTF_CHANGED;
1237 } else if (nq->rip6_metric == np->rip6_metric &&
1238 np->rip6_metric < HOPCNT_INFINITY6) {
1239 if (rrt->rrt_index == ifcp->ifc_index &&
1240 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1241 /* same metric, same route from same gw */
1243 } else if (rrt->rrt_t < t_half_lifetime) {
1244 /* Better route found */
1245 rrt->rrt_index = ifcp->ifc_index;
1246 /* Update routing table */
1247 delroute(nq, &rrt->rrt_gw);
1250 addroute(rrt, &nh, ifcp);
1251 rrt->rrt_rflags |= RRTF_CHANGED;
1256 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1257 * do not update age value. Do nothing.
1259 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1260 /* Got a new valid route */
1261 if ((rrt = MALLOC(struct riprt)) == NULL) {
1262 fatal("malloc: struct riprt");
1265 memset(rrt, 0, sizeof(*rrt));
1266 nq = &rrt->rrt_info;
1268 rrt->rrt_same = NULL;
1269 rrt->rrt_index = ifcp->ifc_index;
1270 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1273 applyplen(&nq->rip6_dest, nq->rip6_plen);
1274 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1275 rrt->rrt_flags |= RTF_HOST;
1277 /* Put the route to the list */
1278 rrt->rrt_next = riprt;
1280 /* Update routing table */
1281 addroute(rrt, &nh, ifcp);
1282 rrt->rrt_rflags |= RRTF_CHANGED;
1287 /* XXX need to care the interval between triggered updates */
1289 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1290 for (ic = ifc; ic; ic = ic->ifc_next) {
1291 if (ifcp->ifc_index == ic->ifc_index)
1293 if (ic->ifc_flags & IFF_UP)
1294 ripsend(ic, &ic->ifc_ripsin,
1298 /* Reset the flag */
1299 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1300 rrt->rrt_rflags &= ~RRTF_CHANGED;
1305 * Send all routes request packet to the specified interface.
1311 struct netinfo6 *np;
1314 if (ifcp->ifc_flags & IFF_LOOPBACK)
1316 ripbuf->rip6_cmd = RIP6_REQUEST;
1317 np = ripbuf->rip6_nets;
1318 memset(np, 0, sizeof(struct netinfo6));
1319 np->rip6_metric = HOPCNT_INFINITY6;
1320 tracet(1, "Send rtdump Request to %s (%s)\n",
1321 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1322 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1323 if (error == EAFNOSUPPORT) {
1324 /* Protocol not supported */
1325 tracet(1, "Could not send rtdump Request to %s (%s): "
1326 "set IFF_UP to 0\n",
1327 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1328 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1330 ripbuf->rip6_cmd = RIP6_RESPONSE;
1334 * Process a RIP6_REQUEST packet.
1337 riprequest(ifcp, np, nn, sin)
1339 struct netinfo6 *np;
1341 struct sockaddr_in6 *sin;
1346 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1347 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1348 /* Specific response, don't split-horizon */
1349 trace(1, "\tRIP Request\n");
1350 for (i = 0; i < nn; i++, np++) {
1351 rrt = rtsearch(np, NULL);
1353 np->rip6_metric = rrt->rrt_info.rip6_metric;
1355 np->rip6_metric = HOPCNT_INFINITY6;
1357 (void)sendpacket(sin, RIPSIZE(nn));
1360 /* Whole routing table dump */
1361 trace(1, "\tRIP Request -- whole routing table\n");
1362 ripsend(ifcp, sin, RRTF_SENDANYWAY);
1366 * Get information of each interface.
1371 struct ifaddrs *ifap, *ifa;
1373 struct ipv6_mreq mreq;
1376 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1381 if (getifaddrs(&ifap) != 0) {
1382 fatal("getifaddrs");
1386 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1387 if (ifa->ifa_addr->sa_family != AF_INET6)
1389 ifcp = ifc_find(ifa->ifa_name);
1390 /* we are interested in multicast-capable interfaces */
1391 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1395 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1396 fatal("malloc: struct ifc");
1399 memset(ifcp, 0, sizeof(*ifcp));
1400 ifcp->ifc_index = -1;
1401 ifcp->ifc_next = ifc;
1404 ifcp->ifc_name = allocopy(ifa->ifa_name);
1406 ifcp->ifc_filter = 0;
1407 ifcp->ifc_flags = ifa->ifa_flags;
1408 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1409 ifflags(ifcp->ifc_flags));
1410 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1413 /* update flag, this may be up again */
1414 if (ifcp->ifc_flags != ifa->ifa_flags) {
1415 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1416 ifflags(ifcp->ifc_flags));
1417 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1418 ifcp->ifc_cflags |= IFC_CHANGED;
1420 ifcp->ifc_flags = ifa->ifa_flags;
1422 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
1423 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1424 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1425 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1426 mreq.ipv6mr_interface = ifcp->ifc_index;
1427 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1428 &mreq, sizeof(mreq)) < 0) {
1429 fatal("IPV6_JOIN_GROUP");
1432 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1441 ifconfig1(name, sa, ifcp, s)
1443 const struct sockaddr *sa;
1447 struct in6_ifreq ifr;
1448 const struct sockaddr_in6 *sin;
1453 sin = (const struct sockaddr_in6 *)sa;
1454 ifr.ifr_addr = *sin;
1455 strcpy(ifr.ifr_name, name);
1456 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1457 fatal("ioctl: SIOCGIFNETMASK_IN6");
1460 plen = sin6mask2len(&ifr.ifr_addr);
1461 if ((ifa = ifa_match(ifcp, &sin->sin6_addr, plen)) != NULL) {
1462 /* same interface found */
1463 /* need check if something changed */
1464 /* XXX not yet implemented */
1468 * New address is found
1470 if ((ifa = MALLOC(struct ifac)) == NULL) {
1471 fatal("malloc: struct ifac");
1474 memset(ifa, 0, sizeof(*ifa));
1475 ifa->ifa_conf = ifcp;
1476 ifa->ifa_next = ifcp->ifc_addr;
1477 ifcp->ifc_addr = ifa;
1478 ifa->ifa_addr = sin->sin6_addr;
1479 ifa->ifa_plen = plen;
1480 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1481 ifr.ifr_addr = *sin;
1482 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1483 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1486 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
1487 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
1488 trace(1, "found address %s/%d -- %s\n",
1489 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
1491 trace(1, "found address %s/%d\n",
1492 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
1494 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1495 ifcp->ifc_mylladdr = ifa->ifa_addr;
1496 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
1497 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1498 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1500 setindex2ifc(ifcp->ifc_index, ifcp);
1501 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1502 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1503 ifcp->ifc_mtu = RIP6_MAXMTU;
1504 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1505 fatal("ioctl: SIOCGIFMETRIC");
1508 ifcp->ifc_metric = ifr.ifr_metric;
1509 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1510 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1512 ifcp->ifc_cflags |= IFC_CHANGED;
1516 * Receive and process routing messages.
1517 * Update interface information as necesssary.
1524 struct rt_msghdr *rtm;
1525 struct ifa_msghdr *ifam;
1526 struct if_msghdr *ifm;
1528 struct ifc *ifcp, *ic;
1529 int iface = 0, rtable = 0;
1530 struct sockaddr_in6 *rta[RTAX_MAX];
1531 struct sockaddr_in6 mask;
1535 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1536 perror("read from rtsock");
1539 if (len < sizeof(*rtm)) {
1540 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1541 len, (u_long)sizeof(*rtm));
1545 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1546 /* safety against bogus message */
1547 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1548 trace(1, "bogus rtmsg: length=%d\n",
1549 ((struct rt_msghdr *)p)->rtm_msglen);
1555 switch (((struct rt_msghdr *)p)->rtm_type) {
1558 ifam = (struct ifa_msghdr *)p;
1559 addrs = ifam->ifam_addrs;
1560 q = (char *)(ifam + 1);
1563 ifm = (struct if_msghdr *)p;
1564 addrs = ifm->ifm_addrs;
1565 q = (char *)(ifm + 1);
1568 rtm = (struct rt_msghdr *)p;
1569 addrs = rtm->rtm_addrs;
1570 q = (char *)(rtm + 1);
1571 if (rtm->rtm_version != RTM_VERSION) {
1572 trace(1, "unexpected rtmsg version %d "
1574 rtm->rtm_version, RTM_VERSION);
1577 if (rtm->rtm_pid == pid) {
1579 trace(1, "rtmsg looped back to me, ignored\n");
1585 memset(&rta, 0, sizeof(rta));
1586 for (i = 0; i < RTAX_MAX; i++) {
1587 if (addrs & (1 << i)) {
1588 rta[i] = (struct sockaddr_in6 *)q;
1589 q += ROUNDUP(rta[i]->sin6_len);
1593 trace(1, "rtsock: %s (addrs=%x)\n",
1594 rttypes((struct rt_msghdr *)p), addrs);
1597 i < ((struct rt_msghdr *)p)->rtm_msglen;
1599 fprintf(stderr, "%02x ", p[i] & 0xff);
1600 if (i % 16 == 15) fprintf(stderr, "\n");
1602 fprintf(stderr, "\n");
1608 * We may be able to optimize by using ifm->ifm_index or
1609 * ifam->ifam_index. For simplicity we don't do that here.
1611 switch (((struct rt_msghdr *)p)->rtm_type) {
1624 /* nothing to be done here */
1625 trace(1, "\tnothing to be done, ignored\n");
1630 if (rta[RTAX_DST] == NULL) {
1631 trace(1, "\tno destination, ignored\n");
1634 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1635 trace(1, "\taf mismatch, ignored\n");
1638 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1639 trace(1, "\tlinklocal destination, ignored\n");
1642 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1643 trace(1, "\tloopback destination, ignored\n");
1644 continue; /* Loopback */
1646 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1647 trace(1, "\tmulticast destination, ignored\n");
1653 switch (((struct rt_msghdr *)p)->rtm_type) {
1662 /* should already be handled */
1663 fatal("rtrecv: never reach here");
1666 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1667 trace(1, "\tsome of dst/gw/netamsk are "
1668 "unavailable, ignored\n");
1671 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1672 mask.sin6_len = sizeof(mask);
1673 memset(&mask.sin6_addr, 0xff,
1674 sizeof(mask.sin6_addr));
1675 rta[RTAX_NETMASK] = &mask;
1676 } else if (!rta[RTAX_NETMASK]) {
1677 trace(1, "\tsome of dst/gw/netamsk are "
1678 "unavailable, ignored\n");
1681 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1682 rta[RTAX_NETMASK]) == 0) {
1683 rtable++; /*just to be sure*/
1688 trace(1, "\tnot supported yet, ignored\n");
1691 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1692 trace(1, "\tno netmask or ifa given, ignored\n");
1695 if (ifam->ifam_index < nindex2ifc)
1696 ifcp = index2ifc[ifam->ifam_index];
1700 trace(1, "\tinvalid ifam_index %d, ignored\n",
1704 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1709 trace(1, "\tnot supported yet, ignored\n");
1716 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1718 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
1719 if (ifcp->ifc_cflags & IFC_CHANGED) {
1720 if (ifrt(ifcp, 1)) {
1721 for (ic = ifc; ic; ic = ic->ifc_next) {
1722 if (ifcp->ifc_index == ic->ifc_index)
1724 if (ic->ifc_flags & IFF_UP)
1725 ripsend(ic, &ic->ifc_ripsin,
1728 /* Reset the flag */
1729 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1730 rrt->rrt_rflags &= ~RRTF_CHANGED;
1732 ifcp->ifc_cflags &= ~IFC_CHANGED;
1736 trace(1, "rtsock: read routing table again\n");
1742 * remove specified route from the internal routing table.
1745 rt_del(sdst, sgw, smask)
1746 const struct sockaddr_in6 *sdst;
1747 const struct sockaddr_in6 *sgw;
1748 const struct sockaddr_in6 *smask;
1750 const struct in6_addr *dst = NULL;
1751 const struct in6_addr *gw = NULL;
1753 struct netinfo6 ni6;
1754 struct riprt *rrt = NULL;
1757 if (sdst->sin6_family != AF_INET6) {
1758 trace(1, "\tother AF, ignored\n");
1761 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1762 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1763 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1764 trace(1, "\taddress %s not interesting, ignored\n",
1765 inet6_n2p(&sdst->sin6_addr));
1768 dst = &sdst->sin6_addr;
1769 if (sgw->sin6_family == AF_INET6) {
1771 gw = &sgw->sin6_addr;
1772 prefix = sin6mask2len(smask);
1773 } else if (sgw->sin6_family == AF_LINK) {
1775 * Interface route... a hard case. We need to get the prefix
1776 * length from the kernel, but we now are parsing rtmsg.
1777 * We'll purge matching routes from my list, then get the
1780 struct riprt *longest;
1781 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1784 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
1785 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1787 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1789 || longest->rrt_info.rip6_plen <
1790 rrt->rrt_info.rip6_plen) {
1797 trace(1, "\tno matching interface route found\n");
1800 gw = &in6addr_loopback;
1801 prefix = rrt->rrt_info.rip6_plen;
1803 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1807 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1808 trace(1, "gw %s\n", inet6_n2p(gw));
1809 t_lifetime = time(NULL) - RIP_LIFETIME;
1810 /* age route for interface address */
1811 memset(&ni6, 0, sizeof(ni6));
1812 ni6.rip6_dest = *dst;
1813 ni6.rip6_plen = prefix;
1814 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1815 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
1817 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
1818 trace(1, "\tno route found\n");
1822 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
1823 trace(1, "\tyou can delete static routes only\n");
1826 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
1827 trace(1, "\tgw mismatch: %s <-> ",
1828 inet6_n2p(&rrt->rrt_gw));
1829 trace(1, "%s\n", inet6_n2p(gw));
1831 trace(1, "\troute found, age it\n");
1832 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1833 rrt->rrt_t = t_lifetime;
1834 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1841 * remove specified address from internal interface/routing table.
1844 rt_deladdr(ifcp, sifa, smask)
1846 const struct sockaddr_in6 *sifa;
1847 const struct sockaddr_in6 *smask;
1849 const struct in6_addr *addr = NULL;
1851 struct ifac *ifa = NULL;
1852 struct netinfo6 ni6;
1853 struct riprt *rrt = NULL;
1857 if (sifa->sin6_family != AF_INET6) {
1858 trace(1, "\tother AF, ignored\n");
1861 addr = &sifa->sin6_addr;
1862 prefix = sin6mask2len(smask);
1864 trace(1, "\tdeleting %s/%d from %s\n",
1865 inet6_n2p(addr), prefix, ifcp->ifc_name);
1866 ifa = ifa_match(ifcp, addr, prefix);
1868 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1869 inet6_n2p(addr), prefix, ifcp->ifc_name);
1872 if (ifa->ifa_conf != ifcp) {
1873 trace(1, "\taddress table corrupt: back pointer does not match "
1875 ifcp->ifc_name, ifa->ifa_conf->ifc_name);
1878 /* remove ifa from interface */
1879 if (ifcp->ifc_addr == ifa)
1880 ifcp->ifc_addr = ifa->ifa_next;
1883 for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
1884 if (p->ifa_next == ifa) {
1885 p->ifa_next = ifa->ifa_next;
1890 ifa->ifa_next = NULL;
1891 ifa->ifa_conf = NULL;
1892 t_lifetime = time(NULL) - RIP_LIFETIME;
1893 /* age route for interface address */
1894 memset(&ni6, 0, sizeof(ni6));
1895 ni6.rip6_dest = ifa->ifa_addr;
1896 ni6.rip6_plen = ifa->ifa_plen;
1897 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
1898 trace(1, "\tfind interface route %s/%d on %d\n",
1899 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
1900 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1901 struct in6_addr none;
1902 memset(&none, 0, sizeof(none));
1903 if (rrt->rrt_index == ifcp->ifc_index &&
1904 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
1905 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
1906 trace(1, "\troute found, age it\n");
1907 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1908 rrt->rrt_t = t_lifetime;
1909 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1913 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1914 inet6_n2p(&rrt->rrt_info.rip6_dest),
1915 rrt->rrt_info.rip6_plen,
1919 trace(1, "\tno interface route found\n");
1920 /* age route for p2p destination */
1921 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1922 memset(&ni6, 0, sizeof(ni6));
1923 ni6.rip6_dest = ifa->ifa_raddr;
1924 ni6.rip6_plen = 128;
1925 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1926 trace(1, "\tfind p2p route %s/%d on %d\n",
1927 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
1929 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1930 if (rrt->rrt_index == ifcp->ifc_index &&
1931 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
1932 trace(1, "\troute found, age it\n");
1933 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1934 rrt->rrt_t = t_lifetime;
1935 rrt->rrt_info.rip6_metric =
1940 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1941 inet6_n2p(&rrt->rrt_info.rip6_dest),
1942 rrt->rrt_info.rip6_plen,
1946 trace(1, "\tno p2p route found\n");
1948 return updated ? 0 : -1;
1952 * Get each interface address and put those interface routes to the route
1961 struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
1962 struct netinfo6 *np;
1964 int need_trigger = 0;
1966 if (ifcp->ifc_flags & IFF_LOOPBACK)
1967 return 0; /* ignore loopback */
1968 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1969 ifrt_p2p(ifcp, again);
1973 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
1974 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1976 trace(1, "route: %s on %s: "
1977 "skip linklocal interface address\n",
1978 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
1982 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
1984 trace(1, "route: %s: skip unspec interface address\n",
1989 if (ifcp->ifc_flags & IFF_UP) {
1990 if ((rrt = MALLOC(struct riprt)) == NULL)
1991 fatal("malloc: struct riprt");
1992 memset(rrt, 0, sizeof(*rrt));
1993 rrt->rrt_same = NULL;
1994 rrt->rrt_index = ifcp->ifc_index;
1995 rrt->rrt_t = 0; /* don't age */
1996 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
1997 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
1998 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
1999 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2000 rrt->rrt_flags = RTF_CLONING;
2001 rrt->rrt_rflags |= RRTF_CHANGED;
2002 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
2003 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2005 /* XXX why gateway address == network adddress? */
2006 rrt->rrt_gw = ifa->ifa_addr;
2008 np = &rrt->rrt_info;
2009 search_rrt = rtsearch(np, &prev_rrt);
2010 if (search_rrt != NULL) {
2011 if (search_rrt->rrt_info.rip6_metric >
2012 rrt->rrt_info.rip6_metric) {
2014 prev_rrt->rrt_next = rrt->rrt_next;
2016 riprt = rrt->rrt_next;
2017 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2020 /* Already have better route */
2022 trace(1, "route: %s/%d: "
2023 "already registered (%s)\n",
2024 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2031 /* Attach the route to the list */
2032 trace(1, "route: %s/%d: register route (%s)\n",
2033 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2035 rrt->rrt_next = riprt;
2037 addroute(rrt, &rrt->rrt_gw, ifcp);
2039 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2042 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
2043 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2044 t_lifetime = time(NULL) - RIP_LIFETIME;
2045 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2046 loop_rrt->rrt_t = t_lifetime;
2047 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2048 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2055 return need_trigger;
2059 * there are couple of p2p interface routing models. "behavior" lets
2060 * you pick one. it looks that gated behavior fits best with BSDs,
2061 * since BSD kernels does not look at prefix length on p2p interfaces.
2064 ifrt_p2p(ifcp, again)
2069 struct riprt *rrt, *orrt, *prevrrt;
2070 struct netinfo6 *np;
2071 struct in6_addr addr, dest;
2072 int advert, ignore, i;
2073 #define P2PADVERT_NETWORK 1
2074 #define P2PADVERT_ADDR 2
2075 #define P2PADVERT_DEST 4
2076 #define P2PADVERT_MAX 4
2077 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2078 const char *category = "";
2081 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2082 addr = ifa->ifa_addr;
2083 dest = ifa->ifa_raddr;
2084 applyplen(&addr, ifa->ifa_plen);
2085 applyplen(&dest, ifa->ifa_plen);
2086 advert = ignore = 0;
2090 * honor addr/plen, just like normal shared medium
2091 * interface. this may cause trouble if you reuse
2092 * addr/plen on other interfaces.
2094 * advertise addr/plen.
2096 advert |= P2PADVERT_NETWORK;
2100 * prefixlen on p2p interface is meaningless.
2101 * advertise addr/128 and dest/128.
2103 * do not install network route to route6d routing
2104 * table (if we do, it would prevent route installation
2105 * for other p2p interface that shares addr/plen).
2107 * XXX what should we do if dest is ::? it will not
2108 * get announced anyways (see following filter),
2109 * but we need to think.
2111 advert |= P2PADVERT_ADDR;
2112 advert |= P2PADVERT_DEST;
2113 ignore |= P2PADVERT_NETWORK;
2117 * just for testing. actually the code is redundant
2118 * given the current p2p interface address assignment
2119 * rule for kame kernel.
2122 * A/n -> announce A/n
2123 * A B/n, A and B share prefix -> A/n (= B/n)
2124 * A B/n, do not share prefix -> A/128 and B/128
2125 * actually, A/64 and A B/128 are the only cases
2126 * permitted by the kernel:
2128 * A B/128 -> A/128 and B/128
2130 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
2131 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2132 advert |= P2PADVERT_NETWORK;
2134 advert |= P2PADVERT_ADDR;
2135 advert |= P2PADVERT_DEST;
2136 ignore |= P2PADVERT_NETWORK;
2139 advert |= P2PADVERT_NETWORK;
2143 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2144 if ((ignore & i) != 0)
2146 if ((rrt = MALLOC(struct riprt)) == NULL) {
2147 fatal("malloc: struct riprt");
2150 memset(rrt, 0, sizeof(*rrt));
2151 rrt->rrt_same = NULL;
2152 rrt->rrt_index = ifcp->ifc_index;
2153 rrt->rrt_t = 0; /* don't age */
2155 case P2PADVERT_NETWORK:
2156 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2157 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2158 applyplen(&rrt->rrt_info.rip6_dest,
2160 category = "network";
2162 case P2PADVERT_ADDR:
2163 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2164 rrt->rrt_info.rip6_plen = 128;
2165 rrt->rrt_gw = in6addr_loopback;
2168 case P2PADVERT_DEST:
2169 rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
2170 rrt->rrt_info.rip6_plen = 128;
2171 rrt->rrt_gw = ifa->ifa_addr;
2175 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2176 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2178 trace(1, "route: %s: skip unspec/linklocal "
2179 "(%s on %s)\n", category, ifcp->ifc_name);
2184 if ((advert & i) == 0) {
2185 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2189 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2190 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2191 np = &rrt->rrt_info;
2192 orrt = rtsearch(np, &prevrrt);
2194 /* Attach the route to the list */
2195 trace(1, "route: %s/%d: register route "
2197 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2198 category, ifcp->ifc_name, noadv);
2199 rrt->rrt_next = riprt;
2201 } else if (rrt->rrt_index != orrt->rrt_index ||
2202 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2204 rrt->rrt_next = orrt->rrt_next;
2206 prevrrt->rrt_next = rrt;
2211 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2212 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2213 category, ifcp->ifc_name, noadv);
2217 trace(1, "route: %s/%d: "
2218 "already registered (%s on %s%s)\n",
2219 inet6_n2p(&np->rip6_dest),
2220 np->rip6_plen, category,
2221 ifcp->ifc_name, noadv);
2227 #undef P2PADVERT_NETWORK
2228 #undef P2PADVERT_ADDR
2229 #undef P2PADVERT_DEST
2230 #undef P2PADVERT_MAX
2240 struct if_msghdr *ifm;
2247 mib[4] = NET_RT_IFLIST;
2249 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2250 fatal("sysctl estimate NET_RT_IFLIST");
2253 if ((buf = malloc(msize)) == NULL) {
2257 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2258 fatal("sysctl NET_RT_IFLIST");
2261 ifm = (struct if_msghdr *)buf;
2262 mtu = ifm->ifm_data.ifi_mtu;
2264 if (ifindex != ifm->ifm_index) {
2265 fatal("ifindex does not match with ifm_index");
2275 struct rt_msghdr *rtm;
2277 #define RTTYPE(s, f) \
2279 if (rtm->rtm_type == (f)) \
2282 RTTYPE("ADD", RTM_ADD);
2283 RTTYPE("DELETE", RTM_DELETE);
2284 RTTYPE("CHANGE", RTM_CHANGE);
2285 RTTYPE("GET", RTM_GET);
2286 RTTYPE("LOSING", RTM_LOSING);
2287 RTTYPE("REDIRECT", RTM_REDIRECT);
2288 RTTYPE("MISS", RTM_MISS);
2289 RTTYPE("LOCK", RTM_LOCK);
2290 RTTYPE("OLDADD", RTM_OLDADD);
2291 RTTYPE("OLDDEL", RTM_OLDDEL);
2292 RTTYPE("RESOLVE", RTM_RESOLVE);
2293 RTTYPE("NEWADDR", RTM_NEWADDR);
2294 RTTYPE("DELADDR", RTM_DELADDR);
2295 RTTYPE("IFINFO", RTM_IFINFO);
2297 RTTYPE("OLDADD", RTM_OLDADD);
2300 RTTYPE("OLDDEL", RTM_OLDDEL);
2303 RTTYPE("OIFINFO", RTM_OIFINFO);
2305 #ifdef RTM_IFANNOUNCE
2306 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2309 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2312 RTTYPE("DELMADDR", RTM_DELMADDR);
2320 struct rt_msghdr *rtm;
2322 static char buf[BUFSIZ];
2325 * letter conflict should be okay. painful when *BSD diverges...
2327 strlcpy(buf, "", sizeof(buf));
2328 #define RTFLAG(s, f) \
2330 if (rtm->rtm_flags & (f)) \
2331 strlcat(buf, (s), sizeof(buf)); \
2333 RTFLAG("U", RTF_UP);
2334 RTFLAG("G", RTF_GATEWAY);
2335 RTFLAG("H", RTF_HOST);
2336 RTFLAG("R", RTF_REJECT);
2337 RTFLAG("D", RTF_DYNAMIC);
2338 RTFLAG("M", RTF_MODIFIED);
2339 RTFLAG("d", RTF_DONE);
2341 RTFLAG("m", RTF_MASK);
2343 RTFLAG("C", RTF_CLONING);
2345 RTFLAG("c", RTF_CLONED);
2347 #ifdef RTF_PRCLONING
2348 RTFLAG("c", RTF_PRCLONING);
2350 #ifdef RTF_WASCLONED
2351 RTFLAG("W", RTF_WASCLONED);
2353 RTFLAG("X", RTF_XRESOLVE);
2354 RTFLAG("L", RTF_LLINFO);
2355 RTFLAG("S", RTF_STATIC);
2356 RTFLAG("B", RTF_BLACKHOLE);
2358 RTFLAG("3", RTF_PROTO3);
2360 RTFLAG("2", RTF_PROTO2);
2361 RTFLAG("1", RTF_PROTO1);
2362 #ifdef RTF_BROADCAST
2363 RTFLAG("b", RTF_BROADCAST);
2366 RTFLAG("d", RTF_DEFAULT);
2368 #ifdef RTF_ISAROUTER
2369 RTFLAG("r", RTF_ISAROUTER);
2372 RTFLAG("T", RTF_TUNNEL);
2375 RTFLAG("A", RTF_AUTH);
2378 RTFLAG("E", RTF_CRYPT);
2388 static char buf[BUFSIZ];
2390 strlcpy(buf, "", sizeof(buf));
2391 #define IFFLAG(s, f) \
2395 strlcat(buf, ",", sizeof(buf)); \
2396 strlcat(buf, s, sizeof(buf)); \
2399 IFFLAG("UP", IFF_UP);
2400 IFFLAG("BROADCAST", IFF_BROADCAST);
2401 IFFLAG("DEBUG", IFF_DEBUG);
2402 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2403 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2404 #ifdef IFF_NOTRAILERS
2405 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2408 IFFLAG("SMART", IFF_SMART);
2410 IFFLAG("RUNNING", IFF_RUNNING);
2411 IFFLAG("NOARP", IFF_NOARP);
2412 IFFLAG("PROMISC", IFF_PROMISC);
2413 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2414 IFFLAG("OACTIVE", IFF_OACTIVE);
2415 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2416 IFFLAG("LINK0", IFF_LINK0);
2417 IFFLAG("LINK1", IFF_LINK1);
2418 IFFLAG("LINK2", IFF_LINK2);
2419 IFFLAG("MULTICAST", IFF_MULTICAST);
2430 char *buf, *p, *lim;
2431 struct rt_msghdr *rtm;
2440 mib[3] = AF_INET6; /* Address family */
2441 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2442 mib[5] = 0; /* No flags */
2448 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2449 errmsg = "sysctl estimate";
2452 if ((buf = malloc(msize)) == NULL) {
2456 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2457 errmsg = "sysctl NET_RT_DUMP";
2460 } while (retry < 5 && errmsg != NULL);
2462 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2465 } else if (1 < retry)
2466 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2469 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2470 rtm = (struct rt_msghdr *)p;
2471 rt_entry(rtm, again);
2477 rt_entry(rtm, again)
2478 struct rt_msghdr *rtm;
2481 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2482 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2483 char *rtmp, *ifname = NULL;
2484 struct riprt *rrt, *orrt;
2485 struct netinfo6 *np;
2488 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2489 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2490 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
2491 return; /* not interested in the link route */
2493 /* do not look at cloned routes */
2494 #ifdef RTF_WASCLONED
2495 if (rtm->rtm_flags & RTF_WASCLONED)
2499 if (rtm->rtm_flags & RTF_CLONED)
2503 * do not look at dynamic routes.
2504 * netbsd/openbsd cloned routes have UGHD.
2506 if (rtm->rtm_flags & RTF_DYNAMIC)
2508 rtmp = (char *)(rtm + 1);
2510 if ((rtm->rtm_addrs & RTA_DST) == 0)
2511 return; /* ignore routes without destination address */
2512 sin6_dst = (struct sockaddr_in6 *)rtmp;
2513 rtmp += ROUNDUP(sin6_dst->sin6_len);
2514 if (rtm->rtm_addrs & RTA_GATEWAY) {
2515 sin6_gw = (struct sockaddr_in6 *)rtmp;
2516 rtmp += ROUNDUP(sin6_gw->sin6_len);
2518 if (rtm->rtm_addrs & RTA_NETMASK) {
2519 sin6_mask = (struct sockaddr_in6 *)rtmp;
2520 rtmp += ROUNDUP(sin6_mask->sin6_len);
2522 if (rtm->rtm_addrs & RTA_GENMASK) {
2523 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2524 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2526 if (rtm->rtm_addrs & RTA_IFP) {
2527 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2528 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2532 if (sin6_dst->sin6_family != AF_INET6)
2534 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2535 return; /* Link-local */
2536 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2537 return; /* Loopback */
2538 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2541 if ((rrt = MALLOC(struct riprt)) == NULL) {
2542 fatal("malloc: struct riprt");
2545 memset(rrt, 0, sizeof(*rrt));
2546 np = &rrt->rrt_info;
2547 rrt->rrt_same = NULL;
2548 rrt->rrt_t = time(NULL);
2549 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2550 rrt->rrt_t = 0; /* Don't age static routes */
2552 np->rip6_tag = htons(routetag & 0xffff);
2556 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2557 if (np->rip6_metric < 1)
2558 np->rip6_metric = 1;
2559 rrt->rrt_flags = rtm->rtm_flags;
2560 np->rip6_dest = sin6_dst->sin6_addr;
2563 if (rtm->rtm_flags & RTF_HOST)
2564 np->rip6_plen = 128; /* Host route */
2566 np->rip6_plen = sin6mask2len(sin6_mask);
2570 orrt = rtsearch(np, NULL);
2571 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2574 trace(1, "route: %s/%d flags %s: already registered\n",
2575 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2583 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2585 if (sin6_gw->sin6_family == AF_INET6)
2586 rrt->rrt_gw = sin6_gw->sin6_addr;
2587 else if (sin6_gw->sin6_family == AF_LINK) {
2588 /* XXX in case ppp link? */
2589 rrt->rrt_gw = in6addr_loopback;
2591 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2593 trace(1, "route: %s/%d flags %s",
2594 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2595 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2599 if (s < nindex2ifc && index2ifc[s])
2600 ifname = index2ifc[s]->ifc_name;
2602 trace(1, " not configured\n");
2606 trace(1, " if %s sock %d", ifname, s);
2612 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2613 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
2615 && (rrt->rrt_flags & RTF_LOCAL) == 0
2618 trace(0, "***** Gateway %s is not a link-local address.\n",
2619 inet6_n2p(&rrt->rrt_gw));
2620 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2621 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2622 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2625 /* Put it to the route list */
2626 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2627 /* replace route list */
2628 rrt->rrt_next = orrt->rrt_next;
2630 trace(1, "route: %s/%d flags %s: replace new route\n",
2631 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2635 rrt->rrt_next = riprt;
2641 addroute(rrt, gw, ifcp)
2643 const struct in6_addr *gw;
2646 struct netinfo6 *np;
2647 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2648 struct rt_msghdr *rtm;
2649 struct sockaddr_in6 *sin;
2652 np = &rrt->rrt_info;
2653 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2654 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2655 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2656 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2657 np->rip6_metric - 1, buf2);
2659 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2660 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2661 np->rip6_metric - 1, buf2);
2665 memset(buf, 0, sizeof(buf));
2666 rtm = (struct rt_msghdr *)buf;
2667 rtm->rtm_type = RTM_ADD;
2668 rtm->rtm_version = RTM_VERSION;
2669 rtm->rtm_seq = ++seq;
2671 rtm->rtm_flags = rrt->rrt_flags;
2672 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2673 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2674 rtm->rtm_inits = RTV_HOPCOUNT;
2675 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2677 sin->sin6_len = sizeof(struct sockaddr_in6);
2678 sin->sin6_family = AF_INET6;
2679 sin->sin6_addr = np->rip6_dest;
2680 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2682 sin->sin6_len = sizeof(struct sockaddr_in6);
2683 sin->sin6_family = AF_INET6;
2684 sin->sin6_addr = *gw;
2685 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2687 sin->sin6_len = sizeof(struct sockaddr_in6);
2688 sin->sin6_family = AF_INET6;
2689 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2690 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2692 len = (char *)sin - (char *)buf;
2693 rtm->rtm_msglen = len;
2694 if (write(rtsock, buf, len) > 0)
2697 if (errno == EEXIST) {
2698 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2699 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2701 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2702 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2704 trace(0, "Can not write to rtsock (addroute): %s\n",
2707 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2715 struct netinfo6 *np;
2716 struct in6_addr *gw;
2718 u_char buf[BUFSIZ], buf2[BUFSIZ];
2719 struct rt_msghdr *rtm;
2720 struct sockaddr_in6 *sin;
2723 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2724 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2725 np->rip6_plen, buf2);
2727 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2728 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2732 memset(buf, 0, sizeof(buf));
2733 rtm = (struct rt_msghdr *)buf;
2734 rtm->rtm_type = RTM_DELETE;
2735 rtm->rtm_version = RTM_VERSION;
2736 rtm->rtm_seq = ++seq;
2738 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2739 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2740 rtm->rtm_flags |= RTF_HOST;
2741 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2742 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2744 sin->sin6_len = sizeof(struct sockaddr_in6);
2745 sin->sin6_family = AF_INET6;
2746 sin->sin6_addr = np->rip6_dest;
2747 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2749 sin->sin6_len = sizeof(struct sockaddr_in6);
2750 sin->sin6_family = AF_INET6;
2751 sin->sin6_addr = *gw;
2752 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2754 sin->sin6_len = sizeof(struct sockaddr_in6);
2755 sin->sin6_family = AF_INET6;
2756 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2757 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2759 len = (char *)sin - (char *)buf;
2760 rtm->rtm_msglen = len;
2761 if (write(rtsock, buf, len) >= 0)
2764 if (errno == ESRCH) {
2765 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2766 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2768 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2769 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2771 trace(0, "Can not write to rtsock (delroute): %s\n",
2774 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2782 struct netinfo6 *np;
2783 struct in6_addr *gw;
2788 struct rt_msghdr *rtm;
2789 struct sockaddr_in6 *sin;
2791 rtm = (struct rt_msghdr *)buf;
2792 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2793 memset(rtm, 0, len);
2794 rtm->rtm_type = RTM_GET;
2795 rtm->rtm_version = RTM_VERSION;
2797 rtm->rtm_seq = myseq;
2798 rtm->rtm_addrs = RTA_DST;
2799 rtm->rtm_msglen = len;
2800 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2801 sin->sin6_len = sizeof(struct sockaddr_in6);
2802 sin->sin6_family = AF_INET6;
2803 sin->sin6_addr = np->rip6_dest;
2804 if (write(rtsock, buf, len) < 0) {
2805 if (errno == ESRCH) /* No such route found */
2807 perror("write to rtsock");
2811 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2812 perror("read from rtsock");
2815 rtm = (struct rt_msghdr *)buf;
2816 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2817 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2818 if (rtm->rtm_addrs & RTA_DST) {
2819 sin = (struct sockaddr_in6 *)
2820 ((char *)sin + ROUNDUP(sin->sin6_len));
2822 if (rtm->rtm_addrs & RTA_GATEWAY) {
2823 *gw = sin->sin6_addr;
2831 const struct in6_addr *p;
2833 static char buf[BUFSIZ];
2835 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
2858 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2861 fprintf(dump, "%s: Interface Table Dump\n", hms());
2862 fprintf(dump, " Number of interfaces: %d\n", nifc);
2863 for (i = 0; i < 2; i++) {
2864 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
2865 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
2867 if ((ifcp->ifc_flags & IFF_UP) == 0)
2869 if (iff_find(ifcp, 'N') != NULL)
2872 if (ifcp->ifc_flags & IFF_UP)
2875 ifdump0(dump, ifcp);
2878 fprintf(dump, "\n");
2886 const struct ifc *ifcp;
2894 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2895 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
2896 inet6_n2p(&ifcp->ifc_mylladdr),
2897 ifcp->ifc_mtu, ifcp->ifc_metric);
2898 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2899 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2900 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
2902 fprintf(dump, "\t%s/%d -- %s\n",
2903 inet6_n2p(&ifa->ifa_addr),
2904 ifa->ifa_plen, buf);
2906 fprintf(dump, "\t%s/%d\n",
2907 inet6_n2p(&ifa->ifa_addr),
2911 if (ifcp->ifc_filter) {
2912 fprintf(dump, "\tFilter:");
2913 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
2915 switch (iffp->iff_type) {
2917 ft = "Aggregate"; addr++; break;
2919 ft = "No-use"; break;
2921 ft = "Advertise-only"; addr++; break;
2923 ft = "Default-only"; break;
2925 ft = "Listen-only"; addr++; break;
2927 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
2932 fprintf(dump, " %s", ft);
2934 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
2938 fprintf(dump, "\n");
2954 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2958 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
2959 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
2960 if (rrt->rrt_t == 0)
2963 age = t - rrt->rrt_t;
2964 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
2966 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2967 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
2968 index2ifc[rrt->rrt_index]->ifc_name,
2969 inet6_n2p(&rrt->rrt_gw),
2970 rrt->rrt_info.rip6_metric, (long)age);
2971 if (rrt->rrt_info.rip6_tag) {
2972 fprintf(dump, " tag(0x%04x)",
2973 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
2975 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
2976 fprintf(dump, " NOT-LL");
2977 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
2978 fprintf(dump, " NO-ADV");
2979 fprintf(dump, "\n");
2981 fprintf(dump, "\n");
2987 * Parse the -A (and -O) options and put corresponding filter object to the
2988 * specified interface structures. Each of the -A/O option has the following
2989 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2990 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2996 char *p, *ap, *iflp, *ifname;
2997 struct iff ftmp, *iff_obj;
3004 for (i = 0; i < nfilter; i++) {
3008 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3012 if ((p = index(ap, ',')) != NULL) {
3016 if ((p = index(ap, '/')) == NULL) {
3017 fatal("no prefixlen specified for '%s'", ap);
3021 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
3022 fatal("invalid prefix specified for '%s'", ap);
3025 ftmp.iff_plen = atoi(p);
3026 ftmp.iff_next = NULL;
3027 applyplen(&ftmp.iff_addr, ftmp.iff_plen);
3029 ftmp.iff_type = filtertype[i];
3030 if (iflp == NULL || *iflp == '\0') {
3031 fatal("no interface specified for '%s'", ap);
3034 /* parse the interface listing portion */
3037 if ((iflp = index(iflp, ',')) != NULL)
3039 ifcp = ifc_find(ifname);
3041 fatal("no interface %s exists", ifname);
3044 iff_obj = (struct iff *)malloc(sizeof(struct iff));
3045 if (iff_obj == NULL) {
3046 fatal("malloc of iff_obj");
3049 memcpy((void *)iff_obj, (void *)&ftmp,
3050 sizeof(struct iff));
3051 /* link it to the interface filter */
3052 iff_obj->iff_next = ifcp->ifc_filter;
3053 ifcp->ifc_filter = iff_obj;
3057 * -A: aggregate configuration.
3059 if (filtertype[i] != 'A')
3061 /* put the aggregate to the kernel routing table */
3062 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3064 fatal("malloc: rrt");
3067 memset(rrt, 0, sizeof(struct riprt));
3068 rrt->rrt_info.rip6_dest = ftmp.iff_addr;
3069 rrt->rrt_info.rip6_plen = ftmp.iff_plen;
3070 rrt->rrt_info.rip6_metric = 1;
3071 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3072 rrt->rrt_gw = in6addr_loopback;
3073 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3074 rrt->rrt_rflags = RRTF_AGGREGATE;
3076 rrt->rrt_index = loopifindex;
3078 if (getroute(&rrt->rrt_info, &gw)) {
3081 * When the address has already been registered in the
3082 * kernel routing table, it should be removed
3084 delroute(&rrt->rrt_info, &gw);
3086 /* it is safer behavior */
3088 fatal("%s/%u already in routing table, "
3090 inet6_n2p(&rrt->rrt_info.rip6_dest),
3091 rrt->rrt_info.rip6_plen);
3096 /* Put the route to the list */
3097 rrt->rrt_next = riprt;
3099 trace(1, "Aggregate: %s/%d for %s\n",
3100 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
3102 /* Add this route to the kernel */
3103 if (nflag) /* do not modify kernel routing table */
3105 addroute(rrt, &in6addr_loopback, loopifcp);
3109 /***************** utility functions *****************/
3112 * Returns a pointer to ifac whose address and prefix length matches
3113 * with the address and prefix length specified in the arguments.
3116 ifa_match(ifcp, ia, plen)
3117 const struct ifc *ifcp;
3118 const struct in6_addr *ia;
3123 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
3124 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
3125 ifa->ifa_plen == plen)
3132 * Return a pointer to riprt structure whose address and prefix length
3133 * matches with the address and prefix length found in the argument.
3134 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3137 rtsearch(np, prev_rrt)
3138 struct netinfo6 *np;
3139 struct riprt **prev_rrt;
3145 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
3146 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3147 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3160 const struct sockaddr_in6 *sin6;
3163 return mask2len(&sin6->sin6_addr,
3164 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3168 mask2len(addr, lenlim)
3169 const struct in6_addr *addr;
3173 const u_char *p = (const u_char *)addr;
3175 for (j = 0; j < lenlim; j++, p++) {
3182 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3183 MASKLEN(0xfe, 7); break;
3184 MASKLEN(0xfc, 6); break;
3185 MASKLEN(0xf8, 5); break;
3186 MASKLEN(0xf0, 4); break;
3187 MASKLEN(0xe0, 3); break;
3188 MASKLEN(0xc0, 2); break;
3189 MASKLEN(0x80, 1); break;
3197 applymask(addr, mask)
3198 struct in6_addr *addr, *mask;
3203 p = (u_long *)addr; q = (u_long *)mask;
3204 for (i = 0; i < 4; i++)
3208 static const u_char plent[8] = {
3209 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3214 struct in6_addr *ia;
3221 for (i = 0; i < 16; i++) {
3230 static const int pl2m[9] = {
3231 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3238 static struct in6_addr ia;
3242 memset(&ia, 0, sizeof(struct in6_addr));
3244 for (i = 0; i < 16; i++, p++, n -= 8) {
3259 char *q = (char *)malloc(strlen(p) + 1);
3268 static char buf[BUFSIZ];
3273 if ((tm = localtime(&t)) == 0) {
3277 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3282 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3290 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3291 nextalarm = time(NULL) + interval;
3301 t = (int)(RIP_TRIG_INT6_MIN +
3302 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3303 sup_trig_update = time(NULL) + t;
3309 fatal(const char *fmt, ...)
3311 fatal(fmt, va_alist)
3324 vsnprintf(buf, sizeof(buf), fmt, ap);
3326 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3333 tracet(int level, const char *fmt, ...)
3335 tracet(level, fmt, va_alist)
3348 if (level <= dflag) {
3349 fprintf(stderr, "%s: ", hms());
3350 vfprintf(stderr, fmt, ap);
3354 vsyslog(LOG_DEBUG, fmt, ap);
3356 vsyslog(LOG_WARNING, fmt, ap);
3363 trace(int level, const char *fmt, ...)
3365 trace(level, fmt, va_alist)
3379 vfprintf(stderr, fmt, ap);
3382 vsyslog(LOG_DEBUG, fmt, ap);
3384 vsyslog(LOG_WARNING, fmt, ap);
3392 struct if_nameindex *p, *p0;
3393 unsigned int max = 0;
3395 p0 = if_nameindex();
3396 for (p = p0; p && p->if_index && p->if_name; p++) {
3397 if (max < p->if_index)
3400 if_freenameindex(p0);
3410 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
3411 if (strcmp(name, ifcp->ifc_name) == 0)
3414 return (struct ifc *)NULL;
3418 iff_find(ifcp, type)
3424 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
3425 if (iffp->iff_type == type)
3432 setindex2ifc(idx, ifcp)
3440 nindex2ifc = 5; /*initial guess*/
3441 index2ifc = (struct ifc **)
3442 malloc(sizeof(*index2ifc) * nindex2ifc);
3443 if (index2ifc == NULL) {
3447 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3450 while (nindex2ifc <= idx)
3452 if (n != nindex2ifc) {
3453 p = (struct ifc **)realloc(index2ifc,
3454 sizeof(*index2ifc) * nindex2ifc);
3459 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3462 index2ifc[idx] = ifcp;