~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mptcp/protocol.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php