{
struct rt *rt;
struct ra *rap;
+ const struct routeinfo *rinfo;
const struct ipv6_addr *addr;
+ struct in6_addr netmask;
if (ctx->ra_routers == NULL)
return 0;
TAILQ_FOREACH(rap, ctx->ra_routers, next) {
if (rap->expired)
continue;
+
+ /* add rfc4191 route information routes */
+ TAILQ_FOREACH (rinfo, &rap->rinfos, next) {
+ if(rinfo->lifetime == 0)
+ continue;
+ if ((rt = inet6_makeroute(rap->iface, rap)) == NULL)
+ continue;
+
+ in6_addr_fromprefix(&netmask, rinfo->prefix_len);
+
+ sa_in6_init(&rt->rt_dest, &rinfo->prefix);
+ sa_in6_init(&rt->rt_netmask, &netmask);
+ sa_in6_init(&rt->rt_gateway, &rap->from);
+#ifdef HAVE_ROUTE_PREF
+ rt->rt_pref = ipv6nd_rtpref(rinfo->flags);
+#endif
+
+ rt_proto_add(routes, rt);
+ }
+
+ /* add subnet routes */
TAILQ_FOREACH(addr, &rap->addrs, next) {
if (addr->prefix_vltime == 0)
continue;
if (rt) {
rt->rt_dflags |= RTDF_RA;
#ifdef HAVE_ROUTE_PREF
- rt->rt_pref = ipv6nd_rtpref(rap);
+ rt->rt_pref = ipv6nd_rtpref(rap->flags);
#endif
rt_proto_add(routes, rt);
}
}
+
+ /* add default route */
if (rap->lifetime == 0)
continue;
if (ipv6_anyglobal(rap->iface) == NULL)
continue;
rt->rt_dflags |= RTDF_RA;
#ifdef HAVE_ROUTE_PREF
- rt->rt_pref = ipv6nd_rtpref(rap);
+ rt->rt_pref = ipv6nd_rtpref(rap->flags);
#endif
rt_proto_add(routes, rt);
}
#define ND_OPT_PI_FLAG_ROUTER 0x20 /* Router flag in PI */
#endif
+#ifndef ND_OPT_RI
+#define ND_OPT_RI 24
+struct nd_opt_ri { /* Route Information option RFC4191 */
+ uint8_t nd_opt_ri_type;
+ uint8_t nd_opt_ri_len;
+ uint8_t nd_opt_ri_prefixlen;
+ uint8_t nd_opt_ri_flags_reserved;
+ uint32_t nd_opt_ri_lifetime;
+ struct in6_addr nd_opt_ri_prefix;
+};
+__CTASSERT(sizeof(struct nd_opt_ri) == 24);
+#define OPT_RI_FLAG_PREFERENCE(flags) ((flags & 0x18) >> 3)
+#endif
+
#ifndef ND_OPT_RDNSS
#define ND_OPT_RDNSS 25
struct nd_opt_rdnss { /* RDNSS option RFC 6106 */
//
static void ipv6nd_handledata(void *, unsigned short);
+static struct routeinfo *routeinfo_findalloc(struct ra *, const struct in6_addr *, uint8_t);
+static void routeinfohead_free(struct routeinfohead *);
/*
* Android ships buggy ICMP6 filter headers.
}
int
-ipv6nd_rtpref(struct ra *rap)
+ipv6nd_rtpref(uint8_t flags)
{
- switch (rap->flags & ND_RA_FLAG_RTPREF_MASK) {
+ switch (flags & ND_RA_FLAG_RTPREF_MASK) {
case ND_RA_FLAG_RTPREF_HIGH:
return RTPREF_HIGH;
case ND_RA_FLAG_RTPREF_MEDIUM:
case ND_RA_FLAG_RTPREF_LOW:
return RTPREF_LOW;
default:
- logerrx("%s: impossible RA flag %x", __func__, rap->flags);
+ logerrx("%s: impossible RA flag %x", __func__, flags);
return RTPREF_INVALID;
}
/* NOTREACHED */
continue;
if (!ra1->isreachable && ra2->reachable)
continue;
- if (ipv6nd_rtpref(ra1) <= ipv6nd_rtpref(ra2))
+ if (ipv6nd_rtpref(ra1->flags) <= ipv6nd_rtpref(ra2->flags))
continue;
/* All things being equal, prefer older routers. */
/* We don't need to check time, becase newer
if (remove_ra)
TAILQ_REMOVE(rap->iface->ctx->ra_routers, rap, next);
ipv6_freedrop_addrs(&rap->addrs, drop_ra, NULL);
+ routeinfohead_free(&rap->rinfos);
free(rap->data);
free(rap);
}
struct nd_opt_prefix_info pi;
struct nd_opt_mtu mtu;
struct nd_opt_rdnss rdnss;
+ struct nd_opt_ri ri;
+ struct routeinfo *rinfo;
uint8_t *p;
struct ra *rap;
struct in6_addr pi_prefix;
rap->from = from->sin6_addr;
strlcpy(rap->sfrom, sfrom, sizeof(rap->sfrom));
TAILQ_INIT(&rap->addrs);
+ TAILQ_INIT(&rap->rinfos);
new_rap = true;
rap->isreachable = true;
} else
rap->flags = nd_ra->nd_ra_flags_reserved;
old_lifetime = rap->lifetime;
rap->lifetime = ntohs(nd_ra->nd_ra_router_lifetime);
- if (!new_rap && rap->lifetime == 0 && old_lifetime != 0)
- logwarnx("%s: %s: no longer a default router (lifetime = 0)",
- ifp->name, rap->sfrom);
if (nd_ra->nd_ra_curhoplimit != 0)
rap->hoplimit = nd_ra->nd_ra_curhoplimit;
else
rdnss.nd_opt_rdnss_len > 1)
rap->hasdns = 1;
break;
+ case ND_OPT_RI:
+ if (ndo.nd_opt_len > 3) {
+ logmessage(loglevel, "%s: invalid route info option",
+ ifp->name);
+ break;
+ }
+ memset(&ri, 0, sizeof(ri));
+ memcpy(&ri, p, olen); /* may be smaller than sizeof(ri), pad with zero */
+ if(ri.nd_opt_ri_prefixlen > 128) {
+ logmessage(loglevel, "%s: invalid route info prefix length",
+ ifp->name);
+ break;
+ }
+
+ /* rfc4191 3.1 - RI for ::/0 applies to default route */
+ if(ri.nd_opt_ri_prefixlen == 0) {
+ rap->lifetime = ntohl(ri.nd_opt_ri_lifetime);
+
+ /* Update preference leaving other flags intact */
+ rap->flags = ((rap->flags & (~ (unsigned int)ND_RA_FLAG_RTPREF_MASK))
+ | ri.nd_opt_ri_flags_reserved) & 0xff;
+
+ break;
+ }
+
+ /* Update existing route info instead of rebuilding all routes so that
+ previously announced but now absent routes can stay alive. To kill a
+ route early, an RI with lifetime=0 needs to be received (rfc4191 3.1)*/
+ rinfo = routeinfo_findalloc(rap, &ri.nd_opt_ri_prefix, ri.nd_opt_ri_prefixlen);
+ if(rinfo == NULL) {
+ logerr(__func__);
+ break;
+ }
+
+ /* Update/initialize other route info params */
+ rinfo->flags = ri.nd_opt_ri_flags_reserved;
+ rinfo->lifetime = ntohl(ri.nd_opt_ri_lifetime);
+ rinfo->acquired = rap->acquired;
+
+ break;
default:
continue;
}
ia->prefix_pltime = 0;
}
+ if (!new_rap && rap->lifetime == 0 && old_lifetime != 0)
+ logwarnx("%s: %s: no longer a default router (lifetime = 0)",
+ ifp->name, rap->sfrom);
+
if (new_data && !has_address && rap->lifetime && !ipv6_anyglobal(ifp))
logwarnx("%s: no global addresses for default route",
ifp->name);
return -1;
if (efprintf(fp, "%s_hoplimit=%u", ndprefix, rap->hoplimit) == -1)
return -1;
- pref = ipv6nd_rtpref(rap);
+ pref = ipv6nd_rtpref(rap->flags);
if (efprintf(fp, "%s_flags=%s%s%s%s%s", ndprefix,
rap->flags & ND_RA_FLAG_MANAGED ? "M" : "",
rap->flags & ND_RA_FLAG_OTHER ? "O" : "",
uint32_t elapsed;
bool expired, valid;
struct ipv6_addr *ia;
+ struct routeinfo *rinfo, *rinfob;
size_t len, olen;
uint8_t *p;
struct nd_opt_hdr ndo;
if (rap->iface != ifp || rap->expired)
continue;
valid = false;
- if (rap->lifetime) {
+ /* lifetime may be set to infinite by rfc4191 route information */
+ if (rap->lifetime && rap->lifetime != ND6_INFINITE_LIFETIME) {
elapsed = (uint32_t)eloop_timespec_diff(&now,
&rap->acquired, NULL);
if (elapsed >= rap->lifetime || rap->doexpire) {
}
}
+ /* Expire route information */
+ TAILQ_FOREACH_SAFE(rinfo, &rap->rinfos, next, rinfob) {
+ if (rinfo->lifetime == ND6_INFINITE_LIFETIME &&
+ !rap->doexpire)
+ continue;
+ elapsed = (uint32_t)eloop_timespec_diff(&now,
+ &rinfo->acquired, NULL);
+ if (elapsed >= rinfo->lifetime || rap->doexpire) {
+ logwarnx("%s: expired route %s",
+ rap->iface->name, rinfo->sprefix);
+ TAILQ_REMOVE(&rap->rinfos, rinfo, next);
+ }
+ }
+
/* Work out expiry for ND options */
elapsed = (uint32_t)eloop_timespec_diff(&now,
&rap->acquired, NULL);
eloop_timeout_add_msec(ifp->ctx->eloop, delay, ipv6nd_startrs1, ifp);
return;
}
+
+static struct routeinfo *routeinfo_findalloc(struct ra *rap, const struct in6_addr *prefix, uint8_t prefix_len)
+{
+ struct routeinfo *ri;
+ char buf[INET6_ADDRSTRLEN];
+ const char *p;
+
+ TAILQ_FOREACH(ri, &rap->rinfos, next) {
+ if (ri->prefix_len == prefix_len &&
+ IN6_ARE_ADDR_EQUAL(&ri->prefix, prefix))
+ return ri;
+ }
+
+ ri = malloc(sizeof(struct routeinfo));
+ if (ri == NULL)
+ return NULL;
+
+ memcpy(&ri->prefix, prefix, sizeof(ri->prefix));
+ ri->prefix_len = prefix_len;
+ p = inet_ntop(AF_INET6, prefix, buf, sizeof(buf));
+ if (p)
+ snprintf(ri->sprefix,
+ sizeof(ri->sprefix),
+ "%s/%d",
+ p, prefix_len);
+ else
+ ri->sprefix[0] = '\0';
+ TAILQ_INSERT_TAIL(&rap->rinfos, ri, next);
+ return ri;
+}
+
+static void routeinfohead_free(struct routeinfohead *head)
+{
+ struct routeinfo *ri;
+
+ while ((ri = TAILQ_FIRST(head))) {
+ TAILQ_REMOVE(head, ri, next);
+ free(ri);
+ }
+}