Skip to content

Commit

Permalink
Fix --mptcp on kernels with mainlined MPTCP.
Browse files Browse the repository at this point in the history
Mainlined MPTCP on Linux 5.6+ is enabled with IPPROTO_MPTCP rather than setsockopt.
  • Loading branch information
tschmelcher authored and Max Lv committed May 23, 2022
1 parent e21f824 commit 1630764
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,13 @@ create_remote(listen_ctx_t *listener,
remote_addr = addr;
}

int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
int protocol = IPPROTO_TCP;
#ifdef IPPROTO_MPTCP
if (listener->mptcp > 0) {
protocol = IPPROTO_MPTCP;
}
#endif
int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, protocol);

if (remotefd == -1) {
ERROR("socket");
Expand All @@ -1285,6 +1291,7 @@ create_remote(listen_ctx_t *listener,
setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif

#ifndef IPPROTO_MPTCP
if (listener->mptcp > 1) {
int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt));
if (err == -1) {
Expand All @@ -1303,6 +1310,7 @@ create_remote(listen_ctx_t *listener,
ERROR("failed to enable multipath TCP");
}
}
#endif

if (tcp_outgoing_sndbuf > 0) {
setsockopt(remotefd, SOL_SOCKET, SO_SNDBUF, &tcp_outgoing_sndbuf, sizeof(int));
Expand Down
2 changes: 2 additions & 0 deletions src/netutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ typedef struct {
char *port;
} ss_addr_t;

#ifndef IPPROTO_MPTCP
/* MPTCP_ENABLED setsockopt values for kernel 4 & 3, best behaviour to be independant of kernel version is to test from newest to the latest values */
#ifndef MPTCP_ENABLED
static const char mptcp_enabled_values[] = { 42, 26, 0 };
#else
static const char mptcp_enabled_values[] = { MPTCP_ENABLED, 0 };
#endif
#endif

#ifndef UPDATE_INTERVAL
#define UPDATE_INTERVAL 5
Expand Down
10 changes: 9 additions & 1 deletion src/redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,13 @@ accept_cb(EV_P_ ev_io *w, int revents)
int index = rand() % listener->remote_num;
struct sockaddr *remote_addr = listener->remote_addr[index];

int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
int protocol = IPPROTO_TCP;
#ifdef IPPROTO_MPTCP
if (listener->mptcp > 0) {
protocol = IPPROTO_MPTCP;
}
#endif
int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, protocol);
if (remotefd == -1) {
ERROR("socket");
return;
Expand Down Expand Up @@ -808,6 +814,7 @@ accept_cb(EV_P_ ev_io *w, int revents)
#endif
}

#ifndef IPPROTO_MPTCP
// Enable MPTCP
if (listener->mptcp > 1) {
int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt));
Expand All @@ -827,6 +834,7 @@ accept_cb(EV_P_ ev_io *w, int revents)
ERROR("failed to enable multipath TCP");
}
}
#endif

if (tcp_outgoing_sndbuf > 0) {
setsockopt(remotefd, SOL_SOCKET, SO_SNDBUF, &tcp_outgoing_sndbuf, sizeof(int));
Expand Down
10 changes: 9 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,13 @@ create_and_bind(const char *host, const char *port, int mptcp)
}

for (/*rp = result*/; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
int protocol = rp->ai_protocol;
#ifdef IPPROTO_MPTCP
if (mptcp == 1) {
protocol = IPPROTO_MPTCP;
}
#endif
listen_sock = socket(rp->ai_family, rp->ai_socktype, protocol);
if (listen_sock == -1) {
continue;
}
Expand All @@ -616,6 +622,7 @@ create_and_bind(const char *host, const char *port, int mptcp)
}
}

#ifndef IPPROTO_MPTCP
if (mptcp == 1) {
int i = 0;
while ((mptcp = mptcp_enabled_values[i]) > 0) {
Expand All @@ -629,6 +636,7 @@ create_and_bind(const char *host, const char *port, int mptcp)
ERROR("failed to enable multipath TCP");
}
}
#endif

s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
if (s == 0) {
Expand Down
10 changes: 9 additions & 1 deletion src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,13 @@ accept_cb(EV_P_ ev_io *w, int revents)
int index = rand() % listener->remote_num;
struct sockaddr *remote_addr = listener->remote_addr[index];

int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
int protocol = IPPROTO_TCP;
#ifdef IPPROTO_MPTCP
if (listener->mptcp > 0) {
protocol = IPPROTO_MPTCP;
}
#endif
int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, protocol);
if (remotefd == -1) {
ERROR("socket");
return;
Expand Down Expand Up @@ -767,6 +773,7 @@ accept_cb(EV_P_ ev_io *w, int revents)
setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif

#ifndef IPPROTO_MPTCP
if (listener->mptcp > 1) {
int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt));
if (err == -1) {
Expand All @@ -785,6 +792,7 @@ accept_cb(EV_P_ ev_io *w, int revents)
ERROR("failed to enable multipath TCP");
}
}
#endif

if (tcp_outgoing_sndbuf > 0) {
setsockopt(remotefd, SOL_SOCKET, SO_SNDBUF, &tcp_outgoing_sndbuf, sizeof(int));
Expand Down

0 comments on commit 1630764

Please sign in to comment.