From 5074cd336d7b9f50d8e5f43afedeccad42993f8c Mon Sep 17 00:00:00 2001 From: Sebastien DUPONCHEEL Date: Thu, 2 Mar 2017 17:19:40 +0100 Subject: [PATCH] mptcp: make shadowsocks kernel independant --- src/local.c | 16 ++++++++++++++-- src/netutils.h | 8 +++----- src/redir.c | 16 ++++++++++++++-- src/server.c | 11 +++++++++-- src/tunnel.c | 16 ++++++++++++++-- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/local.c b/src/local.c index 28472901..76076354 100644 --- a/src/local.c +++ b/src/local.c @@ -1112,11 +1112,23 @@ create_remote(listen_ctx_t *listener, setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif - if (listener->mptcp == 1) { - int err = setsockopt(remotefd, SOL_TCP, MPTCP_ENABLED, &opt, sizeof(opt)); + if (listener->mptcp > 1) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); if (err == -1) { ERROR("failed to enable multipath TCP"); } + } else if (listener->mptcp == 1) { + int i = 0; + while((listener->mptcp = mptcp_enabled_values[i]) > 0) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); + if (err != -1) { + break; + } + i++; + } + if (listener->mptcp == 0) { + ERROR("failed to enable multipath TCP"); + } } // Setup diff --git a/src/netutils.h b/src/netutils.h index 95451fa1..e7dc07dc 100644 --- a/src/netutils.h +++ b/src/netutils.h @@ -46,13 +46,11 @@ #endif #endif -/* Backward compatibility for MPTCP_ENABLED between kernel 3 & 4 */ +/* 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 -#ifdef TCP_CC_INFO -#define MPTCP_ENABLED 42 +static const char mptcp_enabled_values[] = { 42, 26, 0 }; #else -#define MPTCP_ENABLED 26 -#endif +static const char mptcp_enabled_values[] = { MPTCP_ENABLED, 0 }; #endif #ifndef UPDATE_INTERVAL diff --git a/src/redir.c b/src/redir.c index 7e69eb5a..35b29116 100644 --- a/src/redir.c +++ b/src/redir.c @@ -784,11 +784,23 @@ accept_cb(EV_P_ ev_io *w, int revents) setnonblocking(remotefd); // Enable MPTCP - if (listener->mptcp == 1) { - int err = setsockopt(remotefd, SOL_TCP, MPTCP_ENABLED, &opt, sizeof(opt)); + if (listener->mptcp > 1) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); if (err == -1) { ERROR("failed to enable multipath TCP"); } + } else if (listener->mptcp == 1) { + int i = 0; + while((listener->mptcp = mptcp_enabled_values[i]) > 0) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); + if (err != -1) { + break; + } + i++; + } + if (listener->mptcp == 0) { + ERROR("failed to enable multipath TCP"); + } } server_t *server = new_server(serverfd); diff --git a/src/server.c b/src/server.c index e492b167..ff24b217 100644 --- a/src/server.c +++ b/src/server.c @@ -388,8 +388,15 @@ create_and_bind(const char *host, const char *port, int mptcp) } if (mptcp == 1) { - int err = setsockopt(listen_sock, SOL_TCP, MPTCP_ENABLED, &opt, sizeof(opt)); - if (err == -1) { + int i = 0; + while((mptcp = mptcp_enabled_values[i]) > 0) { + int err = setsockopt(listen_sock, IPPROTO_TCP, mptcp, &opt, sizeof(opt)); + if (err != -1) { + break; + } + i++; + } + if (mptcp == 0) { ERROR("failed to enable multipath TCP"); } } diff --git a/src/tunnel.c b/src/tunnel.c index 098478a5..9c792f84 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -659,11 +659,23 @@ accept_cb(EV_P_ ev_io *w, int revents) setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif - if (listener->mptcp == 1) { - int err = setsockopt(remotefd, SOL_TCP, MPTCP_ENABLED, &opt, sizeof(opt)); + if (listener->mptcp > 1) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); if (err == -1) { ERROR("failed to enable multipath TCP"); } + } else if (listener->mptcp == 1) { + int i = 0; + while((listener->mptcp = mptcp_enabled_values[i]) > 0) { + int err = setsockopt(remotefd, SOL_TCP, listener->mptcp, &opt, sizeof(opt)); + if (err != -1) { + break; + } + i++; + } + if (listener->mptcp == 0) { + ERROR("failed to enable multipath TCP"); + } } // Setup