Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
286f54aed4 | ||
|
|
6ef8ca45d7 | ||
|
|
85ddb8a8d6 | ||
|
|
a261f1a8b1 | ||
|
|
53a55e83c4 | ||
|
|
20bdaa22e8 | ||
|
|
246f1bd7c0 | ||
|
|
6095cc021a | ||
|
|
2ad21e9375 | ||
|
|
21ae1f34c3 | ||
|
|
a8ebefbef3 | ||
|
|
14c0c2edb1 | ||
|
|
b8148600f2 | ||
|
|
164c32c23c | ||
|
|
a5e415736d | ||
|
|
0359c21643 | ||
|
|
725a8e2fd0 | ||
|
|
dae5d4a800 | ||
|
|
704e663d6a | ||
|
|
c63885a748 | ||
|
|
8530e4c378 | ||
|
|
4944e76f97 | ||
|
|
5865e61fd2 | ||
|
|
8855ce75fc | ||
|
|
3e1809a608 | ||
|
|
04370f0aa0 |
11
Makefile.am
11
Makefile.am
@@ -1,4 +1,13 @@
|
||||
bin_PROGRAMS = glorytun
|
||||
glorytun_SOURCES = src/common.h src/common-static.h src/main.c src/option.c src/option.h
|
||||
glorytun_CFLAGS = $(libsodium_CFLAGS)
|
||||
glorytun_LDADD = $(libsodium_LIBS)
|
||||
glorytun_SOURCES = \
|
||||
src/common.h \
|
||||
src/common-static.h \
|
||||
src/common.c \
|
||||
src/ip-static.h \
|
||||
src/main.c \
|
||||
src/option.c \
|
||||
src/option.h \
|
||||
src/tun.c \
|
||||
src/tun.h
|
||||
|
||||
@@ -16,5 +16,5 @@ To build and install the latest version:
|
||||
|
||||
To create and use a new secret key:
|
||||
|
||||
$ dd if=/dev/random iflag=fullblock of=glorytun.key bs=32 count=1
|
||||
$ dd if=/dev/urandom of=glorytun.key bs=32 count=1
|
||||
# glorytun keyfile glorytun.key [...]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
AC_PREREQ([2.65])
|
||||
AC_INIT([glorytun], [0.0.1], [https://github.com/angt/glorytun/issues],
|
||||
AC_INIT([glorytun], [0.0.2], [https://github.com/angt/glorytun/issues],
|
||||
[glorytun], [https://github.com/angt/glorytun])
|
||||
AC_CONFIG_SRCDIR([src/common.h])
|
||||
AC_CONFIG_AUX_DIR([build-aux])
|
||||
|
||||
27
src/common.c
Normal file
27
src/common.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void gt_log (const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void gt_fatal (const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void gt_na (const char *name)
|
||||
{
|
||||
gt_log("%s is not available on your platform!\n", name);
|
||||
}
|
||||
@@ -14,6 +14,8 @@
|
||||
#define PALIGN(x) ((void *)ALIGN((size_t)(x)))
|
||||
#define PALIGN_DOWN(x) ((void *)ALIGN_DOWN((size_t)(x)))
|
||||
|
||||
#define _printf_(A,B) __attribute__((format(printf,A,B)))
|
||||
#define _noreturn_ __attribute__((noreturn))
|
||||
#define _unused_ __attribute__((unused))
|
||||
|
||||
typedef struct buffer buffer_t;
|
||||
@@ -24,3 +26,7 @@ struct buffer {
|
||||
uint8_t *write;
|
||||
uint8_t *end;
|
||||
};
|
||||
|
||||
void gt_log (const char *, ...) _printf_(1,2);
|
||||
void gt_fatal (const char *, ...) _printf_(1,2) _noreturn_;
|
||||
void gt_na (const char *);
|
||||
|
||||
30
src/ip-static.h
Normal file
30
src/ip-static.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static inline int ip_get_version (const uint8_t *data, size_t size)
|
||||
{
|
||||
if (size<20) // XXX
|
||||
return -1; // XXX
|
||||
|
||||
return data[0]>>4;
|
||||
}
|
||||
|
||||
static inline void ip_set_size (uint8_t *data, size_t size)
|
||||
{
|
||||
data[2] = 0xFF&(size>>8);
|
||||
data[3] = 0xFF&(size);
|
||||
}
|
||||
|
||||
static inline ssize_t ip_get_size (const uint8_t *data, size_t size)
|
||||
{
|
||||
switch (ip_get_version(data, size)) {
|
||||
case 4:
|
||||
return (data[2]<<8)|data[3];
|
||||
case -1:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
313
src/main.c
313
src/main.c
@@ -1,27 +1,23 @@
|
||||
#include "common-static.h"
|
||||
#include "option.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifdef __linux__
|
||||
# include <linux/if.h>
|
||||
# include <linux/if_tun.h>
|
||||
#endif
|
||||
|
||||
#include <sodium.h>
|
||||
|
||||
#include "common-static.h"
|
||||
#include "ip-static.h"
|
||||
|
||||
#include "option.h"
|
||||
#include "tun.h"
|
||||
|
||||
#ifndef O_CLOEXEC
|
||||
#define O_CLOEXEC 0
|
||||
#endif
|
||||
@@ -33,24 +29,18 @@ struct netio {
|
||||
int fd;
|
||||
struct {
|
||||
buffer_t buf;
|
||||
ssize_t ret;
|
||||
} write, read;
|
||||
};
|
||||
|
||||
struct crypto_ctx {
|
||||
crypto_aead_aes256gcm_state state_r;
|
||||
crypto_aead_aes256gcm_state state_w;
|
||||
uint8_t nonce_r[crypto_aead_aes256gcm_NPUBBYTES];
|
||||
uint8_t nonce_w[crypto_aead_aes256gcm_NPUBBYTES];
|
||||
struct {
|
||||
crypto_aead_aes256gcm_state state;
|
||||
uint8_t nonce[crypto_aead_aes256gcm_NPUBBYTES];
|
||||
} write, read;
|
||||
uint8_t skey[crypto_generichash_KEYBYTES];
|
||||
};
|
||||
|
||||
volatile sig_atomic_t running;
|
||||
|
||||
static void gt_not_available (const char *name)
|
||||
{
|
||||
fprintf(stderr, "%s is not available on your platform!\n", name);
|
||||
}
|
||||
volatile sig_atomic_t gt_close = 0;
|
||||
|
||||
static int64_t dt_ms (struct timeval *ta, struct timeval *tb)
|
||||
{
|
||||
@@ -93,6 +83,14 @@ static void sk_set_reuseaddr (int fd)
|
||||
perror("setsockopt SO_REUSEADDR");
|
||||
}
|
||||
|
||||
static void sk_set_keepalive (int fd)
|
||||
{
|
||||
int val = 1;
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val))==-1)
|
||||
perror("setsockopt SO_KEEPALIVE");
|
||||
}
|
||||
|
||||
#ifdef TCP_CONGESTION
|
||||
static void sk_set_congestion (int fd, const char *name)
|
||||
{
|
||||
@@ -107,7 +105,7 @@ static void sk_set_congestion (int fd, const char *name)
|
||||
#else
|
||||
static void sk_set_congestion (_unused_ int fd, _unused_ const char *name)
|
||||
{
|
||||
gt_not_available("TCP_CONGESTION");
|
||||
gt_na("TCP_CONGESTION");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -222,8 +220,7 @@ static socklen_t sk_get_info (int fd, struct tcp_info *ti)
|
||||
|
||||
static void print_tcp_info (const char *name, struct tcp_info *ti)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"%s: tcpinfo"
|
||||
gt_log("%s: tcpinfo"
|
||||
" rto:%" PRIu32 " ato:%" PRIu32 " snd_mss:%" PRIu32
|
||||
" rcv_mss:%" PRIu32 " unacked:%" PRIu32 " sacked:%" PRIu32
|
||||
" lost:%" PRIu32 " retrans:%" PRIu32 " fackets:%" PRIu32
|
||||
@@ -243,7 +240,7 @@ static void print_tcp_info (const char *name, struct tcp_info *ti)
|
||||
static struct addrinfo *ai_create (const char *host, const char *port, int listener)
|
||||
{
|
||||
if (!port || !port[0]) {
|
||||
fprintf(stderr, "port is not valid\n");
|
||||
gt_log("port is not valid\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -270,74 +267,21 @@ static struct addrinfo *ai_create (const char *host, const char *port, int liste
|
||||
break;
|
||||
case EAI_FAIL:
|
||||
case EAI_AGAIN:
|
||||
fprintf(stderr, "the name server returned a failure\n");
|
||||
gt_log("the name server returned a failure\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s.%s is not valid\n", host?:"", port);
|
||||
gt_log("%s.%s is not valid\n", host?:"", port);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
static int tun_create (char *name, int multiqueue)
|
||||
{
|
||||
int fd = open("/dev/net/tun", O_RDWR);
|
||||
|
||||
if (fd<0) {
|
||||
perror("open /dev/net/tun");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr = {
|
||||
.ifr_flags = IFF_TUN|IFF_NO_PI,
|
||||
};
|
||||
|
||||
if (multiqueue) {
|
||||
#ifdef IFF_MULTI_QUEUE
|
||||
ifr.ifr_flags |= IFF_MULTI_QUEUE;
|
||||
#else
|
||||
gt_not_available("IFF_MULTI_QUEUE");
|
||||
#endif
|
||||
}
|
||||
|
||||
str_cpy(ifr.ifr_name, name, IFNAMSIZ-1);
|
||||
|
||||
int ret = ioctl(fd, TUNSETIFF, &ifr);
|
||||
|
||||
if (ret<0) {
|
||||
perror("ioctl TUNSETIFF");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("tun name: %s\n", ifr.ifr_name);
|
||||
|
||||
return fd;
|
||||
}
|
||||
#else
|
||||
static int tun_create (_unused_ char *name, _unused_ int mq)
|
||||
{
|
||||
for (unsigned dev_id = 0U; dev_id < 32U; dev_id++) {
|
||||
char dev_path[11U];
|
||||
|
||||
snprintf(dev_path, sizeof(dev_path), "/dev/tun%u", dev_id);
|
||||
|
||||
int fd = open(dev_path, O_RDWR);
|
||||
|
||||
if (fd!=-1)
|
||||
return fd;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void gt_sa_stop (int sig)
|
||||
{
|
||||
switch (sig) {
|
||||
case SIGINT:
|
||||
case SIGTERM:
|
||||
running = 0;
|
||||
gt_close = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,7 +290,6 @@ static void gt_set_signal (void)
|
||||
struct sigaction sa;
|
||||
|
||||
byte_set(&sa, 0, sizeof(sa));
|
||||
running = 1;
|
||||
|
||||
sa.sa_handler = gt_sa_stop;
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
@@ -407,7 +350,7 @@ static ssize_t fd_read_all (int fd, void *data, size_t size)
|
||||
};
|
||||
|
||||
while (done<size) {
|
||||
ssize_t ret = fd_read(fd, data+done, size-done);
|
||||
ssize_t ret = fd_read(fd, (uint8_t *)data+done, size-done);
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
@@ -434,7 +377,7 @@ static ssize_t fd_write_all (int fd, const void *data, size_t size)
|
||||
};
|
||||
|
||||
while (done<size) {
|
||||
ssize_t ret = fd_write(fd, data+done, size-done);
|
||||
ssize_t ret = fd_write(fd, (const uint8_t *)data+done, size-done);
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
@@ -466,10 +409,10 @@ static int encrypt_packet (struct crypto_ctx *ctx, uint8_t *packet, size_t size,
|
||||
buffer->write + hs, NULL,
|
||||
packet + hs, size - hs,
|
||||
packet, hs,
|
||||
NULL, ctx->nonce_w,
|
||||
(const crypto_aead_aes256gcm_state *)&ctx->state_w);
|
||||
NULL, ctx->write.nonce,
|
||||
(const crypto_aead_aes256gcm_state *)&ctx->write.state);
|
||||
|
||||
sodium_increment(ctx->nonce_w, crypto_aead_aes256gcm_NPUBBYTES);
|
||||
sodium_increment(ctx->write.nonce, crypto_aead_aes256gcm_NPUBBYTES);
|
||||
buffer->write += ws;
|
||||
|
||||
return 0;
|
||||
@@ -491,11 +434,11 @@ static int decrypt_packet (struct crypto_ctx *ctx, uint8_t *packet, size_t size,
|
||||
NULL,
|
||||
buffer->read + hs, rs - hs,
|
||||
packet, hs,
|
||||
ctx->nonce_r,
|
||||
(const crypto_aead_aes256gcm_state *)&ctx->state_r))
|
||||
ctx->read.nonce,
|
||||
(const crypto_aead_aes256gcm_state *)&ctx->read.state))
|
||||
return -1;
|
||||
|
||||
sodium_increment(ctx->nonce_r, crypto_aead_aes256gcm_NPUBBYTES);
|
||||
sodium_increment(ctx->read.nonce, crypto_aead_aes256gcm_NPUBBYTES);
|
||||
buffer->read += rs;
|
||||
|
||||
return 0;
|
||||
@@ -516,24 +459,7 @@ static void dump_ip_header (uint8_t *data, size_t size)
|
||||
|
||||
hex[40] = 0;
|
||||
|
||||
fprintf(stderr, "DUMP(%zu): %s\n", size, hex);
|
||||
}
|
||||
|
||||
static void set_ip_size (uint8_t *data, size_t size)
|
||||
{
|
||||
data[2] = 0xFF&(size>>8);
|
||||
data[3] = 0xFF&(size);
|
||||
}
|
||||
|
||||
static ssize_t get_ip_size (const uint8_t *data, size_t size)
|
||||
{
|
||||
if (size<20)
|
||||
return -1;
|
||||
|
||||
if ((data[0]>>4)==4)
|
||||
return (data[2]<<8)|data[3];
|
||||
|
||||
return 0;
|
||||
gt_log("DUMP(%zu): %s\n", size, hex);
|
||||
}
|
||||
|
||||
static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
|
||||
@@ -553,13 +479,11 @@ static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
|
||||
}
|
||||
|
||||
if (fd_read_all(fd, ctx->skey, size)!=size) {
|
||||
fprintf(stderr, "unable to read secret key in `%s'\n", keyfile);
|
||||
gt_log("unable to read secret key in `%s'\n", keyfile);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: check key
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
@@ -569,22 +493,25 @@ static int gt_setup_crypto (struct crypto_ctx *ctx, int fd, int listener)
|
||||
{
|
||||
const size_t nonce_size = crypto_aead_aes256gcm_NPUBBYTES;
|
||||
const size_t public_size = crypto_scalarmult_SCALARBYTES;
|
||||
const size_t hkey_size = crypto_generichash_BYTES;
|
||||
const size_t size = nonce_size + public_size + hkey_size;
|
||||
const size_t hash_size = crypto_generichash_BYTES;
|
||||
const size_t size = nonce_size + public_size + hash_size;
|
||||
|
||||
uint8_t secret[crypto_scalarmult_SCALARBYTES];
|
||||
uint8_t shared[crypto_scalarmult_BYTES];
|
||||
uint8_t key[crypto_aead_aes256gcm_KEYBYTES];
|
||||
|
||||
uint8_t data_r[size], data_w[size];
|
||||
uint8_t hkey_c[hkey_size];
|
||||
uint8_t auth_r[hash_size], auth_w[hash_size];
|
||||
uint8_t hash[hash_size];
|
||||
|
||||
crypto_generichash_state state;
|
||||
|
||||
randombytes_buf(data_w, nonce_size);
|
||||
randombytes_buf(secret, sizeof(secret));
|
||||
crypto_scalarmult_base(&data_w[nonce_size], secret);
|
||||
|
||||
crypto_generichash(&data_w[size-hkey_size], hkey_size,
|
||||
data_w, size-hkey_size, ctx->skey, sizeof(ctx->skey));
|
||||
crypto_generichash(&data_w[size-hash_size], hash_size,
|
||||
data_w, size-hash_size, ctx->skey, sizeof(ctx->skey));
|
||||
|
||||
if (!listener && fd_write_all(fd, data_w, size)!=size)
|
||||
return -1;
|
||||
@@ -592,38 +519,53 @@ static int gt_setup_crypto (struct crypto_ctx *ctx, int fd, int listener)
|
||||
if (fd_read_all(fd, data_r, size)!=size)
|
||||
return -1;
|
||||
|
||||
crypto_generichash(hkey_c, hkey_size,
|
||||
data_r, size-hkey_size, ctx->skey, sizeof(ctx->skey));
|
||||
crypto_generichash(hash, hash_size,
|
||||
data_r, size-hash_size, ctx->skey, sizeof(ctx->skey));
|
||||
|
||||
if (sodium_memcmp(&data_r[size-hkey_size], hkey_c, hkey_size))
|
||||
if (sodium_memcmp(&data_r[size-hash_size], hash, hash_size))
|
||||
return -2;
|
||||
|
||||
if (listener && fd_write_all(fd, data_w, size)!=size)
|
||||
return -1;
|
||||
|
||||
crypto_scalarmult(shared, secret, &data_r[nonce_size]);
|
||||
crypto_generichash(auth_w, hash_size,
|
||||
data_r, size, ctx->skey, sizeof(ctx->skey));
|
||||
|
||||
if (fd_write_all(fd, auth_w, hash_size)!=hash_size)
|
||||
return -1;
|
||||
|
||||
if (fd_read_all(fd, auth_r, hash_size)!=hash_size)
|
||||
return -1;
|
||||
|
||||
crypto_generichash(hash, hash_size,
|
||||
data_w, size, ctx->skey, sizeof(ctx->skey));
|
||||
|
||||
if (sodium_memcmp(auth_r, hash, hash_size))
|
||||
return -2;
|
||||
|
||||
if (crypto_scalarmult(shared, secret, &data_r[nonce_size]))
|
||||
return -2;
|
||||
|
||||
crypto_generichash_state state;
|
||||
crypto_generichash_init(&state, ctx->skey, sizeof(ctx->skey), sizeof(key));
|
||||
crypto_generichash_update(&state, shared, sizeof(shared));
|
||||
crypto_generichash_update(&state, data_r, size);
|
||||
crypto_generichash_update(&state, data_w, size);
|
||||
crypto_generichash_final(&state, key, sizeof(key));
|
||||
crypto_aead_aes256gcm_beforenm(&ctx->state_r, key);
|
||||
crypto_aead_aes256gcm_beforenm(&ctx->read.state, key);
|
||||
|
||||
crypto_generichash_init(&state, ctx->skey, sizeof(ctx->skey), sizeof(key));
|
||||
crypto_generichash_update(&state, shared, sizeof(shared));
|
||||
crypto_generichash_update(&state, data_w, size);
|
||||
crypto_generichash_update(&state, data_r, size);
|
||||
crypto_generichash_final(&state, key, sizeof(key));
|
||||
crypto_aead_aes256gcm_beforenm(&ctx->state_w, key);
|
||||
crypto_aead_aes256gcm_beforenm(&ctx->write.state, key);
|
||||
|
||||
sodium_memzero(secret, sizeof(secret));
|
||||
sodium_memzero(shared, sizeof(shared));
|
||||
sodium_memzero(key, sizeof(key));
|
||||
|
||||
byte_cpy(ctx->nonce_r, data_r, nonce_size);
|
||||
byte_cpy(ctx->nonce_w, data_w, nonce_size);
|
||||
byte_cpy(ctx->read.nonce, data_r, nonce_size);
|
||||
byte_cpy(ctx->write.nonce, data_w, nonce_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -640,6 +582,7 @@ int main (int argc, char **argv)
|
||||
char *congestion = NULL;
|
||||
int delay = 0;
|
||||
int multiqueue = 0;
|
||||
int keepalive = 0;
|
||||
int version = 0;
|
||||
int debug = 0;
|
||||
|
||||
@@ -659,6 +602,7 @@ int main (int argc, char **argv)
|
||||
{ "congestion", &congestion, option_str },
|
||||
{ "delay", &delay, option_flag },
|
||||
{ "multiqueue", &multiqueue, option_flag },
|
||||
{ "keepalive", &keepalive, option_flag },
|
||||
{ "debug", &debug, option_flag },
|
||||
{ "version", &version, option_flag },
|
||||
{ NULL },
|
||||
@@ -673,12 +617,12 @@ int main (int argc, char **argv)
|
||||
}
|
||||
|
||||
if (sodium_init()==-1) {
|
||||
fprintf(stderr, "libsodium initialization has failed!\n");
|
||||
gt_log("libsodium initialization has failed!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!crypto_aead_aes256gcm_is_available()) {
|
||||
gt_not_available("AES-256-GCM");
|
||||
gt_na("AES-256-GCM");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -714,12 +658,12 @@ int main (int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (running) {
|
||||
while (1) {
|
||||
sock.fd = listener?sk_accept(fd):sk_create(ai, sk_connect);
|
||||
|
||||
if (sock.fd==-1) {
|
||||
usleep(100000);
|
||||
continue;
|
||||
goto restart;
|
||||
}
|
||||
|
||||
char *sockname = sk_get_name(sock.fd);
|
||||
@@ -727,25 +671,24 @@ int main (int argc, char **argv)
|
||||
if (!sockname)
|
||||
goto restart;
|
||||
|
||||
fprintf(stderr, "%s: connected\n", sockname);
|
||||
gt_log("%s: connected\n", sockname);
|
||||
|
||||
if (!delay)
|
||||
sk_set_nodelay(sock.fd);
|
||||
|
||||
fd_set_nonblock(sock.fd);
|
||||
|
||||
if (keepalive)
|
||||
sk_set_keepalive(sock.fd);
|
||||
|
||||
sk_set_congestion(sock.fd, congestion);
|
||||
|
||||
switch (gt_setup_crypto(&ctx, sock.fd, listener)) {
|
||||
case -2: fprintf(stderr, "%s: key exchange could not be verified!\n", sockname);
|
||||
case -2: gt_log("%s: key exchange could not be verified!\n", sockname);
|
||||
case -1: goto restart;
|
||||
default: break;
|
||||
}
|
||||
|
||||
struct pollfd fds[] = {
|
||||
{ .fd = tun.fd, .events = POLLIN },
|
||||
{ .fd = sock.fd, .events = POLLIN },
|
||||
};
|
||||
|
||||
struct {
|
||||
uint8_t buf[2048];
|
||||
size_t size;
|
||||
@@ -754,9 +697,30 @@ int main (int argc, char **argv)
|
||||
tunr.size = 0;
|
||||
tunw.size = 0;
|
||||
|
||||
while (running) {
|
||||
if (poll(fds, COUNT(fds), -1)==-1 && errno!=EINTR) {
|
||||
perror("poll");
|
||||
fd_set rfds, wfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&wfds);
|
||||
|
||||
int stop_loop = 0;
|
||||
|
||||
while (1) {
|
||||
if (gt_close)
|
||||
stop_loop = 1;
|
||||
|
||||
if (stop_loop) {
|
||||
if (((stop_loop>>1)==3) &&
|
||||
(buffer_read_size(&sock.write.buf)==0) &&
|
||||
(buffer_read_size(&sock.read.buf)==0))
|
||||
goto restart;
|
||||
FD_CLR(tun.fd, &rfds);
|
||||
} else {
|
||||
FD_SET(tun.fd, &rfds);
|
||||
}
|
||||
|
||||
FD_SET(sock.fd, &rfds);
|
||||
|
||||
if (select(sock.fd+1, &rfds, &wfds, NULL, NULL)==-1 && errno!=EINTR) {
|
||||
perror("select");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -773,12 +737,12 @@ int main (int argc, char **argv)
|
||||
|
||||
buffer_shift(&sock.write.buf);
|
||||
|
||||
if (fds[0].revents & POLLIN) {
|
||||
if (FD_ISSET(tun.fd, &rfds)) {
|
||||
while (1) {
|
||||
if (buffer_write_size(&sock.write.buf)<sizeof(tunr.buf)+16)
|
||||
break;
|
||||
|
||||
ssize_t r = fd_read(fds[0].fd, tunr.buf, sizeof(tunr.buf));
|
||||
ssize_t r = tun_read(tun.fd, tunr.buf, sizeof(tunr.buf));
|
||||
|
||||
if (!r)
|
||||
return 2;
|
||||
@@ -786,7 +750,7 @@ int main (int argc, char **argv)
|
||||
if (r<0)
|
||||
break;
|
||||
|
||||
ssize_t ip_size = get_ip_size(tunr.buf, sizeof(tunr.buf));
|
||||
ssize_t ip_size = ip_get_size(tunr.buf, sizeof(tunr.buf));
|
||||
|
||||
if (ip_size<=0)
|
||||
continue;
|
||||
@@ -795,7 +759,7 @@ int main (int argc, char **argv)
|
||||
dump_ip_header(tunr.buf, r);
|
||||
|
||||
if (r<ip_size) {
|
||||
set_ip_size(tunr.buf, r);
|
||||
ip_set_size(tunr.buf, r);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
@@ -805,63 +769,73 @@ int main (int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (fds[1].revents & POLLOUT)
|
||||
fds[1].events = POLLIN;
|
||||
if (FD_ISSET(sock.fd, &wfds))
|
||||
FD_CLR(sock.fd, &wfds);
|
||||
|
||||
if (buffer_read_size(&sock.write.buf)) {
|
||||
sock.write.ret = fd_write(fds[1].fd, sock.write.buf.read, buffer_read_size(&sock.write.buf));
|
||||
ssize_t r = fd_write(sock.fd, sock.write.buf.read, buffer_read_size(&sock.write.buf));
|
||||
|
||||
if (!sock.write.ret)
|
||||
goto restart;
|
||||
if (r==-1)
|
||||
FD_SET(sock.fd, &wfds);
|
||||
|
||||
if (sock.write.ret==-1)
|
||||
fds[1].events = POLLIN|POLLOUT;
|
||||
if (!r) {
|
||||
stop_loop |= (1<<2);
|
||||
buffer_format(&sock.write.buf);
|
||||
}
|
||||
|
||||
if (sock.write.ret>0)
|
||||
sock.write.buf.read += sock.write.ret;
|
||||
if (r>0)
|
||||
sock.write.buf.read += r;
|
||||
} else {
|
||||
if (stop_loop) {
|
||||
stop_loop |= (1<<2);
|
||||
shutdown(sock.fd, SHUT_WR);
|
||||
}
|
||||
}
|
||||
|
||||
buffer_shift(&sock.read.buf);
|
||||
|
||||
if (fds[1].revents & POLLIN) {
|
||||
sock.read.ret = fd_read(fds[1].fd, sock.read.buf.write, buffer_write_size(&sock.read.buf));
|
||||
if (FD_ISSET(sock.fd, &rfds)) {
|
||||
ssize_t r = fd_read(sock.fd, sock.read.buf.write, buffer_write_size(&sock.read.buf));
|
||||
|
||||
if (!sock.read.ret)
|
||||
goto restart;
|
||||
if (!r)
|
||||
stop_loop |= (1<<1);
|
||||
|
||||
if (sock.read.ret>0)
|
||||
sock.read.buf.write += sock.read.ret;
|
||||
if (r>0)
|
||||
sock.read.buf.write += r;
|
||||
}
|
||||
|
||||
if (fds[0].revents & POLLOUT)
|
||||
fds[0].events = POLLIN;
|
||||
if (FD_ISSET(tun.fd, &wfds))
|
||||
FD_CLR(tun.fd, &wfds);
|
||||
|
||||
while (1) {
|
||||
if (!tunw.size) {
|
||||
size_t size = buffer_read_size(&sock.read.buf);
|
||||
ssize_t ip_size = get_ip_size(sock.read.buf.read, size);
|
||||
ssize_t ip_size = ip_get_size(sock.read.buf.read, size);
|
||||
|
||||
if (!ip_size)
|
||||
goto restart;
|
||||
|
||||
if (ip_size<0 || (size_t)ip_size+16>size)
|
||||
if (ip_size<0 || (size_t)ip_size+16>size) {
|
||||
if (stop_loop&(1<<1))
|
||||
buffer_format(&sock.read.buf);
|
||||
break;
|
||||
}
|
||||
|
||||
if (decrypt_packet(&ctx, tunw.buf, ip_size, &sock.read.buf)) {
|
||||
fprintf(stderr, "%s: message could not be verified!\n", sockname);
|
||||
gt_log("%s: message could not be verified!\n", sockname);
|
||||
goto restart;
|
||||
}
|
||||
|
||||
tunw.size = ip_size;
|
||||
}
|
||||
if (tunw.size) {
|
||||
ssize_t r = fd_write(fds[0].fd, tunw.buf, tunw.size);
|
||||
ssize_t r = tun_write(tun.fd, tunw.buf, tunw.size);
|
||||
|
||||
if (!r)
|
||||
return 2;
|
||||
|
||||
if (r==-1)
|
||||
fds[0].events = POLLIN|POLLOUT;
|
||||
FD_SET(tun.fd, &wfds);
|
||||
|
||||
if (r<0)
|
||||
break;
|
||||
@@ -877,8 +851,13 @@ int main (int argc, char **argv)
|
||||
sockname = NULL;
|
||||
}
|
||||
|
||||
close(sock.fd);
|
||||
sock.fd = -1;
|
||||
if (sock.fd!=-1) {
|
||||
close(sock.fd);
|
||||
sock.fd = -1;
|
||||
}
|
||||
|
||||
if (gt_close)
|
||||
break;
|
||||
}
|
||||
|
||||
freeaddrinfo(ai);
|
||||
|
||||
205
src/tun.c
Normal file
205
src/tun.c
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "common-static.h"
|
||||
#include "ip-static.h"
|
||||
|
||||
#include "tun.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#ifdef __linux__
|
||||
# include <linux/if.h>
|
||||
# include <linux/if_tun.h>
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
# include <sys/sys_domain.h>
|
||||
# include <sys/kern_control.h>
|
||||
# include <net/if_utun.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__OpenBSD__)
|
||||
# define GT_BSD_TUN 1
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
int tun_create (char *name, int multiqueue)
|
||||
{
|
||||
int fd = open("/dev/net/tun", O_RDWR);
|
||||
|
||||
if (fd<0) {
|
||||
perror("open /dev/net/tun");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr = {
|
||||
.ifr_flags = IFF_TUN|IFF_NO_PI,
|
||||
};
|
||||
|
||||
if (multiqueue) {
|
||||
#ifdef IFF_MULTI_QUEUE
|
||||
ifr.ifr_flags |= IFF_MULTI_QUEUE;
|
||||
#else
|
||||
gt_na("IFF_MULTI_QUEUE");
|
||||
#endif
|
||||
}
|
||||
|
||||
str_cpy(ifr.ifr_name, name, IFNAMSIZ-1);
|
||||
|
||||
int ret = ioctl(fd, TUNSETIFF, &ifr);
|
||||
|
||||
if (ret<0) {
|
||||
perror("ioctl TUNSETIFF");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("tun name: %s\n", ifr.ifr_name);
|
||||
|
||||
return fd;
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
int tun_create (_unused_ char *name, _unused_ int mq)
|
||||
{
|
||||
struct ctl_info ctlInfo;
|
||||
struct sockaddr_ctl sc;
|
||||
int fd;
|
||||
|
||||
for (unsigned dev_id = 0U; dev_id<32U; dev_id++) {
|
||||
byte_set(&ctlInfo, 0, sizeof(ctlInfo));
|
||||
str_cpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME, sizeof(ctlInfo.ctl_name));
|
||||
fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
|
||||
|
||||
if (fd==-1)
|
||||
return -1;
|
||||
|
||||
if (ioctl(fd, CTLIOCGINFO, &ctlInfo)==-1) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
sc.sc_id = ctlInfo.ctl_id;
|
||||
sc.sc_len = sizeof(sc);
|
||||
sc.sc_family = AF_SYSTEM;
|
||||
sc.ss_sysaddr = AF_SYS_CONTROL;
|
||||
sc.sc_unit = dev_id+1;
|
||||
|
||||
if (connect(fd, (struct sockaddr *) &sc, sizeof(sc))==-1) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("tun name: /dev/utun%u\n", dev_id);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
int tun_create (_unused_ char *name, _unused_ int mq)
|
||||
{
|
||||
for (unsigned dev_id = 0U; dev_id<32U; dev_id++) {
|
||||
char dev_path[11U];
|
||||
|
||||
snprintf(dev_path, sizeof(dev_path), "/dev/tun%u", dev_id);
|
||||
|
||||
int fd = open(dev_path, O_RDWR);
|
||||
|
||||
if (fd!=-1) {
|
||||
printf("tun name: /dev/tun%u\n", dev_id);
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t tun_read (int fd, void *data, size_t size)
|
||||
{
|
||||
if (!size)
|
||||
return -2;
|
||||
|
||||
#ifdef GT_BSD_TUN
|
||||
uint32_t family;
|
||||
struct iovec iov[2] = {
|
||||
{ .iov_base = &family, .iov_len = sizeof(family) },
|
||||
{ .iov_base = data, .iov_len = size }
|
||||
};
|
||||
|
||||
ssize_t ret = readv(fd, iov, 2);
|
||||
#else
|
||||
ssize_t ret = read(fd, data, size);
|
||||
#endif
|
||||
|
||||
if (ret==-1) {
|
||||
if (errno==EAGAIN || errno==EINTR)
|
||||
return -1;
|
||||
|
||||
if (errno)
|
||||
perror("readv");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef GT_BSD_TUN
|
||||
if (ret<(ssize_t) sizeof(family))
|
||||
return 0;
|
||||
|
||||
return ret-sizeof(family);
|
||||
#else
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t tun_write (int fd, const void *data, size_t size)
|
||||
{
|
||||
if (!size)
|
||||
return -2;
|
||||
|
||||
#ifdef GT_BSD_TUN
|
||||
uint32_t family;
|
||||
|
||||
switch (ip_get_version(data, size)) {
|
||||
case 4:
|
||||
family = htonl(AF_INET);
|
||||
break;
|
||||
case 6:
|
||||
family = htonl(AF_INET6);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct iovec iov[2] = {
|
||||
{ .iov_base = &family, .iov_len = sizeof(family) },
|
||||
{ .iov_base = (void *) data, .iov_len = size },
|
||||
};
|
||||
|
||||
ssize_t ret = writev(fd, iov, 2);
|
||||
#else
|
||||
ssize_t ret = write(fd, data, size);
|
||||
#endif
|
||||
|
||||
if (ret==-1) {
|
||||
if (errno==EAGAIN || errno==EINTR)
|
||||
return -1;
|
||||
|
||||
if (errno)
|
||||
perror("write");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef GT_BSD_TUN
|
||||
if (ret<(ssize_t) sizeof(family))
|
||||
return 0;
|
||||
|
||||
return ret-sizeof(family);
|
||||
#else
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user