zebra: return route_node from rib_match_ipv4_safi

The multicast code needs to know the route_node in addition to the rib
entry in order to perform distance or prefix-length comparisons.  Add it
as optional "out" pointer parameter.

Cc: Everton Marques <everton.marques@gmail.com>
Cc: Balaji G <balajig81@gmail.com>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/zebra/rib.h b/zebra/rib.h
index 5eedfde..347fadb 100644
--- a/zebra/rib.h
+++ b/zebra/rib.h
@@ -418,7 +418,8 @@
 		            struct in_addr *gate, unsigned int ifindex, 
 		            u_int32_t, safi_t safi);
 
-extern struct rib *rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp);
+extern struct rib *rib_match_ipv4_safi (struct in_addr addr, safi_t safi,
+					int skip_bgp, struct route_node **rn_out);
 
 extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
 
diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c
index b4ea242..abef90f 100644
--- a/zebra/zebra_rib.c
+++ b/zebra/zebra_rib.c
@@ -719,7 +719,8 @@
 #endif /* HAVE_IPV6 */
 
 struct rib *
-rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp)
+rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp,
+		     struct route_node **rn_out)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -759,16 +760,22 @@
 	}
       else
 	{
-	  if (match->type == ZEBRA_ROUTE_CONNECT)
-	    /* Directly point connected route. */
-	    return match;
-	  else
+	  if (match->type != ZEBRA_ROUTE_CONNECT)
 	    {
+	      int found = 0;
 	      for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
 		if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
-		  return match;
-	      return NULL;
+		  {
+		    found = 1;
+		    break;
+		  }
+	      if (!found)
+		return NULL;
 	    }
+
+	  if (rn_out)
+	    *rn_out = rn;
+	  return match;
 	}
     }
   return NULL;
diff --git a/zebra/zserv.c b/zebra/zserv.c
index e9236de..e678f3a 100644
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -540,7 +540,7 @@
   struct nexthop *nexthop;
 
   /* Lookup nexthop - eBGP excluded */
-  rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1);
+  rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL);
 
   /* Get output stream. */
   s = client->obuf;
@@ -615,7 +615,7 @@
   int skip_bgp = 0; /* bool */
 
   /* Lookup nexthop. */
-  rib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp);
+  rib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, NULL);
 
   if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
     zlog_debug("%s: %s mrib entry found.", __func__, rib ? "Matching" : "No matching");