]> git.ipfire.org Git - thirdparty/bird.git/blobdiff - sysdep/unix/krt.c
Filter: Allow silent filter execution
[thirdparty/bird.git] / sysdep / unix / krt.c
index 0a223a4f8223999efb71bf517e731fa81fcd59f1..526c0cab64a70a96f0f7a5a30f0722260b8351ff 100644 (file)
@@ -77,14 +77,15 @@ krt_io_init(void)
   krt_pool = rp_new(&root_pool, "Kernel Syncer");
   krt_filter_lp = lp_new(krt_pool, 4080);
   init_list(&krt_proto_list);
+  krt_sys_io_init();
 }
 
 /*
  *     Interfaces
  */
 
+struct kif_proto *kif_proto;
 static struct kif_config *kif_cf;
-static struct kif_proto *kif_proto;
 static timer *kif_scan_timer;
 static bird_clock_t kif_last_shot;
 
@@ -416,51 +417,65 @@ again:
       net *n = (net *) f;
       rte *e, **ee, *best, **pbest, *old_best;
 
-      old_best = n->routes;
+      /*
+       * Note that old_best may be NULL even if there was an old best route in
+       * the previous step, because it might be replaced in krt_learn_scan().
+       * But in that case there is a new valid best route.
+       */
+
+      old_best = NULL;
       best = NULL;
       pbest = NULL;
       ee = &n->routes;
       while (e = *ee)
        {
+         if (e->u.krt.best)
+           old_best = e;
+
          if (!e->u.krt.seen)
            {
              *ee = e->next;
              rte_free(e);
              continue;
            }
+
          if (!best || best->u.krt.metric > e->u.krt.metric)
            {
              best = e;
              pbest = ee;
            }
+
          e->u.krt.seen = 0;
+         e->u.krt.best = 0;
          ee = &e->next;
        }
       if (!n->routes)
        {
          DBG("%I/%d: deleting\n", n->n.prefix, n->n.pxlen);
          if (old_best)
-           {
-             krt_learn_announce_delete(p, n);
-             n->n.flags &= ~KRF_INSTALLED;
-           }
+           krt_learn_announce_delete(p, n);
+
          FIB_ITERATE_PUT(&fit, f);
          fib_delete(fib, f);
          goto again;
        }
+
+      best->u.krt.best = 1;
       *pbest = best->next;
       best->next = n->routes;
       n->routes = best;
-      if (best != old_best || !(n->n.flags & KRF_INSTALLED))
+
+      if ((best != old_best) || p->reload)
        {
          DBG("%I/%d: announcing (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
          krt_learn_announce_update(p, best);
-         n->n.flags |= KRF_INSTALLED;
        }
       else
        DBG("%I/%d: uptodate (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
     }
   FIB_ITERATE_END(f);
+
+  p->reload = 0;
 }
 
 static void
@@ -512,31 +527,31 @@ krt_learn_async(struct krt_proto *p, rte *e, int new)
   best = n->routes;
   bestp = &n->routes;
   for(gg=&n->routes; g=*gg; gg=&g->next)
+  {
     if (best->u.krt.metric > g->u.krt.metric)
       {
        best = g;
        bestp = gg;
       }
+
+    g->u.krt.best = 0;
+  }
+
   if (best)
     {
+      best->u.krt.best = 1;
       *bestp = best->next;
       best->next = n->routes;
       n->routes = best;
     }
+
   if (best != old_best)
     {
       DBG("krt_learn_async: distributing change\n");
       if (best)
-       {
-         krt_learn_announce_update(p, best);
-         n->n.flags |= KRF_INSTALLED;
-       }
+       krt_learn_announce_update(p, best);
       else
-       {
-         n->routes = NULL;
-         krt_learn_announce_delete(p, n);
-         n->n.flags &= ~KRF_INSTALLED;
-       }
+       krt_learn_announce_delete(p, n);
     }
 }
 
@@ -561,7 +576,7 @@ krt_dump(struct proto *P)
 static void
 krt_dump_attrs(rte *e)
 {
-  debug(" [m=%d,p=%d,t=%d]", e->u.krt.metric, e->u.krt.proto, e->u.krt.type);
+  debug(" [m=%d,p=%d]", e->u.krt.metric, e->u.krt.proto);
 }
 
 #endif
@@ -590,6 +605,48 @@ krt_flush_routes(struct krt_proto *p)
   FIB_WALK_END;
 }
 
+static struct rte *
+krt_export_net(struct krt_proto *p, net *net, rte **rt_free, ea_list **tmpa)
+{
+  struct announce_hook *ah = p->p.main_ahook;
+  struct filter *filter = ah->out_filter;
+  rte *rt;
+
+  if (p->p.accept_ra_types == RA_MERGED)
+    return rt_export_merged(ah, net, rt_free, tmpa, krt_filter_lp, 1);
+
+  rt = net->routes;
+  *rt_free = NULL;
+
+  if (!rte_is_valid(rt))
+    return NULL;
+
+  if (filter == FILTER_REJECT)
+    return NULL;
+
+  struct proto *src = rt->attrs->src->proto;
+  *tmpa = src->make_tmp_attrs ? src->make_tmp_attrs(rt, krt_filter_lp) : NULL;
+
+  /* We could run krt_import_control() here, but it is already handled by KRF_INSTALLED */
+
+  if (filter == FILTER_ACCEPT)
+    goto accept;
+
+  if (f_run(filter, &rt, tmpa, krt_filter_lp, FF_FORCE_TMPATTR | FF_SILENT) > F_ACCEPT)
+    goto reject;
+
+
+accept:
+  if (rt != net->routes)
+    *rt_free = rt;
+  return rt;
+
+reject:
+  if (rt != net->routes)
+    rte_free(rt);
+  return NULL;
+}
+
 static int
 krt_same_dest(rte *k, rte *e)
 {
@@ -618,7 +675,6 @@ krt_same_dest(rte *k, rte *e)
 void
 krt_got_route(struct krt_proto *p, rte *e)
 {
-  rte *old;
   net *net = e->net;
   int verdict;
 
@@ -661,15 +717,26 @@ krt_got_route(struct krt_proto *p, rte *e)
       goto sentenced;
     }
 
-  old = net->routes;
-  if ((net->n.flags & KRF_INSTALLED) && rte_is_valid(old))
+  if (net->n.flags & KRF_INSTALLED)
     {
-      /* There may be changes in route attributes, we ignore that.
-         Also, this does not work well if gw is changed in export filter */
-      if ((net->n.flags & KRF_SYNC_ERROR) || ! krt_same_dest(e, old))
+      rte *new, *rt_free;
+      ea_list *tmpa;
+
+      new = krt_export_net(p, net, &rt_free, &tmpa);
+
+      /* TODO: There also may be changes in route eattrs, we ignore that for now. */
+
+      if (!new)
+       verdict = KRF_DELETE;
+      else if ((net->n.flags & KRF_SYNC_ERROR) || !krt_same_dest(e, new))
        verdict = KRF_UPDATE;
       else
        verdict = KRF_SEEN;
+
+      if (rt_free)
+       rte_free(rt_free);
+
+      lp_flush(krt_filter_lp);
     }
   else
     verdict = KRF_DELETE;
@@ -690,25 +757,6 @@ krt_got_route(struct krt_proto *p, rte *e)
     rte_free(e);
 }
 
-static inline int
-krt_export_rte(struct krt_proto *p, rte **new, ea_list **tmpa)
-{
-  struct filter *filter = p->p.main_ahook->out_filter;
-
-  if (! *new)
-    return 0;
-
-  if (filter == FILTER_REJECT)
-    return 0;
-
-  if (filter == FILTER_ACCEPT)
-    return 1;
-
-  struct proto *src = (*new)->attrs->src->proto;
-  *tmpa = src->make_tmp_attrs ? src->make_tmp_attrs(*new, krt_filter_lp) : NULL;
-  return f_run(filter, new, tmpa, krt_filter_lp, FF_FORCE_TMPATTR) <= F_ACCEPT;
-}
-
 static void
 krt_prune(struct krt_proto *p)
 {
@@ -719,7 +767,7 @@ krt_prune(struct krt_proto *p)
     {
       net *n = (net *) f;
       int verdict = f->flags & KRF_VERDICT_MASK;
-      rte *new, *new0, *old;
+      rte *new, *old, *rt_free = NULL;
       ea_list *tmpa = NULL;
 
       if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
@@ -731,23 +779,18 @@ krt_prune(struct krt_proto *p)
       else
        old = NULL;
 
-      new = new0 = n->routes;
       if (verdict == KRF_CREATE || verdict == KRF_UPDATE)
        {
          /* We have to run export filter to get proper 'new' route */
-         if (! krt_export_rte(p, &new, &tmpa))
-           {
-             /* Route rejected, should not happen (KRF_INSTALLED) but to be sure .. */
-             verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
-           }
+         new = krt_export_net(p, n, &rt_free, &tmpa);
+
+         if (!new)
+           verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
          else
-           {
-             ea_list **x = &tmpa;
-             while (*x)
-               x = &((*x)->next);
-             *x = new ? new->attrs->eattrs : NULL;
-           }
+           tmpa = ea_append(tmpa, new->attrs->eattrs);
        }
+      else
+       new = NULL;
 
       switch (verdict)
        {
@@ -776,8 +819,8 @@ krt_prune(struct krt_proto *p)
 
       if (old)
        rte_free(old);
-      if (new != new0)
-       rte_free(new);
+      if (rt_free)
+       rte_free(rt_free);
       lp_flush(krt_filter_lp);
       f->flags &= ~KRF_VERDICT_MASK;
     }
@@ -866,7 +909,7 @@ krt_scan_timer_start(struct krt_proto *p)
 }
 
 static void
-krt_scan_timer_stop(struct krt_proto *p)
+krt_scan_timer_stop(struct krt_proto *p UNUSED)
 {
   krt_scan_count--;
 
@@ -955,13 +998,28 @@ krt_store_tmp_attrs(rte *rt, struct ea_list *attrs)
 }
 
 static int
-krt_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
+krt_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
 {
   struct krt_proto *p = (struct krt_proto *) P;
   rte *e = *new;
 
   if (e->attrs->src->proto == P)
+  {
+#ifdef CONFIG_SINGLE_ROUTE
+    /*
+     * Implicit withdraw - when the imported kernel route becomes the best one,
+     * we know that the previous one exported to the kernel was already removed,
+     * but if we processed the update as usual, we would send withdraw to the
+     * kernel, which would remove the new imported route instead.
+     *
+     * We will remove KRT_INSTALLED flag, which stops such withdraw to be
+     * processed in krt_rt_notify() and krt_replace_rte().
+     */
+    if (e == e->net->routes)
+      e->net->n.flags &= ~KRF_INSTALLED;
+#endif
     return -1;
+  }
 
   if (!KRT_CF->devroutes &&
       (e->attrs->dest == RTD_DEVICE) &&
@@ -1017,7 +1075,10 @@ krt_reload_routes(struct proto *P)
   /* Although we keep learned routes in krt_table, we rather schedule a scan */
 
   if (KRT_CF->learn)
+  {
+    p->reload = 1;
     krt_scan_timer_kick(p);
+  }
 
   return 1;
 }
@@ -1047,11 +1108,13 @@ krt_rte_same(rte *a, rte *b)
 struct krt_config *krt_cf;
 
 static struct proto *
-krt_init(struct proto_config *c)
+krt_init(struct proto_config *C)
 {
-  struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
+  struct krt_proto *p = proto_new(C, sizeof(struct krt_proto));
+  struct krt_config *c = (struct krt_config *) C;
 
-  p->p.accept_ra_types = RA_OPTIMAL;
+  p->p.accept_ra_types = c->merge_paths ? RA_MERGED : RA_OPTIMAL;
+  p->p.merge_limit = c->merge_paths;
   p->p.import_control = krt_import_control;
   p->p.rt_notify = krt_rt_notify;
   p->p.if_notify = krt_if_notify;
@@ -1076,7 +1139,11 @@ krt_start(struct proto *P)
   krt_learn_init(p);
 #endif
 
-  krt_sys_start(p);
+  if (!krt_sys_start(p))
+  {
+    rem_node(&p->krt_node);
+    return PS_START;
+  }
 
   krt_scan_timer_start(p);
 
@@ -1100,8 +1167,10 @@ krt_shutdown(struct proto *P)
   p->ready = 0;
   p->initialized = 0;
 
-  krt_sys_shutdown(p);
+  if (p->p.proto_state == PS_START)
+    return PS_DOWN;
 
+  krt_sys_shutdown(p);
   rem_node(&p->krt_node);
 
   return PS_DOWN;
@@ -1117,7 +1186,8 @@ krt_reconfigure(struct proto *p, struct proto_config *new)
     return 0;
 
   /* persist, graceful restart need not be the same */
-  return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes;
+  return o->scan_time == n->scan_time && o->learn == n->learn &&
+    o->devroutes == n->devroutes && o->merge_paths == n->merge_paths;
 }
 
 static void
@@ -1172,7 +1242,7 @@ krt_copy_config(struct proto_config *dest, struct proto_config *src)
 }
 
 static int
-krt_get_attr(eattr * a, byte * buf, int buflen UNUSED)
+krt_get_attr(eattr *a, byte *buf, int buflen)
 {
   switch (a->id)
   {
@@ -1184,16 +1254,8 @@ krt_get_attr(eattr * a, byte * buf, int buflen UNUSED)
     bsprintf(buf, "metric");
     return GA_NAME;
 
-  case EA_KRT_PREFSRC:
-    bsprintf(buf, "prefsrc");
-    return GA_NAME;
-
-  case EA_KRT_REALM:
-    bsprintf(buf, "realm");
-    return GA_NAME;
-
   default:
-    return GA_UNKNOWN;
+    return krt_sys_get_attr(a, buf, buflen);
   }
 }