• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2017 - 2019, Intel Corporation.
5  */
6 
7 #define pr_fmt(fmt) "MPTCP: " fmt
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <crypto/algapi.h>
13 #include <crypto/sha2.h>
14 #include <net/sock.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
18 #include <net/tcp.h>
19 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/ip6_route.h>
21 #include <net/transp_v6.h>
22 #endif
23 #include <net/mptcp.h>
24 #include <uapi/linux/mptcp.h>
25 #include "protocol.h"
26 #include "mib.h"
27 
28 #include <trace/events/mptcp.h>
29 
30 static void mptcp_subflow_ops_undo_override(struct sock *ssk);
31 
SUBFLOW_REQ_INC_STATS(struct request_sock * req,enum linux_mptcp_mib_field field)32 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
33 				  enum linux_mptcp_mib_field field)
34 {
35 	MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
36 }
37 
subflow_req_destructor(struct request_sock * req)38 static void subflow_req_destructor(struct request_sock *req)
39 {
40 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
41 
42 	pr_debug("subflow_req=%p", subflow_req);
43 
44 	if (subflow_req->msk)
45 		sock_put((struct sock *)subflow_req->msk);
46 
47 	mptcp_token_destroy_request(req);
48 }
49 
subflow_generate_hmac(u64 key1,u64 key2,u32 nonce1,u32 nonce2,void * hmac)50 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
51 				  void *hmac)
52 {
53 	u8 msg[8];
54 
55 	put_unaligned_be32(nonce1, &msg[0]);
56 	put_unaligned_be32(nonce2, &msg[4]);
57 
58 	mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
59 }
60 
mptcp_can_accept_new_subflow(const struct mptcp_sock * msk)61 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
62 {
63 	return mptcp_is_fully_established((void *)msk) &&
64 		((mptcp_pm_is_userspace(msk) &&
65 		  mptcp_userspace_pm_active(msk)) ||
66 		 READ_ONCE(msk->pm.accept_subflow));
67 }
68 
69 /* validate received token and create truncated hmac and nonce for SYN-ACK */
subflow_req_create_thmac(struct mptcp_subflow_request_sock * subflow_req)70 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
71 {
72 	struct mptcp_sock *msk = subflow_req->msk;
73 	u8 hmac[SHA256_DIGEST_SIZE];
74 
75 	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
76 
77 	subflow_generate_hmac(msk->local_key, msk->remote_key,
78 			      subflow_req->local_nonce,
79 			      subflow_req->remote_nonce, hmac);
80 
81 	subflow_req->thmac = get_unaligned_be64(hmac);
82 }
83 
subflow_token_join_request(struct request_sock * req)84 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
85 {
86 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
87 	struct mptcp_sock *msk;
88 	int local_id;
89 
90 	msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token);
91 	if (!msk) {
92 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
93 		return NULL;
94 	}
95 
96 	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
97 	if (local_id < 0) {
98 		sock_put((struct sock *)msk);
99 		return NULL;
100 	}
101 	subflow_req->local_id = local_id;
102 
103 	return msk;
104 }
105 
subflow_init_req(struct request_sock * req,const struct sock * sk_listener)106 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
107 {
108 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
109 
110 	subflow_req->mp_capable = 0;
111 	subflow_req->mp_join = 0;
112 	subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener));
113 	subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
114 	subflow_req->msk = NULL;
115 	mptcp_token_init_request(req);
116 }
117 
subflow_use_different_sport(struct mptcp_sock * msk,const struct sock * sk)118 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
119 {
120 	return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
121 }
122 
subflow_add_reset_reason(struct sk_buff * skb,u8 reason)123 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
124 {
125 	struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
126 
127 	if (mpext) {
128 		memset(mpext, 0, sizeof(*mpext));
129 		mpext->reset_reason = reason;
130 	}
131 }
132 
133 /* Init mptcp request socket.
134  *
135  * Returns an error code if a JOIN has failed and a TCP reset
136  * should be sent.
137  */
subflow_check_req(struct request_sock * req,const struct sock * sk_listener,struct sk_buff * skb)138 static int subflow_check_req(struct request_sock *req,
139 			     const struct sock *sk_listener,
140 			     struct sk_buff *skb)
141 {
142 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
143 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
144 	struct mptcp_options_received mp_opt;
145 	bool opt_mp_capable, opt_mp_join;
146 
147 	pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
148 
149 #ifdef CONFIG_TCP_MD5SIG
150 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
151 	 * TCP option space.
152 	 */
153 	if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
154 		return -EINVAL;
155 #endif
156 
157 	mptcp_get_options(skb, &mp_opt);
158 
159 	opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYN);
160 	opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYN);
161 	if (opt_mp_capable) {
162 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
163 
164 		if (opt_mp_join)
165 			return 0;
166 	} else if (opt_mp_join) {
167 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
168 	}
169 
170 	if (opt_mp_capable && listener->request_mptcp) {
171 		int err, retries = MPTCP_TOKEN_MAX_RETRIES;
172 
173 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
174 again:
175 		do {
176 			get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
177 		} while (subflow_req->local_key == 0);
178 
179 		if (unlikely(req->syncookie)) {
180 			mptcp_crypto_key_sha(subflow_req->local_key,
181 					     &subflow_req->token,
182 					     &subflow_req->idsn);
183 			if (mptcp_token_exists(subflow_req->token)) {
184 				if (retries-- > 0)
185 					goto again;
186 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
187 			} else {
188 				subflow_req->mp_capable = 1;
189 			}
190 			return 0;
191 		}
192 
193 		err = mptcp_token_new_request(req);
194 		if (err == 0)
195 			subflow_req->mp_capable = 1;
196 		else if (retries-- > 0)
197 			goto again;
198 		else
199 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
200 
201 	} else if (opt_mp_join && listener->request_mptcp) {
202 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
203 		subflow_req->mp_join = 1;
204 		subflow_req->backup = mp_opt.backup;
205 		subflow_req->remote_id = mp_opt.join_id;
206 		subflow_req->token = mp_opt.token;
207 		subflow_req->remote_nonce = mp_opt.nonce;
208 		subflow_req->msk = subflow_token_join_request(req);
209 
210 		/* Can't fall back to TCP in this case. */
211 		if (!subflow_req->msk) {
212 			subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
213 			return -EPERM;
214 		}
215 
216 		if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
217 			pr_debug("syn inet_sport=%d %d",
218 				 ntohs(inet_sk(sk_listener)->inet_sport),
219 				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
220 			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
221 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
222 				return -EPERM;
223 			}
224 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
225 		}
226 
227 		subflow_req_create_thmac(subflow_req);
228 
229 		if (unlikely(req->syncookie)) {
230 			if (mptcp_can_accept_new_subflow(subflow_req->msk))
231 				subflow_init_req_cookie_join_save(subflow_req, skb);
232 			else
233 				return -EPERM;
234 		}
235 
236 		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
237 			 subflow_req->remote_nonce, subflow_req->msk);
238 	}
239 
240 	return 0;
241 }
242 
mptcp_subflow_init_cookie_req(struct request_sock * req,const struct sock * sk_listener,struct sk_buff * skb)243 int mptcp_subflow_init_cookie_req(struct request_sock *req,
244 				  const struct sock *sk_listener,
245 				  struct sk_buff *skb)
246 {
247 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
248 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
249 	struct mptcp_options_received mp_opt;
250 	bool opt_mp_capable, opt_mp_join;
251 	int err;
252 
253 	subflow_init_req(req, sk_listener);
254 	mptcp_get_options(skb, &mp_opt);
255 
256 	opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK);
257 	opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK);
258 	if (opt_mp_capable && opt_mp_join)
259 		return -EINVAL;
260 
261 	if (opt_mp_capable && listener->request_mptcp) {
262 		if (mp_opt.sndr_key == 0)
263 			return -EINVAL;
264 
265 		subflow_req->local_key = mp_opt.rcvr_key;
266 		err = mptcp_token_new_request(req);
267 		if (err)
268 			return err;
269 
270 		subflow_req->mp_capable = 1;
271 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
272 	} else if (opt_mp_join && listener->request_mptcp) {
273 		if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
274 			return -EINVAL;
275 
276 		subflow_req->mp_join = 1;
277 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
278 	}
279 
280 	return 0;
281 }
282 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
283 
subflow_v4_route_req(const struct sock * sk,struct sk_buff * skb,struct flowi * fl,struct request_sock * req)284 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
285 					      struct sk_buff *skb,
286 					      struct flowi *fl,
287 					      struct request_sock *req)
288 {
289 	struct dst_entry *dst;
290 	int err;
291 
292 	tcp_rsk(req)->is_mptcp = 1;
293 	subflow_init_req(req, sk);
294 
295 	dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
296 	if (!dst)
297 		return NULL;
298 
299 	err = subflow_check_req(req, sk, skb);
300 	if (err == 0)
301 		return dst;
302 
303 	dst_release(dst);
304 	if (!req->syncookie)
305 		tcp_request_sock_ops.send_reset(sk, skb);
306 	return NULL;
307 }
308 
309 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
subflow_v6_route_req(const struct sock * sk,struct sk_buff * skb,struct flowi * fl,struct request_sock * req)310 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
311 					      struct sk_buff *skb,
312 					      struct flowi *fl,
313 					      struct request_sock *req)
314 {
315 	struct dst_entry *dst;
316 	int err;
317 
318 	tcp_rsk(req)->is_mptcp = 1;
319 	subflow_init_req(req, sk);
320 
321 	dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
322 	if (!dst)
323 		return NULL;
324 
325 	err = subflow_check_req(req, sk, skb);
326 	if (err == 0)
327 		return dst;
328 
329 	dst_release(dst);
330 	if (!req->syncookie)
331 		tcp6_request_sock_ops.send_reset(sk, skb);
332 	return NULL;
333 }
334 #endif
335 
336 /* validate received truncated hmac and create hmac for third ACK */
subflow_thmac_valid(struct mptcp_subflow_context * subflow)337 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
338 {
339 	u8 hmac[SHA256_DIGEST_SIZE];
340 	u64 thmac;
341 
342 	subflow_generate_hmac(subflow->remote_key, subflow->local_key,
343 			      subflow->remote_nonce, subflow->local_nonce,
344 			      hmac);
345 
346 	thmac = get_unaligned_be64(hmac);
347 	pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
348 		 subflow, subflow->token, thmac, subflow->thmac);
349 
350 	return thmac == subflow->thmac;
351 }
352 
mptcp_subflow_reset(struct sock * ssk)353 void mptcp_subflow_reset(struct sock *ssk)
354 {
355 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
356 	struct sock *sk = subflow->conn;
357 
358 	/* mptcp_mp_fail_no_response() can reach here on an already closed
359 	 * socket
360 	 */
361 	if (ssk->sk_state == TCP_CLOSE)
362 		return;
363 
364 	/* must hold: tcp_done() could drop last reference on parent */
365 	sock_hold(sk);
366 
367 	tcp_send_active_reset(ssk, GFP_ATOMIC);
368 	tcp_done(ssk);
369 	if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags))
370 		mptcp_schedule_work(sk);
371 
372 	sock_put(sk);
373 }
374 
subflow_use_different_dport(struct mptcp_sock * msk,const struct sock * sk)375 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
376 {
377 	return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
378 }
379 
__mptcp_set_connected(struct sock * sk)380 void __mptcp_set_connected(struct sock *sk)
381 {
382 	if (sk->sk_state == TCP_SYN_SENT) {
383 		inet_sk_state_store(sk, TCP_ESTABLISHED);
384 		sk->sk_state_change(sk);
385 	}
386 }
387 
mptcp_set_connected(struct sock * sk)388 static void mptcp_set_connected(struct sock *sk)
389 {
390 	mptcp_data_lock(sk);
391 	if (!sock_owned_by_user(sk))
392 		__mptcp_set_connected(sk);
393 	else
394 		__set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags);
395 	mptcp_data_unlock(sk);
396 }
397 
subflow_finish_connect(struct sock * sk,const struct sk_buff * skb)398 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
399 {
400 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
401 	struct mptcp_options_received mp_opt;
402 	struct sock *parent = subflow->conn;
403 
404 	subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
405 
406 	/* be sure no special action on any packet other than syn-ack */
407 	if (subflow->conn_finished)
408 		return;
409 
410 	mptcp_propagate_sndbuf(parent, sk);
411 	subflow->rel_write_seq = 1;
412 	subflow->conn_finished = 1;
413 	subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
414 	pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
415 
416 	mptcp_get_options(skb, &mp_opt);
417 	if (subflow->request_mptcp) {
418 		if (!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYNACK)) {
419 			MPTCP_INC_STATS(sock_net(sk),
420 					MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
421 			mptcp_do_fallback(sk);
422 			pr_fallback(mptcp_sk(subflow->conn));
423 			goto fallback;
424 		}
425 
426 		if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
427 			WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true);
428 		if (mp_opt.deny_join_id0)
429 			WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true);
430 		subflow->mp_capable = 1;
431 		subflow->can_ack = 1;
432 		subflow->remote_key = mp_opt.sndr_key;
433 		pr_debug("subflow=%p, remote_key=%llu", subflow,
434 			 subflow->remote_key);
435 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
436 		mptcp_finish_connect(sk);
437 		mptcp_set_connected(parent);
438 	} else if (subflow->request_join) {
439 		u8 hmac[SHA256_DIGEST_SIZE];
440 
441 		if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYNACK)) {
442 			subflow->reset_reason = MPTCP_RST_EMPTCP;
443 			goto do_reset;
444 		}
445 
446 		subflow->backup = mp_opt.backup;
447 		subflow->thmac = mp_opt.thmac;
448 		subflow->remote_nonce = mp_opt.nonce;
449 		subflow->remote_id = mp_opt.join_id;
450 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d",
451 			 subflow, subflow->thmac, subflow->remote_nonce,
452 			 subflow->backup);
453 
454 		if (!subflow_thmac_valid(subflow)) {
455 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
456 			subflow->reset_reason = MPTCP_RST_EMPTCP;
457 			goto do_reset;
458 		}
459 
460 		if (!mptcp_finish_join(sk))
461 			goto do_reset;
462 
463 		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
464 				      subflow->local_nonce,
465 				      subflow->remote_nonce,
466 				      hmac);
467 		memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
468 
469 		subflow->mp_join = 1;
470 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
471 
472 		if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
473 			pr_debug("synack inet_dport=%d %d",
474 				 ntohs(inet_sk(sk)->inet_dport),
475 				 ntohs(inet_sk(parent)->inet_dport));
476 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
477 		}
478 	} else if (mptcp_check_fallback(sk)) {
479 fallback:
480 		mptcp_rcv_space_init(mptcp_sk(parent), sk);
481 		mptcp_set_connected(parent);
482 	}
483 	return;
484 
485 do_reset:
486 	subflow->reset_transient = 0;
487 	mptcp_subflow_reset(sk);
488 }
489 
subflow_set_local_id(struct mptcp_subflow_context * subflow,int local_id)490 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
491 {
492 	subflow->local_id = local_id;
493 	subflow->local_id_valid = 1;
494 }
495 
subflow_chk_local_id(struct sock * sk)496 static int subflow_chk_local_id(struct sock *sk)
497 {
498 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
499 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
500 	int err;
501 
502 	if (likely(subflow->local_id_valid))
503 		return 0;
504 
505 	err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
506 	if (err < 0)
507 		return err;
508 
509 	subflow_set_local_id(subflow, err);
510 	return 0;
511 }
512 
subflow_rebuild_header(struct sock * sk)513 static int subflow_rebuild_header(struct sock *sk)
514 {
515 	int err = subflow_chk_local_id(sk);
516 
517 	if (unlikely(err < 0))
518 		return err;
519 
520 	return inet_sk_rebuild_header(sk);
521 }
522 
523 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
subflow_v6_rebuild_header(struct sock * sk)524 static int subflow_v6_rebuild_header(struct sock *sk)
525 {
526 	int err = subflow_chk_local_id(sk);
527 
528 	if (unlikely(err < 0))
529 		return err;
530 
531 	return inet6_sk_rebuild_header(sk);
532 }
533 #endif
534 
535 static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init;
536 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
537 
subflow_v4_conn_request(struct sock * sk,struct sk_buff * skb)538 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
539 {
540 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
541 
542 	pr_debug("subflow=%p", subflow);
543 
544 	/* Never answer to SYNs sent to broadcast or multicast */
545 	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
546 		goto drop;
547 
548 	return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops,
549 				&subflow_request_sock_ipv4_ops,
550 				sk, skb);
551 drop:
552 	tcp_listendrop(sk);
553 	return 0;
554 }
555 
subflow_v4_req_destructor(struct request_sock * req)556 static void subflow_v4_req_destructor(struct request_sock *req)
557 {
558 	subflow_req_destructor(req);
559 	tcp_request_sock_ops.destructor(req);
560 }
561 
562 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
563 static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init;
564 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
565 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
566 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
567 static struct proto tcpv6_prot_override __ro_after_init;
568 
subflow_v6_conn_request(struct sock * sk,struct sk_buff * skb)569 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
570 {
571 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
572 
573 	pr_debug("subflow=%p", subflow);
574 
575 	if (skb->protocol == htons(ETH_P_IP))
576 		return subflow_v4_conn_request(sk, skb);
577 
578 	if (!ipv6_unicast_destination(skb))
579 		goto drop;
580 
581 	if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
582 		__IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
583 		return 0;
584 	}
585 
586 	return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops,
587 				&subflow_request_sock_ipv6_ops, sk, skb);
588 
589 drop:
590 	tcp_listendrop(sk);
591 	return 0; /* don't send reset */
592 }
593 
subflow_v6_req_destructor(struct request_sock * req)594 static void subflow_v6_req_destructor(struct request_sock *req)
595 {
596 	subflow_req_destructor(req);
597 	tcp6_request_sock_ops.destructor(req);
598 }
599 #endif
600 
mptcp_subflow_reqsk_alloc(const struct request_sock_ops * ops,struct sock * sk_listener,bool attach_listener)601 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
602 					       struct sock *sk_listener,
603 					       bool attach_listener)
604 {
605 	if (ops->family == AF_INET)
606 		ops = &mptcp_subflow_v4_request_sock_ops;
607 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
608 	else if (ops->family == AF_INET6)
609 		ops = &mptcp_subflow_v6_request_sock_ops;
610 #endif
611 
612 	return inet_reqsk_alloc(ops, sk_listener, attach_listener);
613 }
614 EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc);
615 
616 /* validate hmac received in third ACK */
subflow_hmac_valid(const struct request_sock * req,const struct mptcp_options_received * mp_opt)617 static bool subflow_hmac_valid(const struct request_sock *req,
618 			       const struct mptcp_options_received *mp_opt)
619 {
620 	const struct mptcp_subflow_request_sock *subflow_req;
621 	u8 hmac[SHA256_DIGEST_SIZE];
622 	struct mptcp_sock *msk;
623 
624 	subflow_req = mptcp_subflow_rsk(req);
625 	msk = subflow_req->msk;
626 	if (!msk)
627 		return false;
628 
629 	subflow_generate_hmac(msk->remote_key, msk->local_key,
630 			      subflow_req->remote_nonce,
631 			      subflow_req->local_nonce, hmac);
632 
633 	return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
634 }
635 
subflow_ulp_fallback(struct sock * sk,struct mptcp_subflow_context * old_ctx)636 static void subflow_ulp_fallback(struct sock *sk,
637 				 struct mptcp_subflow_context *old_ctx)
638 {
639 	struct inet_connection_sock *icsk = inet_csk(sk);
640 
641 	mptcp_subflow_tcp_fallback(sk, old_ctx);
642 	icsk->icsk_ulp_ops = NULL;
643 	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
644 	tcp_sk(sk)->is_mptcp = 0;
645 
646 	mptcp_subflow_ops_undo_override(sk);
647 }
648 
mptcp_subflow_drop_ctx(struct sock * ssk)649 void mptcp_subflow_drop_ctx(struct sock *ssk)
650 {
651 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
652 
653 	if (!ctx)
654 		return;
655 
656 	list_del(&mptcp_subflow_ctx(ssk)->node);
657 	if (inet_csk(ssk)->icsk_ulp_ops) {
658 		subflow_ulp_fallback(ssk, ctx);
659 		if (ctx->conn)
660 			sock_put(ctx->conn);
661 	}
662 
663 	kfree_rcu(ctx, rcu);
664 }
665 
mptcp_subflow_fully_established(struct mptcp_subflow_context * subflow,struct mptcp_options_received * mp_opt)666 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
667 				     struct mptcp_options_received *mp_opt)
668 {
669 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
670 
671 	subflow->remote_key = mp_opt->sndr_key;
672 	subflow->fully_established = 1;
673 	subflow->can_ack = 1;
674 	WRITE_ONCE(msk->fully_established, true);
675 }
676 
subflow_syn_recv_sock(const struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct dst_entry * dst,struct request_sock * req_unhash,bool * own_req)677 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
678 					  struct sk_buff *skb,
679 					  struct request_sock *req,
680 					  struct dst_entry *dst,
681 					  struct request_sock *req_unhash,
682 					  bool *own_req)
683 {
684 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
685 	struct mptcp_subflow_request_sock *subflow_req;
686 	struct mptcp_options_received mp_opt;
687 	bool fallback, fallback_is_fatal;
688 	struct mptcp_sock *owner;
689 	struct sock *child;
690 
691 	pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
692 
693 	/* After child creation we must look for MPC even when options
694 	 * are not parsed
695 	 */
696 	mp_opt.suboptions = 0;
697 
698 	/* hopefully temporary handling for MP_JOIN+syncookie */
699 	subflow_req = mptcp_subflow_rsk(req);
700 	fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
701 	fallback = !tcp_rsk(req)->is_mptcp;
702 	if (fallback)
703 		goto create_child;
704 
705 	/* if the sk is MP_CAPABLE, we try to fetch the client key */
706 	if (subflow_req->mp_capable) {
707 		/* we can receive and accept an in-window, out-of-order pkt,
708 		 * which may not carry the MP_CAPABLE opt even on mptcp enabled
709 		 * paths: always try to extract the peer key, and fallback
710 		 * for packets missing it.
711 		 * Even OoO DSS packets coming legitly after dropped or
712 		 * reordered MPC will cause fallback, but we don't have other
713 		 * options.
714 		 */
715 		mptcp_get_options(skb, &mp_opt);
716 		if (!(mp_opt.suboptions &
717 		      (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_ACK)))
718 			fallback = true;
719 
720 	} else if (subflow_req->mp_join) {
721 		mptcp_get_options(skb, &mp_opt);
722 		if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK) ||
723 		    !subflow_hmac_valid(req, &mp_opt) ||
724 		    !mptcp_can_accept_new_subflow(subflow_req->msk)) {
725 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
726 			fallback = true;
727 		}
728 	}
729 
730 create_child:
731 	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
732 						     req_unhash, own_req);
733 
734 	if (child && *own_req) {
735 		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
736 
737 		tcp_rsk(req)->drop_req = false;
738 
739 		/* we need to fallback on ctx allocation failure and on pre-reqs
740 		 * checking above. In the latter scenario we additionally need
741 		 * to reset the context to non MPTCP status.
742 		 */
743 		if (!ctx || fallback) {
744 			if (fallback_is_fatal) {
745 				subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
746 				goto dispose_child;
747 			}
748 			goto fallback;
749 		}
750 
751 		/* ssk inherits options of listener sk */
752 		ctx->setsockopt_seq = listener->setsockopt_seq;
753 
754 		if (ctx->mp_capable) {
755 			ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
756 			if (!ctx->conn)
757 				goto fallback;
758 
759 			owner = mptcp_sk(ctx->conn);
760 			mptcp_pm_new_connection(owner, child, 1);
761 
762 			/* with OoO packets we can reach here without ingress
763 			 * mpc option
764 			 */
765 			if (mp_opt.suboptions & OPTIONS_MPTCP_MPC) {
766 				mptcp_subflow_fully_established(ctx, &mp_opt);
767 				mptcp_pm_fully_established(owner, child, GFP_ATOMIC);
768 				ctx->pm_notified = 1;
769 			}
770 		} else if (ctx->mp_join) {
771 			owner = subflow_req->msk;
772 			if (!owner) {
773 				subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
774 				goto dispose_child;
775 			}
776 
777 			/* move the msk reference ownership to the subflow */
778 			subflow_req->msk = NULL;
779 			ctx->conn = (struct sock *)owner;
780 
781 			if (subflow_use_different_sport(owner, sk)) {
782 				pr_debug("ack inet_sport=%d %d",
783 					 ntohs(inet_sk(sk)->inet_sport),
784 					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
785 				if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
786 					SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
787 					goto dispose_child;
788 				}
789 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
790 			}
791 
792 			if (!mptcp_finish_join(child))
793 				goto dispose_child;
794 
795 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
796 			tcp_rsk(req)->drop_req = true;
797 		}
798 	}
799 
800 	/* check for expected invariant - should never trigger, just help
801 	 * catching eariler subtle bugs
802 	 */
803 	WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
804 		     (!mptcp_subflow_ctx(child) ||
805 		      !mptcp_subflow_ctx(child)->conn));
806 	return child;
807 
808 dispose_child:
809 	mptcp_subflow_drop_ctx(child);
810 	tcp_rsk(req)->drop_req = true;
811 	inet_csk_prepare_for_destroy_sock(child);
812 	tcp_done(child);
813 	req->rsk_ops->send_reset(sk, skb);
814 
815 	/* The last child reference will be released by the caller */
816 	return child;
817 
818 fallback:
819 	mptcp_subflow_drop_ctx(child);
820 	return child;
821 }
822 
823 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
824 static struct proto tcp_prot_override __ro_after_init;
825 
826 enum mapping_status {
827 	MAPPING_OK,
828 	MAPPING_INVALID,
829 	MAPPING_EMPTY,
830 	MAPPING_DATA_FIN,
831 	MAPPING_DUMMY,
832 	MAPPING_BAD_CSUM
833 };
834 
dbg_bad_map(struct mptcp_subflow_context * subflow,u32 ssn)835 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
836 {
837 	pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
838 		 ssn, subflow->map_subflow_seq, subflow->map_data_len);
839 }
840 
skb_is_fully_mapped(struct sock * ssk,struct sk_buff * skb)841 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
842 {
843 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
844 	unsigned int skb_consumed;
845 
846 	skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
847 	if (WARN_ON_ONCE(skb_consumed >= skb->len))
848 		return true;
849 
850 	return skb->len - skb_consumed <= subflow->map_data_len -
851 					  mptcp_subflow_get_map_offset(subflow);
852 }
853 
validate_mapping(struct sock * ssk,struct sk_buff * skb)854 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
855 {
856 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
857 	u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
858 
859 	if (unlikely(before(ssn, subflow->map_subflow_seq))) {
860 		/* Mapping covers data later in the subflow stream,
861 		 * currently unsupported.
862 		 */
863 		dbg_bad_map(subflow, ssn);
864 		return false;
865 	}
866 	if (unlikely(!before(ssn, subflow->map_subflow_seq +
867 				  subflow->map_data_len))) {
868 		/* Mapping does covers past subflow data, invalid */
869 		dbg_bad_map(subflow, ssn);
870 		return false;
871 	}
872 	return true;
873 }
874 
validate_data_csum(struct sock * ssk,struct sk_buff * skb,bool csum_reqd)875 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
876 					      bool csum_reqd)
877 {
878 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
879 	u32 offset, seq, delta;
880 	__sum16 csum;
881 	int len;
882 
883 	if (!csum_reqd)
884 		return MAPPING_OK;
885 
886 	/* mapping already validated on previous traversal */
887 	if (subflow->map_csum_len == subflow->map_data_len)
888 		return MAPPING_OK;
889 
890 	/* traverse the receive queue, ensuring it contains a full
891 	 * DSS mapping and accumulating the related csum.
892 	 * Preserve the accoumlate csum across multiple calls, to compute
893 	 * the csum only once
894 	 */
895 	delta = subflow->map_data_len - subflow->map_csum_len;
896 	for (;;) {
897 		seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
898 		offset = seq - TCP_SKB_CB(skb)->seq;
899 
900 		/* if the current skb has not been accounted yet, csum its contents
901 		 * up to the amount covered by the current DSS
902 		 */
903 		if (offset < skb->len) {
904 			__wsum csum;
905 
906 			len = min(skb->len - offset, delta);
907 			csum = skb_checksum(skb, offset, len, 0);
908 			subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
909 								subflow->map_csum_len);
910 
911 			delta -= len;
912 			subflow->map_csum_len += len;
913 		}
914 		if (delta == 0)
915 			break;
916 
917 		if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
918 			/* if this subflow is closed, the partial mapping
919 			 * will be never completed; flush the pending skbs, so
920 			 * that subflow_sched_work_if_closed() can kick in
921 			 */
922 			if (unlikely(ssk->sk_state == TCP_CLOSE))
923 				while ((skb = skb_peek(&ssk->sk_receive_queue)))
924 					sk_eat_skb(ssk, skb);
925 
926 			/* not enough data to validate the csum */
927 			return MAPPING_EMPTY;
928 		}
929 
930 		/* the DSS mapping for next skbs will be validated later,
931 		 * when a get_mapping_status call will process such skb
932 		 */
933 		skb = skb->next;
934 	}
935 
936 	/* note that 'map_data_len' accounts only for the carried data, does
937 	 * not include the eventual seq increment due to the data fin,
938 	 * while the pseudo header requires the original DSS data len,
939 	 * including that
940 	 */
941 	csum = __mptcp_make_csum(subflow->map_seq,
942 				 subflow->map_subflow_seq,
943 				 subflow->map_data_len + subflow->map_data_fin,
944 				 subflow->map_data_csum);
945 	if (unlikely(csum)) {
946 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
947 		return MAPPING_BAD_CSUM;
948 	}
949 
950 	subflow->valid_csum_seen = 1;
951 	return MAPPING_OK;
952 }
953 
get_mapping_status(struct sock * ssk,struct mptcp_sock * msk)954 static enum mapping_status get_mapping_status(struct sock *ssk,
955 					      struct mptcp_sock *msk)
956 {
957 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
958 	bool csum_reqd = READ_ONCE(msk->csum_enabled);
959 	struct mptcp_ext *mpext;
960 	struct sk_buff *skb;
961 	u16 data_len;
962 	u64 map_seq;
963 
964 	skb = skb_peek(&ssk->sk_receive_queue);
965 	if (!skb)
966 		return MAPPING_EMPTY;
967 
968 	if (mptcp_check_fallback(ssk))
969 		return MAPPING_DUMMY;
970 
971 	mpext = mptcp_get_ext(skb);
972 	if (!mpext || !mpext->use_map) {
973 		if (!subflow->map_valid && !skb->len) {
974 			/* the TCP stack deliver 0 len FIN pkt to the receive
975 			 * queue, that is the only 0len pkts ever expected here,
976 			 * and we can admit no mapping only for 0 len pkts
977 			 */
978 			if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
979 				WARN_ONCE(1, "0len seq %d:%d flags %x",
980 					  TCP_SKB_CB(skb)->seq,
981 					  TCP_SKB_CB(skb)->end_seq,
982 					  TCP_SKB_CB(skb)->tcp_flags);
983 			sk_eat_skb(ssk, skb);
984 			return MAPPING_EMPTY;
985 		}
986 
987 		if (!subflow->map_valid)
988 			return MAPPING_INVALID;
989 
990 		goto validate_seq;
991 	}
992 
993 	trace_get_mapping_status(mpext);
994 
995 	data_len = mpext->data_len;
996 	if (data_len == 0) {
997 		pr_debug("infinite mapping received");
998 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
999 		subflow->map_data_len = 0;
1000 		return MAPPING_INVALID;
1001 	}
1002 
1003 	if (mpext->data_fin == 1) {
1004 		if (data_len == 1) {
1005 			bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1006 								 mpext->dsn64);
1007 			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
1008 			if (subflow->map_valid) {
1009 				/* A DATA_FIN might arrive in a DSS
1010 				 * option before the previous mapping
1011 				 * has been fully consumed. Continue
1012 				 * handling the existing mapping.
1013 				 */
1014 				skb_ext_del(skb, SKB_EXT_MPTCP);
1015 				return MAPPING_OK;
1016 			} else {
1017 				if (updated)
1018 					mptcp_schedule_work((struct sock *)msk);
1019 
1020 				return MAPPING_DATA_FIN;
1021 			}
1022 		} else {
1023 			u64 data_fin_seq = mpext->data_seq + data_len - 1;
1024 
1025 			/* If mpext->data_seq is a 32-bit value, data_fin_seq
1026 			 * must also be limited to 32 bits.
1027 			 */
1028 			if (!mpext->dsn64)
1029 				data_fin_seq &= GENMASK_ULL(31, 0);
1030 
1031 			mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1032 			pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
1033 				 data_fin_seq, mpext->dsn64);
1034 		}
1035 
1036 		/* Adjust for DATA_FIN using 1 byte of sequence space */
1037 		data_len--;
1038 	}
1039 
1040 	map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1041 	WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1042 
1043 	if (subflow->map_valid) {
1044 		/* Allow replacing only with an identical map */
1045 		if (subflow->map_seq == map_seq &&
1046 		    subflow->map_subflow_seq == mpext->subflow_seq &&
1047 		    subflow->map_data_len == data_len &&
1048 		    subflow->map_csum_reqd == mpext->csum_reqd) {
1049 			skb_ext_del(skb, SKB_EXT_MPTCP);
1050 			goto validate_csum;
1051 		}
1052 
1053 		/* If this skb data are fully covered by the current mapping,
1054 		 * the new map would need caching, which is not supported
1055 		 */
1056 		if (skb_is_fully_mapped(ssk, skb)) {
1057 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1058 			return MAPPING_INVALID;
1059 		}
1060 
1061 		/* will validate the next map after consuming the current one */
1062 		goto validate_csum;
1063 	}
1064 
1065 	subflow->map_seq = map_seq;
1066 	subflow->map_subflow_seq = mpext->subflow_seq;
1067 	subflow->map_data_len = data_len;
1068 	subflow->map_valid = 1;
1069 	subflow->map_data_fin = mpext->data_fin;
1070 	subflow->mpc_map = mpext->mpc_map;
1071 	subflow->map_csum_reqd = mpext->csum_reqd;
1072 	subflow->map_csum_len = 0;
1073 	subflow->map_data_csum = csum_unfold(mpext->csum);
1074 
1075 	/* Cfr RFC 8684 Section 3.3.0 */
1076 	if (unlikely(subflow->map_csum_reqd != csum_reqd))
1077 		return MAPPING_INVALID;
1078 
1079 	pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1080 		 subflow->map_seq, subflow->map_subflow_seq,
1081 		 subflow->map_data_len, subflow->map_csum_reqd,
1082 		 subflow->map_data_csum);
1083 
1084 validate_seq:
1085 	/* we revalidate valid mapping on new skb, because we must ensure
1086 	 * the current skb is completely covered by the available mapping
1087 	 */
1088 	if (!validate_mapping(ssk, skb)) {
1089 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1090 		return MAPPING_INVALID;
1091 	}
1092 
1093 	skb_ext_del(skb, SKB_EXT_MPTCP);
1094 
1095 validate_csum:
1096 	return validate_data_csum(ssk, skb, csum_reqd);
1097 }
1098 
mptcp_subflow_discard_data(struct sock * ssk,struct sk_buff * skb,u64 limit)1099 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1100 				       u64 limit)
1101 {
1102 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1103 	bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1104 	u32 incr;
1105 
1106 	incr = limit >= skb->len ? skb->len + fin : limit;
1107 
1108 	pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1109 		 subflow->map_subflow_seq);
1110 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1111 	tcp_sk(ssk)->copied_seq += incr;
1112 	if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1113 		sk_eat_skb(ssk, skb);
1114 	if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1115 		subflow->map_valid = 0;
1116 }
1117 
1118 /* sched mptcp worker to remove the subflow if no more data is pending */
subflow_sched_work_if_closed(struct mptcp_sock * msk,struct sock * ssk)1119 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1120 {
1121 	if (likely(ssk->sk_state != TCP_CLOSE))
1122 		return;
1123 
1124 	if (skb_queue_empty(&ssk->sk_receive_queue) &&
1125 	    !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
1126 		mptcp_schedule_work((struct sock *)msk);
1127 }
1128 
subflow_can_fallback(struct mptcp_subflow_context * subflow)1129 static bool subflow_can_fallback(struct mptcp_subflow_context *subflow)
1130 {
1131 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1132 
1133 	if (subflow->mp_join)
1134 		return false;
1135 	else if (READ_ONCE(msk->csum_enabled))
1136 		return !subflow->valid_csum_seen;
1137 	else
1138 		return !subflow->fully_established;
1139 }
1140 
mptcp_subflow_fail(struct mptcp_sock * msk,struct sock * ssk)1141 static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1142 {
1143 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1144 	unsigned long fail_tout;
1145 
1146 	/* greceful failure can happen only on the MPC subflow */
1147 	if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1148 		return;
1149 
1150 	/* since the close timeout take precedence on the fail one,
1151 	 * no need to start the latter when the first is already set
1152 	 */
1153 	if (sock_flag((struct sock *)msk, SOCK_DEAD))
1154 		return;
1155 
1156 	/* we don't need extreme accuracy here, use a zero fail_tout as special
1157 	 * value meaning no fail timeout at all;
1158 	 */
1159 	fail_tout = jiffies + TCP_RTO_MAX;
1160 	if (!fail_tout)
1161 		fail_tout = 1;
1162 	WRITE_ONCE(subflow->fail_tout, fail_tout);
1163 	tcp_send_ack(ssk);
1164 
1165 	mptcp_reset_tout_timer(msk, subflow->fail_tout);
1166 }
1167 
subflow_check_data_avail(struct sock * ssk)1168 static bool subflow_check_data_avail(struct sock *ssk)
1169 {
1170 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1171 	enum mapping_status status;
1172 	struct mptcp_sock *msk;
1173 	struct sk_buff *skb;
1174 
1175 	if (!skb_peek(&ssk->sk_receive_queue))
1176 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1177 	if (subflow->data_avail)
1178 		return true;
1179 
1180 	msk = mptcp_sk(subflow->conn);
1181 	for (;;) {
1182 		u64 ack_seq;
1183 		u64 old_ack;
1184 
1185 		status = get_mapping_status(ssk, msk);
1186 		trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1187 		if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1188 			     status == MAPPING_BAD_CSUM))
1189 			goto fallback;
1190 
1191 		if (status != MAPPING_OK)
1192 			goto no_data;
1193 
1194 		skb = skb_peek(&ssk->sk_receive_queue);
1195 		if (WARN_ON_ONCE(!skb))
1196 			goto no_data;
1197 
1198 		/* if msk lacks the remote key, this subflow must provide an
1199 		 * MP_CAPABLE-based mapping
1200 		 */
1201 		if (unlikely(!READ_ONCE(msk->can_ack))) {
1202 			if (!subflow->mpc_map)
1203 				goto fallback;
1204 			WRITE_ONCE(msk->remote_key, subflow->remote_key);
1205 			WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1206 			WRITE_ONCE(msk->can_ack, true);
1207 		}
1208 
1209 		old_ack = READ_ONCE(msk->ack_seq);
1210 		ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1211 		pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1212 			 ack_seq);
1213 		if (unlikely(before64(ack_seq, old_ack))) {
1214 			mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1215 			continue;
1216 		}
1217 
1218 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1219 		break;
1220 	}
1221 	return true;
1222 
1223 no_data:
1224 	subflow_sched_work_if_closed(msk, ssk);
1225 	return false;
1226 
1227 fallback:
1228 	if (!__mptcp_check_fallback(msk)) {
1229 		/* RFC 8684 section 3.7. */
1230 		if (status == MAPPING_BAD_CSUM &&
1231 		    (subflow->mp_join || subflow->valid_csum_seen)) {
1232 			subflow->send_mp_fail = 1;
1233 
1234 			if (!READ_ONCE(msk->allow_infinite_fallback)) {
1235 				subflow->reset_transient = 0;
1236 				subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1237 				goto reset;
1238 			}
1239 			mptcp_subflow_fail(msk, ssk);
1240 			WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1241 			return true;
1242 		}
1243 
1244 		if (!subflow_can_fallback(subflow) && subflow->map_data_len) {
1245 			/* fatal protocol error, close the socket.
1246 			 * subflow_error_report() will introduce the appropriate barriers
1247 			 */
1248 			subflow->reset_transient = 0;
1249 			subflow->reset_reason = MPTCP_RST_EMPTCP;
1250 
1251 reset:
1252 			WRITE_ONCE(ssk->sk_err, EBADMSG);
1253 			tcp_set_state(ssk, TCP_CLOSE);
1254 			while ((skb = skb_peek(&ssk->sk_receive_queue)))
1255 				sk_eat_skb(ssk, skb);
1256 			tcp_send_active_reset(ssk, GFP_ATOMIC);
1257 			WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1258 			return false;
1259 		}
1260 
1261 		mptcp_do_fallback(ssk);
1262 	}
1263 
1264 	skb = skb_peek(&ssk->sk_receive_queue);
1265 	subflow->map_valid = 1;
1266 	subflow->map_seq = READ_ONCE(msk->ack_seq);
1267 	subflow->map_data_len = skb->len;
1268 	subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1269 	WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1270 	return true;
1271 }
1272 
mptcp_subflow_data_available(struct sock * sk)1273 bool mptcp_subflow_data_available(struct sock *sk)
1274 {
1275 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1276 
1277 	/* check if current mapping is still valid */
1278 	if (subflow->map_valid &&
1279 	    mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1280 		subflow->map_valid = 0;
1281 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1282 
1283 		pr_debug("Done with mapping: seq=%u data_len=%u",
1284 			 subflow->map_subflow_seq,
1285 			 subflow->map_data_len);
1286 	}
1287 
1288 	return subflow_check_data_avail(sk);
1289 }
1290 
1291 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1292  * not the ssk one.
1293  *
1294  * In mptcp, rwin is about the mptcp-level connection data.
1295  *
1296  * Data that is still on the ssk rx queue can thus be ignored,
1297  * as far as mptcp peer is concerned that data is still inflight.
1298  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1299  */
mptcp_space(const struct sock * ssk,int * space,int * full_space)1300 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1301 {
1302 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1303 	const struct sock *sk = subflow->conn;
1304 
1305 	*space = __mptcp_space(sk);
1306 	*full_space = tcp_full_space(sk);
1307 }
1308 
subflow_error_report(struct sock * ssk)1309 static void subflow_error_report(struct sock *ssk)
1310 {
1311 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1312 
1313 	/* bail early if this is a no-op, so that we avoid introducing a
1314 	 * problematic lockdep dependency between TCP accept queue lock
1315 	 * and msk socket spinlock
1316 	 */
1317 	if (!sk->sk_socket)
1318 		return;
1319 
1320 	mptcp_data_lock(sk);
1321 	if (!sock_owned_by_user(sk))
1322 		__mptcp_error_report(sk);
1323 	else
1324 		__set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
1325 	mptcp_data_unlock(sk);
1326 }
1327 
subflow_data_ready(struct sock * sk)1328 static void subflow_data_ready(struct sock *sk)
1329 {
1330 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1331 	u16 state = 1 << inet_sk_state_load(sk);
1332 	struct sock *parent = subflow->conn;
1333 	struct mptcp_sock *msk;
1334 
1335 	msk = mptcp_sk(parent);
1336 	if (state & TCPF_LISTEN) {
1337 		/* MPJ subflow are removed from accept queue before reaching here,
1338 		 * avoid stray wakeups
1339 		 */
1340 		if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1341 			return;
1342 
1343 		parent->sk_data_ready(parent);
1344 		return;
1345 	}
1346 
1347 	WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1348 		     !subflow->mp_join && !(state & TCPF_CLOSE));
1349 
1350 	if (mptcp_subflow_data_available(sk))
1351 		mptcp_data_ready(parent, sk);
1352 	else if (unlikely(sk->sk_err))
1353 		subflow_error_report(sk);
1354 }
1355 
subflow_write_space(struct sock * ssk)1356 static void subflow_write_space(struct sock *ssk)
1357 {
1358 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1359 
1360 	mptcp_propagate_sndbuf(sk, ssk);
1361 	mptcp_write_space(sk);
1362 }
1363 
1364 static const struct inet_connection_sock_af_ops *
subflow_default_af_ops(struct sock * sk)1365 subflow_default_af_ops(struct sock *sk)
1366 {
1367 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1368 	if (sk->sk_family == AF_INET6)
1369 		return &subflow_v6_specific;
1370 #endif
1371 	return &subflow_specific;
1372 }
1373 
1374 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcpv6_handle_mapped(struct sock * sk,bool mapped)1375 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1376 {
1377 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1378 	struct inet_connection_sock *icsk = inet_csk(sk);
1379 	const struct inet_connection_sock_af_ops *target;
1380 
1381 	target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1382 
1383 	pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1384 		 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1385 
1386 	if (likely(icsk->icsk_af_ops == target))
1387 		return;
1388 
1389 	subflow->icsk_af_ops = icsk->icsk_af_ops;
1390 	icsk->icsk_af_ops = target;
1391 }
1392 #endif
1393 
mptcp_info2sockaddr(const struct mptcp_addr_info * info,struct sockaddr_storage * addr,unsigned short family)1394 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1395 			 struct sockaddr_storage *addr,
1396 			 unsigned short family)
1397 {
1398 	memset(addr, 0, sizeof(*addr));
1399 	addr->ss_family = family;
1400 	if (addr->ss_family == AF_INET) {
1401 		struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1402 
1403 		if (info->family == AF_INET)
1404 			in_addr->sin_addr = info->addr;
1405 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1406 		else if (ipv6_addr_v4mapped(&info->addr6))
1407 			in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1408 #endif
1409 		in_addr->sin_port = info->port;
1410 	}
1411 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1412 	else if (addr->ss_family == AF_INET6) {
1413 		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1414 
1415 		if (info->family == AF_INET)
1416 			ipv6_addr_set_v4mapped(info->addr.s_addr,
1417 					       &in6_addr->sin6_addr);
1418 		else
1419 			in6_addr->sin6_addr = info->addr6;
1420 		in6_addr->sin6_port = info->port;
1421 	}
1422 #endif
1423 }
1424 
__mptcp_subflow_connect(struct sock * sk,const struct mptcp_addr_info * loc,const struct mptcp_addr_info * remote)1425 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1426 			    const struct mptcp_addr_info *remote)
1427 {
1428 	struct mptcp_sock *msk = mptcp_sk(sk);
1429 	struct mptcp_subflow_context *subflow;
1430 	struct sockaddr_storage addr;
1431 	int remote_id = remote->id;
1432 	int local_id = loc->id;
1433 	int err = -ENOTCONN;
1434 	struct socket *sf;
1435 	struct sock *ssk;
1436 	u32 remote_token;
1437 	int addrlen;
1438 	int ifindex;
1439 	u8 flags;
1440 
1441 	if (!mptcp_is_fully_established(sk))
1442 		goto err_out;
1443 
1444 	err = mptcp_subflow_create_socket(sk, loc->family, &sf);
1445 	if (err)
1446 		goto err_out;
1447 
1448 	ssk = sf->sk;
1449 	subflow = mptcp_subflow_ctx(ssk);
1450 	do {
1451 		get_random_bytes(&subflow->local_nonce, sizeof(u32));
1452 	} while (!subflow->local_nonce);
1453 
1454 	if (local_id)
1455 		subflow_set_local_id(subflow, local_id);
1456 
1457 	mptcp_pm_get_flags_and_ifindex_by_id(msk, local_id,
1458 					     &flags, &ifindex);
1459 	subflow->remote_key = msk->remote_key;
1460 	subflow->local_key = msk->local_key;
1461 	subflow->token = msk->token;
1462 	mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1463 
1464 	addrlen = sizeof(struct sockaddr_in);
1465 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1466 	if (addr.ss_family == AF_INET6)
1467 		addrlen = sizeof(struct sockaddr_in6);
1468 #endif
1469 	mptcp_sockopt_sync(msk, ssk);
1470 
1471 	ssk->sk_bound_dev_if = ifindex;
1472 	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1473 	if (err)
1474 		goto failed;
1475 
1476 	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1477 	pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1478 		 remote_token, local_id, remote_id);
1479 	subflow->remote_token = remote_token;
1480 	subflow->remote_id = remote_id;
1481 	subflow->request_join = 1;
1482 	subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1483 	mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1484 
1485 	sock_hold(ssk);
1486 	list_add_tail(&subflow->node, &msk->conn_list);
1487 	err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1488 	if (err && err != -EINPROGRESS)
1489 		goto failed_unlink;
1490 
1491 	/* discard the subflow socket */
1492 	mptcp_sock_graft(ssk, sk->sk_socket);
1493 	iput(SOCK_INODE(sf));
1494 	WRITE_ONCE(msk->allow_infinite_fallback, false);
1495 	mptcp_stop_tout_timer(sk);
1496 	return 0;
1497 
1498 failed_unlink:
1499 	list_del(&subflow->node);
1500 	sock_put(mptcp_subflow_tcp_sock(subflow));
1501 
1502 failed:
1503 	subflow->disposable = 1;
1504 	sock_release(sf);
1505 
1506 err_out:
1507 	/* we account subflows before the creation, and this failures will not
1508 	 * be caught by sk_state_change()
1509 	 */
1510 	mptcp_pm_close_subflow(msk);
1511 	return err;
1512 }
1513 
mptcp_attach_cgroup(struct sock * parent,struct sock * child)1514 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1515 {
1516 #ifdef CONFIG_SOCK_CGROUP_DATA
1517 	struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1518 				*child_skcd = &child->sk_cgrp_data;
1519 
1520 	/* only the additional subflows created by kworkers have to be modified */
1521 	if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1522 	    cgroup_id(sock_cgroup_ptr(child_skcd))) {
1523 #ifdef CONFIG_MEMCG
1524 		struct mem_cgroup *memcg = parent->sk_memcg;
1525 
1526 		mem_cgroup_sk_free(child);
1527 		if (memcg && css_tryget(&memcg->css))
1528 			child->sk_memcg = memcg;
1529 #endif /* CONFIG_MEMCG */
1530 
1531 		cgroup_sk_free(child_skcd);
1532 		*child_skcd = *parent_skcd;
1533 		cgroup_sk_clone(child_skcd);
1534 	}
1535 #endif /* CONFIG_SOCK_CGROUP_DATA */
1536 }
1537 
mptcp_subflow_ops_override(struct sock * ssk)1538 static void mptcp_subflow_ops_override(struct sock *ssk)
1539 {
1540 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1541 	if (ssk->sk_prot == &tcpv6_prot)
1542 		ssk->sk_prot = &tcpv6_prot_override;
1543 	else
1544 #endif
1545 		ssk->sk_prot = &tcp_prot_override;
1546 }
1547 
mptcp_subflow_ops_undo_override(struct sock * ssk)1548 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1549 {
1550 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1551 	if (ssk->sk_prot == &tcpv6_prot_override)
1552 		ssk->sk_prot = &tcpv6_prot;
1553 	else
1554 #endif
1555 		ssk->sk_prot = &tcp_prot;
1556 }
1557 
mptcp_subflow_create_socket(struct sock * sk,unsigned short family,struct socket ** new_sock)1558 int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
1559 				struct socket **new_sock)
1560 {
1561 	struct mptcp_subflow_context *subflow;
1562 	struct net *net = sock_net(sk);
1563 	struct socket *sf;
1564 	int err;
1565 
1566 	/* un-accepted server sockets can reach here - on bad configuration
1567 	 * bail early to avoid greater trouble later
1568 	 */
1569 	if (unlikely(!sk->sk_socket))
1570 		return -EINVAL;
1571 
1572 	err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf);
1573 	if (err)
1574 		return err;
1575 
1576 	lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
1577 
1578 	/* the newly created socket has to be in the same cgroup as its parent */
1579 	mptcp_attach_cgroup(sk, sf->sk);
1580 
1581 	/* kernel sockets do not by default acquire net ref, but TCP timer
1582 	 * needs it.
1583 	 */
1584 	sf->sk->sk_net_refcnt = 1;
1585 	get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
1586 	sock_inuse_add(net, 1);
1587 	err = tcp_set_ulp(sf->sk, "mptcp");
1588 	release_sock(sf->sk);
1589 
1590 	if (err) {
1591 		sock_release(sf);
1592 		return err;
1593 	}
1594 
1595 	/* the newly created socket really belongs to the owning MPTCP master
1596 	 * socket, even if for additional subflows the allocation is performed
1597 	 * by a kernel workqueue. Adjust inode references, so that the
1598 	 * procfs/diag interfaces really show this one belonging to the correct
1599 	 * user.
1600 	 */
1601 	SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1602 	SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1603 	SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1604 
1605 	subflow = mptcp_subflow_ctx(sf->sk);
1606 	pr_debug("subflow=%p", subflow);
1607 
1608 	*new_sock = sf;
1609 	sock_hold(sk);
1610 	subflow->conn = sk;
1611 	mptcp_subflow_ops_override(sf->sk);
1612 
1613 	return 0;
1614 }
1615 
subflow_create_ctx(struct sock * sk,gfp_t priority)1616 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1617 							gfp_t priority)
1618 {
1619 	struct inet_connection_sock *icsk = inet_csk(sk);
1620 	struct mptcp_subflow_context *ctx;
1621 
1622 	ctx = kzalloc(sizeof(*ctx), priority);
1623 	if (!ctx)
1624 		return NULL;
1625 
1626 	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1627 	INIT_LIST_HEAD(&ctx->node);
1628 	INIT_LIST_HEAD(&ctx->delegated_node);
1629 
1630 	pr_debug("subflow=%p", ctx);
1631 
1632 	ctx->tcp_sock = sk;
1633 
1634 	return ctx;
1635 }
1636 
__subflow_state_change(struct sock * sk)1637 static void __subflow_state_change(struct sock *sk)
1638 {
1639 	struct socket_wq *wq;
1640 
1641 	rcu_read_lock();
1642 	wq = rcu_dereference(sk->sk_wq);
1643 	if (skwq_has_sleeper(wq))
1644 		wake_up_interruptible_all(&wq->wait);
1645 	rcu_read_unlock();
1646 }
1647 
subflow_is_done(const struct sock * sk)1648 static bool subflow_is_done(const struct sock *sk)
1649 {
1650 	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1651 }
1652 
subflow_state_change(struct sock * sk)1653 static void subflow_state_change(struct sock *sk)
1654 {
1655 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1656 	struct sock *parent = subflow->conn;
1657 	struct mptcp_sock *msk;
1658 
1659 	__subflow_state_change(sk);
1660 
1661 	msk = mptcp_sk(parent);
1662 	if (subflow_simultaneous_connect(sk)) {
1663 		mptcp_propagate_sndbuf(parent, sk);
1664 		mptcp_do_fallback(sk);
1665 		mptcp_rcv_space_init(msk, sk);
1666 		pr_fallback(msk);
1667 		subflow->conn_finished = 1;
1668 		mptcp_set_connected(parent);
1669 	}
1670 
1671 	/* as recvmsg() does not acquire the subflow socket for ssk selection
1672 	 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1673 	 * the data available machinery here.
1674 	 */
1675 	if (mptcp_subflow_data_available(sk))
1676 		mptcp_data_ready(parent, sk);
1677 	else if (unlikely(sk->sk_err))
1678 		subflow_error_report(sk);
1679 
1680 	subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1681 
1682 	/* when the fallback subflow closes the rx side, trigger a 'dummy'
1683 	 * ingress data fin, so that the msk state will follow along
1684 	 */
1685 	if (__mptcp_check_fallback(msk) && subflow_is_done(sk) && msk->first == sk &&
1686 	    mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
1687 		mptcp_schedule_work(parent);
1688 }
1689 
mptcp_subflow_queue_clean(struct sock * listener_sk,struct sock * listener_ssk)1690 void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
1691 {
1692 	struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1693 	struct request_sock *req, *head, *tail;
1694 	struct mptcp_subflow_context *subflow;
1695 	struct sock *sk, *ssk;
1696 
1697 	/* Due to lock dependencies no relevant lock can be acquired under rskq_lock.
1698 	 * Splice the req list, so that accept() can not reach the pending ssk after
1699 	 * the listener socket is released below.
1700 	 */
1701 	spin_lock_bh(&queue->rskq_lock);
1702 	head = queue->rskq_accept_head;
1703 	tail = queue->rskq_accept_tail;
1704 	queue->rskq_accept_head = NULL;
1705 	queue->rskq_accept_tail = NULL;
1706 	spin_unlock_bh(&queue->rskq_lock);
1707 
1708 	if (!head)
1709 		return;
1710 
1711 	/* can't acquire the msk socket lock under the subflow one,
1712 	 * or will cause ABBA deadlock
1713 	 */
1714 	release_sock(listener_ssk);
1715 
1716 	for (req = head; req; req = req->dl_next) {
1717 		ssk = req->sk;
1718 		if (!sk_is_mptcp(ssk))
1719 			continue;
1720 
1721 		subflow = mptcp_subflow_ctx(ssk);
1722 		if (!subflow || !subflow->conn)
1723 			continue;
1724 
1725 		sk = subflow->conn;
1726 		sock_hold(sk);
1727 
1728 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1729 		__mptcp_unaccepted_force_close(sk);
1730 		release_sock(sk);
1731 
1732 		/* lockdep will report a false positive ABBA deadlock
1733 		 * between cancel_work_sync and the listener socket.
1734 		 * The involved locks belong to different sockets WRT
1735 		 * the existing AB chain.
1736 		 * Using a per socket key is problematic as key
1737 		 * deregistration requires process context and must be
1738 		 * performed at socket disposal time, in atomic
1739 		 * context.
1740 		 * Just tell lockdep to consider the listener socket
1741 		 * released here.
1742 		 */
1743 		mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
1744 		mptcp_cancel_work(sk);
1745 		mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1746 
1747 		sock_put(sk);
1748 	}
1749 
1750 	/* we are still under the listener msk socket lock */
1751 	lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1752 
1753 	/* restore the listener queue, to let the TCP code clean it up */
1754 	spin_lock_bh(&queue->rskq_lock);
1755 	WARN_ON_ONCE(queue->rskq_accept_head);
1756 	queue->rskq_accept_head = head;
1757 	queue->rskq_accept_tail = tail;
1758 	spin_unlock_bh(&queue->rskq_lock);
1759 }
1760 
subflow_ulp_init(struct sock * sk)1761 static int subflow_ulp_init(struct sock *sk)
1762 {
1763 	struct inet_connection_sock *icsk = inet_csk(sk);
1764 	struct mptcp_subflow_context *ctx;
1765 	struct tcp_sock *tp = tcp_sk(sk);
1766 	int err = 0;
1767 
1768 	/* disallow attaching ULP to a socket unless it has been
1769 	 * created with sock_create_kern()
1770 	 */
1771 	if (!sk->sk_kern_sock) {
1772 		err = -EOPNOTSUPP;
1773 		goto out;
1774 	}
1775 
1776 	ctx = subflow_create_ctx(sk, GFP_KERNEL);
1777 	if (!ctx) {
1778 		err = -ENOMEM;
1779 		goto out;
1780 	}
1781 
1782 	pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1783 
1784 	tp->is_mptcp = 1;
1785 	ctx->icsk_af_ops = icsk->icsk_af_ops;
1786 	icsk->icsk_af_ops = subflow_default_af_ops(sk);
1787 	ctx->tcp_state_change = sk->sk_state_change;
1788 	ctx->tcp_error_report = sk->sk_error_report;
1789 
1790 	WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1791 	WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1792 
1793 	sk->sk_data_ready = subflow_data_ready;
1794 	sk->sk_write_space = subflow_write_space;
1795 	sk->sk_state_change = subflow_state_change;
1796 	sk->sk_error_report = subflow_error_report;
1797 out:
1798 	return err;
1799 }
1800 
subflow_ulp_release(struct sock * ssk)1801 static void subflow_ulp_release(struct sock *ssk)
1802 {
1803 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1804 	bool release = true;
1805 	struct sock *sk;
1806 
1807 	if (!ctx)
1808 		return;
1809 
1810 	sk = ctx->conn;
1811 	if (sk) {
1812 		/* if the msk has been orphaned, keep the ctx
1813 		 * alive, will be freed by __mptcp_close_ssk(),
1814 		 * when the subflow is still unaccepted
1815 		 */
1816 		release = ctx->disposable || list_empty(&ctx->node);
1817 
1818 		/* inet_child_forget() does not call sk_state_change(),
1819 		 * explicitly trigger the socket close machinery
1820 		 */
1821 		if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
1822 						  &mptcp_sk(sk)->flags))
1823 			mptcp_schedule_work(sk);
1824 		sock_put(sk);
1825 	}
1826 
1827 	mptcp_subflow_ops_undo_override(ssk);
1828 	if (release)
1829 		kfree_rcu(ctx, rcu);
1830 }
1831 
subflow_ulp_clone(const struct request_sock * req,struct sock * newsk,const gfp_t priority)1832 static void subflow_ulp_clone(const struct request_sock *req,
1833 			      struct sock *newsk,
1834 			      const gfp_t priority)
1835 {
1836 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1837 	struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1838 	struct mptcp_subflow_context *new_ctx;
1839 
1840 	if (!tcp_rsk(req)->is_mptcp ||
1841 	    (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1842 		subflow_ulp_fallback(newsk, old_ctx);
1843 		return;
1844 	}
1845 
1846 	new_ctx = subflow_create_ctx(newsk, priority);
1847 	if (!new_ctx) {
1848 		subflow_ulp_fallback(newsk, old_ctx);
1849 		return;
1850 	}
1851 
1852 	new_ctx->conn_finished = 1;
1853 	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1854 	new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1855 	new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1856 	new_ctx->rel_write_seq = 1;
1857 	new_ctx->tcp_sock = newsk;
1858 
1859 	if (subflow_req->mp_capable) {
1860 		/* see comments in subflow_syn_recv_sock(), MPTCP connection
1861 		 * is fully established only after we receive the remote key
1862 		 */
1863 		new_ctx->mp_capable = 1;
1864 		new_ctx->local_key = subflow_req->local_key;
1865 		new_ctx->token = subflow_req->token;
1866 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1867 		new_ctx->idsn = subflow_req->idsn;
1868 
1869 		/* this is the first subflow, id is always 0 */
1870 		new_ctx->local_id_valid = 1;
1871 	} else if (subflow_req->mp_join) {
1872 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1873 		new_ctx->mp_join = 1;
1874 		new_ctx->fully_established = 1;
1875 		new_ctx->backup = subflow_req->backup;
1876 		new_ctx->remote_id = subflow_req->remote_id;
1877 		new_ctx->token = subflow_req->token;
1878 		new_ctx->thmac = subflow_req->thmac;
1879 
1880 		/* the subflow req id is valid, fetched via subflow_check_req()
1881 		 * and subflow_token_join_request()
1882 		 */
1883 		subflow_set_local_id(new_ctx, subflow_req->local_id);
1884 	}
1885 }
1886 
tcp_release_cb_override(struct sock * ssk)1887 static void tcp_release_cb_override(struct sock *ssk)
1888 {
1889 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1890 	long status;
1891 
1892 	/* process and clear all the pending actions, but leave the subflow into
1893 	 * the napi queue. To respect locking, only the same CPU that originated
1894 	 * the action can touch the list. mptcp_napi_poll will take care of it.
1895 	 */
1896 	status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0);
1897 	if (status)
1898 		mptcp_subflow_process_delegated(ssk, status);
1899 
1900 	tcp_release_cb(ssk);
1901 }
1902 
tcp_abort_override(struct sock * ssk,int err)1903 static int tcp_abort_override(struct sock *ssk, int err)
1904 {
1905 	/* closing a listener subflow requires a great deal of care.
1906 	 * keep it simple and just prevent such operation
1907 	 */
1908 	if (inet_sk_state_load(ssk) == TCP_LISTEN)
1909 		return -EINVAL;
1910 
1911 	return tcp_abort(ssk, err);
1912 }
1913 
1914 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1915 	.name		= "mptcp",
1916 	.owner		= THIS_MODULE,
1917 	.init		= subflow_ulp_init,
1918 	.release	= subflow_ulp_release,
1919 	.clone		= subflow_ulp_clone,
1920 };
1921 
subflow_ops_init(struct request_sock_ops * subflow_ops)1922 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1923 {
1924 	subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1925 
1926 	subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1927 					      subflow_ops->obj_size, 0,
1928 					      SLAB_ACCOUNT |
1929 					      SLAB_TYPESAFE_BY_RCU,
1930 					      NULL);
1931 	if (!subflow_ops->slab)
1932 		return -ENOMEM;
1933 
1934 	return 0;
1935 }
1936 
mptcp_subflow_init(void)1937 void __init mptcp_subflow_init(void)
1938 {
1939 	mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
1940 	mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
1941 	mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
1942 
1943 	if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
1944 		panic("MPTCP: failed to init subflow v4 request sock ops\n");
1945 
1946 	subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1947 	subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1948 
1949 	subflow_specific = ipv4_specific;
1950 	subflow_specific.conn_request = subflow_v4_conn_request;
1951 	subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1952 	subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1953 	subflow_specific.rebuild_header = subflow_rebuild_header;
1954 
1955 	tcp_prot_override = tcp_prot;
1956 	tcp_prot_override.release_cb = tcp_release_cb_override;
1957 	tcp_prot_override.diag_destroy = tcp_abort_override;
1958 
1959 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1960 	/* In struct mptcp_subflow_request_sock, we assume the TCP request sock
1961 	 * structures for v4 and v6 have the same size. It should not changed in
1962 	 * the future but better to make sure to be warned if it is no longer
1963 	 * the case.
1964 	 */
1965 	BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
1966 
1967 	mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
1968 	mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
1969 	mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
1970 
1971 	if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
1972 		panic("MPTCP: failed to init subflow v6 request sock ops\n");
1973 
1974 	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1975 	subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1976 
1977 	subflow_v6_specific = ipv6_specific;
1978 	subflow_v6_specific.conn_request = subflow_v6_conn_request;
1979 	subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1980 	subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1981 	subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
1982 
1983 	subflow_v6m_specific = subflow_v6_specific;
1984 	subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1985 	subflow_v6m_specific.send_check = ipv4_specific.send_check;
1986 	subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1987 	subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1988 	subflow_v6m_specific.net_frag_header_len = 0;
1989 	subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
1990 
1991 	tcpv6_prot_override = tcpv6_prot;
1992 	tcpv6_prot_override.release_cb = tcp_release_cb_override;
1993 	tcpv6_prot_override.diag_destroy = tcp_abort_override;
1994 #endif
1995 
1996 	mptcp_diag_subflow_init(&subflow_ulp_ops);
1997 
1998 	if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1999 		panic("MPTCP: failed to register subflows to ULP\n");
2000 }
2001