]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet6/in6_src.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_src.c
index 1eb5cd60fb98f2e23afdbd5cee60c0a4a56f8d6b..86b703bf5c64d4b45bf6c6db316786e8b8771460 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
-/*     $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.2 2001/07/03 11:01:52 ume Exp $    */
-/*     $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $        */
-
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  * All rights reserved.
 #include <sys/sysctl.h>
 #include <sys/kauth.h>
 #include <sys/priv.h>
-#include <kern/lock.h>
+#include <kern/locks.h>
 
 #include <net/if.h>
 #include <net/if_types.h>
@@ -133,10 +130,10 @@ SYSCTL_DECL(_net_inet6_ip6);
 
 static int ip6_select_srcif_debug = 0;
 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, select_srcif_debug,
-    CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_select_srcif_debug, 0,
-    "log source interface selection debug info");
+       CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_select_srcif_debug, 0,
+       "log source interface selection debug info");
 
-#define ADDR_LABEL_NOTAPP (-1)
+#define        ADDR_LABEL_NOTAPP (-1)
 struct in6_addrpolicy defaultaddrpolicy;
 
 int ip6_prefer_tempaddr = 1;
@@ -150,12 +147,12 @@ extern lck_mtx_t *addrsel_mutex;
 #endif
 
 static int selectroute(struct sockaddr_in6 *, struct sockaddr_in6 *,
-       struct ip6_pktopts *, struct ip6_moptions *, struct route_in6 *,
-       struct ifnet **, struct rtentry **, int, int, unsigned int,
-       unsigned int);
+       struct ip6_pktopts *, struct ip6_moptions *, struct in6_ifaddr **,
+       struct route_in6 *, struct ifnet **, struct rtentry **, int, int,
+       struct ip6_out_args *ip6oa);
 static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
-       struct ip6_moptions *, struct route_in6 *ro, unsigned int,
-       unsigned int, struct ifnet **);
+       struct ip6_moptions *, struct route_in6 *ro,
+       struct ip6_out_args *, struct ifnet **);
 static void init_policy_queue(void);
 static int add_addrsel_policyent(const struct in6_addrpolicy *);
 #ifdef ENABLE_ADDRSEL
@@ -173,25 +170,30 @@ void addrsel_policy_init(void);
  * If necessary, this function lookups the routing table and returns
  * an entry to the caller for later use.
  */
-#define REPLACE(r) do {\
-       if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
-               sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
+#define        REPLACE(r) do {\
+       if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
+               sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
                ip6stat.ip6s_sources_rule[(r)]++; \
        goto replace; \
-} while(0)
-#define NEXTSRC(r) do {\
-       if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
-               sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
+} while (0)
+#define        NEXTSRC(r) do {\
+       if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
+               sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
                ip6stat.ip6s_sources_rule[(r)]++; \
        goto next;              /* XXX: we can't use 'continue' here */ \
-} while(0)
-#define BREAK(r) do { \
-       if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
-               sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
+} while (0)
+#define        BREAK(r) do { \
+       if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
+               sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
                ip6stat.ip6s_sources_rule[(r)]++; \
        goto out;               /* XXX: we can't use 'break' here */ \
-} while(0)
+} while (0)
 
+/*
+ * Regardless of error, it will return an ifp with a reference held if the
+ * caller provides a non-NULL ifpp.  The caller is responsible for checking
+ * if the returned ifp is valid and release its reference at all times.
+ */
 struct in6_addr *
 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
     struct inpcb *inp, struct route_in6 *ro,
@@ -207,11 +209,9 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
        u_int32_t odstzone;
        int prefer_tempaddr;
        struct ip6_moptions *mopts;
-       struct timeval timenow;
-       unsigned int nocell;
+       struct ip6_out_args ip6oa = { ifscope, { 0 }, IP6OAF_SELECT_SRCIF, 0 };
        boolean_t islocal = FALSE;
-
-       getmicrotime(&timenow);
+       uint64_t secs = net_uptime();
 
        dst = dstsock->sin6_addr; /* make a copy for local operation */
        *errorp = 0;
@@ -220,12 +220,20 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
 
        if (inp != NULL) {
                mopts = inp->in6p_moptions;
-               nocell = (inp->inp_flags & INP_NO_IFT_CELLULAR) ? 1 : 0;
+               if (INP_NO_CELLULAR(inp))
+                       ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
+               if (INP_NO_EXPENSIVE(inp))
+                       ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
+               if (INP_AWDL_UNRESTRICTED(inp))
+                       ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
+
        } else {
                mopts = NULL;
-               nocell = 0;
        }
 
+       if (ip6oa.ip6oa_boundif != IFSCOPE_NONE)
+               ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
+
        /*
         * If the source address is explicitly specified by the caller,
         * check if the requested source address is indeed a unicast address
@@ -238,9 +246,10 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
                struct in6_ifaddr *ia6;
 
                /* get the outgoing interface */
-               if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, ifscope,
-                   nocell, &ifp)) != 0) {
-                       return (NULL);
+               if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa,
+                   &ifp)) != 0) {
+                       src_storage = NULL;
+                       goto done;
                }
 
                /*
@@ -250,52 +259,47 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
                 * the interface must be specified; otherwise, ifa_ifwithaddr()
                 * will fail matching the address.
                 */
-               bzero(&srcsock, sizeof(srcsock));
+               bzero(&srcsock, sizeof (srcsock));
                srcsock.sin6_family = AF_INET6;
-               srcsock.sin6_len = sizeof(srcsock);
+               srcsock.sin6_len = sizeof (srcsock);
                srcsock.sin6_addr = pi->ipi6_addr;
-               if (ifp) {
+               if (ifp != NULL) {
                        *errorp = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
                        if (*errorp != 0) {
-                               ifnet_release(ifp);
-                               return (NULL);
+                               src_storage = NULL;
+                               goto done;
                        }
                }
-               ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)(&srcsock));
+               ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)
+                   (&srcsock));
                if (ia6 == NULL) {
                        *errorp = EADDRNOTAVAIL;
-                       if (ifp != NULL)
-                               ifnet_release(ifp);
-                       return (NULL);
+                       src_storage = NULL;
+                       goto done;
                }
                IFA_LOCK_SPIN(&ia6->ia_ifa);
                if ((ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) ||
-                   (nocell && (ia6->ia_ifa.ifa_ifp->if_type == IFT_CELLULAR))) {
+                   (inp && inp_restricted_send(inp, ia6->ia_ifa.ifa_ifp))) {
                        IFA_UNLOCK(&ia6->ia_ifa);
                        IFA_REMREF(&ia6->ia_ifa);
-                       *errorp = EADDRNOTAVAIL;
-                       if (ifp != NULL)
-                               ifnet_release(ifp);
-                       return (NULL);
+                       *errorp = EHOSTUNREACH;
+                       src_storage = NULL;
+                       goto done;
                }
 
                *src_storage = satosin6(&ia6->ia_addr)->sin6_addr;
                IFA_UNLOCK(&ia6->ia_ifa);
                IFA_REMREF(&ia6->ia_ifa);
-               if (ifpp != NULL) {
-                       /* if ifp is non-NULL, refcnt held in in6_selectif() */
-                       *ifpp = ifp;
-               } else if (ifp != NULL) {
-                       ifnet_release(ifp);
-               }
-               return (src_storage);
+               goto done;
        }
 
        /*
         * Otherwise, if the socket has already bound the source, just use it.
         */
-       if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 
-               return (&inp->in6p_laddr);
+       if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
+               src_storage = &inp->in6p_laddr;
+               goto done;
+       }
 
        /*
         * If the address is not specified, choose the best one based on
@@ -303,19 +307,16 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
         */
 
        /* get the outgoing interface */
-       if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, ifscope, nocell,
-           &ifp)) != 0)
-               return (NULL);
+       if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa,
+           &ifp)) != 0) {
+               src_storage = NULL;
+               goto done;
+       }
 
-#ifdef DIAGNOSTIC
-       if (ifp == NULL)        /* this should not happen */
-               panic("in6_selectsrc: NULL ifp");
-#endif
        *errorp = in6_setscope(&dst, ifp, &odstzone);
        if (*errorp != 0) {
-               if (ifp != NULL)
-                       ifnet_release(ifp);
-               return (NULL);
+               src_storage = NULL;
+               goto done;
        }
        lck_rw_lock_shared(&in6_ifaddr_rwlock);
 
@@ -345,18 +346,22 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
 
                /* avoid unusable addresses */
                if ((ia->ia6_flags &
-                    (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED)))
+                   (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED)))
                        goto next;
 
-               if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
+               if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia, secs))
+                       goto next;
+
+               if (!nd6_optimistic_dad &&
+                   (ia->ia6_flags & IN6_IFF_OPTIMISTIC) != 0)
                        goto next;
 
                /* Rule 1: Prefer same address */
                if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr))
-                       BREAK(1); /* there should be no better candidate */
+                       BREAK(IP6S_SRCRULE_1); /* there should be no better candidate */
 
                if (ia_best == NULL)
-                       REPLACE(0);
+                       REPLACE(IP6S_SRCRULE_0);
 
                /* Rule 2: Prefer appropriate scope */
                if (dst_scope < 0)
@@ -364,22 +369,35 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
                new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
                if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
                        if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
-                               REPLACE(2);
-                       NEXTSRC(2);
+                               REPLACE(IP6S_SRCRULE_2);
+                       NEXTSRC(IP6S_SRCRULE_2);
                } else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
                        if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
-                               NEXTSRC(2);
-                       REPLACE(2);
+                               NEXTSRC(IP6S_SRCRULE_2);
+                       REPLACE(IP6S_SRCRULE_2);
                }
 
                /*
                 * Rule 3: Avoid deprecated addresses.  Note that the case of
                 * !ip6_use_deprecated is already rejected above.
                 */
-               if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
-                       NEXTSRC(3);
-               if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
-                       REPLACE(3);
+               if (!IFA6_IS_DEPRECATED(ia_best, secs) &&
+                   IFA6_IS_DEPRECATED(ia, secs))
+                       NEXTSRC(IP6S_SRCRULE_3);
+               if (IFA6_IS_DEPRECATED(ia_best, secs) &&
+                   !IFA6_IS_DEPRECATED(ia, secs))
+                       REPLACE(IP6S_SRCRULE_3);
+
+               /*
+                * RFC 4429 says that optimistic addresses are equivalent to
+                * deprecated addresses, so avoid them here.
+                */
+               if ((ia_best->ia6_flags & IN6_IFF_OPTIMISTIC) == 0 &&
+                   (ia->ia6_flags & IN6_IFF_OPTIMISTIC) != 0)
+                       NEXTSRC(IP6S_SRCRULE_3);
+               if ((ia_best->ia6_flags & IN6_IFF_OPTIMISTIC) != 0 &&
+                   (ia->ia6_flags & IN6_IFF_OPTIMISTIC) == 0)
+                       REPLACE(IP6S_SRCRULE_3);
 
                /* Rule 4: Prefer home addresses */
                /*
@@ -389,9 +407,53 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
 
                /* Rule 5: Prefer outgoing interface */
                if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
-                       NEXTSRC(5);
+                       NEXTSRC(IP6S_SRCRULE_5);
                if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
-                       REPLACE(5);
+                       REPLACE(IP6S_SRCRULE_5);
+
+               /* Rule 5.5: Prefer addresses in a prefix advertised by the next hop. */
+               if (ro != NULL && ro->ro_rt != NULL && ia_best->ia6_ndpr != NULL &&
+                   ia->ia6_ndpr != NULL) {
+                       struct rtentry *rta, *rtb;
+                       int op;
+
+                       NDPR_LOCK(ia_best->ia6_ndpr);
+                       rta = ia_best->ia6_ndpr->ndpr_rt;
+                       if (rta != NULL)
+                               RT_ADDREF(rta);
+                       NDPR_UNLOCK(ia_best->ia6_ndpr);
+
+                       NDPR_LOCK(ia->ia6_ndpr);
+                       rtb = ia->ia6_ndpr->ndpr_rt;
+                       if (rtb != NULL)
+                               RT_ADDREF(rtb);
+                       NDPR_UNLOCK(ia->ia6_ndpr);
+
+                       if (rta == NULL || rtb == NULL)
+                               op = 0;
+                       else if (rta == ro->ro_rt && rtb != ro->ro_rt)
+                               op = 1;
+                       else if (rta != ro->ro_rt && rtb == ro->ro_rt)
+                               op = 2;
+                       else
+                               op = 0;
+
+                       if (rta != NULL)
+                               RT_REMREF(rta);
+                       if (rtb != NULL)
+                               RT_REMREF(rtb);
+
+                       switch (op) {
+                       case 1:
+                               NEXTSRC(IP6S_SRCRULE_5_5);
+                               break;
+                       case 2:
+                               REPLACE(IP6S_SRCRULE_5_5);
+                               break;
+                       default:
+                               break;
+                       }
+               }
 
                /*
                 * Rule 6: Prefer matching label
@@ -403,23 +465,23 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
                        new_policy = in6_addrsel_lookup_policy(&ia->ia_addr);
                        if (dst_policy->label == best_policy->label &&
                            dst_policy->label != new_policy->label)
-                               NEXTSRC(6);
+                               NEXTSRC(IP6S_SRCRULE_6);
                        if (dst_policy->label != best_policy->label &&
                            dst_policy->label == new_policy->label)
-                               REPLACE(6);
+                               REPLACE(IP6S_SRCRULE_6);
                }
 
                /*
-                * Rule 7: Prefer public addresses.
+                * Rule 7: Prefer temporary addresses.
                 * We allow users to reverse the logic by configuring
-                * a sysctl variable, so that privacy conscious users can
-                * always prefer temporary addresses.
+                * a sysctl variable, so that transparency conscious users can
+                * always prefer stable addresses.
                 * Don't use temporary addresses for local destinations or
                 * for multicast addresses unless we were passed in an option.
                 */
                if (IN6_IS_ADDR_MULTICAST(&dst) ||
                    in6_matchlen(&ia_best->ia_addr.sin6_addr, &dst) >=
-                   in6_mask2len(&ia_best->ia_prefixmask.sin6_addr, NULL))
+                   ia_best->ia_plen)
                        islocal = TRUE;
                if (opts == NULL ||
                    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
@@ -432,44 +494,37 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
                if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
                    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
                        if (prefer_tempaddr)
-                               REPLACE(7);
+                               REPLACE(IP6S_SRCRULE_7);
                        else
-                               NEXTSRC(7);
+                               NEXTSRC(IP6S_SRCRULE_7);
                }
                if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
                    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
                        if (prefer_tempaddr)
-                               NEXTSRC(7);
+                               NEXTSRC(IP6S_SRCRULE_7);
                        else
-                               REPLACE(7);
+                               REPLACE(IP6S_SRCRULE_7);
                }
 
                /*
-                * Rule 8: prefer addresses on alive interfaces.
+                * Rule 7x: prefer addresses on alive interfaces.
                 * This is a KAME specific rule.
                 */
                if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
                    !(ia->ia_ifp->if_flags & IFF_UP))
-                       NEXTSRC(8);
+                       NEXTSRC(IP6S_SRCRULE_7x);
                if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
                    (ia->ia_ifp->if_flags & IFF_UP))
-                       REPLACE(8);
+                       REPLACE(IP6S_SRCRULE_7x);
 
                /*
-                * Rule 14: Use longest matching prefix.
-                * Note: in the address selection draft, this rule is
-                * documented as "Rule 8".  However, since it is also
-                * documented that this rule can be overridden, we assign
-                * a large number so that it is easy to assign smaller numbers
-                * to more preferred rules.
+                * Rule 8: Use longest matching prefix.
                 */
                new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst);
                if (best_matchlen < new_matchlen)
-                       REPLACE(14);
+                       REPLACE(IP6S_SRCRULE_8);
                if (new_matchlen < best_matchlen)
-                       NEXTSRC(14);
-
-               /* Rule 15 is reserved. */
+                       NEXTSRC(IP6S_SRCRULE_8);
 
                /*
                 * Last resort: just keep the current candidate.
@@ -480,11 +535,11 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
 
 replace:
                best_scope = (new_scope >= 0 ? new_scope :
-                             in6_addrscope(&ia->ia_addr.sin6_addr));
+                   in6_addrscope(&ia->ia_addr.sin6_addr));
                best_policy = (new_policy ? new_policy :
-                              in6_addrsel_lookup_policy(&ia->ia_addr));
+                   in6_addrsel_lookup_policy(&ia->ia_addr));
                best_matchlen = (new_matchlen >= 0 ? new_matchlen :
-                                in6_matchlen(&ia->ia_addr.sin6_addr, &dst));
+                   in6_matchlen(&ia->ia_addr.sin6_addr, &dst));
                IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for ia_best */
                IFA_UNLOCK(&ia->ia_ifa);
                if (ia_best != NULL)
@@ -507,23 +562,25 @@ out:
 
        lck_rw_done(&in6_ifaddr_rwlock);
 
-       if (nocell && ia_best != NULL &&
-           (ia_best->ia_ifa.ifa_ifp->if_type == IFT_CELLULAR)) {
+       if (ia_best != NULL && inp && 
+           inp_restricted_send(inp, ia_best->ia_ifa.ifa_ifp)) {
                IFA_REMREF(&ia_best->ia_ifa);
                ia_best = NULL;
+               *errorp = EHOSTUNREACH;
        }
 
-       if ( (ia = ia_best) == NULL) {
-               *errorp = EADDRNOTAVAIL;
-               if (ifp != NULL)
-                       ifnet_release(ifp);
-               return (NULL);
+       if ((ia = ia_best) == NULL) {
+               if (*errorp == 0)
+                       *errorp = EADDRNOTAVAIL;
+               src_storage = NULL;
+               goto done;
        }
 
        IFA_LOCK_SPIN(&ia->ia_ifa);
        *src_storage = satosin6(&ia->ia_addr)->sin6_addr;
        IFA_UNLOCK(&ia->ia_ifa);
        IFA_REMREF(&ia->ia_ifa);
+done:
        if (ifpp != NULL) {
                /* if ifp is non-NULL, refcnt held in in6_selectif() */
                *ifpp = ifp;
@@ -542,25 +599,31 @@ out:
  * i.e. for any given pcb there can only be one thread performing output at
  * the IPv6 layer.
  *
- * This routine is analogous to in_selectsrcif() for IPv4.
+ * This routine is analogous to in_selectsrcif() for IPv4.  Regardless of
+ * error, it will return an ifp with a reference held if the caller provides
+ * a non-NULL retifp.  The caller is responsible for checking if the
+ * returned ifp is valid and release its reference at all times.
  *
  * clone - meaningful only for bsdi and freebsd
  */
 static int
 selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
-    struct ip6_pktopts *opts, struct ip6_moptions *mopts, struct route_in6 *ro,
+    struct ip6_pktopts *opts, struct ip6_moptions *mopts,
+    struct in6_ifaddr **retsrcia, struct route_in6 *ro,
     struct ifnet **retifp, struct rtentry **retrt, int clone,
-    int norouteok, unsigned int ifscope, unsigned int nocell)
+    int norouteok, struct ip6_out_args *ip6oa)
 {
        int error = 0;
-       struct ifnet *ifp = NULL;
+       struct ifnet *ifp = NULL, *ifp0 = NULL;
        struct route_in6 *route = NULL;
        struct sockaddr_in6 *sin6_next;
        struct in6_pktinfo *pi = NULL;
        struct in6_addr *dst = &dstsock->sin6_addr;
        struct ifaddr *ifa = NULL;
        char s_src[MAX_IPv6_STR_LEN], s_dst[MAX_IPv6_STR_LEN];
-       boolean_t select_srcif;
+       boolean_t select_srcif, proxied_ifa = FALSE, local_dst = FALSE;
+       unsigned int ifscope = ((ip6oa != NULL) ?
+           ip6oa->ip6oa_boundif : IFSCOPE_NONE);
 
 #if 0
        char ip6buf[INET6_ADDRSTRLEN];
@@ -569,11 +632,11 @@ selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
            dstsock->sin6_addr.s6_addr32[1] == 0 &&
            !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
                printf("in6_selectroute: strange destination %s\n",
-                      ip6_sprintf(ip6buf, &dstsock->sin6_addr));
+                   ip6_sprintf(ip6buf, &dstsock->sin6_addr));
        } else {
                printf("in6_selectroute: destination = %s%%%d\n",
-                      ip6_sprintf(ip6buf, &dstsock->sin6_addr),
-                      dstsock->sin6_scope_id); /* for debug */
+                   ip6_sprintf(ip6buf, &dstsock->sin6_addr),
+                   dstsock->sin6_scope_id); /* for debug */
        }
 #endif
 
@@ -622,7 +685,8 @@ selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
                ifscope = pi->ipi6_ifindex;
                ifnet_head_lock_shared();
                /* ifp may be NULL if detached or out of range */
-               ifp = (ifscope <= if_index) ? ifindex2ifnet[ifscope] : NULL;
+               ifp = ifp0 =
+                   ((ifscope <= if_index) ? ifindex2ifnet[ifscope] : NULL);
                ifnet_head_done();
                if (norouteok || retrt == NULL || IN6_IS_ADDR_MULTICAST(dst)) {
                        /*
@@ -645,7 +709,7 @@ selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
         */
        if (IN6_IS_ADDR_MULTICAST(dst) && mopts != NULL) {
                IM6O_LOCK(mopts);
-               if ((ifp = mopts->im6o_multicast_ifp) != NULL) {
+               if ((ifp = ifp0 = mopts->im6o_multicast_ifp) != NULL) {
                        IM6O_UNLOCK(mopts);
                        goto done; /* we do not need a route for multicast. */
                }
@@ -668,14 +732,14 @@ getsrcif:
         * or if we haven't done source interface selection on this
         * route (for this PCB instance) before.
         */
-       if (!select_srcif || (ro != NULL && ro->ro_rt != NULL &&
-           (ro->ro_rt->rt_flags & RTF_UP) &&
-           ro->ro_rt->generation_id == route_generation &&
-           (ro->ro_flags & ROF_SRCIF_SELECTED))) {
-               if (ro != NULL && ro->ro_rt != NULL) {
-                       ifa = ro->ro_rt->rt_ifa;
-                       IFA_ADDREF(ifa);
-               }
+       if (!select_srcif) {
+               goto getroute;
+       } else if (!ROUTE_UNUSABLE(ro) && ro->ro_srcia != NULL &&
+           (ro->ro_flags & ROF_SRCIF_SELECTED)) {
+               if (ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
+                       local_dst = TRUE;
+               ifa = ro->ro_srcia;
+               IFA_ADDREF(ifa);        /* for caller */
                goto getroute;
        }
 
@@ -704,13 +768,28 @@ getsrcif:
                if (scope == IFSCOPE_NONE) {
                        scope = rt_ifp->if_index;
                        if (scope != get_primary_ifscope(AF_INET6) &&
-                           ro->ro_rt->generation_id != route_generation)
+                           ROUTE_UNUSABLE(ro))
                                scope = get_primary_ifscope(AF_INET6);
                }
 
                ifa = (struct ifaddr *)
                    ifa_foraddr6_scoped(&srcsock->sin6_addr, scope);
 
+               /*
+                * If we are forwarding and proxying prefix(es), see if the
+                * source address is one of ours and is a proxied address;
+                * if so, use it.
+                */
+               if (ifa == NULL && ip6_forwarding && nd6_prproxy) {
+                       ifa = (struct ifaddr *)
+                           ifa_foraddr6(&srcsock->sin6_addr);
+                       if (ifa != NULL && !(proxied_ifa =
+                           nd6_prproxy_ifaddr((struct in6_ifaddr *)ifa))) {
+                               IFA_REMREF(ifa);
+                               ifa = NULL;
+                       }
+               }
+
                if (ip6_select_srcif_debug && ifa != NULL) {
                        if (ro->ro_rt != NULL) {
                                printf("%s->%s ifscope %d->%d ifa_if %s "
@@ -736,6 +815,15 @@ getsrcif:
         *      found interface.
         */
        if (ifa == NULL && ifscope == IFSCOPE_NONE) {
+               struct ifaddr *ifadst;
+
+               /* Check if the destination address is one of ours */
+               ifadst = (struct ifaddr *)ifa_foraddr6(&dstsock->sin6_addr);
+               if (ifadst != NULL) {
+                       local_dst = TRUE;
+                       IFA_REMREF(ifadst);
+               }
+
                ifa = (struct ifaddr *)ifa_foraddr6(&srcsock->sin6_addr);
 
                if (ip6_select_srcif_debug && ifa != NULL) {
@@ -746,7 +834,7 @@ getsrcif:
        }
 
 getroute:
-       if (ifa != NULL)
+       if (ifa != NULL && !proxied_ifa && !local_dst)
                ifscope = ifa->ifa_ifp->if_index;
 
        /*
@@ -771,40 +859,36 @@ getroute:
                ron = &opts->ip6po_nextroute;
                if (ron->ro_rt != NULL)
                        RT_LOCK(ron->ro_rt);
-               if ((ron->ro_rt != NULL &&
-                   ((ron->ro_rt->rt_flags & (RTF_UP | RTF_LLINFO)) !=
-                   (RTF_UP | RTF_LLINFO) ||
-                   ron->ro_rt->generation_id != route_generation ||
+               if (ROUTE_UNUSABLE(ron) || (ron->ro_rt != NULL &&
+                   (!(ron->ro_rt->rt_flags & RTF_LLINFO) ||
                    (select_srcif && (ifa == NULL ||
-                   ifa->ifa_ifp != ron->ro_rt->rt_ifp)))) ||
+                   (ifa->ifa_ifp != ron->ro_rt->rt_ifp && !proxied_ifa))))) ||
                    !IN6_ARE_ADDR_EQUAL(&satosin6(&ron->ro_dst)->sin6_addr,
                    &sin6_next->sin6_addr)) {
-                       if (ron->ro_rt != NULL) {
+                       if (ron->ro_rt != NULL)
                                RT_UNLOCK(ron->ro_rt);
-                               rtfree(ron->ro_rt);
-                               ron->ro_rt = NULL;
-                       }
+
+                       ROUTE_RELEASE(ron);
                        *satosin6(&ron->ro_dst) = *sin6_next;
                }
                if (ron->ro_rt == NULL) {
                        rtalloc_scoped((struct route *)ron, ifscope);
                        if (ron->ro_rt != NULL)
                                RT_LOCK(ron->ro_rt);
-                       if (ron->ro_rt == NULL ||
+                       if (ROUTE_UNUSABLE(ron) ||
                            !(ron->ro_rt->rt_flags & RTF_LLINFO) ||
                            !IN6_ARE_ADDR_EQUAL(&satosin6(rt_key(ron->ro_rt))->
                            sin6_addr, &sin6_next->sin6_addr)) {
-                               if (ron->ro_rt != NULL) {
+                               if (ron->ro_rt != NULL)
                                        RT_UNLOCK(ron->ro_rt);
-                                       rtfree(ron->ro_rt);
-                                       ron->ro_rt = NULL;
-                               }
+
+                               ROUTE_RELEASE(ron);
                                error = EHOSTUNREACH;
                                goto done;
                        }
                }
                route = ron;
-               ifp = ron->ro_rt->rt_ifp;
+               ifp = ifp0 = ron->ro_rt->rt_ifp;
 
                /*
                 * When cloning is required, try to allocate a route to the
@@ -830,16 +914,16 @@ getroute:
        if (ro == NULL)
                goto done;
        if (ro->ro_rt != NULL)
-               RT_LOCK(ro->ro_rt);
-       if (ro->ro_rt != NULL && (!(ro->ro_rt->rt_flags & RTF_UP) ||
-           satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
-           ro->ro_rt->generation_id != route_generation ||
+               RT_LOCK_SPIN(ro->ro_rt);
+       if (ROUTE_UNUSABLE(ro) || (ro->ro_rt != NULL &&
+           (satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
            !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst) ||
            (select_srcif && (ifa == NULL ||
-           ifa->ifa_ifp != ro->ro_rt->rt_ifp)))) {
-               RT_UNLOCK(ro->ro_rt);
-               rtfree(ro->ro_rt);
-               ro->ro_rt = NULL;
+           (ifa->ifa_ifp != ro->ro_rt->rt_ifp && !proxied_ifa)))))) {
+               if (ro->ro_rt != NULL)
+                       RT_UNLOCK(ro->ro_rt);
+
+               ROUTE_RELEASE(ro);
        }
        if (ro->ro_rt == NULL) {
                struct sockaddr_in6 *sa6;
@@ -847,10 +931,10 @@ getroute:
                if (ro->ro_rt != NULL)
                        RT_UNLOCK(ro->ro_rt);
                /* No route yet, so try to acquire one */
-               bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
+               bzero(&ro->ro_dst, sizeof (struct sockaddr_in6));
                sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
                sa6->sin6_family = AF_INET6;
-               sa6->sin6_len = sizeof(struct sockaddr_in6);
+               sa6->sin6_len = sizeof (struct sockaddr_in6);
                sa6->sin6_addr = *dst;
                if (IN6_IS_ADDR_MULTICAST(dst)) {
                        ro->ro_rt = rtalloc1_scoped(
@@ -859,7 +943,7 @@ getroute:
                        rtalloc_scoped((struct route *)ro, ifscope);
                }
                if (ro->ro_rt != NULL)
-                       RT_LOCK(ro->ro_rt);
+                       RT_LOCK_SPIN(ro->ro_rt);
        }
 
        /*
@@ -874,7 +958,7 @@ getroute:
 
        if (ro->ro_rt != NULL) {
                RT_LOCK_ASSERT_HELD(ro->ro_rt);
-               ifp = ro->ro_rt->rt_ifp;
+               ifp = ifp0 = ro->ro_rt->rt_ifp;
        } else {
                error = EHOSTUNREACH;
        }
@@ -883,6 +967,7 @@ getroute:
 validateroute:
        if (select_srcif) {
                boolean_t has_route = (route != NULL && route->ro_rt != NULL);
+               boolean_t srcif_selected = FALSE;
 
                if (has_route)
                        RT_LOCK_ASSERT_HELD(route->ro_rt);
@@ -895,30 +980,57 @@ validateroute:
                if (has_route && (ifa == NULL ||
                    (ifa->ifa_ifp != ifp && ifp != lo_ifp) ||
                    !(route->ro_rt->rt_flags & RTF_UP))) {
-                       if (ip6_select_srcif_debug) {
-                               if (ifa != NULL) {
-                                       printf("%s->%s ifscope %d ro_if %s "
-                                           "!= ifa_if %s (cached route "
-                                           "cleared)\n", s_src, s_dst,
-                                           ifscope, if_name(ifp),
-                                           if_name(ifa->ifa_ifp));
-                               } else {
-                                       printf("%s->%s ifscope %d ro_if %s "
-                                           "(no ifa_if found)\n", s_src,
-                                           s_dst, ifscope, if_name(ifp));
+                       /*
+                        * If the destination address belongs to a proxied
+                        * prefix, relax the requirement and allow the packet
+                        * to come out of the proxy interface with the source
+                        * address of the real interface.
+                        */
+                       if (ifa != NULL && proxied_ifa &&
+                           (route->ro_rt->rt_flags & (RTF_UP|RTF_PROXY)) ==
+                           (RTF_UP|RTF_PROXY)) {
+                               srcif_selected = TRUE;
+                       } else {
+                               if (ip6_select_srcif_debug) {
+                                       if (ifa != NULL) {
+                                               printf("%s->%s ifscope %d "
+                                                   "ro_if %s != ifa_if %s "
+                                                   "(cached route cleared)\n",
+                                                   s_src, s_dst,
+                                                   ifscope, if_name(ifp),
+                                                   if_name(ifa->ifa_ifp));
+                                       } else {
+                                               printf("%s->%s ifscope %d "
+                                                   "ro_if %s (no ifa_if "
+                                                   "found)\n", s_src, s_dst,
+                                                   ifscope, if_name(ifp));
+                                       }
                                }
+                               RT_UNLOCK(route->ro_rt);
+                               ROUTE_RELEASE(route);
+                               error = EHOSTUNREACH;
+                               /* Undo the settings done above */
+                               route = NULL;
+                               ifp = NULL;     /* ditch ifp; keep ifp0 */
+                               has_route = FALSE;
                        }
-                       RT_UNLOCK(route->ro_rt);
-                       rtfree(route->ro_rt);
-                       route->ro_rt = NULL;
-                       route->ro_flags &= ~ROF_SRCIF_SELECTED;
-                       error = EHOSTUNREACH;
-                       /* Undo the settings done above */
-                       route = NULL;
-                       ifp = NULL;
                } else if (has_route) {
-                       route->ro_flags |= ROF_SRCIF_SELECTED;
-                       route->ro_rt->generation_id = route_generation;
+                       srcif_selected = TRUE;
+               }
+
+               if (srcif_selected) {
+                       VERIFY(has_route);
+                       if (ifa != route->ro_srcia ||
+                           !(route->ro_flags & ROF_SRCIF_SELECTED)) {
+                               RT_CONVERT_LOCK(route->ro_rt);
+                               if (ifa != NULL)
+                                       IFA_ADDREF(ifa); /* for route_in6 */
+                               if (route->ro_srcia != NULL)
+                                       IFA_REMREF(route->ro_srcia);
+                               route->ro_srcia = ifa;
+                               route->ro_flags |= ROF_SRCIF_SELECTED;
+                               RT_GENID_SYNC(route->ro_rt);
+                       }
                        RT_UNLOCK(route->ro_rt);
                }
        } else {
@@ -943,19 +1055,37 @@ validateroute:
        }
 
 done:
-       if (nocell && error == 0) {
-               if ((ifp != NULL && ifp->if_type == IFT_CELLULAR) ||
-                   (route != NULL && route->ro_rt != NULL &&
-                   route->ro_rt->rt_ifp->if_type == IFT_CELLULAR)) {
-                       if (route != NULL && route->ro_rt != NULL) {
-                               rtfree(route->ro_rt);
-                               route->ro_rt = NULL;
-                               route->ro_flags &= ~ROF_SRCIF_SELECTED;
-                               route = NULL;
-                       }
-                       ifp = NULL;
-                       error = EHOSTUNREACH;
+       /*
+        * Check for interface restrictions.
+        */
+#define        CHECK_RESTRICTIONS(_ip6oa, _ifp)                        \
+       ((((_ip6oa)->ip6oa_flags & IP6OAF_NO_CELLULAR) &&       \
+           IFNET_IS_CELLULAR(_ifp)) ||                         \
+       (((_ip6oa)->ip6oa_flags & IP6OAF_NO_EXPENSIVE) &&       \
+           IFNET_IS_EXPENSIVE(_ifp)) ||                        \
+       (!((_ip6oa)->ip6oa_flags & IP6OAF_AWDL_UNRESTRICTED) && \
+           IFNET_IS_AWDL_RESTRICTED(_ifp))) 
+
+       if (error == 0 && ip6oa != NULL &&
+           ((ifp && CHECK_RESTRICTIONS(ip6oa, ifp)) ||
+           (route && route->ro_rt && 
+           CHECK_RESTRICTIONS(ip6oa, route->ro_rt->rt_ifp)))) {
+               if (route != NULL && route->ro_rt != NULL) {
+                       ROUTE_RELEASE(route);
+                       route = NULL;
                }
+               ifp = NULL;     /* ditch ifp; keep ifp0 */
+               error = EHOSTUNREACH;
+               ip6oa->ip6oa_retflags |= IP6OARF_IFDENIED;
+       }
+#undef CHECK_RESTRICTIONS
+
+       /*
+        * If the interface is disabled for IPv6, then ENETDOWN error.
+        */
+       if (error == 0 &&
+           ifp != NULL && (ifp->if_eflags & IFEF_IPV6_DISABLED)) {
+               error = ENETDOWN;
        }
 
        if (ifp == NULL && (route == NULL || route->ro_rt == NULL)) {
@@ -965,15 +1095,28 @@ done:
                 */
                error = EHOSTUNREACH;
        }
-       if (error == EHOSTUNREACH)
+       if (error == EHOSTUNREACH || error == ENETDOWN)
                ip6stat.ip6s_noroute++;
 
+       /*
+        * We'll return ifp regardless of error, so pick it up from ifp0
+        * in case it was nullified above.  Caller is responsible for
+        * releasing the ifp if it is non-NULL.
+        */
+       ifp = ifp0;
+       if (retifp != NULL) {
+               if (ifp != NULL)
+                       ifnet_reference(ifp);   /* for caller */
+               *retifp = ifp;
+       }
+
+       if (retsrcia != NULL) {
+               if (ifa != NULL)
+                       IFA_ADDREF(ifa);        /* for caller */
+               *retsrcia = (struct in6_ifaddr *)ifa;
+       }
+
        if (error == 0) {
-               if (retifp != NULL) {
-                       if (ifp != NULL)
-                               ifnet_reference(ifp);   /* for caller */
-                       *retifp = ifp;
-               }
                if (retrt != NULL && route != NULL)
                        *retrt = route->ro_rt;  /* ro_rt may be NULL */
        } else if (select_srcif && ip6_select_srcif_debug) {
@@ -989,26 +1132,28 @@ done:
        return (error);
 }
 
+/*
+ * Regardless of error, it will return an ifp with a reference held if the
+ * caller provides a non-NULL retifp.  The caller is responsible for checking
+ * if the returned ifp is valid and release its reference at all times.
+ */
 static int
 in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
-    struct ip6_moptions *mopts, struct route_in6 *ro, unsigned int ifscope,
-    unsigned int nocell, struct ifnet **retifp)
+    struct ip6_moptions *mopts, struct route_in6 *ro,
+    struct ip6_out_args *ip6oa, struct ifnet **retifp)
 {
-       int error;
+       int err = 0;
        struct route_in6 sro;
        struct rtentry *rt = NULL;
 
        if (ro == NULL) {
-               bzero(&sro, sizeof(sro));
+               bzero(&sro, sizeof (sro));
                ro = &sro;
        }
 
-       if ((error = selectroute(NULL, dstsock, opts, mopts, ro, retifp,
-           &rt, 0, 1, ifscope, nocell)) != 0) {
-               if (ro == &sro && rt && rt == sro.ro_rt)
-                       rtfree(rt);
-               return (error);
-       }
+       if ((err = selectroute(NULL, dstsock, opts, mopts, NULL, ro, retifp,
+           &rt, 0, 1, ip6oa)) != 0)
+               goto done;
 
        /*
         * do not use a rejected or black hole route.
@@ -1028,11 +1173,8 @@ in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
         * We thus reject the case here.
         */
        if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) {
-               int flags = (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
-
-               if (ro == &sro && rt && rt == sro.ro_rt)
-                       rtfree(rt);
-               return (flags);
+               err = ((rt->rt_flags & RTF_HOST) ? EHOSTUNREACH : ENETUNREACH);
+               goto done;
        }
 
        /*
@@ -1042,30 +1184,43 @@ in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
         * destination address (which should probably be one of our own
         * addresses.)
         */
-       if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp) {
+       if (rt != NULL && rt->rt_ifa != NULL && rt->rt_ifa->ifa_ifp != NULL &&
+           retifp != NULL) {
+               ifnet_reference(rt->rt_ifa->ifa_ifp);
                if (*retifp != NULL)
                        ifnet_release(*retifp);
                *retifp = rt->rt_ifa->ifa_ifp;
-               ifnet_reference(*retifp);
        }
 
-       if (ro == &sro && rt && rt == sro.ro_rt)
-               rtfree(rt);
-       return (0);
+done:
+       if (ro == &sro) {
+               VERIFY(rt == NULL || rt == ro->ro_rt);
+               ROUTE_RELEASE(ro);
+       }
+
+       /*
+        * retifp might point to a valid ifp with a reference held;
+        * caller is responsible for releasing it if non-NULL.
+        */
+       return (err);
 }
 
 /*
+ * Regardless of error, it will return an ifp with a reference held if the
+ * caller provides a non-NULL retifp.  The caller is responsible for checking
+ * if the returned ifp is valid and release its reference at all times.
+ *
  * clone - meaningful only for bsdi and freebsd
  */
 int
 in6_selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
-    struct ip6_pktopts *opts, struct ip6_moptions *mopts, struct route_in6 *ro,
-    struct ifnet **retifp, struct rtentry **retrt, int clone,
-    unsigned int ifscope, unsigned int nocell)
+    struct ip6_pktopts *opts, struct ip6_moptions *mopts,
+    struct in6_ifaddr **retsrcia, struct route_in6 *ro, struct ifnet **retifp,
+    struct rtentry **retrt, int clone, struct ip6_out_args *ip6oa)
 {
 
-       return (selectroute(srcsock, dstsock, opts, mopts, ro, retifp,
-           retrt, clone, 0, ifscope, nocell));
+       return (selectroute(srcsock, dstsock, opts, mopts, retsrcia, ro, retifp,
+           retrt, clone, 0, ip6oa));
 }
 
 /*
@@ -1074,23 +1229,29 @@ in6_selectroute(struct sockaddr_in6 *srcsock, struct sockaddr_in6 *dstsock,
  * 2. (If the outgoing interface is detected) the current
  *     hop limit of the interface specified by router advertisement.
  * 3. The system default hoplimit.
-*/
+ */
 int
-in6_selecthlim(
-       struct in6pcb *in6p,
-       struct ifnet *ifp)
+in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
 {
        if (in6p && in6p->in6p_hops >= 0) {
-               return(in6p->in6p_hops);
+               return (in6p->in6p_hops);
        } else {
                lck_rw_lock_shared(nd_if_rwlock);
                if (ifp && ifp->if_index < nd_ifinfo_indexlim) {
-                       u_int8_t chlim = nd_ifinfo[ifp->if_index].chlim;
+                       u_int8_t chlim;
+                       struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
+
+                       if (ndi->initialized) {
+                               /* access chlim without lock, for performance */
+                               chlim = ndi->chlim;
+                       } else {
+                               chlim = ip6_defhlim;
+                       }
                        lck_rw_done(nd_if_rwlock);
                        return (chlim);
                } else {
                        lck_rw_done(nd_if_rwlock);
-                       return(ip6_defhlim);
+                       return (ip6_defhlim);
                }
        }
 }
@@ -1100,51 +1261,62 @@ in6_selecthlim(
  * share this function by all *bsd*...
  */
 int
-in6_pcbsetport(
-       __unused struct in6_addr *laddr,
-       struct inpcb *inp,
-       struct proc *p,
-       int locked)
+in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct proc *p,
+    int locked)
 {
+#pragma unused(laddr)
        struct socket *so = inp->inp_socket;
        u_int16_t lport = 0, first, last, *lastport;
        int count, error = 0, wild = 0;
        struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
        kauth_cred_t cred;
        if (!locked) { /* Make sure we don't run into a deadlock: 4052373 */
-               if (!lck_rw_try_lock_exclusive(pcbinfo->mtx)) {
+               if (!lck_rw_try_lock_exclusive(pcbinfo->ipi_lock)) {
                        socket_unlock(inp->inp_socket, 0);
-                       lck_rw_lock_exclusive(pcbinfo->mtx);
+                       lck_rw_lock_exclusive(pcbinfo->ipi_lock);
                        socket_lock(inp->inp_socket, 0);
                }
+
+               /*
+                * Check if a local port was assigned to the inp while
+                * this thread was waiting for the pcbinfo lock
+                */
+               if (inp->inp_lport != 0) {
+                       VERIFY(inp->inp_flags2 & INP2_INHASHLIST);
+                       lck_rw_done(pcbinfo->ipi_lock);
+
+                       /*
+                        * It is not an error if another thread allocated
+                        * a port
+                        */
+                       return (0);
+               }
        }
 
        /* XXX: this is redundant when called from in6_pcbbind */
        if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
                wild = INPLOOKUP_WILDCARD;
 
-       inp->inp_flags |= INP_ANONPORT;
-
        if (inp->inp_flags & INP_HIGHPORT) {
                first = ipport_hifirstauto;     /* sysctl */
                last  = ipport_hilastauto;
-               lastport = &pcbinfo->lasthi;
+               lastport = &pcbinfo->ipi_lasthi;
        } else if (inp->inp_flags & INP_LOWPORT) {
                cred = kauth_cred_proc_ref(p);
                error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
                kauth_cred_unref(&cred);
                if (error != 0) {
                        if (!locked)
-                               lck_rw_done(pcbinfo->mtx);
-                       return error;
+                               lck_rw_done(pcbinfo->ipi_lock);
+                       return (error);
                }
                first = ipport_lowfirstauto;    /* 1023 */
                last  = ipport_lowlastauto;     /* 600 */
-               lastport = &pcbinfo->lastlow;
+               lastport = &pcbinfo->ipi_lastlow;
        } else {
                first = ipport_firstauto;       /* sysctl */
                last  = ipport_lastauto;
-               lastport = &pcbinfo->lastport;
+               lastport = &pcbinfo->ipi_lastport;
        }
        /*
         * Simple check to ensure all ports are not used up causing
@@ -1166,21 +1338,19 @@ in6_pcbsetport(
                                 * occurred above.
                                 */
                                inp->in6p_laddr = in6addr_any;
-                               inp->in6p_last_outif = 0;
+                               inp->in6p_last_outifp = NULL;
                                if (!locked)
-                                       lck_rw_done(pcbinfo->mtx);
+                                       lck_rw_done(pcbinfo->ipi_lock);
                                return (EAGAIN);
                        }
                        --*lastport;
                        if (*lastport > first || *lastport < last)
                                *lastport = first;
                        lport = htons(*lastport);
-               } while (in6_pcblookup_local(pcbinfo,
-                                            &inp->in6p_laddr, lport, wild));
+               } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr, lport,
+                   wild));
        } else {
-               /*
-                        * counting up
-                        */
+               /* counting up */
                count = last - first;
 
                do {
@@ -1190,43 +1360,47 @@ in6_pcbsetport(
                                 * occurred above.
                                 */
                                inp->in6p_laddr = in6addr_any;
-                               inp->in6p_last_outif = 0;
+                               inp->in6p_last_outifp = NULL;
                                if (!locked)
-                                       lck_rw_done(pcbinfo->mtx);
+                                       lck_rw_done(pcbinfo->ipi_lock);
                                return (EAGAIN);
                        }
                        ++*lastport;
                        if (*lastport < first || *lastport > last)
                                *lastport = first;
                        lport = htons(*lastport);
-               } while (in6_pcblookup_local(pcbinfo,
-                                            &inp->in6p_laddr, lport, wild));
+               } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr, lport,
+                   wild));
        }
 
        inp->inp_lport = lport;
+       inp->inp_flags |= INP_ANONPORT;
+
        if (in_pcbinshash(inp, 1) != 0) {
                inp->in6p_laddr = in6addr_any;
+               inp->in6p_last_outifp = NULL;
+
                inp->inp_lport = 0;
-               inp->in6p_last_outif = 0;
+               inp->inp_flags &= ~INP_ANONPORT;
                if (!locked)
-                       lck_rw_done(pcbinfo->mtx);
+                       lck_rw_done(pcbinfo->ipi_lock);
                return (EAGAIN);
        }
 
        if (!locked)
-               lck_rw_done(pcbinfo->mtx);
-       return(0);
+               lck_rw_done(pcbinfo->ipi_lock);
+       return (0);
 }
 
 /*
- * The followings are implementation of the policy table using a
- * simple tail queue.
- * XXX such details should be hidden.
- * XXX implementation using binary tree should be more efficient.
- * */
+ * The followings are implementation of the policy table using a
+ * simple tail queue.
+ * XXX such details should be hidden.
+ * XXX implementation using binary tree should be more efficient.
+ */
 struct addrsel_policyent {
-        TAILQ_ENTRY(addrsel_policyent) ape_entry;
-        struct in6_addrpolicy ape_policy;
+       TAILQ_ENTRY(addrsel_policyent) ape_entry;
+       struct in6_addrpolicy ape_policy;
 };
 
 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
@@ -1236,109 +1410,169 @@ struct addrsel_policyhead addrsel_policytab;
 static void
 init_policy_queue(void)
 {
-
-        TAILQ_INIT(&addrsel_policytab);
+       TAILQ_INIT(&addrsel_policytab);
 }
 
 void
 addrsel_policy_init(void)
 {
        /*
-        * Default address selection policy based on RFC 3484 and
-        * draft-arifumi-6man-rfc3484-revise-03.
+        * Default address selection policy based on RFC 6724.
         */
        static const struct in6_addrpolicy defaddrsel[] = {
-               /* localhost */
-               { .addr     = { .sin6_family = AF_INET6,
+               /* Loopback -- prefix=::1/128, precedence=50, label=0 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr   = IN6ADDR_LOOPBACK_INIT,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK128,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 60,
-                 .label    = 0 },
-               /* ULA */
-               { .addr     = { .sin6_family = AF_INET6,
-                               .sin6_addr   = {{{ 0xfc }}},
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK7,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 50,
-                 .label    = 1 },
-               /* any IPv6 src */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK128,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 50,
+                       .label    = 0
+               },
+
+               /* Unspecified -- prefix=::/0, precedence=40, label=1 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr   = IN6ADDR_ANY_INIT,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK0,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 40,
-                 .label    = 2 },
-               /* any IPv4 src */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK0,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 40,
+                       .label    = 1
+               },
+
+               /* IPv4 Mapped -- prefix=::ffff:0:0/96, precedence=35, label=4 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr   = IN6ADDR_V4MAPPED_INIT,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK96,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 30,
-                 .label    = 3 },
-               /* 6to4 */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK96,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 35,
+                       .label    = 4
+               },
+
+               /* 6to4 -- prefix=2002::/16, precedence=30, label=2 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr   = {{{ 0x20, 0x02 }}},
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK16,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 20,
-                 .label    = 4 },
-               /* Teredo */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK16,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 30,
+                       .label    = 2
+               },
+
+               /* Teredo -- prefix=2001::/32, precedence=5, label=5 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr   = {{{ 0x20, 0x01 }}},
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr   = IN6MASK32,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 10,
-                 .label    = 5 },
-               /* v4 compat addresses */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK32,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 5,
+                       .label    = 5
+               },
+
+               /* Unique Local (ULA) -- prefix=fc00::/7, precedence=3, label=13 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = {{{ 0xfc }}},
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr   = IN6MASK7,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 3,
+                       .label    = 13
+               },
+
+               /* IPv4 Compatible -- prefix=::/96, precedence=1, label=3 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr = IN6ADDR_ANY_INIT,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr = IN6MASK96,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 1,
-                 .label    = 10 },
-               /* site-local (deprecated) */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr = IN6MASK96,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 1,
+                       .label    = 3
+               },
+
+               /* Site-local (deprecated) -- prefix=fec0::/10, precedence=1, label=11 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr = {{{ 0xfe, 0xc0 }}},
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr = IN6MASK16,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 1,
-                 .label    = 11 },
-               /* 6bone (deprecated) */
-               { .addr     = { .sin6_family = AF_INET6,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr = IN6MASK16,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 1,
+                       .label    = 11
+               },
+
+               /* 6bone (deprecated) -- prefix=3ffe::/16, precedence=1, label=12 */
+               {
+                       .addr = {
+                               .sin6_family = AF_INET6,
                                .sin6_addr = {{{ 0x3f, 0xfe }}},
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .addrmask = { .sin6_family = AF_INET6,
-                               .sin6_addr = IN6MASK16,
-                               .sin6_len    = sizeof(struct sockaddr_in6) },
-                 .preced   = 1,
-                 .label    = 12 },
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .addrmask = {
+                               .sin6_family = AF_INET6,
+                               .sin6_addr = IN6MASK16,
+                               .sin6_len    = sizeof (struct sockaddr_in6)
+                       },
+                       .preced   = 1,
+                       .label    = 12
+               },
        };
        int i;
 
        init_policy_queue();
 
        /* initialize the "last resort" policy */
-       bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
+       bzero(&defaultaddrpolicy, sizeof (defaultaddrpolicy));
        defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
 
-       for (i = 0; i < sizeof(defaddrsel) / sizeof(defaddrsel[0]); i++)
+       for (i = 0; i < sizeof (defaddrsel) / sizeof (defaddrsel[0]); i++)
                add_addrsel_policyent(&defaddrsel[i]);
 
 }
@@ -1397,36 +1631,36 @@ match_addrsel_policy(struct sockaddr_in6 *key)
                        bestmatchlen = matchlen;
                }
 
-         next:
+       next:
                continue;
        }
 
        return (bestpol);
-} 
+}
 
 static int
 add_addrsel_policyent(const struct in6_addrpolicy *newpolicy)
 {
        struct addrsel_policyent *new, *pol;
 
-       MALLOC(new, struct addrsel_policyent *, sizeof(*new), M_IFADDR,
-              M_WAITOK);
+       MALLOC(new, struct addrsel_policyent *, sizeof (*new), M_IFADDR,
+           M_WAITOK);
 
        ADDRSEL_LOCK();
 
        /* duplication check */
        TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
                if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
-                                      &pol->ape_policy.addr.sin6_addr) &&
+                   &pol->ape_policy.addr.sin6_addr) &&
                    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
-                                      &pol->ape_policy.addrmask.sin6_addr)) {
+                   &pol->ape_policy.addrmask.sin6_addr)) {
                        ADDRSEL_UNLOCK();
                        FREE(new, M_IFADDR);
                        return (EEXIST);        /* or override it? */
                }
        }
 
-       bzero(new, sizeof(*new));
+       bzero(new, sizeof (*new));
 
        /* XXX: should validate entry */
        new->ape_policy = *newpolicy;
@@ -1499,20 +1733,20 @@ dump_addrsel_policyent(const struct in6_addrpolicy *pol, void *arg)
        int error = 0;
        struct walkarg *w = arg;
 
-       error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
+       error = SYSCTL_OUT(w->w_req, pol, sizeof (*pol));
 
        return (error);
 }
 
 static int
-in6_src_sysctl SYSCTL_HANDLER_ARGS 
+in6_src_sysctl SYSCTL_HANDLER_ARGS
 {
 #pragma unused(oidp, arg1, arg2)
 struct walkarg w;
 
        if (req->newptr)
-               return EPERM;
-       bzero(&w, sizeof(w));
+               return (EPERM);
+       bzero(&w, sizeof (w));
        w.w_req = req;
 
        return (walk_addrsel_policy(dump_addrsel_policyent, &w));
@@ -1530,7 +1764,7 @@ in6_src_ioctl(u_long cmd, caddr_t data)
        if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
                return (EOPNOTSUPP); /* check for safety */
 
-       ent0 = *(struct in6_addrpolicy *)data;
+       bcopy(data, &ent0, sizeof (ent0));
 
        if (ent0.label == ADDR_LABEL_NOTAPP)
                return (EINVAL);
@@ -1578,12 +1812,8 @@ in6_src_ioctl(u_long cmd, caddr_t data)
  * we may want to change the function to return something other than ifp.
  */
 int
-in6_embedscope(
-       struct in6_addr *in6,
-       const struct sockaddr_in6 *sin6,
-       struct in6pcb *in6p,
-       struct ifnet **ifpp,
-       struct ip6_pktopts *opt)
+in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
+    struct in6pcb *in6p, struct ifnet **ifpp, struct ip6_pktopts *opt)
 {
        struct ifnet *ifp = NULL;
        u_int32_t scopeid;
@@ -1604,7 +1834,7 @@ in6_embedscope(
                scopeid = scope6_addr2default(in6);
 #endif
 
-       if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
+       if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
                struct in6_pktinfo *pi;
                struct ifnet *im6o_multicast_ifp = NULL;
 
@@ -1616,37 +1846,45 @@ in6_embedscope(
                        IM6O_UNLOCK(in6p->in6p_moptions);
                }
 
-               if (opt)
+               if (opt != NULL)
                        optp = opt;
-               else if (in6p)
+               else if (in6p != NULL)
                        optp = in6p->in6p_outputopts;
                /*
                 * KAME assumption: link id == interface id
                 */
-               ifnet_head_lock_shared();
-               if (in6p && optp && (pi = optp->ip6po_pktinfo) &&
-                   pi->ipi6_ifindex) {
-                       ifp = ifindex2ifnet[pi->ipi6_ifindex];
+               if (in6p != NULL && optp != NULL &&
+                   (pi = optp->ip6po_pktinfo) != NULL &&
+                   pi->ipi6_ifindex != 0) {
+                       /* ifp is needed here if only we're returning it */
+                       if (ifpp != NULL) {
+                               ifnet_head_lock_shared();
+                               ifp = ifindex2ifnet[pi->ipi6_ifindex];
+                               ifnet_head_done();
+                       }
                        in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
-               } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
+               } else if (in6p != NULL && IN6_IS_ADDR_MULTICAST(in6) &&
                    in6p->in6p_moptions != NULL && im6o_multicast_ifp != NULL) {
                        ifp = im6o_multicast_ifp;
                        in6->s6_addr16[1] = htons(ifp->if_index);
-               } else if (scopeid) {
-                       /* 
+               } else if (scopeid != 0) {
+                       /*
                         * Since scopeid is unsigned, we only have to check it
-                        * against if_index
+                        * against if_index (ifnet_head_lock not needed since
+                        * if_index is an ever-increasing integer.)
                         */
-                       if (if_index < scopeid) {
-                               ifnet_head_done();
-                               return ENXIO;  /* XXX EINVAL? */
+                       if (if_index < scopeid)
+                               return (ENXIO);  /* XXX EINVAL? */
 
+                       /* ifp is needed here only if we're returning it */
+                       if (ifpp != NULL) {
+                               ifnet_head_lock_shared();
+                               ifp = ifindex2ifnet[scopeid];
+                               ifnet_head_done();
                        }
-                       ifp = ifindex2ifnet[scopeid];
-                       /*XXX assignment to 16bit from 32bit variable */
+                       /* XXX assignment to 16bit from 32bit variable */
                        in6->s6_addr16[1] = htons(scopeid & 0xffff);
                }
-               ifnet_head_done();
 
                if (ifpp != NULL) {
                        if (ifp != NULL)
@@ -1655,7 +1893,7 @@ in6_embedscope(
                }
        }
 
-       return 0;
+       return (0);
 }
 
 /*
@@ -1681,26 +1919,26 @@ in6_recoverscope(
         */
 
        sin6->sin6_scope_id = 0;
-       if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
+       if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
                /*
                 * KAME assumption: link id == interface id
                 */
                scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
                if (scopeid) {
-                       /* 
-                        * sanity check 
+                       /*
+                        * sanity check
                         *
                         * Since scopeid is unsigned, we only have to check it
                         * against if_index
                         */
                        if (if_index < scopeid)
-                               return ENXIO;
+                               return (ENXIO);
                        if (ifp && ifp->if_index != scopeid)
-                               return ENXIO;
+                               return (ENXIO);
                        sin6->sin6_addr.s6_addr16[1] = 0;
                        sin6->sin6_scope_id = scopeid;
                }
        }
 
-       return 0;
+       return (0);
 }