]> git.ipfire.org Git - thirdparty/bird.git/blobdiff - nest/neighbor.c
Doc: Remove some superfluous slashes
[thirdparty/bird.git] / nest / neighbor.c
index 7aaf8d72caceb8885fdc74522a2b50116fca3ed9..f8159d35d4ecb0618bf3827d66e8a47de2f5e23e 100644 (file)
@@ -30,7 +30,8 @@
  * when the protocol has explicitly requested it via the %NEF_STICKY
  * flag because it wishes to be notified when the node will again become
  * a neighbor. Such entries are enqueued in a special list which is walked
- * whenever an interface changes its state to up.
+ * whenever an interface changes its state to up. Neighbor entry VRF
+ * association is implied by respective protocol.
  *
  * When a neighbor event occurs (a neighbor gets disconnected or a sticky
  * inactive neighbor becomes connected), the protocol hook neigh_notify()
 static slab *neigh_slab;
 static list sticky_neigh_list, neigh_hash_table[NEIGH_HASH_SIZE];
 
-static inline unsigned int
+static inline uint
 neigh_hash(struct proto *p, ip_addr *a)
 {
   return (p->hash_key ^ ipa_hash(*a)) & (NEIGH_HASH_SIZE-1);
 }
 
 static int
-if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
+if_connected(ip_addr *a, struct iface *i, struct ifa **ap)
 {
   struct ifa *b;
 
   if (!(i->flags & IF_UP))
+  {
+    *ap = NULL;
     return -1;
+  }
+
   WALK_LIST(b, i->addrs)
     {
+      *ap = b;
+
       if (ipa_equal(*a, b->ip))
        return SCOPE_HOST;
-      if (b->flags & IA_UNNUMBERED)
+      if (b->flags & IA_PEER)
        {
          if (ipa_equal(*a, b->opposite))
            return b->scope;
@@ -75,13 +82,22 @@ if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
        {
          if (ipa_in_net(*a, b->prefix, b->pxlen))
            {
-             if (ipa_equal(*a, b->prefix) ||   /* Network address */
-                 ipa_equal(*a, b->brd))        /* Broadcast */
+#ifndef IPV6
+             if ((b->pxlen < (BITS_PER_IP_ADDRESS - 1)) &&
+                 (ipa_equal(*a, b->prefix) ||  /* Network address */
+                  ipa_equal(*a, b->brd)))      /* Broadcast */
+             {
+               *ap = NULL;
                return -1;
+             }
+#endif
+
              return b->scope;
            }
        }
       }
+
+  *ap = NULL;
   return -1;
 }
 
@@ -99,57 +115,71 @@ if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
  * If the node is not connected directly or *@a is not a valid unicast
  * IP address, neigh_find() returns %NULL.
  */
-
-/*
 neighbor *
 neigh_find(struct proto *p, ip_addr *a, unsigned flags)
 {
   return neigh_find2(p, a, NULL, flags);
 }
-*/
+
 
 neighbor *
-neigh_find(struct proto *p, ip_addr *a, unsigned flags)
+neigh_find2(struct proto *p, ip_addr *a, struct iface *ifa, unsigned flags)
 {
   neighbor *n;
-  int class, scope = SCOPE_HOST;
-  unsigned int h = neigh_hash(p, a);
-  struct iface *i, *j;
+  int class, scope = -1;
+  uint h = neigh_hash(p, a);
+  struct iface *i;
+  struct ifa *addr;
 
   WALK_LIST(n, neigh_hash_table[h])    /* Search the cache */
-    if (n->proto == p && ipa_equal(*a, n->addr))
+    if (n->proto == p && ipa_equal(*a, n->addr) && (!ifa || (ifa == n->iface)))
       return n;
 
   class = ipa_classify(*a);
   if (class < 0)                       /* Invalid address */
     return NULL;
-  if ((class & IADDR_SCOPE_MASK) < SCOPE_SITE ||
+  if (((class & IADDR_SCOPE_MASK) == SCOPE_HOST) ||
+      (((class & IADDR_SCOPE_MASK) == SCOPE_LINK) && (ifa == NULL)) ||
       !(class & IADDR_HOST))
     return NULL;                       /* Bad scope or a somecast */
 
-  j = NULL;
-  WALK_LIST(i, iface_list)
-    if ((scope = if_connected(a, i)) >= 0)
-      {
-       j = i;
-       break;
-      }
-  if (!j && !(flags & NEF_STICKY))
+  if (ifa)
+    {
+      scope = if_connected(a, ifa, &addr);
+      flags |= NEF_BIND;
+
+      if ((scope < 0) && (flags & NEF_ONLINK))
+       scope = class & IADDR_SCOPE_MASK;
+    }
+  else
+    WALK_LIST(i, iface_list)
+      if ((!p->vrf || p->vrf == i->master) &&
+         ((scope = if_connected(a, i, &addr)) >= 0))
+        {
+         ifa = i;
+         break;
+       }
+
+  /* scope < 0 means i don't know neighbor */
+  /* scope >= 0 implies ifa != NULL */
+
+  if ((scope < 0) && !(flags & NEF_STICKY))
     return NULL;
 
   n = sl_alloc(neigh_slab);
   n->addr = *a;
-  n->iface = j;
-  if (j)
+  if (scope >= 0)
     {
       add_tail(&neigh_hash_table[h], &n->n);
-      add_tail(&j->neighbors, &n->if_n);
+      add_tail(&ifa->neighbors, &n->if_n);
     }
   else
     {
       add_tail(&sticky_neigh_list, &n->n);
-      scope = 0;
+      scope = -1;
     }
+  n->iface = ifa;
+  n->ifa = addr;
   n->proto = p;
   n->data = NULL;
   n->aux = 0;
@@ -200,6 +230,54 @@ neigh_dump_all(void)
   debug("\n");
 }
 
+static void
+neigh_up(neighbor *n, struct iface *i, int scope, struct ifa *a)
+{
+  n->iface = i;
+  n->ifa = a;
+  n->scope = scope;
+  add_tail(&i->neighbors, &n->if_n);
+  rem_node(&n->n);
+  add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
+  DBG("Waking up sticky neighbor %I\n", n->addr);
+  if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
+    n->proto->neigh_notify(n);
+}
+
+static void
+neigh_down(neighbor *n)
+{
+  DBG("Flushing neighbor %I on %s\n", n->addr, n->iface->name);
+  rem_node(&n->if_n);
+  if (! (n->flags & NEF_BIND))
+    n->iface = NULL;
+  n->ifa = NULL;
+  n->scope = -1;
+  if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
+    n->proto->neigh_notify(n);
+  rem_node(&n->n);
+  if (n->flags & NEF_STICKY)
+    {
+      add_tail(&sticky_neigh_list, &n->n);
+
+      /* Respawn neighbor if there is another matching prefix */
+      struct iface *i;
+      struct ifa *a;
+      int scope;
+
+      if (!n->iface)
+       WALK_LIST(i, iface_list)
+         if ((scope = if_connected(&n->addr, i, &a)) >= 0)
+           {
+             neigh_up(n, i, scope, a);
+             return;
+           }
+    }
+  else
+    sl_free(neigh_slab, n);
+}
+
+
 /**
  * neigh_if_up: notify neighbor cache about interface up event
  * @i: interface in question
@@ -212,21 +290,14 @@ neigh_dump_all(void)
 void
 neigh_if_up(struct iface *i)
 {
+  struct ifa *a;
   neighbor *n, *next;
   int scope;
 
   WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
-    if ((scope = if_connected(&n->addr, i)) >= 0)
-      {
-       n->iface = i;
-       n->scope = scope;
-       add_tail(&i->neighbors, &n->if_n);
-       rem_node(&n->n);
-       add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
-       DBG("Waking up sticky neighbor %I\n", n->addr);
-       if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
-         n->proto->neigh_notify(n);
-      }
+    if ((!n->iface || n->iface == i) &&
+       ((scope = if_connected(&n->addr, i, &a)) >= 0))
+      neigh_up(n, i, scope, a);
 }
 
 /**
@@ -243,29 +314,67 @@ neigh_if_down(struct iface *i)
 {
   node *x, *y;
 
+  WALK_LIST_DELSAFE(x, y, i->neighbors)
+    neigh_down(SKIP_BACK(neighbor, if_n, x));
+}
+
+/**
+ * neigh_if_link - notify neighbor cache about interface link change
+ * @i: the interface in question
+ *
+ * Notify the neighbor cache that an interface changed link state.
+ * All owners of neighbor entries connected to this interface are
+ * notified.
+ */
+void
+neigh_if_link(struct iface *i)
+{
+  node *x, *y;
+
   WALK_LIST_DELSAFE(x, y, i->neighbors)
     {
       neighbor *n = SKIP_BACK(neighbor, if_n, x);
-      DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
-      rem_node(&n->if_n);
-      n->iface = NULL;
       if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
        n->proto->neigh_notify(n);
-      rem_node(&n->n);
-      if (n->flags & NEF_STICKY)
-       add_tail(&sticky_neigh_list, &n->n);
-      else
-       sl_free(neigh_slab, n);
     }
 }
 
+/**
+ * neigh_ifa_update: notify neighbor cache about interface address add or remove event
+ * @a: interface address in question
+ *
+ * Tell the neighbor cache that an address was added or removed.
+ *
+ * The neighbor cache wakes up all inactive sticky neighbors with
+ * addresses belonging to prefixes of the interface belonging to @ifa
+ * and causes all unreachable neighbors to be flushed.
+ */
+void
+neigh_ifa_update(struct ifa *a)
+{
+  struct iface *i = a->iface;
+  node *x, *y;
+  /* Remove all neighbors whose scope has changed */
+  WALK_LIST_DELSAFE(x, y, i->neighbors)
+    {
+      struct ifa *aa;
+      neighbor *n = SKIP_BACK(neighbor, if_n, x);
+      if (if_connected(&n->addr, i, &aa) != n->scope)
+       neigh_down(n);
+    }
+
+  /* Wake up all sticky neighbors that are reachable now */
+  neigh_if_up(i);
+}
+
 static inline void
 neigh_prune_one(neighbor *n)
 {
   if (n->proto->proto_state != PS_DOWN)
     return;
   rem_node(&n->n);
-  if (n->iface)
+  if (n->scope >= 0)
     rem_node(&n->if_n);
   sl_free(neigh_slab, n);
 }