Skip to content

Commit

Permalink
Remove old icmp/auto mtu code
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
angt committed Mar 4, 2018
1 parent b4ec962 commit 4fab60e
Showing 6 changed files with 24 additions and 61 deletions.
2 changes: 1 addition & 1 deletion mud
Submodule mud updated 2 files
+94 −76 mud.c
+11 −10 mud.h
74 changes: 21 additions & 53 deletions src/bind.c
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
#include "tun.h"

#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/select.h>

@@ -17,8 +16,6 @@
#define O_CLOEXEC 0
#endif

#define GT_MTU(X) ((X)-28)

static void
fd_set_nonblock(int fd)
{
@@ -91,13 +88,19 @@ gt_setup_secretkey(struct mud *mud, const char *keyfile)
static size_t
gt_setup_mtu(struct mud *mud, const char *tun_name)
{
static size_t oldmtu = 0;
size_t mtu = mud_get_mtu(mud);

if (mtu == oldmtu)
return mtu;

gt_log("setup MTU to %zu on interface %s\n", mtu, tun_name);

if (iface_set_mtu(tun_name, mtu) == -1)
perror("tun_set_mtu");

oldmtu = mtu;

return mtu;
}

@@ -111,12 +114,7 @@ gt_bind(int argc, char **argv)
const char *dev = NULL;
const char *keyfile = NULL;
size_t bufsize = 64 * 1024 * 1024;
size_t mtu = 1500;

struct argz mtuz[] = {
{"auto", NULL, NULL, argz_option},
{NULL, "BYTES", &mtu, argz_bytes},
{NULL}};
size_t mtu = 1330;

struct argz toz[] = {
{NULL, "IPADDR", &peer_addr, argz_addr},
@@ -128,7 +126,7 @@ gt_bind(int argc, char **argv)
{NULL, "PORT", &bind_port, argz_ushort},
{"to", NULL, &toz, argz_option},
{"dev", "NAME", &dev, argz_str},
{"mtu", NULL, &mtuz, argz_option},
{"mtu", "BYTES", &mtu, argz_option},
{"keyfile", "FILE", &keyfile, argz_str},
{"chacha", NULL, NULL, argz_option},
{"persist", NULL, NULL, argz_option},
@@ -148,19 +146,9 @@ gt_bind(int argc, char **argv)
return 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 (mtu_auto && (peer_addr.ss_family == AF_INET)) {
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((struct sockaddr *)&bind_addr);

if (!mud) {
@@ -183,8 +171,6 @@ gt_bind(int argc, char **argv)
chacha = 1;
}

mud_set_mtu(mud, GT_MTU(mtu));

char tun_name[64];
int tun_fd = tun_create(tun_name, sizeof(tun_name) - 1, dev);

@@ -193,6 +179,9 @@ gt_bind(int argc, char **argv)
return 1;
}

mud_set_mtu(mud, mtu);
mtu = gt_setup_mtu(mud, tun_name);

if (tun_set_persist(tun_fd, persist) == -1)
perror("tun_set_persist");

@@ -203,8 +192,6 @@ gt_bind(int argc, char **argv)
}
}

mtu = gt_setup_mtu(mud, tun_name);

int ctl_fd = ctl_create("/run/" PACKAGE_NAME, tun_name);

if (ctl_fd == -1) {
@@ -216,47 +203,28 @@ gt_bind(int argc, char **argv)

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)));
int last_fd = 1 + MAX(tun_fd, MAX(mud_fd, ctl_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)) {
size_t mtu = ip_get_mtu(&ic, buf, r);
if (mtu > 0) {
gt_log("received MTU from ICMP: %zu\n", mtu);
mud_set_mtu(mud, GT_MTU(mtu));
}
}
}
mtu = gt_setup_mtu(mud, tun_name);

if (FD_ISSET(ctl_fd, &rfds)) {
struct ctl_msg req, res = {.reply = 1};
@@ -299,9 +267,9 @@ gt_bind(int argc, char **argv)
}
break;
case CTL_MTU:
mud_set_mtu(mud, GT_MTU((size_t)req.mtu));
res.mtu = gt_setup_mtu(mud, tun_name);
mtu = res.mtu;
mud_set_mtu(mud, (size_t)req.mtu);
mtu = gt_setup_mtu(mud, tun_name);
res.mtu = mtu;
break;
case CTL_TC:
if (mud_set_tc(mud, req.tc))
@@ -317,7 +285,6 @@ gt_bind(int argc, char **argv)
break;
case CTL_STATUS:
res.status.mtu = mtu;
res.status.mtu_auto = (icmp_fd != -1);
res.status.chacha = chacha;
res.status.bind = bind_addr;
res.status.peer = peer_addr;
@@ -375,11 +342,12 @@ gt_bind(int argc, char **argv)

int r = mud_send(mud, &buf[p], q - p, tc);

if (r == -1 && errno == EMSGSIZE) {
mtu = gt_setup_mtu(mud, tun_name);
} else {
if (r == -1 && errno != EAGAIN)
if (r == -1) {
if (errno == EMSGSIZE) {
mtu = gt_setup_mtu(mud, tun_name);
} else if (errno != EAGAIN) {
perror("mud_send");
}
}

p = q;
1 change: 0 additions & 1 deletion src/ctl.h
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@ struct ctl_msg {
struct mud_path path_status;
struct {
size_t mtu;
int mtu_auto;
int chacha;
struct sockaddr_storage bind;
struct sockaddr_storage peer;
4 changes: 0 additions & 4 deletions src/show.c
Original file line number Diff line number Diff line change
@@ -32,25 +32,21 @@ gt_show_dev_status(int fd, const char *dev)
printf("server %s:\n"
" bind: %s port %"PRIu16"\n"
" mtu: %zu\n"
" auto mtu: %s\n"
" cipher: %s\n",
dev,
bindstr, gt_get_port((struct sockaddr *)&res.status.bind),
res.status.mtu,
res.status.mtu_auto ? "enabled" : "disabled",
res.status.chacha ? "chacha20poly1305" : "aes256gcm");
} else {
printf("client %s:\n"
" bind: %s port %"PRIu16"\n"
" peer: %s port %"PRIu16"\n"
" mtu: %zu\n"
" auto mtu: %s\n"
" cipher: %s\n",
dev,
bindstr, gt_get_port((struct sockaddr *)&res.status.bind),
peerstr, gt_get_port((struct sockaddr *)&res.status.peer),
res.status.mtu,
res.status.mtu_auto ? "enabled" : "disabled",
res.status.chacha ? "chacha20poly1305" : "aes256gcm");
}

2 changes: 1 addition & 1 deletion systemd/glorytun-setup
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ HOST=$HOST
PORT=$PORT
BIND=$BIND
BIND_PORT=$BIND_PORT
OPTIONS="mtu auto"
OPTIONS=
EOF

( umask 077; echo "$KEY" > "$DIR/key" )
2 changes: 1 addition & 1 deletion systemd/glorytun@.service.in
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ Restart=always
EnvironmentFile=/etc/glorytun/%i/env
ExecStart=@bindir@/glorytun-run keyfile /etc/glorytun/%i/key $OPTIONS
ExecStartPost=-/etc/glorytun/%i/post.sh
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW
CapabilityBoundingSet=CAP_NET_ADMIN

[Install]
WantedBy=multi-user.target

0 comments on commit 4fab60e

Please sign in to comment.