2 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char _rcsid
[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
57 #include <sys/types.h>
58 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
62 #include <sys/sysctl.h>
65 #include <net/if_var.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet/udp.h>
76 #include <arpa/inet.h>
83 #define INIT_INTERVAL6 6
85 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
88 /* alignment constraint for routing socket */
90 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
91 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
94 * Following two macros are highly depending on KAME Release
96 #define IN6_LINKLOCAL_IFINDEX(addr) \
97 ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
99 #define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
101 (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
102 (addr).s6_addr[3] = (index) & 0xff; \
105 struct ifc
{ /* Configuration of an interface */
106 char *ifc_name
; /* if name */
107 struct ifc
*ifc_next
;
108 int ifc_index
; /* if index */
109 int ifc_mtu
; /* if mtu */
110 int ifc_metric
; /* if metric */
111 u_int ifc_flags
; /* flags */
112 short ifc_cflags
; /* IFC_XXX */
113 struct in6_addr ifc_mylladdr
; /* my link-local address */
114 struct sockaddr_in6 ifc_ripsin
; /* rip multicast address */
115 struct iff
*ifc_filter
; /* filter structure */
116 struct ifac
*ifc_addr
; /* list of AF_INET6 addresses */
117 int ifc_joined
; /* joined to ff02::9 */
120 struct ifac
{ /* Adddress associated to an interface */
121 struct ifc
*ifa_conf
; /* back pointer */
122 struct ifac
*ifa_next
;
123 struct in6_addr ifa_addr
; /* address */
124 struct in6_addr ifa_raddr
; /* remote address, valid in p2p */
125 int ifa_plen
; /* prefix length */
130 struct in6_addr iff_addr
;
132 struct iff
*iff_next
;
136 int nifc
; /* number of valid ifc's */
137 struct ifc
**index2ifc
;
139 struct ifc
*loopifcp
= NULL
; /* pointing to loopback */
141 struct pollfd set
[2];
143 fd_set
*sockvecp
; /* vector to select() for receiving */
146 int maxfd
; /* maximum fd for select() */
148 int rtsock
; /* the routing socket */
149 int ripsock
; /* socket to send/receive RIP datagram */
151 struct rip6
*ripbuf
; /* packet buffer for sending */
154 * Maintain the routes in a linked list. When the number of the routes
155 * grows, somebody would like to introduce a hash based or a radix tree
156 * based structure. I believe the number of routes handled by RIP is
157 * limited and I don't have to manage a complex data structure, however.
159 * One of the major drawbacks of the linear linked list is the difficulty
160 * of representing the relationship between a couple of routes. This may
161 * be a significant problem when we have to support route aggregation with
162 * supressing the specifices covered by the aggregate.
166 struct riprt
*rrt_next
; /* next destination */
167 struct riprt
*rrt_same
; /* same destination - future use */
168 struct netinfo6 rrt_info
; /* network info */
169 struct in6_addr rrt_gw
; /* gateway */
170 u_long rrt_flags
; /* kernel routing table flags */
171 u_long rrt_rflags
; /* route6d routing table flags */
172 time_t rrt_t
; /* when the route validated */
173 int rrt_index
; /* ifindex from which this route got */
176 struct riprt
*riprt
= 0;
178 int dflag
= 0; /* debug flag */
179 int qflag
= 0; /* quiet flag */
180 int nflag
= 0; /* don't update kernel routing table */
181 int aflag
= 0; /* age out even the statically defined routes */
182 int hflag
= 0; /* don't split horizon */
183 int lflag
= 0; /* exchange site local routes */
184 int sflag
= 0; /* announce static routes w/ split horizon */
185 int Sflag
= 0; /* announce static routes to every interface */
186 unsigned long routetag
= 0; /* route tag attached on originating case */
188 char *filter
[MAXFILTER
];
189 int filtertype
[MAXFILTER
];
194 struct sockaddr_storage ripsin
;
197 time_t nextalarm
= 0;
198 time_t sup_trig_update
= 0;
206 volatile sig_atomic_t seenalrm
;
207 volatile sig_atomic_t seenquit
;
208 volatile sig_atomic_t seenusr1
;
210 #define RRTF_AGGREGATE 0x08000000
211 #define RRTF_NOADVERTISE 0x10000000
212 #define RRTF_NH_NOT_LLADDR 0x20000000
213 #define RRTF_SENDANYWAY 0x40000000
214 #define RRTF_CHANGED 0x80000000
216 int main(int, char **);
217 void sighandler(int);
220 void ripsend(struct ifc
*, struct sockaddr_in6
*, int);
221 int out_filter(struct riprt
*, struct ifc
*);
223 void sockopt(struct ifc
*);
225 void ifconfig1(const char *, const struct sockaddr
*, struct ifc
*, int);
227 int rt_del(const struct sockaddr_in6
*, const struct sockaddr_in6
*,
228 const struct sockaddr_in6
*);
229 int rt_deladdr(struct ifc
*, const struct sockaddr_in6
*,
230 const struct sockaddr_in6
*);
231 void filterconfig(void);
233 const char *rttypes(struct rt_msghdr
*);
234 const char *rtflags(struct rt_msghdr
*);
235 const char *ifflags(int);
236 int ifrt(struct ifc
*, int);
237 void ifrt_p2p(struct ifc
*, int);
238 void applymask(struct in6_addr
*, struct in6_addr
*);
239 void applyplen(struct in6_addr
*, int);
242 void ifdump0(FILE *, const struct ifc
*);
244 void rt_entry(struct rt_msghdr
*, int);
246 void riprequest(struct ifc
*, struct netinfo6
*, int,
247 struct sockaddr_in6
*);
248 void ripflush(struct ifc
*, struct sockaddr_in6
*);
249 void sendrequest(struct ifc
*);
250 int sin6mask2len(const struct sockaddr_in6
*);
251 int mask2len(const struct in6_addr
*, int);
252 int sendpacket(struct sockaddr_in6
*, int);
253 int addroute(struct riprt
*, const struct in6_addr
*, struct ifc
*);
254 int delroute(struct netinfo6
*, struct in6_addr
*);
255 struct in6_addr
*getroute(struct netinfo6
*, struct in6_addr
*);
257 int tobeadv(struct riprt
*, struct ifc
*);
258 char *allocopy(char *);
260 const char *inet6_n2p(const struct in6_addr
*);
261 struct ifac
*ifa_match(const struct ifc
*, const struct in6_addr
*, int);
262 struct in6_addr
*plen2mask(int);
263 struct riprt
*rtsearch(struct netinfo6
*, struct riprt
**);
264 int ripinterval(int);
265 time_t ripsuptrig(void);
266 void fatal(const char *, ...)
267 __attribute__((__format__(__printf__
, 1, 2)));
268 void trace(int, const char *, ...)
269 __attribute__((__format__(__printf__
, 2, 3)));
270 void tracet(int, const char *, ...)
271 __attribute__((__format__(__printf__
, 2, 3)));
272 unsigned int if_maxindex(void);
273 struct ifc
*ifc_find(char *);
274 struct iff
*iff_find(struct ifc
*, int);
275 void setindex2ifc(int, struct ifc
*);
277 #define MALLOC(type) ((type *)malloc(sizeof(type)))
287 sigset_t mask
, omask
;
292 progname
= strrchr(*argv
, '/');
299 while ((ch
= getopt(argc
, argv
, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
306 if (nfilter
>= MAXFILTER
) {
307 fatal("Exceeds MAXFILTER");
310 filtertype
[nfilter
] = ch
;
311 filter
[nfilter
++] = allocopy(optarg
);
315 routetag
= strtoul(optarg
, &ep
, 0);
316 if (!ep
|| *ep
!= '\0' || (routetag
& ~0xffff) != 0) {
317 fatal("invalid route tag");
322 if ((rtlog
= fopen(optarg
, "w")) == NULL
) {
323 fatal("Can not write to routelog");
327 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
328 FLAG('a', aflag
, 1); break;
329 FLAG('d', dflag
, 1); break;
330 FLAG('D', dflag
, 2); break;
331 FLAG('h', hflag
, 1); break;
332 FLAG('l', lflag
, 1); break;
333 FLAG('n', nflag
, 1); break;
334 FLAG('q', qflag
, 1); break;
335 FLAG('s', sflag
, 1); break;
336 FLAG('S', Sflag
, 1); break;
339 fatal("Invalid option specified, terminating");
346 fatal("bogus extra arguments");
352 fprintf(stderr
, "No kernel update is allowed\n");
356 if (daemon(0, 0) < 0) {
362 openlog(progname
, LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
365 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
)
367 memset(ripbuf
, 0, RIP6_MAXMTU
);
368 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
369 ripbuf
->rip6_vers
= RIP6_VERSION
;
370 ripbuf
->rip6_res1
[0] = 0;
371 ripbuf
->rip6_res1
[1] = 0;
375 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
376 if (ifcp
->ifc_index
< 0) {
378 "No ifindex found at %s (no link-local address?)\n",
385 if (loopifcp
== NULL
) {
386 fatal("No loopback found");
389 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
)
398 if ((pidfile
= fopen(ROUTE6D_PID
, "w")) != NULL
) {
399 fprintf(pidfile
, "%d\n", pid
);
404 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
) {
408 memset(ripbuf
, 0, RIP6_MAXMTU
);
409 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
410 ripbuf
->rip6_vers
= RIP6_VERSION
;
411 ripbuf
->rip6_res1
[0] = 0;
412 ripbuf
->rip6_res1
[1] = 0;
414 if (signal(SIGALRM
, sighandler
) == SIG_ERR
||
415 signal(SIGQUIT
, sighandler
) == SIG_ERR
||
416 signal(SIGTERM
, sighandler
) == SIG_ERR
||
417 signal(SIGUSR1
, sighandler
) == SIG_ERR
||
418 signal(SIGHUP
, sighandler
) == SIG_ERR
||
419 signal(SIGINT
, sighandler
) == SIG_ERR
) {
424 * To avoid rip packet congestion (not on a cable but in this
425 * process), wait for a moment to send the first RIP6_RESPONSE
428 alarm(ripinterval(INIT_INTERVAL6
));
430 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
431 if (iff_find(ifcp
, 'N'))
433 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
437 syslog(LOG_INFO
, "**** Started ****");
439 sigaddset(&mask
, SIGALRM
);
458 switch (poll(set
, 2, INFTIM
))
460 memcpy(recvecp
, sockvecp
, fdmasks
);
461 switch (select(maxfd
+ 1, recvecp
, 0, 0, 0))
465 if (errno
!= EINTR
) {
474 if (set
[0].revents
& POLLIN
)
476 if (FD_ISSET(ripsock
, recvecp
))
479 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
481 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
484 if (set
[1].revents
& POLLIN
)
486 if (FD_ISSET(rtsock
, recvecp
))
489 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
491 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
519 * gracefully exits after resetting sockopts.
528 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
529 if (rrt
->rrt_rflags
& RRTF_AGGREGATE
) {
530 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
535 syslog(LOG_INFO
, "**** Terminated ****");
541 * Called periodically:
542 * 1. age out the learned route. remove it if necessary.
543 * 2. submit RIP6_RESPONSE packets.
544 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
545 * to invoke this function in every 1 or 5 or 10 seconds only to age the
546 * routes more precisely.
553 struct riprt
*rrt
, *rrt_prev
, *rrt_next
;
554 time_t t_lifetime
, t_holddown
;
556 /* age the RIP routes */
558 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
559 t_holddown
= t_lifetime
- RIP_HOLDDOWN
;
560 for (rrt
= riprt
; rrt
; rrt
= rrt_next
) {
561 rrt_next
= rrt
->rrt_next
;
563 if (rrt
->rrt_t
== 0) {
567 if (rrt
->rrt_t
< t_holddown
) {
569 rrt_prev
->rrt_next
= rrt
->rrt_next
;
571 riprt
= rrt
->rrt_next
;
573 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
577 if (rrt
->rrt_t
< t_lifetime
)
578 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
582 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
583 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
584 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
586 alarm(ripinterval(SUPPLY_INTERVAL6
));
593 const int int0
= 0, int1
= 1, int255
= 255;
594 struct addrinfo hints
, *res
;
595 char port
[NI_MAXSERV
];
597 ifc
= (struct ifc
*)NULL
;
599 nindex2ifc
= 0; /*initial guess*/
601 snprintf(port
, sizeof(port
), "%u", RIP6_PORT
);
603 memset(&hints
, 0, sizeof(hints
));
604 hints
.ai_family
= PF_INET6
;
605 hints
.ai_socktype
= SOCK_DGRAM
;
606 hints
.ai_protocol
= IPPROTO_UDP
;
607 hints
.ai_flags
= AI_PASSIVE
;
608 error
= getaddrinfo(NULL
, port
, &hints
, &res
);
610 fatal("%s", gai_strerror(error
));
614 fatal(":: resolved to multiple address");
618 ripsock
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
624 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
625 &int1
, sizeof(int1
)) < 0) {
626 fatal("rip IPV6_V6ONLY");
630 if (bind(ripsock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
634 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
635 &int255
, sizeof(int255
)) < 0) {
636 fatal("rip IPV6_MULTICAST_HOPS");
639 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
640 &int0
, sizeof(int0
)) < 0) {
641 fatal("rip IPV6_MULTICAST_LOOP");
645 #ifdef IPV6_RECVPKTINFO
646 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
647 &int1
, sizeof(int1
)) < 0) {
648 fatal("rip IPV6_RECVPKTINFO");
651 #else /* old adv. API */
652 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_PKTINFO
,
653 &int1
, sizeof(int1
)) < 0) {
654 fatal("rip IPV6_PKTINFO");
659 #ifdef IPV6_RECVPKTINFO
660 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
,
661 &int1
, sizeof(int1
)) < 0) {
662 fatal("rip IPV6_RECVHOPLIMIT");
665 #else /* old adv. API */
666 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
,
667 &int1
, sizeof(int1
)) < 0) {
668 fatal("rip IPV6_HOPLIMIT");
673 memset(&hints
, 0, sizeof(hints
));
674 hints
.ai_family
= PF_INET6
;
675 hints
.ai_socktype
= SOCK_DGRAM
;
676 hints
.ai_protocol
= IPPROTO_UDP
;
677 error
= getaddrinfo(RIP6_DEST
, port
, &hints
, &res
);
679 fatal("%s", gai_strerror(error
));
683 fatal("%s resolved to multiple address", RIP6_DEST
);
686 memcpy(&ripsin
, res
->ai_addr
, res
->ai_addrlen
);
690 set
[0].events
= POLLIN
;
696 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
697 fatal("route socket");
702 set
[1].events
= POLLIN
;
711 rtsock
= -1; /*just for safety */
716 fdmasks
= howmany(maxfd
+ 1, NFDBITS
) * sizeof(fd_mask
);
717 if ((sockvecp
= malloc(fdmasks
)) == NULL
) {
721 if ((recvecp
= malloc(fdmasks
)) == NULL
) {
725 memset(sockvecp
, 0, fdmasks
);
726 FD_SET(ripsock
, sockvecp
);
728 FD_SET(rtsock
, sockvecp
);
733 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
736 * ripflush flushes the rip datagram stored in the rip buffer
739 static struct netinfo6
*np
;
744 struct sockaddr_in6
*sin6
;
750 tracet(1, "Send(%s): info(%d) to %s.%d\n",
752 inet6_n2p(&sin6
->sin6_addr
), ntohs(sin6
->sin6_port
));
754 tracet(1, "Send: info(%d) to %s.%d\n",
755 nrt
, inet6_n2p(&sin6
->sin6_addr
), ntohs(sin6
->sin6_port
));
757 np
= ripbuf
->rip6_nets
;
758 for (i
= 0; i
< nrt
; i
++, np
++) {
759 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
760 if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
))
761 trace(2, " NextHop reset");
763 trace(2, " NextHop %s",
764 inet6_n2p(&np
->rip6_dest
));
767 trace(2, " %s/%d[%d]",
768 inet6_n2p(&np
->rip6_dest
),
769 np
->rip6_plen
, np
->rip6_metric
);
772 trace(2, " tag=0x%04x",
773 ntohs(np
->rip6_tag
) & 0xffff);
778 error
= sendpacket(sin6
, RIPSIZE(nrt
));
779 if (error
== EAFNOSUPPORT
) {
780 /* Protocol not supported */
781 tracet(1, "Could not send info to %s (%s): "
783 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
784 ifcp
->ifc_flags
&= ~IFF_UP
; /* As if down for AF_INET6 */
786 nrt
= 0; np
= ripbuf
->rip6_nets
;
790 * Generate RIP6_RESPONSE packets and send them.
793 ripsend(ifcp
, sin6
, flag
)
795 struct sockaddr_in6
*sin6
;
799 struct in6_addr
*nh
; /* next hop */
807 * Request from non-link local address is not
808 * a regular route6d update.
810 maxrte
= (IFMINMTU
- sizeof(struct ip6_hdr
) -
811 sizeof(struct udphdr
) -
812 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
813 sizeof(struct netinfo6
);
814 nrt
= 0; np
= ripbuf
->rip6_nets
; nh
= NULL
;
815 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
816 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
818 /* Put the route to the buffer */
822 ripflush(NULL
, sin6
);
826 if (nrt
) /* Send last packet */
827 ripflush(NULL
, sin6
);
831 if ((flag
& RRTF_SENDANYWAY
) == 0 &&
832 (qflag
|| (ifcp
->ifc_flags
& IFF_LOOPBACK
)))
836 if (iff_find(ifcp
, 'N') != NULL
)
839 /* -T: generate default route only */
840 if (iff_find(ifcp
, 'T') != NULL
) {
841 struct netinfo6 rrt_info
;
842 memset(&rrt_info
, 0, sizeof(struct netinfo6
));
843 rrt_info
.rip6_dest
= in6addr_any
;
844 rrt_info
.rip6_plen
= 0;
845 rrt_info
.rip6_metric
= 1;
846 rrt_info
.rip6_metric
+= ifcp
->ifc_metric
;
847 rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
848 np
= ripbuf
->rip6_nets
;
851 ripflush(ifcp
, sin6
);
855 maxrte
= (ifcp
->ifc_mtu
- sizeof(struct ip6_hdr
) -
856 sizeof(struct udphdr
) -
857 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
858 sizeof(struct netinfo6
);
860 nrt
= 0; np
= ripbuf
->rip6_nets
; nh
= NULL
;
861 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
862 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
865 /* Need to check filter here */
866 if (out_filter(rrt
, ifcp
) == 0)
869 /* Check split horizon and other conditions */
870 if (tobeadv(rrt
, ifcp
) == 0)
873 /* Only considers the routes with flag if specified */
874 if ((flag
& RRTF_CHANGED
) &&
875 (rrt
->rrt_rflags
& RRTF_CHANGED
) == 0)
879 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
880 !IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_gw
) &&
881 (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
) == 0) {
882 if (nh
== NULL
|| !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
)) {
883 if (nrt
== maxrte
- 2)
884 ripflush(ifcp
, sin6
);
885 np
->rip6_dest
= rrt
->rrt_gw
;
886 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
))
887 SET_IN6_LINKLOCAL_IFINDEX(np
->rip6_dest
, 0);
890 np
->rip6_metric
= NEXTHOP_METRIC
;
894 } else if (nh
&& (rrt
->rrt_index
!= ifcp
->ifc_index
||
895 !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
) ||
896 rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)) {
898 if (nrt
== maxrte
- 2)
899 ripflush(ifcp
, sin6
);
900 memset(np
, 0, sizeof(struct netinfo6
));
901 np
->rip6_metric
= NEXTHOP_METRIC
;
906 /* Put the route to the buffer */
910 ripflush(ifcp
, sin6
);
914 if (nrt
) /* Send last packet */
915 ripflush(ifcp
, sin6
);
919 * outbound filter logic, per-route/interface.
922 out_filter(rrt
, ifcp
)
931 * -A: filter out less specific routes, if we have aggregated
934 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
935 if (iffp
->iff_type
!= 'A')
937 if (rrt
->rrt_info
.rip6_plen
<= iffp
->iff_plen
)
939 ia
= rrt
->rrt_info
.rip6_dest
;
940 applyplen(&ia
, iffp
->iff_plen
);
941 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
))
946 * if it is an aggregated route, advertise it only to the
947 * interfaces specified on -A.
949 if ((rrt
->rrt_rflags
& RRTF_AGGREGATE
) != 0) {
951 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
952 if (iffp
->iff_type
!= 'A')
954 if (rrt
->rrt_info
.rip6_plen
== iffp
->iff_plen
&&
955 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
966 * -O: advertise only if prefix matches the configured prefix.
968 if (iff_find(ifcp
, 'O')) {
970 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
971 if (iffp
->iff_type
!= 'O')
973 if (rrt
->rrt_info
.rip6_plen
< iffp
->iff_plen
)
975 ia
= rrt
->rrt_info
.rip6_dest
;
976 applyplen(&ia
, iffp
->iff_plen
);
977 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
986 /* the prefix should be advertised */
991 * Determine if the route is to be advertised on the specified interface.
992 * It checks options specified in the arguments and the split horizon rule.
1000 /* Special care for static routes */
1001 if (rrt
->rrt_flags
& RTF_STATIC
) {
1002 /* XXX don't advertise reject/blackhole routes */
1003 if (rrt
->rrt_flags
& (RTF_REJECT
| RTF_BLACKHOLE
))
1006 if (Sflag
) /* Yes, advertise it anyway */
1008 if (sflag
&& rrt
->rrt_index
!= ifcp
->ifc_index
)
1012 /* Regular split horizon */
1013 if (hflag
== 0 && rrt
->rrt_index
== ifcp
->ifc_index
)
1019 * Send a rip packet actually.
1022 sendpacket(sin6
, len
)
1023 struct sockaddr_in6
*sin6
;
1028 struct iovec iov
[2];
1029 u_char cmsgbuf
[256];
1030 struct in6_pktinfo
*pi
;
1032 struct sockaddr_in6 sincopy
;
1034 /* do not overwrite the given sin */
1038 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
1039 IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
1040 /* XXX: do not mix the interface index and link index */
1041 idx
= IN6_LINKLOCAL_IFINDEX(sin6
->sin6_addr
);
1042 SET_IN6_LINKLOCAL_IFINDEX(sin6
->sin6_addr
, 0);
1043 sin6
->sin6_scope_id
= idx
;
1047 m
.msg_name
= (caddr_t
)sin6
;
1048 m
.msg_namelen
= sizeof(*sin6
);
1049 iov
[0].iov_base
= (caddr_t
)ripbuf
;
1050 iov
[0].iov_len
= len
;
1054 m
.msg_control
= NULL
;
1055 m
.msg_controllen
= 0;
1057 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
1058 cm
= (struct cmsghdr
*)cmsgbuf
;
1059 m
.msg_control
= (caddr_t
)cm
;
1060 m
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
1062 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1063 cm
->cmsg_level
= IPPROTO_IPV6
;
1064 cm
->cmsg_type
= IPV6_PKTINFO
;
1065 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
1066 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*::*/
1067 pi
->ipi6_ifindex
= idx
;
1070 if (sendmsg(ripsock
, &m
, 0 /*MSG_DONTROUTE*/) < 0) {
1071 trace(1, "sendmsg: %s\n", strerror(errno
));
1079 * Receive and process RIP packets. Update the routes/kernel forwarding
1080 * table if necessary.
1085 struct ifc
*ifcp
, *ic
;
1086 struct sockaddr_in6 fsock
;
1087 struct in6_addr nh
; /* next hop */
1089 struct netinfo6
*np
, *nq
;
1092 unsigned int need_trigger
, idx
;
1093 char buf
[4 * RIP6_MAXMTU
];
1097 struct iovec iov
[2];
1098 u_char cmsgbuf
[256];
1099 struct in6_pktinfo
*pi
= NULL
;
1104 time_t t_half_lifetime
;
1108 m
.msg_name
= (caddr_t
)&fsock
;
1109 m
.msg_namelen
= sizeof(fsock
);
1110 iov
[0].iov_base
= (caddr_t
)buf
;
1111 iov
[0].iov_len
= sizeof(buf
);
1114 cm
= (struct cmsghdr
*)cmsgbuf
;
1115 m
.msg_control
= (caddr_t
)cm
;
1116 m
.msg_controllen
= sizeof(cmsgbuf
);
1117 if ((len
= recvmsg(ripsock
, &m
, 0)) < 0) {
1122 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&m
);
1124 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&m
, cm
)) {
1125 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1127 switch (cm
->cmsg_type
) {
1129 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(*pi
))) {
1131 "invalid cmsg length for IPV6_PKTINFO\n");
1134 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
1135 idx
= pi
->ipi6_ifindex
;
1138 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
1140 "invalid cmsg length for IPV6_HOPLIMIT\n");
1143 hlimp
= (int *)CMSG_DATA(cm
);
1147 if (idx
&& IN6_IS_ADDR_LINKLOCAL(&fsock
.sin6_addr
))
1148 SET_IN6_LINKLOCAL_IFINDEX(fsock
.sin6_addr
, idx
);
1150 if (len
< sizeof(struct rip6
)) {
1151 trace(1, "Packet too short\n");
1155 if (pi
== NULL
|| hlimp
== NULL
) {
1157 * This can happen when the kernel failed to allocate memory
1158 * for the ancillary data. Although we might be able to handle
1159 * some cases without this info, those are minor and not so
1160 * important, so it's better to discard the packet for safer
1163 trace(1, "IPv6 packet information cannot be retrieved\n");
1167 nh
= fsock
.sin6_addr
;
1168 nn
= (len
- sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
1169 sizeof(struct netinfo6
);
1170 rp
= (struct rip6
*)buf
;
1173 if (rp
->rip6_vers
!= RIP6_VERSION
) {
1174 trace(1, "Incorrect RIP version %d\n", rp
->rip6_vers
);
1177 if (rp
->rip6_cmd
== RIP6_REQUEST
) {
1178 if (idx
&& idx
< nindex2ifc
) {
1179 ifcp
= index2ifc
[idx
];
1180 riprequest(ifcp
, np
, nn
, &fsock
);
1182 riprequest(NULL
, np
, nn
, &fsock
);
1187 if (!IN6_IS_ADDR_LINKLOCAL(&fsock
.sin6_addr
)) {
1188 trace(1, "Response from non-ll addr: %s\n",
1189 inet6_n2p(&fsock
.sin6_addr
));
1190 return; /* Ignore packets from non-link-local addr */
1192 if (ntohs(fsock
.sin6_port
) != RIP6_PORT
) {
1193 trace(1, "Response from non-rip port from %s\n",
1194 inet6_n2p(&fsock
.sin6_addr
));
1197 if (IN6_IS_ADDR_MULTICAST(&pi
->ipi6_addr
) && *hlimp
!= 255) {
1199 "Response packet with a smaller hop limit (%d) from %s\n",
1200 *hlimp
, inet6_n2p(&fsock
.sin6_addr
));
1204 * Further validation: since this program does not send off-link
1205 * requests, an incoming response must always come from an on-link
1206 * node. Although this is normally ensured by the source address
1207 * check above, it may not 100% be safe because there are router
1208 * implementations that (invalidly) allow a packet with a link-local
1209 * source address to be forwarded to a different link.
1210 * So we also check whether the destination address is a link-local
1211 * address or the hop limit is 255. Note that RFC2080 does not require
1212 * the specific hop limit for a unicast response, so we cannot assume
1215 if (!IN6_IS_ADDR_LINKLOCAL(&pi
->ipi6_addr
) && *hlimp
!= 255) {
1217 "Response packet possibly from an off-link node: "
1218 "from %s to %s hlim=%d\n",
1219 inet6_n2p(&fsock
.sin6_addr
),
1220 inet6_n2p(&pi
->ipi6_addr
), *hlimp
);
1224 idx
= IN6_LINKLOCAL_IFINDEX(fsock
.sin6_addr
);
1225 ifcp
= (idx
< nindex2ifc
) ? index2ifc
[idx
] : NULL
;
1227 trace(1, "Packets to unknown interface index %d\n", idx
);
1228 return; /* Ignore it */
1230 if (IN6_ARE_ADDR_EQUAL(&ifcp
->ifc_mylladdr
, &fsock
.sin6_addr
))
1231 return; /* The packet is from me; ignore */
1232 if (rp
->rip6_cmd
!= RIP6_RESPONSE
) {
1233 trace(1, "Invalid command %d\n", rp
->rip6_cmd
);
1238 if (iff_find(ifcp
, 'N') != NULL
)
1241 tracet(1, "Recv(%s): from %s.%d info(%d)\n",
1242 ifcp
->ifc_name
, inet6_n2p(&nh
), ntohs(fsock
.sin6_port
), nn
);
1245 t_half_lifetime
= t
- (RIP_LIFETIME
/2);
1246 for (; nn
; nn
--, np
++) {
1247 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
1248 /* modify neighbor address */
1249 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1251 SET_IN6_LINKLOCAL_IFINDEX(nh
, idx
);
1252 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1253 } else if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
)) {
1254 nh
= fsock
.sin6_addr
;
1255 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1257 nh
= fsock
.sin6_addr
;
1258 trace(1, "\tInvalid Nexthop: %s\n",
1259 inet6_n2p(&np
->rip6_dest
));
1263 if (IN6_IS_ADDR_MULTICAST(&np
->rip6_dest
)) {
1264 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1265 inet6_n2p(&np
->rip6_dest
),
1266 np
->rip6_plen
, np
->rip6_metric
);
1269 if (IN6_IS_ADDR_LOOPBACK(&np
->rip6_dest
)) {
1270 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1271 inet6_n2p(&np
->rip6_dest
),
1272 np
->rip6_plen
, np
->rip6_metric
);
1275 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1276 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1277 inet6_n2p(&np
->rip6_dest
),
1278 np
->rip6_plen
, np
->rip6_metric
);
1281 /* may need to pass sitelocal prefix in some case, however*/
1282 if (IN6_IS_ADDR_SITELOCAL(&np
->rip6_dest
) && !lflag
) {
1283 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1284 inet6_n2p(&np
->rip6_dest
),
1285 np
->rip6_plen
, np
->rip6_metric
);
1288 trace(2, "\tnetinfo6: %s/%d [%d]",
1289 inet6_n2p(&np
->rip6_dest
),
1290 np
->rip6_plen
, np
->rip6_metric
);
1292 trace(2, " tag=0x%04x", ntohs(np
->rip6_tag
) & 0xffff);
1295 applyplen(&ia
, np
->rip6_plen
);
1296 if (!IN6_ARE_ADDR_EQUAL(&ia
, &np
->rip6_dest
))
1297 trace(2, " [junk outside prefix]");
1301 * -L: listen only if the prefix matches the configuration
1303 ok
= 1; /* if there's no L filter, it is ok */
1304 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
1305 if (iffp
->iff_type
!= 'L')
1308 if (np
->rip6_plen
< iffp
->iff_plen
)
1310 /* special rule: ::/0 means default, not "in /0" */
1311 if (iffp
->iff_plen
== 0 && np
->rip6_plen
> 0)
1314 applyplen(&ia
, iffp
->iff_plen
);
1315 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
1321 trace(2, " (filtered)\n");
1327 np
->rip6_metric
+= ifcp
->ifc_metric
;
1328 if (np
->rip6_metric
> HOPCNT_INFINITY6
)
1329 np
->rip6_metric
= HOPCNT_INFINITY6
;
1331 applyplen(&np
->rip6_dest
, np
->rip6_plen
);
1332 if ((rrt
= rtsearch(np
, NULL
)) != NULL
) {
1333 if (rrt
->rrt_t
== 0)
1334 continue; /* Intf route has priority */
1335 nq
= &rrt
->rrt_info
;
1336 if (nq
->rip6_metric
> np
->rip6_metric
) {
1337 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1338 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1339 /* Small metric from the same gateway */
1340 nq
->rip6_metric
= np
->rip6_metric
;
1342 /* Better route found */
1343 rrt
->rrt_index
= ifcp
->ifc_index
;
1344 /* Update routing table */
1345 delroute(nq
, &rrt
->rrt_gw
);
1348 addroute(rrt
, &nh
, ifcp
);
1350 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1353 } else if (nq
->rip6_metric
< np
->rip6_metric
&&
1354 rrt
->rrt_index
== ifcp
->ifc_index
&&
1355 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1356 /* Got worse route from same gw */
1357 nq
->rip6_metric
= np
->rip6_metric
;
1359 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1361 } else if (nq
->rip6_metric
== np
->rip6_metric
&&
1362 np
->rip6_metric
< HOPCNT_INFINITY6
) {
1363 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1364 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1365 /* same metric, same route from same gw */
1367 } else if (rrt
->rrt_t
< t_half_lifetime
) {
1368 /* Better route found */
1369 rrt
->rrt_index
= ifcp
->ifc_index
;
1370 /* Update routing table */
1371 delroute(nq
, &rrt
->rrt_gw
);
1374 addroute(rrt
, &nh
, ifcp
);
1375 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1380 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1381 * do not update age value. Do nothing.
1383 } else if (np
->rip6_metric
< HOPCNT_INFINITY6
) {
1384 /* Got a new valid route */
1385 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
1386 fatal("malloc: struct riprt");
1389 memset(rrt
, 0, sizeof(*rrt
));
1390 nq
= &rrt
->rrt_info
;
1392 rrt
->rrt_same
= NULL
;
1393 rrt
->rrt_index
= ifcp
->ifc_index
;
1394 rrt
->rrt_flags
= RTF_UP
|RTF_GATEWAY
;
1397 applyplen(&nq
->rip6_dest
, nq
->rip6_plen
);
1398 if (nq
->rip6_plen
== sizeof(struct in6_addr
) * 8)
1399 rrt
->rrt_flags
|= RTF_HOST
;
1401 /* Put the route to the list */
1402 rrt
->rrt_next
= riprt
;
1404 /* Update routing table */
1405 addroute(rrt
, &nh
, ifcp
);
1406 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1411 /* XXX need to care the interval between triggered updates */
1413 if (nextalarm
> time(NULL
) + RIP_TRIG_INT6_MAX
) {
1414 for (ic
= ifc
; ic
; ic
= ic
->ifc_next
) {
1415 if (ifcp
->ifc_index
== ic
->ifc_index
)
1417 if (ic
->ifc_flags
& IFF_UP
)
1418 ripsend(ic
, &ic
->ifc_ripsin
,
1422 /* Reset the flag */
1423 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
)
1424 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1429 * Send all routes request packet to the specified interface.
1435 struct netinfo6
*np
;
1438 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
1440 ripbuf
->rip6_cmd
= RIP6_REQUEST
;
1441 np
= ripbuf
->rip6_nets
;
1442 memset(np
, 0, sizeof(struct netinfo6
));
1443 np
->rip6_metric
= HOPCNT_INFINITY6
;
1444 tracet(1, "Send rtdump Request to %s (%s)\n",
1445 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1446 error
= sendpacket(&ifcp
->ifc_ripsin
, RIPSIZE(1));
1447 if (error
== EAFNOSUPPORT
) {
1448 /* Protocol not supported */
1449 tracet(1, "Could not send rtdump Request to %s (%s): "
1450 "set IFF_UP to 0\n",
1451 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1452 ifcp
->ifc_flags
&= ~IFF_UP
; /* As if down for AF_INET6 */
1454 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
1458 * Process a RIP6_REQUEST packet.
1461 riprequest(ifcp
, np
, nn
, sin6
)
1463 struct netinfo6
*np
;
1465 struct sockaddr_in6
*sin6
;
1470 if (!(nn
== 1 && IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
) &&
1471 np
->rip6_plen
== 0 && np
->rip6_metric
== HOPCNT_INFINITY6
)) {
1472 /* Specific response, don't split-horizon */
1473 trace(1, "\tRIP Request\n");
1474 for (i
= 0; i
< nn
; i
++, np
++) {
1475 rrt
= rtsearch(np
, NULL
);
1477 np
->rip6_metric
= rrt
->rrt_info
.rip6_metric
;
1479 np
->rip6_metric
= HOPCNT_INFINITY6
;
1481 (void)sendpacket(sin6
, RIPSIZE(nn
));
1484 /* Whole routing table dump */
1485 trace(1, "\tRIP Request -- whole routing table\n");
1486 ripsend(ifcp
, sin6
, RRTF_SENDANYWAY
);
1490 * Get information of each interface.
1495 struct ifaddrs
*ifap
, *ifa
;
1497 struct ipv6_mreq mreq
;
1500 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1505 if (getifaddrs(&ifap
) != 0) {
1506 fatal("getifaddrs");
1510 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1511 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1513 ifcp
= ifc_find(ifa
->ifa_name
);
1514 /* we are interested in multicast-capable interfaces */
1515 if ((ifa
->ifa_flags
& IFF_MULTICAST
) == 0)
1519 if ((ifcp
= MALLOC(struct ifc
)) == NULL
) {
1520 fatal("malloc: struct ifc");
1523 memset(ifcp
, 0, sizeof(*ifcp
));
1524 ifcp
->ifc_index
= -1;
1525 ifcp
->ifc_next
= ifc
;
1528 ifcp
->ifc_name
= allocopy(ifa
->ifa_name
);
1530 ifcp
->ifc_filter
= 0;
1531 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1532 trace(1, "newif %s <%s>\n", ifcp
->ifc_name
,
1533 ifflags(ifcp
->ifc_flags
));
1534 if (!strcmp(ifcp
->ifc_name
, LOOPBACK_IF
))
1537 /* update flag, this may be up again */
1538 if (ifcp
->ifc_flags
!= ifa
->ifa_flags
) {
1539 trace(1, "%s: <%s> -> ", ifcp
->ifc_name
,
1540 ifflags(ifcp
->ifc_flags
));
1541 trace(1, "<%s>\n", ifflags(ifa
->ifa_flags
));
1542 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1544 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1546 ifconfig1(ifa
->ifa_name
, ifa
->ifa_addr
, ifcp
, s
);
1547 if ((ifcp
->ifc_flags
& (IFF_LOOPBACK
| IFF_UP
)) == IFF_UP
1548 && 0 < ifcp
->ifc_index
&& !ifcp
->ifc_joined
) {
1549 mreq
.ipv6mr_multiaddr
= ifcp
->ifc_ripsin
.sin6_addr
;
1550 mreq
.ipv6mr_interface
= ifcp
->ifc_index
;
1551 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1552 &mreq
, sizeof(mreq
)) < 0) {
1553 fatal("IPV6_JOIN_GROUP");
1556 trace(1, "join %s %s\n", ifcp
->ifc_name
, RIP6_DEST
);
1565 ifconfig1(name
, sa
, ifcp
, s
)
1567 const struct sockaddr
*sa
;
1571 struct in6_ifreq ifr
;
1572 const struct sockaddr_in6
*sin6
;
1577 sin6
= (const struct sockaddr_in6
*)sa
;
1578 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
) && !lflag
)
1580 ifr
.ifr_addr
= *sin6
;
1581 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1582 if (ioctl(s
, SIOCGIFNETMASK_IN6
, (char *)&ifr
) < 0) {
1583 fatal("ioctl: SIOCGIFNETMASK_IN6");
1586 plen
= sin6mask2len(&ifr
.ifr_addr
);
1587 if ((ifa
= ifa_match(ifcp
, &sin6
->sin6_addr
, plen
)) != NULL
) {
1588 /* same interface found */
1589 /* need check if something changed */
1590 /* XXX not yet implemented */
1594 * New address is found
1596 if ((ifa
= MALLOC(struct ifac
)) == NULL
) {
1597 fatal("malloc: struct ifac");
1600 memset(ifa
, 0, sizeof(*ifa
));
1601 ifa
->ifa_conf
= ifcp
;
1602 ifa
->ifa_next
= ifcp
->ifc_addr
;
1603 ifcp
->ifc_addr
= ifa
;
1604 ifa
->ifa_addr
= sin6
->sin6_addr
;
1605 ifa
->ifa_plen
= plen
;
1606 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1607 ifr
.ifr_addr
= *sin6
;
1608 if (ioctl(s
, SIOCGIFDSTADDR_IN6
, (char *)&ifr
) < 0) {
1609 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1612 ifa
->ifa_raddr
= ifr
.ifr_dstaddr
.sin6_addr
;
1613 inet_ntop(AF_INET6
, (void *)&ifa
->ifa_raddr
, buf
, sizeof(buf
));
1614 trace(1, "found address %s/%d -- %s\n",
1615 inet6_n2p(&ifa
->ifa_addr
), ifa
->ifa_plen
, buf
);
1617 trace(1, "found address %s/%d\n",
1618 inet6_n2p(&ifa
->ifa_addr
), ifa
->ifa_plen
);
1620 if (ifcp
->ifc_index
< 0 && IN6_IS_ADDR_LINKLOCAL(&ifa
->ifa_addr
)) {
1621 ifcp
->ifc_mylladdr
= ifa
->ifa_addr
;
1622 ifcp
->ifc_index
= IN6_LINKLOCAL_IFINDEX(ifa
->ifa_addr
);
1623 memcpy(&ifcp
->ifc_ripsin
, &ripsin
, ripsin
.ss_len
);
1624 SET_IN6_LINKLOCAL_IFINDEX(ifcp
->ifc_ripsin
.sin6_addr
,
1626 setindex2ifc(ifcp
->ifc_index
, ifcp
);
1627 ifcp
->ifc_mtu
= getifmtu(ifcp
->ifc_index
);
1628 if (ifcp
->ifc_mtu
> RIP6_MAXMTU
)
1629 ifcp
->ifc_mtu
= RIP6_MAXMTU
;
1630 if (ioctl(s
, SIOCGIFMETRIC
, (char *)&ifr
) < 0) {
1631 fatal("ioctl: SIOCGIFMETRIC");
1634 ifcp
->ifc_metric
= ifr
.ifr_metric
;
1635 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1636 ifcp
->ifc_index
, ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
1638 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1642 * Receive and process routing messages.
1643 * Update interface information as necesssary.
1650 struct rt_msghdr
*rtm
;
1651 struct ifa_msghdr
*ifam
;
1652 struct if_msghdr
*ifm
;
1654 struct ifc
*ifcp
, *ic
;
1655 int iface
= 0, rtable
= 0;
1656 struct sockaddr_in6
*rta
[RTAX_MAX
];
1657 struct sockaddr_in6 mask
;
1661 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
1662 perror("read from rtsock");
1665 if (len
< sizeof(*rtm
)) {
1666 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1667 len
, (u_long
)sizeof(*rtm
));
1671 fprintf(stderr
, "rtmsg:\n");
1672 for (i
= 0; i
< len
; i
++) {
1673 fprintf(stderr
, "%02x ", buf
[i
] & 0xff);
1674 if (i
% 16 == 15) fprintf(stderr
, "\n");
1676 fprintf(stderr
, "\n");
1679 for (p
= buf
; p
- buf
< len
; p
+= ((struct rt_msghdr
*)p
)->rtm_msglen
) {
1680 /* safety against bogus message */
1681 if (((struct rt_msghdr
*)p
)->rtm_msglen
<= 0) {
1682 trace(1, "bogus rtmsg: length=%d\n",
1683 ((struct rt_msghdr
*)p
)->rtm_msglen
);
1689 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1692 ifam
= (struct ifa_msghdr
*)p
;
1693 addrs
= ifam
->ifam_addrs
;
1694 q
= (char *)(ifam
+ 1);
1697 ifm
= (struct if_msghdr
*)p
;
1698 addrs
= ifm
->ifm_addrs
;
1699 q
= (char *)(ifm
+ 1);
1702 rtm
= (struct rt_msghdr
*)p
;
1703 addrs
= rtm
->rtm_addrs
;
1704 q
= (char *)(rtm
+ 1);
1705 if (rtm
->rtm_version
!= RTM_VERSION
) {
1706 trace(1, "unexpected rtmsg version %d "
1708 rtm
->rtm_version
, RTM_VERSION
);
1711 if (rtm
->rtm_pid
== pid
) {
1713 trace(1, "rtmsg looped back to me, ignored\n");
1719 memset(&rta
, 0, sizeof(rta
));
1720 for (i
= 0; i
< RTAX_MAX
; i
++) {
1721 if (addrs
& (1 << i
)) {
1722 rta
[i
] = (struct sockaddr_in6
*)q
;
1723 q
+= ROUNDUP(rta
[i
]->sin6_len
);
1727 trace(1, "rtsock: %s (addrs=%x)\n",
1728 rttypes((struct rt_msghdr
*)p
), addrs
);
1731 i
< ((struct rt_msghdr
*)p
)->rtm_msglen
;
1733 fprintf(stderr
, "%02x ", p
[i
] & 0xff);
1734 if (i
% 16 == 15) fprintf(stderr
, "\n");
1736 fprintf(stderr
, "\n");
1742 * We may be able to optimize by using ifm->ifm_index or
1743 * ifam->ifam_index. For simplicity we don't do that here.
1745 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1758 /* nothing to be done here */
1759 trace(1, "\tnothing to be done, ignored\n");
1764 if (rta
[RTAX_DST
] == NULL
) {
1765 trace(1, "\tno destination, ignored\n");
1768 if (rta
[RTAX_DST
]->sin6_family
!= AF_INET6
) {
1769 trace(1, "\taf mismatch, ignored\n");
1772 if (IN6_IS_ADDR_LINKLOCAL(&rta
[RTAX_DST
]->sin6_addr
)) {
1773 trace(1, "\tlinklocal destination, ignored\n");
1776 if (IN6_ARE_ADDR_EQUAL(&rta
[RTAX_DST
]->sin6_addr
, &in6addr_loopback
)) {
1777 trace(1, "\tloopback destination, ignored\n");
1778 continue; /* Loopback */
1780 if (IN6_IS_ADDR_MULTICAST(&rta
[RTAX_DST
]->sin6_addr
)) {
1781 trace(1, "\tmulticast destination, ignored\n");
1787 switch (((struct rt_msghdr
*)p
)->rtm_type
) {
1796 /* should already be handled */
1797 fatal("rtrecv: never reach here");
1800 if (!rta
[RTAX_DST
] || !rta
[RTAX_GATEWAY
]) {
1801 trace(1, "\tsome of dst/gw/netamsk are "
1802 "unavailable, ignored\n");
1805 if ((rtm
->rtm_flags
& RTF_HOST
) != 0) {
1806 mask
.sin6_len
= sizeof(mask
);
1807 memset(&mask
.sin6_addr
, 0xff,
1808 sizeof(mask
.sin6_addr
));
1809 rta
[RTAX_NETMASK
] = &mask
;
1810 } else if (!rta
[RTAX_NETMASK
]) {
1811 trace(1, "\tsome of dst/gw/netamsk are "
1812 "unavailable, ignored\n");
1815 if (rt_del(rta
[RTAX_DST
], rta
[RTAX_GATEWAY
],
1816 rta
[RTAX_NETMASK
]) == 0) {
1817 rtable
++; /*just to be sure*/
1822 trace(1, "\tnot supported yet, ignored\n");
1825 if (!rta
[RTAX_NETMASK
] || !rta
[RTAX_IFA
]) {
1826 trace(1, "\tno netmask or ifa given, ignored\n");
1829 if (ifam
->ifam_index
< nindex2ifc
)
1830 ifcp
= index2ifc
[ifam
->ifam_index
];
1834 trace(1, "\tinvalid ifam_index %d, ignored\n",
1838 if (!rt_deladdr(ifcp
, rta
[RTAX_IFA
], rta
[RTAX_NETMASK
]))
1843 trace(1, "\tnot supported yet, ignored\n");
1850 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1852 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
)
1853 if (ifcp
->ifc_cflags
& IFC_CHANGED
) {
1854 if (ifrt(ifcp
, 1)) {
1855 for (ic
= ifc
; ic
; ic
= ic
->ifc_next
) {
1856 if (ifcp
->ifc_index
== ic
->ifc_index
)
1858 if (ic
->ifc_flags
& IFF_UP
)
1859 ripsend(ic
, &ic
->ifc_ripsin
,
1862 /* Reset the flag */
1863 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
)
1864 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1866 ifcp
->ifc_cflags
&= ~IFC_CHANGED
;
1870 trace(1, "rtsock: read routing table again\n");
1876 * remove specified route from the internal routing table.
1879 rt_del(sdst
, sgw
, smask
)
1880 const struct sockaddr_in6
*sdst
;
1881 const struct sockaddr_in6
*sgw
;
1882 const struct sockaddr_in6
*smask
;
1884 const struct in6_addr
*dst
= NULL
;
1885 const struct in6_addr
*gw
= NULL
;
1887 struct netinfo6 ni6
;
1888 struct riprt
*rrt
= NULL
;
1891 if (sdst
->sin6_family
!= AF_INET6
) {
1892 trace(1, "\tother AF, ignored\n");
1895 if (IN6_IS_ADDR_LINKLOCAL(&sdst
->sin6_addr
)
1896 || IN6_ARE_ADDR_EQUAL(&sdst
->sin6_addr
, &in6addr_loopback
)
1897 || IN6_IS_ADDR_MULTICAST(&sdst
->sin6_addr
)) {
1898 trace(1, "\taddress %s not interesting, ignored\n",
1899 inet6_n2p(&sdst
->sin6_addr
));
1902 dst
= &sdst
->sin6_addr
;
1903 if (sgw
->sin6_family
== AF_INET6
) {
1905 gw
= &sgw
->sin6_addr
;
1906 prefix
= sin6mask2len(smask
);
1907 } else if (sgw
->sin6_family
== AF_LINK
) {
1909 * Interface route... a hard case. We need to get the prefix
1910 * length from the kernel, but we now are parsing rtmsg.
1911 * We'll purge matching routes from my list, then get the
1914 struct riprt
*longest
;
1915 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1918 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
1919 if (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
1921 && IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)) {
1923 || longest
->rrt_info
.rip6_plen
<
1924 rrt
->rrt_info
.rip6_plen
) {
1931 trace(1, "\tno matching interface route found\n");
1934 gw
= &in6addr_loopback
;
1935 prefix
= rrt
->rrt_info
.rip6_plen
;
1937 trace(1, "\tunsupported af: (gw=%d)\n", sgw
->sin6_family
);
1941 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst
), prefix
);
1942 trace(1, "gw %s\n", inet6_n2p(gw
));
1943 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
1944 /* age route for interface address */
1945 memset(&ni6
, 0, sizeof(ni6
));
1946 ni6
.rip6_dest
= *dst
;
1947 ni6
.rip6_plen
= prefix
;
1948 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
1949 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6
.rip6_dest
),
1951 if (!rrt
&& (rrt
= rtsearch(&ni6
, NULL
)) == NULL
) {
1952 trace(1, "\tno route found\n");
1956 if ((rrt
->rrt_flags
& RTF_STATIC
) == 0) {
1957 trace(1, "\tyou can delete static routes only\n");
1960 if (!IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, gw
)) {
1961 trace(1, "\tgw mismatch: %s <-> ",
1962 inet6_n2p(&rrt
->rrt_gw
));
1963 trace(1, "%s\n", inet6_n2p(gw
));
1965 trace(1, "\troute found, age it\n");
1966 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
1967 rrt
->rrt_t
= t_lifetime
;
1968 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
1975 * remove specified address from internal interface/routing table.
1978 rt_deladdr(ifcp
, sifa
, smask
)
1980 const struct sockaddr_in6
*sifa
;
1981 const struct sockaddr_in6
*smask
;
1983 const struct in6_addr
*addr
= NULL
;
1985 struct ifac
*ifa
= NULL
;
1986 struct netinfo6 ni6
;
1987 struct riprt
*rrt
= NULL
;
1991 if (sifa
->sin6_family
!= AF_INET6
) {
1992 trace(1, "\tother AF, ignored\n");
1995 addr
= &sifa
->sin6_addr
;
1996 prefix
= sin6mask2len(smask
);
1998 trace(1, "\tdeleting %s/%d from %s\n",
1999 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
2000 ifa
= ifa_match(ifcp
, addr
, prefix
);
2002 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2003 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
2006 if (ifa
->ifa_conf
!= ifcp
) {
2007 trace(1, "\taddress table corrupt: back pointer does not match "
2009 ifcp
->ifc_name
, ifa
->ifa_conf
->ifc_name
);
2012 /* remove ifa from interface */
2013 if (ifcp
->ifc_addr
== ifa
)
2014 ifcp
->ifc_addr
= ifa
->ifa_next
;
2017 for (p
= ifcp
->ifc_addr
; p
; p
= p
->ifa_next
) {
2018 if (p
->ifa_next
== ifa
) {
2019 p
->ifa_next
= ifa
->ifa_next
;
2024 ifa
->ifa_next
= NULL
;
2025 ifa
->ifa_conf
= NULL
;
2026 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2027 /* age route for interface address */
2028 memset(&ni6
, 0, sizeof(ni6
));
2029 ni6
.rip6_dest
= ifa
->ifa_addr
;
2030 ni6
.rip6_plen
= ifa
->ifa_plen
;
2031 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
);
2032 trace(1, "\tfind interface route %s/%d on %d\n",
2033 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
, ifcp
->ifc_index
);
2034 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
2035 struct in6_addr none
;
2036 memset(&none
, 0, sizeof(none
));
2037 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
2038 (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &none
) ||
2039 IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
))) {
2040 trace(1, "\troute found, age it\n");
2041 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
2042 rrt
->rrt_t
= t_lifetime
;
2043 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2047 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2048 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
2049 rrt
->rrt_info
.rip6_plen
,
2053 trace(1, "\tno interface route found\n");
2054 /* age route for p2p destination */
2055 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2056 memset(&ni6
, 0, sizeof(ni6
));
2057 ni6
.rip6_dest
= ifa
->ifa_raddr
;
2058 ni6
.rip6_plen
= 128;
2059 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
2060 trace(1, "\tfind p2p route %s/%d on %d\n",
2061 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
,
2063 if ((rrt
= rtsearch(&ni6
, NULL
)) != NULL
) {
2064 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
2065 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &ifa
->ifa_addr
)) {
2066 trace(1, "\troute found, age it\n");
2067 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
2068 rrt
->rrt_t
= t_lifetime
;
2069 rrt
->rrt_info
.rip6_metric
=
2074 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2075 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
2076 rrt
->rrt_info
.rip6_plen
,
2080 trace(1, "\tno p2p route found\n");
2082 return updated
? 0 : -1;
2086 * Get each interface address and put those interface routes to the route
2095 struct riprt
*rrt
= NULL
, *search_rrt
, *prev_rrt
, *loop_rrt
;
2096 struct netinfo6
*np
;
2098 int need_trigger
= 0;
2101 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
2102 return 0; /* ignore loopback */
2105 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2106 ifrt_p2p(ifcp
, again
);
2110 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2111 if (IN6_IS_ADDR_LINKLOCAL(&ifa
->ifa_addr
)) {
2113 trace(1, "route: %s on %s: "
2114 "skip linklocal interface address\n",
2115 inet6_n2p(&ifa
->ifa_addr
), ifcp
->ifc_name
);
2119 if (IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_addr
)) {
2121 trace(1, "route: %s: skip unspec interface address\n",
2126 if (IN6_IS_ADDR_LOOPBACK(&ifa
->ifa_addr
)) {
2128 trace(1, "route: %s: skip loopback address\n",
2133 if (ifcp
->ifc_flags
& IFF_UP
) {
2134 if ((rrt
= MALLOC(struct riprt
)) == NULL
)
2135 fatal("malloc: struct riprt");
2136 memset(rrt
, 0, sizeof(*rrt
));
2137 rrt
->rrt_same
= NULL
;
2138 rrt
->rrt_index
= ifcp
->ifc_index
;
2139 rrt
->rrt_t
= 0; /* don't age */
2140 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2141 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2142 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2143 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
2144 if (ifa
->ifa_plen
== 128)
2145 rrt
->rrt_flags
= RTF_HOST
;
2147 rrt
->rrt_flags
= RTF_CLONING
;
2148 rrt
->rrt_rflags
|= RRTF_CHANGED
;
2149 applyplen(&rrt
->rrt_info
.rip6_dest
, ifa
->ifa_plen
);
2150 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2151 rrt
->rrt_gw
= ifa
->ifa_addr
;
2152 np
= &rrt
->rrt_info
;
2153 search_rrt
= rtsearch(np
, &prev_rrt
);
2154 if (search_rrt
!= NULL
) {
2155 if (search_rrt
->rrt_info
.rip6_metric
<=
2156 rrt
->rrt_info
.rip6_metric
) {
2157 /* Already have better route */
2159 trace(1, "route: %s/%d: "
2160 "already registered (%s)\n",
2161 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2168 prev_rrt
->rrt_next
= rrt
->rrt_next
;
2170 riprt
= rrt
->rrt_next
;
2171 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
2173 /* Attach the route to the list */
2174 trace(1, "route: %s/%d: register route (%s)\n",
2175 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2177 rrt
->rrt_next
= riprt
;
2179 addroute(rrt
, &rrt
->rrt_gw
, ifcp
);
2182 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
2185 for (loop_rrt
= riprt
; loop_rrt
; loop_rrt
= loop_rrt
->rrt_next
) {
2186 if (loop_rrt
->rrt_index
== ifcp
->ifc_index
) {
2187 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2188 if (loop_rrt
->rrt_t
== 0 || loop_rrt
->rrt_t
> t_lifetime
) {
2189 loop_rrt
->rrt_t
= t_lifetime
;
2190 loop_rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2191 loop_rrt
->rrt_rflags
|= RRTF_CHANGED
;
2201 return need_trigger
;
2205 * there are couple of p2p interface routing models. "behavior" lets
2206 * you pick one. it looks that gated behavior fits best with BSDs,
2207 * since BSD kernels do not look at prefix length on p2p interfaces.
2210 ifrt_p2p(ifcp
, again
)
2215 struct riprt
*rrt
, *orrt
, *prevrrt
;
2216 struct netinfo6
*np
;
2217 struct in6_addr addr
, dest
;
2218 int advert
, ignore
, i
;
2219 #define P2PADVERT_NETWORK 1
2220 #define P2PADVERT_ADDR 2
2221 #define P2PADVERT_DEST 4
2222 #define P2PADVERT_MAX 4
2223 const enum { CISCO
, GATED
, ROUTE6D
} behavior
= GATED
;
2224 const char *category
= "";
2227 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
2228 addr
= ifa
->ifa_addr
;
2229 dest
= ifa
->ifa_raddr
;
2230 applyplen(&addr
, ifa
->ifa_plen
);
2231 applyplen(&dest
, ifa
->ifa_plen
);
2232 advert
= ignore
= 0;
2236 * honor addr/plen, just like normal shared medium
2237 * interface. this may cause trouble if you reuse
2238 * addr/plen on other interfaces.
2240 * advertise addr/plen.
2242 advert
|= P2PADVERT_NETWORK
;
2246 * prefixlen on p2p interface is meaningless.
2247 * advertise addr/128 and dest/128.
2249 * do not install network route to route6d routing
2250 * table (if we do, it would prevent route installation
2251 * for other p2p interface that shares addr/plen).
2253 * XXX what should we do if dest is ::? it will not
2254 * get announced anyways (see following filter),
2255 * but we need to think.
2257 advert
|= P2PADVERT_ADDR
;
2258 advert
|= P2PADVERT_DEST
;
2259 ignore
|= P2PADVERT_NETWORK
;
2263 * just for testing. actually the code is redundant
2264 * given the current p2p interface address assignment
2265 * rule for kame kernel.
2268 * A/n -> announce A/n
2269 * A B/n, A and B share prefix -> A/n (= B/n)
2270 * A B/n, do not share prefix -> A/128 and B/128
2271 * actually, A/64 and A B/128 are the only cases
2272 * permitted by the kernel:
2274 * A B/128 -> A/128 and B/128
2276 if (!IN6_IS_ADDR_UNSPECIFIED(&ifa
->ifa_raddr
)) {
2277 if (IN6_ARE_ADDR_EQUAL(&addr
, &dest
))
2278 advert
|= P2PADVERT_NETWORK
;
2280 advert
|= P2PADVERT_ADDR
;
2281 advert
|= P2PADVERT_DEST
;
2282 ignore
|= P2PADVERT_NETWORK
;
2285 advert
|= P2PADVERT_NETWORK
;
2289 for (i
= 1; i
<= P2PADVERT_MAX
; i
*= 2) {
2290 if ((ignore
& i
) != 0)
2292 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2293 fatal("malloc: struct riprt");
2296 memset(rrt
, 0, sizeof(*rrt
));
2297 rrt
->rrt_same
= NULL
;
2298 rrt
->rrt_index
= ifcp
->ifc_index
;
2299 rrt
->rrt_t
= 0; /* don't age */
2301 case P2PADVERT_NETWORK
:
2302 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2303 rrt
->rrt_info
.rip6_plen
= ifa
->ifa_plen
;
2304 applyplen(&rrt
->rrt_info
.rip6_dest
,
2306 category
= "network";
2308 case P2PADVERT_ADDR
:
2309 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_addr
;
2310 rrt
->rrt_info
.rip6_plen
= 128;
2311 rrt
->rrt_gw
= in6addr_loopback
;
2314 case P2PADVERT_DEST
:
2315 rrt
->rrt_info
.rip6_dest
= ifa
->ifa_raddr
;
2316 rrt
->rrt_info
.rip6_plen
= 128;
2317 rrt
->rrt_gw
= ifa
->ifa_addr
;
2321 if (IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_info
.rip6_dest
) ||
2322 IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_info
.rip6_dest
)) {
2324 trace(1, "route: %s: skip unspec/linklocal "
2325 "(%s on %s)\n", category
, ifcp
->ifc_name
);
2330 if ((advert
& i
) == 0) {
2331 rrt
->rrt_rflags
|= RRTF_NOADVERTISE
;
2335 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2336 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2337 np
= &rrt
->rrt_info
;
2338 orrt
= rtsearch(np
, &prevrrt
);
2340 /* Attach the route to the list */
2341 trace(1, "route: %s/%d: register route "
2343 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2344 category
, ifcp
->ifc_name
, noadv
);
2345 rrt
->rrt_next
= riprt
;
2347 } else if (rrt
->rrt_index
!= orrt
->rrt_index
||
2348 rrt
->rrt_info
.rip6_metric
!= orrt
->rrt_info
.rip6_metric
) {
2350 rrt
->rrt_next
= orrt
->rrt_next
;
2352 prevrrt
->rrt_next
= rrt
;
2357 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2358 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2359 category
, ifcp
->ifc_name
, noadv
);
2363 trace(1, "route: %s/%d: "
2364 "already registered (%s on %s%s)\n",
2365 inet6_n2p(&np
->rip6_dest
),
2366 np
->rip6_plen
, category
,
2367 ifcp
->ifc_name
, noadv
);
2373 #undef P2PADVERT_NETWORK
2374 #undef P2PADVERT_ADDR
2375 #undef P2PADVERT_DEST
2376 #undef P2PADVERT_MAX
2386 struct if_msghdr
*ifm
;
2393 mib
[4] = NET_RT_IFLIST
;
2395 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2396 fatal("sysctl estimate NET_RT_IFLIST");
2399 if ((buf
= malloc(msize
)) == NULL
) {
2403 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2404 fatal("sysctl NET_RT_IFLIST");
2407 ifm
= (struct if_msghdr
*)buf
;
2408 mtu
= ifm
->ifm_data
.ifi_mtu
;
2409 if (ifindex
!= ifm
->ifm_index
) {
2410 fatal("ifindex does not match with ifm_index");
2419 struct rt_msghdr
*rtm
;
2421 #define RTTYPE(s, f) \
2423 if (rtm->rtm_type == (f)) \
2426 RTTYPE("ADD", RTM_ADD
);
2427 RTTYPE("DELETE", RTM_DELETE
);
2428 RTTYPE("CHANGE", RTM_CHANGE
);
2429 RTTYPE("GET", RTM_GET
);
2430 RTTYPE("LOSING", RTM_LOSING
);
2431 RTTYPE("REDIRECT", RTM_REDIRECT
);
2432 RTTYPE("MISS", RTM_MISS
);
2433 RTTYPE("LOCK", RTM_LOCK
);
2434 RTTYPE("OLDADD", RTM_OLDADD
);
2435 RTTYPE("OLDDEL", RTM_OLDDEL
);
2436 RTTYPE("RESOLVE", RTM_RESOLVE
);
2437 RTTYPE("NEWADDR", RTM_NEWADDR
);
2438 RTTYPE("DELADDR", RTM_DELADDR
);
2439 RTTYPE("IFINFO", RTM_IFINFO
);
2441 RTTYPE("OLDADD", RTM_OLDADD
);
2444 RTTYPE("OLDDEL", RTM_OLDDEL
);
2447 RTTYPE("OIFINFO", RTM_OIFINFO
);
2449 #ifdef RTM_IFANNOUNCE
2450 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE
);
2453 RTTYPE("NEWMADDR", RTM_NEWMADDR
);
2456 RTTYPE("DELMADDR", RTM_DELMADDR
);
2464 struct rt_msghdr
*rtm
;
2466 static char buf
[BUFSIZ
];
2469 * letter conflict should be okay. painful when *BSD diverges...
2471 strlcpy(buf
, "", sizeof(buf
));
2472 #define RTFLAG(s, f) \
2474 if (rtm->rtm_flags & (f)) \
2475 strlcat(buf, (s), sizeof(buf)); \
2477 RTFLAG("U", RTF_UP
);
2478 RTFLAG("G", RTF_GATEWAY
);
2479 RTFLAG("H", RTF_HOST
);
2480 RTFLAG("R", RTF_REJECT
);
2481 RTFLAG("D", RTF_DYNAMIC
);
2482 RTFLAG("M", RTF_MODIFIED
);
2483 RTFLAG("d", RTF_DONE
);
2485 RTFLAG("m", RTF_MASK
);
2487 RTFLAG("C", RTF_CLONING
);
2489 RTFLAG("c", RTF_CLONED
);
2491 #ifdef RTF_PRCLONING
2492 RTFLAG("c", RTF_PRCLONING
);
2494 #ifdef RTF_WASCLONED
2495 RTFLAG("W", RTF_WASCLONED
);
2497 RTFLAG("X", RTF_XRESOLVE
);
2498 RTFLAG("L", RTF_LLINFO
);
2499 RTFLAG("S", RTF_STATIC
);
2500 RTFLAG("B", RTF_BLACKHOLE
);
2502 RTFLAG("3", RTF_PROTO3
);
2504 RTFLAG("2", RTF_PROTO2
);
2505 RTFLAG("1", RTF_PROTO1
);
2506 #ifdef RTF_BROADCAST
2507 RTFLAG("b", RTF_BROADCAST
);
2510 RTFLAG("d", RTF_DEFAULT
);
2512 #ifdef RTF_ISAROUTER
2513 RTFLAG("r", RTF_ISAROUTER
);
2516 RTFLAG("T", RTF_TUNNEL
);
2519 RTFLAG("A", RTF_AUTH
);
2522 RTFLAG("E", RTF_CRYPT
);
2532 static char buf
[BUFSIZ
];
2534 strlcpy(buf
, "", sizeof(buf
));
2535 #define IFFLAG(s, f) \
2537 if (flags & (f)) { \
2539 strlcat(buf, ",", sizeof(buf)); \
2540 strlcat(buf, (s), sizeof(buf)); \
2543 IFFLAG("UP", IFF_UP
);
2544 IFFLAG("BROADCAST", IFF_BROADCAST
);
2545 IFFLAG("DEBUG", IFF_DEBUG
);
2546 IFFLAG("LOOPBACK", IFF_LOOPBACK
);
2547 IFFLAG("POINTOPOINT", IFF_POINTOPOINT
);
2548 #ifdef IFF_NOTRAILERS
2549 IFFLAG("NOTRAILERS", IFF_NOTRAILERS
);
2552 IFFLAG("SMART", IFF_SMART
);
2554 IFFLAG("RUNNING", IFF_RUNNING
);
2555 IFFLAG("NOARP", IFF_NOARP
);
2556 IFFLAG("PROMISC", IFF_PROMISC
);
2557 IFFLAG("ALLMULTI", IFF_ALLMULTI
);
2558 IFFLAG("OACTIVE", IFF_OACTIVE
);
2559 IFFLAG("SIMPLEX", IFF_SIMPLEX
);
2560 IFFLAG("LINK0", IFF_LINK0
);
2561 IFFLAG("LINK1", IFF_LINK1
);
2562 IFFLAG("LINK2", IFF_LINK2
);
2563 IFFLAG("MULTICAST", IFF_MULTICAST
);
2574 char *buf
, *p
, *lim
;
2575 struct rt_msghdr
*rtm
;
2584 mib
[3] = AF_INET6
; /* Address family */
2585 mib
[4] = NET_RT_DUMP
; /* Dump the kernel routing table */
2586 mib
[5] = 0; /* No flags */
2592 if (sysctl(mib
, 6, NULL
, &msize
, NULL
, 0) < 0) {
2593 errmsg
= "sysctl estimate";
2596 if ((buf
= malloc(msize
)) == NULL
) {
2600 if (sysctl(mib
, 6, buf
, &msize
, NULL
, 0) < 0) {
2601 errmsg
= "sysctl NET_RT_DUMP";
2604 } while (retry
< 5 && errmsg
!= NULL
);
2606 fatal("%s (with %d retries, msize=%lu)", errmsg
, retry
,
2609 } else if (1 < retry
)
2610 syslog(LOG_INFO
, "NET_RT_DUMP %d retires", retry
);
2613 for (p
= buf
; p
< lim
; p
+= rtm
->rtm_msglen
) {
2614 rtm
= (struct rt_msghdr
*)p
;
2615 rt_entry(rtm
, again
);
2621 rt_entry(rtm
, again
)
2622 struct rt_msghdr
*rtm
;
2625 struct sockaddr_in6
*sin6_dst
, *sin6_gw
, *sin6_mask
;
2626 struct sockaddr_in6
*sin6_genmask
, *sin6_ifp
;
2627 char *rtmp
, *ifname
= NULL
;
2628 struct riprt
*rrt
, *orrt
;
2629 struct netinfo6
*np
;
2632 sin6_dst
= sin6_gw
= sin6_mask
= sin6_genmask
= sin6_ifp
= 0;
2633 if ((rtm
->rtm_flags
& RTF_UP
) == 0 || rtm
->rtm_flags
&
2634 (RTF_CLONING
|RTF_XRESOLVE
|RTF_LLINFO
|RTF_BLACKHOLE
)) {
2635 return; /* not interested in the link route */
2637 /* do not look at cloned routes */
2638 #ifdef RTF_WASCLONED
2639 if (rtm
->rtm_flags
& RTF_WASCLONED
)
2643 if (rtm
->rtm_flags
& RTF_CLONED
)
2647 * do not look at dynamic routes.
2648 * netbsd/openbsd cloned routes have UGHD.
2650 if (rtm
->rtm_flags
& RTF_DYNAMIC
)
2652 rtmp
= (char *)(rtm
+ 1);
2654 if ((rtm
->rtm_addrs
& RTA_DST
) == 0)
2655 return; /* ignore routes without destination address */
2656 sin6_dst
= (struct sockaddr_in6
*)rtmp
;
2657 rtmp
+= ROUNDUP(sin6_dst
->sin6_len
);
2658 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2659 sin6_gw
= (struct sockaddr_in6
*)rtmp
;
2660 rtmp
+= ROUNDUP(sin6_gw
->sin6_len
);
2662 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
2663 sin6_mask
= (struct sockaddr_in6
*)rtmp
;
2664 rtmp
+= ROUNDUP(sin6_mask
->sin6_len
);
2666 if (rtm
->rtm_addrs
& RTA_GENMASK
) {
2667 sin6_genmask
= (struct sockaddr_in6
*)rtmp
;
2668 rtmp
+= ROUNDUP(sin6_genmask
->sin6_len
);
2670 if (rtm
->rtm_addrs
& RTA_IFP
) {
2671 sin6_ifp
= (struct sockaddr_in6
*)rtmp
;
2672 rtmp
+= ROUNDUP(sin6_ifp
->sin6_len
);
2676 if (sin6_dst
->sin6_family
!= AF_INET6
)
2678 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst
->sin6_addr
))
2679 return; /* Link-local */
2680 if (IN6_ARE_ADDR_EQUAL(&sin6_dst
->sin6_addr
, &in6addr_loopback
))
2681 return; /* Loopback */
2682 if (IN6_IS_ADDR_MULTICAST(&sin6_dst
->sin6_addr
))
2685 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2686 fatal("malloc: struct riprt");
2689 memset(rrt
, 0, sizeof(*rrt
));
2690 np
= &rrt
->rrt_info
;
2691 rrt
->rrt_same
= NULL
;
2692 rrt
->rrt_t
= time(NULL
);
2693 if (aflag
== 0 && (rtm
->rtm_flags
& RTF_STATIC
))
2694 rrt
->rrt_t
= 0; /* Don't age static routes */
2695 if ((rtm
->rtm_flags
& (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
)
2696 rrt
->rrt_t
= 0; /* Don't age non-gateway host routes */
2698 np
->rip6_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
2699 if (np
->rip6_metric
< 1)
2700 np
->rip6_metric
= 1;
2701 rrt
->rrt_flags
= rtm
->rtm_flags
;
2702 np
->rip6_dest
= sin6_dst
->sin6_addr
;
2705 if (rtm
->rtm_flags
& RTF_HOST
)
2706 np
->rip6_plen
= 128; /* Host route */
2708 np
->rip6_plen
= sin6mask2len(sin6_mask
);
2712 orrt
= rtsearch(np
, NULL
);
2713 if (orrt
&& orrt
->rrt_info
.rip6_metric
!= HOPCNT_INFINITY6
) {
2716 trace(1, "route: %s/%d flags %s: already registered\n",
2717 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2725 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2727 if (sin6_gw
->sin6_family
== AF_INET6
)
2728 rrt
->rrt_gw
= sin6_gw
->sin6_addr
;
2729 else if (sin6_gw
->sin6_family
== AF_LINK
) {
2730 /* XXX in case ppp link? */
2731 rrt
->rrt_gw
= in6addr_loopback
;
2733 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2735 trace(1, "route: %s/%d flags %s",
2736 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, rtflags(rtm
));
2737 trace(1, " gw %s", inet6_n2p(&rrt
->rrt_gw
));
2741 if (s
< nindex2ifc
&& index2ifc
[s
])
2742 ifname
= index2ifc
[s
]->ifc_name
;
2744 trace(1, " not configured\n");
2748 trace(1, " if %s sock %d", ifname
, s
);
2754 if (!IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_gw
) &&
2755 !IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
) &&
2756 (rrt
->rrt_flags
& RTF_LOCAL
) == 0) {
2757 trace(0, "***** Gateway %s is not a link-local address.\n",
2758 inet6_n2p(&rrt
->rrt_gw
));
2759 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2760 inet6_n2p(&rrt
->rrt_info
.rip6_dest
), ifname
);
2761 rrt
->rrt_rflags
|= RRTF_NH_NOT_LLADDR
;
2764 /* Put it to the route list */
2765 if (orrt
&& orrt
->rrt_info
.rip6_metric
== HOPCNT_INFINITY6
) {
2766 /* replace route list */
2767 rrt
->rrt_next
= orrt
->rrt_next
;
2769 trace(1, "route: %s/%d flags %s: replace new route\n",
2770 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2774 rrt
->rrt_next
= riprt
;
2780 addroute(rrt
, gw
, ifcp
)
2782 const struct in6_addr
*gw
;
2785 struct netinfo6
*np
;
2786 u_char buf
[BUFSIZ
], buf1
[BUFSIZ
], buf2
[BUFSIZ
];
2787 struct rt_msghdr
*rtm
;
2788 struct sockaddr_in6
*sin6
;
2791 np
= &rrt
->rrt_info
;
2792 inet_ntop(AF_INET6
, (const void *)gw
, (char *)buf1
, sizeof(buf1
));
2793 inet_ntop(AF_INET6
, (void *)&ifcp
->ifc_mylladdr
, (char *)buf2
, sizeof(buf2
));
2794 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2795 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2796 np
->rip6_metric
- 1, buf2
);
2798 fprintf(rtlog
, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2799 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2800 np
->rip6_metric
- 1, buf2
);
2804 memset(buf
, 0, sizeof(buf
));
2805 rtm
= (struct rt_msghdr
*)buf
;
2806 rtm
->rtm_type
= RTM_ADD
;
2807 rtm
->rtm_version
= RTM_VERSION
;
2808 rtm
->rtm_seq
= ++seq
;
2810 rtm
->rtm_flags
= rrt
->rrt_flags
;
2811 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2812 rtm
->rtm_rmx
.rmx_hopcount
= np
->rip6_metric
- 1;
2813 rtm
->rtm_inits
= RTV_HOPCOUNT
;
2814 sin6
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2816 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2817 sin6
->sin6_family
= AF_INET6
;
2818 sin6
->sin6_addr
= np
->rip6_dest
;
2819 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2821 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2822 sin6
->sin6_family
= AF_INET6
;
2823 sin6
->sin6_addr
= *gw
;
2824 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2826 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2827 sin6
->sin6_family
= AF_INET6
;
2828 sin6
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2829 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2831 len
= (char *)sin6
- (char *)buf
;
2832 rtm
->rtm_msglen
= len
;
2833 if (write(rtsock
, buf
, len
) > 0)
2836 if (errno
== EEXIST
) {
2837 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2838 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2840 fprintf(rtlog
, "ADD: Route already exists %s/%d gw %s\n",
2841 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2843 trace(0, "Can not write to rtsock (addroute): %s\n",
2846 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2854 struct netinfo6
*np
;
2855 struct in6_addr
*gw
;
2857 u_char buf
[BUFSIZ
], buf2
[BUFSIZ
];
2858 struct rt_msghdr
*rtm
;
2859 struct sockaddr_in6
*sin6
;
2862 inet_ntop(AF_INET6
, (void *)gw
, (char *)buf2
, sizeof(buf2
));
2863 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np
->rip6_dest
),
2864 np
->rip6_plen
, buf2
);
2866 fprintf(rtlog
, "%s: DEL: %s/%d gw %s\n",
2867 hms(), inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2871 memset(buf
, 0, sizeof(buf
));
2872 rtm
= (struct rt_msghdr
*)buf
;
2873 rtm
->rtm_type
= RTM_DELETE
;
2874 rtm
->rtm_version
= RTM_VERSION
;
2875 rtm
->rtm_seq
= ++seq
;
2877 rtm
->rtm_flags
= RTF_UP
| RTF_GATEWAY
;
2878 if (np
->rip6_plen
== sizeof(struct in6_addr
) * 8)
2879 rtm
->rtm_flags
|= RTF_HOST
;
2880 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2881 sin6
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2883 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2884 sin6
->sin6_family
= AF_INET6
;
2885 sin6
->sin6_addr
= np
->rip6_dest
;
2886 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2888 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2889 sin6
->sin6_family
= AF_INET6
;
2890 sin6
->sin6_addr
= *gw
;
2891 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2893 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2894 sin6
->sin6_family
= AF_INET6
;
2895 sin6
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2896 sin6
= (struct sockaddr_in6
*)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2898 len
= (char *)sin6
- (char *)buf
;
2899 rtm
->rtm_msglen
= len
;
2900 if (write(rtsock
, buf
, len
) >= 0)
2903 if (errno
== ESRCH
) {
2904 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2905 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2907 fprintf(rtlog
, "RTDEL: Route does not exist: %s/%d gw %s\n",
2908 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2910 trace(0, "Can not write to rtsock (delroute): %s\n",
2913 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2921 struct netinfo6
*np
;
2922 struct in6_addr
*gw
;
2927 struct rt_msghdr
*rtm
;
2928 struct sockaddr_in6
*sin6
;
2930 rtm
= (struct rt_msghdr
*)buf
;
2931 len
= sizeof(struct rt_msghdr
) + sizeof(struct sockaddr_in6
);
2932 memset(rtm
, 0, len
);
2933 rtm
->rtm_type
= RTM_GET
;
2934 rtm
->rtm_version
= RTM_VERSION
;
2936 rtm
->rtm_seq
= myseq
;
2937 rtm
->rtm_addrs
= RTA_DST
;
2938 rtm
->rtm_msglen
= len
;
2939 sin6
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2940 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2941 sin6
->sin6_family
= AF_INET6
;
2942 sin6
->sin6_addr
= np
->rip6_dest
;
2943 if (write(rtsock
, buf
, len
) < 0) {
2944 if (errno
== ESRCH
) /* No such route found */
2946 perror("write to rtsock");
2950 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
2951 perror("read from rtsock");
2954 rtm
= (struct rt_msghdr
*)buf
;
2955 } while (rtm
->rtm_seq
!= myseq
|| rtm
->rtm_pid
!= pid
);
2956 sin6
= (struct sockaddr_in6
*)&buf
[sizeof(struct rt_msghdr
)];
2957 if (rtm
->rtm_addrs
& RTA_DST
) {
2958 sin6
= (struct sockaddr_in6
*)
2959 ((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2961 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2962 *gw
= sin6
->sin6_addr
;
2970 const struct in6_addr
*p
;
2972 static char buf
[BUFSIZ
];
2974 return inet_ntop(AF_INET6
, (const void *)p
, buf
, sizeof(buf
));
2997 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
3000 fprintf(dump
, "%s: Interface Table Dump\n", hms());
3001 fprintf(dump
, " Number of interfaces: %d\n", nifc
);
3002 for (i
= 0; i
< 2; i
++) {
3003 fprintf(dump
, " %sadvertising interfaces:\n", i
? "non-" : "");
3004 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
3006 if ((ifcp
->ifc_flags
& IFF_UP
) == 0)
3008 if (iff_find(ifcp
, 'N') != NULL
)
3011 if (ifcp
->ifc_flags
& IFF_UP
)
3014 ifdump0(dump
, ifcp
);
3017 fprintf(dump
, "\n");
3025 const struct ifc
*ifcp
;
3033 fprintf(dump
, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3034 ifcp
->ifc_name
, ifcp
->ifc_index
, ifflags(ifcp
->ifc_flags
),
3035 inet6_n2p(&ifcp
->ifc_mylladdr
),
3036 ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
3037 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
3038 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
3039 inet_ntop(AF_INET6
, (void *)&ifa
->ifa_raddr
,
3041 fprintf(dump
, "\t%s/%d -- %s\n",
3042 inet6_n2p(&ifa
->ifa_addr
),
3043 ifa
->ifa_plen
, buf
);
3045 fprintf(dump
, "\t%s/%d\n",
3046 inet6_n2p(&ifa
->ifa_addr
),
3050 if (ifcp
->ifc_filter
) {
3051 fprintf(dump
, "\tFilter:");
3052 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
3054 switch (iffp
->iff_type
) {
3056 ft
= "Aggregate"; addr
++; break;
3058 ft
= "No-use"; break;
3060 ft
= "Advertise-only"; addr
++; break;
3062 ft
= "Default-only"; break;
3064 ft
= "Listen-only"; addr
++; break;
3066 snprintf(buf
, sizeof(buf
), "Unknown-%c", iffp
->iff_type
);
3071 fprintf(dump
, " %s", ft
);
3073 fprintf(dump
, "(%s/%d)", inet6_n2p(&iffp
->iff_addr
),
3077 fprintf(dump
, "\n");
3093 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
3097 fprintf(dump
, "\n%s: Routing Table Dump\n", hms());
3098 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
3099 if (rrt
->rrt_t
== 0)
3102 age
= t
- rrt
->rrt_t
;
3103 inet_ntop(AF_INET6
, (void *)&rrt
->rrt_info
.rip6_dest
,
3105 fprintf(dump
, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3106 buf
, rrt
->rrt_info
.rip6_plen
, rrt
->rrt_index
,
3107 index2ifc
[rrt
->rrt_index
]->ifc_name
,
3108 inet6_n2p(&rrt
->rrt_gw
),
3109 rrt
->rrt_info
.rip6_metric
, (long)age
);
3110 if (rrt
->rrt_info
.rip6_tag
) {
3111 fprintf(dump
, " tag(0x%04x)",
3112 ntohs(rrt
->rrt_info
.rip6_tag
) & 0xffff);
3114 if (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)
3115 fprintf(dump
, " NOT-LL");
3116 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
3117 fprintf(dump
, " NO-ADV");
3118 fprintf(dump
, "\n");
3120 fprintf(dump
, "\n");
3126 * Parse the -A (and -O) options and put corresponding filter object to the
3127 * specified interface structures. Each of the -A/O option has the following
3128 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3129 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3135 char *p
, *ap
, *iflp
, *ifname
, *ep
;
3136 struct iff ftmp
, *iff_obj
;
3144 for (i
= 0; i
< nfilter
; i
++) {
3148 if (filtertype
[i
] == 'N' || filtertype
[i
] == 'T') {
3152 if ((p
= strchr(ap
, ',')) != NULL
) {
3156 if ((p
= strchr(ap
, '/')) == NULL
) {
3157 fatal("no prefixlen specified for '%s'", ap
);
3161 if (inet_pton(AF_INET6
, ap
, &ftmp
.iff_addr
) != 1) {
3162 fatal("invalid prefix specified for '%s'", ap
);
3167 plen
= strtoul(p
, &ep
, 10);
3168 if (errno
|| !*p
|| *ep
|| plen
> sizeof(ftmp
.iff_addr
) * 8) {
3169 fatal("invalid prefix length specified for '%s'", ap
);
3172 ftmp
.iff_plen
= plen
;
3173 ftmp
.iff_next
= NULL
;
3174 applyplen(&ftmp
.iff_addr
, ftmp
.iff_plen
);
3176 ftmp
.iff_type
= filtertype
[i
];
3177 if (iflp
== NULL
|| *iflp
== '\0') {
3178 fatal("no interface specified for '%s'", ap
);
3181 /* parse the interface listing portion */
3184 if ((iflp
= strchr(iflp
, ',')) != NULL
)
3186 ifcp
= ifc_find(ifname
);
3188 fatal("no interface %s exists", ifname
);
3191 iff_obj
= (struct iff
*)malloc(sizeof(struct iff
));
3192 if (iff_obj
== NULL
) {
3193 fatal("malloc of iff_obj");
3196 memcpy((void *)iff_obj
, (void *)&ftmp
,
3197 sizeof(struct iff
));
3198 /* link it to the interface filter */
3199 iff_obj
->iff_next
= ifcp
->ifc_filter
;
3200 ifcp
->ifc_filter
= iff_obj
;
3204 * -A: aggregate configuration.
3206 if (filtertype
[i
] != 'A')
3208 /* put the aggregate to the kernel routing table */
3209 rrt
= (struct riprt
*)malloc(sizeof(struct riprt
));
3211 fatal("malloc: rrt");
3214 memset(rrt
, 0, sizeof(struct riprt
));
3215 rrt
->rrt_info
.rip6_dest
= ftmp
.iff_addr
;
3216 rrt
->rrt_info
.rip6_plen
= ftmp
.iff_plen
;
3217 rrt
->rrt_info
.rip6_metric
= 1;
3218 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
3219 rrt
->rrt_gw
= in6addr_loopback
;
3220 rrt
->rrt_flags
= RTF_UP
| RTF_REJECT
;
3221 rrt
->rrt_rflags
= RRTF_AGGREGATE
;
3223 rrt
->rrt_index
= loopifcp
->ifc_index
;
3225 if (getroute(&rrt
->rrt_info
, &gw
)) {
3228 * When the address has already been registered in the
3229 * kernel routing table, it should be removed
3231 delroute(&rrt
->rrt_info
, &gw
);
3233 /* it is safer behavior */
3235 fatal("%s/%u already in routing table, "
3237 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
3238 rrt
->rrt_info
.rip6_plen
);
3243 /* Put the route to the list */
3244 rrt
->rrt_next
= riprt
;
3246 trace(1, "Aggregate: %s/%d for %s\n",
3247 inet6_n2p(&ftmp
.iff_addr
), ftmp
.iff_plen
,
3249 /* Add this route to the kernel */
3250 if (nflag
) /* do not modify kernel routing table */
3252 addroute(rrt
, &in6addr_loopback
, loopifcp
);
3256 /***************** utility functions *****************/
3259 * Returns a pointer to ifac whose address and prefix length matches
3260 * with the address and prefix length specified in the arguments.
3263 ifa_match(ifcp
, ia
, plen
)
3264 const struct ifc
*ifcp
;
3265 const struct in6_addr
*ia
;
3270 for (ifa
= ifcp
->ifc_addr
; ifa
; ifa
= ifa
->ifa_next
) {
3271 if (IN6_ARE_ADDR_EQUAL(&ifa
->ifa_addr
, ia
) &&
3272 ifa
->ifa_plen
== plen
)
3279 * Return a pointer to riprt structure whose address and prefix length
3280 * matches with the address and prefix length found in the argument.
3281 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3284 rtsearch(np
, prev_rrt
)
3285 struct netinfo6
*np
;
3286 struct riprt
**prev_rrt
;
3292 for (rrt
= riprt
; rrt
; rrt
= rrt
->rrt_next
) {
3293 if (rrt
->rrt_info
.rip6_plen
== np
->rip6_plen
&&
3294 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
3307 const struct sockaddr_in6
*sin6
;
3310 return mask2len(&sin6
->sin6_addr
,
3311 sin6
->sin6_len
- offsetof(struct sockaddr_in6
, sin6_addr
));
3315 mask2len(addr
, lenlim
)
3316 const struct in6_addr
*addr
;
3320 const u_char
*p
= (const u_char
*)addr
;
3322 for (j
= 0; j
< lenlim
; j
++, p
++) {
3329 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3330 MASKLEN(0xfe, 7); break;
3331 MASKLEN(0xfc, 6); break;
3332 MASKLEN(0xf8, 5); break;
3333 MASKLEN(0xf0, 4); break;
3334 MASKLEN(0xe0, 3); break;
3335 MASKLEN(0xc0, 2); break;
3336 MASKLEN(0x80, 1); break;
3344 applymask(addr
, mask
)
3345 struct in6_addr
*addr
, *mask
;
3350 p
= (u_long
*)addr
; q
= (u_long
*)mask
;
3351 for (i
= 0; i
< 4; i
++)
3355 static const u_char plent
[8] = {
3356 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3361 struct in6_addr
*ia
;
3368 for (i
= 0; i
< 16; i
++) {
3377 static const int pl2m
[9] = {
3378 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3385 static struct in6_addr ia
;
3389 memset(&ia
, 0, sizeof(struct in6_addr
));
3391 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
3406 int len
= strlen(p
) + 1;
3407 char *q
= (char *)malloc(len
);
3421 static char buf
[BUFSIZ
];
3426 if ((tm
= localtime(&t
)) == 0) {
3430 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3435 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3443 interval
= (int)(timer
+ timer
* RIPRANDDEV
* (r
/ RAND_MAX
- 0.5));
3444 nextalarm
= time(NULL
) + interval
;
3454 t
= (int)(RIP_TRIG_INT6_MIN
+
3455 (RIP_TRIG_INT6_MAX
- RIP_TRIG_INT6_MIN
) * (r
/ RAND_MAX
));
3456 sup_trig_update
= time(NULL
) + t
;
3462 fatal(const char *fmt
, ...)
3464 fatal(fmt
, va_alist
)
3477 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
3481 syslog(LOG_ERR
, "%s: %s", buf
, strerror(errno
));
3483 syslog(LOG_ERR
, "%s", buf
);
3489 tracet(int level
, const char *fmt
, ...)
3491 tracet(level
, fmt
, va_alist
)
3499 if (level
<= dflag
) {
3505 fprintf(stderr
, "%s: ", hms());
3506 vfprintf(stderr
, fmt
, ap
);
3516 vsyslog(LOG_DEBUG
, fmt
, ap
);
3518 vsyslog(LOG_WARNING
, fmt
, ap
);
3525 trace(int level
, const char *fmt
, ...)
3527 trace(level
, fmt
, va_alist
)
3535 if (level
<= dflag
) {
3541 vfprintf(stderr
, fmt
, ap
);
3551 vsyslog(LOG_DEBUG
, fmt
, ap
);
3553 vsyslog(LOG_WARNING
, fmt
, ap
);
3561 struct if_nameindex
*p
, *p0
;
3562 unsigned int max
= 0;
3564 p0
= if_nameindex();
3565 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
3566 if (max
< p
->if_index
)
3569 if_freenameindex(p0
);
3579 for (ifcp
= ifc
; ifcp
; ifcp
= ifcp
->ifc_next
) {
3580 if (strcmp(name
, ifcp
->ifc_name
) == 0)
3583 return (struct ifc
*)NULL
;
3587 iff_find(ifcp
, type
)
3593 for (iffp
= ifcp
->ifc_filter
; iffp
; iffp
= iffp
->iff_next
) {
3594 if (iffp
->iff_type
== type
)
3601 setindex2ifc(idx
, ifcp
)
3609 nindex2ifc
= 5; /*initial guess*/
3610 index2ifc
= (struct ifc
**)
3611 malloc(sizeof(*index2ifc
) * nindex2ifc
);
3612 if (index2ifc
== NULL
) {
3616 memset(index2ifc
, 0, sizeof(*index2ifc
) * nindex2ifc
);
3619 for (nsize
= nindex2ifc
; nsize
<= idx
; nsize
*= 2)
3622 p
= (struct ifc
**)realloc(index2ifc
,
3623 sizeof(*index2ifc
) * nsize
);
3628 memset(p
+ n
, 0, sizeof(*index2ifc
) * (nindex2ifc
- n
));
3632 index2ifc
[idx
] = ifcp
;