Merge branch 'mptcp-part-two'
Christoph Paasch says: ==================== Multipath TCP part 2: Single subflow & RFC8684 support v2 -> v3: Added RFC8684-style handshake (see below fore more details) and some minor fixes v1 -> v2: Rebased on latest "Multipath TCP: Prerequisites" v3 series This set adds MPTCP connection establishment, writing & reading MPTCP options on data packets, a sysctl to allow MPTCP per-namespace, and self tests. This is sufficient to establish and maintain a connection with a MPTCP peer, but will not yet allow or initiate establishment of additional MPTCP subflows. We also add the necessary code for the RFC8684-style handshake. RFC8684 obsoletes the experimental RFC6824 and makes MPTCP move-on to version 1. Originally our plan was to submit single-subflow and RFC8684 support in two patchsets, but to simplify the merging-process and ensure that a coherent MPTCP-version lands in Linux we decided to merge the two sets into a single one. The MPTCP patchset exclusively supports RFC 8684. Although all MPTCP deployments are currently based on RFC 6824, future deployments will be migrating to MPTCP version 1. 3GPP's 5G standardization also solely supports RFC 8684. In addition, we believe that this initial submission of MPTCP will be cleaner by solely supporting RFC 8684. If later on support for the old MPTCP-version is required it can always be added in the future. The major difference between RFC 8684 and RFC 6824 is that it has a better support for servers using TCP SYN-cookies by reliably retransmitting the MP_CAPABLE option. Before ending this cover letter with some refs, it is worth mentioning that we promise David Miller that merging this series will be rewarded by Twitter dopamine hits :-D Clone/fetch: https://github.com/multipath-tcp/mptcp_net-next.git (tag: netdev-v3-part2) Browse: https://github.com/multipath-tcp/mptcp_net-next/tree/netdev-v3-part2 Thank you for your review. You can find us at mptcp@lists.01.org and https://is.gd/mptcp_upstream ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
commit
08a45c59f1
|
@ -11583,6 +11583,8 @@ W: https://github.com/multipath-tcp/mptcp_net-next/wiki
|
|||
B: https://github.com/multipath-tcp/mptcp_net-next/issues
|
||||
S: Maintained
|
||||
F: include/net/mptcp.h
|
||||
F: net/mptcp/
|
||||
F: tools/testing/selftests/net/mptcp/
|
||||
|
||||
NETWORKING [TCP]
|
||||
M: Eric Dumazet <edumazet@google.com>
|
||||
|
|
|
@ -78,6 +78,27 @@ struct tcp_sack_block {
|
|||
#define TCP_SACK_SEEN (1 << 0) /*1 = peer is SACK capable, */
|
||||
#define TCP_DSACK_SEEN (1 << 2) /*1 = DSACK was received from peer*/
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
struct mptcp_options_received {
|
||||
u64 sndr_key;
|
||||
u64 rcvr_key;
|
||||
u64 data_ack;
|
||||
u64 data_seq;
|
||||
u32 subflow_seq;
|
||||
u16 data_len;
|
||||
u8 mp_capable : 1,
|
||||
mp_join : 1,
|
||||
dss : 1;
|
||||
u8 use_map:1,
|
||||
dsn64:1,
|
||||
data_fin:1,
|
||||
use_ack:1,
|
||||
ack64:1,
|
||||
mpc_map:1,
|
||||
__unused:2;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct tcp_options_received {
|
||||
/* PAWS/RTTM data */
|
||||
int ts_recent_stamp;/* Time we stored ts_recent (for aging) */
|
||||
|
@ -95,6 +116,9 @@ struct tcp_options_received {
|
|||
u8 num_sacks; /* Number of SACK blocks */
|
||||
u16 user_mss; /* mss requested by user in ioctl */
|
||||
u16 mss_clamp; /* Maximal mss, negotiated at connection setup */
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
struct mptcp_options_received mptcp;
|
||||
#endif
|
||||
};
|
||||
|
||||
static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
|
||||
|
@ -104,6 +128,11 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
|
|||
#if IS_ENABLED(CONFIG_SMC)
|
||||
rx_opt->smc_ok = 0;
|
||||
#endif
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
rx_opt->mptcp.mp_capable = 0;
|
||||
rx_opt->mptcp.mp_join = 0;
|
||||
rx_opt->mptcp.dss = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* This is the max number of SACKS that we'll generate and process. It's safe
|
||||
|
@ -119,6 +148,9 @@ struct tcp_request_sock {
|
|||
const struct tcp_request_sock_ops *af_specific;
|
||||
u64 snt_synack; /* first SYNACK sent time */
|
||||
bool tfo_listener;
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
bool is_mptcp;
|
||||
#endif
|
||||
u32 txhash;
|
||||
u32 rcv_isn;
|
||||
u32 snt_isn;
|
||||
|
@ -379,6 +411,9 @@ struct tcp_sock {
|
|||
u32 mtu_info; /* We received an ICMP_FRAG_NEEDED / ICMPV6_PKT_TOOBIG
|
||||
* while socket was owned by user.
|
||||
*/
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
bool is_mptcp;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_TCP_MD5SIG
|
||||
/* TCP AF-Specific parts; only used by MD5 Signature support so far */
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#define __NET_MPTCP_H
|
||||
|
||||
#include <linux/skbuff.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/types.h>
|
||||
|
||||
/* MPTCP sk_buff extension data */
|
||||
|
@ -22,12 +23,49 @@ struct mptcp_ext {
|
|||
data_fin:1,
|
||||
use_ack:1,
|
||||
ack64:1,
|
||||
__unused:3;
|
||||
mpc_map:1,
|
||||
__unused:2;
|
||||
/* one byte hole */
|
||||
};
|
||||
|
||||
struct mptcp_out_options {
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
u16 suboptions;
|
||||
u64 sndr_key;
|
||||
u64 rcvr_key;
|
||||
struct mptcp_ext ext_copy;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef CONFIG_MPTCP
|
||||
|
||||
void mptcp_init(void);
|
||||
|
||||
static inline bool sk_is_mptcp(const struct sock *sk)
|
||||
{
|
||||
return tcp_sk(sk)->is_mptcp;
|
||||
}
|
||||
|
||||
static inline bool rsk_is_mptcp(const struct request_sock *req)
|
||||
{
|
||||
return tcp_rsk(req)->is_mptcp;
|
||||
}
|
||||
|
||||
void mptcp_parse_option(const struct sk_buff *skb, const unsigned char *ptr,
|
||||
int opsize, struct tcp_options_received *opt_rx);
|
||||
bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
|
||||
unsigned int *size, struct mptcp_out_options *opts);
|
||||
void mptcp_rcv_synsent(struct sock *sk);
|
||||
bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
|
||||
struct mptcp_out_options *opts);
|
||||
bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
|
||||
unsigned int *size, unsigned int remaining,
|
||||
struct mptcp_out_options *opts);
|
||||
void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
|
||||
struct tcp_options_received *opt_rx);
|
||||
|
||||
void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts);
|
||||
|
||||
/* move the skb extension owership, with the assumption that 'to' is
|
||||
* newly allocated
|
||||
*/
|
||||
|
@ -70,6 +108,59 @@ static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
|
|||
|
||||
#else
|
||||
|
||||
static inline void mptcp_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
static inline bool sk_is_mptcp(const struct sock *sk)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool rsk_is_mptcp(const struct request_sock *req)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void mptcp_parse_option(const struct sk_buff *skb,
|
||||
const unsigned char *ptr, int opsize,
|
||||
struct tcp_options_received *opt_rx)
|
||||
{
|
||||
}
|
||||
|
||||
static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
|
||||
unsigned int *size,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void mptcp_rcv_synsent(struct sock *sk)
|
||||
{
|
||||
}
|
||||
|
||||
static inline bool mptcp_synack_options(const struct request_sock *req,
|
||||
unsigned int *size,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool mptcp_established_options(struct sock *sk,
|
||||
struct sk_buff *skb,
|
||||
unsigned int *size,
|
||||
unsigned int remaining,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void mptcp_incoming_options(struct sock *sk,
|
||||
struct sk_buff *skb,
|
||||
struct tcp_options_received *opt_rx)
|
||||
{
|
||||
}
|
||||
|
||||
static inline void mptcp_skb_ext_move(struct sk_buff *to,
|
||||
const struct sk_buff *from)
|
||||
{
|
||||
|
@ -82,4 +173,16 @@ static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
|
|||
}
|
||||
|
||||
#endif /* CONFIG_MPTCP */
|
||||
|
||||
void mptcp_handle_ipv6_mapped(struct sock *sk, bool mapped);
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
int mptcpv6_init(void);
|
||||
#elif IS_ENABLED(CONFIG_IPV6)
|
||||
static inline int mptcpv6_init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __NET_MPTCP_H */
|
||||
|
|
|
@ -91,6 +91,7 @@ if INET
|
|||
source "net/ipv4/Kconfig"
|
||||
source "net/ipv6/Kconfig"
|
||||
source "net/netlabel/Kconfig"
|
||||
source "net/mptcp/Kconfig"
|
||||
|
||||
endif # if INET
|
||||
|
||||
|
|
|
@ -87,3 +87,4 @@ endif
|
|||
obj-$(CONFIG_QRTR) += qrtr/
|
||||
obj-$(CONFIG_NET_NCSI) += ncsi/
|
||||
obj-$(CONFIG_XDP_SOCKETS) += xdp/
|
||||
obj-$(CONFIG_MPTCP) += mptcp/
|
||||
|
|
|
@ -271,6 +271,7 @@
|
|||
#include <net/icmp.h>
|
||||
#include <net/inet_common.h>
|
||||
#include <net/tcp.h>
|
||||
#include <net/mptcp.h>
|
||||
#include <net/xfrm.h>
|
||||
#include <net/ip.h>
|
||||
#include <net/sock.h>
|
||||
|
@ -4021,4 +4022,5 @@ void __init tcp_init(void)
|
|||
tcp_metrics_init();
|
||||
BUG_ON(tcp_register_congestion_control(&tcp_reno) != 0);
|
||||
tcp_tasklet_init();
|
||||
mptcp_init();
|
||||
}
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
#include <trace/events/tcp.h>
|
||||
#include <linux/jump_label_ratelimit.h>
|
||||
#include <net/busy_poll.h>
|
||||
#include <net/mptcp.h>
|
||||
|
||||
int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
|
||||
|
||||
|
@ -3924,6 +3925,10 @@ void tcp_parse_options(const struct net *net,
|
|||
*/
|
||||
break;
|
||||
#endif
|
||||
case TCPOPT_MPTCP:
|
||||
mptcp_parse_option(skb, ptr, opsize, opt_rx);
|
||||
break;
|
||||
|
||||
case TCPOPT_FASTOPEN:
|
||||
tcp_parse_fastopen_option(
|
||||
opsize - TCPOLEN_FASTOPEN_BASE,
|
||||
|
@ -4765,6 +4770,9 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
|
|||
bool fragstolen;
|
||||
int eaten;
|
||||
|
||||
if (sk_is_mptcp(sk))
|
||||
mptcp_incoming_options(sk, skb, &tp->rx_opt);
|
||||
|
||||
if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
|
||||
__kfree_skb(skb);
|
||||
return;
|
||||
|
@ -5973,6 +5981,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
|
|||
tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
|
||||
tcp_initialize_rcv_mss(sk);
|
||||
|
||||
if (sk_is_mptcp(sk))
|
||||
mptcp_rcv_synsent(sk);
|
||||
|
||||
/* Remember, tcp_poll() does not lock socket!
|
||||
* Change state from SYN-SENT only after copied_seq
|
||||
* is initialized. */
|
||||
|
@ -6338,8 +6349,11 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
|
|||
case TCP_CLOSE_WAIT:
|
||||
case TCP_CLOSING:
|
||||
case TCP_LAST_ACK:
|
||||
if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
|
||||
if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
|
||||
if (sk_is_mptcp(sk))
|
||||
mptcp_incoming_options(sk, skb, &tp->rx_opt);
|
||||
break;
|
||||
}
|
||||
/* fall through */
|
||||
case TCP_FIN_WAIT1:
|
||||
case TCP_FIN_WAIT2:
|
||||
|
@ -6595,6 +6609,9 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
|
|||
|
||||
tcp_rsk(req)->af_specific = af_ops;
|
||||
tcp_rsk(req)->ts_off = 0;
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
tcp_rsk(req)->is_mptcp = 0;
|
||||
#endif
|
||||
|
||||
tcp_clear_options(&tmp_opt);
|
||||
tmp_opt.mss_clamp = af_ops->mss_clamp;
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
#define pr_fmt(fmt) "TCP: " fmt
|
||||
|
||||
#include <net/tcp.h>
|
||||
#include <net/mptcp.h>
|
||||
|
||||
#include <linux/compiler.h>
|
||||
#include <linux/gfp.h>
|
||||
|
@ -414,6 +415,7 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp)
|
|||
#define OPTION_WSCALE (1 << 3)
|
||||
#define OPTION_FAST_OPEN_COOKIE (1 << 8)
|
||||
#define OPTION_SMC (1 << 9)
|
||||
#define OPTION_MPTCP (1 << 10)
|
||||
|
||||
static void smc_options_write(__be32 *ptr, u16 *options)
|
||||
{
|
||||
|
@ -439,8 +441,17 @@ struct tcp_out_options {
|
|||
__u8 *hash_location; /* temporary pointer, overloaded */
|
||||
__u32 tsval, tsecr; /* need to include OPTION_TS */
|
||||
struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */
|
||||
struct mptcp_out_options mptcp;
|
||||
};
|
||||
|
||||
static void mptcp_options_write(__be32 *ptr, struct tcp_out_options *opts)
|
||||
{
|
||||
#if IS_ENABLED(CONFIG_MPTCP)
|
||||
if (unlikely(OPTION_MPTCP & opts->options))
|
||||
mptcp_write_options(ptr, &opts->mptcp);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Write previously computed TCP options to the packet.
|
||||
*
|
||||
* Beware: Something in the Internet is very sensitive to the ordering of
|
||||
|
@ -549,6 +560,8 @@ static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
|
|||
}
|
||||
|
||||
smc_options_write(ptr, &options);
|
||||
|
||||
mptcp_options_write(ptr, opts);
|
||||
}
|
||||
|
||||
static void smc_set_option(const struct tcp_sock *tp,
|
||||
|
@ -584,6 +597,22 @@ static void smc_set_option_cond(const struct tcp_sock *tp,
|
|||
#endif
|
||||
}
|
||||
|
||||
static void mptcp_set_option_cond(const struct request_sock *req,
|
||||
struct tcp_out_options *opts,
|
||||
unsigned int *remaining)
|
||||
{
|
||||
if (rsk_is_mptcp(req)) {
|
||||
unsigned int size;
|
||||
|
||||
if (mptcp_synack_options(req, &size, &opts->mptcp)) {
|
||||
if (*remaining >= size) {
|
||||
opts->options |= OPTION_MPTCP;
|
||||
*remaining -= size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Compute TCP options for SYN packets. This is not the final
|
||||
* network wire format yet.
|
||||
*/
|
||||
|
@ -653,6 +682,15 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
|
|||
|
||||
smc_set_option(tp, opts, &remaining);
|
||||
|
||||
if (sk_is_mptcp(sk)) {
|
||||
unsigned int size;
|
||||
|
||||
if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) {
|
||||
opts->options |= OPTION_MPTCP;
|
||||
remaining -= size;
|
||||
}
|
||||
}
|
||||
|
||||
return MAX_TCP_OPTION_SPACE - remaining;
|
||||
}
|
||||
|
||||
|
@ -714,6 +752,8 @@ static unsigned int tcp_synack_options(const struct sock *sk,
|
|||
}
|
||||
}
|
||||
|
||||
mptcp_set_option_cond(req, opts, &remaining);
|
||||
|
||||
smc_set_option_cond(tcp_sk(sk), ireq, opts, &remaining);
|
||||
|
||||
return MAX_TCP_OPTION_SPACE - remaining;
|
||||
|
@ -751,6 +791,23 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
|
|||
size += TCPOLEN_TSTAMP_ALIGNED;
|
||||
}
|
||||
|
||||
/* MPTCP options have precedence over SACK for the limited TCP
|
||||
* option space because a MPTCP connection would be forced to
|
||||
* fall back to regular TCP if a required multipath option is
|
||||
* missing. SACK still gets a chance to use whatever space is
|
||||
* left.
|
||||
*/
|
||||
if (sk_is_mptcp(sk)) {
|
||||
unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
|
||||
unsigned int opt_size = 0;
|
||||
|
||||
if (mptcp_established_options(sk, skb, &opt_size, remaining,
|
||||
&opts->mptcp)) {
|
||||
opts->options |= OPTION_MPTCP;
|
||||
size += opt_size;
|
||||
}
|
||||
}
|
||||
|
||||
eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
|
||||
if (unlikely(eff_sacks)) {
|
||||
const unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
|
||||
|
|
|
@ -238,6 +238,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
|
|||
sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
|
||||
|
||||
icsk->icsk_af_ops = &ipv6_mapped;
|
||||
if (sk_is_mptcp(sk))
|
||||
mptcp_handle_ipv6_mapped(sk, true);
|
||||
sk->sk_backlog_rcv = tcp_v4_do_rcv;
|
||||
#ifdef CONFIG_TCP_MD5SIG
|
||||
tp->af_specific = &tcp_sock_ipv6_mapped_specific;
|
||||
|
@ -248,6 +250,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
|
|||
if (err) {
|
||||
icsk->icsk_ext_hdr_len = exthdrlen;
|
||||
icsk->icsk_af_ops = &ipv6_specific;
|
||||
if (sk_is_mptcp(sk))
|
||||
mptcp_handle_ipv6_mapped(sk, false);
|
||||
sk->sk_backlog_rcv = tcp_v6_do_rcv;
|
||||
#ifdef CONFIG_TCP_MD5SIG
|
||||
tp->af_specific = &tcp_sock_ipv6_specific;
|
||||
|
@ -1203,6 +1207,8 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
|
|||
newnp->saddr = newsk->sk_v6_rcv_saddr;
|
||||
|
||||
inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
|
||||
if (sk_is_mptcp(newsk))
|
||||
mptcp_handle_ipv6_mapped(newsk, true);
|
||||
newsk->sk_backlog_rcv = tcp_v4_do_rcv;
|
||||
#ifdef CONFIG_TCP_MD5SIG
|
||||
newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
|
||||
|
@ -2163,9 +2169,16 @@ int __init tcpv6_init(void)
|
|||
ret = register_pernet_subsys(&tcpv6_net_ops);
|
||||
if (ret)
|
||||
goto out_tcpv6_protosw;
|
||||
|
||||
ret = mptcpv6_init();
|
||||
if (ret)
|
||||
goto out_tcpv6_pernet_subsys;
|
||||
|
||||
out:
|
||||
return ret;
|
||||
|
||||
out_tcpv6_pernet_subsys:
|
||||
unregister_pernet_subsys(&tcpv6_net_ops);
|
||||
out_tcpv6_protosw:
|
||||
inet6_unregister_protosw(&tcpv6_protosw);
|
||||
out_tcpv6_protocol:
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
config MPTCP
|
||||
bool "MPTCP: Multipath TCP"
|
||||
depends on INET
|
||||
select SKB_EXTENSIONS
|
||||
select CRYPTO_LIB_SHA256
|
||||
help
|
||||
Multipath TCP (MPTCP) connections send and receive data over multiple
|
||||
subflows in order to utilize multiple network paths. Each subflow
|
||||
uses the TCP protocol, and TCP options carry header information for
|
||||
MPTCP.
|
||||
|
||||
config MPTCP_IPV6
|
||||
bool "MPTCP: IPv6 support for Multipath TCP"
|
||||
depends on MPTCP
|
||||
select IPV6
|
||||
default y
|
||||
|
||||
config MPTCP_HMAC_TEST
|
||||
bool "Tests for MPTCP HMAC implementation"
|
||||
default n
|
||||
help
|
||||
This option enable boot time self-test for the HMAC implementation
|
||||
used by the MPTCP code
|
||||
|
||||
Say N if you are unsure.
|
|
@ -0,0 +1,4 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
obj-$(CONFIG_MPTCP) += mptcp.o
|
||||
|
||||
mptcp-y := protocol.o subflow.o options.o token.o crypto.o ctrl.o
|
|
@ -0,0 +1,152 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP cryptographic functions
|
||||
* Copyright (c) 2017 - 2019, Intel Corporation.
|
||||
*
|
||||
* Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
|
||||
* mptcp_ipv6 from multipath-tcp.org, authored by:
|
||||
*
|
||||
* Sébastien Barré <sebastien.barre@uclouvain.be>
|
||||
* Christoph Paasch <christoph.paasch@uclouvain.be>
|
||||
* Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
|
||||
* Gregory Detal <gregory.detal@uclouvain.be>
|
||||
* Fabien Duchêne <fabien.duchene@uclouvain.be>
|
||||
* Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
|
||||
* Lavkesh Lahngir <lavkesh51@gmail.com>
|
||||
* Andreas Ripke <ripke@neclab.eu>
|
||||
* Vlad Dogaru <vlad.dogaru@intel.com>
|
||||
* Octavian Purdila <octavian.purdila@intel.com>
|
||||
* John Ronan <jronan@tssg.org>
|
||||
* Catalin Nicutar <catalin.nicutar@gmail.com>
|
||||
* Brandon Heller <brandonh@stanford.edu>
|
||||
*/
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <crypto/sha.h>
|
||||
#include <asm/unaligned.h>
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#define SHA256_DIGEST_WORDS (SHA256_DIGEST_SIZE / 4)
|
||||
|
||||
void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
|
||||
{
|
||||
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
|
||||
__be64 input = cpu_to_be64(key);
|
||||
struct sha256_state state;
|
||||
|
||||
sha256_init(&state);
|
||||
sha256_update(&state, (__force u8 *)&input, sizeof(input));
|
||||
sha256_final(&state, (u8 *)mptcp_hashed_key);
|
||||
|
||||
if (token)
|
||||
*token = be32_to_cpu(mptcp_hashed_key[0]);
|
||||
if (idsn)
|
||||
*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
|
||||
}
|
||||
|
||||
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
|
||||
void *hmac)
|
||||
{
|
||||
u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
|
||||
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
|
||||
__be32 *hash_out = (__force __be32 *)hmac;
|
||||
struct sha256_state state;
|
||||
u8 key1be[8];
|
||||
u8 key2be[8];
|
||||
int i;
|
||||
|
||||
put_unaligned_be64(key1, key1be);
|
||||
put_unaligned_be64(key2, key2be);
|
||||
|
||||
/* Generate key xored with ipad */
|
||||
memset(input, 0x36, SHA_MESSAGE_BYTES);
|
||||
for (i = 0; i < 8; i++)
|
||||
input[i] ^= key1be[i];
|
||||
for (i = 0; i < 8; i++)
|
||||
input[i + 8] ^= key2be[i];
|
||||
|
||||
put_unaligned_be32(nonce1, &input[SHA256_BLOCK_SIZE]);
|
||||
put_unaligned_be32(nonce2, &input[SHA256_BLOCK_SIZE + 4]);
|
||||
|
||||
sha256_init(&state);
|
||||
sha256_update(&state, input, SHA256_BLOCK_SIZE + 8);
|
||||
|
||||
/* emit sha256(K1 || msg) on the second input block, so we can
|
||||
* reuse 'input' for the last hashing
|
||||
*/
|
||||
sha256_final(&state, &input[SHA256_BLOCK_SIZE]);
|
||||
|
||||
/* Prepare second part of hmac */
|
||||
memset(input, 0x5C, SHA_MESSAGE_BYTES);
|
||||
for (i = 0; i < 8; i++)
|
||||
input[i] ^= key1be[i];
|
||||
for (i = 0; i < 8; i++)
|
||||
input[i + 8] ^= key2be[i];
|
||||
|
||||
sha256_init(&state);
|
||||
sha256_update(&state, input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE);
|
||||
sha256_final(&state, (u8 *)mptcp_hashed_key);
|
||||
|
||||
/* takes only first 160 bits */
|
||||
for (i = 0; i < 5; i++)
|
||||
hash_out[i] = mptcp_hashed_key[i];
|
||||
}
|
||||
|
||||
#ifdef CONFIG_MPTCP_HMAC_TEST
|
||||
struct test_cast {
|
||||
char *key;
|
||||
char *msg;
|
||||
char *result;
|
||||
};
|
||||
|
||||
/* we can't reuse RFC 4231 test vectors, as we have constraint on the
|
||||
* input and key size, and we truncate the output.
|
||||
*/
|
||||
static struct test_cast tests[] = {
|
||||
{
|
||||
.key = "0b0b0b0b0b0b0b0b",
|
||||
.msg = "48692054",
|
||||
.result = "8385e24fb4235ac37556b6b886db106284a1da67",
|
||||
},
|
||||
{
|
||||
.key = "aaaaaaaaaaaaaaaa",
|
||||
.msg = "dddddddd",
|
||||
.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492",
|
||||
},
|
||||
{
|
||||
.key = "0102030405060708",
|
||||
.msg = "cdcdcdcd",
|
||||
.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6",
|
||||
},
|
||||
};
|
||||
|
||||
static int __init test_mptcp_crypto(void)
|
||||
{
|
||||
char hmac[20], hmac_hex[41];
|
||||
u32 nonce1, nonce2;
|
||||
u64 key1, key2;
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(tests); ++i) {
|
||||
/* mptcp hmap will convert to be before computing the hmac */
|
||||
key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
|
||||
key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
|
||||
nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
|
||||
nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
|
||||
|
||||
mptcp_crypto_hmac_sha(key1, key2, nonce1, nonce2, hmac);
|
||||
for (j = 0; j < 20; ++j)
|
||||
sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
|
||||
hmac_hex[40] = 0;
|
||||
|
||||
if (memcmp(hmac_hex, tests[i].result, 40))
|
||||
pr_err("test %d failed, got %s expected %s", i,
|
||||
hmac_hex, tests[i].result);
|
||||
else
|
||||
pr_info("test %d [ ok ]", i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
late_initcall(test_mptcp_crypto);
|
||||
#endif
|
|
@ -0,0 +1,130 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP
|
||||
*
|
||||
* Copyright (c) 2019, Tessares SA.
|
||||
*/
|
||||
|
||||
#include <linux/sysctl.h>
|
||||
|
||||
#include <net/net_namespace.h>
|
||||
#include <net/netns/generic.h>
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#define MPTCP_SYSCTL_PATH "net/mptcp"
|
||||
|
||||
static int mptcp_pernet_id;
|
||||
struct mptcp_pernet {
|
||||
struct ctl_table_header *ctl_table_hdr;
|
||||
|
||||
int mptcp_enabled;
|
||||
};
|
||||
|
||||
static struct mptcp_pernet *mptcp_get_pernet(struct net *net)
|
||||
{
|
||||
return net_generic(net, mptcp_pernet_id);
|
||||
}
|
||||
|
||||
int mptcp_is_enabled(struct net *net)
|
||||
{
|
||||
return mptcp_get_pernet(net)->mptcp_enabled;
|
||||
}
|
||||
|
||||
static struct ctl_table mptcp_sysctl_table[] = {
|
||||
{
|
||||
.procname = "enabled",
|
||||
.maxlen = sizeof(int),
|
||||
.mode = 0644,
|
||||
/* users with CAP_NET_ADMIN or root (not and) can change this
|
||||
* value, same as other sysctl or the 'net' tree.
|
||||
*/
|
||||
.proc_handler = proc_dointvec,
|
||||
},
|
||||
{}
|
||||
};
|
||||
|
||||
static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
|
||||
{
|
||||
pernet->mptcp_enabled = 1;
|
||||
}
|
||||
|
||||
static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
|
||||
{
|
||||
struct ctl_table_header *hdr;
|
||||
struct ctl_table *table;
|
||||
|
||||
table = mptcp_sysctl_table;
|
||||
if (!net_eq(net, &init_net)) {
|
||||
table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
|
||||
if (!table)
|
||||
goto err_alloc;
|
||||
}
|
||||
|
||||
table[0].data = &pernet->mptcp_enabled;
|
||||
|
||||
hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
|
||||
if (!hdr)
|
||||
goto err_reg;
|
||||
|
||||
pernet->ctl_table_hdr = hdr;
|
||||
|
||||
return 0;
|
||||
|
||||
err_reg:
|
||||
if (!net_eq(net, &init_net))
|
||||
kfree(table);
|
||||
err_alloc:
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
|
||||
{
|
||||
struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
|
||||
|
||||
unregister_net_sysctl_table(pernet->ctl_table_hdr);
|
||||
|
||||
kfree(table);
|
||||
}
|
||||
|
||||
static int __net_init mptcp_net_init(struct net *net)
|
||||
{
|
||||
struct mptcp_pernet *pernet = mptcp_get_pernet(net);
|
||||
|
||||
mptcp_pernet_set_defaults(pernet);
|
||||
|
||||
return mptcp_pernet_new_table(net, pernet);
|
||||
}
|
||||
|
||||
/* Note: the callback will only be called per extra netns */
|
||||
static void __net_exit mptcp_net_exit(struct net *net)
|
||||
{
|
||||
struct mptcp_pernet *pernet = mptcp_get_pernet(net);
|
||||
|
||||
mptcp_pernet_del_table(pernet);
|
||||
}
|
||||
|
||||
static struct pernet_operations mptcp_pernet_ops = {
|
||||
.init = mptcp_net_init,
|
||||
.exit = mptcp_net_exit,
|
||||
.id = &mptcp_pernet_id,
|
||||
.size = sizeof(struct mptcp_pernet),
|
||||
};
|
||||
|
||||
void __init mptcp_init(void)
|
||||
{
|
||||
mptcp_proto_init();
|
||||
|
||||
if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
|
||||
panic("Failed to register MPTCP pernet subsystem.\n");
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
int __init mptcpv6_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = mptcp_proto_v6_init();
|
||||
|
||||
return err;
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,586 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP
|
||||
*
|
||||
* Copyright (c) 2017 - 2019, Intel Corporation.
|
||||
*/
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <net/tcp.h>
|
||||
#include <net/mptcp.h>
|
||||
#include "protocol.h"
|
||||
|
||||
static bool mptcp_cap_flag_sha256(u8 flags)
|
||||
{
|
||||
return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
|
||||
}
|
||||
|
||||
void mptcp_parse_option(const struct sk_buff *skb, const unsigned char *ptr,
|
||||
int opsize, struct tcp_options_received *opt_rx)
|
||||
{
|
||||
struct mptcp_options_received *mp_opt = &opt_rx->mptcp;
|
||||
u8 subtype = *ptr >> 4;
|
||||
int expected_opsize;
|
||||
u8 version;
|
||||
u8 flags;
|
||||
|
||||
switch (subtype) {
|
||||
case MPTCPOPT_MP_CAPABLE:
|
||||
/* strict size checking */
|
||||
if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
|
||||
if (skb->len > tcp_hdr(skb)->doff << 2)
|
||||
expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
|
||||
else
|
||||
expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
|
||||
} else {
|
||||
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
|
||||
expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
|
||||
else
|
||||
expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
|
||||
}
|
||||
if (opsize != expected_opsize)
|
||||
break;
|
||||
|
||||
/* try to be gentle vs future versions on the initial syn */
|
||||
version = *ptr++ & MPTCP_VERSION_MASK;
|
||||
if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
|
||||
if (version != MPTCP_SUPPORTED_VERSION)
|
||||
break;
|
||||
} else if (version < MPTCP_SUPPORTED_VERSION) {
|
||||
break;
|
||||
}
|
||||
|
||||
flags = *ptr++;
|
||||
if (!mptcp_cap_flag_sha256(flags) ||
|
||||
(flags & MPTCP_CAP_EXTENSIBILITY))
|
||||
break;
|
||||
|
||||
/* RFC 6824, Section 3.1:
|
||||
* "For the Checksum Required bit (labeled "A"), if either
|
||||
* host requires the use of checksums, checksums MUST be used.
|
||||
* In other words, the only way for checksums not to be used
|
||||
* is if both hosts in their SYNs set A=0."
|
||||
*
|
||||
* Section 3.3.0:
|
||||
* "If a checksum is not present when its use has been
|
||||
* negotiated, the receiver MUST close the subflow with a RST as
|
||||
* it is considered broken."
|
||||
*
|
||||
* We don't implement DSS checksum - fall back to TCP.
|
||||
*/
|
||||
if (flags & MPTCP_CAP_CHECKSUM_REQD)
|
||||
break;
|
||||
|
||||
mp_opt->mp_capable = 1;
|
||||
if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
|
||||
mp_opt->sndr_key = get_unaligned_be64(ptr);
|
||||
ptr += 8;
|
||||
}
|
||||
if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
|
||||
mp_opt->rcvr_key = get_unaligned_be64(ptr);
|
||||
ptr += 8;
|
||||
}
|
||||
if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
|
||||
/* Section 3.1.:
|
||||
* "the data parameters in a MP_CAPABLE are semantically
|
||||
* equivalent to those in a DSS option and can be used
|
||||
* interchangeably."
|
||||
*/
|
||||
mp_opt->dss = 1;
|
||||
mp_opt->use_map = 1;
|
||||
mp_opt->mpc_map = 1;
|
||||
mp_opt->data_len = get_unaligned_be16(ptr);
|
||||
ptr += 2;
|
||||
}
|
||||
pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
|
||||
version, flags, opsize, mp_opt->sndr_key,
|
||||
mp_opt->rcvr_key, mp_opt->data_len);
|
||||
break;
|
||||
|
||||
case MPTCPOPT_DSS:
|
||||
pr_debug("DSS");
|
||||
ptr++;
|
||||
|
||||
/* we must clear 'mpc_map' be able to detect MP_CAPABLE
|
||||
* map vs DSS map in mptcp_incoming_options(), and reconstruct
|
||||
* map info accordingly
|
||||
*/
|
||||
mp_opt->mpc_map = 0;
|
||||
flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
|
||||
mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
|
||||
mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
|
||||
mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
|
||||
mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
|
||||
mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
|
||||
|
||||
pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
|
||||
mp_opt->data_fin, mp_opt->dsn64,
|
||||
mp_opt->use_map, mp_opt->ack64,
|
||||
mp_opt->use_ack);
|
||||
|
||||
expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
|
||||
|
||||
if (mp_opt->use_ack) {
|
||||
if (mp_opt->ack64)
|
||||
expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
|
||||
else
|
||||
expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
|
||||
}
|
||||
|
||||
if (mp_opt->use_map) {
|
||||
if (mp_opt->dsn64)
|
||||
expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
|
||||
else
|
||||
expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
|
||||
}
|
||||
|
||||
/* RFC 6824, Section 3.3:
|
||||
* If a checksum is present, but its use had
|
||||
* not been negotiated in the MP_CAPABLE handshake,
|
||||
* the checksum field MUST be ignored.
|
||||
*/
|
||||
if (opsize != expected_opsize &&
|
||||
opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
|
||||
break;
|
||||
|
||||
mp_opt->dss = 1;
|
||||
|
||||
if (mp_opt->use_ack) {
|
||||
if (mp_opt->ack64) {
|
||||
mp_opt->data_ack = get_unaligned_be64(ptr);
|
||||
ptr += 8;
|
||||
} else {
|
||||
mp_opt->data_ack = get_unaligned_be32(ptr);
|
||||
ptr += 4;
|
||||
}
|
||||
|
||||
pr_debug("data_ack=%llu", mp_opt->data_ack);
|
||||
}
|
||||
|
||||
if (mp_opt->use_map) {
|
||||
if (mp_opt->dsn64) {
|
||||
mp_opt->data_seq = get_unaligned_be64(ptr);
|
||||
ptr += 8;
|
||||
} else {
|
||||
mp_opt->data_seq = get_unaligned_be32(ptr);
|
||||
ptr += 4;
|
||||
}
|
||||
|
||||
mp_opt->subflow_seq = get_unaligned_be32(ptr);
|
||||
ptr += 4;
|
||||
|
||||
mp_opt->data_len = get_unaligned_be16(ptr);
|
||||
ptr += 2;
|
||||
|
||||
pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
|
||||
mp_opt->data_seq, mp_opt->subflow_seq,
|
||||
mp_opt->data_len);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void mptcp_get_options(const struct sk_buff *skb,
|
||||
struct tcp_options_received *opt_rx)
|
||||
{
|
||||
const unsigned char *ptr;
|
||||
const struct tcphdr *th = tcp_hdr(skb);
|
||||
int length = (th->doff * 4) - sizeof(struct tcphdr);
|
||||
|
||||
ptr = (const unsigned char *)(th + 1);
|
||||
|
||||
while (length > 0) {
|
||||
int opcode = *ptr++;
|
||||
int opsize;
|
||||
|
||||
switch (opcode) {
|
||||
case TCPOPT_EOL:
|
||||
return;
|
||||
case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
|
||||
length--;
|
||||
continue;
|
||||
default:
|
||||
opsize = *ptr++;
|
||||
if (opsize < 2) /* "silly options" */
|
||||
return;
|
||||
if (opsize > length)
|
||||
return; /* don't parse partial options */
|
||||
if (opcode == TCPOPT_MPTCP)
|
||||
mptcp_parse_option(skb, ptr, opsize, opt_rx);
|
||||
ptr += opsize - 2;
|
||||
length -= opsize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
|
||||
unsigned int *size, struct mptcp_out_options *opts)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
|
||||
/* we will use snd_isn to detect first pkt [re]transmission
|
||||
* in mptcp_established_options_mp()
|
||||
*/
|
||||
subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
|
||||
if (subflow->request_mptcp) {
|
||||
pr_debug("local_key=%llu", subflow->local_key);
|
||||
opts->suboptions = OPTION_MPTCP_MPC_SYN;
|
||||
opts->sndr_key = subflow->local_key;
|
||||
*size = TCPOLEN_MPTCP_MPC_SYN;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void mptcp_rcv_synsent(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct tcp_sock *tp = tcp_sk(sk);
|
||||
|
||||
pr_debug("subflow=%p", subflow);
|
||||
if (subflow->request_mptcp && tp->rx_opt.mptcp.mp_capable) {
|
||||
subflow->mp_capable = 1;
|
||||
subflow->can_ack = 1;
|
||||
subflow->remote_key = tp->rx_opt.mptcp.sndr_key;
|
||||
} else {
|
||||
tcp_sk(sk)->is_mptcp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
|
||||
unsigned int *size,
|
||||
unsigned int remaining,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct mptcp_ext *mpext;
|
||||
unsigned int data_len;
|
||||
|
||||
pr_debug("subflow=%p fourth_ack=%d seq=%x:%x remaining=%d", subflow,
|
||||
subflow->fourth_ack, subflow->snd_isn,
|
||||
skb ? TCP_SKB_CB(skb)->seq : 0, remaining);
|
||||
|
||||
if (subflow->mp_capable && !subflow->fourth_ack && skb &&
|
||||
subflow->snd_isn == TCP_SKB_CB(skb)->seq) {
|
||||
/* When skb is not available, we better over-estimate the
|
||||
* emitted options len. A full DSS option is longer than
|
||||
* TCPOLEN_MPTCP_MPC_ACK_DATA, so let's the caller try to fit
|
||||
* that.
|
||||
*/
|
||||
mpext = mptcp_get_ext(skb);
|
||||
data_len = mpext ? mpext->data_len : 0;
|
||||
|
||||
/* we will check ext_copy.data_len in mptcp_write_options() to
|
||||
* discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
|
||||
* TCPOLEN_MPTCP_MPC_ACK
|
||||
*/
|
||||
opts->ext_copy.data_len = data_len;
|
||||
opts->suboptions = OPTION_MPTCP_MPC_ACK;
|
||||
opts->sndr_key = subflow->local_key;
|
||||
opts->rcvr_key = subflow->remote_key;
|
||||
|
||||
/* Section 3.1.
|
||||
* The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
|
||||
* packets that start the first subflow of an MPTCP connection,
|
||||
* as well as the first packet that carries data
|
||||
*/
|
||||
if (data_len > 0)
|
||||
*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
|
||||
else
|
||||
*size = TCPOLEN_MPTCP_MPC_ACK;
|
||||
|
||||
pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
|
||||
subflow, subflow->local_key, subflow->remote_key,
|
||||
data_len);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
|
||||
struct mptcp_ext *ext)
|
||||
{
|
||||
ext->data_fin = 1;
|
||||
|
||||
if (!ext->use_map) {
|
||||
/* RFC6824 requires a DSS mapping with specific values
|
||||
* if DATA_FIN is set but no data payload is mapped
|
||||
*/
|
||||
ext->use_map = 1;
|
||||
ext->dsn64 = 1;
|
||||
ext->data_seq = mptcp_sk(subflow->conn)->write_seq;
|
||||
ext->subflow_seq = 0;
|
||||
ext->data_len = 1;
|
||||
} else {
|
||||
/* If there's an existing DSS mapping, DATA_FIN consumes
|
||||
* 1 additional byte of mapping space.
|
||||
*/
|
||||
ext->data_len++;
|
||||
}
|
||||
}
|
||||
|
||||
static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
|
||||
unsigned int *size,
|
||||
unsigned int remaining,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
unsigned int dss_size = 0;
|
||||
struct mptcp_ext *mpext;
|
||||
struct mptcp_sock *msk;
|
||||
unsigned int ack_size;
|
||||
bool ret = false;
|
||||
u8 tcp_fin;
|
||||
|
||||
if (skb) {
|
||||
mpext = mptcp_get_ext(skb);
|
||||
tcp_fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
|
||||
} else {
|
||||
mpext = NULL;
|
||||
tcp_fin = 0;
|
||||
}
|
||||
|
||||
if (!skb || (mpext && mpext->use_map) || tcp_fin) {
|
||||
unsigned int map_size;
|
||||
|
||||
map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
|
||||
|
||||
remaining -= map_size;
|
||||
dss_size = map_size;
|
||||
if (mpext)
|
||||
opts->ext_copy = *mpext;
|
||||
|
||||
if (skb && tcp_fin &&
|
||||
subflow->conn->sk_state != TCP_ESTABLISHED)
|
||||
mptcp_write_data_fin(subflow, &opts->ext_copy);
|
||||
ret = true;
|
||||
}
|
||||
|
||||
opts->ext_copy.use_ack = 0;
|
||||
msk = mptcp_sk(subflow->conn);
|
||||
if (!msk || !READ_ONCE(msk->can_ack)) {
|
||||
*size = ALIGN(dss_size, 4);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ack_size = TCPOLEN_MPTCP_DSS_ACK64;
|
||||
|
||||
/* Add kind/length/subtype/flag overhead if mapping is not populated */
|
||||
if (dss_size == 0)
|
||||
ack_size += TCPOLEN_MPTCP_DSS_BASE;
|
||||
|
||||
dss_size += ack_size;
|
||||
|
||||
opts->ext_copy.data_ack = msk->ack_seq;
|
||||
opts->ext_copy.ack64 = 1;
|
||||
opts->ext_copy.use_ack = 1;
|
||||
|
||||
*size = ALIGN(dss_size, 4);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
|
||||
unsigned int *size, unsigned int remaining,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
unsigned int opt_size = 0;
|
||||
bool ret = false;
|
||||
|
||||
if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
|
||||
ret = true;
|
||||
else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
|
||||
opts))
|
||||
ret = true;
|
||||
|
||||
/* we reserved enough space for the above options, and exceeding the
|
||||
* TCP option space would be fatal
|
||||
*/
|
||||
if (WARN_ON_ONCE(opt_size > remaining))
|
||||
return false;
|
||||
|
||||
*size += opt_size;
|
||||
remaining -= opt_size;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
|
||||
struct mptcp_out_options *opts)
|
||||
{
|
||||
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
||||
|
||||
if (subflow_req->mp_capable) {
|
||||
opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
|
||||
opts->sndr_key = subflow_req->local_key;
|
||||
*size = TCPOLEN_MPTCP_MPC_SYNACK;
|
||||
pr_debug("subflow_req=%p, local_key=%llu",
|
||||
subflow_req, subflow_req->local_key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool check_fourth_ack(struct mptcp_subflow_context *subflow,
|
||||
struct sk_buff *skb,
|
||||
struct mptcp_options_received *mp_opt)
|
||||
{
|
||||
/* here we can process OoO, in-window pkts, only in-sequence 4th ack
|
||||
* are relevant
|
||||
*/
|
||||
if (likely(subflow->fourth_ack ||
|
||||
TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1))
|
||||
return true;
|
||||
|
||||
if (mp_opt->use_ack)
|
||||
subflow->fourth_ack = 1;
|
||||
|
||||
if (subflow->can_ack)
|
||||
return true;
|
||||
|
||||
/* If the first established packet does not contain MP_CAPABLE + data
|
||||
* then fallback to TCP
|
||||
*/
|
||||
if (!mp_opt->mp_capable) {
|
||||
subflow->mp_capable = 0;
|
||||
tcp_sk(mptcp_subflow_tcp_sock(subflow))->is_mptcp = 0;
|
||||
return false;
|
||||
}
|
||||
subflow->remote_key = mp_opt->sndr_key;
|
||||
subflow->can_ack = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
|
||||
struct tcp_options_received *opt_rx)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct mptcp_options_received *mp_opt;
|
||||
struct mptcp_ext *mpext;
|
||||
|
||||
mp_opt = &opt_rx->mptcp;
|
||||
if (!check_fourth_ack(subflow, skb, mp_opt))
|
||||
return;
|
||||
|
||||
if (!mp_opt->dss)
|
||||
return;
|
||||
|
||||
mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
|
||||
if (!mpext)
|
||||
return;
|
||||
|
||||
memset(mpext, 0, sizeof(*mpext));
|
||||
|
||||
if (mp_opt->use_map) {
|
||||
if (mp_opt->mpc_map) {
|
||||
/* this is an MP_CAPABLE carrying MPTCP data
|
||||
* we know this map the first chunk of data
|
||||
*/
|
||||
mptcp_crypto_key_sha(subflow->remote_key, NULL,
|
||||
&mpext->data_seq);
|
||||
mpext->data_seq++;
|
||||
mpext->subflow_seq = 1;
|
||||
mpext->dsn64 = 1;
|
||||
mpext->mpc_map = 1;
|
||||
} else {
|
||||
mpext->data_seq = mp_opt->data_seq;
|
||||
mpext->subflow_seq = mp_opt->subflow_seq;
|
||||
mpext->dsn64 = mp_opt->dsn64;
|
||||
}
|
||||
mpext->data_len = mp_opt->data_len;
|
||||
mpext->use_map = 1;
|
||||
}
|
||||
|
||||
if (mp_opt->use_ack) {
|
||||
mpext->data_ack = mp_opt->data_ack;
|
||||
mpext->use_ack = 1;
|
||||
mpext->ack64 = mp_opt->ack64;
|
||||
}
|
||||
|
||||
mpext->data_fin = mp_opt->data_fin;
|
||||
}
|
||||
|
||||
void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
|
||||
{
|
||||
if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
|
||||
OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
|
||||
u8 len;
|
||||
|
||||
if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
|
||||
len = TCPOLEN_MPTCP_MPC_SYN;
|
||||
else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
|
||||
len = TCPOLEN_MPTCP_MPC_SYNACK;
|
||||
else if (opts->ext_copy.data_len)
|
||||
len = TCPOLEN_MPTCP_MPC_ACK_DATA;
|
||||
else
|
||||
len = TCPOLEN_MPTCP_MPC_ACK;
|
||||
|
||||
*ptr++ = htonl((TCPOPT_MPTCP << 24) | (len << 16) |
|
||||
(MPTCPOPT_MP_CAPABLE << 12) |
|
||||
(MPTCP_SUPPORTED_VERSION << 8) |
|
||||
MPTCP_CAP_HMAC_SHA256);
|
||||
|
||||
if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
|
||||
opts->suboptions))
|
||||
goto mp_capable_done;
|
||||
|
||||
put_unaligned_be64(opts->sndr_key, ptr);
|
||||
ptr += 2;
|
||||
if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
|
||||
goto mp_capable_done;
|
||||
|
||||
put_unaligned_be64(opts->rcvr_key, ptr);
|
||||
ptr += 2;
|
||||
if (!opts->ext_copy.data_len)
|
||||
goto mp_capable_done;
|
||||
|
||||
put_unaligned_be32(opts->ext_copy.data_len << 16 |
|
||||
TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
|
||||
ptr += 1;
|
||||
}
|
||||
|
||||
mp_capable_done:
|
||||
if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
|
||||
struct mptcp_ext *mpext = &opts->ext_copy;
|
||||
u8 len = TCPOLEN_MPTCP_DSS_BASE;
|
||||
u8 flags = 0;
|
||||
|
||||
if (mpext->use_ack) {
|
||||
len += TCPOLEN_MPTCP_DSS_ACK64;
|
||||
flags = MPTCP_DSS_HAS_ACK | MPTCP_DSS_ACK64;
|
||||
}
|
||||
|
||||
if (mpext->use_map) {
|
||||
len += TCPOLEN_MPTCP_DSS_MAP64;
|
||||
|
||||
/* Use only 64-bit mapping flags for now, add
|
||||
* support for optional 32-bit mappings later.
|
||||
*/
|
||||
flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
|
||||
if (mpext->data_fin)
|
||||
flags |= MPTCP_DSS_DATA_FIN;
|
||||
}
|
||||
|
||||
*ptr++ = htonl((TCPOPT_MPTCP << 24) |
|
||||
(len << 16) |
|
||||
(MPTCPOPT_DSS << 12) |
|
||||
(flags));
|
||||
|
||||
if (mpext->use_ack) {
|
||||
put_unaligned_be64(mpext->data_ack, ptr);
|
||||
ptr += 2;
|
||||
}
|
||||
|
||||
if (mpext->use_map) {
|
||||
put_unaligned_be64(mpext->data_seq, ptr);
|
||||
ptr += 2;
|
||||
put_unaligned_be32(mpext->subflow_seq, ptr);
|
||||
ptr += 1;
|
||||
put_unaligned_be32(mpext->data_len << 16 |
|
||||
TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,240 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/* Multipath TCP
|
||||
*
|
||||
* Copyright (c) 2017 - 2019, Intel Corporation.
|
||||
*/
|
||||
|
||||
#ifndef __MPTCP_PROTOCOL_H
|
||||
#define __MPTCP_PROTOCOL_H
|
||||
|
||||
#include <linux/random.h>
|
||||
#include <net/tcp.h>
|
||||
#include <net/inet_connection_sock.h>
|
||||
|
||||
#define MPTCP_SUPPORTED_VERSION 1
|
||||
|
||||
/* MPTCP option bits */
|
||||
#define OPTION_MPTCP_MPC_SYN BIT(0)
|
||||
#define OPTION_MPTCP_MPC_SYNACK BIT(1)
|
||||
#define OPTION_MPTCP_MPC_ACK BIT(2)
|
||||
|
||||
/* MPTCP option subtypes */
|
||||
#define MPTCPOPT_MP_CAPABLE 0
|
||||
#define MPTCPOPT_MP_JOIN 1
|
||||
#define MPTCPOPT_DSS 2
|
||||
#define MPTCPOPT_ADD_ADDR 3
|
||||
#define MPTCPOPT_RM_ADDR 4
|
||||
#define MPTCPOPT_MP_PRIO 5
|
||||
#define MPTCPOPT_MP_FAIL 6
|
||||
#define MPTCPOPT_MP_FASTCLOSE 7
|
||||
|
||||
/* MPTCP suboption lengths */
|
||||
#define TCPOLEN_MPTCP_MPC_SYN 4
|
||||
#define TCPOLEN_MPTCP_MPC_SYNACK 12
|
||||
#define TCPOLEN_MPTCP_MPC_ACK 20
|
||||
#define TCPOLEN_MPTCP_MPC_ACK_DATA 22
|
||||
#define TCPOLEN_MPTCP_DSS_BASE 4
|
||||
#define TCPOLEN_MPTCP_DSS_ACK32 4
|
||||
#define TCPOLEN_MPTCP_DSS_ACK64 8
|
||||
#define TCPOLEN_MPTCP_DSS_MAP32 10
|
||||
#define TCPOLEN_MPTCP_DSS_MAP64 14
|
||||
#define TCPOLEN_MPTCP_DSS_CHECKSUM 2
|
||||
|
||||
/* MPTCP MP_CAPABLE flags */
|
||||
#define MPTCP_VERSION_MASK (0x0F)
|
||||
#define MPTCP_CAP_CHECKSUM_REQD BIT(7)
|
||||
#define MPTCP_CAP_EXTENSIBILITY BIT(6)
|
||||
#define MPTCP_CAP_HMAC_SHA256 BIT(0)
|
||||
#define MPTCP_CAP_FLAG_MASK (0x3F)
|
||||
|
||||
/* MPTCP DSS flags */
|
||||
#define MPTCP_DSS_DATA_FIN BIT(4)
|
||||
#define MPTCP_DSS_DSN64 BIT(3)
|
||||
#define MPTCP_DSS_HAS_MAP BIT(2)
|
||||
#define MPTCP_DSS_ACK64 BIT(1)
|
||||
#define MPTCP_DSS_HAS_ACK BIT(0)
|
||||
#define MPTCP_DSS_FLAG_MASK (0x1F)
|
||||
|
||||
/* MPTCP socket flags */
|
||||
#define MPTCP_DATA_READY BIT(0)
|
||||
#define MPTCP_SEND_SPACE BIT(1)
|
||||
|
||||
/* MPTCP connection sock */
|
||||
struct mptcp_sock {
|
||||
/* inet_connection_sock must be the first member */
|
||||
struct inet_connection_sock sk;
|
||||
u64 local_key;
|
||||
u64 remote_key;
|
||||
u64 write_seq;
|
||||
u64 ack_seq;
|
||||
u32 token;
|
||||
unsigned long flags;
|
||||
bool can_ack;
|
||||
struct list_head conn_list;
|
||||
struct skb_ext *cached_ext; /* for the next sendmsg */
|
||||
struct socket *subflow; /* outgoing connect/listener/!mp_capable */
|
||||
struct sock *first;
|
||||
};
|
||||
|
||||
#define mptcp_for_each_subflow(__msk, __subflow) \
|
||||
list_for_each_entry(__subflow, &((__msk)->conn_list), node)
|
||||
|
||||
static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
|
||||
{
|
||||
return (struct mptcp_sock *)sk;
|
||||
}
|
||||
|
||||
struct mptcp_subflow_request_sock {
|
||||
struct tcp_request_sock sk;
|
||||
u16 mp_capable : 1,
|
||||
mp_join : 1,
|
||||
backup : 1,
|
||||
remote_key_valid : 1;
|
||||
u64 local_key;
|
||||
u64 remote_key;
|
||||
u64 idsn;
|
||||
u32 token;
|
||||
u32 ssn_offset;
|
||||
};
|
||||
|
||||
static inline struct mptcp_subflow_request_sock *
|
||||
mptcp_subflow_rsk(const struct request_sock *rsk)
|
||||
{
|
||||
return (struct mptcp_subflow_request_sock *)rsk;
|
||||
}
|
||||
|
||||
/* MPTCP subflow context */
|
||||
struct mptcp_subflow_context {
|
||||
struct list_head node;/* conn_list of subflows */
|
||||
u64 local_key;
|
||||
u64 remote_key;
|
||||
u64 idsn;
|
||||
u64 map_seq;
|
||||
u32 snd_isn;
|
||||
u32 token;
|
||||
u32 rel_write_seq;
|
||||
u32 map_subflow_seq;
|
||||
u32 ssn_offset;
|
||||
u32 map_data_len;
|
||||
u32 request_mptcp : 1, /* send MP_CAPABLE */
|
||||
mp_capable : 1, /* remote is MPTCP capable */
|
||||
fourth_ack : 1, /* send initial DSS */
|
||||
conn_finished : 1,
|
||||
map_valid : 1,
|
||||
mpc_map : 1,
|
||||
data_avail : 1,
|
||||
rx_eof : 1,
|
||||
can_ack : 1; /* only after processing the remote a key */
|
||||
|
||||
struct sock *tcp_sock; /* tcp sk backpointer */
|
||||
struct sock *conn; /* parent mptcp_sock */
|
||||
const struct inet_connection_sock_af_ops *icsk_af_ops;
|
||||
void (*tcp_data_ready)(struct sock *sk);
|
||||
void (*tcp_state_change)(struct sock *sk);
|
||||
void (*tcp_write_space)(struct sock *sk);
|
||||
|
||||
struct rcu_head rcu;
|
||||
};
|
||||
|
||||
static inline struct mptcp_subflow_context *
|
||||
mptcp_subflow_ctx(const struct sock *sk)
|
||||
{
|
||||
struct inet_connection_sock *icsk = inet_csk(sk);
|
||||
|
||||
/* Use RCU on icsk_ulp_data only for sock diag code */
|
||||
return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
|
||||
}
|
||||
|
||||
static inline struct sock *
|
||||
mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
|
||||
{
|
||||
return subflow->tcp_sock;
|
||||
}
|
||||
|
||||
static inline u64
|
||||
mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
|
||||
{
|
||||
return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
|
||||
subflow->ssn_offset -
|
||||
subflow->map_subflow_seq;
|
||||
}
|
||||
|
||||
static inline u64
|
||||
mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
|
||||
{
|
||||
return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
|
||||
}
|
||||
|
||||
int mptcp_is_enabled(struct net *net);
|
||||
bool mptcp_subflow_data_available(struct sock *sk);
|
||||
void mptcp_subflow_init(void);
|
||||
int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
|
||||
|
||||
static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
|
||||
struct mptcp_subflow_context *ctx)
|
||||
{
|
||||
sk->sk_data_ready = ctx->tcp_data_ready;
|
||||
sk->sk_state_change = ctx->tcp_state_change;
|
||||
sk->sk_write_space = ctx->tcp_write_space;
|
||||
|
||||
inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
|
||||
}
|
||||
|
||||
extern const struct inet_connection_sock_af_ops ipv4_specific;
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
extern const struct inet_connection_sock_af_ops ipv6_specific;
|
||||
#endif
|
||||
|
||||
void mptcp_proto_init(void);
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
int mptcp_proto_v6_init(void);
|
||||
#endif
|
||||
|
||||
struct mptcp_read_arg {
|
||||
struct msghdr *msg;
|
||||
};
|
||||
|
||||
int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
|
||||
unsigned int offset, size_t len);
|
||||
|
||||
void mptcp_get_options(const struct sk_buff *skb,
|
||||
struct tcp_options_received *opt_rx);
|
||||
|
||||
void mptcp_finish_connect(struct sock *sk);
|
||||
|
||||
int mptcp_token_new_request(struct request_sock *req);
|
||||
void mptcp_token_destroy_request(u32 token);
|
||||
int mptcp_token_new_connect(struct sock *sk);
|
||||
int mptcp_token_new_accept(u32 token);
|
||||
void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
|
||||
void mptcp_token_destroy(u32 token);
|
||||
|
||||
void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
|
||||
static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
|
||||
{
|
||||
/* we might consider a faster version that computes the key as a
|
||||
* hash of some information available in the MPTCP socket. Use
|
||||
* random data at the moment, as it's probably the safest option
|
||||
* in case multiple sockets are opened in different namespaces at
|
||||
* the same time.
|
||||
*/
|
||||
get_random_bytes(key, sizeof(u64));
|
||||
mptcp_crypto_key_sha(*key, token, idsn);
|
||||
}
|
||||
|
||||
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
|
||||
void *hash_out);
|
||||
|
||||
static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
|
||||
{
|
||||
return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
|
||||
}
|
||||
|
||||
static inline bool before64(__u64 seq1, __u64 seq2)
|
||||
{
|
||||
return (__s64)(seq1 - seq2) < 0;
|
||||
}
|
||||
|
||||
#define after64(seq2, seq1) before64(seq1, seq2)
|
||||
|
||||
#endif /* __MPTCP_PROTOCOL_H */
|
|
@ -0,0 +1,860 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP
|
||||
*
|
||||
* Copyright (c) 2017 - 2019, Intel Corporation.
|
||||
*/
|
||||
|
||||
#define pr_fmt(fmt) "MPTCP: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/netdevice.h>
|
||||
#include <net/sock.h>
|
||||
#include <net/inet_common.h>
|
||||
#include <net/inet_hashtables.h>
|
||||
#include <net/protocol.h>
|
||||
#include <net/tcp.h>
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
#include <net/ip6_route.h>
|
||||
#endif
|
||||
#include <net/mptcp.h>
|
||||
#include "protocol.h"
|
||||
|
||||
static int subflow_rebuild_header(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
int err = 0;
|
||||
|
||||
if (subflow->request_mptcp && !subflow->token) {
|
||||
pr_debug("subflow=%p", sk);
|
||||
err = mptcp_token_new_connect(sk);
|
||||
}
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return subflow->icsk_af_ops->rebuild_header(sk);
|
||||
}
|
||||
|
||||
static void subflow_req_destructor(struct request_sock *req)
|
||||
{
|
||||
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
||||
|
||||
pr_debug("subflow_req=%p", subflow_req);
|
||||
|
||||
if (subflow_req->mp_capable)
|
||||
mptcp_token_destroy_request(subflow_req->token);
|
||||
tcp_request_sock_ops.destructor(req);
|
||||
}
|
||||
|
||||
static void subflow_init_req(struct request_sock *req,
|
||||
const struct sock *sk_listener,
|
||||
struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
|
||||
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
||||
struct tcp_options_received rx_opt;
|
||||
|
||||
pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
|
||||
|
||||
memset(&rx_opt.mptcp, 0, sizeof(rx_opt.mptcp));
|
||||
mptcp_get_options(skb, &rx_opt);
|
||||
|
||||
subflow_req->mp_capable = 0;
|
||||
subflow_req->remote_key_valid = 0;
|
||||
|
||||
#ifdef CONFIG_TCP_MD5SIG
|
||||
/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
|
||||
* TCP option space.
|
||||
*/
|
||||
if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
|
||||
return;
|
||||
#endif
|
||||
|
||||
if (rx_opt.mptcp.mp_capable && listener->request_mptcp) {
|
||||
int err;
|
||||
|
||||
err = mptcp_token_new_request(req);
|
||||
if (err == 0)
|
||||
subflow_req->mp_capable = 1;
|
||||
|
||||
subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
|
||||
}
|
||||
}
|
||||
|
||||
static void subflow_v4_init_req(struct request_sock *req,
|
||||
const struct sock *sk_listener,
|
||||
struct sk_buff *skb)
|
||||
{
|
||||
tcp_rsk(req)->is_mptcp = 1;
|
||||
|
||||
tcp_request_sock_ipv4_ops.init_req(req, sk_listener, skb);
|
||||
|
||||
subflow_init_req(req, sk_listener, skb);
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
static void subflow_v6_init_req(struct request_sock *req,
|
||||
const struct sock *sk_listener,
|
||||
struct sk_buff *skb)
|
||||
{
|
||||
tcp_rsk(req)->is_mptcp = 1;
|
||||
|
||||
tcp_request_sock_ipv6_ops.init_req(req, sk_listener, skb);
|
||||
|
||||
subflow_init_req(req, sk_listener, skb);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
|
||||
subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
|
||||
|
||||
if (subflow->conn && !subflow->conn_finished) {
|
||||
pr_debug("subflow=%p, remote_key=%llu", mptcp_subflow_ctx(sk),
|
||||
subflow->remote_key);
|
||||
mptcp_finish_connect(sk);
|
||||
subflow->conn_finished = 1;
|
||||
|
||||
if (skb) {
|
||||
pr_debug("synack seq=%u", TCP_SKB_CB(skb)->seq);
|
||||
subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static struct request_sock_ops subflow_request_sock_ops;
|
||||
static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
|
||||
|
||||
static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
|
||||
pr_debug("subflow=%p", subflow);
|
||||
|
||||
/* Never answer to SYNs sent to broadcast or multicast */
|
||||
if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
|
||||
goto drop;
|
||||
|
||||
return tcp_conn_request(&subflow_request_sock_ops,
|
||||
&subflow_request_sock_ipv4_ops,
|
||||
sk, skb);
|
||||
drop:
|
||||
tcp_listendrop(sk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
|
||||
static struct inet_connection_sock_af_ops subflow_v6_specific;
|
||||
static struct inet_connection_sock_af_ops subflow_v6m_specific;
|
||||
|
||||
static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
|
||||
pr_debug("subflow=%p", subflow);
|
||||
|
||||
if (skb->protocol == htons(ETH_P_IP))
|
||||
return subflow_v4_conn_request(sk, skb);
|
||||
|
||||
if (!ipv6_unicast_destination(skb))
|
||||
goto drop;
|
||||
|
||||
return tcp_conn_request(&subflow_request_sock_ops,
|
||||
&subflow_request_sock_ipv6_ops, sk, skb);
|
||||
|
||||
drop:
|
||||
tcp_listendrop(sk);
|
||||
return 0; /* don't send reset */
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct sock *subflow_syn_recv_sock(const struct sock *sk,
|
||||
struct sk_buff *skb,
|
||||
struct request_sock *req,
|
||||
struct dst_entry *dst,
|
||||
struct request_sock *req_unhash,
|
||||
bool *own_req)
|
||||
{
|
||||
struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
|
||||
struct mptcp_subflow_request_sock *subflow_req;
|
||||
struct tcp_options_received opt_rx;
|
||||
struct sock *child;
|
||||
|
||||
pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
|
||||
|
||||
/* if the sk is MP_CAPABLE, we try to fetch the client key */
|
||||
subflow_req = mptcp_subflow_rsk(req);
|
||||
if (subflow_req->mp_capable) {
|
||||
if (TCP_SKB_CB(skb)->seq != subflow_req->ssn_offset + 1) {
|
||||
/* here we can receive and accept an in-window,
|
||||
* out-of-order pkt, which will not carry the MP_CAPABLE
|
||||
* opt even on mptcp enabled paths
|
||||
*/
|
||||
goto create_child;
|
||||
}
|
||||
|
||||
opt_rx.mptcp.mp_capable = 0;
|
||||
mptcp_get_options(skb, &opt_rx);
|
||||
if (opt_rx.mptcp.mp_capable) {
|
||||
subflow_req->remote_key = opt_rx.mptcp.sndr_key;
|
||||
subflow_req->remote_key_valid = 1;
|
||||
} else {
|
||||
subflow_req->mp_capable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
create_child:
|
||||
child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
|
||||
req_unhash, own_req);
|
||||
|
||||
if (child && *own_req) {
|
||||
struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
|
||||
|
||||
/* we have null ctx on TCP fallback, not fatal on MPC
|
||||
* handshake
|
||||
*/
|
||||
if (!ctx)
|
||||
return child;
|
||||
|
||||
if (ctx->mp_capable) {
|
||||
if (mptcp_token_new_accept(ctx->token))
|
||||
goto close_child;
|
||||
}
|
||||
}
|
||||
|
||||
return child;
|
||||
|
||||
close_child:
|
||||
pr_debug("closing child socket");
|
||||
tcp_send_active_reset(child, GFP_ATOMIC);
|
||||
inet_csk_prepare_forced_close(child);
|
||||
tcp_done(child);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct inet_connection_sock_af_ops subflow_specific;
|
||||
|
||||
enum mapping_status {
|
||||
MAPPING_OK,
|
||||
MAPPING_INVALID,
|
||||
MAPPING_EMPTY,
|
||||
MAPPING_DATA_FIN
|
||||
};
|
||||
|
||||
static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
|
||||
{
|
||||
if ((u32)seq == (u32)old_seq)
|
||||
return old_seq;
|
||||
|
||||
/* Assume map covers data not mapped yet. */
|
||||
return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
|
||||
}
|
||||
|
||||
static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
|
||||
{
|
||||
WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
|
||||
ssn, subflow->map_subflow_seq, subflow->map_data_len);
|
||||
}
|
||||
|
||||
static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
|
||||
unsigned int skb_consumed;
|
||||
|
||||
skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
|
||||
if (WARN_ON_ONCE(skb_consumed >= skb->len))
|
||||
return true;
|
||||
|
||||
return skb->len - skb_consumed <= subflow->map_data_len -
|
||||
mptcp_subflow_get_map_offset(subflow);
|
||||
}
|
||||
|
||||
static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
|
||||
u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
|
||||
|
||||
if (unlikely(before(ssn, subflow->map_subflow_seq))) {
|
||||
/* Mapping covers data later in the subflow stream,
|
||||
* currently unsupported.
|
||||
*/
|
||||
warn_bad_map(subflow, ssn);
|
||||
return false;
|
||||
}
|
||||
if (unlikely(!before(ssn, subflow->map_subflow_seq +
|
||||
subflow->map_data_len))) {
|
||||
/* Mapping does covers past subflow data, invalid */
|
||||
warn_bad_map(subflow, ssn + skb->len);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static enum mapping_status get_mapping_status(struct sock *ssk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
|
||||
struct mptcp_ext *mpext;
|
||||
struct sk_buff *skb;
|
||||
u16 data_len;
|
||||
u64 map_seq;
|
||||
|
||||
skb = skb_peek(&ssk->sk_receive_queue);
|
||||
if (!skb)
|
||||
return MAPPING_EMPTY;
|
||||
|
||||
mpext = mptcp_get_ext(skb);
|
||||
if (!mpext || !mpext->use_map) {
|
||||
if (!subflow->map_valid && !skb->len) {
|
||||
/* the TCP stack deliver 0 len FIN pkt to the receive
|
||||
* queue, that is the only 0len pkts ever expected here,
|
||||
* and we can admit no mapping only for 0 len pkts
|
||||
*/
|
||||
if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
|
||||
WARN_ONCE(1, "0len seq %d:%d flags %x",
|
||||
TCP_SKB_CB(skb)->seq,
|
||||
TCP_SKB_CB(skb)->end_seq,
|
||||
TCP_SKB_CB(skb)->tcp_flags);
|
||||
sk_eat_skb(ssk, skb);
|
||||
return MAPPING_EMPTY;
|
||||
}
|
||||
|
||||
if (!subflow->map_valid)
|
||||
return MAPPING_INVALID;
|
||||
|
||||
goto validate_seq;
|
||||
}
|
||||
|
||||
pr_debug("seq=%llu is64=%d ssn=%u data_len=%u data_fin=%d",
|
||||
mpext->data_seq, mpext->dsn64, mpext->subflow_seq,
|
||||
mpext->data_len, mpext->data_fin);
|
||||
|
||||
data_len = mpext->data_len;
|
||||
if (data_len == 0) {
|
||||
pr_err("Infinite mapping not handled");
|
||||
return MAPPING_INVALID;
|
||||
}
|
||||
|
||||
if (mpext->data_fin == 1) {
|
||||
if (data_len == 1) {
|
||||
pr_debug("DATA_FIN with no payload");
|
||||
if (subflow->map_valid) {
|
||||
/* A DATA_FIN might arrive in a DSS
|
||||
* option before the previous mapping
|
||||
* has been fully consumed. Continue
|
||||
* handling the existing mapping.
|
||||
*/
|
||||
skb_ext_del(skb, SKB_EXT_MPTCP);
|
||||
return MAPPING_OK;
|
||||
} else {
|
||||
return MAPPING_DATA_FIN;
|
||||
}
|
||||
}
|
||||
|
||||
/* Adjust for DATA_FIN using 1 byte of sequence space */
|
||||
data_len--;
|
||||
}
|
||||
|
||||
if (!mpext->dsn64) {
|
||||
map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
|
||||
mpext->data_seq);
|
||||
pr_debug("expanded seq=%llu", subflow->map_seq);
|
||||
} else {
|
||||
map_seq = mpext->data_seq;
|
||||
}
|
||||
|
||||
if (subflow->map_valid) {
|
||||
/* Allow replacing only with an identical map */
|
||||
if (subflow->map_seq == map_seq &&
|
||||
subflow->map_subflow_seq == mpext->subflow_seq &&
|
||||
subflow->map_data_len == data_len) {
|
||||
skb_ext_del(skb, SKB_EXT_MPTCP);
|
||||
return MAPPING_OK;
|
||||
}
|
||||
|
||||
/* If this skb data are fully covered by the current mapping,
|
||||
* the new map would need caching, which is not supported
|
||||
*/
|
||||
if (skb_is_fully_mapped(ssk, skb))
|
||||
return MAPPING_INVALID;
|
||||
|
||||
/* will validate the next map after consuming the current one */
|
||||
return MAPPING_OK;
|
||||
}
|
||||
|
||||
subflow->map_seq = map_seq;
|
||||
subflow->map_subflow_seq = mpext->subflow_seq;
|
||||
subflow->map_data_len = data_len;
|
||||
subflow->map_valid = 1;
|
||||
subflow->mpc_map = mpext->mpc_map;
|
||||
pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
|
||||
subflow->map_seq, subflow->map_subflow_seq,
|
||||
subflow->map_data_len);
|
||||
|
||||
validate_seq:
|
||||
/* we revalidate valid mapping on new skb, because we must ensure
|
||||
* the current skb is completely covered by the available mapping
|
||||
*/
|
||||
if (!validate_mapping(ssk, skb))
|
||||
return MAPPING_INVALID;
|
||||
|
||||
skb_ext_del(skb, SKB_EXT_MPTCP);
|
||||
return MAPPING_OK;
|
||||
}
|
||||
|
||||
static bool subflow_check_data_avail(struct sock *ssk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
|
||||
enum mapping_status status;
|
||||
struct mptcp_sock *msk;
|
||||
struct sk_buff *skb;
|
||||
|
||||
pr_debug("msk=%p ssk=%p data_avail=%d skb=%p", subflow->conn, ssk,
|
||||
subflow->data_avail, skb_peek(&ssk->sk_receive_queue));
|
||||
if (subflow->data_avail)
|
||||
return true;
|
||||
|
||||
if (!subflow->conn)
|
||||
return false;
|
||||
|
||||
msk = mptcp_sk(subflow->conn);
|
||||
for (;;) {
|
||||
u32 map_remaining;
|
||||
size_t delta;
|
||||
u64 ack_seq;
|
||||
u64 old_ack;
|
||||
|
||||
status = get_mapping_status(ssk);
|
||||
pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
|
||||
if (status == MAPPING_INVALID) {
|
||||
ssk->sk_err = EBADMSG;
|
||||
goto fatal;
|
||||
}
|
||||
|
||||
if (status != MAPPING_OK)
|
||||
return false;
|
||||
|
||||
skb = skb_peek(&ssk->sk_receive_queue);
|
||||
if (WARN_ON_ONCE(!skb))
|
||||
return false;
|
||||
|
||||
/* if msk lacks the remote key, this subflow must provide an
|
||||
* MP_CAPABLE-based mapping
|
||||
*/
|
||||
if (unlikely(!READ_ONCE(msk->can_ack))) {
|
||||
if (!subflow->mpc_map) {
|
||||
ssk->sk_err = EBADMSG;
|
||||
goto fatal;
|
||||
}
|
||||
WRITE_ONCE(msk->remote_key, subflow->remote_key);
|
||||
WRITE_ONCE(msk->ack_seq, subflow->map_seq);
|
||||
WRITE_ONCE(msk->can_ack, true);
|
||||
}
|
||||
|
||||
old_ack = READ_ONCE(msk->ack_seq);
|
||||
ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
|
||||
pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
|
||||
ack_seq);
|
||||
if (ack_seq == old_ack)
|
||||
break;
|
||||
|
||||
/* only accept in-sequence mapping. Old values are spurious
|
||||
* retransmission; we can hit "future" values on active backup
|
||||
* subflow switch, we relay on retransmissions to get
|
||||
* in-sequence data.
|
||||
* Cuncurrent subflows support will require subflow data
|
||||
* reordering
|
||||
*/
|
||||
map_remaining = subflow->map_data_len -
|
||||
mptcp_subflow_get_map_offset(subflow);
|
||||
if (before64(ack_seq, old_ack))
|
||||
delta = min_t(size_t, old_ack - ack_seq, map_remaining);
|
||||
else
|
||||
delta = min_t(size_t, ack_seq - old_ack, map_remaining);
|
||||
|
||||
/* discard mapped data */
|
||||
pr_debug("discarding %zu bytes, current map len=%d", delta,
|
||||
map_remaining);
|
||||
if (delta) {
|
||||
struct mptcp_read_arg arg = {
|
||||
.msg = NULL,
|
||||
};
|
||||
read_descriptor_t desc = {
|
||||
.count = delta,
|
||||
.arg.data = &arg,
|
||||
};
|
||||
int ret;
|
||||
|
||||
ret = tcp_read_sock(ssk, &desc, mptcp_read_actor);
|
||||
if (ret < 0) {
|
||||
ssk->sk_err = -ret;
|
||||
goto fatal;
|
||||
}
|
||||
if (ret < delta)
|
||||
return false;
|
||||
if (delta == map_remaining)
|
||||
subflow->map_valid = 0;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
fatal:
|
||||
/* fatal protocol error, close the socket */
|
||||
/* This barrier is coupled with smp_rmb() in tcp_poll() */
|
||||
smp_wmb();
|
||||
ssk->sk_error_report(ssk);
|
||||
tcp_set_state(ssk, TCP_CLOSE);
|
||||
tcp_send_active_reset(ssk, GFP_ATOMIC);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool mptcp_subflow_data_available(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct sk_buff *skb;
|
||||
|
||||
/* check if current mapping is still valid */
|
||||
if (subflow->map_valid &&
|
||||
mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
|
||||
subflow->map_valid = 0;
|
||||
subflow->data_avail = 0;
|
||||
|
||||
pr_debug("Done with mapping: seq=%u data_len=%u",
|
||||
subflow->map_subflow_seq,
|
||||
subflow->map_data_len);
|
||||
}
|
||||
|
||||
if (!subflow_check_data_avail(sk)) {
|
||||
subflow->data_avail = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
skb = skb_peek(&sk->sk_receive_queue);
|
||||
subflow->data_avail = skb &&
|
||||
before(tcp_sk(sk)->copied_seq, TCP_SKB_CB(skb)->end_seq);
|
||||
return subflow->data_avail;
|
||||
}
|
||||
|
||||
static void subflow_data_ready(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct sock *parent = subflow->conn;
|
||||
|
||||
if (!parent || !subflow->mp_capable) {
|
||||
subflow->tcp_data_ready(sk);
|
||||
|
||||
if (parent)
|
||||
parent->sk_data_ready(parent);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mptcp_subflow_data_available(sk)) {
|
||||
set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
|
||||
|
||||
parent->sk_data_ready(parent);
|
||||
}
|
||||
}
|
||||
|
||||
static void subflow_write_space(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct sock *parent = subflow->conn;
|
||||
|
||||
sk_stream_write_space(sk);
|
||||
if (parent && sk_stream_is_writeable(sk)) {
|
||||
set_bit(MPTCP_SEND_SPACE, &mptcp_sk(parent)->flags);
|
||||
smp_mb__after_atomic();
|
||||
/* set SEND_SPACE before sk_stream_write_space clears NOSPACE */
|
||||
sk_stream_write_space(parent);
|
||||
}
|
||||
}
|
||||
|
||||
static struct inet_connection_sock_af_ops *
|
||||
subflow_default_af_ops(struct sock *sk)
|
||||
{
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
if (sk->sk_family == AF_INET6)
|
||||
return &subflow_v6_specific;
|
||||
#endif
|
||||
return &subflow_specific;
|
||||
}
|
||||
|
||||
void mptcp_handle_ipv6_mapped(struct sock *sk, bool mapped)
|
||||
{
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct inet_connection_sock *icsk = inet_csk(sk);
|
||||
struct inet_connection_sock_af_ops *target;
|
||||
|
||||
target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
|
||||
|
||||
pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
|
||||
subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
|
||||
|
||||
if (likely(icsk->icsk_af_ops == target))
|
||||
return;
|
||||
|
||||
subflow->icsk_af_ops = icsk->icsk_af_ops;
|
||||
icsk->icsk_af_ops = target;
|
||||
#endif
|
||||
}
|
||||
|
||||
int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow;
|
||||
struct net *net = sock_net(sk);
|
||||
struct socket *sf;
|
||||
int err;
|
||||
|
||||
err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
|
||||
&sf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
lock_sock(sf->sk);
|
||||
|
||||
/* kernel sockets do not by default acquire net ref, but TCP timer
|
||||
* needs it.
|
||||
*/
|
||||
sf->sk->sk_net_refcnt = 1;
|
||||
get_net(net);
|
||||
this_cpu_add(*net->core.sock_inuse, 1);
|
||||
err = tcp_set_ulp(sf->sk, "mptcp");
|
||||
release_sock(sf->sk);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
subflow = mptcp_subflow_ctx(sf->sk);
|
||||
pr_debug("subflow=%p", subflow);
|
||||
|
||||
*new_sock = sf;
|
||||
sock_hold(sk);
|
||||
subflow->conn = sk;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
|
||||
gfp_t priority)
|
||||
{
|
||||
struct inet_connection_sock *icsk = inet_csk(sk);
|
||||
struct mptcp_subflow_context *ctx;
|
||||
|
||||
ctx = kzalloc(sizeof(*ctx), priority);
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
|
||||
INIT_LIST_HEAD(&ctx->node);
|
||||
|
||||
pr_debug("subflow=%p", ctx);
|
||||
|
||||
ctx->tcp_sock = sk;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void __subflow_state_change(struct sock *sk)
|
||||
{
|
||||
struct socket_wq *wq;
|
||||
|
||||
rcu_read_lock();
|
||||
wq = rcu_dereference(sk->sk_wq);
|
||||
if (skwq_has_sleeper(wq))
|
||||
wake_up_interruptible_all(&wq->wait);
|
||||
rcu_read_unlock();
|
||||
}
|
||||
|
||||
static bool subflow_is_done(const struct sock *sk)
|
||||
{
|
||||
return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
|
||||
}
|
||||
|
||||
static void subflow_state_change(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct sock *parent = READ_ONCE(subflow->conn);
|
||||
|
||||
__subflow_state_change(sk);
|
||||
|
||||
/* as recvmsg() does not acquire the subflow socket for ssk selection
|
||||
* a fin packet carrying a DSS can be unnoticed if we don't trigger
|
||||
* the data available machinery here.
|
||||
*/
|
||||
if (parent && subflow->mp_capable && mptcp_subflow_data_available(sk)) {
|
||||
set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
|
||||
|
||||
parent->sk_data_ready(parent);
|
||||
}
|
||||
|
||||
if (parent && !(parent->sk_shutdown & RCV_SHUTDOWN) &&
|
||||
!subflow->rx_eof && subflow_is_done(sk)) {
|
||||
subflow->rx_eof = 1;
|
||||
parent->sk_shutdown |= RCV_SHUTDOWN;
|
||||
__subflow_state_change(parent);
|
||||
}
|
||||
}
|
||||
|
||||
static int subflow_ulp_init(struct sock *sk)
|
||||
{
|
||||
struct inet_connection_sock *icsk = inet_csk(sk);
|
||||
struct mptcp_subflow_context *ctx;
|
||||
struct tcp_sock *tp = tcp_sk(sk);
|
||||
int err = 0;
|
||||
|
||||
/* disallow attaching ULP to a socket unless it has been
|
||||
* created with sock_create_kern()
|
||||
*/
|
||||
if (!sk->sk_kern_sock) {
|
||||
err = -EOPNOTSUPP;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ctx = subflow_create_ctx(sk, GFP_KERNEL);
|
||||
if (!ctx) {
|
||||
err = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
|
||||
|
||||
tp->is_mptcp = 1;
|
||||
ctx->icsk_af_ops = icsk->icsk_af_ops;
|
||||
icsk->icsk_af_ops = subflow_default_af_ops(sk);
|
||||
ctx->tcp_data_ready = sk->sk_data_ready;
|
||||
ctx->tcp_state_change = sk->sk_state_change;
|
||||
ctx->tcp_write_space = sk->sk_write_space;
|
||||
sk->sk_data_ready = subflow_data_ready;
|
||||
sk->sk_write_space = subflow_write_space;
|
||||
sk->sk_state_change = subflow_state_change;
|
||||
out:
|
||||
return err;
|
||||
}
|
||||
|
||||
static void subflow_ulp_release(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
|
||||
|
||||
if (!ctx)
|
||||
return;
|
||||
|
||||
if (ctx->conn)
|
||||
sock_put(ctx->conn);
|
||||
|
||||
kfree_rcu(ctx, rcu);
|
||||
}
|
||||
|
||||
static void subflow_ulp_fallback(struct sock *sk,
|
||||
struct mptcp_subflow_context *old_ctx)
|
||||
{
|
||||
struct inet_connection_sock *icsk = inet_csk(sk);
|
||||
|
||||
mptcp_subflow_tcp_fallback(sk, old_ctx);
|
||||
icsk->icsk_ulp_ops = NULL;
|
||||
rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
|
||||
tcp_sk(sk)->is_mptcp = 0;
|
||||
}
|
||||
|
||||
static void subflow_ulp_clone(const struct request_sock *req,
|
||||
struct sock *newsk,
|
||||
const gfp_t priority)
|
||||
{
|
||||
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
||||
struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
|
||||
struct mptcp_subflow_context *new_ctx;
|
||||
|
||||
if (!subflow_req->mp_capable) {
|
||||
subflow_ulp_fallback(newsk, old_ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
new_ctx = subflow_create_ctx(newsk, priority);
|
||||
if (new_ctx == NULL) {
|
||||
subflow_ulp_fallback(newsk, old_ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
/* see comments in subflow_syn_recv_sock(), MPTCP connection is fully
|
||||
* established only after we receive the remote key
|
||||
*/
|
||||
new_ctx->conn_finished = 1;
|
||||
new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
|
||||
new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
|
||||
new_ctx->tcp_state_change = old_ctx->tcp_state_change;
|
||||
new_ctx->tcp_write_space = old_ctx->tcp_write_space;
|
||||
new_ctx->mp_capable = 1;
|
||||
new_ctx->fourth_ack = subflow_req->remote_key_valid;
|
||||
new_ctx->can_ack = subflow_req->remote_key_valid;
|
||||
new_ctx->remote_key = subflow_req->remote_key;
|
||||
new_ctx->local_key = subflow_req->local_key;
|
||||
new_ctx->token = subflow_req->token;
|
||||
new_ctx->ssn_offset = subflow_req->ssn_offset;
|
||||
new_ctx->idsn = subflow_req->idsn;
|
||||
}
|
||||
|
||||
static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
|
||||
.name = "mptcp",
|
||||
.owner = THIS_MODULE,
|
||||
.init = subflow_ulp_init,
|
||||
.release = subflow_ulp_release,
|
||||
.clone = subflow_ulp_clone,
|
||||
};
|
||||
|
||||
static int subflow_ops_init(struct request_sock_ops *subflow_ops)
|
||||
{
|
||||
subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
|
||||
subflow_ops->slab_name = "request_sock_subflow";
|
||||
|
||||
subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
|
||||
subflow_ops->obj_size, 0,
|
||||
SLAB_ACCOUNT |
|
||||
SLAB_TYPESAFE_BY_RCU,
|
||||
NULL);
|
||||
if (!subflow_ops->slab)
|
||||
return -ENOMEM;
|
||||
|
||||
subflow_ops->destructor = subflow_req_destructor;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mptcp_subflow_init(void)
|
||||
{
|
||||
subflow_request_sock_ops = tcp_request_sock_ops;
|
||||
if (subflow_ops_init(&subflow_request_sock_ops) != 0)
|
||||
panic("MPTCP: failed to init subflow request sock ops\n");
|
||||
|
||||
subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
|
||||
subflow_request_sock_ipv4_ops.init_req = subflow_v4_init_req;
|
||||
|
||||
subflow_specific = ipv4_specific;
|
||||
subflow_specific.conn_request = subflow_v4_conn_request;
|
||||
subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
|
||||
subflow_specific.sk_rx_dst_set = subflow_finish_connect;
|
||||
subflow_specific.rebuild_header = subflow_rebuild_header;
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
|
||||
subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
|
||||
subflow_request_sock_ipv6_ops.init_req = subflow_v6_init_req;
|
||||
|
||||
subflow_v6_specific = ipv6_specific;
|
||||
subflow_v6_specific.conn_request = subflow_v6_conn_request;
|
||||
subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
|
||||
subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
|
||||
subflow_v6_specific.rebuild_header = subflow_rebuild_header;
|
||||
|
||||
subflow_v6m_specific = subflow_v6_specific;
|
||||
subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
|
||||
subflow_v6m_specific.send_check = ipv4_specific.send_check;
|
||||
subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
|
||||
subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
|
||||
subflow_v6m_specific.net_frag_header_len = 0;
|
||||
#endif
|
||||
|
||||
if (tcp_register_ulp(&subflow_ulp_ops) != 0)
|
||||
panic("MPTCP: failed to register subflows to ULP\n");
|
||||
}
|
|
@ -0,0 +1,195 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP token management
|
||||
* Copyright (c) 2017 - 2019, Intel Corporation.
|
||||
*
|
||||
* Note: This code is based on mptcp_ctrl.c from multipath-tcp.org,
|
||||
* authored by:
|
||||
*
|
||||
* Sébastien Barré <sebastien.barre@uclouvain.be>
|
||||
* Christoph Paasch <christoph.paasch@uclouvain.be>
|
||||
* Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
|
||||
* Gregory Detal <gregory.detal@uclouvain.be>
|
||||
* Fabien Duchêne <fabien.duchene@uclouvain.be>
|
||||
* Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
|
||||
* Lavkesh Lahngir <lavkesh51@gmail.com>
|
||||
* Andreas Ripke <ripke@neclab.eu>
|
||||
* Vlad Dogaru <vlad.dogaru@intel.com>
|
||||
* Octavian Purdila <octavian.purdila@intel.com>
|
||||
* John Ronan <jronan@tssg.org>
|
||||
* Catalin Nicutar <catalin.nicutar@gmail.com>
|
||||
* Brandon Heller <brandonh@stanford.edu>
|
||||
*/
|
||||
|
||||
#define pr_fmt(fmt) "MPTCP: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/radix-tree.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <net/sock.h>
|
||||
#include <net/inet_common.h>
|
||||
#include <net/protocol.h>
|
||||
#include <net/mptcp.h>
|
||||
#include "protocol.h"
|
||||
|
||||
static RADIX_TREE(token_tree, GFP_ATOMIC);
|
||||
static RADIX_TREE(token_req_tree, GFP_ATOMIC);
|
||||
static DEFINE_SPINLOCK(token_tree_lock);
|
||||
static int token_used __read_mostly;
|
||||
|
||||
/**
|
||||
* mptcp_token_new_request - create new key/idsn/token for subflow_request
|
||||
* @req - the request socket
|
||||
*
|
||||
* This function is called when a new mptcp connection is coming in.
|
||||
*
|
||||
* It creates a unique token to identify the new mptcp connection,
|
||||
* a secret local key and the initial data sequence number (idsn).
|
||||
*
|
||||
* Returns 0 on success.
|
||||
*/
|
||||
int mptcp_token_new_request(struct request_sock *req)
|
||||
{
|
||||
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
||||
int err;
|
||||
|
||||
while (1) {
|
||||
u32 token;
|
||||
|
||||
mptcp_crypto_key_gen_sha(&subflow_req->local_key,
|
||||
&subflow_req->token,
|
||||
&subflow_req->idsn);
|
||||
pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n",
|
||||
req, subflow_req->local_key, subflow_req->token,
|
||||
subflow_req->idsn);
|
||||
|
||||
token = subflow_req->token;
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
if (!radix_tree_lookup(&token_req_tree, token) &&
|
||||
!radix_tree_lookup(&token_tree, token))
|
||||
break;
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
}
|
||||
|
||||
err = radix_tree_insert(&token_req_tree,
|
||||
subflow_req->token, &token_used);
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* mptcp_token_new_connect - create new key/idsn/token for subflow
|
||||
* @sk - the socket that will initiate a connection
|
||||
*
|
||||
* This function is called when a new outgoing mptcp connection is
|
||||
* initiated.
|
||||
*
|
||||
* It creates a unique token to identify the new mptcp connection,
|
||||
* a secret local key and the initial data sequence number (idsn).
|
||||
*
|
||||
* On success, the mptcp connection can be found again using
|
||||
* the computed token at a later time, this is needed to process
|
||||
* join requests.
|
||||
*
|
||||
* returns 0 on success.
|
||||
*/
|
||||
int mptcp_token_new_connect(struct sock *sk)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
struct sock *mptcp_sock = subflow->conn;
|
||||
int err;
|
||||
|
||||
while (1) {
|
||||
u32 token;
|
||||
|
||||
mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token,
|
||||
&subflow->idsn);
|
||||
|
||||
pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
|
||||
sk, subflow->local_key, subflow->token, subflow->idsn);
|
||||
|
||||
token = subflow->token;
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
if (!radix_tree_lookup(&token_req_tree, token) &&
|
||||
!radix_tree_lookup(&token_tree, token))
|
||||
break;
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
}
|
||||
err = radix_tree_insert(&token_tree, subflow->token, mptcp_sock);
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* mptcp_token_new_accept - insert token for later processing
|
||||
* @token: the token to insert to the tree
|
||||
*
|
||||
* Called when a SYN packet creates a new logical connection, i.e.
|
||||
* is not a join request.
|
||||
*
|
||||
* We don't have an mptcp socket yet at that point.
|
||||
* This is paired with mptcp_token_update_accept, called on accept().
|
||||
*/
|
||||
int mptcp_token_new_accept(u32 token)
|
||||
{
|
||||
int err;
|
||||
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
err = radix_tree_insert(&token_tree, token, &token_used);
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* mptcp_token_update_accept - update token to map to mptcp socket
|
||||
* @conn: the new struct mptcp_sock
|
||||
* @sk: the initial subflow for this mptcp socket
|
||||
*
|
||||
* Called when the first mptcp socket is created on accept to
|
||||
* refresh the dummy mapping (done to reserve the token) with
|
||||
* the mptcp_socket structure that wasn't allocated before.
|
||||
*/
|
||||
void mptcp_token_update_accept(struct sock *sk, struct sock *conn)
|
||||
{
|
||||
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
||||
void __rcu **slot;
|
||||
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
slot = radix_tree_lookup_slot(&token_tree, subflow->token);
|
||||
WARN_ON_ONCE(!slot);
|
||||
if (slot) {
|
||||
WARN_ON_ONCE(rcu_access_pointer(*slot) != &token_used);
|
||||
radix_tree_replace_slot(&token_tree, slot, conn);
|
||||
}
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* mptcp_token_destroy_request - remove mptcp connection/token
|
||||
* @token - token of mptcp connection to remove
|
||||
*
|
||||
* Remove not-yet-fully-established incoming connection identified
|
||||
* by @token.
|
||||
*/
|
||||
void mptcp_token_destroy_request(u32 token)
|
||||
{
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
radix_tree_delete(&token_req_tree, token);
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* mptcp_token_destroy - remove mptcp connection/token
|
||||
* @token - token of mptcp connection to remove
|
||||
*
|
||||
* Remove the connection identified by @token.
|
||||
*/
|
||||
void mptcp_token_destroy(u32 token)
|
||||
{
|
||||
spin_lock_bh(&token_tree_lock);
|
||||
radix_tree_delete(&token_tree, token);
|
||||
spin_unlock_bh(&token_tree_lock);
|
||||
}
|
|
@ -32,6 +32,7 @@ TARGETS += memory-hotplug
|
|||
TARGETS += mount
|
||||
TARGETS += mqueue
|
||||
TARGETS += net
|
||||
TARGETS += net/mptcp
|
||||
TARGETS += netfilter
|
||||
TARGETS += networking/timestamping
|
||||
TARGETS += nsfs
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
mptcp_connect
|
||||
*.pcap
|
|
@ -0,0 +1,13 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
top_srcdir = ../../../../..
|
||||
|
||||
CFLAGS = -Wall -Wl,--no-as-needed -O2 -g
|
||||
|
||||
TEST_PROGS := mptcp_connect.sh
|
||||
|
||||
TEST_GEN_FILES = mptcp_connect
|
||||
|
||||
EXTRA_CLEAN := *.pcap
|
||||
|
||||
include ../../lib.mk
|
|
@ -0,0 +1,4 @@
|
|||
CONFIG_MPTCP=y
|
||||
CONFIG_MPTCP_IPV6=y
|
||||
CONFIG_VETH=y
|
||||
CONFIG_NET_SCH_NETEM=m
|
|
@ -0,0 +1,832 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/poll.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <linux/tcp.h>
|
||||
|
||||
extern int optind;
|
||||
|
||||
#ifndef IPPROTO_MPTCP
|
||||
#define IPPROTO_MPTCP 262
|
||||
#endif
|
||||
#ifndef TCP_ULP
|
||||
#define TCP_ULP 31
|
||||
#endif
|
||||
|
||||
static bool listen_mode;
|
||||
static int poll_timeout;
|
||||
|
||||
enum cfg_mode {
|
||||
CFG_MODE_POLL,
|
||||
CFG_MODE_MMAP,
|
||||
CFG_MODE_SENDFILE,
|
||||
};
|
||||
|
||||
static enum cfg_mode cfg_mode = CFG_MODE_POLL;
|
||||
static const char *cfg_host;
|
||||
static const char *cfg_port = "12000";
|
||||
static int cfg_sock_proto = IPPROTO_MPTCP;
|
||||
static bool tcpulp_audit;
|
||||
static int pf = AF_INET;
|
||||
static int cfg_sndbuf;
|
||||
|
||||
static void die_usage(void)
|
||||
{
|
||||
fprintf(stderr, "Usage: mptcp_connect [-6] [-u] [-s MPTCP|TCP] [-p port] -m mode]"
|
||||
"[ -l ] [ -t timeout ] connect_address\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static const char *getxinfo_strerr(int err)
|
||||
{
|
||||
if (err == EAI_SYSTEM)
|
||||
return strerror(errno);
|
||||
|
||||
return gai_strerror(err);
|
||||
}
|
||||
|
||||
static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
|
||||
char *host, socklen_t hostlen,
|
||||
char *serv, socklen_t servlen)
|
||||
{
|
||||
int flags = NI_NUMERICHOST | NI_NUMERICSERV;
|
||||
int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
|
||||
flags);
|
||||
|
||||
if (err) {
|
||||
const char *errstr = getxinfo_strerr(err);
|
||||
|
||||
fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void xgetaddrinfo(const char *node, const char *service,
|
||||
const struct addrinfo *hints,
|
||||
struct addrinfo **res)
|
||||
{
|
||||
int err = getaddrinfo(node, service, hints, res);
|
||||
|
||||
if (err) {
|
||||
const char *errstr = getxinfo_strerr(err);
|
||||
|
||||
fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
|
||||
node ? node : "", service ? service : "", errstr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void set_sndbuf(int fd, unsigned int size)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
|
||||
if (err) {
|
||||
perror("set SO_SNDBUF");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static int sock_listen_mptcp(const char * const listenaddr,
|
||||
const char * const port)
|
||||
{
|
||||
int sock;
|
||||
struct addrinfo hints = {
|
||||
.ai_protocol = IPPROTO_TCP,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
.ai_flags = AI_PASSIVE | AI_NUMERICHOST
|
||||
};
|
||||
|
||||
hints.ai_family = pf;
|
||||
|
||||
struct addrinfo *a, *addr;
|
||||
int one = 1;
|
||||
|
||||
xgetaddrinfo(listenaddr, port, &hints, &addr);
|
||||
hints.ai_family = pf;
|
||||
|
||||
for (a = addr; a; a = a->ai_next) {
|
||||
sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
|
||||
if (sock < 0)
|
||||
continue;
|
||||
|
||||
if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
|
||||
sizeof(one)))
|
||||
perror("setsockopt");
|
||||
|
||||
if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
|
||||
break; /* success */
|
||||
|
||||
perror("bind");
|
||||
close(sock);
|
||||
sock = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr);
|
||||
|
||||
if (sock < 0) {
|
||||
fprintf(stderr, "Could not create listen socket\n");
|
||||
return sock;
|
||||
}
|
||||
|
||||
if (listen(sock, 20)) {
|
||||
perror("listen");
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
static bool sock_test_tcpulp(const char * const remoteaddr,
|
||||
const char * const port)
|
||||
{
|
||||
struct addrinfo hints = {
|
||||
.ai_protocol = IPPROTO_TCP,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
};
|
||||
struct addrinfo *a, *addr;
|
||||
int sock = -1, ret = 0;
|
||||
bool test_pass = false;
|
||||
|
||||
hints.ai_family = AF_INET;
|
||||
|
||||
xgetaddrinfo(remoteaddr, port, &hints, &addr);
|
||||
for (a = addr; a; a = a->ai_next) {
|
||||
sock = socket(a->ai_family, a->ai_socktype, IPPROTO_TCP);
|
||||
if (sock < 0) {
|
||||
perror("socket");
|
||||
continue;
|
||||
}
|
||||
ret = setsockopt(sock, IPPROTO_TCP, TCP_ULP, "mptcp",
|
||||
sizeof("mptcp"));
|
||||
if (ret == -1 && errno == EOPNOTSUPP)
|
||||
test_pass = true;
|
||||
close(sock);
|
||||
|
||||
if (test_pass)
|
||||
break;
|
||||
if (!ret)
|
||||
fprintf(stderr,
|
||||
"setsockopt(TCP_ULP) returned 0\n");
|
||||
else
|
||||
perror("setsockopt(TCP_ULP)");
|
||||
}
|
||||
return test_pass;
|
||||
}
|
||||
|
||||
static int sock_connect_mptcp(const char * const remoteaddr,
|
||||
const char * const port, int proto)
|
||||
{
|
||||
struct addrinfo hints = {
|
||||
.ai_protocol = IPPROTO_TCP,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
};
|
||||
struct addrinfo *a, *addr;
|
||||
int sock = -1;
|
||||
|
||||
hints.ai_family = pf;
|
||||
|
||||
xgetaddrinfo(remoteaddr, port, &hints, &addr);
|
||||
for (a = addr; a; a = a->ai_next) {
|
||||
sock = socket(a->ai_family, a->ai_socktype, proto);
|
||||
if (sock < 0) {
|
||||
perror("socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connect(sock, a->ai_addr, a->ai_addrlen) == 0)
|
||||
break; /* success */
|
||||
|
||||
perror("connect()");
|
||||
close(sock);
|
||||
sock = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr);
|
||||
return sock;
|
||||
}
|
||||
|
||||
static size_t do_rnd_write(const int fd, char *buf, const size_t len)
|
||||
{
|
||||
unsigned int do_w;
|
||||
ssize_t bw;
|
||||
|
||||
do_w = rand() & 0xffff;
|
||||
if (do_w == 0 || do_w > len)
|
||||
do_w = len;
|
||||
|
||||
bw = write(fd, buf, do_w);
|
||||
if (bw < 0)
|
||||
perror("write");
|
||||
|
||||
return bw;
|
||||
}
|
||||
|
||||
static size_t do_write(const int fd, char *buf, const size_t len)
|
||||
{
|
||||
size_t offset = 0;
|
||||
|
||||
while (offset < len) {
|
||||
size_t written;
|
||||
ssize_t bw;
|
||||
|
||||
bw = write(fd, buf + offset, len - offset);
|
||||
if (bw < 0) {
|
||||
perror("write");
|
||||
return 0;
|
||||
}
|
||||
|
||||
written = (size_t)bw;
|
||||
offset += written;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
|
||||
{
|
||||
size_t cap = rand();
|
||||
|
||||
cap &= 0xffff;
|
||||
|
||||
if (cap == 0)
|
||||
cap = 1;
|
||||
else if (cap > len)
|
||||
cap = len;
|
||||
|
||||
return read(fd, buf, cap);
|
||||
}
|
||||
|
||||
static void set_nonblock(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFL);
|
||||
|
||||
if (flags == -1)
|
||||
return;
|
||||
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
static int copyfd_io_poll(int infd, int peerfd, int outfd)
|
||||
{
|
||||
struct pollfd fds = {
|
||||
.fd = peerfd,
|
||||
.events = POLLIN | POLLOUT,
|
||||
};
|
||||
unsigned int woff = 0, wlen = 0;
|
||||
char wbuf[8192];
|
||||
|
||||
set_nonblock(peerfd);
|
||||
|
||||
for (;;) {
|
||||
char rbuf[8192];
|
||||
ssize_t len;
|
||||
|
||||
if (fds.events == 0)
|
||||
break;
|
||||
|
||||
switch (poll(&fds, 1, poll_timeout)) {
|
||||
case -1:
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
perror("poll");
|
||||
return 1;
|
||||
case 0:
|
||||
fprintf(stderr, "%s: poll timed out (events: "
|
||||
"POLLIN %u, POLLOUT %u)\n", __func__,
|
||||
fds.events & POLLIN, fds.events & POLLOUT);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (fds.revents & POLLIN) {
|
||||
len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
|
||||
if (len == 0) {
|
||||
/* no more data to receive:
|
||||
* peer has closed its write side
|
||||
*/
|
||||
fds.events &= ~POLLIN;
|
||||
|
||||
if ((fds.events & POLLOUT) == 0)
|
||||
/* and nothing more to send */
|
||||
break;
|
||||
|
||||
/* Else, still have data to transmit */
|
||||
} else if (len < 0) {
|
||||
perror("read");
|
||||
return 3;
|
||||
}
|
||||
|
||||
do_write(outfd, rbuf, len);
|
||||
}
|
||||
|
||||
if (fds.revents & POLLOUT) {
|
||||
if (wlen == 0) {
|
||||
woff = 0;
|
||||
wlen = read(infd, wbuf, sizeof(wbuf));
|
||||
}
|
||||
|
||||
if (wlen > 0) {
|
||||
ssize_t bw;
|
||||
|
||||
bw = do_rnd_write(peerfd, wbuf + woff, wlen);
|
||||
if (bw < 0)
|
||||
return 111;
|
||||
|
||||
woff += bw;
|
||||
wlen -= bw;
|
||||
} else if (wlen == 0) {
|
||||
/* We have no more data to send. */
|
||||
fds.events &= ~POLLOUT;
|
||||
|
||||
if ((fds.events & POLLIN) == 0)
|
||||
/* ... and peer also closed already */
|
||||
break;
|
||||
|
||||
/* ... but we still receive.
|
||||
* Close our write side.
|
||||
*/
|
||||
shutdown(peerfd, SHUT_WR);
|
||||
} else {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
perror("read");
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
if (fds.revents & (POLLERR | POLLNVAL)) {
|
||||
fprintf(stderr, "Unexpected revents: "
|
||||
"POLLERR/POLLNVAL(%x)\n", fds.revents);
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
close(peerfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_recvfile(int infd, int outfd)
|
||||
{
|
||||
ssize_t r;
|
||||
|
||||
do {
|
||||
char buf[16384];
|
||||
|
||||
r = do_rnd_read(infd, buf, sizeof(buf));
|
||||
if (r > 0) {
|
||||
if (write(outfd, buf, r) != r)
|
||||
break;
|
||||
} else if (r < 0) {
|
||||
perror("read");
|
||||
}
|
||||
} while (r > 0);
|
||||
|
||||
return (int)r;
|
||||
}
|
||||
|
||||
static int do_mmap(int infd, int outfd, unsigned int size)
|
||||
{
|
||||
char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
|
||||
ssize_t ret = 0, off = 0;
|
||||
size_t rem;
|
||||
|
||||
if (inbuf == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
return 1;
|
||||
}
|
||||
|
||||
rem = size;
|
||||
|
||||
while (rem > 0) {
|
||||
ret = write(outfd, inbuf + off, rem);
|
||||
|
||||
if (ret < 0) {
|
||||
perror("write");
|
||||
break;
|
||||
}
|
||||
|
||||
off += ret;
|
||||
rem -= ret;
|
||||
}
|
||||
|
||||
munmap(inbuf, size);
|
||||
return rem;
|
||||
}
|
||||
|
||||
static int get_infd_size(int fd)
|
||||
{
|
||||
struct stat sb;
|
||||
ssize_t count;
|
||||
int err;
|
||||
|
||||
err = fstat(fd, &sb);
|
||||
if (err < 0) {
|
||||
perror("fstat");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((sb.st_mode & S_IFMT) != S_IFREG) {
|
||||
fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
|
||||
return -2;
|
||||
}
|
||||
|
||||
count = sb.st_size;
|
||||
if (count > INT_MAX) {
|
||||
fprintf(stderr, "File too large: %zu\n", count);
|
||||
return -3;
|
||||
}
|
||||
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
static int do_sendfile(int infd, int outfd, unsigned int count)
|
||||
{
|
||||
while (count > 0) {
|
||||
ssize_t r;
|
||||
|
||||
r = sendfile(outfd, infd, NULL, count);
|
||||
if (r < 0) {
|
||||
perror("sendfile");
|
||||
return 3;
|
||||
}
|
||||
|
||||
count -= r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int copyfd_io_mmap(int infd, int peerfd, int outfd,
|
||||
unsigned int size)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (listen_mode) {
|
||||
err = do_recvfile(peerfd, outfd);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = do_mmap(infd, peerfd, size);
|
||||
} else {
|
||||
err = do_mmap(infd, peerfd, size);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
shutdown(peerfd, SHUT_WR);
|
||||
|
||||
err = do_recvfile(peerfd, outfd);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
|
||||
unsigned int size)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (listen_mode) {
|
||||
err = do_recvfile(peerfd, outfd);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = do_sendfile(infd, peerfd, size);
|
||||
} else {
|
||||
err = do_sendfile(infd, peerfd, size);
|
||||
if (err)
|
||||
return err;
|
||||
err = do_recvfile(peerfd, outfd);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int copyfd_io(int infd, int peerfd, int outfd)
|
||||
{
|
||||
int file_size;
|
||||
|
||||
switch (cfg_mode) {
|
||||
case CFG_MODE_POLL:
|
||||
return copyfd_io_poll(infd, peerfd, outfd);
|
||||
case CFG_MODE_MMAP:
|
||||
file_size = get_infd_size(infd);
|
||||
if (file_size < 0)
|
||||
return file_size;
|
||||
return copyfd_io_mmap(infd, peerfd, outfd, file_size);
|
||||
case CFG_MODE_SENDFILE:
|
||||
file_size = get_infd_size(infd);
|
||||
if (file_size < 0)
|
||||
return file_size;
|
||||
return copyfd_io_sendfile(infd, peerfd, outfd, file_size);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Invalid mode %d\n", cfg_mode);
|
||||
|
||||
die_usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void check_sockaddr(int pf, struct sockaddr_storage *ss,
|
||||
socklen_t salen)
|
||||
{
|
||||
struct sockaddr_in6 *sin6;
|
||||
struct sockaddr_in *sin;
|
||||
socklen_t wanted_size = 0;
|
||||
|
||||
switch (pf) {
|
||||
case AF_INET:
|
||||
wanted_size = sizeof(*sin);
|
||||
sin = (void *)ss;
|
||||
if (!sin->sin_port)
|
||||
fprintf(stderr, "accept: something wrong: ip connection from port 0");
|
||||
break;
|
||||
case AF_INET6:
|
||||
wanted_size = sizeof(*sin6);
|
||||
sin6 = (void *)ss;
|
||||
if (!sin6->sin6_port)
|
||||
fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
|
||||
return;
|
||||
}
|
||||
|
||||
if (salen != wanted_size)
|
||||
fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
|
||||
(int)salen, wanted_size);
|
||||
|
||||
if (ss->ss_family != pf)
|
||||
fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
|
||||
(int)ss->ss_family, pf);
|
||||
}
|
||||
|
||||
static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
|
||||
{
|
||||
struct sockaddr_storage peerss;
|
||||
socklen_t peersalen = sizeof(peerss);
|
||||
|
||||
if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
|
||||
perror("getpeername");
|
||||
return;
|
||||
}
|
||||
|
||||
if (peersalen != salen) {
|
||||
fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
|
||||
return;
|
||||
}
|
||||
|
||||
if (memcmp(ss, &peerss, peersalen)) {
|
||||
char a[INET6_ADDRSTRLEN];
|
||||
char b[INET6_ADDRSTRLEN];
|
||||
char c[INET6_ADDRSTRLEN];
|
||||
char d[INET6_ADDRSTRLEN];
|
||||
|
||||
xgetnameinfo((struct sockaddr *)ss, salen,
|
||||
a, sizeof(a), b, sizeof(b));
|
||||
|
||||
xgetnameinfo((struct sockaddr *)&peerss, peersalen,
|
||||
c, sizeof(c), d, sizeof(d));
|
||||
|
||||
fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
|
||||
__func__, a, c, b, d, peersalen, salen);
|
||||
}
|
||||
}
|
||||
|
||||
static void check_getpeername_connect(int fd)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t salen = sizeof(ss);
|
||||
char a[INET6_ADDRSTRLEN];
|
||||
char b[INET6_ADDRSTRLEN];
|
||||
|
||||
if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
|
||||
perror("getpeername");
|
||||
return;
|
||||
}
|
||||
|
||||
xgetnameinfo((struct sockaddr *)&ss, salen,
|
||||
a, sizeof(a), b, sizeof(b));
|
||||
|
||||
if (strcmp(cfg_host, a) || strcmp(cfg_port, b))
|
||||
fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
|
||||
cfg_host, a, cfg_port, b);
|
||||
}
|
||||
|
||||
int main_loop_s(int listensock)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
struct pollfd polls;
|
||||
socklen_t salen;
|
||||
int remotesock;
|
||||
|
||||
polls.fd = listensock;
|
||||
polls.events = POLLIN;
|
||||
|
||||
switch (poll(&polls, 1, poll_timeout)) {
|
||||
case -1:
|
||||
perror("poll");
|
||||
return 1;
|
||||
case 0:
|
||||
fprintf(stderr, "%s: timed out\n", __func__);
|
||||
close(listensock);
|
||||
return 2;
|
||||
}
|
||||
|
||||
salen = sizeof(ss);
|
||||
remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
|
||||
if (remotesock >= 0) {
|
||||
check_sockaddr(pf, &ss, salen);
|
||||
check_getpeername(remotesock, &ss, salen);
|
||||
|
||||
return copyfd_io(0, remotesock, 1);
|
||||
}
|
||||
|
||||
perror("accept");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void init_rng(void)
|
||||
{
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
unsigned int foo;
|
||||
|
||||
if (fd > 0) {
|
||||
int ret = read(fd, &foo, sizeof(foo));
|
||||
|
||||
if (ret < 0)
|
||||
srand(fd + foo);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
srand(foo);
|
||||
}
|
||||
|
||||
int main_loop(void)
|
||||
{
|
||||
int fd;
|
||||
|
||||
/* listener is ready. */
|
||||
fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto);
|
||||
if (fd < 0)
|
||||
return 2;
|
||||
|
||||
check_getpeername_connect(fd);
|
||||
|
||||
if (cfg_sndbuf)
|
||||
set_sndbuf(fd, cfg_sndbuf);
|
||||
|
||||
return copyfd_io(0, fd, 1);
|
||||
}
|
||||
|
||||
int parse_proto(const char *proto)
|
||||
{
|
||||
if (!strcasecmp(proto, "MPTCP"))
|
||||
return IPPROTO_MPTCP;
|
||||
if (!strcasecmp(proto, "TCP"))
|
||||
return IPPROTO_TCP;
|
||||
|
||||
fprintf(stderr, "Unknown protocol: %s\n.", proto);
|
||||
die_usage();
|
||||
|
||||
/* silence compiler warning */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_mode(const char *mode)
|
||||
{
|
||||
if (!strcasecmp(mode, "poll"))
|
||||
return CFG_MODE_POLL;
|
||||
if (!strcasecmp(mode, "mmap"))
|
||||
return CFG_MODE_MMAP;
|
||||
if (!strcasecmp(mode, "sendfile"))
|
||||
return CFG_MODE_SENDFILE;
|
||||
|
||||
fprintf(stderr, "Unknown test mode: %s\n", mode);
|
||||
fprintf(stderr, "Supported modes are:\n");
|
||||
fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
|
||||
fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
|
||||
fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
|
||||
|
||||
die_usage();
|
||||
|
||||
/* silence compiler warning */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_sndbuf(const char *size)
|
||||
{
|
||||
unsigned long s;
|
||||
|
||||
errno = 0;
|
||||
|
||||
s = strtoul(size, NULL, 0);
|
||||
|
||||
if (errno) {
|
||||
fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
|
||||
size, strerror(errno));
|
||||
die_usage();
|
||||
}
|
||||
|
||||
if (s > INT_MAX) {
|
||||
fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
|
||||
size, strerror(ERANGE));
|
||||
die_usage();
|
||||
}
|
||||
|
||||
cfg_sndbuf = s;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void parse_opts(int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
|
||||
while ((c = getopt(argc, argv, "6lp:s:hut:m:b:")) != -1) {
|
||||
switch (c) {
|
||||
case 'l':
|
||||
listen_mode = true;
|
||||
break;
|
||||
case 'p':
|
||||
cfg_port = optarg;
|
||||
break;
|
||||
case 's':
|
||||
cfg_sock_proto = parse_proto(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
die_usage();
|
||||
break;
|
||||
case 'u':
|
||||
tcpulp_audit = true;
|
||||
break;
|
||||
case '6':
|
||||
pf = AF_INET6;
|
||||
break;
|
||||
case 't':
|
||||
poll_timeout = atoi(optarg) * 1000;
|
||||
if (poll_timeout <= 0)
|
||||
poll_timeout = -1;
|
||||
break;
|
||||
case 'm':
|
||||
cfg_mode = parse_mode(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
cfg_sndbuf = parse_sndbuf(optarg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind + 1 != argc)
|
||||
die_usage();
|
||||
cfg_host = argv[optind];
|
||||
|
||||
if (strchr(cfg_host, ':'))
|
||||
pf = AF_INET6;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
init_rng();
|
||||
|
||||
parse_opts(argc, argv);
|
||||
|
||||
if (tcpulp_audit)
|
||||
return sock_test_tcpulp(cfg_host, cfg_port) ? 0 : 1;
|
||||
|
||||
if (listen_mode) {
|
||||
int fd = sock_listen_mptcp(cfg_host, cfg_port);
|
||||
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
|
||||
if (cfg_sndbuf)
|
||||
set_sndbuf(fd, cfg_sndbuf);
|
||||
|
||||
return main_loop_s(fd);
|
||||
}
|
||||
|
||||
return main_loop();
|
||||
}
|
|
@ -0,0 +1,595 @@
|
|||
#!/bin/bash
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
time_start=$(date +%s)
|
||||
|
||||
optstring="b:d:e:l:r:h4cm:"
|
||||
ret=0
|
||||
sin=""
|
||||
sout=""
|
||||
cin=""
|
||||
cout=""
|
||||
ksft_skip=4
|
||||
capture=false
|
||||
timeout=30
|
||||
ipv6=true
|
||||
ethtool_random_on=true
|
||||
tc_delay="$((RANDOM%400))"
|
||||
tc_loss=$((RANDOM%101))
|
||||
tc_reorder=""
|
||||
testmode=""
|
||||
sndbuf=0
|
||||
options_log=true
|
||||
|
||||
if [ $tc_loss -eq 100 ];then
|
||||
tc_loss=1%
|
||||
elif [ $tc_loss -ge 10 ]; then
|
||||
tc_loss=0.$tc_loss%
|
||||
elif [ $tc_loss -ge 1 ]; then
|
||||
tc_loss=0.0$tc_loss%
|
||||
else
|
||||
tc_loss=""
|
||||
fi
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [ -a ]"
|
||||
echo -e "\t-d: tc/netem delay in milliseconds, e.g. \"-d 10\" (default random)"
|
||||
echo -e "\t-l: tc/netem loss percentage, e.g. \"-l 0.02\" (default random)"
|
||||
echo -e "\t-r: tc/netem reorder mode, e.g. \"-r 25% 50% gap 5\", use "-r 0" to disable reordering (default random)"
|
||||
echo -e "\t-e: ethtool features to disable, e.g.: \"-e tso -e gso\" (default: randomly disable any of tso/gso/gro)"
|
||||
echo -e "\t-4: IPv4 only: disable IPv6 tests (default: test both IPv4 and IPv6)"
|
||||
echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
|
||||
echo -e "\t-b: set sndbuf value (default: use kernel default)"
|
||||
echo -e "\t-m: test mode (poll, sendfile; default: poll)"
|
||||
}
|
||||
|
||||
while getopts "$optstring" option;do
|
||||
case "$option" in
|
||||
"h")
|
||||
usage $0
|
||||
exit 0
|
||||
;;
|
||||
"d")
|
||||
if [ $OPTARG -ge 0 ];then
|
||||
tc_delay="$OPTARG"
|
||||
else
|
||||
echo "-d requires numeric argument, got \"$OPTARG\"" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
"e")
|
||||
ethtool_args="$ethtool_args $OPTARG off"
|
||||
ethtool_random_on=false
|
||||
;;
|
||||
"l")
|
||||
tc_loss="$OPTARG"
|
||||
;;
|
||||
"r")
|
||||
tc_reorder="$OPTARG"
|
||||
;;
|
||||
"4")
|
||||
ipv6=false
|
||||
;;
|
||||
"c")
|
||||
capture=true
|
||||
;;
|
||||
"b")
|
||||
if [ $OPTARG -ge 0 ];then
|
||||
sndbuf="$OPTARG"
|
||||
else
|
||||
echo "-s requires numeric argument, got \"$OPTARG\"" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
"m")
|
||||
testmode="$OPTARG"
|
||||
;;
|
||||
"?")
|
||||
usage $0
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
sec=$(date +%s)
|
||||
rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
|
||||
ns1="ns1-$rndh"
|
||||
ns2="ns2-$rndh"
|
||||
ns3="ns3-$rndh"
|
||||
ns4="ns4-$rndh"
|
||||
|
||||
TEST_COUNT=0
|
||||
|
||||
cleanup()
|
||||
{
|
||||
rm -f "$cin" "$cout"
|
||||
rm -f "$sin" "$sout"
|
||||
rm -f "$capout"
|
||||
|
||||
local netns
|
||||
for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
|
||||
ip netns del $netns
|
||||
done
|
||||
}
|
||||
|
||||
ip -Version > /dev/null 2>&1
|
||||
if [ $? -ne 0 ];then
|
||||
echo "SKIP: Could not run test without ip tool"
|
||||
exit $ksft_skip
|
||||
fi
|
||||
|
||||
sin=$(mktemp)
|
||||
sout=$(mktemp)
|
||||
cin=$(mktemp)
|
||||
cout=$(mktemp)
|
||||
capout=$(mktemp)
|
||||
trap cleanup EXIT
|
||||
|
||||
for i in "$ns1" "$ns2" "$ns3" "$ns4";do
|
||||
ip netns add $i || exit $ksft_skip
|
||||
ip -net $i link set lo up
|
||||
done
|
||||
|
||||
# "$ns1" ns2 ns3 ns4
|
||||
# ns1eth2 ns2eth1 ns2eth3 ns3eth2 ns3eth4 ns4eth3
|
||||
# - drop 1% -> reorder 25%
|
||||
# <- TSO off -
|
||||
|
||||
ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
|
||||
ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth2 netns "$ns3"
|
||||
ip link add ns3eth4 netns "$ns3" type veth peer name ns4eth3 netns "$ns4"
|
||||
|
||||
ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
|
||||
ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
|
||||
|
||||
ip -net "$ns1" link set ns1eth2 up
|
||||
ip -net "$ns1" route add default via 10.0.1.2
|
||||
ip -net "$ns1" route add default via dead:beef:1::2
|
||||
|
||||
ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
|
||||
ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
|
||||
ip -net "$ns2" link set ns2eth1 up
|
||||
|
||||
ip -net "$ns2" addr add 10.0.2.1/24 dev ns2eth3
|
||||
ip -net "$ns2" addr add dead:beef:2::1/64 dev ns2eth3 nodad
|
||||
ip -net "$ns2" link set ns2eth3 up
|
||||
ip -net "$ns2" route add default via 10.0.2.2
|
||||
ip -net "$ns2" route add default via dead:beef:2::2
|
||||
ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
|
||||
ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
|
||||
|
||||
ip -net "$ns3" addr add 10.0.2.2/24 dev ns3eth2
|
||||
ip -net "$ns3" addr add dead:beef:2::2/64 dev ns3eth2 nodad
|
||||
ip -net "$ns3" link set ns3eth2 up
|
||||
|
||||
ip -net "$ns3" addr add 10.0.3.2/24 dev ns3eth4
|
||||
ip -net "$ns3" addr add dead:beef:3::2/64 dev ns3eth4 nodad
|
||||
ip -net "$ns3" link set ns3eth4 up
|
||||
ip -net "$ns3" route add default via 10.0.2.1
|
||||
ip -net "$ns3" route add default via dead:beef:2::1
|
||||
ip netns exec "$ns3" sysctl -q net.ipv4.ip_forward=1
|
||||
ip netns exec "$ns3" sysctl -q net.ipv6.conf.all.forwarding=1
|
||||
|
||||
ip -net "$ns4" addr add 10.0.3.1/24 dev ns4eth3
|
||||
ip -net "$ns4" addr add dead:beef:3::1/64 dev ns4eth3 nodad
|
||||
ip -net "$ns4" link set ns4eth3 up
|
||||
ip -net "$ns4" route add default via 10.0.3.2
|
||||
ip -net "$ns4" route add default via dead:beef:3::2
|
||||
|
||||
set_ethtool_flags() {
|
||||
local ns="$1"
|
||||
local dev="$2"
|
||||
local flags="$3"
|
||||
|
||||
ip netns exec $ns ethtool -K $dev $flags 2>/dev/null
|
||||
[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags"
|
||||
}
|
||||
|
||||
set_random_ethtool_flags() {
|
||||
local flags=""
|
||||
local r=$RANDOM
|
||||
|
||||
local pick1=$((r & 1))
|
||||
local pick2=$((r & 2))
|
||||
local pick3=$((r & 4))
|
||||
|
||||
[ $pick1 -ne 0 ] && flags="tso off"
|
||||
[ $pick2 -ne 0 ] && flags="$flags gso off"
|
||||
[ $pick3 -ne 0 ] && flags="$flags gro off"
|
||||
|
||||
[ -z "$flags" ] && return
|
||||
|
||||
set_ethtool_flags "$1" "$2" "$flags"
|
||||
}
|
||||
|
||||
if $ethtool_random_on;then
|
||||
set_random_ethtool_flags "$ns3" ns3eth2
|
||||
set_random_ethtool_flags "$ns4" ns4eth3
|
||||
else
|
||||
set_ethtool_flags "$ns3" ns3eth2 "$ethtool_args"
|
||||
set_ethtool_flags "$ns4" ns4eth3 "$ethtool_args"
|
||||
fi
|
||||
|
||||
print_file_err()
|
||||
{
|
||||
ls -l "$1" 1>&2
|
||||
echo "Trailing bytes are: "
|
||||
tail -c 27 "$1"
|
||||
}
|
||||
|
||||
check_transfer()
|
||||
{
|
||||
local in=$1
|
||||
local out=$2
|
||||
local what=$3
|
||||
|
||||
cmp "$in" "$out" > /dev/null 2>&1
|
||||
if [ $? -ne 0 ] ;then
|
||||
echo "[ FAIL ] $what does not match (in, out):"
|
||||
print_file_err "$in"
|
||||
print_file_err "$out"
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
check_mptcp_disabled()
|
||||
{
|
||||
local disabled_ns
|
||||
disabled_ns="ns_disabled-$sech-$(mktemp -u XXXXXX)"
|
||||
ip netns add ${disabled_ns} || exit $ksft_skip
|
||||
|
||||
# net.mptcp.enabled should be enabled by default
|
||||
if [ "$(ip netns exec ${disabled_ns} sysctl net.mptcp.enabled | awk '{ print $3 }')" -ne 1 ]; then
|
||||
echo -e "net.mptcp.enabled sysctl is not 1 by default\t\t[ FAIL ]"
|
||||
ret=1
|
||||
return 1
|
||||
fi
|
||||
ip netns exec ${disabled_ns} sysctl -q net.mptcp.enabled=0
|
||||
|
||||
local err=0
|
||||
LANG=C ip netns exec ${disabled_ns} ./mptcp_connect -t $timeout -p 10000 -s MPTCP 127.0.0.1 < "$cin" 2>&1 | \
|
||||
grep -q "^socket: Protocol not available$" && err=1
|
||||
ip netns delete ${disabled_ns}
|
||||
|
||||
if [ ${err} -eq 0 ]; then
|
||||
echo -e "New MPTCP socket cannot be blocked via sysctl\t\t[ FAIL ]"
|
||||
ret=1
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo -e "New MPTCP socket can be blocked via sysctl\t\t[ OK ]"
|
||||
return 0
|
||||
}
|
||||
|
||||
check_mptcp_ulp_setsockopt()
|
||||
{
|
||||
local t retval
|
||||
t="ns_ulp-$sech-$(mktemp -u XXXXXX)"
|
||||
|
||||
ip netns add ${t} || exit $ksft_skip
|
||||
if ! ip netns exec ${t} ./mptcp_connect -u -p 10000 -s TCP 127.0.0.1 2>&1; then
|
||||
printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) allowed\t[ FAIL ]\n"
|
||||
retval=1
|
||||
ret=$retval
|
||||
else
|
||||
printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) blocked\t[ OK ]\n"
|
||||
retval=0
|
||||
fi
|
||||
ip netns del ${t}
|
||||
return $retval
|
||||
}
|
||||
|
||||
# $1: IP address
|
||||
is_v6()
|
||||
{
|
||||
[ -z "${1##*:*}" ]
|
||||
}
|
||||
|
||||
do_ping()
|
||||
{
|
||||
local listener_ns="$1"
|
||||
local connector_ns="$2"
|
||||
local connect_addr="$3"
|
||||
local ping_args="-q -c 1"
|
||||
|
||||
if is_v6 "${connect_addr}"; then
|
||||
$ipv6 || return 0
|
||||
ping_args="${ping_args} -6"
|
||||
fi
|
||||
|
||||
ip netns exec ${connector_ns} ping ${ping_args} $connect_addr >/dev/null
|
||||
if [ $? -ne 0 ] ; then
|
||||
echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
|
||||
ret=1
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# $1: ns, $2: port
|
||||
wait_local_port_listen()
|
||||
{
|
||||
local listener_ns="${1}"
|
||||
local port="${2}"
|
||||
|
||||
local port_hex i
|
||||
|
||||
port_hex="$(printf "%04X" "${port}")"
|
||||
for i in $(seq 10); do
|
||||
ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
|
||||
awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
|
||||
break
|
||||
sleep 0.1
|
||||
done
|
||||
}
|
||||
|
||||
do_transfer()
|
||||
{
|
||||
local listener_ns="$1"
|
||||
local connector_ns="$2"
|
||||
local cl_proto="$3"
|
||||
local srv_proto="$4"
|
||||
local connect_addr="$5"
|
||||
local local_addr="$6"
|
||||
local extra_args=""
|
||||
|
||||
local port
|
||||
port=$((10000+$TEST_COUNT))
|
||||
TEST_COUNT=$((TEST_COUNT+1))
|
||||
|
||||
if [ "$sndbuf" -gt 0 ]; then
|
||||
extra_args="$extra_args -b $sndbuf"
|
||||
fi
|
||||
|
||||
if [ -n "$testmode" ]; then
|
||||
extra_args="$extra_args -m $testmode"
|
||||
fi
|
||||
|
||||
if [ -n "$extra_args" ] && $options_log; then
|
||||
options_log=false
|
||||
echo "INFO: extra options: $extra_args"
|
||||
fi
|
||||
|
||||
:> "$cout"
|
||||
:> "$sout"
|
||||
:> "$capout"
|
||||
|
||||
local addr_port
|
||||
addr_port=$(printf "%s:%d" ${connect_addr} ${port})
|
||||
printf "%.3s %-5s -> %.3s (%-20s) %-5s\t" ${connector_ns} ${cl_proto} ${listener_ns} ${addr_port} ${srv_proto}
|
||||
|
||||
if $capture; then
|
||||
local capuser
|
||||
if [ -z $SUDO_USER ] ; then
|
||||
capuser=""
|
||||
else
|
||||
capuser="-Z $SUDO_USER"
|
||||
fi
|
||||
|
||||
local capfile="${listener_ns}-${connector_ns}-${cl_proto}-${srv_proto}-${connect_addr}.pcap"
|
||||
|
||||
ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
|
||||
local cappid=$!
|
||||
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
ip netns exec ${listener_ns} ./mptcp_connect -t $timeout -l -p $port -s ${srv_proto} $extra_args $local_addr < "$sin" > "$sout" &
|
||||
local spid=$!
|
||||
|
||||
wait_local_port_listen "${listener_ns}" "${port}"
|
||||
|
||||
local start
|
||||
start=$(date +%s%3N)
|
||||
ip netns exec ${connector_ns} ./mptcp_connect -t $timeout -p $port -s ${cl_proto} $extra_args $connect_addr < "$cin" > "$cout" &
|
||||
local cpid=$!
|
||||
|
||||
wait $cpid
|
||||
local retc=$?
|
||||
wait $spid
|
||||
local rets=$?
|
||||
|
||||
local stop
|
||||
stop=$(date +%s%3N)
|
||||
|
||||
if $capture; then
|
||||
sleep 1
|
||||
kill $cappid
|
||||
fi
|
||||
|
||||
local duration
|
||||
duration=$((stop-start))
|
||||
duration=$(printf "(duration %05sms)" $duration)
|
||||
if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
|
||||
echo "$duration [ FAIL ] client exit code $retc, server $rets" 1>&2
|
||||
echo "\nnetns ${listener_ns} socket stat for $port:" 1>&2
|
||||
ip netns exec ${listener_ns} ss -nita 1>&2 -o "sport = :$port"
|
||||
echo "\nnetns ${connector_ns} socket stat for $port:" 1>&2
|
||||
ip netns exec ${connector_ns} ss -nita 1>&2 -o "dport = :$port"
|
||||
|
||||
cat "$capout"
|
||||
return 1
|
||||
fi
|
||||
|
||||
check_transfer $sin $cout "file received by client"
|
||||
retc=$?
|
||||
check_transfer $cin $sout "file received by server"
|
||||
rets=$?
|
||||
|
||||
if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
|
||||
echo "$duration [ OK ]"
|
||||
cat "$capout"
|
||||
return 0
|
||||
fi
|
||||
|
||||
cat "$capout"
|
||||
return 1
|
||||
}
|
||||
|
||||
make_file()
|
||||
{
|
||||
local name=$1
|
||||
local who=$2
|
||||
|
||||
local SIZE TSIZE
|
||||
SIZE=$((RANDOM % (1024 * 8)))
|
||||
TSIZE=$((SIZE * 1024))
|
||||
|
||||
dd if=/dev/urandom of="$name" bs=1024 count=$SIZE 2> /dev/null
|
||||
|
||||
SIZE=$((RANDOM % 1024))
|
||||
SIZE=$((SIZE + 128))
|
||||
TSIZE=$((TSIZE + SIZE))
|
||||
dd if=/dev/urandom conv=notrunc of="$name" bs=1 count=$SIZE 2> /dev/null
|
||||
echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
|
||||
|
||||
echo "Created $name (size $TSIZE) containing data sent by $who"
|
||||
}
|
||||
|
||||
run_tests_lo()
|
||||
{
|
||||
local listener_ns="$1"
|
||||
local connector_ns="$2"
|
||||
local connect_addr="$3"
|
||||
local loopback="$4"
|
||||
local lret=0
|
||||
|
||||
# skip if test programs are running inside same netns for subsequent runs.
|
||||
if [ $loopback -eq 0 ] && [ ${listener_ns} = ${connector_ns} ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# skip if we don't want v6
|
||||
if ! $ipv6 && is_v6 "${connect_addr}"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
local local_addr
|
||||
if is_v6 "${connect_addr}"; then
|
||||
local_addr="::"
|
||||
else
|
||||
local_addr="0.0.0.0"
|
||||
fi
|
||||
|
||||
do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} ${local_addr}
|
||||
lret=$?
|
||||
if [ $lret -ne 0 ]; then
|
||||
ret=$lret
|
||||
return 1
|
||||
fi
|
||||
|
||||
# don't bother testing fallback tcp except for loopback case.
|
||||
if [ ${listener_ns} != ${connector_ns} ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
do_transfer ${listener_ns} ${connector_ns} MPTCP TCP ${connect_addr} ${local_addr}
|
||||
lret=$?
|
||||
if [ $lret -ne 0 ]; then
|
||||
ret=$lret
|
||||
return 1
|
||||
fi
|
||||
|
||||
do_transfer ${listener_ns} ${connector_ns} TCP MPTCP ${connect_addr} ${local_addr}
|
||||
lret=$?
|
||||
if [ $lret -ne 0 ]; then
|
||||
ret=$lret
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
run_tests()
|
||||
{
|
||||
run_tests_lo $1 $2 $3 0
|
||||
}
|
||||
|
||||
make_file "$cin" "client"
|
||||
make_file "$sin" "server"
|
||||
|
||||
check_mptcp_disabled
|
||||
|
||||
check_mptcp_ulp_setsockopt
|
||||
|
||||
echo "INFO: validating network environment with pings"
|
||||
for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
|
||||
do_ping "$ns1" $sender 10.0.1.1
|
||||
do_ping "$ns1" $sender dead:beef:1::1
|
||||
|
||||
do_ping "$ns2" $sender 10.0.1.2
|
||||
do_ping "$ns2" $sender dead:beef:1::2
|
||||
do_ping "$ns2" $sender 10.0.2.1
|
||||
do_ping "$ns2" $sender dead:beef:2::1
|
||||
|
||||
do_ping "$ns3" $sender 10.0.2.2
|
||||
do_ping "$ns3" $sender dead:beef:2::2
|
||||
do_ping "$ns3" $sender 10.0.3.2
|
||||
do_ping "$ns3" $sender dead:beef:3::2
|
||||
|
||||
do_ping "$ns4" $sender 10.0.3.1
|
||||
do_ping "$ns4" $sender dead:beef:3::1
|
||||
done
|
||||
|
||||
[ -n "$tc_loss" ] && tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $tc_loss
|
||||
echo -n "INFO: Using loss of $tc_loss "
|
||||
test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "
|
||||
|
||||
if [ -z "${tc_reorder}" ]; then
|
||||
reorder1=$((RANDOM%10))
|
||||
reorder1=$((100 - reorder1))
|
||||
reorder2=$((RANDOM%100))
|
||||
|
||||
if [ $tc_delay -gt 0 ] && [ $reorder1 -lt 100 ] && [ $reorder2 -gt 0 ]; then
|
||||
tc_reorder="reorder ${reorder1}% ${reorder2}%"
|
||||
echo -n "$tc_reorder "
|
||||
fi
|
||||
elif [ "$tc_reorder" = "0" ];then
|
||||
tc_reorder=""
|
||||
elif [ "$tc_delay" -gt 0 ];then
|
||||
# reordering requires some delay
|
||||
tc_reorder="reorder $tc_reorder"
|
||||
echo -n "$tc_reorder "
|
||||
fi
|
||||
|
||||
echo "on ns3eth4"
|
||||
|
||||
tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${tc_delay}ms $tc_reorder
|
||||
|
||||
for sender in $ns1 $ns2 $ns3 $ns4;do
|
||||
run_tests_lo "$ns1" "$sender" 10.0.1.1 1
|
||||
if [ $ret -ne 0 ] ;then
|
||||
echo "FAIL: Could not even run loopback test" 1>&2
|
||||
exit $ret
|
||||
fi
|
||||
run_tests_lo "$ns1" $sender dead:beef:1::1 1
|
||||
if [ $ret -ne 0 ] ;then
|
||||
echo "FAIL: Could not even run loopback v6 test" 2>&1
|
||||
exit $ret
|
||||
fi
|
||||
|
||||
run_tests "$ns2" $sender 10.0.1.2
|
||||
run_tests "$ns2" $sender dead:beef:1::2
|
||||
run_tests "$ns2" $sender 10.0.2.1
|
||||
run_tests "$ns2" $sender dead:beef:2::1
|
||||
|
||||
run_tests "$ns3" $sender 10.0.2.2
|
||||
run_tests "$ns3" $sender dead:beef:2::2
|
||||
run_tests "$ns3" $sender 10.0.3.2
|
||||
run_tests "$ns3" $sender dead:beef:3::2
|
||||
|
||||
run_tests "$ns4" $sender 10.0.3.1
|
||||
run_tests "$ns4" $sender dead:beef:3::1
|
||||
done
|
||||
|
||||
time_end=$(date +%s)
|
||||
time_run=$((time_end-time_start))
|
||||
|
||||
echo "Time: ${time_run} seconds"
|
||||
|
||||
exit $ret
|
|
@ -0,0 +1 @@
|
|||
timeout=450
|
Loading…
Reference in New Issue