Compare commits

...

47 Commits

Author SHA1 Message Date
Adrien Gallouët
d4e70feddc Update mud 2016-10-05 08:02:48 +00:00
Adrien Gallouët
726a3eed13 Add MTU negotiation 2016-10-04 15:27:37 +00:00
Adrien Gallouët
caeed75918 Update mud 2016-09-21 16:17:20 +00:00
Adrien Gallouët
9a6795125d Update mud 2016-09-16 15:13:18 +00:00
Adrien Gallouët
db26fc5676 Update mud 2016-09-06 08:46:23 +00:00
Adrien Gallouët
05cc7b1087 Be more verbose when mud fails 2016-09-06 08:45:53 +00:00
Adrien Gallouët
6df1f9e243 Update mud 2016-09-05 09:31:34 +00:00
Adrien Gallouët
73ce84ccf7 Fixes #15 2016-08-26 11:19:52 +00:00
Adrien Gallouët
91bb0b1231 Forward tos/tclass to mud 2016-07-25 15:15:27 +00:00
Adrien Gallouët
84156a9eba Update mud 2016-07-20 15:15:11 +00:00
Adrien Gallouët
b13501b9fb Update mud 2016-07-19 16:53:53 +00:00
Adrien Gallouët
3363e219a7 Update mud 2016-07-15 18:45:36 +00:00
Adrien Gallouët
00ee23b0b6 Allow IPv4 and IPv6 only on Linux 2016-07-15 18:42:28 +00:00
Adrien Gallouët
1286b0f69e Update mud 2016-07-15 17:35:45 +00:00
Adrien Gallouët
fe5bc5454e Add chacha20 option and AES-NI negotiation 2016-07-12 17:01:41 +00:00
Adrien Gallouët
f4e94a9089 Remove -flto for now 2016-07-11 10:24:47 +00:00
Adrien Gallouët
6a7da371e2 Add .build.sh 2016-07-11 09:34:16 +00:00
Adrien Gallouët
4cf5f7a118 Update mud 2016-07-07 14:39:19 +00:00
Adrien Gallouët
35fd01f9ee Update mud 2016-07-06 13:42:30 +00:00
Adrien Gallouët
04aad57789 Update mud 2016-07-05 15:41:32 +00:00
Adrien Gallouët
7a277a8810 Update configure.ac 2016-06-30 09:52:34 +00:00
Adrien Gallouët
b232a101d2 Update mud 2016-06-30 09:41:32 +00:00
Adrien Gallouët
a01dc81500 Don't wait too long when we have data to send 2016-06-27 11:19:16 +00:00
Adrien Gallouët
1db628d84a Update mud 2016-06-24 13:21:02 +00:00
Adrien Gallouët
f11cd34dc4 Update mud 2016-06-22 17:55:18 +00:00
Adrien Gallouët
d0376e3aa5 Update mud 2016-06-22 09:18:34 +00:00
Adrien Gallouët
a7518c0e5a Update mud 2016-06-21 16:51:24 +00:00
Adrien Gallouët
378316bd68 Add mtu option 2016-06-13 15:43:45 +00:00
angt
286d6abf2d Update mud 2016-05-10 10:46:15 +00:00
angt
1f1464e90d Update mud 2016-05-09 14:23:17 +00:00
angt
55d9dd9277 Update mud 2016-05-02 09:18:44 +00:00
angt
2f290dbf85 Update mud and merge from master 2016-04-26 06:54:30 +00:00
angt
39e3f53139 Add v{4,6}only option 2016-04-25 16:29:20 +00:00
angt
babe14d544 Quit on tun error 2016-04-25 10:24:42 +00:00
angt
278fc69789 Update mud 2016-04-21 15:35:35 +00:00
angt
99262777fc Update mud 2016-04-17 10:16:13 +00:00
angt
b0f60caab2 Update mud 2016-04-14 20:12:45 +00:00
angt
efd5e0bb36 Remove down-timeout and use timeout for send-timeout 2016-04-08 14:38:07 +00:00
angt
ade4617d53 Update mud 2016-04-08 13:00:52 +00:00
angt
64a5fd8227 Update mud 2016-04-05 14:23:45 +00:00
angt
eba968797c Add {down,send,pong}-timeout and time-tolerance option 2016-04-05 14:17:07 +00:00
angt
d60f28a7fe Update mud 2016-04-05 06:42:03 +00:00
angt
32069eb104 Print tun device on STARTED and STOPPED too 2016-04-04 20:05:33 +00:00
angt
a6adcefc25 Update mud 2016-04-04 16:48:11 +00:00
angt
743b0ee0da Don't check AES-NI 2016-04-01 16:50:04 +00:00
angt
ba06a6fc10 Update mud 2016-04-01 16:37:02 +00:00
Adrien Gallouët
80d4c2814f Update README.md 2016-03-24 18:52:18 +01:00
10 changed files with 280 additions and 94 deletions

15
.build.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/bin/sh
export CC="gcc -static"
git clone https://github.com/jedisct1/libsodium --depth=1 --branch stable
cd libsodium || exit 1
./autogen.sh && ./configure --enable-minimal --disable-shared --prefix=/usr && make install
cd ..
./autogen.sh && ./configure && make
[ -x glorytun ] || exit 1
mkdir -p deploy
strip -s glorytun
mv glorytun deploy/glorytun-$(cat VERSION)-$(uname -m).bin

View File

@@ -1,4 +1,4 @@
# Glorytun
# π₁(Glorytun)=ℤ²
Small, Simple and Stupid VPN over [mud](https://github.com/angt/mud).

View File

@@ -14,8 +14,9 @@ AM_SILENT_RULES([yes])
AM_PROG_CC_C_O
AC_PROG_CC_C99
AC_USE_SYSTEM_EXTENSIONS
AC_SEARCH_LIBS([getaddrinfo], [resolv nsl])
AC_SEARCH_LIBS([socket], [socket])
AC_CHECK_LIB([rt], [clock_gettime])
AC_CHECK_FUNCS([clock_gettime])
PKG_CHECK_MODULES([libsodium], [libsodium >= 1.0.4])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

2
mud

Submodule mud updated: 1b58401095...4c34454a23

View File

@@ -4,6 +4,7 @@
struct ip_common {
uint8_t version;
uint8_t tc;
uint8_t proto;
uint8_t hdr_size;
uint16_t size;
@@ -24,11 +25,13 @@ static inline int ip_get_common (struct ip_common *ic, const uint8_t *data, size
switch (ic->version) {
case 4:
ic->tc = data[1];
ic->proto = data[9];
ic->hdr_size = (data[0]&0xF)<<2;
ic->size = ((data[2]<<8)|data[3]);
return 0;
case 6:
ic->tc = ((data[0]&0xF)<<4)|(data[1]>>4);
ic->proto = data[6];
ic->hdr_size = 40;
ic->size = ((data[4]<<8)|data[5])+40;

View File

@@ -20,8 +20,6 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <sodium.h>
#include "mud.h"
#ifndef O_CLOEXEC
@@ -29,10 +27,10 @@
#endif
static struct {
int timeout;
volatile sig_atomic_t quit;
volatile sig_atomic_t info;
uint8_t key[crypto_generichash_KEYBYTES];
int timeout;
int state_fd;
} gt;
static void fd_set_nonblock (int fd)
@@ -185,16 +183,19 @@ static size_t fd_write_all (int fd, const void *data, size_t size)
return done;
}
static int gt_setup_secretkey (char *keyfile)
static int gt_setup_secretkey (struct mud *mud, char *keyfile)
{
const size_t size = sizeof(gt.key);
unsigned char key[32];
if (str_empty(keyfile)) {
char buf[2*size+1];
char buf[2*sizeof(key)+1];
size_t size = sizeof(key);
randombytes_buf(gt.key, size);
gt_tohex(buf, sizeof(buf), gt.key, size);
state("SECRETKEY", buf);
if (mud_get_key(mud, key, &size))
return -1;
gt_tohex(buf, sizeof(buf), key, size);
state_send(gt.state_fd, "SECRETKEY", buf);
return 0;
}
@@ -210,21 +211,23 @@ static int gt_setup_secretkey (char *keyfile)
return -1;
}
char key[2*size];
size_t r = fd_read_all(fd, key, sizeof(key));
char buf[2*sizeof(key)];
size_t r = fd_read_all(fd, buf, sizeof(buf));
close(fd);
if (r!=sizeof(key)) {
if (r!=sizeof(buf)) {
gt_log("unable to read secret key\n");
return -1;
}
if (gt_fromhex(gt.key, size, key, sizeof(key))) {
if (gt_fromhex(key, sizeof(key), buf, sizeof(buf))) {
gt_log("secret key is not valid\n");
return -1;
}
mud_set_key(mud, key, sizeof(key));
return 0;
}
@@ -233,26 +236,44 @@ int main (int argc, char **argv)
gt_set_signal();
char *host = NULL;
char *port = "5000";
long port = 5000;
char *bind_list = NULL;
char *bind_port = "5000";
long bind_port = 5000;
char *dev = NULL;
char *keyfile = NULL;
char *statefile = NULL;
long mtu = 1450;
gt.timeout = 5000;
long time_tolerance = 0;
int v4 = 1;
int v6 = 0;
#ifdef __linux__
v6 = 1;
#endif
struct option opts[] = {
{ "host", &host, option_str },
{ "port", &port, option_str },
{ "bind", &bind_list, option_str },
{ "bind-port", &bind_port, option_str },
{ "dev", &dev, option_str },
{ "keyfile", &keyfile, option_str },
{ "multiqueue", NULL, option_option },
{ "statefile", &statefile, option_str },
{ "timeout", &gt.timeout, option_long },
{ "version", NULL, option_option },
{ "host", &host, option_str },
{ "port", &port, option_long },
{ "bind", &bind_list, option_str },
{ "bind-port", &bind_port, option_long },
{ "dev", &dev, option_str },
{ "mtu", &mtu, option_long },
{ "keyfile", &keyfile, option_str },
{ "multiqueue", NULL, option_option },
{ "statefile", &statefile, option_str },
{ "timeout", &gt.timeout, option_long },
{ "time-tolerance", &time_tolerance, option_long },
{ "v4only", NULL, option_option },
{ "v6only", NULL, option_option },
{ "chacha20", NULL, option_option },
{ "version", NULL, option_option },
{ NULL },
};
@@ -264,7 +285,23 @@ int main (int argc, char **argv)
return 0;
}
if (!option_is_set(opts, "keyfile")) {
if (option_is_set(opts, "v4only")) {
v4 = 1;
v6 = 0;
}
if (option_is_set(opts, "v6only")) {
v4 = 0;
v6 = 1;
}
if (option_is_set(opts, "v4only") &&
option_is_set(opts, "v6only")) {
gt_log("v4only and v6only are both set\n");
return 1;
}
if (host && !option_is_set(opts, "keyfile")) {
gt_log("keyfile option must be set\n");
return 1;
}
@@ -274,17 +311,9 @@ int main (int argc, char **argv)
return 1;
}
if (sodium_init()==-1) {
gt_log("libsodium initialization has failed\n");
return 1;
}
gt.state_fd = state_create(statefile);
if (!crypto_aead_aes256gcm_is_available()) {
gt_na("AES-256-GCM");
return 1;
}
if (state_init(statefile))
if (statefile && gt.state_fd==-1)
return 1;
char *tun_name = NULL;
@@ -296,21 +325,31 @@ int main (int argc, char **argv)
return 1;
}
if (tun_set_mtu(tun_name, mtu)==-1) {
perror("tun_set_mtu");
return 1;
}
fd_set_nonblock(tun_fd);
if (gt_setup_secretkey(keyfile))
return 1;
int chacha = option_is_set(opts, "chacha20");
struct mud *mud = mud_create(bind_port);
struct mud *mud = mud_create(bind_port, v4, v6, !chacha, mtu);
if (!mud) {
gt_log("couldn't create mud\n");
return 1;
}
mud_set_key(mud, gt.key, sizeof(gt.key));
if (gt_setup_secretkey(mud, keyfile))
return 1;
if (bind_list) {
mud_set_send_timeout_msec(mud, gt.timeout);
if (time_tolerance > 0)
mud_set_time_tolerance_sec(mud, time_tolerance);
if (host && port && bind_list) {
char tmp[1024];
char *name = &tmp[0];
@@ -322,28 +361,36 @@ int main (int argc, char **argv)
tmp[i] = 0;
if (mud_bind(mud, name))
if (mud_peer(mud, name, host, port))
return 1;
name = &tmp[i+1];
}
if (name[0] && mud_bind(mud, name))
if (name[0] && mud_peer(mud, name, host, port))
return 1;
}
if (host && mud_peer(mud, host, port))
return 1;
int mud_fd = mud_get_fd(mud);
state("INITIALIZED", tun_name);
state_send(gt.state_fd, "INITIALIZED", tun_name);
fd_set rfds;
FD_ZERO(&rfds);
int started = 0;
unsigned char buf[2048];
struct {
unsigned char *buf;
} send, recv;
send.buf = malloc(2*mtu);
recv.buf = malloc(mtu);
size_t send_size = 0;
size_t send_limit = 0;
int send_tc = 0;
int send_next_tc = 0;
while (!gt.quit) {
FD_SET(tun_fd, &rfds);
@@ -355,9 +402,12 @@ int main (int argc, char **argv)
}
struct timeval timeout = {
.tv_usec = 1000,
.tv_usec = 100000,
};
if (mud_can_push(mud) || send_size)
timeout.tv_usec = 1000;
if _0_(select(mud_fd+1, &rfds, NULL, NULL, &timeout)==-1) {
if (errno==EINTR)
continue;
@@ -367,40 +417,133 @@ int main (int argc, char **argv)
if (mud_is_up(mud)) {
if (!started) {
state("STARTED", NULL);
state_send(gt.state_fd, "STARTED", tun_name);
started = 1;
}
} else if (started) {
state("STOPPED", NULL);
started = 0;
} else {
if (started) {
state_send(gt.state_fd, "STOPPED", tun_name);
started = 0;
}
}
if (FD_ISSET(tun_fd, &rfds)) {
while (1) {
const ssize_t r = tun_read(tun_fd, buf, sizeof(buf));
while (send_size<mtu) {
const ssize_t r = tun_read(tun_fd, send.buf+send_size, mtu);
if (r<=0)
if (r<=0) {
gt.quit |= !r;
break;
}
struct ip_common ic;
if (!ip_get_common(&ic, buf, sizeof(buf)) && ic.size==r)
mud_send(mud, buf, r);
if (ip_get_common(&ic, send.buf+send_size, mtu) || ic.size!=r) {
gt_log("packet dropped: malformed\n");
continue;
}
send_size += r;
if (send_size<=mtu) {
send_limit = send_size;
if ((ic.tc&0xFC)>(send_tc&0xFC))
send_tc = ic.tc;
} else {
if ((ic.tc&0xFC)>(send_next_tc&0xFC))
send_next_tc = ic.tc;
}
}
}
mud_push(mud);
if (send_limit) {
int r = mud_send(mud, send.buf, send_limit, send_tc);
if (FD_ISSET(mud_fd, &rfds))
mud_pull(mud);
if (r==send_limit) {
if (send_size>send_limit)
memmove(send.buf, &send.buf[send_limit], send_size-send_limit);
send_size -= send_limit;
send_limit = send_size;
send_tc = send_next_tc;
send_next_tc = 0;
} else if (r==-1) {
if (errno==EMSGSIZE) {
long new_mtu = mud_get_mtu(mud);
if (new_mtu!=mtu) {
size_t total = send_size;
while (1) {
const int r = mud_recv(mud, buf, sizeof(buf));
if (tun_set_mtu(tun_name, new_mtu)==-1) {
perror("tun_set_mtu");
gt.quit |= 1;
break;
}
if (r<=0)
gt_log("MTU changed: %li\n", new_mtu);
mtu = new_mtu;
send_size = 0;
send_limit = 0;
send_tc = 0;
send_next_tc = 0;
while (send_size<total) {
struct ip_common ic;
if (ip_get_common(&ic, send.buf+send_size, mtu))
break;
send_size += ic.size;
if (send_size<=mtu) {
send_limit = send_size;
if ((ic.tc&0xFC)>(send_tc&0xFC))
send_tc = ic.tc;
} else {
if ((ic.tc&0xFC)>(send_next_tc&0xFC))
send_next_tc = ic.tc;
}
}
}
} else if (errno!=EAGAIN) {
perror("mud_send");
}
}
}
if (mud_push(mud)==-1 && errno!=EAGAIN)
perror("mud_push");
if (FD_ISSET(mud_fd, &rfds)) {
if (mud_pull(mud)==-1 && errno!=EAGAIN)
perror("mud_pull");
}
while (!gt.quit) {
const int size = mud_recv(mud, recv.buf, mtu);
if (size<=0) {
if (size==-1 && errno!=EAGAIN)
perror("mud_recv");
break;
}
tun_write(tun_fd, buf, r);
int p = 0;
while (p<size) {
struct ip_common ic;
if (ip_get_common(&ic, recv.buf+p, size-p) || ic.size>size-p)
break;
const ssize_t r = tun_write(tun_fd, recv.buf+p, ic.size);
if (r<=0) {
gt.quit |= !r;
break;
}
p += ic.size;
}
}
}

View File

@@ -7,16 +7,14 @@
#include <fcntl.h>
#include <sys/stat.h>
static int state_fd = -1;
int state_init (const char *filename)
int state_create (const char *filename)
{
if (str_empty(filename))
return 0;
return -1;
state_fd = open(filename, O_WRONLY);
int fd = open(filename, O_WRONLY);
if (state_fd==-1) {
if (fd==-1) {
if (errno!=EINTR)
perror("open");
return -1;
@@ -24,38 +22,41 @@ int state_init (const char *filename)
struct stat st = {0};
if (fstat(state_fd, &st)==-1) {
if (fstat(fd, &st)==-1) {
perror("fstat");
close(state_fd);
state_fd = -1;
close(fd);
return -1;
}
if (!S_ISFIFO(st.st_mode)) {
gt_log("`%s' is not a fifo\n", filename);
close(state_fd);
state_fd = -1;
close(fd);
return -1;
}
return 0;
return fd;
}
void state (const char *state, const char *info)
void state_send (int fd, const char *state, const char *info)
{
if (str_empty(state))
return;
if (fd==-1) {
gt_print("%s %s\n", state, info);
return;
}
const char *strs[] = { state, " ", info, "\n" };
char *str = str_cat(strs, COUNT(strs));
if (!str)
if (!str) {
perror("str_cat");
return;
if (state_fd==-1) {
gt_print("%s", str);
} else {
if (write(state_fd, str, str_len(str))==-1 && errno!=EINTR)
perror("write");
}
if (write(fd, str, str_len(str))==-1 && errno!=EINTR)
perror("write");
free(str);
}

View File

@@ -1,4 +1,4 @@
#pragma once
int state_init (const char *);
void state (const char *, const char *);
int state_create (const char *);
void state_send (int, const char *, const char *);

View File

@@ -240,3 +240,25 @@ ssize_t tun_write (int fd, const void *data, size_t size)
return ret;
#endif
}
int tun_set_mtu (char *dev_name, int mtu)
{
struct ifreq ifr = {
.ifr_mtu = mtu,
};
str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ-1);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd==-1)
return -1;
int ret = ioctl(fd, SIOCSIFMTU, &ifr);
int err = errno;
close(fd);
errno = err;
return ret;
}

View File

@@ -2,6 +2,7 @@
#include <unistd.h>
int tun_create (char *, char **, int);
ssize_t tun_read (int, void *, size_t);
ssize_t tun_write (int, const void *, size_t);
int tun_create (char *, char **, int);
ssize_t tun_read (int, void *, size_t);
ssize_t tun_write (int, const void *, size_t);
int tun_set_mtu (char *, int);