nd6_rtr.c revision 112678
162587Sitojun/* $FreeBSD: head/sys/netinet6/nd6_rtr.c 112678 2003-03-26 17:37:35Z ume $ */ 278064Sume/* $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ */ 362587Sitojun 453541Sshin/* 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 3362587Sitojun#include "opt_inet.h" 3462587Sitojun#include "opt_inet6.h" 3562587Sitojun 3653541Sshin#include <sys/param.h> 3753541Sshin#include <sys/systm.h> 3853541Sshin#include <sys/malloc.h> 3953541Sshin#include <sys/mbuf.h> 4053541Sshin#include <sys/socket.h> 4153541Sshin#include <sys/sockio.h> 4253541Sshin#include <sys/time.h> 4378064Sume#include <sys/kernel.h> 4453541Sshin#include <sys/errno.h> 4553541Sshin#include <sys/syslog.h> 4678064Sume#include <sys/queue.h> 4753541Sshin 4853541Sshin#include <net/if.h> 4953541Sshin#include <net/if_types.h> 5053541Sshin#include <net/if_dl.h> 5153541Sshin#include <net/route.h> 5253541Sshin#include <net/radix.h> 5353541Sshin 5453541Sshin#include <netinet/in.h> 5553541Sshin#include <netinet6/in6_var.h> 5678064Sume#include <netinet6/in6_ifattach.h> 5762587Sitojun#include <netinet/ip6.h> 5853541Sshin#include <netinet6/ip6_var.h> 5953541Sshin#include <netinet6/nd6.h> 6062587Sitojun#include <netinet/icmp6.h> 6162587Sitojun#include <netinet6/scope6_var.h> 6253541Sshin 6353541Sshin#include <net/net_osdep.h> 6453541Sshin 6562587Sitojun#define SDL(s) ((struct sockaddr_dl *)s) 6653541Sshin 6762587Sitojunstatic struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *)); 6878064Sumestatic struct in6_ifaddr *in6_ifadd __P((struct nd_prefix *, 6978064Sume struct in6_addr *)); 7062587Sitojunstatic struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 7178064Sume struct nd_defrouter *)); 7262587Sitojunstatic void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *)); 7362587Sitojunstatic void pfxrtr_del __P((struct nd_pfxrouter *)); 7462587Sitojunstatic struct nd_pfxrouter *find_pfxlist_reachable_router 7578064Sume __P((struct nd_prefix *)); 7662587Sitojunstatic void defrouter_addifreq __P((struct ifnet *)); 7778064Sumestatic void nd6_rtmsg __P((int, struct rtentry *)); 7853541Sshin 7962587Sitojunstatic void in6_init_address_ltimes __P((struct nd_prefix *ndpr, 8078064Sume struct in6_addrlifetime *lt6)); 8153541Sshin 8262587Sitojunstatic int rt6_deleteroute __P((struct radix_node *, void *)); 8353541Sshin 8462587Sitojunextern int nd6_recalc_reachtm_interval; 8553541Sshin 8678064Sumestatic struct ifnet *nd6_defifp; 8762587Sitojunint nd6_defifindex; 8862587Sitojun 8978064Sumeint ip6_use_tempaddr = 0; 9078064Sume 9178064Sumeint ip6_desync_factor; 9278064Sumeu_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME; 9378064Sumeu_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME; 9453541Sshin/* 9578064Sume * shorter lifetimes for debugging purposes. 9678064Sumeint ip6_temp_preferred_lifetime = 800; 9778064Sumestatic int ip6_temp_valid_lifetime = 1800; 9878064Sume*/ 9978064Sumeint ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE; 10078064Sume 10178064Sume/* 10253541Sshin * Receive Router Solicitation Message - just for routers. 10353541Sshin * Router solicitation/advertisement is mostly managed by userland program 10453541Sshin * (rtadvd) so here we have no function like nd6_ra_output(). 10553541Sshin * 10653541Sshin * Based on RFC 2461 10753541Sshin */ 10853541Sshinvoid 10953541Sshinnd6_rs_input(m, off, icmp6len) 11053541Sshin struct mbuf *m; 11153541Sshin int off, icmp6len; 11253541Sshin{ 11353541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 11453541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 11562587Sitojun struct nd_router_solicit *nd_rs; 11653541Sshin struct in6_addr saddr6 = ip6->ip6_src; 11762587Sitojun#if 0 11862587Sitojun struct in6_addr daddr6 = ip6->ip6_dst; 11962587Sitojun#endif 12053541Sshin char *lladdr = NULL; 12153541Sshin int lladdrlen = 0; 12262587Sitojun#if 0 12362587Sitojun struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL; 12462587Sitojun struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL; 12562587Sitojun struct rtentry *rt = NULL; 12662587Sitojun int is_newentry; 12762587Sitojun#endif 12853541Sshin union nd_opts ndopts; 12953541Sshin 13053541Sshin /* If I'm not a router, ignore it. */ 13153541Sshin if (ip6_accept_rtadv != 0 || ip6_forwarding != 1) 13262587Sitojun goto freeit; 13353541Sshin 13453541Sshin /* Sanity checks */ 13553541Sshin if (ip6->ip6_hlim != 255) { 13678064Sume nd6log((LOG_ERR, 13778064Sume "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 13878064Sume ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 13978064Sume ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 14078064Sume goto bad; 14153541Sshin } 14253541Sshin 14353541Sshin /* 14453541Sshin * Don't update the neighbor cache, if src = ::. 14553541Sshin * This indicates that the src has no IP address assigned yet. 14653541Sshin */ 14753541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 14862587Sitojun goto freeit; 14962587Sitojun 15062587Sitojun#ifndef PULLDOWN_TEST 15162587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 15262587Sitojun nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 15362587Sitojun#else 15462587Sitojun IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 15562587Sitojun if (nd_rs == NULL) { 15662587Sitojun icmp6stat.icp6s_tooshort++; 15753541Sshin return; 15862587Sitojun } 15962587Sitojun#endif 16053541Sshin 16153541Sshin icmp6len -= sizeof(*nd_rs); 16253541Sshin nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 16353541Sshin if (nd6_options(&ndopts) < 0) { 16478064Sume nd6log((LOG_INFO, 16578064Sume "nd6_rs_input: invalid ND option, ignored\n")); 16678064Sume /* nd6_options have incremented stats */ 16762587Sitojun goto freeit; 16853541Sshin } 16953541Sshin 17053541Sshin if (ndopts.nd_opts_src_lladdr) { 17153541Sshin lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 17253541Sshin lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 17353541Sshin } 17453541Sshin 17553541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 17678064Sume nd6log((LOG_INFO, 17753541Sshin "nd6_rs_input: lladdrlen mismatch for %s " 17853541Sshin "(if %d, RS packet %d)\n", 17978064Sume ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 18078064Sume goto bad; 18153541Sshin } 18253541Sshin 18353541Sshin nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 18462587Sitojun 18562587Sitojun freeit: 18662587Sitojun m_freem(m); 18778064Sume return; 18878064Sume 18978064Sume bad: 19078064Sume icmp6stat.icp6s_badrs++; 19178064Sume m_freem(m); 19253541Sshin} 19353541Sshin 19453541Sshin/* 19553541Sshin * Receive Router Advertisement Message. 19653541Sshin * 19753541Sshin * Based on RFC 2461 19853541Sshin * TODO: on-link bit on prefix information 19953541Sshin * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 20053541Sshin */ 20153541Sshinvoid 20253541Sshinnd6_ra_input(m, off, icmp6len) 20353541Sshin struct mbuf *m; 20453541Sshin int off, icmp6len; 20553541Sshin{ 20653541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 20753541Sshin struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index]; 20853541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 20962587Sitojun struct nd_router_advert *nd_ra; 21053541Sshin struct in6_addr saddr6 = ip6->ip6_src; 21162587Sitojun#if 0 21262587Sitojun struct in6_addr daddr6 = ip6->ip6_dst; 21362587Sitojun int flags; /* = nd_ra->nd_ra_flags_reserved; */ 21462587Sitojun int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0); 21562587Sitojun int is_other = ((flags & ND_RA_FLAG_OTHER) != 0); 21662587Sitojun#endif 21753541Sshin union nd_opts ndopts; 21853541Sshin struct nd_defrouter *dr; 21953541Sshin 22053541Sshin if (ip6_accept_rtadv == 0) 22162587Sitojun goto freeit; 22253541Sshin 22353541Sshin if (ip6->ip6_hlim != 255) { 22478064Sume nd6log((LOG_ERR, 22578064Sume "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 22678064Sume ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 22778064Sume ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 22878064Sume goto bad; 22953541Sshin } 23053541Sshin 23153541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 23278064Sume nd6log((LOG_ERR, 23353541Sshin "nd6_ra_input: src %s is not link-local\n", 23478064Sume ip6_sprintf(&saddr6))); 23578064Sume goto bad; 23662587Sitojun } 23762587Sitojun 23862587Sitojun#ifndef PULLDOWN_TEST 23962587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 24062587Sitojun nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 24162587Sitojun#else 24262587Sitojun IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 24362587Sitojun if (nd_ra == NULL) { 24462587Sitojun icmp6stat.icp6s_tooshort++; 24553541Sshin return; 24653541Sshin } 24762587Sitojun#endif 24853541Sshin 24953541Sshin icmp6len -= sizeof(*nd_ra); 25053541Sshin nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 25153541Sshin if (nd6_options(&ndopts) < 0) { 25278064Sume nd6log((LOG_INFO, 25378064Sume "nd6_ra_input: invalid ND option, ignored\n")); 25478064Sume /* nd6_options have incremented stats */ 25562587Sitojun goto freeit; 25653541Sshin } 25753541Sshin 25853541Sshin { 25953541Sshin struct nd_defrouter dr0; 26053541Sshin u_int32_t advreachable = nd_ra->nd_ra_reachable; 26153541Sshin 26253541Sshin dr0.rtaddr = saddr6; 26353541Sshin dr0.flags = nd_ra->nd_ra_flags_reserved; 26453541Sshin dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 26553541Sshin dr0.expire = time_second + dr0.rtlifetime; 26653541Sshin dr0.ifp = ifp; 26762587Sitojun dr0.advint = 0; /* Mobile IPv6 */ 26862587Sitojun dr0.advint_expire = 0; /* Mobile IPv6 */ 26962587Sitojun dr0.advints_lost = 0; /* Mobile IPv6 */ 27053541Sshin /* unspecified or not? (RFC 2461 6.3.4) */ 27153541Sshin if (advreachable) { 27290868Smike advreachable = ntohl(advreachable); 27353541Sshin if (advreachable <= MAX_REACHABLE_TIME && 27453541Sshin ndi->basereachable != advreachable) { 27553541Sshin ndi->basereachable = advreachable; 27653541Sshin ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 27753541Sshin ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */ 27853541Sshin } 27953541Sshin } 28053541Sshin if (nd_ra->nd_ra_retransmit) 28153541Sshin ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 28253541Sshin if (nd_ra->nd_ra_curhoplimit) 28353541Sshin ndi->chlim = nd_ra->nd_ra_curhoplimit; 28453541Sshin dr = defrtrlist_update(&dr0); 28553541Sshin } 28653541Sshin 28753541Sshin /* 28853541Sshin * prefix 28953541Sshin */ 29053541Sshin if (ndopts.nd_opts_pi) { 29153541Sshin struct nd_opt_hdr *pt; 29278064Sume struct nd_opt_prefix_info *pi = NULL; 29353541Sshin struct nd_prefix pr; 29453541Sshin 29553541Sshin for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 29653541Sshin pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 29753541Sshin pt = (struct nd_opt_hdr *)((caddr_t)pt + 29853541Sshin (pt->nd_opt_len << 3))) { 29953541Sshin if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 30053541Sshin continue; 30153541Sshin pi = (struct nd_opt_prefix_info *)pt; 30253541Sshin 30353541Sshin if (pi->nd_opt_pi_len != 4) { 30478064Sume nd6log((LOG_INFO, 30578064Sume "nd6_ra_input: invalid option " 30678064Sume "len %d for prefix information option, " 30778064Sume "ignored\n", pi->nd_opt_pi_len)); 30853541Sshin continue; 30953541Sshin } 31053541Sshin 31153541Sshin if (128 < pi->nd_opt_pi_prefix_len) { 31278064Sume nd6log((LOG_INFO, 31378064Sume "nd6_ra_input: invalid prefix " 31478064Sume "len %d for prefix information option, " 31578064Sume "ignored\n", pi->nd_opt_pi_prefix_len)); 31653541Sshin continue; 31753541Sshin } 31853541Sshin 31953541Sshin if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 32053541Sshin || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 32178064Sume nd6log((LOG_INFO, 32278064Sume "nd6_ra_input: invalid prefix " 32378064Sume "%s, ignored\n", 32478064Sume ip6_sprintf(&pi->nd_opt_pi_prefix))); 32553541Sshin continue; 32653541Sshin } 32753541Sshin 32853541Sshin /* aggregatable unicast address, rfc2374 */ 32953541Sshin if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20 33053541Sshin && pi->nd_opt_pi_prefix_len != 64) { 33178064Sume nd6log((LOG_INFO, 33278064Sume "nd6_ra_input: invalid prefixlen " 33378064Sume "%d for rfc2374 prefix %s, ignored\n", 33478064Sume pi->nd_opt_pi_prefix_len, 33578064Sume ip6_sprintf(&pi->nd_opt_pi_prefix))); 33653541Sshin continue; 33753541Sshin } 33853541Sshin 33953541Sshin bzero(&pr, sizeof(pr)); 34053541Sshin pr.ndpr_prefix.sin6_family = AF_INET6; 34153541Sshin pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 34253541Sshin pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 34353541Sshin pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 34453541Sshin 34553541Sshin pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 34653541Sshin ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 34753541Sshin pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 34853541Sshin ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 34953541Sshin pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 35053541Sshin pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 35153541Sshin pr.ndpr_pltime = 35253541Sshin ntohl(pi->nd_opt_pi_preferred_time); 35353541Sshin 35453541Sshin if (in6_init_prefix_ltimes(&pr)) 35553541Sshin continue; /* prefix lifetime init failed */ 35653541Sshin 35753541Sshin (void)prelist_update(&pr, dr, m); 35853541Sshin } 35953541Sshin } 36053541Sshin 36153541Sshin /* 36253541Sshin * MTU 36353541Sshin */ 36453541Sshin if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 36553541Sshin u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 36653541Sshin 36753541Sshin /* lower bound */ 36853541Sshin if (mtu < IPV6_MMTU) { 36978064Sume nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 37053541Sshin "mtu=%d sent from %s, ignoring\n", 37178064Sume mtu, ip6_sprintf(&ip6->ip6_src))); 37253541Sshin goto skip; 37353541Sshin } 37453541Sshin 37553541Sshin /* upper bound */ 37653541Sshin if (ndi->maxmtu) { 37753541Sshin if (mtu <= ndi->maxmtu) { 37853541Sshin int change = (ndi->linkmtu != mtu); 37953541Sshin 38053541Sshin ndi->linkmtu = mtu; 38153541Sshin if (change) /* in6_maxmtu may change */ 38253541Sshin in6_setmaxmtu(); 38353541Sshin } else { 38478064Sume nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 38553541Sshin "mtu=%d sent from %s; " 38653541Sshin "exceeds maxmtu %d, ignoring\n", 38753541Sshin mtu, ip6_sprintf(&ip6->ip6_src), 38878064Sume ndi->maxmtu)); 38953541Sshin } 39053541Sshin } else { 39178064Sume nd6log((LOG_INFO, "nd6_ra_input: mtu option " 39253541Sshin "mtu=%d sent from %s; maxmtu unknown, " 39353541Sshin "ignoring\n", 39478064Sume mtu, ip6_sprintf(&ip6->ip6_src))); 39553541Sshin } 39653541Sshin } 39753541Sshin 39853541Sshin skip: 39953541Sshin 40053541Sshin /* 40195023Ssuz * Source link layer address 40253541Sshin */ 40353541Sshin { 40453541Sshin char *lladdr = NULL; 40553541Sshin int lladdrlen = 0; 40653541Sshin 40753541Sshin if (ndopts.nd_opts_src_lladdr) { 40853541Sshin lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 40953541Sshin lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 41053541Sshin } 41153541Sshin 41253541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 41378064Sume nd6log((LOG_INFO, 41453541Sshin "nd6_ra_input: lladdrlen mismatch for %s " 41553541Sshin "(if %d, RA packet %d)\n", 41678064Sume ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 41778064Sume goto bad; 41853541Sshin } 41953541Sshin 42053541Sshin nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0); 42162587Sitojun 42262587Sitojun /* 42362587Sitojun * Installing a link-layer address might change the state of the 42462587Sitojun * router's neighbor cache, which might also affect our on-link 42562587Sitojun * detection of adveritsed prefixes. 42662587Sitojun */ 42762587Sitojun pfxlist_onlink_check(); 42853541Sshin } 42962587Sitojun 43078064Sume freeit: 43162587Sitojun m_freem(m); 43278064Sume return; 43378064Sume 43478064Sume bad: 43578064Sume icmp6stat.icp6s_badra++; 43678064Sume m_freem(m); 43753541Sshin} 43853541Sshin 43953541Sshin/* 44053541Sshin * default router list proccessing sub routines 44153541Sshin */ 44262587Sitojun 44362587Sitojun/* tell the change to user processes watching the routing socket. */ 44462587Sitojunstatic void 44578064Sumend6_rtmsg(cmd, rt) 44662587Sitojun int cmd; 44762587Sitojun struct rtentry *rt; 44862587Sitojun{ 44962587Sitojun struct rt_addrinfo info; 45062587Sitojun 45162587Sitojun bzero((caddr_t)&info, sizeof(info)); 45262587Sitojun info.rti_info[RTAX_DST] = rt_key(rt); 45362587Sitojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 45462587Sitojun info.rti_info[RTAX_NETMASK] = rt_mask(rt); 45578064Sume info.rti_info[RTAX_IFP] = 45678064Sume (struct sockaddr *)TAILQ_FIRST(&rt->rt_ifp->if_addrlist); 45778064Sume info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 45862587Sitojun 45962587Sitojun rt_missmsg(cmd, &info, rt->rt_flags, 0); 46062587Sitojun} 46162587Sitojun 46253541Sshinvoid 46353541Sshindefrouter_addreq(new) 46453541Sshin struct nd_defrouter *new; 46553541Sshin{ 46653541Sshin struct sockaddr_in6 def, mask, gate; 46762587Sitojun struct rtentry *newrt = NULL; 46853541Sshin int s; 46953541Sshin 47053541Sshin Bzero(&def, sizeof(def)); 47153541Sshin Bzero(&mask, sizeof(mask)); 47253541Sshin Bzero(&gate, sizeof(gate)); 47353541Sshin 47453541Sshin def.sin6_len = mask.sin6_len = gate.sin6_len 47553541Sshin = sizeof(struct sockaddr_in6); 47653541Sshin def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 47753541Sshin gate.sin6_addr = new->rtaddr; 47853541Sshin 47953541Sshin s = splnet(); 48053541Sshin (void)rtrequest(RTM_ADD, (struct sockaddr *)&def, 48153541Sshin (struct sockaddr *)&gate, (struct sockaddr *)&mask, 48262587Sitojun RTF_GATEWAY, &newrt); 48362587Sitojun if (newrt) { 48478064Sume nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 48562587Sitojun newrt->rt_refcnt--; 48662587Sitojun } 48753541Sshin splx(s); 48853541Sshin return; 48953541Sshin} 49053541Sshin 49162587Sitojun/* Add a route to a given interface as default */ 49262587Sitojunvoid 49362587Sitojundefrouter_addifreq(ifp) 49462587Sitojun struct ifnet *ifp; 49562587Sitojun{ 49662587Sitojun struct sockaddr_in6 def, mask; 49762587Sitojun struct ifaddr *ifa; 49862587Sitojun struct rtentry *newrt = NULL; 49962587Sitojun int error, flags; 50062587Sitojun 50162587Sitojun bzero(&def, sizeof(def)); 50262587Sitojun bzero(&mask, sizeof(mask)); 50362587Sitojun 50462587Sitojun def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6); 50562587Sitojun def.sin6_family = mask.sin6_family = AF_INET6; 50662587Sitojun 50762587Sitojun /* 50862587Sitojun * Search for an ifaddr beloging to the specified interface. 50962587Sitojun * XXX: An IPv6 address are required to be assigned on the interface. 51062587Sitojun */ 51162587Sitojun if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) { 51278064Sume nd6log((LOG_ERR, /* better error? */ 51362587Sitojun "defrouter_addifreq: failed to find an ifaddr " 51462587Sitojun "to install a route to interface %s\n", 51578064Sume if_name(ifp))); 51662587Sitojun return; 51762587Sitojun } 51862587Sitojun 51962587Sitojun flags = ifa->ifa_flags; 52078064Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&def, ifa->ifa_addr, 52178064Sume (struct sockaddr *)&mask, flags, &newrt); 52278064Sume if (error != 0) { 52378064Sume nd6log((LOG_ERR, 52462587Sitojun "defrouter_addifreq: failed to install a route to " 52562587Sitojun "interface %s (errno = %d)\n", 52678064Sume if_name(ifp), error)); 52762587Sitojun 52862587Sitojun if (newrt) /* maybe unnecessary, but do it for safety */ 52962587Sitojun newrt->rt_refcnt--; 53062587Sitojun } else { 53162587Sitojun if (newrt) { 53278064Sume nd6_rtmsg(RTM_ADD, newrt); 53362587Sitojun newrt->rt_refcnt--; 53462587Sitojun } 53562587Sitojun } 53662587Sitojun} 53762587Sitojun 53853541Sshinstruct nd_defrouter * 53953541Sshindefrouter_lookup(addr, ifp) 54053541Sshin struct in6_addr *addr; 54153541Sshin struct ifnet *ifp; 54253541Sshin{ 54353541Sshin struct nd_defrouter *dr; 54453541Sshin 54562587Sitojun for (dr = TAILQ_FIRST(&nd_defrouter); dr; 54662587Sitojun dr = TAILQ_NEXT(dr, dr_entry)) { 54753541Sshin if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 54853541Sshin return(dr); 54962587Sitojun } 55053541Sshin 55153541Sshin return(NULL); /* search failed */ 55253541Sshin} 55353541Sshin 55453541Sshinvoid 55553541Sshindefrouter_delreq(dr, dofree) 55653541Sshin struct nd_defrouter *dr; 55753541Sshin int dofree; 55853541Sshin{ 55953541Sshin struct sockaddr_in6 def, mask, gate; 56062587Sitojun struct rtentry *oldrt = NULL; 56153541Sshin 56253541Sshin Bzero(&def, sizeof(def)); 56353541Sshin Bzero(&mask, sizeof(mask)); 56453541Sshin Bzero(&gate, sizeof(gate)); 56553541Sshin 56653541Sshin def.sin6_len = mask.sin6_len = gate.sin6_len 56753541Sshin = sizeof(struct sockaddr_in6); 56853541Sshin def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 56953541Sshin gate.sin6_addr = dr->rtaddr; 57053541Sshin 57153541Sshin rtrequest(RTM_DELETE, (struct sockaddr *)&def, 57253541Sshin (struct sockaddr *)&gate, 57353541Sshin (struct sockaddr *)&mask, 57462587Sitojun RTF_GATEWAY, &oldrt); 57562587Sitojun if (oldrt) { 57678064Sume nd6_rtmsg(RTM_DELETE, oldrt); 577108269Sru RTFREE(oldrt); 57862587Sitojun } 57953541Sshin 58062587Sitojun if (dofree) /* XXX: necessary? */ 58153541Sshin free(dr, M_IP6NDP); 58253541Sshin} 58353541Sshin 58453541Sshinvoid 58553541Sshindefrtrlist_del(dr) 58653541Sshin struct nd_defrouter *dr; 58753541Sshin{ 58853541Sshin struct nd_defrouter *deldr = NULL; 58953541Sshin struct nd_prefix *pr; 59053541Sshin 59153541Sshin /* 59253541Sshin * Flush all the routing table entries that use the router 59353541Sshin * as a next hop. 59453541Sshin */ 59553541Sshin if (!ip6_forwarding && ip6_accept_rtadv) { 59653541Sshin /* above is a good condition? */ 59753541Sshin rt6_flush(&dr->rtaddr, dr->ifp); 59853541Sshin } 59953541Sshin 60062587Sitojun if (dr == TAILQ_FIRST(&nd_defrouter)) 60153541Sshin deldr = dr; /* The router is primary. */ 60253541Sshin 60362587Sitojun TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 60453541Sshin 60553541Sshin /* 60653541Sshin * Also delete all the pointers to the router in each prefix lists. 60753541Sshin */ 60862587Sitojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 60953541Sshin struct nd_pfxrouter *pfxrtr; 61053541Sshin if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 61153541Sshin pfxrtr_del(pfxrtr); 61253541Sshin } 61353541Sshin pfxlist_onlink_check(); 61453541Sshin 61553541Sshin /* 61662587Sitojun * If the router is the primary one, choose a new one. 61762587Sitojun * Note that defrouter_select() will remove the current gateway 61862587Sitojun * from the routing table. 61953541Sshin */ 62053541Sshin if (deldr) 62162587Sitojun defrouter_select(); 62262587Sitojun 62353541Sshin free(dr, M_IP6NDP); 62453541Sshin} 62553541Sshin 62662587Sitojun/* 62762587Sitojun * Default Router Selection according to Section 6.3.6 of RFC 2461: 62862587Sitojun * 1) Routers that are reachable or probably reachable should be 62962587Sitojun * preferred. 63062587Sitojun * 2) When no routers on the list are known to be reachable or 63162587Sitojun * probably reachable, routers SHOULD be selected in a round-robin 63262587Sitojun * fashion. 63362587Sitojun * 3) If the Default Router List is empty, assume that all 63462587Sitojun * destinations are on-link. 63562587Sitojun */ 63662587Sitojunvoid 63762587Sitojundefrouter_select() 63862587Sitojun{ 63962587Sitojun int s = splnet(); 64062587Sitojun struct nd_defrouter *dr, anydr; 64162587Sitojun struct rtentry *rt = NULL; 64262587Sitojun struct llinfo_nd6 *ln = NULL; 64362587Sitojun 64462587Sitojun /* 64562587Sitojun * Search for a (probably) reachable router from the list. 64662587Sitojun */ 64762587Sitojun for (dr = TAILQ_FIRST(&nd_defrouter); dr; 64862587Sitojun dr = TAILQ_NEXT(dr, dr_entry)) { 64962587Sitojun if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 65062587Sitojun (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 65162587Sitojun ND6_IS_LLINFO_PROBREACH(ln)) { 65262587Sitojun /* Got it, and move it to the head */ 65362587Sitojun TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 65462587Sitojun TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry); 65562587Sitojun break; 65662587Sitojun } 65762587Sitojun } 65862587Sitojun 65962587Sitojun if ((dr = TAILQ_FIRST(&nd_defrouter))) { 66062587Sitojun /* 66162587Sitojun * De-install the previous default gateway and install 66262587Sitojun * a new one. 66362587Sitojun * Note that if there is no reachable router in the list, 66462587Sitojun * the head entry will be used anyway. 66562587Sitojun * XXX: do we have to check the current routing table entry? 66662587Sitojun */ 66762587Sitojun bzero(&anydr, sizeof(anydr)); 66862587Sitojun defrouter_delreq(&anydr, 0); 66962587Sitojun defrouter_addreq(dr); 67062587Sitojun } 67162587Sitojun else { 67262587Sitojun /* 67362587Sitojun * The Default Router List is empty, so install the default 67462587Sitojun * route to an inteface. 67562587Sitojun * XXX: The specification does not say this mechanism should 67662587Sitojun * be restricted to hosts, but this would be not useful 67762587Sitojun * (even harmful) for routers. 67862587Sitojun */ 67962587Sitojun if (!ip6_forwarding) { 68062587Sitojun /* 68162587Sitojun * De-install the current default route 68262587Sitojun * in advance. 68362587Sitojun */ 68462587Sitojun bzero(&anydr, sizeof(anydr)); 68562587Sitojun defrouter_delreq(&anydr, 0); 68662587Sitojun if (nd6_defifp) { 68762587Sitojun /* 68862587Sitojun * Install a route to the default interface 68962587Sitojun * as default route. 69078064Sume * XXX: we enable this for host only, because 69178064Sume * this may override a default route installed 69278064Sume * a user process (e.g. routing daemon) in a 69378064Sume * router case. 69462587Sitojun */ 69562587Sitojun defrouter_addifreq(nd6_defifp); 69678064Sume } else { 69778064Sume nd6log((LOG_INFO, "defrouter_select: " 69878064Sume "there's no default router and no default" 69978064Sume " interface\n")); 70062587Sitojun } 70162587Sitojun } 70262587Sitojun } 70362587Sitojun 70462587Sitojun splx(s); 70562587Sitojun return; 70662587Sitojun} 70762587Sitojun 70853541Sshinstatic struct nd_defrouter * 70953541Sshindefrtrlist_update(new) 71053541Sshin struct nd_defrouter *new; 71153541Sshin{ 71253541Sshin struct nd_defrouter *dr, *n; 71353541Sshin int s = splnet(); 71453541Sshin 71553541Sshin if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 71653541Sshin /* entry exists */ 71753541Sshin if (new->rtlifetime == 0) { 71853541Sshin defrtrlist_del(dr); 71953541Sshin dr = NULL; 72053541Sshin } else { 72153541Sshin /* override */ 72253541Sshin dr->flags = new->flags; /* xxx flag check */ 72353541Sshin dr->rtlifetime = new->rtlifetime; 72453541Sshin dr->expire = new->expire; 72553541Sshin } 72653541Sshin splx(s); 72753541Sshin return(dr); 72853541Sshin } 72953541Sshin 73053541Sshin /* entry does not exist */ 73153541Sshin if (new->rtlifetime == 0) { 73253541Sshin splx(s); 73353541Sshin return(NULL); 73453541Sshin } 73553541Sshin 73653541Sshin n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT); 73753541Sshin if (n == NULL) { 73853541Sshin splx(s); 73953541Sshin return(NULL); 74053541Sshin } 74153541Sshin bzero(n, sizeof(*n)); 74253541Sshin *n = *new; 74362587Sitojun 74462587Sitojun /* 74562587Sitojun * Insert the new router at the end of the Default Router List. 74662587Sitojun * If there is no other router, install it anyway. Otherwise, 74762587Sitojun * just continue to use the current default router. 74862587Sitojun */ 74962587Sitojun TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry); 75062587Sitojun if (TAILQ_FIRST(&nd_defrouter) == n) 75162587Sitojun defrouter_select(); 75253541Sshin splx(s); 75353541Sshin 75453541Sshin return(n); 75553541Sshin} 75653541Sshin 75753541Sshinstatic struct nd_pfxrouter * 75853541Sshinpfxrtr_lookup(pr, dr) 75953541Sshin struct nd_prefix *pr; 76053541Sshin struct nd_defrouter *dr; 76153541Sshin{ 76253541Sshin struct nd_pfxrouter *search; 76362587Sitojun 76462587Sitojun for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) { 76553541Sshin if (search->router == dr) 76653541Sshin break; 76753541Sshin } 76853541Sshin 76953541Sshin return(search); 77053541Sshin} 77153541Sshin 77253541Sshinstatic void 77353541Sshinpfxrtr_add(pr, dr) 77453541Sshin struct nd_prefix *pr; 77553541Sshin struct nd_defrouter *dr; 77653541Sshin{ 77753541Sshin struct nd_pfxrouter *new; 77853541Sshin 77953541Sshin new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 78053541Sshin if (new == NULL) 78153541Sshin return; 78253541Sshin bzero(new, sizeof(*new)); 78353541Sshin new->router = dr; 78453541Sshin 78553541Sshin LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 78653541Sshin 78753541Sshin pfxlist_onlink_check(); 78853541Sshin} 78953541Sshin 79053541Sshinstatic void 79153541Sshinpfxrtr_del(pfr) 79253541Sshin struct nd_pfxrouter *pfr; 79353541Sshin{ 79453541Sshin LIST_REMOVE(pfr, pfr_entry); 79553541Sshin free(pfr, M_IP6NDP); 79653541Sshin} 79753541Sshin 79878064Sumestruct nd_prefix * 79978064Sumend6_prefix_lookup(pr) 80053541Sshin struct nd_prefix *pr; 80153541Sshin{ 80253541Sshin struct nd_prefix *search; 80353541Sshin 80462587Sitojun for (search = nd_prefix.lh_first; search; search = search->ndpr_next) { 80553541Sshin if (pr->ndpr_ifp == search->ndpr_ifp && 80653541Sshin pr->ndpr_plen == search->ndpr_plen && 80753541Sshin in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 80853541Sshin &search->ndpr_prefix.sin6_addr, 80953541Sshin pr->ndpr_plen) 81053541Sshin ) { 81153541Sshin break; 81253541Sshin } 81353541Sshin } 81453541Sshin 81553541Sshin return(search); 81653541Sshin} 81753541Sshin 81878064Sumeint 81978064Sumend6_prelist_add(pr, dr, newp) 82078064Sume struct nd_prefix *pr, **newp; 82153541Sshin struct nd_defrouter *dr; 82253541Sshin{ 82378064Sume struct nd_prefix *new = NULL; 82453541Sshin int i, s; 82553541Sshin 82653541Sshin new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 82753541Sshin if (new == NULL) 82853541Sshin return ENOMEM; 82953541Sshin bzero(new, sizeof(*new)); 83053541Sshin *new = *pr; 83178064Sume if (newp != NULL) 83278064Sume *newp = new; 83353541Sshin 83453541Sshin /* initilization */ 83553541Sshin LIST_INIT(&new->ndpr_advrtrs); 83653541Sshin in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 83753541Sshin /* make prefix in the canonical form */ 83853541Sshin for (i = 0; i < 4; i++) 83953541Sshin new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 84053541Sshin new->ndpr_mask.s6_addr32[i]; 84153541Sshin 84253541Sshin s = splnet(); 84353541Sshin /* link ndpr_entry to nd_prefix list */ 84453541Sshin LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry); 84553541Sshin splx(s); 84653541Sshin 84778064Sume /* ND_OPT_PI_FLAG_ONLINK processing */ 84878064Sume if (new->ndpr_raf_onlink) { 84978064Sume int e; 85078064Sume 85178064Sume if ((e = nd6_prefix_onlink(new)) != 0) { 85278064Sume nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 85378064Sume "the prefix %s/%d on-link on %s (errno=%d)\n", 85478064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 85578064Sume pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 85678064Sume /* proceed anyway. XXX: is it correct? */ 85778064Sume } 85878064Sume } 85978064Sume 86062587Sitojun if (dr) { 86153541Sshin pfxrtr_add(new, dr); 86262587Sitojun } 86353541Sshin 86453541Sshin return 0; 86553541Sshin} 86653541Sshin 86753541Sshinvoid 86853541Sshinprelist_remove(pr) 86953541Sshin struct nd_prefix *pr; 87053541Sshin{ 87153541Sshin struct nd_pfxrouter *pfr, *next; 87278064Sume int e, s; 87353541Sshin 87478064Sume /* make sure to invalidate the prefix until it is really freed. */ 87578064Sume pr->ndpr_vltime = 0; 87678064Sume pr->ndpr_pltime = 0; 87778064Sume#if 0 87878064Sume /* 87978064Sume * Though these flags are now meaningless, we'd rather keep the value 88078064Sume * not to confuse users when executing "ndp -p". 88178064Sume */ 88278064Sume pr->ndpr_raf_onlink = 0; 88378064Sume pr->ndpr_raf_auto = 0; 88478064Sume#endif 88578064Sume if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 88678064Sume (e = nd6_prefix_offlink(pr)) != 0) { 88778064Sume nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 88878064Sume "on %s, errno=%d\n", 88978064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 89078064Sume pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 89178064Sume /* what should we do? */ 89278064Sume } 89378064Sume 89478064Sume if (pr->ndpr_refcnt > 0) 89578064Sume return; /* notice here? */ 89678064Sume 89753541Sshin s = splnet(); 89878064Sume 89953541Sshin /* unlink ndpr_entry from nd_prefix list */ 90053541Sshin LIST_REMOVE(pr, ndpr_entry); 90153541Sshin 90253541Sshin /* free list of routers that adversed the prefix */ 90362587Sitojun for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) { 90462587Sitojun next = pfr->pfr_next; 90553541Sshin 90653541Sshin free(pfr, M_IP6NDP); 90753541Sshin } 90878064Sume splx(s); 90978064Sume 91053541Sshin free(pr, M_IP6NDP); 91153541Sshin 91253541Sshin pfxlist_onlink_check(); 91353541Sshin} 91453541Sshin 91553541Sshinint 91653541Sshinprelist_update(new, dr, m) 91753541Sshin struct nd_prefix *new; 91853541Sshin struct nd_defrouter *dr; /* may be NULL */ 91953541Sshin struct mbuf *m; 92053541Sshin{ 92178064Sume struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 92278064Sume struct ifaddr *ifa; 92378064Sume struct ifnet *ifp = new->ndpr_ifp; 92453541Sshin struct nd_prefix *pr; 92553541Sshin int s = splnet(); 92653541Sshin int error = 0; 92778064Sume int newprefix = 0; 92853541Sshin int auth; 92978064Sume struct in6_addrlifetime lt6_tmp; 93053541Sshin 93153541Sshin auth = 0; 93253541Sshin if (m) { 93353541Sshin /* 93453541Sshin * Authenticity for NA consists authentication for 93553541Sshin * both IP header and IP datagrams, doesn't it ? 93653541Sshin */ 93753541Sshin#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 93853541Sshin auth = (m->m_flags & M_AUTHIPHDR 93953541Sshin && m->m_flags & M_AUTHIPDGM) ? 1 : 0; 94053541Sshin#endif 94153541Sshin } 94253541Sshin 94362587Sitojun 94478064Sume if ((pr = nd6_prefix_lookup(new)) != NULL) { 94578064Sume /* 94678064Sume * nd6_prefix_lookup() ensures that pr and new have the same 94778064Sume * prefix on a same interface. 94878064Sume */ 94953541Sshin 95053541Sshin /* 95178064Sume * Update prefix information. Note that the on-link (L) bit 95278064Sume * and the autonomous (A) bit should NOT be changed from 1 95378064Sume * to 0. 95453541Sshin */ 95578064Sume if (new->ndpr_raf_onlink == 1) 95678064Sume pr->ndpr_raf_onlink = 1; 95778064Sume if (new->ndpr_raf_auto == 1) 95878064Sume pr->ndpr_raf_auto = 1; 95978064Sume if (new->ndpr_raf_onlink) { 96078064Sume pr->ndpr_vltime = new->ndpr_vltime; 96178064Sume pr->ndpr_pltime = new->ndpr_pltime; 96278064Sume pr->ndpr_preferred = new->ndpr_preferred; 96378064Sume pr->ndpr_expire = new->ndpr_expire; 96478064Sume } 96553541Sshin 96678064Sume if (new->ndpr_raf_onlink && 96778064Sume (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 96878064Sume int e; 96953541Sshin 97078064Sume if ((e = nd6_prefix_onlink(pr)) != 0) { 97178064Sume nd6log((LOG_ERR, 97278064Sume "prelist_update: failed to make " 97378064Sume "the prefix %s/%d on-link on %s " 97478064Sume "(errno=%d)\n", 97578064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 97678064Sume pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 97778064Sume /* proceed anyway. XXX: is it correct? */ 97853541Sshin } 97978064Sume } 98053541Sshin 98178064Sume if (dr && pfxrtr_lookup(pr, dr) == NULL) 98278064Sume pfxrtr_add(pr, dr); 98378064Sume } else { 98478064Sume struct nd_prefix *newpr = NULL; 98553541Sshin 98678064Sume newprefix = 1; 98753541Sshin 98878064Sume if (new->ndpr_vltime == 0) 98978064Sume goto end; 99078064Sume if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 99178064Sume goto end; 99253541Sshin 99378064Sume bzero(&new->ndpr_addr, sizeof(struct in6_addr)); 99462587Sitojun 99578064Sume error = nd6_prelist_add(new, dr, &newpr); 99678064Sume if (error != 0 || newpr == NULL) { 99778064Sume nd6log((LOG_NOTICE, "prelist_update: " 99878064Sume "nd6_prelist_add failed for %s/%d on %s " 99978064Sume "errno=%d, returnpr=%p\n", 100078064Sume ip6_sprintf(&new->ndpr_prefix.sin6_addr), 100178064Sume new->ndpr_plen, if_name(new->ndpr_ifp), 100278064Sume error, newpr)); 100378064Sume goto end; /* we should just give up in this case. */ 100478064Sume } 100553541Sshin 100678064Sume /* 100778064Sume * XXX: from the ND point of view, we can ignore a prefix 100878064Sume * with the on-link bit being zero. However, we need a 100978064Sume * prefix structure for references from autoconfigured 101078064Sume * addresses. Thus, we explicitly make suret that the prefix 101178064Sume * itself expires now. 101278064Sume */ 101378064Sume if (newpr->ndpr_raf_onlink == 0) { 101478064Sume newpr->ndpr_vltime = 0; 101578064Sume newpr->ndpr_pltime = 0; 101678064Sume in6_init_prefix_ltimes(newpr); 101753541Sshin } 101853541Sshin 101978064Sume pr = newpr; 102078064Sume } 102153541Sshin 102278064Sume /* 102378064Sume * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 102478064Sume * Note that pr must be non NULL at this point. 102578064Sume */ 102662587Sitojun 102778064Sume /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 102878064Sume if (!new->ndpr_raf_auto) 102978064Sume goto afteraddrconf; 103062587Sitojun 103178064Sume /* 103278064Sume * 5.5.3 (b). the link-local prefix should have been ignored in 103378064Sume * nd6_ra_input. 103478064Sume */ 103562587Sitojun 103678064Sume /* 103778064Sume * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. 103878064Sume * This should have been done in nd6_ra_input. 103978064Sume */ 104062587Sitojun 104178064Sume /* 104278064Sume * 5.5.3 (d). If the prefix advertised does not match the prefix of an 104378064Sume * address already in the list, and the Valid Lifetime is not 0, 104478064Sume * form an address. Note that even a manually configured address 104578064Sume * should reject autoconfiguration of a new address. 104678064Sume */ 104778064Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 104878064Sume { 104978064Sume struct in6_ifaddr *ifa6; 105078064Sume int ifa_plen; 105178064Sume u_int32_t storedlifetime; 105253541Sshin 105378064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 105478064Sume continue; 105553541Sshin 105678064Sume ifa6 = (struct in6_ifaddr *)ifa; 105753541Sshin 105853541Sshin /* 105978064Sume * Spec is not clear here, but I believe we should concentrate 106078064Sume * on unicast (i.e. not anycast) addresses. 106178064Sume * XXX: other ia6_flags? detached or duplicated? 106253541Sshin */ 106378064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 106478064Sume continue; 106578064Sume 106678064Sume ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL); 106778064Sume if (ifa_plen != new->ndpr_plen || 106878064Sume !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr, 106978064Sume &new->ndpr_prefix.sin6_addr, 107078064Sume ifa_plen)) 107178064Sume continue; 107253541Sshin 107378064Sume if (ia6_match == NULL) /* remember the first one */ 107478064Sume ia6_match = ifa6; 107578064Sume 107678064Sume if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 107778064Sume continue; 107878064Sume 107978064Sume /* 108078064Sume * An already autoconfigured address matched. Now that we 108178064Sume * are sure there is at least one matched address, we can 108278064Sume * proceed to 5.5.3. (e): update the lifetimes according to the 108378064Sume * "two hours" rule and the privacy extension. 108478064Sume */ 108578064Sume#define TWOHOUR (120*60) 108678064Sume lt6_tmp = ifa6->ia6_lifetime; 108778064Sume 1088112678Sume if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1089112678Sume storedlifetime = ND6_INFINITE_LIFETIME; 1090112678Sume else if (IFA6_IS_INVALID(ifa6)) 1091112678Sume storedlifetime = 0; 1092112678Sume else 1093112678Sume storedlifetime = lt6_tmp.ia6t_expire - time_second; 109478064Sume 1095112678Sume /* when not updating, keep the current stored lifetime. */ 1096112678Sume lt6_tmp.ia6t_vltime = storedlifetime; 1097112678Sume 109878064Sume if (TWOHOUR < new->ndpr_vltime || 109978064Sume storedlifetime < new->ndpr_vltime) { 110078064Sume lt6_tmp.ia6t_vltime = new->ndpr_vltime; 110178064Sume } else if (storedlifetime <= TWOHOUR 110278064Sume#if 0 110378064Sume /* 110478064Sume * This condition is logically redundant, so we just 110578064Sume * omit it. 110678064Sume * See IPng 6712, 6717, and 6721. 110778064Sume */ 110878064Sume && new->ndpr_vltime <= storedlifetime 110978064Sume#endif 111078064Sume ) { 111178064Sume if (auth) { 111278064Sume lt6_tmp.ia6t_vltime = new->ndpr_vltime; 111378064Sume } 111478064Sume } else { 111578064Sume /* 111678064Sume * new->ndpr_vltime <= TWOHOUR && 111778064Sume * TWOHOUR < storedlifetime 111878064Sume */ 111978064Sume lt6_tmp.ia6t_vltime = TWOHOUR; 112053541Sshin } 112153541Sshin 112278064Sume /* The 2 hour rule is not imposed for preferred lifetime. */ 112378064Sume lt6_tmp.ia6t_pltime = new->ndpr_pltime; 112453541Sshin 112578064Sume in6_init_address_ltimes(pr, <6_tmp); 112653541Sshin 112778064Sume /* 112878064Sume * When adjusting the lifetimes of an existing temporary 112978064Sume * address, only lower the lifetimes. 113078064Sume * RFC 3041 3.3. (1). 113178064Sume * XXX: how should we modify ia6t_[pv]ltime? 113278064Sume */ 113378064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 113478064Sume if (lt6_tmp.ia6t_expire == 0 || /* no expire */ 113578064Sume lt6_tmp.ia6t_expire > 113678064Sume ifa6->ia6_lifetime.ia6t_expire) { 113778064Sume lt6_tmp.ia6t_expire = 113878064Sume ifa6->ia6_lifetime.ia6t_expire; 113978064Sume } 114078064Sume if (lt6_tmp.ia6t_preferred == 0 || /* no expire */ 114178064Sume lt6_tmp.ia6t_preferred > 114278064Sume ifa6->ia6_lifetime.ia6t_preferred) { 114378064Sume lt6_tmp.ia6t_preferred = 114478064Sume ifa6->ia6_lifetime.ia6t_preferred; 114578064Sume } 114678064Sume } 114778064Sume 114878064Sume ifa6->ia6_lifetime = lt6_tmp; 114953541Sshin } 115078064Sume if (ia6_match == NULL && new->ndpr_vltime) { 115178064Sume /* 115278064Sume * No address matched and the valid lifetime is non-zero. 115378064Sume * Create a new address. 115478064Sume */ 115578064Sume if ((ia6 = in6_ifadd(new, NULL)) != NULL) { 115678064Sume /* 115778064Sume * note that we should use pr (not new) for reference. 115878064Sume */ 115978064Sume pr->ndpr_refcnt++; 116078064Sume ia6->ia6_ndpr = pr; 116153541Sshin 116278064Sume#if 0 116378064Sume /* XXXYYY Don't do this, according to Jinmei. */ 116478064Sume pr->ndpr_addr = new->ndpr_addr; 116578064Sume#endif 116678064Sume 116778064Sume /* 116878064Sume * RFC 3041 3.3 (2). 116978064Sume * When a new public address is created as described 117078064Sume * in RFC2462, also create a new temporary address. 117178064Sume * 117278064Sume * RFC 3041 3.5. 117378064Sume * When an interface connects to a new link, a new 117478064Sume * randomized interface identifier should be generated 117578064Sume * immediately together with a new set of temporary 117678064Sume * addresses. Thus, we specifiy 1 as the 2nd arg of 117778064Sume * in6_tmpifadd(). 117878064Sume */ 117978064Sume if (ip6_use_tempaddr) { 118078064Sume int e; 118178064Sume if ((e = in6_tmpifadd(ia6, 1)) != 0) { 118278064Sume nd6log((LOG_NOTICE, "prelist_update: " 118378064Sume "failed to create a temporary " 118478064Sume "address, errno=%d\n", 118578064Sume e)); 118678064Sume } 118778064Sume } 118878064Sume 118978064Sume /* 119078064Sume * A newly added address might affect the status 119178064Sume * of other addresses, so we check and update it. 119278064Sume * XXX: what if address duplication happens? 119378064Sume */ 119478064Sume pfxlist_onlink_check(); 119578064Sume } else { 119678064Sume /* just set an error. do not bark here. */ 119778064Sume error = EADDRNOTAVAIL; /* XXX: might be unused. */ 119878064Sume } 119978064Sume } 120078064Sume 120178064Sume afteraddrconf: 120278064Sume 120353541Sshin end: 120453541Sshin splx(s); 120553541Sshin return error; 120653541Sshin} 120753541Sshin 120853541Sshin/* 120962587Sitojun * A supplement function used in the on-link detection below; 121062587Sitojun * detect if a given prefix has a (probably) reachable advertising router. 121162587Sitojun * XXX: lengthy function name... 121262587Sitojun */ 121378064Sumestatic struct nd_pfxrouter * 121462587Sitojunfind_pfxlist_reachable_router(pr) 121562587Sitojun struct nd_prefix *pr; 121662587Sitojun{ 121762587Sitojun struct nd_pfxrouter *pfxrtr; 121862587Sitojun struct rtentry *rt; 121962587Sitojun struct llinfo_nd6 *ln; 122062587Sitojun 122162587Sitojun for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr; 122262587Sitojun pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) { 122362587Sitojun if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0, 122462587Sitojun pfxrtr->router->ifp)) && 122562587Sitojun (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 122662587Sitojun ND6_IS_LLINFO_PROBREACH(ln)) 122762587Sitojun break; /* found */ 122862587Sitojun } 122962587Sitojun 123062587Sitojun return(pfxrtr); 123162587Sitojun 123262587Sitojun} 123362587Sitojun 123462587Sitojun/* 123553541Sshin * Check if each prefix in the prefix list has at least one available router 123678064Sume * that advertised the prefix (a router is "available" if its neighbor cache 123778064Sume * entry is reachable or probably reachable). 123862587Sitojun * If the check fails, the prefix may be off-link, because, for example, 123953541Sshin * we have moved from the network but the lifetime of the prefix has not 124078064Sume * expired yet. So we should not use the prefix if there is another prefix 124178064Sume * that has an available router. 124278064Sume * But, if there is no prefix that has an available router, we still regards 124378064Sume * all the prefixes as on-link. This is because we can't tell if all the 124453541Sshin * routers are simply dead or if we really moved from the network and there 124553541Sshin * is no router around us. 124653541Sshin */ 124762587Sitojunvoid 124853541Sshinpfxlist_onlink_check() 124953541Sshin{ 125053541Sshin struct nd_prefix *pr; 125178064Sume struct in6_ifaddr *ifa; 125253541Sshin 125362587Sitojun /* 125462587Sitojun * Check if there is a prefix that has a reachable advertising 125562587Sitojun * router. 125662587Sitojun */ 125762587Sitojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 125878064Sume if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 125953541Sshin break; 126062587Sitojun } 126153541Sshin 126253541Sshin if (pr) { 126353541Sshin /* 126462587Sitojun * There is at least one prefix that has a reachable router. 126578064Sume * Detach prefixes which have no reachable advertising 126678064Sume * router, and attach other prefixes. 126753541Sshin */ 126862587Sitojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 126978064Sume /* XXX: a link-local prefix should never be detached */ 127078064Sume if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 127178064Sume continue; 127278064Sume 127378064Sume /* 127478064Sume * we aren't interested in prefixes without the L bit 127578064Sume * set. 127678064Sume */ 127778064Sume if (pr->ndpr_raf_onlink == 0) 127878064Sume continue; 127978064Sume 128078064Sume if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 128178064Sume find_pfxlist_reachable_router(pr) == NULL) 128278064Sume pr->ndpr_stateflags |= NDPRF_DETACHED; 128378064Sume if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 128478064Sume find_pfxlist_reachable_router(pr) != 0) 128578064Sume pr->ndpr_stateflags &= ~NDPRF_DETACHED; 128653541Sshin } 128778064Sume } else { 128878064Sume /* there is no prefix that has a reachable router */ 128962587Sitojun for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 129078064Sume if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 129178064Sume continue; 129278064Sume 129378064Sume if (pr->ndpr_raf_onlink == 0) 129478064Sume continue; 129578064Sume 129678064Sume if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 129778064Sume pr->ndpr_stateflags &= ~NDPRF_DETACHED; 129853541Sshin } 129962587Sitojun } 130078064Sume 130178064Sume /* 130278064Sume * Remove each interface route associated with a (just) detached 130378064Sume * prefix, and reinstall the interface route for a (just) attached 130478064Sume * prefix. Note that all attempt of reinstallation does not 130578064Sume * necessarily success, when a same prefix is shared among multiple 130678064Sume * interfaces. Such cases will be handled in nd6_prefix_onlink, 130778064Sume * so we don't have to care about them. 130878064Sume */ 130978064Sume for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 131078064Sume int e; 131178064Sume 131278064Sume if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 131378064Sume continue; 131478064Sume 131578064Sume if (pr->ndpr_raf_onlink == 0) 131678064Sume continue; 131778064Sume 131878064Sume if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 131978064Sume (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 132078064Sume if ((e = nd6_prefix_offlink(pr)) != 0) { 132178064Sume nd6log((LOG_ERR, 132278064Sume "pfxlist_onlink_check: failed to " 132378064Sume "make %s/%d offlink, errno=%d\n", 132478064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 132578064Sume pr->ndpr_plen, e)); 132678064Sume } 132778064Sume } 132878064Sume if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 132978064Sume (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 133078064Sume pr->ndpr_raf_onlink) { 133178064Sume if ((e = nd6_prefix_onlink(pr)) != 0) { 133278064Sume nd6log((LOG_ERR, 133378064Sume "pfxlist_onlink_check: failed to " 133478064Sume "make %s/%d offlink, errno=%d\n", 133578064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 133678064Sume pr->ndpr_plen, e)); 133778064Sume } 133878064Sume } 133978064Sume } 134078064Sume 134178064Sume /* 134278064Sume * Changes on the prefix status might affect address status as well. 134378064Sume * Make sure that all addresses derived from an attached prefix are 134478064Sume * attached, and that all addresses derived from a detached prefix are 134578064Sume * detached. Note, however, that a manually configured address should 134678064Sume * always be attached. 134778064Sume * The precise detection logic is same as the one for prefixes. 134878064Sume */ 134978064Sume for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 135078064Sume if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 135178064Sume continue; 135278064Sume 135378064Sume if (ifa->ia6_ndpr == NULL) { 135478064Sume /* 135578064Sume * This can happen when we first configure the address 135678064Sume * (i.e. the address exists, but the prefix does not). 135778064Sume * XXX: complicated relationships... 135878064Sume */ 135978064Sume continue; 136078064Sume } 136178064Sume 136278064Sume if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 136378064Sume break; 136478064Sume } 136578064Sume if (ifa) { 136678064Sume for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 136778064Sume if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 136878064Sume continue; 136978064Sume 137078064Sume if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 137178064Sume continue; 137278064Sume 137378064Sume if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 137478064Sume ifa->ia6_flags &= ~IN6_IFF_DETACHED; 137578064Sume else 137678064Sume ifa->ia6_flags |= IN6_IFF_DETACHED; 137778064Sume } 137878064Sume } 137962587Sitojun else { 138078064Sume for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 138178064Sume if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 138278064Sume continue; 138378064Sume 138478064Sume ifa->ia6_flags &= ~IN6_IFF_DETACHED; 138578064Sume } 138653541Sshin } 138753541Sshin} 138853541Sshin 138978064Sumeint 139078064Sumend6_prefix_onlink(pr) 139153541Sshin struct nd_prefix *pr; 139253541Sshin{ 139378064Sume struct ifaddr *ifa; 139478064Sume struct ifnet *ifp = pr->ndpr_ifp; 139578064Sume struct sockaddr_in6 mask6; 139678064Sume struct nd_prefix *opr; 139778064Sume u_long rtflags; 139878064Sume int error = 0; 139978064Sume struct rtentry *rt = NULL; 140053541Sshin 140178064Sume /* sanity check */ 140278064Sume if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 140378064Sume nd6log((LOG_ERR, 140478064Sume "nd6_prefix_onlink: %s/%d is already on-link\n", 140578064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen); 140678064Sume return(EEXIST)); 140778064Sume } 140878064Sume 140953541Sshin /* 141078064Sume * Add the interface route associated with the prefix. Before 141178064Sume * installing the route, check if there's the same prefix on another 141278064Sume * interface, and the prefix has already installed the interface route. 141378064Sume * Although such a configuration is expected to be rare, we explicitly 141478064Sume * allow it. 141553541Sshin */ 141678064Sume for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 141778064Sume if (opr == pr) 141878064Sume continue; 141978064Sume 142078064Sume if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 142178064Sume continue; 142278064Sume 142378064Sume if (opr->ndpr_plen == pr->ndpr_plen && 142478064Sume in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 142578064Sume &opr->ndpr_prefix.sin6_addr, 142678064Sume pr->ndpr_plen)) 142778064Sume return(0); 142878064Sume } 142978064Sume 143078064Sume /* 143178064Sume * We prefer link-local addresses as the associated interface address. 143278064Sume */ 143378064Sume /* search for a link-local addr */ 143478064Sume ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 143578064Sume IN6_IFF_NOTREADY| 143678064Sume IN6_IFF_ANYCAST); 143778064Sume if (ifa == NULL) { 143878064Sume /* XXX: freebsd does not have ifa_ifwithaf */ 143978064Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 144078064Sume { 144178064Sume if (ifa->ifa_addr->sa_family == AF_INET6) 144278064Sume break; 144378064Sume } 144478064Sume /* should we care about ia6_flags? */ 144578064Sume } 144678064Sume if (ifa == NULL) { 144778064Sume /* 144878064Sume * This can still happen, when, for example, we receive an RA 144978064Sume * containing a prefix with the L bit set and the A bit clear, 145078064Sume * after removing all IPv6 addresses on the receiving 145178064Sume * interface. This should, of course, be rare though. 145278064Sume */ 145378064Sume nd6log((LOG_NOTICE, 145478064Sume "nd6_prefix_onlink: failed to find any ifaddr" 145578064Sume " to add route for a prefix(%s/%d) on %s\n", 145678064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 145778064Sume pr->ndpr_plen, if_name(ifp))); 145878064Sume return(0); 145978064Sume } 146078064Sume 146178064Sume /* 146278064Sume * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 146378064Sume * ifa->ifa_rtrequest = nd6_rtrequest; 146478064Sume */ 146578064Sume bzero(&mask6, sizeof(mask6)); 146678064Sume mask6.sin6_len = sizeof(mask6); 146778064Sume mask6.sin6_addr = pr->ndpr_mask; 146878064Sume rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP; 146978064Sume if (nd6_need_cache(ifp)) { 147078064Sume /* explicitly set in case ifa_flags does not set the flag. */ 147178064Sume rtflags |= RTF_CLONING; 147278064Sume } else { 147378064Sume /* 147478064Sume * explicitly clear the cloning bit in case ifa_flags sets it. 147578064Sume */ 147678064Sume rtflags &= ~RTF_CLONING; 147778064Sume } 147878064Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix, 147978064Sume ifa->ifa_addr, (struct sockaddr *)&mask6, 148078064Sume rtflags, &rt); 148178064Sume if (error == 0) { 148278064Sume if (rt != NULL) /* this should be non NULL, though */ 148378064Sume nd6_rtmsg(RTM_ADD, rt); 148478064Sume pr->ndpr_stateflags |= NDPRF_ONLINK; 148578064Sume } 148678064Sume else { 148778064Sume nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a" 148878064Sume " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx " 148978064Sume "errno = %d\n", 149078064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 149178064Sume pr->ndpr_plen, if_name(ifp), 149278064Sume ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr), 149378064Sume ip6_sprintf(&mask6.sin6_addr), rtflags, error)); 149478064Sume } 149578064Sume 149678064Sume if (rt != NULL) 149778064Sume rt->rt_refcnt--; 149878064Sume 149978064Sume return(error); 150078064Sume} 150178064Sume 150278064Sumeint 150378064Sumend6_prefix_offlink(pr) 150478064Sume struct nd_prefix *pr; 150578064Sume{ 150678064Sume int error = 0; 150778064Sume struct ifnet *ifp = pr->ndpr_ifp; 150878064Sume struct nd_prefix *opr; 150978064Sume struct sockaddr_in6 sa6, mask6; 151078064Sume struct rtentry *rt = NULL; 151178064Sume 151278064Sume /* sanity check */ 151378064Sume if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 151478064Sume nd6log((LOG_ERR, 151578064Sume "nd6_prefix_offlink: %s/%d is already off-link\n", 151678064Sume ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen)); 151778064Sume return(EEXIST); 151878064Sume } 151978064Sume 152053541Sshin bzero(&sa6, sizeof(sa6)); 152153541Sshin sa6.sin6_family = AF_INET6; 152253541Sshin sa6.sin6_len = sizeof(sa6); 152353541Sshin bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 152453541Sshin sizeof(struct in6_addr)); 152553541Sshin bzero(&mask6, sizeof(mask6)); 152653541Sshin mask6.sin6_family = AF_INET6; 152753541Sshin mask6.sin6_len = sizeof(sa6); 152853541Sshin bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 152978064Sume error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 153078064Sume (struct sockaddr *)&mask6, 0, &rt); 153178064Sume if (error == 0) { 153278064Sume pr->ndpr_stateflags &= ~NDPRF_ONLINK; 153353541Sshin 153478064Sume /* report the route deletion to the routing socket. */ 153578064Sume if (rt != NULL) 153678064Sume nd6_rtmsg(RTM_DELETE, rt); 153753541Sshin 153878064Sume /* 153978064Sume * There might be the same prefix on another interface, 154078064Sume * the prefix which could not be on-link just because we have 154178064Sume * the interface route (see comments in nd6_prefix_onlink). 154278064Sume * If there's one, try to make the prefix on-link on the 154378064Sume * interface. 154478064Sume */ 154578064Sume for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 154678064Sume if (opr == pr) 154778064Sume continue; 154853541Sshin 154978064Sume if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 155078064Sume continue; 155153541Sshin 155278064Sume /* 155378064Sume * KAME specific: detached prefixes should not be 155478064Sume * on-link. 155578064Sume */ 155678064Sume if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 155778064Sume continue; 155878064Sume 155978064Sume if (opr->ndpr_plen == pr->ndpr_plen && 156078064Sume in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 156178064Sume &opr->ndpr_prefix.sin6_addr, 156278064Sume pr->ndpr_plen)) { 156378064Sume int e; 156478064Sume 156578064Sume if ((e = nd6_prefix_onlink(opr)) != 0) { 156678064Sume nd6log((LOG_ERR, 156778064Sume "nd6_prefix_offlink: failed to " 156878064Sume "recover a prefix %s/%d from %s " 156978064Sume "to %s (errno = %d)\n", 157078064Sume ip6_sprintf(&opr->ndpr_prefix.sin6_addr), 157178064Sume opr->ndpr_plen, if_name(ifp), 157278064Sume if_name(opr->ndpr_ifp), e)); 157378064Sume } 157478064Sume } 157578064Sume } 157662587Sitojun } 157762587Sitojun else { 157878064Sume /* XXX: can we still set the NDPRF_ONLINK flag? */ 157978064Sume nd6log((LOG_ERR, 158078064Sume "nd6_prefix_offlink: failed to delete route: " 158178064Sume "%s/%d on %s (errno = %d)\n", 158278064Sume ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp), 158378064Sume error)); 158478064Sume } 158553541Sshin 1586108269Sru if (rt != NULL) 1587108269Sru RTFREE(rt); 158853541Sshin 158978064Sume return(error); 159053541Sshin} 159153541Sshin 159253541Sshinstatic struct in6_ifaddr * 159378064Sumein6_ifadd(pr, ifid) 159478064Sume struct nd_prefix *pr; 159578064Sume struct in6_addr *ifid; /* Mobile IPv6 addition */ 159653541Sshin{ 159778064Sume struct ifnet *ifp = pr->ndpr_ifp; 159853541Sshin struct ifaddr *ifa; 159978064Sume struct in6_aliasreq ifra; 160078064Sume struct in6_ifaddr *ia, *ib; 160178064Sume int error, plen0; 160253541Sshin struct in6_addr mask; 160378064Sume int prefixlen = pr->ndpr_plen; 160453541Sshin 160553541Sshin in6_len2mask(&mask, prefixlen); 160653541Sshin 160778064Sume /* 160878064Sume * find a link-local address (will be interface ID). 160978064Sume * Is it really mandatory? Theoretically, a global or a site-local 161078064Sume * address can be configured without a link-local address, if we 161178064Sume * have a unique interface identifier... 161278064Sume * 161378064Sume * it is not mandatory to have a link-local address, we can generate 161478064Sume * interface identifier on the fly. we do this because: 161578064Sume * (1) it should be the easiest way to find interface identifier. 161678064Sume * (2) RFC2462 5.4 suggesting the use of the same interface identifier 161778064Sume * for multiple addresses on a single interface, and possible shortcut 161878064Sume * of DAD. we omitted DAD for this reason in the past. 161978064Sume * (3) a user can prevent autoconfiguration of global address 162078064Sume * by removing link-local address by hand (this is partly because we 1621108533Sschweikh * don't have other way to control the use of IPv6 on an interface. 162278064Sume * this has been our design choice - cf. NRL's "ifconfig auto"). 162378064Sume * (4) it is easier to manage when an interface has addresses 162478064Sume * with the same interface identifier, than to have multiple addresses 162578064Sume * with different interface identifiers. 162678064Sume * 162778064Sume * Mobile IPv6 addition: allow for caller to specify a wished interface 162878064Sume * ID. This is to not break connections when moving addresses between 162978064Sume * interfaces. 163078064Sume */ 163162587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */ 163253541Sshin if (ifa) 163353541Sshin ib = (struct in6_ifaddr *)ifa; 163453541Sshin else 163553541Sshin return NULL; 163653541Sshin 163762587Sitojun#if 0 /* don't care link local addr state, and always do DAD */ 163862587Sitojun /* if link-local address is not eligible, do not autoconfigure. */ 163962587Sitojun if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) { 164062587Sitojun printf("in6_ifadd: link-local address not ready\n"); 164162587Sitojun return NULL; 164262587Sitojun } 164362587Sitojun#endif 164462587Sitojun 164553541Sshin /* prefixlen + ifidlen must be equal to 128 */ 164678064Sume plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 164778064Sume if (prefixlen != plen0) { 164878064Sume nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 164978064Sume "(prefix=%d ifid=%d)\n", 165078064Sume if_name(ifp), prefixlen, 128 - plen0)); 165153541Sshin return NULL; 165253541Sshin } 165353541Sshin 165453541Sshin /* make ifaddr */ 165553541Sshin 165678064Sume bzero(&ifra, sizeof(ifra)); 165778064Sume /* 165878064Sume * in6_update_ifa() does not use ifra_name, but we accurately set it 165978064Sume * for safety. 166078064Sume */ 166178064Sume strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 166278064Sume ifra.ifra_addr.sin6_family = AF_INET6; 166378064Sume ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 166478064Sume /* prefix */ 166578064Sume bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr, 166678064Sume sizeof(ifra.ifra_addr.sin6_addr)); 166778064Sume ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 166878064Sume ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 166978064Sume ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 167078064Sume ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 167153541Sshin 167253541Sshin /* interface ID */ 167378064Sume if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid)) 167478064Sume ifid = &ib->ia_addr.sin6_addr; 167578064Sume ifra.ifra_addr.sin6_addr.s6_addr32[0] 167678064Sume |= (ifid->s6_addr32[0] & ~mask.s6_addr32[0]); 167778064Sume ifra.ifra_addr.sin6_addr.s6_addr32[1] 167878064Sume |= (ifid->s6_addr32[1] & ~mask.s6_addr32[1]); 167978064Sume ifra.ifra_addr.sin6_addr.s6_addr32[2] 168078064Sume |= (ifid->s6_addr32[2] & ~mask.s6_addr32[2]); 168178064Sume ifra.ifra_addr.sin6_addr.s6_addr32[3] 168278064Sume |= (ifid->s6_addr32[3] & ~mask.s6_addr32[3]); 168378064Sume 168478064Sume /* new prefix mask. */ 168578064Sume ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 168678064Sume ifra.ifra_prefixmask.sin6_family = AF_INET6; 168778064Sume bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 168878064Sume sizeof(ifra.ifra_prefixmask.sin6_addr)); 168953541Sshin 169078064Sume /* 169178064Sume * lifetime. 169278064Sume * XXX: in6_init_address_ltimes would override these values later. 169378064Sume * We should reconsider this logic. 169478064Sume */ 169578064Sume ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 169678064Sume ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 169753541Sshin 169878064Sume /* XXX: scope zone ID? */ 169953541Sshin 170078064Sume ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 170178064Sume /* 170278064Sume * temporarily set the nopfx flag to avoid conflict. 170378064Sume * XXX: we should reconsider the entire mechanism about prefix 170478064Sume * manipulation. 170578064Sume */ 170678064Sume ifra.ifra_flags |= IN6_IFF_NOPFX; 170753541Sshin 170853541Sshin /* 170978064Sume * keep the new address, regardless of the result of in6_update_ifa. 171078064Sume * XXX: this address is now meaningless. 171178064Sume * We should reconsider its role. 171253541Sshin */ 171378064Sume pr->ndpr_addr = ifra.ifra_addr.sin6_addr; 171478064Sume 171578064Sume /* allocate ifaddr structure, link into chain, etc. */ 171678064Sume if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 171778064Sume nd6log((LOG_ERR, 171878064Sume "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 171978064Sume ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp), 172078064Sume error)); 172178064Sume return(NULL); /* ifaddr must not have been allocated. */ 172253541Sshin } 172353541Sshin 172478064Sume ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 172553541Sshin 172678064Sume return(ia); /* this must NOT be NULL. */ 172753541Sshin} 172853541Sshin 172953541Sshinint 173078064Sumein6_tmpifadd(ia0, forcegen) 173178064Sume const struct in6_ifaddr *ia0; /* corresponding public address */ 173278407Sume int forcegen; 173353541Sshin{ 173478064Sume struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 173578064Sume struct in6_ifaddr *newia; 173678064Sume struct in6_aliasreq ifra; 173778064Sume int i, error; 173878064Sume int trylimit = 3; /* XXX: adhoc value */ 173978064Sume u_int32_t randid[2]; 174078064Sume time_t vltime0, pltime0; 174153541Sshin 174278064Sume bzero(&ifra, sizeof(ifra)); 174378064Sume strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 174478064Sume ifra.ifra_addr = ia0->ia_addr; 174578064Sume /* copy prefix mask */ 174678064Sume ifra.ifra_prefixmask = ia0->ia_prefixmask; 174778064Sume /* clear the old IFID */ 174878064Sume for (i = 0; i < 4; i++) { 174978064Sume ifra.ifra_addr.sin6_addr.s6_addr32[i] 175078064Sume &= ifra.ifra_prefixmask.sin6_addr.s6_addr32[i]; 175178064Sume } 175253541Sshin 175378064Sume again: 175478064Sume in6_get_tmpifid(ifp, (u_int8_t *)randid, 175578064Sume (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], 175678064Sume forcegen); 175778064Sume ifra.ifra_addr.sin6_addr.s6_addr32[2] 175878064Sume |= (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 175978064Sume ifra.ifra_addr.sin6_addr.s6_addr32[3] 176078064Sume |= (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 176153541Sshin 176278064Sume /* 176378064Sume * If by chance the new temporary address is the same as an address 176478064Sume * already assigned to the interface, generate a new randomized 176578064Sume * interface identifier and repeat this step. 176678064Sume * RFC 3041 3.3 (4). 176778064Sume */ 176878064Sume if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) { 176978064Sume if (trylimit-- == 0) { 177078064Sume nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find " 177178064Sume "a unique random IFID\n")); 177278064Sume return(EEXIST); 177378064Sume } 177478064Sume forcegen = 1; 177578064Sume goto again; 177653541Sshin } 177753541Sshin 177878064Sume /* 177978064Sume * The Valid Lifetime is the lower of the Valid Lifetime of the 178078064Sume * public address or TEMP_VALID_LIFETIME. 178178064Sume * The Preferred Lifetime is the lower of the Preferred Lifetime 178278064Sume * of the public address or TEMP_PREFERRED_LIFETIME - 178378064Sume * DESYNC_FACTOR. 178478064Sume */ 178578064Sume if (ia0->ia6_lifetime.ia6t_expire != 0) { 178678064Sume vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 178778064Sume (ia0->ia6_lifetime.ia6t_expire - time_second); 178878064Sume if (vltime0 > ip6_temp_valid_lifetime) 178978064Sume vltime0 = ip6_temp_valid_lifetime; 179078064Sume } else 179178064Sume vltime0 = ip6_temp_valid_lifetime; 179278064Sume if (ia0->ia6_lifetime.ia6t_preferred != 0) { 179378064Sume pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 179478064Sume (ia0->ia6_lifetime.ia6t_preferred - time_second); 179578064Sume if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){ 179678064Sume pltime0 = ip6_temp_preferred_lifetime - 179778064Sume ip6_desync_factor; 179878064Sume } 179978064Sume } else 180078064Sume pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor; 180178064Sume ifra.ifra_lifetime.ia6t_vltime = vltime0; 180278064Sume ifra.ifra_lifetime.ia6t_pltime = pltime0; 180353541Sshin 180478064Sume /* 180578064Sume * A temporary address is created only if this calculated Preferred 180678064Sume * Lifetime is greater than REGEN_ADVANCE time units. 180778064Sume */ 180878064Sume if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance) 180978064Sume return(0); 181053541Sshin 181178064Sume /* XXX: scope zone ID? */ 181278064Sume 181378064Sume ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 181478064Sume 181578064Sume /* allocate ifaddr structure, link into chain, etc. */ 181678064Sume if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) 181778064Sume return(error); 181878064Sume 181978064Sume newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 182078064Sume if (newia == NULL) { /* XXX: can it happen? */ 182178064Sume nd6log((LOG_ERR, 182278064Sume "in6_tmpifadd: ifa update succeeded, but we got " 182378064Sume "no ifaddr\n")); 182478064Sume return(EINVAL); /* XXX */ 182553541Sshin } 182678064Sume newia->ia6_ndpr = ia0->ia6_ndpr; 182778064Sume newia->ia6_ndpr->ndpr_refcnt++; 182853541Sshin 182978407Sume /* 183078407Sume * A newly added address might affect the status of other addresses. 183178407Sume * XXX: when the temporary address is generated with a new public 183278407Sume * address, the onlink check is redundant. However, it would be safe 183378407Sume * to do the check explicitly everywhere a new address is generated, 183478407Sume * and, in fact, we surely need the check when we create a new 183578407Sume * temporary address due to deprecation of an old temporary address. 183678407Sume */ 183778407Sume pfxlist_onlink_check(); 183878407Sume 183978064Sume return(0); 184078064Sume} 184153541Sshin 184253541Sshinint 184353541Sshinin6_init_prefix_ltimes(struct nd_prefix *ndpr) 184453541Sshin{ 184578064Sume /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */ 184653541Sshin if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) { 184778064Sume nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime" 184853541Sshin "(%d) is greater than valid lifetime(%d)\n", 184978064Sume (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime)); 185053541Sshin return (EINVAL); 185153541Sshin } 185253541Sshin if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 185353541Sshin ndpr->ndpr_preferred = 0; 185453541Sshin else 185553541Sshin ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 185653541Sshin if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 185753541Sshin ndpr->ndpr_expire = 0; 185853541Sshin else 185953541Sshin ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 186053541Sshin 186153541Sshin return 0; 186253541Sshin} 186353541Sshin 186453541Sshinstatic void 186578064Sumein6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 186653541Sshin{ 186778064Sume /* init ia6t_expire */ 186878064Sume if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 186978064Sume lt6->ia6t_expire = 0; 187078064Sume else { 187178064Sume lt6->ia6t_expire = time_second; 187278064Sume lt6->ia6t_expire += lt6->ia6t_vltime; 187353541Sshin } 187462587Sitojun 187553541Sshin /* init ia6t_preferred */ 187653541Sshin if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 187753541Sshin lt6->ia6t_preferred = 0; 187853541Sshin else { 187953541Sshin lt6->ia6t_preferred = time_second; 188053541Sshin lt6->ia6t_preferred += lt6->ia6t_pltime; 188153541Sshin } 188253541Sshin} 188353541Sshin 188453541Sshin/* 188553541Sshin * Delete all the routing table entries that use the specified gateway. 188653541Sshin * XXX: this function causes search through all entries of routing table, so 188753541Sshin * it shouldn't be called when acting as a router. 188853541Sshin */ 188953541Sshinvoid 189053541Sshinrt6_flush(gateway, ifp) 189178064Sume struct in6_addr *gateway; 189278064Sume struct ifnet *ifp; 189353541Sshin{ 189453541Sshin struct radix_node_head *rnh = rt_tables[AF_INET6]; 189553541Sshin int s = splnet(); 189653541Sshin 189753541Sshin /* We'll care only link-local addresses */ 189853541Sshin if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 189953541Sshin splx(s); 190053541Sshin return; 190153541Sshin } 190253541Sshin /* XXX: hack for KAME's link-local address kludge */ 190353541Sshin gateway->s6_addr16[1] = htons(ifp->if_index); 190453541Sshin 1905108250Shsu RADIX_NODE_HEAD_LOCK(rnh); 190653541Sshin rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 1907108250Shsu RADIX_NODE_HEAD_UNLOCK(rnh); 190853541Sshin splx(s); 190953541Sshin} 191053541Sshin 191153541Sshinstatic int 191253541Sshinrt6_deleteroute(rn, arg) 191353541Sshin struct radix_node *rn; 191453541Sshin void *arg; 191553541Sshin{ 191653541Sshin#define SIN6(s) ((struct sockaddr_in6 *)s) 191753541Sshin struct rtentry *rt = (struct rtentry *)rn; 191853541Sshin struct in6_addr *gate = (struct in6_addr *)arg; 191953541Sshin 192053541Sshin if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 192153541Sshin return(0); 192253541Sshin 192353541Sshin if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) 192453541Sshin return(0); 192553541Sshin 192653541Sshin /* 192778064Sume * Do not delete a static route. 192878064Sume * XXX: this seems to be a bit ad-hoc. Should we consider the 192978064Sume * 'cloned' bit instead? 193078064Sume */ 193178064Sume if ((rt->rt_flags & RTF_STATIC) != 0) 193278064Sume return(0); 193378064Sume 193478064Sume /* 193553541Sshin * We delete only host route. This means, in particular, we don't 193653541Sshin * delete default route. 193753541Sshin */ 193853541Sshin if ((rt->rt_flags & RTF_HOST) == 0) 193953541Sshin return(0); 194053541Sshin 194153541Sshin return(rtrequest(RTM_DELETE, rt_key(rt), 194253541Sshin rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0)); 194353541Sshin#undef SIN6 194453541Sshin} 194562587Sitojun 194662587Sitojunint 194762587Sitojunnd6_setdefaultiface(ifindex) 194862587Sitojun int ifindex; 194962587Sitojun{ 195062587Sitojun int error = 0; 195162587Sitojun 195262587Sitojun if (ifindex < 0 || if_index < ifindex) 195362587Sitojun return(EINVAL); 195462587Sitojun 195562587Sitojun if (nd6_defifindex != ifindex) { 195662587Sitojun nd6_defifindex = ifindex; 195762587Sitojun if (nd6_defifindex > 0) 195883130Sjlemon nd6_defifp = ifnet_byindex(nd6_defifindex); 195962587Sitojun else 196062587Sitojun nd6_defifp = NULL; 196162587Sitojun 196262587Sitojun /* 196362587Sitojun * If the Default Router List is empty, install a route 196462587Sitojun * to the specified interface as default or remove the default 196562587Sitojun * route when the default interface becomes canceled. 196662587Sitojun * The check for the queue is actually redundant, but 196762587Sitojun * we do this here to avoid re-install the default route 196862587Sitojun * if the list is NOT empty. 196962587Sitojun */ 197062587Sitojun if (TAILQ_FIRST(&nd_defrouter) == NULL) 197162587Sitojun defrouter_select(); 197262587Sitojun 197362587Sitojun /* 197462587Sitojun * Our current implementation assumes one-to-one maping between 197562587Sitojun * interfaces and links, so it would be natural to use the 197662587Sitojun * default interface as the default link. 197762587Sitojun */ 197862587Sitojun scope6_setdefault(nd6_defifp); 197962587Sitojun } 198062587Sitojun 198162587Sitojun return(error); 198262587Sitojun} 1983