#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/queue.h>
#include <kern/lock.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <net/radix.h>
#include <netinet/in.h>
#include <netinet6/in6_var.h>
#include <netinet6/in6_ifattach.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/nd6.h>
#include <netinet/icmp6.h>
#include <netinet6/scope6_var.h>
#include <net/net_osdep.h>
#define SDL(s) ((struct sockaddr_dl *)s)
static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
static struct in6_ifaddr *in6_ifadd(struct nd_prefix *,
struct in6_addr *);
static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
struct nd_defrouter *);
static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
static void pfxrtr_del(struct nd_pfxrouter *);
static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
static void defrouter_addifreq(struct ifnet *);
static void nd6_rtmsg(int, struct rtentry *);
static void in6_init_address_ltimes(struct nd_prefix *ndpr,
struct in6_addrlifetime *lt6);
static int rt6_deleteroute(struct radix_node *, void *);
extern int nd6_recalc_reachtm_interval;
static struct ifnet *nd6_defifp;
int nd6_defifindex;
int ip6_use_tempaddr = 0;
int ip6_desync_factor;
u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
extern lck_mtx_t *rt_mtx;
extern lck_mtx_t *nd6_mutex;
void
nd6_rs_input(
struct mbuf *m,
int off,
int icmp6len)
{
struct ifnet *ifp = m->m_pkthdr.rcvif;
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
struct nd_router_solicit *nd_rs;
struct in6_addr saddr6 = ip6->ip6_src;
#if 0
struct in6_addr daddr6 = ip6->ip6_dst;
#endif
char *lladdr = NULL;
int lladdrlen = 0;
#if 0
struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
struct rtentry *rt = NULL;
int is_newentry;
#endif
union nd_opts ndopts;
if (ip6_accept_rtadv != 0 || (ifp->if_eflags & IFEF_ACCEPT_RTADVD) || ip6_forwarding != 1)
goto freeit;
if (ip6->ip6_hlim != 255) {
nd6log((LOG_ERR,
"nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
goto bad;
}
if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
goto freeit;
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, icmp6len, return);
nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
#else
IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
if (nd_rs == NULL) {
icmp6stat.icp6s_tooshort++;
return;
}
#endif
icmp6len -= sizeof(*nd_rs);
nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
if (nd6_options(&ndopts) < 0) {
nd6log((LOG_INFO,
"nd6_rs_input: invalid ND option, ignored\n"));
goto freeit;
}
if (ndopts.nd_opts_src_lladdr) {
lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
}
if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
nd6log((LOG_INFO,
"nd6_rs_input: lladdrlen mismatch for %s "
"(if %d, RS packet %d)\n",
ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
goto bad;
}
nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
freeit:
m_freem(m);
return;
bad:
icmp6stat.icp6s_badrs++;
m_freem(m);
}
void
nd6_ra_input(
struct mbuf *m,
int off,
int icmp6len)
{
struct ifnet *ifp = m->m_pkthdr.rcvif;
struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
struct nd_router_advert *nd_ra;
struct in6_addr saddr6 = ip6->ip6_src;
#if 0
struct in6_addr daddr6 = ip6->ip6_dst;
int flags;
int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
#endif
union nd_opts ndopts;
struct nd_defrouter *dr;
struct timeval timenow;
getmicrotime(&timenow);
if (ip6_accept_rtadv == 0 && ((ifp->if_eflags & IFEF_ACCEPT_RTADVD) == 0))
goto freeit;
if (ip6->ip6_hlim != 255) {
nd6log((LOG_ERR,
"nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
goto bad;
}
if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
nd6log((LOG_ERR,
"nd6_ra_input: src %s is not link-local\n",
ip6_sprintf(&saddr6)));
goto bad;
}
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, icmp6len, return);
nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
#else
IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
if (nd_ra == NULL) {
icmp6stat.icp6s_tooshort++;
return;
}
#endif
icmp6len -= sizeof(*nd_ra);
nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
if (nd6_options(&ndopts) < 0) {
nd6log((LOG_INFO,
"nd6_ra_input: invalid ND option, ignored\n"));
goto freeit;
}
{
struct nd_defrouter dr0;
u_int32_t advreachable = nd_ra->nd_ra_reachable;
dr0.rtaddr = saddr6;
dr0.flags = nd_ra->nd_ra_flags_reserved;
dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
dr0.expire = timenow.tv_sec + dr0.rtlifetime;
dr0.ifp = ifp;
dr0.advint = 0;
dr0.advint_expire = 0;
dr0.advints_lost = 0;
if (advreachable) {
advreachable = ntohl(advreachable);
if (advreachable <= MAX_REACHABLE_TIME &&
ndi->basereachable != advreachable) {
ndi->basereachable = advreachable;
ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
ndi->recalctm = nd6_recalc_reachtm_interval;
}
}
if (nd_ra->nd_ra_retransmit)
ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
if (nd_ra->nd_ra_curhoplimit)
ndi->chlim = nd_ra->nd_ra_curhoplimit;
dr = defrtrlist_update(&dr0);
}
if (ndopts.nd_opts_pi) {
struct nd_opt_hdr *pt;
struct nd_opt_prefix_info *pi = NULL;
struct nd_prefix pr;
for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
pt = (struct nd_opt_hdr *)((caddr_t)pt +
(pt->nd_opt_len << 3))) {
if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
continue;
pi = (struct nd_opt_prefix_info *)pt;
if (pi->nd_opt_pi_len != 4) {
nd6log((LOG_INFO,
"nd6_ra_input: invalid option "
"len %d for prefix information option, "
"ignored\n", pi->nd_opt_pi_len));
continue;
}
if (128 < pi->nd_opt_pi_prefix_len) {
nd6log((LOG_INFO,
"nd6_ra_input: invalid prefix "
"len %d for prefix information option, "
"ignored\n", pi->nd_opt_pi_prefix_len));
continue;
}
if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
|| IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
nd6log((LOG_INFO,
"nd6_ra_input: invalid prefix "
"%s, ignored\n",
ip6_sprintf(&pi->nd_opt_pi_prefix)));
continue;
}
if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
&& pi->nd_opt_pi_prefix_len != 64) {
nd6log((LOG_INFO,
"nd6_ra_input: invalid prefixlen "
"%d for rfc2374 prefix %s, ignored\n",
pi->nd_opt_pi_prefix_len,
ip6_sprintf(&pi->nd_opt_pi_prefix)));
continue;
}
bzero(&pr, sizeof(pr));
pr.ndpr_prefix.sin6_family = AF_INET6;
pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
pr.ndpr_ifp = m->m_pkthdr.rcvif;
pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
pr.ndpr_pltime =
ntohl(pi->nd_opt_pi_preferred_time);
if (in6_init_prefix_ltimes(&pr))
continue;
(void)prelist_update(&pr, dr, m);
}
}
if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
if (mtu < IPV6_MMTU) {
nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
"mtu=%d sent from %s, ignoring\n",
mtu, ip6_sprintf(&ip6->ip6_src)));
goto skip;
}
if (ndi->maxmtu) {
if (mtu <= ndi->maxmtu) {
int change = (ndi->linkmtu != mtu);
ndi->linkmtu = mtu;
if (change)
in6_setmaxmtu();
} else {
nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
"mtu=%d sent from %s; "
"exceeds maxmtu %d, ignoring\n",
mtu, ip6_sprintf(&ip6->ip6_src),
ndi->maxmtu));
}
} else {
nd6log((LOG_INFO, "nd6_ra_input: mtu option "
"mtu=%d sent from %s; maxmtu unknown, "
"ignoring\n",
mtu, ip6_sprintf(&ip6->ip6_src)));
}
}
skip:
{
char *lladdr = NULL;
int lladdrlen = 0;
if (ndopts.nd_opts_src_lladdr) {
lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
}
if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
nd6log((LOG_INFO,
"nd6_ra_input: lladdrlen mismatch for %s "
"(if %d, RA packet %d)\n",
ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
goto bad;
}
nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
pfxlist_onlink_check(0);
}
freeit:
m_freem(m);
return;
bad:
icmp6stat.icp6s_badra++;
m_freem(m);
}
static void
nd6_rtmsg(cmd, rt)
int cmd;
struct rtentry *rt;
{
struct rt_addrinfo info;
lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
bzero((caddr_t)&info, sizeof(info));
info.rti_info[RTAX_DST] = rt_key(rt);
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
info.rti_info[RTAX_IFP] =
TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
rt_missmsg(cmd, &info, rt->rt_flags, 0);
}
void
defrouter_addreq(
struct nd_defrouter *new)
{
struct sockaddr_in6 def, mask, gate;
struct rtentry *newrt = NULL;
Bzero(&def, sizeof(def));
Bzero(&mask, sizeof(mask));
Bzero(&gate, sizeof(gate));
def.sin6_len = mask.sin6_len = gate.sin6_len
= sizeof(struct sockaddr_in6);
def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
gate.sin6_addr = new->rtaddr;
lck_mtx_lock(rt_mtx);
(void)rtrequest_locked(RTM_ADD, (struct sockaddr *)&def,
(struct sockaddr *)&gate, (struct sockaddr *)&mask,
RTF_GATEWAY, &newrt);
if (newrt) {
nd6_rtmsg(RTM_ADD, newrt);
rtunref(newrt);
}
lck_mtx_unlock(rt_mtx);
return;
}
void
defrouter_addifreq(
struct ifnet *ifp)
{
struct sockaddr_in6 def, mask;
struct ifaddr *ifa = NULL;
struct rtentry *newrt = NULL;
int error;
u_long flags;
bzero(&def, sizeof(def));
bzero(&mask, sizeof(mask));
def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
def.sin6_family = mask.sin6_family = AF_INET6;
if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
nd6log((LOG_ERR,
"defrouter_addifreq: failed to find an ifaddr "
"to install a route to interface %s\n",
if_name(ifp)));
return;
}
lck_mtx_lock(rt_mtx);
flags = ifa->ifa_flags;
error = rtrequest_locked(RTM_ADD, (struct sockaddr *)&def, ifa->ifa_addr,
(struct sockaddr *)&mask, flags, &newrt);
if (error != 0) {
nd6log((LOG_ERR,
"defrouter_addifreq: failed to install a route to "
"interface %s (errno = %d)\n",
if_name(ifp), error));
if (newrt)
rtunref(newrt);
} else {
if (newrt) {
nd6_rtmsg(RTM_ADD, newrt);
rtunref(newrt);
}
in6_post_msg(ifp, KEV_INET6_DEFROUTER, (struct in6_ifaddr *)ifa);
}
lck_mtx_unlock(rt_mtx);
ifafree(ifa);
}
struct nd_defrouter *
defrouter_lookup(
struct in6_addr *addr,
struct ifnet *ifp)
{
struct nd_defrouter *dr;
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (dr = TAILQ_FIRST(&nd_defrouter); dr;
dr = TAILQ_NEXT(dr, dr_entry)) {
if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
return(dr);
}
return(NULL);
}
void
defrouter_delreq(
struct nd_defrouter *dr,
int dofree)
{
struct sockaddr_in6 def, mask, gate;
struct rtentry *oldrt = NULL;
Bzero(&def, sizeof(def));
Bzero(&mask, sizeof(mask));
Bzero(&gate, sizeof(gate));
def.sin6_len = mask.sin6_len = gate.sin6_len
= sizeof(struct sockaddr_in6);
def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
gate.sin6_addr = dr->rtaddr;
lck_mtx_lock(rt_mtx);
rtrequest_locked(RTM_DELETE, (struct sockaddr *)&def,
(struct sockaddr *)&gate,
(struct sockaddr *)&mask,
RTF_GATEWAY, &oldrt);
if (oldrt) {
nd6_rtmsg(RTM_DELETE, oldrt);
if (oldrt->rt_refcnt <= 0) {
rtref(oldrt);
rtfree_locked(oldrt);
}
}
if (dofree)
FREE(dr, M_IP6NDP);
lck_mtx_unlock(rt_mtx);
}
void
defrtrlist_del(
struct nd_defrouter *dr, int nd6locked)
{
struct nd_defrouter *deldr = NULL;
struct nd_prefix *pr;
if (!ip6_forwarding && (ip6_accept_rtadv || (dr->ifp->if_eflags & IFEF_ACCEPT_RTADVD))) {
rt6_flush(&dr->rtaddr, dr->ifp);
}
if (nd6locked == 0)
lck_mtx_lock(nd6_mutex);
if (dr == TAILQ_FIRST(&nd_defrouter))
deldr = dr;
TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
struct nd_pfxrouter *pfxrtr;
if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
pfxrtr_del(pfxrtr);
}
pfxlist_onlink_check(1);
if (deldr)
defrouter_select();
if (nd6locked == 0)
lck_mtx_unlock(nd6_mutex);
FREE(dr, M_IP6NDP);
}
void
defrouter_select()
{
struct nd_defrouter *dr, anydr;
struct rtentry *rt = NULL;
struct llinfo_nd6 *ln = NULL;
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (dr = TAILQ_FIRST(&nd_defrouter); dr;
dr = TAILQ_NEXT(dr, dr_entry)) {
if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp, 0)) &&
(ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
ND6_IS_LLINFO_PROBREACH(ln)) {
TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
break;
}
}
if ((dr = TAILQ_FIRST(&nd_defrouter))) {
bzero(&anydr, sizeof(anydr));
defrouter_delreq(&anydr, 0);
defrouter_addreq(dr);
}
else {
if (!ip6_forwarding) {
bzero(&anydr, sizeof(anydr));
defrouter_delreq(&anydr, 0);
if (nd6_defifp) {
defrouter_addifreq(nd6_defifp);
} else {
nd6log((LOG_INFO, "defrouter_select: "
"there's no default router and no default"
" interface\n"));
}
}
}
return;
}
static struct nd_defrouter *
defrtrlist_update(
struct nd_defrouter *new)
{
struct nd_defrouter *dr, *n;
lck_mtx_lock(nd6_mutex);
if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
if (new->rtlifetime == 0) {
defrtrlist_del(dr, 1);
dr = NULL;
} else {
dr->flags = new->flags;
dr->rtlifetime = new->rtlifetime;
dr->expire = new->expire;
}
lck_mtx_unlock(nd6_mutex);
return(dr);
}
if (new->rtlifetime == 0) {
lck_mtx_unlock(nd6_mutex);
return(NULL);
}
n = (struct nd_defrouter *)_MALLOC(sizeof(*n), M_IP6NDP, M_NOWAIT);
if (n == NULL) {
lck_mtx_unlock(nd6_mutex);
return(NULL);
}
bzero(n, sizeof(*n));
*n = *new;
TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
if (TAILQ_FIRST(&nd_defrouter) == n)
defrouter_select();
lck_mtx_unlock(nd6_mutex);
return(n);
}
static struct nd_pfxrouter *
pfxrtr_lookup(
struct nd_prefix *pr,
struct nd_defrouter *dr)
{
struct nd_pfxrouter *search;
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
if (search->router == dr)
break;
}
return(search);
}
static void
pfxrtr_add(
struct nd_prefix *pr,
struct nd_defrouter *dr)
{
struct nd_pfxrouter *new;
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
new = (struct nd_pfxrouter *)_MALLOC(sizeof(*new), M_IP6NDP, M_NOWAIT);
if (new == NULL)
return;
bzero(new, sizeof(*new));
new->router = dr;
LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
pfxlist_onlink_check(1);
}
static void
pfxrtr_del(
struct nd_pfxrouter *pfr)
{
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
LIST_REMOVE(pfr, pfr_entry);
FREE(pfr, M_IP6NDP);
}
struct nd_prefix *
nd6_prefix_lookup(
struct nd_prefix *pr)
{
struct nd_prefix *search;
lck_mtx_lock(nd6_mutex);
for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
if (pr->ndpr_ifp == search->ndpr_ifp &&
pr->ndpr_plen == search->ndpr_plen &&
in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
&search->ndpr_prefix.sin6_addr,
pr->ndpr_plen)
) {
break;
}
}
lck_mtx_unlock(nd6_mutex);
return(search);
}
int
nd6_prelist_add(
struct nd_prefix *pr,
struct nd_defrouter *dr,
struct nd_prefix **newp)
{
struct nd_prefix *new = NULL;
int i;
new = (struct nd_prefix *)_MALLOC(sizeof(*new), M_IP6NDP, M_NOWAIT);
if (new == NULL)
return ENOMEM;
bzero(new, sizeof(*new));
*new = *pr;
if (newp != NULL)
*newp = new;
LIST_INIT(&new->ndpr_advrtrs);
in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
for (i = 0; i < 4; i++)
new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
new->ndpr_mask.s6_addr32[i];
lck_mtx_lock(nd6_mutex);
LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
if (new->ndpr_raf_onlink) {
int e;
if ((e = nd6_prefix_onlink(new, 0, 1)) != 0) {
nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
"the prefix %s/%d on-link on %s (errno=%d)\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
}
}
if (dr) {
pfxrtr_add(new, dr);
}
lck_mtx_unlock(nd6_mutex);
return 0;
}
void
prelist_remove(
struct nd_prefix *pr, int nd6locked)
{
struct nd_pfxrouter *pfr, *next;
int e;
pr->ndpr_vltime = 0;
pr->ndpr_pltime = 0;
#if 0
pr->ndpr_raf_onlink = 0;
pr->ndpr_raf_auto = 0;
#endif
if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
(e = nd6_prefix_offlink(pr)) != 0) {
nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
"on %s, errno=%d\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
}
if (pr->ndpr_refcnt > 0)
return;
if (nd6locked == 0)
lck_mtx_lock(nd6_mutex);
LIST_REMOVE(pr, ndpr_entry);
for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
next = pfr->pfr_next;
FREE(pfr, M_IP6NDP);
}
FREE(pr, M_IP6NDP);
pfxlist_onlink_check(1);
if (nd6locked == 0)
lck_mtx_unlock(nd6_mutex);
}
int
prelist_update(
struct nd_prefix *new,
struct nd_defrouter *dr,
struct mbuf *m)
{
struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
struct ifaddr *ifa;
struct ifnet *ifp = new->ndpr_ifp;
struct nd_prefix *pr;
int error = 0;
int newprefix = 0;
int auth;
struct in6_addrlifetime lt6_tmp;
struct timeval timenow;
auth = 0;
if (m) {
#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
auth = (m->m_flags & M_AUTHIPHDR
&& m->m_flags & M_AUTHIPDGM) ? 1 : 0;
#endif
}
if ((pr = nd6_prefix_lookup(new)) != NULL) {
if (new->ndpr_raf_onlink == 1)
pr->ndpr_raf_onlink = 1;
if (new->ndpr_raf_auto == 1)
pr->ndpr_raf_auto = 1;
if (new->ndpr_raf_onlink) {
pr->ndpr_vltime = new->ndpr_vltime;
pr->ndpr_pltime = new->ndpr_pltime;
pr->ndpr_preferred = new->ndpr_preferred;
pr->ndpr_expire = new->ndpr_expire;
}
if (new->ndpr_raf_onlink &&
(pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
int e;
if ((e = nd6_prefix_onlink(pr, 0, 0)) != 0) {
nd6log((LOG_ERR,
"prelist_update: failed to make "
"the prefix %s/%d on-link on %s "
"(errno=%d)\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
}
}
lck_mtx_lock(nd6_mutex);
if (dr && pfxrtr_lookup(pr, dr) == NULL)
pfxrtr_add(pr, dr);
lck_mtx_unlock(nd6_mutex);
} else {
struct nd_prefix *newpr = NULL;
newprefix = 1;
if (new->ndpr_vltime == 0)
goto end;
if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
goto end;
bzero(&new->ndpr_addr, sizeof(struct in6_addr));
error = nd6_prelist_add(new, dr, &newpr);
if (error != 0 || newpr == NULL) {
nd6log((LOG_NOTICE, "prelist_update: "
"nd6_prelist_add failed for %s/%d on %s "
"errno=%d, returnpr=%p\n",
ip6_sprintf(&new->ndpr_prefix.sin6_addr),
new->ndpr_plen, if_name(new->ndpr_ifp),
error, newpr));
goto end;
}
if (newpr->ndpr_raf_onlink == 0) {
newpr->ndpr_vltime = 0;
newpr->ndpr_pltime = 0;
in6_init_prefix_ltimes(newpr);
}
pr = newpr;
}
if (!new->ndpr_raf_auto)
goto afteraddrconf;
getmicrotime(&timenow);
ifnet_lock_exclusive(ifp);
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
{
struct in6_ifaddr *ifa6;
int ifa_plen;
u_int32_t storedlifetime;
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
ifa6 = (struct in6_ifaddr *)ifa;
if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
continue;
ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL);
if (ifa_plen != new->ndpr_plen ||
!in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr,
&new->ndpr_prefix.sin6_addr,
ifa_plen))
continue;
if (ia6_match == NULL)
ia6_match = ifa6;
if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
continue;
#define TWOHOUR (120*60)
lt6_tmp = ifa6->ia6_lifetime;
storedlifetime = IFA6_IS_INVALID(ifa6) ? 0 :
(lt6_tmp.ia6t_expire - timenow.tv_sec);
if (TWOHOUR < new->ndpr_vltime ||
storedlifetime < new->ndpr_vltime) {
lt6_tmp.ia6t_vltime = new->ndpr_vltime;
} else if (storedlifetime <= TWOHOUR
#if 0
&& new->ndpr_vltime <= storedlifetime
#endif
) {
if (auth) {
lt6_tmp.ia6t_vltime = new->ndpr_vltime;
}
} else {
lt6_tmp.ia6t_vltime = TWOHOUR;
}
lt6_tmp.ia6t_pltime = new->ndpr_pltime;
in6_init_address_ltimes(pr, <6_tmp);
if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
if (lt6_tmp.ia6t_expire == 0 ||
lt6_tmp.ia6t_expire >
ifa6->ia6_lifetime.ia6t_expire) {
lt6_tmp.ia6t_expire =
ifa6->ia6_lifetime.ia6t_expire;
}
if (lt6_tmp.ia6t_preferred == 0 ||
lt6_tmp.ia6t_preferred >
ifa6->ia6_lifetime.ia6t_preferred) {
lt6_tmp.ia6t_preferred =
ifa6->ia6_lifetime.ia6t_preferred;
}
}
ifa6->ia6_lifetime = lt6_tmp;
}
ifnet_lock_done(ifp);
if (ia6_match == NULL && new->ndpr_vltime) {
if ((ia6 = in6_ifadd(new, NULL)) != NULL) {
pr->ndpr_refcnt++;
ia6->ia6_ndpr = pr;
#if 0
pr->ndpr_addr = new->ndpr_addr;
#endif
if (ip6_use_tempaddr) {
int e;
if ((e = in6_tmpifadd(ia6, 1)) != 0) {
nd6log((LOG_NOTICE, "prelist_update: "
"failed to create a temporary "
"address, errno=%d\n",
e));
}
}
pfxlist_onlink_check(0);
} else {
error = EADDRNOTAVAIL;
}
}
afteraddrconf:
end:
return error;
}
static struct nd_pfxrouter *
find_pfxlist_reachable_router(
struct nd_prefix *pr)
{
struct nd_pfxrouter *pfxrtr;
struct rtentry *rt;
struct llinfo_nd6 *ln;
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
pfxrtr->router->ifp, 0)) &&
(ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
ND6_IS_LLINFO_PROBREACH(ln))
break;
}
return(pfxrtr);
}
void
pfxlist_onlink_check(int nd6locked)
{
struct nd_prefix *pr;
struct in6_ifaddr *ifa;
if (nd6locked == 0)
lck_mtx_lock(nd6_mutex);
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
break;
}
if (pr) {
for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
continue;
if (pr->ndpr_raf_onlink == 0)
continue;
if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
find_pfxlist_reachable_router(pr) == NULL)
pr->ndpr_stateflags |= NDPRF_DETACHED;
if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
find_pfxlist_reachable_router(pr) != 0)
pr->ndpr_stateflags &= ~NDPRF_DETACHED;
}
} else {
for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
continue;
if (pr->ndpr_raf_onlink == 0)
continue;
if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
pr->ndpr_stateflags &= ~NDPRF_DETACHED;
}
}
for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
int e;
if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
continue;
if (pr->ndpr_raf_onlink == 0)
continue;
if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
(pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
if ((e = nd6_prefix_offlink(pr)) != 0) {
nd6log((LOG_ERR,
"pfxlist_onlink_check: failed to "
"make %s/%d offlink, errno=%d\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, e));
}
}
if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
(pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
pr->ndpr_raf_onlink) {
if ((e = nd6_prefix_onlink(pr, 0, 1)) != 0) {
nd6log((LOG_ERR,
"pfxlist_onlink_check: failed to "
"make %s/%d offlink, errno=%d\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, e));
}
}
}
for (ifa = in6_ifaddrs; ifa; ifa = ifa->ia_next) {
if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
continue;
if (ifa->ia6_ndpr == NULL) {
continue;
}
if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
break;
}
if (ifa) {
for (ifa = in6_ifaddrs; ifa; ifa = ifa->ia_next) {
if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
continue;
if (ifa->ia6_ndpr == NULL)
continue;
if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
ifa->ia6_flags &= ~IN6_IFF_DETACHED;
else
ifa->ia6_flags |= IN6_IFF_DETACHED;
}
}
else {
for (ifa = in6_ifaddrs; ifa; ifa = ifa->ia_next) {
if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
continue;
ifa->ia6_flags &= ~IN6_IFF_DETACHED;
}
}
if (nd6locked == 0)
lck_mtx_unlock(nd6_mutex);
}
int
nd6_prefix_onlink(
struct nd_prefix *pr, int rtlocked, int nd6locked)
{
struct ifaddr *ifa;
struct ifnet *ifp = pr->ndpr_ifp;
struct sockaddr_in6 mask6;
struct nd_prefix *opr;
u_long rtflags;
int error = 0;
struct rtentry *rt = NULL;
if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
nd6log((LOG_ERR,
"nd6_prefix_onlink: %s/%d is already on-link\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
return(EEXIST));
}
if (nd6locked == 0)
lck_mtx_lock(nd6_mutex);
else
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
if (opr == pr)
continue;
if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
continue;
if (opr->ndpr_plen == pr->ndpr_plen &&
in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
&opr->ndpr_prefix.sin6_addr,
pr->ndpr_plen)) {
if (nd6locked == 0)
lck_mtx_unlock(nd6_mutex);
return(0);
}
}
if (nd6locked == 0)
lck_mtx_unlock(nd6_mutex);
ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
IN6_IFF_NOTREADY|
IN6_IFF_ANYCAST);
if (ifa == NULL) {
ifnet_lock_exclusive(ifp);
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
{
if (ifa->ifa_addr->sa_family == AF_INET6)
break;
}
ifnet_lock_done(ifp);
}
if (ifa == NULL) {
nd6log((LOG_NOTICE,
"nd6_prefix_onlink: failed to find any ifaddr"
" to add route for a prefix(%s/%d) on %s\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, if_name(ifp)));
return(0);
}
bzero(&mask6, sizeof(mask6));
mask6.sin6_len = sizeof(mask6);
mask6.sin6_addr = pr->ndpr_mask;
if (rtlocked == 0)
lck_mtx_lock(rt_mtx);
rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP;
if (nd6_need_cache(ifp)) {
rtflags |= RTF_CLONING;
} else {
rtflags &= ~RTF_CLONING;
}
error = rtrequest_locked(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
ifa->ifa_addr, (struct sockaddr *)&mask6,
rtflags, &rt);
if (error == 0) {
if (rt != NULL)
nd6_rtmsg(RTM_ADD, rt);
pr->ndpr_stateflags |= NDPRF_ONLINK;
}
else {
nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
" prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
"errno = %d\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
pr->ndpr_plen, if_name(ifp),
ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
ip6_sprintf(&mask6.sin6_addr), rtflags, error));
}
if (rt != NULL)
rtunref(rt);
if (rtlocked == 0)
lck_mtx_unlock(rt_mtx);
return(error);
}
int
nd6_prefix_offlink(
struct nd_prefix *pr)
{
int error = 0;
struct ifnet *ifp = pr->ndpr_ifp;
struct nd_prefix *opr;
struct sockaddr_in6 sa6, mask6;
struct rtentry *rt = NULL;
if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
nd6log((LOG_ERR,
"nd6_prefix_offlink: %s/%d is already off-link\n",
ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
return(EEXIST);
}
bzero(&sa6, sizeof(sa6));
sa6.sin6_family = AF_INET6;
sa6.sin6_len = sizeof(sa6);
bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
sizeof(struct in6_addr));
bzero(&mask6, sizeof(mask6));
mask6.sin6_family = AF_INET6;
mask6.sin6_len = sizeof(sa6);
bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
lck_mtx_lock(rt_mtx);
error = rtrequest_locked(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
(struct sockaddr *)&mask6, 0, &rt);
if (error == 0) {
pr->ndpr_stateflags &= ~NDPRF_ONLINK;
if (rt != NULL)
nd6_rtmsg(RTM_DELETE, rt);
lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_OWNED);
for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
if (opr == pr)
continue;
if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
continue;
if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
continue;
if (opr->ndpr_plen == pr->ndpr_plen &&
in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
&opr->ndpr_prefix.sin6_addr,
pr->ndpr_plen)) {
int e;
if ((e = nd6_prefix_onlink(opr, 1, 1)) != 0) {
nd6log((LOG_ERR,
"nd6_prefix_offlink: failed to "
"recover a prefix %s/%d from %s "
"to %s (errno = %d)\n",
ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
opr->ndpr_plen, if_name(ifp),
if_name(opr->ndpr_ifp), e));
}
}
}
}
else {
nd6log((LOG_ERR,
"nd6_prefix_offlink: failed to delete route: "
"%s/%d on %s (errno = %d)\n",
ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
error));
}
if (rt != NULL) {
if (rt->rt_refcnt <= 0) {
rtref(rt);
rtfree_locked(rt);
}
}
lck_mtx_unlock(rt_mtx);
return(error);
}
static struct in6_ifaddr *
in6_ifadd(
struct nd_prefix *pr,
struct in6_addr *ifid)
{
struct ifnet *ifp = pr->ndpr_ifp;
struct ifaddr *ifa;
struct in6_aliasreq ifra;
struct in6_ifaddr *ia, *ib;
int error, plen0;
struct in6_addr mask;
int prefixlen = pr->ndpr_plen;
in6_len2mask(&mask, prefixlen);
ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
if (ifa)
ib = (struct in6_ifaddr *)ifa;
else
return NULL;
#if 0
if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
printf("in6_ifadd: link-local address not ready\n");
return NULL;
}
#endif
plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
if (prefixlen != plen0) {
nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
"(prefix=%d ifid=%d)\n",
if_name(ifp), prefixlen, 128 - plen0));
return NULL;
}
bzero(&ifra, sizeof(ifra));
strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
ifra.ifra_addr.sin6_family = AF_INET6;
ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
sizeof(ifra.ifra_addr.sin6_addr));
ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid))
ifid = &ib->ia_addr.sin6_addr;
ifra.ifra_addr.sin6_addr.s6_addr32[0]
|= (ifid->s6_addr32[0] & ~mask.s6_addr32[0]);
ifra.ifra_addr.sin6_addr.s6_addr32[1]
|= (ifid->s6_addr32[1] & ~mask.s6_addr32[1]);
ifra.ifra_addr.sin6_addr.s6_addr32[2]
|= (ifid->s6_addr32[2] & ~mask.s6_addr32[2]);
ifra.ifra_addr.sin6_addr.s6_addr32[3]
|= (ifid->s6_addr32[3] & ~mask.s6_addr32[3]);
ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
ifra.ifra_prefixmask.sin6_family = AF_INET6;
bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
sizeof(ifra.ifra_prefixmask.sin6_addr));
ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
ifra.ifra_flags |= IN6_IFF_AUTOCONF;
ifra.ifra_flags |= IN6_IFF_NOPFX;
pr->ndpr_addr = ifra.ifra_addr.sin6_addr;
if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
nd6log((LOG_ERR,
"in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
error));
return(NULL);
}
ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
in6_post_msg(ifp, KEV_INET6_NEW_RTADV_ADDR, ia);
return(ia);
}
int
in6_tmpifadd(
const struct in6_ifaddr *ia0,
int forcegen)
{
struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
struct in6_ifaddr *newia;
struct in6_aliasreq ifra;
int i, error;
int trylimit = 3;
u_int32_t randid[2];
time_t vltime0, pltime0;
struct timeval timenow;
getmicrotime(&timenow);
bzero(&ifra, sizeof(ifra));
strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
ifra.ifra_addr = ia0->ia_addr;
ifra.ifra_prefixmask = ia0->ia_prefixmask;
for (i = 0; i < 4; i++) {
ifra.ifra_addr.sin6_addr.s6_addr32[i]
&= ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
}
again:
in6_get_tmpifid(ifp, (u_int8_t *)randid,
(const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8],
forcegen);
ifra.ifra_addr.sin6_addr.s6_addr32[2]
|= (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
ifra.ifra_addr.sin6_addr.s6_addr32[3]
|= (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
if (trylimit-- == 0) {
nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find "
"a unique random IFID\n"));
return(EEXIST);
}
forcegen = 1;
goto again;
}
if (ia0->ia6_lifetime.ia6t_expire != 0) {
vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
(ia0->ia6_lifetime.ia6t_expire - timenow.tv_sec);
if (vltime0 > ip6_temp_valid_lifetime)
vltime0 = ip6_temp_valid_lifetime;
} else
vltime0 = ip6_temp_valid_lifetime;
if (ia0->ia6_lifetime.ia6t_preferred != 0) {
pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
(ia0->ia6_lifetime.ia6t_preferred - timenow.tv_sec);
if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){
pltime0 = ip6_temp_preferred_lifetime -
ip6_desync_factor;
}
} else
pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
ifra.ifra_lifetime.ia6t_vltime = vltime0;
ifra.ifra_lifetime.ia6t_pltime = pltime0;
if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
return(0);
ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0)
return(error);
newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
if (newia == NULL) {
nd6log((LOG_ERR,
"in6_tmpifadd: ifa update succeeded, but we got "
"no ifaddr\n"));
return(EINVAL);
}
newia->ia6_ndpr = ia0->ia6_ndpr;
newia->ia6_ndpr->ndpr_refcnt++;
pfxlist_onlink_check(0);
return(0);
}
int
in6_init_prefix_ltimes(struct nd_prefix *ndpr)
{
struct timeval timenow;
getmicrotime(&timenow);
if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
"(%d) is greater than valid lifetime(%d)\n",
(u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
return (EINVAL);
}
if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
ndpr->ndpr_preferred = 0;
else
ndpr->ndpr_preferred = timenow.tv_sec + ndpr->ndpr_pltime;
if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
ndpr->ndpr_expire = 0;
else
ndpr->ndpr_expire = timenow.tv_sec + ndpr->ndpr_vltime;
return 0;
}
static void
in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
{
struct timeval timenow;
getmicrotime(&timenow);
if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
lt6->ia6t_expire = 0;
else {
lt6->ia6t_expire = timenow.tv_sec;
lt6->ia6t_expire += lt6->ia6t_vltime;
}
if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
lt6->ia6t_preferred = 0;
else {
lt6->ia6t_preferred = timenow.tv_sec;
lt6->ia6t_preferred += lt6->ia6t_pltime;
}
}
void
rt6_flush(
struct in6_addr *gateway,
struct ifnet *ifp)
{
struct radix_node_head *rnh = rt_tables[AF_INET6];
if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
return;
}
lck_mtx_lock(rt_mtx);
gateway->s6_addr16[1] = htons(ifp->if_index);
rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
lck_mtx_unlock(rt_mtx);
}
static int
rt6_deleteroute(
struct radix_node *rn,
void *arg)
{
#define SIN6(s) ((struct sockaddr_in6 *)s)
struct rtentry *rt = (struct rtentry *)rn;
struct in6_addr *gate = (struct in6_addr *)arg;
lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
return(0);
if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
return(0);
if ((rt->rt_flags & RTF_STATIC) != 0)
return(0);
if ((rt->rt_flags & RTF_HOST) == 0)
return(0);
return(rtrequest_locked(RTM_DELETE, rt_key(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
#undef SIN6
}
int
nd6_setdefaultiface(
int ifindex)
{
int error = 0;
if (ifindex < 0 || if_index < ifindex)
return(EINVAL);
lck_mtx_lock(nd6_mutex);
if (nd6_defifindex != ifindex) {
nd6_defifindex = ifindex;
if (nd6_defifindex > 0)
nd6_defifp = ifindex2ifnet[nd6_defifindex];
else
nd6_defifp = NULL;
if (TAILQ_FIRST(&nd_defrouter) == NULL)
defrouter_select();
scope6_setdefault(nd6_defifp);
}
lck_mtx_unlock(nd6_mutex);
return(error);
}