2004-05-31 21:25:00 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Unix Routing Table Syncing
|
|
|
|
*
|
|
|
|
* (c) 2004 Ondrej Filip <feela@network.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
|
|
|
#undef LOCAL_DEBUG
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "lib/unix.h"
|
|
|
|
#include "lib/krt.h"
|
|
|
|
#include "lib/string.h"
|
|
|
|
#include "lib/socket.h"
|
|
|
|
|
|
|
|
int rt_sock = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
krt_capable(rte *e)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
#ifdef RTF_BLACKHOLE
|
2010-02-26 17:55:58 +08:00
|
|
|
|| a->dest == RTD_BLACKHOLE
|
2004-05-31 21:25:00 +08:00
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ROUNDUP(a) \
|
|
|
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
|
|
|
|
|
|
|
#define NEXTADDR(w, u) \
|
|
|
|
if (msg.rtm.rtm_addrs & (w)) {\
|
|
|
|
l = ROUNDUP(((struct sockaddr *)&(u))->sa_len);\
|
|
|
|
memmove(body, &(u), l); body += l;}
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
#define GETADDR(p, F) \
|
|
|
|
bzero(p, sizeof(*p));\
|
|
|
|
if ((addrs & (F)) && ((struct sockaddr *)body)->sa_len) {\
|
|
|
|
unsigned int l = ROUNDUP(((struct sockaddr *)body)->sa_len);\
|
|
|
|
memcpy(p, body, (l > sizeof(*p) ? sizeof(*p) : l));\
|
|
|
|
body += l;}
|
|
|
|
|
2011-10-07 04:48:49 +08:00
|
|
|
static int
|
2004-06-07 17:09:14 +08:00
|
|
|
krt_sock_send(int cmd, rte *e)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
net *net = e->net;
|
|
|
|
rta *a = e->attrs;
|
|
|
|
static int msg_seq;
|
|
|
|
struct iface *j, *i = a->iface;
|
|
|
|
int l;
|
|
|
|
struct ks_msg msg;
|
|
|
|
char *body = (char *)msg.buf;
|
|
|
|
sockaddr gate, mask, dst;
|
2010-04-03 17:42:18 +08:00
|
|
|
ip_addr gw;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
DBG("krt-sock: send %I/%d via %I\n", net->n.prefix, net->n.pxlen, a->gw);
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2004-06-01 16:59:47 +08:00
|
|
|
bzero(&msg,sizeof (struct rt_msghdr));
|
2004-05-31 21:25:00 +08:00
|
|
|
msg.rtm.rtm_version = RTM_VERSION;
|
|
|
|
msg.rtm.rtm_type = cmd;
|
|
|
|
msg.rtm.rtm_seq = msg_seq++;
|
|
|
|
msg.rtm.rtm_addrs = RTA_DST;
|
2010-02-26 17:55:58 +08:00
|
|
|
msg.rtm.rtm_flags = RTF_UP | RTF_PROTO1;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (net->n.pxlen == MAX_PREFIX_LENGTH)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
msg.rtm.rtm_flags |= RTF_HOST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg.rtm.rtm_addrs |= RTA_NETMASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RTF_REJECT
|
|
|
|
if(a->dest == RTD_UNREACHABLE)
|
|
|
|
msg.rtm.rtm_flags |= RTF_REJECT;
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_BLACKHOLE
|
|
|
|
if(a->dest == RTD_BLACKHOLE)
|
|
|
|
msg.rtm.rtm_flags |= RTF_BLACKHOLE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is really very nasty, but I'm not able
|
|
|
|
* to add "(reject|blackhole)" route without
|
|
|
|
* gateway set
|
|
|
|
*/
|
|
|
|
if(!i)
|
|
|
|
{
|
2004-06-01 20:57:13 +08:00
|
|
|
i = HEAD(iface_list);
|
2004-06-01 18:55:10 +08:00
|
|
|
|
2004-06-01 20:57:13 +08:00
|
|
|
WALK_LIST(j, iface_list)
|
|
|
|
{
|
|
|
|
if (j->flags & IF_LOOPBACK)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2004-06-01 20:57:13 +08:00
|
|
|
i = j;
|
|
|
|
break;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-03 17:42:18 +08:00
|
|
|
gw = a->gw;
|
|
|
|
|
|
|
|
#ifdef IPV6
|
|
|
|
/* Embed interface ID to link-local address */
|
|
|
|
if (ipa_has_link_scope(gw))
|
|
|
|
_I0(gw) = 0xfe800000 | (i->index & 0x0000ffff);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fill_in_sockaddr(&dst, net->n.prefix, 0);
|
|
|
|
fill_in_sockaddr(&mask, ipa_mkmask(net->n.pxlen), 0);
|
|
|
|
fill_in_sockaddr(&gate, gw, 0);
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
switch (a->dest)
|
|
|
|
{
|
|
|
|
case RTD_ROUTER:
|
|
|
|
msg.rtm.rtm_flags |= RTF_GATEWAY;
|
|
|
|
msg.rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
break;
|
|
|
|
#ifdef RTF_REJECT
|
|
|
|
case RTD_UNREACHABLE:
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_BLACKHOLE
|
|
|
|
case RTD_BLACKHOLE:
|
|
|
|
#endif
|
|
|
|
case RTD_DEVICE:
|
|
|
|
if(i)
|
|
|
|
{
|
2010-02-10 21:57:16 +08:00
|
|
|
#ifdef RTF_CLONING
|
2004-05-31 21:25:00 +08:00
|
|
|
if (cmd == RTM_ADD && (i->flags & IF_MULTIACCESS) != IF_MULTIACCESS) /* PTP */
|
|
|
|
msg.rtm.rtm_flags |= RTF_CLONING;
|
2010-02-10 21:57:16 +08:00
|
|
|
#endif
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
if(!i->addr) {
|
2010-04-04 21:41:31 +08:00
|
|
|
log(L_ERR "KRT: interface %s has no IP addess", i->name);
|
2011-10-07 04:48:49 +08:00
|
|
|
return -1;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fill_in_sockaddr(&gate, i->addr->ip, 0);
|
|
|
|
msg.rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bug("krt-sock: unknown flags, but not filtered");
|
|
|
|
}
|
|
|
|
|
2004-06-01 18:45:28 +08:00
|
|
|
msg.rtm.rtm_index = i->index;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
NEXTADDR(RTA_DST, dst);
|
|
|
|
NEXTADDR(RTA_GATEWAY, gate);
|
|
|
|
NEXTADDR(RTA_NETMASK, mask);
|
|
|
|
|
|
|
|
l = body - (char *)&msg;
|
|
|
|
msg.rtm.rtm_msglen = l;
|
|
|
|
|
|
|
|
if ((l = write(rt_sock, (char *)&msg, l)) < 0) {
|
2011-10-09 23:01:01 +08:00
|
|
|
log(L_ERR "KRT: Error sending route %I/%d to kernel: %m", net->n.prefix, net->n.pxlen);
|
2011-10-07 04:48:49 +08:00
|
|
|
return -1;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
2011-10-07 04:48:49 +08:00
|
|
|
|
|
|
|
return 0;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-07 04:48:49 +08:00
|
|
|
krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-10-07 04:48:49 +08:00
|
|
|
int err = 0;
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
if (old)
|
2011-10-07 04:48:49 +08:00
|
|
|
krt_sock_send(RTM_DELETE, old);
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
if (new)
|
2011-10-07 04:48:49 +08:00
|
|
|
err = krt_sock_send(RTM_ADD, new);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
n->n.flags |= KRF_SYNC_ERROR;
|
|
|
|
else
|
|
|
|
n->n.flags &= ~KRF_SYNC_ERROR;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
2004-06-07 18:00:29 +08:00
|
|
|
static int
|
|
|
|
krt_set_hook(sock *sk, int size UNUSED)
|
|
|
|
{
|
|
|
|
struct ks_msg msg;
|
|
|
|
int l = read(sk->fd, (char *)&msg, sizeof(msg));
|
|
|
|
|
|
|
|
if(l <= 0)
|
|
|
|
log(L_ERR "krt-sock: read failed");
|
|
|
|
else
|
|
|
|
krt_read_msg((struct proto *)sk->data, &msg, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_set_start(struct krt_proto *x, int first UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
sock *sk_rt;
|
|
|
|
static int ks_open_tried = 0;
|
|
|
|
|
|
|
|
if (ks_open_tried)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ks_open_tried = 1;
|
|
|
|
|
|
|
|
DBG("KRT: Opening kernel socket\n");
|
|
|
|
|
|
|
|
if( (rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0)
|
|
|
|
die("Cannot open kernel socket for routes");
|
|
|
|
|
|
|
|
sk_rt = sk_new(krt_pool);
|
|
|
|
sk_rt->type = SK_MAGIC;
|
|
|
|
sk_rt->rx_hook = krt_set_hook;
|
|
|
|
sk_rt->fd = rt_sock;
|
|
|
|
sk_rt->data = x;
|
|
|
|
if (sk_open(sk_rt))
|
2004-06-01 18:53:30 +08:00
|
|
|
bug("krt-sock: sk_open failed");
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
|
|
|
|
|
2004-06-07 18:00:29 +08:00
|
|
|
static void
|
2004-05-31 21:25:00 +08:00
|
|
|
krt_read_rt(struct ks_msg *msg, struct krt_proto *p, int scan)
|
|
|
|
{
|
|
|
|
rte *e;
|
|
|
|
net *net;
|
2010-02-26 17:55:58 +08:00
|
|
|
sockaddr dst, gate, mask;
|
2004-05-31 21:25:00 +08:00
|
|
|
ip_addr idst, igate, imask;
|
|
|
|
void *body = (char *)msg->buf;
|
|
|
|
int new = (msg->rtm.rtm_type == RTM_ADD);
|
|
|
|
int src;
|
2010-02-26 17:55:58 +08:00
|
|
|
char *errmsg = "KRT: Invalid route received";
|
2004-05-31 21:25:00 +08:00
|
|
|
int flags = msg->rtm.rtm_flags;
|
|
|
|
int addrs = msg->rtm.rtm_addrs;
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (!(flags & RTF_UP) && scan)
|
|
|
|
SKIP("not up in scan\n");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (!(flags & RTF_DONE) && !scan)
|
|
|
|
SKIP("not done in async\n");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (flags & RTF_LLINFO)
|
|
|
|
SKIP("link-local\n");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
GETADDR(&dst, RTA_DST);
|
|
|
|
GETADDR(&gate, RTA_GATEWAY);
|
|
|
|
GETADDR(&mask, RTA_NETMASK);
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (sa_family_check(&dst))
|
|
|
|
get_sockaddr(&dst, &idst, NULL, 0);
|
|
|
|
else
|
|
|
|
SKIP("invalid DST");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
/* We will check later whether we have valid gateway addr */
|
|
|
|
if (sa_family_check(&gate))
|
|
|
|
get_sockaddr(&gate, &igate, NULL, 0);
|
|
|
|
else
|
|
|
|
igate = IPA_NONE;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
/* We do not test family for RTA_NETMASK, because BSD sends us
|
|
|
|
some strange values, but interpreting them as IPv4/IPv6 works */
|
2004-05-31 21:25:00 +08:00
|
|
|
get_sockaddr(&mask, &imask, NULL, 0);
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
int c = ipa_classify_net(idst);
|
|
|
|
if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
|
|
|
|
SKIP("strange class/scope\n");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
int pxlen = (flags & RTF_HOST) ? MAX_PREFIX_LENGTH : ipa_mklen(imask);
|
|
|
|
if (pxlen < 0)
|
|
|
|
{ log(L_ERR "%s (%I) - netmask %I", errmsg, idst, imask); return; }
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if ((flags & RTF_GATEWAY) && ipa_zero(igate))
|
|
|
|
{ log(L_ERR "%s (%I/%d) - missing gateway", errmsg, idst, pxlen); return; }
|
|
|
|
|
|
|
|
u32 self_mask = RTF_PROTO1;
|
2010-04-09 00:41:17 +08:00
|
|
|
u32 alien_mask = RTF_STATIC | RTF_PROTO1 | RTF_GATEWAY;
|
2010-02-26 17:55:58 +08:00
|
|
|
|
|
|
|
#ifdef RTF_PROTO2
|
|
|
|
alien_mask |= RTF_PROTO2;
|
|
|
|
#endif
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
#ifdef RTF_PROTO3
|
|
|
|
alien_mask |= RTF_PROTO3;
|
|
|
|
#endif
|
|
|
|
|
2010-04-09 00:41:17 +08:00
|
|
|
#ifdef RTF_REJECT
|
|
|
|
alien_mask |= RTF_REJECT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RTF_BLACKHOLE
|
|
|
|
alien_mask |= RTF_BLACKHOLE;
|
|
|
|
#endif
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (flags & (RTF_DYNAMIC | RTF_MODIFIED))
|
|
|
|
src = KRT_SRC_REDIRECT;
|
|
|
|
else if (flags & self_mask)
|
|
|
|
{
|
|
|
|
if (!scan)
|
|
|
|
SKIP("echo\n");
|
|
|
|
src = KRT_SRC_BIRD;
|
|
|
|
}
|
|
|
|
else if (flags & alien_mask)
|
|
|
|
src = KRT_SRC_ALIEN;
|
|
|
|
else
|
|
|
|
src = KRT_SRC_KERNEL;
|
|
|
|
|
|
|
|
net = net_get(p->p.table, idst, pxlen);
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-07-05 23:50:19 +08:00
|
|
|
rta a = {
|
|
|
|
.proto = &p->p,
|
|
|
|
.source = RTS_INHERIT,
|
|
|
|
.scope = SCOPE_UNIVERSE,
|
|
|
|
.cast = RTC_UNICAST
|
|
|
|
};
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
/* reject/blackhole routes have also set RTF_GATEWAY,
|
|
|
|
we wil check them first. */
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
#ifdef RTF_REJECT
|
|
|
|
if(flags & RTF_REJECT) {
|
|
|
|
a.dest = RTD_UNREACHABLE;
|
2010-02-26 17:55:58 +08:00
|
|
|
goto done;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RTF_BLACKHOLE
|
|
|
|
if(flags & RTF_BLACKHOLE) {
|
|
|
|
a.dest = RTD_BLACKHOLE;
|
2010-02-26 17:55:58 +08:00
|
|
|
goto done;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
a.iface = if_find_by_index(msg->rtm.rtm_index);
|
|
|
|
if (!a.iface)
|
|
|
|
{
|
|
|
|
log(L_ERR "KRT: Received route %I/%d with unknown ifindex %u",
|
|
|
|
net->n.prefix, net->n.pxlen, msg->rtm.rtm_index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & RTF_GATEWAY)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2010-02-26 17:55:58 +08:00
|
|
|
neighbor *ng;
|
|
|
|
a.dest = RTD_ROUTER;
|
|
|
|
a.gw = igate;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-04-03 17:42:18 +08:00
|
|
|
#ifdef IPV6
|
|
|
|
/* Clean up embedded interface ID returned in link-local address */
|
|
|
|
if (ipa_has_link_scope(a.gw))
|
|
|
|
_I0(a.gw) = 0xfe800000;
|
|
|
|
#endif
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
ng = neigh_find2(&p->p, &a.gw, a.iface, 0);
|
|
|
|
if (!ng || (ng->scope == SCOPE_HOST))
|
|
|
|
{
|
2010-04-09 06:56:47 +08:00
|
|
|
/* Ignore routes with next-hop 127.0.0.1, host routes with such
|
|
|
|
next-hop appear on OpenBSD for address aliases. */
|
|
|
|
if (ipa_classify(a.gw) == (IADDR_HOST | SCOPE_HOST))
|
|
|
|
return;
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
log(L_ERR "KRT: Received route %I/%d with strange next-hop %I",
|
|
|
|
net->n.prefix, net->n.pxlen, a.gw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
a.dest = RTD_DEVICE;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
done:
|
2004-05-31 21:25:00 +08:00
|
|
|
e = rte_get_temp(&a);
|
|
|
|
e->net = net;
|
|
|
|
e->u.krt.src = src;
|
2010-02-26 17:55:58 +08:00
|
|
|
|
|
|
|
/* These are probably too Linux-specific */
|
|
|
|
e->u.krt.proto = 0;
|
|
|
|
e->u.krt.type = 0;
|
2004-05-31 21:25:00 +08:00
|
|
|
e->u.krt.metric = 0;
|
|
|
|
|
|
|
|
if (scan)
|
|
|
|
krt_got_route(p, e);
|
|
|
|
else
|
|
|
|
krt_got_route_async(p, e, new);
|
|
|
|
}
|
|
|
|
|
2004-06-07 18:00:29 +08:00
|
|
|
static void
|
2004-05-31 21:25:00 +08:00
|
|
|
krt_read_ifinfo(struct ks_msg *msg)
|
|
|
|
{
|
|
|
|
struct if_msghdr *ifm = (struct if_msghdr *)&msg->rtm;
|
|
|
|
void *body = (void *)(ifm + 1);
|
|
|
|
struct sockaddr_dl *dl = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
struct iface *iface = NULL, f;
|
|
|
|
int fl = ifm->ifm_flags;
|
|
|
|
|
2011-01-08 18:31:12 +08:00
|
|
|
for (i = 1; i<=RTA_IFP; i <<= 1)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-01-08 18:31:12 +08:00
|
|
|
if (i & ifm->ifm_addrs)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-01-08 18:31:12 +08:00
|
|
|
if (i == RTA_IFP)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
dl = (struct sockaddr_dl *)body;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-08 18:31:12 +08:00
|
|
|
body += ROUNDUP(((struct sockaddr *)&(body))->sa_len);
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dl && (dl->sdl_family != AF_LINK))
|
|
|
|
{
|
|
|
|
log("Ignoring strange IFINFO");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iface = if_find_by_index(ifm->ifm_index);
|
|
|
|
|
|
|
|
if(!iface)
|
|
|
|
{
|
|
|
|
/* New interface */
|
|
|
|
if(!dl) return; /* No interface name, ignoring */
|
2011-01-08 18:22:38 +08:00
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
bzero(&f, sizeof(f));
|
|
|
|
f.index = ifm->ifm_index;
|
2011-01-08 18:22:38 +08:00
|
|
|
memcpy(f.name, dl->sdl_data, MIN(sizeof(f.name)-1, dl->sdl_nlen));
|
|
|
|
DBG("New interface '%s' found", f.name);
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&f, iface, sizeof(struct iface));
|
|
|
|
}
|
|
|
|
|
|
|
|
f.mtu = ifm->ifm_data.ifi_mtu;
|
|
|
|
f.flags = 0;
|
|
|
|
|
|
|
|
if (fl & IFF_UP)
|
2010-11-11 17:03:02 +08:00
|
|
|
f.flags |= IF_ADMIN_UP;
|
|
|
|
if (ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
|
|
|
|
f.flags |= IF_LINK_UP; /* up or unknown */
|
2004-05-31 21:25:00 +08:00
|
|
|
if (fl & IFF_LOOPBACK) /* Loopback */
|
|
|
|
f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE;
|
|
|
|
else if (fl & IFF_POINTOPOINT) /* PtP */
|
|
|
|
f.flags |= IF_MULTICAST;
|
|
|
|
else if (fl & IFF_BROADCAST) /* Broadcast */
|
|
|
|
f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST;
|
|
|
|
else
|
|
|
|
f.flags |= IF_MULTIACCESS; /* NBMA */
|
|
|
|
|
|
|
|
if((!iface) || memcmp(&f, iface, sizeof(struct iface)))
|
|
|
|
if_update(&f); /* Just if something happens */
|
|
|
|
}
|
|
|
|
|
2004-06-07 18:00:29 +08:00
|
|
|
static void
|
2004-05-31 21:25:00 +08:00
|
|
|
krt_read_addr(struct ks_msg *msg)
|
|
|
|
{
|
|
|
|
struct ifa_msghdr *ifam = (struct ifa_msghdr *)&msg->rtm;
|
|
|
|
void *body = (void *)(ifam + 1);
|
|
|
|
sockaddr addr, mask, brd;
|
|
|
|
struct iface *iface = NULL;
|
|
|
|
struct ifa ifa;
|
|
|
|
struct sockaddr null;
|
|
|
|
ip_addr iaddr, imask, ibrd;
|
|
|
|
int addrs = ifam->ifam_addrs;
|
|
|
|
int scope, masklen = -1;
|
|
|
|
int new = (ifam->ifam_type == RTM_NEWADDR);
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
/* Strange messages with zero (invalid) ifindex appear on OpenBSD */
|
|
|
|
if (ifam->ifam_index == 0)
|
|
|
|
return;
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
if(!(iface = if_find_by_index(ifam->ifam_index)))
|
|
|
|
{
|
|
|
|
log(L_ERR "KIF: Received address message for unknown interface %d", ifam->ifam_index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GETADDR (&null, RTA_DST);
|
|
|
|
GETADDR (&null, RTA_GATEWAY);
|
|
|
|
GETADDR (&mask, RTA_NETMASK);
|
|
|
|
GETADDR (&null, RTA_GENMASK);
|
|
|
|
GETADDR (&null, RTA_IFP);
|
|
|
|
GETADDR (&addr, RTA_IFA);
|
|
|
|
GETADDR (&null, RTA_AUTHOR);
|
|
|
|
GETADDR (&brd, RTA_BRD);
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
/* Some other family address */
|
|
|
|
if (!sa_family_check(&addr))
|
|
|
|
return;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
get_sockaddr(&addr, &iaddr, NULL, 0);
|
|
|
|
get_sockaddr(&mask, &imask, NULL, 0);
|
|
|
|
get_sockaddr(&brd, &ibrd, NULL, 0);
|
|
|
|
|
|
|
|
if ((masklen = ipa_mklen(imask)) < 0)
|
|
|
|
{
|
|
|
|
log("Invalid masklen");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-06-01 16:59:47 +08:00
|
|
|
bzero(&ifa, sizeof(ifa));
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
ifa.iface = iface;
|
|
|
|
|
|
|
|
memcpy(&ifa.ip, &iaddr, sizeof(ip_addr));
|
|
|
|
ifa.pxlen = masklen;
|
|
|
|
memcpy(&ifa.brd, &ibrd, sizeof(ip_addr));
|
|
|
|
|
|
|
|
scope = ipa_classify(ifa.ip);
|
|
|
|
if (scope < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, iface->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ifa.scope = scope & IADDR_SCOPE_MASK;
|
|
|
|
|
2010-04-03 17:42:18 +08:00
|
|
|
#ifdef IPV6
|
|
|
|
/* Clean up embedded interface ID returned in link-local address */
|
|
|
|
if (ipa_has_link_scope(ifa.ip))
|
2010-04-02 17:36:38 +08:00
|
|
|
_I0(ifa.ip) = 0xfe800000;
|
2010-04-03 17:42:18 +08:00
|
|
|
#endif
|
2010-04-02 17:36:38 +08:00
|
|
|
|
2012-01-08 23:28:33 +08:00
|
|
|
#ifdef IPV6
|
|
|
|
/* Why not the same check also for IPv4? */
|
|
|
|
if ((iface->flags & IF_MULTIACCESS) || (masklen != BITS_PER_IP_ADDRESS))
|
|
|
|
#else
|
2010-03-10 08:04:09 +08:00
|
|
|
if (iface->flags & IF_MULTIACCESS)
|
2012-01-08 23:28:33 +08:00
|
|
|
#endif
|
2010-04-28 06:39:57 +08:00
|
|
|
{
|
2010-03-10 08:04:09 +08:00
|
|
|
ifa.prefix = ipa_and(ifa.ip, ipa_mkmask(masklen));
|
2010-04-28 06:39:57 +08:00
|
|
|
|
2011-03-29 04:46:18 +08:00
|
|
|
if (masklen == BITS_PER_IP_ADDRESS)
|
|
|
|
ifa.flags |= IA_HOST;
|
|
|
|
|
2010-05-28 17:16:39 +08:00
|
|
|
if (masklen == (BITS_PER_IP_ADDRESS - 1))
|
2010-04-28 06:39:57 +08:00
|
|
|
ifa.opposite = ipa_opposite_m1(ifa.ip);
|
|
|
|
|
|
|
|
#ifndef IPV6
|
2010-05-28 17:16:39 +08:00
|
|
|
if (masklen == (BITS_PER_IP_ADDRESS - 2))
|
2010-04-28 06:39:57 +08:00
|
|
|
ifa.opposite = ipa_opposite_m2(ifa.ip);
|
|
|
|
#endif
|
|
|
|
}
|
2010-03-10 08:04:09 +08:00
|
|
|
else /* PtP iface */
|
|
|
|
{
|
2011-03-29 04:46:18 +08:00
|
|
|
ifa.flags |= IA_PEER;
|
2010-03-10 08:04:09 +08:00
|
|
|
ifa.prefix = ifa.opposite = ifa.brd;
|
|
|
|
}
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
if (new)
|
|
|
|
ifa_update(&ifa);
|
|
|
|
else
|
|
|
|
ifa_delete(&ifa);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_read_msg(struct proto *p, struct ks_msg *msg, int scan)
|
|
|
|
{
|
|
|
|
switch (msg->rtm.rtm_type)
|
|
|
|
{
|
|
|
|
case RTM_GET:
|
|
|
|
if(!scan) return;
|
|
|
|
case RTM_ADD:
|
|
|
|
case RTM_DELETE:
|
|
|
|
krt_read_rt(msg, (struct krt_proto *)p, scan);
|
|
|
|
break;
|
|
|
|
case RTM_IFINFO:
|
|
|
|
krt_read_ifinfo(msg);
|
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
|
|
|
krt_read_addr(msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_scan_construct(struct krt_config *c UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_scan_preconfig(struct config *c UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_scan_postconfig(struct krt_config *c UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_scan_start(struct krt_proto *x, int first UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
init_list(&x->scan.temp_ifs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_scan_shutdown(struct krt_proto *x UNUSED, int last UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-06-07 18:00:29 +08:00
|
|
|
static void
|
2009-05-22 19:37:07 +08:00
|
|
|
krt_sysctl_scan(struct proto *p, pool *pool, byte **buf, size_t *bl, int cmd)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
byte *next;
|
2011-04-07 17:31:56 +08:00
|
|
|
int mib[6];
|
2009-05-22 19:37:07 +08:00
|
|
|
size_t obl, needed;
|
2004-05-31 21:25:00 +08:00
|
|
|
struct ks_msg *m;
|
2011-04-07 17:31:56 +08:00
|
|
|
int retries = 3;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = BIRD_PF;
|
|
|
|
mib[4] = cmd;
|
|
|
|
mib[5] = 0;
|
|
|
|
|
2011-04-07 17:31:56 +08:00
|
|
|
try:
|
2010-02-26 17:55:58 +08:00
|
|
|
if (sysctl(mib, 6 , NULL , &needed, NULL, 0) < 0)
|
2011-04-07 17:31:56 +08:00
|
|
|
die("krt_sysctl_scan 1: %m");
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
obl = *bl;
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
while (needed > *bl) *bl *= 2;
|
|
|
|
while (needed < (*bl/2)) *bl /= 2;
|
2004-05-31 21:25:00 +08:00
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if ((obl!=*bl) || !*buf)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2010-02-26 17:55:58 +08:00
|
|
|
if (*buf) mb_free(*buf);
|
|
|
|
if ((*buf = mb_alloc(pool, *bl)) == NULL) die("RT scan buf alloc");
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
if (sysctl(mib, 6 , *buf, &needed, NULL, 0) < 0)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-04-07 17:31:56 +08:00
|
|
|
if (errno == ENOMEM)
|
|
|
|
{
|
|
|
|
/* The buffer size changed since last sysctl ('needed' is not changed) */
|
|
|
|
if (retries--)
|
|
|
|
goto try;
|
|
|
|
|
|
|
|
log(L_ERR "KRT: Route scan failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
die("krt_sysctl_scan 2: %m");
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (next = *buf; next < (*buf + needed); next += m->rtm.rtm_msglen)
|
|
|
|
{
|
|
|
|
m = (struct ks_msg *)next;
|
|
|
|
krt_read_msg(p, m, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
static byte *krt_buffer = NULL;
|
|
|
|
static byte *kif_buffer = NULL;
|
|
|
|
static size_t krt_buflen = 32768;
|
|
|
|
static size_t kif_buflen = 4096;
|
|
|
|
|
2004-05-31 21:25:00 +08:00
|
|
|
void
|
|
|
|
krt_scan_fire(struct krt_proto *p)
|
|
|
|
{
|
2010-02-26 17:55:58 +08:00
|
|
|
krt_sysctl_scan((struct proto *)p, p->krt_pool, &krt_buffer, &krt_buflen, NET_RT_DUMP);
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
krt_if_scan(struct kif_proto *p)
|
|
|
|
{
|
|
|
|
struct proto *P = (struct proto *)p;
|
|
|
|
if_start_update();
|
2010-02-26 17:55:58 +08:00
|
|
|
krt_sysctl_scan(P, P->pool, &kif_buffer, &kif_buflen, NET_RT_IFLIST);
|
2004-05-31 21:25:00 +08:00
|
|
|
if_end_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_set_construct(struct krt_config *c UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-08-15 08:06:56 +08:00
|
|
|
if (!krt_buffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mb_free(krt_buffer);
|
2010-02-26 17:55:58 +08:00
|
|
|
krt_buffer = NULL;
|
|
|
|
}
|
2004-05-31 21:25:00 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
krt_if_io_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_if_construct(struct kif_config *c UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_if_start(struct kif_proto *p UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-07 17:52:15 +08:00
|
|
|
krt_if_shutdown(struct kif_proto *p UNUSED)
|
2004-05-31 21:25:00 +08:00
|
|
|
{
|
2011-08-15 08:06:56 +08:00
|
|
|
if (!kif_buffer)
|
|
|
|
return;
|
|
|
|
|
2010-02-26 17:55:58 +08:00
|
|
|
mb_free(kif_buffer);
|
|
|
|
kif_buffer = NULL;
|
2004-05-31 21:25:00 +08:00
|
|
|
}
|
|
|
|
|