1998-05-26 21:42:05 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Unix Interface Scanning and Syncing
|
|
|
|
*
|
1999-02-05 21:38:50 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
|
|
|
#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"
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
#include "unix.h"
|
|
|
|
|
|
|
|
int if_scan_sock;
|
|
|
|
|
|
|
|
static timer *if_scan_timer;
|
|
|
|
|
|
|
|
static void
|
|
|
|
scan_ifs(struct ifreq *r, int cnt)
|
|
|
|
{
|
|
|
|
struct iface i;
|
|
|
|
char *err;
|
|
|
|
unsigned fl;
|
|
|
|
ip_addr netmask;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
for (cnt /= sizeof(struct ifreq); cnt; cnt--, r++)
|
|
|
|
{
|
|
|
|
bzero(&i, sizeof(i));
|
1999-02-13 18:42:00 +00:00
|
|
|
DBG("%s\n", r->ifr_ifrn.ifrn_name);
|
1998-05-26 21:42:05 +00:00
|
|
|
strncpy(i.name, r->ifr_ifrn.ifrn_name, sizeof(i.name) - 1);
|
|
|
|
i.name[sizeof(i.name) - 1] = 0;
|
1998-06-01 21:41:32 +00:00
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &i.ip, NULL);
|
|
|
|
l = ipa_classify(i.ip);
|
1998-05-26 21:42:05 +00:00
|
|
|
if (l < 0 || !(l & IADDR_HOST))
|
|
|
|
{
|
|
|
|
log(L_ERR "%s: Invalid interface address", i.name);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if ((l & IADDR_SCOPE_MASK) == SCOPE_HOST)
|
|
|
|
i.flags |= IF_LOOPBACK | IF_IGNORE;
|
|
|
|
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFFLAGS, r) < 0)
|
|
|
|
{
|
|
|
|
err = "SIOCGIFFLAGS";
|
|
|
|
faulty:
|
|
|
|
log(L_ERR "%s(%s): %m", err, i.name);
|
|
|
|
bad:
|
|
|
|
i.flags = (i.flags & ~IF_UP) | IF_ADMIN_DOWN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fl = r->ifr_flags;
|
|
|
|
if (fl & IFF_UP)
|
|
|
|
i.flags |= IF_UP;
|
|
|
|
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFNETMASK, r) < 0)
|
|
|
|
{ err = "SIOCGIFNETMASK"; goto faulty; }
|
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &netmask, NULL);
|
|
|
|
l = ipa_mklen(netmask);
|
|
|
|
if (l < 0 || l == 31)
|
|
|
|
{
|
|
|
|
log(L_ERR "%s: Invalid netmask", i.name);
|
|
|
|
goto bad;
|
|
|
|
}
|
1998-06-01 21:41:32 +00:00
|
|
|
i.pxlen = l;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
|
|
|
if (fl & IFF_POINTOPOINT)
|
|
|
|
{
|
|
|
|
i.flags |= IF_UNNUMBERED;
|
1998-06-01 21:41:32 +00:00
|
|
|
i.pxlen = BITS_PER_IP_ADDRESS;
|
1998-05-26 21:42:05 +00:00
|
|
|
if (ioctl(if_scan_sock, SIOCGIFDSTADDR, r) < 0)
|
|
|
|
{ err = "SIOCGIFDSTADDR"; goto faulty; }
|
1998-06-01 21:41:32 +00:00
|
|
|
get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &i.opposite, NULL);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
if (fl & IFF_LOOPBACK)
|
|
|
|
i.flags |= IF_LOOPBACK | IF_IGNORE;
|
|
|
|
#ifndef CONFIG_ALL_MULTICAST
|
|
|
|
if (fl & IFF_MULTICAST)
|
|
|
|
#endif
|
|
|
|
i.flags |= IF_MULTICAST;
|
|
|
|
|
1998-06-01 21:41:32 +00:00
|
|
|
i.prefix = ipa_and(i.ip, ipa_mkmask(i.pxlen));
|
|
|
|
if (i.pxlen < 32)
|
1998-05-26 21:42:05 +00:00
|
|
|
{
|
1998-06-01 21:41:32 +00:00
|
|
|
i.brd = ipa_or(i.prefix, ipa_not(ipa_mkmask(i.pxlen)));
|
|
|
|
if (ipa_equal(i.ip, i.prefix) || ipa_equal(i.ip, i.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;
|
1998-06-01 21:41:32 +00:00
|
|
|
if (i.pxlen < 30)
|
1998-05-26 21:42:05 +00:00
|
|
|
i.flags |= IF_MULTIACCESS;
|
|
|
|
else
|
1998-06-01 21:41:32 +00:00
|
|
|
i.opposite = ipa_opposite(i.ip);
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
else
|
1998-06-01 21:41:32 +00:00
|
|
|
i.brd = i.opposite;
|
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
|
|
|
|
if (ioctl(if_scan_sock, SIOCGIFINDEX, r) < 0)
|
1998-10-13 19:57:33 +00:00
|
|
|
DBG("SIOCGIFINDEX failed: %m\n");
|
|
|
|
else
|
|
|
|
i.index = r->ifr_ifindex;
|
1998-05-26 21:42:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if_update(&i);
|
|
|
|
}
|
1998-06-01 21:41:32 +00:00
|
|
|
if_end_update();
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scan_if(timer *t)
|
|
|
|
{
|
|
|
|
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;
|
1998-12-08 18:37:58 +00:00
|
|
|
struct krt_proto *p = t->data;
|
1998-05-26 21:42:05 +00:00
|
|
|
|
1998-12-08 18:37:58 +00:00
|
|
|
DBG("It's interface scan time...\n");
|
1998-05-26 21:42:05 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (last_ifbuf_size)
|
|
|
|
{
|
|
|
|
struct ifreq *r = alloca(last_ifbuf_size);
|
|
|
|
ic.ifc_ifcu.ifcu_req = r;
|
|
|
|
ic.ifc_len = last_ifbuf_size;
|
|
|
|
res = ioctl(if_scan_sock, SIOCGIFCONF, &ic);
|
1998-10-13 19:57:33 +00:00
|
|
|
if (res < 0 && errno != EFAULT)
|
1998-05-26 21:42:05 +00:00
|
|
|
die("SIOCCGIFCONF: %m");
|
|
|
|
if (res < last_ifbuf_size)
|
|
|
|
{
|
|
|
|
scan_ifs(r, ic.ifc_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-10-13 19:57:33 +00:00
|
|
|
#ifdef CLEAN_WAY_WORKING_ONLY_ON_LINUX_2_1 /* FIXME */
|
|
|
|
ic.ifc_req = NULL;
|
1998-10-13 14:59:46 +00:00
|
|
|
ic.ifc_len = 999999999;
|
1998-05-26 21:42:05 +00:00
|
|
|
if (ioctl(if_scan_sock, SIOCGIFCONF, &ic) < 0)
|
|
|
|
die("SIOCIFCONF: %m");
|
|
|
|
ic.ifc_len += sizeof(struct ifreq);
|
|
|
|
if (last_ifbuf_size < ic.ifc_len)
|
|
|
|
{
|
|
|
|
last_ifbuf_size = ic.ifc_len;
|
|
|
|
DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size);
|
|
|
|
}
|
1998-10-13 19:57:33 +00:00
|
|
|
#else
|
|
|
|
last_ifbuf_size *= 2;
|
|
|
|
DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size);
|
|
|
|
#endif
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
1998-12-08 18:37:58 +00:00
|
|
|
krt_scan_ifaces_done(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_if_start(struct krt_proto *p)
|
|
|
|
{
|
1999-02-05 21:38:50 +00:00
|
|
|
struct krt_config *c = (struct krt_config *) p->p.cf;
|
|
|
|
|
|
|
|
if_scan_timer = tm_new(p->p.pool);
|
1998-12-08 18:37:58 +00:00
|
|
|
if_scan_timer->hook = scan_if;
|
|
|
|
if_scan_timer->data = p;
|
1999-02-05 21:38:50 +00:00
|
|
|
if_scan_timer->recurrent = c->ifopt.scan_time;
|
1998-12-08 18:37:58 +00:00
|
|
|
scan_if(if_scan_timer);
|
1999-02-05 21:38:50 +00:00
|
|
|
tm_start(if_scan_timer, c->ifopt.scan_time);
|
1998-12-08 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-02-05 21:38:50 +00:00
|
|
|
krt_if_preconfig(struct krt_config *c)
|
1998-12-08 18:37:58 +00:00
|
|
|
{
|
1999-02-05 21:38:50 +00:00
|
|
|
c->ifopt.scan_time = 60;
|
1998-12-08 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_if_shutdown(struct krt_proto *p)
|
|
|
|
{
|
|
|
|
tm_stop(if_scan_timer);
|
|
|
|
/* FIXME: What should we do with interfaces? */
|
1998-05-26 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
scan_if_init(void)
|
|
|
|
{
|
1998-10-19 17:48:45 +00:00
|
|
|
if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
1998-05-26 21:42:05 +00:00
|
|
|
DBG("Using socket %d for interface and route scanning\n", if_scan_sock);
|
|
|
|
if (if_scan_sock < 0)
|
|
|
|
die("Cannot create scanning socket: %m");
|
|
|
|
}
|