Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 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> |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
| 13 | #include <linux/atomic.h> |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 14 | #include <net/sock.h> |
| 15 | #include <net/inet_common.h> |
| 16 | #include <net/inet_hashtables.h> |
| 17 | #include <net/protocol.h> |
| 18 | #include <net/tcp.h> |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 19 | #include <net/tcp_states.h> |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 20 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 21 | #include <net/transp_v6.h> |
| 22 | #endif |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 23 | #include <net/mptcp.h> |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 24 | #include <net/xfrm.h> |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 25 | #include "protocol.h" |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 26 | #include "mib.h" |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 27 | |
Geliang Tang | e10a989 | 2021-04-16 15:38:04 -0700 | [diff] [blame] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include <trace/events/mptcp.h> |
| 30 | |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 31 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 32 | struct mptcp6_sock { |
| 33 | struct mptcp_sock msk; |
| 34 | struct ipv6_pinfo np; |
| 35 | }; |
| 36 | #endif |
| 37 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 38 | struct mptcp_skb_cb { |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 39 | u64 map_seq; |
| 40 | u64 end_seq; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 41 | u32 offset; |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 42 | u8 has_rxtstamp:1; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) |
| 46 | |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 47 | enum { |
| 48 | MPTCP_CMSG_TS = BIT(0), |
| 49 | }; |
| 50 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 51 | static struct percpu_counter mptcp_sockets_allocated; |
| 52 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 53 | static void __mptcp_destroy_sock(struct sock *sk); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 54 | static void __mptcp_check_send_data_fin(struct sock *sk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 55 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 56 | DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); |
| 57 | static struct net_device mptcp_napi_dev; |
| 58 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 59 | /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not |
| 60 | * completed yet or has failed, return the subflow socket. |
| 61 | * Otherwise return NULL. |
| 62 | */ |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 63 | struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 64 | { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 65 | if (!msk->subflow || READ_ONCE(msk->can_ack)) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 66 | return NULL; |
| 67 | |
| 68 | return msk->subflow; |
| 69 | } |
| 70 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 71 | /* Returns end sequence number of the receiver's advertised window */ |
| 72 | static u64 mptcp_wnd_end(const struct mptcp_sock *msk) |
| 73 | { |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 74 | return READ_ONCE(msk->wnd_end); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 77 | static bool mptcp_is_tcpsk(struct sock *sk) |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 78 | { |
| 79 | struct socket *sock = sk->sk_socket; |
| 80 | |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 81 | if (unlikely(sk->sk_prot == &tcp_prot)) { |
| 82 | /* we are being invoked after mptcp_accept() has |
| 83 | * accepted a non-mp-capable flow: sk is a tcp_sk, |
| 84 | * not an mptcp one. |
| 85 | * |
| 86 | * Hand the socket over to tcp so all further socket ops |
| 87 | * bypass mptcp. |
| 88 | */ |
| 89 | sock->ops = &inet_stream_ops; |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 90 | return true; |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 91 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 92 | } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { |
| 93 | sock->ops = &inet6_stream_ops; |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 94 | return true; |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 95 | #endif |
| 96 | } |
| 97 | |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 98 | return false; |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 101 | static int __mptcp_socket_create(struct mptcp_sock *msk) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 102 | { |
| 103 | struct mptcp_subflow_context *subflow; |
| 104 | struct sock *sk = (struct sock *)msk; |
| 105 | struct socket *ssock; |
| 106 | int err; |
| 107 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 108 | err = mptcp_subflow_create_socket(sk, &ssock); |
| 109 | if (err) |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 110 | return err; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 111 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 112 | msk->first = ssock->sk; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 113 | msk->subflow = ssock; |
| 114 | subflow = mptcp_subflow_ctx(ssock->sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 115 | list_add(&subflow->node, &msk->conn_list); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 116 | sock_hold(ssock->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 117 | subflow->request_mptcp = 1; |
Paolo Abeni | 866f26f | 2021-01-20 15:39:10 +0100 | [diff] [blame] | 118 | mptcp_sock_graft(msk->first, sk->sk_socket); |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 119 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 120 | return 0; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 123 | static void mptcp_drop(struct sock *sk, struct sk_buff *skb) |
| 124 | { |
| 125 | sk_drops_add(sk, skb); |
| 126 | __kfree_skb(skb); |
| 127 | } |
| 128 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 129 | static void mptcp_rmem_charge(struct sock *sk, int size) |
| 130 | { |
| 131 | mptcp_sk(sk)->rmem_fwd_alloc -= size; |
| 132 | } |
| 133 | |
Paolo Abeni | 8268ed4 | 2020-09-14 10:01:11 +0200 | [diff] [blame] | 134 | static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, |
| 135 | struct sk_buff *from) |
| 136 | { |
| 137 | bool fragstolen; |
| 138 | int delta; |
| 139 | |
| 140 | if (MPTCP_SKB_CB(from)->offset || |
| 141 | !skb_try_coalesce(to, from, &fragstolen, &delta)) |
| 142 | return false; |
| 143 | |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 144 | pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", |
| 145 | MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, |
| 146 | to->len, MPTCP_SKB_CB(from)->end_seq); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 147 | MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; |
Paolo Abeni | 8268ed4 | 2020-09-14 10:01:11 +0200 | [diff] [blame] | 148 | kfree_skb_partial(from, fragstolen); |
| 149 | atomic_add(delta, &sk->sk_rmem_alloc); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 150 | mptcp_rmem_charge(sk, delta); |
Paolo Abeni | 8268ed4 | 2020-09-14 10:01:11 +0200 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 154 | static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, |
| 155 | struct sk_buff *from) |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 156 | { |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 157 | if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) |
| 158 | return false; |
| 159 | |
| 160 | return mptcp_try_coalesce((struct sock *)msk, to, from); |
| 161 | } |
| 162 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 163 | static void __mptcp_rmem_reclaim(struct sock *sk, int amount) |
| 164 | { |
| 165 | amount >>= SK_MEM_QUANTUM_SHIFT; |
| 166 | mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT; |
| 167 | __sk_mem_reduce_allocated(sk, amount); |
| 168 | } |
| 169 | |
| 170 | static void mptcp_rmem_uncharge(struct sock *sk, int size) |
| 171 | { |
| 172 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 173 | int reclaimable; |
| 174 | |
| 175 | msk->rmem_fwd_alloc += size; |
| 176 | reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); |
| 177 | |
| 178 | /* see sk_mem_uncharge() for the rationale behind the following schema */ |
| 179 | if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD)) |
| 180 | __mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK); |
| 181 | } |
| 182 | |
| 183 | static void mptcp_rfree(struct sk_buff *skb) |
| 184 | { |
| 185 | unsigned int len = skb->truesize; |
| 186 | struct sock *sk = skb->sk; |
| 187 | |
| 188 | atomic_sub(len, &sk->sk_rmem_alloc); |
| 189 | mptcp_rmem_uncharge(sk, len); |
| 190 | } |
| 191 | |
| 192 | static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) |
| 193 | { |
| 194 | skb_orphan(skb); |
| 195 | skb->sk = sk; |
| 196 | skb->destructor = mptcp_rfree; |
| 197 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 198 | mptcp_rmem_charge(sk, skb->truesize); |
| 199 | } |
| 200 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 201 | /* "inspired" by tcp_data_queue_ofo(), main differences: |
| 202 | * - use mptcp seqs |
| 203 | * - don't cope with sacks |
| 204 | */ |
| 205 | static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) |
| 206 | { |
| 207 | struct sock *sk = (struct sock *)msk; |
| 208 | struct rb_node **p, *parent; |
| 209 | u64 seq, end_seq, max_seq; |
| 210 | struct sk_buff *skb1; |
| 211 | |
| 212 | seq = MPTCP_SKB_CB(skb)->map_seq; |
| 213 | end_seq = MPTCP_SKB_CB(skb)->end_seq; |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 214 | max_seq = READ_ONCE(msk->rcv_wnd_sent); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 215 | |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 216 | pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, |
| 217 | RB_EMPTY_ROOT(&msk->out_of_order_queue)); |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 218 | if (after64(end_seq, max_seq)) { |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 219 | /* out of window */ |
| 220 | mptcp_drop(sk, skb); |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 221 | pr_debug("oow by %lld, rcv_wnd_sent %llu\n", |
| 222 | (unsigned long long)end_seq - (unsigned long)max_seq, |
| 223 | (unsigned long long)msk->rcv_wnd_sent); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 224 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
| 228 | p = &msk->out_of_order_queue.rb_node; |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 229 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 230 | if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { |
| 231 | rb_link_node(&skb->rbnode, NULL, p); |
| 232 | rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); |
| 233 | msk->ooo_last_skb = skb; |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | /* with 2 subflows, adding at end of ooo queue is quite likely |
| 238 | * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. |
| 239 | */ |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 240 | if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { |
| 241 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); |
| 242 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 243 | return; |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 244 | } |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 245 | |
| 246 | /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ |
| 247 | if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 248 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 249 | parent = &msk->ooo_last_skb->rbnode; |
| 250 | p = &parent->rb_right; |
| 251 | goto insert; |
| 252 | } |
| 253 | |
| 254 | /* Find place to insert this segment. Handle overlaps on the way. */ |
| 255 | parent = NULL; |
| 256 | while (*p) { |
| 257 | parent = *p; |
| 258 | skb1 = rb_to_skb(parent); |
| 259 | if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { |
| 260 | p = &parent->rb_left; |
| 261 | continue; |
| 262 | } |
| 263 | if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { |
| 264 | if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { |
| 265 | /* All the bits are present. Drop. */ |
| 266 | mptcp_drop(sk, skb); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 267 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { |
| 271 | /* partial overlap: |
| 272 | * | skb | |
| 273 | * | skb1 | |
| 274 | * continue traversing |
| 275 | */ |
| 276 | } else { |
| 277 | /* skb's seq == skb1's seq and skb covers skb1. |
| 278 | * Replace skb1 with skb. |
| 279 | */ |
| 280 | rb_replace_node(&skb1->rbnode, &skb->rbnode, |
| 281 | &msk->out_of_order_queue); |
| 282 | mptcp_drop(sk, skb1); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 283 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 284 | goto merge_right; |
| 285 | } |
| 286 | } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 287 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | p = &parent->rb_right; |
| 291 | } |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 292 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 293 | insert: |
| 294 | /* Insert segment into RB tree. */ |
| 295 | rb_link_node(&skb->rbnode, parent, p); |
| 296 | rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); |
| 297 | |
| 298 | merge_right: |
| 299 | /* Remove other segments covered by skb. */ |
| 300 | while ((skb1 = skb_rb_next(skb)) != NULL) { |
| 301 | if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) |
| 302 | break; |
| 303 | rb_erase(&skb1->rbnode, &msk->out_of_order_queue); |
| 304 | mptcp_drop(sk, skb1); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 305 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 306 | } |
| 307 | /* If there is no skb after us, we are the last_skb ! */ |
| 308 | if (!skb1) |
| 309 | msk->ooo_last_skb = skb; |
| 310 | |
| 311 | end: |
| 312 | skb_condense(skb); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 313 | mptcp_set_owner_r(skb, sk); |
| 314 | } |
| 315 | |
| 316 | static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) |
| 317 | { |
| 318 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 319 | int amt, amount; |
| 320 | |
| 321 | if (size < msk->rmem_fwd_alloc) |
| 322 | return true; |
| 323 | |
| 324 | amt = sk_mem_pages(size); |
| 325 | amount = amt << SK_MEM_QUANTUM_SHIFT; |
| 326 | msk->rmem_fwd_alloc += amount; |
| 327 | if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) { |
| 328 | if (ssk->sk_forward_alloc < amount) { |
| 329 | msk->rmem_fwd_alloc -= amount; |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | ssk->sk_forward_alloc -= amount; |
| 334 | } |
| 335 | return true; |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, |
| 339 | struct sk_buff *skb, unsigned int offset, |
| 340 | size_t copy_len) |
| 341 | { |
| 342 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 343 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 344 | struct sk_buff *tail; |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 345 | bool has_rxtstamp; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 346 | |
| 347 | __skb_unlink(skb, &ssk->sk_receive_queue); |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 348 | |
| 349 | skb_ext_reset(skb); |
| 350 | skb_orphan(skb); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 351 | |
Paolo Abeni | 9c3f94e | 2020-10-27 15:59:14 +0100 | [diff] [blame] | 352 | /* try to fetch required memory from subflow */ |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 353 | if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) |
| 354 | goto drop; |
Paolo Abeni | 9c3f94e | 2020-10-27 15:59:14 +0100 | [diff] [blame] | 355 | |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 356 | has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; |
| 357 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 358 | /* the skb map_seq accounts for the skb offset: |
| 359 | * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq |
| 360 | * value |
| 361 | */ |
| 362 | MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); |
| 363 | MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; |
Paolo Abeni | 8268ed4 | 2020-09-14 10:01:11 +0200 | [diff] [blame] | 364 | MPTCP_SKB_CB(skb)->offset = offset; |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 365 | MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 366 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 367 | if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { |
| 368 | /* in sequence */ |
David S. Miller | 8b0308f | 2020-10-05 17:33:26 -0700 | [diff] [blame] | 369 | WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 370 | tail = skb_peek_tail(&sk->sk_receive_queue); |
| 371 | if (tail && mptcp_try_coalesce(sk, tail, skb)) |
| 372 | return true; |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 373 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 374 | mptcp_set_owner_r(skb, sk); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 375 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 376 | return true; |
| 377 | } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { |
| 378 | mptcp_data_queue_ofo(msk, skb); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | /* old data, keep it simple and drop the whole pkt, sender |
| 383 | * will retransmit as needed, if needed. |
| 384 | */ |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 385 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | 9c3f94e | 2020-10-27 15:59:14 +0100 | [diff] [blame] | 386 | drop: |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 387 | mptcp_drop(sk, skb); |
| 388 | return false; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 391 | static void mptcp_stop_timer(struct sock *sk) |
| 392 | { |
| 393 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 394 | |
| 395 | sk_stop_timer(sk, &icsk->icsk_retransmit_timer); |
| 396 | mptcp_sk(sk)->timer_ival = 0; |
| 397 | } |
| 398 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 399 | static void mptcp_close_wake_up(struct sock *sk) |
| 400 | { |
| 401 | if (sock_flag(sk, SOCK_DEAD)) |
| 402 | return; |
| 403 | |
| 404 | sk->sk_state_change(sk); |
| 405 | if (sk->sk_shutdown == SHUTDOWN_MASK || |
| 406 | sk->sk_state == TCP_CLOSE) |
| 407 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); |
| 408 | else |
| 409 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
| 410 | } |
| 411 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 412 | static bool mptcp_pending_data_fin_ack(struct sock *sk) |
| 413 | { |
| 414 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 415 | |
| 416 | return !__mptcp_check_fallback(msk) && |
| 417 | ((1 << sk->sk_state) & |
| 418 | (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && |
| 419 | msk->write_seq == READ_ONCE(msk->snd_una); |
| 420 | } |
| 421 | |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 422 | static void mptcp_check_data_fin_ack(struct sock *sk) |
| 423 | { |
| 424 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 425 | |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 426 | /* Look for an acknowledged DATA_FIN */ |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 427 | if (mptcp_pending_data_fin_ack(sk)) { |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 428 | WRITE_ONCE(msk->snd_data_fin_enable, 0); |
| 429 | |
| 430 | switch (sk->sk_state) { |
| 431 | case TCP_FIN_WAIT1: |
| 432 | inet_sk_state_store(sk, TCP_FIN_WAIT2); |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 433 | break; |
| 434 | case TCP_CLOSING: |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 435 | case TCP_LAST_ACK: |
| 436 | inet_sk_state_store(sk, TCP_CLOSE); |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 440 | mptcp_close_wake_up(sk); |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 444 | static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) |
| 445 | { |
| 446 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 447 | |
| 448 | if (READ_ONCE(msk->rcv_data_fin) && |
| 449 | ((1 << sk->sk_state) & |
| 450 | (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { |
| 451 | u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); |
| 452 | |
| 453 | if (msk->ack_seq == rcv_data_fin_seq) { |
| 454 | if (seq) |
| 455 | *seq = rcv_data_fin_seq; |
| 456 | |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
Mat Martineau | 6477dd3 | 2021-04-23 09:40:33 -0700 | [diff] [blame] | 464 | static void mptcp_set_datafin_timeout(const struct sock *sk) |
| 465 | { |
| 466 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 467 | |
| 468 | mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX, |
| 469 | TCP_RTO_MIN << icsk->icsk_retransmits); |
| 470 | } |
| 471 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 472 | static void __mptcp_set_timeout(struct sock *sk, long tout) |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 473 | { |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 474 | mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; |
| 475 | } |
| 476 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 477 | static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) |
| 478 | { |
| 479 | const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 480 | |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 481 | return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? |
| 482 | inet_csk(ssk)->icsk_timeout - jiffies : 0; |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void mptcp_set_timeout(struct sock *sk) |
| 486 | { |
| 487 | struct mptcp_subflow_context *subflow; |
| 488 | long tout = 0; |
| 489 | |
| 490 | mptcp_for_each_subflow(mptcp_sk(sk), subflow) |
| 491 | tout = max(tout, mptcp_timeout_from_subflow(subflow)); |
| 492 | __mptcp_set_timeout(sk, tout); |
| 493 | } |
| 494 | |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 495 | static bool tcp_can_send_ack(const struct sock *ssk) |
| 496 | { |
| 497 | return !((1 << inet_sk_state_load(ssk)) & |
Paolo Abeni | 20bc80b | 2021-01-12 18:25:23 +0100 | [diff] [blame] | 498 | (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Mat Martineau | 340fa66 | 2021-09-02 11:51:19 -0700 | [diff] [blame] | 501 | void mptcp_subflow_send_ack(struct sock *ssk) |
| 502 | { |
| 503 | bool slow; |
| 504 | |
| 505 | slow = lock_sock_fast(ssk); |
| 506 | if (tcp_can_send_ack(ssk)) |
| 507 | tcp_send_ack(ssk); |
| 508 | unlock_sock_fast(ssk, slow); |
| 509 | } |
| 510 | |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 511 | static void mptcp_send_ack(struct mptcp_sock *msk) |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 512 | { |
| 513 | struct mptcp_subflow_context *subflow; |
| 514 | |
Mat Martineau | 340fa66 | 2021-09-02 11:51:19 -0700 | [diff] [blame] | 515 | mptcp_for_each_subflow(msk, subflow) |
| 516 | mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 517 | } |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 518 | |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 519 | static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 520 | { |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 521 | bool slow; |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 522 | |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 523 | slow = lock_sock_fast(ssk); |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 524 | if (tcp_can_send_ack(ssk)) |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 525 | tcp_cleanup_rbuf(ssk, 1); |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 526 | unlock_sock_fast(ssk, slow); |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) |
| 530 | { |
| 531 | const struct inet_connection_sock *icsk = inet_csk(ssk); |
Matthieu Baerts | c4512c6 | 2021-06-25 14:25:22 -0700 | [diff] [blame] | 532 | u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 533 | const struct tcp_sock *tp = tcp_sk(ssk); |
| 534 | |
| 535 | return (ack_pending & ICSK_ACK_SCHED) && |
| 536 | ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > |
| 537 | READ_ONCE(icsk->icsk_ack.rcv_mss)) || |
| 538 | (rx_empty && ack_pending & |
| 539 | (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) |
| 543 | { |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 544 | int old_space = READ_ONCE(msk->old_wspace); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 545 | struct mptcp_subflow_context *subflow; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 546 | struct sock *sk = (struct sock *)msk; |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 547 | int space = __mptcp_space(sk); |
| 548 | bool cleanup, rx_empty; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 549 | |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 550 | cleanup = (space > 0) && (space >= (old_space << 1)); |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 551 | rx_empty = !__mptcp_rmem(sk); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 552 | |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 553 | mptcp_for_each_subflow(msk, subflow) { |
| 554 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 555 | |
Paolo Abeni | fde56ee | 2021-06-22 12:25:23 -0700 | [diff] [blame] | 556 | if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) |
| 557 | mptcp_subflow_cleanup_rbuf(ssk); |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 558 | } |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static bool mptcp_check_data_fin(struct sock *sk) |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 562 | { |
| 563 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 564 | u64 rcv_data_fin_seq; |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 565 | bool ret = false; |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 566 | |
Paolo Abeni | 781bf13 | 2021-04-01 16:19:43 -0700 | [diff] [blame] | 567 | if (__mptcp_check_fallback(msk)) |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 568 | return ret; |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 569 | |
| 570 | /* Need to ack a DATA_FIN received from a peer while this side |
| 571 | * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. |
| 572 | * msk->rcv_data_fin was set when parsing the incoming options |
| 573 | * at the subflow level and the msk lock was not held, so this |
| 574 | * is the first opportunity to act on the DATA_FIN and change |
| 575 | * the msk state. |
| 576 | * |
| 577 | * If we are caught up to the sequence number of the incoming |
| 578 | * DATA_FIN, send the DATA_ACK now and do state transition. If |
| 579 | * not caught up, do nothing and let the recv code send DATA_ACK |
| 580 | * when catching up. |
| 581 | */ |
| 582 | |
| 583 | if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { |
Mat Martineau | 917944d | 2020-09-29 15:08:19 -0700 | [diff] [blame] | 584 | WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 585 | WRITE_ONCE(msk->rcv_data_fin, 0); |
| 586 | |
| 587 | sk->sk_shutdown |= RCV_SHUTDOWN; |
Mat Martineau | 16a9a9d | 2020-07-28 15:12:05 -0700 | [diff] [blame] | 588 | smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 589 | |
| 590 | switch (sk->sk_state) { |
| 591 | case TCP_ESTABLISHED: |
| 592 | inet_sk_state_store(sk, TCP_CLOSE_WAIT); |
| 593 | break; |
| 594 | case TCP_FIN_WAIT1: |
| 595 | inet_sk_state_store(sk, TCP_CLOSING); |
| 596 | break; |
| 597 | case TCP_FIN_WAIT2: |
| 598 | inet_sk_state_store(sk, TCP_CLOSE); |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 599 | break; |
| 600 | default: |
| 601 | /* Other states not expected */ |
| 602 | WARN_ON_ONCE(1); |
| 603 | break; |
| 604 | } |
| 605 | |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 606 | ret = true; |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 607 | mptcp_send_ack(msk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 608 | mptcp_close_wake_up(sk); |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 609 | } |
Paolo Abeni | 7ed9080 | 2020-11-16 10:48:14 +0100 | [diff] [blame] | 610 | return ret; |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 613 | static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, |
| 614 | struct sock *ssk, |
| 615 | unsigned int *bytes) |
| 616 | { |
| 617 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 618 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 619 | unsigned int moved = 0; |
| 620 | bool more_data_avail; |
| 621 | struct tcp_sock *tp; |
| 622 | bool done = false; |
Florian Westphal | 13c7ba0 | 2020-11-03 11:05:03 -0800 | [diff] [blame] | 623 | int sk_rbuf; |
| 624 | |
| 625 | sk_rbuf = READ_ONCE(sk->sk_rcvbuf); |
| 626 | |
| 627 | if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { |
| 628 | int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); |
| 629 | |
| 630 | if (unlikely(ssk_rbuf > sk_rbuf)) { |
| 631 | WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); |
| 632 | sk_rbuf = ssk_rbuf; |
| 633 | } |
| 634 | } |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 635 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 636 | pr_debug("msk=%p ssk=%p", msk, ssk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 637 | tp = tcp_sk(ssk); |
| 638 | do { |
| 639 | u32 map_remaining, offset; |
| 640 | u32 seq = tp->copied_seq; |
| 641 | struct sk_buff *skb; |
| 642 | bool fin; |
| 643 | |
| 644 | /* try to move as much data as available */ |
| 645 | map_remaining = subflow->map_data_len - |
| 646 | mptcp_subflow_get_map_offset(subflow); |
| 647 | |
| 648 | skb = skb_peek(&ssk->sk_receive_queue); |
Paolo Abeni | d9fb8c5 | 2020-10-06 08:27:34 +0200 | [diff] [blame] | 649 | if (!skb) { |
| 650 | /* if no data is found, a racing workqueue/recvmsg |
| 651 | * already processed the new data, stop here or we |
| 652 | * can enter an infinite loop |
| 653 | */ |
| 654 | if (!moved) |
| 655 | done = true; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 656 | break; |
Paolo Abeni | d9fb8c5 | 2020-10-06 08:27:34 +0200 | [diff] [blame] | 657 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 658 | |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 659 | if (__mptcp_check_fallback(msk)) { |
| 660 | /* if we are running under the workqueue, TCP could have |
| 661 | * collapsed skbs between dummy map creation and now |
| 662 | * be sure to adjust the size |
| 663 | */ |
| 664 | map_remaining = skb->len; |
| 665 | subflow->map_data_len = skb->len; |
| 666 | } |
| 667 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 668 | offset = seq - TCP_SKB_CB(skb)->seq; |
| 669 | fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; |
| 670 | if (fin) { |
| 671 | done = true; |
| 672 | seq++; |
| 673 | } |
| 674 | |
| 675 | if (offset < skb->len) { |
| 676 | size_t len = skb->len - offset; |
| 677 | |
| 678 | if (tp->urg_data) |
| 679 | done = true; |
| 680 | |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 681 | if (__mptcp_move_skb(msk, ssk, skb, offset, len)) |
| 682 | moved += len; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 683 | seq += len; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 684 | |
| 685 | if (WARN_ON_ONCE(map_remaining < len)) |
| 686 | break; |
| 687 | } else { |
| 688 | WARN_ON_ONCE(!fin); |
| 689 | sk_eat_skb(ssk, skb); |
| 690 | done = true; |
| 691 | } |
| 692 | |
| 693 | WRITE_ONCE(tp->copied_seq, seq); |
| 694 | more_data_avail = mptcp_subflow_data_available(ssk); |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 695 | |
Florian Westphal | 13c7ba0 | 2020-11-03 11:05:03 -0800 | [diff] [blame] | 696 | if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 697 | done = true; |
| 698 | break; |
| 699 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 700 | } while (more_data_avail); |
| 701 | |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 702 | *bytes += moved; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 703 | return done; |
| 704 | } |
| 705 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 706 | static bool __mptcp_ofo_queue(struct mptcp_sock *msk) |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 707 | { |
| 708 | struct sock *sk = (struct sock *)msk; |
| 709 | struct sk_buff *skb, *tail; |
| 710 | bool moved = false; |
| 711 | struct rb_node *p; |
| 712 | u64 end_seq; |
| 713 | |
| 714 | p = rb_first(&msk->out_of_order_queue); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 715 | pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 716 | while (p) { |
| 717 | skb = rb_to_skb(p); |
| 718 | if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) |
| 719 | break; |
| 720 | |
| 721 | p = rb_next(p); |
| 722 | rb_erase(&skb->rbnode, &msk->out_of_order_queue); |
| 723 | |
| 724 | if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, |
| 725 | msk->ack_seq))) { |
| 726 | mptcp_drop(sk, skb); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 727 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 728 | continue; |
| 729 | } |
| 730 | |
| 731 | end_seq = MPTCP_SKB_CB(skb)->end_seq; |
| 732 | tail = skb_peek_tail(&sk->sk_receive_queue); |
| 733 | if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { |
| 734 | int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; |
| 735 | |
| 736 | /* skip overlapping data, if any */ |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 737 | pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", |
| 738 | MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, |
| 739 | delta); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 740 | MPTCP_SKB_CB(skb)->offset += delta; |
| 741 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 742 | } |
| 743 | msk->ack_seq = end_seq; |
| 744 | moved = true; |
| 745 | } |
| 746 | return moved; |
| 747 | } |
| 748 | |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 749 | /* In most cases we will be able to lock the mptcp socket. If its already |
| 750 | * owned, we need to defer to the work queue to avoid ABBA deadlock. |
| 751 | */ |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 752 | static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 753 | { |
| 754 | struct sock *sk = (struct sock *)msk; |
| 755 | unsigned int moved = 0; |
| 756 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 757 | __mptcp_move_skbs_from_subflow(msk, ssk, &moved); |
| 758 | __mptcp_ofo_queue(msk); |
Paolo Abeni | 499ada5 | 2021-06-10 15:59:44 -0700 | [diff] [blame] | 759 | if (unlikely(ssk->sk_err)) { |
| 760 | if (!sock_owned_by_user(sk)) |
| 761 | __mptcp_error_report(sk); |
| 762 | else |
| 763 | set_bit(MPTCP_ERROR_REPORT, &msk->flags); |
| 764 | } |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 765 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 766 | /* If the moves have caught up with the DATA_FIN sequence number |
| 767 | * it's time to ack the DATA_FIN and change socket state, but |
| 768 | * this is not a good place to change state. Let the workqueue |
| 769 | * do it. |
| 770 | */ |
| 771 | if (mptcp_pending_data_fin(sk, NULL)) |
| 772 | mptcp_schedule_work(sk); |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 773 | return moved > 0; |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void mptcp_data_ready(struct sock *sk, struct sock *ssk) |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 777 | { |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 778 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 779 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | 13c7ba0 | 2020-11-03 11:05:03 -0800 | [diff] [blame] | 780 | int sk_rbuf, ssk_rbuf; |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 781 | |
Paolo Abeni | d7b1bfd | 2020-12-09 12:03:31 +0100 | [diff] [blame] | 782 | /* The peer can send data while we are shutting down this |
| 783 | * subflow at msk destruction time, but we must avoid enqueuing |
| 784 | * more data to the msk receive queue |
| 785 | */ |
| 786 | if (unlikely(subflow->disposable)) |
| 787 | return; |
| 788 | |
Florian Westphal | 13c7ba0 | 2020-11-03 11:05:03 -0800 | [diff] [blame] | 789 | ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); |
| 790 | sk_rbuf = READ_ONCE(sk->sk_rcvbuf); |
| 791 | if (unlikely(ssk_rbuf > sk_rbuf)) |
| 792 | sk_rbuf = ssk_rbuf; |
| 793 | |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 794 | /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 795 | if (__mptcp_rmem(sk) > sk_rbuf) { |
| 796 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 797 | return; |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 798 | } |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 799 | |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 800 | /* Wake-up the reader only for in-sequence data */ |
| 801 | mptcp_data_lock(sk); |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 802 | if (move_skbs_to_msk(msk, ssk)) |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 803 | sk->sk_data_ready(sk); |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 804 | |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 805 | mptcp_data_unlock(sk); |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 808 | static bool mptcp_do_flush_join_list(struct mptcp_sock *msk) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 809 | { |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 810 | struct mptcp_subflow_context *subflow; |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 811 | bool ret = false; |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 812 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 813 | if (likely(list_empty(&msk->join_list))) |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 814 | return false; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 815 | |
| 816 | spin_lock_bh(&msk->join_list_lock); |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 817 | list_for_each_entry(subflow, &msk->join_list, node) { |
| 818 | u32 sseq = READ_ONCE(subflow->setsockopt_seq); |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 819 | |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 820 | mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow)); |
| 821 | if (READ_ONCE(msk->setsockopt_seq) != sseq) |
| 822 | ret = true; |
| 823 | } |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 824 | list_splice_tail_init(&msk->join_list, &msk->conn_list); |
| 825 | spin_unlock_bh(&msk->join_list_lock); |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 826 | |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 827 | return ret; |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void __mptcp_flush_join_list(struct mptcp_sock *msk) |
| 831 | { |
| 832 | if (likely(!mptcp_do_flush_join_list(msk))) |
| 833 | return; |
| 834 | |
| 835 | if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags)) |
| 836 | mptcp_schedule_work((struct sock *)msk); |
| 837 | } |
| 838 | |
| 839 | static void mptcp_flush_join_list(struct mptcp_sock *msk) |
| 840 | { |
| 841 | bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags); |
| 842 | |
| 843 | might_sleep(); |
| 844 | |
| 845 | if (!mptcp_do_flush_join_list(msk) && !sync_needed) |
| 846 | return; |
| 847 | |
| 848 | mptcp_sockopt_sync_all(msk); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 851 | static bool mptcp_timer_pending(struct sock *sk) |
| 852 | { |
| 853 | return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); |
| 854 | } |
| 855 | |
| 856 | static void mptcp_reset_timer(struct sock *sk) |
| 857 | { |
| 858 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 859 | unsigned long tout; |
| 860 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 861 | /* prevent rescheduling on close */ |
| 862 | if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) |
| 863 | return; |
| 864 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 865 | tout = mptcp_sk(sk)->timer_ival; |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 866 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); |
| 867 | } |
| 868 | |
Paolo Abeni | ba8f48f | 2020-11-16 10:48:05 +0100 | [diff] [blame] | 869 | bool mptcp_schedule_work(struct sock *sk) |
| 870 | { |
| 871 | if (inet_sk_state_load(sk) != TCP_CLOSE && |
| 872 | schedule_work(&mptcp_sk(sk)->work)) { |
| 873 | /* each subflow already holds a reference to the sk, and the |
| 874 | * workqueue is invoked by a subflow, so sk can't go away here. |
| 875 | */ |
| 876 | sock_hold(sk); |
| 877 | return true; |
| 878 | } |
| 879 | return false; |
| 880 | } |
| 881 | |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 882 | void mptcp_subflow_eof(struct sock *sk) |
| 883 | { |
Paolo Abeni | ba8f48f | 2020-11-16 10:48:05 +0100 | [diff] [blame] | 884 | if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) |
| 885 | mptcp_schedule_work(sk); |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 888 | static void mptcp_check_for_eof(struct mptcp_sock *msk) |
| 889 | { |
| 890 | struct mptcp_subflow_context *subflow; |
| 891 | struct sock *sk = (struct sock *)msk; |
| 892 | int receivers = 0; |
| 893 | |
| 894 | mptcp_for_each_subflow(msk, subflow) |
| 895 | receivers += !subflow->rx_eof; |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 896 | if (receivers) |
| 897 | return; |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 898 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 899 | if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 900 | /* hopefully temporary hack: propagate shutdown status |
| 901 | * to msk, when all subflows agree on it |
| 902 | */ |
| 903 | sk->sk_shutdown |= RCV_SHUTDOWN; |
| 904 | |
| 905 | smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 906 | sk->sk_data_ready(sk); |
| 907 | } |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 908 | |
| 909 | switch (sk->sk_state) { |
| 910 | case TCP_ESTABLISHED: |
| 911 | inet_sk_state_store(sk, TCP_CLOSE_WAIT); |
| 912 | break; |
| 913 | case TCP_FIN_WAIT1: |
Paolo Abeni | 26aa231 | 2020-11-19 11:45:55 -0800 | [diff] [blame] | 914 | inet_sk_state_store(sk, TCP_CLOSING); |
| 915 | break; |
| 916 | case TCP_FIN_WAIT2: |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 917 | inet_sk_state_store(sk, TCP_CLOSE); |
| 918 | break; |
| 919 | default: |
| 920 | return; |
| 921 | } |
| 922 | mptcp_close_wake_up(sk); |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 925 | static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) |
| 926 | { |
| 927 | struct mptcp_subflow_context *subflow; |
| 928 | struct sock *sk = (struct sock *)msk; |
| 929 | |
| 930 | sock_owned_by_me(sk); |
| 931 | |
| 932 | mptcp_for_each_subflow(msk, subflow) { |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 933 | if (READ_ONCE(subflow->data_avail)) |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 934 | return mptcp_subflow_tcp_sock(subflow); |
| 935 | } |
| 936 | |
| 937 | return NULL; |
| 938 | } |
| 939 | |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 940 | static bool mptcp_skb_can_collapse_to(u64 write_seq, |
| 941 | const struct sk_buff *skb, |
| 942 | const struct mptcp_ext *mpext) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 943 | { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 944 | if (!tcp_skb_can_collapse_to(skb)) |
| 945 | return false; |
| 946 | |
Paolo Abeni | 5a369ca | 2020-11-03 11:05:05 -0800 | [diff] [blame] | 947 | /* can collapse only if MPTCP level sequence is in order and this |
| 948 | * mapping has not been xmitted yet |
| 949 | */ |
| 950 | return mpext && mpext->data_seq + mpext->data_len == write_seq && |
| 951 | !mpext->frozen; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Paolo Abeni | 29249ea | 2021-05-11 19:13:51 +0200 | [diff] [blame] | 954 | /* we can append data to the given data frag if: |
| 955 | * - there is space available in the backing page_frag |
| 956 | * - the data frag tail matches the current page_frag free offset |
| 957 | * - the data frag end sequence number matches the current write seq |
| 958 | */ |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 959 | static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, |
| 960 | const struct page_frag *pfrag, |
| 961 | const struct mptcp_data_frag *df) |
| 962 | { |
| 963 | return df && pfrag->page == df->page && |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 964 | pfrag->size - pfrag->offset > 0 && |
Paolo Abeni | 29249ea | 2021-05-11 19:13:51 +0200 | [diff] [blame] | 965 | pfrag->offset == (df->offset + df->data_len) && |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 966 | df->data_seq + df->data_len == msk->write_seq; |
| 967 | } |
| 968 | |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 969 | static void __mptcp_mem_reclaim_partial(struct sock *sk) |
| 970 | { |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 971 | int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk); |
| 972 | |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 973 | lockdep_assert_held_once(&sk->sk_lock.slock); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 974 | |
| 975 | __mptcp_rmem_reclaim(sk, reclaimable - 1); |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 976 | sk_mem_reclaim_partial(sk); |
| 977 | } |
| 978 | |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 979 | static void mptcp_mem_reclaim_partial(struct sock *sk) |
| 980 | { |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 981 | mptcp_data_lock(sk); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 982 | __mptcp_mem_reclaim_partial(sk); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 983 | mptcp_data_unlock(sk); |
| 984 | } |
| 985 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 986 | static void dfrag_uncharge(struct sock *sk, int len) |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 987 | { |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 988 | sk_mem_uncharge(sk, len); |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 989 | sk_wmem_queued_add(sk, -len); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) |
| 993 | { |
| 994 | int len = dfrag->data_len + dfrag->overhead; |
| 995 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 996 | list_del(&dfrag->list); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 997 | dfrag_uncharge(sk, len); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 998 | put_page(dfrag->page); |
| 999 | } |
| 1000 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1001 | static void __mptcp_clean_una(struct sock *sk) |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1002 | { |
| 1003 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1004 | struct mptcp_data_frag *dtmp, *dfrag; |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1005 | bool cleaned = false; |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 1006 | u64 snd_una; |
| 1007 | |
| 1008 | /* on fallback we just need to ignore snd_una, as this is really |
| 1009 | * plain TCP |
| 1010 | */ |
| 1011 | if (__mptcp_check_fallback(msk)) |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1012 | msk->snd_una = READ_ONCE(msk->snd_nxt); |
| 1013 | |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1014 | snd_una = msk->snd_una; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1015 | list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { |
| 1016 | if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) |
| 1017 | break; |
| 1018 | |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 1019 | if (unlikely(dfrag == msk->first_pending)) { |
| 1020 | /* in recovery mode can see ack after the current snd head */ |
| 1021 | if (WARN_ON_ONCE(!msk->recovery)) |
| 1022 | break; |
| 1023 | |
| 1024 | WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); |
| 1025 | } |
| 1026 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1027 | dfrag_clear(sk, dfrag); |
| 1028 | cleaned = true; |
| 1029 | } |
| 1030 | |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 1031 | dfrag = mptcp_rtx_head(sk); |
| 1032 | if (dfrag && after64(snd_una, dfrag->data_seq)) { |
Paolo Abeni | 53eb4c3 | 2020-07-23 13:02:30 +0200 | [diff] [blame] | 1033 | u64 delta = snd_una - dfrag->data_seq; |
| 1034 | |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 1035 | /* prevent wrap around in recovery mode */ |
| 1036 | if (unlikely(delta > dfrag->already_sent)) { |
| 1037 | if (WARN_ON_ONCE(!msk->recovery)) |
| 1038 | goto out; |
| 1039 | if (WARN_ON_ONCE(delta > dfrag->data_len)) |
| 1040 | goto out; |
| 1041 | dfrag->already_sent += delta - dfrag->already_sent; |
| 1042 | } |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 1043 | |
| 1044 | dfrag->data_seq += delta; |
Paolo Abeni | 53eb4c3 | 2020-07-23 13:02:30 +0200 | [diff] [blame] | 1045 | dfrag->offset += delta; |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 1046 | dfrag->data_len -= delta; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1047 | dfrag->already_sent -= delta; |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 1048 | |
| 1049 | dfrag_uncharge(sk, delta); |
| 1050 | cleaned = true; |
| 1051 | } |
| 1052 | |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 1053 | /* all retransmitted data acked, recovery completed */ |
| 1054 | if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) |
| 1055 | msk->recovery = false; |
| 1056 | |
Paolo Abeni | 53eb4c3 | 2020-07-23 13:02:30 +0200 | [diff] [blame] | 1057 | out: |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1058 | if (cleaned && tcp_under_memory_pressure(sk)) |
| 1059 | __mptcp_mem_reclaim_partial(sk); |
Paolo Abeni | 63561a4 | 2020-09-14 10:01:07 +0200 | [diff] [blame] | 1060 | |
Florian Westphal | 3241a9c | 2021-09-24 14:12:38 -0700 | [diff] [blame] | 1061 | if (snd_una == READ_ONCE(msk->snd_nxt) && |
| 1062 | snd_una == READ_ONCE(msk->write_seq)) { |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1063 | if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1064 | mptcp_stop_timer(sk); |
| 1065 | } else { |
| 1066 | mptcp_reset_timer(sk); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Paolo Abeni | 417789d | 2021-03-04 13:32:15 -0800 | [diff] [blame] | 1070 | static void __mptcp_clean_una_wakeup(struct sock *sk) |
| 1071 | { |
Paolo Abeni | 765ff42 | 2021-09-24 14:12:36 -0700 | [diff] [blame] | 1072 | lockdep_assert_held_once(&sk->sk_lock.slock); |
| 1073 | |
Paolo Abeni | 417789d | 2021-03-04 13:32:15 -0800 | [diff] [blame] | 1074 | __mptcp_clean_una(sk); |
| 1075 | mptcp_write_space(sk); |
| 1076 | } |
| 1077 | |
Paolo Abeni | b5941f0 | 2021-05-27 16:31:37 -0700 | [diff] [blame] | 1078 | static void mptcp_clean_una_wakeup(struct sock *sk) |
| 1079 | { |
| 1080 | mptcp_data_lock(sk); |
| 1081 | __mptcp_clean_una_wakeup(sk); |
| 1082 | mptcp_data_unlock(sk); |
| 1083 | } |
| 1084 | |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1085 | static void mptcp_enter_memory_pressure(struct sock *sk) |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1086 | { |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1087 | struct mptcp_subflow_context *subflow; |
| 1088 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1089 | bool first = true; |
| 1090 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1091 | sk_stream_moderate_sndbuf(sk); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1092 | mptcp_for_each_subflow(msk, subflow) { |
| 1093 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 1094 | |
| 1095 | if (first) |
| 1096 | tcp_enter_memory_pressure(ssk); |
| 1097 | sk_stream_moderate_sndbuf(ssk); |
| 1098 | first = false; |
| 1099 | } |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* ensure we get enough memory for the frag hdr, beyond some minimal amount of |
| 1103 | * data |
| 1104 | */ |
| 1105 | static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) |
| 1106 | { |
| 1107 | if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), |
| 1108 | pfrag, sk->sk_allocation))) |
| 1109 | return true; |
| 1110 | |
| 1111 | mptcp_enter_memory_pressure(sk); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | static struct mptcp_data_frag * |
| 1116 | mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, |
| 1117 | int orig_offset) |
| 1118 | { |
| 1119 | int offset = ALIGN(orig_offset, sizeof(long)); |
| 1120 | struct mptcp_data_frag *dfrag; |
| 1121 | |
| 1122 | dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); |
| 1123 | dfrag->data_len = 0; |
| 1124 | dfrag->data_seq = msk->write_seq; |
| 1125 | dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); |
| 1126 | dfrag->offset = offset + sizeof(struct mptcp_data_frag); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1127 | dfrag->already_sent = 0; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1128 | dfrag->page = pfrag->page; |
| 1129 | |
| 1130 | return dfrag; |
| 1131 | } |
| 1132 | |
Paolo Abeni | caf971d | 2020-11-16 10:48:06 +0100 | [diff] [blame] | 1133 | struct mptcp_sendmsg_info { |
| 1134 | int mss_now; |
| 1135 | int size_goal; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1136 | u16 limit; |
| 1137 | u16 sent; |
| 1138 | unsigned int flags; |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1139 | bool data_lock_held; |
Paolo Abeni | caf971d | 2020-11-16 10:48:06 +0100 | [diff] [blame] | 1140 | }; |
| 1141 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1142 | static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq, |
| 1143 | int avail_size) |
| 1144 | { |
| 1145 | u64 window_end = mptcp_wnd_end(msk); |
| 1146 | |
| 1147 | if (__mptcp_check_fallback(msk)) |
| 1148 | return avail_size; |
| 1149 | |
| 1150 | if (!before64(data_seq + avail_size, window_end)) { |
| 1151 | u64 allowed_size = window_end - data_seq; |
| 1152 | |
| 1153 | return min_t(unsigned int, allowed_size, avail_size); |
| 1154 | } |
| 1155 | |
| 1156 | return avail_size; |
| 1157 | } |
| 1158 | |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1159 | static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) |
| 1160 | { |
| 1161 | struct skb_ext *mpext = __skb_ext_alloc(gfp); |
| 1162 | |
| 1163 | if (!mpext) |
| 1164 | return false; |
| 1165 | __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1169 | static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1170 | { |
| 1171 | struct sk_buff *skb; |
| 1172 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1173 | skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1174 | if (likely(skb)) { |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1175 | if (likely(__mptcp_add_ext(skb, gfp))) { |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1176 | skb_reserve(skb, MAX_TCP_HEADER); |
| 1177 | skb->reserved_tailroom = skb->end - skb->tail; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1178 | INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1179 | return skb; |
| 1180 | } |
| 1181 | __kfree_skb(skb); |
| 1182 | } else { |
| 1183 | mptcp_enter_memory_pressure(sk); |
| 1184 | } |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1188 | static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1189 | { |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1190 | struct sk_buff *skb; |
| 1191 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1192 | skb = __mptcp_do_alloc_tx_skb(sk, gfp); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1193 | if (!skb) |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1194 | return NULL; |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1195 | |
| 1196 | if (likely(sk_wmem_schedule(ssk, skb->truesize))) { |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1197 | tcp_skb_entail(ssk, skb); |
| 1198 | return skb; |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1199 | } |
| 1200 | kfree_skb(skb); |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1201 | return NULL; |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1204 | static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1205 | { |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1206 | gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1207 | |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1208 | if (unlikely(tcp_under_memory_pressure(sk))) { |
| 1209 | if (data_lock_held) |
| 1210 | __mptcp_mem_reclaim_partial(sk); |
| 1211 | else |
| 1212 | mptcp_mem_reclaim_partial(sk); |
| 1213 | } |
| 1214 | return __mptcp_alloc_tx_skb(sk, ssk, gfp); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
Geliang Tang | d0cc298 | 2021-06-17 16:46:08 -0700 | [diff] [blame] | 1217 | /* note: this always recompute the csum on the whole skb, even |
| 1218 | * if we just appended a single frag. More status info needed |
| 1219 | */ |
| 1220 | static void mptcp_update_data_checksum(struct sk_buff *skb, int added) |
| 1221 | { |
| 1222 | struct mptcp_ext *mpext = mptcp_get_ext(skb); |
| 1223 | __wsum csum = ~csum_unfold(mpext->csum); |
| 1224 | int offset = skb->len - added; |
| 1225 | |
| 1226 | mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); |
| 1227 | } |
| 1228 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1229 | static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1230 | struct mptcp_data_frag *dfrag, |
Paolo Abeni | caf971d | 2020-11-16 10:48:06 +0100 | [diff] [blame] | 1231 | struct mptcp_sendmsg_info *info) |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1232 | { |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1233 | u64 data_seq = dfrag->data_seq + info->sent; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1234 | int offset = dfrag->offset + info->sent; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1235 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1236 | bool zero_window_probe = false; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1237 | struct mptcp_ext *mpext = NULL; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1238 | bool can_coalesce = false; |
| 1239 | bool reuse_skb = true; |
| 1240 | struct sk_buff *skb; |
| 1241 | size_t copy; |
| 1242 | int i; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1243 | |
Geliang Tang | e4b6135 | 2021-04-16 15:38:02 -0700 | [diff] [blame] | 1244 | pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1245 | msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1246 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1247 | if (WARN_ON_ONCE(info->sent > info->limit || |
| 1248 | info->limit > dfrag->data_len)) |
| 1249 | return 0; |
| 1250 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1251 | /* compute send limit */ |
| 1252 | info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1253 | copy = info->size_goal; |
| 1254 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1255 | skb = tcp_write_queue_tail(ssk); |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1256 | if (skb && copy > skb->len) { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1257 | /* Limit the write to the size available in the |
| 1258 | * current skb, if any, so that we create at most a new skb. |
| 1259 | * Explicitly tells TCP internals to avoid collapsing on later |
| 1260 | * queue management operation, to avoid breaking the ext <-> |
| 1261 | * SSN association set here |
| 1262 | */ |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1263 | mpext = skb_ext_find(skb, SKB_EXT_MPTCP); |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1264 | if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1265 | TCP_SKB_CB(skb)->eor = 1; |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1266 | goto alloc_skb; |
| 1267 | } |
| 1268 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1269 | i = skb_shinfo(skb)->nr_frags; |
| 1270 | can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); |
| 1271 | if (!can_coalesce && i >= sysctl_max_skb_frags) { |
| 1272 | tcp_mark_push(tcp_sk(ssk), skb); |
| 1273 | goto alloc_skb; |
Paolo Abeni | 15e6ca97 | 2020-12-10 14:25:06 -0800 | [diff] [blame] | 1274 | } |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1275 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1276 | copy -= skb->len; |
| 1277 | } else { |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1278 | alloc_skb: |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1279 | skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); |
| 1280 | if (!skb) |
| 1281 | return -ENOMEM; |
| 1282 | |
| 1283 | i = skb_shinfo(skb)->nr_frags; |
| 1284 | reuse_skb = false; |
| 1285 | mpext = skb_ext_find(skb, SKB_EXT_MPTCP); |
| 1286 | } |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1287 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1288 | /* Zero window and all data acked? Probe. */ |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1289 | copy = mptcp_check_allowed_size(msk, data_seq, copy); |
| 1290 | if (copy == 0) { |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1291 | u64 snd_una = READ_ONCE(msk->snd_una); |
| 1292 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1293 | if (snd_una != msk->snd_nxt) { |
| 1294 | tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk)); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1295 | return 0; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1298 | zero_window_probe = true; |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1299 | data_seq = snd_una - 1; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1300 | copy = 1; |
| 1301 | |
| 1302 | /* all mptcp-level data is acked, no skbs should be present into the |
| 1303 | * ssk write queue |
| 1304 | */ |
| 1305 | WARN_ON_ONCE(reuse_skb); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1308 | copy = min_t(size_t, copy, info->limit - info->sent); |
| 1309 | if (!sk_wmem_schedule(ssk, copy)) { |
| 1310 | tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk)); |
Paolo Abeni | e222399 | 2020-11-16 10:48:03 +0100 | [diff] [blame] | 1311 | return -ENOMEM; |
Florian Westphal | 35759383 | 2020-08-14 15:56:34 +0200 | [diff] [blame] | 1312 | } |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1313 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1314 | if (can_coalesce) { |
| 1315 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); |
| 1316 | } else { |
| 1317 | get_page(dfrag->page); |
| 1318 | skb_fill_page_desc(skb, i, dfrag->page, offset, copy); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1321 | skb->len += copy; |
| 1322 | skb->data_len += copy; |
| 1323 | skb->truesize += copy; |
| 1324 | sk_wmem_queued_add(ssk, copy); |
| 1325 | sk_mem_charge(ssk, copy); |
| 1326 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 1327 | WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); |
| 1328 | TCP_SKB_CB(skb)->end_seq += copy; |
| 1329 | tcp_skb_pcount_set(skb, 0); |
| 1330 | |
| 1331 | /* on skb reuse we just need to update the DSS len */ |
| 1332 | if (reuse_skb) { |
| 1333 | TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; |
| 1334 | mpext->data_len += copy; |
| 1335 | WARN_ON_ONCE(zero_window_probe); |
| 1336 | goto out; |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1337 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1338 | |
| 1339 | memset(mpext, 0, sizeof(*mpext)); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1340 | mpext->data_seq = data_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1341 | mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1342 | mpext->data_len = copy; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1343 | mpext->use_map = 1; |
| 1344 | mpext->dsn64 = 1; |
| 1345 | |
| 1346 | pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", |
| 1347 | mpext->data_seq, mpext->subflow_seq, mpext->data_len, |
| 1348 | mpext->dsn64); |
| 1349 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1350 | if (zero_window_probe) { |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1351 | mptcp_subflow_ctx(ssk)->rel_write_seq += copy; |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1352 | mpext->frozen = 1; |
Geliang Tang | d0cc298 | 2021-06-17 16:46:08 -0700 | [diff] [blame] | 1353 | if (READ_ONCE(msk->csum_enabled)) |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1354 | mptcp_update_data_checksum(skb, copy); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1355 | tcp_push_pending_frames(ssk); |
Geliang Tang | d0cc298 | 2021-06-17 16:46:08 -0700 | [diff] [blame] | 1356 | return 0; |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1357 | } |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 1358 | out: |
Geliang Tang | d0cc298 | 2021-06-17 16:46:08 -0700 | [diff] [blame] | 1359 | if (READ_ONCE(msk->csum_enabled)) |
Paolo Abeni | f70cad1 | 2021-09-22 19:26:41 +0200 | [diff] [blame] | 1360 | mptcp_update_data_checksum(skb, copy); |
| 1361 | mptcp_subflow_ctx(ssk)->rel_write_seq += copy; |
| 1362 | return copy; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1363 | } |
| 1364 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1365 | #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ |
| 1366 | sizeof(struct tcphdr) - \ |
| 1367 | MAX_TCP_OPTION_SPACE - \ |
| 1368 | sizeof(struct ipv6hdr) - \ |
| 1369 | sizeof(struct frag_hdr)) |
| 1370 | |
| 1371 | struct subflow_send_info { |
| 1372 | struct sock *ssk; |
| 1373 | u64 ratio; |
| 1374 | }; |
| 1375 | |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 1376 | void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) |
| 1377 | { |
| 1378 | if (!subflow->stale) |
| 1379 | return; |
| 1380 | |
| 1381 | subflow->stale = 0; |
Paolo Abeni | fc1b4e3 | 2021-08-13 15:15:46 -0700 | [diff] [blame] | 1382 | MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) |
| 1386 | { |
| 1387 | if (unlikely(subflow->stale)) { |
| 1388 | u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); |
| 1389 | |
| 1390 | if (subflow->stale_rcv_tstamp == rcv_tstamp) |
| 1391 | return false; |
| 1392 | |
| 1393 | mptcp_subflow_set_active(subflow); |
| 1394 | } |
| 1395 | return __mptcp_subflow_active(subflow); |
| 1396 | } |
| 1397 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1398 | /* implement the mptcp packet scheduler; |
| 1399 | * returns the subflow that will transmit the next DSS |
| 1400 | * additionally updates the rtx timeout |
| 1401 | */ |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1402 | static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1403 | { |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1404 | struct subflow_send_info send_info[2]; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1405 | struct mptcp_subflow_context *subflow; |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1406 | struct sock *sk = (struct sock *)msk; |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1407 | int i, nr_active = 0; |
| 1408 | struct sock *ssk; |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1409 | long tout = 0; |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1410 | u64 ratio; |
| 1411 | u32 pace; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1412 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1413 | sock_owned_by_me(sk); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1414 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1415 | if (__mptcp_check_fallback(msk)) { |
| 1416 | if (!msk->first) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1417 | return NULL; |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1418 | return sk_stream_memory_free(msk->first) ? msk->first : NULL; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1421 | /* re-use last subflow, if the burst allow that */ |
| 1422 | if (msk->last_snd && msk->snd_burst > 0 && |
| 1423 | sk_stream_memory_free(msk->last_snd) && |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1424 | mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { |
| 1425 | mptcp_set_timeout(sk); |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1426 | return msk->last_snd; |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1427 | } |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1428 | |
| 1429 | /* pick the subflow with the lower wmem/wspace ratio */ |
| 1430 | for (i = 0; i < 2; ++i) { |
| 1431 | send_info[i].ssk = NULL; |
| 1432 | send_info[i].ratio = -1; |
| 1433 | } |
| 1434 | mptcp_for_each_subflow(msk, subflow) { |
Geliang Tang | e10a989 | 2021-04-16 15:38:04 -0700 | [diff] [blame] | 1435 | trace_mptcp_subflow_get_send(subflow); |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1436 | ssk = mptcp_subflow_tcp_sock(subflow); |
| 1437 | if (!mptcp_subflow_active(subflow)) |
| 1438 | continue; |
| 1439 | |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1440 | tout = max(tout, mptcp_timeout_from_subflow(subflow)); |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1441 | nr_active += !subflow->backup; |
Paolo Abeni | ec369c3 | 2021-01-20 15:39:12 +0100 | [diff] [blame] | 1442 | if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd) |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1443 | continue; |
| 1444 | |
| 1445 | pace = READ_ONCE(ssk->sk_pacing_rate); |
| 1446 | if (!pace) |
| 1447 | continue; |
| 1448 | |
| 1449 | ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, |
| 1450 | pace); |
| 1451 | if (ratio < send_info[subflow->backup].ratio) { |
| 1452 | send_info[subflow->backup].ssk = ssk; |
| 1453 | send_info[subflow->backup].ratio = ratio; |
| 1454 | } |
| 1455 | } |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1456 | __mptcp_set_timeout(sk, tout); |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1457 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1458 | /* pick the best backup if no other subflow is active */ |
| 1459 | if (!nr_active) |
| 1460 | send_info[0].ssk = send_info[1].ssk; |
| 1461 | |
| 1462 | if (send_info[0].ssk) { |
| 1463 | msk->last_snd = send_info[0].ssk; |
| 1464 | msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, |
Paolo Abeni | ec369c3 | 2021-01-20 15:39:12 +0100 | [diff] [blame] | 1465 | tcp_sk(msk->last_snd)->snd_wnd); |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1466 | return msk->last_snd; |
| 1467 | } |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1468 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1469 | return NULL; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Geliang Tang | b8e0def | 2021-10-26 16:29:16 -0700 | [diff] [blame^] | 1472 | static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1473 | { |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1474 | tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); |
| 1475 | release_sock(ssk); |
| 1476 | } |
| 1477 | |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 1478 | static void mptcp_update_post_push(struct mptcp_sock *msk, |
| 1479 | struct mptcp_data_frag *dfrag, |
| 1480 | u32 sent) |
| 1481 | { |
| 1482 | u64 snd_nxt_new = dfrag->data_seq; |
| 1483 | |
| 1484 | dfrag->already_sent += sent; |
| 1485 | |
| 1486 | msk->snd_burst -= sent; |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 1487 | |
| 1488 | snd_nxt_new += dfrag->already_sent; |
| 1489 | |
| 1490 | /* snd_nxt_new can be smaller than snd_nxt in case mptcp |
| 1491 | * is recovering after a failover. In that event, this re-sends |
| 1492 | * old segments. |
| 1493 | * |
| 1494 | * Thus compute snd_nxt_new candidate based on |
| 1495 | * the dfrag->data_seq that was sent and the data |
| 1496 | * that has been handed to the subflow for transmission |
| 1497 | * and skip update in case it was old dfrag. |
| 1498 | */ |
| 1499 | if (likely(after64(snd_nxt_new, msk->snd_nxt))) |
| 1500 | msk->snd_nxt = snd_nxt_new; |
| 1501 | } |
| 1502 | |
Florian Westphal | 3241a9c | 2021-09-24 14:12:38 -0700 | [diff] [blame] | 1503 | static void mptcp_check_and_set_pending(struct sock *sk) |
| 1504 | { |
| 1505 | if (mptcp_send_head(sk) && |
| 1506 | !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) |
| 1507 | set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); |
| 1508 | } |
| 1509 | |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 1510 | void __mptcp_push_pending(struct sock *sk, unsigned int flags) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1511 | { |
| 1512 | struct sock *prev_ssk = NULL, *ssk = NULL; |
| 1513 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1514 | struct mptcp_sendmsg_info info = { |
| 1515 | .flags = flags, |
| 1516 | }; |
| 1517 | struct mptcp_data_frag *dfrag; |
| 1518 | int len, copied = 0; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1519 | |
| 1520 | while ((dfrag = mptcp_send_head(sk))) { |
| 1521 | info.sent = dfrag->already_sent; |
| 1522 | info.limit = dfrag->data_len; |
| 1523 | len = dfrag->data_len - dfrag->already_sent; |
| 1524 | while (len > 0) { |
| 1525 | int ret = 0; |
| 1526 | |
| 1527 | prev_ssk = ssk; |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 1528 | mptcp_flush_join_list(msk); |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1529 | ssk = mptcp_subflow_get_send(msk); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1530 | |
Paolo Abeni | 9758f40 | 2021-08-26 17:44:54 -0700 | [diff] [blame] | 1531 | /* First check. If the ssk has changed since |
| 1532 | * the last round, release prev_ssk |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1533 | */ |
| 1534 | if (ssk != prev_ssk && prev_ssk) |
Geliang Tang | b8e0def | 2021-10-26 16:29:16 -0700 | [diff] [blame^] | 1535 | mptcp_push_release(prev_ssk, &info); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1536 | if (!ssk) |
| 1537 | goto out; |
| 1538 | |
Paolo Abeni | 9758f40 | 2021-08-26 17:44:54 -0700 | [diff] [blame] | 1539 | /* Need to lock the new subflow only if different |
| 1540 | * from the previous one, otherwise we are still |
| 1541 | * helding the relevant lock |
| 1542 | */ |
| 1543 | if (ssk != prev_ssk) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1544 | lock_sock(ssk); |
| 1545 | |
| 1546 | ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); |
| 1547 | if (ret <= 0) { |
Geliang Tang | b8e0def | 2021-10-26 16:29:16 -0700 | [diff] [blame^] | 1548 | mptcp_push_release(ssk, &info); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1549 | goto out; |
| 1550 | } |
| 1551 | |
| 1552 | info.sent += ret; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1553 | copied += ret; |
| 1554 | len -= ret; |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 1555 | |
| 1556 | mptcp_update_post_push(msk, dfrag, ret); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1557 | } |
| 1558 | WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); |
| 1559 | } |
| 1560 | |
| 1561 | /* at this point we held the socket lock for the last subflow we used */ |
| 1562 | if (ssk) |
Geliang Tang | b8e0def | 2021-10-26 16:29:16 -0700 | [diff] [blame^] | 1563 | mptcp_push_release(ssk, &info); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1564 | |
| 1565 | out: |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 1566 | /* ensure the rtx timer is running */ |
| 1567 | if (!mptcp_timer_pending(sk)) |
| 1568 | mptcp_reset_timer(sk); |
| 1569 | if (copied) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1570 | __mptcp_check_send_data_fin(sk); |
| 1571 | } |
| 1572 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1573 | static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) |
| 1574 | { |
| 1575 | struct mptcp_sock *msk = mptcp_sk(sk); |
Paolo Abeni | 1094c6f | 2021-09-01 10:15:36 -0700 | [diff] [blame] | 1576 | struct mptcp_sendmsg_info info = { |
| 1577 | .data_lock_held = true, |
| 1578 | }; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1579 | struct mptcp_data_frag *dfrag; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1580 | struct sock *xmit_ssk; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1581 | int len, copied = 0; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1582 | bool first = true; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1583 | |
| 1584 | info.flags = 0; |
| 1585 | while ((dfrag = mptcp_send_head(sk))) { |
| 1586 | info.sent = dfrag->already_sent; |
| 1587 | info.limit = dfrag->data_len; |
| 1588 | len = dfrag->data_len - dfrag->already_sent; |
| 1589 | while (len > 0) { |
| 1590 | int ret = 0; |
| 1591 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1592 | /* the caller already invoked the packet scheduler, |
| 1593 | * check for a different subflow usage only after |
| 1594 | * spooling the first chunk of data |
| 1595 | */ |
| 1596 | xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); |
| 1597 | if (!xmit_ssk) |
| 1598 | goto out; |
| 1599 | if (xmit_ssk != ssk) { |
| 1600 | mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); |
| 1601 | goto out; |
| 1602 | } |
| 1603 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1604 | ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); |
| 1605 | if (ret <= 0) |
| 1606 | goto out; |
| 1607 | |
| 1608 | info.sent += ret; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1609 | copied += ret; |
| 1610 | len -= ret; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1611 | first = false; |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 1612 | |
| 1613 | mptcp_update_post_push(msk, dfrag, ret); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1614 | } |
| 1615 | WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); |
| 1616 | } |
| 1617 | |
| 1618 | out: |
| 1619 | /* __mptcp_alloc_tx_skb could have released some wmem and we are |
| 1620 | * not going to flush it via release_sock() |
| 1621 | */ |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1622 | if (copied) { |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1623 | tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, |
| 1624 | info.size_goal); |
Paolo Abeni | d09d818 | 2021-02-11 15:30:42 -0800 | [diff] [blame] | 1625 | if (!mptcp_timer_pending(sk)) |
| 1626 | mptcp_reset_timer(sk); |
| 1627 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1628 | if (msk->snd_data_fin_enable && |
| 1629 | msk->snd_nxt + 1 == msk->write_seq) |
| 1630 | mptcp_schedule_work(sk); |
| 1631 | } |
| 1632 | } |
| 1633 | |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1634 | static void mptcp_set_nospace(struct sock *sk) |
| 1635 | { |
| 1636 | /* enable autotune */ |
| 1637 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1638 | |
| 1639 | /* will be cleared on avail space */ |
| 1640 | set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); |
| 1641 | } |
| 1642 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1643 | static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
| 1644 | { |
| 1645 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 1646 | struct page_frag *pfrag; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1647 | size_t copied = 0; |
Paolo Abeni | caf971d | 2020-11-16 10:48:06 +0100 | [diff] [blame] | 1648 | int ret = 0; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1649 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1650 | |
Paolo Abeni | 987858e | 2021-04-23 11:17:07 -0700 | [diff] [blame] | 1651 | /* we don't support FASTOPEN yet */ |
| 1652 | if (msg->msg_flags & MSG_FASTOPEN) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1653 | return -EOPNOTSUPP; |
| 1654 | |
Paolo Abeni | 987858e | 2021-04-23 11:17:07 -0700 | [diff] [blame] | 1655 | /* silently ignore everything else */ |
| 1656 | msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1657 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 1658 | lock_sock(sk); |
Mat Martineau | 1954b86 | 2020-02-28 15:47:39 -0800 | [diff] [blame] | 1659 | |
| 1660 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1661 | |
| 1662 | if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { |
| 1663 | ret = sk_stream_wait_connect(sk, &timeo); |
| 1664 | if (ret) |
| 1665 | goto out; |
| 1666 | } |
| 1667 | |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 1668 | pfrag = sk_page_frag(sk); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1669 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1670 | while (msg_data_left(msg)) { |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1671 | int total_ts, frag_truesize = 0; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1672 | struct mptcp_data_frag *dfrag; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1673 | bool dfrag_collapsed; |
| 1674 | size_t psize, offset; |
Mat Martineau | 57baaf2 | 2020-07-28 15:12:00 -0700 | [diff] [blame] | 1675 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1676 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { |
| 1677 | ret = -EPIPE; |
| 1678 | goto out; |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1681 | /* reuse tail pfrag, if possible, or carve a new one from the |
| 1682 | * page allocator |
| 1683 | */ |
| 1684 | dfrag = mptcp_pending_tail(sk); |
| 1685 | dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); |
| 1686 | if (!dfrag_collapsed) { |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1687 | if (!sk_stream_memory_free(sk)) |
| 1688 | goto wait_for_memory; |
| 1689 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1690 | if (!mptcp_page_frag_refill(sk, pfrag)) |
| 1691 | goto wait_for_memory; |
| 1692 | |
| 1693 | dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); |
| 1694 | frag_truesize = dfrag->overhead; |
| 1695 | } |
| 1696 | |
| 1697 | /* we do not bound vs wspace, to allow a single packet. |
| 1698 | * memory accounting will prevent execessive memory usage |
| 1699 | * anyway |
| 1700 | */ |
| 1701 | offset = dfrag->offset + dfrag->data_len; |
| 1702 | psize = pfrag->size - offset; |
| 1703 | psize = min_t(size_t, psize, msg_data_left(msg)); |
Paolo Abeni | 724cfd2 | 2020-11-27 11:10:25 +0100 | [diff] [blame] | 1704 | total_ts = psize + frag_truesize; |
Paolo Abeni | 8ce568e | 2021-06-21 15:54:33 -0700 | [diff] [blame] | 1705 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 1706 | if (!sk_wmem_schedule(sk, total_ts)) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1707 | goto wait_for_memory; |
| 1708 | |
| 1709 | if (copy_page_from_iter(dfrag->page, offset, psize, |
| 1710 | &msg->msg_iter) != psize) { |
| 1711 | ret = -EFAULT; |
| 1712 | goto out; |
| 1713 | } |
| 1714 | |
| 1715 | /* data successfully copied into the write queue */ |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 1716 | sk->sk_forward_alloc -= total_ts; |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1717 | copied += psize; |
| 1718 | dfrag->data_len += psize; |
| 1719 | frag_truesize += psize; |
| 1720 | pfrag->offset += frag_truesize; |
| 1721 | WRITE_ONCE(msk->write_seq, msk->write_seq + psize); |
| 1722 | |
| 1723 | /* charge data on mptcp pending queue to the msk socket |
| 1724 | * Note: we charge such data both to sk and ssk |
| 1725 | */ |
| 1726 | sk_wmem_queued_add(sk, frag_truesize); |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1727 | if (!dfrag_collapsed) { |
| 1728 | get_page(dfrag->page); |
| 1729 | list_add_tail(&dfrag->list, &msk->rtx_queue); |
| 1730 | if (!msk->first_pending) |
| 1731 | WRITE_ONCE(msk->first_pending, dfrag); |
| 1732 | } |
Geliang Tang | e4b6135 | 2021-04-16 15:38:02 -0700 | [diff] [blame] | 1733 | pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1734 | dfrag->data_seq, dfrag->data_len, dfrag->already_sent, |
| 1735 | !dfrag_collapsed); |
| 1736 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 1737 | continue; |
| 1738 | |
| 1739 | wait_for_memory: |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1740 | mptcp_set_nospace(sk); |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 1741 | __mptcp_push_pending(sk, msg->msg_flags); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1742 | ret = sk_stream_wait_memory(sk, &timeo); |
| 1743 | if (ret) |
| 1744 | goto out; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
Paolo Abeni | 13e1603 | 2020-12-16 12:48:35 +0100 | [diff] [blame] | 1747 | if (copied) |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 1748 | __mptcp_push_pending(sk, msg->msg_flags); |
Paolo Abeni | da51aef | 2020-09-14 10:01:10 +0200 | [diff] [blame] | 1749 | |
Mat Martineau | 1954b86 | 2020-02-28 15:47:39 -0800 | [diff] [blame] | 1750 | out: |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1751 | release_sock(sk); |
Paolo Abeni | 8555c6b | 2020-08-03 18:40:39 +0200 | [diff] [blame] | 1752 | return copied ? : ret; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1753 | } |
| 1754 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1755 | static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, |
| 1756 | struct msghdr *msg, |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 1757 | size_t len, int flags, |
| 1758 | struct scm_timestamping_internal *tss, |
| 1759 | int *cmsg_flags) |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1760 | { |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 1761 | struct sk_buff *skb, *tmp; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1762 | int copied = 0; |
| 1763 | |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 1764 | skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1765 | u32 offset = MPTCP_SKB_CB(skb)->offset; |
| 1766 | u32 data_len = skb->len - offset; |
| 1767 | u32 count = min_t(size_t, len - copied, data_len); |
| 1768 | int err; |
| 1769 | |
Paolo Abeni | d976092 | 2021-04-23 11:17:06 -0700 | [diff] [blame] | 1770 | if (!(flags & MSG_TRUNC)) { |
| 1771 | err = skb_copy_datagram_msg(skb, offset, msg, count); |
| 1772 | if (unlikely(err < 0)) { |
| 1773 | if (!copied) |
| 1774 | return err; |
| 1775 | break; |
| 1776 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 1779 | if (MPTCP_SKB_CB(skb)->has_rxtstamp) { |
| 1780 | tcp_update_recv_tstamps(skb, tss); |
| 1781 | *cmsg_flags |= MPTCP_CMSG_TS; |
| 1782 | } |
| 1783 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1784 | copied += count; |
| 1785 | |
| 1786 | if (count < data_len) { |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 1787 | if (!(flags & MSG_PEEK)) |
| 1788 | MPTCP_SKB_CB(skb)->offset += count; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1789 | break; |
| 1790 | } |
| 1791 | |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 1792 | if (!(flags & MSG_PEEK)) { |
| 1793 | /* we will bulk release the skb memory later */ |
| 1794 | skb->destructor = NULL; |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 1795 | WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 1796 | __skb_unlink(skb, &msk->receive_queue); |
| 1797 | __kfree_skb(skb); |
| 1798 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1799 | |
| 1800 | if (copied >= len) |
| 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | return copied; |
| 1805 | } |
| 1806 | |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 1807 | /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. |
| 1808 | * |
| 1809 | * Only difference: Use highest rtt estimate of the subflows in use. |
| 1810 | */ |
| 1811 | static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) |
| 1812 | { |
| 1813 | struct mptcp_subflow_context *subflow; |
| 1814 | struct sock *sk = (struct sock *)msk; |
| 1815 | u32 time, advmss = 1; |
| 1816 | u64 rtt_us, mstamp; |
| 1817 | |
| 1818 | sock_owned_by_me(sk); |
| 1819 | |
| 1820 | if (copied <= 0) |
| 1821 | return; |
| 1822 | |
| 1823 | msk->rcvq_space.copied += copied; |
| 1824 | |
| 1825 | mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); |
| 1826 | time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); |
| 1827 | |
| 1828 | rtt_us = msk->rcvq_space.rtt_us; |
| 1829 | if (rtt_us && time < (rtt_us >> 3)) |
| 1830 | return; |
| 1831 | |
| 1832 | rtt_us = 0; |
| 1833 | mptcp_for_each_subflow(msk, subflow) { |
| 1834 | const struct tcp_sock *tp; |
| 1835 | u64 sf_rtt_us; |
| 1836 | u32 sf_advmss; |
| 1837 | |
| 1838 | tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); |
| 1839 | |
| 1840 | sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); |
| 1841 | sf_advmss = READ_ONCE(tp->advmss); |
| 1842 | |
| 1843 | rtt_us = max(sf_rtt_us, rtt_us); |
| 1844 | advmss = max(sf_advmss, advmss); |
| 1845 | } |
| 1846 | |
| 1847 | msk->rcvq_space.rtt_us = rtt_us; |
| 1848 | if (time < (rtt_us >> 3) || rtt_us == 0) |
| 1849 | return; |
| 1850 | |
| 1851 | if (msk->rcvq_space.copied <= msk->rcvq_space.space) |
| 1852 | goto new_measure; |
| 1853 | |
| 1854 | if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && |
| 1855 | !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { |
| 1856 | int rcvmem, rcvbuf; |
| 1857 | u64 rcvwin, grow; |
| 1858 | |
| 1859 | rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; |
| 1860 | |
| 1861 | grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); |
| 1862 | |
| 1863 | do_div(grow, msk->rcvq_space.space); |
| 1864 | rcvwin += (grow << 1); |
| 1865 | |
| 1866 | rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); |
| 1867 | while (tcp_win_from_space(sk, rcvmem) < advmss) |
| 1868 | rcvmem += 128; |
| 1869 | |
| 1870 | do_div(rcvwin, advmss); |
| 1871 | rcvbuf = min_t(u64, rcvwin * rcvmem, |
| 1872 | sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); |
| 1873 | |
| 1874 | if (rcvbuf > sk->sk_rcvbuf) { |
| 1875 | u32 window_clamp; |
| 1876 | |
| 1877 | window_clamp = tcp_win_from_space(sk, rcvbuf); |
| 1878 | WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); |
| 1879 | |
| 1880 | /* Make subflows follow along. If we do not do this, we |
| 1881 | * get drops at subflow level if skbs can't be moved to |
| 1882 | * the mptcp rx queue fast enough (announced rcv_win can |
| 1883 | * exceed ssk->sk_rcvbuf). |
| 1884 | */ |
| 1885 | mptcp_for_each_subflow(msk, subflow) { |
| 1886 | struct sock *ssk; |
Paolo Abeni | c76c695 | 2020-09-14 10:01:18 +0200 | [diff] [blame] | 1887 | bool slow; |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 1888 | |
| 1889 | ssk = mptcp_subflow_tcp_sock(subflow); |
Paolo Abeni | c76c695 | 2020-09-14 10:01:18 +0200 | [diff] [blame] | 1890 | slow = lock_sock_fast(ssk); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 1891 | WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); |
| 1892 | tcp_sk(ssk)->window_clamp = window_clamp; |
Paolo Abeni | c76c695 | 2020-09-14 10:01:18 +0200 | [diff] [blame] | 1893 | tcp_cleanup_rbuf(ssk, 1); |
| 1894 | unlock_sock_fast(ssk, slow); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | msk->rcvq_space.space = msk->rcvq_space.copied; |
| 1900 | new_measure: |
| 1901 | msk->rcvq_space.copied = 0; |
| 1902 | msk->rcvq_space.time = mstamp; |
| 1903 | } |
| 1904 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1905 | static void __mptcp_update_rmem(struct sock *sk) |
| 1906 | { |
| 1907 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1908 | |
| 1909 | if (!msk->rmem_released) |
| 1910 | return; |
| 1911 | |
| 1912 | atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 1913 | mptcp_rmem_uncharge(sk, msk->rmem_released); |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 1914 | WRITE_ONCE(msk->rmem_released, 0); |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static void __mptcp_splice_receive_queue(struct sock *sk) |
| 1918 | { |
| 1919 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1920 | |
| 1921 | skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); |
| 1922 | } |
| 1923 | |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 1924 | static bool __mptcp_move_skbs(struct mptcp_sock *msk) |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1925 | { |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1926 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1927 | unsigned int moved = 0; |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1928 | bool ret, done; |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 1929 | |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 1930 | mptcp_flush_join_list(msk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1931 | do { |
| 1932 | struct sock *ssk = mptcp_subflow_recv_lookup(msk); |
Florian Westphal | 65f49fe7 | 2020-11-03 11:05:04 -0800 | [diff] [blame] | 1933 | bool slowpath; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1934 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1935 | /* we can have data pending in the subflows only if the msk |
| 1936 | * receive buffer was full at subflow_data_ready() time, |
| 1937 | * that is an unlikely slow path. |
| 1938 | */ |
| 1939 | if (likely(!ssk)) |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1940 | break; |
| 1941 | |
Florian Westphal | 65f49fe7 | 2020-11-03 11:05:04 -0800 | [diff] [blame] | 1942 | slowpath = lock_sock_fast(ssk); |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1943 | mptcp_data_lock(sk); |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 1944 | __mptcp_update_rmem(sk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1945 | done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1946 | mptcp_data_unlock(sk); |
Paolo Abeni | 499ada5 | 2021-06-10 15:59:44 -0700 | [diff] [blame] | 1947 | |
| 1948 | if (unlikely(ssk->sk_err)) |
| 1949 | __mptcp_error_report(sk); |
Florian Westphal | 65f49fe7 | 2020-11-03 11:05:04 -0800 | [diff] [blame] | 1950 | unlock_sock_fast(ssk, slowpath); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1951 | } while (!done); |
| 1952 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1953 | /* acquire the data lock only if some input data is pending */ |
| 1954 | ret = moved > 0; |
| 1955 | if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || |
| 1956 | !skb_queue_empty_lockless(&sk->sk_receive_queue)) { |
| 1957 | mptcp_data_lock(sk); |
| 1958 | __mptcp_update_rmem(sk); |
| 1959 | ret |= __mptcp_ofo_queue(msk); |
| 1960 | __mptcp_splice_receive_queue(sk); |
| 1961 | mptcp_data_unlock(sk); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 1962 | } |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 1963 | if (ret) |
| 1964 | mptcp_check_data_fin((struct sock *)msk); |
| 1965 | return !skb_queue_empty(&msk->receive_queue); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1968 | static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 1969 | int nonblock, int flags, int *addr_len) |
| 1970 | { |
| 1971 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 1972 | struct scm_timestamping_internal tss; |
| 1973 | int copied = 0, cmsg_flags = 0; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1974 | int target; |
| 1975 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1976 | |
Paolo Abeni | cb9d80f | 2021-04-23 11:17:05 -0700 | [diff] [blame] | 1977 | /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ |
| 1978 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 1979 | return inet_recv_error(sk, msg, len, addr_len); |
| 1980 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 1981 | lock_sock(sk); |
Paolo Abeni | fd89767 | 2020-11-24 22:51:24 +0100 | [diff] [blame] | 1982 | if (unlikely(sk->sk_state == TCP_LISTEN)) { |
| 1983 | copied = -ENOTCONN; |
| 1984 | goto out_err; |
| 1985 | } |
| 1986 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1987 | timeo = sock_rcvtimeo(sk, nonblock); |
| 1988 | |
| 1989 | len = min_t(size_t, len, INT_MAX); |
| 1990 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
| 1991 | |
Eric Dumazet | 05e3ece | 2020-12-02 09:16:57 -0800 | [diff] [blame] | 1992 | while (copied < len) { |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 1993 | int bytes_read; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1994 | |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 1995 | bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1996 | if (unlikely(bytes_read < 0)) { |
| 1997 | if (!copied) |
| 1998 | copied = bytes_read; |
| 1999 | goto out_err; |
| 2000 | } |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2001 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 2002 | copied += bytes_read; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2003 | |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 2004 | /* be sure to advertise window change */ |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 2005 | mptcp_cleanup_rbuf(msk); |
| 2006 | |
| 2007 | if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) |
| 2008 | continue; |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 2009 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2010 | /* only the master socket status is relevant here. The exit |
| 2011 | * conditions mirror closely tcp_recvmsg() |
| 2012 | */ |
| 2013 | if (copied >= target) |
| 2014 | break; |
| 2015 | |
| 2016 | if (copied) { |
| 2017 | if (sk->sk_err || |
| 2018 | sk->sk_state == TCP_CLOSE || |
| 2019 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 2020 | !timeo || |
| 2021 | signal_pending(current)) |
| 2022 | break; |
| 2023 | } else { |
| 2024 | if (sk->sk_err) { |
| 2025 | copied = sock_error(sk); |
| 2026 | break; |
| 2027 | } |
| 2028 | |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame] | 2029 | if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) |
| 2030 | mptcp_check_for_eof(msk); |
| 2031 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2032 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 2033 | /* race breaker: the shutdown could be after the |
| 2034 | * previous receive queue check |
| 2035 | */ |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 2036 | if (__mptcp_move_skbs(msk)) |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2037 | continue; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2038 | break; |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2039 | } |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2040 | |
| 2041 | if (sk->sk_state == TCP_CLOSE) { |
| 2042 | copied = -ENOTCONN; |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | if (!timeo) { |
| 2047 | copied = -EAGAIN; |
| 2048 | break; |
| 2049 | } |
| 2050 | |
| 2051 | if (signal_pending(current)) { |
| 2052 | copied = sock_intr_errno(timeo); |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | pr_debug("block timeout %ld", timeo); |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 2058 | sk_wait_data(sk, &timeo, NULL); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 2059 | } |
Paolo Abeni | 3c90e37 | 2021-06-21 15:54:35 -0700 | [diff] [blame] | 2060 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 2061 | out_err: |
Florian Westphal | b7f653b | 2021-06-03 16:24:32 -0700 | [diff] [blame] | 2062 | if (cmsg_flags && copied >= 0) { |
| 2063 | if (cmsg_flags & MPTCP_CMSG_TS) |
| 2064 | tcp_recv_timestamp(msg, sk, &tss); |
| 2065 | } |
| 2066 | |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 2067 | pr_debug("msk=%p rx queue empty=%d:%d copied=%d", |
| 2068 | msk, skb_queue_empty_lockless(&sk->sk_receive_queue), |
| 2069 | skb_queue_empty(&msk->receive_queue), copied); |
Yonglong Li | ca4fb892 | 2021-04-23 11:17:08 -0700 | [diff] [blame] | 2070 | if (!(flags & MSG_PEEK)) |
| 2071 | mptcp_rcv_space_adjust(msk, copied); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 2072 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2073 | release_sock(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2074 | return copied; |
| 2075 | } |
| 2076 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 2077 | static void mptcp_retransmit_timer(struct timer_list *t) |
| 2078 | { |
| 2079 | struct inet_connection_sock *icsk = from_timer(icsk, t, |
| 2080 | icsk_retransmit_timer); |
| 2081 | struct sock *sk = &icsk->icsk_inet.sk; |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2082 | struct mptcp_sock *msk = mptcp_sk(sk); |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 2083 | |
| 2084 | bh_lock_sock(sk); |
| 2085 | if (!sock_owned_by_user(sk)) { |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2086 | /* we need a process context to retransmit */ |
| 2087 | if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) |
| 2088 | mptcp_schedule_work(sk); |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 2089 | } else { |
| 2090 | /* delegate our work to tcp_release_cb() */ |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2091 | set_bit(MPTCP_RETRANSMIT, &msk->flags); |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 2092 | } |
| 2093 | bh_unlock_sock(sk); |
| 2094 | sock_put(sk); |
| 2095 | } |
| 2096 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2097 | static void mptcp_timeout_timer(struct timer_list *t) |
| 2098 | { |
| 2099 | struct sock *sk = from_timer(sk, t, sk_timer); |
| 2100 | |
| 2101 | mptcp_schedule_work(sk); |
Florian Westphal | b6d69fc | 2020-11-24 17:24:46 +0100 | [diff] [blame] | 2102 | sock_put(sk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2103 | } |
| 2104 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2105 | /* Find an idle subflow. Return NULL if there is unacked data at tcp |
| 2106 | * level. |
| 2107 | * |
| 2108 | * A backup subflow is returned only if that is the only kind available. |
| 2109 | */ |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 2110 | static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2111 | { |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 2112 | struct sock *backup = NULL, *pick = NULL; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2113 | struct mptcp_subflow_context *subflow; |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 2114 | int min_stale_count = INT_MAX; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2115 | |
| 2116 | sock_owned_by_me((const struct sock *)msk); |
| 2117 | |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 2118 | if (__mptcp_check_fallback(msk)) |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 2119 | return NULL; |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 2120 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2121 | mptcp_for_each_subflow(msk, subflow) { |
| 2122 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 2123 | |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 2124 | if (!__mptcp_subflow_active(subflow)) |
Paolo Abeni | d5f4919 | 2020-09-14 10:01:17 +0200 | [diff] [blame] | 2125 | continue; |
| 2126 | |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 2127 | /* still data outstanding at TCP level? skip this */ |
| 2128 | if (!tcp_rtx_and_write_queues_empty(ssk)) { |
| 2129 | mptcp_pm_subflow_chk_stale(msk, ssk); |
| 2130 | min_stale_count = min_t(int, min_stale_count, subflow->stale_count); |
| 2131 | continue; |
Florian Westphal | 860975c | 2020-11-19 11:45:56 -0800 | [diff] [blame] | 2132 | } |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2133 | |
| 2134 | if (subflow->backup) { |
| 2135 | if (!backup) |
| 2136 | backup = ssk; |
| 2137 | continue; |
| 2138 | } |
| 2139 | |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 2140 | if (!pick) |
| 2141 | pick = ssk; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Paolo Abeni | 71b7dec | 2021-08-13 15:15:42 -0700 | [diff] [blame] | 2144 | if (pick) |
| 2145 | return pick; |
| 2146 | |
| 2147 | /* use backup only if there are no progresses anywhere */ |
| 2148 | return min_stale_count > 1 ? backup : NULL; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
Florian Westphal | 17aee05 | 2021-03-04 13:32:11 -0800 | [diff] [blame] | 2151 | static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) |
| 2152 | { |
| 2153 | if (msk->subflow) { |
| 2154 | iput(SOCK_INODE(msk->subflow)); |
| 2155 | msk->subflow = NULL; |
| 2156 | } |
| 2157 | } |
| 2158 | |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2159 | bool __mptcp_retransmit_pending_data(struct sock *sk) |
| 2160 | { |
| 2161 | struct mptcp_data_frag *cur, *rtx_head; |
| 2162 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2163 | |
| 2164 | if (__mptcp_check_fallback(mptcp_sk(sk))) |
| 2165 | return false; |
| 2166 | |
| 2167 | if (tcp_rtx_and_write_queues_empty(sk)) |
| 2168 | return false; |
| 2169 | |
| 2170 | /* the closing socket has some data untransmitted and/or unacked: |
| 2171 | * some data in the mptcp rtx queue has not really xmitted yet. |
| 2172 | * keep it simple and re-inject the whole mptcp level rtx queue |
| 2173 | */ |
| 2174 | mptcp_data_lock(sk); |
| 2175 | __mptcp_clean_una_wakeup(sk); |
| 2176 | rtx_head = mptcp_rtx_head(sk); |
| 2177 | if (!rtx_head) { |
| 2178 | mptcp_data_unlock(sk); |
| 2179 | return false; |
| 2180 | } |
| 2181 | |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 2182 | msk->recovery_snd_nxt = msk->snd_nxt; |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2183 | msk->recovery = true; |
| 2184 | mptcp_data_unlock(sk); |
| 2185 | |
| 2186 | msk->first_pending = rtx_head; |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2187 | msk->snd_burst = 0; |
| 2188 | |
| 2189 | /* be sure to clear the "sent status" on all re-injected fragments */ |
| 2190 | list_for_each_entry(cur, &msk->rtx_queue, list) { |
| 2191 | if (!cur->already_sent) |
| 2192 | break; |
| 2193 | cur->already_sent = 0; |
| 2194 | } |
| 2195 | |
| 2196 | return true; |
| 2197 | } |
| 2198 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2199 | /* subflow sockets can be either outgoing (connect) or incoming |
| 2200 | * (accept). |
| 2201 | * |
| 2202 | * Outgoing subflows use in-kernel sockets. |
| 2203 | * Incoming subflows do not have their own 'struct socket' allocated, |
| 2204 | * so we need to use tcp_close() after detaching them from the mptcp |
| 2205 | * parent socket. |
| 2206 | */ |
Florian Westphal | a141e02 | 2021-02-12 15:59:55 -0800 | [diff] [blame] | 2207 | static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, |
| 2208 | struct mptcp_subflow_context *subflow) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2209 | { |
Florian Westphal | e0be493 | 2021-03-04 13:32:08 -0800 | [diff] [blame] | 2210 | struct mptcp_sock *msk = mptcp_sk(sk); |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2211 | bool need_push; |
Florian Westphal | e0be493 | 2021-03-04 13:32:08 -0800 | [diff] [blame] | 2212 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2213 | list_del(&subflow->node); |
| 2214 | |
Paolo Abeni | 3f8b266 | 2020-12-16 12:48:33 +0100 | [diff] [blame] | 2215 | lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* if we are invoked by the msk cleanup code, the subflow is |
| 2218 | * already orphaned |
| 2219 | */ |
Paolo Abeni | 866f26f | 2021-01-20 15:39:10 +0100 | [diff] [blame] | 2220 | if (ssk->sk_socket) |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2221 | sock_orphan(ssk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2222 | |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2223 | need_push = __mptcp_retransmit_pending_data(sk); |
Paolo Abeni | d7b1bfd | 2020-12-09 12:03:31 +0100 | [diff] [blame] | 2224 | subflow->disposable = 1; |
| 2225 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2226 | /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops |
| 2227 | * the ssk has been already destroyed, we just need to release the |
| 2228 | * reference owned by msk; |
| 2229 | */ |
| 2230 | if (!inet_csk(ssk)->icsk_ulp_ops) { |
| 2231 | kfree_rcu(subflow, rcu); |
| 2232 | } else { |
Paolo Abeni | d7b1bfd | 2020-12-09 12:03:31 +0100 | [diff] [blame] | 2233 | /* otherwise tcp will dispose of the ssk and subflow ctx */ |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2234 | __tcp_close(ssk, 0); |
| 2235 | |
| 2236 | /* close acquired an extra ref */ |
| 2237 | __sock_put(ssk); |
| 2238 | } |
| 2239 | release_sock(ssk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2240 | |
| 2241 | sock_put(ssk); |
Florian Westphal | e0be493 | 2021-03-04 13:32:08 -0800 | [diff] [blame] | 2242 | |
| 2243 | if (ssk == msk->last_snd) |
| 2244 | msk->last_snd = NULL; |
Florian Westphal | 17aee05 | 2021-03-04 13:32:11 -0800 | [diff] [blame] | 2245 | |
Florian Westphal | c8fe62f | 2021-03-04 13:32:12 -0800 | [diff] [blame] | 2246 | if (ssk == msk->first) |
| 2247 | msk->first = NULL; |
| 2248 | |
Florian Westphal | 17aee05 | 2021-03-04 13:32:11 -0800 | [diff] [blame] | 2249 | if (msk->subflow && ssk == msk->subflow->sk) |
| 2250 | mptcp_dispose_initial_subflow(msk); |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2251 | |
| 2252 | if (need_push) |
| 2253 | __mptcp_push_pending(sk, 0); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2254 | } |
| 2255 | |
Florian Westphal | a141e02 | 2021-02-12 15:59:55 -0800 | [diff] [blame] | 2256 | void mptcp_close_ssk(struct sock *sk, struct sock *ssk, |
| 2257 | struct mptcp_subflow_context *subflow) |
| 2258 | { |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 2259 | if (sk->sk_state == TCP_ESTABLISHED) |
| 2260 | mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); |
Florian Westphal | a141e02 | 2021-02-12 15:59:55 -0800 | [diff] [blame] | 2261 | __mptcp_close_ssk(sk, ssk, subflow); |
| 2262 | } |
| 2263 | |
Paolo Abeni | dc24f8b | 2020-02-26 12:19:03 +0100 | [diff] [blame] | 2264 | static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) |
| 2265 | { |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Paolo Abeni | 0e4f35d | 2020-10-09 19:00:01 +0200 | [diff] [blame] | 2269 | static void __mptcp_close_subflow(struct mptcp_sock *msk) |
| 2270 | { |
| 2271 | struct mptcp_subflow_context *subflow, *tmp; |
| 2272 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 2273 | might_sleep(); |
| 2274 | |
Paolo Abeni | 0e4f35d | 2020-10-09 19:00:01 +0200 | [diff] [blame] | 2275 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 2276 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 2277 | |
| 2278 | if (inet_sk_state_load(ssk) != TCP_CLOSE) |
| 2279 | continue; |
| 2280 | |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 2281 | /* 'subflow_data_ready' will re-sched once rx queue is empty */ |
| 2282 | if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) |
| 2283 | continue; |
| 2284 | |
Florian Westphal | a141e02 | 2021-02-12 15:59:55 -0800 | [diff] [blame] | 2285 | mptcp_close_ssk((struct sock *)msk, ssk, subflow); |
Paolo Abeni | 0e4f35d | 2020-10-09 19:00:01 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2289 | static bool mptcp_check_close_timeout(const struct sock *sk) |
| 2290 | { |
| 2291 | s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; |
| 2292 | struct mptcp_subflow_context *subflow; |
| 2293 | |
| 2294 | if (delta >= TCP_TIMEWAIT_LEN) |
| 2295 | return true; |
| 2296 | |
| 2297 | /* if all subflows are in closed status don't bother with additional |
| 2298 | * timeout |
| 2299 | */ |
| 2300 | mptcp_for_each_subflow(mptcp_sk(sk), subflow) { |
| 2301 | if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != |
| 2302 | TCP_CLOSE) |
| 2303 | return false; |
| 2304 | } |
| 2305 | return true; |
| 2306 | } |
| 2307 | |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2308 | static void mptcp_check_fastclose(struct mptcp_sock *msk) |
| 2309 | { |
| 2310 | struct mptcp_subflow_context *subflow, *tmp; |
| 2311 | struct sock *sk = &msk->sk.icsk_inet.sk; |
| 2312 | |
| 2313 | if (likely(!READ_ONCE(msk->rcv_fastclose))) |
| 2314 | return; |
| 2315 | |
| 2316 | mptcp_token_destroy(msk); |
| 2317 | |
| 2318 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 2319 | struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 2320 | bool slow; |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2321 | |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 2322 | slow = lock_sock_fast(tcp_sk); |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2323 | if (tcp_sk->sk_state != TCP_CLOSE) { |
| 2324 | tcp_send_active_reset(tcp_sk, GFP_ATOMIC); |
| 2325 | tcp_set_state(tcp_sk, TCP_CLOSE); |
| 2326 | } |
Paolo Abeni | 75e908c | 2021-06-21 15:54:34 -0700 | [diff] [blame] | 2327 | unlock_sock_fast(tcp_sk, slow); |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | inet_sk_state_store(sk, TCP_CLOSE); |
| 2331 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 2332 | smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2333 | set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); |
| 2334 | |
| 2335 | mptcp_close_wake_up(sk); |
| 2336 | } |
| 2337 | |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2338 | static void __mptcp_retrans(struct sock *sk) |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2339 | { |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2340 | struct mptcp_sock *msk = mptcp_sk(sk); |
Paolo Abeni | caf971d | 2020-11-16 10:48:06 +0100 | [diff] [blame] | 2341 | struct mptcp_sendmsg_info info = {}; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2342 | struct mptcp_data_frag *dfrag; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2343 | size_t copied = 0; |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2344 | struct sock *ssk; |
| 2345 | int ret; |
| 2346 | |
Paolo Abeni | b5941f0 | 2021-05-27 16:31:37 -0700 | [diff] [blame] | 2347 | mptcp_clean_una_wakeup(sk); |
Florian Westphal | 3241a9c | 2021-09-24 14:12:38 -0700 | [diff] [blame] | 2348 | |
| 2349 | /* first check ssk: need to kick "stale" logic */ |
| 2350 | ssk = mptcp_subflow_get_retrans(msk); |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2351 | dfrag = mptcp_rtx_head(sk); |
Mat Martineau | 6477dd3 | 2021-04-23 09:40:33 -0700 | [diff] [blame] | 2352 | if (!dfrag) { |
| 2353 | if (mptcp_data_fin_enabled(msk)) { |
| 2354 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 2355 | |
| 2356 | icsk->icsk_retransmits++; |
| 2357 | mptcp_set_datafin_timeout(sk); |
| 2358 | mptcp_send_ack(msk); |
| 2359 | |
| 2360 | goto reset_timer; |
| 2361 | } |
| 2362 | |
Florian Westphal | 3241a9c | 2021-09-24 14:12:38 -0700 | [diff] [blame] | 2363 | if (!mptcp_send_head(sk)) |
| 2364 | return; |
| 2365 | |
| 2366 | goto reset_timer; |
Mat Martineau | 6477dd3 | 2021-04-23 09:40:33 -0700 | [diff] [blame] | 2367 | } |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2368 | |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2369 | if (!ssk) |
| 2370 | goto reset_timer; |
| 2371 | |
| 2372 | lock_sock(ssk); |
| 2373 | |
| 2374 | /* limit retransmission to the bytes already sent on some subflows */ |
| 2375 | info.sent = 0; |
Paolo Abeni | 4e14867 | 2021-06-17 16:46:17 -0700 | [diff] [blame] | 2376 | info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; |
| 2377 | while (info.sent < info.limit) { |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2378 | ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); |
| 2379 | if (ret <= 0) |
| 2380 | break; |
| 2381 | |
| 2382 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); |
| 2383 | copied += ret; |
| 2384 | info.sent += ret; |
| 2385 | } |
Paolo Abeni | 4e14867 | 2021-06-17 16:46:17 -0700 | [diff] [blame] | 2386 | if (copied) { |
| 2387 | dfrag->already_sent = max(dfrag->already_sent, info.sent); |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2388 | tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, |
| 2389 | info.size_goal); |
Paolo Abeni | 4e14867 | 2021-06-17 16:46:17 -0700 | [diff] [blame] | 2390 | } |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2391 | |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2392 | release_sock(ssk); |
| 2393 | |
| 2394 | reset_timer: |
Florian Westphal | 3241a9c | 2021-09-24 14:12:38 -0700 | [diff] [blame] | 2395 | mptcp_check_and_set_pending(sk); |
| 2396 | |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2397 | if (!mptcp_timer_pending(sk)) |
| 2398 | mptcp_reset_timer(sk); |
| 2399 | } |
| 2400 | |
| 2401 | static void mptcp_worker(struct work_struct *work) |
| 2402 | { |
| 2403 | struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); |
| 2404 | struct sock *sk = &msk->sk.icsk_inet.sk; |
| 2405 | int state; |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2406 | |
| 2407 | lock_sock(sk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2408 | state = sk->sk_state; |
| 2409 | if (unlikely(state == TCP_CLOSE)) |
| 2410 | goto unlock; |
| 2411 | |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 2412 | mptcp_check_data_fin_ack(sk); |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 2413 | mptcp_flush_join_list(msk); |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 2414 | |
| 2415 | mptcp_check_fastclose(msk); |
| 2416 | |
Florian Westphal | b416268 | 2020-07-07 14:40:48 +0200 | [diff] [blame] | 2417 | if (msk->pm.status) |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 2418 | mptcp_pm_nl_work(msk); |
Florian Westphal | b416268 | 2020-07-07 14:40:48 +0200 | [diff] [blame] | 2419 | |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 2420 | if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) |
| 2421 | mptcp_check_for_eof(msk); |
| 2422 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2423 | __mptcp_check_send_data_fin(sk); |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 2424 | mptcp_check_data_fin(sk); |
| 2425 | |
Paolo Abeni | 341c652 | 2021-02-19 18:35:37 +0100 | [diff] [blame] | 2426 | /* There is no point in keeping around an orphaned sk timedout or |
| 2427 | * closed, but we need the msk around to reply to incoming DATA_FIN, |
| 2428 | * even if it is orphaned and in FIN_WAIT2 state |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2429 | */ |
| 2430 | if (sock_flag(sk, SOCK_DEAD) && |
Paolo Abeni | 341c652 | 2021-02-19 18:35:37 +0100 | [diff] [blame] | 2431 | (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2432 | inet_sk_state_store(sk, TCP_CLOSE); |
| 2433 | __mptcp_destroy_sock(sk); |
| 2434 | goto unlock; |
| 2435 | } |
| 2436 | |
Florian Westphal | b263b0d | 2021-02-12 15:59:57 -0800 | [diff] [blame] | 2437 | if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) |
| 2438 | __mptcp_close_subflow(msk); |
| 2439 | |
Paolo Abeni | 2948d0a | 2021-03-04 13:32:13 -0800 | [diff] [blame] | 2440 | if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) |
| 2441 | __mptcp_retrans(sk); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 2442 | |
| 2443 | unlock: |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2444 | release_sock(sk); |
| 2445 | sock_put(sk); |
| 2446 | } |
| 2447 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 2448 | static int __mptcp_init_sock(struct sock *sk) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2449 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2450 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2451 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 2452 | spin_lock_init(&msk->join_list_lock); |
| 2453 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2454 | INIT_LIST_HEAD(&msk->conn_list); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 2455 | INIT_LIST_HEAD(&msk->join_list); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2456 | INIT_LIST_HEAD(&msk->rtx_queue); |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2457 | INIT_WORK(&msk->work, mptcp_worker); |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2458 | __skb_queue_head_init(&msk->receive_queue); |
Paolo Abeni | ab174ad | 2020-09-14 10:01:12 +0200 | [diff] [blame] | 2459 | msk->out_of_order_queue = RB_ROOT; |
Paolo Abeni | f0e6a4c | 2020-11-16 10:48:07 +0100 | [diff] [blame] | 2460 | msk->first_pending = NULL; |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 2461 | msk->rmem_fwd_alloc = 0; |
Paolo Abeni | ce599c5 | 2021-07-09 17:20:51 -0700 | [diff] [blame] | 2462 | WRITE_ONCE(msk->rmem_released, 0); |
Paolo Abeni | 33d41c9 | 2021-08-13 15:15:41 -0700 | [diff] [blame] | 2463 | msk->timer_ival = TCP_RTO_MIN; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2464 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 2465 | msk->first = NULL; |
Paolo Abeni | dc24f8b | 2020-02-26 12:19:03 +0100 | [diff] [blame] | 2466 | inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; |
Geliang Tang | 752e906 | 2021-06-17 16:46:07 -0700 | [diff] [blame] | 2467 | WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); |
Paolo Abeni | 1e1d9d6 | 2021-08-13 15:15:43 -0700 | [diff] [blame] | 2468 | msk->recovery = false; |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 2469 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 2470 | mptcp_pm_data_init(msk); |
| 2471 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 2472 | /* re-use the csk retrans timer for MPTCP-level retrans */ |
| 2473 | timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2474 | timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); |
Florian Westphal | aa1fbd9 | 2021-04-15 16:45:01 -0700 | [diff] [blame] | 2475 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
| 2478 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 2479 | static int mptcp_init_sock(struct sock *sk) |
| 2480 | { |
Paolo Abeni | 20b5759 | 2021-05-25 14:23:10 -0700 | [diff] [blame] | 2481 | struct inet_connection_sock *icsk = inet_csk(sk); |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 2482 | struct net *net = sock_net(sk); |
| 2483 | int ret; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2484 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 2485 | ret = __mptcp_init_sock(sk); |
| 2486 | if (ret) |
| 2487 | return ret; |
| 2488 | |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 2489 | if (!mptcp_is_enabled(net)) |
| 2490 | return -ENOPROTOOPT; |
| 2491 | |
| 2492 | if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) |
| 2493 | return -ENOMEM; |
| 2494 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 2495 | ret = __mptcp_socket_create(mptcp_sk(sk)); |
| 2496 | if (ret) |
| 2497 | return ret; |
| 2498 | |
Paolo Abeni | 20b5759 | 2021-05-25 14:23:10 -0700 | [diff] [blame] | 2499 | /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will |
| 2500 | * propagate the correct value |
| 2501 | */ |
| 2502 | tcp_assign_congestion_control(sk); |
| 2503 | strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); |
| 2504 | |
| 2505 | /* no need to keep a reference to the ops, the name will suffice */ |
| 2506 | tcp_cleanup_congestion_control(sk); |
| 2507 | icsk->icsk_ca_ops = NULL; |
| 2508 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 2509 | sk_sockets_allocated_inc(sk); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 2510 | sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; |
Paolo Abeni | da51aef | 2020-09-14 10:01:10 +0200 | [diff] [blame] | 2511 | sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 2512 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | static void __mptcp_clear_xmit(struct sock *sk) |
| 2517 | { |
| 2518 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2519 | struct mptcp_data_frag *dtmp, *dfrag; |
| 2520 | |
Paolo Abeni | d9ca1de | 2020-11-16 10:48:10 +0100 | [diff] [blame] | 2521 | WRITE_ONCE(msk->first_pending, NULL); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2522 | list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 2523 | dfrag_clear(sk, dfrag); |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 2524 | } |
| 2525 | |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2526 | static void mptcp_cancel_work(struct sock *sk) |
| 2527 | { |
| 2528 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2529 | |
Paolo Abeni | b2771d2 | 2020-11-19 11:45:54 -0800 | [diff] [blame] | 2530 | if (cancel_work_sync(&msk->work)) |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2531 | __sock_put(sk); |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2532 | } |
| 2533 | |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 2534 | void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2535 | { |
| 2536 | lock_sock(ssk); |
| 2537 | |
| 2538 | switch (ssk->sk_state) { |
| 2539 | case TCP_LISTEN: |
| 2540 | if (!(how & RCV_SHUTDOWN)) |
| 2541 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2542 | fallthrough; |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2543 | case TCP_SYN_SENT: |
| 2544 | tcp_disconnect(ssk, O_NONBLOCK); |
| 2545 | break; |
| 2546 | default: |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 2547 | if (__mptcp_check_fallback(mptcp_sk(sk))) { |
| 2548 | pr_debug("Fallback"); |
| 2549 | ssk->sk_shutdown |= how; |
| 2550 | tcp_shutdown(ssk, how); |
| 2551 | } else { |
| 2552 | pr_debug("Sending DATA_FIN on subflow %p", ssk); |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 2553 | tcp_send_ack(ssk); |
Mat Martineau | 6477dd3 | 2021-04-23 09:40:33 -0700 | [diff] [blame] | 2554 | if (!mptcp_timer_pending(sk)) |
| 2555 | mptcp_reset_timer(sk); |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 2556 | } |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2557 | break; |
| 2558 | } |
| 2559 | |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2560 | release_sock(ssk); |
| 2561 | } |
| 2562 | |
Mat Martineau | 6920b85 | 2020-07-28 15:12:04 -0700 | [diff] [blame] | 2563 | static const unsigned char new_state[16] = { |
| 2564 | /* current state: new state: action: */ |
| 2565 | [0 /* (Invalid) */] = TCP_CLOSE, |
| 2566 | [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, |
| 2567 | [TCP_SYN_SENT] = TCP_CLOSE, |
| 2568 | [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, |
| 2569 | [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, |
| 2570 | [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, |
| 2571 | [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ |
| 2572 | [TCP_CLOSE] = TCP_CLOSE, |
| 2573 | [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, |
| 2574 | [TCP_LAST_ACK] = TCP_LAST_ACK, |
| 2575 | [TCP_LISTEN] = TCP_CLOSE, |
| 2576 | [TCP_CLOSING] = TCP_CLOSING, |
| 2577 | [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ |
| 2578 | }; |
| 2579 | |
| 2580 | static int mptcp_close_state(struct sock *sk) |
| 2581 | { |
| 2582 | int next = (int)new_state[sk->sk_state]; |
| 2583 | int ns = next & TCP_STATE_MASK; |
| 2584 | |
| 2585 | inet_sk_state_store(sk, ns); |
| 2586 | |
| 2587 | return next & TCP_ACTION_FIN; |
| 2588 | } |
| 2589 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2590 | static void __mptcp_check_send_data_fin(struct sock *sk) |
| 2591 | { |
| 2592 | struct mptcp_subflow_context *subflow; |
| 2593 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2594 | |
| 2595 | pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", |
| 2596 | msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), |
| 2597 | msk->snd_nxt, msk->write_seq); |
| 2598 | |
| 2599 | /* we still need to enqueue subflows or not really shutting down, |
| 2600 | * skip this |
| 2601 | */ |
| 2602 | if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || |
| 2603 | mptcp_send_head(sk)) |
| 2604 | return; |
| 2605 | |
| 2606 | WRITE_ONCE(msk->snd_nxt, msk->write_seq); |
| 2607 | |
Paolo Abeni | 26aa231 | 2020-11-19 11:45:55 -0800 | [diff] [blame] | 2608 | /* fallback socket will not get data_fin/ack, can move to the next |
| 2609 | * state now |
| 2610 | */ |
| 2611 | if (__mptcp_check_fallback(msk)) { |
| 2612 | if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { |
| 2613 | inet_sk_state_store(sk, TCP_CLOSE); |
| 2614 | mptcp_close_wake_up(sk); |
| 2615 | } else if (sk->sk_state == TCP_FIN_WAIT1) { |
| 2616 | inet_sk_state_store(sk, TCP_FIN_WAIT2); |
| 2617 | } |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2618 | } |
| 2619 | |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 2620 | mptcp_flush_join_list(msk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2621 | mptcp_for_each_subflow(msk, subflow) { |
| 2622 | struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); |
| 2623 | |
| 2624 | mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | static void __mptcp_wr_shutdown(struct sock *sk) |
| 2629 | { |
| 2630 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2631 | |
| 2632 | pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", |
| 2633 | msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, |
| 2634 | !!mptcp_send_head(sk)); |
| 2635 | |
| 2636 | /* will be ignored by fallback sockets */ |
| 2637 | WRITE_ONCE(msk->write_seq, msk->write_seq + 1); |
| 2638 | WRITE_ONCE(msk->snd_data_fin_enable, 1); |
| 2639 | |
| 2640 | __mptcp_check_send_data_fin(sk); |
| 2641 | } |
| 2642 | |
| 2643 | static void __mptcp_destroy_sock(struct sock *sk) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2644 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2645 | struct mptcp_subflow_context *subflow, *tmp; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2646 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 2647 | LIST_HEAD(conn_list); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2648 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2649 | pr_debug("msk=%p", msk); |
Florian Westphal | 2c22c06 | 2020-02-04 18:12:30 +0100 | [diff] [blame] | 2650 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 2651 | might_sleep(); |
| 2652 | |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 2653 | /* be sure to always acquire the join list lock, to sync vs |
| 2654 | * mptcp_finish_join(). |
| 2655 | */ |
| 2656 | spin_lock_bh(&msk->join_list_lock); |
| 2657 | list_splice_tail_init(&msk->join_list, &msk->conn_list); |
| 2658 | spin_unlock_bh(&msk->join_list_lock); |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 2659 | list_splice_init(&msk->conn_list, &conn_list); |
| 2660 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2661 | sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2662 | sk_stop_timer(sk, &sk->sk_timer); |
| 2663 | msk->pm.status = 0; |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 2664 | |
| 2665 | list_for_each_entry_safe(subflow, tmp, &conn_list, node) { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 2666 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2667 | __mptcp_close_ssk(sk, ssk, subflow); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2668 | } |
| 2669 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2670 | sk->sk_prot->destroy(sk); |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 2671 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 2672 | WARN_ON_ONCE(msk->rmem_fwd_alloc); |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2673 | WARN_ON_ONCE(msk->rmem_released); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2674 | sk_stream_kill_queues(sk); |
| 2675 | xfrm_sk_free_policy(sk); |
Florian Westphal | aa1fbd9 | 2021-04-15 16:45:01 -0700 | [diff] [blame] | 2676 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2677 | sk_refcnt_debug_release(sk); |
Florian Westphal | 17aee05 | 2021-03-04 13:32:11 -0800 | [diff] [blame] | 2678 | mptcp_dispose_initial_subflow(msk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2679 | sock_put(sk); |
| 2680 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 2681 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2682 | static void mptcp_close(struct sock *sk, long timeout) |
| 2683 | { |
| 2684 | struct mptcp_subflow_context *subflow; |
| 2685 | bool do_cancel_work = false; |
| 2686 | |
| 2687 | lock_sock(sk); |
| 2688 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 2689 | |
| 2690 | if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { |
| 2691 | inet_sk_state_store(sk, TCP_CLOSE); |
| 2692 | goto cleanup; |
| 2693 | } |
| 2694 | |
| 2695 | if (mptcp_close_state(sk)) |
| 2696 | __mptcp_wr_shutdown(sk); |
| 2697 | |
| 2698 | sk_stream_wait_close(sk, timeout); |
| 2699 | |
| 2700 | cleanup: |
| 2701 | /* orphan all the subflows */ |
| 2702 | inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; |
Geliang Tang | 4422791 | 2021-04-16 15:38:08 -0700 | [diff] [blame] | 2703 | mptcp_for_each_subflow(mptcp_sk(sk), subflow) { |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2704 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Paolo Abeni | 4905455 | 2021-09-29 11:59:17 +0200 | [diff] [blame] | 2705 | bool slow = lock_sock_fast_nested(ssk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2706 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2707 | sock_orphan(ssk); |
| 2708 | unlock_sock_fast(ssk, slow); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2709 | } |
| 2710 | sock_orphan(sk); |
| 2711 | |
| 2712 | sock_hold(sk); |
| 2713 | pr_debug("msk=%p state=%d", sk, sk->sk_state); |
| 2714 | if (sk->sk_state == TCP_CLOSE) { |
| 2715 | __mptcp_destroy_sock(sk); |
| 2716 | do_cancel_work = true; |
| 2717 | } else { |
| 2718 | sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); |
| 2719 | } |
| 2720 | release_sock(sk); |
| 2721 | if (do_cancel_work) |
| 2722 | mptcp_cancel_work(sk); |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 2723 | |
| 2724 | if (mptcp_sk(sk)->token) |
| 2725 | mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); |
| 2726 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 2727 | sock_put(sk); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2728 | } |
| 2729 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2730 | static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) |
| 2731 | { |
| 2732 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 2733 | const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); |
| 2734 | struct ipv6_pinfo *msk6 = inet6_sk(msk); |
| 2735 | |
| 2736 | msk->sk_v6_daddr = ssk->sk_v6_daddr; |
| 2737 | msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; |
| 2738 | |
| 2739 | if (msk6 && ssk6) { |
| 2740 | msk6->saddr = ssk6->saddr; |
| 2741 | msk6->flow_label = ssk6->flow_label; |
| 2742 | } |
| 2743 | #endif |
| 2744 | |
| 2745 | inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; |
| 2746 | inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; |
| 2747 | inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; |
| 2748 | inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; |
| 2749 | inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; |
| 2750 | inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; |
| 2751 | } |
| 2752 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2753 | static int mptcp_disconnect(struct sock *sk, int flags) |
| 2754 | { |
Paolo Abeni | 76e2a55 | 2021-01-12 18:25:24 +0100 | [diff] [blame] | 2755 | struct mptcp_subflow_context *subflow; |
| 2756 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2757 | |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 2758 | mptcp_do_flush_join_list(msk); |
| 2759 | |
Paolo Abeni | 13a9499 | 2021-01-14 16:37:37 +0100 | [diff] [blame] | 2760 | mptcp_for_each_subflow(msk, subflow) { |
| 2761 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 2762 | |
| 2763 | lock_sock(ssk); |
| 2764 | tcp_disconnect(ssk, flags); |
| 2765 | release_sock(ssk); |
| 2766 | } |
Florian Westphal | 42c556f | 2020-04-29 20:43:20 +0200 | [diff] [blame] | 2767 | return 0; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2770 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 2771 | static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) |
| 2772 | { |
| 2773 | unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); |
| 2774 | |
| 2775 | return (struct ipv6_pinfo *)(((u8 *)sk) + offset); |
| 2776 | } |
| 2777 | #endif |
| 2778 | |
Paolo Abeni | fca5c82 | 2020-04-20 16:25:06 +0200 | [diff] [blame] | 2779 | struct sock *mptcp_sk_clone(const struct sock *sk, |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 2780 | const struct mptcp_options_received *mp_opt, |
Paolo Abeni | fca5c82 | 2020-04-20 16:25:06 +0200 | [diff] [blame] | 2781 | struct request_sock *req) |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2782 | { |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2783 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2784 | struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2785 | struct mptcp_sock *msk; |
| 2786 | u64 ack_seq; |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2787 | |
| 2788 | if (!nsk) |
| 2789 | return NULL; |
| 2790 | |
| 2791 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 2792 | if (nsk->sk_family == AF_INET6) |
| 2793 | inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); |
| 2794 | #endif |
| 2795 | |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2796 | __mptcp_init_sock(nsk); |
| 2797 | |
| 2798 | msk = mptcp_sk(nsk); |
| 2799 | msk->local_key = subflow_req->local_key; |
| 2800 | msk->token = subflow_req->token; |
| 2801 | msk->subflow = NULL; |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 2802 | WRITE_ONCE(msk->fully_established, false); |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 2803 | if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) |
Geliang Tang | 0625118 | 2021-06-17 16:46:13 -0700 | [diff] [blame] | 2804 | WRITE_ONCE(msk->csum_enabled, true); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2805 | |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2806 | msk->write_seq = subflow_req->idsn + 1; |
Paolo Abeni | eaa2ffa | 2020-11-16 10:48:08 +0100 | [diff] [blame] | 2807 | msk->snd_nxt = msk->write_seq; |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 2808 | msk->snd_una = msk->write_seq; |
| 2809 | msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 2810 | msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 2811 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 2812 | if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2813 | msk->can_ack = true; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 2814 | msk->remote_key = mp_opt->sndr_key; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2815 | mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); |
| 2816 | ack_seq++; |
Mat Martineau | 917944d | 2020-09-29 15:08:19 -0700 | [diff] [blame] | 2817 | WRITE_ONCE(msk->ack_seq, ack_seq); |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 2818 | WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2819 | } |
Paolo Abeni | 7f20d5f | 2020-03-17 15:53:34 +0100 | [diff] [blame] | 2820 | |
Florian Westphal | 5e20087 | 2020-04-20 16:25:04 +0200 | [diff] [blame] | 2821 | sock_reset_flag(nsk, SOCK_RCU_FREE); |
Paolo Abeni | 7f20d5f | 2020-03-17 15:53:34 +0100 | [diff] [blame] | 2822 | /* will be fully established after successful MPC subflow creation */ |
| 2823 | inet_sk_state_store(nsk, TCP_SYN_RECV); |
Paolo Abeni | 0c14846 | 2020-12-16 12:48:32 +0100 | [diff] [blame] | 2824 | |
| 2825 | security_inet_csk_clone(nsk, req); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2826 | bh_unlock_sock(nsk); |
| 2827 | |
| 2828 | /* keep a single reference */ |
| 2829 | __sock_put(nsk); |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2830 | return nsk; |
| 2831 | } |
| 2832 | |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 2833 | void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) |
| 2834 | { |
| 2835 | const struct tcp_sock *tp = tcp_sk(ssk); |
| 2836 | |
| 2837 | msk->rcvq_space.copied = 0; |
| 2838 | msk->rcvq_space.rtt_us = 0; |
| 2839 | |
| 2840 | msk->rcvq_space.time = tp->tcp_mstamp; |
| 2841 | |
| 2842 | /* initial rcv_space offering made to peer */ |
| 2843 | msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, |
| 2844 | TCP_INIT_CWND * tp->advmss); |
| 2845 | if (msk->rcvq_space.space == 0) |
| 2846 | msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 2847 | |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 2848 | WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2851 | static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, |
| 2852 | bool kern) |
| 2853 | { |
| 2854 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2855 | struct socket *listener; |
| 2856 | struct sock *newsk; |
| 2857 | |
| 2858 | listener = __mptcp_nmpc_socket(msk); |
| 2859 | if (WARN_ON_ONCE(!listener)) { |
| 2860 | *err = -EINVAL; |
| 2861 | return NULL; |
| 2862 | } |
| 2863 | |
| 2864 | pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); |
| 2865 | newsk = inet_csk_accept(listener->sk, flags, err, kern); |
| 2866 | if (!newsk) |
| 2867 | return NULL; |
| 2868 | |
| 2869 | pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2870 | if (sk_is_mptcp(newsk)) { |
| 2871 | struct mptcp_subflow_context *subflow; |
| 2872 | struct sock *new_mptcp_sock; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2873 | |
| 2874 | subflow = mptcp_subflow_ctx(newsk); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 2875 | new_mptcp_sock = subflow->conn; |
| 2876 | |
| 2877 | /* is_mptcp should be false if subflow->conn is missing, see |
| 2878 | * subflow_syn_recv_sock() |
| 2879 | */ |
| 2880 | if (WARN_ON_ONCE(!new_mptcp_sock)) { |
| 2881 | tcp_sk(newsk)->is_mptcp = 0; |
| 2882 | return newsk; |
| 2883 | } |
| 2884 | |
| 2885 | /* acquire the 2nd reference for the owning socket */ |
| 2886 | sock_hold(new_mptcp_sock); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2887 | newsk = new_mptcp_sock; |
Paolo Abeni | 0397c6d | 2020-11-19 11:45:58 -0800 | [diff] [blame] | 2888 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 2889 | } else { |
| 2890 | MPTCP_INC_STATS(sock_net(sk), |
| 2891 | MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | return newsk; |
| 2895 | } |
| 2896 | |
Geliang Tang | 5c8c164 | 2020-09-24 08:29:57 +0800 | [diff] [blame] | 2897 | void mptcp_destroy_common(struct mptcp_sock *msk) |
| 2898 | { |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2899 | struct sock *sk = (struct sock *)msk; |
| 2900 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2901 | __mptcp_clear_xmit(sk); |
| 2902 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2903 | /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ |
| 2904 | skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 2905 | __skb_queue_purge(&sk->sk_receive_queue); |
Geliang Tang | 5c8c164 | 2020-09-24 08:29:57 +0800 | [diff] [blame] | 2906 | skb_rbtree_purge(&msk->out_of_order_queue); |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 2907 | |
| 2908 | /* move all the rx fwd alloc into the sk_mem_reclaim_final in |
| 2909 | * inet_sock_destruct() will dispose it |
| 2910 | */ |
| 2911 | sk->sk_forward_alloc += msk->rmem_fwd_alloc; |
| 2912 | msk->rmem_fwd_alloc = 0; |
Geliang Tang | 5c8c164 | 2020-09-24 08:29:57 +0800 | [diff] [blame] | 2913 | mptcp_token_destroy(msk); |
| 2914 | mptcp_pm_free_anno_list(msk); |
| 2915 | } |
| 2916 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 2917 | static void mptcp_destroy(struct sock *sk) |
| 2918 | { |
Florian Westphal | c9fd9c5 | 2020-01-29 15:54:43 +0100 | [diff] [blame] | 2919 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 2920 | |
Geliang Tang | 5c8c164 | 2020-09-24 08:29:57 +0800 | [diff] [blame] | 2921 | mptcp_destroy_common(msk); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 2922 | sk_sockets_allocated_dec(sk); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 2923 | } |
| 2924 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2925 | void __mptcp_data_acked(struct sock *sk) |
| 2926 | { |
| 2927 | if (!sock_owned_by_user(sk)) |
| 2928 | __mptcp_clean_una(sk); |
| 2929 | else |
| 2930 | set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); |
| 2931 | |
| 2932 | if (mptcp_pending_data_fin_ack(sk)) |
| 2933 | mptcp_schedule_work(sk); |
| 2934 | } |
| 2935 | |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 2936 | void __mptcp_check_push(struct sock *sk, struct sock *ssk) |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2937 | { |
| 2938 | if (!mptcp_send_head(sk)) |
| 2939 | return; |
| 2940 | |
Paolo Abeni | 40dc941 | 2021-01-20 15:39:13 +0100 | [diff] [blame] | 2941 | if (!sock_owned_by_user(sk)) { |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 2942 | struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); |
| 2943 | |
| 2944 | if (xmit_ssk == ssk) |
Paolo Abeni | 40dc941 | 2021-01-20 15:39:13 +0100 | [diff] [blame] | 2945 | __mptcp_subflow_push_pending(sk, ssk); |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 2946 | else if (xmit_ssk) |
| 2947 | mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); |
Paolo Abeni | 40dc941 | 2021-01-20 15:39:13 +0100 | [diff] [blame] | 2948 | } else { |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2949 | set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); |
Paolo Abeni | 40dc941 | 2021-01-20 15:39:13 +0100 | [diff] [blame] | 2950 | } |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
Paolo Abeni | e93da92 | 2020-11-27 11:10:23 +0100 | [diff] [blame] | 2953 | /* processes deferred events and flush wmem */ |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 2954 | static void mptcp_release_cb(struct sock *sk) |
| 2955 | { |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2956 | for (;;) { |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2957 | unsigned long flags = 0; |
| 2958 | |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2959 | if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) |
Dan Carpenter | 2e5de7e | 2021-03-12 10:41:12 +0300 | [diff] [blame] | 2960 | flags |= BIT(MPTCP_PUSH_PENDING); |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2961 | if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) |
| 2962 | flags |= BIT(MPTCP_RETRANSMIT); |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2963 | if (!flags) |
| 2964 | break; |
| 2965 | |
| 2966 | /* the following actions acquire the subflow socket lock |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2967 | * |
| 2968 | * 1) can't be invoked in atomic scope |
| 2969 | * 2) must avoid ABBA deadlock with msk socket spinlock: the RX |
| 2970 | * datapath acquires the msk socket spinlock while helding |
| 2971 | * the subflow socket lock |
| 2972 | */ |
| 2973 | |
| 2974 | spin_unlock_bh(&sk->sk_lock.slock); |
Dan Carpenter | 2e5de7e | 2021-03-12 10:41:12 +0300 | [diff] [blame] | 2975 | if (flags & BIT(MPTCP_PUSH_PENDING)) |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2976 | __mptcp_push_pending(sk, 0); |
Paolo Abeni | 2d6f5a2 | 2021-03-26 11:26:30 -0700 | [diff] [blame] | 2977 | if (flags & BIT(MPTCP_RETRANSMIT)) |
| 2978 | __mptcp_retrans(sk); |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2979 | |
| 2980 | cond_resched(); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2981 | spin_lock_bh(&sk->sk_lock.slock); |
| 2982 | } |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2983 | |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 2984 | /* be sure to set the current sk state before tacking actions |
| 2985 | * depending on sk_state |
| 2986 | */ |
| 2987 | if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags)) |
| 2988 | __mptcp_set_connected(sk); |
Paolo Abeni | c2e6048 | 2021-03-04 13:32:14 -0800 | [diff] [blame] | 2989 | if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) |
Paolo Abeni | 417789d | 2021-03-04 13:32:15 -0800 | [diff] [blame] | 2990 | __mptcp_clean_una_wakeup(sk); |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 2991 | if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) |
| 2992 | __mptcp_error_report(sk); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 2993 | |
Paolo Abeni | 8795260 | 2020-11-27 11:10:24 +0100 | [diff] [blame] | 2994 | __mptcp_update_rmem(sk); |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 2997 | void mptcp_subflow_process_delegated(struct sock *ssk) |
| 2998 | { |
| 2999 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 3000 | struct sock *sk = subflow->conn; |
| 3001 | |
| 3002 | mptcp_data_lock(sk); |
| 3003 | if (!sock_owned_by_user(sk)) |
| 3004 | __mptcp_subflow_push_pending(sk, ssk); |
| 3005 | else |
| 3006 | set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); |
| 3007 | mptcp_data_unlock(sk); |
| 3008 | mptcp_subflow_delegated_done(subflow); |
| 3009 | } |
| 3010 | |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3011 | static int mptcp_hash(struct sock *sk) |
| 3012 | { |
| 3013 | /* should never be called, |
| 3014 | * we hash the TCP subflows not the master socket |
| 3015 | */ |
| 3016 | WARN_ON_ONCE(1); |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
| 3020 | static void mptcp_unhash(struct sock *sk) |
| 3021 | { |
| 3022 | /* called from sk_common_release(), but nothing to do here */ |
| 3023 | } |
| 3024 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3025 | static int mptcp_get_port(struct sock *sk, unsigned short snum) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3026 | { |
| 3027 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3028 | struct socket *ssock; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3029 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3030 | ssock = __mptcp_nmpc_socket(msk); |
| 3031 | pr_debug("msk=%p, subflow=%p", msk, ssock); |
| 3032 | if (WARN_ON_ONCE(!ssock)) |
| 3033 | return -EINVAL; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3034 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3035 | return inet_csk_get_port(ssock->sk, snum); |
| 3036 | } |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3037 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3038 | void mptcp_finish_connect(struct sock *ssk) |
| 3039 | { |
| 3040 | struct mptcp_subflow_context *subflow; |
| 3041 | struct mptcp_sock *msk; |
| 3042 | struct sock *sk; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 3043 | u64 ack_seq; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3044 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3045 | subflow = mptcp_subflow_ctx(ssk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3046 | sk = subflow->conn; |
| 3047 | msk = mptcp_sk(sk); |
| 3048 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 3049 | pr_debug("msk=%p, token=%u", sk, subflow->token); |
| 3050 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 3051 | mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); |
| 3052 | ack_seq++; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 3053 | subflow->map_seq = ack_seq; |
| 3054 | subflow->map_subflow_seq = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 3055 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3056 | /* the socket is not connected yet, no msk/subflow ops can access/race |
| 3057 | * accessing the field below |
| 3058 | */ |
| 3059 | WRITE_ONCE(msk->remote_key, subflow->remote_key); |
| 3060 | WRITE_ONCE(msk->local_key, subflow->local_key); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 3061 | WRITE_ONCE(msk->write_seq, subflow->idsn + 1); |
Paolo Abeni | eaa2ffa | 2020-11-16 10:48:08 +0100 | [diff] [blame] | 3062 | WRITE_ONCE(msk->snd_nxt, msk->write_seq); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 3063 | WRITE_ONCE(msk->ack_seq, ack_seq); |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 3064 | WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 3065 | WRITE_ONCE(msk->can_ack, 1); |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 3066 | WRITE_ONCE(msk->snd_una, msk->write_seq); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 3067 | |
Florian Westphal | 6c714f1 | 2021-02-12 15:59:58 -0800 | [diff] [blame] | 3068 | mptcp_pm_new_connection(msk, ssk, 0); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 3069 | |
| 3070 | mptcp_rcv_space_init(msk, ssk); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3071 | } |
| 3072 | |
Paolo Abeni | 866f26f | 2021-01-20 15:39:10 +0100 | [diff] [blame] | 3073 | void mptcp_sock_graft(struct sock *sk, struct socket *parent) |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3074 | { |
| 3075 | write_lock_bh(&sk->sk_callback_lock); |
| 3076 | rcu_assign_pointer(sk->sk_wq, &parent->wq); |
| 3077 | sk_set_socket(sk, parent); |
| 3078 | sk->sk_uid = SOCK_INODE(parent)->i_uid; |
| 3079 | write_unlock_bh(&sk->sk_callback_lock); |
| 3080 | } |
| 3081 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3082 | bool mptcp_finish_join(struct sock *ssk) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3083 | { |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3084 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3085 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
| 3086 | struct sock *parent = (void *)msk; |
| 3087 | struct socket *parent_sock; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 3088 | bool ret; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3089 | |
| 3090 | pr_debug("msk=%p, subflow=%p", msk, subflow); |
| 3091 | |
| 3092 | /* mptcp socket already closing? */ |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3093 | if (!mptcp_is_fully_established(parent)) { |
| 3094 | subflow->reset_reason = MPTCP_RST_EMPTCP; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3095 | return false; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3096 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3097 | |
| 3098 | if (!msk->pm.server_side) |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 3099 | goto out; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3100 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3101 | if (!mptcp_pm_allow_new_subflow(msk)) { |
| 3102 | subflow->reset_reason = MPTCP_RST_EPROHIBIT; |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3103 | return false; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3104 | } |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3105 | |
| 3106 | /* active connections are already on conn_list, and we can't acquire |
| 3107 | * msk lock here. |
| 3108 | * use the join list lock as synchronization point and double-check |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3109 | * msk status to avoid racing with __mptcp_destroy_sock() |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3110 | */ |
| 3111 | spin_lock_bh(&msk->join_list_lock); |
| 3112 | ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3113 | if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3114 | list_add_tail(&subflow->node, &msk->join_list); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3115 | sock_hold(ssk); |
| 3116 | } |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3117 | spin_unlock_bh(&msk->join_list_lock); |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3118 | if (!ret) { |
| 3119 | subflow->reset_reason = MPTCP_RST_EPROHIBIT; |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3120 | return false; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 3121 | } |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3122 | |
| 3123 | /* attach to msk socket only after we are sure he will deal with us |
| 3124 | * at close time |
| 3125 | */ |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3126 | parent_sock = READ_ONCE(parent->sk_socket); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 3127 | if (parent_sock && !ssk->sk_socket) |
| 3128 | mptcp_sock_graft(ssk, parent_sock); |
Mat Martineau | 917944d | 2020-09-29 15:08:19 -0700 | [diff] [blame] | 3129 | subflow->map_seq = READ_ONCE(msk->ack_seq); |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 3130 | out: |
| 3131 | mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 3132 | return true; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 3133 | } |
| 3134 | |
Paolo Abeni | 76e2a55 | 2021-01-12 18:25:24 +0100 | [diff] [blame] | 3135 | static void mptcp_shutdown(struct sock *sk, int how) |
| 3136 | { |
| 3137 | pr_debug("sk=%p, how=%d", sk, how); |
| 3138 | |
| 3139 | if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) |
| 3140 | __mptcp_wr_shutdown(sk); |
| 3141 | } |
| 3142 | |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 3143 | static int mptcp_forward_alloc_get(const struct sock *sk) |
| 3144 | { |
| 3145 | return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; |
| 3146 | } |
| 3147 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3148 | static struct proto mptcp_prot = { |
| 3149 | .name = "MPTCP", |
| 3150 | .owner = THIS_MODULE, |
| 3151 | .init = mptcp_init_sock, |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 3152 | .disconnect = mptcp_disconnect, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3153 | .close = mptcp_close, |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3154 | .accept = mptcp_accept, |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 3155 | .setsockopt = mptcp_setsockopt, |
| 3156 | .getsockopt = mptcp_getsockopt, |
Paolo Abeni | 76e2a55 | 2021-01-12 18:25:24 +0100 | [diff] [blame] | 3157 | .shutdown = mptcp_shutdown, |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 3158 | .destroy = mptcp_destroy, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3159 | .sendmsg = mptcp_sendmsg, |
| 3160 | .recvmsg = mptcp_recvmsg, |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 3161 | .release_cb = mptcp_release_cb, |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3162 | .hash = mptcp_hash, |
| 3163 | .unhash = mptcp_unhash, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 3164 | .get_port = mptcp_get_port, |
Paolo Abeni | 6511882 | 2021-10-26 16:29:15 -0700 | [diff] [blame] | 3165 | .forward_alloc_get = mptcp_forward_alloc_get, |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 3166 | .sockets_allocated = &mptcp_sockets_allocated, |
| 3167 | .memory_allocated = &tcp_memory_allocated, |
| 3168 | .memory_pressure = &tcp_memory_pressure, |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 3169 | .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), |
Paolo Abeni | 989ef49 | 2020-11-08 19:49:59 +0100 | [diff] [blame] | 3170 | .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 3171 | .sysctl_mem = sysctl_tcp_mem, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3172 | .obj_size = sizeof(struct mptcp_sock), |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3173 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3174 | .no_autobind = true, |
| 3175 | }; |
| 3176 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3177 | static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 3178 | { |
| 3179 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 3180 | struct socket *ssock; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3181 | int err; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3182 | |
| 3183 | lock_sock(sock->sk); |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 3184 | ssock = __mptcp_nmpc_socket(msk); |
| 3185 | if (!ssock) { |
| 3186 | err = -EINVAL; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3187 | goto unlock; |
| 3188 | } |
| 3189 | |
| 3190 | err = ssock->ops->bind(ssock, uaddr, addr_len); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3191 | if (!err) |
| 3192 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3193 | |
| 3194 | unlock: |
| 3195 | release_sock(sock->sk); |
| 3196 | return err; |
| 3197 | } |
| 3198 | |
Paolo Abeni | 0235d07 | 2020-07-23 13:02:31 +0200 | [diff] [blame] | 3199 | static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, |
| 3200 | struct mptcp_subflow_context *subflow) |
| 3201 | { |
| 3202 | subflow->request_mptcp = 0; |
| 3203 | __mptcp_do_fallback(msk); |
| 3204 | } |
| 3205 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3206 | static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, |
| 3207 | int addr_len, int flags) |
| 3208 | { |
| 3209 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3210 | struct mptcp_subflow_context *subflow; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3211 | struct socket *ssock; |
| 3212 | int err; |
| 3213 | |
| 3214 | lock_sock(sock->sk); |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 3215 | if (sock->state != SS_UNCONNECTED && msk->subflow) { |
| 3216 | /* pending connection or invalid state, let existing subflow |
| 3217 | * cope with that |
| 3218 | */ |
| 3219 | ssock = msk->subflow; |
| 3220 | goto do_connect; |
| 3221 | } |
| 3222 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 3223 | ssock = __mptcp_nmpc_socket(msk); |
| 3224 | if (!ssock) { |
| 3225 | err = -EINVAL; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3226 | goto unlock; |
| 3227 | } |
| 3228 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 3229 | mptcp_token_destroy(msk); |
| 3230 | inet_sk_state_store(sock->sk, TCP_SYN_SENT); |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3231 | subflow = mptcp_subflow_ctx(ssock->sk); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3232 | #ifdef CONFIG_TCP_MD5SIG |
| 3233 | /* no MPTCP if MD5SIG is enabled on this socket or we may run out of |
| 3234 | * TCP option space. |
| 3235 | */ |
| 3236 | if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) |
Paolo Abeni | 0235d07 | 2020-07-23 13:02:31 +0200 | [diff] [blame] | 3237 | mptcp_subflow_early_fallback(msk, subflow); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3238 | #endif |
Paolo Abeni | a16195e | 2021-04-01 16:19:41 -0700 | [diff] [blame] | 3239 | if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { |
| 3240 | MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); |
Paolo Abeni | 0235d07 | 2020-07-23 13:02:31 +0200 | [diff] [blame] | 3241 | mptcp_subflow_early_fallback(msk, subflow); |
Paolo Abeni | a16195e | 2021-04-01 16:19:41 -0700 | [diff] [blame] | 3242 | } |
Paolo Abeni | 5695eb8 | 2021-04-01 16:19:42 -0700 | [diff] [blame] | 3243 | if (likely(!__mptcp_check_fallback(msk))) |
| 3244 | MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3245 | |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 3246 | do_connect: |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3247 | err = ssock->ops->connect(ssock, uaddr, addr_len, flags); |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 3248 | sock->state = ssock->state; |
| 3249 | |
| 3250 | /* on successful connect, the msk state will be moved to established by |
| 3251 | * subflow_finish_connect() |
| 3252 | */ |
Matthieu Baerts | 367fe04 | 2020-07-27 12:24:33 +0200 | [diff] [blame] | 3253 | if (!err || err == -EINPROGRESS) |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 3254 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
| 3255 | else |
| 3256 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3257 | |
| 3258 | unlock: |
| 3259 | release_sock(sock->sk); |
| 3260 | return err; |
| 3261 | } |
| 3262 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3263 | static int mptcp_listen(struct socket *sock, int backlog) |
| 3264 | { |
| 3265 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 3266 | struct socket *ssock; |
| 3267 | int err; |
| 3268 | |
| 3269 | pr_debug("msk=%p", msk); |
| 3270 | |
| 3271 | lock_sock(sock->sk); |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 3272 | ssock = __mptcp_nmpc_socket(msk); |
| 3273 | if (!ssock) { |
| 3274 | err = -EINVAL; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3275 | goto unlock; |
| 3276 | } |
| 3277 | |
Paolo Abeni | fa68018 | 2020-06-29 22:26:23 +0200 | [diff] [blame] | 3278 | mptcp_token_destroy(msk); |
| 3279 | inet_sk_state_store(sock->sk, TCP_LISTEN); |
Florian Westphal | 5e20087 | 2020-04-20 16:25:04 +0200 | [diff] [blame] | 3280 | sock_set_flag(sock->sk, SOCK_RCU_FREE); |
| 3281 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3282 | err = ssock->ops->listen(ssock, backlog); |
| 3283 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
| 3284 | if (!err) |
| 3285 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
| 3286 | |
| 3287 | unlock: |
| 3288 | release_sock(sock->sk); |
| 3289 | return err; |
| 3290 | } |
| 3291 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3292 | static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, |
| 3293 | int flags, bool kern) |
| 3294 | { |
| 3295 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 3296 | struct socket *ssock; |
| 3297 | int err; |
| 3298 | |
| 3299 | pr_debug("msk=%p", msk); |
| 3300 | |
| 3301 | lock_sock(sock->sk); |
| 3302 | if (sock->sk->sk_state != TCP_LISTEN) |
| 3303 | goto unlock_fail; |
| 3304 | |
| 3305 | ssock = __mptcp_nmpc_socket(msk); |
| 3306 | if (!ssock) |
| 3307 | goto unlock_fail; |
| 3308 | |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3309 | clear_bit(MPTCP_DATA_READY, &msk->flags); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3310 | sock_hold(ssock->sk); |
| 3311 | release_sock(sock->sk); |
| 3312 | |
| 3313 | err = ssock->ops->accept(sock, newsock, flags, kern); |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 3314 | if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3315 | struct mptcp_sock *msk = mptcp_sk(newsock->sk); |
| 3316 | struct mptcp_subflow_context *subflow; |
Paolo Abeni | 0397c6d | 2020-11-19 11:45:58 -0800 | [diff] [blame] | 3317 | struct sock *newsk = newsock->sk; |
Paolo Abeni | 0397c6d | 2020-11-19 11:45:58 -0800 | [diff] [blame] | 3318 | |
Florian Westphal | 4d54cc3 | 2021-02-12 15:59:59 -0800 | [diff] [blame] | 3319 | lock_sock(newsk); |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 3320 | |
| 3321 | /* PM/worker can now acquire the first subflow socket |
| 3322 | * lock without racing with listener queue cleanup, |
| 3323 | * we can notify it, if needed. |
Florian Westphal | c8fe62f | 2021-03-04 13:32:12 -0800 | [diff] [blame] | 3324 | * |
| 3325 | * Even if remote has reset the initial subflow by now |
| 3326 | * the refcnt is still at least one. |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 3327 | */ |
| 3328 | subflow = mptcp_subflow_ctx(msk->first); |
| 3329 | list_add(&subflow->node, &msk->conn_list); |
| 3330 | sock_hold(msk->first); |
| 3331 | if (mptcp_is_fully_established(newsk)) |
Florian Westphal | 6c714f1 | 2021-02-12 15:59:58 -0800 | [diff] [blame] | 3332 | mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 3333 | |
Paolo Abeni | 0397c6d | 2020-11-19 11:45:58 -0800 | [diff] [blame] | 3334 | mptcp_copy_inaddrs(newsk, msk->first); |
| 3335 | mptcp_rcv_space_init(msk, msk->first); |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 3336 | mptcp_propagate_sndbuf(newsk, msk->first); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3337 | |
| 3338 | /* set ssk->sk_socket of accept()ed flows to mptcp socket. |
| 3339 | * This is needed so NOSPACE flag can be set from tcp stack. |
| 3340 | */ |
Florian Westphal | 7896248 | 2021-04-15 16:44:53 -0700 | [diff] [blame] | 3341 | mptcp_flush_join_list(msk); |
Geliang Tang | 190f8b0 | 2020-08-03 21:00:44 +0800 | [diff] [blame] | 3342 | mptcp_for_each_subflow(msk, subflow) { |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3343 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 3344 | |
| 3345 | if (!ssk->sk_socket) |
| 3346 | mptcp_sock_graft(ssk, newsock); |
| 3347 | } |
Florian Westphal | 4d54cc3 | 2021-02-12 15:59:59 -0800 | [diff] [blame] | 3348 | release_sock(newsk); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3349 | } |
| 3350 | |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3351 | if (inet_csk_listen_poll(ssock->sk)) |
| 3352 | set_bit(MPTCP_DATA_READY, &msk->flags); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 3353 | sock_put(ssock->sk); |
| 3354 | return err; |
| 3355 | |
| 3356 | unlock_fail: |
| 3357 | release_sock(sock->sk); |
| 3358 | return -EINVAL; |
| 3359 | } |
| 3360 | |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3361 | static __poll_t mptcp_check_readable(struct mptcp_sock *msk) |
| 3362 | { |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 3363 | /* Concurrent splices from sk_receive_queue into receive_queue will |
| 3364 | * always show at least one non-empty queue when checked in this order. |
| 3365 | */ |
| 3366 | if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && |
| 3367 | skb_queue_empty_lockless(&msk->receive_queue)) |
| 3368 | return 0; |
| 3369 | |
| 3370 | return EPOLLIN | EPOLLRDNORM; |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3373 | static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) |
| 3374 | { |
| 3375 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3376 | |
| 3377 | if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) |
Paolo Abeni | dd91341 | 2021-02-11 15:30:38 -0800 | [diff] [blame] | 3378 | return EPOLLOUT | EPOLLWRNORM; |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3379 | |
| 3380 | if (sk_stream_is_writeable(sk)) |
| 3381 | return EPOLLOUT | EPOLLWRNORM; |
| 3382 | |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 3383 | mptcp_set_nospace(sk); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 3384 | smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ |
| 3385 | if (sk_stream_is_writeable(sk)) |
| 3386 | return EPOLLOUT | EPOLLWRNORM; |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3387 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 3388 | return 0; |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3389 | } |
| 3390 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3391 | static __poll_t mptcp_poll(struct file *file, struct socket *sock, |
| 3392 | struct poll_table_struct *wait) |
| 3393 | { |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 3394 | struct sock *sk = sock->sk; |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 3395 | struct mptcp_sock *msk; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3396 | __poll_t mask = 0; |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3397 | int state; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3398 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 3399 | msk = mptcp_sk(sk); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 3400 | sock_poll_wait(file, sock, wait); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 3401 | |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3402 | state = inet_sk_state_load(sk); |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 3403 | pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3404 | if (state == TCP_LISTEN) |
Paolo Abeni | 612f71d | 2021-10-07 15:05:00 -0700 | [diff] [blame] | 3405 | return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0; |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3406 | |
| 3407 | if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { |
| 3408 | mask |= mptcp_check_readable(msk); |
Florian Westphal | 8edf086 | 2020-11-16 10:48:12 +0100 | [diff] [blame] | 3409 | mask |= mptcp_check_writeable(msk); |
Paolo Abeni | 8a05661 | 2020-06-29 22:26:25 +0200 | [diff] [blame] | 3410 | } |
Paolo Abeni | dd91341 | 2021-02-11 15:30:38 -0800 | [diff] [blame] | 3411 | if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) |
| 3412 | mask |= EPOLLHUP; |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 3413 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 3414 | mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
| 3415 | |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 3416 | /* This barrier is coupled with smp_wmb() in tcp_reset() */ |
| 3417 | smp_rmb(); |
| 3418 | if (sk->sk_err) |
| 3419 | mask |= EPOLLERR; |
| 3420 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3421 | return mask; |
| 3422 | } |
| 3423 | |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3424 | static const struct proto_ops mptcp_stream_ops = { |
| 3425 | .family = PF_INET, |
| 3426 | .owner = THIS_MODULE, |
Paolo Abeni | 0a3cc57 | 2021-04-01 18:57:45 +0200 | [diff] [blame] | 3427 | .release = inet_release, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3428 | .bind = mptcp_bind, |
| 3429 | .connect = mptcp_stream_connect, |
| 3430 | .socketpair = sock_no_socketpair, |
| 3431 | .accept = mptcp_stream_accept, |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 3432 | .getname = inet_getname, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3433 | .poll = mptcp_poll, |
| 3434 | .ioctl = inet_ioctl, |
| 3435 | .gettstamp = sock_gettstamp, |
| 3436 | .listen = mptcp_listen, |
Paolo Abeni | 76e2a55 | 2021-01-12 18:25:24 +0100 | [diff] [blame] | 3437 | .shutdown = inet_shutdown, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3438 | .setsockopt = sock_common_setsockopt, |
| 3439 | .getsockopt = sock_common_getsockopt, |
| 3440 | .sendmsg = inet_sendmsg, |
| 3441 | .recvmsg = inet_recvmsg, |
| 3442 | .mmap = sock_no_mmap, |
| 3443 | .sendpage = inet_sendpage, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3444 | }; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3445 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3446 | static struct inet_protosw mptcp_protosw = { |
| 3447 | .type = SOCK_STREAM, |
| 3448 | .protocol = IPPROTO_MPTCP, |
| 3449 | .prot = &mptcp_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3450 | .ops = &mptcp_stream_ops, |
| 3451 | .flags = INET_PROTOSW_ICSK, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3452 | }; |
| 3453 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 3454 | static int mptcp_napi_poll(struct napi_struct *napi, int budget) |
| 3455 | { |
| 3456 | struct mptcp_delegated_action *delegated; |
| 3457 | struct mptcp_subflow_context *subflow; |
| 3458 | int work_done = 0; |
| 3459 | |
| 3460 | delegated = container_of(napi, struct mptcp_delegated_action, napi); |
| 3461 | while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { |
| 3462 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 3463 | |
| 3464 | bh_lock_sock_nested(ssk); |
| 3465 | if (!sock_owned_by_user(ssk) && |
| 3466 | mptcp_subflow_has_delegated_action(subflow)) |
| 3467 | mptcp_subflow_process_delegated(ssk); |
| 3468 | /* ... elsewhere tcp_release_cb_override already processed |
| 3469 | * the action or will do at next release_sock(). |
| 3470 | * In both case must dequeue the subflow here - on the same |
| 3471 | * CPU that scheduled it. |
| 3472 | */ |
| 3473 | bh_unlock_sock(ssk); |
| 3474 | sock_put(ssk); |
| 3475 | |
| 3476 | if (++work_done == budget) |
| 3477 | return budget; |
| 3478 | } |
| 3479 | |
| 3480 | /* always provide a 0 'work_done' argument, so that napi_complete_done |
| 3481 | * will not try accessing the NULL napi->dev ptr |
| 3482 | */ |
| 3483 | napi_complete_done(napi, 0); |
| 3484 | return work_done; |
| 3485 | } |
| 3486 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 3487 | void __init mptcp_proto_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3488 | { |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 3489 | struct mptcp_delegated_action *delegated; |
| 3490 | int cpu; |
| 3491 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3492 | mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3493 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 3494 | if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) |
| 3495 | panic("Failed to allocate MPTCP pcpu counter\n"); |
| 3496 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 3497 | init_dummy_netdev(&mptcp_napi_dev); |
| 3498 | for_each_possible_cpu(cpu) { |
| 3499 | delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); |
| 3500 | INIT_LIST_HEAD(&delegated->head); |
| 3501 | netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, |
| 3502 | NAPI_POLL_WEIGHT); |
| 3503 | napi_enable(&delegated->napi); |
| 3504 | } |
| 3505 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3506 | mptcp_subflow_init(); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 3507 | mptcp_pm_init(); |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 3508 | mptcp_token_init(); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3509 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3510 | if (proto_register(&mptcp_prot, 1) != 0) |
| 3511 | panic("Failed to register MPTCP proto.\n"); |
| 3512 | |
| 3513 | inet_register_protosw(&mptcp_protosw); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 3514 | |
| 3515 | BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3519 | static const struct proto_ops mptcp_v6_stream_ops = { |
| 3520 | .family = PF_INET6, |
| 3521 | .owner = THIS_MODULE, |
Paolo Abeni | 0a3cc57 | 2021-04-01 18:57:45 +0200 | [diff] [blame] | 3522 | .release = inet6_release, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3523 | .bind = mptcp_bind, |
| 3524 | .connect = mptcp_stream_connect, |
| 3525 | .socketpair = sock_no_socketpair, |
| 3526 | .accept = mptcp_stream_accept, |
Paolo Abeni | d2f77c5 | 2020-06-29 22:26:22 +0200 | [diff] [blame] | 3527 | .getname = inet6_getname, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3528 | .poll = mptcp_poll, |
| 3529 | .ioctl = inet6_ioctl, |
| 3530 | .gettstamp = sock_gettstamp, |
| 3531 | .listen = mptcp_listen, |
Paolo Abeni | 76e2a55 | 2021-01-12 18:25:24 +0100 | [diff] [blame] | 3532 | .shutdown = inet_shutdown, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3533 | .setsockopt = sock_common_setsockopt, |
| 3534 | .getsockopt = sock_common_getsockopt, |
| 3535 | .sendmsg = inet6_sendmsg, |
| 3536 | .recvmsg = inet6_recvmsg, |
| 3537 | .mmap = sock_no_mmap, |
| 3538 | .sendpage = inet_sendpage, |
| 3539 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 3986912 | 2020-05-18 08:28:06 +0200 | [diff] [blame] | 3540 | .compat_ioctl = inet6_compat_ioctl, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 3541 | #endif |
| 3542 | }; |
| 3543 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3544 | static struct proto mptcp_v6_prot; |
| 3545 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 3546 | static void mptcp_v6_destroy(struct sock *sk) |
| 3547 | { |
| 3548 | mptcp_destroy(sk); |
| 3549 | inet6_destroy_sock(sk); |
| 3550 | } |
| 3551 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3552 | static struct inet_protosw mptcp_v6_protosw = { |
| 3553 | .type = SOCK_STREAM, |
| 3554 | .protocol = IPPROTO_MPTCP, |
| 3555 | .prot = &mptcp_v6_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 3556 | .ops = &mptcp_v6_stream_ops, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3557 | .flags = INET_PROTOSW_ICSK, |
| 3558 | }; |
| 3559 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 3560 | int __init mptcp_proto_v6_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3561 | { |
| 3562 | int err; |
| 3563 | |
| 3564 | mptcp_v6_prot = mptcp_prot; |
| 3565 | strcpy(mptcp_v6_prot.name, "MPTCPv6"); |
| 3566 | mptcp_v6_prot.slab = NULL; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 3567 | mptcp_v6_prot.destroy = mptcp_v6_destroy; |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 3568 | mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3569 | |
| 3570 | err = proto_register(&mptcp_v6_prot, 1); |
| 3571 | if (err) |
| 3572 | return err; |
| 3573 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 3574 | err = inet6_register_protosw(&mptcp_v6_protosw); |
| 3575 | if (err) |
| 3576 | proto_unregister(&mptcp_v6_prot); |
| 3577 | |
| 3578 | return err; |
| 3579 | } |
| 3580 | #endif |