-#if IFNET_ROUTE_REFCNT
- /*
- * Always discard the cached route for unconnected socket
- * or if it is a non-unicast route.
- */
- if (inp->inp_route.ro_rt != NULL &&
- ((inp->inp_route.ro_rt->rt_flags & (RTF_MULTICAST|RTF_BROADCAST)) ||
- inp->inp_socket == NULL ||
- inp->inp_socket->so_state != SS_ISCONNECTED)) {
- rtfree(inp->inp_route.ro_rt);
- inp->inp_route.ro_rt = NULL;
+ if (imo != NULL)
+ IMO_REMREF(imo);
+
+ if (inp->inp_route.ro_rt != NULL) {
+ struct rtentry *rt = inp->inp_route.ro_rt;
+ unsigned int outif;
+
+ if ((rt->rt_flags & (RTF_MULTICAST|RTF_BROADCAST)) ||
+ inp->inp_socket == NULL ||
+ !(inp->inp_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(inp->inp_route.ro_rt);
+ inp->inp_route.ro_rt = NULL;
+ }
+ /*
+ * If this is a connected socket and the destination
+ * route is unicast, update outif with that of the route
+ * interface index used by IP.
+ */
+ if (rt != NULL &&
+ (outif = rt->rt_ifp->if_index) != inp->inp_last_outif)
+ inp->inp_last_outif = outif;