1998-05-26 21:42:05 +00:00
|
|
|
/*
|
1998-06-01 21:41:11 +00:00
|
|
|
* BIRD -- Management of Interfaces and Neighbor Cache
|
1998-05-26 21:42:05 +00:00
|
|
|
*
|
1999-02-05 21:37:34 +00:00
|
|
|
* (c) 1998--1999 Martin Mares <mj@ucw.cz>
|
1998-05-26 21:42:05 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
1998-06-01 21:41:11 +00:00
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
1998-05-26 21:42:05 +00:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
1998-06-01 21:41:11 +00:00
|
|
|
#include "nest/protocol.h"
|
1998-05-26 21:42:05 +00:00
|
|
|
#include "lib/resource.h"
|
1998-11-29 22:01:03 +00:00
|
|
|
#include "lib/string.h"
|
1999-02-13 18:42:00 +00:00
|
|
|
#include "conf/conf.h"
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
static pool *if_pool;
|
|
|
|
|
1999-02-13 18:42:00 +00:00
|
|
|
static void auto_router_id(void);
|
|
|
|
|
1998-06-01 21:41:11 +00:00
|
|
|
/*
|
|
|
|
* Neighbor Cache
|
|
|
|
*
|
|
|
|
* FIXME: Use hashing to get some real speed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *b;
|
|
|
|
|
1999-03-02 17:28:06 +00:00
|
|
|
if (!(i->flags & IF_UP))
|
1998-06-01 21:41:11 +00:00
|
|
|
return 0;
|
1999-05-06 21:38:11 +00:00
|
|
|
if ((i->flags & IF_UNNUMBERED) && ipa_equal(*a, i->addr->opposite))
|
1998-06-01 21:41:11 +00:00
|
|
|
return 1;
|
1999-05-06 21:38:11 +00:00
|
|
|
WALK_LIST(b, i->addrs)
|
|
|
|
if (ipa_in_net(*a, b->prefix, b->pxlen))
|
|
|
|
{
|
|
|
|
if (ipa_equal(*a, b->prefix) || /* Network address */
|
|
|
|
ipa_equal(*a, b->brd) || /* Broadcast */
|
|
|
|
ipa_equal(*a, b->ip)) /* Our own address */
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
1998-06-01 21:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
1999-05-06 21:38:11 +00:00
|
|
|
if (!j) /* FIXME: Search for _optimal_ iface route? */
|
1998-06-01 21:41:11 +00:00
|
|
|
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-26 21:42:05 +00:00
|
|
|
void
|
1998-06-01 21:41:11 +00:00
|
|
|
neigh_dump(neighbor *n)
|
|
|
|
{
|
1998-06-17 14:36:02 +00:00
|
|
|
debug("%p %I ", n, n->addr);
|
1998-06-01 21:41:11 +00:00
|
|
|
if (n->iface)
|
|
|
|
debug("%s ", n->iface->name);
|
|
|
|
else
|
|
|
|
debug("[] ");
|
1999-02-11 22:45:54 +00:00
|
|
|
debug("%s %p", n->proto->name, n->data);
|
1998-06-01 21:41:11 +00:00
|
|
|
if (n->flags & NEF_STICKY)
|
1998-12-06 23:11:18 +00:00
|
|
|
debug(" STICKY");
|
1998-06-01 21:41:11 +00: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 14:36:02 +00:00
|
|
|
DBG("Waking up sticky neighbor %I\n", n->addr);
|
1999-02-13 20:57:47 +00:00
|
|
|
if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
|
1998-06-03 08:40:10 +00:00
|
|
|
n->proto->neigh_notify(n);
|
1998-06-01 21:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
neigh_if_down(struct iface *i)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1998-06-01 21:41:11 +00:00
|
|
|
neighbor *n, *m;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1998-06-01 21:41:11 +00:00
|
|
|
for(m=i->neigh; n = m;)
|
|
|
|
{
|
|
|
|
m = n->sibling;
|
1999-03-26 21:33:36 +00:00
|
|
|
DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
|
1998-06-01 21:41:11 +00:00
|
|
|
n->iface = NULL;
|
1999-02-13 20:57:47 +00:00
|
|
|
if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
|
1998-06-03 08:40:10 +00:00
|
|
|
n->proto->neigh_notify(n);
|
1998-06-01 21:41:11 +00:00
|
|
|
if (!(n->flags & NEF_STICKY))
|
|
|
|
{
|
|
|
|
rem_node(&n->n);
|
|
|
|
sl_free(neigh_slab, n);
|
|
|
|
}
|
|
|
|
}
|
1999-03-26 21:33:36 +00:00
|
|
|
i->neigh = NULL;
|
1998-06-01 21:41:11 +00:00
|
|
|
}
|
|
|
|
|
1999-02-13 20:55:08 +00:00
|
|
|
void
|
|
|
|
neigh_prune(void)
|
|
|
|
{
|
|
|
|
neighbor *n, *m, **N;
|
|
|
|
struct iface *i;
|
|
|
|
|
|
|
|
DBG("Pruning neighbors\n");
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
{
|
|
|
|
N = &i->neigh;
|
|
|
|
while (n = *N)
|
|
|
|
{
|
1999-02-13 20:57:47 +00:00
|
|
|
if (n->proto->core_state == FS_FLUSHING)
|
1999-02-13 20:55:08 +00:00
|
|
|
{
|
|
|
|
*N = n->sibling;
|
|
|
|
rem_node(&n->n);
|
|
|
|
sl_free(neigh_slab, n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
N = &n->sibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-06-01 21:41:11 +00:00
|
|
|
/*
|
|
|
|
* The Interface List
|
|
|
|
*/
|
|
|
|
|
|
|
|
list iface_list;
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
void
|
|
|
|
ifa_dump(struct ifa *a)
|
|
|
|
{
|
|
|
|
debug("\t%I, net %I/%-2d bc %I -> %I%s%s\n", a->ip, a->prefix, a->pxlen, a->brd, a->opposite,
|
|
|
|
(a->flags & IF_UP) ? "" : " DOWN",
|
|
|
|
(a->flags & IA_PRIMARY) ? "" : " SEC");
|
|
|
|
}
|
|
|
|
|
1998-06-01 21:41:11 +00:00
|
|
|
void
|
|
|
|
if_dump(struct iface *i)
|
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a;
|
|
|
|
|
1998-05-26 21:42:05 +00: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");
|
1999-03-02 17:28:06 +00:00
|
|
|
else
|
|
|
|
debug(" DOWN");
|
|
|
|
if (i->flags & IF_LINK_UP)
|
|
|
|
debug(" LINK-UP");
|
1998-05-26 21:42:05 +00:00
|
|
|
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");
|
1999-05-06 21:38:11 +00:00
|
|
|
if (i->flags & IF_TMP_DOWN)
|
|
|
|
debug(" TDOWN");
|
1998-05-26 21:42:05 +00:00
|
|
|
debug(" MTU=%d\n", i->mtu);
|
1999-05-06 21:38:11 +00:00
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
{
|
|
|
|
ifa_dump(a);
|
|
|
|
ASSERT((a != i->addr) == !(a->flags & IA_PRIMARY));
|
|
|
|
}
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_dump_all(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
1998-06-04 20:30:11 +00:00
|
|
|
debug("Known network interfaces:\n");
|
1998-05-26 21:42:05 +00:00
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if_dump(i);
|
1999-02-13 18:42:00 +00:00
|
|
|
debug("Router ID: %08x\n", config->router_id);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
static inline unsigned
|
|
|
|
if_what_changed(struct iface *i, struct iface *j)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
unsigned c;
|
|
|
|
|
|
|
|
if (((i->flags ^ j->flags) & ~(IF_UP | IF_ADMIN_DOWN | IF_UPDATED | IF_LINK_UP | IF_TMP_DOWN | IF_JUST_CREATED))
|
|
|
|
|| i->index != j->index)
|
|
|
|
return IF_CHANGE_TOO_MUCH;
|
|
|
|
c = 0;
|
|
|
|
if ((i->flags ^ j->flags) & IF_UP)
|
|
|
|
c |= (i->flags & IF_UP) ? IF_CHANGE_DOWN : IF_CHANGE_UP;
|
|
|
|
if (i->mtu != j->mtu)
|
|
|
|
c |= IF_CHANGE_MTU;
|
|
|
|
return c;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
1998-06-01 21:41:11 +00:00
|
|
|
if_copy(struct iface *to, struct iface *from)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
to->flags = from->flags | (to->flags & IF_TMP_DOWN);
|
1998-06-01 21:41:11 +00:00
|
|
|
to->mtu = from->mtu;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
static void
|
|
|
|
ifa_notify_change(unsigned c, struct ifa *a)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
struct proto *p;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
debug("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip);
|
|
|
|
WALK_LIST(p, proto_list)
|
|
|
|
if (p->ifa_notify)
|
|
|
|
p->ifa_notify(p, c, a);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-05-06 21:38:11 +00:00
|
|
|
if_notify_change(unsigned c, struct iface *i)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1998-06-03 08:40:10 +00:00
|
|
|
struct proto *p;
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a;
|
1998-06-03 08:40:10 +00:00
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
if (i->flags & IF_JUST_CREATED)
|
|
|
|
{
|
|
|
|
i->flags &= ~IF_JUST_CREATED;
|
|
|
|
c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Interface change notification (%x) for %s\n", c, i->name);
|
|
|
|
if_dump(i);
|
1998-06-01 21:41:11 +00:00
|
|
|
|
|
|
|
if (c & IF_CHANGE_UP)
|
1999-05-06 21:38:11 +00:00
|
|
|
neigh_if_up(i);
|
|
|
|
if (c & IF_CHANGE_DOWN)
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
{
|
|
|
|
a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
|
|
|
|
ifa_notify_change(IF_CHANGE_DOWN, a);
|
|
|
|
}
|
1998-06-01 21:41:11 +00:00
|
|
|
|
1998-06-03 08:40:10 +00:00
|
|
|
WALK_LIST(p, proto_list)
|
|
|
|
if (p->if_notify)
|
1999-05-06 21:38:11 +00:00
|
|
|
p->if_notify(p, c, i);
|
1998-06-01 21:41:11 +00:00
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
if (c & IF_CHANGE_UP)
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
{
|
|
|
|
a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
|
|
|
|
ifa_notify_change(IF_CHANGE_UP, a);
|
|
|
|
}
|
1998-06-01 21:41:11 +00:00
|
|
|
if (c & IF_CHANGE_DOWN)
|
1999-05-06 21:38:11 +00:00
|
|
|
neigh_if_down(i);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
static unsigned
|
|
|
|
if_recalc_flags(struct iface *i, unsigned flags)
|
|
|
|
{
|
|
|
|
if ((flags & (IF_ADMIN_DOWN | IF_TMP_DOWN)) ||
|
|
|
|
!(flags & IF_LINK_UP) ||
|
|
|
|
!i->addr)
|
|
|
|
flags &= ~IF_UP;
|
|
|
|
else
|
|
|
|
flags |= IF_UP;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
if_change_flags(struct iface *i, unsigned flags)
|
|
|
|
{
|
|
|
|
unsigned of = i->flags;
|
|
|
|
|
|
|
|
i->flags = if_recalc_flags(i, flags);
|
|
|
|
if ((i->flags ^ of) & IF_UP)
|
|
|
|
if_notify_change((i->flags & IF_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iface *
|
1998-05-26 21:42:05 +00:00
|
|
|
if_update(struct iface *new)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a, *b;
|
1998-06-01 21:41:11 +00:00
|
|
|
unsigned c;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if (!strcmp(new->name, i->name))
|
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
new->addr = i->addr;
|
|
|
|
new->flags = if_recalc_flags(new, new->flags);
|
|
|
|
c = if_what_changed(i, new);
|
|
|
|
if (c & IF_CHANGE_TOO_MUCH) /* Changed a lot, convert it to down/up */
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1998-06-01 21:41:11 +00:00
|
|
|
DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
|
1999-05-06 21:38:11 +00:00
|
|
|
if_change_flags(i, i->flags | IF_TMP_DOWN);
|
1998-05-26 21:42:05 +00:00
|
|
|
rem_node(&i->n);
|
1999-05-07 13:46:16 +00:00
|
|
|
new->addr = i->addr;
|
|
|
|
memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
|
|
|
|
memcpy(i, new, sizeof(*i));
|
1998-06-01 21:41:11 +00:00
|
|
|
goto newif;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
1999-05-06 21:38:11 +00:00
|
|
|
else if (c)
|
|
|
|
{
|
|
|
|
if_copy(i, new);
|
|
|
|
if_notify_change(c, i);
|
|
|
|
}
|
1999-03-02 18:36:09 +00:00
|
|
|
i->flags |= IF_UPDATED;
|
1999-05-06 21:38:11 +00:00
|
|
|
return i;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
1998-06-01 21:41:11 +00:00
|
|
|
i = mb_alloc(if_pool, sizeof(struct iface));
|
|
|
|
memcpy(i, new, sizeof(*i));
|
1999-05-06 21:38:11 +00:00
|
|
|
init_list(&i->addrs);
|
1999-05-07 13:46:16 +00:00
|
|
|
newif:
|
1999-05-06 21:38:11 +00:00
|
|
|
i->flags |= IF_UPDATED | IF_TMP_DOWN; /* Tmp down as we don't have addresses yet */
|
1998-05-26 21:42:05 +00:00
|
|
|
add_tail(&iface_list, &i->n);
|
1999-05-06 21:38:11 +00:00
|
|
|
return i;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 18:36:09 +00:00
|
|
|
void
|
|
|
|
if_start_update(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a;
|
1999-03-02 18:36:09 +00:00
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-06 21:38:11 +00:00
|
|
|
{
|
|
|
|
i->flags &= ~IF_UPDATED;
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
a->flags &= ~IF_UPDATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_end_partial_update(struct iface *i)
|
|
|
|
{
|
|
|
|
if (i->flags & IF_TMP_DOWN)
|
|
|
|
if_change_flags(i, i->flags & ~IF_TMP_DOWN);
|
1999-03-02 18:36:09 +00:00
|
|
|
}
|
|
|
|
|
1998-05-26 21:42:05 +00:00
|
|
|
void
|
|
|
|
if_end_update(void)
|
|
|
|
{
|
|
|
|
struct iface *i, j;
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a, *b;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1999-02-13 18:42:00 +00:00
|
|
|
if (!config->router_id)
|
|
|
|
auto_router_id();
|
|
|
|
|
1998-05-26 21:42:05 +00:00
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-06 21:38:11 +00:00
|
|
|
{
|
|
|
|
if (!(i->flags & IF_UPDATED))
|
|
|
|
if_change_flags(i, (i->flags & ~IF_LINK_UP) | IF_ADMIN_DOWN);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WALK_LIST_DELSAFE(a, b, i->addrs)
|
|
|
|
if (!(a->flags & IF_UPDATED))
|
|
|
|
ifa_delete(a);
|
|
|
|
if_end_partial_update(i);
|
|
|
|
}
|
|
|
|
}
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1998-10-17 11:05:18 +00:00
|
|
|
void
|
|
|
|
if_feed_baby(struct proto *p)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1999-05-06 21:38:11 +00:00
|
|
|
struct ifa *a;
|
1998-10-17 11:05:18 +00:00
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
if (!p->if_notify && !p->ifa_notify)
|
1998-10-17 11:05:18 +00:00
|
|
|
return;
|
1999-02-11 22:45:54 +00:00
|
|
|
debug("Announcing interfaces to new protocol %s\n", p->name);
|
1998-10-17 11:05:18 +00:00
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-06 21:38:11 +00:00
|
|
|
{
|
|
|
|
if (p->if_notify)
|
|
|
|
p->if_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
|
|
|
|
if (p->ifa_notify && (i->flags & IF_UP))
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
p->ifa_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
|
|
|
|
}
|
1998-10-17 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 18:36:09 +00:00
|
|
|
struct iface *
|
|
|
|
if_find_by_index(unsigned idx)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if (i->index == idx)
|
|
|
|
return i;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
struct iface *
|
|
|
|
if_find_by_name(char *name)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
if (!strcmp(i->name, name))
|
|
|
|
return i;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ifa_recalc_primary(struct iface *i)
|
|
|
|
{
|
|
|
|
struct ifa *a, *b = NULL;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
{
|
|
|
|
if (!(a->flags & IA_SECONDARY) && (!b || a->scope > b->scope))
|
|
|
|
b = a;
|
|
|
|
a->flags &= ~IA_PRIMARY;
|
|
|
|
}
|
|
|
|
res = (b != i->addr);
|
|
|
|
i->addr = b;
|
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
b->flags |= IA_PRIMARY;
|
|
|
|
rem_node(&b->n);
|
|
|
|
add_head(&i->addrs, &b->n);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ifa *
|
|
|
|
ifa_update(struct ifa *a)
|
|
|
|
{
|
|
|
|
struct iface *i = a->iface;
|
|
|
|
struct ifa *b;
|
|
|
|
|
|
|
|
WALK_LIST(b, i->addrs)
|
|
|
|
if (ipa_equal(b->ip, a->ip))
|
|
|
|
{
|
|
|
|
if (ipa_equal(b->prefix, a->prefix) &&
|
|
|
|
b->pxlen == a->pxlen &&
|
|
|
|
ipa_equal(b->brd, a->brd) &&
|
|
|
|
ipa_equal(b->opposite, a->opposite) &&
|
|
|
|
b->scope == a->scope)
|
|
|
|
{
|
|
|
|
b->flags |= IF_UPDATED;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
ifa_delete(b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
b = mb_alloc(if_pool, sizeof(struct ifa));
|
|
|
|
memcpy(b, a, sizeof(struct ifa));
|
|
|
|
add_tail(&i->addrs, &b->n);
|
|
|
|
b->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
|
|
|
|
if ((!i->addr || i->addr->scope < b->scope) && ifa_recalc_primary(i))
|
|
|
|
if_change_flags(i, i->flags | IF_TMP_DOWN);
|
|
|
|
if (b->flags & IF_UP)
|
|
|
|
ifa_notify_change(IF_CHANGE_CREATE | IF_CHANGE_UP, b);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ifa_delete(struct ifa *a)
|
|
|
|
{
|
|
|
|
struct iface *i = a->iface;
|
|
|
|
struct ifa *b;
|
|
|
|
|
|
|
|
WALK_LIST(b, i->addrs)
|
|
|
|
if (ipa_equal(b->ip, a->ip))
|
|
|
|
{
|
|
|
|
rem_node(&b->n);
|
|
|
|
if (b->flags & IF_UP)
|
|
|
|
{
|
|
|
|
b->flags &= ~IF_UP;
|
|
|
|
ifa_notify_change(IF_CHANGE_DOWN, b);
|
|
|
|
}
|
|
|
|
if (b->flags & IA_PRIMARY)
|
|
|
|
{
|
|
|
|
if_change_flags(i, i->flags | IF_TMP_DOWN);
|
|
|
|
ifa_recalc_primary(i);
|
|
|
|
}
|
|
|
|
mb_free(b);
|
1999-05-07 13:46:16 +00:00
|
|
|
return;
|
1999-05-06 21:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-13 18:42:00 +00:00
|
|
|
static void
|
1999-08-03 19:36:06 +00:00
|
|
|
auto_router_id(void)
|
1998-10-19 18:13:36 +00:00
|
|
|
{
|
1999-08-03 19:36:06 +00:00
|
|
|
#ifndef IPV6
|
1998-10-19 18:13:36 +00:00
|
|
|
struct iface *i, *j;
|
|
|
|
|
|
|
|
j = NULL;
|
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-06 21:38:11 +00:00
|
|
|
if ((i->flags & IF_LINK_UP) &&
|
|
|
|
!(i->flags & (IF_UNNUMBERED | IF_IGNORE | IF_ADMIN_DOWN)) &&
|
|
|
|
i->addr &&
|
|
|
|
(!j || ipa_to_u32(i->addr->ip) < ipa_to_u32(j->addr->ip)))
|
1998-10-19 18:13:36 +00:00
|
|
|
j = i;
|
1999-02-05 21:37:34 +00:00
|
|
|
if (!j)
|
1999-02-13 18:42:00 +00:00
|
|
|
die("Cannot determine router ID (no suitable network interface found), please configure it manually");
|
1999-05-06 21:38:11 +00:00
|
|
|
debug("Guessed router ID %I (%s)\n", j->addr->ip, j->name);
|
|
|
|
config->router_id = ipa_to_u32(j->addr->ip);
|
1999-08-03 19:36:06 +00:00
|
|
|
#endif
|
1998-10-19 18:13:36 +00:00
|
|
|
}
|
|
|
|
|
1998-05-26 21:42:05 +00:00
|
|
|
void
|
|
|
|
if_init(void)
|
|
|
|
{
|
|
|
|
if_pool = rp_new(&root_pool, "Interfaces");
|
|
|
|
init_list(&iface_list);
|
1998-06-01 21:41:11 +00:00
|
|
|
neigh_slab = sl_new(if_pool, sizeof(neighbor));
|
|
|
|
init_list(&neigh_list);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
1998-11-29 22:01:03 +00: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;
|
1999-08-03 19:30:49 +00:00
|
|
|
if (t)
|
1998-11-29 22:01:03 +00:00
|
|
|
{
|
1999-08-03 19:30:49 +00:00
|
|
|
if (*t == '-')
|
|
|
|
{
|
|
|
|
t++;
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
if (!patmatch(t, i->name))
|
|
|
|
continue;
|
1998-11-29 22:01:03 +00:00
|
|
|
}
|
1999-08-03 19:30:49 +00:00
|
|
|
if (!i->addr || !ipa_in_net(i->addr->ip, p->prefix, p->pxlen))
|
|
|
|
continue;
|
|
|
|
return ok ? p : NULL;
|
1998-11-29 22:01:03 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
1999-08-03 19:30:49 +00:00
|
|
|
if (strcmp(x->pattern, y->pattern) ||
|
|
|
|
!ipa_equal(x->prefix, y->prefix) ||
|
|
|
|
x->pxlen != y->pxlen ||
|
|
|
|
comp && !comp(x, y))
|
1998-11-29 22:01:03 +00:00
|
|
|
return 0;
|
|
|
|
x = (void *) x->n.next;
|
|
|
|
y = (void *) y->n.next;
|
|
|
|
}
|
|
|
|
return (!x->n.next && !y->n.next);
|
|
|
|
}
|