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