+
+ if (ifapp != NULL)
+ *ifapp = &ia->ia_ifa;
+ else
+ IFA_REMREF(&ia->ia_ifa);
+
+done:
+ if (srcsel_debug) {
+ (void) inet_ntop(AF_INET6, &dst, s_dst, sizeof (s_src));
+
+ tmp = (src_storage != NULL) ? src_storage : &in6addr_any;
+ (void) inet_ntop(AF_INET6, tmp, s_src, sizeof (s_src));
+
+ printf("%s out src %s dst %s dst_scope %d best_scope %d\n",
+ __func__, s_src, s_dst, dst_scope, best_scope);
+ }
+
+ return (src_storage);
+}
+
+/*
+ * 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,
+ struct ifnet **ifpp, struct in6_addr *src_storage, unsigned int ifscope,
+ int *errorp)
+{
+ struct ifnet *ifp = NULL;
+ struct in6_pktinfo *pi = NULL;
+ struct ip6_moptions *mopts;
+ struct ip6_out_args ip6oa = { ifscope, { 0 }, IP6OAF_SELECT_SRCIF, 0,
+ SO_TC_UNSPEC, _NET_SERVICE_TYPE_UNSPEC };
+ boolean_t inp_debug = FALSE;
+ uint32_t hint_mask = 0;
+ int prefer_tempaddr = 0;
+ struct ifnet *sifp = NULL;
+
+ *errorp = 0;
+ if (ifpp != NULL)
+ *ifpp = NULL;
+
+ if (inp != NULL) {
+ inp_debug = SASEL_DO_DBG(inp);
+ mopts = inp->in6p_moptions;
+ 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;
+ if (INP_INTCOPROC_ALLOWED(inp))
+ ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
+ } else {
+ mopts = NULL;
+ /* Allow the kernel to retransmit packets. */
+ ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED |
+ IP6OAF_AWDL_UNRESTRICTED;
+ }
+
+ 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
+ * assigned to the node, and can be used as the packet's source
+ * address. If everything is okay, use the address as source.
+ */
+ if (opts && (pi = opts->ip6po_pktinfo) &&
+ !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
+ struct sockaddr_in6 srcsock;
+ struct in6_ifaddr *ia6;
+
+ /* get the outgoing interface */
+ if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa,
+ &ifp)) != 0) {
+ src_storage = NULL;
+ goto done;
+ }
+
+ /*
+ * determine the appropriate zone id of the source based on
+ * the zone of the destination and the outgoing interface.
+ * If the specified address is ambiguous wrt the scope zone,
+ * the interface must be specified; otherwise, ifa_ifwithaddr()
+ * will fail matching the address.
+ */
+ bzero(&srcsock, sizeof (srcsock));
+ srcsock.sin6_family = AF_INET6;
+ srcsock.sin6_len = sizeof (srcsock);
+ srcsock.sin6_addr = pi->ipi6_addr;
+ if (ifp != NULL) {
+ *errorp = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
+ if (*errorp != 0) {
+ src_storage = NULL;
+ goto done;
+ }
+ }
+ ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)
+ (&srcsock));
+ if (ia6 == NULL) {
+ *errorp = EADDRNOTAVAIL;
+ src_storage = NULL;
+ goto done;
+ }
+ IFA_LOCK_SPIN(&ia6->ia_ifa);
+ if ((ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) ||
+ (inp && inp_restricted_send(inp, ia6->ia_ifa.ifa_ifp))) {
+ IFA_UNLOCK(&ia6->ia_ifa);
+ IFA_REMREF(&ia6->ia_ifa);
+ *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);
+ goto done;
+ }
+
+ /*
+ * Otherwise, if the socket has already bound the source, just use it.
+ */
+ 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
+ * the outgoing interface and the destination address.
+ */
+ /* get the outgoing interface */
+ if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa,
+ &ifp)) != 0) {
+ src_storage = NULL;
+ goto done;
+ }
+
+ VERIFY(ifp != NULL);
+
+ if (opts == NULL ||
+ opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
+ prefer_tempaddr = ip6_prefer_tempaddr;
+ } else if (opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_NOTPREFER) {
+ prefer_tempaddr = 0;
+ } else
+ prefer_tempaddr = 1;
+
+ if (prefer_tempaddr)
+ hint_mask |= IPV6_SRCSEL_HINT_PREFER_TMPADDR;
+
+ if (in6_selectsrc_core(dstsock, hint_mask, ifp, inp_debug, src_storage,
+ &sifp, errorp, NULL) == NULL) {
+ src_storage = NULL;
+ goto done;
+ }
+
+ VERIFY(sifp != NULL);
+
+ if (inp && inp_restricted_send(inp, sifp)) {
+ src_storage = NULL;
+ *errorp = EHOSTUNREACH;
+ ifnet_release(sifp);
+ goto done;
+ } else {
+ ifnet_release(sifp);
+ }
+