+ if (ifa != NULL && ro->ro_rt == NULL) {
+ struct rtentry *rt;
+ struct sockaddr_in sin;
+ struct ifaddr *oifa = NULL;
+
+ bzero(&sin, sizeof (sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof (sin);
+ sin.sin_addr = dst;
+
+ lck_mtx_lock(rnh_lock);
+ if ((rt = rt_lookup(TRUE, (struct sockaddr *)&sin, NULL,
+ rt_tables[AF_INET], IFSCOPE_NONE)) != NULL) {
+ RT_LOCK(rt);
+ /*
+ * If the route uses a different interface,
+ * use that one instead. The IP address of
+ * the ifaddr that we pick up here is not
+ * relevant.
+ */
+ if (ifa->ifa_ifp != rt->rt_ifp) {
+ oifa = ifa;
+ ifa = rt->rt_ifa;
+ ifaref(ifa);
+ RT_UNLOCK(rt);
+ } else {
+ RT_UNLOCK(rt);
+ }
+ rtfree_locked(rt);
+ }
+ lck_mtx_unlock(rnh_lock);
+
+ if (oifa != NULL) {
+ struct ifaddr *iifa;
+
+ /*
+ * See if the interface pointed to by the
+ * route is configured with the source IP
+ * address of the packet.
+ */
+ iifa = (struct ifaddr *)ifa_foraddr_scoped(
+ src.s_addr, ifa->ifa_ifp->if_index);
+
+ if (iifa != NULL) {
+ /*
+ * Found it; drop the original one
+ * as well as the route interface
+ * address, and use this instead.
+ */
+ ifafree(oifa);
+ ifafree(ifa);
+ ifa = iifa;
+ } else if (!ipforwarding ||
+ (rt->rt_flags & RTF_GATEWAY)) {
+ /*
+ * This interface doesn't have that
+ * source IP address; drop the route
+ * interface address and just use the
+ * original one, and let the caller
+ * do a scoped route lookup.
+ */
+ ifafree(ifa);
+ ifa = oifa;
+ } else {
+ /*
+ * Forwarding is enabled and the source
+ * address belongs to one of our own
+ * interfaces which isn't the outgoing
+ * interface, and we have a route, and
+ * the destination is on a network that
+ * is directly attached (onlink); drop
+ * the original one and use the route
+ * interface address instead.
+ */
+ ifafree(oifa);
+ }
+ }
+ } else if (ifa != NULL && ro->ro_rt != NULL &&
+ !(ro->ro_rt->rt_flags & RTF_GATEWAY) &&
+ ifa->ifa_ifp != ro->ro_rt->rt_ifp && ipforwarding) {
+ /*
+ * Forwarding is enabled and the source address belongs
+ * to one of our own interfaces which isn't the same
+ * as the interface used by the known route; drop the
+ * original one and use the route interface address.
+ */
+ ifafree(ifa);
+ ifa = ro->ro_rt->rt_ifa;
+ ifaref(ifa);
+ }
+
+ if (ip_select_srcif_debug && ifa != NULL) {
+ printf("%s->%s ifscope %d ifa_if %s%d\n",
+ s_src, s_dst, ifscope, ifa->ifa_ifp->if_name,
+ ifa->ifa_ifp->if_unit);
+ }
+ }
+
+ if (ro->ro_rt != NULL)
+ RT_LOCK_ASSERT_HELD(ro->ro_rt);
+ /*
+ * If there is a non-loopback route with the wrong interface, or if
+ * there is no interface configured with such an address, blow it
+ * away. Except for local/loopback, we look for one with a matching
+ * interface scope/index.
+ */
+ if (ro->ro_rt != NULL &&
+ (ifa == NULL || (ifa->ifa_ifp != rt_ifp && rt_ifp != lo_ifp) ||
+ !(ro->ro_rt->rt_flags & RTF_UP))) {
+ if (ip_select_srcif_debug) {
+ if (ifa != NULL) {
+ printf("%s->%s ifscope %d ro_if %s%d != "
+ "ifa_if %s%d (cached route cleared)\n",
+ s_src, s_dst, ifscope, rt_ifp->if_name,
+ rt_ifp->if_unit, ifa->ifa_ifp->if_name,
+ ifa->ifa_ifp->if_unit);
+ } else {
+ printf("%s->%s ifscope %d ro_if %s%d "
+ "(no ifa_if found)\n",
+ s_src, s_dst, ifscope, rt_ifp->if_name,
+ rt_ifp->if_unit);
+ }
+ }
+
+ RT_UNLOCK(ro->ro_rt);
+ rtfree(ro->ro_rt);
+ ro->ro_rt = NULL;
+ ro->ro_flags &= ~ROF_SRCIF_SELECTED;
+
+ /*
+ * If the destination is IPv4 LLA and the route's interface
+ * doesn't match the source interface, then the source IP
+ * address is wrong; it most likely belongs to the primary
+ * interface associated with the IPv4 LL subnet. Drop the
+ * packet rather than letting it go out and return an error
+ * to the ULP. This actually applies not only to IPv4 LL
+ * but other shared subnets; for now we explicitly test only
+ * for the former case and save the latter for future.
+ */
+ if (IN_LINKLOCAL(ntohl(dst.s_addr)) &&
+ !IN_LINKLOCAL(ntohl(src.s_addr)) && ifa != NULL) {
+ ifafree(ifa);
+ ifa = NULL;