1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- UNIX Kernel Synchronization
|
|
|
|
*
|
2000-01-16 16:44:50 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-03-03 19:49:56 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* DOC: Kernel synchronization
|
|
|
|
*
|
|
|
|
* This system dependent module implements the Kernel and Device protocol,
|
|
|
|
* that is synchronization of interface lists and routing tables with the
|
|
|
|
* OS kernel.
|
|
|
|
*
|
|
|
|
* 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
|
2000-06-07 12:29:08 +00:00
|
|
|
* to keep the overhead of our KRT business as low as possible and avoid maintaining
|
2000-06-05 12:49:04 +00:00
|
|
|
* a local routing table copy.
|
|
|
|
*
|
|
|
|
* The kernel syncer can work in three different modes (according to system config header):
|
2000-06-08 12:37:21 +00:00
|
|
|
* Either with a single routing table and single KRT protocol [traditional UNIX]
|
2000-06-07 12:29:08 +00:00
|
|
|
* or with many routing tables and separate KRT protocols for all of them
|
2000-06-05 12:49:04 +00:00
|
|
|
* or with many routing tables, but every scan including all tables, so we start
|
2012-04-30 20:21:52 +00:00
|
|
|
* separate KRT protocols which cooperate with each other [Linux].
|
2000-06-05 12:49:04 +00:00
|
|
|
* In this case, we keep only a single scan timer.
|
|
|
|
*
|
2000-06-07 13:25:53 +00:00
|
|
|
* We use FIB node flags in the routing table to keep track of route
|
|
|
|
* synchronization status. We also attach temporary &rte's to the routing table,
|
|
|
|
* but it cannot do any harm to the rest of BIRD since table synchronization is
|
|
|
|
* an atomic process.
|
2000-06-05 12:49:04 +00:00
|
|
|
*
|
|
|
|
* When starting up, we cheat by looking if there is another
|
|
|
|
* KRT instance to be initialized later and performing table scan
|
2000-06-08 12:37:21 +00:00
|
|
|
* only once for all the instances.
|
2012-04-30 20:21:52 +00:00
|
|
|
*
|
|
|
|
* The code uses OS-dependent parts for kernel updates and scans. These parts are
|
2014-10-02 09:05:55 +00:00
|
|
|
* in more specific sysdep directories (e.g. sysdep/linux) in functions krt_sys_*
|
2012-04-30 20:34:06 +00:00
|
|
|
* and kif_sys_* (and some others like krt_replace_rte()) and krt-sys.h header file.
|
2012-04-30 20:21:52 +00:00
|
|
|
* This is also used for platform specific protocol options and route attributes.
|
|
|
|
*
|
|
|
|
* There was also an old code that used traditional UNIX ioctls for these tasks.
|
|
|
|
* It was unmaintained and later removed. For reference, see sysdep/krt-* files
|
|
|
|
* in commit 396dfa9042305f62da1f56589c4b98fac57fc2f6
|
2000-06-08 12:37:21 +00:00
|
|
|
*/
|
2000-06-05 12:49:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If you are brave enough, continue now. You cannot say you haven't been warned.
|
|
|
|
*/
|
|
|
|
|
2000-03-12 21:54:39 +00:00
|
|
|
#undef LOCAL_DEBUG
|
1999-03-03 19:49:56 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
2022-03-31 17:09:38 +00:00
|
|
|
#include "nest/rt.h"
|
1999-03-03 19:49:56 +00:00
|
|
|
#include "nest/protocol.h"
|
2012-03-25 17:44:14 +00:00
|
|
|
#include "filter/filter.h"
|
1999-08-03 19:33:22 +00:00
|
|
|
#include "conf/conf.h"
|
2002-11-13 08:47:06 +00:00
|
|
|
#include "lib/string.h"
|
2017-11-28 16:43:20 +00:00
|
|
|
#include "lib/timer.h"
|
1999-03-03 19:49:56 +00:00
|
|
|
|
|
|
|
#include "unix.h"
|
|
|
|
#include "krt.h"
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
/*
|
|
|
|
* Global resources
|
|
|
|
*/
|
|
|
|
|
1999-08-03 19:33:22 +00:00
|
|
|
pool *krt_pool;
|
2012-03-25 17:44:14 +00:00
|
|
|
static linpool *krt_filter_lp;
|
2013-06-29 20:55:41 +00:00
|
|
|
static list krt_proto_list;
|
1999-08-03 19:33:22 +00:00
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
void
|
|
|
|
krt_io_init(void)
|
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
krt_pool = rp_new(&root_pool, the_bird_domain.the_bird, "Kernel Syncer");
|
2017-05-16 12:31:16 +00:00
|
|
|
krt_filter_lp = lp_new_default(krt_pool);
|
2013-06-29 20:55:41 +00:00
|
|
|
init_list(&krt_proto_list);
|
2015-11-03 13:42:41 +00:00
|
|
|
krt_sys_io_init();
|
1999-03-26 21:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interfaces
|
|
|
|
*/
|
|
|
|
|
2015-11-23 10:13:40 +00:00
|
|
|
struct kif_proto *kif_proto;
|
2012-04-30 13:31:32 +00:00
|
|
|
static struct kif_config *kif_cf;
|
1999-03-26 21:44:38 +00:00
|
|
|
static timer *kif_scan_timer;
|
2017-06-14 11:15:35 +00:00
|
|
|
static btime kif_last_shot;
|
1999-03-26 21:44:38 +00:00
|
|
|
|
2017-12-07 12:06:01 +00:00
|
|
|
static struct kif_iface_config kif_default_iface = {};
|
|
|
|
|
|
|
|
struct kif_iface_config *
|
|
|
|
kif_get_iface_config(struct iface *iface)
|
|
|
|
{
|
|
|
|
struct kif_config *cf = (void *) (kif_proto->p.cf);
|
|
|
|
struct kif_iface_config *ic = (void *) iface_patt_find(&cf->iface_list, iface, NULL);
|
|
|
|
return ic ?: &kif_default_iface;
|
|
|
|
}
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
static void
|
|
|
|
kif_scan(timer *t)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = t->data;
|
|
|
|
|
2000-03-12 21:54:39 +00:00
|
|
|
KRT_TRACE(p, D_EVENTS, "Scanning interfaces");
|
2017-06-14 11:15:35 +00:00
|
|
|
kif_last_shot = current_time();
|
2012-04-30 13:31:32 +00:00
|
|
|
kif_do_scan(p);
|
1999-03-26 21:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kif_force_scan(void)
|
|
|
|
{
|
2017-06-14 11:15:35 +00:00
|
|
|
if (kif_proto && ((kif_last_shot + 2 S) < current_time()))
|
1999-03-26 21:44:38 +00:00
|
|
|
{
|
|
|
|
kif_scan(kif_scan_timer);
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
|
1999-03-26 21:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 02:15:12 +00:00
|
|
|
void
|
|
|
|
kif_request_scan(void)
|
|
|
|
{
|
2017-06-14 11:15:35 +00:00
|
|
|
if (kif_proto && (kif_scan_timer->expires > (current_time() + 1 S)))
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_start(kif_scan_timer, 1 S);
|
2012-01-23 02:15:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
static struct proto *
|
|
|
|
kif_init(struct proto_config *c)
|
|
|
|
{
|
2016-01-26 10:48:58 +00:00
|
|
|
struct kif_proto *p = proto_new(c);
|
2012-04-30 13:31:32 +00:00
|
|
|
|
|
|
|
kif_sys_init(p);
|
|
|
|
return &p->p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kif_start(struct proto *P)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = (struct kif_proto *) P;
|
|
|
|
|
|
|
|
kif_proto = p;
|
|
|
|
kif_sys_start(p);
|
|
|
|
|
|
|
|
/* Start periodic interface scanning */
|
2017-11-28 16:43:20 +00:00
|
|
|
kif_scan_timer = tm_new_init(P->pool, kif_scan, p, KIF_CF->scan_time, 0);
|
2012-04-30 13:31:32 +00:00
|
|
|
kif_scan(kif_scan_timer);
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_start(kif_scan_timer, KIF_CF->scan_time);
|
2012-04-30 13:31:32 +00:00
|
|
|
|
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kif_shutdown(struct proto *P)
|
|
|
|
{
|
|
|
|
struct kif_proto *p = (struct kif_proto *) P;
|
|
|
|
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_stop(kif_scan_timer);
|
2012-04-30 13:31:32 +00:00
|
|
|
kif_sys_shutdown(p);
|
|
|
|
kif_proto = NULL;
|
|
|
|
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:11:55 +00:00
|
|
|
static void
|
|
|
|
kif_cleanup(struct proto *p)
|
|
|
|
{
|
|
|
|
if (p->debug & D_EVENTS)
|
|
|
|
log(L_TRACE "%s: Flushing interfaces", p->name);
|
|
|
|
if_start_update();
|
|
|
|
if_end_update();
|
|
|
|
}
|
|
|
|
|
2000-01-17 11:17:33 +00: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;
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
if (!kif_sys_reconfigure((struct kif_proto *) p, n, o))
|
2000-01-17 11:17:33 +00:00
|
|
|
return 0;
|
2009-05-29 20:49:30 +00:00
|
|
|
|
2000-01-17 11:17:33 +00:00
|
|
|
if (o->scan_time != n->scan_time)
|
|
|
|
{
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_stop(kif_scan_timer);
|
2017-06-14 11:15:35 +00:00
|
|
|
kif_scan_timer->recurrent = n->scan_time;
|
2000-01-17 11:17:33 +00:00
|
|
|
kif_scan(kif_scan_timer);
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_start(kif_scan_timer, n->scan_time);
|
2000-01-17 11:17:33 +00:00
|
|
|
}
|
2009-05-29 20:49:30 +00:00
|
|
|
|
2017-12-07 12:06:01 +00:00
|
|
|
if (!EMPTY_LIST(o->iface_list) || !EMPTY_LIST(n->iface_list))
|
2009-05-29 20:49:30 +00:00
|
|
|
{
|
|
|
|
/* This is hack, we have to update a configuration
|
|
|
|
* to the new value just now, because it is used
|
2017-12-07 12:06:01 +00:00
|
|
|
* for recalculation of preferred addresses.
|
2009-05-29 20:49:30 +00:00
|
|
|
*/
|
|
|
|
p->cf = new;
|
|
|
|
|
2017-12-07 12:06:01 +00:00
|
|
|
if_recalc_all_preferred_addresses();
|
2009-05-29 20:49:30 +00:00
|
|
|
}
|
|
|
|
|
2000-01-17 11:17:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
kif_preconfig(struct protocol *P UNUSED, struct config *c)
|
|
|
|
{
|
|
|
|
kif_cf = NULL;
|
|
|
|
kif_sys_preconfig(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct proto_config *
|
|
|
|
kif_init_config(int class)
|
|
|
|
{
|
|
|
|
if (kif_cf)
|
|
|
|
cf_error("Kernel device protocol already defined");
|
|
|
|
|
2015-02-21 20:08:23 +00:00
|
|
|
kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, class);
|
2017-06-14 11:15:35 +00:00
|
|
|
kif_cf->scan_time = 60 S;
|
2017-12-07 12:06:01 +00:00
|
|
|
init_list(&kif_cf->iface_list);
|
2012-04-30 13:31:32 +00:00
|
|
|
|
|
|
|
kif_sys_init_config(kif_cf);
|
|
|
|
return (struct proto_config *) kif_cf;
|
|
|
|
}
|
|
|
|
|
2011-11-06 23:31:23 +00:00
|
|
|
static void
|
|
|
|
kif_copy_config(struct proto_config *dest, struct proto_config *src)
|
|
|
|
{
|
|
|
|
struct kif_config *d = (struct kif_config *) dest;
|
|
|
|
struct kif_config *s = (struct kif_config *) src;
|
|
|
|
|
2017-12-07 12:06:01 +00:00
|
|
|
/* Copy interface config list */
|
|
|
|
cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct kif_iface_config));
|
2011-11-06 23:31:23 +00:00
|
|
|
|
|
|
|
/* Fix sysdep parts */
|
2012-04-30 13:31:32 +00:00
|
|
|
kif_sys_copy_config(d, s);
|
2011-11-06 23:31:23 +00:00
|
|
|
}
|
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
struct protocol proto_unix_iface = {
|
2014-12-03 09:10:34 +00:00
|
|
|
.name = "Device",
|
|
|
|
.template = "device%d",
|
2016-01-26 10:48:58 +00:00
|
|
|
.proto_size = sizeof(struct kif_proto),
|
2015-02-21 20:08:23 +00:00
|
|
|
.config_size = sizeof(struct kif_config),
|
2023-09-24 21:22:43 +00:00
|
|
|
.startup = PROTOCOL_STARTUP_NECESSARY,
|
2014-12-03 09:10:34 +00:00
|
|
|
.preconfig = kif_preconfig,
|
|
|
|
.init = kif_init,
|
|
|
|
.start = kif_start,
|
|
|
|
.shutdown = kif_shutdown,
|
2023-01-31 12:11:55 +00:00
|
|
|
.cleanup = kif_cleanup,
|
2014-12-03 09:10:34 +00:00
|
|
|
.reconfigure = kif_reconfigure,
|
|
|
|
.copy_config = kif_copy_config
|
1999-03-26 21:44:38 +00:00
|
|
|
};
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2022-03-18 21:05:50 +00:00
|
|
|
void
|
|
|
|
kif_build(void)
|
|
|
|
{
|
|
|
|
proto_build(&proto_unix_iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-12 21:54:39 +00:00
|
|
|
/*
|
|
|
|
* Tracing of routes
|
|
|
|
*/
|
|
|
|
|
2009-02-26 13:23:54 +00:00
|
|
|
static inline void
|
|
|
|
krt_trace_in(struct krt_proto *p, rte *e, char *msg)
|
2000-03-12 21:54:39 +00:00
|
|
|
{
|
2009-02-26 13:23:54 +00:00
|
|
|
if (p->p.debug & D_PACKETS)
|
2020-01-28 10:42:46 +00:00
|
|
|
log(L_TRACE "%s: %N: %s", p->p.name, e->net, msg);
|
2000-03-12 21:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2014-10-02 09:41:34 +00:00
|
|
|
krt_trace_in_rl(struct tbf *f, struct krt_proto *p, rte *e, char *msg)
|
2000-03-12 21:54:39 +00:00
|
|
|
{
|
|
|
|
if (p->p.debug & D_PACKETS)
|
2020-01-28 10:42:46 +00:00
|
|
|
log_rl(f, L_TRACE "%s: %N: %s", p->p.name, e->net, msg);
|
2000-03-12 21:54:39 +00:00
|
|
|
}
|
|
|
|
|
1999-04-03 13:05:18 +00:00
|
|
|
/*
|
|
|
|
* Inherited Routes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef KRT_ALLOW_LEARN
|
|
|
|
|
2014-10-02 09:41:34 +00:00
|
|
|
static struct tbf rl_alien = TBF_DEFAULT_LOG_LIMITS;
|
2009-02-26 13:23:54 +00:00
|
|
|
|
2012-03-25 17:44:14 +00:00
|
|
|
/*
|
|
|
|
* krt_same_key() specifies what (aside from the net) is the key in
|
|
|
|
* kernel routing tables. It should be OS-dependent, this is for
|
|
|
|
* Linux. It is important for asynchronous alien updates, because a
|
|
|
|
* positive update is implicitly a negative one for any old route with
|
|
|
|
* the same key.
|
|
|
|
*/
|
|
|
|
|
2020-02-13 15:59:53 +00:00
|
|
|
static inline u32
|
|
|
|
krt_metric(rte *a)
|
|
|
|
{
|
2022-05-30 10:03:03 +00:00
|
|
|
eattr *ea = ea_find(a->attrs, &ea_krt_metric);
|
2020-02-13 15:59:53 +00:00
|
|
|
return ea ? ea->u.data : 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:07:11 +00:00
|
|
|
static void
|
|
|
|
krt_learn_alien_attr(struct channel *c, rte *e)
|
|
|
|
{
|
2022-11-07 08:51:21 +00:00
|
|
|
ea_set_attr_u32(&e->attrs, &ea_gen_preference, 0, c->preference);
|
2022-09-21 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2012-03-25 17:44:14 +00:00
|
|
|
/* Called when alien route is discovered during scan */
|
1999-04-03 13:05:18 +00:00
|
|
|
static void
|
|
|
|
krt_learn_scan(struct krt_proto *p, rte *e)
|
|
|
|
{
|
2022-07-11 15:04:52 +00:00
|
|
|
rte e0 = {
|
|
|
|
.attrs = e->attrs,
|
|
|
|
.src = rt_get_source(&p->p, krt_metric(e)),
|
|
|
|
};
|
2016-03-23 17:25:15 +00:00
|
|
|
|
2022-11-07 08:51:21 +00:00
|
|
|
krt_learn_alien_attr(p->p.main_channel, &e0);
|
2015-04-25 19:41:43 +00:00
|
|
|
|
2022-07-11 15:04:52 +00:00
|
|
|
rte_update(p->p.main_channel, e->net, &e0, e0.src);
|
2022-08-02 20:08:59 +00:00
|
|
|
rt_unlock_source(e0.src);
|
1999-04-03 13:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_learn_async(struct krt_proto *p, rte *e, int new)
|
|
|
|
{
|
|
|
|
if (new)
|
2022-07-11 15:04:52 +00:00
|
|
|
return krt_learn_scan(p, e);
|
2018-02-06 15:08:45 +00:00
|
|
|
|
2022-08-02 20:08:59 +00:00
|
|
|
struct rte_src *src = rt_get_source(&p->p, krt_metric(e));
|
|
|
|
rte_update(p->p.main_channel, e->net, NULL, src);
|
|
|
|
rt_unlock_source(src);
|
1999-04-03 13:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* Routes
|
|
|
|
*/
|
|
|
|
|
2019-12-16 01:40:28 +00:00
|
|
|
static inline int
|
2019-12-17 15:30:29 +00:00
|
|
|
krt_is_installed(struct krt_proto *p, net *n)
|
2019-12-16 01:40:28 +00:00
|
|
|
{
|
2020-01-28 10:42:46 +00:00
|
|
|
return n->routes && bmap_test(&p->p.main_channel->export_map, n->routes->rte.id);
|
2019-12-16 01:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 15:07:31 +00:00
|
|
|
static uint
|
2023-09-24 20:20:27 +00:00
|
|
|
rte_feed_count_valid(net *n)
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
2021-06-21 15:07:31 +00:00
|
|
|
uint count = 0;
|
|
|
|
for (struct rte_storage *e = n->routes; e; e = e->next)
|
|
|
|
if (rte_is_valid(RTE_OR_NULL(e)))
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2021-06-21 15:07:31 +00:00
|
|
|
static void
|
2023-09-24 20:20:27 +00:00
|
|
|
rte_feed_obtain_valid(net *n, const rte **feed, uint count)
|
2021-06-21 15:07:31 +00:00
|
|
|
{
|
|
|
|
uint i = 0;
|
|
|
|
for (struct rte_storage *e = n->routes; e; e = e->next)
|
|
|
|
if (rte_is_valid(RTE_OR_NULL(e)))
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
2021-06-21 15:07:31 +00:00
|
|
|
ASSERT_DIE(i < count);
|
|
|
|
feed[i++] = &e->rte;
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
2021-06-21 15:07:31 +00:00
|
|
|
ASSERT_DIE(i == count);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 09:58:46 +00:00
|
|
|
static struct rte *
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_export_net(struct krt_proto *p, net *net)
|
2015-06-03 09:58:46 +00:00
|
|
|
{
|
2022-09-07 11:54:20 +00:00
|
|
|
/* FIXME: Here we are calling filters in table-locked context when exporting
|
|
|
|
* to kernel. Here BIRD can crash if the user requested ROA check in kernel
|
|
|
|
* export filter. It doesn't make much sense to write the filters like this,
|
|
|
|
* therefore we may keep this unfinished piece of work here for later as it
|
|
|
|
* won't really affect anybody. */
|
|
|
|
ASSERT_DIE(RT_IS_LOCKED(p->p.main_channel->table));
|
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
struct channel *c = p->p.main_channel;
|
2019-02-15 12:53:17 +00:00
|
|
|
const struct filter *filter = c->out_filter;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
if (c->ra_mode == RA_MERGED)
|
2021-06-21 15:07:31 +00:00
|
|
|
{
|
2023-09-24 20:20:27 +00:00
|
|
|
uint count = rte_feed_count_valid(net);
|
2021-06-21 15:07:31 +00:00
|
|
|
if (!count)
|
|
|
|
return NULL;
|
|
|
|
|
2023-03-30 09:37:16 +00:00
|
|
|
const rte **feed = alloca(count * sizeof(rte *));
|
2023-09-24 20:20:27 +00:00
|
|
|
rte_feed_obtain_valid(net, feed, count);
|
2023-09-29 14:24:50 +00:00
|
|
|
return rt_export_merged(c, net->n.addr, feed, count, krt_filter_lp, 1);
|
2021-06-21 15:07:31 +00:00
|
|
|
}
|
2015-06-08 00:20:43 +00:00
|
|
|
|
2020-01-28 10:42:46 +00:00
|
|
|
static _Thread_local rte rt;
|
|
|
|
rt = net->routes->rte;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-28 10:42:46 +00:00
|
|
|
if (!rte_is_valid(&rt))
|
2015-06-03 09:58:46 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (filter == FILTER_REJECT)
|
|
|
|
return NULL;
|
|
|
|
|
2019-12-16 01:40:28 +00:00
|
|
|
/* We could run krt_preexport() here, but it is already handled by krt_is_installed() */
|
2015-06-03 09:58:46 +00:00
|
|
|
|
|
|
|
if (filter == FILTER_ACCEPT)
|
|
|
|
goto accept;
|
|
|
|
|
2022-04-10 16:55:15 +00:00
|
|
|
if (f_run(filter, &rt, FF_SILENT) > F_ACCEPT)
|
2015-06-03 09:58:46 +00:00
|
|
|
goto reject;
|
|
|
|
|
|
|
|
|
|
|
|
accept:
|
2020-01-28 10:42:46 +00:00
|
|
|
return &rt;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
|
|
|
reject:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
static int
|
2012-03-25 17:44:14 +00:00
|
|
|
krt_same_dest(rte *k, rte *e)
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
2022-05-30 10:03:03 +00:00
|
|
|
ea_list *ka = k->attrs, *ea = e->attrs;
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2022-05-30 10:03:03 +00:00
|
|
|
eattr *nhea_k = ea_find(ka, &ea_gen_nexthop);
|
|
|
|
eattr *nhea_e = ea_find(ea, &ea_gen_nexthop);
|
2016-05-06 13:48:35 +00:00
|
|
|
|
2022-05-15 16:09:30 +00:00
|
|
|
return (!nhea_k == !nhea_e) && adata_same(nhea_k->u.ptr, nhea_e->u.ptr);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2020-02-13 15:59:53 +00:00
|
|
|
krt_got_route(struct krt_proto *p, rte *e, s8 src)
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
2023-09-24 20:20:27 +00:00
|
|
|
/* Ignore when flushing from table */
|
|
|
|
if (p->flush_routes == 1)
|
|
|
|
return;
|
|
|
|
|
2020-01-28 10:42:46 +00:00
|
|
|
rte *new = NULL;
|
2020-02-13 15:59:53 +00:00
|
|
|
e->pflags = 0;
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2010-02-26 09:55:58 +00:00
|
|
|
#ifdef KRT_ALLOW_LEARN
|
2020-02-13 15:59:53 +00:00
|
|
|
switch (src)
|
1999-04-03 13:05:18 +00:00
|
|
|
{
|
2010-02-26 09:55:58 +00:00
|
|
|
case KRT_SRC_KERNEL:
|
2023-01-20 13:07:38 +00:00
|
|
|
krt_trace_in(p, e, "ignored");
|
|
|
|
return;
|
1999-04-03 13:05:18 +00:00
|
|
|
|
2010-02-26 09:55:58 +00:00
|
|
|
case KRT_SRC_REDIRECT:
|
2023-01-20 13:07:38 +00:00
|
|
|
krt_trace_in(p, e, "deleting");
|
|
|
|
krt_replace_rte(p, e->net, NULL, e);
|
|
|
|
return;
|
2010-02-26 09:55:58 +00:00
|
|
|
|
|
|
|
case KRT_SRC_ALIEN:
|
1999-04-03 13:05:18 +00:00
|
|
|
if (KRT_CF->learn)
|
|
|
|
krt_learn_scan(p, e);
|
|
|
|
else
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_trace_in_rl(&rl_alien, p, e, "[alien] ignored");
|
1999-04-03 13:05:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-20 13:07:38 +00:00
|
|
|
|
2010-02-26 09:55:58 +00:00
|
|
|
/* The rest is for KRT_SRC_BIRD (or KRT_SRC_UNKNOWN) */
|
1999-04-03 13:05:18 +00:00
|
|
|
|
2022-09-07 11:54:20 +00:00
|
|
|
RT_LOCKED(p->p.main_channel->table, tab)
|
|
|
|
{
|
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
/* Deleting all routes if final flush is requested */
|
|
|
|
if (p->flush_routes == 2)
|
2021-10-01 12:11:13 +00:00
|
|
|
goto delete;
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
/* We wait for the initial feed to have correct installed state */
|
2014-03-20 13:07:12 +00:00
|
|
|
if (!p->ready)
|
2020-01-07 17:35:03 +00:00
|
|
|
goto ignore;
|
2014-03-20 13:07:12 +00:00
|
|
|
|
2022-09-07 11:54:20 +00:00
|
|
|
net *net = net_find(tab, e->net);
|
2020-01-28 10:42:46 +00:00
|
|
|
if (!net || !krt_is_installed(p, net))
|
2020-01-07 17:35:03 +00:00
|
|
|
goto delete;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-28 10:42:46 +00:00
|
|
|
new = krt_export_net(p, net);
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
/* Rejected by filters */
|
|
|
|
if (!new)
|
|
|
|
goto delete;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
/* Route to this destination was already seen. Strange, but it happens... */
|
|
|
|
if (bmap_test(&p->seen_map, new->id))
|
|
|
|
goto aseen;
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
/* Mark route as seen */
|
|
|
|
bmap_set(&p->seen_map, new->id);
|
2015-06-03 09:58:46 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
/* TODO: There also may be changes in route eattrs, we ignore that for now. */
|
|
|
|
if (!bmap_test(&p->sync_map, new->id) || !krt_same_dest(e, new))
|
|
|
|
goto update;
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
goto seen;
|
|
|
|
|
|
|
|
seen:
|
|
|
|
krt_trace_in(p, e, "seen");
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
aseen:
|
|
|
|
krt_trace_in(p, e, "already seen");
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ignore:
|
|
|
|
krt_trace_in(p, e, "ignored");
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
update:
|
|
|
|
krt_trace_in(p, new, "updating");
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_replace_rte(p, e->net, new, e);
|
2020-01-07 17:35:03 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
delete:
|
|
|
|
krt_trace_in(p, e, "deleting");
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_replace_rte(p, e->net, NULL, e);
|
2020-01-07 17:35:03 +00:00
|
|
|
goto done;
|
|
|
|
|
2022-09-07 11:54:20 +00:00
|
|
|
done:;
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
lp_flush(krt_filter_lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_init_scan(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
bmap_reset(&p->seen_map, 1024);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_prune(struct krt_proto *p)
|
|
|
|
{
|
2022-09-07 11:54:20 +00:00
|
|
|
RT_LOCKED(p->p.main_channel->table, t)
|
|
|
|
{
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2000-03-12 21:54:39 +00:00
|
|
|
KRT_TRACE(p, D_EVENTS, "Pruning table %s", t->name);
|
2015-12-21 19:16:05 +00:00
|
|
|
FIB_WALK(&t->fib, net, n)
|
2020-01-07 17:35:03 +00:00
|
|
|
{
|
2020-01-28 10:42:46 +00:00
|
|
|
if (p->ready && krt_is_installed(p, n) && !bmap_test(&p->seen_map, n->routes->rte.id))
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
2020-01-28 10:42:46 +00:00
|
|
|
rte *new = krt_export_net(p, n);
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
if (new)
|
|
|
|
{
|
|
|
|
krt_trace_in(p, new, "installing");
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_replace_rte(p, n->n.addr, new, NULL);
|
2020-01-07 17:35:03 +00:00
|
|
|
}
|
2012-03-25 17:44:14 +00:00
|
|
|
|
|
|
|
lp_flush(krt_filter_lp);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
2020-01-07 17:35:03 +00:00
|
|
|
}
|
1999-03-03 19:49:56 +00:00
|
|
|
FIB_WALK_END;
|
1999-04-03 13:05:18 +00:00
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
if (p->ready)
|
|
|
|
p->initialized = 1;
|
2022-09-07 11:54:20 +00:00
|
|
|
|
|
|
|
}
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
1999-03-04 18:36:18 +00:00
|
|
|
void
|
2020-02-13 15:59:53 +00:00
|
|
|
krt_got_route_async(struct krt_proto *p, rte *e, int new, s8 src)
|
1999-03-04 18:36:18 +00:00
|
|
|
{
|
2020-02-13 15:59:53 +00:00
|
|
|
e->pflags = 0;
|
1999-03-04 18:36:18 +00:00
|
|
|
|
2020-02-13 15:59:53 +00:00
|
|
|
switch (src)
|
1999-03-04 18:36:18 +00:00
|
|
|
{
|
|
|
|
case KRT_SRC_BIRD:
|
2018-09-18 12:21:11 +00:00
|
|
|
/* Should be filtered by the back end */
|
|
|
|
bug("BIRD originated routes should not get here.");
|
2010-02-26 09:55:58 +00:00
|
|
|
|
1999-03-04 18:36:18 +00:00
|
|
|
case KRT_SRC_REDIRECT:
|
2010-02-26 09:55:58 +00:00
|
|
|
if (new)
|
|
|
|
{
|
|
|
|
krt_trace_in(p, e, "[redirect] deleting");
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_replace_rte(p, e->net, NULL, e);
|
2010-02-26 09:55:58 +00:00
|
|
|
}
|
|
|
|
/* If !new, it is probably echo of our deletion */
|
1999-03-04 18:36:18 +00:00
|
|
|
break;
|
2010-02-26 09:55:58 +00:00
|
|
|
|
1999-04-03 13:05:18 +00:00
|
|
|
#ifdef KRT_ALLOW_LEARN
|
2009-05-04 15:49:56 +00:00
|
|
|
case KRT_SRC_ALIEN:
|
1999-04-03 13:05:18 +00:00
|
|
|
if (KRT_CF->learn)
|
1999-03-04 18:36:18 +00:00
|
|
|
{
|
1999-04-03 13:05:18 +00:00
|
|
|
krt_learn_async(p, e, new);
|
|
|
|
return;
|
1999-03-04 18:36:18 +00:00
|
|
|
}
|
1999-04-03 13:05:18 +00:00
|
|
|
#endif
|
1999-03-04 18:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:15:20 +00:00
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* Periodic scanning
|
|
|
|
*/
|
|
|
|
|
2022-07-24 00:15:20 +00:00
|
|
|
static timer *krt_scan_all_timer;
|
|
|
|
static int krt_scan_all_count;
|
|
|
|
static _Bool krt_scan_all_tables;
|
2013-06-29 20:55:41 +00:00
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
static void
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all(timer *t UNUSED)
|
1999-03-03 19:49:56 +00:00
|
|
|
{
|
1999-08-03 19:33:22 +00:00
|
|
|
struct krt_proto *p;
|
2020-01-07 17:35:03 +00:00
|
|
|
node *n;
|
1999-03-03 19:49:56 +00:00
|
|
|
|
1999-03-26 21:44:38 +00:00
|
|
|
kif_force_scan();
|
2013-06-29 20:55:41 +00:00
|
|
|
|
|
|
|
/* We need some node to decide whether to print the debug messages or not */
|
|
|
|
p = SKIP_BACK(struct krt_proto, krt_node, HEAD(krt_proto_list));
|
|
|
|
KRT_TRACE(p, D_EVENTS, "Scanning routing table");
|
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
WALK_LIST2(p, n, krt_proto_list, krt_node)
|
|
|
|
krt_init_scan(p);
|
|
|
|
|
2013-06-29 20:55:41 +00:00
|
|
|
krt_do_scan(NULL);
|
|
|
|
|
2020-01-07 17:35:03 +00:00
|
|
|
WALK_LIST2(p, n, krt_proto_list, krt_node)
|
2013-06-29 20:55:41 +00:00
|
|
|
krt_prune(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all_timer_start(struct krt_proto *p)
|
2013-06-29 20:55:41 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
if (!krt_scan_all_count)
|
|
|
|
krt_scan_all_timer = tm_new_init(krt_pool, krt_scan_all, NULL, KRT_CF->scan_time, 0);
|
2013-06-29 20:55:41 +00:00
|
|
|
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all_count++;
|
2013-10-15 08:57:57 +00:00
|
|
|
|
2022-07-24 00:15:20 +00:00
|
|
|
tm_start(krt_scan_all_timer, 1 S);
|
2013-06-29 20:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all_timer_stop(void)
|
2013-06-29 20:55:41 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
ASSERT(krt_scan_all_count > 0);
|
|
|
|
|
|
|
|
krt_scan_all_count--;
|
2013-06-29 20:55:41 +00:00
|
|
|
|
2022-07-24 00:15:20 +00:00
|
|
|
if (!krt_scan_all_count)
|
2013-06-29 20:55:41 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
rfree(krt_scan_all_timer);
|
|
|
|
krt_scan_all_timer = NULL;
|
1999-08-03 19:33:22 +00:00
|
|
|
}
|
2013-06-29 20:55:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
static void
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all_timer_kick(void)
|
2014-03-20 13:07:12 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
tm_start(krt_scan_all_timer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_use_shared_scan(void)
|
2014-03-20 13:07:12 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
krt_scan_all_tables = 1;
|
2014-03-20 13:07:12 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 20:55:41 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
krt_scan(timer *t)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = t->data;
|
|
|
|
|
|
|
|
kif_force_scan();
|
|
|
|
|
2000-03-12 21:54:39 +00:00
|
|
|
KRT_TRACE(p, D_EVENTS, "Scanning routing table");
|
2020-01-07 17:35:03 +00:00
|
|
|
krt_init_scan(p);
|
2012-04-30 13:31:32 +00:00
|
|
|
krt_do_scan(p);
|
1999-03-26 21:44:38 +00:00
|
|
|
krt_prune(p);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 20:55:41 +00:00
|
|
|
static void
|
|
|
|
krt_scan_timer_start(struct krt_proto *p)
|
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
if (krt_scan_all_tables)
|
|
|
|
krt_scan_all_timer_start(p);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->scan_timer = tm_new_init(p->p.pool, krt_scan, p, KRT_CF->scan_time, 0);
|
|
|
|
tm_start(p->scan_timer, 1 S);
|
|
|
|
}
|
2013-06-29 20:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_scan_timer_stop(struct krt_proto *p)
|
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
if (krt_scan_all_tables)
|
|
|
|
krt_scan_all_timer_stop();
|
|
|
|
else
|
|
|
|
tm_stop(p->scan_timer);
|
2013-06-29 20:55:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
static void
|
2014-10-02 09:05:55 +00:00
|
|
|
krt_scan_timer_kick(struct krt_proto *p)
|
2014-03-20 13:07:12 +00:00
|
|
|
{
|
2022-07-24 00:15:20 +00:00
|
|
|
if (krt_scan_all_tables)
|
|
|
|
krt_scan_all_timer_kick();
|
|
|
|
else
|
|
|
|
tm_start(p->scan_timer, 0);
|
2014-03-20 13:07:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
|
1999-04-03 13:05:18 +00:00
|
|
|
/*
|
|
|
|
* Updates
|
|
|
|
*/
|
2012-04-30 13:31:32 +00:00
|
|
|
|
2010-04-04 13:41:31 +00:00
|
|
|
static int
|
2022-07-11 08:41:17 +00:00
|
|
|
krt_preexport(struct channel *C, rte *e)
|
2010-04-04 13:41:31 +00:00
|
|
|
{
|
2022-08-02 20:08:59 +00:00
|
|
|
if (e->src->owner == &C->proto->sources)
|
2023-01-20 13:07:38 +00:00
|
|
|
#ifdef CONFIG_SINGLE_ROUTE
|
|
|
|
return 1;
|
|
|
|
#else
|
2010-04-04 13:41:31 +00:00
|
|
|
return -1;
|
2023-01-20 13:07:38 +00:00
|
|
|
#endif
|
2010-04-04 13:41:31 +00:00
|
|
|
|
|
|
|
if (!krt_capable(e))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1999-04-03 13:05:18 +00:00
|
|
|
|
|
|
|
static void
|
2020-01-28 10:42:46 +00:00
|
|
|
krt_rt_notify(struct proto *P, struct channel *ch UNUSED, const net_addr *net,
|
|
|
|
rte *new, const rte *old)
|
1999-04-03 13:05:18 +00:00
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
if (p->flush_routes)
|
|
|
|
{
|
|
|
|
krt_replace_rte(p, net, NULL, old ?: new);
|
2000-05-16 13:36:38 +00:00
|
|
|
return;
|
2023-09-24 20:20:27 +00:00
|
|
|
}
|
2019-12-16 01:40:28 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_SINGLE_ROUTE
|
2023-01-20 13:07:38 +00:00
|
|
|
/* Got the same route as we imported. Keep it, do nothing. */
|
|
|
|
if (new && new->src->owner == &P->sources)
|
2019-12-16 01:40:28 +00:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2012-03-25 17:44:14 +00:00
|
|
|
if (p->initialized) /* Before first scan we don't touch the routes */
|
2018-05-29 10:08:12 +00:00
|
|
|
krt_replace_rte(p, net, new, old);
|
1999-04-03 13:05:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 09:05:55 +00:00
|
|
|
static void
|
|
|
|
krt_if_notify(struct proto *P, uint flags, struct iface *iface UNUSED)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When interface went down, we should remove routes to it. In the ideal world,
|
|
|
|
* OS kernel would send us route removal notifications in such cases, but we
|
|
|
|
* cannot rely on it as it is often not true. E.g. Linux kernel removes related
|
|
|
|
* routes when an interface went down, but it does not notify userspace about
|
|
|
|
* that. To be sure, we just schedule a scan to ensure synchronization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & IF_CHANGE_DOWN) && KRT_CF->learn)
|
|
|
|
krt_scan_timer_kick(p);
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:00:03 +00:00
|
|
|
static int
|
|
|
|
krt_reload_routes(struct channel *C, struct channel_import_request *UNUSED)
|
2014-10-02 09:05:55 +00:00
|
|
|
{
|
2016-01-26 10:48:58 +00:00
|
|
|
struct krt_proto *p = (void *) C->proto;
|
2014-10-02 09:05:55 +00:00
|
|
|
|
|
|
|
/* Although we keep learned routes in krt_table, we rather schedule a scan */
|
|
|
|
|
|
|
|
if (KRT_CF->learn)
|
2015-04-25 19:41:43 +00:00
|
|
|
{
|
|
|
|
p->reload = 1;
|
2014-10-02 09:05:55 +00:00
|
|
|
krt_scan_timer_kick(p);
|
2015-04-25 19:41:43 +00:00
|
|
|
}
|
2023-10-24 10:00:03 +00:00
|
|
|
return 1;
|
2014-10-02 09:05:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
static void krt_cleanup(struct krt_proto *p);
|
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
static void
|
2016-01-26 10:48:58 +00:00
|
|
|
krt_feed_end(struct channel *C)
|
2014-03-20 13:07:12 +00:00
|
|
|
{
|
2016-01-26 10:48:58 +00:00
|
|
|
struct krt_proto *p = (void *) C->proto;
|
2014-03-20 13:07:12 +00:00
|
|
|
|
2023-09-29 14:24:50 +00:00
|
|
|
if (C->refeeding && C->refeed_req.hook)
|
|
|
|
return;
|
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
if (p->flush_routes)
|
|
|
|
{
|
|
|
|
p->flush_routes = 2;
|
|
|
|
krt_init_scan(p);
|
|
|
|
krt_do_scan(p);
|
|
|
|
krt_cleanup(p);
|
|
|
|
proto_notify_state(&p->p, PS_DOWN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
p->ready = 1;
|
|
|
|
krt_scan_timer_kick(p);
|
|
|
|
}
|
|
|
|
|
2022-09-29 07:58:27 +00:00
|
|
|
static int
|
2023-03-30 09:37:16 +00:00
|
|
|
krt_rte_better(const rte *new, const rte *old)
|
2022-09-29 07:58:27 +00:00
|
|
|
{
|
|
|
|
u32 n = ea_get_int(new->attrs, &ea_krt_metric, IGP_METRIC_UNKNOWN);
|
|
|
|
u32 o = ea_get_int(old->attrs, &ea_krt_metric, IGP_METRIC_UNKNOWN);
|
|
|
|
|
|
|
|
return (n < o);
|
|
|
|
}
|
2014-03-20 13:07:12 +00:00
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
/*
|
|
|
|
* Protocol glue
|
|
|
|
*/
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
struct krt_config *krt_cf;
|
1999-03-26 21:44:38 +00:00
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
static void
|
|
|
|
krt_preconfig(struct protocol *P UNUSED, struct config *c)
|
|
|
|
{
|
|
|
|
krt_cf = NULL;
|
|
|
|
krt_sys_preconfig(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
krt_postconfig(struct proto_config *CF)
|
|
|
|
{
|
|
|
|
struct krt_config *cf = (void *) CF;
|
|
|
|
|
2021-01-07 00:56:00 +00:00
|
|
|
/* Do not check templates at all */
|
|
|
|
if (cf->c.class == SYM_TEMPLATE)
|
|
|
|
return;
|
|
|
|
|
2021-06-17 14:56:51 +00:00
|
|
|
if (! proto_cf_main_channel(CF))
|
2016-01-26 10:48:58 +00:00
|
|
|
cf_error("Channel not specified");
|
|
|
|
|
2018-01-23 14:12:43 +00:00
|
|
|
struct channel_config *cc = proto_cf_main_channel(CF);
|
|
|
|
struct rtable_config *tab = cc->table;
|
2016-01-26 10:48:58 +00:00
|
|
|
if (tab->krt_attached)
|
|
|
|
cf_error("Kernel syncer (%s) already attached to table %s", tab->krt_attached->name, tab->name);
|
|
|
|
tab->krt_attached = CF;
|
|
|
|
|
2018-01-23 14:12:43 +00:00
|
|
|
if (cf->merge_paths)
|
|
|
|
{
|
|
|
|
cc->ra_mode = RA_MERGED;
|
|
|
|
cc->merge_limit = cf->merge_paths;
|
|
|
|
}
|
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
krt_sys_postconfig(cf);
|
|
|
|
}
|
|
|
|
|
2022-10-04 13:53:12 +00:00
|
|
|
struct rte_owner_class krt_rte_owner_class = {
|
|
|
|
.rte_better = krt_rte_better,
|
|
|
|
};
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
static struct proto *
|
2016-01-26 10:48:58 +00:00
|
|
|
krt_init(struct proto_config *CF)
|
1999-08-03 19:33:22 +00:00
|
|
|
{
|
2016-01-26 10:48:58 +00:00
|
|
|
struct krt_proto *p = proto_new(CF);
|
|
|
|
// struct krt_config *cf = (void *) CF;
|
|
|
|
|
|
|
|
p->p.main_channel = proto_add_channel(&p->p, proto_cf_main_channel(CF));
|
1999-08-03 19:33:22 +00:00
|
|
|
|
Terminology cleanup: The import_control hook is now called preexport.
Once upon a time, far far away, there were the old Bird developers
discussing what direction of route flow shall be called import and
export. They decided to say "import to protocol" and "export to table"
when speaking about a protocol. When speaking about a table, they
spoke about "importing to table" and "exporting to protocol".
The latter terminology was adopted in configuration, then also the
bird CLI in commit ea2ae6dd0 started to use it (in year 2009). Now
it's 2018 and the terminology is the latter. Import is from protocol to
table, export is from table to protocol. Anyway, there was still an
import_control hook which executed right before route export.
One thing is funny. There are two commits in April 1999 with just two
minutes between them. The older announces the final settlement
on config terminology, the newer uses the other definition. Let's see
their commit messages as the git-log tool shows them (the newer first):
commit 9e0e485e50ea74c4f1c5cb65bdfe6ce819c2cee2
Author: Martin Mares <mj@ucw.cz>
Date: Mon Apr 5 20:17:59 1999 +0000
Added some new protocol hooks (look at the comments for better explanation):
make_tmp_attrs Convert inline attributes to ea_list
store_tmp_attrs Convert ea_list to inline attributes
import_control Pre-import decisions
commit 5056c559c4eb253a4eee10cf35b694faec5265eb
Author: Martin Mares <mj@ucw.cz>
Date: Mon Apr 5 20:15:31 1999 +0000
Changed syntax of attaching filters to protocols to hopefully the final
version:
EXPORT <filter-spec> for outbound routes (i.e., those announced
by BIRD to the rest of the world).
IMPORT <filter-spec> for inbound routes (i.e., those imported
by BIRD from the rest of the world).
where <filter-spec> is one of:
ALL pass all routes
NONE drop all routes
FILTER <name> use named filter
FILTER { <filter> } use explicitly defined filter
For all protocols, the default is IMPORT ALL, EXPORT NONE. This includes
the kernel protocol, so that you need to add EXPORT ALL to get the previous
configuration of kernel syncer (as usually, see doc/bird.conf.example for
a bird.conf example :)).
Let's say RIP to this almost 19-years-old inconsistency. For now, if you
import a route, it is always from protocol to table. If you export a
route, it is always from table to protocol.
And they lived happily ever after.
2018-02-14 12:42:53 +00:00
|
|
|
p->p.preexport = krt_preexport;
|
2014-10-02 09:05:55 +00:00
|
|
|
p->p.rt_notify = krt_rt_notify;
|
2023-01-31 12:07:46 +00:00
|
|
|
p->p.iface_sub.if_notify = krt_if_notify;
|
2014-10-02 09:05:55 +00:00
|
|
|
p->p.reload_routes = krt_reload_routes;
|
2015-03-29 16:27:13 +00:00
|
|
|
p->p.feed_end = krt_feed_end;
|
1999-08-03 19:33:22 +00:00
|
|
|
|
2022-10-04 13:53:12 +00:00
|
|
|
p->p.sources.class = &krt_rte_owner_class;
|
1999-08-03 19:33:22 +00:00
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
krt_sys_init(p);
|
|
|
|
return &p->p;
|
1999-08-03 19:33:22 +00:00
|
|
|
}
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
static int
|
|
|
|
krt_start(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
1999-08-03 19:33:22 +00:00
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
switch (p->p.net_type)
|
2015-12-20 15:58:37 +00:00
|
|
|
{
|
2018-02-13 15:27:57 +00:00
|
|
|
case NET_IP4: p->af = AF_INET; break;
|
|
|
|
case NET_IP6: p->af = AF_INET6; break;
|
|
|
|
case NET_IP6_SADR: p->af = AF_INET6; break;
|
2017-04-28 22:36:35 +00:00
|
|
|
#ifdef AF_MPLS
|
2018-02-13 15:27:57 +00:00
|
|
|
case NET_MPLS: p->af = AF_MPLS; break;
|
2017-04-28 22:36:35 +00:00
|
|
|
#endif
|
2016-03-04 11:55:50 +00:00
|
|
|
default: log(L_ERR "KRT: Tried to start with strange net type: %d", p->p.net_type); return PS_START; break;
|
2015-12-20 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:34:35 +00:00
|
|
|
bmap_init(&p->sync_map, p->p.pool, 1024);
|
2020-01-07 17:35:03 +00:00
|
|
|
bmap_init(&p->seen_map, p->p.pool, 1024);
|
2013-06-29 20:55:41 +00:00
|
|
|
add_tail(&krt_proto_list, &p->krt_node);
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2015-11-03 13:42:41 +00:00
|
|
|
if (!krt_sys_start(p))
|
|
|
|
{
|
|
|
|
rem_node(&p->krt_node);
|
|
|
|
return PS_START;
|
|
|
|
}
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2013-06-29 20:55:41 +00:00
|
|
|
krt_scan_timer_start(p);
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2016-01-26 10:48:58 +00:00
|
|
|
if (p->p.gr_recovery && KRT_CF->graceful_restart)
|
|
|
|
p->p.main_channel->gr_wait = 1;
|
2014-03-20 13:07:12 +00:00
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
|
1999-08-03 19:33:22 +00:00
|
|
|
static int
|
1999-03-03 19:49:56 +00:00
|
|
|
krt_shutdown(struct proto *P)
|
|
|
|
{
|
|
|
|
struct krt_proto *p = (struct krt_proto *) P;
|
|
|
|
|
2013-06-29 20:55:41 +00:00
|
|
|
krt_scan_timer_stop(p);
|
1999-03-26 21:44:38 +00:00
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
if (p->p.proto_state == PS_START)
|
|
|
|
return PS_DOWN;
|
|
|
|
|
2009-03-20 11:58:21 +00:00
|
|
|
/* FIXME we should flush routes even when persist during reconfiguration */
|
2019-06-18 14:27:21 +00:00
|
|
|
if (p->initialized && !KRT_CF->persist && (P->down_code != PDC_CMD_GR_DOWN))
|
2023-09-24 20:20:27 +00:00
|
|
|
{
|
|
|
|
p->flush_routes = 1;
|
2023-09-29 14:24:50 +00:00
|
|
|
channel_request_feeding_dynamic(p->p.main_channel, CFRT_AUXILIARY);
|
2023-09-24 20:20:27 +00:00
|
|
|
return PS_UP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
krt_cleanup(p);
|
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
}
|
1999-03-03 19:49:56 +00:00
|
|
|
|
2023-09-24 20:20:27 +00:00
|
|
|
static void
|
|
|
|
krt_cleanup(struct krt_proto *p)
|
|
|
|
{
|
2014-03-20 13:07:12 +00:00
|
|
|
p->ready = 0;
|
|
|
|
p->initialized = 0;
|
|
|
|
|
2015-11-03 13:42:41 +00:00
|
|
|
krt_sys_shutdown(p);
|
2013-06-29 20:55:41 +00:00
|
|
|
rem_node(&p->krt_node);
|
2019-12-19 15:34:35 +00:00
|
|
|
bmap_free(&p->sync_map);
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
static int
|
2016-01-26 10:48:58 +00:00
|
|
|
krt_reconfigure(struct proto *p, struct proto_config *CF)
|
2012-03-22 23:26:26 +00:00
|
|
|
{
|
2016-01-26 10:48:58 +00:00
|
|
|
struct krt_config *o = (void *) p->cf;
|
|
|
|
struct krt_config *n = (void *) CF;
|
|
|
|
|
|
|
|
if (!proto_configure_channel(p, &p->main_channel, proto_cf_main_channel(CF)))
|
|
|
|
return 0;
|
2012-03-22 23:26:26 +00:00
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
if (!krt_sys_reconfigure((struct krt_proto *) p, n, o))
|
|
|
|
return 0;
|
2012-03-23 00:17:02 +00:00
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
/* persist, graceful restart need not be the same */
|
2018-01-23 13:48:07 +00:00
|
|
|
return o->scan_time == n->scan_time && o->learn == n->learn;
|
1999-03-03 19:49:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
struct proto_config *
|
|
|
|
krt_init_config(int class)
|
2000-01-18 10:39:30 +00:00
|
|
|
{
|
2012-04-30 13:31:32 +00:00
|
|
|
#ifndef CONFIG_MULTIPLE_TABLES
|
|
|
|
if (krt_cf)
|
|
|
|
cf_error("Kernel protocol already defined");
|
|
|
|
#endif
|
|
|
|
|
2015-02-21 20:08:23 +00:00
|
|
|
krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
|
2017-06-14 11:15:35 +00:00
|
|
|
krt_cf->scan_time = 60 S;
|
2000-01-18 10:39:30 +00:00
|
|
|
|
2012-04-30 13:31:32 +00:00
|
|
|
krt_sys_init_config(krt_cf);
|
|
|
|
return (struct proto_config *) krt_cf;
|
2000-01-18 10:39:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 23:31:23 +00:00
|
|
|
static void
|
|
|
|
krt_copy_config(struct proto_config *dest, struct proto_config *src)
|
|
|
|
{
|
|
|
|
struct krt_config *d = (struct krt_config *) dest;
|
|
|
|
struct krt_config *s = (struct krt_config *) src;
|
|
|
|
|
|
|
|
/* Fix sysdep parts */
|
2012-04-30 13:31:32 +00:00
|
|
|
krt_sys_copy_config(d, s);
|
2011-11-06 23:31:23 +00:00
|
|
|
}
|
2011-04-13 10:32:27 +00:00
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class ea_krt_source = {
|
|
|
|
.name = "krt_source",
|
|
|
|
.type = T_INT,
|
|
|
|
};
|
2011-04-13 10:32:27 +00:00
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class ea_krt_metric = {
|
|
|
|
.name = "krt_metric",
|
|
|
|
.type = T_INT,
|
|
|
|
};
|
2011-04-13 10:32:27 +00:00
|
|
|
|
2018-02-13 15:27:57 +00:00
|
|
|
#ifdef CONFIG_IP6_SADR_KERNEL
|
|
|
|
#define MAYBE_IP6_SADR NB_IP6_SADR
|
|
|
|
#else
|
|
|
|
#define MAYBE_IP6_SADR 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MPLS_KERNEL
|
|
|
|
#define MAYBE_MPLS NB_MPLS
|
|
|
|
#else
|
|
|
|
#define MAYBE_MPLS 0
|
|
|
|
#endif
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
struct protocol proto_unix_kernel = {
|
2014-12-03 09:10:34 +00:00
|
|
|
.name = "Kernel",
|
|
|
|
.template = "kernel%d",
|
|
|
|
.preference = DEF_PREF_INHERITED,
|
2018-02-13 15:27:57 +00:00
|
|
|
.channel_mask = NB_IP | MAYBE_IP6_SADR | MAYBE_MPLS,
|
2016-01-26 10:48:58 +00:00
|
|
|
.proto_size = sizeof(struct krt_proto),
|
2015-02-21 20:08:23 +00:00
|
|
|
.config_size = sizeof(struct krt_config),
|
2023-09-24 21:22:43 +00:00
|
|
|
.startup = PROTOCOL_STARTUP_CONNECTOR,
|
2014-12-03 09:10:34 +00:00
|
|
|
.preconfig = krt_preconfig,
|
|
|
|
.postconfig = krt_postconfig,
|
|
|
|
.init = krt_init,
|
|
|
|
.start = krt_start,
|
|
|
|
.shutdown = krt_shutdown,
|
|
|
|
.reconfigure = krt_reconfigure,
|
|
|
|
.copy_config = krt_copy_config,
|
1999-03-03 19:49:56 +00:00
|
|
|
};
|
2022-03-18 21:05:50 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
krt_build(void)
|
|
|
|
{
|
|
|
|
proto_build(&proto_unix_kernel);
|
2022-03-19 15:23:42 +00:00
|
|
|
|
|
|
|
EA_REGISTER_ALL(
|
|
|
|
&ea_krt_source,
|
|
|
|
&ea_krt_metric,
|
|
|
|
);
|
2022-03-18 21:05:50 +00:00
|
|
|
}
|
2023-10-24 07:52:09 +00:00
|
|
|
|
|
|
|
|