1998-10-18 20:50:43 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Linux Routing Table Scanning
|
|
|
|
*
|
2000-03-13 05:54:39 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-10-18 20:50:43 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
1998-10-19 06:25:56 +08:00
|
|
|
#include <stdio.h>
|
1998-12-09 02:37:58 +08:00
|
|
|
#include <ctype.h>
|
1998-10-19 06:25:56 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <net/route.h>
|
1998-10-18 20:50:43 +08:00
|
|
|
|
2000-03-13 05:54:39 +08:00
|
|
|
#undef LOCAL_DEBUG
|
1998-10-18 20:50:43 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
1998-10-19 06:25:56 +08:00
|
|
|
#include "nest/iface.h"
|
1998-10-18 20:50:43 +08:00
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "lib/unix.h"
|
|
|
|
#include "lib/krt.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1998-10-18 20:50:43 +08:00
|
|
|
|
1998-10-19 06:25:56 +08:00
|
|
|
static int krt_scan_fd = -1;
|
|
|
|
|
1998-12-09 02:37:58 +08:00
|
|
|
struct iface *
|
1999-03-04 03:49:56 +08:00
|
|
|
krt_temp_iface(struct krt_proto *p, char *name)
|
1998-12-09 02:37:58 +08:00
|
|
|
{
|
|
|
|
struct iface *i;
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
WALK_LIST(i, p->scan.temp_ifs)
|
1998-12-09 02:37:58 +08:00
|
|
|
if (!strcmp(i->name, name))
|
|
|
|
return i;
|
1999-03-04 19:40:05 +08:00
|
|
|
i = mb_allocz(p->p.pool, sizeof(struct iface));
|
1998-12-09 02:37:58 +08:00
|
|
|
strcpy(i->name, name);
|
1999-03-04 03:49:56 +08:00
|
|
|
add_tail(&p->scan.temp_ifs, &i->n);
|
1998-12-09 02:37:58 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1998-10-21 00:39:04 +08:00
|
|
|
static void
|
1999-03-04 03:49:56 +08:00
|
|
|
krt_parse_entry(byte *ent, struct krt_proto *p)
|
1998-10-19 06:25:56 +08:00
|
|
|
{
|
|
|
|
u32 dest0, gw0, mask0;
|
|
|
|
ip_addr dest, gw, mask;
|
1999-03-04 03:49:56 +08:00
|
|
|
unsigned int flags;
|
1998-10-19 06:25:56 +08:00
|
|
|
int masklen;
|
|
|
|
net *net;
|
1998-12-09 00:20:13 +08:00
|
|
|
byte *iface = ent;
|
|
|
|
rta a;
|
1999-03-04 03:49:56 +08:00
|
|
|
rte *e;
|
1998-10-19 06:25:56 +08:00
|
|
|
|
1998-12-09 00:20:13 +08:00
|
|
|
if (sscanf(ent, "%*s\t%x\t%x\t%x\t%*d\t%*d\t%*d\t%x\t", &dest0, &gw0, &flags, &mask0) != 4)
|
1998-10-19 06:25:56 +08:00
|
|
|
{
|
1998-12-09 00:20:13 +08:00
|
|
|
log(L_ERR "krt read: unable to parse `%s'", ent);
|
1998-10-19 06:25:56 +08:00
|
|
|
return;
|
|
|
|
}
|
1998-12-09 00:20:13 +08:00
|
|
|
while (*ent != '\t')
|
|
|
|
ent++;
|
|
|
|
*ent = 0;
|
1998-10-19 06:25:56 +08:00
|
|
|
|
|
|
|
dest = ipa_from_u32(dest0);
|
|
|
|
ipa_ntoh(dest);
|
|
|
|
gw = ipa_from_u32(gw0);
|
|
|
|
ipa_ntoh(gw);
|
|
|
|
mask = ipa_from_u32(mask0);
|
|
|
|
ipa_ntoh(mask);
|
|
|
|
if ((masklen = ipa_mklen(mask)) < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "krt read: invalid netmask %08x", mask0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("Got %I/%d via %I flags %x\n", dest, masklen, gw, flags);
|
|
|
|
|
|
|
|
if (!(flags & RTF_UP))
|
1998-12-09 02:37:58 +08:00
|
|
|
{
|
|
|
|
DBG("Down.\n");
|
|
|
|
return;
|
|
|
|
}
|
1998-10-19 06:25:56 +08:00
|
|
|
if (flags & RTF_HOST)
|
|
|
|
masklen = 32;
|
|
|
|
if (flags & (RTF_DYNAMIC | RTF_MODIFIED)) /* Redirect route */
|
|
|
|
{
|
|
|
|
log(L_WARN "krt: Ignoring redirect to %I/%d via %I", dest, masklen, gw);
|
|
|
|
return;
|
|
|
|
}
|
1998-12-09 00:20:13 +08:00
|
|
|
|
1999-05-18 04:16:53 +08:00
|
|
|
net = net_get(p->p.table, dest, masklen);
|
1998-12-09 02:37:58 +08:00
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
a.proto = &p->p;
|
1998-12-09 00:20:13 +08:00
|
|
|
a.source = RTS_INHERIT;
|
|
|
|
a.scope = SCOPE_UNIVERSE;
|
|
|
|
a.cast = RTC_UNICAST;
|
1999-04-13 02:01:07 +08:00
|
|
|
a.flags = a.aflags = 0;
|
1998-12-09 00:20:13 +08:00
|
|
|
a.from = IPA_NONE;
|
|
|
|
a.iface = NULL;
|
1999-11-04 21:29:43 +08:00
|
|
|
a.eattrs = NULL;
|
1998-12-09 00:20:13 +08:00
|
|
|
|
|
|
|
if (flags & RTF_GATEWAY)
|
1998-10-19 06:25:56 +08:00
|
|
|
{
|
1999-03-04 03:49:56 +08:00
|
|
|
neighbor *ng = neigh_find(&p->p, &gw, 0);
|
2000-06-01 20:58:41 +08:00
|
|
|
if (ng && ng->scope)
|
1998-12-09 00:20:13 +08:00
|
|
|
a.iface = ng->iface;
|
|
|
|
else
|
2009-09-25 01:08:14 +08:00
|
|
|
{
|
|
|
|
log(L_WARN "Kernel told us to use non-neighbor %I for %I/%d", ra.gw, net->n.prefix, net->n.pxlen);
|
|
|
|
return;
|
|
|
|
}
|
1998-12-09 00:20:13 +08:00
|
|
|
a.dest = RTD_ROUTER;
|
|
|
|
a.gw = gw;
|
|
|
|
}
|
|
|
|
else if (flags & RTF_REJECT)
|
|
|
|
{
|
|
|
|
a.dest = RTD_UNREACHABLE;
|
|
|
|
a.gw = IPA_NONE;
|
1998-10-19 06:25:56 +08:00
|
|
|
}
|
1998-12-09 02:37:58 +08:00
|
|
|
else if (isalpha(iface[0]))
|
|
|
|
{
|
|
|
|
a.dest = RTD_DEVICE;
|
|
|
|
a.gw = IPA_NONE;
|
1999-03-04 03:49:56 +08:00
|
|
|
a.iface = krt_temp_iface(p, iface);
|
1998-12-09 02:37:58 +08:00
|
|
|
}
|
1998-10-19 06:25:56 +08:00
|
|
|
else
|
|
|
|
{
|
1998-12-09 02:37:58 +08:00
|
|
|
log(L_WARN "Kernel reporting unknown route type to %I/%d", net->n.prefix, net->n.pxlen);
|
1998-12-09 00:20:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = rte_get_temp(&a);
|
|
|
|
e->net = net;
|
1999-04-03 21:05:18 +08:00
|
|
|
e->u.krt.src = KRT_SRC_UNKNOWN;
|
1999-03-04 03:49:56 +08:00
|
|
|
krt_got_route(p, e);
|
1998-10-19 06:25:56 +08:00
|
|
|
}
|
|
|
|
|
1999-03-04 03:49:56 +08:00
|
|
|
void
|
|
|
|
krt_scan_fire(struct krt_proto *p)
|
1998-10-19 06:25:56 +08:00
|
|
|
{
|
|
|
|
byte buf[32768];
|
|
|
|
int l, seen_hdr;
|
|
|
|
|
|
|
|
if (krt_scan_fd < 0)
|
|
|
|
{
|
|
|
|
krt_scan_fd = open("/proc/net/route", O_RDONLY);
|
|
|
|
if (krt_scan_fd < 0)
|
|
|
|
die("/proc/net/route: %m");
|
|
|
|
}
|
|
|
|
else if (lseek(krt_scan_fd, 0, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "krt seek: %m");
|
1999-03-04 03:49:56 +08:00
|
|
|
return;
|
1998-10-19 06:25:56 +08:00
|
|
|
}
|
|
|
|
seen_hdr = 0;
|
|
|
|
while ((l = read(krt_scan_fd, buf, sizeof(buf))) > 0)
|
|
|
|
{
|
|
|
|
byte *z = buf;
|
|
|
|
if (l & 127)
|
|
|
|
{
|
|
|
|
log(L_ERR "krt read: misaligned entry: l=%d", l);
|
1999-03-04 03:49:56 +08:00
|
|
|
return;
|
1998-10-19 06:25:56 +08:00
|
|
|
}
|
|
|
|
while (l >= 128)
|
|
|
|
{
|
|
|
|
if (seen_hdr++)
|
1998-10-21 00:39:04 +08:00
|
|
|
krt_parse_entry(z, p);
|
1998-10-19 06:25:56 +08:00
|
|
|
z += 128;
|
|
|
|
l -= 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "krt read: %m");
|
1999-03-04 03:49:56 +08:00
|
|
|
return;
|
1998-10-19 06:25:56 +08:00
|
|
|
}
|
|
|
|
DBG("KRT scan done, seen %d lines\n", seen_hdr);
|
1998-10-18 20:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_scan_construct(struct krt_config *c)
|
1998-10-18 20:50:43 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_scan_preconfig(struct config *c)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_scan_postconfig(struct krt_config *c)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_scan_start(struct krt_proto *x, int first)
|
1998-10-18 20:50:43 +08:00
|
|
|
{
|
1999-03-04 03:49:56 +08:00
|
|
|
init_list(&x->scan.temp_ifs);
|
1998-10-18 20:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-04 03:33:22 +08:00
|
|
|
krt_scan_shutdown(struct krt_proto *x, int last)
|
1998-10-18 20:50:43 +08:00
|
|
|
{
|
|
|
|
}
|