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