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