1999-03-04 03:49:56 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- UNIX Kernel Synchronization
|
|
|
|
*
|
2000-01-17 00:44:50 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-03-04 03:49:56 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "lib/timer.h"
|
1999-08-04 03:33:22 +08:00
|
|
|
#include "conf/conf.h"
|
1999-03-04 03:49:56 +08:00
|
|
|
|
|
|
|
#include "unix.h"
|
|
|
|
#include "krt.h"
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
/*
|
|
|
|
* The whole kernel synchronization is a bit messy and touches some internals
|
|
|
|
* of the routing table engine, because routing table maintenance is a typical
|
|
|
|
* example of the proverbial compatibility between different Unices and we want
|
|
|
|
* to keep the overhead of our krt business as low as possible and avoid maintaining
|
|
|
|
* a local routing table copy.
|
|
|
|
*
|
|
|
|
* The kernel syncer can work in three different modes (according to system config header):
|
|
|
|
* o Single routing table, single krt protocol. [traditional Unix]
|
|
|
|
* o Many routing tables, separate krt protocols for all of them.
|
|
|
|
* o Many routing tables, but every scan includes all tables, so we start
|
|
|
|
* separate krt protocols which cooperate with each other. [Linux 2.2]
|
|
|
|
* In this case, we keep only a single scan timer.
|
|
|
|
*
|
|
|
|
* The hacky bits:
|
|
|
|
* o We use FIB node flags to keep track of route synchronization status.
|
|
|
|
* o When starting up, we cheat by looking if there is another kernel
|
|
|
|
* krt instance to be initialized later and performing table scan
|
|
|
|
* only once for all the instances.
|
|
|
|
* o We attach temporary rte's to routing tables.
|
|
|
|
*
|
|
|
|
* If you are brave enough, continue now. You cannot say you haven't been warned.
|
|
|
|
*/
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
static int krt_uptodate(rte *k, rte *e);
|
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
/*
|
|
|
|
* Global resources
|
|
|
|
*/
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
pool *krt_pool;
|
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
void
|
|
|
|
krt_io_init(void)
|
|
|
|
{
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_pool = rp_new(&root_pool, "Kernel Syncer");
|
1999-03-27 05:44:38 +08:00
|
|
|
krt_if_io_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interfaces
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct proto_config *cf_kif;
|
|
|
|
|
|
|
|
static struct kif_proto *kif_proto;
|
|
|
|
static timer *kif_scan_timer;
|
|
|
|
static bird_clock_t kif_last_shot;
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
static void
|
|
|
|
kif_preconfig(struct protocol *P, struct config *c)
|
|
|
|
{
|
|
|
|
cf_kif = NULL;
|
|
|
|
}
|
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
static void
|
|
|
|
kif_scan(timer *t)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = t->data;
|
|
|
|
|
|
|
|
DBG("KIF: It's interface scan time...\n");
|
|
|
|
kif_last_shot = now;
|
|
|
|
krt_if_scan(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kif_force_scan(void)
|
|
|
|
{
|
|
|
|
if (kif_proto && kif_last_shot + 2 < now)
|
|
|
|
{
|
|
|
|
kif_scan(kif_scan_timer);
|
|
|
|
tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto *
|
|
|
|
kif_init(struct proto_config *c)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = proto_new(c, sizeof(struct kif_proto));
|
|
|
|
return &p->p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kif_start(struct proto *P)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = (struct kif_proto *) P;
|
|
|
|
|
|
|
|
kif_proto = p;
|
|
|
|
krt_if_start(p);
|
|
|
|
|
|
|
|
/* Start periodic interface scanning */
|
|
|
|
kif_scan_timer = tm_new(P->pool);
|
|
|
|
kif_scan_timer->hook = kif_scan;
|
|
|
|
kif_scan_timer->data = p;
|
|
|
|
kif_scan_timer->recurrent = KIF_CF->scan_time;
|
|
|
|
kif_scan(kif_scan_timer);
|
|
|
|
tm_start(kif_scan_timer, KIF_CF->scan_time);
|
|
|
|
|
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kif_shutdown(struct proto *P)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = (struct kif_proto *) P;
|
|
|
|
|
|
|
|
tm_stop(kif_scan_timer);
|
|
|
|
krt_if_shutdown(p);
|
|
|
|
kif_proto = NULL;
|
|
|
|
|
|
|
|
if_start_update(); /* Remove all interfaces */
|
|
|
|
if_end_update();
|
1999-08-04 03:33:22 +08:00
|
|
|
/*
|
|
|
|
* FIXME: Is it really a good idea? It causes routes to be flushed,
|
|
|
|
* but at the same time it avoids sending of these deletions to the kernel,
|
|
|
|
* because krt thinks the kernel itself has already removed the route
|
|
|
|
* when downing the interface. Sad.
|
|
|
|
*/
|
1999-03-27 05:44:38 +08:00
|
|
|
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
2000-01-17 19:17:33 +08:00
|
|
|
static int
|
|
|
|
kif_reconfigure(struct proto *p, struct proto_config *new)
|
|
|
|
{
|
|
|
|
struct kif_config *o = (struct kif_config *) p->cf;
|
|
|
|
struct kif_config *n = (struct kif_config *) new;
|
|
|
|
|
|
|
|
if (!kif_params_same(&o->iface, &n->iface))
|
|
|
|
return 0;
|
|
|
|
if (o->scan_time != n->scan_time)
|
|
|
|
{
|
|
|
|
tm_stop(kif_scan_timer);
|
|
|
|
kif_scan_timer->recurrent = n->scan_time;
|
|
|
|
kif_scan(kif_scan_timer);
|
|
|
|
tm_start(kif_scan_timer, n->scan_time);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
struct protocol proto_unix_iface = {
|
|
|
|
name: "Device",
|
2000-01-17 19:52:50 +08:00
|
|
|
template: "device%d",
|
1999-03-27 05:44:38 +08:00
|
|
|
priority: 100,
|
2000-01-17 00:44:50 +08:00
|
|
|
preconfig: kif_preconfig,
|
1999-03-27 05:44:38 +08:00
|
|
|
init: kif_init,
|
|
|
|
start: kif_start,
|
|
|
|
shutdown: kif_shutdown,
|
2000-01-17 19:17:33 +08:00
|
|
|
reconfigure: kif_reconfigure,
|
1999-03-27 05:44:38 +08:00
|
|
|
};
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
/*
|
|
|
|
* Inherited Routes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
krt_same_key(rte *a, rte *b)
|
|
|
|
{
|
|
|
|
return a->u.krt.proto == b->u.krt.proto &&
|
|
|
|
a->u.krt.metric == b->u.krt.metric &&
|
|
|
|
a->u.krt.type == b->u.krt.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_announce_update(struct krt_proto *p, rte *e)
|
|
|
|
{
|
|
|
|
net *n = e->net;
|
|
|
|
rta *aa = rta_clone(e->attrs);
|
|
|
|
rte *ee = rte_get_temp(aa);
|
1999-04-13 02:01:07 +08:00
|
|
|
net *nn = net_get(p->p.table, n->n.prefix, n->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
ee->net = nn;
|
|
|
|
ee->pflags = 0;
|
|
|
|
ee->u.krt = e->u.krt;
|
1999-05-18 04:16:53 +08:00
|
|
|
rte_update(p->p.table, nn, &p->p, ee);
|
1999-04-03 21:05:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_announce_delete(struct krt_proto *p, net *n)
|
|
|
|
{
|
1999-04-13 02:01:07 +08:00
|
|
|
n = net_find(p->p.table, n->n.prefix, n->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
if (n)
|
1999-05-18 04:16:53 +08:00
|
|
|
rte_update(p->p.table, n, &p->p, NULL);
|
1999-04-03 21:05:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_scan(struct krt_proto *p, rte *e)
|
|
|
|
{
|
|
|
|
net *n0 = e->net;
|
1999-04-13 02:01:07 +08:00
|
|
|
net *n = net_get(&p->krt_table, n0->n.prefix, n0->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
rte *m, **mm;
|
|
|
|
|
|
|
|
e->attrs->source = RTS_INHERIT;
|
|
|
|
|
|
|
|
for(mm=&n->routes; m = *mm; mm=&m->next)
|
|
|
|
if (krt_same_key(m, e))
|
|
|
|
break;
|
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
if (krt_uptodate(m, e))
|
|
|
|
{
|
|
|
|
DBG("krt_learn_scan: SEEN\n");
|
|
|
|
rte_free(e);
|
|
|
|
m->u.krt.seen = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBG("krt_learn_scan: OVERRIDE\n");
|
|
|
|
*mm = m->next;
|
|
|
|
rte_free(m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DBG("krt_learn_scan: CREATE\n");
|
|
|
|
if (!m)
|
|
|
|
{
|
|
|
|
e->attrs = rta_lookup(e->attrs);
|
|
|
|
e->next = n->routes;
|
|
|
|
n->routes = e;
|
|
|
|
e->u.krt.seen = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_prune(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
struct fib *fib = &p->krt_table.fib;
|
|
|
|
struct fib_iterator fit;
|
|
|
|
|
|
|
|
DBG("Pruning inheritance data...\n");
|
|
|
|
|
|
|
|
FIB_ITERATE_INIT(&fit, fib);
|
|
|
|
again:
|
|
|
|
FIB_ITERATE_START(fib, &fit, f)
|
|
|
|
{
|
|
|
|
net *n = (net *) f;
|
|
|
|
rte *e, **ee, *best, **pbest, *old_best;
|
|
|
|
|
|
|
|
old_best = n->routes;
|
|
|
|
best = NULL;
|
|
|
|
pbest = NULL;
|
|
|
|
ee = &n->routes;
|
|
|
|
while (e = *ee)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
FIB_ITERATE_PUT(&fit, f);
|
|
|
|
fib_delete(fib, f);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
*pbest = best->next;
|
|
|
|
best->next = n->routes;
|
|
|
|
n->routes = best;
|
|
|
|
if (best != old_best || !(n->n.flags & KRF_INSTALLED))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_async(struct krt_proto *p, rte *e, int new)
|
|
|
|
{
|
|
|
|
net *n0 = e->net;
|
1999-04-13 02:01:07 +08:00
|
|
|
net *n = net_get(&p->krt_table, n0->n.prefix, n0->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
rte *g, **gg, *best, **bestp, *old_best;
|
|
|
|
|
|
|
|
e->attrs->source = RTS_INHERIT;
|
|
|
|
|
|
|
|
old_best = n->routes;
|
|
|
|
for(gg=&n->routes; g = *gg; gg = &g->next)
|
|
|
|
if (krt_same_key(g, e))
|
|
|
|
break;
|
|
|
|
if (new)
|
|
|
|
{
|
|
|
|
if (g)
|
|
|
|
{
|
|
|
|
if (krt_uptodate(g, e))
|
|
|
|
{
|
|
|
|
DBG("krt_learn_async: same\n");
|
|
|
|
rte_free(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("krt_learn_async: update\n");
|
|
|
|
*gg = g->next;
|
|
|
|
rte_free(g);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DBG("krt_learn_async: create\n");
|
|
|
|
e->attrs = rta_lookup(e->attrs);
|
|
|
|
e->next = n->routes;
|
|
|
|
n->routes = e;
|
|
|
|
}
|
|
|
|
else if (!g)
|
|
|
|
{
|
|
|
|
DBG("krt_learn_async: not found\n");
|
|
|
|
rte_free(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBG("krt_learn_async: delete\n");
|
|
|
|
*gg = g->next;
|
|
|
|
rte_free(e);
|
|
|
|
rte_free(g);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (best)
|
|
|
|
{
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n->routes = NULL;
|
|
|
|
krt_learn_announce_delete(p, n);
|
|
|
|
n->n.flags &= ~KRF_INSTALLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_init(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
if (KRT_CF->learn)
|
|
|
|
rt_setup(p->p.pool, &p->krt_table, "Inherited");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_dump(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
|
|
|
if (!KRT_CF->learn)
|
|
|
|
return;
|
|
|
|
debug("KRT: Table of inheritable routes\n");
|
|
|
|
rt_dump(&p->krt_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
/*
|
|
|
|
* Routes
|
|
|
|
*/
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
static timer *krt_scan_timer;
|
|
|
|
static int krt_instance_count;
|
|
|
|
static list krt_instance_list;
|
|
|
|
#endif
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
static void
|
|
|
|
krt_flush_routes(struct krt_proto *p)
|
|
|
|
{
|
1999-05-18 04:16:53 +08:00
|
|
|
struct rtable *t = p->p.table;
|
1999-03-04 03:49:56 +08:00
|
|
|
|
|
|
|
DBG("Flushing kernel routes...\n");
|
|
|
|
FIB_WALK(&t->fib, f)
|
|
|
|
{
|
|
|
|
net *n = (net *) f;
|
|
|
|
rte *e = n->routes;
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
rta *a = e->attrs;
|
|
|
|
if (a->source != RTS_DEVICE && a->source != RTS_INHERIT)
|
1999-04-03 21:05:18 +08:00
|
|
|
krt_set_notify(p, e->net, NULL, e);
|
1999-03-04 03:49:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FIB_WALK_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
krt_uptodate(rte *k, rte *e)
|
|
|
|
{
|
|
|
|
rta *ka = k->attrs, *ea = e->attrs;
|
|
|
|
|
|
|
|
if (ka->dest != ea->dest)
|
|
|
|
return 0;
|
|
|
|
switch (ka->dest)
|
|
|
|
{
|
|
|
|
case RTD_ROUTER:
|
|
|
|
return ipa_equal(ka->gw, ea->gw);
|
|
|
|
case RTD_DEVICE:
|
|
|
|
return !strcmp(ka->iface->name, ea->iface->name);
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This gets called back when the low-level scanning code discovers a route.
|
|
|
|
* We expect that the route is a temporary rte and its attributes are uncached.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_got_route(struct krt_proto *p, rte *e)
|
|
|
|
{
|
|
|
|
rte *old;
|
|
|
|
net *net = e->net;
|
1999-04-03 21:05:18 +08:00
|
|
|
int src = e->u.krt.src;
|
1999-03-04 03:49:56 +08:00
|
|
|
int verdict;
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
#ifdef CONFIG_AUTO_ROUTES
|
|
|
|
if (e->attrs->dest == RTD_DEVICE)
|
|
|
|
{
|
|
|
|
/* It's a device route. Probably a kernel-generated one. */
|
|
|
|
verdict = KRF_IGNORE;
|
|
|
|
goto sentenced;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
if (src == KRT_SRC_ALIEN)
|
|
|
|
{
|
|
|
|
if (KRT_CF->learn)
|
|
|
|
krt_learn_scan(p, e);
|
|
|
|
else
|
|
|
|
DBG("krt_parse_entry: Alien route, ignoring\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (net->n.flags & KRF_VERDICT_MASK)
|
1999-03-04 03:49:56 +08:00
|
|
|
{
|
|
|
|
/* Route to this destination was already seen. Strange, but it happens... */
|
|
|
|
DBG("Already seen.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
if (net->n.flags & KRF_INSTALLED)
|
1999-03-04 03:49:56 +08:00
|
|
|
{
|
1999-04-03 21:05:18 +08:00
|
|
|
old = net->routes;
|
|
|
|
ASSERT(old);
|
|
|
|
if (krt_uptodate(e, old))
|
1999-03-04 03:49:56 +08:00
|
|
|
verdict = KRF_SEEN;
|
|
|
|
else
|
|
|
|
verdict = KRF_UPDATE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
verdict = KRF_DELETE;
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
sentenced:
|
|
|
|
DBG("krt_parse_entry: verdict=%s\n", ((char *[]) { "CREATE", "SEEN", "UPDATE", "DELETE", "IGNORE" }) [verdict]);
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
|
|
|
|
if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
|
1999-03-04 03:49:56 +08:00
|
|
|
{
|
|
|
|
/* Get a cached copy of attributes and link the route */
|
|
|
|
rta *a = e->attrs;
|
|
|
|
a->source = RTS_DUMMY;
|
|
|
|
e->attrs = rta_lookup(a);
|
|
|
|
e->next = net->routes;
|
|
|
|
net->routes = e;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rte_free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_prune(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
struct proto *pp = &p->p;
|
1999-05-18 04:16:53 +08:00
|
|
|
struct rtable *t = p->p.table;
|
1999-03-04 03:49:56 +08:00
|
|
|
struct fib_node *f;
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
DBG("Pruning routes in table %s...\n", t->name);
|
1999-03-04 03:49:56 +08:00
|
|
|
FIB_WALK(&t->fib, f)
|
|
|
|
{
|
|
|
|
net *n = (net *) f;
|
1999-04-03 21:05:18 +08:00
|
|
|
int verdict = f->flags & KRF_VERDICT_MASK;
|
1999-03-04 03:49:56 +08:00
|
|
|
rte *new, *old;
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
if (verdict != KRF_CREATE && verdict != KRF_SEEN && verdict != KRF_IGNORE)
|
1999-03-04 03:49:56 +08:00
|
|
|
{
|
|
|
|
old = n->routes;
|
|
|
|
n->routes = old->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
old = NULL;
|
|
|
|
new = n->routes;
|
|
|
|
|
|
|
|
switch (verdict)
|
|
|
|
{
|
|
|
|
case KRF_CREATE:
|
1999-04-03 21:05:18 +08:00
|
|
|
if (new && (f->flags & KRF_INSTALLED))
|
1999-03-04 03:49:56 +08:00
|
|
|
{
|
1999-04-03 21:05:18 +08:00
|
|
|
DBG("krt_prune: reinstalling %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
krt_set_notify(p, n, new, NULL);
|
1999-03-04 03:49:56 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KRF_SEEN:
|
1999-04-03 21:05:18 +08:00
|
|
|
case KRF_IGNORE:
|
1999-03-04 03:49:56 +08:00
|
|
|
/* Nothing happens */
|
|
|
|
break;
|
|
|
|
case KRF_UPDATE:
|
|
|
|
DBG("krt_prune: updating %I/%d\n", n->n.prefix, n->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
krt_set_notify(p, n, new, old);
|
1999-03-04 03:49:56 +08:00
|
|
|
break;
|
|
|
|
case KRF_DELETE:
|
|
|
|
DBG("krt_prune: deleting %I/%d\n", n->n.prefix, n->n.pxlen);
|
1999-04-03 21:05:18 +08:00
|
|
|
krt_set_notify(p, n, NULL, old);
|
1999-03-04 03:49:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bug("krt_prune: invalid route status");
|
|
|
|
}
|
|
|
|
if (old)
|
|
|
|
rte_free(old);
|
1999-04-03 21:05:18 +08:00
|
|
|
f->flags &= ~KRF_VERDICT_MASK;
|
1999-03-04 03:49:56 +08:00
|
|
|
}
|
|
|
|
FIB_WALK_END;
|
1999-04-03 21:05:18 +08:00
|
|
|
|
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
if (KRT_CF->learn)
|
|
|
|
krt_learn_prune(p);
|
|
|
|
#endif
|
2000-01-18 18:39:30 +08:00
|
|
|
p->initialized = 1;
|
1999-03-04 03:49:56 +08:00
|
|
|
}
|
|
|
|
|
1999-03-05 02:36:18 +08:00
|
|
|
void
|
|
|
|
krt_got_route_async(struct krt_proto *p, rte *e, int new)
|
|
|
|
{
|
|
|
|
net *net = e->net;
|
|
|
|
rte *old = net->routes;
|
1999-04-03 21:05:18 +08:00
|
|
|
int src = e->u.krt.src;
|
1999-03-05 02:36:18 +08:00
|
|
|
|
|
|
|
switch (src)
|
|
|
|
{
|
|
|
|
case KRT_SRC_BIRD:
|
|
|
|
ASSERT(0);
|
|
|
|
case KRT_SRC_REDIRECT:
|
|
|
|
DBG("It's a redirect, kill him! Kill! Kill!\n");
|
1999-04-03 21:05:18 +08:00
|
|
|
krt_set_notify(p, net, NULL, e);
|
1999-03-05 02:36:18 +08:00
|
|
|
break;
|
1999-04-03 21:05:18 +08:00
|
|
|
case KRT_SRC_ALIEN:
|
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
if (KRT_CF->learn)
|
1999-03-05 02:36:18 +08:00
|
|
|
{
|
1999-04-03 21:05:18 +08:00
|
|
|
krt_learn_async(p, e, new);
|
|
|
|
return;
|
1999-03-05 02:36:18 +08:00
|
|
|
}
|
1999-04-03 21:05:18 +08:00
|
|
|
#endif
|
|
|
|
/* Fall-thru */
|
|
|
|
default:
|
|
|
|
DBG("Discarding\n");
|
1999-05-18 04:16:53 +08:00
|
|
|
rte_update(p->p.table, net, &p->p, NULL);
|
1999-03-05 02:36:18 +08:00
|
|
|
}
|
1999-04-03 21:05:18 +08:00
|
|
|
rte_free(e);
|
1999-03-05 02:36:18 +08:00
|
|
|
}
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
/*
|
|
|
|
* Periodic scanning
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_scan(timer *t)
|
|
|
|
{
|
1999-08-04 03:33:22 +08:00
|
|
|
struct krt_proto *p;
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
kif_force_scan();
|
1999-08-04 03:33:22 +08:00
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
{
|
|
|
|
void *q;
|
|
|
|
DBG("KRT: It's route scan time...\n");
|
|
|
|
krt_scan_fire(NULL);
|
|
|
|
WALK_LIST(q, krt_instance_list)
|
|
|
|
{
|
|
|
|
p = SKIP_BACK(struct krt_proto, instance_node, q);
|
|
|
|
krt_prune(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
p = t->data;
|
|
|
|
DBG("KRT: It's route scan time for %s...\n", p->p.name);
|
1999-03-27 05:44:38 +08:00
|
|
|
krt_scan_fire(p);
|
|
|
|
krt_prune(p);
|
1999-08-04 03:33:22 +08:00
|
|
|
#endif
|
1999-03-04 03:49:56 +08:00
|
|
|
}
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
/*
|
|
|
|
* Updates
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
1999-06-01 02:55:35 +08:00
|
|
|
krt_notify(struct proto *P, net *net, rte *new, rte *old, struct ea_list *tmpa)
|
1999-04-03 21:05:18 +08:00
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
|
|
|
if (new && (!krt_capable(new) || new->attrs->source == RTS_INHERIT))
|
|
|
|
new = NULL;
|
|
|
|
if (!(net->n.flags & KRF_INSTALLED))
|
|
|
|
old = NULL;
|
|
|
|
if (new)
|
|
|
|
net->n.flags |= KRF_INSTALLED;
|
|
|
|
else
|
|
|
|
net->n.flags &= ~KRF_INSTALLED;
|
2000-01-18 18:39:30 +08:00
|
|
|
if (p->initialized) /* Before first scan we don't touch the routes */
|
|
|
|
krt_set_notify(p, net, new, old);
|
1999-04-03 21:05:18 +08:00
|
|
|
}
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
/*
|
|
|
|
* Protocol glue
|
|
|
|
*/
|
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
struct proto_config *cf_krt;
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
static void
|
|
|
|
krt_preconfig(struct protocol *P, struct config *c)
|
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
cf_krt = NULL;
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_scan_preconfig(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_postconfig(struct proto_config *C)
|
|
|
|
{
|
|
|
|
struct krt_config *c = (struct krt_config *) C;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
struct krt_config *first = (struct krt_config *) cf_krt;
|
|
|
|
if (first->scan_time != c->scan_time)
|
|
|
|
cf_error("All kernel syncers must use the same table scan interval");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (C->table->krt_attached)
|
|
|
|
cf_error("Kernel syncer (%s) already attached to table %s", C->table->krt_attached->name, C->table->name);
|
|
|
|
C->table->krt_attached = C;
|
|
|
|
krt_scan_postconfig(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static timer *
|
|
|
|
krt_start_timer(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
timer *t;
|
|
|
|
|
|
|
|
t = tm_new(p->krt_pool);
|
|
|
|
t->hook = krt_scan;
|
|
|
|
t->data = p;
|
|
|
|
t->recurrent = KRT_CF->scan_time;
|
2000-01-18 18:39:30 +08:00
|
|
|
tm_start(t, 0);
|
1999-08-04 03:33:22 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
static int
|
|
|
|
krt_start(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
1999-08-04 03:33:22 +08:00
|
|
|
int first = 1;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
if (!krt_instance_count++)
|
|
|
|
init_list(&krt_instance_list);
|
|
|
|
else
|
|
|
|
first = 0;
|
|
|
|
p->krt_pool = krt_pool;
|
|
|
|
add_tail(&krt_instance_list, &p->instance_node);
|
|
|
|
#else
|
|
|
|
p->krt_pool = P->pool;
|
|
|
|
#endif
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
krt_learn_init(p);
|
|
|
|
#endif
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_scan_start(p, first);
|
|
|
|
krt_set_start(p, first);
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-03-27 05:44:38 +08:00
|
|
|
/* Start periodic routing table scanning */
|
1999-08-04 03:33:22 +08:00
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
if (first)
|
|
|
|
krt_scan_timer = krt_start_timer(p);
|
2000-01-18 18:39:30 +08:00
|
|
|
else
|
|
|
|
tm_start(p->scan_timer, 0);
|
1999-08-04 03:33:22 +08:00
|
|
|
p->scan_timer = krt_scan_timer;
|
|
|
|
#else
|
|
|
|
p->scan_timer = krt_start_timer(p);
|
|
|
|
#endif
|
1999-03-04 03:49:56 +08:00
|
|
|
|
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
static int
|
1999-03-04 03:49:56 +08:00
|
|
|
krt_shutdown(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
1999-08-04 03:33:22 +08:00
|
|
|
int last = 1;
|
1999-03-04 03:49:56 +08:00
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
rem_node(&p->instance_node);
|
|
|
|
if (--krt_instance_count)
|
|
|
|
last = 0;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
tm_stop(p->scan_timer);
|
1999-03-27 05:44:38 +08:00
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
if (!KRT_CF->persist)
|
|
|
|
krt_flush_routes(p);
|
|
|
|
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_set_shutdown(p, last);
|
|
|
|
krt_scan_shutdown(p, last);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ALL_TABLES_AT_ONCE
|
|
|
|
if (last)
|
|
|
|
rfree(krt_scan_timer);
|
|
|
|
#endif
|
1999-03-04 03:49:56 +08:00
|
|
|
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto *
|
|
|
|
krt_init(struct proto_config *c)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
|
|
|
|
|
1999-04-03 21:05:18 +08:00
|
|
|
p->p.rt_notify = krt_notify;
|
1999-12-08 22:16:13 +08:00
|
|
|
p->p.min_scope = SCOPE_HOST;
|
1999-03-04 03:49:56 +08:00
|
|
|
return &p->p;
|
|
|
|
}
|
|
|
|
|
2000-01-18 18:39:30 +08:00
|
|
|
static int
|
|
|
|
krt_reconfigure(struct proto *p, struct proto_config *new)
|
|
|
|
{
|
|
|
|
struct krt_config *o = (struct krt_config *) p->cf;
|
|
|
|
struct krt_config *n = (struct krt_config *) new;
|
|
|
|
|
|
|
|
return o->scan_time == n->scan_time
|
|
|
|
&& o->learn == n->learn /* persist needn't be the same */
|
|
|
|
&& krt_set_params_same(&o->set, &n->set)
|
|
|
|
&& krt_scan_params_same(&o->scan, &n->scan)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
struct protocol proto_unix_kernel = {
|
|
|
|
name: "Kernel",
|
2000-01-17 19:52:50 +08:00
|
|
|
template: "kernel%d",
|
1999-03-27 05:44:38 +08:00
|
|
|
priority: 80,
|
1999-08-04 03:33:22 +08:00
|
|
|
preconfig: krt_preconfig,
|
|
|
|
postconfig: krt_postconfig,
|
1999-03-04 03:49:56 +08:00
|
|
|
init: krt_init,
|
|
|
|
start: krt_start,
|
|
|
|
shutdown: krt_shutdown,
|
2000-01-18 18:39:30 +08:00
|
|
|
reconfigure: krt_reconfigure,
|
1999-04-03 21:05:18 +08:00
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
dump: krt_dump,
|
|
|
|
dump_attrs: krt_dump_attrs,
|
|
|
|
#endif
|
1999-03-04 03:49:56 +08:00
|
|
|
};
|