xref: /openbmc/linux/net/mptcp/protocol.h (revision 800e26b8)
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_IPVERSION_4	4
85 #define MPTCP_ADDR_IPVERSION_6	6
86 
87 /* MPTCP socket flags */
88 #define MPTCP_DATA_READY	0
89 #define MPTCP_SEND_SPACE	1
90 #define MPTCP_WORK_RTX		2
91 #define MPTCP_WORK_EOF		3
92 
93 struct mptcp_options_received {
94 	u64	sndr_key;
95 	u64	rcvr_key;
96 	u64	data_ack;
97 	u64	data_seq;
98 	u32	subflow_seq;
99 	u16	data_len;
100 	u16	mp_capable : 1,
101 		mp_join : 1,
102 		dss : 1,
103 		add_addr : 1,
104 		rm_addr : 1,
105 		family : 4,
106 		echo : 1,
107 		backup : 1;
108 	u32	token;
109 	u32	nonce;
110 	u64	thmac;
111 	u8	hmac[20];
112 	u8	join_id;
113 	u8	use_map:1,
114 		dsn64:1,
115 		data_fin:1,
116 		use_ack:1,
117 		ack64:1,
118 		mpc_map:1,
119 		__unused:2;
120 	u8	addr_id;
121 	u8	rm_id;
122 	union {
123 		struct in_addr	addr;
124 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
125 		struct in6_addr	addr6;
126 #endif
127 	};
128 	u64	ahmac;
129 	u16	port;
130 };
131 
132 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
133 {
134 	return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
135 		     ((nib & 0xF) << 8) | field);
136 }
137 
138 struct mptcp_addr_info {
139 	sa_family_t		family;
140 	__be16			port;
141 	u8			id;
142 	union {
143 		struct in_addr addr;
144 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
145 		struct in6_addr addr6;
146 #endif
147 	};
148 };
149 
150 enum mptcp_pm_status {
151 	MPTCP_PM_ADD_ADDR_RECEIVED,
152 	MPTCP_PM_ESTABLISHED,
153 	MPTCP_PM_SUBFLOW_ESTABLISHED,
154 };
155 
156 struct mptcp_pm_data {
157 	struct mptcp_addr_info local;
158 	struct mptcp_addr_info remote;
159 
160 	spinlock_t	lock;		/*protects the whole PM data */
161 
162 	bool		addr_signal;
163 	bool		server_side;
164 	bool		work_pending;
165 	bool		accept_addr;
166 	bool		accept_subflow;
167 	u8		add_addr_signaled;
168 	u8		add_addr_accepted;
169 	u8		local_addr_used;
170 	u8		subflows;
171 	u8		add_addr_signal_max;
172 	u8		add_addr_accept_max;
173 	u8		local_addr_max;
174 	u8		subflows_max;
175 	u8		status;
176 
177 	struct		work_struct work;
178 };
179 
180 struct mptcp_data_frag {
181 	struct list_head list;
182 	u64 data_seq;
183 	int data_len;
184 	int offset;
185 	int overhead;
186 	struct page *page;
187 };
188 
189 /* MPTCP connection sock */
190 struct mptcp_sock {
191 	/* inet_connection_sock must be the first member */
192 	struct inet_connection_sock sk;
193 	u64		local_key;
194 	u64		remote_key;
195 	u64		write_seq;
196 	u64		ack_seq;
197 	atomic64_t	snd_una;
198 	unsigned long	timer_ival;
199 	u32		token;
200 	unsigned long	flags;
201 	bool		can_ack;
202 	spinlock_t	join_list_lock;
203 	struct work_struct work;
204 	struct list_head conn_list;
205 	struct list_head rtx_queue;
206 	struct list_head join_list;
207 	struct skb_ext	*cached_ext;	/* for the next sendmsg */
208 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
209 	struct sock	*first;
210 	struct mptcp_pm_data	pm;
211 };
212 
213 #define mptcp_for_each_subflow(__msk, __subflow)			\
214 	list_for_each_entry(__subflow, &((__msk)->conn_list), node)
215 
216 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
217 {
218 	return (struct mptcp_sock *)sk;
219 }
220 
221 static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
222 {
223 	struct mptcp_sock *msk = mptcp_sk(sk);
224 
225 	if (list_empty(&msk->rtx_queue))
226 		return NULL;
227 
228 	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
229 }
230 
231 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
232 {
233 	struct mptcp_sock *msk = mptcp_sk(sk);
234 
235 	return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
236 }
237 
238 struct mptcp_subflow_request_sock {
239 	struct	tcp_request_sock sk;
240 	u16	mp_capable : 1,
241 		mp_join : 1,
242 		backup : 1;
243 	u8	local_id;
244 	u8	remote_id;
245 	u64	local_key;
246 	u64	idsn;
247 	u32	token;
248 	u32	ssn_offset;
249 	u64	thmac;
250 	u32	local_nonce;
251 	u32	remote_nonce;
252 };
253 
254 static inline struct mptcp_subflow_request_sock *
255 mptcp_subflow_rsk(const struct request_sock *rsk)
256 {
257 	return (struct mptcp_subflow_request_sock *)rsk;
258 }
259 
260 /* MPTCP subflow context */
261 struct mptcp_subflow_context {
262 	struct	list_head node;/* conn_list of subflows */
263 	u64	local_key;
264 	u64	remote_key;
265 	u64	idsn;
266 	u64	map_seq;
267 	u32	snd_isn;
268 	u32	token;
269 	u32	rel_write_seq;
270 	u32	map_subflow_seq;
271 	u32	ssn_offset;
272 	u32	map_data_len;
273 	u32	request_mptcp : 1,  /* send MP_CAPABLE */
274 		request_join : 1,   /* send MP_JOIN */
275 		request_bkup : 1,
276 		mp_capable : 1,	    /* remote is MPTCP capable */
277 		mp_join : 1,	    /* remote is JOINing */
278 		fully_established : 1,	    /* path validated */
279 		pm_notified : 1,    /* PM hook called for established status */
280 		conn_finished : 1,
281 		map_valid : 1,
282 		mpc_map : 1,
283 		backup : 1,
284 		data_avail : 1,
285 		rx_eof : 1,
286 		data_fin_tx_enable : 1,
287 		use_64bit_ack : 1, /* Set when we received a 64-bit DSN */
288 		can_ack : 1;	    /* only after processing the remote a key */
289 	u64	data_fin_tx_seq;
290 	u32	remote_nonce;
291 	u64	thmac;
292 	u32	local_nonce;
293 	u32	remote_token;
294 	u8	hmac[MPTCPOPT_HMAC_LEN];
295 	u8	local_id;
296 	u8	remote_id;
297 
298 	struct	sock *tcp_sock;	    /* tcp sk backpointer */
299 	struct	sock *conn;	    /* parent mptcp_sock */
300 	const	struct inet_connection_sock_af_ops *icsk_af_ops;
301 	void	(*tcp_data_ready)(struct sock *sk);
302 	void	(*tcp_state_change)(struct sock *sk);
303 	void	(*tcp_write_space)(struct sock *sk);
304 
305 	struct	rcu_head rcu;
306 };
307 
308 static inline struct mptcp_subflow_context *
309 mptcp_subflow_ctx(const struct sock *sk)
310 {
311 	struct inet_connection_sock *icsk = inet_csk(sk);
312 
313 	/* Use RCU on icsk_ulp_data only for sock diag code */
314 	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
315 }
316 
317 static inline struct sock *
318 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
319 {
320 	return subflow->tcp_sock;
321 }
322 
323 static inline u64
324 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
325 {
326 	return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
327 		      subflow->ssn_offset -
328 		      subflow->map_subflow_seq;
329 }
330 
331 static inline u64
332 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
333 {
334 	return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
335 }
336 
337 int mptcp_is_enabled(struct net *net);
338 bool mptcp_subflow_data_available(struct sock *sk);
339 void mptcp_subflow_init(void);
340 
341 /* called with sk socket lock held */
342 int __mptcp_subflow_connect(struct sock *sk, int ifindex,
343 			    const struct mptcp_addr_info *loc,
344 			    const struct mptcp_addr_info *remote);
345 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
346 
347 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
348 					      struct mptcp_subflow_context *ctx)
349 {
350 	sk->sk_data_ready = ctx->tcp_data_ready;
351 	sk->sk_state_change = ctx->tcp_state_change;
352 	sk->sk_write_space = ctx->tcp_write_space;
353 
354 	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
355 }
356 
357 extern const struct inet_connection_sock_af_ops ipv4_specific;
358 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
359 extern const struct inet_connection_sock_af_ops ipv6_specific;
360 #endif
361 
362 void mptcp_proto_init(void);
363 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
364 int mptcp_proto_v6_init(void);
365 #endif
366 
367 struct sock *mptcp_sk_clone(const struct sock *sk,
368 			    const struct mptcp_options_received *mp_opt,
369 			    struct request_sock *req);
370 void mptcp_get_options(const struct sk_buff *skb,
371 		       struct mptcp_options_received *mp_opt);
372 
373 void mptcp_finish_connect(struct sock *sk);
374 void mptcp_data_ready(struct sock *sk, struct sock *ssk);
375 bool mptcp_finish_join(struct sock *sk);
376 void mptcp_data_acked(struct sock *sk);
377 void mptcp_subflow_eof(struct sock *sk);
378 
379 int mptcp_token_new_request(struct request_sock *req);
380 void mptcp_token_destroy_request(u32 token);
381 int mptcp_token_new_connect(struct sock *sk);
382 int mptcp_token_new_accept(u32 token, struct sock *conn);
383 struct mptcp_sock *mptcp_token_get_sock(u32 token);
384 void mptcp_token_destroy(u32 token);
385 
386 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
387 static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
388 {
389 	/* we might consider a faster version that computes the key as a
390 	 * hash of some information available in the MPTCP socket. Use
391 	 * random data at the moment, as it's probably the safest option
392 	 * in case multiple sockets are opened in different namespaces at
393 	 * the same time.
394 	 */
395 	get_random_bytes(key, sizeof(u64));
396 	mptcp_crypto_key_sha(*key, token, idsn);
397 }
398 
399 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
400 
401 void mptcp_pm_init(void);
402 void mptcp_pm_data_init(struct mptcp_sock *msk);
403 void mptcp_pm_close(struct mptcp_sock *msk);
404 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
405 void mptcp_pm_fully_established(struct mptcp_sock *msk);
406 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
407 void mptcp_pm_connection_closed(struct mptcp_sock *msk);
408 void mptcp_pm_subflow_established(struct mptcp_sock *msk,
409 				  struct mptcp_subflow_context *subflow);
410 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
411 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
412 				const struct mptcp_addr_info *addr);
413 
414 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
415 			   const struct mptcp_addr_info *addr);
416 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
417 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 remote_id);
418 
419 static inline bool mptcp_pm_should_signal(struct mptcp_sock *msk)
420 {
421 	return READ_ONCE(msk->pm.addr_signal);
422 }
423 
424 static inline unsigned int mptcp_add_addr_len(int family)
425 {
426 	if (family == AF_INET)
427 		return TCPOLEN_MPTCP_ADD_ADDR;
428 	return TCPOLEN_MPTCP_ADD_ADDR6;
429 }
430 
431 bool mptcp_pm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
432 			  struct mptcp_addr_info *saddr);
433 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
434 
435 void mptcp_pm_nl_init(void);
436 void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
437 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk);
438 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk);
439 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk);
440 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
441 
442 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
443 {
444 	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
445 }
446 
447 static inline bool before64(__u64 seq1, __u64 seq2)
448 {
449 	return (__s64)(seq1 - seq2) < 0;
450 }
451 
452 #define after64(seq2, seq1)	before64(seq1, seq2)
453 
454 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
455 
456 #endif /* __MPTCP_PROTOCOL_H */
457