1998-12-07 02:21:23 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Static Route Generator
|
|
|
|
*
|
1999-02-06 05:38:22 +08:00
|
|
|
* (c) 1998--1999 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
1998-12-07 07:13:31 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "conf/conf.h"
|
|
|
|
|
|
|
|
#include "static.h"
|
|
|
|
|
1998-12-07 07:13:31 +08:00
|
|
|
static void
|
1999-02-06 05:38:22 +08:00
|
|
|
static_install(struct proto *p, struct static_route *r, struct iface *ifa)
|
1998-12-07 07:13:31 +08:00
|
|
|
{
|
|
|
|
net *n;
|
|
|
|
rta a, *aa;
|
|
|
|
rte *e;
|
|
|
|
|
|
|
|
DBG("Installing static route %I/%d, rtd=%d\n", r->net, r->masklen, r->dest);
|
|
|
|
bzero(&a, sizeof(a));
|
1999-02-06 05:38:22 +08:00
|
|
|
a.proto = p;
|
1998-12-07 07:13:31 +08:00
|
|
|
a.source = (r->dest == RTD_DEVICE) ? RTS_STATIC_DEVICE : RTS_STATIC;
|
|
|
|
a.scope = SCOPE_UNIVERSE;
|
|
|
|
a.cast = RTC_UNICAST;
|
|
|
|
a.dest = r->dest;
|
|
|
|
a.tos = 0;
|
|
|
|
a.gw = r->via;
|
|
|
|
a.iface = ifa;
|
|
|
|
aa = rta_lookup(&a);
|
|
|
|
|
|
|
|
n = net_get(&master_table, a.tos, r->net, r->masklen);
|
|
|
|
e = rte_get_temp(aa);
|
|
|
|
e->net = n;
|
|
|
|
e->pflags = 0;
|
1999-02-06 05:38:22 +08:00
|
|
|
rte_update(n, p, e);
|
1998-12-07 07:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-02-06 05:38:22 +08:00
|
|
|
static_remove(struct proto *p, struct static_route *r)
|
1998-12-07 07:13:31 +08:00
|
|
|
{
|
|
|
|
net *n;
|
|
|
|
|
|
|
|
DBG("Removing static route %I/%d\n", r->net, r->masklen);
|
|
|
|
n = net_find(&master_table, 0, r->net, r->masklen);
|
|
|
|
if (n)
|
1999-02-06 05:38:22 +08:00
|
|
|
rte_update(n, p, NULL);
|
1998-12-07 07:13:31 +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
|
|
|
{
|
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;
|
|
|
|
|
1998-12-07 02:21:23 +08:00
|
|
|
DBG("Static: take off!\n");
|
1999-02-06 05:38:22 +08:00
|
|
|
WALK_LIST(r, c->other_routes)
|
1998-12-09 02:31:31 +08:00
|
|
|
switch (r->dest)
|
1998-12-07 07:13:31 +08:00
|
|
|
{
|
1998-12-09 02:31:31 +08:00
|
|
|
case RTD_ROUTER:
|
|
|
|
{
|
1999-02-06 05:38:22 +08:00
|
|
|
struct neighbor *n = neigh_find(p, &r->via, NEF_STICKY);
|
1998-12-09 02:31:31 +08:00
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
r->chain = n->data;
|
|
|
|
n->data = r;
|
|
|
|
r->neigh = n;
|
|
|
|
static_install(p, r, n->iface);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log(L_ERR "Static route destination %I is invalid. Ignoring.\n", r->via);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RTD_DEVICE:
|
1999-02-14 05:59:48 +08:00
|
|
|
break;
|
1998-12-09 02:31:31 +08:00
|
|
|
default:
|
|
|
|
static_install(p, r, NULL);
|
1998-12-07 07:13:31 +08:00
|
|
|
}
|
1999-02-06 05:38:22 +08:00
|
|
|
return PS_UP;
|
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;
|
|
|
|
|
1998-12-07 07:13:31 +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)
|
|
|
|
if (n->iface)
|
|
|
|
static_install(p, r, n->iface);
|
|
|
|
else
|
|
|
|
static_remove(p, r);
|
1998-12-07 07:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
static_dump_rt(struct static_route *r)
|
|
|
|
{
|
|
|
|
debug("%16I/%2d: ", r->net, r->masklen);
|
|
|
|
switch (r->dest)
|
|
|
|
{
|
|
|
|
case RTD_ROUTER:
|
|
|
|
debug("via %I\n", r->via);
|
|
|
|
break;
|
|
|
|
case RTD_DEVICE:
|
|
|
|
debug("dev %s\n", r->if_name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("rtd %d\n", r->dest);
|
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
debug("Independent static routes:\n");
|
1999-02-06 05:38:22 +08:00
|
|
|
WALK_LIST(r, c->other_routes)
|
1998-12-07 07:13:31 +08:00
|
|
|
static_dump_rt(r);
|
|
|
|
debug("Device static routes:\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);
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 05:59:48 +08:00
|
|
|
static void
|
|
|
|
static_if_notify(struct proto *p, unsigned flags, struct iface *new, struct iface *old)
|
|
|
|
{
|
|
|
|
struct static_route *r;
|
|
|
|
struct static_config *c = (void *) p->cf;
|
|
|
|
|
|
|
|
if (flags & IF_CHANGE_UP)
|
|
|
|
{
|
|
|
|
WALK_LIST(r, c->iface_routes)
|
|
|
|
if (!strcmp(r->if_name, new->name))
|
|
|
|
static_install(p, r, new);
|
|
|
|
}
|
|
|
|
else if (flags & IF_CHANGE_DOWN)
|
|
|
|
{
|
|
|
|
WALK_LIST(r, c->iface_routes)
|
|
|
|
if (!strcmp(r->if_name, old->name))
|
|
|
|
static_remove(p, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
1999-02-06 05:38:22 +08:00
|
|
|
c->c.preference = DEF_PREF_STATIC;
|
|
|
|
init_list(&c->iface_routes);
|
|
|
|
init_list(&c->other_routes);
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
1999-02-06 05:38:22 +08:00
|
|
|
static struct proto *
|
|
|
|
static_init(struct proto_config *c)
|
1998-12-07 02:21:23 +08:00
|
|
|
{
|
1999-02-06 05:38:22 +08:00
|
|
|
struct proto *p = proto_new(c, sizeof(struct proto));
|
1998-12-07 02:21:23 +08:00
|
|
|
|
1999-02-06 05:38:22 +08:00
|
|
|
p->neigh_notify = static_neigh_notify;
|
1999-02-14 05:59:48 +08:00
|
|
|
p->if_notify = static_if_notify;
|
1999-02-06 05:38:22 +08:00
|
|
|
return p;
|
1998-12-07 02:21:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct protocol proto_static = {
|
1999-02-06 05:38:22 +08:00
|
|
|
name: "Static",
|
|
|
|
init: static_init,
|
|
|
|
dump: static_dump,
|
|
|
|
start: static_start,
|
1998-12-07 02:21:23 +08:00
|
|
|
};
|