protocol.h 6.3 KB
Newer Older
M
Mat Martineau 已提交
1 2 3 4 5 6 7 8 9
/* SPDX-License-Identifier: GPL-2.0 */
/* Multipath TCP
 *
 * Copyright (c) 2017 - 2019, Intel Corporation.
 */

#ifndef __MPTCP_PROTOCOL_H
#define __MPTCP_PROTOCOL_H

10 11 12 13
#include <linux/random.h>
#include <net/tcp.h>
#include <net/inet_connection_sock.h>

14
#define MPTCP_SUPPORTED_VERSION	1
P
Peter Krystad 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

/* 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 */
32
#define TCPOLEN_MPTCP_MPC_SYN		4
P
Peter Krystad 已提交
33 34
#define TCPOLEN_MPTCP_MPC_SYNACK	12
#define TCPOLEN_MPTCP_MPC_ACK		20
35
#define TCPOLEN_MPTCP_MPC_ACK_DATA	22
36
#define TCPOLEN_MPTCP_DSS_BASE		4
37
#define TCPOLEN_MPTCP_DSS_ACK32		4
38
#define TCPOLEN_MPTCP_DSS_ACK64		8
39
#define TCPOLEN_MPTCP_DSS_MAP32		10
40 41
#define TCPOLEN_MPTCP_DSS_MAP64		14
#define TCPOLEN_MPTCP_DSS_CHECKSUM	2
P
Peter Krystad 已提交
42 43 44 45 46

/* MPTCP MP_CAPABLE flags */
#define MPTCP_VERSION_MASK	(0x0F)
#define MPTCP_CAP_CHECKSUM_REQD	BIT(7)
#define MPTCP_CAP_EXTENSIBILITY	BIT(6)
47
#define MPTCP_CAP_HMAC_SHA256	BIT(0)
P
Peter Krystad 已提交
48 49
#define MPTCP_CAP_FLAG_MASK	(0x3F)

50 51 52 53 54 55
/* 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)
56 57 58
#define MPTCP_DSS_FLAG_MASK	(0x1F)

/* MPTCP socket flags */
59 60
#define MPTCP_DATA_READY	0
#define MPTCP_SEND_SPACE	1
61

M
Mat Martineau 已提交
62 63 64 65
/* MPTCP connection sock */
struct mptcp_sock {
	/* inet_connection_sock must be the first member */
	struct inet_connection_sock sk;
66 67
	u64		local_key;
	u64		remote_key;
68 69
	u64		write_seq;
	u64		ack_seq;
70
	u32		token;
71
	unsigned long	flags;
72
	bool		can_ack;
P
Paolo Abeni 已提交
73
	struct work_struct work;
74
	struct list_head conn_list;
75
	struct skb_ext	*cached_ext;	/* for the next sendmsg */
M
Mat Martineau 已提交
76
	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
77
	struct sock	*first;
M
Mat Martineau 已提交
78 79
};

80 81 82
#define mptcp_for_each_subflow(__msk, __subflow)			\
	list_for_each_entry(__subflow, &((__msk)->conn_list), node)

M
Mat Martineau 已提交
83 84 85 86 87
static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
{
	return (struct mptcp_sock *)sk;
}

88 89
struct mptcp_subflow_request_sock {
	struct	tcp_request_sock sk;
90
	u16	mp_capable : 1,
91
		mp_join : 1,
92 93
		backup : 1,
		remote_key_valid : 1;
94 95
	u64	local_key;
	u64	remote_key;
96 97
	u64	idsn;
	u32	token;
98
	u32	ssn_offset;
99 100 101 102 103 104 105 106
};

static inline struct mptcp_subflow_request_sock *
mptcp_subflow_rsk(const struct request_sock *rsk)
{
	return (struct mptcp_subflow_request_sock *)rsk;
}

107 108
/* MPTCP subflow context */
struct mptcp_subflow_context {
109 110 111
	struct	list_head node;/* conn_list of subflows */
	u64	local_key;
	u64	remote_key;
112
	u64	idsn;
113
	u64	map_seq;
114
	u32	snd_isn;
115
	u32	token;
116
	u32	rel_write_seq;
117 118 119
	u32	map_subflow_seq;
	u32	ssn_offset;
	u32	map_data_len;
120 121 122
	u32	request_mptcp : 1,  /* send MP_CAPABLE */
		mp_capable : 1,	    /* remote is MPTCP capable */
		fourth_ack : 1,	    /* send initial DSS */
123 124
		conn_finished : 1,
		map_valid : 1,
125
		mpc_map : 1,
126
		data_avail : 1,
127 128
		rx_eof : 1,
		can_ack : 1;	    /* only after processing the remote a key */
129

130 131
	struct	sock *tcp_sock;	    /* tcp sk backpointer */
	struct	sock *conn;	    /* parent mptcp_sock */
132
	const	struct inet_connection_sock_af_ops *icsk_af_ops;
133 134 135 136
	void	(*tcp_data_ready)(struct sock *sk);
	void	(*tcp_state_change)(struct sock *sk);
	void	(*tcp_write_space)(struct sock *sk);

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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;
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
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);
171 172 173
void mptcp_subflow_init(void);
int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);

174 175 176 177 178 179 180 181 182 183
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;
}

184 185 186 187 188
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

189
void mptcp_proto_init(void);
190 191 192
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
int mptcp_proto_v6_init(void);
#endif
193

194 195 196 197
void mptcp_get_options(const struct sk_buff *skb,
		       struct tcp_options_received *opt_rx);

void mptcp_finish_connect(struct sock *sk);
198
void mptcp_data_ready(struct sock *sk, struct sock *ssk);
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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,
221
			   void *hash_out);
222

223 224 225 226 227
static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
{
	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
}

228 229 230 231 232 233 234
static inline bool before64(__u64 seq1, __u64 seq2)
{
	return (__s64)(seq1 - seq2) < 0;
}

#define after64(seq2, seq1)	before64(seq1, seq2)

M
Mat Martineau 已提交
235
#endif /* __MPTCP_PROTOCOL_H */