1 /* $FreeBSD: src/usr.sbin/route6d/route6d.c,v 1.2.2.5 2001/07/03 11:02:09 ume Exp $ */
2 /* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $
35 #define _KERNEL_STRUCTURES
50 #include <sys/types.h>
51 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/sysctl.h>
58 #if defined(__DragonFly__)
59 #include <net/if_var.h>
60 #endif /* __DragonFly__ */
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet/udp.h>
69 #include <arpa/inet.h>
76 #define INIT_INTERVAL6 6
78 #define INIT_INTERVAL6 10 /* Wait to submit a initial riprequest */
82 * Following two macros are highly depending on KAME Release
84 #define IN6_LINKLOCAL_IFINDEX(addr) \
85 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
87 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
89 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
90 (addr).s6_addr[3] = (index) & 0xff; \
93 struct ifc
{ /* Configuration of an interface */
94 char *ifc_name
; /* if name */
96 int ifc_index
; /* if index */
97 int ifc_mtu
; /* if mtu */
98 int ifc_metric
; /* if metric */
99 u_int ifc_flags
; /* flags */
100 short ifc_cflags
; /* IFC_XXX */
101 struct in6_addr ifc_mylladdr
; /* my link-local address */
102 struct sockaddr_in6 ifc_ripsin
; /* rip multicast address */
103 struct iff
*ifc_filter
; /* filter structure */
104 struct ifac
*ifc_addr
; /* list of AF_INET6 addresses */
105 int ifc_joined
; /* joined to ff02::9 */
108 struct ifac
{ /* Adddress associated to an interface */
109 struct ifc
*ifa_conf
; /* back pointer */
110 struct ifac
*ifa_next
;
111 struct in6_addr ifa_addr
; /* address */
112 struct in6_addr ifa_raddr
; /* remote address, valid in p2p */
113 int ifa_plen
; /* prefix length */
118 struct in6_addr iff_addr
;
120 struct iff
*iff_next
;
124 int nifc
; /* number of valid ifc's */
125 struct ifc
**index2ifc
;
127 struct ifc
*loopifcp
= NULL
; /* pointing to loopback */
128 int loopifindex
= 0; /* ditto */
129 fd_set sockvec
; /* vector to select() for receiving */
130 int rtsock
; /* the routing socket */
131 int ripsock
; /* socket to send/receive RIP datagram */
133 struct rip6
*ripbuf
; /* packet buffer for sending */
136 * Maintain the routes in a linked list. When the number of the routes
137 * grows, somebody would like to introduce a hash based or a radix tree
138 * based structure. I believe the number of routes handled by RIP is
139 * limited and I don't have to manage a complex data structure, however.
141 * One of the major drawbacks of the linear linked list is the difficulty
142 * of representing the relationship between a couple of routes. This may
143 * be a significant problem when we have to support route aggregation with
144 * supressing the specifices covered by the aggregate.
148 struct riprt
*rrt_next
; /* next destination */
149 struct riprt
*rrt_same
; /* same destination - future use */
150 struct netinfo6 rrt_info
; /* network info */
151 struct in6_addr rrt_gw
; /* gateway */
152 u_long rrt_flags
; /* kernel routing table flags */
153 u_long rrt_rflags
; /* route6d routing table flags */
154 time_t rrt_t
; /* when the route validated */
155 int rrt_index
; /* ifindex from which this route got */
158 struct riprt
*riprt
= NULL
;
160 int dflag
= 0; /* debug flag */
161 int qflag
= 0; /* quiet flag */
162 int nflag
= 0; /* don't update kernel routing table */
163 int aflag
= 0; /* age out even the statically defined routes */
164 int hflag
= 0; /* don't split horizon */
165 int lflag
= 0; /* exchange site local routes */
166 int sflag
= 0; /* announce static routes w/ split horizon */
167 int Sflag
= 0; /* announce static routes to every interface */
168 unsigned long routetag
= 0; /* route tag attached on originating case */
170 char *filter
[MAXFILTER
];
171 int filtertype
[MAXFILTER
];
176 struct sockaddr_storage ripsin
;
178 struct rtentry rtentry
;
181 time_t nextalarm
= 0;
182 time_t sup_trig_update
= 0;
188 static u_long seq
= 0;
191 volatile sig_atomic_t seenalrm
;
192 volatile sig_atomic_t seenquit
;
193 volatile sig_atomic_t seenusr1
;
195 #define RRTF_AGGREGATE 0x08000000
196 #define RRTF_NOADVERTISE 0x10000000
197 #define RRTF_NH_NOT_LLADDR 0x20000000
198 #define RRTF_SENDANYWAY 0x40000000
199 #define RRTF_CHANGED 0x80000000
201 void sighandler(int);
204 void ripsend(struct ifc
*, struct sockaddr_in6
*, int);
205 int out_filter(struct riprt
*, struct ifc
*);
207 void sockopt(struct ifc
*);
209 void ifconfig1(const char *, const struct sockaddr
*, struct ifc
*, int);
211 int rt_del(const struct sockaddr_in6
*, const struct sockaddr_in6
*,
212 const struct sockaddr_in6
*);
213 int rt_deladdr(struct ifc
*, const struct sockaddr_in6
*,
214 const struct sockaddr_in6
*);
215 void filterconfig(void);
217 const char *rttypes(struct rt_msghdr
*);
218 const char *rtflags(struct rt_msghdr
*);
219 const char *ifflags(int);
220 int ifrt(struct ifc
*, int);
221 void ifrt_p2p(struct ifc
*, int);
222 void applymask(struct in6_addr
*, struct in6_addr
*);
223 void applyplen(struct in6_addr
*, int);
226 void ifdump0(FILE *, const struct ifc
*);
228 void rt_entry(struct rt_msghdr
*, int);
230 void riprequest(struct ifc
*, struct netinfo6
*, int,
231 struct sockaddr_in6
*);
232 void ripflush(struct ifc
*, struct sockaddr_in6
*);
233 void sendrequest(struct ifc
*);
234 int sin6mask2len(const struct sockaddr_in6
*);
235 int mask2len(const struct in6_addr
*, int);
236 int sendpacket(struct sockaddr_in6
*, int);
237 int addroute(struct riprt
*, const struct in6_addr
*, struct ifc
*);
238 int delroute(struct netinfo6
*, struct in6_addr
*);
239 struct in6_addr
*getroute(struct netinfo6
*, struct in6_addr
*);
241 int tobeadv(struct riprt
*, struct ifc
*);
242 char *allocopy(char *);
244 const char *inet6_n2p(const struct in6_addr
*);
245 struct ifac
*ifa_match(const struct ifc
*, const struct in6_addr
*, int);
246 struct in6_addr
*plen2mask(int);
247 struct riprt
*rtsearch(struct netinfo6
*, struct riprt
**);
248 int ripinterval(int);
249 time_t ripsuptrig(void);
250 void fatal(const char *, ...)
251 __attribute__((__format__(__printf__
, 1, 2)));
252 void trace(int, const char *, ...)
253 __attribute__((__format__(__printf__
, 2, 3)));
254 void tracet(int, const char *, ...)
255 __attribute__((__format__(__printf__
, 2, 3)));
256 unsigned int if_maxindex(void);
257 struct ifc
*ifc_find(char *);
258 struct iff
*iff_find(struct ifc
*, int);
259 void setindex2ifc(int, struct ifc
*);
261 #define MALLOC(type) ((type *)malloc(sizeof(type)))
264 main(int argc
, char **argv
)
269 sigset_t mask
, omask
;
274 progname
= strrchr(*argv
, '/');
281 while ((ch
= getopt(argc
, argv
, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
288 if (nfilter
>= MAXFILTER
) {
289 fatal("Exceeds MAXFILTER");
292 filtertype
[nfilter
] = ch
;
293 filter
[nfilter
++] = allocopy(optarg
);
297 routetag
= strtoul(optarg
, &ep
, 0);
298 if (!ep
|| *ep
!= '\0' || (routetag
& ~0xffff) != 0) {
299 fatal("invalid route tag");
304 if ((rtlog
= fopen(optarg
, "w")) == NULL
) {
305 fatal("Can not write to routelog");
309 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
310 FLAG('a', aflag
, 1); break;
311 FLAG('d', dflag
, 1); break;
312 FLAG('D', dflag
, 2); break;
313 FLAG('h', hflag
, 1); break;
314 FLAG('l', lflag
, 1); break;
315 FLAG('n', nflag
, 1); break;
316 FLAG('q', qflag
, 1); break;
317 FLAG('s', sflag
, 1); break;
318 FLAG('S', Sflag
, 1); break;
321 fatal("Invalid option specified, terminating");
328 fatal("bogus extra arguments");
334 fprintf(stderr
, "No kernel update is allowed\n");
336 openlog(progname
, LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
339 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
)
341 memset(ripbuf
, 0, RIP6_MAXMTU
);
342 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
343 ripbuf
->rip6_vers
= RIP6_VERSION
;
344 ripbuf
->rip6_res1
[0] = 0;
345 ripbuf
->rip6_res1
[1] = 0;
349 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
350 if (ifcp
->ifc_index
< 0) {
352 "No ifindex found at %s (no link-local address?)\n",
359 if (loopifcp
== NULL
) {
360 fatal("No loopback found");
363 loopifindex
= loopifcp
->ifc_index
;
364 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
)
373 if (daemon(0, 0) < 0) {
387 if ((pidfile
= fopen(ROUTE6D_PID
, "w")) != NULL
) {
388 fprintf(pidfile
, "%d\n", pid
);
392 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
) {
396 memset(ripbuf
, 0, RIP6_MAXMTU
);
397 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
398 ripbuf
->rip6_vers
= RIP6_VERSION
;
399 ripbuf
->rip6_res1
[0] = 0;
400 ripbuf
->rip6_res1
[1] = 0;
402 if (signal(SIGALRM
, sighandler
) == SIG_ERR
||
403 signal(SIGQUIT
, sighandler
) == SIG_ERR
||
404 signal(SIGTERM
, sighandler
) == SIG_ERR
||
405 signal(SIGUSR1
, sighandler
) == SIG_ERR
||
406 signal(SIGHUP
, sighandler
) == SIG_ERR
||
407 signal(SIGINT
, sighandler
) == SIG_ERR
) {
412 * To avoid rip packet congestion (not on a cable but in this
413 * process), wait for a moment to send the first RIP6_RESPONSE
416 alarm(ripinterval(INIT_INTERVAL6
));
418 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
419 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
423 syslog(LOG_INFO
, "**** Started ****");
425 sigaddset(&mask
, SIGALRM
);
445 FD_COPY(&sockvec
, &recvec
);
447 switch (select(FD_SETSIZE
, &recvec
, 0, 0, 0)) {
449 if (errno
!= EINTR
) {
457 if (FD_ISSET(ripsock
, &recvec
)) {
458 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
460 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
462 if (FD_ISSET(rtsock
, &recvec
)) {
463 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
465 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
493 * gracefully exits after resetting sockopts.
502 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
503 if (rrt
->rrt_rflags
& RRTF_AGGREGATE
) {
504 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
509 syslog(LOG_INFO
, "**** Terminated ****");
515 * Called periodically:
516 * 1. age out the learned route. remove it if necessary.
517 * 2. submit RIP6_RESPONSE packets.
518 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
519 * to invoke this function in every 1 or 5 or 10 seconds only to age the
520 * routes more precisely.
527 struct riprt
*rrt
, *rrt_prev
, *rrt_next
;
528 time_t t_lifetime
, t_holddown
;
530 /* age the RIP routes */
532 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
533 t_holddown
= t_lifetime
- RIP_HOLDDOWN
;
534 for (rrt
= riprt
; rrt
; rrt
= rrt_next
) {
535 rrt_next
= rrt
->rrt_next
;
537 if (rrt
->rrt_t
== 0) {
541 if (rrt
->rrt_t
< t_holddown
) {
543 rrt_prev
->rrt_next
= rrt
->rrt_next
;
545 riprt
= rrt
->rrt_next
;
547 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
551 if (rrt
->rrt_t
< t_lifetime
)
552 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
556 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
557 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
558 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
560 alarm(ripinterval(SUPPLY_INTERVAL6
));
566 int i
, int0
, int255
, error
;
567 struct addrinfo hints
, *res
;
572 nindex2ifc
= 0; /*initial guess*/
574 snprintf(port
, sizeof(port
), "%d", RIP6_PORT
);
576 memset(&hints
, 0, sizeof(hints
));
577 hints
.ai_family
= PF_INET6
;
578 hints
.ai_socktype
= SOCK_DGRAM
;
579 hints
.ai_flags
= AI_PASSIVE
;
580 error
= getaddrinfo(NULL
, port
, &hints
, &res
);
582 fatal("%s", gai_strerror(error
));
586 fatal(":: resolved to multiple address");
590 int0
= 0; int255
= 255;
591 ripsock
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
596 if (bind(ripsock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
600 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
601 &int255
, sizeof(int255
)) < 0) {
602 fatal("rip IPV6_MULTICAST_HOPS");
605 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
606 &int0
, sizeof(int0
)) < 0) {
607 fatal("rip IPV6_MULTICAST_LOOP");
612 #ifdef IPV6_RECVPKTINFO
613 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &i
,
615 fatal("rip IPV6_RECVPKTINFO");
618 #else /* old adv. API */
619 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &i
,
621 fatal("rip IPV6_PKTINFO");
626 memset(&hints
, 0, sizeof(hints
));
627 hints
.ai_family
= PF_INET6
;
628 hints
.ai_socktype
= SOCK_DGRAM
;
629 error
= getaddrinfo(RIP6_DEST
, port
, &hints
, &res
);
631 fatal("%s", gai_strerror(error
));
635 fatal("%s resolved to multiple address", RIP6_DEST
);
638 memcpy(&ripsin
, res
->ai_addr
, res
->ai_addrlen
);
643 memset(&sockvec
, 0, sizeof(sockvec
));
645 FD_SET(ripsock
, &sockvec
);
648 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
649 fatal("route socket");
652 FD_SET(rtsock
, &sockvec
);
654 rtsock
= -1; /*just for safety */
658 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
661 * ripflush flushes the rip datagram stored in the rip buffer
664 static struct netinfo6
*np
;
667 ripflush(struct ifc
*ifcp
, struct sockaddr_in6
*sin
)
673 tracet(1, "Send(%s): info(%d) to %s.%d\n",
675 inet6_n2p(&sin
->sin6_addr
), ntohs(sin
->sin6_port
));
677 tracet(1, "Send: info(%d) to %s.%d\n",
678 nrt
, inet6_n2p(&sin
->sin6_addr
), ntohs(sin
->sin6_port
));
680 np
= ripbuf
->rip6_nets
;
681 for (i
= 0; i
< nrt
; i
++, np
++) {
682 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
683 if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
))
684 trace(2, " NextHop reset");
686 trace(2, " NextHop %s",
687 inet6_n2p(&np
->rip6_dest
));
690 trace(2, " %s/%d[%d]",
691 inet6_n2p(&np
->rip6_dest
),
692 np
->rip6_plen
, np
->rip6_metric
);
695 trace(2, " tag=0x%04x",
696 ntohs(np
->rip6_tag
) & 0xffff);
701 error
= sendpacket(sin
, RIPSIZE(nrt
));
702 if (error
== EAFNOSUPPORT
) {
703 /* Protocol not supported */
704 tracet(1, "Could not send info to %s (%s): "
706 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
707 ifcp
->ifc_flags
&= ~IFF_UP
; /* As if down for AF_INET6 */
709 nrt
= 0; np
= ripbuf
->rip6_nets
;
713 * Generate RIP6_RESPONSE packets and send them.
716 ripsend(struct ifc
*ifcp
, struct sockaddr_in6
*sin
, int flag
)
719 struct in6_addr
*nh
; /* next hop */
724 * Request from non-link local address is not
725 * a regular route6d update.
727 maxrte
= (IFMINMTU
- sizeof(struct ip6_hdr
) -
728 sizeof(struct udphdr
) -
729 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
730 sizeof(struct netinfo6
);
731 nrt
= 0; np
= ripbuf
->rip6_nets
; nh
= NULL
;
732 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
733 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
735 /* Put the route to the buffer */
743 if (nrt
) /* Send last packet */
748 if ((flag
& RRTF_SENDANYWAY
) == 0 &&
749 (qflag
|| (ifcp
->ifc_flags
& IFF_LOOPBACK
)))
753 if (iff_find(ifcp
, 'N') != NULL
)
756 /* -T: generate default route only */
757 if (iff_find(ifcp
, 'T') != NULL
) {
758 struct netinfo6 rrt_info
;
759 memset(&rrt_info
, 0, sizeof(struct netinfo6
));
760 rrt_info
.rip6_dest
= in6addr_any
;
761 rrt_info
.rip6_plen
= 0;
762 rrt_info
.rip6_metric
= 1;
763 rrt_info
.rip6_metric
+= ifcp
->ifc_metric
;
764 rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
765 np
= ripbuf
->rip6_nets
;
772 maxrte
= (ifcp
->ifc_mtu
- sizeof(struct ip6_hdr
) -
773 sizeof(struct udphdr
) -
774 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
775 sizeof(struct netinfo6
);
777 nrt
= 0; np
= ripbuf
->rip6_nets
; nh
= NULL
;
778 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
779 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
782 /* Need to check filter here */
783 if (out_filter(rrt
, ifcp
) == 0)
786 /* Check split horizon and other conditions */
787 if (tobeadv(rrt
, ifcp
) == 0)
790 /* Only considers the routes with flag if specified */
791 if ((flag
& RRTF_CHANGED
) &&
792 (rrt
->rrt_rflags
& RRTF_CHANGED
) == 0)
796 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
797 !IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_gw
) &&
798 (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
) == 0) {
799 if (nh
== NULL
|| !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
)) {
800 if (nrt
== maxrte
- 2)
802 np
->rip6_dest
= rrt
->rrt_gw
;
803 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
))
804 SET_IN6_LINKLOCAL_IFINDEX(np
->rip6_dest
, 0);
807 np
->rip6_metric
= NEXTHOP_METRIC
;
811 } else if (nh
&& (rrt
->rrt_index
!= ifcp
->ifc_index
||
812 !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
) ||
813 rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)) {
815 if (nrt
== maxrte
- 2)
817 memset(np
, 0, sizeof(struct netinfo6
));
818 np
->rip6_metric
= NEXTHOP_METRIC
;
823 /* Put the route to the buffer */
831 if (nrt
) /* Send last packet */
836 * outbound filter logic, per-route/interface.
839 out_filter(struct riprt
*rrt
, struct ifc
*ifcp
)
846 * -A: filter out less specific routes, if we have aggregated
849 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
850 if (iffp
->iff_type
!= 'A')
852 if (rrt
->rrt_info
.rip6_plen
<= iffp
->iff_plen
)
854 ia
= rrt
->rrt_info
.rip6_dest
;
855 applyplen(&ia
, iffp
->iff_plen
);
856 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
))
861 * if it is an aggregated route, advertise it only to the
862 * interfaces specified on -A.
864 if ((rrt
->rrt_rflags
& RRTF_AGGREGATE
) != 0) {
866 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
867 if (iffp
->iff_type
!= 'A')
869 if (rrt
->rrt_info
.rip6_plen
== iffp
->iff_plen
&&
870 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
881 * -O: advertise only if prefix matches the configured prefix.
883 if (iff_find(ifcp
, 'O')) {
885 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
886 if (iffp
->iff_type
!= 'O')
888 if (rrt
->rrt_info
.rip6_plen
< iffp
->iff_plen
)
890 ia
= rrt
->rrt_info
.rip6_dest
;
891 applyplen(&ia
, iffp
->iff_plen
);
892 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
901 /* the prefix should be advertised */
906 * Determine if the route is to be advertised on the specified interface.
907 * It checks options specified in the arguments and the split horizon rule.
910 tobeadv(struct riprt
*rrt
, struct ifc
*ifcp
)
913 /* Special care for static routes */
914 if (rrt
->rrt_flags
& RTF_STATIC
) {
915 /* XXX don't advertise reject/blackhole routes */
916 if (rrt
->rrt_flags
& (RTF_REJECT
| RTF_BLACKHOLE
))
919 if (Sflag
) /* Yes, advertise it anyway */
921 if (sflag
&& rrt
->rrt_index
!= ifcp
->ifc_index
)
925 /* Regular split horizon */
926 if (hflag
== 0 && rrt
->rrt_index
== ifcp
->ifc_index
)
932 * Send a rip packet actually.
935 sendpacket(struct sockaddr_in6
*sin
, int len
)
938 * MSG_DONTROUTE should not be specified when it responds with a
939 * RIP6_REQUEST message. SO_DONTROUTE has been specified to
946 struct in6_pktinfo
*pi
;
948 struct sockaddr_in6 sincopy
;
950 /* do not overwrite the given sin */
954 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)
955 || IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
)) {
956 idx
= IN6_LINKLOCAL_IFINDEX(sin
->sin6_addr
);
957 SET_IN6_LINKLOCAL_IFINDEX(sin
->sin6_addr
, 0);
961 m
.msg_name
= (caddr_t
)sin
;
962 m
.msg_namelen
= sizeof(*sin
);
963 iov
[0].iov_base
= (caddr_t
)ripbuf
;
964 iov
[0].iov_len
= len
;
968 m
.msg_control
= NULL
;
969 m
.msg_controllen
= 0;
971 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
972 cm
= (struct cmsghdr
*)cmsgbuf
;
973 m
.msg_control
= (caddr_t
)cm
;
974 m
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
976 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
977 cm
->cmsg_level
= IPPROTO_IPV6
;
978 cm
->cmsg_type
= IPV6_PKTINFO
;
979 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
980 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*::*/
981 pi
->ipi6_ifindex
= idx
;
984 if (sendmsg(ripsock
, &m
, 0 /*MSG_DONTROUTE*/) < 0) {
985 trace(1, "sendmsg: %s\n", strerror(errno
));
993 * Receive and process RIP packets. Update the routes/kernel forwarding
994 * table if necessary.
999 struct ifc
*ifcp
, *ic
;
1000 struct sockaddr_in6 fsock
;
1001 struct in6_addr nh
; /* next hop */
1003 struct netinfo6
*np
, *nq
;
1005 int len
, nn
, need_trigger
, idx
;
1006 char buf
[4 * RIP6_MAXMTU
];
1010 struct iovec iov
[2];
1011 u_char cmsgbuf
[256];
1012 struct in6_pktinfo
*pi
;
1016 time_t t_half_lifetime
;
1020 m
.msg_name
= (caddr_t
)&fsock
;
1021 m
.msg_namelen
= sizeof(fsock
);
1022 iov
[0].iov_base
= (caddr_t
)buf
;
1023 iov
[0].iov_len
= sizeof(buf
);
1026 cm
= (struct cmsghdr
*)cmsgbuf
;
1027 m
.msg_control
= (caddr_t
)cm
;
1028 m
.msg_controllen
= sizeof(cmsgbuf
);
1029 if ((len
= recvmsg(ripsock
, &m
, 0)) < 0) {
1034 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&m
);
1036 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&m
, cm
)) {
1037 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1038 cm
->cmsg_type
== IPV6_PKTINFO
) {
1039 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
1040 idx
= pi
->ipi6_ifindex
;
1044 if (idx
&& IN6_IS_ADDR_LINKLOCAL(&fsock
.sin6_addr
))
1045 SET_IN6_LINKLOCAL_IFINDEX(fsock
.sin6_addr
, idx
);
1047 nh
= fsock
.sin6_addr
;
1048 nn
= (len
- sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
1049 sizeof(struct netinfo6
);
1050 rp
= (struct rip6
*)buf
;
1053 if (rp
->rip6_vers
!= RIP6_VERSION
) {
1054 trace(1, "Incorrect RIP version %d\n", rp
->rip6_vers
);
1057 if (rp
->rip6_cmd
== RIP6_REQUEST
) {
1058 if (idx
&& idx
< nindex2ifc
) {
1059 ifcp
= index2ifc
[idx
];
1060 riprequest(ifcp
, np
, nn
, &fsock
);
1062 riprequest(NULL
, np
, nn
, &fsock
);
1067 if (!IN6_IS_ADDR_LINKLOCAL(&fsock
.sin6_addr
)) {
1068 trace(1, "Packets from non-ll addr: %s\n",
1069 inet6_n2p(&fsock
.sin6_addr
));
1070 return; /* Ignore packets from non-link-local addr */
1072 idx
= IN6_LINKLOCAL_IFINDEX(fsock
.sin6_addr
);
1073 ifcp
= (idx
< nindex2ifc
) ? index2ifc
[idx
] : NULL
;
1075 trace(1, "Packets to unknown interface index %d\n", idx
);
1076 return; /* Ignore it */
1078 if (IN6_ARE_ADDR_EQUAL(&ifcp
->ifc_mylladdr
, &fsock
.sin6_addr
))
1079 return; /* The packet is from me; ignore */
1080 if (rp
->rip6_cmd
!= RIP6_RESPONSE
) {
1081 trace(1, "Invalid command %d\n", rp
->rip6_cmd
);
1086 if (iff_find(ifcp
, 'N') != NULL
)
1089 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1090 ifcp
->ifc_name
, inet6_n2p(&nh
), ntohs(fsock
.sin6_port
), nn
);
1093 t_half_lifetime
= t
- (RIP_LIFETIME
/2);
1094 for (; nn
; nn
--, np
++) {
1095 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
1096 /* modify neighbor address */
1097 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1099 SET_IN6_LINKLOCAL_IFINDEX(nh
, idx
);
1100 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1101 } else if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
)) {
1102 nh
= fsock
.sin6_addr
;
1103 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1105 nh
= fsock
.sin6_addr
;
1106 trace(1, "\tInvalid Nexthop: %s\n",
1107 inet6_n2p(&np
->rip6_dest
));
1111 if (IN6_IS_ADDR_MULTICAST(&np
->rip6_dest
)) {
1112 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1113 inet6_n2p(&np
->rip6_dest
),
1114 np
->rip6_plen
, np
->rip6_metric
);
1117 if (IN6_IS_ADDR_LOOPBACK(&np
->rip6_dest
)) {
1118 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1119 inet6_n2p(&np
->rip6_dest
),
1120 np
->rip6_plen
, np
->rip6_metric
);
1123 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1124 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1125 inet6_n2p(&np
->rip6_dest
),
1126 np
->rip6_plen
, np
->rip6_metric
);
1129 /* may need to pass sitelocal prefix in some case, however*/
1130 if (IN6_IS_ADDR_SITELOCAL(&np
->rip6_dest
) && !lflag
) {
1131 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1132 inet6_n2p(&np
->rip6_dest
),
1133 np
->rip6_plen
, np
->rip6_metric
);
1136 trace(2, "\tnetinfo6: %s/%d [%d]",
1137 inet6_n2p(&np
->rip6_dest
),
1138 np
->rip6_plen
, np
->rip6_metric
);
1140 trace(2, " tag=0x%04x", ntohs(np
->rip6_tag
) & 0xffff);
1143 applyplen(&ia
, np
->rip6_plen
);
1144 if (!IN6_ARE_ADDR_EQUAL(&ia
, &np
->rip6_dest
))
1145 trace(2, " [junk outside prefix]");
1149 * -L: listen only if the prefix matches the configuration
1151 ok
= 1; /* if there's no L filter, it is ok */
1152 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
1153 if (iffp
->iff_type
!= 'L')
1156 if (np
->rip6_plen
< iffp
->iff_plen
)
1158 /* special rule: ::/0 means default, not "in /0" */
1159 if (iffp
->iff_plen
== 0 && np
->rip6_plen
> 0)
1162 applyplen(&ia
, iffp
->iff_plen
);
1163 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
1169 trace(2, " (filtered)\n");
1175 np
->rip6_metric
+= ifcp
->ifc_metric
;
1176 if (np
->rip6_metric
> HOPCNT_INFINITY6
)
1177 np
->rip6_metric
= HOPCNT_INFINITY6
;
1179 applyplen(&np
->rip6_dest
, np
->rip6_plen
);
1180 if ((rrt
= rtsearch(np
, NULL
)) != NULL
) {
1181 if (rrt
->rrt_t
== 0)
1182 continue; /* Intf route has priority */
1183 nq
= &rrt
->rrt_info
;
1184 if (nq
->rip6_metric
> np
->rip6_metric
) {
1185 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1186 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1187 /* Small metric from the same gateway */
1188 nq
->rip6_metric
= np
->rip6_metric
;
1190 /* Better route found */
1191 rrt
->rrt_index
= ifcp
->ifc_index
;
1192 /* Update routing table */
1193 delroute(nq
, &rrt
->rrt_gw
);
1196 addroute(rrt
, &nh
, ifcp
);
1198 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1201 } else if (nq
->rip6_metric
< np
->rip6_metric
&&
1202 rrt
->rrt_index
== ifcp
->ifc_index
&&
1203 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1204 /* Got worse route from same gw */
1205 nq
->rip6_metric
= np
->rip6_metric
;
1207 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1209 } else if (nq
->rip6_metric
== np
->rip6_metric
&&
1210 np
->rip6_metric
< HOPCNT_INFINITY6
) {
1211 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1212 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1213 /* same metric, same route from same gw */
1215 } else if (rrt
->rrt_t
< t_half_lifetime
) {
1216 /* Better route found */
1217 rrt
->rrt_index
= ifcp
->ifc_index
;
1218 /* Update routing table */
1219 delroute(nq
, &rrt
->rrt_gw
);
1222 addroute(rrt
, &nh
, ifcp
);
1223 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1228 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1229 * do not update age value. Do nothing.
1231 } else if (np
->rip6_metric
< HOPCNT_INFINITY6
) {
1232 /* Got a new valid route */
1233 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
1234 fatal("malloc: struct riprt");
1237 memset(rrt
, 0, sizeof(*rrt
));
1238 nq
= &rrt
->rrt_info
;
1240 rrt
->rrt_same
= NULL
;
1241 rrt
->rrt_index
= ifcp
->ifc_index
;
1242 rrt
->rrt_flags
= RTF_UP
|RTF_GATEWAY
;
1245 applyplen(&nq
->rip6_dest
, nq
->rip6_plen
);
1246 if (nq
->rip6_plen
== sizeof(struct in6_addr
) * 8)
1247 rrt
->rrt_flags
|= RTF_HOST
;
1249 /* Put the route to the list */
1250 rrt
->rrt_next
= riprt
;
1252 /* Update routing table */
1253 addroute(rrt
, &nh
, ifcp
);
1254 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1259 /* XXX need to care the interval between triggered updates */
1261 if (nextalarm
> time(NULL
) + RIP_TRIG_INT6_MAX
) {
1262 for (ic
= ifc
; ic
; ic
= ic
->ifc_next
) {
1263 if (ifcp
->ifc_index
== ic
->ifc_index
)
1265 if (ic
->ifc_flags
& IFF_UP
)
1266 ripsend(ic
, &ic
->ifc_ripsin
,
1270 /* Reset the flag */
1271 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
)
1272 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1277 * Send all routes request packet to the specified interface.
1280 sendrequest(struct ifc
*ifcp
)
1282 struct netinfo6
*np
;
1285 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
1287 ripbuf
->rip6_cmd
= RIP6_REQUEST
;
1288 np
= ripbuf
->rip6_nets
;
1289 memset(np
, 0, sizeof(struct netinfo6
));
1290 np
->rip6_metric
= HOPCNT_INFINITY6
;
1291 tracet(1, "Send rtdump Request to %s (%s)\n",
1292 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1293 error
= sendpacket(&ifcp
->ifc_ripsin
, RIPSIZE(1));
1294 if (error
== EAFNOSUPPORT
) {
1295 /* Protocol not supported */
1296 tracet(1, "Could not send rtdump Request to %s (%s): "
1297 "set IFF_UP to 0\n",
1298 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1299 ifcp
->ifc_flags
&= ~IFF_UP
; /* As if down for AF_INET6 */
1301 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
1305 * Process a RIP6_REQUEST packet.
1308 riprequest(struct ifc
*ifcp
, struct netinfo6
*np
, int nn
,
1309 struct sockaddr_in6
*sin
)
1314 if (!(nn
== 1 && IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
) &&
1315 np
->rip6_plen
== 0 && np
->rip6_metric
== HOPCNT_INFINITY6
)) {
1316 /* Specific response, don't split-horizon */
1317 trace(1, "\tRIP Request\n");
1318 for (i
= 0; i
< nn
; i
++, np
++) {
1319 rrt
= rtsearch(np
, NULL
);
1321 np
->rip6_metric
= rrt
->rrt_info
.rip6_metric
;
1323 np
->rip6_metric
= HOPCNT_INFINITY6
;
1325 sendpacket(sin
, RIPSIZE(nn
));
1328 /* Whole routing table dump */
1329 trace(1, "\tRIP Request -- whole routing table\n");
1330 ripsend(ifcp
, sin
, RRTF_SENDANYWAY
);
1334 * Get information of each interface.
1339 struct ifaddrs
*ifap
, *ifa
;
1341 struct ipv6_mreq mreq
;
1344 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1349 if (getifaddrs(&ifap
) != 0) {
1350 fatal("getifaddrs");
1354 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1355 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1357 ifcp
= ifc_find(ifa
->ifa_name
);
1358 /* we are interested in multicast-capable interfaces */
1359 if ((ifa
->ifa_flags
& IFF_MULTICAST
) == 0)
1363 if ((ifcp
= MALLOC(struct ifc
)) == NULL
) {
1364 fatal("malloc: struct ifc");
1367 memset(ifcp
, 0, sizeof(*ifcp
));
1368 ifcp
->ifc_index
= -1;
1369 ifcp
->ifc_next
= ifc
;
1372 ifcp
->ifc_name
= allocopy(ifa
->ifa_name
);
1373 ifcp
->ifc_addr
= NULL
;
1374 ifcp
->ifc_filter
= NULL
;
1375 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1376 trace(1, "newif %s <%s>\n", ifcp
->ifc_name
,
1377 ifflags(ifcp
->ifc_flags
));
1378 if (!strcmp(ifcp
->ifc_name
, LOOPBACK_IF
))
1381 /* update flag, this may be up again */
1382 if (ifcp
->ifc_flags
!= ifa
->ifa_flags
) {
1383 trace(1, "%s: <%s> -> ", ifcp
->ifc_name
,
1384 ifflags(ifcp
->ifc_flags
));
1385 trace(1, "<%s>\n", ifflags(ifa
->ifa_flags
));
1386 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1388 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1390 ifconfig1(ifa
->ifa_name
, ifa
->ifa_addr
, ifcp
, s
);
1391 if ((ifcp
->ifc_flags
& (IFF_LOOPBACK
| IFF_UP
)) == IFF_UP
1392 && 0 < ifcp
->ifc_index
&& !ifcp
->ifc_joined
) {
1393 mreq
.ipv6mr_multiaddr
= ifcp
->ifc_ripsin
.sin6_addr
;
1394 mreq
.ipv6mr_interface
= ifcp
->ifc_index
;
1395 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1396 &mreq
, sizeof(mreq
)) < 0) {
1397 fatal("IPV6_JOIN_GROUP");
1400 trace(1, "join %s %s\n", ifcp
->ifc_name
, RIP6_DEST
);
1409 ifconfig1(const char *name
, const struct sockaddr
*sa
, struct ifc
*ifcp
, int s
)
1411 struct in6_ifreq ifr
;
1412 const struct sockaddr_in6
*sin
;
1417 sin
= (const struct sockaddr_in6
*)sa
;
1418 ifr
.ifr_addr
= *sin
;
1419 strcpy(ifr
.ifr_name
, name
);
1420 if (ioctl(s
, SIOCGIFNETMASK_IN6
, (char *)&ifr
) < 0) {
1421 fatal("ioctl: SIOCGIFNETMASK_IN6");
1424 plen
= sin6mask2len(&ifr
.ifr_addr
);
1425 if ((ifa
= ifa_match(ifcp
, &sin
->sin6_addr
, plen
)) != NULL
) {
1426 /* same interface found */
1427 /* need check if something changed */
1428 /* XXX not yet implemented */
1432 * New address is found
1434 if ((ifa
= MALLOC(struct ifac
)) == NULL
) {
1435 fatal("malloc: struct ifac");
1438 memset(ifa
, 0, sizeof(*ifa
));
1439 ifa
->ifa_conf
= ifcp
;
1440 ifa
->ifa_next
= ifcp
->ifc_addr
;
1441 ifcp
->ifc_addr
= ifa
;
1442 ifa
->ifa_addr
= sin
->sin6_addr
;
1443 ifa
->ifa_plen
= plen
;
1444 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1445 ifr
.ifr_addr
= *sin
;
1446 if (ioctl(s
, SIOCGIFDSTADDR_IN6
, (char *)&ifr
) < 0) {
1447 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1450 ifa
->ifa_raddr
= ifr
.ifr_dstaddr
.sin6_addr
;
1451 inet_ntop(AF_INET6
, (void *)&ifa
->ifa_raddr
, buf
, sizeof(buf
));
1452 trace(1, "found address %s/%d -- %s\n",
1453 inet6_n2p(&ifa
->ifa_addr
), ifa
->ifa_plen
, buf
);
1455 trace(1, "found address %s/%d\n",
1456 inet6_n2p(&ifa
->ifa_addr
), ifa
->ifa_plen
);
1458 if (ifcp
->ifc_index
< 0 && IN6_IS_ADDR_LINKLOCAL(&ifa
->ifa_addr
)) {
1459 ifcp
->ifc_mylladdr
= ifa
->ifa_addr
;
1460 ifcp
->ifc_index
= IN6_LINKLOCAL_IFINDEX(ifa
->ifa_addr
);
1461 memcpy(&ifcp
->ifc_ripsin
, &ripsin
, ripsin
.ss_len
);
1462 SET_IN6_LINKLOCAL_IFINDEX(ifcp
->ifc_ripsin
.sin6_addr
,
1464 setindex2ifc(ifcp
->ifc_index
, ifcp
);
1465 ifcp
->ifc_mtu
= getifmtu(ifcp
->ifc_index
);
1466 if (ifcp
->ifc_mtu
> RIP6_MAXMTU
)
1467 ifcp
->ifc_mtu
= RIP6_MAXMTU
;
1468 if (ioctl(s
, SIOCGIFMETRIC
, (char *)&ifr
) < 0) {
1469 fatal("ioctl: SIOCGIFMETRIC");
1472 ifcp
->ifc_metric
= ifr
.ifr_metric
;
1473 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1474 ifcp
->ifc_index
, ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
1476 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1480 * Receive and process routing messages.
1481 * Update interface information as necesssary.
1488 struct rt_msghdr
*rtm
;
1489 struct ifa_msghdr
*ifam
;
1490 struct if_msghdr
*ifm
;
1492 struct ifc
*ifcp
, *ic
;
1493 int iface
= 0, rtable
= 0;
1494 struct sockaddr_in6
*rta
[RTAX_MAX
];
1495 struct sockaddr_in6 mask
;
1499 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
1500 perror("read from rtsock");
1503 if (len
< sizeof(*rtm
)) {
1504 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1505 len
, (u_long
)sizeof(*rtm
));
1509 for (p
= buf
; p
- buf
< len
; p
+= ((struct rt_msghdr
*)p
)->rtm_msglen
) {
1510 /* safety against bogus message */
1511 if (((struct rt_msghdr
*)p
)->rtm_msglen
<= 0) {
1512 trace(1, "bogus rtmsg: length=%d\n",
1513 ((struct rt_msghdr
*)p
)->rtm_msglen
);
1519 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1522 ifam
= (struct ifa_msghdr
*)p
;
1523 addrs
= ifam
->ifam_addrs
;
1524 q
= (char *)(ifam
+ 1);
1527 ifm
= (struct if_msghdr
*)p
;
1528 addrs
= ifm
->ifm_addrs
;
1529 q
= (char *)(ifm
+ 1);
1532 rtm
= (struct rt_msghdr
*)p
;
1533 addrs
= rtm
->rtm_addrs
;
1534 q
= (char *)(rtm
+ 1);
1535 if (rtm
->rtm_version
!= RTM_VERSION
) {
1536 trace(1, "unexpected rtmsg version %d "
1538 rtm
->rtm_version
, RTM_VERSION
);
1541 if (rtm
->rtm_pid
== pid
) {
1543 trace(1, "rtmsg looped back to me, ignored\n");
1549 memset(&rta
, 0, sizeof(rta
));
1550 for (i
= 0; i
< RTAX_MAX
; i
++) {
1551 if (addrs
& (1 << i
)) {
1552 rta
[i
] = (struct sockaddr_in6
*)q
;
1553 q
+= RT_ROUNDUP(rta
[i
]->sin6_len
);
1557 trace(1, "rtsock: %s (addrs=%x)\n",
1558 rttypes((struct rt_msghdr
*)p
), addrs
);
1561 i
< ((struct rt_msghdr
*)p
)->rtm_msglen
;
1563 fprintf(stderr
, "%02x ", p
[i
] & 0xff);
1564 if (i
% 16 == 15) fprintf(stderr
, "\n");
1566 fprintf(stderr
, "\n");
1572 * We may be able to optimize by using ifm->ifm_index or
1573 * ifam->ifam_index. For simplicity we don't do that here.
1575 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1588 /* nothing to be done here */
1589 trace(1, "\tnothing to be done, ignored\n");
1594 if (rta
[RTAX_DST
] == NULL
) {
1595 trace(1, "\tno destination, ignored\n");
1598 if (rta
[RTAX_DST
]->sin6_family
!= AF_INET6
) {
1599 trace(1, "\taf mismatch, ignored\n");
1602 if (IN6_IS_ADDR_LINKLOCAL(&rta
[RTAX_DST
]->sin6_addr
)) {
1603 trace(1, "\tlinklocal destination, ignored\n");
1606 if (IN6_ARE_ADDR_EQUAL(&rta
[RTAX_DST
]->sin6_addr
, &in6addr_loopback
)) {
1607 trace(1, "\tloopback destination, ignored\n");
1608 continue; /* Loopback */
1610 if (IN6_IS_ADDR_MULTICAST(&rta
[RTAX_DST
]->sin6_addr
)) {
1611 trace(1, "\tmulticast destination, ignored\n");
1617 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1626 /* should already be handled */
1627 fatal("rtrecv: never reach here");
1630 if (!rta
[RTAX_DST
] || !rta
[RTAX_GATEWAY
]) {
1631 trace(1, "\tsome of dst/gw/netamsk are "
1632 "unavailable, ignored\n");
1635 if ((rtm
->rtm_flags
& RTF_HOST
) != 0) {
1636 mask
.sin6_len
= sizeof(mask
);
1637 memset(&mask
.sin6_addr
, 0xff,
1638 sizeof(mask
.sin6_addr
));
1639 rta
[RTAX_NETMASK
] = &mask
;
1640 } else if (!rta
[RTAX_NETMASK
]) {
1641 trace(1, "\tsome of dst/gw/netamsk are "
1642 "unavailable, ignored\n");
1645 if (rt_del(rta
[RTAX_DST
], rta
[RTAX_GATEWAY
],
1646 rta
[RTAX_NETMASK
]) == 0) {
1647 rtable
++; /*just to be sure*/
1652 trace(1, "\tnot supported yet, ignored\n");
1655 if (!rta
[RTAX_NETMASK
] || !rta
[RTAX_IFA
]) {
1656 trace(1, "\tno netmask or ifa given, ignored\n");
1659 if (ifam
->ifam_index
< nindex2ifc
)
1660 ifcp
= index2ifc
[ifam
->ifam_index
];
1664 trace(1, "\tinvalid ifam_index %d, ignored\n",
1668 if (!rt_deladdr(ifcp
, rta
[RTAX_IFA
], rta
[RTAX_NETMASK
]))
1672 trace(1, "\tnot supported yet, ignored\n");
1679 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1681 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
)
1682 if (ifcp
->ifc_cflags
& IFC_CHANGED
) {
1683 if (ifrt(ifcp
, 1)) {
1684 for (ic
= ifc
; ic
; ic
= ic
->ifc_next
) {
1685 if (ifcp
->ifc_index
== ic
->ifc_index
)
1687 if (ic
->ifc_flags
& IFF_UP
)
1688 ripsend(ic
, &ic
->ifc_ripsin
,
1691 /* Reset the flag */
1692 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
)
1693 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1695 ifcp
->ifc_cflags
&= ~IFC_CHANGED
;
1699 trace(1, "rtsock: read routing table again\n");
1705 * remove specified route from the internal routing table.
1708 rt_del(const struct sockaddr_in6
*sdst
, const struct sockaddr_in6
*sgw
,
1709 const struct sockaddr_in6
*smask
)
1711 const struct in6_addr
*dst
= NULL
;
1712 const struct in6_addr
*gw
= NULL
;
1714 struct netinfo6 ni6
;
1715 struct riprt
*rrt
= NULL
;
1718 if (sdst
->sin6_family
!= AF_INET6
) {
1719 trace(1, "\tother AF, ignored\n");
1722 if (IN6_IS_ADDR_LINKLOCAL(&sdst
->sin6_addr
)
1723 || IN6_ARE_ADDR_EQUAL(&sdst
->sin6_addr
, &in6addr_loopback
)
1724 || IN6_IS_ADDR_MULTICAST(&sdst
->sin6_addr
)) {
1725 trace(1, "\taddress %s not interesting, ignored\n",
1726 inet6_n2p(&sdst
->sin6_addr
));
1729 dst
= &sdst
->sin6_addr
;
1730 if (sgw
->sin6_family
== AF_INET6
) {
1732 gw
= &sgw
->sin6_addr
;
1733 prefix
= sin6mask2len(smask
);
1734 } else if (sgw
->sin6_family
== AF_LINK
) {
1736 * Interface route... a hard case. We need to get the prefix
1737 * length from the kernel, but we now are parsing rtmsg.
1738 * We'll purge matching routes from my list, then get the
1741 struct riprt
*longest
;
1742 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1745 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
1746 if (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
1748 && IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)) {
1750 || longest
->rrt_info
.rip6_plen
<
1751 rrt
->rrt_info
.rip6_plen
) {
1758 trace(1, "\tno matching interface route found\n");
1761 gw
= &in6addr_loopback
;
1762 prefix
= rrt
->rrt_info
.rip6_plen
;
1764 trace(1, "\tunsupported af: (gw=%d)\n", sgw
->sin6_family
);
1768 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst
), prefix
);
1769 trace(1, "gw %s\n", inet6_n2p(gw
));
1770 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
1771 /* age route for interface address */
1772 memset(&ni6
, 0, sizeof(ni6
));
1773 ni6
.rip6_dest
= *dst
;
1774 ni6
.rip6_plen
= prefix
;
1775 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
1776 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6
.rip6_dest
),
1778 if (!rrt
&& (rrt
= rtsearch(&ni6
, NULL
)) == NULL
) {
1779 trace(1, "\tno route found\n");
1783 if ((rrt
->rrt_flags
& RTF_STATIC
) == 0) {
1784 trace(1, "\tyou can delete static routes only\n");
1787 if (!IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, gw
)) {
1788 trace(1, "\tgw mismatch: %s <-> ",
1789 inet6_n2p(&rrt
->rrt_gw
));
1790 trace(1, "%s\n", inet6_n2p(gw
));
1792 trace(1, "\troute found, age it\n");
1793 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1794 rrt
->rrt_t
= t_lifetime
;
1795 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
1802 * remove specified address from internal interface/routing table.
1805 rt_deladdr(struct ifc
*ifcp
, const struct sockaddr_in6
*sifa
,
1806 const struct sockaddr_in6
*smask
)
1808 const struct in6_addr
*addr
= NULL
;
1810 struct ifac
*ifa
= NULL
;
1811 struct netinfo6 ni6
;
1812 struct riprt
*rrt
= NULL
;
1816 if (sifa
->sin6_family
!= AF_INET6
) {
1817 trace(1, "\tother AF, ignored\n");
1820 addr
= &sifa
->sin6_addr
;
1821 prefix
= sin6mask2len(smask
);
1823 trace(1, "\tdeleting %s/%d from %s\n",
1824 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
1825 ifa
= ifa_match(ifcp
, addr
, prefix
);
1827 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1828 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
1831 if (ifa
->ifa_conf
!= ifcp
) {
1832 trace(1, "\taddress table corrupt: back pointer does not match "
1834 ifcp
->ifc_name
, ifa
->ifa_conf
->ifc_name
);
1837 /* remove ifa from interface */
1838 if (ifcp
->ifc_addr
== ifa
)
1839 ifcp
->ifc_addr
= ifa
->ifa_next
;
1842 for (p
= ifcp
->ifc_addr
; p
; p
= p
->ifa_next
) {
1843 if (p
->ifa_next
== ifa
) {
1844 p
->ifa_next
= ifa
->ifa_next
;
1849 ifa
->ifa_next
= NULL
;
1850 ifa
->ifa_conf
= NULL
;
1851 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
1852 /* age route for interface address */
1853 memset(&ni6
, 0, sizeof(ni6
));
1854 ni6
.rip6_dest
= ifa
->ifa_addr
;
1855 ni6
.rip6_plen
= ifa
->ifa_plen
;
1856 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
);
1857 trace(1, "\tfind interface route %s/%d on %d\n",
1858 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
, ifcp
->ifc_index
);
1859 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
1860 struct in6_addr none
;
1861 memset(&none
, 0, sizeof(none
));
1862 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1863 (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &none
) ||
1864 IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
))) {
1865 trace(1, "\troute found, age it\n");
1866 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1867 rrt
->rrt_t
= t_lifetime
;
1868 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
1872 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1873 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
1874 rrt
->rrt_info
.rip6_plen
,
1878 trace(1, "\tno interface route found\n");
1879 /* age route for p2p destination */
1880 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1881 memset(&ni6
, 0, sizeof(ni6
));
1882 ni6
.rip6_dest
= ifa
->ifa_raddr
;
1883 ni6
.rip6_plen
= 128;
1884 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
1885 trace(1, "\tfind p2p route %s/%d on %d\n",
1886 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
,
1888 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
1889 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1890 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &ifa
->ifa_addr
)) {
1891 trace(1, "\troute found, age it\n");
1892 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1893 rrt
->rrt_t
= t_lifetime
;
1894 rrt
->rrt_info
.rip6_metric
=
1899 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1900 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
1901 rrt
->rrt_info
.rip6_plen
,
1905 trace(1, "\tno p2p route found\n");
1907 return updated
? 0 : -1;
1911 * Get each interface address and put those interface routes to the route
1915 ifrt(struct ifc
*ifcp
, int again
)
1918 struct riprt
*rrt
, *search_rrt
, *prev_rrt
, *loop_rrt
;
1919 struct netinfo6
*np
;
1921 int need_trigger
= 0;
1923 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
1924 return 0; /* ignore loopback */
1925 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1926 ifrt_p2p(ifcp
, again
);
1930 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
1931 if (IN6_IS_ADDR_LINKLOCAL(&ifa
->ifa_addr
)) {
1933 trace(1, "route: %s on %s: "
1934 "skip linklocal interface address\n",
1935 inet6_n2p(&ifa
->ifa_addr
), ifcp
->ifc_name
);
1939 if (IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_addr
)) {
1941 trace(1, "route: %s: skip unspec interface address\n",
1946 if (ifcp
->ifc_flags
& IFF_UP
) {
1947 if ((rrt
= MALLOC(struct riprt
)) == NULL
)
1948 fatal("malloc: struct riprt");
1949 memset(rrt
, 0, sizeof(*rrt
));
1950 rrt
->rrt_same
= NULL
;
1951 rrt
->rrt_index
= ifcp
->ifc_index
;
1952 rrt
->rrt_t
= 0; /* don't age */
1953 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
1954 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
1955 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
1956 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
1957 rrt
->rrt_flags
= RTF_CLONING
;
1958 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1959 applyplen(&rrt
->rrt_info
.rip6_dest
, ifa
->ifa_plen
);
1960 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
1962 /* XXX why gateway address == network adddress? */
1963 rrt
->rrt_gw
= ifa
->ifa_addr
;
1965 np
= &rrt
->rrt_info
;
1966 search_rrt
= rtsearch(np
, &prev_rrt
);
1967 if (search_rrt
!= NULL
) {
1968 if (search_rrt
->rrt_info
.rip6_metric
>
1969 rrt
->rrt_info
.rip6_metric
) {
1971 prev_rrt
->rrt_next
= rrt
->rrt_next
;
1973 riprt
= rrt
->rrt_next
;
1974 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
1977 /* Already have better route */
1979 trace(1, "route: %s/%d: "
1980 "already registered (%s)\n",
1981 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
1988 /* Attach the route to the list */
1989 trace(1, "route: %s/%d: register route (%s)\n",
1990 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
1992 rrt
->rrt_next
= riprt
;
1994 addroute(rrt
, &rrt
->rrt_gw
, ifcp
);
1996 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
1999 for (loop_rrt
= riprt
; loop_rrt
; loop_rrt
= loop_rrt
->rrt_next
) {
2000 if (loop_rrt
->rrt_index
== ifcp
->ifc_index
) {
2001 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2002 if (loop_rrt
->rrt_t
== 0 || loop_rrt
->rrt_t
> t_lifetime
) {
2003 loop_rrt
->rrt_t
= t_lifetime
;
2004 loop_rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2005 loop_rrt
->rrt_rflags
|= RRTF_CHANGED
;
2012 return need_trigger
;
2016 * there are couple of p2p interface routing models. "behavior" lets
2017 * you pick one. it looks that gated behavior fits best with BSDs,
2018 * since BSD kernels does not look at prefix length on p2p interfaces.
2021 ifrt_p2p(struct ifc
*ifcp
, int again
)
2024 struct riprt
*rrt
, *orrt
, *prevrrt
;
2025 struct netinfo6
*np
;
2026 struct in6_addr addr
, dest
;
2027 int advert
, ignore
, i
;
2028 #define P2PADVERT_NETWORK 1
2029 #define P2PADVERT_ADDR 2
2030 #define P2PADVERT_DEST 4
2031 #define P2PADVERT_MAX 4
2032 const enum { CISCO
, GATED
, ROUTE6D
} behavior
= GATED
;
2033 const char *category
= "";
2036 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2037 addr
= ifa
->ifa_addr
;
2038 dest
= ifa
->ifa_raddr
;
2039 applyplen(&addr
, ifa
->ifa_plen
);
2040 applyplen(&dest
, ifa
->ifa_plen
);
2041 advert
= ignore
= 0;
2045 * honor addr/plen, just like normal shared medium
2046 * interface. this may cause trouble if you reuse
2047 * addr/plen on other interfaces.
2049 * advertise addr/plen.
2051 advert
|= P2PADVERT_NETWORK
;
2055 * prefixlen on p2p interface is meaningless.
2056 * advertise addr/128 and dest/128.
2058 * do not install network route to route6d routing
2059 * table (if we do, it would prevent route installation
2060 * for other p2p interface that shares addr/plen).
2062 * XXX what should we do if dest is ::? it will not
2063 * get announced anyways (see following filter),
2064 * but we need to think.
2066 advert
|= P2PADVERT_ADDR
;
2067 advert
|= P2PADVERT_DEST
;
2068 ignore
|= P2PADVERT_NETWORK
;
2072 * just for testing. actually the code is redundant
2073 * given the current p2p interface address assignment
2074 * rule for kame kernel.
2077 * A/n -> announce A/n
2078 * A B/n, A and B share prefix -> A/n (= B/n)
2079 * A B/n, do not share prefix -> A/128 and B/128
2080 * actually, A/64 and A B/128 are the only cases
2081 * permitted by the kernel:
2083 * A B/128 -> A/128 and B/128
2085 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_raddr
)) {
2086 if (IN6_ARE_ADDR_EQUAL(&addr
, &dest
))
2087 advert
|= P2PADVERT_NETWORK
;
2089 advert
|= P2PADVERT_ADDR
;
2090 advert
|= P2PADVERT_DEST
;
2091 ignore
|= P2PADVERT_NETWORK
;
2094 advert
|= P2PADVERT_NETWORK
;
2098 for (i
= 1; i
<= P2PADVERT_MAX
; i
*= 2) {
2099 if ((ignore
& i
) != 0)
2101 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2102 fatal("malloc: struct riprt");
2105 memset(rrt
, 0, sizeof(*rrt
));
2106 rrt
->rrt_same
= NULL
;
2107 rrt
->rrt_index
= ifcp
->ifc_index
;
2108 rrt
->rrt_t
= 0; /* don't age */
2110 case P2PADVERT_NETWORK
:
2111 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2112 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
2113 applyplen(&rrt
->rrt_info
.rip6_dest
,
2115 category
= "network";
2117 case P2PADVERT_ADDR
:
2118 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2119 rrt
->rrt_info
.rip6_plen
= 128;
2120 rrt
->rrt_gw
= in6addr_loopback
;
2123 case P2PADVERT_DEST
:
2124 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_raddr
;
2125 rrt
->rrt_info
.rip6_plen
= 128;
2126 rrt
->rrt_gw
= ifa
->ifa_addr
;
2130 if (IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_info
.rip6_dest
) ||
2131 IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_info
.rip6_dest
)) {
2133 trace(1, "route: %s: skip unspec/linklocal "
2134 "(%s on %s)\n", category
, ifcp
->ifc_name
);
2139 if ((advert
& i
) == 0) {
2140 rrt
->rrt_rflags
|= RRTF_NOADVERTISE
;
2144 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2145 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2146 np
= &rrt
->rrt_info
;
2147 orrt
= rtsearch(np
, &prevrrt
);
2149 /* Attach the route to the list */
2150 trace(1, "route: %s/%d: register route "
2152 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2153 category
, ifcp
->ifc_name
, noadv
);
2154 rrt
->rrt_next
= riprt
;
2156 } else if (rrt
->rrt_index
!= orrt
->rrt_index
||
2157 rrt
->rrt_info
.rip6_metric
!= orrt
->rrt_info
.rip6_metric
) {
2159 rrt
->rrt_next
= orrt
->rrt_next
;
2161 prevrrt
->rrt_next
= rrt
;
2166 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2167 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2168 category
, ifcp
->ifc_name
, noadv
);
2172 trace(1, "route: %s/%d: "
2173 "already registered (%s on %s%s)\n",
2174 inet6_n2p(&np
->rip6_dest
),
2175 np
->rip6_plen
, category
,
2176 ifcp
->ifc_name
, noadv
);
2182 #undef P2PADVERT_NETWORK
2183 #undef P2PADVERT_ADDR
2184 #undef P2PADVERT_DEST
2185 #undef P2PADVERT_MAX
2189 getifmtu(int ifindex
)
2194 struct if_msghdr
*ifm
;
2201 mib
[4] = NET_RT_IFLIST
;
2203 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2204 fatal("sysctl estimate NET_RT_IFLIST");
2207 if ((buf
= malloc(msize
)) == NULL
) {
2211 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2212 fatal("sysctl NET_RT_IFLIST");
2215 ifm
= (struct if_msghdr
*)buf
;
2216 mtu
= ifm
->ifm_data
.ifi_mtu
;
2217 #ifdef __DragonFly__
2218 if (ifindex
!= ifm
->ifm_index
) {
2219 fatal("ifindex does not match with ifm_index");
2228 rttypes(struct rt_msghdr
*rtm
)
2230 #define RTTYPE(s, f) \
2232 if (rtm->rtm_type == (f)) \
2235 RTTYPE("ADD", RTM_ADD
);
2236 RTTYPE("DELETE", RTM_DELETE
);
2237 RTTYPE("CHANGE", RTM_CHANGE
);
2238 RTTYPE("GET", RTM_GET
);
2239 RTTYPE("LOSING", RTM_LOSING
);
2240 RTTYPE("REDIRECT", RTM_REDIRECT
);
2241 RTTYPE("MISS", RTM_MISS
);
2242 RTTYPE("LOCK", RTM_LOCK
);
2243 RTTYPE("RESOLVE", RTM_RESOLVE
);
2244 RTTYPE("NEWADDR", RTM_NEWADDR
);
2245 RTTYPE("DELADDR", RTM_DELADDR
);
2246 RTTYPE("IFINFO", RTM_IFINFO
);
2248 RTTYPE("OIFINFO", RTM_OIFINFO
);
2250 #ifdef RTM_IFANNOUNCE
2251 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE
);
2254 RTTYPE("NEWMADDR", RTM_NEWMADDR
);
2257 RTTYPE("DELMADDR", RTM_DELMADDR
);
2264 rtflags(struct rt_msghdr
*rtm
)
2266 static char buf
[BUFSIZ
];
2269 * letter conflict should be okay. painful when *BSD diverges...
2271 strlcpy(buf
, "", sizeof(buf
));
2272 #define RTFLAG(s, f) \
2274 if (rtm->rtm_flags & (f)) \
2275 strlcat(buf, (s), sizeof(buf)); \
2277 RTFLAG("U", RTF_UP
);
2278 RTFLAG("G", RTF_GATEWAY
);
2279 RTFLAG("H", RTF_HOST
);
2280 RTFLAG("R", RTF_REJECT
);
2281 RTFLAG("D", RTF_DYNAMIC
);
2282 RTFLAG("M", RTF_MODIFIED
);
2283 RTFLAG("d", RTF_DONE
);
2285 RTFLAG("m", RTF_MASK
);
2287 RTFLAG("C", RTF_CLONING
);
2289 RTFLAG("c", RTF_CLONED
);
2291 #ifdef RTF_PRCLONING
2292 RTFLAG("c", RTF_PRCLONING
);
2294 #ifdef RTF_WASCLONED
2295 RTFLAG("W", RTF_WASCLONED
);
2297 RTFLAG("X", RTF_XRESOLVE
);
2298 RTFLAG("L", RTF_LLINFO
);
2299 RTFLAG("S", RTF_STATIC
);
2300 RTFLAG("B", RTF_BLACKHOLE
);
2302 RTFLAG("3", RTF_PROTO3
);
2304 RTFLAG("2", RTF_PROTO2
);
2305 RTFLAG("1", RTF_PROTO1
);
2306 #ifdef RTF_BROADCAST
2307 RTFLAG("b", RTF_BROADCAST
);
2310 RTFLAG("d", RTF_DEFAULT
);
2312 #ifdef RTF_ISAROUTER
2313 RTFLAG("r", RTF_ISAROUTER
);
2316 RTFLAG("T", RTF_TUNNEL
);
2319 RTFLAG("A", RTF_AUTH
);
2322 RTFLAG("E", RTF_CRYPT
);
2331 static char buf
[BUFSIZ
];
2333 strlcpy(buf
, "", sizeof(buf
));
2334 #define IFFLAG(s, f) \
2338 strlcat(buf, ",", sizeof(buf)); \
2339 strlcat(buf, s, sizeof(buf)); \
2342 IFFLAG("UP", IFF_UP
);
2343 IFFLAG("BROADCAST", IFF_BROADCAST
);
2344 IFFLAG("DEBUG", IFF_DEBUG
);
2345 IFFLAG("LOOPBACK", IFF_LOOPBACK
);
2346 IFFLAG("POINTOPOINT", IFF_POINTOPOINT
);
2347 #ifdef IFF_NOTRAILERS
2348 IFFLAG("NOTRAILERS", IFF_NOTRAILERS
);
2351 IFFLAG("SMART", IFF_SMART
);
2353 IFFLAG("RUNNING", IFF_RUNNING
);
2354 IFFLAG("NOARP", IFF_NOARP
);
2355 IFFLAG("PROMISC", IFF_PROMISC
);
2356 IFFLAG("ALLMULTI", IFF_ALLMULTI
);
2357 IFFLAG("OACTIVE", IFF_OACTIVE
);
2358 IFFLAG("SIMPLEX", IFF_SIMPLEX
);
2359 IFFLAG("LINK0", IFF_LINK0
);
2360 IFFLAG("LINK1", IFF_LINK1
);
2361 IFFLAG("LINK2", IFF_LINK2
);
2362 IFFLAG("MULTICAST", IFF_MULTICAST
);
2372 char *buf
, *p
, *lim
;
2373 struct rt_msghdr
*rtm
;
2382 mib
[3] = AF_INET6
; /* Address family */
2383 mib
[4] = NET_RT_DUMP
; /* Dump the kernel routing table */
2384 mib
[5] = 0; /* No flags */
2390 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2391 errmsg
= "sysctl estimate";
2394 if ((buf
= malloc(msize
)) == NULL
) {
2398 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2399 errmsg
= "sysctl NET_RT_DUMP";
2402 } while (retry
< 5 && errmsg
!= NULL
);
2404 fatal("%s (with %d retries, msize=%lu)", errmsg
, retry
,
2407 } else if (1 < retry
)
2408 syslog(LOG_INFO
, "NET_RT_DUMP %d retires", retry
);
2411 for (p
= buf
; p
< lim
; p
+= rtm
->rtm_msglen
) {
2412 rtm
= (struct rt_msghdr
*)p
;
2413 rt_entry(rtm
, again
);
2419 rt_entry(struct rt_msghdr
*rtm
, int again
)
2421 struct sockaddr_in6
*sin6_dst
, *sin6_gw
, *sin6_mask
;
2422 struct sockaddr_in6
*sin6_genmask
, *sin6_ifp
;
2423 char *rtmp
, *ifname
= NULL
;
2424 struct riprt
*rrt
, *orrt
;
2425 struct netinfo6
*np
;
2428 sin6_dst
= sin6_gw
= sin6_mask
= sin6_genmask
= sin6_ifp
= NULL
;
2429 if ((rtm
->rtm_flags
& RTF_UP
) == 0 || rtm
->rtm_flags
&
2430 (RTF_CLONING
|RTF_XRESOLVE
|RTF_LLINFO
|RTF_BLACKHOLE
)) {
2431 return; /* not interested in the link route */
2433 /* do not look at cloned routes */
2434 #ifdef RTF_WASCLONED
2435 if (rtm
->rtm_flags
& RTF_WASCLONED
)
2439 if (rtm
->rtm_flags
& RTF_CLONED
)
2443 * do not look at dynamic routes.
2444 * netbsd/openbsd cloned routes have UGHD.
2446 if (rtm
->rtm_flags
& RTF_DYNAMIC
)
2448 rtmp
= (char *)(rtm
+ 1);
2450 if ((rtm
->rtm_addrs
& RTA_DST
) == 0)
2451 return; /* ignore routes without destination address */
2452 sin6_dst
= (struct sockaddr_in6
*)rtmp
;
2453 rtmp
+= RT_ROUNDUP(sin6_dst
->sin6_len
);
2454 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2455 sin6_gw
= (struct sockaddr_in6
*)rtmp
;
2456 rtmp
+= RT_ROUNDUP(sin6_gw
->sin6_len
);
2458 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
2459 sin6_mask
= (struct sockaddr_in6
*)rtmp
;
2460 rtmp
+= RT_ROUNDUP(sin6_mask
->sin6_len
);
2462 if (rtm
->rtm_addrs
& RTA_GENMASK
) {
2463 sin6_genmask
= (struct sockaddr_in6
*)rtmp
;
2464 rtmp
+= RT_ROUNDUP(sin6_genmask
->sin6_len
);
2466 if (rtm
->rtm_addrs
& RTA_IFP
) {
2467 sin6_ifp
= (struct sockaddr_in6
*)rtmp
;
2468 rtmp
+= RT_ROUNDUP(sin6_ifp
->sin6_len
);
2472 if (sin6_dst
->sin6_family
!= AF_INET6
)
2474 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst
->sin6_addr
))
2475 return; /* Link-local */
2476 if (IN6_ARE_ADDR_EQUAL(&sin6_dst
->sin6_addr
, &in6addr_loopback
))
2477 return; /* Loopback */
2478 if (IN6_IS_ADDR_MULTICAST(&sin6_dst
->sin6_addr
))
2481 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2482 fatal("malloc: struct riprt");
2485 memset(rrt
, 0, sizeof(*rrt
));
2486 np
= &rrt
->rrt_info
;
2487 rrt
->rrt_same
= NULL
;
2488 rrt
->rrt_t
= time(NULL
);
2489 if (aflag
== 0 && (rtm
->rtm_flags
& RTF_STATIC
))
2490 rrt
->rrt_t
= 0; /* Don't age static routes */
2492 np
->rip6_tag
= htons(routetag
& 0xffff);
2496 np
->rip6_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
2497 if (np
->rip6_metric
< 1)
2498 np
->rip6_metric
= 1;
2499 rrt
->rrt_flags
= rtm
->rtm_flags
;
2500 np
->rip6_dest
= sin6_dst
->sin6_addr
;
2503 if (rtm
->rtm_flags
& RTF_HOST
)
2504 np
->rip6_plen
= 128; /* Host route */
2506 np
->rip6_plen
= sin6mask2len(sin6_mask
);
2510 orrt
= rtsearch(np
, NULL
);
2511 if (orrt
&& orrt
->rrt_info
.rip6_metric
!= HOPCNT_INFINITY6
) {
2514 trace(1, "route: %s/%d flags %s: already registered\n",
2515 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2523 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2525 if (sin6_gw
->sin6_family
== AF_INET6
)
2526 rrt
->rrt_gw
= sin6_gw
->sin6_addr
;
2527 else if (sin6_gw
->sin6_family
== AF_LINK
) {
2528 /* XXX in case ppp link? */
2529 rrt
->rrt_gw
= in6addr_loopback
;
2531 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2533 trace(1, "route: %s/%d flags %s",
2534 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, rtflags(rtm
));
2535 trace(1, " gw %s", inet6_n2p(&rrt
->rrt_gw
));
2539 if (s
< nindex2ifc
&& index2ifc
[s
])
2540 ifname
= index2ifc
[s
]->ifc_name
;
2542 trace(1, " not configured\n");
2546 trace(1, " if %s sock %d", ifname
, s
);
2552 if (!IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_gw
) &&
2553 !IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)
2554 #ifdef __DragonFly__
2555 && (rrt
->rrt_flags
& RTF_LOCAL
) == 0
2558 trace(0, "***** Gateway %s is not a link-local address.\n",
2559 inet6_n2p(&rrt
->rrt_gw
));
2560 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2561 inet6_n2p(&rrt
->rrt_info
.rip6_dest
), ifname
);
2562 rrt
->rrt_rflags
|= RRTF_NH_NOT_LLADDR
;
2565 /* Put it to the route list */
2566 if (orrt
&& orrt
->rrt_info
.rip6_metric
== HOPCNT_INFINITY6
) {
2567 /* replace route list */
2568 rrt
->rrt_next
= orrt
->rrt_next
;
2570 trace(1, "route: %s/%d flags %s: replace new route\n",
2571 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2575 rrt
->rrt_next
= riprt
;
2581 addroute(struct riprt
*rrt
, const struct in6_addr
*gw
, struct ifc
*ifcp
)
2583 struct netinfo6
*np
;
2584 u_char buf
[BUFSIZ
], buf1
[BUFSIZ
], buf2
[BUFSIZ
];
2585 struct rt_msghdr
*rtm
;
2586 struct sockaddr_in6
*sin
;
2589 np
= &rrt
->rrt_info
;
2590 inet_ntop(AF_INET6
, (const void *)gw
, (char *)buf1
, sizeof(buf1
));
2591 inet_ntop(AF_INET6
, (void *)&ifcp
->ifc_mylladdr
, (char *)buf2
, sizeof(buf2
));
2592 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2593 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2594 np
->rip6_metric
- 1, buf2
);
2596 fprintf(rtlog
, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2597 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2598 np
->rip6_metric
- 1, buf2
);
2602 memset(buf
, 0, sizeof(buf
));
2603 rtm
= (struct rt_msghdr
*)buf
;
2604 rtm
->rtm_type
= RTM_ADD
;
2605 rtm
->rtm_version
= RTM_VERSION
;
2606 rtm
->rtm_seq
= ++seq
;
2608 rtm
->rtm_flags
= rrt
->rrt_flags
;
2609 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2610 rtm
->rtm_rmx
.rmx_hopcount
= np
->rip6_metric
- 1;
2611 rtm
->rtm_inits
= RTV_HOPCOUNT
;
2612 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2614 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2615 sin
->sin6_family
= AF_INET6
;
2616 sin
->sin6_addr
= np
->rip6_dest
;
2617 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2619 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2620 sin
->sin6_family
= AF_INET6
;
2621 sin
->sin6_addr
= *gw
;
2622 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2624 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2625 sin
->sin6_family
= AF_INET6
;
2626 sin
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2627 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2629 len
= (char *)sin
- (char *)buf
;
2630 rtm
->rtm_msglen
= len
;
2631 if (write(rtsock
, buf
, len
) > 0)
2634 if (errno
== EEXIST
) {
2635 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2636 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2638 fprintf(rtlog
, "ADD: Route already exists %s/%d gw %s\n",
2639 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2641 trace(0, "Can not write to rtsock (addroute): %s\n",
2644 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2651 delroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2653 u_char buf
[BUFSIZ
], buf2
[BUFSIZ
];
2654 struct rt_msghdr
*rtm
;
2655 struct sockaddr_in6
*sin
;
2658 inet_ntop(AF_INET6
, (void *)gw
, (char *)buf2
, sizeof(buf2
));
2659 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np
->rip6_dest
),
2660 np
->rip6_plen
, buf2
);
2662 fprintf(rtlog
, "%s: DEL: %s/%d gw %s\n",
2663 hms(), inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2667 memset(buf
, 0, sizeof(buf
));
2668 rtm
= (struct rt_msghdr
*)buf
;
2669 rtm
->rtm_type
= RTM_DELETE
;
2670 rtm
->rtm_version
= RTM_VERSION
;
2671 rtm
->rtm_seq
= ++seq
;
2673 rtm
->rtm_flags
= RTF_UP
| RTF_GATEWAY
;
2674 if (np
->rip6_plen
== sizeof(struct in6_addr
) * 8)
2675 rtm
->rtm_flags
|= RTF_HOST
;
2676 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2677 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2679 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2680 sin
->sin6_family
= AF_INET6
;
2681 sin
->sin6_addr
= np
->rip6_dest
;
2682 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2684 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2685 sin
->sin6_family
= AF_INET6
;
2686 sin
->sin6_addr
= *gw
;
2687 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2689 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2690 sin
->sin6_family
= AF_INET6
;
2691 sin
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2692 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2694 len
= (char *)sin
- (char *)buf
;
2695 rtm
->rtm_msglen
= len
;
2696 if (write(rtsock
, buf
, len
) >= 0)
2699 if (errno
== ESRCH
) {
2700 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2701 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2703 fprintf(rtlog
, "RTDEL: Route does not exist: %s/%d gw %s\n",
2704 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2706 trace(0, "Can not write to rtsock (delroute): %s\n",
2709 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2716 getroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2721 struct rt_msghdr
*rtm
;
2722 struct sockaddr_in6
*sin
;
2724 rtm
= (struct rt_msghdr
*)buf
;
2725 len
= sizeof(struct rt_msghdr
) + sizeof(struct sockaddr_in6
);
2726 memset(rtm
, 0, len
);
2727 rtm
->rtm_type
= RTM_GET
;
2728 rtm
->rtm_version
= RTM_VERSION
;
2730 rtm
->rtm_seq
= myseq
;
2731 rtm
->rtm_addrs
= RTA_DST
;
2732 rtm
->rtm_msglen
= len
;
2733 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2734 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2735 sin
->sin6_family
= AF_INET6
;
2736 sin
->sin6_addr
= np
->rip6_dest
;
2737 if (write(rtsock
, buf
, len
) < 0) {
2738 if (errno
== ESRCH
) /* No such route found */
2740 perror("write to rtsock");
2744 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
2745 perror("read from rtsock");
2748 rtm
= (struct rt_msghdr
*)buf
;
2749 } while (rtm
->rtm_seq
!= myseq
|| rtm
->rtm_pid
!= pid
);
2750 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2751 if (rtm
->rtm_addrs
& RTA_DST
) {
2752 sin
= (struct sockaddr_in6
*)
2753 ((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2755 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2756 *gw
= sin
->sin6_addr
;
2763 inet6_n2p(const struct in6_addr
*p
)
2765 static char buf
[BUFSIZ
];
2767 return inet_ntop(AF_INET6
, (const void *)p
, buf
, sizeof(buf
));
2787 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
2790 fprintf(dump
, "%s: Interface Table Dump\n", hms());
2791 fprintf(dump
, " Number of interfaces: %d\n", nifc
);
2792 for (i
= 0; i
< 2; i
++) {
2793 fprintf(dump
, " %sadvertising interfaces:\n", i
? "non-" : "");
2794 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
2796 if ((ifcp
->ifc_flags
& IFF_UP
) == 0)
2798 if (iff_find(ifcp
, 'N') != NULL
)
2801 if (ifcp
->ifc_flags
& IFF_UP
)
2804 ifdump0(dump
, ifcp
);
2807 fprintf(dump
, "\n");
2813 ifdump0(FILE *dump
, const struct ifc
*ifcp
)
2821 fprintf(dump
, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2822 ifcp
->ifc_name
, ifcp
->ifc_index
, ifflags(ifcp
->ifc_flags
),
2823 inet6_n2p(&ifcp
->ifc_mylladdr
),
2824 ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
2825 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2826 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2827 inet_ntop(AF_INET6
, (void *)&ifa
->ifa_raddr
,
2829 fprintf(dump
, "\t%s/%d -- %s\n",
2830 inet6_n2p(&ifa
->ifa_addr
),
2831 ifa
->ifa_plen
, buf
);
2833 fprintf(dump
, "\t%s/%d\n",
2834 inet6_n2p(&ifa
->ifa_addr
),
2838 if (ifcp
->ifc_filter
) {
2839 fprintf(dump
, "\tFilter:");
2840 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
2842 switch (iffp
->iff_type
) {
2844 ft
= "Aggregate"; addr
++; break;
2846 ft
= "No-use"; break;
2848 ft
= "Advertise-only"; addr
++; break;
2850 ft
= "Default-only"; break;
2852 ft
= "Listen-only"; addr
++; break;
2854 snprintf(buf
, sizeof(buf
), "Unknown-%c", iffp
->iff_type
);
2859 fprintf(dump
, " %s", ft
);
2861 fprintf(dump
, "(%s/%d)", inet6_n2p(&iffp
->iff_addr
),
2865 fprintf(dump
, "\n");
2880 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
2884 fprintf(dump
, "\n%s: Routing Table Dump\n", hms());
2885 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
2886 if (rrt
->rrt_t
== 0)
2889 age
= t
- rrt
->rrt_t
;
2890 inet_ntop(AF_INET6
, (void *)&rrt
->rrt_info
.rip6_dest
,
2892 fprintf(dump
, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2893 buf
, rrt
->rrt_info
.rip6_plen
, rrt
->rrt_index
,
2894 index2ifc
[rrt
->rrt_index
]->ifc_name
,
2895 inet6_n2p(&rrt
->rrt_gw
),
2896 rrt
->rrt_info
.rip6_metric
, (long)age
);
2897 if (rrt
->rrt_info
.rip6_tag
) {
2898 fprintf(dump
, " tag(0x%04x)",
2899 ntohs(rrt
->rrt_info
.rip6_tag
) & 0xffff);
2901 if (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)
2902 fprintf(dump
, " NOT-LL");
2903 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
2904 fprintf(dump
, " NO-ADV");
2905 fprintf(dump
, "\n");
2907 fprintf(dump
, "\n");
2913 * Parse the -A (and -O) options and put corresponding filter object to the
2914 * specified interface structures. Each of the -A/O option has the following
2915 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2916 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2922 char *p
, *ap
, *iflp
, *ifname
;
2923 struct iff ftmp
, *iff_obj
;
2930 for (i
= 0; i
< nfilter
; i
++) {
2934 if (filtertype
[i
] == 'N' || filtertype
[i
] == 'T') {
2938 if ((p
= strchr(ap
, ',')) != NULL
) {
2942 if ((p
= strchr(ap
, '/')) == NULL
) {
2943 fatal("no prefixlen specified for '%s'", ap
);
2947 if (inet_pton(AF_INET6
, ap
, &ftmp
.iff_addr
) != 1) {
2948 fatal("invalid prefix specified for '%s'", ap
);
2951 ftmp
.iff_plen
= atoi(p
);
2952 ftmp
.iff_next
= NULL
;
2953 applyplen(&ftmp
.iff_addr
, ftmp
.iff_plen
);
2955 ftmp
.iff_type
= filtertype
[i
];
2956 if (iflp
== NULL
|| *iflp
== '\0') {
2957 fatal("no interface specified for '%s'", ap
);
2960 /* parse the interface listing portion */
2963 if ((iflp
= strchr(iflp
, ',')) != NULL
)
2965 ifcp
= ifc_find(ifname
);
2967 fatal("no interface %s exists", ifname
);
2970 iff_obj
= (struct iff
*)malloc(sizeof(struct iff
));
2971 if (iff_obj
== NULL
) {
2972 fatal("malloc of iff_obj");
2975 memcpy((void *)iff_obj
, (void *)&ftmp
,
2976 sizeof(struct iff
));
2977 /* link it to the interface filter */
2978 iff_obj
->iff_next
= ifcp
->ifc_filter
;
2979 ifcp
->ifc_filter
= iff_obj
;
2983 * -A: aggregate configuration.
2985 if (filtertype
[i
] != 'A')
2987 /* put the aggregate to the kernel routing table */
2988 rrt
= (struct riprt
*)malloc(sizeof(struct riprt
));
2990 fatal("malloc: rrt");
2993 memset(rrt
, 0, sizeof(struct riprt
));
2994 rrt
->rrt_info
.rip6_dest
= ftmp
.iff_addr
;
2995 rrt
->rrt_info
.rip6_plen
= ftmp
.iff_plen
;
2996 rrt
->rrt_info
.rip6_metric
= 1;
2997 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2998 rrt
->rrt_gw
= in6addr_loopback
;
2999 rrt
->rrt_flags
= RTF_UP
| RTF_REJECT
;
3000 rrt
->rrt_rflags
= RRTF_AGGREGATE
;
3002 rrt
->rrt_index
= loopifindex
;
3004 if (getroute(&rrt
->rrt_info
, &gw
)) {
3007 * When the address has already been registered in the
3008 * kernel routing table, it should be removed
3010 delroute(&rrt
->rrt_info
, &gw
);
3012 /* it is safer behavior */
3014 fatal("%s/%u already in routing table, "
3016 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
3017 rrt
->rrt_info
.rip6_plen
);
3022 /* Put the route to the list */
3023 rrt
->rrt_next
= riprt
;
3025 trace(1, "Aggregate: %s/%d for %s\n",
3026 inet6_n2p(&ftmp
.iff_addr
), ftmp
.iff_plen
,
3028 /* Add this route to the kernel */
3029 if (nflag
) /* do not modify kernel routing table */
3031 addroute(rrt
, &in6addr_loopback
, loopifcp
);
3035 /***************** utility functions *****************/
3038 * Returns a pointer to ifac whose address and prefix length matches
3039 * with the address and prefix length specified in the arguments.
3042 ifa_match(const struct ifc
*ifcp
, const struct in6_addr
*ia
, int plen
)
3046 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
3047 if (IN6_ARE_ADDR_EQUAL(&ifa
->ifa_addr
, ia
) &&
3048 ifa
->ifa_plen
== plen
)
3055 * Return a pointer to riprt structure whose address and prefix length
3056 * matches with the address and prefix length found in the argument.
3057 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3060 rtsearch(struct netinfo6
*np
, struct riprt
**prev_rrt
)
3066 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
3067 if (rrt
->rrt_info
.rip6_plen
== np
->rip6_plen
&&
3068 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
3080 sin6mask2len(const struct sockaddr_in6
*sin6
)
3082 return mask2len(&sin6
->sin6_addr
,
3083 sin6
->sin6_len
- offsetof(struct sockaddr_in6
, sin6_addr
));
3087 mask2len(const struct in6_addr
*addr
, int lenlim
)
3090 const u_char
*p
= (const u_char
*)addr
;
3092 for (j
= 0; j
< lenlim
; j
++, p
++) {
3099 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3100 MASKLEN(0xfe, 7); break;
3101 MASKLEN(0xfc, 6); break;
3102 MASKLEN(0xf8, 5); break;
3103 MASKLEN(0xf0, 4); break;
3104 MASKLEN(0xe0, 3); break;
3105 MASKLEN(0xc0, 2); break;
3106 MASKLEN(0x80, 1); break;
3114 applymask(struct in6_addr
*addr
, struct in6_addr
*mask
)
3119 p
= (u_long
*)addr
; q
= (u_long
*)mask
;
3120 for (i
= 0; i
< 4; i
++)
3124 static const u_char plent
[8] = {
3125 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3129 applyplen(struct in6_addr
*ia
, int plen
)
3135 for (i
= 0; i
< 16; i
++) {
3144 static const int pl2m
[9] = {
3145 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3151 static struct in6_addr ia
;
3155 memset(&ia
, 0, sizeof(struct in6_addr
));
3157 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
3171 char *q
= (char *)malloc(strlen(p
) + 1);
3180 static char buf
[BUFSIZ
];
3185 if ((tm
= localtime(&t
)) == NULL
) {
3189 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3194 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3197 ripinterval(int timer
)
3201 interval
= (int)(timer
+ timer
* RIPRANDDEV
* (r
/ RAND_MAX
- 0.5));
3202 nextalarm
= time(NULL
) + interval
;
3212 t
= (int)(RIP_TRIG_INT6_MIN
+
3213 (RIP_TRIG_INT6_MAX
- RIP_TRIG_INT6_MIN
) * (r
/ RAND_MAX
));
3214 sup_trig_update
= time(NULL
) + t
;
3219 fatal(const char *fmt
, ...)
3225 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
3227 syslog(LOG_ERR
, "%s: %s", buf
, strerror(errno
));
3233 tracet(int level
, const char *fmt
, ...)
3238 if (level
<= dflag
) {
3239 fprintf(stderr
, "%s: ", hms());
3240 vfprintf(stderr
, fmt
, ap
);
3244 vsyslog(LOG_DEBUG
, fmt
, ap
);
3246 vsyslog(LOG_WARNING
, fmt
, ap
);
3252 trace(int level
, const char *fmt
, ...)
3258 vfprintf(stderr
, fmt
, ap
);
3261 vsyslog(LOG_DEBUG
, fmt
, ap
);
3263 vsyslog(LOG_WARNING
, fmt
, ap
);
3271 struct if_nameindex
*p
, *p0
;
3272 unsigned int max
= 0;
3274 p0
= if_nameindex();
3275 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
3276 if (max
< p
->if_index
)
3279 if_freenameindex(p0
);
3284 ifc_find(char *name
)
3288 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
3289 if (strcmp(name
, ifcp
->ifc_name
) == 0)
3296 iff_find(struct ifc
*ifcp
, int type
)
3300 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
3301 if (iffp
->iff_type
== type
)
3308 setindex2ifc(int idx
, struct ifc
*ifcp
)
3314 nindex2ifc
= 5; /*initial guess*/
3315 index2ifc
= (struct ifc
**)
3316 malloc(sizeof(*index2ifc
) * nindex2ifc
);
3317 if (index2ifc
== NULL
) {
3321 memset(index2ifc
, 0, sizeof(*index2ifc
) * nindex2ifc
);
3324 while (nindex2ifc
<= idx
)
3326 if (n
!= nindex2ifc
) {
3327 p
= (struct ifc
**)realloc(index2ifc
,
3328 sizeof(*index2ifc
) * nindex2ifc
);
3333 memset(p
+ n
, 0, sizeof(*index2ifc
) * (nindex2ifc
- n
));
3336 index2ifc
[idx
] = ifcp
;