xref: /linux/net/mptcp/protocol.h (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
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 
15 #define MPTCP_SUPPORTED_VERSION	1
16 
17 /* MPTCP option bits */
18 #define OPTION_MPTCP_MPC_SYN	BIT(0)
19 #define OPTION_MPTCP_MPC_SYNACK	BIT(1)
20 #define OPTION_MPTCP_MPC_ACK	BIT(2)
21 #define OPTION_MPTCP_MPJ_SYN	BIT(3)
22 #define OPTION_MPTCP_MPJ_SYNACK	BIT(4)
23 #define OPTION_MPTCP_MPJ_ACK	BIT(5)
24 #define OPTION_MPTCP_ADD_ADDR	BIT(6)
25 #define OPTION_MPTCP_RM_ADDR	BIT(7)
26 #define OPTION_MPTCP_FASTCLOSE	BIT(8)
27 #define OPTION_MPTCP_PRIO	BIT(9)
28 #define OPTION_MPTCP_RST	BIT(10)
29 
30 /* MPTCP option subtypes */
31 #define MPTCPOPT_MP_CAPABLE	0
32 #define MPTCPOPT_MP_JOIN	1
33 #define MPTCPOPT_DSS		2
34 #define MPTCPOPT_ADD_ADDR	3
35 #define MPTCPOPT_RM_ADDR	4
36 #define MPTCPOPT_MP_PRIO	5
37 #define MPTCPOPT_MP_FAIL	6
38 #define MPTCPOPT_MP_FASTCLOSE	7
39 #define MPTCPOPT_RST		8
40 
41 /* MPTCP suboption lengths */
42 #define TCPOLEN_MPTCP_MPC_SYN		4
43 #define TCPOLEN_MPTCP_MPC_SYNACK	12
44 #define TCPOLEN_MPTCP_MPC_ACK		20
45 #define TCPOLEN_MPTCP_MPC_ACK_DATA	22
46 #define TCPOLEN_MPTCP_MPJ_SYN		12
47 #define TCPOLEN_MPTCP_MPJ_SYNACK	16
48 #define TCPOLEN_MPTCP_MPJ_ACK		24
49 #define TCPOLEN_MPTCP_DSS_BASE		4
50 #define TCPOLEN_MPTCP_DSS_ACK32		4
51 #define TCPOLEN_MPTCP_DSS_ACK64		8
52 #define TCPOLEN_MPTCP_DSS_MAP32		10
53 #define TCPOLEN_MPTCP_DSS_MAP64		14
54 #define TCPOLEN_MPTCP_DSS_CHECKSUM	2
55 #define TCPOLEN_MPTCP_ADD_ADDR		16
56 #define TCPOLEN_MPTCP_ADD_ADDR_PORT	18
57 #define TCPOLEN_MPTCP_ADD_ADDR_BASE	8
58 #define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT	10
59 #define TCPOLEN_MPTCP_ADD_ADDR6		28
60 #define TCPOLEN_MPTCP_ADD_ADDR6_PORT	30
61 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE	20
62 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT	22
63 #define TCPOLEN_MPTCP_PORT_LEN		2
64 #define TCPOLEN_MPTCP_PORT_ALIGN	2
65 #define TCPOLEN_MPTCP_RM_ADDR_BASE	3
66 #define TCPOLEN_MPTCP_PRIO		3
67 #define TCPOLEN_MPTCP_PRIO_ALIGN	4
68 #define TCPOLEN_MPTCP_FASTCLOSE		12
69 #define TCPOLEN_MPTCP_RST		4
70 
71 /* MPTCP MP_JOIN flags */
72 #define MPTCPOPT_BACKUP		BIT(0)
73 #define MPTCPOPT_HMAC_LEN	20
74 #define MPTCPOPT_THMAC_LEN	8
75 
76 /* MPTCP MP_CAPABLE flags */
77 #define MPTCP_VERSION_MASK	(0x0F)
78 #define MPTCP_CAP_CHECKSUM_REQD	BIT(7)
79 #define MPTCP_CAP_EXTENSIBILITY	BIT(6)
80 #define MPTCP_CAP_HMAC_SHA256	BIT(0)
81 #define MPTCP_CAP_FLAG_MASK	(0x3F)
82 
83 /* MPTCP DSS flags */
84 #define MPTCP_DSS_DATA_FIN	BIT(4)
85 #define MPTCP_DSS_DSN64		BIT(3)
86 #define MPTCP_DSS_HAS_MAP	BIT(2)
87 #define MPTCP_DSS_ACK64		BIT(1)
88 #define MPTCP_DSS_HAS_ACK	BIT(0)
89 #define MPTCP_DSS_FLAG_MASK	(0x1F)
90 
91 /* MPTCP ADD_ADDR flags */
92 #define MPTCP_ADDR_ECHO		BIT(0)
93 
94 /* MPTCP MP_PRIO flags */
95 #define MPTCP_PRIO_BKUP		BIT(0)
96 
97 /* MPTCP TCPRST flags */
98 #define MPTCP_RST_TRANSIENT	BIT(0)
99 
100 /* MPTCP socket flags */
101 #define MPTCP_DATA_READY	0
102 #define MPTCP_NOSPACE		1
103 #define MPTCP_WORK_RTX		2
104 #define MPTCP_WORK_EOF		3
105 #define MPTCP_FALLBACK_DONE	4
106 #define MPTCP_WORK_CLOSE_SUBFLOW 5
107 #define MPTCP_PUSH_PENDING	6
108 #define MPTCP_CLEAN_UNA		7
109 #define MPTCP_ERROR_REPORT	8
110 #define MPTCP_RETRANSMIT	9
111 #define MPTCP_WORK_SYNC_SETSOCKOPT 10
112 
113 static inline bool before64(__u64 seq1, __u64 seq2)
114 {
115 	return (__s64)(seq1 - seq2) < 0;
116 }
117 
118 #define after64(seq2, seq1)	before64(seq1, seq2)
119 
120 struct mptcp_options_received {
121 	u64	sndr_key;
122 	u64	rcvr_key;
123 	u64	data_ack;
124 	u64	data_seq;
125 	u32	subflow_seq;
126 	u16	data_len;
127 	u16	mp_capable : 1,
128 		mp_join : 1,
129 		fastclose : 1,
130 		reset : 1,
131 		dss : 1,
132 		add_addr : 1,
133 		rm_addr : 1,
134 		mp_prio : 1,
135 		echo : 1,
136 		backup : 1;
137 	u32	token;
138 	u32	nonce;
139 	u64	thmac;
140 	u8	hmac[MPTCPOPT_HMAC_LEN];
141 	u8	join_id;
142 	u8	use_map:1,
143 		dsn64:1,
144 		data_fin:1,
145 		use_ack:1,
146 		ack64:1,
147 		mpc_map:1,
148 		__unused:2;
149 	struct mptcp_addr_info addr;
150 	struct mptcp_rm_list rm_list;
151 	u64	ahmac;
152 	u8	reset_reason:4;
153 	u8	reset_transient:1;
154 };
155 
156 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
157 {
158 	return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
159 		     ((nib & 0xF) << 8) | field);
160 }
161 
162 enum mptcp_pm_status {
163 	MPTCP_PM_ADD_ADDR_RECEIVED,
164 	MPTCP_PM_ADD_ADDR_SEND_ACK,
165 	MPTCP_PM_RM_ADDR_RECEIVED,
166 	MPTCP_PM_ESTABLISHED,
167 	MPTCP_PM_ALREADY_ESTABLISHED,	/* persistent status, set after ESTABLISHED event */
168 	MPTCP_PM_SUBFLOW_ESTABLISHED,
169 };
170 
171 enum mptcp_addr_signal_status {
172 	MPTCP_ADD_ADDR_SIGNAL,
173 	MPTCP_ADD_ADDR_ECHO,
174 	MPTCP_ADD_ADDR_IPV6,
175 	MPTCP_ADD_ADDR_PORT,
176 	MPTCP_RM_ADDR_SIGNAL,
177 };
178 
179 struct mptcp_pm_data {
180 	struct mptcp_addr_info local;
181 	struct mptcp_addr_info remote;
182 	struct list_head anno_list;
183 
184 	spinlock_t	lock;		/*protects the whole PM data */
185 
186 	u8		addr_signal;
187 	bool		server_side;
188 	bool		work_pending;
189 	bool		accept_addr;
190 	bool		accept_subflow;
191 	u8		add_addr_signaled;
192 	u8		add_addr_accepted;
193 	u8		local_addr_used;
194 	u8		subflows;
195 	u8		status;
196 	struct mptcp_rm_list rm_list_tx;
197 	struct mptcp_rm_list rm_list_rx;
198 };
199 
200 struct mptcp_data_frag {
201 	struct list_head list;
202 	u64 data_seq;
203 	u16 data_len;
204 	u16 offset;
205 	u16 overhead;
206 	u16 already_sent;
207 	struct page *page;
208 };
209 
210 /* MPTCP connection sock */
211 struct mptcp_sock {
212 	/* inet_connection_sock must be the first member */
213 	struct inet_connection_sock sk;
214 	u64		local_key;
215 	u64		remote_key;
216 	u64		write_seq;
217 	u64		snd_nxt;
218 	u64		ack_seq;
219 	u64		rcv_wnd_sent;
220 	u64		rcv_data_fin_seq;
221 	int		wmem_reserved;
222 	struct sock	*last_snd;
223 	int		snd_burst;
224 	int		old_wspace;
225 	u64		snd_una;
226 	u64		wnd_end;
227 	unsigned long	timer_ival;
228 	u32		token;
229 	int		rmem_released;
230 	unsigned long	flags;
231 	bool		can_ack;
232 	bool		fully_established;
233 	bool		rcv_data_fin;
234 	bool		snd_data_fin_enable;
235 	bool		rcv_fastclose;
236 	bool		use_64bit_ack; /* Set when we received a 64-bit DSN */
237 	spinlock_t	join_list_lock;
238 	struct sock	*ack_hint;
239 	struct work_struct work;
240 	struct sk_buff  *ooo_last_skb;
241 	struct rb_root  out_of_order_queue;
242 	struct sk_buff_head receive_queue;
243 	struct sk_buff_head skb_tx_cache;	/* this is wmem accounted */
244 	int		tx_pending_data;
245 	int		size_goal_cache;
246 	struct list_head conn_list;
247 	struct list_head rtx_queue;
248 	struct mptcp_data_frag *first_pending;
249 	struct list_head join_list;
250 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
251 	struct sock	*first;
252 	struct mptcp_pm_data	pm;
253 	struct {
254 		u32	space;	/* bytes copied in last measurement window */
255 		u32	copied; /* bytes copied in this measurement window */
256 		u64	time;	/* start time of measurement window */
257 		u64	rtt_us; /* last maximum rtt of subflows */
258 	} rcvq_space;
259 
260 	u32 setsockopt_seq;
261 };
262 
263 #define mptcp_lock_sock(___sk, cb) do {					\
264 	struct sock *__sk = (___sk); /* silence macro reuse warning */	\
265 	might_sleep();							\
266 	spin_lock_bh(&__sk->sk_lock.slock);				\
267 	if (__sk->sk_lock.owned)					\
268 		__lock_sock(__sk);					\
269 	cb;								\
270 	__sk->sk_lock.owned = 1;					\
271 	spin_unlock(&__sk->sk_lock.slock);				\
272 	mutex_acquire(&__sk->sk_lock.dep_map, 0, 0, _RET_IP_);		\
273 	local_bh_enable();						\
274 } while (0)
275 
276 #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
277 #define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
278 
279 #define mptcp_for_each_subflow(__msk, __subflow)			\
280 	list_for_each_entry(__subflow, &((__msk)->conn_list), node)
281 
282 static inline void msk_owned_by_me(const struct mptcp_sock *msk)
283 {
284 	sock_owned_by_me((const struct sock *)msk);
285 }
286 
287 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
288 {
289 	return (struct mptcp_sock *)sk;
290 }
291 
292 static inline int __mptcp_space(const struct sock *sk)
293 {
294 	return tcp_space(sk) + READ_ONCE(mptcp_sk(sk)->rmem_released);
295 }
296 
297 static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
298 {
299 	const struct mptcp_sock *msk = mptcp_sk(sk);
300 
301 	return READ_ONCE(msk->first_pending);
302 }
303 
304 static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
305 {
306 	struct mptcp_sock *msk = mptcp_sk(sk);
307 	struct mptcp_data_frag *cur;
308 
309 	cur = msk->first_pending;
310 	return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
311 						     list_next_entry(cur, list);
312 }
313 
314 static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
315 {
316 	struct mptcp_sock *msk = mptcp_sk(sk);
317 
318 	if (!msk->first_pending)
319 		return NULL;
320 
321 	if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
322 		return NULL;
323 
324 	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
325 }
326 
327 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
328 {
329 	struct mptcp_sock *msk = mptcp_sk(sk);
330 
331 	if (msk->snd_una == READ_ONCE(msk->snd_nxt))
332 		return NULL;
333 
334 	return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
335 }
336 
337 struct mptcp_subflow_request_sock {
338 	struct	tcp_request_sock sk;
339 	u16	mp_capable : 1,
340 		mp_join : 1,
341 		backup : 1;
342 	u8	local_id;
343 	u8	remote_id;
344 	u64	local_key;
345 	u64	idsn;
346 	u32	token;
347 	u32	ssn_offset;
348 	u64	thmac;
349 	u32	local_nonce;
350 	u32	remote_nonce;
351 	struct mptcp_sock	*msk;
352 	struct hlist_nulls_node token_node;
353 };
354 
355 static inline struct mptcp_subflow_request_sock *
356 mptcp_subflow_rsk(const struct request_sock *rsk)
357 {
358 	return (struct mptcp_subflow_request_sock *)rsk;
359 }
360 
361 enum mptcp_data_avail {
362 	MPTCP_SUBFLOW_NODATA,
363 	MPTCP_SUBFLOW_DATA_AVAIL,
364 	MPTCP_SUBFLOW_OOO_DATA
365 };
366 
367 struct mptcp_delegated_action {
368 	struct napi_struct napi;
369 	struct list_head head;
370 };
371 
372 DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
373 
374 #define MPTCP_DELEGATE_SEND		0
375 
376 /* MPTCP subflow context */
377 struct mptcp_subflow_context {
378 	struct	list_head node;/* conn_list of subflows */
379 	u64	local_key;
380 	u64	remote_key;
381 	u64	idsn;
382 	u64	map_seq;
383 	u32	snd_isn;
384 	u32	token;
385 	u32	rel_write_seq;
386 	u32	map_subflow_seq;
387 	u32	ssn_offset;
388 	u32	map_data_len;
389 	u32	request_mptcp : 1,  /* send MP_CAPABLE */
390 		request_join : 1,   /* send MP_JOIN */
391 		request_bkup : 1,
392 		mp_capable : 1,	    /* remote is MPTCP capable */
393 		mp_join : 1,	    /* remote is JOINing */
394 		fully_established : 1,	    /* path validated */
395 		pm_notified : 1,    /* PM hook called for established status */
396 		conn_finished : 1,
397 		map_valid : 1,
398 		mpc_map : 1,
399 		backup : 1,
400 		send_mp_prio : 1,
401 		rx_eof : 1,
402 		can_ack : 1,        /* only after processing the remote a key */
403 		disposable : 1;	    /* ctx can be free at ulp release time */
404 	enum mptcp_data_avail data_avail;
405 	u32	remote_nonce;
406 	u64	thmac;
407 	u32	local_nonce;
408 	u32	remote_token;
409 	u8	hmac[MPTCPOPT_HMAC_LEN];
410 	u8	local_id;
411 	u8	remote_id;
412 	u8	reset_seen:1;
413 	u8	reset_transient:1;
414 	u8	reset_reason:4;
415 
416 	long	delegated_status;
417 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
418 
419 	u32 setsockopt_seq;
420 
421 	struct	sock *tcp_sock;	    /* tcp sk backpointer */
422 	struct	sock *conn;	    /* parent mptcp_sock */
423 	const	struct inet_connection_sock_af_ops *icsk_af_ops;
424 	void	(*tcp_data_ready)(struct sock *sk);
425 	void	(*tcp_state_change)(struct sock *sk);
426 	void	(*tcp_write_space)(struct sock *sk);
427 	void	(*tcp_error_report)(struct sock *sk);
428 
429 	struct	rcu_head rcu;
430 };
431 
432 static inline struct mptcp_subflow_context *
433 mptcp_subflow_ctx(const struct sock *sk)
434 {
435 	struct inet_connection_sock *icsk = inet_csk(sk);
436 
437 	/* Use RCU on icsk_ulp_data only for sock diag code */
438 	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
439 }
440 
441 static inline struct sock *
442 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
443 {
444 	return subflow->tcp_sock;
445 }
446 
447 static inline u64
448 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
449 {
450 	return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
451 		      subflow->ssn_offset -
452 		      subflow->map_subflow_seq;
453 }
454 
455 static inline u64
456 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
457 {
458 	return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
459 }
460 
461 static inline void mptcp_add_pending_subflow(struct mptcp_sock *msk,
462 					     struct mptcp_subflow_context *subflow)
463 {
464 	sock_hold(mptcp_subflow_tcp_sock(subflow));
465 	spin_lock_bh(&msk->join_list_lock);
466 	list_add_tail(&subflow->node, &msk->join_list);
467 	spin_unlock_bh(&msk->join_list_lock);
468 }
469 
470 void mptcp_subflow_process_delegated(struct sock *ssk);
471 
472 static inline void mptcp_subflow_delegate(struct mptcp_subflow_context *subflow)
473 {
474 	struct mptcp_delegated_action *delegated;
475 	bool schedule;
476 
477 	/* The implied barrier pairs with mptcp_subflow_delegated_done(), and
478 	 * ensures the below list check sees list updates done prior to status
479 	 * bit changes
480 	 */
481 	if (!test_and_set_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
482 		/* still on delegated list from previous scheduling */
483 		if (!list_empty(&subflow->delegated_node))
484 			return;
485 
486 		/* the caller held the subflow bh socket lock */
487 		lockdep_assert_in_softirq();
488 
489 		delegated = this_cpu_ptr(&mptcp_delegated_actions);
490 		schedule = list_empty(&delegated->head);
491 		list_add_tail(&subflow->delegated_node, &delegated->head);
492 		sock_hold(mptcp_subflow_tcp_sock(subflow));
493 		if (schedule)
494 			napi_schedule(&delegated->napi);
495 	}
496 }
497 
498 static inline struct mptcp_subflow_context *
499 mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)
500 {
501 	struct mptcp_subflow_context *ret;
502 
503 	if (list_empty(&delegated->head))
504 		return NULL;
505 
506 	ret = list_first_entry(&delegated->head, struct mptcp_subflow_context, delegated_node);
507 	list_del_init(&ret->delegated_node);
508 	return ret;
509 }
510 
511 static inline bool mptcp_subflow_has_delegated_action(const struct mptcp_subflow_context *subflow)
512 {
513 	return test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status);
514 }
515 
516 static inline void mptcp_subflow_delegated_done(struct mptcp_subflow_context *subflow)
517 {
518 	/* pairs with mptcp_subflow_delegate, ensures delegate_node is updated before
519 	 * touching the status bit
520 	 */
521 	smp_wmb();
522 	clear_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status);
523 }
524 
525 int mptcp_is_enabled(struct net *net);
526 unsigned int mptcp_get_add_addr_timeout(struct net *net);
527 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
528 				     struct mptcp_options_received *mp_opt);
529 bool mptcp_subflow_data_available(struct sock *sk);
530 void __init mptcp_subflow_init(void);
531 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
532 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
533 		     struct mptcp_subflow_context *subflow);
534 void mptcp_subflow_reset(struct sock *ssk);
535 void mptcp_sock_graft(struct sock *sk, struct socket *parent);
536 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk);
537 
538 /* called with sk socket lock held */
539 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
540 			    const struct mptcp_addr_info *remote,
541 			    u8 flags, int ifindex);
542 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
543 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
544 			 struct sockaddr_storage *addr,
545 			 unsigned short family);
546 
547 static inline bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
548 {
549 	struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
550 
551 	/* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
552 	if (subflow->request_join && !subflow->fully_established)
553 		return false;
554 
555 	/* only send if our side has not closed yet */
556 	return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
557 }
558 
559 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
560 					      struct mptcp_subflow_context *ctx)
561 {
562 	sk->sk_data_ready = ctx->tcp_data_ready;
563 	sk->sk_state_change = ctx->tcp_state_change;
564 	sk->sk_write_space = ctx->tcp_write_space;
565 	sk->sk_error_report = ctx->tcp_error_report;
566 
567 	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
568 }
569 
570 void __init mptcp_proto_init(void);
571 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
572 int __init mptcp_proto_v6_init(void);
573 #endif
574 
575 struct sock *mptcp_sk_clone(const struct sock *sk,
576 			    const struct mptcp_options_received *mp_opt,
577 			    struct request_sock *req);
578 void mptcp_get_options(const struct sk_buff *skb,
579 		       struct mptcp_options_received *mp_opt);
580 
581 void mptcp_finish_connect(struct sock *sk);
582 static inline bool mptcp_is_fully_established(struct sock *sk)
583 {
584 	return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
585 	       READ_ONCE(mptcp_sk(sk)->fully_established);
586 }
587 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
588 void mptcp_data_ready(struct sock *sk, struct sock *ssk);
589 bool mptcp_finish_join(struct sock *sk);
590 bool mptcp_schedule_work(struct sock *sk);
591 int mptcp_setsockopt(struct sock *sk, int level, int optname,
592 		     sockptr_t optval, unsigned int optlen);
593 int mptcp_getsockopt(struct sock *sk, int level, int optname,
594 		     char __user *optval, int __user *option);
595 
596 void __mptcp_check_push(struct sock *sk, struct sock *ssk);
597 void __mptcp_data_acked(struct sock *sk);
598 void __mptcp_error_report(struct sock *sk);
599 void mptcp_subflow_eof(struct sock *sk);
600 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
601 void __mptcp_flush_join_list(struct mptcp_sock *msk);
602 static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
603 {
604 	return READ_ONCE(msk->snd_data_fin_enable) &&
605 	       READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
606 }
607 
608 static inline bool mptcp_propagate_sndbuf(struct sock *sk, struct sock *ssk)
609 {
610 	if ((sk->sk_userlocks & SOCK_SNDBUF_LOCK) || ssk->sk_sndbuf <= READ_ONCE(sk->sk_sndbuf))
611 		return false;
612 
613 	WRITE_ONCE(sk->sk_sndbuf, ssk->sk_sndbuf);
614 	return true;
615 }
616 
617 static inline void mptcp_write_space(struct sock *sk)
618 {
619 	if (sk_stream_is_writeable(sk)) {
620 		/* pairs with memory barrier in mptcp_poll */
621 		smp_mb();
622 		if (test_and_clear_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags))
623 			sk_stream_write_space(sk);
624 	}
625 }
626 
627 void mptcp_destroy_common(struct mptcp_sock *msk);
628 
629 void __init mptcp_token_init(void);
630 static inline void mptcp_token_init_request(struct request_sock *req)
631 {
632 	mptcp_subflow_rsk(req)->token_node.pprev = NULL;
633 }
634 
635 int mptcp_token_new_request(struct request_sock *req);
636 void mptcp_token_destroy_request(struct request_sock *req);
637 int mptcp_token_new_connect(struct sock *sk);
638 void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
639 			struct mptcp_sock *msk);
640 bool mptcp_token_exists(u32 token);
641 struct mptcp_sock *mptcp_token_get_sock(u32 token);
642 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
643 					 long *s_num);
644 void mptcp_token_destroy(struct mptcp_sock *msk);
645 
646 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
647 
648 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
649 
650 void __init mptcp_pm_init(void);
651 void mptcp_pm_data_init(struct mptcp_sock *msk);
652 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side);
653 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp);
654 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
655 void mptcp_pm_connection_closed(struct mptcp_sock *msk);
656 void mptcp_pm_subflow_established(struct mptcp_sock *msk);
657 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
658 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
659 				const struct mptcp_addr_info *addr);
660 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
661 			      struct mptcp_addr_info *addr);
662 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
663 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk);
664 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
665 			       const struct mptcp_rm_list *rm_list);
666 void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup);
667 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
668 				 struct mptcp_addr_info *addr,
669 				 u8 bkup);
670 void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
671 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk);
672 struct mptcp_pm_add_entry *
673 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
674 		       struct mptcp_addr_info *addr);
675 struct mptcp_pm_add_entry *
676 mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk,
677 				struct mptcp_addr_info *addr);
678 
679 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
680 			   const struct mptcp_addr_info *addr,
681 			   bool echo);
682 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
683 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
684 
685 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
686 		 const struct sock *ssk, gfp_t gfp);
687 void mptcp_event_addr_announced(const struct mptcp_sock *msk, const struct mptcp_addr_info *info);
688 void mptcp_event_addr_removed(const struct mptcp_sock *msk, u8 id);
689 
690 static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
691 {
692 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
693 }
694 
695 static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
696 {
697 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
698 }
699 
700 static inline bool mptcp_pm_should_add_signal_ipv6(struct mptcp_sock *msk)
701 {
702 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_IPV6);
703 }
704 
705 static inline bool mptcp_pm_should_add_signal_port(struct mptcp_sock *msk)
706 {
707 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_PORT);
708 }
709 
710 static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
711 {
712 	return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
713 }
714 
715 static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
716 {
717 	u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
718 
719 	if (family == AF_INET6)
720 		len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
721 	if (!echo)
722 		len += MPTCPOPT_THMAC_LEN;
723 	/* account for 2 trailing 'nop' options */
724 	if (port)
725 		len += TCPOLEN_MPTCP_PORT_LEN + TCPOLEN_MPTCP_PORT_ALIGN;
726 
727 	return len;
728 }
729 
730 static inline int mptcp_rm_addr_len(const struct mptcp_rm_list *rm_list)
731 {
732 	if (rm_list->nr == 0 || rm_list->nr > MPTCP_RM_IDS_MAX)
733 		return -EINVAL;
734 
735 	return TCPOLEN_MPTCP_RM_ADDR_BASE + roundup(rm_list->nr - 1, 4) + 1;
736 }
737 
738 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
739 			      struct mptcp_addr_info *saddr, bool *echo, bool *port);
740 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
741 			     struct mptcp_rm_list *rm_list);
742 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
743 
744 void __init mptcp_pm_nl_init(void);
745 void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
746 void mptcp_pm_nl_work(struct mptcp_sock *msk);
747 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
748 				     const struct mptcp_rm_list *rm_list);
749 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
750 unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk);
751 unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk);
752 unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk);
753 unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk);
754 
755 int mptcp_setsockopt(struct sock *sk, int level, int optname,
756 		     sockptr_t optval, unsigned int optlen);
757 
758 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk);
759 void mptcp_sockopt_sync_all(struct mptcp_sock *msk);
760 
761 static inline struct mptcp_ext *mptcp_get_ext(const struct sk_buff *skb)
762 {
763 	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
764 }
765 
766 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
767 
768 static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
769 {
770 	return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
771 }
772 
773 static inline bool mptcp_check_fallback(const struct sock *sk)
774 {
775 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
776 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
777 
778 	return __mptcp_check_fallback(msk);
779 }
780 
781 static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
782 {
783 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
784 		pr_debug("TCP fallback already done (msk=%p)", msk);
785 		return;
786 	}
787 	set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
788 }
789 
790 static inline void mptcp_do_fallback(struct sock *sk)
791 {
792 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
793 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
794 
795 	__mptcp_do_fallback(msk);
796 }
797 
798 #define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
799 
800 static inline bool subflow_simultaneous_connect(struct sock *sk)
801 {
802 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
803 	struct sock *parent = subflow->conn;
804 
805 	return sk->sk_state == TCP_ESTABLISHED &&
806 	       !mptcp_sk(parent)->pm.server_side &&
807 	       !subflow->conn_finished;
808 }
809 
810 #ifdef CONFIG_SYN_COOKIES
811 void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
812 				       struct sk_buff *skb);
813 bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
814 					struct sk_buff *skb);
815 void __init mptcp_join_cookie_init(void);
816 #else
817 static inline void
818 subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
819 				  struct sk_buff *skb) {}
820 static inline bool
821 mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
822 				   struct sk_buff *skb)
823 {
824 	return false;
825 }
826 
827 static inline void mptcp_join_cookie_init(void) {}
828 #endif
829 
830 #endif /* __MPTCP_PROTOCOL_H */
831