1998-12-07 02:21:23 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Static Route Generator
|
|
|
|
*
|
2000-01-17 19:52:50 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-12-07 02:21:23 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 00:15:37 +08:00
|
|
|
/**
|
|
|
|
* DOC: Static
|
|
|
|
*
|
2000-06-08 20:37:21 +08:00
|
|
|
* The Static protocol is implemented in a straightforward way. It keeps
|
|
|
|
* two lists of static routes: one containing interface routes and one
|
2000-06-05 00:15:37 +08:00
|
|
|
* holding the remaining ones. Interface routes are inserted and removed according
|
2000-06-08 20:37:21 +08:00
|
|
|
* to interface events received from the core via the if_notify() hook. Routes
|
2000-06-05 00:15:37 +08:00
|
|
|
* pointing to a neighboring router use a sticky node in the neighbor cache
|
2000-06-08 20:37:21 +08:00
|
|
|
* to be notified about gaining or losing the neighbor. Special
|
2000-06-05 00:15:37 +08:00
|
|
|
* routes like black holes or rejects are inserted all the time.
|
|
|
|
*
|
2010-12-08 06:34:36 +08:00
|
|
|
* Multipath routes are tricky. Because these routes depends on
|
|
|
|
* several neighbors we need to integrate that to the neighbor
|
|
|
|
* notification handling, we use dummy static_route nodes, one for
|
|
|
|
* each nexthop. Therefore, a multipath route consists of a master
|
|
|
|
* static_route node (of dest RTD_MULTIPATH), which specifies prefix
|
|
|
|
* and is used in most circumstances, and a list of dummy static_route
|
|
|
|
* nodes (of dest RTD_NONE), which stores info about nexthops and are
|
|
|
|
* connected to neighbor entries and neighbor notifications. Dummy
|
|
|
|
* nodes are chained using mp_next, they aren't in other_routes list,
|
2015-11-12 09:03:59 +08:00
|
|
|
* and abuse if_name field for other purposes.
|
2010-12-08 06:34:36 +08:00
|
|
|
*
|
2000-06-05 00:15:37 +08:00
|
|
|
* The only other thing worth mentioning is that when asked for reconfiguration,
|
|
|
|
* Static not only compares the two configurations, but it also calculates
|
2000-06-08 20:37:21 +08:00
|
|
|
* difference between the lists of static routes and it just inserts the
|
|
|
|
* newly added routes and removes the obsolete ones.
|
2000-06-05 00:15:37 +08:00
|
|
|
*/
|
|
|
|
|
2000-03-13 05:01:38 +08:00
|
|
|
#undef LOCAL_DEBUG
|
1998-12-07 02:21:23 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/route.h"
|
1999-12-03 19:41:23 +08:00
|
|
|
#include "nest/cli.h"
|
1998-12-07 02:21:23 +08:00
|
|
|
#include "conf/conf.h"
|
2015-07-20 17:12:02 +08:00
|
|
|
#include "filter/filter.h"
|
1999-12-03 19:41:23 +08:00
|
|
|
#include "lib/string.h"
|
2010-12-08 06:34:36 +08:00
|
|
|
#include "lib/alloca.h"
|
1998-12-07 02:21:23 +08:00
|
|
|
|
|
|
|
#include "static.h"
|
|
|
|
|
2015-07-20 17:12:02 +08:00
|
|
|
static linpool *static_lp;
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
static inline rtable *
|
|
|
|
p_igp_table(struct proto *p)
|
|
|
|
{
|
|
|
|
struct static_config *cf = (void *) p->cf;
|
2016-01-26 18:48:58 +08:00
|
|
|
return cf->igp_table ? cf->igp_table->table : p->main_channel->table;
|
2011-09-24 08:21:52 +08:00
|
|
|
}
|
|
|
|
|
1998-12-07 07:13:31 +08:00
|
|
|
static void
|
2016-05-06 21:48:35 +08:00
|
|
|
static_install(struct proto *p, struct static_route *r)
|
1998-12-07 07:13:31 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
rta *ap = alloca(RTA_MAX_SIZE);
|
1998-12-07 07:13:31 +08:00
|
|
|
rte *e;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
if (!(r->state & STS_WANT) && (r->state & (STS_INSTALLED | STS_FORCE)) && r->dest != RTD_UNICAST)
|
|
|
|
goto drop;
|
2000-01-17 20:38:50 +08:00
|
|
|
|
2015-11-12 09:03:59 +08:00
|
|
|
DBG("Installing static route %N, rtd=%d\n", r->net, r->dest);
|
2016-06-13 21:49:53 +08:00
|
|
|
bzero(ap, RTA_MAX_SIZE);
|
|
|
|
ap->src = p->main_source;
|
|
|
|
ap->source = ((r->dest == RTD_UNICAST) && ipa_zero(r->via)) ? RTS_STATIC_DEVICE : RTS_STATIC;
|
|
|
|
ap->scope = SCOPE_UNIVERSE;
|
|
|
|
ap->dest = r->dest;
|
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
if (r->dest == RTD_UNICAST)
|
2010-12-08 06:34:36 +08:00
|
|
|
{
|
|
|
|
struct static_route *r2;
|
2016-06-13 21:49:53 +08:00
|
|
|
int num = 0, update = 0;
|
2016-05-06 21:48:35 +08:00
|
|
|
|
|
|
|
for (r2 = r; r2; r2 = r2->mp_next)
|
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
|
|
|
|
if ((r2->state & STS_FORCE) ||
|
|
|
|
(!!(r2->state & STS_INSTALLED) != !!(r2->state & STS_WANT)))
|
|
|
|
update++;
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
if (r2->state & STS_WANT)
|
2010-12-08 06:34:36 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
struct nexthop *nh = (ap->nh.next) ? alloca(NEXTHOP_MAX_SIZE) : &(ap->nh);
|
|
|
|
if (ipa_zero(r2->via)) // Device nexthop
|
|
|
|
{
|
|
|
|
nh->gw = IPA_NONE;
|
|
|
|
nh->iface = r2->iface;
|
|
|
|
}
|
|
|
|
else // Router nexthop
|
|
|
|
{
|
|
|
|
nh->gw = r2->via;
|
|
|
|
nh->iface = r2->neigh->iface;
|
|
|
|
}
|
2015-11-12 09:03:59 +08:00
|
|
|
nh->weight = r2->weight;
|
2016-06-13 21:49:53 +08:00
|
|
|
nh->labels = r2->label_count;
|
|
|
|
for (int i=0; i<nh->labels; i++)
|
|
|
|
nh->label[i] = r2->label_stack[i];
|
|
|
|
|
|
|
|
if (ap->nh.next)
|
|
|
|
nexthop_insert(&(ap->nh), nh);
|
2016-05-06 21:48:35 +08:00
|
|
|
r2->state |= STS_INSTALLED;
|
|
|
|
num++;
|
2010-12-08 06:34:36 +08:00
|
|
|
}
|
2016-05-06 21:48:35 +08:00
|
|
|
else
|
|
|
|
r2->state = 0;
|
|
|
|
}
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
if (!update) // Nothing changed
|
|
|
|
return;
|
|
|
|
|
|
|
|
r = r->mp_head;
|
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
if (!num) // No nexthop to install
|
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
drop:
|
|
|
|
rte_update(p, r->net, NULL);
|
2016-05-06 21:48:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-12-08 06:34:36 +08:00
|
|
|
}
|
2016-06-13 21:49:53 +08:00
|
|
|
else
|
|
|
|
r->state |= STS_INSTALLED;
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
if (r->dest == RTDX_RECURSIVE)
|
2016-08-09 20:47:51 +08:00
|
|
|
{
|
|
|
|
ap->nh.labels_append = ap->nh.labels = r->label_count;
|
|
|
|
memcpy(ap->nh.label, r->label_stack, r->label_count * sizeof(u32));
|
|
|
|
rta_set_recursive_next_hop(p->main_channel->table, ap, p_igp_table(p), r->via, IPA_NONE);
|
|
|
|
}
|
2011-09-24 08:21:52 +08:00
|
|
|
|
2015-07-20 17:12:02 +08:00
|
|
|
/* We skip rta_lookup() here */
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
e = rte_get_temp(ap);
|
1998-12-07 07:13:31 +08:00
|
|
|
e->pflags = 0;
|
2015-07-20 17:12:02 +08:00
|
|
|
|
|
|
|
if (r->cmds)
|
|
|
|
f_eval_rte(r->cmds, &e, static_lp);
|
|
|
|
|
2016-04-08 19:08:03 +08:00
|
|
|
rte_update(p, r->net, e);
|
2015-07-20 17:12:02 +08:00
|
|
|
|
|
|
|
if (r->cmds)
|
|
|
|
lp_flush(static_lp);
|
1998-12-07 07:13:31 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
static void
|
|
|
|
static_bfd_notify(struct bfd_request *req);
|
|
|
|
|
|
|
|
static void
|
|
|
|
static_update_bfd(struct proto *p, struct static_route *r)
|
|
|
|
{
|
|
|
|
struct neighbor *nb = r->neigh;
|
|
|
|
int bfd_up = (nb->scope > 0) && r->use_bfd;
|
|
|
|
|
|
|
|
if (bfd_up && !r->bfd_req)
|
|
|
|
{
|
|
|
|
// ip_addr local = ipa_nonzero(r->local) ? r->local : nb->ifa->ip;
|
|
|
|
r->bfd_req = bfd_request_session(p->pool, r->via, nb->ifa->ip, nb->iface,
|
|
|
|
static_bfd_notify, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bfd_up && r->bfd_req)
|
|
|
|
{
|
|
|
|
rfree(r->bfd_req);
|
|
|
|
r->bfd_req = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 06:34:36 +08:00
|
|
|
static int
|
|
|
|
static_decide(struct static_config *cf, struct static_route *r)
|
|
|
|
{
|
|
|
|
/* r->dest != RTD_MULTIPATH, but may be RTD_NONE (part of multipath route)
|
|
|
|
the route also have to be valid (r->neigh != NULL) */
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
r->state &= ~STS_WANT;
|
|
|
|
|
2012-01-01 19:02:20 +08:00
|
|
|
if (r->neigh->scope < 0)
|
2010-12-08 06:34:36 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-01-01 19:02:20 +08:00
|
|
|
if (cf->check_link && !(r->neigh->iface->flags & IF_LINK_UP))
|
2010-12-08 06:34:36 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
if (r->bfd_req && r->bfd_req->state != BFD_STATE_UP)
|
|
|
|
return 0;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
r->state |= STS_WANT;
|
2010-12-08 06:34:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
static void
|
2010-11-11 19:24:27 +08:00
|
|
|
static_add(struct proto *p, struct static_config *cf, struct static_route *r)
|
2000-01-17 20:38:50 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r->mp_head && r != r->mp_head)
|
|
|
|
return;
|
|
|
|
|
2015-11-12 09:03:59 +08:00
|
|
|
DBG("static_add(%N,%d)\n", r->net, r->dest);
|
2000-01-17 20:38:50 +08:00
|
|
|
switch (r->dest)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
case RTD_UNICAST:
|
2010-12-08 06:34:36 +08:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
struct static_route *r2;
|
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
for (r2 = r; r2; r2 = r2->mp_next)
|
2010-12-08 06:34:36 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
if (ipa_zero(r2->via)) // No struct neighbor for device routes
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct neighbor *n = neigh_find2(p, &r2->via, r2->iface, NEF_STICKY);
|
2010-12-08 06:34:36 +08:00
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
r2->chain = n->data;
|
|
|
|
n->data = r2;
|
|
|
|
r2->neigh = n;
|
2015-07-25 00:02:07 +08:00
|
|
|
|
|
|
|
static_update_bfd(p, r2);
|
2016-06-13 21:49:53 +08:00
|
|
|
static_decide(cf,r2);
|
2016-05-06 21:48:35 +08:00
|
|
|
count++;
|
2010-12-08 06:34:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log(L_ERR "Static route destination %I is invalid. Ignoring.", r2->via);
|
2016-05-06 21:48:35 +08:00
|
|
|
r2->state = 0;
|
2010-12-08 06:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
2016-06-07 17:46:07 +08:00
|
|
|
static_install(p, r);
|
2016-05-06 21:48:35 +08:00
|
|
|
|
2010-12-08 06:34:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
default:
|
2016-06-07 17:46:07 +08:00
|
|
|
static_install(p, r);
|
2000-01-17 20:38:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
static void
|
2016-10-14 21:37:04 +08:00
|
|
|
static_rte_cleanup(struct proto *p UNUSED, struct static_route *r)
|
2015-07-25 00:02:07 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r->mp_head && (r != r->mp_head))
|
|
|
|
return;
|
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
struct static_route *r2;
|
2016-05-06 21:48:35 +08:00
|
|
|
|
|
|
|
for (r2 = r; r2; r2 = r2->mp_next)
|
|
|
|
if (r2->bfd_req)
|
|
|
|
{
|
|
|
|
rfree(r2->bfd_req);
|
|
|
|
r2->bfd_req = NULL;
|
|
|
|
}
|
2015-07-25 00:02:07 +08:00
|
|
|
}
|
|
|
|
|
1999-02-06 05:38:22 +08:00
|
|
|
static int
|
|
|
|
static_start(struct proto *p)
|
1998-12-07 02:21:23 +08:00
|
|
|
{
|
2011-09-24 08:21:52 +08:00
|
|
|
struct static_config *cf = (void *) p->cf;
|
1998-12-07 07:13:31 +08:00
|
|
|
struct static_route *r;
|
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
DBG("Static: take off!\n");
|
2011-09-24 08:21:52 +08:00
|
|
|
|
2015-07-20 17:12:02 +08:00
|
|
|
if (!static_lp)
|
|
|
|
static_lp = lp_new(&root_pool, 1008);
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
if (cf->igp_table)
|
|
|
|
rt_lock_table(cf->igp_table->table);
|
|
|
|
|
2012-04-15 21:13:12 +08:00
|
|
|
/* We have to go UP before routes could be installed */
|
|
|
|
proto_notify_state(p, PS_UP);
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->neigh_routes)
|
2011-09-24 08:21:52 +08:00
|
|
|
static_add(p, cf, r);
|
2016-06-13 21:49:53 +08:00
|
|
|
|
|
|
|
WALK_LIST(r, cf->iface_routes)
|
|
|
|
static_add(p, cf, r);
|
|
|
|
|
|
|
|
WALK_LIST(r, cf->other_routes)
|
|
|
|
static_install(p, r);
|
|
|
|
|
1999-02-06 05:38:22 +08:00
|
|
|
return PS_UP;
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
2000-05-13 19:01:41 +08:00
|
|
|
static int
|
|
|
|
static_shutdown(struct proto *p)
|
|
|
|
{
|
2011-09-24 08:21:52 +08:00
|
|
|
struct static_config *cf = (void *) p->cf;
|
2000-05-13 19:01:41 +08:00
|
|
|
struct static_route *r;
|
|
|
|
|
2010-02-27 23:00:07 +08:00
|
|
|
/* Just reset the flag, the routes will be flushed by the nest */
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->other_routes)
|
|
|
|
{
|
|
|
|
static_rte_cleanup(p, r);
|
|
|
|
r->state = 0;
|
|
|
|
}
|
2011-09-24 08:21:52 +08:00
|
|
|
WALK_LIST(r, cf->iface_routes)
|
2016-05-06 21:48:35 +08:00
|
|
|
r->state = 0;
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->neigh_routes)
|
2015-07-25 00:02:07 +08:00
|
|
|
{
|
|
|
|
static_rte_cleanup(p, r);
|
2016-05-06 21:48:35 +08:00
|
|
|
r->state = 0;
|
2015-07-25 00:02:07 +08:00
|
|
|
}
|
2010-02-27 23:00:07 +08:00
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
/* Handle failure during channel reconfigure */
|
|
|
|
/* FIXME: This should be handled in a better way */
|
|
|
|
cf = (void *) p->cf_new;
|
|
|
|
if (cf)
|
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->other_routes)
|
|
|
|
r->state = 0;
|
2016-01-26 18:48:58 +08:00
|
|
|
WALK_LIST(r, cf->iface_routes)
|
2016-05-06 21:48:35 +08:00
|
|
|
r->state = 0;
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->neigh_routes)
|
2016-05-06 21:48:35 +08:00
|
|
|
r->state = 0;
|
2016-01-26 18:48:58 +08:00
|
|
|
}
|
|
|
|
|
2000-05-13 19:01:41 +08:00
|
|
|
return PS_DOWN;
|
|
|
|
}
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
static void
|
|
|
|
static_cleanup(struct proto *p)
|
|
|
|
{
|
|
|
|
struct static_config *cf = (void *) p->cf;
|
|
|
|
|
|
|
|
if (cf->igp_table)
|
|
|
|
rt_unlock_table(cf->igp_table->table);
|
|
|
|
}
|
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
static void
|
|
|
|
static_update_rte(struct proto *p, struct static_route *r)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
if (r->dest != RTD_UNICAST)
|
|
|
|
return;
|
2015-07-25 00:02:07 +08:00
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
static_decide((struct static_config *) p->cf, r);
|
|
|
|
static_install(p, r);
|
2015-07-25 00:02:07 +08:00
|
|
|
}
|
2011-09-24 08:21:52 +08:00
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
static void
|
|
|
|
static_neigh_notify(struct neighbor *n)
|
|
|
|
{
|
1999-02-06 05:38:22 +08:00
|
|
|
struct proto *p = n->proto;
|
1998-12-09 02:31:31 +08:00
|
|
|
struct static_route *r;
|
|
|
|
|
2011-04-01 18:21:18 +08:00
|
|
|
DBG("Static: neighbor notify for %I: iface %p\n", n->addr, n->iface);
|
1998-12-09 02:31:31 +08:00
|
|
|
for(r=n->data; r; r=r->chain)
|
2015-07-25 00:02:07 +08:00
|
|
|
{
|
|
|
|
static_update_bfd(p, r);
|
|
|
|
static_update_rte(p, r);
|
|
|
|
}
|
|
|
|
}
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
static void
|
|
|
|
static_bfd_notify(struct bfd_request *req)
|
|
|
|
{
|
|
|
|
struct static_route *r = req->data;
|
|
|
|
struct proto *p = r->neigh->proto;
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
// if (req->down) TRACE(D_EVENTS, "BFD session down for nbr %I on %s", XXXX);
|
|
|
|
|
|
|
|
static_update_rte(p, r);
|
1998-12-07 07:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
static_dump_rt(struct static_route *r)
|
|
|
|
{
|
2015-11-12 09:03:59 +08:00
|
|
|
debug("%-1N: ", r->net);
|
2016-05-06 21:48:35 +08:00
|
|
|
if (r->dest == RTD_UNICAST)
|
|
|
|
if (ipa_zero(r->via))
|
1998-12-07 07:13:31 +08:00
|
|
|
debug("dev %s\n", r->if_name);
|
2016-05-06 21:48:35 +08:00
|
|
|
else
|
|
|
|
debug("via %I\n", r->via);
|
|
|
|
else
|
|
|
|
debug("rtd %d\n", r->dest);
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-02-06 05:38:22 +08:00
|
|
|
static_dump(struct proto *p)
|
1998-12-07 02:21:23 +08:00
|
|
|
{
|
1999-02-06 05:38:22 +08:00
|
|
|
struct static_config *c = (void *) p->cf;
|
1998-12-07 07:13:31 +08:00
|
|
|
struct static_route *r;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
debug("Independent static nexthops:\n");
|
|
|
|
WALK_LIST(r, c->neigh_routes)
|
1998-12-07 07:13:31 +08:00
|
|
|
static_dump_rt(r);
|
2016-06-13 21:49:53 +08:00
|
|
|
debug("Device static nexthops:\n");
|
1999-02-06 05:38:22 +08:00
|
|
|
WALK_LIST(r, c->iface_routes)
|
1998-12-07 07:13:31 +08:00
|
|
|
static_dump_rt(r);
|
2016-06-13 21:49:53 +08:00
|
|
|
debug("Other static routes:\n");
|
|
|
|
WALK_LIST(r, c->other_routes)
|
|
|
|
static_dump_rt(r);
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 05:59:48 +08:00
|
|
|
static void
|
1999-05-07 05:38:11 +08:00
|
|
|
static_if_notify(struct proto *p, unsigned flags, struct iface *i)
|
1999-02-14 05:59:48 +08:00
|
|
|
{
|
|
|
|
struct static_route *r;
|
|
|
|
struct static_config *c = (void *) p->cf;
|
|
|
|
|
|
|
|
if (flags & IF_CHANGE_UP)
|
|
|
|
{
|
|
|
|
WALK_LIST(r, c->iface_routes)
|
1999-05-07 05:38:11 +08:00
|
|
|
if (!strcmp(r->if_name, i->name))
|
2016-06-13 21:49:53 +08:00
|
|
|
{
|
|
|
|
r->state |= STS_WANT;
|
|
|
|
r->iface = i;
|
2016-06-07 17:46:07 +08:00
|
|
|
static_install(p, r);
|
2016-06-13 21:49:53 +08:00
|
|
|
}
|
1999-02-14 05:59:48 +08:00
|
|
|
}
|
|
|
|
else if (flags & IF_CHANGE_DOWN)
|
|
|
|
{
|
|
|
|
WALK_LIST(r, c->iface_routes)
|
1999-05-07 05:38:11 +08:00
|
|
|
if (!strcmp(r->if_name, i->name))
|
2016-06-13 21:49:53 +08:00
|
|
|
{
|
|
|
|
r->state &= ~STS_WANT;
|
|
|
|
r->iface = NULL;
|
|
|
|
static_install(p, r);
|
|
|
|
}
|
1999-02-14 05:59:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 08:20:43 +08:00
|
|
|
int
|
2016-10-14 21:37:04 +08:00
|
|
|
static_rte_mergable(rte *pri UNUSED, rte *sec UNUSED)
|
2015-06-08 08:20:43 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
void
|
1999-02-06 05:38:22 +08:00
|
|
|
static_init_config(struct static_config *c)
|
1998-12-07 02:21:23 +08:00
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
init_list(&c->neigh_routes);
|
1999-02-06 05:38:22 +08:00
|
|
|
init_list(&c->iface_routes);
|
|
|
|
init_list(&c->other_routes);
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
static void
|
|
|
|
static_postconfig(struct proto_config *CF)
|
|
|
|
{
|
|
|
|
struct static_config *cf = (void *) CF;
|
|
|
|
struct static_route *r;
|
|
|
|
|
|
|
|
if (EMPTY_LIST(CF->channels))
|
|
|
|
cf_error("Channel not specified");
|
|
|
|
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, cf->neigh_routes)
|
|
|
|
if (r->net && (r->net->type != CF->net_type))
|
|
|
|
cf_error("Route %N incompatible with channel type", r->net);
|
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
WALK_LIST(r, cf->iface_routes)
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r->net && (r->net->type != CF->net_type))
|
2016-01-26 18:48:58 +08:00
|
|
|
cf_error("Route %N incompatible with channel type", r->net);
|
|
|
|
|
|
|
|
WALK_LIST(r, cf->other_routes)
|
|
|
|
if (r->net->type != CF->net_type)
|
|
|
|
cf_error("Route %N incompatible with channel type", r->net);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-06 05:38:22 +08:00
|
|
|
static struct proto *
|
2016-01-26 18:48:58 +08:00
|
|
|
static_init(struct proto_config *CF)
|
1998-12-07 02:21:23 +08:00
|
|
|
{
|
2016-01-26 18:48:58 +08:00
|
|
|
struct proto *P = proto_new(CF);
|
|
|
|
// struct static_proto *p = (void *) P;
|
|
|
|
// struct static_config *cf = (void *) CF;
|
1998-12-07 02:21:23 +08:00
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF));
|
2012-08-14 22:25:22 +08:00
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
P->neigh_notify = static_neigh_notify;
|
|
|
|
P->if_notify = static_if_notify;
|
|
|
|
P->rte_mergable = static_rte_mergable;
|
|
|
|
|
|
|
|
return P;
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
2010-05-16 17:03:59 +08:00
|
|
|
static inline int
|
|
|
|
static_same_dest(struct static_route *x, struct static_route *y)
|
|
|
|
{
|
2010-12-08 06:34:36 +08:00
|
|
|
if (x->dest != y->dest)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (x->dest)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
case RTD_UNICAST:
|
|
|
|
{
|
|
|
|
struct static_route *xc, *yc;
|
|
|
|
for (xc = x, yc = y; xc && yc; xc = xc->mp_next, yc = yc->mp_next)
|
|
|
|
{
|
|
|
|
if (ipa_nonzero(xc->via) && ipa_nonzero(yc->via))
|
|
|
|
{
|
|
|
|
if (!ipa_equal(x->via, y->via) ||
|
2016-06-13 21:49:53 +08:00
|
|
|
(x->iface != y->iface) ||
|
2016-05-06 21:48:35 +08:00
|
|
|
(x->use_bfd != y->use_bfd) ||
|
2016-06-13 21:49:53 +08:00
|
|
|
(x->weight != y->weight) ||
|
|
|
|
(x->label_count != y->label_count))
|
2016-05-06 21:48:35 +08:00
|
|
|
return 0;
|
2016-06-13 21:49:53 +08:00
|
|
|
for (int i=0; i<x->label_count; i++)
|
|
|
|
if (x->label_stack[i] != y->label_stack[i])
|
|
|
|
return 0;
|
2016-05-06 21:48:35 +08:00
|
|
|
}
|
|
|
|
else
|
2016-06-13 21:49:53 +08:00
|
|
|
if ((!x->if_name) || (!y->if_name) ||
|
|
|
|
strcmp(x->if_name, y->if_name) ||
|
2016-05-06 21:48:35 +08:00
|
|
|
(x->use_bfd != y->use_bfd) ||
|
|
|
|
(x->weight != y->weight))
|
|
|
|
return 0;
|
2016-06-13 21:49:53 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
case RTDX_RECURSIVE:
|
|
|
|
return ipa_equal(x->via, y->via);
|
|
|
|
|
2010-12-08 06:34:36 +08:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
2000-01-17 20:38:50 +08:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:12:02 +08:00
|
|
|
static inline int
|
|
|
|
static_same_rte(struct static_route *x, struct static_route *y)
|
|
|
|
{
|
|
|
|
return static_same_dest(x, y) && i_same(x->cmds, y->cmds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
static void
|
|
|
|
static_match(struct proto *p, struct static_route *r, struct static_config *n)
|
|
|
|
{
|
|
|
|
struct static_route *t;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r->mp_head && (r->mp_head != r))
|
|
|
|
return;
|
|
|
|
|
2010-05-16 17:03:59 +08:00
|
|
|
/*
|
|
|
|
* For given old route *r we find whether a route to the same
|
|
|
|
* network is also in the new route list. In that case, we keep the
|
|
|
|
* route and possibly update the route later if destination changed.
|
|
|
|
* Otherwise, we remove the route.
|
|
|
|
*/
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
if (r->neigh)
|
|
|
|
r->neigh->data = NULL;
|
2014-02-26 20:25:39 +08:00
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(t, n->neigh_routes)
|
|
|
|
if ((!t->mp_head || (t->mp_head == t)) && net_equal(r->net, t->net))
|
|
|
|
goto found;
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
WALK_LIST(t, n->iface_routes)
|
2016-06-13 21:49:53 +08:00
|
|
|
if ((!t->mp_head || (t->mp_head == t)) && net_equal(r->net, t->net))
|
2014-02-26 20:25:39 +08:00
|
|
|
goto found;
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
WALK_LIST(t, n->other_routes)
|
2015-11-12 09:03:59 +08:00
|
|
|
if (net_equal(r->net, t->net))
|
2014-02-26 20:25:39 +08:00
|
|
|
goto found;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
r->state &= ~STS_WANT;
|
|
|
|
static_install(p, r);
|
2014-02-26 20:25:39 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
found:
|
2016-06-13 21:49:53 +08:00
|
|
|
t->state = r->state;
|
|
|
|
|
2014-02-26 20:25:39 +08:00
|
|
|
/* If destination is different, force reinstall */
|
2016-06-13 21:49:53 +08:00
|
|
|
if (!static_same_rte(r, t))
|
|
|
|
t->state |= STS_FORCE;
|
2000-01-17 20:38:50 +08:00
|
|
|
}
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
static inline rtable *
|
|
|
|
cf_igp_table(struct static_config *cf)
|
|
|
|
{
|
|
|
|
return cf->igp_table ? cf->igp_table->table : NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-17 19:52:50 +08:00
|
|
|
static int
|
2016-01-26 18:48:58 +08:00
|
|
|
static_reconfigure(struct proto *p, struct proto_config *CF)
|
2000-01-17 19:52:50 +08:00
|
|
|
{
|
2000-01-17 20:38:50 +08:00
|
|
|
struct static_config *o = (void *) p->cf;
|
2016-01-26 18:48:58 +08:00
|
|
|
struct static_config *n = (void *) CF;
|
2000-01-17 20:38:50 +08:00
|
|
|
struct static_route *r;
|
|
|
|
|
2011-09-24 08:21:52 +08:00
|
|
|
if (cf_igp_table(o) != cf_igp_table(n))
|
|
|
|
return 0;
|
|
|
|
|
2016-01-26 18:48:58 +08:00
|
|
|
if (!proto_configure_channel(p, &p->main_channel, proto_cf_main_channel(CF)))
|
|
|
|
return 0;
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
/* Delete all obsolete routes and reset neighbor entries */
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, o->other_routes)
|
|
|
|
static_match(p, r, n);
|
2000-01-17 20:38:50 +08:00
|
|
|
WALK_LIST(r, o->iface_routes)
|
|
|
|
static_match(p, r, n);
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, o->neigh_routes)
|
2000-01-17 20:38:50 +08:00
|
|
|
static_match(p, r, n);
|
|
|
|
|
|
|
|
/* Now add all new routes, those not changed will be ignored by static_install() */
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, n->neigh_routes)
|
|
|
|
static_add(p, n, r);
|
|
|
|
WALK_LIST(r, o->neigh_routes)
|
|
|
|
static_rte_cleanup(p, r);
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
WALK_LIST(r, n->iface_routes)
|
|
|
|
{
|
|
|
|
struct iface *ifa;
|
2013-02-09 07:53:04 +08:00
|
|
|
if ((ifa = if_find_by_name(r->if_name)) && (ifa->flags & IF_UP))
|
2016-06-13 21:49:53 +08:00
|
|
|
{
|
|
|
|
r->iface = ifa;
|
|
|
|
static_install(p, r);
|
|
|
|
}
|
2000-01-17 20:38:50 +08:00
|
|
|
}
|
2016-06-13 21:49:53 +08:00
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
WALK_LIST(r, n->other_routes)
|
2016-06-13 21:49:53 +08:00
|
|
|
{
|
|
|
|
r->state |= STS_WANT;
|
|
|
|
static_install(p, r);
|
|
|
|
}
|
2000-01-17 20:38:50 +08:00
|
|
|
|
2015-07-25 00:02:07 +08:00
|
|
|
WALK_LIST(r, o->other_routes)
|
|
|
|
static_rte_cleanup(p, r);
|
|
|
|
|
2000-01-17 20:38:50 +08:00
|
|
|
return 1;
|
2000-01-17 19:52:50 +08:00
|
|
|
}
|
|
|
|
|
2011-11-07 07:31:23 +08:00
|
|
|
static void
|
|
|
|
static_copy_routes(list *dlst, list *slst)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct static_route *sr;
|
2011-11-07 07:31:23 +08:00
|
|
|
|
|
|
|
init_list(dlst);
|
|
|
|
WALK_LIST(sr, *slst)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct static_route *srr, *drr = NULL;
|
2016-06-13 21:49:53 +08:00
|
|
|
for (srr = sr->mp_head; srr; srr = srr->mp_next)
|
2016-05-06 21:48:35 +08:00
|
|
|
{
|
|
|
|
/* copy one route */
|
|
|
|
struct static_route *dr = cfg_alloc(sizeof(struct static_route));
|
|
|
|
if (drr)
|
|
|
|
drr->mp_next = dr;
|
|
|
|
else
|
|
|
|
add_tail(dlst, &(dr->n));
|
2011-11-07 07:31:23 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
memcpy(dr, sr, sizeof(struct static_route));
|
|
|
|
drr = dr;
|
|
|
|
}
|
2011-11-07 07:31:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
static_copy_config(struct proto_config *dest, struct proto_config *src)
|
|
|
|
{
|
|
|
|
struct static_config *d = (struct static_config *) dest;
|
|
|
|
struct static_config *s = (struct static_config *) src;
|
|
|
|
|
|
|
|
/* Copy route lists */
|
2016-06-13 21:49:53 +08:00
|
|
|
static_copy_routes(&d->neigh_routes, &s->neigh_routes);
|
2011-11-07 07:31:23 +08:00
|
|
|
static_copy_routes(&d->iface_routes, &s->iface_routes);
|
|
|
|
static_copy_routes(&d->other_routes, &s->other_routes);
|
|
|
|
}
|
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
struct protocol proto_static = {
|
2014-12-03 17:10:34 +08:00
|
|
|
.name = "Static",
|
|
|
|
.template = "static%d",
|
|
|
|
.preference = DEF_PREF_STATIC,
|
2016-01-26 18:48:58 +08:00
|
|
|
.channel_mask = NB_ANY,
|
|
|
|
.proto_size = sizeof(struct proto),
|
2015-02-22 04:08:23 +08:00
|
|
|
.config_size = sizeof(struct static_config),
|
2016-01-26 18:48:58 +08:00
|
|
|
.postconfig = static_postconfig,
|
2014-12-03 17:10:34 +08:00
|
|
|
.init = static_init,
|
|
|
|
.dump = static_dump,
|
|
|
|
.start = static_start,
|
|
|
|
.shutdown = static_shutdown,
|
|
|
|
.cleanup = static_cleanup,
|
|
|
|
.reconfigure = static_reconfigure,
|
|
|
|
.copy_config = static_copy_config
|
1998-12-07 02:21:23 +08:00
|
|
|
};
|
1999-12-03 19:41:23 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
static byte *
|
|
|
|
static_format_via(struct static_route *r)
|
1999-12-03 19:41:23 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
static byte via[IPA_MAX_TEXT_LENGTH + 25];
|
1999-12-03 19:41:23 +08:00
|
|
|
|
|
|
|
switch (r->dest)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
case RTD_UNICAST: if (ipa_zero(r->via)) bsprintf(via, "dev %s", r->if_name);
|
2016-06-13 21:49:53 +08:00
|
|
|
else bsprintf(via, "via %I%J", r->via, r->iface);
|
2016-05-06 21:48:35 +08:00
|
|
|
break;
|
1999-12-03 19:41:23 +08:00
|
|
|
case RTD_BLACKHOLE: bsprintf(via, "blackhole"); break;
|
2010-12-08 06:34:36 +08:00
|
|
|
case RTD_UNREACHABLE: bsprintf(via, "unreachable"); break;
|
1999-12-03 19:41:23 +08:00
|
|
|
case RTD_PROHIBIT: bsprintf(via, "prohibited"); break;
|
2011-09-24 08:21:52 +08:00
|
|
|
case RTDX_RECURSIVE: bsprintf(via, "recursive %I", r->via); break;
|
1999-12-03 19:41:23 +08:00
|
|
|
default: bsprintf(via, "???");
|
|
|
|
}
|
2016-05-06 21:48:35 +08:00
|
|
|
return via;
|
|
|
|
}
|
2010-12-08 06:34:36 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
static void
|
|
|
|
static_show_rt(struct static_route *r)
|
|
|
|
{
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r->mp_head && (r != r->mp_head))
|
|
|
|
return;
|
2016-05-06 21:48:35 +08:00
|
|
|
if (r->mp_next)
|
|
|
|
{
|
|
|
|
cli_msg(-1009, "%N", r->net);
|
|
|
|
struct static_route *r2;
|
|
|
|
for (r2 = r; r2; r2 = r2->mp_next)
|
2016-06-13 21:49:53 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
cli_msg(-1009, "\t%s weight %d%s%s", static_format_via(r2), r2->weight + 1,
|
|
|
|
r2->bfd_req ? " (bfd)" : "", (r2->state & STS_INSTALLED) ? "" : " (dormant)");
|
2016-06-13 21:49:53 +08:00
|
|
|
if (r2->mp_next == r)
|
|
|
|
break;
|
|
|
|
}
|
2016-05-06 21:48:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cli_msg(-1009, "%N %s%s%s", r->net, static_format_via(r),
|
|
|
|
r->bfd_req ? " (bfd)" : "", (r->state & STS_INSTALLED) ? "" : " (dormant)");
|
1999-12-03 19:41:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
static_show(struct proto *P)
|
|
|
|
{
|
|
|
|
struct static_config *c = (void *) P->cf;
|
|
|
|
struct static_route *r;
|
|
|
|
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, c->neigh_routes)
|
1999-12-03 19:41:23 +08:00
|
|
|
static_show_rt(r);
|
|
|
|
WALK_LIST(r, c->iface_routes)
|
|
|
|
static_show_rt(r);
|
2016-06-13 21:49:53 +08:00
|
|
|
WALK_LIST(r, c->other_routes)
|
|
|
|
static_show_rt(r);
|
1999-12-03 19:41:23 +08:00
|
|
|
cli_msg(0, "");
|
|
|
|
}
|