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
]))
1673 trace(1, "\tnot supported yet, ignored\n");
1680 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1682 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
)
1683 if (ifcp
->ifc_cflags
& IFC_CHANGED
) {
1684 if (ifrt(ifcp
, 1)) {
1685 for (ic
= ifc
; ic
; ic
= ic
->ifc_next
) {
1686 if (ifcp
->ifc_index
== ic
->ifc_index
)
1688 if (ic
->ifc_flags
& IFF_UP
)
1689 ripsend(ic
, &ic
->ifc_ripsin
,
1692 /* Reset the flag */
1693 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
)
1694 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1696 ifcp
->ifc_cflags
&= ~IFC_CHANGED
;
1700 trace(1, "rtsock: read routing table again\n");
1706 * remove specified route from the internal routing table.
1709 rt_del(const struct sockaddr_in6
*sdst
, const struct sockaddr_in6
*sgw
,
1710 const struct sockaddr_in6
*smask
)
1712 const struct in6_addr
*dst
= NULL
;
1713 const struct in6_addr
*gw
= NULL
;
1715 struct netinfo6 ni6
;
1716 struct riprt
*rrt
= NULL
;
1719 if (sdst
->sin6_family
!= AF_INET6
) {
1720 trace(1, "\tother AF, ignored\n");
1723 if (IN6_IS_ADDR_LINKLOCAL(&sdst
->sin6_addr
)
1724 || IN6_ARE_ADDR_EQUAL(&sdst
->sin6_addr
, &in6addr_loopback
)
1725 || IN6_IS_ADDR_MULTICAST(&sdst
->sin6_addr
)) {
1726 trace(1, "\taddress %s not interesting, ignored\n",
1727 inet6_n2p(&sdst
->sin6_addr
));
1730 dst
= &sdst
->sin6_addr
;
1731 if (sgw
->sin6_family
== AF_INET6
) {
1733 gw
= &sgw
->sin6_addr
;
1734 prefix
= sin6mask2len(smask
);
1735 } else if (sgw
->sin6_family
== AF_LINK
) {
1737 * Interface route... a hard case. We need to get the prefix
1738 * length from the kernel, but we now are parsing rtmsg.
1739 * We'll purge matching routes from my list, then get the
1742 struct riprt
*longest
;
1743 trace(1, "\t%s is a interface route, guessing prefixlen\n",
1746 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
1747 if (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
1749 && IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)) {
1751 || longest
->rrt_info
.rip6_plen
<
1752 rrt
->rrt_info
.rip6_plen
) {
1759 trace(1, "\tno matching interface route found\n");
1762 gw
= &in6addr_loopback
;
1763 prefix
= rrt
->rrt_info
.rip6_plen
;
1765 trace(1, "\tunsupported af: (gw=%d)\n", sgw
->sin6_family
);
1769 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst
), prefix
);
1770 trace(1, "gw %s\n", inet6_n2p(gw
));
1771 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
1772 /* age route for interface address */
1773 memset(&ni6
, 0, sizeof(ni6
));
1774 ni6
.rip6_dest
= *dst
;
1775 ni6
.rip6_plen
= prefix
;
1776 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
1777 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6
.rip6_dest
),
1779 if (!rrt
&& (rrt
= rtsearch(&ni6
, NULL
)) == NULL
) {
1780 trace(1, "\tno route found\n");
1784 if ((rrt
->rrt_flags
& RTF_STATIC
) == 0) {
1785 trace(1, "\tyou can delete static routes only\n");
1788 if (!IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, gw
)) {
1789 trace(1, "\tgw mismatch: %s <-> ",
1790 inet6_n2p(&rrt
->rrt_gw
));
1791 trace(1, "%s\n", inet6_n2p(gw
));
1793 trace(1, "\troute found, age it\n");
1794 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1795 rrt
->rrt_t
= t_lifetime
;
1796 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
1803 * remove specified address from internal interface/routing table.
1806 rt_deladdr(struct ifc
*ifcp
, const struct sockaddr_in6
*sifa
,
1807 const struct sockaddr_in6
*smask
)
1809 const struct in6_addr
*addr
= NULL
;
1811 struct ifac
*ifa
= NULL
;
1812 struct netinfo6 ni6
;
1813 struct riprt
*rrt
= NULL
;
1817 if (sifa
->sin6_family
!= AF_INET6
) {
1818 trace(1, "\tother AF, ignored\n");
1821 addr
= &sifa
->sin6_addr
;
1822 prefix
= sin6mask2len(smask
);
1824 trace(1, "\tdeleting %s/%d from %s\n",
1825 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
1826 ifa
= ifa_match(ifcp
, addr
, prefix
);
1828 trace(1, "\tno matching ifa found for %s/%d on %s\n",
1829 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
1832 if (ifa
->ifa_conf
!= ifcp
) {
1833 trace(1, "\taddress table corrupt: back pointer does not match "
1835 ifcp
->ifc_name
, ifa
->ifa_conf
->ifc_name
);
1838 /* remove ifa from interface */
1839 if (ifcp
->ifc_addr
== ifa
)
1840 ifcp
->ifc_addr
= ifa
->ifa_next
;
1843 for (p
= ifcp
->ifc_addr
; p
; p
= p
->ifa_next
) {
1844 if (p
->ifa_next
== ifa
) {
1845 p
->ifa_next
= ifa
->ifa_next
;
1850 ifa
->ifa_next
= NULL
;
1851 ifa
->ifa_conf
= NULL
;
1852 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
1853 /* age route for interface address */
1854 memset(&ni6
, 0, sizeof(ni6
));
1855 ni6
.rip6_dest
= ifa
->ifa_addr
;
1856 ni6
.rip6_plen
= ifa
->ifa_plen
;
1857 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
);
1858 trace(1, "\tfind interface route %s/%d on %d\n",
1859 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
, ifcp
->ifc_index
);
1860 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
1861 struct in6_addr none
;
1862 memset(&none
, 0, sizeof(none
));
1863 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1864 (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &none
) ||
1865 IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
))) {
1866 trace(1, "\troute found, age it\n");
1867 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1868 rrt
->rrt_t
= t_lifetime
;
1869 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
1873 trace(1, "\tnon-interface route found: %s/%d on %d\n",
1874 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
1875 rrt
->rrt_info
.rip6_plen
,
1879 trace(1, "\tno interface route found\n");
1880 /* age route for p2p destination */
1881 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1882 memset(&ni6
, 0, sizeof(ni6
));
1883 ni6
.rip6_dest
= ifa
->ifa_raddr
;
1884 ni6
.rip6_plen
= 128;
1885 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
1886 trace(1, "\tfind p2p route %s/%d on %d\n",
1887 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
,
1889 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
1890 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1891 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &ifa
->ifa_addr
)) {
1892 trace(1, "\troute found, age it\n");
1893 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1894 rrt
->rrt_t
= t_lifetime
;
1895 rrt
->rrt_info
.rip6_metric
=
1900 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
1901 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
1902 rrt
->rrt_info
.rip6_plen
,
1906 trace(1, "\tno p2p route found\n");
1908 return updated
? 0 : -1;
1912 * Get each interface address and put those interface routes to the route
1916 ifrt(struct ifc
*ifcp
, int again
)
1919 struct riprt
*rrt
, *search_rrt
, *prev_rrt
, *loop_rrt
;
1920 struct netinfo6
*np
;
1922 int need_trigger
= 0;
1924 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
1925 return 0; /* ignore loopback */
1926 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1927 ifrt_p2p(ifcp
, again
);
1931 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
1932 if (IN6_IS_ADDR_LINKLOCAL(&ifa
->ifa_addr
)) {
1934 trace(1, "route: %s on %s: "
1935 "skip linklocal interface address\n",
1936 inet6_n2p(&ifa
->ifa_addr
), ifcp
->ifc_name
);
1940 if (IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_addr
)) {
1942 trace(1, "route: %s: skip unspec interface address\n",
1947 if (ifcp
->ifc_flags
& IFF_UP
) {
1948 if ((rrt
= MALLOC(struct riprt
)) == NULL
)
1949 fatal("malloc: struct riprt");
1950 memset(rrt
, 0, sizeof(*rrt
));
1951 rrt
->rrt_same
= NULL
;
1952 rrt
->rrt_index
= ifcp
->ifc_index
;
1953 rrt
->rrt_t
= 0; /* don't age */
1954 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
1955 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
1956 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
1957 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
1958 rrt
->rrt_flags
= RTF_CLONING
;
1959 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1960 applyplen(&rrt
->rrt_info
.rip6_dest
, ifa
->ifa_plen
);
1961 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
1963 /* XXX why gateway address == network adddress? */
1964 rrt
->rrt_gw
= ifa
->ifa_addr
;
1966 np
= &rrt
->rrt_info
;
1967 search_rrt
= rtsearch(np
, &prev_rrt
);
1968 if (search_rrt
!= NULL
) {
1969 if (search_rrt
->rrt_info
.rip6_metric
>
1970 rrt
->rrt_info
.rip6_metric
) {
1972 prev_rrt
->rrt_next
= rrt
->rrt_next
;
1974 riprt
= rrt
->rrt_next
;
1975 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
1978 /* Already have better route */
1980 trace(1, "route: %s/%d: "
1981 "already registered (%s)\n",
1982 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
1989 /* Attach the route to the list */
1990 trace(1, "route: %s/%d: register route (%s)\n",
1991 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
1993 rrt
->rrt_next
= riprt
;
1995 addroute(rrt
, &rrt
->rrt_gw
, ifcp
);
1997 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
2000 for (loop_rrt
= riprt
; loop_rrt
; loop_rrt
= loop_rrt
->rrt_next
) {
2001 if (loop_rrt
->rrt_index
== ifcp
->ifc_index
) {
2002 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2003 if (loop_rrt
->rrt_t
== 0 || loop_rrt
->rrt_t
> t_lifetime
) {
2004 loop_rrt
->rrt_t
= t_lifetime
;
2005 loop_rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2006 loop_rrt
->rrt_rflags
|= RRTF_CHANGED
;
2013 return need_trigger
;
2017 * there are couple of p2p interface routing models. "behavior" lets
2018 * you pick one. it looks that gated behavior fits best with BSDs,
2019 * since BSD kernels does not look at prefix length on p2p interfaces.
2022 ifrt_p2p(struct ifc
*ifcp
, int again
)
2025 struct riprt
*rrt
, *orrt
, *prevrrt
;
2026 struct netinfo6
*np
;
2027 struct in6_addr addr
, dest
;
2028 int advert
, ignore
, i
;
2029 #define P2PADVERT_NETWORK 1
2030 #define P2PADVERT_ADDR 2
2031 #define P2PADVERT_DEST 4
2032 #define P2PADVERT_MAX 4
2033 const enum { CISCO
, GATED
, ROUTE6D
} behavior
= GATED
;
2034 const char *category
= "";
2037 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2038 addr
= ifa
->ifa_addr
;
2039 dest
= ifa
->ifa_raddr
;
2040 applyplen(&addr
, ifa
->ifa_plen
);
2041 applyplen(&dest
, ifa
->ifa_plen
);
2042 advert
= ignore
= 0;
2046 * honor addr/plen, just like normal shared medium
2047 * interface. this may cause trouble if you reuse
2048 * addr/plen on other interfaces.
2050 * advertise addr/plen.
2052 advert
|= P2PADVERT_NETWORK
;
2056 * prefixlen on p2p interface is meaningless.
2057 * advertise addr/128 and dest/128.
2059 * do not install network route to route6d routing
2060 * table (if we do, it would prevent route installation
2061 * for other p2p interface that shares addr/plen).
2063 * XXX what should we do if dest is ::? it will not
2064 * get announced anyways (see following filter),
2065 * but we need to think.
2067 advert
|= P2PADVERT_ADDR
;
2068 advert
|= P2PADVERT_DEST
;
2069 ignore
|= P2PADVERT_NETWORK
;
2073 * just for testing. actually the code is redundant
2074 * given the current p2p interface address assignment
2075 * rule for kame kernel.
2078 * A/n -> announce A/n
2079 * A B/n, A and B share prefix -> A/n (= B/n)
2080 * A B/n, do not share prefix -> A/128 and B/128
2081 * actually, A/64 and A B/128 are the only cases
2082 * permitted by the kernel:
2084 * A B/128 -> A/128 and B/128
2086 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_raddr
)) {
2087 if (IN6_ARE_ADDR_EQUAL(&addr
, &dest
))
2088 advert
|= P2PADVERT_NETWORK
;
2090 advert
|= P2PADVERT_ADDR
;
2091 advert
|= P2PADVERT_DEST
;
2092 ignore
|= P2PADVERT_NETWORK
;
2095 advert
|= P2PADVERT_NETWORK
;
2099 for (i
= 1; i
<= P2PADVERT_MAX
; i
*= 2) {
2100 if ((ignore
& i
) != 0)
2102 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2103 fatal("malloc: struct riprt");
2106 memset(rrt
, 0, sizeof(*rrt
));
2107 rrt
->rrt_same
= NULL
;
2108 rrt
->rrt_index
= ifcp
->ifc_index
;
2109 rrt
->rrt_t
= 0; /* don't age */
2111 case P2PADVERT_NETWORK
:
2112 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2113 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
2114 applyplen(&rrt
->rrt_info
.rip6_dest
,
2116 category
= "network";
2118 case P2PADVERT_ADDR
:
2119 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2120 rrt
->rrt_info
.rip6_plen
= 128;
2121 rrt
->rrt_gw
= in6addr_loopback
;
2124 case P2PADVERT_DEST
:
2125 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_raddr
;
2126 rrt
->rrt_info
.rip6_plen
= 128;
2127 rrt
->rrt_gw
= ifa
->ifa_addr
;
2131 if (IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_info
.rip6_dest
) ||
2132 IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_info
.rip6_dest
)) {
2134 trace(1, "route: %s: skip unspec/linklocal "
2135 "(%s on %s)\n", category
, ifcp
->ifc_name
);
2140 if ((advert
& i
) == 0) {
2141 rrt
->rrt_rflags
|= RRTF_NOADVERTISE
;
2145 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2146 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2147 np
= &rrt
->rrt_info
;
2148 orrt
= rtsearch(np
, &prevrrt
);
2150 /* Attach the route to the list */
2151 trace(1, "route: %s/%d: register route "
2153 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2154 category
, ifcp
->ifc_name
, noadv
);
2155 rrt
->rrt_next
= riprt
;
2157 } else if (rrt
->rrt_index
!= orrt
->rrt_index
||
2158 rrt
->rrt_info
.rip6_metric
!= orrt
->rrt_info
.rip6_metric
) {
2160 rrt
->rrt_next
= orrt
->rrt_next
;
2162 prevrrt
->rrt_next
= rrt
;
2167 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2168 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2169 category
, ifcp
->ifc_name
, noadv
);
2173 trace(1, "route: %s/%d: "
2174 "already registered (%s on %s%s)\n",
2175 inet6_n2p(&np
->rip6_dest
),
2176 np
->rip6_plen
, category
,
2177 ifcp
->ifc_name
, noadv
);
2183 #undef P2PADVERT_NETWORK
2184 #undef P2PADVERT_ADDR
2185 #undef P2PADVERT_DEST
2186 #undef P2PADVERT_MAX
2190 getifmtu(int ifindex
)
2195 struct if_msghdr
*ifm
;
2202 mib
[4] = NET_RT_IFLIST
;
2204 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2205 fatal("sysctl estimate NET_RT_IFLIST");
2208 if ((buf
= malloc(msize
)) == NULL
) {
2212 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2213 fatal("sysctl NET_RT_IFLIST");
2216 ifm
= (struct if_msghdr
*)buf
;
2217 mtu
= ifm
->ifm_data
.ifi_mtu
;
2218 #ifdef __DragonFly__
2219 if (ifindex
!= ifm
->ifm_index
) {
2220 fatal("ifindex does not match with ifm_index");
2229 rttypes(struct rt_msghdr
*rtm
)
2231 #define RTTYPE(s, f) \
2233 if (rtm->rtm_type == (f)) \
2236 RTTYPE("ADD", RTM_ADD
);
2237 RTTYPE("DELETE", RTM_DELETE
);
2238 RTTYPE("CHANGE", RTM_CHANGE
);
2239 RTTYPE("GET", RTM_GET
);
2240 RTTYPE("LOSING", RTM_LOSING
);
2241 RTTYPE("REDIRECT", RTM_REDIRECT
);
2242 RTTYPE("MISS", RTM_MISS
);
2243 RTTYPE("LOCK", RTM_LOCK
);
2244 RTTYPE("OLDADD", RTM_OLDADD
);
2245 RTTYPE("OLDDEL", RTM_OLDDEL
);
2246 RTTYPE("RESOLVE", RTM_RESOLVE
);
2247 RTTYPE("NEWADDR", RTM_NEWADDR
);
2248 RTTYPE("DELADDR", RTM_DELADDR
);
2249 RTTYPE("IFINFO", RTM_IFINFO
);
2251 RTTYPE("OLDADD", RTM_OLDADD
);
2254 RTTYPE("OLDDEL", RTM_OLDDEL
);
2257 RTTYPE("OIFINFO", RTM_OIFINFO
);
2259 #ifdef RTM_IFANNOUNCE
2260 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE
);
2263 RTTYPE("NEWMADDR", RTM_NEWMADDR
);
2266 RTTYPE("DELMADDR", RTM_DELMADDR
);
2273 rtflags(struct rt_msghdr
*rtm
)
2275 static char buf
[BUFSIZ
];
2278 * letter conflict should be okay. painful when *BSD diverges...
2280 strlcpy(buf
, "", sizeof(buf
));
2281 #define RTFLAG(s, f) \
2283 if (rtm->rtm_flags & (f)) \
2284 strlcat(buf, (s), sizeof(buf)); \
2286 RTFLAG("U", RTF_UP
);
2287 RTFLAG("G", RTF_GATEWAY
);
2288 RTFLAG("H", RTF_HOST
);
2289 RTFLAG("R", RTF_REJECT
);
2290 RTFLAG("D", RTF_DYNAMIC
);
2291 RTFLAG("M", RTF_MODIFIED
);
2292 RTFLAG("d", RTF_DONE
);
2294 RTFLAG("m", RTF_MASK
);
2296 RTFLAG("C", RTF_CLONING
);
2298 RTFLAG("c", RTF_CLONED
);
2300 #ifdef RTF_PRCLONING
2301 RTFLAG("c", RTF_PRCLONING
);
2303 #ifdef RTF_WASCLONED
2304 RTFLAG("W", RTF_WASCLONED
);
2306 RTFLAG("X", RTF_XRESOLVE
);
2307 RTFLAG("L", RTF_LLINFO
);
2308 RTFLAG("S", RTF_STATIC
);
2309 RTFLAG("B", RTF_BLACKHOLE
);
2311 RTFLAG("3", RTF_PROTO3
);
2313 RTFLAG("2", RTF_PROTO2
);
2314 RTFLAG("1", RTF_PROTO1
);
2315 #ifdef RTF_BROADCAST
2316 RTFLAG("b", RTF_BROADCAST
);
2319 RTFLAG("d", RTF_DEFAULT
);
2321 #ifdef RTF_ISAROUTER
2322 RTFLAG("r", RTF_ISAROUTER
);
2325 RTFLAG("T", RTF_TUNNEL
);
2328 RTFLAG("A", RTF_AUTH
);
2331 RTFLAG("E", RTF_CRYPT
);
2340 static char buf
[BUFSIZ
];
2342 strlcpy(buf
, "", sizeof(buf
));
2343 #define IFFLAG(s, f) \
2347 strlcat(buf, ",", sizeof(buf)); \
2348 strlcat(buf, s, sizeof(buf)); \
2351 IFFLAG("UP", IFF_UP
);
2352 IFFLAG("BROADCAST", IFF_BROADCAST
);
2353 IFFLAG("DEBUG", IFF_DEBUG
);
2354 IFFLAG("LOOPBACK", IFF_LOOPBACK
);
2355 IFFLAG("POINTOPOINT", IFF_POINTOPOINT
);
2356 #ifdef IFF_NOTRAILERS
2357 IFFLAG("NOTRAILERS", IFF_NOTRAILERS
);
2360 IFFLAG("SMART", IFF_SMART
);
2362 IFFLAG("RUNNING", IFF_RUNNING
);
2363 IFFLAG("NOARP", IFF_NOARP
);
2364 IFFLAG("PROMISC", IFF_PROMISC
);
2365 IFFLAG("ALLMULTI", IFF_ALLMULTI
);
2366 IFFLAG("OACTIVE", IFF_OACTIVE
);
2367 IFFLAG("SIMPLEX", IFF_SIMPLEX
);
2368 IFFLAG("LINK0", IFF_LINK0
);
2369 IFFLAG("LINK1", IFF_LINK1
);
2370 IFFLAG("LINK2", IFF_LINK2
);
2371 IFFLAG("MULTICAST", IFF_MULTICAST
);
2381 char *buf
, *p
, *lim
;
2382 struct rt_msghdr
*rtm
;
2391 mib
[3] = AF_INET6
; /* Address family */
2392 mib
[4] = NET_RT_DUMP
; /* Dump the kernel routing table */
2393 mib
[5] = 0; /* No flags */
2399 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2400 errmsg
= "sysctl estimate";
2403 if ((buf
= malloc(msize
)) == NULL
) {
2407 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2408 errmsg
= "sysctl NET_RT_DUMP";
2411 } while (retry
< 5 && errmsg
!= NULL
);
2413 fatal("%s (with %d retries, msize=%lu)", errmsg
, retry
,
2416 } else if (1 < retry
)
2417 syslog(LOG_INFO
, "NET_RT_DUMP %d retires", retry
);
2420 for (p
= buf
; p
< lim
; p
+= rtm
->rtm_msglen
) {
2421 rtm
= (struct rt_msghdr
*)p
;
2422 rt_entry(rtm
, again
);
2428 rt_entry(struct rt_msghdr
*rtm
, int again
)
2430 struct sockaddr_in6
*sin6_dst
, *sin6_gw
, *sin6_mask
;
2431 struct sockaddr_in6
*sin6_genmask
, *sin6_ifp
;
2432 char *rtmp
, *ifname
= NULL
;
2433 struct riprt
*rrt
, *orrt
;
2434 struct netinfo6
*np
;
2437 sin6_dst
= sin6_gw
= sin6_mask
= sin6_genmask
= sin6_ifp
= NULL
;
2438 if ((rtm
->rtm_flags
& RTF_UP
) == 0 || rtm
->rtm_flags
&
2439 (RTF_CLONING
|RTF_XRESOLVE
|RTF_LLINFO
|RTF_BLACKHOLE
)) {
2440 return; /* not interested in the link route */
2442 /* do not look at cloned routes */
2443 #ifdef RTF_WASCLONED
2444 if (rtm
->rtm_flags
& RTF_WASCLONED
)
2448 if (rtm
->rtm_flags
& RTF_CLONED
)
2452 * do not look at dynamic routes.
2453 * netbsd/openbsd cloned routes have UGHD.
2455 if (rtm
->rtm_flags
& RTF_DYNAMIC
)
2457 rtmp
= (char *)(rtm
+ 1);
2459 if ((rtm
->rtm_addrs
& RTA_DST
) == 0)
2460 return; /* ignore routes without destination address */
2461 sin6_dst
= (struct sockaddr_in6
*)rtmp
;
2462 rtmp
+= RT_ROUNDUP(sin6_dst
->sin6_len
);
2463 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2464 sin6_gw
= (struct sockaddr_in6
*)rtmp
;
2465 rtmp
+= RT_ROUNDUP(sin6_gw
->sin6_len
);
2467 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
2468 sin6_mask
= (struct sockaddr_in6
*)rtmp
;
2469 rtmp
+= RT_ROUNDUP(sin6_mask
->sin6_len
);
2471 if (rtm
->rtm_addrs
& RTA_GENMASK
) {
2472 sin6_genmask
= (struct sockaddr_in6
*)rtmp
;
2473 rtmp
+= RT_ROUNDUP(sin6_genmask
->sin6_len
);
2475 if (rtm
->rtm_addrs
& RTA_IFP
) {
2476 sin6_ifp
= (struct sockaddr_in6
*)rtmp
;
2477 rtmp
+= RT_ROUNDUP(sin6_ifp
->sin6_len
);
2481 if (sin6_dst
->sin6_family
!= AF_INET6
)
2483 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst
->sin6_addr
))
2484 return; /* Link-local */
2485 if (IN6_ARE_ADDR_EQUAL(&sin6_dst
->sin6_addr
, &in6addr_loopback
))
2486 return; /* Loopback */
2487 if (IN6_IS_ADDR_MULTICAST(&sin6_dst
->sin6_addr
))
2490 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2491 fatal("malloc: struct riprt");
2494 memset(rrt
, 0, sizeof(*rrt
));
2495 np
= &rrt
->rrt_info
;
2496 rrt
->rrt_same
= NULL
;
2497 rrt
->rrt_t
= time(NULL
);
2498 if (aflag
== 0 && (rtm
->rtm_flags
& RTF_STATIC
))
2499 rrt
->rrt_t
= 0; /* Don't age static routes */
2501 np
->rip6_tag
= htons(routetag
& 0xffff);
2505 np
->rip6_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
2506 if (np
->rip6_metric
< 1)
2507 np
->rip6_metric
= 1;
2508 rrt
->rrt_flags
= rtm
->rtm_flags
;
2509 np
->rip6_dest
= sin6_dst
->sin6_addr
;
2512 if (rtm
->rtm_flags
& RTF_HOST
)
2513 np
->rip6_plen
= 128; /* Host route */
2515 np
->rip6_plen
= sin6mask2len(sin6_mask
);
2519 orrt
= rtsearch(np
, NULL
);
2520 if (orrt
&& orrt
->rrt_info
.rip6_metric
!= HOPCNT_INFINITY6
) {
2523 trace(1, "route: %s/%d flags %s: already registered\n",
2524 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2532 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2534 if (sin6_gw
->sin6_family
== AF_INET6
)
2535 rrt
->rrt_gw
= sin6_gw
->sin6_addr
;
2536 else if (sin6_gw
->sin6_family
== AF_LINK
) {
2537 /* XXX in case ppp link? */
2538 rrt
->rrt_gw
= in6addr_loopback
;
2540 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2542 trace(1, "route: %s/%d flags %s",
2543 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, rtflags(rtm
));
2544 trace(1, " gw %s", inet6_n2p(&rrt
->rrt_gw
));
2548 if (s
< nindex2ifc
&& index2ifc
[s
])
2549 ifname
= index2ifc
[s
]->ifc_name
;
2551 trace(1, " not configured\n");
2555 trace(1, " if %s sock %d", ifname
, s
);
2561 if (!IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_gw
) &&
2562 !IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)
2563 #ifdef __DragonFly__
2564 && (rrt
->rrt_flags
& RTF_LOCAL
) == 0
2567 trace(0, "***** Gateway %s is not a link-local address.\n",
2568 inet6_n2p(&rrt
->rrt_gw
));
2569 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2570 inet6_n2p(&rrt
->rrt_info
.rip6_dest
), ifname
);
2571 rrt
->rrt_rflags
|= RRTF_NH_NOT_LLADDR
;
2574 /* Put it to the route list */
2575 if (orrt
&& orrt
->rrt_info
.rip6_metric
== HOPCNT_INFINITY6
) {
2576 /* replace route list */
2577 rrt
->rrt_next
= orrt
->rrt_next
;
2579 trace(1, "route: %s/%d flags %s: replace new route\n",
2580 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2584 rrt
->rrt_next
= riprt
;
2590 addroute(struct riprt
*rrt
, const struct in6_addr
*gw
, struct ifc
*ifcp
)
2592 struct netinfo6
*np
;
2593 u_char buf
[BUFSIZ
], buf1
[BUFSIZ
], buf2
[BUFSIZ
];
2594 struct rt_msghdr
*rtm
;
2595 struct sockaddr_in6
*sin
;
2598 np
= &rrt
->rrt_info
;
2599 inet_ntop(AF_INET6
, (const void *)gw
, (char *)buf1
, sizeof(buf1
));
2600 inet_ntop(AF_INET6
, (void *)&ifcp
->ifc_mylladdr
, (char *)buf2
, sizeof(buf2
));
2601 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2602 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2603 np
->rip6_metric
- 1, buf2
);
2605 fprintf(rtlog
, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2606 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2607 np
->rip6_metric
- 1, buf2
);
2611 memset(buf
, 0, sizeof(buf
));
2612 rtm
= (struct rt_msghdr
*)buf
;
2613 rtm
->rtm_type
= RTM_ADD
;
2614 rtm
->rtm_version
= RTM_VERSION
;
2615 rtm
->rtm_seq
= ++seq
;
2617 rtm
->rtm_flags
= rrt
->rrt_flags
;
2618 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2619 rtm
->rtm_rmx
.rmx_hopcount
= np
->rip6_metric
- 1;
2620 rtm
->rtm_inits
= RTV_HOPCOUNT
;
2621 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2623 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2624 sin
->sin6_family
= AF_INET6
;
2625 sin
->sin6_addr
= np
->rip6_dest
;
2626 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2628 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2629 sin
->sin6_family
= AF_INET6
;
2630 sin
->sin6_addr
= *gw
;
2631 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2633 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2634 sin
->sin6_family
= AF_INET6
;
2635 sin
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2636 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2638 len
= (char *)sin
- (char *)buf
;
2639 rtm
->rtm_msglen
= len
;
2640 if (write(rtsock
, buf
, len
) > 0)
2643 if (errno
== EEXIST
) {
2644 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2645 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2647 fprintf(rtlog
, "ADD: Route already exists %s/%d gw %s\n",
2648 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2650 trace(0, "Can not write to rtsock (addroute): %s\n",
2653 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2660 delroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2662 u_char buf
[BUFSIZ
], buf2
[BUFSIZ
];
2663 struct rt_msghdr
*rtm
;
2664 struct sockaddr_in6
*sin
;
2667 inet_ntop(AF_INET6
, (void *)gw
, (char *)buf2
, sizeof(buf2
));
2668 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np
->rip6_dest
),
2669 np
->rip6_plen
, buf2
);
2671 fprintf(rtlog
, "%s: DEL: %s/%d gw %s\n",
2672 hms(), inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2676 memset(buf
, 0, sizeof(buf
));
2677 rtm
= (struct rt_msghdr
*)buf
;
2678 rtm
->rtm_type
= RTM_DELETE
;
2679 rtm
->rtm_version
= RTM_VERSION
;
2680 rtm
->rtm_seq
= ++seq
;
2682 rtm
->rtm_flags
= RTF_UP
| RTF_GATEWAY
;
2683 if (np
->rip6_plen
== sizeof(struct in6_addr
) * 8)
2684 rtm
->rtm_flags
|= RTF_HOST
;
2685 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2686 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2688 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2689 sin
->sin6_family
= AF_INET6
;
2690 sin
->sin6_addr
= np
->rip6_dest
;
2691 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2693 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2694 sin
->sin6_family
= AF_INET6
;
2695 sin
->sin6_addr
= *gw
;
2696 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2698 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2699 sin
->sin6_family
= AF_INET6
;
2700 sin
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2701 sin
= (struct sockaddr_in6
*)((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2703 len
= (char *)sin
- (char *)buf
;
2704 rtm
->rtm_msglen
= len
;
2705 if (write(rtsock
, buf
, len
) >= 0)
2708 if (errno
== ESRCH
) {
2709 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2710 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2712 fprintf(rtlog
, "RTDEL: Route does not exist: %s/%d gw %s\n",
2713 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2715 trace(0, "Can not write to rtsock (delroute): %s\n",
2718 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2725 getroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2730 struct rt_msghdr
*rtm
;
2731 struct sockaddr_in6
*sin
;
2733 rtm
= (struct rt_msghdr
*)buf
;
2734 len
= sizeof(struct rt_msghdr
) + sizeof(struct sockaddr_in6
);
2735 memset(rtm
, 0, len
);
2736 rtm
->rtm_type
= RTM_GET
;
2737 rtm
->rtm_version
= RTM_VERSION
;
2739 rtm
->rtm_seq
= myseq
;
2740 rtm
->rtm_addrs
= RTA_DST
;
2741 rtm
->rtm_msglen
= len
;
2742 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2743 sin
->sin6_len
= sizeof(struct sockaddr_in6
);
2744 sin
->sin6_family
= AF_INET6
;
2745 sin
->sin6_addr
= np
->rip6_dest
;
2746 if (write(rtsock
, buf
, len
) < 0) {
2747 if (errno
== ESRCH
) /* No such route found */
2749 perror("write to rtsock");
2753 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
2754 perror("read from rtsock");
2757 rtm
= (struct rt_msghdr
*)buf
;
2758 } while (rtm
->rtm_seq
!= myseq
|| rtm
->rtm_pid
!= pid
);
2759 sin
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2760 if (rtm
->rtm_addrs
& RTA_DST
) {
2761 sin
= (struct sockaddr_in6
*)
2762 ((char *)sin
+ RT_ROUNDUP(sin
->sin6_len
));
2764 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2765 *gw
= sin
->sin6_addr
;
2772 inet6_n2p(const struct in6_addr
*p
)
2774 static char buf
[BUFSIZ
];
2776 return inet_ntop(AF_INET6
, (const void *)p
, buf
, sizeof(buf
));
2796 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
2799 fprintf(dump
, "%s: Interface Table Dump\n", hms());
2800 fprintf(dump
, " Number of interfaces: %d\n", nifc
);
2801 for (i
= 0; i
< 2; i
++) {
2802 fprintf(dump
, " %sadvertising interfaces:\n", i
? "non-" : "");
2803 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
2805 if ((ifcp
->ifc_flags
& IFF_UP
) == 0)
2807 if (iff_find(ifcp
, 'N') != NULL
)
2810 if (ifcp
->ifc_flags
& IFF_UP
)
2813 ifdump0(dump
, ifcp
);
2816 fprintf(dump
, "\n");
2822 ifdump0(FILE *dump
, const struct ifc
*ifcp
)
2830 fprintf(dump
, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
2831 ifcp
->ifc_name
, ifcp
->ifc_index
, ifflags(ifcp
->ifc_flags
),
2832 inet6_n2p(&ifcp
->ifc_mylladdr
),
2833 ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
2834 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2835 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2836 inet_ntop(AF_INET6
, (void *)&ifa
->ifa_raddr
,
2838 fprintf(dump
, "\t%s/%d -- %s\n",
2839 inet6_n2p(&ifa
->ifa_addr
),
2840 ifa
->ifa_plen
, buf
);
2842 fprintf(dump
, "\t%s/%d\n",
2843 inet6_n2p(&ifa
->ifa_addr
),
2847 if (ifcp
->ifc_filter
) {
2848 fprintf(dump
, "\tFilter:");
2849 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
2851 switch (iffp
->iff_type
) {
2853 ft
= "Aggregate"; addr
++; break;
2855 ft
= "No-use"; break;
2857 ft
= "Advertise-only"; addr
++; break;
2859 ft
= "Default-only"; break;
2861 ft
= "Listen-only"; addr
++; break;
2863 snprintf(buf
, sizeof(buf
), "Unknown-%c", iffp
->iff_type
);
2868 fprintf(dump
, " %s", ft
);
2870 fprintf(dump
, "(%s/%d)", inet6_n2p(&iffp
->iff_addr
),
2874 fprintf(dump
, "\n");
2889 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
2893 fprintf(dump
, "\n%s: Routing Table Dump\n", hms());
2894 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
2895 if (rrt
->rrt_t
== 0)
2898 age
= t
- rrt
->rrt_t
;
2899 inet_ntop(AF_INET6
, (void *)&rrt
->rrt_info
.rip6_dest
,
2901 fprintf(dump
, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
2902 buf
, rrt
->rrt_info
.rip6_plen
, rrt
->rrt_index
,
2903 index2ifc
[rrt
->rrt_index
]->ifc_name
,
2904 inet6_n2p(&rrt
->rrt_gw
),
2905 rrt
->rrt_info
.rip6_metric
, (long)age
);
2906 if (rrt
->rrt_info
.rip6_tag
) {
2907 fprintf(dump
, " tag(0x%04x)",
2908 ntohs(rrt
->rrt_info
.rip6_tag
) & 0xffff);
2910 if (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)
2911 fprintf(dump
, " NOT-LL");
2912 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
2913 fprintf(dump
, " NO-ADV");
2914 fprintf(dump
, "\n");
2916 fprintf(dump
, "\n");
2922 * Parse the -A (and -O) options and put corresponding filter object to the
2923 * specified interface structures. Each of the -A/O option has the following
2924 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
2925 * -O 5f09:c400::/32,ef0,ef1 (only when match)
2931 char *p
, *ap
, *iflp
, *ifname
;
2932 struct iff ftmp
, *iff_obj
;
2939 for (i
= 0; i
< nfilter
; i
++) {
2943 if (filtertype
[i
] == 'N' || filtertype
[i
] == 'T') {
2947 if ((p
= strchr(ap
, ',')) != NULL
) {
2951 if ((p
= strchr(ap
, '/')) == NULL
) {
2952 fatal("no prefixlen specified for '%s'", ap
);
2956 if (inet_pton(AF_INET6
, ap
, &ftmp
.iff_addr
) != 1) {
2957 fatal("invalid prefix specified for '%s'", ap
);
2960 ftmp
.iff_plen
= atoi(p
);
2961 ftmp
.iff_next
= NULL
;
2962 applyplen(&ftmp
.iff_addr
, ftmp
.iff_plen
);
2964 ftmp
.iff_type
= filtertype
[i
];
2965 if (iflp
== NULL
|| *iflp
== '\0') {
2966 fatal("no interface specified for '%s'", ap
);
2969 /* parse the interface listing portion */
2972 if ((iflp
= strchr(iflp
, ',')) != NULL
)
2974 ifcp
= ifc_find(ifname
);
2976 fatal("no interface %s exists", ifname
);
2979 iff_obj
= (struct iff
*)malloc(sizeof(struct iff
));
2980 if (iff_obj
== NULL
) {
2981 fatal("malloc of iff_obj");
2984 memcpy((void *)iff_obj
, (void *)&ftmp
,
2985 sizeof(struct iff
));
2986 /* link it to the interface filter */
2987 iff_obj
->iff_next
= ifcp
->ifc_filter
;
2988 ifcp
->ifc_filter
= iff_obj
;
2992 * -A: aggregate configuration.
2994 if (filtertype
[i
] != 'A')
2996 /* put the aggregate to the kernel routing table */
2997 rrt
= (struct riprt
*)malloc(sizeof(struct riprt
));
2999 fatal("malloc: rrt");
3002 memset(rrt
, 0, sizeof(struct riprt
));
3003 rrt
->rrt_info
.rip6_dest
= ftmp
.iff_addr
;
3004 rrt
->rrt_info
.rip6_plen
= ftmp
.iff_plen
;
3005 rrt
->rrt_info
.rip6_metric
= 1;
3006 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
3007 rrt
->rrt_gw
= in6addr_loopback
;
3008 rrt
->rrt_flags
= RTF_UP
| RTF_REJECT
;
3009 rrt
->rrt_rflags
= RRTF_AGGREGATE
;
3011 rrt
->rrt_index
= loopifindex
;
3013 if (getroute(&rrt
->rrt_info
, &gw
)) {
3016 * When the address has already been registered in the
3017 * kernel routing table, it should be removed
3019 delroute(&rrt
->rrt_info
, &gw
);
3021 /* it is safer behavior */
3023 fatal("%s/%u already in routing table, "
3025 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
3026 rrt
->rrt_info
.rip6_plen
);
3031 /* Put the route to the list */
3032 rrt
->rrt_next
= riprt
;
3034 trace(1, "Aggregate: %s/%d for %s\n",
3035 inet6_n2p(&ftmp
.iff_addr
), ftmp
.iff_plen
,
3037 /* Add this route to the kernel */
3038 if (nflag
) /* do not modify kernel routing table */
3040 addroute(rrt
, &in6addr_loopback
, loopifcp
);
3044 /***************** utility functions *****************/
3047 * Returns a pointer to ifac whose address and prefix length matches
3048 * with the address and prefix length specified in the arguments.
3051 ifa_match(const struct ifc
*ifcp
, const struct in6_addr
*ia
, int plen
)
3055 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
3056 if (IN6_ARE_ADDR_EQUAL(&ifa
->ifa_addr
, ia
) &&
3057 ifa
->ifa_plen
== plen
)
3064 * Return a pointer to riprt structure whose address and prefix length
3065 * matches with the address and prefix length found in the argument.
3066 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3069 rtsearch(struct netinfo6
*np
, struct riprt
**prev_rrt
)
3075 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
3076 if (rrt
->rrt_info
.rip6_plen
== np
->rip6_plen
&&
3077 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
3089 sin6mask2len(const struct sockaddr_in6
*sin6
)
3091 return mask2len(&sin6
->sin6_addr
,
3092 sin6
->sin6_len
- offsetof(struct sockaddr_in6
, sin6_addr
));
3096 mask2len(const struct in6_addr
*addr
, int lenlim
)
3099 const u_char
*p
= (const u_char
*)addr
;
3101 for (j
= 0; j
< lenlim
; j
++, p
++) {
3108 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3109 MASKLEN(0xfe, 7); break;
3110 MASKLEN(0xfc, 6); break;
3111 MASKLEN(0xf8, 5); break;
3112 MASKLEN(0xf0, 4); break;
3113 MASKLEN(0xe0, 3); break;
3114 MASKLEN(0xc0, 2); break;
3115 MASKLEN(0x80, 1); break;
3123 applymask(struct in6_addr
*addr
, struct in6_addr
*mask
)
3128 p
= (u_long
*)addr
; q
= (u_long
*)mask
;
3129 for (i
= 0; i
< 4; i
++)
3133 static const u_char plent
[8] = {
3134 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3138 applyplen(struct in6_addr
*ia
, int plen
)
3144 for (i
= 0; i
< 16; i
++) {
3153 static const int pl2m
[9] = {
3154 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3160 static struct in6_addr ia
;
3164 memset(&ia
, 0, sizeof(struct in6_addr
));
3166 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
3180 char *q
= (char *)malloc(strlen(p
) + 1);
3189 static char buf
[BUFSIZ
];
3194 if ((tm
= localtime(&t
)) == NULL
) {
3198 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3203 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3206 ripinterval(int timer
)
3210 interval
= (int)(timer
+ timer
* RIPRANDDEV
* (r
/ RAND_MAX
- 0.5));
3211 nextalarm
= time(NULL
) + interval
;
3221 t
= (int)(RIP_TRIG_INT6_MIN
+
3222 (RIP_TRIG_INT6_MAX
- RIP_TRIG_INT6_MIN
) * (r
/ RAND_MAX
));
3223 sup_trig_update
= time(NULL
) + t
;
3228 fatal(const char *fmt
, ...)
3234 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
3236 syslog(LOG_ERR
, "%s: %s", buf
, strerror(errno
));
3242 tracet(int level
, const char *fmt
, ...)
3247 if (level
<= dflag
) {
3248 fprintf(stderr
, "%s: ", hms());
3249 vfprintf(stderr
, fmt
, ap
);
3253 vsyslog(LOG_DEBUG
, fmt
, ap
);
3255 vsyslog(LOG_WARNING
, fmt
, ap
);
3261 trace(int level
, const char *fmt
, ...)
3267 vfprintf(stderr
, fmt
, ap
);
3270 vsyslog(LOG_DEBUG
, fmt
, ap
);
3272 vsyslog(LOG_WARNING
, fmt
, ap
);
3280 struct if_nameindex
*p
, *p0
;
3281 unsigned int max
= 0;
3283 p0
= if_nameindex();
3284 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
3285 if (max
< p
->if_index
)
3288 if_freenameindex(p0
);
3293 ifc_find(char *name
)
3297 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
3298 if (strcmp(name
, ifcp
->ifc_name
) == 0)
3305 iff_find(struct ifc
*ifcp
, int type
)
3309 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
3310 if (iffp
->iff_type
== type
)
3317 setindex2ifc(int idx
, struct ifc
*ifcp
)
3323 nindex2ifc
= 5; /*initial guess*/
3324 index2ifc
= (struct ifc
**)
3325 malloc(sizeof(*index2ifc
) * nindex2ifc
);
3326 if (index2ifc
== NULL
) {
3330 memset(index2ifc
, 0, sizeof(*index2ifc
) * nindex2ifc
);
3333 while (nindex2ifc
<= idx
)
3335 if (n
!= nindex2ifc
) {
3336 p
= (struct ifc
**)realloc(index2ifc
,
3337 sizeof(*index2ifc
) * nindex2ifc
);
3342 memset(p
+ n
, 0, sizeof(*index2ifc
) * (nindex2ifc
- n
));
3345 index2ifc
[idx
] = ifcp
;