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 <linux/sched/signal.h>
13 #include <linux/atomic.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 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
22 #endif
23 #include <net/mptcp.h>
24 #include <net/xfrm.h>
25 #include <asm/ioctls.h>
26 #include "protocol.h"
27 #include "mib.h"
28
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
31
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33 struct mptcp6_sock {
34 struct mptcp_sock msk;
35 struct ipv6_pinfo np;
36 };
37 #endif
38
39 enum {
40 MPTCP_CMSG_TS = BIT(0),
41 MPTCP_CMSG_INQ = BIT(1),
42 };
43
44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
45
46 static void __mptcp_destroy_sock(struct sock *sk);
47 static void mptcp_check_send_data_fin(struct sock *sk);
48
49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 static struct net_device mptcp_napi_dev;
51
52 /* Returns end sequence number of the receiver's advertised window */
mptcp_wnd_end(const struct mptcp_sock * msk)53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
54 {
55 return READ_ONCE(msk->wnd_end);
56 }
57
mptcp_fallback_tcp_ops(const struct sock * sk)58 static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
59 {
60 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
61 if (sk->sk_prot == &tcpv6_prot)
62 return &inet6_stream_ops;
63 #endif
64 WARN_ON_ONCE(sk->sk_prot != &tcp_prot);
65 return &inet_stream_ops;
66 }
67
__mptcp_socket_create(struct mptcp_sock * msk)68 static int __mptcp_socket_create(struct mptcp_sock *msk)
69 {
70 struct mptcp_subflow_context *subflow;
71 struct sock *sk = (struct sock *)msk;
72 struct socket *ssock;
73 int err;
74
75 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
76 if (err)
77 return err;
78
79 msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
80 WRITE_ONCE(msk->first, ssock->sk);
81 subflow = mptcp_subflow_ctx(ssock->sk);
82 list_add(&subflow->node, &msk->conn_list);
83 sock_hold(ssock->sk);
84 subflow->request_mptcp = 1;
85 subflow->subflow_id = msk->subflow_id++;
86
87 /* This is the first subflow, always with id 0 */
88 WRITE_ONCE(subflow->local_id, 0);
89 mptcp_sock_graft(msk->first, sk->sk_socket);
90 iput(SOCK_INODE(ssock));
91
92 return 0;
93 }
94
95 /* If the MPC handshake is not started, returns the first subflow,
96 * eventually allocating it.
97 */
__mptcp_nmpc_sk(struct mptcp_sock * msk)98 struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
99 {
100 struct sock *sk = (struct sock *)msk;
101 int ret;
102
103 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
104 return ERR_PTR(-EINVAL);
105
106 if (!msk->first) {
107 ret = __mptcp_socket_create(msk);
108 if (ret)
109 return ERR_PTR(ret);
110
111 mptcp_sockopt_sync(msk, msk->first);
112 }
113
114 return msk->first;
115 }
116
mptcp_drop(struct sock * sk,struct sk_buff * skb)117 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
118 {
119 sk_drops_add(sk, skb);
120 __kfree_skb(skb);
121 }
122
mptcp_rmem_fwd_alloc_add(struct sock * sk,int size)123 static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size)
124 {
125 WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc,
126 mptcp_sk(sk)->rmem_fwd_alloc + size);
127 }
128
mptcp_rmem_charge(struct sock * sk,int size)129 static void mptcp_rmem_charge(struct sock *sk, int size)
130 {
131 mptcp_rmem_fwd_alloc_add(sk, -size);
132 }
133
mptcp_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from)134 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
135 struct sk_buff *from)
136 {
137 bool fragstolen;
138 int delta;
139
140 if (MPTCP_SKB_CB(from)->offset ||
141 !skb_try_coalesce(to, from, &fragstolen, &delta))
142 return false;
143
144 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
145 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
146 to->len, MPTCP_SKB_CB(from)->end_seq);
147 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
148
149 /* note the fwd memory can reach a negative value after accounting
150 * for the delta, but the later skb free will restore a non
151 * negative one
152 */
153 atomic_add(delta, &sk->sk_rmem_alloc);
154 mptcp_rmem_charge(sk, delta);
155 kfree_skb_partial(from, fragstolen);
156
157 return true;
158 }
159
mptcp_ooo_try_coalesce(struct mptcp_sock * msk,struct sk_buff * to,struct sk_buff * from)160 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
161 struct sk_buff *from)
162 {
163 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
164 return false;
165
166 return mptcp_try_coalesce((struct sock *)msk, to, from);
167 }
168
__mptcp_rmem_reclaim(struct sock * sk,int amount)169 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
170 {
171 amount >>= PAGE_SHIFT;
172 mptcp_rmem_charge(sk, amount << PAGE_SHIFT);
173 __sk_mem_reduce_allocated(sk, amount);
174 }
175
mptcp_rmem_uncharge(struct sock * sk,int size)176 static void mptcp_rmem_uncharge(struct sock *sk, int size)
177 {
178 struct mptcp_sock *msk = mptcp_sk(sk);
179 int reclaimable;
180
181 mptcp_rmem_fwd_alloc_add(sk, size);
182 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
183
184 /* see sk_mem_uncharge() for the rationale behind the following schema */
185 if (unlikely(reclaimable >= PAGE_SIZE))
186 __mptcp_rmem_reclaim(sk, reclaimable);
187 }
188
mptcp_rfree(struct sk_buff * skb)189 static void mptcp_rfree(struct sk_buff *skb)
190 {
191 unsigned int len = skb->truesize;
192 struct sock *sk = skb->sk;
193
194 atomic_sub(len, &sk->sk_rmem_alloc);
195 mptcp_rmem_uncharge(sk, len);
196 }
197
mptcp_set_owner_r(struct sk_buff * skb,struct sock * sk)198 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
199 {
200 skb_orphan(skb);
201 skb->sk = sk;
202 skb->destructor = mptcp_rfree;
203 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
204 mptcp_rmem_charge(sk, skb->truesize);
205 }
206
207 /* "inspired" by tcp_data_queue_ofo(), main differences:
208 * - use mptcp seqs
209 * - don't cope with sacks
210 */
mptcp_data_queue_ofo(struct mptcp_sock * msk,struct sk_buff * skb)211 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
212 {
213 struct sock *sk = (struct sock *)msk;
214 struct rb_node **p, *parent;
215 u64 seq, end_seq, max_seq;
216 struct sk_buff *skb1;
217
218 seq = MPTCP_SKB_CB(skb)->map_seq;
219 end_seq = MPTCP_SKB_CB(skb)->end_seq;
220 max_seq = atomic64_read(&msk->rcv_wnd_sent);
221
222 pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
223 RB_EMPTY_ROOT(&msk->out_of_order_queue));
224 if (after64(end_seq, max_seq)) {
225 /* out of window */
226 mptcp_drop(sk, skb);
227 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
228 (unsigned long long)end_seq - (unsigned long)max_seq,
229 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
230 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
231 return;
232 }
233
234 p = &msk->out_of_order_queue.rb_node;
235 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
236 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
237 rb_link_node(&skb->rbnode, NULL, p);
238 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
239 msk->ooo_last_skb = skb;
240 goto end;
241 }
242
243 /* with 2 subflows, adding at end of ooo queue is quite likely
244 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
245 */
246 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
247 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
248 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
249 return;
250 }
251
252 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
253 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
254 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
255 parent = &msk->ooo_last_skb->rbnode;
256 p = &parent->rb_right;
257 goto insert;
258 }
259
260 /* Find place to insert this segment. Handle overlaps on the way. */
261 parent = NULL;
262 while (*p) {
263 parent = *p;
264 skb1 = rb_to_skb(parent);
265 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
266 p = &parent->rb_left;
267 continue;
268 }
269 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
270 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
271 /* All the bits are present. Drop. */
272 mptcp_drop(sk, skb);
273 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
274 return;
275 }
276 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
277 /* partial overlap:
278 * | skb |
279 * | skb1 |
280 * continue traversing
281 */
282 } else {
283 /* skb's seq == skb1's seq and skb covers skb1.
284 * Replace skb1 with skb.
285 */
286 rb_replace_node(&skb1->rbnode, &skb->rbnode,
287 &msk->out_of_order_queue);
288 mptcp_drop(sk, skb1);
289 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
290 goto merge_right;
291 }
292 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
293 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
294 return;
295 }
296 p = &parent->rb_right;
297 }
298
299 insert:
300 /* Insert segment into RB tree. */
301 rb_link_node(&skb->rbnode, parent, p);
302 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
303
304 merge_right:
305 /* Remove other segments covered by skb. */
306 while ((skb1 = skb_rb_next(skb)) != NULL) {
307 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
308 break;
309 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
310 mptcp_drop(sk, skb1);
311 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
312 }
313 /* If there is no skb after us, we are the last_skb ! */
314 if (!skb1)
315 msk->ooo_last_skb = skb;
316
317 end:
318 skb_condense(skb);
319 mptcp_set_owner_r(skb, sk);
320 }
321
mptcp_rmem_schedule(struct sock * sk,struct sock * ssk,int size)322 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
323 {
324 struct mptcp_sock *msk = mptcp_sk(sk);
325 int amt, amount;
326
327 if (size <= msk->rmem_fwd_alloc)
328 return true;
329
330 size -= msk->rmem_fwd_alloc;
331 amt = sk_mem_pages(size);
332 amount = amt << PAGE_SHIFT;
333 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
334 return false;
335
336 mptcp_rmem_fwd_alloc_add(sk, amount);
337 return true;
338 }
339
__mptcp_move_skb(struct mptcp_sock * msk,struct sock * ssk,struct sk_buff * skb,unsigned int offset,size_t copy_len)340 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
341 struct sk_buff *skb, unsigned int offset,
342 size_t copy_len)
343 {
344 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
345 struct sock *sk = (struct sock *)msk;
346 struct sk_buff *tail;
347 bool has_rxtstamp;
348
349 __skb_unlink(skb, &ssk->sk_receive_queue);
350
351 skb_ext_reset(skb);
352 skb_orphan(skb);
353
354 /* try to fetch required memory from subflow */
355 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) {
356 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
357 goto drop;
358 }
359
360 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
361
362 /* the skb map_seq accounts for the skb offset:
363 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
364 * value
365 */
366 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
367 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
368 MPTCP_SKB_CB(skb)->offset = offset;
369 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
370
371 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
372 /* in sequence */
373 msk->bytes_received += copy_len;
374 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
375 tail = skb_peek_tail(&sk->sk_receive_queue);
376 if (tail && mptcp_try_coalesce(sk, tail, skb))
377 return true;
378
379 mptcp_set_owner_r(skb, sk);
380 __skb_queue_tail(&sk->sk_receive_queue, skb);
381 return true;
382 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
383 mptcp_data_queue_ofo(msk, skb);
384 return false;
385 }
386
387 /* old data, keep it simple and drop the whole pkt, sender
388 * will retransmit as needed, if needed.
389 */
390 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
391 drop:
392 mptcp_drop(sk, skb);
393 return false;
394 }
395
mptcp_stop_rtx_timer(struct sock * sk)396 static void mptcp_stop_rtx_timer(struct sock *sk)
397 {
398 struct inet_connection_sock *icsk = inet_csk(sk);
399
400 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
401 mptcp_sk(sk)->timer_ival = 0;
402 }
403
mptcp_close_wake_up(struct sock * sk)404 static void mptcp_close_wake_up(struct sock *sk)
405 {
406 if (sock_flag(sk, SOCK_DEAD))
407 return;
408
409 sk->sk_state_change(sk);
410 if (sk->sk_shutdown == SHUTDOWN_MASK ||
411 sk->sk_state == TCP_CLOSE)
412 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
413 else
414 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
415 }
416
mptcp_pending_data_fin_ack(struct sock * sk)417 static bool mptcp_pending_data_fin_ack(struct sock *sk)
418 {
419 struct mptcp_sock *msk = mptcp_sk(sk);
420
421 return ((1 << sk->sk_state) &
422 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
423 msk->write_seq == READ_ONCE(msk->snd_una);
424 }
425
mptcp_check_data_fin_ack(struct sock * sk)426 static void mptcp_check_data_fin_ack(struct sock *sk)
427 {
428 struct mptcp_sock *msk = mptcp_sk(sk);
429
430 /* Look for an acknowledged DATA_FIN */
431 if (mptcp_pending_data_fin_ack(sk)) {
432 WRITE_ONCE(msk->snd_data_fin_enable, 0);
433
434 switch (sk->sk_state) {
435 case TCP_FIN_WAIT1:
436 mptcp_set_state(sk, TCP_FIN_WAIT2);
437 break;
438 case TCP_CLOSING:
439 case TCP_LAST_ACK:
440 mptcp_set_state(sk, TCP_CLOSE);
441 break;
442 }
443
444 mptcp_close_wake_up(sk);
445 }
446 }
447
mptcp_pending_data_fin(struct sock * sk,u64 * seq)448 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
449 {
450 struct mptcp_sock *msk = mptcp_sk(sk);
451
452 if (READ_ONCE(msk->rcv_data_fin) &&
453 ((1 << sk->sk_state) &
454 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
455 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
456
457 if (msk->ack_seq == rcv_data_fin_seq) {
458 if (seq)
459 *seq = rcv_data_fin_seq;
460
461 return true;
462 }
463 }
464
465 return false;
466 }
467
mptcp_set_datafin_timeout(struct sock * sk)468 static void mptcp_set_datafin_timeout(struct sock *sk)
469 {
470 struct inet_connection_sock *icsk = inet_csk(sk);
471 u32 retransmits;
472
473 retransmits = min_t(u32, icsk->icsk_retransmits,
474 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
475
476 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
477 }
478
__mptcp_set_timeout(struct sock * sk,long tout)479 static void __mptcp_set_timeout(struct sock *sk, long tout)
480 {
481 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
482 }
483
mptcp_timeout_from_subflow(const struct mptcp_subflow_context * subflow)484 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
485 {
486 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
487
488 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
489 inet_csk(ssk)->icsk_timeout - jiffies : 0;
490 }
491
mptcp_set_timeout(struct sock * sk)492 static void mptcp_set_timeout(struct sock *sk)
493 {
494 struct mptcp_subflow_context *subflow;
495 long tout = 0;
496
497 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
498 tout = max(tout, mptcp_timeout_from_subflow(subflow));
499 __mptcp_set_timeout(sk, tout);
500 }
501
tcp_can_send_ack(const struct sock * ssk)502 static inline bool tcp_can_send_ack(const struct sock *ssk)
503 {
504 return !((1 << inet_sk_state_load(ssk)) &
505 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
506 }
507
__mptcp_subflow_send_ack(struct sock * ssk)508 void __mptcp_subflow_send_ack(struct sock *ssk)
509 {
510 if (tcp_can_send_ack(ssk))
511 tcp_send_ack(ssk);
512 }
513
mptcp_subflow_send_ack(struct sock * ssk)514 static void mptcp_subflow_send_ack(struct sock *ssk)
515 {
516 bool slow;
517
518 slow = lock_sock_fast(ssk);
519 __mptcp_subflow_send_ack(ssk);
520 unlock_sock_fast(ssk, slow);
521 }
522
mptcp_send_ack(struct mptcp_sock * msk)523 static void mptcp_send_ack(struct mptcp_sock *msk)
524 {
525 struct mptcp_subflow_context *subflow;
526
527 mptcp_for_each_subflow(msk, subflow)
528 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
529 }
530
mptcp_subflow_cleanup_rbuf(struct sock * ssk)531 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
532 {
533 bool slow;
534
535 slow = lock_sock_fast(ssk);
536 if (tcp_can_send_ack(ssk))
537 tcp_cleanup_rbuf(ssk, 1);
538 unlock_sock_fast(ssk, slow);
539 }
540
mptcp_subflow_could_cleanup(const struct sock * ssk,bool rx_empty)541 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
542 {
543 const struct inet_connection_sock *icsk = inet_csk(ssk);
544 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
545 const struct tcp_sock *tp = tcp_sk(ssk);
546
547 return (ack_pending & ICSK_ACK_SCHED) &&
548 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
549 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
550 (rx_empty && ack_pending &
551 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
552 }
553
mptcp_cleanup_rbuf(struct mptcp_sock * msk)554 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
555 {
556 int old_space = READ_ONCE(msk->old_wspace);
557 struct mptcp_subflow_context *subflow;
558 struct sock *sk = (struct sock *)msk;
559 int space = __mptcp_space(sk);
560 bool cleanup, rx_empty;
561
562 cleanup = (space > 0) && (space >= (old_space << 1));
563 rx_empty = !__mptcp_rmem(sk);
564
565 mptcp_for_each_subflow(msk, subflow) {
566 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
567
568 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
569 mptcp_subflow_cleanup_rbuf(ssk);
570 }
571 }
572
mptcp_check_data_fin(struct sock * sk)573 static bool mptcp_check_data_fin(struct sock *sk)
574 {
575 struct mptcp_sock *msk = mptcp_sk(sk);
576 u64 rcv_data_fin_seq;
577 bool ret = false;
578
579 /* Need to ack a DATA_FIN received from a peer while this side
580 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
581 * msk->rcv_data_fin was set when parsing the incoming options
582 * at the subflow level and the msk lock was not held, so this
583 * is the first opportunity to act on the DATA_FIN and change
584 * the msk state.
585 *
586 * If we are caught up to the sequence number of the incoming
587 * DATA_FIN, send the DATA_ACK now and do state transition. If
588 * not caught up, do nothing and let the recv code send DATA_ACK
589 * when catching up.
590 */
591
592 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
593 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
594 WRITE_ONCE(msk->rcv_data_fin, 0);
595
596 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
597 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
598
599 switch (sk->sk_state) {
600 case TCP_ESTABLISHED:
601 mptcp_set_state(sk, TCP_CLOSE_WAIT);
602 break;
603 case TCP_FIN_WAIT1:
604 mptcp_set_state(sk, TCP_CLOSING);
605 break;
606 case TCP_FIN_WAIT2:
607 mptcp_set_state(sk, TCP_CLOSE);
608 break;
609 default:
610 /* Other states not expected */
611 WARN_ON_ONCE(1);
612 break;
613 }
614
615 ret = true;
616 if (!__mptcp_check_fallback(msk))
617 mptcp_send_ack(msk);
618 mptcp_close_wake_up(sk);
619 }
620 return ret;
621 }
622
mptcp_dss_corruption(struct mptcp_sock * msk,struct sock * ssk)623 static void mptcp_dss_corruption(struct mptcp_sock *msk, struct sock *ssk)
624 {
625 if (READ_ONCE(msk->allow_infinite_fallback)) {
626 MPTCP_INC_STATS(sock_net(ssk),
627 MPTCP_MIB_DSSCORRUPTIONFALLBACK);
628 mptcp_do_fallback(ssk);
629 } else {
630 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSCORRUPTIONRESET);
631 mptcp_subflow_reset(ssk);
632 }
633 }
634
__mptcp_move_skbs_from_subflow(struct mptcp_sock * msk,struct sock * ssk,unsigned int * bytes)635 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
636 struct sock *ssk,
637 unsigned int *bytes)
638 {
639 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
640 struct sock *sk = (struct sock *)msk;
641 unsigned int moved = 0;
642 bool more_data_avail;
643 struct tcp_sock *tp;
644 bool done = false;
645 int sk_rbuf;
646
647 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
648
649 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
650 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
651
652 if (unlikely(ssk_rbuf > sk_rbuf)) {
653 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
654 sk_rbuf = ssk_rbuf;
655 }
656 }
657
658 pr_debug("msk=%p ssk=%p\n", msk, ssk);
659 tp = tcp_sk(ssk);
660 do {
661 u32 map_remaining, offset;
662 u32 seq = tp->copied_seq;
663 struct sk_buff *skb;
664 bool fin;
665
666 /* try to move as much data as available */
667 map_remaining = subflow->map_data_len -
668 mptcp_subflow_get_map_offset(subflow);
669
670 skb = skb_peek(&ssk->sk_receive_queue);
671 if (!skb) {
672 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
673 * a different CPU can have already processed the pending
674 * data, stop here or we can enter an infinite loop
675 */
676 if (!moved)
677 done = true;
678 break;
679 }
680
681 if (__mptcp_check_fallback(msk)) {
682 /* Under fallback skbs have no MPTCP extension and TCP could
683 * collapse them between the dummy map creation and the
684 * current dequeue. Be sure to adjust the map size.
685 */
686 map_remaining = skb->len;
687 subflow->map_data_len = skb->len;
688 }
689
690 offset = seq - TCP_SKB_CB(skb)->seq;
691 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
692 if (fin) {
693 done = true;
694 seq++;
695 }
696
697 if (offset < skb->len) {
698 size_t len = skb->len - offset;
699
700 if (tp->urg_data)
701 done = true;
702
703 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
704 moved += len;
705 seq += len;
706
707 if (unlikely(map_remaining < len)) {
708 DEBUG_NET_WARN_ON_ONCE(1);
709 mptcp_dss_corruption(msk, ssk);
710 }
711 } else {
712 if (unlikely(!fin)) {
713 DEBUG_NET_WARN_ON_ONCE(1);
714 mptcp_dss_corruption(msk, ssk);
715 }
716
717 sk_eat_skb(ssk, skb);
718 done = true;
719 }
720
721 WRITE_ONCE(tp->copied_seq, seq);
722 more_data_avail = mptcp_subflow_data_available(ssk);
723
724 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
725 done = true;
726 break;
727 }
728 } while (more_data_avail);
729
730 *bytes += moved;
731 return done;
732 }
733
__mptcp_ofo_queue(struct mptcp_sock * msk)734 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
735 {
736 struct sock *sk = (struct sock *)msk;
737 struct sk_buff *skb, *tail;
738 bool moved = false;
739 struct rb_node *p;
740 u64 end_seq;
741
742 p = rb_first(&msk->out_of_order_queue);
743 pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
744 while (p) {
745 skb = rb_to_skb(p);
746 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
747 break;
748
749 p = rb_next(p);
750 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
751
752 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
753 msk->ack_seq))) {
754 mptcp_drop(sk, skb);
755 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
756 continue;
757 }
758
759 end_seq = MPTCP_SKB_CB(skb)->end_seq;
760 tail = skb_peek_tail(&sk->sk_receive_queue);
761 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
762 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
763
764 /* skip overlapping data, if any */
765 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
766 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
767 delta);
768 MPTCP_SKB_CB(skb)->offset += delta;
769 MPTCP_SKB_CB(skb)->map_seq += delta;
770 __skb_queue_tail(&sk->sk_receive_queue, skb);
771 }
772 msk->bytes_received += end_seq - msk->ack_seq;
773 msk->ack_seq = end_seq;
774 moved = true;
775 }
776 return moved;
777 }
778
__mptcp_subflow_error_report(struct sock * sk,struct sock * ssk)779 static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
780 {
781 int err = sock_error(ssk);
782 int ssk_state;
783
784 if (!err)
785 return false;
786
787 /* only propagate errors on fallen-back sockets or
788 * on MPC connect
789 */
790 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
791 return false;
792
793 /* We need to propagate only transition to CLOSE state.
794 * Orphaned socket will see such state change via
795 * subflow_sched_work_if_closed() and that path will properly
796 * destroy the msk as needed.
797 */
798 ssk_state = inet_sk_state_load(ssk);
799 if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
800 mptcp_set_state(sk, ssk_state);
801 WRITE_ONCE(sk->sk_err, -err);
802
803 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
804 smp_wmb();
805 sk_error_report(sk);
806 return true;
807 }
808
__mptcp_error_report(struct sock * sk)809 void __mptcp_error_report(struct sock *sk)
810 {
811 struct mptcp_subflow_context *subflow;
812 struct mptcp_sock *msk = mptcp_sk(sk);
813
814 mptcp_for_each_subflow(msk, subflow)
815 if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
816 break;
817 }
818
819 /* In most cases we will be able to lock the mptcp socket. If its already
820 * owned, we need to defer to the work queue to avoid ABBA deadlock.
821 */
move_skbs_to_msk(struct mptcp_sock * msk,struct sock * ssk)822 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
823 {
824 struct sock *sk = (struct sock *)msk;
825 unsigned int moved = 0;
826
827 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
828 __mptcp_ofo_queue(msk);
829 if (unlikely(ssk->sk_err)) {
830 if (!sock_owned_by_user(sk))
831 __mptcp_error_report(sk);
832 else
833 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
834 }
835
836 /* If the moves have caught up with the DATA_FIN sequence number
837 * it's time to ack the DATA_FIN and change socket state, but
838 * this is not a good place to change state. Let the workqueue
839 * do it.
840 */
841 if (mptcp_pending_data_fin(sk, NULL))
842 mptcp_schedule_work(sk);
843 return moved > 0;
844 }
845
mptcp_data_ready(struct sock * sk,struct sock * ssk)846 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
847 {
848 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
849 struct mptcp_sock *msk = mptcp_sk(sk);
850 int sk_rbuf, ssk_rbuf;
851
852 /* The peer can send data while we are shutting down this
853 * subflow at msk destruction time, but we must avoid enqueuing
854 * more data to the msk receive queue
855 */
856 if (unlikely(subflow->disposable))
857 return;
858
859 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
860 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
861 if (unlikely(ssk_rbuf > sk_rbuf))
862 sk_rbuf = ssk_rbuf;
863
864 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
865 if (__mptcp_rmem(sk) > sk_rbuf)
866 return;
867
868 /* Wake-up the reader only for in-sequence data */
869 mptcp_data_lock(sk);
870 if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
871 sk->sk_data_ready(sk);
872 mptcp_data_unlock(sk);
873 }
874
mptcp_subflow_joined(struct mptcp_sock * msk,struct sock * ssk)875 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
876 {
877 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
878 WRITE_ONCE(msk->allow_infinite_fallback, false);
879 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
880 }
881
__mptcp_finish_join(struct mptcp_sock * msk,struct sock * ssk)882 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
883 {
884 struct sock *sk = (struct sock *)msk;
885
886 if (sk->sk_state != TCP_ESTABLISHED)
887 return false;
888
889 /* attach to msk socket only after we are sure we will deal with it
890 * at close time
891 */
892 if (sk->sk_socket && !ssk->sk_socket)
893 mptcp_sock_graft(ssk, sk->sk_socket);
894
895 mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
896 mptcp_sockopt_sync_locked(msk, ssk);
897 mptcp_subflow_joined(msk, ssk);
898 mptcp_stop_tout_timer(sk);
899 __mptcp_propagate_sndbuf(sk, ssk);
900 return true;
901 }
902
__mptcp_flush_join_list(struct sock * sk,struct list_head * join_list)903 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
904 {
905 struct mptcp_subflow_context *tmp, *subflow;
906 struct mptcp_sock *msk = mptcp_sk(sk);
907
908 list_for_each_entry_safe(subflow, tmp, join_list, node) {
909 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
910 bool slow = lock_sock_fast(ssk);
911
912 list_move_tail(&subflow->node, &msk->conn_list);
913 if (!__mptcp_finish_join(msk, ssk))
914 mptcp_subflow_reset(ssk);
915 unlock_sock_fast(ssk, slow);
916 }
917 }
918
mptcp_rtx_timer_pending(struct sock * sk)919 static bool mptcp_rtx_timer_pending(struct sock *sk)
920 {
921 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
922 }
923
mptcp_reset_rtx_timer(struct sock * sk)924 static void mptcp_reset_rtx_timer(struct sock *sk)
925 {
926 struct inet_connection_sock *icsk = inet_csk(sk);
927 unsigned long tout;
928
929 /* prevent rescheduling on close */
930 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
931 return;
932
933 tout = mptcp_sk(sk)->timer_ival;
934 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
935 }
936
mptcp_schedule_work(struct sock * sk)937 bool mptcp_schedule_work(struct sock *sk)
938 {
939 if (inet_sk_state_load(sk) != TCP_CLOSE &&
940 schedule_work(&mptcp_sk(sk)->work)) {
941 /* each subflow already holds a reference to the sk, and the
942 * workqueue is invoked by a subflow, so sk can't go away here.
943 */
944 sock_hold(sk);
945 return true;
946 }
947 return false;
948 }
949
mptcp_subflow_recv_lookup(const struct mptcp_sock * msk)950 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
951 {
952 struct mptcp_subflow_context *subflow;
953
954 msk_owned_by_me(msk);
955
956 mptcp_for_each_subflow(msk, subflow) {
957 if (READ_ONCE(subflow->data_avail))
958 return mptcp_subflow_tcp_sock(subflow);
959 }
960
961 return NULL;
962 }
963
mptcp_skb_can_collapse_to(u64 write_seq,const struct sk_buff * skb,const struct mptcp_ext * mpext)964 static bool mptcp_skb_can_collapse_to(u64 write_seq,
965 const struct sk_buff *skb,
966 const struct mptcp_ext *mpext)
967 {
968 if (!tcp_skb_can_collapse_to(skb))
969 return false;
970
971 /* can collapse only if MPTCP level sequence is in order and this
972 * mapping has not been xmitted yet
973 */
974 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
975 !mpext->frozen;
976 }
977
978 /* we can append data to the given data frag if:
979 * - there is space available in the backing page_frag
980 * - the data frag tail matches the current page_frag free offset
981 * - the data frag end sequence number matches the current write seq
982 */
mptcp_frag_can_collapse_to(const struct mptcp_sock * msk,const struct page_frag * pfrag,const struct mptcp_data_frag * df)983 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
984 const struct page_frag *pfrag,
985 const struct mptcp_data_frag *df)
986 {
987 return df && pfrag->page == df->page &&
988 pfrag->size - pfrag->offset > 0 &&
989 pfrag->offset == (df->offset + df->data_len) &&
990 df->data_seq + df->data_len == msk->write_seq;
991 }
992
dfrag_uncharge(struct sock * sk,int len)993 static void dfrag_uncharge(struct sock *sk, int len)
994 {
995 sk_mem_uncharge(sk, len);
996 sk_wmem_queued_add(sk, -len);
997 }
998
dfrag_clear(struct sock * sk,struct mptcp_data_frag * dfrag)999 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
1000 {
1001 int len = dfrag->data_len + dfrag->overhead;
1002
1003 list_del(&dfrag->list);
1004 dfrag_uncharge(sk, len);
1005 put_page(dfrag->page);
1006 }
1007
__mptcp_clean_una(struct sock * sk)1008 static void __mptcp_clean_una(struct sock *sk)
1009 {
1010 struct mptcp_sock *msk = mptcp_sk(sk);
1011 struct mptcp_data_frag *dtmp, *dfrag;
1012 u64 snd_una;
1013
1014 snd_una = msk->snd_una;
1015 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1016 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1017 break;
1018
1019 if (unlikely(dfrag == msk->first_pending)) {
1020 /* in recovery mode can see ack after the current snd head */
1021 if (WARN_ON_ONCE(!msk->recovery))
1022 break;
1023
1024 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1025 }
1026
1027 dfrag_clear(sk, dfrag);
1028 }
1029
1030 dfrag = mptcp_rtx_head(sk);
1031 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1032 u64 delta = snd_una - dfrag->data_seq;
1033
1034 /* prevent wrap around in recovery mode */
1035 if (unlikely(delta > dfrag->already_sent)) {
1036 if (WARN_ON_ONCE(!msk->recovery))
1037 goto out;
1038 if (WARN_ON_ONCE(delta > dfrag->data_len))
1039 goto out;
1040 dfrag->already_sent += delta - dfrag->already_sent;
1041 }
1042
1043 dfrag->data_seq += delta;
1044 dfrag->offset += delta;
1045 dfrag->data_len -= delta;
1046 dfrag->already_sent -= delta;
1047
1048 dfrag_uncharge(sk, delta);
1049 }
1050
1051 /* all retransmitted data acked, recovery completed */
1052 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1053 msk->recovery = false;
1054
1055 out:
1056 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1057 snd_una == READ_ONCE(msk->write_seq)) {
1058 if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1059 mptcp_stop_rtx_timer(sk);
1060 } else {
1061 mptcp_reset_rtx_timer(sk);
1062 }
1063 }
1064
__mptcp_clean_una_wakeup(struct sock * sk)1065 static void __mptcp_clean_una_wakeup(struct sock *sk)
1066 {
1067 lockdep_assert_held_once(&sk->sk_lock.slock);
1068
1069 __mptcp_clean_una(sk);
1070 mptcp_write_space(sk);
1071 }
1072
mptcp_clean_una_wakeup(struct sock * sk)1073 static void mptcp_clean_una_wakeup(struct sock *sk)
1074 {
1075 mptcp_data_lock(sk);
1076 __mptcp_clean_una_wakeup(sk);
1077 mptcp_data_unlock(sk);
1078 }
1079
mptcp_enter_memory_pressure(struct sock * sk)1080 static void mptcp_enter_memory_pressure(struct sock *sk)
1081 {
1082 struct mptcp_subflow_context *subflow;
1083 struct mptcp_sock *msk = mptcp_sk(sk);
1084 bool first = true;
1085
1086 mptcp_for_each_subflow(msk, subflow) {
1087 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1088
1089 if (first)
1090 tcp_enter_memory_pressure(ssk);
1091 sk_stream_moderate_sndbuf(ssk);
1092
1093 first = false;
1094 }
1095 __mptcp_sync_sndbuf(sk);
1096 }
1097
1098 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1099 * data
1100 */
mptcp_page_frag_refill(struct sock * sk,struct page_frag * pfrag)1101 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1102 {
1103 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1104 pfrag, sk->sk_allocation)))
1105 return true;
1106
1107 mptcp_enter_memory_pressure(sk);
1108 return false;
1109 }
1110
1111 static struct mptcp_data_frag *
mptcp_carve_data_frag(const struct mptcp_sock * msk,struct page_frag * pfrag,int orig_offset)1112 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1113 int orig_offset)
1114 {
1115 int offset = ALIGN(orig_offset, sizeof(long));
1116 struct mptcp_data_frag *dfrag;
1117
1118 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1119 dfrag->data_len = 0;
1120 dfrag->data_seq = msk->write_seq;
1121 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1122 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1123 dfrag->already_sent = 0;
1124 dfrag->page = pfrag->page;
1125
1126 return dfrag;
1127 }
1128
1129 struct mptcp_sendmsg_info {
1130 int mss_now;
1131 int size_goal;
1132 u16 limit;
1133 u16 sent;
1134 unsigned int flags;
1135 bool data_lock_held;
1136 };
1137
mptcp_check_allowed_size(const struct mptcp_sock * msk,struct sock * ssk,u64 data_seq,int avail_size)1138 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1139 u64 data_seq, int avail_size)
1140 {
1141 u64 window_end = mptcp_wnd_end(msk);
1142 u64 mptcp_snd_wnd;
1143
1144 if (__mptcp_check_fallback(msk))
1145 return avail_size;
1146
1147 mptcp_snd_wnd = window_end - data_seq;
1148 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1149
1150 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1151 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1152 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1153 }
1154
1155 return avail_size;
1156 }
1157
__mptcp_add_ext(struct sk_buff * skb,gfp_t gfp)1158 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1159 {
1160 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1161
1162 if (!mpext)
1163 return false;
1164 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1165 return true;
1166 }
1167
__mptcp_do_alloc_tx_skb(struct sock * sk,gfp_t gfp)1168 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1169 {
1170 struct sk_buff *skb;
1171
1172 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1173 if (likely(skb)) {
1174 if (likely(__mptcp_add_ext(skb, gfp))) {
1175 skb_reserve(skb, MAX_TCP_HEADER);
1176 skb->ip_summed = CHECKSUM_PARTIAL;
1177 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1178 return skb;
1179 }
1180 __kfree_skb(skb);
1181 } else {
1182 mptcp_enter_memory_pressure(sk);
1183 }
1184 return NULL;
1185 }
1186
__mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,gfp_t gfp)1187 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1188 {
1189 struct sk_buff *skb;
1190
1191 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1192 if (!skb)
1193 return NULL;
1194
1195 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1196 tcp_skb_entail(ssk, skb);
1197 return skb;
1198 }
1199 tcp_skb_tsorted_anchor_cleanup(skb);
1200 kfree_skb(skb);
1201 return NULL;
1202 }
1203
mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,bool data_lock_held)1204 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1205 {
1206 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1207
1208 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1209 }
1210
1211 /* note: this always recompute the csum on the whole skb, even
1212 * if we just appended a single frag. More status info needed
1213 */
mptcp_update_data_checksum(struct sk_buff * skb,int added)1214 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1215 {
1216 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1217 __wsum csum = ~csum_unfold(mpext->csum);
1218 int offset = skb->len - added;
1219
1220 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1221 }
1222
mptcp_update_infinite_map(struct mptcp_sock * msk,struct sock * ssk,struct mptcp_ext * mpext)1223 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1224 struct sock *ssk,
1225 struct mptcp_ext *mpext)
1226 {
1227 if (!mpext)
1228 return;
1229
1230 mpext->infinite_map = 1;
1231 mpext->data_len = 0;
1232
1233 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1234 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1235 pr_fallback(msk);
1236 mptcp_do_fallback(ssk);
1237 }
1238
1239 #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
1240
mptcp_sendmsg_frag(struct sock * sk,struct sock * ssk,struct mptcp_data_frag * dfrag,struct mptcp_sendmsg_info * info)1241 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1242 struct mptcp_data_frag *dfrag,
1243 struct mptcp_sendmsg_info *info)
1244 {
1245 u64 data_seq = dfrag->data_seq + info->sent;
1246 int offset = dfrag->offset + info->sent;
1247 struct mptcp_sock *msk = mptcp_sk(sk);
1248 bool zero_window_probe = false;
1249 struct mptcp_ext *mpext = NULL;
1250 bool can_coalesce = false;
1251 bool reuse_skb = true;
1252 struct sk_buff *skb;
1253 size_t copy;
1254 int i;
1255
1256 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
1257 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1258
1259 if (WARN_ON_ONCE(info->sent > info->limit ||
1260 info->limit > dfrag->data_len))
1261 return 0;
1262
1263 if (unlikely(!__tcp_can_send(ssk)))
1264 return -EAGAIN;
1265
1266 /* compute send limit */
1267 if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
1268 ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1269 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1270 copy = info->size_goal;
1271
1272 skb = tcp_write_queue_tail(ssk);
1273 if (skb && copy > skb->len) {
1274 /* Limit the write to the size available in the
1275 * current skb, if any, so that we create at most a new skb.
1276 * Explicitly tells TCP internals to avoid collapsing on later
1277 * queue management operation, to avoid breaking the ext <->
1278 * SSN association set here
1279 */
1280 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1281 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1282 TCP_SKB_CB(skb)->eor = 1;
1283 tcp_mark_push(tcp_sk(ssk), skb);
1284 goto alloc_skb;
1285 }
1286
1287 i = skb_shinfo(skb)->nr_frags;
1288 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1289 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1290 tcp_mark_push(tcp_sk(ssk), skb);
1291 goto alloc_skb;
1292 }
1293
1294 copy -= skb->len;
1295 } else {
1296 alloc_skb:
1297 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1298 if (!skb)
1299 return -ENOMEM;
1300
1301 i = skb_shinfo(skb)->nr_frags;
1302 reuse_skb = false;
1303 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1304 }
1305
1306 /* Zero window and all data acked? Probe. */
1307 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1308 if (copy == 0) {
1309 u64 snd_una = READ_ONCE(msk->snd_una);
1310
1311 if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) {
1312 tcp_remove_empty_skb(ssk);
1313 return 0;
1314 }
1315
1316 zero_window_probe = true;
1317 data_seq = snd_una - 1;
1318 copy = 1;
1319 }
1320
1321 copy = min_t(size_t, copy, info->limit - info->sent);
1322 if (!sk_wmem_schedule(ssk, copy)) {
1323 tcp_remove_empty_skb(ssk);
1324 return -ENOMEM;
1325 }
1326
1327 if (can_coalesce) {
1328 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1329 } else {
1330 get_page(dfrag->page);
1331 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1332 }
1333
1334 skb->len += copy;
1335 skb->data_len += copy;
1336 skb->truesize += copy;
1337 sk_wmem_queued_add(ssk, copy);
1338 sk_mem_charge(ssk, copy);
1339 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1340 TCP_SKB_CB(skb)->end_seq += copy;
1341 tcp_skb_pcount_set(skb, 0);
1342
1343 /* on skb reuse we just need to update the DSS len */
1344 if (reuse_skb) {
1345 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1346 mpext->data_len += copy;
1347 goto out;
1348 }
1349
1350 memset(mpext, 0, sizeof(*mpext));
1351 mpext->data_seq = data_seq;
1352 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1353 mpext->data_len = copy;
1354 mpext->use_map = 1;
1355 mpext->dsn64 = 1;
1356
1357 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
1358 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1359 mpext->dsn64);
1360
1361 if (zero_window_probe) {
1362 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1363 mpext->frozen = 1;
1364 if (READ_ONCE(msk->csum_enabled))
1365 mptcp_update_data_checksum(skb, copy);
1366 tcp_push_pending_frames(ssk);
1367 return 0;
1368 }
1369 out:
1370 if (READ_ONCE(msk->csum_enabled))
1371 mptcp_update_data_checksum(skb, copy);
1372 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1373 mptcp_update_infinite_map(msk, ssk, mpext);
1374 trace_mptcp_sendmsg_frag(mpext);
1375 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1376 return copy;
1377 }
1378
1379 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1380 sizeof(struct tcphdr) - \
1381 MAX_TCP_OPTION_SPACE - \
1382 sizeof(struct ipv6hdr) - \
1383 sizeof(struct frag_hdr))
1384
1385 struct subflow_send_info {
1386 struct sock *ssk;
1387 u64 linger_time;
1388 };
1389
mptcp_subflow_set_active(struct mptcp_subflow_context * subflow)1390 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1391 {
1392 if (!subflow->stale)
1393 return;
1394
1395 subflow->stale = 0;
1396 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1397 }
1398
mptcp_subflow_active(struct mptcp_subflow_context * subflow)1399 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1400 {
1401 if (unlikely(subflow->stale)) {
1402 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1403
1404 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1405 return false;
1406
1407 mptcp_subflow_set_active(subflow);
1408 }
1409 return __mptcp_subflow_active(subflow);
1410 }
1411
1412 #define SSK_MODE_ACTIVE 0
1413 #define SSK_MODE_BACKUP 1
1414 #define SSK_MODE_MAX 2
1415
1416 /* implement the mptcp packet scheduler;
1417 * returns the subflow that will transmit the next DSS
1418 * additionally updates the rtx timeout
1419 */
mptcp_subflow_get_send(struct mptcp_sock * msk)1420 struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1421 {
1422 struct subflow_send_info send_info[SSK_MODE_MAX];
1423 struct mptcp_subflow_context *subflow;
1424 struct sock *sk = (struct sock *)msk;
1425 u32 pace, burst, wmem;
1426 int i, nr_active = 0;
1427 struct sock *ssk;
1428 u64 linger_time;
1429 long tout = 0;
1430
1431 /* pick the subflow with the lower wmem/wspace ratio */
1432 for (i = 0; i < SSK_MODE_MAX; ++i) {
1433 send_info[i].ssk = NULL;
1434 send_info[i].linger_time = -1;
1435 }
1436
1437 mptcp_for_each_subflow(msk, subflow) {
1438 bool backup = subflow->backup || subflow->request_bkup;
1439
1440 trace_mptcp_subflow_get_send(subflow);
1441 ssk = mptcp_subflow_tcp_sock(subflow);
1442 if (!mptcp_subflow_active(subflow))
1443 continue;
1444
1445 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1446 nr_active += !backup;
1447 pace = subflow->avg_pacing_rate;
1448 if (unlikely(!pace)) {
1449 /* init pacing rate from socket */
1450 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1451 pace = subflow->avg_pacing_rate;
1452 if (!pace)
1453 continue;
1454 }
1455
1456 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1457 if (linger_time < send_info[backup].linger_time) {
1458 send_info[backup].ssk = ssk;
1459 send_info[backup].linger_time = linger_time;
1460 }
1461 }
1462 __mptcp_set_timeout(sk, tout);
1463
1464 /* pick the best backup if no other subflow is active */
1465 if (!nr_active)
1466 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1467
1468 /* According to the blest algorithm, to avoid HoL blocking for the
1469 * faster flow, we need to:
1470 * - estimate the faster flow linger time
1471 * - use the above to estimate the amount of byte transferred
1472 * by the faster flow
1473 * - check that the amount of queued data is greter than the above,
1474 * otherwise do not use the picked, slower, subflow
1475 * We select the subflow with the shorter estimated time to flush
1476 * the queued mem, which basically ensure the above. We just need
1477 * to check that subflow has a non empty cwin.
1478 */
1479 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1480 if (!ssk || !sk_stream_memory_free(ssk))
1481 return NULL;
1482
1483 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1484 wmem = READ_ONCE(ssk->sk_wmem_queued);
1485 if (!burst)
1486 return ssk;
1487
1488 subflow = mptcp_subflow_ctx(ssk);
1489 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1490 READ_ONCE(ssk->sk_pacing_rate) * burst,
1491 burst + wmem);
1492 msk->snd_burst = burst;
1493 return ssk;
1494 }
1495
mptcp_push_release(struct sock * ssk,struct mptcp_sendmsg_info * info)1496 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1497 {
1498 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1499 release_sock(ssk);
1500 }
1501
mptcp_update_post_push(struct mptcp_sock * msk,struct mptcp_data_frag * dfrag,u32 sent)1502 static void mptcp_update_post_push(struct mptcp_sock *msk,
1503 struct mptcp_data_frag *dfrag,
1504 u32 sent)
1505 {
1506 u64 snd_nxt_new = dfrag->data_seq;
1507
1508 dfrag->already_sent += sent;
1509
1510 msk->snd_burst -= sent;
1511
1512 snd_nxt_new += dfrag->already_sent;
1513
1514 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1515 * is recovering after a failover. In that event, this re-sends
1516 * old segments.
1517 *
1518 * Thus compute snd_nxt_new candidate based on
1519 * the dfrag->data_seq that was sent and the data
1520 * that has been handed to the subflow for transmission
1521 * and skip update in case it was old dfrag.
1522 */
1523 if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1524 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1525 msk->snd_nxt = snd_nxt_new;
1526 }
1527 }
1528
mptcp_check_and_set_pending(struct sock * sk)1529 void mptcp_check_and_set_pending(struct sock *sk)
1530 {
1531 if (mptcp_send_head(sk)) {
1532 mptcp_data_lock(sk);
1533 mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1534 mptcp_data_unlock(sk);
1535 }
1536 }
1537
__subflow_push_pending(struct sock * sk,struct sock * ssk,struct mptcp_sendmsg_info * info)1538 static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1539 struct mptcp_sendmsg_info *info)
1540 {
1541 struct mptcp_sock *msk = mptcp_sk(sk);
1542 struct mptcp_data_frag *dfrag;
1543 int len, copied = 0, err = 0;
1544
1545 while ((dfrag = mptcp_send_head(sk))) {
1546 info->sent = dfrag->already_sent;
1547 info->limit = dfrag->data_len;
1548 len = dfrag->data_len - dfrag->already_sent;
1549 while (len > 0) {
1550 int ret = 0;
1551
1552 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1553 if (ret <= 0) {
1554 err = copied ? : ret;
1555 goto out;
1556 }
1557
1558 info->sent += ret;
1559 copied += ret;
1560 len -= ret;
1561
1562 mptcp_update_post_push(msk, dfrag, ret);
1563 }
1564 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1565
1566 if (msk->snd_burst <= 0 ||
1567 !sk_stream_memory_free(ssk) ||
1568 !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1569 err = copied;
1570 goto out;
1571 }
1572 mptcp_set_timeout(sk);
1573 }
1574 err = copied;
1575
1576 out:
1577 return err;
1578 }
1579
__mptcp_push_pending(struct sock * sk,unsigned int flags)1580 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1581 {
1582 struct sock *prev_ssk = NULL, *ssk = NULL;
1583 struct mptcp_sock *msk = mptcp_sk(sk);
1584 struct mptcp_sendmsg_info info = {
1585 .flags = flags,
1586 };
1587 bool do_check_data_fin = false;
1588 int push_count = 1;
1589
1590 while (mptcp_send_head(sk) && (push_count > 0)) {
1591 struct mptcp_subflow_context *subflow;
1592 int ret = 0;
1593
1594 if (mptcp_sched_get_send(msk))
1595 break;
1596
1597 push_count = 0;
1598
1599 mptcp_for_each_subflow(msk, subflow) {
1600 if (READ_ONCE(subflow->scheduled)) {
1601 mptcp_subflow_set_scheduled(subflow, false);
1602
1603 prev_ssk = ssk;
1604 ssk = mptcp_subflow_tcp_sock(subflow);
1605 if (ssk != prev_ssk) {
1606 /* First check. If the ssk has changed since
1607 * the last round, release prev_ssk
1608 */
1609 if (prev_ssk)
1610 mptcp_push_release(prev_ssk, &info);
1611
1612 /* Need to lock the new subflow only if different
1613 * from the previous one, otherwise we are still
1614 * helding the relevant lock
1615 */
1616 lock_sock(ssk);
1617 }
1618
1619 push_count++;
1620
1621 ret = __subflow_push_pending(sk, ssk, &info);
1622 if (ret <= 0) {
1623 if (ret != -EAGAIN ||
1624 (1 << ssk->sk_state) &
1625 (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1626 push_count--;
1627 continue;
1628 }
1629 do_check_data_fin = true;
1630 }
1631 }
1632 }
1633
1634 /* at this point we held the socket lock for the last subflow we used */
1635 if (ssk)
1636 mptcp_push_release(ssk, &info);
1637
1638 /* ensure the rtx timer is running */
1639 if (!mptcp_rtx_timer_pending(sk))
1640 mptcp_reset_rtx_timer(sk);
1641 if (do_check_data_fin)
1642 mptcp_check_send_data_fin(sk);
1643 }
1644
__mptcp_subflow_push_pending(struct sock * sk,struct sock * ssk,bool first)1645 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1646 {
1647 struct mptcp_sock *msk = mptcp_sk(sk);
1648 struct mptcp_sendmsg_info info = {
1649 .data_lock_held = true,
1650 };
1651 bool keep_pushing = true;
1652 struct sock *xmit_ssk;
1653 int copied = 0;
1654
1655 info.flags = 0;
1656 while (mptcp_send_head(sk) && keep_pushing) {
1657 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1658 int ret = 0;
1659
1660 /* check for a different subflow usage only after
1661 * spooling the first chunk of data
1662 */
1663 if (first) {
1664 mptcp_subflow_set_scheduled(subflow, false);
1665 ret = __subflow_push_pending(sk, ssk, &info);
1666 first = false;
1667 if (ret <= 0)
1668 break;
1669 copied += ret;
1670 continue;
1671 }
1672
1673 if (mptcp_sched_get_send(msk))
1674 goto out;
1675
1676 if (READ_ONCE(subflow->scheduled)) {
1677 mptcp_subflow_set_scheduled(subflow, false);
1678 ret = __subflow_push_pending(sk, ssk, &info);
1679 if (ret <= 0)
1680 keep_pushing = false;
1681 copied += ret;
1682 }
1683
1684 mptcp_for_each_subflow(msk, subflow) {
1685 if (READ_ONCE(subflow->scheduled)) {
1686 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1687 if (xmit_ssk != ssk) {
1688 mptcp_subflow_delegate(subflow,
1689 MPTCP_DELEGATE_SEND);
1690 keep_pushing = false;
1691 }
1692 }
1693 }
1694 }
1695
1696 out:
1697 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1698 * not going to flush it via release_sock()
1699 */
1700 if (copied) {
1701 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1702 info.size_goal);
1703 if (!mptcp_rtx_timer_pending(sk))
1704 mptcp_reset_rtx_timer(sk);
1705
1706 if (msk->snd_data_fin_enable &&
1707 msk->snd_nxt + 1 == msk->write_seq)
1708 mptcp_schedule_work(sk);
1709 }
1710 }
1711
mptcp_set_nospace(struct sock * sk)1712 static void mptcp_set_nospace(struct sock *sk)
1713 {
1714 /* enable autotune */
1715 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1716
1717 /* will be cleared on avail space */
1718 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1719 }
1720
1721 static int mptcp_disconnect(struct sock *sk, int flags);
1722
mptcp_sendmsg_fastopen(struct sock * sk,struct msghdr * msg,size_t len,int * copied_syn)1723 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1724 size_t len, int *copied_syn)
1725 {
1726 unsigned int saved_flags = msg->msg_flags;
1727 struct mptcp_sock *msk = mptcp_sk(sk);
1728 struct sock *ssk;
1729 int ret;
1730
1731 /* on flags based fastopen the mptcp is supposed to create the
1732 * first subflow right now. Otherwise we are in the defer_connect
1733 * path, and the first subflow must be already present.
1734 * Since the defer_connect flag is cleared after the first succsful
1735 * fastopen attempt, no need to check for additional subflow status.
1736 */
1737 if (msg->msg_flags & MSG_FASTOPEN) {
1738 ssk = __mptcp_nmpc_sk(msk);
1739 if (IS_ERR(ssk))
1740 return PTR_ERR(ssk);
1741 }
1742 if (!msk->first)
1743 return -EINVAL;
1744
1745 ssk = msk->first;
1746
1747 lock_sock(ssk);
1748 msg->msg_flags |= MSG_DONTWAIT;
1749 msk->fastopening = 1;
1750 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1751 msk->fastopening = 0;
1752 msg->msg_flags = saved_flags;
1753 release_sock(ssk);
1754
1755 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1756 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1757 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1758 msg->msg_namelen, msg->msg_flags, 1);
1759
1760 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1761 * case of any error, except timeout or signal
1762 */
1763 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1764 *copied_syn = 0;
1765 } else if (ret && ret != -EINPROGRESS) {
1766 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1767 * __inet_stream_connect() can fail, due to looking check,
1768 * see mptcp_disconnect().
1769 * Attempt it again outside the problematic scope.
1770 */
1771 if (!mptcp_disconnect(sk, 0))
1772 sk->sk_socket->state = SS_UNCONNECTED;
1773 }
1774 inet_clear_bit(DEFER_CONNECT, sk);
1775
1776 return ret;
1777 }
1778
mptcp_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)1779 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1780 {
1781 struct mptcp_sock *msk = mptcp_sk(sk);
1782 struct page_frag *pfrag;
1783 size_t copied = 0;
1784 int ret = 0;
1785 long timeo;
1786
1787 /* silently ignore everything else */
1788 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1789
1790 lock_sock(sk);
1791
1792 if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1793 msg->msg_flags & MSG_FASTOPEN)) {
1794 int copied_syn = 0;
1795
1796 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1797 copied += copied_syn;
1798 if (ret == -EINPROGRESS && copied_syn > 0)
1799 goto out;
1800 else if (ret)
1801 goto do_error;
1802 }
1803
1804 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1805
1806 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1807 ret = sk_stream_wait_connect(sk, &timeo);
1808 if (ret)
1809 goto do_error;
1810 }
1811
1812 ret = -EPIPE;
1813 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1814 goto do_error;
1815
1816 pfrag = sk_page_frag(sk);
1817
1818 while (msg_data_left(msg)) {
1819 int total_ts, frag_truesize = 0;
1820 struct mptcp_data_frag *dfrag;
1821 bool dfrag_collapsed;
1822 size_t psize, offset;
1823
1824 /* reuse tail pfrag, if possible, or carve a new one from the
1825 * page allocator
1826 */
1827 dfrag = mptcp_pending_tail(sk);
1828 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1829 if (!dfrag_collapsed) {
1830 if (!sk_stream_memory_free(sk))
1831 goto wait_for_memory;
1832
1833 if (!mptcp_page_frag_refill(sk, pfrag))
1834 goto wait_for_memory;
1835
1836 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1837 frag_truesize = dfrag->overhead;
1838 }
1839
1840 /* we do not bound vs wspace, to allow a single packet.
1841 * memory accounting will prevent execessive memory usage
1842 * anyway
1843 */
1844 offset = dfrag->offset + dfrag->data_len;
1845 psize = pfrag->size - offset;
1846 psize = min_t(size_t, psize, msg_data_left(msg));
1847 total_ts = psize + frag_truesize;
1848
1849 if (!sk_wmem_schedule(sk, total_ts))
1850 goto wait_for_memory;
1851
1852 if (copy_page_from_iter(dfrag->page, offset, psize,
1853 &msg->msg_iter) != psize) {
1854 ret = -EFAULT;
1855 goto do_error;
1856 }
1857
1858 /* data successfully copied into the write queue */
1859 sk_forward_alloc_add(sk, -total_ts);
1860 copied += psize;
1861 dfrag->data_len += psize;
1862 frag_truesize += psize;
1863 pfrag->offset += frag_truesize;
1864 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1865
1866 /* charge data on mptcp pending queue to the msk socket
1867 * Note: we charge such data both to sk and ssk
1868 */
1869 sk_wmem_queued_add(sk, frag_truesize);
1870 if (!dfrag_collapsed) {
1871 get_page(dfrag->page);
1872 list_add_tail(&dfrag->list, &msk->rtx_queue);
1873 if (!msk->first_pending)
1874 WRITE_ONCE(msk->first_pending, dfrag);
1875 }
1876 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
1877 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1878 !dfrag_collapsed);
1879
1880 continue;
1881
1882 wait_for_memory:
1883 mptcp_set_nospace(sk);
1884 __mptcp_push_pending(sk, msg->msg_flags);
1885 ret = sk_stream_wait_memory(sk, &timeo);
1886 if (ret)
1887 goto do_error;
1888 }
1889
1890 if (copied)
1891 __mptcp_push_pending(sk, msg->msg_flags);
1892
1893 out:
1894 release_sock(sk);
1895 return copied;
1896
1897 do_error:
1898 if (copied)
1899 goto out;
1900
1901 copied = sk_stream_error(sk, msg->msg_flags, ret);
1902 goto out;
1903 }
1904
__mptcp_recvmsg_mskq(struct mptcp_sock * msk,struct msghdr * msg,size_t len,int flags,struct scm_timestamping_internal * tss,int * cmsg_flags)1905 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1906 struct msghdr *msg,
1907 size_t len, int flags,
1908 struct scm_timestamping_internal *tss,
1909 int *cmsg_flags)
1910 {
1911 struct sk_buff *skb, *tmp;
1912 int copied = 0;
1913
1914 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1915 u32 offset = MPTCP_SKB_CB(skb)->offset;
1916 u32 data_len = skb->len - offset;
1917 u32 count = min_t(size_t, len - copied, data_len);
1918 int err;
1919
1920 if (!(flags & MSG_TRUNC)) {
1921 err = skb_copy_datagram_msg(skb, offset, msg, count);
1922 if (unlikely(err < 0)) {
1923 if (!copied)
1924 return err;
1925 break;
1926 }
1927 }
1928
1929 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1930 tcp_update_recv_tstamps(skb, tss);
1931 *cmsg_flags |= MPTCP_CMSG_TS;
1932 }
1933
1934 copied += count;
1935
1936 if (count < data_len) {
1937 if (!(flags & MSG_PEEK)) {
1938 MPTCP_SKB_CB(skb)->offset += count;
1939 MPTCP_SKB_CB(skb)->map_seq += count;
1940 msk->bytes_consumed += count;
1941 }
1942 break;
1943 }
1944
1945 if (!(flags & MSG_PEEK)) {
1946 /* we will bulk release the skb memory later */
1947 skb->destructor = NULL;
1948 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1949 __skb_unlink(skb, &msk->receive_queue);
1950 __kfree_skb(skb);
1951 msk->bytes_consumed += count;
1952 }
1953
1954 if (copied >= len)
1955 break;
1956 }
1957
1958 return copied;
1959 }
1960
1961 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1962 *
1963 * Only difference: Use highest rtt estimate of the subflows in use.
1964 */
mptcp_rcv_space_adjust(struct mptcp_sock * msk,int copied)1965 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1966 {
1967 struct mptcp_subflow_context *subflow;
1968 struct sock *sk = (struct sock *)msk;
1969 u8 scaling_ratio = U8_MAX;
1970 u32 time, advmss = 1;
1971 u64 rtt_us, mstamp;
1972
1973 msk_owned_by_me(msk);
1974
1975 if (copied <= 0)
1976 return;
1977
1978 if (!msk->rcvspace_init)
1979 mptcp_rcv_space_init(msk, msk->first);
1980
1981 msk->rcvq_space.copied += copied;
1982
1983 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1984 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1985
1986 rtt_us = msk->rcvq_space.rtt_us;
1987 if (rtt_us && time < (rtt_us >> 3))
1988 return;
1989
1990 rtt_us = 0;
1991 mptcp_for_each_subflow(msk, subflow) {
1992 const struct tcp_sock *tp;
1993 u64 sf_rtt_us;
1994 u32 sf_advmss;
1995
1996 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1997
1998 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1999 sf_advmss = READ_ONCE(tp->advmss);
2000
2001 rtt_us = max(sf_rtt_us, rtt_us);
2002 advmss = max(sf_advmss, advmss);
2003 scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
2004 }
2005
2006 msk->rcvq_space.rtt_us = rtt_us;
2007 msk->scaling_ratio = scaling_ratio;
2008 if (time < (rtt_us >> 3) || rtt_us == 0)
2009 return;
2010
2011 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
2012 goto new_measure;
2013
2014 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
2015 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
2016 u64 rcvwin, grow;
2017 int rcvbuf;
2018
2019 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
2020
2021 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
2022
2023 do_div(grow, msk->rcvq_space.space);
2024 rcvwin += (grow << 1);
2025
2026 rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin),
2027 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
2028
2029 if (rcvbuf > sk->sk_rcvbuf) {
2030 u32 window_clamp;
2031
2032 window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf);
2033 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
2034
2035 /* Make subflows follow along. If we do not do this, we
2036 * get drops at subflow level if skbs can't be moved to
2037 * the mptcp rx queue fast enough (announced rcv_win can
2038 * exceed ssk->sk_rcvbuf).
2039 */
2040 mptcp_for_each_subflow(msk, subflow) {
2041 struct sock *ssk;
2042 bool slow;
2043
2044 ssk = mptcp_subflow_tcp_sock(subflow);
2045 slow = lock_sock_fast(ssk);
2046 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
2047 WRITE_ONCE(tcp_sk(ssk)->window_clamp, window_clamp);
2048 if (tcp_can_send_ack(ssk))
2049 tcp_cleanup_rbuf(ssk, 1);
2050 unlock_sock_fast(ssk, slow);
2051 }
2052 }
2053 }
2054
2055 msk->rcvq_space.space = msk->rcvq_space.copied;
2056 new_measure:
2057 msk->rcvq_space.copied = 0;
2058 msk->rcvq_space.time = mstamp;
2059 }
2060
__mptcp_update_rmem(struct sock * sk)2061 static void __mptcp_update_rmem(struct sock *sk)
2062 {
2063 struct mptcp_sock *msk = mptcp_sk(sk);
2064
2065 if (!msk->rmem_released)
2066 return;
2067
2068 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2069 mptcp_rmem_uncharge(sk, msk->rmem_released);
2070 WRITE_ONCE(msk->rmem_released, 0);
2071 }
2072
__mptcp_splice_receive_queue(struct sock * sk)2073 static void __mptcp_splice_receive_queue(struct sock *sk)
2074 {
2075 struct mptcp_sock *msk = mptcp_sk(sk);
2076
2077 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2078 }
2079
__mptcp_move_skbs(struct mptcp_sock * msk)2080 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2081 {
2082 struct sock *sk = (struct sock *)msk;
2083 unsigned int moved = 0;
2084 bool ret, done;
2085
2086 do {
2087 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2088 bool slowpath;
2089
2090 /* we can have data pending in the subflows only if the msk
2091 * receive buffer was full at subflow_data_ready() time,
2092 * that is an unlikely slow path.
2093 */
2094 if (likely(!ssk))
2095 break;
2096
2097 slowpath = lock_sock_fast(ssk);
2098 mptcp_data_lock(sk);
2099 __mptcp_update_rmem(sk);
2100 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2101 mptcp_data_unlock(sk);
2102
2103 if (unlikely(ssk->sk_err))
2104 __mptcp_error_report(sk);
2105 unlock_sock_fast(ssk, slowpath);
2106 } while (!done);
2107
2108 /* acquire the data lock only if some input data is pending */
2109 ret = moved > 0;
2110 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2111 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2112 mptcp_data_lock(sk);
2113 __mptcp_update_rmem(sk);
2114 ret |= __mptcp_ofo_queue(msk);
2115 __mptcp_splice_receive_queue(sk);
2116 mptcp_data_unlock(sk);
2117 }
2118 if (ret)
2119 mptcp_check_data_fin((struct sock *)msk);
2120 return !skb_queue_empty(&msk->receive_queue);
2121 }
2122
mptcp_inq_hint(const struct sock * sk)2123 static unsigned int mptcp_inq_hint(const struct sock *sk)
2124 {
2125 const struct mptcp_sock *msk = mptcp_sk(sk);
2126 const struct sk_buff *skb;
2127
2128 skb = skb_peek(&msk->receive_queue);
2129 if (skb) {
2130 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2131
2132 if (hint_val >= INT_MAX)
2133 return INT_MAX;
2134
2135 return (unsigned int)hint_val;
2136 }
2137
2138 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2139 return 1;
2140
2141 return 0;
2142 }
2143
mptcp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)2144 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2145 int flags, int *addr_len)
2146 {
2147 struct mptcp_sock *msk = mptcp_sk(sk);
2148 struct scm_timestamping_internal tss;
2149 int copied = 0, cmsg_flags = 0;
2150 int target;
2151 long timeo;
2152
2153 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2154 if (unlikely(flags & MSG_ERRQUEUE))
2155 return inet_recv_error(sk, msg, len, addr_len);
2156
2157 lock_sock(sk);
2158 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2159 copied = -ENOTCONN;
2160 goto out_err;
2161 }
2162
2163 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2164
2165 len = min_t(size_t, len, INT_MAX);
2166 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2167
2168 if (unlikely(msk->recvmsg_inq))
2169 cmsg_flags = MPTCP_CMSG_INQ;
2170
2171 while (copied < len) {
2172 int err, bytes_read;
2173
2174 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2175 if (unlikely(bytes_read < 0)) {
2176 if (!copied)
2177 copied = bytes_read;
2178 goto out_err;
2179 }
2180
2181 copied += bytes_read;
2182
2183 /* be sure to advertise window change */
2184 mptcp_cleanup_rbuf(msk);
2185
2186 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2187 continue;
2188
2189 /* only the master socket status is relevant here. The exit
2190 * conditions mirror closely tcp_recvmsg()
2191 */
2192 if (copied >= target)
2193 break;
2194
2195 if (copied) {
2196 if (sk->sk_err ||
2197 sk->sk_state == TCP_CLOSE ||
2198 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2199 !timeo ||
2200 signal_pending(current))
2201 break;
2202 } else {
2203 if (sk->sk_err) {
2204 copied = sock_error(sk);
2205 break;
2206 }
2207
2208 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2209 /* race breaker: the shutdown could be after the
2210 * previous receive queue check
2211 */
2212 if (__mptcp_move_skbs(msk))
2213 continue;
2214 break;
2215 }
2216
2217 if (sk->sk_state == TCP_CLOSE) {
2218 copied = -ENOTCONN;
2219 break;
2220 }
2221
2222 if (!timeo) {
2223 copied = -EAGAIN;
2224 break;
2225 }
2226
2227 if (signal_pending(current)) {
2228 copied = sock_intr_errno(timeo);
2229 break;
2230 }
2231 }
2232
2233 pr_debug("block timeout %ld\n", timeo);
2234 mptcp_rcv_space_adjust(msk, copied);
2235 err = sk_wait_data(sk, &timeo, NULL);
2236 if (err < 0) {
2237 err = copied ? : err;
2238 goto out_err;
2239 }
2240 }
2241
2242 mptcp_rcv_space_adjust(msk, copied);
2243
2244 out_err:
2245 if (cmsg_flags && copied >= 0) {
2246 if (cmsg_flags & MPTCP_CMSG_TS)
2247 tcp_recv_timestamp(msg, sk, &tss);
2248
2249 if (cmsg_flags & MPTCP_CMSG_INQ) {
2250 unsigned int inq = mptcp_inq_hint(sk);
2251
2252 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2253 }
2254 }
2255
2256 pr_debug("msk=%p rx queue empty=%d:%d copied=%d\n",
2257 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2258 skb_queue_empty(&msk->receive_queue), copied);
2259
2260 release_sock(sk);
2261 return copied;
2262 }
2263
mptcp_retransmit_timer(struct timer_list * t)2264 static void mptcp_retransmit_timer(struct timer_list *t)
2265 {
2266 struct inet_connection_sock *icsk = from_timer(icsk, t,
2267 icsk_retransmit_timer);
2268 struct sock *sk = &icsk->icsk_inet.sk;
2269 struct mptcp_sock *msk = mptcp_sk(sk);
2270
2271 bh_lock_sock(sk);
2272 if (!sock_owned_by_user(sk)) {
2273 /* we need a process context to retransmit */
2274 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2275 mptcp_schedule_work(sk);
2276 } else {
2277 /* delegate our work to tcp_release_cb() */
2278 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2279 }
2280 bh_unlock_sock(sk);
2281 sock_put(sk);
2282 }
2283
mptcp_tout_timer(struct timer_list * t)2284 static void mptcp_tout_timer(struct timer_list *t)
2285 {
2286 struct sock *sk = from_timer(sk, t, sk_timer);
2287
2288 mptcp_schedule_work(sk);
2289 sock_put(sk);
2290 }
2291
2292 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2293 * level.
2294 *
2295 * A backup subflow is returned only if that is the only kind available.
2296 */
mptcp_subflow_get_retrans(struct mptcp_sock * msk)2297 struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2298 {
2299 struct sock *backup = NULL, *pick = NULL;
2300 struct mptcp_subflow_context *subflow;
2301 int min_stale_count = INT_MAX;
2302
2303 mptcp_for_each_subflow(msk, subflow) {
2304 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2305
2306 if (!__mptcp_subflow_active(subflow))
2307 continue;
2308
2309 /* still data outstanding at TCP level? skip this */
2310 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2311 mptcp_pm_subflow_chk_stale(msk, ssk);
2312 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2313 continue;
2314 }
2315
2316 if (subflow->backup || subflow->request_bkup) {
2317 if (!backup)
2318 backup = ssk;
2319 continue;
2320 }
2321
2322 if (!pick)
2323 pick = ssk;
2324 }
2325
2326 if (pick)
2327 return pick;
2328
2329 /* use backup only if there are no progresses anywhere */
2330 return min_stale_count > 1 ? backup : NULL;
2331 }
2332
__mptcp_retransmit_pending_data(struct sock * sk)2333 bool __mptcp_retransmit_pending_data(struct sock *sk)
2334 {
2335 struct mptcp_data_frag *cur, *rtx_head;
2336 struct mptcp_sock *msk = mptcp_sk(sk);
2337
2338 if (__mptcp_check_fallback(msk))
2339 return false;
2340
2341 /* the closing socket has some data untransmitted and/or unacked:
2342 * some data in the mptcp rtx queue has not really xmitted yet.
2343 * keep it simple and re-inject the whole mptcp level rtx queue
2344 */
2345 mptcp_data_lock(sk);
2346 __mptcp_clean_una_wakeup(sk);
2347 rtx_head = mptcp_rtx_head(sk);
2348 if (!rtx_head) {
2349 mptcp_data_unlock(sk);
2350 return false;
2351 }
2352
2353 msk->recovery_snd_nxt = msk->snd_nxt;
2354 msk->recovery = true;
2355 mptcp_data_unlock(sk);
2356
2357 msk->first_pending = rtx_head;
2358 msk->snd_burst = 0;
2359
2360 /* be sure to clear the "sent status" on all re-injected fragments */
2361 list_for_each_entry(cur, &msk->rtx_queue, list) {
2362 if (!cur->already_sent)
2363 break;
2364 cur->already_sent = 0;
2365 }
2366
2367 return true;
2368 }
2369
2370 /* flags for __mptcp_close_ssk() */
2371 #define MPTCP_CF_PUSH BIT(1)
2372 #define MPTCP_CF_FASTCLOSE BIT(2)
2373
2374 /* be sure to send a reset only if the caller asked for it, also
2375 * clean completely the subflow status when the subflow reaches
2376 * TCP_CLOSE state
2377 */
__mptcp_subflow_disconnect(struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2378 static void __mptcp_subflow_disconnect(struct sock *ssk,
2379 struct mptcp_subflow_context *subflow,
2380 unsigned int flags)
2381 {
2382 if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
2383 (flags & MPTCP_CF_FASTCLOSE)) {
2384 /* The MPTCP code never wait on the subflow sockets, TCP-level
2385 * disconnect should never fail
2386 */
2387 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2388 mptcp_subflow_ctx_reset(subflow);
2389 } else {
2390 tcp_shutdown(ssk, SEND_SHUTDOWN);
2391 }
2392 }
2393
2394 /* subflow sockets can be either outgoing (connect) or incoming
2395 * (accept).
2396 *
2397 * Outgoing subflows use in-kernel sockets.
2398 * Incoming subflows do not have their own 'struct socket' allocated,
2399 * so we need to use tcp_close() after detaching them from the mptcp
2400 * parent socket.
2401 */
__mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2402 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2403 struct mptcp_subflow_context *subflow,
2404 unsigned int flags)
2405 {
2406 struct mptcp_sock *msk = mptcp_sk(sk);
2407 bool dispose_it, need_push = false;
2408
2409 /* If the first subflow moved to a close state before accept, e.g. due
2410 * to an incoming reset or listener shutdown, the subflow socket is
2411 * already deleted by inet_child_forget() and the mptcp socket can't
2412 * survive too.
2413 */
2414 if (msk->in_accept_queue && msk->first == ssk &&
2415 (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
2416 /* ensure later check in mptcp_worker() will dispose the msk */
2417 mptcp_set_close_tout(sk, tcp_jiffies32 - (TCP_TIMEWAIT_LEN + 1));
2418 sock_set_flag(sk, SOCK_DEAD);
2419 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2420 mptcp_subflow_drop_ctx(ssk);
2421 goto out_release;
2422 }
2423
2424 dispose_it = msk->free_first || ssk != msk->first;
2425 if (dispose_it)
2426 list_del(&subflow->node);
2427
2428 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2429
2430 if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2431 /* be sure to force the tcp_close path
2432 * to generate the egress reset
2433 */
2434 ssk->sk_lingertime = 0;
2435 sock_set_flag(ssk, SOCK_LINGER);
2436 subflow->send_fastclose = 1;
2437 }
2438
2439 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2440 if (!dispose_it) {
2441 __mptcp_subflow_disconnect(ssk, subflow, flags);
2442 release_sock(ssk);
2443
2444 goto out;
2445 }
2446
2447 subflow->disposable = 1;
2448
2449 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2450 * the ssk has been already destroyed, we just need to release the
2451 * reference owned by msk;
2452 */
2453 if (!inet_csk(ssk)->icsk_ulp_ops) {
2454 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2455 kfree_rcu(subflow, rcu);
2456 } else {
2457 /* otherwise tcp will dispose of the ssk and subflow ctx */
2458 __tcp_close(ssk, 0);
2459
2460 /* close acquired an extra ref */
2461 __sock_put(ssk);
2462 }
2463
2464 out_release:
2465 __mptcp_subflow_error_report(sk, ssk);
2466 release_sock(ssk);
2467
2468 sock_put(ssk);
2469
2470 if (ssk == msk->first)
2471 WRITE_ONCE(msk->first, NULL);
2472
2473 out:
2474 __mptcp_sync_sndbuf(sk);
2475 if (need_push)
2476 __mptcp_push_pending(sk, 0);
2477
2478 /* Catch every 'all subflows closed' scenario, including peers silently
2479 * closing them, e.g. due to timeout.
2480 * For established sockets, allow an additional timeout before closing,
2481 * as the protocol can still create more subflows.
2482 */
2483 if (list_is_singular(&msk->conn_list) && msk->first &&
2484 inet_sk_state_load(msk->first) == TCP_CLOSE) {
2485 if (sk->sk_state != TCP_ESTABLISHED ||
2486 msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2487 mptcp_set_state(sk, TCP_CLOSE);
2488 mptcp_close_wake_up(sk);
2489 } else {
2490 mptcp_start_tout_timer(sk);
2491 }
2492 }
2493 }
2494
mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow)2495 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2496 struct mptcp_subflow_context *subflow)
2497 {
2498 /* The first subflow can already be closed and still in the list */
2499 if (subflow->close_event_done)
2500 return;
2501
2502 subflow->close_event_done = true;
2503
2504 if (sk->sk_state == TCP_ESTABLISHED)
2505 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2506
2507 /* subflow aborted before reaching the fully_established status
2508 * attempt the creation of the next subflow
2509 */
2510 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2511
2512 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2513 }
2514
mptcp_sync_mss(struct sock * sk,u32 pmtu)2515 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2516 {
2517 return 0;
2518 }
2519
__mptcp_close_subflow(struct sock * sk)2520 static void __mptcp_close_subflow(struct sock *sk)
2521 {
2522 struct mptcp_subflow_context *subflow, *tmp;
2523 struct mptcp_sock *msk = mptcp_sk(sk);
2524
2525 might_sleep();
2526
2527 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2528 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2529 int ssk_state = inet_sk_state_load(ssk);
2530
2531 if (ssk_state != TCP_CLOSE &&
2532 (ssk_state != TCP_CLOSE_WAIT ||
2533 inet_sk_state_load(sk) != TCP_ESTABLISHED))
2534 continue;
2535
2536 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2537 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2538 continue;
2539
2540 mptcp_close_ssk(sk, ssk, subflow);
2541 }
2542
2543 }
2544
mptcp_close_tout_expired(const struct sock * sk)2545 static bool mptcp_close_tout_expired(const struct sock *sk)
2546 {
2547 if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2548 sk->sk_state == TCP_CLOSE)
2549 return false;
2550
2551 return time_after32(tcp_jiffies32,
2552 inet_csk(sk)->icsk_mtup.probe_timestamp + TCP_TIMEWAIT_LEN);
2553 }
2554
mptcp_check_fastclose(struct mptcp_sock * msk)2555 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2556 {
2557 struct mptcp_subflow_context *subflow, *tmp;
2558 struct sock *sk = (struct sock *)msk;
2559
2560 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2561 return;
2562
2563 mptcp_token_destroy(msk);
2564
2565 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2566 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2567 bool slow;
2568
2569 slow = lock_sock_fast(tcp_sk);
2570 if (tcp_sk->sk_state != TCP_CLOSE) {
2571 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2572 tcp_set_state(tcp_sk, TCP_CLOSE);
2573 }
2574 unlock_sock_fast(tcp_sk, slow);
2575 }
2576
2577 /* Mirror the tcp_reset() error propagation */
2578 switch (sk->sk_state) {
2579 case TCP_SYN_SENT:
2580 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2581 break;
2582 case TCP_CLOSE_WAIT:
2583 WRITE_ONCE(sk->sk_err, EPIPE);
2584 break;
2585 case TCP_CLOSE:
2586 return;
2587 default:
2588 WRITE_ONCE(sk->sk_err, ECONNRESET);
2589 }
2590
2591 mptcp_set_state(sk, TCP_CLOSE);
2592 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2593 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2594 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2595
2596 /* the calling mptcp_worker will properly destroy the socket */
2597 if (sock_flag(sk, SOCK_DEAD))
2598 return;
2599
2600 sk->sk_state_change(sk);
2601 sk_error_report(sk);
2602 }
2603
__mptcp_retrans(struct sock * sk)2604 static void __mptcp_retrans(struct sock *sk)
2605 {
2606 struct mptcp_sock *msk = mptcp_sk(sk);
2607 struct mptcp_subflow_context *subflow;
2608 struct mptcp_sendmsg_info info = {};
2609 struct mptcp_data_frag *dfrag;
2610 struct sock *ssk;
2611 int ret, err;
2612 u16 len = 0;
2613
2614 mptcp_clean_una_wakeup(sk);
2615
2616 /* first check ssk: need to kick "stale" logic */
2617 err = mptcp_sched_get_retrans(msk);
2618 dfrag = mptcp_rtx_head(sk);
2619 if (!dfrag) {
2620 if (mptcp_data_fin_enabled(msk)) {
2621 struct inet_connection_sock *icsk = inet_csk(sk);
2622
2623 icsk->icsk_retransmits++;
2624 mptcp_set_datafin_timeout(sk);
2625 mptcp_send_ack(msk);
2626
2627 goto reset_timer;
2628 }
2629
2630 if (!mptcp_send_head(sk))
2631 return;
2632
2633 goto reset_timer;
2634 }
2635
2636 if (err)
2637 goto reset_timer;
2638
2639 mptcp_for_each_subflow(msk, subflow) {
2640 if (READ_ONCE(subflow->scheduled)) {
2641 u16 copied = 0;
2642
2643 mptcp_subflow_set_scheduled(subflow, false);
2644
2645 ssk = mptcp_subflow_tcp_sock(subflow);
2646
2647 lock_sock(ssk);
2648
2649 /* limit retransmission to the bytes already sent on some subflows */
2650 info.sent = 0;
2651 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2652 dfrag->already_sent;
2653 while (info.sent < info.limit) {
2654 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2655 if (ret <= 0)
2656 break;
2657
2658 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2659 copied += ret;
2660 info.sent += ret;
2661 }
2662 if (copied) {
2663 len = max(copied, len);
2664 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2665 info.size_goal);
2666 WRITE_ONCE(msk->allow_infinite_fallback, false);
2667 }
2668
2669 release_sock(ssk);
2670 }
2671 }
2672
2673 msk->bytes_retrans += len;
2674 dfrag->already_sent = max(dfrag->already_sent, len);
2675
2676 reset_timer:
2677 mptcp_check_and_set_pending(sk);
2678
2679 if (!mptcp_rtx_timer_pending(sk))
2680 mptcp_reset_rtx_timer(sk);
2681 }
2682
2683 /* schedule the timeout timer for the relevant event: either close timeout
2684 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2685 */
mptcp_reset_tout_timer(struct mptcp_sock * msk,unsigned long fail_tout)2686 void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2687 {
2688 struct sock *sk = (struct sock *)msk;
2689 unsigned long timeout, close_timeout;
2690
2691 if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2692 return;
2693
2694 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies +
2695 TCP_TIMEWAIT_LEN;
2696
2697 /* the close timeout takes precedence on the fail one, and here at least one of
2698 * them is active
2699 */
2700 timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2701
2702 sk_reset_timer(sk, &sk->sk_timer, timeout);
2703 }
2704
mptcp_mp_fail_no_response(struct mptcp_sock * msk)2705 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2706 {
2707 struct sock *ssk = msk->first;
2708 bool slow;
2709
2710 if (!ssk)
2711 return;
2712
2713 pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
2714
2715 slow = lock_sock_fast(ssk);
2716 mptcp_subflow_reset(ssk);
2717 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2718 unlock_sock_fast(ssk, slow);
2719 }
2720
mptcp_do_fastclose(struct sock * sk)2721 static void mptcp_do_fastclose(struct sock *sk)
2722 {
2723 struct mptcp_subflow_context *subflow, *tmp;
2724 struct mptcp_sock *msk = mptcp_sk(sk);
2725
2726 mptcp_set_state(sk, TCP_CLOSE);
2727 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2728 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2729 subflow, MPTCP_CF_FASTCLOSE);
2730 }
2731
mptcp_worker(struct work_struct * work)2732 static void mptcp_worker(struct work_struct *work)
2733 {
2734 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2735 struct sock *sk = (struct sock *)msk;
2736 unsigned long fail_tout;
2737 int state;
2738
2739 lock_sock(sk);
2740 state = sk->sk_state;
2741 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2742 goto unlock;
2743
2744 mptcp_check_fastclose(msk);
2745
2746 mptcp_pm_nl_work(msk);
2747
2748 mptcp_check_send_data_fin(sk);
2749 mptcp_check_data_fin_ack(sk);
2750 mptcp_check_data_fin(sk);
2751
2752 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2753 __mptcp_close_subflow(sk);
2754
2755 if (mptcp_close_tout_expired(sk)) {
2756 mptcp_do_fastclose(sk);
2757 mptcp_close_wake_up(sk);
2758 }
2759
2760 if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2761 __mptcp_destroy_sock(sk);
2762 goto unlock;
2763 }
2764
2765 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2766 __mptcp_retrans(sk);
2767
2768 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2769 if (fail_tout && time_after(jiffies, fail_tout))
2770 mptcp_mp_fail_no_response(msk);
2771
2772 unlock:
2773 release_sock(sk);
2774 sock_put(sk);
2775 }
2776
__mptcp_init_sock(struct sock * sk)2777 static void __mptcp_init_sock(struct sock *sk)
2778 {
2779 struct mptcp_sock *msk = mptcp_sk(sk);
2780
2781 INIT_LIST_HEAD(&msk->conn_list);
2782 INIT_LIST_HEAD(&msk->join_list);
2783 INIT_LIST_HEAD(&msk->rtx_queue);
2784 INIT_WORK(&msk->work, mptcp_worker);
2785 __skb_queue_head_init(&msk->receive_queue);
2786 msk->out_of_order_queue = RB_ROOT;
2787 msk->first_pending = NULL;
2788 msk->rmem_fwd_alloc = 0;
2789 WRITE_ONCE(msk->rmem_released, 0);
2790 msk->timer_ival = TCP_RTO_MIN;
2791 msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2792
2793 WRITE_ONCE(msk->first, NULL);
2794 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2795 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2796 WRITE_ONCE(msk->allow_infinite_fallback, true);
2797 msk->recovery = false;
2798 msk->subflow_id = 1;
2799
2800 mptcp_pm_data_init(msk);
2801
2802 /* re-use the csk retrans timer for MPTCP-level retrans */
2803 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2804 timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2805 }
2806
mptcp_ca_reset(struct sock * sk)2807 static void mptcp_ca_reset(struct sock *sk)
2808 {
2809 struct inet_connection_sock *icsk = inet_csk(sk);
2810
2811 tcp_assign_congestion_control(sk);
2812 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2813
2814 /* no need to keep a reference to the ops, the name will suffice */
2815 tcp_cleanup_congestion_control(sk);
2816 icsk->icsk_ca_ops = NULL;
2817 }
2818
mptcp_init_sock(struct sock * sk)2819 static int mptcp_init_sock(struct sock *sk)
2820 {
2821 struct net *net = sock_net(sk);
2822 int ret;
2823
2824 __mptcp_init_sock(sk);
2825
2826 if (!mptcp_is_enabled(net))
2827 return -ENOPROTOOPT;
2828
2829 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2830 return -ENOMEM;
2831
2832 rcu_read_lock();
2833 ret = mptcp_init_sched(mptcp_sk(sk),
2834 mptcp_sched_find(mptcp_get_scheduler(net)));
2835 rcu_read_unlock();
2836 if (ret)
2837 return ret;
2838
2839 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2840
2841 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2842 * propagate the correct value
2843 */
2844 mptcp_ca_reset(sk);
2845
2846 sk_sockets_allocated_inc(sk);
2847 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2848 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2849
2850 return 0;
2851 }
2852
__mptcp_clear_xmit(struct sock * sk)2853 static void __mptcp_clear_xmit(struct sock *sk)
2854 {
2855 struct mptcp_sock *msk = mptcp_sk(sk);
2856 struct mptcp_data_frag *dtmp, *dfrag;
2857
2858 WRITE_ONCE(msk->first_pending, NULL);
2859 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2860 dfrag_clear(sk, dfrag);
2861 }
2862
mptcp_cancel_work(struct sock * sk)2863 void mptcp_cancel_work(struct sock *sk)
2864 {
2865 struct mptcp_sock *msk = mptcp_sk(sk);
2866
2867 if (cancel_work_sync(&msk->work))
2868 __sock_put(sk);
2869 }
2870
mptcp_subflow_shutdown(struct sock * sk,struct sock * ssk,int how)2871 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2872 {
2873 lock_sock(ssk);
2874
2875 switch (ssk->sk_state) {
2876 case TCP_LISTEN:
2877 if (!(how & RCV_SHUTDOWN))
2878 break;
2879 fallthrough;
2880 case TCP_SYN_SENT:
2881 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2882 break;
2883 default:
2884 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2885 pr_debug("Fallback\n");
2886 ssk->sk_shutdown |= how;
2887 tcp_shutdown(ssk, how);
2888
2889 /* simulate the data_fin ack reception to let the state
2890 * machine move forward
2891 */
2892 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2893 mptcp_schedule_work(sk);
2894 } else {
2895 pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
2896 tcp_send_ack(ssk);
2897 if (!mptcp_rtx_timer_pending(sk))
2898 mptcp_reset_rtx_timer(sk);
2899 }
2900 break;
2901 }
2902
2903 release_sock(ssk);
2904 }
2905
mptcp_set_state(struct sock * sk,int state)2906 void mptcp_set_state(struct sock *sk, int state)
2907 {
2908 int oldstate = sk->sk_state;
2909
2910 switch (state) {
2911 case TCP_ESTABLISHED:
2912 if (oldstate != TCP_ESTABLISHED)
2913 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2914 break;
2915 case TCP_CLOSE_WAIT:
2916 /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
2917 * MPTCP "accepted" sockets will be created later on. So no
2918 * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
2919 */
2920 break;
2921 default:
2922 if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
2923 MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2924 }
2925
2926 inet_sk_state_store(sk, state);
2927 }
2928
2929 static const unsigned char new_state[16] = {
2930 /* current state: new state: action: */
2931 [0 /* (Invalid) */] = TCP_CLOSE,
2932 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2933 [TCP_SYN_SENT] = TCP_CLOSE,
2934 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2935 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2936 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2937 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2938 [TCP_CLOSE] = TCP_CLOSE,
2939 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2940 [TCP_LAST_ACK] = TCP_LAST_ACK,
2941 [TCP_LISTEN] = TCP_CLOSE,
2942 [TCP_CLOSING] = TCP_CLOSING,
2943 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2944 };
2945
mptcp_close_state(struct sock * sk)2946 static int mptcp_close_state(struct sock *sk)
2947 {
2948 int next = (int)new_state[sk->sk_state];
2949 int ns = next & TCP_STATE_MASK;
2950
2951 mptcp_set_state(sk, ns);
2952
2953 return next & TCP_ACTION_FIN;
2954 }
2955
mptcp_check_send_data_fin(struct sock * sk)2956 static void mptcp_check_send_data_fin(struct sock *sk)
2957 {
2958 struct mptcp_subflow_context *subflow;
2959 struct mptcp_sock *msk = mptcp_sk(sk);
2960
2961 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
2962 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2963 msk->snd_nxt, msk->write_seq);
2964
2965 /* we still need to enqueue subflows or not really shutting down,
2966 * skip this
2967 */
2968 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2969 mptcp_send_head(sk))
2970 return;
2971
2972 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2973
2974 mptcp_for_each_subflow(msk, subflow) {
2975 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2976
2977 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2978 }
2979 }
2980
__mptcp_wr_shutdown(struct sock * sk)2981 static void __mptcp_wr_shutdown(struct sock *sk)
2982 {
2983 struct mptcp_sock *msk = mptcp_sk(sk);
2984
2985 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
2986 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2987 !!mptcp_send_head(sk));
2988
2989 /* will be ignored by fallback sockets */
2990 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2991 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2992
2993 mptcp_check_send_data_fin(sk);
2994 }
2995
__mptcp_destroy_sock(struct sock * sk)2996 static void __mptcp_destroy_sock(struct sock *sk)
2997 {
2998 struct mptcp_sock *msk = mptcp_sk(sk);
2999
3000 pr_debug("msk=%p\n", msk);
3001
3002 might_sleep();
3003
3004 mptcp_stop_rtx_timer(sk);
3005 sk_stop_timer(sk, &sk->sk_timer);
3006 msk->pm.status = 0;
3007 mptcp_release_sched(msk);
3008
3009 sk->sk_prot->destroy(sk);
3010
3011 WARN_ON_ONCE(msk->rmem_fwd_alloc);
3012 WARN_ON_ONCE(msk->rmem_released);
3013 sk_stream_kill_queues(sk);
3014 xfrm_sk_free_policy(sk);
3015
3016 sock_put(sk);
3017 }
3018
__mptcp_unaccepted_force_close(struct sock * sk)3019 void __mptcp_unaccepted_force_close(struct sock *sk)
3020 {
3021 sock_set_flag(sk, SOCK_DEAD);
3022 mptcp_do_fastclose(sk);
3023 __mptcp_destroy_sock(sk);
3024 }
3025
mptcp_check_readable(struct sock * sk)3026 static __poll_t mptcp_check_readable(struct sock *sk)
3027 {
3028 return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3029 }
3030
mptcp_check_listen_stop(struct sock * sk)3031 static void mptcp_check_listen_stop(struct sock *sk)
3032 {
3033 struct sock *ssk;
3034
3035 if (inet_sk_state_load(sk) != TCP_LISTEN)
3036 return;
3037
3038 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3039 ssk = mptcp_sk(sk)->first;
3040 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3041 return;
3042
3043 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3044 tcp_set_state(ssk, TCP_CLOSE);
3045 mptcp_subflow_queue_clean(sk, ssk);
3046 inet_csk_listen_stop(ssk);
3047 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
3048 release_sock(ssk);
3049 }
3050
__mptcp_close(struct sock * sk,long timeout)3051 bool __mptcp_close(struct sock *sk, long timeout)
3052 {
3053 struct mptcp_subflow_context *subflow;
3054 struct mptcp_sock *msk = mptcp_sk(sk);
3055 bool do_cancel_work = false;
3056 int subflows_alive = 0;
3057
3058 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3059
3060 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3061 mptcp_check_listen_stop(sk);
3062 mptcp_set_state(sk, TCP_CLOSE);
3063 goto cleanup;
3064 }
3065
3066 if (mptcp_data_avail(msk) || timeout < 0) {
3067 /* If the msk has read data, or the caller explicitly ask it,
3068 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3069 */
3070 mptcp_do_fastclose(sk);
3071 timeout = 0;
3072 } else if (mptcp_close_state(sk)) {
3073 __mptcp_wr_shutdown(sk);
3074 }
3075
3076 sk_stream_wait_close(sk, timeout);
3077
3078 cleanup:
3079 /* orphan all the subflows */
3080 mptcp_for_each_subflow(msk, subflow) {
3081 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3082 bool slow = lock_sock_fast_nested(ssk);
3083
3084 subflows_alive += ssk->sk_state != TCP_CLOSE;
3085
3086 /* since the close timeout takes precedence on the fail one,
3087 * cancel the latter
3088 */
3089 if (ssk == msk->first)
3090 subflow->fail_tout = 0;
3091
3092 /* detach from the parent socket, but allow data_ready to
3093 * push incoming data into the mptcp stack, to properly ack it
3094 */
3095 ssk->sk_socket = NULL;
3096 ssk->sk_wq = NULL;
3097 unlock_sock_fast(ssk, slow);
3098 }
3099 sock_orphan(sk);
3100
3101 /* all the subflows are closed, only timeout can change the msk
3102 * state, let's not keep resources busy for no reasons
3103 */
3104 if (subflows_alive == 0)
3105 mptcp_set_state(sk, TCP_CLOSE);
3106
3107 sock_hold(sk);
3108 pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3109 if (msk->token)
3110 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3111
3112 if (sk->sk_state == TCP_CLOSE) {
3113 __mptcp_destroy_sock(sk);
3114 do_cancel_work = true;
3115 } else {
3116 mptcp_start_tout_timer(sk);
3117 }
3118
3119 return do_cancel_work;
3120 }
3121
mptcp_close(struct sock * sk,long timeout)3122 static void mptcp_close(struct sock *sk, long timeout)
3123 {
3124 bool do_cancel_work;
3125
3126 lock_sock(sk);
3127
3128 do_cancel_work = __mptcp_close(sk, timeout);
3129 release_sock(sk);
3130 if (do_cancel_work)
3131 mptcp_cancel_work(sk);
3132
3133 sock_put(sk);
3134 }
3135
mptcp_copy_inaddrs(struct sock * msk,const struct sock * ssk)3136 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3137 {
3138 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3139 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3140 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3141
3142 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3143 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3144
3145 if (msk6 && ssk6) {
3146 msk6->saddr = ssk6->saddr;
3147 msk6->flow_label = ssk6->flow_label;
3148 }
3149 #endif
3150
3151 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3152 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3153 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3154 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3155 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3156 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3157 }
3158
mptcp_disconnect(struct sock * sk,int flags)3159 static int mptcp_disconnect(struct sock *sk, int flags)
3160 {
3161 struct mptcp_sock *msk = mptcp_sk(sk);
3162
3163 /* We are on the fastopen error path. We can't call straight into the
3164 * subflows cleanup code due to lock nesting (we are already under
3165 * msk->firstsocket lock).
3166 */
3167 if (msk->fastopening)
3168 return -EBUSY;
3169
3170 mptcp_check_listen_stop(sk);
3171 mptcp_set_state(sk, TCP_CLOSE);
3172
3173 mptcp_stop_rtx_timer(sk);
3174 mptcp_stop_tout_timer(sk);
3175
3176 if (msk->token)
3177 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3178
3179 /* msk->subflow is still intact, the following will not free the first
3180 * subflow
3181 */
3182 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3183 WRITE_ONCE(msk->flags, 0);
3184 msk->cb_flags = 0;
3185 msk->recovery = false;
3186 msk->can_ack = false;
3187 msk->fully_established = false;
3188 msk->rcv_data_fin = false;
3189 msk->snd_data_fin_enable = false;
3190 msk->rcv_fastclose = false;
3191 msk->use_64bit_ack = false;
3192 msk->bytes_consumed = 0;
3193 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3194 mptcp_pm_data_reset(msk);
3195 mptcp_ca_reset(sk);
3196 msk->bytes_acked = 0;
3197 msk->bytes_received = 0;
3198 msk->bytes_sent = 0;
3199 msk->bytes_retrans = 0;
3200 msk->rcvspace_init = 0;
3201
3202 WRITE_ONCE(sk->sk_shutdown, 0);
3203 sk_error_report(sk);
3204 return 0;
3205 }
3206
3207 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcp_inet6_sk(const struct sock * sk)3208 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3209 {
3210 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3211
3212 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3213 }
3214
mptcp_copy_ip6_options(struct sock * newsk,const struct sock * sk)3215 static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3216 {
3217 const struct ipv6_pinfo *np = inet6_sk(sk);
3218 struct ipv6_txoptions *opt;
3219 struct ipv6_pinfo *newnp;
3220
3221 newnp = inet6_sk(newsk);
3222
3223 rcu_read_lock();
3224 opt = rcu_dereference(np->opt);
3225 if (opt) {
3226 opt = ipv6_dup_options(newsk, opt);
3227 if (!opt)
3228 net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3229 }
3230 RCU_INIT_POINTER(newnp->opt, opt);
3231 rcu_read_unlock();
3232 }
3233 #endif
3234
mptcp_copy_ip_options(struct sock * newsk,const struct sock * sk)3235 static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3236 {
3237 struct ip_options_rcu *inet_opt, *newopt = NULL;
3238 const struct inet_sock *inet = inet_sk(sk);
3239 struct inet_sock *newinet;
3240
3241 newinet = inet_sk(newsk);
3242
3243 rcu_read_lock();
3244 inet_opt = rcu_dereference(inet->inet_opt);
3245 if (inet_opt) {
3246 newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
3247 inet_opt->opt.optlen, GFP_ATOMIC);
3248 if (newopt)
3249 memcpy(newopt, inet_opt, sizeof(*inet_opt) +
3250 inet_opt->opt.optlen);
3251 else
3252 net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3253 }
3254 RCU_INIT_POINTER(newinet->inet_opt, newopt);
3255 rcu_read_unlock();
3256 }
3257
mptcp_sk_clone_init(const struct sock * sk,const struct mptcp_options_received * mp_opt,struct sock * ssk,struct request_sock * req)3258 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3259 const struct mptcp_options_received *mp_opt,
3260 struct sock *ssk,
3261 struct request_sock *req)
3262 {
3263 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3264 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3265 struct mptcp_subflow_context *subflow;
3266 struct mptcp_sock *msk;
3267
3268 if (!nsk)
3269 return NULL;
3270
3271 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3272 if (nsk->sk_family == AF_INET6)
3273 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3274 #endif
3275
3276 __mptcp_init_sock(nsk);
3277
3278 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3279 if (nsk->sk_family == AF_INET6)
3280 mptcp_copy_ip6_options(nsk, sk);
3281 else
3282 #endif
3283 mptcp_copy_ip_options(nsk, sk);
3284
3285 msk = mptcp_sk(nsk);
3286 msk->local_key = subflow_req->local_key;
3287 msk->token = subflow_req->token;
3288 msk->in_accept_queue = 1;
3289 WRITE_ONCE(msk->fully_established, false);
3290 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3291 WRITE_ONCE(msk->csum_enabled, true);
3292
3293 msk->write_seq = subflow_req->idsn + 1;
3294 msk->snd_nxt = msk->write_seq;
3295 msk->snd_una = msk->write_seq;
3296 msk->wnd_end = msk->snd_nxt + tcp_sk(ssk)->snd_wnd;
3297 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3298 mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3299
3300 /* passive msk is created after the first/MPC subflow */
3301 msk->subflow_id = 2;
3302
3303 sock_reset_flag(nsk, SOCK_RCU_FREE);
3304 security_inet_csk_clone(nsk, req);
3305
3306 /* this can't race with mptcp_close(), as the msk is
3307 * not yet exposted to user-space
3308 */
3309 mptcp_set_state(nsk, TCP_ESTABLISHED);
3310
3311 /* The msk maintain a ref to each subflow in the connections list */
3312 WRITE_ONCE(msk->first, ssk);
3313 subflow = mptcp_subflow_ctx(ssk);
3314 list_add(&subflow->node, &msk->conn_list);
3315 sock_hold(ssk);
3316
3317 /* new mpc subflow takes ownership of the newly
3318 * created mptcp socket
3319 */
3320 mptcp_token_accept(subflow_req, msk);
3321
3322 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3323 * uses the correct data
3324 */
3325 mptcp_copy_inaddrs(nsk, ssk);
3326 __mptcp_propagate_sndbuf(nsk, ssk);
3327
3328 mptcp_rcv_space_init(msk, ssk);
3329
3330 if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3331 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
3332 bh_unlock_sock(nsk);
3333
3334 /* note: the newly allocated socket refcount is 2 now */
3335 return nsk;
3336 }
3337
mptcp_rcv_space_init(struct mptcp_sock * msk,const struct sock * ssk)3338 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3339 {
3340 const struct tcp_sock *tp = tcp_sk(ssk);
3341
3342 msk->rcvspace_init = 1;
3343 msk->rcvq_space.copied = 0;
3344 msk->rcvq_space.rtt_us = 0;
3345
3346 msk->rcvq_space.time = tp->tcp_mstamp;
3347
3348 /* initial rcv_space offering made to peer */
3349 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3350 TCP_INIT_CWND * tp->advmss);
3351 if (msk->rcvq_space.space == 0)
3352 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3353 }
3354
mptcp_destroy_common(struct mptcp_sock * msk,unsigned int flags)3355 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3356 {
3357 struct mptcp_subflow_context *subflow, *tmp;
3358 struct sock *sk = (struct sock *)msk;
3359
3360 __mptcp_clear_xmit(sk);
3361
3362 /* join list will be eventually flushed (with rst) at sock lock release time */
3363 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3364 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3365
3366 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3367 mptcp_data_lock(sk);
3368 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3369 __skb_queue_purge(&sk->sk_receive_queue);
3370 skb_rbtree_purge(&msk->out_of_order_queue);
3371 mptcp_data_unlock(sk);
3372
3373 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3374 * inet_sock_destruct() will dispose it
3375 */
3376 sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
3377 WRITE_ONCE(msk->rmem_fwd_alloc, 0);
3378 mptcp_token_destroy(msk);
3379 mptcp_pm_free_anno_list(msk);
3380 mptcp_free_local_addr_list(msk);
3381 }
3382
mptcp_destroy(struct sock * sk)3383 static void mptcp_destroy(struct sock *sk)
3384 {
3385 struct mptcp_sock *msk = mptcp_sk(sk);
3386
3387 /* allow the following to close even the initial subflow */
3388 msk->free_first = 1;
3389 mptcp_destroy_common(msk, 0);
3390 sk_sockets_allocated_dec(sk);
3391 }
3392
__mptcp_data_acked(struct sock * sk)3393 void __mptcp_data_acked(struct sock *sk)
3394 {
3395 if (!sock_owned_by_user(sk))
3396 __mptcp_clean_una(sk);
3397 else
3398 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3399
3400 if (mptcp_pending_data_fin_ack(sk))
3401 mptcp_schedule_work(sk);
3402 }
3403
__mptcp_check_push(struct sock * sk,struct sock * ssk)3404 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3405 {
3406 if (!mptcp_send_head(sk))
3407 return;
3408
3409 if (!sock_owned_by_user(sk))
3410 __mptcp_subflow_push_pending(sk, ssk, false);
3411 else
3412 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3413 }
3414
3415 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3416 BIT(MPTCP_RETRANSMIT) | \
3417 BIT(MPTCP_FLUSH_JOIN_LIST))
3418
3419 /* processes deferred events and flush wmem */
mptcp_release_cb(struct sock * sk)3420 static void mptcp_release_cb(struct sock *sk)
3421 __must_hold(&sk->sk_lock.slock)
3422 {
3423 struct mptcp_sock *msk = mptcp_sk(sk);
3424
3425 for (;;) {
3426 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3427 struct list_head join_list;
3428
3429 if (!flags)
3430 break;
3431
3432 INIT_LIST_HEAD(&join_list);
3433 list_splice_init(&msk->join_list, &join_list);
3434
3435 /* the following actions acquire the subflow socket lock
3436 *
3437 * 1) can't be invoked in atomic scope
3438 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3439 * datapath acquires the msk socket spinlock while helding
3440 * the subflow socket lock
3441 */
3442 msk->cb_flags &= ~flags;
3443 spin_unlock_bh(&sk->sk_lock.slock);
3444
3445 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3446 __mptcp_flush_join_list(sk, &join_list);
3447 if (flags & BIT(MPTCP_PUSH_PENDING))
3448 __mptcp_push_pending(sk, 0);
3449 if (flags & BIT(MPTCP_RETRANSMIT))
3450 __mptcp_retrans(sk);
3451
3452 cond_resched();
3453 spin_lock_bh(&sk->sk_lock.slock);
3454 }
3455
3456 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3457 __mptcp_clean_una_wakeup(sk);
3458 if (unlikely(msk->cb_flags)) {
3459 /* be sure to sync the msk state before taking actions
3460 * depending on sk_state (MPTCP_ERROR_REPORT)
3461 * On sk release avoid actions depending on the first subflow
3462 */
3463 if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
3464 __mptcp_sync_state(sk, msk->pending_state);
3465 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3466 __mptcp_error_report(sk);
3467 if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
3468 __mptcp_sync_sndbuf(sk);
3469 }
3470
3471 __mptcp_update_rmem(sk);
3472 }
3473
3474 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3475 * TCP can't schedule delack timer before the subflow is fully established.
3476 * MPTCP uses the delack timer to do 3rd ack retransmissions
3477 */
schedule_3rdack_retransmission(struct sock * ssk)3478 static void schedule_3rdack_retransmission(struct sock *ssk)
3479 {
3480 struct inet_connection_sock *icsk = inet_csk(ssk);
3481 struct tcp_sock *tp = tcp_sk(ssk);
3482 unsigned long timeout;
3483
3484 if (mptcp_subflow_ctx(ssk)->fully_established)
3485 return;
3486
3487 /* reschedule with a timeout above RTT, as we must look only for drop */
3488 if (tp->srtt_us)
3489 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3490 else
3491 timeout = TCP_TIMEOUT_INIT;
3492 timeout += jiffies;
3493
3494 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3495 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3496 icsk->icsk_ack.timeout = timeout;
3497 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3498 }
3499
mptcp_subflow_process_delegated(struct sock * ssk,long status)3500 void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3501 {
3502 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3503 struct sock *sk = subflow->conn;
3504
3505 if (status & BIT(MPTCP_DELEGATE_SEND)) {
3506 mptcp_data_lock(sk);
3507 if (!sock_owned_by_user(sk))
3508 __mptcp_subflow_push_pending(sk, ssk, true);
3509 else
3510 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3511 mptcp_data_unlock(sk);
3512 }
3513 if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3514 mptcp_data_lock(sk);
3515 if (!sock_owned_by_user(sk))
3516 __mptcp_sync_sndbuf(sk);
3517 else
3518 __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3519 mptcp_data_unlock(sk);
3520 }
3521 if (status & BIT(MPTCP_DELEGATE_ACK))
3522 schedule_3rdack_retransmission(ssk);
3523 }
3524
mptcp_hash(struct sock * sk)3525 static int mptcp_hash(struct sock *sk)
3526 {
3527 /* should never be called,
3528 * we hash the TCP subflows not the master socket
3529 */
3530 WARN_ON_ONCE(1);
3531 return 0;
3532 }
3533
mptcp_unhash(struct sock * sk)3534 static void mptcp_unhash(struct sock *sk)
3535 {
3536 /* called from sk_common_release(), but nothing to do here */
3537 }
3538
mptcp_get_port(struct sock * sk,unsigned short snum)3539 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3540 {
3541 struct mptcp_sock *msk = mptcp_sk(sk);
3542
3543 pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3544 if (WARN_ON_ONCE(!msk->first))
3545 return -EINVAL;
3546
3547 return inet_csk_get_port(msk->first, snum);
3548 }
3549
mptcp_finish_connect(struct sock * ssk)3550 void mptcp_finish_connect(struct sock *ssk)
3551 {
3552 struct mptcp_subflow_context *subflow;
3553 struct mptcp_sock *msk;
3554 struct sock *sk;
3555
3556 subflow = mptcp_subflow_ctx(ssk);
3557 sk = subflow->conn;
3558 msk = mptcp_sk(sk);
3559
3560 pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3561
3562 subflow->map_seq = subflow->iasn;
3563 subflow->map_subflow_seq = 1;
3564
3565 /* the socket is not connected yet, no msk/subflow ops can access/race
3566 * accessing the field below
3567 */
3568 WRITE_ONCE(msk->local_key, subflow->local_key);
3569
3570 mptcp_pm_new_connection(msk, ssk, 0);
3571 }
3572
mptcp_sock_graft(struct sock * sk,struct socket * parent)3573 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3574 {
3575 write_lock_bh(&sk->sk_callback_lock);
3576 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3577 sk_set_socket(sk, parent);
3578 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3579 write_unlock_bh(&sk->sk_callback_lock);
3580 }
3581
mptcp_finish_join(struct sock * ssk)3582 bool mptcp_finish_join(struct sock *ssk)
3583 {
3584 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3585 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3586 struct sock *parent = (void *)msk;
3587 bool ret = true;
3588
3589 pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3590
3591 /* mptcp socket already closing? */
3592 if (!mptcp_is_fully_established(parent)) {
3593 subflow->reset_reason = MPTCP_RST_EMPTCP;
3594 return false;
3595 }
3596
3597 /* active subflow, already present inside the conn_list */
3598 if (!list_empty(&subflow->node)) {
3599 mptcp_subflow_joined(msk, ssk);
3600 mptcp_propagate_sndbuf(parent, ssk);
3601 return true;
3602 }
3603
3604 if (!mptcp_pm_allow_new_subflow(msk))
3605 goto err_prohibited;
3606
3607 /* If we can't acquire msk socket lock here, let the release callback
3608 * handle it
3609 */
3610 mptcp_data_lock(parent);
3611 if (!sock_owned_by_user(parent)) {
3612 ret = __mptcp_finish_join(msk, ssk);
3613 if (ret) {
3614 sock_hold(ssk);
3615 list_add_tail(&subflow->node, &msk->conn_list);
3616 }
3617 } else {
3618 sock_hold(ssk);
3619 list_add_tail(&subflow->node, &msk->join_list);
3620 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3621 }
3622 mptcp_data_unlock(parent);
3623
3624 if (!ret) {
3625 err_prohibited:
3626 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3627 return false;
3628 }
3629
3630 return true;
3631 }
3632
mptcp_shutdown(struct sock * sk,int how)3633 static void mptcp_shutdown(struct sock *sk, int how)
3634 {
3635 pr_debug("sk=%p, how=%d\n", sk, how);
3636
3637 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3638 __mptcp_wr_shutdown(sk);
3639 }
3640
mptcp_forward_alloc_get(const struct sock * sk)3641 static int mptcp_forward_alloc_get(const struct sock *sk)
3642 {
3643 return READ_ONCE(sk->sk_forward_alloc) +
3644 READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc);
3645 }
3646
mptcp_ioctl_outq(const struct mptcp_sock * msk,u64 v)3647 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3648 {
3649 const struct sock *sk = (void *)msk;
3650 u64 delta;
3651
3652 if (sk->sk_state == TCP_LISTEN)
3653 return -EINVAL;
3654
3655 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3656 return 0;
3657
3658 delta = msk->write_seq - v;
3659 if (__mptcp_check_fallback(msk) && msk->first) {
3660 struct tcp_sock *tp = tcp_sk(msk->first);
3661
3662 /* the first subflow is disconnected after close - see
3663 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3664 * so ignore that status, too.
3665 */
3666 if (!((1 << msk->first->sk_state) &
3667 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3668 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3669 }
3670 if (delta > INT_MAX)
3671 delta = INT_MAX;
3672
3673 return (int)delta;
3674 }
3675
mptcp_ioctl(struct sock * sk,int cmd,int * karg)3676 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3677 {
3678 struct mptcp_sock *msk = mptcp_sk(sk);
3679 bool slow;
3680
3681 switch (cmd) {
3682 case SIOCINQ:
3683 if (sk->sk_state == TCP_LISTEN)
3684 return -EINVAL;
3685
3686 lock_sock(sk);
3687 __mptcp_move_skbs(msk);
3688 *karg = mptcp_inq_hint(sk);
3689 release_sock(sk);
3690 break;
3691 case SIOCOUTQ:
3692 slow = lock_sock_fast(sk);
3693 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3694 unlock_sock_fast(sk, slow);
3695 break;
3696 case SIOCOUTQNSD:
3697 slow = lock_sock_fast(sk);
3698 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3699 unlock_sock_fast(sk, slow);
3700 break;
3701 default:
3702 return -ENOIOCTLCMD;
3703 }
3704
3705 return 0;
3706 }
3707
mptcp_subflow_early_fallback(struct mptcp_sock * msk,struct mptcp_subflow_context * subflow)3708 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3709 struct mptcp_subflow_context *subflow)
3710 {
3711 subflow->request_mptcp = 0;
3712 __mptcp_do_fallback(msk);
3713 }
3714
mptcp_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)3715 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3716 {
3717 struct mptcp_subflow_context *subflow;
3718 struct mptcp_sock *msk = mptcp_sk(sk);
3719 int err = -EINVAL;
3720 struct sock *ssk;
3721
3722 ssk = __mptcp_nmpc_sk(msk);
3723 if (IS_ERR(ssk))
3724 return PTR_ERR(ssk);
3725
3726 mptcp_set_state(sk, TCP_SYN_SENT);
3727 subflow = mptcp_subflow_ctx(ssk);
3728 #ifdef CONFIG_TCP_MD5SIG
3729 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3730 * TCP option space.
3731 */
3732 if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3733 mptcp_subflow_early_fallback(msk, subflow);
3734 #endif
3735 if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) {
3736 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
3737 mptcp_subflow_early_fallback(msk, subflow);
3738 }
3739
3740 WRITE_ONCE(msk->write_seq, subflow->idsn);
3741 WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3742 WRITE_ONCE(msk->snd_una, subflow->idsn);
3743 if (likely(!__mptcp_check_fallback(msk)))
3744 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3745
3746 /* if reaching here via the fastopen/sendmsg path, the caller already
3747 * acquired the subflow socket lock, too.
3748 */
3749 if (!msk->fastopening)
3750 lock_sock(ssk);
3751
3752 /* the following mirrors closely a very small chunk of code from
3753 * __inet_stream_connect()
3754 */
3755 if (ssk->sk_state != TCP_CLOSE)
3756 goto out;
3757
3758 if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3759 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3760 if (err)
3761 goto out;
3762 }
3763
3764 err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3765 if (err < 0)
3766 goto out;
3767
3768 inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3769
3770 out:
3771 if (!msk->fastopening)
3772 release_sock(ssk);
3773
3774 /* on successful connect, the msk state will be moved to established by
3775 * subflow_finish_connect()
3776 */
3777 if (unlikely(err)) {
3778 /* avoid leaving a dangling token in an unconnected socket */
3779 mptcp_token_destroy(msk);
3780 mptcp_set_state(sk, TCP_CLOSE);
3781 return err;
3782 }
3783
3784 mptcp_copy_inaddrs(sk, ssk);
3785 return 0;
3786 }
3787
3788 static struct proto mptcp_prot = {
3789 .name = "MPTCP",
3790 .owner = THIS_MODULE,
3791 .init = mptcp_init_sock,
3792 .connect = mptcp_connect,
3793 .disconnect = mptcp_disconnect,
3794 .close = mptcp_close,
3795 .setsockopt = mptcp_setsockopt,
3796 .getsockopt = mptcp_getsockopt,
3797 .shutdown = mptcp_shutdown,
3798 .destroy = mptcp_destroy,
3799 .sendmsg = mptcp_sendmsg,
3800 .ioctl = mptcp_ioctl,
3801 .recvmsg = mptcp_recvmsg,
3802 .release_cb = mptcp_release_cb,
3803 .hash = mptcp_hash,
3804 .unhash = mptcp_unhash,
3805 .get_port = mptcp_get_port,
3806 .forward_alloc_get = mptcp_forward_alloc_get,
3807 .sockets_allocated = &mptcp_sockets_allocated,
3808
3809 .memory_allocated = &tcp_memory_allocated,
3810 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3811
3812 .memory_pressure = &tcp_memory_pressure,
3813 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3814 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3815 .sysctl_mem = sysctl_tcp_mem,
3816 .obj_size = sizeof(struct mptcp_sock),
3817 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3818 .no_autobind = true,
3819 };
3820
mptcp_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)3821 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3822 {
3823 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3824 struct sock *ssk, *sk = sock->sk;
3825 int err = -EINVAL;
3826
3827 lock_sock(sk);
3828 ssk = __mptcp_nmpc_sk(msk);
3829 if (IS_ERR(ssk)) {
3830 err = PTR_ERR(ssk);
3831 goto unlock;
3832 }
3833
3834 if (sk->sk_family == AF_INET)
3835 err = inet_bind_sk(ssk, uaddr, addr_len);
3836 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3837 else if (sk->sk_family == AF_INET6)
3838 err = inet6_bind_sk(ssk, uaddr, addr_len);
3839 #endif
3840 if (!err)
3841 mptcp_copy_inaddrs(sk, ssk);
3842
3843 unlock:
3844 release_sock(sk);
3845 return err;
3846 }
3847
mptcp_listen(struct socket * sock,int backlog)3848 static int mptcp_listen(struct socket *sock, int backlog)
3849 {
3850 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3851 struct sock *sk = sock->sk;
3852 struct sock *ssk;
3853 int err;
3854
3855 pr_debug("msk=%p\n", msk);
3856
3857 lock_sock(sk);
3858
3859 err = -EINVAL;
3860 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3861 goto unlock;
3862
3863 ssk = __mptcp_nmpc_sk(msk);
3864 if (IS_ERR(ssk)) {
3865 err = PTR_ERR(ssk);
3866 goto unlock;
3867 }
3868
3869 mptcp_set_state(sk, TCP_LISTEN);
3870 sock_set_flag(sk, SOCK_RCU_FREE);
3871
3872 lock_sock(ssk);
3873 err = __inet_listen_sk(ssk, backlog);
3874 release_sock(ssk);
3875 mptcp_set_state(sk, inet_sk_state_load(ssk));
3876
3877 if (!err) {
3878 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3879 mptcp_copy_inaddrs(sk, ssk);
3880 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3881 }
3882
3883 unlock:
3884 release_sock(sk);
3885 return err;
3886 }
3887
mptcp_stream_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)3888 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3889 int flags, bool kern)
3890 {
3891 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3892 struct sock *ssk, *newsk;
3893 int err;
3894
3895 pr_debug("msk=%p\n", msk);
3896
3897 /* Buggy applications can call accept on socket states other then LISTEN
3898 * but no need to allocate the first subflow just to error out.
3899 */
3900 ssk = READ_ONCE(msk->first);
3901 if (!ssk)
3902 return -EINVAL;
3903
3904 pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
3905 newsk = inet_csk_accept(ssk, flags, &err, kern);
3906 if (!newsk)
3907 return err;
3908
3909 pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
3910 if (sk_is_mptcp(newsk)) {
3911 struct mptcp_subflow_context *subflow;
3912 struct sock *new_mptcp_sock;
3913
3914 subflow = mptcp_subflow_ctx(newsk);
3915 new_mptcp_sock = subflow->conn;
3916
3917 /* is_mptcp should be false if subflow->conn is missing, see
3918 * subflow_syn_recv_sock()
3919 */
3920 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3921 tcp_sk(newsk)->is_mptcp = 0;
3922 goto tcpfallback;
3923 }
3924
3925 newsk = new_mptcp_sock;
3926 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3927
3928 newsk->sk_kern_sock = kern;
3929 lock_sock(newsk);
3930 __inet_accept(sock, newsock, newsk);
3931
3932 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3933 msk = mptcp_sk(newsk);
3934 msk->in_accept_queue = 0;
3935
3936 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3937 * This is needed so NOSPACE flag can be set from tcp stack.
3938 */
3939 mptcp_for_each_subflow(msk, subflow) {
3940 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3941
3942 if (!ssk->sk_socket)
3943 mptcp_sock_graft(ssk, newsock);
3944 }
3945
3946 /* Do late cleanup for the first subflow as necessary. Also
3947 * deal with bad peers not doing a complete shutdown.
3948 */
3949 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3950 __mptcp_close_ssk(newsk, msk->first,
3951 mptcp_subflow_ctx(msk->first), 0);
3952 if (unlikely(list_is_singular(&msk->conn_list)))
3953 mptcp_set_state(newsk, TCP_CLOSE);
3954 }
3955 } else {
3956 tcpfallback:
3957 newsk->sk_kern_sock = kern;
3958 lock_sock(newsk);
3959 __inet_accept(sock, newsock, newsk);
3960 /* we are being invoked after accepting a non-mp-capable
3961 * flow: sk is a tcp_sk, not an mptcp one.
3962 *
3963 * Hand the socket over to tcp so all further socket ops
3964 * bypass mptcp.
3965 */
3966 WRITE_ONCE(newsock->sk->sk_socket->ops,
3967 mptcp_fallback_tcp_ops(newsock->sk));
3968 }
3969 release_sock(newsk);
3970
3971 return 0;
3972 }
3973
mptcp_check_writeable(struct mptcp_sock * msk)3974 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3975 {
3976 struct sock *sk = (struct sock *)msk;
3977
3978 if (sk_stream_is_writeable(sk))
3979 return EPOLLOUT | EPOLLWRNORM;
3980
3981 mptcp_set_nospace(sk);
3982 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3983 if (sk_stream_is_writeable(sk))
3984 return EPOLLOUT | EPOLLWRNORM;
3985
3986 return 0;
3987 }
3988
mptcp_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)3989 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3990 struct poll_table_struct *wait)
3991 {
3992 struct sock *sk = sock->sk;
3993 struct mptcp_sock *msk;
3994 __poll_t mask = 0;
3995 u8 shutdown;
3996 int state;
3997
3998 msk = mptcp_sk(sk);
3999 sock_poll_wait(file, sock, wait);
4000
4001 state = inet_sk_state_load(sk);
4002 pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
4003 if (state == TCP_LISTEN) {
4004 struct sock *ssk = READ_ONCE(msk->first);
4005
4006 if (WARN_ON_ONCE(!ssk))
4007 return 0;
4008
4009 return inet_csk_listen_poll(ssk);
4010 }
4011
4012 shutdown = READ_ONCE(sk->sk_shutdown);
4013 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
4014 mask |= EPOLLHUP;
4015 if (shutdown & RCV_SHUTDOWN)
4016 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
4017
4018 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4019 mask |= mptcp_check_readable(sk);
4020 if (shutdown & SEND_SHUTDOWN)
4021 mask |= EPOLLOUT | EPOLLWRNORM;
4022 else
4023 mask |= mptcp_check_writeable(msk);
4024 } else if (state == TCP_SYN_SENT &&
4025 inet_test_bit(DEFER_CONNECT, sk)) {
4026 /* cf tcp_poll() note about TFO */
4027 mask |= EPOLLOUT | EPOLLWRNORM;
4028 }
4029
4030 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
4031 smp_rmb();
4032 if (READ_ONCE(sk->sk_err))
4033 mask |= EPOLLERR;
4034
4035 return mask;
4036 }
4037
4038 static const struct proto_ops mptcp_stream_ops = {
4039 .family = PF_INET,
4040 .owner = THIS_MODULE,
4041 .release = inet_release,
4042 .bind = mptcp_bind,
4043 .connect = inet_stream_connect,
4044 .socketpair = sock_no_socketpair,
4045 .accept = mptcp_stream_accept,
4046 .getname = inet_getname,
4047 .poll = mptcp_poll,
4048 .ioctl = inet_ioctl,
4049 .gettstamp = sock_gettstamp,
4050 .listen = mptcp_listen,
4051 .shutdown = inet_shutdown,
4052 .setsockopt = sock_common_setsockopt,
4053 .getsockopt = sock_common_getsockopt,
4054 .sendmsg = inet_sendmsg,
4055 .recvmsg = inet_recvmsg,
4056 .mmap = sock_no_mmap,
4057 .set_rcvlowat = mptcp_set_rcvlowat,
4058 };
4059
4060 static struct inet_protosw mptcp_protosw = {
4061 .type = SOCK_STREAM,
4062 .protocol = IPPROTO_MPTCP,
4063 .prot = &mptcp_prot,
4064 .ops = &mptcp_stream_ops,
4065 .flags = INET_PROTOSW_ICSK,
4066 };
4067
mptcp_napi_poll(struct napi_struct * napi,int budget)4068 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4069 {
4070 struct mptcp_delegated_action *delegated;
4071 struct mptcp_subflow_context *subflow;
4072 int work_done = 0;
4073
4074 delegated = container_of(napi, struct mptcp_delegated_action, napi);
4075 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4076 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4077
4078 bh_lock_sock_nested(ssk);
4079 if (!sock_owned_by_user(ssk)) {
4080 mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4081 } else {
4082 /* tcp_release_cb_override already processed
4083 * the action or will do at next release_sock().
4084 * In both case must dequeue the subflow here - on the same
4085 * CPU that scheduled it.
4086 */
4087 smp_wmb();
4088 clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4089 }
4090 bh_unlock_sock(ssk);
4091 sock_put(ssk);
4092
4093 if (++work_done == budget)
4094 return budget;
4095 }
4096
4097 /* always provide a 0 'work_done' argument, so that napi_complete_done
4098 * will not try accessing the NULL napi->dev ptr
4099 */
4100 napi_complete_done(napi, 0);
4101 return work_done;
4102 }
4103
mptcp_proto_init(void)4104 void __init mptcp_proto_init(void)
4105 {
4106 struct mptcp_delegated_action *delegated;
4107 int cpu;
4108
4109 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4110
4111 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4112 panic("Failed to allocate MPTCP pcpu counter\n");
4113
4114 init_dummy_netdev(&mptcp_napi_dev);
4115 for_each_possible_cpu(cpu) {
4116 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4117 INIT_LIST_HEAD(&delegated->head);
4118 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
4119 mptcp_napi_poll);
4120 napi_enable(&delegated->napi);
4121 }
4122
4123 mptcp_subflow_init();
4124 mptcp_pm_init();
4125 mptcp_sched_init();
4126 mptcp_token_init();
4127
4128 if (proto_register(&mptcp_prot, 1) != 0)
4129 panic("Failed to register MPTCP proto.\n");
4130
4131 inet_register_protosw(&mptcp_protosw);
4132
4133 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4134 }
4135
4136 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4137 static const struct proto_ops mptcp_v6_stream_ops = {
4138 .family = PF_INET6,
4139 .owner = THIS_MODULE,
4140 .release = inet6_release,
4141 .bind = mptcp_bind,
4142 .connect = inet_stream_connect,
4143 .socketpair = sock_no_socketpair,
4144 .accept = mptcp_stream_accept,
4145 .getname = inet6_getname,
4146 .poll = mptcp_poll,
4147 .ioctl = inet6_ioctl,
4148 .gettstamp = sock_gettstamp,
4149 .listen = mptcp_listen,
4150 .shutdown = inet_shutdown,
4151 .setsockopt = sock_common_setsockopt,
4152 .getsockopt = sock_common_getsockopt,
4153 .sendmsg = inet6_sendmsg,
4154 .recvmsg = inet6_recvmsg,
4155 .mmap = sock_no_mmap,
4156 #ifdef CONFIG_COMPAT
4157 .compat_ioctl = inet6_compat_ioctl,
4158 #endif
4159 .set_rcvlowat = mptcp_set_rcvlowat,
4160 };
4161
4162 static struct proto mptcp_v6_prot;
4163
4164 static struct inet_protosw mptcp_v6_protosw = {
4165 .type = SOCK_STREAM,
4166 .protocol = IPPROTO_MPTCP,
4167 .prot = &mptcp_v6_prot,
4168 .ops = &mptcp_v6_stream_ops,
4169 .flags = INET_PROTOSW_ICSK,
4170 };
4171
mptcp_proto_v6_init(void)4172 int __init mptcp_proto_v6_init(void)
4173 {
4174 int err;
4175
4176 mptcp_v6_prot = mptcp_prot;
4177 strcpy(mptcp_v6_prot.name, "MPTCPv6");
4178 mptcp_v6_prot.slab = NULL;
4179 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4180 mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4181
4182 err = proto_register(&mptcp_v6_prot, 1);
4183 if (err)
4184 return err;
4185
4186 err = inet6_register_protosw(&mptcp_v6_protosw);
4187 if (err)
4188 proto_unregister(&mptcp_v6_prot);
4189
4190 return err;
4191 }
4192 #endif
4193