/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
struct ifc { /* Configuration of an interface */
char *ifc_name; /* if name */
struct ifc *ifc_next;
int ifc_index; /* if index */
int ifc_mtu; /* if mtu */
int ifc_metric; /* if metric */
u_int ifc_flags; /* flags */
short ifc_cflags; /* IFC_XXX */
struct in6_addr ifc_mylladdr; /* my link-local address */
struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
struct iff *ifc_filter; /* filter structure */
struct ifac *ifc_addr; /* list of AF_INET6 addresses */
int ifc_joined; /* joined to ff02::9 */
};
struct ifac { /* Address associated to an interface */
struct ifc *ifa_conf; /* back pointer */
struct ifac *ifa_next;
struct in6_addr ifa_addr; /* address */
struct in6_addr ifa_raddr; /* remote address, valid in p2p */
int ifa_plen; /* prefix length */
};
struct iff {
int iff_type;
struct in6_addr iff_addr;
int iff_plen;
struct iff *iff_next;
};
static struct ifc *ifc;
static int nifc; /* number of valid ifc's */
static struct ifc **index2ifc;
static int nindex2ifc;
static struct ifc *loopifcp = NULL; /* pointing to loopback */
static struct pollfd set[2];
static int rtsock; /* the routing socket */
static int ripsock; /* socket to send/receive RIP datagram */
static struct rip6 *ripbuf; /* packet buffer for sending */
/*
* Maintain the routes in a linked list. When the number of the routes
* grows, somebody would like to introduce a hash based or a radix tree
* based structure. I believe the number of routes handled by RIP is
* limited and I don't have to manage a complex data structure, however.
*
* One of the major drawbacks of the linear linked list is the difficulty
* of representing the relationship between a couple of routes. This may
* be a significant problem when we have to support route aggregation with
* suppressing the specifices covered by the aggregate.
*/
struct riprt {
struct riprt *rrt_next; /* next destination */
struct riprt *rrt_same; /* same destination - future use */
struct netinfo6 rrt_info; /* network info */
struct in6_addr rrt_gw; /* gateway */
u_long rrt_flags; /* kernel routing table flags */
u_long rrt_rflags; /* route6d routing table flags */
time_t rrt_t; /* when the route validated */
int rrt_index; /* ifindex from which this route got */
};
static struct riprt *riprt = 0;
static int dflag = 0; /* debug flag */
static int qflag = 0; /* quiet flag */
static int nflag = 0; /* don't update kernel routing table */
static int aflag = 0; /* age out even the statically defined routes */
static int hflag = 0; /* don't split horizon */
static int lflag = 0; /* exchange site local routes */
static int sflag = 0; /* announce static routes w/ split horizon */
static int Sflag = 0; /* announce static routes to every interface */
static unsigned long routetag = 0; /* route tag attached on originating case */
static char *filter[MAXFILTER];
static int filtertype[MAXFILTER];
static int nfilter = 0;
static pid_t pid;
static struct sockaddr_storage ripsin;
static int interval = 1;
static time_t nextalarm = 0;
if (signal(SIGALRM, sighandler) == SIG_ERR ||
signal(SIGQUIT, sighandler) == SIG_ERR ||
signal(SIGTERM, sighandler) == SIG_ERR ||
signal(SIGUSR1, sighandler) == SIG_ERR ||
signal(SIGHUP, sighandler) == SIG_ERR ||
signal(SIGINT, sighandler) == SIG_ERR) {
fatal("signal");
}
/*
* To avoid rip packet congestion (not on a cable but in this
* process), wait for a moment to send the first RIP6_RESPONSE
* packets.
*/
alarm(ripinterval(INIT_INTERVAL6));
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (iff_find(ifcp, 'N'))
continue;
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
sendrequest(ifcp);
}
syslog(LOG_INFO, "**** Started ****");
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
while (1) {
if (seenalrm) {
ripalarm();
seenalrm = 0;
continue;
}
if (seenquit) {
rtdexit();
seenquit = 0;
continue;
}
if (seenusr1) {
ifrtdump(SIGUSR1);
seenusr1 = 0;
continue;
}
switch (poll(set, 2, INFTIM))
{
case -1:
if (errno != EINTR) {
fatal("poll");
}
continue;
case 0:
continue;
default:
if (set[0].revents & POLLIN)
{
sigprocmask(SIG_BLOCK, &mask, &omask);
riprecv();
sigprocmask(SIG_SETMASK, &omask, NULL);
}
if (set[1].revents & POLLIN)
{
sigprocmask(SIG_BLOCK, &mask, &omask);
rtrecv();
sigprocmask(SIG_SETMASK, &omask, NULL);
}
}
}
}
static void
sighandler(int signo)
{
switch (signo) {
case SIGALRM:
seenalrm++;
break;
case SIGQUIT:
case SIGTERM:
seenquit++;
break;
case SIGUSR1:
case SIGHUP:
case SIGINT:
seenusr1++;
break;
}
}
/*
* Called periodically:
* 1. age out the learned route. remove it if necessary.
* 2. submit RIP6_RESPONSE packets.
* Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
* to invoke this function in every 1 or 5 or 10 seconds only to age the
* routes more precisely.
*/
/* ARGSUSED */
static void
ripalarm(void)
{
struct ifc *ifcp;
struct riprt *rrt, *rrt_prev, *rrt_next;
time_t t_lifetime, t_holddown;
/* Put the route to the buffer */
*nip = rrt->rrt_info;
nip++; nrt++;
if (nrt == maxrte) {
ripflush(ifcp, sin6);
nh = NULL;
}
}
if (nrt) /* Send last packet */
ripflush(ifcp, sin6);
}
/*
* -A: filter out less specific routes, if we have aggregated
* route configured.
*/
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
continue;
ia = rrt->rrt_info.rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
return 0;
}
/*
* if it is an aggregated route, advertise it only to the
* interfaces specified on -A.
*/
if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
ok = 0;
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok)
return 0;
}
/*
* -O: advertise only if prefix matches the configured prefix.
*/
if (iff_find(ifcp, 'O')) {
ok = 0;
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'O')
continue;
if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
continue;
ia = rrt->rrt_info.rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok)
return 0;
}
/* the prefix should be advertised */
return 1;
}
/*
* Determine if the route is to be advertised on the specified interface.
* It checks options specified in the arguments and the split horizon rule.
*/
static int
tobeadv(struct riprt *rrt, struct ifc *ifcp)
{
/* Special care for static routes */
if (rrt->rrt_flags & RTF_STATIC) {
/* XXX don't advertise reject/blackhole routes */
if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
return 0;
if (Sflag) /* Yes, advertise it anyway */
return 1;
if (sflag && rrt->rrt_index != ifcp->ifc_index)
return 1;
return 0;
}
/* Regular split horizon */
if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
return 0;
return 1;
}
/*
* Send a rip packet actually.
*/
static int
sendpacket(struct sockaddr_in6 *sin6, int len)
{
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov[2];
u_char cmsgbuf[256];
struct in6_pktinfo *pi;
int idx;
struct sockaddr_in6 sincopy;
/* do not overwrite the given sin */
sincopy = *sin6;
sin6 = &sincopy;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
/* XXX: do not mix the interface index and link index */
idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
sin6->sin6_scope_id = idx;
} else
idx = 0;
if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
trace(1, "Packets from non-ll addr: %s\n",
inet6_n2p(&fsock.sin6_addr));
return; /* Ignore packets from non-link-local addr */
}
idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
ifcp = (idx < (unsigned)nindex2ifc) ? index2ifc[idx] : NULL;
if (!ifcp) {
trace(1, "Packets to unknown interface index %d\n", idx);
return; /* Ignore it */
}
if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
return; /* The packet is from me; ignore */
if (rp->rip6_cmd != RIP6_RESPONSE) {
trace(1, "Invalid command %d\n", rp->rip6_cmd);
return;
}
/* -N: no use */
if (iff_find(ifcp, 'N') != NULL)
return;
tracet(1, "Recv(%s): from %s.%d info(%d)\n",
ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), (int)nn);
t = time(NULL);
t_half_lifetime = t - (RIP_LIFETIME/2);
for (; nn; nn--, np++) {
if (np->rip6_metric == NEXTHOP_METRIC) {
/* modify neighbor address */
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
nh = np->rip6_dest;
SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
nh = fsock.sin6_addr;
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
} else {
nh = fsock.sin6_addr;
trace(1, "\tInvalid Nexthop: %s\n",
inet6_n2p(&np->rip6_dest));
}
continue;
}
if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
/* may need to pass sitelocal prefix in some case, however*/
if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
trace(2, "\tnetinfo6: %s/%d [%d]",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
if (np->rip6_tag)
trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
if (dflag >= 2) {
ia = np->rip6_dest;
applyplen(&ia, np->rip6_plen);
if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
trace(2, " [junk outside prefix]");
}
/*
* -L: listen only if the prefix matches the configuration
*/
ok = 1; /* if there's no L filter, it is ok */
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'L')
continue;
ok = 0;
if (np->rip6_plen < iffp->iff_plen)
continue;
/* special rule: ::/0 means default, not "in /0" */
if (iffp->iff_plen == 0 && np->rip6_plen > 0)
continue;
ia = np->rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok) {
trace(2, " (filtered)\n");
continue;
}
/* Put the route to the list */
rrt->rrt_next = riprt;
riprt = rrt;
/* Update routing table */
addroute(rrt, &nh, ifcp);
rrt->rrt_rflags |= RRTF_CHANGED;
need_trigger = 1;
rrt->rrt_t = t;
}
}
/* XXX need to care the interval between triggered updates */
if (need_trigger) {
if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
for (ic = ifc; ic; ic = ic->ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
ripsend(ic, &ic->ifc_ripsin,
RRTF_CHANGED);
}
}
/* Reset the flag */
for (rrt = riprt; rrt; rrt = rrt->rrt_next)
rrt->rrt_rflags &= ~RRTF_CHANGED;
}
}
/*
* Send all routes request packet to the specified interface.
*/
static void
sendrequest(struct ifc *ifcp)
{
struct netinfo6 *np;
int error;
if (ifcp->ifc_flags & IFF_LOOPBACK)
return;
ripbuf->rip6_cmd = RIP6_REQUEST;
np = ripbuf->rip6_nets;
memset(np, 0, sizeof(struct netinfo6));
np->rip6_metric = HOPCNT_INFINITY6;
tracet(1, "Send rtdump Request to %s (%s)\n",
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
if (error == EAFNOSUPPORT) {
/* Protocol not supported */
tracet(1, "Could not send rtdump Request to %s (%s): "
"set IFF_UP to 0\n",
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
}
ripbuf->rip6_cmd = RIP6_RESPONSE;
}
/*
* Process a RIP6_REQUEST packet.
*/
static void
riprequest(struct ifc *ifcp, struct netinfo6 *np, int nn,
struct sockaddr_in6 *sin6)
{
int i;
struct riprt *rrt;
/*
* Receive and process routing messages.
* Update interface information as necessary.
*/
static void
rtrecv(void)
{
char buf[BUFSIZ];
char *p, *q;
struct rt_msghdr *rtm;
struct ifa_msghdr *ifam;
struct if_msghdr *ifm;
int len;
struct ifc *ifcp, *ic;
int iface = 0, rtable = 0;
struct sockaddr_in6 *rta[RTAX_MAX];
struct sockaddr_in6 mask;
int i, addrs;
struct riprt *rrt;
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
perror("read from rtsock");
exit(1);
}
if (len < (int)sizeof(*rtm)) {
trace(1, "short read from rtsock: %d (should be > %lu)\n",
len, (u_long)sizeof(*rtm));
return;
}
if (dflag >= 2) {
fprintf(stderr, "rtmsg:\n");
for (i = 0; i < len; i++) {
fprintf(stderr, "%02x ", buf[i] & 0xff);
if (i % 16 == 15) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
/* safety against bogus message */
if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
trace(1, "bogus rtmsg: length=%d\n",
((struct rt_msghdr *)p)->rtm_msglen);
break;
}
rtm = NULL;
ifam = NULL;
ifm = NULL;
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_DELADDR:
ifam = (struct ifa_msghdr *)p;
addrs = ifam->ifam_addrs;
q = (char *)(ifam + 1);
break;
case RTM_IFINFO:
ifm = (struct if_msghdr *)p;
addrs = ifm->ifm_addrs;
q = (char *)(ifm + 1);
break;
default:
rtm = (struct rt_msghdr *)p;
addrs = rtm->rtm_addrs;
q = (char *)(rtm + 1);
if (rtm->rtm_version != RTM_VERSION) {
trace(1, "unexpected rtmsg version %d "
"(should be %d)\n",
rtm->rtm_version, RTM_VERSION);
continue;
}
if (rtm->rtm_pid == pid) {
#if 0
trace(1, "rtmsg looped back to me, ignored\n");
#endif
continue;
}
break;
}
memset(&rta, 0, sizeof(rta));
for (i = 0; i < RTAX_MAX; i++) {
if (addrs & (1 << i)) {
rta[i] = (struct sockaddr_in6 *)q;
q += ROUNDUP(rta[i]->sin6_len);
}
}
trace(1, "rtsock: %s (addrs=%x)\n",
rttypes((struct rt_msghdr *)p), addrs);
if (dflag >= 2) {
for (i = 0;
i < ((struct rt_msghdr *)p)->rtm_msglen;
i++) {
fprintf(stderr, "%02x ", p[i] & 0xff);
if (i % 16 == 15) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
/*
* Easy ones first.
*
* We may be able to optimize by using ifm->ifm_index or
* ifam->ifam_index. For simplicity we don't do that here.
*/
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_IFINFO:
iface++;
continue;
case RTM_ADD:
rtable++;
continue;
case RTM_LOSING:
case RTM_MISS:
case RTM_GET:
case RTM_LOCK:
/* nothing to be done here */
trace(1, "\tnothing to be done, ignored\n");
continue;
}
/* hard ones */
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_IFINFO:
case RTM_ADD:
case RTM_LOSING:
case RTM_MISS:
case RTM_GET:
case RTM_LOCK:
/* should already be handled */
fatal("rtrecv: never reach here");
/* NOTREACHED */
case RTM_DELETE:
if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
trace(1, "\tsome of dst/gw/netamsk are "
"unavailable, ignored\n");
break;
}
if ((rtm->rtm_flags & RTF_HOST) != 0) {
mask.sin6_len = sizeof(mask);
memset(&mask.sin6_addr, 0xff,
sizeof(mask.sin6_addr));
rta[RTAX_NETMASK] = &mask;
} else if (!rta[RTAX_NETMASK]) {
trace(1, "\tsome of dst/gw/netamsk are "
"unavailable, ignored\n");
break;
}
if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
rta[RTAX_NETMASK]) == 0) {
rtable++; /*just to be sure*/
}
break;
case RTM_CHANGE:
case RTM_REDIRECT:
trace(1, "\tnot supported yet, ignored\n");
break;
case RTM_DELADDR:
if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
trace(1, "\tno netmask or ifa given, ignored\n");
break;
}
if (ifam->ifam_index < nindex2ifc)
ifcp = index2ifc[ifam->ifam_index];
else
ifcp = NULL;
if (!ifcp) {
trace(1, "\tinvalid ifam_index %d, ignored\n",
ifam->ifam_index);
break;
}
if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
iface++;
break;
case RTM_OLDADD:
case RTM_OLDDEL:
trace(1, "\tnot supported yet, ignored\n");
break;
}
}
if (iface) {
trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
ifconfig();
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
if (ifcp->ifc_cflags & IFC_CHANGED) {
if (ifrt(ifcp, 1)) {
for (ic = ifc; ic; ic = ic->ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
ripsend(ic, &ic->ifc_ripsin,
RRTF_CHANGED);
}
/* Reset the flag */
for (rrt = riprt; rrt; rrt = rrt->rrt_next)
rrt->rrt_rflags &= ~RRTF_CHANGED;
}
ifcp->ifc_cflags &= ~IFC_CHANGED;
}
}
if (rtable) {
trace(1, "rtsock: read routing table again\n");
krtread(1);
}
}
if (sifa->sin6_family != AF_INET6) {
trace(1, "\tother AF, ignored\n");
return -1;
}
addr = &sifa->sin6_addr;
prefix = sin6mask2len(smask);
trace(1, "\tdeleting %s/%d from %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
ifa = ifa_match(ifcp, addr, prefix);
if (!ifa) {
trace(1, "\tno matching ifa found for %s/%d on %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
return -1;
}
if (ifa->ifa_conf != ifcp) {
trace(1, "\taddress table corrupt: back pointer does not match "
"(%s != %s)\n",
ifcp->ifc_name, ifa->ifa_conf->ifc_name);
return -1;
}
/* remove ifa from interface */
if (ifcp->ifc_addr == ifa)
ifcp->ifc_addr = ifa->ifa_next;
else {
struct ifac *p;
for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
if (p->ifa_next == ifa) {
p->ifa_next = ifa->ifa_next;
break;
}
}
}
ifa->ifa_next = NULL;
ifa->ifa_conf = NULL;
t_lifetime = time(NULL) - RIP_LIFETIME;
/* age route for interface address */
memset(&ni6, 0, sizeof(ni6));
ni6.rip6_dest = ifa->ifa_addr;
ni6.rip6_plen = ifa->ifa_plen;
applyplen(&ni6.rip6_dest, ni6.rip6_plen);
trace(1, "\tfind interface route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
struct in6_addr none;
memset(&none, 0, sizeof(none));
if (rrt->rrt_index == ifcp->ifc_index &&
(IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
}
updated++;
} else {
trace(1, "\tnon-interface route found: %s/%d on %d\n",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen,
rrt->rrt_index);
}
} else
trace(1, "\tno interface route found\n");
/* age route for p2p destination */
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
memset(&ni6, 0, sizeof(ni6));
ni6.rip6_dest = ifa->ifa_raddr;
ni6.rip6_plen = 128;
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
trace(1, "\tfind p2p route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
ifcp->ifc_index);
if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
if (rrt->rrt_index == ifcp->ifc_index &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
rrt->rrt_info.rip6_metric =
HOPCNT_INFINITY6;
updated++;
}
} else {
trace(1, "\tnon-p2p route found: %s/%d on %d\n",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen,
rrt->rrt_index);
}
} else
trace(1, "\tno p2p route found\n");
}
return updated ? 0 : -1;
}
/*
* Get each interface address and put those interface routes to the route
* list.
*/
static int
ifrt(struct ifc *ifcp, int again)
{
struct ifac *ifa;
struct riprt *rrt = NULL, *search_rrt, *prev_rrt, *loop_rrt;
struct netinfo6 *np;
time_t t_lifetime;
int need_trigger = 0;
/*
* there are couple of p2p interface routing models. "behavior" lets
* you pick one. it looks that gated behavior fits best with BSDs,
* since BSD kernels do not look at prefix length on p2p interfaces.
*/
static void
ifrt_p2p(struct ifc *ifcp, int again)
{
struct ifac *ifa;
struct riprt *rrt, *orrt, *prevrrt;
struct netinfo6 *np;
struct in6_addr addr, dest;
int advert, ignore, i;
#define P2PADVERT_NETWORK 1
#define P2PADVERT_ADDR 2
#define P2PADVERT_DEST 4
#define P2PADVERT_MAX 4
#define CISCO 0
#define GATED 1
#define ROUTE6D 2
#define BEHAVIOR GATED
const char *category = "";
const char *noadv;
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
addr = ifa->ifa_addr;
dest = ifa->ifa_raddr;
applyplen(&addr, ifa->ifa_plen);
applyplen(&dest, ifa->ifa_plen);
advert = ignore = 0;
#if BEHAVIOR == CISCO
/*
* honor addr/plen, just like normal shared medium
* interface. this may cause trouble if you reuse
* addr/plen on other interfaces.
*
* advertise addr/plen.
*/
advert |= P2PADVERT_NETWORK;
#endif
#if BEHAVIOR == GATED
/*
* prefixlen on p2p interface is meaningless.
* advertise addr/128 and dest/128.
*
* do not install network route to route6d routing
* table (if we do, it would prevent route installation
* for other p2p interface that shares addr/plen).
*
* XXX what should we do if dest is ::? it will not
* get announced anyways (see following filter),
* but we need to think.
*/
advert |= P2PADVERT_ADDR;
advert |= P2PADVERT_DEST;
ignore |= P2PADVERT_NETWORK;
#endif
#if BEHAVIOR == ROUTE6D
/*
* just for testing. actually the code is redundant
* given the current p2p interface address assignment
* rule for kame kernel.
*
* intent:
* A/n -> announce A/n
* A B/n, A and B share prefix -> A/n (= B/n)
* A B/n, do not share prefix -> A/128 and B/128
* actually, A/64 and A B/128 are the only cases
* permitted by the kernel:
* A/64 -> A/64
* A B/128 -> A/128 and B/128
*/
if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
advert |= P2PADVERT_NETWORK;
else {
advert |= P2PADVERT_ADDR;
advert |= P2PADVERT_DEST;
ignore |= P2PADVERT_NETWORK;
}
} else
advert |= P2PADVERT_NETWORK;
#endif
if (sig == 0)
dump = stderr;
else
if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
dump = stderr;
t = time(NULL);
fprintf(dump, "\n%s: Routing Table Dump\n", hms());
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_t == 0)
age = 0;
else
age = t - rrt->rrt_t;
inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
buf, sizeof(buf));
fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
index2ifc[rrt->rrt_index]->ifc_name,
inet6_n2p(&rrt->rrt_gw),
rrt->rrt_info.rip6_metric, (long)age);
if (rrt->rrt_info.rip6_tag) {
fprintf(dump, " tag(0x%04x)",
ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
}
if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
fprintf(dump, " NOT-LL");
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
fprintf(dump, " NO-ADV");
fprintf(dump, "\n");
}
fprintf(dump, "\n");
if (dump != stderr)
fclose(dump);
}
/*
* Parse the -A (and -O) options and put corresponding filter object to the
* specified interface structures. Each of the -A/O option has the following
* syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
* -O 5f09:c400::/32,ef0,ef1 (only when match)
*/
static void
filterconfig(void)
{
int i;
char *p, *ap, *iflp, *ifname, *ep;
struct iff ftmp, *iff_obj;
struct ifc *ifcp;
struct riprt *rrt;
#if 0
struct in6_addr gw;
#endif
u_long plen;
for (i = 0; i < nfilter; i++) {
ap = filter[i];
iflp = NULL;
ifcp = NULL;
if (filtertype[i] == 'N' || filtertype[i] == 'T') {
iflp = ap;
goto ifonly;
}
if ((p = strchr(ap, ',')) != NULL) {
*p++ = '\0';
iflp = p;
}
if ((p = strchr(ap, '/')) == NULL) {
fatal("no prefixlen specified for '%s'", ap);
}
*p++ = '\0';
if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
fatal("invalid prefix specified for '%s'", ap);
}
errno = 0;
ep = NULL;
plen = strtoul(p, &ep, 10);
if (errno || !*p || *ep || plen > sizeof(ftmp.iff_addr) * 8) {
fatal("invalid prefix length specified for '%s'", ap);
}
ftmp.iff_plen = plen;
ftmp.iff_next = NULL;
applyplen(&ftmp.iff_addr, ftmp.iff_plen);
ifonly:
ftmp.iff_type = filtertype[i];
if (iflp == NULL || *iflp == '\0') {
fatal("no interface specified for '%s'", ap);
}
/* parse the interface listing portion */
while (iflp) {
ifname = iflp;
if ((iflp = strchr(iflp, ',')) != NULL)
*iflp++ = '\0';
ifcp = ifc_find(ifname);
if (ifcp == NULL) {
fatal("no interface %s exists", ifname);
}
iff_obj = malloc(sizeof(struct iff));
if (iff_obj == NULL) {
fatal("malloc of iff_obj");
}
memcpy((void *)iff_obj, (void *)&ftmp,
sizeof(struct iff));
/* link it to the interface filter */
iff_obj->iff_next = ifcp->ifc_filter;
ifcp->ifc_filter = iff_obj;
}
/*
* -A: aggregate configuration.
*/
if (filtertype[i] != 'A')
continue;
/* put the aggregate to the kernel routing table */
rrt = malloc(sizeof(struct riprt));
if (rrt == NULL) {
fatal("malloc: rrt");
}
memset(rrt, 0, sizeof(struct riprt));
rrt->rrt_info.rip6_dest = ftmp.iff_addr;
rrt->rrt_info.rip6_plen = ftmp.iff_plen;
rrt->rrt_info.rip6_metric = 1;
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_gw = in6addr_loopback;
rrt->rrt_flags = RTF_UP | RTF_REJECT;
rrt->rrt_rflags = RRTF_AGGREGATE;
rrt->rrt_t = 0;
rrt->rrt_index = loopifcp->ifc_index;
#if 0
if (getroute(&rrt->rrt_info, &gw)) {
#if 0
/*
* When the address has already been registered in the
* kernel routing table, it should be removed
*/
delroute(&rrt->rrt_info, &gw);
#else
/* it is safer behavior */
errno = EINVAL;
fatal("%s/%u already in routing table, "
"cannot aggregate",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen);
#endif
}
#endif
/* Put the route to the list */
rrt->rrt_next = riprt;
riprt = rrt;
trace(1, "Aggregate: %s/%d for %s\n",
inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
ifcp->ifc_name);
/* Add this route to the kernel */
if (nflag) /* do not modify kernel routing table */
continue;
addroute(rrt, &in6addr_loopback, loopifcp);
}
}
/*
* Returns a pointer to ifac whose address and prefix length matches
* with the address and prefix length specified in the arguments.
*/
static struct ifac *
ifa_match(const struct ifc *ifcp, const struct in6_addr *ia, int plen)
{
struct ifac *ifa;
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
ifa->ifa_plen == plen)
break;
}
return ifa;
}
/*
* Return a pointer to riprt structure whose address and prefix length
* matches with the address and prefix length found in the argument.
* Note: This is not a rtalloc(). Therefore exact match is necessary.
*/
static struct riprt *
rtsearch(struct netinfo6 *np, struct riprt **prev_rrt)
{
struct riprt *rrt;
if (prev_rrt)
*prev_rrt = NULL;
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&np->rip6_dest))
return rrt;
if (prev_rrt)
*prev_rrt = rrt;
}
if (prev_rrt)
*prev_rrt = NULL;
return 0;
}
static int
sin6mask2len(const struct sockaddr_in6 *sin6)
{