2004-05-05 Paul Jakma <paul@dishone.st>

        * ospf_packet.c:  (ospf_associate_packet_vl) cleanup, move
          some of the checks up to ospf_read, return either a
          virtual link oi, or NULL.
          (ospf_read) Cleanup, make it responsible for checks. Remove
          the nbr lookup - moved to ospf_neighbor. Adjust all nbr
          lookups to use new wrappers exported by ospf_neighbor.
        * ospf_neighbor.h: Add ospf_neigbour_get and ospf_nbr_lookup.
        * ospf_neighbor.c: (ospf_neigbour_get) Index ospf_interface
          neighbour table by router-id for virtual-link ospf_interfaces,
          not by peer_addr (which breaks for asymmetric vlinks)
          (ospf_nbr_lookup) add a wrapper for nbr lookups to deal with
          above.
diff --git a/ospfd/ChangeLog b/ospfd/ChangeLog
index 39a1887..6b15fe6 100644
--- a/ospfd/ChangeLog
+++ b/ospfd/ChangeLog
@@ -1,3 +1,18 @@
+2004-05-05 Paul Jakma <paul@dishone.st>
+
+	* ospf_packet.c:  (ospf_associate_packet_vl) cleanup, move 
+	  some of the checks up to ospf_read, return either a 
+	  virtual link oi, or NULL. 
+	  (ospf_read) Cleanup, make it responsible for checks. Remove
+	  the nbr lookup - moved to ospf_neighbor. Adjust all nbr
+	  lookups to use new wrappers exported by ospf_neighbor.
+	* ospf_neighbor.h: Add ospf_neigbour_get and ospf_nbr_lookup.
+	* ospf_neighbor.c: (ospf_neigbour_get) Index ospf_interface 
+	  neighbour table by router-id for virtual-link ospf_interfaces, 
+	  not by peer_addr (which breaks for asymmetric vlinks)
+	  (ospf_nbr_lookup) add a wrapper for nbr lookups to deal with
+	  above.
+
 2004-04-22 Hasso Tepper <hasso@estpak.ee>
 
 	* ospf_zebra.c: Don't ignore reject/bh routes, it's the only way
diff --git a/ospfd/ospf_neighbor.c b/ospfd/ospf_neighbor.c
index ccef055..6f2d4a0 100644
--- a/ospfd/ospf_neighbor.c
+++ b/ospfd/ospf_neighbor.c
@@ -138,6 +138,11 @@
   /* Unlink ospf neighbor from the interface. */
   p.family = AF_INET;
   p.prefixlen = IPV4_MAX_BITLEN;
+
+  /* vlinks are indexed by router-id */
+  if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+    p.u.prefix4 = nbr->router_id;
+  else
   p.u.prefix4 = nbr->src;
 
   rn = route_node_lookup (oi->nbrs, &p);
@@ -236,6 +241,10 @@
 }
 #endif /* HAVE_OPAQUE_LSA */
 
+/* lookup nbr by address - use this only if you know you must
+ * otherwise use the ospf_nbr_lookup() wrapper, which deals 
+ * with virtual link neighbours
+ */
 struct ospf_neighbor *
 ospf_nbr_lookup_by_addr (struct route_table *nbrs,
 			 struct in_addr *addr)
@@ -317,3 +326,99 @@
 
   return;
 }
+
+
+struct ospf_neighbor *
+ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
+                 struct ospf_header *ospfh)
+{
+  if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+    return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
+  else
+    return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
+}
+
+struct ospf_neighbor *
+ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
+              struct prefix *p)
+{
+  struct ospf_neighbor *nbr;
+  
+  nbr = ospf_nbr_new (oi);
+  nbr->state = NSM_Down;
+  nbr->src = p->u.prefix4;
+  memcpy (&nbr->address, p, sizeof (struct prefix));
+
+  nbr->nbr_nbma = NULL;
+  if (oi->type == OSPF_IFTYPE_NBMA)
+    {
+      struct ospf_nbr_nbma *nbr_nbma;
+      listnode node;
+
+      for (node = listhead (oi->nbr_nbma); node; nextnode (node))
+        {
+          nbr_nbma = getdata (node);
+          assert (nbr_nbma);
+
+          if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
+            {
+              nbr_nbma->nbr = nbr;
+              nbr->nbr_nbma = nbr_nbma;
+
+              if (nbr_nbma->t_poll)
+                OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
+
+              nbr->state_change = nbr_nbma->state_change + 1;
+            }
+        }
+    }
+      
+  /* New nbr, save the crypto sequence number if necessary */
+  if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
+    nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
+  
+  if (IS_DEBUG_OSPF_EVENT)
+    zlog_info ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
+               inet_ntoa (nbr->router_id));
+  
+  return nbr;
+}
+
+struct ospf_neighbor *
+ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
+              struct ip *iph, struct prefix *p)
+{
+  struct route_node *rn;
+  struct prefix key;
+  struct ospf_neighbor *nbr;
+  
+  key.family = AF_INET;
+  key.prefixlen = IPV4_MAX_BITLEN;
+
+  if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+    key.u.prefix4 = ospfh->router_id;   /* index vlink nbrs by router-id */
+  else
+    key.u.prefix4 = iph->ip_src;
+
+  rn = route_node_get (oi->nbrs, &key);
+  if (rn->info)
+    {
+      route_unlock_node (rn);
+      nbr = rn->info;
+      
+      if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
+        {
+          nbr->src = iph->ip_src;
+          memcpy (&nbr->address, p, sizeof (struct prefix));
+        }
+    }
+  else
+    {
+      rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
+    }
+  
+  nbr->router_id = ospfh->router_id;
+
+  return nbr;
+}
+  
diff --git a/ospfd/ospf_neighbor.h b/ospfd/ospf_neighbor.h
index f7b1874..f91ef3f 100644
--- a/ospfd/ospf_neighbor.h
+++ b/ospfd/ospf_neighbor.h
@@ -23,6 +23,8 @@
 #ifndef _ZEBRA_OSPF_NEIGHBOR_H
 #define _ZEBRA_OSPF_NEIGHBOR_H
 
+#include <ospfd/ospf_packet.h>
+
 /* Neighbor Data Structure */
 struct ospf_neighbor
 {
@@ -93,10 +95,16 @@
 void ospf_nbr_delete (struct ospf_neighbor *);
 int ospf_nbr_bidirectional (struct in_addr *, struct in_addr *, int);
 void ospf_nbr_add_self (struct ospf_interface *);
-int ospf_nbr_count (struct route_table *, int);
+int ospf_nbr_count (struct ospf_interface *, int);
 #ifdef HAVE_OPAQUE_LSA
-int ospf_opaque_capable_nbr_count (struct route_table *nbrs, int status);
+int ospf_nbr_count_opaque_capable (struct ospf_interface *);
 #endif /* HAVE_OPAQUE_LSA */
+struct ospf_neighbor *ospf_nbr_get (struct ospf_interface *,
+                                    struct ospf_header *,
+                                    struct ip *,
+                                    struct prefix *);
+struct ospf_neighbor *ospf_nbr_lookup (struct ospf_interface *, struct ip *,
+                                       struct ospf_header *);
 struct ospf_neighbor *ospf_nbr_lookup_by_addr (struct route_table *,
 					       struct in_addr *);
 struct ospf_neighbor *ospf_nbr_lookup_by_routerid (struct route_table *,
diff --git a/ospfd/ospf_packet.c b/ospfd/ospf_packet.c
index 1f8ecd4..e6b2ea7 100644
--- a/ospfd/ospf_packet.c
+++ b/ospfd/ospf_packet.c
@@ -607,9 +607,8 @@
 {
   struct ospf_hello *hello;
   struct ospf_neighbor *nbr;
-  struct route_node *rn;
-  struct prefix p, key;
   int old_state;
+  struct prefix p;
 
   /* increment statistics. */
   oi->hello_in++;
@@ -736,70 +735,12 @@
 		   OPTIONS (oi), hello->options);
 	return;
       }
-
-
-  /* Get neighbor information from table. */
-  key.family = AF_INET;
-  key.prefixlen = IPV4_MAX_BITLEN;
-  key.u.prefix4 = iph->ip_src;
-
-  rn = route_node_get (oi->nbrs, &key);
-  if (rn->info)
-    {
-      route_unlock_node (rn);
-      nbr = rn->info;
-
-      if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
-	{
-	  nbr->src = iph->ip_src;
-	  nbr->address = p;
-	}
-    }
-  else
-    {
-      /* Create new OSPF Neighbor structure. */
-      nbr = ospf_nbr_new (oi);
-      nbr->state = NSM_Down;
-      nbr->src = iph->ip_src;
-      nbr->address = p;
-
-      rn->info = nbr;
-
-      nbr->nbr_nbma = NULL;
-
-      if (oi->type == OSPF_IFTYPE_NBMA)
-	{
-	  struct ospf_nbr_nbma *nbr_nbma;
-	  listnode node;
-
-	  for (node = listhead (oi->nbr_nbma); node; nextnode (node))
-	    {
-	      nbr_nbma = getdata (node);
-	      assert (nbr_nbma);
-      
-	      if (IPV4_ADDR_SAME(&nbr_nbma->addr, &iph->ip_src))
-		{
-		  nbr_nbma->nbr = nbr;
-		  nbr->nbr_nbma = nbr_nbma;
-
-		  if (nbr_nbma->t_poll)
-		    OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
-		  
-		  nbr->state_change = nbr_nbma->state_change + 1;
-		}
-	    }
-	}
-      
-      /* New nbr, save the crypto sequence number if necessary */
-      if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
-	nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
-
-      if (IS_DEBUG_OSPF_EVENT)
-	zlog_info ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
-		   inet_ntoa (nbr->router_id));
-    }
   
-  nbr->router_id = ospfh->router_id;
+  /* get neighbour struct */
+  nbr = ospf_nbr_get (oi, ospfh, iph, &p);
+
+  /* neighbour must be valid, ospf_nbr_get creates if none existed */
+  assert (nbr);
 
   old_state = nbr->state;
 
@@ -1029,7 +970,7 @@
 
   dd = (struct ospf_db_desc *) STREAM_PNT (s);
 
-  nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
+  nbr = ospf_nbr_lookup (oi, iph, ospfh);
   if (nbr == NULL)
     {
       zlog_warn ("Packet[DD]: Unknown Neighbor %s",
@@ -1286,7 +1227,7 @@
   /* Increment statistics. */
   oi->ls_req_in++;
 
-  nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
+  nbr = ospf_nbr_lookup (oi, iph, ospfh);
   if (nbr == NULL)
     {
       zlog_warn ("Link State Request: Unknown Neighbor %s.",
@@ -1520,7 +1461,7 @@
   oi->ls_upd_in++;
 
   /* Check neighbor. */
-  nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
+  nbr = ospf_nbr_lookup (oi, iph, ospfh);
   if (nbr == NULL)
     {
       zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
@@ -1881,7 +1822,7 @@
   /* increment statistics. */
   oi->ls_ack_in++;
 
-  nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
+  nbr = ospf_nbr_lookup (oi, iph, ospfh);
   if (nbr == NULL)
     {
       zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
@@ -2043,8 +1984,7 @@
 }
 
 struct ospf_interface *
-ospf_associate_packet_vl (struct ospf *ospf,
-			  struct interface *ifp, struct ospf_interface *oi,
+ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp, 
 			  struct ip *iph, struct ospf_header *ospfh)
 {
   struct ospf_interface *rcv_oi;
@@ -2054,14 +1994,16 @@
 
   if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
       !OSPF_IS_AREA_BACKBONE (ospfh))
-    return oi;
+    return NULL;
 
-  if ((rcv_oi = oi) == NULL)
-    {
-     if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL, 
-                                                 iph->ip_dst)) == NULL)
-       return NULL;
-    }
+  /* look for local OSPF interface matching the destination
+   * to determine Area ID. We presume therefore the destination address
+   * is unique, or at least (for "unnumbered" links), not used in other 
+   * areas
+   */
+  if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL, 
+                                              iph->ip_dst)) == NULL)
+    return NULL;
 
   for (node = listhead (ospf->vlinks); node; nextnode (node))
     {
@@ -2092,7 +2034,7 @@
   if (IS_DEBUG_OSPF_EVENT)
     zlog_info ("couldn't find any VL to associate the packet with");
   
-  return oi;
+  return NULL;
 }
 
 int
@@ -2287,6 +2229,12 @@
   if (ibuf == NULL)
     return -1;
   
+  if (ifp == NULL)
+    {
+      stream_free (ibuf);
+      return 0;
+    }
+    
   iph = (struct ip *) STREAM_DATA (ibuf);
 
   /* prepare for next packet. */
@@ -2316,26 +2264,36 @@
 
   /* associate packet with ospf interface */
   oi = ospf_if_lookup_recv_if (ospf, iph->ip_src);
-  if (ifp && oi && oi->ifp != ifp)
+
+  /* if no local ospf_interface, 
+   * or header area is backbone but ospf_interface is not
+   * check for VLINK interface
+   */
+  if ( (oi == NULL) ||
+      (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
+      && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))
+     )
+    {
+      if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL)
+        {
+          zlog_warn ("Packet from [%s] received on link %s"
+                     " but no ospf_interface",
+                     inet_ntoa (iph->ip_src), ifp->name);
+          stream_free (ibuf);
+          return 0;
+        }
+    }
+    
+  /* else it must be a local ospf interface, check it was received on 
+   * correct link 
+   */
+  else if (oi->ifp != ifp)
     {
       zlog_warn ("Packet from [%s] received on wrong link %s",
                  inet_ntoa (iph->ip_src), ifp->name); 
       stream_free (ibuf);
       return 0;
     }
-  
-  if ((oi = ospf_associate_packet_vl (ospf, ifp, oi, iph, ospfh)) == NULL)
-    {
-      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
-        {
-          zlog_info ("ospf_read[%s/%s]: Could not associate packet with VL, "
-                     "dropping.",
-                     ospf_packet_type_str[ospfh->type],
-                     inet_ntoa (iph->ip_src));
-        }
-      stream_free (ibuf);
-      return 0;
-    }
 
   /*
    * If the received packet is destined for AllDRouters, the packet