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
|
|
|
*
|
2000-03-01 22:51:47 +08:00
|
|
|
* (c) 1998--2000 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.
|
|
|
|
*/
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* DOC: Interfaces
|
|
|
|
*
|
|
|
|
* The interface module keeps track of all network interfaces in the
|
|
|
|
* system and their addresses.
|
|
|
|
*
|
|
|
|
* Each interface is represented by an &iface structure which carries
|
|
|
|
* interface capability flags (%IF_MULTIACCESS, %IF_BROADCAST etc.),
|
|
|
|
* MTU, interface name and index and finally a linked list of network
|
|
|
|
* prefixes assigned to the interface, each one represented by
|
|
|
|
* struct &ifa.
|
|
|
|
*
|
|
|
|
* The interface module keeps a `soft-up' state for each &iface which
|
|
|
|
* is a conjunction of link being up, the interface being of a `sane'
|
|
|
|
* type and at least one IP address assigned to it.
|
|
|
|
*/
|
|
|
|
|
2000-03-13 04:50:35 +08:00
|
|
|
#undef LOCAL_DEBUG
|
1998-06-02 05:41:11 +08:00
|
|
|
|
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"
|
1999-11-25 23:35:30 +08:00
|
|
|
#include "nest/cli.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"
|
1999-02-14 02:42:00 +08:00
|
|
|
#include "conf/conf.h"
|
1998-05-27 05:42:05 +08:00
|
|
|
|
|
|
|
static pool *if_pool;
|
|
|
|
|
1999-02-14 02:42:00 +08:00
|
|
|
static void auto_router_id(void);
|
|
|
|
|
1998-06-02 05:41:11 +08:00
|
|
|
list iface_list;
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* ifa_dump - dump interface address
|
|
|
|
* @a: interface address descriptor
|
|
|
|
*
|
|
|
|
* This function dumps contents of an &ifa to the debug output.
|
|
|
|
*/
|
1999-05-07 05:38:11 +08:00
|
|
|
void
|
|
|
|
ifa_dump(struct ifa *a)
|
|
|
|
{
|
2000-03-01 07:19:52 +08:00
|
|
|
debug("\t%I, net %I/%-2d bc %I -> %I%s%s%s\n", a->ip, a->prefix, a->pxlen, a->brd, a->opposite,
|
1999-05-07 05:38:11 +08:00
|
|
|
(a->flags & IF_UP) ? "" : " DOWN",
|
2000-03-01 07:19:52 +08:00
|
|
|
(a->flags & IA_PRIMARY) ? "" : " SEC",
|
|
|
|
(a->flags & IA_UNNUMBERED) ? " UNNUM" : "");
|
1999-05-07 05:38:11 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_dump - dump interface
|
|
|
|
* @i: interface to dump
|
|
|
|
*
|
|
|
|
* This function dumps all information associated with a given
|
|
|
|
* network interface to the debug output.
|
|
|
|
*/
|
1998-06-02 05:41:11 +08:00
|
|
|
void
|
|
|
|
if_dump(struct iface *i)
|
|
|
|
{
|
1999-05-07 05:38:11 +08:00
|
|
|
struct ifa *a;
|
|
|
|
|
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");
|
1999-03-03 01:28:06 +08:00
|
|
|
else
|
|
|
|
debug(" DOWN");
|
|
|
|
if (i->flags & IF_LINK_UP)
|
|
|
|
debug(" LINK-UP");
|
1998-05-27 05:42:05 +08:00
|
|
|
if (i->flags & IF_MULTIACCESS)
|
|
|
|
debug(" MA");
|
|
|
|
if (i->flags & IF_BROADCAST)
|
|
|
|
debug(" BC");
|
|
|
|
if (i->flags & IF_MULTICAST)
|
|
|
|
debug(" MC");
|
|
|
|
if (i->flags & IF_LOOPBACK)
|
|
|
|
debug(" LOOP");
|
|
|
|
if (i->flags & IF_IGNORE)
|
|
|
|
debug(" IGN");
|
1999-05-07 05:38:11 +08:00
|
|
|
if (i->flags & IF_TMP_DOWN)
|
|
|
|
debug(" TDOWN");
|
1998-05-27 05:42:05 +08:00
|
|
|
debug(" MTU=%d\n", i->mtu);
|
1999-05-07 05:38:11 +08:00
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
{
|
|
|
|
ifa_dump(a);
|
|
|
|
ASSERT((a != i->addr) == !(a->flags & IA_PRIMARY));
|
|
|
|
}
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_dump_all - dump all interfaces
|
|
|
|
*
|
|
|
|
* This function dumps information about all known network
|
|
|
|
* interfaces to the debug output.
|
|
|
|
*/
|
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);
|
1999-02-14 02:42:00 +08:00
|
|
|
debug("Router ID: %08x\n", config->router_id);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
1999-05-07 05:38:11 +08:00
|
|
|
static inline unsigned
|
|
|
|
if_what_changed(struct iface *i, struct iface *j)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1999-05-07 05:38:11 +08: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-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
|
|
|
{
|
1999-05-07 05:38:11 +08:00
|
|
|
to->flags = from->flags | (to->flags & IF_TMP_DOWN);
|
1998-06-02 05:41:11 +08:00
|
|
|
to->mtu = from->mtu;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
2000-03-13 04:50:35 +08:00
|
|
|
static inline void
|
|
|
|
ifa_send_notify(struct proto *p, unsigned c, struct ifa *a)
|
|
|
|
{
|
|
|
|
if (p->ifa_notify)
|
|
|
|
{
|
|
|
|
if (p->debug & D_IFACES)
|
|
|
|
log(L_TRACE "%s < %s address %I/%d on interface %s %s",
|
|
|
|
p->name, (a->flags & IA_PRIMARY) ? "primary" : "secondary",
|
|
|
|
a->prefix, a->pxlen, a->iface->name,
|
|
|
|
(c & IF_CHANGE_UP) ? "added" : "removed");
|
|
|
|
p->ifa_notify(p, c, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-07 05:38:11 +08:00
|
|
|
static void
|
|
|
|
ifa_notify_change(unsigned c, struct ifa *a)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1999-05-07 05:38:11 +08:00
|
|
|
struct proto *p;
|
1998-05-27 05:42:05 +08:00
|
|
|
|
2000-03-13 04:50:35 +08:00
|
|
|
DBG("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip);
|
2000-01-17 07:30:06 +08:00
|
|
|
WALK_LIST(p, active_proto_list)
|
2000-03-13 04:50:35 +08:00
|
|
|
ifa_send_notify(p, c, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
if_send_notify(struct proto *p, unsigned c, struct iface *i)
|
|
|
|
{
|
|
|
|
if (p->if_notify)
|
|
|
|
{
|
|
|
|
if (p->debug & D_IFACES)
|
|
|
|
log(L_TRACE "%s < interface %s %s", p->name, i->name,
|
|
|
|
(c & IF_CHANGE_UP) ? "goes up" :
|
|
|
|
(c & IF_CHANGE_DOWN) ? "goes down" :
|
|
|
|
(c & IF_CHANGE_MTU) ? "changes MTU" :
|
|
|
|
(c & IF_CHANGE_CREATE) ? "created" :
|
|
|
|
"sends unknown event");
|
|
|
|
p->if_notify(p, c, i);
|
|
|
|
}
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-05-07 05:38:11 +08:00
|
|
|
if_notify_change(unsigned c, struct iface *i)
|
1998-05-27 05:42:05 +08:00
|
|
|
{
|
1998-06-03 16:40:10 +08:00
|
|
|
struct proto *p;
|
1999-05-07 05:38:11 +08:00
|
|
|
struct ifa *a;
|
1998-06-03 16:40:10 +08:00
|
|
|
|
1999-05-07 05:38:11 +08:00
|
|
|
if (i->flags & IF_JUST_CREATED)
|
|
|
|
{
|
|
|
|
i->flags &= ~IF_JUST_CREATED;
|
|
|
|
c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
|
|
|
|
}
|
|
|
|
|
2000-03-13 04:50:35 +08:00
|
|
|
DBG("Interface change notification (%x) for %s\n", c, i->name);
|
2000-05-05 04:52:28 +08:00
|
|
|
#ifdef LOCAL_DEBUG
|
1999-05-07 05:38:11 +08:00
|
|
|
if_dump(i);
|
2000-05-05 04:52:28 +08:00
|
|
|
#endif
|
1998-06-02 05:41:11 +08:00
|
|
|
|
|
|
|
if (c & IF_CHANGE_UP)
|
1999-05-07 05:38:11 +08: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-02 05:41:11 +08:00
|
|
|
|
2000-01-17 07:30:06 +08:00
|
|
|
WALK_LIST(p, active_proto_list)
|
2000-03-13 04:50:35 +08:00
|
|
|
if_send_notify(p, c, i);
|
1998-06-02 05:41:11 +08:00
|
|
|
|
1999-05-07 05:38:11 +08: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-02 05:41:11 +08:00
|
|
|
if (c & IF_CHANGE_DOWN)
|
1999-05-07 05:38:11 +08:00
|
|
|
neigh_if_down(i);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
1999-05-07 05:38:11 +08: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);
|
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_update - update interface status
|
|
|
|
* @new: new interface status
|
|
|
|
*
|
|
|
|
* if_update() is called by the low-level platform dependent code
|
|
|
|
* whenever it notices an interface change.
|
|
|
|
*
|
2000-06-06 05:26:11 +08:00
|
|
|
* There exist two types of interface updates -- synchronous and asynchronous
|
2000-06-03 22:40:39 +08:00
|
|
|
* ones. In the synchronous case, the low-level code calls if_start_update(),
|
|
|
|
* scans all interfaces reported by the OS, uses if_update() and ifa_update()
|
|
|
|
* to pass them to the core and then it finishes the update sequence by
|
|
|
|
* calling if_end_update(). When working asynchronously, the sysdep code
|
|
|
|
* calls if_update() and ifa_update() whenever it notices a change.
|
|
|
|
*
|
|
|
|
* if_update() will automatically notify all other modules about the change.
|
|
|
|
*/
|
1999-05-07 05:38:11 +08:00
|
|
|
struct iface *
|
1998-05-27 05:42:05 +08:00
|
|
|
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))
|
|
|
|
{
|
1999-05-07 05:38:11 +08: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-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);
|
1999-05-07 05:38:11 +08:00
|
|
|
if_change_flags(i, i->flags | IF_TMP_DOWN);
|
1998-05-27 05:42:05 +08:00
|
|
|
rem_node(&i->n);
|
1999-05-07 21:46:16 +08:00
|
|
|
new->addr = i->addr;
|
|
|
|
memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
|
|
|
|
memcpy(i, new, sizeof(*i));
|
1998-06-02 05:41:11 +08:00
|
|
|
goto newif;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
1999-05-07 05:38:11 +08:00
|
|
|
else if (c)
|
|
|
|
{
|
|
|
|
if_copy(i, new);
|
|
|
|
if_notify_change(c, i);
|
|
|
|
}
|
1999-03-03 02:36:09 +08:00
|
|
|
i->flags |= IF_UPDATED;
|
1999-05-07 05:38:11 +08:00
|
|
|
return i;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
1998-06-02 05:41:11 +08:00
|
|
|
i = mb_alloc(if_pool, sizeof(struct iface));
|
|
|
|
memcpy(i, new, sizeof(*i));
|
1999-05-07 05:38:11 +08:00
|
|
|
init_list(&i->addrs);
|
1999-05-07 21:46:16 +08:00
|
|
|
newif:
|
2000-04-10 18:40:00 +08:00
|
|
|
init_list(&i->neighbors);
|
1999-05-07 05:38:11 +08:00
|
|
|
i->flags |= IF_UPDATED | IF_TMP_DOWN; /* Tmp down as we don't have addresses yet */
|
1998-05-27 05:42:05 +08:00
|
|
|
add_tail(&iface_list, &i->n);
|
1999-05-07 05:38:11 +08:00
|
|
|
return i;
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
1999-03-03 02:36:09 +08:00
|
|
|
void
|
|
|
|
if_start_update(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1999-05-07 05:38:11 +08:00
|
|
|
struct ifa *a;
|
1999-03-03 02:36:09 +08:00
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-07 05:38:11 +08: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-03 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
void
|
|
|
|
if_end_update(void)
|
|
|
|
{
|
2000-05-05 04:30:36 +08:00
|
|
|
struct iface *i;
|
1999-05-07 05:38:11 +08:00
|
|
|
struct ifa *a, *b;
|
1998-05-27 05:42:05 +08:00
|
|
|
|
1999-02-14 02:42:00 +08:00
|
|
|
if (!config->router_id)
|
|
|
|
auto_router_id();
|
|
|
|
|
1998-05-27 05:42:05 +08:00
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-07 05:38:11 +08: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-27 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_feed_baby - advertise interfaces to a new protocol
|
|
|
|
* @p: protocol to feed
|
|
|
|
*
|
|
|
|
* When a new protocol starts, this function sends it a series
|
|
|
|
* of notifications about all existing interfaces.
|
|
|
|
*/
|
1998-10-17 19:05:18 +08:00
|
|
|
void
|
|
|
|
if_feed_baby(struct proto *p)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
1999-05-07 05:38:11 +08:00
|
|
|
struct ifa *a;
|
1998-10-17 19:05:18 +08:00
|
|
|
|
2000-03-13 04:50:35 +08:00
|
|
|
if (!p->if_notify && !p->ifa_notify) /* shortcut */
|
1998-10-17 19:05:18 +08:00
|
|
|
return;
|
2000-03-13 04:50:35 +08:00
|
|
|
DBG("Announcing interfaces to new protocol %s\n", p->name);
|
1998-10-17 19:05:18 +08:00
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-07 05:38:11 +08:00
|
|
|
{
|
2000-03-13 04:50:35 +08:00
|
|
|
if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
|
|
|
|
if (i->flags & IF_UP)
|
1999-05-07 05:38:11 +08:00
|
|
|
WALK_LIST(a, i->addrs)
|
2000-03-13 04:50:35 +08:00
|
|
|
ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
|
1999-05-07 05:38:11 +08:00
|
|
|
}
|
1998-10-17 19:05:18 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_find_by_index - find interface by ifindex
|
|
|
|
* @idx: ifindex
|
|
|
|
*
|
|
|
|
* This function finds an &iface structure corresponding to an interface
|
|
|
|
* of the given index @idx. Returns a pointer to the structure or %NULL
|
|
|
|
* if no such structure exists.
|
|
|
|
*/
|
1999-03-03 02:36:09 +08: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;
|
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_find_by_name - find interface by name
|
|
|
|
* @name: interface name
|
|
|
|
*
|
|
|
|
* This function finds an &iface structure corresponding to an interface
|
|
|
|
* of the given name @name. Returns a pointer to the structure or %NULL
|
|
|
|
* if no such structure exists.
|
|
|
|
*/
|
1999-05-07 05:38:11 +08: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;
|
|
|
|
}
|
|
|
|
|
2009-05-30 04:49:30 +08:00
|
|
|
struct ifa *kif_choose_primary(struct iface *i);
|
|
|
|
|
1999-05-07 05:38:11 +08:00
|
|
|
static int
|
|
|
|
ifa_recalc_primary(struct iface *i)
|
|
|
|
{
|
2009-05-30 04:49:30 +08:00
|
|
|
struct ifa *a = kif_choose_primary(i);
|
1999-05-07 05:38:11 +08:00
|
|
|
|
2009-05-30 04:49:30 +08:00
|
|
|
if (a == i->addr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (i->addr)
|
|
|
|
i->addr->flags &= ~IA_PRIMARY;
|
|
|
|
|
|
|
|
if (a)
|
1999-05-07 05:38:11 +08:00
|
|
|
{
|
2009-05-30 04:49:30 +08:00
|
|
|
a->flags |= IA_PRIMARY;
|
|
|
|
rem_node(&a->n);
|
|
|
|
add_head(&i->addrs, &a->n);
|
1999-05-07 05:38:11 +08:00
|
|
|
}
|
2009-05-30 04:49:30 +08:00
|
|
|
|
|
|
|
i->addr = a;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ifa_recalc_all_primary_addresses(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-07 05:38:11 +08:00
|
|
|
{
|
2009-05-30 04:49:30 +08:00
|
|
|
if (ifa_recalc_primary(i))
|
|
|
|
if_change_flags(i, i->flags | IF_TMP_DOWN);
|
1999-05-07 05:38:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-30 04:49:30 +08:00
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* ifa_update - update interface address
|
|
|
|
* @a: new interface address
|
|
|
|
*
|
|
|
|
* This function adds address information to a network
|
|
|
|
* interface. It's called by the platform dependent code during
|
|
|
|
* the interface update process described under if_update().
|
|
|
|
*/
|
1999-05-07 05:38:11 +08:00
|
|
|
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) &&
|
2000-03-01 07:19:52 +08:00
|
|
|
b->scope == a->scope &&
|
|
|
|
!((b->flags ^ a->flags) & IA_UNNUMBERED))
|
1999-05-07 05:38:11 +08:00
|
|
|
{
|
|
|
|
b->flags |= IF_UPDATED;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
ifa_delete(b);
|
|
|
|
break;
|
|
|
|
}
|
2000-03-01 07:19:52 +08:00
|
|
|
|
|
|
|
if (!(i->flags & IF_MULTIACCESS) && a->pxlen < BITS_PER_IP_ADDRESS - 2)
|
|
|
|
log(L_WARN "Strange prefix length %d for point-to-point interface %s", a->pxlen, i->name);
|
2000-05-02 23:21:51 +08:00
|
|
|
#ifndef IPV6
|
2000-03-01 07:19:52 +08:00
|
|
|
if ((i->flags & IF_BROADCAST) && !ipa_nonzero(a->brd))
|
|
|
|
log(L_ERR "Missing broadcast address for interface %s", i->name);
|
2000-05-02 23:21:51 +08:00
|
|
|
#endif
|
2000-03-01 07:19:52 +08:00
|
|
|
|
1999-05-07 05:38:11 +08:00
|
|
|
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);
|
2009-05-30 04:49:30 +08:00
|
|
|
if (ifa_recalc_primary(i))
|
1999-05-07 05:38:11 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* ifa_delete - remove interface address
|
|
|
|
* @a: interface address
|
|
|
|
*
|
|
|
|
* This function removes address information from a network
|
|
|
|
* interface. It's called by the platform dependent code during
|
|
|
|
* the interface update process described under if_update().
|
|
|
|
*/
|
1999-05-07 05:38:11 +08:00
|
|
|
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 21:46:16 +08:00
|
|
|
return;
|
1999-05-07 05:38:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-14 02:42:00 +08:00
|
|
|
static void
|
1999-08-04 03:36:06 +08:00
|
|
|
auto_router_id(void)
|
1998-10-20 02:13:36 +08:00
|
|
|
{
|
1999-08-04 03:36:06 +08:00
|
|
|
#ifndef IPV6
|
1998-10-20 02:13:36 +08:00
|
|
|
struct iface *i, *j;
|
|
|
|
|
|
|
|
j = NULL;
|
|
|
|
WALK_LIST(i, iface_list)
|
1999-05-07 05:38:11 +08:00
|
|
|
if ((i->flags & IF_LINK_UP) &&
|
2000-03-01 07:19:52 +08:00
|
|
|
!(i->flags & (IF_IGNORE | IF_ADMIN_DOWN)) &&
|
1999-05-07 05:38:11 +08:00
|
|
|
i->addr &&
|
2000-03-01 07:19:52 +08:00
|
|
|
!(i->addr->flags & IA_UNNUMBERED) &&
|
1999-05-07 05:38:11 +08:00
|
|
|
(!j || ipa_to_u32(i->addr->ip) < ipa_to_u32(j->addr->ip)))
|
1998-10-20 02:13:36 +08:00
|
|
|
j = i;
|
1999-02-06 05:37:34 +08:00
|
|
|
if (!j)
|
1999-02-14 02:42:00 +08:00
|
|
|
die("Cannot determine router ID (no suitable network interface found), please configure it manually");
|
2000-03-13 05:01:38 +08:00
|
|
|
log(L_INFO "Guessed router ID %I according to interface %s", j->addr->ip, j->name);
|
1999-05-07 05:38:11 +08:00
|
|
|
config->router_id = ipa_to_u32(j->addr->ip);
|
1999-08-04 03:36:06 +08:00
|
|
|
#endif
|
1998-10-20 02:13:36 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 22:40:39 +08:00
|
|
|
/**
|
|
|
|
* if_init - initialize interface module
|
|
|
|
*
|
|
|
|
* This function is called during BIRD startup to initialize
|
|
|
|
* all data structures of the interface module.
|
|
|
|
*/
|
1998-05-27 05:42:05 +08:00
|
|
|
void
|
|
|
|
if_init(void)
|
|
|
|
{
|
|
|
|
if_pool = rp_new(&root_pool, "Interfaces");
|
|
|
|
init_list(&iface_list);
|
2000-03-01 22:51:47 +08:00
|
|
|
neigh_init(if_pool);
|
1998-05-27 05:42:05 +08:00
|
|
|
}
|
1998-11-30 06:01:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface Pattern Lists
|
|
|
|
*/
|
|
|
|
|
2009-05-07 04:02:45 +08:00
|
|
|
static int
|
|
|
|
iface_patt_match(struct iface_patt *ifp, struct iface *i)
|
1998-11-30 06:01:03 +08:00
|
|
|
{
|
2009-05-07 04:02:45 +08:00
|
|
|
struct iface_patt_node *p;
|
1998-11-30 06:01:03 +08:00
|
|
|
|
2009-05-07 04:02:45 +08:00
|
|
|
WALK_LIST(p, ifp->ipn_list)
|
1998-11-30 06:01:03 +08:00
|
|
|
{
|
|
|
|
char *t = p->pattern;
|
2009-05-07 04:02:45 +08:00
|
|
|
int pos = p->positive;
|
|
|
|
|
1999-08-04 03:30:49 +08:00
|
|
|
if (t)
|
1998-11-30 06:01:03 +08:00
|
|
|
{
|
1999-08-04 03:30:49 +08:00
|
|
|
if (*t == '-')
|
|
|
|
{
|
|
|
|
t++;
|
2009-05-07 04:02:45 +08:00
|
|
|
pos = !pos;
|
1999-08-04 03:30:49 +08:00
|
|
|
}
|
2009-05-07 04:02:45 +08:00
|
|
|
|
1999-08-04 03:30:49 +08:00
|
|
|
if (!patmatch(t, i->name))
|
|
|
|
continue;
|
1998-11-30 06:01:03 +08:00
|
|
|
}
|
2009-05-07 04:02:45 +08:00
|
|
|
|
|
|
|
if (p->pxlen)
|
|
|
|
if (!i->addr || !ipa_in_net(i->addr->ip, p->prefix, p->pxlen))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return pos;
|
1998-11-30 06:01:03 +08:00
|
|
|
}
|
2009-05-07 04:02:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iface_patt *
|
|
|
|
iface_patt_find(list *l, struct iface *i)
|
|
|
|
{
|
|
|
|
struct iface_patt *p;
|
|
|
|
|
|
|
|
WALK_LIST(p, *l)
|
|
|
|
if (iface_patt_match(p, i))
|
|
|
|
return p;
|
|
|
|
|
1998-11-30 06:01:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-07 04:02:45 +08:00
|
|
|
static int
|
|
|
|
iface_plists_equal(struct iface_patt *pa, struct iface_patt *pb)
|
|
|
|
{
|
|
|
|
struct iface_patt_node *x, *y;
|
|
|
|
|
|
|
|
x = HEAD(pa->ipn_list);
|
|
|
|
y = HEAD(pb->ipn_list);
|
|
|
|
while (x->n.next && y->n.next)
|
|
|
|
{
|
|
|
|
if ((x->positive != y->positive) ||
|
|
|
|
(!x->pattern && y->pattern) || /* This nasty lines where written by me... :-( Feela */
|
|
|
|
(!y->pattern && x->pattern) ||
|
|
|
|
((x->pattern != y->pattern) && strcmp(x->pattern, y->pattern)) ||
|
|
|
|
!ipa_equal(x->prefix, y->prefix) ||
|
|
|
|
(x->pxlen != y->pxlen))
|
|
|
|
return 0;
|
|
|
|
x = (void *) x->n.next;
|
|
|
|
y = (void *) y->n.next;
|
|
|
|
}
|
|
|
|
return (!x->n.next && !y->n.next);
|
|
|
|
}
|
|
|
|
|
1998-11-30 06:01:03 +08:00
|
|
|
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)
|
|
|
|
{
|
2009-05-07 04:02:45 +08:00
|
|
|
if (!iface_plists_equal(x, y) ||
|
|
|
|
(comp && !comp(x, y)))
|
1998-11-30 06:01:03 +08:00
|
|
|
return 0;
|
|
|
|
x = (void *) x->n.next;
|
|
|
|
y = (void *) y->n.next;
|
|
|
|
}
|
|
|
|
return (!x->n.next && !y->n.next);
|
|
|
|
}
|
1999-11-25 23:35:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CLI commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
if_show_addr(struct ifa *a)
|
|
|
|
{
|
|
|
|
byte broad[STD_ADDRESS_P_LENGTH + 16];
|
|
|
|
byte opp[STD_ADDRESS_P_LENGTH + 16];
|
|
|
|
|
|
|
|
if (ipa_nonzero(a->brd))
|
|
|
|
bsprintf(broad, ", broadcast %I", a->brd);
|
|
|
|
else
|
|
|
|
broad[0] = 0;
|
|
|
|
if (ipa_nonzero(a->opposite))
|
|
|
|
bsprintf(opp, ", opposite %I", a->opposite);
|
|
|
|
else
|
|
|
|
opp[0] = 0;
|
2000-03-01 07:19:52 +08:00
|
|
|
cli_msg(-1003, "\t%I/%d (%s%s%s, scope %s%s)",
|
1999-11-25 23:35:30 +08:00
|
|
|
a->ip, a->pxlen,
|
2003-02-23 06:39:06 +08:00
|
|
|
(a->flags & IA_PRIMARY) ? "Primary" : (a->flags & IA_SECONDARY) ? "Secondary" : "Unselected",
|
1999-11-25 23:35:30 +08:00
|
|
|
broad, opp,
|
2000-03-01 07:19:52 +08:00
|
|
|
ip_scope_text(a->scope),
|
|
|
|
(a->flags & IA_UNNUMBERED) ? ", unnumbered" : "");
|
1999-11-25 23:35:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_show(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
struct ifa *a;
|
|
|
|
char *type;
|
|
|
|
|
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
{
|
1999-11-30 20:57:14 +08:00
|
|
|
cli_msg(-1001, "%s %s (index=%d)", i->name, (i->flags & IF_UP) ? "up" : "DOWN", i->index);
|
2000-03-01 07:19:52 +08:00
|
|
|
if (!(i->flags & IF_MULTIACCESS))
|
1999-11-25 23:35:30 +08:00
|
|
|
type = "PtP";
|
|
|
|
else
|
|
|
|
type = "MultiAccess";
|
1999-11-30 20:57:14 +08:00
|
|
|
cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
|
1999-11-25 23:35:30 +08:00
|
|
|
type,
|
|
|
|
(i->flags & IF_BROADCAST) ? " Broadcast" : "",
|
|
|
|
(i->flags & IF_MULTICAST) ? " Multicast" : "",
|
|
|
|
(i->flags & IF_ADMIN_DOWN) ? "Down" : "Up",
|
|
|
|
(i->flags & IF_LINK_UP) ? "Up" : "Down",
|
|
|
|
(i->flags & IF_LOOPBACK) ? " Loopback" : "",
|
|
|
|
(i->flags & IF_IGNORE) ? " Ignored" : "",
|
|
|
|
i->mtu);
|
|
|
|
if (i->addr)
|
|
|
|
if_show_addr(i->addr);
|
|
|
|
WALK_LIST(a, i->addrs)
|
|
|
|
if (a != i->addr)
|
|
|
|
if_show_addr(a);
|
|
|
|
}
|
|
|
|
cli_msg(0, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_show_summary(void)
|
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
byte addr[STD_ADDRESS_P_LENGTH + 16];
|
|
|
|
|
1999-11-30 20:57:14 +08:00
|
|
|
cli_msg(-2005, "interface state address");
|
1999-11-25 23:35:30 +08:00
|
|
|
WALK_LIST(i, iface_list)
|
|
|
|
{
|
|
|
|
if (i->addr)
|
|
|
|
bsprintf(addr, "%I/%d", i->addr->ip, i->addr->pxlen);
|
|
|
|
else
|
|
|
|
addr[0] = 0;
|
1999-11-30 20:57:14 +08:00
|
|
|
cli_msg(-1005, "%-9s %-5s %s", i->name, (i->flags & IF_UP) ? "up" : "DOWN", addr);
|
1999-11-25 23:35:30 +08:00
|
|
|
}
|
|
|
|
cli_msg(0, "");
|
|
|
|
}
|