-#if IFNET_ROUTE_REFCNT
- /*
- * Always discard the cached route for unconnected socket
- * or if it is a multicast route.
- */
- if (in6p->in6p_route.ro_rt != NULL &&
- ((in6p->in6p_route.ro_rt->rt_flags & RTF_MULTICAST) ||
- in6p->in6p_socket == NULL ||
- in6p->in6p_socket->so_state != SS_ISCONNECTED)) {
- rtfree(in6p->in6p_route.ro_rt);
- in6p->in6p_route.ro_rt = NULL;
+ if (in6p->in6p_route.ro_rt != NULL) {
+ struct rtentry *rt = in6p->in6p_route.ro_rt;
+ unsigned int outif;
+
+ if ((rt->rt_flags & RTF_MULTICAST) ||
+ in6p->in6p_socket == NULL ||
+ !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
+ rt = NULL; /* unusable */
+ }
+ /*
+ * Always discard the cached route for unconnected
+ * socket or if it is a multicast route.
+ */
+ if (rt == NULL) {
+ rtfree(in6p->in6p_route.ro_rt);
+ in6p->in6p_route.ro_rt = NULL;
+ }
+ /*
+ * If this is a connected socket and the destination
+ * route is not multicast, update outif with that of
+ * the route interface index used by IP.
+ */
+ if (rt != NULL && (outif = rt->rt_ifp->if_index) !=
+ in6p->in6p_last_outif)
+ in6p->in6p_last_outif = outif;