1/*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ 30 */ 31 32#include <sys/cdefs.h>
| 1/*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ 30 */ 31 32#include <sys/cdefs.h>
|
33__FBSDID("$FreeBSD: head/sys/netinet6/nd6_rtr.c 231852 2012-02-17 02:39:58Z bz $");
| 33__FBSDID("$FreeBSD: head/sys/netinet6/nd6_rtr.c 241686 2012-10-18 13:57:24Z andre $");
|
34 35#include "opt_inet.h" 36#include "opt_inet6.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/socket.h> 43#include <sys/sockio.h> 44#include <sys/time.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/errno.h> 48#include <sys/rwlock.h> 49#include <sys/syslog.h> 50#include <sys/queue.h> 51 52#include <net/if.h> 53#include <net/if_types.h> 54#include <net/if_dl.h> 55#include <net/route.h> 56#include <net/radix.h> 57#include <net/vnet.h> 58 59#include <netinet/in.h> 60#include <net/if_llatbl.h> 61#include <netinet6/in6_var.h> 62#include <netinet6/in6_ifattach.h> 63#include <netinet/ip6.h> 64#include <netinet6/ip6_var.h> 65#include <netinet6/nd6.h> 66#include <netinet/icmp6.h> 67#include <netinet6/scope6_var.h> 68 69static int rtpref(struct nd_defrouter *); 70static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *); 71static int prelist_update __P((struct nd_prefixctl *, struct nd_defrouter *, 72 struct mbuf *, int)); 73static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int); 74static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 75 struct nd_defrouter *)); 76static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *); 77static void pfxrtr_del(struct nd_pfxrouter *); 78static struct nd_pfxrouter *find_pfxlist_reachable_router 79(struct nd_prefix *); 80static void defrouter_delreq(struct nd_defrouter *); 81static void nd6_rtmsg(int, struct rtentry *); 82 83static int in6_init_prefix_ltimes(struct nd_prefix *); 84static void in6_init_address_ltimes __P((struct nd_prefix *, 85 struct in6_addrlifetime *)); 86 87static int nd6_prefix_onlink(struct nd_prefix *); 88static int nd6_prefix_offlink(struct nd_prefix *); 89 90static int rt6_deleteroute(struct radix_node *, void *); 91 92VNET_DECLARE(int, nd6_recalc_reachtm_interval); 93#define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval) 94 95static VNET_DEFINE(struct ifnet *, nd6_defifp); 96VNET_DEFINE(int, nd6_defifindex); 97#define V_nd6_defifp VNET(nd6_defifp) 98 99VNET_DEFINE(int, ip6_use_tempaddr) = 0; 100 101VNET_DEFINE(int, ip6_desync_factor); 102VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME; 103VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME; 104 105VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE; 106 107/* RTPREF_MEDIUM has to be 0! */ 108#define RTPREF_HIGH 1 109#define RTPREF_MEDIUM 0 110#define RTPREF_LOW (-1) 111#define RTPREF_RESERVED (-2) 112#define RTPREF_INVALID (-3) /* internal */ 113 114/* 115 * Receive Router Solicitation Message - just for routers. 116 * Router solicitation/advertisement is mostly managed by userland program 117 * (rtadvd) so here we have no function like nd6_ra_output(). 118 * 119 * Based on RFC 2461 120 */ 121void 122nd6_rs_input(struct mbuf *m, int off, int icmp6len) 123{ 124 struct ifnet *ifp = m->m_pkthdr.rcvif; 125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 126 struct nd_router_solicit *nd_rs; 127 struct in6_addr saddr6 = ip6->ip6_src; 128 char *lladdr = NULL; 129 int lladdrlen = 0; 130 union nd_opts ndopts; 131 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 132 133 /* 134 * Accept RS only when V_ip6_forwarding=1 and the interface has 135 * no ND6_IFF_ACCEPT_RTADV. 136 */ 137 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) 138 goto freeit; 139 140 /* Sanity checks */ 141 if (ip6->ip6_hlim != 255) { 142 nd6log((LOG_ERR, 143 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 144 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 145 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 146 goto bad; 147 } 148 149 /* 150 * Don't update the neighbor cache, if src = ::. 151 * This indicates that the src has no IP address assigned yet. 152 */ 153 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 154 goto freeit; 155 156#ifndef PULLDOWN_TEST 157 IP6_EXTHDR_CHECK(m, off, icmp6len,); 158 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 159#else 160 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 161 if (nd_rs == NULL) { 162 ICMP6STAT_INC(icp6s_tooshort); 163 return; 164 } 165#endif 166 167 icmp6len -= sizeof(*nd_rs); 168 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 169 if (nd6_options(&ndopts) < 0) { 170 nd6log((LOG_INFO, 171 "nd6_rs_input: invalid ND option, ignored\n")); 172 /* nd6_options have incremented stats */ 173 goto freeit; 174 } 175 176 if (ndopts.nd_opts_src_lladdr) { 177 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 178 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 179 } 180 181 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 182 nd6log((LOG_INFO, 183 "nd6_rs_input: lladdrlen mismatch for %s " 184 "(if %d, RS packet %d)\n", 185 ip6_sprintf(ip6bufs, &saddr6), 186 ifp->if_addrlen, lladdrlen - 2)); 187 goto bad; 188 } 189 190 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 191 192 freeit: 193 m_freem(m); 194 return; 195 196 bad: 197 ICMP6STAT_INC(icp6s_badrs); 198 m_freem(m); 199} 200 201/* 202 * Receive Router Advertisement Message. 203 * 204 * Based on RFC 2461 205 * TODO: on-link bit on prefix information 206 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 207 */ 208void 209nd6_ra_input(struct mbuf *m, int off, int icmp6len) 210{ 211 struct ifnet *ifp = m->m_pkthdr.rcvif; 212 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 213 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 214 struct nd_router_advert *nd_ra; 215 struct in6_addr saddr6 = ip6->ip6_src; 216 int mcast = 0; 217 union nd_opts ndopts; 218 struct nd_defrouter *dr; 219 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 220 221 /* 222 * We only accept RAs only when the per-interface flag 223 * ND6_IFF_ACCEPT_RTADV is on the receiving interface. 224 */ 225 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV)) 226 goto freeit; 227 228 if (ip6->ip6_hlim != 255) { 229 nd6log((LOG_ERR, 230 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 231 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 232 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 233 goto bad; 234 } 235 236 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 237 nd6log((LOG_ERR, 238 "nd6_ra_input: src %s is not link-local\n", 239 ip6_sprintf(ip6bufs, &saddr6))); 240 goto bad; 241 } 242 243#ifndef PULLDOWN_TEST 244 IP6_EXTHDR_CHECK(m, off, icmp6len,); 245 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 246#else 247 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 248 if (nd_ra == NULL) { 249 ICMP6STAT_INC(icp6s_tooshort); 250 return; 251 } 252#endif 253 254 icmp6len -= sizeof(*nd_ra); 255 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 256 if (nd6_options(&ndopts) < 0) { 257 nd6log((LOG_INFO, 258 "nd6_ra_input: invalid ND option, ignored\n")); 259 /* nd6_options have incremented stats */ 260 goto freeit; 261 } 262 263 { 264 struct nd_defrouter dr0; 265 u_int32_t advreachable = nd_ra->nd_ra_reachable; 266 267 /* remember if this is a multicasted advertisement */ 268 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 269 mcast = 1; 270 271 bzero(&dr0, sizeof(dr0)); 272 dr0.rtaddr = saddr6; 273 dr0.flags = nd_ra->nd_ra_flags_reserved; 274 /* 275 * Effectively-disable routes from RA messages when 276 * ND6_IFF_NO_RADR enabled on the receiving interface or 277 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1). 278 */ 279 if (ndi->flags & ND6_IFF_NO_RADR) 280 dr0.rtlifetime = 0; 281 else if (V_ip6_forwarding && !V_ip6_rfc6204w3) 282 dr0.rtlifetime = 0; 283 else 284 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 285 dr0.expire = time_second + dr0.rtlifetime; 286 dr0.ifp = ifp; 287 /* unspecified or not? (RFC 2461 6.3.4) */ 288 if (advreachable) { 289 advreachable = ntohl(advreachable); 290 if (advreachable <= MAX_REACHABLE_TIME && 291 ndi->basereachable != advreachable) { 292 ndi->basereachable = advreachable; 293 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 294 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */ 295 } 296 } 297 if (nd_ra->nd_ra_retransmit) 298 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 299 if (nd_ra->nd_ra_curhoplimit) 300 ndi->chlim = nd_ra->nd_ra_curhoplimit; 301 dr = defrtrlist_update(&dr0); 302 } 303 304 /* 305 * prefix 306 */ 307 if (ndopts.nd_opts_pi) { 308 struct nd_opt_hdr *pt; 309 struct nd_opt_prefix_info *pi = NULL; 310 struct nd_prefixctl pr; 311 312 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 313 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 314 pt = (struct nd_opt_hdr *)((caddr_t)pt + 315 (pt->nd_opt_len << 3))) { 316 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 317 continue; 318 pi = (struct nd_opt_prefix_info *)pt; 319 320 if (pi->nd_opt_pi_len != 4) { 321 nd6log((LOG_INFO, 322 "nd6_ra_input: invalid option " 323 "len %d for prefix information option, " 324 "ignored\n", pi->nd_opt_pi_len)); 325 continue; 326 } 327 328 if (128 < pi->nd_opt_pi_prefix_len) { 329 nd6log((LOG_INFO, 330 "nd6_ra_input: invalid prefix " 331 "len %d for prefix information option, " 332 "ignored\n", pi->nd_opt_pi_prefix_len)); 333 continue; 334 } 335 336 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 337 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 338 nd6log((LOG_INFO, 339 "nd6_ra_input: invalid prefix " 340 "%s, ignored\n", 341 ip6_sprintf(ip6bufs, 342 &pi->nd_opt_pi_prefix))); 343 continue; 344 } 345 346 bzero(&pr, sizeof(pr)); 347 pr.ndpr_prefix.sin6_family = AF_INET6; 348 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 349 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 350 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 351 352 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 353 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 354 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 355 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 356 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 357 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 358 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time); 359 (void)prelist_update(&pr, dr, m, mcast); 360 } 361 } 362 363 /* 364 * MTU 365 */ 366 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 367 u_long mtu; 368 u_long maxmtu; 369 370 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 371 372 /* lower bound */ 373 if (mtu < IPV6_MMTU) { 374 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 375 "mtu=%lu sent from %s, ignoring\n", 376 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src))); 377 goto skip; 378 } 379 380 /* upper bound */ 381 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu) 382 ? ndi->maxmtu : ifp->if_mtu; 383 if (mtu <= maxmtu) { 384 int change = (ndi->linkmtu != mtu); 385 386 ndi->linkmtu = mtu; 387 if (change) /* in6_maxmtu may change */ 388 in6_setmaxmtu(); 389 } else { 390 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 391 "mtu=%lu sent from %s; " 392 "exceeds maxmtu %lu, ignoring\n", 393 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu)); 394 } 395 } 396 397 skip: 398 399 /* 400 * Source link layer address 401 */ 402 { 403 char *lladdr = NULL; 404 int lladdrlen = 0; 405 406 if (ndopts.nd_opts_src_lladdr) { 407 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 408 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 409 } 410 411 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 412 nd6log((LOG_INFO, 413 "nd6_ra_input: lladdrlen mismatch for %s " 414 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6), 415 ifp->if_addrlen, lladdrlen - 2)); 416 goto bad; 417 } 418 419 nd6_cache_lladdr(ifp, &saddr6, lladdr, 420 lladdrlen, ND_ROUTER_ADVERT, 0); 421 422 /* 423 * Installing a link-layer address might change the state of the 424 * router's neighbor cache, which might also affect our on-link 425 * detection of adveritsed prefixes. 426 */ 427 pfxlist_onlink_check(); 428 } 429 430 freeit: 431 m_freem(m); 432 return; 433 434 bad: 435 ICMP6STAT_INC(icp6s_badra); 436 m_freem(m); 437} 438 439/* 440 * default router list proccessing sub routines 441 */ 442 443/* tell the change to user processes watching the routing socket. */ 444static void 445nd6_rtmsg(int cmd, struct rtentry *rt) 446{ 447 struct rt_addrinfo info; 448 struct ifnet *ifp; 449 struct ifaddr *ifa; 450 451 bzero((caddr_t)&info, sizeof(info)); 452 info.rti_info[RTAX_DST] = rt_key(rt); 453 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 454 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 455 ifp = rt->rt_ifp; 456 if (ifp != NULL) { 457 IF_ADDR_RLOCK(ifp); 458 ifa = TAILQ_FIRST(&ifp->if_addrhead); 459 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 460 ifa_ref(ifa); 461 IF_ADDR_RUNLOCK(ifp); 462 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 463 } else 464 ifa = NULL; 465 466 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum); 467 if (ifa != NULL) 468 ifa_free(ifa); 469} 470 471static void 472defrouter_addreq(struct nd_defrouter *new) 473{ 474 struct sockaddr_in6 def, mask, gate; 475 struct rtentry *newrt = NULL;
| 34 35#include "opt_inet.h" 36#include "opt_inet6.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/socket.h> 43#include <sys/sockio.h> 44#include <sys/time.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/errno.h> 48#include <sys/rwlock.h> 49#include <sys/syslog.h> 50#include <sys/queue.h> 51 52#include <net/if.h> 53#include <net/if_types.h> 54#include <net/if_dl.h> 55#include <net/route.h> 56#include <net/radix.h> 57#include <net/vnet.h> 58 59#include <netinet/in.h> 60#include <net/if_llatbl.h> 61#include <netinet6/in6_var.h> 62#include <netinet6/in6_ifattach.h> 63#include <netinet/ip6.h> 64#include <netinet6/ip6_var.h> 65#include <netinet6/nd6.h> 66#include <netinet/icmp6.h> 67#include <netinet6/scope6_var.h> 68 69static int rtpref(struct nd_defrouter *); 70static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *); 71static int prelist_update __P((struct nd_prefixctl *, struct nd_defrouter *, 72 struct mbuf *, int)); 73static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int); 74static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 75 struct nd_defrouter *)); 76static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *); 77static void pfxrtr_del(struct nd_pfxrouter *); 78static struct nd_pfxrouter *find_pfxlist_reachable_router 79(struct nd_prefix *); 80static void defrouter_delreq(struct nd_defrouter *); 81static void nd6_rtmsg(int, struct rtentry *); 82 83static int in6_init_prefix_ltimes(struct nd_prefix *); 84static void in6_init_address_ltimes __P((struct nd_prefix *, 85 struct in6_addrlifetime *)); 86 87static int nd6_prefix_onlink(struct nd_prefix *); 88static int nd6_prefix_offlink(struct nd_prefix *); 89 90static int rt6_deleteroute(struct radix_node *, void *); 91 92VNET_DECLARE(int, nd6_recalc_reachtm_interval); 93#define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval) 94 95static VNET_DEFINE(struct ifnet *, nd6_defifp); 96VNET_DEFINE(int, nd6_defifindex); 97#define V_nd6_defifp VNET(nd6_defifp) 98 99VNET_DEFINE(int, ip6_use_tempaddr) = 0; 100 101VNET_DEFINE(int, ip6_desync_factor); 102VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME; 103VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME; 104 105VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE; 106 107/* RTPREF_MEDIUM has to be 0! */ 108#define RTPREF_HIGH 1 109#define RTPREF_MEDIUM 0 110#define RTPREF_LOW (-1) 111#define RTPREF_RESERVED (-2) 112#define RTPREF_INVALID (-3) /* internal */ 113 114/* 115 * Receive Router Solicitation Message - just for routers. 116 * Router solicitation/advertisement is mostly managed by userland program 117 * (rtadvd) so here we have no function like nd6_ra_output(). 118 * 119 * Based on RFC 2461 120 */ 121void 122nd6_rs_input(struct mbuf *m, int off, int icmp6len) 123{ 124 struct ifnet *ifp = m->m_pkthdr.rcvif; 125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 126 struct nd_router_solicit *nd_rs; 127 struct in6_addr saddr6 = ip6->ip6_src; 128 char *lladdr = NULL; 129 int lladdrlen = 0; 130 union nd_opts ndopts; 131 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 132 133 /* 134 * Accept RS only when V_ip6_forwarding=1 and the interface has 135 * no ND6_IFF_ACCEPT_RTADV. 136 */ 137 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) 138 goto freeit; 139 140 /* Sanity checks */ 141 if (ip6->ip6_hlim != 255) { 142 nd6log((LOG_ERR, 143 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 144 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 145 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 146 goto bad; 147 } 148 149 /* 150 * Don't update the neighbor cache, if src = ::. 151 * This indicates that the src has no IP address assigned yet. 152 */ 153 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 154 goto freeit; 155 156#ifndef PULLDOWN_TEST 157 IP6_EXTHDR_CHECK(m, off, icmp6len,); 158 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 159#else 160 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 161 if (nd_rs == NULL) { 162 ICMP6STAT_INC(icp6s_tooshort); 163 return; 164 } 165#endif 166 167 icmp6len -= sizeof(*nd_rs); 168 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 169 if (nd6_options(&ndopts) < 0) { 170 nd6log((LOG_INFO, 171 "nd6_rs_input: invalid ND option, ignored\n")); 172 /* nd6_options have incremented stats */ 173 goto freeit; 174 } 175 176 if (ndopts.nd_opts_src_lladdr) { 177 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 178 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 179 } 180 181 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 182 nd6log((LOG_INFO, 183 "nd6_rs_input: lladdrlen mismatch for %s " 184 "(if %d, RS packet %d)\n", 185 ip6_sprintf(ip6bufs, &saddr6), 186 ifp->if_addrlen, lladdrlen - 2)); 187 goto bad; 188 } 189 190 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 191 192 freeit: 193 m_freem(m); 194 return; 195 196 bad: 197 ICMP6STAT_INC(icp6s_badrs); 198 m_freem(m); 199} 200 201/* 202 * Receive Router Advertisement Message. 203 * 204 * Based on RFC 2461 205 * TODO: on-link bit on prefix information 206 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 207 */ 208void 209nd6_ra_input(struct mbuf *m, int off, int icmp6len) 210{ 211 struct ifnet *ifp = m->m_pkthdr.rcvif; 212 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 213 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 214 struct nd_router_advert *nd_ra; 215 struct in6_addr saddr6 = ip6->ip6_src; 216 int mcast = 0; 217 union nd_opts ndopts; 218 struct nd_defrouter *dr; 219 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 220 221 /* 222 * We only accept RAs only when the per-interface flag 223 * ND6_IFF_ACCEPT_RTADV is on the receiving interface. 224 */ 225 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV)) 226 goto freeit; 227 228 if (ip6->ip6_hlim != 255) { 229 nd6log((LOG_ERR, 230 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 231 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 232 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 233 goto bad; 234 } 235 236 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 237 nd6log((LOG_ERR, 238 "nd6_ra_input: src %s is not link-local\n", 239 ip6_sprintf(ip6bufs, &saddr6))); 240 goto bad; 241 } 242 243#ifndef PULLDOWN_TEST 244 IP6_EXTHDR_CHECK(m, off, icmp6len,); 245 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 246#else 247 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 248 if (nd_ra == NULL) { 249 ICMP6STAT_INC(icp6s_tooshort); 250 return; 251 } 252#endif 253 254 icmp6len -= sizeof(*nd_ra); 255 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 256 if (nd6_options(&ndopts) < 0) { 257 nd6log((LOG_INFO, 258 "nd6_ra_input: invalid ND option, ignored\n")); 259 /* nd6_options have incremented stats */ 260 goto freeit; 261 } 262 263 { 264 struct nd_defrouter dr0; 265 u_int32_t advreachable = nd_ra->nd_ra_reachable; 266 267 /* remember if this is a multicasted advertisement */ 268 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 269 mcast = 1; 270 271 bzero(&dr0, sizeof(dr0)); 272 dr0.rtaddr = saddr6; 273 dr0.flags = nd_ra->nd_ra_flags_reserved; 274 /* 275 * Effectively-disable routes from RA messages when 276 * ND6_IFF_NO_RADR enabled on the receiving interface or 277 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1). 278 */ 279 if (ndi->flags & ND6_IFF_NO_RADR) 280 dr0.rtlifetime = 0; 281 else if (V_ip6_forwarding && !V_ip6_rfc6204w3) 282 dr0.rtlifetime = 0; 283 else 284 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 285 dr0.expire = time_second + dr0.rtlifetime; 286 dr0.ifp = ifp; 287 /* unspecified or not? (RFC 2461 6.3.4) */ 288 if (advreachable) { 289 advreachable = ntohl(advreachable); 290 if (advreachable <= MAX_REACHABLE_TIME && 291 ndi->basereachable != advreachable) { 292 ndi->basereachable = advreachable; 293 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 294 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */ 295 } 296 } 297 if (nd_ra->nd_ra_retransmit) 298 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 299 if (nd_ra->nd_ra_curhoplimit) 300 ndi->chlim = nd_ra->nd_ra_curhoplimit; 301 dr = defrtrlist_update(&dr0); 302 } 303 304 /* 305 * prefix 306 */ 307 if (ndopts.nd_opts_pi) { 308 struct nd_opt_hdr *pt; 309 struct nd_opt_prefix_info *pi = NULL; 310 struct nd_prefixctl pr; 311 312 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 313 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 314 pt = (struct nd_opt_hdr *)((caddr_t)pt + 315 (pt->nd_opt_len << 3))) { 316 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 317 continue; 318 pi = (struct nd_opt_prefix_info *)pt; 319 320 if (pi->nd_opt_pi_len != 4) { 321 nd6log((LOG_INFO, 322 "nd6_ra_input: invalid option " 323 "len %d for prefix information option, " 324 "ignored\n", pi->nd_opt_pi_len)); 325 continue; 326 } 327 328 if (128 < pi->nd_opt_pi_prefix_len) { 329 nd6log((LOG_INFO, 330 "nd6_ra_input: invalid prefix " 331 "len %d for prefix information option, " 332 "ignored\n", pi->nd_opt_pi_prefix_len)); 333 continue; 334 } 335 336 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 337 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 338 nd6log((LOG_INFO, 339 "nd6_ra_input: invalid prefix " 340 "%s, ignored\n", 341 ip6_sprintf(ip6bufs, 342 &pi->nd_opt_pi_prefix))); 343 continue; 344 } 345 346 bzero(&pr, sizeof(pr)); 347 pr.ndpr_prefix.sin6_family = AF_INET6; 348 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 349 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 350 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 351 352 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 353 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 354 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 355 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 356 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 357 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 358 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time); 359 (void)prelist_update(&pr, dr, m, mcast); 360 } 361 } 362 363 /* 364 * MTU 365 */ 366 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 367 u_long mtu; 368 u_long maxmtu; 369 370 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 371 372 /* lower bound */ 373 if (mtu < IPV6_MMTU) { 374 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 375 "mtu=%lu sent from %s, ignoring\n", 376 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src))); 377 goto skip; 378 } 379 380 /* upper bound */ 381 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu) 382 ? ndi->maxmtu : ifp->if_mtu; 383 if (mtu <= maxmtu) { 384 int change = (ndi->linkmtu != mtu); 385 386 ndi->linkmtu = mtu; 387 if (change) /* in6_maxmtu may change */ 388 in6_setmaxmtu(); 389 } else { 390 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 391 "mtu=%lu sent from %s; " 392 "exceeds maxmtu %lu, ignoring\n", 393 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu)); 394 } 395 } 396 397 skip: 398 399 /* 400 * Source link layer address 401 */ 402 { 403 char *lladdr = NULL; 404 int lladdrlen = 0; 405 406 if (ndopts.nd_opts_src_lladdr) { 407 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 408 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 409 } 410 411 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 412 nd6log((LOG_INFO, 413 "nd6_ra_input: lladdrlen mismatch for %s " 414 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6), 415 ifp->if_addrlen, lladdrlen - 2)); 416 goto bad; 417 } 418 419 nd6_cache_lladdr(ifp, &saddr6, lladdr, 420 lladdrlen, ND_ROUTER_ADVERT, 0); 421 422 /* 423 * Installing a link-layer address might change the state of the 424 * router's neighbor cache, which might also affect our on-link 425 * detection of adveritsed prefixes. 426 */ 427 pfxlist_onlink_check(); 428 } 429 430 freeit: 431 m_freem(m); 432 return; 433 434 bad: 435 ICMP6STAT_INC(icp6s_badra); 436 m_freem(m); 437} 438 439/* 440 * default router list proccessing sub routines 441 */ 442 443/* tell the change to user processes watching the routing socket. */ 444static void 445nd6_rtmsg(int cmd, struct rtentry *rt) 446{ 447 struct rt_addrinfo info; 448 struct ifnet *ifp; 449 struct ifaddr *ifa; 450 451 bzero((caddr_t)&info, sizeof(info)); 452 info.rti_info[RTAX_DST] = rt_key(rt); 453 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 454 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 455 ifp = rt->rt_ifp; 456 if (ifp != NULL) { 457 IF_ADDR_RLOCK(ifp); 458 ifa = TAILQ_FIRST(&ifp->if_addrhead); 459 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 460 ifa_ref(ifa); 461 IF_ADDR_RUNLOCK(ifp); 462 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 463 } else 464 ifa = NULL; 465 466 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum); 467 if (ifa != NULL) 468 ifa_free(ifa); 469} 470 471static void 472defrouter_addreq(struct nd_defrouter *new) 473{ 474 struct sockaddr_in6 def, mask, gate; 475 struct rtentry *newrt = NULL;
|
476 int s;
| |
477 int error; 478 479 bzero(&def, sizeof(def)); 480 bzero(&mask, sizeof(mask)); 481 bzero(&gate, sizeof(gate)); 482 483 def.sin6_len = mask.sin6_len = gate.sin6_len = 484 sizeof(struct sockaddr_in6); 485 def.sin6_family = gate.sin6_family = AF_INET6; 486 gate.sin6_addr = new->rtaddr; 487
| 476 int error; 477 478 bzero(&def, sizeof(def)); 479 bzero(&mask, sizeof(mask)); 480 bzero(&gate, sizeof(gate)); 481 482 def.sin6_len = mask.sin6_len = gate.sin6_len = 483 sizeof(struct sockaddr_in6); 484 def.sin6_family = gate.sin6_family = AF_INET6; 485 gate.sin6_addr = new->rtaddr; 486
|
488 s = splnet();
| |
489 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def, 490 (struct sockaddr *)&gate, (struct sockaddr *)&mask, 491 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB); 492 if (newrt) { 493 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 494 RTFREE(newrt); 495 } 496 if (error == 0) 497 new->installed = 1;
| 487 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def, 488 (struct sockaddr *)&gate, (struct sockaddr *)&mask, 489 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB); 490 if (newrt) { 491 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 492 RTFREE(newrt); 493 } 494 if (error == 0) 495 new->installed = 1;
|
498 splx(s);
| |
499 return; 500} 501 502struct nd_defrouter * 503defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp) 504{ 505 struct nd_defrouter *dr; 506 507 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 508 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 509 return (dr); 510 } 511 512 return (NULL); /* search failed */ 513} 514 515/* 516 * Remove the default route for a given router. 517 * This is just a subroutine function for defrouter_select(), and should 518 * not be called from anywhere else. 519 */ 520static void 521defrouter_delreq(struct nd_defrouter *dr) 522{ 523 struct sockaddr_in6 def, mask, gate; 524 struct rtentry *oldrt = NULL; 525 526 bzero(&def, sizeof(def)); 527 bzero(&mask, sizeof(mask)); 528 bzero(&gate, sizeof(gate)); 529 530 def.sin6_len = mask.sin6_len = gate.sin6_len = 531 sizeof(struct sockaddr_in6); 532 def.sin6_family = gate.sin6_family = AF_INET6; 533 gate.sin6_addr = dr->rtaddr; 534 535 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def, 536 (struct sockaddr *)&gate, 537 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB); 538 if (oldrt) { 539 nd6_rtmsg(RTM_DELETE, oldrt); 540 RTFREE(oldrt); 541 } 542 543 dr->installed = 0; 544} 545 546/* 547 * remove all default routes from default router list 548 */ 549void 550defrouter_reset(void) 551{ 552 struct nd_defrouter *dr; 553 554 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) 555 defrouter_delreq(dr); 556 557 /* 558 * XXX should we also nuke any default routers in the kernel, by 559 * going through them by rtalloc1()? 560 */ 561} 562 563void 564defrtrlist_del(struct nd_defrouter *dr) 565{ 566 struct nd_defrouter *deldr = NULL; 567 struct nd_prefix *pr; 568 569 /* 570 * Flush all the routing table entries that use the router 571 * as a next hop. 572 */ 573 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV) 574 rt6_flush(&dr->rtaddr, dr->ifp); 575 576 if (dr->installed) { 577 deldr = dr; 578 defrouter_delreq(dr); 579 } 580 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 581 582 /* 583 * Also delete all the pointers to the router in each prefix lists. 584 */ 585 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 586 struct nd_pfxrouter *pfxrtr; 587 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 588 pfxrtr_del(pfxrtr); 589 } 590 pfxlist_onlink_check(); 591 592 /* 593 * If the router is the primary one, choose a new one. 594 * Note that defrouter_select() will remove the current gateway 595 * from the routing table. 596 */ 597 if (deldr) 598 defrouter_select(); 599 600 free(dr, M_IP6NDP); 601} 602 603/* 604 * Default Router Selection according to Section 6.3.6 of RFC 2461 and 605 * draft-ietf-ipngwg-router-selection: 606 * 1) Routers that are reachable or probably reachable should be preferred. 607 * If we have more than one (probably) reachable router, prefer ones 608 * with the highest router preference. 609 * 2) When no routers on the list are known to be reachable or 610 * probably reachable, routers SHOULD be selected in a round-robin 611 * fashion, regardless of router preference values. 612 * 3) If the Default Router List is empty, assume that all 613 * destinations are on-link. 614 * 615 * We assume nd_defrouter is sorted by router preference value. 616 * Since the code below covers both with and without router preference cases, 617 * we do not need to classify the cases by ifdef. 618 * 619 * At this moment, we do not try to install more than one default router, 620 * even when the multipath routing is available, because we're not sure about 621 * the benefits for stub hosts comparing to the risk of making the code 622 * complicated and the possibility of introducing bugs. 623 */ 624void 625defrouter_select(void) 626{
| 496 return; 497} 498 499struct nd_defrouter * 500defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp) 501{ 502 struct nd_defrouter *dr; 503 504 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 505 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 506 return (dr); 507 } 508 509 return (NULL); /* search failed */ 510} 511 512/* 513 * Remove the default route for a given router. 514 * This is just a subroutine function for defrouter_select(), and should 515 * not be called from anywhere else. 516 */ 517static void 518defrouter_delreq(struct nd_defrouter *dr) 519{ 520 struct sockaddr_in6 def, mask, gate; 521 struct rtentry *oldrt = NULL; 522 523 bzero(&def, sizeof(def)); 524 bzero(&mask, sizeof(mask)); 525 bzero(&gate, sizeof(gate)); 526 527 def.sin6_len = mask.sin6_len = gate.sin6_len = 528 sizeof(struct sockaddr_in6); 529 def.sin6_family = gate.sin6_family = AF_INET6; 530 gate.sin6_addr = dr->rtaddr; 531 532 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def, 533 (struct sockaddr *)&gate, 534 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB); 535 if (oldrt) { 536 nd6_rtmsg(RTM_DELETE, oldrt); 537 RTFREE(oldrt); 538 } 539 540 dr->installed = 0; 541} 542 543/* 544 * remove all default routes from default router list 545 */ 546void 547defrouter_reset(void) 548{ 549 struct nd_defrouter *dr; 550 551 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) 552 defrouter_delreq(dr); 553 554 /* 555 * XXX should we also nuke any default routers in the kernel, by 556 * going through them by rtalloc1()? 557 */ 558} 559 560void 561defrtrlist_del(struct nd_defrouter *dr) 562{ 563 struct nd_defrouter *deldr = NULL; 564 struct nd_prefix *pr; 565 566 /* 567 * Flush all the routing table entries that use the router 568 * as a next hop. 569 */ 570 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV) 571 rt6_flush(&dr->rtaddr, dr->ifp); 572 573 if (dr->installed) { 574 deldr = dr; 575 defrouter_delreq(dr); 576 } 577 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 578 579 /* 580 * Also delete all the pointers to the router in each prefix lists. 581 */ 582 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 583 struct nd_pfxrouter *pfxrtr; 584 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 585 pfxrtr_del(pfxrtr); 586 } 587 pfxlist_onlink_check(); 588 589 /* 590 * If the router is the primary one, choose a new one. 591 * Note that defrouter_select() will remove the current gateway 592 * from the routing table. 593 */ 594 if (deldr) 595 defrouter_select(); 596 597 free(dr, M_IP6NDP); 598} 599 600/* 601 * Default Router Selection according to Section 6.3.6 of RFC 2461 and 602 * draft-ietf-ipngwg-router-selection: 603 * 1) Routers that are reachable or probably reachable should be preferred. 604 * If we have more than one (probably) reachable router, prefer ones 605 * with the highest router preference. 606 * 2) When no routers on the list are known to be reachable or 607 * probably reachable, routers SHOULD be selected in a round-robin 608 * fashion, regardless of router preference values. 609 * 3) If the Default Router List is empty, assume that all 610 * destinations are on-link. 611 * 612 * We assume nd_defrouter is sorted by router preference value. 613 * Since the code below covers both with and without router preference cases, 614 * we do not need to classify the cases by ifdef. 615 * 616 * At this moment, we do not try to install more than one default router, 617 * even when the multipath routing is available, because we're not sure about 618 * the benefits for stub hosts comparing to the risk of making the code 619 * complicated and the possibility of introducing bugs. 620 */ 621void 622defrouter_select(void) 623{
|
627 int s = splnet();
| |
628 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL; 629 struct llentry *ln = NULL; 630 631 /* 632 * Let's handle easy case (3) first: 633 * If default router list is empty, there's nothing to be done. 634 */
| 624 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL; 625 struct llentry *ln = NULL; 626 627 /* 628 * Let's handle easy case (3) first: 629 * If default router list is empty, there's nothing to be done. 630 */
|
635 if (TAILQ_EMPTY(&V_nd_defrouter)) { 636 splx(s);
| 631 if (TAILQ_EMPTY(&V_nd_defrouter))
|
637 return;
| 632 return;
|
638 }
| |
639 640 /* 641 * Search for a (probably) reachable router from the list. 642 * We just pick up the first reachable one (if any), assuming that 643 * the ordering rule of the list described in defrtrlist_update(). 644 */ 645 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 646 IF_AFDATA_LOCK(dr->ifp); 647 if (selected_dr == NULL && 648 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 649 ND6_IS_LLINFO_PROBREACH(ln)) { 650 selected_dr = dr; 651 } 652 IF_AFDATA_UNLOCK(dr->ifp); 653 if (ln != NULL) { 654 LLE_RUNLOCK(ln); 655 ln = NULL; 656 } 657 658 if (dr->installed && installed_dr == NULL) 659 installed_dr = dr; 660 else if (dr->installed && installed_dr) { 661 /* this should not happen. warn for diagnosis. */ 662 log(LOG_ERR, "defrouter_select: more than one router" 663 " is installed\n"); 664 } 665 } 666 /* 667 * If none of the default routers was found to be reachable, 668 * round-robin the list regardless of preference. 669 * Otherwise, if we have an installed router, check if the selected 670 * (reachable) router should really be preferred to the installed one. 671 * We only prefer the new router when the old one is not reachable 672 * or when the new one has a really higher preference value. 673 */ 674 if (selected_dr == NULL) { 675 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry)) 676 selected_dr = TAILQ_FIRST(&V_nd_defrouter); 677 else 678 selected_dr = TAILQ_NEXT(installed_dr, dr_entry); 679 } else if (installed_dr) { 680 IF_AFDATA_LOCK(installed_dr->ifp); 681 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) && 682 ND6_IS_LLINFO_PROBREACH(ln) && 683 rtpref(selected_dr) <= rtpref(installed_dr)) { 684 selected_dr = installed_dr; 685 } 686 IF_AFDATA_UNLOCK(installed_dr->ifp); 687 if (ln != NULL) 688 LLE_RUNLOCK(ln); 689 } 690 691 /* 692 * If the selected router is different than the installed one, 693 * remove the installed router and install the selected one. 694 * Note that the selected router is never NULL here. 695 */ 696 if (installed_dr != selected_dr) { 697 if (installed_dr) 698 defrouter_delreq(installed_dr); 699 defrouter_addreq(selected_dr); 700 } 701
| 633 634 /* 635 * Search for a (probably) reachable router from the list. 636 * We just pick up the first reachable one (if any), assuming that 637 * the ordering rule of the list described in defrtrlist_update(). 638 */ 639 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 640 IF_AFDATA_LOCK(dr->ifp); 641 if (selected_dr == NULL && 642 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 643 ND6_IS_LLINFO_PROBREACH(ln)) { 644 selected_dr = dr; 645 } 646 IF_AFDATA_UNLOCK(dr->ifp); 647 if (ln != NULL) { 648 LLE_RUNLOCK(ln); 649 ln = NULL; 650 } 651 652 if (dr->installed && installed_dr == NULL) 653 installed_dr = dr; 654 else if (dr->installed && installed_dr) { 655 /* this should not happen. warn for diagnosis. */ 656 log(LOG_ERR, "defrouter_select: more than one router" 657 " is installed\n"); 658 } 659 } 660 /* 661 * If none of the default routers was found to be reachable, 662 * round-robin the list regardless of preference. 663 * Otherwise, if we have an installed router, check if the selected 664 * (reachable) router should really be preferred to the installed one. 665 * We only prefer the new router when the old one is not reachable 666 * or when the new one has a really higher preference value. 667 */ 668 if (selected_dr == NULL) { 669 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry)) 670 selected_dr = TAILQ_FIRST(&V_nd_defrouter); 671 else 672 selected_dr = TAILQ_NEXT(installed_dr, dr_entry); 673 } else if (installed_dr) { 674 IF_AFDATA_LOCK(installed_dr->ifp); 675 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) && 676 ND6_IS_LLINFO_PROBREACH(ln) && 677 rtpref(selected_dr) <= rtpref(installed_dr)) { 678 selected_dr = installed_dr; 679 } 680 IF_AFDATA_UNLOCK(installed_dr->ifp); 681 if (ln != NULL) 682 LLE_RUNLOCK(ln); 683 } 684 685 /* 686 * If the selected router is different than the installed one, 687 * remove the installed router and install the selected one. 688 * Note that the selected router is never NULL here. 689 */ 690 if (installed_dr != selected_dr) { 691 if (installed_dr) 692 defrouter_delreq(installed_dr); 693 defrouter_addreq(selected_dr); 694 } 695
|
702 splx(s);
| |
703 return; 704} 705 706/* 707 * for default router selection 708 * regards router-preference field as a 2-bit signed integer 709 */ 710static int 711rtpref(struct nd_defrouter *dr) 712{ 713 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) { 714 case ND_RA_FLAG_RTPREF_HIGH: 715 return (RTPREF_HIGH); 716 case ND_RA_FLAG_RTPREF_MEDIUM: 717 case ND_RA_FLAG_RTPREF_RSV: 718 return (RTPREF_MEDIUM); 719 case ND_RA_FLAG_RTPREF_LOW: 720 return (RTPREF_LOW); 721 default: 722 /* 723 * This case should never happen. If it did, it would mean a 724 * serious bug of kernel internal. We thus always bark here. 725 * Or, can we even panic? 726 */ 727 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags); 728 return (RTPREF_INVALID); 729 } 730 /* NOTREACHED */ 731} 732 733static struct nd_defrouter * 734defrtrlist_update(struct nd_defrouter *new) 735{ 736 struct nd_defrouter *dr, *n;
| 696 return; 697} 698 699/* 700 * for default router selection 701 * regards router-preference field as a 2-bit signed integer 702 */ 703static int 704rtpref(struct nd_defrouter *dr) 705{ 706 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) { 707 case ND_RA_FLAG_RTPREF_HIGH: 708 return (RTPREF_HIGH); 709 case ND_RA_FLAG_RTPREF_MEDIUM: 710 case ND_RA_FLAG_RTPREF_RSV: 711 return (RTPREF_MEDIUM); 712 case ND_RA_FLAG_RTPREF_LOW: 713 return (RTPREF_LOW); 714 default: 715 /* 716 * This case should never happen. If it did, it would mean a 717 * serious bug of kernel internal. We thus always bark here. 718 * Or, can we even panic? 719 */ 720 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags); 721 return (RTPREF_INVALID); 722 } 723 /* NOTREACHED */ 724} 725 726static struct nd_defrouter * 727defrtrlist_update(struct nd_defrouter *new) 728{ 729 struct nd_defrouter *dr, *n;
|
737 int s = splnet();
| |
738 739 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 740 /* entry exists */ 741 if (new->rtlifetime == 0) { 742 defrtrlist_del(dr); 743 dr = NULL; 744 } else { 745 int oldpref = rtpref(dr); 746 747 /* override */ 748 dr->flags = new->flags; /* xxx flag check */ 749 dr->rtlifetime = new->rtlifetime; 750 dr->expire = new->expire; 751 752 /* 753 * If the preference does not change, there's no need 754 * to sort the entries. Also make sure the selected 755 * router is still installed in the kernel. 756 */
| 730 731 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 732 /* entry exists */ 733 if (new->rtlifetime == 0) { 734 defrtrlist_del(dr); 735 dr = NULL; 736 } else { 737 int oldpref = rtpref(dr); 738 739 /* override */ 740 dr->flags = new->flags; /* xxx flag check */ 741 dr->rtlifetime = new->rtlifetime; 742 dr->expire = new->expire; 743 744 /* 745 * If the preference does not change, there's no need 746 * to sort the entries. Also make sure the selected 747 * router is still installed in the kernel. 748 */
|
757 if (dr->installed && rtpref(new) == oldpref) { 758 splx(s);
| 749 if (dr->installed && rtpref(new) == oldpref)
|
759 return (dr);
| 750 return (dr);
|
760 }
| |
761 762 /* 763 * preferred router may be changed, so relocate 764 * this router. 765 * XXX: calling TAILQ_REMOVE directly is a bad manner. 766 * However, since defrtrlist_del() has many side 767 * effects, we intentionally do so here. 768 * defrouter_select() below will handle routing 769 * changes later. 770 */ 771 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 772 n = dr; 773 goto insert; 774 }
| 751 752 /* 753 * preferred router may be changed, so relocate 754 * this router. 755 * XXX: calling TAILQ_REMOVE directly is a bad manner. 756 * However, since defrtrlist_del() has many side 757 * effects, we intentionally do so here. 758 * defrouter_select() below will handle routing 759 * changes later. 760 */ 761 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 762 n = dr; 763 goto insert; 764 }
|
775 splx(s);
| |
776 return (dr); 777 } 778 779 /* entry does not exist */
| 765 return (dr); 766 } 767 768 /* entry does not exist */
|
780 if (new->rtlifetime == 0) { 781 splx(s);
| 769 if (new->rtlifetime == 0)
|
782 return (NULL);
| 770 return (NULL);
|
783 }
| |
784 785 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
| 771 772 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
|
786 if (n == NULL) { 787 splx(s);
| 773 if (n == NULL)
|
788 return (NULL);
| 774 return (NULL);
|
789 }
| |
790 bzero(n, sizeof(*n)); 791 *n = *new; 792 793insert: 794 /* 795 * Insert the new router in the Default Router List; 796 * The Default Router List should be in the descending order 797 * of router-preferece. Routers with the same preference are 798 * sorted in the arriving time order. 799 */ 800 801 /* insert at the end of the group */ 802 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 803 if (rtpref(n) > rtpref(dr)) 804 break; 805 } 806 if (dr) 807 TAILQ_INSERT_BEFORE(dr, n, dr_entry); 808 else 809 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry); 810 811 defrouter_select(); 812
| 775 bzero(n, sizeof(*n)); 776 *n = *new; 777 778insert: 779 /* 780 * Insert the new router in the Default Router List; 781 * The Default Router List should be in the descending order 782 * of router-preferece. Routers with the same preference are 783 * sorted in the arriving time order. 784 */ 785 786 /* insert at the end of the group */ 787 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 788 if (rtpref(n) > rtpref(dr)) 789 break; 790 } 791 if (dr) 792 TAILQ_INSERT_BEFORE(dr, n, dr_entry); 793 else 794 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry); 795 796 defrouter_select(); 797
|
813 splx(s); 814
| |
815 return (n); 816} 817 818static struct nd_pfxrouter * 819pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr) 820{ 821 struct nd_pfxrouter *search; 822 823 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) { 824 if (search->router == dr) 825 break; 826 } 827 828 return (search); 829} 830 831static void 832pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr) 833{ 834 struct nd_pfxrouter *new; 835 836 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 837 if (new == NULL) 838 return; 839 bzero(new, sizeof(*new)); 840 new->router = dr; 841 842 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 843 844 pfxlist_onlink_check(); 845} 846 847static void 848pfxrtr_del(struct nd_pfxrouter *pfr) 849{ 850 LIST_REMOVE(pfr, pfr_entry); 851 free(pfr, M_IP6NDP); 852} 853 854struct nd_prefix * 855nd6_prefix_lookup(struct nd_prefixctl *key) 856{ 857 struct nd_prefix *search; 858 859 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) { 860 if (key->ndpr_ifp == search->ndpr_ifp && 861 key->ndpr_plen == search->ndpr_plen && 862 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr, 863 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) { 864 break; 865 } 866 } 867 868 return (search); 869} 870 871int 872nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr, 873 struct nd_prefix **newp) 874{ 875 struct nd_prefix *new = NULL; 876 int error = 0;
| 798 return (n); 799} 800 801static struct nd_pfxrouter * 802pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr) 803{ 804 struct nd_pfxrouter *search; 805 806 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) { 807 if (search->router == dr) 808 break; 809 } 810 811 return (search); 812} 813 814static void 815pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr) 816{ 817 struct nd_pfxrouter *new; 818 819 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 820 if (new == NULL) 821 return; 822 bzero(new, sizeof(*new)); 823 new->router = dr; 824 825 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 826 827 pfxlist_onlink_check(); 828} 829 830static void 831pfxrtr_del(struct nd_pfxrouter *pfr) 832{ 833 LIST_REMOVE(pfr, pfr_entry); 834 free(pfr, M_IP6NDP); 835} 836 837struct nd_prefix * 838nd6_prefix_lookup(struct nd_prefixctl *key) 839{ 840 struct nd_prefix *search; 841 842 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) { 843 if (key->ndpr_ifp == search->ndpr_ifp && 844 key->ndpr_plen == search->ndpr_plen && 845 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr, 846 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) { 847 break; 848 } 849 } 850 851 return (search); 852} 853 854int 855nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr, 856 struct nd_prefix **newp) 857{ 858 struct nd_prefix *new = NULL; 859 int error = 0;
|
877 int i, s;
| 860 int i;
|
878 char ip6buf[INET6_ADDRSTRLEN]; 879 880 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 881 if (new == NULL) 882 return(ENOMEM); 883 bzero(new, sizeof(*new)); 884 new->ndpr_ifp = pr->ndpr_ifp; 885 new->ndpr_prefix = pr->ndpr_prefix; 886 new->ndpr_plen = pr->ndpr_plen; 887 new->ndpr_vltime = pr->ndpr_vltime; 888 new->ndpr_pltime = pr->ndpr_pltime; 889 new->ndpr_flags = pr->ndpr_flags; 890 if ((error = in6_init_prefix_ltimes(new)) != 0) { 891 free(new, M_IP6NDP); 892 return(error); 893 } 894 new->ndpr_lastupdate = time_second; 895 if (newp != NULL) 896 *newp = new; 897 898 /* initialization */ 899 LIST_INIT(&new->ndpr_advrtrs); 900 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 901 /* make prefix in the canonical form */ 902 for (i = 0; i < 4; i++) 903 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 904 new->ndpr_mask.s6_addr32[i]; 905
| 861 char ip6buf[INET6_ADDRSTRLEN]; 862 863 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 864 if (new == NULL) 865 return(ENOMEM); 866 bzero(new, sizeof(*new)); 867 new->ndpr_ifp = pr->ndpr_ifp; 868 new->ndpr_prefix = pr->ndpr_prefix; 869 new->ndpr_plen = pr->ndpr_plen; 870 new->ndpr_vltime = pr->ndpr_vltime; 871 new->ndpr_pltime = pr->ndpr_pltime; 872 new->ndpr_flags = pr->ndpr_flags; 873 if ((error = in6_init_prefix_ltimes(new)) != 0) { 874 free(new, M_IP6NDP); 875 return(error); 876 } 877 new->ndpr_lastupdate = time_second; 878 if (newp != NULL) 879 *newp = new; 880 881 /* initialization */ 882 LIST_INIT(&new->ndpr_advrtrs); 883 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 884 /* make prefix in the canonical form */ 885 for (i = 0; i < 4; i++) 886 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 887 new->ndpr_mask.s6_addr32[i]; 888
|
906 s = splnet();
| |
907 /* link ndpr_entry to nd_prefix list */ 908 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
| 889 /* link ndpr_entry to nd_prefix list */ 890 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
|
909 splx(s);
| |
910 911 /* ND_OPT_PI_FLAG_ONLINK processing */ 912 if (new->ndpr_raf_onlink) { 913 int e; 914 915 if ((e = nd6_prefix_onlink(new)) != 0) { 916 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 917 "the prefix %s/%d on-link on %s (errno=%d)\n", 918 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 919 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 920 /* proceed anyway. XXX: is it correct? */ 921 } 922 } 923 924 if (dr) 925 pfxrtr_add(new, dr); 926 927 return 0; 928} 929 930void 931prelist_remove(struct nd_prefix *pr) 932{ 933 struct nd_pfxrouter *pfr, *next;
| 891 892 /* ND_OPT_PI_FLAG_ONLINK processing */ 893 if (new->ndpr_raf_onlink) { 894 int e; 895 896 if ((e = nd6_prefix_onlink(new)) != 0) { 897 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 898 "the prefix %s/%d on-link on %s (errno=%d)\n", 899 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 900 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 901 /* proceed anyway. XXX: is it correct? */ 902 } 903 } 904 905 if (dr) 906 pfxrtr_add(new, dr); 907 908 return 0; 909} 910 911void 912prelist_remove(struct nd_prefix *pr) 913{ 914 struct nd_pfxrouter *pfr, *next;
|
934 int e, s;
| 915 int e;
|
935 char ip6buf[INET6_ADDRSTRLEN]; 936 937 /* make sure to invalidate the prefix until it is really freed. */ 938 pr->ndpr_vltime = 0; 939 pr->ndpr_pltime = 0; 940 941 /* 942 * Though these flags are now meaningless, we'd rather keep the value 943 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users 944 * when executing "ndp -p". 945 */ 946 947 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 948 (e = nd6_prefix_offlink(pr)) != 0) { 949 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 950 "on %s, errno=%d\n", 951 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 952 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 953 /* what should we do? */ 954 } 955 956 if (pr->ndpr_refcnt > 0) 957 return; /* notice here? */ 958
| 916 char ip6buf[INET6_ADDRSTRLEN]; 917 918 /* make sure to invalidate the prefix until it is really freed. */ 919 pr->ndpr_vltime = 0; 920 pr->ndpr_pltime = 0; 921 922 /* 923 * Though these flags are now meaningless, we'd rather keep the value 924 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users 925 * when executing "ndp -p". 926 */ 927 928 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 929 (e = nd6_prefix_offlink(pr)) != 0) { 930 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 931 "on %s, errno=%d\n", 932 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 933 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 934 /* what should we do? */ 935 } 936 937 if (pr->ndpr_refcnt > 0) 938 return; /* notice here? */ 939
|
959 s = splnet(); 960
| |
961 /* unlink ndpr_entry from nd_prefix list */ 962 LIST_REMOVE(pr, ndpr_entry); 963 964 /* free list of routers that adversed the prefix */ 965 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) { 966 free(pfr, M_IP6NDP); 967 }
| 940 /* unlink ndpr_entry from nd_prefix list */ 941 LIST_REMOVE(pr, ndpr_entry); 942 943 /* free list of routers that adversed the prefix */ 944 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) { 945 free(pfr, M_IP6NDP); 946 }
|
968 splx(s); 969
| |
970 free(pr, M_IP6NDP); 971 972 pfxlist_onlink_check(); 973} 974 975/* 976 * dr - may be NULL 977 */ 978 979static int 980prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr, 981 struct mbuf *m, int mcast) 982{ 983 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 984 struct ifaddr *ifa; 985 struct ifnet *ifp = new->ndpr_ifp; 986 struct nd_prefix *pr;
| 947 free(pr, M_IP6NDP); 948 949 pfxlist_onlink_check(); 950} 951 952/* 953 * dr - may be NULL 954 */ 955 956static int 957prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr, 958 struct mbuf *m, int mcast) 959{ 960 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 961 struct ifaddr *ifa; 962 struct ifnet *ifp = new->ndpr_ifp; 963 struct nd_prefix *pr;
|
987 int s = splnet();
| |
988 int error = 0; 989 int newprefix = 0; 990 int auth; 991 struct in6_addrlifetime lt6_tmp; 992 char ip6buf[INET6_ADDRSTRLEN]; 993 994 auth = 0; 995 if (m) { 996 /* 997 * Authenticity for NA consists authentication for 998 * both IP header and IP datagrams, doesn't it ? 999 */ 1000#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 1001 auth = ((m->m_flags & M_AUTHIPHDR) && 1002 (m->m_flags & M_AUTHIPDGM)); 1003#endif 1004 } 1005 1006 if ((pr = nd6_prefix_lookup(new)) != NULL) { 1007 /* 1008 * nd6_prefix_lookup() ensures that pr and new have the same 1009 * prefix on a same interface. 1010 */ 1011 1012 /* 1013 * Update prefix information. Note that the on-link (L) bit 1014 * and the autonomous (A) bit should NOT be changed from 1 1015 * to 0. 1016 */ 1017 if (new->ndpr_raf_onlink == 1) 1018 pr->ndpr_raf_onlink = 1; 1019 if (new->ndpr_raf_auto == 1) 1020 pr->ndpr_raf_auto = 1; 1021 if (new->ndpr_raf_onlink) { 1022 pr->ndpr_vltime = new->ndpr_vltime; 1023 pr->ndpr_pltime = new->ndpr_pltime; 1024 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */ 1025 pr->ndpr_lastupdate = time_second; 1026 } 1027 1028 if (new->ndpr_raf_onlink && 1029 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1030 int e; 1031 1032 if ((e = nd6_prefix_onlink(pr)) != 0) { 1033 nd6log((LOG_ERR, 1034 "prelist_update: failed to make " 1035 "the prefix %s/%d on-link on %s " 1036 "(errno=%d)\n", 1037 ip6_sprintf(ip6buf, 1038 &pr->ndpr_prefix.sin6_addr), 1039 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 1040 /* proceed anyway. XXX: is it correct? */ 1041 } 1042 } 1043 1044 if (dr && pfxrtr_lookup(pr, dr) == NULL) 1045 pfxrtr_add(pr, dr); 1046 } else { 1047 struct nd_prefix *newpr = NULL; 1048 1049 newprefix = 1; 1050 1051 if (new->ndpr_vltime == 0) 1052 goto end; 1053 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 1054 goto end; 1055 1056 error = nd6_prelist_add(new, dr, &newpr); 1057 if (error != 0 || newpr == NULL) { 1058 nd6log((LOG_NOTICE, "prelist_update: " 1059 "nd6_prelist_add failed for %s/%d on %s " 1060 "errno=%d, returnpr=%p\n", 1061 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr), 1062 new->ndpr_plen, if_name(new->ndpr_ifp), 1063 error, newpr)); 1064 goto end; /* we should just give up in this case. */ 1065 } 1066 1067 /* 1068 * XXX: from the ND point of view, we can ignore a prefix 1069 * with the on-link bit being zero. However, we need a 1070 * prefix structure for references from autoconfigured 1071 * addresses. Thus, we explicitly make sure that the prefix 1072 * itself expires now. 1073 */ 1074 if (newpr->ndpr_raf_onlink == 0) { 1075 newpr->ndpr_vltime = 0; 1076 newpr->ndpr_pltime = 0; 1077 in6_init_prefix_ltimes(newpr); 1078 } 1079 1080 pr = newpr; 1081 } 1082 1083 /* 1084 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1085 * Note that pr must be non NULL at this point. 1086 */ 1087 1088 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1089 if (!new->ndpr_raf_auto) 1090 goto end; 1091 1092 /* 1093 * 5.5.3 (b). the link-local prefix should have been ignored in 1094 * nd6_ra_input. 1095 */ 1096 1097 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */ 1098 if (new->ndpr_pltime > new->ndpr_vltime) { 1099 error = EINVAL; /* XXX: won't be used */ 1100 goto end; 1101 } 1102 1103 /* 1104 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of 1105 * an address configured by stateless autoconfiguration already in the 1106 * list of addresses associated with the interface, and the Valid 1107 * Lifetime is not 0, form an address. We first check if we have 1108 * a matching prefix. 1109 * Note: we apply a clarification in rfc2462bis-02 here. We only 1110 * consider autoconfigured addresses while RFC2462 simply said 1111 * "address". 1112 */ 1113 IF_ADDR_RLOCK(ifp); 1114 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1115 struct in6_ifaddr *ifa6; 1116 u_int32_t remaininglifetime; 1117 1118 if (ifa->ifa_addr->sa_family != AF_INET6) 1119 continue; 1120 1121 ifa6 = (struct in6_ifaddr *)ifa; 1122 1123 /* 1124 * We only consider autoconfigured addresses as per rfc2462bis. 1125 */ 1126 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF)) 1127 continue; 1128 1129 /* 1130 * Spec is not clear here, but I believe we should concentrate 1131 * on unicast (i.e. not anycast) addresses. 1132 * XXX: other ia6_flags? detached or duplicated? 1133 */ 1134 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1135 continue; 1136 1137 /* 1138 * Ignore the address if it is not associated with a prefix 1139 * or is associated with a prefix that is different from this 1140 * one. (pr is never NULL here) 1141 */ 1142 if (ifa6->ia6_ndpr != pr) 1143 continue; 1144 1145 if (ia6_match == NULL) /* remember the first one */ 1146 ia6_match = ifa6; 1147 1148 /* 1149 * An already autoconfigured address matched. Now that we 1150 * are sure there is at least one matched address, we can 1151 * proceed to 5.5.3. (e): update the lifetimes according to the 1152 * "two hours" rule and the privacy extension. 1153 * We apply some clarifications in rfc2462bis: 1154 * - use remaininglifetime instead of storedlifetime as a 1155 * variable name 1156 * - remove the dead code in the "two-hour" rule 1157 */ 1158#define TWOHOUR (120*60) 1159 lt6_tmp = ifa6->ia6_lifetime; 1160 1161 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1162 remaininglifetime = ND6_INFINITE_LIFETIME; 1163 else if (time_second - ifa6->ia6_updatetime > 1164 lt6_tmp.ia6t_vltime) { 1165 /* 1166 * The case of "invalid" address. We should usually 1167 * not see this case. 1168 */ 1169 remaininglifetime = 0; 1170 } else 1171 remaininglifetime = lt6_tmp.ia6t_vltime - 1172 (time_second - ifa6->ia6_updatetime); 1173 1174 /* when not updating, keep the current stored lifetime. */ 1175 lt6_tmp.ia6t_vltime = remaininglifetime; 1176 1177 if (TWOHOUR < new->ndpr_vltime || 1178 remaininglifetime < new->ndpr_vltime) { 1179 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1180 } else if (remaininglifetime <= TWOHOUR) { 1181 if (auth) { 1182 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1183 } 1184 } else { 1185 /* 1186 * new->ndpr_vltime <= TWOHOUR && 1187 * TWOHOUR < remaininglifetime 1188 */ 1189 lt6_tmp.ia6t_vltime = TWOHOUR; 1190 } 1191 1192 /* The 2 hour rule is not imposed for preferred lifetime. */ 1193 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1194 1195 in6_init_address_ltimes(pr, <6_tmp); 1196 1197 /* 1198 * We need to treat lifetimes for temporary addresses 1199 * differently, according to 1200 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1); 1201 * we only update the lifetimes when they are in the maximum 1202 * intervals. 1203 */ 1204 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 1205 u_int32_t maxvltime, maxpltime; 1206 1207 if (V_ip6_temp_valid_lifetime > 1208 (u_int32_t)((time_second - ifa6->ia6_createtime) + 1209 V_ip6_desync_factor)) { 1210 maxvltime = V_ip6_temp_valid_lifetime - 1211 (time_second - ifa6->ia6_createtime) - 1212 V_ip6_desync_factor; 1213 } else 1214 maxvltime = 0; 1215 if (V_ip6_temp_preferred_lifetime > 1216 (u_int32_t)((time_second - ifa6->ia6_createtime) + 1217 V_ip6_desync_factor)) { 1218 maxpltime = V_ip6_temp_preferred_lifetime - 1219 (time_second - ifa6->ia6_createtime) - 1220 V_ip6_desync_factor; 1221 } else 1222 maxpltime = 0; 1223 1224 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME || 1225 lt6_tmp.ia6t_vltime > maxvltime) { 1226 lt6_tmp.ia6t_vltime = maxvltime; 1227 } 1228 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME || 1229 lt6_tmp.ia6t_pltime > maxpltime) { 1230 lt6_tmp.ia6t_pltime = maxpltime; 1231 } 1232 } 1233 ifa6->ia6_lifetime = lt6_tmp; 1234 ifa6->ia6_updatetime = time_second; 1235 } 1236 IF_ADDR_RUNLOCK(ifp); 1237 if (ia6_match == NULL && new->ndpr_vltime) { 1238 int ifidlen; 1239 1240 /* 1241 * 5.5.3 (d) (continued) 1242 * No address matched and the valid lifetime is non-zero. 1243 * Create a new address. 1244 */ 1245 1246 /* 1247 * Prefix Length check: 1248 * If the sum of the prefix length and interface identifier 1249 * length does not equal 128 bits, the Prefix Information 1250 * option MUST be ignored. The length of the interface 1251 * identifier is defined in a separate link-type specific 1252 * document. 1253 */ 1254 ifidlen = in6_if2idlen(ifp); 1255 if (ifidlen < 0) { 1256 /* this should not happen, so we always log it. */ 1257 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n", 1258 if_name(ifp)); 1259 goto end; 1260 } 1261 if (ifidlen + pr->ndpr_plen != 128) { 1262 nd6log((LOG_INFO, 1263 "prelist_update: invalid prefixlen " 1264 "%d for %s, ignored\n", 1265 pr->ndpr_plen, if_name(ifp))); 1266 goto end; 1267 } 1268 1269 if ((ia6 = in6_ifadd(new, mcast)) != NULL) { 1270 /* 1271 * note that we should use pr (not new) for reference. 1272 */ 1273 pr->ndpr_refcnt++; 1274 ia6->ia6_ndpr = pr; 1275 1276 /* 1277 * RFC 3041 3.3 (2). 1278 * When a new public address is created as described 1279 * in RFC2462, also create a new temporary address. 1280 * 1281 * RFC 3041 3.5. 1282 * When an interface connects to a new link, a new 1283 * randomized interface identifier should be generated 1284 * immediately together with a new set of temporary 1285 * addresses. Thus, we specifiy 1 as the 2nd arg of 1286 * in6_tmpifadd(). 1287 */ 1288 if (V_ip6_use_tempaddr) { 1289 int e; 1290 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) { 1291 nd6log((LOG_NOTICE, "prelist_update: " 1292 "failed to create a temporary " 1293 "address, errno=%d\n", 1294 e)); 1295 } 1296 } 1297 ifa_free(&ia6->ia_ifa); 1298 1299 /* 1300 * A newly added address might affect the status 1301 * of other addresses, so we check and update it. 1302 * XXX: what if address duplication happens? 1303 */ 1304 pfxlist_onlink_check(); 1305 } else { 1306 /* just set an error. do not bark here. */ 1307 error = EADDRNOTAVAIL; /* XXX: might be unused. */ 1308 } 1309 } 1310 1311 end:
| 964 int error = 0; 965 int newprefix = 0; 966 int auth; 967 struct in6_addrlifetime lt6_tmp; 968 char ip6buf[INET6_ADDRSTRLEN]; 969 970 auth = 0; 971 if (m) { 972 /* 973 * Authenticity for NA consists authentication for 974 * both IP header and IP datagrams, doesn't it ? 975 */ 976#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 977 auth = ((m->m_flags & M_AUTHIPHDR) && 978 (m->m_flags & M_AUTHIPDGM)); 979#endif 980 } 981 982 if ((pr = nd6_prefix_lookup(new)) != NULL) { 983 /* 984 * nd6_prefix_lookup() ensures that pr and new have the same 985 * prefix on a same interface. 986 */ 987 988 /* 989 * Update prefix information. Note that the on-link (L) bit 990 * and the autonomous (A) bit should NOT be changed from 1 991 * to 0. 992 */ 993 if (new->ndpr_raf_onlink == 1) 994 pr->ndpr_raf_onlink = 1; 995 if (new->ndpr_raf_auto == 1) 996 pr->ndpr_raf_auto = 1; 997 if (new->ndpr_raf_onlink) { 998 pr->ndpr_vltime = new->ndpr_vltime; 999 pr->ndpr_pltime = new->ndpr_pltime; 1000 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */ 1001 pr->ndpr_lastupdate = time_second; 1002 } 1003 1004 if (new->ndpr_raf_onlink && 1005 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1006 int e; 1007 1008 if ((e = nd6_prefix_onlink(pr)) != 0) { 1009 nd6log((LOG_ERR, 1010 "prelist_update: failed to make " 1011 "the prefix %s/%d on-link on %s " 1012 "(errno=%d)\n", 1013 ip6_sprintf(ip6buf, 1014 &pr->ndpr_prefix.sin6_addr), 1015 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 1016 /* proceed anyway. XXX: is it correct? */ 1017 } 1018 } 1019 1020 if (dr && pfxrtr_lookup(pr, dr) == NULL) 1021 pfxrtr_add(pr, dr); 1022 } else { 1023 struct nd_prefix *newpr = NULL; 1024 1025 newprefix = 1; 1026 1027 if (new->ndpr_vltime == 0) 1028 goto end; 1029 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 1030 goto end; 1031 1032 error = nd6_prelist_add(new, dr, &newpr); 1033 if (error != 0 || newpr == NULL) { 1034 nd6log((LOG_NOTICE, "prelist_update: " 1035 "nd6_prelist_add failed for %s/%d on %s " 1036 "errno=%d, returnpr=%p\n", 1037 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr), 1038 new->ndpr_plen, if_name(new->ndpr_ifp), 1039 error, newpr)); 1040 goto end; /* we should just give up in this case. */ 1041 } 1042 1043 /* 1044 * XXX: from the ND point of view, we can ignore a prefix 1045 * with the on-link bit being zero. However, we need a 1046 * prefix structure for references from autoconfigured 1047 * addresses. Thus, we explicitly make sure that the prefix 1048 * itself expires now. 1049 */ 1050 if (newpr->ndpr_raf_onlink == 0) { 1051 newpr->ndpr_vltime = 0; 1052 newpr->ndpr_pltime = 0; 1053 in6_init_prefix_ltimes(newpr); 1054 } 1055 1056 pr = newpr; 1057 } 1058 1059 /* 1060 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1061 * Note that pr must be non NULL at this point. 1062 */ 1063 1064 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1065 if (!new->ndpr_raf_auto) 1066 goto end; 1067 1068 /* 1069 * 5.5.3 (b). the link-local prefix should have been ignored in 1070 * nd6_ra_input. 1071 */ 1072 1073 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */ 1074 if (new->ndpr_pltime > new->ndpr_vltime) { 1075 error = EINVAL; /* XXX: won't be used */ 1076 goto end; 1077 } 1078 1079 /* 1080 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of 1081 * an address configured by stateless autoconfiguration already in the 1082 * list of addresses associated with the interface, and the Valid 1083 * Lifetime is not 0, form an address. We first check if we have 1084 * a matching prefix. 1085 * Note: we apply a clarification in rfc2462bis-02 here. We only 1086 * consider autoconfigured addresses while RFC2462 simply said 1087 * "address". 1088 */ 1089 IF_ADDR_RLOCK(ifp); 1090 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1091 struct in6_ifaddr *ifa6; 1092 u_int32_t remaininglifetime; 1093 1094 if (ifa->ifa_addr->sa_family != AF_INET6) 1095 continue; 1096 1097 ifa6 = (struct in6_ifaddr *)ifa; 1098 1099 /* 1100 * We only consider autoconfigured addresses as per rfc2462bis. 1101 */ 1102 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF)) 1103 continue; 1104 1105 /* 1106 * Spec is not clear here, but I believe we should concentrate 1107 * on unicast (i.e. not anycast) addresses. 1108 * XXX: other ia6_flags? detached or duplicated? 1109 */ 1110 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1111 continue; 1112 1113 /* 1114 * Ignore the address if it is not associated with a prefix 1115 * or is associated with a prefix that is different from this 1116 * one. (pr is never NULL here) 1117 */ 1118 if (ifa6->ia6_ndpr != pr) 1119 continue; 1120 1121 if (ia6_match == NULL) /* remember the first one */ 1122 ia6_match = ifa6; 1123 1124 /* 1125 * An already autoconfigured address matched. Now that we 1126 * are sure there is at least one matched address, we can 1127 * proceed to 5.5.3. (e): update the lifetimes according to the 1128 * "two hours" rule and the privacy extension. 1129 * We apply some clarifications in rfc2462bis: 1130 * - use remaininglifetime instead of storedlifetime as a 1131 * variable name 1132 * - remove the dead code in the "two-hour" rule 1133 */ 1134#define TWOHOUR (120*60) 1135 lt6_tmp = ifa6->ia6_lifetime; 1136 1137 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1138 remaininglifetime = ND6_INFINITE_LIFETIME; 1139 else if (time_second - ifa6->ia6_updatetime > 1140 lt6_tmp.ia6t_vltime) { 1141 /* 1142 * The case of "invalid" address. We should usually 1143 * not see this case. 1144 */ 1145 remaininglifetime = 0; 1146 } else 1147 remaininglifetime = lt6_tmp.ia6t_vltime - 1148 (time_second - ifa6->ia6_updatetime); 1149 1150 /* when not updating, keep the current stored lifetime. */ 1151 lt6_tmp.ia6t_vltime = remaininglifetime; 1152 1153 if (TWOHOUR < new->ndpr_vltime || 1154 remaininglifetime < new->ndpr_vltime) { 1155 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1156 } else if (remaininglifetime <= TWOHOUR) { 1157 if (auth) { 1158 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1159 } 1160 } else { 1161 /* 1162 * new->ndpr_vltime <= TWOHOUR && 1163 * TWOHOUR < remaininglifetime 1164 */ 1165 lt6_tmp.ia6t_vltime = TWOHOUR; 1166 } 1167 1168 /* The 2 hour rule is not imposed for preferred lifetime. */ 1169 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1170 1171 in6_init_address_ltimes(pr, <6_tmp); 1172 1173 /* 1174 * We need to treat lifetimes for temporary addresses 1175 * differently, according to 1176 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1); 1177 * we only update the lifetimes when they are in the maximum 1178 * intervals. 1179 */ 1180 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 1181 u_int32_t maxvltime, maxpltime; 1182 1183 if (V_ip6_temp_valid_lifetime > 1184 (u_int32_t)((time_second - ifa6->ia6_createtime) + 1185 V_ip6_desync_factor)) { 1186 maxvltime = V_ip6_temp_valid_lifetime - 1187 (time_second - ifa6->ia6_createtime) - 1188 V_ip6_desync_factor; 1189 } else 1190 maxvltime = 0; 1191 if (V_ip6_temp_preferred_lifetime > 1192 (u_int32_t)((time_second - ifa6->ia6_createtime) + 1193 V_ip6_desync_factor)) { 1194 maxpltime = V_ip6_temp_preferred_lifetime - 1195 (time_second - ifa6->ia6_createtime) - 1196 V_ip6_desync_factor; 1197 } else 1198 maxpltime = 0; 1199 1200 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME || 1201 lt6_tmp.ia6t_vltime > maxvltime) { 1202 lt6_tmp.ia6t_vltime = maxvltime; 1203 } 1204 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME || 1205 lt6_tmp.ia6t_pltime > maxpltime) { 1206 lt6_tmp.ia6t_pltime = maxpltime; 1207 } 1208 } 1209 ifa6->ia6_lifetime = lt6_tmp; 1210 ifa6->ia6_updatetime = time_second; 1211 } 1212 IF_ADDR_RUNLOCK(ifp); 1213 if (ia6_match == NULL && new->ndpr_vltime) { 1214 int ifidlen; 1215 1216 /* 1217 * 5.5.3 (d) (continued) 1218 * No address matched and the valid lifetime is non-zero. 1219 * Create a new address. 1220 */ 1221 1222 /* 1223 * Prefix Length check: 1224 * If the sum of the prefix length and interface identifier 1225 * length does not equal 128 bits, the Prefix Information 1226 * option MUST be ignored. The length of the interface 1227 * identifier is defined in a separate link-type specific 1228 * document. 1229 */ 1230 ifidlen = in6_if2idlen(ifp); 1231 if (ifidlen < 0) { 1232 /* this should not happen, so we always log it. */ 1233 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n", 1234 if_name(ifp)); 1235 goto end; 1236 } 1237 if (ifidlen + pr->ndpr_plen != 128) { 1238 nd6log((LOG_INFO, 1239 "prelist_update: invalid prefixlen " 1240 "%d for %s, ignored\n", 1241 pr->ndpr_plen, if_name(ifp))); 1242 goto end; 1243 } 1244 1245 if ((ia6 = in6_ifadd(new, mcast)) != NULL) { 1246 /* 1247 * note that we should use pr (not new) for reference. 1248 */ 1249 pr->ndpr_refcnt++; 1250 ia6->ia6_ndpr = pr; 1251 1252 /* 1253 * RFC 3041 3.3 (2). 1254 * When a new public address is created as described 1255 * in RFC2462, also create a new temporary address. 1256 * 1257 * RFC 3041 3.5. 1258 * When an interface connects to a new link, a new 1259 * randomized interface identifier should be generated 1260 * immediately together with a new set of temporary 1261 * addresses. Thus, we specifiy 1 as the 2nd arg of 1262 * in6_tmpifadd(). 1263 */ 1264 if (V_ip6_use_tempaddr) { 1265 int e; 1266 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) { 1267 nd6log((LOG_NOTICE, "prelist_update: " 1268 "failed to create a temporary " 1269 "address, errno=%d\n", 1270 e)); 1271 } 1272 } 1273 ifa_free(&ia6->ia_ifa); 1274 1275 /* 1276 * A newly added address might affect the status 1277 * of other addresses, so we check and update it. 1278 * XXX: what if address duplication happens? 1279 */ 1280 pfxlist_onlink_check(); 1281 } else { 1282 /* just set an error. do not bark here. */ 1283 error = EADDRNOTAVAIL; /* XXX: might be unused. */ 1284 } 1285 } 1286 1287 end:
|
1312 splx(s);
| |
1313 return error; 1314} 1315 1316/* 1317 * A supplement function used in the on-link detection below; 1318 * detect if a given prefix has a (probably) reachable advertising router. 1319 * XXX: lengthy function name... 1320 */ 1321static struct nd_pfxrouter * 1322find_pfxlist_reachable_router(struct nd_prefix *pr) 1323{ 1324 struct nd_pfxrouter *pfxrtr; 1325 struct llentry *ln; 1326 int canreach; 1327 1328 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) { 1329 IF_AFDATA_LOCK(pfxrtr->router->ifp); 1330 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp); 1331 IF_AFDATA_UNLOCK(pfxrtr->router->ifp); 1332 if (ln == NULL) 1333 continue; 1334 canreach = ND6_IS_LLINFO_PROBREACH(ln); 1335 LLE_RUNLOCK(ln); 1336 if (canreach) 1337 break; 1338 } 1339 return (pfxrtr); 1340} 1341 1342/* 1343 * Check if each prefix in the prefix list has at least one available router 1344 * that advertised the prefix (a router is "available" if its neighbor cache 1345 * entry is reachable or probably reachable). 1346 * If the check fails, the prefix may be off-link, because, for example, 1347 * we have moved from the network but the lifetime of the prefix has not 1348 * expired yet. So we should not use the prefix if there is another prefix 1349 * that has an available router. 1350 * But, if there is no prefix that has an available router, we still regards 1351 * all the prefixes as on-link. This is because we can't tell if all the 1352 * routers are simply dead or if we really moved from the network and there 1353 * is no router around us. 1354 */ 1355void 1356pfxlist_onlink_check() 1357{ 1358 struct nd_prefix *pr; 1359 struct in6_ifaddr *ifa; 1360 struct nd_defrouter *dr; 1361 struct nd_pfxrouter *pfxrtr = NULL; 1362 1363 /* 1364 * Check if there is a prefix that has a reachable advertising 1365 * router. 1366 */ 1367 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1368 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1369 break; 1370 } 1371 1372 /* 1373 * If we have no such prefix, check whether we still have a router 1374 * that does not advertise any prefixes. 1375 */ 1376 if (pr == NULL) { 1377 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 1378 struct nd_prefix *pr0; 1379 1380 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) { 1381 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL) 1382 break; 1383 } 1384 if (pfxrtr != NULL) 1385 break; 1386 } 1387 } 1388 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) { 1389 /* 1390 * There is at least one prefix that has a reachable router, 1391 * or at least a router which probably does not advertise 1392 * any prefixes. The latter would be the case when we move 1393 * to a new link where we have a router that does not provide 1394 * prefixes and we configure an address by hand. 1395 * Detach prefixes which have no reachable advertising 1396 * router, and attach other prefixes. 1397 */ 1398 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1399 /* XXX: a link-local prefix should never be detached */ 1400 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1401 continue; 1402 1403 /* 1404 * we aren't interested in prefixes without the L bit 1405 * set. 1406 */ 1407 if (pr->ndpr_raf_onlink == 0) 1408 continue; 1409 1410 if (pr->ndpr_raf_auto == 0) 1411 continue; 1412 1413 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1414 find_pfxlist_reachable_router(pr) == NULL) 1415 pr->ndpr_stateflags |= NDPRF_DETACHED; 1416 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1417 find_pfxlist_reachable_router(pr) != 0) 1418 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1419 } 1420 } else { 1421 /* there is no prefix that has a reachable router */ 1422 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1423 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1424 continue; 1425 1426 if (pr->ndpr_raf_onlink == 0) 1427 continue; 1428 1429 if (pr->ndpr_raf_auto == 0) 1430 continue; 1431 1432 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1433 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1434 } 1435 } 1436 1437 /* 1438 * Remove each interface route associated with a (just) detached 1439 * prefix, and reinstall the interface route for a (just) attached 1440 * prefix. Note that all attempt of reinstallation does not 1441 * necessarily success, when a same prefix is shared among multiple 1442 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1443 * so we don't have to care about them. 1444 */ 1445 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1446 int e; 1447 char ip6buf[INET6_ADDRSTRLEN]; 1448 1449 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1450 continue; 1451 1452 if (pr->ndpr_raf_onlink == 0) 1453 continue; 1454 1455 if (pr->ndpr_raf_auto == 0) 1456 continue; 1457 1458 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1459 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1460 if ((e = nd6_prefix_offlink(pr)) != 0) { 1461 nd6log((LOG_ERR, 1462 "pfxlist_onlink_check: failed to " 1463 "make %s/%d offlink, errno=%d\n", 1464 ip6_sprintf(ip6buf, 1465 &pr->ndpr_prefix.sin6_addr), 1466 pr->ndpr_plen, e)); 1467 } 1468 } 1469 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1470 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 1471 pr->ndpr_raf_onlink) { 1472 if ((e = nd6_prefix_onlink(pr)) != 0) { 1473 nd6log((LOG_ERR, 1474 "pfxlist_onlink_check: failed to " 1475 "make %s/%d onlink, errno=%d\n", 1476 ip6_sprintf(ip6buf, 1477 &pr->ndpr_prefix.sin6_addr), 1478 pr->ndpr_plen, e)); 1479 } 1480 } 1481 } 1482 1483 /* 1484 * Changes on the prefix status might affect address status as well. 1485 * Make sure that all addresses derived from an attached prefix are 1486 * attached, and that all addresses derived from a detached prefix are 1487 * detached. Note, however, that a manually configured address should 1488 * always be attached. 1489 * The precise detection logic is same as the one for prefixes. 1490 * 1491 * XXXRW: in6_ifaddrhead locking. 1492 */ 1493 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1494 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF)) 1495 continue; 1496 1497 if (ifa->ia6_ndpr == NULL) { 1498 /* 1499 * This can happen when we first configure the address 1500 * (i.e. the address exists, but the prefix does not). 1501 * XXX: complicated relationships... 1502 */ 1503 continue; 1504 } 1505 1506 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1507 break; 1508 } 1509 if (ifa) { 1510 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1511 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1512 continue; 1513 1514 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 1515 continue; 1516 1517 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) { 1518 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1519 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1520 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1521 nd6_dad_start((struct ifaddr *)ifa, 0); 1522 } 1523 } else { 1524 ifa->ia6_flags |= IN6_IFF_DETACHED; 1525 } 1526 } 1527 } 1528 else { 1529 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1530 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1531 continue; 1532 1533 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1534 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1535 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1536 /* Do we need a delay in this case? */ 1537 nd6_dad_start((struct ifaddr *)ifa, 0); 1538 } 1539 } 1540 } 1541} 1542 1543static int 1544nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa) 1545{ 1546 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1547 struct radix_node_head *rnh; 1548 struct rtentry *rt; 1549 struct sockaddr_in6 mask6; 1550 u_long rtflags; 1551 int error, a_failure, fibnum; 1552 1553 /* 1554 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1555 * ifa->ifa_rtrequest = nd6_rtrequest; 1556 */ 1557 bzero(&mask6, sizeof(mask6)); 1558 mask6.sin6_len = sizeof(mask6); 1559 mask6.sin6_addr = pr->ndpr_mask; 1560 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP; 1561 1562 a_failure = 0; 1563 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1564 1565 rt = NULL; 1566 error = in6_rtrequest(RTM_ADD, 1567 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr, 1568 (struct sockaddr *)&mask6, rtflags, &rt, fibnum); 1569 if (error == 0) { 1570 KASSERT(rt != NULL, ("%s: in6_rtrequest return no " 1571 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__, 1572 error, pr, ifa)); 1573 1574 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6); 1575 /* XXX what if rhn == NULL? */ 1576 RADIX_NODE_HEAD_LOCK(rnh); 1577 RT_LOCK(rt); 1578 if (rt_setgate(rt, rt_key(rt), 1579 (struct sockaddr *)&null_sdl) == 0) { 1580 struct sockaddr_dl *dl; 1581 1582 dl = (struct sockaddr_dl *)rt->rt_gateway; 1583 dl->sdl_type = rt->rt_ifp->if_type; 1584 dl->sdl_index = rt->rt_ifp->if_index; 1585 } 1586 RADIX_NODE_HEAD_UNLOCK(rnh); 1587 nd6_rtmsg(RTM_ADD, rt); 1588 RT_UNLOCK(rt); 1589 pr->ndpr_stateflags |= NDPRF_ONLINK; 1590 } else { 1591 char ip6buf[INET6_ADDRSTRLEN]; 1592 char ip6bufg[INET6_ADDRSTRLEN]; 1593 char ip6bufm[INET6_ADDRSTRLEN]; 1594 struct sockaddr_in6 *sin6; 1595 1596 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1597 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add " 1598 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, " 1599 "flags=%lx errno = %d\n", 1600 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1601 pr->ndpr_plen, if_name(pr->ndpr_ifp), 1602 ip6_sprintf(ip6bufg, &sin6->sin6_addr), 1603 ip6_sprintf(ip6bufm, &mask6.sin6_addr), 1604 rtflags, error)); 1605 1606 /* Save last error to return, see rtinit(). */ 1607 a_failure = error; 1608 } 1609 1610 if (rt != NULL) { 1611 RT_LOCK(rt); 1612 RT_REMREF(rt); 1613 RT_UNLOCK(rt); 1614 } 1615 } 1616 1617 /* Return the last error we got. */ 1618 return (a_failure); 1619} 1620 1621static int 1622nd6_prefix_onlink(struct nd_prefix *pr) 1623{ 1624 struct ifaddr *ifa; 1625 struct ifnet *ifp = pr->ndpr_ifp; 1626 struct nd_prefix *opr; 1627 int error = 0; 1628 char ip6buf[INET6_ADDRSTRLEN]; 1629 1630 /* sanity check */ 1631 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1632 nd6log((LOG_ERR, 1633 "nd6_prefix_onlink: %s/%d is already on-link\n", 1634 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1635 pr->ndpr_plen)); 1636 return (EEXIST); 1637 } 1638 1639 /* 1640 * Add the interface route associated with the prefix. Before 1641 * installing the route, check if there's the same prefix on another 1642 * interface, and the prefix has already installed the interface route. 1643 * Although such a configuration is expected to be rare, we explicitly 1644 * allow it. 1645 */ 1646 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 1647 if (opr == pr) 1648 continue; 1649 1650 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1651 continue; 1652 1653 if (opr->ndpr_plen == pr->ndpr_plen && 1654 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1655 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) 1656 return (0); 1657 } 1658 1659 /* 1660 * We prefer link-local addresses as the associated interface address. 1661 */ 1662 /* search for a link-local addr */ 1663 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 1664 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST); 1665 if (ifa == NULL) { 1666 /* XXX: freebsd does not have ifa_ifwithaf */ 1667 IF_ADDR_RLOCK(ifp); 1668 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1669 if (ifa->ifa_addr->sa_family == AF_INET6) 1670 break; 1671 } 1672 if (ifa != NULL) 1673 ifa_ref(ifa); 1674 IF_ADDR_RUNLOCK(ifp); 1675 /* should we care about ia6_flags? */ 1676 } 1677 if (ifa == NULL) { 1678 /* 1679 * This can still happen, when, for example, we receive an RA 1680 * containing a prefix with the L bit set and the A bit clear, 1681 * after removing all IPv6 addresses on the receiving 1682 * interface. This should, of course, be rare though. 1683 */ 1684 nd6log((LOG_NOTICE, 1685 "nd6_prefix_onlink: failed to find any ifaddr" 1686 " to add route for a prefix(%s/%d) on %s\n", 1687 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1688 pr->ndpr_plen, if_name(ifp))); 1689 return (0); 1690 } 1691 1692 error = nd6_prefix_onlink_rtrequest(pr, ifa); 1693 1694 if (ifa != NULL) 1695 ifa_free(ifa); 1696 1697 return (error); 1698} 1699 1700static int 1701nd6_prefix_offlink(struct nd_prefix *pr) 1702{ 1703 int error = 0; 1704 struct ifnet *ifp = pr->ndpr_ifp; 1705 struct nd_prefix *opr; 1706 struct sockaddr_in6 sa6, mask6; 1707 struct rtentry *rt; 1708 char ip6buf[INET6_ADDRSTRLEN]; 1709 int fibnum, a_failure; 1710 1711 /* sanity check */ 1712 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1713 nd6log((LOG_ERR, 1714 "nd6_prefix_offlink: %s/%d is already off-link\n", 1715 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1716 pr->ndpr_plen)); 1717 return (EEXIST); 1718 } 1719 1720 bzero(&sa6, sizeof(sa6)); 1721 sa6.sin6_family = AF_INET6; 1722 sa6.sin6_len = sizeof(sa6); 1723 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1724 sizeof(struct in6_addr)); 1725 bzero(&mask6, sizeof(mask6)); 1726 mask6.sin6_family = AF_INET6; 1727 mask6.sin6_len = sizeof(sa6); 1728 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1729 1730 a_failure = 0; 1731 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1732 rt = NULL; 1733 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 1734 (struct sockaddr *)&mask6, 0, &rt, fibnum); 1735 if (error == 0) { 1736 /* report the route deletion to the routing socket. */ 1737 if (rt != NULL) 1738 nd6_rtmsg(RTM_DELETE, rt); 1739 } else { 1740 /* Save last error to return, see rtinit(). */ 1741 a_failure = error; 1742 } 1743 if (rt != NULL) { 1744 RTFREE(rt); 1745 } 1746 } 1747 error = a_failure; 1748 if (error == 0) { 1749 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 1750 1751 /* 1752 * There might be the same prefix on another interface, 1753 * the prefix which could not be on-link just because we have 1754 * the interface route (see comments in nd6_prefix_onlink). 1755 * If there's one, try to make the prefix on-link on the 1756 * interface. 1757 */ 1758 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 1759 if (opr == pr) 1760 continue; 1761 1762 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1763 continue; 1764 1765 /* 1766 * KAME specific: detached prefixes should not be 1767 * on-link. 1768 */ 1769 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1770 continue; 1771 1772 if (opr->ndpr_plen == pr->ndpr_plen && 1773 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1774 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 1775 int e; 1776 1777 if ((e = nd6_prefix_onlink(opr)) != 0) { 1778 nd6log((LOG_ERR, 1779 "nd6_prefix_offlink: failed to " 1780 "recover a prefix %s/%d from %s " 1781 "to %s (errno = %d)\n", 1782 ip6_sprintf(ip6buf, 1783 &opr->ndpr_prefix.sin6_addr), 1784 opr->ndpr_plen, if_name(ifp), 1785 if_name(opr->ndpr_ifp), e)); 1786 } 1787 } 1788 } 1789 } else { 1790 /* XXX: can we still set the NDPRF_ONLINK flag? */ 1791 nd6log((LOG_ERR, 1792 "nd6_prefix_offlink: failed to delete route: " 1793 "%s/%d on %s (errno = %d)\n", 1794 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen, 1795 if_name(ifp), error)); 1796 } 1797 1798 return (error); 1799} 1800 1801static struct in6_ifaddr * 1802in6_ifadd(struct nd_prefixctl *pr, int mcast) 1803{ 1804 struct ifnet *ifp = pr->ndpr_ifp; 1805 struct ifaddr *ifa; 1806 struct in6_aliasreq ifra; 1807 struct in6_ifaddr *ia, *ib; 1808 int error, plen0; 1809 struct in6_addr mask; 1810 int prefixlen = pr->ndpr_plen; 1811 int updateflags; 1812 char ip6buf[INET6_ADDRSTRLEN]; 1813 1814 in6_prefixlen2mask(&mask, prefixlen); 1815 1816 /* 1817 * find a link-local address (will be interface ID). 1818 * Is it really mandatory? Theoretically, a global or a site-local 1819 * address can be configured without a link-local address, if we 1820 * have a unique interface identifier... 1821 * 1822 * it is not mandatory to have a link-local address, we can generate 1823 * interface identifier on the fly. we do this because: 1824 * (1) it should be the easiest way to find interface identifier. 1825 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 1826 * for multiple addresses on a single interface, and possible shortcut 1827 * of DAD. we omitted DAD for this reason in the past. 1828 * (3) a user can prevent autoconfiguration of global address 1829 * by removing link-local address by hand (this is partly because we 1830 * don't have other way to control the use of IPv6 on an interface. 1831 * this has been our design choice - cf. NRL's "ifconfig auto"). 1832 * (4) it is easier to manage when an interface has addresses 1833 * with the same interface identifier, than to have multiple addresses 1834 * with different interface identifiers. 1835 */ 1836 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */ 1837 if (ifa) 1838 ib = (struct in6_ifaddr *)ifa; 1839 else 1840 return NULL; 1841 1842 /* prefixlen + ifidlen must be equal to 128 */ 1843 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 1844 if (prefixlen != plen0) { 1845 ifa_free(ifa); 1846 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 1847 "(prefix=%d ifid=%d)\n", 1848 if_name(ifp), prefixlen, 128 - plen0)); 1849 return NULL; 1850 } 1851 1852 /* make ifaddr */ 1853 1854 bzero(&ifra, sizeof(ifra)); 1855 /* 1856 * in6_update_ifa() does not use ifra_name, but we accurately set it 1857 * for safety. 1858 */ 1859 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1860 ifra.ifra_addr.sin6_family = AF_INET6; 1861 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 1862 /* prefix */ 1863 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr; 1864 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1865 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1866 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1867 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1868 1869 /* interface ID */ 1870 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 1871 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]); 1872 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 1873 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]); 1874 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1875 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]); 1876 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1877 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]); 1878 ifa_free(ifa); 1879 1880 /* new prefix mask. */ 1881 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1882 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1883 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 1884 sizeof(ifra.ifra_prefixmask.sin6_addr)); 1885 1886 /* lifetimes. */ 1887 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 1888 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 1889 1890 /* XXX: scope zone ID? */ 1891 1892 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 1893 1894 /* 1895 * Make sure that we do not have this address already. This should 1896 * usually not happen, but we can still see this case, e.g., if we 1897 * have manually configured the exact address to be configured. 1898 */ 1899 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 1900 &ifra.ifra_addr.sin6_addr); 1901 if (ifa != NULL) { 1902 ifa_free(ifa); 1903 /* this should be rare enough to make an explicit log */ 1904 log(LOG_INFO, "in6_ifadd: %s is already configured\n", 1905 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr)); 1906 return (NULL); 1907 } 1908 1909 /* 1910 * Allocate ifaddr structure, link into chain, etc. 1911 * If we are going to create a new address upon receiving a multicasted 1912 * RA, we need to impose a random delay before starting DAD. 1913 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2] 1914 */ 1915 updateflags = 0; 1916 if (mcast) 1917 updateflags |= IN6_IFAUPDATE_DADDELAY; 1918 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) { 1919 nd6log((LOG_ERR, 1920 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 1921 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr), 1922 if_name(ifp), error)); 1923 return (NULL); /* ifaddr must not have been allocated. */ 1924 } 1925 1926 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1927 /* 1928 * XXXRW: Assumption of non-NULLness here might not be true with 1929 * fine-grained locking -- should we validate it? Or just return 1930 * earlier ifa rather than looking it up again? 1931 */ 1932 return (ia); /* this is always non-NULL and referenced. */ 1933} 1934 1935/* 1936 * ia0 - corresponding public address 1937 */ 1938int 1939in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay) 1940{ 1941 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 1942 struct in6_ifaddr *newia, *ia; 1943 struct in6_aliasreq ifra; 1944 int i, error; 1945 int trylimit = 3; /* XXX: adhoc value */ 1946 int updateflags; 1947 u_int32_t randid[2]; 1948 time_t vltime0, pltime0; 1949 1950 bzero(&ifra, sizeof(ifra)); 1951 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1952 ifra.ifra_addr = ia0->ia_addr; 1953 /* copy prefix mask */ 1954 ifra.ifra_prefixmask = ia0->ia_prefixmask; 1955 /* clear the old IFID */ 1956 for (i = 0; i < 4; i++) { 1957 ifra.ifra_addr.sin6_addr.s6_addr32[i] &= 1958 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i]; 1959 } 1960 1961 again: 1962 if (in6_get_tmpifid(ifp, (u_int8_t *)randid, 1963 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) { 1964 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good " 1965 "random IFID\n")); 1966 return (EINVAL); 1967 } 1968 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1969 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 1970 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1971 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 1972 1973 /* 1974 * in6_get_tmpifid() quite likely provided a unique interface ID. 1975 * However, we may still have a chance to see collision, because 1976 * there may be a time lag between generation of the ID and generation 1977 * of the address. So, we'll do one more sanity check. 1978 */ 1979 IN6_IFADDR_RLOCK(); 1980 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1981 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1982 &ifra.ifra_addr.sin6_addr)) { 1983 if (trylimit-- == 0) { 1984 IN6_IFADDR_RUNLOCK(); 1985 /* 1986 * Give up. Something strange should have 1987 * happened. 1988 */ 1989 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to " 1990 "find a unique random IFID\n")); 1991 return (EEXIST); 1992 } 1993 IN6_IFADDR_RUNLOCK(); 1994 forcegen = 1; 1995 goto again; 1996 } 1997 } 1998 IN6_IFADDR_RUNLOCK(); 1999 2000 /* 2001 * The Valid Lifetime is the lower of the Valid Lifetime of the 2002 * public address or TEMP_VALID_LIFETIME. 2003 * The Preferred Lifetime is the lower of the Preferred Lifetime 2004 * of the public address or TEMP_PREFERRED_LIFETIME - 2005 * DESYNC_FACTOR. 2006 */ 2007 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 2008 vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 2009 (ia0->ia6_lifetime.ia6t_vltime - 2010 (time_second - ia0->ia6_updatetime)); 2011 if (vltime0 > V_ip6_temp_valid_lifetime) 2012 vltime0 = V_ip6_temp_valid_lifetime; 2013 } else 2014 vltime0 = V_ip6_temp_valid_lifetime; 2015 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 2016 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 2017 (ia0->ia6_lifetime.ia6t_pltime - 2018 (time_second - ia0->ia6_updatetime)); 2019 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){ 2020 pltime0 = V_ip6_temp_preferred_lifetime - 2021 V_ip6_desync_factor; 2022 } 2023 } else 2024 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor; 2025 ifra.ifra_lifetime.ia6t_vltime = vltime0; 2026 ifra.ifra_lifetime.ia6t_pltime = pltime0; 2027 2028 /* 2029 * A temporary address is created only if this calculated Preferred 2030 * Lifetime is greater than REGEN_ADVANCE time units. 2031 */ 2032 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance) 2033 return (0); 2034 2035 /* XXX: scope zone ID? */ 2036 2037 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 2038 2039 /* allocate ifaddr structure, link into chain, etc. */ 2040 updateflags = 0; 2041 if (delay) 2042 updateflags |= IN6_IFAUPDATE_DADDELAY; 2043 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) 2044 return (error); 2045 2046 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 2047 if (newia == NULL) { /* XXX: can it happen? */ 2048 nd6log((LOG_ERR, 2049 "in6_tmpifadd: ifa update succeeded, but we got " 2050 "no ifaddr\n")); 2051 return (EINVAL); /* XXX */ 2052 } 2053 newia->ia6_ndpr = ia0->ia6_ndpr; 2054 newia->ia6_ndpr->ndpr_refcnt++; 2055 ifa_free(&newia->ia_ifa); 2056 2057 /* 2058 * A newly added address might affect the status of other addresses. 2059 * XXX: when the temporary address is generated with a new public 2060 * address, the onlink check is redundant. However, it would be safe 2061 * to do the check explicitly everywhere a new address is generated, 2062 * and, in fact, we surely need the check when we create a new 2063 * temporary address due to deprecation of an old temporary address. 2064 */ 2065 pfxlist_onlink_check(); 2066 2067 return (0); 2068} 2069 2070static int 2071in6_init_prefix_ltimes(struct nd_prefix *ndpr) 2072{ 2073 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 2074 ndpr->ndpr_preferred = 0; 2075 else 2076 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 2077 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2078 ndpr->ndpr_expire = 0; 2079 else 2080 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 2081 2082 return 0; 2083} 2084 2085static void 2086in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 2087{ 2088 /* init ia6t_expire */ 2089 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 2090 lt6->ia6t_expire = 0; 2091 else { 2092 lt6->ia6t_expire = time_second; 2093 lt6->ia6t_expire += lt6->ia6t_vltime; 2094 } 2095 2096 /* init ia6t_preferred */ 2097 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 2098 lt6->ia6t_preferred = 0; 2099 else { 2100 lt6->ia6t_preferred = time_second; 2101 lt6->ia6t_preferred += lt6->ia6t_pltime; 2102 } 2103} 2104 2105/* 2106 * Delete all the routing table entries that use the specified gateway. 2107 * XXX: this function causes search through all entries of routing table, so 2108 * it shouldn't be called when acting as a router. 2109 */ 2110void 2111rt6_flush(struct in6_addr *gateway, struct ifnet *ifp) 2112{ 2113 struct radix_node_head *rnh; 2114 u_int fibnum;
| 1288 return error; 1289} 1290 1291/* 1292 * A supplement function used in the on-link detection below; 1293 * detect if a given prefix has a (probably) reachable advertising router. 1294 * XXX: lengthy function name... 1295 */ 1296static struct nd_pfxrouter * 1297find_pfxlist_reachable_router(struct nd_prefix *pr) 1298{ 1299 struct nd_pfxrouter *pfxrtr; 1300 struct llentry *ln; 1301 int canreach; 1302 1303 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) { 1304 IF_AFDATA_LOCK(pfxrtr->router->ifp); 1305 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp); 1306 IF_AFDATA_UNLOCK(pfxrtr->router->ifp); 1307 if (ln == NULL) 1308 continue; 1309 canreach = ND6_IS_LLINFO_PROBREACH(ln); 1310 LLE_RUNLOCK(ln); 1311 if (canreach) 1312 break; 1313 } 1314 return (pfxrtr); 1315} 1316 1317/* 1318 * Check if each prefix in the prefix list has at least one available router 1319 * that advertised the prefix (a router is "available" if its neighbor cache 1320 * entry is reachable or probably reachable). 1321 * If the check fails, the prefix may be off-link, because, for example, 1322 * we have moved from the network but the lifetime of the prefix has not 1323 * expired yet. So we should not use the prefix if there is another prefix 1324 * that has an available router. 1325 * But, if there is no prefix that has an available router, we still regards 1326 * all the prefixes as on-link. This is because we can't tell if all the 1327 * routers are simply dead or if we really moved from the network and there 1328 * is no router around us. 1329 */ 1330void 1331pfxlist_onlink_check() 1332{ 1333 struct nd_prefix *pr; 1334 struct in6_ifaddr *ifa; 1335 struct nd_defrouter *dr; 1336 struct nd_pfxrouter *pfxrtr = NULL; 1337 1338 /* 1339 * Check if there is a prefix that has a reachable advertising 1340 * router. 1341 */ 1342 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1343 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1344 break; 1345 } 1346 1347 /* 1348 * If we have no such prefix, check whether we still have a router 1349 * that does not advertise any prefixes. 1350 */ 1351 if (pr == NULL) { 1352 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 1353 struct nd_prefix *pr0; 1354 1355 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) { 1356 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL) 1357 break; 1358 } 1359 if (pfxrtr != NULL) 1360 break; 1361 } 1362 } 1363 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) { 1364 /* 1365 * There is at least one prefix that has a reachable router, 1366 * or at least a router which probably does not advertise 1367 * any prefixes. The latter would be the case when we move 1368 * to a new link where we have a router that does not provide 1369 * prefixes and we configure an address by hand. 1370 * Detach prefixes which have no reachable advertising 1371 * router, and attach other prefixes. 1372 */ 1373 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1374 /* XXX: a link-local prefix should never be detached */ 1375 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1376 continue; 1377 1378 /* 1379 * we aren't interested in prefixes without the L bit 1380 * set. 1381 */ 1382 if (pr->ndpr_raf_onlink == 0) 1383 continue; 1384 1385 if (pr->ndpr_raf_auto == 0) 1386 continue; 1387 1388 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1389 find_pfxlist_reachable_router(pr) == NULL) 1390 pr->ndpr_stateflags |= NDPRF_DETACHED; 1391 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1392 find_pfxlist_reachable_router(pr) != 0) 1393 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1394 } 1395 } else { 1396 /* there is no prefix that has a reachable router */ 1397 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1398 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1399 continue; 1400 1401 if (pr->ndpr_raf_onlink == 0) 1402 continue; 1403 1404 if (pr->ndpr_raf_auto == 0) 1405 continue; 1406 1407 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1408 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1409 } 1410 } 1411 1412 /* 1413 * Remove each interface route associated with a (just) detached 1414 * prefix, and reinstall the interface route for a (just) attached 1415 * prefix. Note that all attempt of reinstallation does not 1416 * necessarily success, when a same prefix is shared among multiple 1417 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1418 * so we don't have to care about them. 1419 */ 1420 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1421 int e; 1422 char ip6buf[INET6_ADDRSTRLEN]; 1423 1424 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1425 continue; 1426 1427 if (pr->ndpr_raf_onlink == 0) 1428 continue; 1429 1430 if (pr->ndpr_raf_auto == 0) 1431 continue; 1432 1433 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1434 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1435 if ((e = nd6_prefix_offlink(pr)) != 0) { 1436 nd6log((LOG_ERR, 1437 "pfxlist_onlink_check: failed to " 1438 "make %s/%d offlink, errno=%d\n", 1439 ip6_sprintf(ip6buf, 1440 &pr->ndpr_prefix.sin6_addr), 1441 pr->ndpr_plen, e)); 1442 } 1443 } 1444 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1445 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 1446 pr->ndpr_raf_onlink) { 1447 if ((e = nd6_prefix_onlink(pr)) != 0) { 1448 nd6log((LOG_ERR, 1449 "pfxlist_onlink_check: failed to " 1450 "make %s/%d onlink, errno=%d\n", 1451 ip6_sprintf(ip6buf, 1452 &pr->ndpr_prefix.sin6_addr), 1453 pr->ndpr_plen, e)); 1454 } 1455 } 1456 } 1457 1458 /* 1459 * Changes on the prefix status might affect address status as well. 1460 * Make sure that all addresses derived from an attached prefix are 1461 * attached, and that all addresses derived from a detached prefix are 1462 * detached. Note, however, that a manually configured address should 1463 * always be attached. 1464 * The precise detection logic is same as the one for prefixes. 1465 * 1466 * XXXRW: in6_ifaddrhead locking. 1467 */ 1468 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1469 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF)) 1470 continue; 1471 1472 if (ifa->ia6_ndpr == NULL) { 1473 /* 1474 * This can happen when we first configure the address 1475 * (i.e. the address exists, but the prefix does not). 1476 * XXX: complicated relationships... 1477 */ 1478 continue; 1479 } 1480 1481 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1482 break; 1483 } 1484 if (ifa) { 1485 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1486 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1487 continue; 1488 1489 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 1490 continue; 1491 1492 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) { 1493 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1494 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1495 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1496 nd6_dad_start((struct ifaddr *)ifa, 0); 1497 } 1498 } else { 1499 ifa->ia6_flags |= IN6_IFF_DETACHED; 1500 } 1501 } 1502 } 1503 else { 1504 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1505 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1506 continue; 1507 1508 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1509 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1510 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1511 /* Do we need a delay in this case? */ 1512 nd6_dad_start((struct ifaddr *)ifa, 0); 1513 } 1514 } 1515 } 1516} 1517 1518static int 1519nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa) 1520{ 1521 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1522 struct radix_node_head *rnh; 1523 struct rtentry *rt; 1524 struct sockaddr_in6 mask6; 1525 u_long rtflags; 1526 int error, a_failure, fibnum; 1527 1528 /* 1529 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1530 * ifa->ifa_rtrequest = nd6_rtrequest; 1531 */ 1532 bzero(&mask6, sizeof(mask6)); 1533 mask6.sin6_len = sizeof(mask6); 1534 mask6.sin6_addr = pr->ndpr_mask; 1535 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP; 1536 1537 a_failure = 0; 1538 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1539 1540 rt = NULL; 1541 error = in6_rtrequest(RTM_ADD, 1542 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr, 1543 (struct sockaddr *)&mask6, rtflags, &rt, fibnum); 1544 if (error == 0) { 1545 KASSERT(rt != NULL, ("%s: in6_rtrequest return no " 1546 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__, 1547 error, pr, ifa)); 1548 1549 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6); 1550 /* XXX what if rhn == NULL? */ 1551 RADIX_NODE_HEAD_LOCK(rnh); 1552 RT_LOCK(rt); 1553 if (rt_setgate(rt, rt_key(rt), 1554 (struct sockaddr *)&null_sdl) == 0) { 1555 struct sockaddr_dl *dl; 1556 1557 dl = (struct sockaddr_dl *)rt->rt_gateway; 1558 dl->sdl_type = rt->rt_ifp->if_type; 1559 dl->sdl_index = rt->rt_ifp->if_index; 1560 } 1561 RADIX_NODE_HEAD_UNLOCK(rnh); 1562 nd6_rtmsg(RTM_ADD, rt); 1563 RT_UNLOCK(rt); 1564 pr->ndpr_stateflags |= NDPRF_ONLINK; 1565 } else { 1566 char ip6buf[INET6_ADDRSTRLEN]; 1567 char ip6bufg[INET6_ADDRSTRLEN]; 1568 char ip6bufm[INET6_ADDRSTRLEN]; 1569 struct sockaddr_in6 *sin6; 1570 1571 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1572 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add " 1573 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, " 1574 "flags=%lx errno = %d\n", 1575 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1576 pr->ndpr_plen, if_name(pr->ndpr_ifp), 1577 ip6_sprintf(ip6bufg, &sin6->sin6_addr), 1578 ip6_sprintf(ip6bufm, &mask6.sin6_addr), 1579 rtflags, error)); 1580 1581 /* Save last error to return, see rtinit(). */ 1582 a_failure = error; 1583 } 1584 1585 if (rt != NULL) { 1586 RT_LOCK(rt); 1587 RT_REMREF(rt); 1588 RT_UNLOCK(rt); 1589 } 1590 } 1591 1592 /* Return the last error we got. */ 1593 return (a_failure); 1594} 1595 1596static int 1597nd6_prefix_onlink(struct nd_prefix *pr) 1598{ 1599 struct ifaddr *ifa; 1600 struct ifnet *ifp = pr->ndpr_ifp; 1601 struct nd_prefix *opr; 1602 int error = 0; 1603 char ip6buf[INET6_ADDRSTRLEN]; 1604 1605 /* sanity check */ 1606 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1607 nd6log((LOG_ERR, 1608 "nd6_prefix_onlink: %s/%d is already on-link\n", 1609 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1610 pr->ndpr_plen)); 1611 return (EEXIST); 1612 } 1613 1614 /* 1615 * Add the interface route associated with the prefix. Before 1616 * installing the route, check if there's the same prefix on another 1617 * interface, and the prefix has already installed the interface route. 1618 * Although such a configuration is expected to be rare, we explicitly 1619 * allow it. 1620 */ 1621 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 1622 if (opr == pr) 1623 continue; 1624 1625 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1626 continue; 1627 1628 if (opr->ndpr_plen == pr->ndpr_plen && 1629 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1630 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) 1631 return (0); 1632 } 1633 1634 /* 1635 * We prefer link-local addresses as the associated interface address. 1636 */ 1637 /* search for a link-local addr */ 1638 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 1639 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST); 1640 if (ifa == NULL) { 1641 /* XXX: freebsd does not have ifa_ifwithaf */ 1642 IF_ADDR_RLOCK(ifp); 1643 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1644 if (ifa->ifa_addr->sa_family == AF_INET6) 1645 break; 1646 } 1647 if (ifa != NULL) 1648 ifa_ref(ifa); 1649 IF_ADDR_RUNLOCK(ifp); 1650 /* should we care about ia6_flags? */ 1651 } 1652 if (ifa == NULL) { 1653 /* 1654 * This can still happen, when, for example, we receive an RA 1655 * containing a prefix with the L bit set and the A bit clear, 1656 * after removing all IPv6 addresses on the receiving 1657 * interface. This should, of course, be rare though. 1658 */ 1659 nd6log((LOG_NOTICE, 1660 "nd6_prefix_onlink: failed to find any ifaddr" 1661 " to add route for a prefix(%s/%d) on %s\n", 1662 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1663 pr->ndpr_plen, if_name(ifp))); 1664 return (0); 1665 } 1666 1667 error = nd6_prefix_onlink_rtrequest(pr, ifa); 1668 1669 if (ifa != NULL) 1670 ifa_free(ifa); 1671 1672 return (error); 1673} 1674 1675static int 1676nd6_prefix_offlink(struct nd_prefix *pr) 1677{ 1678 int error = 0; 1679 struct ifnet *ifp = pr->ndpr_ifp; 1680 struct nd_prefix *opr; 1681 struct sockaddr_in6 sa6, mask6; 1682 struct rtentry *rt; 1683 char ip6buf[INET6_ADDRSTRLEN]; 1684 int fibnum, a_failure; 1685 1686 /* sanity check */ 1687 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1688 nd6log((LOG_ERR, 1689 "nd6_prefix_offlink: %s/%d is already off-link\n", 1690 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1691 pr->ndpr_plen)); 1692 return (EEXIST); 1693 } 1694 1695 bzero(&sa6, sizeof(sa6)); 1696 sa6.sin6_family = AF_INET6; 1697 sa6.sin6_len = sizeof(sa6); 1698 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1699 sizeof(struct in6_addr)); 1700 bzero(&mask6, sizeof(mask6)); 1701 mask6.sin6_family = AF_INET6; 1702 mask6.sin6_len = sizeof(sa6); 1703 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1704 1705 a_failure = 0; 1706 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1707 rt = NULL; 1708 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 1709 (struct sockaddr *)&mask6, 0, &rt, fibnum); 1710 if (error == 0) { 1711 /* report the route deletion to the routing socket. */ 1712 if (rt != NULL) 1713 nd6_rtmsg(RTM_DELETE, rt); 1714 } else { 1715 /* Save last error to return, see rtinit(). */ 1716 a_failure = error; 1717 } 1718 if (rt != NULL) { 1719 RTFREE(rt); 1720 } 1721 } 1722 error = a_failure; 1723 if (error == 0) { 1724 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 1725 1726 /* 1727 * There might be the same prefix on another interface, 1728 * the prefix which could not be on-link just because we have 1729 * the interface route (see comments in nd6_prefix_onlink). 1730 * If there's one, try to make the prefix on-link on the 1731 * interface. 1732 */ 1733 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 1734 if (opr == pr) 1735 continue; 1736 1737 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1738 continue; 1739 1740 /* 1741 * KAME specific: detached prefixes should not be 1742 * on-link. 1743 */ 1744 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1745 continue; 1746 1747 if (opr->ndpr_plen == pr->ndpr_plen && 1748 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1749 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 1750 int e; 1751 1752 if ((e = nd6_prefix_onlink(opr)) != 0) { 1753 nd6log((LOG_ERR, 1754 "nd6_prefix_offlink: failed to " 1755 "recover a prefix %s/%d from %s " 1756 "to %s (errno = %d)\n", 1757 ip6_sprintf(ip6buf, 1758 &opr->ndpr_prefix.sin6_addr), 1759 opr->ndpr_plen, if_name(ifp), 1760 if_name(opr->ndpr_ifp), e)); 1761 } 1762 } 1763 } 1764 } else { 1765 /* XXX: can we still set the NDPRF_ONLINK flag? */ 1766 nd6log((LOG_ERR, 1767 "nd6_prefix_offlink: failed to delete route: " 1768 "%s/%d on %s (errno = %d)\n", 1769 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen, 1770 if_name(ifp), error)); 1771 } 1772 1773 return (error); 1774} 1775 1776static struct in6_ifaddr * 1777in6_ifadd(struct nd_prefixctl *pr, int mcast) 1778{ 1779 struct ifnet *ifp = pr->ndpr_ifp; 1780 struct ifaddr *ifa; 1781 struct in6_aliasreq ifra; 1782 struct in6_ifaddr *ia, *ib; 1783 int error, plen0; 1784 struct in6_addr mask; 1785 int prefixlen = pr->ndpr_plen; 1786 int updateflags; 1787 char ip6buf[INET6_ADDRSTRLEN]; 1788 1789 in6_prefixlen2mask(&mask, prefixlen); 1790 1791 /* 1792 * find a link-local address (will be interface ID). 1793 * Is it really mandatory? Theoretically, a global or a site-local 1794 * address can be configured without a link-local address, if we 1795 * have a unique interface identifier... 1796 * 1797 * it is not mandatory to have a link-local address, we can generate 1798 * interface identifier on the fly. we do this because: 1799 * (1) it should be the easiest way to find interface identifier. 1800 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 1801 * for multiple addresses on a single interface, and possible shortcut 1802 * of DAD. we omitted DAD for this reason in the past. 1803 * (3) a user can prevent autoconfiguration of global address 1804 * by removing link-local address by hand (this is partly because we 1805 * don't have other way to control the use of IPv6 on an interface. 1806 * this has been our design choice - cf. NRL's "ifconfig auto"). 1807 * (4) it is easier to manage when an interface has addresses 1808 * with the same interface identifier, than to have multiple addresses 1809 * with different interface identifiers. 1810 */ 1811 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */ 1812 if (ifa) 1813 ib = (struct in6_ifaddr *)ifa; 1814 else 1815 return NULL; 1816 1817 /* prefixlen + ifidlen must be equal to 128 */ 1818 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 1819 if (prefixlen != plen0) { 1820 ifa_free(ifa); 1821 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 1822 "(prefix=%d ifid=%d)\n", 1823 if_name(ifp), prefixlen, 128 - plen0)); 1824 return NULL; 1825 } 1826 1827 /* make ifaddr */ 1828 1829 bzero(&ifra, sizeof(ifra)); 1830 /* 1831 * in6_update_ifa() does not use ifra_name, but we accurately set it 1832 * for safety. 1833 */ 1834 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1835 ifra.ifra_addr.sin6_family = AF_INET6; 1836 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 1837 /* prefix */ 1838 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr; 1839 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1840 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1841 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1842 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1843 1844 /* interface ID */ 1845 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 1846 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]); 1847 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 1848 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]); 1849 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1850 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]); 1851 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1852 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]); 1853 ifa_free(ifa); 1854 1855 /* new prefix mask. */ 1856 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1857 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1858 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 1859 sizeof(ifra.ifra_prefixmask.sin6_addr)); 1860 1861 /* lifetimes. */ 1862 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 1863 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 1864 1865 /* XXX: scope zone ID? */ 1866 1867 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 1868 1869 /* 1870 * Make sure that we do not have this address already. This should 1871 * usually not happen, but we can still see this case, e.g., if we 1872 * have manually configured the exact address to be configured. 1873 */ 1874 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 1875 &ifra.ifra_addr.sin6_addr); 1876 if (ifa != NULL) { 1877 ifa_free(ifa); 1878 /* this should be rare enough to make an explicit log */ 1879 log(LOG_INFO, "in6_ifadd: %s is already configured\n", 1880 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr)); 1881 return (NULL); 1882 } 1883 1884 /* 1885 * Allocate ifaddr structure, link into chain, etc. 1886 * If we are going to create a new address upon receiving a multicasted 1887 * RA, we need to impose a random delay before starting DAD. 1888 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2] 1889 */ 1890 updateflags = 0; 1891 if (mcast) 1892 updateflags |= IN6_IFAUPDATE_DADDELAY; 1893 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) { 1894 nd6log((LOG_ERR, 1895 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 1896 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr), 1897 if_name(ifp), error)); 1898 return (NULL); /* ifaddr must not have been allocated. */ 1899 } 1900 1901 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1902 /* 1903 * XXXRW: Assumption of non-NULLness here might not be true with 1904 * fine-grained locking -- should we validate it? Or just return 1905 * earlier ifa rather than looking it up again? 1906 */ 1907 return (ia); /* this is always non-NULL and referenced. */ 1908} 1909 1910/* 1911 * ia0 - corresponding public address 1912 */ 1913int 1914in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay) 1915{ 1916 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 1917 struct in6_ifaddr *newia, *ia; 1918 struct in6_aliasreq ifra; 1919 int i, error; 1920 int trylimit = 3; /* XXX: adhoc value */ 1921 int updateflags; 1922 u_int32_t randid[2]; 1923 time_t vltime0, pltime0; 1924 1925 bzero(&ifra, sizeof(ifra)); 1926 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1927 ifra.ifra_addr = ia0->ia_addr; 1928 /* copy prefix mask */ 1929 ifra.ifra_prefixmask = ia0->ia_prefixmask; 1930 /* clear the old IFID */ 1931 for (i = 0; i < 4; i++) { 1932 ifra.ifra_addr.sin6_addr.s6_addr32[i] &= 1933 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i]; 1934 } 1935 1936 again: 1937 if (in6_get_tmpifid(ifp, (u_int8_t *)randid, 1938 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) { 1939 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good " 1940 "random IFID\n")); 1941 return (EINVAL); 1942 } 1943 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1944 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 1945 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1946 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 1947 1948 /* 1949 * in6_get_tmpifid() quite likely provided a unique interface ID. 1950 * However, we may still have a chance to see collision, because 1951 * there may be a time lag between generation of the ID and generation 1952 * of the address. So, we'll do one more sanity check. 1953 */ 1954 IN6_IFADDR_RLOCK(); 1955 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1956 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1957 &ifra.ifra_addr.sin6_addr)) { 1958 if (trylimit-- == 0) { 1959 IN6_IFADDR_RUNLOCK(); 1960 /* 1961 * Give up. Something strange should have 1962 * happened. 1963 */ 1964 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to " 1965 "find a unique random IFID\n")); 1966 return (EEXIST); 1967 } 1968 IN6_IFADDR_RUNLOCK(); 1969 forcegen = 1; 1970 goto again; 1971 } 1972 } 1973 IN6_IFADDR_RUNLOCK(); 1974 1975 /* 1976 * The Valid Lifetime is the lower of the Valid Lifetime of the 1977 * public address or TEMP_VALID_LIFETIME. 1978 * The Preferred Lifetime is the lower of the Preferred Lifetime 1979 * of the public address or TEMP_PREFERRED_LIFETIME - 1980 * DESYNC_FACTOR. 1981 */ 1982 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 1983 vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 1984 (ia0->ia6_lifetime.ia6t_vltime - 1985 (time_second - ia0->ia6_updatetime)); 1986 if (vltime0 > V_ip6_temp_valid_lifetime) 1987 vltime0 = V_ip6_temp_valid_lifetime; 1988 } else 1989 vltime0 = V_ip6_temp_valid_lifetime; 1990 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 1991 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 1992 (ia0->ia6_lifetime.ia6t_pltime - 1993 (time_second - ia0->ia6_updatetime)); 1994 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){ 1995 pltime0 = V_ip6_temp_preferred_lifetime - 1996 V_ip6_desync_factor; 1997 } 1998 } else 1999 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor; 2000 ifra.ifra_lifetime.ia6t_vltime = vltime0; 2001 ifra.ifra_lifetime.ia6t_pltime = pltime0; 2002 2003 /* 2004 * A temporary address is created only if this calculated Preferred 2005 * Lifetime is greater than REGEN_ADVANCE time units. 2006 */ 2007 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance) 2008 return (0); 2009 2010 /* XXX: scope zone ID? */ 2011 2012 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 2013 2014 /* allocate ifaddr structure, link into chain, etc. */ 2015 updateflags = 0; 2016 if (delay) 2017 updateflags |= IN6_IFAUPDATE_DADDELAY; 2018 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) 2019 return (error); 2020 2021 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 2022 if (newia == NULL) { /* XXX: can it happen? */ 2023 nd6log((LOG_ERR, 2024 "in6_tmpifadd: ifa update succeeded, but we got " 2025 "no ifaddr\n")); 2026 return (EINVAL); /* XXX */ 2027 } 2028 newia->ia6_ndpr = ia0->ia6_ndpr; 2029 newia->ia6_ndpr->ndpr_refcnt++; 2030 ifa_free(&newia->ia_ifa); 2031 2032 /* 2033 * A newly added address might affect the status of other addresses. 2034 * XXX: when the temporary address is generated with a new public 2035 * address, the onlink check is redundant. However, it would be safe 2036 * to do the check explicitly everywhere a new address is generated, 2037 * and, in fact, we surely need the check when we create a new 2038 * temporary address due to deprecation of an old temporary address. 2039 */ 2040 pfxlist_onlink_check(); 2041 2042 return (0); 2043} 2044 2045static int 2046in6_init_prefix_ltimes(struct nd_prefix *ndpr) 2047{ 2048 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 2049 ndpr->ndpr_preferred = 0; 2050 else 2051 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 2052 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2053 ndpr->ndpr_expire = 0; 2054 else 2055 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 2056 2057 return 0; 2058} 2059 2060static void 2061in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 2062{ 2063 /* init ia6t_expire */ 2064 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 2065 lt6->ia6t_expire = 0; 2066 else { 2067 lt6->ia6t_expire = time_second; 2068 lt6->ia6t_expire += lt6->ia6t_vltime; 2069 } 2070 2071 /* init ia6t_preferred */ 2072 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 2073 lt6->ia6t_preferred = 0; 2074 else { 2075 lt6->ia6t_preferred = time_second; 2076 lt6->ia6t_preferred += lt6->ia6t_pltime; 2077 } 2078} 2079 2080/* 2081 * Delete all the routing table entries that use the specified gateway. 2082 * XXX: this function causes search through all entries of routing table, so 2083 * it shouldn't be called when acting as a router. 2084 */ 2085void 2086rt6_flush(struct in6_addr *gateway, struct ifnet *ifp) 2087{ 2088 struct radix_node_head *rnh; 2089 u_int fibnum;
|
2115 int s = splnet();
| |
2116 2117 /* We'll care only link-local addresses */
| 2090 2091 /* We'll care only link-local addresses */
|
2118 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 2119 splx(s);
| 2092 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
|
2120 return;
| 2093 return;
|
2121 }
| |
2122 2123 /* XXX Do we really need to walk any but the default FIB? */ 2124 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 2125 rnh = rt_tables_get_rnh(fibnum, AF_INET6); 2126 if (rnh == NULL) 2127 continue; 2128 2129 RADIX_NODE_HEAD_LOCK(rnh); 2130 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 2131 RADIX_NODE_HEAD_UNLOCK(rnh); 2132 }
| 2094 2095 /* XXX Do we really need to walk any but the default FIB? */ 2096 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 2097 rnh = rt_tables_get_rnh(fibnum, AF_INET6); 2098 if (rnh == NULL) 2099 continue; 2100 2101 RADIX_NODE_HEAD_LOCK(rnh); 2102 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 2103 RADIX_NODE_HEAD_UNLOCK(rnh); 2104 }
|
2133 splx(s);
| |
2134} 2135 2136static int 2137rt6_deleteroute(struct radix_node *rn, void *arg) 2138{ 2139#define SIN6(s) ((struct sockaddr_in6 *)s) 2140 struct rtentry *rt = (struct rtentry *)rn; 2141 struct in6_addr *gate = (struct in6_addr *)arg; 2142 2143 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 2144 return (0); 2145 2146 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) { 2147 return (0); 2148 } 2149 2150 /* 2151 * Do not delete a static route. 2152 * XXX: this seems to be a bit ad-hoc. Should we consider the 2153 * 'cloned' bit instead? 2154 */ 2155 if ((rt->rt_flags & RTF_STATIC) != 0) 2156 return (0); 2157 2158 /* 2159 * We delete only host route. This means, in particular, we don't 2160 * delete default route. 2161 */ 2162 if ((rt->rt_flags & RTF_HOST) == 0) 2163 return (0); 2164 2165 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 2166 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum)); 2167#undef SIN6 2168} 2169 2170int 2171nd6_setdefaultiface(int ifindex) 2172{ 2173 int error = 0; 2174 2175 if (ifindex < 0 || V_if_index < ifindex) 2176 return (EINVAL); 2177 if (ifindex != 0 && !ifnet_byindex(ifindex)) 2178 return (EINVAL); 2179 2180 if (V_nd6_defifindex != ifindex) { 2181 V_nd6_defifindex = ifindex; 2182 if (V_nd6_defifindex > 0) 2183 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex); 2184 else 2185 V_nd6_defifp = NULL; 2186 2187 /* 2188 * Our current implementation assumes one-to-one maping between 2189 * interfaces and links, so it would be natural to use the 2190 * default interface as the default link. 2191 */ 2192 scope6_setdefault(V_nd6_defifp); 2193 } 2194 2195 return (error); 2196}
| 2105} 2106 2107static int 2108rt6_deleteroute(struct radix_node *rn, void *arg) 2109{ 2110#define SIN6(s) ((struct sockaddr_in6 *)s) 2111 struct rtentry *rt = (struct rtentry *)rn; 2112 struct in6_addr *gate = (struct in6_addr *)arg; 2113 2114 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 2115 return (0); 2116 2117 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) { 2118 return (0); 2119 } 2120 2121 /* 2122 * Do not delete a static route. 2123 * XXX: this seems to be a bit ad-hoc. Should we consider the 2124 * 'cloned' bit instead? 2125 */ 2126 if ((rt->rt_flags & RTF_STATIC) != 0) 2127 return (0); 2128 2129 /* 2130 * We delete only host route. This means, in particular, we don't 2131 * delete default route. 2132 */ 2133 if ((rt->rt_flags & RTF_HOST) == 0) 2134 return (0); 2135 2136 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 2137 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum)); 2138#undef SIN6 2139} 2140 2141int 2142nd6_setdefaultiface(int ifindex) 2143{ 2144 int error = 0; 2145 2146 if (ifindex < 0 || V_if_index < ifindex) 2147 return (EINVAL); 2148 if (ifindex != 0 && !ifnet_byindex(ifindex)) 2149 return (EINVAL); 2150 2151 if (V_nd6_defifindex != ifindex) { 2152 V_nd6_defifindex = ifindex; 2153 if (V_nd6_defifindex > 0) 2154 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex); 2155 else 2156 V_nd6_defifp = NULL; 2157 2158 /* 2159 * Our current implementation assumes one-to-one maping between 2160 * interfaces and links, so it would be natural to use the 2161 * default interface as the default link. 2162 */ 2163 scope6_setdefault(V_nd6_defifp); 2164 } 2165 2166 return (error); 2167}
|