LCOV - code coverage report
Current view: top level - mptcp/protocol.c (source / functions) Coverage Total Hit
Test: export Lines: 90.6 % 2734 2476
Test Date: 2026-11-30 19:02:55 Functions: 94.5 % 163 154
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 68.6 % 1763 1210

             Branch data     Line data    Source code
       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 <linux/errqueue.h>
      15                 :             : #include <net/aligned_data.h>
      16                 :             : #include <net/rps.h>
      17                 :             : #include <net/sock.h>
      18                 :             : #include <net/inet_common.h>
      19                 :             : #include <net/inet_hashtables.h>
      20                 :             : #include <net/protocol.h>
      21                 :             : #include <net/tcp_states.h>
      22                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      23                 :             : #include <net/transp_v6.h>
      24                 :             : #endif
      25                 :             : #include <net/mptcp.h>
      26                 :             : #include <net/hotdata.h>
      27                 :             : #include <net/xfrm.h>
      28                 :             : #include <asm/ioctls.h>
      29                 :             : #include "protocol.h"
      30                 :             : #include "mib.h"
      31                 :             : 
      32                 :             : static unsigned int mptcp_inq_hint(const struct sock *sk);
      33                 :             : 
      34                 :             : #define CREATE_TRACE_POINTS
      35                 :             : #include <trace/events/mptcp.h>
      36                 :             : 
      37                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      38                 :             : struct mptcp6_sock {
      39                 :             :         struct mptcp_sock msk;
      40                 :             :         struct ipv6_pinfo np;
      41                 :             : };
      42                 :             : #endif
      43                 :             : 
      44                 :             : enum {
      45                 :             :         MPTCP_CMSG_TS = BIT(0),
      46                 :             :         MPTCP_CMSG_INQ = BIT(1),
      47                 :             : };
      48                 :             : 
      49                 :             : static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
      50                 :             : 
      51                 :             : static void __mptcp_destroy_sock(struct sock *sk);
      52                 :             : static void mptcp_check_send_data_fin(struct sock *sk);
      53                 :             : 
      54                 :             : DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions) = {
      55                 :             :         .bh_lock = INIT_LOCAL_LOCK(bh_lock),
      56                 :             : };
      57                 :             : static struct net_device *mptcp_napi_dev;
      58                 :             : 
      59                 :             : /* Returns end sequence number of the receiver's advertised window */
      60                 :      393305 : u64 mptcp_wnd_end(const struct mptcp_sock *msk)
      61                 :             : {
      62                 :      393305 :         return READ_ONCE(msk->wnd_end);
      63                 :             : }
      64                 :             : 
      65                 :         178 : static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
      66                 :             : {
      67                 :         178 :         unsigned short family = READ_ONCE(sk->sk_family);
      68                 :             : 
      69                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
      70            [ + ]:         178 :         if (family == AF_INET6)
      71                 :             :                 return &inet6_stream_ops;
      72                 :             : #endif
      73            [ + ]:          88 :         WARN_ON_ONCE(family != AF_INET);
      74                 :             :         return &inet_stream_ops;
      75                 :             : }
      76                 :             : 
      77                 :         220 : bool __mptcp_try_fallback(struct mptcp_sock *msk, int fb_mib)
      78                 :             : {
      79                 :         220 :         struct net *net = sock_net((struct sock *)msk);
      80                 :             : 
      81       [ + ][ + ]:         220 :         if (__mptcp_check_fallback(msk))
      82                 :             :                 return true;
      83                 :             : 
      84                 :             :         /* The caller possibly is not holding the msk socket lock, but
      85                 :             :          * in the fallback case only the current subflow is touching
      86                 :             :          * the OoO queue.
      87                 :             :          */
      88       [ + ][ + ]:         220 :         if (!RB_EMPTY_ROOT(&msk->out_of_order_queue))
      89                 :             :                 return false;
      90                 :             : 
      91                 :         220 :         spin_lock_bh(&msk->fallback_lock);
      92    [ +  + ][ + ]:         220 :         if (!msk->allow_infinite_fallback) {
      93                 :           6 :                 spin_unlock_bh(&msk->fallback_lock);
      94                 :           6 :                 return false;
      95                 :             :         }
      96                 :             : 
      97                 :         214 :         msk->allow_subflows = false;
      98                 :         214 :         set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
      99                 :         214 :         clear_bit(MPTCP_RTX_ENABLED, &msk->flags);
     100       [ - ][ - ]:         214 :         __MPTCP_INC_STATS(net, fb_mib);
     101                 :         214 :         spin_unlock_bh(&msk->fallback_lock);
     102                 :         214 :         return true;
     103                 :             : }
     104                 :             : 
     105                 :        3586 : static int __mptcp_socket_create(struct mptcp_sock *msk)
     106                 :             : {
     107                 :        3586 :         struct mptcp_subflow_context *subflow;
     108                 :        3586 :         struct sock *sk = (struct sock *)msk;
     109                 :        3586 :         struct socket *ssock;
     110                 :        3586 :         int err;
     111                 :             : 
     112                 :        3586 :         err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
     113       [ - ][ - ]:        3586 :         if (err)
     114                 :             :                 return err;
     115                 :             : 
     116            [ + ]:        3586 :         msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
     117                 :        3586 :         WRITE_ONCE(msk->first, ssock->sk);
     118            [ - ]:        3586 :         subflow = mptcp_subflow_ctx(ssock->sk);
     119            [ - ]:        3586 :         list_add(&subflow->node, &msk->conn_list);
     120                 :        3586 :         sock_hold(ssock->sk);
     121                 :        3586 :         subflow->request_mptcp = 1;
     122                 :        3586 :         subflow->subflow_id = msk->subflow_id++;
     123                 :             : 
     124                 :             :         /* This is the first subflow, always with id 0 */
     125                 :        3586 :         WRITE_ONCE(subflow->local_id, 0);
     126                 :        3586 :         mptcp_sock_graft(msk->first, sk->sk_socket);
     127                 :        3586 :         iput(SOCK_INODE(ssock));
     128                 :             : 
     129                 :        3586 :         return 0;
     130                 :             : }
     131                 :             : 
     132                 :             : /* If the MPC handshake is not started, returns the first subflow,
     133                 :             :  * eventually allocating it.
     134                 :             :  */
     135                 :       10443 : struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
     136                 :             : {
     137                 :       10443 :         struct sock *sk = (struct sock *)msk;
     138                 :       10443 :         int ret;
     139                 :             : 
     140    [ +  + ][ + ]:       10443 :         if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
     141                 :             :                 return ERR_PTR(-EINVAL);
     142                 :             : 
     143       [ + ][ + ]:       10425 :         if (!msk->first) {
     144                 :        3586 :                 ret = __mptcp_socket_create(msk);
     145       [ + ][ + ]:        3586 :                 if (ret)
     146                 :           0 :                         return ERR_PTR(ret);
     147                 :             :         }
     148                 :             : 
     149                 :       10425 :         return msk->first;
     150                 :             : }
     151                 :             : 
     152                 :        1083 : static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
     153                 :             : {
     154                 :             :         /* The skb forward memory was already transferred to sk by
     155                 :             :          * mptcp_borrow_fwdmem(), even before setting the destructor.
     156                 :             :          */
     157            [ + ]:        1083 :         if (!skb->destructor)
     158                 :         810 :                 sk_mem_reclaim(sk);
     159                 :             : 
     160                 :        1083 :         sk_drops_skbadd(sk, skb);
     161                 :        1083 :         __kfree_skb(skb);
     162                 :        1083 : }
     163                 :             : 
     164                 :      553315 : static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
     165                 :             :                                  struct sk_buff *from, bool *fragstolen,
     166                 :             :                                  int *delta)
     167                 :             : {
     168                 :      553315 :         int limit = READ_ONCE(sk->sk_rcvbuf);
     169                 :             : 
     170            [ + ]:      553315 :         if (unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
     171            [ + ]:      553291 :             MPTCP_SKB_CB(from)->offset ||
     172         [ +  + ]:     1067061 :             ((to->len + from->len) > (limit >> 3)) ||
     173                 :      513810 :             !skb_try_coalesce(to, from, fragstolen, delta))
     174                 :      171673 :                 return false;
     175                 :             : 
     176         [ -  - ]:      381642 :         pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
     177                 :             :                  MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
     178                 :             :                  to->len, MPTCP_SKB_CB(from)->end_seq);
     179                 :      381642 :         MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
     180                 :      381642 :         return true;
     181                 :             : }
     182                 :             : 
     183                 :      497553 : static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
     184                 :             :                                struct sk_buff *from)
     185                 :             : {
     186                 :      497553 :         bool fragstolen;
     187                 :      497553 :         int delta;
     188                 :             : 
     189       [ + ][ + ]:      497553 :         if (!__mptcp_try_coalesce(sk, to, from, &fragstolen, &delta))
     190                 :             :                 return false;
     191                 :             : 
     192                 :             :         /* note the fwd memory can reach a negative value after accounting
     193                 :             :          * for the delta, but the later skb free will restore a non
     194                 :             :          * negative one
     195                 :             :          */
     196                 :      349950 :         atomic_add(delta, &sk->sk_rmem_alloc);
     197       [ - ][ - ]:      349950 :         sk_mem_charge(sk, delta);
     198            [ + ]:      349950 :         kfree_skb_partial(from, fragstolen);
     199                 :             : 
     200                 :             :         /* release what is left of the borrowed 'from' truesize */
     201                 :      349950 :         sk_mem_reclaim(sk);
     202                 :             : 
     203                 :      349950 :         return true;
     204                 :             : }
     205                 :             : 
     206                 :       11049 : static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
     207                 :             :                                    struct sk_buff *from)
     208                 :             : {
     209       [ + ][ + ]:       53865 :         if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
     210                 :             :                 return false;
     211                 :             : 
     212                 :      241815 :         return mptcp_try_coalesce((struct sock *)msk, to, from);
     213                 :             : }
     214                 :             : 
     215                 :             : /* "inspired" by tcp_rcvbuf_grow(), main difference:
     216                 :             :  * - mptcp does not maintain a msk-level window clamp
     217                 :             :  * - returns true when  the receive buffer is actually updated
     218                 :             :  */
     219                 :        5008 : static bool mptcp_rcvbuf_grow(struct sock *sk, u32 newval)
     220                 :             : {
     221            [ + ]:        5008 :         struct mptcp_sock *msk = mptcp_sk(sk);
     222            [ - ]:        5008 :         const struct net *net = sock_net(sk);
     223                 :        5008 :         u32 rcvwin, rcvbuf, cap, oldval;
     224                 :        5008 :         u64 grow;
     225                 :             : 
     226                 :        5008 :         oldval = msk->rcvq_space.space;
     227                 :        5008 :         msk->rcvq_space.space = newval;
     228       [ - ][ - ]:        5008 :         if (!READ_ONCE(net->ipv4.sysctl_tcp_moderate_rcvbuf) ||
     229       [ + ][ + ]:        5008 :             (sk->sk_userlocks & SOCK_RCVBUF_LOCK))
     230                 :             :                 return false;
     231                 :             : 
     232                 :             :         /* DRS is always one RTT late. */
     233                 :        5008 :         rcvwin = newval << 1;
     234                 :             : 
     235                 :             :         /* slow start: allow the sender to double its rate. */
     236                 :        5008 :         grow = (u64)rcvwin * (newval - oldval);
     237                 :        5008 :         do_div(grow, oldval);
     238                 :        5008 :         rcvwin += grow << 1;
     239                 :             : 
     240                 :        5008 :         cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
     241                 :             : 
     242                 :        5008 :         rcvbuf = min_t(u32, mptcp_space_from_win(sk, rcvwin), cap);
     243       [ + ][ + ]:        5008 :         if (rcvbuf > sk->sk_rcvbuf) {
     244                 :        2689 :                 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
     245                 :        2689 :                 return true;
     246                 :             :         }
     247                 :             :         return false;
     248                 :             : }
     249                 :             : 
     250                 :             : /* "Inspired" from the TCP version; main difference: stop as soon as the MPTCP
     251                 :             :  * socket is under memory limit.
     252                 :             :  */
     253                 :           1 : static void mptcp_prune_ofo_queue(struct sock *sk,
     254                 :             :                                   const struct sk_buff *in_skb)
     255                 :             : {
     256            [ + ]:           1 :         struct mptcp_sock *msk = mptcp_sk(sk);
     257                 :           1 :         struct rb_node *node, *prev;
     258                 :           1 :         bool pruned = false;
     259                 :           1 :         u64 mem;
     260                 :             : 
     261       [ + ][ # ]:           1 :         if (RB_EMPTY_ROOT(&msk->out_of_order_queue))
     262                 :             :                 return;
     263                 :             : 
     264                 :           0 :         node = &msk->ooo_last_skb->rbnode;
     265                 :             : 
     266                 :           0 :         do {
     267                 :           0 :                 struct sk_buff *skb = rb_to_skb(node);
     268                 :             : 
     269                 :             :                 /* Stop pruning if the incoming skb would land in OoO tail. */
     270       [ # ][ # ]:           0 :                 if (after64(MPTCP_SKB_CB(in_skb)->map_seq,
     271                 :             :                             MPTCP_SKB_CB(skb)->map_seq))
     272                 :             :                         break;
     273                 :             : 
     274                 :           0 :                 pruned = true;
     275                 :           0 :                 prev = rb_prev(node);
     276                 :           0 :                 rb_erase(node, &msk->out_of_order_queue);
     277                 :           0 :                 mptcp_drop(sk, skb);
     278                 :           0 :                 msk->ooo_last_skb = rb_to_skb(prev);
     279                 :             : 
     280            [ # ]:           0 :                 mem = (unsigned int)sk_rmem_alloc_get(sk);
     281       [ # ][ # ]:           0 :                 if (mem <= sk->sk_rcvbuf)
     282                 :             :                         break;
     283                 :             : 
     284                 :           0 :                 node = prev;
     285       [ # ][ # ]:           0 :         } while (node);
     286                 :             : 
     287       [ # ][ # ]:           0 :         if (pruned)
     288       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOPRUNED);
     289                 :             : }
     290                 :             : 
     291                 :             : /* The stack can't drop packets for fallback socket at the msk level, or the
     292                 :             :  * stream will break.
     293                 :             :  */
     294                 :     1127448 : static bool mptcp_can_ingest(const struct sock *sk)
     295                 :             : {
     296 [ +  + ][ +  - ]:     1127451 :         return likely(sk_rmem_alloc_get(sk) <= READ_ONCE(sk->sk_rcvbuf)) ||
     297       [ + ][ # ]:           3 :                __mptcp_check_fallback(mptcp_sk(sk));
     298                 :             : }
     299                 :             : 
     300                 :     1127447 : static bool mptcp_try_rmem_schedule(struct sock *sk, const struct sk_buff *skb)
     301                 :             : {
     302            [ + ]:     1127447 :         if (!mptcp_can_ingest(sk)) {
     303                 :           1 :                 mptcp_prune_ofo_queue(sk, skb);
     304                 :           1 :                 return mptcp_can_ingest(sk);
     305                 :             :         }
     306                 :             :         return true;
     307                 :             : }
     308                 :             : 
     309                 :             : /* "inspired" by tcp_data_queue_ofo(), main differences:
     310                 :             :  * - use mptcp seqs
     311                 :             :  * - don't cope with sacks
     312                 :             :  */
     313                 :      212534 : static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
     314                 :             : {
     315                 :      212534 :         struct sock *sk = (struct sock *)msk;
     316                 :      212534 :         struct rb_node **p, *parent;
     317                 :      212534 :         u64 seq, end_seq, max_seq;
     318                 :      212534 :         struct sk_buff *skb1;
     319                 :             : 
     320                 :      212534 :         seq = MPTCP_SKB_CB(skb)->map_seq;
     321                 :      212534 :         end_seq = MPTCP_SKB_CB(skb)->end_seq;
     322            [ - ]:      212534 :         max_seq = atomic64_read(&msk->rcv_wnd_sent);
     323                 :             : 
     324 [ -  - ][ -  - ]:      212534 :         pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
     325                 :             :                  RB_EMPTY_ROOT(&msk->out_of_order_queue));
     326       [ + ][ + ]:      212534 :         if (after64(end_seq, max_seq)) {
     327                 :             :                 /* out of window */
     328                 :          66 :                 mptcp_drop(sk, skb);
     329 [ -  - ][ #  # ]:          66 :                 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
     330                 :             :                          (unsigned long long)end_seq - (unsigned long)max_seq,
     331                 :             :                          (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
     332       [ # ][ # ]:          66 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
     333                 :          66 :                 return;
     334                 :             :         }
     335                 :             : 
     336       [ + ][ + ]:      212468 :         if (!mptcp_try_rmem_schedule(sk, skb)) {
     337       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
     338                 :           0 :                 mptcp_drop(sk, skb);
     339                 :           0 :                 return;
     340                 :             :         }
     341                 :             : 
     342                 :      212468 :         p = &msk->out_of_order_queue.rb_node;
     343       [ - ][ - ]:      212468 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
     344       [ + ][ + ]:      212468 :         if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
     345                 :       13270 :                 rb_link_node(&skb->rbnode, NULL, p);
     346                 :       13270 :                 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
     347                 :       13270 :                 msk->ooo_last_skb = skb;
     348                 :       13270 :                 goto end;
     349                 :             :         }
     350                 :             : 
     351                 :             :         /* with 2 subflows, adding at end of ooo queue is quite likely
     352                 :             :          * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
     353                 :             :          */
     354 [ +  + ][ +  + ]:      366995 :         if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
     355       [ - ][ - ]:      143784 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
     356       [ - ][ - ]:      143784 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
     357                 :      143784 :                 return;
     358                 :             :         }
     359                 :             : 
     360                 :             :         /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
     361       [ + ][ + ]:       55414 :         if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
     362       [ - ][ - ]:       31878 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
     363                 :       31878 :                 parent = &msk->ooo_last_skb->rbnode;
     364                 :       31878 :                 p = &parent->rb_right;
     365                 :       31878 :                 goto insert;
     366                 :             :         }
     367                 :             : 
     368                 :             :         /* Find place to insert this segment. Handle overlaps on the way. */
     369                 :             :         parent = NULL;
     370       [ + ][ + ]:      138696 :         while (*p) {
     371                 :      129726 :                 parent = *p;
     372                 :      129726 :                 skb1 = rb_to_skb(parent);
     373       [ + ][ + ]:      129726 :                 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
     374                 :       35281 :                         p = &parent->rb_left;
     375                 :       35281 :                         continue;
     376                 :             :                 }
     377       [ + ][ + ]:       94445 :                 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
     378       [ + ][ + ]:         667 :                         if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
     379                 :             :                                 /* All the bits are present. Drop. */
     380                 :         419 :                                 mptcp_drop(sk, skb);
     381       [ - ][ - ]:         419 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     382                 :         419 :                                 return;
     383                 :             :                         }
     384       [ + ][ + ]:         248 :                         if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
     385                 :             :                                 /* partial overlap:
     386                 :             :                                  *     |     skb      |
     387                 :             :                                  *  |     skb1    |
     388                 :             :                                  * continue traversing
     389                 :             :                                  */
     390                 :             :                         } else {
     391                 :             :                                 /* skb's seq == skb1's seq and skb covers skb1.
     392                 :             :                                  * Replace skb1 with skb.
     393                 :             :                                  */
     394                 :          20 :                                 rb_replace_node(&skb1->rbnode, &skb->rbnode,
     395                 :             :                                                 &msk->out_of_order_queue);
     396                 :          20 :                                 mptcp_drop(sk, skb1);
     397       [ - ][ # ]:          20 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     398                 :          20 :                                 goto merge_right;
     399                 :             :                         }
     400 [ +  + ][ +  + ]:      114664 :                 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
     401       [ - ][ - ]:       14127 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
     402                 :       14127 :                         return;
     403                 :             :                 }
     404                 :       79879 :                 p = &parent->rb_right;
     405                 :             :         }
     406                 :             : 
     407                 :        8970 : insert:
     408                 :             :         /* Insert segment into RB tree. */
     409                 :       40848 :         rb_link_node(&skb->rbnode, parent, p);
     410                 :       40848 :         rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
     411                 :             : 
     412                 :             : merge_right:
     413                 :             :         /* Remove other segments covered by skb. */
     414       [ + ][ + ]:       40955 :         while ((skb1 = skb_rb_next(skb)) != NULL) {
     415       [ + ][ + ]:        9076 :                 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
     416                 :             :                         break;
     417                 :          87 :                 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
     418                 :          87 :                 mptcp_drop(sk, skb1);
     419       [ - ][ - ]:        2021 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     420                 :             :         }
     421                 :             :         /* If there is no skb after us, we are the last_skb ! */
     422       [ + ][ + ]:       40868 :         if (!skb1)
     423                 :       31879 :                 msk->ooo_last_skb = skb;
     424                 :             : 
     425                 :        8989 : end:
     426                 :       54138 :         skb_condense(skb);
     427                 :       54138 :         skb_set_owner_r(skb, sk);
     428                 :             : 
     429                 :             :         /* skb_condense() can shrink the truesize borrowed from the subflow */
     430                 :       54138 :         sk_mem_reclaim(sk);
     431                 :             : }
     432                 :             : 
     433                 :     1159530 : static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
     434                 :             :                            int copy_len)
     435                 :             : {
     436                 :     1159530 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
     437                 :     1159530 :         bool has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
     438                 :             : 
     439                 :             :         /* the skb map_seq accounts for the skb offset:
     440                 :             :          * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
     441                 :             :          * value
     442                 :             :          */
     443                 :     1159530 :         MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
     444                 :     1159530 :         MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
     445                 :     1159530 :         MPTCP_SKB_CB(skb)->offset = offset;
     446                 :     1159530 :         MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
     447                 :     1159530 :         MPTCP_SKB_CB(skb)->cant_coalesce = 0;
     448                 :             : 
     449                 :     1159530 :         __skb_unlink(skb, &ssk->sk_receive_queue);
     450                 :             : 
     451                 :     1159530 :         skb_ext_reset(skb);
     452            [ + ]:     1159530 :         skb_dst_drop(skb);
     453                 :     1159530 : }
     454                 :             : 
     455                 :     1127837 : static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
     456                 :             : {
     457                 :     1127837 :         u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;
     458            [ + ]:     1127837 :         struct mptcp_sock *msk = mptcp_sk(sk);
     459                 :     1127837 :         struct sk_buff *tail;
     460                 :             : 
     461                 :     1127837 :         mptcp_borrow_fwdmem(sk, skb);
     462                 :             : 
     463       [ + ][ + ]:     1127837 :         if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
     464                 :             :                 /* in sequence */
     465                 :      914962 : insert:
     466       [ + ][ + ]:      914979 :                 if (!mptcp_try_rmem_schedule(sk, skb)) {
     467       [ # ][ # ]:           1 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
     468                 :           1 :                         mptcp_drop(sk, skb);
     469                 :           1 :                         return false;
     470                 :             :                 }
     471                 :             : 
     472                 :      914978 :                 msk->bytes_received += copy_len;
     473                 :      914978 :                 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
     474       [ + ][ + ]:      914978 :                 tail = skb_peek_tail(&sk->sk_receive_queue);
     475 [ -  + ][ -  + ]:      255738 :                 if (tail && mptcp_try_coalesce(sk, tail, skb))
     476                 :             :                         return true;
     477                 :             : 
     478                 :      742882 :                 skb_set_owner_r(skb, sk);
     479                 :      742882 :                 __skb_queue_tail(&sk->sk_receive_queue, skb);
     480                 :      742882 :                 return true;
     481       [ + ][ + ]:      212875 :         } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
     482                 :      212534 :                 mptcp_data_queue_ofo(msk, skb);
     483                 :      212534 :                 return false;
     484                 :             :         }
     485                 :             : 
     486                 :             :         /* Partial packet */
     487       [ + ][ + ]:         341 :         if (after64(MPTCP_SKB_CB(skb)->end_seq, msk->ack_seq)) {
     488                 :          17 :                 copy_len = MPTCP_SKB_CB(skb)->end_seq - msk->ack_seq;
     489                 :          17 :                 MPTCP_SKB_CB(skb)->offset += msk->ack_seq -
     490                 :             :                                              MPTCP_SKB_CB(skb)->map_seq;
     491                 :          17 :                 MPTCP_SKB_CB(skb)->map_seq += msk->ack_seq -
     492                 :             :                                               MPTCP_SKB_CB(skb)->map_seq;
     493                 :          17 :                 goto insert;
     494                 :             :         }
     495                 :             : 
     496                 :             :         /* Completely old data */
     497       [ - ][ - ]:         324 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     498                 :         324 :         mptcp_drop(sk, skb);
     499                 :         324 :         return false;
     500                 :             : }
     501                 :             : 
     502                 :       43493 : static void mptcp_stop_rtx_timer(struct sock *sk)
     503                 :             : {
     504                 :       43493 :         sk_stop_timer(sk, &sk->mptcp_retransmit_timer);
     505            [ + ]:       43493 :         mptcp_sk(sk)->timer_ival = 0;
     506                 :       43493 : }
     507                 :             : 
     508                 :        6145 : static void mptcp_close_wake_up(struct sock *sk)
     509                 :             : {
     510            [ + ]:        6145 :         if (sock_flag(sk, SOCK_DEAD))
     511                 :             :                 return;
     512                 :             : 
     513                 :        4460 :         sk->sk_state_change(sk);
     514            [ + ]:        4460 :         if (sk->sk_shutdown == SHUTDOWN_MASK ||
     515            [ + ]:        2241 :             sk->sk_state == TCP_CLOSE)
     516                 :        2277 :                 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
     517                 :             :         else
     518                 :        2183 :                 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
     519                 :             : }
     520                 :             : 
     521                 :        2480 : static void mptcp_shutdown_subflows(struct mptcp_sock *msk)
     522                 :             : {
     523                 :        2480 :         struct mptcp_subflow_context *subflow;
     524                 :             : 
     525            [ + ]:        5866 :         mptcp_for_each_subflow(msk, subflow) {
     526                 :        3386 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     527                 :        3386 :                 bool slow;
     528                 :             : 
     529                 :        3386 :                 slow = lock_sock_fast(ssk);
     530                 :        3386 :                 tcp_shutdown(ssk, SEND_SHUTDOWN);
     531                 :        3386 :                 unlock_sock_fast(ssk, slow);
     532                 :             :         }
     533                 :        2480 : }
     534                 :             : 
     535                 :             : /* called under the msk socket lock */
     536                 :      451902 : static bool mptcp_pending_data_fin_ack(struct sock *sk)
     537                 :             : {
     538            [ + ]:      451902 :         struct mptcp_sock *msk = mptcp_sk(sk);
     539                 :             : 
     540                 :      229636 :         return ((1 << sk->sk_state) &
     541    [ +  + ][ + ]:      451902 :                 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
     542       [ + ][ + ]:       83581 :                msk->write_seq == READ_ONCE(msk->snd_una);
     543                 :             : }
     544                 :             : 
     545                 :       12740 : static void mptcp_check_data_fin_ack(struct sock *sk)
     546                 :             : {
     547            [ + ]:       12740 :         struct mptcp_sock *msk = mptcp_sk(sk);
     548                 :             : 
     549                 :             :         /* Look for an acknowledged DATA_FIN */
     550            [ + ]:       12740 :         if (mptcp_pending_data_fin_ack(sk)) {
     551                 :        2566 :                 WRITE_ONCE(msk->snd_data_fin_enable, 0);
     552                 :             : 
     553      [ +  +  - ]:        2566 :                 switch (sk->sk_state) {
     554                 :        1367 :                 case TCP_FIN_WAIT1:
     555                 :        1367 :                         mptcp_set_state(sk, TCP_FIN_WAIT2);
     556                 :        1367 :                         break;
     557                 :        1199 :                 case TCP_CLOSING:
     558                 :             :                 case TCP_LAST_ACK:
     559                 :        1199 :                         mptcp_shutdown_subflows(msk);
     560                 :        1199 :                         mptcp_set_state(sk, TCP_CLOSE);
     561                 :        1199 :                         break;
     562                 :             :                 }
     563                 :             : 
     564                 :        2566 :                 mptcp_close_wake_up(sk);
     565                 :             :         }
     566                 :       12740 : }
     567                 :             : 
     568                 :             : /* can be called with no lock acquired */
     569                 :     1049562 : static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
     570                 :             : {
     571            [ + ]:     1049562 :         struct mptcp_sock *msk = mptcp_sk(sk);
     572                 :             : 
     573   [ +  +  +  + ]:     1083684 :         if (READ_ONCE(msk->rcv_data_fin) &&
                 [ +  + ]
         [ +  +  + ][ + ]
     574       [ + ][ + ]:         158 :             ((1 << inet_sk_state_load(sk)) &
     575                 :             :              (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
     576                 :       33336 :                 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
     577                 :             : 
     578       [ + ][ + ]:       33336 :                 if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
     579       [ + ][ + ]:        3259 :                         if (seq)
     580                 :        2699 :                                 *seq = rcv_data_fin_seq;
     581                 :             : 
     582                 :        3259 :                         return true;
     583                 :             :                 }
     584                 :             :         }
     585                 :             : 
     586                 :             :         return false;
     587                 :             : }
     588                 :             : 
     589                 :        1398 : static void mptcp_set_datafin_timeout(struct sock *sk)
     590                 :             : {
     591                 :        1398 :         struct inet_connection_sock *icsk = inet_csk(sk);
     592                 :        1398 :         u32 rto_min = READ_ONCE(icsk->icsk_rto_min);
     593                 :        1398 :         u32 rto_max = READ_ONCE(icsk->icsk_rto_max);
     594                 :        1398 :         u32 retransmits;
     595                 :             : 
     596                 :             :         /* The sysctls are validated independently: rto_min > rto_max is
     597                 :             :          * possible, guard against ilog2(0).
     598                 :             :          */
     599            [ + ]:        1398 :         retransmits = min_t(u32, icsk->icsk_retransmits,
     600                 :             :                             ilog2(max_t(u32, rto_max / rto_min, 1)));
     601                 :             : 
     602    [ +  + ][ + ]:        1398 :         mptcp_sk(sk)->timer_ival = rto_min << retransmits;
     603                 :        1398 : }
     604                 :             : 
     605                 :     1104187 : static void __mptcp_set_timeout(struct sock *sk, long tout)
     606                 :             : {
     607         [ +  + ]:     1371388 :         mptcp_sk(sk)->timer_ival = tout > 0 ? tout :
     608                 :      267201 :                                    READ_ONCE(inet_csk(sk)->icsk_rto_min);
     609                 :     1104187 : }
     610                 :             : 
     611                 :      228820 : static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
     612                 :             : {
     613                 :     1698503 :         const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     614                 :             : 
     615 [ +  + ][ +  + ]:     1407112 :         return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
     616 [ +  + ][ +  + ]:     2929786 :                tcp_timeout_expires(ssk) - jiffies : 0;
     617                 :             : }
     618                 :             : 
     619                 :      376625 : void mptcp_set_timeout(struct sock *sk)
     620                 :             : {
     621                 :      376625 :         struct mptcp_subflow_context *subflow;
     622                 :      376625 :         long tout = 0;
     623                 :             : 
     624      [ +  +  + ]:      923323 :         mptcp_for_each_subflow(mptcp_sk(sk), subflow)
     625            [ + ]:      970947 :                 tout = max(tout, mptcp_timeout_from_subflow(subflow));
     626                 :      376625 :         __mptcp_set_timeout(sk, tout);
     627                 :      376625 : }
     628                 :             : 
     629                 :        6569 : void __mptcp_subflow_send_ack(struct sock *ssk)
     630                 :             : {
     631            [ + ]:        6569 :         if (tcp_can_send_ack(ssk))
     632                 :        6246 :                 tcp_send_ack(ssk);
     633                 :        6569 : }
     634                 :             : 
     635                 :        5511 : static void mptcp_subflow_send_ack(struct sock *ssk)
     636                 :             : {
     637                 :        5511 :         bool slow;
     638                 :             : 
     639                 :        5511 :         slow = lock_sock_fast(ssk);
     640                 :        5511 :         __mptcp_subflow_send_ack(ssk);
     641                 :        5511 :         unlock_sock_fast(ssk, slow);
     642                 :        5511 : }
     643                 :             : 
     644                 :        3939 : static void mptcp_send_ack(struct mptcp_sock *msk)
     645                 :             : {
     646                 :        3939 :         struct mptcp_subflow_context *subflow;
     647                 :             : 
     648            [ + ]:        9450 :         mptcp_for_each_subflow(msk, subflow)
     649                 :        5511 :                 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
     650                 :        3939 : }
     651                 :             : 
     652                 :      616178 : static void mptcp_subflow_cleanup_rbuf(struct sock *ssk, int copied)
     653                 :             : {
     654                 :      616178 :         bool slow;
     655                 :             : 
     656                 :      616178 :         slow = lock_sock_fast(ssk);
     657            [ + ]:      616178 :         if (tcp_can_send_ack(ssk))
     658                 :      613537 :                 tcp_cleanup_rbuf(ssk, copied);
     659                 :      616178 :         unlock_sock_fast(ssk, slow);
     660                 :      616178 : }
     661                 :             : 
     662                 :     1980573 : static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
     663                 :             : {
     664                 :     1980573 :         const struct inet_connection_sock *icsk = inet_csk(ssk);
     665                 :     1980573 :         u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
     666            [ + ]:     1980573 :         const struct tcp_sock *tp = tcp_sk(ssk);
     667                 :             : 
     668            [ + ]:     1980573 :         return (ack_pending & ICSK_ACK_SCHED) &&
     669                 :     1004036 :                 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
     670         [ +  + ]:     1004036 :                   READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
     671            [ + ]:      247541 :                  (rx_empty && ack_pending &
     672                 :             :                               (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
     673                 :             : }
     674                 :             : 
     675                 :     1546508 : static void mptcp_cleanup_rbuf(struct mptcp_sock *msk, int copied)
     676                 :             : {
     677                 :     1546508 :         int old_space = READ_ONCE(msk->old_wspace);
     678                 :     1546508 :         struct mptcp_subflow_context *subflow;
     679                 :     1546508 :         struct sock *sk = (struct sock *)msk;
     680                 :     1546508 :         int space =  __mptcp_space(sk);
     681                 :     1546508 :         bool cleanup, rx_empty;
     682                 :             : 
     683      [ +  +  + ]:     1546508 :         cleanup = (space > 0) && (space >= (old_space << 1)) && copied;
     684 [ +  + ][ +  + ]:     1546508 :         rx_empty = !sk_rmem_alloc_get(sk) && copied;
     685                 :             : 
     686       [ + ][ + ]:     3799898 :         mptcp_for_each_subflow(msk, subflow) {
     687       [ + ][ + ]:     2253390 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
     688                 :             : 
     689 [ +  + ][ +  + ]:     2253390 :                 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
     690                 :      616178 :                         mptcp_subflow_cleanup_rbuf(ssk, copied);
     691                 :             :         }
     692                 :     1546508 : }
     693                 :             : 
     694                 :      165266 : static void mptcp_check_data_fin(struct sock *sk)
     695                 :             : {
     696            [ + ]:      165266 :         struct mptcp_sock *msk = mptcp_sk(sk);
     697                 :      165266 :         u64 rcv_data_fin_seq;
     698                 :             : 
     699                 :             :         /* Need to ack a DATA_FIN received from a peer while this side
     700                 :             :          * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
     701                 :             :          * msk->rcv_data_fin was set when parsing the incoming options
     702                 :             :          * at the subflow level and the msk lock was not held, so this
     703                 :             :          * is the first opportunity to act on the DATA_FIN and change
     704                 :             :          * the msk state.
     705                 :             :          *
     706                 :             :          * If we are caught up to the sequence number of the incoming
     707                 :             :          * DATA_FIN, send the DATA_ACK now and do state transition.  If
     708                 :             :          * not caught up, do nothing and let the recv code send DATA_ACK
     709                 :             :          * when catching up.
     710                 :             :          */
     711                 :             : 
     712            [ + ]:      165266 :         if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
     713                 :        2699 :                 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
     714                 :        2699 :                 WRITE_ONCE(msk->rcv_data_fin, 0);
     715                 :             : 
     716                 :        2699 :                 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
     717                 :             :                 /* on fallback, also emulated after a reset: check for a FIN */
     718            [ + ]:        2699 :                 if (!__mptcp_check_fallback(msk) ||
     719         [ -  - ]:         158 :                     (msk->first && sock_flag(msk->first, SOCK_DONE)))
     720                 :        2699 :                         sock_set_flag(sk, SOCK_DONE);
     721                 :        2699 :                 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
     722                 :             : 
     723   [ +  +  +  - ]:        2699 :                 switch (sk->sk_state) {
     724                 :             :                 case TCP_ESTABLISHED:
     725                 :        1096 :                         mptcp_set_state(sk, TCP_CLOSE_WAIT);
     726                 :        1096 :                         break;
     727                 :         322 :                 case TCP_FIN_WAIT1:
     728                 :         322 :                         mptcp_set_state(sk, TCP_CLOSING);
     729                 :         322 :                         break;
     730                 :        1281 :                 case TCP_FIN_WAIT2:
     731                 :        1281 :                         mptcp_shutdown_subflows(msk);
     732                 :        1281 :                         mptcp_set_state(sk, TCP_CLOSE);
     733                 :        1281 :                         break;
     734                 :             :                 default:
     735                 :             :                         /* Other states not expected */
     736                 :           0 :                         WARN_ON_ONCE(1);
     737                 :           0 :                         break;
     738                 :             :                 }
     739                 :             : 
     740            [ + ]:        2699 :                 if (!__mptcp_check_fallback(msk))
     741                 :        2541 :                         mptcp_send_ack(msk);
     742                 :        2699 :                 mptcp_close_wake_up(sk);
     743                 :             :         }
     744                 :      165266 : }
     745                 :             : 
     746                 :           0 : static void mptcp_dss_corruption(struct mptcp_sock *msk, struct sock *ssk)
     747                 :             : {
     748       [ # ][ # ]:           0 :         if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSCORRUPTIONFALLBACK)) {
     749       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSCORRUPTIONRESET);
     750                 :           0 :                 mptcp_subflow_reset(ssk);
     751                 :             :         }
     752                 :           0 : }
     753                 :             : 
     754                 :      223919 : static void __mptcp_add_backlog(struct sock *sk,
     755                 :             :                                 struct mptcp_subflow_context *subflow,
     756                 :             :                                 struct sk_buff *skb)
     757                 :             : {
     758            [ + ]:      223919 :         struct mptcp_sock *msk = mptcp_sk(sk);
     759                 :      223919 :         struct sk_buff *tail = NULL;
     760                 :      223919 :         struct sock *ssk = skb->sk;
     761                 :      223919 :         bool fragstolen;
     762                 :      223919 :         u64 limit;
     763                 :      223919 :         int delta;
     764                 :             : 
     765            [ + ]:      223919 :         if (unlikely(sk->sk_state == TCP_CLOSE)) {
     766                 :           1 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE);
     767                 :           2 :                 return;
     768                 :             :         }
     769                 :             : 
     770                 :             :         /* Similar additional allowance as plain TCP. */
     771                 :      223918 :         limit = READ_ONCE(sk->sk_rcvbuf);
     772                 :      223918 :         limit += (limit >> 1) + 64 * 1024;
     773                 :      223918 :         limit = min_t(u64, limit, UINT_MAX);
     774 [ +  - ][ +  - ]:      223918 :         if (msk->backlog_len > limit && !__mptcp_check_fallback(msk)) {
     775       [ # ][ # ]:           0 :                 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_BACKLOGDROP);
     776                 :           0 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_BACKLOG);
     777                 :           0 :                 return;
     778                 :             :         }
     779                 :             : 
     780                 :             :         /* Try to coalesce with the last skb in our backlog */
     781       [ + ][ + ]:      223918 :         if (!list_empty(&msk->backlog_list))
     782                 :       67234 :                 tail = list_last_entry(&msk->backlog_list, struct sk_buff, list);
     783                 :             : 
     784 [ -  + ][ -  + ]:       67234 :         if (tail && MPTCP_SKB_CB(skb)->map_seq == MPTCP_SKB_CB(tail)->end_seq &&
     785 [ +  + ][ +  + ]:      116463 :             ssk == tail->sk &&
     786                 :       55762 :             __mptcp_try_coalesce(sk, tail, skb, &fragstolen, &delta)) {
     787                 :       31692 :                 skb->truesize -= delta;
     788            [ + ]:       31692 :                 kfree_skb_partial(skb, fragstolen);
     789                 :       31692 :                 __mptcp_subflow_lend_fwdmem(subflow, delta);
     790                 :       31692 :                 goto account;
     791                 :             :         }
     792                 :             : 
     793                 :      192226 :         list_add_tail(&skb->list, &msk->backlog_list);
     794                 :      192226 :         mptcp_subflow_lend_fwdmem(subflow, skb);
     795                 :      192226 :         delta = skb->truesize;
     796                 :             : 
     797                 :      223918 : account:
     798                 :      223918 :         WRITE_ONCE(msk->backlog_len, msk->backlog_len + delta);
     799                 :             : }
     800                 :             : 
     801                 :     1088737 : static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
     802                 :             :                                            struct sock *ssk, bool own_msk)
     803                 :             : {
     804            [ - ]:     1088737 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
     805                 :     1088737 :         struct sock *sk = (struct sock *)msk;
     806                 :     1088737 :         bool more_data_avail;
     807                 :     1088737 :         struct tcp_sock *tp;
     808                 :     1088737 :         bool ret = false;
     809                 :             : 
     810         [ -  - ]:     1088737 :         pr_debug("msk=%p ssk=%p\n", msk, ssk);
     811            [ + ]:     1088737 :         tp = tcp_sk(ssk);
     812                 :     1159579 :         do {
     813                 :     1159579 :                 u32 map_remaining, offset;
     814                 :     1159579 :                 u32 seq = tp->copied_seq;
     815                 :     1159579 :                 struct sk_buff *skb;
     816                 :     1159579 :                 bool fin;
     817                 :             : 
     818                 :             :                 /* try to move as much data as available */
     819                 :     2319158 :                 map_remaining = subflow->map_data_len -
     820                 :     1159579 :                                 mptcp_subflow_get_map_offset(subflow);
     821                 :             : 
     822            [ - ]:     1159579 :                 skb = skb_peek(&ssk->sk_receive_queue);
     823            [ - ]:     1159579 :                 if (unlikely(!skb))
     824                 :             :                         break;
     825                 :             : 
     826            [ + ]:     1159579 :                 if (__mptcp_check_fallback(msk)) {
     827                 :             :                         /* Under fallback skbs have no MPTCP extension and TCP could
     828                 :             :                          * collapse them between the dummy map creation and the
     829                 :             :                          * current dequeue. Be sure to adjust the map size.
     830                 :             :                          */
     831                 :       10700 :                         map_remaining = skb->len;
     832                 :       10700 :                         subflow->map_data_len = skb->len;
     833                 :             :                 }
     834                 :             : 
     835                 :     1159579 :                 offset = seq - TCP_SKB_CB(skb)->seq;
     836                 :     1159579 :                 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
     837            [ + ]:     1159579 :                 if (fin)
     838                 :         160 :                         seq++;
     839                 :             : 
     840            [ + ]:     1159579 :                 if (offset < skb->len) {
     841                 :     1159530 :                         size_t len = skb->len - offset;
     842                 :             : 
     843                 :     1159530 :                         mptcp_init_skb(ssk, skb, offset, len);
     844                 :             : 
     845            [ + ]:     1159530 :                         if (own_msk) {
     846                 :      935611 :                                 mptcp_subflow_lend_fwdmem(subflow, skb);
     847                 :      935611 :                                 ret |= __mptcp_move_skb(sk, skb);
     848                 :             :                         } else {
     849                 :      223919 :                                 __mptcp_add_backlog(sk, subflow, skb);
     850                 :             :                         }
     851                 :     1159530 :                         seq += len;
     852                 :             : 
     853            [ + ]:     1159530 :                         if (unlikely(map_remaining < len)) {
     854                 :           0 :                                 DEBUG_NET_WARN_ON_ONCE(1);
     855                 :           0 :                                 mptcp_dss_corruption(msk, ssk);
     856                 :             :                         }
     857                 :             :                 } else {
     858                 :          49 :                         sk_eat_skb(ssk, skb);
     859                 :             : 
     860            [ - ]:          49 :                         if (unlikely(!fin)) {
     861                 :           0 :                                 DEBUG_NET_WARN_ON_ONCE(1);
     862                 :           0 :                                 mptcp_dss_corruption(msk, ssk);
     863                 :             :                         }
     864                 :             :                 }
     865                 :             : 
     866                 :     1159579 :                 WRITE_ONCE(tp->copied_seq, seq);
     867                 :     1159579 :                 more_data_avail = mptcp_subflow_data_available(ssk);
     868                 :             : 
     869            [ + ]:     1159579 :         } while (more_data_avail);
     870                 :             : 
     871            [ + ]:     1088737 :         if (ret)
     872                 :      686799 :                 msk->last_data_recv = tcp_jiffies32;
     873                 :     1088737 :         return ret;
     874                 :             : }
     875                 :             : 
     876                 :     1040980 : static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
     877                 :             : {
     878                 :     1040980 :         struct sock *sk = (struct sock *)msk;
     879                 :     1040980 :         struct sk_buff *skb, *tail;
     880                 :     1040980 :         bool moved = false;
     881                 :     1040980 :         struct rb_node *p;
     882                 :     1040980 :         u64 end_seq;
     883                 :             : 
     884            [ + ]:     1040980 :         p = rb_first(&msk->out_of_order_queue);
     885         [ -  - ]:     1040980 :         pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
     886       [ + ][ + ]:     1095011 :         while (p) {
     887                 :      377357 :                 skb = rb_to_skb(p);
     888       [ + ][ + ]:      377357 :                 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
     889                 :             :                         break;
     890                 :             : 
     891                 :       54031 :                 p = rb_next(p);
     892                 :       54031 :                 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
     893                 :             : 
     894       [ + ][ + ]:       54031 :                 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
     895                 :             :                                       msk->ack_seq))) {
     896                 :         166 :                         mptcp_drop(sk, skb);
     897       [ - ][ - ]:         166 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
     898                 :         166 :                         continue;
     899                 :             :                 }
     900                 :             : 
     901                 :       53865 :                 end_seq = MPTCP_SKB_CB(skb)->end_seq;
     902       [ - ][ - ]:       53865 :                 tail = skb_peek_tail(&sk->sk_receive_queue);
     903 [ -  + ][ -  + ]:      106997 :                 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
     904                 :       33922 :                         int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
     905                 :             : 
     906                 :             :                         /* skip overlapping data, if any */
     907 [ -  - ][ -  - ]:       33922 :                         pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
     908                 :             :                                  MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
     909                 :             :                                  delta);
     910                 :       33922 :                         MPTCP_SKB_CB(skb)->offset += delta;
     911                 :       33922 :                         MPTCP_SKB_CB(skb)->map_seq += delta;
     912                 :       33922 :                         __skb_queue_tail(&sk->sk_receive_queue, skb);
     913                 :             :                 }
     914                 :       53865 :                 msk->bytes_received += end_seq - msk->ack_seq;
     915                 :       53865 :                 WRITE_ONCE(msk->ack_seq, end_seq);
     916                 :       53865 :                 moved = true;
     917                 :             :         }
     918                 :     1040980 :         return moved;
     919                 :             : }
     920                 :             : 
     921                 :           0 : static bool mptcp_errqueue_skb_forwardable(const struct sk_buff *skb)
     922                 :             : {
     923                 :             :         /* Subflow-level ICMP errors are dropped: the legacy RECVERR ABI
     924                 :             :          * cannot convey their per-subflow peer identity.
     925                 :             :          */
     926                 :          17 :         return SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING;
     927                 :             : }
     928                 :             : 
     929                 :        7800 : static bool __mptcp_subflow_splice_errqueue(struct sock *sk, struct sock *ssk)
     930                 :             : {
     931                 :        7800 :         struct sk_buff *skb;
     932                 :        7800 :         bool moved = false;
     933                 :             : 
     934            [ + ]:        7817 :         while ((skb = skb_dequeue(&ssk->sk_error_queue))) {
     935                 :             :                 /* sock_queue_err_skb() re-homes skb->sk onto the parent and
     936                 :             :                  * charges sk_rmem_alloc, bounding the queue by sk_rcvbuf.
     937                 :             :                  */
     938         [ +  - ]:          17 :                 if (!mptcp_errqueue_skb_forwardable(skb) ||
     939                 :           0 :                     sock_queue_err_skb(sk, skb)) {
     940                 :          17 :                         kfree_skb(skb);
     941                 :          17 :                         continue;
     942                 :             :                 }
     943                 :             :                 moved = true;
     944                 :             :         }
     945                 :             : 
     946                 :        7800 :         return moved;
     947                 :             : }
     948                 :             : 
     949                 :        7800 : static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
     950                 :             : {
     951                 :        7800 :         bool propagated = false;
     952                 :        7800 :         int ssk_state;
     953                 :        7800 :         bool report;
     954                 :        7800 :         int err;
     955                 :             : 
     956                 :        7800 :         report = __mptcp_subflow_splice_errqueue(sk, ssk);
     957                 :             : 
     958                 :             :         /* only propagate errors on fallen-back sockets or
     959                 :             :          * on MPC connect
     960                 :             :          */
     961      [ +  +  + ]:        7800 :         if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
     962                 :        7562 :                 goto out;
     963                 :             : 
     964            [ + ]:         238 :         err = sock_error(ssk);
     965       [ + ][ + ]:         146 :         if (!err)
     966                 :         212 :                 goto out;
     967                 :             :         /* We need to propagate only transition to CLOSE state.
     968                 :             :          * Orphaned socket will see such state change via
     969                 :             :          * subflow_sched_work_if_closed() and that path will properly
     970                 :             :          * destroy the msk as needed.
     971                 :             :          */
     972                 :          26 :         ssk_state = inet_sk_state_load(ssk);
     973                 :             :         /* a FIN received before still leads to EOF, as with TCP */
     974 [ +  - ][ +  - ]:          26 :         if (sock_flag(ssk, SOCK_DONE) && mptcp_pending_data_fin(sk, NULL))
     975                 :           0 :                 sock_set_flag(sk, SOCK_DONE);
     976                 :          26 :         WRITE_ONCE(sk->sk_err, -err);
     977 [ -  + ][ -  + ]:          26 :         if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD)) {
     978                 :             :                 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
     979                 :          26 :                 smp_wmb();
     980                 :          26 :                 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | SEND_SHUTDOWN);
     981                 :          26 :                 mptcp_set_state(sk, ssk_state);
     982                 :             :         }
     983                 :             :         report = true;
     984                 :             :         propagated = true;
     985                 :             : 
     986                 :        7774 : out:
     987       [ + ][ + ]:        7774 :         if (report) {
     988                 :             :                 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
     989                 :          26 :                 smp_wmb();
     990                 :          26 :                 sk_error_report(sk);
     991                 :             :         }
     992                 :        7800 :         return propagated;
     993                 :             : }
     994                 :             : 
     995                 :         993 : void __mptcp_error_report(struct sock *sk)
     996                 :             : {
     997                 :         993 :         struct mptcp_subflow_context *subflow;
     998            [ + ]:         993 :         struct mptcp_sock *msk = mptcp_sk(sk);
     999                 :             : 
    1000            [ + ]:        2227 :         mptcp_for_each_subflow(msk, subflow)
    1001            [ + ]:        1260 :                 if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
    1002                 :             :                         break;
    1003                 :         993 : }
    1004                 :             : 
    1005                 :             : /* In most cases we will be able to lock the mptcp socket.  If its already
    1006                 :             :  * owned, we need to defer to the work queue to avoid ABBA deadlock.
    1007                 :             :  */
    1008                 :      884296 : static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
    1009                 :             : {
    1010                 :      884296 :         struct sock *sk = (struct sock *)msk;
    1011                 :      884296 :         bool moved;
    1012                 :             : 
    1013                 :      884296 :         moved = __mptcp_move_skbs_from_subflow(msk, ssk, true);
    1014                 :      884296 :         __mptcp_ofo_queue(msk);
    1015            [ + ]:      884296 :         if (unlikely(READ_ONCE(ssk->sk_err)))
    1016                 :           0 :                 __mptcp_subflow_error_report(sk, ssk);
    1017                 :             : 
    1018                 :             :         /* If the moves have caught up with the DATA_FIN sequence number
    1019                 :             :          * it's time to ack the DATA_FIN and change socket state, but
    1020                 :             :          * this is not a good place to change state. Let the workqueue
    1021                 :             :          * do it.
    1022                 :             :          */
    1023            [ + ]:      884296 :         if (mptcp_pending_data_fin(sk, NULL))
    1024                 :         560 :                 mptcp_schedule_work(sk);
    1025                 :      884296 :         return moved;
    1026                 :             : }
    1027                 :             : 
    1028                 :     1088737 : static void mptcp_rcv_rtt_update(struct mptcp_sock *msk,
    1029                 :             :                                  struct mptcp_subflow_context *subflow)
    1030                 :             : {
    1031            [ + ]:     1088737 :         const struct tcp_sock *tp = tcp_sk(subflow->tcp_sock);
    1032                 :     1088737 :         u32 rtt_us = tp->rcv_rtt_est.rtt_us;
    1033                 :     1088737 :         u8 scaling_ratio;
    1034                 :     1088737 :         int id;
    1035                 :             : 
    1036                 :             :         /* Update once per subflow per rcvwnd to avoid touching the msk
    1037                 :             :          * too often.
    1038                 :             :          */
    1039 [ +  + ][ +  + ]:     1088737 :         if (!rtt_us || tp->rcv_rtt_est.seq == subflow->prev_rtt_seq)
    1040                 :             :                 return;
    1041                 :             : 
    1042                 :       24673 :         subflow->prev_rtt_seq = tp->rcv_rtt_est.seq;
    1043                 :             : 
    1044                 :             :         /* Pairs with READ_ONCE() in mptcp_rtt_us_est(). */
    1045                 :       24673 :         id = msk->rcv_rtt_est.next_sample;
    1046                 :       24673 :         WRITE_ONCE(msk->rcv_rtt_est.samples[id], rtt_us);
    1047       [ + ][ + ]:       24673 :         if (++msk->rcv_rtt_est.next_sample == MPTCP_RTT_SAMPLES)
    1048                 :        4431 :                 msk->rcv_rtt_est.next_sample = 0;
    1049                 :             : 
    1050                 :             :         /* EWMA among the incoming subflows.
    1051                 :             :          * Pairs with READ_ONCE() in mptcp_win_from_space().
    1052                 :             :          */
    1053                 :       24673 :         scaling_ratio = READ_ONCE(msk->scaling_ratio);
    1054                 :       24673 :         WRITE_ONCE(msk->scaling_ratio,
    1055                 :             :                    ((scaling_ratio << 3) - scaling_ratio +
    1056                 :             :                     tp->scaling_ratio) >> 3);
    1057                 :             : }
    1058                 :             : 
    1059                 :     1088737 : void mptcp_data_ready(struct sock *sk, struct sock *ssk)
    1060                 :             : {
    1061            [ + ]:     1088737 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1062            [ + ]:     1088737 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1063                 :             : 
    1064                 :             :         /* The peer can send data while we are shutting down this
    1065                 :             :          * subflow at subflow destruction time, but we must avoid enqueuing
    1066                 :             :          * more data to the msk receive queue
    1067                 :             :          */
    1068       [ - ][ - ]:     1088737 :         if (unlikely(subflow->closing))
    1069                 :             :                 return;
    1070                 :             : 
    1071                 :     1088737 :         mptcp_data_lock(sk);
    1072                 :     1088737 :         mptcp_rcv_rtt_update(msk, subflow);
    1073       [ + ][ + ]:     1088737 :         if (!sock_owned_by_user(sk)) {
    1074                 :             :                 /* Wake-up the reader only for in-sequence data */
    1075 [ +  - ][ +  - ]:      884296 :                 if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
    1076                 :      686799 :                         sk->sk_data_ready(sk);
    1077                 :             :         } else {
    1078                 :      204441 :                 __mptcp_move_skbs_from_subflow(msk, ssk, false);
    1079                 :             :         }
    1080                 :     1088737 :         mptcp_data_unlock(sk);
    1081                 :             : }
    1082                 :             : 
    1083                 :        1278 : static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
    1084                 :             : {
    1085       [ + ][ + ]:        1278 :         mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
    1086                 :        1278 :         msk->allow_infinite_fallback = false;
    1087                 :             :         /* an active join is added once fully established, see options.c */
    1088       [ + ][ + ]:        1278 :         if (!mptcp_subflow_ctx(ssk)->request_join)
    1089                 :         648 :                 set_bit(MPTCP_JOINED, &msk->flags);
    1090                 :        1278 :         mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
    1091                 :        1278 : }
    1092                 :             : 
    1093                 :         648 : static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
    1094                 :             : {
    1095                 :         648 :         struct sock *sk = (struct sock *)msk;
    1096                 :             : 
    1097            [ + ]:         648 :         if (sk->sk_state != TCP_ESTABLISHED)
    1098                 :             :                 return false;
    1099                 :             : 
    1100                 :         648 :         spin_lock_bh(&msk->fallback_lock);
    1101    [ +  + ][ + ]:         648 :         if (!msk->allow_subflows) {
    1102                 :           0 :                 spin_unlock_bh(&msk->fallback_lock);
    1103                 :           0 :                 return false;
    1104                 :             :         }
    1105                 :         648 :         mptcp_subflow_joined(msk, ssk);
    1106                 :         648 :         spin_unlock_bh(&msk->fallback_lock);
    1107                 :             : 
    1108                 :         648 :         mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
    1109                 :         648 :         mptcp_sockopt_sync_locked(msk, ssk);
    1110                 :         648 :         mptcp_stop_tout_timer(sk);
    1111       [ - ][ - ]:         648 :         __mptcp_propagate_sndbuf(sk, ssk);
    1112                 :             :         return true;
    1113                 :             : }
    1114                 :             : 
    1115                 :          93 : static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
    1116                 :             : {
    1117                 :          93 :         struct mptcp_subflow_context *tmp, *subflow;
    1118            [ + ]:          93 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1119                 :             : 
    1120       [ + ][ + ]:         144 :         list_for_each_entry_safe(subflow, tmp, join_list, node) {
    1121                 :          51 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    1122                 :          51 :                 bool slow = lock_sock_fast(ssk);
    1123                 :             : 
    1124                 :          51 :                 list_move_tail(&subflow->node, &msk->conn_list);
    1125       [ + ][ + ]:          51 :                 if (!__mptcp_finish_join(msk, ssk))
    1126                 :           0 :                         mptcp_subflow_reset(ssk);
    1127                 :          51 :                 unlock_sock_fast(ssk, slow);
    1128                 :             :         }
    1129                 :          93 : }
    1130                 :             : 
    1131                 :      264795 : static bool mptcp_rtx_timer_pending(struct sock *sk)
    1132                 :             : {
    1133 [ +  + ][ +  + ]:      884807 :         return timer_pending(&sk->mptcp_retransmit_timer);
    1134                 :             : }
    1135                 :             : 
    1136                 :      468074 : static void mptcp_reset_rtx_timer(struct sock *sk)
    1137                 :             : {
    1138            [ + ]:      468074 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1139                 :      468074 :         unsigned long tout;
    1140                 :             : 
    1141                 :             :         /* Prevent rescheduling on close and in case of fallback. */
    1142   [ +  -  -  + ]:      663520 :         if (!test_bit(MPTCP_RTX_ENABLED, &msk->flags))
           [ +  -  -  + ]
    1143                 :             :                 return;
    1144                 :             : 
    1145                 :      424785 :         tout = msk->timer_ival;
    1146                 :      424785 :         sk_reset_timer(sk, &sk->mptcp_retransmit_timer, jiffies + tout);
    1147                 :             : }
    1148                 :             : 
    1149                 :       17692 : bool mptcp_schedule_work(struct sock *sk)
    1150                 :             : {
    1151            [ + ]:       17692 :         if (inet_sk_state_load(sk) == TCP_CLOSE)
    1152                 :             :                 return false;
    1153                 :             : 
    1154                 :             :         /* Get a reference on this socket, mptcp_worker() will release it.
    1155                 :             :          * As mptcp_worker() might complete before us, we can not avoid
    1156                 :             :          * a sock_hold()/sock_put() if schedule_work() returns false.
    1157                 :             :          */
    1158                 :       15841 :         sock_hold(sk);
    1159                 :             : 
    1160         [ +  + ]:       15841 :         if (schedule_work(&mptcp_sk(sk)->work))
    1161                 :             :                 return true;
    1162                 :             : 
    1163                 :        1774 :         sock_put(sk);
    1164                 :        1774 :         return false;
    1165                 :             : }
    1166                 :             : 
    1167                 :      848019 : static bool mptcp_skb_can_collapse_to(u64 write_seq,
    1168                 :             :                                       const struct sk_buff *skb,
    1169                 :             :                                       const struct mptcp_ext *mpext)
    1170                 :             : {
    1171            [ + ]:      848019 :         if (!tcp_skb_can_collapse_to(skb))
    1172                 :             :                 return false;
    1173                 :             : 
    1174                 :             :         /* can collapse only if MPTCP level sequence is in order and this
    1175                 :             :          * mapping has not been xmitted yet
    1176                 :             :          */
    1177         [ -  + ]:     1584008 :         return mpext && mpext->data_seq + mpext->data_len == write_seq &&
    1178            [ + ]:      747299 :                !mpext->frozen;
    1179                 :             : }
    1180                 :             : 
    1181                 :             : /* we can append data to the given data frag if:
    1182                 :             :  * - there is space available in the backing page_frag
    1183                 :             :  * - the data frag tail matches the current page_frag free offset
    1184                 :             :  * - the data frag end sequence number matches the current write seq
    1185                 :             :  */
    1186                 :      829789 : static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
    1187                 :             :                                        const struct page_frag *pfrag,
    1188                 :             :                                        const struct mptcp_data_frag *df)
    1189                 :             : {
    1190            [ - ]:      384814 :         return df && !df->eor &&
    1191            [ - ]:      384814 :                 pfrag->page == df->page &&
    1192            [ + ]:      384814 :                 pfrag->size - pfrag->offset > 0 &&
    1193         [ +  - ]:     1052656 :                 pfrag->offset == (df->offset + df->data_len) &&
    1194            [ + ]:      222867 :                 df->data_seq + df->data_len == msk->write_seq;
    1195                 :             : }
    1196                 :             : 
    1197                 :      206399 : static void dfrag_uncharge(struct sock *sk, int len)
    1198                 :             : {
    1199                 :      885966 :         sk_mem_uncharge(sk, len);
    1200                 :      885966 :         sk_wmem_queued_add(sk, -len);
    1201                 :      279059 : }
    1202                 :             : 
    1203                 :      606907 : static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
    1204                 :             : {
    1205                 :      606907 :         int len = dfrag->data_len + dfrag->overhead;
    1206                 :             : 
    1207                 :      606907 :         list_del(&dfrag->list);
    1208                 :      606907 :         dfrag_uncharge(sk, len);
    1209                 :      606907 :         put_page(dfrag->page);
    1210                 :      606907 : }
    1211                 :             : 
    1212                 :             : /* called under both the msk socket lock and the data lock */
    1213                 :      439162 : static void __mptcp_clean_una(struct sock *sk)
    1214                 :             : {
    1215            [ + ]:      439162 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1216                 :      439162 :         struct mptcp_data_frag *dtmp, *dfrag;
    1217                 :      439162 :         u64 snd_una;
    1218                 :             : 
    1219                 :      439162 :         snd_una = msk->snd_una;
    1220       [ + ][ + ]:     1045842 :         list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
    1221       [ + ][ + ]:      984844 :                 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
    1222                 :             :                         break;
    1223                 :             : 
    1224       [ + ][ + ]:      606680 :                 if (unlikely(dfrag == msk->first_pending)) {
    1225                 :             :                         /* in recovery mode can see ack after the current snd head */
    1226    [ #  # ][ # ]:           0 :                         if (WARN_ON_ONCE(!msk->recovery))
    1227                 :             :                                 break;
    1228                 :             : 
    1229                 :           0 :                         msk->first_pending = mptcp_send_next(sk);
    1230                 :             :                 }
    1231                 :             : 
    1232                 :      606680 :                 dfrag_clear(sk, dfrag);
    1233                 :             :         }
    1234                 :             : 
    1235                 :      439162 :         dfrag = mptcp_rtx_head(sk);
    1236 [ +  + ][ +  + ]:      439162 :         if (dfrag && after64(snd_una, dfrag->data_seq)) {
    1237                 :      279059 :                 u64 delta = snd_una - dfrag->data_seq;
    1238                 :             : 
    1239                 :             :                 /* prevent wrap around in recovery mode */
    1240       [ + ][ + ]:      279059 :                 if (unlikely(delta > dfrag->already_sent)) {
    1241    [ #  # ][ # ]:           0 :                         if (WARN_ON_ONCE(!msk->recovery))
    1242                 :           0 :                                 goto out;
    1243 [ #  # ][ #  # ]:           0 :                         if (WARN_ON_ONCE(delta > dfrag->data_len))
    1244                 :           0 :                                 goto out;
    1245                 :           0 :                         dfrag->already_sent += delta - dfrag->already_sent;
    1246                 :             :                 }
    1247                 :             : 
    1248                 :      279059 :                 dfrag->data_seq += delta;
    1249                 :      279059 :                 dfrag->offset += delta;
    1250                 :      279059 :                 dfrag->data_len -= delta;
    1251                 :      279059 :                 dfrag->already_sent -= delta;
    1252                 :             : 
    1253                 :      279059 :                 dfrag_uncharge(sk, delta);
    1254                 :             :         }
    1255                 :             : 
    1256                 :             :         /* all retransmitted data acked, recovery completed */
    1257      [ +  +  + ]:      439162 :         if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
                 [ +  + ]
    1258                 :          57 :                 msk->recovery = false;
    1259                 :             : 
    1260                 :      392834 : out:
    1261 [ +  + ][ +  + ]:      439162 :         if (snd_una == msk->snd_nxt && snd_una == msk->write_seq) {
    1262    [ +  + ][ + ]:       58921 :                 if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
    1263                 :       38254 :                         mptcp_stop_rtx_timer(sk);
    1264                 :             :         } else {
    1265                 :      380241 :                 mptcp_reset_rtx_timer(sk);
    1266                 :             :         }
    1267                 :             : 
    1268       [ + ][ + ]:      439162 :         if (mptcp_pending_data_fin_ack(sk))
    1269                 :        2668 :                 mptcp_schedule_work(sk);
    1270                 :      439162 : }
    1271                 :             : 
    1272                 :       95362 : static void __mptcp_clean_una_wakeup(struct sock *sk)
    1273                 :             : {
    1274         [ -  + ]:      144245 :         lockdep_assert_held_once(&sk->sk_lock.slock);
    1275                 :             : 
    1276                 :      144245 :         __mptcp_clean_una(sk);
    1277                 :      126298 :         mptcp_write_space(sk);
    1278                 :      140896 : }
    1279                 :             : 
    1280                 :           0 : static void mptcp_enter_memory_pressure(struct sock *sk)
    1281                 :             : {
    1282                 :           0 :         struct mptcp_subflow_context *subflow;
    1283            [ # ]:           0 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1284                 :           0 :         bool first = true;
    1285                 :             : 
    1286            [ # ]:           0 :         mptcp_for_each_subflow(msk, subflow) {
    1287            [ # ]:           0 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    1288                 :             : 
    1289         [ #  # ]:           0 :                 if (first && !ssk->sk_bypass_prot_mem) {
    1290                 :           0 :                         tcp_enter_memory_pressure(ssk);
    1291                 :           0 :                         first = false;
    1292                 :             :                 }
    1293                 :             : 
    1294                 :           0 :                 sk_stream_moderate_sndbuf(ssk);
    1295                 :             :         }
    1296                 :           0 :         __mptcp_sync_sndbuf(sk);
    1297                 :           0 : }
    1298                 :             : 
    1299                 :             : /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
    1300                 :             :  * data
    1301                 :             :  */
    1302                 :      606922 : static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
    1303                 :             : {
    1304            [ + ]:      606922 :         if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
    1305                 :             :                                         pfrag, sk->sk_allocation)))
    1306                 :             :                 return true;
    1307                 :             : 
    1308                 :           0 :         mptcp_enter_memory_pressure(sk);
    1309                 :           0 :         return false;
    1310                 :             : }
    1311                 :             : 
    1312                 :             : static struct mptcp_data_frag *
    1313                 :      606922 : mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
    1314                 :             :                       int orig_offset)
    1315                 :             : {
    1316                 :      606922 :         int offset = ALIGN(orig_offset, sizeof(long));
    1317                 :      606922 :         struct mptcp_data_frag *dfrag;
    1318                 :             : 
    1319                 :      606922 :         dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
    1320                 :      606922 :         dfrag->data_len = 0;
    1321                 :      606922 :         dfrag->data_seq = msk->write_seq;
    1322                 :      606922 :         dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
    1323                 :      606922 :         dfrag->offset = offset + sizeof(struct mptcp_data_frag);
    1324                 :      606922 :         dfrag->already_sent = 0;
    1325                 :      606922 :         dfrag->page = pfrag->page;
    1326                 :      606922 :         dfrag->eor = 0;
    1327                 :             : 
    1328                 :      606922 :         return dfrag;
    1329                 :             : }
    1330                 :             : 
    1331                 :             : struct mptcp_sendmsg_info {
    1332                 :             :         int mss_now;
    1333                 :             :         int size_goal;
    1334                 :             :         u16 limit;
    1335                 :             :         u16 sent;
    1336                 :             :         unsigned int flags;
    1337                 :             :         bool data_lock_held;
    1338                 :             : };
    1339                 :             : 
    1340                 :     1322375 : static size_t mptcp_check_allowed_size(const struct mptcp_sock *msk,
    1341                 :             :                                        struct sock *ssk, u64 data_seq,
    1342                 :             :                                        size_t avail_size)
    1343                 :             : {
    1344                 :     1322375 :         u64 window_end = mptcp_wnd_end(msk);
    1345                 :     1322375 :         u64 mptcp_snd_wnd;
    1346                 :             : 
    1347       [ + ][ + ]:     1322375 :         if (__mptcp_check_fallback(msk))
    1348                 :             :                 return avail_size;
    1349                 :             : 
    1350                 :     1263071 :         mptcp_snd_wnd = window_end - data_seq;
    1351                 :     1263071 :         avail_size = min(mptcp_snd_wnd, avail_size);
    1352                 :             : 
    1353 [ +  + ][ +  + ]:     1263071 :         if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
    1354            [ + ]:       12077 :                 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
    1355       [ - ][ - ]:       12077 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
    1356                 :             :         }
    1357                 :             : 
    1358                 :             :         return avail_size;
    1359                 :             : }
    1360                 :             : 
    1361                 :      635625 : static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
    1362                 :             : {
    1363                 :      635625 :         struct skb_ext *mpext = __skb_ext_alloc(gfp);
    1364                 :             : 
    1365            [ - ]:      635625 :         if (!mpext)
    1366                 :             :                 return false;
    1367                 :      635625 :         __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
    1368                 :      635625 :         return true;
    1369                 :             : }
    1370                 :             : 
    1371                 :      635625 : static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
    1372                 :             : {
    1373                 :      635625 :         struct sk_buff *skb;
    1374                 :             : 
    1375                 :      635625 :         skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
    1376            [ - ]:      635625 :         if (likely(skb)) {
    1377            [ - ]:      635625 :                 if (likely(__mptcp_add_ext(skb, gfp))) {
    1378                 :      635625 :                         skb_reserve(skb, MAX_TCP_HEADER);
    1379                 :      635625 :                         skb->ip_summed = CHECKSUM_PARTIAL;
    1380                 :      635625 :                         INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
    1381                 :      635625 :                         return skb;
    1382                 :             :                 }
    1383                 :           0 :                 __kfree_skb(skb);
    1384                 :             :         } else {
    1385                 :           0 :                 mptcp_enter_memory_pressure(sk);
    1386                 :             :         }
    1387                 :             :         return NULL;
    1388                 :             : }
    1389                 :             : 
    1390                 :      635625 : static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
    1391                 :             : {
    1392                 :      635625 :         struct sk_buff *skb;
    1393                 :             : 
    1394                 :      635625 :         skb = __mptcp_do_alloc_tx_skb(sk, gfp);
    1395            [ + ]:      635625 :         if (!skb)
    1396                 :             :                 return NULL;
    1397                 :             : 
    1398                 :             :         /* like TCP, force the charge if nothing is queued */
    1399            [ + ]:      635625 :         if (tcp_rtx_and_write_queues_empty(ssk)) {
    1400                 :       94538 :                 sk_forced_mem_schedule(ssk, skb->truesize);
    1401            [ + ]:      541087 :         } else if (unlikely(!sk_wmem_schedule(ssk, skb->truesize))) {
    1402                 :           0 :                 tcp_skb_tsorted_anchor_cleanup(skb);
    1403                 :           0 :                 kfree_skb(skb);
    1404                 :           0 :                 return NULL;
    1405                 :             :         }
    1406                 :             : 
    1407                 :      635625 :         tcp_skb_entail(ssk, skb);
    1408                 :      635625 :         return skb;
    1409                 :             : }
    1410                 :             : 
    1411                 :       97215 : static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
    1412                 :             : {
    1413                 :      399345 :         gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
    1414                 :             : 
    1415                 :      635625 :         return __mptcp_alloc_tx_skb(sk, ssk, gfp);
    1416                 :             : }
    1417                 :             : 
    1418                 :             : /* note: this always recompute the csum on the whole skb, even
    1419                 :             :  * if we just appended a single frag. More status info needed
    1420                 :             :  */
    1421                 :      178742 : static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
    1422                 :             : {
    1423            [ - ]:      178742 :         struct mptcp_ext *mpext = mptcp_get_ext(skb);
    1424                 :      178742 :         __wsum csum = ~csum_unfold(mpext->csum);
    1425                 :      178742 :         int offset = skb->len - added;
    1426                 :             : 
    1427       [ # ][ # ]:      178742 :         mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
    1428                 :      178742 : }
    1429                 :             : 
    1430                 :           2 : static void mptcp_update_infinite_map(struct mptcp_sock *msk,
    1431                 :             :                                       struct sock *ssk,
    1432                 :             :                                       struct mptcp_ext *mpext)
    1433                 :             : {
    1434       [ - ][ - ]:           2 :         if (!mpext)
    1435                 :             :                 return;
    1436                 :             : 
    1437                 :           2 :         mpext->infinite_map = 1;
    1438                 :           2 :         mpext->data_len = 0;
    1439                 :             : 
    1440       [ + ][ + ]:           2 :         if (!mptcp_try_fallback(ssk, MPTCP_MIB_INFINITEMAPTX)) {
    1441       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
    1442                 :           0 :                 mptcp_subflow_reset(ssk);
    1443                 :           0 :                 return;
    1444                 :             :         }
    1445                 :             : 
    1446                 :           2 :         mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
    1447                 :             : }
    1448                 :             : 
    1449                 :             : #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
    1450                 :             : 
    1451                 :     1322375 : static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
    1452                 :             :                               struct mptcp_data_frag *dfrag,
    1453                 :             :                               struct mptcp_sendmsg_info *info)
    1454                 :             : {
    1455            [ + ]:     1322375 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1456                 :     1322375 :         u64 data_seq = dfrag->data_seq + info->sent;
    1457                 :     1322375 :         int offset = dfrag->offset + info->sent;
    1458            [ + ]:     1322375 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1459                 :     1322375 :         bool zero_window_probe = false;
    1460                 :     1322375 :         struct mptcp_ext *mpext = NULL;
    1461                 :     1322375 :         bool can_coalesce = false;
    1462                 :     1322375 :         bool reuse_skb = true;
    1463                 :     1322375 :         struct sk_buff *skb;
    1464                 :     1322375 :         size_t copy;
    1465                 :     1322375 :         int avail;
    1466                 :     1322375 :         int i;
    1467                 :             : 
    1468         [ -  - ]:     1322375 :         pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
    1469                 :             :                  msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
    1470                 :             : 
    1471         [ -  + ]:     1322375 :         if (WARN_ON_ONCE(info->sent > info->limit ||
    1472                 :             :                          info->limit > dfrag->data_len))
    1473                 :           0 :                 return 0;
    1474                 :             : 
    1475       [ + ][ - ]:     1322375 :         if (unlikely(!__tcp_can_send(ssk)))
    1476                 :             :                 return -EAGAIN;
    1477                 :             : 
    1478                 :             :         /* compute send limit */
    1479       [ + ][ + ]:     1322375 :         if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
    1480                 :           0 :                 ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
    1481                 :     1322375 :         info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
    1482                 :     1322375 :         copy = info->size_goal;
    1483                 :             : 
    1484                 :             :         /* Once the infinite mapping is sent, the MSS no longer includes its DSS
    1485                 :             :          * option: send it in a single segment.
    1486                 :             :          */
    1487 [ +  - ][ +  - ]:     1322375 :         if (unlikely(mptcp_subflow_ctx(ssk)->send_infinite_map &&
    1488                 :             :                      !mptcp_subflow_ctx(ssk)->old_data_pending))
    1489                 :           2 :                 copy = info->mss_now;
    1490                 :             : 
    1491                 :             :         /* after a fallback, the subflow got all data below snd_nxt already */
    1492 [ +  + ][ +  + ]:     1322375 :         if (unlikely(__mptcp_check_fallback(msk) &&
    1493                 :             :                      before64(data_seq, msk->snd_nxt)))
    1494                 :           0 :                 return min_t(u64, msk->snd_nxt - data_seq,
    1495                 :             :                              info->limit - info->sent);
    1496                 :             : 
    1497       [ + ][ + ]:     1322375 :         skb = tcp_write_queue_tail(ssk);
    1498 [ -  + ][ -  + ]:     1136447 :         if (skb && copy > skb->len) {
    1499                 :             :                 /* Limit the write to the size available in the
    1500                 :             :                  * current skb, if any, so that we create at most a new skb.
    1501                 :             :                  * Explicitly tells TCP internals to avoid collapsing on later
    1502                 :             :                  * queue management operation, to avoid breaking the ext <->
    1503                 :             :                  * SSN association set here
    1504                 :             :                  */
    1505            [ - ]:      848019 :                 mpext = mptcp_get_ext(skb);
    1506            [ + ]:      848019 :                 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
    1507                 :      155370 :                         TCP_SKB_CB(skb)->eor = 1;
    1508            [ + ]:      155370 :                         tcp_mark_push(tcp_sk(ssk), skb);
    1509                 :      155370 :                         goto alloc_skb;
    1510                 :             :                 }
    1511                 :             : 
    1512            [ + ]:      692649 :                 i = skb_shinfo(skb)->nr_frags;
    1513            [ + ]:      692649 :                 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
    1514 [ +  + ][ +  + ]:      692649 :                 if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
    1515       [ + ][ + ]:        5899 :                         tcp_mark_push(tcp_sk(ssk), skb);
    1516                 :        5899 :                         goto alloc_skb;
    1517                 :             :                 }
    1518                 :             : 
    1519                 :      686750 :                 copy -= skb->len;
    1520                 :             :         } else {
    1521       [ + ][ + ]:       97215 : alloc_skb:
    1522    [ +  + ][ + ]:      635625 :                 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
    1523       [ + ][ + ]:      635625 :                 if (!skb)
    1524                 :             :                         return -ENOMEM;
    1525                 :             : 
    1526            [ - ]:      635625 :                 i = skb_shinfo(skb)->nr_frags;
    1527                 :      635625 :                 reuse_skb = false;
    1528            [ - ]:      635625 :                 mpext = mptcp_get_ext(skb);
    1529                 :             :         }
    1530                 :             : 
    1531                 :             :         /* Zero window and all data acked? Probe. */
    1532                 :     1322375 :         copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
    1533       [ + ][ + ]:     1322375 :         if (copy == 0) {
    1534                 :      330908 :                 u64 snd_una = READ_ONCE(msk->snd_una);
    1535                 :             : 
    1536                 :             :                 /* No need for zero probe if there are any data pending
    1537                 :             :                  * either at the msk or ssk level; skb is the current write
    1538                 :             :                  * queue tail and can be empty at this point.
    1539                 :             :                  */
    1540      [ +  -  + ]:      342461 :                 if (snd_una != msk->snd_nxt || skb->len ||
              [ +  -  + ]
         [ +  - ][ +  - ]
    1541       [ + ][ + ]:         200 :                     skb != tcp_send_head(ssk)) {
    1542                 :      328611 :                         tcp_remove_empty_skb(ssk);
    1543                 :      328611 :                         return 0;
    1544                 :             :                 }
    1545                 :             : 
    1546                 :             :                 /* a new probe would move old_data_end: one at a time */
    1547       [ + ][ + ]:        2297 :                 if (!tcp_rtx_queue_empty(ssk)) {
    1548                 :           0 :                         tcp_remove_empty_skb(ssk);
    1549                 :           0 :                         return 0;
    1550                 :             :                 }
    1551                 :             : 
    1552                 :        2297 :                 zero_window_probe = true;
    1553                 :        2297 :                 data_seq = snd_una - 1;
    1554                 :        2297 :                 copy = 1;
    1555                 :             :         }
    1556                 :             : 
    1557                 :             :         /* data acked without MPTCP opts: the peer may have fallen back, only
    1558                 :             :          * possible before a join, see RFC 8684
    1559                 :             :          */
    1560   [ +  +  -  -  :      997649 :         if (unlikely(before64(data_seq, msk->snd_nxt) &&
           +  +  + ][ +  
          +  -  -  +  +  
                      + ]
    1561                 :             :                      !test_bit(MPTCP_JOINED, &msk->flags) &&
    1562                 :             :                      after(tcp_sk(ssk)->snd_una, subflow->opt_ack_seq))) {
    1563                 :           0 :                 tcp_remove_empty_skb(ssk);
    1564       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
    1565                 :           0 :                 subflow->reset_transient = 0;
    1566                 :           0 :                 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
    1567                 :           0 :                 mptcp_subflow_reset(ssk);
    1568                 :           0 :                 return -EAGAIN;
    1569                 :             :         }
    1570                 :             : 
    1571                 :      993764 :         copy = min_t(size_t, copy, info->limit - info->sent);
    1572                 :             : 
    1573                 :             :         /* like TCP, allow some progress up to tcp_wmem[0] */
    1574                 :      993764 :         avail = tcp_wmem_schedule(ssk, copy);
    1575       [ + ][ + ]:      993764 :         if (avail <= 0) {
    1576                 :           0 :                 tcp_remove_empty_skb(ssk);
    1577                 :           0 :                 return -ENOMEM;
    1578                 :             :         }
    1579                 :      993764 :         copy = avail;
    1580                 :             : 
    1581       [ + ][ + ]:      993764 :         if (can_coalesce) {
    1582                 :       54291 :                 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
    1583                 :             :         } else {
    1584                 :      939473 :                 get_page(dfrag->page);
    1585            [ + ]:     1159568 :                 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
    1586                 :             :         }
    1587                 :             : 
    1588                 :      993764 :         skb->len += copy;
    1589                 :      993764 :         skb->data_len += copy;
    1590                 :      993764 :         skb->truesize += copy;
    1591       [ - ][ - ]:      993764 :         sk_wmem_queued_add(ssk, copy);
    1592       [ - ][ - ]:      993764 :         sk_mem_charge(ssk, copy);
    1593 [ +  + ][ +  + ]:      993764 :         WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
    1594                 :      993764 :         TCP_SKB_CB(skb)->end_seq += copy;
    1595       [ + ][ + ]:      993764 :         tcp_skb_pcount_set(skb, 0);
    1596                 :             : 
    1597                 :             :         /* old data mapped again: no fallback until acked with MPTCP opts */
    1598       [ + ][ + ]:      993764 :         if (unlikely(before64(data_seq, msk->snd_nxt))) {
    1599       [ + ][ + ]:       21652 :                 mptcp_subflow_ctx(ssk)->old_data_end = tcp_sk(ssk)->write_seq;
    1600                 :       21652 :                 mptcp_subflow_ctx(ssk)->old_data_pending = 1;
    1601                 :             :         }
    1602                 :             : 
    1603                 :             :         /* on skb reuse we just need to update the DSS len */
    1604       [ + ][ + ]:      993764 :         if (reuse_skb) {
    1605                 :      443181 :                 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
    1606                 :      443181 :                 mpext->data_len += copy;
    1607                 :      443181 :                 goto out;
    1608                 :             :         }
    1609                 :             : 
    1610                 :      550583 :         memset(mpext, 0, sizeof(*mpext));
    1611                 :      550583 :         mpext->data_seq = data_seq;
    1612       [ - ][ - ]:      550583 :         mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
    1613                 :      550583 :         mpext->data_len = copy;
    1614                 :      550583 :         mpext->use_map = 1;
    1615                 :      550583 :         mpext->dsn64 = 1;
    1616                 :             : 
    1617 [ -  - ][ -  - ]:      550583 :         pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
    1618                 :             :                  mpext->data_seq, mpext->subflow_seq, mpext->data_len,
    1619                 :             :                  mpext->dsn64);
    1620                 :             : 
    1621       [ + ][ + ]:      550583 :         if (zero_window_probe) {
    1622       [ - ][ - ]:        2297 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_WINPROBE);
    1623       [ + ][ + ]:        2297 :                 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
    1624                 :        2297 :                 mpext->frozen = 1;
    1625    [ +  + ][ + ]:        2297 :                 if (READ_ONCE(msk->csum_enabled))
    1626                 :        1632 :                         mptcp_update_data_checksum(skb, copy);
    1627                 :        2297 :                 tcp_push_pending_frames(ssk);
    1628                 :        2297 :                 return 0;
    1629                 :             :         }
    1630                 :      548286 : out:
    1631    [ +  + ][ + ]:      991467 :         if (READ_ONCE(msk->csum_enabled))
    1632                 :      177110 :                 mptcp_update_data_checksum(skb, copy);
    1633                 :             : 
    1634                 :             :         /* the infinite map implies a fallback: wait for old data acks */
    1635       [ + ][ + ]:      991467 :         if (mptcp_subflow_ctx(ssk)->send_infinite_map &&
    1636       [ - ][ - ]:           2 :             !mptcp_subflow_ctx(ssk)->old_data_pending)
    1637                 :           2 :                 mptcp_update_infinite_map(msk, ssk, mpext);
    1638                 :      991467 :         trace_mptcp_sendmsg_frag(mpext);
    1639       [ + ][ + ]:      991467 :         mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
    1640                 :             : 
    1641                 :             :         /* if this is the last chunk of a dfrag with MSG_EOR set,
    1642                 :             :          * mark the skb to prevent coalescing with subsequent data.
    1643                 :             :          */
    1644 [ +  + ][ +  + ]:      991467 :         if (dfrag->eor && info->sent + copy >= dfrag->data_len)
    1645                 :           6 :                 TCP_SKB_CB(skb)->eor = 1;
    1646                 :             : 
    1647                 :             :         return copy;
    1648                 :             : }
    1649                 :             : 
    1650                 :             : #define MPTCP_SEND_BURST_SIZE           ((1 << 16) - \
    1651                 :             :                                          sizeof(struct tcphdr) - \
    1652                 :             :                                          MAX_TCP_OPTION_SPACE - \
    1653                 :             :                                          sizeof(struct ipv6hdr) - \
    1654                 :             :                                          sizeof(struct frag_hdr))
    1655                 :             : 
    1656                 :             : struct subflow_send_info {
    1657                 :             :         struct sock *ssk;
    1658                 :             :         u64 linger_time;
    1659                 :             : };
    1660                 :             : 
    1661                 :        4293 : void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
    1662                 :             : {
    1663       [ + ][ + ]:        4293 :         if (!subflow->stale)
    1664                 :             :                 return;
    1665                 :             : 
    1666                 :           0 :         subflow->stale = 0;
    1667       [ # ][ # ]:           0 :         MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
    1668                 :             : }
    1669                 :             : 
    1670                 :     1698717 : bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
    1671                 :             : {
    1672            [ + ]:     1698717 :         if (unlikely(subflow->stale)) {
    1673            [ + ]:      125881 :                 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
    1674                 :             : 
    1675            [ + ]:      125881 :                 if (subflow->stale_rcv_tstamp == rcv_tstamp)
    1676                 :             :                         return false;
    1677                 :             : 
    1678                 :           0 :                 mptcp_subflow_set_active(subflow);
    1679                 :             :         }
    1680                 :     1572836 :         return __mptcp_subflow_active(subflow);
    1681                 :             : }
    1682                 :             : 
    1683                 :             : #define SSK_MODE_ACTIVE 0
    1684                 :             : #define SSK_MODE_BACKUP 1
    1685                 :             : #define SSK_MODE_MAX    2
    1686                 :             : 
    1687                 :             : /* implement the mptcp packet scheduler;
    1688                 :             :  * returns the subflow that will transmit the next DSS
    1689                 :             :  * additionally updates the rtx timeout
    1690                 :             :  */
    1691                 :      727562 : struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
    1692                 :             : {
    1693                 :      727562 :         struct subflow_send_info send_info[SSK_MODE_MAX];
    1694                 :      727562 :         struct mptcp_subflow_context *subflow;
    1695                 :      727562 :         struct sock *sk = (struct sock *)msk;
    1696                 :      727562 :         u32 pace, burst, wmem;
    1697                 :      727562 :         int i, nr_active = 0;
    1698                 :      727562 :         struct sock *ssk;
    1699                 :      727562 :         u64 linger_time;
    1700                 :      727562 :         long tout = 0;
    1701                 :             : 
    1702                 :             :         /* pick the subflow with the lower wmem/wspace ratio */
    1703       [ + ][ + ]:     2182686 :         for (i = 0; i < SSK_MODE_MAX; ++i) {
    1704                 :     1455124 :                 send_info[i].ssk = NULL;
    1705                 :     1455124 :                 send_info[i].linger_time = -1;
    1706                 :             :         }
    1707                 :             : 
    1708       [ + ][ + ]:     2006417 :         mptcp_for_each_subflow(msk, subflow) {
    1709 [ +  + ][ +  + ]:     1278855 :                 bool backup = subflow->backup || subflow->request_bkup;
    1710                 :             : 
    1711                 :     1278855 :                 trace_mptcp_subflow_get_send(subflow);
    1712                 :     1278855 :                 ssk =  mptcp_subflow_tcp_sock(subflow);
    1713       [ + ][ + ]:     1278855 :                 if (!mptcp_subflow_active(subflow))
    1714                 :      127050 :                         continue;
    1715                 :             : 
    1716       [ + ][ + ]:     1151805 :                 tout = max(tout, mptcp_timeout_from_subflow(subflow));
    1717                 :     1151805 :                 nr_active += !backup;
    1718                 :     1151805 :                 pace = subflow->avg_pacing_rate;
    1719       [ + ][ + ]:     1151805 :                 if (unlikely(!pace)) {
    1720                 :             :                         /* init pacing rate from socket */
    1721                 :        3556 :                         subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
    1722                 :        3556 :                         pace = subflow->avg_pacing_rate;
    1723       [ + ][ + ]:        3556 :                         if (!pace)
    1724                 :           0 :                                 continue;
    1725                 :             :                 }
    1726                 :             : 
    1727            [ + ]:     1151805 :                 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
    1728       [ + ][ + ]:     1151805 :                 if (linger_time < send_info[backup].linger_time) {
    1729                 :      903985 :                         send_info[backup].ssk = ssk;
    1730                 :      903985 :                         send_info[backup].linger_time = linger_time;
    1731                 :             :                 }
    1732                 :             :         }
    1733                 :      727562 :         __mptcp_set_timeout(sk, tout);
    1734                 :             : 
    1735                 :             :         /* pick the best backup if no other subflow is active */
    1736       [ + ][ + ]:      727562 :         if (!nr_active)
    1737                 :       40765 :                 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
    1738                 :             : 
    1739                 :             :         /* According to the blest algorithm, to avoid HoL blocking for the
    1740                 :             :          * faster flow, we need to:
    1741                 :             :          * - estimate the faster flow linger time
    1742                 :             :          * - use the above to estimate the amount of byte transferred
    1743                 :             :          *   by the faster flow
    1744                 :             :          * - check that the amount of queued data is greater than the above,
    1745                 :             :          *   otherwise do not use the picked, slower, subflow
    1746                 :             :          * We select the subflow with the shorter estimated time to flush
    1747                 :             :          * the queued mem, which basically ensure the above. We just need
    1748                 :             :          * to check that subflow has a non empty cwin.
    1749                 :             :          */
    1750                 :      727562 :         ssk = send_info[SSK_MODE_ACTIVE].ssk;
    1751 [ +  + ][ +  + ]:     1455092 :         if (!ssk || !sk_stream_memory_free(ssk))
    1752                 :      174223 :                 return NULL;
    1753                 :             : 
    1754                 :      553339 :         burst = min(MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
    1755                 :      553339 :         wmem = READ_ONCE(ssk->sk_wmem_queued);
    1756       [ + ][ + ]:      553339 :         if (!burst)
    1757                 :             :                 return ssk;
    1758                 :             : 
    1759                 :      328062 :         subflow = mptcp_subflow_ctx(ssk);
    1760                 :      328062 :         subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
    1761                 :      328062 :                                            READ_ONCE(ssk->sk_pacing_rate) * burst,
    1762                 :             :                                            burst + wmem);
    1763                 :      328062 :         msk->snd_burst = burst;
    1764                 :      328062 :         return ssk;
    1765                 :             : }
    1766                 :             : 
    1767                 :      669719 : static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
    1768                 :             : {
    1769            [ - ]:      669719 :         if (info->mss_now)
    1770            [ + ]:      669719 :                 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle,
    1771                 :             :                          info->size_goal);
    1772                 :      669719 :         release_sock(ssk);
    1773                 :      669719 : }
    1774                 :             : 
    1775                 :      977578 : static void mptcp_update_post_push(struct mptcp_sock *msk,
    1776                 :             :                                    struct mptcp_data_frag *dfrag,
    1777                 :             :                                    u32 sent)
    1778                 :             : {
    1779                 :      977578 :         u64 snd_nxt_new = dfrag->data_seq;
    1780                 :             : 
    1781                 :      977578 :         dfrag->already_sent += sent;
    1782                 :             : 
    1783                 :      977578 :         msk->snd_burst -= sent;
    1784                 :             : 
    1785                 :      977578 :         snd_nxt_new += dfrag->already_sent;
    1786                 :             : 
    1787                 :             :         /* snd_nxt_new can be smaller than snd_nxt in case mptcp
    1788                 :             :          * is recovering after a failover. In that event, this re-sends
    1789                 :             :          * old segments.
    1790                 :             :          *
    1791                 :             :          * Thus compute snd_nxt_new candidate based on
    1792                 :             :          * the dfrag->data_seq that was sent and the data
    1793                 :             :          * that has been handed to the subflow for transmission
    1794                 :             :          * and skip update in case it was old dfrag.
    1795                 :             :          */
    1796            [ + ]:      977578 :         if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
    1797                 :      972121 :                 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
    1798                 :      972121 :                 WRITE_ONCE(msk->snd_nxt, snd_nxt_new);
    1799                 :             :         }
    1800                 :      977578 : }
    1801                 :             : 
    1802                 :        8668 : void mptcp_check_and_set_pending(struct sock *sk)
    1803                 :             : {
    1804            [ + ]:        8668 :         if (mptcp_send_head(sk)) {
    1805                 :         240 :                 mptcp_data_lock(sk);
    1806            [ + ]:         240 :                 mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
    1807                 :         240 :                 mptcp_data_unlock(sk);
    1808                 :             :         }
    1809                 :        8668 : }
    1810                 :             : 
    1811                 :      863389 : static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
    1812                 :             :                                   struct mptcp_sendmsg_info *info)
    1813                 :             : {
    1814            [ + ]:      863389 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1815                 :      330320 :         struct mptcp_data_frag *dfrag;
    1816                 :      330320 :         int len, copied = 0, err = 0;
    1817                 :             : 
    1818            [ + ]:     1197126 :         while ((dfrag = mptcp_send_head(sk))) {
    1819                 :      942859 :                 info->sent = dfrag->already_sent;
    1820                 :      942859 :                 info->limit = dfrag->data_len;
    1821                 :      942859 :                 len = dfrag->data_len - dfrag->already_sent;
    1822            [ + ]:     1920437 :                 while (len > 0) {
    1823                 :     1308486 :                         int ret = 0;
    1824                 :             : 
    1825                 :     1308486 :                         ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
    1826            [ + ]:     1308486 :                         if (ret <= 0) {
    1827            [ + ]:      330908 :                                 err = copied ? : ret;
    1828                 :      330908 :                                 goto out;
    1829                 :             :                         }
    1830                 :             : 
    1831                 :      977578 :                         info->sent += ret;
    1832                 :      977578 :                         copied += ret;
    1833                 :      977578 :                         len -= ret;
    1834                 :             : 
    1835                 :      977578 :                         mptcp_update_post_push(msk, dfrag, ret);
    1836                 :             :                 }
    1837                 :      611951 :                 msk->first_pending = mptcp_send_next(sk);
    1838                 :             : 
    1839    [ +  + ][ + ]:      932125 :                 if (msk->snd_burst <= 0 ||
    1840    [ + ][ -  + ]:      364975 :                     !sk_stream_memory_free(ssk) ||
    1841                 :      333737 :                     !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
    1842                 :      278214 :                         err = copied;
    1843                 :      278214 :                         goto out;
    1844                 :             :                 }
    1845                 :      333737 :                 mptcp_set_timeout(sk);
    1846                 :             :         }
    1847                 :             :         err = copied;
    1848                 :             : 
    1849                 :      863389 : out:
    1850            [ + ]:      863389 :         if (err > 0)
    1851                 :      571240 :                 msk->last_data_sent = tcp_jiffies32;
    1852                 :      863389 :         return err;
    1853                 :             : }
    1854                 :             : 
    1855                 :      922176 : void __mptcp_push_pending(struct sock *sk, unsigned int flags)
    1856                 :             : {
    1857                 :      922176 :         struct sock *prev_ssk = NULL, *ssk = NULL;
    1858            [ + ]:      922176 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1859                 :      922176 :         struct mptcp_sendmsg_info info = {
    1860                 :             :                                 .flags = flags,
    1861                 :             :         };
    1862                 :      922176 :         bool copied = false;
    1863                 :      922176 :         int push_count = 1;
    1864                 :             : 
    1865 [ +  + ][ +  + ]:     1585260 :         while (mptcp_send_head(sk) && (push_count > 0)) {
    1866                 :      718733 :                 struct mptcp_subflow_context *subflow;
    1867                 :      718733 :                 int ret = 0;
    1868                 :             : 
    1869       [ + ][ + ]:      718733 :                 if (mptcp_sched_get_send(msk))
    1870                 :             :                         break;
    1871                 :             : 
    1872                 :      663084 :                 push_count = 0;
    1873                 :             : 
    1874       [ + ][ + ]:     1746970 :                 mptcp_for_each_subflow(msk, subflow) {
    1875    [ +  + ][ + ]:     1083886 :                         if (READ_ONCE(subflow->scheduled)) {
    1876                 :      703883 :                                 mptcp_subflow_set_scheduled(subflow, false);
    1877                 :             : 
    1878                 :      703883 :                                 prev_ssk = ssk;
    1879       [ + ][ + ]:      703883 :                                 ssk = mptcp_subflow_tcp_sock(subflow);
    1880       [ + ][ + ]:      703883 :                                 if (ssk != prev_ssk) {
    1881                 :             :                                         /* First check. If the ssk has changed since
    1882                 :             :                                          * the last round, release prev_ssk
    1883                 :             :                                          */
    1884       [ + ][ + ]:      669719 :                                         if (prev_ssk)
    1885                 :       44786 :                                                 mptcp_push_release(prev_ssk, &info);
    1886                 :             : 
    1887                 :             :                                         /* Need to lock the new subflow only if different
    1888                 :             :                                          * from the previous one, otherwise we are still
    1889                 :             :                                          * helding the relevant lock
    1890                 :             :                                          */
    1891                 :      669719 :                                         lock_sock(ssk);
    1892                 :             :                                 }
    1893                 :             : 
    1894                 :      703883 :                                 push_count++;
    1895                 :             : 
    1896                 :      703883 :                                 ret = __subflow_push_pending(sk, ssk, &info);
    1897       [ + ][ + ]:      703883 :                                 if (ret <= 0) {
    1898    [ +  - ][ + ]:      224274 :                                         if (ret != -EAGAIN ||
    1899       [ # ][ # ]:           0 :                                             (1 << ssk->sk_state) &
    1900                 :             :                                              (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
    1901                 :      129845 :                                                 push_count--;
    1902                 :      224274 :                                         continue;
    1903                 :             :                                 }
    1904                 :             :                                 copied = true;
    1905                 :             :                         }
    1906                 :             :                 }
    1907                 :             :         }
    1908                 :             : 
    1909                 :             :         /* at this point we held the socket lock for the last subflow we used */
    1910       [ + ][ + ]:      922176 :         if (ssk)
    1911                 :      624933 :                 mptcp_push_release(ssk, &info);
    1912                 :             : 
    1913                 :             :         /* also retry later if unsent data is left: no ack may trigger it */
    1914      [ +  +  + ]:      922176 :         if ((copied || mptcp_send_head(sk)) && !mptcp_rtx_timer_pending(sk))
              [ +  +  + ]
    1915                 :       84749 :                 mptcp_reset_rtx_timer(sk);
    1916       [ + ][ + ]:      922176 :         if (copied)
    1917                 :      449036 :                 mptcp_check_send_data_fin(sk);
    1918                 :      922176 : }
    1919                 :             : 
    1920                 :     1437816 : static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
    1921                 :             : {
    1922            [ + ]:     1437816 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1923                 :     1437816 :         struct mptcp_sendmsg_info info = {
    1924                 :             :                 .data_lock_held = true,
    1925                 :             :         };
    1926                 :     1437816 :         bool keep_pushing = true;
    1927                 :     1437816 :         struct sock *xmit_ssk;
    1928                 :     1437816 :         int copied = 0;
    1929                 :             : 
    1930                 :     1437816 :         info.flags = 0;
    1931 [ +  + ][ +  + ]:     1605407 :         while (mptcp_send_head(sk) && keep_pushing) {
    1932            [ + ]:      290700 :                 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1933                 :      290700 :                 int ret = 0;
    1934                 :             : 
    1935                 :             :                 /* check for a different subflow usage only after
    1936                 :             :                  * spooling the first chunk of data
    1937                 :             :                  */
    1938            [ + ]:      290700 :                 if (first) {
    1939                 :       10005 :                         mptcp_subflow_set_scheduled(subflow, false);
    1940                 :       10005 :                         ret = __subflow_push_pending(sk, ssk, &info);
    1941                 :       10005 :                         first = false;
    1942            [ + ]:       10005 :                         if (ret <= 0)
    1943                 :             :                                 break;
    1944                 :        6534 :                         copied += ret;
    1945                 :        6534 :                         continue;
    1946                 :             :                 }
    1947                 :             : 
    1948            [ + ]:      280695 :                 if (mptcp_sched_get_send(msk))
    1949                 :      119638 :                         goto out;
    1950                 :             : 
    1951    [ +  + ][ + ]:      161057 :                 if (READ_ONCE(subflow->scheduled)) {
    1952                 :      149501 :                         mptcp_subflow_set_scheduled(subflow, false);
    1953                 :      149501 :                         ret = __subflow_push_pending(sk, ssk, &info);
    1954       [ + ][ + ]:      149501 :                         if (ret <= 0)
    1955                 :             :                                 keep_pushing = false;
    1956                 :             :                         else
    1957                 :       85097 :                                 copied += ret;
    1958                 :             :                 }
    1959                 :             : 
    1960       [ + ][ + ]:      434978 :                 mptcp_for_each_subflow(msk, subflow) {
    1961    [ +  + ][ + ]:      273921 :                         if (READ_ONCE(subflow->scheduled)) {
    1962       [ - ][ - ]:       11556 :                                 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
    1963       [ - ][ - ]:       11556 :                                 if (xmit_ssk != ssk) {
    1964                 :       11556 :                                         mptcp_subflow_delegate(subflow,
    1965                 :             :                                                                MPTCP_DELEGATE_SEND);
    1966                 :       11556 :                                         keep_pushing = false;
    1967                 :             :                                 }
    1968                 :             :                         }
    1969                 :             :                 }
    1970                 :             :         }
    1971                 :             : 
    1972                 :     1318178 : out:
    1973                 :             :         /* __mptcp_alloc_tx_skb could have released some wmem and we are
    1974                 :             :          * not going to flush it via release_sock()
    1975                 :             :          */
    1976       [ + ][ + ]:     1437816 :         if (copied) {
    1977       [ + ][ + ]:       87307 :                 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
    1978                 :             :                          info.size_goal);
    1979                 :             : 
    1980    [ +  + ][ + ]:       87307 :                 if (msk->snd_data_fin_enable &&
    1981       [ + ][ + ]:       12653 :                     msk->snd_nxt + 1 == msk->write_seq)
    1982                 :         415 :                         mptcp_schedule_work(sk);
    1983                 :             :         }
    1984                 :             : 
    1985                 :             :         /* see __mptcp_push_pending() */
    1986      [ +  +  + ]:     1437816 :         if ((copied || mptcp_send_head(sk)) && !mptcp_rtx_timer_pending(sk))
              [ +  +  + ]
    1987                 :         882 :                 mptcp_reset_rtx_timer(sk);
    1988                 :     1437816 : }
    1989                 :             : 
    1990                 :             : static int mptcp_disconnect(struct sock *sk, int flags);
    1991                 :             : 
    1992                 :         112 : static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
    1993                 :             :                                   size_t len, int *copied_syn)
    1994                 :             : {
    1995                 :         112 :         unsigned int saved_flags = msg->msg_flags;
    1996            [ + ]:         112 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1997                 :         112 :         struct sock *ssk;
    1998                 :         112 :         int ret;
    1999                 :             : 
    2000                 :             :         /* on flags based fastopen the mptcp is supposed to create the
    2001                 :             :          * first subflow right now. Otherwise we are in the defer_connect
    2002                 :             :          * path, and the first subflow must be already present.
    2003                 :             :          */
    2004            [ + ]:         112 :         if (msg->msg_flags & MSG_FASTOPEN) {
    2005                 :          76 :                 ssk = __mptcp_nmpc_sk(msk);
    2006            [ + ]:          76 :                 if (IS_ERR(ssk))
    2007                 :           6 :                         return PTR_ERR(ssk);
    2008                 :             :         }
    2009       [ - ][ - ]:         106 :         if (!msk->first)
    2010                 :             :                 return -EINVAL;
    2011                 :             : 
    2012                 :         106 :         ssk = msk->first;
    2013                 :             : 
    2014                 :         106 :         lock_sock(ssk);
    2015                 :             : 
    2016                 :             :         /* subflow aborted or disconnected: its error report may be deferred.
    2017                 :             :          * With MSG_FASTOPEN, the socket is disconnected below, as with TCP.
    2018                 :             :          */
    2019   [ +  +  -  -  :         142 :         if (unlikely(!(msg->msg_flags & MSG_FASTOPEN) &&
             -  +  -  -  
           + ][ +  +  -  
          -  -  +  -  -  
                      + ]
    2020                 :             :                      inet_test_bit(DEFER_CONNECT, sk) &&
    2021                 :             :                      !inet_test_bit(DEFER_CONNECT, ssk))) {
    2022                 :           0 :                 inet_clear_bit(DEFER_CONNECT, sk);
    2023                 :           0 :                 __mptcp_subflow_error_report(sk, ssk);
    2024                 :           0 :                 release_sock(ssk);
    2025                 :           0 :                 return 0;
    2026                 :             :         }
    2027                 :             : 
    2028                 :         106 :         msg->msg_flags |= MSG_DONTWAIT;
    2029                 :         106 :         msk->fastopening = 1;
    2030                 :         106 :         ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
    2031                 :         106 :         msk->fastopening = 0;
    2032                 :         106 :         msg->msg_flags = saved_flags;
    2033                 :         106 :         release_sock(ssk);
    2034                 :             : 
    2035                 :         106 :         inet_clear_bit(DEFER_CONNECT, sk);
    2036                 :             : 
    2037                 :             :         /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
    2038 [ -  + ][ -  + ]:         106 :         if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
    2039                 :          46 :                 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
    2040                 :             :                                             msg->msg_namelen, msg->msg_flags, true);
    2041                 :             : 
    2042                 :             :                 /* Keep the same behaviour of plain TCP: zero the copied bytes in
    2043                 :             :                  * case of any error, except timeout or signal
    2044                 :             :                  */
    2045 [ +  - ][ +  - ]:          46 :                 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
    2046                 :           0 :                         *copied_syn = 0;
    2047 [ -  - ][ -  - ]:          60 :         } else if (ret && ret != -EINPROGRESS && sk->sk_state != TCP_LISTEN &&
    2048       [ # ][ # ]:           0 :                    sk->sk_socket->state != SS_CONNECTED) {
    2049                 :             :                 /* The disconnect() op called by tcp_sendmsg_fastopen()/
    2050                 :             :                  * __inet_stream_connect() can fail, due to looking check,
    2051                 :             :                  * see mptcp_disconnect().
    2052                 :             :                  * Attempt it again outside the problematic scope.
    2053                 :             :                  * A listener or a connected socket is not disconnected there,
    2054                 :             :                  * as with TCP.
    2055                 :             :                  */
    2056       [ # ][ # ]:           0 :                 if (!mptcp_disconnect(sk, 0)) {
    2057                 :           0 :                         sk->sk_disconnects++;
    2058                 :           0 :                         sk->sk_socket->state = SS_UNCONNECTED;
    2059                 :             :                 }
    2060                 :             :         }
    2061                 :             : 
    2062                 :             :         return ret;
    2063                 :             : }
    2064                 :             : 
    2065                 :      829789 : static int do_copy_data_nocache(struct sock *sk, int copy,
    2066                 :             :                                 struct iov_iter *from, char *to)
    2067                 :             : {
    2068            [ + ]:      829789 :         if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
    2069            [ # ]:           0 :                 if (!copy_from_iter_full_nocache(to, copy, from))
    2070                 :           0 :                         return -EFAULT;
    2071            [ + ]:      829789 :         } else if (!copy_from_iter_full(to, copy, from)) {
    2072                 :           6 :                 return -EFAULT;
    2073                 :             :         }
    2074                 :             :         return 0;
    2075                 :             : }
    2076                 :             : 
    2077                 :             : /* open-code sk_stream_memory_free() plus sent limit computation to
    2078                 :             :  * avoid indirect calls in fast-path.
    2079                 :             :  * Called under the msk socket lock, so we can avoid a bunch of ONCE
    2080                 :             :  * annotations.
    2081                 :             :  */
    2082                 :      833940 : static u32 mptcp_send_limit(const struct sock *sk)
    2083                 :             : {
    2084            [ + ]:      833940 :         const struct mptcp_sock *msk = mptcp_sk(sk);
    2085                 :      833940 :         u32 limit, not_sent;
    2086                 :             : 
    2087            [ + ]:      833940 :         if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
    2088                 :             :                 return 0;
    2089                 :             : 
    2090                 :      829795 :         limit = mptcp_notsent_lowat(sk);
    2091            [ + ]:      829795 :         if (limit == UINT_MAX)
    2092                 :             :                 return UINT_MAX;
    2093                 :             : 
    2094                 :          12 :         not_sent = msk->write_seq - msk->snd_nxt;
    2095            [ + ]:          12 :         if (not_sent >= limit)
    2096                 :             :                 return 0;
    2097                 :             : 
    2098                 :           6 :         return limit - not_sent;
    2099                 :             : }
    2100                 :             : 
    2101                 :     1872005 : static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
    2102                 :             : {
    2103                 :     1872005 :         struct mptcp_subflow_context *subflow;
    2104                 :             : 
    2105            [ + ]:     1872005 :         if (!rfs_is_needed())
    2106                 :             :                 return;
    2107                 :             : 
    2108            [ # ]:           0 :         mptcp_for_each_subflow(msk, subflow) {
    2109                 :           0 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2110                 :             : 
    2111                 :           0 :                 sock_rps_record_flow(ssk);
    2112                 :             :         }
    2113                 :             : }
    2114                 :             : 
    2115                 :      665621 : static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
    2116                 :             : {
    2117            [ + ]:      665621 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2118                 :      665621 :         struct page_frag *pfrag;
    2119                 :      665621 :         size_t copied = 0;
    2120                 :      665621 :         int ret = 0;
    2121                 :      665621 :         long timeo;
    2122                 :             : 
    2123                 :             :         /* silently ignore everything else */
    2124                 :      665621 :         msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
    2125                 :             :                           MSG_FASTOPEN | MSG_EOR;
    2126                 :             : 
    2127                 :      665621 :         lock_sock(sk);
    2128                 :             : 
    2129                 :      665621 :         mptcp_rps_record_subflows(msk);
    2130                 :             : 
    2131   [ +  -  -  +  :      847846 :         if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
           + ][ +  -  -  
                   +  + ]
    2132                 :             :                      msg->msg_flags & MSG_FASTOPEN)) {
    2133                 :         112 :                 int copied_syn = 0;
    2134                 :             : 
    2135                 :         112 :                 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
    2136                 :         112 :                 copied += copied_syn;
    2137 [ +  + ][ +  + ]:         112 :                 if (ret == -EINPROGRESS && copied_syn > 0)
    2138                 :          42 :                         goto out;
    2139       [ + ][ + ]:          70 :                 else if (ret)
    2140                 :          24 :                         goto do_error;
    2141                 :             :         }
    2142                 :             : 
    2143       [ + ][ + ]:      665555 :         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
    2144                 :             : 
    2145    [ +  + ][ + ]:      665555 :         if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
    2146                 :          44 :                 ret = sk_stream_wait_connect(sk, &timeo);
    2147       [ - ][ - ]:          44 :                 if (ret)
    2148                 :          44 :                         goto do_error;
    2149                 :             :         }
    2150                 :             : 
    2151                 :      665511 :         ret = -EPIPE;
    2152 [ -  + ][ -  + ]:      665511 :         if (unlikely(READ_ONCE(sk->sk_err) || (sk->sk_shutdown & SEND_SHUTDOWN)))
    2153                 :           0 :                 goto do_error;
    2154                 :             : 
    2155            [ - ]:      665511 :         pfrag = sk_page_frag(sk);
    2156                 :             : 
    2157       [ + ][ + ]:     1499439 :         while (msg_data_left(msg)) {
    2158                 :      833940 :                 int total_ts, frag_truesize = 0;
    2159                 :      833940 :                 struct mptcp_data_frag *dfrag;
    2160                 :      833940 :                 bool dfrag_collapsed;
    2161                 :      833940 :                 size_t psize, offset;
    2162                 :      833940 :                 u32 copy_limit;
    2163                 :             : 
    2164                 :             :                 /* ensure fitting the notsent_lowat() constraint */
    2165                 :      833940 :                 copy_limit = mptcp_send_limit(sk);
    2166       [ + ][ + ]:      833940 :                 if (!copy_limit)
    2167                 :        4151 :                         goto wait_for_memory;
    2168                 :             : 
    2169                 :             :                 /* reuse tail pfrag, if possible, or carve a new one from the
    2170                 :             :                  * page allocator
    2171                 :             :                  */
    2172                 :      829789 :                 dfrag = mptcp_pending_tail(sk);
    2173                 :      829789 :                 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
    2174       [ + ][ + ]:      829789 :                 if (!dfrag_collapsed) {
    2175       [ + ][ + ]:      606922 :                         if (!mptcp_page_frag_refill(sk, pfrag))
    2176                 :           0 :                                 goto wait_for_memory;
    2177                 :             : 
    2178                 :      606922 :                         dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
    2179                 :      606922 :                         frag_truesize = dfrag->overhead;
    2180                 :             :                 }
    2181                 :             : 
    2182                 :             :                 /* we do not bound vs wspace, to allow a single packet.
    2183                 :             :                  * memory accounting will prevent execessive memory usage
    2184                 :             :                  * anyway
    2185                 :             :                  */
    2186                 :      829789 :                 offset = dfrag->offset + dfrag->data_len;
    2187                 :      829789 :                 psize = pfrag->size - offset;
    2188                 :      829789 :                 psize = min_t(size_t, psize, msg_data_left(msg));
    2189                 :      829789 :                 psize = min_t(size_t, psize, copy_limit);
    2190                 :      829789 :                 total_ts = psize + frag_truesize;
    2191                 :             : 
    2192       [ + ][ + ]:      829789 :                 if (!sk_wmem_schedule(sk, total_ts))
    2193                 :           0 :                         goto wait_for_memory;
    2194                 :             : 
    2195                 :     1091570 :                 ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
    2196                 :      829789 :                                            page_address(dfrag->page) + offset);
    2197       [ + ][ + ]:      829789 :                 if (ret)
    2198                 :           6 :                         goto do_error;
    2199                 :             : 
    2200                 :             :                 /* data successfully copied into the write queue */
    2201       [ + ][ + ]:      829783 :                 sk_forward_alloc_add(sk, -total_ts);
    2202                 :      829783 :                 copied += psize;
    2203                 :      829783 :                 dfrag->data_len += psize;
    2204                 :      829783 :                 frag_truesize += psize;
    2205                 :      829783 :                 pfrag->offset += frag_truesize;
    2206                 :      829783 :                 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
    2207                 :             : 
    2208                 :             :                 /* charge data on mptcp pending queue to the msk socket
    2209                 :             :                  * Note: we charge such data both to sk and ssk
    2210                 :             :                  */
    2211       [ + ][ + ]:      829783 :                 sk_wmem_queued_add(sk, frag_truesize);
    2212       [ + ][ + ]:      829783 :                 if (!dfrag_collapsed) {
    2213                 :      606916 :                         get_page(dfrag->page);
    2214            [ + ]:      606916 :                         list_add_tail(&dfrag->list, &msk->rtx_queue);
    2215       [ + ][ + ]:      606916 :                         if (!msk->first_pending)
    2216                 :      444975 :                                 msk->first_pending = dfrag;
    2217                 :             :                 }
    2218 [ -  - ][ -  - ]:      829783 :                 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
    2219                 :             :                          dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
    2220                 :             :                          !dfrag_collapsed);
    2221                 :             : 
    2222                 :      829783 :                 continue;
    2223                 :             : 
    2224                 :        4151 : wait_for_memory:
    2225                 :        4151 :                 sk_set_nospace(sk);
    2226                 :        4151 :                 __mptcp_push_pending(sk, msg->msg_flags);
    2227                 :        4151 :                 ret = sk_stream_wait_memory(sk, &timeo);
    2228       [ + ][ + ]:        4151 :                 if (ret)
    2229                 :           6 :                         goto do_error;
    2230                 :             :         }
    2231                 :             : 
    2232       [ + ][ + ]:      665499 :         if (copied) {
    2233                 :             :                 /* mark the last dfrag with EOR if MSG_EOR was set */
    2234       [ + ][ + ]:      665370 :                 if (msg->msg_flags & MSG_EOR) {
    2235                 :           6 :                         struct mptcp_data_frag *dfrag = mptcp_pending_tail(sk);
    2236                 :             : 
    2237       [ - ][ - ]:           6 :                         if (dfrag)
    2238                 :           6 :                                 dfrag->eor = 1;
    2239                 :             :                 }
    2240                 :      665370 :                 __mptcp_push_pending(sk, msg->msg_flags);
    2241                 :             :         }
    2242                 :             : 
    2243                 :         129 : out:
    2244                 :      665621 :         release_sock(sk);
    2245                 :      665621 :         return copied;
    2246                 :             : 
    2247                 :          80 : do_error:
    2248       [ + ][ + ]:          80 :         if (copied) {
    2249                 :          12 :                 __mptcp_push_pending(sk, msg->msg_flags);
    2250                 :          12 :                 goto out;
    2251                 :             :         }
    2252                 :             : 
    2253                 :          68 :         copied = sk_stream_error(sk, msg->msg_flags, ret);
    2254                 :          68 :         goto out;
    2255                 :             : }
    2256                 :             : 
    2257                 :             : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied);
    2258                 :             : 
    2259                 :      776463 : static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)
    2260                 :             : {
    2261                 :             :         /* avoid the indirect call, we know the destructor is sock_rfree */
    2262                 :      776463 :         skb->destructor = NULL;
    2263                 :      776463 :         skb->sk = NULL;
    2264                 :      776463 :         atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
    2265                 :      776463 :         sk_mem_uncharge(sk, skb->truesize);
    2266                 :      776463 :         __skb_unlink(skb, &sk->sk_receive_queue);
    2267                 :      776463 :         skb_attempt_defer_free(skb);
    2268                 :      776463 : }
    2269                 :             : 
    2270                 :     1263365 : static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
    2271                 :             :                                 size_t len, int flags, int copied_total,
    2272                 :             :                                 struct scm_timestamping_internal *tss,
    2273                 :             :                                 int *cmsg_flags, struct sk_buff **last)
    2274                 :             : {
    2275            [ + ]:     1263365 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2276                 :     1263365 :         struct sk_buff *skb, *tmp;
    2277                 :     1263365 :         int total_data_len = 0;
    2278                 :     1263365 :         int copied = 0;
    2279                 :             : 
    2280            [ + ]:     1793804 :         skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
    2281                 :     1173912 :                 u32 delta, offset = MPTCP_SKB_CB(skb)->offset;
    2282                 :     1173912 :                 u32 data_len = skb->len - offset;
    2283                 :     1173912 :                 u32 count;
    2284                 :     1173912 :                 int err;
    2285                 :             : 
    2286            [ + ]:     1173912 :                 if (flags & MSG_PEEK) {
    2287                 :             :                         /* skip already peeked skbs */
    2288            [ + ]:       41429 :                         if (total_data_len + data_len <= copied_total) {
    2289                 :          15 :                                 total_data_len += data_len;
    2290                 :          15 :                                 *last = skb;
    2291                 :          15 :                                 continue;
    2292                 :             :                         }
    2293                 :             : 
    2294                 :             :                         /* skip the already peeked data in the current skb */
    2295                 :       41414 :                         delta = copied_total - total_data_len;
    2296                 :       41414 :                         offset += delta;
    2297                 :       41414 :                         data_len -= delta;
    2298                 :             :                 }
    2299                 :             : 
    2300                 :     1173897 :                 count = min_t(size_t, len - copied, data_len);
    2301            [ - ]:     1173897 :                 if (!(flags & MSG_TRUNC)) {
    2302                 :     1173897 :                         err = skb_copy_datagram_msg(skb, offset, msg, count);
    2303            [ + ]:     1173897 :                         if (unlikely(err < 0)) {
    2304         [ #  # ]:           0 :                                 if (!copied)
    2305                 :             :                                         return err;
    2306                 :             :                                 break;
    2307                 :             :                         }
    2308                 :             :                 }
    2309                 :             : 
    2310    [ +  + ][ + ]:     1173897 :                 if (MPTCP_SKB_CB(skb)->has_rxtstamp &&
    2311            [ + ]:           6 :                     tcp_want_rx_tstamp_cmsg(sk)) {
    2312                 :         421 :                         tcp_update_recv_tstamps(skb, tss);
    2313                 :         421 :                         *cmsg_flags |= MPTCP_CMSG_TS;
    2314                 :             :                 }
    2315                 :             : 
    2316                 :     1173897 :                 copied += count;
    2317                 :             : 
    2318            [ + ]:     1173897 :                 if (!(flags & MSG_PEEK)) {
    2319                 :     1132483 :                         msk->bytes_consumed += count;
    2320            [ + ]:     1132483 :                         if (count < data_len) {
    2321                 :      513298 :                                 MPTCP_SKB_CB(skb)->offset += count;
    2322                 :      513298 :                                 MPTCP_SKB_CB(skb)->map_seq += count;
    2323                 :      513298 :                                 break;
    2324                 :             :                         }
    2325                 :             : 
    2326                 :      619185 :                         mptcp_eat_recv_skb(sk, skb);
    2327                 :             :                 } else {
    2328                 :       41414 :                         *last = skb;
    2329                 :             :                 }
    2330                 :             : 
    2331            [ + ]:      660599 :                 if (copied >= len)
    2332                 :             :                         break;
    2333                 :             :         }
    2334                 :             : 
    2335            [ + ]:     1263365 :         if (!(flags & MSG_PEEK))
    2336                 :     1227750 :                 mptcp_rcv_space_adjust(msk, copied);
    2337                 :             :         return copied;
    2338                 :             : }
    2339                 :             : 
    2340                 :        2858 : static void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
    2341                 :             : {
    2342            [ + ]:        2858 :         const struct tcp_sock *tp = tcp_sk(ssk);
    2343                 :             : 
    2344                 :        2858 :         msk->rcvspace_init = 1;
    2345                 :        2858 :         msk->rcvq_space.copied = 0;
    2346                 :             : 
    2347                 :             :         /* initial rcv_space offering made to peer */
    2348                 :        2858 :         msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
    2349                 :             :                                       TCP_INIT_CWND * tp->advmss);
    2350            [ + ]:        2858 :         if (msk->rcvq_space.space == 0)
    2351                 :           2 :                 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
    2352                 :        2858 : }
    2353                 :             : 
    2354                 :             : /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
    2355                 :             :  *
    2356                 :             :  * Only difference: Use lowest rtt estimate of the subflows in use, see
    2357                 :             :  * mptcp_rcv_rtt_update() and mptcp_rtt_us_est().
    2358                 :             :  */
    2359                 :     1603606 : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
    2360                 :             : {
    2361                 :     1603606 :         struct mptcp_subflow_context *subflow;
    2362                 :     1603606 :         struct sock *sk = (struct sock *)msk;
    2363                 :     1603606 :         u32 time, rtt_us;
    2364                 :     1603606 :         u64 mstamp;
    2365                 :             : 
    2366                 :     1603606 :         msk_owned_by_me(msk);
    2367                 :             : 
    2368       [ + ][ + ]:     1603606 :         if (copied <= 0)
    2369                 :             :                 return;
    2370                 :             : 
    2371       [ + ][ + ]:     1187474 :         if (!msk->rcvspace_init)
    2372                 :        1162 :                 mptcp_rcv_space_init(msk, msk->first);
    2373                 :             : 
    2374                 :     1187474 :         msk->rcvq_space.copied += copied;
    2375                 :             : 
    2376                 :     1187474 :         mstamp = mptcp_stamp();
    2377                 :     1187474 :         time = tcp_stamp_us_delta(mstamp, READ_ONCE(msk->rcvq_space.time));
    2378                 :             : 
    2379                 :     1187474 :         rtt_us = mptcp_rtt_us_est(msk);
    2380 [ +  + ][ +  + ]:     1187474 :         if (rtt_us == U32_MAX || time < (rtt_us >> 3))
    2381                 :             :                 return;
    2382                 :             : 
    2383                 :       87690 :         copied = msk->rcvq_space.copied;
    2384                 :       87690 :         copied -= mptcp_inq_hint(sk);
    2385       [ + ][ + ]:       87690 :         if (copied <= msk->rcvq_space.space)
    2386                 :       82682 :                 goto new_measure;
    2387                 :             : 
    2388                 :        5008 :         trace_mptcp_rcvbuf_grow(sk, time);
    2389       [ + ][ + ]:        5008 :         if (mptcp_rcvbuf_grow(sk, copied)) {
    2390                 :             :                 /* Make subflows follow along.  If we do not do this, we
    2391                 :             :                  * get drops at subflow level if skbs can't be moved to
    2392                 :             :                  * the mptcp rx queue fast enough (announced rcv_win can
    2393                 :             :                  * exceed ssk->sk_rcvbuf).
    2394                 :             :                  */
    2395       [ + ][ + ]:        5666 :                 mptcp_for_each_subflow(msk, subflow) {
    2396                 :        2977 :                         struct sock *ssk;
    2397                 :        2977 :                         bool slow;
    2398                 :             : 
    2399                 :        2977 :                         ssk = mptcp_subflow_tcp_sock(subflow);
    2400                 :        2977 :                         slow = lock_sock_fast(ssk);
    2401                 :             :                         /* subflows can be added before tcp_init_transfer() */
    2402 [ +  - ][ +  - ]:        2977 :                         if (tcp_sk(ssk)->rcvq_space.space)
    2403                 :        2977 :                                 tcp_rcvbuf_grow(ssk, copied);
    2404                 :        2977 :                         unlock_sock_fast(ssk, slow);
    2405                 :             :                 }
    2406                 :             :         }
    2407                 :             : 
    2408                 :        5008 : new_measure:
    2409                 :       87690 :         msk->rcvq_space.copied = 0;
    2410                 :       87690 :         msk->rcvq_space.time = mstamp;
    2411                 :             : }
    2412                 :             : 
    2413                 :      156684 : static bool __mptcp_move_skbs(struct sock *sk, struct list_head *skbs, u32 *delta)
    2414                 :             : {
    2415                 :      156684 :         struct sk_buff *skb = list_first_entry(skbs, struct sk_buff, list);
    2416            [ + ]:      156684 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2417                 :             :         bool moved = false;
    2418                 :             : 
    2419                 :      227768 :         while (1) {
    2420                 :      192226 :                 prefetch(skb->next);
    2421                 :      192226 :                 list_del(&skb->list);
    2422                 :      192226 :                 *delta += skb->truesize;
    2423                 :             : 
    2424                 :      192226 :                 moved |= __mptcp_move_skb(sk, skb);
    2425       [ + ][ + ]:      192226 :                 if (list_empty(skbs))
    2426                 :             :                         break;
    2427                 :             : 
    2428                 :       35542 :                 skb = list_first_entry(skbs, struct sk_buff, list);
    2429                 :             :         }
    2430                 :             : 
    2431                 :      156684 :         __mptcp_ofo_queue(msk);
    2432       [ + ][ + ]:      156684 :         if (!moved)
    2433                 :             :                 return false;
    2434                 :             : 
    2435                 :             :         /* Only the worker can close an orphan, as it must then destroy it */
    2436       [ - ][ - ]:      151996 :         if (!sock_flag(sk, SOCK_DEAD))
    2437                 :      151996 :                 mptcp_check_data_fin(sk);
    2438       [ # ][ # ]:           0 :         else if (mptcp_pending_data_fin(sk, NULL))
    2439                 :           0 :                 mptcp_schedule_work(sk);
    2440                 :             :         return true;
    2441                 :             : }
    2442                 :             : 
    2443                 :     2695420 : static bool mptcp_can_spool_backlog(struct sock *sk, struct list_head *skbs)
    2444                 :             : {
    2445            [ + ]:     2695420 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2446                 :             : 
    2447            [ + ]:     2695420 :         if (list_empty(&msk->backlog_list))
    2448                 :             :                 return false;
    2449                 :             : 
    2450            [ - ]:      156684 :         INIT_LIST_HEAD(skbs);
    2451            [ - ]:      156684 :         list_splice_init(&msk->backlog_list, skbs);
    2452                 :             :         return true;
    2453                 :             : }
    2454                 :             : 
    2455                 :       14283 : static bool mptcp_move_skbs(struct sock *sk)
    2456                 :             : {
    2457            [ + ]:       14283 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2458                 :       14283 :         struct list_head skbs;
    2459                 :       14283 :         bool enqueued = false;
    2460                 :       14283 :         u32 moved = 0;
    2461                 :             : 
    2462                 :       14283 :         mptcp_data_lock(sk);
    2463            [ + ]:       43057 :         while (mptcp_can_spool_backlog(sk, &skbs)) {
    2464                 :       14491 :                 mptcp_data_unlock(sk);
    2465                 :       14491 :                 enqueued |= __mptcp_move_skbs(sk, &skbs, &moved);
    2466                 :             : 
    2467                 :       14491 :                 mptcp_data_lock(sk);
    2468                 :             :         }
    2469                 :       14283 :         WRITE_ONCE(msk->backlog_len, msk->backlog_len - moved);
    2470                 :       14283 :         mptcp_data_unlock(sk);
    2471                 :             : 
    2472         [ +  - ]:       14283 :         if (enqueued && mptcp_epollin_ready(sk))
    2473                 :       13338 :                 sk->sk_data_ready(sk);
    2474                 :             : 
    2475                 :       14283 :         return enqueued;
    2476                 :             : }
    2477                 :             : 
    2478                 :       88119 : static unsigned int mptcp_inq_hint(const struct sock *sk)
    2479                 :             : {
    2480            [ + ]:       88119 :         const struct mptcp_sock *msk = mptcp_sk(sk);
    2481                 :       88119 :         const struct sk_buff *skb;
    2482                 :             : 
    2483            [ + ]:       88119 :         skb = skb_peek(&sk->sk_receive_queue);
    2484            [ - ]:       50199 :         if (skb) {
    2485                 :       50199 :                 u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
    2486                 :             : 
    2487            [ - ]:       50199 :                 if (hint_val >= INT_MAX)
    2488                 :             :                         return INT_MAX;
    2489                 :             : 
    2490                 :       50199 :                 return (unsigned int)hint_val;
    2491                 :             :         }
    2492                 :             : 
    2493         [ +  + ]:       37920 :         if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
    2494                 :         266 :                 return 1;
    2495                 :             : 
    2496                 :             :         return 0;
    2497                 :             : }
    2498                 :             : 
    2499                 :     1069226 : static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
    2500                 :             :                          int flags)
    2501                 :             : {
    2502            [ + ]:     1069226 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2503                 :     1069226 :         struct scm_timestamping_internal tss;
    2504                 :     1069226 :         int copied = 0, cmsg_flags = 0;
    2505                 :     1069226 :         int target;
    2506                 :     1069226 :         long timeo;
    2507                 :             : 
    2508            [ + ]:     1069226 :         if (unlikely(flags & MSG_ERRQUEUE))
    2509                 :           4 :                 return inet_recv_error(sk, msg, len);
    2510                 :             : 
    2511                 :     1069222 :         lock_sock(sk);
    2512            [ + ]:     1069222 :         if (unlikely(sk->sk_state == TCP_LISTEN)) {
    2513                 :           0 :                 copied = -ENOTCONN;
    2514                 :           0 :                 goto out_err;
    2515                 :             :         }
    2516                 :             : 
    2517                 :     1069222 :         mptcp_rps_record_subflows(msk);
    2518                 :             : 
    2519            [ + ]:     1069222 :         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
    2520                 :             : 
    2521                 :     1069222 :         len = min_t(size_t, len, INT_MAX);
    2522            [ + ]:     1069222 :         target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
    2523                 :             : 
    2524            [ + ]:     1069222 :         if (unlikely(msk->recvmsg_inq))
    2525                 :         429 :                 cmsg_flags = MPTCP_CMSG_INQ;
    2526                 :             : 
    2527       [ + ][ + ]:     1268261 :         while (copied < len) {
    2528                 :     1263365 :                 struct sk_buff *last = NULL;
    2529                 :     1263365 :                 int err, bytes_read;
    2530                 :             : 
    2531                 :     1263365 :                 bytes_read = __mptcp_recvmsg_mskq(sk, msg, len - copied, flags,
    2532                 :             :                                                   copied, &tss, &cmsg_flags,
    2533                 :             :                                                   &last);
    2534            [ + ]:     1263365 :                 if (unlikely(bytes_read < 0)) {
    2535            [ # ]:           0 :                         if (!copied)
    2536                 :           0 :                                 copied = bytes_read;
    2537                 :           0 :                         goto out_err;
    2538                 :             :                 }
    2539                 :             : 
    2540                 :     1263365 :                 copied += bytes_read;
    2541                 :             : 
    2542         [ +  + ]:     1263365 :                 if (!list_empty(&msk->backlog_list) && mptcp_move_skbs(sk))
    2543                 :        9877 :                         continue;
    2544                 :             : 
    2545                 :             :                 /* only the MPTCP socket status is relevant here. The exit
    2546                 :             :                  * conditions mirror closely tcp_recvmsg()
    2547                 :             :                  */
    2548            [ + ]:     1253488 :                 if (copied >= target)
    2549                 :             :                         break;
    2550                 :             : 
    2551            [ + ]:      191156 :                 if (copied) {
    2552            [ - ]:           6 :                         if (tcp_recv_should_stop(sk) ||
    2553 [ -  + ][ -  + ]:           6 :                             !timeo)
    2554                 :             :                                 break;
    2555                 :             :                 } else {
    2556       [ + ][ + ]:      191150 :                         if (sock_flag(sk, SOCK_DONE))
    2557                 :             :                                 break;
    2558                 :             : 
    2559            [ + ]:      189176 :                         copied = sock_error(sk);
    2560       [ + ][ + ]:      173484 :                         if (copied)
    2561                 :             :                                 break;
    2562                 :             : 
    2563       [ - ][ - ]:      189174 :                         if (sk->sk_shutdown & RCV_SHUTDOWN)
    2564                 :             :                                 break;
    2565                 :             : 
    2566       [ + ][ + ]:      189174 :                         if (sk->sk_state == TCP_CLOSE) {
    2567                 :             :                                 copied = -ENOTCONN;
    2568                 :             :                                 break;
    2569                 :             :                         }
    2570                 :             : 
    2571       [ - ][ - ]:      189156 :                         if (!timeo) {
    2572                 :             :                                 copied = -EAGAIN;
    2573                 :             :                                 break;
    2574                 :             :                         }
    2575                 :             : 
    2576       [ + ][ + ]:      189156 :                         if (signal_pending(current)) {
    2577       [ - ][ - ]:      252672 :                                 copied = sock_intr_errno(timeo);
    2578                 :             :                                 break;
    2579                 :             :                         }
    2580                 :             :                 }
    2581                 :             : 
    2582 [ -  - ][ -  - ]:      189162 :                 pr_debug("block timeout %ld\n", timeo);
    2583                 :      189162 :                 mptcp_cleanup_rbuf(msk, copied);
    2584                 :      189162 :                 err = sk_wait_data(sk, &timeo, last);
    2585       [ + ][ + ]:      189162 :                 if (err < 0) {
    2586       [ # ][ # ]:           0 :                         copied = copied ? : err;
    2587                 :           0 :                         goto out_err;
    2588                 :             :                 }
    2589                 :             :         }
    2590                 :             : 
    2591                 :     1069222 :         mptcp_cleanup_rbuf(msk, copied);
    2592                 :             : 
    2593                 :     1069222 : out_err:
    2594 [ +  - ][ +  - ]:     1069222 :         if (cmsg_flags && copied >= 0) {
    2595       [ + ][ + ]:         429 :                 if (cmsg_flags & MPTCP_CMSG_TS)
    2596                 :         421 :                         tcp_recv_timestamp(msg, sk, &tss);
    2597                 :             : 
    2598       [ - ][ - ]:         429 :                 if (cmsg_flags & MPTCP_CMSG_INQ) {
    2599                 :         429 :                         unsigned int inq = mptcp_inq_hint(sk);
    2600                 :             : 
    2601                 :         429 :                         put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
    2602                 :             :                 }
    2603                 :             :         }
    2604                 :             : 
    2605 [ -  - ][ -  - ]:     1069222 :         pr_debug("msk=%p rx queue empty=%d copied=%d\n",
    2606                 :             :                  msk, skb_queue_empty(&sk->sk_receive_queue), copied);
    2607                 :             : 
    2608                 :     1069222 :         release_sock(sk);
    2609                 :     1069222 :         return copied;
    2610                 :             : }
    2611                 :             : 
    2612                 :       11796 : static void mptcp_retransmit_timer(struct timer_list *t)
    2613                 :             : {
    2614                 :       11796 :         struct sock *sk = timer_container_of(sk, t, mptcp_retransmit_timer);
    2615            [ + ]:       11796 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2616                 :             : 
    2617                 :       11796 :         bh_lock_sock(sk);
    2618       [ + ][ + ]:       11796 :         if (!sock_owned_by_user(sk)) {
    2619                 :             :                 /* we need a process context to retransmit */
    2620       [ + ][ - ]:        8378 :                 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
    2621                 :        5147 :                         mptcp_schedule_work(sk);
    2622                 :             :         } else {
    2623                 :             :                 /* delegate our work to tcp_release_cb() */
    2624      [ +  -  - ]:        6636 :                 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
              [ +  -  - ]
    2625                 :             :         }
    2626                 :       11796 :         bh_unlock_sock(sk);
    2627                 :       11796 :         sock_put(sk);
    2628                 :       11796 : }
    2629                 :             : 
    2630                 :         136 : static void mptcp_tout_timer(struct timer_list *t)
    2631                 :             : {
    2632                 :         136 :         struct inet_connection_sock *icsk =
    2633                 :             :                 timer_container_of(icsk, t, mptcp_tout_timer);
    2634                 :         136 :         struct sock *sk = &icsk->icsk_inet.sk;
    2635                 :             : 
    2636                 :         136 :         mptcp_schedule_work(sk);
    2637                 :         136 :         sock_put(sk);
    2638                 :         136 : }
    2639                 :             : 
    2640                 :             : /* Find an idle subflow.  Return NULL if there is unacked data at tcp
    2641                 :             :  * level.
    2642                 :             :  *
    2643                 :             :  * A backup subflow is returned only if that is the only kind available.
    2644                 :             :  */
    2645                 :        5850 : struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
    2646                 :             : {
    2647                 :        5850 :         struct sock *backup = NULL, *pick = NULL;
    2648                 :        5850 :         struct mptcp_subflow_context *subflow;
    2649                 :        5850 :         int min_stale_count = INT_MAX;
    2650                 :             : 
    2651            [ + ]:       14888 :         mptcp_for_each_subflow(msk, subflow) {
    2652                 :        9038 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2653                 :             : 
    2654            [ + ]:        9038 :                 if (!__mptcp_subflow_active(subflow))
    2655                 :        1863 :                         continue;
    2656                 :             : 
    2657                 :             :                 /* still data outstanding at TCP level? skip this */
    2658            [ + ]:        7175 :                 if (!tcp_rtx_and_write_queues_empty(ssk)) {
    2659                 :        2980 :                         min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
    2660                 :        2980 :                         continue;
    2661                 :             :                 }
    2662                 :             : 
    2663      [ +  +  + ]:        4195 :                 if (subflow->backup || subflow->request_bkup) {
    2664            [ + ]:         129 :                         if (!backup)
    2665                 :         102 :                                 backup = ssk;
    2666                 :         129 :                         continue;
    2667                 :             :                 }
    2668                 :             : 
    2669            [ + ]:        4066 :                 if (!pick)
    2670                 :        3038 :                         pick = ssk;
    2671                 :             :         }
    2672                 :             : 
    2673            [ + ]:        5850 :         if (pick)
    2674                 :             :                 return pick;
    2675                 :             : 
    2676                 :             :         /* use backup only if there are no progresses anywhere */
    2677            [ + ]:        2812 :         return min_stale_count > 1 ? backup : NULL;
    2678                 :             : }
    2679                 :             : 
    2680                 :        2344 : bool __mptcp_retransmit_pending_data(struct sock *sk)
    2681                 :             : {
    2682                 :        2344 :         struct mptcp_data_frag *cur, *rtx_head;
    2683            [ + ]:        2344 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2684                 :             : 
    2685            [ + ]:        2344 :         if (__mptcp_check_fallback(msk))
    2686                 :             :                 return false;
    2687                 :             : 
    2688                 :             :         /* the closing socket has some data untransmitted and/or unacked:
    2689                 :             :          * some data in the mptcp rtx queue has not really xmitted yet.
    2690                 :             :          * keep it simple and re-inject the whole mptcp level rtx queue
    2691                 :             :          */
    2692                 :        2268 :         mptcp_data_lock(sk);
    2693                 :        2268 :         __mptcp_clean_una_wakeup(sk);
    2694                 :        2268 :         rtx_head = mptcp_rtx_head(sk);
    2695       [ + ][ + ]:        2268 :         if (!rtx_head) {
    2696                 :        2177 :                 mptcp_data_unlock(sk);
    2697                 :        2177 :                 return false;
    2698                 :             :         }
    2699                 :             : 
    2700                 :          91 :         msk->recovery_snd_nxt = msk->snd_nxt;
    2701                 :          91 :         msk->recovery = true;
    2702                 :          91 :         mptcp_data_unlock(sk);
    2703                 :             : 
    2704                 :          91 :         msk->first_pending = rtx_head;
    2705                 :          91 :         msk->snd_burst = 0;
    2706                 :             : 
    2707                 :             :         /* be sure to clear the "sent status" on all re-injected fragments */
    2708       [ + ][ + ]:        5175 :         list_for_each_entry(cur, &msk->rtx_queue, list) {
    2709       [ + ][ + ]:        5101 :                 if (!cur->already_sent)
    2710                 :             :                         break;
    2711                 :        5084 :                 cur->already_sent = 0;
    2712                 :             :         }
    2713                 :             : 
    2714                 :             :         return true;
    2715                 :             : }
    2716                 :             : 
    2717                 :             : /* flags for __mptcp_close_ssk() */
    2718                 :             : #define MPTCP_CF_PUSH           BIT(1)
    2719                 :             : 
    2720                 :             : /* be sure to send a reset only if the caller asked for it, also
    2721                 :             :  * clean completely the subflow status when the subflow reaches
    2722                 :             :  * TCP_CLOSE state
    2723                 :             :  */
    2724                 :        1865 : static void __mptcp_subflow_disconnect(struct sock *ssk,
    2725                 :             :                                        struct mptcp_subflow_context *subflow,
    2726                 :             :                                        bool fastclosing)
    2727                 :             : {
    2728      [ +  +  + ]:        1865 :         if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
                 [ +  + ]
    2729                 :             :             fastclosing) {
    2730                 :             :                 /* The MPTCP code never wait on the subflow sockets, TCP-level
    2731                 :             :                  * disconnect should never fail
    2732                 :             :                  */
    2733       [ + ][ + ]:        1835 :                 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
    2734                 :        1835 :                 mptcp_subflow_ctx_reset(subflow);
    2735                 :             :         } else {
    2736                 :          30 :                 tcp_shutdown(ssk, SEND_SHUTDOWN);
    2737                 :             :         }
    2738                 :        1865 : }
    2739                 :             : 
    2740                 :        2327 : static void mptcp_cleanup_ssk_backlog(struct sock *sk, struct sock *ssk)
    2741                 :             : {
    2742            [ + ]:        2327 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2743                 :        2327 :         struct sk_buff *skb;
    2744                 :             : 
    2745                 :        2327 :         mptcp_data_lock(sk);
    2746       [ + ][ + ]:        2333 :         list_for_each_entry(skb, &msk->backlog_list, list) {
    2747            [ - ]:           6 :                 if (skb->sk != ssk)
    2748                 :           6 :                         continue;
    2749                 :             : 
    2750                 :           0 :                 atomic_sub(skb->truesize, &skb->sk->sk_rmem_alloc);
    2751                 :           0 :                 skb->sk = NULL;
    2752                 :             :         }
    2753                 :        2327 :         mptcp_data_unlock(sk);
    2754                 :        2327 : }
    2755                 :             : 
    2756                 :             : static void mptcp_check_fastclose(struct mptcp_sock *msk);
    2757                 :             : 
    2758                 :             : /* After the close, a pending DATA_FIN or MP_FASTCLOSE would be ignored */
    2759                 :         594 : static void mptcp_check_rcv_close(struct sock *sk)
    2760                 :             : {
    2761                 :             :         /* process the DATA_FIN after the data still in the backlog */
    2762      [ +  +  + ]:         594 :         if (READ_ONCE(mptcp_sk(sk)->rcv_data_fin)) {
                 [ +  + ]
    2763                 :         172 :                 mptcp_move_skbs(sk);
    2764                 :         172 :                 mptcp_check_data_fin(sk);
    2765                 :             :         }
    2766                 :             : 
    2767       [ + ][ + ]:         594 :         mptcp_check_fastclose(mptcp_sk(sk));
    2768                 :         594 : }
    2769                 :             : 
    2770                 :             : /* subflow sockets can be either outgoing (connect) or incoming
    2771                 :             :  * (accept).
    2772                 :             :  *
    2773                 :             :  * Outgoing subflows use in-kernel sockets.
    2774                 :             :  * Incoming subflows do not have their own 'struct socket' allocated,
    2775                 :             :  * so we need to use tcp_close() after detaching them from the mptcp
    2776                 :             :  * parent socket.
    2777                 :             :  */
    2778                 :        8405 : static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
    2779                 :             :                               struct mptcp_subflow_context *subflow,
    2780                 :             :                               unsigned int flags)
    2781                 :             : {
    2782            [ + ]:        8405 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2783                 :        8405 :         bool dispose_it, need_push = false;
    2784                 :        8405 :         int fwd_remaining;
    2785                 :             : 
    2786                 :             :         /* Do not pass RX data to the msk, even if the subflow socket is not
    2787                 :             :          * going to be freed (i.e. even for the first subflow on graceful
    2788                 :             :          * subflow close.
    2789                 :             :          */
    2790                 :        8405 :         lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
    2791                 :        8405 :         subflow->closing = 1;
    2792                 :             : 
    2793            [ + ]:        8405 :         if (flags & MPTCP_CF_PUSH)
    2794                 :        2327 :                 mptcp_cleanup_ssk_backlog(sk, ssk);
    2795                 :             : 
    2796                 :             :         /* Borrow the fwd allocated page left-over; fwd memory for the subflow
    2797                 :             :          * could be negative at this point, but will be reach zero soon - when
    2798                 :             :          * the data allocated using such fragment will be freed.
    2799                 :             :          */
    2800            [ + ]:        8405 :         if (subflow->lent_mem_frag) {
    2801                 :        2966 :                 fwd_remaining = PAGE_SIZE - subflow->lent_mem_frag;
    2802                 :        2966 :                 sk_forward_alloc_add(sk, fwd_remaining);
    2803                 :        2966 :                 sk_forward_alloc_add(ssk, -fwd_remaining);
    2804                 :        2966 :                 subflow->lent_mem_frag = 0;
    2805                 :             :         }
    2806                 :             : 
    2807                 :             :         /* also before accept(), not to reset again in inet_child_forget() */
    2808         [ +  + ]:        8405 :         if (subflow->send_fastclose && ssk->sk_state != TCP_CLOSE)
    2809                 :         483 :                 tcp_set_state(ssk, TCP_CLOSE);
    2810                 :             : 
    2811                 :             :         /* If the first subflow moved to a close state before accept, e.g. due
    2812                 :             :          * to an incoming reset or listener shutdown, the subflow socket is
    2813                 :             :          * already deleted by inet_child_forget() and the mptcp socket can't
    2814                 :             :          * survive too.
    2815                 :             :          */
    2816      [ +  +  + ]:        8449 :         if (msk->in_accept_queue && msk->first == ssk &&
    2817            [ + ]:          46 :             (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
    2818                 :             :                 /* ensure later check in mptcp_worker() will dispose the msk */
    2819                 :          42 :                 sock_set_flag(sk, SOCK_DEAD);
    2820                 :          42 :                 mptcp_set_close_tout(sk, tcp_jiffies32 - (mptcp_close_timeout(sk) + 1));
    2821                 :          42 :                 mptcp_subflow_drop_ctx(ssk);
    2822                 :          42 :                 goto out_release;
    2823                 :             :         }
    2824                 :             : 
    2825 [ +  + ][ +  + ]:        8363 :         dispose_it = msk->free_first || ssk != msk->first;
    2826                 :             :         if (dispose_it)
    2827                 :        6498 :                 list_del(&subflow->node);
    2828                 :             : 
    2829 [ +  + ][ +  + ]:        8363 :         need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
    2830       [ + ][ + ]:        8363 :         if (!dispose_it) {
    2831                 :        1865 :                 __mptcp_subflow_disconnect(ssk, subflow, msk->fastclosing);
    2832                 :        1865 :                 release_sock(ssk);
    2833                 :             : 
    2834                 :        1865 :                 goto out;
    2835                 :             :         }
    2836                 :             : 
    2837                 :        6498 :         subflow->disposable = 1;
    2838                 :             : 
    2839                 :             :         /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
    2840                 :             :          * the ssk has been already destroyed, we just need to release the
    2841                 :             :          * reference owned by msk;
    2842                 :             :          */
    2843       [ + ][ + ]:        6498 :         if (!inet_csk(ssk)->icsk_ulp_ops) {
    2844       [ # ][ # ]:           0 :                 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
    2845                 :           0 :                 mptcp_subflow_free_ctx(subflow);
    2846                 :             :         } else {
    2847                 :             :                 /* otherwise tcp will dispose of the ssk and subflow ctx */
    2848                 :        6498 :                 __tcp_close(ssk, 0);
    2849                 :             : 
    2850                 :             :                 /* close acquired an extra ref */
    2851                 :        6498 :                 __sock_put(ssk);
    2852                 :             :         }
    2853                 :             : 
    2854                 :        6540 : out_release:
    2855                 :        6540 :         __mptcp_subflow_error_report(sk, ssk);
    2856                 :        6540 :         release_sock(ssk);
    2857                 :             : 
    2858                 :        6540 :         sock_put(ssk);
    2859                 :             : 
    2860       [ + ][ + ]:        6540 :         if (ssk == msk->first)
    2861                 :        5221 :                 WRITE_ONCE(msk->first, NULL);
    2862                 :             : 
    2863                 :        1319 : out:
    2864                 :        8405 :         __mptcp_sync_sndbuf(sk);
    2865       [ + ][ + ]:        8405 :         if (need_push)
    2866                 :          74 :                 __mptcp_push_pending(sk, 0);
    2867                 :             : 
    2868                 :             :         /* Catch every 'all subflows closed' scenario, including peers silently
    2869                 :             :          * closing them, e.g. due to timeout.
    2870                 :             :          * For established sockets, allow an additional timeout before closing,
    2871                 :             :          * as the protocol can still create more subflows.
    2872                 :             :          */
    2873      [ +  +  + ]:       12533 :         if (list_is_singular(&msk->conn_list) && msk->first &&
              [ +  +  + ]
         [ +  + ][ +  + ]
    2874       [ - ][ - ]:          78 :             inet_sk_state_load(msk->first) == TCP_CLOSE) {
    2875       [ + ][ + ]:        1814 :                 if (sock_flag(sk, SOCK_DEAD)) {
    2876                 :             :                         /* let the worker close and destroy the orphan */
    2877                 :        1198 :                         mptcp_set_close_tout(sk, tcp_jiffies32 -
    2878                 :        1198 :                                              (mptcp_close_timeout(sk) + 1));
    2879                 :        1198 :                         mptcp_schedule_work(sk);
    2880 [ +  + ][ +  + ]:         616 :                 } else if (sk->sk_state != TCP_ESTABLISHED ||
    2881                 :             :                            msk->in_accept_queue) {
    2882                 :         588 :                         mptcp_check_rcv_close(sk);
    2883                 :         588 :                         mptcp_set_state(sk, TCP_CLOSE);
    2884                 :         588 :                         WRITE_ONCE(sk->sk_shutdown,
    2885                 :             :                                    sk->sk_shutdown | SEND_SHUTDOWN);
    2886                 :         588 :                         mptcp_close_wake_up(sk);
    2887                 :             :                 } else {
    2888                 :          28 :                         mptcp_start_tout_timer(sk);
    2889                 :             :                 }
    2890      [ +  +  + ]:        7015 :         } else if (list_is_singular(&msk->conn_list) && msk->first &&
              [ +  +  + ]
    2891       [ # ][ # ]:           0 :                    mptcp_subflow_ctx(msk->first)->closing &&
    2892       [ # ][ # ]:           0 :                    !inet_csk(sk)->icsk_mtup.probe_timestamp &&
    2893       [ # ][ # ]:           0 :                    sk->sk_state != TCP_CLOSE) {
    2894                 :             :                 /* The initial subflow has only been shut down, and its later
    2895                 :             :                  * close will be ignored: start the close timeout now.
    2896                 :             :                  */
    2897                 :           0 :                 mptcp_start_tout_timer(sk);
    2898                 :             :         }
    2899                 :        8405 : }
    2900                 :             : 
    2901                 :        2373 : void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
    2902                 :             :                      struct mptcp_subflow_context *subflow)
    2903                 :             : {
    2904            [ + ]:        2373 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2905                 :             : 
    2906                 :             :         /* The first subflow can already be closed or disconnected */
    2907      [ +  +  + ]:        2373 :         if (subflow->close_event_done || READ_ONCE(subflow->local_id) < 0)
                 [ +  + ]
    2908                 :             :                 return;
    2909                 :             : 
    2910                 :        2327 :         subflow->close_event_done = true;
    2911                 :             : 
    2912       [ + ][ + ]:        2327 :         if (sk->sk_state == TCP_ESTABLISHED)
    2913                 :         354 :                 mptcp_event(MPTCP_EVENT_SUB_CLOSED, msk, ssk, GFP_KERNEL);
    2914                 :             : 
    2915                 :             :         /* subflow aborted before reaching the fully_established status
    2916                 :             :          * attempt the creation of the next subflow
    2917                 :             :          */
    2918                 :        2327 :         mptcp_pm_subflow_check_next(msk, subflow);
    2919                 :             : 
    2920                 :             :         /* On fallback, the FIN of the first subflow is cleared when it is
    2921                 :             :          * disconnected: process the DATA_FIN emulated from it before.
    2922                 :             :          */
    2923 [ +  + ][ +  + ]:        2327 :         if (ssk == msk->first && __mptcp_check_fallback(msk) &&
    2924      [ +  +  - ]:          76 :             READ_ONCE(msk->rcv_data_fin) && !sock_flag(sk, SOCK_DEAD)) {
                 [ +  - ]
    2925                 :          14 :                 mptcp_move_skbs(sk);
    2926                 :          14 :                 mptcp_check_data_fin(sk);
    2927                 :             :         }
    2928                 :             : 
    2929                 :        2327 :         __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
    2930                 :             : }
    2931                 :             : 
    2932                 :           0 : static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
    2933                 :             : {
    2934                 :           0 :         return 0;
    2935                 :             : }
    2936                 :             : 
    2937                 :        1663 : static void __mptcp_close_subflow(struct sock *sk)
    2938                 :             : {
    2939                 :        1663 :         struct mptcp_subflow_context *subflow, *tmp;
    2940            [ + ]:        1663 :         struct mptcp_sock *msk = mptcp_sk(sk);
    2941                 :             : 
    2942                 :        1663 :         might_sleep();
    2943                 :             : 
    2944       [ + ][ + ]:        4197 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    2945                 :        2534 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    2946                 :        2534 :                 int ssk_state = inet_sk_state_load(ssk);
    2947                 :             : 
    2948 [ +  + ][ +  + ]:        2534 :                 if (ssk_state != TCP_CLOSE &&
    2949       [ + ][ + ]:          85 :                     (ssk_state != TCP_CLOSE_WAIT ||
    2950       [ + ][ + ]:          42 :                      inet_sk_state_load(sk) != TCP_ESTABLISHED ||
         [ #  # ][ #  # ]
    2951                 :          42 :                      __mptcp_check_fallback(msk)))
    2952                 :         365 :                         continue;
    2953                 :             : 
    2954                 :             :                 /* 'subflow_data_ready' will re-sched once rx queue is empty */
    2955       [ + ][ + ]:        2169 :                 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
    2956                 :           0 :                         continue;
    2957                 :             : 
    2958                 :        2169 :                 mptcp_close_ssk(sk, ssk, subflow);
    2959                 :             :         }
    2960                 :             : 
    2961                 :        1663 : }
    2962                 :             : 
    2963                 :       38220 : static bool mptcp_close_tout_expired(const struct sock *sk)
    2964                 :             : {
    2965            [ + ]:       38220 :         if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
    2966            [ + ]:       16128 :             sk->sk_state == TCP_CLOSE)
    2967                 :             :                 return false;
    2968                 :             : 
    2969                 :       12669 :         return time_after32(tcp_jiffies32,
    2970                 :             :                   inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
    2971                 :             : }
    2972                 :             : 
    2973                 :             : /* Would TCP abort this subflow if it were an orphan probing a zero window? */
    2974                 :           0 : static bool mptcp_subflow_probe_timed_out(struct sock *ssk)
    2975                 :             : {
    2976            [ # ]:           0 :         u8 retries2 = READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_retries2);
    2977                 :           0 :         struct inet_connection_sock *icsk = inet_csk(ssk);
    2978       [ # ][ # ]:           0 :         const struct tcp_sock *tp = tcp_sk(ssk);
    2979                 :           0 :         unsigned long limit;
    2980                 :           0 :         bool slow, ret;
    2981                 :           0 :         u64 when;
    2982                 :             : 
    2983                 :           0 :         slow = lock_sock_fast_nested(ssk);
    2984       [ # ][ # ]:           0 :         limit = tcp_rto_max(ssk);
    2985       [ # ][ # ]:           0 :         if (tp->packets_out) {
    2986 [ #  # ][ #  # ]:           0 :                 ret = !tp->snd_wnd && icsk->icsk_rto >= limit;
    2987                 :             :         } else {
    2988       [ # ][ # ]:           0 :                 if (icsk->icsk_backoff < retries2)
    2989                 :           0 :                         limit *= 2;
    2990                 :             :                 /* a shift of 32 already exceeds the limit */
    2991                 :           0 :                 when = (u64)icsk->icsk_rto << min_t(u8, icsk->icsk_backoff, 32);
    2992      [ #  #  # ]:           0 :                 ret = tcp_send_head(ssk) && when >= limit;
              [ #  #  # ]
    2993                 :             :         }
    2994                 :           0 :         unlock_sock_fast(ssk, slow);
    2995                 :             : 
    2996                 :           0 :         return ret;
    2997                 :             : }
    2998                 :             : 
    2999                 :             : /* As TCP orphans: limited in number and memory */
    3000                 :        1508 : static bool mptcp_orphan_oom(struct sock *sk)
    3001                 :             : {
    3002       [ + ][ + ]:        1508 :         if (!tcp_check_oom(sk, 0))
    3003                 :             :                 return false;
    3004                 :             : 
    3005                 :           0 :         NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
    3006                 :           0 :         return true;
    3007                 :             : }
    3008                 :             : 
    3009                 :             : /* Before its DATA_FIN, keep an orphan while a subflow can send its data */
    3010                 :         313 : static bool mptcp_orphan_keep(struct sock *sk)
    3011                 :             : {
    3012            [ + ]:         313 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3013                 :         313 :         struct mptcp_subflow_context *subflow;
    3014                 :             : 
    3015         [ +  + ]:         313 :         if (!sock_flag(sk, SOCK_DEAD) || msk->snd_nxt == msk->write_seq)
    3016                 :             :                 return false;
    3017                 :             : 
    3018         [ +  + ]:          12 :         mptcp_for_each_subflow(msk, subflow) {
    3019                 :           6 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3020                 :             : 
    3021                 :             :                 /* a live peer acks at least the probes, every TCP_RTO_MAX */
    3022            [ + ]:           6 :                 if (__mptcp_subflow_active(subflow) &&
    3023         [ #  # ]:           0 :                     tcp_jiffies32 - READ_ONCE(tcp_sk(ssk)->rcv_tstamp) <
    3024            [ # ]:           0 :                     2 * TCP_RTO_MAX &&
    3025                 :           0 :                     !mptcp_subflow_probe_timed_out(ssk))
    3026                 :           0 :                         return !mptcp_orphan_oom(sk);
    3027                 :             :         }
    3028                 :             : 
    3029                 :             :         return false;
    3030                 :             : }
    3031                 :             : 
    3032                 :             : /* On fallback, TCP can send what an orphan gave to the subflow. As in
    3033                 :             :  * __mptcp_close(), unread data requires a reset, also in the backlog: stop
    3034                 :             :  * passing data to the msk first, TCP resets on what is received later.
    3035                 :             :  */
    3036                 :         313 : static bool mptcp_orphan_handover(struct sock *sk)
    3037                 :             : {
    3038            [ + ]:         313 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3039                 :         313 :         struct mptcp_subflow_context *subflow;
    3040                 :             : 
    3041         [ +  + ]:         313 :         if (!sock_flag(sk, SOCK_DEAD) || !__mptcp_check_fallback(msk) ||
    3042            [ + ]:          21 :             msk->snd_nxt != msk->write_seq)
    3043                 :         292 :                 return false;
    3044                 :             : 
    3045            [ + ]:          42 :         mptcp_for_each_subflow(msk, subflow) {
    3046                 :          21 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3047                 :             : 
    3048                 :          21 :                 lock_sock(ssk);
    3049                 :          21 :                 subflow->closing = 1;
    3050                 :          21 :                 release_sock(ssk);
    3051                 :             :         }
    3052                 :             : 
    3053         [ -  + ]:          21 :         return !mptcp_data_avail(msk) && !READ_ONCE(msk->backlog_len);
    3054                 :             : }
    3055                 :             : 
    3056                 :       13334 : static void mptcp_check_fastclose(struct mptcp_sock *msk)
    3057                 :             : {
    3058                 :       13334 :         struct mptcp_subflow_context *subflow, *tmp;
    3059                 :       13334 :         struct sock *sk = (struct sock *)msk;
    3060                 :             : 
    3061    [ +  + ][ + ]:       13334 :         if (likely(!READ_ONCE(msk->rcv_fastclose)))
    3062                 :             :                 return;
    3063                 :             : 
    3064                 :             :         /* a DATA_FIN received before still leads to EOF, as with TCP */
    3065    [ +  + ][ + ]:         344 :         if (READ_ONCE(msk->rcv_data_fin))
    3066                 :         158 :                 mptcp_move_skbs(sk);
    3067                 :         344 :         mptcp_check_data_fin(sk);
    3068                 :             : 
    3069                 :         344 :         mptcp_token_destroy(msk);
    3070                 :             : 
    3071       [ + ][ + ]:         722 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    3072                 :         378 :                 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
    3073                 :         378 :                 bool slow;
    3074                 :             : 
    3075                 :         378 :                 slow = lock_sock_fast(tcp_sk);
    3076       [ + ][ + ]:         378 :                 if (tcp_sk->sk_state != TCP_CLOSE) {
    3077                 :          28 :                         mptcp_send_active_reset_reason(tcp_sk);
    3078                 :          28 :                         tcp_set_state(tcp_sk, TCP_CLOSE);
    3079                 :             :                 }
    3080                 :         378 :                 unlock_sock_fast(tcp_sk, slow);
    3081                 :             :         }
    3082                 :             : 
    3083                 :             :         /* Mirror the tcp_reset() error propagation */
    3084   [ -  +  +  + ]:         344 :         switch (sk->sk_state) {
           [ -  +  +  + ]
    3085                 :             :         case TCP_SYN_SENT:
    3086                 :           0 :                 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
    3087                 :           0 :                 break;
    3088                 :             :         case TCP_CLOSE_WAIT:
    3089                 :          84 :                 WRITE_ONCE(sk->sk_err, EPIPE);
    3090                 :          84 :                 break;
    3091                 :             :         case TCP_CLOSE:
    3092                 :             :                 return;
    3093                 :             :         default:
    3094                 :          68 :                 WRITE_ONCE(sk->sk_err, ECONNRESET);
    3095                 :             :         }
    3096                 :             : 
    3097                 :         152 :         mptcp_set_state(sk, TCP_CLOSE);
    3098                 :         152 :         WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
    3099                 :         152 :         smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
    3100                 :         152 :         set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
    3101                 :             : 
    3102                 :             :         /* the calling mptcp_worker will properly destroy the socket */
    3103       [ + ][ + ]:         152 :         if (sock_flag(sk, SOCK_DEAD))
    3104                 :             :                 return;
    3105                 :             : 
    3106                 :         112 :         sk->sk_state_change(sk);
    3107                 :         112 :         sk_error_report(sk);
    3108                 :             : }
    3109                 :             : 
    3110                 :             : /*
    3111                 :             :  * Retransmit the specified data fragment on all the selected subflows,
    3112                 :             :  * starting from the specified sequence
    3113                 :             :  */
    3114                 :       12229 : static int __mptcp_push_retrans(struct sock *sk, struct mptcp_data_frag *dfrag,
    3115                 :             :                                 u64 sent_seq)
    3116                 :             : {
    3117                 :       12229 :         struct mptcp_sendmsg_info info = { .data_lock_held = true, };
    3118            [ + ]:       12229 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3119                 :       12229 :         struct mptcp_subflow_context *subflow;
    3120                 :       12229 :         struct sock *ssk;
    3121                 :       12229 :         int ret, len = 0;
    3122                 :             : 
    3123       [ + ][ + ]:       36353 :         mptcp_for_each_subflow(msk, subflow) {
    3124    [ +  + ][ + ]:       24124 :                 if (READ_ONCE(subflow->scheduled)) {
    3125                 :       13877 :                         u16 offset = sent_seq - dfrag->data_seq;
    3126                 :       13877 :                         u16 copied = 0;
    3127                 :             : 
    3128                 :       13877 :                         mptcp_subflow_set_scheduled(subflow, false);
    3129                 :             : 
    3130                 :       13877 :                         ssk = mptcp_subflow_tcp_sock(subflow);
    3131                 :             : 
    3132                 :       13877 :                         lock_sock(ssk);
    3133                 :             : 
    3134                 :             :                         /* limit retransmission to the bytes already sent on some subflows */
    3135                 :       13877 :                         info.sent = offset;
    3136    [ +  + ][ + ]:       13877 :                         info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
    3137                 :             :                                                                     dfrag->already_sent;
    3138                 :             : 
    3139                 :             :                         /*
    3140                 :             :                          * make the whole retrans decision, xmit, disallow
    3141                 :             :                          * fallback atomic, note that we can't retrans even
    3142                 :             :                          * when an infinite fallback is in progress, i.e. new
    3143                 :             :                          * subflows are disallowed.
    3144                 :             :                          */
    3145                 :       13877 :                         spin_lock_bh(&msk->fallback_lock);
    3146       [ - ][ - ]:       13877 :                         if (__mptcp_check_fallback(msk) ||
    3147    [ +  + ][ + ]:       13877 :                             !msk->allow_subflows) {
    3148                 :           0 :                                 spin_unlock_bh(&msk->fallback_lock);
    3149                 :           0 :                                 release_sock(ssk);
    3150                 :           0 :                                 return -1;
    3151                 :             :                         }
    3152                 :             : 
    3153       [ + ][ + ]:       27766 :                         while (info.sent < info.limit) {
    3154                 :       13889 :                                 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
    3155       [ - ][ - ]:       13889 :                                 if (ret <= 0)
    3156                 :             :                                         break;
    3157                 :             : 
    3158       [ - ][ - ]:       13889 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
    3159                 :       13889 :                                 copied += ret;
    3160                 :       13889 :                                 info.sent += ret;
    3161                 :             :                         }
    3162       [ - ][ - ]:       13877 :                         if (copied) {
    3163                 :       13877 :                                 len = max(copied, len);
    3164       [ + ][ + ]:       13877 :                                 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
    3165                 :             :                                          info.size_goal);
    3166                 :       13877 :                                 msk->allow_infinite_fallback = false;
    3167                 :             :                         }
    3168                 :       13877 :                         spin_unlock_bh(&msk->fallback_lock);
    3169                 :             : 
    3170                 :       13877 :                         release_sock(ssk);
    3171                 :             :                 }
    3172                 :             :         }
    3173                 :             :         return len;
    3174                 :             : }
    3175                 :             : 
    3176                 :       11594 : static void __mptcp_retrans(struct sock *sk)
    3177                 :             : {
    3178            [ + ]:       11594 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3179                 :       11594 :         struct mptcp_subflow_context *subflow;
    3180                 :       11594 :         struct mptcp_data_frag *dfrag;
    3181                 :       11594 :         u64 retrans_seq, sent_seq;
    3182                 :       11594 :         bool need_retrans;
    3183                 :       11594 :         int err, len;
    3184                 :             : 
    3185                 :       11594 :         mptcp_pm_chk_stale(msk);
    3186                 :             : 
    3187                 :             :         /* Get an updated and consistent rtx queue status. */
    3188                 :       11594 :         mptcp_data_lock(sk);
    3189                 :       11594 :         __mptcp_clean_una_wakeup(sk);
    3190                 :       11594 :         retrans_seq = msk->snd_una;
    3191                 :       11594 :         dfrag = mptcp_rtx_head(sk);
    3192                 :       11594 :         need_retrans = !!dfrag;
    3193                 :       11594 :         mptcp_data_unlock(sk);
    3194                 :             : 
    3195                 :        7672 :         for (;;) {
    3196                 :       19266 :                 bool already_acked;
    3197                 :             : 
    3198                 :       19266 :                 err = mptcp_sched_get_retrans(msk);
    3199       [ + ][ + ]:       19266 :                 if (err)
    3200                 :             :                         break;
    3201                 :             : 
    3202                 :             :                 /* `already_sent` can be 0 for `dfrag` belonging to the RTX
    3203                 :             :                  *  queue due to __mptcp_retransmit_pending_data().
    3204                 :             :                  */
    3205 [ +  - ][ +  - ]:       16481 :                 if (!dfrag || !dfrag->already_sent)
    3206                 :             :                         break;
    3207                 :             : 
    3208                 :             :                 /* Can fail only in case of fallback. */
    3209                 :       12229 :                 len = __mptcp_push_retrans(sk, dfrag, retrans_seq);
    3210       [ + ][ + ]:       12229 :                 if (len < 0)
    3211                 :           0 :                         goto clear_scheduled;
    3212                 :             : 
    3213                 :       12229 :                 retrans_seq += len;
    3214                 :       12229 :                 msk->bytes_retrans += len;
    3215                 :       12229 :                 dfrag->already_sent = max_t(u16, dfrag->already_sent,
    3216                 :             :                                             retrans_seq - dfrag->data_seq);
    3217                 :             : 
    3218                 :             :                 /* With csum enabled, retransmission can send new data. */
    3219                 :       12229 :                 sent_seq = dfrag->already_sent + dfrag->data_seq;
    3220       [ + ][ + ]:       12229 :                 if (after64(sent_seq, msk->snd_nxt))
    3221                 :           0 :                         WRITE_ONCE(msk->snd_nxt, sent_seq);
    3222                 :             : 
    3223                 :             :                 /* Attempt the next fragment only if the current one is
    3224                 :             :                  * completely retransmitted.
    3225                 :             :                  */
    3226       [ + ][ - ]:       12229 :                 if (before64(retrans_seq, dfrag->data_seq + dfrag->data_len))
    3227                 :             :                         break;
    3228                 :             : 
    3229       [ + ][ + ]:       12226 :                 dfrag = list_is_last(&dfrag->list, &msk->rtx_queue) ?
    3230       [ + ][ + ]:       12226 :                                 NULL : list_next_entry(dfrag, list);
    3231       [ - ][ - ]:        7672 :                 if (!dfrag)
    3232                 :             :                         break;
    3233                 :             : 
    3234                 :             :                 /* Incoming acks can move snd_una after the current dfrag
    3235                 :             :                  * across loop iterations, if so start again from RTX head.
    3236                 :             :                  */
    3237                 :        7672 :                 mptcp_data_lock(sk);
    3238                 :        7672 :                 already_acked = !before64(msk->snd_una, dfrag->data_seq +
    3239       [ + ][ + ]:        7672 :                                           dfrag->already_sent);
    3240       [ + ][ + ]:        7672 :                 if (already_acked) {
    3241                 :        2294 :                         __mptcp_clean_una_wakeup(sk);
    3242                 :        2294 :                         retrans_seq = msk->snd_una;
    3243                 :        2294 :                         dfrag = mptcp_rtx_head(sk);
    3244                 :        2294 :                         need_retrans = !!dfrag;
    3245       [ + ][ + ]:        5378 :                 } else if (after64(msk->snd_una, retrans_seq)) {
    3246                 :           0 :                         retrans_seq = msk->snd_una;
    3247                 :             :                 }
    3248                 :        7672 :                 mptcp_data_unlock(sk);
    3249                 :             :         }
    3250                 :             : 
    3251                 :             :         /* Attempt data-fin retransmission only when the RTX queue is empty. */
    3252       [ + ][ + ]:       11594 :         if (!need_retrans) {
    3253       [ + ][ + ]:        4420 :                 if (mptcp_data_fin_enabled(msk)) {
    3254                 :        1398 :                         struct inet_connection_sock *icsk = inet_csk(sk);
    3255                 :             : 
    3256                 :        1398 :                         WRITE_ONCE(icsk->icsk_retransmits,
    3257                 :             :                                    icsk->icsk_retransmits + 1);
    3258                 :        1398 :                         mptcp_set_datafin_timeout(sk);
    3259                 :        1398 :                         mptcp_send_ack(msk);
    3260                 :        1398 :                         goto reset_timer;
    3261                 :             :                 }
    3262                 :             : 
    3263       [ + ][ + ]:        3022 :                 if (!mptcp_send_head(sk))
    3264                 :        2956 :                         goto clear_scheduled;
    3265                 :             :         }
    3266                 :             : 
    3267                 :        7127 : reset_timer:
    3268                 :        8638 :         mptcp_check_and_set_pending(sk);
    3269                 :             : 
    3270       [ + ][ + ]:        8638 :         if (!mptcp_rtx_timer_pending(sk))
    3271                 :         252 :                 mptcp_reset_rtx_timer(sk);
    3272                 :             : 
    3273                 :        8386 : clear_scheduled:
    3274                 :             :         /* If no rtx data was available or in case of fallback, there
    3275                 :             :          * could be left-over scheduled subflows; clear them all
    3276                 :             :          * or later xmit could use bad ones
    3277                 :             :          */
    3278       [ + ][ + ]:       32468 :         mptcp_for_each_subflow(msk, subflow)
    3279    [ +  + ][ + ]:       20874 :                 if (READ_ONCE(subflow->scheduled))
    3280                 :        4925 :                         mptcp_subflow_set_scheduled(subflow, false);
    3281                 :       11594 : }
    3282                 :             : 
    3283                 :             : /* schedule the timeout timer for the relevant event: either close timeout
    3284                 :             :  * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
    3285                 :             :  */
    3286                 :        1705 : void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
    3287                 :             : {
    3288                 :        1705 :         struct sock *sk = (struct sock *)msk;
    3289                 :        1705 :         unsigned long timeout, close_timeout;
    3290                 :             : 
    3291         [ +  - ]:        1705 :         if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
    3292                 :             :                 return;
    3293                 :             : 
    3294                 :        1705 :         close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
    3295                 :        1705 :                         tcp_jiffies32;
    3296                 :        1705 :         close_timeout += jiffies + mptcp_close_timeout(sk);
    3297                 :             : 
    3298                 :             :         /* the close timeout takes precedence on the fail one, and here at least one of
    3299                 :             :          * them is active
    3300                 :             :          */
    3301            [ + ]:        1705 :         timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
    3302                 :             : 
    3303                 :        1705 :         sk_reset_timer(sk, &inet_csk(sk)->mptcp_tout_timer, timeout);
    3304                 :             : }
    3305                 :             : 
    3306                 :           0 : static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
    3307                 :             : {
    3308                 :           0 :         struct sock *ssk = msk->first;
    3309                 :           0 :         bool slow;
    3310                 :             : 
    3311            [ # ]:           0 :         if (!ssk)
    3312                 :             :                 return;
    3313                 :             : 
    3314         [ #  # ]:           0 :         pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
    3315                 :             : 
    3316                 :           0 :         slow = lock_sock_fast(ssk);
    3317                 :           0 :         mptcp_subflow_reset(ssk);
    3318                 :           0 :         WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
    3319                 :           0 :         unlock_sock_fast(ssk, slow);
    3320                 :             : }
    3321                 :             : 
    3322                 :        6105 : static void mptcp_backlog_purge(struct sock *sk)
    3323                 :             : {
    3324            [ + ]:        6105 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3325                 :        6105 :         struct sk_buff *tmp, *skb;
    3326                 :        6105 :         LIST_HEAD(backlog);
    3327                 :             : 
    3328                 :        6105 :         mptcp_data_lock(sk);
    3329            [ + ]:        6105 :         list_splice_init(&msk->backlog_list, &backlog);
    3330                 :        6105 :         msk->backlog_len = 0;
    3331                 :        6105 :         mptcp_data_unlock(sk);
    3332                 :             : 
    3333            [ - ]:        6105 :         list_for_each_entry_safe(skb, tmp, &backlog, list) {
    3334                 :           0 :                 mptcp_borrow_fwdmem(sk, skb);
    3335                 :           0 :                 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE);
    3336                 :             :         }
    3337                 :        6105 :         sk_mem_reclaim(sk);
    3338                 :        6105 : }
    3339                 :             : 
    3340                 :         744 : static void mptcp_do_fastclose(struct sock *sk)
    3341                 :             : {
    3342                 :         744 :         struct mptcp_subflow_context *subflow, *tmp;
    3343            [ + ]:         744 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3344                 :             : 
    3345                 :         744 :         mptcp_set_state(sk, TCP_CLOSE);
    3346                 :         744 :         mptcp_backlog_purge(sk);
    3347                 :         744 :         msk->fastclosing = 1;
    3348                 :             : 
    3349                 :             :         /* Explicitly send the fastclose reset, a plain one after a fallback */
    3350       [ + ][ + ]:        1575 :         mptcp_for_each_subflow_safe(msk, subflow, tmp) {
    3351                 :         831 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3352                 :             : 
    3353                 :         831 :                 lock_sock(ssk);
    3354                 :             : 
    3355                 :             :                 /* Some subflow socket states don't allow/need a reset.*/
    3356    [ +  + ][ + ]:         831 :                 if ((1 << ssk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
    3357                 :         324 :                         goto unlock;
    3358                 :             : 
    3359                 :         507 :                 subflow->send_fastclose = 1;
    3360                 :             : 
    3361                 :             :                 /* no reset in SYN_SENT, but a close at once, as TCP does */
    3362       [ + ][ + ]:         507 :                 if (ssk->sk_state == TCP_SYN_SENT) {
    3363                 :           6 :                         tcp_set_state(ssk, TCP_CLOSE);
    3364                 :           6 :                         goto unlock;
    3365                 :             :                 }
    3366                 :             : 
    3367                 :             :                 /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
    3368                 :             :                  * issue in __tcp_select_window(), see tcp_disconnect().
    3369                 :             :                  */
    3370                 :         501 :                 inet_csk(ssk)->icsk_ack.rcv_mss = TCP_MIN_MSS;
    3371                 :             : 
    3372                 :         501 :                 tcp_send_active_reset(ssk, SK_RST_REASON_TCP_ABORT_ON_CLOSE);
    3373                 :         831 : unlock:
    3374                 :         831 :                 release_sock(ssk);
    3375                 :             :         }
    3376                 :         744 : }
    3377                 :             : 
    3378                 :       14011 : static void mptcp_worker(struct work_struct *work)
    3379                 :             : {
    3380                 :       14011 :         struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
    3381                 :       14011 :         struct sock *sk = (struct sock *)msk;
    3382                 :       14011 :         unsigned long fail_tout;
    3383                 :       14011 :         int state;
    3384                 :             : 
    3385                 :       14011 :         lock_sock(sk);
    3386                 :       14011 :         state = sk->sk_state;
    3387    [ +  + ][ + ]:       14011 :         if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
    3388                 :        1271 :                 goto unlock;
    3389                 :             : 
    3390                 :       12740 :         mptcp_check_fastclose(msk);
    3391                 :             : 
    3392                 :       12740 :         mptcp_pm_worker(msk);
    3393                 :             : 
    3394                 :       12740 :         mptcp_check_send_data_fin(sk);
    3395                 :       12740 :         mptcp_check_data_fin_ack(sk);
    3396                 :       12740 :         mptcp_check_data_fin(sk);
    3397                 :             : 
    3398       [ + ][ + ]:       19595 :         if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
    3399                 :        1663 :                 __mptcp_close_subflow(sk);
    3400                 :             : 
    3401 [ +  + ][ +  + ]:       12740 :         if (mptcp_close_tout_expired(sk) && mptcp_orphan_keep(sk)) {
    3402                 :           0 :                 mptcp_start_tout_timer(sk);
    3403                 :           0 :                 __mptcp_push_pending(sk, 0);
    3404                 :             :         }
    3405                 :             : 
    3406                 :             :         /* TCP will close the subflow, as with its own orphans */
    3407 [ +  + ][ +  + ]:       12740 :         if (mptcp_close_tout_expired(sk) && mptcp_orphan_handover(sk))
    3408                 :          21 :                 mptcp_set_state(sk, TCP_CLOSE);
    3409                 :             : 
    3410       [ + ][ + ]:       12740 :         if (mptcp_close_tout_expired(sk)) {
    3411                 :         292 :                 struct mptcp_subflow_context *subflow, *tmp;
    3412                 :             : 
    3413                 :         292 :                 mptcp_do_fastclose(sk);
    3414       [ + ][ + ]:         606 :                 mptcp_for_each_subflow_safe(msk, subflow, tmp)
    3415                 :         314 :                         __mptcp_close_ssk(sk, subflow->tcp_sock, subflow, 0);
    3416                 :         292 :                 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | SEND_SHUTDOWN);
    3417                 :         292 :                 mptcp_close_wake_up(sk);
    3418                 :             :         }
    3419                 :             : 
    3420 [ +  + ][ +  + ]:       12740 :         if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
    3421                 :        1447 :                 __mptcp_destroy_sock(sk);
    3422                 :        1447 :                 goto unlock;
    3423                 :             :         }
    3424                 :             : 
    3425       [ + ][ + ]:       17387 :         if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
    3426                 :        4973 :                 __mptcp_retrans(sk);
    3427                 :             : 
    3428 [ -  - ][ -  - ]:       11293 :         fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
    3429 [ -  - ][ -  - ]:       11293 :         if (fail_tout && time_after(jiffies, fail_tout))
    3430                 :           0 :                 mptcp_mp_fail_no_response(msk);
    3431                 :             : 
    3432                 :           0 : unlock:
    3433                 :       14011 :         release_sock(sk);
    3434                 :       14011 :         sock_put(sk);
    3435                 :       14011 : }
    3436                 :             : 
    3437                 :        5310 : static void __mptcp_init_sock(struct sock *sk)
    3438                 :             : {
    3439                 :        5310 :         struct inet_connection_sock *icsk = inet_csk(sk);
    3440            [ + ]:        5310 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3441            [ + ]:        5310 :         struct net *net = sock_net(sk);
    3442                 :             : 
    3443            [ + ]:        5310 :         INIT_LIST_HEAD(&msk->conn_list);
    3444                 :        5310 :         INIT_LIST_HEAD(&msk->join_list);
    3445                 :        5310 :         INIT_LIST_HEAD(&msk->rtx_queue);
    3446                 :        5310 :         INIT_LIST_HEAD(&msk->backlog_list);
    3447            [ + ]:        5310 :         INIT_WORK(&msk->work, mptcp_worker);
    3448                 :        5310 :         msk->out_of_order_queue = RB_ROOT;
    3449                 :        5310 :         msk->first_pending = NULL;
    3450                 :             : 
    3451                 :             :         /* msk does not go through tcp_init_sock(); seed RTO bounds. */
    3452                 :       10620 :         icsk->icsk_rto_min =
    3453       [ + ][ + ]:        5310 :                 usecs_to_jiffies(READ_ONCE(net->ipv4.sysctl_tcp_rto_min_us));
    3454                 :       10620 :         icsk->icsk_rto_max =
    3455       [ + ][ + ]:        5310 :                 msecs_to_jiffies(READ_ONCE(net->ipv4.sysctl_tcp_rto_max_ms));
    3456                 :        5310 :         msk->timer_ival = icsk->icsk_rto_min;
    3457                 :        5310 :         msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
    3458                 :        5310 :         msk->backlog_len = 0;
    3459                 :        5310 :         mptcp_init_rtt_est(msk);
    3460                 :             : 
    3461                 :        5310 :         WRITE_ONCE(msk->first, NULL);
    3462                 :        5310 :         inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
    3463                 :        5310 :         WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
    3464                 :        5310 :         msk->allow_infinite_fallback = true;
    3465                 :        5310 :         msk->allow_subflows = true;
    3466                 :        5310 :         msk->recovery = false;
    3467                 :        5310 :         msk->subflow_id = 1;
    3468                 :        5310 :         msk->last_data_sent = tcp_jiffies32;
    3469                 :        5310 :         msk->last_data_recv = tcp_jiffies32;
    3470                 :        5310 :         msk->last_ack_recv = tcp_jiffies32;
    3471                 :             : 
    3472                 :        5310 :         mptcp_pm_data_init(msk);
    3473                 :        5310 :         spin_lock_init(&msk->fallback_lock);
    3474                 :             : 
    3475                 :             :         /* re-use the csk retrans timer for MPTCP-level retrans */
    3476                 :        5310 :         timer_setup(&sk->mptcp_retransmit_timer, mptcp_retransmit_timer, 0);
    3477                 :        5310 :         timer_setup(&msk->sk.mptcp_tout_timer, mptcp_tout_timer, 0);
    3478                 :        5310 : }
    3479                 :             : 
    3480                 :        3716 : static void mptcp_ca_reset(struct sock *sk)
    3481                 :             : {
    3482                 :        3716 :         struct inet_connection_sock *icsk = inet_csk(sk);
    3483                 :             : 
    3484                 :        3716 :         tcp_assign_congestion_control(sk);
    3485            [ + ]:        3716 :         strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
    3486                 :             :                 sizeof(mptcp_sk(sk)->ca_name));
    3487                 :             : 
    3488                 :             :         /* no need to keep a reference to the ops, the name will suffice */
    3489                 :        3716 :         tcp_cleanup_congestion_control(sk);
    3490                 :        3716 :         icsk->icsk_ca_ops = NULL;
    3491                 :        3716 : }
    3492                 :             : 
    3493                 :        3614 : static int mptcp_init_sock(struct sock *sk)
    3494                 :             : {
    3495                 :        3614 :         struct net *net = sock_net(sk);
    3496                 :        3614 :         int ret;
    3497                 :             : 
    3498                 :        3614 :         __mptcp_init_sock(sk);
    3499                 :             : 
    3500       [ + ][ + ]:        3614 :         if (!mptcp_is_enabled(net))
    3501                 :             :                 return -ENOPROTOOPT;
    3502                 :             : 
    3503 [ +  - ][ +  - ]:        3604 :         if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
    3504                 :             :                 return -ENOMEM;
    3505                 :             : 
    3506                 :        3604 :         rcu_read_lock();
    3507       [ + ][ + ]:        3604 :         ret = mptcp_init_sched(mptcp_sk(sk),
    3508                 :             :                                mptcp_sched_find(mptcp_get_scheduler(net)));
    3509                 :        3604 :         rcu_read_unlock();
    3510       [ - ][ - ]:        3604 :         if (ret)
    3511                 :             :                 return ret;
    3512                 :             : 
    3513                 :        3604 :         set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
    3514                 :             : 
    3515                 :             :         /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
    3516                 :             :          * propagate the correct value
    3517                 :             :          */
    3518                 :        3604 :         mptcp_ca_reset(sk);
    3519                 :             : 
    3520                 :        3604 :         sk_sockets_allocated_inc(sk);
    3521                 :        3604 :         sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
    3522                 :        3604 :         sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
    3523                 :        3604 :         sk->sk_write_space = sk_stream_write_space;
    3524                 :             : 
    3525                 :        3604 :         return 0;
    3526                 :             : }
    3527                 :             : 
    3528                 :        5361 : static void __mptcp_clear_xmit(struct sock *sk)
    3529                 :             : {
    3530            [ + ]:        5361 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3531                 :        5361 :         struct mptcp_data_frag *dtmp, *dfrag;
    3532                 :             : 
    3533                 :        5361 :         msk->first_pending = NULL;
    3534            [ + ]:        5588 :         list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
    3535                 :         227 :                 dfrag_clear(sk, dfrag);
    3536                 :        5361 : }
    3537                 :             : 
    3538                 :        3792 : void mptcp_cancel_work(struct sock *sk)
    3539                 :             : {
    3540            [ + ]:        3792 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3541                 :             : 
    3542            [ + ]:        3792 :         if (cancel_work_sync(&msk->work))
    3543                 :          56 :                 __sock_put(sk);
    3544                 :        3792 : }
    3545                 :             : 
    3546                 :        4248 : void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
    3547                 :             : {
    3548                 :        4248 :         lock_sock(ssk);
    3549                 :             : 
    3550      [ -  +  + ]:        4248 :         switch (ssk->sk_state) {
    3551                 :           0 :         case TCP_LISTEN:
    3552            [ # ]:           0 :                 if (!(how & RCV_SHUTDOWN))
    3553                 :             :                         break;
    3554                 :          18 :                 fallthrough;
    3555                 :             :         case TCP_SYN_SENT:
    3556            [ + ]:          18 :                 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
    3557                 :             :                 break;
    3558                 :        4230 :         default:
    3559         [ +  + ]:        4230 :                 if (__mptcp_check_fallback(mptcp_sk(sk))) {
    3560         [ -  - ]:         184 :                         pr_debug("Fallback\n");
    3561                 :         184 :                         ssk->sk_shutdown |= how;
    3562                 :         184 :                         tcp_shutdown(ssk, how);
    3563                 :             : 
    3564                 :             :                         /* simulate the data_fin ack reception to let the state
    3565                 :             :                          * machine move forward
    3566                 :             :                          */
    3567         [ +  + ]:         184 :                         WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
    3568                 :         184 :                         mptcp_schedule_work(sk);
    3569                 :             :                 } else {
    3570         [ -  - ]:        4046 :                         pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
    3571                 :        4046 :                         tcp_send_ack(ssk);
    3572            [ + ]:        4046 :                         if (!mptcp_rtx_timer_pending(sk))
    3573                 :        1950 :                                 mptcp_reset_rtx_timer(sk);
    3574                 :             :                 }
    3575                 :             :                 break;
    3576                 :             :         }
    3577                 :             : 
    3578                 :        4248 :         release_sock(ssk);
    3579                 :        4248 : }
    3580                 :             : 
    3581                 :       24641 : void mptcp_set_state(struct sock *sk, int state)
    3582                 :             : {
    3583                 :       25688 :         int oldstate = sk->sk_state;
    3584                 :             : 
    3585   [ +  +  +  - ]:       24592 :         switch (state) {
           [ +  +  +  - ]
    3586                 :        3396 :         case TCP_ESTABLISHED:
    3587       [ - ][ - ]:        3396 :                 if (oldstate != TCP_ESTABLISHED)
    3588       [ - ][ - ]:        3396 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
    3589                 :             :                 break;
    3590                 :             :         case TCP_CLOSE_WAIT:
    3591                 :             :                 /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
    3592                 :             :                  * MPTCP "accepted" sockets will be created later on. So no
    3593                 :             :                  * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
    3594                 :             :                  */
    3595                 :             :                 break;
    3596                 :       10656 :         case TCP_CLOSE:
    3597       [ + ][ + ]:       10656 :                 clear_bit(MPTCP_RTX_ENABLED, &mptcp_sk(sk)->flags);
    3598                 :       21196 :                 fallthrough;
    3599                 :       21196 :         default:
    3600       [ + ][ + ]:       21196 :                 if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
    3601       [ - ][ - ]:        3396 :                         MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
    3602                 :             :         }
    3603                 :             : 
    3604                 :       25688 :         inet_sk_state_store(sk, state);
    3605                 :        1096 : }
    3606                 :             : 
    3607                 :             : static const unsigned char new_state[16] = {
    3608                 :             :         /* current state:     new state:      action:   */
    3609                 :             :         [0 /* (Invalid) */] = TCP_CLOSE,
    3610                 :             :         [TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
    3611                 :             :         [TCP_SYN_SENT]      = TCP_CLOSE,
    3612                 :             :         [TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
    3613                 :             :         [TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
    3614                 :             :         [TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
    3615                 :             :         [TCP_TIME_WAIT]     = TCP_CLOSE,        /* should not happen ! */
    3616                 :             :         [TCP_CLOSE]         = TCP_CLOSE,
    3617                 :             :         [TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
    3618                 :             :         [TCP_LAST_ACK]      = TCP_LAST_ACK,
    3619                 :             :         [TCP_LISTEN]        = TCP_CLOSE,
    3620                 :             :         [TCP_CLOSING]       = TCP_CLOSING,
    3621                 :             :         [TCP_NEW_SYN_RECV]  = TCP_CLOSE,        /* should not happen ! */
    3622                 :             : };
    3623                 :             : 
    3624                 :        3586 : static int mptcp_close_state(struct sock *sk)
    3625                 :             : {
    3626                 :        3586 :         int next = (int)new_state[sk->sk_state];
    3627                 :        3586 :         int ns = next & TCP_STATE_MASK;
    3628                 :             : 
    3629                 :        3586 :         mptcp_set_state(sk, ns);
    3630                 :             : 
    3631                 :        3586 :         return next & TCP_ACTION_FIN;
    3632                 :             : }
    3633                 :             : 
    3634                 :      464877 : static void mptcp_check_send_data_fin(struct sock *sk)
    3635                 :             : {
    3636                 :      464877 :         struct mptcp_subflow_context *subflow;
    3637            [ + ]:      464877 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3638                 :             : 
    3639      [ -  -  - ]:      464877 :         pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
                 [ -  - ]
    3640                 :             :                  msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
    3641                 :             :                  msk->snd_nxt, msk->write_seq);
    3642                 :             : 
    3643                 :             :         /* we still need to enqueue subflows or not really shutting down,
    3644                 :             :          * skip this
    3645                 :             :          */
    3646   [ +  +  +  + ]:      467972 :         if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
              [ +  +  + ]
    3647                 :        3095 :             mptcp_send_head(sk))
    3648                 :      461782 :                 return;
    3649                 :             : 
    3650                 :        3095 :         WRITE_ONCE(msk->snd_nxt, msk->write_seq);
    3651                 :             : 
    3652                 :             :         /* an orphan's close timeout is for the DATA_FIN exchange */
    3653       [ + ][ + ]:        3095 :         if (sock_flag(sk, SOCK_DEAD))
    3654                 :         167 :                 mptcp_start_tout_timer(sk);
    3655                 :             : 
    3656       [ + ][ + ]:        7145 :         mptcp_for_each_subflow(msk, subflow) {
    3657                 :        4050 :                 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
    3658                 :             : 
    3659                 :        4050 :                 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
    3660                 :             :         }
    3661                 :             : }
    3662                 :             : 
    3663                 :        3101 : static void __mptcp_wr_shutdown(struct sock *sk)
    3664                 :             : {
    3665            [ + ]:        3101 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3666                 :             : 
    3667      [ -  -  - ]:        3101 :         pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
                 [ -  - ]
    3668                 :             :                  msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
    3669                 :             :                  !!mptcp_send_head(sk));
    3670                 :             : 
    3671                 :             :         /* will be ignored by fallback sockets */
    3672                 :        3101 :         WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
    3673                 :        3101 :         WRITE_ONCE(msk->snd_data_fin_enable, 1);
    3674                 :             : 
    3675                 :        3101 :         mptcp_check_send_data_fin(sk);
    3676                 :        3101 : }
    3677                 :             : 
    3678                 :        5239 : static void __mptcp_destroy_sock(struct sock *sk)
    3679                 :             : {
    3680            [ + ]:        5239 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3681                 :             : 
    3682         [ -  - ]:        5239 :         pr_debug("msk=%p\n", msk);
    3683                 :             : 
    3684                 :        5239 :         might_sleep();
    3685                 :             : 
    3686                 :        5239 :         mptcp_stop_rtx_timer(sk);
    3687                 :        5239 :         sk_stop_timer(sk, &inet_csk(sk)->mptcp_tout_timer);
    3688                 :        5239 :         msk->pm.status = 0;
    3689                 :        5239 :         mptcp_release_sched(msk);
    3690                 :             : 
    3691                 :        5239 :         sk->sk_prot->destroy(sk);
    3692                 :             : 
    3693                 :        5239 :         sk_stream_kill_queues(sk);
    3694                 :        5239 :         xfrm_sk_free_policy(sk);
    3695                 :             : 
    3696       [ + ][ + ]:        7874 :         if (test_and_clear_bit(MPTCP_ORPHAN_COUNTED, &msk->flags))
    3697                 :        1447 :                 tcp_orphan_count_dec();
    3698                 :             : 
    3699                 :        5239 :         sock_put(sk);
    3700                 :        5239 : }
    3701                 :             : 
    3702                 :          42 : void __mptcp_unaccepted_force_close(struct sock *sk)
    3703                 :             : {
    3704                 :          42 :         sock_set_flag(sk, SOCK_DEAD);
    3705                 :          42 :         mptcp_do_fastclose(sk);
    3706                 :          42 :         __mptcp_destroy_sock(sk);
    3707                 :          42 : }
    3708                 :             : 
    3709                 :           0 : static __poll_t mptcp_check_readable(struct sock *sk)
    3710                 :             : {
    3711            [ + ]:     1049579 :         return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
    3712                 :             : }
    3713                 :             : 
    3714                 :        3484 : static void mptcp_check_listen_stop(struct sock *sk)
    3715                 :             : {
    3716                 :        3484 :         struct sock *ssk;
    3717                 :             : 
    3718       [ + ][ + ]:        3484 :         if (inet_sk_state_load(sk) != TCP_LISTEN)
    3719                 :             :                 return;
    3720                 :             : 
    3721                 :        1891 :         sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
    3722       [ + ][ + ]:        1891 :         ssk = mptcp_sk(sk)->first;
    3723 [ -  + ][ -  + ]:        3782 :         if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
    3724                 :           0 :                 return;
    3725                 :             : 
    3726                 :        1891 :         lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
    3727                 :        1891 :         tcp_set_state(ssk, TCP_CLOSE);
    3728                 :        1891 :         mptcp_subflow_queue_clean(sk, ssk);
    3729                 :        1891 :         inet_csk_listen_stop(ssk);
    3730                 :        1891 :         mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
    3731                 :        1891 :         release_sock(ssk);
    3732                 :             : }
    3733                 :             : 
    3734                 :        5258 : bool __mptcp_close(struct sock *sk, long timeout)
    3735                 :             : {
    3736                 :        5258 :         struct mptcp_subflow_context *subflow;
    3737            [ + ]:        5258 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3738                 :        5258 :         bool do_cancel_work = false;
    3739                 :        5258 :         int subflows_alive = 0;
    3740                 :             : 
    3741                 :        5258 :         WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
    3742                 :             : 
    3743    [ +  + ][ + ]:        5258 :         if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
    3744                 :        3372 :                 mptcp_check_listen_stop(sk);
    3745                 :        3372 :                 mptcp_set_state(sk, TCP_CLOSE);
    3746                 :        3372 :                 goto cleanup;
    3747                 :             :         }
    3748                 :             : 
    3749      [ +  -  + ]:        3486 :         if (mptcp_data_avail(msk) || timeout < 0 ||
              [ +  -  + ]
    3750       [ - ][ - ]:        1612 :             (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
    3751                 :             :                 /* If the msk has read data, or the caller explicitly ask it,
    3752                 :             :                  * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
    3753                 :             :                  */
    3754                 :         298 :                 mptcp_do_fastclose(sk);
    3755                 :         298 :                 timeout = 0;
    3756       [ + ][ + ]:        1588 :         } else if (mptcp_close_state(sk)) {
    3757                 :        1248 :                 __mptcp_wr_shutdown(sk);
    3758                 :             :         }
    3759                 :             : 
    3760                 :        1886 :         sk_stream_wait_close(sk, timeout);
    3761                 :             : 
    3762                 :        5258 : cleanup:
    3763                 :             :         /* orphan all the subflows */
    3764       [ + ][ + ]:       11076 :         mptcp_for_each_subflow(msk, subflow) {
    3765                 :        5818 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    3766                 :        5818 :                 bool slow = lock_sock_fast_nested(ssk);
    3767                 :             : 
    3768                 :        5818 :                 subflows_alive += ssk->sk_state != TCP_CLOSE;
    3769                 :             : 
    3770                 :             :                 /* since the close timeout takes precedence on the fail one,
    3771                 :             :                  * cancel the latter
    3772                 :             :                  */
    3773       [ + ][ + ]:        5818 :                 if (ssk == msk->first)
    3774                 :        5240 :                         subflow->fail_tout = 0;
    3775                 :             : 
    3776                 :             :                 /* detach from the parent socket, but allow data_ready to
    3777                 :             :                  * push incoming data into the mptcp stack, to properly ack it
    3778                 :             :                  */
    3779                 :        5818 :                 ssk->sk_socket = NULL;
    3780                 :        5818 :                 ssk->sk_wq = NULL;
    3781                 :        5818 :                 unlock_sock_fast(ssk, slow);
    3782                 :             :         }
    3783                 :        5258 :         sock_orphan(sk);
    3784                 :             : 
    3785                 :             :         /* all the subflows are closed, only timeout can change the msk
    3786                 :             :          * state, let's not keep resources busy for no reasons
    3787                 :             :          */
    3788       [ + ][ + ]:        5258 :         if (subflows_alive == 0) {
    3789                 :        2964 :                 mptcp_set_state(sk, TCP_CLOSE);
    3790       [ + ][ + ]:        2294 :         } else if (sk->sk_state != TCP_CLOSE) {
    3791                 :             :                 /* as TCP orphans: counted, and limited */
    3792                 :        1508 :                 tcp_orphan_count_inc();
    3793                 :        1508 :                 set_bit(MPTCP_ORPHAN_COUNTED, &msk->flags);
    3794       [ + ][ + ]:        1508 :                 if (mptcp_orphan_oom(sk))
    3795                 :           0 :                         mptcp_do_fastclose(sk);
    3796                 :             :         }
    3797                 :             : 
    3798                 :        5258 :         sock_hold(sk);
    3799 [ -  - ][ -  - ]:        5258 :         pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
    3800                 :        5258 :         mptcp_pm_connection_closed(msk);
    3801                 :             : 
    3802       [ + ][ + ]:        5258 :         if (sk->sk_state == TCP_CLOSE) {
    3803                 :        3750 :                 __mptcp_destroy_sock(sk);
    3804                 :        3750 :                 do_cancel_work = true;
    3805                 :             :         } else {
    3806                 :        1508 :                 mptcp_start_tout_timer(sk);
    3807                 :             :         }
    3808                 :             : 
    3809                 :        5258 :         return do_cancel_work;
    3810                 :             : }
    3811                 :             : 
    3812                 :        5258 : static void mptcp_close(struct sock *sk, long timeout)
    3813                 :             : {
    3814                 :        5258 :         bool do_cancel_work;
    3815                 :             : 
    3816                 :        5258 :         lock_sock(sk);
    3817                 :             : 
    3818                 :        5258 :         do_cancel_work = __mptcp_close(sk, timeout);
    3819                 :        5258 :         release_sock(sk);
    3820            [ + ]:        5258 :         if (do_cancel_work)
    3821                 :        3750 :                 mptcp_cancel_work(sk);
    3822                 :             : 
    3823                 :        5258 :         sock_put(sk);
    3824                 :        5258 : }
    3825                 :             : 
    3826                 :        7140 : static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
    3827                 :             : {
    3828                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3829            [ - ]:        7140 :         const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
    3830            [ - ]:        7140 :         struct ipv6_pinfo *msk6 = inet6_sk(msk);
    3831                 :             : 
    3832                 :        7140 :         msk->sk_v6_daddr = ssk->sk_v6_daddr;
    3833                 :        7140 :         msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
    3834                 :             : 
    3835       [ + ][ + ]:        7140 :         if (msk6 && ssk6) {
    3836                 :        3156 :                 msk6->saddr = ssk6->saddr;
    3837                 :        3156 :                 msk6->flow_label = ssk6->flow_label;
    3838                 :             :         }
    3839                 :             : #endif
    3840                 :             : 
    3841                 :        7140 :         inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
    3842                 :        7140 :         inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
    3843                 :        7140 :         inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
    3844                 :        7140 :         inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
    3845                 :        7140 :         inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
    3846                 :        7140 :         inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
    3847                 :        7140 : }
    3848                 :             : 
    3849                 :        5361 : static void mptcp_destroy_common(struct mptcp_sock *msk)
    3850                 :             : {
    3851                 :        5361 :         struct mptcp_subflow_context *subflow, *tmp;
    3852                 :        5361 :         struct sock *sk = (struct sock *)msk;
    3853                 :             : 
    3854                 :        5361 :         __mptcp_clear_xmit(sk);
    3855                 :        5361 :         mptcp_backlog_purge(sk);
    3856                 :             : 
    3857                 :             :         /* join list will be eventually flushed (with rst) at sock lock release time */
    3858            [ + ]:       11125 :         mptcp_for_each_subflow_safe(msk, subflow, tmp)
    3859                 :        5764 :                 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, 0);
    3860                 :             : 
    3861                 :        5361 :         __skb_queue_purge(&sk->sk_receive_queue);
    3862                 :        5361 :         skb_rbtree_purge(&msk->out_of_order_queue);
    3863                 :             : 
    3864                 :             :         /* move all the rx fwd alloc into the sk_mem_reclaim_final in
    3865                 :             :          * inet_sock_destruct() will dispose it
    3866                 :             :          */
    3867                 :        5361 :         mptcp_token_destroy(msk);
    3868                 :        5361 :         mptcp_pm_destroy(msk);
    3869                 :        5361 : }
    3870                 :             : 
    3871                 :         112 : static int mptcp_disconnect(struct sock *sk, int flags)
    3872                 :             : {
    3873                 :         112 :         struct inet_connection_sock *icsk = inet_csk(sk);
    3874            [ + ]:         112 :         struct mptcp_sock *msk = mptcp_sk(sk);
    3875                 :             : 
    3876                 :             :         /* We are on the fastopen error path. We can't call straight into the
    3877                 :             :          * subflows cleanup code due to lock nesting (we are already under
    3878                 :             :          * msk->firstsocket lock).
    3879                 :             :          */
    3880       [ - ][ - ]:         112 :         if (msk->fastopening)
    3881                 :             :                 return -EBUSY;
    3882                 :             : 
    3883                 :         112 :         mptcp_check_listen_stop(sk);
    3884                 :         112 :         mptcp_set_state(sk, TCP_CLOSE);
    3885                 :         112 :         inet_clear_bit(DEFER_CONNECT, sk);
    3886                 :             : 
    3887                 :             :         /* The later subflow close can not kick again the tout timer,
    3888                 :             :          * as the msk is already in closed status.
    3889                 :             :          */
    3890                 :         112 :         msk->timer_ival = icsk->icsk_rto_min;
    3891                 :         112 :         sk_stop_timer_sync(sk, &sk->mptcp_retransmit_timer);
    3892                 :         112 :         icsk->icsk_mtup.probe_timestamp = 0;
    3893                 :         112 :         sk_stop_timer_sync(sk, &icsk->mptcp_tout_timer);
    3894                 :             : 
    3895                 :         112 :         mptcp_pm_connection_closed(msk);
    3896                 :             : 
    3897                 :             :         /* msk->subflow is still intact, the following will not free the first
    3898                 :             :          * subflow
    3899                 :             :          */
    3900                 :         112 :         mptcp_do_fastclose(sk);
    3901                 :         112 :         mptcp_destroy_common(msk);
    3902                 :             : 
    3903                 :             :         /* The first subflow is already in TCP_CLOSE status, the following
    3904                 :             :          * can't overlap with a fallback anymore
    3905                 :             :          */
    3906                 :         112 :         spin_lock_bh(&msk->fallback_lock);
    3907                 :         112 :         msk->allow_subflows = true;
    3908                 :         112 :         msk->allow_infinite_fallback = true;
    3909                 :         112 :         WRITE_ONCE(msk->flags, 0);
    3910                 :         112 :         spin_unlock_bh(&msk->fallback_lock);
    3911                 :             : 
    3912                 :         112 :         msk->cb_flags = 0;
    3913                 :         112 :         msk->recovery = false;
    3914                 :         112 :         WRITE_ONCE(msk->can_ack, false);
    3915                 :         112 :         WRITE_ONCE(msk->fully_established, false);
    3916                 :         112 :         WRITE_ONCE(msk->rcv_data_fin, false);
    3917                 :         112 :         WRITE_ONCE(msk->snd_data_fin_enable, false);
    3918                 :         112 :         WRITE_ONCE(msk->rcv_fastclose, false);
    3919                 :         112 :         WRITE_ONCE(msk->use_64bit_ack, false);
    3920                 :         112 :         WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
    3921                 :         112 :         mptcp_pm_data_reset(msk);
    3922                 :         112 :         mptcp_ca_reset(sk);
    3923                 :         112 :         msk->bytes_consumed = 0;
    3924                 :         112 :         msk->bytes_acked = 0;
    3925                 :         112 :         msk->bytes_received = 0;
    3926                 :         112 :         msk->bytes_sent = 0;
    3927                 :         112 :         msk->bytes_retrans = 0;
    3928                 :         112 :         msk->rcvspace_init = 0;
    3929                 :         112 :         msk->fastclosing = 0;
    3930                 :         112 :         mptcp_init_rtt_est(msk);
    3931                 :             : 
    3932                 :             :         /* for fallback's sake */
    3933                 :         112 :         WRITE_ONCE(msk->ack_seq, 0);
    3934                 :         112 :         atomic64_set(&msk->rcv_wnd_sent, 0);
    3935                 :             : 
    3936                 :         112 :         WRITE_ONCE(sk->sk_shutdown, 0);
    3937                 :         112 :         sock_reset_flag(sk, SOCK_DONE);
    3938                 :         112 :         sk_error_report(sk);
    3939                 :         112 :         return 0;
    3940                 :             : }
    3941                 :             : 
    3942                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    3943                 :         802 : static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
    3944                 :             : {
    3945            [ + ]:         802 :         struct mptcp6_sock *msk6 = container_of(mptcp_sk(sk), struct mptcp6_sock, msk);
    3946                 :             : 
    3947                 :         802 :         return &msk6->np;
    3948                 :             : }
    3949                 :             : 
    3950                 :         802 : static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
    3951                 :             : {
    3952            [ - ]:         802 :         const struct ipv6_pinfo *np = inet6_sk(sk);
    3953                 :         802 :         struct ipv6_txoptions *opt;
    3954                 :         802 :         struct ipv6_pinfo *newnp;
    3955                 :             : 
    3956            [ - ]:         802 :         newnp = inet6_sk(newsk);
    3957                 :             : 
    3958                 :             :         /* The IPv4 options belong to the listener, which frees them */
    3959                 :         802 :         RCU_INIT_POINTER(inet_sk(newsk)->inet_opt, NULL);
    3960                 :             : 
    3961                 :         802 :         rcu_read_lock();
    3962   [ -  +  -  -  :         802 :         opt = rcu_dereference(np->opt);
                      - ]
    3963       [ - ][ - ]:         802 :         if (opt) {
    3964                 :           0 :                 opt = ipv6_dup_options(newsk, opt);
    3965       [ # ][ # ]:           0 :                 if (!opt)
    3966       [ # ][ # ]:           0 :                         net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
    3967                 :             :         }
    3968                 :         802 :         RCU_INIT_POINTER(newnp->opt, opt);
    3969                 :         802 :         rcu_read_unlock();
    3970                 :         802 : }
    3971                 :             : #endif
    3972                 :             : 
    3973                 :         894 : static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
    3974                 :             : {
    3975                 :         894 :         struct ip_options_rcu *inet_opt, *newopt = NULL;
    3976                 :         894 :         const struct inet_sock *inet = inet_sk(sk);
    3977                 :         894 :         struct inet_sock *newinet;
    3978                 :             : 
    3979                 :         894 :         newinet = inet_sk(newsk);
    3980                 :             : 
    3981                 :         894 :         rcu_read_lock();
    3982   [ -  +  -  -  :         894 :         inet_opt = rcu_dereference(inet->inet_opt);
                      - ]
    3983       [ - ][ - ]:         894 :         if (inet_opt) {
    3984                 :           0 :                 newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
    3985                 :           0 :                                       inet_opt->opt.optlen, GFP_ATOMIC);
    3986       [ # ][ # ]:           0 :                 if (!newopt)
    3987       [ # ][ # ]:           0 :                         net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
    3988                 :             :         }
    3989                 :         894 :         RCU_INIT_POINTER(newinet->inet_opt, newopt);
    3990                 :         894 :         rcu_read_unlock();
    3991                 :         894 : }
    3992                 :             : 
    3993                 :           0 : static void mptcp_sk_clone_destroy(struct sock *nsk)
    3994                 :             : {
    3995            [ # ]:           0 :         struct mptcp_sock *msk = mptcp_sk(nsk);
    3996                 :             : 
    3997                 :           0 :         mptcp_release_sched(msk);
    3998                 :           0 :         mptcp_set_state(nsk, TCP_CLOSE);
    3999                 :             :         /* inet_csk_prepare_forced_close() clears TCP sock_ops state via
    4000                 :             :          * tcp_sk(), but nsk is an MPTCP master socket; keep the inet-level
    4001                 :             :          * destroy preparation here.
    4002                 :             :          */
    4003                 :           0 :         bh_unlock_sock(nsk);
    4004                 :           0 :         sock_put(nsk);
    4005                 :           0 :         sock_set_flag(nsk, SOCK_DEAD);
    4006                 :           0 :         tcp_orphan_count_inc();
    4007                 :           0 :         inet_sk(nsk)->inet_num = 0;
    4008                 :           0 :         inet_csk_destroy_sock(nsk);
    4009                 :           0 : }
    4010                 :             : 
    4011                 :        1696 : struct sock *mptcp_sk_clone_init(const struct sock *sk,
    4012                 :             :                                  const struct mptcp_options_received *mp_opt,
    4013                 :             :                                  struct sock *ssk,
    4014                 :             :                                  struct request_sock *req)
    4015                 :             : {
    4016                 :        1696 :         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
    4017                 :        1696 :         struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
    4018                 :        1696 :         struct mptcp_subflow_context *subflow;
    4019                 :        1696 :         struct mptcp_sock *msk;
    4020                 :             : 
    4021            [ + ]:        1696 :         if (!nsk)
    4022                 :             :                 return NULL;
    4023                 :             : 
    4024                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    4025            [ + ]:        1696 :         if (nsk->sk_family == AF_INET6)
    4026                 :         802 :                 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
    4027                 :             : #endif
    4028                 :             : 
    4029                 :        1696 :         __mptcp_init_sock(nsk);
    4030                 :             : 
    4031                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    4032            [ + ]:        1696 :         if (nsk->sk_family == AF_INET6)
    4033                 :         802 :                 mptcp_copy_ip6_options(nsk, sk);
    4034                 :             :         else
    4035                 :             : #endif
    4036                 :         894 :                 mptcp_copy_ip_options(nsk, sk);
    4037                 :             : 
    4038            [ + ]:        1696 :         msk = mptcp_sk(nsk);
    4039                 :        1696 :         WRITE_ONCE(msk->local_key, subflow_req->local_key);
    4040                 :        1696 :         WRITE_ONCE(msk->token, subflow_req->token);
    4041                 :        1696 :         msk->in_accept_queue = 1;
    4042                 :        1696 :         msk->fastopening = 0;
    4043                 :        1696 :         WRITE_ONCE(msk->fully_established, false);
    4044            [ + ]:        1696 :         if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
    4045                 :         118 :                 WRITE_ONCE(msk->csum_enabled, true);
    4046                 :             : 
    4047                 :        1696 :         WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
    4048                 :        1696 :         WRITE_ONCE(msk->snd_nxt, msk->write_seq);
    4049                 :        1696 :         WRITE_ONCE(msk->snd_una, msk->write_seq);
    4050            [ + ]:        1696 :         WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
    4051            [ + ]:        1696 :         msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
    4052            [ + ]:        1696 :         mptcp_init_sched(msk, mptcp_sk(sk)->sched);
    4053                 :             : 
    4054                 :             :         /* passive msk is created after the first/MPC subflow */
    4055                 :        1696 :         msk->subflow_id = 2;
    4056                 :             : 
    4057                 :        1696 :         sock_reset_flag(nsk, SOCK_RCU_FREE);
    4058                 :        1696 :         security_inet_csk_clone(nsk, req);
    4059                 :             : 
    4060                 :             :         /* this can't race with mptcp_close(), as the msk is
    4061                 :             :          * not yet exposted to user-space
    4062                 :             :          */
    4063                 :        1696 :         mptcp_set_state(nsk, TCP_ESTABLISHED);
    4064                 :             : 
    4065                 :             :         /* The msk maintain a ref to each subflow in the connections list */
    4066                 :        1696 :         WRITE_ONCE(msk->first, ssk);
    4067       [ - ][ - ]:        1696 :         subflow = mptcp_subflow_ctx(ssk);
    4068       [ - ][ - ]:        1696 :         list_add(&subflow->node, &msk->conn_list);
    4069                 :        1696 :         sock_hold(ssk);
    4070                 :             : 
    4071                 :             :         /* set msk addresses early to ensure mptcp_pm_get_local_id()
    4072                 :             :          * uses the correct data
    4073                 :             :          */
    4074                 :        1696 :         mptcp_copy_inaddrs(nsk, ssk);
    4075                 :             : 
    4076                 :        1696 :         mptcp_rcv_space_init(msk, ssk);
    4077                 :        1696 :         msk->rcvq_space.time = mptcp_stamp();
    4078                 :             : 
    4079  [ + ][ + ][ + ]:        1696 :         if (!mptcp_token_accept(subflow_req, msk)) {
                    [ + ]
    4080                 :           0 :                 list_del_init(&subflow->node);
    4081                 :           0 :                 WRITE_ONCE(msk->first, NULL);
    4082                 :           0 :                 sock_put(ssk);
    4083                 :           0 :                 mptcp_sk_clone_destroy(nsk);
    4084                 :           0 :                 return NULL;
    4085                 :             :         }
    4086                 :             : 
    4087  [ + ][ + ][ - ]:        1696 :         if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
                    [ - ]
    4088                 :        1640 :                 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
    4089                 :        1696 :         bh_unlock_sock(nsk);
    4090                 :             : 
    4091                 :             :         /* note: the newly allocated socket refcount is 2 now */
    4092                 :        1696 :         return nsk;
    4093                 :             : }
    4094                 :             : 
    4095                 :        5249 : static void mptcp_destroy(struct sock *sk)
    4096                 :             : {
    4097            [ + ]:        5249 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4098                 :             : 
    4099                 :             :         /* allow the following to close even the initial subflow */
    4100                 :        5249 :         msk->free_first = 1;
    4101                 :        5249 :         mptcp_destroy_common(msk);
    4102                 :        5249 :         sk_sockets_allocated_dec(sk);
    4103                 :        5249 : }
    4104                 :             : 
    4105                 :      598799 : void __mptcp_data_acked(struct sock *sk)
    4106                 :             : {
    4107       [ + ][ + ]:      598799 :         if (!sock_owned_by_user(sk))
    4108                 :      294917 :                 __mptcp_clean_una(sk);
    4109                 :             :         else
    4110       [ + ][ + ]:      303882 :                 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
    4111                 :      598799 : }
    4112                 :             : 
    4113                 :     1964487 : void __mptcp_check_push(struct sock *sk, struct sock *ssk)
    4114                 :             : {
    4115       [ + ][ + ]:     1964487 :         if (!sock_owned_by_user(sk))
    4116                 :     1427645 :                 __mptcp_subflow_push_pending(sk, ssk, false);
    4117                 :             :         else
    4118       [ + ][ + ]:      536842 :                 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
    4119                 :     1964487 : }
    4120                 :             : 
    4121                 :             : #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
    4122                 :             :                                       BIT(MPTCP_RETRANSMIT) | \
    4123                 :             :                                       BIT(MPTCP_FLUSH_JOIN_LIST))
    4124                 :             : 
    4125                 :             : /* processes deferred events and flush wmem */
    4126                 :     2360974 : static void mptcp_release_cb(struct sock *sk)
    4127                 :             :         __must_hold(&sk->sk_lock.slock)
    4128                 :             : {
    4129            [ + ]:     2360974 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4130                 :     2360974 :         u32 moved = 0;
    4131                 :             : 
    4132                 :      920031 :         for (;;) {
    4133                 :     2666646 :                 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
    4134                 :     2666646 :                 struct list_head join_list, skbs;
    4135                 :     2666646 :                 bool spool_bl;
    4136                 :             : 
    4137                 :     2666646 :                 spool_bl = mptcp_can_spool_backlog(sk, &skbs);
    4138       [ + ][ + ]:     2666646 :                 if (!flags && !spool_bl)
    4139                 :             :                         break;
    4140                 :             : 
    4141            [ + ]:      305672 :                 INIT_LIST_HEAD(&join_list);
    4142            [ + ]:      305672 :                 list_splice_init(&msk->join_list, &join_list);
    4143                 :             : 
    4144                 :             :                 /* the following actions acquire the subflow socket lock
    4145                 :             :                  *
    4146                 :             :                  * 1) can't be invoked in atomic scope
    4147                 :             :                  * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
    4148                 :             :                  *    datapath acquires the msk socket spinlock while helding
    4149                 :             :                  *    the subflow socket lock
    4150                 :             :                  */
    4151                 :      305672 :                 msk->cb_flags &= ~flags;
    4152                 :      305672 :                 spin_unlock_bh(&sk->sk_lock.slock);
    4153                 :             : 
    4154            [ + ]:      305672 :                 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
    4155                 :          51 :                         __mptcp_flush_join_list(sk, &join_list);
    4156            [ + ]:      305672 :                 if (flags & BIT(MPTCP_PUSH_PENDING))
    4157                 :      252552 :                         __mptcp_push_pending(sk, 0);
    4158            [ + ]:      305672 :                 if (flags & BIT(MPTCP_RETRANSMIT))
    4159                 :        6621 :                         __mptcp_retrans(sk);
    4160         [ +  + ]:      305672 :                 if (spool_bl && __mptcp_move_skbs(sk, &skbs, &moved)) {
    4161                 :             :                         /* notify ack seq update */
    4162                 :      138126 :                         mptcp_cleanup_rbuf(msk, 0);
    4163                 :      138126 :                         sk->sk_data_ready(sk);
    4164                 :             :                 }
    4165                 :             : 
    4166                 :      305672 :                 cond_resched();
    4167                 :      305672 :                 spin_lock_bh(&sk->sk_lock.slock);
    4168                 :             :         }
    4169       [ + ][ + ]:     2360974 :         if (moved)
    4170                 :      138146 :                 WRITE_ONCE(msk->backlog_len, msk->backlog_len - moved);
    4171                 :             : 
    4172   [ +  -  -  + ]:     4189264 :         if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
           [ +  -  -  + ]
    4173                 :      128089 :                 __mptcp_clean_una_wakeup(sk);
    4174       [ + ][ + ]:     2360974 :         if (unlikely(msk->cb_flags)) {
    4175                 :             :                 /* be sure to sync the msk state before taking actions
    4176                 :             :                  * depending on sk_state (MPTCP_ERROR_REPORT)
    4177                 :             :                  * On sk release avoid actions depending on the first subflow
    4178                 :             :                  */
    4179   [ +  -  -  +  :        8940 :                 if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
           - ][ +  -  -  
                   +  - ]
    4180                 :        1170 :                         __mptcp_sync_state(sk, msk->pending_state);
    4181   [ +  -  -  + ]:        8940 :                 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
           [ +  -  -  + ]
    4182                 :         593 :                         __mptcp_error_report(sk);
    4183   [ +  -  -  + ]:        8940 :                 if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
           [ +  -  -  + ]
    4184                 :        2764 :                         __mptcp_sync_sndbuf(sk);
    4185                 :             :         }
    4186                 :     2360974 : }
    4187                 :             : 
    4188                 :             : /* MP_JOIN client subflow must wait for 4th ack before sending any data:
    4189                 :             :  * TCP can't schedule delack timer before the subflow is fully established.
    4190                 :             :  * MPTCP uses the delack timer to do 3rd ack retransmissions
    4191                 :             :  */
    4192                 :         642 : static void schedule_3rdack_retransmission(struct sock *ssk)
    4193                 :             : {
    4194                 :         642 :         struct inet_connection_sock *icsk = inet_csk(ssk);
    4195            [ + ]:         642 :         struct tcp_sock *tp = tcp_sk(ssk);
    4196                 :         642 :         unsigned long timeout;
    4197                 :             : 
    4198    [ +  + ][ + ]:         642 :         if (mptcp_subflow_ctx(ssk)->fully_established)
    4199                 :             :                 return;
    4200                 :             : 
    4201                 :             :         /* reschedule with a timeout above RTT, as we must look only for drop */
    4202       [ - ][ - ]:          96 :         if (tp->srtt_us)
    4203       [ + ][ + ]:          96 :                 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
    4204                 :             :         else
    4205                 :             :                 timeout = TCP_TIMEOUT_INIT;
    4206                 :          96 :         timeout += jiffies;
    4207                 :             : 
    4208       [ + ][ + ]:          96 :         WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
    4209                 :          96 :         smp_store_release(&icsk->icsk_ack.pending,
    4210                 :             :                           icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
    4211                 :          96 :         sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
    4212                 :             : }
    4213                 :             : 
    4214                 :       33162 : void mptcp_subflow_process_delegated(struct sock *ssk, long status)
    4215                 :             : {
    4216       [ + ][ + ]:       33162 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    4217                 :       33162 :         struct sock *sk = subflow->conn;
    4218                 :             : 
    4219       [ + ][ + ]:       33162 :         if (status & BIT(MPTCP_DELEGATE_SEND)) {
    4220                 :       11047 :                 mptcp_data_lock(sk);
    4221       [ + ][ + ]:       11047 :                 if (!sock_owned_by_user(sk))
    4222                 :       10171 :                         __mptcp_subflow_push_pending(sk, ssk, true);
    4223                 :             :                 else
    4224       [ + ][ + ]:         876 :                         __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
    4225                 :       11047 :                 mptcp_data_unlock(sk);
    4226                 :             :         }
    4227       [ + ][ + ]:       33162 :         if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
    4228                 :       19372 :                 mptcp_data_lock(sk);
    4229       [ + ][ + ]:       19372 :                 if (!sock_owned_by_user(sk))
    4230                 :       15581 :                         __mptcp_sync_sndbuf(sk);
    4231                 :             :                 else
    4232       [ + ][ + ]:        3791 :                         __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
    4233                 :       19372 :                 mptcp_data_unlock(sk);
    4234                 :             :         }
    4235       [ + ][ + ]:       33162 :         if (status & BIT(MPTCP_DELEGATE_ACK))
    4236                 :         642 :                 schedule_3rdack_retransmission(ssk);
    4237                 :       33162 : }
    4238                 :             : 
    4239                 :           0 : static int mptcp_hash(struct sock *sk)
    4240                 :             : {
    4241                 :             :         /* should never be called,
    4242                 :             :          * we hash the TCP subflows not the MPTCP socket
    4243                 :             :          */
    4244                 :           0 :         WARN_ON_ONCE(1);
    4245                 :           0 :         return 0;
    4246                 :             : }
    4247                 :             : 
    4248                 :          10 : static void mptcp_unhash(struct sock *sk)
    4249                 :             : {
    4250                 :             :         /* called from sk_common_release(), but nothing to do here */
    4251                 :          10 : }
    4252                 :             : 
    4253                 :           0 : static int mptcp_get_port(struct sock *sk, unsigned short snum)
    4254                 :             : {
    4255            [ # ]:           0 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4256                 :             : 
    4257         [ #  # ]:           0 :         pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
    4258            [ # ]:           0 :         if (WARN_ON_ONCE(!msk->first))
    4259                 :           0 :                 return -EINVAL;
    4260                 :             : 
    4261                 :           0 :         return inet_csk_get_port(msk->first, snum);
    4262                 :             : }
    4263                 :             : 
    4264                 :        1520 : void mptcp_finish_connect(struct sock *ssk)
    4265                 :             : {
    4266                 :        1520 :         struct mptcp_subflow_context *subflow;
    4267                 :        1520 :         struct mptcp_sock *msk;
    4268                 :        1520 :         struct sock *sk;
    4269                 :             : 
    4270            [ + ]:        1520 :         subflow = mptcp_subflow_ctx(ssk);
    4271                 :        1520 :         sk = subflow->conn;
    4272            [ + ]:        1520 :         msk = mptcp_sk(sk);
    4273                 :             : 
    4274         [ -  - ]:        1520 :         pr_debug("msk=%p, token=%u\n", sk, subflow->token);
    4275                 :             : 
    4276                 :        1520 :         subflow->map_seq = subflow->iasn;
    4277                 :        1520 :         subflow->map_subflow_seq = 1;
    4278                 :             : 
    4279                 :             :         /* the socket is not connected yet, no msk/subflow ops can access/race
    4280                 :             :          * accessing the field below
    4281                 :             :          */
    4282                 :        1520 :         WRITE_ONCE(msk->local_key, subflow->local_key);
    4283                 :        1520 :         WRITE_ONCE(msk->rcvq_space.time, mptcp_stamp());
    4284                 :             : 
    4285                 :        1520 :         mptcp_pm_new_connection(msk, ssk, 0);
    4286                 :        1520 : }
    4287                 :             : 
    4288                 :        6569 : void mptcp_sock_graft(struct sock *sk, struct socket *parent)
    4289                 :             : {
    4290                 :        6569 :         write_lock_bh(&sk->sk_callback_lock);
    4291                 :        6569 :         rcu_assign_pointer(sk->sk_wq, &parent->wq);
    4292            [ - ]:        6569 :         sk_set_socket(sk, parent);
    4293                 :        6569 :         write_unlock_bh(&sk->sk_callback_lock);
    4294                 :        6569 : }
    4295                 :             : 
    4296                 :             : /* Can be called without holding the msk socket lock; use the callback lock
    4297                 :             :  * to avoid {READ_,WRITE_}ONCE annotations on sk_socket.
    4298                 :             :  */
    4299                 :         648 : static void mptcp_sock_check_graft(struct sock *sk, struct sock *ssk)
    4300                 :             : {
    4301                 :         648 :         struct socket *sock;
    4302                 :             : 
    4303                 :         648 :         write_lock_bh(&sk->sk_callback_lock);
    4304                 :         648 :         sock = sk->sk_socket;
    4305                 :         648 :         write_unlock_bh(&sk->sk_callback_lock);
    4306            [ + ]:         648 :         if (sock) {
    4307                 :         631 :                 mptcp_sock_graft(ssk, sock);
    4308                 :         631 :                 __mptcp_inherit_cgrp_data(sk, ssk);
    4309                 :         631 :                 __mptcp_inherit_memcg(sk, ssk, GFP_ATOMIC);
    4310                 :             :         }
    4311                 :         648 : }
    4312                 :             : 
    4313                 :        1278 : bool mptcp_finish_join(struct sock *ssk)
    4314                 :             : {
    4315            [ + ]:        1278 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    4316            [ + ]:        1278 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
    4317                 :        1278 :         struct sock *parent = (void *)msk;
    4318                 :        1278 :         bool ret = true;
    4319                 :             : 
    4320         [ -  - ]:        1278 :         pr_debug("msk=%p, subflow=%p\n", msk, subflow);
    4321                 :             : 
    4322                 :             :         /* mptcp socket already closing? */
    4323       [ + ][ + ]:        1278 :         if (!mptcp_is_fully_established(parent)) {
    4324       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(parent), MPTCP_MIB_MPJOINNOTESTABLISHED);
    4325                 :           0 :                 subflow->reset_reason = MPTCP_RST_EMPTCP;
    4326                 :           0 :                 return false;
    4327                 :             :         }
    4328                 :             : 
    4329                 :             :         /* Active subflow, already present inside the conn_list; is grafted
    4330                 :             :          * either by __mptcp_subflow_connect() or accept.
    4331                 :             :          */
    4332       [ + ][ + ]:        1278 :         if (!list_empty(&subflow->node)) {
    4333                 :         630 :                 spin_lock_bh(&msk->fallback_lock);
    4334    [ +  + ][ + ]:         630 :                 if (!msk->allow_subflows) {
    4335                 :           0 :                         spin_unlock_bh(&msk->fallback_lock);
    4336                 :           0 :                         return false;
    4337                 :             :                 }
    4338                 :         630 :                 mptcp_subflow_joined(msk, ssk);
    4339                 :         630 :                 spin_unlock_bh(&msk->fallback_lock);
    4340                 :         630 :                 mptcp_propagate_sndbuf(parent, ssk);
    4341                 :         630 :                 return true;
    4342                 :             :         }
    4343                 :             : 
    4344       [ + ][ + ]:         648 :         if (!mptcp_pm_allow_new_subflow(msk)) {
    4345       [ # ][ # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_JOINREJECTED);
    4346                 :           0 :                 goto err_prohibited;
    4347                 :             :         }
    4348                 :             : 
    4349                 :             :         /* If we can't acquire msk socket lock here, let the release callback
    4350                 :             :          * handle it
    4351                 :             :          */
    4352                 :         648 :         mptcp_data_lock(parent);
    4353       [ + ][ + ]:         648 :         if (!sock_owned_by_user(parent)) {
    4354                 :         597 :                 ret = __mptcp_finish_join(msk, ssk);
    4355       [ - ][ - ]:         597 :                 if (ret) {
    4356                 :         597 :                         sock_hold(ssk);
    4357                 :         597 :                         list_add_tail(&subflow->node, &msk->conn_list);
    4358                 :         597 :                         mptcp_sock_check_graft(parent, ssk);
    4359                 :             :                 }
    4360                 :             :         } else {
    4361                 :          51 :                 sock_hold(ssk);
    4362            [ + ]:          51 :                 list_add_tail(&subflow->node, &msk->join_list);
    4363      [ +  -  - ]:          51 :                 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
              [ +  -  - ]
    4364                 :             : 
    4365                 :             :                 /* In case of later failures, __mptcp_flush_join_list() will
    4366                 :             :                  * properly orphan the ssk via mptcp_close_ssk().
    4367                 :             :                  */
    4368                 :          51 :                 mptcp_sock_check_graft(parent, ssk);
    4369                 :             :         }
    4370                 :         648 :         mptcp_data_unlock(parent);
    4371                 :             : 
    4372       [ + ][ + ]:         648 :         if (!ret) {
    4373                 :           0 :                 mptcp_pm_close_subflow(msk);
    4374                 :           0 : err_prohibited:
    4375                 :           0 :                 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
    4376                 :           0 :                 return false;
    4377                 :             :         }
    4378                 :             : 
    4379                 :             :         return true;
    4380                 :             : }
    4381                 :             : 
    4382                 :        1998 : static void mptcp_shutdown(struct sock *sk, int how)
    4383                 :             : {
    4384         [ -  - ]:        1998 :         pr_debug("sk=%p, how=%d\n", sk, how);
    4385                 :             : 
    4386         [ -  + ]:        1998 :         if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
    4387                 :        1853 :                 __mptcp_wr_shutdown(sk);
    4388                 :        1998 : }
    4389                 :             : 
    4390                 :          16 : static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
    4391                 :             : {
    4392                 :          16 :         const struct sock *sk = (void *)msk;
    4393                 :          16 :         u64 delta;
    4394                 :             : 
    4395       [ - ][ - ]:          16 :         if (sk->sk_state == TCP_LISTEN)
    4396                 :             :                 return -EINVAL;
    4397                 :             : 
    4398    [ +  - ][ - ]:          16 :         if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
    4399                 :             :                 return 0;
    4400                 :             : 
    4401                 :          16 :         delta = msk->write_seq - v;
    4402 [ +  - ][ -  - ]:          16 :         if (__mptcp_check_fallback(msk) && msk->first) {
    4403       [ + ][ + ]:          16 :                 struct tcp_sock *tp = tcp_sk(msk->first);
    4404                 :             : 
    4405                 :             :                 /* the first subflow is disconnected after close - see
    4406                 :             :                  * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
    4407                 :             :                  * so ignore that status, too.
    4408                 :             :                  */
    4409    [ +  - ][ - ]:          16 :                 if (!((1 << msk->first->sk_state) &
    4410                 :             :                       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
    4411                 :          16 :                         delta += READ_ONCE(tp->write_seq) - tp->snd_una;
    4412                 :             :         }
    4413                 :          16 :         if (delta > INT_MAX)
    4414                 :             :                 delta = INT_MAX;
    4415                 :             : 
    4416                 :          16 :         return (int)delta;
    4417                 :             : }
    4418                 :             : 
    4419                 :          16 : static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
    4420                 :             : {
    4421            [ + ]:          16 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4422                 :          16 :         bool slow;
    4423                 :             : 
    4424   [ -  +  +  - ]:          16 :         switch (cmd) {
    4425                 :           0 :         case SIOCINQ:
    4426            [ # ]:           0 :                 if (sk->sk_state == TCP_LISTEN)
    4427                 :             :                         return -EINVAL;
    4428                 :             : 
    4429                 :           0 :                 lock_sock(sk);
    4430            [ # ]:           0 :                 if (mptcp_move_skbs(sk))
    4431                 :           0 :                         mptcp_cleanup_rbuf(msk, 0);
    4432                 :           0 :                 *karg = mptcp_inq_hint(sk);
    4433                 :           0 :                 release_sock(sk);
    4434                 :           0 :                 break;
    4435                 :           4 :         case SIOCOUTQ:
    4436                 :           8 :                 slow = lock_sock_fast(sk);
    4437                 :           8 :                 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
    4438                 :           8 :                 unlock_sock_fast(sk, slow);
    4439                 :           8 :                 break;
    4440                 :           4 :         case SIOCOUTQNSD:
    4441                 :           8 :                 slow = lock_sock_fast(sk);
    4442                 :           8 :                 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
    4443                 :           8 :                 unlock_sock_fast(sk, slow);
    4444                 :           8 :                 break;
    4445                 :             :         default:
    4446                 :             :                 return -ENOIOCTLCMD;
    4447                 :             :         }
    4448                 :             : 
    4449                 :             :         return 0;
    4450                 :             : }
    4451                 :             : 
    4452                 :        1780 : static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
    4453                 :             :                          int addr_len)
    4454                 :             : {
    4455                 :        1780 :         struct mptcp_subflow_context *subflow;
    4456            [ + ]:        1780 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4457                 :        1780 :         int err = -EINVAL;
    4458                 :        1780 :         struct sock *ssk;
    4459                 :             : 
    4460                 :        1780 :         ssk = __mptcp_nmpc_sk(msk);
    4461            [ + ]:        1780 :         if (IS_ERR(ssk))
    4462                 :           0 :                 return PTR_ERR(ssk);
    4463                 :             : 
    4464                 :        1780 :         set_bit(MPTCP_RTX_ENABLED, &msk->flags);
    4465                 :        1780 :         mptcp_set_state(sk, TCP_SYN_SENT);
    4466       [ - ][ - ]:        1780 :         subflow = mptcp_subflow_ctx(ssk);
    4467                 :             : #ifdef CONFIG_TCP_MD5SIG
    4468                 :             :         /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
    4469                 :             :          * TCP option space.
    4470                 :             :          */
    4471                 :             :         if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
    4472                 :             :                 mptcp_early_fallback(msk, subflow, MPTCP_MIB_MD5SIGFALLBACK);
    4473                 :             : #endif
    4474       [ - ][ - ]:        1780 :         if (subflow->request_mptcp) {
    4475       [ + ][ + ]:        1780 :                 if (mptcp_active_should_disable(sk))
    4476                 :          12 :                         mptcp_early_fallback(msk, subflow,
    4477                 :             :                                              MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
    4478       [ + ][ + ]:        1768 :                 else if (mptcp_token_new_connect(ssk) < 0)
    4479                 :           0 :                         mptcp_early_fallback(msk, subflow,
    4480                 :             :                                              MPTCP_MIB_TOKENFALLBACKINIT);
    4481                 :             :         }
    4482                 :             : 
    4483                 :        1780 :         WRITE_ONCE(msk->write_seq, subflow->idsn);
    4484                 :        1780 :         WRITE_ONCE(msk->snd_nxt, subflow->idsn);
    4485                 :        1780 :         WRITE_ONCE(msk->snd_una, subflow->idsn);
    4486       [ + ][ + ]:        1780 :         if (likely(!__mptcp_check_fallback(msk)))
    4487       [ - ][ - ]:        1768 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
    4488                 :             : 
    4489                 :             :         /* if reaching here via the fastopen/sendmsg path, the caller already
    4490                 :             :          * acquired the subflow socket lock, too.
    4491                 :             :          */
    4492       [ + ][ + ]:        1780 :         if (!msk->fastopening)
    4493                 :        1710 :                 lock_sock(ssk);
    4494                 :             : 
    4495                 :             :         /* the following mirrors closely a very small chunk of code from
    4496                 :             :          * __inet_stream_connect()
    4497                 :             :          */
    4498       [ + ][ + ]:        1780 :         if (ssk->sk_state != TCP_CLOSE)
    4499                 :           0 :                 goto out;
    4500                 :             : 
    4501      [ -  -  - ]:        1780 :         if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
              [ -  -  - ]
    4502                 :           0 :                 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
    4503       [ - ][ - ]:           0 :                 if (err)
    4504                 :           0 :                         goto out;
    4505                 :             :         }
    4506                 :             : 
    4507                 :        1780 :         err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
    4508       [ + ][ + ]:        1780 :         if (err < 0)
    4509                 :           6 :                 goto out;
    4510                 :             : 
    4511                 :             :         /* as tcp_connect_init(): drop an error from a previous connection */
    4512                 :        1774 :         WRITE_ONCE(sk->sk_err, 0);
    4513   [ +  -  -  + ]:        3482 :         inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
           [ +  -  -  + ]
    4514                 :             : 
    4515                 :        1780 : out:
    4516       [ + ][ + ]:        1780 :         if (!msk->fastopening)
    4517                 :        1710 :                 release_sock(ssk);
    4518                 :             : 
    4519                 :             :         /* on successful connect, the msk state will be moved to established by
    4520                 :             :          * subflow_finish_connect()
    4521                 :             :          */
    4522       [ + ][ + ]:        1780 :         if (unlikely(err)) {
    4523                 :           6 :                 mptcp_token_destroy(msk);
    4524                 :           6 :                 mptcp_set_state(sk, TCP_CLOSE);
    4525                 :             : 
    4526                 :           6 :                 spin_lock_bh(&msk->fallback_lock);
    4527                 :           6 :                 msk->allow_subflows = true;
    4528                 :           6 :                 msk->allow_infinite_fallback = true;
    4529                 :           6 :                 clear_bit(MPTCP_FALLBACK_DONE, &msk->flags);
    4530                 :           6 :                 spin_unlock_bh(&msk->fallback_lock);
    4531                 :             : 
    4532                 :           6 :                 mptcp_subflow_ctx_reset(mptcp_subflow_ctx(ssk));
    4533                 :           6 :                 return err;
    4534                 :             :         }
    4535                 :             : 
    4536                 :        1774 :         mptcp_copy_inaddrs(sk, ssk);
    4537                 :        1774 :         return 0;
    4538                 :             : }
    4539                 :             : 
    4540                 :             : static struct proto mptcp_prot = {
    4541                 :             :         .name           = "MPTCP",
    4542                 :             :         .owner          = THIS_MODULE,
    4543                 :             :         .init           = mptcp_init_sock,
    4544                 :             :         .connect        = mptcp_connect,
    4545                 :             :         .disconnect     = mptcp_disconnect,
    4546                 :             :         .close          = mptcp_close,
    4547                 :             :         .setsockopt     = mptcp_setsockopt,
    4548                 :             :         .getsockopt     = mptcp_getsockopt,
    4549                 :             :         .shutdown       = mptcp_shutdown,
    4550                 :             :         .destroy        = mptcp_destroy,
    4551                 :             :         .sendmsg        = mptcp_sendmsg,
    4552                 :             :         .ioctl          = mptcp_ioctl,
    4553                 :             :         .recvmsg        = mptcp_recvmsg,
    4554                 :             :         .release_cb     = mptcp_release_cb,
    4555                 :             :         .hash           = mptcp_hash,
    4556                 :             :         .unhash         = mptcp_unhash,
    4557                 :             :         .get_port       = mptcp_get_port,
    4558                 :             :         .stream_memory_free     = mptcp_stream_memory_free,
    4559                 :             :         .sockets_allocated      = &mptcp_sockets_allocated,
    4560                 :             : 
    4561                 :             :         .memory_allocated       = &net_aligned_data.tcp_memory_allocated,
    4562                 :             :         .per_cpu_fw_alloc       = &tcp_memory_per_cpu_fw_alloc,
    4563                 :             : 
    4564                 :             :         .memory_pressure        = &tcp_memory_pressure,
    4565                 :             :         .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_wmem),
    4566                 :             :         .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_rmem),
    4567                 :             :         .sysctl_mem     = sysctl_tcp_mem,
    4568                 :             :         .obj_size       = sizeof(struct mptcp_sock),
    4569                 :             :         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
    4570                 :             :         .no_autobind    = true,
    4571                 :             : };
    4572                 :             : 
    4573                 :        1836 : static int mptcp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
    4574                 :             : {
    4575            [ + ]:        1836 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4576                 :        1836 :         struct sock *ssk, *sk = sock->sk;
    4577                 :        1836 :         int err = -EINVAL;
    4578                 :             : 
    4579                 :        1836 :         lock_sock(sk);
    4580                 :        1836 :         ssk = __mptcp_nmpc_sk(msk);
    4581            [ + ]:        1836 :         if (IS_ERR(ssk)) {
    4582                 :           0 :                 err = PTR_ERR(ssk);
    4583                 :           0 :                 goto unlock;
    4584                 :             :         }
    4585                 :             : 
    4586            [ + ]:        1836 :         if (sk->sk_family == AF_INET)
    4587                 :         958 :                 err = inet_bind_sk(ssk, uaddr, addr_len);
    4588                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    4589            [ - ]:         878 :         else if (sk->sk_family == AF_INET6)
    4590                 :         878 :                 err = inet6_bind_sk(ssk, uaddr, addr_len);
    4591                 :             : #endif
    4592            [ + ]:        1836 :         if (!err)
    4593                 :        1835 :                 mptcp_copy_inaddrs(sk, ssk);
    4594                 :             : 
    4595                 :           1 : unlock:
    4596                 :        1836 :         release_sock(sk);
    4597                 :        1836 :         return err;
    4598                 :             : }
    4599                 :             : 
    4600                 :        1835 : static int mptcp_listen(struct socket *sock, int backlog)
    4601                 :             : {
    4602            [ + ]:        1835 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4603                 :        1835 :         struct sock *sk = sock->sk;
    4604                 :        1835 :         struct sock *ssk;
    4605                 :        1835 :         bool listening;
    4606                 :        1835 :         int err;
    4607                 :             : 
    4608         [ -  - ]:        1835 :         pr_debug("msk=%p\n", msk);
    4609                 :             : 
    4610                 :        1835 :         lock_sock(sk);
    4611                 :             : 
    4612                 :        1835 :         err = -EINVAL;
    4613         [ -  + ]:        1835 :         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
    4614                 :           0 :                 goto unlock;
    4615                 :             : 
    4616                 :        1835 :         ssk = __mptcp_nmpc_sk(msk);
    4617            [ + ]:        1835 :         if (IS_ERR(ssk)) {
    4618                 :           0 :                 err = PTR_ERR(ssk);
    4619                 :           0 :                 goto unlock;
    4620                 :             :         }
    4621                 :             : 
    4622                 :        1835 :         listening = sk->sk_state == TCP_LISTEN;
    4623                 :        1835 :         set_bit(MPTCP_RTX_ENABLED, &msk->flags);
    4624                 :        1835 :         mptcp_set_state(sk, TCP_LISTEN);
    4625                 :        1835 :         sock_set_flag(sk, SOCK_RCU_FREE);
    4626                 :             : 
    4627                 :        1835 :         lock_sock(ssk);
    4628                 :        1835 :         err = __inet_listen_sk(ssk, backlog);
    4629 [ +  - ][ +  - ]:        1835 :         if (err && ssk->sk_state == TCP_CLOSE) {
    4630                 :             :                 /* inet_csk_listen_start() leaves this drain to MPTCP */
    4631                 :           0 :                 mptcp_subflow_queue_clean(sk, ssk);
    4632                 :           0 :                 inet_csk_listen_stop(ssk);
    4633                 :             :         }
    4634                 :        1835 :         release_sock(ssk);
    4635                 :        1835 :         mptcp_set_state(sk, inet_sk_state_load(ssk));
    4636                 :             : 
    4637                 :             :         /* only the backlog changes when listening already */
    4638       [ + ][ + ]:        1835 :         if (!err && !listening) {
    4639                 :        1835 :                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
    4640                 :        1835 :                 mptcp_copy_inaddrs(sk, ssk);
    4641                 :        1835 :                 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
    4642                 :             :         }
    4643                 :             : 
    4644                 :           0 : unlock:
    4645                 :        1835 :         release_sock(sk);
    4646                 :        1835 :         return err;
    4647                 :             : }
    4648                 :             : 
    4649                 :             : /* Similar to __sk_charge(), but the msk and its subflows share pages */
    4650                 :          42 : static int mptcp_charge_subflow(struct sock *sk, struct sock *ssk, int mem)
    4651                 :             : {
    4652                 :          63 :         int new_mem = mem + ssk->sk_forward_alloc +
    4653                 :          42 :                       atomic_read(&ssk->sk_rmem_alloc);
    4654                 :          42 :         int amt = sk_mem_pages(new_mem) - sk_mem_pages(mem);
    4655                 :          42 :         gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
    4656                 :             : 
    4657                 :          42 :         mem_cgroup_sk_inherit(sk, ssk);
    4658       [ + ][ + ]:          42 :         if (amt > 0)
    4659                 :           0 :                 mem_cgroup_sk_charge(ssk, amt, gfp);
    4660                 :          42 :         kmem_cache_charge(ssk, gfp);
    4661                 :          42 :         return new_mem;
    4662                 :             : }
    4663                 :             : 
    4664                 :        1654 : static void mptcp_graft_subflows(struct sock *sk)
    4665                 :             : {
    4666                 :        1654 :         struct mptcp_subflow_context *subflow;
    4667            [ + ]:        1654 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4668                 :        1654 :         int mem;
    4669                 :             : 
    4670            [ + ]:        1654 :         if (mem_cgroup_sockets_enabled) {
    4671                 :          42 :                 LIST_HEAD(join_list);
    4672                 :             : 
    4673                 :             :                 /* Subflows joining after __inet_accept() will get the
    4674                 :             :                  * mem CG properly initialized at mptcp_finish_join() time,
    4675                 :             :                  * even if pending in join_list. The ones that joined before
    4676                 :             :                  * need explicit initialization by the loop below.
    4677                 :             :                  */
    4678                 :          42 :                 mptcp_data_lock(sk);
    4679            [ + ]:          42 :                 list_splice_init(&msk->join_list, &join_list);
    4680                 :          42 :                 mptcp_data_unlock(sk);
    4681                 :             : 
    4682                 :          42 :                 __mptcp_flush_join_list(sk, &join_list);
    4683                 :             :         }
    4684                 :             : 
    4685                 :             :         /* What __inet_accept() just charged for the msk, before rounding up. */
    4686                 :        1654 :         mem = sk->sk_forward_alloc + atomic_read(&sk->sk_rmem_alloc);
    4687                 :             : 
    4688       [ + ][ + ]:        3319 :         mptcp_for_each_subflow(msk, subflow) {
    4689                 :        1665 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    4690                 :             : 
    4691                 :        1665 :                 lock_sock(ssk);
    4692                 :             : 
    4693                 :             :                 /* Set ssk->sk_socket of accept()ed flows to mptcp socket.
    4694                 :             :                  * This is needed so NOSPACE flag can be set from tcp stack.
    4695                 :             :                  */
    4696       [ - ][ - ]:        1665 :                 if (!ssk->sk_socket)
    4697                 :        1665 :                         mptcp_sock_graft(ssk, sk->sk_socket);
    4698                 :             : 
    4699                 :             :                 /* Got the memcg when joining: its memory is already charged */
    4700 [ +  + ][ +  + ]:        1665 :                 if (!mem_cgroup_sk_enabled(sk) || mem_cgroup_from_sk(ssk))
    4701                 :        1623 :                         goto unlock;
    4702                 :             : 
    4703                 :          42 :                 __mptcp_inherit_cgrp_data(sk, ssk);
    4704                 :          42 :                 mem = mptcp_charge_subflow(sk, ssk, mem);
    4705                 :             : 
    4706                 :        1665 : unlock:
    4707                 :        1665 :                 release_sock(ssk);
    4708                 :             :         }
    4709                 :        1654 : }
    4710                 :             : 
    4711                 :        1844 : static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
    4712                 :             :                                struct proto_accept_arg *arg)
    4713                 :             : {
    4714            [ + ]:        1844 :         struct mptcp_sock *msk = mptcp_sk(sock->sk);
    4715                 :        1844 :         struct sock *ssk, *newsk;
    4716                 :             : 
    4717         [ -  - ]:        1844 :         pr_debug("msk=%p\n", msk);
    4718                 :             : 
    4719                 :             :         /* Buggy applications can call accept on socket states other then LISTEN
    4720                 :             :          * but no need to allocate the first subflow just to error out.
    4721                 :             :          */
    4722                 :        1844 :         ssk = READ_ONCE(msk->first);
    4723       [ - ][ - ]:        1844 :         if (!ssk)
    4724                 :             :                 return -EINVAL;
    4725                 :             : 
    4726         [ -  - ]:        1844 :         pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
    4727                 :        1844 :         newsk = inet_csk_accept(ssk, arg);
    4728            [ + ]:        1844 :         if (!newsk)
    4729                 :          12 :                 return arg->err;
    4730                 :             : 
    4731      [ -  -  - ]:        1832 :         pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
                 [ -  - ]
    4732    [ +  + ][ + ]:        1832 :         if (sk_is_mptcp(newsk)) {
    4733                 :        1654 :                 struct mptcp_subflow_context *subflow;
    4734                 :        1654 :                 struct sock *new_mptcp_sock;
    4735                 :             : 
    4736       [ + ][ + ]:        1654 :                 subflow = mptcp_subflow_ctx(newsk);
    4737                 :        1654 :                 new_mptcp_sock = subflow->conn;
    4738                 :             : 
    4739                 :             :                 /* is_mptcp should be false if subflow->conn is missing, see
    4740                 :             :                  * subflow_syn_recv_sock()
    4741                 :             :                  */
    4742       [ + ][ + ]:        1654 :                 if (WARN_ON_ONCE(!new_mptcp_sock)) {
    4743       [ # ][ # ]:           0 :                         tcp_sk(newsk)->is_mptcp = 0;
    4744                 :           0 :                         goto tcpfallback;
    4745                 :             :                 }
    4746                 :             : 
    4747                 :        1654 :                 newsk = new_mptcp_sock;
    4748       [ - ][ - ]:        1654 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
    4749                 :             : 
    4750            [ + ]:        1654 :                 newsk->sk_kern_sock = arg->kern;
    4751                 :        1654 :                 lock_sock(newsk);
    4752                 :        1654 :                 __inet_accept(sock, newsock, newsk);
    4753                 :             : 
    4754                 :        1654 :                 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
    4755       [ + ][ + ]:        1654 :                 msk = mptcp_sk(newsk);
    4756                 :        1654 :                 msk->in_accept_queue = 0;
    4757                 :             : 
    4758                 :        1654 :                 mptcp_graft_subflows(newsk);
    4759                 :        1654 :                 mptcp_rps_record_subflows(msk);
    4760       [ + ][ + ]:        1654 :                 __mptcp_propagate_sndbuf(newsk, mptcp_subflow_tcp_sock(subflow));
    4761                 :             : 
    4762                 :             :                 /* Do late cleanup for the first subflow as necessary. Also
    4763                 :             :                  * deal with bad peers not doing a complete shutdown.
    4764                 :             :                  */
    4765       [ + ][ + ]:        1654 :                 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
    4766       [ - ][ - ]:           6 :                         if (unlikely(list_is_singular(&msk->conn_list))) {
    4767                 :           6 :                                 mptcp_check_rcv_close(newsk);
    4768                 :             :                                 /* not reported in the accept queue */
    4769                 :           6 :                                 __mptcp_error_report(newsk);
    4770                 :           6 :                                 mptcp_set_state(newsk, TCP_CLOSE);
    4771                 :             :                         }
    4772                 :           6 :                         mptcp_close_ssk(newsk, msk->first,
    4773                 :           6 :                                         mptcp_subflow_ctx(msk->first));
    4774                 :             :                 }
    4775                 :             :         } else {
    4776                 :         178 : tcpfallback:
    4777            [ + ]:         178 :                 newsk->sk_kern_sock = arg->kern;
    4778                 :         178 :                 lock_sock(newsk);
    4779                 :         178 :                 __inet_accept(sock, newsock, newsk);
    4780                 :             :                 /* we are being invoked after accepting a non-mp-capable
    4781                 :             :                  * flow: sk is a tcp_sk, not an mptcp one.
    4782                 :             :                  *
    4783                 :             :                  * Hand the socket over to tcp so all further socket ops
    4784                 :             :                  * bypass mptcp.
    4785                 :             :                  */
    4786                 :         178 :                 WRITE_ONCE(newsock->sk->sk_socket->ops,
    4787                 :             :                            mptcp_fallback_tcp_ops(newsock->sk));
    4788                 :             :         }
    4789                 :        1832 :         release_sock(newsk);
    4790                 :             : 
    4791                 :        1832 :         return 0;
    4792                 :             : }
    4793                 :             : 
    4794                 :      536848 : static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
    4795                 :             : {
    4796                 :      536848 :         struct sock *sk = (struct sock *)msk;
    4797                 :             : 
    4798            [ + ]:      536848 :         if (__mptcp_stream_is_writeable(sk, 1))
    4799                 :             :                 return EPOLLOUT | EPOLLWRNORM;
    4800                 :             : 
    4801                 :      158779 :         sk_set_nospace(sk);
    4802                 :      158779 :         smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
    4803            [ + ]:      158779 :         if (__mptcp_stream_is_writeable(sk, 1))
    4804                 :             :                 return EPOLLOUT | EPOLLWRNORM;
    4805                 :             : 
    4806                 :             :         return 0;
    4807                 :             : }
    4808                 :             : 
    4809                 :     1052347 : static __poll_t mptcp_poll(struct file *file, struct socket *sock,
    4810                 :             :                            struct poll_table_struct *wait)
    4811                 :             : {
    4812                 :     1052347 :         struct sock *sk = sock->sk;
    4813                 :     1052347 :         struct mptcp_sock *msk;
    4814                 :     1052347 :         __poll_t mask = 0;
    4815                 :     1052347 :         u8 shutdown;
    4816                 :     1052347 :         int state;
    4817                 :             : 
    4818            [ + ]:     1052347 :         msk = mptcp_sk(sk);
    4819                 :     1052347 :         sock_poll_wait(file, sock, wait);
    4820                 :             : 
    4821                 :     1052347 :         state = inet_sk_state_load(sk);
    4822         [ -  - ]:     1052347 :         pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
    4823            [ + ]:     1052347 :         if (state == TCP_LISTEN) {
    4824                 :        2768 :                 struct sock *ssk = READ_ONCE(msk->first);
    4825                 :             : 
    4826            [ + ]:        2768 :                 if (WARN_ON_ONCE(!ssk))
    4827                 :           0 :                         return 0;
    4828                 :             : 
    4829            [ + ]:        4160 :                 return inet_csk_listen_poll(ssk);
    4830                 :             :         }
    4831                 :             : 
    4832                 :     1049579 :         shutdown = READ_ONCE(sk->sk_shutdown);
    4833            [ + ]:     1049579 :         if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
    4834                 :        4095 :                 mask |= EPOLLHUP;
    4835            [ + ]:     1049579 :         if (shutdown & RCV_SHUTDOWN)
    4836                 :      200571 :                 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
    4837                 :             : 
    4838            [ - ]:     1049579 :         if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
    4839                 :     1049579 :                 mask |= mptcp_check_readable(sk);
    4840            [ + ]:     1049579 :                 if (shutdown & SEND_SHUTDOWN)
    4841                 :      512731 :                         mask |= EPOLLOUT | EPOLLWRNORM;
    4842                 :             :                 else
    4843                 :      536848 :                         mask |= mptcp_check_writeable(msk);
    4844 [ #  # ][ #  # ]:           0 :         } else if (state == TCP_SYN_SENT &&
    4845      [ #  #  # ]:           0 :                    inet_test_bit(DEFER_CONNECT, sk)) {
              [ #  #  # ]
    4846                 :             :                 /* cf tcp_poll() note about TFO */
    4847                 :           0 :                 mask |= EPOLLOUT | EPOLLWRNORM;
    4848                 :             :         }
    4849                 :             : 
    4850                 :             :         /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
    4851                 :     1049579 :         smp_rmb();
    4852 [ +  + ][ +  + ]:     1049579 :         if (READ_ONCE(sk->sk_err) ||
    4853       [ + ][ + ]:     1049569 :             !skb_queue_empty_lockless(&sk->sk_error_queue))
    4854                 :          10 :                 mask |= EPOLLERR;
    4855                 :             : 
    4856                 :             :         return mask;
    4857                 :             : }
    4858                 :             : 
    4859                 :      681442 : static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
    4860                 :             : {
    4861            [ + ]:      681442 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4862                 :      681442 :         struct sk_buff *skb;
    4863                 :      681442 :         u32 offset;
    4864                 :             : 
    4865            [ + ]:      681442 :         if (!list_empty(&msk->backlog_list))
    4866                 :        3461 :                 mptcp_move_skbs(sk);
    4867                 :             : 
    4868         [ +  + ]:      681442 :         while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
    4869                 :      165059 :                 offset = MPTCP_SKB_CB(skb)->offset;
    4870            [ - ]:      165059 :                 if (offset < skb->len) {
    4871                 :      165059 :                         *off = offset;
    4872                 :      165059 :                         return skb;
    4873                 :             :                 }
    4874                 :           0 :                 mptcp_eat_recv_skb(sk, skb);
    4875                 :             :         }
    4876                 :             :         return NULL;
    4877                 :             : }
    4878                 :             : 
    4879                 :             : /*
    4880                 :             :  * Note:
    4881                 :             :  *      - It is assumed that the socket was locked by the caller.
    4882                 :             :  */
    4883                 :      375856 : static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
    4884                 :             :                              sk_read_actor_t recv_actor, bool noack)
    4885                 :             : {
    4886            [ + ]:      375856 :         struct mptcp_sock *msk = mptcp_sk(sk);
    4887                 :      375856 :         struct sk_buff *skb;
    4888                 :      375856 :         int copied = 0;
    4889                 :      375856 :         u32 offset;
    4890                 :             : 
    4891                 :      375856 :         msk_owned_by_me(msk);
    4892                 :             : 
    4893 [ +  - ][ +  - ]:      375856 :         if (sk->sk_state == TCP_LISTEN)
    4894                 :             :                 return -ENOTCONN;
    4895       [ + ][ + ]:      531444 :         while ((skb = mptcp_recv_skb(sk, &offset)) != NULL) {
    4896                 :      161390 :                 u32 data_len = skb->len - offset;
    4897                 :      161390 :                 int count;
    4898                 :      161390 :                 u32 size;
    4899                 :             : 
    4900                 :      161390 :                 size = min_t(size_t, data_len, INT_MAX);
    4901                 :      161390 :                 count = recv_actor(desc, skb, offset, size);
    4902       [ + ][ + ]:      161390 :                 if (count <= 0) {
    4903       [ + ][ + ]:        1241 :                         if (!copied)
    4904                 :        1163 :                                 copied = count;
    4905                 :             :                         break;
    4906                 :             :                 }
    4907                 :             : 
    4908                 :      160149 :                 copied += count;
    4909                 :             : 
    4910                 :      160149 :                 msk->bytes_consumed += count;
    4911       [ + ][ + ]:      160149 :                 if (count < data_len) {
    4912                 :        2871 :                         MPTCP_SKB_CB(skb)->offset += count;
    4913                 :        2871 :                         MPTCP_SKB_CB(skb)->map_seq += count;
    4914                 :        2871 :                         break;
    4915                 :             :                 }
    4916                 :             : 
    4917                 :      157278 :                 mptcp_eat_recv_skb(sk, skb);
    4918       [ + ][ + ]:      157278 :                 if (!desc->count)
    4919                 :             :                         break;
    4920                 :             :         }
    4921                 :             : 
    4922       [ + ][ + ]:      375856 :         if (noack)
    4923                 :           0 :                 goto out;
    4924                 :             : 
    4925                 :      375856 :         mptcp_rcv_space_adjust(msk, copied);
    4926                 :             : 
    4927       [ + ][ + ]:      375856 :         if (copied > 0) {
    4928                 :      149998 :                 mptcp_recv_skb(sk, &offset);
    4929                 :      149998 :                 mptcp_cleanup_rbuf(msk, copied);
    4930                 :             :         }
    4931                 :      225858 : out:
    4932                 :             :         return copied;
    4933                 :             : }
    4934                 :             : 
    4935                 :           0 : static int mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
    4936                 :             :                            sk_read_actor_t recv_actor)
    4937                 :             : {
    4938                 :           0 :         return __mptcp_read_sock(sk, desc, recv_actor, false);
    4939                 :             : }
    4940                 :             : 
    4941                 :      375856 : static int __mptcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
    4942                 :             : {
    4943                 :             :         /* Store TCP splice context information in read_descriptor_t. */
    4944                 :      375856 :         read_descriptor_t rd_desc = {
    4945                 :             :                 .arg.data = tss,
    4946                 :      375856 :                 .count    = tss->len,
    4947                 :             :         };
    4948                 :             : 
    4949                 :      375856 :         return mptcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
    4950                 :             : }
    4951                 :             : 
    4952                 :             : /**
    4953                 :             :  *  mptcp_splice_read - splice data from MPTCP socket to a pipe
    4954                 :             :  * @sock:       socket to splice from
    4955                 :             :  * @ppos:       position (not valid)
    4956                 :             :  * @pipe:       pipe to splice to
    4957                 :             :  * @len:        number of bytes to splice
    4958                 :             :  * @flags:      splice modifier flags
    4959                 :             :  *
    4960                 :             :  * Description:
    4961                 :             :  *    Will read pages from given socket and fill them into a pipe.
    4962                 :             :  *
    4963                 :             :  * Return:
    4964                 :             :  *    Amount of bytes that have been spliced.
    4965                 :             :  *
    4966                 :             :  **/
    4967                 :      135508 : static ssize_t mptcp_splice_read(struct socket *sock, loff_t *ppos,
    4968                 :             :                                  struct pipe_inode_info *pipe, size_t len,
    4969                 :             :                                  unsigned int flags)
    4970                 :             : {
    4971                 :      135508 :         struct tcp_splice_state tss = {
    4972                 :             :                 .pipe   = pipe,
    4973                 :             :                 .len    = len,
    4974                 :             :                 .flags  = flags,
    4975                 :             :         };
    4976                 :      135508 :         struct sock *sk = sock->sk;
    4977                 :      135508 :         ssize_t spliced = 0;
    4978                 :      135508 :         int ret = 0;
    4979                 :      135508 :         long timeo;
    4980                 :             : 
    4981                 :             :         /*
    4982                 :             :          * We can't seek on a socket input
    4983                 :             :          */
    4984       [ - ][ - ]:      135508 :         if (unlikely(*ppos))
    4985                 :             :                 return -ESPIPE;
    4986                 :             : 
    4987                 :      135508 :         lock_sock(sk);
    4988                 :             : 
    4989            [ + ]:      135508 :         mptcp_rps_record_subflows(mptcp_sk(sk));
    4990                 :             : 
    4991            [ - ]:      135508 :         timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK);
    4992       [ + ][ + ]:      375856 :         while (tss.len) {
    4993                 :      375856 :                 ret = __mptcp_splice_read(sk, &tss);
    4994       [ + ][ + ]:      375856 :                 if (ret < 0) {
    4995                 :             :                         break;
    4996       [ + ][ + ]:      374693 :                 } else if (!ret) {
    4997       [ + ][ + ]:      224695 :                         if (spliced)
    4998                 :             :                                 break;
    4999       [ + ][ + ]:       94509 :                         if (sock_flag(sk, SOCK_DONE))
    5000                 :             :                                 break;
    5001            [ + ]:       94269 :                         ret = sock_error(sk);
    5002       [ - ][ # ]:       89124 :                         if (ret)
    5003                 :             :                                 break;
    5004       [ - ][ - ]:       94269 :                         if (sk->sk_shutdown & RCV_SHUTDOWN)
    5005                 :             :                                 break;
    5006       [ - ][ - ]:       94269 :                         if (sk->sk_state == TCP_CLOSE) {
    5007                 :             :                                 /*
    5008                 :             :                                  * This occurs when user tries to read
    5009                 :             :                                  * from never connected socket.
    5010                 :             :                                  */
    5011                 :             :                                 ret = -ENOTCONN;
    5012                 :             :                                 break;
    5013                 :             :                         }
    5014       [ - ][ - ]:       94269 :                         if (!timeo) {
    5015                 :             :                                 ret = -EAGAIN;
    5016                 :             :                                 break;
    5017                 :             :                         }
    5018                 :             :                         /* if __mptcp_splice_read() got nothing while we have
    5019                 :             :                          * an skb in receive queue, we do not want to loop.
    5020                 :             :                          * This might happen with URG data.
    5021                 :             :                          */
    5022       [ - ][ - ]:       94269 :                         if (!skb_queue_empty(&sk->sk_receive_queue))
    5023                 :             :                                 break;
    5024                 :       94269 :                         ret = sk_wait_data(sk, &timeo, NULL);
    5025       [ - ][ - ]:       94269 :                         if (ret < 0)
    5026                 :             :                                 break;
    5027       [ + ][ + ]:       94269 :                         if (signal_pending(current)) {
    5028       [ # ][ # ]:           0 :                                 ret = sock_intr_errno(timeo);
    5029                 :             :                                 break;
    5030                 :             :                         }
    5031                 :       94269 :                         continue;
    5032                 :             :                 }
    5033                 :      149998 :                 tss.len -= ret;
    5034                 :      149998 :                 spliced += ret;
    5035                 :             : 
    5036 [ +  - ][ +  - ]:      149998 :                 if (!tss.len || !timeo)
    5037                 :             :                         break;
    5038                 :      146360 :                 release_sock(sk);
    5039                 :      146360 :                 lock_sock(sk);
    5040                 :             : 
    5041       [ + ][ + ]:      146360 :                 if (tcp_recv_should_stop(sk))
    5042                 :             :                         break;
    5043                 :             :         }
    5044                 :             : 
    5045                 :      135508 :         release_sock(sk);
    5046                 :             : 
    5047       [ + ][ + ]:      135508 :         if (spliced)
    5048                 :             :                 return spliced;
    5049                 :             : 
    5050                 :         240 :         return ret;
    5051                 :             : }
    5052                 :             : 
    5053                 :             : static const struct proto_ops mptcp_stream_ops = {
    5054                 :             :         .family            = PF_INET,
    5055                 :             :         .owner             = THIS_MODULE,
    5056                 :             :         .release           = inet_release,
    5057                 :             :         .bind              = mptcp_bind,
    5058                 :             :         .connect           = inet_stream_connect,
    5059                 :             :         .socketpair        = sock_no_socketpair,
    5060                 :             :         .accept            = mptcp_stream_accept,
    5061                 :             :         .getname           = inet_getname,
    5062                 :             :         .poll              = mptcp_poll,
    5063                 :             :         .ioctl             = inet_ioctl,
    5064                 :             :         .gettstamp         = sock_gettstamp,
    5065                 :             :         .listen            = mptcp_listen,
    5066                 :             :         .shutdown          = inet_shutdown,
    5067                 :             :         .setsockopt        = sock_common_setsockopt,
    5068                 :             :         .getsockopt        = sock_common_getsockopt,
    5069                 :             :         .sendmsg           = inet_sendmsg,
    5070                 :             :         .recvmsg           = inet_recvmsg,
    5071                 :             :         .mmap              = sock_no_mmap,
    5072                 :             :         .set_rcvlowat      = mptcp_set_rcvlowat,
    5073                 :             :         .read_sock         = mptcp_read_sock,
    5074                 :             :         .splice_read       = mptcp_splice_read,
    5075                 :             : };
    5076                 :             : 
    5077                 :             : static struct inet_protosw mptcp_protosw = {
    5078                 :             :         .type           = SOCK_STREAM,
    5079                 :             :         .protocol       = IPPROTO_MPTCP,
    5080                 :             :         .prot           = &mptcp_prot,
    5081                 :             :         .ops            = &mptcp_stream_ops,
    5082                 :             :         .flags          = INET_PROTOSW_ICSK,
    5083                 :             : };
    5084                 :             : 
    5085                 :       75988 : static int mptcp_napi_poll(struct napi_struct *napi, int budget)
    5086                 :             : {
    5087                 :       75988 :         struct mptcp_delegated_action *delegated;
    5088                 :       75988 :         struct mptcp_subflow_context *subflow;
    5089                 :       75988 :         int work_done = 0;
    5090                 :             : 
    5091                 :       75988 :         delegated = container_of(napi, struct mptcp_delegated_action, napi);
    5092       [ + ][ + ]:      153668 :         while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
    5093                 :       77680 :                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
    5094                 :       77680 :                 long status;
    5095                 :             : 
    5096                 :       77680 :                 bh_lock_sock_nested(ssk);
    5097       [ + ][ + ]:       77680 :                 if (!sock_owned_by_user(ssk)) {
    5098                 :       26910 :                         status = xchg(&subflow->delegated_status, 0);
    5099       [ - ][ - ]:       26910 :                         if (!(status & BIT(MPTCP_DELEGATE_DEAD)))
    5100                 :       26910 :                                 mptcp_subflow_process_delegated(ssk, status);
    5101                 :             :                 } else {
    5102                 :             :                         /* tcp_release_cb_override already processed
    5103                 :             :                          * the action or will do at next release_sock().
    5104                 :             :                          * In both case must dequeue the subflow here - on the same
    5105                 :             :                          * CPU that scheduled it.
    5106                 :             :                          */
    5107                 :       50770 :                         smp_wmb();
    5108         [ +  - ]:       50770 :                         status = set_mask_bits(&subflow->delegated_status,
              [ +  -  + ]
    5109                 :             :                                                BIT(MPTCP_DELEGATE_SCHEDULED), 0);
    5110                 :             :                 }
    5111                 :       77680 :                 bh_unlock_sock(ssk);
    5112                 :       77680 :                 sock_put(ssk);
    5113                 :             : 
    5114       [ + ][ + ]:       77680 :                 if (status & BIT(MPTCP_DELEGATE_DEAD))
    5115                 :           0 :                         kfree_rcu(subflow, rcu);
    5116                 :             : 
    5117       [ - ][ - ]:       77680 :                 if (++work_done == budget)
    5118                 :             :                         return budget;
    5119                 :             :         }
    5120                 :             : 
    5121                 :             :         /* always provide a 0 'work_done' argument, so that napi_complete_done
    5122                 :             :          * will not try accessing the NULL napi->dev ptr
    5123                 :             :          */
    5124                 :       75988 :         napi_complete_done(napi, 0);
    5125                 :       75988 :         return work_done;
    5126                 :             : }
    5127                 :             : 
    5128                 :           6 : void __init mptcp_proto_init(void)
    5129                 :             : {
    5130                 :           6 :         struct mptcp_delegated_action *delegated;
    5131                 :           6 :         int cpu;
    5132                 :             : 
    5133                 :           6 :         mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
    5134                 :             : 
    5135            [ + ]:           6 :         if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
    5136                 :           0 :                 panic("Failed to allocate MPTCP pcpu counter\n");
    5137                 :             : 
    5138                 :           6 :         mptcp_napi_dev = alloc_netdev_dummy(0);
    5139            [ + ]:           6 :         if (!mptcp_napi_dev)
    5140                 :           0 :                 panic("Failed to allocate MPTCP dummy netdev\n");
    5141         [ -  + ]:          54 :         for_each_possible_cpu(cpu) {
    5142                 :          24 :                 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
    5143                 :          24 :                 INIT_LIST_HEAD(&delegated->head);
    5144                 :          24 :                 netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
    5145                 :             :                                   mptcp_napi_poll);
    5146                 :          24 :                 napi_enable(&delegated->napi);
    5147                 :             :         }
    5148                 :             : 
    5149                 :           6 :         mptcp_subflow_init();
    5150                 :           6 :         mptcp_pm_init();
    5151                 :           6 :         mptcp_sched_init();
    5152                 :           6 :         mptcp_token_init();
    5153                 :             : 
    5154            [ + ]:           6 :         if (proto_register(&mptcp_prot, 1) != 0)
    5155                 :           0 :                 panic("Failed to register MPTCP proto.\n");
    5156                 :             : 
    5157                 :           6 :         inet_register_protosw(&mptcp_protosw);
    5158                 :             : 
    5159                 :           6 :         BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
    5160                 :             : 
    5161                 :             :         /* struct mptcp_data_frag: 'overhead' corresponds to the alignment
    5162                 :             :          * (ALIGN(1, sizeof(long)) - 1, so 8-1) + the struct's size
    5163                 :             :          */
    5164                 :           6 :         BUILD_BUG_ON(ALIGN(1, sizeof(long)) - 1 + sizeof(struct mptcp_data_frag)
    5165                 :             :                      > U8_MAX);
    5166                 :           6 : }
    5167                 :             : 
    5168                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    5169                 :             : static const struct proto_ops mptcp_v6_stream_ops = {
    5170                 :             :         .family            = PF_INET6,
    5171                 :             :         .owner             = THIS_MODULE,
    5172                 :             :         .release           = inet6_release,
    5173                 :             :         .bind              = mptcp_bind,
    5174                 :             :         .connect           = inet_stream_connect,
    5175                 :             :         .socketpair        = sock_no_socketpair,
    5176                 :             :         .accept            = mptcp_stream_accept,
    5177                 :             :         .getname           = inet6_getname,
    5178                 :             :         .poll              = mptcp_poll,
    5179                 :             :         .ioctl             = inet6_ioctl,
    5180                 :             :         .gettstamp         = sock_gettstamp,
    5181                 :             :         .listen            = mptcp_listen,
    5182                 :             :         .shutdown          = inet_shutdown,
    5183                 :             :         .setsockopt        = sock_common_setsockopt,
    5184                 :             :         .getsockopt        = sock_common_getsockopt,
    5185                 :             :         .sendmsg           = inet6_sendmsg,
    5186                 :             :         .recvmsg           = inet6_recvmsg,
    5187                 :             :         .mmap              = sock_no_mmap,
    5188                 :             : #ifdef CONFIG_COMPAT
    5189                 :             :         .compat_ioctl      = inet6_compat_ioctl,
    5190                 :             : #endif
    5191                 :             :         .set_rcvlowat      = mptcp_set_rcvlowat,
    5192                 :             :         .read_sock         = mptcp_read_sock,
    5193                 :             :         .splice_read       = mptcp_splice_read,
    5194                 :             : };
    5195                 :             : 
    5196                 :             : static struct proto mptcp_v6_prot;
    5197                 :             : 
    5198                 :             : static struct inet_protosw mptcp_v6_protosw = {
    5199                 :             :         .type           = SOCK_STREAM,
    5200                 :             :         .protocol       = IPPROTO_MPTCP,
    5201                 :             :         .prot           = &mptcp_v6_prot,
    5202                 :             :         .ops            = &mptcp_v6_stream_ops,
    5203                 :             :         .flags          = INET_PROTOSW_ICSK,
    5204                 :             : };
    5205                 :             : 
    5206                 :           6 : int __init mptcp_proto_v6_init(void)
    5207                 :             : {
    5208                 :           6 :         int err;
    5209                 :             : 
    5210                 :           6 :         mptcp_subflow_v6_init();
    5211                 :             : 
    5212                 :           6 :         mptcp_v6_prot = mptcp_prot;
    5213                 :           6 :         strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
    5214                 :           6 :         mptcp_v6_prot.slab = NULL;
    5215                 :           6 :         mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
    5216                 :           6 :         mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
    5217                 :             : 
    5218                 :           6 :         err = proto_register(&mptcp_v6_prot, 1);
    5219            [ - ]:           6 :         if (err)
    5220                 :             :                 return err;
    5221                 :             : 
    5222                 :           6 :         err = inet6_register_protosw(&mptcp_v6_protosw);
    5223            [ + ]:           6 :         if (err)
    5224                 :           0 :                 proto_unregister(&mptcp_v6_prot);
    5225                 :             : 
    5226                 :             :         return err;
    5227                 :             : }
    5228                 :             : #endif
        

Generated by: LCOV version 2.3.1-1