1 /* $FreeBSD: src/usr.sbin/route6d/route6d.c,v 1.2.2.5 2001/07/03 11:02:09 ume Exp $ */
2 /* $DragonFly: src/usr.sbin/route6d/route6d.c,v 1.7 2004/12/18 22:48:05 swildner Exp $ */
3 /* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $
36 #define _KERNEL_STRUCTURES
55 #include <sys/types.h>
56 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/sysctl.h>
63 #if defined(__DragonFly__)
64 #include <net/if_var.h>
65 #endif /* __DragonFly__ */
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet/udp.h>
74 #include <arpa/inet.h>
81 #define INIT_INTERVAL6 6
83 #define INIT_INTERVAL6 10 /* Wait to submit a initial riprequest */
86 /* alignment constraint for routing socket */
88 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
89 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
92 * Following two macros are highly depending on KAME Release
94 #define IN6_LINKLOCAL_IFINDEX(addr) \
95 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
97 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
99 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
100 (addr).s6_addr[3] = (index) & 0xff; \
103 struct ifc { /* Configuration of an interface */
104 char *ifc_name; /* if name */
105 struct ifc *ifc_next;
106 int ifc_index; /* if index */
107 int ifc_mtu; /* if mtu */
108 int ifc_metric; /* if metric */
109 u_int ifc_flags; /* flags */
110 short ifc_cflags; /* IFC_XXX */
111 struct in6_addr ifc_mylladdr; /* my link-local address */
112 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
113 struct iff *ifc_filter; /* filter structure */
114 struct ifac *ifc_addr; /* list of AF_INET6 addresses */
115 int ifc_joined; /* joined to ff02::9 */
118 struct ifac { /* Adddress associated to an interface */
119 struct ifc *ifa_conf; /* back pointer */
120 struct ifac *ifa_next;
121 struct in6_addr ifa_addr; /* address */
122 struct in6_addr ifa_raddr; /* remote address, valid in p2p */
123 int ifa_plen; /* prefix length */
128 struct in6_addr iff_addr;
130 struct iff *iff_next;
134 int nifc; /* number of valid ifc's */
135 struct ifc **index2ifc;
137 struct ifc *loopifcp = NULL; /* pointing to loopback */
138 int loopifindex = 0; /* ditto */
139 fd_set sockvec; /* vector to select() for receiving */
140 int rtsock; /* the routing socket */
141 int ripsock; /* socket to send/receive RIP datagram */
143 struct rip6 *ripbuf; /* packet buffer for sending */
146 * Maintain the routes in a linked list. When the number of the routes
147 * grows, somebody would like to introduce a hash based or a radix tree
148 * based structure. I believe the number of routes handled by RIP is
149 * limited and I don't have to manage a complex data structure, however.
151 * One of the major drawbacks of the linear linked list is the difficulty
152 * of representing the relationship between a couple of routes. This may
153 * be a significant problem when we have to support route aggregation with
154 * supressing the specifices covered by the aggregate.
158 struct riprt *rrt_next; /* next destination */
159 struct riprt *rrt_same; /* same destination - future use */
160 struct netinfo6 rrt_info; /* network info */
161 struct in6_addr rrt_gw; /* gateway */
162 u_long rrt_flags; /* kernel routing table flags */
163 u_long rrt_rflags; /* route6d routing table flags */
164 time_t rrt_t; /* when the route validated */
165 int rrt_index; /* ifindex from which this route got */
168 struct riprt *riprt = 0;
170 int dflag = 0; /* debug flag */
171 int qflag = 0; /* quiet flag */
172 int nflag = 0; /* don't update kernel routing table */
173 int aflag = 0; /* age out even the statically defined routes */
174 int hflag = 0; /* don't split horizon */
175 int lflag = 0; /* exchange site local routes */
176 int sflag = 0; /* announce static routes w/ split horizon */
177 int Sflag = 0; /* announce static routes to every interface */
178 unsigned long routetag = 0; /* route tag attached on originating case */
180 char *filter[MAXFILTER];
181 int filtertype[MAXFILTER];
186 struct sockaddr_storage ripsin;
188 struct rtentry rtentry;
191 time_t nextalarm = 0;
192 time_t sup_trig_update = 0;
198 static u_long seq = 0;
201 volatile sig_atomic_t seenalrm;
202 volatile sig_atomic_t seenquit;
203 volatile sig_atomic_t seenusr1;
205 #define RRTF_AGGREGATE 0x08000000
206 #define RRTF_NOADVERTISE 0x10000000
207 #define RRTF_NH_NOT_LLADDR 0x20000000
208 #define RRTF_SENDANYWAY 0x40000000
209 #define RRTF_CHANGED 0x80000000
211 int main(int, char **);
212 void sighandler(int);
215 void ripsend(struct ifc *, struct sockaddr_in6 *, int);
216 int out_filter(struct riprt *, struct ifc *);
218 void sockopt(struct ifc *);
220 void ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
222 int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
223 const struct sockaddr_in6 *);
224 int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
225 const struct sockaddr_in6 *);
226 void filterconfig(void);
228 const char *rttypes(struct rt_msghdr *);
229 const char *rtflags(struct rt_msghdr *);
230 const char *ifflags(int);
231 int ifrt(struct ifc *, int);
232 void ifrt_p2p(struct ifc *, int);
233 void applymask(struct in6_addr *, struct in6_addr *);
234 void applyplen(struct in6_addr *, int);
237 void ifdump0(FILE *, const struct ifc *);
239 void rt_entry(struct rt_msghdr *, int);
241 void riprequest(struct ifc *, struct netinfo6 *, int,
242 struct sockaddr_in6 *);
243 void ripflush(struct ifc *, struct sockaddr_in6 *);
244 void sendrequest(struct ifc *);
245 int sin6mask2len(const struct sockaddr_in6 *);
246 int mask2len(const struct in6_addr *, int);
247 int sendpacket(struct sockaddr_in6 *, int);
248 int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
249 int delroute(struct netinfo6 *, struct in6_addr *);
250 struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
252 int tobeadv(struct riprt *, struct ifc *);
253 char *allocopy(char *);
255 const char *inet6_n2p(const struct in6_addr *);
256 struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
257 struct in6_addr *plen2mask(int);
258 struct riprt *rtsearch(struct netinfo6 *, struct riprt **);
259 int ripinterval(int);
260 time_t ripsuptrig(void);
261 void fatal(const char *, ...)
262 __attribute__((__format__(__printf__, 1, 2)));
263 void trace(int, const char *, ...)
264 __attribute__((__format__(__printf__, 2, 3)));
265 void tracet(int, const char *, ...)
266 __attribute__((__format__(__printf__, 2, 3)));
267 unsigned int if_maxindex(void);
268 struct ifc *ifc_find(char *);
269 struct iff *iff_find(struct ifc *, int);
270 void setindex2ifc(int, struct ifc *);
272 #define MALLOC(type) ((type *)malloc(sizeof(type)))
282 sigset_t mask, omask;
287 progname = strrchr(*argv, '/');
294 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
301 if (nfilter >= MAXFILTER) {
302 fatal("Exceeds MAXFILTER");
305 filtertype[nfilter] = ch;
306 filter[nfilter++] = allocopy(optarg);
310 routetag = strtoul(optarg, &ep, 0);
311 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
312 fatal("invalid route tag");
317 if ((rtlog = fopen(optarg, "w")) == NULL) {
318 fatal("Can not write to routelog");
322 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
323 FLAG('a', aflag, 1); break;
324 FLAG('d', dflag, 1); break;
325 FLAG('D', dflag, 2); break;
326 FLAG('h', hflag, 1); break;
327 FLAG('l', lflag, 1); break;
328 FLAG('n', nflag, 1); break;
329 FLAG('q', qflag, 1); break;
330 FLAG('s', sflag, 1); break;
331 FLAG('S', Sflag, 1); break;
334 fatal("Invalid option specified, terminating");
341 fatal("bogus extra arguments");
347 fprintf(stderr, "No kernel update is allowed\n");
349 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
352 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
354 memset(ripbuf, 0, RIP6_MAXMTU);
355 ripbuf->rip6_cmd = RIP6_RESPONSE;
356 ripbuf->rip6_vers = RIP6_VERSION;
357 ripbuf->rip6_res1[0] = 0;
358 ripbuf->rip6_res1[1] = 0;
362 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
363 if (ifcp->ifc_index < 0) {
365 "No ifindex found at %s (no link-local address?)\n",
372 if (loopifcp == NULL) {
373 fatal("No loopback found");
376 loopifindex = loopifcp->ifc_index;
377 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
386 if (daemon(0, 0) < 0) {
400 if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
401 fprintf(pidfile, "%d\n", pid);
405 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
409 memset(ripbuf, 0, RIP6_MAXMTU);
410 ripbuf->rip6_cmd = RIP6_RESPONSE;
411 ripbuf->rip6_vers = RIP6_VERSION;
412 ripbuf->rip6_res1[0] = 0;
413 ripbuf->rip6_res1[1] = 0;
415 if (signal(SIGALRM, sighandler) == SIG_ERR ||
416 signal(SIGQUIT, sighandler) == SIG_ERR ||
417 signal(SIGTERM, sighandler) == SIG_ERR ||
418 signal(SIGUSR1, sighandler) == SIG_ERR ||
419 signal(SIGHUP, sighandler) == SIG_ERR ||
420 signal(SIGINT, sighandler) == SIG_ERR) {
425 * To avoid rip packet congestion (not on a cable but in this
426 * process), wait for a moment to send the first RIP6_RESPONSE
429 alarm(ripinterval(INIT_INTERVAL6));
431 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
432 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
436 syslog(LOG_INFO, "**** Started ****");
438 sigaddset(&mask, SIGALRM);
458 FD_COPY(&sockvec, &recvec);
460 switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
462 if (errno != EINTR) {
470 if (FD_ISSET(ripsock, &recvec)) {
471 sigprocmask(SIG_BLOCK, &mask, &omask);
473 sigprocmask(SIG_SETMASK, &omask, NULL);
475 if (FD_ISSET(rtsock, &recvec)) {
476 sigprocmask(SIG_BLOCK, &mask, &omask);
478 sigprocmask(SIG_SETMASK, &omask, NULL);
507 * gracefully exits after resetting sockopts.
516 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
517 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
518 delroute(&rrt->rrt_info, &rrt->rrt_gw);
523 syslog(LOG_INFO, "**** Terminated ****");
529 * Called periodically:
530 * 1. age out the learned route. remove it if necessary.
531 * 2. submit RIP6_RESPONSE packets.
532 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
533 * to invoke this function in every 1 or 5 or 10 seconds only to age the
534 * routes more precisely.
541 struct riprt *rrt, *rrt_prev, *rrt_next;
542 time_t t_lifetime, t_holddown;
544 /* age the RIP routes */
546 t_lifetime = time(NULL) - RIP_LIFETIME;
547 t_holddown = t_lifetime - RIP_HOLDDOWN;
548 for (rrt = riprt; rrt; rrt = rrt_next) {
549 rrt_next = rrt->rrt_next;
551 if (rrt->rrt_t == 0) {
555 if (rrt->rrt_t < t_holddown) {
557 rrt_prev->rrt_next = rrt->rrt_next;
559 riprt = rrt->rrt_next;
561 delroute(&rrt->rrt_info, &rrt->rrt_gw);
565 if (rrt->rrt_t < t_lifetime)
566 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
570 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
571 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
572 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
574 alarm(ripinterval(SUPPLY_INTERVAL6));
580 int i, int0, int255, error;
581 struct addrinfo hints, *res;
584 ifc = (struct ifc *)NULL;
586 nindex2ifc = 0; /*initial guess*/
588 snprintf(port, sizeof(port), "%d", RIP6_PORT);
590 memset(&hints, 0, sizeof(hints));
591 hints.ai_family = PF_INET6;
592 hints.ai_socktype = SOCK_DGRAM;
593 hints.ai_flags = AI_PASSIVE;
594 error = getaddrinfo(NULL, port, &hints, &res);
596 fatal("%s", gai_strerror(error));
600 fatal(":: resolved to multiple address");
604 int0 = 0; int255 = 255;
605 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
610 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
614 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
615 &int255, sizeof(int255)) < 0) {
616 fatal("rip IPV6_MULTICAST_HOPS");
619 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
620 &int0, sizeof(int0)) < 0) {
621 fatal("rip IPV6_MULTICAST_LOOP");
626 #ifdef IPV6_RECVPKTINFO
627 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
629 fatal("rip IPV6_RECVPKTINFO");
632 #else /* old adv. API */
633 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
635 fatal("rip IPV6_PKTINFO");
640 memset(&hints, 0, sizeof(hints));
641 hints.ai_family = PF_INET6;
642 hints.ai_socktype = SOCK_DGRAM;
643 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
645 fatal("%s", gai_strerror(error));
649 fatal("%s resolved to multiple address", RIP6_DEST);
652 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
657 memset(&sockvec, 0, sizeof(sockvec));
659 FD_SET(ripsock, &sockvec);
662 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
663 fatal("route socket");
666 FD_SET(rtsock, &sockvec);
668 rtsock = -1; /*just for safety */
672 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
675 * ripflush flushes the rip datagram stored in the rip buffer
678 static struct netinfo6 *np;
683 struct sockaddr_in6 *sin;
689 tracet(1, "Send(%s): info(%d) to %s.%d\n",
691 inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
693 tracet(1, "Send: info(%d) to %s.%d\n",
694 nrt, inet6_n2p(&sin->sin6_addr), ntohs(sin->sin6_port));
696 np = ripbuf->rip6_nets;
697 for (i = 0; i < nrt; i++, np++) {
698 if (np->rip6_metric == NEXTHOP_METRIC) {
699 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
700 trace(2, " NextHop reset");
702 trace(2, " NextHop %s",
703 inet6_n2p(&np->rip6_dest));
706 trace(2, " %s/%d[%d]",
707 inet6_n2p(&np->rip6_dest),
708 np->rip6_plen, np->rip6_metric);
711 trace(2, " tag=0x%04x",
712 ntohs(np->rip6_tag) & 0xffff);
717 error = sendpacket(sin, RIPSIZE(nrt));
718 if (error == EAFNOSUPPORT) {
719 /* Protocol not supported */
720 tracet(1, "Could not send info to %s (%s): "
722 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
723 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
725 nrt = 0; np = ripbuf->rip6_nets;
729 * Generate RIP6_RESPONSE packets and send them.
732 ripsend(ifcp, sin, flag)
734 struct sockaddr_in6 *sin;
738 struct in6_addr *nh; /* next hop */
743 * Request from non-link local address is not
744 * a regular route6d update.
746 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
747 sizeof(struct udphdr) -
748 sizeof(struct rip6) + sizeof(struct netinfo6)) /
749 sizeof(struct netinfo6);
750 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
751 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
752 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
754 /* Put the route to the buffer */
762 if (nrt) /* Send last packet */
767 if ((flag & RRTF_SENDANYWAY) == 0 &&
768 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
772 if (iff_find(ifcp, 'N') != NULL)
775 /* -T: generate default route only */
776 if (iff_find(ifcp, 'T') != NULL) {
777 struct netinfo6 rrt_info;
778 memset(&rrt_info, 0, sizeof(struct netinfo6));
779 rrt_info.rip6_dest = in6addr_any;
780 rrt_info.rip6_plen = 0;
781 rrt_info.rip6_metric = 1;
782 rrt_info.rip6_metric += ifcp->ifc_metric;
783 rrt_info.rip6_tag = htons(routetag & 0xffff);
784 np = ripbuf->rip6_nets;
791 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
792 sizeof(struct udphdr) -
793 sizeof(struct rip6) + sizeof(struct netinfo6)) /
794 sizeof(struct netinfo6);
796 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
797 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
798 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
801 /* Need to check filter here */
802 if (out_filter(rrt, ifcp) == 0)
805 /* Check split horizon and other conditions */
806 if (tobeadv(rrt, ifcp) == 0)
809 /* Only considers the routes with flag if specified */
810 if ((flag & RRTF_CHANGED) &&
811 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
815 if (rrt->rrt_index == ifcp->ifc_index &&
816 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
817 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
818 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
819 if (nrt == maxrte - 2)
821 np->rip6_dest = rrt->rrt_gw;
822 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
823 SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
826 np->rip6_metric = NEXTHOP_METRIC;
830 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
831 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
832 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
834 if (nrt == maxrte - 2)
836 memset(np, 0, sizeof(struct netinfo6));
837 np->rip6_metric = NEXTHOP_METRIC;
842 /* Put the route to the buffer */
850 if (nrt) /* Send last packet */
855 * outbound filter logic, per-route/interface.
858 out_filter(rrt, ifcp)
867 * -A: filter out less specific routes, if we have aggregated
870 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
871 if (iffp->iff_type != 'A')
873 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
875 ia = rrt->rrt_info.rip6_dest;
876 applyplen(&ia, iffp->iff_plen);
877 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
882 * if it is an aggregated route, advertise it only to the
883 * interfaces specified on -A.
885 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
887 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
888 if (iffp->iff_type != 'A')
890 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
891 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
902 * -O: advertise only if prefix matches the configured prefix.
904 if (iff_find(ifcp, 'O')) {
906 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
907 if (iffp->iff_type != 'O')
909 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
911 ia = rrt->rrt_info.rip6_dest;
912 applyplen(&ia, iffp->iff_plen);
913 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
922 /* the prefix should be advertised */
927 * Determine if the route is to be advertised on the specified interface.
928 * It checks options specified in the arguments and the split horizon rule.
936 /* Special care for static routes */
937 if (rrt->rrt_flags & RTF_STATIC) {
938 /* XXX don't advertise reject/blackhole routes */
939 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
942 if (Sflag) /* Yes, advertise it anyway */
944 if (sflag && rrt->rrt_index != ifcp->ifc_index)
948 /* Regular split horizon */
949 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
955 * Send a rip packet actually.
959 struct sockaddr_in6 *sin;
963 * MSG_DONTROUTE should not be specified when it responds with a
964 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
971 struct in6_pktinfo *pi;
973 struct sockaddr_in6 sincopy;
975 /* do not overwrite the given sin */
979 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)
980 || IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) {
981 idx = IN6_LINKLOCAL_IFINDEX(sin->sin6_addr);
982 SET_IN6_LINKLOCAL_IFINDEX(sin->sin6_addr, 0);
986 m.msg_name = (caddr_t)sin;
987 m.msg_namelen = sizeof(*sin);
988 iov[0].iov_base = (caddr_t)ripbuf;
989 iov[0].iov_len = len;
993 m.msg_control = NULL;
994 m.msg_controllen = 0;
996 memset(cmsgbuf, 0, sizeof(cmsgbuf));
997 cm = (struct cmsghdr *)cmsgbuf;
998 m.msg_control = (caddr_t)cm;
999 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1001 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1002 cm->cmsg_level = IPPROTO_IPV6;
1003 cm->cmsg_type = IPV6_PKTINFO;
1004 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1005 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1006 pi->ipi6_ifindex = idx;
1009 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1010 trace(1, "sendmsg: %s\n", strerror(errno));
1018 * Receive and process RIP packets. Update the routes/kernel forwarding
1019 * table if necessary.
1024 struct ifc *ifcp, *ic;
1025 struct sockaddr_in6 fsock;
1026 struct in6_addr nh; /* next hop */
1028 struct netinfo6 *np, *nq;
1030 int len, nn, need_trigger, idx;
1031 char buf[4 * RIP6_MAXMTU];
1035 struct iovec iov[2];
1036 u_char cmsgbuf[256];
1037 struct in6_pktinfo *pi;
1041 time_t t_half_lifetime;
1045 m.msg_name = (caddr_t)&fsock;
1046 m.msg_namelen = sizeof(fsock);
1047 iov[0].iov_base = (caddr_t)buf;
1048 iov[0].iov_len = sizeof(buf);
1051 cm = (struct cmsghdr *)cmsgbuf;
1052 m.msg_control = (caddr_t)cm;
1053 m.msg_controllen = sizeof(cmsgbuf);
1054 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1059 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1061 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1062 if (cm->cmsg_level == IPPROTO_IPV6 &&
1063 cm->cmsg_type == IPV6_PKTINFO) {
1064 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
1065 idx = pi->ipi6_ifindex;
1069 if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
1070 SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
1072 nh = fsock.sin6_addr;
1073 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1074 sizeof(struct netinfo6);
1075 rp = (struct rip6 *)buf;
1078 if (rp->rip6_vers != RIP6_VERSION) {
1079 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1082 if (rp->rip6_cmd == RIP6_REQUEST) {
1083 if (idx && idx < nindex2ifc) {
1084 ifcp = index2ifc[idx];
1085 riprequest(ifcp, np, nn, &fsock);
1087 riprequest(NULL, np, nn, &fsock);
1092 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1093 trace(1, "Packets from non-ll addr: %s\n",
1094 inet6_n2p(&fsock.sin6_addr));
1095 return; /* Ignore packets from non-link-local addr */
1097 idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
1098 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1100 trace(1, "Packets to unknown interface index %d\n", idx);
1101 return; /* Ignore it */
1103 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1104 return; /* The packet is from me; ignore */
1105 if (rp->rip6_cmd != RIP6_RESPONSE) {
1106 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1111 if (iff_find(ifcp, 'N') != NULL)
1114 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1115 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1118 t_half_lifetime = t - (RIP_LIFETIME/2);
1119 for (; nn; nn--, np++) {
1120 if (np->rip6_metric == NEXTHOP_METRIC) {
1121 /* modify neighbor address */
1122 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1124 SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
1125 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1126 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1127 nh = fsock.sin6_addr;
1128 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1130 nh = fsock.sin6_addr;
1131 trace(1, "\tInvalid Nexthop: %s\n",
1132 inet6_n2p(&np->rip6_dest));
1136 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1137 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1138 inet6_n2p(&np->rip6_dest),
1139 np->rip6_plen, np->rip6_metric);
1142 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1143 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1144 inet6_n2p(&np->rip6_dest),
1145 np->rip6_plen, np->rip6_metric);
1148 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1149 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1150 inet6_n2p(&np->rip6_dest),
1151 np->rip6_plen, np->rip6_metric);
1154 /* may need to pass sitelocal prefix in some case, however*/
1155 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1156 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1157 inet6_n2p(&np->rip6_dest),
1158 np->rip6_plen, np->rip6_metric);
1161 trace(2, "\tnetinfo6: %s/%d [%d]",
1162 inet6_n2p(&np->rip6_dest),
1163 np->rip6_plen, np->rip6_metric);
1165 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1168 applyplen(&ia, np->rip6_plen);
1169 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1170 trace(2, " [junk outside prefix]");
1174 * -L: listen only if the prefix matches the configuration
1176 ok = 1; /* if there's no L filter, it is ok */
1177 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
1178 if (iffp->iff_type != 'L')
1181 if (np->rip6_plen < iffp->iff_plen)
1183 /* special rule: ::/0 means default, not "in /0" */
1184 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1187 applyplen(&ia, iffp->iff_plen);
1188 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1194 trace(2, " (filtered)\n");
1200 np->rip6_metric += ifcp->ifc_metric;
1201 if (np->rip6_metric > HOPCNT_INFINITY6)
1202 np->rip6_metric = HOPCNT_INFINITY6;
1204 applyplen(&np->rip6_dest, np->rip6_plen);
1205 if ((rrt = rtsearch(np, NULL)) != NULL) {
1206 if (rrt->rrt_t == 0)
1207 continue; /* Intf route has priority */
1208 nq = &rrt->rrt_info;
1209 if (nq->rip6_metric > np->rip6_metric) {
1210 if (rrt->rrt_index == ifcp->ifc_index &&
1211 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1212 /* Small metric from the same gateway */
1213 nq->rip6_metric = np->rip6_metric;
1215 /* Better route found */
1216 rrt->rrt_index = ifcp->ifc_index;
1217 /* Update routing table */
1218 delroute(nq, &rrt->rrt_gw);
1221 addroute(rrt, &nh, ifcp);
1223 rrt->rrt_rflags |= RRTF_CHANGED;
1226 } else if (nq->rip6_metric < np->rip6_metric &&
1227 rrt->rrt_index == ifcp->ifc_index &&
1228 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1229 /* Got worse route from same gw */
1230 nq->rip6_metric = np->rip6_metric;
1232 rrt->rrt_rflags |= RRTF_CHANGED;
1234 } else if (nq->rip6_metric == np->rip6_metric &&
1235 np->rip6_metric < HOPCNT_INFINITY6) {
1236 if (rrt->rrt_index == ifcp->ifc_index &&
1237 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1238 /* same metric, same route from same gw */
1240 } else if (rrt->rrt_t < t_half_lifetime) {
1241 /* Better route found */
1242 rrt->rrt_index = ifcp->ifc_index;
1243 /* Update routing table */
1244 delroute(nq, &rrt->rrt_gw);
1247 addroute(rrt, &nh, ifcp);
1248 rrt->rrt_rflags |= RRTF_CHANGED;
1253 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1254 * do not update age value. Do nothing.
1256 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1257 /* Got a new valid route */
1258 if ((rrt = MALLOC(struct riprt)) == NULL) {
1259 fatal("malloc: struct riprt");
1262 memset(rrt, 0, sizeof(*rrt));
1263 nq = &rrt->rrt_info;
1265 rrt->rrt_same = NULL;
1266 rrt->rrt_index = ifcp->ifc_index;
1267 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1270 applyplen(&nq->rip6_dest, nq->rip6_plen);
1271 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1272 rrt->rrt_flags |= RTF_HOST;
1274 /* Put the route to the list */
1275 rrt->rrt_next = riprt;
1277 /* Update routing table */
1278 addroute(rrt, &nh, ifcp);
1279 rrt->rrt_rflags |= RRTF_CHANGED;
1284 /* XXX need to care the interval between triggered updates */
1286 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1287 for (ic = ifc; ic; ic = ic->ifc_next) {
1288 if (ifcp->ifc_index == ic->ifc_index)
1290 if (ic->ifc_flags & IFF_UP)
1291 ripsend(ic, &ic->ifc_ripsin,
1295 /* Reset the flag */
1296 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1297 rrt->rrt_rflags &= ~RRTF_CHANGED;
1302 * Send all routes request packet to the specified interface.
1308 struct netinfo6 *np;
1311 if (ifcp->ifc_flags & IFF_LOOPBACK)
1313 ripbuf->rip6_cmd = RIP6_REQUEST;
1314 np = ripbuf->rip6_nets;
1315 memset(np, 0, sizeof(struct netinfo6));
1316 np->rip6_metric = HOPCNT_INFINITY6;
1317 tracet(1, "Send rtdump Request to %s (%s)\n",
1318 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1319 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1320 if (error == EAFNOSUPPORT) {
1321 /* Protocol not supported */
1322 tracet(1, "Could not send rtdump Request to %s (%s): "
1323 "set IFF_UP to 0\n",
1324 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1325 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1327 ripbuf->rip6_cmd = RIP6_RESPONSE;
1331 * Process a RIP6_REQUEST packet.
1334 riprequest(ifcp, np, nn, sin)
1336 struct netinfo6 *np;
1338 struct sockaddr_in6 *sin;
1343 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1344 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1345 /* Specific response, don't split-horizon */
1346 trace(1, "\tRIP Request\n");
1347 for (i = 0; i < nn; i++, np++) {
1348 rrt = rtsearch(np, NULL);
1350 np->rip6_metric = rrt->rrt_info.rip6_metric;
1352 np->rip6_metric = HOPCNT_INFINITY6;
1354 sendpacket(sin, RIPSIZE(nn));
1357 /* Whole routing table dump */
1358 trace(1, "\tRIP Request -- whole routing table\n");
1359 ripsend(ifcp, sin, RRTF_SENDANYWAY);
1363 * Get information of each interface.
1368 struct ifaddrs *ifap, *ifa;
1370 struct ipv6_mreq mreq;
1373 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1378 if (getifaddrs(&ifap) != 0) {
1379 fatal("getifaddrs");
1383 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1384 if (ifa->ifa_addr->sa_family != AF_INET6)
1386 ifcp = ifc_find(ifa->ifa_name);
1387 /* we are interested in multicast-capable interfaces */
1388 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1392 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1393 fatal("malloc: struct ifc");
1396 memset(ifcp, 0, sizeof(*ifcp));
1397 ifcp->ifc_index = -1;
1398 ifcp->ifc_next = ifc;
1401 ifcp->ifc_name = allocopy(ifa->ifa_name);
1403 ifcp->ifc_filter = 0;
1404 ifcp->ifc_flags = ifa->ifa_flags;
1405 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1406 ifflags(ifcp->ifc_flags));
1407 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1410 /* update flag, this may be up again */
1411 if (ifcp->ifc_flags != ifa->ifa_flags) {
1412 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1413 ifflags(ifcp->ifc_flags));
1414 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1415 ifcp->ifc_cflags |= IFC_CHANGED;
1417 ifcp->ifc_flags = ifa->ifa_flags;
1419 ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
1420 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1421 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1422 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1423 mreq.ipv6mr_interface = ifcp->ifc_index;
1424 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1425 &mreq, sizeof(mreq)) < 0) {
1426 fatal("IPV6_JOIN_GROUP");
1429 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1438 ifconfig1(name, sa, ifcp, s)
1440 const struct sockaddr *sa;
1444 struct in6_ifreq ifr;
1445 const struct sockaddr_in6 *sin;
1450 sin = (const struct sockaddr_in6 *)sa;
1451 ifr.ifr_addr = *sin;
1452 strcpy(ifr.ifr_name, name);
1453 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1454 fatal("ioctl: SIOCGIFNETMASK_IN6");
1457 plen = sin6mask2len(&ifr.ifr_addr);
1458 if ((ifa = ifa_match(ifcp, &sin->sin6_addr, plen)) != NULL) {
1459 /* same interface found */
1460 /* need check if something changed */
1461 /* XXX not yet implemented */
1465 * New address is found
1467 if ((ifa = MALLOC(struct ifac)) == NULL) {
1468 fatal("malloc: struct ifac");
1471 memset(ifa, 0, sizeof(*ifa));
1472 ifa->ifa_conf = ifcp;
1473 ifa->ifa_next = ifcp->ifc_addr;
1474 ifcp->ifc_addr = ifa;
1475 ifa->ifa_addr = sin->sin6_addr;
1476 ifa->ifa_plen = plen;
1477 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1478 ifr.ifr_addr = *sin;
1479 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1480 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1483 ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
1484 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
1485 trace(1, "found address %s/%d -- %s\n",
1486 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
1488 trace(1, "found address %s/%d\n",
1489 inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
1491 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1492 ifcp->ifc_mylladdr = ifa->ifa_addr;
1493 ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
1494 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1495 SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
1497 setindex2ifc(ifcp->ifc_index, ifcp);
1498 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1499 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1500 ifcp->ifc_mtu = RIP6_MAXMTU;
1501 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1502 fatal("ioctl: SIOCGIFMETRIC");
1505 ifcp->ifc_metric = ifr.ifr_metric;
1506 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1507 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1509 ifcp->ifc_cflags |= IFC_CHANGED;
1513 * Receive and process routing messages.
1514 * Update interface information as necesssary.
1521 struct rt_msghdr *rtm;
1522 struct ifa_msghdr *ifam;
1523 struct if_msghdr *ifm;
1525 struct ifc *ifcp, *ic;
1526 int iface = 0, rtable = 0;
1527 struct sockaddr_in6 *rta[RTAX_MAX];
1528 struct sockaddr_in6 mask;
1532 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1533 perror("read from rtsock");
1536 if (len < sizeof(*rtm)) {
1537 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1538 len, (u_long)sizeof(*rtm));
1542 for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1543 /* safety against bogus message */
1544 if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
1545 trace(1, "bogus rtmsg: length=%d\n",
1546 ((struct rt_msghdr *)p)->rtm_msglen);
1552 switch (((struct rt_msghdr *)p)->rtm_type) {
1555 ifam = (struct ifa_msghdr *)p;
1556 addrs = ifam->ifam_addrs;
1557 q = (char *)(ifam + 1);
1560 ifm = (struct if_msghdr *)p;
1561 addrs = ifm->ifm_addrs;
1562 q = (char *)(ifm + 1);
1565 rtm = (struct rt_msghdr *)p;
1566 addrs = rtm->rtm_addrs;
1567 q = (char *)(rtm + 1);
1568 if (rtm->rtm_version != RTM_VERSION) {
1569 trace(1, "unexpected rtmsg version %d "
1571 rtm->rtm_version, RTM_VERSION);
1574 if (rtm->rtm_pid == pid) {
1576 trace(1, "rtmsg looped back to me, ignored\n");
1582 memset(&rta, 0, sizeof(rta));
1583 for (i = 0; i < RTAX_MAX; i++) {
1584 if (addrs & (1 << i)) {
1585 rta[i] = (struct sockaddr_in6 *)q;
1586 q += ROUNDUP(rta[i]->sin6_len);
1590 trace(1, "rtsock: %s (addrs=%x)\n",
1591 rttypes((struct rt_msghdr *)p), addrs);
1594 i < ((struct rt_msghdr *)p)->rtm_msglen;
1596 fprintf(stderr, "%02x ", p[i] & 0xff);
1597 if (i % 16 == 15) fprintf(stderr, "\n");
1599 fprintf(stderr, "\n");
1605 * We may be able to optimize by using ifm->ifm_index or
1606 * ifam->ifam_index. For simplicity we don't do that here.
1608 switch (((struct rt_msghdr *)p)->rtm_type) {
1621 /* nothing to be done here */
1622 trace(1, "\tnothing to be done, ignored\n");
1627 if (rta[RTAX_DST] == NULL) {
1628 trace(1, "\tno destination, ignored\n");
1631 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1632 trace(1, "\taf mismatch, ignored\n");
1635 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1636 trace(1, "\tlinklocal destination, ignored\n");
1639 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1640 trace(1, "\tloopback destination, ignored\n");
1641 continue; /* Loopback */
1643 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1644 trace(1, "\tmulticast destination, ignored\n");
1650 switch (((struct rt_msghdr *)p)->rtm_type) {
1659 /* should already be handled */
1660 fatal("rtrecv: never reach here");
1663 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1664 trace(1, "\tsome of dst/gw/netamsk are "
1665 "unavailable, ignored\n");
1668 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1669 mask.sin6_len = sizeof(mask);
1670 memset(&mask.sin6_addr, 0xff,
1671 sizeof(mask.sin6_addr));
1672 rta[RTAX_NETMASK] = &mask;
1673 } else if (!rta[RTAX_NETMASK]) {
1674 trace(1, "\tsome of dst/gw/netamsk are "
1675 "unavailable, ignored\n");
1678 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1679 rta[RTAX_NETMASK]) == 0) {
1680 rtable++; /*just to be sure*/
1685 trace(1, "\tnot supported yet, ignored\n");
1688 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1689 trace(1, "\tno netmask or ifa given, ignored\n");
1692 if (ifam->ifam_index < nindex2ifc)
1693 ifcp = index2ifc[ifam->ifam_index];
1697 trace(1, "\tinvalid ifam_index %d, ignored\n",
1701 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1706 trace(1, "\tnot supported yet, ignored\n");
1713 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1715 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
1716 if (ifcp->ifc_cflags & IFC_CHANGED) {
1717 if (ifrt(ifcp, 1)) {
1718 for (ic = ifc; ic; ic = ic->ifc_next) {
1719 if (ifcp->ifc_index == ic->ifc_index)
1721 if (ic->ifc_flags & IFF_UP)
1722 ripsend(ic, &ic->ifc_ripsin,
1725 /* Reset the flag */
1726 for (rrt = riprt; rrt; rrt = rrt->rrt_next)
1727 rrt->rrt_rflags &= ~RRTF_CHANGED;
1729 ifcp->ifc_cflags &= ~IFC_CHANGED;
1733 trace(1, "rtsock: read routing table again\n");
1739 * remove specified route from the internal routing table.
1742 rt_del(sdst, sgw, smask)
1743 const struct sockaddr_in6 *sdst;
1744 const struct sockaddr_in6 *sgw;
1745 const struct sockaddr_in6 *smask;
1747 const struct in6_addr *dst = NULL;
1748 const struct in6_addr *gw = NULL;
1750 struct netinfo6 ni6;
1751 struct riprt *rrt = NULL;
1754 if (sdst->sin6_family != AF_INET6) {
1755 trace(1, "\tother AF, ignored\n");
1758 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1759 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1760 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1761 trace(1, "\taddress %s not interesting, ignored\n",
1762 inet6_n2p(&sdst->sin6_addr));
1765 dst = &sdst->sin6_addr;
1766 if (sgw->sin6_family == AF_INET6) {
1768 gw = &sgw->sin6_addr;
1769 prefix = sin6mask2len(smask);
1770 } else if (sgw->sin6_family == AF_LINK) {
1772 * Interface route... a hard case. We need to get the prefix
1773 * length from the kernel, but we now are parsing rtmsg.
1774 * We'll purge matching routes from my list, then get the
1777 struct riprt *longest;
1778 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1781 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
1782 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1784 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1786 || longest->rrt_info.rip6_plen <
1787 rrt->rrt_info.rip6_plen) {
1794 trace(1, "\tno matching interface route found\n");
1797 gw = &in6addr_loopback;
1798 prefix = rrt->rrt_info.rip6_plen;
1800 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
1804 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
1805 trace(1, "gw %s\n", inet6_n2p(gw));
1806 t_lifetime = time(NULL) - RIP_LIFETIME;
1807 /* age route for interface address */
1808 memset(&ni6, 0, sizeof(ni6));
1809 ni6.rip6_dest = *dst;
1810 ni6.rip6_plen = prefix;
1811 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1812 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
1814 if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
1815 trace(1, "\tno route found\n");
1819 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
1820 trace(1, "\tyou can delete static routes only\n");
1823 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
1824 trace(1, "\tgw mismatch: %s <-> ",
1825 inet6_n2p(&rrt->rrt_gw));
1826 trace(1, "%s\n", inet6_n2p(gw));
1828 trace(1, "\troute found, age it\n");
1829 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1830 rrt->rrt_t = t_lifetime;
1831 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1838 * remove specified address from internal interface/routing table.
1841 rt_deladdr(ifcp, sifa, smask)
1843 const struct sockaddr_in6 *sifa;
1844 const struct sockaddr_in6 *smask;
1846 const struct in6_addr *addr = NULL;
1848 struct ifac *ifa = NULL;
1849 struct netinfo6 ni6;
1850 struct riprt *rrt = NULL;
1854 if (sifa->sin6_family != AF_INET6) {
1855 trace(1, "\tother AF, ignored\n");
1858 addr = &sifa->sin6_addr;
1859 prefix = sin6mask2len(smask);
1861 trace(1, "\tdeleting %s/%d from %s\n",
1862 inet6_n2p(addr), prefix, ifcp->ifc_name);
1863 ifa = ifa_match(ifcp, addr, prefix);
1865 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1866 inet6_n2p(addr), prefix, ifcp->ifc_name);
1869 if (ifa->ifa_conf != ifcp) {
1870 trace(1, "\taddress table corrupt: back pointer does not match "
1872 ifcp->ifc_name, ifa->ifa_conf->ifc_name);
1875 /* remove ifa from interface */
1876 if (ifcp->ifc_addr == ifa)
1877 ifcp->ifc_addr = ifa->ifa_next;
1880 for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
1881 if (p->ifa_next == ifa) {
1882 p->ifa_next = ifa->ifa_next;
1887 ifa->ifa_next = NULL;
1888 ifa->ifa_conf = NULL;
1889 t_lifetime = time(NULL) - RIP_LIFETIME;
1890 /* age route for interface address */
1891 memset(&ni6, 0, sizeof(ni6));
1892 ni6.rip6_dest = ifa->ifa_addr;
1893 ni6.rip6_plen = ifa->ifa_plen;
1894 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
1895 trace(1, "\tfind interface route %s/%d on %d\n",
1896 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
1897 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1898 struct in6_addr none;
1899 memset(&none, 0, sizeof(none));
1900 if (rrt->rrt_index == ifcp->ifc_index &&
1901 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
1902 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
1903 trace(1, "\troute found, age it\n");
1904 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1905 rrt->rrt_t = t_lifetime;
1906 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
1910 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1911 inet6_n2p(&rrt->rrt_info.rip6_dest),
1912 rrt->rrt_info.rip6_plen,
1916 trace(1, "\tno interface route found\n");
1917 /* age route for p2p destination */
1918 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1919 memset(&ni6, 0, sizeof(ni6));
1920 ni6.rip6_dest = ifa->ifa_raddr;
1921 ni6.rip6_plen = 128;
1922 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
1923 trace(1, "\tfind p2p route %s/%d on %d\n",
1924 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
1926 if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
1927 if (rrt->rrt_index == ifcp->ifc_index &&
1928 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
1929 trace(1, "\troute found, age it\n");
1930 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
1931 rrt->rrt_t = t_lifetime;
1932 rrt->rrt_info.rip6_metric =
1937 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1938 inet6_n2p(&rrt->rrt_info.rip6_dest),
1939 rrt->rrt_info.rip6_plen,
1943 trace(1, "\tno p2p route found\n");
1945 return updated ? 0 : -1;
1949 * Get each interface address and put those interface routes to the route
1958 struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
1959 struct netinfo6 *np;
1961 int need_trigger = 0;
1963 if (ifcp->ifc_flags & IFF_LOOPBACK)
1964 return 0; /* ignore loopback */
1965 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1966 ifrt_p2p(ifcp, again);
1970 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
1971 if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
1973 trace(1, "route: %s on %s: "
1974 "skip linklocal interface address\n",
1975 inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
1979 if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
1981 trace(1, "route: %s: skip unspec interface address\n",
1986 if (ifcp->ifc_flags & IFF_UP) {
1987 if ((rrt = MALLOC(struct riprt)) == NULL)
1988 fatal("malloc: struct riprt");
1989 memset(rrt, 0, sizeof(*rrt));
1990 rrt->rrt_same = NULL;
1991 rrt->rrt_index = ifcp->ifc_index;
1992 rrt->rrt_t = 0; /* don't age */
1993 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
1994 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
1995 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
1996 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
1997 rrt->rrt_flags = RTF_CLONING;
1998 rrt->rrt_rflags |= RRTF_CHANGED;
1999 applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
2000 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2002 /* XXX why gateway address == network adddress? */
2003 rrt->rrt_gw = ifa->ifa_addr;
2005 np = &rrt->rrt_info;
2006 search_rrt = rtsearch(np, &prev_rrt);
2007 if (search_rrt != NULL) {
2008 if (search_rrt->rrt_info.rip6_metric >
2009 rrt->rrt_info.rip6_metric) {
2011 prev_rrt->rrt_next = rrt->rrt_next;
2013 riprt = rrt->rrt_next;
2014 delroute(&rrt->rrt_info, &rrt->rrt_gw);
2017 /* Already have better route */
2019 trace(1, "route: %s/%d: "
2020 "already registered (%s)\n",
2021 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2028 /* Attach the route to the list */
2029 trace(1, "route: %s/%d: register route (%s)\n",
2030 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2032 rrt->rrt_next = riprt;
2034 addroute(rrt, &rrt->rrt_gw, ifcp);
2036 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2039 for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
2040 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2041 t_lifetime = time(NULL) - RIP_LIFETIME;
2042 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2043 loop_rrt->rrt_t = t_lifetime;
2044 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2045 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2052 return need_trigger;
2056 * there are couple of p2p interface routing models. "behavior" lets
2057 * you pick one. it looks that gated behavior fits best with BSDs,
2058 * since BSD kernels does not look at prefix length on p2p interfaces.
2061 ifrt_p2p(ifcp, again)
2066 struct riprt *rrt, *orrt, *prevrrt;
2067 struct netinfo6 *np;
2068 struct in6_addr addr, dest;
2069 int advert, ignore, i;
2070 #define P2PADVERT_NETWORK 1
2071 #define P2PADVERT_ADDR 2
2072 #define P2PADVERT_DEST 4
2073 #define P2PADVERT_MAX 4
2074 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2075 const char *category = "";
2078 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2079 addr = ifa->ifa_addr;
2080 dest = ifa->ifa_raddr;
2081 applyplen(&addr, ifa->ifa_plen);
2082 applyplen(&dest, ifa->ifa_plen);
2083 advert = ignore = 0;
2087 * honor addr/plen, just like normal shared medium
2088 * interface. this may cause trouble if you reuse
2089 * addr/plen on other interfaces.
2091 * advertise addr/plen.
2093 advert |= P2PADVERT_NETWORK;
2097 * prefixlen on p2p interface is meaningless.
2098 * advertise addr/128 and dest/128.
2100 * do not install network route to route6d routing
2101 * table (if we do, it would prevent route installation
2102 * for other p2p interface that shares addr/plen).
2104 * XXX what should we do if dest is ::? it will not
2105 * get announced anyways (see following filter),
2106 * but we need to think.
2108 advert |= P2PADVERT_ADDR;
2109 advert |= P2PADVERT_DEST;
2110 ignore |= P2PADVERT_NETWORK;
2114 * just for testing. actually the code is redundant
2115 * given the current p2p interface address assignment
2116 * rule for kame kernel.
2119 * A/n -> announce A/n
2120 * A B/n, A and B share prefix -> A/n (= B/n)
2121 * A B/n, do not share prefix -> A/128 and B/128
2122 * actually, A/64 and A B/128 are the only cases
2123 * permitted by the kernel:
2125 * A B/128 -> A/128 and B/128
2127 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
2128 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2129 advert |= P2PADVERT_NETWORK;
2131 advert |= P2PADVERT_ADDR;
2132 advert |= P2PADVERT_DEST;
2133 ignore |= P2PADVERT_NETWORK;
2136 advert |= P2PADVERT_NETWORK;
2140 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2141 if ((ignore & i) != 0)
2143 if ((rrt = MALLOC(struct riprt)) == NULL) {
2144 fatal("malloc: struct riprt");
2147 memset(rrt, 0, sizeof(*rrt));
2148 rrt->rrt_same = NULL;
2149 rrt->rrt_index = ifcp->ifc_index;
2150 rrt->rrt_t = 0; /* don't age */
2152 case P2PADVERT_NETWORK:
2153 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2154 rrt->rrt_info.rip6_plen = ifa->ifa_plen;
2155 applyplen(&rrt->rrt_info.rip6_dest,
2157 category = "network";
2159 case P2PADVERT_ADDR:
2160 rrt->rrt_info.rip6_dest = ifa->ifa_addr;
2161 rrt->rrt_info.rip6_plen = 128;
2162 rrt->rrt_gw = in6addr_loopback;
2165 case P2PADVERT_DEST:
2166 rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
2167 rrt->rrt_info.rip6_plen = 128;
2168 rrt->rrt_gw = ifa->ifa_addr;
2172 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2173 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2175 trace(1, "route: %s: skip unspec/linklocal "
2176 "(%s on %s)\n", category, ifcp->ifc_name);
2181 if ((advert & i) == 0) {
2182 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2186 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2187 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2188 np = &rrt->rrt_info;
2189 orrt = rtsearch(np, &prevrrt);
2191 /* Attach the route to the list */
2192 trace(1, "route: %s/%d: register route "
2194 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2195 category, ifcp->ifc_name, noadv);
2196 rrt->rrt_next = riprt;
2198 } else if (rrt->rrt_index != orrt->rrt_index ||
2199 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2201 rrt->rrt_next = orrt->rrt_next;
2203 prevrrt->rrt_next = rrt;
2208 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2209 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2210 category, ifcp->ifc_name, noadv);
2214 trace(1, "route: %s/%d: "
2215 "already registered (%s on %s%s)\n",
2216 inet6_n2p(&np->rip6_dest),
2217 np->rip6_plen, category,
2218 ifcp->ifc_name, noadv);
2224 #undef P2PADVERT_NETWORK
2225 #undef P2PADVERT_ADDR
2226 #undef P2PADVERT_DEST
2227 #undef P2PADVERT_MAX
2237 struct if_msghdr *ifm;
2244 mib[4] = NET_RT_IFLIST;
2246 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2247 fatal("sysctl estimate NET_RT_IFLIST");
2250 if ((buf = malloc(msize)) == NULL) {
2254 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2255 fatal("sysctl NET_RT_IFLIST");
2258 ifm = (struct if_msghdr *)buf;
2259 mtu = ifm->ifm_data.ifi_mtu;
2260 #ifdef __DragonFly__
2261 if (ifindex != ifm->ifm_index) {
2262 fatal("ifindex does not match with ifm_index");
2272 struct rt_msghdr *rtm;
2274 #define RTTYPE(s, f) \
2276 if (rtm->rtm_type == (f)) \
2279 RTTYPE("ADD", RTM_ADD);
2280 RTTYPE("DELETE", RTM_DELETE);
2281 RTTYPE("CHANGE", RTM_CHANGE);
2282 RTTYPE("GET", RTM_GET);
2283 RTTYPE("LOSING", RTM_LOSING);
2284 RTTYPE("REDIRECT", RTM_REDIRECT);
2285 RTTYPE("MISS", RTM_MISS);
2286 RTTYPE("LOCK", RTM_LOCK);
2287 RTTYPE("OLDADD", RTM_OLDADD);
2288 RTTYPE("OLDDEL", RTM_OLDDEL);
2289 RTTYPE("RESOLVE", RTM_RESOLVE);
2290 RTTYPE("NEWADDR", RTM_NEWADDR);
2291 RTTYPE("DELADDR", RTM_DELADDR);
2292 RTTYPE("IFINFO", RTM_IFINFO);
2294 RTTYPE("OLDADD", RTM_OLDADD);
2297 RTTYPE("OLDDEL", RTM_OLDDEL);
2300 RTTYPE("OIFINFO", RTM_OIFINFO);
2302 #ifdef RTM_IFANNOUNCE
2303 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2306 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2309 RTTYPE("DELMADDR", RTM_DELMADDR);
2317 struct rt_msghdr *rtm;
2319 static char buf[BUFSIZ];
2322 * letter conflict should be okay. painful when *BSD diverges...
2324 strlcpy(buf, "", sizeof(buf));
2325 #define RTFLAG(s, f) \
2327 if (rtm->rtm_flags & (f)) \
2328 strlcat(buf, (s), sizeof(buf)); \
2330 RTFLAG("U", RTF_UP);
2331 RTFLAG("G", RTF_GATEWAY);
2332 RTFLAG("H", RTF_HOST);
2333 RTFLAG("R", RTF_REJECT);
2334 RTFLAG("D", RTF_DYNAMIC);
2335 RTFLAG("M", RTF_MODIFIED);
2336 RTFLAG("d", RTF_DONE);
2338 RTFLAG("m", RTF_MASK);
2340 RTFLAG("C", RTF_CLONING);
2342 RTFLAG("c", RTF_CLONED);
2344 #ifdef RTF_PRCLONING
2345 RTFLAG("c", RTF_PRCLONING);
2347 #ifdef RTF_WASCLONED
2348 RTFLAG("W", RTF_WASCLONED);
2350 RTFLAG("X", RTF_XRESOLVE);
2351 RTFLAG("L", RTF_LLINFO);
2352 RTFLAG("S", RTF_STATIC);
2353 RTFLAG("B", RTF_BLACKHOLE);
2355 RTFLAG("3", RTF_PROTO3);
2357 RTFLAG("2", RTF_PROTO2);
2358 RTFLAG("1", RTF_PROTO1);
2359 #ifdef RTF_BROADCAST
2360 RTFLAG("b", RTF_BROADCAST);
2363 RTFLAG("d", RTF_DEFAULT);
2365 #ifdef RTF_ISAROUTER
2366 RTFLAG("r", RTF_ISAROUTER);
2369 RTFLAG("T", RTF_TUNNEL);
2372 RTFLAG("A", RTF_AUTH);
2375 RTFLAG("E", RTF_CRYPT);
2385 static char buf[BUFSIZ];
2387 strlcpy(buf, "", sizeof(buf));
2388 #define IFFLAG(s, f) \
2392 strlcat(buf, ",", sizeof(buf)); \
2393 strlcat(buf, s, sizeof(buf)); \
2396 IFFLAG("UP", IFF_UP);
2397 IFFLAG("BROADCAST", IFF_BROADCAST);
2398 IFFLAG("DEBUG", IFF_DEBUG);
2399 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2400 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2401 #ifdef IFF_NOTRAILERS
2402 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2405 IFFLAG("SMART", IFF_SMART);
2407 IFFLAG("RUNNING", IFF_RUNNING);
2408 IFFLAG("NOARP", IFF_NOARP);
2409 IFFLAG("PROMISC", IFF_PROMISC);
2410 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2411 IFFLAG("OACTIVE", IFF_OACTIVE);
2412 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2413 IFFLAG("LINK0", IFF_LINK0);
2414 IFFLAG("LINK1", IFF_LINK1);
2415 IFFLAG("LINK2", IFF_LINK2);
2416 IFFLAG("MULTICAST", IFF_MULTICAST);
2427 char *buf, *p, *lim;
2428 struct rt_msghdr *rtm;
2437 mib[3] = AF_INET6; /* Address family */
2438 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2439 mib[5] = 0; /* No flags */
2445 if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
2446 errmsg = "sysctl estimate";
2449 if ((buf = malloc(msize)) == NULL) {
2453 if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
2454 errmsg = "sysctl NET_RT_DUMP";
2457 } while (retry < 5 && errmsg != NULL);
2459 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2462 } else if (1 < retry)
2463 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2466 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2467 rtm = (struct rt_msghdr *)p;
2468 rt_entry(rtm, again);
2474 rt_entry(rtm, again)
2475 struct rt_msghdr *rtm;
2478 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2479 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2480 char *rtmp, *ifname = NULL;
2481 struct riprt *rrt, *orrt;
2482 struct netinfo6 *np;
2485 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2486 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2487 (RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
2488 return; /* not interested in the link route */
2490 /* do not look at cloned routes */
2491 #ifdef RTF_WASCLONED
2492 if (rtm->rtm_flags & RTF_WASCLONED)
2496 if (rtm->rtm_flags & RTF_CLONED)
2500 * do not look at dynamic routes.
2501 * netbsd/openbsd cloned routes have UGHD.
2503 if (rtm->rtm_flags & RTF_DYNAMIC)
2505 rtmp = (char *)(rtm + 1);
2507 if ((rtm->rtm_addrs & RTA_DST) == 0)
2508 return; /* ignore routes without destination address */
2509 sin6_dst = (struct sockaddr_in6 *)rtmp;
2510 rtmp += ROUNDUP(sin6_dst->sin6_len);
2511 if (rtm->rtm_addrs & RTA_GATEWAY) {
2512 sin6_gw = (struct sockaddr_in6 *)rtmp;
2513 rtmp += ROUNDUP(sin6_gw->sin6_len);
2515 if (rtm->rtm_addrs & RTA_NETMASK) {
2516 sin6_mask = (struct sockaddr_in6 *)rtmp;
2517 rtmp += ROUNDUP(sin6_mask->sin6_len);
2519 if (rtm->rtm_addrs & RTA_GENMASK) {
2520 sin6_genmask = (struct sockaddr_in6 *)rtmp;
2521 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2523 if (rtm->rtm_addrs & RTA_IFP) {
2524 sin6_ifp = (struct sockaddr_in6 *)rtmp;
2525 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2529 if (sin6_dst->sin6_family != AF_INET6)
2531 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2532 return; /* Link-local */
2533 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2534 return; /* Loopback */
2535 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2538 if ((rrt = MALLOC(struct riprt)) == NULL) {
2539 fatal("malloc: struct riprt");
2542 memset(rrt, 0, sizeof(*rrt));
2543 np = &rrt->rrt_info;
2544 rrt->rrt_same = NULL;
2545 rrt->rrt_t = time(NULL);
2546 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2547 rrt->rrt_t = 0; /* Don't age static routes */
2549 np->rip6_tag = htons(routetag & 0xffff);
2553 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2554 if (np->rip6_metric < 1)
2555 np->rip6_metric = 1;
2556 rrt->rrt_flags = rtm->rtm_flags;
2557 np->rip6_dest = sin6_dst->sin6_addr;
2560 if (rtm->rtm_flags & RTF_HOST)
2561 np->rip6_plen = 128; /* Host route */
2563 np->rip6_plen = sin6mask2len(sin6_mask);
2567 orrt = rtsearch(np, NULL);
2568 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2571 trace(1, "route: %s/%d flags %s: already registered\n",
2572 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2580 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2582 if (sin6_gw->sin6_family == AF_INET6)
2583 rrt->rrt_gw = sin6_gw->sin6_addr;
2584 else if (sin6_gw->sin6_family == AF_LINK) {
2585 /* XXX in case ppp link? */
2586 rrt->rrt_gw = in6addr_loopback;
2588 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2590 trace(1, "route: %s/%d flags %s",
2591 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2592 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2596 if (s < nindex2ifc && index2ifc[s])
2597 ifname = index2ifc[s]->ifc_name;
2599 trace(1, " not configured\n");
2603 trace(1, " if %s sock %d", ifname, s);
2609 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2610 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
2611 #ifdef __DragonFly__
2612 && (rrt->rrt_flags & RTF_LOCAL) == 0
2615 trace(0, "***** Gateway %s is not a link-local address.\n",
2616 inet6_n2p(&rrt->rrt_gw));
2617 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2618 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2619 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2622 /* Put it to the route list */
2623 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2624 /* replace route list */
2625 rrt->rrt_next = orrt->rrt_next;
2627 trace(1, "route: %s/%d flags %s: replace new route\n",
2628 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2632 rrt->rrt_next = riprt;
2638 addroute(rrt, gw, ifcp)
2640 const struct in6_addr *gw;
2643 struct netinfo6 *np;
2644 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2645 struct rt_msghdr *rtm;
2646 struct sockaddr_in6 *sin;
2649 np = &rrt->rrt_info;
2650 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2651 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2652 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2653 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2654 np->rip6_metric - 1, buf2);
2656 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2657 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2658 np->rip6_metric - 1, buf2);
2662 memset(buf, 0, sizeof(buf));
2663 rtm = (struct rt_msghdr *)buf;
2664 rtm->rtm_type = RTM_ADD;
2665 rtm->rtm_version = RTM_VERSION;
2666 rtm->rtm_seq = ++seq;
2668 rtm->rtm_flags = rrt->rrt_flags;
2669 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2670 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2671 rtm->rtm_inits = RTV_HOPCOUNT;
2672 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2674 sin->sin6_len = sizeof(struct sockaddr_in6);
2675 sin->sin6_family = AF_INET6;
2676 sin->sin6_addr = np->rip6_dest;
2677 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2679 sin->sin6_len = sizeof(struct sockaddr_in6);
2680 sin->sin6_family = AF_INET6;
2681 sin->sin6_addr = *gw;
2682 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2684 sin->sin6_len = sizeof(struct sockaddr_in6);
2685 sin->sin6_family = AF_INET6;
2686 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2687 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2689 len = (char *)sin - (char *)buf;
2690 rtm->rtm_msglen = len;
2691 if (write(rtsock, buf, len) > 0)
2694 if (errno == EEXIST) {
2695 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2696 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2698 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2699 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2701 trace(0, "Can not write to rtsock (addroute): %s\n",
2704 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2712 struct netinfo6 *np;
2713 struct in6_addr *gw;
2715 u_char buf[BUFSIZ], buf2[BUFSIZ];
2716 struct rt_msghdr *rtm;
2717 struct sockaddr_in6 *sin;
2720 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2721 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2722 np->rip6_plen, buf2);
2724 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2725 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2729 memset(buf, 0, sizeof(buf));
2730 rtm = (struct rt_msghdr *)buf;
2731 rtm->rtm_type = RTM_DELETE;
2732 rtm->rtm_version = RTM_VERSION;
2733 rtm->rtm_seq = ++seq;
2735 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2736 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2737 rtm->rtm_flags |= RTF_HOST;
2738 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2739 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2741 sin->sin6_len = sizeof(struct sockaddr_in6);
2742 sin->sin6_family = AF_INET6;
2743 sin->sin6_addr = np->rip6_dest;
2744 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2746 sin->sin6_len = sizeof(struct sockaddr_in6);
2747 sin->sin6_family = AF_INET6;
2748 sin->sin6_addr = *gw;
2749 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2751 sin->sin6_len = sizeof(struct sockaddr_in6);
2752 sin->sin6_family = AF_INET6;
2753 sin->sin6_addr = *(plen2mask(np->rip6_plen));
2754 sin = (struct sockaddr_in6 *)((char *)sin + ROUNDUP(sin->sin6_len));
2756 len = (char *)sin - (char *)buf;
2757 rtm->rtm_msglen = len;
2758 if (write(rtsock, buf, len) >= 0)
2761 if (errno == ESRCH) {
2762 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2763 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2765 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2766 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2768 trace(0, "Can not write to rtsock (delroute): %s\n",
2771 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2779 struct netinfo6 *np;
2780 struct in6_addr *gw;
2785 struct rt_msghdr *rtm;
2786 struct sockaddr_in6 *sin;
2788 rtm = (struct rt_msghdr *)buf;
2789 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2790 memset(rtm, 0, len);
2791 rtm->rtm_type = RTM_GET;
2792 rtm->rtm_version = RTM_VERSION;
2794 rtm->rtm_seq = myseq;
2795 rtm->rtm_addrs = RTA_DST;
2796 rtm->rtm_msglen = len;
2797 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2798 sin->sin6_len = sizeof(struct sockaddr_in6);
2799 sin->sin6_family = AF_INET6;
2800 sin->sin6_addr = np->rip6_dest;
2801 if (write(rtsock, buf, len) < 0) {
2802 if (errno == ESRCH) /* No such route found */
2804 perror("write to rtsock");
2808 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2809 perror("read from rtsock");
2812 rtm = (struct rt_msghdr *)buf;
2813 } while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2814 sin = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2815 if (rtm->rtm_addrs & RTA_DST) {
2816 sin = (struct sockaddr_in6 *)
2817 ((char *)sin + ROUNDUP(sin->sin6_len));
2819 if (rtm->rtm_addrs & RTA_GATEWAY) {
2820 *gw = sin->sin6_addr;
2828 const struct in6_addr *p;
2830 static char buf[BUFSIZ];
2832 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
2855 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2858 fprintf(dump, "%s: Interface Table Dump\n", hms());
2859 fprintf(dump, " Number of interfaces: %d\n", nifc);
2860 for (i = 0; i < 2; i++) {
2861 fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
2862 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
2864 if ((ifcp->ifc_flags & IFF_UP) == 0)
2866 if (iff_find(ifcp, 'N') != NULL)
2869 if (ifcp->ifc_flags & IFF_UP)
2872 ifdump0(dump, ifcp);
2875 fprintf(dump, "\n");
2883 const struct ifc *ifcp;
2891 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2892 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
2893 inet6_n2p(&ifcp->ifc_mylladdr),
2894 ifcp->ifc_mtu, ifcp->ifc_metric);
2895 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
2896 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2897 inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
2899 fprintf(dump, "\t%s/%d -- %s\n",
2900 inet6_n2p(&ifa->ifa_addr),
2901 ifa->ifa_plen, buf);
2903 fprintf(dump, "\t%s/%d\n",
2904 inet6_n2p(&ifa->ifa_addr),
2908 if (ifcp->ifc_filter) {
2909 fprintf(dump, "\tFilter:");
2910 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
2912 switch (iffp->iff_type) {
2914 ft = "Aggregate"; addr++; break;
2916 ft = "No-use"; break;
2918 ft = "Advertise-only"; addr++; break;
2920 ft = "Default-only"; break;
2922 ft = "Listen-only"; addr++; break;
2924 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
2929 fprintf(dump, " %s", ft);
2931 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
2935 fprintf(dump, "\n");
2951 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
2955 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
2956 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
2957 if (rrt->rrt_t == 0)
2960 age = t - rrt->rrt_t;
2961 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
2963 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2964 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
2965 index2ifc[rrt->rrt_index]->ifc_name,
2966 inet6_n2p(&rrt->rrt_gw),
2967 rrt->rrt_info.rip6_metric, (long)age);
2968 if (rrt->rrt_info.rip6_tag) {
2969 fprintf(dump, " tag(0x%04x)",
2970 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
2972 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
2973 fprintf(dump, " NOT-LL");
2974 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
2975 fprintf(dump, " NO-ADV");
2976 fprintf(dump, "\n");
2978 fprintf(dump, "\n");
2984 * Parse the -A (and -O) options and put corresponding filter object to the
2985 * specified interface structures. Each of the -A/O option has the following
2986 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2987 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2993 char *p, *ap, *iflp, *ifname;
2994 struct iff ftmp, *iff_obj;
3001 for (i = 0; i < nfilter; i++) {
3005 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3009 if ((p = strchr(ap, ',')) != NULL) {
3013 if ((p = strchr(ap, '/')) == NULL) {
3014 fatal("no prefixlen specified for '%s'", ap);
3018 if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
3019 fatal("invalid prefix specified for '%s'", ap);
3022 ftmp.iff_plen = atoi(p);
3023 ftmp.iff_next = NULL;
3024 applyplen(&ftmp.iff_addr, ftmp.iff_plen);
3026 ftmp.iff_type = filtertype[i];
3027 if (iflp == NULL || *iflp == '\0') {
3028 fatal("no interface specified for '%s'", ap);
3031 /* parse the interface listing portion */
3034 if ((iflp = strchr(iflp, ',')) != NULL)
3036 ifcp = ifc_find(ifname);
3038 fatal("no interface %s exists", ifname);
3041 iff_obj = (struct iff *)malloc(sizeof(struct iff));
3042 if (iff_obj == NULL) {
3043 fatal("malloc of iff_obj");
3046 memcpy((void *)iff_obj, (void *)&ftmp,
3047 sizeof(struct iff));
3048 /* link it to the interface filter */
3049 iff_obj->iff_next = ifcp->ifc_filter;
3050 ifcp->ifc_filter = iff_obj;
3054 * -A: aggregate configuration.
3056 if (filtertype[i] != 'A')
3058 /* put the aggregate to the kernel routing table */
3059 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3061 fatal("malloc: rrt");
3064 memset(rrt, 0, sizeof(struct riprt));
3065 rrt->rrt_info.rip6_dest = ftmp.iff_addr;
3066 rrt->rrt_info.rip6_plen = ftmp.iff_plen;
3067 rrt->rrt_info.rip6_metric = 1;
3068 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3069 rrt->rrt_gw = in6addr_loopback;
3070 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3071 rrt->rrt_rflags = RRTF_AGGREGATE;
3073 rrt->rrt_index = loopifindex;
3075 if (getroute(&rrt->rrt_info, &gw)) {
3078 * When the address has already been registered in the
3079 * kernel routing table, it should be removed
3081 delroute(&rrt->rrt_info, &gw);
3083 /* it is safer behavior */
3085 fatal("%s/%u already in routing table, "
3087 inet6_n2p(&rrt->rrt_info.rip6_dest),
3088 rrt->rrt_info.rip6_plen);
3093 /* Put the route to the list */
3094 rrt->rrt_next = riprt;
3096 trace(1, "Aggregate: %s/%d for %s\n",
3097 inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
3099 /* Add this route to the kernel */
3100 if (nflag) /* do not modify kernel routing table */
3102 addroute(rrt, &in6addr_loopback, loopifcp);
3106 /***************** utility functions *****************/
3109 * Returns a pointer to ifac whose address and prefix length matches
3110 * with the address and prefix length specified in the arguments.
3113 ifa_match(ifcp, ia, plen)
3114 const struct ifc *ifcp;
3115 const struct in6_addr *ia;
3120 for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
3121 if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
3122 ifa->ifa_plen == plen)
3129 * Return a pointer to riprt structure whose address and prefix length
3130 * matches with the address and prefix length found in the argument.
3131 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3134 rtsearch(np, prev_rrt)
3135 struct netinfo6 *np;
3136 struct riprt **prev_rrt;
3142 for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
3143 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3144 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3157 const struct sockaddr_in6 *sin6;
3160 return mask2len(&sin6->sin6_addr,
3161 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3165 mask2len(addr, lenlim)
3166 const struct in6_addr *addr;
3170 const u_char *p = (const u_char *)addr;
3172 for (j = 0; j < lenlim; j++, p++) {
3179 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3180 MASKLEN(0xfe, 7); break;
3181 MASKLEN(0xfc, 6); break;
3182 MASKLEN(0xf8, 5); break;
3183 MASKLEN(0xf0, 4); break;
3184 MASKLEN(0xe0, 3); break;
3185 MASKLEN(0xc0, 2); break;
3186 MASKLEN(0x80, 1); break;
3194 applymask(addr, mask)
3195 struct in6_addr *addr, *mask;
3200 p = (u_long *)addr; q = (u_long *)mask;
3201 for (i = 0; i < 4; i++)
3205 static const u_char plent[8] = {
3206 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3211 struct in6_addr *ia;
3218 for (i = 0; i < 16; i++) {
3227 static const int pl2m[9] = {
3228 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3235 static struct in6_addr ia;
3239 memset(&ia, 0, sizeof(struct in6_addr));
3241 for (i = 0; i < 16; i++, p++, n -= 8) {
3256 char *q = (char *)malloc(strlen(p) + 1);
3265 static char buf[BUFSIZ];
3270 if ((tm = localtime(&t)) == 0) {
3274 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3279 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3287 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3288 nextalarm = time(NULL) + interval;
3298 t = (int)(RIP_TRIG_INT6_MIN +
3299 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3300 sup_trig_update = time(NULL) + t;
3306 fatal(const char *fmt, ...)
3308 fatal(fmt, va_alist)
3321 vsnprintf(buf, sizeof(buf), fmt, ap);
3323 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3330 tracet(int level, const char *fmt, ...)
3332 tracet(level, fmt, va_alist)
3345 if (level <= dflag) {
3346 fprintf(stderr, "%s: ", hms());
3347 vfprintf(stderr, fmt, ap);
3351 vsyslog(LOG_DEBUG, fmt, ap);
3353 vsyslog(LOG_WARNING, fmt, ap);
3360 trace(int level, const char *fmt, ...)
3362 trace(level, fmt, va_alist)
3376 vfprintf(stderr, fmt, ap);
3379 vsyslog(LOG_DEBUG, fmt, ap);
3381 vsyslog(LOG_WARNING, fmt, ap);
3389 struct if_nameindex *p, *p0;
3390 unsigned int max = 0;
3392 p0 = if_nameindex();
3393 for (p = p0; p && p->if_index && p->if_name; p++) {
3394 if (max < p->if_index)
3397 if_freenameindex(p0);
3407 for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
3408 if (strcmp(name, ifcp->ifc_name) == 0)
3411 return (struct ifc *)NULL;
3415 iff_find(ifcp, type)
3421 for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
3422 if (iffp->iff_type == type)
3429 setindex2ifc(idx, ifcp)
3437 nindex2ifc = 5; /*initial guess*/
3438 index2ifc = (struct ifc **)
3439 malloc(sizeof(*index2ifc) * nindex2ifc);
3440 if (index2ifc == NULL) {
3444 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3447 while (nindex2ifc <= idx)
3449 if (n != nindex2ifc) {
3450 p = (struct ifc **)realloc(index2ifc,
3451 sizeof(*index2ifc) * nindex2ifc);
3456 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3459 index2ifc[idx] = ifcp;