1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- UNIX Kernel Synchronization
|
|
|
|
*
|
|
|
|
* (c) 1998--1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#include "unix.h"
|
|
|
|
#include "krt.h"
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
/*
|
|
|
|
* Global resources
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_io_init(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct protocol proto_unix_iface = {
|
|
|
|
name: "Device",
|
|
|
|
priority: 100,
|
|
|
|
init: kif_init,
|
|
|
|
start: kif_start,
|
|
|
|
shutdown: kif_shutdown,
|
|
|
|
};
|
1999-03-03 19:49:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Routes
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_flush_routes(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
struct rtable *t = &master_table;
|
|
|
|
|
|
|
|
DBG("Flushing kernel routes...\n");
|
|
|
|
while (t && t->tos)
|
|
|
|
t = t->sibling;
|
|
|
|
if (!t)
|
|
|
|
return;
|
|
|
|
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-03-03 20:55:35 +00:00
|
|
|
krt_set_notify(&p->p, e->net, NULL, e);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FIB_WALK_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Inbound/outbound route filtering? */
|
|
|
|
/* FIXME: Synchronization of multiple routing tables? */
|
|
|
|
|
|
|
|
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-03-04 18:36:18 +00:00
|
|
|
int src = e->u.krt_sync.src;
|
1999-03-03 19:49:56 +00:00
|
|
|
int verdict;
|
|
|
|
|
|
|
|
if (net->n.flags)
|
|
|
|
{
|
|
|
|
/* Route to this destination was already seen. Strange, but it happens... */
|
|
|
|
DBG("Already seen.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-03-04 19:00:31 +00:00
|
|
|
if (old = net->routes)
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
1999-03-04 19:00:31 +00:00
|
|
|
if (!krt_capable(old))
|
1999-03-29 20:14:00 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_AUTO_ROUTES
|
|
|
|
if (old->attrs->source == RTS_DEVICE)
|
|
|
|
verdict = KRF_SEEN;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
verdict = krt_capable(e) ? KRF_DELETE : KRF_SEEN;
|
|
|
|
}
|
1999-03-04 19:00:31 +00:00
|
|
|
else if (krt_uptodate(e, net->routes))
|
1999-03-03 19:49:56 +00:00
|
|
|
verdict = KRF_SEEN;
|
|
|
|
else
|
|
|
|
verdict = KRF_UPDATE;
|
|
|
|
}
|
1999-03-04 18:36:18 +00:00
|
|
|
else if (KRT_CF->learn && !net->routes && (src == KRT_SRC_ALIEN || src < 0))
|
1999-03-03 19:49:56 +00:00
|
|
|
verdict = KRF_LEARN;
|
|
|
|
else
|
|
|
|
verdict = KRF_DELETE;
|
|
|
|
|
|
|
|
DBG("krt_parse_entry: verdict=%s\n", ((char *[]) { "CREATE", "SEEN", "UPDATE", "DELETE", "LEARN" }) [verdict]);
|
|
|
|
|
|
|
|
net->n.flags = verdict;
|
|
|
|
if (verdict != KRF_SEEN)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
struct rtable *t = &master_table;
|
|
|
|
struct fib_node *f;
|
|
|
|
|
|
|
|
DBG("Pruning routes...\n");
|
|
|
|
while (t && t->tos)
|
|
|
|
t = t->sibling;
|
|
|
|
if (!t)
|
|
|
|
return;
|
|
|
|
FIB_WALK(&t->fib, f)
|
|
|
|
{
|
|
|
|
net *n = (net *) f;
|
|
|
|
int verdict = f->flags;
|
|
|
|
rte *new, *old;
|
|
|
|
|
|
|
|
if (verdict != KRF_CREATE && verdict != KRF_SEEN)
|
|
|
|
{
|
|
|
|
old = n->routes;
|
|
|
|
n->routes = old->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
old = NULL;
|
|
|
|
new = n->routes;
|
|
|
|
|
|
|
|
switch (verdict)
|
|
|
|
{
|
|
|
|
case KRF_CREATE:
|
|
|
|
if (new)
|
|
|
|
{
|
|
|
|
if (new->attrs->source == RTS_INHERIT)
|
|
|
|
{
|
|
|
|
DBG("krt_prune: removing inherited %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
rte_update(n, pp, NULL);
|
|
|
|
}
|
1999-03-04 19:00:31 +00:00
|
|
|
else if (krt_capable(new))
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
|
|
|
DBG("krt_prune: reinstalling %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
krt_set_notify(pp, n, new, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KRF_SEEN:
|
|
|
|
/* Nothing happens */
|
|
|
|
break;
|
|
|
|
case KRF_UPDATE:
|
|
|
|
DBG("krt_prune: updating %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
krt_set_notify(pp, n, new, old);
|
|
|
|
break;
|
|
|
|
case KRF_DELETE:
|
|
|
|
DBG("krt_prune: deleting %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
krt_set_notify(pp, n, NULL, old);
|
|
|
|
break;
|
|
|
|
case KRF_LEARN:
|
|
|
|
DBG("krt_prune: learning %I/%d\n", n->n.prefix, n->n.pxlen);
|
|
|
|
rte_update(n, pp, new);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bug("krt_prune: invalid route status");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
rte_free(old);
|
|
|
|
f->flags = 0;
|
|
|
|
}
|
|
|
|
FIB_WALK_END;
|
|
|
|
}
|
|
|
|
|
1999-03-04 18:36:18 +00:00
|
|
|
void
|
|
|
|
krt_got_route_async(struct krt_proto *p, rte *e, int new)
|
|
|
|
{
|
|
|
|
net *net = e->net;
|
|
|
|
rte *old = net->routes;
|
|
|
|
int src = e->u.krt_sync.src;
|
|
|
|
|
|
|
|
switch (src)
|
|
|
|
{
|
|
|
|
case KRT_SRC_BIRD:
|
|
|
|
ASSERT(0);
|
|
|
|
case KRT_SRC_REDIRECT:
|
|
|
|
DBG("It's a redirect, kill him! Kill! Kill!\n");
|
|
|
|
krt_set_notify(&p->p, net, NULL, e);
|
|
|
|
break;
|
|
|
|
default: /* Alien or unspecified */
|
|
|
|
if (KRT_CF->learn && new)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* FIXME: This is limited to one inherited route per destination as we
|
|
|
|
* use single protocol for all inherited routes. Probably leave it
|
|
|
|
* as-is (and document it :)), because the correct solution is to
|
1999-03-26 21:44:38 +00:00
|
|
|
* use multiple kernel tables anyway.
|
1999-03-04 18:36:18 +00:00
|
|
|
*/
|
|
|
|
DBG("Learning\n");
|
|
|
|
rte_update(net, &p->p, e);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBG("Discarding\n");
|
|
|
|
rte_update(net, &p->p, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* Periodic scanning
|
|
|
|
*/
|
|
|
|
|
|
|
|
static timer *krt_scan_timer;
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_scan(timer *t)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = t->data;
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
kif_force_scan();
|
|
|
|
DBG("KRT: It's route scan time...\n");
|
|
|
|
krt_scan_fire(p);
|
|
|
|
krt_prune(p);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Protocol glue
|
|
|
|
*/
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
struct proto_config *cf_krt;
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
static int
|
|
|
|
krt_start(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
|
|
|
krt_scan_start(p);
|
|
|
|
krt_set_start(p);
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
/* Start periodic routing table scanning */
|
1999-03-03 19:49:56 +00:00
|
|
|
krt_scan_timer = tm_new(P->pool);
|
|
|
|
krt_scan_timer->hook = krt_scan;
|
|
|
|
krt_scan_timer->data = p;
|
|
|
|
krt_scan_timer->recurrent = KRT_CF->scan_time;
|
|
|
|
krt_scan(krt_scan_timer);
|
|
|
|
tm_start(krt_scan_timer, KRT_CF->scan_time);
|
|
|
|
|
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
krt_shutdown(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
tm_stop(krt_scan_timer);
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
if (!KRT_CF->persist)
|
|
|
|
krt_flush_routes(p);
|
|
|
|
|
|
|
|
krt_set_shutdown(p);
|
|
|
|
krt_scan_shutdown(p);
|
|
|
|
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto *
|
|
|
|
krt_init(struct proto_config *c)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
|
|
|
|
|
|
|
|
p->p.rt_notify = krt_set_notify;
|
|
|
|
return &p->p;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct protocol proto_unix_kernel = {
|
|
|
|
name: "Kernel",
|
1999-03-26 21:44:38 +00:00
|
|
|
priority: 80,
|
1999-03-03 19:49:56 +00:00
|
|
|
init: krt_init,
|
|
|
|
start: krt_start,
|
|
|
|
shutdown: krt_shutdown,
|
|
|
|
};
|