Commit 7b689ebe authored by dermiste's avatar dermiste

fixes and cleanup

parent 547d312f
/*
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Copyright (c) 2010 by Vincent Gross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -102,7 +103,7 @@ int
main(int argc, char **argv)
{
struct sockaddr_in6 sin6;
int rc, fd, rfd, i, opt;
int rc, fd, i, opt;
time_t expiry_time, source_expiry_time, kernel_dump_time;
char *config_file = NULL;
void *vrc;
......@@ -111,15 +112,8 @@ main(int argc, char **argv)
gettime(&now);
rfd = open(RND_DEV, O_RDONLY);
if(rfd < 0) {
perror("open(random)");
} else {
rc = read(rfd, &seed, sizeof(unsigned int));
if(rc < sizeof(unsigned int)) {
perror("read(random)");
}
}
if (read_random_bytes(&seed, sizeof(seed)) == -1)
perror("read_random_bytes(seed, sizeof(seed))");
seed ^= (now.tv_sec ^ now.tv_usec);
srandom(seed);
......@@ -390,23 +384,14 @@ main(int argc, char **argv)
fprintf(stderr,
"Warning: couldn't find router id -- using random value.\n");
if(rfd >= 0) {
rc = read(rfd, myid, 8);
if(rc < 8) {
if(read_random_bytes(myid, 8) == -1) {
perror("read(random)");
goto fail;
}
} else {
goto fail;
}
/* Clear group and global bits */
myid[0] &= ~3;
have_id:
if(rfd >= 0)
close(rfd);
rfd = -1;
reboot_time = now.tv_sec;
myseqno = (random() & 0xFFFF);
......
......@@ -80,14 +80,6 @@ THE SOFTWARE.
#endif
#endif
#if defined(__linux)
#define RND_DEV "/dev/urandom"
#elif defined(__OpenBSD__)
#define RND_DEV "/dev/arandom"
#else
#error "don't know which random device to use"
#endif
extern struct timeval now;
extern int debug;
extern time_t reboot_time;
......
......@@ -76,3 +76,33 @@ gettime(struct timeval *tv)
previous = tv->tv_sec;
return rc;
}
#if defined(__linux)
#define RND_DEV "/dev/urandom"
#elif defined (__OpenBSD__)
#define RND_DEV "/dev/arandom"
#endif
int
read_random_bytes(void *buf, size_t len)
{
int rfd;
int rc;
#ifdef RND_DEV
rfd = open(RND_DEV, O_RDONLY);
if(rfd < 0) {
rc = -1;
} else {
rc = read(rfd, buf, len);
if(rc < len)
rc = -1;
close(rfd);
}
#else
rc = -1;
errno = ENOSYS;
#endif
return rc;
}
......@@ -62,3 +62,4 @@ int kernel_addresses(char *ifname, int ifindex, int ll,
struct kernel_route *routes, int maxroutes);
int if_eui64(char *ifname, int ifindex, unsigned char *eui);
int gettime(struct timeval *tv);
int read_random_bytes(void *buf, size_t len);
/*
Copyright (c) 2007 by Grégoire Henry
Copyright (c) 2008, 2009 by Juliusz Chroboczek
Copyright (c) 2010 by Vincent Gross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -133,7 +134,7 @@ plen2mask(int n, struct in6_addr *dest)
int
kernel_setup(int setup)
{
int rc;
int rc = 0;
int forwarding = 1;
int accept_redirects = 0;
int mib[4];
......@@ -149,31 +150,26 @@ kernel_setup(int setup)
if (setup)
rc = sysctl(mib, 4, &old_forwarding, &datasize,
&forwarding, datasize);
else if (0 <= old_forwarding)
else if (old_forwarding >= 0)
rc = sysctl(mib, 4, NULL, NULL,
&old_forwarding, datasize);
if (rc == -1) {
if(errno == ENOMEM)
perror("Couldn't read forwarding knob.");
else
perror("Couldn't write forwarding knob.");
perror("Couldn't tweak forwarding knob.");
return -1;
}
rc = 0;
mib[2] = IPPROTO_ICMPV6;
mib[3] = ICMPV6CTL_REDIRACCEPT;
datasize = sizeof(old_accept_redirects);
if (setup)
rc = sysctl(mib, 4, &old_accept_redirects, &datasize,
&accept_redirects, datasize);
else if (0 <= old_accept_redirects)
else if (old_accept_redirects >= 0)
rc = sysctl(mib, 4, NULL, NULL,
&old_accept_redirects, datasize);
if (rc == -1) {
if(errno == ENOMEM)
perror("Couldn't read accept_redirects knob.");
else
perror("Couldn't write accept_redirects knob.");
perror("Couldn't tweak accept_redirects knob.");
return -1;
}
return 1;
......@@ -307,11 +303,10 @@ kernel_route(int operation, const unsigned char *dest, unsigned short plen,
const unsigned char *newgate, int newifindex,
unsigned int newmetric)
{
unsigned char msg[512];
struct rt_msghdr *rtm;
struct sockaddr_in6 *sin6;
struct sockaddr_in *sin4;
struct sockaddr_in *sin;
int rc, len, ipv4;
char local6[1][1][16] = IN6ADDR_LOOPBACK_INIT;
......@@ -368,12 +363,12 @@ kernel_route(int operation, const unsigned char *dest, unsigned short plen,
rtm->rtm_index = ifindex;
rtm->rtm_flags = RTF_UP | RTF_PROTO2;
if(plen == 128) rtm->rtm_flags |= RTF_HOST;
/* if(memcmp(nexthop->id, dest, 16) == 0) { */
/* rtm -> rtm_flags |= RTF_LLINFO; */
/* rtm -> rtm_flags |= RTF_CLONING; */
/* } else { */
/* if(memcmp(nexthop->id, dest, 16) == 0) { */
/* rtm -> rtm_flags |= RTF_LLINFO; */
/* rtm -> rtm_flags |= RTF_CLONING; */
/* } else { */
rtm->rtm_flags |= RTF_GATEWAY;
/* } */
/* } */
if(metric == KERNEL_INFINITY) {
rtm->rtm_flags |= RTF_BLACKHOLE;
if(ifindex_lo < 0) {
......@@ -405,26 +400,27 @@ kernel_route(int operation, const unsigned char *dest, unsigned short plen,
#define get_sin6_addr(dst,src) \
do { memcpy((dst), (src), 16); } while (0)
/* KAME ipv6 stack does not support IPv4 mapped IPv6, so we have to */
/* KAME ipv6 stack does not support IPv4 mapped IPv6, so we have to
* duplicate the codepath */
if(ipv4) {
sin4 = (struct sockaddr_in *)msg;
sin = (struct sockaddr_in *)msg;
/* destination */
push_sockaddr_in(sin4, sizeof(*rtm));
get_sin_addr(&(sin4->sin_addr), dest);
push_sockaddr_in(sin, sizeof(*rtm));
get_sin_addr(&(sin->sin_addr), dest);
/* gateway */
push_sockaddr_in(sin4, ROUNDUP(sin4->sin_len));
push_sockaddr_in(sin, ROUNDUP(sin->sin_len));
if (metric == KERNEL_INFINITY)
get_sin_addr(&(sin4->sin_addr),**local4);
get_sin_addr(&(sin->sin_addr),**local4);
else
get_sin_addr(&(sin4->sin_addr),gate);
get_sin_addr(&(sin->sin_addr),gate);
/* netmask */
if((rtm->rtm_addrs | RTA_NETMASK) != 0) {
struct in6_addr tmp_sin6_addr;
push_sockaddr_in(sin4, ROUNDUP(sin4->sin_len));
push_sockaddr_in(sin, ROUNDUP(sin->sin_len));
plen2mask(plen, &tmp_sin6_addr);
get_sin_addr(&(sin4->sin_addr), (char *)&tmp_sin6_addr);
get_sin_addr(&(sin->sin_addr), (char *)&tmp_sin6_addr);
}
len = (char *)sin4 + ROUNDUP(sin4->sin_len) - (char *)msg;
len = (char *)sin + ROUNDUP(sin->sin_len) - (char *)msg;
} else {
sin6 = (struct sockaddr_in6 *)msg;
/* destination */
......@@ -659,9 +655,10 @@ kernel_addresses(char *ifname, int ifindex, int ll,
if(!!ll != !!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
goto next;
memcpy(routes[i].prefix, &sin6->sin6_addr, 16);
if(ll) /* This a perfect example of counter-productive optimisation :
if(ll)
/* This a perfect example of counter-productive optimisation :
KAME encodes interface index onto bytes 2 and 3, so we have to
reset those bytes to 0 before passing them to babel. */
reset those bytes to 0 before passing them to babeld. */
memset(routes[i].prefix + 2, 0, 2);
routes[i].plen = 128;
routes[i].metric = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment