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
__mptcp_error_report(struct sock * sk)1272 void __mptcp_error_report(struct sock *sk)
1273 {
1274 struct mptcp_subflow_context *subflow;
1275 struct mptcp_sock *msk = mptcp_sk(sk);
1276
1277 mptcp_for_each_subflow(msk, subflow) {
1278 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1279 int err = sock_error(ssk);
1280 int ssk_state;
1281
1282 if (!err)
1283 continue;
1284
1285 /* only propagate errors on fallen-back sockets or
1286 * on MPC connect
1287 */
1288 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1289 continue;
1290
1291 /* We need to propagate only transition to CLOSE state.
1292 * Orphaned socket will see such state change via
1293 * subflow_sched_work_if_closed() and that path will properly
1294 * destroy the msk as needed.
1295 */
1296 ssk_state = inet_sk_state_load(ssk);
1297 if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
1298 inet_sk_state_store(sk, ssk_state);
1299 sk->sk_err = -err;
1300
1301 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1302 smp_wmb();
1303 sk_error_report(sk);
1304 break;
1305 }
1306 }
1307
subflow_error_report(struct sock * ssk)1308 static void subflow_error_report(struct sock *ssk)
1309 {
1310 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1311
1312 /* bail early if this is a no-op, so that we avoid introducing a
1313 * problematic lockdep dependency between TCP accept queue lock
1314 * and msk socket spinlock
1315 */
1316 if (!sk->sk_socket)
1317 return;
1318
1319 mptcp_data_lock(sk);
1320 if (!sock_owned_by_user(sk))
1321 __mptcp_error_report(sk);
1322 else
1323 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags);
1324 mptcp_data_unlock(sk);
1325 }
1326
subflow_data_ready(struct sock * sk)1327 static void subflow_data_ready(struct sock *sk)
1328 {
1329 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1330 u16 state = 1 << inet_sk_state_load(sk);
1331 struct sock *parent = subflow->conn;
1332 struct mptcp_sock *msk;
1333
1334 msk = mptcp_sk(parent);
1335 if (state & TCPF_LISTEN) {
1336 /* MPJ subflow are removed from accept queue before reaching here,
1337 * avoid stray wakeups
1338 */
1339 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1340 return;
1341
1342 set_bit(MPTCP_DATA_READY, &msk->flags);
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 struct socket *sf;
1434 struct sock *ssk;
1435 u32 remote_token;
1436 int addrlen;
1437 int ifindex;
1438 u8 flags;
1439 int err;
1440
1441 if (!mptcp_is_fully_established(sk))
1442 return -ENOTCONN;
1443
1444 err = mptcp_subflow_create_socket(sk, &sf);
1445 if (err)
1446 return err;
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 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1456 if (err < 0)
1457 goto failed;
1458
1459 local_id = err;
1460 }
1461
1462 mptcp_pm_get_flags_and_ifindex_by_id(sock_net(sk), local_id,
1463 &flags, &ifindex);
1464 subflow->remote_key = msk->remote_key;
1465 subflow->local_key = msk->local_key;
1466 subflow->token = msk->token;
1467 mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1468
1469 addrlen = sizeof(struct sockaddr_in);
1470 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1471 if (addr.ss_family == AF_INET6)
1472 addrlen = sizeof(struct sockaddr_in6);
1473 #endif
1474 ssk->sk_bound_dev_if = ifindex;
1475 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1476 if (err)
1477 goto failed;
1478
1479 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1480 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1481 remote_token, local_id, remote_id);
1482 subflow->remote_token = remote_token;
1483 subflow->local_id = local_id;
1484 subflow->remote_id = remote_id;
1485 subflow->request_join = 1;
1486 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1487 mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1488
1489 mptcp_add_pending_subflow(msk, subflow);
1490 mptcp_sockopt_sync(msk, ssk);
1491 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1492 if (err && err != -EINPROGRESS)
1493 goto failed_unlink;
1494
1495 /* discard the subflow socket */
1496 mptcp_sock_graft(ssk, sk->sk_socket);
1497 iput(SOCK_INODE(sf));
1498 return err;
1499
1500 failed_unlink:
1501 spin_lock_bh(&msk->join_list_lock);
1502 list_del(&subflow->node);
1503 spin_unlock_bh(&msk->join_list_lock);
1504 sock_put(mptcp_subflow_tcp_sock(subflow));
1505
1506 failed:
1507 subflow->disposable = 1;
1508 sock_release(sf);
1509 return err;
1510 }
1511
mptcp_attach_cgroup(struct sock * parent,struct sock * child)1512 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1513 {
1514 #ifdef CONFIG_SOCK_CGROUP_DATA
1515 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1516 *child_skcd = &child->sk_cgrp_data;
1517
1518 /* only the additional subflows created by kworkers have to be modified */
1519 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1520 cgroup_id(sock_cgroup_ptr(child_skcd))) {
1521 #ifdef CONFIG_MEMCG
1522 struct mem_cgroup *memcg = parent->sk_memcg;
1523
1524 mem_cgroup_sk_free(child);
1525 if (memcg && css_tryget(&memcg->css))
1526 child->sk_memcg = memcg;
1527 #endif /* CONFIG_MEMCG */
1528
1529 cgroup_sk_free(child_skcd);
1530 *child_skcd = *parent_skcd;
1531 cgroup_sk_clone(child_skcd);
1532 }
1533 #endif /* CONFIG_SOCK_CGROUP_DATA */
1534 }
1535
mptcp_subflow_ops_override(struct sock * ssk)1536 static void mptcp_subflow_ops_override(struct sock *ssk)
1537 {
1538 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1539 if (ssk->sk_prot == &tcpv6_prot)
1540 ssk->sk_prot = &tcpv6_prot_override;
1541 else
1542 #endif
1543 ssk->sk_prot = &tcp_prot_override;
1544 }
1545
mptcp_subflow_ops_undo_override(struct sock * ssk)1546 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1547 {
1548 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1549 if (ssk->sk_prot == &tcpv6_prot_override)
1550 ssk->sk_prot = &tcpv6_prot;
1551 else
1552 #endif
1553 ssk->sk_prot = &tcp_prot;
1554 }
mptcp_subflow_create_socket(struct sock * sk,struct socket ** new_sock)1555 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1556 {
1557 struct mptcp_subflow_context *subflow;
1558 struct net *net = sock_net(sk);
1559 struct socket *sf;
1560 int err;
1561
1562 /* un-accepted server sockets can reach here - on bad configuration
1563 * bail early to avoid greater trouble later
1564 */
1565 if (unlikely(!sk->sk_socket))
1566 return -EINVAL;
1567
1568 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1569 &sf);
1570 if (err)
1571 return err;
1572
1573 lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
1574
1575 /* the newly created socket has to be in the same cgroup as its parent */
1576 mptcp_attach_cgroup(sk, sf->sk);
1577
1578 /* kernel sockets do not by default acquire net ref, but TCP timer
1579 * needs it.
1580 */
1581 sf->sk->sk_net_refcnt = 1;
1582 get_net(net);
1583 #ifdef CONFIG_PROC_FS
1584 this_cpu_add(*net->core.sock_inuse, 1);
1585 #endif
1586 err = tcp_set_ulp(sf->sk, "mptcp");
1587 release_sock(sf->sk);
1588
1589 if (err) {
1590 sock_release(sf);
1591 return err;
1592 }
1593
1594 /* the newly created socket really belongs to the owning MPTCP master
1595 * socket, even if for additional subflows the allocation is performed
1596 * by a kernel workqueue. Adjust inode references, so that the
1597 * procfs/diag interaces really show this one belonging to the correct
1598 * user.
1599 */
1600 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1601 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1602 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1603
1604 subflow = mptcp_subflow_ctx(sf->sk);
1605 pr_debug("subflow=%p", subflow);
1606
1607 *new_sock = sf;
1608 sock_hold(sk);
1609 subflow->conn = sk;
1610 mptcp_subflow_ops_override(sf->sk);
1611
1612 return 0;
1613 }
1614
subflow_create_ctx(struct sock * sk,gfp_t priority)1615 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1616 gfp_t priority)
1617 {
1618 struct inet_connection_sock *icsk = inet_csk(sk);
1619 struct mptcp_subflow_context *ctx;
1620
1621 ctx = kzalloc(sizeof(*ctx), priority);
1622 if (!ctx)
1623 return NULL;
1624
1625 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1626 INIT_LIST_HEAD(&ctx->node);
1627 INIT_LIST_HEAD(&ctx->delegated_node);
1628
1629 pr_debug("subflow=%p", ctx);
1630
1631 ctx->tcp_sock = sk;
1632
1633 return ctx;
1634 }
1635
__subflow_state_change(struct sock * sk)1636 static void __subflow_state_change(struct sock *sk)
1637 {
1638 struct socket_wq *wq;
1639
1640 rcu_read_lock();
1641 wq = rcu_dereference(sk->sk_wq);
1642 if (skwq_has_sleeper(wq))
1643 wake_up_interruptible_all(&wq->wait);
1644 rcu_read_unlock();
1645 }
1646
subflow_is_done(const struct sock * sk)1647 static bool subflow_is_done(const struct sock *sk)
1648 {
1649 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1650 }
1651
subflow_state_change(struct sock * sk)1652 static void subflow_state_change(struct sock *sk)
1653 {
1654 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1655 struct sock *parent = subflow->conn;
1656 struct mptcp_sock *msk;
1657
1658 __subflow_state_change(sk);
1659
1660 msk = mptcp_sk(parent);
1661 if (subflow_simultaneous_connect(sk)) {
1662 mptcp_propagate_sndbuf(parent, sk);
1663 mptcp_do_fallback(sk);
1664 mptcp_rcv_space_init(msk, sk);
1665 pr_fallback(msk);
1666 subflow->conn_finished = 1;
1667 mptcp_set_connected(parent);
1668 }
1669
1670 /* as recvmsg() does not acquire the subflow socket for ssk selection
1671 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1672 * the data available machinery here.
1673 */
1674 if (mptcp_subflow_data_available(sk))
1675 mptcp_data_ready(parent, sk);
1676 else if (unlikely(sk->sk_err))
1677 subflow_error_report(sk);
1678
1679 subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1680
1681 /* when the fallback subflow closes the rx side, trigger a 'dummy'
1682 * ingress data fin, so that the msk state will follow along
1683 */
1684 if (__mptcp_check_fallback(msk) && subflow_is_done(sk) && msk->first == sk &&
1685 mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
1686 mptcp_schedule_work(parent);
1687 }
1688
subflow_ulp_init(struct sock * sk)1689 static int subflow_ulp_init(struct sock *sk)
1690 {
1691 struct inet_connection_sock *icsk = inet_csk(sk);
1692 struct mptcp_subflow_context *ctx;
1693 struct tcp_sock *tp = tcp_sk(sk);
1694 int err = 0;
1695
1696 /* disallow attaching ULP to a socket unless it has been
1697 * created with sock_create_kern()
1698 */
1699 if (!sk->sk_kern_sock) {
1700 err = -EOPNOTSUPP;
1701 goto out;
1702 }
1703
1704 ctx = subflow_create_ctx(sk, GFP_KERNEL);
1705 if (!ctx) {
1706 err = -ENOMEM;
1707 goto out;
1708 }
1709
1710 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1711
1712 tp->is_mptcp = 1;
1713 ctx->icsk_af_ops = icsk->icsk_af_ops;
1714 icsk->icsk_af_ops = subflow_default_af_ops(sk);
1715 ctx->tcp_data_ready = sk->sk_data_ready;
1716 ctx->tcp_state_change = sk->sk_state_change;
1717 ctx->tcp_write_space = sk->sk_write_space;
1718 ctx->tcp_error_report = sk->sk_error_report;
1719 sk->sk_data_ready = subflow_data_ready;
1720 sk->sk_write_space = subflow_write_space;
1721 sk->sk_state_change = subflow_state_change;
1722 sk->sk_error_report = subflow_error_report;
1723 out:
1724 return err;
1725 }
1726
subflow_ulp_release(struct sock * ssk)1727 static void subflow_ulp_release(struct sock *ssk)
1728 {
1729 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1730 bool release = true;
1731 struct sock *sk;
1732
1733 if (!ctx)
1734 return;
1735
1736 sk = ctx->conn;
1737 if (sk) {
1738 /* if the msk has been orphaned, keep the ctx
1739 * alive, will be freed by __mptcp_close_ssk(),
1740 * when the subflow is still unaccepted
1741 */
1742 release = ctx->disposable || list_empty(&ctx->node);
1743 sock_put(sk);
1744 }
1745
1746 mptcp_subflow_ops_undo_override(ssk);
1747 if (release)
1748 kfree_rcu(ctx, rcu);
1749 }
1750
subflow_ulp_clone(const struct request_sock * req,struct sock * newsk,const gfp_t priority)1751 static void subflow_ulp_clone(const struct request_sock *req,
1752 struct sock *newsk,
1753 const gfp_t priority)
1754 {
1755 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1756 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1757 struct mptcp_subflow_context *new_ctx;
1758
1759 if (!tcp_rsk(req)->is_mptcp ||
1760 (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1761 subflow_ulp_fallback(newsk, old_ctx);
1762 return;
1763 }
1764
1765 new_ctx = subflow_create_ctx(newsk, priority);
1766 if (!new_ctx) {
1767 subflow_ulp_fallback(newsk, old_ctx);
1768 return;
1769 }
1770
1771 new_ctx->conn_finished = 1;
1772 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1773 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1774 new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1775 new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1776 new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1777 new_ctx->rel_write_seq = 1;
1778 new_ctx->tcp_sock = newsk;
1779
1780 if (subflow_req->mp_capable) {
1781 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1782 * is fully established only after we receive the remote key
1783 */
1784 new_ctx->mp_capable = 1;
1785 new_ctx->local_key = subflow_req->local_key;
1786 new_ctx->token = subflow_req->token;
1787 new_ctx->ssn_offset = subflow_req->ssn_offset;
1788 new_ctx->idsn = subflow_req->idsn;
1789 } else if (subflow_req->mp_join) {
1790 new_ctx->ssn_offset = subflow_req->ssn_offset;
1791 new_ctx->mp_join = 1;
1792 new_ctx->fully_established = 1;
1793 new_ctx->backup = subflow_req->backup;
1794 new_ctx->local_id = subflow_req->local_id;
1795 new_ctx->remote_id = subflow_req->remote_id;
1796 new_ctx->token = subflow_req->token;
1797 new_ctx->thmac = subflow_req->thmac;
1798 }
1799 }
1800
tcp_release_cb_override(struct sock * ssk)1801 static void tcp_release_cb_override(struct sock *ssk)
1802 {
1803 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1804
1805 if (mptcp_subflow_has_delegated_action(subflow))
1806 mptcp_subflow_process_delegated(ssk);
1807
1808 tcp_release_cb(ssk);
1809 }
1810
1811 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1812 .name = "mptcp",
1813 .owner = THIS_MODULE,
1814 .init = subflow_ulp_init,
1815 .release = subflow_ulp_release,
1816 .clone = subflow_ulp_clone,
1817 };
1818
subflow_ops_init(struct request_sock_ops * subflow_ops)1819 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1820 {
1821 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1822
1823 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1824 subflow_ops->obj_size, 0,
1825 SLAB_ACCOUNT |
1826 SLAB_TYPESAFE_BY_RCU,
1827 NULL);
1828 if (!subflow_ops->slab)
1829 return -ENOMEM;
1830
1831 return 0;
1832 }
1833
mptcp_subflow_init(void)1834 void __init mptcp_subflow_init(void)
1835 {
1836 mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
1837 mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
1838 mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
1839
1840 if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
1841 panic("MPTCP: failed to init subflow v4 request sock ops\n");
1842
1843 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1844 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1845
1846 subflow_specific = ipv4_specific;
1847 subflow_specific.conn_request = subflow_v4_conn_request;
1848 subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1849 subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1850
1851 tcp_prot_override = tcp_prot;
1852 tcp_prot_override.release_cb = tcp_release_cb_override;
1853
1854 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1855 /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
1856 * structures for v4 and v6 have the same size. It should not changed in
1857 * the future but better to make sure to be warned if it is no longer
1858 * the case.
1859 */
1860 BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
1861
1862 mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
1863 mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
1864 mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
1865
1866 if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
1867 panic("MPTCP: failed to init subflow v6 request sock ops\n");
1868
1869 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1870 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1871
1872 subflow_v6_specific = ipv6_specific;
1873 subflow_v6_specific.conn_request = subflow_v6_conn_request;
1874 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1875 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1876
1877 subflow_v6m_specific = subflow_v6_specific;
1878 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1879 subflow_v6m_specific.send_check = ipv4_specific.send_check;
1880 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1881 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1882 subflow_v6m_specific.net_frag_header_len = 0;
1883
1884 tcpv6_prot_override = tcpv6_prot;
1885 tcpv6_prot_override.release_cb = tcp_release_cb_override;
1886 #endif
1887
1888 mptcp_diag_subflow_init(&subflow_ulp_ops);
1889
1890 if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1891 panic("MPTCP: failed to register subflows to ULP\n");
1892 }
1893