diff --git a/.gitmodules b/.gitmodules index 85c211c..9879380 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "mud"] path = mud url = https://github.com/angt/mud.git +[submodule "argz"] + path = argz + url = https://github.com/angt/argz.git diff --git a/Makefile.am b/Makefile.am index ec13872..7d75598 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,45 +1,37 @@ ACLOCAL_AMFLAGS = -I m4 --install -bin_PROGRAMS = glorytun glorytunctl +bin_PROGRAMS = glorytun glorytun_CFLAGS = $(libsodium_CFLAGS) glorytun_LDADD = $(libsodium_LIBS) glorytun_SOURCES = \ - src/common.h \ + argz/argz.c \ + argz/argz.h \ + mud/mud.c + mud/mud.h \ + src/bench.c \ + src/bind.c \ src/common.c \ - src/ip.h \ - src/str.h \ + src/common.h \ src/ctl.c \ src/ctl.h \ - src/main.c \ - src/option.c \ - src/option.h \ - src/tun.c \ - src/tun.h \ src/iface.c \ src/iface.h \ - mud/mud.h \ - mud/mud.c - -glorytunctl_CFLAGS = -glorytunctl_LDADD = -glorytunctl_SOURCES = \ - src/common.h \ - src/common.c \ + src/ip.h \ + src/keygen.c \ + src/main.c \ + src/path.c \ src/str.h \ - src/ctl.c \ - src/ctl.h \ - src/mainctl.c \ - src/option.c \ - src/option.h + src/tun.c \ + src/tun.h \ EXTRA_DIST = \ LICENSE \ README.md \ VERSION \ - systemd \ + autogen.sh \ + meson.build \ mud/LICENSE \ mud/README.md \ - meson.build \ - autogen.sh \ + systemd \ version.sh diff --git a/argz b/argz new file mode 160000 index 0000000..1c756db --- /dev/null +++ b/argz @@ -0,0 +1 @@ +Subproject commit 1c756db3924a25feff596b8121513f3c55688512 diff --git a/meson.build b/meson.build index a4723ed..6a8c7db 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,8 @@ project('glorytun', 'c', default_options : [ 'buildtype=debugoptimized' ] ) +cc = meson.get_compiler('c') + prefix = get_option('prefix') bindir = join_paths(prefix, get_option('bindir')) @@ -16,25 +18,21 @@ add_global_arguments('-DPACKAGE_NAME="'+meson.project_name()+'"', language : 'c' executable('glorytun', install: true, sources: [ - 'src/common.c', - 'src/iface.c', - 'src/option.c', - 'src/tun.c', - 'src/ctl.c', + 'argz/argz.c', 'mud/mud.c', - 'src/main.c' + 'src/bench.c', + 'src/bind.c', + 'src/common.c', + 'src/ctl.c', + 'src/iface.c', + 'src/keygen.c', + 'src/main.c', + 'src/path.c', + 'src/tun.c', ], dependencies: [ - dependency('libsodium', version : '>=1.0.4') - ] -) - -executable('glorytunctl', install: true, - sources: [ - 'src/common.c', - 'src/option.c', - 'src/ctl.c', - 'src/mainctl.c' + dependency('libsodium', version : '>=1.0.4'), + cc.find_library('m', required : false) ] ) diff --git a/mud b/mud index d4546cc..582eb29 160000 --- a/mud +++ b/mud @@ -1 +1 @@ -Subproject commit d4546ccae0d4960641b261efe1d0e7a8b2b8a1fe +Subproject commit 582eb2961784e30ea1d067c2ab4e792f9a4c3d0e diff --git a/src/bench.c b/src/bench.c new file mode 100644 index 0000000..63b7c77 --- /dev/null +++ b/src/bench.c @@ -0,0 +1,155 @@ +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "../argz/argz.h" + +#define STR_S(X) (((X) > 1) ? "s" : "") + +static unsigned long long +gt_now(void) +{ +#if defined __APPLE__ + static mach_timebase_info_data_t mtid; + if (!mtid.denom) + mach_timebase_info(&mtid); + return (mach_absolute_time() * mtid.numer / mtid.denom) / 1000ULL; +#elif defined CLOCK_MONOTONIC + struct timespec tv; + clock_gettime(CLOCK_MONOTONIC, &tv); + return tv.tv_sec * 1000000ULL + tv.tv_nsec / 1000ULL; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000000ULL + tv.tv_usec; +#endif +} + +int +gt_bench(int argc, char **argv) +{ + unsigned long precision = 10; + size_t bufsize = 64 * 1024; + unsigned long duration = 1000; + + struct argz bench_argz[] = { + {"aes|chacha", NULL, NULL, argz_option}, + {"precision", "EXPONENT", &precision, argz_ulong}, + {"bufsize", "BYTES", &bufsize, argz_bytes}, + {"duration", "SECONDS", &duration, argz_time}, + {}}; + + if (argz(bench_argz, argc, argv)) + return 1; + + if (duration == 0 || bufsize == 0) + return 0; + + if (sodium_init() == -1) { + gt_log("sodium init failed\n"); + return 1; + } + + duration /= 1000; + + int term = isatty(1); + int chacha = argz_is_set(bench_argz, "chacha"); + + if (!chacha && !crypto_aead_aes256gcm_is_available()) { + gt_log("aes is not available on your platform\n"); + return 1; + } + + unsigned char *buf = calloc(1, bufsize + crypto_aead_aes256gcm_ABYTES); + + if (!buf) { + perror("calloc"); + return 1; + } + + unsigned char npub[crypto_aead_aes256gcm_NPUBBYTES]; + unsigned char key[crypto_aead_aes256gcm_KEYBYTES]; + + randombytes_buf(npub, sizeof(npub)); + randombytes_buf(key, sizeof(key)); + + if (term) { + printf("\n"); + printf(" %-10s %s\n", "bench", chacha ? "chacha20poly1305" : "aes256gcm"); + printf(" %-10s %s\n", "libsodium", sodium_version_string()); + printf("\n"); + printf(" %-10s 2^(-%lu)\n", "precision", precision); + printf(" %-10s %zu byte%s\n", "bufsize", bufsize, STR_S(bufsize)); + printf(" %-10s %lu second%s\n", "duration", duration, STR_S(duration)); + printf("\n"); + printf("------------------------------------------------------------\n"); + printf(" %3s %9s %14s %14s %14s\n", "2^n", "min", "avg", "max", "delta"); + printf("------------------------------------------------------------\n"); + } + + for (int i = 0; !gt_quit && bufsize >> i; i++) { + unsigned long long total_dt = 0ULL; + size_t total_bytes = 0; + double mbps = 0.0; + double mbps_min = INFINITY; + double mbps_max = 0.0; + double mbps_dlt = INFINITY; + + while (!gt_quit && mbps_dlt > ldexp(mbps, -precision)) { + crypto_aead_aes256gcm_state ctx; + + if (!chacha) + crypto_aead_aes256gcm_beforenm(&ctx, key); + + unsigned long long now = gt_now(); + double mbps_old = mbps; + size_t bytes = 0; + + gt_alarm = 0; + alarm(duration); + + while (!gt_quit && !gt_alarm) { + if (chacha) { + crypto_aead_chacha20poly1305_encrypt( + buf, NULL, buf, 1ULL << i, NULL, 0, NULL, npub, key); + } else { + crypto_aead_aes256gcm_encrypt_afternm( + buf, NULL, buf, 1ULL << i, NULL, 0, NULL, npub, + (const crypto_aead_aes256gcm_state *)&ctx); + } + bytes += 1ULL << i; + } + + total_dt += gt_now() - now; + total_bytes += bytes; + + mbps = (total_bytes * 8.0) / total_dt; + mbps_min = fmin(mbps_min, mbps); + mbps_max = fmax(mbps_max, mbps); + mbps_dlt = fabs(mbps_old - mbps); + + if (term) { + printf("\r %3i %9.2f Mbps %9.2f Mbps %9.2f Mbps %9.2e", + i, mbps_min, mbps, mbps_max, mbps_dlt); + fflush(stdout); + } + } + + if (term) { + printf("\n"); + } else { + printf("%i %.2f %.2f %.2f\n", i, mbps_min, mbps, mbps_max); + } + } + + printf("\n"); + free(buf); + + return 0; +} diff --git a/src/bind.c b/src/bind.c new file mode 100644 index 0000000..62e6242 --- /dev/null +++ b/src/bind.c @@ -0,0 +1,421 @@ +#include "common.h" +#include "ctl.h" +#include "iface.h" +#include "ip.h" +#include "str.h" +#include "tun.h" + +#include +#include +#include + +#include "../argz/argz.h" +#include "../mud/mud.h" + +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + +#define GT_MTU(X) ((X)-28) + +static void +fd_set_nonblock(int fd) +{ + int ret; + + if (fd == -1) + return; + + do { + ret = fcntl(fd, F_GETFL, 0); + } while (ret == -1 && errno == EINTR); + + int flags = (ret == -1) ? 0 : ret; + + do { + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + } while (ret == -1 && errno == EINTR); + + if (ret == -1) + perror("fcntl O_NONBLOCK"); +} + +static int +gt_setup_secretkey(struct mud *mud, const char *keyfile) +{ + int fd; + + do { + fd = open(keyfile, O_RDONLY | O_CLOEXEC); + } while (fd == -1 && errno == EINTR); + + if (fd == -1) { + perror("open keyfile"); + return -1; + } + + unsigned char key[32]; + char buf[2 * sizeof(key)]; + size_t size = 0; + + while (size < sizeof(buf)) { + ssize_t r = read(fd, &buf[size], sizeof(buf) - size); + + if (r <= (ssize_t)0) { + if (r && (errno == EAGAIN || errno == EINTR)) + continue; + break; + } + + size += r; + } + + close(fd); + + if (size != sizeof(buf)) { + gt_log("unable to read secret key\n"); + return -1; + } + + 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; +} + +static void +gt_setup_mtu(struct mud *mud, const char *tun_name, size_t *old_mtu) +{ + int mtu = mud_get_mtu(mud); + + if ((size_t)mtu == *old_mtu) + return; + + gt_log("setup MTU to %i on interface %s\n", mtu, tun_name); + + if (iface_set_mtu(tun_name, mtu) == -1) + perror("tun_set_mtu"); + + *old_mtu = (size_t)mtu; +} + +int +gt_bind(int argc, char **argv) +{ + unsigned short bind_port = 5000; + unsigned short port = bind_port; + const char *host = NULL; + const char *dev = NULL; + const char *keyfile = NULL; + unsigned long timeout = 5000; + unsigned long timetolerance = 0; + size_t bufsize = 64 * 1024 * 1024; + size_t mtu = 1500; + + struct argz mtuz[] = { + {"auto", NULL, NULL, argz_option}, + {NULL, "BYTES", &mtu, argz_bytes}, + {}}; + + struct argz toz[] = { + {NULL, "IPADDR", &host, argz_str}, + {NULL, "PORT", &port, argz_ushort}, + {}}; + + struct argz bindz[] = { + {"port", "PORT", &bind_port, argz_ushort}, + {"to", NULL, &toz, argz_option}, + {"dev", "NAME", &dev, argz_str}, + {"v4only|v6only", NULL, NULL, argz_option}, + {"mtu", NULL, &mtuz, argz_option}, + {"keyfile", "FILE", &keyfile, argz_str}, + {"chacha", NULL, NULL, argz_option}, + {"timeout", "SECONDS", &timeout, argz_time}, + {"timetolerance", "SECONDS", &timetolerance, argz_time}, + {"persist", NULL, NULL, argz_option}, + {"bufsize", "BYTES", &bufsize, argz_bytes}, + {}}; + + if (argz(bindz, argc, argv)) + return 1; + + unsigned char *buf = malloc(bufsize); + + if (!buf) { + perror("malloc"); + return 1; + } + + int ipv4 = 1; + int ipv6 = !!(__linux__ + 0); + + if (argz_is_set(bindz, "v4only")) { + ipv4 = 1; + ipv6 = 0; + } + + if (argz_is_set(bindz, "v6only")) { + ipv4 = 0; + ipv6 = 1; + } + + int mtu_auto = argz_is_set(mtuz, "auto"); + int chacha = argz_is_set(bindz, "chacha"); + int persist = argz_is_set(bindz, "persist"); + + int icmp_fd = -1; + + if (ipv4 && mtu_auto && host) { + icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + + if (icmp_fd == -1) + gt_log("couldn't create ICMP socket\n"); + } + + struct mud *mud = mud_create(bind_port, ipv4, ipv6); + + if (!mud) { + gt_log("couldn't create mud\n"); + return 1; + } + + if (str_empty(keyfile)) { + if (mud_set_key(mud, NULL, 0)) { + gt_log("couldn't generate a new key\n"); + return 1; + } + } else { + if (gt_setup_secretkey(mud, keyfile)) + return 1; + } + + if (!chacha && mud_set_aes(mud)) + gt_log("AES is not available\n"); + + if (timeout && mud_set_send_timeout(mud, timeout)) { + perror("timeout"); + return 1; + } + + if (timetolerance && mud_set_time_tolerance(mud, timetolerance)) { + perror("timetolerance"); + return 1; + } + + mud_set_mtu(mud, GT_MTU(mtu)); + + char tun_name[64]; + int tun_fd = tun_create(tun_name, sizeof(tun_name) - 1, dev); + + if (tun_fd == -1) { + gt_log("couldn't create tun device\n"); + return 1; + } + + if (tun_set_persist(tun_fd, persist) == -1) + perror("tun_set_persist"); + + if (host && port) { + if (mud_peer(mud, host, port)) { + perror("mud_peer"); + return 1; + } + } + + gt_setup_mtu(mud, tun_name, &mtu); + + int ctl_fd = ctl_init("/run/" PACKAGE_NAME, tun_name); + + if (ctl_fd == -1) { + perror("ctl_init"); + return 1; + } + + int mud_fd = mud_get_fd(mud); + + fd_set_nonblock(tun_fd); + fd_set_nonblock(mud_fd); + fd_set_nonblock(icmp_fd); + fd_set_nonblock(ctl_fd); + + gt_log("running...\n"); + + fd_set rfds; + FD_ZERO(&rfds); + + int last_fd = 1 + MAX(tun_fd, MAX(mud_fd, MAX(ctl_fd, icmp_fd))); + + while (!gt_quit) { + FD_SET(tun_fd, &rfds); + FD_SET(mud_fd, &rfds); + FD_SET(ctl_fd, &rfds); + + if (icmp_fd != -1) + FD_SET(icmp_fd, &rfds); + + if (select(last_fd, &rfds, NULL, NULL, NULL) == -1) { + if (errno != EBADF) + continue; + perror("select"); + return 1; + } + + if (icmp_fd != -1 && FD_ISSET(icmp_fd, &rfds)) { + struct ip_common ic; + struct sockaddr_storage ss; + socklen_t sl = sizeof(ss); + + ssize_t r = recvfrom(icmp_fd, buf, bufsize, 0, + (struct sockaddr *)&ss, &sl); + + if (!ip_get_common(&ic, buf, r)) { + int mtu = ip_get_mtu(&ic, buf, r); + if (mtu > 0) { + gt_log("received MTU from ICMP: %i\n", mtu); + mud_set_mtu(mud, GT_MTU(mtu)); + } + } + } + + if (FD_ISSET(ctl_fd, &rfds)) { + struct ctl_msg msg, reply = {.type = CTL_REPLY}; + struct sockaddr_storage ss; + socklen_t sl = sizeof(ss); + + ssize_t r = recvfrom(ctl_fd, &msg, sizeof(msg), 0, + (struct sockaddr *)&ss, &sl); + + if (r == (ssize_t)sizeof(msg)) { + switch (msg.type) { + case CTL_PATH_ADD: + gt_log("[ctl path add] addr=%s\n", + &msg.path.add.addr[0]); + if (mud_add_path(mud, &msg.path.add.addr[0])) { + reply.reply = errno; + perror("mud_add_path"); + } + break; + case CTL_PATH_DEL: + gt_log("[ctl path del] addr=%s\n", + &msg.path.del.addr[0]); + if (mud_del_path(mud, &msg.path.del.addr[0])) { + reply.reply = errno; + perror("mud_del_path"); + } + break; + case CTL_PING: + break; + default: + reply = (struct ctl_msg){ + .type = CTL_UNKNOWN, + .unknown = { + .type = msg.type, + }, + }; + break; + } + if (sendto(ctl_fd, &reply, sizeof(reply), 0, + (const struct sockaddr *)&ss, sl) == -1) + perror("sendto(ctl)"); + } else if (r == -1 && errno != EAGAIN) { + perror("recvfrom(ctl)"); + } + } + + if (FD_ISSET(tun_fd, &rfds)) { + size_t size = 0; + + while (bufsize - size >= mtu) { + const int r = tun_read(tun_fd, &buf[size], bufsize - size); + + if (r <= 0 || r > mtu) + break; + + struct ip_common ic; + + if (ip_get_common(&ic, &buf[size], r) || ic.size != r) + break; + + size += r; + } + + size_t p = 0; + + while (p < size) { + size_t q = p; + int tc = 0; + + while (q < size) { + struct ip_common ic; + + if ((ip_get_common(&ic, &buf[q], size - q)) || + (ic.size > size - q)) + break; + + if (q + ic.size > p + mtu) + break; + + q += ic.size; + + if (tc < (ic.tc & 0xFC)) + tc = ic.tc & 0xFC; + } + + if (p >= q) + break; + + int r = mud_send(mud, &buf[p], q - p, tc); + + if (r == -1 && errno == EMSGSIZE) { + gt_setup_mtu(mud, tun_name, &mtu); + } else { + if (r == -1 && errno != EAGAIN) + perror("mud_send"); + } + + p = q; + } + } + + if (FD_ISSET(mud_fd, &rfds)) { + size_t size = 0; + + while (bufsize - size >= mtu) { + const int r = mud_recv(mud, &buf[size], bufsize - size); + + if (r <= 0) { + if (r == -1 && errno != EAGAIN) + perror("mud_recv"); + break; + } + + size += r; + } + + size_t p = 0; + + while (p < size) { + struct ip_common ic; + + if ((ip_get_common(&ic, &buf[p], size - p)) || + (ic.size > size - p)) + break; + + tun_write(tun_fd, &buf[p], ic.size); + + p += ic.size; + } + } + } + + if (gt_reload && tun_fd >= 0) { + if (tun_set_persist(tun_fd, 1) == -1) + perror("tun_set_persist"); + } + + return 0; +} diff --git a/src/common.c b/src/common.c index 47c208f..45b1bdd 100644 --- a/src/common.c +++ b/src/common.c @@ -3,20 +3,6 @@ #include #include -int -gt_print(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - int ret = vfprintf(stdout, fmt, ap); - va_end(ap); - - if (ret < 0) - return 0; - - return ret; -} - void gt_log(const char *fmt, ...) { diff --git a/src/common.h b/src/common.h index fa14795..a242e2b 100644 --- a/src/common.h +++ b/src/common.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #ifndef PACKAGE_NAME @@ -43,6 +44,10 @@ #undef MIN #define MIN(x,y) ({ __typeof__(x) X=(x); __typeof__(y) Y=(y); X < Y ? X : Y; }) +extern volatile sig_atomic_t gt_alarm; +extern volatile sig_atomic_t gt_reload; +extern volatile sig_atomic_t gt_quit; + int gt_print (const char *, ...) _printf_(1,2); void gt_log (const char *, ...) _printf_(1,2); diff --git a/src/ctl.c b/src/ctl.c index 44a2388..e5bcf36 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -1,5 +1,4 @@ #include "common.h" - #include "ctl.h" #include "str.h" diff --git a/src/iface.c b/src/iface.c index d94e2db..e8bdd5d 100644 --- a/src/iface.c +++ b/src/iface.c @@ -1,5 +1,4 @@ #include "common.h" - #include "iface.h" #include "str.h" @@ -7,7 +6,7 @@ #include int -iface_set_mtu(char *dev_name, int mtu) +iface_set_mtu(const char *dev_name, int mtu) { struct ifreq ifr = { .ifr_mtu = mtu, diff --git a/src/iface.h b/src/iface.h index 01baf8d..5c8c354 100644 --- a/src/iface.h +++ b/src/iface.h @@ -1,3 +1,3 @@ #pragma once -int iface_set_mtu (char *, int); +int iface_set_mtu (const char *, int); diff --git a/src/main.c b/src/main.c index 73512a8..7a432a7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,89 +1,24 @@ #include "common.h" - -#include "ctl.h" -#include "iface.h" -#include "ip.h" -#include "option.h" #include "str.h" -#include "tun.h" -#include -#include -#include -#include #include -#include "../mud/mud.h" - -#ifndef O_CLOEXEC -#define O_CLOEXEC 0 -#endif - -#define GT_MTU(X) ((X)-28) - -static struct { - volatile sig_atomic_t quit; - volatile sig_atomic_t reload; - char *dev; - char *keyfile; - char *host; - long port; - long bind_port; - long mtu; - long timeout; - long time_tolerance; - int ipv4; - int ipv6; - int mtu_auto; - int chacha20; - int version; - int keygen; - int persist; - struct { - unsigned char *data; - long size; - } buf; -} gt = { - .port = 5000, - .bind_port = 5000, - .mtu = 1500, - .timeout = 5000, - .ipv4 = 1, -#ifdef __linux__ - .ipv6 = 1, -#endif - .buf = { - .size = 64 * 1024, - }, -}; - -static void -fd_set_nonblock(int fd) -{ - int ret; - - if (fd == -1) - return; - - do { - ret = fcntl(fd, F_GETFL, 0); - } while (ret == -1 && errno == EINTR); - - int flags = (ret == -1) ? 0 : ret; - - do { - ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); - } while (ret == -1 && errno == EINTR); - - if (ret == -1) - perror("fcntl O_NONBLOCK"); -} +volatile sig_atomic_t gt_alarm; +volatile sig_atomic_t gt_reload; +volatile sig_atomic_t gt_quit; static void gt_quit_handler(int sig) { - gt.reload = (sig == SIGHUP); - gt.quit = 1; + switch (sig) { + case SIGALRM: + gt_alarm = 1; + return; + case SIGHUP: + gt_reload = 1; /* FALLTHRU */ + default: + gt_quit = 1; + } } static void @@ -100,6 +35,7 @@ gt_set_signal(void) sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGHUP, &sa, NULL); + sigaction(SIGALRM, &sa, NULL); sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); @@ -107,423 +43,73 @@ gt_set_signal(void) sigaction(SIGUSR2, &sa, NULL); } -static void -gt_print_secretkey(struct mud *mud) -{ - unsigned char key[32]; - size_t size = sizeof(key); - - if (mud_get_key(mud, key, &size)) - return; - - char buf[2 * sizeof(key) + 1]; - - gt_tohex(buf, sizeof(buf), key, size); - gt_print("%s\n", buf); -} - static int -gt_setup_secretkey(struct mud *mud, char *keyfile) +gt_version(int argc, char **argv) { - int fd; - - do { - fd = open(keyfile, O_RDONLY | O_CLOEXEC); - } while (fd == -1 && errno == EINTR); - - if (fd == -1) { - perror("open keyfile"); - return -1; - } - - unsigned char key[32]; - char buf[2 * sizeof(key)]; - size_t size = 0; - - while (size < sizeof(buf)) { - ssize_t r = read(fd, &buf[size], sizeof(buf) - size); - - if (r <= (ssize_t)0) { - if (r && (errno == EAGAIN || errno == EINTR)) - continue; - break; - } - - size += r; - } - - close(fd); - - if (size != sizeof(buf)) { - gt_log("unable to read secret key\n"); - return -1; - } - - 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)); - + printf(PACKAGE_VERSION "\n"); return 0; } -static int -gt_setup_option(int argc, char **argv) +int +gt_show(int argc, char **argv) { - // clang-format off - - struct option opts[] = { - { "host", >.host, option_str }, - { "port", >.port, option_long }, - { "bind-port", >.bind_port, option_long }, - { "dev", >.dev, option_str }, - { "persist", NULL, option_option }, - { "mtu", >.mtu, option_long }, - { "mtu-auto", NULL, option_option }, - { "keyfile", >.keyfile, option_str }, - { "keygen", NULL, option_option }, - { "timeout", >.timeout, option_long }, - { "time-tolerance", >.time_tolerance, option_long }, - { "v4only", NULL, option_option }, - { "v6only", NULL, option_option }, - { "chacha20", NULL, option_option }, - { "buf-size", >.buf.size, option_long }, - { "version", NULL, option_option }, - { NULL }, - }; - - // clang-format on - - if (option(opts, argc, argv)) - return 1; - - int v4only = option_is_set(opts, "v4only"); - int v6only = option_is_set(opts, "v6only"); - - if (v4only && v6only) { - gt_log("v4only and v6only cannot be both set\n"); - return 1; - } - - if ((int)gt.timeout <= 0) { - gt_log("bad timeout\n"); - return 1; - } - - if (gt.buf.size <= 0) { - gt_log("bad buf-size\n"); - return 1; - } - - if (v4only) { - gt.ipv4 = 1; - gt.ipv6 = 0; - } - - if (v6only) { - gt.ipv4 = 0; - gt.ipv6 = 1; - } - - gt.mtu_auto = option_is_set(opts, "mtu-auto"); - gt.chacha20 = option_is_set(opts, "chacha20"); - gt.version = option_is_set(opts, "version"); - gt.keygen = option_is_set(opts, "keygen"); - gt.persist = option_is_set(opts, "persist"); - - gt.buf.data = malloc(gt.buf.size); - + printf("show (todo)\n"); return 0; } -static void -gt_setup_mtu(struct mud *mud, char *tun_name) +int +gt_key(int argc, char **argv) { - int mtu = mud_get_mtu(mud); - - if (mtu == (int)gt.mtu) - return; - - gt.mtu = mtu; - - gt_log("setup MTU to %i on interface %s\n", mtu, tun_name); - - if (iface_set_mtu(tun_name, mtu) == -1) - perror("tun_set_mtu"); + printf("key (todo)\n"); + return 0; } +int +gt_bind(int, char **); +int +gt_path(int, char **); +int +gt_bench(int, char **); +int +gt_keygen(int, char **); + int main(int argc, char **argv) { gt_set_signal(); - if (gt_setup_option(argc, argv)) - return 1; + struct { + char *name; + char *help; + int (*call)(int, char **); + } cmd[] = { + {"show", "show all running tunnels", gt_show}, + {"bind", "start a new tunnel", gt_bind}, + {"path", "manage paths", gt_path}, + {"keygen", "generate a new secret key", gt_keygen}, + {"bench", "start a crypto bench", gt_bench}, + {"version", "show version", gt_version}, + {}}; - if (gt.version) { - gt_print(PACKAGE_VERSION "\n"); - return 0; - } - - int icmp_fd = -1; - - if (gt.ipv4 && gt.mtu_auto && gt.host) { - icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - - if (icmp_fd == -1) - gt_log("couldn't create ICMP socket\n"); - } - - struct mud *mud = mud_create(gt.bind_port, gt.ipv4, gt.ipv6); - - if (!mud) { - gt_log("couldn't create mud\n"); - return 1; - } - - if (gt.keygen || str_empty(gt.keyfile)) { - if (mud_new_key(mud)) { - gt_log("couldn't generate a new key\n"); - return 1; + if (argc > 1) { + for (int k = 0; cmd[k].name; k++) { + if (!str_cmp(cmd[k].name, argv[1])) + return cmd[k].call(argc - 1, argv + 1); } + printf("unknown command %s\n", argv[1]); } - if (gt.keygen) { - gt_print_secretkey(mud); - return 0; - } + printf("\navailable commands:\n"); - if (!gt.chacha20 && mud_set_aes(mud)) - gt_log("AES is not available\n"); + int len = 0; - if (gt.timeout > 0) - mud_set_send_timeout_msec(mud, gt.timeout); + for (int k = 0; cmd[k].name; k++) + len = MAX(len, (int)str_len(cmd[k].name, 32)); - if (gt.time_tolerance > 0) - mud_set_time_tolerance_sec(mud, gt.time_tolerance); + for (int k = 0; cmd[k].name; k++) + printf(" %-*s %s\n", len, cmd[k].name, cmd[k].help); - mud_set_mtu(mud, GT_MTU(gt.mtu)); + printf("\n"); - char tun_name[64]; - - int tun_fd = tun_create(tun_name, sizeof(tun_name) - 1, gt.dev); - - if (tun_fd == -1) { - gt_log("couldn't create tun device\n"); - return 1; - } - - if (tun_set_persist(tun_fd, gt.persist) == -1) - perror("tun_set_persist"); - - if (str_empty(gt.keyfile)) { - gt_print("here is your new secret key:\n"); - gt_print_secretkey(mud); - } else { - if (gt_setup_secretkey(mud, gt.keyfile)) - return 1; - } - - if (gt.host && gt.port) { - if (mud_peer(mud, gt.host, gt.port)) { - perror("mud_peer"); - return 1; - } - } - - gt_setup_mtu(mud, tun_name); - - int ctl_fd = ctl_init("/run/" PACKAGE_NAME, tun_name); - - if (ctl_fd == -1) { - perror("ctl_init"); - return 1; - } - - int mud_fd = mud_get_fd(mud); - - fd_set_nonblock(tun_fd); - fd_set_nonblock(mud_fd); - fd_set_nonblock(icmp_fd); - fd_set_nonblock(ctl_fd); - - gt_log("running...\n"); - - fd_set rfds; - FD_ZERO(&rfds); - - int last_fd = 1 + MAX(tun_fd, MAX(mud_fd, MAX(ctl_fd, icmp_fd))); - - while (!gt.quit) { - FD_SET(tun_fd, &rfds); - FD_SET(mud_fd, &rfds); - FD_SET(ctl_fd, &rfds); - - if (icmp_fd != -1) - FD_SET(icmp_fd, &rfds); - - if (select(last_fd, &rfds, NULL, NULL, NULL) == -1) { - if (errno != EBADF) - continue; - perror("select"); - return 1; - } - - if (icmp_fd != -1 && FD_ISSET(icmp_fd, &rfds)) { - struct ip_common ic; - struct sockaddr_storage ss; - socklen_t sl = sizeof(ss); - - ssize_t r = recvfrom(icmp_fd, gt.buf.data, gt.buf.size, 0, - (struct sockaddr *)&ss, &sl); - - if (!ip_get_common(&ic, gt.buf.data, r)) { - int mtu = ip_get_mtu(&ic, gt.buf.data, r); - if (mtu > 0) { - gt_log("received MTU from ICMP: %i\n", mtu); - mud_set_mtu(mud, GT_MTU(mtu)); - } - } - } - - if (FD_ISSET(ctl_fd, &rfds)) { - struct ctl_msg msg, reply = {.type = CTL_REPLY}; - - struct sockaddr_storage ss; - socklen_t sl = sizeof(ss); - - ssize_t r = recvfrom(ctl_fd, &msg, sizeof(msg), 0, - (struct sockaddr *)&ss, &sl); - - if (r == (ssize_t)sizeof(msg)) { - switch (msg.type) { - case CTL_PATH_ADD: - gt_log("[ctl path add] addr=%s\n", - &msg.path.add.addr[0]); - if (mud_add_path(mud, &msg.path.add.addr[0])) { - reply.reply = errno; - perror("mud_add_path"); - } - break; - case CTL_PATH_DEL: - gt_log("[ctl path del] addr=%s\n", - &msg.path.del.addr[0]); - if (mud_del_path(mud, &msg.path.del.addr[0])) { - reply.reply = errno; - perror("mud_del_path"); - } - break; - case CTL_PING: - break; - default: - reply = (struct ctl_msg){ - .type = CTL_UNKNOWN, - .unknown = { - .type = msg.type, - }, - }; - break; - } - if (sendto(ctl_fd, &reply, sizeof(reply), 0, - (const struct sockaddr *)&ss, sl) == -1) - perror("sendto(ctl)"); - } else if (r == -1 && errno != EAGAIN) { - perror("recvfrom(ctl)"); - } - } - - if (FD_ISSET(tun_fd, &rfds)) { - size_t size = 0; - - while (gt.buf.size - size >= gt.mtu) { - const int r = tun_read(tun_fd, >.buf.data[size], gt.buf.size - size); - - if (r <= 0 || r > gt.mtu) - break; - - struct ip_common ic; - - if (ip_get_common(&ic, >.buf.data[size], r) || ic.size != r) - break; - - size += r; - } - - int p = 0; - - while (p < size) { - int tc = 0; - int q = p; - - while (q < size) { - struct ip_common ic; - - if ((ip_get_common(&ic, >.buf.data[q], size - q)) || - (ic.size > size - q)) - break; - - if (q + ic.size > p + gt.mtu) - break; - - q += ic.size; - - if (tc < (ic.tc & 0xFC)) - tc = ic.tc & 0xFC; - } - - if (p >= q) - break; - - int r = mud_send(mud, >.buf.data[p], q - p, tc); - - if (r == -1 && errno == EMSGSIZE) { - gt_setup_mtu(mud, tun_name); - } else { - if (r == -1 && errno != EAGAIN) - perror("mud_send"); - } - - p = q; - } - } - - if (FD_ISSET(mud_fd, &rfds)) { - size_t size = 0; - - while (gt.buf.size - size >= gt.mtu) { - const int r = mud_recv(mud, >.buf.data[size], gt.buf.size - size); - - if (r <= 0) { - if (r == -1 && errno != EAGAIN) - perror("mud_recv"); - break; - } - - size += r; - } - - int p = 0; - - while (p < size) { - struct ip_common ic; - - if ((ip_get_common(&ic, >.buf.data[p], size - p)) || - (ic.size > size - p)) - break; - - tun_write(tun_fd, >.buf.data[p], ic.size); - - p += ic.size; - } - } - } - - if (gt.reload && tun_fd >= 0) { - if (tun_set_persist(tun_fd, 1) == -1) - perror("tun_set_persist"); - } - - return 0; + return argc != 1; } diff --git a/src/mainctl.c b/src/mainctl.c deleted file mode 100644 index 3c4e87b..0000000 --- a/src/mainctl.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "common.h" - -#include "ctl.h" -#include "option.h" -#include "str.h" - -#include -#include - -#ifndef PACKAGE_VERSION -#define PACKAGE_VERSION "unknown" -#endif - -static struct { - char *dev; - int version; - struct { - struct { - int set; - const char *addr; - } add, del; - } path; -} gt = { - .dev = "tun0", -}; - -static int -gt_setup_option(int argc, char **argv) -{ - // clang-format off - - struct option path_opts[] = { - { "add", >.path.add.addr, option_str }, - { "del", >.path.del.addr, option_str }, - { NULL }, - }; - - struct option opts[] = { - { "dev", >.dev, option_str }, - { "path", &path_opts, option_option }, - { "version", NULL, option_option }, - { NULL }, - - }; - - // clang-format on - - if (option(opts, argc, argv)) - return 1; - - gt.path.add.set = option_is_set(path_opts, "add"); - gt.path.del.set = option_is_set(path_opts, "del"); - gt.version = option_is_set(opts, "version"); - - return 0; -} - -int -main(int argc, char **argv) -{ - if (gt_setup_option(argc, argv)) - return 1; - - if (gt.version) { - gt_print(PACKAGE_VERSION "\n"); - return 0; - } - - int ctl_fd = ctl_init("/run/" PACKAGE_NAME, "client"); - - if (ctl_fd == -1) { - perror("ctl_init"); - return 1; - } - - if (ctl_connect(ctl_fd, "/run/" PACKAGE_NAME, gt.dev) == -1) { - gt_log("couldn't connect to %s\n", gt.dev); - return 1; - } - - struct ctl_msg msg; - - if (gt.path.add.set) { - msg = (struct ctl_msg){ - .type = CTL_PATH_ADD, - }; - str_cpy(msg.path.add.addr, sizeof(msg.path.add.addr) - 1, gt.path.add.addr); - } else if (gt.path.del.set) { - msg = (struct ctl_msg){ - .type = CTL_PATH_DEL, - }; - str_cpy(msg.path.del.addr, sizeof(msg.path.del.addr) - 1, gt.path.del.addr); - } - - if (send(ctl_fd, &msg, sizeof(msg), 0) == -1) { - perror("send"); - return 1; - } - - struct ctl_msg reply; - - if (recv(ctl_fd, &reply, sizeof(reply), 0) == -1) { - perror("recv"); - return 1; - } - - switch (reply.type) { - case CTL_REPLY: - if (reply.reply) { - errno = reply.reply; - perror("error"); - } - break; - case CTL_UNKNOWN: - gt_print("unknown command: %i\n", reply.unknown.type); - break; - default: - gt_log("bad reply from server: %i\n", reply.type); - } - - close(ctl_fd); - - return 0; -} diff --git a/src/option.c b/src/option.c deleted file mode 100644 index c973a80..0000000 --- a/src/option.c +++ /dev/null @@ -1,156 +0,0 @@ -#include "common.h" - -#include "option.h" -#include "str.h" - -int -option_str(void *data, int argc, char **argv) -{ - if (argc < 2 || str_empty(argv[1])) { - gt_print("option `%s' need a string argument\n", argv[0]); - return -1; - } - - memcpy(data, &argv[1], sizeof(argv[1])); - - return 1; -} - -int -option_long(void *data, int argc, char **argv) -{ - if (argc < 2 || str_empty(argv[1])) { - gt_print("option `%s' need an integer argument\n", argv[0]); - return -1; - } - - errno = 0; - char *end; - long val = strtol(argv[1], &end, 0); - - if (errno || argv[1] == end) { - gt_print("argument `%s' is not a valid integer\n", argv[1]); - return -1; - } - - memcpy(data, &val, sizeof(val)); - - return 1; -} - -int -option_is_set(struct option *opts, const char *name) -{ - for (int k = 0; opts[k].name; k++) { - if (!str_cmp(opts[k].name, name)) - return opts[k].set; - } - - return 0; -} - -int -option_option(void *data, int argc, char **argv) -{ - if (!data) - return 0; - - struct option *opts = (struct option *)data; - - for (int k = 0; opts[k].name; k++) - opts[k].set = 0; - - for (int i = 1; i < argc; i++) { - if (!str_cmp(argv[i], "--")) - return i; - - int found = 0; - - for (int k = 0; opts[k].name; k++) { - if (str_cmp(opts[k].name, argv[i])) - continue; - - if (opts[k].set) { - gt_print("option `%s' is already set\n", opts[k].name); - return -1; - } - - int ret = opts[k].call(opts[k].data, argc - i, &argv[i]); - - if (ret < 0) - return -1; - - opts[k].set = 1; - - i += ret; - found = 1; - break; - } - - if (!found) - return i - 1; - } - - for (int k = 0; opts[k].name; k++) { - if (opts[k].mandatory && !opts[k].set) { - gt_print("option `%s' is mandatory\n", opts[k].name); - return -1; - } - } - - return argc; -} - -static int -option_usage(struct option *opts, int slen) -{ - if (!opts) - return 0; - - int len = 0; - - for (int k = 0; opts[k].name; k++) { - if (len > 40) { - gt_print("\n%*s", slen, ""); - len = 0; - } - - len += gt_print(" %s%s", opts[k].mandatory ? "" : "[", opts[k].name); - - if (opts[k].call == option_option) { - len += option_usage((struct option *)opts[k].data, slen + len); - } else { - len += gt_print(" ARG"); - } - - len += gt_print(opts[k].mandatory ? "" : "]"); - } - - return len; -} - -int -option(struct option *opts, int argc, char **argv) -{ - int ret = option_option(opts, argc, argv); - - if (ret == argc) - return 0; - - if (ret < 0 || ret + 1 >= argc) - return 1; - - gt_print("option `%s' is unknown\n", argv[ret + 1]); - - int slen = gt_print("usage: %s", argv[0]); - - if (slen > 40) { - slen = 12; - gt_print("\n%*s", slen, ""); - } - - option_usage(opts, slen); - gt_print("\n"); - - return 1; -} diff --git a/src/option.h b/src/option.h deleted file mode 100644 index 797e584..0000000 --- a/src/option.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -struct option { - char *name; - void *data; - int (*call) (void *, int, char **); - int mandatory; - int set; -}; - -int option_option (void *, int, char **); -int option_str (void *, int, char **); -int option_long (void *, int, char **); - -int option_is_set (struct option *, const char *); -int option (struct option *, int, char **); diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..bf8dd73 --- /dev/null +++ b/src/path.c @@ -0,0 +1,88 @@ +#include "common.h" +#include "ctl.h" +#include "str.h" + +#include +#include + +#include "../argz/argz.h" + +int +gt_path(int argc, char **argv) +{ + const char *dev = "tun0"; + const char *addr = NULL; + + struct argz actionz[] = { + {NULL, "IPADDR", &addr, argz_str}, + {}}; + + struct argz pathz[] = { + {"dev", "NAME", &dev, argz_str}, + {"up|down", NULL, &actionz, argz_option}, + {}}; + + if (argz(pathz, argc, argv)) + return 1; + + struct ctl_msg msg; + + if (argz_is_set(pathz, "up")) { + gt_log("up\n"); + msg = (struct ctl_msg){ + .type = CTL_PATH_ADD, + }; + str_cpy(msg.path.add.addr, sizeof(msg.path.add.addr) - 1, addr); + } else if (argz_is_set(pathz, "down")) { + gt_log("down\n"); + msg = (struct ctl_msg){ + .type = CTL_PATH_DEL, + }; + str_cpy(msg.path.del.addr, sizeof(msg.path.del.addr) - 1, addr); + } else { + gt_log("nothing to do..\n"); + return 0; + } + + int ctl_fd = ctl_init("/run/" PACKAGE_NAME, "client"); + + if (ctl_fd == -1) { + perror("ctl_init"); + return 1; + } + + if (ctl_connect(ctl_fd, "/run/" PACKAGE_NAME, dev) == -1) { + gt_log("couldn't connect to %s\n", dev); + return 1; + } + + if (send(ctl_fd, &msg, sizeof(msg), 0) == -1) { + perror("send"); + return 1; + } + + struct ctl_msg reply; + + if (recv(ctl_fd, &reply, sizeof(reply), 0) == -1) { + perror("recv"); + return 1; + } + + switch (reply.type) { + case CTL_REPLY: + if (reply.reply) { + errno = reply.reply; + perror("error"); + } + break; + case CTL_UNKNOWN: + printf("unknown command: %i\n", reply.unknown.type); + break; + default: + gt_log("bad reply from server: %i\n", reply.type); + } + + close(ctl_fd); + + return 0; +} diff --git a/src/tun.c b/src/tun.c index 268c8d5..64153eb 100644 --- a/src/tun.c +++ b/src/tun.c @@ -1,16 +1,13 @@ #include "common.h" - #include "ip.h" #include "str.h" #include "tun.h" #include #include - #include #include #include - #include #ifdef __linux__ @@ -76,7 +73,7 @@ tun_create_by_id(char *name, size_t len, unsigned id) } static int -tun_create_by_name(char *name, size_t len, char *dev_name) +tun_create_by_name(char *name, size_t len, const char *dev_name) { unsigned id = 0; @@ -93,7 +90,7 @@ tun_create_by_name(char *name, size_t len, char *dev_name) #ifdef __linux__ static int -tun_create_by_name(char *name, size_t len, char *dev_name) +tun_create_by_name(char *name, size_t len, const char *dev_name) { struct ifreq ifr = { .ifr_flags = IFF_TUN | IFF_NO_PI, @@ -129,7 +126,7 @@ tun_create_by_name(char *name, size_t len, char *dev_name) #else /* not __linux__ not __APPLE__ */ static int -tun_create_by_name(char *name, size_t len, char *dev_name) +tun_create_by_name(char *name, size_t len, const char *dev_name) { char tmp[128]; @@ -170,7 +167,7 @@ tun_create_by_id(char *name, size_t len, unsigned id) #endif int -tun_create(char *name, size_t len, char *dev_name) +tun_create(char *name, size_t len, const char *dev_name) { int fd = -1; diff --git a/src/tun.h b/src/tun.h index 7340cd2..2b4aa70 100644 --- a/src/tun.h +++ b/src/tun.h @@ -1,6 +1,6 @@ #pragma once -int tun_create (char *, size_t, char *); +int tun_create (char *, size_t, const char *); int tun_read (int, void *, size_t); int tun_write (int, const void *, size_t); int tun_set_persist (int, int); diff --git a/systemd/glorytun-run b/systemd/glorytun-run index 1d9d518..264c514 100755 --- a/systemd/glorytun-run +++ b/systemd/glorytun-run @@ -1,8 +1,6 @@ #!/bin/sh -exec glorytun "$@" \ +exec glorytun bind "$@" \ ${DEV:+dev "$DEV"} \ - ${HOST:+host "$HOST"} \ - ${PORT:+port "$PORT"} \ - ${BIND:+bind "$BIND"} \ - ${BIND_PORT:+bind-port "$BIND_PORT"} + ${HOST:+to "$HOST" "$PORT"} \ + ${BIND_PORT:+port "$BIND_PORT"} diff --git a/systemd/glorytun-setup b/systemd/glorytun-setup index af95fec..ceed5fd 100755 --- a/systemd/glorytun-setup +++ b/systemd/glorytun-setup @@ -39,7 +39,7 @@ DEV=gt${HOST:+c}-$NAME HOST=$HOST PORT=$PORT BIND_PORT=$BIND_PORT -OPTIONS=v4only mtu-auto +OPTIONS=v4only mtu auto EOF ( umask 077; echo "$KEY" > "$DIR/key" ) @@ -53,7 +53,7 @@ TABLE=200 # keep the current route to HOST SRC=$(ip route get "$HOST" | awk '/src/{getline;print $0}' RS=' ') ip rule add from "$SRC" table main pref "$((PREF-1))" || true -glorytunctl path add "$SRC" dev "$DEV" +glorytun path up "$SRC" dev "$DEV" # forward everything else to the tunnel ip rule add from all table "$TABLE" pref "$PREF" || true