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