blob: 943f74e804bd05bd39a8f5eba5530145b2aaaf54 [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>
Florian Westphal644807e2021-12-03 14:35:34 -080025#include <asm/ioctls.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080026#include "protocol.h"
Florian Westphalfc518952020-03-27 14:48:50 -070027#include "mib.h"
Mat Martineauf870fa02020-01-21 16:56:15 -080028
Geliang Tange10a9892021-04-16 15:38:04 -070029#define CREATE_TRACE_POINTS
30#include <trace/events/mptcp.h>
31
Florian Westphalb0519de2020-02-06 00:39:37 +010032#if IS_ENABLED(CONFIG_MPTCP_IPV6)
33struct mptcp6_sock {
34 struct mptcp_sock msk;
35 struct ipv6_pinfo np;
36};
37#endif
38
Florian Westphal6771bfd2020-02-26 10:14:48 +010039struct mptcp_skb_cb {
Paolo Abeniab174ad2020-09-14 10:01:12 +020040 u64 map_seq;
41 u64 end_seq;
Florian Westphal6771bfd2020-02-26 10:14:48 +010042 u32 offset;
Florian Westphalb7f653b2021-06-03 16:24:32 -070043 u8 has_rxtstamp:1;
Florian Westphal6771bfd2020-02-26 10:14:48 +010044};
45
46#define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
47
Florian Westphalb7f653b2021-06-03 16:24:32 -070048enum {
49 MPTCP_CMSG_TS = BIT(0),
Florian Westphal2c9e7762021-12-03 14:35:32 -080050 MPTCP_CMSG_INQ = BIT(1),
Florian Westphalb7f653b2021-06-03 16:24:32 -070051};
52
Eric Dumazet91b6d322021-11-15 11:02:39 -080053static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
Paolo Abenid0272362020-03-27 14:48:45 -070054
Paolo Abenie16163b2020-11-16 10:48:09 +010055static void __mptcp_destroy_sock(struct sock *sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +010056static void __mptcp_check_send_data_fin(struct sock *sk);
Paolo Abenie16163b2020-11-16 10:48:09 +010057
Paolo Abenib19bc292021-01-20 15:39:14 +010058DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
59static struct net_device mptcp_napi_dev;
60
Peter Krystad2303f992020-01-21 16:56:17 -080061/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
62 * completed yet or has failed, return the subflow socket.
63 * Otherwise return NULL.
64 */
Geliang Tang1729cf12021-02-01 15:09:12 -080065struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -080066{
Christoph Paaschd22f4982020-01-21 16:56:32 -080067 if (!msk->subflow || READ_ONCE(msk->can_ack))
Peter Krystad2303f992020-01-21 16:56:17 -080068 return NULL;
69
70 return msk->subflow;
71}
72
Florian Westphal6f8a6122020-11-16 10:48:13 +010073/* Returns end sequence number of the receiver's advertised window */
74static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
75{
Paolo Abeni7439d682020-11-27 11:10:26 +010076 return READ_ONCE(msk->wnd_end);
Florian Westphal6f8a6122020-11-16 10:48:13 +010077}
78
Paolo Abenid2f77c52020-06-29 22:26:22 +020079static bool mptcp_is_tcpsk(struct sock *sk)
Florian Westphal0b4f33d2020-04-02 13:44:51 +020080{
81 struct socket *sock = sk->sk_socket;
82
Florian Westphal0b4f33d2020-04-02 13:44:51 +020083 if (unlikely(sk->sk_prot == &tcp_prot)) {
84 /* we are being invoked after mptcp_accept() has
85 * accepted a non-mp-capable flow: sk is a tcp_sk,
86 * not an mptcp one.
87 *
88 * Hand the socket over to tcp so all further socket ops
89 * bypass mptcp.
90 */
91 sock->ops = &inet_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020092 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020093#if IS_ENABLED(CONFIG_MPTCP_IPV6)
94 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
95 sock->ops = &inet6_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020096 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020097#endif
98 }
99
Paolo Abenid2f77c52020-06-29 22:26:22 +0200100 return false;
Florian Westphal0b4f33d2020-04-02 13:44:51 +0200101}
102
Paolo Abenifa680182020-06-29 22:26:23 +0200103static int __mptcp_socket_create(struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -0800104{
105 struct mptcp_subflow_context *subflow;
106 struct sock *sk = (struct sock *)msk;
107 struct socket *ssock;
108 int err;
109
Peter Krystad2303f992020-01-21 16:56:17 -0800110 err = mptcp_subflow_create_socket(sk, &ssock);
111 if (err)
Paolo Abenifa680182020-06-29 22:26:23 +0200112 return err;
Peter Krystad2303f992020-01-21 16:56:17 -0800113
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800114 msk->first = ssock->sk;
Peter Krystad2303f992020-01-21 16:56:17 -0800115 msk->subflow = ssock;
116 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800117 list_add(&subflow->node, &msk->conn_list);
Paolo Abenie16163b2020-11-16 10:48:09 +0100118 sock_hold(ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -0800119 subflow->request_mptcp = 1;
Paolo Abeni866f26f2021-01-20 15:39:10 +0100120 mptcp_sock_graft(msk->first, sk->sk_socket);
Davide Carattie1ff9e82020-06-29 22:26:20 +0200121
Paolo Abenifa680182020-06-29 22:26:23 +0200122 return 0;
Peter Krystad2303f992020-01-21 16:56:17 -0800123}
124
Paolo Abeniab174ad2020-09-14 10:01:12 +0200125static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
126{
127 sk_drops_add(sk, skb);
128 __kfree_skb(skb);
129}
130
Paolo Abeni65118822021-10-26 16:29:15 -0700131static void mptcp_rmem_charge(struct sock *sk, int size)
132{
133 mptcp_sk(sk)->rmem_fwd_alloc -= size;
134}
135
Paolo Abeni8268ed42020-09-14 10:01:11 +0200136static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
137 struct sk_buff *from)
138{
139 bool fragstolen;
140 int delta;
141
142 if (MPTCP_SKB_CB(from)->offset ||
143 !skb_try_coalesce(to, from, &fragstolen, &delta))
144 return false;
145
Paolo Abeni06242e42020-09-14 10:01:14 +0200146 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
147 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
148 to->len, MPTCP_SKB_CB(from)->end_seq);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200149 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200150 kfree_skb_partial(from, fragstolen);
151 atomic_add(delta, &sk->sk_rmem_alloc);
Paolo Abeni65118822021-10-26 16:29:15 -0700152 mptcp_rmem_charge(sk, delta);
Paolo Abeni8268ed42020-09-14 10:01:11 +0200153 return true;
154}
155
Paolo Abeniab174ad2020-09-14 10:01:12 +0200156static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
157 struct sk_buff *from)
Florian Westphal6771bfd2020-02-26 10:14:48 +0100158{
Paolo Abeniab174ad2020-09-14 10:01:12 +0200159 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
160 return false;
161
162 return mptcp_try_coalesce((struct sock *)msk, to, from);
163}
164
Paolo Abeni65118822021-10-26 16:29:15 -0700165static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
166{
167 amount >>= SK_MEM_QUANTUM_SHIFT;
168 mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
169 __sk_mem_reduce_allocated(sk, amount);
170}
171
172static void mptcp_rmem_uncharge(struct sock *sk, int size)
173{
174 struct mptcp_sock *msk = mptcp_sk(sk);
175 int reclaimable;
176
177 msk->rmem_fwd_alloc += size;
178 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
179
180 /* see sk_mem_uncharge() for the rationale behind the following schema */
181 if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD))
182 __mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK);
183}
184
185static void mptcp_rfree(struct sk_buff *skb)
186{
187 unsigned int len = skb->truesize;
188 struct sock *sk = skb->sk;
189
190 atomic_sub(len, &sk->sk_rmem_alloc);
191 mptcp_rmem_uncharge(sk, len);
192}
193
194static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
195{
196 skb_orphan(skb);
197 skb->sk = sk;
198 skb->destructor = mptcp_rfree;
199 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
200 mptcp_rmem_charge(sk, skb->truesize);
201}
202
Paolo Abeniab174ad2020-09-14 10:01:12 +0200203/* "inspired" by tcp_data_queue_ofo(), main differences:
204 * - use mptcp seqs
205 * - don't cope with sacks
206 */
207static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
208{
209 struct sock *sk = (struct sock *)msk;
210 struct rb_node **p, *parent;
211 u64 seq, end_seq, max_seq;
212 struct sk_buff *skb1;
213
214 seq = MPTCP_SKB_CB(skb)->map_seq;
215 end_seq = MPTCP_SKB_CB(skb)->end_seq;
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800216 max_seq = READ_ONCE(msk->rcv_wnd_sent);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200217
Paolo Abeni06242e42020-09-14 10:01:14 +0200218 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
219 RB_EMPTY_ROOT(&msk->out_of_order_queue));
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800220 if (after64(end_seq, max_seq)) {
Paolo Abeniab174ad2020-09-14 10:01:12 +0200221 /* out of window */
222 mptcp_drop(sk, skb);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800223 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
224 (unsigned long long)end_seq - (unsigned long)max_seq,
225 (unsigned long long)msk->rcv_wnd_sent);
Paolo Abeni06242e42020-09-14 10:01:14 +0200226 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200227 return;
228 }
229
230 p = &msk->out_of_order_queue.rb_node;
Paolo Abeni06242e42020-09-14 10:01:14 +0200231 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200232 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
233 rb_link_node(&skb->rbnode, NULL, p);
234 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
235 msk->ooo_last_skb = skb;
236 goto end;
237 }
238
239 /* with 2 subflows, adding at end of ooo queue is quite likely
240 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
241 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200242 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
243 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
244 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200245 return;
Paolo Abeni06242e42020-09-14 10:01:14 +0200246 }
Paolo Abeniab174ad2020-09-14 10:01:12 +0200247
248 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
249 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200250 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200251 parent = &msk->ooo_last_skb->rbnode;
252 p = &parent->rb_right;
253 goto insert;
254 }
255
256 /* Find place to insert this segment. Handle overlaps on the way. */
257 parent = NULL;
258 while (*p) {
259 parent = *p;
260 skb1 = rb_to_skb(parent);
261 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
262 p = &parent->rb_left;
263 continue;
264 }
265 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
266 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
267 /* All the bits are present. Drop. */
268 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200269 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200270 return;
271 }
272 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
273 /* partial overlap:
274 * | skb |
275 * | skb1 |
276 * continue traversing
277 */
278 } else {
279 /* skb's seq == skb1's seq and skb covers skb1.
280 * Replace skb1 with skb.
281 */
282 rb_replace_node(&skb1->rbnode, &skb->rbnode,
283 &msk->out_of_order_queue);
284 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200285 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200286 goto merge_right;
287 }
288 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200289 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200290 return;
291 }
292 p = &parent->rb_right;
293 }
Paolo Abeni06242e42020-09-14 10:01:14 +0200294
Paolo Abeniab174ad2020-09-14 10:01:12 +0200295insert:
296 /* Insert segment into RB tree. */
297 rb_link_node(&skb->rbnode, parent, p);
298 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
299
300merge_right:
301 /* Remove other segments covered by skb. */
302 while ((skb1 = skb_rb_next(skb)) != NULL) {
303 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
304 break;
305 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
306 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200307 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200308 }
309 /* If there is no skb after us, we are the last_skb ! */
310 if (!skb1)
311 msk->ooo_last_skb = skb;
312
313end:
314 skb_condense(skb);
Paolo Abeni65118822021-10-26 16:29:15 -0700315 mptcp_set_owner_r(skb, sk);
316}
317
318static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
319{
320 struct mptcp_sock *msk = mptcp_sk(sk);
321 int amt, amount;
322
323 if (size < msk->rmem_fwd_alloc)
324 return true;
325
326 amt = sk_mem_pages(size);
327 amount = amt << SK_MEM_QUANTUM_SHIFT;
328 msk->rmem_fwd_alloc += amount;
329 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) {
330 if (ssk->sk_forward_alloc < amount) {
331 msk->rmem_fwd_alloc -= amount;
332 return false;
333 }
334
335 ssk->sk_forward_alloc -= amount;
336 }
337 return true;
Paolo Abeniab174ad2020-09-14 10:01:12 +0200338}
339
340static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
341 struct sk_buff *skb, unsigned int offset,
342 size_t copy_len)
343{
344 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100345 struct sock *sk = (struct sock *)msk;
Florian Westphal4e637c72020-05-25 23:41:13 +0200346 struct sk_buff *tail;
Florian Westphalb7f653b2021-06-03 16:24:32 -0700347 bool has_rxtstamp;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100348
349 __skb_unlink(skb, &ssk->sk_receive_queue);
Florian Westphal4e637c72020-05-25 23:41:13 +0200350
351 skb_ext_reset(skb);
352 skb_orphan(skb);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200353
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100354 /* try to fetch required memory from subflow */
Paolo Abeni65118822021-10-26 16:29:15 -0700355 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
356 goto drop;
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100357
Florian Westphalb7f653b2021-06-03 16:24:32 -0700358 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
359
Paolo Abeniab174ad2020-09-14 10:01:12 +0200360 /* the skb map_seq accounts for the skb offset:
361 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
362 * value
363 */
364 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
365 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200366 MPTCP_SKB_CB(skb)->offset = offset;
Florian Westphalb7f653b2021-06-03 16:24:32 -0700367 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
Florian Westphal4e637c72020-05-25 23:41:13 +0200368
Paolo Abeniab174ad2020-09-14 10:01:12 +0200369 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
370 /* in sequence */
David S. Miller8b0308f2020-10-05 17:33:26 -0700371 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200372 tail = skb_peek_tail(&sk->sk_receive_queue);
373 if (tail && mptcp_try_coalesce(sk, tail, skb))
374 return true;
Florian Westphal4e637c72020-05-25 23:41:13 +0200375
Paolo Abeni65118822021-10-26 16:29:15 -0700376 mptcp_set_owner_r(skb, sk);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200377 __skb_queue_tail(&sk->sk_receive_queue, skb);
378 return true;
379 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
380 mptcp_data_queue_ofo(msk, skb);
381 return false;
382 }
383
384 /* old data, keep it simple and drop the whole pkt, sender
385 * will retransmit as needed, if needed.
386 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200387 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100388drop:
Paolo Abeniab174ad2020-09-14 10:01:12 +0200389 mptcp_drop(sk, skb);
390 return false;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100391}
392
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700393static void mptcp_stop_timer(struct sock *sk)
394{
395 struct inet_connection_sock *icsk = inet_csk(sk);
396
397 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
398 mptcp_sk(sk)->timer_ival = 0;
399}
400
Paolo Abenie16163b2020-11-16 10:48:09 +0100401static void mptcp_close_wake_up(struct sock *sk)
402{
403 if (sock_flag(sk, SOCK_DEAD))
404 return;
405
406 sk->sk_state_change(sk);
407 if (sk->sk_shutdown == SHUTDOWN_MASK ||
408 sk->sk_state == TCP_CLOSE)
409 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
410 else
411 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
412}
413
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100414static bool mptcp_pending_data_fin_ack(struct sock *sk)
415{
416 struct mptcp_sock *msk = mptcp_sk(sk);
417
418 return !__mptcp_check_fallback(msk) &&
419 ((1 << sk->sk_state) &
420 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
421 msk->write_seq == READ_ONCE(msk->snd_una);
422}
423
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700424static void mptcp_check_data_fin_ack(struct sock *sk)
425{
426 struct mptcp_sock *msk = mptcp_sk(sk);
427
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700428 /* Look for an acknowledged DATA_FIN */
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100429 if (mptcp_pending_data_fin_ack(sk)) {
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700430 WRITE_ONCE(msk->snd_data_fin_enable, 0);
431
432 switch (sk->sk_state) {
433 case TCP_FIN_WAIT1:
434 inet_sk_state_store(sk, TCP_FIN_WAIT2);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700435 break;
436 case TCP_CLOSING:
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700437 case TCP_LAST_ACK:
438 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700439 break;
440 }
441
Paolo Abenie16163b2020-11-16 10:48:09 +0100442 mptcp_close_wake_up(sk);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700443 }
444}
445
Mat Martineau3721b9b2020-07-28 15:12:03 -0700446static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
447{
448 struct mptcp_sock *msk = mptcp_sk(sk);
449
450 if (READ_ONCE(msk->rcv_data_fin) &&
451 ((1 << sk->sk_state) &
452 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
453 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
454
455 if (msk->ack_seq == rcv_data_fin_seq) {
456 if (seq)
457 *seq = rcv_data_fin_seq;
458
459 return true;
460 }
461 }
462
463 return false;
464}
465
Mat Martineau6477dd32021-04-23 09:40:33 -0700466static void mptcp_set_datafin_timeout(const struct sock *sk)
467{
468 struct inet_connection_sock *icsk = inet_csk(sk);
469
470 mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX,
471 TCP_RTO_MIN << icsk->icsk_retransmits);
472}
473
Paolo Abeni33d41c92021-08-13 15:15:41 -0700474static void __mptcp_set_timeout(struct sock *sk, long tout)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700475{
Mat Martineau3721b9b2020-07-28 15:12:03 -0700476 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
477}
478
Paolo Abeni33d41c92021-08-13 15:15:41 -0700479static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
480{
481 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
482
Paolo Abeni71b7dec2021-08-13 15:15:42 -0700483 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
484 inet_csk(ssk)->icsk_timeout - jiffies : 0;
Paolo Abeni33d41c92021-08-13 15:15:41 -0700485}
486
487static void mptcp_set_timeout(struct sock *sk)
488{
489 struct mptcp_subflow_context *subflow;
490 long tout = 0;
491
492 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
493 tout = max(tout, mptcp_timeout_from_subflow(subflow));
494 __mptcp_set_timeout(sk, tout);
495}
496
Paolo Abenifd897672020-11-24 22:51:24 +0100497static bool tcp_can_send_ack(const struct sock *ssk)
498{
499 return !((1 << inet_sk_state_load(ssk)) &
Paolo Abeni20bc80b2021-01-12 18:25:23 +0100500 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
Paolo Abenifd897672020-11-24 22:51:24 +0100501}
502
Mat Martineau340fa662021-09-02 11:51:19 -0700503void mptcp_subflow_send_ack(struct sock *ssk)
504{
505 bool slow;
506
507 slow = lock_sock_fast(ssk);
508 if (tcp_can_send_ack(ssk))
509 tcp_send_ack(ssk);
510 unlock_sock_fast(ssk, slow);
511}
512
Paolo Abenifd897672020-11-24 22:51:24 +0100513static void mptcp_send_ack(struct mptcp_sock *msk)
Paolo Abeni7ed90802020-11-16 10:48:14 +0100514{
515 struct mptcp_subflow_context *subflow;
516
Mat Martineau340fa662021-09-02 11:51:19 -0700517 mptcp_for_each_subflow(msk, subflow)
518 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
Paolo Abenifd897672020-11-24 22:51:24 +0100519}
Paolo Abeniea4ca582020-11-19 11:46:03 -0800520
Paolo Abenifde56ee2021-06-22 12:25:23 -0700521static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
Paolo Abenifd897672020-11-24 22:51:24 +0100522{
Paolo Abeni75e908c2021-06-21 15:54:34 -0700523 bool slow;
Paolo Abenifd897672020-11-24 22:51:24 +0100524
Paolo Abeni75e908c2021-06-21 15:54:34 -0700525 slow = lock_sock_fast(ssk);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700526 if (tcp_can_send_ack(ssk))
Paolo Abenifd897672020-11-24 22:51:24 +0100527 tcp_cleanup_rbuf(ssk, 1);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700528 unlock_sock_fast(ssk, slow);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700529}
530
531static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
532{
533 const struct inet_connection_sock *icsk = inet_csk(ssk);
Matthieu Baertsc4512c62021-06-25 14:25:22 -0700534 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
Paolo Abenifde56ee2021-06-22 12:25:23 -0700535 const struct tcp_sock *tp = tcp_sk(ssk);
536
537 return (ack_pending & ICSK_ACK_SCHED) &&
538 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
539 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
540 (rx_empty && ack_pending &
541 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
Paolo Abenifd897672020-11-24 22:51:24 +0100542}
543
544static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
545{
Paolo Abenie3859602021-02-11 15:30:41 -0800546 int old_space = READ_ONCE(msk->old_wspace);
Paolo Abenifd897672020-11-24 22:51:24 +0100547 struct mptcp_subflow_context *subflow;
Paolo Abenie3859602021-02-11 15:30:41 -0800548 struct sock *sk = (struct sock *)msk;
Paolo Abenifde56ee2021-06-22 12:25:23 -0700549 int space = __mptcp_space(sk);
550 bool cleanup, rx_empty;
Paolo Abenie3859602021-02-11 15:30:41 -0800551
Paolo Abenifde56ee2021-06-22 12:25:23 -0700552 cleanup = (space > 0) && (space >= (old_space << 1));
Paolo Abenice599c52021-07-09 17:20:51 -0700553 rx_empty = !__mptcp_rmem(sk);
Paolo Abenifd897672020-11-24 22:51:24 +0100554
Paolo Abenifde56ee2021-06-22 12:25:23 -0700555 mptcp_for_each_subflow(msk, subflow) {
556 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenifd897672020-11-24 22:51:24 +0100557
Paolo Abenifde56ee2021-06-22 12:25:23 -0700558 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
559 mptcp_subflow_cleanup_rbuf(ssk);
Paolo Abeniea4ca582020-11-19 11:46:03 -0800560 }
Paolo Abeni7ed90802020-11-16 10:48:14 +0100561}
562
563static bool mptcp_check_data_fin(struct sock *sk)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700564{
565 struct mptcp_sock *msk = mptcp_sk(sk);
566 u64 rcv_data_fin_seq;
Paolo Abeni7ed90802020-11-16 10:48:14 +0100567 bool ret = false;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700568
Paolo Abeni781bf132021-04-01 16:19:43 -0700569 if (__mptcp_check_fallback(msk))
Paolo Abeni7ed90802020-11-16 10:48:14 +0100570 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700571
572 /* Need to ack a DATA_FIN received from a peer while this side
573 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
574 * msk->rcv_data_fin was set when parsing the incoming options
575 * at the subflow level and the msk lock was not held, so this
576 * is the first opportunity to act on the DATA_FIN and change
577 * the msk state.
578 *
579 * If we are caught up to the sequence number of the incoming
580 * DATA_FIN, send the DATA_ACK now and do state transition. If
581 * not caught up, do nothing and let the recv code send DATA_ACK
582 * when catching up.
583 */
584
585 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
Mat Martineau917944d2020-09-29 15:08:19 -0700586 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700587 WRITE_ONCE(msk->rcv_data_fin, 0);
588
589 sk->sk_shutdown |= RCV_SHUTDOWN;
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700590 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Mat Martineau3721b9b2020-07-28 15:12:03 -0700591
592 switch (sk->sk_state) {
593 case TCP_ESTABLISHED:
594 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
595 break;
596 case TCP_FIN_WAIT1:
597 inet_sk_state_store(sk, TCP_CLOSING);
598 break;
599 case TCP_FIN_WAIT2:
600 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700601 break;
602 default:
603 /* Other states not expected */
604 WARN_ON_ONCE(1);
605 break;
606 }
607
Paolo Abeni7ed90802020-11-16 10:48:14 +0100608 ret = true;
Paolo Abenifd897672020-11-24 22:51:24 +0100609 mptcp_send_ack(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +0100610 mptcp_close_wake_up(sk);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700611 }
Paolo Abeni7ed90802020-11-16 10:48:14 +0100612 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700613}
614
Florian Westphal6771bfd2020-02-26 10:14:48 +0100615static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
616 struct sock *ssk,
617 unsigned int *bytes)
618{
619 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100620 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100621 unsigned int moved = 0;
622 bool more_data_avail;
623 struct tcp_sock *tp;
624 bool done = false;
Florian Westphal13c7ba02020-11-03 11:05:03 -0800625 int sk_rbuf;
626
627 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
628
629 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
630 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
631
632 if (unlikely(ssk_rbuf > sk_rbuf)) {
633 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
634 sk_rbuf = ssk_rbuf;
635 }
636 }
Florian Westphal600911f2020-02-26 10:14:49 +0100637
Paolo Abeniab174ad2020-09-14 10:01:12 +0200638 pr_debug("msk=%p ssk=%p", msk, ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100639 tp = tcp_sk(ssk);
640 do {
641 u32 map_remaining, offset;
642 u32 seq = tp->copied_seq;
643 struct sk_buff *skb;
644 bool fin;
645
646 /* try to move as much data as available */
647 map_remaining = subflow->map_data_len -
648 mptcp_subflow_get_map_offset(subflow);
649
650 skb = skb_peek(&ssk->sk_receive_queue);
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200651 if (!skb) {
652 /* if no data is found, a racing workqueue/recvmsg
653 * already processed the new data, stop here or we
654 * can enter an infinite loop
655 */
656 if (!moved)
657 done = true;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100658 break;
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200659 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100660
Davide Carattie1ff9e82020-06-29 22:26:20 +0200661 if (__mptcp_check_fallback(msk)) {
662 /* if we are running under the workqueue, TCP could have
663 * collapsed skbs between dummy map creation and now
664 * be sure to adjust the size
665 */
666 map_remaining = skb->len;
667 subflow->map_data_len = skb->len;
668 }
669
Florian Westphal6771bfd2020-02-26 10:14:48 +0100670 offset = seq - TCP_SKB_CB(skb)->seq;
671 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
672 if (fin) {
673 done = true;
674 seq++;
675 }
676
677 if (offset < skb->len) {
678 size_t len = skb->len - offset;
679
680 if (tp->urg_data)
681 done = true;
682
Paolo Abeniab174ad2020-09-14 10:01:12 +0200683 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
684 moved += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100685 seq += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100686
687 if (WARN_ON_ONCE(map_remaining < len))
688 break;
689 } else {
690 WARN_ON_ONCE(!fin);
691 sk_eat_skb(ssk, skb);
692 done = true;
693 }
694
695 WRITE_ONCE(tp->copied_seq, seq);
696 more_data_avail = mptcp_subflow_data_available(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100697
Florian Westphal13c7ba02020-11-03 11:05:03 -0800698 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
Florian Westphal600911f2020-02-26 10:14:49 +0100699 done = true;
700 break;
701 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100702 } while (more_data_avail);
703
Paolo Abeni67193312020-09-14 10:01:09 +0200704 *bytes += moved;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100705 return done;
706}
707
Paolo Abeni87952602020-11-27 11:10:24 +0100708static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
Paolo Abeniab174ad2020-09-14 10:01:12 +0200709{
710 struct sock *sk = (struct sock *)msk;
711 struct sk_buff *skb, *tail;
712 bool moved = false;
713 struct rb_node *p;
714 u64 end_seq;
715
716 p = rb_first(&msk->out_of_order_queue);
Paolo Abeni06242e42020-09-14 10:01:14 +0200717 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
Paolo Abeniab174ad2020-09-14 10:01:12 +0200718 while (p) {
719 skb = rb_to_skb(p);
720 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
721 break;
722
723 p = rb_next(p);
724 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
725
726 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
727 msk->ack_seq))) {
728 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200729 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200730 continue;
731 }
732
733 end_seq = MPTCP_SKB_CB(skb)->end_seq;
734 tail = skb_peek_tail(&sk->sk_receive_queue);
735 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
736 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
737
738 /* skip overlapping data, if any */
Paolo Abeni06242e42020-09-14 10:01:14 +0200739 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
740 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
741 delta);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200742 MPTCP_SKB_CB(skb)->offset += delta;
Florian Westphal2c9e7762021-12-03 14:35:32 -0800743 MPTCP_SKB_CB(skb)->map_seq += delta;
Paolo Abeniab174ad2020-09-14 10:01:12 +0200744 __skb_queue_tail(&sk->sk_receive_queue, skb);
745 }
746 msk->ack_seq = end_seq;
747 moved = true;
748 }
749 return moved;
750}
751
Florian Westphal2e522132020-02-26 10:14:51 +0100752/* In most cases we will be able to lock the mptcp socket. If its already
753 * owned, we need to defer to the work queue to avoid ABBA deadlock.
754 */
Paolo Abeni99d10552021-06-10 15:59:41 -0700755static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
Florian Westphal2e522132020-02-26 10:14:51 +0100756{
757 struct sock *sk = (struct sock *)msk;
758 unsigned int moved = 0;
759
Paolo Abeni87952602020-11-27 11:10:24 +0100760 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
761 __mptcp_ofo_queue(msk);
Paolo Abeni499ada52021-06-10 15:59:44 -0700762 if (unlikely(ssk->sk_err)) {
763 if (!sock_owned_by_user(sk))
764 __mptcp_error_report(sk);
765 else
766 set_bit(MPTCP_ERROR_REPORT, &msk->flags);
767 }
Paolo Abeniab174ad2020-09-14 10:01:12 +0200768
Paolo Abeni87952602020-11-27 11:10:24 +0100769 /* If the moves have caught up with the DATA_FIN sequence number
770 * it's time to ack the DATA_FIN and change socket state, but
771 * this is not a good place to change state. Let the workqueue
772 * do it.
773 */
774 if (mptcp_pending_data_fin(sk, NULL))
775 mptcp_schedule_work(sk);
Paolo Abeni99d10552021-06-10 15:59:41 -0700776 return moved > 0;
Florian Westphal2e522132020-02-26 10:14:51 +0100777}
778
779void mptcp_data_ready(struct sock *sk, struct sock *ssk)
Florian Westphal101f6f82020-02-26 10:14:46 +0100780{
Paolo Abeni67193312020-09-14 10:01:09 +0200781 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100782 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal13c7ba02020-11-03 11:05:03 -0800783 int sk_rbuf, ssk_rbuf;
Florian Westphal101f6f82020-02-26 10:14:46 +0100784
Paolo Abenid7b1bfd2020-12-09 12:03:31 +0100785 /* The peer can send data while we are shutting down this
786 * subflow at msk destruction time, but we must avoid enqueuing
787 * more data to the msk receive queue
788 */
789 if (unlikely(subflow->disposable))
790 return;
791
Florian Westphal13c7ba02020-11-03 11:05:03 -0800792 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
793 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
794 if (unlikely(ssk_rbuf > sk_rbuf))
795 sk_rbuf = ssk_rbuf;
796
Paolo Abeni99d10552021-06-10 15:59:41 -0700797 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
Paolo Abenice599c52021-07-09 17:20:51 -0700798 if (__mptcp_rmem(sk) > sk_rbuf) {
799 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
Paolo Abeni99d10552021-06-10 15:59:41 -0700800 return;
Paolo Abenice599c52021-07-09 17:20:51 -0700801 }
Florian Westphal2e522132020-02-26 10:14:51 +0100802
Paolo Abeni99d10552021-06-10 15:59:41 -0700803 /* Wake-up the reader only for in-sequence data */
804 mptcp_data_lock(sk);
Paolo Abeni612f71d2021-10-07 15:05:00 -0700805 if (move_skbs_to_msk(msk, ssk))
Paolo Abeni67193312020-09-14 10:01:09 +0200806 sk->sk_data_ready(sk);
Paolo Abeni612f71d2021-10-07 15:05:00 -0700807
Paolo Abeni99d10552021-06-10 15:59:41 -0700808 mptcp_data_unlock(sk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100809}
810
Florian Westphal78962482021-04-15 16:44:53 -0700811static bool mptcp_do_flush_join_list(struct mptcp_sock *msk)
Peter Krystadec3edaa2020-03-27 14:48:40 -0700812{
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100813 struct mptcp_subflow_context *subflow;
Florian Westphaldf00b082021-04-15 16:44:54 -0700814 bool ret = false;
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100815
Peter Krystadec3edaa2020-03-27 14:48:40 -0700816 if (likely(list_empty(&msk->join_list)))
Florian Westphal78962482021-04-15 16:44:53 -0700817 return false;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700818
819 spin_lock_bh(&msk->join_list_lock);
Florian Westphaldf00b082021-04-15 16:44:54 -0700820 list_for_each_entry(subflow, &msk->join_list, node) {
821 u32 sseq = READ_ONCE(subflow->setsockopt_seq);
Florian Westphal78962482021-04-15 16:44:53 -0700822
Florian Westphaldf00b082021-04-15 16:44:54 -0700823 mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow));
824 if (READ_ONCE(msk->setsockopt_seq) != sseq)
825 ret = true;
826 }
Peter Krystadec3edaa2020-03-27 14:48:40 -0700827 list_splice_tail_init(&msk->join_list, &msk->conn_list);
828 spin_unlock_bh(&msk->join_list_lock);
Florian Westphal78962482021-04-15 16:44:53 -0700829
Florian Westphaldf00b082021-04-15 16:44:54 -0700830 return ret;
Florian Westphal78962482021-04-15 16:44:53 -0700831}
832
833void __mptcp_flush_join_list(struct mptcp_sock *msk)
834{
835 if (likely(!mptcp_do_flush_join_list(msk)))
836 return;
837
838 if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags))
839 mptcp_schedule_work((struct sock *)msk);
840}
841
842static void mptcp_flush_join_list(struct mptcp_sock *msk)
843{
844 bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags);
845
846 might_sleep();
847
848 if (!mptcp_do_flush_join_list(msk) && !sync_needed)
849 return;
850
851 mptcp_sockopt_sync_all(msk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700852}
853
Paolo Abenib51f9b82020-03-27 14:48:44 -0700854static bool mptcp_timer_pending(struct sock *sk)
855{
856 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
857}
858
859static void mptcp_reset_timer(struct sock *sk)
860{
861 struct inet_connection_sock *icsk = inet_csk(sk);
862 unsigned long tout;
863
Paolo Abenie16163b2020-11-16 10:48:09 +0100864 /* prevent rescheduling on close */
865 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
866 return;
867
Paolo Abeni33d41c92021-08-13 15:15:41 -0700868 tout = mptcp_sk(sk)->timer_ival;
Paolo Abenib51f9b82020-03-27 14:48:44 -0700869 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
870}
871
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100872bool mptcp_schedule_work(struct sock *sk)
873{
874 if (inet_sk_state_load(sk) != TCP_CLOSE &&
875 schedule_work(&mptcp_sk(sk)->work)) {
876 /* each subflow already holds a reference to the sk, and the
877 * workqueue is invoked by a subflow, so sk can't go away here.
878 */
879 sock_hold(sk);
880 return true;
881 }
882 return false;
883}
884
Florian Westphal59832e22020-04-02 13:44:52 +0200885void mptcp_subflow_eof(struct sock *sk)
886{
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100887 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
888 mptcp_schedule_work(sk);
Florian Westphal59832e22020-04-02 13:44:52 +0200889}
890
Paolo Abeni59698562020-06-10 10:47:41 +0200891static void mptcp_check_for_eof(struct mptcp_sock *msk)
892{
893 struct mptcp_subflow_context *subflow;
894 struct sock *sk = (struct sock *)msk;
895 int receivers = 0;
896
897 mptcp_for_each_subflow(msk, subflow)
898 receivers += !subflow->rx_eof;
Paolo Abenie16163b2020-11-16 10:48:09 +0100899 if (receivers)
900 return;
Paolo Abeni59698562020-06-10 10:47:41 +0200901
Paolo Abenie16163b2020-11-16 10:48:09 +0100902 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
Paolo Abeni59698562020-06-10 10:47:41 +0200903 /* hopefully temporary hack: propagate shutdown status
904 * to msk, when all subflows agree on it
905 */
906 sk->sk_shutdown |= RCV_SHUTDOWN;
907
908 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Paolo Abeni59698562020-06-10 10:47:41 +0200909 sk->sk_data_ready(sk);
910 }
Paolo Abenie16163b2020-11-16 10:48:09 +0100911
912 switch (sk->sk_state) {
913 case TCP_ESTABLISHED:
914 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
915 break;
916 case TCP_FIN_WAIT1:
Paolo Abeni26aa2312020-11-19 11:45:55 -0800917 inet_sk_state_store(sk, TCP_CLOSING);
918 break;
919 case TCP_FIN_WAIT2:
Paolo Abenie16163b2020-11-16 10:48:09 +0100920 inet_sk_state_store(sk, TCP_CLOSE);
921 break;
922 default:
923 return;
924 }
925 mptcp_close_wake_up(sk);
Paolo Abeni59698562020-06-10 10:47:41 +0200926}
927
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800928static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
929{
930 struct mptcp_subflow_context *subflow;
931 struct sock *sk = (struct sock *)msk;
932
933 sock_owned_by_me(sk);
934
935 mptcp_for_each_subflow(msk, subflow) {
Paolo Abeni99d10552021-06-10 15:59:41 -0700936 if (READ_ONCE(subflow->data_avail))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800937 return mptcp_subflow_tcp_sock(subflow);
938 }
939
940 return NULL;
941}
942
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700943static bool mptcp_skb_can_collapse_to(u64 write_seq,
944 const struct sk_buff *skb,
945 const struct mptcp_ext *mpext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800946{
Paolo Abeni57040752020-01-21 16:56:27 -0800947 if (!tcp_skb_can_collapse_to(skb))
948 return false;
949
Paolo Abeni5a369ca2020-11-03 11:05:05 -0800950 /* can collapse only if MPTCP level sequence is in order and this
951 * mapping has not been xmitted yet
952 */
953 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
954 !mpext->frozen;
Paolo Abeni57040752020-01-21 16:56:27 -0800955}
956
Paolo Abeni29249ea2021-05-11 19:13:51 +0200957/* we can append data to the given data frag if:
958 * - there is space available in the backing page_frag
959 * - the data frag tail matches the current page_frag free offset
960 * - the data frag end sequence number matches the current write seq
961 */
Paolo Abeni18b683b2020-03-27 14:48:43 -0700962static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
963 const struct page_frag *pfrag,
964 const struct mptcp_data_frag *df)
965{
966 return df && pfrag->page == df->page &&
Paolo Abenid9ca1de2020-11-16 10:48:10 +0100967 pfrag->size - pfrag->offset > 0 &&
Paolo Abeni29249ea2021-05-11 19:13:51 +0200968 pfrag->offset == (df->offset + df->data_len) &&
Paolo Abeni18b683b2020-03-27 14:48:43 -0700969 df->data_seq + df->data_len == msk->write_seq;
970}
971
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700972static void __mptcp_mem_reclaim_partial(struct sock *sk)
973{
Paolo Abeni65118822021-10-26 16:29:15 -0700974 int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
975
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700976 lockdep_assert_held_once(&sk->sk_lock.slock);
Paolo Abeni65118822021-10-26 16:29:15 -0700977
978 __mptcp_rmem_reclaim(sk, reclaimable - 1);
Paolo Abeni1094c6f2021-09-01 10:15:36 -0700979 sk_mem_reclaim_partial(sk);
980}
981
Paolo Abeni724cfd22020-11-27 11:10:25 +0100982static void mptcp_mem_reclaim_partial(struct sock *sk)
983{
Paolo Abeni724cfd22020-11-27 11:10:25 +0100984 mptcp_data_lock(sk);
Paolo Abeni65118822021-10-26 16:29:15 -0700985 __mptcp_mem_reclaim_partial(sk);
Paolo Abeni724cfd22020-11-27 11:10:25 +0100986 mptcp_data_unlock(sk);
987}
988
Paolo Abenid0272362020-03-27 14:48:45 -0700989static void dfrag_uncharge(struct sock *sk, int len)
Paolo Abeni18b683b2020-03-27 14:48:43 -0700990{
Paolo Abenid0272362020-03-27 14:48:45 -0700991 sk_mem_uncharge(sk, len);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700992 sk_wmem_queued_add(sk, -len);
Paolo Abenid0272362020-03-27 14:48:45 -0700993}
994
995static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
996{
997 int len = dfrag->data_len + dfrag->overhead;
998
Paolo Abeni18b683b2020-03-27 14:48:43 -0700999 list_del(&dfrag->list);
Paolo Abenid0272362020-03-27 14:48:45 -07001000 dfrag_uncharge(sk, len);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001001 put_page(dfrag->page);
1002}
1003
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001004static void __mptcp_clean_una(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001005{
1006 struct mptcp_sock *msk = mptcp_sk(sk);
1007 struct mptcp_data_frag *dtmp, *dfrag;
Paolo Abenid0272362020-03-27 14:48:45 -07001008 bool cleaned = false;
Davide Carattie1ff9e82020-06-29 22:26:20 +02001009 u64 snd_una;
1010
1011 /* on fallback we just need to ignore snd_una, as this is really
1012 * plain TCP
1013 */
1014 if (__mptcp_check_fallback(msk))
Paolo Abeni7439d682020-11-27 11:10:26 +01001015 msk->snd_una = READ_ONCE(msk->snd_nxt);
1016
Paolo Abeni7439d682020-11-27 11:10:26 +01001017 snd_una = msk->snd_una;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001018 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1019 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1020 break;
1021
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001022 if (unlikely(dfrag == msk->first_pending)) {
1023 /* in recovery mode can see ack after the current snd head */
1024 if (WARN_ON_ONCE(!msk->recovery))
1025 break;
1026
1027 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1028 }
1029
Paolo Abenid0272362020-03-27 14:48:45 -07001030 dfrag_clear(sk, dfrag);
1031 cleaned = true;
1032 }
1033
Florian Westphal7948f6c2020-03-27 14:48:46 -07001034 dfrag = mptcp_rtx_head(sk);
1035 if (dfrag && after64(snd_una, dfrag->data_seq)) {
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001036 u64 delta = snd_una - dfrag->data_seq;
1037
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001038 /* prevent wrap around in recovery mode */
1039 if (unlikely(delta > dfrag->already_sent)) {
1040 if (WARN_ON_ONCE(!msk->recovery))
1041 goto out;
1042 if (WARN_ON_ONCE(delta > dfrag->data_len))
1043 goto out;
1044 dfrag->already_sent += delta - dfrag->already_sent;
1045 }
Florian Westphal7948f6c2020-03-27 14:48:46 -07001046
1047 dfrag->data_seq += delta;
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001048 dfrag->offset += delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001049 dfrag->data_len -= delta;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001050 dfrag->already_sent -= delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001051
1052 dfrag_uncharge(sk, delta);
1053 cleaned = true;
1054 }
1055
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07001056 /* all retransmitted data acked, recovery completed */
1057 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1058 msk->recovery = false;
1059
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001060out:
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001061 if (cleaned && tcp_under_memory_pressure(sk))
1062 __mptcp_mem_reclaim_partial(sk);
Paolo Abeni63561a42020-09-14 10:01:07 +02001063
Florian Westphal3241a9c2021-09-24 14:12:38 -07001064 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1065 snd_una == READ_ONCE(msk->write_seq)) {
Paolo Abeni33d41c92021-08-13 15:15:41 -07001066 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001067 mptcp_stop_timer(sk);
1068 } else {
1069 mptcp_reset_timer(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001070 }
1071}
1072
Paolo Abeni417789d2021-03-04 13:32:15 -08001073static void __mptcp_clean_una_wakeup(struct sock *sk)
1074{
Paolo Abeni765ff422021-09-24 14:12:36 -07001075 lockdep_assert_held_once(&sk->sk_lock.slock);
1076
Paolo Abeni417789d2021-03-04 13:32:15 -08001077 __mptcp_clean_una(sk);
1078 mptcp_write_space(sk);
1079}
1080
Paolo Abenib5941f02021-05-27 16:31:37 -07001081static void mptcp_clean_una_wakeup(struct sock *sk)
1082{
1083 mptcp_data_lock(sk);
1084 __mptcp_clean_una_wakeup(sk);
1085 mptcp_data_unlock(sk);
1086}
1087
Paolo Abeni724cfd22020-11-27 11:10:25 +01001088static void mptcp_enter_memory_pressure(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001089{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001090 struct mptcp_subflow_context *subflow;
1091 struct mptcp_sock *msk = mptcp_sk(sk);
1092 bool first = true;
1093
Paolo Abeni18b683b2020-03-27 14:48:43 -07001094 sk_stream_moderate_sndbuf(sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001095 mptcp_for_each_subflow(msk, subflow) {
1096 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1097
1098 if (first)
1099 tcp_enter_memory_pressure(ssk);
1100 sk_stream_moderate_sndbuf(ssk);
1101 first = false;
1102 }
Paolo Abeni724cfd22020-11-27 11:10:25 +01001103}
1104
1105/* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1106 * data
1107 */
1108static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1109{
1110 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1111 pfrag, sk->sk_allocation)))
1112 return true;
1113
1114 mptcp_enter_memory_pressure(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001115 return false;
1116}
1117
1118static struct mptcp_data_frag *
1119mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1120 int orig_offset)
1121{
1122 int offset = ALIGN(orig_offset, sizeof(long));
1123 struct mptcp_data_frag *dfrag;
1124
1125 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1126 dfrag->data_len = 0;
1127 dfrag->data_seq = msk->write_seq;
1128 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1129 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001130 dfrag->already_sent = 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001131 dfrag->page = pfrag->page;
1132
1133 return dfrag;
1134}
1135
Paolo Abenicaf971d2020-11-16 10:48:06 +01001136struct mptcp_sendmsg_info {
1137 int mss_now;
1138 int size_goal;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001139 u16 limit;
1140 u16 sent;
1141 unsigned int flags;
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001142 bool data_lock_held;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001143};
1144
Florian Westphal6f8a6122020-11-16 10:48:13 +01001145static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1146 int avail_size)
1147{
1148 u64 window_end = mptcp_wnd_end(msk);
1149
1150 if (__mptcp_check_fallback(msk))
1151 return avail_size;
1152
1153 if (!before64(data_seq + avail_size, window_end)) {
1154 u64 allowed_size = window_end - data_seq;
1155
1156 return min_t(unsigned int, allowed_size, avail_size);
1157 }
1158
1159 return avail_size;
1160}
1161
Paolo Abeni724cfd22020-11-27 11:10:25 +01001162static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1163{
1164 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1165
1166 if (!mpext)
1167 return false;
1168 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1169 return true;
1170}
1171
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001172static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001173{
1174 struct sk_buff *skb;
1175
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001176 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001177 if (likely(skb)) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001178 if (likely(__mptcp_add_ext(skb, gfp))) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001179 skb_reserve(skb, MAX_TCP_HEADER);
Eric Dumazeta52fe462021-10-27 13:19:21 -07001180 skb->ip_summed = CHECKSUM_PARTIAL;
Paolo Abenif70cad12021-09-22 19:26:41 +02001181 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001182 return skb;
1183 }
1184 __kfree_skb(skb);
1185 } else {
1186 mptcp_enter_memory_pressure(sk);
1187 }
1188 return NULL;
1189}
1190
Paolo Abenif70cad12021-09-22 19:26:41 +02001191static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001192{
Paolo Abeni724cfd22020-11-27 11:10:25 +01001193 struct sk_buff *skb;
1194
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001195 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001196 if (!skb)
Paolo Abenif70cad12021-09-22 19:26:41 +02001197 return NULL;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001198
1199 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
Paolo Abenif70cad12021-09-22 19:26:41 +02001200 tcp_skb_entail(ssk, skb);
1201 return skb;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001202 }
1203 kfree_skb(skb);
Paolo Abenif70cad12021-09-22 19:26:41 +02001204 return NULL;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001205}
1206
Paolo Abenif70cad12021-09-22 19:26:41 +02001207static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001208{
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001209 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001210
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001211 if (unlikely(tcp_under_memory_pressure(sk))) {
1212 if (data_lock_held)
1213 __mptcp_mem_reclaim_partial(sk);
1214 else
1215 mptcp_mem_reclaim_partial(sk);
1216 }
1217 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001218}
1219
Geliang Tangd0cc2982021-06-17 16:46:08 -07001220/* note: this always recompute the csum on the whole skb, even
1221 * if we just appended a single frag. More status info needed
1222 */
1223static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1224{
1225 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1226 __wsum csum = ~csum_unfold(mpext->csum);
1227 int offset = skb->len - added;
1228
1229 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1230}
1231
Paolo Abeni57040752020-01-21 16:56:27 -08001232static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001233 struct mptcp_data_frag *dfrag,
Paolo Abenicaf971d2020-11-16 10:48:06 +01001234 struct mptcp_sendmsg_info *info)
Paolo Abeni57040752020-01-21 16:56:27 -08001235{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001236 u64 data_seq = dfrag->data_seq + info->sent;
Paolo Abenif70cad12021-09-22 19:26:41 +02001237 int offset = dfrag->offset + info->sent;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001238 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001239 bool zero_window_probe = false;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001240 struct mptcp_ext *mpext = NULL;
Paolo Abenif70cad12021-09-22 19:26:41 +02001241 bool can_coalesce = false;
1242 bool reuse_skb = true;
1243 struct sk_buff *skb;
1244 size_t copy;
1245 int i;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001246
Geliang Tange4b61352021-04-16 15:38:02 -07001247 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001248 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001249
Paolo Abenif70cad12021-09-22 19:26:41 +02001250 if (WARN_ON_ONCE(info->sent > info->limit ||
1251 info->limit > dfrag->data_len))
1252 return 0;
1253
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001254 /* compute send limit */
1255 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
Paolo Abenif70cad12021-09-22 19:26:41 +02001256 copy = info->size_goal;
1257
Paolo Abeni57040752020-01-21 16:56:27 -08001258 skb = tcp_write_queue_tail(ssk);
Paolo Abenif70cad12021-09-22 19:26:41 +02001259 if (skb && copy > skb->len) {
Paolo Abeni57040752020-01-21 16:56:27 -08001260 /* Limit the write to the size available in the
1261 * current skb, if any, so that we create at most a new skb.
1262 * Explicitly tells TCP internals to avoid collapsing on later
1263 * queue management operation, to avoid breaking the ext <->
1264 * SSN association set here
1265 */
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001266 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001267 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
Paolo Abeni57040752020-01-21 16:56:27 -08001268 TCP_SKB_CB(skb)->eor = 1;
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001269 goto alloc_skb;
1270 }
1271
Paolo Abenif70cad12021-09-22 19:26:41 +02001272 i = skb_shinfo(skb)->nr_frags;
1273 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1274 if (!can_coalesce && i >= sysctl_max_skb_frags) {
1275 tcp_mark_push(tcp_sk(ssk), skb);
1276 goto alloc_skb;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001277 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001278
Paolo Abenif70cad12021-09-22 19:26:41 +02001279 copy -= skb->len;
1280 } else {
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001281alloc_skb:
Paolo Abenif70cad12021-09-22 19:26:41 +02001282 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1283 if (!skb)
1284 return -ENOMEM;
1285
1286 i = skb_shinfo(skb)->nr_frags;
1287 reuse_skb = false;
1288 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1289 }
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001290
Florian Westphal6f8a6122020-11-16 10:48:13 +01001291 /* Zero window and all data acked? Probe. */
Paolo Abenif70cad12021-09-22 19:26:41 +02001292 copy = mptcp_check_allowed_size(msk, data_seq, copy);
1293 if (copy == 0) {
Paolo Abeni7439d682020-11-27 11:10:26 +01001294 u64 snd_una = READ_ONCE(msk->snd_una);
1295
Paolo Abenif70cad12021-09-22 19:26:41 +02001296 if (snd_una != msk->snd_nxt) {
Eric Dumazet27728ba2021-10-27 13:19:18 -07001297 tcp_remove_empty_skb(ssk);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001298 return 0;
Paolo Abenif70cad12021-09-22 19:26:41 +02001299 }
1300
Florian Westphal6f8a6122020-11-16 10:48:13 +01001301 zero_window_probe = true;
Paolo Abeni7439d682020-11-27 11:10:26 +01001302 data_seq = snd_una - 1;
Paolo Abenif70cad12021-09-22 19:26:41 +02001303 copy = 1;
1304
1305 /* all mptcp-level data is acked, no skbs should be present into the
1306 * ssk write queue
1307 */
1308 WARN_ON_ONCE(reuse_skb);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001309 }
1310
Paolo Abenif70cad12021-09-22 19:26:41 +02001311 copy = min_t(size_t, copy, info->limit - info->sent);
1312 if (!sk_wmem_schedule(ssk, copy)) {
Eric Dumazet27728ba2021-10-27 13:19:18 -07001313 tcp_remove_empty_skb(ssk);
Paolo Abenie2223992020-11-16 10:48:03 +01001314 return -ENOMEM;
Florian Westphal357593832020-08-14 15:56:34 +02001315 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001316
Paolo Abenif70cad12021-09-22 19:26:41 +02001317 if (can_coalesce) {
1318 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1319 } else {
1320 get_page(dfrag->page);
1321 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
Paolo Abeni57040752020-01-21 16:56:27 -08001322 }
1323
Paolo Abenif70cad12021-09-22 19:26:41 +02001324 skb->len += copy;
1325 skb->data_len += copy;
1326 skb->truesize += copy;
1327 sk_wmem_queued_add(ssk, copy);
1328 sk_mem_charge(ssk, copy);
Paolo Abenif70cad12021-09-22 19:26:41 +02001329 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1330 TCP_SKB_CB(skb)->end_seq += copy;
1331 tcp_skb_pcount_set(skb, 0);
1332
1333 /* on skb reuse we just need to update the DSS len */
1334 if (reuse_skb) {
1335 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1336 mpext->data_len += copy;
1337 WARN_ON_ONCE(zero_window_probe);
1338 goto out;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001339 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001340
1341 memset(mpext, 0, sizeof(*mpext));
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001342 mpext->data_seq = data_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001343 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
Paolo Abenif70cad12021-09-22 19:26:41 +02001344 mpext->data_len = copy;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001345 mpext->use_map = 1;
1346 mpext->dsn64 = 1;
1347
1348 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1349 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1350 mpext->dsn64);
1351
Florian Westphal6f8a6122020-11-16 10:48:13 +01001352 if (zero_window_probe) {
Paolo Abenif70cad12021-09-22 19:26:41 +02001353 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
Florian Westphal6f8a6122020-11-16 10:48:13 +01001354 mpext->frozen = 1;
Geliang Tangd0cc2982021-06-17 16:46:08 -07001355 if (READ_ONCE(msk->csum_enabled))
Paolo Abenif70cad12021-09-22 19:26:41 +02001356 mptcp_update_data_checksum(skb, copy);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001357 tcp_push_pending_frames(ssk);
Geliang Tangd0cc2982021-06-17 16:46:08 -07001358 return 0;
Florian Westphal6f8a6122020-11-16 10:48:13 +01001359 }
Paolo Abeni57040752020-01-21 16:56:27 -08001360out:
Geliang Tangd0cc2982021-06-17 16:46:08 -07001361 if (READ_ONCE(msk->csum_enabled))
Paolo Abenif70cad12021-09-22 19:26:41 +02001362 mptcp_update_data_checksum(skb, copy);
1363 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1364 return copy;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001365}
1366
Paolo Abenid5f49192020-09-14 10:01:17 +02001367#define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1368 sizeof(struct tcphdr) - \
1369 MAX_TCP_OPTION_SPACE - \
1370 sizeof(struct ipv6hdr) - \
1371 sizeof(struct frag_hdr))
1372
1373struct subflow_send_info {
1374 struct sock *ssk;
1375 u64 ratio;
1376};
1377
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001378void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1379{
1380 if (!subflow->stale)
1381 return;
1382
1383 subflow->stale = 0;
Paolo Abenifc1b4e32021-08-13 15:15:46 -07001384 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001385}
1386
1387bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1388{
1389 if (unlikely(subflow->stale)) {
1390 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1391
1392 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1393 return false;
1394
1395 mptcp_subflow_set_active(subflow);
1396 }
1397 return __mptcp_subflow_active(subflow);
1398}
1399
Paolo Abeni33d41c92021-08-13 15:15:41 -07001400/* implement the mptcp packet scheduler;
1401 * returns the subflow that will transmit the next DSS
1402 * additionally updates the rtx timeout
1403 */
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001404static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
Peter Krystadf2962342020-03-27 14:48:39 -07001405{
Paolo Abenid5f49192020-09-14 10:01:17 +02001406 struct subflow_send_info send_info[2];
Peter Krystadf2962342020-03-27 14:48:39 -07001407 struct mptcp_subflow_context *subflow;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001408 struct sock *sk = (struct sock *)msk;
Paolo Abenid5f49192020-09-14 10:01:17 +02001409 int i, nr_active = 0;
1410 struct sock *ssk;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001411 long tout = 0;
Paolo Abenid5f49192020-09-14 10:01:17 +02001412 u64 ratio;
1413 u32 pace;
Peter Krystadf2962342020-03-27 14:48:39 -07001414
Paolo Abeni33d41c92021-08-13 15:15:41 -07001415 sock_owned_by_me(sk);
Peter Krystadf2962342020-03-27 14:48:39 -07001416
Paolo Abenid5f49192020-09-14 10:01:17 +02001417 if (__mptcp_check_fallback(msk)) {
1418 if (!msk->first)
Peter Krystadf2962342020-03-27 14:48:39 -07001419 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02001420 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001421 }
1422
Paolo Abenid5f49192020-09-14 10:01:17 +02001423 /* re-use last subflow, if the burst allow that */
1424 if (msk->last_snd && msk->snd_burst > 0 &&
1425 sk_stream_memory_free(msk->last_snd) &&
Paolo Abeni33d41c92021-08-13 15:15:41 -07001426 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1427 mptcp_set_timeout(sk);
Paolo Abenid5f49192020-09-14 10:01:17 +02001428 return msk->last_snd;
Paolo Abeni33d41c92021-08-13 15:15:41 -07001429 }
Paolo Abenid5f49192020-09-14 10:01:17 +02001430
1431 /* pick the subflow with the lower wmem/wspace ratio */
1432 for (i = 0; i < 2; ++i) {
1433 send_info[i].ssk = NULL;
1434 send_info[i].ratio = -1;
1435 }
1436 mptcp_for_each_subflow(msk, subflow) {
Geliang Tange10a9892021-04-16 15:38:04 -07001437 trace_mptcp_subflow_get_send(subflow);
Paolo Abenid5f49192020-09-14 10:01:17 +02001438 ssk = mptcp_subflow_tcp_sock(subflow);
1439 if (!mptcp_subflow_active(subflow))
1440 continue;
1441
Paolo Abeni33d41c92021-08-13 15:15:41 -07001442 tout = max(tout, mptcp_timeout_from_subflow(subflow));
Paolo Abenid5f49192020-09-14 10:01:17 +02001443 nr_active += !subflow->backup;
Paolo Abeniec369c32021-01-20 15:39:12 +01001444 if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd)
Paolo Abenid5f49192020-09-14 10:01:17 +02001445 continue;
1446
1447 pace = READ_ONCE(ssk->sk_pacing_rate);
1448 if (!pace)
1449 continue;
1450
1451 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1452 pace);
1453 if (ratio < send_info[subflow->backup].ratio) {
1454 send_info[subflow->backup].ssk = ssk;
1455 send_info[subflow->backup].ratio = ratio;
1456 }
1457 }
Paolo Abeni33d41c92021-08-13 15:15:41 -07001458 __mptcp_set_timeout(sk, tout);
Paolo Abenid5f49192020-09-14 10:01:17 +02001459
Paolo Abenid5f49192020-09-14 10:01:17 +02001460 /* pick the best backup if no other subflow is active */
1461 if (!nr_active)
1462 send_info[0].ssk = send_info[1].ssk;
1463
1464 if (send_info[0].ssk) {
1465 msk->last_snd = send_info[0].ssk;
1466 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
Paolo Abeniec369c32021-01-20 15:39:12 +01001467 tcp_sk(msk->last_snd)->snd_wnd);
Paolo Abenid5f49192020-09-14 10:01:17 +02001468 return msk->last_snd;
1469 }
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001470
Paolo Abenid5f49192020-09-14 10:01:17 +02001471 return NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001472}
1473
Geliang Tangb8e0def2021-10-26 16:29:16 -07001474static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001475{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001476 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1477 release_sock(ssk);
1478}
1479
Florian Westphal0d199e42021-09-24 14:12:34 -07001480static void mptcp_update_post_push(struct mptcp_sock *msk,
1481 struct mptcp_data_frag *dfrag,
1482 u32 sent)
1483{
1484 u64 snd_nxt_new = dfrag->data_seq;
1485
1486 dfrag->already_sent += sent;
1487
1488 msk->snd_burst -= sent;
Florian Westphal0d199e42021-09-24 14:12:34 -07001489
1490 snd_nxt_new += dfrag->already_sent;
1491
1492 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1493 * is recovering after a failover. In that event, this re-sends
1494 * old segments.
1495 *
1496 * Thus compute snd_nxt_new candidate based on
1497 * the dfrag->data_seq that was sent and the data
1498 * that has been handed to the subflow for transmission
1499 * and skip update in case it was old dfrag.
1500 */
1501 if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1502 msk->snd_nxt = snd_nxt_new;
1503}
1504
Florian Westphal3241a9c2021-09-24 14:12:38 -07001505static void mptcp_check_and_set_pending(struct sock *sk)
1506{
1507 if (mptcp_send_head(sk) &&
1508 !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
1509 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
1510}
1511
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001512void __mptcp_push_pending(struct sock *sk, unsigned int flags)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001513{
1514 struct sock *prev_ssk = NULL, *ssk = NULL;
1515 struct mptcp_sock *msk = mptcp_sk(sk);
1516 struct mptcp_sendmsg_info info = {
1517 .flags = flags,
1518 };
1519 struct mptcp_data_frag *dfrag;
1520 int len, copied = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001521
1522 while ((dfrag = mptcp_send_head(sk))) {
1523 info.sent = dfrag->already_sent;
1524 info.limit = dfrag->data_len;
1525 len = dfrag->data_len - dfrag->already_sent;
1526 while (len > 0) {
1527 int ret = 0;
1528
1529 prev_ssk = ssk;
Florian Westphal78962482021-04-15 16:44:53 -07001530 mptcp_flush_join_list(msk);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001531 ssk = mptcp_subflow_get_send(msk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001532
Paolo Abeni9758f402021-08-26 17:44:54 -07001533 /* First check. If the ssk has changed since
1534 * the last round, release prev_ssk
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001535 */
1536 if (ssk != prev_ssk && prev_ssk)
Geliang Tangb8e0def2021-10-26 16:29:16 -07001537 mptcp_push_release(prev_ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001538 if (!ssk)
1539 goto out;
1540
Paolo Abeni9758f402021-08-26 17:44:54 -07001541 /* Need to lock the new subflow only if different
1542 * from the previous one, otherwise we are still
1543 * helding the relevant lock
1544 */
1545 if (ssk != prev_ssk)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001546 lock_sock(ssk);
1547
1548 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1549 if (ret <= 0) {
Geliang Tangb8e0def2021-10-26 16:29:16 -07001550 mptcp_push_release(ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001551 goto out;
1552 }
1553
1554 info.sent += ret;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001555 copied += ret;
1556 len -= ret;
Florian Westphal0d199e42021-09-24 14:12:34 -07001557
1558 mptcp_update_post_push(msk, dfrag, ret);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001559 }
1560 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1561 }
1562
1563 /* at this point we held the socket lock for the last subflow we used */
1564 if (ssk)
Geliang Tangb8e0def2021-10-26 16:29:16 -07001565 mptcp_push_release(ssk, &info);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001566
1567out:
Paolo Abeni33d41c92021-08-13 15:15:41 -07001568 /* ensure the rtx timer is running */
1569 if (!mptcp_timer_pending(sk))
1570 mptcp_reset_timer(sk);
1571 if (copied)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001572 __mptcp_check_send_data_fin(sk);
1573}
1574
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001575static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1576{
1577 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abeni1094c6f2021-09-01 10:15:36 -07001578 struct mptcp_sendmsg_info info = {
1579 .data_lock_held = true,
1580 };
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001581 struct mptcp_data_frag *dfrag;
Paolo Abenib19bc292021-01-20 15:39:14 +01001582 struct sock *xmit_ssk;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001583 int len, copied = 0;
Paolo Abenib19bc292021-01-20 15:39:14 +01001584 bool first = true;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001585
1586 info.flags = 0;
1587 while ((dfrag = mptcp_send_head(sk))) {
1588 info.sent = dfrag->already_sent;
1589 info.limit = dfrag->data_len;
1590 len = dfrag->data_len - dfrag->already_sent;
1591 while (len > 0) {
1592 int ret = 0;
1593
Paolo Abenib19bc292021-01-20 15:39:14 +01001594 /* the caller already invoked the packet scheduler,
1595 * check for a different subflow usage only after
1596 * spooling the first chunk of data
1597 */
1598 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1599 if (!xmit_ssk)
1600 goto out;
1601 if (xmit_ssk != ssk) {
Paolo Abenibcd97732021-11-19 15:27:55 +01001602 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1603 MPTCP_DELEGATE_SEND);
Paolo Abenib19bc292021-01-20 15:39:14 +01001604 goto out;
1605 }
1606
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001607 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1608 if (ret <= 0)
1609 goto out;
1610
1611 info.sent += ret;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001612 copied += ret;
1613 len -= ret;
Paolo Abenib19bc292021-01-20 15:39:14 +01001614 first = false;
Florian Westphal0d199e42021-09-24 14:12:34 -07001615
1616 mptcp_update_post_push(msk, dfrag, ret);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001617 }
1618 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1619 }
1620
1621out:
1622 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1623 * not going to flush it via release_sock()
1624 */
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001625 if (copied) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001626 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1627 info.size_goal);
Paolo Abenid09d8182021-02-11 15:30:42 -08001628 if (!mptcp_timer_pending(sk))
1629 mptcp_reset_timer(sk);
1630
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001631 if (msk->snd_data_fin_enable &&
1632 msk->snd_nxt + 1 == msk->write_seq)
1633 mptcp_schedule_work(sk);
1634 }
1635}
1636
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001637static void mptcp_set_nospace(struct sock *sk)
1638{
1639 /* enable autotune */
1640 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1641
1642 /* will be cleared on avail space */
1643 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1644}
1645
Mat Martineauf870fa02020-01-21 16:56:15 -08001646static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1647{
1648 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal17091702020-05-16 10:46:21 +02001649 struct page_frag *pfrag;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001650 size_t copied = 0;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001651 int ret = 0;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001652 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08001653
Paolo Abeni987858e2021-04-23 11:17:07 -07001654 /* we don't support FASTOPEN yet */
1655 if (msg->msg_flags & MSG_FASTOPEN)
Mat Martineauf870fa02020-01-21 16:56:15 -08001656 return -EOPNOTSUPP;
1657
Paolo Abeni987858e2021-04-23 11:17:07 -07001658 /* silently ignore everything else */
1659 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1660
Paolo Abeni65118822021-10-26 16:29:15 -07001661 lock_sock(sk);
Mat Martineau1954b862020-02-28 15:47:39 -08001662
1663 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1664
1665 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1666 ret = sk_stream_wait_connect(sk, &timeo);
1667 if (ret)
1668 goto out;
1669 }
1670
Florian Westphal17091702020-05-16 10:46:21 +02001671 pfrag = sk_page_frag(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001672
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001673 while (msg_data_left(msg)) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001674 int total_ts, frag_truesize = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001675 struct mptcp_data_frag *dfrag;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001676 bool dfrag_collapsed;
1677 size_t psize, offset;
Mat Martineau57baaf22020-07-28 15:12:00 -07001678
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001679 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1680 ret = -EPIPE;
1681 goto out;
Florian Westphalfb529e62020-05-16 10:46:18 +02001682 }
1683
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001684 /* reuse tail pfrag, if possible, or carve a new one from the
1685 * page allocator
1686 */
1687 dfrag = mptcp_pending_tail(sk);
1688 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1689 if (!dfrag_collapsed) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001690 if (!sk_stream_memory_free(sk))
1691 goto wait_for_memory;
1692
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001693 if (!mptcp_page_frag_refill(sk, pfrag))
1694 goto wait_for_memory;
1695
1696 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1697 frag_truesize = dfrag->overhead;
1698 }
1699
1700 /* we do not bound vs wspace, to allow a single packet.
1701 * memory accounting will prevent execessive memory usage
1702 * anyway
1703 */
1704 offset = dfrag->offset + dfrag->data_len;
1705 psize = pfrag->size - offset;
1706 psize = min_t(size_t, psize, msg_data_left(msg));
Paolo Abeni724cfd22020-11-27 11:10:25 +01001707 total_ts = psize + frag_truesize;
Paolo Abeni8ce568e2021-06-21 15:54:33 -07001708
Paolo Abeni65118822021-10-26 16:29:15 -07001709 if (!sk_wmem_schedule(sk, total_ts))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001710 goto wait_for_memory;
1711
1712 if (copy_page_from_iter(dfrag->page, offset, psize,
1713 &msg->msg_iter) != psize) {
1714 ret = -EFAULT;
1715 goto out;
1716 }
1717
1718 /* data successfully copied into the write queue */
Paolo Abeni65118822021-10-26 16:29:15 -07001719 sk->sk_forward_alloc -= total_ts;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001720 copied += psize;
1721 dfrag->data_len += psize;
1722 frag_truesize += psize;
1723 pfrag->offset += frag_truesize;
1724 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1725
1726 /* charge data on mptcp pending queue to the msk socket
1727 * Note: we charge such data both to sk and ssk
1728 */
1729 sk_wmem_queued_add(sk, frag_truesize);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001730 if (!dfrag_collapsed) {
1731 get_page(dfrag->page);
1732 list_add_tail(&dfrag->list, &msk->rtx_queue);
1733 if (!msk->first_pending)
1734 WRITE_ONCE(msk->first_pending, dfrag);
1735 }
Geliang Tange4b61352021-04-16 15:38:02 -07001736 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001737 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1738 !dfrag_collapsed);
1739
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001740 continue;
1741
1742wait_for_memory:
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001743 mptcp_set_nospace(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08001744 __mptcp_push_pending(sk, msg->msg_flags);
Peter Krystadf2962342020-03-27 14:48:39 -07001745 ret = sk_stream_wait_memory(sk, &timeo);
1746 if (ret)
1747 goto out;
Peter Krystadcec37a62020-01-21 16:56:18 -08001748 }
1749
Paolo Abeni13e16032020-12-16 12:48:35 +01001750 if (copied)
Paolo Abenic2e60482021-03-04 13:32:14 -08001751 __mptcp_push_pending(sk, msg->msg_flags);
Paolo Abenida51aef2020-09-14 10:01:10 +02001752
Mat Martineau1954b862020-02-28 15:47:39 -08001753out:
Peter Krystadcec37a62020-01-21 16:56:18 -08001754 release_sock(sk);
Paolo Abeni8555c6b2020-08-03 18:40:39 +02001755 return copied ? : ret;
Mat Martineauf870fa02020-01-21 16:56:15 -08001756}
1757
Florian Westphal6771bfd2020-02-26 10:14:48 +01001758static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1759 struct msghdr *msg,
Florian Westphalb7f653b2021-06-03 16:24:32 -07001760 size_t len, int flags,
1761 struct scm_timestamping_internal *tss,
1762 int *cmsg_flags)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001763{
Yonglong Lica4fb8922021-04-23 11:17:08 -07001764 struct sk_buff *skb, *tmp;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001765 int copied = 0;
1766
Yonglong Lica4fb8922021-04-23 11:17:08 -07001767 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
Florian Westphal6771bfd2020-02-26 10:14:48 +01001768 u32 offset = MPTCP_SKB_CB(skb)->offset;
1769 u32 data_len = skb->len - offset;
1770 u32 count = min_t(size_t, len - copied, data_len);
1771 int err;
1772
Paolo Abenid9760922021-04-23 11:17:06 -07001773 if (!(flags & MSG_TRUNC)) {
1774 err = skb_copy_datagram_msg(skb, offset, msg, count);
1775 if (unlikely(err < 0)) {
1776 if (!copied)
1777 return err;
1778 break;
1779 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001780 }
1781
Florian Westphalb7f653b2021-06-03 16:24:32 -07001782 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1783 tcp_update_recv_tstamps(skb, tss);
1784 *cmsg_flags |= MPTCP_CMSG_TS;
1785 }
1786
Florian Westphal6771bfd2020-02-26 10:14:48 +01001787 copied += count;
1788
1789 if (count < data_len) {
Florian Westphal2c9e7762021-12-03 14:35:32 -08001790 if (!(flags & MSG_PEEK)) {
Yonglong Lica4fb8922021-04-23 11:17:08 -07001791 MPTCP_SKB_CB(skb)->offset += count;
Florian Westphal2c9e7762021-12-03 14:35:32 -08001792 MPTCP_SKB_CB(skb)->map_seq += count;
1793 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001794 break;
1795 }
1796
Yonglong Lica4fb8922021-04-23 11:17:08 -07001797 if (!(flags & MSG_PEEK)) {
1798 /* we will bulk release the skb memory later */
1799 skb->destructor = NULL;
Paolo Abenice599c52021-07-09 17:20:51 -07001800 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
Yonglong Lica4fb8922021-04-23 11:17:08 -07001801 __skb_unlink(skb, &msk->receive_queue);
1802 __kfree_skb(skb);
1803 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001804
1805 if (copied >= len)
1806 break;
1807 }
1808
1809 return copied;
1810}
1811
Florian Westphala6b118f2020-06-30 21:24:45 +02001812/* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1813 *
1814 * Only difference: Use highest rtt estimate of the subflows in use.
1815 */
1816static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1817{
1818 struct mptcp_subflow_context *subflow;
1819 struct sock *sk = (struct sock *)msk;
1820 u32 time, advmss = 1;
1821 u64 rtt_us, mstamp;
1822
1823 sock_owned_by_me(sk);
1824
1825 if (copied <= 0)
1826 return;
1827
1828 msk->rcvq_space.copied += copied;
1829
1830 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1831 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1832
1833 rtt_us = msk->rcvq_space.rtt_us;
1834 if (rtt_us && time < (rtt_us >> 3))
1835 return;
1836
1837 rtt_us = 0;
1838 mptcp_for_each_subflow(msk, subflow) {
1839 const struct tcp_sock *tp;
1840 u64 sf_rtt_us;
1841 u32 sf_advmss;
1842
1843 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1844
1845 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1846 sf_advmss = READ_ONCE(tp->advmss);
1847
1848 rtt_us = max(sf_rtt_us, rtt_us);
1849 advmss = max(sf_advmss, advmss);
1850 }
1851
1852 msk->rcvq_space.rtt_us = rtt_us;
1853 if (time < (rtt_us >> 3) || rtt_us == 0)
1854 return;
1855
1856 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1857 goto new_measure;
1858
1859 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1860 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1861 int rcvmem, rcvbuf;
1862 u64 rcvwin, grow;
1863
1864 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1865
1866 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1867
1868 do_div(grow, msk->rcvq_space.space);
1869 rcvwin += (grow << 1);
1870
1871 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1872 while (tcp_win_from_space(sk, rcvmem) < advmss)
1873 rcvmem += 128;
1874
1875 do_div(rcvwin, advmss);
1876 rcvbuf = min_t(u64, rcvwin * rcvmem,
1877 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1878
1879 if (rcvbuf > sk->sk_rcvbuf) {
1880 u32 window_clamp;
1881
1882 window_clamp = tcp_win_from_space(sk, rcvbuf);
1883 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1884
1885 /* Make subflows follow along. If we do not do this, we
1886 * get drops at subflow level if skbs can't be moved to
1887 * the mptcp rx queue fast enough (announced rcv_win can
1888 * exceed ssk->sk_rcvbuf).
1889 */
1890 mptcp_for_each_subflow(msk, subflow) {
1891 struct sock *ssk;
Paolo Abenic76c6952020-09-14 10:01:18 +02001892 bool slow;
Florian Westphala6b118f2020-06-30 21:24:45 +02001893
1894 ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenic76c6952020-09-14 10:01:18 +02001895 slow = lock_sock_fast(ssk);
Florian Westphala6b118f2020-06-30 21:24:45 +02001896 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1897 tcp_sk(ssk)->window_clamp = window_clamp;
Paolo Abenic76c6952020-09-14 10:01:18 +02001898 tcp_cleanup_rbuf(ssk, 1);
1899 unlock_sock_fast(ssk, slow);
Florian Westphala6b118f2020-06-30 21:24:45 +02001900 }
1901 }
1902 }
1903
1904 msk->rcvq_space.space = msk->rcvq_space.copied;
1905new_measure:
1906 msk->rcvq_space.copied = 0;
1907 msk->rcvq_space.time = mstamp;
1908}
1909
Paolo Abeni87952602020-11-27 11:10:24 +01001910static void __mptcp_update_rmem(struct sock *sk)
1911{
1912 struct mptcp_sock *msk = mptcp_sk(sk);
1913
1914 if (!msk->rmem_released)
1915 return;
1916
1917 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
Paolo Abeni65118822021-10-26 16:29:15 -07001918 mptcp_rmem_uncharge(sk, msk->rmem_released);
Paolo Abenice599c52021-07-09 17:20:51 -07001919 WRITE_ONCE(msk->rmem_released, 0);
Paolo Abeni87952602020-11-27 11:10:24 +01001920}
1921
1922static void __mptcp_splice_receive_queue(struct sock *sk)
1923{
1924 struct mptcp_sock *msk = mptcp_sk(sk);
1925
1926 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1927}
1928
Paolo Abenie3859602021-02-11 15:30:41 -08001929static bool __mptcp_move_skbs(struct mptcp_sock *msk)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001930{
Paolo Abeni87952602020-11-27 11:10:24 +01001931 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001932 unsigned int moved = 0;
Paolo Abeni87952602020-11-27 11:10:24 +01001933 bool ret, done;
Paolo Abenid5f49192020-09-14 10:01:17 +02001934
Florian Westphal78962482021-04-15 16:44:53 -07001935 mptcp_flush_join_list(msk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001936 do {
1937 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001938 bool slowpath;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001939
Paolo Abeni87952602020-11-27 11:10:24 +01001940 /* we can have data pending in the subflows only if the msk
1941 * receive buffer was full at subflow_data_ready() time,
1942 * that is an unlikely slow path.
1943 */
1944 if (likely(!ssk))
Florian Westphal6771bfd2020-02-26 10:14:48 +01001945 break;
1946
Florian Westphal65f49fe72020-11-03 11:05:04 -08001947 slowpath = lock_sock_fast(ssk);
Paolo Abeni87952602020-11-27 11:10:24 +01001948 mptcp_data_lock(sk);
Paolo Abenie3859602021-02-11 15:30:41 -08001949 __mptcp_update_rmem(sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001950 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
Paolo Abeni87952602020-11-27 11:10:24 +01001951 mptcp_data_unlock(sk);
Paolo Abeni499ada52021-06-10 15:59:44 -07001952
1953 if (unlikely(ssk->sk_err))
1954 __mptcp_error_report(sk);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001955 unlock_sock_fast(ssk, slowpath);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001956 } while (!done);
1957
Paolo Abeni87952602020-11-27 11:10:24 +01001958 /* acquire the data lock only if some input data is pending */
1959 ret = moved > 0;
1960 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1961 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
1962 mptcp_data_lock(sk);
1963 __mptcp_update_rmem(sk);
1964 ret |= __mptcp_ofo_queue(msk);
1965 __mptcp_splice_receive_queue(sk);
1966 mptcp_data_unlock(sk);
Paolo Abeniab174ad2020-09-14 10:01:12 +02001967 }
Paolo Abeni87952602020-11-27 11:10:24 +01001968 if (ret)
1969 mptcp_check_data_fin((struct sock *)msk);
1970 return !skb_queue_empty(&msk->receive_queue);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001971}
1972
Florian Westphal2c9e7762021-12-03 14:35:32 -08001973static unsigned int mptcp_inq_hint(const struct sock *sk)
1974{
1975 const struct mptcp_sock *msk = mptcp_sk(sk);
1976 const struct sk_buff *skb;
1977
1978 skb = skb_peek(&msk->receive_queue);
1979 if (skb) {
1980 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
1981
1982 if (hint_val >= INT_MAX)
1983 return INT_MAX;
1984
1985 return (unsigned int)hint_val;
1986 }
1987
1988 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
1989 return 1;
1990
1991 return 0;
1992}
1993
Mat Martineauf870fa02020-01-21 16:56:15 -08001994static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1995 int nonblock, int flags, int *addr_len)
1996{
1997 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb7f653b2021-06-03 16:24:32 -07001998 struct scm_timestamping_internal tss;
1999 int copied = 0, cmsg_flags = 0;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002000 int target;
2001 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08002002
Paolo Abenicb9d80f2021-04-23 11:17:05 -07002003 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2004 if (unlikely(flags & MSG_ERRQUEUE))
2005 return inet_recv_error(sk, msg, len, addr_len);
2006
Paolo Abeni65118822021-10-26 16:29:15 -07002007 lock_sock(sk);
Paolo Abenifd897672020-11-24 22:51:24 +01002008 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2009 copied = -ENOTCONN;
2010 goto out_err;
2011 }
2012
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002013 timeo = sock_rcvtimeo(sk, nonblock);
2014
2015 len = min_t(size_t, len, INT_MAX);
2016 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2017
Florian Westphal2c9e7762021-12-03 14:35:32 -08002018 if (unlikely(msk->recvmsg_inq))
2019 cmsg_flags = MPTCP_CMSG_INQ;
2020
Eric Dumazet05e3ece2020-12-02 09:16:57 -08002021 while (copied < len) {
Paolo Abenie3859602021-02-11 15:30:41 -08002022 int bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002023
Florian Westphalb7f653b2021-06-03 16:24:32 -07002024 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +01002025 if (unlikely(bytes_read < 0)) {
2026 if (!copied)
2027 copied = bytes_read;
2028 goto out_err;
2029 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002030
Florian Westphal6771bfd2020-02-26 10:14:48 +01002031 copied += bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002032
Paolo Abeniea4ca582020-11-19 11:46:03 -08002033 /* be sure to advertise window change */
Paolo Abenie3859602021-02-11 15:30:41 -08002034 mptcp_cleanup_rbuf(msk);
2035
2036 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2037 continue;
Paolo Abeniea4ca582020-11-19 11:46:03 -08002038
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002039 /* only the master socket status is relevant here. The exit
2040 * conditions mirror closely tcp_recvmsg()
2041 */
2042 if (copied >= target)
2043 break;
2044
2045 if (copied) {
2046 if (sk->sk_err ||
2047 sk->sk_state == TCP_CLOSE ||
2048 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2049 !timeo ||
2050 signal_pending(current))
2051 break;
2052 } else {
2053 if (sk->sk_err) {
2054 copied = sock_error(sk);
2055 break;
2056 }
2057
Paolo Abeni59698562020-06-10 10:47:41 +02002058 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2059 mptcp_check_for_eof(msk);
2060
Paolo Abeni87952602020-11-27 11:10:24 +01002061 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2062 /* race breaker: the shutdown could be after the
2063 * previous receive queue check
2064 */
Paolo Abenie3859602021-02-11 15:30:41 -08002065 if (__mptcp_move_skbs(msk))
Paolo Abeni87952602020-11-27 11:10:24 +01002066 continue;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002067 break;
Paolo Abeni87952602020-11-27 11:10:24 +01002068 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002069
2070 if (sk->sk_state == TCP_CLOSE) {
2071 copied = -ENOTCONN;
2072 break;
2073 }
2074
2075 if (!timeo) {
2076 copied = -EAGAIN;
2077 break;
2078 }
2079
2080 if (signal_pending(current)) {
2081 copied = sock_intr_errno(timeo);
2082 break;
2083 }
2084 }
2085
2086 pr_debug("block timeout %ld", timeo);
Paolo Abeni612f71d2021-10-07 15:05:00 -07002087 sk_wait_data(sk, &timeo, NULL);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002088 }
Paolo Abeni3c90e372021-06-21 15:54:35 -07002089
Florian Westphal6771bfd2020-02-26 10:14:48 +01002090out_err:
Florian Westphalb7f653b2021-06-03 16:24:32 -07002091 if (cmsg_flags && copied >= 0) {
2092 if (cmsg_flags & MPTCP_CMSG_TS)
2093 tcp_recv_timestamp(msg, sk, &tss);
Florian Westphal2c9e7762021-12-03 14:35:32 -08002094
2095 if (cmsg_flags & MPTCP_CMSG_INQ) {
2096 unsigned int inq = mptcp_inq_hint(sk);
2097
2098 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2099 }
Florian Westphalb7f653b2021-06-03 16:24:32 -07002100 }
2101
Paolo Abeni612f71d2021-10-07 15:05:00 -07002102 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2103 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2104 skb_queue_empty(&msk->receive_queue), copied);
Yonglong Lica4fb8922021-04-23 11:17:08 -07002105 if (!(flags & MSG_PEEK))
2106 mptcp_rcv_space_adjust(msk, copied);
Florian Westphala6b118f2020-06-30 21:24:45 +02002107
Peter Krystadcec37a62020-01-21 16:56:18 -08002108 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08002109 return copied;
2110}
2111
Paolo Abenib51f9b82020-03-27 14:48:44 -07002112static void mptcp_retransmit_timer(struct timer_list *t)
2113{
2114 struct inet_connection_sock *icsk = from_timer(icsk, t,
2115 icsk_retransmit_timer);
2116 struct sock *sk = &icsk->icsk_inet.sk;
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002117 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002118
2119 bh_lock_sock(sk);
2120 if (!sock_owned_by_user(sk)) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002121 /* we need a process context to retransmit */
2122 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2123 mptcp_schedule_work(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002124 } else {
2125 /* delegate our work to tcp_release_cb() */
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002126 set_bit(MPTCP_RETRANSMIT, &msk->flags);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002127 }
2128 bh_unlock_sock(sk);
2129 sock_put(sk);
2130}
2131
Paolo Abenie16163b2020-11-16 10:48:09 +01002132static void mptcp_timeout_timer(struct timer_list *t)
2133{
2134 struct sock *sk = from_timer(sk, t, sk_timer);
2135
2136 mptcp_schedule_work(sk);
Florian Westphalb6d69fc2020-11-24 17:24:46 +01002137 sock_put(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002138}
2139
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002140/* Find an idle subflow. Return NULL if there is unacked data at tcp
2141 * level.
2142 *
2143 * A backup subflow is returned only if that is the only kind available.
2144 */
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002145static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002146{
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002147 struct sock *backup = NULL, *pick = NULL;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002148 struct mptcp_subflow_context *subflow;
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002149 int min_stale_count = INT_MAX;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002150
2151 sock_owned_by_me((const struct sock *)msk);
2152
Paolo Abenid5f49192020-09-14 10:01:17 +02002153 if (__mptcp_check_fallback(msk))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002154 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02002155
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002156 mptcp_for_each_subflow(msk, subflow) {
2157 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2158
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002159 if (!__mptcp_subflow_active(subflow))
Paolo Abenid5f49192020-09-14 10:01:17 +02002160 continue;
2161
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002162 /* still data outstanding at TCP level? skip this */
2163 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2164 mptcp_pm_subflow_chk_stale(msk, ssk);
2165 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2166 continue;
Florian Westphal860975c2020-11-19 11:45:56 -08002167 }
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002168
2169 if (subflow->backup) {
2170 if (!backup)
2171 backup = ssk;
2172 continue;
2173 }
2174
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002175 if (!pick)
2176 pick = ssk;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002177 }
2178
Paolo Abeni71b7dec2021-08-13 15:15:42 -07002179 if (pick)
2180 return pick;
2181
2182 /* use backup only if there are no progresses anywhere */
2183 return min_stale_count > 1 ? backup : NULL;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002184}
2185
Florian Westphal17aee052021-03-04 13:32:11 -08002186static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2187{
2188 if (msk->subflow) {
2189 iput(SOCK_INODE(msk->subflow));
2190 msk->subflow = NULL;
2191 }
2192}
2193
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002194bool __mptcp_retransmit_pending_data(struct sock *sk)
2195{
2196 struct mptcp_data_frag *cur, *rtx_head;
2197 struct mptcp_sock *msk = mptcp_sk(sk);
2198
2199 if (__mptcp_check_fallback(mptcp_sk(sk)))
2200 return false;
2201
2202 if (tcp_rtx_and_write_queues_empty(sk))
2203 return false;
2204
2205 /* the closing socket has some data untransmitted and/or unacked:
2206 * some data in the mptcp rtx queue has not really xmitted yet.
2207 * keep it simple and re-inject the whole mptcp level rtx queue
2208 */
2209 mptcp_data_lock(sk);
2210 __mptcp_clean_una_wakeup(sk);
2211 rtx_head = mptcp_rtx_head(sk);
2212 if (!rtx_head) {
2213 mptcp_data_unlock(sk);
2214 return false;
2215 }
2216
Florian Westphal0d199e42021-09-24 14:12:34 -07002217 msk->recovery_snd_nxt = msk->snd_nxt;
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002218 msk->recovery = true;
2219 mptcp_data_unlock(sk);
2220
2221 msk->first_pending = rtx_head;
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002222 msk->snd_burst = 0;
2223
2224 /* be sure to clear the "sent status" on all re-injected fragments */
2225 list_for_each_entry(cur, &msk->rtx_queue, list) {
2226 if (!cur->already_sent)
2227 break;
2228 cur->already_sent = 0;
2229 }
2230
2231 return true;
2232}
2233
Peter Krystadcec37a62020-01-21 16:56:18 -08002234/* subflow sockets can be either outgoing (connect) or incoming
2235 * (accept).
2236 *
2237 * Outgoing subflows use in-kernel sockets.
2238 * Incoming subflows do not have their own 'struct socket' allocated,
2239 * so we need to use tcp_close() after detaching them from the mptcp
2240 * parent socket.
2241 */
Florian Westphala141e022021-02-12 15:59:55 -08002242static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2243 struct mptcp_subflow_context *subflow)
Peter Krystadcec37a62020-01-21 16:56:18 -08002244{
Florian Westphale0be4932021-03-04 13:32:08 -08002245 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002246 bool need_push;
Florian Westphale0be4932021-03-04 13:32:08 -08002247
Peter Krystadcec37a62020-01-21 16:56:18 -08002248 list_del(&subflow->node);
2249
Paolo Abeni3f8b2662020-12-16 12:48:33 +01002250 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
Paolo Abenie16163b2020-11-16 10:48:09 +01002251
2252 /* if we are invoked by the msk cleanup code, the subflow is
2253 * already orphaned
2254 */
Paolo Abeni866f26f2021-01-20 15:39:10 +01002255 if (ssk->sk_socket)
Paolo Abenie16163b2020-11-16 10:48:09 +01002256 sock_orphan(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002257
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002258 need_push = __mptcp_retransmit_pending_data(sk);
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002259 subflow->disposable = 1;
2260
Paolo Abenie16163b2020-11-16 10:48:09 +01002261 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2262 * the ssk has been already destroyed, we just need to release the
2263 * reference owned by msk;
2264 */
2265 if (!inet_csk(ssk)->icsk_ulp_ops) {
2266 kfree_rcu(subflow, rcu);
2267 } else {
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002268 /* otherwise tcp will dispose of the ssk and subflow ctx */
Paolo Abenie16163b2020-11-16 10:48:09 +01002269 __tcp_close(ssk, 0);
2270
2271 /* close acquired an extra ref */
2272 __sock_put(ssk);
2273 }
2274 release_sock(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002275
2276 sock_put(ssk);
Florian Westphale0be4932021-03-04 13:32:08 -08002277
2278 if (ssk == msk->last_snd)
2279 msk->last_snd = NULL;
Florian Westphal17aee052021-03-04 13:32:11 -08002280
Florian Westphalc8fe62f2021-03-04 13:32:12 -08002281 if (ssk == msk->first)
2282 msk->first = NULL;
2283
Florian Westphal17aee052021-03-04 13:32:11 -08002284 if (msk->subflow && ssk == msk->subflow->sk)
2285 mptcp_dispose_initial_subflow(msk);
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002286
2287 if (need_push)
2288 __mptcp_push_pending(sk, 0);
Mat Martineauf870fa02020-01-21 16:56:15 -08002289}
2290
Florian Westphala141e022021-02-12 15:59:55 -08002291void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2292 struct mptcp_subflow_context *subflow)
2293{
Florian Westphalb911c972021-02-12 16:00:01 -08002294 if (sk->sk_state == TCP_ESTABLISHED)
2295 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
Florian Westphala141e022021-02-12 15:59:55 -08002296 __mptcp_close_ssk(sk, ssk, subflow);
2297}
2298
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002299static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2300{
2301 return 0;
2302}
2303
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002304static void __mptcp_close_subflow(struct mptcp_sock *msk)
2305{
2306 struct mptcp_subflow_context *subflow, *tmp;
2307
Florian Westphal3abc05d2021-02-04 15:23:30 -08002308 might_sleep();
2309
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002310 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2311 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2312
2313 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2314 continue;
2315
Florian Westphal40947e12021-02-12 15:59:56 -08002316 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2317 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2318 continue;
2319
Florian Westphala141e022021-02-12 15:59:55 -08002320 mptcp_close_ssk((struct sock *)msk, ssk, subflow);
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002321 }
2322}
2323
Paolo Abenie16163b2020-11-16 10:48:09 +01002324static bool mptcp_check_close_timeout(const struct sock *sk)
2325{
2326 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2327 struct mptcp_subflow_context *subflow;
2328
2329 if (delta >= TCP_TIMEWAIT_LEN)
2330 return true;
2331
2332 /* if all subflows are in closed status don't bother with additional
2333 * timeout
2334 */
2335 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2336 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2337 TCP_CLOSE)
2338 return false;
2339 }
2340 return true;
2341}
2342
Florian Westphal50c504a2020-12-10 14:25:04 -08002343static void mptcp_check_fastclose(struct mptcp_sock *msk)
2344{
2345 struct mptcp_subflow_context *subflow, *tmp;
2346 struct sock *sk = &msk->sk.icsk_inet.sk;
2347
2348 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2349 return;
2350
2351 mptcp_token_destroy(msk);
2352
2353 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2354 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni75e908c2021-06-21 15:54:34 -07002355 bool slow;
Florian Westphal50c504a2020-12-10 14:25:04 -08002356
Paolo Abeni75e908c2021-06-21 15:54:34 -07002357 slow = lock_sock_fast(tcp_sk);
Florian Westphal50c504a2020-12-10 14:25:04 -08002358 if (tcp_sk->sk_state != TCP_CLOSE) {
2359 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2360 tcp_set_state(tcp_sk, TCP_CLOSE);
2361 }
Paolo Abeni75e908c2021-06-21 15:54:34 -07002362 unlock_sock_fast(tcp_sk, slow);
Florian Westphal50c504a2020-12-10 14:25:04 -08002363 }
2364
2365 inet_sk_state_store(sk, TCP_CLOSE);
2366 sk->sk_shutdown = SHUTDOWN_MASK;
2367 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
Florian Westphal50c504a2020-12-10 14:25:04 -08002368 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2369
2370 mptcp_close_wake_up(sk);
2371}
2372
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002373static void __mptcp_retrans(struct sock *sk)
Paolo Abeni80992012020-02-26 10:14:47 +01002374{
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002375 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenicaf971d2020-11-16 10:48:06 +01002376 struct mptcp_sendmsg_info info = {};
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002377 struct mptcp_data_frag *dfrag;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002378 size_t copied = 0;
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002379 struct sock *ssk;
2380 int ret;
2381
Paolo Abenib5941f02021-05-27 16:31:37 -07002382 mptcp_clean_una_wakeup(sk);
Florian Westphal3241a9c2021-09-24 14:12:38 -07002383
2384 /* first check ssk: need to kick "stale" logic */
2385 ssk = mptcp_subflow_get_retrans(msk);
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002386 dfrag = mptcp_rtx_head(sk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002387 if (!dfrag) {
2388 if (mptcp_data_fin_enabled(msk)) {
2389 struct inet_connection_sock *icsk = inet_csk(sk);
2390
2391 icsk->icsk_retransmits++;
2392 mptcp_set_datafin_timeout(sk);
2393 mptcp_send_ack(msk);
2394
2395 goto reset_timer;
2396 }
2397
Florian Westphal3241a9c2021-09-24 14:12:38 -07002398 if (!mptcp_send_head(sk))
2399 return;
2400
2401 goto reset_timer;
Mat Martineau6477dd32021-04-23 09:40:33 -07002402 }
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002403
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002404 if (!ssk)
2405 goto reset_timer;
2406
2407 lock_sock(ssk);
2408
2409 /* limit retransmission to the bytes already sent on some subflows */
2410 info.sent = 0;
Paolo Abeni4e148672021-06-17 16:46:17 -07002411 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2412 while (info.sent < info.limit) {
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002413 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2414 if (ret <= 0)
2415 break;
2416
2417 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2418 copied += ret;
2419 info.sent += ret;
2420 }
Paolo Abeni4e148672021-06-17 16:46:17 -07002421 if (copied) {
2422 dfrag->already_sent = max(dfrag->already_sent, info.sent);
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002423 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2424 info.size_goal);
Paolo Abeni4e148672021-06-17 16:46:17 -07002425 }
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002426
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002427 release_sock(ssk);
2428
2429reset_timer:
Florian Westphal3241a9c2021-09-24 14:12:38 -07002430 mptcp_check_and_set_pending(sk);
2431
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002432 if (!mptcp_timer_pending(sk))
2433 mptcp_reset_timer(sk);
2434}
2435
2436static void mptcp_worker(struct work_struct *work)
2437{
2438 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2439 struct sock *sk = &msk->sk.icsk_inet.sk;
2440 int state;
Paolo Abeni80992012020-02-26 10:14:47 +01002441
2442 lock_sock(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002443 state = sk->sk_state;
2444 if (unlikely(state == TCP_CLOSE))
2445 goto unlock;
2446
Mat Martineau43b54c62020-07-28 15:12:06 -07002447 mptcp_check_data_fin_ack(sk);
Florian Westphal78962482021-04-15 16:44:53 -07002448 mptcp_flush_join_list(msk);
Florian Westphal50c504a2020-12-10 14:25:04 -08002449
2450 mptcp_check_fastclose(msk);
2451
Florian Westphalb4162682020-07-07 14:40:48 +02002452 if (msk->pm.status)
Florian Westphale9801432021-02-12 15:59:54 -08002453 mptcp_pm_nl_work(msk);
Florian Westphalb4162682020-07-07 14:40:48 +02002454
Florian Westphal59832e22020-04-02 13:44:52 +02002455 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2456 mptcp_check_for_eof(msk);
2457
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002458 __mptcp_check_send_data_fin(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002459 mptcp_check_data_fin(sk);
2460
Paolo Abeni341c6522021-02-19 18:35:37 +01002461 /* There is no point in keeping around an orphaned sk timedout or
2462 * closed, but we need the msk around to reply to incoming DATA_FIN,
2463 * even if it is orphaned and in FIN_WAIT2 state
Paolo Abenie16163b2020-11-16 10:48:09 +01002464 */
2465 if (sock_flag(sk, SOCK_DEAD) &&
Paolo Abeni341c6522021-02-19 18:35:37 +01002466 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002467 inet_sk_state_store(sk, TCP_CLOSE);
2468 __mptcp_destroy_sock(sk);
2469 goto unlock;
2470 }
2471
Florian Westphalb263b0d2021-02-12 15:59:57 -08002472 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2473 __mptcp_close_subflow(msk);
2474
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002475 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2476 __mptcp_retrans(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002477
2478unlock:
Paolo Abeni80992012020-02-26 10:14:47 +01002479 release_sock(sk);
2480 sock_put(sk);
2481}
2482
Matthieu Baerts784325e2020-01-21 16:56:28 -08002483static int __mptcp_init_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002484{
Peter Krystadcec37a62020-01-21 16:56:18 -08002485 struct mptcp_sock *msk = mptcp_sk(sk);
2486
Peter Krystadec3edaa2020-03-27 14:48:40 -07002487 spin_lock_init(&msk->join_list_lock);
2488
Peter Krystadcec37a62020-01-21 16:56:18 -08002489 INIT_LIST_HEAD(&msk->conn_list);
Peter Krystadec3edaa2020-03-27 14:48:40 -07002490 INIT_LIST_HEAD(&msk->join_list);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002491 INIT_LIST_HEAD(&msk->rtx_queue);
Paolo Abeni80992012020-02-26 10:14:47 +01002492 INIT_WORK(&msk->work, mptcp_worker);
Paolo Abeni87952602020-11-27 11:10:24 +01002493 __skb_queue_head_init(&msk->receive_queue);
Paolo Abeniab174ad2020-09-14 10:01:12 +02002494 msk->out_of_order_queue = RB_ROOT;
Paolo Abenif0e6a4c2020-11-16 10:48:07 +01002495 msk->first_pending = NULL;
Paolo Abeni65118822021-10-26 16:29:15 -07002496 msk->rmem_fwd_alloc = 0;
Paolo Abenice599c52021-07-09 17:20:51 -07002497 WRITE_ONCE(msk->rmem_released, 0);
Paolo Abeni33d41c92021-08-13 15:15:41 -07002498 msk->timer_ival = TCP_RTO_MIN;
Peter Krystadcec37a62020-01-21 16:56:18 -08002499
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002500 msk->first = NULL;
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002501 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
Geliang Tang752e9062021-06-17 16:46:07 -07002502 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
Paolo Abeni1e1d9d62021-08-13 15:15:43 -07002503 msk->recovery = false;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002504
Peter Krystad1b1c7a02020-03-27 14:48:38 -07002505 mptcp_pm_data_init(msk);
2506
Paolo Abenib51f9b82020-03-27 14:48:44 -07002507 /* re-use the csk retrans timer for MPTCP-level retrans */
2508 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
Paolo Abenie16163b2020-11-16 10:48:09 +01002509 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002510
Mat Martineauf870fa02020-01-21 16:56:15 -08002511 return 0;
2512}
2513
Matthieu Baerts784325e2020-01-21 16:56:28 -08002514static int mptcp_init_sock(struct sock *sk)
2515{
Paolo Abeni20b57592021-05-25 14:23:10 -07002516 struct inet_connection_sock *icsk = inet_csk(sk);
Florian Westphalfc518952020-03-27 14:48:50 -07002517 struct net *net = sock_net(sk);
2518 int ret;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002519
Geliang Tangb6c08382020-09-24 08:29:54 +08002520 ret = __mptcp_init_sock(sk);
2521 if (ret)
2522 return ret;
2523
Florian Westphalfc518952020-03-27 14:48:50 -07002524 if (!mptcp_is_enabled(net))
2525 return -ENOPROTOOPT;
2526
2527 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2528 return -ENOMEM;
2529
Paolo Abenifa680182020-06-29 22:26:23 +02002530 ret = __mptcp_socket_create(mptcp_sk(sk));
2531 if (ret)
2532 return ret;
2533
Paolo Abeni20b57592021-05-25 14:23:10 -07002534 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2535 * propagate the correct value
2536 */
2537 tcp_assign_congestion_control(sk);
2538 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2539
2540 /* no need to keep a reference to the ops, the name will suffice */
2541 tcp_cleanup_congestion_control(sk);
2542 icsk->icsk_ca_ops = NULL;
2543
Paolo Abenid0272362020-03-27 14:48:45 -07002544 sk_sockets_allocated_inc(sk);
Florian Westphala6b118f2020-06-30 21:24:45 +02002545 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
Paolo Abenida51aef2020-09-14 10:01:10 +02002546 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
Paolo Abenid0272362020-03-27 14:48:45 -07002547
Paolo Abeni18b683b2020-03-27 14:48:43 -07002548 return 0;
2549}
2550
2551static void __mptcp_clear_xmit(struct sock *sk)
2552{
2553 struct mptcp_sock *msk = mptcp_sk(sk);
2554 struct mptcp_data_frag *dtmp, *dfrag;
2555
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002556 WRITE_ONCE(msk->first_pending, NULL);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002557 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
Paolo Abenid0272362020-03-27 14:48:45 -07002558 dfrag_clear(sk, dfrag);
Matthieu Baerts784325e2020-01-21 16:56:28 -08002559}
2560
Paolo Abeni80992012020-02-26 10:14:47 +01002561static void mptcp_cancel_work(struct sock *sk)
2562{
2563 struct mptcp_sock *msk = mptcp_sk(sk);
2564
Paolo Abenib2771d22020-11-19 11:45:54 -08002565 if (cancel_work_sync(&msk->work))
Paolo Abenie16163b2020-11-16 10:48:09 +01002566 __sock_put(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002567}
2568
Geliang Tangd0876b22020-09-24 08:29:49 +08002569void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
Peter Krystad21498492020-01-21 16:56:21 -08002570{
2571 lock_sock(ssk);
2572
2573 switch (ssk->sk_state) {
2574 case TCP_LISTEN:
2575 if (!(how & RCV_SHUTDOWN))
2576 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002577 fallthrough;
Peter Krystad21498492020-01-21 16:56:21 -08002578 case TCP_SYN_SENT:
2579 tcp_disconnect(ssk, O_NONBLOCK);
2580 break;
2581 default:
Mat Martineau43b54c62020-07-28 15:12:06 -07002582 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2583 pr_debug("Fallback");
2584 ssk->sk_shutdown |= how;
2585 tcp_shutdown(ssk, how);
2586 } else {
2587 pr_debug("Sending DATA_FIN on subflow %p", ssk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002588 tcp_send_ack(ssk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002589 if (!mptcp_timer_pending(sk))
2590 mptcp_reset_timer(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002591 }
Peter Krystad21498492020-01-21 16:56:21 -08002592 break;
2593 }
2594
Peter Krystad21498492020-01-21 16:56:21 -08002595 release_sock(ssk);
2596}
2597
Mat Martineau6920b852020-07-28 15:12:04 -07002598static const unsigned char new_state[16] = {
2599 /* current state: new state: action: */
2600 [0 /* (Invalid) */] = TCP_CLOSE,
2601 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2602 [TCP_SYN_SENT] = TCP_CLOSE,
2603 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2604 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2605 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2606 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2607 [TCP_CLOSE] = TCP_CLOSE,
2608 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2609 [TCP_LAST_ACK] = TCP_LAST_ACK,
2610 [TCP_LISTEN] = TCP_CLOSE,
2611 [TCP_CLOSING] = TCP_CLOSING,
2612 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2613};
2614
2615static int mptcp_close_state(struct sock *sk)
2616{
2617 int next = (int)new_state[sk->sk_state];
2618 int ns = next & TCP_STATE_MASK;
2619
2620 inet_sk_state_store(sk, ns);
2621
2622 return next & TCP_ACTION_FIN;
2623}
2624
Paolo Abenie16163b2020-11-16 10:48:09 +01002625static void __mptcp_check_send_data_fin(struct sock *sk)
2626{
2627 struct mptcp_subflow_context *subflow;
2628 struct mptcp_sock *msk = mptcp_sk(sk);
2629
2630 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2631 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2632 msk->snd_nxt, msk->write_seq);
2633
2634 /* we still need to enqueue subflows or not really shutting down,
2635 * skip this
2636 */
2637 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2638 mptcp_send_head(sk))
2639 return;
2640
2641 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2642
Paolo Abeni26aa2312020-11-19 11:45:55 -08002643 /* fallback socket will not get data_fin/ack, can move to the next
2644 * state now
2645 */
2646 if (__mptcp_check_fallback(msk)) {
2647 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2648 inet_sk_state_store(sk, TCP_CLOSE);
2649 mptcp_close_wake_up(sk);
2650 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2651 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2652 }
Paolo Abenie16163b2020-11-16 10:48:09 +01002653 }
2654
Florian Westphal78962482021-04-15 16:44:53 -07002655 mptcp_flush_join_list(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002656 mptcp_for_each_subflow(msk, subflow) {
2657 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2658
2659 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2660 }
2661}
2662
2663static void __mptcp_wr_shutdown(struct sock *sk)
2664{
2665 struct mptcp_sock *msk = mptcp_sk(sk);
2666
2667 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2668 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2669 !!mptcp_send_head(sk));
2670
2671 /* will be ignored by fallback sockets */
2672 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2673 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2674
2675 __mptcp_check_send_data_fin(sk);
2676}
2677
2678static void __mptcp_destroy_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002679{
Peter Krystadcec37a62020-01-21 16:56:18 -08002680 struct mptcp_subflow_context *subflow, *tmp;
Mat Martineauf870fa02020-01-21 16:56:15 -08002681 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002682 LIST_HEAD(conn_list);
Mat Martineauf870fa02020-01-21 16:56:15 -08002683
Paolo Abenie16163b2020-11-16 10:48:09 +01002684 pr_debug("msk=%p", msk);
Florian Westphal2c22c062020-02-04 18:12:30 +01002685
Florian Westphal3abc05d2021-02-04 15:23:30 -08002686 might_sleep();
2687
Paolo Abeni10f6d462020-05-29 17:43:30 +02002688 /* be sure to always acquire the join list lock, to sync vs
2689 * mptcp_finish_join().
2690 */
2691 spin_lock_bh(&msk->join_list_lock);
2692 list_splice_tail_init(&msk->join_list, &msk->conn_list);
2693 spin_unlock_bh(&msk->join_list_lock);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002694 list_splice_init(&msk->conn_list, &conn_list);
2695
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002696 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
Paolo Abenie16163b2020-11-16 10:48:09 +01002697 sk_stop_timer(sk, &sk->sk_timer);
2698 msk->pm.status = 0;
Florian Westphalb2c5b612020-01-29 15:54:45 +01002699
2700 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
Peter Krystadcec37a62020-01-21 16:56:18 -08002701 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002702 __mptcp_close_ssk(sk, ssk, subflow);
Mat Martineauf870fa02020-01-21 16:56:15 -08002703 }
2704
Paolo Abenie16163b2020-11-16 10:48:09 +01002705 sk->sk_prot->destroy(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002706
Paolo Abeni65118822021-10-26 16:29:15 -07002707 WARN_ON_ONCE(msk->rmem_fwd_alloc);
Paolo Abeni87952602020-11-27 11:10:24 +01002708 WARN_ON_ONCE(msk->rmem_released);
Paolo Abenie16163b2020-11-16 10:48:09 +01002709 sk_stream_kill_queues(sk);
2710 xfrm_sk_free_policy(sk);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002711
Paolo Abenie16163b2020-11-16 10:48:09 +01002712 sk_refcnt_debug_release(sk);
Florian Westphal17aee052021-03-04 13:32:11 -08002713 mptcp_dispose_initial_subflow(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002714 sock_put(sk);
2715}
Florian Westphal6771bfd2020-02-26 10:14:48 +01002716
Paolo Abenie16163b2020-11-16 10:48:09 +01002717static void mptcp_close(struct sock *sk, long timeout)
2718{
2719 struct mptcp_subflow_context *subflow;
2720 bool do_cancel_work = false;
2721
2722 lock_sock(sk);
2723 sk->sk_shutdown = SHUTDOWN_MASK;
2724
2725 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2726 inet_sk_state_store(sk, TCP_CLOSE);
2727 goto cleanup;
2728 }
2729
2730 if (mptcp_close_state(sk))
2731 __mptcp_wr_shutdown(sk);
2732
2733 sk_stream_wait_close(sk, timeout);
2734
2735cleanup:
2736 /* orphan all the subflows */
2737 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
Geliang Tang44227912021-04-16 15:38:08 -07002738 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002739 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni49054552021-09-29 11:59:17 +02002740 bool slow = lock_sock_fast_nested(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002741
Paolo Abenie16163b2020-11-16 10:48:09 +01002742 sock_orphan(ssk);
2743 unlock_sock_fast(ssk, slow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002744 }
2745 sock_orphan(sk);
2746
2747 sock_hold(sk);
2748 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2749 if (sk->sk_state == TCP_CLOSE) {
2750 __mptcp_destroy_sock(sk);
2751 do_cancel_work = true;
2752 } else {
2753 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2754 }
2755 release_sock(sk);
2756 if (do_cancel_work)
2757 mptcp_cancel_work(sk);
Florian Westphalb911c972021-02-12 16:00:01 -08002758
2759 if (mptcp_sk(sk)->token)
2760 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2761
Paolo Abenie16163b2020-11-16 10:48:09 +01002762 sock_put(sk);
Mat Martineauf870fa02020-01-21 16:56:15 -08002763}
2764
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002765static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2766{
2767#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2768 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2769 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2770
2771 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2772 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2773
2774 if (msk6 && ssk6) {
2775 msk6->saddr = ssk6->saddr;
2776 msk6->flow_label = ssk6->flow_label;
2777 }
2778#endif
2779
2780 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2781 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2782 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2783 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2784 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2785 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2786}
2787
Paolo Abeni18b683b2020-03-27 14:48:43 -07002788static int mptcp_disconnect(struct sock *sk, int flags)
2789{
Paolo Abeni76e2a552021-01-12 18:25:24 +01002790 struct mptcp_subflow_context *subflow;
2791 struct mptcp_sock *msk = mptcp_sk(sk);
2792
Florian Westphal78962482021-04-15 16:44:53 -07002793 mptcp_do_flush_join_list(msk);
2794
Paolo Abeni13a94992021-01-14 16:37:37 +01002795 mptcp_for_each_subflow(msk, subflow) {
2796 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2797
2798 lock_sock(ssk);
2799 tcp_disconnect(ssk, flags);
2800 release_sock(ssk);
2801 }
Florian Westphal42c556f2020-04-29 20:43:20 +02002802 return 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002803}
2804
Florian Westphalb0519de2020-02-06 00:39:37 +01002805#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2806static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2807{
2808 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2809
2810 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2811}
2812#endif
2813
Paolo Abenifca5c822020-04-20 16:25:06 +02002814struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +02002815 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +02002816 struct request_sock *req)
Florian Westphalb0519de2020-02-06 00:39:37 +01002817{
Paolo Abeni58b09912020-03-13 16:52:41 +01002818 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
Florian Westphalb0519de2020-02-06 00:39:37 +01002819 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
Paolo Abeni58b09912020-03-13 16:52:41 +01002820 struct mptcp_sock *msk;
2821 u64 ack_seq;
Florian Westphalb0519de2020-02-06 00:39:37 +01002822
2823 if (!nsk)
2824 return NULL;
2825
2826#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2827 if (nsk->sk_family == AF_INET6)
2828 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2829#endif
2830
Paolo Abeni58b09912020-03-13 16:52:41 +01002831 __mptcp_init_sock(nsk);
2832
2833 msk = mptcp_sk(nsk);
2834 msk->local_key = subflow_req->local_key;
2835 msk->token = subflow_req->token;
2836 msk->subflow = NULL;
Paolo Abenib93df082020-07-23 13:02:32 +02002837 WRITE_ONCE(msk->fully_established, false);
Paolo Abeni74c7dfb2021-08-26 17:44:52 -07002838 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
Geliang Tang06251182021-06-17 16:46:13 -07002839 WRITE_ONCE(msk->csum_enabled, true);
Paolo Abeni58b09912020-03-13 16:52:41 +01002840
Paolo Abeni58b09912020-03-13 16:52:41 +01002841 msk->write_seq = subflow_req->idsn + 1;
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01002842 msk->snd_nxt = msk->write_seq;
Paolo Abeni7439d682020-11-27 11:10:26 +01002843 msk->snd_una = msk->write_seq;
2844 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
Florian Westphaldf00b082021-04-15 16:44:54 -07002845 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002846
Paolo Abeni74c7dfb2021-08-26 17:44:52 -07002847 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
Paolo Abeni58b09912020-03-13 16:52:41 +01002848 msk->can_ack = true;
Paolo Abenicfde1412020-04-30 15:01:52 +02002849 msk->remote_key = mp_opt->sndr_key;
Paolo Abeni58b09912020-03-13 16:52:41 +01002850 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2851 ack_seq++;
Mat Martineau917944d2020-09-29 15:08:19 -07002852 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08002853 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Paolo Abeni58b09912020-03-13 16:52:41 +01002854 }
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002855
Florian Westphal5e200872020-04-20 16:25:04 +02002856 sock_reset_flag(nsk, SOCK_RCU_FREE);
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002857 /* will be fully established after successful MPC subflow creation */
2858 inet_sk_state_store(nsk, TCP_SYN_RECV);
Paolo Abeni0c148462020-12-16 12:48:32 +01002859
2860 security_inet_csk_clone(nsk, req);
Paolo Abeni58b09912020-03-13 16:52:41 +01002861 bh_unlock_sock(nsk);
2862
2863 /* keep a single reference */
2864 __sock_put(nsk);
Florian Westphalb0519de2020-02-06 00:39:37 +01002865 return nsk;
2866}
2867
Florian Westphala6b118f2020-06-30 21:24:45 +02002868void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2869{
2870 const struct tcp_sock *tp = tcp_sk(ssk);
2871
2872 msk->rcvq_space.copied = 0;
2873 msk->rcvq_space.rtt_us = 0;
2874
2875 msk->rcvq_space.time = tp->tcp_mstamp;
2876
2877 /* initial rcv_space offering made to peer */
2878 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2879 TCP_INIT_CWND * tp->advmss);
2880 if (msk->rcvq_space.space == 0)
2881 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002882
Paolo Abeni7439d682020-11-27 11:10:26 +01002883 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
Florian Westphala6b118f2020-06-30 21:24:45 +02002884}
2885
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002886static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2887 bool kern)
2888{
2889 struct mptcp_sock *msk = mptcp_sk(sk);
2890 struct socket *listener;
2891 struct sock *newsk;
2892
2893 listener = __mptcp_nmpc_socket(msk);
2894 if (WARN_ON_ONCE(!listener)) {
2895 *err = -EINVAL;
2896 return NULL;
2897 }
2898
2899 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2900 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2901 if (!newsk)
2902 return NULL;
2903
2904 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002905 if (sk_is_mptcp(newsk)) {
2906 struct mptcp_subflow_context *subflow;
2907 struct sock *new_mptcp_sock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002908
2909 subflow = mptcp_subflow_ctx(newsk);
Paolo Abeni58b09912020-03-13 16:52:41 +01002910 new_mptcp_sock = subflow->conn;
2911
2912 /* is_mptcp should be false if subflow->conn is missing, see
2913 * subflow_syn_recv_sock()
2914 */
2915 if (WARN_ON_ONCE(!new_mptcp_sock)) {
2916 tcp_sk(newsk)->is_mptcp = 0;
2917 return newsk;
2918 }
2919
2920 /* acquire the 2nd reference for the owning socket */
2921 sock_hold(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002922 newsk = new_mptcp_sock;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08002923 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
Florian Westphalfc518952020-03-27 14:48:50 -07002924 } else {
2925 MPTCP_INC_STATS(sock_net(sk),
2926 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002927 }
2928
2929 return newsk;
2930}
2931
Geliang Tang5c8c1642020-09-24 08:29:57 +08002932void mptcp_destroy_common(struct mptcp_sock *msk)
2933{
Paolo Abeni87952602020-11-27 11:10:24 +01002934 struct sock *sk = (struct sock *)msk;
2935
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002936 __mptcp_clear_xmit(sk);
2937
Paolo Abeni87952602020-11-27 11:10:24 +01002938 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
2939 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
Paolo Abeni65118822021-10-26 16:29:15 -07002940 __skb_queue_purge(&sk->sk_receive_queue);
Geliang Tang5c8c1642020-09-24 08:29:57 +08002941 skb_rbtree_purge(&msk->out_of_order_queue);
Paolo Abeni65118822021-10-26 16:29:15 -07002942
2943 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
2944 * inet_sock_destruct() will dispose it
2945 */
2946 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
2947 msk->rmem_fwd_alloc = 0;
Geliang Tang5c8c1642020-09-24 08:29:57 +08002948 mptcp_token_destroy(msk);
2949 mptcp_pm_free_anno_list(msk);
2950}
2951
Peter Krystad79c09492020-01-21 16:56:20 -08002952static void mptcp_destroy(struct sock *sk)
2953{
Florian Westphalc9fd9c52020-01-29 15:54:43 +01002954 struct mptcp_sock *msk = mptcp_sk(sk);
2955
Geliang Tang5c8c1642020-09-24 08:29:57 +08002956 mptcp_destroy_common(msk);
Paolo Abenid0272362020-03-27 14:48:45 -07002957 sk_sockets_allocated_dec(sk);
Peter Krystad79c09492020-01-21 16:56:20 -08002958}
2959
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002960void __mptcp_data_acked(struct sock *sk)
2961{
2962 if (!sock_owned_by_user(sk))
2963 __mptcp_clean_una(sk);
2964 else
2965 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
2966
2967 if (mptcp_pending_data_fin_ack(sk))
2968 mptcp_schedule_work(sk);
2969}
2970
Paolo Abeni219d0492020-12-16 12:48:34 +01002971void __mptcp_check_push(struct sock *sk, struct sock *ssk)
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002972{
2973 if (!mptcp_send_head(sk))
2974 return;
2975
Paolo Abeni40dc9412021-01-20 15:39:13 +01002976 if (!sock_owned_by_user(sk)) {
Paolo Abenib19bc292021-01-20 15:39:14 +01002977 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
2978
2979 if (xmit_ssk == ssk)
Paolo Abeni40dc9412021-01-20 15:39:13 +01002980 __mptcp_subflow_push_pending(sk, ssk);
Paolo Abenib19bc292021-01-20 15:39:14 +01002981 else if (xmit_ssk)
Paolo Abenibcd97732021-11-19 15:27:55 +01002982 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND);
Paolo Abeni40dc9412021-01-20 15:39:13 +01002983 } else {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002984 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
Paolo Abeni40dc9412021-01-20 15:39:13 +01002985 }
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002986}
2987
Paolo Abenie93da922020-11-27 11:10:23 +01002988/* processes deferred events and flush wmem */
Paolo Abeni14c441b2020-02-26 10:14:52 +01002989static void mptcp_release_cb(struct sock *sk)
2990{
Paolo Abenic2e60482021-03-04 13:32:14 -08002991 for (;;) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002992 unsigned long flags = 0;
2993
Paolo Abenic2e60482021-03-04 13:32:14 -08002994 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03002995 flags |= BIT(MPTCP_PUSH_PENDING);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002996 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
2997 flags |= BIT(MPTCP_RETRANSMIT);
Paolo Abenic2e60482021-03-04 13:32:14 -08002998 if (!flags)
2999 break;
3000
3001 /* the following actions acquire the subflow socket lock
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003002 *
3003 * 1) can't be invoked in atomic scope
3004 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3005 * datapath acquires the msk socket spinlock while helding
3006 * the subflow socket lock
3007 */
3008
3009 spin_unlock_bh(&sk->sk_lock.slock);
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03003010 if (flags & BIT(MPTCP_PUSH_PENDING))
Paolo Abenic2e60482021-03-04 13:32:14 -08003011 __mptcp_push_pending(sk, 0);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07003012 if (flags & BIT(MPTCP_RETRANSMIT))
3013 __mptcp_retrans(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08003014
3015 cond_resched();
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003016 spin_lock_bh(&sk->sk_lock.slock);
3017 }
Paolo Abenic2e60482021-03-04 13:32:14 -08003018
Paolo Abeni490274b2021-06-21 17:33:08 -07003019 /* be sure to set the current sk state before tacking actions
3020 * depending on sk_state
3021 */
3022 if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags))
3023 __mptcp_set_connected(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08003024 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
Paolo Abeni417789d2021-03-04 13:32:15 -08003025 __mptcp_clean_una_wakeup(sk);
Paolo Abeni15cc1042021-02-11 15:30:37 -08003026 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
3027 __mptcp_error_report(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003028
Paolo Abeni87952602020-11-27 11:10:24 +01003029 __mptcp_update_rmem(sk);
Paolo Abeni14c441b2020-02-26 10:14:52 +01003030}
3031
Paolo Abenibcd97732021-11-19 15:27:55 +01003032/* MP_JOIN client subflow must wait for 4th ack before sending any data:
3033 * TCP can't schedule delack timer before the subflow is fully established.
3034 * MPTCP uses the delack timer to do 3rd ack retransmissions
3035 */
3036static void schedule_3rdack_retransmission(struct sock *ssk)
3037{
3038 struct inet_connection_sock *icsk = inet_csk(ssk);
3039 struct tcp_sock *tp = tcp_sk(ssk);
3040 unsigned long timeout;
3041
3042 if (mptcp_subflow_ctx(ssk)->fully_established)
3043 return;
3044
3045 /* reschedule with a timeout above RTT, as we must look only for drop */
3046 if (tp->srtt_us)
3047 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3048 else
3049 timeout = TCP_TIMEOUT_INIT;
3050 timeout += jiffies;
3051
3052 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3053 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3054 icsk->icsk_ack.timeout = timeout;
3055 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3056}
3057
Paolo Abenib19bc292021-01-20 15:39:14 +01003058void mptcp_subflow_process_delegated(struct sock *ssk)
3059{
3060 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3061 struct sock *sk = subflow->conn;
3062
Paolo Abenibcd97732021-11-19 15:27:55 +01003063 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3064 mptcp_data_lock(sk);
3065 if (!sock_owned_by_user(sk))
3066 __mptcp_subflow_push_pending(sk, ssk);
3067 else
3068 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
3069 mptcp_data_unlock(sk);
3070 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3071 }
3072 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3073 schedule_3rdack_retransmission(ssk);
3074 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3075 }
Paolo Abenib19bc292021-01-20 15:39:14 +01003076}
3077
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003078static int mptcp_hash(struct sock *sk)
3079{
3080 /* should never be called,
3081 * we hash the TCP subflows not the master socket
3082 */
3083 WARN_ON_ONCE(1);
3084 return 0;
3085}
3086
3087static void mptcp_unhash(struct sock *sk)
3088{
3089 /* called from sk_common_release(), but nothing to do here */
3090}
3091
Peter Krystadcec37a62020-01-21 16:56:18 -08003092static int mptcp_get_port(struct sock *sk, unsigned short snum)
Mat Martineauf870fa02020-01-21 16:56:15 -08003093{
3094 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08003095 struct socket *ssock;
Mat Martineauf870fa02020-01-21 16:56:15 -08003096
Peter Krystadcec37a62020-01-21 16:56:18 -08003097 ssock = __mptcp_nmpc_socket(msk);
3098 pr_debug("msk=%p, subflow=%p", msk, ssock);
3099 if (WARN_ON_ONCE(!ssock))
3100 return -EINVAL;
Mat Martineauf870fa02020-01-21 16:56:15 -08003101
Peter Krystadcec37a62020-01-21 16:56:18 -08003102 return inet_csk_get_port(ssock->sk, snum);
3103}
Mat Martineauf870fa02020-01-21 16:56:15 -08003104
Peter Krystadcec37a62020-01-21 16:56:18 -08003105void mptcp_finish_connect(struct sock *ssk)
3106{
3107 struct mptcp_subflow_context *subflow;
3108 struct mptcp_sock *msk;
3109 struct sock *sk;
Mat Martineau6d0060f2020-01-21 16:56:23 -08003110 u64 ack_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -08003111
Peter Krystadcec37a62020-01-21 16:56:18 -08003112 subflow = mptcp_subflow_ctx(ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -08003113 sk = subflow->conn;
3114 msk = mptcp_sk(sk);
3115
Mat Martineau648ef4b2020-01-21 16:56:24 -08003116 pr_debug("msk=%p, token=%u", sk, subflow->token);
3117
Mat Martineau6d0060f2020-01-21 16:56:23 -08003118 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3119 ack_seq++;
Mat Martineau648ef4b2020-01-21 16:56:24 -08003120 subflow->map_seq = ack_seq;
3121 subflow->map_subflow_seq = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -08003122
Peter Krystadcec37a62020-01-21 16:56:18 -08003123 /* the socket is not connected yet, no msk/subflow ops can access/race
3124 * accessing the field below
3125 */
3126 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3127 WRITE_ONCE(msk->local_key, subflow->local_key);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003128 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01003129 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003130 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08003131 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Christoph Paaschd22f4982020-01-21 16:56:32 -08003132 WRITE_ONCE(msk->can_ack, 1);
Paolo Abeni7439d682020-11-27 11:10:26 +01003133 WRITE_ONCE(msk->snd_una, msk->write_seq);
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003134
Florian Westphal6c714f12021-02-12 15:59:58 -08003135 mptcp_pm_new_connection(msk, ssk, 0);
Florian Westphala6b118f2020-06-30 21:24:45 +02003136
3137 mptcp_rcv_space_init(msk, ssk);
Mat Martineauf870fa02020-01-21 16:56:15 -08003138}
3139
Paolo Abeni866f26f2021-01-20 15:39:10 +01003140void mptcp_sock_graft(struct sock *sk, struct socket *parent)
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003141{
3142 write_lock_bh(&sk->sk_callback_lock);
3143 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3144 sk_set_socket(sk, parent);
3145 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3146 write_unlock_bh(&sk->sk_callback_lock);
3147}
3148
Paolo Abenie16163b2020-11-16 10:48:09 +01003149bool mptcp_finish_join(struct sock *ssk)
Peter Krystadf2962342020-03-27 14:48:39 -07003150{
Paolo Abenie16163b2020-11-16 10:48:09 +01003151 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Peter Krystadf2962342020-03-27 14:48:39 -07003152 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3153 struct sock *parent = (void *)msk;
3154 struct socket *parent_sock;
Peter Krystadec3edaa2020-03-27 14:48:40 -07003155 bool ret;
Peter Krystadf2962342020-03-27 14:48:39 -07003156
3157 pr_debug("msk=%p, subflow=%p", msk, subflow);
3158
3159 /* mptcp socket already closing? */
Florian Westphaldc87efd2021-04-01 16:19:44 -07003160 if (!mptcp_is_fully_established(parent)) {
3161 subflow->reset_reason = MPTCP_RST_EMPTCP;
Peter Krystadf2962342020-03-27 14:48:39 -07003162 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003163 }
Peter Krystadf2962342020-03-27 14:48:39 -07003164
3165 if (!msk->pm.server_side)
Florian Westphalb911c972021-02-12 16:00:01 -08003166 goto out;
Peter Krystadf2962342020-03-27 14:48:39 -07003167
Florian Westphaldc87efd2021-04-01 16:19:44 -07003168 if (!mptcp_pm_allow_new_subflow(msk)) {
3169 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003170 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003171 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003172
3173 /* active connections are already on conn_list, and we can't acquire
3174 * msk lock here.
3175 * use the join list lock as synchronization point and double-check
Paolo Abenie16163b2020-11-16 10:48:09 +01003176 * msk status to avoid racing with __mptcp_destroy_sock()
Paolo Abeni10f6d462020-05-29 17:43:30 +02003177 */
3178 spin_lock_bh(&msk->join_list_lock);
3179 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
Paolo Abenie16163b2020-11-16 10:48:09 +01003180 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
Paolo Abeni10f6d462020-05-29 17:43:30 +02003181 list_add_tail(&subflow->node, &msk->join_list);
Paolo Abenie16163b2020-11-16 10:48:09 +01003182 sock_hold(ssk);
3183 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003184 spin_unlock_bh(&msk->join_list_lock);
Florian Westphaldc87efd2021-04-01 16:19:44 -07003185 if (!ret) {
3186 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003187 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003188 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003189
3190 /* attach to msk socket only after we are sure he will deal with us
3191 * at close time
3192 */
Peter Krystadf2962342020-03-27 14:48:39 -07003193 parent_sock = READ_ONCE(parent->sk_socket);
Paolo Abenie16163b2020-11-16 10:48:09 +01003194 if (parent_sock && !ssk->sk_socket)
3195 mptcp_sock_graft(ssk, parent_sock);
Mat Martineau917944d2020-09-29 15:08:19 -07003196 subflow->map_seq = READ_ONCE(msk->ack_seq);
Florian Westphalb911c972021-02-12 16:00:01 -08003197out:
3198 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
Paolo Abeni10f6d462020-05-29 17:43:30 +02003199 return true;
Peter Krystadf2962342020-03-27 14:48:39 -07003200}
3201
Paolo Abeni76e2a552021-01-12 18:25:24 +01003202static void mptcp_shutdown(struct sock *sk, int how)
3203{
3204 pr_debug("sk=%p, how=%d", sk, how);
3205
3206 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3207 __mptcp_wr_shutdown(sk);
3208}
3209
Paolo Abeni65118822021-10-26 16:29:15 -07003210static int mptcp_forward_alloc_get(const struct sock *sk)
3211{
3212 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3213}
3214
Florian Westphal644807e2021-12-03 14:35:34 -08003215static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3216{
3217 const struct sock *sk = (void *)msk;
3218 u64 delta;
3219
3220 if (sk->sk_state == TCP_LISTEN)
3221 return -EINVAL;
3222
3223 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3224 return 0;
3225
3226 delta = msk->write_seq - v;
3227 if (delta > INT_MAX)
3228 delta = INT_MAX;
3229
3230 return (int)delta;
3231}
3232
3233static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3234{
3235 struct mptcp_sock *msk = mptcp_sk(sk);
3236 bool slow;
3237 int answ;
3238
3239 switch (cmd) {
3240 case SIOCINQ:
3241 if (sk->sk_state == TCP_LISTEN)
3242 return -EINVAL;
3243
3244 lock_sock(sk);
3245 __mptcp_move_skbs(msk);
3246 answ = mptcp_inq_hint(sk);
3247 release_sock(sk);
3248 break;
3249 case SIOCOUTQ:
3250 slow = lock_sock_fast(sk);
3251 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3252 unlock_sock_fast(sk, slow);
3253 break;
3254 case SIOCOUTQNSD:
3255 slow = lock_sock_fast(sk);
3256 answ = mptcp_ioctl_outq(msk, msk->snd_nxt);
3257 unlock_sock_fast(sk, slow);
3258 break;
3259 default:
3260 return -ENOIOCTLCMD;
3261 }
3262
3263 return put_user(answ, (int __user *)arg);
3264}
3265
Mat Martineauf870fa02020-01-21 16:56:15 -08003266static struct proto mptcp_prot = {
3267 .name = "MPTCP",
3268 .owner = THIS_MODULE,
3269 .init = mptcp_init_sock,
Paolo Abeni18b683b2020-03-27 14:48:43 -07003270 .disconnect = mptcp_disconnect,
Mat Martineauf870fa02020-01-21 16:56:15 -08003271 .close = mptcp_close,
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003272 .accept = mptcp_accept,
Peter Krystad717e79c2020-01-21 16:56:22 -08003273 .setsockopt = mptcp_setsockopt,
3274 .getsockopt = mptcp_getsockopt,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003275 .shutdown = mptcp_shutdown,
Peter Krystad79c09492020-01-21 16:56:20 -08003276 .destroy = mptcp_destroy,
Mat Martineauf870fa02020-01-21 16:56:15 -08003277 .sendmsg = mptcp_sendmsg,
Florian Westphal644807e2021-12-03 14:35:34 -08003278 .ioctl = mptcp_ioctl,
Mat Martineauf870fa02020-01-21 16:56:15 -08003279 .recvmsg = mptcp_recvmsg,
Paolo Abeni14c441b2020-02-26 10:14:52 +01003280 .release_cb = mptcp_release_cb,
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003281 .hash = mptcp_hash,
3282 .unhash = mptcp_unhash,
Peter Krystadcec37a62020-01-21 16:56:18 -08003283 .get_port = mptcp_get_port,
Paolo Abeni65118822021-10-26 16:29:15 -07003284 .forward_alloc_get = mptcp_forward_alloc_get,
Paolo Abenid0272362020-03-27 14:48:45 -07003285 .sockets_allocated = &mptcp_sockets_allocated,
3286 .memory_allocated = &tcp_memory_allocated,
3287 .memory_pressure = &tcp_memory_pressure,
Paolo Abenid0272362020-03-27 14:48:45 -07003288 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
Paolo Abeni989ef492020-11-08 19:49:59 +01003289 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
Paolo Abenid0272362020-03-27 14:48:45 -07003290 .sysctl_mem = sysctl_tcp_mem,
Mat Martineauf870fa02020-01-21 16:56:15 -08003291 .obj_size = sizeof(struct mptcp_sock),
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003292 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Mat Martineauf870fa02020-01-21 16:56:15 -08003293 .no_autobind = true,
3294};
3295
Peter Krystad2303f992020-01-21 16:56:17 -08003296static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3297{
3298 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3299 struct socket *ssock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003300 int err;
Peter Krystad2303f992020-01-21 16:56:17 -08003301
3302 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003303 ssock = __mptcp_nmpc_socket(msk);
3304 if (!ssock) {
3305 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003306 goto unlock;
3307 }
3308
3309 err = ssock->ops->bind(ssock, uaddr, addr_len);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003310 if (!err)
3311 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08003312
3313unlock:
3314 release_sock(sock->sk);
3315 return err;
3316}
3317
Paolo Abeni0235d072020-07-23 13:02:31 +02003318static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3319 struct mptcp_subflow_context *subflow)
3320{
3321 subflow->request_mptcp = 0;
3322 __mptcp_do_fallback(msk);
3323}
3324
Peter Krystad2303f992020-01-21 16:56:17 -08003325static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3326 int addr_len, int flags)
3327{
3328 struct mptcp_sock *msk = mptcp_sk(sock->sk);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003329 struct mptcp_subflow_context *subflow;
Peter Krystad2303f992020-01-21 16:56:17 -08003330 struct socket *ssock;
3331 int err;
3332
3333 lock_sock(sock->sk);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003334 if (sock->state != SS_UNCONNECTED && msk->subflow) {
3335 /* pending connection or invalid state, let existing subflow
3336 * cope with that
3337 */
3338 ssock = msk->subflow;
3339 goto do_connect;
3340 }
3341
Paolo Abenifa680182020-06-29 22:26:23 +02003342 ssock = __mptcp_nmpc_socket(msk);
3343 if (!ssock) {
3344 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003345 goto unlock;
3346 }
3347
Paolo Abenifa680182020-06-29 22:26:23 +02003348 mptcp_token_destroy(msk);
3349 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003350 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003351#ifdef CONFIG_TCP_MD5SIG
3352 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3353 * TCP option space.
3354 */
3355 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
Paolo Abeni0235d072020-07-23 13:02:31 +02003356 mptcp_subflow_early_fallback(msk, subflow);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003357#endif
Paolo Abenia16195e2021-04-01 16:19:41 -07003358 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3359 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
Paolo Abeni0235d072020-07-23 13:02:31 +02003360 mptcp_subflow_early_fallback(msk, subflow);
Paolo Abenia16195e2021-04-01 16:19:41 -07003361 }
Paolo Abeni5695eb82021-04-01 16:19:42 -07003362 if (likely(!__mptcp_check_fallback(msk)))
3363 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003364
Paolo Abeni41be81a2020-05-29 17:43:29 +02003365do_connect:
Peter Krystad2303f992020-01-21 16:56:17 -08003366 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003367 sock->state = ssock->state;
3368
3369 /* on successful connect, the msk state will be moved to established by
3370 * subflow_finish_connect()
3371 */
Matthieu Baerts367fe042020-07-27 12:24:33 +02003372 if (!err || err == -EINPROGRESS)
Paolo Abeni41be81a2020-05-29 17:43:29 +02003373 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3374 else
3375 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
Peter Krystad2303f992020-01-21 16:56:17 -08003376
3377unlock:
3378 release_sock(sock->sk);
3379 return err;
3380}
3381
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003382static int mptcp_listen(struct socket *sock, int backlog)
3383{
3384 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3385 struct socket *ssock;
3386 int err;
3387
3388 pr_debug("msk=%p", msk);
3389
3390 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003391 ssock = __mptcp_nmpc_socket(msk);
3392 if (!ssock) {
3393 err = -EINVAL;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003394 goto unlock;
3395 }
3396
Paolo Abenifa680182020-06-29 22:26:23 +02003397 mptcp_token_destroy(msk);
3398 inet_sk_state_store(sock->sk, TCP_LISTEN);
Florian Westphal5e200872020-04-20 16:25:04 +02003399 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3400
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003401 err = ssock->ops->listen(ssock, backlog);
3402 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3403 if (!err)
3404 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3405
3406unlock:
3407 release_sock(sock->sk);
3408 return err;
3409}
3410
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003411static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3412 int flags, bool kern)
3413{
3414 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3415 struct socket *ssock;
3416 int err;
3417
3418 pr_debug("msk=%p", msk);
3419
3420 lock_sock(sock->sk);
3421 if (sock->sk->sk_state != TCP_LISTEN)
3422 goto unlock_fail;
3423
3424 ssock = __mptcp_nmpc_socket(msk);
3425 if (!ssock)
3426 goto unlock_fail;
3427
Paolo Abeni8a056612020-06-29 22:26:25 +02003428 clear_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003429 sock_hold(ssock->sk);
3430 release_sock(sock->sk);
3431
3432 err = ssock->ops->accept(sock, newsock, flags, kern);
Paolo Abenid2f77c52020-06-29 22:26:22 +02003433 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003434 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3435 struct mptcp_subflow_context *subflow;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003436 struct sock *newsk = newsock->sk;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003437
Florian Westphal4d54cc32021-02-12 15:59:59 -08003438 lock_sock(newsk);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003439
3440 /* PM/worker can now acquire the first subflow socket
3441 * lock without racing with listener queue cleanup,
3442 * we can notify it, if needed.
Florian Westphalc8fe62f2021-03-04 13:32:12 -08003443 *
3444 * Even if remote has reset the initial subflow by now
3445 * the refcnt is still at least one.
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003446 */
3447 subflow = mptcp_subflow_ctx(msk->first);
3448 list_add(&subflow->node, &msk->conn_list);
3449 sock_hold(msk->first);
3450 if (mptcp_is_fully_established(newsk))
Florian Westphal6c714f12021-02-12 15:59:58 -08003451 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003452
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003453 mptcp_copy_inaddrs(newsk, msk->first);
3454 mptcp_rcv_space_init(msk, msk->first);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003455 mptcp_propagate_sndbuf(newsk, msk->first);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003456
3457 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3458 * This is needed so NOSPACE flag can be set from tcp stack.
3459 */
Florian Westphal78962482021-04-15 16:44:53 -07003460 mptcp_flush_join_list(msk);
Geliang Tang190f8b02020-08-03 21:00:44 +08003461 mptcp_for_each_subflow(msk, subflow) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003462 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3463
3464 if (!ssk->sk_socket)
3465 mptcp_sock_graft(ssk, newsock);
3466 }
Florian Westphal4d54cc32021-02-12 15:59:59 -08003467 release_sock(newsk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003468 }
3469
Paolo Abeni8a056612020-06-29 22:26:25 +02003470 if (inet_csk_listen_poll(ssock->sk))
3471 set_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003472 sock_put(ssock->sk);
3473 return err;
3474
3475unlock_fail:
3476 release_sock(sock->sk);
3477 return -EINVAL;
3478}
3479
Paolo Abeni8a056612020-06-29 22:26:25 +02003480static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3481{
Paolo Abeni612f71d2021-10-07 15:05:00 -07003482 /* Concurrent splices from sk_receive_queue into receive_queue will
3483 * always show at least one non-empty queue when checked in this order.
3484 */
3485 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
3486 skb_queue_empty_lockless(&msk->receive_queue))
3487 return 0;
3488
3489 return EPOLLIN | EPOLLRDNORM;
Paolo Abeni8a056612020-06-29 22:26:25 +02003490}
3491
Florian Westphal8edf0862020-11-16 10:48:12 +01003492static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3493{
3494 struct sock *sk = (struct sock *)msk;
Florian Westphal8edf0862020-11-16 10:48:12 +01003495
3496 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
Paolo Abenidd913412021-02-11 15:30:38 -08003497 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003498
3499 if (sk_stream_is_writeable(sk))
3500 return EPOLLOUT | EPOLLWRNORM;
3501
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003502 mptcp_set_nospace(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003503 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3504 if (sk_stream_is_writeable(sk))
3505 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003506
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003507 return 0;
Florian Westphal8edf0862020-11-16 10:48:12 +01003508}
3509
Peter Krystad2303f992020-01-21 16:56:17 -08003510static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3511 struct poll_table_struct *wait)
3512{
Florian Westphal1891c4a2020-01-21 16:56:25 -08003513 struct sock *sk = sock->sk;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08003514 struct mptcp_sock *msk;
Peter Krystad2303f992020-01-21 16:56:17 -08003515 __poll_t mask = 0;
Paolo Abeni8a056612020-06-29 22:26:25 +02003516 int state;
Peter Krystad2303f992020-01-21 16:56:17 -08003517
Florian Westphal1891c4a2020-01-21 16:56:25 -08003518 msk = mptcp_sk(sk);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003519 sock_poll_wait(file, sock, wait);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003520
Paolo Abeni8a056612020-06-29 22:26:25 +02003521 state = inet_sk_state_load(sk);
Paolo Abeni67193312020-09-14 10:01:09 +02003522 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
Paolo Abeni8a056612020-06-29 22:26:25 +02003523 if (state == TCP_LISTEN)
Paolo Abeni612f71d2021-10-07 15:05:00 -07003524 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0;
Paolo Abeni8a056612020-06-29 22:26:25 +02003525
3526 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3527 mask |= mptcp_check_readable(msk);
Florian Westphal8edf0862020-11-16 10:48:12 +01003528 mask |= mptcp_check_writeable(msk);
Paolo Abeni8a056612020-06-29 22:26:25 +02003529 }
Paolo Abenidd913412021-02-11 15:30:38 -08003530 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3531 mask |= EPOLLHUP;
Florian Westphal1891c4a2020-01-21 16:56:25 -08003532 if (sk->sk_shutdown & RCV_SHUTDOWN)
3533 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3534
Paolo Abeni15cc1042021-02-11 15:30:37 -08003535 /* This barrier is coupled with smp_wmb() in tcp_reset() */
3536 smp_rmb();
3537 if (sk->sk_err)
3538 mask |= EPOLLERR;
3539
Peter Krystad2303f992020-01-21 16:56:17 -08003540 return mask;
3541}
3542
Florian Westphale42f1ac2020-01-24 16:04:02 -08003543static const struct proto_ops mptcp_stream_ops = {
3544 .family = PF_INET,
3545 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003546 .release = inet_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003547 .bind = mptcp_bind,
3548 .connect = mptcp_stream_connect,
3549 .socketpair = sock_no_socketpair,
3550 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003551 .getname = inet_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003552 .poll = mptcp_poll,
3553 .ioctl = inet_ioctl,
3554 .gettstamp = sock_gettstamp,
3555 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003556 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003557 .setsockopt = sock_common_setsockopt,
3558 .getsockopt = sock_common_getsockopt,
3559 .sendmsg = inet_sendmsg,
3560 .recvmsg = inet_recvmsg,
3561 .mmap = sock_no_mmap,
3562 .sendpage = inet_sendpage,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003563};
Peter Krystad2303f992020-01-21 16:56:17 -08003564
Mat Martineauf870fa02020-01-21 16:56:15 -08003565static struct inet_protosw mptcp_protosw = {
3566 .type = SOCK_STREAM,
3567 .protocol = IPPROTO_MPTCP,
3568 .prot = &mptcp_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003569 .ops = &mptcp_stream_ops,
3570 .flags = INET_PROTOSW_ICSK,
Mat Martineauf870fa02020-01-21 16:56:15 -08003571};
3572
Paolo Abenib19bc292021-01-20 15:39:14 +01003573static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3574{
3575 struct mptcp_delegated_action *delegated;
3576 struct mptcp_subflow_context *subflow;
3577 int work_done = 0;
3578
3579 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3580 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3581 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3582
3583 bh_lock_sock_nested(ssk);
3584 if (!sock_owned_by_user(ssk) &&
3585 mptcp_subflow_has_delegated_action(subflow))
3586 mptcp_subflow_process_delegated(ssk);
3587 /* ... elsewhere tcp_release_cb_override already processed
3588 * the action or will do at next release_sock().
3589 * In both case must dequeue the subflow here - on the same
3590 * CPU that scheduled it.
3591 */
3592 bh_unlock_sock(ssk);
3593 sock_put(ssk);
3594
3595 if (++work_done == budget)
3596 return budget;
3597 }
3598
3599 /* always provide a 0 'work_done' argument, so that napi_complete_done
3600 * will not try accessing the NULL napi->dev ptr
3601 */
3602 napi_complete_done(napi, 0);
3603 return work_done;
3604}
3605
Paolo Abenid39dcec2020-06-26 19:29:59 +02003606void __init mptcp_proto_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003607{
Paolo Abenib19bc292021-01-20 15:39:14 +01003608 struct mptcp_delegated_action *delegated;
3609 int cpu;
3610
Peter Krystad2303f992020-01-21 16:56:17 -08003611 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
Peter Krystad2303f992020-01-21 16:56:17 -08003612
Paolo Abenid0272362020-03-27 14:48:45 -07003613 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3614 panic("Failed to allocate MPTCP pcpu counter\n");
3615
Paolo Abenib19bc292021-01-20 15:39:14 +01003616 init_dummy_netdev(&mptcp_napi_dev);
3617 for_each_possible_cpu(cpu) {
3618 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3619 INIT_LIST_HEAD(&delegated->head);
3620 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3621 NAPI_POLL_WEIGHT);
3622 napi_enable(&delegated->napi);
3623 }
3624
Peter Krystad2303f992020-01-21 16:56:17 -08003625 mptcp_subflow_init();
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003626 mptcp_pm_init();
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003627 mptcp_token_init();
Peter Krystad2303f992020-01-21 16:56:17 -08003628
Mat Martineauf870fa02020-01-21 16:56:15 -08003629 if (proto_register(&mptcp_prot, 1) != 0)
3630 panic("Failed to register MPTCP proto.\n");
3631
3632 inet_register_protosw(&mptcp_protosw);
Florian Westphal6771bfd2020-02-26 10:14:48 +01003633
3634 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
Mat Martineauf870fa02020-01-21 16:56:15 -08003635}
3636
3637#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Florian Westphale42f1ac2020-01-24 16:04:02 -08003638static const struct proto_ops mptcp_v6_stream_ops = {
3639 .family = PF_INET6,
3640 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003641 .release = inet6_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003642 .bind = mptcp_bind,
3643 .connect = mptcp_stream_connect,
3644 .socketpair = sock_no_socketpair,
3645 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003646 .getname = inet6_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003647 .poll = mptcp_poll,
3648 .ioctl = inet6_ioctl,
3649 .gettstamp = sock_gettstamp,
3650 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003651 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003652 .setsockopt = sock_common_setsockopt,
3653 .getsockopt = sock_common_getsockopt,
3654 .sendmsg = inet6_sendmsg,
3655 .recvmsg = inet6_recvmsg,
3656 .mmap = sock_no_mmap,
3657 .sendpage = inet_sendpage,
3658#ifdef CONFIG_COMPAT
Christoph Hellwig39869122020-05-18 08:28:06 +02003659 .compat_ioctl = inet6_compat_ioctl,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003660#endif
3661};
3662
Mat Martineauf870fa02020-01-21 16:56:15 -08003663static struct proto mptcp_v6_prot;
3664
Peter Krystad79c09492020-01-21 16:56:20 -08003665static void mptcp_v6_destroy(struct sock *sk)
3666{
3667 mptcp_destroy(sk);
3668 inet6_destroy_sock(sk);
3669}
3670
Mat Martineauf870fa02020-01-21 16:56:15 -08003671static struct inet_protosw mptcp_v6_protosw = {
3672 .type = SOCK_STREAM,
3673 .protocol = IPPROTO_MPTCP,
3674 .prot = &mptcp_v6_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003675 .ops = &mptcp_v6_stream_ops,
Mat Martineauf870fa02020-01-21 16:56:15 -08003676 .flags = INET_PROTOSW_ICSK,
3677};
3678
Paolo Abenid39dcec2020-06-26 19:29:59 +02003679int __init mptcp_proto_v6_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003680{
3681 int err;
3682
3683 mptcp_v6_prot = mptcp_prot;
3684 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3685 mptcp_v6_prot.slab = NULL;
Peter Krystad79c09492020-01-21 16:56:20 -08003686 mptcp_v6_prot.destroy = mptcp_v6_destroy;
Florian Westphalb0519de2020-02-06 00:39:37 +01003687 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
Mat Martineauf870fa02020-01-21 16:56:15 -08003688
3689 err = proto_register(&mptcp_v6_prot, 1);
3690 if (err)
3691 return err;
3692
Mat Martineauf870fa02020-01-21 16:56:15 -08003693 err = inet6_register_protosw(&mptcp_v6_protosw);
3694 if (err)
3695 proto_unregister(&mptcp_v6_prot);
3696
3697 return err;
3698}
3699#endif