1998-05-27 05:42:05 +08:00
|
|
|
/*
|
1998-06-02 05:41:11 +08:00
|
|
|
* BIRD -- Management of Interfaces and Neighbor Cache
|
1998-05-27 05:42:05 +08:00
|
|
|
*
|
1999-02-06 05:37:34 +08:00
|
|
|
* (c) 1998--1999 Martin Mares <mj@ucw.cz>
|
1998-05-27 05:42:05 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
1998-06-02 05:41:11 +08:00
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
1998-06-02 05:41:11 +08:00
|
|
|
#include "nest/protocol.h"
|
1998-05-27 05:42:05 +08:00
|
|
|
#include "lib/resource.h"
|
1998-11-30 06:01:03 +08:00
|
|
|
#include "lib/string.h"
|
1998-05-27 05:42:05 +08:00
|
|
|
|
|
|
|
static pool *if_pool;
|
|
|
|
|
1998-06-02 05:41:11 +08:00
|
|
|
/*
|
|
|
|
* Neighbor Cache
|
|
|
|
*
|
|
|
|
* FIXME: Use hashing to get some real speed.
|
|
|
|
* FIXME: Cleanup when a protocol goes down.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static slab *neigh_slab;
|
|
|
|
static list neigh_list;
|
|
|
|
|
|
|
|
static int
|
|
|
|
if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
|
|
|
|
{
|
|
|
|
if (i->flags & (IF_ADMIN_DOWN | IF_IGNORE))
|
|
|
|
return 0;
|
|
|
|
if ((i->flags & IF_UNNUMBERED) && ipa_equal(*a, i->opposite))
|
|
|
|
return 1;
|
|
|
|
if (!ipa_in_net(*a, i->prefix, i->pxlen))
|
|
|
|
return 0;
|
|
|
|
if (ipa_equal(*a, i->prefix) || /* Network address */
|
|
|
|
ipa_equal(*a, i->brd) || /* Broadcast */
|
|
|
|
ipa_equal(*a, i->ip)) /* Our own address */
|
|
|
|
return -1;
|
|
|
|
if (!(i->flags & IF_UP))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
neighbor *
|
|
|
|
neigh_find(struct proto *p, ip_addr *a, unsigned flags)
|
|
|
|
{
|
|
|
|
neighbor *n;
|
|
|
|
int class;
|
|
|
|
struct iface *i, *j;
|
|
|
|
|
|
|
|
WALK_LIST(n, neigh_list)
|
|
|
|
if (n->proto == p && ipa_equal(*a, n->addr))
|
|
|
|
return n;
|
|
|
|
|
|
|
|
class = ipa_classify(*a);
|
|
|
|
if (class < 0) /* Invalid address */
|
|
|
|
return NULL;
|
|
|
|
if ((class & IADDR_SCOPE_MASK) < SCOPE_SITE ||
|
|
|
|
!(class & IADDR_HOST))
|
|
|
|
return NULL; /* Bad scope or a somecast */
|
|
|
|
|
|
|
|
j = NULL;
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
switch (if_connected(a, i))
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
return NULL;
|
|
|
|
case 1:
|
|
|
|
if (!j || j->pxlen > i->pxlen)
|
|
|
|
j = i;
|
|
|
|
/* Fall-thru */
|
|
|
|
}
|
|
|
|
if (!j && !(flags & NEF_STICKY))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n = sl_alloc(neigh_slab);
|
|
|
|
n->addr = *a;
|
|
|
|
n->iface = j;
|
|
|
|
add_tail(&neigh_list, &n->n);
|
|
|
|
if (j)
|
|
|
|
{
|
|
|
|
n->sibling = j->neigh;
|
|
|
|
j->neigh = n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
n->sibling = NULL;
|
|
|
|
n->proto = p;
|
|
|
|
n->data = NULL;
|
|
|
|
n->flags = flags;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
void
|
1998-06-02 05:41:11 +08:00
|
|
|
neigh_dump(neighbor *n)
|
|
|
|
{
|
1998-06-17 22:36:02 +08:00
|
|
|
debug("%p %I ", n, n->addr);
|
1998-06-02 05:41:11 +08:00
|
|
|
if (n->iface)
|
|
|
|
debug("%s ", n->iface->name);
|
|
|
|
else
|
|
|
|
debug("[] ");
|
1999-02-06 05:37:34 +08:00
|
|
|
debug("%s %p", n->proto->cf->name, n->data);
|
1998-06-02 05:41:11 +08:00
|
|
|
if (n->flags & NEF_STICKY)
|
1998-12-07 07:11:18 +08:00
|
|
|
debug(" STICKY");
|
1998-06-02 05:41:11 +08:00
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
neigh_dump_all(void)
|
|
|
|
{
|
|
|
|
neighbor *n;
|
|
|
|
|
|
|
|
debug("Known neighbors:\n");
|
|
|
|
WALK_LIST(n, neigh_list)
|
|
|
|
neigh_dump(n);
|
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
neigh_if_up(struct iface *i)
|
|
|
|
{
|
|
|
|
neighbor *n;
|
|
|
|
|
|
|
|
WALK_LIST(n, neigh_list)
|
|
|
|
if (!n->iface &&
|
|
|
|
if_connected(&n->addr, i) > 0)
|
|
|
|
{
|
|
|
|
n->iface = i;
|
|
|
|
n->sibling = i->neigh;
|
|
|
|
i->neigh = n;
|
1998-06-17 22:36:02 +08:00
|
|
|
DBG("Waking up sticky neighbor %I\n", n->addr);
|
1998-06-03 16:40:10 +08:00
|
|
|
if (n->proto->neigh_notify)
|
|
|
|
n->proto->neigh_notify(n);
|
1998-06-02 05:41:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
neigh_if_down(struct iface *i)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1998-06-02 05:41:11 +08:00
|
|
|
neighbor *n, *m;
|
1998-05-27 05:42:05 +08:00
|
|
|
|
1998-06-02 05:41:11 +08:00
|
|
|
for(m=i->neigh; n = m;)
|
|
|
|
{
|
|
|
|
m = n->sibling;
|
1998-06-17 22:36:02 +08:00
|
|
|
DBG("Flushing neighbor %I on %s\n", n->addr, n->iface->name);
|
1998-06-02 05:41:11 +08:00
|
|
|
n->iface = NULL;
|
1998-06-03 16:40:10 +08:00
|
|
|
if (n->proto->neigh_notify)
|
|
|
|
n->proto->neigh_notify(n);
|
1998-06-02 05:41:11 +08:00
|
|
|
if (!(n->flags & NEF_STICKY))
|
|
|
|
{
|
|
|
|
rem_node(&n->n);
|
|
|
|
sl_free(neigh_slab, n);
|
|
|
|
}
|
|
|
|
i->neigh = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Interface List
|
|
|
|
*/
|
|
|
|
|
|
|
|
list iface_list;
|
|
|
|
|
|
|
|
void
|
|
|
|
if_dump(struct iface *i)
|
|
|
|
{
|
1998-05-27 05:42:05 +08:00
|
|
|
debug("IF%d: %s", i->index, i->name);
|
|
|
|
if (i->flags & IF_ADMIN_DOWN)
|
|
|
|
debug(" ADMIN-DOWN");
|
|
|
|
if (i->flags & IF_UP)
|
|
|
|
debug(" UP");
|
|
|
|
if (i->flags & IF_MULTIACCESS)
|
|
|
|
debug(" MA");
|
|
|
|
if (i->flags & IF_UNNUMBERED)
|
|
|
|
debug(" UNNUM");
|
|
|
|
if (i->flags & IF_BROADCAST)
|
|
|
|
debug(" BC");
|
|
|
|
if (i->flags & IF_MULTICAST)
|
|
|
|
debug(" MC");
|
|
|
|
if (i->flags & IF_TUNNEL)
|
|
|
|
debug(" TUNL");
|
|
|
|
if (i->flags & IF_LOOPBACK)
|
|
|
|
debug(" LOOP");
|
|
|
|
if (i->flags & IF_IGNORE)
|
|
|
|
debug(" IGN");
|
|
|
|
debug(" MTU=%d\n", i->mtu);
|
1998-06-17 22:36:02 +08:00
|
|
|
debug("\t%I, net %I/%-2d bc %I -> %I\n", i->ip, i->prefix, i->pxlen, i->brd, i->opposite);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_dump_all(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
1998-06-05 04:30:11 +08:00
|
|
|
debug("Known network interfaces:\n");
|
1998-05-27 05:42:05 +08:00
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if_dump(i);
|
|
|
|
}
|
|
|
|
|
1998-06-02 05:41:11 +08:00
|
|
|
static inline int
|
|
|
|
if_change_too_big_p(struct iface *i, struct iface *j)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1998-06-02 05:41:11 +08:00
|
|
|
if (!ipa_equal(i->ip, j->ip) ||
|
|
|
|
!ipa_equal(i->prefix, j->prefix) ||
|
|
|
|
i->pxlen != j->pxlen ||
|
|
|
|
!ipa_equal(i->brd, j->brd) ||
|
|
|
|
!ipa_equal(i->opposite, j->opposite))
|
|
|
|
return 1; /* Changed addresses */
|
|
|
|
if ((i->flags ^ j->flags) & ~(IF_UP | IF_ADMIN_DOWN | IF_UPDATED))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
1998-06-02 05:41:11 +08:00
|
|
|
if_copy(struct iface *to, struct iface *from)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1998-06-02 05:41:11 +08:00
|
|
|
to->flags = from->flags;
|
|
|
|
to->mtu = from->mtu;
|
|
|
|
to->index = from->index;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
if_changed(struct iface *i, struct iface *j)
|
|
|
|
{
|
|
|
|
unsigned f = 0;
|
|
|
|
|
|
|
|
if (i->mtu != j->mtu)
|
|
|
|
f |= IF_CHANGE_MTU;
|
1998-06-02 05:41:11 +08:00
|
|
|
if ((i->flags ^ j->flags) & ~IF_UPDATED)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
|
|
|
f |= IF_CHANGE_FLAGS;
|
|
|
|
if ((i->flags ^ j->flags) & IF_UP)
|
|
|
|
if (i->flags & IF_UP)
|
|
|
|
f |= IF_CHANGE_DOWN;
|
|
|
|
else
|
|
|
|
f |= IF_CHANGE_UP;
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
if_notify_change(unsigned c, struct iface *old, struct iface *new)
|
|
|
|
{
|
1998-06-03 16:40:10 +08:00
|
|
|
struct proto *p;
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
debug("Interface change notification (%x) for %s\n", c, new->name);
|
1998-06-02 05:41:11 +08:00
|
|
|
if (old)
|
|
|
|
if_dump(old);
|
|
|
|
if (new)
|
|
|
|
if_dump(new);
|
|
|
|
|
|
|
|
if (c & IF_CHANGE_UP)
|
|
|
|
neigh_if_up(new);
|
|
|
|
|
1998-06-03 16:40:10 +08:00
|
|
|
WALK_LIST(p, proto_list)
|
|
|
|
if (p->if_notify)
|
|
|
|
p->if_notify(p, c, old, new);
|
1998-06-02 05:41:11 +08:00
|
|
|
|
|
|
|
if (c & IF_CHANGE_DOWN)
|
|
|
|
neigh_if_down(old);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_update(struct iface *new)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1998-06-02 05:41:11 +08:00
|
|
|
unsigned c;
|
1998-05-27 05:42:05 +08:00
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if (!strcmp(new->name, i->name))
|
|
|
|
{
|
1998-06-02 05:41:11 +08:00
|
|
|
if (if_change_too_big_p(i, new)) /* Changed a lot, convert it to down/up */
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1998-06-02 05:41:11 +08:00
|
|
|
DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
|
|
|
|
i->flags &= ~IF_UP;
|
|
|
|
if_notify_change(IF_CHANGE_DOWN | IF_CHANGE_FLAGS, i, NULL);
|
1998-05-27 05:42:05 +08:00
|
|
|
rem_node(&i->n);
|
1998-06-02 05:41:11 +08:00
|
|
|
goto newif;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
1998-06-02 05:41:11 +08:00
|
|
|
c = if_changed(i, new);
|
|
|
|
if_copy(i, new); /* Even if c==0 as we might need to update i->index et al. */
|
|
|
|
i->flags |= IF_UPDATED;
|
|
|
|
if (c)
|
|
|
|
if_notify_change(c, i, new);
|
1998-05-27 05:42:05 +08:00
|
|
|
return;
|
|
|
|
}
|
1998-06-02 05:41:11 +08:00
|
|
|
|
|
|
|
i = mb_alloc(if_pool, sizeof(struct iface));
|
|
|
|
newif:
|
|
|
|
memcpy(i, new, sizeof(*i));
|
|
|
|
i->flags |= IF_UPDATED;
|
1998-05-27 05:42:05 +08:00
|
|
|
add_tail(&iface_list, &i->n);
|
1998-06-05 04:30:11 +08:00
|
|
|
if_notify_change(IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0)
|
|
|
|
| IF_CHANGE_FLAGS | IF_CHANGE_MTU, NULL, i);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_end_update(void)
|
|
|
|
{
|
|
|
|
struct iface *i, j;
|
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if (i->flags & IF_UPDATED)
|
|
|
|
i->flags &= ~IF_UPDATED;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&j, i, sizeof(struct iface));
|
|
|
|
i->flags = (i->flags & ~IF_UP) | IF_ADMIN_DOWN;
|
|
|
|
if_notify_change(IF_CHANGE_DOWN | IF_CHANGE_FLAGS, &j, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-17 19:05:18 +08:00
|
|
|
void
|
|
|
|
if_feed_baby(struct proto *p)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
|
|
|
if (!p->if_notify)
|
|
|
|
return;
|
1999-02-06 05:37:34 +08:00
|
|
|
debug("Announcing interfaces to new protocol %s\n", p->cf->name);
|
1998-10-17 19:05:18 +08:00
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
p->if_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), NULL, i);
|
|
|
|
}
|
|
|
|
|
1999-02-06 05:37:34 +08:00
|
|
|
u32
|
1998-10-20 02:13:36 +08:00
|
|
|
auto_router_id(void) /* FIXME: What if we run IPv6??? */
|
|
|
|
{
|
|
|
|
struct iface *i, *j;
|
|
|
|
|
|
|
|
j = NULL;
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if ((i->flags & IF_UP) &&
|
|
|
|
!(i->flags & (IF_UNNUMBERED | IF_LOOPBACK | IF_IGNORE)) &&
|
|
|
|
(!j || ipa_to_u32(i->ip) < ipa_to_u32(j->ip)))
|
|
|
|
j = i;
|
1999-02-06 05:37:34 +08:00
|
|
|
if (!j)
|
|
|
|
return 0;
|
|
|
|
debug("Guessed router ID %I (%s)\n", j->ip, j->name);
|
|
|
|
return ipa_to_u32(j->ip);
|
1998-10-20 02:13:36 +08:00
|
|
|
}
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
void
|
|
|
|
if_init(void)
|
|
|
|
{
|
|
|
|
if_pool = rp_new(&root_pool, "Interfaces");
|
|
|
|
init_list(&iface_list);
|
1998-06-02 05:41:11 +08:00
|
|
|
neigh_slab = sl_new(if_pool, sizeof(neighbor));
|
|
|
|
init_list(&neigh_list);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
1998-11-30 06:01:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface Pattern Lists
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct iface_patt *
|
|
|
|
iface_patt_match(list *l, struct iface *i)
|
|
|
|
{
|
|
|
|
struct iface_patt *p;
|
|
|
|
|
|
|
|
WALK_LIST(p, *l)
|
|
|
|
{
|
|
|
|
char *t = p->pattern;
|
|
|
|
int ok = 1;
|
|
|
|
if (*t == '-')
|
|
|
|
{
|
|
|
|
t++;
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
if (patmatch(t, i->name))
|
|
|
|
return ok ? p : NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iface_patts_equal(list *a, list *b, int (*comp)(struct iface_patt *, struct iface_patt *))
|
|
|
|
{
|
|
|
|
struct iface_patt *x, *y;
|
|
|
|
|
|
|
|
x = HEAD(*a);
|
|
|
|
y = HEAD(*b);
|
|
|
|
while (x->n.next && y->n.next)
|
|
|
|
{
|
|
|
|
if (strcmp(x->pattern, y->pattern) || comp && !comp(x, y))
|
|
|
|
return 0;
|
|
|
|
x = (void *) x->n.next;
|
|
|
|
y = (void *) y->n.next;
|
|
|
|
}
|
|
|
|
return (!x->n.next && !y->n.next);
|
|
|
|
}
|