zebra: add ZEBRA_IFC_QUEUED to keep track of kernel state

As there are timeframes when we don't get a notification from the kernel
about new addresses. (e.g. while Linux performs IPv6 DAD), we need to
have some information whether an address has been sent to the kernel or
not.

One case where this is relevant would be a user adding an IPv6 address,
but deleting it before DAD has been complete. With the next patch which
removes some (ill assuming) synchronous parts in address setup,
ipv6_address_uninstall would not know whether or not it has to actually
delete the prefix from the kernel. Resolving these windows where we lack
information is what the flag ZEBRA_IFC_QUEUED is intended for.

Signed-off-by: Christian Franke <chris@opensourcerouting.org>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/lib/if.h b/lib/if.h
index 2116e12..13cc254 100644
--- a/lib/if.h
+++ b/lib/if.h
@@ -151,11 +151,16 @@
   u_char conf;
 #define ZEBRA_IFC_REAL         (1 << 0)
 #define ZEBRA_IFC_CONFIGURED   (1 << 1)
+#define ZEBRA_IFC_QUEUED       (1 << 2)
   /*
      The ZEBRA_IFC_REAL flag should be set if and only if this address
-     exists in the kernel.
+     exists in the kernel and is actually usable. (A case where it exists but
+     is not yet usable would be IPv6 with DAD)
      The ZEBRA_IFC_CONFIGURED flag should be set if and only if this address
      was configured by the user from inside quagga.
+     The ZEBRA_IFC_QUEUED flag should be set if and only if the address exists
+     in the kernel. It may and should be set although the address might not be
+     usable yet. (compare with ZEBRA_IFC_REAL)
    */
 
   /* Flags for connected address. */
diff --git a/zebra/connected.c b/zebra/connected.c
index 38ab37d..d474560 100644
--- a/zebra/connected.c
+++ b/zebra/connected.c
@@ -62,6 +62,9 @@
       UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
     }
 
+  /* The address is not in the kernel anymore, so clear the flag */
+  UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
+
   if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
     {
       listnode_delete (ifc->ifp->connected, ifc);
@@ -211,6 +214,9 @@
   ifc = connected_new ();
   ifc->ifp = ifp;
   ifc->flags = flags;
+  /* If we get a notification from the kernel,
+   * we can safely assume the address is known to the kernel */
+  SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
 
   /* Allocate new connected address. */
   p = prefix_ipv4_new ();
@@ -363,6 +369,9 @@
   ifc = connected_new ();
   ifc->ifp = ifp;
   ifc->flags = flags;
+  /* If we get a notification from the kernel,
+   * we can safely assume the address is known to the kernel */
+  SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
 
   /* Allocate new connected address. */
   p = prefix_ipv6_new ();
diff --git a/zebra/interface.c b/zebra/interface.c
index cd78ebb..470df0c 100644
--- a/zebra/interface.c
+++ b/zebra/interface.c
@@ -289,7 +289,7 @@
       p = ifc->address;
 	
       if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
-	  && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
+	  && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED))
 	{
 	  /* Address check. */
 	  if (p->family == AF_INET)
@@ -321,6 +321,7 @@
 	      /* Add to subnet chain list. */
 	      if_subnet_add (ifp, ifc);
 
+	      SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
 	      SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
 
 	      zebra_interface_address_add_update (ifp, ifc);
@@ -345,6 +346,7 @@
 			     safe_strerror(errno));
 		  continue;
 		}
+	      SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
 	      SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
 
 	      zebra_interface_address_add_update (ifp, ifc);
@@ -454,6 +456,7 @@
 		  zebra_interface_address_delete_update (ifp, ifc);
 
 		  UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
+		  UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
 
 		  /* Remove from subnet chain. */
 		  list_delete_node (addr_list, anode);
@@ -482,6 +485,7 @@
 	      zebra_interface_address_delete_update (ifp, ifc);
 
 	      UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
+	      UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
 
 	      if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
 		last = node;
@@ -1229,7 +1233,7 @@
     SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
 
   /* In case of this route need to install kernel. */
-  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
+  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
       && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
     {
       /* Some system need to up the interface to set IP address. */
@@ -1251,6 +1255,7 @@
       if_subnet_add (ifp, ifc);
 
       /* IP address propery set. */
+      SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
       SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
 
       /* Update interface address information to protocol daemon. */
@@ -1296,7 +1301,7 @@
   UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
   
   /* This is not real address or interface is not active. */
-  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
+  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
       || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
     {
       listnode_delete (ifp->connected, ifc);
@@ -1321,6 +1326,7 @@
    * through the route socket, and we don't want to touch that behaviour
    * for now.  It should work without the #ifdef, but why take the risk...
    * -- equinox 2012-07-13 */
+  UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
 #ifdef HAVE_NETLINK
 
   /* Remove connected route. */
@@ -1437,7 +1443,7 @@
     SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
 
   /* In case of this route need to install kernel. */
-  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
+  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
       && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
     {
       /* Some system need to up the interface to set IP address. */
@@ -1457,6 +1463,7 @@
 	}
 
       /* IP address propery set. */
+      SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
       SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
 
       /* Update interface address information to protocol daemon. */
@@ -1502,7 +1509,7 @@
   UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
 
   /* This is not real address or interface is not active. */
-  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
+  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
       || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
     {
       listnode_delete (ifp->connected, ifc);
@@ -1519,6 +1526,8 @@
       return CMD_WARNING;
     }
 
+  UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
+
   /* Redistribute this information. */
   zebra_interface_address_delete_update (ifp, ifc);