xref: /linux/net/mptcp/protocol.h (revision 86287543715ac2a6d92d561cc105d79306511457)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Multipath TCP
3  *
4  * Copyright (c) 2017 - 2019, Intel Corporation.
5  */
6 
7 #ifndef __MPTCP_PROTOCOL_H
8 #define __MPTCP_PROTOCOL_H
9 
10 #include <linux/random.h>
11 #include <net/tcp.h>
12 #include <net/inet_connection_sock.h>
13 
14 #define MPTCP_SUPPORTED_VERSION	1
15 
16 /* MPTCP option bits */
17 #define OPTION_MPTCP_MPC_SYN	BIT(0)
18 #define OPTION_MPTCP_MPC_SYNACK	BIT(1)
19 #define OPTION_MPTCP_MPC_ACK	BIT(2)
20 #define OPTION_MPTCP_MPJ_SYN	BIT(3)
21 #define OPTION_MPTCP_MPJ_SYNACK	BIT(4)
22 #define OPTION_MPTCP_MPJ_ACK	BIT(5)
23 #define OPTION_MPTCP_ADD_ADDR	BIT(6)
24 #define OPTION_MPTCP_ADD_ADDR6	BIT(7)
25 #define OPTION_MPTCP_RM_ADDR	BIT(8)
26 
27 /* MPTCP option subtypes */
28 #define MPTCPOPT_MP_CAPABLE	0
29 #define MPTCPOPT_MP_JOIN	1
30 #define MPTCPOPT_DSS		2
31 #define MPTCPOPT_ADD_ADDR	3
32 #define MPTCPOPT_RM_ADDR	4
33 #define MPTCPOPT_MP_PRIO	5
34 #define MPTCPOPT_MP_FAIL	6
35 #define MPTCPOPT_MP_FASTCLOSE	7
36 
37 /* MPTCP suboption lengths */
38 #define TCPOLEN_MPTCP_MPC_SYN		4
39 #define TCPOLEN_MPTCP_MPC_SYNACK	12
40 #define TCPOLEN_MPTCP_MPC_ACK		20
41 #define TCPOLEN_MPTCP_MPC_ACK_DATA	22
42 #define TCPOLEN_MPTCP_MPJ_SYN		12
43 #define TCPOLEN_MPTCP_MPJ_SYNACK	16
44 #define TCPOLEN_MPTCP_MPJ_ACK		24
45 #define TCPOLEN_MPTCP_DSS_BASE		4
46 #define TCPOLEN_MPTCP_DSS_ACK32		4
47 #define TCPOLEN_MPTCP_DSS_ACK64		8
48 #define TCPOLEN_MPTCP_DSS_MAP32		10
49 #define TCPOLEN_MPTCP_DSS_MAP64		14
50 #define TCPOLEN_MPTCP_DSS_CHECKSUM	2
51 #define TCPOLEN_MPTCP_ADD_ADDR		16
52 #define TCPOLEN_MPTCP_ADD_ADDR_PORT	18
53 #define TCPOLEN_MPTCP_ADD_ADDR_BASE	8
54 #define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT	10
55 #define TCPOLEN_MPTCP_ADD_ADDR6		28
56 #define TCPOLEN_MPTCP_ADD_ADDR6_PORT	30
57 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE	20
58 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT	22
59 #define TCPOLEN_MPTCP_PORT_LEN		2
60 #define TCPOLEN_MPTCP_RM_ADDR_BASE	4
61 
62 /* MPTCP MP_JOIN flags */
63 #define MPTCPOPT_BACKUP		BIT(0)
64 #define MPTCPOPT_HMAC_LEN	20
65 #define MPTCPOPT_THMAC_LEN	8
66 
67 /* MPTCP MP_CAPABLE flags */
68 #define MPTCP_VERSION_MASK	(0x0F)
69 #define MPTCP_CAP_CHECKSUM_REQD	BIT(7)
70 #define MPTCP_CAP_EXTENSIBILITY	BIT(6)
71 #define MPTCP_CAP_HMAC_SHA256	BIT(0)
72 #define MPTCP_CAP_FLAG_MASK	(0x3F)
73 
74 /* MPTCP DSS flags */
75 #define MPTCP_DSS_DATA_FIN	BIT(4)
76 #define MPTCP_DSS_DSN64		BIT(3)
77 #define MPTCP_DSS_HAS_MAP	BIT(2)
78 #define MPTCP_DSS_ACK64		BIT(1)
79 #define MPTCP_DSS_HAS_ACK	BIT(0)
80 #define MPTCP_DSS_FLAG_MASK	(0x1F)
81 
82 /* MPTCP ADD_ADDR flags */
83 #define MPTCP_ADDR_ECHO		BIT(0)
84 #define MPTCP_ADDR_HMAC_LEN	20
85 #define MPTCP_ADDR_IPVERSION_4	4
86 #define MPTCP_ADDR_IPVERSION_6	6
87 
88 /* MPTCP socket flags */
89 #define MPTCP_DATA_READY	0
90 #define MPTCP_SEND_SPACE	1
91 #define MPTCP_WORK_RTX		2
92 
93 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
94 {
95 	return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
96 		     ((nib & 0xF) << 8) | field);
97 }
98 
99 #define MPTCP_PM_MAX_ADDR	4
100 
101 struct mptcp_addr_info {
102 	sa_family_t		family;
103 	__be16			port;
104 	u8			id;
105 	union {
106 		struct in_addr addr;
107 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
108 		struct in6_addr addr6;
109 #endif
110 	};
111 };
112 
113 enum mptcp_pm_status {
114 	MPTCP_PM_ADD_ADDR_RECEIVED,
115 	MPTCP_PM_ESTABLISHED,
116 	MPTCP_PM_SUBFLOW_ESTABLISHED,
117 };
118 
119 struct mptcp_pm_data {
120 	struct mptcp_addr_info local;
121 	struct mptcp_addr_info remote;
122 
123 	spinlock_t	lock;		/*protects the whole PM data */
124 
125 	bool		addr_signal;
126 	bool		server_side;
127 	bool		work_pending;
128 	bool		accept_addr;
129 	bool		accept_subflow;
130 	u8		add_addr_signaled;
131 	u8		add_addr_accepted;
132 	u8		local_addr_used;
133 	u8		subflows;
134 	u8		add_addr_signal_max;
135 	u8		add_addr_accept_max;
136 	u8		local_addr_max;
137 	u8		subflows_max;
138 	u8		status;
139 
140 	struct		work_struct work;
141 };
142 
143 struct mptcp_data_frag {
144 	struct list_head list;
145 	u64 data_seq;
146 	int data_len;
147 	int offset;
148 	int overhead;
149 	struct page *page;
150 };
151 
152 /* MPTCP connection sock */
153 struct mptcp_sock {
154 	/* inet_connection_sock must be the first member */
155 	struct inet_connection_sock sk;
156 	u64		local_key;
157 	u64		remote_key;
158 	u64		write_seq;
159 	u64		ack_seq;
160 	atomic64_t	snd_una;
161 	unsigned long	timer_ival;
162 	u32		token;
163 	unsigned long	flags;
164 	bool		can_ack;
165 	spinlock_t	join_list_lock;
166 	struct work_struct work;
167 	struct list_head conn_list;
168 	struct list_head rtx_queue;
169 	struct list_head join_list;
170 	struct skb_ext	*cached_ext;	/* for the next sendmsg */
171 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
172 	struct sock	*first;
173 	struct mptcp_pm_data	pm;
174 };
175 
176 #define mptcp_for_each_subflow(__msk, __subflow)			\
177 	list_for_each_entry(__subflow, &((__msk)->conn_list), node)
178 
179 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
180 {
181 	return (struct mptcp_sock *)sk;
182 }
183 
184 static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
185 {
186 	struct mptcp_sock *msk = mptcp_sk(sk);
187 
188 	if (list_empty(&msk->rtx_queue))
189 		return NULL;
190 
191 	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
192 }
193 
194 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
195 {
196 	struct mptcp_sock *msk = mptcp_sk(sk);
197 
198 	if (list_empty(&msk->rtx_queue))
199 		return NULL;
200 
201 	return list_first_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
202 }
203 
204 struct mptcp_subflow_request_sock {
205 	struct	tcp_request_sock sk;
206 	u16	mp_capable : 1,
207 		mp_join : 1,
208 		backup : 1,
209 		remote_key_valid : 1;
210 	u8	local_id;
211 	u8	remote_id;
212 	u64	local_key;
213 	u64	remote_key;
214 	u64	idsn;
215 	u32	token;
216 	u32	ssn_offset;
217 	u64	thmac;
218 	u32	local_nonce;
219 	u32	remote_nonce;
220 };
221 
222 static inline struct mptcp_subflow_request_sock *
223 mptcp_subflow_rsk(const struct request_sock *rsk)
224 {
225 	return (struct mptcp_subflow_request_sock *)rsk;
226 }
227 
228 /* MPTCP subflow context */
229 struct mptcp_subflow_context {
230 	struct	list_head node;/* conn_list of subflows */
231 	u64	local_key;
232 	u64	remote_key;
233 	u64	idsn;
234 	u64	map_seq;
235 	u32	snd_isn;
236 	u32	token;
237 	u32	rel_write_seq;
238 	u32	map_subflow_seq;
239 	u32	ssn_offset;
240 	u32	map_data_len;
241 	u32	request_mptcp : 1,  /* send MP_CAPABLE */
242 		request_join : 1,   /* send MP_JOIN */
243 		request_bkup : 1,
244 		mp_capable : 1,	    /* remote is MPTCP capable */
245 		mp_join : 1,	    /* remote is JOINing */
246 		fully_established : 1,	    /* path validated */
247 		pm_notified : 1,    /* PM hook called for established status */
248 		conn_finished : 1,
249 		map_valid : 1,
250 		mpc_map : 1,
251 		backup : 1,
252 		data_avail : 1,
253 		rx_eof : 1,
254 		data_fin_tx_enable : 1,
255 		can_ack : 1;	    /* only after processing the remote a key */
256 	u64	data_fin_tx_seq;
257 	u32	remote_nonce;
258 	u64	thmac;
259 	u32	local_nonce;
260 	u32	remote_token;
261 	u8	hmac[MPTCPOPT_HMAC_LEN];
262 	u8	local_id;
263 	u8	remote_id;
264 
265 	struct	sock *tcp_sock;	    /* tcp sk backpointer */
266 	struct	sock *conn;	    /* parent mptcp_sock */
267 	const	struct inet_connection_sock_af_ops *icsk_af_ops;
268 	void	(*tcp_data_ready)(struct sock *sk);
269 	void	(*tcp_state_change)(struct sock *sk);
270 	void	(*tcp_write_space)(struct sock *sk);
271 
272 	struct	rcu_head rcu;
273 };
274 
275 static inline struct mptcp_subflow_context *
276 mptcp_subflow_ctx(const struct sock *sk)
277 {
278 	struct inet_connection_sock *icsk = inet_csk(sk);
279 
280 	/* Use RCU on icsk_ulp_data only for sock diag code */
281 	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
282 }
283 
284 static inline struct sock *
285 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
286 {
287 	return subflow->tcp_sock;
288 }
289 
290 static inline u64
291 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
292 {
293 	return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
294 		      subflow->ssn_offset -
295 		      subflow->map_subflow_seq;
296 }
297 
298 static inline u64
299 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
300 {
301 	return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
302 }
303 
304 int mptcp_is_enabled(struct net *net);
305 bool mptcp_subflow_data_available(struct sock *sk);
306 void mptcp_subflow_init(void);
307 
308 /* called with sk socket lock held */
309 int __mptcp_subflow_connect(struct sock *sk, int ifindex,
310 			    const struct mptcp_addr_info *loc,
311 			    const struct mptcp_addr_info *remote);
312 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
313 
314 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
315 					      struct mptcp_subflow_context *ctx)
316 {
317 	sk->sk_data_ready = ctx->tcp_data_ready;
318 	sk->sk_state_change = ctx->tcp_state_change;
319 	sk->sk_write_space = ctx->tcp_write_space;
320 
321 	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
322 }
323 
324 extern const struct inet_connection_sock_af_ops ipv4_specific;
325 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
326 extern const struct inet_connection_sock_af_ops ipv6_specific;
327 #endif
328 
329 void mptcp_proto_init(void);
330 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
331 int mptcp_proto_v6_init(void);
332 #endif
333 
334 struct sock *mptcp_sk_clone(const struct sock *sk, struct request_sock *req);
335 void mptcp_get_options(const struct sk_buff *skb,
336 		       struct tcp_options_received *opt_rx);
337 
338 void mptcp_finish_connect(struct sock *sk);
339 void mptcp_data_ready(struct sock *sk, struct sock *ssk);
340 bool mptcp_finish_join(struct sock *sk);
341 void mptcp_data_acked(struct sock *sk);
342 
343 int mptcp_token_new_request(struct request_sock *req);
344 void mptcp_token_destroy_request(u32 token);
345 int mptcp_token_new_connect(struct sock *sk);
346 int mptcp_token_new_accept(u32 token, struct sock *conn);
347 struct mptcp_sock *mptcp_token_get_sock(u32 token);
348 void mptcp_token_destroy(u32 token);
349 
350 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
351 static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
352 {
353 	/* we might consider a faster version that computes the key as a
354 	 * hash of some information available in the MPTCP socket. Use
355 	 * random data at the moment, as it's probably the safest option
356 	 * in case multiple sockets are opened in different namespaces at
357 	 * the same time.
358 	 */
359 	get_random_bytes(key, sizeof(u64));
360 	mptcp_crypto_key_sha(*key, token, idsn);
361 }
362 
363 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
364 
365 void mptcp_pm_init(void);
366 void mptcp_pm_data_init(struct mptcp_sock *msk);
367 void mptcp_pm_close(struct mptcp_sock *msk);
368 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
369 void mptcp_pm_fully_established(struct mptcp_sock *msk);
370 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
371 void mptcp_pm_connection_closed(struct mptcp_sock *msk);
372 void mptcp_pm_subflow_established(struct mptcp_sock *msk,
373 				  struct mptcp_subflow_context *subflow);
374 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
375 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
376 				const struct mptcp_addr_info *addr);
377 
378 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
379 			   const struct mptcp_addr_info *addr);
380 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
381 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 remote_id);
382 
383 static inline bool mptcp_pm_should_signal(struct mptcp_sock *msk)
384 {
385 	return READ_ONCE(msk->pm.addr_signal);
386 }
387 
388 static inline unsigned int mptcp_add_addr_len(int family)
389 {
390 	if (family == AF_INET)
391 		return TCPOLEN_MPTCP_ADD_ADDR;
392 	return TCPOLEN_MPTCP_ADD_ADDR6;
393 }
394 
395 bool mptcp_pm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
396 			  struct mptcp_addr_info *saddr);
397 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
398 
399 void mptcp_pm_nl_init(void);
400 void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
401 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk);
402 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk);
403 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk);
404 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
405 
406 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
407 {
408 	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
409 }
410 
411 static inline bool before64(__u64 seq1, __u64 seq2)
412 {
413 	return (__s64)(seq1 - seq2) < 0;
414 }
415 
416 #define after64(seq2, seq1)	before64(seq1, seq2)
417 
418 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
419 
420 #endif /* __MPTCP_PROTOCOL_H */
421