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