1 /*      $FreeBSD: src/sys/netinet6/nd6_rtr.c,v 1.11 2002/04/19 04:46:23 suz Exp $       */ 
   2 /*      $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $       */ 
   5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 
   8  * Redistribution and use in source and binary forms, with or without 
   9  * modification, are permitted provided that the following conditions 
  11  * 1. Redistributions of source code must retain the above copyright 
  12  *    notice, this list of conditions and the following disclaimer. 
  13  * 2. Redistributions in binary form must reproduce the above copyright 
  14  *    notice, this list of conditions and the following disclaimer in the 
  15  *    documentation and/or other materials provided with the distribution. 
  16  * 3. Neither the name of the project nor the names of its contributors 
  17  *    may be used to endorse or promote products derived from this software 
  18  *    without specific prior written permission. 
  20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  34 #include <sys/param.h> 
  35 #include <sys/systm.h> 
  36 #include <sys/malloc.h> 
  38 #include <sys/socket.h> 
  39 #include <sys/sockio.h> 
  41 #include <sys/kernel.h> 
  42 #include <sys/errno.h> 
  43 #include <sys/syslog.h> 
  44 #include <sys/queue.h> 
  45 #include <kern/lock.h> 
  48 #include <net/if_types.h> 
  49 #include <net/if_dl.h> 
  50 #include <net/route.h> 
  51 #include <net/radix.h> 
  53 #include <netinet/in.h> 
  54 #include <netinet6/in6_var.h> 
  55 #include <netinet6/in6_ifattach.h> 
  56 #include <netinet/ip6.h> 
  57 #include <netinet6/ip6_var.h> 
  58 #include <netinet6/nd6.h> 
  59 #include <netinet/icmp6.h> 
  60 #include <netinet6/scope6_var.h> 
  62 #include <net/net_osdep.h> 
  64 #define SDL(s)  ((struct sockaddr_dl *)s) 
  66 static struct nd_defrouter 
*defrtrlist_update(struct nd_defrouter 
*); 
  67 static struct in6_ifaddr 
*in6_ifadd(struct nd_prefix 
*, 
  69 static struct nd_pfxrouter 
*pfxrtr_lookup(struct nd_prefix 
*, 
  70         struct nd_defrouter 
*); 
  71 static void pfxrtr_add(struct nd_prefix 
*, struct nd_defrouter 
*); 
  72 static void pfxrtr_del(struct nd_pfxrouter 
*); 
  73 static struct nd_pfxrouter 
*find_pfxlist_reachable_router(struct nd_prefix 
*); 
  74 static void defrouter_addifreq(struct ifnet 
*); 
  75 static void nd6_rtmsg(int, struct rtentry 
*); 
  77 static void in6_init_address_ltimes(struct nd_prefix 
*ndpr
, 
  78                                          struct in6_addrlifetime 
*lt6
); 
  80 static int rt6_deleteroute(struct radix_node 
*, void *); 
  82 extern int nd6_recalc_reachtm_interval
; 
  84 static struct ifnet 
*nd6_defifp
; 
  87 int ip6_use_tempaddr 
= 0; 
  89 int ip6_desync_factor
; 
  90 u_int32_t ip6_temp_preferred_lifetime 
= DEF_TEMP_PREFERRED_LIFETIME
; 
  91 u_int32_t ip6_temp_valid_lifetime 
= DEF_TEMP_VALID_LIFETIME
; 
  93  * shorter lifetimes for debugging purposes. 
  94 int ip6_temp_preferred_lifetime = 800; 
  95 static int ip6_temp_valid_lifetime = 1800; 
  97 int ip6_temp_regen_advance 
= TEMPADDR_REGEN_ADVANCE
; 
  99 extern lck_mtx_t 
*rt_mtx
; 
 100 extern lck_mtx_t 
*nd6_mutex
; 
 103  * Receive Router Solicitation Message - just for routers. 
 104  * Router solicitation/advertisement is mostly managed by userland program 
 105  * (rtadvd) so here we have no function like nd6_ra_output(). 
 115         struct ifnet 
*ifp 
= m
->m_pkthdr
.rcvif
; 
 116         struct ip6_hdr 
*ip6 
= mtod(m
, struct ip6_hdr 
*); 
 117         struct nd_router_solicit 
*nd_rs
; 
 118         struct in6_addr saddr6 
= ip6
->ip6_src
; 
 120         struct in6_addr daddr6 
= ip6
->ip6_dst
; 
 125         struct sockaddr_dl 
*sdl 
= (struct sockaddr_dl 
*)NULL
; 
 126         struct llinfo_nd6 
*ln 
= (struct llinfo_nd6 
*)NULL
; 
 127         struct rtentry 
*rt 
= NULL
; 
 130         union nd_opts ndopts
; 
 132         /* If I'm not a router, ignore it. */ 
 133         if (ip6_accept_rtadv 
!= 0 || (ifp
->if_eflags 
& IFEF_ACCEPT_RTADVD
) || ip6_forwarding 
!= 1) 
 137         if (ip6
->ip6_hlim 
!= 255) { 
 139                     "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 
 140                     ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
), 
 141                     ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
))); 
 146          * Don't update the neighbor cache, if src = ::. 
 147          * This indicates that the src has no IP address assigned yet. 
 149         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) 
 152 #ifndef PULLDOWN_TEST 
 153         IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return); 
 154         nd_rs 
= (struct nd_router_solicit 
*)((caddr_t
)ip6 
+ off
); 
 156         IP6_EXTHDR_GET(nd_rs
, struct nd_router_solicit 
*, m
, off
, icmp6len
); 
 158                 icmp6stat
.icp6s_tooshort
++; 
 163         icmp6len 
-= sizeof(*nd_rs
); 
 164         nd6_option_init(nd_rs 
+ 1, icmp6len
, &ndopts
); 
 165         if (nd6_options(&ndopts
) < 0) { 
 167                     "nd6_rs_input: invalid ND option, ignored\n")); 
 168                 /* nd6_options have incremented stats */ 
 172         if (ndopts
.nd_opts_src_lladdr
) { 
 173                 lladdr 
= (char *)(ndopts
.nd_opts_src_lladdr 
+ 1); 
 174                 lladdrlen 
= ndopts
.nd_opts_src_lladdr
->nd_opt_len 
<< 3; 
 177         if (lladdr 
&& ((ifp
->if_addrlen 
+ 2 + 7) & ~7) != lladdrlen
) { 
 179                     "nd6_rs_input: lladdrlen mismatch for %s " 
 180                     "(if %d, RS packet %d)\n", 
 181                         ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen 
- 2)); 
 185         nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0); 
 192         icmp6stat
.icp6s_badrs
++; 
 197  * Receive Router Advertisement Message. 
 200  * TODO: on-link bit on prefix information 
 201  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 
 209         struct ifnet 
*ifp 
= m
->m_pkthdr
.rcvif
; 
 210         struct nd_ifinfo 
*ndi 
= &nd_ifinfo
[ifp
->if_index
]; 
 211         struct ip6_hdr 
*ip6 
= mtod(m
, struct ip6_hdr 
*); 
 212         struct nd_router_advert 
*nd_ra
; 
 213         struct in6_addr saddr6 
= ip6
->ip6_src
; 
 215         struct in6_addr daddr6 
= ip6
->ip6_dst
; 
 216         int flags
; /* = nd_ra->nd_ra_flags_reserved; */ 
 217         int is_managed 
= ((flags 
& ND_RA_FLAG_MANAGED
) != 0); 
 218         int is_other 
= ((flags 
& ND_RA_FLAG_OTHER
) != 0); 
 220         union nd_opts ndopts
; 
 221         struct nd_defrouter 
*dr
; 
 222         struct timeval timenow
; 
 224         getmicrotime(&timenow
); 
 226         if (ip6_accept_rtadv 
== 0 && ((ifp
->if_eflags 
& IFEF_ACCEPT_RTADVD
) == 0)) 
 229         if (ip6
->ip6_hlim 
!= 255) { 
 231                     "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 
 232                     ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
), 
 233                     ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
))); 
 237         if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) { 
 239                     "nd6_ra_input: src %s is not link-local\n", 
 240                     ip6_sprintf(&saddr6
))); 
 244 #ifndef PULLDOWN_TEST 
 245         IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return); 
 246         nd_ra 
= (struct nd_router_advert 
*)((caddr_t
)ip6 
+ off
); 
 248         IP6_EXTHDR_GET(nd_ra
, struct nd_router_advert 
*, m
, off
, icmp6len
); 
 250                 icmp6stat
.icp6s_tooshort
++; 
 255         icmp6len 
-= sizeof(*nd_ra
); 
 256         nd6_option_init(nd_ra 
+ 1, icmp6len
, &ndopts
); 
 257         if (nd6_options(&ndopts
) < 0) { 
 259                     "nd6_ra_input: invalid ND option, ignored\n")); 
 260                 /* nd6_options have incremented stats */ 
 265         struct nd_defrouter dr0
; 
 266         u_int32_t advreachable 
= nd_ra
->nd_ra_reachable
; 
 269         dr0
.flags  
= nd_ra
->nd_ra_flags_reserved
; 
 270         dr0
.rtlifetime 
= ntohs(nd_ra
->nd_ra_router_lifetime
); 
 271         dr0
.expire 
= timenow
.tv_sec 
+ dr0
.rtlifetime
; 
 273         dr0
.advint 
= 0;         /* Mobile IPv6 */ 
 274         dr0
.advint_expire 
= 0;  /* Mobile IPv6 */ 
 275         dr0
.advints_lost 
= 0;   /* Mobile IPv6 */ 
 276         /* unspecified or not? (RFC 2461 6.3.4) */ 
 278                 advreachable 
= ntohl(advreachable
); 
 279                 if (advreachable 
<= MAX_REACHABLE_TIME 
&& 
 280                     ndi
->basereachable 
!= advreachable
) { 
 281                         ndi
->basereachable 
= advreachable
; 
 282                         ndi
->reachable 
= ND_COMPUTE_RTIME(ndi
->basereachable
); 
 283                         ndi
->recalctm 
= nd6_recalc_reachtm_interval
; /* reset */ 
 286         if (nd_ra
->nd_ra_retransmit
) 
 287                 ndi
->retrans 
= ntohl(nd_ra
->nd_ra_retransmit
); 
 288         if (nd_ra
->nd_ra_curhoplimit
) 
 289                 ndi
->chlim 
= nd_ra
->nd_ra_curhoplimit
; 
 290         dr 
= defrtrlist_update(&dr0
); 
 296         if (ndopts
.nd_opts_pi
) { 
 297                 struct nd_opt_hdr 
*pt
; 
 298                 struct nd_opt_prefix_info 
*pi 
= NULL
; 
 301                 for (pt 
= (struct nd_opt_hdr 
*)ndopts
.nd_opts_pi
; 
 302                      pt 
<= (struct nd_opt_hdr 
*)ndopts
.nd_opts_pi_end
; 
 303                      pt 
= (struct nd_opt_hdr 
*)((caddr_t
)pt 
+ 
 304                                                 (pt
->nd_opt_len 
<< 3))) { 
 305                         if (pt
->nd_opt_type 
!= ND_OPT_PREFIX_INFORMATION
) 
 307                         pi 
= (struct nd_opt_prefix_info 
*)pt
; 
 309                         if (pi
->nd_opt_pi_len 
!= 4) { 
 311                                     "nd6_ra_input: invalid option " 
 312                                     "len %d for prefix information option, " 
 313                                     "ignored\n", pi
->nd_opt_pi_len
)); 
 317                         if (128 < pi
->nd_opt_pi_prefix_len
) { 
 319                                     "nd6_ra_input: invalid prefix " 
 320                                     "len %d for prefix information option, " 
 321                                     "ignored\n", pi
->nd_opt_pi_prefix_len
)); 
 325                         if (IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) 
 326                          || IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) { 
 328                                     "nd6_ra_input: invalid prefix " 
 330                                     ip6_sprintf(&pi
->nd_opt_pi_prefix
))); 
 334                         /* aggregatable unicast address, rfc2374 */ 
 335                         if ((pi
->nd_opt_pi_prefix
.s6_addr8
[0] & 0xe0) == 0x20 
 336                          && pi
->nd_opt_pi_prefix_len 
!= 64) { 
 338                                     "nd6_ra_input: invalid prefixlen " 
 339                                     "%d for rfc2374 prefix %s, ignored\n", 
 340                                     pi
->nd_opt_pi_prefix_len
, 
 341                                     ip6_sprintf(&pi
->nd_opt_pi_prefix
))); 
 345                         bzero(&pr
, sizeof(pr
)); 
 346                         pr
.ndpr_prefix
.sin6_family 
= AF_INET6
; 
 347                         pr
.ndpr_prefix
.sin6_len 
= sizeof(pr
.ndpr_prefix
); 
 348                         pr
.ndpr_prefix
.sin6_addr 
= pi
->nd_opt_pi_prefix
; 
 349                         pr
.ndpr_ifp 
= m
->m_pkthdr
.rcvif
; 
 351                         pr
.ndpr_raf_onlink 
= (pi
->nd_opt_pi_flags_reserved 
& 
 352                                               ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0; 
 353                         pr
.ndpr_raf_auto 
= (pi
->nd_opt_pi_flags_reserved 
& 
 354                                             ND_OPT_PI_FLAG_AUTO
) ? 1 : 0; 
 355                         pr
.ndpr_plen 
= pi
->nd_opt_pi_prefix_len
; 
 356                         pr
.ndpr_vltime 
= ntohl(pi
->nd_opt_pi_valid_time
); 
 358                                 ntohl(pi
->nd_opt_pi_preferred_time
); 
 360                         if (in6_init_prefix_ltimes(&pr
)) 
 361                                 continue; /* prefix lifetime init failed */ 
 363                         (void)prelist_update(&pr
, dr
, m
); 
 370         if (ndopts
.nd_opts_mtu 
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len 
== 1) { 
 371                 u_int32_t mtu 
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
); 
 374                 if (mtu 
< IPV6_MMTU
) { 
 375                         nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option " 
 376                             "mtu=%d sent from %s, ignoring\n", 
 377                             mtu
, ip6_sprintf(&ip6
->ip6_src
))); 
 383                         if (mtu 
<= ndi
->maxmtu
) { 
 384                                 int change 
= (ndi
->linkmtu 
!= mtu
); 
 387                                 if (change
) /* in6_maxmtu may change */ 
 390                                 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu " 
 391                                     "mtu=%d sent from %s; " 
 392                                     "exceeds maxmtu %d, ignoring\n", 
 393                                     mtu
, ip6_sprintf(&ip6
->ip6_src
), 
 397                         nd6log((LOG_INFO
, "nd6_ra_input: mtu option " 
 398                             "mtu=%d sent from %s; maxmtu unknown, " 
 400                             mtu
, ip6_sprintf(&ip6
->ip6_src
))); 
 407          * Source link layer address 
 413         if (ndopts
.nd_opts_src_lladdr
) { 
 414                 lladdr 
= (char *)(ndopts
.nd_opts_src_lladdr 
+ 1); 
 415                 lladdrlen 
= ndopts
.nd_opts_src_lladdr
->nd_opt_len 
<< 3; 
 418         if (lladdr 
&& ((ifp
->if_addrlen 
+ 2 + 7) & ~7) != lladdrlen
) { 
 420                     "nd6_ra_input: lladdrlen mismatch for %s " 
 421                     "(if %d, RA packet %d)\n", 
 422                         ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen 
- 2)); 
 426         nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_ADVERT
, 0); 
 429          * Installing a link-layer address might change the state of the 
 430          * router's neighbor cache, which might also affect our on-link 
 431          * detection of adveritsed prefixes. 
 433         pfxlist_onlink_check(0); 
 441         icmp6stat
.icp6s_badra
++; 
 446  * default router list proccessing sub routines 
 449 /* tell the change to user processes watching the routing socket. */ 
 455         struct rt_addrinfo info
; 
 457         lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
); 
 459         bzero((caddr_t
)&info
, sizeof(info
)); 
 460         info
.rti_info
[RTAX_DST
] = rt_key(rt
); 
 461         info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
; 
 462         info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
); 
 463         info
.rti_info
[RTAX_IFP
] = 
 464                 TAILQ_FIRST(&rt
->rt_ifp
->if_addrlist
)->ifa_addr
; 
 465         info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
; 
 467         rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0); 
 472         struct nd_defrouter 
*new) 
 474         struct sockaddr_in6 def
, mask
, gate
; 
 475         struct rtentry 
*newrt 
= NULL
; 
 477         Bzero(&def
, sizeof(def
)); 
 478         Bzero(&mask
, sizeof(mask
)); 
 479         Bzero(&gate
, sizeof(gate
)); 
 481         def
.sin6_len 
= mask
.sin6_len 
= gate
.sin6_len
 
 482                 = sizeof(struct sockaddr_in6
); 
 483         def
.sin6_family 
= mask
.sin6_family 
= gate
.sin6_family 
= AF_INET6
; 
 484         gate
.sin6_addr 
= new->rtaddr
; 
 486         lck_mtx_lock(rt_mtx
); 
 487         (void)rtrequest_locked(RTM_ADD
, (struct sockaddr 
*)&def
, 
 488                 (struct sockaddr 
*)&gate
, (struct sockaddr 
*)&mask
, 
 489                 RTF_GATEWAY
, &newrt
); 
 491                 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */ 
 494         lck_mtx_unlock(rt_mtx
); 
 498 /* Add a route to a given interface as default */ 
 503         struct sockaddr_in6 def
, mask
; 
 504         struct ifaddr 
*ifa 
= NULL
; 
 505         struct rtentry 
*newrt 
= NULL
; 
 509         bzero(&def
, sizeof(def
)); 
 510         bzero(&mask
, sizeof(mask
)); 
 512         def
.sin6_len 
= mask
.sin6_len 
= sizeof(struct sockaddr_in6
); 
 513         def
.sin6_family 
= mask
.sin6_family 
= AF_INET6
; 
 516          * Search for an ifaddr beloging to the specified interface. 
 517          * XXX: An IPv6 address are required to be assigned on the interface. 
 519         if ((ifa 
= ifaof_ifpforaddr((struct sockaddr 
*)&def
, ifp
)) == NULL
) { 
 520                 nd6log((LOG_ERR
,        /* better error? */ 
 521                     "defrouter_addifreq: failed to find an ifaddr " 
 522                     "to install a route to interface %s\n", 
 527         lck_mtx_lock(rt_mtx
); 
 528         flags 
= ifa
->ifa_flags
; 
 529         error 
= rtrequest_locked(RTM_ADD
, (struct sockaddr 
*)&def
, ifa
->ifa_addr
, 
 530                           (struct sockaddr 
*)&mask
, flags
, &newrt
); 
 533                     "defrouter_addifreq: failed to install a route to " 
 534                     "interface %s (errno = %d)\n", 
 535                     if_name(ifp
), error
)); 
 537                 if (newrt
)      /* maybe unnecessary, but do it for safety */ 
 541                         nd6_rtmsg(RTM_ADD
, newrt
); 
 544                 in6_post_msg(ifp
, KEV_INET6_DEFROUTER
, (struct in6_ifaddr 
*)ifa
); 
 546         lck_mtx_unlock(rt_mtx
); 
 550 struct nd_defrouter 
* 
 552         struct in6_addr 
*addr
, 
 555         struct nd_defrouter 
*dr
; 
 558         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
 560         for (dr 
= TAILQ_FIRST(&nd_defrouter
); dr
; 
 561              dr 
= TAILQ_NEXT(dr
, dr_entry
)) { 
 562                 if (dr
->ifp 
== ifp 
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) 
 566         return(NULL
);           /* search failed */ 
 571         struct nd_defrouter 
*dr
, 
 574         struct sockaddr_in6 def
, mask
, gate
; 
 575         struct rtentry 
*oldrt 
= NULL
; 
 577         Bzero(&def
, sizeof(def
)); 
 578         Bzero(&mask
, sizeof(mask
)); 
 579         Bzero(&gate
, sizeof(gate
)); 
 581         def
.sin6_len 
= mask
.sin6_len 
= gate
.sin6_len
 
 582                 = sizeof(struct sockaddr_in6
); 
 583         def
.sin6_family 
= mask
.sin6_family 
= gate
.sin6_family 
= AF_INET6
; 
 584         gate
.sin6_addr 
= dr
->rtaddr
; 
 586         lck_mtx_lock(rt_mtx
); 
 587         rtrequest_locked(RTM_DELETE
, (struct sockaddr 
*)&def
, 
 588                   (struct sockaddr 
*)&gate
, 
 589                   (struct sockaddr 
*)&mask
, 
 590                   RTF_GATEWAY
, &oldrt
); 
 592                 nd6_rtmsg(RTM_DELETE
, oldrt
); 
 593                 if (oldrt
->rt_refcnt 
<= 0) { 
 595                          * XXX: borrowed from the RTM_DELETE case of 
 599                         rtfree_locked(oldrt
); 
 603         if (dofree
)             /* XXX: necessary? */ 
 605         lck_mtx_unlock(rt_mtx
); 
 610         struct nd_defrouter 
*dr
, int nd6locked
) 
 612         struct nd_defrouter 
*deldr 
= NULL
; 
 613         struct nd_prefix 
*pr
; 
 616          * Flush all the routing table entries that use the router 
 619         if (!ip6_forwarding 
&& (ip6_accept_rtadv 
|| (dr
->ifp
->if_eflags 
& IFEF_ACCEPT_RTADVD
))) { 
 620                 /* above is a good condition? */ 
 621                 rt6_flush(&dr
->rtaddr
, dr
->ifp
); 
 625                 lck_mtx_lock(nd6_mutex
); 
 626         if (dr 
== TAILQ_FIRST(&nd_defrouter
)) 
 627                 deldr 
= dr
;     /* The router is primary. */ 
 629         TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
); 
 632          * Also delete all the pointers to the router in each prefix lists. 
 634         for (pr 
= nd_prefix
.lh_first
; pr
; pr 
= pr
->ndpr_next
) { 
 635                 struct nd_pfxrouter 
*pfxrtr
; 
 636                 if ((pfxrtr 
= pfxrtr_lookup(pr
, dr
)) != NULL
) 
 639         pfxlist_onlink_check(1); 
 642          * If the router is the primary one, choose a new one. 
 643          * Note that defrouter_select() will remove the current gateway 
 644          * from the routing table. 
 650                 lck_mtx_unlock(nd6_mutex
); 
 656  * Default Router Selection according to Section 6.3.6 of RFC 2461: 
 657  * 1) Routers that are reachable or probably reachable should be 
 659  * 2) When no routers on the list are known to be reachable or 
 660  *    probably reachable, routers SHOULD be selected in a round-robin 
 662  * 3) If the Default Router List is empty, assume that all 
 663  *    destinations are on-link. 
 668         struct nd_defrouter 
*dr
, anydr
; 
 669         struct rtentry 
*rt 
= NULL
; 
 670         struct llinfo_nd6 
*ln 
= NULL
; 
 673          * Search for a (probably) reachable router from the list. 
 675         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
 677         for (dr 
= TAILQ_FIRST(&nd_defrouter
); dr
; 
 678              dr 
= TAILQ_NEXT(dr
, dr_entry
)) { 
 679                 if ((rt 
= nd6_lookup(&dr
->rtaddr
, 0, dr
->ifp
, 0)) && 
 680                     (ln 
= (struct llinfo_nd6 
*)rt
->rt_llinfo
) && 
 681                     ND6_IS_LLINFO_PROBREACH(ln
)) { 
 682                         /* Got it, and move it to the head */ 
 683                         TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
); 
 684                         TAILQ_INSERT_HEAD(&nd_defrouter
, dr
, dr_entry
); 
 689         if ((dr 
= TAILQ_FIRST(&nd_defrouter
))) { 
 691                  * De-install the previous default gateway and install 
 693                  * Note that if there is no reachable router in the list, 
 694                  * the head entry will be used anyway. 
 695                  * XXX: do we have to check the current routing table entry? 
 697                 bzero(&anydr
, sizeof(anydr
)); 
 698                 defrouter_delreq(&anydr
, 0); 
 699                 defrouter_addreq(dr
); 
 703                  * The Default Router List is empty, so install the default 
 704                  * route to an inteface. 
 705                  * XXX: The specification does not say this mechanism should 
 706                  * be restricted to hosts, but this would be not useful 
 707                  * (even harmful) for routers. 
 709                 if (!ip6_forwarding
) { 
 711                          * De-install the current default route 
 714                         bzero(&anydr
, sizeof(anydr
)); 
 715                         defrouter_delreq(&anydr
, 0); 
 718                                  * Install a route to the default interface 
 720                                  * XXX: we enable this for host only, because 
 721                                  * this may override a default route installed 
 722                                  * a user process (e.g. routing daemon) in a 
 725                                 defrouter_addifreq(nd6_defifp
); 
 727                                 nd6log((LOG_INFO
, "defrouter_select: " 
 728                                     "there's no default router and no default" 
 737 static struct nd_defrouter 
* 
 739         struct nd_defrouter 
*new) 
 741         struct nd_defrouter 
*dr
, *n
; 
 743         lck_mtx_lock(nd6_mutex
); 
 744         if ((dr 
= defrouter_lookup(&new->rtaddr
, new->ifp
)) != NULL
) { 
 746                 if (new->rtlifetime 
== 0) { 
 747                         defrtrlist_del(dr
, 1); 
 751                         dr
->flags 
= new->flags
; /* xxx flag check */ 
 752                         dr
->rtlifetime 
= new->rtlifetime
; 
 753                         dr
->expire 
= new->expire
; 
 755                 lck_mtx_unlock(nd6_mutex
); 
 759         /* entry does not exist */ 
 760         if (new->rtlifetime 
== 0) { 
 761                 lck_mtx_unlock(nd6_mutex
); 
 765         n 
= (struct nd_defrouter 
*)_MALLOC(sizeof(*n
), M_IP6NDP
, M_NOWAIT
); 
 767                 lck_mtx_unlock(nd6_mutex
); 
 770         bzero(n
, sizeof(*n
)); 
 774          * Insert the new router at the end of the Default Router List. 
 775          * If there is no other router, install it anyway. Otherwise, 
 776          * just continue to use the current default router. 
 778         TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
); 
 779         if (TAILQ_FIRST(&nd_defrouter
) == n
) 
 782         lck_mtx_unlock(nd6_mutex
); 
 786 static struct nd_pfxrouter 
* 
 788         struct nd_prefix 
*pr
, 
 789         struct nd_defrouter 
*dr
) 
 791         struct nd_pfxrouter 
*search
; 
 793         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
 794         for (search 
= pr
->ndpr_advrtrs
.lh_first
; search
; search 
= search
->pfr_next
) { 
 795                 if (search
->router 
== dr
) 
 804         struct nd_prefix 
*pr
, 
 805         struct nd_defrouter 
*dr
) 
 807         struct nd_pfxrouter 
*new; 
 809         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
 811         new = (struct nd_pfxrouter 
*)_MALLOC(sizeof(*new), M_IP6NDP
, M_NOWAIT
); 
 814         bzero(new, sizeof(*new)); 
 817         LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
); 
 819         pfxlist_onlink_check(1); 
 824         struct nd_pfxrouter 
*pfr
) 
 826         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
 827         LIST_REMOVE(pfr
, pfr_entry
); 
 833         struct nd_prefix 
*pr
) 
 835         struct nd_prefix 
*search
; 
 837         lck_mtx_lock(nd6_mutex
); 
 838         for (search 
= nd_prefix
.lh_first
; search
; search 
= search
->ndpr_next
) { 
 839                 if (pr
->ndpr_ifp 
== search
->ndpr_ifp 
&& 
 840                     pr
->ndpr_plen 
== search
->ndpr_plen 
&& 
 841                     in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
, 
 842                                          &search
->ndpr_prefix
.sin6_addr
, 
 848         lck_mtx_unlock(nd6_mutex
); 
 855         struct nd_prefix 
*pr
, 
 856         struct nd_defrouter 
*dr
, 
 857         struct nd_prefix 
**newp
) 
 859         struct nd_prefix 
*new = NULL
; 
 862         new = (struct nd_prefix 
*)_MALLOC(sizeof(*new), M_IP6NDP
, M_NOWAIT
); 
 865         bzero(new, sizeof(*new)); 
 871         LIST_INIT(&new->ndpr_advrtrs
); 
 872         in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
); 
 873         /* make prefix in the canonical form */ 
 874         for (i 
= 0; i 
< 4; i
++) 
 875                 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &= 
 876                         new->ndpr_mask
.s6_addr32
[i
]; 
 878         /* link ndpr_entry to nd_prefix list */ 
 879         lck_mtx_lock(nd6_mutex
); 
 880         LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
); 
 882         /* ND_OPT_PI_FLAG_ONLINK processing */ 
 883         if (new->ndpr_raf_onlink
) { 
 886                 if ((e 
= nd6_prefix_onlink(new, 0, 1)) != 0) { 
 887                         nd6log((LOG_ERR
, "nd6_prelist_add: failed to make " 
 888                             "the prefix %s/%d on-link on %s (errno=%d)\n", 
 889                             ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
 890                             pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
)); 
 891                         /* proceed anyway. XXX: is it correct? */ 
 898         lck_mtx_unlock(nd6_mutex
); 
 905         struct nd_prefix 
*pr
, int nd6locked
) 
 907         struct nd_pfxrouter 
*pfr
, *next
; 
 910         /* make sure to invalidate the prefix until it is really freed. */ 
 915          * Though these flags are now meaningless, we'd rather keep the value 
 916          * not to confuse users when executing "ndp -p". 
 918         pr
->ndpr_raf_onlink 
= 0; 
 919         pr
->ndpr_raf_auto 
= 0; 
 921         if ((pr
->ndpr_stateflags 
& NDPRF_ONLINK
) != 0 && 
 922             (e 
= nd6_prefix_offlink(pr
)) != 0) { 
 923                 nd6log((LOG_ERR
, "prelist_remove: failed to make %s/%d offlink " 
 925                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
 926                     pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
)); 
 927                 /* what should we do? */ 
 930         if (pr
->ndpr_refcnt 
> 0) 
 931                 return;         /* notice here? */ 
 934                 lck_mtx_lock(nd6_mutex
); 
 935         /* unlink ndpr_entry from nd_prefix list */ 
 936         LIST_REMOVE(pr
, ndpr_entry
); 
 938         /* free list of routers that adversed the prefix */ 
 939         for (pfr 
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr 
= next
) { 
 940                 next 
= pfr
->pfr_next
; 
 947         pfxlist_onlink_check(1); 
 949                 lck_mtx_unlock(nd6_mutex
); 
 954         struct nd_prefix 
*new, 
 955         struct nd_defrouter 
*dr
, /* may be NULL */ 
 958         struct in6_ifaddr 
*ia6 
= NULL
, *ia6_match 
= NULL
; 
 960         struct ifnet 
*ifp 
= new->ndpr_ifp
; 
 961         struct nd_prefix 
*pr
; 
 965         struct in6_addrlifetime lt6_tmp
; 
 966         struct timeval timenow
; 
 971                  * Authenticity for NA consists authentication for 
 972                  * both IP header and IP datagrams, doesn't it ? 
 974 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 
 975                 auth 
= (m
->m_flags 
& M_AUTHIPHDR
 
 976                      && m
->m_flags 
& M_AUTHIPDGM
) ? 1 : 0; 
 981         if ((pr 
= nd6_prefix_lookup(new)) != NULL
) { 
 983                  * nd6_prefix_lookup() ensures that pr and new have the same 
 984                  * prefix on a same interface. 
 988                  * Update prefix information.  Note that the on-link (L) bit 
 989                  * and the autonomous (A) bit should NOT be changed from 1 
 992                 if (new->ndpr_raf_onlink 
== 1) 
 993                         pr
->ndpr_raf_onlink 
= 1; 
 994                 if (new->ndpr_raf_auto 
== 1) 
 995                         pr
->ndpr_raf_auto 
= 1; 
 996                 if (new->ndpr_raf_onlink
) { 
 997                         pr
->ndpr_vltime 
= new->ndpr_vltime
; 
 998                         pr
->ndpr_pltime 
= new->ndpr_pltime
; 
 999                         pr
->ndpr_preferred 
= new->ndpr_preferred
; 
1000                         pr
->ndpr_expire 
= new->ndpr_expire
; 
1003                 if (new->ndpr_raf_onlink 
&& 
1004                     (pr
->ndpr_stateflags 
& NDPRF_ONLINK
) == 0) { 
1007                         if ((e 
= nd6_prefix_onlink(pr
, 0, 0)) != 0) { 
1009                                     "prelist_update: failed to make " 
1010                                     "the prefix %s/%d on-link on %s " 
1012                                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
1013                                     pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
)); 
1014                                 /* proceed anyway. XXX: is it correct? */ 
1018                 lck_mtx_lock(nd6_mutex
); 
1019                 if (dr 
&& pfxrtr_lookup(pr
, dr
) == NULL
) 
1021                 lck_mtx_unlock(nd6_mutex
); 
1023                 struct nd_prefix 
*newpr 
= NULL
; 
1027                 if (new->ndpr_vltime 
== 0) 
1029                 if (new->ndpr_raf_onlink 
== 0 && new->ndpr_raf_auto 
== 0) 
1032                 bzero(&new->ndpr_addr
, sizeof(struct in6_addr
)); 
1034                 error 
= nd6_prelist_add(new, dr
, &newpr
); 
1035                 if (error 
!= 0 || newpr 
== NULL
) { 
1036                         nd6log((LOG_NOTICE
, "prelist_update: " 
1037                             "nd6_prelist_add failed for %s/%d on %s " 
1038                             "errno=%d, returnpr=%p\n", 
1039                             ip6_sprintf(&new->ndpr_prefix
.sin6_addr
), 
1040                                         new->ndpr_plen
, if_name(new->ndpr_ifp
), 
1042                         goto end
; /* we should just give up in this case. */ 
1046                  * XXX: from the ND point of view, we can ignore a prefix 
1047                  * with the on-link bit being zero.  However, we need a 
1048                  * prefix structure for references from autoconfigured 
1049                  * addresses.  Thus, we explicitly make suret that the prefix 
1050                  * itself expires now. 
1052                 if (newpr
->ndpr_raf_onlink 
== 0) { 
1053                         newpr
->ndpr_vltime 
= 0; 
1054                         newpr
->ndpr_pltime 
= 0; 
1055                         in6_init_prefix_ltimes(newpr
); 
1062          * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 
1063          * Note that pr must be non NULL at this point. 
1066         /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 
1067         if (!new->ndpr_raf_auto
) 
1071          * 5.5.3 (b). the link-local prefix should have been ignored in 
1076          * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. 
1077          * This should have been done in nd6_ra_input. 
1081          * 5.5.3 (d). If the prefix advertised does not match the prefix of an 
1082          * address already in the list, and the Valid Lifetime is not 0, 
1083          * form an address.  Note that even a manually configured address 
1084          * should reject autoconfiguration of a new address. 
1086         getmicrotime(&timenow
); 
1088         ifnet_lock_exclusive(ifp
); 
1089         TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) 
1091                 struct in6_ifaddr 
*ifa6
; 
1093                 u_int32_t storedlifetime
; 
1095                 if (ifa
->ifa_addr
->sa_family 
!= AF_INET6
) 
1098                 ifa6 
= (struct in6_ifaddr 
*)ifa
; 
1101                  * Spec is not clear here, but I believe we should concentrate 
1102                  * on unicast (i.e. not anycast) addresses. 
1103                  * XXX: other ia6_flags? detached or duplicated? 
1105                 if ((ifa6
->ia6_flags 
& IN6_IFF_ANYCAST
) != 0) 
1108                 ifa_plen 
= in6_mask2len(&ifa6
->ia_prefixmask
.sin6_addr
, NULL
); 
1109                 if (ifa_plen 
!= new->ndpr_plen 
|| 
1110                     !in6_are_prefix_equal(&ifa6
->ia_addr
.sin6_addr
, 
1111                                           &new->ndpr_prefix
.sin6_addr
, 
1115                 if (ia6_match 
== NULL
) /* remember the first one */ 
1118                 if ((ifa6
->ia6_flags 
& IN6_IFF_AUTOCONF
) == 0) 
1122                  * An already autoconfigured address matched.  Now that we 
1123                  * are sure there is at least one matched address, we can 
1124                  * proceed to 5.5.3. (e): update the lifetimes according to the 
1125                  * "two hours" rule and the privacy extension. 
1127 #define TWOHOUR         (120*60) 
1128                 lt6_tmp 
= ifa6
->ia6_lifetime
; 
1130                 storedlifetime 
= IFA6_IS_INVALID(ifa6
) ? 0 : 
1131                         (lt6_tmp
.ia6t_expire 
- timenow
.tv_sec
); 
1133                 if (TWOHOUR 
< new->ndpr_vltime 
|| 
1134                     storedlifetime 
< new->ndpr_vltime
) { 
1135                         lt6_tmp
.ia6t_vltime 
= new->ndpr_vltime
; 
1136                 } else if (storedlifetime 
<= TWOHOUR
 
1139                             * This condition is logically redundant, so we just 
1141                             * See IPng 6712, 6717, and 6721. 
1143                            && new->ndpr_vltime 
<= storedlifetime
 
1147                                 lt6_tmp
.ia6t_vltime 
= new->ndpr_vltime
; 
1151                          * new->ndpr_vltime <= TWOHOUR && 
1152                          * TWOHOUR < storedlifetime 
1154                         lt6_tmp
.ia6t_vltime 
= TWOHOUR
; 
1157                 /* The 2 hour rule is not imposed for preferred lifetime. */ 
1158                 lt6_tmp
.ia6t_pltime 
= new->ndpr_pltime
; 
1160                 in6_init_address_ltimes(pr
, <6_tmp
); 
1163                  * When adjusting the lifetimes of an existing temporary 
1164                  * address, only lower the lifetimes. 
1165                  * RFC 3041 3.3. (1). 
1166                  * XXX: how should we modify ia6t_[pv]ltime? 
1168                 if ((ifa6
->ia6_flags 
& IN6_IFF_TEMPORARY
) != 0) { 
1169                         if (lt6_tmp
.ia6t_expire 
== 0 || /* no expire */ 
1170                             lt6_tmp
.ia6t_expire 
> 
1171                             ifa6
->ia6_lifetime
.ia6t_expire
) { 
1172                                 lt6_tmp
.ia6t_expire 
= 
1173                                         ifa6
->ia6_lifetime
.ia6t_expire
; 
1175                         if (lt6_tmp
.ia6t_preferred 
== 0 || /* no expire */ 
1176                             lt6_tmp
.ia6t_preferred 
> 
1177                             ifa6
->ia6_lifetime
.ia6t_preferred
) { 
1178                                 lt6_tmp
.ia6t_preferred 
= 
1179                                         ifa6
->ia6_lifetime
.ia6t_preferred
; 
1183                 ifa6
->ia6_lifetime 
= lt6_tmp
; 
1185         ifnet_lock_done(ifp
); 
1186         if (ia6_match 
== NULL 
&& new->ndpr_vltime
) { 
1188                  * No address matched and the valid lifetime is non-zero. 
1189                  * Create a new address. 
1191                 if ((ia6 
= in6_ifadd(new, NULL
)) != NULL
) { 
1193                          * note that we should use pr (not new) for reference. 
1199                         /* XXXYYY Don't do this, according to Jinmei. */ 
1200                         pr
->ndpr_addr 
= new->ndpr_addr
; 
1205                          * When a new public address is created as described 
1206                          * in RFC2462, also create a new temporary address. 
1209                          * When an interface connects to a new link, a new 
1210                          * randomized interface identifier should be generated 
1211                          * immediately together with a new set of temporary 
1212                          * addresses.  Thus, we specifiy 1 as the 2nd arg of 
1215                         if (ip6_use_tempaddr
) { 
1217                                 if ((e 
= in6_tmpifadd(ia6
, 1)) != 0) { 
1218                                         nd6log((LOG_NOTICE
, "prelist_update: " 
1219                                             "failed to create a temporary " 
1220                                             "address, errno=%d\n", 
1226                          * A newly added address might affect the status 
1227                          * of other addresses, so we check and update it. 
1228                          * XXX: what if address duplication happens? 
1230                         pfxlist_onlink_check(0); 
1232                         /* just set an error. do not bark here. */ 
1233                         error 
= EADDRNOTAVAIL
; /* XXX: might be unused. */ 
1244  * A supplement function used in the on-link detection below; 
1245  * detect if a given prefix has a (probably) reachable advertising router. 
1246  * XXX: lengthy function name... 
1248 static struct nd_pfxrouter 
* 
1249 find_pfxlist_reachable_router( 
1250         struct nd_prefix 
*pr
) 
1252         struct nd_pfxrouter 
*pfxrtr
; 
1254         struct llinfo_nd6 
*ln
; 
1256         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
1258         for (pfxrtr 
= LIST_FIRST(&pr
->ndpr_advrtrs
); pfxrtr
; 
1259              pfxrtr 
= LIST_NEXT(pfxrtr
, pfr_entry
)) { 
1260                 if ((rt 
= nd6_lookup(&pfxrtr
->router
->rtaddr
, 0, 
1261                                      pfxrtr
->router
->ifp
, 0)) && 
1262                     (ln 
= (struct llinfo_nd6 
*)rt
->rt_llinfo
) && 
1263                     ND6_IS_LLINFO_PROBREACH(ln
)) 
1272  * Check if each prefix in the prefix list has at least one available router 
1273  * that advertised the prefix (a router is "available" if its neighbor cache 
1274  * entry is reachable or probably reachable). 
1275  * If the check fails, the prefix may be off-link, because, for example, 
1276  * we have moved from the network but the lifetime of the prefix has not 
1277  * expired yet.  So we should not use the prefix if there is another prefix 
1278  * that has an available router. 
1279  * But, if there is no prefix that has an available router, we still regards 
1280  * all the prefixes as on-link.  This is because we can't tell if all the 
1281  * routers are simply dead or if we really moved from the network and there 
1282  * is no router around us. 
1285 pfxlist_onlink_check(int nd6locked
) 
1287         struct nd_prefix 
*pr
; 
1288         struct in6_ifaddr 
*ifa
; 
1291          * Check if there is a prefix that has a reachable advertising 
1295                 lck_mtx_lock(nd6_mutex
); 
1296         lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
1297         for (pr 
= nd_prefix
.lh_first
; pr
; pr 
= pr
->ndpr_next
) { 
1298                 if (pr
->ndpr_raf_onlink 
&& find_pfxlist_reachable_router(pr
)) 
1304                  * There is at least one prefix that has a reachable router. 
1305                  * Detach prefixes which have no reachable advertising 
1306                  * router, and attach other prefixes. 
1308                 for (pr 
= nd_prefix
.lh_first
; pr
; pr 
= pr
->ndpr_next
) { 
1309                         /* XXX: a link-local prefix should never be detached */ 
1310                         if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) 
1314                          * we aren't interested in prefixes without the L bit 
1317                         if (pr
->ndpr_raf_onlink 
== 0) 
1320                         if ((pr
->ndpr_stateflags 
& NDPRF_DETACHED
) == 0 && 
1321                             find_pfxlist_reachable_router(pr
) == NULL
) 
1322                                 pr
->ndpr_stateflags 
|= NDPRF_DETACHED
; 
1323                         if ((pr
->ndpr_stateflags 
& NDPRF_DETACHED
) != 0 && 
1324                             find_pfxlist_reachable_router(pr
) != 0) 
1325                                 pr
->ndpr_stateflags 
&= ~NDPRF_DETACHED
; 
1328                 /* there is no prefix that has a reachable router */ 
1329                 for (pr 
= nd_prefix
.lh_first
; pr
; pr 
= pr
->ndpr_next
) { 
1330                         if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) 
1333                         if (pr
->ndpr_raf_onlink 
== 0) 
1336                         if ((pr
->ndpr_stateflags 
& NDPRF_DETACHED
) != 0) 
1337                                 pr
->ndpr_stateflags 
&= ~NDPRF_DETACHED
; 
1342          * Remove each interface route associated with a (just) detached 
1343          * prefix, and reinstall the interface route for a (just) attached 
1344          * prefix.  Note that all attempt of reinstallation does not 
1345          * necessarily success, when a same prefix is shared among multiple 
1346          * interfaces.  Such cases will be handled in nd6_prefix_onlink, 
1347          * so we don't have to care about them. 
1349         for (pr 
= nd_prefix
.lh_first
; pr
; pr 
= pr
->ndpr_next
) { 
1352                 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) 
1355                 if (pr
->ndpr_raf_onlink 
== 0) 
1358                 if ((pr
->ndpr_stateflags 
& NDPRF_DETACHED
) != 0 && 
1359                     (pr
->ndpr_stateflags 
& NDPRF_ONLINK
) != 0) { 
1360                         if ((e 
= nd6_prefix_offlink(pr
)) != 0) { 
1362                                     "pfxlist_onlink_check: failed to " 
1363                                     "make %s/%d offlink, errno=%d\n", 
1364                                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
1368                 if ((pr
->ndpr_stateflags 
& NDPRF_DETACHED
) == 0 && 
1369                     (pr
->ndpr_stateflags 
& NDPRF_ONLINK
) == 0 && 
1370                     pr
->ndpr_raf_onlink
) { 
1371                         if ((e 
= nd6_prefix_onlink(pr
, 0, 1)) != 0) { 
1373                                     "pfxlist_onlink_check: failed to " 
1374                                     "make %s/%d offlink, errno=%d\n", 
1375                                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
1382          * Changes on the prefix status might affect address status as well. 
1383          * Make sure that all addresses derived from an attached prefix are 
1384          * attached, and that all addresses derived from a detached prefix are 
1385          * detached.  Note, however, that a manually configured address should 
1386          * always be attached. 
1387          * The precise detection logic is same as the one for prefixes. 
1389         for (ifa 
= in6_ifaddrs
; ifa
; ifa 
= ifa
->ia_next
) { 
1390                 if ((ifa
->ia6_flags 
& IN6_IFF_AUTOCONF
) == 0) 
1393                 if (ifa
->ia6_ndpr 
== NULL
) { 
1395                          * This can happen when we first configure the address 
1396                          * (i.e. the address exists, but the prefix does not). 
1397                          * XXX: complicated relationships... 
1402                 if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
)) 
1406                 for (ifa 
= in6_ifaddrs
; ifa
; ifa 
= ifa
->ia_next
) { 
1407                         if ((ifa
->ia6_flags 
& IN6_IFF_AUTOCONF
) == 0) 
1410                         if (ifa
->ia6_ndpr 
== NULL
) /* XXX: see above. */ 
1413                         if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
)) 
1414                                 ifa
->ia6_flags 
&= ~IN6_IFF_DETACHED
; 
1416                                 ifa
->ia6_flags 
|= IN6_IFF_DETACHED
; 
1420                 for (ifa 
= in6_ifaddrs
; ifa
; ifa 
= ifa
->ia_next
) { 
1421                         if ((ifa
->ia6_flags 
& IN6_IFF_AUTOCONF
) == 0) 
1424                         ifa
->ia6_flags 
&= ~IN6_IFF_DETACHED
; 
1428                 lck_mtx_unlock(nd6_mutex
); 
1433         struct nd_prefix 
*pr
, int rtlocked
, int nd6locked
) 
1436         struct ifnet 
*ifp 
= pr
->ndpr_ifp
; 
1437         struct sockaddr_in6 mask6
; 
1438         struct nd_prefix 
*opr
; 
1441         struct rtentry 
*rt 
= NULL
; 
1444         if ((pr
->ndpr_stateflags 
& NDPRF_ONLINK
) != 0) { 
1446                     "nd6_prefix_onlink: %s/%d is already on-link\n", 
1447                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
); 
1452          * Add the interface route associated with the prefix.  Before 
1453          * installing the route, check if there's the same prefix on another 
1454          * interface, and the prefix has already installed the interface route. 
1455          * Although such a configuration is expected to be rare, we explicitly 
1459                 lck_mtx_lock(nd6_mutex
); 
1461                 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
1462         for (opr 
= nd_prefix
.lh_first
; opr
; opr 
= opr
->ndpr_next
) { 
1466                 if ((opr
->ndpr_stateflags 
& NDPRF_ONLINK
) == 0) 
1469                 if (opr
->ndpr_plen 
== pr
->ndpr_plen 
&& 
1470                     in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
, 
1471                                          &opr
->ndpr_prefix
.sin6_addr
, 
1474                                 lck_mtx_unlock(nd6_mutex
); 
1480                 lck_mtx_unlock(nd6_mutex
); 
1482          * We prefer link-local addresses as the associated interface address.  
1484         /* search for a link-local addr */ 
1485         ifa 
= (struct ifaddr 
*)in6ifa_ifpforlinklocal(ifp
, 
1489                 /* XXX: freebsd does not have ifa_ifwithaf */ 
1490                 ifnet_lock_exclusive(ifp
); 
1491                 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) 
1493                         if (ifa
->ifa_addr
->sa_family 
== AF_INET6
) 
1496                 ifnet_lock_done(ifp
); 
1497                 /* should we care about ia6_flags? */ 
1501                  * This can still happen, when, for example, we receive an RA 
1502                  * containing a prefix with the L bit set and the A bit clear, 
1503                  * after removing all IPv6 addresses on the receiving 
1504                  * interface.  This should, of course, be rare though. 
1507                     "nd6_prefix_onlink: failed to find any ifaddr" 
1508                     " to add route for a prefix(%s/%d) on %s\n", 
1509                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
1510                     pr
->ndpr_plen
, if_name(ifp
))); 
1515          * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 
1516          * ifa->ifa_rtrequest = nd6_rtrequest; 
1518         bzero(&mask6
, sizeof(mask6
)); 
1519         mask6
.sin6_len 
= sizeof(mask6
); 
1520         mask6
.sin6_addr 
= pr
->ndpr_mask
; 
1523                 lck_mtx_lock(rt_mtx
); 
1525         rtflags 
= ifa
->ifa_flags 
| RTF_CLONING 
| RTF_UP
; 
1526         if (nd6_need_cache(ifp
)) { 
1527                 /* explicitly set in case ifa_flags does not set the flag. */ 
1528                 rtflags 
|= RTF_CLONING
; 
1531                  * explicitly clear the cloning bit in case ifa_flags sets it. 
1533                 rtflags 
&= ~RTF_CLONING
; 
1535         error 
= rtrequest_locked(RTM_ADD
, (struct sockaddr 
*)&pr
->ndpr_prefix
, 
1536                           ifa
->ifa_addr
, (struct sockaddr 
*)&mask6
, 
1539                 if (rt 
!= NULL
) /* this should be non NULL, though */ 
1540                         nd6_rtmsg(RTM_ADD
, rt
); 
1541                 pr
->ndpr_stateflags 
|= NDPRF_ONLINK
; 
1544                 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a" 
1545                     " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx " 
1547                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), 
1548                     pr
->ndpr_plen
, if_name(ifp
), 
1549                     ip6_sprintf(&((struct sockaddr_in6 
*)ifa
->ifa_addr
)->sin6_addr
), 
1550                     ip6_sprintf(&mask6
.sin6_addr
), rtflags
, error
)); 
1557                 lck_mtx_unlock(rt_mtx
); 
1563         struct nd_prefix 
*pr
) 
1566         struct ifnet 
*ifp 
= pr
->ndpr_ifp
; 
1567         struct nd_prefix 
*opr
; 
1568         struct sockaddr_in6 sa6
, mask6
; 
1569         struct rtentry 
*rt 
= NULL
; 
1572         if ((pr
->ndpr_stateflags 
& NDPRF_ONLINK
) == 0) { 
1574                     "nd6_prefix_offlink: %s/%d is already off-link\n", 
1575                     ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
)); 
1579         bzero(&sa6
, sizeof(sa6
)); 
1580         sa6
.sin6_family 
= AF_INET6
; 
1581         sa6
.sin6_len 
= sizeof(sa6
); 
1582         bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
, 
1583               sizeof(struct in6_addr
)); 
1584         bzero(&mask6
, sizeof(mask6
)); 
1585         mask6
.sin6_family 
= AF_INET6
; 
1586         mask6
.sin6_len 
= sizeof(sa6
); 
1587         bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof(struct in6_addr
)); 
1588         lck_mtx_lock(rt_mtx
); 
1589         error 
= rtrequest_locked(RTM_DELETE
, (struct sockaddr 
*)&sa6
, NULL
, 
1590                           (struct sockaddr 
*)&mask6
, 0, &rt
); 
1592                 pr
->ndpr_stateflags 
&= ~NDPRF_ONLINK
; 
1594                 /* report the route deletion to the routing socket. */ 
1596                         nd6_rtmsg(RTM_DELETE
, rt
); 
1599                  * There might be the same prefix on another interface, 
1600                  * the prefix which could not be on-link just because we have 
1601                  * the interface route (see comments in nd6_prefix_onlink). 
1602                  * If there's one, try to make the prefix on-link on the 
1605                 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
); 
1606                 for (opr 
= nd_prefix
.lh_first
; opr
; opr 
= opr
->ndpr_next
) { 
1610                         if ((opr
->ndpr_stateflags 
& NDPRF_ONLINK
) != 0) 
1614                          * KAME specific: detached prefixes should not be 
1617                         if ((opr
->ndpr_stateflags 
& NDPRF_DETACHED
) != 0) 
1620                         if (opr
->ndpr_plen 
== pr
->ndpr_plen 
&& 
1621                             in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
, 
1622                                                  &opr
->ndpr_prefix
.sin6_addr
, 
1626                                 if ((e 
= nd6_prefix_onlink(opr
, 1, 1)) != 0) { 
1628                                             "nd6_prefix_offlink: failed to " 
1629                                             "recover a prefix %s/%d from %s " 
1630                                             "to %s (errno = %d)\n", 
1631                                             ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
), 
1632                                             opr
->ndpr_plen
, if_name(ifp
), 
1633                                             if_name(opr
->ndpr_ifp
), e
)); 
1639                 /* XXX: can we still set the NDPRF_ONLINK flag? */ 
1641                     "nd6_prefix_offlink: failed to delete route: " 
1642                     "%s/%d on %s (errno = %d)\n", 
1643                     ip6_sprintf(&sa6
.sin6_addr
), pr
->ndpr_plen
, if_name(ifp
), 
1648                 if (rt
->rt_refcnt 
<= 0) { 
1649                         /* XXX: we should free the entry ourselves. */ 
1654         lck_mtx_unlock(rt_mtx
); 
1659 static struct in6_ifaddr 
* 
1661         struct nd_prefix 
*pr
, 
1662         struct in6_addr  
*ifid
)   /* Mobile IPv6 addition */ 
1664         struct ifnet 
*ifp 
= pr
->ndpr_ifp
; 
1666         struct in6_aliasreq ifra
; 
1667         struct in6_ifaddr 
*ia
, *ib
; 
1669         struct in6_addr mask
; 
1670         int prefixlen 
= pr
->ndpr_plen
; 
1672         in6_len2mask(&mask
, prefixlen
); 
1675          * find a link-local address (will be interface ID). 
1676          * Is it really mandatory? Theoretically, a global or a site-local 
1677          * address can be configured without a link-local address, if we 
1678          * have a unique interface identifier... 
1680          * it is not mandatory to have a link-local address, we can generate 
1681          * interface identifier on the fly.  we do this because: 
1682          * (1) it should be the easiest way to find interface identifier. 
1683          * (2) RFC2462 5.4 suggesting the use of the same interface identifier 
1684          * for multiple addresses on a single interface, and possible shortcut 
1685          * of DAD.  we omitted DAD for this reason in the past. 
1686          * (3) a user can prevent autoconfiguration of global address  
1687          * by removing link-local address by hand (this is partly because we 
1688          * don't have other way to control the use of IPv6 on a interface. 
1689          * this has been our design choice - cf. NRL's "ifconfig auto"). 
1690          * (4) it is easier to manage when an interface has addresses 
1691          * with the same interface identifier, than to have multiple addresses 
1692          * with different interface identifiers. 
1694          * Mobile IPv6 addition: allow for caller to specify a wished interface 
1695          * ID. This is to not break connections when moving addresses between 
1698         ifa 
= (struct ifaddr 
*)in6ifa_ifpforlinklocal(ifp
, 0);/* 0 is OK? */ 
1700                 ib 
= (struct in6_ifaddr 
*)ifa
; 
1704 #if 0 /* don't care link local addr state, and always do DAD */ 
1705         /* if link-local address is not eligible, do not autoconfigure. */ 
1706         if (((struct in6_ifaddr 
*)ifa
)->ia6_flags 
& IN6_IFF_NOTREADY
) { 
1707                 printf("in6_ifadd: link-local address not ready\n"); 
1712         /* prefixlen + ifidlen must be equal to 128 */ 
1713         plen0 
= in6_mask2len(&ib
->ia_prefixmask
.sin6_addr
, NULL
); 
1714         if (prefixlen 
!= plen0
) { 
1715                 nd6log((LOG_INFO
, "in6_ifadd: wrong prefixlen for %s " 
1716                     "(prefix=%d ifid=%d)\n", 
1717                     if_name(ifp
), prefixlen
, 128 - plen0
)); 
1723         bzero(&ifra
, sizeof(ifra
)); 
1725          * in6_update_ifa() does not use ifra_name, but we accurately set it 
1728         strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
)); 
1729         ifra
.ifra_addr
.sin6_family 
= AF_INET6
; 
1730         ifra
.ifra_addr
.sin6_len 
= sizeof(struct sockaddr_in6
); 
1732         bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
, 
1733               sizeof(ifra
.ifra_addr
.sin6_addr
)); 
1734         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0]; 
1735         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1]; 
1736         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2]; 
1737         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3]; 
1740         if (ifid 
== NULL 
|| IN6_IS_ADDR_UNSPECIFIED(ifid
)) 
1741                 ifid 
= &ib
->ia_addr
.sin6_addr
; 
1742         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] 
1743                 |= (ifid
->s6_addr32
[0] & ~mask
.s6_addr32
[0]); 
1744         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] 
1745                 |= (ifid
->s6_addr32
[1] & ~mask
.s6_addr32
[1]); 
1746         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] 
1747                 |= (ifid
->s6_addr32
[2] & ~mask
.s6_addr32
[2]); 
1748         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] 
1749                 |= (ifid
->s6_addr32
[3] & ~mask
.s6_addr32
[3]); 
1751         /* new prefix mask. */ 
1752         ifra
.ifra_prefixmask
.sin6_len 
= sizeof(struct sockaddr_in6
); 
1753         ifra
.ifra_prefixmask
.sin6_family 
= AF_INET6
; 
1754         bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
, 
1755               sizeof(ifra
.ifra_prefixmask
.sin6_addr
)); 
1759          * XXX: in6_init_address_ltimes would override these values later. 
1760          * We should reconsider this logic.  
1762         ifra
.ifra_lifetime
.ia6t_vltime 
= pr
->ndpr_vltime
; 
1763         ifra
.ifra_lifetime
.ia6t_pltime 
= pr
->ndpr_pltime
; 
1765         /* XXX: scope zone ID? */ 
1767         ifra
.ifra_flags 
|= IN6_IFF_AUTOCONF
; /* obey autoconf */ 
1769          * temporarily set the nopfx flag to avoid conflict. 
1770          * XXX: we should reconsider the entire mechanism about prefix 
1773         ifra
.ifra_flags 
|= IN6_IFF_NOPFX
; 
1776          * keep the new address, regardless of the result of in6_update_ifa. 
1777          * XXX: this address is now meaningless. 
1778          * We should reconsider its role. 
1780         pr
->ndpr_addr 
= ifra
.ifra_addr
.sin6_addr
; 
1782         /* allocate ifaddr structure, link into chain, etc. */ 
1783         if ((error 
= in6_update_ifa(ifp
, &ifra
, NULL
)) != 0) { 
1785                     "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 
1786                     ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
), if_name(ifp
), 
1788                 return(NULL
);   /* ifaddr must not have been allocated. */ 
1791         ia 
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
); 
1793         in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia
);  
1795         return(ia
);             /* this must NOT be NULL. */ 
1800         const struct in6_ifaddr 
*ia0
, /* corresponding public address */ 
1803         struct ifnet 
*ifp 
= ia0
->ia_ifa
.ifa_ifp
; 
1804         struct in6_ifaddr 
*newia
; 
1805         struct in6_aliasreq ifra
; 
1807         int trylimit 
= 3;       /* XXX: adhoc value */ 
1808         u_int32_t randid
[2]; 
1809         time_t vltime0
, pltime0
; 
1810         struct timeval timenow
; 
1812         getmicrotime(&timenow
); 
1814         bzero(&ifra
, sizeof(ifra
)); 
1815         strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
)); 
1816         ifra
.ifra_addr 
= ia0
->ia_addr
; 
1817         /* copy prefix mask */ 
1818         ifra
.ifra_prefixmask 
= ia0
->ia_prefixmask
; 
1819         /* clear the old IFID */ 
1820         for (i 
= 0; i 
< 4; i
++) { 
1821                 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
] 
1822                         &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
]; 
1826         in6_get_tmpifid(ifp
, (u_int8_t 
*)randid
, 
1827                         (const u_int8_t 
*)&ia0
->ia_addr
.sin6_addr
.s6_addr
[8], 
1829         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] 
1830                 |= (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2])); 
1831         ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] 
1832                 |= (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3])); 
1835          * If by chance the new temporary address is the same as an address 
1836          * already assigned to the interface, generate a new randomized 
1837          * interface identifier and repeat this step. 
1840         if (in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
) != NULL
) { 
1841                 if (trylimit
-- == 0) { 
1842                         nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find " 
1843                             "a unique random IFID\n")); 
1851          * The Valid Lifetime is the lower of the Valid Lifetime of the 
1852          * public address or TEMP_VALID_LIFETIME. 
1853          * The Preferred Lifetime is the lower of the Preferred Lifetime 
1854          * of the public address or TEMP_PREFERRED_LIFETIME - 
1857         if (ia0
->ia6_lifetime
.ia6t_expire 
!= 0) { 
1858                 vltime0 
= IFA6_IS_INVALID(ia0
) ? 0 : 
1859                         (ia0
->ia6_lifetime
.ia6t_expire 
- timenow
.tv_sec
); 
1860                 if (vltime0 
> ip6_temp_valid_lifetime
) 
1861                         vltime0 
= ip6_temp_valid_lifetime
; 
1863                 vltime0 
= ip6_temp_valid_lifetime
; 
1864         if (ia0
->ia6_lifetime
.ia6t_preferred 
!= 0) { 
1865                 pltime0 
= IFA6_IS_DEPRECATED(ia0
) ? 0 : 
1866                         (ia0
->ia6_lifetime
.ia6t_preferred 
- timenow
.tv_sec
); 
1867                 if (pltime0 
> ip6_temp_preferred_lifetime 
- ip6_desync_factor
){ 
1868                         pltime0 
= ip6_temp_preferred_lifetime 
- 
1872                 pltime0 
= ip6_temp_preferred_lifetime 
- ip6_desync_factor
; 
1873         ifra
.ifra_lifetime
.ia6t_vltime 
= vltime0
; 
1874         ifra
.ifra_lifetime
.ia6t_pltime 
= pltime0
; 
1877          * A temporary address is created only if this calculated Preferred 
1878          * Lifetime is greater than REGEN_ADVANCE time units. 
1880         if (ifra
.ifra_lifetime
.ia6t_pltime 
<= ip6_temp_regen_advance
) 
1883         /* XXX: scope zone ID? */ 
1885         ifra
.ifra_flags 
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
); 
1887         /* allocate ifaddr structure, link into chain, etc. */ 
1888         if ((error 
= in6_update_ifa(ifp
, &ifra
, NULL
)) != 0) 
1891         newia 
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
); 
1892         if (newia 
== NULL
) {    /* XXX: can it happen? */ 
1894                     "in6_tmpifadd: ifa update succeeded, but we got " 
1896                 return(EINVAL
); /* XXX */ 
1898         newia
->ia6_ndpr 
= ia0
->ia6_ndpr
; 
1899         newia
->ia6_ndpr
->ndpr_refcnt
++; 
1902          * A newly added address might affect the status of other addresses. 
1903          * XXX: when the temporary address is generated with a new public 
1904          * address, the onlink check is redundant.  However, it would be safe 
1905          * to do the check explicitly everywhere a new address is generated, 
1906          * and, in fact, we surely need the check when we create a new 
1907          * temporary address due to deprecation of an old temporary address. 
1909         pfxlist_onlink_check(0); 
1915 in6_init_prefix_ltimes(struct nd_prefix 
*ndpr
) 
1917         struct timeval timenow
; 
1919         getmicrotime(&timenow
); 
1920         /* check if preferred lifetime > valid lifetime.  RFC2462 5.5.3 (c) */ 
1921         if (ndpr
->ndpr_pltime 
> ndpr
->ndpr_vltime
) { 
1922                 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime" 
1923                     "(%d) is greater than valid lifetime(%d)\n", 
1924                     (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
)); 
1927         if (ndpr
->ndpr_pltime 
== ND6_INFINITE_LIFETIME
) 
1928                 ndpr
->ndpr_preferred 
= 0; 
1930                 ndpr
->ndpr_preferred 
= timenow
.tv_sec 
+ ndpr
->ndpr_pltime
; 
1931         if (ndpr
->ndpr_vltime 
== ND6_INFINITE_LIFETIME
) 
1932                 ndpr
->ndpr_expire 
= 0; 
1934                 ndpr
->ndpr_expire 
= timenow
.tv_sec 
+ ndpr
->ndpr_vltime
; 
1940 in6_init_address_ltimes(struct nd_prefix 
*new, struct in6_addrlifetime 
*lt6
) 
1942         struct timeval timenow
; 
1944         getmicrotime(&timenow
); 
1945         /* Valid lifetime must not be updated unless explicitly specified. */ 
1946         /* init ia6t_expire */ 
1947         if (lt6
->ia6t_vltime 
== ND6_INFINITE_LIFETIME
) 
1948                 lt6
->ia6t_expire 
= 0; 
1950                 lt6
->ia6t_expire 
= timenow
.tv_sec
; 
1951                 lt6
->ia6t_expire 
+= lt6
->ia6t_vltime
; 
1954         /* init ia6t_preferred */ 
1955         if (lt6
->ia6t_pltime 
== ND6_INFINITE_LIFETIME
) 
1956                 lt6
->ia6t_preferred 
= 0; 
1958                 lt6
->ia6t_preferred 
= timenow
.tv_sec
; 
1959                 lt6
->ia6t_preferred 
+= lt6
->ia6t_pltime
; 
1964  * Delete all the routing table entries that use the specified gateway. 
1965  * XXX: this function causes search through all entries of routing table, so 
1966  * it shouldn't be called when acting as a router. 
1970         struct in6_addr 
*gateway
, 
1973         struct radix_node_head 
*rnh 
= rt_tables
[AF_INET6
]; 
1975         /* We'll care only link-local addresses */ 
1976         if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) { 
1979         lck_mtx_lock(rt_mtx
); 
1980         /* XXX: hack for KAME's link-local address kludge */ 
1981         gateway
->s6_addr16
[1] = htons(ifp
->if_index
); 
1983         rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
); 
1984         lck_mtx_unlock(rt_mtx
); 
1989         struct radix_node 
*rn
, 
1992 #define SIN6(s) ((struct sockaddr_in6 *)s) 
1993         struct rtentry 
*rt 
= (struct rtentry 
*)rn
; 
1994         struct in6_addr 
*gate 
= (struct in6_addr 
*)arg
; 
1996         lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
); 
1998         if (rt
->rt_gateway 
== NULL 
|| rt
->rt_gateway
->sa_family 
!= AF_INET6
) 
2001         if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) 
2005          * Do not delete a static route. 
2006          * XXX: this seems to be a bit ad-hoc. Should we consider the 
2007          * 'cloned' bit instead? 
2009         if ((rt
->rt_flags 
& RTF_STATIC
) != 0) 
2013          * We delete only host route. This means, in particular, we don't 
2014          * delete default route. 
2016         if ((rt
->rt_flags 
& RTF_HOST
) == 0) 
2019         return(rtrequest_locked(RTM_DELETE
, rt_key(rt
), 
2020                          rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0)); 
2025 nd6_setdefaultiface( 
2030         if (ifindex 
< 0 || if_index 
< ifindex
) 
2033         lck_mtx_lock(nd6_mutex
); 
2034         if (nd6_defifindex 
!= ifindex
) { 
2035                 nd6_defifindex 
= ifindex
; 
2036                 if (nd6_defifindex 
> 0) 
2037                         nd6_defifp 
= ifindex2ifnet
[nd6_defifindex
]; 
2042                  * If the Default Router List is empty, install a route 
2043                  * to the specified interface as default or remove the default 
2044                  * route when the default interface becomes canceled. 
2045                  * The check for the queue is actually redundant, but 
2046                  * we do this here to avoid re-install the default route 
2047                  * if the list is NOT empty. 
2049                 if (TAILQ_FIRST(&nd_defrouter
) == NULL
) 
2053                  * Our current implementation assumes one-to-one maping between 
2054                  * interfaces and links, so it would be natural to use the 
2055                  * default interface as the default link. 
2057                 scope6_setdefault(nd6_defifp
); 
2060         lck_mtx_unlock(nd6_mutex
);