1998-05-26 21:42:05 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Unix Interface Scanning and Syncing
|
|
|
|
*
|
2000-03-12 21:58:51 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
2004-05-31 13:25:00 +00:00
|
|
|
* (c) 2004 Ondrej Filip <feela@network.cz>
|
1998-05-26 21:42:05 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
1999-04-07 14:25:56 +00:00
|
|
|
#include <sys/types.h>
|
1998-05-26 21:42:05 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-03-12 21:58:51 +00:00
|
|
|
#undef LOCAL_DEBUG
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
1998-12-08 18:37:58 +00:00
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
1998-05-26 21:42:05 +00:00
|
|
|
#include "lib/timer.h"
|
1998-12-08 18:37:58 +00:00
|
|
|
#include "lib/krt.h"
|
2000-03-31 23:30:21 +00:00
|
|
|
#include "lib/string.h"
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
#include "unix.h"
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
int if_scan_sock = -1;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
scan_ifs(struct ifreq *r, int cnt)
|
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
struct iface i, *pi;
|
|
|
|
struct ifa a;
|
1999-08-03 19:30:20 +00:00
|
|
|
char *err, *colon;
|
1998-05-26 21:42:05 +00:00
|
|
|
unsigned fl;
|
|
|
|
ip_addr netmask;
|
2000-06-01 12:58:41 +00:00
|
|
|
int l, scope;
|
2004-05-31 13:25:00 +00:00
|
|
|
sockaddr *sa;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1999-03-02 18:36:09 +00:00
|
|
|
if_start_update();
|
1998-05-26 21:42:05 +00:00
|
|
|
for (cnt /= sizeof(struct ifreq); cnt; cnt--, r++)
|
|
|
|
{
|
1999-12-16 13:23:32 +00:00
|
|
|
int sec = 0;
|
1998-05-26 21:42:05 +00:00
|
|
|
bzero(&i, sizeof(i));
|
1999-05-06 21:38:11 +00:00
|
|
|
bzero(&a, sizeof(a));
|
1999-08-03 19:30:20 +00:00
|
|
|
if (colon = strchr(r->ifr_name, ':'))
|
1999-04-12 18:07:05 +00:00
|
|
|
{
|
1999-08-03 19:30:20 +00:00
|
|
|
/* It's an alias -- let's interpret it as a secondary interface address */
|
|
|
|
sec = 1;
|
|
|
|
*colon = 0;
|
1999-04-12 18:07:05 +00:00
|
|
|
}
|
1999-04-07 14:25:56 +00:00
|
|
|
strncpy(i.name, r->ifr_name, sizeof(i.name) - 1);
|
2004-05-31 13:25:00 +00:00
|
|
|
|
|
|
|
if(ioctl(if_scan_sock, SIOCGIFADDR,r)<0) continue;
|
|
|
|
|
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.ip, NULL, 1);
|
1999-05-06 21:38:11 +00:00
|
|
|
if (ipa_nonzero(a.ip))
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
l = ipa_classify(a.ip);
|
1999-03-02 17:28:06 +00:00
|
|
|
if (l < 0 || !(l & IADDR_HOST))
|
|
|
|
{
|
|
|
|
log(L_ERR "%s: Invalid interface address", i.name);
|
1999-05-06 21:38:11 +00:00
|
|
|
a.ip = IPA_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a.scope = l & IADDR_SCOPE_MASK;
|
|
|
|
if (a.scope == SCOPE_HOST)
|
|
|
|
i.flags |= IF_LOOPBACK | IF_IGNORE;
|
1999-03-02 17:28:06 +00:00
|
|
|
}
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFFLAGS, r) < 0)
|
|
|
|
{
|
|
|
|
err = "SIOCGIFFLAGS";
|
|
|
|
faulty:
|
|
|
|
log(L_ERR "%s(%s): %m", err, i.name);
|
|
|
|
bad:
|
1999-03-02 17:28:06 +00:00
|
|
|
i.flags = (i.flags & ~IF_LINK_UP) | IF_ADMIN_DOWN;
|
1998-05-26 21:42:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fl = r->ifr_flags;
|
|
|
|
if (fl & IFF_UP)
|
1999-03-02 17:28:06 +00:00
|
|
|
i.flags |= IF_LINK_UP;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFNETMASK, r) < 0)
|
|
|
|
{ err = "SIOCGIFNETMASK"; goto faulty; }
|
2004-05-31 13:25:00 +00:00
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &netmask, NULL, 0);
|
1998-05-26 21:42:05 +00:00
|
|
|
l = ipa_mklen(netmask);
|
|
|
|
if (l < 0 || l == 31)
|
|
|
|
{
|
2004-05-31 13:25:00 +00:00
|
|
|
log(L_ERR "%s: Invalid netmask (%x)", i.name, netmask);
|
1998-05-26 21:42:05 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
1999-05-06 21:38:11 +00:00
|
|
|
a.pxlen = l;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
if (fl & IFF_POINTOPOINT)
|
|
|
|
{
|
2000-02-29 23:19:52 +00:00
|
|
|
a.flags |= IA_UNNUMBERED;
|
1998-05-26 21:42:05 +00:00
|
|
|
if (ioctl(if_scan_sock, SIOCGIFDSTADDR, r) < 0)
|
|
|
|
{ err = "SIOCGIFDSTADDR"; goto faulty; }
|
2004-05-31 13:25:00 +00:00
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.opposite, NULL, 1);
|
2000-02-29 23:19:52 +00:00
|
|
|
a.prefix = a.opposite;
|
|
|
|
a.pxlen = BITS_PER_IP_ADDRESS;
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
2000-02-29 23:19:52 +00:00
|
|
|
else
|
|
|
|
a.prefix = ipa_and(a.ip, ipa_mkmask(a.pxlen));
|
1998-05-26 21:42:05 +00:00
|
|
|
if (fl & IFF_LOOPBACK)
|
|
|
|
i.flags |= IF_LOOPBACK | IF_IGNORE;
|
1999-12-16 13:06:13 +00:00
|
|
|
if (1
|
1998-05-26 21:42:05 +00:00
|
|
|
#ifndef CONFIG_ALL_MULTICAST
|
1999-12-16 13:06:13 +00:00
|
|
|
&& (fl & IFF_MULTICAST)
|
1998-05-26 21:42:05 +00:00
|
|
|
#endif
|
1999-12-16 13:06:13 +00:00
|
|
|
#ifndef CONFIG_UNNUM_MULTICAST
|
2000-02-29 23:19:52 +00:00
|
|
|
&& !(a.flags & IA_UNNUMBERED)
|
1999-12-16 13:06:13 +00:00
|
|
|
#endif
|
|
|
|
)
|
1998-05-26 21:42:05 +00:00
|
|
|
i.flags |= IF_MULTICAST;
|
|
|
|
|
2000-06-01 12:58:41 +00:00
|
|
|
scope = ipa_classify(a.ip);
|
|
|
|
if (scope < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "%s: Invalid address", i.name);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
a.scope = scope & IADDR_SCOPE_MASK;
|
|
|
|
|
1999-05-06 21:38:11 +00:00
|
|
|
if (a.pxlen < 32)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-05-06 21:38:11 +00:00
|
|
|
a.brd = ipa_or(a.prefix, ipa_not(ipa_mkmask(a.pxlen)));
|
|
|
|
if (ipa_equal(a.ip, a.prefix) || ipa_equal(a.ip, a.brd))
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
|
|
|
log(L_ERR "%s: Using network or broadcast address for interface", i.name);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (fl & IFF_BROADCAST)
|
|
|
|
i.flags |= IF_BROADCAST;
|
1999-05-06 21:38:11 +00:00
|
|
|
if (a.pxlen < 30)
|
1998-05-26 21:42:05 +00:00
|
|
|
i.flags |= IF_MULTIACCESS;
|
|
|
|
else
|
2008-08-25 11:19:49 +00:00
|
|
|
a.opposite = ipa_opposite(a.ip, a.pxlen);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
else
|
1999-05-06 21:38:11 +00:00
|
|
|
a.brd = a.opposite;
|
2000-06-01 12:58:41 +00:00
|
|
|
a.scope = SCOPE_UNIVERSE;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFMTU, r) < 0)
|
|
|
|
{ err = "SIOCGIFMTU"; goto faulty; }
|
|
|
|
i.mtu = r->ifr_mtu;
|
|
|
|
|
|
|
|
#ifdef SIOCGIFINDEX
|
1999-12-16 13:23:32 +00:00
|
|
|
if (ioctl(if_scan_sock, SIOCGIFINDEX, r) >= 0)
|
1998-10-13 19:57:33 +00:00
|
|
|
i.index = r->ifr_ifindex;
|
2008-11-21 12:01:05 +00:00
|
|
|
else if (errno != EINVAL)
|
1999-12-16 13:23:32 +00:00
|
|
|
DBG("SIOCGIFINDEX failed: %m\n");
|
|
|
|
else /* defined, but not supported by the kernel */
|
|
|
|
#endif
|
1999-08-03 19:30:20 +00:00
|
|
|
/*
|
|
|
|
* The kernel doesn't give us real ifindices, but we still need them
|
|
|
|
* at least for OSPF unnumbered links. So let's make them up ourselves.
|
|
|
|
*/
|
|
|
|
if (pi = if_find_by_name(i.name))
|
|
|
|
i.index = pi->index;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static int if_index_counter = 1;
|
|
|
|
i.index = if_index_counter++;
|
|
|
|
}
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1999-08-03 19:30:20 +00:00
|
|
|
pi = NULL;
|
|
|
|
if (sec)
|
|
|
|
{
|
|
|
|
a.flags |= IA_SECONDARY;
|
|
|
|
pi = if_find_by_index(i.index);
|
|
|
|
}
|
|
|
|
if (!pi)
|
|
|
|
pi = if_update(&i);
|
1999-05-06 21:38:11 +00:00
|
|
|
a.iface = pi;
|
|
|
|
ifa_update(&a);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
1998-06-01 21:41:32 +00:00
|
|
|
if_end_update();
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
1999-03-03 19:49:56 +00:00
|
|
|
void
|
1999-03-26 21:44:38 +00:00
|
|
|
krt_if_scan(struct kif_proto *p)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
|
|
|
struct ifconf ic;
|
1998-10-13 19:57:33 +00:00
|
|
|
static int last_ifbuf_size = 4*sizeof(struct ifreq);
|
1998-05-26 21:42:05 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2004-05-31 13:25:00 +00:00
|
|
|
ic.ifc_buf = alloca(last_ifbuf_size);
|
|
|
|
ic.ifc_len = last_ifbuf_size;
|
|
|
|
res = ioctl(if_scan_sock, SIOCGIFCONF, &ic);
|
|
|
|
if (res < 0 && errno != EFAULT)
|
|
|
|
die("SIOCCGIFCONF: %m");
|
|
|
|
if (res >= 0 && ic.ifc_len <= last_ifbuf_size)
|
|
|
|
break;
|
1998-10-13 19:57:33 +00:00
|
|
|
last_ifbuf_size *= 2;
|
|
|
|
DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
2004-05-31 13:25:00 +00:00
|
|
|
scan_ifs(ic.ifc_req, ic.ifc_len);
|
1998-12-08 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-03 19:33:22 +00:00
|
|
|
krt_if_construct(struct kif_config *c)
|
1998-12-08 18:37:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-26 21:44:38 +00:00
|
|
|
krt_if_start(struct kif_proto *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_if_shutdown(struct kif_proto *p)
|
1998-12-08 18:37:58 +00:00
|
|
|
{
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-26 21:44:38 +00:00
|
|
|
krt_if_io_init(void)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1999-03-26 21:44:38 +00:00
|
|
|
if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
DBG("Using socket %d for interface and route scanning\n", if_scan_sock);
|
|
|
|
if (if_scan_sock < 0)
|
|
|
|
die("Cannot create scanning socket: %m");
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
2004-05-31 13:25:00 +00:00
|
|
|
|