blob: eb316bd578bb28c54db553f667bbf96eabf6cf30 [file] [log] [blame]
Mat Martineauf870fa02020-01-21 16:56:15 -08001// 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 Abeni7a6a6cb2020-01-21 16:56:26 -080012#include <linux/sched/signal.h>
13#include <linux/atomic.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080014#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 Martineau3721b9b2020-07-28 15:12:03 -070019#include <net/tcp_states.h>
Peter Krystadcf7da0d2020-01-21 16:56:19 -080020#if IS_ENABLED(CONFIG_MPTCP_IPV6)
21#include <net/transp_v6.h>
22#endif
Mat Martineauf870fa02020-01-21 16:56:15 -080023#include <net/mptcp.h>
Paolo Abenie16163b2020-11-16 10:48:09 +010024#include <net/xfrm.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080025#include "protocol.h"
Florian Westphalfc518952020-03-27 14:48:50 -070026#include "mib.h"
Mat Martineauf870fa02020-01-21 16:56:15 -080027
Geliang Tange10a9892021-04-16 15:38:04 -070028#define CREATE_TRACE_POINTS
29#include <trace/events/mptcp.h>
30
Florian Westphalb0519de2020-02-06 00:39:37 +010031#if IS_ENABLED(CONFIG_MPTCP_IPV6)
32struct mptcp6_sock {
33 struct mptcp_sock msk;
34 struct ipv6_pinfo np;
35};
36#endif
37
Florian Westphal6771bfd2020-02-26 10:14:48 +010038struct mptcp_skb_cb {
Paolo Abeniab174ad2020-09-14 10:01:12 +020039 u64 map_seq;
40 u64 end_seq;
Florian Westphal6771bfd2020-02-26 10:14:48 +010041 u32 offset;
Florian Westphalb7f653b2021-06-03 16:24:32 -070042 u8 has_rxtstamp:1;
Florian Westphal6771bfd2020-02-26 10:14:48 +010043};
44
45#define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
46
Florian Westphalb7f653b2021-06-03 16:24:32 -070047enum {
48 MPTCP_CMSG_TS = BIT(0),
49};
50
Paolo Abenid0272362020-03-27 14:48:45 -070051static struct percpu_counter mptcp_sockets_allocated;
52
Paolo Abenie16163b2020-11-16 10:48:09 +010053static void __mptcp_destroy_sock(struct sock *sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +010054static void __mptcp_check_send_data_fin(struct sock *sk);
Paolo Abenie16163b2020-11-16 10:48:09 +010055
Paolo Abenib19bc292021-01-20 15:39:14 +010056DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
57static struct net_device mptcp_napi_dev;
58
Peter Krystad2303f992020-01-21 16:56:17 -080059/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
60 * completed yet or has failed, return the subflow socket.
61 * Otherwise return NULL.
62 */
Geliang Tang1729cf12021-02-01 15:09:12 -080063struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -080064{
Christoph Paaschd22f4982020-01-21 16:56:32 -080065 if (!msk->subflow || READ_ONCE(msk->can_ack))
Peter Krystad2303f992020-01-21 16:56:17 -080066 return NULL;
67
68 return msk->subflow;
69}
70
Florian Westphal6f8a6122020-11-16 10:48:13 +010071/* Returns end sequence number of the receiver's advertised window */
72static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
73{
Paolo Abeni7439d682020-11-27 11:10:26 +010074 return READ_ONCE(msk->wnd_end);
Florian Westphal6f8a6122020-11-16 10:48:13 +010075}
76
Paolo Abenid2f77c52020-06-29 22:26:22 +020077static bool mptcp_is_tcpsk(struct sock *sk)
Florian Westphal0b4f33d2020-04-02 13:44:51 +020078{
79 struct socket *sock = sk->sk_socket;
80
Florian Westphal0b4f33d2020-04-02 13:44:51 +020081 if (unlikely(sk->sk_prot == &tcp_prot)) {
82 /* we are being invoked after mptcp_accept() has
83 * accepted a non-mp-capable flow: sk is a tcp_sk,
84 * not an mptcp one.
85 *
86 * Hand the socket over to tcp so all further socket ops
87 * bypass mptcp.
88 */
89 sock->ops = &inet_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020090 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020091#if IS_ENABLED(CONFIG_MPTCP_IPV6)
92 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
93 sock->ops = &inet6_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020094 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020095#endif
96 }
97
Paolo Abenid2f77c52020-06-29 22:26:22 +020098 return false;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020099}
100
Paolo Abenifa680182020-06-29 22:26:23 +0200101static int __mptcp_socket_create(struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -0800102{
103 struct mptcp_subflow_context *subflow;
104 struct sock *sk = (struct sock *)msk;
105 struct socket *ssock;
106 int err;
107
Peter Krystad2303f992020-01-21 16:56:17 -0800108 err = mptcp_subflow_create_socket(sk, &ssock);
109 if (err)
Paolo Abenifa680182020-06-29 22:26:23 +0200110 return err;
Peter Krystad2303f992020-01-21 16:56:17 -0800111
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800112 msk->first = ssock->sk;
Peter Krystad2303f992020-01-21 16:56:17 -0800113 msk->subflow = ssock;
114 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800115 list_add(&subflow->node, &msk->conn_list);
Paolo Abenie16163b2020-11-16 10:48:09 +0100116 sock_hold(ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -0800117 subflow->request_mptcp = 1;
Paolo Abeni866f26f2021-01-20 15:39:10 +0100118 mptcp_sock_graft(msk->first, sk->sk_socket);
Davide Carattie1ff9e82020-06-29 22:26:20 +0200119
Paolo Abenifa680182020-06-29 22:26:23 +0200120 return 0;
Peter Krystad2303f992020-01-21 16:56:17 -0800121}
122
Paolo Abeniab174ad2020-09-14 10:01:12 +0200123static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
124{
125 sk_drops_add(sk, skb);
126 __kfree_skb(skb);
127}
128
Paolo Abeni65118822021-10-26 16:29:15 -0700129static void mptcp_rmem_charge(struct sock *sk, int size)
130{
131 mptcp_sk(sk)->rmem_fwd_alloc -= size;
132}
133
Paolo Abeni8268ed42020-09-14 10:01:11 +0200134static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
135 struct sk_buff *from)
136{
137 bool fragstolen;
138 int delta;
139
140 if (MPTCP_SKB_CB(from)->offset ||
141 !skb_try_coalesce(to, from, &fragstolen, &delta))
142 return false;
143
Paolo Abeni06242e42020-09-14 10:01:14 +0200144 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
145 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
146 to->len, MPTCP_SKB_CB(from)->end_seq);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200147 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200148 kfree_skb_partial(from, fragstolen);
149 atomic_add(delta, &sk->sk_rmem_alloc);
Paolo Abeni65118822021-10-26 16:29:15 -0700150 mptcp_rmem_charge(sk, delta);
Paolo Abeni8268ed42020-09-14 10:01:11 +0200151 return true;
152}
153
Paolo Abeniab174ad2020-09-14 10:01:12 +0200154static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
155 struct sk_buff *from)
Florian Westphal6771bfd2020-02-26 10:14:48 +0100156{
Paolo Abeniab174ad2020-09-14 10:01:12 +0200157 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
158 return false;
159
160 return mptcp_try_coalesce((struct sock *)msk, to, from);
161}
162
Paolo Abeni65118822021-10-26 16:29:15 -0700163static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
164{
165 amount >>= SK_MEM_QUANTUM_SHIFT;
166 mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
167 __sk_mem_reduce_allocated(sk, amount);
168}
169
170static void mptcp_rmem_uncharge(struct sock *sk, int size)
171{
172 struct mptcp_sock *msk = mptcp_sk(sk);
173 int reclaimable;
174
175 msk->rmem_fwd_alloc += size;
176 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
177
178 /* see sk_mem_uncharge() for the rationale behind the following schema */
179 if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD))
180 __mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK);
181}
182
183static void mptcp_rfree(struct sk_buff *skb)
184{
185 unsigned int len = skb->truesize;
186 struct sock *sk = skb->sk;
187
188 atomic_sub(len, &sk->sk_rmem_alloc);
189 mptcp_rmem_uncharge(sk, len);
190}
191
192static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
193{
194 skb_orphan(skb);
195 skb->sk = sk;
196 skb->destructor = mptcp_rfree;
197 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
198 mptcp_rmem_charge(sk, skb->truesize);
199}
200
Paolo Abeniab174ad2020-09-14 10:01:12 +0200201/* "inspired" by tcp_data_queue_ofo(), main differences:
202 * - use mptcp seqs
203 * - don't cope with sacks
204 */
205static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
206{
207 struct sock *sk = (struct sock *)msk;
208 struct rb_node **p, *parent;
209 u64 seq, end_seq, max_seq;
210 struct sk_buff *skb1;
211
212 seq = MPTCP_SKB_CB(skb)->map_seq;
213 end_seq = MPTCP_SKB_CB(skb)->end_seq;
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800214 max_seq = READ_ONCE(msk->rcv_wnd_sent);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200215
Paolo Abeni06242e42020-09-14 10:01:14 +0200216 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
217 RB_EMPTY_ROOT(&msk->out_of_order_queue));
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800218 if (after64(end_seq, max_seq)) {
Paolo Abeniab174ad2020-09-14 10:01:12 +0200219 /* out of window */
220 mptcp_drop(sk, skb);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800221 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
222 (unsigned long long)end_seq - (unsigned long)max_seq,
223 (unsigned long long)msk->rcv_wnd_sent);
Paolo Abeni06242e42020-09-14 10:01:14 +0200224 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200225 return;
226 }
227
228 p = &msk->out_of_order_queue.rb_node;
Paolo Abeni06242e42020-09-14 10:01:14 +0200229 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200230 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
231 rb_link_node(&skb->rbnode, NULL, p);
232 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
233 msk->ooo_last_skb = skb;
234 goto end;
235 }
236
237 /* with 2 subflows, adding at end of ooo queue is quite likely
238 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
239 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200240 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
241 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
242 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200243 return;
Paolo Abeni06242e42020-09-14 10:01:14 +0200244 }
Paolo Abeniab174ad2020-09-14 10:01:12 +0200245
246 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
247 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200248 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200249 parent = &msk->ooo_last_skb->rbnode;
250 p = &parent->rb_right;
251 goto insert;
252 }
253
254 /* Find place to insert this segment. Handle overlaps on the way. */
255 parent = NULL;
256 while (*p) {
257 parent = *p;
258 skb1 = rb_to_skb(parent);
259 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
260 p = &parent->rb_left;
261 continue;
262 }
263 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
264 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
265 /* All the bits are present. Drop. */
266 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200267 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200268 return;
269 }
270 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
271 /* partial overlap:
272 * | skb |
273 * | skb1 |
274 * continue traversing
275 */
276 } else {
277 /* skb's seq == skb1's seq and skb covers skb1.
278 * Replace skb1 with skb.
279 */
280 rb_replace_node(&skb1->rbnode, &skb->rbnode,
281 &msk->out_of_order_queue);
282 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200283 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200284 goto merge_right;
285 }
286 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200287 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200288 return;
289 }
290 p = &parent->rb_right;
291 }
Paolo Abeni06242e42020-09-14 10:01:14 +0200292
Paolo Abeniab174ad2020-09-14 10:01:12 +0200293insert:
294 /* Insert segment into RB tree. */
295 rb_link_node(&skb->rbnode, parent, p);
296 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
297
298merge_right:
299 /* Remove other segments covered by skb. */
300 while ((skb1 = skb_rb_next(skb)) != NULL) {
301 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
302 break;
303 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
304 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200305 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200306 }
307 /* If there is no skb after us, we are the last_skb ! */
308 if (!skb1)
309 msk->ooo_last_skb = skb;
310
311end:
312 skb_condense(skb);
Paolo Abeni65118822021-10-26 16:29:15 -0700313 mptcp_set_owner_r(skb, sk);
314}
315
316static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
317{
318 struct mptcp_sock *msk = mptcp_sk(sk);
319 int amt, amount;
320
321 if (size < msk->rmem_fwd_alloc)
322 return true;
323
324 amt = sk_mem_pages(size);
325 amount = amt << SK_MEM_QUANTUM_SHIFT;
326 msk->rmem_fwd_alloc += amount;
327 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) {
328 if (ssk->sk_forward_alloc < amount) {
329 msk->rmem_fwd_alloc -= amount;
330 return false;
331 }
332
333 ssk->sk_forward_alloc -= amount;
334 }
335 return true;
Paolo Abeniab174ad2020-09-14 10:01:12 +0200336}
337
338static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
339 struct sk_buff *skb, unsigned int offset,
340 size_t copy_len)
341{
342 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100343 struct sock *sk = (struct sock *)msk;
Florian Westphal4e637c72020-05-25 23:41:13 +0200344 struct sk_buff *tail;
Florian Westphalb7f653b2021-06-03 16:24:32 -0700345 bool has_rxtstamp;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100346
347 __skb_unlink(skb, &ssk->sk_receive_queue);
Florian Westphal4e637c72020-05-25 23:41:13 +0200348
349 skb_ext_reset(skb);
350 skb_orphan(skb);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200351
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100352 /* try to fetch required memory from subflow */
Paolo Abeni65118822021-10-26 16:29:15 -0700353 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
354 goto drop;
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100355
Florian Westphalb7f653b2021-06-03 16:24:32 -0700356 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
357
Paolo Abeniab174ad2020-09-14 10:01:12 +0200358 /* the skb map_seq accounts for the skb offset:
359 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
360 * value
361 */
362 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
363 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200364 MPTCP_SKB_CB(skb)->offset = offset;
Florian Westphalb7f653b2021-06-03 16:24:32 -0700365 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
Florian Westphal4e637c72020-05-25 23:41:13 +0200366
Paolo Abeniab174ad2020-09-14 10:01:12 +0200367 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
368 /* in sequence */
David S. Miller8b0308f2020-10-05 17:33:26 -0700369 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200370 tail = skb_peek_tail(&sk->sk_receive_queue);
371 if (tail && mptcp_try_coalesce(sk, tail, skb))
372 return true;
Florian Westphal4e637c72020-05-25 23:41:13 +0200373
Paolo Abeni65118822021-10-26 16:29:15 -0700374 mptcp_set_owner_r(skb, sk);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200375 __skb_queue_tail(&sk->sk_receive_queue, skb);
376 return true;
377 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
378 mptcp_data_queue_ofo(msk, skb);
379 return false;
380 }
381
382 /* old data, keep it simple and drop the whole pkt, sender
383 * will retransmit as needed, if needed.
384 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200385 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100386drop:
Paolo Abeniab174ad2020-09-14 10:01:12 +0200387 mptcp_drop(sk, skb);
388 return false;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100389}
390
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700391static void mptcp_stop_timer(struct sock *sk)
392{
393 struct inet_connection_sock *icsk = inet_csk(sk);
394
395 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
396 mptcp_sk(sk)->timer_ival = 0;
397}
398
Paolo Abenie16163b2020-11-16 10:48:09 +0100399static void mptcp_close_wake_up(struct sock *sk)
400{
401 if (sock_flag(sk, SOCK_DEAD))
402 return;
403
404 sk->sk_state_change(sk);
405 if (sk->sk_shutdown == SHUTDOWN_MASK ||
406 sk->sk_state == TCP_CLOSE)
407 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
408 else
409 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
410}
411
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100412static bool mptcp_pending_data_fin_ack(struct sock *sk)
413{
414 struct mptcp_sock *msk = mptcp_sk(sk);
415
416 return !__mptcp_check_fallback(msk) &&
417 ((1 << sk->sk_state) &
418 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
419 msk->write_seq == READ_ONCE(msk->snd_una);
420}
421
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700422static void mptcp_check_data_fin_ack(struct sock *sk)
423{
424 struct mptcp_sock *msk = mptcp_sk(sk);
425
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700426 /* Look for an acknowledged DATA_FIN */
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100427 if (mptcp_pending_data_fin_ack(sk)) {
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700428 WRITE_ONCE(msk->snd_data_fin_enable, 0);
429
430 switch (sk->sk_state) {
431 case TCP_FIN_WAIT1:
432 inet_sk_state_store(sk, TCP_FIN_WAIT2);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700433 break;
434 case TCP_CLOSING:
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700435 case TCP_LAST_ACK:
436 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700437 break;
438 }
439
Paolo Abenie16163b2020-11-16 10:48:09 +0100440 mptcp_close_wake_up(sk);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700441 }
442}
443
Mat Martineau3721b9b2020-07-28 15:12:03 -0700444static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
445{
446 struct mptcp_sock *msk = mptcp_sk(sk);
447
448 if (READ_ONCE(msk->rcv_data_fin) &&
449 ((1 << sk->sk_state) &
450 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
451 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
452
453 if (msk->ack_seq == rcv_data_fin_seq) {
454 if (seq)
455 *seq = rcv_data_fin_seq;
456
457 return true;
458 }
459 }
460
461 return false;
462}
463
Mat Martineau6477dd32021-04-23 09:40:33 -0700464static void mptcp_set_datafin_timeout(const struct sock *sk)
465{
466 struct inet_connection_sock *icsk = inet_csk(sk);
467
468 mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX,
469 TCP_RTO_MIN << icsk->icsk_retransmits);
470}
471
Paolo Abeni33d41c92021-08-13 15:15:41 -0700472static void __mptcp_set_timeout(struct sock *sk, long tout)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700473{
Mat Martineau3721b9b2020-07-28 15:12:03 -0700474 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
475}
476
Paolo Abeni33d41c92021-08-13 15:15:41 -0700477static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
478{
479 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
480
Paolo Abeni71b7dec2021-08-13 15:15:42 -0700481 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
482 inet_csk(ssk)->icsk_timeout - jiffies : 0;
Paolo Abeni33d41c92021-08-13 15:15:41 -0700483}
484
485static void mptcp_set_timeout(struct sock *sk)
486{
487 struct mptcp_subflow_context *subflow;
488 long tout = 0;
489
490 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
491 tout = max(tout, mptcp_timeout_from_subflow(subflow));
492 __mptcp_set_timeout(sk, tout);
493}
494
Paolo Abenifd897672020-11-24 22:51:24 +0100495static bool tcp_can_send_ack(const struct sock *ssk)
496{
497 return !((1 << inet_sk_state_load(ssk)) &
Paolo Abeni20bc80b2021-01-12 18:25:23 +0100498 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
Paolo Abenifd897672020-11-24 22:51:24 +0100499}
500
Mat Martineau340fa662021-09-02 11:51:19 -0700501void mptcp_subflow_send_ack(struct sock *ssk)
502{
503 bool slow;
504
505 slow = lock_sock_fast(ssk);
506 if (tcp_can_send_ack(ssk))
507 tcp_send_ack(ssk);
508 unlock_sock_fast(ssk, slow);
509}
510
Paolo Abenifd897672020-11-24 22:51:24 +0100511static void mptcp_send_ack(struct mptcp_sock *msk)
Paolo Abeni7ed90802020-11-16 10:48:14 +0100512{
513 struct mptcp_subflow_context *subflow;
514
Mat Martineau340fa662021-09-02 11:51:19 -0700515 mptcp_for_each_subflow(msk, subflow)
516 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
Paolo Abenifd897672020-11-24 22:51:24 +0100517}
Paolo Abeniea4ca582020-11-19 11:46:03 -0800518
Paolo Abenifde56ee2021-06-22 12:25:23 -0700519static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
Paolo Abenifd897672020-11-24 22:51:24 +0100520{
Paolo Abeni75e908c2021-06-21 15:54:34 -0700521 bool slow;
Paolo Abenifd897672020-11-24 22:51:24 +0100522
Paolo Abeni75e908c2021-06-21 15:54:34 -0700523 slow = lock_sock_fast(ssk);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700524 if (tcp_can_send_ack(ssk))
Paolo Abenifd897672020-11-24 22:51:24 +0100525 tcp_cleanup_rbuf(ssk, 1);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700526 unlock_sock_fast(ssk, slow);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700527}
528
529static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
530{
531 const struct inet_connection_sock *icsk = inet_csk(ssk);
Matthieu Baertsc4512c62021-06-25 14:25:22 -0700532 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700533 const struct tcp_sock *tp = tcp_sk(ssk);
534
535 return (ack_pending & ICSK_ACK_SCHED) &&
536 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
537 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
538 (rx_empty && ack_pending &
539 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
Paolo Abenifd897672020-11-24 22:51:24 +0100540}
541
542static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
543{
Paolo Abenie3859602021-02-11 15:30:41 -0800544 int old_space = READ_ONCE(msk->old_wspace);
Paolo Abenifd897672020-11-24 22:51:24 +0100545 struct mptcp_subflow_context *subflow;
Paolo Abenie3859602021-02-11 15:30:41 -0800546 struct sock *sk = (struct sock *)msk;
Paolo Abenifde56ee2021-06-22 12:25:23 -0700547 int space = __mptcp_space(sk);
548 bool cleanup, rx_empty;
Paolo Abenie3859602021-02-11 15:30:41 -0800549
Paolo Abenifde56ee2021-06-22 12:25:23 -0700550 cleanup = (space > 0) && (space >= (old_space << 1));
Paolo Abenice599c52021-07-09 17:20:51 -0700551 rx_empty = !__mptcp_rmem(sk);
Paolo Abenifd897672020-11-24 22:51:24 +0100552
Paolo Abenifde56ee2021-06-22 12:25:23 -0700553 mptcp_for_each_subflow(msk, subflow) {
554 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenifd897672020-11-24 22:51:24 +0100555
Paolo Abenifde56ee2021-06-22 12:25:23 -0700556 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
557 mptcp_subflow_cleanup_rbuf(ssk);
Paolo Abeniea4ca582020-11-19 11:46:03 -0800558 }
Paolo Abeni7ed90802020-11-16 10:48:14 +0100559}
560
561static bool mptcp_check_data_fin(struct sock *sk)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700562{
563 struct mptcp_sock *msk = mptcp_sk(sk);
564 u64 rcv_data_fin_seq;
Paolo Abeni7ed90802020-11-16 10:48:14 +0100565 bool ret = false;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700566
Paolo Abeni781bf132021-04-01 16:19:43 -0700567 if (__mptcp_check_fallback(msk))
Paolo Abeni7ed90802020-11-16 10:48:14 +0100568 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700569
570 /* Need to ack a DATA_FIN received from a peer while this side
571 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
572 * msk->rcv_data_fin was set when parsing the incoming options
573 * at the subflow level and the msk lock was not held, so this
574 * is the first opportunity to act on the DATA_FIN and change
575 * the msk state.
576 *
577 * If we are caught up to the sequence number of the incoming
578 * DATA_FIN, send the DATA_ACK now and do state transition. If
579 * not caught up, do nothing and let the recv code send DATA_ACK
580 * when catching up.
581 */
582
583 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
Mat Martineau917944d2020-09-29 15:08:19 -0700584 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700585 WRITE_ONCE(msk->rcv_data_fin, 0);
586
587 sk->sk_shutdown |= RCV_SHUTDOWN;
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700588 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Mat Martineau3721b9b2020-07-28 15:12:03 -0700589
590 switch (sk->sk_state) {
591 case TCP_ESTABLISHED:
592 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
593 break;
594 case TCP_FIN_WAIT1:
595 inet_sk_state_store(sk, TCP_CLOSING);
596 break;
597 case TCP_FIN_WAIT2:
598 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700599 break;
600 default:
601 /* Other states not expected */
602 WARN_ON_ONCE(1);
603 break;
604 }
605
Paolo Abeni7ed90802020-11-16 10:48:14 +0100606 ret = true;
Paolo Abenifd897672020-11-24 22:51:24 +0100607 mptcp_send_ack(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +0100608 mptcp_close_wake_up(sk);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700609 }
Paolo Abeni7ed90802020-11-16 10:48:14 +0100610 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700611}
612
Florian Westphal6771bfd2020-02-26 10:14:48 +0100613static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
614 struct sock *ssk,
615 unsigned int *bytes)
616{
617 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100618 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100619 unsigned int moved = 0;
620 bool more_data_avail;
621 struct tcp_sock *tp;
622 bool done = false;
Florian Westphal13c7ba02020-11-03 11:05:03 -0800623 int sk_rbuf;
624
625 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
626
627 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
628 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
629
630 if (unlikely(ssk_rbuf > sk_rbuf)) {
631 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
632 sk_rbuf = ssk_rbuf;
633 }
634 }
Florian Westphal600911f2020-02-26 10:14:49 +0100635
Paolo Abeniab174ad2020-09-14 10:01:12 +0200636 pr_debug("msk=%p ssk=%p", msk, ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100637 tp = tcp_sk(ssk);
638 do {
639 u32 map_remaining, offset;
640 u32 seq = tp->copied_seq;
641 struct sk_buff *skb;
642 bool fin;
643
644 /* try to move as much data as available */
645 map_remaining = subflow->map_data_len -
646 mptcp_subflow_get_map_offset(subflow);
647
648 skb = skb_peek(&ssk->sk_receive_queue);
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200649 if (!skb) {
650 /* if no data is found, a racing workqueue/recvmsg
651 * already processed the new data, stop here or we
652 * can enter an infinite loop
653 */
654 if (!moved)
655 done = true;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100656 break;
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200657 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100658
Davide Carattie1ff9e82020-06-29 22:26:20 +0200659 if (__mptcp_check_fallback(msk)) {
660 /* if we are running under the workqueue, TCP could have
661 * collapsed skbs between dummy map creation and now
662 * be sure to adjust the size
663 */
664 map_remaining = skb->len;
665 subflow->map_data_len = skb->len;
666 }
667
Florian Westphal6771bfd2020-02-26 10:14:48 +0100668 offset = seq - TCP_SKB_CB(skb)->seq;
669 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
670 if (fin) {
671 done = true;
672 seq++;
673 }
674
675 if (offset < skb->len) {
676 size_t len = skb->len - offset;
677
678 if (tp->urg_data)
679 done = true;
680
Paolo Abeniab174ad2020-09-14 10:01:12 +0200681 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
682 moved += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100683 seq += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100684
685 if (WARN_ON_ONCE(map_remaining < len))
686 break;
687 } else {
688 WARN_ON_ONCE(!fin);
689 sk_eat_skb(ssk, skb);
690 done = true;
691 }
692
693 WRITE_ONCE(tp->copied_seq, seq);
694 more_data_avail = mptcp_subflow_data_available(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100695
Florian Westphal13c7ba02020-11-03 11:05:03 -0800696 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
Florian Westphal600911f2020-02-26 10:14:49 +0100697 done = true;
698 break;
699 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100700 } while (more_data_avail);
701
Paolo Abeni67193312020-09-14 10:01:09 +0200702 *bytes += moved;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100703 return done;
704}
705
Paolo Abeni87952602020-11-27 11:10:24 +0100706static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
Paolo Abeniab174ad2020-09-14 10:01:12 +0200707{
708 struct sock *sk = (struct sock *)msk;
709 struct sk_buff *skb, *tail;
710 bool moved = false;
711 struct rb_node *p;
712 u64 end_seq;
713
714 p = rb_first(&msk->out_of_order_queue);
Paolo Abeni06242e42020-09-14 10:01:14 +0200715 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
Paolo Abeniab174ad2020-09-14 10:01:12 +0200716 while (p) {
717 skb = rb_to_skb(p);
718 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
719 break;
720
721 p = rb_next(p);
722 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
723
724 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
725 msk->ack_seq))) {
726 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200727 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200728 continue;
729 }
730
731 end_seq = MPTCP_SKB_CB(skb)->end_seq;
732 tail = skb_peek_tail(&sk->sk_receive_queue);
733 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
734 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
735
736 /* skip overlapping data, if any */
Paolo Abeni06242e42020-09-14 10:01:14 +0200737 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
738 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
739 delta);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200740 MPTCP_SKB_CB(skb)->offset += delta;
741 __skb_queue_tail(&sk->sk_receive_queue, skb);
742 }
743 msk->ack_seq = end_seq;
744 moved = true;
745 }
746 return moved;
747}
748
Florian Westphal2e522132020-02-26 10:14:51 +0100749/* In most cases we will be able to lock the mptcp socket. If its already
750 * owned, we need to defer to the work queue to avoid ABBA deadlock.
751 */
Paolo Abeni99d10552021-06-10 15:59:41 -0700752static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
Florian Westphal2e522132020-02-26 10:14:51 +0100753{
754 struct sock *sk = (struct sock *)msk;
755 unsigned int moved = 0;
756
Paolo Abeni87952602020-11-27 11:10:24 +0100757 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
758 __mptcp_ofo_queue(msk);
Paolo Abeni499ada52021-06-10 15:59:44 -0700759 if (unlikely(ssk->sk_err)) {
760 if (!sock_owned_by_user(sk))
761 __mptcp_error_report(sk);
762 else
763 set_bit(MPTCP_ERROR_REPORT, &msk->flags);
764 }
Paolo Abeniab174ad2020-09-14 10:01:12 +0200765
Paolo Abeni87952602020-11-27 11:10:24 +0100766 /* If the moves have caught up with the DATA_FIN sequence number
767 * it's time to ack the DATA_FIN and change socket state, but
768 * this is not a good place to change state. Let the workqueue
769 * do it.
770 */
771 if (mptcp_pending_data_fin(sk, NULL))
772 mptcp_schedule_work(sk);
Paolo Abeni99d10552021-06-10 15:59:41 -0700773 return moved > 0;
Florian Westphal2e522132020-02-26 10:14:51 +0100774}
775
776void mptcp_data_ready(struct sock *sk, struct sock *ssk)
Florian Westphal101f6f82020-02-26 10:14:46 +0100777{
Paolo Abeni67193312020-09-14 10:01:09 +0200778 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100779 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal13c7ba02020-11-03 11:05:03 -0800780 int sk_rbuf, ssk_rbuf;
Florian Westphal101f6f82020-02-26 10:14:46 +0100781
Paolo Abenid7b1bfd2020-12-09 12:03:31 +0100782 /* The peer can send data while we are shutting down this
783 * subflow at msk destruction time, but we must avoid enqueuing
784 * more data to the msk receive queue
785 */
786 if (unlikely(subflow->disposable))
787 return;
788
Florian Westphal13c7ba02020-11-03 11:05:03 -0800789 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
790 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
791 if (unlikely(ssk_rbuf > sk_rbuf))
792 sk_rbuf = ssk_rbuf;
793
Paolo Abeni99d10552021-06-10 15:59:41 -0700794 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
Paolo Abenice599c52021-07-09 17:20:51 -0700795 if (__mptcp_rmem(sk) > sk_rbuf) {
796 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
Paolo Abeni99d10552021-06-10 15:59:41 -0700797 return;
Paolo Abenice599c52021-07-09 17:20:51 -0700798 }
Florian Westphal2e522132020-02-26 10:14:51 +0100799
Paolo Abeni99d10552021-06-10 15:59:41 -0700800 /* Wake-up the reader only for in-sequence data */
801 mptcp_data_lock(sk);
Paolo Abeni612f71d2021-10-07 15:05:00 -0700802 if (move_skbs_to_msk(msk, ssk))
Paolo Abeni67193312020-09-14 10:01:09 +0200803 sk->sk_data_ready(sk);
Paolo Abeni612f71d2021-10-07 15:05:00 -0700804
Paolo Abeni99d10552021-06-10 15:59:41 -0700805 mptcp_data_unlock(sk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100806}
807
Florian Westphal78962482021-04-15 16:44:53 -0700808static bool mptcp_do_flush_join_list(struct mptcp_sock *msk)
Peter Krystadec3edaa2020-03-27 14:48:40 -0700809{
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100810 struct mptcp_subflow_context *subflow;
Florian Westphaldf00b082021-04-15 16:44:54 -0700811 bool ret = false;
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100812
Peter Krystadec3edaa2020-03-27 14:48:40 -0700813 if (likely(list_empty(&msk->join_list)))
Florian Westphal78962482021-04-15 16:44:53 -0700814 return false;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700815
816 spin_lock_bh(&msk->join_list_lock);
Florian Westphaldf00b082021-04-15 16:44:54 -0700817 list_for_each_entry(subflow, &msk->join_list, node) {
818 u32 sseq = READ_ONCE(subflow->setsockopt_seq);
Florian Westphal78962482021-04-15 16:44:53 -0700819
Florian Westphaldf00b082021-04-15 16:44:54 -0700820 mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow));
821 if (READ_ONCE(msk->setsockopt_seq) != sseq)
822 ret = true;
823 }
Peter Krystadec3edaa2020-03-27 14:48:40 -0700824 list_splice_tail_init(&msk->join_list, &msk->conn_list);
825 spin_unlock_bh(&msk->join_list_lock);
Florian Westphal78962482021-04-15 16:44:53 -0700826
Florian Westphaldf00b082021-04-15 16:44:54 -0700827 return ret;
Florian Westphal78962482021-04-15 16:44:53 -0700828}
829
830void __mptcp_flush_join_list(struct mptcp_sock *msk)
831{
832 if (likely(!mptcp_do_flush_join_list(msk)))
833 return;
834
835 if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags))
836 mptcp_schedule_work((struct sock *)msk);
837}
838
839static void mptcp_flush_join_list(struct mptcp_sock *msk)
840{
841 bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags);
842
843 might_sleep();
844
845 if (!mptcp_do_flush_join_list(msk) && !sync_needed)
846 return;
847
848 mptcp_sockopt_sync_all(msk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700849}
850
Paolo Abenib51f9b82020-03-27 14:48:44 -0700851static bool mptcp_timer_pending(struct sock *sk)
852{
853 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
854}
855
856static void mptcp_reset_timer(struct sock *sk)
857{
858 struct inet_connection_sock *icsk = inet_csk(sk);
859 unsigned long tout;
860
Paolo Abenie16163b2020-11-16 10:48:09 +0100861 /* prevent rescheduling on close */
862 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
863 return;
864
Paolo Abeni33d41c92021-08-13 15:15:41 -0700865 tout = mptcp_sk(sk)->timer_ival;
Paolo Abenib51f9b82020-03-27 14:48:44 -0700866 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
867}
868
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100869bool mptcp_schedule_work(struct sock *sk)
870{
871 if (inet_sk_state_load(sk) != TCP_CLOSE &&
872 schedule_work(&mptcp_sk(sk)->work)) {
873 /* each subflow already holds a reference to the sk, and the
874 * workqueue is invoked by a subflow, so sk can't go away here.
875 */
876 sock_hold(sk);
877 return true;
878 }
879 return false;
880}
881
Florian Westphal59832e22020-04-02 13:44:52 +0200882void mptcp_subflow_eof(struct sock *sk)
883{
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100884 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
885 mptcp_schedule_work(sk);
Florian Westphal59832e22020-04-02 13:44:52 +0200886}
887
Paolo Abeni59698562020-06-10 10:47:41 +0200888static void mptcp_check_for_eof(struct mptcp_sock *msk)
889{
890 struct mptcp_subflow_context *subflow;
891 struct sock *sk = (struct sock *)msk;
892 int receivers = 0;
893
894 mptcp_for_each_subflow(msk, subflow)
895 receivers += !subflow->rx_eof;
Paolo Abenie16163b2020-11-16 10:48:09 +0100896 if (receivers)
897 return;
Paolo Abeni59698562020-06-10 10:47:41 +0200898
Paolo Abenie16163b2020-11-16 10:48:09 +0100899 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
Paolo Abeni59698562020-06-10 10:47:41 +0200900 /* hopefully temporary hack: propagate shutdown status
901 * to msk, when all subflows agree on it
902 */
903 sk->sk_shutdown |= RCV_SHUTDOWN;
904
905 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Paolo Abeni59698562020-06-10 10:47:41 +0200906 sk->sk_data_ready(sk);
907 }
Paolo Abenie16163b2020-11-16 10:48:09 +0100908
909 switch (sk->sk_state) {
910 case TCP_ESTABLISHED:
911 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
912 break;
913 case TCP_FIN_WAIT1:
Paolo Abeni26aa2312020-11-19 11:45:55 -0800914 inet_sk_state_store(sk, TCP_CLOSING);
915 break;
916 case TCP_FIN_WAIT2:
Paolo Abenie16163b2020-11-16 10:48:09 +0100917 inet_sk_state_store(sk, TCP_CLOSE);
918 break;
919 default:
920 return;
921 }
922 mptcp_close_wake_up(sk);
Paolo Abeni59698562020-06-10 10:47:41 +0200923}
924
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800925static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
926{
927 struct mptcp_subflow_context *subflow;
928 struct sock *sk = (struct sock *)msk;
929
930 sock_owned_by_me(sk);
931
932 mptcp_for_each_subflow(msk, subflow) {
Paolo Abeni99d10552021-06-10 15:59:41 -0700933 if (READ_ONCE(subflow->data_avail))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800934 return mptcp_subflow_tcp_sock(subflow);
935 }
936
937 return NULL;
938}
939
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700940static bool mptcp_skb_can_collapse_to(u64 write_seq,
941 const struct sk_buff *skb,
942 const struct mptcp_ext *mpext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800943{
Paolo Abeni57040752020-01-21 16:56:27 -0800944 if (!tcp_skb_can_collapse_to(skb))
945 return false;
946
Paolo Abeni5a369ca2020-11-03 11:05:05 -0800947 /* can collapse only if MPTCP level sequence is in order and this
948 * mapping has not been xmitted yet
949 */
950 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
951 !mpext->frozen;
Paolo Abeni57040752020-01-21 16:56:27 -0800952}
953
Paolo Abeni29249ea2021-05-11 19:13:51 +0200954/* we can append data to the given data frag if:
955 * - there is space available in the backing page_frag
956 * - the data frag tail matches the current page_frag free offset
957 * - the data frag end sequence number matches the current write seq
958 */
Paolo Abeni18b683b2020-03-27 14:48:43 -0700959static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
960 const struct page_frag *pfrag,
961 const struct mptcp_data_frag *df)
962{
963 return df && pfrag->page == df->page &&
Paolo Abenid9ca1de2020-11-16 10:48:10 +0100964 pfrag->size - pfrag->offset > 0 &&
Paolo Abeni29249ea2021-05-11 19:13:51 +0200965 pfrag->offset == (df->offset + df->data_len) &&
Paolo Abeni18b683b2020-03-27 14:48:43 -0700966 df->data_seq + df->data_len == msk->write_seq;
967}
968
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700969static void __mptcp_mem_reclaim_partial(struct sock *sk)
970{
Paolo Abeni65118822021-10-26 16:29:15 -0700971 int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
972
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700973 lockdep_assert_held_once(&sk->sk_lock.slock);
Paolo Abeni65118822021-10-26 16:29:15 -0700974
975 __mptcp_rmem_reclaim(sk, reclaimable - 1);
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700976 sk_mem_reclaim_partial(sk);
977}
978
Paolo Abeni724cfd22020-11-27 11:10:25 +0100979static void mptcp_mem_reclaim_partial(struct sock *sk)
980{
Paolo Abeni724cfd22020-11-27 11:10:25 +0100981 mptcp_data_lock(sk);
Paolo Abeni65118822021-10-26 16:29:15 -0700982 __mptcp_mem_reclaim_partial(sk);
Paolo Abeni724cfd22020-11-27 11:10:25 +0100983 mptcp_data_unlock(sk);
984}
985
Paolo Abenid0272362020-03-27 14:48:45 -0700986static void dfrag_uncharge(struct sock *sk, int len)
Paolo Abeni18b683b2020-03-27 14:48:43 -0700987{
Paolo Abenid0272362020-03-27 14:48:45 -0700988 sk_mem_uncharge(sk, len);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700989 sk_wmem_queued_add(sk, -len);
Paolo Abenid0272362020-03-27 14:48:45 -0700990}
991
992static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
993{
994 int len = dfrag->data_len + dfrag->overhead;
995
Paolo Abeni18b683b2020-03-27 14:48:43 -0700996 list_del(&dfrag->list);
Paolo Abenid0272362020-03-27 14:48:45 -0700997 dfrag_uncharge(sk, len);
Paolo Abeni18b683b2020-03-27 14:48:43 -0700998 put_page(dfrag->page);
999}
1000
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001001static void __mptcp_clean_una(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001002{
1003 struct mptcp_sock *msk = mptcp_sk(sk);
1004 struct mptcp_data_frag *dtmp, *dfrag;
Paolo Abenid0272362020-03-27 14:48:45 -07001005 bool cleaned = false;
Davide Carattie1ff9e82020-06-29 22:26:20 +02001006 u64 snd_una;
1007
1008 /* on fallback we just need to ignore snd_una, as this is really
1009 * plain TCP
1010 */
1011 if (__mptcp_check_fallback(msk))
Paolo Abeni7439d682020-11-27 11:10:26 +01001012 msk->snd_una = READ_ONCE(msk->snd_nxt);
1013
Paolo Abeni7439d682020-11-27 11:10:26 +01001014 snd_una = msk->snd_una;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001015 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1016 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1017 break;
1018
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001019 if (unlikely(dfrag == msk->first_pending)) {
1020 /* in recovery mode can see ack after the current snd head */
1021 if (WARN_ON_ONCE(!msk->recovery))
1022 break;
1023
1024 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1025 }
1026
Paolo Abenid0272362020-03-27 14:48:45 -07001027 dfrag_clear(sk, dfrag);
1028 cleaned = true;
1029 }
1030
Florian Westphal7948f6c2020-03-27 14:48:46 -07001031 dfrag = mptcp_rtx_head(sk);
1032 if (dfrag && after64(snd_una, dfrag->data_seq)) {
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001033 u64 delta = snd_una - dfrag->data_seq;
1034
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001035 /* prevent wrap around in recovery mode */
1036 if (unlikely(delta > dfrag->already_sent)) {
1037 if (WARN_ON_ONCE(!msk->recovery))
1038 goto out;
1039 if (WARN_ON_ONCE(delta > dfrag->data_len))
1040 goto out;
1041 dfrag->already_sent += delta - dfrag->already_sent;
1042 }
Florian Westphal7948f6c2020-03-27 14:48:46 -07001043
1044 dfrag->data_seq += delta;
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001045 dfrag->offset += delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001046 dfrag->data_len -= delta;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001047 dfrag->already_sent -= delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001048
1049 dfrag_uncharge(sk, delta);
1050 cleaned = true;
1051 }
1052
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001053 /* all retransmitted data acked, recovery completed */
1054 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1055 msk->recovery = false;
1056
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001057out:
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001058 if (cleaned && tcp_under_memory_pressure(sk))
1059 __mptcp_mem_reclaim_partial(sk);
Paolo Abeni63561a42020-09-14 10:01:07 +02001060
Florian Westphal3241a9c2021-09-24 14:12:38 -07001061 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1062 snd_una == READ_ONCE(msk->write_seq)) {
Paolo Abeni33d41c92021-08-13 15:15:41 -07001063 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001064 mptcp_stop_timer(sk);
1065 } else {
1066 mptcp_reset_timer(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001067 }
1068}
1069
Paolo Abeni417789d2021-03-04 13:32:15 -08001070static void __mptcp_clean_una_wakeup(struct sock *sk)
1071{
Paolo Abeni765ff422021-09-24 14:12:36 -07001072 lockdep_assert_held_once(&sk->sk_lock.slock);
1073
Paolo Abeni417789d2021-03-04 13:32:15 -08001074 __mptcp_clean_una(sk);
1075 mptcp_write_space(sk);
1076}
1077
Paolo Abenib5941f02021-05-27 16:31:37 -07001078static void mptcp_clean_una_wakeup(struct sock *sk)
1079{
1080 mptcp_data_lock(sk);
1081 __mptcp_clean_una_wakeup(sk);
1082 mptcp_data_unlock(sk);
1083}
1084
Paolo Abeni724cfd22020-11-27 11:10:25 +01001085static void mptcp_enter_memory_pressure(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001086{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001087 struct mptcp_subflow_context *subflow;
1088 struct mptcp_sock *msk = mptcp_sk(sk);
1089 bool first = true;
1090
Paolo Abeni18b683b2020-03-27 14:48:43 -07001091 sk_stream_moderate_sndbuf(sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001092 mptcp_for_each_subflow(msk, subflow) {
1093 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1094
1095 if (first)
1096 tcp_enter_memory_pressure(ssk);
1097 sk_stream_moderate_sndbuf(ssk);
1098 first = false;
1099 }
Paolo Abeni724cfd22020-11-27 11:10:25 +01001100}
1101
1102/* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1103 * data
1104 */
1105static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1106{
1107 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1108 pfrag, sk->sk_allocation)))
1109 return true;
1110
1111 mptcp_enter_memory_pressure(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001112 return false;
1113}
1114
1115static struct mptcp_data_frag *
1116mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1117 int orig_offset)
1118{
1119 int offset = ALIGN(orig_offset, sizeof(long));
1120 struct mptcp_data_frag *dfrag;
1121
1122 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1123 dfrag->data_len = 0;
1124 dfrag->data_seq = msk->write_seq;
1125 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1126 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001127 dfrag->already_sent = 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001128 dfrag->page = pfrag->page;
1129
1130 return dfrag;
1131}
1132
Paolo Abenicaf971d2020-11-16 10:48:06 +01001133struct mptcp_sendmsg_info {
1134 int mss_now;
1135 int size_goal;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001136 u16 limit;
1137 u16 sent;
1138 unsigned int flags;
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001139 bool data_lock_held;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001140};
1141
Florian Westphal6f8a6122020-11-16 10:48:13 +01001142static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1143 int avail_size)
1144{
1145 u64 window_end = mptcp_wnd_end(msk);
1146
1147 if (__mptcp_check_fallback(msk))
1148 return avail_size;
1149
1150 if (!before64(data_seq + avail_size, window_end)) {
1151 u64 allowed_size = window_end - data_seq;
1152
1153 return min_t(unsigned int, allowed_size, avail_size);
1154 }
1155
1156 return avail_size;
1157}
1158
Paolo Abeni724cfd22020-11-27 11:10:25 +01001159static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1160{
1161 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1162
1163 if (!mpext)
1164 return false;
1165 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1166 return true;
1167}
1168
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001169static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001170{
1171 struct sk_buff *skb;
1172
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001173 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001174 if (likely(skb)) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001175 if (likely(__mptcp_add_ext(skb, gfp))) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001176 skb_reserve(skb, MAX_TCP_HEADER);
1177 skb->reserved_tailroom = skb->end - skb->tail;
Paolo Abenif70cad12021-09-22 19:26:41 +02001178 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001179 return skb;
1180 }
1181 __kfree_skb(skb);
1182 } else {
1183 mptcp_enter_memory_pressure(sk);
1184 }
1185 return NULL;
1186}
1187
Paolo Abenif70cad12021-09-22 19:26:41 +02001188static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001189{
Paolo Abeni724cfd22020-11-27 11:10:25 +01001190 struct sk_buff *skb;
1191
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001192 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001193 if (!skb)
Paolo Abenif70cad12021-09-22 19:26:41 +02001194 return NULL;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001195
1196 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
Paolo Abenif70cad12021-09-22 19:26:41 +02001197 tcp_skb_entail(ssk, skb);
1198 return skb;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001199 }
1200 kfree_skb(skb);
Paolo Abenif70cad12021-09-22 19:26:41 +02001201 return NULL;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001202}
1203
Paolo Abenif70cad12021-09-22 19:26:41 +02001204static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001205{
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001206 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001207
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001208 if (unlikely(tcp_under_memory_pressure(sk))) {
1209 if (data_lock_held)
1210 __mptcp_mem_reclaim_partial(sk);
1211 else
1212 mptcp_mem_reclaim_partial(sk);
1213 }
1214 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001215}
1216
Geliang Tangd0cc2982021-06-17 16:46:08 -07001217/* note: this always recompute the csum on the whole skb, even
1218 * if we just appended a single frag. More status info needed
1219 */
1220static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1221{
1222 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1223 __wsum csum = ~csum_unfold(mpext->csum);
1224 int offset = skb->len - added;
1225
1226 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1227}
1228
Paolo Abeni57040752020-01-21 16:56:27 -08001229static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001230 struct mptcp_data_frag *dfrag,
Paolo Abenicaf971d2020-11-16 10:48:06 +01001231 struct mptcp_sendmsg_info *info)
Paolo Abeni57040752020-01-21 16:56:27 -08001232{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001233 u64 data_seq = dfrag->data_seq + info->sent;
Paolo Abenif70cad12021-09-22 19:26:41 +02001234 int offset = dfrag->offset + info->sent;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001235 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001236 bool zero_window_probe = false;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001237 struct mptcp_ext *mpext = NULL;
Paolo Abenif70cad12021-09-22 19:26:41 +02001238 bool can_coalesce = false;
1239 bool reuse_skb = true;
1240 struct sk_buff *skb;
1241 size_t copy;
1242 int i;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001243
Geliang Tange4b61352021-04-16 15:38:02 -07001244 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001245 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001246
Paolo Abenif70cad12021-09-22 19:26:41 +02001247 if (WARN_ON_ONCE(info->sent > info->limit ||
1248 info->limit > dfrag->data_len))
1249 return 0;
1250
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001251 /* compute send limit */
1252 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
Paolo Abenif70cad12021-09-22 19:26:41 +02001253 copy = info->size_goal;
1254
Paolo Abeni57040752020-01-21 16:56:27 -08001255 skb = tcp_write_queue_tail(ssk);
Paolo Abenif70cad12021-09-22 19:26:41 +02001256 if (skb && copy > skb->len) {
Paolo Abeni57040752020-01-21 16:56:27 -08001257 /* Limit the write to the size available in the
1258 * current skb, if any, so that we create at most a new skb.
1259 * Explicitly tells TCP internals to avoid collapsing on later
1260 * queue management operation, to avoid breaking the ext <->
1261 * SSN association set here
1262 */
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001263 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001264 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
Paolo Abeni57040752020-01-21 16:56:27 -08001265 TCP_SKB_CB(skb)->eor = 1;
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001266 goto alloc_skb;
1267 }
1268
Paolo Abenif70cad12021-09-22 19:26:41 +02001269 i = skb_shinfo(skb)->nr_frags;
1270 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1271 if (!can_coalesce && i >= sysctl_max_skb_frags) {
1272 tcp_mark_push(tcp_sk(ssk), skb);
1273 goto alloc_skb;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001274 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001275
Paolo Abenif70cad12021-09-22 19:26:41 +02001276 copy -= skb->len;
1277 } else {
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001278alloc_skb:
Paolo Abenif70cad12021-09-22 19:26:41 +02001279 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1280 if (!skb)
1281 return -ENOMEM;
1282
1283 i = skb_shinfo(skb)->nr_frags;
1284 reuse_skb = false;
1285 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1286 }
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001287
Florian Westphal6f8a6122020-11-16 10:48:13 +01001288 /* Zero window and all data acked? Probe. */
Paolo Abenif70cad12021-09-22 19:26:41 +02001289 copy = mptcp_check_allowed_size(msk, data_seq, copy);
1290 if (copy == 0) {
Paolo Abeni7439d682020-11-27 11:10:26 +01001291 u64 snd_una = READ_ONCE(msk->snd_una);
1292
Paolo Abenif70cad12021-09-22 19:26:41 +02001293 if (snd_una != msk->snd_nxt) {
1294 tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk));
Florian Westphal6f8a6122020-11-16 10:48:13 +01001295 return 0;
Paolo Abenif70cad12021-09-22 19:26:41 +02001296 }
1297
Florian Westphal6f8a6122020-11-16 10:48:13 +01001298 zero_window_probe = true;
Paolo Abeni7439d682020-11-27 11:10:26 +01001299 data_seq = snd_una - 1;
Paolo Abenif70cad12021-09-22 19:26:41 +02001300 copy = 1;
1301
1302 /* all mptcp-level data is acked, no skbs should be present into the
1303 * ssk write queue
1304 */
1305 WARN_ON_ONCE(reuse_skb);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001306 }
1307
Paolo Abenif70cad12021-09-22 19:26:41 +02001308 copy = min_t(size_t, copy, info->limit - info->sent);
1309 if (!sk_wmem_schedule(ssk, copy)) {
1310 tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk));
Paolo Abenie2223992020-11-16 10:48:03 +01001311 return -ENOMEM;
Florian Westphal357593832020-08-14 15:56:34 +02001312 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001313
Paolo Abenif70cad12021-09-22 19:26:41 +02001314 if (can_coalesce) {
1315 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1316 } else {
1317 get_page(dfrag->page);
1318 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
Paolo Abeni57040752020-01-21 16:56:27 -08001319 }
1320
Paolo Abenif70cad12021-09-22 19:26:41 +02001321 skb->len += copy;
1322 skb->data_len += copy;
1323 skb->truesize += copy;
1324 sk_wmem_queued_add(ssk, copy);
1325 sk_mem_charge(ssk, copy);
1326 skb->ip_summed = CHECKSUM_PARTIAL;
1327 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1328 TCP_SKB_CB(skb)->end_seq += copy;
1329 tcp_skb_pcount_set(skb, 0);
1330
1331 /* on skb reuse we just need to update the DSS len */
1332 if (reuse_skb) {
1333 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1334 mpext->data_len += copy;
1335 WARN_ON_ONCE(zero_window_probe);
1336 goto out;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001337 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001338
1339 memset(mpext, 0, sizeof(*mpext));
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001340 mpext->data_seq = data_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001341 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
Paolo Abenif70cad12021-09-22 19:26:41 +02001342 mpext->data_len = copy;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001343 mpext->use_map = 1;
1344 mpext->dsn64 = 1;
1345
1346 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1347 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1348 mpext->dsn64);
1349
Florian Westphal6f8a6122020-11-16 10:48:13 +01001350 if (zero_window_probe) {
Paolo Abenif70cad12021-09-22 19:26:41 +02001351 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
Florian Westphal6f8a6122020-11-16 10:48:13 +01001352 mpext->frozen = 1;
Geliang Tangd0cc2982021-06-17 16:46:08 -07001353 if (READ_ONCE(msk->csum_enabled))
Paolo Abenif70cad12021-09-22 19:26:41 +02001354 mptcp_update_data_checksum(skb, copy);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001355 tcp_push_pending_frames(ssk);
Geliang Tangd0cc2982021-06-17 16:46:08 -07001356 return 0;
Florian Westphal6f8a6122020-11-16 10:48:13 +01001357 }
Paolo Abeni57040752020-01-21 16:56:27 -08001358out:
Geliang Tangd0cc2982021-06-17 16:46:08 -07001359 if (READ_ONCE(msk->csum_enabled))
Paolo Abenif70cad12021-09-22 19:26:41 +02001360 mptcp_update_data_checksum(skb, copy);
1361 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1362 return copy;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001363}
1364
Paolo Abenid5f49192020-09-14 10:01:17 +02001365#define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1366 sizeof(struct tcphdr) - \
1367 MAX_TCP_OPTION_SPACE - \
1368 sizeof(struct ipv6hdr) - \
1369 sizeof(struct frag_hdr))
1370
1371struct subflow_send_info {
1372 struct sock *ssk;
1373 u64 ratio;
1374};
1375
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001376void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1377{
1378 if (!subflow->stale)
1379 return;
1380
1381 subflow->stale = 0;
Paolo Abenifc1b4e32021-08-13 15:15:46 -07001382 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001383}
1384
1385bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1386{
1387 if (unlikely(subflow->stale)) {
1388 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1389
1390 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1391 return false;
1392
1393 mptcp_subflow_set_active(subflow);
1394 }
1395 return __mptcp_subflow_active(subflow);
1396}
1397
Paolo Abeni33d41c92021-08-13 15:15:41 -07001398/* implement the mptcp packet scheduler;
1399 * returns the subflow that will transmit the next DSS
1400 * additionally updates the rtx timeout
1401 */
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001402static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
Peter Krystadf2962342020-03-27 14:48:39 -07001403{
Paolo Abenid5f49192020-09-14 10:01:17 +02001404 struct subflow_send_info send_info[2];
Peter Krystadf2962342020-03-27 14:48:39 -07001405 struct mptcp_subflow_context *subflow;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001406 struct sock *sk = (struct sock *)msk;
Paolo Abenid5f49192020-09-14 10:01:17 +02001407 int i, nr_active = 0;
1408 struct sock *ssk;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001409 long tout = 0;
Paolo Abenid5f49192020-09-14 10:01:17 +02001410 u64 ratio;
1411 u32 pace;
Peter Krystadf2962342020-03-27 14:48:39 -07001412
Paolo Abeni33d41c92021-08-13 15:15:41 -07001413 sock_owned_by_me(sk);
Peter Krystadf2962342020-03-27 14:48:39 -07001414
Paolo Abenid5f49192020-09-14 10:01:17 +02001415 if (__mptcp_check_fallback(msk)) {
1416 if (!msk->first)
Peter Krystadf2962342020-03-27 14:48:39 -07001417 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02001418 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001419 }
1420
Paolo Abenid5f49192020-09-14 10:01:17 +02001421 /* re-use last subflow, if the burst allow that */
1422 if (msk->last_snd && msk->snd_burst > 0 &&
1423 sk_stream_memory_free(msk->last_snd) &&
Paolo Abeni33d41c92021-08-13 15:15:41 -07001424 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1425 mptcp_set_timeout(sk);
Paolo Abenid5f49192020-09-14 10:01:17 +02001426 return msk->last_snd;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001427 }
Paolo Abenid5f49192020-09-14 10:01:17 +02001428
1429 /* pick the subflow with the lower wmem/wspace ratio */
1430 for (i = 0; i < 2; ++i) {
1431 send_info[i].ssk = NULL;
1432 send_info[i].ratio = -1;
1433 }
1434 mptcp_for_each_subflow(msk, subflow) {
Geliang Tange10a9892021-04-16 15:38:04 -07001435 trace_mptcp_subflow_get_send(subflow);
Paolo Abenid5f49192020-09-14 10:01:17 +02001436 ssk = mptcp_subflow_tcp_sock(subflow);
1437 if (!mptcp_subflow_active(subflow))
1438 continue;
1439
Paolo Abeni33d41c92021-08-13 15:15:41 -07001440 tout = max(tout, mptcp_timeout_from_subflow(subflow));
Paolo Abenid5f49192020-09-14 10:01:17 +02001441 nr_active += !subflow->backup;
Paolo Abeniec369c32021-01-20 15:39:12 +01001442 if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd)
Paolo Abenid5f49192020-09-14 10:01:17 +02001443 continue;
1444
1445 pace = READ_ONCE(ssk->sk_pacing_rate);
1446 if (!pace)
1447 continue;
1448
1449 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1450 pace);
1451 if (ratio < send_info[subflow->backup].ratio) {
1452 send_info[subflow->backup].ssk = ssk;
1453 send_info[subflow->backup].ratio = ratio;
1454 }
1455 }
Paolo Abeni33d41c92021-08-13 15:15:41 -07001456 __mptcp_set_timeout(sk, tout);
Paolo Abenid5f49192020-09-14 10:01:17 +02001457
Paolo Abenid5f49192020-09-14 10:01:17 +02001458 /* pick the best backup if no other subflow is active */
1459 if (!nr_active)
1460 send_info[0].ssk = send_info[1].ssk;
1461
1462 if (send_info[0].ssk) {
1463 msk->last_snd = send_info[0].ssk;
1464 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
Paolo Abeniec369c32021-01-20 15:39:12 +01001465 tcp_sk(msk->last_snd)->snd_wnd);
Paolo Abenid5f49192020-09-14 10:01:17 +02001466 return msk->last_snd;
1467 }
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001468
Paolo Abenid5f49192020-09-14 10:01:17 +02001469 return NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001470}
1471
Geliang Tangb8e0def2021-10-26 16:29:16 -07001472static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001473{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001474 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1475 release_sock(ssk);
1476}
1477
Florian Westphal0d199e42021-09-24 14:12:34 -07001478static void mptcp_update_post_push(struct mptcp_sock *msk,
1479 struct mptcp_data_frag *dfrag,
1480 u32 sent)
1481{
1482 u64 snd_nxt_new = dfrag->data_seq;
1483
1484 dfrag->already_sent += sent;
1485
1486 msk->snd_burst -= sent;
Florian Westphal0d199e42021-09-24 14:12:34 -07001487
1488 snd_nxt_new += dfrag->already_sent;
1489
1490 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1491 * is recovering after a failover. In that event, this re-sends
1492 * old segments.
1493 *
1494 * Thus compute snd_nxt_new candidate based on
1495 * the dfrag->data_seq that was sent and the data
1496 * that has been handed to the subflow for transmission
1497 * and skip update in case it was old dfrag.
1498 */
1499 if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1500 msk->snd_nxt = snd_nxt_new;
1501}
1502
Florian Westphal3241a9c2021-09-24 14:12:38 -07001503static void mptcp_check_and_set_pending(struct sock *sk)
1504{
1505 if (mptcp_send_head(sk) &&
1506 !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
1507 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
1508}
1509
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001510void __mptcp_push_pending(struct sock *sk, unsigned int flags)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001511{
1512 struct sock *prev_ssk = NULL, *ssk = NULL;
1513 struct mptcp_sock *msk = mptcp_sk(sk);
1514 struct mptcp_sendmsg_info info = {
1515 .flags = flags,
1516 };
1517 struct mptcp_data_frag *dfrag;
1518 int len, copied = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001519
1520 while ((dfrag = mptcp_send_head(sk))) {
1521 info.sent = dfrag->already_sent;
1522 info.limit = dfrag->data_len;
1523 len = dfrag->data_len - dfrag->already_sent;
1524 while (len > 0) {
1525 int ret = 0;
1526
1527 prev_ssk = ssk;
Florian Westphal78962482021-04-15 16:44:53 -07001528 mptcp_flush_join_list(msk);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001529 ssk = mptcp_subflow_get_send(msk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001530
Paolo Abeni9758f402021-08-26 17:44:54 -07001531 /* First check. If the ssk has changed since
1532 * the last round, release prev_ssk
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001533 */
1534 if (ssk != prev_ssk && prev_ssk)
Geliang Tangb8e0def2021-10-26 16:29:16 -07001535 mptcp_push_release(prev_ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001536 if (!ssk)
1537 goto out;
1538
Paolo Abeni9758f402021-08-26 17:44:54 -07001539 /* Need to lock the new subflow only if different
1540 * from the previous one, otherwise we are still
1541 * helding the relevant lock
1542 */
1543 if (ssk != prev_ssk)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001544 lock_sock(ssk);
1545
1546 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1547 if (ret <= 0) {
Geliang Tangb8e0def2021-10-26 16:29:16 -07001548 mptcp_push_release(ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001549 goto out;
1550 }
1551
1552 info.sent += ret;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001553 copied += ret;
1554 len -= ret;
Florian Westphal0d199e42021-09-24 14:12:34 -07001555
1556 mptcp_update_post_push(msk, dfrag, ret);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001557 }
1558 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1559 }
1560
1561 /* at this point we held the socket lock for the last subflow we used */
1562 if (ssk)
Geliang Tangb8e0def2021-10-26 16:29:16 -07001563 mptcp_push_release(ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001564
1565out:
Paolo Abeni33d41c92021-08-13 15:15:41 -07001566 /* ensure the rtx timer is running */
1567 if (!mptcp_timer_pending(sk))
1568 mptcp_reset_timer(sk);
1569 if (copied)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001570 __mptcp_check_send_data_fin(sk);
1571}
1572
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001573static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1574{
1575 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001576 struct mptcp_sendmsg_info info = {
1577 .data_lock_held = true,
1578 };
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001579 struct mptcp_data_frag *dfrag;
Paolo Abenib19bc292021-01-20 15:39:14 +01001580 struct sock *xmit_ssk;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001581 int len, copied = 0;
Paolo Abenib19bc292021-01-20 15:39:14 +01001582 bool first = true;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001583
1584 info.flags = 0;
1585 while ((dfrag = mptcp_send_head(sk))) {
1586 info.sent = dfrag->already_sent;
1587 info.limit = dfrag->data_len;
1588 len = dfrag->data_len - dfrag->already_sent;
1589 while (len > 0) {
1590 int ret = 0;
1591
Paolo Abenib19bc292021-01-20 15:39:14 +01001592 /* the caller already invoked the packet scheduler,
1593 * check for a different subflow usage only after
1594 * spooling the first chunk of data
1595 */
1596 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1597 if (!xmit_ssk)
1598 goto out;
1599 if (xmit_ssk != ssk) {
1600 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
1601 goto out;
1602 }
1603
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001604 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1605 if (ret <= 0)
1606 goto out;
1607
1608 info.sent += ret;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001609 copied += ret;
1610 len -= ret;
Paolo Abenib19bc292021-01-20 15:39:14 +01001611 first = false;
Florian Westphal0d199e42021-09-24 14:12:34 -07001612
1613 mptcp_update_post_push(msk, dfrag, ret);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001614 }
1615 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1616 }
1617
1618out:
1619 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1620 * not going to flush it via release_sock()
1621 */
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001622 if (copied) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001623 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1624 info.size_goal);
Paolo Abenid09d8182021-02-11 15:30:42 -08001625 if (!mptcp_timer_pending(sk))
1626 mptcp_reset_timer(sk);
1627
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001628 if (msk->snd_data_fin_enable &&
1629 msk->snd_nxt + 1 == msk->write_seq)
1630 mptcp_schedule_work(sk);
1631 }
1632}
1633
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001634static void mptcp_set_nospace(struct sock *sk)
1635{
1636 /* enable autotune */
1637 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1638
1639 /* will be cleared on avail space */
1640 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1641}
1642
Mat Martineauf870fa02020-01-21 16:56:15 -08001643static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1644{
1645 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal17091702020-05-16 10:46:21 +02001646 struct page_frag *pfrag;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001647 size_t copied = 0;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001648 int ret = 0;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001649 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08001650
Paolo Abeni987858e2021-04-23 11:17:07 -07001651 /* we don't support FASTOPEN yet */
1652 if (msg->msg_flags & MSG_FASTOPEN)
Mat Martineauf870fa02020-01-21 16:56:15 -08001653 return -EOPNOTSUPP;
1654
Paolo Abeni987858e2021-04-23 11:17:07 -07001655 /* silently ignore everything else */
1656 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1657
Paolo Abeni65118822021-10-26 16:29:15 -07001658 lock_sock(sk);
Mat Martineau1954b862020-02-28 15:47:39 -08001659
1660 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1661
1662 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1663 ret = sk_stream_wait_connect(sk, &timeo);
1664 if (ret)
1665 goto out;
1666 }
1667
Florian Westphal17091702020-05-16 10:46:21 +02001668 pfrag = sk_page_frag(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001669
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001670 while (msg_data_left(msg)) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001671 int total_ts, frag_truesize = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001672 struct mptcp_data_frag *dfrag;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001673 bool dfrag_collapsed;
1674 size_t psize, offset;
Mat Martineau57baaf22020-07-28 15:12:00 -07001675
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001676 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1677 ret = -EPIPE;
1678 goto out;
Florian Westphalfb529e62020-05-16 10:46:18 +02001679 }
1680
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001681 /* reuse tail pfrag, if possible, or carve a new one from the
1682 * page allocator
1683 */
1684 dfrag = mptcp_pending_tail(sk);
1685 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1686 if (!dfrag_collapsed) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001687 if (!sk_stream_memory_free(sk))
1688 goto wait_for_memory;
1689
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001690 if (!mptcp_page_frag_refill(sk, pfrag))
1691 goto wait_for_memory;
1692
1693 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1694 frag_truesize = dfrag->overhead;
1695 }
1696
1697 /* we do not bound vs wspace, to allow a single packet.
1698 * memory accounting will prevent execessive memory usage
1699 * anyway
1700 */
1701 offset = dfrag->offset + dfrag->data_len;
1702 psize = pfrag->size - offset;
1703 psize = min_t(size_t, psize, msg_data_left(msg));
Paolo Abeni724cfd22020-11-27 11:10:25 +01001704 total_ts = psize + frag_truesize;
Paolo Abeni8ce568e2021-06-21 15:54:33 -07001705
Paolo Abeni65118822021-10-26 16:29:15 -07001706 if (!sk_wmem_schedule(sk, total_ts))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001707 goto wait_for_memory;
1708
1709 if (copy_page_from_iter(dfrag->page, offset, psize,
1710 &msg->msg_iter) != psize) {
1711 ret = -EFAULT;
1712 goto out;
1713 }
1714
1715 /* data successfully copied into the write queue */
Paolo Abeni65118822021-10-26 16:29:15 -07001716 sk->sk_forward_alloc -= total_ts;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001717 copied += psize;
1718 dfrag->data_len += psize;
1719 frag_truesize += psize;
1720 pfrag->offset += frag_truesize;
1721 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1722
1723 /* charge data on mptcp pending queue to the msk socket
1724 * Note: we charge such data both to sk and ssk
1725 */
1726 sk_wmem_queued_add(sk, frag_truesize);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001727 if (!dfrag_collapsed) {
1728 get_page(dfrag->page);
1729 list_add_tail(&dfrag->list, &msk->rtx_queue);
1730 if (!msk->first_pending)
1731 WRITE_ONCE(msk->first_pending, dfrag);
1732 }
Geliang Tange4b61352021-04-16 15:38:02 -07001733 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001734 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1735 !dfrag_collapsed);
1736
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001737 continue;
1738
1739wait_for_memory:
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001740 mptcp_set_nospace(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08001741 __mptcp_push_pending(sk, msg->msg_flags);
Peter Krystadf2962342020-03-27 14:48:39 -07001742 ret = sk_stream_wait_memory(sk, &timeo);
1743 if (ret)
1744 goto out;
Peter Krystadcec37a62020-01-21 16:56:18 -08001745 }
1746
Paolo Abeni13e16032020-12-16 12:48:35 +01001747 if (copied)
Paolo Abenic2e60482021-03-04 13:32:14 -08001748 __mptcp_push_pending(sk, msg->msg_flags);
Paolo Abenida51aef2020-09-14 10:01:10 +02001749
Mat Martineau1954b862020-02-28 15:47:39 -08001750out:
Peter Krystadcec37a62020-01-21 16:56:18 -08001751 release_sock(sk);
Paolo Abeni8555c6b2020-08-03 18:40:39 +02001752 return copied ? : ret;
Mat Martineauf870fa02020-01-21 16:56:15 -08001753}
1754
Florian Westphal6771bfd2020-02-26 10:14:48 +01001755static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1756 struct msghdr *msg,
Florian Westphalb7f653b2021-06-03 16:24:32 -07001757 size_t len, int flags,
1758 struct scm_timestamping_internal *tss,
1759 int *cmsg_flags)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001760{
Yonglong Lica4fb8922021-04-23 11:17:08 -07001761 struct sk_buff *skb, *tmp;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001762 int copied = 0;
1763
Yonglong Lica4fb8922021-04-23 11:17:08 -07001764 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
Florian Westphal6771bfd2020-02-26 10:14:48 +01001765 u32 offset = MPTCP_SKB_CB(skb)->offset;
1766 u32 data_len = skb->len - offset;
1767 u32 count = min_t(size_t, len - copied, data_len);
1768 int err;
1769
Paolo Abenid9760922021-04-23 11:17:06 -07001770 if (!(flags & MSG_TRUNC)) {
1771 err = skb_copy_datagram_msg(skb, offset, msg, count);
1772 if (unlikely(err < 0)) {
1773 if (!copied)
1774 return err;
1775 break;
1776 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001777 }
1778
Florian Westphalb7f653b2021-06-03 16:24:32 -07001779 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1780 tcp_update_recv_tstamps(skb, tss);
1781 *cmsg_flags |= MPTCP_CMSG_TS;
1782 }
1783
Florian Westphal6771bfd2020-02-26 10:14:48 +01001784 copied += count;
1785
1786 if (count < data_len) {
Yonglong Lica4fb8922021-04-23 11:17:08 -07001787 if (!(flags & MSG_PEEK))
1788 MPTCP_SKB_CB(skb)->offset += count;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001789 break;
1790 }
1791
Yonglong Lica4fb8922021-04-23 11:17:08 -07001792 if (!(flags & MSG_PEEK)) {
1793 /* we will bulk release the skb memory later */
1794 skb->destructor = NULL;
Paolo Abenice599c52021-07-09 17:20:51 -07001795 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
Yonglong Lica4fb8922021-04-23 11:17:08 -07001796 __skb_unlink(skb, &msk->receive_queue);
1797 __kfree_skb(skb);
1798 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001799
1800 if (copied >= len)
1801 break;
1802 }
1803
1804 return copied;
1805}
1806
Florian Westphala6b118f2020-06-30 21:24:45 +02001807/* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1808 *
1809 * Only difference: Use highest rtt estimate of the subflows in use.
1810 */
1811static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1812{
1813 struct mptcp_subflow_context *subflow;
1814 struct sock *sk = (struct sock *)msk;
1815 u32 time, advmss = 1;
1816 u64 rtt_us, mstamp;
1817
1818 sock_owned_by_me(sk);
1819
1820 if (copied <= 0)
1821 return;
1822
1823 msk->rcvq_space.copied += copied;
1824
1825 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1826 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1827
1828 rtt_us = msk->rcvq_space.rtt_us;
1829 if (rtt_us && time < (rtt_us >> 3))
1830 return;
1831
1832 rtt_us = 0;
1833 mptcp_for_each_subflow(msk, subflow) {
1834 const struct tcp_sock *tp;
1835 u64 sf_rtt_us;
1836 u32 sf_advmss;
1837
1838 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1839
1840 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1841 sf_advmss = READ_ONCE(tp->advmss);
1842
1843 rtt_us = max(sf_rtt_us, rtt_us);
1844 advmss = max(sf_advmss, advmss);
1845 }
1846
1847 msk->rcvq_space.rtt_us = rtt_us;
1848 if (time < (rtt_us >> 3) || rtt_us == 0)
1849 return;
1850
1851 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1852 goto new_measure;
1853
1854 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1855 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1856 int rcvmem, rcvbuf;
1857 u64 rcvwin, grow;
1858
1859 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1860
1861 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1862
1863 do_div(grow, msk->rcvq_space.space);
1864 rcvwin += (grow << 1);
1865
1866 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1867 while (tcp_win_from_space(sk, rcvmem) < advmss)
1868 rcvmem += 128;
1869
1870 do_div(rcvwin, advmss);
1871 rcvbuf = min_t(u64, rcvwin * rcvmem,
1872 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1873
1874 if (rcvbuf > sk->sk_rcvbuf) {
1875 u32 window_clamp;
1876
1877 window_clamp = tcp_win_from_space(sk, rcvbuf);
1878 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1879
1880 /* Make subflows follow along. If we do not do this, we
1881 * get drops at subflow level if skbs can't be moved to
1882 * the mptcp rx queue fast enough (announced rcv_win can
1883 * exceed ssk->sk_rcvbuf).
1884 */
1885 mptcp_for_each_subflow(msk, subflow) {
1886 struct sock *ssk;
Paolo Abenic76c6952020-09-14 10:01:18 +02001887 bool slow;
Florian Westphala6b118f2020-06-30 21:24:45 +02001888
1889 ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenic76c6952020-09-14 10:01:18 +02001890 slow = lock_sock_fast(ssk);
Florian Westphala6b118f2020-06-30 21:24:45 +02001891 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1892 tcp_sk(ssk)->window_clamp = window_clamp;
Paolo Abenic76c6952020-09-14 10:01:18 +02001893 tcp_cleanup_rbuf(ssk, 1);
1894 unlock_sock_fast(ssk, slow);
Florian Westphala6b118f2020-06-30 21:24:45 +02001895 }
1896 }
1897 }
1898
1899 msk->rcvq_space.space = msk->rcvq_space.copied;
1900new_measure:
1901 msk->rcvq_space.copied = 0;
1902 msk->rcvq_space.time = mstamp;
1903}
1904
Paolo Abeni87952602020-11-27 11:10:24 +01001905static void __mptcp_update_rmem(struct sock *sk)
1906{
1907 struct mptcp_sock *msk = mptcp_sk(sk);
1908
1909 if (!msk->rmem_released)
1910 return;
1911
1912 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
Paolo Abeni65118822021-10-26 16:29:15 -07001913 mptcp_rmem_uncharge(sk, msk->rmem_released);
Paolo Abenice599c52021-07-09 17:20:51 -07001914 WRITE_ONCE(msk->rmem_released, 0);
Paolo Abeni87952602020-11-27 11:10:24 +01001915}
1916
1917static void __mptcp_splice_receive_queue(struct sock *sk)
1918{
1919 struct mptcp_sock *msk = mptcp_sk(sk);
1920
1921 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1922}
1923
Paolo Abenie3859602021-02-11 15:30:41 -08001924static bool __mptcp_move_skbs(struct mptcp_sock *msk)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001925{
Paolo Abeni87952602020-11-27 11:10:24 +01001926 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001927 unsigned int moved = 0;
Paolo Abeni87952602020-11-27 11:10:24 +01001928 bool ret, done;
Paolo Abenid5f49192020-09-14 10:01:17 +02001929
Florian Westphal78962482021-04-15 16:44:53 -07001930 mptcp_flush_join_list(msk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001931 do {
1932 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001933 bool slowpath;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001934
Paolo Abeni87952602020-11-27 11:10:24 +01001935 /* we can have data pending in the subflows only if the msk
1936 * receive buffer was full at subflow_data_ready() time,
1937 * that is an unlikely slow path.
1938 */
1939 if (likely(!ssk))
Florian Westphal6771bfd2020-02-26 10:14:48 +01001940 break;
1941
Florian Westphal65f49fe72020-11-03 11:05:04 -08001942 slowpath = lock_sock_fast(ssk);
Paolo Abeni87952602020-11-27 11:10:24 +01001943 mptcp_data_lock(sk);
Paolo Abenie3859602021-02-11 15:30:41 -08001944 __mptcp_update_rmem(sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001945 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
Paolo Abeni87952602020-11-27 11:10:24 +01001946 mptcp_data_unlock(sk);
Paolo Abeni499ada52021-06-10 15:59:44 -07001947
1948 if (unlikely(ssk->sk_err))
1949 __mptcp_error_report(sk);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001950 unlock_sock_fast(ssk, slowpath);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001951 } while (!done);
1952
Paolo Abeni87952602020-11-27 11:10:24 +01001953 /* acquire the data lock only if some input data is pending */
1954 ret = moved > 0;
1955 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1956 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
1957 mptcp_data_lock(sk);
1958 __mptcp_update_rmem(sk);
1959 ret |= __mptcp_ofo_queue(msk);
1960 __mptcp_splice_receive_queue(sk);
1961 mptcp_data_unlock(sk);
Paolo Abeniab174ad2020-09-14 10:01:12 +02001962 }
Paolo Abeni87952602020-11-27 11:10:24 +01001963 if (ret)
1964 mptcp_check_data_fin((struct sock *)msk);
1965 return !skb_queue_empty(&msk->receive_queue);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001966}
1967
Mat Martineauf870fa02020-01-21 16:56:15 -08001968static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1969 int nonblock, int flags, int *addr_len)
1970{
1971 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb7f653b2021-06-03 16:24:32 -07001972 struct scm_timestamping_internal tss;
1973 int copied = 0, cmsg_flags = 0;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001974 int target;
1975 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08001976
Paolo Abenicb9d80f2021-04-23 11:17:05 -07001977 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
1978 if (unlikely(flags & MSG_ERRQUEUE))
1979 return inet_recv_error(sk, msg, len, addr_len);
1980
Paolo Abeni65118822021-10-26 16:29:15 -07001981 lock_sock(sk);
Paolo Abenifd897672020-11-24 22:51:24 +01001982 if (unlikely(sk->sk_state == TCP_LISTEN)) {
1983 copied = -ENOTCONN;
1984 goto out_err;
1985 }
1986
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001987 timeo = sock_rcvtimeo(sk, nonblock);
1988
1989 len = min_t(size_t, len, INT_MAX);
1990 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1991
Eric Dumazet05e3ece2020-12-02 09:16:57 -08001992 while (copied < len) {
Paolo Abenie3859602021-02-11 15:30:41 -08001993 int bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001994
Florian Westphalb7f653b2021-06-03 16:24:32 -07001995 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001996 if (unlikely(bytes_read < 0)) {
1997 if (!copied)
1998 copied = bytes_read;
1999 goto out_err;
2000 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002001
Florian Westphal6771bfd2020-02-26 10:14:48 +01002002 copied += bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002003
Paolo Abeniea4ca582020-11-19 11:46:03 -08002004 /* be sure to advertise window change */
Paolo Abenie3859602021-02-11 15:30:41 -08002005 mptcp_cleanup_rbuf(msk);
2006
2007 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2008 continue;
Paolo Abeniea4ca582020-11-19 11:46:03 -08002009
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002010 /* only the master socket status is relevant here. The exit
2011 * conditions mirror closely tcp_recvmsg()
2012 */
2013 if (copied >= target)
2014 break;
2015
2016 if (copied) {
2017 if (sk->sk_err ||
2018 sk->sk_state == TCP_CLOSE ||
2019 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2020 !timeo ||
2021 signal_pending(current))
2022 break;
2023 } else {
2024 if (sk->sk_err) {
2025 copied = sock_error(sk);
2026 break;
2027 }
2028
Paolo Abeni59698562020-06-10 10:47:41 +02002029 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2030 mptcp_check_for_eof(msk);
2031
Paolo Abeni87952602020-11-27 11:10:24 +01002032 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2033 /* race breaker: the shutdown could be after the
2034 * previous receive queue check
2035 */
Paolo Abenie3859602021-02-11 15:30:41 -08002036 if (__mptcp_move_skbs(msk))
Paolo Abeni87952602020-11-27 11:10:24 +01002037 continue;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002038 break;
Paolo Abeni87952602020-11-27 11:10:24 +01002039 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002040
2041 if (sk->sk_state == TCP_CLOSE) {
2042 copied = -ENOTCONN;
2043 break;
2044 }
2045
2046 if (!timeo) {
2047 copied = -EAGAIN;
2048 break;
2049 }
2050
2051 if (signal_pending(current)) {
2052 copied = sock_intr_errno(timeo);
2053 break;
2054 }
2055 }
2056
2057 pr_debug("block timeout %ld", timeo);
Paolo Abeni612f71d2021-10-07 15:05:00 -07002058 sk_wait_data(sk, &timeo, NULL);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002059 }
Paolo Abeni3c90e372021-06-21 15:54:35 -07002060
Florian Westphal6771bfd2020-02-26 10:14:48 +01002061out_err:
Florian Westphalb7f653b2021-06-03 16:24:32 -07002062 if (cmsg_flags && copied >= 0) {
2063 if (cmsg_flags & MPTCP_CMSG_TS)
2064 tcp_recv_timestamp(msg, sk, &tss);
2065 }
2066
Paolo Abeni612f71d2021-10-07 15:05:00 -07002067 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2068 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2069 skb_queue_empty(&msk->receive_queue), copied);
Yonglong Lica4fb8922021-04-23 11:17:08 -07002070 if (!(flags & MSG_PEEK))
2071 mptcp_rcv_space_adjust(msk, copied);
Florian Westphala6b118f2020-06-30 21:24:45 +02002072
Peter Krystadcec37a62020-01-21 16:56:18 -08002073 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08002074 return copied;
2075}
2076
Paolo Abenib51f9b82020-03-27 14:48:44 -07002077static void mptcp_retransmit_timer(struct timer_list *t)
2078{
2079 struct inet_connection_sock *icsk = from_timer(icsk, t,
2080 icsk_retransmit_timer);
2081 struct sock *sk = &icsk->icsk_inet.sk;
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002082 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002083
2084 bh_lock_sock(sk);
2085 if (!sock_owned_by_user(sk)) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002086 /* we need a process context to retransmit */
2087 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2088 mptcp_schedule_work(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002089 } else {
2090 /* delegate our work to tcp_release_cb() */
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002091 set_bit(MPTCP_RETRANSMIT, &msk->flags);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002092 }
2093 bh_unlock_sock(sk);
2094 sock_put(sk);
2095}
2096
Paolo Abenie16163b2020-11-16 10:48:09 +01002097static void mptcp_timeout_timer(struct timer_list *t)
2098{
2099 struct sock *sk = from_timer(sk, t, sk_timer);
2100
2101 mptcp_schedule_work(sk);
Florian Westphalb6d69fc2020-11-24 17:24:46 +01002102 sock_put(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002103}
2104
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002105/* Find an idle subflow. Return NULL if there is unacked data at tcp
2106 * level.
2107 *
2108 * A backup subflow is returned only if that is the only kind available.
2109 */
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002110static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002111{
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002112 struct sock *backup = NULL, *pick = NULL;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002113 struct mptcp_subflow_context *subflow;
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002114 int min_stale_count = INT_MAX;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002115
2116 sock_owned_by_me((const struct sock *)msk);
2117
Paolo Abenid5f49192020-09-14 10:01:17 +02002118 if (__mptcp_check_fallback(msk))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002119 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02002120
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002121 mptcp_for_each_subflow(msk, subflow) {
2122 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2123
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002124 if (!__mptcp_subflow_active(subflow))
Paolo Abenid5f49192020-09-14 10:01:17 +02002125 continue;
2126
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002127 /* still data outstanding at TCP level? skip this */
2128 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2129 mptcp_pm_subflow_chk_stale(msk, ssk);
2130 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2131 continue;
Florian Westphal860975c2020-11-19 11:45:56 -08002132 }
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002133
2134 if (subflow->backup) {
2135 if (!backup)
2136 backup = ssk;
2137 continue;
2138 }
2139
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002140 if (!pick)
2141 pick = ssk;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002142 }
2143
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002144 if (pick)
2145 return pick;
2146
2147 /* use backup only if there are no progresses anywhere */
2148 return min_stale_count > 1 ? backup : NULL;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002149}
2150
Florian Westphal17aee052021-03-04 13:32:11 -08002151static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2152{
2153 if (msk->subflow) {
2154 iput(SOCK_INODE(msk->subflow));
2155 msk->subflow = NULL;
2156 }
2157}
2158
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002159bool __mptcp_retransmit_pending_data(struct sock *sk)
2160{
2161 struct mptcp_data_frag *cur, *rtx_head;
2162 struct mptcp_sock *msk = mptcp_sk(sk);
2163
2164 if (__mptcp_check_fallback(mptcp_sk(sk)))
2165 return false;
2166
2167 if (tcp_rtx_and_write_queues_empty(sk))
2168 return false;
2169
2170 /* the closing socket has some data untransmitted and/or unacked:
2171 * some data in the mptcp rtx queue has not really xmitted yet.
2172 * keep it simple and re-inject the whole mptcp level rtx queue
2173 */
2174 mptcp_data_lock(sk);
2175 __mptcp_clean_una_wakeup(sk);
2176 rtx_head = mptcp_rtx_head(sk);
2177 if (!rtx_head) {
2178 mptcp_data_unlock(sk);
2179 return false;
2180 }
2181
Florian Westphal0d199e42021-09-24 14:12:34 -07002182 msk->recovery_snd_nxt = msk->snd_nxt;
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002183 msk->recovery = true;
2184 mptcp_data_unlock(sk);
2185
2186 msk->first_pending = rtx_head;
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002187 msk->snd_burst = 0;
2188
2189 /* be sure to clear the "sent status" on all re-injected fragments */
2190 list_for_each_entry(cur, &msk->rtx_queue, list) {
2191 if (!cur->already_sent)
2192 break;
2193 cur->already_sent = 0;
2194 }
2195
2196 return true;
2197}
2198
Peter Krystadcec37a62020-01-21 16:56:18 -08002199/* subflow sockets can be either outgoing (connect) or incoming
2200 * (accept).
2201 *
2202 * Outgoing subflows use in-kernel sockets.
2203 * Incoming subflows do not have their own 'struct socket' allocated,
2204 * so we need to use tcp_close() after detaching them from the mptcp
2205 * parent socket.
2206 */
Florian Westphala141e022021-02-12 15:59:55 -08002207static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2208 struct mptcp_subflow_context *subflow)
Peter Krystadcec37a62020-01-21 16:56:18 -08002209{
Florian Westphale0be4932021-03-04 13:32:08 -08002210 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002211 bool need_push;
Florian Westphale0be4932021-03-04 13:32:08 -08002212
Peter Krystadcec37a62020-01-21 16:56:18 -08002213 list_del(&subflow->node);
2214
Paolo Abeni3f8b2662020-12-16 12:48:33 +01002215 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
Paolo Abenie16163b2020-11-16 10:48:09 +01002216
2217 /* if we are invoked by the msk cleanup code, the subflow is
2218 * already orphaned
2219 */
Paolo Abeni866f26f2021-01-20 15:39:10 +01002220 if (ssk->sk_socket)
Paolo Abenie16163b2020-11-16 10:48:09 +01002221 sock_orphan(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002222
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002223 need_push = __mptcp_retransmit_pending_data(sk);
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002224 subflow->disposable = 1;
2225
Paolo Abenie16163b2020-11-16 10:48:09 +01002226 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2227 * the ssk has been already destroyed, we just need to release the
2228 * reference owned by msk;
2229 */
2230 if (!inet_csk(ssk)->icsk_ulp_ops) {
2231 kfree_rcu(subflow, rcu);
2232 } else {
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002233 /* otherwise tcp will dispose of the ssk and subflow ctx */
Paolo Abenie16163b2020-11-16 10:48:09 +01002234 __tcp_close(ssk, 0);
2235
2236 /* close acquired an extra ref */
2237 __sock_put(ssk);
2238 }
2239 release_sock(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002240
2241 sock_put(ssk);
Florian Westphale0be4932021-03-04 13:32:08 -08002242
2243 if (ssk == msk->last_snd)
2244 msk->last_snd = NULL;
Florian Westphal17aee052021-03-04 13:32:11 -08002245
Florian Westphalc8fe62f2021-03-04 13:32:12 -08002246 if (ssk == msk->first)
2247 msk->first = NULL;
2248
Florian Westphal17aee052021-03-04 13:32:11 -08002249 if (msk->subflow && ssk == msk->subflow->sk)
2250 mptcp_dispose_initial_subflow(msk);
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002251
2252 if (need_push)
2253 __mptcp_push_pending(sk, 0);
Mat Martineauf870fa02020-01-21 16:56:15 -08002254}
2255
Florian Westphala141e022021-02-12 15:59:55 -08002256void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2257 struct mptcp_subflow_context *subflow)
2258{
Florian Westphalb911c972021-02-12 16:00:01 -08002259 if (sk->sk_state == TCP_ESTABLISHED)
2260 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
Florian Westphala141e022021-02-12 15:59:55 -08002261 __mptcp_close_ssk(sk, ssk, subflow);
2262}
2263
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002264static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2265{
2266 return 0;
2267}
2268
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002269static void __mptcp_close_subflow(struct mptcp_sock *msk)
2270{
2271 struct mptcp_subflow_context *subflow, *tmp;
2272
Florian Westphal3abc05d2021-02-04 15:23:30 -08002273 might_sleep();
2274
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002275 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2276 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2277
2278 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2279 continue;
2280
Florian Westphal40947e12021-02-12 15:59:56 -08002281 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2282 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2283 continue;
2284
Florian Westphala141e022021-02-12 15:59:55 -08002285 mptcp_close_ssk((struct sock *)msk, ssk, subflow);
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002286 }
2287}
2288
Paolo Abenie16163b2020-11-16 10:48:09 +01002289static bool mptcp_check_close_timeout(const struct sock *sk)
2290{
2291 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2292 struct mptcp_subflow_context *subflow;
2293
2294 if (delta >= TCP_TIMEWAIT_LEN)
2295 return true;
2296
2297 /* if all subflows are in closed status don't bother with additional
2298 * timeout
2299 */
2300 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2301 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2302 TCP_CLOSE)
2303 return false;
2304 }
2305 return true;
2306}
2307
Florian Westphal50c504a2020-12-10 14:25:04 -08002308static void mptcp_check_fastclose(struct mptcp_sock *msk)
2309{
2310 struct mptcp_subflow_context *subflow, *tmp;
2311 struct sock *sk = &msk->sk.icsk_inet.sk;
2312
2313 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2314 return;
2315
2316 mptcp_token_destroy(msk);
2317
2318 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2319 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni75e908c2021-06-21 15:54:34 -07002320 bool slow;
Florian Westphal50c504a2020-12-10 14:25:04 -08002321
Paolo Abeni75e908c2021-06-21 15:54:34 -07002322 slow = lock_sock_fast(tcp_sk);
Florian Westphal50c504a2020-12-10 14:25:04 -08002323 if (tcp_sk->sk_state != TCP_CLOSE) {
2324 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2325 tcp_set_state(tcp_sk, TCP_CLOSE);
2326 }
Paolo Abeni75e908c2021-06-21 15:54:34 -07002327 unlock_sock_fast(tcp_sk, slow);
Florian Westphal50c504a2020-12-10 14:25:04 -08002328 }
2329
2330 inet_sk_state_store(sk, TCP_CLOSE);
2331 sk->sk_shutdown = SHUTDOWN_MASK;
2332 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Florian Westphal50c504a2020-12-10 14:25:04 -08002333 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2334
2335 mptcp_close_wake_up(sk);
2336}
2337
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002338static void __mptcp_retrans(struct sock *sk)
Paolo Abeni80992012020-02-26 10:14:47 +01002339{
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002340 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenicaf971d2020-11-16 10:48:06 +01002341 struct mptcp_sendmsg_info info = {};
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002342 struct mptcp_data_frag *dfrag;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002343 size_t copied = 0;
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002344 struct sock *ssk;
2345 int ret;
2346
Paolo Abenib5941f02021-05-27 16:31:37 -07002347 mptcp_clean_una_wakeup(sk);
Florian Westphal3241a9c2021-09-24 14:12:38 -07002348
2349 /* first check ssk: need to kick "stale" logic */
2350 ssk = mptcp_subflow_get_retrans(msk);
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002351 dfrag = mptcp_rtx_head(sk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002352 if (!dfrag) {
2353 if (mptcp_data_fin_enabled(msk)) {
2354 struct inet_connection_sock *icsk = inet_csk(sk);
2355
2356 icsk->icsk_retransmits++;
2357 mptcp_set_datafin_timeout(sk);
2358 mptcp_send_ack(msk);
2359
2360 goto reset_timer;
2361 }
2362
Florian Westphal3241a9c2021-09-24 14:12:38 -07002363 if (!mptcp_send_head(sk))
2364 return;
2365
2366 goto reset_timer;
Mat Martineau6477dd32021-04-23 09:40:33 -07002367 }
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002368
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002369 if (!ssk)
2370 goto reset_timer;
2371
2372 lock_sock(ssk);
2373
2374 /* limit retransmission to the bytes already sent on some subflows */
2375 info.sent = 0;
Paolo Abeni4e148672021-06-17 16:46:17 -07002376 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2377 while (info.sent < info.limit) {
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002378 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2379 if (ret <= 0)
2380 break;
2381
2382 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2383 copied += ret;
2384 info.sent += ret;
2385 }
Paolo Abeni4e148672021-06-17 16:46:17 -07002386 if (copied) {
2387 dfrag->already_sent = max(dfrag->already_sent, info.sent);
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002388 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2389 info.size_goal);
Paolo Abeni4e148672021-06-17 16:46:17 -07002390 }
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002391
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002392 release_sock(ssk);
2393
2394reset_timer:
Florian Westphal3241a9c2021-09-24 14:12:38 -07002395 mptcp_check_and_set_pending(sk);
2396
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002397 if (!mptcp_timer_pending(sk))
2398 mptcp_reset_timer(sk);
2399}
2400
2401static void mptcp_worker(struct work_struct *work)
2402{
2403 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2404 struct sock *sk = &msk->sk.icsk_inet.sk;
2405 int state;
Paolo Abeni80992012020-02-26 10:14:47 +01002406
2407 lock_sock(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002408 state = sk->sk_state;
2409 if (unlikely(state == TCP_CLOSE))
2410 goto unlock;
2411
Mat Martineau43b54c62020-07-28 15:12:06 -07002412 mptcp_check_data_fin_ack(sk);
Florian Westphal78962482021-04-15 16:44:53 -07002413 mptcp_flush_join_list(msk);
Florian Westphal50c504a2020-12-10 14:25:04 -08002414
2415 mptcp_check_fastclose(msk);
2416
Florian Westphalb4162682020-07-07 14:40:48 +02002417 if (msk->pm.status)
Florian Westphale9801432021-02-12 15:59:54 -08002418 mptcp_pm_nl_work(msk);
Florian Westphalb4162682020-07-07 14:40:48 +02002419
Florian Westphal59832e22020-04-02 13:44:52 +02002420 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2421 mptcp_check_for_eof(msk);
2422
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002423 __mptcp_check_send_data_fin(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002424 mptcp_check_data_fin(sk);
2425
Paolo Abeni341c6522021-02-19 18:35:37 +01002426 /* There is no point in keeping around an orphaned sk timedout or
2427 * closed, but we need the msk around to reply to incoming DATA_FIN,
2428 * even if it is orphaned and in FIN_WAIT2 state
Paolo Abenie16163b2020-11-16 10:48:09 +01002429 */
2430 if (sock_flag(sk, SOCK_DEAD) &&
Paolo Abeni341c6522021-02-19 18:35:37 +01002431 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002432 inet_sk_state_store(sk, TCP_CLOSE);
2433 __mptcp_destroy_sock(sk);
2434 goto unlock;
2435 }
2436
Florian Westphalb263b0d2021-02-12 15:59:57 -08002437 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2438 __mptcp_close_subflow(msk);
2439
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002440 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2441 __mptcp_retrans(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002442
2443unlock:
Paolo Abeni80992012020-02-26 10:14:47 +01002444 release_sock(sk);
2445 sock_put(sk);
2446}
2447
Matthieu Baerts784325e2020-01-21 16:56:28 -08002448static int __mptcp_init_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002449{
Peter Krystadcec37a62020-01-21 16:56:18 -08002450 struct mptcp_sock *msk = mptcp_sk(sk);
2451
Peter Krystadec3edaa2020-03-27 14:48:40 -07002452 spin_lock_init(&msk->join_list_lock);
2453
Peter Krystadcec37a62020-01-21 16:56:18 -08002454 INIT_LIST_HEAD(&msk->conn_list);
Peter Krystadec3edaa2020-03-27 14:48:40 -07002455 INIT_LIST_HEAD(&msk->join_list);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002456 INIT_LIST_HEAD(&msk->rtx_queue);
Paolo Abeni80992012020-02-26 10:14:47 +01002457 INIT_WORK(&msk->work, mptcp_worker);
Paolo Abeni87952602020-11-27 11:10:24 +01002458 __skb_queue_head_init(&msk->receive_queue);
Paolo Abeniab174ad2020-09-14 10:01:12 +02002459 msk->out_of_order_queue = RB_ROOT;
Paolo Abenif0e6a4c2020-11-16 10:48:07 +01002460 msk->first_pending = NULL;
Paolo Abeni65118822021-10-26 16:29:15 -07002461 msk->rmem_fwd_alloc = 0;
Paolo Abenice599c52021-07-09 17:20:51 -07002462 WRITE_ONCE(msk->rmem_released, 0);
Paolo Abeni33d41c92021-08-13 15:15:41 -07002463 msk->timer_ival = TCP_RTO_MIN;
Peter Krystadcec37a62020-01-21 16:56:18 -08002464
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002465 msk->first = NULL;
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002466 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
Geliang Tang752e9062021-06-17 16:46:07 -07002467 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002468 msk->recovery = false;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002469
Peter Krystad1b1c7a02020-03-27 14:48:38 -07002470 mptcp_pm_data_init(msk);
2471
Paolo Abenib51f9b82020-03-27 14:48:44 -07002472 /* re-use the csk retrans timer for MPTCP-level retrans */
2473 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
Paolo Abenie16163b2020-11-16 10:48:09 +01002474 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002475
Mat Martineauf870fa02020-01-21 16:56:15 -08002476 return 0;
2477}
2478
Matthieu Baerts784325e2020-01-21 16:56:28 -08002479static int mptcp_init_sock(struct sock *sk)
2480{
Paolo Abeni20b57592021-05-25 14:23:10 -07002481 struct inet_connection_sock *icsk = inet_csk(sk);
Florian Westphalfc518952020-03-27 14:48:50 -07002482 struct net *net = sock_net(sk);
2483 int ret;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002484
Geliang Tangb6c08382020-09-24 08:29:54 +08002485 ret = __mptcp_init_sock(sk);
2486 if (ret)
2487 return ret;
2488
Florian Westphalfc518952020-03-27 14:48:50 -07002489 if (!mptcp_is_enabled(net))
2490 return -ENOPROTOOPT;
2491
2492 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2493 return -ENOMEM;
2494
Paolo Abenifa680182020-06-29 22:26:23 +02002495 ret = __mptcp_socket_create(mptcp_sk(sk));
2496 if (ret)
2497 return ret;
2498
Paolo Abeni20b57592021-05-25 14:23:10 -07002499 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2500 * propagate the correct value
2501 */
2502 tcp_assign_congestion_control(sk);
2503 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2504
2505 /* no need to keep a reference to the ops, the name will suffice */
2506 tcp_cleanup_congestion_control(sk);
2507 icsk->icsk_ca_ops = NULL;
2508
Paolo Abenid0272362020-03-27 14:48:45 -07002509 sk_sockets_allocated_inc(sk);
Florian Westphala6b118f2020-06-30 21:24:45 +02002510 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
Paolo Abenida51aef2020-09-14 10:01:10 +02002511 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
Paolo Abenid0272362020-03-27 14:48:45 -07002512
Paolo Abeni18b683b2020-03-27 14:48:43 -07002513 return 0;
2514}
2515
2516static void __mptcp_clear_xmit(struct sock *sk)
2517{
2518 struct mptcp_sock *msk = mptcp_sk(sk);
2519 struct mptcp_data_frag *dtmp, *dfrag;
2520
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002521 WRITE_ONCE(msk->first_pending, NULL);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002522 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
Paolo Abenid0272362020-03-27 14:48:45 -07002523 dfrag_clear(sk, dfrag);
Matthieu Baerts784325e2020-01-21 16:56:28 -08002524}
2525
Paolo Abeni80992012020-02-26 10:14:47 +01002526static void mptcp_cancel_work(struct sock *sk)
2527{
2528 struct mptcp_sock *msk = mptcp_sk(sk);
2529
Paolo Abenib2771d22020-11-19 11:45:54 -08002530 if (cancel_work_sync(&msk->work))
Paolo Abenie16163b2020-11-16 10:48:09 +01002531 __sock_put(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002532}
2533
Geliang Tangd0876b22020-09-24 08:29:49 +08002534void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
Peter Krystad21498492020-01-21 16:56:21 -08002535{
2536 lock_sock(ssk);
2537
2538 switch (ssk->sk_state) {
2539 case TCP_LISTEN:
2540 if (!(how & RCV_SHUTDOWN))
2541 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002542 fallthrough;
Peter Krystad21498492020-01-21 16:56:21 -08002543 case TCP_SYN_SENT:
2544 tcp_disconnect(ssk, O_NONBLOCK);
2545 break;
2546 default:
Mat Martineau43b54c62020-07-28 15:12:06 -07002547 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2548 pr_debug("Fallback");
2549 ssk->sk_shutdown |= how;
2550 tcp_shutdown(ssk, how);
2551 } else {
2552 pr_debug("Sending DATA_FIN on subflow %p", ssk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002553 tcp_send_ack(ssk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002554 if (!mptcp_timer_pending(sk))
2555 mptcp_reset_timer(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002556 }
Peter Krystad21498492020-01-21 16:56:21 -08002557 break;
2558 }
2559
Peter Krystad21498492020-01-21 16:56:21 -08002560 release_sock(ssk);
2561}
2562
Mat Martineau6920b852020-07-28 15:12:04 -07002563static const unsigned char new_state[16] = {
2564 /* current state: new state: action: */
2565 [0 /* (Invalid) */] = TCP_CLOSE,
2566 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2567 [TCP_SYN_SENT] = TCP_CLOSE,
2568 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2569 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2570 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2571 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2572 [TCP_CLOSE] = TCP_CLOSE,
2573 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2574 [TCP_LAST_ACK] = TCP_LAST_ACK,
2575 [TCP_LISTEN] = TCP_CLOSE,
2576 [TCP_CLOSING] = TCP_CLOSING,
2577 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2578};
2579
2580static int mptcp_close_state(struct sock *sk)
2581{
2582 int next = (int)new_state[sk->sk_state];
2583 int ns = next & TCP_STATE_MASK;
2584
2585 inet_sk_state_store(sk, ns);
2586
2587 return next & TCP_ACTION_FIN;
2588}
2589
Paolo Abenie16163b2020-11-16 10:48:09 +01002590static void __mptcp_check_send_data_fin(struct sock *sk)
2591{
2592 struct mptcp_subflow_context *subflow;
2593 struct mptcp_sock *msk = mptcp_sk(sk);
2594
2595 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2596 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2597 msk->snd_nxt, msk->write_seq);
2598
2599 /* we still need to enqueue subflows or not really shutting down,
2600 * skip this
2601 */
2602 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2603 mptcp_send_head(sk))
2604 return;
2605
2606 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2607
Paolo Abeni26aa2312020-11-19 11:45:55 -08002608 /* fallback socket will not get data_fin/ack, can move to the next
2609 * state now
2610 */
2611 if (__mptcp_check_fallback(msk)) {
2612 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2613 inet_sk_state_store(sk, TCP_CLOSE);
2614 mptcp_close_wake_up(sk);
2615 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2616 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2617 }
Paolo Abenie16163b2020-11-16 10:48:09 +01002618 }
2619
Florian Westphal78962482021-04-15 16:44:53 -07002620 mptcp_flush_join_list(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002621 mptcp_for_each_subflow(msk, subflow) {
2622 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2623
2624 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2625 }
2626}
2627
2628static void __mptcp_wr_shutdown(struct sock *sk)
2629{
2630 struct mptcp_sock *msk = mptcp_sk(sk);
2631
2632 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2633 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2634 !!mptcp_send_head(sk));
2635
2636 /* will be ignored by fallback sockets */
2637 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2638 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2639
2640 __mptcp_check_send_data_fin(sk);
2641}
2642
2643static void __mptcp_destroy_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002644{
Peter Krystadcec37a62020-01-21 16:56:18 -08002645 struct mptcp_subflow_context *subflow, *tmp;
Mat Martineauf870fa02020-01-21 16:56:15 -08002646 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002647 LIST_HEAD(conn_list);
Mat Martineauf870fa02020-01-21 16:56:15 -08002648
Paolo Abenie16163b2020-11-16 10:48:09 +01002649 pr_debug("msk=%p", msk);
Florian Westphal2c22c062020-02-04 18:12:30 +01002650
Florian Westphal3abc05d2021-02-04 15:23:30 -08002651 might_sleep();
2652
Paolo Abeni10f6d462020-05-29 17:43:30 +02002653 /* be sure to always acquire the join list lock, to sync vs
2654 * mptcp_finish_join().
2655 */
2656 spin_lock_bh(&msk->join_list_lock);
2657 list_splice_tail_init(&msk->join_list, &msk->conn_list);
2658 spin_unlock_bh(&msk->join_list_lock);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002659 list_splice_init(&msk->conn_list, &conn_list);
2660
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002661 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
Paolo Abenie16163b2020-11-16 10:48:09 +01002662 sk_stop_timer(sk, &sk->sk_timer);
2663 msk->pm.status = 0;
Florian Westphalb2c5b612020-01-29 15:54:45 +01002664
2665 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
Peter Krystadcec37a62020-01-21 16:56:18 -08002666 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002667 __mptcp_close_ssk(sk, ssk, subflow);
Mat Martineauf870fa02020-01-21 16:56:15 -08002668 }
2669
Paolo Abenie16163b2020-11-16 10:48:09 +01002670 sk->sk_prot->destroy(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002671
Paolo Abeni65118822021-10-26 16:29:15 -07002672 WARN_ON_ONCE(msk->rmem_fwd_alloc);
Paolo Abeni87952602020-11-27 11:10:24 +01002673 WARN_ON_ONCE(msk->rmem_released);
Paolo Abenie16163b2020-11-16 10:48:09 +01002674 sk_stream_kill_queues(sk);
2675 xfrm_sk_free_policy(sk);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002676
Paolo Abenie16163b2020-11-16 10:48:09 +01002677 sk_refcnt_debug_release(sk);
Florian Westphal17aee052021-03-04 13:32:11 -08002678 mptcp_dispose_initial_subflow(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002679 sock_put(sk);
2680}
Florian Westphal6771bfd2020-02-26 10:14:48 +01002681
Paolo Abenie16163b2020-11-16 10:48:09 +01002682static void mptcp_close(struct sock *sk, long timeout)
2683{
2684 struct mptcp_subflow_context *subflow;
2685 bool do_cancel_work = false;
2686
2687 lock_sock(sk);
2688 sk->sk_shutdown = SHUTDOWN_MASK;
2689
2690 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2691 inet_sk_state_store(sk, TCP_CLOSE);
2692 goto cleanup;
2693 }
2694
2695 if (mptcp_close_state(sk))
2696 __mptcp_wr_shutdown(sk);
2697
2698 sk_stream_wait_close(sk, timeout);
2699
2700cleanup:
2701 /* orphan all the subflows */
2702 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
Geliang Tang44227912021-04-16 15:38:08 -07002703 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002704 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni49054552021-09-29 11:59:17 +02002705 bool slow = lock_sock_fast_nested(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002706
Paolo Abenie16163b2020-11-16 10:48:09 +01002707 sock_orphan(ssk);
2708 unlock_sock_fast(ssk, slow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002709 }
2710 sock_orphan(sk);
2711
2712 sock_hold(sk);
2713 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2714 if (sk->sk_state == TCP_CLOSE) {
2715 __mptcp_destroy_sock(sk);
2716 do_cancel_work = true;
2717 } else {
2718 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2719 }
2720 release_sock(sk);
2721 if (do_cancel_work)
2722 mptcp_cancel_work(sk);
Florian Westphalb911c972021-02-12 16:00:01 -08002723
2724 if (mptcp_sk(sk)->token)
2725 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2726
Paolo Abenie16163b2020-11-16 10:48:09 +01002727 sock_put(sk);
Mat Martineauf870fa02020-01-21 16:56:15 -08002728}
2729
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002730static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2731{
2732#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2733 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2734 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2735
2736 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2737 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2738
2739 if (msk6 && ssk6) {
2740 msk6->saddr = ssk6->saddr;
2741 msk6->flow_label = ssk6->flow_label;
2742 }
2743#endif
2744
2745 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2746 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2747 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2748 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2749 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2750 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2751}
2752
Paolo Abeni18b683b2020-03-27 14:48:43 -07002753static int mptcp_disconnect(struct sock *sk, int flags)
2754{
Paolo Abeni76e2a552021-01-12 18:25:24 +01002755 struct mptcp_subflow_context *subflow;
2756 struct mptcp_sock *msk = mptcp_sk(sk);
2757
Florian Westphal78962482021-04-15 16:44:53 -07002758 mptcp_do_flush_join_list(msk);
2759
Paolo Abeni13a94992021-01-14 16:37:37 +01002760 mptcp_for_each_subflow(msk, subflow) {
2761 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2762
2763 lock_sock(ssk);
2764 tcp_disconnect(ssk, flags);
2765 release_sock(ssk);
2766 }
Florian Westphal42c556f2020-04-29 20:43:20 +02002767 return 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002768}
2769
Florian Westphalb0519de2020-02-06 00:39:37 +01002770#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2771static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2772{
2773 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2774
2775 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2776}
2777#endif
2778
Paolo Abenifca5c822020-04-20 16:25:06 +02002779struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +02002780 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +02002781 struct request_sock *req)
Florian Westphalb0519de2020-02-06 00:39:37 +01002782{
Paolo Abeni58b09912020-03-13 16:52:41 +01002783 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
Florian Westphalb0519de2020-02-06 00:39:37 +01002784 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
Paolo Abeni58b09912020-03-13 16:52:41 +01002785 struct mptcp_sock *msk;
2786 u64 ack_seq;
Florian Westphalb0519de2020-02-06 00:39:37 +01002787
2788 if (!nsk)
2789 return NULL;
2790
2791#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2792 if (nsk->sk_family == AF_INET6)
2793 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2794#endif
2795
Paolo Abeni58b09912020-03-13 16:52:41 +01002796 __mptcp_init_sock(nsk);
2797
2798 msk = mptcp_sk(nsk);
2799 msk->local_key = subflow_req->local_key;
2800 msk->token = subflow_req->token;
2801 msk->subflow = NULL;
Paolo Abenib93df082020-07-23 13:02:32 +02002802 WRITE_ONCE(msk->fully_established, false);
Paolo Abeni74c7dfb2021-08-26 17:44:52 -07002803 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
Geliang Tang06251182021-06-17 16:46:13 -07002804 WRITE_ONCE(msk->csum_enabled, true);
Paolo Abeni58b09912020-03-13 16:52:41 +01002805
Paolo Abeni58b09912020-03-13 16:52:41 +01002806 msk->write_seq = subflow_req->idsn + 1;
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01002807 msk->snd_nxt = msk->write_seq;
Paolo Abeni7439d682020-11-27 11:10:26 +01002808 msk->snd_una = msk->write_seq;
2809 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
Florian Westphaldf00b082021-04-15 16:44:54 -07002810 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002811
Paolo Abeni74c7dfb2021-08-26 17:44:52 -07002812 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
Paolo Abeni58b09912020-03-13 16:52:41 +01002813 msk->can_ack = true;
Paolo Abenicfde1412020-04-30 15:01:52 +02002814 msk->remote_key = mp_opt->sndr_key;
Paolo Abeni58b09912020-03-13 16:52:41 +01002815 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2816 ack_seq++;
Mat Martineau917944d2020-09-29 15:08:19 -07002817 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08002818 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Paolo Abeni58b09912020-03-13 16:52:41 +01002819 }
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002820
Florian Westphal5e200872020-04-20 16:25:04 +02002821 sock_reset_flag(nsk, SOCK_RCU_FREE);
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002822 /* will be fully established after successful MPC subflow creation */
2823 inet_sk_state_store(nsk, TCP_SYN_RECV);
Paolo Abeni0c148462020-12-16 12:48:32 +01002824
2825 security_inet_csk_clone(nsk, req);
Paolo Abeni58b09912020-03-13 16:52:41 +01002826 bh_unlock_sock(nsk);
2827
2828 /* keep a single reference */
2829 __sock_put(nsk);
Florian Westphalb0519de2020-02-06 00:39:37 +01002830 return nsk;
2831}
2832
Florian Westphala6b118f2020-06-30 21:24:45 +02002833void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2834{
2835 const struct tcp_sock *tp = tcp_sk(ssk);
2836
2837 msk->rcvq_space.copied = 0;
2838 msk->rcvq_space.rtt_us = 0;
2839
2840 msk->rcvq_space.time = tp->tcp_mstamp;
2841
2842 /* initial rcv_space offering made to peer */
2843 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2844 TCP_INIT_CWND * tp->advmss);
2845 if (msk->rcvq_space.space == 0)
2846 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002847
Paolo Abeni7439d682020-11-27 11:10:26 +01002848 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
Florian Westphala6b118f2020-06-30 21:24:45 +02002849}
2850
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002851static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2852 bool kern)
2853{
2854 struct mptcp_sock *msk = mptcp_sk(sk);
2855 struct socket *listener;
2856 struct sock *newsk;
2857
2858 listener = __mptcp_nmpc_socket(msk);
2859 if (WARN_ON_ONCE(!listener)) {
2860 *err = -EINVAL;
2861 return NULL;
2862 }
2863
2864 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2865 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2866 if (!newsk)
2867 return NULL;
2868
2869 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002870 if (sk_is_mptcp(newsk)) {
2871 struct mptcp_subflow_context *subflow;
2872 struct sock *new_mptcp_sock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002873
2874 subflow = mptcp_subflow_ctx(newsk);
Paolo Abeni58b09912020-03-13 16:52:41 +01002875 new_mptcp_sock = subflow->conn;
2876
2877 /* is_mptcp should be false if subflow->conn is missing, see
2878 * subflow_syn_recv_sock()
2879 */
2880 if (WARN_ON_ONCE(!new_mptcp_sock)) {
2881 tcp_sk(newsk)->is_mptcp = 0;
2882 return newsk;
2883 }
2884
2885 /* acquire the 2nd reference for the owning socket */
2886 sock_hold(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002887 newsk = new_mptcp_sock;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08002888 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
Florian Westphalfc518952020-03-27 14:48:50 -07002889 } else {
2890 MPTCP_INC_STATS(sock_net(sk),
2891 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002892 }
2893
2894 return newsk;
2895}
2896
Geliang Tang5c8c1642020-09-24 08:29:57 +08002897void mptcp_destroy_common(struct mptcp_sock *msk)
2898{
Paolo Abeni87952602020-11-27 11:10:24 +01002899 struct sock *sk = (struct sock *)msk;
2900
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002901 __mptcp_clear_xmit(sk);
2902
Paolo Abeni87952602020-11-27 11:10:24 +01002903 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
2904 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
Paolo Abeni65118822021-10-26 16:29:15 -07002905 __skb_queue_purge(&sk->sk_receive_queue);
Geliang Tang5c8c1642020-09-24 08:29:57 +08002906 skb_rbtree_purge(&msk->out_of_order_queue);
Paolo Abeni65118822021-10-26 16:29:15 -07002907
2908 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
2909 * inet_sock_destruct() will dispose it
2910 */
2911 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
2912 msk->rmem_fwd_alloc = 0;
Geliang Tang5c8c1642020-09-24 08:29:57 +08002913 mptcp_token_destroy(msk);
2914 mptcp_pm_free_anno_list(msk);
2915}
2916
Peter Krystad79c09492020-01-21 16:56:20 -08002917static void mptcp_destroy(struct sock *sk)
2918{
Florian Westphalc9fd9c52020-01-29 15:54:43 +01002919 struct mptcp_sock *msk = mptcp_sk(sk);
2920
Geliang Tang5c8c1642020-09-24 08:29:57 +08002921 mptcp_destroy_common(msk);
Paolo Abenid0272362020-03-27 14:48:45 -07002922 sk_sockets_allocated_dec(sk);
Peter Krystad79c09492020-01-21 16:56:20 -08002923}
2924
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002925void __mptcp_data_acked(struct sock *sk)
2926{
2927 if (!sock_owned_by_user(sk))
2928 __mptcp_clean_una(sk);
2929 else
2930 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
2931
2932 if (mptcp_pending_data_fin_ack(sk))
2933 mptcp_schedule_work(sk);
2934}
2935
Paolo Abeni219d0492020-12-16 12:48:34 +01002936void __mptcp_check_push(struct sock *sk, struct sock *ssk)
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002937{
2938 if (!mptcp_send_head(sk))
2939 return;
2940
Paolo Abeni40dc9412021-01-20 15:39:13 +01002941 if (!sock_owned_by_user(sk)) {
Paolo Abenib19bc292021-01-20 15:39:14 +01002942 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
2943
2944 if (xmit_ssk == ssk)
Paolo Abeni40dc9412021-01-20 15:39:13 +01002945 __mptcp_subflow_push_pending(sk, ssk);
Paolo Abenib19bc292021-01-20 15:39:14 +01002946 else if (xmit_ssk)
2947 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
Paolo Abeni40dc9412021-01-20 15:39:13 +01002948 } else {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002949 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
Paolo Abeni40dc9412021-01-20 15:39:13 +01002950 }
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002951}
2952
Paolo Abenie93da922020-11-27 11:10:23 +01002953/* processes deferred events and flush wmem */
Paolo Abeni14c441b2020-02-26 10:14:52 +01002954static void mptcp_release_cb(struct sock *sk)
2955{
Paolo Abenic2e60482021-03-04 13:32:14 -08002956 for (;;) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002957 unsigned long flags = 0;
2958
Paolo Abenic2e60482021-03-04 13:32:14 -08002959 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03002960 flags |= BIT(MPTCP_PUSH_PENDING);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002961 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
2962 flags |= BIT(MPTCP_RETRANSMIT);
Paolo Abenic2e60482021-03-04 13:32:14 -08002963 if (!flags)
2964 break;
2965
2966 /* the following actions acquire the subflow socket lock
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002967 *
2968 * 1) can't be invoked in atomic scope
2969 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
2970 * datapath acquires the msk socket spinlock while helding
2971 * the subflow socket lock
2972 */
2973
2974 spin_unlock_bh(&sk->sk_lock.slock);
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03002975 if (flags & BIT(MPTCP_PUSH_PENDING))
Paolo Abenic2e60482021-03-04 13:32:14 -08002976 __mptcp_push_pending(sk, 0);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002977 if (flags & BIT(MPTCP_RETRANSMIT))
2978 __mptcp_retrans(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08002979
2980 cond_resched();
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002981 spin_lock_bh(&sk->sk_lock.slock);
2982 }
Paolo Abenic2e60482021-03-04 13:32:14 -08002983
Paolo Abeni490274b2021-06-21 17:33:08 -07002984 /* be sure to set the current sk state before tacking actions
2985 * depending on sk_state
2986 */
2987 if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags))
2988 __mptcp_set_connected(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08002989 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
Paolo Abeni417789d2021-03-04 13:32:15 -08002990 __mptcp_clean_una_wakeup(sk);
Paolo Abeni15cc1042021-02-11 15:30:37 -08002991 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
2992 __mptcp_error_report(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002993
Paolo Abeni87952602020-11-27 11:10:24 +01002994 __mptcp_update_rmem(sk);
Paolo Abeni14c441b2020-02-26 10:14:52 +01002995}
2996
Paolo Abenib19bc292021-01-20 15:39:14 +01002997void mptcp_subflow_process_delegated(struct sock *ssk)
2998{
2999 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3000 struct sock *sk = subflow->conn;
3001
3002 mptcp_data_lock(sk);
3003 if (!sock_owned_by_user(sk))
3004 __mptcp_subflow_push_pending(sk, ssk);
3005 else
3006 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
3007 mptcp_data_unlock(sk);
3008 mptcp_subflow_delegated_done(subflow);
3009}
3010
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003011static int mptcp_hash(struct sock *sk)
3012{
3013 /* should never be called,
3014 * we hash the TCP subflows not the master socket
3015 */
3016 WARN_ON_ONCE(1);
3017 return 0;
3018}
3019
3020static void mptcp_unhash(struct sock *sk)
3021{
3022 /* called from sk_common_release(), but nothing to do here */
3023}
3024
Peter Krystadcec37a62020-01-21 16:56:18 -08003025static int mptcp_get_port(struct sock *sk, unsigned short snum)
Mat Martineauf870fa02020-01-21 16:56:15 -08003026{
3027 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08003028 struct socket *ssock;
Mat Martineauf870fa02020-01-21 16:56:15 -08003029
Peter Krystadcec37a62020-01-21 16:56:18 -08003030 ssock = __mptcp_nmpc_socket(msk);
3031 pr_debug("msk=%p, subflow=%p", msk, ssock);
3032 if (WARN_ON_ONCE(!ssock))
3033 return -EINVAL;
Mat Martineauf870fa02020-01-21 16:56:15 -08003034
Peter Krystadcec37a62020-01-21 16:56:18 -08003035 return inet_csk_get_port(ssock->sk, snum);
3036}
Mat Martineauf870fa02020-01-21 16:56:15 -08003037
Peter Krystadcec37a62020-01-21 16:56:18 -08003038void mptcp_finish_connect(struct sock *ssk)
3039{
3040 struct mptcp_subflow_context *subflow;
3041 struct mptcp_sock *msk;
3042 struct sock *sk;
Mat Martineau6d0060f2020-01-21 16:56:23 -08003043 u64 ack_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -08003044
Peter Krystadcec37a62020-01-21 16:56:18 -08003045 subflow = mptcp_subflow_ctx(ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -08003046 sk = subflow->conn;
3047 msk = mptcp_sk(sk);
3048
Mat Martineau648ef4b2020-01-21 16:56:24 -08003049 pr_debug("msk=%p, token=%u", sk, subflow->token);
3050
Mat Martineau6d0060f2020-01-21 16:56:23 -08003051 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3052 ack_seq++;
Mat Martineau648ef4b2020-01-21 16:56:24 -08003053 subflow->map_seq = ack_seq;
3054 subflow->map_subflow_seq = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -08003055
Peter Krystadcec37a62020-01-21 16:56:18 -08003056 /* the socket is not connected yet, no msk/subflow ops can access/race
3057 * accessing the field below
3058 */
3059 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3060 WRITE_ONCE(msk->local_key, subflow->local_key);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003061 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01003062 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003063 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08003064 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Christoph Paaschd22f4982020-01-21 16:56:32 -08003065 WRITE_ONCE(msk->can_ack, 1);
Paolo Abeni7439d682020-11-27 11:10:26 +01003066 WRITE_ONCE(msk->snd_una, msk->write_seq);
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003067
Florian Westphal6c714f12021-02-12 15:59:58 -08003068 mptcp_pm_new_connection(msk, ssk, 0);
Florian Westphala6b118f2020-06-30 21:24:45 +02003069
3070 mptcp_rcv_space_init(msk, ssk);
Mat Martineauf870fa02020-01-21 16:56:15 -08003071}
3072
Paolo Abeni866f26f2021-01-20 15:39:10 +01003073void mptcp_sock_graft(struct sock *sk, struct socket *parent)
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003074{
3075 write_lock_bh(&sk->sk_callback_lock);
3076 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3077 sk_set_socket(sk, parent);
3078 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3079 write_unlock_bh(&sk->sk_callback_lock);
3080}
3081
Paolo Abenie16163b2020-11-16 10:48:09 +01003082bool mptcp_finish_join(struct sock *ssk)
Peter Krystadf2962342020-03-27 14:48:39 -07003083{
Paolo Abenie16163b2020-11-16 10:48:09 +01003084 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Peter Krystadf2962342020-03-27 14:48:39 -07003085 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3086 struct sock *parent = (void *)msk;
3087 struct socket *parent_sock;
Peter Krystadec3edaa2020-03-27 14:48:40 -07003088 bool ret;
Peter Krystadf2962342020-03-27 14:48:39 -07003089
3090 pr_debug("msk=%p, subflow=%p", msk, subflow);
3091
3092 /* mptcp socket already closing? */
Florian Westphaldc87efd2021-04-01 16:19:44 -07003093 if (!mptcp_is_fully_established(parent)) {
3094 subflow->reset_reason = MPTCP_RST_EMPTCP;
Peter Krystadf2962342020-03-27 14:48:39 -07003095 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003096 }
Peter Krystadf2962342020-03-27 14:48:39 -07003097
3098 if (!msk->pm.server_side)
Florian Westphalb911c972021-02-12 16:00:01 -08003099 goto out;
Peter Krystadf2962342020-03-27 14:48:39 -07003100
Florian Westphaldc87efd2021-04-01 16:19:44 -07003101 if (!mptcp_pm_allow_new_subflow(msk)) {
3102 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003103 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003104 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003105
3106 /* active connections are already on conn_list, and we can't acquire
3107 * msk lock here.
3108 * use the join list lock as synchronization point and double-check
Paolo Abenie16163b2020-11-16 10:48:09 +01003109 * msk status to avoid racing with __mptcp_destroy_sock()
Paolo Abeni10f6d462020-05-29 17:43:30 +02003110 */
3111 spin_lock_bh(&msk->join_list_lock);
3112 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
Paolo Abenie16163b2020-11-16 10:48:09 +01003113 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
Paolo Abeni10f6d462020-05-29 17:43:30 +02003114 list_add_tail(&subflow->node, &msk->join_list);
Paolo Abenie16163b2020-11-16 10:48:09 +01003115 sock_hold(ssk);
3116 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003117 spin_unlock_bh(&msk->join_list_lock);
Florian Westphaldc87efd2021-04-01 16:19:44 -07003118 if (!ret) {
3119 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003120 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003121 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003122
3123 /* attach to msk socket only after we are sure he will deal with us
3124 * at close time
3125 */
Peter Krystadf2962342020-03-27 14:48:39 -07003126 parent_sock = READ_ONCE(parent->sk_socket);
Paolo Abenie16163b2020-11-16 10:48:09 +01003127 if (parent_sock && !ssk->sk_socket)
3128 mptcp_sock_graft(ssk, parent_sock);
Mat Martineau917944d2020-09-29 15:08:19 -07003129 subflow->map_seq = READ_ONCE(msk->ack_seq);
Florian Westphalb911c972021-02-12 16:00:01 -08003130out:
3131 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
Paolo Abeni10f6d462020-05-29 17:43:30 +02003132 return true;
Peter Krystadf2962342020-03-27 14:48:39 -07003133}
3134
Paolo Abeni76e2a552021-01-12 18:25:24 +01003135static void mptcp_shutdown(struct sock *sk, int how)
3136{
3137 pr_debug("sk=%p, how=%d", sk, how);
3138
3139 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3140 __mptcp_wr_shutdown(sk);
3141}
3142
Paolo Abeni65118822021-10-26 16:29:15 -07003143static int mptcp_forward_alloc_get(const struct sock *sk)
3144{
3145 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3146}
3147
Mat Martineauf870fa02020-01-21 16:56:15 -08003148static struct proto mptcp_prot = {
3149 .name = "MPTCP",
3150 .owner = THIS_MODULE,
3151 .init = mptcp_init_sock,
Paolo Abeni18b683b2020-03-27 14:48:43 -07003152 .disconnect = mptcp_disconnect,
Mat Martineauf870fa02020-01-21 16:56:15 -08003153 .close = mptcp_close,
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003154 .accept = mptcp_accept,
Peter Krystad717e79c2020-01-21 16:56:22 -08003155 .setsockopt = mptcp_setsockopt,
3156 .getsockopt = mptcp_getsockopt,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003157 .shutdown = mptcp_shutdown,
Peter Krystad79c09492020-01-21 16:56:20 -08003158 .destroy = mptcp_destroy,
Mat Martineauf870fa02020-01-21 16:56:15 -08003159 .sendmsg = mptcp_sendmsg,
3160 .recvmsg = mptcp_recvmsg,
Paolo Abeni14c441b2020-02-26 10:14:52 +01003161 .release_cb = mptcp_release_cb,
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003162 .hash = mptcp_hash,
3163 .unhash = mptcp_unhash,
Peter Krystadcec37a62020-01-21 16:56:18 -08003164 .get_port = mptcp_get_port,
Paolo Abeni65118822021-10-26 16:29:15 -07003165 .forward_alloc_get = mptcp_forward_alloc_get,
Paolo Abenid0272362020-03-27 14:48:45 -07003166 .sockets_allocated = &mptcp_sockets_allocated,
3167 .memory_allocated = &tcp_memory_allocated,
3168 .memory_pressure = &tcp_memory_pressure,
Paolo Abenid0272362020-03-27 14:48:45 -07003169 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
Paolo Abeni989ef492020-11-08 19:49:59 +01003170 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
Paolo Abenid0272362020-03-27 14:48:45 -07003171 .sysctl_mem = sysctl_tcp_mem,
Mat Martineauf870fa02020-01-21 16:56:15 -08003172 .obj_size = sizeof(struct mptcp_sock),
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003173 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Mat Martineauf870fa02020-01-21 16:56:15 -08003174 .no_autobind = true,
3175};
3176
Peter Krystad2303f992020-01-21 16:56:17 -08003177static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3178{
3179 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3180 struct socket *ssock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003181 int err;
Peter Krystad2303f992020-01-21 16:56:17 -08003182
3183 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003184 ssock = __mptcp_nmpc_socket(msk);
3185 if (!ssock) {
3186 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003187 goto unlock;
3188 }
3189
3190 err = ssock->ops->bind(ssock, uaddr, addr_len);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003191 if (!err)
3192 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08003193
3194unlock:
3195 release_sock(sock->sk);
3196 return err;
3197}
3198
Paolo Abeni0235d072020-07-23 13:02:31 +02003199static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3200 struct mptcp_subflow_context *subflow)
3201{
3202 subflow->request_mptcp = 0;
3203 __mptcp_do_fallback(msk);
3204}
3205
Peter Krystad2303f992020-01-21 16:56:17 -08003206static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3207 int addr_len, int flags)
3208{
3209 struct mptcp_sock *msk = mptcp_sk(sock->sk);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003210 struct mptcp_subflow_context *subflow;
Peter Krystad2303f992020-01-21 16:56:17 -08003211 struct socket *ssock;
3212 int err;
3213
3214 lock_sock(sock->sk);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003215 if (sock->state != SS_UNCONNECTED && msk->subflow) {
3216 /* pending connection or invalid state, let existing subflow
3217 * cope with that
3218 */
3219 ssock = msk->subflow;
3220 goto do_connect;
3221 }
3222
Paolo Abenifa680182020-06-29 22:26:23 +02003223 ssock = __mptcp_nmpc_socket(msk);
3224 if (!ssock) {
3225 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003226 goto unlock;
3227 }
3228
Paolo Abenifa680182020-06-29 22:26:23 +02003229 mptcp_token_destroy(msk);
3230 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003231 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003232#ifdef CONFIG_TCP_MD5SIG
3233 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3234 * TCP option space.
3235 */
3236 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
Paolo Abeni0235d072020-07-23 13:02:31 +02003237 mptcp_subflow_early_fallback(msk, subflow);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003238#endif
Paolo Abenia16195e2021-04-01 16:19:41 -07003239 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3240 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
Paolo Abeni0235d072020-07-23 13:02:31 +02003241 mptcp_subflow_early_fallback(msk, subflow);
Paolo Abenia16195e2021-04-01 16:19:41 -07003242 }
Paolo Abeni5695eb82021-04-01 16:19:42 -07003243 if (likely(!__mptcp_check_fallback(msk)))
3244 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003245
Paolo Abeni41be81a2020-05-29 17:43:29 +02003246do_connect:
Peter Krystad2303f992020-01-21 16:56:17 -08003247 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003248 sock->state = ssock->state;
3249
3250 /* on successful connect, the msk state will be moved to established by
3251 * subflow_finish_connect()
3252 */
Matthieu Baerts367fe042020-07-27 12:24:33 +02003253 if (!err || err == -EINPROGRESS)
Paolo Abeni41be81a2020-05-29 17:43:29 +02003254 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3255 else
3256 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
Peter Krystad2303f992020-01-21 16:56:17 -08003257
3258unlock:
3259 release_sock(sock->sk);
3260 return err;
3261}
3262
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003263static int mptcp_listen(struct socket *sock, int backlog)
3264{
3265 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3266 struct socket *ssock;
3267 int err;
3268
3269 pr_debug("msk=%p", msk);
3270
3271 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003272 ssock = __mptcp_nmpc_socket(msk);
3273 if (!ssock) {
3274 err = -EINVAL;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003275 goto unlock;
3276 }
3277
Paolo Abenifa680182020-06-29 22:26:23 +02003278 mptcp_token_destroy(msk);
3279 inet_sk_state_store(sock->sk, TCP_LISTEN);
Florian Westphal5e200872020-04-20 16:25:04 +02003280 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3281
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003282 err = ssock->ops->listen(ssock, backlog);
3283 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3284 if (!err)
3285 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3286
3287unlock:
3288 release_sock(sock->sk);
3289 return err;
3290}
3291
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003292static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3293 int flags, bool kern)
3294{
3295 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3296 struct socket *ssock;
3297 int err;
3298
3299 pr_debug("msk=%p", msk);
3300
3301 lock_sock(sock->sk);
3302 if (sock->sk->sk_state != TCP_LISTEN)
3303 goto unlock_fail;
3304
3305 ssock = __mptcp_nmpc_socket(msk);
3306 if (!ssock)
3307 goto unlock_fail;
3308
Paolo Abeni8a056612020-06-29 22:26:25 +02003309 clear_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003310 sock_hold(ssock->sk);
3311 release_sock(sock->sk);
3312
3313 err = ssock->ops->accept(sock, newsock, flags, kern);
Paolo Abenid2f77c52020-06-29 22:26:22 +02003314 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003315 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3316 struct mptcp_subflow_context *subflow;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003317 struct sock *newsk = newsock->sk;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003318
Florian Westphal4d54cc32021-02-12 15:59:59 -08003319 lock_sock(newsk);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003320
3321 /* PM/worker can now acquire the first subflow socket
3322 * lock without racing with listener queue cleanup,
3323 * we can notify it, if needed.
Florian Westphalc8fe62f2021-03-04 13:32:12 -08003324 *
3325 * Even if remote has reset the initial subflow by now
3326 * the refcnt is still at least one.
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003327 */
3328 subflow = mptcp_subflow_ctx(msk->first);
3329 list_add(&subflow->node, &msk->conn_list);
3330 sock_hold(msk->first);
3331 if (mptcp_is_fully_established(newsk))
Florian Westphal6c714f12021-02-12 15:59:58 -08003332 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003333
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003334 mptcp_copy_inaddrs(newsk, msk->first);
3335 mptcp_rcv_space_init(msk, msk->first);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003336 mptcp_propagate_sndbuf(newsk, msk->first);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003337
3338 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3339 * This is needed so NOSPACE flag can be set from tcp stack.
3340 */
Florian Westphal78962482021-04-15 16:44:53 -07003341 mptcp_flush_join_list(msk);
Geliang Tang190f8b02020-08-03 21:00:44 +08003342 mptcp_for_each_subflow(msk, subflow) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003343 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3344
3345 if (!ssk->sk_socket)
3346 mptcp_sock_graft(ssk, newsock);
3347 }
Florian Westphal4d54cc32021-02-12 15:59:59 -08003348 release_sock(newsk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003349 }
3350
Paolo Abeni8a056612020-06-29 22:26:25 +02003351 if (inet_csk_listen_poll(ssock->sk))
3352 set_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003353 sock_put(ssock->sk);
3354 return err;
3355
3356unlock_fail:
3357 release_sock(sock->sk);
3358 return -EINVAL;
3359}
3360
Paolo Abeni8a056612020-06-29 22:26:25 +02003361static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3362{
Paolo Abeni612f71d2021-10-07 15:05:00 -07003363 /* Concurrent splices from sk_receive_queue into receive_queue will
3364 * always show at least one non-empty queue when checked in this order.
3365 */
3366 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
3367 skb_queue_empty_lockless(&msk->receive_queue))
3368 return 0;
3369
3370 return EPOLLIN | EPOLLRDNORM;
Paolo Abeni8a056612020-06-29 22:26:25 +02003371}
3372
Florian Westphal8edf0862020-11-16 10:48:12 +01003373static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3374{
3375 struct sock *sk = (struct sock *)msk;
Florian Westphal8edf0862020-11-16 10:48:12 +01003376
3377 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
Paolo Abenidd913412021-02-11 15:30:38 -08003378 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003379
3380 if (sk_stream_is_writeable(sk))
3381 return EPOLLOUT | EPOLLWRNORM;
3382
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003383 mptcp_set_nospace(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003384 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3385 if (sk_stream_is_writeable(sk))
3386 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003387
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003388 return 0;
Florian Westphal8edf0862020-11-16 10:48:12 +01003389}
3390
Peter Krystad2303f992020-01-21 16:56:17 -08003391static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3392 struct poll_table_struct *wait)
3393{
Florian Westphal1891c4a2020-01-21 16:56:25 -08003394 struct sock *sk = sock->sk;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08003395 struct mptcp_sock *msk;
Peter Krystad2303f992020-01-21 16:56:17 -08003396 __poll_t mask = 0;
Paolo Abeni8a056612020-06-29 22:26:25 +02003397 int state;
Peter Krystad2303f992020-01-21 16:56:17 -08003398
Florian Westphal1891c4a2020-01-21 16:56:25 -08003399 msk = mptcp_sk(sk);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003400 sock_poll_wait(file, sock, wait);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003401
Paolo Abeni8a056612020-06-29 22:26:25 +02003402 state = inet_sk_state_load(sk);
Paolo Abeni67193312020-09-14 10:01:09 +02003403 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
Paolo Abeni8a056612020-06-29 22:26:25 +02003404 if (state == TCP_LISTEN)
Paolo Abeni612f71d2021-10-07 15:05:00 -07003405 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0;
Paolo Abeni8a056612020-06-29 22:26:25 +02003406
3407 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3408 mask |= mptcp_check_readable(msk);
Florian Westphal8edf0862020-11-16 10:48:12 +01003409 mask |= mptcp_check_writeable(msk);
Paolo Abeni8a056612020-06-29 22:26:25 +02003410 }
Paolo Abenidd913412021-02-11 15:30:38 -08003411 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3412 mask |= EPOLLHUP;
Florian Westphal1891c4a2020-01-21 16:56:25 -08003413 if (sk->sk_shutdown & RCV_SHUTDOWN)
3414 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3415
Paolo Abeni15cc1042021-02-11 15:30:37 -08003416 /* This barrier is coupled with smp_wmb() in tcp_reset() */
3417 smp_rmb();
3418 if (sk->sk_err)
3419 mask |= EPOLLERR;
3420
Peter Krystad2303f992020-01-21 16:56:17 -08003421 return mask;
3422}
3423
Florian Westphale42f1ac2020-01-24 16:04:02 -08003424static const struct proto_ops mptcp_stream_ops = {
3425 .family = PF_INET,
3426 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003427 .release = inet_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003428 .bind = mptcp_bind,
3429 .connect = mptcp_stream_connect,
3430 .socketpair = sock_no_socketpair,
3431 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003432 .getname = inet_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003433 .poll = mptcp_poll,
3434 .ioctl = inet_ioctl,
3435 .gettstamp = sock_gettstamp,
3436 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003437 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003438 .setsockopt = sock_common_setsockopt,
3439 .getsockopt = sock_common_getsockopt,
3440 .sendmsg = inet_sendmsg,
3441 .recvmsg = inet_recvmsg,
3442 .mmap = sock_no_mmap,
3443 .sendpage = inet_sendpage,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003444};
Peter Krystad2303f992020-01-21 16:56:17 -08003445
Mat Martineauf870fa02020-01-21 16:56:15 -08003446static struct inet_protosw mptcp_protosw = {
3447 .type = SOCK_STREAM,
3448 .protocol = IPPROTO_MPTCP,
3449 .prot = &mptcp_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003450 .ops = &mptcp_stream_ops,
3451 .flags = INET_PROTOSW_ICSK,
Mat Martineauf870fa02020-01-21 16:56:15 -08003452};
3453
Paolo Abenib19bc292021-01-20 15:39:14 +01003454static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3455{
3456 struct mptcp_delegated_action *delegated;
3457 struct mptcp_subflow_context *subflow;
3458 int work_done = 0;
3459
3460 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3461 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3462 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3463
3464 bh_lock_sock_nested(ssk);
3465 if (!sock_owned_by_user(ssk) &&
3466 mptcp_subflow_has_delegated_action(subflow))
3467 mptcp_subflow_process_delegated(ssk);
3468 /* ... elsewhere tcp_release_cb_override already processed
3469 * the action or will do at next release_sock().
3470 * In both case must dequeue the subflow here - on the same
3471 * CPU that scheduled it.
3472 */
3473 bh_unlock_sock(ssk);
3474 sock_put(ssk);
3475
3476 if (++work_done == budget)
3477 return budget;
3478 }
3479
3480 /* always provide a 0 'work_done' argument, so that napi_complete_done
3481 * will not try accessing the NULL napi->dev ptr
3482 */
3483 napi_complete_done(napi, 0);
3484 return work_done;
3485}
3486
Paolo Abenid39dcec2020-06-26 19:29:59 +02003487void __init mptcp_proto_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003488{
Paolo Abenib19bc292021-01-20 15:39:14 +01003489 struct mptcp_delegated_action *delegated;
3490 int cpu;
3491
Peter Krystad2303f992020-01-21 16:56:17 -08003492 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
Peter Krystad2303f992020-01-21 16:56:17 -08003493
Paolo Abenid0272362020-03-27 14:48:45 -07003494 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3495 panic("Failed to allocate MPTCP pcpu counter\n");
3496
Paolo Abenib19bc292021-01-20 15:39:14 +01003497 init_dummy_netdev(&mptcp_napi_dev);
3498 for_each_possible_cpu(cpu) {
3499 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3500 INIT_LIST_HEAD(&delegated->head);
3501 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3502 NAPI_POLL_WEIGHT);
3503 napi_enable(&delegated->napi);
3504 }
3505
Peter Krystad2303f992020-01-21 16:56:17 -08003506 mptcp_subflow_init();
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003507 mptcp_pm_init();
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003508 mptcp_token_init();
Peter Krystad2303f992020-01-21 16:56:17 -08003509
Mat Martineauf870fa02020-01-21 16:56:15 -08003510 if (proto_register(&mptcp_prot, 1) != 0)
3511 panic("Failed to register MPTCP proto.\n");
3512
3513 inet_register_protosw(&mptcp_protosw);
Florian Westphal6771bfd2020-02-26 10:14:48 +01003514
3515 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
Mat Martineauf870fa02020-01-21 16:56:15 -08003516}
3517
3518#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Florian Westphale42f1ac2020-01-24 16:04:02 -08003519static const struct proto_ops mptcp_v6_stream_ops = {
3520 .family = PF_INET6,
3521 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003522 .release = inet6_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003523 .bind = mptcp_bind,
3524 .connect = mptcp_stream_connect,
3525 .socketpair = sock_no_socketpair,
3526 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003527 .getname = inet6_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003528 .poll = mptcp_poll,
3529 .ioctl = inet6_ioctl,
3530 .gettstamp = sock_gettstamp,
3531 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003532 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003533 .setsockopt = sock_common_setsockopt,
3534 .getsockopt = sock_common_getsockopt,
3535 .sendmsg = inet6_sendmsg,
3536 .recvmsg = inet6_recvmsg,
3537 .mmap = sock_no_mmap,
3538 .sendpage = inet_sendpage,
3539#ifdef CONFIG_COMPAT
Christoph Hellwig39869122020-05-18 08:28:06 +02003540 .compat_ioctl = inet6_compat_ioctl,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003541#endif
3542};
3543
Mat Martineauf870fa02020-01-21 16:56:15 -08003544static struct proto mptcp_v6_prot;
3545
Peter Krystad79c09492020-01-21 16:56:20 -08003546static void mptcp_v6_destroy(struct sock *sk)
3547{
3548 mptcp_destroy(sk);
3549 inet6_destroy_sock(sk);
3550}
3551
Mat Martineauf870fa02020-01-21 16:56:15 -08003552static struct inet_protosw mptcp_v6_protosw = {
3553 .type = SOCK_STREAM,
3554 .protocol = IPPROTO_MPTCP,
3555 .prot = &mptcp_v6_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003556 .ops = &mptcp_v6_stream_ops,
Mat Martineauf870fa02020-01-21 16:56:15 -08003557 .flags = INET_PROTOSW_ICSK,
3558};
3559
Paolo Abenid39dcec2020-06-26 19:29:59 +02003560int __init mptcp_proto_v6_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003561{
3562 int err;
3563
3564 mptcp_v6_prot = mptcp_prot;
3565 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3566 mptcp_v6_prot.slab = NULL;
Peter Krystad79c09492020-01-21 16:56:20 -08003567 mptcp_v6_prot.destroy = mptcp_v6_destroy;
Florian Westphalb0519de2020-02-06 00:39:37 +01003568 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
Mat Martineauf870fa02020-01-21 16:56:15 -08003569
3570 err = proto_register(&mptcp_v6_prot, 1);
3571 if (err)
3572 return err;
3573
Mat Martineauf870fa02020-01-21 16:56:15 -08003574 err = inet6_register_protosw(&mptcp_v6_protosw);
3575 if (err)
3576 proto_unregister(&mptcp_v6_prot);
3577
3578 return err;
3579}
3580#endif