[PtP over ethernet] New peer flag allows much more addressing flexibility

2006-12-12 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

	* if.h: (struct connected) Add new ZEBRA_IFA_PEER flag indicating
	  whether a peer address has been configured.  Comment now shows
	  the new interpretation of the destination addr: if ZEBRA_IFA_PEER
	  is set, then it must contain the destination address, otherwise
	  it may contain the broadcast address or be NULL.
	  (CONNECTED_DEST_HOST,CONNECTED_POINTOPOINT_HOST) Remove obsolete
	  macros that were specific to IPv4 and not fully general.
	  (CONNECTED_PEER) New macro to check ZEBRA_IFA_PEER flag.
	  (CONNECTED_PREFIX) New macro giving the prefix to insert into
	  the RIB: if CONNECTED_PEER, then use the destination (peer) address,
	  else use the address field.
	  (CONNECTED_ID) New macro to come up with an identifying address
	  for the struct connected.
	* if.c: (if_lookup_address, connected_lookup_address) Streamline
	  logic with new CONNECTED_PREFIX macro.
	* prefix.h: (PREFIX_COPY_IPV4, PREFIX_COPY_IPV6) New macros
	  for better performance than the general prefix_copy function.
	* zclient.c: (zebra_interface_address_read) For non-null destination
	  addresses, set prefixlen to equal the address prefixlen.  This
	  is needed to get the new CONNECTED_PREFIX macro to work properly.
	* connected.c: (connected_up_ipv4, connected_down_ipv4,
	  connected_up_ipv6, connected_down_ipv6) Simplify logic using the
	  new CONNECTED_PREFIX macro.
	  (connected_add_ipv4) Set prefixlen in destination addresses (required
	  by the CONNECTED_PREFIX macro).  Use CONNECTED_PEER macro instead
	  of testing for IFF_POINTOPOINT.  Delete invalid warning message.
	  Warn about cases where the ZEBRA_IFA_PEER is set but no
	  destination address has been supplied (and turn off the flag).
	  (connected_add_ipv6) Add new flags argument so callers may set
	  the ZEBRA_IFA_PEER flag.  If peer/broadcast address satisfies
	  IN6_IS_ADDR_UNSPECIFIED, then reject it with a warning.
	  Set prefixlen in destination address so CONNECTED_PREFIX will work.
	* connected.h: (connected_add_ipv6) Add new flags argument so
	  callers may set the ZEBRA_IFA_PEER flag.
	* interface.c: (connected_dump_vty) Use CONNECTED_PEER macro
	  to decide whether the destination address is a peer or broadcast
	  address (instead of checking IFF_BROADCAST and IFF_POINTOPOINT).
	* if_ioctl.c: (if_getaddrs) Instead of setting a peer address
	  only when the IFF_POINTOPOINT is set, we now accept a peer
	  address whenever it is available and not the same as the local
	  address.  Otherwise (no peer address assigned), we check
	  for a broadcast address (regardless of the IFF_BROADCAST flag).
	  And must now pass a flags value of ZEBRA_IFA_PEER to
	  connected_add_ipv4 when a peer address is assigned.
	  The same new logic is used with the IPv6 code as well (and we
	  pass the new flags argument to connected_add_ipv6).
	  (if_get_addr) Do not bother to check IFF_POINTOPOINT: just
	  issue the SIOCGIFDSTADDR ioctl and see if we get back
	  a peer address not matching the local address (and set
	  the ZEBRA_IFA_PEER in that case).  If there's no peer address,
	  try to grab SIOCGIFBRDADDR regardless of whether IFF_BROADCAST is set.
	* if_ioctl_solaris.c: (if_get_addr) Just try the SIOCGLIFDSTADDR ioctl
	  without bothering to check the IFF_POINTOPOINT flag.  And if
	  no peer address was found, just try the SIOCGLIFBRDADDR ioctl
	  without checking the IFF_BROADCAST flag.  Call connected_add_ipv4
	  and connected_add_ipv6 with appropriate flags.
	* if_proc.c: (ifaddr_proc_ipv6) Must pass new flags argument to
	  connected_add_ipv6.
	* kernel_socket.c: (ifam_read) Must pass new flags argument to
	  connected_add_ipv6.
	* rt_netlink.c: (netlink_interface_addr) Copy logic from iproute2
	  to determine local and possible peer address (so there's no longer
	  a test for IFF_POINTOPOINT).  Set ZEBRA_IFA_PEER flag appropriately.
	  Pass new flags argument to connected_add_ipv6.
	  (netlink_address) Test !CONNECTED_PEER instead of if_is_broadcast
	  to determine whether the connected destination address is a
	  broadcast address.
	* bgp_nexthop.c: (bgp_connected_add, bgp_connected_delete)
	  Simplify logic by using new CONNECTED_PREFIX macro.
	* ospf_interface.c: (ospf_if_is_configured, ospf_if_lookup_by_prefix,
	  ospf_if_lookup_recv_if) Simplify logic using new CONNECTED_PREFIX
	  macro.
	* ospf_lsa.c: (lsa_link_ptop_set) Using the new CONNECTED_PREFIX
	  macro, both options collapse into the same code.
	* ospf_snmp.c: (ospf_snmp_if_update) Simplify logic using new
	  CONNECTED_ID macro.
	  (ospf_snmp_is_if_have_addr) Simplify logic using new CONNECTED_PREFIX
	  macro.
	* ospf_vty.c: (show_ip_ospf_interface_sub) Use new CONNECTED_PEER macro
	  instead of testing the IFF_POINTOPOINT flag.
	* ospfd.c: (ospf_network_match_iface) Use new CONNECTED_PEER macro
	  instead of testing with if_is_pointopoint.  And add commented-out
	  code to implement alternative (in my opinion) more elegant behavior
	  that has no special-case treatment for PtP addresses.
	  (ospf_network_run) Use new CONNECTED_ID macro to simplify logic.
	* rip_interface.c: (rip_interface_multicast_set) Use new CONNECTED_ID
	  macro to simplify logic.
	  (rip_request_interface_send) Fix minor bug: ipv4_broadcast_addr does
	  not give a useful result if prefixlen is 32 (we require a peer
	  address in such cases).
	* ripd.c: (rip_update_interface) Fix same bug as above.
diff --git a/ospfd/ChangeLog b/ospfd/ChangeLog
index 0a2b999..c80f3b6 100644
--- a/ospfd/ChangeLog
+++ b/ospfd/ChangeLog
@@ -1,3 +1,22 @@
+2006-12-12 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+	* ospf_interface.c: (ospf_if_is_configured, ospf_if_lookup_by_prefix,
+	  ospf_if_lookup_recv_if) Simplify logic using new CONNECTED_PREFIX
+	  macro.
+	* ospf_lsa.c: (lsa_link_ptop_set) Using the new CONNECTED_PREFIX
+	  macro, both options collapse into the same code.
+	* ospf_snmp.c: (ospf_snmp_if_update) Simplify logic using new
+	  CONNECTED_ID macro.
+	  (ospf_snmp_is_if_have_addr) Simplify logic using new CONNECTED_PREFIX
+	  macro.
+	* ospf_vty.c: (show_ip_ospf_interface_sub) Use new CONNECTED_PEER macro
+	  instead of testing the IFF_POINTOPOINT flag.
+	* ospfd.c: (ospf_network_match_iface) Use new CONNECTED_PEER macro
+	  instead of testing with if_is_pointopoint.  And add commented-out
+	  code to implement alternative (in my opinion) more elegant behavior
+	  that has no special-case treatment for PtP addresses.
+	  (ospf_network_run) Use new CONNECTED_ID macro to simplify logic.
+
 2006-12-04 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
 	* ospfd.c: (ospf_network_run) Remove an offending 'break' statement.
diff --git a/ospfd/ospf_interface.c b/ospfd/ospf_interface.c
index b6e3474..4b6b424 100644
--- a/ospfd/ospf_interface.c
+++ b/ospfd/ospf_interface.c
@@ -354,28 +354,18 @@
   addr.family = AF_INET;
   addr.prefix = *address;
   addr.prefixlen = IPV4_MAX_PREFIXLEN;
-  
+
   for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
     if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
       {
-	if (oi->type == OSPF_IFTYPE_POINTOPOINT)
+        if (oi->type == OSPF_IFTYPE_POINTOPOINT)
 	  {
-	    if (CONNECTED_DEST_HOST(oi->connected))
-	      {
-		/* match only destination addr, since local addr is most likely
-		 * not unique (borrowed from another interface) */
-		if (IPV4_ADDR_SAME (address,
-				    &oi->connected->destination->u.prefix4))
-		return oi;
-	      }
-	    else
-	      {
-		/* special leniency: match if addr is anywhere on PtP subnet */
-		if (prefix_match(oi->address,(struct prefix *)&addr))
-		  return oi;
-	      }
+	    /* special leniency: match if addr is anywhere on peer subnet */
+	    if (prefix_match(CONNECTED_PREFIX(oi->connected),
+			     (struct prefix *)&addr))
+	      return oi;
 	  }
-	else
+        else
 	  {
 	    if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
 	      return oi;
@@ -432,22 +422,15 @@
 {
   struct listnode *node;
   struct ospf_interface *oi;
-  struct prefix ptmp;
   
   /* Check each Interface. */
   for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
     {
       if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
 	{
-	  if ((oi->type == OSPF_IFTYPE_POINTOPOINT) &&
-	      CONNECTED_DEST_HOST(oi->connected))
-	    {
-	      prefix_copy (&ptmp, oi->connected->destination);
-	      ptmp.prefixlen = IPV4_MAX_BITLEN;
-	    }
-	  else
-	    prefix_copy (&ptmp, oi->address);
-	
+	  struct prefix ptmp;
+
+	  prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
 	  apply_mask (&ptmp);
 	  if (prefix_same (&ptmp, (struct prefix *) p))
 	    return oi;
@@ -477,22 +460,14 @@
       
       if (if_is_loopback (oi->ifp))
         continue;
-      
-      if ((oi->type == OSPF_IFTYPE_POINTOPOINT) &&
-	  CONNECTED_DEST_HOST(oi->connected))
+
+      if (prefix_match (CONNECTED_PREFIX(oi->connected),
+      			(struct prefix *) &addr))
 	{
-	  if (IPV4_ADDR_SAME (&oi->connected->destination->u.prefix4, &src))
-	    return oi;
-	}
-      else
-	{
-	  if (prefix_match (oi->address, (struct prefix *) &addr))
-	    {
-	      if ( (match == NULL) || 
-	           (match->address->prefixlen < oi->address->prefixlen)
-	         )
-	        match = oi;
-	    }
+	  if ( (match == NULL) || 
+	       (match->address->prefixlen < oi->address->prefixlen)
+	     )
+	    match = oi;
 	}
     }
 
diff --git a/ospfd/ospf_lsa.c b/ospfd/ospf_lsa.c
index 05eed35..b9a70a6 100644
--- a/ospfd/ospf_lsa.c
+++ b/ospfd/ospf_lsa.c
@@ -543,7 +543,7 @@
   return 1;
 }
 
-/* Describe Point-to-Point link. */
+/* Describe Point-to-Point link (Section 12.4.1.1). */
 static int
 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
 {
@@ -564,28 +564,13 @@
 		                LSA_LINK_TYPE_POINTOPOINT, 0, cost);
       }
 
-  if (CONNECTED_DEST_HOST(oi->connected))
-    {
-      /* Option 1:
-	 link_type = LSA_LINK_TYPE_STUB;
-	 link_id = nbr->address.u.prefix4;
-	 link_data.s_addr = 0xffffffff;
-	 link_cost = o->output_cost; */
-      
-      id.s_addr = oi->connected->destination->u.prefix4.s_addr;
-      mask.s_addr = 0xffffffff;
-      links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
-                              oi->output_cost);
-    }
-  else
-    {
-       /* Option 2:  We need to include link to a stub
-	 network regardless of the state of the neighbor */
-      masklen2ip (oi->address->prefixlen, &mask);
-      id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
-      links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 
-                              oi->output_cost);
-    }
+  /* Regardless of the state of the neighboring router, we must
+     add a Type 3 link (stub network).
+     N.B. Options 1 & 2 share basically the same logic. */
+  masklen2ip (oi->address->prefixlen, &mask);
+  id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
+  links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
+			  oi->output_cost);
   return links;
 }
 
diff --git a/ospfd/ospf_snmp.c b/ospfd/ospf_snmp.c
index 4a6cc15..bc594b3 100644
--- a/ospfd/ospf_snmp.c
+++ b/ospfd/ospf_snmp.c
@@ -1439,10 +1439,7 @@
   /* Lookup first IPv4 address entry. */
   for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
     {
-      if (CONNECTED_POINTOPOINT_HOST(ifc))
-	p = ifc->destination;
-      else
-	p = ifc->address;
+      p = CONNECTED_ID(ifc);
 
       if (p->family == AF_INET)
 	{
@@ -1492,19 +1489,13 @@
 int
 ospf_snmp_is_if_have_addr (struct interface *ifp)
 {
-  struct prefix *p;
   struct listnode *nn;
   struct connected *ifc;
 
   /* Is this interface having any connected IPv4 address ? */
   for (ALL_LIST_ELEMENTS_RO (ifp->connected, nn, ifc))
   {
-    if (if_is_pointopoint (ifp))
-      p = ifc->destination;
-    else
-      p = ifc->address;
-
-    if (p->family == AF_INET)
+    if (CONNECTED_PREFIX(ifc)->family == AF_INET)
       return 1;
   }
   
diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c
index d6da11d..dd700b0 100644
--- a/ospfd/ospf_vty.c
+++ b/ospfd/ospf_vty.c
@@ -2865,8 +2865,8 @@
           struct in_addr *dest;
           const char *dstr;
           
-	  if ((ifp->flags & IFF_POINTOPOINT)
-              || oi->type == OSPF_IFTYPE_VIRTUALLINK)
+	  if (CONNECTED_PEER(oi->connected)
+	      || oi->type == OSPF_IFTYPE_VIRTUALLINK)
             dstr = "Peer";
           else
             dstr = "Broadcast";
diff --git a/ospfd/ospfd.c b/ospfd/ospfd.c
index dd5af5e..f72295d 100644
--- a/ospfd/ospfd.c
+++ b/ospfd/ospfd.c
@@ -795,6 +795,23 @@
 int 
 ospf_network_match_iface(struct connected *co, struct prefix *net)
 {
+#define COMPATIBILITY_MODE
+  /* The old code used to have a special case for PtP interfaces:
+
+     if (if_is_pointopoint (co->ifp) && co->destination &&
+	 IPV4_ADDR_SAME ( &(co->destination->u.prefix4), &(net->u.prefix4)))
+       return 1;
+
+     The new approach is much more general.  If a peer address is supplied,
+     then we are routing to that prefix, so that's the address to compare
+     against (not the local address, which may not be unique).
+  */
+#ifndef COMPATIBILITY_MODE
+  /* new approach: more elegant and conceptually clean */
+  return prefix_match(net, CONNECTED_PREFIX(co));
+#else /* COMPATIBILITY_MODE */
+  /* match old (strange?) behavior */
+
   /* Behaviour to match both Cisco where:
    *   iface address lies within network specified -> ospf
    * and zebra 0.9[2ish-3]:
@@ -806,7 +823,7 @@
    * exactly; this is not a test for falling within the prefix.  This
    * test is solely for compatibility with zebra.
    */
-  if (if_is_pointopoint (co->ifp) && co->destination &&
+  if (CONNECTED_PEER(co) &&
       IPV4_ADDR_SAME ( &(co->destination->u.prefix4), &(net->u.prefix4)))
     return 1;
 
@@ -826,6 +843,8 @@
     return 1;
 
   return 0;			/* no match */
+
+#endif /* COMPATIBILITY_MODE */
 }
 
 void
@@ -856,10 +875,7 @@
           if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
             continue;
 
-	  if (CONNECTED_POINTOPOINT_HOST(co))
-	    addr = co->destination;
-	  else 
-	    addr = co->address;
+	  addr = CONNECTED_ID(co);
 
 	  if (p->family == co->address->family 
 	      && ! ospf_if_is_configured (ospf, &(addr->u.prefix4))