1998-10-18 20:50:43 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Unix Routing Table Syncing
|
|
|
|
*
|
1999-02-06 05:38:50 +08:00
|
|
|
* (c) 1998--1999 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/ioctl.h>
|
1998-10-20 01:52:29 +08:00
|
|
|
#include <net/route.h>
|
1998-10-18 20:50:43 +08:00
|
|
|
|
|
|
|
#define LOCAL_DEBUG
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "lib/unix.h"
|
|
|
|
#include "lib/krt.h"
|
|
|
|
|
1998-10-20 01:52:29 +08:00
|
|
|
int
|
1998-12-09 00:20:13 +08:00
|
|
|
krt_capable(rte *e)
|
1998-10-20 01:52:29 +08:00
|
|
|
{
|
|
|
|
rta *a = e->attrs;
|
|
|
|
|
|
|
|
return
|
|
|
|
a->cast == RTC_UNICAST &&
|
|
|
|
(a->dest == RTD_ROUTER
|
|
|
|
|| a->dest == RTD_DEVICE
|
|
|
|
#ifdef RTF_REJECT
|
|
|
|
|| a->dest == RTD_UNREACHABLE
|
|
|
|
#endif
|
|
|
|
) &&
|
|
|
|
!a->tos;
|
|
|
|
}
|
|
|
|
|
1998-12-09 02:37:58 +08:00
|
|
|
static inline int
|
|
|
|
krt_capable_op(rte *e)
|
1998-10-20 01:52:29 +08:00
|
|
|
{
|
1998-12-09 02:37:58 +08:00
|
|
|
rta *a = e->attrs;
|
1998-10-20 01:52:29 +08:00
|
|
|
|
1998-12-09 02:37:58 +08:00
|
|
|
#ifdef CONFIG_AUTO_ROUTES
|
|
|
|
if (a->dest == RTD_ROUTER && a->source == RTS_DEVICE)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
return krt_capable(e);
|
1998-10-20 01:52:29 +08:00
|
|
|
}
|
|
|
|
|
1998-12-09 02:37:58 +08:00
|
|
|
static void
|
|
|
|
krt_ioctl(int ioc, rte *e, char *name)
|
1998-10-20 01:52:29 +08:00
|
|
|
{
|
1998-12-09 02:37:58 +08:00
|
|
|
net *net = e->net;
|
1998-10-20 01:52:29 +08:00
|
|
|
struct rtentry re;
|
1998-12-09 02:37:58 +08:00
|
|
|
rta *a = e->attrs;
|
1998-10-20 01:52:29 +08:00
|
|
|
|
|
|
|
bzero(&re, sizeof(re));
|
|
|
|
fill_in_sockaddr((struct sockaddr_in *) &re.rt_dst, net->n.prefix, 0);
|
|
|
|
fill_in_sockaddr((struct sockaddr_in *) &re.rt_genmask, ipa_mkmask(net->n.pxlen), 0);
|
|
|
|
re.rt_flags = RTF_UP;
|
|
|
|
if (net->n.pxlen == 32)
|
|
|
|
re.rt_flags |= RTF_HOST;
|
|
|
|
switch (a->dest)
|
|
|
|
{
|
|
|
|
case RTD_ROUTER:
|
|
|
|
fill_in_sockaddr((struct sockaddr_in *) &re.rt_gateway, a->gw, 0);
|
|
|
|
re.rt_flags |= RTF_GATEWAY;
|
|
|
|
break;
|
|
|
|
case RTD_DEVICE:
|
|
|
|
re.rt_dev = a->iface->name;
|
|
|
|
break;
|
|
|
|
#ifdef RTF_REJECT
|
|
|
|
case RTD_UNREACHABLE:
|
|
|
|
re.rt_flags |= RTF_REJECT;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
1998-12-20 22:27:37 +08:00
|
|
|
bug("krt set: unknown flags, but not filtered");
|
1998-10-20 01:52:29 +08:00
|
|
|
}
|
|
|
|
|
1998-12-09 02:37:58 +08:00
|
|
|
if (ioctl(if_scan_sock, ioc, &re) < 0)
|
|
|
|
log(L_ERR "%s(%I/%d): %m", name, net->n.prefix, net->n.pxlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_remove_route(rte *old)
|
|
|
|
{
|
|
|
|
net *net = old->net;
|
|
|
|
|
|
|
|
if (!krt_capable_op(old))
|
|
|
|
{
|
|
|
|
DBG("krt_remove_route(ignored %I/%d)\n", net->n.prefix, net->n.pxlen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("krt_remove_route(%I/%d)\n", net->n.prefix, net->n.pxlen);
|
|
|
|
krt_ioctl(SIOCDELRT, old, "SIOCDELRT");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_add_route(rte *new)
|
|
|
|
{
|
|
|
|
net *net = new->net;
|
|
|
|
|
|
|
|
if (!krt_capable_op(new))
|
|
|
|
{
|
|
|
|
DBG("krt_add_route(ignored %I/%d)\n", net->n.prefix, net->n.pxlen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("krt_add_route(%I/%d)\n", net->n.prefix, net->n.pxlen);
|
|
|
|
krt_ioctl(SIOCADDRT, new, "SIOCADDRT");
|
1998-10-20 01:52:29 +08:00
|
|
|
}
|
|
|
|
|
1998-10-18 20:50:43 +08:00
|
|
|
void
|
|
|
|
krt_set_notify(struct proto *x, net *net, rte *new, rte *old)
|
|
|
|
{
|
1998-10-20 01:52:29 +08:00
|
|
|
if (old)
|
1998-12-09 00:20:13 +08:00
|
|
|
krt_remove_route(old);
|
1998-10-20 01:52:29 +08:00
|
|
|
if (new)
|
1998-12-09 00:20:13 +08:00
|
|
|
krt_add_route(new);
|
1998-10-18 20:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-02-06 05:38:50 +08:00
|
|
|
krt_set_start(struct krt_proto *x)
|
1998-10-18 20:50:43 +08:00
|
|
|
{
|
1998-10-20 01:52:29 +08:00
|
|
|
if (if_scan_sock < 0)
|
1998-12-20 22:27:37 +08:00
|
|
|
bug("krt set: missing socket");
|
1998-10-18 20:50:43 +08:00
|
|
|
x->p.rt_notify = krt_set_notify;
|
|
|
|
}
|
1999-02-06 05:38:50 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
krt_set_preconfig(struct krt_config *c)
|
|
|
|
{
|
|
|
|
}
|