xref: /openbmc/linux/net/mptcp/protocol.h (revision 3db55767)
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 #include <uapi/linux/mptcp.h>
14 #include <net/genetlink.h>
15 
16 #define MPTCP_SUPPORTED_VERSION	1
17 
18 /* MPTCP option bits */
19 #define OPTION_MPTCP_MPC_SYN	BIT(0)
20 #define OPTION_MPTCP_MPC_SYNACK	BIT(1)
21 #define OPTION_MPTCP_MPC_ACK	BIT(2)
22 #define OPTION_MPTCP_MPJ_SYN	BIT(3)
23 #define OPTION_MPTCP_MPJ_SYNACK	BIT(4)
24 #define OPTION_MPTCP_MPJ_ACK	BIT(5)
25 #define OPTION_MPTCP_ADD_ADDR	BIT(6)
26 #define OPTION_MPTCP_RM_ADDR	BIT(7)
27 #define OPTION_MPTCP_FASTCLOSE	BIT(8)
28 #define OPTION_MPTCP_PRIO	BIT(9)
29 #define OPTION_MPTCP_RST	BIT(10)
30 #define OPTION_MPTCP_DSS	BIT(11)
31 #define OPTION_MPTCP_FAIL	BIT(12)
32 
33 #define OPTION_MPTCP_CSUMREQD	BIT(13)
34 
35 #define OPTIONS_MPTCP_MPC	(OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK | \
36 				 OPTION_MPTCP_MPC_ACK)
37 #define OPTIONS_MPTCP_MPJ	(OPTION_MPTCP_MPJ_SYN | OPTION_MPTCP_MPJ_SYNACK | \
38 				 OPTION_MPTCP_MPJ_ACK)
39 
40 /* MPTCP option subtypes */
41 #define MPTCPOPT_MP_CAPABLE	0
42 #define MPTCPOPT_MP_JOIN	1
43 #define MPTCPOPT_DSS		2
44 #define MPTCPOPT_ADD_ADDR	3
45 #define MPTCPOPT_RM_ADDR	4
46 #define MPTCPOPT_MP_PRIO	5
47 #define MPTCPOPT_MP_FAIL	6
48 #define MPTCPOPT_MP_FASTCLOSE	7
49 #define MPTCPOPT_RST		8
50 
51 /* MPTCP suboption lengths */
52 #define TCPOLEN_MPTCP_MPC_SYN		4
53 #define TCPOLEN_MPTCP_MPC_SYNACK	12
54 #define TCPOLEN_MPTCP_MPC_ACK		20
55 #define TCPOLEN_MPTCP_MPC_ACK_DATA	22
56 #define TCPOLEN_MPTCP_MPJ_SYN		12
57 #define TCPOLEN_MPTCP_MPJ_SYNACK	16
58 #define TCPOLEN_MPTCP_MPJ_ACK		24
59 #define TCPOLEN_MPTCP_DSS_BASE		4
60 #define TCPOLEN_MPTCP_DSS_ACK32		4
61 #define TCPOLEN_MPTCP_DSS_ACK64		8
62 #define TCPOLEN_MPTCP_DSS_MAP32		10
63 #define TCPOLEN_MPTCP_DSS_MAP64		14
64 #define TCPOLEN_MPTCP_DSS_CHECKSUM	2
65 #define TCPOLEN_MPTCP_ADD_ADDR		16
66 #define TCPOLEN_MPTCP_ADD_ADDR_PORT	18
67 #define TCPOLEN_MPTCP_ADD_ADDR_BASE	8
68 #define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT	10
69 #define TCPOLEN_MPTCP_ADD_ADDR6		28
70 #define TCPOLEN_MPTCP_ADD_ADDR6_PORT	30
71 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE	20
72 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT	22
73 #define TCPOLEN_MPTCP_PORT_LEN		2
74 #define TCPOLEN_MPTCP_PORT_ALIGN	2
75 #define TCPOLEN_MPTCP_RM_ADDR_BASE	3
76 #define TCPOLEN_MPTCP_PRIO		3
77 #define TCPOLEN_MPTCP_PRIO_ALIGN	4
78 #define TCPOLEN_MPTCP_FASTCLOSE		12
79 #define TCPOLEN_MPTCP_RST		4
80 #define TCPOLEN_MPTCP_FAIL		12
81 
82 #define TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM	(TCPOLEN_MPTCP_DSS_CHECKSUM + TCPOLEN_MPTCP_MPC_ACK_DATA)
83 
84 /* MPTCP MP_JOIN flags */
85 #define MPTCPOPT_BACKUP		BIT(0)
86 #define MPTCPOPT_THMAC_LEN	8
87 
88 /* MPTCP MP_CAPABLE flags */
89 #define MPTCP_VERSION_MASK	(0x0F)
90 #define MPTCP_CAP_CHECKSUM_REQD	BIT(7)
91 #define MPTCP_CAP_EXTENSIBILITY	BIT(6)
92 #define MPTCP_CAP_DENY_JOIN_ID0	BIT(5)
93 #define MPTCP_CAP_HMAC_SHA256	BIT(0)
94 #define MPTCP_CAP_FLAG_MASK	(0x1F)
95 
96 /* MPTCP DSS flags */
97 #define MPTCP_DSS_DATA_FIN	BIT(4)
98 #define MPTCP_DSS_DSN64		BIT(3)
99 #define MPTCP_DSS_HAS_MAP	BIT(2)
100 #define MPTCP_DSS_ACK64		BIT(1)
101 #define MPTCP_DSS_HAS_ACK	BIT(0)
102 #define MPTCP_DSS_FLAG_MASK	(0x1F)
103 
104 /* MPTCP ADD_ADDR flags */
105 #define MPTCP_ADDR_ECHO		BIT(0)
106 
107 /* MPTCP MP_PRIO flags */
108 #define MPTCP_PRIO_BKUP		BIT(0)
109 
110 /* MPTCP TCPRST flags */
111 #define MPTCP_RST_TRANSIENT	BIT(0)
112 
113 /* MPTCP socket atomic flags */
114 #define MPTCP_NOSPACE		1
115 #define MPTCP_WORK_RTX		2
116 #define MPTCP_WORK_EOF		3
117 #define MPTCP_FALLBACK_DONE	4
118 #define MPTCP_WORK_CLOSE_SUBFLOW 5
119 
120 /* MPTCP socket release cb flags */
121 #define MPTCP_PUSH_PENDING	1
122 #define MPTCP_CLEAN_UNA		2
123 #define MPTCP_ERROR_REPORT	3
124 #define MPTCP_RETRANSMIT	4
125 #define MPTCP_FLUSH_JOIN_LIST	5
126 #define MPTCP_CONNECTED		6
127 #define MPTCP_RESET_SCHEDULER	7
128 
129 struct mptcp_skb_cb {
130 	u64 map_seq;
131 	u64 end_seq;
132 	u32 offset;
133 	u8  has_rxtstamp:1;
134 };
135 
136 #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
137 
138 static inline bool before64(__u64 seq1, __u64 seq2)
139 {
140 	return (__s64)(seq1 - seq2) < 0;
141 }
142 
143 #define after64(seq2, seq1)	before64(seq1, seq2)
144 
145 struct mptcp_options_received {
146 	u64	sndr_key;
147 	u64	rcvr_key;
148 	u64	data_ack;
149 	u64	data_seq;
150 	u32	subflow_seq;
151 	u16	data_len;
152 	__sum16	csum;
153 	u16	suboptions;
154 	u32	token;
155 	u32	nonce;
156 	u16	use_map:1,
157 		dsn64:1,
158 		data_fin:1,
159 		use_ack:1,
160 		ack64:1,
161 		mpc_map:1,
162 		reset_reason:4,
163 		reset_transient:1,
164 		echo:1,
165 		backup:1,
166 		deny_join_id0:1,
167 		__unused:2;
168 	u8	join_id;
169 	u64	thmac;
170 	u8	hmac[MPTCPOPT_HMAC_LEN];
171 	struct mptcp_addr_info addr;
172 	struct mptcp_rm_list rm_list;
173 	u64	ahmac;
174 	u64	fail_seq;
175 };
176 
177 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
178 {
179 	return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
180 		     ((nib & 0xF) << 8) | field);
181 }
182 
183 enum mptcp_pm_status {
184 	MPTCP_PM_ADD_ADDR_RECEIVED,
185 	MPTCP_PM_ADD_ADDR_SEND_ACK,
186 	MPTCP_PM_RM_ADDR_RECEIVED,
187 	MPTCP_PM_ESTABLISHED,
188 	MPTCP_PM_SUBFLOW_ESTABLISHED,
189 	MPTCP_PM_ALREADY_ESTABLISHED,	/* persistent status, set after ESTABLISHED event */
190 	MPTCP_PM_MPC_ENDPOINT_ACCOUNTED /* persistent status, set after MPC local address is
191 					 * accounted int id_avail_bitmap
192 					 */
193 };
194 
195 enum mptcp_pm_type {
196 	MPTCP_PM_TYPE_KERNEL = 0,
197 	MPTCP_PM_TYPE_USERSPACE,
198 
199 	__MPTCP_PM_TYPE_NR,
200 	__MPTCP_PM_TYPE_MAX = __MPTCP_PM_TYPE_NR - 1,
201 };
202 
203 /* Status bits below MPTCP_PM_ALREADY_ESTABLISHED need pm worker actions */
204 #define MPTCP_PM_WORK_MASK ((1 << MPTCP_PM_ALREADY_ESTABLISHED) - 1)
205 
206 enum mptcp_addr_signal_status {
207 	MPTCP_ADD_ADDR_SIGNAL,
208 	MPTCP_ADD_ADDR_ECHO,
209 	MPTCP_RM_ADDR_SIGNAL,
210 };
211 
212 /* max value of mptcp_addr_info.id */
213 #define MPTCP_PM_MAX_ADDR_ID		U8_MAX
214 
215 struct mptcp_pm_data {
216 	struct mptcp_addr_info local;
217 	struct mptcp_addr_info remote;
218 	struct list_head anno_list;
219 	struct list_head userspace_pm_local_addr_list;
220 
221 	spinlock_t	lock;		/*protects the whole PM data */
222 
223 	u8		addr_signal;
224 	bool		server_side;
225 	bool		work_pending;
226 	bool		accept_addr;
227 	bool		accept_subflow;
228 	bool		remote_deny_join_id0;
229 	u8		add_addr_signaled;
230 	u8		add_addr_accepted;
231 	u8		local_addr_used;
232 	u8		pm_type;
233 	u8		subflows;
234 	u8		status;
235 	DECLARE_BITMAP(id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
236 	struct mptcp_rm_list rm_list_tx;
237 	struct mptcp_rm_list rm_list_rx;
238 };
239 
240 struct mptcp_pm_addr_entry {
241 	struct list_head	list;
242 	struct mptcp_addr_info	addr;
243 	u8			flags;
244 	int			ifindex;
245 	struct socket		*lsk;
246 };
247 
248 struct mptcp_data_frag {
249 	struct list_head list;
250 	u64 data_seq;
251 	u16 data_len;
252 	u16 offset;
253 	u16 overhead;
254 	u16 already_sent;
255 	struct page *page;
256 };
257 
258 /* MPTCP connection sock */
259 struct mptcp_sock {
260 	/* inet_connection_sock must be the first member */
261 	struct inet_connection_sock sk;
262 	u64		local_key;
263 	u64		remote_key;
264 	u64		write_seq;
265 	u64		snd_nxt;
266 	u64		ack_seq;
267 	atomic64_t	rcv_wnd_sent;
268 	u64		rcv_data_fin_seq;
269 	int		rmem_fwd_alloc;
270 	struct sock	*last_snd;
271 	int		snd_burst;
272 	int		old_wspace;
273 	u64		recovery_snd_nxt;	/* in recovery mode accept up to this seq;
274 						 * recovery related fields are under data_lock
275 						 * protection
276 						 */
277 	u64		snd_una;
278 	u64		wnd_end;
279 	unsigned long	timer_ival;
280 	u32		token;
281 	int		rmem_released;
282 	unsigned long	flags;
283 	unsigned long	cb_flags;
284 	unsigned long	push_pending;
285 	bool		recovery;		/* closing subflow write queue reinjected */
286 	bool		can_ack;
287 	bool		fully_established;
288 	bool		rcv_data_fin;
289 	bool		snd_data_fin_enable;
290 	bool		rcv_fastclose;
291 	bool		use_64bit_ack; /* Set when we received a 64-bit DSN */
292 	bool		csum_enabled;
293 	bool		allow_infinite_fallback;
294 	u8		mpc_endpoint_id;
295 	u8		recvmsg_inq:1,
296 			cork:1,
297 			nodelay:1,
298 			fastopening:1,
299 			in_accept_queue:1;
300 	int		connect_flags;
301 	struct work_struct work;
302 	struct sk_buff  *ooo_last_skb;
303 	struct rb_root  out_of_order_queue;
304 	struct sk_buff_head receive_queue;
305 	struct list_head conn_list;
306 	struct list_head rtx_queue;
307 	struct mptcp_data_frag *first_pending;
308 	struct list_head join_list;
309 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
310 	struct sock	*first;
311 	struct mptcp_pm_data	pm;
312 	struct {
313 		u32	space;	/* bytes copied in last measurement window */
314 		u32	copied; /* bytes copied in this measurement window */
315 		u64	time;	/* start time of measurement window */
316 		u64	rtt_us; /* last maximum rtt of subflows */
317 	} rcvq_space;
318 
319 	u32 setsockopt_seq;
320 	char		ca_name[TCP_CA_NAME_MAX];
321 	struct mptcp_sock	*dl_next;
322 };
323 
324 #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
325 #define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
326 
327 #define mptcp_for_each_subflow(__msk, __subflow)			\
328 	list_for_each_entry(__subflow, &((__msk)->conn_list), node)
329 #define mptcp_for_each_subflow_safe(__msk, __subflow, __tmp)			\
330 	list_for_each_entry_safe(__subflow, __tmp, &((__msk)->conn_list), node)
331 
332 static inline void msk_owned_by_me(const struct mptcp_sock *msk)
333 {
334 	sock_owned_by_me((const struct sock *)msk);
335 }
336 
337 #define mptcp_sk(ptr) container_of_const(ptr, struct mptcp_sock, sk.icsk_inet.sk)
338 
339 /* the msk socket don't use the backlog, also account for the bulk
340  * free memory
341  */
342 static inline int __mptcp_rmem(const struct sock *sk)
343 {
344 	return atomic_read(&sk->sk_rmem_alloc) - READ_ONCE(mptcp_sk(sk)->rmem_released);
345 }
346 
347 static inline int __mptcp_space(const struct sock *sk)
348 {
349 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) - __mptcp_rmem(sk));
350 }
351 
352 static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
353 {
354 	const struct mptcp_sock *msk = mptcp_sk(sk);
355 
356 	return READ_ONCE(msk->first_pending);
357 }
358 
359 static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
360 {
361 	struct mptcp_sock *msk = mptcp_sk(sk);
362 	struct mptcp_data_frag *cur;
363 
364 	cur = msk->first_pending;
365 	return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
366 						     list_next_entry(cur, list);
367 }
368 
369 static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
370 {
371 	const struct mptcp_sock *msk = mptcp_sk(sk);
372 
373 	if (!msk->first_pending)
374 		return NULL;
375 
376 	if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
377 		return NULL;
378 
379 	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
380 }
381 
382 static inline struct mptcp_data_frag *mptcp_rtx_head(struct sock *sk)
383 {
384 	struct mptcp_sock *msk = mptcp_sk(sk);
385 
386 	if (msk->snd_una == READ_ONCE(msk->snd_nxt))
387 		return NULL;
388 
389 	return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
390 }
391 
392 struct csum_pseudo_header {
393 	__be64 data_seq;
394 	__be32 subflow_seq;
395 	__be16 data_len;
396 	__sum16 csum;
397 };
398 
399 struct mptcp_subflow_request_sock {
400 	struct	tcp_request_sock sk;
401 	u16	mp_capable : 1,
402 		mp_join : 1,
403 		backup : 1,
404 		csum_reqd : 1,
405 		allow_join_id0 : 1;
406 	u8	local_id;
407 	u8	remote_id;
408 	u64	local_key;
409 	u64	idsn;
410 	u32	token;
411 	u32	ssn_offset;
412 	u64	thmac;
413 	u32	local_nonce;
414 	u32	remote_nonce;
415 	struct mptcp_sock	*msk;
416 	struct hlist_nulls_node token_node;
417 };
418 
419 static inline struct mptcp_subflow_request_sock *
420 mptcp_subflow_rsk(const struct request_sock *rsk)
421 {
422 	return (struct mptcp_subflow_request_sock *)rsk;
423 }
424 
425 enum mptcp_data_avail {
426 	MPTCP_SUBFLOW_NODATA,
427 	MPTCP_SUBFLOW_DATA_AVAIL,
428 };
429 
430 struct mptcp_delegated_action {
431 	struct napi_struct napi;
432 	struct list_head head;
433 };
434 
435 DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
436 
437 #define MPTCP_DELEGATE_SEND		0
438 #define MPTCP_DELEGATE_ACK		1
439 
440 /* MPTCP subflow context */
441 struct mptcp_subflow_context {
442 	struct	list_head node;/* conn_list of subflows */
443 
444 	struct_group(reset,
445 
446 	unsigned long avg_pacing_rate; /* protected by msk socket lock */
447 	u64	local_key;
448 	u64	remote_key;
449 	u64	idsn;
450 	u64	map_seq;
451 	u32	snd_isn;
452 	u32	token;
453 	u32	rel_write_seq;
454 	u32	map_subflow_seq;
455 	u32	ssn_offset;
456 	u32	map_data_len;
457 	__wsum	map_data_csum;
458 	u32	map_csum_len;
459 	u32	request_mptcp : 1,  /* send MP_CAPABLE */
460 		request_join : 1,   /* send MP_JOIN */
461 		request_bkup : 1,
462 		mp_capable : 1,	    /* remote is MPTCP capable */
463 		mp_join : 1,	    /* remote is JOINing */
464 		fully_established : 1,	    /* path validated */
465 		pm_notified : 1,    /* PM hook called for established status */
466 		conn_finished : 1,
467 		map_valid : 1,
468 		map_csum_reqd : 1,
469 		map_data_fin : 1,
470 		mpc_map : 1,
471 		backup : 1,
472 		send_mp_prio : 1,
473 		send_mp_fail : 1,
474 		send_fastclose : 1,
475 		send_infinite_map : 1,
476 		rx_eof : 1,
477 		remote_key_valid : 1,        /* received the peer key from */
478 		disposable : 1,	    /* ctx can be free at ulp release time */
479 		stale : 1,	    /* unable to snd/rcv data, do not use for xmit */
480 		local_id_valid : 1, /* local_id is correctly initialized */
481 		valid_csum_seen : 1,        /* at least one csum validated */
482 		is_mptfo : 1,	    /* subflow is doing TFO */
483 		__unused : 8;
484 	enum mptcp_data_avail data_avail;
485 	u32	remote_nonce;
486 	u64	thmac;
487 	u32	local_nonce;
488 	u32	remote_token;
489 	union {
490 		u8	hmac[MPTCPOPT_HMAC_LEN]; /* MPJ subflow only */
491 		u64	iasn;	    /* initial ack sequence number, MPC subflows only */
492 	};
493 	u8	local_id;
494 	u8	remote_id;
495 	u8	reset_seen:1;
496 	u8	reset_transient:1;
497 	u8	reset_reason:4;
498 	u8	stale_count;
499 
500 	long	delegated_status;
501 	unsigned long	fail_tout;
502 
503 	);
504 
505 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
506 
507 	u32	setsockopt_seq;
508 	u32	stale_rcv_tstamp;
509 
510 	struct	sock *tcp_sock;	    /* tcp sk backpointer */
511 	struct	sock *conn;	    /* parent mptcp_sock */
512 	const	struct inet_connection_sock_af_ops *icsk_af_ops;
513 	void	(*tcp_state_change)(struct sock *sk);
514 	void	(*tcp_error_report)(struct sock *sk);
515 
516 	struct	rcu_head rcu;
517 };
518 
519 static inline struct mptcp_subflow_context *
520 mptcp_subflow_ctx(const struct sock *sk)
521 {
522 	struct inet_connection_sock *icsk = inet_csk(sk);
523 
524 	/* Use RCU on icsk_ulp_data only for sock diag code */
525 	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
526 }
527 
528 static inline struct sock *
529 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
530 {
531 	return subflow->tcp_sock;
532 }
533 
534 static inline void
535 mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow)
536 {
537 	memset(&subflow->reset, 0, sizeof(subflow->reset));
538 	subflow->request_mptcp = 1;
539 }
540 
541 static inline u64
542 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
543 {
544 	return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
545 		      subflow->ssn_offset -
546 		      subflow->map_subflow_seq;
547 }
548 
549 static inline u64
550 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
551 {
552 	return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
553 }
554 
555 void mptcp_subflow_process_delegated(struct sock *ssk);
556 
557 static inline void mptcp_subflow_delegate(struct mptcp_subflow_context *subflow, int action)
558 {
559 	struct mptcp_delegated_action *delegated;
560 	bool schedule;
561 
562 	/* the caller held the subflow bh socket lock */
563 	lockdep_assert_in_softirq();
564 
565 	/* The implied barrier pairs with mptcp_subflow_delegated_done(), and
566 	 * ensures the below list check sees list updates done prior to status
567 	 * bit changes
568 	 */
569 	if (!test_and_set_bit(action, &subflow->delegated_status)) {
570 		/* still on delegated list from previous scheduling */
571 		if (!list_empty(&subflow->delegated_node))
572 			return;
573 
574 		delegated = this_cpu_ptr(&mptcp_delegated_actions);
575 		schedule = list_empty(&delegated->head);
576 		list_add_tail(&subflow->delegated_node, &delegated->head);
577 		sock_hold(mptcp_subflow_tcp_sock(subflow));
578 		if (schedule)
579 			napi_schedule(&delegated->napi);
580 	}
581 }
582 
583 static inline struct mptcp_subflow_context *
584 mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)
585 {
586 	struct mptcp_subflow_context *ret;
587 
588 	if (list_empty(&delegated->head))
589 		return NULL;
590 
591 	ret = list_first_entry(&delegated->head, struct mptcp_subflow_context, delegated_node);
592 	list_del_init(&ret->delegated_node);
593 	return ret;
594 }
595 
596 static inline bool mptcp_subflow_has_delegated_action(const struct mptcp_subflow_context *subflow)
597 {
598 	return !!READ_ONCE(subflow->delegated_status);
599 }
600 
601 static inline void mptcp_subflow_delegated_done(struct mptcp_subflow_context *subflow, int action)
602 {
603 	/* pairs with mptcp_subflow_delegate, ensures delegate_node is updated before
604 	 * touching the status bit
605 	 */
606 	smp_wmb();
607 	clear_bit(action, &subflow->delegated_status);
608 }
609 
610 int mptcp_is_enabled(const struct net *net);
611 unsigned int mptcp_get_add_addr_timeout(const struct net *net);
612 int mptcp_is_checksum_enabled(const struct net *net);
613 int mptcp_allow_join_id0(const struct net *net);
614 unsigned int mptcp_stale_loss_cnt(const struct net *net);
615 int mptcp_get_pm_type(const struct net *net);
616 void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk);
617 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
618 				     const struct mptcp_options_received *mp_opt);
619 bool __mptcp_retransmit_pending_data(struct sock *sk);
620 void mptcp_check_and_set_pending(struct sock *sk);
621 void __mptcp_push_pending(struct sock *sk, unsigned int flags);
622 bool mptcp_subflow_data_available(struct sock *sk);
623 void __init mptcp_subflow_init(void);
624 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
625 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
626 		     struct mptcp_subflow_context *subflow);
627 void __mptcp_subflow_send_ack(struct sock *ssk);
628 void mptcp_subflow_reset(struct sock *ssk);
629 void mptcp_subflow_queue_clean(struct sock *sk, struct sock *ssk);
630 void mptcp_sock_graft(struct sock *sk, struct socket *parent);
631 struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk);
632 bool __mptcp_close(struct sock *sk, long timeout);
633 void mptcp_cancel_work(struct sock *sk);
634 void __mptcp_unaccepted_force_close(struct sock *sk);
635 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk);
636 
637 bool mptcp_addresses_equal(const struct mptcp_addr_info *a,
638 			   const struct mptcp_addr_info *b, bool use_port);
639 
640 /* called with sk socket lock held */
641 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
642 			    const struct mptcp_addr_info *remote);
643 int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
644 				struct socket **new_sock);
645 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
646 			 struct sockaddr_storage *addr,
647 			 unsigned short family);
648 
649 static inline bool __tcp_can_send(const struct sock *ssk)
650 {
651 	/* only send if our side has not closed yet */
652 	return ((1 << inet_sk_state_load(ssk)) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
653 }
654 
655 static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
656 {
657 	/* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
658 	if (subflow->request_join && !subflow->fully_established)
659 		return false;
660 
661 	return __tcp_can_send(mptcp_subflow_tcp_sock(subflow));
662 }
663 
664 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow);
665 
666 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow);
667 
668 void mptcp_subflow_drop_ctx(struct sock *ssk);
669 
670 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
671 					      struct mptcp_subflow_context *ctx)
672 {
673 	sk->sk_data_ready = sock_def_readable;
674 	sk->sk_state_change = ctx->tcp_state_change;
675 	sk->sk_write_space = sk_stream_write_space;
676 	sk->sk_error_report = ctx->tcp_error_report;
677 
678 	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
679 }
680 
681 void __init mptcp_proto_init(void);
682 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
683 int __init mptcp_proto_v6_init(void);
684 #endif
685 
686 struct sock *mptcp_sk_clone(const struct sock *sk,
687 			    const struct mptcp_options_received *mp_opt,
688 			    struct request_sock *req);
689 void mptcp_get_options(const struct sk_buff *skb,
690 		       struct mptcp_options_received *mp_opt);
691 
692 void mptcp_finish_connect(struct sock *sk);
693 void __mptcp_set_connected(struct sock *sk);
694 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout);
695 static inline bool mptcp_is_fully_established(struct sock *sk)
696 {
697 	return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
698 	       READ_ONCE(mptcp_sk(sk)->fully_established);
699 }
700 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
701 void mptcp_data_ready(struct sock *sk, struct sock *ssk);
702 bool mptcp_finish_join(struct sock *sk);
703 bool mptcp_schedule_work(struct sock *sk);
704 int mptcp_setsockopt(struct sock *sk, int level, int optname,
705 		     sockptr_t optval, unsigned int optlen);
706 int mptcp_getsockopt(struct sock *sk, int level, int optname,
707 		     char __user *optval, int __user *option);
708 
709 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq);
710 static inline u64 mptcp_expand_seq(u64 old_seq, u64 cur_seq, bool use_64bit)
711 {
712 	if (use_64bit)
713 		return cur_seq;
714 
715 	return __mptcp_expand_seq(old_seq, cur_seq);
716 }
717 void __mptcp_check_push(struct sock *sk, struct sock *ssk);
718 void __mptcp_data_acked(struct sock *sk);
719 void __mptcp_error_report(struct sock *sk);
720 void mptcp_subflow_eof(struct sock *sk);
721 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
722 static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
723 {
724 	return READ_ONCE(msk->snd_data_fin_enable) &&
725 	       READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
726 }
727 
728 static inline bool mptcp_propagate_sndbuf(struct sock *sk, struct sock *ssk)
729 {
730 	if ((sk->sk_userlocks & SOCK_SNDBUF_LOCK) || ssk->sk_sndbuf <= READ_ONCE(sk->sk_sndbuf))
731 		return false;
732 
733 	WRITE_ONCE(sk->sk_sndbuf, ssk->sk_sndbuf);
734 	return true;
735 }
736 
737 static inline void mptcp_write_space(struct sock *sk)
738 {
739 	if (sk_stream_is_writeable(sk)) {
740 		/* pairs with memory barrier in mptcp_poll */
741 		smp_mb();
742 		if (test_and_clear_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags))
743 			sk_stream_write_space(sk);
744 	}
745 }
746 
747 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags);
748 
749 #define MPTCP_TOKEN_MAX_RETRIES	4
750 
751 void __init mptcp_token_init(void);
752 static inline void mptcp_token_init_request(struct request_sock *req)
753 {
754 	mptcp_subflow_rsk(req)->token_node.pprev = NULL;
755 }
756 
757 int mptcp_token_new_request(struct request_sock *req);
758 void mptcp_token_destroy_request(struct request_sock *req);
759 int mptcp_token_new_connect(struct sock *ssk);
760 void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
761 			struct mptcp_sock *msk);
762 bool mptcp_token_exists(u32 token);
763 struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token);
764 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
765 					 long *s_num);
766 void mptcp_token_destroy(struct mptcp_sock *msk);
767 
768 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
769 
770 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
771 __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum);
772 
773 void __init mptcp_pm_init(void);
774 void mptcp_pm_data_init(struct mptcp_sock *msk);
775 void mptcp_pm_data_reset(struct mptcp_sock *msk);
776 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
777 			struct mptcp_addr_info *addr);
778 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
779 			 bool require_family,
780 			 struct mptcp_pm_addr_entry *entry);
781 bool mptcp_pm_addr_families_match(const struct sock *sk,
782 				  const struct mptcp_addr_info *loc,
783 				  const struct mptcp_addr_info *rem);
784 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
785 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
786 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side);
787 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk);
788 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
789 void mptcp_pm_connection_closed(struct mptcp_sock *msk);
790 void mptcp_pm_subflow_established(struct mptcp_sock *msk);
791 bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk);
792 void mptcp_pm_subflow_check_next(struct mptcp_sock *msk, const struct sock *ssk,
793 				 const struct mptcp_subflow_context *subflow);
794 void mptcp_pm_add_addr_received(const struct sock *ssk,
795 				const struct mptcp_addr_info *addr);
796 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
797 			      const struct mptcp_addr_info *addr);
798 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
799 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk);
800 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
801 			       const struct mptcp_rm_list *rm_list);
802 void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup);
803 void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq);
804 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
805 				 struct mptcp_addr_info *addr,
806 				 struct mptcp_addr_info *rem,
807 				 u8 bkup);
808 bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
809 			      const struct mptcp_pm_addr_entry *entry);
810 void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
811 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk);
812 struct mptcp_pm_add_entry *
813 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
814 		       const struct mptcp_addr_info *addr, bool check_id);
815 struct mptcp_pm_add_entry *
816 mptcp_lookup_anno_list_by_saddr(const struct mptcp_sock *msk,
817 				const struct mptcp_addr_info *addr);
818 int mptcp_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
819 					 unsigned int id,
820 					 u8 *flags, int *ifindex);
821 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
822 						   unsigned int id,
823 						   u8 *flags, int *ifindex);
824 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
825 				 struct mptcp_pm_addr_entry *loc,
826 				 struct mptcp_pm_addr_entry *rem, u8 bkup);
827 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
828 			   const struct mptcp_addr_info *addr,
829 			   bool echo);
830 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
831 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
832 void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
833 					struct list_head *rm_list);
834 
835 void mptcp_free_local_addr_list(struct mptcp_sock *msk);
836 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info);
837 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info);
838 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info);
839 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info);
840 
841 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
842 		 const struct sock *ssk, gfp_t gfp);
843 void mptcp_event_addr_announced(const struct sock *ssk, const struct mptcp_addr_info *info);
844 void mptcp_event_addr_removed(const struct mptcp_sock *msk, u8 id);
845 void mptcp_event_pm_listener(const struct sock *ssk,
846 			     enum mptcp_event_type event);
847 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk);
848 
849 void mptcp_fastopen_gen_msk_ackseq(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
850 				   const struct mptcp_options_received *mp_opt);
851 void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subflow,
852 					      struct request_sock *req);
853 
854 static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
855 {
856 	return READ_ONCE(msk->pm.addr_signal) &
857 		(BIT(MPTCP_ADD_ADDR_SIGNAL) | BIT(MPTCP_ADD_ADDR_ECHO));
858 }
859 
860 static inline bool mptcp_pm_should_add_signal_addr(struct mptcp_sock *msk)
861 {
862 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
863 }
864 
865 static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
866 {
867 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
868 }
869 
870 static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
871 {
872 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
873 }
874 
875 static inline bool mptcp_pm_is_userspace(const struct mptcp_sock *msk)
876 {
877 	return READ_ONCE(msk->pm.pm_type) == MPTCP_PM_TYPE_USERSPACE;
878 }
879 
880 static inline bool mptcp_pm_is_kernel(const struct mptcp_sock *msk)
881 {
882 	return READ_ONCE(msk->pm.pm_type) == MPTCP_PM_TYPE_KERNEL;
883 }
884 
885 static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
886 {
887 	u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
888 
889 	if (family == AF_INET6)
890 		len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
891 	if (!echo)
892 		len += MPTCPOPT_THMAC_LEN;
893 	/* account for 2 trailing 'nop' options */
894 	if (port)
895 		len += TCPOLEN_MPTCP_PORT_LEN + TCPOLEN_MPTCP_PORT_ALIGN;
896 
897 	return len;
898 }
899 
900 static inline int mptcp_rm_addr_len(const struct mptcp_rm_list *rm_list)
901 {
902 	if (rm_list->nr == 0 || rm_list->nr > MPTCP_RM_IDS_MAX)
903 		return -EINVAL;
904 
905 	return TCPOLEN_MPTCP_RM_ADDR_BASE + roundup(rm_list->nr - 1, 4) + 1;
906 }
907 
908 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb,
909 			      unsigned int opt_size, unsigned int remaining,
910 			      struct mptcp_addr_info *addr, bool *echo,
911 			      bool *drop_other_suboptions);
912 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
913 			     struct mptcp_rm_list *rm_list);
914 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
915 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc);
916 
917 void __init mptcp_pm_nl_init(void);
918 void mptcp_pm_nl_work(struct mptcp_sock *msk);
919 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
920 				     const struct mptcp_rm_list *rm_list);
921 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
922 unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk);
923 unsigned int mptcp_pm_get_add_addr_accept_max(const struct mptcp_sock *msk);
924 unsigned int mptcp_pm_get_subflows_max(const struct mptcp_sock *msk);
925 unsigned int mptcp_pm_get_local_addr_max(const struct mptcp_sock *msk);
926 
927 /* called under PM lock */
928 static inline void __mptcp_pm_close_subflow(struct mptcp_sock *msk)
929 {
930 	if (--msk->pm.subflows < mptcp_pm_get_subflows_max(msk))
931 		WRITE_ONCE(msk->pm.accept_subflow, true);
932 }
933 
934 static inline void mptcp_pm_close_subflow(struct mptcp_sock *msk)
935 {
936 	spin_lock_bh(&msk->pm.lock);
937 	__mptcp_pm_close_subflow(msk);
938 	spin_unlock_bh(&msk->pm.lock);
939 }
940 
941 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk);
942 void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk);
943 
944 static inline struct mptcp_ext *mptcp_get_ext(const struct sk_buff *skb)
945 {
946 	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
947 }
948 
949 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
950 
951 static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
952 {
953 	return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
954 }
955 
956 static inline bool mptcp_check_fallback(const struct sock *sk)
957 {
958 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
959 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
960 
961 	return __mptcp_check_fallback(msk);
962 }
963 
964 static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
965 {
966 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
967 		pr_debug("TCP fallback already done (msk=%p)", msk);
968 		return;
969 	}
970 	set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
971 }
972 
973 static inline void mptcp_do_fallback(struct sock *ssk)
974 {
975 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
976 	struct sock *sk = subflow->conn;
977 	struct mptcp_sock *msk;
978 
979 	msk = mptcp_sk(sk);
980 	__mptcp_do_fallback(msk);
981 	if (READ_ONCE(msk->snd_data_fin_enable) && !(ssk->sk_shutdown & SEND_SHUTDOWN)) {
982 		gfp_t saved_allocation = ssk->sk_allocation;
983 
984 		/* we are in a atomic (BH) scope, override ssk default for data
985 		 * fin allocation
986 		 */
987 		ssk->sk_allocation = GFP_ATOMIC;
988 		ssk->sk_shutdown |= SEND_SHUTDOWN;
989 		tcp_shutdown(ssk, SEND_SHUTDOWN);
990 		ssk->sk_allocation = saved_allocation;
991 	}
992 }
993 
994 #define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
995 
996 static inline bool mptcp_check_infinite_map(struct sk_buff *skb)
997 {
998 	struct mptcp_ext *mpext;
999 
1000 	mpext = skb ? mptcp_get_ext(skb) : NULL;
1001 	if (mpext && mpext->infinite_map)
1002 		return true;
1003 
1004 	return false;
1005 }
1006 
1007 static inline bool is_active_ssk(struct mptcp_subflow_context *subflow)
1008 {
1009 	return (subflow->request_mptcp || subflow->request_join);
1010 }
1011 
1012 static inline bool subflow_simultaneous_connect(struct sock *sk)
1013 {
1014 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1015 
1016 	return sk->sk_state == TCP_ESTABLISHED &&
1017 	       is_active_ssk(subflow) &&
1018 	       !subflow->conn_finished;
1019 }
1020 
1021 #ifdef CONFIG_SYN_COOKIES
1022 void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
1023 				       struct sk_buff *skb);
1024 bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
1025 					struct sk_buff *skb);
1026 void __init mptcp_join_cookie_init(void);
1027 #else
1028 static inline void
1029 subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
1030 				  struct sk_buff *skb) {}
1031 static inline bool
1032 mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
1033 				   struct sk_buff *skb)
1034 {
1035 	return false;
1036 }
1037 
1038 static inline void mptcp_join_cookie_init(void) {}
1039 #endif
1040 
1041 #endif /* __MPTCP_PROTOCOL_H */
1042