1 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/queue.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
42 #include <arpa/inet.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <netinet/ip6.h>
48 #include <netinet/udp.h>
70 #define RT_DUMP_MAXRETRY 15
73 #define INIT_INTERVAL6 6
75 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
78 /* alignment constraint for routing socket */
80 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
81 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
83 struct ifc
{ /* Configuration of an interface */
84 TAILQ_ENTRY(ifc
) ifc_next
;
86 char ifc_name
[IFNAMSIZ
]; /* if name */
87 int ifc_index
; /* if index */
88 int ifc_mtu
; /* if mtu */
89 int ifc_metric
; /* if metric */
90 u_int ifc_flags
; /* flags */
91 short ifc_cflags
; /* IFC_XXX */
92 struct in6_addr ifc_mylladdr
; /* my link-local address */
93 struct sockaddr_in6 ifc_ripsin
; /* rip multicast address */
94 TAILQ_HEAD(, ifac
) ifc_ifac_head
; /* list of AF_INET6 addrs */
95 TAILQ_HEAD(, iff
) ifc_iff_head
; /* list of filters */
96 int ifc_joined
; /* joined to ff02::9 */
98 static TAILQ_HEAD(, ifc
) ifc_head
= TAILQ_HEAD_INITIALIZER(ifc_head
);
100 struct ifac
{ /* Address associated to an interface */
101 TAILQ_ENTRY(ifac
) ifac_next
;
103 struct ifc
*ifac_ifc
; /* back pointer */
104 struct in6_addr ifac_addr
; /* address */
105 struct in6_addr ifac_raddr
; /* remote address, valid in p2p */
106 int ifac_scope_id
; /* scope id */
107 int ifac_plen
; /* prefix length */
110 struct iff
{ /* Filters for an interface */
111 TAILQ_ENTRY(iff
) iff_next
;
114 struct in6_addr iff_addr
;
118 static struct ifc
**index2ifc
;
119 static unsigned int nindex2ifc
;
120 static struct ifc
*loopifcp
= NULL
; /* pointing to loopback */
122 static struct pollfd set
[2];
124 static fd_set
*sockvecp
; /* vector to select() for receiving */
125 static fd_set
*recvecp
;
127 static int maxfd
; /* maximum fd for select() */
129 static int rtsock
; /* the routing socket */
130 static int ripsock
; /* socket to send/receive RIP datagram */
132 static struct rip6
*ripbuf
; /* packet buffer for sending */
135 * Maintain the routes in a linked list. When the number of the routes
136 * grows, somebody would like to introduce a hash based or a radix tree
137 * based structure. I believe the number of routes handled by RIP is
138 * limited and I don't have to manage a complex data structure, however.
140 * One of the major drawbacks of the linear linked list is the difficulty
141 * of representing the relationship between a couple of routes. This may
142 * be a significant problem when we have to support route aggregation with
143 * suppressing the specifics covered by the aggregate.
147 TAILQ_ENTRY(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 */
157 static TAILQ_HEAD(, riprt
) riprt_head
= TAILQ_HEAD_INITIALIZER(riprt_head
);
159 static int dflag
= 0; /* debug flag */
160 static int qflag
= 0; /* quiet flag */
161 static int nflag
= 0; /* don't update kernel routing table */
162 static int aflag
= 0; /* age out even the statically defined routes */
163 static int hflag
= 0; /* don't split horizon */
164 static int lflag
= 0; /* exchange site local routes */
165 static int Pflag
= 0; /* don't age out routes with RTF_PROTO[123] */
166 static int Qflag
= RTF_PROTO2
; /* set RTF_PROTO[123] flag to routes by RIPng */
167 static int sflag
= 0; /* announce static routes w/ split horizon */
168 static int Sflag
= 0; /* announce static routes to every interface */
169 static unsigned long routetag
= 0; /* route tag attached on originating case */
171 static char *filter
[MAXFILTER
];
172 static int filtertype
[MAXFILTER
];
173 static int nfilter
= 0;
177 static struct sockaddr_storage ripsin
;
179 static int interval
= 1;
180 static time_t nextalarm
= 0;
182 static time_t sup_trig_update
= 0;
185 static FILE *rtlog
= NULL
;
187 static int logopened
= 0;
191 static volatile sig_atomic_t seenalrm
;
192 static volatile sig_atomic_t seenquit
;
193 static 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 static void sighandler(int);
202 static void ripalarm(void);
203 static void riprecv(void);
204 static void ripsend(struct ifc
*, struct sockaddr_in6
*, int);
205 static int out_filter(struct riprt
*, struct ifc
*);
206 static void init(void);
207 static void ifconfig(void);
208 static int ifconfig1(const char *, const struct sockaddr
*, struct ifc
*, int);
209 static void rtrecv(void);
210 static int rt_del(const struct sockaddr_in6
*, const struct sockaddr_in6
*,
211 const struct sockaddr_in6
*);
212 static int rt_deladdr(struct ifc
*, const struct sockaddr_in6
*,
213 const struct sockaddr_in6
*);
214 static void filterconfig(void);
215 static int getifmtu(int);
216 static const char *rttypes(struct rt_msghdr
*);
217 static const char *rtflags(struct rt_msghdr
*);
218 static const char *ifflags(int);
219 static int ifrt(struct ifc
*, int);
220 static void ifrt_p2p(struct ifc
*, int);
221 static void applyplen(struct in6_addr
*, int);
222 static void ifrtdump(int);
223 static void ifdump(int);
224 static void ifdump0(FILE *, const struct ifc
*);
225 static void ifremove(int);
226 static void rtdump(int);
227 static void rt_entry(struct rt_msghdr
*, int);
228 static void rtdexit(void);
229 static void riprequest(struct ifc
*, struct netinfo6
*, int,
230 struct sockaddr_in6
*);
231 static void ripflush(struct ifc
*, struct sockaddr_in6
*, int, struct netinfo6
*np
);
232 static void sendrequest(struct ifc
*);
233 static int sin6mask2len(const struct sockaddr_in6
*);
234 static int mask2len(const struct in6_addr
*, int);
235 static int sendpacket(struct sockaddr_in6
*, int);
236 static int addroute(struct riprt
*, const struct in6_addr
*, struct ifc
*);
237 static int delroute(struct netinfo6
*, struct in6_addr
*);
239 static struct in6_addr
*getroute(struct netinfo6
*, struct in6_addr
*);
241 static void krtread(int);
242 static int tobeadv(struct riprt
*, struct ifc
*);
243 static char *allocopy(char *);
244 static char *hms(void);
245 static const char *inet6_n2p(const struct in6_addr
*);
246 static struct ifac
*ifa_match(const struct ifc
*, const struct in6_addr
*, int);
247 static struct in6_addr
*plen2mask(int);
248 static struct riprt
*rtsearch(struct netinfo6
*);
249 static int ripinterval(int);
251 static time_t ripsuptrig(void);
253 static void fatal(const char *, ...)
254 __attribute__((__format__(__printf__
, 1, 2)));
255 static void trace(int, const char *, ...)
256 __attribute__((__format__(__printf__
, 2, 3)));
257 static void tracet(int, const char *, ...)
258 __attribute__((__format__(__printf__
, 2, 3)));
259 static struct ifc
*ifc_find(char *);
260 static struct iff
*iff_find(struct ifc
*, int);
261 static void setindex2ifc(int, struct ifc
*);
263 #define MALLOC(type) ((type *)malloc(sizeof(type)))
265 #define IFIL_TYPE_ANY 0x0
266 #define IFIL_TYPE_A 'A'
267 #define IFIL_TYPE_N 'N'
268 #define IFIL_TYPE_T 'T'
269 #define IFIL_TYPE_O 'O'
270 #define IFIL_TYPE_L 'L'
273 main(int argc
, char *argv
[])
279 sigset_t mask
, omask
;
280 const char *pidfile
= ROUTE6D_PID
;
285 progname
= strrchr(*argv
, '/');
292 while ((ch
= getopt(argc
, argv
, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
299 if (nfilter
>= MAXFILTER
) {
300 fatal("Exceeds MAXFILTER");
303 filtertype
[nfilter
] = ch
;
304 filter
[nfilter
++] = allocopy(optarg
);
308 routetag
= strtoul(optarg
, &ep
, 0);
309 if (!ep
|| *ep
!= '\0' || (routetag
& ~0xffff) != 0) {
310 fatal("invalid route tag");
319 proto
= strtoul(optarg
, &ep
, 0);
320 if (!ep
|| *ep
!= '\0' || 3 < proto
) {
321 fatal("invalid P flag");
335 proto
= strtoul(optarg
, &ep
, 0);
336 if (!ep
|| *ep
!= '\0' || 3 < proto
) {
337 fatal("invalid Q flag");
350 if ((rtlog
= fopen(optarg
, "w")) == NULL
) {
351 fatal("Can not write to routelog");
355 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
356 FLAG('a', aflag
, 1); break;
357 FLAG('d', dflag
, 1); break;
358 FLAG('D', dflag
, 2); break;
359 FLAG('h', hflag
, 1); break;
360 FLAG('l', lflag
, 1); break;
361 FLAG('n', nflag
, 1); break;
362 FLAG('q', qflag
, 1); break;
363 FLAG('s', sflag
, 1); break;
364 FLAG('S', Sflag
, 1); break;
367 fatal("Invalid option specified, terminating");
374 fatal("bogus extra arguments");
380 fprintf(stderr
, "No kernel update is allowed\n");
384 if (daemon(0, 0) < 0) {
390 openlog(progname
, LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
393 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
)
395 memset(ripbuf
, 0, RIP6_MAXMTU
);
396 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
397 ripbuf
->rip6_vers
= RIP6_VERSION
;
398 ripbuf
->rip6_res1
[0] = 0;
399 ripbuf
->rip6_res1
[1] = 0;
403 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
404 if (ifcp
->ifc_index
< 0) {
405 fprintf(stderr
, "No ifindex found at %s "
406 "(no link-local address?)\n", ifcp
->ifc_name
);
412 if (loopifcp
== NULL
) {
413 fatal("No loopback found");
416 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
425 if ((pidfh
= fopen(pidfile
, "w")) != NULL
) {
426 fprintf(pidfh
, "%d\n", pid
);
430 if ((ripbuf
= (struct rip6
*)malloc(RIP6_MAXMTU
)) == NULL
) {
434 memset(ripbuf
, 0, RIP6_MAXMTU
);
435 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
436 ripbuf
->rip6_vers
= RIP6_VERSION
;
437 ripbuf
->rip6_res1
[0] = 0;
438 ripbuf
->rip6_res1
[1] = 0;
440 if (signal(SIGALRM
, sighandler
) == SIG_ERR
||
441 signal(SIGQUIT
, sighandler
) == SIG_ERR
||
442 signal(SIGTERM
, sighandler
) == SIG_ERR
||
443 signal(SIGUSR1
, sighandler
) == SIG_ERR
||
444 signal(SIGHUP
, sighandler
) == SIG_ERR
||
445 signal(SIGINT
, sighandler
) == SIG_ERR
) {
450 * To avoid rip packet congestion (not on a cable but in this
451 * process), wait for a moment to send the first RIP6_RESPONSE
454 alarm(ripinterval(INIT_INTERVAL6
));
456 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
457 if (iff_find(ifcp
, IFIL_TYPE_N
) != NULL
)
459 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
463 syslog(LOG_INFO
, "**** Started ****");
465 sigaddset(&mask
, SIGALRM
);
484 switch (poll(set
, 2, INFTIM
))
486 memcpy(recvecp
, sockvecp
, fdmasks
);
487 switch (select(maxfd
+ 1, recvecp
, 0, 0, 0))
491 if (errno
!= EINTR
) {
500 if (set
[0].revents
& POLLIN
)
502 if (FD_ISSET(ripsock
, recvecp
))
505 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
507 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
510 if (set
[1].revents
& POLLIN
)
512 if (FD_ISSET(rtsock
, recvecp
))
515 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
517 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
524 sighandler(int signo
)
544 * gracefully exits after resetting sockopts.
553 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
554 if (rrt
->rrt_rflags
& RRTF_AGGREGATE
) {
555 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
560 syslog(LOG_INFO
, "**** Terminated ****");
566 * Called periodically:
567 * 1. age out the learned route. remove it if necessary.
568 * 2. submit RIP6_RESPONSE packets.
569 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
570 * to invoke this function in every 1 or 5 or 10 seconds only to age the
571 * routes more precisely.
578 struct riprt
*rrt
, *rrt_tmp
;
579 time_t t_lifetime
, t_holddown
;
581 /* age the RIP routes */
582 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
583 t_holddown
= t_lifetime
- RIP_HOLDDOWN
;
584 TAILQ_FOREACH_SAFE(rrt
, &riprt_head
, rrt_next
, rrt_tmp
) {
587 else if (rrt
->rrt_t
< t_holddown
) {
588 TAILQ_REMOVE(&riprt_head
, rrt
, rrt_next
);
589 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
591 } else if (rrt
->rrt_t
< t_lifetime
)
592 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
595 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
596 if (ifcp
->ifc_index
> 0 && (ifcp
->ifc_flags
& IFF_UP
))
597 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
599 alarm(ripinterval(SUPPLY_INTERVAL6
));
606 const int int0
= 0, int1
= 1, int255
= 255;
607 struct addrinfo hints
, *res
;
608 char port
[NI_MAXSERV
];
610 TAILQ_INIT(&ifc_head
);
611 nindex2ifc
= 0; /*initial guess*/
613 snprintf(port
, sizeof(port
), "%u", RIP6_PORT
);
615 memset(&hints
, 0, sizeof(hints
));
616 hints
.ai_family
= PF_INET6
;
617 hints
.ai_socktype
= SOCK_DGRAM
;
618 hints
.ai_protocol
= IPPROTO_UDP
;
619 hints
.ai_flags
= AI_PASSIVE
;
620 error
= getaddrinfo(NULL
, port
, &hints
, &res
);
622 fatal("%s", gai_strerror(error
));
626 fatal(":: resolved to multiple address");
630 ripsock
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
636 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
637 &int1
, sizeof(int1
)) < 0) {
638 fatal("rip IPV6_V6ONLY");
642 if (bind(ripsock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
646 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
647 &int255
, sizeof(int255
)) < 0) {
648 fatal("rip IPV6_MULTICAST_HOPS");
651 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
652 &int0
, sizeof(int0
)) < 0) {
653 fatal("rip IPV6_MULTICAST_LOOP");
657 #ifdef IPV6_RECVPKTINFO
658 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
659 &int1
, sizeof(int1
)) < 0) {
660 fatal("rip IPV6_RECVPKTINFO");
663 #else /* old adv. API */
664 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_PKTINFO
,
665 &int1
, sizeof(int1
)) < 0) {
666 fatal("rip IPV6_PKTINFO");
671 #ifdef IPV6_RECVPKTINFO
672 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
,
673 &int1
, sizeof(int1
)) < 0) {
674 fatal("rip IPV6_RECVHOPLIMIT");
677 #else /* old adv. API */
678 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
,
679 &int1
, sizeof(int1
)) < 0) {
680 fatal("rip IPV6_HOPLIMIT");
686 memset(&hints
, 0, sizeof(hints
));
687 hints
.ai_family
= PF_INET6
;
688 hints
.ai_socktype
= SOCK_DGRAM
;
689 hints
.ai_protocol
= IPPROTO_UDP
;
690 error
= getaddrinfo(RIP6_DEST
, port
, &hints
, &res
);
692 fatal("%s", gai_strerror(error
));
696 fatal("%s resolved to multiple address", RIP6_DEST
);
699 memcpy(&ripsin
, res
->ai_addr
, res
->ai_addrlen
);
704 set
[0].events
= POLLIN
;
710 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
711 fatal("route socket");
716 set
[1].events
= POLLIN
;
725 rtsock
= -1; /*just for safety */
730 fdmasks
= howmany(maxfd
+ 1, NFDBITS
) * sizeof(fd_mask
);
731 if ((sockvecp
= malloc(fdmasks
)) == NULL
) {
735 if ((recvecp
= malloc(fdmasks
)) == NULL
) {
739 memset(sockvecp
, 0, fdmasks
);
740 FD_SET(ripsock
, sockvecp
);
742 FD_SET(rtsock
, sockvecp
);
747 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
750 * ripflush flushes the rip datagram stored in the rip buffer
753 ripflush(struct ifc
*ifcp
, struct sockaddr_in6
*sin6
, int nrt
, struct netinfo6
*np
)
759 tracet(1, "Send(%s): info(%d) to %s.%d\n",
761 inet6_n2p(&sin6
->sin6_addr
), ntohs(sin6
->sin6_port
));
763 tracet(1, "Send: info(%d) to %s.%d\n",
764 nrt
, inet6_n2p(&sin6
->sin6_addr
), ntohs(sin6
->sin6_port
));
766 np
= ripbuf
->rip6_nets
;
767 for (i
= 0; i
< nrt
; i
++, np
++) {
768 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
769 if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
))
770 trace(2, " NextHop reset");
772 trace(2, " NextHop %s",
773 inet6_n2p(&np
->rip6_dest
));
776 trace(2, " %s/%d[%d]",
777 inet6_n2p(&np
->rip6_dest
),
778 np
->rip6_plen
, np
->rip6_metric
);
781 trace(2, " tag=0x%04x",
782 ntohs(np
->rip6_tag
) & 0xffff);
787 error
= sendpacket(sin6
, RIPSIZE(nrt
));
788 if (error
== EAFNOSUPPORT
) {
789 /* Protocol not supported */
791 tracet(1, "Could not send info to %s (%s): "
794 inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
795 /* As if down for AF_INET6 */
796 ifcp
->ifc_flags
&= ~IFF_UP
;
798 tracet(1, "Could not send info to %s\n",
799 inet6_n2p(&sin6
->sin6_addr
));
805 * Generate RIP6_RESPONSE packets and send them.
808 ripsend(struct ifc
*ifcp
, struct sockaddr_in6
*sin6
, int flag
)
811 struct in6_addr
*nh
; /* next hop */
821 * Request from non-link local address is not
822 * a regular route6d update.
824 maxrte
= (IFMINMTU
- sizeof(struct ip6_hdr
) -
825 sizeof(struct udphdr
) -
826 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
827 sizeof(struct netinfo6
);
830 np
= ripbuf
->rip6_nets
;
831 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
832 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
834 /* Put the route to the buffer */
838 ripflush(NULL
, sin6
, nrt
, np
);
841 np
= ripbuf
->rip6_nets
;
844 if (nrt
) /* Send last packet */
845 ripflush(NULL
, sin6
, nrt
, np
);
849 if ((flag
& RRTF_SENDANYWAY
) == 0 &&
850 (qflag
|| (ifcp
->ifc_flags
& IFF_LOOPBACK
)))
854 if (iff_find(ifcp
, IFIL_TYPE_N
) != NULL
)
857 /* -T: generate default route only */
858 if (iff_find(ifcp
, IFIL_TYPE_T
) != NULL
) {
859 struct netinfo6 rrt_info
;
860 memset(&rrt_info
, 0, sizeof(struct netinfo6
));
861 rrt_info
.rip6_dest
= in6addr_any
;
862 rrt_info
.rip6_plen
= 0;
863 rrt_info
.rip6_metric
= 1;
864 rrt_info
.rip6_metric
+= ifcp
->ifc_metric
;
865 rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
866 np
= ripbuf
->rip6_nets
;
869 ripflush(ifcp
, sin6
, nrt
, np
);
873 maxrte
= (ifcp
->ifc_mtu
- sizeof(struct ip6_hdr
) -
874 sizeof(struct udphdr
) -
875 sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
876 sizeof(struct netinfo6
);
878 nrt
= 0; np
= ripbuf
->rip6_nets
; nh
= NULL
;
879 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
880 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
883 /* Need to check filter here */
884 if (out_filter(rrt
, ifcp
) == 0)
887 /* Check split horizon and other conditions */
888 if (tobeadv(rrt
, ifcp
) == 0)
891 /* Only considers the routes with flag if specified */
892 if ((flag
& RRTF_CHANGED
) &&
893 (rrt
->rrt_rflags
& RRTF_CHANGED
) == 0)
897 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
898 !IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_gw
) &&
899 (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
) == 0) {
900 if (nh
== NULL
|| !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
)) {
901 if (nrt
== maxrte
- 2) {
902 ripflush(ifcp
, sin6
, nrt
, np
);
905 np
= ripbuf
->rip6_nets
;
908 np
->rip6_dest
= rrt
->rrt_gw
;
911 np
->rip6_metric
= NEXTHOP_METRIC
;
915 } else if (nh
&& (rrt
->rrt_index
!= ifcp
->ifc_index
||
916 !IN6_ARE_ADDR_EQUAL(nh
, &rrt
->rrt_gw
) ||
917 rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)) {
919 if (nrt
== maxrte
- 2) {
920 ripflush(ifcp
, sin6
, nrt
, np
);
923 np
= ripbuf
->rip6_nets
;
925 memset(np
, 0, sizeof(struct netinfo6
));
926 np
->rip6_metric
= NEXTHOP_METRIC
;
931 /* Put the route to the buffer */
935 ripflush(ifcp
, sin6
, nrt
, np
);
938 np
= ripbuf
->rip6_nets
;
941 if (nrt
) /* Send last packet */
942 ripflush(ifcp
, sin6
, nrt
, np
);
946 * outbound filter logic, per-route/interface.
949 out_filter(struct riprt
*rrt
, struct ifc
*ifcp
)
956 * -A: filter out less specific routes, if we have aggregated
959 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
960 if (iffp
->iff_type
!= 'A')
962 if (rrt
->rrt_info
.rip6_plen
<= iffp
->iff_plen
)
964 ia
= rrt
->rrt_info
.rip6_dest
;
965 applyplen(&ia
, iffp
->iff_plen
);
966 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
))
971 * if it is an aggregated route, advertise it only to the
972 * interfaces specified on -A.
974 if ((rrt
->rrt_rflags
& RRTF_AGGREGATE
) != 0) {
976 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
977 if (iffp
->iff_type
!= 'A')
979 if (rrt
->rrt_info
.rip6_plen
== iffp
->iff_plen
&&
980 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
991 * -O: advertise only if prefix matches the configured prefix.
993 if (iff_find(ifcp
, IFIL_TYPE_O
) != NULL
) {
995 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
996 if (iffp
->iff_type
!= 'O')
998 if (rrt
->rrt_info
.rip6_plen
< iffp
->iff_plen
)
1000 ia
= rrt
->rrt_info
.rip6_dest
;
1001 applyplen(&ia
, iffp
->iff_plen
);
1002 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
1011 /* the prefix should be advertised */
1016 * Determine if the route is to be advertised on the specified interface.
1017 * It checks options specified in the arguments and the split horizon rule.
1020 tobeadv(struct riprt
*rrt
, struct ifc
*ifcp
)
1023 /* Special care for static routes */
1024 if (rrt
->rrt_flags
& RTF_STATIC
) {
1025 /* XXX don't advertise reject/blackhole routes */
1026 if (rrt
->rrt_flags
& (RTF_REJECT
| RTF_BLACKHOLE
))
1029 if (Sflag
) /* Yes, advertise it anyway */
1031 if (sflag
&& rrt
->rrt_index
!= ifcp
->ifc_index
)
1035 /* Regular split horizon */
1036 if (hflag
== 0 && rrt
->rrt_index
== ifcp
->ifc_index
)
1042 * Send a rip packet actually.
1045 sendpacket(struct sockaddr_in6
*sin6
, int len
)
1049 struct iovec iov
[2];
1050 struct in6_pktinfo
*pi
;
1051 u_char cmsgbuf
[256];
1053 struct sockaddr_in6 sincopy
;
1055 /* do not overwrite the given sin */
1059 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
1060 IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
1061 idx
= sin6
->sin6_scope_id
;
1065 m
.msg_name
= (caddr_t
)sin6
;
1066 m
.msg_namelen
= sizeof(*sin6
);
1067 iov
[0].iov_base
= (caddr_t
)ripbuf
;
1068 iov
[0].iov_len
= len
;
1073 m
.msg_control
= NULL
;
1074 m
.msg_controllen
= 0;
1076 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
1077 cm
= (struct cmsghdr
*)(void *)cmsgbuf
;
1078 m
.msg_control
= (caddr_t
)cm
;
1079 m
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
1081 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1082 cm
->cmsg_level
= IPPROTO_IPV6
;
1083 cm
->cmsg_type
= IPV6_PKTINFO
;
1084 pi
= (struct in6_pktinfo
*)(void *)CMSG_DATA(cm
);
1085 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*::*/
1086 pi
->ipi6_ifindex
= idx
;
1089 if (sendmsg(ripsock
, &m
, 0 /*MSG_DONTROUTE*/) < 0) {
1090 trace(1, "sendmsg: %s\n", strerror(errno
));
1098 * Receive and process RIP packets. Update the routes/kernel forwarding
1099 * table if necessary.
1104 struct ifc
*ifcp
, *ic
;
1105 struct sockaddr_in6 fsock
;
1106 struct in6_addr nh
; /* next hop */
1108 struct netinfo6
*np
, *nq
;
1111 unsigned int need_trigger
, idx
;
1112 char buf
[4 * RIP6_MAXMTU
];
1116 struct iovec iov
[2];
1117 u_char cmsgbuf
[256];
1118 struct in6_pktinfo
*pi
= NULL
;
1123 time_t t_half_lifetime
;
1127 m
.msg_name
= (caddr_t
)&fsock
;
1128 m
.msg_namelen
= sizeof(fsock
);
1129 iov
[0].iov_base
= (caddr_t
)buf
;
1130 iov
[0].iov_len
= sizeof(buf
);
1133 cm
= (struct cmsghdr
*)(void *)cmsgbuf
;
1134 m
.msg_control
= (caddr_t
)cm
;
1135 m
.msg_controllen
= sizeof(cmsgbuf
);
1137 if ((len
= recvmsg(ripsock
, &m
, 0)) < 0) {
1142 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&m
);
1144 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&m
, cm
)) {
1145 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1147 switch (cm
->cmsg_type
) {
1149 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(*pi
))) {
1151 "invalid cmsg length for IPV6_PKTINFO\n");
1154 pi
= (struct in6_pktinfo
*)(void *)CMSG_DATA(cm
);
1155 idx
= pi
->ipi6_ifindex
;
1158 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int))) {
1160 "invalid cmsg length for IPV6_HOPLIMIT\n");
1163 hlimp
= (int *)(void *)CMSG_DATA(cm
);
1168 if ((size_t)len
< sizeof(struct rip6
)) {
1169 trace(1, "Packet too short\n");
1173 if (pi
== NULL
|| hlimp
== NULL
) {
1175 * This can happen when the kernel failed to allocate memory
1176 * for the ancillary data. Although we might be able to handle
1177 * some cases without this info, those are minor and not so
1178 * important, so it's better to discard the packet for safer
1181 trace(1, "IPv6 packet information cannot be retrieved\n");
1185 nh
= fsock
.sin6_addr
;
1186 nn
= (len
- sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
1187 sizeof(struct netinfo6
);
1188 rp
= (struct rip6
*)(void *)buf
;
1191 if (rp
->rip6_vers
!= RIP6_VERSION
) {
1192 trace(1, "Incorrect RIP version %d\n", rp
->rip6_vers
);
1195 if (rp
->rip6_cmd
== RIP6_REQUEST
) {
1196 if (idx
&& idx
< nindex2ifc
) {
1197 ifcp
= index2ifc
[idx
];
1198 riprequest(ifcp
, np
, nn
, &fsock
);
1200 riprequest(NULL
, np
, nn
, &fsock
);
1205 if (!IN6_IS_ADDR_LINKLOCAL(&fsock
.sin6_addr
)) {
1206 trace(1, "Response from non-ll addr: %s\n",
1207 inet6_n2p(&fsock
.sin6_addr
));
1208 return; /* Ignore packets from non-link-local addr */
1210 if (ntohs(fsock
.sin6_port
) != RIP6_PORT
) {
1211 trace(1, "Response from non-rip port from %s\n",
1212 inet6_n2p(&fsock
.sin6_addr
));
1215 if (IN6_IS_ADDR_MULTICAST(&pi
->ipi6_addr
) && *hlimp
!= 255) {
1217 "Response packet with a smaller hop limit (%d) from %s\n",
1218 *hlimp
, inet6_n2p(&fsock
.sin6_addr
));
1222 * Further validation: since this program does not send off-link
1223 * requests, an incoming response must always come from an on-link
1224 * node. Although this is normally ensured by the source address
1225 * check above, it may not 100% be safe because there are router
1226 * implementations that (invalidly) allow a packet with a link-local
1227 * source address to be forwarded to a different link.
1228 * So we also check whether the destination address is a link-local
1229 * address or the hop limit is 255. Note that RFC2080 does not require
1230 * the specific hop limit for a unicast response, so we cannot assume
1233 if (!IN6_IS_ADDR_LINKLOCAL(&pi
->ipi6_addr
) && *hlimp
!= 255) {
1235 "Response packet possibly from an off-link node: "
1236 "from %s to %s hlim=%d\n",
1237 inet6_n2p(&fsock
.sin6_addr
),
1238 inet6_n2p(&pi
->ipi6_addr
), *hlimp
);
1242 idx
= fsock
.sin6_scope_id
;
1243 ifcp
= (idx
< nindex2ifc
) ? index2ifc
[idx
] : NULL
;
1245 trace(1, "Packets to unknown interface index %d\n", idx
);
1246 return; /* Ignore it */
1248 if (IN6_ARE_ADDR_EQUAL(&ifcp
->ifc_mylladdr
, &fsock
.sin6_addr
))
1249 return; /* The packet is from me; ignore */
1250 if (rp
->rip6_cmd
!= RIP6_RESPONSE
) {
1251 trace(1, "Invalid command %d\n", rp
->rip6_cmd
);
1256 if (iff_find(ifcp
, IFIL_TYPE_N
) != NULL
)
1259 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1260 ifcp
->ifc_name
, inet6_n2p(&nh
), ntohs(fsock
.sin6_port
), nn
);
1263 t_half_lifetime
= t
- (RIP_LIFETIME
/2);
1264 for (; nn
; nn
--, np
++) {
1265 if (np
->rip6_metric
== NEXTHOP_METRIC
) {
1266 /* modify neighbor address */
1267 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1269 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1270 } else if (IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
)) {
1271 nh
= fsock
.sin6_addr
;
1272 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh
));
1274 nh
= fsock
.sin6_addr
;
1275 trace(1, "\tInvalid Nexthop: %s\n",
1276 inet6_n2p(&np
->rip6_dest
));
1280 if (IN6_IS_ADDR_MULTICAST(&np
->rip6_dest
)) {
1281 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1282 inet6_n2p(&np
->rip6_dest
),
1283 np
->rip6_plen
, np
->rip6_metric
);
1286 if (IN6_IS_ADDR_LOOPBACK(&np
->rip6_dest
)) {
1287 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1288 inet6_n2p(&np
->rip6_dest
),
1289 np
->rip6_plen
, np
->rip6_metric
);
1292 if (IN6_IS_ADDR_LINKLOCAL(&np
->rip6_dest
)) {
1293 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1294 inet6_n2p(&np
->rip6_dest
),
1295 np
->rip6_plen
, np
->rip6_metric
);
1298 /* may need to pass sitelocal prefix in some case, however*/
1299 if (IN6_IS_ADDR_SITELOCAL(&np
->rip6_dest
) && !lflag
) {
1300 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1301 inet6_n2p(&np
->rip6_dest
),
1302 np
->rip6_plen
, np
->rip6_metric
);
1305 trace(2, "\tnetinfo6: %s/%d [%d]",
1306 inet6_n2p(&np
->rip6_dest
),
1307 np
->rip6_plen
, np
->rip6_metric
);
1309 trace(2, " tag=0x%04x", ntohs(np
->rip6_tag
) & 0xffff);
1312 applyplen(&ia
, np
->rip6_plen
);
1313 if (!IN6_ARE_ADDR_EQUAL(&ia
, &np
->rip6_dest
))
1314 trace(2, " [junk outside prefix]");
1318 * -L: listen only if the prefix matches the configuration
1320 ok
= 1; /* if there's no L filter, it is ok */
1321 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
1322 if (iffp
->iff_type
!= IFIL_TYPE_L
)
1325 if (np
->rip6_plen
< iffp
->iff_plen
)
1327 /* special rule: ::/0 means default, not "in /0" */
1328 if (iffp
->iff_plen
== 0 && np
->rip6_plen
> 0)
1331 applyplen(&ia
, iffp
->iff_plen
);
1332 if (IN6_ARE_ADDR_EQUAL(&ia
, &iffp
->iff_addr
)) {
1338 trace(2, " (filtered)\n");
1344 np
->rip6_metric
+= ifcp
->ifc_metric
;
1345 if (np
->rip6_metric
> HOPCNT_INFINITY6
)
1346 np
->rip6_metric
= HOPCNT_INFINITY6
;
1348 applyplen(&np
->rip6_dest
, np
->rip6_plen
);
1349 if ((rrt
= rtsearch(np
)) != NULL
) {
1350 if (rrt
->rrt_t
== 0)
1351 continue; /* Intf route has priority */
1352 nq
= &rrt
->rrt_info
;
1353 if (nq
->rip6_metric
> np
->rip6_metric
) {
1354 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1355 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1356 /* Small metric from the same gateway */
1357 nq
->rip6_metric
= np
->rip6_metric
;
1359 /* Better route found */
1360 rrt
->rrt_index
= ifcp
->ifc_index
;
1361 /* Update routing table */
1362 delroute(nq
, &rrt
->rrt_gw
);
1365 addroute(rrt
, &nh
, ifcp
);
1367 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1370 } else if (nq
->rip6_metric
< np
->rip6_metric
&&
1371 rrt
->rrt_index
== ifcp
->ifc_index
&&
1372 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1373 /* Got worse route from same gw */
1374 nq
->rip6_metric
= np
->rip6_metric
;
1376 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1378 } else if (nq
->rip6_metric
== np
->rip6_metric
&&
1379 np
->rip6_metric
< HOPCNT_INFINITY6
) {
1380 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1381 IN6_ARE_ADDR_EQUAL(&nh
, &rrt
->rrt_gw
)) {
1382 /* same metric, same route from same gw */
1384 } else if (rrt
->rrt_t
< t_half_lifetime
) {
1385 /* Better route found */
1386 rrt
->rrt_index
= ifcp
->ifc_index
;
1387 /* Update routing table */
1388 delroute(nq
, &rrt
->rrt_gw
);
1391 addroute(rrt
, &nh
, ifcp
);
1392 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1397 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1398 * do not update age value. Do nothing.
1400 } else if (np
->rip6_metric
< HOPCNT_INFINITY6
) {
1401 /* Got a new valid route */
1402 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
1403 fatal("malloc: struct riprt");
1406 memset(rrt
, 0, sizeof(*rrt
));
1407 nq
= &rrt
->rrt_info
;
1409 rrt
->rrt_same
= NULL
;
1410 rrt
->rrt_index
= ifcp
->ifc_index
;
1411 rrt
->rrt_flags
= RTF_UP
|RTF_GATEWAY
;
1414 applyplen(&nq
->rip6_dest
, nq
->rip6_plen
);
1415 if (nq
->rip6_plen
== sizeof(struct in6_addr
) * 8)
1416 rrt
->rrt_flags
|= RTF_HOST
;
1418 /* Update routing table */
1419 addroute(rrt
, &nh
, ifcp
);
1420 rrt
->rrt_rflags
|= RRTF_CHANGED
;
1424 /* Put the route to the list */
1425 TAILQ_INSERT_HEAD(&riprt_head
, rrt
, rrt_next
);
1428 /* XXX need to care the interval between triggered updates */
1430 if (nextalarm
> time(NULL
) + RIP_TRIG_INT6_MAX
) {
1431 TAILQ_FOREACH(ic
, &ifc_head
, ifc_next
) {
1432 if (ifcp
->ifc_index
== ic
->ifc_index
)
1434 if (ic
->ifc_flags
& IFF_UP
)
1435 ripsend(ic
, &ic
->ifc_ripsin
,
1439 /* Reset the flag */
1440 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
1441 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1447 * Send all routes request packet to the specified interface.
1450 sendrequest(struct ifc
*ifcp
)
1452 struct netinfo6
*np
;
1455 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
1457 ripbuf
->rip6_cmd
= RIP6_REQUEST
;
1458 np
= ripbuf
->rip6_nets
;
1459 memset(np
, 0, sizeof(struct netinfo6
));
1460 np
->rip6_metric
= HOPCNT_INFINITY6
;
1461 tracet(1, "Send rtdump Request to %s (%s)\n",
1462 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1463 error
= sendpacket(&ifcp
->ifc_ripsin
, RIPSIZE(1));
1464 if (error
== EAFNOSUPPORT
) {
1465 /* Protocol not supported */
1466 tracet(1, "Could not send rtdump Request to %s (%s): "
1467 "set IFF_UP to 0\n",
1468 ifcp
->ifc_name
, inet6_n2p(&ifcp
->ifc_ripsin
.sin6_addr
));
1469 ifcp
->ifc_flags
&= ~IFF_UP
; /* As if down for AF_INET6 */
1471 ripbuf
->rip6_cmd
= RIP6_RESPONSE
;
1475 * Process a RIP6_REQUEST packet.
1478 riprequest(struct ifc
*ifcp
,
1479 struct netinfo6
*np
,
1481 struct sockaddr_in6
*sin6
)
1486 if (!(nn
== 1 && IN6_IS_ADDR_UNSPECIFIED(&np
->rip6_dest
) &&
1487 np
->rip6_plen
== 0 && np
->rip6_metric
== HOPCNT_INFINITY6
)) {
1488 /* Specific response, don't split-horizon */
1489 trace(1, "\tRIP Request\n");
1490 for (i
= 0; i
< nn
; i
++, np
++) {
1493 np
->rip6_metric
= rrt
->rrt_info
.rip6_metric
;
1495 np
->rip6_metric
= HOPCNT_INFINITY6
;
1497 (void)sendpacket(sin6
, RIPSIZE(nn
));
1500 /* Whole routing table dump */
1501 trace(1, "\tRIP Request -- whole routing table\n");
1502 ripsend(ifcp
, sin6
, RRTF_SENDANYWAY
);
1506 * Get information of each interface.
1511 struct ifaddrs
*ifap
, *ifa
;
1513 struct ipv6_mreq mreq
;
1516 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1521 if (getifaddrs(&ifap
) != 0) {
1522 fatal("getifaddrs");
1526 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1527 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1529 ifcp
= ifc_find(ifa
->ifa_name
);
1530 /* we are interested in multicast-capable interfaces */
1531 if ((ifa
->ifa_flags
& IFF_MULTICAST
) == 0)
1535 if ((ifcp
= MALLOC(struct ifc
)) == NULL
) {
1536 fatal("malloc: struct ifc");
1539 memset(ifcp
, 0, sizeof(*ifcp
));
1541 ifcp
->ifc_index
= -1;
1542 strlcpy(ifcp
->ifc_name
, ifa
->ifa_name
,
1543 sizeof(ifcp
->ifc_name
));
1544 TAILQ_INIT(&ifcp
->ifc_ifac_head
);
1545 TAILQ_INIT(&ifcp
->ifc_iff_head
);
1546 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1547 TAILQ_INSERT_HEAD(&ifc_head
, ifcp
, ifc_next
);
1548 trace(1, "newif %s <%s>\n", ifcp
->ifc_name
,
1549 ifflags(ifcp
->ifc_flags
));
1550 if (!strcmp(ifcp
->ifc_name
, LOOPBACK_IF
))
1553 /* update flag, this may be up again */
1554 if (ifcp
->ifc_flags
!= ifa
->ifa_flags
) {
1555 trace(1, "%s: <%s> -> ", ifcp
->ifc_name
,
1556 ifflags(ifcp
->ifc_flags
));
1557 trace(1, "<%s>\n", ifflags(ifa
->ifa_flags
));
1558 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1560 ifcp
->ifc_flags
= ifa
->ifa_flags
;
1562 if (ifconfig1(ifa
->ifa_name
, ifa
->ifa_addr
, ifcp
, s
) < 0) {
1563 /* maybe temporary failure */
1566 if ((ifcp
->ifc_flags
& (IFF_LOOPBACK
| IFF_UP
)) == IFF_UP
1567 && 0 < ifcp
->ifc_index
&& !ifcp
->ifc_joined
) {
1568 mreq
.ipv6mr_multiaddr
= ifcp
->ifc_ripsin
.sin6_addr
;
1569 mreq
.ipv6mr_interface
= ifcp
->ifc_index
;
1570 if (setsockopt(ripsock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1571 &mreq
, sizeof(mreq
)) < 0) {
1572 fatal("IPV6_JOIN_GROUP");
1575 trace(1, "join %s %s\n", ifcp
->ifc_name
, RIP6_DEST
);
1584 ifconfig1(const char *name
,
1585 const struct sockaddr
*sa
,
1589 struct in6_ifreq ifr
;
1590 const struct sockaddr_in6
*sin6
;
1595 sin6
= (const struct sockaddr_in6
*)(const void *)sa
;
1596 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
) && !lflag
)
1598 ifr
.ifr_addr
= *sin6
;
1599 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1600 if (ioctl(s
, SIOCGIFNETMASK_IN6
, (char *)&ifr
) < 0) {
1601 syslog(LOG_INFO
, "ioctl: SIOCGIFNETMASK_IN6");
1604 plen
= sin6mask2len(&ifr
.ifr_addr
);
1605 if ((ifac
= ifa_match(ifcp
, &sin6
->sin6_addr
, plen
)) != NULL
) {
1606 /* same interface found */
1607 /* need check if something changed */
1608 /* XXX not yet implemented */
1612 * New address is found
1614 if ((ifac
= MALLOC(struct ifac
)) == NULL
) {
1615 fatal("malloc: struct ifac");
1618 memset(ifac
, 0, sizeof(*ifac
));
1620 ifac
->ifac_ifc
= ifcp
;
1621 ifac
->ifac_addr
= sin6
->sin6_addr
;
1622 ifac
->ifac_plen
= plen
;
1623 ifac
->ifac_scope_id
= sin6
->sin6_scope_id
;
1624 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
1625 ifr
.ifr_addr
= *sin6
;
1626 if (ioctl(s
, SIOCGIFDSTADDR_IN6
, (char *)&ifr
) < 0) {
1627 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1630 ifac
->ifac_raddr
= ifr
.ifr_dstaddr
.sin6_addr
;
1631 inet_ntop(AF_INET6
, (void *)&ifac
->ifac_raddr
, buf
,
1633 trace(1, "found address %s/%d -- %s\n",
1634 inet6_n2p(&ifac
->ifac_addr
), ifac
->ifac_plen
, buf
);
1636 trace(1, "found address %s/%d\n",
1637 inet6_n2p(&ifac
->ifac_addr
), ifac
->ifac_plen
);
1639 if (ifcp
->ifc_index
< 0 && IN6_IS_ADDR_LINKLOCAL(&ifac
->ifac_addr
)) {
1640 ifcp
->ifc_mylladdr
= ifac
->ifac_addr
;
1641 ifcp
->ifc_index
= ifac
->ifac_scope_id
;
1642 memcpy(&ifcp
->ifc_ripsin
, &ripsin
, ripsin
.ss_len
);
1643 ifcp
->ifc_ripsin
.sin6_scope_id
= ifcp
->ifc_index
;
1644 setindex2ifc(ifcp
->ifc_index
, ifcp
);
1645 ifcp
->ifc_mtu
= getifmtu(ifcp
->ifc_index
);
1646 if (ifcp
->ifc_mtu
> RIP6_MAXMTU
)
1647 ifcp
->ifc_mtu
= RIP6_MAXMTU
;
1648 if (ioctl(s
, SIOCGIFMETRIC
, (char *)&ifr
) < 0) {
1649 fatal("ioctl: SIOCGIFMETRIC");
1652 ifcp
->ifc_metric
= ifr
.ifr_metric
;
1653 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1654 ifcp
->ifc_index
, ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
1656 ifcp
->ifc_cflags
|= IFC_CHANGED
;
1658 TAILQ_INSERT_HEAD(&ifcp
->ifc_ifac_head
, ifac
, ifac_next
);
1664 ifremove(int ifindex
)
1669 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
1670 if (ifcp
->ifc_index
== ifindex
)
1676 tracet(1, "ifremove: %s is departed.\n", ifcp
->ifc_name
);
1677 TAILQ_REMOVE(&ifc_head
, ifcp
, ifc_next
);
1679 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
1680 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
1681 rrt
->rrt_rflags
& RRTF_AGGREGATE
)
1682 delroute(&rrt
->rrt_info
, &rrt
->rrt_gw
);
1688 * Receive and process routing messages.
1689 * Update interface information as necessary.
1696 struct rt_msghdr
*rtm
;
1697 struct ifa_msghdr
*ifam
;
1698 struct if_msghdr
*ifm
;
1699 struct if_announcemsghdr
*ifan
;
1701 struct ifc
*ifcp
, *ic
;
1702 int iface
= 0, rtable
= 0;
1703 struct sockaddr_in6
*rta
[RTAX_MAX
];
1704 struct sockaddr_in6 mask
;
1708 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
1709 perror("read from rtsock");
1715 if (len
< sizeof(*rtm
)) {
1716 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1717 len
, (u_long
)sizeof(*rtm
));
1722 fprintf(stderr
, "rtmsg:\n");
1723 for (i
= 0; i
< len
; i
++) {
1724 fprintf(stderr
, "%02x ", buf
[i
] & 0xff);
1725 if (i
% 16 == 15) fprintf(stderr
, "\n");
1727 fprintf(stderr
, "\n");
1730 for (p
= buf
; p
- buf
< len
; p
+=
1731 ((struct rt_msghdr
*)(void *)p
)->rtm_msglen
) {
1732 if (((struct rt_msghdr
*)(void *)p
)->rtm_version
!= RTM_VERSION
)
1735 /* safety against bogus message */
1736 if (((struct rt_msghdr
*)(void *)p
)->rtm_msglen
<= 0) {
1737 trace(1, "bogus rtmsg: length=%d\n",
1738 ((struct rt_msghdr
*)(void *)p
)->rtm_msglen
);
1744 switch (((struct rt_msghdr
*)(void *)p
)->rtm_type
) {
1747 ifam
= (struct ifa_msghdr
*)(void *)p
;
1748 addrs
= ifam
->ifam_addrs
;
1749 q
= (char *)(ifam
+ 1);
1752 ifm
= (struct if_msghdr
*)(void *)p
;
1753 addrs
= ifm
->ifm_addrs
;
1754 q
= (char *)(ifm
+ 1);
1756 case RTM_IFANNOUNCE
:
1757 ifan
= (struct if_announcemsghdr
*)(void *)p
;
1758 switch (ifan
->ifan_what
) {
1762 case IFAN_DEPARTURE
:
1763 ifremove(ifan
->ifan_index
);
1769 rtm
= (struct rt_msghdr
*)(void *)p
;
1770 if (rtm
->rtm_version
!= RTM_VERSION
) {
1771 trace(1, "unexpected rtmsg version %d "
1773 rtm
->rtm_version
, RTM_VERSION
);
1777 * Only messages that use the struct rt_msghdr
1778 * format are allowed beyond this point.
1780 if (rtm
->rtm_type
> RTM_RESOLVE
) {
1781 trace(1, "rtmsg type %d ignored\n",
1785 addrs
= rtm
->rtm_addrs
;
1786 q
= (char *)(rtm
+ 1);
1787 if (rtm
->rtm_pid
== pid
) {
1789 trace(1, "rtmsg looped back to me, ignored\n");
1795 memset(&rta
, 0, sizeof(rta
));
1796 for (i
= 0; i
< RTAX_MAX
; i
++) {
1797 if (addrs
& (1 << i
)) {
1798 rta
[i
] = (struct sockaddr_in6
*)(void *)q
;
1799 q
+= ROUNDUP(rta
[i
]->sin6_len
);
1803 trace(1, "rtsock: %s (addrs=%x)\n",
1804 rttypes((struct rt_msghdr
*)(void *)p
), addrs
);
1807 i
< ((struct rt_msghdr
*)(void *)p
)->rtm_msglen
;
1809 fprintf(stderr
, "%02x ", p
[i
] & 0xff);
1810 if (i
% 16 == 15) fprintf(stderr
, "\n");
1812 fprintf(stderr
, "\n");
1818 * We may be able to optimize by using ifm->ifm_index or
1819 * ifam->ifam_index. For simplicity we don't do that here.
1821 switch (((struct rt_msghdr
*)(void *)p
)->rtm_type
) {
1833 /* nothing to be done here */
1834 trace(1, "\tnothing to be done, ignored\n");
1839 if (rta
[RTAX_DST
] == NULL
) {
1840 trace(1, "\tno destination, ignored\n");
1843 if (rta
[RTAX_DST
]->sin6_family
!= AF_INET6
) {
1844 trace(1, "\taf mismatch, ignored\n");
1847 if (IN6_IS_ADDR_LINKLOCAL(&rta
[RTAX_DST
]->sin6_addr
)) {
1848 trace(1, "\tlinklocal destination, ignored\n");
1851 if (IN6_ARE_ADDR_EQUAL(&rta
[RTAX_DST
]->sin6_addr
, &in6addr_loopback
)) {
1852 trace(1, "\tloopback destination, ignored\n");
1853 continue; /* Loopback */
1855 if (IN6_IS_ADDR_MULTICAST(&rta
[RTAX_DST
]->sin6_addr
)) {
1856 trace(1, "\tmulticast destination, ignored\n");
1862 switch (((struct rt_msghdr
*)(void *)p
)->rtm_type
) {
1870 /* should already be handled */
1871 fatal("rtrecv: never reach here");
1874 if (!rta
[RTAX_DST
] || !rta
[RTAX_GATEWAY
]) {
1875 trace(1, "\tsome of dst/gw/netamsk are "
1876 "unavailable, ignored\n");
1879 if ((rtm
->rtm_flags
& RTF_HOST
) != 0) {
1880 mask
.sin6_len
= sizeof(mask
);
1881 memset(&mask
.sin6_addr
, 0xff,
1882 sizeof(mask
.sin6_addr
));
1883 rta
[RTAX_NETMASK
] = &mask
;
1884 } else if (!rta
[RTAX_NETMASK
]) {
1885 trace(1, "\tsome of dst/gw/netamsk are "
1886 "unavailable, ignored\n");
1889 if (rt_del(rta
[RTAX_DST
], rta
[RTAX_GATEWAY
],
1890 rta
[RTAX_NETMASK
]) == 0) {
1891 rtable
++; /*just to be sure*/
1896 trace(1, "\tnot supported yet, ignored\n");
1899 if (!rta
[RTAX_NETMASK
] || !rta
[RTAX_IFA
]) {
1900 trace(1, "\tno netmask or ifa given, ignored\n");
1903 if (ifam
->ifam_index
< nindex2ifc
)
1904 ifcp
= index2ifc
[ifam
->ifam_index
];
1908 trace(1, "\tinvalid ifam_index %d, ignored\n",
1912 if (!rt_deladdr(ifcp
, rta
[RTAX_IFA
], rta
[RTAX_NETMASK
]))
1920 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1922 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
1923 if (ifcp
->ifc_cflags
& IFC_CHANGED
) {
1924 if (ifrt(ifcp
, 1)) {
1925 TAILQ_FOREACH(ic
, &ifc_head
, ifc_next
) {
1926 if (ifcp
->ifc_index
== ic
->ifc_index
)
1928 if (ic
->ifc_flags
& IFF_UP
)
1929 ripsend(ic
, &ic
->ifc_ripsin
,
1932 /* Reset the flag */
1933 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
1934 rrt
->rrt_rflags
&= ~RRTF_CHANGED
;
1937 ifcp
->ifc_cflags
&= ~IFC_CHANGED
;
1942 trace(1, "rtsock: read routing table again\n");
1948 * remove specified route from the internal routing table.
1951 rt_del(const struct sockaddr_in6
*sdst
,
1952 const struct sockaddr_in6
*sgw
,
1953 const struct sockaddr_in6
*smask
)
1955 const struct in6_addr
*dst
= NULL
;
1956 const struct in6_addr
*gw
= NULL
;
1958 struct netinfo6 ni6
;
1959 struct riprt
*rrt
= NULL
;
1962 if (sdst
->sin6_family
!= AF_INET6
) {
1963 trace(1, "\tother AF, ignored\n");
1966 if (IN6_IS_ADDR_LINKLOCAL(&sdst
->sin6_addr
)
1967 || IN6_ARE_ADDR_EQUAL(&sdst
->sin6_addr
, &in6addr_loopback
)
1968 || IN6_IS_ADDR_MULTICAST(&sdst
->sin6_addr
)) {
1969 trace(1, "\taddress %s not interesting, ignored\n",
1970 inet6_n2p(&sdst
->sin6_addr
));
1973 dst
= &sdst
->sin6_addr
;
1974 if (sgw
->sin6_family
== AF_INET6
) {
1976 gw
= &sgw
->sin6_addr
;
1977 prefix
= sin6mask2len(smask
);
1978 } else if (sgw
->sin6_family
== AF_LINK
) {
1980 * Interface route... a hard case. We need to get the prefix
1981 * length from the kernel, but we now are parsing rtmsg.
1982 * We'll purge matching routes from my list, then get the
1985 struct riprt
*longest
;
1986 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1989 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
1990 if (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
1992 && IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
)) {
1994 || longest
->rrt_info
.rip6_plen
<
1995 rrt
->rrt_info
.rip6_plen
) {
2002 trace(1, "\tno matching interface route found\n");
2005 gw
= &in6addr_loopback
;
2006 prefix
= rrt
->rrt_info
.rip6_plen
;
2008 trace(1, "\tunsupported af: (gw=%d)\n", sgw
->sin6_family
);
2012 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst
), prefix
);
2013 trace(1, "gw %s\n", inet6_n2p(gw
));
2014 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2015 /* age route for interface address */
2016 memset(&ni6
, 0, sizeof(ni6
));
2017 ni6
.rip6_dest
= *dst
;
2018 ni6
.rip6_plen
= prefix
;
2019 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
2020 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6
.rip6_dest
),
2022 if (!rrt
&& (rrt
= rtsearch(&ni6
)) == NULL
) {
2023 trace(1, "\tno route found\n");
2027 if ((rrt
->rrt_flags
& RTF_STATIC
) == 0) {
2028 trace(1, "\tyou can delete static routes only\n");
2031 if (!IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, gw
)) {
2032 trace(1, "\tgw mismatch: %s <-> ",
2033 inet6_n2p(&rrt
->rrt_gw
));
2034 trace(1, "%s\n", inet6_n2p(gw
));
2036 trace(1, "\troute found, age it\n");
2037 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
2038 rrt
->rrt_t
= t_lifetime
;
2039 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2046 * remove specified address from internal interface/routing table.
2049 rt_deladdr(struct ifc
*ifcp
,
2050 const struct sockaddr_in6
*sifa
,
2051 const struct sockaddr_in6
*smask
)
2053 const struct in6_addr
*addr
= NULL
;
2055 struct ifac
*ifac
= NULL
;
2056 struct netinfo6 ni6
;
2057 struct riprt
*rrt
= NULL
;
2061 if (sifa
->sin6_family
!= AF_INET6
) {
2062 trace(1, "\tother AF, ignored\n");
2065 addr
= &sifa
->sin6_addr
;
2066 prefix
= sin6mask2len(smask
);
2068 trace(1, "\tdeleting %s/%d from %s\n",
2069 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
2070 ifac
= ifa_match(ifcp
, addr
, prefix
);
2072 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2073 inet6_n2p(addr
), prefix
, ifcp
->ifc_name
);
2076 if (ifac
->ifac_ifc
!= ifcp
) {
2077 trace(1, "\taddress table corrupt: back pointer does not match "
2079 ifcp
->ifc_name
, ifac
->ifac_ifc
->ifc_name
);
2082 TAILQ_REMOVE(&ifcp
->ifc_ifac_head
, ifac
, ifac_next
);
2083 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2084 /* age route for interface address */
2085 memset(&ni6
, 0, sizeof(ni6
));
2086 ni6
.rip6_dest
= ifac
->ifac_addr
;
2087 ni6
.rip6_plen
= ifac
->ifac_plen
;
2088 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
);
2089 trace(1, "\tfind interface route %s/%d on %d\n",
2090 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
, ifcp
->ifc_index
);
2091 if ((rrt
= rtsearch(&ni6
)) != NULL
) {
2092 struct in6_addr none
;
2093 memset(&none
, 0, sizeof(none
));
2094 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
2095 (IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
, &none
) ||
2096 IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
))) {
2097 trace(1, "\troute found, age it\n");
2098 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
2099 rrt
->rrt_t
= t_lifetime
;
2100 rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2104 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2105 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
2106 rrt
->rrt_info
.rip6_plen
,
2110 trace(1, "\tno interface route found\n");
2111 /* age route for p2p destination */
2112 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2113 memset(&ni6
, 0, sizeof(ni6
));
2114 ni6
.rip6_dest
= ifac
->ifac_raddr
;
2115 ni6
.rip6_plen
= 128;
2116 applyplen(&ni6
.rip6_dest
, ni6
.rip6_plen
); /*to be sure*/
2117 trace(1, "\tfind p2p route %s/%d on %d\n",
2118 inet6_n2p(&ni6
.rip6_dest
), ni6
.rip6_plen
,
2120 if ((rrt
= rtsearch(&ni6
)) != NULL
) {
2121 if (rrt
->rrt_index
== ifcp
->ifc_index
&&
2122 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_gw
,
2123 &ifac
->ifac_addr
)) {
2124 trace(1, "\troute found, age it\n");
2125 if (rrt
->rrt_t
== 0 || rrt
->rrt_t
> t_lifetime
) {
2126 rrt
->rrt_t
= t_lifetime
;
2127 rrt
->rrt_info
.rip6_metric
=
2132 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2133 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
2134 rrt
->rrt_info
.rip6_plen
,
2138 trace(1, "\tno p2p route found\n");
2142 return ((updated
) ? 0 : -1);
2146 * Get each interface address and put those interface routes to the route
2150 ifrt(struct ifc
*ifcp
, int again
)
2153 struct riprt
*rrt
= NULL
, *search_rrt
, *loop_rrt
;
2154 struct netinfo6
*np
;
2156 int need_trigger
= 0;
2159 if (ifcp
->ifc_flags
& IFF_LOOPBACK
)
2160 return 0; /* ignore loopback */
2163 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
2164 ifrt_p2p(ifcp
, again
);
2168 TAILQ_FOREACH(ifac
, &ifcp
->ifc_ifac_head
, ifac_next
) {
2169 if (IN6_IS_ADDR_LINKLOCAL(&ifac
->ifac_addr
)) {
2171 trace(1, "route: %s on %s: "
2172 "skip linklocal interface address\n",
2173 inet6_n2p(&ifac
->ifac_addr
), ifcp
->ifc_name
);
2177 if (IN6_IS_ADDR_UNSPECIFIED(&ifac
->ifac_addr
)) {
2179 trace(1, "route: %s: skip unspec interface address\n",
2184 if (IN6_IS_ADDR_LOOPBACK(&ifac
->ifac_addr
)) {
2186 trace(1, "route: %s: skip loopback address\n",
2191 if (ifcp
->ifc_flags
& IFF_UP
) {
2192 if ((rrt
= MALLOC(struct riprt
)) == NULL
)
2193 fatal("malloc: struct riprt");
2194 memset(rrt
, 0, sizeof(*rrt
));
2195 rrt
->rrt_same
= NULL
;
2196 rrt
->rrt_index
= ifcp
->ifc_index
;
2197 rrt
->rrt_t
= 0; /* don't age */
2198 rrt
->rrt_info
.rip6_dest
= ifac
->ifac_addr
;
2199 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2200 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2201 rrt
->rrt_info
.rip6_plen
= ifac
->ifac_plen
;
2202 rrt
->rrt_flags
= RTF_HOST
;
2203 rrt
->rrt_rflags
|= RRTF_CHANGED
;
2204 applyplen(&rrt
->rrt_info
.rip6_dest
, ifac
->ifac_plen
);
2205 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2206 rrt
->rrt_gw
= ifac
->ifac_addr
;
2207 np
= &rrt
->rrt_info
;
2208 search_rrt
= rtsearch(np
);
2209 if (search_rrt
!= NULL
) {
2210 if (search_rrt
->rrt_info
.rip6_metric
<=
2211 rrt
->rrt_info
.rip6_metric
) {
2212 /* Already have better route */
2214 trace(1, "route: %s/%d: "
2215 "already registered (%s)\n",
2216 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2222 TAILQ_REMOVE(&riprt_head
, search_rrt
, rrt_next
);
2223 delroute(&search_rrt
->rrt_info
,
2224 &search_rrt
->rrt_gw
);
2227 /* Attach the route to the list */
2228 trace(1, "route: %s/%d: register route (%s)\n",
2229 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2231 TAILQ_INSERT_HEAD(&riprt_head
, rrt
, rrt_next
);
2232 addroute(rrt
, &rrt
->rrt_gw
, ifcp
);
2235 ripsend(ifcp
, &ifcp
->ifc_ripsin
, 0);
2238 TAILQ_FOREACH(loop_rrt
, &riprt_head
, rrt_next
) {
2239 if (loop_rrt
->rrt_index
== ifcp
->ifc_index
) {
2240 t_lifetime
= time(NULL
) - RIP_LIFETIME
;
2241 if (loop_rrt
->rrt_t
== 0 || loop_rrt
->rrt_t
> t_lifetime
) {
2242 loop_rrt
->rrt_t
= t_lifetime
;
2243 loop_rrt
->rrt_info
.rip6_metric
= HOPCNT_INFINITY6
;
2244 loop_rrt
->rrt_rflags
|= RRTF_CHANGED
;
2254 return need_trigger
;
2258 * there are couple of p2p interface routing models. "behavior" lets
2259 * you pick one. it looks that gated behavior fits best with BSDs,
2260 * since BSD kernels do not look at prefix length on p2p interfaces.
2263 ifrt_p2p(struct ifc
*ifcp
, int again
)
2266 struct riprt
*rrt
, *orrt
;
2267 struct netinfo6
*np
;
2268 struct in6_addr addr
, dest
;
2269 int advert
, ignore
, i
;
2270 #define P2PADVERT_NETWORK 1
2271 #define P2PADVERT_ADDR 2
2272 #define P2PADVERT_DEST 4
2273 #define P2PADVERT_MAX 4
2274 const enum { CISCO
, GATED
, ROUTE6D
} behavior
= GATED
;
2275 const char *category
= "";
2278 TAILQ_FOREACH(ifac
, &ifcp
->ifc_ifac_head
, ifac_next
) {
2279 addr
= ifac
->ifac_addr
;
2280 dest
= ifac
->ifac_raddr
;
2281 applyplen(&addr
, ifac
->ifac_plen
);
2282 applyplen(&dest
, ifac
->ifac_plen
);
2283 advert
= ignore
= 0;
2287 * honor addr/plen, just like normal shared medium
2288 * interface. this may cause trouble if you reuse
2289 * addr/plen on other interfaces.
2291 * advertise addr/plen.
2293 advert
|= P2PADVERT_NETWORK
;
2297 * prefixlen on p2p interface is meaningless.
2298 * advertise addr/128 and dest/128.
2300 * do not install network route to route6d routing
2301 * table (if we do, it would prevent route installation
2302 * for other p2p interface that shares addr/plen).
2304 * XXX what should we do if dest is ::? it will not
2305 * get announced anyways (see following filter),
2306 * but we need to think.
2308 advert
|= P2PADVERT_ADDR
;
2309 advert
|= P2PADVERT_DEST
;
2310 ignore
|= P2PADVERT_NETWORK
;
2314 * just for testing. actually the code is redundant
2315 * given the current p2p interface address assignment
2316 * rule for kame kernel.
2319 * A/n -> announce A/n
2320 * A B/n, A and B share prefix -> A/n (= B/n)
2321 * A B/n, do not share prefix -> A/128 and B/128
2322 * actually, A/64 and A B/128 are the only cases
2323 * permitted by the kernel:
2325 * A B/128 -> A/128 and B/128
2327 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac
->ifac_raddr
)) {
2328 if (IN6_ARE_ADDR_EQUAL(&addr
, &dest
))
2329 advert
|= P2PADVERT_NETWORK
;
2331 advert
|= P2PADVERT_ADDR
;
2332 advert
|= P2PADVERT_DEST
;
2333 ignore
|= P2PADVERT_NETWORK
;
2336 advert
|= P2PADVERT_NETWORK
;
2340 for (i
= 1; i
<= P2PADVERT_MAX
; i
*= 2) {
2341 if ((ignore
& i
) != 0)
2343 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2344 fatal("malloc: struct riprt");
2347 memset(rrt
, 0, sizeof(*rrt
));
2348 rrt
->rrt_same
= NULL
;
2349 rrt
->rrt_index
= ifcp
->ifc_index
;
2350 rrt
->rrt_t
= 0; /* don't age */
2352 case P2PADVERT_NETWORK
:
2353 rrt
->rrt_info
.rip6_dest
= ifac
->ifac_addr
;
2354 rrt
->rrt_info
.rip6_plen
= ifac
->ifac_plen
;
2355 applyplen(&rrt
->rrt_info
.rip6_dest
,
2357 category
= "network";
2359 case P2PADVERT_ADDR
:
2360 rrt
->rrt_info
.rip6_dest
= ifac
->ifac_addr
;
2361 rrt
->rrt_info
.rip6_plen
= 128;
2362 rrt
->rrt_gw
= in6addr_loopback
;
2365 case P2PADVERT_DEST
:
2366 rrt
->rrt_info
.rip6_dest
= ifac
->ifac_raddr
;
2367 rrt
->rrt_info
.rip6_plen
= 128;
2368 rrt
->rrt_gw
= ifac
->ifac_addr
;
2372 if (IN6_IS_ADDR_UNSPECIFIED(&rrt
->rrt_info
.rip6_dest
) ||
2373 IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_info
.rip6_dest
)) {
2375 trace(1, "route: %s: skip unspec/linklocal "
2376 "(%s on %s)\n", category
, ifcp
->ifc_name
);
2381 if ((advert
& i
) == 0) {
2382 rrt
->rrt_rflags
|= RRTF_NOADVERTISE
;
2386 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
2387 rrt
->rrt_info
.rip6_metric
= 1 + ifcp
->ifc_metric
;
2388 np
= &rrt
->rrt_info
;
2389 orrt
= rtsearch(np
);
2391 /* Attach the route to the list */
2392 trace(1, "route: %s/%d: register route "
2394 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2395 category
, ifcp
->ifc_name
, noadv
);
2396 TAILQ_INSERT_HEAD(&riprt_head
, rrt
, rrt_next
);
2397 } else if (rrt
->rrt_index
!= orrt
->rrt_index
||
2398 rrt
->rrt_info
.rip6_metric
!= orrt
->rrt_info
.rip6_metric
) {
2400 TAILQ_INSERT_BEFORE(orrt
, rrt
, rrt_next
);
2401 TAILQ_REMOVE(&riprt_head
, orrt
, rrt_next
);
2404 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2405 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2406 category
, ifcp
->ifc_name
, noadv
);
2410 trace(1, "route: %s/%d: "
2411 "already registered (%s on %s%s)\n",
2412 inet6_n2p(&np
->rip6_dest
),
2413 np
->rip6_plen
, category
,
2414 ifcp
->ifc_name
, noadv
);
2420 #undef P2PADVERT_NETWORK
2421 #undef P2PADVERT_ADDR
2422 #undef P2PADVERT_DEST
2423 #undef P2PADVERT_MAX
2427 getifmtu(int ifindex
)
2432 struct if_msghdr
*ifm
;
2439 mib
[4] = NET_RT_IFLIST
;
2441 if (sysctl(mib
, nitems(mib
), NULL
, &msize
, NULL
, 0) < 0) {
2442 fatal("sysctl estimate NET_RT_IFLIST");
2445 if ((buf
= malloc(msize
)) == NULL
) {
2449 if (sysctl(mib
, nitems(mib
), buf
, &msize
, NULL
, 0) < 0) {
2450 fatal("sysctl NET_RT_IFLIST");
2453 ifm
= (struct if_msghdr
*)(void *)buf
;
2454 mtu
= ifm
->ifm_data
.ifi_mtu
;
2455 if (ifindex
!= ifm
->ifm_index
) {
2456 fatal("ifindex does not match with ifm_index");
2464 rttypes(struct rt_msghdr
*rtm
)
2466 #define RTTYPE(s, f) \
2468 if (rtm->rtm_type == (f)) \
2471 RTTYPE("ADD", RTM_ADD
);
2472 RTTYPE("DELETE", RTM_DELETE
);
2473 RTTYPE("CHANGE", RTM_CHANGE
);
2474 RTTYPE("GET", RTM_GET
);
2475 RTTYPE("LOSING", RTM_LOSING
);
2476 RTTYPE("REDIRECT", RTM_REDIRECT
);
2477 RTTYPE("MISS", RTM_MISS
);
2478 RTTYPE("LOCK", RTM_LOCK
);
2479 RTTYPE("NEWADDR", RTM_NEWADDR
);
2480 RTTYPE("DELADDR", RTM_DELADDR
);
2481 RTTYPE("IFINFO", RTM_IFINFO
);
2483 RTTYPE("OIFINFO", RTM_OIFINFO
);
2485 #ifdef RTM_IFANNOUNCE
2486 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE
);
2489 RTTYPE("NEWMADDR", RTM_NEWMADDR
);
2492 RTTYPE("DELMADDR", RTM_DELMADDR
);
2499 rtflags(struct rt_msghdr
*rtm
)
2501 static char buf
[BUFSIZ
];
2504 * letter conflict should be okay. painful when *BSD diverges...
2506 strlcpy(buf
, "", sizeof(buf
));
2507 #define RTFLAG(s, f) \
2509 if (rtm->rtm_flags & (f)) \
2510 strlcat(buf, (s), sizeof(buf)); \
2512 RTFLAG("U", RTF_UP
);
2513 RTFLAG("G", RTF_GATEWAY
);
2514 RTFLAG("H", RTF_HOST
);
2515 RTFLAG("R", RTF_REJECT
);
2516 RTFLAG("D", RTF_DYNAMIC
);
2517 RTFLAG("M", RTF_MODIFIED
);
2518 RTFLAG("d", RTF_DONE
);
2520 RTFLAG("m", RTF_MASK
);
2523 RTFLAG("c", RTF_CLONED
);
2525 RTFLAG("X", RTF_XRESOLVE
);
2527 RTFLAG("L", RTF_LLINFO
);
2529 RTFLAG("S", RTF_STATIC
);
2530 RTFLAG("B", RTF_BLACKHOLE
);
2532 RTFLAG("3", RTF_PROTO3
);
2534 RTFLAG("2", RTF_PROTO2
);
2535 RTFLAG("1", RTF_PROTO1
);
2536 #ifdef RTF_BROADCAST
2537 RTFLAG("b", RTF_BROADCAST
);
2540 RTFLAG("d", RTF_DEFAULT
);
2542 #ifdef RTF_ISAROUTER
2543 RTFLAG("r", RTF_ISAROUTER
);
2546 RTFLAG("T", RTF_TUNNEL
);
2549 RTFLAG("A", RTF_AUTH
);
2552 RTFLAG("E", RTF_CRYPT
);
2561 static char buf
[BUFSIZ
];
2563 strlcpy(buf
, "", sizeof(buf
));
2564 #define IFFLAG(s, f) \
2566 if (flags & (f)) { \
2568 strlcat(buf, ",", sizeof(buf)); \
2569 strlcat(buf, (s), sizeof(buf)); \
2572 IFFLAG("UP", IFF_UP
);
2573 IFFLAG("BROADCAST", IFF_BROADCAST
);
2574 IFFLAG("DEBUG", IFF_DEBUG
);
2575 IFFLAG("LOOPBACK", IFF_LOOPBACK
);
2576 IFFLAG("POINTOPOINT", IFF_POINTOPOINT
);
2577 #ifdef IFF_NOTRAILERS
2578 IFFLAG("NOTRAILERS", IFF_NOTRAILERS
);
2580 IFFLAG("RUNNING", IFF_RUNNING
);
2581 IFFLAG("NOARP", IFF_NOARP
);
2582 IFFLAG("PROMISC", IFF_PROMISC
);
2583 IFFLAG("ALLMULTI", IFF_ALLMULTI
);
2584 IFFLAG("OACTIVE", IFF_OACTIVE
);
2585 IFFLAG("SIMPLEX", IFF_SIMPLEX
);
2586 IFFLAG("LINK0", IFF_LINK0
);
2587 IFFLAG("LINK1", IFF_LINK1
);
2588 IFFLAG("LINK2", IFF_LINK2
);
2589 IFFLAG("MULTICAST", IFF_MULTICAST
);
2599 char *buf
, *p
, *lim
;
2600 struct rt_msghdr
*rtm
;
2609 mib
[3] = AF_INET6
; /* Address family */
2610 mib
[4] = NET_RT_DUMP
; /* Dump the kernel routing table */
2611 mib
[5] = 0; /* No flags */
2621 if (sysctl(mib
, nitems(mib
), NULL
, &msize
, NULL
, 0) < 0) {
2622 errmsg
= "sysctl estimate";
2625 if ((buf
= malloc(msize
)) == NULL
) {
2629 if (sysctl(mib
, nitems(mib
), buf
, &msize
, NULL
, 0) < 0) {
2630 errmsg
= "sysctl NET_RT_DUMP";
2633 } while (retry
< RT_DUMP_MAXRETRY
&& errmsg
!= NULL
);
2635 fatal("%s (with %d retries, msize=%lu)", errmsg
, retry
,
2638 } else if (1 < retry
)
2639 syslog(LOG_INFO
, "NET_RT_DUMP %d retires", retry
);
2642 for (p
= buf
; p
< lim
; p
+= rtm
->rtm_msglen
) {
2643 rtm
= (struct rt_msghdr
*)(void *)p
;
2644 rt_entry(rtm
, again
);
2650 rt_entry(struct rt_msghdr
*rtm
, int again
)
2652 struct sockaddr_in6
*sin6_dst
, *sin6_gw
, *sin6_mask
;
2653 struct sockaddr_in6
*sin6_genmask
, *sin6_ifp
;
2654 char *rtmp
, *ifname
= NULL
;
2655 struct riprt
*rrt
, *orrt
;
2656 struct netinfo6
*np
;
2659 sin6_dst
= sin6_gw
= sin6_mask
= sin6_genmask
= sin6_ifp
= 0;
2660 if ((rtm
->rtm_flags
& RTF_UP
) == 0 || rtm
->rtm_flags
&
2661 (RTF_XRESOLVE
|RTF_BLACKHOLE
)) {
2662 return; /* not interested in the link route */
2664 /* do not look at cloned routes */
2665 #ifdef RTF_WASCLONED
2666 if (rtm
->rtm_flags
& RTF_WASCLONED
)
2670 if (rtm
->rtm_flags
& RTF_CLONED
)
2673 /* XXX: Ignore connected routes. */
2674 if (!(rtm
->rtm_flags
& (RTF_GATEWAY
|RTF_HOST
|RTF_STATIC
)))
2677 * do not look at dynamic routes.
2678 * netbsd/openbsd cloned routes have UGHD.
2680 if (rtm
->rtm_flags
& RTF_DYNAMIC
)
2682 rtmp
= (char *)(rtm
+ 1);
2684 if ((rtm
->rtm_addrs
& RTA_DST
) == 0)
2685 return; /* ignore routes without destination address */
2686 sin6_dst
= (struct sockaddr_in6
*)(void *)rtmp
;
2687 rtmp
+= ROUNDUP(sin6_dst
->sin6_len
);
2688 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2689 sin6_gw
= (struct sockaddr_in6
*)(void *)rtmp
;
2690 rtmp
+= ROUNDUP(sin6_gw
->sin6_len
);
2692 if (rtm
->rtm_addrs
& RTA_NETMASK
) {
2693 sin6_mask
= (struct sockaddr_in6
*)(void *)rtmp
;
2694 rtmp
+= ROUNDUP(sin6_mask
->sin6_len
);
2696 if (rtm
->rtm_addrs
& RTA_GENMASK
) {
2697 sin6_genmask
= (struct sockaddr_in6
*)(void *)rtmp
;
2698 rtmp
+= ROUNDUP(sin6_genmask
->sin6_len
);
2700 if (rtm
->rtm_addrs
& RTA_IFP
) {
2701 sin6_ifp
= (struct sockaddr_in6
*)(void *)rtmp
;
2702 rtmp
+= ROUNDUP(sin6_ifp
->sin6_len
);
2706 if (sin6_dst
->sin6_family
!= AF_INET6
)
2708 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst
->sin6_addr
))
2709 return; /* Link-local */
2710 if (IN6_ARE_ADDR_EQUAL(&sin6_dst
->sin6_addr
, &in6addr_loopback
))
2711 return; /* Loopback */
2712 if (IN6_IS_ADDR_MULTICAST(&sin6_dst
->sin6_addr
))
2715 if ((rrt
= MALLOC(struct riprt
)) == NULL
) {
2716 fatal("malloc: struct riprt");
2719 memset(rrt
, 0, sizeof(*rrt
));
2720 np
= &rrt
->rrt_info
;
2721 rrt
->rrt_same
= NULL
;
2722 rrt
->rrt_t
= time(NULL
);
2723 if (aflag
== 0 && (rtm
->rtm_flags
& RTF_STATIC
))
2724 rrt
->rrt_t
= 0; /* Don't age static routes */
2725 if (rtm
->rtm_flags
& Pflag
)
2726 rrt
->rrt_t
= 0; /* Don't age PROTO[123] routes */
2727 if ((rtm
->rtm_flags
& (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
)
2728 rrt
->rrt_t
= 0; /* Don't age non-gateway host routes */
2730 np
->rip6_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
2731 if (np
->rip6_metric
< 1)
2732 np
->rip6_metric
= 1;
2733 rrt
->rrt_flags
= rtm
->rtm_flags
;
2734 np
->rip6_dest
= sin6_dst
->sin6_addr
;
2737 if (rtm
->rtm_flags
& RTF_HOST
)
2738 np
->rip6_plen
= 128; /* Host route */
2740 np
->rip6_plen
= sin6mask2len(sin6_mask
);
2744 orrt
= rtsearch(np
);
2745 if (orrt
&& orrt
->rrt_info
.rip6_metric
!= HOPCNT_INFINITY6
) {
2748 trace(1, "route: %s/%d flags %s: already registered\n",
2749 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2757 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2759 if (sin6_gw
->sin6_family
== AF_INET6
)
2760 rrt
->rrt_gw
= sin6_gw
->sin6_addr
;
2761 else if (sin6_gw
->sin6_family
== AF_LINK
) {
2762 /* XXX in case ppp link? */
2763 rrt
->rrt_gw
= in6addr_loopback
;
2765 memset(&rrt
->rrt_gw
, 0, sizeof(struct in6_addr
));
2767 trace(1, "route: %s/%d flags %s",
2768 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, rtflags(rtm
));
2769 trace(1, " gw %s", inet6_n2p(&rrt
->rrt_gw
));
2772 ifindex
= rtm
->rtm_index
;
2773 if ((unsigned int)ifindex
< nindex2ifc
&& index2ifc
[ifindex
])
2774 ifname
= index2ifc
[ifindex
]->ifc_name
;
2776 trace(1, " not configured\n");
2780 trace(1, " if %s sock %d", ifname
, ifindex
);
2781 rrt
->rrt_index
= ifindex
;
2786 if (!IN6_IS_ADDR_LINKLOCAL(&rrt
->rrt_gw
) &&
2787 !IN6_IS_ADDR_LOOPBACK(&rrt
->rrt_gw
) &&
2788 (rrt
->rrt_flags
& RTF_LOCAL
) == 0) {
2789 trace(0, "***** Gateway %s is not a link-local address.\n",
2790 inet6_n2p(&rrt
->rrt_gw
));
2791 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2792 inet6_n2p(&rrt
->rrt_info
.rip6_dest
), ifname
);
2793 rrt
->rrt_rflags
|= RRTF_NH_NOT_LLADDR
;
2796 /* Put it to the route list */
2797 if (orrt
&& orrt
->rrt_info
.rip6_metric
== HOPCNT_INFINITY6
) {
2798 /* replace route list */
2799 TAILQ_INSERT_BEFORE(orrt
, rrt
, rrt_next
);
2800 TAILQ_REMOVE(&riprt_head
, orrt
, rrt_next
);
2802 trace(1, "route: %s/%d flags %s: replace new route\n",
2803 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
,
2807 TAILQ_INSERT_HEAD(&riprt_head
, rrt
, rrt_next
);
2811 addroute(struct riprt
*rrt
,
2812 const struct in6_addr
*gw
,
2815 struct netinfo6
*np
;
2816 u_char buf
[BUFSIZ
], buf1
[BUFSIZ
], buf2
[BUFSIZ
];
2817 struct rt_msghdr
*rtm
;
2818 struct sockaddr_in6
*sin6
;
2821 np
= &rrt
->rrt_info
;
2822 inet_ntop(AF_INET6
, (const void *)gw
, (char *)buf1
, sizeof(buf1
));
2823 inet_ntop(AF_INET6
, (void *)&ifcp
->ifc_mylladdr
, (char *)buf2
, sizeof(buf2
));
2824 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2825 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2826 np
->rip6_metric
- 1, buf2
);
2828 fprintf(rtlog
, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2829 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
,
2830 np
->rip6_metric
- 1, buf2
);
2834 memset(buf
, 0, sizeof(buf
));
2835 rtm
= (struct rt_msghdr
*)(void *)buf
;
2836 rtm
->rtm_type
= RTM_ADD
;
2837 rtm
->rtm_version
= RTM_VERSION
;
2838 rtm
->rtm_seq
= ++seq
;
2840 rtm
->rtm_flags
= rrt
->rrt_flags
;
2841 rtm
->rtm_flags
|= Qflag
;
2842 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2843 rtm
->rtm_rmx
.rmx_hopcount
= np
->rip6_metric
- 1;
2844 rtm
->rtm_inits
= RTV_HOPCOUNT
;
2845 sin6
= (struct sockaddr_in6
*)(void *)&buf
[sizeof(struct rt_msghdr
)];
2847 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2848 sin6
->sin6_family
= AF_INET6
;
2849 sin6
->sin6_addr
= np
->rip6_dest
;
2850 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2852 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2853 sin6
->sin6_family
= AF_INET6
;
2854 sin6
->sin6_addr
= *gw
;
2855 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
2856 sin6
->sin6_scope_id
= ifcp
->ifc_index
;
2857 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2859 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2860 sin6
->sin6_family
= AF_INET6
;
2861 sin6
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2862 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2864 len
= (char *)sin6
- (char *)buf
;
2865 rtm
->rtm_msglen
= len
;
2866 if (write(rtsock
, buf
, len
) > 0)
2869 if (errno
== EEXIST
) {
2870 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2871 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2873 fprintf(rtlog
, "ADD: Route already exists %s/%d gw %s\n",
2874 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf1
);
2876 trace(0, "Can not write to rtsock (addroute): %s\n",
2879 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2886 delroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2888 u_char buf
[BUFSIZ
], buf2
[BUFSIZ
];
2889 struct rt_msghdr
*rtm
;
2890 struct sockaddr_in6
*sin6
;
2893 inet_ntop(AF_INET6
, (void *)gw
, (char *)buf2
, sizeof(buf2
));
2894 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np
->rip6_dest
),
2895 np
->rip6_plen
, buf2
);
2897 fprintf(rtlog
, "%s: DEL: %s/%d gw %s\n",
2898 hms(), inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2902 memset(buf
, 0, sizeof(buf
));
2903 rtm
= (struct rt_msghdr
*)(void *)buf
;
2904 rtm
->rtm_type
= RTM_DELETE
;
2905 rtm
->rtm_version
= RTM_VERSION
;
2906 rtm
->rtm_seq
= ++seq
;
2908 rtm
->rtm_flags
= RTF_UP
| RTF_GATEWAY
;
2909 rtm
->rtm_flags
|= Qflag
;
2910 if (np
->rip6_plen
== sizeof(struct in6_addr
) * 8)
2911 rtm
->rtm_flags
|= RTF_HOST
;
2912 rtm
->rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
2913 sin6
= (struct sockaddr_in6
*)(void *)&buf
[sizeof(struct rt_msghdr
)];
2915 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2916 sin6
->sin6_family
= AF_INET6
;
2917 sin6
->sin6_addr
= np
->rip6_dest
;
2918 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2920 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2921 sin6
->sin6_family
= AF_INET6
;
2922 sin6
->sin6_addr
= *gw
;
2923 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2925 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2926 sin6
->sin6_family
= AF_INET6
;
2927 sin6
->sin6_addr
= *(plen2mask(np
->rip6_plen
));
2928 sin6
= (struct sockaddr_in6
*)(void *)((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2930 len
= (char *)sin6
- (char *)buf
;
2931 rtm
->rtm_msglen
= len
;
2932 if (write(rtsock
, buf
, len
) >= 0)
2935 if (errno
== ESRCH
) {
2936 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2937 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2939 fprintf(rtlog
, "RTDEL: Route does not exist: %s/%d gw %s\n",
2940 inet6_n2p(&np
->rip6_dest
), np
->rip6_plen
, buf2
);
2942 trace(0, "Can not write to rtsock (delroute): %s\n",
2945 fprintf(rtlog
, "\tCan not write to rtsock: %s\n",
2952 static struct in6_addr
*
2953 getroute(struct netinfo6
*np
, struct in6_addr
*gw
)
2958 struct rt_msghdr
*rtm
;
2959 struct sockaddr_in6
*sin6
;
2961 rtm
= (struct rt_msghdr
*)(void *)buf
;
2962 len
= sizeof(struct rt_msghdr
) + sizeof(struct sockaddr_in6
);
2963 memset(rtm
, 0, len
);
2964 rtm
->rtm_type
= RTM_GET
;
2965 rtm
->rtm_version
= RTM_VERSION
;
2967 rtm
->rtm_seq
= myseq
;
2968 rtm
->rtm_addrs
= RTA_DST
;
2969 rtm
->rtm_msglen
= len
;
2970 sin6
= (struct sockaddr_in6
*)(void *)&buf
[sizeof(struct rt_msghdr
)];
2971 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2972 sin6
->sin6_family
= AF_INET6
;
2973 sin6
->sin6_addr
= np
->rip6_dest
;
2974 if (write(rtsock
, buf
, len
) < 0) {
2975 if (errno
== ESRCH
) /* No such route found */
2977 perror("write to rtsock");
2981 if ((len
= read(rtsock
, buf
, sizeof(buf
))) < 0) {
2982 perror("read from rtsock");
2985 rtm
= (struct rt_msghdr
*)(void *)buf
;
2986 } while (rtm
->rtm_type
!= RTM_GET
|| rtm
->rtm_seq
!= myseq
||
2987 rtm
->rtm_pid
!= pid
);
2988 sin6
= (struct sockaddr_in6
*)(void *)&buf
[sizeof(struct rt_msghdr
)];
2989 if (rtm
->rtm_addrs
& RTA_DST
) {
2990 sin6
= (struct sockaddr_in6
*)(void *)
2991 ((char *)sin6
+ ROUNDUP(sin6
->sin6_len
));
2993 if (rtm
->rtm_addrs
& RTA_GATEWAY
) {
2994 *gw
= sin6
->sin6_addr
;
3002 inet6_n2p(const struct in6_addr
*p
)
3004 static char buf
[BUFSIZ
];
3006 return inet_ntop(AF_INET6
, (const void *)p
, buf
, sizeof(buf
));
3027 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
3030 fprintf(dump
, "%s: Interface Table Dump\n", hms());
3031 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
)
3033 fprintf(dump
, " Number of interfaces: %d\n", nifc
);
3035 fprintf(dump
, " advertising interfaces:\n");
3036 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
3037 if ((ifcp
->ifc_flags
& IFF_UP
) == 0)
3039 if (iff_find(ifcp
, IFIL_TYPE_N
) != NULL
)
3041 ifdump0(dump
, ifcp
);
3043 fprintf(dump
, "\n");
3044 fprintf(dump
, " non-advertising interfaces:\n");
3045 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
3046 if ((ifcp
->ifc_flags
& IFF_UP
) &&
3047 (iff_find(ifcp
, IFIL_TYPE_N
) == NULL
))
3049 ifdump0(dump
, ifcp
);
3051 fprintf(dump
, "\n");
3057 ifdump0(FILE *dump
, const struct ifc
*ifcp
)
3065 fprintf(dump
, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3066 ifcp
->ifc_name
, ifcp
->ifc_index
, ifflags(ifcp
->ifc_flags
),
3067 inet6_n2p(&ifcp
->ifc_mylladdr
),
3068 ifcp
->ifc_mtu
, ifcp
->ifc_metric
);
3069 TAILQ_FOREACH(ifac
, &ifcp
->ifc_ifac_head
, ifac_next
) {
3070 if (ifcp
->ifc_flags
& IFF_POINTOPOINT
) {
3071 inet_ntop(AF_INET6
, (void *)&ifac
->ifac_raddr
,
3073 fprintf(dump
, "\t%s/%d -- %s\n",
3074 inet6_n2p(&ifac
->ifac_addr
),
3075 ifac
->ifac_plen
, buf
);
3077 fprintf(dump
, "\t%s/%d\n",
3078 inet6_n2p(&ifac
->ifac_addr
),
3083 fprintf(dump
, "\tFilter:\n");
3084 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
3086 switch (iffp
->iff_type
) {
3088 ft
= "Aggregate"; addr
++; break;
3090 ft
= "No-use"; break;
3092 ft
= "Advertise-only"; addr
++; break;
3094 ft
= "Default-only"; break;
3096 ft
= "Listen-only"; addr
++; break;
3098 snprintf(buf
, sizeof(buf
), "Unknown-%c", iffp
->iff_type
);
3103 fprintf(dump
, "\t\t%s", ft
);
3105 fprintf(dump
, "(%s/%d)", inet6_n2p(&iffp
->iff_addr
),
3107 fprintf(dump
, "\n");
3109 fprintf(dump
, "\n");
3123 if ((dump
= fopen(ROUTE6D_DUMP
, "a")) == NULL
)
3127 fprintf(dump
, "\n%s: Routing Table Dump\n", hms());
3128 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
3129 if (rrt
->rrt_t
== 0)
3132 age
= t
- rrt
->rrt_t
;
3133 inet_ntop(AF_INET6
, (void *)&rrt
->rrt_info
.rip6_dest
,
3135 fprintf(dump
, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3136 buf
, rrt
->rrt_info
.rip6_plen
, rrt
->rrt_index
,
3137 index2ifc
[rrt
->rrt_index
]->ifc_name
,
3138 inet6_n2p(&rrt
->rrt_gw
),
3139 rrt
->rrt_info
.rip6_metric
, (long)age
);
3140 if (rrt
->rrt_info
.rip6_tag
) {
3141 fprintf(dump
, " tag(0x%04x)",
3142 ntohs(rrt
->rrt_info
.rip6_tag
) & 0xffff);
3144 if (rrt
->rrt_rflags
& RRTF_NH_NOT_LLADDR
)
3145 fprintf(dump
, " NOT-LL");
3146 if (rrt
->rrt_rflags
& RRTF_NOADVERTISE
)
3147 fprintf(dump
, " NO-ADV");
3148 fprintf(dump
, "\n");
3150 fprintf(dump
, "\n");
3156 * Parse the -A (and -O) options and put corresponding filter object to the
3157 * specified interface structures. Each of the -A/O option has the following
3158 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3159 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3165 char *p
, *ap
, *iflp
, *ifname
, *ep
;
3166 struct iff iff
, *iffp
;
3174 for (i
= 0; i
< nfilter
; i
++) {
3178 memset(iffp
, 0, sizeof(*iffp
));
3179 if (filtertype
[i
] == 'N' || filtertype
[i
] == 'T') {
3183 if ((p
= strchr(ap
, ',')) != NULL
) {
3187 if ((p
= strchr(ap
, '/')) == NULL
) {
3188 fatal("no prefixlen specified for '%s'", ap
);
3192 if (inet_pton(AF_INET6
, ap
, &iffp
->iff_addr
) != 1) {
3193 fatal("invalid prefix specified for '%s'", ap
);
3198 plen
= strtoul(p
, &ep
, 10);
3199 if (errno
|| !*p
|| *ep
|| plen
> sizeof(iffp
->iff_addr
) * 8) {
3200 fatal("invalid prefix length specified for '%s'", ap
);
3203 iffp
->iff_plen
= plen
;
3204 applyplen(&iffp
->iff_addr
, iffp
->iff_plen
);
3206 iffp
->iff_type
= filtertype
[i
];
3207 if (iflp
== NULL
|| *iflp
== '\0') {
3208 fatal("no interface specified for '%s'", ap
);
3211 /* parse the interface listing portion */
3214 if ((iflp
= strchr(iflp
, ',')) != NULL
)
3217 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
3218 if (fnmatch(ifname
, ifcp
->ifc_name
, 0) != 0)
3221 iffp
= malloc(sizeof(*iffp
));
3223 fatal("malloc of iff");
3226 memcpy(iffp
, &iff
, sizeof(*iffp
));
3228 syslog(LOG_INFO
, "Add filter: type %d, ifname %s.", iffp
->iff_type
, ifname
);
3230 TAILQ_INSERT_HEAD(&ifcp
->ifc_iff_head
, iffp
, iff_next
);
3235 * -A: aggregate configuration.
3237 if (filtertype
[i
] != IFIL_TYPE_A
)
3239 /* put the aggregate to the kernel routing table */
3240 rrt
= (struct riprt
*)malloc(sizeof(struct riprt
));
3242 fatal("malloc: rrt");
3245 memset(rrt
, 0, sizeof(struct riprt
));
3246 rrt
->rrt_info
.rip6_dest
= iff
.iff_addr
;
3247 rrt
->rrt_info
.rip6_plen
= iff
.iff_plen
;
3248 rrt
->rrt_info
.rip6_metric
= 1;
3249 rrt
->rrt_info
.rip6_tag
= htons(routetag
& 0xffff);
3250 rrt
->rrt_gw
= in6addr_loopback
;
3251 rrt
->rrt_flags
= RTF_UP
| RTF_REJECT
;
3252 rrt
->rrt_rflags
= RRTF_AGGREGATE
;
3254 rrt
->rrt_index
= loopifcp
->ifc_index
;
3256 if (getroute(&rrt
->rrt_info
, &gw
)) {
3259 * When the address has already been registered in the
3260 * kernel routing table, it should be removed
3262 delroute(&rrt
->rrt_info
, &gw
);
3264 /* it is safer behavior */
3266 fatal("%s/%u already in routing table, "
3268 inet6_n2p(&rrt
->rrt_info
.rip6_dest
),
3269 rrt
->rrt_info
.rip6_plen
);
3274 /* Put the route to the list */
3275 TAILQ_INSERT_HEAD(&riprt_head
, rrt
, rrt_next
);
3276 trace(1, "Aggregate: %s/%d for %s\n",
3277 inet6_n2p(&iff
.iff_addr
), iff
.iff_plen
,
3278 loopifcp
->ifc_name
);
3279 /* Add this route to the kernel */
3280 if (nflag
) /* do not modify kernel routing table */
3282 addroute(rrt
, &in6addr_loopback
, loopifcp
);
3286 /***************** utility functions *****************/
3289 * Returns a pointer to ifac whose address and prefix length matches
3290 * with the address and prefix length specified in the arguments.
3292 static struct ifac
*
3293 ifa_match(const struct ifc
*ifcp
,
3294 const struct in6_addr
*ia
,
3299 TAILQ_FOREACH(ifac
, &ifcp
->ifc_ifac_head
, ifac_next
) {
3300 if (IN6_ARE_ADDR_EQUAL(&ifac
->ifac_addr
, ia
) &&
3301 ifac
->ifac_plen
== plen
)
3309 * Return a pointer to riprt structure whose address and prefix length
3310 * matches with the address and prefix length found in the argument.
3311 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3313 static struct riprt
*
3314 rtsearch(struct netinfo6
*np
)
3318 TAILQ_FOREACH(rrt
, &riprt_head
, rrt_next
) {
3319 if (rrt
->rrt_info
.rip6_plen
== np
->rip6_plen
&&
3320 IN6_ARE_ADDR_EQUAL(&rrt
->rrt_info
.rip6_dest
,
3329 sin6mask2len(const struct sockaddr_in6
*sin6
)
3332 return mask2len(&sin6
->sin6_addr
,
3333 sin6
->sin6_len
- offsetof(struct sockaddr_in6
, sin6_addr
));
3337 mask2len(const struct in6_addr
*addr
, int lenlim
)
3340 const u_char
*p
= (const u_char
*)addr
;
3342 for (j
= 0; j
< lenlim
; j
++, p
++) {
3349 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3350 MASKLEN(0xfe, 7); break;
3351 MASKLEN(0xfc, 6); break;
3352 MASKLEN(0xf8, 5); break;
3353 MASKLEN(0xf0, 4); break;
3354 MASKLEN(0xe0, 3); break;
3355 MASKLEN(0xc0, 2); break;
3356 MASKLEN(0x80, 1); break;
3363 static const u_char plent
[8] = {
3364 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3368 applyplen(struct in6_addr
*ia
, int plen
)
3374 for (i
= 0; i
< 16; i
++) {
3383 static const int pl2m
[9] = {
3384 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3387 static struct in6_addr
*
3390 static struct in6_addr ia
;
3394 memset(&ia
, 0, sizeof(struct in6_addr
));
3396 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
3410 int len
= strlen(p
) + 1;
3411 char *q
= (char *)malloc(len
);
3425 static char buf
[BUFSIZ
];
3430 if ((tm
= localtime(&t
)) == 0) {
3434 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3439 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3442 ripinterval(int timer
)
3446 interval
= (int)(timer
+ timer
* RIPRANDDEV
* (r
/ RAND_MAX
- 0.5));
3447 nextalarm
= time(NULL
) + interval
;
3458 t
= (int)(RIP_TRIG_INT6_MIN
+
3459 (RIP_TRIG_INT6_MAX
- RIP_TRIG_INT6_MIN
) * (r
/ RAND_MAX
));
3460 sup_trig_update
= time(NULL
) + t
;
3466 fatal(const char *fmt
, ...)
3472 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
3476 syslog(LOG_ERR
, "%s: %s", buf
, strerror(errno
));
3478 syslog(LOG_ERR
, "%s", buf
);
3483 tracet(int level
, const char *fmt
, ...)
3487 if (level
<= dflag
) {
3489 fprintf(stderr
, "%s: ", hms());
3490 vfprintf(stderr
, fmt
, ap
);
3496 vsyslog(LOG_DEBUG
, fmt
, ap
);
3498 vsyslog(LOG_WARNING
, fmt
, ap
);
3504 trace(int level
, const char *fmt
, ...)
3508 if (level
<= dflag
) {
3510 vfprintf(stderr
, fmt
, ap
);
3516 vsyslog(LOG_DEBUG
, fmt
, ap
);
3518 vsyslog(LOG_WARNING
, fmt
, ap
);
3524 ifc_find(char *name
)
3528 TAILQ_FOREACH(ifcp
, &ifc_head
, ifc_next
) {
3529 if (strcmp(name
, ifcp
->ifc_name
) == 0)
3536 iff_find(struct ifc
*ifcp
, int type
)
3540 TAILQ_FOREACH(iffp
, &ifcp
->ifc_iff_head
, iff_next
) {
3541 if (type
== IFIL_TYPE_ANY
||
3542 type
== iffp
->iff_type
)
3550 setindex2ifc(int idx
, struct ifc
*ifcp
)
3556 nindex2ifc
= 5; /*initial guess*/
3557 index2ifc
= (struct ifc
**)
3558 malloc(sizeof(*index2ifc
) * nindex2ifc
);
3559 if (index2ifc
== NULL
) {
3563 memset(index2ifc
, 0, sizeof(*index2ifc
) * nindex2ifc
);
3566 for (nsize
= nindex2ifc
; nsize
<= idx
; nsize
*= 2)
3569 p
= (struct ifc
**)realloc(index2ifc
,
3570 sizeof(*index2ifc
) * nsize
);
3575 memset(p
+ n
, 0, sizeof(*index2ifc
) * (nindex2ifc
- n
));
3579 index2ifc
[idx
] = ifcp
;