nd6_rtr.c revision 78064
1258945Sroberto/* $FreeBSD: head/sys/netinet6/nd6_rtr.c 78064 2001-06-11 12:39:29Z ume $ */ 2280849Scy/* $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ */ 3258945Sroberto 4258945Sroberto/* 5258945Sroberto * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6258945Sroberto * All rights reserved. 7258945Sroberto * 8258945Sroberto * Redistribution and use in source and binary forms, with or without 9258945Sroberto * modification, are permitted provided that the following conditions 10258945Sroberto * are met: 11258945Sroberto * 1. Redistributions of source code must retain the above copyright 12258945Sroberto * notice, this list of conditions and the following disclaimer. 13258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright 14258945Sroberto * notice, this list of conditions and the following disclaimer in the 15258945Sroberto * documentation and/or other materials provided with the distribution. 16258945Sroberto * 3. Neither the name of the project nor the names of its contributors 17258945Sroberto * may be used to endorse or promote products derived from this software 18280849Scy * without specific prior written permission. 19258945Sroberto * 20258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23258945Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30258945Sroberto * SUCH DAMAGE. 31258945Sroberto */ 32258945Sroberto 33258945Sroberto#include "opt_inet.h" 34280849Scy#include "opt_inet6.h" 35258945Sroberto 36258945Sroberto#include <sys/param.h> 37258945Sroberto#include <sys/systm.h> 38258945Sroberto#include <sys/malloc.h> 39280849Scy#include <sys/mbuf.h> 40258945Sroberto#include <sys/socket.h> 41258945Sroberto#include <sys/sockio.h> 42258945Sroberto#include <sys/time.h> 43258945Sroberto#include <sys/kernel.h> 44258945Sroberto#include <sys/errno.h> 45258945Sroberto#include <sys/syslog.h> 46258945Sroberto#include <sys/queue.h> 47258945Sroberto 48258945Sroberto#include <net/if.h> 49258945Sroberto#include <net/if_types.h> 50258945Sroberto#include <net/if_dl.h> 51258945Sroberto#include <net/route.h> 52258945Sroberto#include <net/radix.h> 53258945Sroberto 54258945Sroberto#include <netinet/in.h> 55258945Sroberto#include <netinet6/in6_var.h> 56258945Sroberto#include <netinet6/in6_ifattach.h> 57258945Sroberto#include <netinet/ip6.h> 58258945Sroberto#include <netinet6/ip6_var.h> 59258945Sroberto#include <netinet6/nd6.h> 60258945Sroberto#include <netinet/icmp6.h> 61258945Sroberto#include <netinet6/scope6_var.h> 62258945Sroberto 63258945Sroberto#include <net/net_osdep.h> 64258945Sroberto 65258945Sroberto#define SDL(s) ((struct sockaddr_dl *)s) 66258945Sroberto 67258945Srobertostatic struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *)); 68258945Srobertostatic struct in6_ifaddr *in6_ifadd __P((struct nd_prefix *, 69258945Sroberto struct in6_addr *)); 70258945Srobertostatic struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 71258945Sroberto struct nd_defrouter *)); 72258945Srobertostatic void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *)); 73258945Srobertostatic void pfxrtr_del __P((struct nd_pfxrouter *)); 74258945Srobertostatic struct nd_pfxrouter *find_pfxlist_reachable_router 75258945Sroberto __P((struct nd_prefix *)); 76258945Srobertostatic void defrouter_addifreq __P((struct ifnet *)); 77258945Srobertostatic void nd6_rtmsg __P((int, struct rtentry *)); 78258945Sroberto 79258945Srobertostatic void in6_init_address_ltimes __P((struct nd_prefix *ndpr, 80258945Sroberto struct in6_addrlifetime *lt6)); 81258945Sroberto 82258945Srobertostatic int rt6_deleteroute __P((struct radix_node *, void *)); 83258945Sroberto 84258945Srobertoextern int nd6_recalc_reachtm_interval; 85258945Sroberto 86258945Srobertostatic struct ifnet *nd6_defifp; 87258945Srobertoint nd6_defifindex; 88258945Sroberto 89258945Srobertoint ip6_use_tempaddr = 0; 90258945Sroberto 91258945Srobertoint ip6_desync_factor; 92258945Srobertou_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME; 93258945Srobertou_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME; 94258945Sroberto/* 95258945Sroberto * shorter lifetimes for debugging purposes. 96258945Srobertoint ip6_temp_preferred_lifetime = 800; 97258945Srobertostatic int ip6_temp_valid_lifetime = 1800; 98258945Sroberto*/ 99258945Srobertoint ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE; 100258945Sroberto 101258945Sroberto/* 102258945Sroberto * Receive Router Solicitation Message - just for routers. 103258945Sroberto * Router solicitation/advertisement is mostly managed by userland program 104258945Sroberto * (rtadvd) so here we have no function like nd6_ra_output(). 105258945Sroberto * 106258945Sroberto * Based on RFC 2461 107258945Sroberto */ 108258945Srobertovoid 109258945Srobertond6_rs_input(m, off, icmp6len) 110258945Sroberto struct mbuf *m; 111258945Sroberto int off, icmp6len; 112258945Sroberto{ 113258945Sroberto struct ifnet *ifp = m->m_pkthdr.rcvif; 114258945Sroberto struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 115258945Sroberto struct nd_router_solicit *nd_rs; 116258945Sroberto struct in6_addr saddr6 = ip6->ip6_src; 117258945Sroberto#if 0 118258945Sroberto struct in6_addr daddr6 = ip6->ip6_dst; 119258945Sroberto#endif 120258945Sroberto char *lladdr = NULL; 121258945Sroberto int lladdrlen = 0; 122258945Sroberto#if 0 123258945Sroberto struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL; 124258945Sroberto struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL; 125258945Sroberto struct rtentry *rt = NULL; 126258945Sroberto int is_newentry; 127258945Sroberto#endif 128258945Sroberto union nd_opts ndopts; 129258945Sroberto 130258945Sroberto /* If I'm not a router, ignore it. */ 131258945Sroberto if (ip6_accept_rtadv != 0 || ip6_forwarding != 1) 132258945Sroberto goto freeit; 133258945Sroberto 134258945Sroberto /* Sanity checks */ 135258945Sroberto if (ip6->ip6_hlim != 255) { 136258945Sroberto nd6log((LOG_ERR, 137258945Sroberto "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 138258945Sroberto ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 139258945Sroberto ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 140258945Sroberto goto bad; 141258945Sroberto } 142258945Sroberto 143258945Sroberto /* 144258945Sroberto * Don't update the neighbor cache, if src = ::. 145258945Sroberto * This indicates that the src has no IP address assigned yet. 146258945Sroberto */ 147258945Sroberto if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 148258945Sroberto goto freeit; 149258945Sroberto 150258945Sroberto#ifndef PULLDOWN_TEST 151258945Sroberto IP6_EXTHDR_CHECK(m, off, icmp6len,); 152258945Sroberto nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 153258945Sroberto#else 154258945Sroberto IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 155258945Sroberto if (nd_rs == NULL) { 156258945Sroberto icmp6stat.icp6s_tooshort++; 157258945Sroberto return; 158258945Sroberto } 159258945Sroberto#endif 160258945Sroberto 161258945Sroberto icmp6len -= sizeof(*nd_rs); 162258945Sroberto nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 163258945Sroberto if (nd6_options(&ndopts) < 0) { 164258945Sroberto nd6log((LOG_INFO, 165258945Sroberto "nd6_rs_input: invalid ND option, ignored\n")); 166258945Sroberto /* nd6_options have incremented stats */ 167258945Sroberto goto freeit; 168258945Sroberto } 169258945Sroberto 170258945Sroberto if (ndopts.nd_opts_src_lladdr) { 171258945Sroberto lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 172258945Sroberto lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 173258945Sroberto } 174258945Sroberto 175258945Sroberto if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 176258945Sroberto nd6log((LOG_INFO, 177258945Sroberto "nd6_rs_input: lladdrlen mismatch for %s " 178258945Sroberto "(if %d, RS packet %d)\n", 179258945Sroberto ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 180258945Sroberto goto bad; 181258945Sroberto } 182258945Sroberto 183258945Sroberto nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 184258945Sroberto 185258945Sroberto freeit: 186258945Sroberto m_freem(m); 187258945Sroberto return; 188258945Sroberto 189258945Sroberto bad: 190258945Sroberto icmp6stat.icp6s_badrs++; 191258945Sroberto m_freem(m); 192258945Sroberto} 193258945Sroberto 194258945Sroberto/* 195258945Sroberto * Receive Router Advertisement Message. 196258945Sroberto * 197258945Sroberto * Based on RFC 2461 198258945Sroberto * TODO: on-link bit on prefix information 199258945Sroberto * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 200258945Sroberto */ 201258945Srobertovoid 202258945Srobertond6_ra_input(m, off, icmp6len) 203258945Sroberto struct mbuf *m; 204258945Sroberto int off, icmp6len; 205258945Sroberto{ 206258945Sroberto struct ifnet *ifp = m->m_pkthdr.rcvif; 207258945Sroberto struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index]; 208258945Sroberto struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 209258945Sroberto struct nd_router_advert *nd_ra; 210258945Sroberto struct in6_addr saddr6 = ip6->ip6_src; 211258945Sroberto#if 0 212258945Sroberto struct in6_addr daddr6 = ip6->ip6_dst; 213258945Sroberto int flags; /* = nd_ra->nd_ra_flags_reserved; */ 214258945Sroberto int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0); 215258945Sroberto int is_other = ((flags & ND_RA_FLAG_OTHER) != 0); 216258945Sroberto#endif 217258945Sroberto union nd_opts ndopts; 218280849Scy struct nd_defrouter *dr; 219280849Scy 220280849Scy if (ip6_accept_rtadv == 0) 221258945Sroberto goto freeit; 222258945Sroberto 223258945Sroberto if (ip6->ip6_hlim != 255) { 224258945Sroberto nd6log((LOG_ERR, 225258945Sroberto "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 226258945Sroberto ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 227258945Sroberto ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 228258945Sroberto goto bad; 229258945Sroberto } 230258945Sroberto 231258945Sroberto if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 232258945Sroberto nd6log((LOG_ERR, 233258945Sroberto "nd6_ra_input: src %s is not link-local\n", 234280849Scy ip6_sprintf(&saddr6))); 235258945Sroberto goto bad; 236280849Scy } 237280849Scy 238280849Scy#ifndef PULLDOWN_TEST 239258945Sroberto IP6_EXTHDR_CHECK(m, off, icmp6len,); 240258945Sroberto nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 241258945Sroberto#else 242258945Sroberto IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 243258945Sroberto if (nd_ra == NULL) { 244258945Sroberto icmp6stat.icp6s_tooshort++; 245280849Scy return; 246280849Scy } 247280849Scy#endif 248258945Sroberto 249258945Sroberto icmp6len -= sizeof(*nd_ra); 250280849Scy nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 251258945Sroberto if (nd6_options(&ndopts) < 0) { 252258945Sroberto nd6log((LOG_INFO, 253258945Sroberto "nd6_ra_input: invalid ND option, ignored\n")); 254258945Sroberto /* nd6_options have incremented stats */ 255258945Sroberto goto freeit; 256258945Sroberto } 257258945Sroberto 258258945Sroberto { 259258945Sroberto struct nd_defrouter dr0; 260258945Sroberto u_int32_t advreachable = nd_ra->nd_ra_reachable; 261258945Sroberto 262258945Sroberto dr0.rtaddr = saddr6; 263258945Sroberto dr0.flags = nd_ra->nd_ra_flags_reserved; 264258945Sroberto dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 265258945Sroberto dr0.expire = time_second + dr0.rtlifetime; 266258945Sroberto dr0.ifp = ifp; 267258945Sroberto dr0.advint = 0; /* Mobile IPv6 */ 268258945Sroberto dr0.advint_expire = 0; /* Mobile IPv6 */ 269258945Sroberto dr0.advints_lost = 0; /* Mobile IPv6 */ 270258945Sroberto /* unspecified or not? (RFC 2461 6.3.4) */ 271258945Sroberto if (advreachable) { 272258945Sroberto NTOHL(advreachable); 273258945Sroberto if (advreachable <= MAX_REACHABLE_TIME && 274258945Sroberto ndi->basereachable != advreachable) { 275258945Sroberto ndi->basereachable = advreachable; 276258945Sroberto ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 277258945Sroberto ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */ 278258945Sroberto } 279258945Sroberto } 280258945Sroberto if (nd_ra->nd_ra_retransmit) 281258945Sroberto ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 282258945Sroberto if (nd_ra->nd_ra_curhoplimit) 283258945Sroberto ndi->chlim = nd_ra->nd_ra_curhoplimit; 284258945Sroberto dr = defrtrlist_update(&dr0); 285258945Sroberto } 286258945Sroberto 287258945Sroberto /* 288258945Sroberto * prefix 289258945Sroberto */ 290258945Sroberto if (ndopts.nd_opts_pi) { 291258945Sroberto struct nd_opt_hdr *pt; 292258945Sroberto struct nd_opt_prefix_info *pi = NULL; 293258945Sroberto struct nd_prefix pr; 294258945Sroberto 295258945Sroberto for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 296258945Sroberto pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 297258945Sroberto pt = (struct nd_opt_hdr *)((caddr_t)pt + 298258945Sroberto (pt->nd_opt_len << 3))) { 299258945Sroberto if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 300258945Sroberto continue; 301258945Sroberto pi = (struct nd_opt_prefix_info *)pt; 302258945Sroberto 303258945Sroberto if (pi->nd_opt_pi_len != 4) { 304258945Sroberto nd6log((LOG_INFO, 305258945Sroberto "nd6_ra_input: invalid option " 306258945Sroberto "len %d for prefix information option, " 307258945Sroberto "ignored\n", pi->nd_opt_pi_len)); 308258945Sroberto continue; 309258945Sroberto } 310258945Sroberto 311258945Sroberto if (128 < pi->nd_opt_pi_prefix_len) { 312258945Sroberto nd6log((LOG_INFO, 313258945Sroberto "nd6_ra_input: invalid prefix " 314258945Sroberto "len %d for prefix information option, " 315258945Sroberto "ignored\n", pi->nd_opt_pi_prefix_len)); 316258945Sroberto continue; 317258945Sroberto } 318258945Sroberto 319258945Sroberto if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 320280849Scy || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 321280849Scy nd6log((LOG_INFO, 322280849Scy "nd6_ra_input: invalid prefix " 323280849Scy "%s, ignored\n", 324280849Scy ip6_sprintf(&pi->nd_opt_pi_prefix))); 325280849Scy continue; 326258945Sroberto } 327280849Scy 328280849Scy /* aggregatable unicast address, rfc2374 */ 329280849Scy if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20 330280849Scy && pi->nd_opt_pi_prefix_len != 64) { 331280849Scy nd6log((LOG_INFO, 332280849Scy "nd6_ra_input: invalid prefixlen " 333258945Sroberto "%d for rfc2374 prefix %s, ignored\n", 334258945Sroberto pi->nd_opt_pi_prefix_len, 335258945Sroberto ip6_sprintf(&pi->nd_opt_pi_prefix))); 336258945Sroberto continue; 337258945Sroberto } 338258945Sroberto 339258945Sroberto bzero(&pr, sizeof(pr)); 340258945Sroberto pr.ndpr_prefix.sin6_family = AF_INET6; 341258945Sroberto pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 342258945Sroberto pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 343258945Sroberto pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 344258945Sroberto 345258945Sroberto pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 346258945Sroberto ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 347258945Sroberto pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 348280849Scy ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 349280849Scy pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 350280849Scy pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 351280849Scy pr.ndpr_pltime = 352280849Scy ntohl(pi->nd_opt_pi_preferred_time); 353258945Sroberto 354258945Sroberto if (in6_init_prefix_ltimes(&pr)) 355258945Sroberto continue; /* prefix lifetime init failed */ 356258945Sroberto 357258945Sroberto (void)prelist_update(&pr, dr, m); 358258945Sroberto } 359258945Sroberto } 360258945Sroberto 361258945Sroberto /* 362258945Sroberto * MTU 363258945Sroberto */ 364258945Sroberto if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 365258945Sroberto u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 366258945Sroberto 367258945Sroberto /* lower bound */ 368258945Sroberto if (mtu < IPV6_MMTU) { 369258945Sroberto nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 370258945Sroberto "mtu=%d sent from %s, ignoring\n", 371258945Sroberto mtu, ip6_sprintf(&ip6->ip6_src))); 372258945Sroberto goto skip; 373258945Sroberto } 374258945Sroberto 375258945Sroberto /* upper bound */ 376258945Sroberto if (ndi->maxmtu) { 377258945Sroberto if (mtu <= ndi->maxmtu) { 378258945Sroberto int change = (ndi->linkmtu != mtu); 379258945Sroberto 380258945Sroberto ndi->linkmtu = mtu; 381258945Sroberto if (change) /* in6_maxmtu may change */ 382258945Sroberto in6_setmaxmtu(); 383258945Sroberto } else { 384258945Sroberto nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 385258945Sroberto "mtu=%d sent from %s; " 386258945Sroberto "exceeds maxmtu %d, ignoring\n", 387258945Sroberto mtu, ip6_sprintf(&ip6->ip6_src), 388258945Sroberto ndi->maxmtu)); 389258945Sroberto } 390258945Sroberto } else { 391258945Sroberto nd6log((LOG_INFO, "nd6_ra_input: mtu option " 392258945Sroberto "mtu=%d sent from %s; maxmtu unknown, " 393258945Sroberto "ignoring\n", 394258945Sroberto mtu, ip6_sprintf(&ip6->ip6_src))); 395258945Sroberto } 396258945Sroberto } 397258945Sroberto 398258945Sroberto skip: 399258945Sroberto 400258945Sroberto /* 401280849Scy * Src linkaddress 402280849Scy */ 403280849Scy { 404280849Scy char *lladdr = NULL; 405280849Scy int lladdrlen = 0; 406280849Scy 407280849Scy if (ndopts.nd_opts_src_lladdr) { 408280849Scy lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 409280849Scy lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 410280849Scy } 411280849Scy 412280849Scy if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 413280849Scy nd6log((LOG_INFO, 414280849Scy "nd6_ra_input: lladdrlen mismatch for %s " 415280849Scy "(if %d, RA packet %d)\n", 416280849Scy ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 417280849Scy goto bad; 418258945Sroberto } 419258945Sroberto 420258945Sroberto nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0); 421258945Sroberto 422258945Sroberto /* 423258945Sroberto * Installing a link-layer address might change the state of the 424258945Sroberto * router's neighbor cache, which might also affect our on-link 425258945Sroberto * detection of adveritsed prefixes. 426258945Sroberto */ 427258945Sroberto pfxlist_onlink_check(); 428258945Sroberto } 429258945Sroberto 430258945Sroberto freeit: 431258945Sroberto m_freem(m); 432258945Sroberto return; 433258945Sroberto 434258945Sroberto bad: 435258945Sroberto icmp6stat.icp6s_badra++; 436258945Sroberto m_freem(m); 437258945Sroberto} 438258945Sroberto 439258945Sroberto/* 440258945Sroberto * default router list proccessing sub routines 441258945Sroberto */ 442258945Sroberto 443258945Sroberto/* tell the change to user processes watching the routing socket. */ 444258945Srobertostatic void 445258945Srobertond6_rtmsg(cmd, rt) 446258945Sroberto int cmd; 447258945Sroberto struct rtentry *rt; 448258945Sroberto{ 449258945Sroberto struct rt_addrinfo info; 450258945Sroberto 451258945Sroberto bzero((caddr_t)&info, sizeof(info)); 452258945Sroberto info.rti_info[RTAX_DST] = rt_key(rt); 453258945Sroberto info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 454258945Sroberto info.rti_info[RTAX_NETMASK] = rt_mask(rt); 455258945Sroberto info.rti_info[RTAX_IFP] = 456258945Sroberto (struct sockaddr *)TAILQ_FIRST(&rt->rt_ifp->if_addrlist); 457258945Sroberto info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 458258945Sroberto 459258945Sroberto rt_missmsg(cmd, &info, rt->rt_flags, 0); 460258945Sroberto} 461258945Sroberto 462258945Srobertovoid 463258945Srobertodefrouter_addreq(new) 464258945Sroberto struct nd_defrouter *new; 465258945Sroberto{ 466258945Sroberto struct sockaddr_in6 def, mask, gate; 467258945Sroberto struct rtentry *newrt = NULL; 468258945Sroberto int s; 469258945Sroberto 470258945Sroberto Bzero(&def, sizeof(def)); 471258945Sroberto Bzero(&mask, sizeof(mask)); 472258945Sroberto Bzero(&gate, sizeof(gate)); 473258945Sroberto 474258945Sroberto def.sin6_len = mask.sin6_len = gate.sin6_len 475258945Sroberto = sizeof(struct sockaddr_in6); 476258945Sroberto def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 477258945Sroberto gate.sin6_addr = new->rtaddr; 478258945Sroberto 479258945Sroberto s = splnet(); 480258945Sroberto (void)rtrequest(RTM_ADD, (struct sockaddr *)&def, 481258945Sroberto (struct sockaddr *)&gate, (struct sockaddr *)&mask, 482258945Sroberto RTF_GATEWAY, &newrt); 483258945Sroberto if (newrt) { 484258945Sroberto nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 485258945Sroberto newrt->rt_refcnt--; 486258945Sroberto } 487258945Sroberto splx(s); 488258945Sroberto return; 489258945Sroberto} 490258945Sroberto 491258945Sroberto/* Add a route to a given interface as default */ 492258945Srobertovoid 493258945Srobertodefrouter_addifreq(ifp) 494258945Sroberto struct ifnet *ifp; 495258945Sroberto{ 496280849Scy struct sockaddr_in6 def, mask; 497258945Sroberto struct ifaddr *ifa; 498258945Sroberto struct rtentry *newrt = NULL; 499258945Sroberto int error, flags; 500258945Sroberto 501258945Sroberto bzero(&def, sizeof(def)); 502258945Sroberto bzero(&mask, sizeof(mask)); 503258945Sroberto 504258945Sroberto def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6); 505258945Sroberto def.sin6_family = mask.sin6_family = AF_INET6; 506258945Sroberto 507258945Sroberto /* 508258945Sroberto * Search for an ifaddr beloging to the specified interface. 509258945Sroberto * XXX: An IPv6 address are required to be assigned on the interface. 510258945Sroberto */ 511258945Sroberto if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) { 512258945Sroberto nd6log((LOG_ERR, /* better error? */ 513258945Sroberto "defrouter_addifreq: failed to find an ifaddr " 514258945Sroberto "to install a route to interface %s\n", 515258945Sroberto if_name(ifp))); 516258945Sroberto return; 517258945Sroberto } 518258945Sroberto 519258945Sroberto flags = ifa->ifa_flags; 520258945Sroberto error = rtrequest(RTM_ADD, (struct sockaddr *)&def, ifa->ifa_addr, 521258945Sroberto (struct sockaddr *)&mask, flags, &newrt); 522258945Sroberto if (error != 0) { 523258945Sroberto nd6log((LOG_ERR, 524258945Sroberto "defrouter_addifreq: failed to install a route to " 525258945Sroberto "interface %s (errno = %d)\n", 526258945Sroberto if_name(ifp), error)); 527258945Sroberto 528258945Sroberto if (newrt) /* maybe unnecessary, but do it for safety */ 529258945Sroberto newrt->rt_refcnt--; 530258945Sroberto } else { 531258945Sroberto if (newrt) { 532258945Sroberto nd6_rtmsg(RTM_ADD, newrt); 533258945Sroberto newrt->rt_refcnt--; 534258945Sroberto } 535258945Sroberto } 536258945Sroberto} 537258945Sroberto 538258945Srobertostruct nd_defrouter * 539258945Srobertodefrouter_lookup(addr, ifp) 540258945Sroberto struct in6_addr *addr; 541258945Sroberto struct ifnet *ifp; 542258945Sroberto{ 543258945Sroberto struct nd_defrouter *dr; 544280849Scy 545280849Scy for (dr = TAILQ_FIRST(&nd_defrouter); dr; 546280849Scy dr = TAILQ_NEXT(dr, dr_entry)) { 547280849Scy if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 548280849Scy return(dr); 549280849Scy } 550280849Scy 551280849Scy return(NULL); /* search failed */ 552280849Scy} 553280849Scy 554280849Scyvoid 555280849Scydefrouter_delreq(dr, dofree) 556280849Scy struct nd_defrouter *dr; 557280849Scy int dofree; 558280849Scy{ 559280849Scy struct sockaddr_in6 def, mask, gate; 560280849Scy struct rtentry *oldrt = NULL; 561280849Scy 562280849Scy Bzero(&def, sizeof(def)); 563280849Scy Bzero(&mask, sizeof(mask)); 564280849Scy Bzero(&gate, sizeof(gate)); 565280849Scy 566280849Scy def.sin6_len = mask.sin6_len = gate.sin6_len 567280849Scy = sizeof(struct sockaddr_in6); 568280849Scy def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 569280849Scy gate.sin6_addr = dr->rtaddr; 570280849Scy 571280849Scy rtrequest(RTM_DELETE, (struct sockaddr *)&def, 572280849Scy (struct sockaddr *)&gate, 573280849Scy (struct sockaddr *)&mask, 574280849Scy RTF_GATEWAY, &oldrt); 575280849Scy if (oldrt) { 576280849Scy nd6_rtmsg(RTM_DELETE, oldrt); 577280849Scy if (oldrt->rt_refcnt <= 0) { 578280849Scy /* 579280849Scy * XXX: borrowed from the RTM_DELETE case of 580280849Scy * rtrequest(). 581280849Scy */ 582280849Scy oldrt->rt_refcnt++; 583280849Scy rtfree(oldrt); 584280849Scy } 585280849Scy } 586280849Scy 587280849Scy if (dofree) /* XXX: necessary? */ 588280849Scy free(dr, M_IP6NDP); 589280849Scy} 590280849Scy 591280849Scyvoid 592280849Scydefrtrlist_del(dr) 593280849Scy struct nd_defrouter *dr; 594280849Scy{ 595280849Scy struct nd_defrouter *deldr = NULL; 596280849Scy struct nd_prefix *pr; 597280849Scy 598280849Scy /* 599280849Scy * Flush all the routing table entries that use the router 600280849Scy * as a next hop. 601280849Scy */ 602280849Scy if (!ip6_forwarding && ip6_accept_rtadv) { 603280849Scy /* above is a good condition? */ 604280849Scy rt6_flush(&dr->rtaddr, dr->ifp); 605280849Scy } 606280849Scy 607280849Scy if (dr == TAILQ_FIRST(&nd_defrouter)) 608280849Scy deldr = dr; /* The router is primary. */ 609280849Scy 610280849Scy TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 611280849Scy 612280849Scy /* 613280849Scy * Also delete all the pointers to the router in each prefix lists. 614280849Scy */ 615280849Scy for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 616280849Scy struct nd_pfxrouter *pfxrtr; 617280849Scy if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 618280849Scy pfxrtr_del(pfxrtr); 619280849Scy } 620 pfxlist_onlink_check(); 621 622 /* 623 * If the router is the primary one, choose a new one. 624 * Note that defrouter_select() will remove the current gateway 625 * from the routing table. 626 */ 627 if (deldr) 628 defrouter_select(); 629 630 free(dr, M_IP6NDP); 631} 632 633/* 634 * Default Router Selection according to Section 6.3.6 of RFC 2461: 635 * 1) Routers that are reachable or probably reachable should be 636 * preferred. 637 * 2) When no routers on the list are known to be reachable or 638 * probably reachable, routers SHOULD be selected in a round-robin 639 * fashion. 640 * 3) If the Default Router List is empty, assume that all 641 * destinations are on-link. 642 */ 643void 644defrouter_select() 645{ 646 int s = splnet(); 647 struct nd_defrouter *dr, anydr; 648 struct rtentry *rt = NULL; 649 struct llinfo_nd6 *ln = NULL; 650 651 /* 652 * Search for a (probably) reachable router from the list. 653 */ 654 for (dr = TAILQ_FIRST(&nd_defrouter); dr; 655 dr = TAILQ_NEXT(dr, dr_entry)) { 656 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 657 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 658 ND6_IS_LLINFO_PROBREACH(ln)) { 659 /* Got it, and move it to the head */ 660 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 661 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry); 662 break; 663 } 664 } 665 666 if ((dr = TAILQ_FIRST(&nd_defrouter))) { 667 /* 668 * De-install the previous default gateway and install 669 * a new one. 670 * Note that if there is no reachable router in the list, 671 * the head entry will be used anyway. 672 * XXX: do we have to check the current routing table entry? 673 */ 674 bzero(&anydr, sizeof(anydr)); 675 defrouter_delreq(&anydr, 0); 676 defrouter_addreq(dr); 677 } 678 else { 679 /* 680 * The Default Router List is empty, so install the default 681 * route to an inteface. 682 * XXX: The specification does not say this mechanism should 683 * be restricted to hosts, but this would be not useful 684 * (even harmful) for routers. 685 */ 686 if (!ip6_forwarding) { 687 /* 688 * De-install the current default route 689 * in advance. 690 */ 691 bzero(&anydr, sizeof(anydr)); 692 defrouter_delreq(&anydr, 0); 693 if (nd6_defifp) { 694 /* 695 * Install a route to the default interface 696 * as default route. 697 * XXX: we enable this for host only, because 698 * this may override a default route installed 699 * a user process (e.g. routing daemon) in a 700 * router case. 701 */ 702 defrouter_addifreq(nd6_defifp); 703 } else { 704 nd6log((LOG_INFO, "defrouter_select: " 705 "there's no default router and no default" 706 " interface\n")); 707 } 708 } 709 } 710 711 splx(s); 712 return; 713} 714 715static struct nd_defrouter * 716defrtrlist_update(new) 717 struct nd_defrouter *new; 718{ 719 struct nd_defrouter *dr, *n; 720 int s = splnet(); 721 722 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 723 /* entry exists */ 724 if (new->rtlifetime == 0) { 725 defrtrlist_del(dr); 726 dr = NULL; 727 } else { 728 /* override */ 729 dr->flags = new->flags; /* xxx flag check */ 730 dr->rtlifetime = new->rtlifetime; 731 dr->expire = new->expire; 732 } 733 splx(s); 734 return(dr); 735 } 736 737 /* entry does not exist */ 738 if (new->rtlifetime == 0) { 739 splx(s); 740 return(NULL); 741 } 742 743 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT); 744 if (n == NULL) { 745 splx(s); 746 return(NULL); 747 } 748 bzero(n, sizeof(*n)); 749 *n = *new; 750 751 /* 752 * Insert the new router at the end of the Default Router List. 753 * If there is no other router, install it anyway. Otherwise, 754 * just continue to use the current default router. 755 */ 756 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry); 757 if (TAILQ_FIRST(&nd_defrouter) == n) 758 defrouter_select(); 759 splx(s); 760 761 return(n); 762} 763 764static struct nd_pfxrouter * 765pfxrtr_lookup(pr, dr) 766 struct nd_prefix *pr; 767 struct nd_defrouter *dr; 768{ 769 struct nd_pfxrouter *search; 770 771 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) { 772 if (search->router == dr) 773 break; 774 } 775 776 return(search); 777} 778 779static void 780pfxrtr_add(pr, dr) 781 struct nd_prefix *pr; 782 struct nd_defrouter *dr; 783{ 784 struct nd_pfxrouter *new; 785 786 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 787 if (new == NULL) 788 return; 789 bzero(new, sizeof(*new)); 790 new->router = dr; 791 792 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 793 794 pfxlist_onlink_check(); 795} 796 797static void 798pfxrtr_del(pfr) 799 struct nd_pfxrouter *pfr; 800{ 801 LIST_REMOVE(pfr, pfr_entry); 802 free(pfr, M_IP6NDP); 803} 804 805struct nd_prefix * 806nd6_prefix_lookup(pr) 807 struct nd_prefix *pr; 808{ 809 struct nd_prefix *search; 810 811 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) { 812 if (pr->ndpr_ifp == search->ndpr_ifp && 813 pr->ndpr_plen == search->ndpr_plen && 814 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 815 &search->ndpr_prefix.sin6_addr, 816 pr->ndpr_plen) 817 ) { 818 break; 819 } 820 } 821 822 return(search); 823} 824 825int 826nd6_prelist_add(pr, dr, newp) 827 struct nd_prefix *pr, **newp; 828 struct nd_defrouter *dr; 829{ 830 struct nd_prefix *new = NULL; 831 int i, s; 832 833 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 834 if (new == NULL) 835 return ENOMEM; 836 bzero(new, sizeof(*new)); 837 *new = *pr; 838 if (newp != NULL) 839 *newp = new; 840 841 /* initilization */ 842 LIST_INIT(&new->ndpr_advrtrs); 843 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 844 /* make prefix in the canonical form */ 845 for (i = 0; i < 4; i++) 846 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 847 new->ndpr_mask.s6_addr32[i]; 848 849 s = splnet(); 850 /* link ndpr_entry to nd_prefix list */ 851 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry); 852 splx(s); 853 854 /* ND_OPT_PI_FLAG_ONLINK processing */ 855 if (new->ndpr_raf_onlink) { 856 int e; 857 858 if ((e = nd6_prefix_onlink(new)) != 0) { 859 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 860 "the prefix %s/%d on-link on %s (errno=%d)\n", 861 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 862 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 863 /* proceed anyway. XXX: is it correct? */ 864 } 865 } 866 867 if (dr) { 868 pfxrtr_add(new, dr); 869 } 870 871 return 0; 872} 873 874void 875prelist_remove(pr) 876 struct nd_prefix *pr; 877{ 878 struct nd_pfxrouter *pfr, *next; 879 int e, s; 880 881 /* make sure to invalidate the prefix until it is really freed. */ 882 pr->ndpr_vltime = 0; 883 pr->ndpr_pltime = 0; 884#if 0 885 /* 886 * Though these flags are now meaningless, we'd rather keep the value 887 * not to confuse users when executing "ndp -p". 888 */ 889 pr->ndpr_raf_onlink = 0; 890 pr->ndpr_raf_auto = 0; 891#endif 892 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 893 (e = nd6_prefix_offlink(pr)) != 0) { 894 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 895 "on %s, errno=%d\n", 896 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 897 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 898 /* what should we do? */ 899 } 900 901 if (pr->ndpr_refcnt > 0) 902 return; /* notice here? */ 903 904 s = splnet(); 905 906 /* unlink ndpr_entry from nd_prefix list */ 907 LIST_REMOVE(pr, ndpr_entry); 908 909 /* free list of routers that adversed the prefix */ 910 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) { 911 next = pfr->pfr_next; 912 913 free(pfr, M_IP6NDP); 914 } 915 splx(s); 916 917 free(pr, M_IP6NDP); 918 919 pfxlist_onlink_check(); 920} 921 922int 923prelist_update(new, dr, m) 924 struct nd_prefix *new; 925 struct nd_defrouter *dr; /* may be NULL */ 926 struct mbuf *m; 927{ 928 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 929 struct ifaddr *ifa; 930 struct ifnet *ifp = new->ndpr_ifp; 931 struct nd_prefix *pr; 932 int s = splnet(); 933 int error = 0; 934 int newprefix = 0; 935 int auth; 936 struct in6_addrlifetime lt6_tmp; 937 938 auth = 0; 939 if (m) { 940 /* 941 * Authenticity for NA consists authentication for 942 * both IP header and IP datagrams, doesn't it ? 943 */ 944#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 945 auth = (m->m_flags & M_AUTHIPHDR 946 && m->m_flags & M_AUTHIPDGM) ? 1 : 0; 947#endif 948 } 949 950 951 if ((pr = nd6_prefix_lookup(new)) != NULL) { 952 /* 953 * nd6_prefix_lookup() ensures that pr and new have the same 954 * prefix on a same interface. 955 */ 956 957 /* 958 * Update prefix information. Note that the on-link (L) bit 959 * and the autonomous (A) bit should NOT be changed from 1 960 * to 0. 961 */ 962 if (new->ndpr_raf_onlink == 1) 963 pr->ndpr_raf_onlink = 1; 964 if (new->ndpr_raf_auto == 1) 965 pr->ndpr_raf_auto = 1; 966 if (new->ndpr_raf_onlink) { 967 pr->ndpr_vltime = new->ndpr_vltime; 968 pr->ndpr_pltime = new->ndpr_pltime; 969 pr->ndpr_preferred = new->ndpr_preferred; 970 pr->ndpr_expire = new->ndpr_expire; 971 } 972 973 if (new->ndpr_raf_onlink && 974 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 975 int e; 976 977 if ((e = nd6_prefix_onlink(pr)) != 0) { 978 nd6log((LOG_ERR, 979 "prelist_update: failed to make " 980 "the prefix %s/%d on-link on %s " 981 "(errno=%d)\n", 982 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 983 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 984 /* proceed anyway. XXX: is it correct? */ 985 } 986 } 987 988 if (dr && pfxrtr_lookup(pr, dr) == NULL) 989 pfxrtr_add(pr, dr); 990 } else { 991 struct nd_prefix *newpr = NULL; 992 993 newprefix = 1; 994 995 if (new->ndpr_vltime == 0) 996 goto end; 997 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 998 goto end; 999 1000 bzero(&new->ndpr_addr, sizeof(struct in6_addr)); 1001 1002 error = nd6_prelist_add(new, dr, &newpr); 1003 if (error != 0 || newpr == NULL) { 1004 nd6log((LOG_NOTICE, "prelist_update: " 1005 "nd6_prelist_add failed for %s/%d on %s " 1006 "errno=%d, returnpr=%p\n", 1007 ip6_sprintf(&new->ndpr_prefix.sin6_addr), 1008 new->ndpr_plen, if_name(new->ndpr_ifp), 1009 error, newpr)); 1010 goto end; /* we should just give up in this case. */ 1011 } 1012 1013 /* 1014 * XXX: from the ND point of view, we can ignore a prefix 1015 * with the on-link bit being zero. However, we need a 1016 * prefix structure for references from autoconfigured 1017 * addresses. Thus, we explicitly make suret that the prefix 1018 * itself expires now. 1019 */ 1020 if (newpr->ndpr_raf_onlink == 0) { 1021 newpr->ndpr_vltime = 0; 1022 newpr->ndpr_pltime = 0; 1023 in6_init_prefix_ltimes(newpr); 1024 } 1025 1026 pr = newpr; 1027 } 1028 1029 /* 1030 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1031 * Note that pr must be non NULL at this point. 1032 */ 1033 1034 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1035 if (!new->ndpr_raf_auto) 1036 goto afteraddrconf; 1037 1038 /* 1039 * 5.5.3 (b). the link-local prefix should have been ignored in 1040 * nd6_ra_input. 1041 */ 1042 1043 /* 1044 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. 1045 * This should have been done in nd6_ra_input. 1046 */ 1047 1048 /* 1049 * 5.5.3 (d). If the prefix advertised does not match the prefix of an 1050 * address already in the list, and the Valid Lifetime is not 0, 1051 * form an address. Note that even a manually configured address 1052 * should reject autoconfiguration of a new address. 1053 */ 1054 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1055 { 1056 struct in6_ifaddr *ifa6; 1057 int ifa_plen; 1058 u_int32_t storedlifetime; 1059 1060 if (ifa->ifa_addr->sa_family != AF_INET6) 1061 continue; 1062 1063 ifa6 = (struct in6_ifaddr *)ifa; 1064 1065 /* 1066 * Spec is not clear here, but I believe we should concentrate 1067 * on unicast (i.e. not anycast) addresses. 1068 * XXX: other ia6_flags? detached or duplicated? 1069 */ 1070 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1071 continue; 1072 1073 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL); 1074 if (ifa_plen != new->ndpr_plen || 1075 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr, 1076 &new->ndpr_prefix.sin6_addr, 1077 ifa_plen)) 1078 continue; 1079 1080 if (ia6_match == NULL) /* remember the first one */ 1081 ia6_match = ifa6; 1082 1083 if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1084 continue; 1085 1086 /* 1087 * An already autoconfigured address matched. Now that we 1088 * are sure there is at least one matched address, we can 1089 * proceed to 5.5.3. (e): update the lifetimes according to the 1090 * "two hours" rule and the privacy extension. 1091 */ 1092#define TWOHOUR (120*60) 1093 lt6_tmp = ifa6->ia6_lifetime; 1094 1095 storedlifetime = IFA6_IS_INVALID(ifa6) ? 0 : 1096 (lt6_tmp.ia6t_expire - time_second); 1097 1098 if (TWOHOUR < new->ndpr_vltime || 1099 storedlifetime < new->ndpr_vltime) { 1100 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1101 } else if (storedlifetime <= TWOHOUR 1102#if 0 1103 /* 1104 * This condition is logically redundant, so we just 1105 * omit it. 1106 * See IPng 6712, 6717, and 6721. 1107 */ 1108 && new->ndpr_vltime <= storedlifetime 1109#endif 1110 ) { 1111 if (auth) { 1112 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1113 } 1114 } else { 1115 /* 1116 * new->ndpr_vltime <= TWOHOUR && 1117 * TWOHOUR < storedlifetime 1118 */ 1119 lt6_tmp.ia6t_vltime = TWOHOUR; 1120 } 1121 1122 /* The 2 hour rule is not imposed for preferred lifetime. */ 1123 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1124 1125 in6_init_address_ltimes(pr, <6_tmp); 1126 1127 /* 1128 * When adjusting the lifetimes of an existing temporary 1129 * address, only lower the lifetimes. 1130 * RFC 3041 3.3. (1). 1131 * XXX: how should we modify ia6t_[pv]ltime? 1132 */ 1133 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 1134 if (lt6_tmp.ia6t_expire == 0 || /* no expire */ 1135 lt6_tmp.ia6t_expire > 1136 ifa6->ia6_lifetime.ia6t_expire) { 1137 lt6_tmp.ia6t_expire = 1138 ifa6->ia6_lifetime.ia6t_expire; 1139 } 1140 if (lt6_tmp.ia6t_preferred == 0 || /* no expire */ 1141 lt6_tmp.ia6t_preferred > 1142 ifa6->ia6_lifetime.ia6t_preferred) { 1143 lt6_tmp.ia6t_preferred = 1144 ifa6->ia6_lifetime.ia6t_preferred; 1145 } 1146 } 1147 1148 ifa6->ia6_lifetime = lt6_tmp; 1149 } 1150 if (ia6_match == NULL && new->ndpr_vltime) { 1151 /* 1152 * No address matched and the valid lifetime is non-zero. 1153 * Create a new address. 1154 */ 1155 if ((ia6 = in6_ifadd(new, NULL)) != NULL) { 1156 /* 1157 * note that we should use pr (not new) for reference. 1158 */ 1159 pr->ndpr_refcnt++; 1160 ia6->ia6_ndpr = pr; 1161 1162#if 0 1163 /* XXXYYY Don't do this, according to Jinmei. */ 1164 pr->ndpr_addr = new->ndpr_addr; 1165#endif 1166 1167 /* 1168 * RFC 3041 3.3 (2). 1169 * When a new public address is created as described 1170 * in RFC2462, also create a new temporary address. 1171 * 1172 * RFC 3041 3.5. 1173 * When an interface connects to a new link, a new 1174 * randomized interface identifier should be generated 1175 * immediately together with a new set of temporary 1176 * addresses. Thus, we specifiy 1 as the 2nd arg of 1177 * in6_tmpifadd(). 1178 */ 1179 if (ip6_use_tempaddr) { 1180 int e; 1181 if ((e = in6_tmpifadd(ia6, 1)) != 0) { 1182 nd6log((LOG_NOTICE, "prelist_update: " 1183 "failed to create a temporary " 1184 "address, errno=%d\n", 1185 e)); 1186 } 1187 } 1188 1189 /* 1190 * A newly added address might affect the status 1191 * of other addresses, so we check and update it. 1192 * XXX: what if address duplication happens? 1193 */ 1194 pfxlist_onlink_check(); 1195 } else { 1196 /* just set an error. do not bark here. */ 1197 error = EADDRNOTAVAIL; /* XXX: might be unused. */ 1198 } 1199 } 1200 1201 afteraddrconf: 1202 1203 end: 1204 splx(s); 1205 return error; 1206} 1207 1208/* 1209 * A supplement function used in the on-link detection below; 1210 * detect if a given prefix has a (probably) reachable advertising router. 1211 * XXX: lengthy function name... 1212 */ 1213static struct nd_pfxrouter * 1214find_pfxlist_reachable_router(pr) 1215 struct nd_prefix *pr; 1216{ 1217 struct nd_pfxrouter *pfxrtr; 1218 struct rtentry *rt; 1219 struct llinfo_nd6 *ln; 1220 1221 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr; 1222 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) { 1223 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0, 1224 pfxrtr->router->ifp)) && 1225 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 1226 ND6_IS_LLINFO_PROBREACH(ln)) 1227 break; /* found */ 1228 } 1229 1230 return(pfxrtr); 1231 1232} 1233 1234/* 1235 * Check if each prefix in the prefix list has at least one available router 1236 * that advertised the prefix (a router is "available" if its neighbor cache 1237 * entry is reachable or probably reachable). 1238 * If the check fails, the prefix may be off-link, because, for example, 1239 * we have moved from the network but the lifetime of the prefix has not 1240 * expired yet. So we should not use the prefix if there is another prefix 1241 * that has an available router. 1242 * But, if there is no prefix that has an available router, we still regards 1243 * all the prefixes as on-link. This is because we can't tell if all the 1244 * routers are simply dead or if we really moved from the network and there 1245 * is no router around us. 1246 */ 1247void 1248pfxlist_onlink_check() 1249{ 1250 struct nd_prefix *pr; 1251 struct in6_ifaddr *ifa; 1252 1253 /* 1254 * Check if there is a prefix that has a reachable advertising 1255 * router. 1256 */ 1257 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1258 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1259 break; 1260 } 1261 1262 if (pr) { 1263 /* 1264 * There is at least one prefix that has a reachable router. 1265 * Detach prefixes which have no reachable advertising 1266 * router, and attach other prefixes. 1267 */ 1268 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1269 /* XXX: a link-local prefix should never be detached */ 1270 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1271 continue; 1272 1273 /* 1274 * we aren't interested in prefixes without the L bit 1275 * set. 1276 */ 1277 if (pr->ndpr_raf_onlink == 0) 1278 continue; 1279 1280 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1281 find_pfxlist_reachable_router(pr) == NULL) 1282 pr->ndpr_stateflags |= NDPRF_DETACHED; 1283 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1284 find_pfxlist_reachable_router(pr) != 0) 1285 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1286 } 1287 } else { 1288 /* there is no prefix that has a reachable router */ 1289 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1290 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1291 continue; 1292 1293 if (pr->ndpr_raf_onlink == 0) 1294 continue; 1295 1296 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1297 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1298 } 1299 } 1300 1301 /* 1302 * Remove each interface route associated with a (just) detached 1303 * prefix, and reinstall the interface route for a (just) attached 1304 * prefix. Note that all attempt of reinstallation does not 1305 * necessarily success, when a same prefix is shared among multiple 1306 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1307 * so we don't have to care about them. 1308 */ 1309 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1310 int e; 1311 1312 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1313 continue; 1314 1315 if (pr->ndpr_raf_onlink == 0) 1316 continue; 1317 1318 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1319 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1320 if ((e = nd6_prefix_offlink(pr)) != 0) { 1321 nd6log((LOG_ERR, 1322 "pfxlist_onlink_check: failed to " 1323 "make %s/%d offlink, errno=%d\n", 1324 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1325 pr->ndpr_plen, e)); 1326 } 1327 } 1328 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1329 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 1330 pr->ndpr_raf_onlink) { 1331 if ((e = nd6_prefix_onlink(pr)) != 0) { 1332 nd6log((LOG_ERR, 1333 "pfxlist_onlink_check: failed to " 1334 "make %s/%d offlink, errno=%d\n", 1335 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1336 pr->ndpr_plen, e)); 1337 } 1338 } 1339 } 1340 1341 /* 1342 * Changes on the prefix status might affect address status as well. 1343 * Make sure that all addresses derived from an attached prefix are 1344 * attached, and that all addresses derived from a detached prefix are 1345 * detached. Note, however, that a manually configured address should 1346 * always be attached. 1347 * The precise detection logic is same as the one for prefixes. 1348 */ 1349 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1350 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1351 continue; 1352 1353 if (ifa->ia6_ndpr == NULL) { 1354 /* 1355 * This can happen when we first configure the address 1356 * (i.e. the address exists, but the prefix does not). 1357 * XXX: complicated relationships... 1358 */ 1359 continue; 1360 } 1361 1362 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1363 break; 1364 } 1365 if (ifa) { 1366 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1367 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1368 continue; 1369 1370 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 1371 continue; 1372 1373 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1374 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1375 else 1376 ifa->ia6_flags |= IN6_IFF_DETACHED; 1377 } 1378 } 1379 else { 1380 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1381 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1382 continue; 1383 1384 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1385 } 1386 } 1387} 1388 1389int 1390nd6_prefix_onlink(pr) 1391 struct nd_prefix *pr; 1392{ 1393 struct ifaddr *ifa; 1394 struct ifnet *ifp = pr->ndpr_ifp; 1395 struct sockaddr_in6 mask6; 1396 struct nd_prefix *opr; 1397 u_long rtflags; 1398 int error = 0; 1399 struct rtentry *rt = NULL; 1400 1401 /* sanity check */ 1402 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1403 nd6log((LOG_ERR, 1404 "nd6_prefix_onlink: %s/%d is already on-link\n", 1405 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen); 1406 return(EEXIST)); 1407 } 1408 1409 /* 1410 * Add the interface route associated with the prefix. Before 1411 * installing the route, check if there's the same prefix on another 1412 * interface, and the prefix has already installed the interface route. 1413 * Although such a configuration is expected to be rare, we explicitly 1414 * allow it. 1415 */ 1416 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 1417 if (opr == pr) 1418 continue; 1419 1420 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1421 continue; 1422 1423 if (opr->ndpr_plen == pr->ndpr_plen && 1424 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1425 &opr->ndpr_prefix.sin6_addr, 1426 pr->ndpr_plen)) 1427 return(0); 1428 } 1429 1430 /* 1431 * We prefer link-local addresses as the associated interface address. 1432 */ 1433 /* search for a link-local addr */ 1434 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 1435 IN6_IFF_NOTREADY| 1436 IN6_IFF_ANYCAST); 1437 if (ifa == NULL) { 1438 /* XXX: freebsd does not have ifa_ifwithaf */ 1439 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1440 { 1441 if (ifa->ifa_addr->sa_family == AF_INET6) 1442 break; 1443 } 1444 /* should we care about ia6_flags? */ 1445 } 1446 if (ifa == NULL) { 1447 /* 1448 * This can still happen, when, for example, we receive an RA 1449 * containing a prefix with the L bit set and the A bit clear, 1450 * after removing all IPv6 addresses on the receiving 1451 * interface. This should, of course, be rare though. 1452 */ 1453 nd6log((LOG_NOTICE, 1454 "nd6_prefix_onlink: failed to find any ifaddr" 1455 " to add route for a prefix(%s/%d) on %s\n", 1456 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1457 pr->ndpr_plen, if_name(ifp))); 1458 return(0); 1459 } 1460 1461 /* 1462 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1463 * ifa->ifa_rtrequest = nd6_rtrequest; 1464 */ 1465 bzero(&mask6, sizeof(mask6)); 1466 mask6.sin6_len = sizeof(mask6); 1467 mask6.sin6_addr = pr->ndpr_mask; 1468 rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP; 1469 if (nd6_need_cache(ifp)) { 1470 /* explicitly set in case ifa_flags does not set the flag. */ 1471 rtflags |= RTF_CLONING; 1472 } else { 1473 /* 1474 * explicitly clear the cloning bit in case ifa_flags sets it. 1475 */ 1476 rtflags &= ~RTF_CLONING; 1477 } 1478 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix, 1479 ifa->ifa_addr, (struct sockaddr *)&mask6, 1480 rtflags, &rt); 1481 if (error == 0) { 1482 if (rt != NULL) /* this should be non NULL, though */ 1483 nd6_rtmsg(RTM_ADD, rt); 1484 pr->ndpr_stateflags |= NDPRF_ONLINK; 1485 } 1486 else { 1487 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a" 1488 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx " 1489 "errno = %d\n", 1490 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1491 pr->ndpr_plen, if_name(ifp), 1492 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr), 1493 ip6_sprintf(&mask6.sin6_addr), rtflags, error)); 1494 } 1495 1496 if (rt != NULL) 1497 rt->rt_refcnt--; 1498 1499 return(error); 1500} 1501 1502int 1503nd6_prefix_offlink(pr) 1504 struct nd_prefix *pr; 1505{ 1506 int error = 0; 1507 struct ifnet *ifp = pr->ndpr_ifp; 1508 struct nd_prefix *opr; 1509 struct sockaddr_in6 sa6, mask6; 1510 struct rtentry *rt = NULL; 1511 1512 /* sanity check */ 1513 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1514 nd6log((LOG_ERR, 1515 "nd6_prefix_offlink: %s/%d is already off-link\n", 1516 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen)); 1517 return(EEXIST); 1518 } 1519 1520 bzero(&sa6, sizeof(sa6)); 1521 sa6.sin6_family = AF_INET6; 1522 sa6.sin6_len = sizeof(sa6); 1523 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1524 sizeof(struct in6_addr)); 1525 bzero(&mask6, sizeof(mask6)); 1526 mask6.sin6_family = AF_INET6; 1527 mask6.sin6_len = sizeof(sa6); 1528 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1529 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 1530 (struct sockaddr *)&mask6, 0, &rt); 1531 if (error == 0) { 1532 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 1533 1534 /* report the route deletion to the routing socket. */ 1535 if (rt != NULL) 1536 nd6_rtmsg(RTM_DELETE, rt); 1537 1538 /* 1539 * There might be the same prefix on another interface, 1540 * the prefix which could not be on-link just because we have 1541 * the interface route (see comments in nd6_prefix_onlink). 1542 * If there's one, try to make the prefix on-link on the 1543 * interface. 1544 */ 1545 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 1546 if (opr == pr) 1547 continue; 1548 1549 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1550 continue; 1551 1552 /* 1553 * KAME specific: detached prefixes should not be 1554 * on-link. 1555 */ 1556 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1557 continue; 1558 1559 if (opr->ndpr_plen == pr->ndpr_plen && 1560 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1561 &opr->ndpr_prefix.sin6_addr, 1562 pr->ndpr_plen)) { 1563 int e; 1564 1565 if ((e = nd6_prefix_onlink(opr)) != 0) { 1566 nd6log((LOG_ERR, 1567 "nd6_prefix_offlink: failed to " 1568 "recover a prefix %s/%d from %s " 1569 "to %s (errno = %d)\n", 1570 ip6_sprintf(&opr->ndpr_prefix.sin6_addr), 1571 opr->ndpr_plen, if_name(ifp), 1572 if_name(opr->ndpr_ifp), e)); 1573 } 1574 } 1575 } 1576 } 1577 else { 1578 /* XXX: can we still set the NDPRF_ONLINK flag? */ 1579 nd6log((LOG_ERR, 1580 "nd6_prefix_offlink: failed to delete route: " 1581 "%s/%d on %s (errno = %d)\n", 1582 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp), 1583 error)); 1584 } 1585 1586 if (rt != NULL) { 1587 if (rt->rt_refcnt <= 0) { 1588 /* XXX: we should free the entry ourselves. */ 1589 rt->rt_refcnt++; 1590 rtfree(rt); 1591 } 1592 } 1593 1594 return(error); 1595} 1596 1597static struct in6_ifaddr * 1598in6_ifadd(pr, ifid) 1599 struct nd_prefix *pr; 1600 struct in6_addr *ifid; /* Mobile IPv6 addition */ 1601{ 1602 struct ifnet *ifp = pr->ndpr_ifp; 1603 struct ifaddr *ifa; 1604 struct in6_aliasreq ifra; 1605 struct in6_ifaddr *ia, *ib; 1606 int error, plen0; 1607 struct in6_addr mask; 1608 int prefixlen = pr->ndpr_plen; 1609 1610 in6_len2mask(&mask, prefixlen); 1611 1612 /* 1613 * find a link-local address (will be interface ID). 1614 * Is it really mandatory? Theoretically, a global or a site-local 1615 * address can be configured without a link-local address, if we 1616 * have a unique interface identifier... 1617 * 1618 * it is not mandatory to have a link-local address, we can generate 1619 * interface identifier on the fly. we do this because: 1620 * (1) it should be the easiest way to find interface identifier. 1621 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 1622 * for multiple addresses on a single interface, and possible shortcut 1623 * of DAD. we omitted DAD for this reason in the past. 1624 * (3) a user can prevent autoconfiguration of global address 1625 * by removing link-local address by hand (this is partly because we 1626 * don't have other way to control the use of IPv6 on a interface. 1627 * this has been our design choice - cf. NRL's "ifconfig auto"). 1628 * (4) it is easier to manage when an interface has addresses 1629 * with the same interface identifier, than to have multiple addresses 1630 * with different interface identifiers. 1631 * 1632 * Mobile IPv6 addition: allow for caller to specify a wished interface 1633 * ID. This is to not break connections when moving addresses between 1634 * interfaces. 1635 */ 1636 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */ 1637 if (ifa) 1638 ib = (struct in6_ifaddr *)ifa; 1639 else 1640 return NULL; 1641 1642#if 0 /* don't care link local addr state, and always do DAD */ 1643 /* if link-local address is not eligible, do not autoconfigure. */ 1644 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) { 1645 printf("in6_ifadd: link-local address not ready\n"); 1646 return NULL; 1647 } 1648#endif 1649 1650 /* prefixlen + ifidlen must be equal to 128 */ 1651 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 1652 if (prefixlen != plen0) { 1653 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 1654 "(prefix=%d ifid=%d)\n", 1655 if_name(ifp), prefixlen, 128 - plen0)); 1656 return NULL; 1657 } 1658 1659 /* make ifaddr */ 1660 1661 bzero(&ifra, sizeof(ifra)); 1662 /* 1663 * in6_update_ifa() does not use ifra_name, but we accurately set it 1664 * for safety. 1665 */ 1666 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1667 ifra.ifra_addr.sin6_family = AF_INET6; 1668 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 1669 /* prefix */ 1670 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr, 1671 sizeof(ifra.ifra_addr.sin6_addr)); 1672 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1673 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1674 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1675 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1676 1677 /* interface ID */ 1678 if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid)) 1679 ifid = &ib->ia_addr.sin6_addr; 1680 ifra.ifra_addr.sin6_addr.s6_addr32[0] 1681 |= (ifid->s6_addr32[0] & ~mask.s6_addr32[0]); 1682 ifra.ifra_addr.sin6_addr.s6_addr32[1] 1683 |= (ifid->s6_addr32[1] & ~mask.s6_addr32[1]); 1684 ifra.ifra_addr.sin6_addr.s6_addr32[2] 1685 |= (ifid->s6_addr32[2] & ~mask.s6_addr32[2]); 1686 ifra.ifra_addr.sin6_addr.s6_addr32[3] 1687 |= (ifid->s6_addr32[3] & ~mask.s6_addr32[3]); 1688 1689 /* new prefix mask. */ 1690 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1691 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1692 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 1693 sizeof(ifra.ifra_prefixmask.sin6_addr)); 1694 1695 /* 1696 * lifetime. 1697 * XXX: in6_init_address_ltimes would override these values later. 1698 * We should reconsider this logic. 1699 */ 1700 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 1701 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 1702 1703 /* XXX: scope zone ID? */ 1704 1705 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 1706 /* 1707 * temporarily set the nopfx flag to avoid conflict. 1708 * XXX: we should reconsider the entire mechanism about prefix 1709 * manipulation. 1710 */ 1711 ifra.ifra_flags |= IN6_IFF_NOPFX; 1712 1713 /* 1714 * keep the new address, regardless of the result of in6_update_ifa. 1715 * XXX: this address is now meaningless. 1716 * We should reconsider its role. 1717 */ 1718 pr->ndpr_addr = ifra.ifra_addr.sin6_addr; 1719 1720 /* allocate ifaddr structure, link into chain, etc. */ 1721 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 1722 nd6log((LOG_ERR, 1723 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 1724 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp), 1725 error)); 1726 return(NULL); /* ifaddr must not have been allocated. */ 1727 } 1728 1729 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1730 1731 return(ia); /* this must NOT be NULL. */ 1732} 1733 1734int 1735in6_tmpifadd(ia0, forcegen) 1736 const struct in6_ifaddr *ia0; /* corresponding public address */ 1737{ 1738 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 1739 struct in6_ifaddr *newia; 1740 struct in6_aliasreq ifra; 1741 int i, error; 1742 int trylimit = 3; /* XXX: adhoc value */ 1743 u_int32_t randid[2]; 1744 time_t vltime0, pltime0; 1745 1746 bzero(&ifra, sizeof(ifra)); 1747 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1748 ifra.ifra_addr = ia0->ia_addr; 1749 /* copy prefix mask */ 1750 ifra.ifra_prefixmask = ia0->ia_prefixmask; 1751 /* clear the old IFID */ 1752 for (i = 0; i < 4; i++) { 1753 ifra.ifra_addr.sin6_addr.s6_addr32[i] 1754 &= ifra.ifra_prefixmask.sin6_addr.s6_addr32[i]; 1755 } 1756 1757 again: 1758 in6_get_tmpifid(ifp, (u_int8_t *)randid, 1759 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], 1760 forcegen); 1761 ifra.ifra_addr.sin6_addr.s6_addr32[2] 1762 |= (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 1763 ifra.ifra_addr.sin6_addr.s6_addr32[3] 1764 |= (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 1765 1766 /* 1767 * If by chance the new temporary address is the same as an address 1768 * already assigned to the interface, generate a new randomized 1769 * interface identifier and repeat this step. 1770 * RFC 3041 3.3 (4). 1771 */ 1772 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) { 1773 if (trylimit-- == 0) { 1774 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find " 1775 "a unique random IFID\n")); 1776 return(EEXIST); 1777 } 1778 forcegen = 1; 1779 goto again; 1780 } 1781 1782 /* 1783 * The Valid Lifetime is the lower of the Valid Lifetime of the 1784 * public address or TEMP_VALID_LIFETIME. 1785 * The Preferred Lifetime is the lower of the Preferred Lifetime 1786 * of the public address or TEMP_PREFERRED_LIFETIME - 1787 * DESYNC_FACTOR. 1788 */ 1789 if (ia0->ia6_lifetime.ia6t_expire != 0) { 1790 vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 1791 (ia0->ia6_lifetime.ia6t_expire - time_second); 1792 if (vltime0 > ip6_temp_valid_lifetime) 1793 vltime0 = ip6_temp_valid_lifetime; 1794 } else 1795 vltime0 = ip6_temp_valid_lifetime; 1796 if (ia0->ia6_lifetime.ia6t_preferred != 0) { 1797 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 1798 (ia0->ia6_lifetime.ia6t_preferred - time_second); 1799 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){ 1800 pltime0 = ip6_temp_preferred_lifetime - 1801 ip6_desync_factor; 1802 } 1803 } else 1804 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor; 1805 ifra.ifra_lifetime.ia6t_vltime = vltime0; 1806 ifra.ifra_lifetime.ia6t_pltime = pltime0; 1807 1808 /* 1809 * A temporary address is created only if this calculated Preferred 1810 * Lifetime is greater than REGEN_ADVANCE time units. 1811 */ 1812 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance) 1813 return(0); 1814 1815 /* XXX: scope zone ID? */ 1816 1817 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 1818 1819 /* allocate ifaddr structure, link into chain, etc. */ 1820 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) 1821 return(error); 1822 1823 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1824 if (newia == NULL) { /* XXX: can it happen? */ 1825 nd6log((LOG_ERR, 1826 "in6_tmpifadd: ifa update succeeded, but we got " 1827 "no ifaddr\n")); 1828 return(EINVAL); /* XXX */ 1829 } 1830 newia->ia6_ndpr = ia0->ia6_ndpr; 1831 newia->ia6_ndpr->ndpr_refcnt++; 1832 1833 return(0); 1834} 1835 1836int 1837in6_init_prefix_ltimes(struct nd_prefix *ndpr) 1838{ 1839 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */ 1840 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) { 1841 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime" 1842 "(%d) is greater than valid lifetime(%d)\n", 1843 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime)); 1844 return (EINVAL); 1845 } 1846 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 1847 ndpr->ndpr_preferred = 0; 1848 else 1849 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 1850 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1851 ndpr->ndpr_expire = 0; 1852 else 1853 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 1854 1855 return 0; 1856} 1857 1858static void 1859in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 1860{ 1861 /* Valid lifetime must not be updated unless explicitly specified. */ 1862 /* init ia6t_expire */ 1863 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 1864 lt6->ia6t_expire = 0; 1865 else { 1866 lt6->ia6t_expire = time_second; 1867 lt6->ia6t_expire += lt6->ia6t_vltime; 1868 } 1869 1870 /* init ia6t_preferred */ 1871 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 1872 lt6->ia6t_preferred = 0; 1873 else { 1874 lt6->ia6t_preferred = time_second; 1875 lt6->ia6t_preferred += lt6->ia6t_pltime; 1876 } 1877} 1878 1879/* 1880 * Delete all the routing table entries that use the specified gateway. 1881 * XXX: this function causes search through all entries of routing table, so 1882 * it shouldn't be called when acting as a router. 1883 */ 1884void 1885rt6_flush(gateway, ifp) 1886 struct in6_addr *gateway; 1887 struct ifnet *ifp; 1888{ 1889 struct radix_node_head *rnh = rt_tables[AF_INET6]; 1890 int s = splnet(); 1891 1892 /* We'll care only link-local addresses */ 1893 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 1894 splx(s); 1895 return; 1896 } 1897 /* XXX: hack for KAME's link-local address kludge */ 1898 gateway->s6_addr16[1] = htons(ifp->if_index); 1899 1900 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 1901 splx(s); 1902} 1903 1904static int 1905rt6_deleteroute(rn, arg) 1906 struct radix_node *rn; 1907 void *arg; 1908{ 1909#define SIN6(s) ((struct sockaddr_in6 *)s) 1910 struct rtentry *rt = (struct rtentry *)rn; 1911 struct in6_addr *gate = (struct in6_addr *)arg; 1912 1913 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 1914 return(0); 1915 1916 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) 1917 return(0); 1918 1919 /* 1920 * Do not delete a static route. 1921 * XXX: this seems to be a bit ad-hoc. Should we consider the 1922 * 'cloned' bit instead? 1923 */ 1924 if ((rt->rt_flags & RTF_STATIC) != 0) 1925 return(0); 1926 1927 /* 1928 * We delete only host route. This means, in particular, we don't 1929 * delete default route. 1930 */ 1931 if ((rt->rt_flags & RTF_HOST) == 0) 1932 return(0); 1933 1934 return(rtrequest(RTM_DELETE, rt_key(rt), 1935 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0)); 1936#undef SIN6 1937} 1938 1939int 1940nd6_setdefaultiface(ifindex) 1941 int ifindex; 1942{ 1943 int error = 0; 1944 1945 if (ifindex < 0 || if_index < ifindex) 1946 return(EINVAL); 1947 1948 if (nd6_defifindex != ifindex) { 1949 nd6_defifindex = ifindex; 1950 if (nd6_defifindex > 0) 1951 nd6_defifp = ifindex2ifnet[nd6_defifindex]; 1952 else 1953 nd6_defifp = NULL; 1954 1955 /* 1956 * If the Default Router List is empty, install a route 1957 * to the specified interface as default or remove the default 1958 * route when the default interface becomes canceled. 1959 * The check for the queue is actually redundant, but 1960 * we do this here to avoid re-install the default route 1961 * if the list is NOT empty. 1962 */ 1963 if (TAILQ_FIRST(&nd_defrouter) == NULL) 1964 defrouter_select(); 1965 1966 /* 1967 * Our current implementation assumes one-to-one maping between 1968 * interfaces and links, so it would be natural to use the 1969 * default interface as the default link. 1970 */ 1971 scope6_setdefault(nd6_defifp); 1972 } 1973 1974 return(error); 1975} 1976