]> git.ipfire.org Git - thirdparty/bird.git/blobdiff - nest/rt-dev.c
KRT: Remove KRF_INSTALLED flag
[thirdparty/bird.git] / nest / rt-dev.c
index ebe3a1a2e13b80e1db7f5a74ff2281cf1235813c..61f025cea8a28a974e9542dc579db83934f5184e 100644 (file)
@@ -1,14 +1,19 @@
 /*
  *     BIRD -- Direct Device Routes
  *
- *     (c) 1998--1999 Martin Mares <mj@ucw.cz>
+ *     (c) 1998--2000 Martin Mares <mj@ucw.cz>
  *
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-#define LOCAL_DEBUG
+/**
+ * DOC: Direct
+ *
+ * The Direct protocol works by converting all ifa_notify() events it receives
+ * to rte_update() calls for the corresponding network.
+ */
 
-#include <string.h>
+#undef LOCAL_DEBUG
 
 #include "nest/bird.h"
 #include "nest/iface.h"
 #include "nest/rt-dev.h"
 #include "conf/conf.h"
 #include "lib/resource.h"
+#include "lib/string.h"
 
-struct proto_config *cf_dev_proto;
 
 static void
-dev_if_notify(struct proto *p, unsigned c, struct iface *new, struct iface *old)
+dev_ifa_notify(struct proto *P, uint flags, struct ifa *ad)
 {
-  struct rt_dev_config *P = (void *) p->cf;
+  struct rt_dev_proto *p = (void *) P;
+  struct rt_dev_config *cf = (void *) P->cf;
+  struct channel *c;
+  net_addr *net = &ad->prefix;
+
+  if (!EMPTY_LIST(cf->iface_list) &&
+      !iface_patt_find(&cf->iface_list, ad->iface, ad))
+    /* Empty list is automatically treated as "*" */
+    return;
+
+  if (ad->flags & IA_SECONDARY)
+    return;
 
-  if (old && !iface_patt_match(&P->iface_list, old) ||
-      new && !iface_patt_match(&P->iface_list, new))
+  if (ad->scope <= SCOPE_LINK)
     return;
-  if (c & IF_CHANGE_DOWN)
+
+  if (ad->prefix.type == NET_IP4)
+    c = p->ip4_channel;
+  else if (ad->prefix.type == NET_IP6)
+    c = p->ip6_channel;
+  else
+    return;
+
+  if (!c)
+    return;
+
+  /* For IPv6 SADR, replace regular prefix with SADR prefix */
+  if (c->net_type == NET_IP6_SADR)
+  {
+    net = alloca(sizeof(net_addr_ip6_sadr));
+    net_fill_ip6_sadr(net, net6_prefix(&ad->prefix), net6_pxlen(&ad->prefix), IP6_NONE, 0);
+  }
+
+  if (flags & IF_CHANGE_DOWN)
     {
-      net *n;
-
-      debug("dev_if_notify: %s going down\n", old->name);
-      n = net_find(&master_table, 0, old->prefix, old->pxlen);
-      if (!n)
-       {
-         debug("dev_if_notify: device shutdown: prefix not found\n");
-         return;
-       }
-      rte_update(n, p, NULL);
+      DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
+
+      /* Use iface ID as local source ID */
+      struct rte_src *src = rt_get_source(P, ad->iface->index);
+      rte_update2(c, net, NULL, src);
     }
-  else if (c & IF_CHANGE_UP)
+  else if (flags & IF_CHANGE_UP)
     {
-      rta *a, A;
-      net *n;
+      rta *a;
       rte *e;
 
-      debug("dev_if_notify: %s going up\n", new->name);
-      bzero(&A, sizeof(A));
-      A.proto = p;
-      A.source = RTS_DEVICE;
-      A.scope = (new->flags & IF_LOOPBACK) ? SCOPE_HOST : SCOPE_UNIVERSE;
-      A.cast = RTC_UNICAST;
-      A.dest = RTD_DEVICE;
-      A.iface = new;
-      A.attrs = NULL;
-      a = rta_lookup(&A);
-      if (new->flags & IF_UNNUMBERED)
-       n = net_get(&master_table, 0, new->opposite, new->pxlen);
-      else
-       n = net_get(&master_table, 0, new->prefix, new->pxlen);
+      DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
+
+      if (cf->check_link && !(ad->iface->flags & IF_LINK_UP))
+       return;
+
+      /* Use iface ID as local source ID */
+      struct rte_src *src = rt_get_source(P, ad->iface->index);
+
+      rta a0 = {
+       .src = src,
+       .source = RTS_DEVICE,
+       .scope = SCOPE_UNIVERSE,
+       .dest = RTD_UNICAST,
+       .nh.iface = ad->iface,
+      };
+
+      a = rta_lookup(&a0);
       e = rte_get_temp(a);
-      e->net = n;
       e->pflags = 0;
-      rte_update(n, p, e);
+      rte_update2(c, net, e, src);
     }
 }
 
+static void
+dev_if_notify(struct proto *p, uint c, struct iface *iface)
+{
+  struct rt_dev_config *cf = (void *) p->cf;
+
+  if (c & (IF_CHANGE_UP | IF_CHANGE_DOWN))
+    return;
+
+  if ((c & IF_CHANGE_LINK) && cf->check_link)
+  {
+    uint ac = (iface->flags & IF_LINK_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN;
+
+    struct ifa *a;
+    WALK_LIST(a, iface->addrs)
+      dev_ifa_notify(p, ac, a);
+  }
+}
+
+static void
+dev_postconfig(struct proto_config *CF)
+{
+  struct rt_dev_config *cf = (void *) CF;
+  struct channel_config *ip4, *ip6, *ip6_sadr;
+
+  ip4 = proto_cf_find_channel(CF, NET_IP4);
+  ip6 = proto_cf_find_channel(CF, NET_IP6);
+  ip6_sadr = proto_cf_find_channel(CF, NET_IP6_SADR);
+
+  if (ip6 && ip6_sadr)
+    cf_error("Both ipv6 and ipv6-sadr channels");
+
+  cf->ip4_channel = ip4;
+  cf->ip6_channel = ip6 ?: ip6_sadr;
+}
+
 static struct proto *
-dev_init(struct proto_config *c)
+dev_init(struct proto_config *CF)
 {
-  struct proto *p = proto_new(c, sizeof(struct proto));
+  struct proto *P = proto_new(CF);
+  struct rt_dev_proto *p = (void *) P;
+  struct rt_dev_config *cf = (void *) CF;
 
-  p->if_notify = dev_if_notify;
-  return p;
+  proto_configure_channel(P, &p->ip4_channel, cf->ip4_channel);
+  proto_configure_channel(P, &p->ip6_channel, cf->ip6_channel);
+
+  P->if_notify = dev_if_notify;
+  P->ifa_notify = dev_ifa_notify;
+
+  return P;
+}
+
+static int
+dev_reconfigure(struct proto *P, struct proto_config *CF)
+{
+  struct rt_dev_proto *p = (void *) P;
+  struct rt_dev_config *o = (void *) P->cf;
+  struct rt_dev_config *n = (void *) CF;
+
+  if (!iface_patts_equal(&o->iface_list, &n->iface_list, NULL) ||
+      (o->check_link != n->check_link))
+    return 0;
+
+  return
+    proto_configure_channel(P, &p->ip4_channel, n->ip4_channel) &&
+    proto_configure_channel(P, &p->ip6_channel, n->ip6_channel);
+
+  return 1;
 }
 
 static void
-dev_preconfig(struct protocol *x, struct config *c)
+dev_copy_config(struct proto_config *dest, struct proto_config *src)
 {
-  struct rt_dev_config *p = proto_config_new(&proto_device, sizeof(struct rt_dev_config));
-  struct iface_patt *k = cfg_alloc(sizeof(struct iface_patt));
-
-  cf_dev_proto = &p->c;
-  p->c.preference = DEF_PREF_DIRECT;
-  init_list(&p->iface_list);
-  k->pattern = "*";
-  add_tail(&p->iface_list, &k->n);
+  struct rt_dev_config *d = (void *) dest;
+  struct rt_dev_config *s = (void *) src;
+
+  /*
+   * We copy iface_list as ifaces can be shared by more direct protocols.
+   * Copy suffices to be is shallow, because new nodes can be added, but
+   * old nodes cannot be modified (although they contain internal lists).
+   */
+  cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct iface_patt));
+
+  d->check_link = s->check_link;
 }
 
 struct protocol proto_device = {
-  name:                "Device",
-  preconfig:   dev_preconfig,
-  init:                dev_init,
+  .name =              "Direct",
+  .template =          "direct%d",
+  .class =             PROTOCOL_DIRECT,
+  .preference =                DEF_PREF_DIRECT,
+  .channel_mask =      NB_IP | NB_IP6_SADR,
+  .proto_size =                sizeof(struct rt_dev_proto),
+  .config_size =       sizeof(struct rt_dev_config),
+  .postconfig =                dev_postconfig,
+  .init =              dev_init,
+  .reconfigure =       dev_reconfigure,
+  .copy_config =       dev_copy_config
 };