blob: f6e62a6dc9fbd18e7fbc20a8e954ce5511140cae [file] [log] [blame]
Mat Martineauf870fa02020-01-21 16:56:15 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/netdevice.h>
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -080012#include <linux/sched/signal.h>
13#include <linux/atomic.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080014#include <net/sock.h>
15#include <net/inet_common.h>
16#include <net/inet_hashtables.h>
17#include <net/protocol.h>
18#include <net/tcp.h>
Mat Martineau3721b9b2020-07-28 15:12:03 -070019#include <net/tcp_states.h>
Peter Krystadcf7da0d2020-01-21 16:56:19 -080020#if IS_ENABLED(CONFIG_MPTCP_IPV6)
21#include <net/transp_v6.h>
22#endif
Mat Martineauf870fa02020-01-21 16:56:15 -080023#include <net/mptcp.h>
Paolo Abenie16163b2020-11-16 10:48:09 +010024#include <net/xfrm.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080025#include "protocol.h"
Florian Westphalfc518952020-03-27 14:48:50 -070026#include "mib.h"
Mat Martineauf870fa02020-01-21 16:56:15 -080027
Geliang Tange10a9892021-04-16 15:38:04 -070028#define CREATE_TRACE_POINTS
29#include <trace/events/mptcp.h>
30
Florian Westphalb0519de2020-02-06 00:39:37 +010031#if IS_ENABLED(CONFIG_MPTCP_IPV6)
32struct mptcp6_sock {
33 struct mptcp_sock msk;
34 struct ipv6_pinfo np;
35};
36#endif
37
Florian Westphal6771bfd2020-02-26 10:14:48 +010038struct mptcp_skb_cb {
Paolo Abeniab174ad2020-09-14 10:01:12 +020039 u64 map_seq;
40 u64 end_seq;
Florian Westphal6771bfd2020-02-26 10:14:48 +010041 u32 offset;
42};
43
44#define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
45
Paolo Abenid0272362020-03-27 14:48:45 -070046static struct percpu_counter mptcp_sockets_allocated;
47
Paolo Abenie16163b2020-11-16 10:48:09 +010048static void __mptcp_destroy_sock(struct sock *sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +010049static void __mptcp_check_send_data_fin(struct sock *sk);
Paolo Abenie16163b2020-11-16 10:48:09 +010050
Paolo Abenib19bc292021-01-20 15:39:14 +010051DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
52static struct net_device mptcp_napi_dev;
53
Peter Krystad2303f992020-01-21 16:56:17 -080054/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
55 * completed yet or has failed, return the subflow socket.
56 * Otherwise return NULL.
57 */
Geliang Tang1729cf12021-02-01 15:09:12 -080058struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -080059{
Christoph Paaschd22f4982020-01-21 16:56:32 -080060 if (!msk->subflow || READ_ONCE(msk->can_ack))
Peter Krystad2303f992020-01-21 16:56:17 -080061 return NULL;
62
63 return msk->subflow;
64}
65
Florian Westphal6f8a6122020-11-16 10:48:13 +010066/* Returns end sequence number of the receiver's advertised window */
67static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
68{
Paolo Abeni7439d682020-11-27 11:10:26 +010069 return READ_ONCE(msk->wnd_end);
Florian Westphal6f8a6122020-11-16 10:48:13 +010070}
71
Paolo Abenid2f77c52020-06-29 22:26:22 +020072static bool mptcp_is_tcpsk(struct sock *sk)
Florian Westphal0b4f33d2020-04-02 13:44:51 +020073{
74 struct socket *sock = sk->sk_socket;
75
Florian Westphal0b4f33d2020-04-02 13:44:51 +020076 if (unlikely(sk->sk_prot == &tcp_prot)) {
77 /* we are being invoked after mptcp_accept() has
78 * accepted a non-mp-capable flow: sk is a tcp_sk,
79 * not an mptcp one.
80 *
81 * Hand the socket over to tcp so all further socket ops
82 * bypass mptcp.
83 */
84 sock->ops = &inet_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020085 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020086#if IS_ENABLED(CONFIG_MPTCP_IPV6)
87 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
88 sock->ops = &inet6_stream_ops;
Paolo Abenid2f77c52020-06-29 22:26:22 +020089 return true;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020090#endif
91 }
92
Paolo Abenid2f77c52020-06-29 22:26:22 +020093 return false;
Florian Westphal0b4f33d2020-04-02 13:44:51 +020094}
95
Paolo Abenifa680182020-06-29 22:26:23 +020096static int __mptcp_socket_create(struct mptcp_sock *msk)
Peter Krystad2303f992020-01-21 16:56:17 -080097{
98 struct mptcp_subflow_context *subflow;
99 struct sock *sk = (struct sock *)msk;
100 struct socket *ssock;
101 int err;
102
Peter Krystad2303f992020-01-21 16:56:17 -0800103 err = mptcp_subflow_create_socket(sk, &ssock);
104 if (err)
Paolo Abenifa680182020-06-29 22:26:23 +0200105 return err;
Peter Krystad2303f992020-01-21 16:56:17 -0800106
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800107 msk->first = ssock->sk;
Peter Krystad2303f992020-01-21 16:56:17 -0800108 msk->subflow = ssock;
109 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800110 list_add(&subflow->node, &msk->conn_list);
Paolo Abenie16163b2020-11-16 10:48:09 +0100111 sock_hold(ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -0800112 subflow->request_mptcp = 1;
Paolo Abeni866f26f2021-01-20 15:39:10 +0100113 mptcp_sock_graft(msk->first, sk->sk_socket);
Davide Carattie1ff9e82020-06-29 22:26:20 +0200114
Paolo Abenifa680182020-06-29 22:26:23 +0200115 return 0;
Peter Krystad2303f992020-01-21 16:56:17 -0800116}
117
Paolo Abeniab174ad2020-09-14 10:01:12 +0200118static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
119{
120 sk_drops_add(sk, skb);
121 __kfree_skb(skb);
122}
123
Paolo Abeni8268ed42020-09-14 10:01:11 +0200124static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
125 struct sk_buff *from)
126{
127 bool fragstolen;
128 int delta;
129
130 if (MPTCP_SKB_CB(from)->offset ||
131 !skb_try_coalesce(to, from, &fragstolen, &delta))
132 return false;
133
Paolo Abeni06242e42020-09-14 10:01:14 +0200134 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
135 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
136 to->len, MPTCP_SKB_CB(from)->end_seq);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200137 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200138 kfree_skb_partial(from, fragstolen);
139 atomic_add(delta, &sk->sk_rmem_alloc);
140 sk_mem_charge(sk, delta);
141 return true;
142}
143
Paolo Abeniab174ad2020-09-14 10:01:12 +0200144static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
145 struct sk_buff *from)
Florian Westphal6771bfd2020-02-26 10:14:48 +0100146{
Paolo Abeniab174ad2020-09-14 10:01:12 +0200147 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
148 return false;
149
150 return mptcp_try_coalesce((struct sock *)msk, to, from);
151}
152
153/* "inspired" by tcp_data_queue_ofo(), main differences:
154 * - use mptcp seqs
155 * - don't cope with sacks
156 */
157static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
158{
159 struct sock *sk = (struct sock *)msk;
160 struct rb_node **p, *parent;
161 u64 seq, end_seq, max_seq;
162 struct sk_buff *skb1;
163
164 seq = MPTCP_SKB_CB(skb)->map_seq;
165 end_seq = MPTCP_SKB_CB(skb)->end_seq;
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800166 max_seq = READ_ONCE(msk->rcv_wnd_sent);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200167
Paolo Abeni06242e42020-09-14 10:01:14 +0200168 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
169 RB_EMPTY_ROOT(&msk->out_of_order_queue));
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800170 if (after64(end_seq, max_seq)) {
Paolo Abeniab174ad2020-09-14 10:01:12 +0200171 /* out of window */
172 mptcp_drop(sk, skb);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800173 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
174 (unsigned long long)end_seq - (unsigned long)max_seq,
175 (unsigned long long)msk->rcv_wnd_sent);
Paolo Abeni06242e42020-09-14 10:01:14 +0200176 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200177 return;
178 }
179
180 p = &msk->out_of_order_queue.rb_node;
Paolo Abeni06242e42020-09-14 10:01:14 +0200181 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200182 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
183 rb_link_node(&skb->rbnode, NULL, p);
184 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
185 msk->ooo_last_skb = skb;
186 goto end;
187 }
188
189 /* with 2 subflows, adding at end of ooo queue is quite likely
190 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
191 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200192 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
193 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
194 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200195 return;
Paolo Abeni06242e42020-09-14 10:01:14 +0200196 }
Paolo Abeniab174ad2020-09-14 10:01:12 +0200197
198 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
199 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200200 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200201 parent = &msk->ooo_last_skb->rbnode;
202 p = &parent->rb_right;
203 goto insert;
204 }
205
206 /* Find place to insert this segment. Handle overlaps on the way. */
207 parent = NULL;
208 while (*p) {
209 parent = *p;
210 skb1 = rb_to_skb(parent);
211 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
212 p = &parent->rb_left;
213 continue;
214 }
215 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
216 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
217 /* All the bits are present. Drop. */
218 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200219 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200220 return;
221 }
222 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
223 /* partial overlap:
224 * | skb |
225 * | skb1 |
226 * continue traversing
227 */
228 } else {
229 /* skb's seq == skb1's seq and skb covers skb1.
230 * Replace skb1 with skb.
231 */
232 rb_replace_node(&skb1->rbnode, &skb->rbnode,
233 &msk->out_of_order_queue);
234 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200235 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200236 goto merge_right;
237 }
238 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
Paolo Abeni06242e42020-09-14 10:01:14 +0200239 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200240 return;
241 }
242 p = &parent->rb_right;
243 }
Paolo Abeni06242e42020-09-14 10:01:14 +0200244
Paolo Abeniab174ad2020-09-14 10:01:12 +0200245insert:
246 /* Insert segment into RB tree. */
247 rb_link_node(&skb->rbnode, parent, p);
248 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
249
250merge_right:
251 /* Remove other segments covered by skb. */
252 while ((skb1 = skb_rb_next(skb)) != NULL) {
253 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
254 break;
255 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
256 mptcp_drop(sk, skb1);
Paolo Abeni06242e42020-09-14 10:01:14 +0200257 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200258 }
259 /* If there is no skb after us, we are the last_skb ! */
260 if (!skb1)
261 msk->ooo_last_skb = skb;
262
263end:
264 skb_condense(skb);
265 skb_set_owner_r(skb, sk);
266}
267
268static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
269 struct sk_buff *skb, unsigned int offset,
270 size_t copy_len)
271{
272 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100273 struct sock *sk = (struct sock *)msk;
Florian Westphal4e637c72020-05-25 23:41:13 +0200274 struct sk_buff *tail;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100275
276 __skb_unlink(skb, &ssk->sk_receive_queue);
Florian Westphal4e637c72020-05-25 23:41:13 +0200277
278 skb_ext_reset(skb);
279 skb_orphan(skb);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200280
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100281 /* try to fetch required memory from subflow */
282 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
Paolo Abeni72f96132021-06-10 15:59:40 -0700283 int amount = sk_mem_pages(skb->truesize) << SK_MEM_QUANTUM_SHIFT;
284
285 if (ssk->sk_forward_alloc < amount)
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100286 goto drop;
Paolo Abeni72f96132021-06-10 15:59:40 -0700287
288 ssk->sk_forward_alloc -= amount;
289 sk->sk_forward_alloc += amount;
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100290 }
291
Paolo Abeniab174ad2020-09-14 10:01:12 +0200292 /* the skb map_seq accounts for the skb offset:
293 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
294 * value
295 */
296 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
297 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
Paolo Abeni8268ed42020-09-14 10:01:11 +0200298 MPTCP_SKB_CB(skb)->offset = offset;
Florian Westphal4e637c72020-05-25 23:41:13 +0200299
Paolo Abeniab174ad2020-09-14 10:01:12 +0200300 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
301 /* in sequence */
David S. Miller8b0308f2020-10-05 17:33:26 -0700302 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200303 tail = skb_peek_tail(&sk->sk_receive_queue);
304 if (tail && mptcp_try_coalesce(sk, tail, skb))
305 return true;
Florian Westphal4e637c72020-05-25 23:41:13 +0200306
Paolo Abeniab174ad2020-09-14 10:01:12 +0200307 skb_set_owner_r(skb, sk);
308 __skb_queue_tail(&sk->sk_receive_queue, skb);
309 return true;
310 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
311 mptcp_data_queue_ofo(msk, skb);
312 return false;
313 }
314
315 /* old data, keep it simple and drop the whole pkt, sender
316 * will retransmit as needed, if needed.
317 */
Paolo Abeni06242e42020-09-14 10:01:14 +0200318 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeni9c3f94e2020-10-27 15:59:14 +0100319drop:
Paolo Abeniab174ad2020-09-14 10:01:12 +0200320 mptcp_drop(sk, skb);
321 return false;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100322}
323
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700324static void mptcp_stop_timer(struct sock *sk)
325{
326 struct inet_connection_sock *icsk = inet_csk(sk);
327
328 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
329 mptcp_sk(sk)->timer_ival = 0;
330}
331
Paolo Abenie16163b2020-11-16 10:48:09 +0100332static void mptcp_close_wake_up(struct sock *sk)
333{
334 if (sock_flag(sk, SOCK_DEAD))
335 return;
336
337 sk->sk_state_change(sk);
338 if (sk->sk_shutdown == SHUTDOWN_MASK ||
339 sk->sk_state == TCP_CLOSE)
340 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
341 else
342 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
343}
344
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100345static bool mptcp_pending_data_fin_ack(struct sock *sk)
346{
347 struct mptcp_sock *msk = mptcp_sk(sk);
348
349 return !__mptcp_check_fallback(msk) &&
350 ((1 << sk->sk_state) &
351 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
352 msk->write_seq == READ_ONCE(msk->snd_una);
353}
354
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700355static void mptcp_check_data_fin_ack(struct sock *sk)
356{
357 struct mptcp_sock *msk = mptcp_sk(sk);
358
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700359 /* Look for an acknowledged DATA_FIN */
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100360 if (mptcp_pending_data_fin_ack(sk)) {
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700361 WRITE_ONCE(msk->snd_data_fin_enable, 0);
362
363 switch (sk->sk_state) {
364 case TCP_FIN_WAIT1:
365 inet_sk_state_store(sk, TCP_FIN_WAIT2);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700366 break;
367 case TCP_CLOSING:
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700368 case TCP_LAST_ACK:
369 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700370 break;
371 }
372
Paolo Abenie16163b2020-11-16 10:48:09 +0100373 mptcp_close_wake_up(sk);
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700374 }
375}
376
Mat Martineau3721b9b2020-07-28 15:12:03 -0700377static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
378{
379 struct mptcp_sock *msk = mptcp_sk(sk);
380
381 if (READ_ONCE(msk->rcv_data_fin) &&
382 ((1 << sk->sk_state) &
383 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
384 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
385
386 if (msk->ack_seq == rcv_data_fin_seq) {
387 if (seq)
388 *seq = rcv_data_fin_seq;
389
390 return true;
391 }
392 }
393
394 return false;
395}
396
Mat Martineau6477dd32021-04-23 09:40:33 -0700397static void mptcp_set_datafin_timeout(const struct sock *sk)
398{
399 struct inet_connection_sock *icsk = inet_csk(sk);
400
401 mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX,
402 TCP_RTO_MIN << icsk->icsk_retransmits);
403}
404
Mat Martineau3721b9b2020-07-28 15:12:03 -0700405static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
406{
407 long tout = ssk && inet_csk(ssk)->icsk_pending ?
408 inet_csk(ssk)->icsk_timeout - jiffies : 0;
409
410 if (tout <= 0)
411 tout = mptcp_sk(sk)->timer_ival;
412 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
413}
414
Paolo Abenifd897672020-11-24 22:51:24 +0100415static bool tcp_can_send_ack(const struct sock *ssk)
416{
417 return !((1 << inet_sk_state_load(ssk)) &
Paolo Abeni20bc80b2021-01-12 18:25:23 +0100418 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
Paolo Abenifd897672020-11-24 22:51:24 +0100419}
420
421static void mptcp_send_ack(struct mptcp_sock *msk)
Paolo Abeni7ed90802020-11-16 10:48:14 +0100422{
423 struct mptcp_subflow_context *subflow;
424
425 mptcp_for_each_subflow(msk, subflow) {
426 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
427
Paolo Abenifd897672020-11-24 22:51:24 +0100428 lock_sock(ssk);
429 if (tcp_can_send_ack(ssk))
Paolo Abeniea4ca582020-11-19 11:46:03 -0800430 tcp_send_ack(ssk);
Paolo Abenifd897672020-11-24 22:51:24 +0100431 release_sock(ssk);
432 }
433}
Paolo Abeniea4ca582020-11-19 11:46:03 -0800434
Paolo Abenifd897672020-11-24 22:51:24 +0100435static bool mptcp_subflow_cleanup_rbuf(struct sock *ssk)
436{
437 int ret;
438
439 lock_sock(ssk);
440 ret = tcp_can_send_ack(ssk);
441 if (ret)
442 tcp_cleanup_rbuf(ssk, 1);
443 release_sock(ssk);
444 return ret;
445}
446
447static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
448{
Paolo Abeni87952602020-11-27 11:10:24 +0100449 struct sock *ack_hint = READ_ONCE(msk->ack_hint);
Paolo Abenie3859602021-02-11 15:30:41 -0800450 int old_space = READ_ONCE(msk->old_wspace);
Paolo Abenifd897672020-11-24 22:51:24 +0100451 struct mptcp_subflow_context *subflow;
Paolo Abenie3859602021-02-11 15:30:41 -0800452 struct sock *sk = (struct sock *)msk;
453 bool cleanup;
454
455 /* this is a simple superset of what tcp_cleanup_rbuf() implements
456 * so that we don't have to acquire the ssk socket lock most of the time
457 * to do actually nothing
458 */
459 cleanup = __mptcp_space(sk) - old_space >= max(0, old_space);
460 if (!cleanup)
461 return;
Paolo Abenifd897672020-11-24 22:51:24 +0100462
463 /* if the hinted ssk is still active, try to use it */
Paolo Abeni87952602020-11-27 11:10:24 +0100464 if (likely(ack_hint)) {
Paolo Abenifd897672020-11-24 22:51:24 +0100465 mptcp_for_each_subflow(msk, subflow) {
466 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
467
Paolo Abeni87952602020-11-27 11:10:24 +0100468 if (ack_hint == ssk && mptcp_subflow_cleanup_rbuf(ssk))
Paolo Abenifd897672020-11-24 22:51:24 +0100469 return;
470 }
Paolo Abeniea4ca582020-11-19 11:46:03 -0800471 }
Paolo Abenifd897672020-11-24 22:51:24 +0100472
473 /* otherwise pick the first active subflow */
474 mptcp_for_each_subflow(msk, subflow)
475 if (mptcp_subflow_cleanup_rbuf(mptcp_subflow_tcp_sock(subflow)))
476 return;
Paolo Abeni7ed90802020-11-16 10:48:14 +0100477}
478
479static bool mptcp_check_data_fin(struct sock *sk)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700480{
481 struct mptcp_sock *msk = mptcp_sk(sk);
482 u64 rcv_data_fin_seq;
Paolo Abeni7ed90802020-11-16 10:48:14 +0100483 bool ret = false;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700484
Paolo Abeni781bf132021-04-01 16:19:43 -0700485 if (__mptcp_check_fallback(msk))
Paolo Abeni7ed90802020-11-16 10:48:14 +0100486 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700487
488 /* Need to ack a DATA_FIN received from a peer while this side
489 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
490 * msk->rcv_data_fin was set when parsing the incoming options
491 * at the subflow level and the msk lock was not held, so this
492 * is the first opportunity to act on the DATA_FIN and change
493 * the msk state.
494 *
495 * If we are caught up to the sequence number of the incoming
496 * DATA_FIN, send the DATA_ACK now and do state transition. If
497 * not caught up, do nothing and let the recv code send DATA_ACK
498 * when catching up.
499 */
500
501 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
Mat Martineau917944d2020-09-29 15:08:19 -0700502 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700503 WRITE_ONCE(msk->rcv_data_fin, 0);
504
505 sk->sk_shutdown |= RCV_SHUTDOWN;
Mat Martineau16a9a9d2020-07-28 15:12:05 -0700506 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
507 set_bit(MPTCP_DATA_READY, &msk->flags);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700508
509 switch (sk->sk_state) {
510 case TCP_ESTABLISHED:
511 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
512 break;
513 case TCP_FIN_WAIT1:
514 inet_sk_state_store(sk, TCP_CLOSING);
515 break;
516 case TCP_FIN_WAIT2:
517 inet_sk_state_store(sk, TCP_CLOSE);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700518 break;
519 default:
520 /* Other states not expected */
521 WARN_ON_ONCE(1);
522 break;
523 }
524
Paolo Abeni7ed90802020-11-16 10:48:14 +0100525 ret = true;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700526 mptcp_set_timeout(sk, NULL);
Paolo Abenifd897672020-11-24 22:51:24 +0100527 mptcp_send_ack(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +0100528 mptcp_close_wake_up(sk);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700529 }
Paolo Abeni7ed90802020-11-16 10:48:14 +0100530 return ret;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700531}
532
Florian Westphal6771bfd2020-02-26 10:14:48 +0100533static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
534 struct sock *ssk,
535 unsigned int *bytes)
536{
537 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100538 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100539 unsigned int moved = 0;
540 bool more_data_avail;
541 struct tcp_sock *tp;
542 bool done = false;
Florian Westphal13c7ba02020-11-03 11:05:03 -0800543 int sk_rbuf;
544
545 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
546
547 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
548 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
549
550 if (unlikely(ssk_rbuf > sk_rbuf)) {
551 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
552 sk_rbuf = ssk_rbuf;
553 }
554 }
Florian Westphal600911f2020-02-26 10:14:49 +0100555
Paolo Abeniab174ad2020-09-14 10:01:12 +0200556 pr_debug("msk=%p ssk=%p", msk, ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100557 tp = tcp_sk(ssk);
558 do {
559 u32 map_remaining, offset;
560 u32 seq = tp->copied_seq;
561 struct sk_buff *skb;
562 bool fin;
563
564 /* try to move as much data as available */
565 map_remaining = subflow->map_data_len -
566 mptcp_subflow_get_map_offset(subflow);
567
568 skb = skb_peek(&ssk->sk_receive_queue);
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200569 if (!skb) {
570 /* if no data is found, a racing workqueue/recvmsg
571 * already processed the new data, stop here or we
572 * can enter an infinite loop
573 */
574 if (!moved)
575 done = true;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100576 break;
Paolo Abenid9fb8c52020-10-06 08:27:34 +0200577 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100578
Davide Carattie1ff9e82020-06-29 22:26:20 +0200579 if (__mptcp_check_fallback(msk)) {
580 /* if we are running under the workqueue, TCP could have
581 * collapsed skbs between dummy map creation and now
582 * be sure to adjust the size
583 */
584 map_remaining = skb->len;
585 subflow->map_data_len = skb->len;
586 }
587
Florian Westphal6771bfd2020-02-26 10:14:48 +0100588 offset = seq - TCP_SKB_CB(skb)->seq;
589 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
590 if (fin) {
591 done = true;
592 seq++;
593 }
594
595 if (offset < skb->len) {
596 size_t len = skb->len - offset;
597
598 if (tp->urg_data)
599 done = true;
600
Paolo Abeniab174ad2020-09-14 10:01:12 +0200601 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
602 moved += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100603 seq += len;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100604
605 if (WARN_ON_ONCE(map_remaining < len))
606 break;
607 } else {
608 WARN_ON_ONCE(!fin);
609 sk_eat_skb(ssk, skb);
610 done = true;
611 }
612
613 WRITE_ONCE(tp->copied_seq, seq);
614 more_data_avail = mptcp_subflow_data_available(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100615
Florian Westphal13c7ba02020-11-03 11:05:03 -0800616 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
Florian Westphal600911f2020-02-26 10:14:49 +0100617 done = true;
618 break;
619 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100620 } while (more_data_avail);
Paolo Abeni87952602020-11-27 11:10:24 +0100621 WRITE_ONCE(msk->ack_hint, ssk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100622
Paolo Abeni67193312020-09-14 10:01:09 +0200623 *bytes += moved;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100624 return done;
625}
626
Paolo Abeni87952602020-11-27 11:10:24 +0100627static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
Paolo Abeniab174ad2020-09-14 10:01:12 +0200628{
629 struct sock *sk = (struct sock *)msk;
630 struct sk_buff *skb, *tail;
631 bool moved = false;
632 struct rb_node *p;
633 u64 end_seq;
634
635 p = rb_first(&msk->out_of_order_queue);
Paolo Abeni06242e42020-09-14 10:01:14 +0200636 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
Paolo Abeniab174ad2020-09-14 10:01:12 +0200637 while (p) {
638 skb = rb_to_skb(p);
639 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
640 break;
641
642 p = rb_next(p);
643 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
644
645 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
646 msk->ack_seq))) {
647 mptcp_drop(sk, skb);
Paolo Abeni06242e42020-09-14 10:01:14 +0200648 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200649 continue;
650 }
651
652 end_seq = MPTCP_SKB_CB(skb)->end_seq;
653 tail = skb_peek_tail(&sk->sk_receive_queue);
654 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
655 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
656
657 /* skip overlapping data, if any */
Paolo Abeni06242e42020-09-14 10:01:14 +0200658 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
659 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
660 delta);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200661 MPTCP_SKB_CB(skb)->offset += delta;
662 __skb_queue_tail(&sk->sk_receive_queue, skb);
663 }
664 msk->ack_seq = end_seq;
665 moved = true;
666 }
667 return moved;
668}
669
Florian Westphal2e522132020-02-26 10:14:51 +0100670/* In most cases we will be able to lock the mptcp socket. If its already
671 * owned, we need to defer to the work queue to avoid ABBA deadlock.
672 */
Paolo Abeni99d10552021-06-10 15:59:41 -0700673static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
Florian Westphal2e522132020-02-26 10:14:51 +0100674{
675 struct sock *sk = (struct sock *)msk;
676 unsigned int moved = 0;
677
Paolo Abeni87952602020-11-27 11:10:24 +0100678 if (inet_sk_state_load(sk) == TCP_CLOSE)
Paolo Abeni99d10552021-06-10 15:59:41 -0700679 return false;
Florian Westphal2e522132020-02-26 10:14:51 +0100680
Paolo Abeni87952602020-11-27 11:10:24 +0100681 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
682 __mptcp_ofo_queue(msk);
Paolo Abeniab174ad2020-09-14 10:01:12 +0200683
Paolo Abeni87952602020-11-27 11:10:24 +0100684 /* If the moves have caught up with the DATA_FIN sequence number
685 * it's time to ack the DATA_FIN and change socket state, but
686 * this is not a good place to change state. Let the workqueue
687 * do it.
688 */
689 if (mptcp_pending_data_fin(sk, NULL))
690 mptcp_schedule_work(sk);
Paolo Abeni99d10552021-06-10 15:59:41 -0700691 return moved > 0;
Florian Westphal2e522132020-02-26 10:14:51 +0100692}
693
694void mptcp_data_ready(struct sock *sk, struct sock *ssk)
Florian Westphal101f6f82020-02-26 10:14:46 +0100695{
Paolo Abeni67193312020-09-14 10:01:09 +0200696 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100697 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal13c7ba02020-11-03 11:05:03 -0800698 int sk_rbuf, ssk_rbuf;
Florian Westphal101f6f82020-02-26 10:14:46 +0100699
Paolo Abenid7b1bfd2020-12-09 12:03:31 +0100700 /* The peer can send data while we are shutting down this
701 * subflow at msk destruction time, but we must avoid enqueuing
702 * more data to the msk receive queue
703 */
704 if (unlikely(subflow->disposable))
705 return;
706
Florian Westphal13c7ba02020-11-03 11:05:03 -0800707 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
708 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
709 if (unlikely(ssk_rbuf > sk_rbuf))
710 sk_rbuf = ssk_rbuf;
711
Paolo Abeni99d10552021-06-10 15:59:41 -0700712 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
Florian Westphal13c7ba02020-11-03 11:05:03 -0800713 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf)
Paolo Abeni99d10552021-06-10 15:59:41 -0700714 return;
Florian Westphal2e522132020-02-26 10:14:51 +0100715
Paolo Abeni99d10552021-06-10 15:59:41 -0700716 /* Wake-up the reader only for in-sequence data */
717 mptcp_data_lock(sk);
718 if (move_skbs_to_msk(msk, ssk)) {
719 set_bit(MPTCP_DATA_READY, &msk->flags);
Paolo Abeni67193312020-09-14 10:01:09 +0200720 sk->sk_data_ready(sk);
Paolo Abeni99d10552021-06-10 15:59:41 -0700721 }
722 mptcp_data_unlock(sk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100723}
724
Florian Westphal78962482021-04-15 16:44:53 -0700725static bool mptcp_do_flush_join_list(struct mptcp_sock *msk)
Peter Krystadec3edaa2020-03-27 14:48:40 -0700726{
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100727 struct mptcp_subflow_context *subflow;
Florian Westphaldf00b082021-04-15 16:44:54 -0700728 bool ret = false;
Paolo Abeni5cf92bb2021-01-20 15:39:11 +0100729
Peter Krystadec3edaa2020-03-27 14:48:40 -0700730 if (likely(list_empty(&msk->join_list)))
Florian Westphal78962482021-04-15 16:44:53 -0700731 return false;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700732
733 spin_lock_bh(&msk->join_list_lock);
Florian Westphaldf00b082021-04-15 16:44:54 -0700734 list_for_each_entry(subflow, &msk->join_list, node) {
735 u32 sseq = READ_ONCE(subflow->setsockopt_seq);
Florian Westphal78962482021-04-15 16:44:53 -0700736
Florian Westphaldf00b082021-04-15 16:44:54 -0700737 mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow));
738 if (READ_ONCE(msk->setsockopt_seq) != sseq)
739 ret = true;
740 }
Peter Krystadec3edaa2020-03-27 14:48:40 -0700741 list_splice_tail_init(&msk->join_list, &msk->conn_list);
742 spin_unlock_bh(&msk->join_list_lock);
Florian Westphal78962482021-04-15 16:44:53 -0700743
Florian Westphaldf00b082021-04-15 16:44:54 -0700744 return ret;
Florian Westphal78962482021-04-15 16:44:53 -0700745}
746
747void __mptcp_flush_join_list(struct mptcp_sock *msk)
748{
749 if (likely(!mptcp_do_flush_join_list(msk)))
750 return;
751
752 if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags))
753 mptcp_schedule_work((struct sock *)msk);
754}
755
756static void mptcp_flush_join_list(struct mptcp_sock *msk)
757{
758 bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags);
759
760 might_sleep();
761
762 if (!mptcp_do_flush_join_list(msk) && !sync_needed)
763 return;
764
765 mptcp_sockopt_sync_all(msk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700766}
767
Paolo Abenib51f9b82020-03-27 14:48:44 -0700768static bool mptcp_timer_pending(struct sock *sk)
769{
770 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
771}
772
773static void mptcp_reset_timer(struct sock *sk)
774{
775 struct inet_connection_sock *icsk = inet_csk(sk);
776 unsigned long tout;
777
Paolo Abenie16163b2020-11-16 10:48:09 +0100778 /* prevent rescheduling on close */
779 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
780 return;
781
Paolo Abenib51f9b82020-03-27 14:48:44 -0700782 /* should never be called with mptcp level timer cleared */
783 tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
784 if (WARN_ON_ONCE(!tout))
785 tout = TCP_RTO_MIN;
786 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
787}
788
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100789bool mptcp_schedule_work(struct sock *sk)
790{
791 if (inet_sk_state_load(sk) != TCP_CLOSE &&
792 schedule_work(&mptcp_sk(sk)->work)) {
793 /* each subflow already holds a reference to the sk, and the
794 * workqueue is invoked by a subflow, so sk can't go away here.
795 */
796 sock_hold(sk);
797 return true;
798 }
799 return false;
800}
801
Florian Westphal59832e22020-04-02 13:44:52 +0200802void mptcp_subflow_eof(struct sock *sk)
803{
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100804 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
805 mptcp_schedule_work(sk);
Florian Westphal59832e22020-04-02 13:44:52 +0200806}
807
Paolo Abeni59698562020-06-10 10:47:41 +0200808static void mptcp_check_for_eof(struct mptcp_sock *msk)
809{
810 struct mptcp_subflow_context *subflow;
811 struct sock *sk = (struct sock *)msk;
812 int receivers = 0;
813
814 mptcp_for_each_subflow(msk, subflow)
815 receivers += !subflow->rx_eof;
Paolo Abenie16163b2020-11-16 10:48:09 +0100816 if (receivers)
817 return;
Paolo Abeni59698562020-06-10 10:47:41 +0200818
Paolo Abenie16163b2020-11-16 10:48:09 +0100819 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
Paolo Abeni59698562020-06-10 10:47:41 +0200820 /* hopefully temporary hack: propagate shutdown status
821 * to msk, when all subflows agree on it
822 */
823 sk->sk_shutdown |= RCV_SHUTDOWN;
824
825 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
826 set_bit(MPTCP_DATA_READY, &msk->flags);
827 sk->sk_data_ready(sk);
828 }
Paolo Abenie16163b2020-11-16 10:48:09 +0100829
830 switch (sk->sk_state) {
831 case TCP_ESTABLISHED:
832 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
833 break;
834 case TCP_FIN_WAIT1:
Paolo Abeni26aa2312020-11-19 11:45:55 -0800835 inet_sk_state_store(sk, TCP_CLOSING);
836 break;
837 case TCP_FIN_WAIT2:
Paolo Abenie16163b2020-11-16 10:48:09 +0100838 inet_sk_state_store(sk, TCP_CLOSE);
839 break;
840 default:
841 return;
842 }
843 mptcp_close_wake_up(sk);
Paolo Abeni59698562020-06-10 10:47:41 +0200844}
845
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800846static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
847{
848 struct mptcp_subflow_context *subflow;
849 struct sock *sk = (struct sock *)msk;
850
851 sock_owned_by_me(sk);
852
853 mptcp_for_each_subflow(msk, subflow) {
Paolo Abeni99d10552021-06-10 15:59:41 -0700854 if (READ_ONCE(subflow->data_avail))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800855 return mptcp_subflow_tcp_sock(subflow);
856 }
857
858 return NULL;
859}
860
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700861static bool mptcp_skb_can_collapse_to(u64 write_seq,
862 const struct sk_buff *skb,
863 const struct mptcp_ext *mpext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800864{
Paolo Abeni57040752020-01-21 16:56:27 -0800865 if (!tcp_skb_can_collapse_to(skb))
866 return false;
867
Paolo Abeni5a369ca2020-11-03 11:05:05 -0800868 /* can collapse only if MPTCP level sequence is in order and this
869 * mapping has not been xmitted yet
870 */
871 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
872 !mpext->frozen;
Paolo Abeni57040752020-01-21 16:56:27 -0800873}
874
Paolo Abeni29249ea2021-05-11 19:13:51 +0200875/* we can append data to the given data frag if:
876 * - there is space available in the backing page_frag
877 * - the data frag tail matches the current page_frag free offset
878 * - the data frag end sequence number matches the current write seq
879 */
Paolo Abeni18b683b2020-03-27 14:48:43 -0700880static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
881 const struct page_frag *pfrag,
882 const struct mptcp_data_frag *df)
883{
884 return df && pfrag->page == df->page &&
Paolo Abenid9ca1de2020-11-16 10:48:10 +0100885 pfrag->size - pfrag->offset > 0 &&
Paolo Abeni29249ea2021-05-11 19:13:51 +0200886 pfrag->offset == (df->offset + df->data_len) &&
Paolo Abeni18b683b2020-03-27 14:48:43 -0700887 df->data_seq + df->data_len == msk->write_seq;
888}
889
Paolo Abeni724cfd22020-11-27 11:10:25 +0100890static int mptcp_wmem_with_overhead(struct sock *sk, int size)
Paolo Abenie93da922020-11-27 11:10:23 +0100891{
Paolo Abeni724cfd22020-11-27 11:10:25 +0100892 struct mptcp_sock *msk = mptcp_sk(sk);
893 int ret, skbs;
894
895 ret = size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT);
896 skbs = (msk->tx_pending_data + size) / msk->size_goal_cache;
897 if (skbs < msk->skb_tx_cache.qlen)
898 return ret;
899
900 return ret + (skbs - msk->skb_tx_cache.qlen) * SKB_TRUESIZE(MAX_TCP_HEADER);
Paolo Abenie93da922020-11-27 11:10:23 +0100901}
902
903static void __mptcp_wmem_reserve(struct sock *sk, int size)
904{
Paolo Abeni724cfd22020-11-27 11:10:25 +0100905 int amount = mptcp_wmem_with_overhead(sk, size);
Paolo Abenie93da922020-11-27 11:10:23 +0100906 struct mptcp_sock *msk = mptcp_sk(sk);
907
908 WARN_ON_ONCE(msk->wmem_reserved);
Davide Carattie7579d52020-12-21 22:07:25 +0100909 if (WARN_ON_ONCE(amount < 0))
910 amount = 0;
911
Paolo Abenie93da922020-11-27 11:10:23 +0100912 if (amount <= sk->sk_forward_alloc)
913 goto reserve;
914
915 /* under memory pressure try to reserve at most a single page
916 * otherwise try to reserve the full estimate and fallback
917 * to a single page before entering the error path
918 */
919 if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) ||
920 !sk_wmem_schedule(sk, amount)) {
921 if (amount <= PAGE_SIZE)
922 goto nomem;
923
924 amount = PAGE_SIZE;
925 if (!sk_wmem_schedule(sk, amount))
926 goto nomem;
927 }
928
929reserve:
930 msk->wmem_reserved = amount;
931 sk->sk_forward_alloc -= amount;
932 return;
933
934nomem:
935 /* we will wait for memory on next allocation */
936 msk->wmem_reserved = -1;
937}
938
939static void __mptcp_update_wmem(struct sock *sk)
940{
941 struct mptcp_sock *msk = mptcp_sk(sk);
942
Paolo Abenib5941f02021-05-27 16:31:37 -0700943#ifdef CONFIG_LOCKDEP
944 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock));
945#endif
946
Paolo Abenie93da922020-11-27 11:10:23 +0100947 if (!msk->wmem_reserved)
948 return;
949
950 if (msk->wmem_reserved < 0)
951 msk->wmem_reserved = 0;
952 if (msk->wmem_reserved > 0) {
953 sk->sk_forward_alloc += msk->wmem_reserved;
954 msk->wmem_reserved = 0;
955 }
956}
957
958static bool mptcp_wmem_alloc(struct sock *sk, int size)
959{
960 struct mptcp_sock *msk = mptcp_sk(sk);
961
962 /* check for pre-existing error condition */
963 if (msk->wmem_reserved < 0)
964 return false;
965
966 if (msk->wmem_reserved >= size)
967 goto account;
968
Paolo Abeni87952602020-11-27 11:10:24 +0100969 mptcp_data_lock(sk);
970 if (!sk_wmem_schedule(sk, size)) {
971 mptcp_data_unlock(sk);
Paolo Abenie93da922020-11-27 11:10:23 +0100972 return false;
Paolo Abeni87952602020-11-27 11:10:24 +0100973 }
Paolo Abenie93da922020-11-27 11:10:23 +0100974
975 sk->sk_forward_alloc -= size;
976 msk->wmem_reserved += size;
Paolo Abeni87952602020-11-27 11:10:24 +0100977 mptcp_data_unlock(sk);
Paolo Abenie93da922020-11-27 11:10:23 +0100978
979account:
980 msk->wmem_reserved -= size;
981 return true;
982}
983
Paolo Abeni87952602020-11-27 11:10:24 +0100984static void mptcp_wmem_uncharge(struct sock *sk, int size)
985{
986 struct mptcp_sock *msk = mptcp_sk(sk);
987
988 if (msk->wmem_reserved < 0)
989 msk->wmem_reserved = 0;
990 msk->wmem_reserved += size;
991}
992
Paolo Abeni724cfd22020-11-27 11:10:25 +0100993static void mptcp_mem_reclaim_partial(struct sock *sk)
994{
995 struct mptcp_sock *msk = mptcp_sk(sk);
996
997 /* if we are experiencing a transint allocation error,
998 * the forward allocation memory has been already
999 * released
1000 */
1001 if (msk->wmem_reserved < 0)
1002 return;
1003
1004 mptcp_data_lock(sk);
1005 sk->sk_forward_alloc += msk->wmem_reserved;
1006 sk_mem_reclaim_partial(sk);
1007 msk->wmem_reserved = sk->sk_forward_alloc;
1008 sk->sk_forward_alloc = 0;
1009 mptcp_data_unlock(sk);
1010}
1011
Paolo Abenid0272362020-03-27 14:48:45 -07001012static void dfrag_uncharge(struct sock *sk, int len)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001013{
Paolo Abenid0272362020-03-27 14:48:45 -07001014 sk_mem_uncharge(sk, len);
Florian Westphal7948f6c2020-03-27 14:48:46 -07001015 sk_wmem_queued_add(sk, -len);
Paolo Abenid0272362020-03-27 14:48:45 -07001016}
1017
1018static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
1019{
1020 int len = dfrag->data_len + dfrag->overhead;
1021
Paolo Abeni18b683b2020-03-27 14:48:43 -07001022 list_del(&dfrag->list);
Paolo Abenid0272362020-03-27 14:48:45 -07001023 dfrag_uncharge(sk, len);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001024 put_page(dfrag->page);
1025}
1026
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001027static void __mptcp_clean_una(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001028{
1029 struct mptcp_sock *msk = mptcp_sk(sk);
1030 struct mptcp_data_frag *dtmp, *dfrag;
Paolo Abenid0272362020-03-27 14:48:45 -07001031 bool cleaned = false;
Davide Carattie1ff9e82020-06-29 22:26:20 +02001032 u64 snd_una;
1033
1034 /* on fallback we just need to ignore snd_una, as this is really
1035 * plain TCP
1036 */
1037 if (__mptcp_check_fallback(msk))
Paolo Abeni7439d682020-11-27 11:10:26 +01001038 msk->snd_una = READ_ONCE(msk->snd_nxt);
1039
Paolo Abeni7439d682020-11-27 11:10:26 +01001040 snd_una = msk->snd_una;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001041 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1042 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1043 break;
1044
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001045 if (WARN_ON_ONCE(dfrag == msk->first_pending))
1046 break;
Paolo Abenid0272362020-03-27 14:48:45 -07001047 dfrag_clear(sk, dfrag);
1048 cleaned = true;
1049 }
1050
Florian Westphal7948f6c2020-03-27 14:48:46 -07001051 dfrag = mptcp_rtx_head(sk);
1052 if (dfrag && after64(snd_una, dfrag->data_seq)) {
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001053 u64 delta = snd_una - dfrag->data_seq;
1054
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001055 if (WARN_ON_ONCE(delta > dfrag->already_sent))
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001056 goto out;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001057
1058 dfrag->data_seq += delta;
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001059 dfrag->offset += delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001060 dfrag->data_len -= delta;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001061 dfrag->already_sent -= delta;
Florian Westphal7948f6c2020-03-27 14:48:46 -07001062
1063 dfrag_uncharge(sk, delta);
1064 cleaned = true;
1065 }
1066
Paolo Abeni53eb4c32020-07-23 13:02:30 +02001067out:
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001068 if (cleaned) {
1069 if (tcp_under_memory_pressure(sk)) {
1070 __mptcp_update_wmem(sk);
1071 sk_mem_reclaim_partial(sk);
1072 }
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001073 }
Paolo Abeni63561a42020-09-14 10:01:07 +02001074
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001075 if (snd_una == READ_ONCE(msk->snd_nxt)) {
Mat Martineau6477dd32021-04-23 09:40:33 -07001076 if (msk->timer_ival && !mptcp_data_fin_enabled(msk))
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001077 mptcp_stop_timer(sk);
1078 } else {
1079 mptcp_reset_timer(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001080 }
1081}
1082
Paolo Abeni417789d2021-03-04 13:32:15 -08001083static void __mptcp_clean_una_wakeup(struct sock *sk)
1084{
Paolo Abenib5941f02021-05-27 16:31:37 -07001085#ifdef CONFIG_LOCKDEP
1086 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock));
1087#endif
Paolo Abeni417789d2021-03-04 13:32:15 -08001088 __mptcp_clean_una(sk);
1089 mptcp_write_space(sk);
1090}
1091
Paolo Abenib5941f02021-05-27 16:31:37 -07001092static void mptcp_clean_una_wakeup(struct sock *sk)
1093{
1094 mptcp_data_lock(sk);
1095 __mptcp_clean_una_wakeup(sk);
1096 mptcp_data_unlock(sk);
1097}
1098
Paolo Abeni724cfd22020-11-27 11:10:25 +01001099static void mptcp_enter_memory_pressure(struct sock *sk)
Paolo Abeni18b683b2020-03-27 14:48:43 -07001100{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001101 struct mptcp_subflow_context *subflow;
1102 struct mptcp_sock *msk = mptcp_sk(sk);
1103 bool first = true;
1104
Paolo Abeni18b683b2020-03-27 14:48:43 -07001105 sk_stream_moderate_sndbuf(sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001106 mptcp_for_each_subflow(msk, subflow) {
1107 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1108
1109 if (first)
1110 tcp_enter_memory_pressure(ssk);
1111 sk_stream_moderate_sndbuf(ssk);
1112 first = false;
1113 }
Paolo Abeni724cfd22020-11-27 11:10:25 +01001114}
1115
1116/* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1117 * data
1118 */
1119static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1120{
1121 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1122 pfrag, sk->sk_allocation)))
1123 return true;
1124
1125 mptcp_enter_memory_pressure(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001126 return false;
1127}
1128
1129static struct mptcp_data_frag *
1130mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1131 int orig_offset)
1132{
1133 int offset = ALIGN(orig_offset, sizeof(long));
1134 struct mptcp_data_frag *dfrag;
1135
1136 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1137 dfrag->data_len = 0;
1138 dfrag->data_seq = msk->write_seq;
1139 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1140 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001141 dfrag->already_sent = 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001142 dfrag->page = pfrag->page;
1143
1144 return dfrag;
1145}
1146
Paolo Abenicaf971d2020-11-16 10:48:06 +01001147struct mptcp_sendmsg_info {
1148 int mss_now;
1149 int size_goal;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001150 u16 limit;
1151 u16 sent;
1152 unsigned int flags;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001153};
1154
Florian Westphal6f8a6122020-11-16 10:48:13 +01001155static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1156 int avail_size)
1157{
1158 u64 window_end = mptcp_wnd_end(msk);
1159
1160 if (__mptcp_check_fallback(msk))
1161 return avail_size;
1162
1163 if (!before64(data_seq + avail_size, window_end)) {
1164 u64 allowed_size = window_end - data_seq;
1165
1166 return min_t(unsigned int, allowed_size, avail_size);
1167 }
1168
1169 return avail_size;
1170}
1171
Paolo Abeni724cfd22020-11-27 11:10:25 +01001172static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1173{
1174 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1175
1176 if (!mpext)
1177 return false;
1178 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1179 return true;
1180}
1181
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001182static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001183{
1184 struct sk_buff *skb;
1185
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001186 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001187 if (likely(skb)) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001188 if (likely(__mptcp_add_ext(skb, gfp))) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001189 skb_reserve(skb, MAX_TCP_HEADER);
1190 skb->reserved_tailroom = skb->end - skb->tail;
1191 return skb;
1192 }
1193 __kfree_skb(skb);
1194 } else {
1195 mptcp_enter_memory_pressure(sk);
1196 }
1197 return NULL;
1198}
1199
1200static bool mptcp_tx_cache_refill(struct sock *sk, int size,
1201 struct sk_buff_head *skbs, int *total_ts)
1202{
1203 struct mptcp_sock *msk = mptcp_sk(sk);
1204 struct sk_buff *skb;
1205 int space_needed;
1206
1207 if (unlikely(tcp_under_memory_pressure(sk))) {
1208 mptcp_mem_reclaim_partial(sk);
1209
1210 /* under pressure pre-allocate at most a single skb */
1211 if (msk->skb_tx_cache.qlen)
1212 return true;
1213 space_needed = msk->size_goal_cache;
1214 } else {
1215 space_needed = msk->tx_pending_data + size -
1216 msk->skb_tx_cache.qlen * msk->size_goal_cache;
1217 }
1218
1219 while (space_needed > 0) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001220 skb = __mptcp_do_alloc_tx_skb(sk, sk->sk_allocation);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001221 if (unlikely(!skb)) {
1222 /* under memory pressure, try to pass the caller a
1223 * single skb to allow forward progress
1224 */
1225 while (skbs->qlen > 1) {
1226 skb = __skb_dequeue_tail(skbs);
Paolo Abenieaeef1c2021-03-04 13:32:10 -08001227 *total_ts -= skb->truesize;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001228 __kfree_skb(skb);
1229 }
1230 return skbs->qlen > 0;
1231 }
1232
1233 *total_ts += skb->truesize;
1234 __skb_queue_tail(skbs, skb);
1235 space_needed -= msk->size_goal_cache;
1236 }
1237 return true;
1238}
1239
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001240static bool __mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
Paolo Abeni724cfd22020-11-27 11:10:25 +01001241{
1242 struct mptcp_sock *msk = mptcp_sk(sk);
1243 struct sk_buff *skb;
1244
1245 if (ssk->sk_tx_skb_cache) {
1246 skb = ssk->sk_tx_skb_cache;
1247 if (unlikely(!skb_ext_find(skb, SKB_EXT_MPTCP) &&
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001248 !__mptcp_add_ext(skb, gfp)))
Paolo Abeni724cfd22020-11-27 11:10:25 +01001249 return false;
1250 return true;
1251 }
1252
1253 skb = skb_peek(&msk->skb_tx_cache);
1254 if (skb) {
1255 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1256 skb = __skb_dequeue(&msk->skb_tx_cache);
1257 if (WARN_ON_ONCE(!skb))
1258 return false;
1259
1260 mptcp_wmem_uncharge(sk, skb->truesize);
1261 ssk->sk_tx_skb_cache = skb;
1262 return true;
1263 }
1264
1265 /* over memory limit, no point to try to allocate a new skb */
1266 return false;
1267 }
1268
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001269 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001270 if (!skb)
1271 return false;
1272
1273 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1274 ssk->sk_tx_skb_cache = skb;
1275 return true;
1276 }
1277 kfree_skb(skb);
1278 return false;
1279}
1280
1281static bool mptcp_must_reclaim_memory(struct sock *sk, struct sock *ssk)
1282{
1283 return !ssk->sk_tx_skb_cache &&
1284 !skb_peek(&mptcp_sk(sk)->skb_tx_cache) &&
1285 tcp_under_memory_pressure(sk);
1286}
1287
1288static bool mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk)
1289{
1290 if (unlikely(mptcp_must_reclaim_memory(sk, ssk)))
1291 mptcp_mem_reclaim_partial(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001292 return __mptcp_alloc_tx_skb(sk, ssk, sk->sk_allocation);
Paolo Abeni724cfd22020-11-27 11:10:25 +01001293}
1294
Paolo Abeni57040752020-01-21 16:56:27 -08001295static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001296 struct mptcp_data_frag *dfrag,
Paolo Abenicaf971d2020-11-16 10:48:06 +01001297 struct mptcp_sendmsg_info *info)
Paolo Abeni57040752020-01-21 16:56:27 -08001298{
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001299 u64 data_seq = dfrag->data_seq + info->sent;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001300 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001301 bool zero_window_probe = false;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001302 struct mptcp_ext *mpext = NULL;
Paolo Abeni57040752020-01-21 16:56:27 -08001303 struct sk_buff *skb, *tail;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001304 bool can_collapse = false;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001305 int size_bias = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001306 int avail_size;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001307 size_t ret = 0;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001308
Geliang Tange4b61352021-04-16 15:38:02 -07001309 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001310 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001311
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001312 /* compute send limit */
1313 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
Paolo Abenicaf971d2020-11-16 10:48:06 +01001314 avail_size = info->size_goal;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001315 msk->size_goal_cache = info->size_goal;
Paolo Abeni57040752020-01-21 16:56:27 -08001316 skb = tcp_write_queue_tail(ssk);
1317 if (skb) {
Paolo Abeni57040752020-01-21 16:56:27 -08001318 /* Limit the write to the size available in the
1319 * current skb, if any, so that we create at most a new skb.
1320 * Explicitly tells TCP internals to avoid collapsing on later
1321 * queue management operation, to avoid breaking the ext <->
1322 * SSN association set here
1323 */
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001324 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
Paolo Abenicaf971d2020-11-16 10:48:06 +01001325 can_collapse = (info->size_goal - skb->len > 0) &&
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001326 mptcp_skb_can_collapse_to(data_seq, skb, mpext);
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001327 if (!can_collapse) {
Paolo Abeni57040752020-01-21 16:56:27 -08001328 TCP_SKB_CB(skb)->eor = 1;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001329 } else {
1330 size_bias = skb->len;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001331 avail_size = info->size_goal - skb->len;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001332 }
Paolo Abeni57040752020-01-21 16:56:27 -08001333 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001334
Florian Westphal6f8a6122020-11-16 10:48:13 +01001335 /* Zero window and all data acked? Probe. */
1336 avail_size = mptcp_check_allowed_size(msk, data_seq, avail_size);
1337 if (avail_size == 0) {
Paolo Abeni7439d682020-11-27 11:10:26 +01001338 u64 snd_una = READ_ONCE(msk->snd_una);
1339
1340 if (skb || snd_una != msk->snd_nxt)
Florian Westphal6f8a6122020-11-16 10:48:13 +01001341 return 0;
1342 zero_window_probe = true;
Paolo Abeni7439d682020-11-27 11:10:26 +01001343 data_seq = snd_una - 1;
Florian Westphal6f8a6122020-11-16 10:48:13 +01001344 avail_size = 1;
1345 }
1346
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001347 if (WARN_ON_ONCE(info->sent > info->limit ||
1348 info->limit > dfrag->data_len))
1349 return 0;
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -07001350
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001351 ret = info->limit - info->sent;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001352 tail = tcp_build_frag(ssk, avail_size + size_bias, info->flags,
1353 dfrag->page, dfrag->offset + info->sent, &ret);
Paolo Abenie2223992020-11-16 10:48:03 +01001354 if (!tail) {
1355 tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk));
1356 return -ENOMEM;
Florian Westphal357593832020-08-14 15:56:34 +02001357 }
Paolo Abeni18b683b2020-03-27 14:48:43 -07001358
Paolo Abenie2223992020-11-16 10:48:03 +01001359 /* if the tail skb is still the cached one, collapsing really happened.
Paolo Abeni57040752020-01-21 16:56:27 -08001360 */
Paolo Abenie2223992020-11-16 10:48:03 +01001361 if (skb == tail) {
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001362 TCP_SKB_CB(tail)->tcp_flags &= ~TCPHDR_PSH;
Paolo Abeni57040752020-01-21 16:56:27 -08001363 mpext->data_len += ret;
Paolo Abeni15e6ca972020-12-10 14:25:06 -08001364 WARN_ON_ONCE(!can_collapse);
Florian Westphal6f8a6122020-11-16 10:48:13 +01001365 WARN_ON_ONCE(zero_window_probe);
Paolo Abeni57040752020-01-21 16:56:27 -08001366 goto out;
1367 }
1368
Paolo Abeni724cfd22020-11-27 11:10:25 +01001369 mpext = skb_ext_find(tail, SKB_EXT_MPTCP);
1370 if (WARN_ON_ONCE(!mpext)) {
1371 /* should never reach here, stream corrupted */
1372 return -EINVAL;
1373 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001374
1375 memset(mpext, 0, sizeof(*mpext));
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001376 mpext->data_seq = data_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001377 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1378 mpext->data_len = ret;
1379 mpext->use_map = 1;
1380 mpext->dsn64 = 1;
1381
1382 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1383 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1384 mpext->dsn64);
1385
Florian Westphal6f8a6122020-11-16 10:48:13 +01001386 if (zero_window_probe) {
1387 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
1388 mpext->frozen = 1;
1389 ret = 0;
1390 tcp_push_pending_frames(ssk);
1391 }
Paolo Abeni57040752020-01-21 16:56:27 -08001392out:
Mat Martineau6d0060f2020-01-21 16:56:23 -08001393 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001394 return ret;
1395}
1396
Paolo Abenid5f49192020-09-14 10:01:17 +02001397#define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1398 sizeof(struct tcphdr) - \
1399 MAX_TCP_OPTION_SPACE - \
1400 sizeof(struct ipv6hdr) - \
1401 sizeof(struct frag_hdr))
1402
1403struct subflow_send_info {
1404 struct sock *ssk;
1405 u64 ratio;
1406};
1407
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001408static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
Peter Krystadf2962342020-03-27 14:48:39 -07001409{
Paolo Abenid5f49192020-09-14 10:01:17 +02001410 struct subflow_send_info send_info[2];
Peter Krystadf2962342020-03-27 14:48:39 -07001411 struct mptcp_subflow_context *subflow;
Paolo Abenid5f49192020-09-14 10:01:17 +02001412 int i, nr_active = 0;
1413 struct sock *ssk;
1414 u64 ratio;
1415 u32 pace;
Peter Krystadf2962342020-03-27 14:48:39 -07001416
Paolo Abenid5f49192020-09-14 10:01:17 +02001417 sock_owned_by_me((struct sock *)msk);
Peter Krystadf2962342020-03-27 14:48:39 -07001418
Paolo Abenid5f49192020-09-14 10:01:17 +02001419 if (__mptcp_check_fallback(msk)) {
1420 if (!msk->first)
Peter Krystadf2962342020-03-27 14:48:39 -07001421 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02001422 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001423 }
1424
Paolo Abenid5f49192020-09-14 10:01:17 +02001425 /* re-use last subflow, if the burst allow that */
1426 if (msk->last_snd && msk->snd_burst > 0 &&
1427 sk_stream_memory_free(msk->last_snd) &&
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001428 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd)))
Paolo Abenid5f49192020-09-14 10:01:17 +02001429 return msk->last_snd;
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
1442 nr_active += !subflow->backup;
Paolo Abeniec369c32021-01-20 15:39:12 +01001443 if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd)
Paolo Abenid5f49192020-09-14 10:01:17 +02001444 continue;
1445
1446 pace = READ_ONCE(ssk->sk_pacing_rate);
1447 if (!pace)
1448 continue;
1449
1450 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1451 pace);
1452 if (ratio < send_info[subflow->backup].ratio) {
1453 send_info[subflow->backup].ssk = ssk;
1454 send_info[subflow->backup].ratio = ratio;
1455 }
1456 }
1457
Paolo Abenid5f49192020-09-14 10:01:17 +02001458 /* pick the best backup if no other subflow is active */
1459 if (!nr_active)
1460 send_info[0].ssk = send_info[1].ssk;
1461
1462 if (send_info[0].ssk) {
1463 msk->last_snd = send_info[0].ssk;
1464 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
Paolo Abeniec369c32021-01-20 15:39:12 +01001465 tcp_sk(msk->last_snd)->snd_wnd);
Paolo Abenid5f49192020-09-14 10:01:17 +02001466 return msk->last_snd;
1467 }
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001468
Paolo Abenid5f49192020-09-14 10:01:17 +02001469 return NULL;
Peter Krystadf2962342020-03-27 14:48:39 -07001470}
1471
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001472static void mptcp_push_release(struct sock *sk, struct sock *ssk,
1473 struct mptcp_sendmsg_info *info)
1474{
1475 mptcp_set_timeout(sk, ssk);
1476 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1477 release_sock(ssk);
1478}
1479
Paolo Abenic2e60482021-03-04 13:32:14 -08001480static void __mptcp_push_pending(struct sock *sk, unsigned int flags)
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001481{
1482 struct sock *prev_ssk = NULL, *ssk = NULL;
1483 struct mptcp_sock *msk = mptcp_sk(sk);
1484 struct mptcp_sendmsg_info info = {
1485 .flags = flags,
1486 };
1487 struct mptcp_data_frag *dfrag;
1488 int len, copied = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001489
1490 while ((dfrag = mptcp_send_head(sk))) {
1491 info.sent = dfrag->already_sent;
1492 info.limit = dfrag->data_len;
1493 len = dfrag->data_len - dfrag->already_sent;
1494 while (len > 0) {
1495 int ret = 0;
1496
1497 prev_ssk = ssk;
Florian Westphal78962482021-04-15 16:44:53 -07001498 mptcp_flush_join_list(msk);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001499 ssk = mptcp_subflow_get_send(msk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001500
1501 /* try to keep the subflow socket lock across
1502 * consecutive xmit on the same socket
1503 */
1504 if (ssk != prev_ssk && prev_ssk)
1505 mptcp_push_release(sk, prev_ssk, &info);
1506 if (!ssk)
1507 goto out;
1508
1509 if (ssk != prev_ssk || !prev_ssk)
1510 lock_sock(ssk);
1511
Paolo Abeni724cfd22020-11-27 11:10:25 +01001512 /* keep it simple and always provide a new skb for the
1513 * subflow, even if we will not use it when collapsing
1514 * on the pending one
1515 */
1516 if (!mptcp_alloc_tx_skb(sk, ssk)) {
1517 mptcp_push_release(sk, ssk, &info);
1518 goto out;
1519 }
1520
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001521 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1522 if (ret <= 0) {
1523 mptcp_push_release(sk, ssk, &info);
1524 goto out;
1525 }
1526
1527 info.sent += ret;
1528 dfrag->already_sent += ret;
1529 msk->snd_nxt += ret;
1530 msk->snd_burst -= ret;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001531 msk->tx_pending_data -= ret;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001532 copied += ret;
1533 len -= ret;
1534 }
1535 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1536 }
1537
1538 /* at this point we held the socket lock for the last subflow we used */
1539 if (ssk)
1540 mptcp_push_release(sk, ssk, &info);
1541
1542out:
Paolo Abenib680a212020-11-18 23:05:34 +01001543 if (copied) {
1544 /* start the timer, if it's not pending */
1545 if (!mptcp_timer_pending(sk))
1546 mptcp_reset_timer(sk);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001547 __mptcp_check_send_data_fin(sk);
Paolo Abenib680a212020-11-18 23:05:34 +01001548 }
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001549}
1550
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001551static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1552{
1553 struct mptcp_sock *msk = mptcp_sk(sk);
1554 struct mptcp_sendmsg_info info;
1555 struct mptcp_data_frag *dfrag;
Paolo Abenib19bc292021-01-20 15:39:14 +01001556 struct sock *xmit_ssk;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001557 int len, copied = 0;
Paolo Abenib19bc292021-01-20 15:39:14 +01001558 bool first = true;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001559
1560 info.flags = 0;
1561 while ((dfrag = mptcp_send_head(sk))) {
1562 info.sent = dfrag->already_sent;
1563 info.limit = dfrag->data_len;
1564 len = dfrag->data_len - dfrag->already_sent;
1565 while (len > 0) {
1566 int ret = 0;
1567
Paolo Abenib19bc292021-01-20 15:39:14 +01001568 /* the caller already invoked the packet scheduler,
1569 * check for a different subflow usage only after
1570 * spooling the first chunk of data
1571 */
1572 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1573 if (!xmit_ssk)
1574 goto out;
1575 if (xmit_ssk != ssk) {
1576 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
1577 goto out;
1578 }
1579
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001580 if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) {
1581 __mptcp_update_wmem(sk);
1582 sk_mem_reclaim_partial(sk);
1583 }
1584 if (!__mptcp_alloc_tx_skb(sk, ssk, GFP_ATOMIC))
1585 goto out;
1586
1587 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1588 if (ret <= 0)
1589 goto out;
1590
1591 info.sent += ret;
1592 dfrag->already_sent += ret;
1593 msk->snd_nxt += ret;
1594 msk->snd_burst -= ret;
1595 msk->tx_pending_data -= ret;
1596 copied += ret;
1597 len -= ret;
Paolo Abenib19bc292021-01-20 15:39:14 +01001598 first = false;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001599 }
1600 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1601 }
1602
1603out:
1604 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1605 * not going to flush it via release_sock()
1606 */
1607 __mptcp_update_wmem(sk);
1608 if (copied) {
1609 mptcp_set_timeout(sk, ssk);
1610 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1611 info.size_goal);
Paolo Abenid09d8182021-02-11 15:30:42 -08001612 if (!mptcp_timer_pending(sk))
1613 mptcp_reset_timer(sk);
1614
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001615 if (msk->snd_data_fin_enable &&
1616 msk->snd_nxt + 1 == msk->write_seq)
1617 mptcp_schedule_work(sk);
1618 }
1619}
1620
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001621static void mptcp_set_nospace(struct sock *sk)
1622{
1623 /* enable autotune */
1624 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1625
1626 /* will be cleared on avail space */
1627 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1628}
1629
Mat Martineauf870fa02020-01-21 16:56:15 -08001630static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1631{
1632 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal17091702020-05-16 10:46:21 +02001633 struct page_frag *pfrag;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001634 size_t copied = 0;
Paolo Abenicaf971d2020-11-16 10:48:06 +01001635 int ret = 0;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001636 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08001637
Paolo Abeni987858e2021-04-23 11:17:07 -07001638 /* we don't support FASTOPEN yet */
1639 if (msg->msg_flags & MSG_FASTOPEN)
Mat Martineauf870fa02020-01-21 16:56:15 -08001640 return -EOPNOTSUPP;
1641
Paolo Abeni987858e2021-04-23 11:17:07 -07001642 /* silently ignore everything else */
1643 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1644
Davide Carattie7579d52020-12-21 22:07:25 +01001645 mptcp_lock_sock(sk, __mptcp_wmem_reserve(sk, min_t(size_t, 1 << 20, len)));
Mat Martineau1954b862020-02-28 15:47:39 -08001646
1647 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1648
1649 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1650 ret = sk_stream_wait_connect(sk, &timeo);
1651 if (ret)
1652 goto out;
1653 }
1654
Florian Westphal17091702020-05-16 10:46:21 +02001655 pfrag = sk_page_frag(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001656
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001657 while (msg_data_left(msg)) {
Paolo Abeni724cfd22020-11-27 11:10:25 +01001658 int total_ts, frag_truesize = 0;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001659 struct mptcp_data_frag *dfrag;
Paolo Abeni724cfd22020-11-27 11:10:25 +01001660 struct sk_buff_head skbs;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001661 bool dfrag_collapsed;
1662 size_t psize, offset;
Mat Martineau57baaf22020-07-28 15:12:00 -07001663
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001664 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1665 ret = -EPIPE;
1666 goto out;
Florian Westphalfb529e62020-05-16 10:46:18 +02001667 }
1668
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001669 /* reuse tail pfrag, if possible, or carve a new one from the
1670 * page allocator
1671 */
1672 dfrag = mptcp_pending_tail(sk);
1673 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1674 if (!dfrag_collapsed) {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001675 if (!sk_stream_memory_free(sk))
1676 goto wait_for_memory;
1677
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001678 if (!mptcp_page_frag_refill(sk, pfrag))
1679 goto wait_for_memory;
1680
1681 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1682 frag_truesize = dfrag->overhead;
1683 }
1684
1685 /* we do not bound vs wspace, to allow a single packet.
1686 * memory accounting will prevent execessive memory usage
1687 * anyway
1688 */
1689 offset = dfrag->offset + dfrag->data_len;
1690 psize = pfrag->size - offset;
1691 psize = min_t(size_t, psize, msg_data_left(msg));
Paolo Abeni724cfd22020-11-27 11:10:25 +01001692 total_ts = psize + frag_truesize;
1693 __skb_queue_head_init(&skbs);
1694 if (!mptcp_tx_cache_refill(sk, psize, &skbs, &total_ts))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001695 goto wait_for_memory;
1696
Paolo Abeni724cfd22020-11-27 11:10:25 +01001697 if (!mptcp_wmem_alloc(sk, total_ts)) {
1698 __skb_queue_purge(&skbs);
1699 goto wait_for_memory;
1700 }
1701
1702 skb_queue_splice_tail(&skbs, &msk->skb_tx_cache);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001703 if (copy_page_from_iter(dfrag->page, offset, psize,
1704 &msg->msg_iter) != psize) {
Paolo Abeni87952602020-11-27 11:10:24 +01001705 mptcp_wmem_uncharge(sk, psize + frag_truesize);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001706 ret = -EFAULT;
1707 goto out;
1708 }
1709
1710 /* data successfully copied into the write queue */
1711 copied += psize;
1712 dfrag->data_len += psize;
1713 frag_truesize += psize;
1714 pfrag->offset += frag_truesize;
1715 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
Paolo Abeni13e16032020-12-16 12:48:35 +01001716 msk->tx_pending_data += psize;
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001717
1718 /* charge data on mptcp pending queue to the msk socket
1719 * Note: we charge such data both to sk and ssk
1720 */
1721 sk_wmem_queued_add(sk, frag_truesize);
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001722 if (!dfrag_collapsed) {
1723 get_page(dfrag->page);
1724 list_add_tail(&dfrag->list, &msk->rtx_queue);
1725 if (!msk->first_pending)
1726 WRITE_ONCE(msk->first_pending, dfrag);
1727 }
Geliang Tange4b61352021-04-16 15:38:02 -07001728 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001729 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1730 !dfrag_collapsed);
1731
Paolo Abenid9ca1de2020-11-16 10:48:10 +01001732 continue;
1733
1734wait_for_memory:
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01001735 mptcp_set_nospace(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08001736 __mptcp_push_pending(sk, msg->msg_flags);
Peter Krystadf2962342020-03-27 14:48:39 -07001737 ret = sk_stream_wait_memory(sk, &timeo);
1738 if (ret)
1739 goto out;
Peter Krystadcec37a62020-01-21 16:56:18 -08001740 }
1741
Paolo Abeni13e16032020-12-16 12:48:35 +01001742 if (copied)
Paolo Abenic2e60482021-03-04 13:32:14 -08001743 __mptcp_push_pending(sk, msg->msg_flags);
Paolo Abenida51aef2020-09-14 10:01:10 +02001744
Mat Martineau1954b862020-02-28 15:47:39 -08001745out:
Peter Krystadcec37a62020-01-21 16:56:18 -08001746 release_sock(sk);
Paolo Abeni8555c6b2020-08-03 18:40:39 +02001747 return copied ? : ret;
Mat Martineauf870fa02020-01-21 16:56:15 -08001748}
1749
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001750static void mptcp_wait_data(struct sock *sk, long *timeo)
1751{
1752 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1753 struct mptcp_sock *msk = mptcp_sk(sk);
1754
1755 add_wait_queue(sk_sleep(sk), &wait);
1756 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1757
1758 sk_wait_event(sk, timeo,
1759 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
1760
1761 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1762 remove_wait_queue(sk_sleep(sk), &wait);
1763}
1764
Florian Westphal6771bfd2020-02-26 10:14:48 +01001765static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1766 struct msghdr *msg,
Paolo Abenid9760922021-04-23 11:17:06 -07001767 size_t len, int flags)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001768{
Yonglong Lica4fb8922021-04-23 11:17:08 -07001769 struct sk_buff *skb, *tmp;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001770 int copied = 0;
1771
Yonglong Lica4fb8922021-04-23 11:17:08 -07001772 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
Florian Westphal6771bfd2020-02-26 10:14:48 +01001773 u32 offset = MPTCP_SKB_CB(skb)->offset;
1774 u32 data_len = skb->len - offset;
1775 u32 count = min_t(size_t, len - copied, data_len);
1776 int err;
1777
Paolo Abenid9760922021-04-23 11:17:06 -07001778 if (!(flags & MSG_TRUNC)) {
1779 err = skb_copy_datagram_msg(skb, offset, msg, count);
1780 if (unlikely(err < 0)) {
1781 if (!copied)
1782 return err;
1783 break;
1784 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001785 }
1786
1787 copied += count;
1788
1789 if (count < data_len) {
Yonglong Lica4fb8922021-04-23 11:17:08 -07001790 if (!(flags & MSG_PEEK))
1791 MPTCP_SKB_CB(skb)->offset += count;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001792 break;
1793 }
1794
Yonglong Lica4fb8922021-04-23 11:17:08 -07001795 if (!(flags & MSG_PEEK)) {
1796 /* we will bulk release the skb memory later */
1797 skb->destructor = NULL;
1798 msk->rmem_released += skb->truesize;
1799 __skb_unlink(skb, &msk->receive_queue);
1800 __kfree_skb(skb);
1801 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001802
1803 if (copied >= len)
1804 break;
1805 }
1806
1807 return copied;
1808}
1809
Florian Westphala6b118f2020-06-30 21:24:45 +02001810/* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1811 *
1812 * Only difference: Use highest rtt estimate of the subflows in use.
1813 */
1814static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1815{
1816 struct mptcp_subflow_context *subflow;
1817 struct sock *sk = (struct sock *)msk;
1818 u32 time, advmss = 1;
1819 u64 rtt_us, mstamp;
1820
1821 sock_owned_by_me(sk);
1822
1823 if (copied <= 0)
1824 return;
1825
1826 msk->rcvq_space.copied += copied;
1827
1828 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1829 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1830
1831 rtt_us = msk->rcvq_space.rtt_us;
1832 if (rtt_us && time < (rtt_us >> 3))
1833 return;
1834
1835 rtt_us = 0;
1836 mptcp_for_each_subflow(msk, subflow) {
1837 const struct tcp_sock *tp;
1838 u64 sf_rtt_us;
1839 u32 sf_advmss;
1840
1841 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1842
1843 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1844 sf_advmss = READ_ONCE(tp->advmss);
1845
1846 rtt_us = max(sf_rtt_us, rtt_us);
1847 advmss = max(sf_advmss, advmss);
1848 }
1849
1850 msk->rcvq_space.rtt_us = rtt_us;
1851 if (time < (rtt_us >> 3) || rtt_us == 0)
1852 return;
1853
1854 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1855 goto new_measure;
1856
1857 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1858 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1859 int rcvmem, rcvbuf;
1860 u64 rcvwin, grow;
1861
1862 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1863
1864 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1865
1866 do_div(grow, msk->rcvq_space.space);
1867 rcvwin += (grow << 1);
1868
1869 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1870 while (tcp_win_from_space(sk, rcvmem) < advmss)
1871 rcvmem += 128;
1872
1873 do_div(rcvwin, advmss);
1874 rcvbuf = min_t(u64, rcvwin * rcvmem,
1875 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1876
1877 if (rcvbuf > sk->sk_rcvbuf) {
1878 u32 window_clamp;
1879
1880 window_clamp = tcp_win_from_space(sk, rcvbuf);
1881 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1882
1883 /* Make subflows follow along. If we do not do this, we
1884 * get drops at subflow level if skbs can't be moved to
1885 * the mptcp rx queue fast enough (announced rcv_win can
1886 * exceed ssk->sk_rcvbuf).
1887 */
1888 mptcp_for_each_subflow(msk, subflow) {
1889 struct sock *ssk;
Paolo Abenic76c6952020-09-14 10:01:18 +02001890 bool slow;
Florian Westphala6b118f2020-06-30 21:24:45 +02001891
1892 ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenic76c6952020-09-14 10:01:18 +02001893 slow = lock_sock_fast(ssk);
Florian Westphala6b118f2020-06-30 21:24:45 +02001894 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1895 tcp_sk(ssk)->window_clamp = window_clamp;
Paolo Abenic76c6952020-09-14 10:01:18 +02001896 tcp_cleanup_rbuf(ssk, 1);
1897 unlock_sock_fast(ssk, slow);
Florian Westphala6b118f2020-06-30 21:24:45 +02001898 }
1899 }
1900 }
1901
1902 msk->rcvq_space.space = msk->rcvq_space.copied;
1903new_measure:
1904 msk->rcvq_space.copied = 0;
1905 msk->rcvq_space.time = mstamp;
1906}
1907
Paolo Abeni87952602020-11-27 11:10:24 +01001908static void __mptcp_update_rmem(struct sock *sk)
1909{
1910 struct mptcp_sock *msk = mptcp_sk(sk);
1911
1912 if (!msk->rmem_released)
1913 return;
1914
1915 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
1916 sk_mem_uncharge(sk, msk->rmem_released);
1917 msk->rmem_released = 0;
1918}
1919
1920static void __mptcp_splice_receive_queue(struct sock *sk)
1921{
1922 struct mptcp_sock *msk = mptcp_sk(sk);
1923
1924 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1925}
1926
Paolo Abenie3859602021-02-11 15:30:41 -08001927static bool __mptcp_move_skbs(struct mptcp_sock *msk)
Florian Westphal6771bfd2020-02-26 10:14:48 +01001928{
Paolo Abeni87952602020-11-27 11:10:24 +01001929 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001930 unsigned int moved = 0;
Paolo Abeni87952602020-11-27 11:10:24 +01001931 bool ret, done;
Paolo Abenid5f49192020-09-14 10:01:17 +02001932
Florian Westphal78962482021-04-15 16:44:53 -07001933 mptcp_flush_join_list(msk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001934 do {
1935 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001936 bool slowpath;
Florian Westphal6771bfd2020-02-26 10:14:48 +01001937
Paolo Abeni87952602020-11-27 11:10:24 +01001938 /* we can have data pending in the subflows only if the msk
1939 * receive buffer was full at subflow_data_ready() time,
1940 * that is an unlikely slow path.
1941 */
1942 if (likely(!ssk))
Florian Westphal6771bfd2020-02-26 10:14:48 +01001943 break;
1944
Florian Westphal65f49fe72020-11-03 11:05:04 -08001945 slowpath = lock_sock_fast(ssk);
Paolo Abeni87952602020-11-27 11:10:24 +01001946 mptcp_data_lock(sk);
Paolo Abenie3859602021-02-11 15:30:41 -08001947 __mptcp_update_rmem(sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001948 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
Paolo Abeni87952602020-11-27 11:10:24 +01001949 mptcp_data_unlock(sk);
Paolo Abenie3859602021-02-11 15:30:41 -08001950 tcp_cleanup_rbuf(ssk, moved);
Florian Westphal65f49fe72020-11-03 11:05:04 -08001951 unlock_sock_fast(ssk, slowpath);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001952 } while (!done);
1953
Paolo Abeni87952602020-11-27 11:10:24 +01001954 /* acquire the data lock only if some input data is pending */
1955 ret = moved > 0;
1956 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1957 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
1958 mptcp_data_lock(sk);
1959 __mptcp_update_rmem(sk);
1960 ret |= __mptcp_ofo_queue(msk);
1961 __mptcp_splice_receive_queue(sk);
1962 mptcp_data_unlock(sk);
Paolo Abenie3859602021-02-11 15:30:41 -08001963 mptcp_cleanup_rbuf(msk);
Paolo Abeniab174ad2020-09-14 10:01:12 +02001964 }
Paolo Abeni87952602020-11-27 11:10:24 +01001965 if (ret)
1966 mptcp_check_data_fin((struct sock *)msk);
1967 return !skb_queue_empty(&msk->receive_queue);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001968}
1969
Mat Martineauf870fa02020-01-21 16:56:15 -08001970static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1971 int nonblock, int flags, int *addr_len)
1972{
1973 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08001974 int copied = 0;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001975 int target;
1976 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -08001977
Paolo Abenicb9d80f2021-04-23 11:17:05 -07001978 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
1979 if (unlikely(flags & MSG_ERRQUEUE))
1980 return inet_recv_error(sk, msg, len, addr_len);
1981
Paolo Abeni87952602020-11-27 11:10:24 +01001982 mptcp_lock_sock(sk, __mptcp_splice_receive_queue(sk));
Paolo Abenifd897672020-11-24 22:51:24 +01001983 if (unlikely(sk->sk_state == TCP_LISTEN)) {
1984 copied = -ENOTCONN;
1985 goto out_err;
1986 }
1987
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001988 timeo = sock_rcvtimeo(sk, nonblock);
1989
1990 len = min_t(size_t, len, INT_MAX);
1991 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1992
Eric Dumazet05e3ece2020-12-02 09:16:57 -08001993 while (copied < len) {
Paolo Abenie3859602021-02-11 15:30:41 -08001994 int bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001995
Paolo Abenid9760922021-04-23 11:17:06 -07001996 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001997 if (unlikely(bytes_read < 0)) {
1998 if (!copied)
1999 copied = bytes_read;
2000 goto out_err;
2001 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002002
Florian Westphal6771bfd2020-02-26 10:14:48 +01002003 copied += bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002004
Paolo Abeniea4ca582020-11-19 11:46:03 -08002005 /* be sure to advertise window change */
Paolo Abenie3859602021-02-11 15:30:41 -08002006 mptcp_cleanup_rbuf(msk);
2007
2008 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2009 continue;
Paolo Abeniea4ca582020-11-19 11:46:03 -08002010
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002011 /* only the master socket status is relevant here. The exit
2012 * conditions mirror closely tcp_recvmsg()
2013 */
2014 if (copied >= target)
2015 break;
2016
2017 if (copied) {
2018 if (sk->sk_err ||
2019 sk->sk_state == TCP_CLOSE ||
2020 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2021 !timeo ||
2022 signal_pending(current))
2023 break;
2024 } else {
2025 if (sk->sk_err) {
2026 copied = sock_error(sk);
2027 break;
2028 }
2029
Paolo Abeni59698562020-06-10 10:47:41 +02002030 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2031 mptcp_check_for_eof(msk);
2032
Paolo Abeni87952602020-11-27 11:10:24 +01002033 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2034 /* race breaker: the shutdown could be after the
2035 * previous receive queue check
2036 */
Paolo Abenie3859602021-02-11 15:30:41 -08002037 if (__mptcp_move_skbs(msk))
Paolo Abeni87952602020-11-27 11:10:24 +01002038 continue;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002039 break;
Paolo Abeni87952602020-11-27 11:10:24 +01002040 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002041
2042 if (sk->sk_state == TCP_CLOSE) {
2043 copied = -ENOTCONN;
2044 break;
2045 }
2046
2047 if (!timeo) {
2048 copied = -EAGAIN;
2049 break;
2050 }
2051
2052 if (signal_pending(current)) {
2053 copied = sock_intr_errno(timeo);
2054 break;
2055 }
2056 }
2057
2058 pr_debug("block timeout %ld", timeo);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002059 mptcp_wait_data(sk, &timeo);
Peter Krystadcec37a62020-01-21 16:56:18 -08002060 }
2061
Paolo Abeni87952602020-11-27 11:10:24 +01002062 if (skb_queue_empty_lockless(&sk->sk_receive_queue) &&
2063 skb_queue_empty(&msk->receive_queue)) {
Florian Westphal6771bfd2020-02-26 10:14:48 +01002064 /* entire backlog drained, clear DATA_READY. */
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002065 clear_bit(MPTCP_DATA_READY, &msk->flags);
2066
Florian Westphal6771bfd2020-02-26 10:14:48 +01002067 /* .. race-breaker: ssk might have gotten new data
2068 * after last __mptcp_move_skbs() returned false.
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002069 */
Paolo Abenie3859602021-02-11 15:30:41 -08002070 if (unlikely(__mptcp_move_skbs(msk)))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002071 set_bit(MPTCP_DATA_READY, &msk->flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +01002072 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
2073 /* data to read but mptcp_wait_data() cleared DATA_READY */
2074 set_bit(MPTCP_DATA_READY, &msk->flags);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08002075 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01002076out_err:
Paolo Abeni67193312020-09-14 10:01:09 +02002077 pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d",
2078 msk, test_bit(MPTCP_DATA_READY, &msk->flags),
Paolo Abeni87952602020-11-27 11:10:24 +01002079 skb_queue_empty_lockless(&sk->sk_receive_queue), copied);
Yonglong Lica4fb8922021-04-23 11:17:08 -07002080 if (!(flags & MSG_PEEK))
2081 mptcp_rcv_space_adjust(msk, copied);
Florian Westphala6b118f2020-06-30 21:24:45 +02002082
Peter Krystadcec37a62020-01-21 16:56:18 -08002083 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08002084 return copied;
2085}
2086
Paolo Abenib51f9b82020-03-27 14:48:44 -07002087static void mptcp_retransmit_timer(struct timer_list *t)
2088{
2089 struct inet_connection_sock *icsk = from_timer(icsk, t,
2090 icsk_retransmit_timer);
2091 struct sock *sk = &icsk->icsk_inet.sk;
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002092 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002093
2094 bh_lock_sock(sk);
2095 if (!sock_owned_by_user(sk)) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002096 /* we need a process context to retransmit */
2097 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2098 mptcp_schedule_work(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002099 } else {
2100 /* delegate our work to tcp_release_cb() */
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002101 set_bit(MPTCP_RETRANSMIT, &msk->flags);
Paolo Abenib51f9b82020-03-27 14:48:44 -07002102 }
2103 bh_unlock_sock(sk);
2104 sock_put(sk);
2105}
2106
Paolo Abenie16163b2020-11-16 10:48:09 +01002107static void mptcp_timeout_timer(struct timer_list *t)
2108{
2109 struct sock *sk = from_timer(sk, t, sk_timer);
2110
2111 mptcp_schedule_work(sk);
Florian Westphalb6d69fc2020-11-24 17:24:46 +01002112 sock_put(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002113}
2114
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002115/* Find an idle subflow. Return NULL if there is unacked data at tcp
2116 * level.
2117 *
2118 * A backup subflow is returned only if that is the only kind available.
2119 */
2120static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
2121{
2122 struct mptcp_subflow_context *subflow;
2123 struct sock *backup = NULL;
2124
2125 sock_owned_by_me((const struct sock *)msk);
2126
Paolo Abenid5f49192020-09-14 10:01:17 +02002127 if (__mptcp_check_fallback(msk))
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002128 return NULL;
Paolo Abenid5f49192020-09-14 10:01:17 +02002129
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002130 mptcp_for_each_subflow(msk, subflow) {
2131 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2132
Paolo Abenid5f49192020-09-14 10:01:17 +02002133 if (!mptcp_subflow_active(subflow))
2134 continue;
2135
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002136 /* still data outstanding at TCP level? Don't retransmit. */
Florian Westphal860975c2020-11-19 11:45:56 -08002137 if (!tcp_write_queue_empty(ssk)) {
2138 if (inet_csk(ssk)->icsk_ca_state >= TCP_CA_Loss)
2139 continue;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002140 return NULL;
Florian Westphal860975c2020-11-19 11:45:56 -08002141 }
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002142
2143 if (subflow->backup) {
2144 if (!backup)
2145 backup = ssk;
2146 continue;
2147 }
2148
2149 return ssk;
2150 }
2151
2152 return backup;
2153}
2154
Florian Westphal17aee052021-03-04 13:32:11 -08002155static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2156{
2157 if (msk->subflow) {
2158 iput(SOCK_INODE(msk->subflow));
2159 msk->subflow = NULL;
2160 }
2161}
2162
Peter Krystadcec37a62020-01-21 16:56:18 -08002163/* subflow sockets can be either outgoing (connect) or incoming
2164 * (accept).
2165 *
2166 * Outgoing subflows use in-kernel sockets.
2167 * Incoming subflows do not have their own 'struct socket' allocated,
2168 * so we need to use tcp_close() after detaching them from the mptcp
2169 * parent socket.
2170 */
Florian Westphala141e022021-02-12 15:59:55 -08002171static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2172 struct mptcp_subflow_context *subflow)
Peter Krystadcec37a62020-01-21 16:56:18 -08002173{
Florian Westphale0be4932021-03-04 13:32:08 -08002174 struct mptcp_sock *msk = mptcp_sk(sk);
2175
Peter Krystadcec37a62020-01-21 16:56:18 -08002176 list_del(&subflow->node);
2177
Paolo Abeni3f8b2662020-12-16 12:48:33 +01002178 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
Paolo Abenie16163b2020-11-16 10:48:09 +01002179
2180 /* if we are invoked by the msk cleanup code, the subflow is
2181 * already orphaned
2182 */
Paolo Abeni866f26f2021-01-20 15:39:10 +01002183 if (ssk->sk_socket)
Paolo Abenie16163b2020-11-16 10:48:09 +01002184 sock_orphan(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002185
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002186 subflow->disposable = 1;
2187
Paolo Abenie16163b2020-11-16 10:48:09 +01002188 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2189 * the ssk has been already destroyed, we just need to release the
2190 * reference owned by msk;
2191 */
2192 if (!inet_csk(ssk)->icsk_ulp_ops) {
2193 kfree_rcu(subflow, rcu);
2194 } else {
Paolo Abenid7b1bfd2020-12-09 12:03:31 +01002195 /* otherwise tcp will dispose of the ssk and subflow ctx */
Paolo Abenie16163b2020-11-16 10:48:09 +01002196 __tcp_close(ssk, 0);
2197
2198 /* close acquired an extra ref */
2199 __sock_put(ssk);
2200 }
2201 release_sock(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002202
2203 sock_put(ssk);
Florian Westphale0be4932021-03-04 13:32:08 -08002204
2205 if (ssk == msk->last_snd)
2206 msk->last_snd = NULL;
Florian Westphal17aee052021-03-04 13:32:11 -08002207
Florian Westphalc8fe62f2021-03-04 13:32:12 -08002208 if (ssk == msk->ack_hint)
2209 msk->ack_hint = NULL;
2210
2211 if (ssk == msk->first)
2212 msk->first = NULL;
2213
Florian Westphal17aee052021-03-04 13:32:11 -08002214 if (msk->subflow && ssk == msk->subflow->sk)
2215 mptcp_dispose_initial_subflow(msk);
Mat Martineauf870fa02020-01-21 16:56:15 -08002216}
2217
Florian Westphala141e022021-02-12 15:59:55 -08002218void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2219 struct mptcp_subflow_context *subflow)
2220{
Florian Westphalb911c972021-02-12 16:00:01 -08002221 if (sk->sk_state == TCP_ESTABLISHED)
2222 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
Florian Westphala141e022021-02-12 15:59:55 -08002223 __mptcp_close_ssk(sk, ssk, subflow);
2224}
2225
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002226static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2227{
2228 return 0;
2229}
2230
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002231static void __mptcp_close_subflow(struct mptcp_sock *msk)
2232{
2233 struct mptcp_subflow_context *subflow, *tmp;
2234
Florian Westphal3abc05d2021-02-04 15:23:30 -08002235 might_sleep();
2236
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002237 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2238 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2239
2240 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2241 continue;
2242
Florian Westphal40947e12021-02-12 15:59:56 -08002243 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2244 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2245 continue;
2246
Florian Westphala141e022021-02-12 15:59:55 -08002247 mptcp_close_ssk((struct sock *)msk, ssk, subflow);
Paolo Abeni0e4f35d2020-10-09 19:00:01 +02002248 }
2249}
2250
Paolo Abenie16163b2020-11-16 10:48:09 +01002251static bool mptcp_check_close_timeout(const struct sock *sk)
2252{
2253 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2254 struct mptcp_subflow_context *subflow;
2255
2256 if (delta >= TCP_TIMEWAIT_LEN)
2257 return true;
2258
2259 /* if all subflows are in closed status don't bother with additional
2260 * timeout
2261 */
2262 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2263 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2264 TCP_CLOSE)
2265 return false;
2266 }
2267 return true;
2268}
2269
Florian Westphal50c504a2020-12-10 14:25:04 -08002270static void mptcp_check_fastclose(struct mptcp_sock *msk)
2271{
2272 struct mptcp_subflow_context *subflow, *tmp;
2273 struct sock *sk = &msk->sk.icsk_inet.sk;
2274
2275 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2276 return;
2277
2278 mptcp_token_destroy(msk);
2279
2280 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2281 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2282
2283 lock_sock(tcp_sk);
2284 if (tcp_sk->sk_state != TCP_CLOSE) {
2285 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2286 tcp_set_state(tcp_sk, TCP_CLOSE);
2287 }
2288 release_sock(tcp_sk);
2289 }
2290
2291 inet_sk_state_store(sk, TCP_CLOSE);
2292 sk->sk_shutdown = SHUTDOWN_MASK;
2293 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2294 set_bit(MPTCP_DATA_READY, &msk->flags);
2295 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2296
2297 mptcp_close_wake_up(sk);
2298}
2299
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002300static void __mptcp_retrans(struct sock *sk)
Paolo Abeni80992012020-02-26 10:14:47 +01002301{
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002302 struct mptcp_sock *msk = mptcp_sk(sk);
Paolo Abenicaf971d2020-11-16 10:48:06 +01002303 struct mptcp_sendmsg_info info = {};
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002304 struct mptcp_data_frag *dfrag;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002305 size_t copied = 0;
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002306 struct sock *ssk;
2307 int ret;
2308
Paolo Abenib5941f02021-05-27 16:31:37 -07002309 mptcp_clean_una_wakeup(sk);
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002310 dfrag = mptcp_rtx_head(sk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002311 if (!dfrag) {
2312 if (mptcp_data_fin_enabled(msk)) {
2313 struct inet_connection_sock *icsk = inet_csk(sk);
2314
2315 icsk->icsk_retransmits++;
2316 mptcp_set_datafin_timeout(sk);
2317 mptcp_send_ack(msk);
2318
2319 goto reset_timer;
2320 }
2321
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002322 return;
Mat Martineau6477dd32021-04-23 09:40:33 -07002323 }
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002324
2325 ssk = mptcp_subflow_get_retrans(msk);
2326 if (!ssk)
2327 goto reset_timer;
2328
2329 lock_sock(ssk);
2330
2331 /* limit retransmission to the bytes already sent on some subflows */
2332 info.sent = 0;
2333 info.limit = dfrag->already_sent;
2334 while (info.sent < dfrag->already_sent) {
2335 if (!mptcp_alloc_tx_skb(sk, ssk))
2336 break;
2337
2338 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2339 if (ret <= 0)
2340 break;
2341
2342 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2343 copied += ret;
2344 info.sent += ret;
2345 }
2346 if (copied)
2347 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2348 info.size_goal);
2349
2350 mptcp_set_timeout(sk, ssk);
2351 release_sock(ssk);
2352
2353reset_timer:
2354 if (!mptcp_timer_pending(sk))
2355 mptcp_reset_timer(sk);
2356}
2357
2358static void mptcp_worker(struct work_struct *work)
2359{
2360 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2361 struct sock *sk = &msk->sk.icsk_inet.sk;
2362 int state;
Paolo Abeni80992012020-02-26 10:14:47 +01002363
2364 lock_sock(sk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002365 state = sk->sk_state;
2366 if (unlikely(state == TCP_CLOSE))
2367 goto unlock;
2368
Mat Martineau43b54c62020-07-28 15:12:06 -07002369 mptcp_check_data_fin_ack(sk);
Florian Westphal78962482021-04-15 16:44:53 -07002370 mptcp_flush_join_list(msk);
Florian Westphal50c504a2020-12-10 14:25:04 -08002371
2372 mptcp_check_fastclose(msk);
2373
Florian Westphalb4162682020-07-07 14:40:48 +02002374 if (msk->pm.status)
Florian Westphale9801432021-02-12 15:59:54 -08002375 mptcp_pm_nl_work(msk);
Florian Westphalb4162682020-07-07 14:40:48 +02002376
Florian Westphal59832e22020-04-02 13:44:52 +02002377 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2378 mptcp_check_for_eof(msk);
2379
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002380 __mptcp_check_send_data_fin(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002381 mptcp_check_data_fin(sk);
2382
Paolo Abeni341c6522021-02-19 18:35:37 +01002383 /* There is no point in keeping around an orphaned sk timedout or
2384 * closed, but we need the msk around to reply to incoming DATA_FIN,
2385 * even if it is orphaned and in FIN_WAIT2 state
Paolo Abenie16163b2020-11-16 10:48:09 +01002386 */
2387 if (sock_flag(sk, SOCK_DEAD) &&
Paolo Abeni341c6522021-02-19 18:35:37 +01002388 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002389 inet_sk_state_store(sk, TCP_CLOSE);
2390 __mptcp_destroy_sock(sk);
2391 goto unlock;
2392 }
2393
Florian Westphalb263b0d2021-02-12 15:59:57 -08002394 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2395 __mptcp_close_subflow(msk);
2396
Paolo Abeni2948d0a2021-03-04 13:32:13 -08002397 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2398 __mptcp_retrans(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07002399
2400unlock:
Paolo Abeni80992012020-02-26 10:14:47 +01002401 release_sock(sk);
2402 sock_put(sk);
2403}
2404
Matthieu Baerts784325e2020-01-21 16:56:28 -08002405static int __mptcp_init_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002406{
Peter Krystadcec37a62020-01-21 16:56:18 -08002407 struct mptcp_sock *msk = mptcp_sk(sk);
2408
Peter Krystadec3edaa2020-03-27 14:48:40 -07002409 spin_lock_init(&msk->join_list_lock);
2410
Peter Krystadcec37a62020-01-21 16:56:18 -08002411 INIT_LIST_HEAD(&msk->conn_list);
Peter Krystadec3edaa2020-03-27 14:48:40 -07002412 INIT_LIST_HEAD(&msk->join_list);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002413 INIT_LIST_HEAD(&msk->rtx_queue);
Paolo Abeni80992012020-02-26 10:14:47 +01002414 INIT_WORK(&msk->work, mptcp_worker);
Paolo Abeni87952602020-11-27 11:10:24 +01002415 __skb_queue_head_init(&msk->receive_queue);
Paolo Abeni724cfd22020-11-27 11:10:25 +01002416 __skb_queue_head_init(&msk->skb_tx_cache);
Paolo Abeniab174ad2020-09-14 10:01:12 +02002417 msk->out_of_order_queue = RB_ROOT;
Paolo Abenif0e6a4c2020-11-16 10:48:07 +01002418 msk->first_pending = NULL;
Paolo Abenie93da922020-11-27 11:10:23 +01002419 msk->wmem_reserved = 0;
Paolo Abeni87952602020-11-27 11:10:24 +01002420 msk->rmem_released = 0;
Paolo Abeni724cfd22020-11-27 11:10:25 +01002421 msk->tx_pending_data = 0;
2422 msk->size_goal_cache = TCP_BASE_MSS;
Peter Krystadcec37a62020-01-21 16:56:18 -08002423
Paolo Abeniea4ca582020-11-19 11:46:03 -08002424 msk->ack_hint = NULL;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002425 msk->first = NULL;
Paolo Abenidc24f8b2020-02-26 12:19:03 +01002426 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08002427
Peter Krystad1b1c7a02020-03-27 14:48:38 -07002428 mptcp_pm_data_init(msk);
2429
Paolo Abenib51f9b82020-03-27 14:48:44 -07002430 /* re-use the csk retrans timer for MPTCP-level retrans */
2431 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
Paolo Abenie16163b2020-11-16 10:48:09 +01002432 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002433
Mat Martineauf870fa02020-01-21 16:56:15 -08002434 return 0;
2435}
2436
Matthieu Baerts784325e2020-01-21 16:56:28 -08002437static int mptcp_init_sock(struct sock *sk)
2438{
Paolo Abeni20b57592021-05-25 14:23:10 -07002439 struct inet_connection_sock *icsk = inet_csk(sk);
Florian Westphalfc518952020-03-27 14:48:50 -07002440 struct net *net = sock_net(sk);
2441 int ret;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002442
Geliang Tangb6c08382020-09-24 08:29:54 +08002443 ret = __mptcp_init_sock(sk);
2444 if (ret)
2445 return ret;
2446
Florian Westphalfc518952020-03-27 14:48:50 -07002447 if (!mptcp_is_enabled(net))
2448 return -ENOPROTOOPT;
2449
2450 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2451 return -ENOMEM;
2452
Paolo Abenifa680182020-06-29 22:26:23 +02002453 ret = __mptcp_socket_create(mptcp_sk(sk));
2454 if (ret)
2455 return ret;
2456
Paolo Abeni20b57592021-05-25 14:23:10 -07002457 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2458 * propagate the correct value
2459 */
2460 tcp_assign_congestion_control(sk);
2461 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2462
2463 /* no need to keep a reference to the ops, the name will suffice */
2464 tcp_cleanup_congestion_control(sk);
2465 icsk->icsk_ca_ops = NULL;
2466
Paolo Abenid0272362020-03-27 14:48:45 -07002467 sk_sockets_allocated_inc(sk);
Florian Westphala6b118f2020-06-30 21:24:45 +02002468 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
Paolo Abenida51aef2020-09-14 10:01:10 +02002469 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
Paolo Abenid0272362020-03-27 14:48:45 -07002470
Paolo Abeni18b683b2020-03-27 14:48:43 -07002471 return 0;
2472}
2473
2474static void __mptcp_clear_xmit(struct sock *sk)
2475{
2476 struct mptcp_sock *msk = mptcp_sk(sk);
2477 struct mptcp_data_frag *dtmp, *dfrag;
Paolo Abeni724cfd22020-11-27 11:10:25 +01002478 struct sk_buff *skb;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002479
Paolo Abenid9ca1de2020-11-16 10:48:10 +01002480 WRITE_ONCE(msk->first_pending, NULL);
Paolo Abeni18b683b2020-03-27 14:48:43 -07002481 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
Paolo Abenid0272362020-03-27 14:48:45 -07002482 dfrag_clear(sk, dfrag);
Paolo Abeni724cfd22020-11-27 11:10:25 +01002483 while ((skb = __skb_dequeue(&msk->skb_tx_cache)) != NULL) {
2484 sk->sk_forward_alloc += skb->truesize;
2485 kfree_skb(skb);
2486 }
Matthieu Baerts784325e2020-01-21 16:56:28 -08002487}
2488
Paolo Abeni80992012020-02-26 10:14:47 +01002489static void mptcp_cancel_work(struct sock *sk)
2490{
2491 struct mptcp_sock *msk = mptcp_sk(sk);
2492
Paolo Abenib2771d22020-11-19 11:45:54 -08002493 if (cancel_work_sync(&msk->work))
Paolo Abenie16163b2020-11-16 10:48:09 +01002494 __sock_put(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002495}
2496
Geliang Tangd0876b22020-09-24 08:29:49 +08002497void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
Peter Krystad21498492020-01-21 16:56:21 -08002498{
2499 lock_sock(ssk);
2500
2501 switch (ssk->sk_state) {
2502 case TCP_LISTEN:
2503 if (!(how & RCV_SHUTDOWN))
2504 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002505 fallthrough;
Peter Krystad21498492020-01-21 16:56:21 -08002506 case TCP_SYN_SENT:
2507 tcp_disconnect(ssk, O_NONBLOCK);
2508 break;
2509 default:
Mat Martineau43b54c62020-07-28 15:12:06 -07002510 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2511 pr_debug("Fallback");
2512 ssk->sk_shutdown |= how;
2513 tcp_shutdown(ssk, how);
2514 } else {
2515 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2516 mptcp_set_timeout(sk, ssk);
2517 tcp_send_ack(ssk);
Mat Martineau6477dd32021-04-23 09:40:33 -07002518 if (!mptcp_timer_pending(sk))
2519 mptcp_reset_timer(sk);
Mat Martineau43b54c62020-07-28 15:12:06 -07002520 }
Peter Krystad21498492020-01-21 16:56:21 -08002521 break;
2522 }
2523
Peter Krystad21498492020-01-21 16:56:21 -08002524 release_sock(ssk);
2525}
2526
Mat Martineau6920b852020-07-28 15:12:04 -07002527static const unsigned char new_state[16] = {
2528 /* current state: new state: action: */
2529 [0 /* (Invalid) */] = TCP_CLOSE,
2530 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2531 [TCP_SYN_SENT] = TCP_CLOSE,
2532 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2533 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2534 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2535 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2536 [TCP_CLOSE] = TCP_CLOSE,
2537 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2538 [TCP_LAST_ACK] = TCP_LAST_ACK,
2539 [TCP_LISTEN] = TCP_CLOSE,
2540 [TCP_CLOSING] = TCP_CLOSING,
2541 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2542};
2543
2544static int mptcp_close_state(struct sock *sk)
2545{
2546 int next = (int)new_state[sk->sk_state];
2547 int ns = next & TCP_STATE_MASK;
2548
2549 inet_sk_state_store(sk, ns);
2550
2551 return next & TCP_ACTION_FIN;
2552}
2553
Paolo Abenie16163b2020-11-16 10:48:09 +01002554static void __mptcp_check_send_data_fin(struct sock *sk)
2555{
2556 struct mptcp_subflow_context *subflow;
2557 struct mptcp_sock *msk = mptcp_sk(sk);
2558
2559 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2560 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2561 msk->snd_nxt, msk->write_seq);
2562
2563 /* we still need to enqueue subflows or not really shutting down,
2564 * skip this
2565 */
2566 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2567 mptcp_send_head(sk))
2568 return;
2569
2570 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2571
Paolo Abeni26aa2312020-11-19 11:45:55 -08002572 /* fallback socket will not get data_fin/ack, can move to the next
2573 * state now
2574 */
2575 if (__mptcp_check_fallback(msk)) {
2576 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2577 inet_sk_state_store(sk, TCP_CLOSE);
2578 mptcp_close_wake_up(sk);
2579 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2580 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2581 }
Paolo Abenie16163b2020-11-16 10:48:09 +01002582 }
2583
Florian Westphal78962482021-04-15 16:44:53 -07002584 mptcp_flush_join_list(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002585 mptcp_for_each_subflow(msk, subflow) {
2586 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2587
2588 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2589 }
2590}
2591
2592static void __mptcp_wr_shutdown(struct sock *sk)
2593{
2594 struct mptcp_sock *msk = mptcp_sk(sk);
2595
2596 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2597 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2598 !!mptcp_send_head(sk));
2599
2600 /* will be ignored by fallback sockets */
2601 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2602 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2603
2604 __mptcp_check_send_data_fin(sk);
2605}
2606
2607static void __mptcp_destroy_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08002608{
Peter Krystadcec37a62020-01-21 16:56:18 -08002609 struct mptcp_subflow_context *subflow, *tmp;
Mat Martineauf870fa02020-01-21 16:56:15 -08002610 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002611 LIST_HEAD(conn_list);
Mat Martineauf870fa02020-01-21 16:56:15 -08002612
Paolo Abenie16163b2020-11-16 10:48:09 +01002613 pr_debug("msk=%p", msk);
Florian Westphal2c22c062020-02-04 18:12:30 +01002614
Florian Westphal3abc05d2021-02-04 15:23:30 -08002615 might_sleep();
2616
Paolo Abeni10f6d462020-05-29 17:43:30 +02002617 /* be sure to always acquire the join list lock, to sync vs
2618 * mptcp_finish_join().
2619 */
2620 spin_lock_bh(&msk->join_list_lock);
2621 list_splice_tail_init(&msk->join_list, &msk->conn_list);
2622 spin_unlock_bh(&msk->join_list_lock);
Florian Westphalb2c5b612020-01-29 15:54:45 +01002623 list_splice_init(&msk->conn_list, &conn_list);
2624
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002625 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
Paolo Abenie16163b2020-11-16 10:48:09 +01002626 sk_stop_timer(sk, &sk->sk_timer);
2627 msk->pm.status = 0;
Florian Westphalb2c5b612020-01-29 15:54:45 +01002628
2629 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
Peter Krystadcec37a62020-01-21 16:56:18 -08002630 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002631 __mptcp_close_ssk(sk, ssk, subflow);
Mat Martineauf870fa02020-01-21 16:56:15 -08002632 }
2633
Paolo Abenie16163b2020-11-16 10:48:09 +01002634 sk->sk_prot->destroy(sk);
Paolo Abeni80992012020-02-26 10:14:47 +01002635
Paolo Abenie93da922020-11-27 11:10:23 +01002636 WARN_ON_ONCE(msk->wmem_reserved);
Paolo Abeni87952602020-11-27 11:10:24 +01002637 WARN_ON_ONCE(msk->rmem_released);
Paolo Abenie16163b2020-11-16 10:48:09 +01002638 sk_stream_kill_queues(sk);
2639 xfrm_sk_free_policy(sk);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07002640
Paolo Abenie16163b2020-11-16 10:48:09 +01002641 sk_refcnt_debug_release(sk);
Florian Westphal17aee052021-03-04 13:32:11 -08002642 mptcp_dispose_initial_subflow(msk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002643 sock_put(sk);
2644}
Florian Westphal6771bfd2020-02-26 10:14:48 +01002645
Paolo Abenie16163b2020-11-16 10:48:09 +01002646static void mptcp_close(struct sock *sk, long timeout)
2647{
2648 struct mptcp_subflow_context *subflow;
2649 bool do_cancel_work = false;
2650
2651 lock_sock(sk);
2652 sk->sk_shutdown = SHUTDOWN_MASK;
2653
2654 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2655 inet_sk_state_store(sk, TCP_CLOSE);
2656 goto cleanup;
2657 }
2658
2659 if (mptcp_close_state(sk))
2660 __mptcp_wr_shutdown(sk);
2661
2662 sk_stream_wait_close(sk, timeout);
2663
2664cleanup:
2665 /* orphan all the subflows */
2666 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
Geliang Tang44227912021-04-16 15:38:08 -07002667 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
Paolo Abenie16163b2020-11-16 10:48:09 +01002668 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni866f26f2021-01-20 15:39:10 +01002669 bool slow = lock_sock_fast(ssk);
Paolo Abenie16163b2020-11-16 10:48:09 +01002670
Paolo Abenie16163b2020-11-16 10:48:09 +01002671 sock_orphan(ssk);
2672 unlock_sock_fast(ssk, slow);
Paolo Abenie16163b2020-11-16 10:48:09 +01002673 }
2674 sock_orphan(sk);
2675
2676 sock_hold(sk);
2677 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2678 if (sk->sk_state == TCP_CLOSE) {
2679 __mptcp_destroy_sock(sk);
2680 do_cancel_work = true;
2681 } else {
2682 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2683 }
2684 release_sock(sk);
2685 if (do_cancel_work)
2686 mptcp_cancel_work(sk);
Florian Westphalb911c972021-02-12 16:00:01 -08002687
2688 if (mptcp_sk(sk)->token)
2689 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2690
Paolo Abenie16163b2020-11-16 10:48:09 +01002691 sock_put(sk);
Mat Martineauf870fa02020-01-21 16:56:15 -08002692}
2693
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002694static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2695{
2696#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2697 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2698 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2699
2700 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2701 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2702
2703 if (msk6 && ssk6) {
2704 msk6->saddr = ssk6->saddr;
2705 msk6->flow_label = ssk6->flow_label;
2706 }
2707#endif
2708
2709 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2710 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2711 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2712 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2713 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2714 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2715}
2716
Paolo Abeni18b683b2020-03-27 14:48:43 -07002717static int mptcp_disconnect(struct sock *sk, int flags)
2718{
Paolo Abeni76e2a552021-01-12 18:25:24 +01002719 struct mptcp_subflow_context *subflow;
2720 struct mptcp_sock *msk = mptcp_sk(sk);
2721
Florian Westphal78962482021-04-15 16:44:53 -07002722 mptcp_do_flush_join_list(msk);
2723
Paolo Abeni13a94992021-01-14 16:37:37 +01002724 mptcp_for_each_subflow(msk, subflow) {
2725 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2726
2727 lock_sock(ssk);
2728 tcp_disconnect(ssk, flags);
2729 release_sock(ssk);
2730 }
Florian Westphal42c556f2020-04-29 20:43:20 +02002731 return 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07002732}
2733
Florian Westphalb0519de2020-02-06 00:39:37 +01002734#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2735static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2736{
2737 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2738
2739 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2740}
2741#endif
2742
Paolo Abenifca5c822020-04-20 16:25:06 +02002743struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +02002744 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +02002745 struct request_sock *req)
Florian Westphalb0519de2020-02-06 00:39:37 +01002746{
Paolo Abeni58b09912020-03-13 16:52:41 +01002747 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
Florian Westphalb0519de2020-02-06 00:39:37 +01002748 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
Paolo Abeni58b09912020-03-13 16:52:41 +01002749 struct mptcp_sock *msk;
2750 u64 ack_seq;
Florian Westphalb0519de2020-02-06 00:39:37 +01002751
2752 if (!nsk)
2753 return NULL;
2754
2755#if IS_ENABLED(CONFIG_MPTCP_IPV6)
2756 if (nsk->sk_family == AF_INET6)
2757 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2758#endif
2759
Paolo Abeni58b09912020-03-13 16:52:41 +01002760 __mptcp_init_sock(nsk);
2761
2762 msk = mptcp_sk(nsk);
2763 msk->local_key = subflow_req->local_key;
2764 msk->token = subflow_req->token;
2765 msk->subflow = NULL;
Paolo Abenib93df082020-07-23 13:02:32 +02002766 WRITE_ONCE(msk->fully_established, false);
Paolo Abeni58b09912020-03-13 16:52:41 +01002767
Paolo Abeni58b09912020-03-13 16:52:41 +01002768 msk->write_seq = subflow_req->idsn + 1;
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01002769 msk->snd_nxt = msk->write_seq;
Paolo Abeni7439d682020-11-27 11:10:26 +01002770 msk->snd_una = msk->write_seq;
2771 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
Florian Westphaldf00b082021-04-15 16:44:54 -07002772 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002773
Paolo Abenicfde1412020-04-30 15:01:52 +02002774 if (mp_opt->mp_capable) {
Paolo Abeni58b09912020-03-13 16:52:41 +01002775 msk->can_ack = true;
Paolo Abenicfde1412020-04-30 15:01:52 +02002776 msk->remote_key = mp_opt->sndr_key;
Paolo Abeni58b09912020-03-13 16:52:41 +01002777 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2778 ack_seq++;
Mat Martineau917944d2020-09-29 15:08:19 -07002779 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08002780 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Paolo Abeni58b09912020-03-13 16:52:41 +01002781 }
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002782
Florian Westphal5e200872020-04-20 16:25:04 +02002783 sock_reset_flag(nsk, SOCK_RCU_FREE);
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01002784 /* will be fully established after successful MPC subflow creation */
2785 inet_sk_state_store(nsk, TCP_SYN_RECV);
Paolo Abeni0c148462020-12-16 12:48:32 +01002786
2787 security_inet_csk_clone(nsk, req);
Paolo Abeni58b09912020-03-13 16:52:41 +01002788 bh_unlock_sock(nsk);
2789
2790 /* keep a single reference */
2791 __sock_put(nsk);
Florian Westphalb0519de2020-02-06 00:39:37 +01002792 return nsk;
2793}
2794
Florian Westphala6b118f2020-06-30 21:24:45 +02002795void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2796{
2797 const struct tcp_sock *tp = tcp_sk(ssk);
2798
2799 msk->rcvq_space.copied = 0;
2800 msk->rcvq_space.rtt_us = 0;
2801
2802 msk->rcvq_space.time = tp->tcp_mstamp;
2803
2804 /* initial rcv_space offering made to peer */
2805 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2806 TCP_INIT_CWND * tp->advmss);
2807 if (msk->rcvq_space.space == 0)
2808 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
Florian Westphal6f8a6122020-11-16 10:48:13 +01002809
Paolo Abeni7439d682020-11-27 11:10:26 +01002810 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
Florian Westphala6b118f2020-06-30 21:24:45 +02002811}
2812
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002813static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2814 bool kern)
2815{
2816 struct mptcp_sock *msk = mptcp_sk(sk);
2817 struct socket *listener;
2818 struct sock *newsk;
2819
2820 listener = __mptcp_nmpc_socket(msk);
2821 if (WARN_ON_ONCE(!listener)) {
2822 *err = -EINVAL;
2823 return NULL;
2824 }
2825
2826 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2827 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2828 if (!newsk)
2829 return NULL;
2830
2831 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002832 if (sk_is_mptcp(newsk)) {
2833 struct mptcp_subflow_context *subflow;
2834 struct sock *new_mptcp_sock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002835
2836 subflow = mptcp_subflow_ctx(newsk);
Paolo Abeni58b09912020-03-13 16:52:41 +01002837 new_mptcp_sock = subflow->conn;
2838
2839 /* is_mptcp should be false if subflow->conn is missing, see
2840 * subflow_syn_recv_sock()
2841 */
2842 if (WARN_ON_ONCE(!new_mptcp_sock)) {
2843 tcp_sk(newsk)->is_mptcp = 0;
2844 return newsk;
2845 }
2846
2847 /* acquire the 2nd reference for the owning socket */
2848 sock_hold(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002849 newsk = new_mptcp_sock;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08002850 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
Florian Westphalfc518952020-03-27 14:48:50 -07002851 } else {
2852 MPTCP_INC_STATS(sock_net(sk),
2853 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08002854 }
2855
2856 return newsk;
2857}
2858
Geliang Tang5c8c1642020-09-24 08:29:57 +08002859void mptcp_destroy_common(struct mptcp_sock *msk)
2860{
Paolo Abeni87952602020-11-27 11:10:24 +01002861 struct sock *sk = (struct sock *)msk;
2862
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002863 __mptcp_clear_xmit(sk);
2864
Paolo Abeni87952602020-11-27 11:10:24 +01002865 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
2866 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
2867
Geliang Tang5c8c1642020-09-24 08:29:57 +08002868 skb_rbtree_purge(&msk->out_of_order_queue);
2869 mptcp_token_destroy(msk);
2870 mptcp_pm_free_anno_list(msk);
2871}
2872
Peter Krystad79c09492020-01-21 16:56:20 -08002873static void mptcp_destroy(struct sock *sk)
2874{
Florian Westphalc9fd9c52020-01-29 15:54:43 +01002875 struct mptcp_sock *msk = mptcp_sk(sk);
2876
Geliang Tang5c8c1642020-09-24 08:29:57 +08002877 mptcp_destroy_common(msk);
Paolo Abenid0272362020-03-27 14:48:45 -07002878 sk_sockets_allocated_dec(sk);
Peter Krystad79c09492020-01-21 16:56:20 -08002879}
2880
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002881void __mptcp_data_acked(struct sock *sk)
2882{
2883 if (!sock_owned_by_user(sk))
2884 __mptcp_clean_una(sk);
2885 else
2886 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
2887
2888 if (mptcp_pending_data_fin_ack(sk))
2889 mptcp_schedule_work(sk);
2890}
2891
Paolo Abeni219d0492020-12-16 12:48:34 +01002892void __mptcp_check_push(struct sock *sk, struct sock *ssk)
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002893{
2894 if (!mptcp_send_head(sk))
2895 return;
2896
Paolo Abeni40dc9412021-01-20 15:39:13 +01002897 if (!sock_owned_by_user(sk)) {
Paolo Abenib19bc292021-01-20 15:39:14 +01002898 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
2899
2900 if (xmit_ssk == ssk)
Paolo Abeni40dc9412021-01-20 15:39:13 +01002901 __mptcp_subflow_push_pending(sk, ssk);
Paolo Abenib19bc292021-01-20 15:39:14 +01002902 else if (xmit_ssk)
2903 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
Paolo Abeni40dc9412021-01-20 15:39:13 +01002904 } else {
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002905 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
Paolo Abeni40dc9412021-01-20 15:39:13 +01002906 }
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002907}
2908
Paolo Abenie93da922020-11-27 11:10:23 +01002909/* processes deferred events and flush wmem */
Paolo Abeni14c441b2020-02-26 10:14:52 +01002910static void mptcp_release_cb(struct sock *sk)
2911{
Paolo Abenic2e60482021-03-04 13:32:14 -08002912 for (;;) {
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002913 unsigned long flags = 0;
2914
Paolo Abenic2e60482021-03-04 13:32:14 -08002915 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03002916 flags |= BIT(MPTCP_PUSH_PENDING);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002917 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
2918 flags |= BIT(MPTCP_RETRANSMIT);
Paolo Abenic2e60482021-03-04 13:32:14 -08002919 if (!flags)
2920 break;
2921
2922 /* the following actions acquire the subflow socket lock
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002923 *
2924 * 1) can't be invoked in atomic scope
2925 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
2926 * datapath acquires the msk socket spinlock while helding
2927 * the subflow socket lock
2928 */
2929
2930 spin_unlock_bh(&sk->sk_lock.slock);
Dan Carpenter2e5de7e2021-03-12 10:41:12 +03002931 if (flags & BIT(MPTCP_PUSH_PENDING))
Paolo Abenic2e60482021-03-04 13:32:14 -08002932 __mptcp_push_pending(sk, 0);
Paolo Abeni2d6f5a22021-03-26 11:26:30 -07002933 if (flags & BIT(MPTCP_RETRANSMIT))
2934 __mptcp_retrans(sk);
Paolo Abenic2e60482021-03-04 13:32:14 -08002935
2936 cond_resched();
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002937 spin_lock_bh(&sk->sk_lock.slock);
2938 }
Paolo Abenic2e60482021-03-04 13:32:14 -08002939
2940 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
Paolo Abeni417789d2021-03-04 13:32:15 -08002941 __mptcp_clean_una_wakeup(sk);
Paolo Abeni15cc1042021-02-11 15:30:37 -08002942 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
2943 __mptcp_error_report(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01002944
Paolo Abenic2e60482021-03-04 13:32:14 -08002945 /* push_pending may touch wmem_reserved, ensure we do the cleanup
2946 * later
2947 */
Paolo Abenie93da922020-11-27 11:10:23 +01002948 __mptcp_update_wmem(sk);
Paolo Abeni87952602020-11-27 11:10:24 +01002949 __mptcp_update_rmem(sk);
Paolo Abeni14c441b2020-02-26 10:14:52 +01002950}
2951
Paolo Abenib19bc292021-01-20 15:39:14 +01002952void mptcp_subflow_process_delegated(struct sock *ssk)
2953{
2954 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2955 struct sock *sk = subflow->conn;
2956
2957 mptcp_data_lock(sk);
2958 if (!sock_owned_by_user(sk))
2959 __mptcp_subflow_push_pending(sk, ssk);
2960 else
2961 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
2962 mptcp_data_unlock(sk);
2963 mptcp_subflow_delegated_done(subflow);
2964}
2965
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02002966static int mptcp_hash(struct sock *sk)
2967{
2968 /* should never be called,
2969 * we hash the TCP subflows not the master socket
2970 */
2971 WARN_ON_ONCE(1);
2972 return 0;
2973}
2974
2975static void mptcp_unhash(struct sock *sk)
2976{
2977 /* called from sk_common_release(), but nothing to do here */
2978}
2979
Peter Krystadcec37a62020-01-21 16:56:18 -08002980static int mptcp_get_port(struct sock *sk, unsigned short snum)
Mat Martineauf870fa02020-01-21 16:56:15 -08002981{
2982 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08002983 struct socket *ssock;
Mat Martineauf870fa02020-01-21 16:56:15 -08002984
Peter Krystadcec37a62020-01-21 16:56:18 -08002985 ssock = __mptcp_nmpc_socket(msk);
2986 pr_debug("msk=%p, subflow=%p", msk, ssock);
2987 if (WARN_ON_ONCE(!ssock))
2988 return -EINVAL;
Mat Martineauf870fa02020-01-21 16:56:15 -08002989
Peter Krystadcec37a62020-01-21 16:56:18 -08002990 return inet_csk_get_port(ssock->sk, snum);
2991}
Mat Martineauf870fa02020-01-21 16:56:15 -08002992
Peter Krystadcec37a62020-01-21 16:56:18 -08002993void mptcp_finish_connect(struct sock *ssk)
2994{
2995 struct mptcp_subflow_context *subflow;
2996 struct mptcp_sock *msk;
2997 struct sock *sk;
Mat Martineau6d0060f2020-01-21 16:56:23 -08002998 u64 ack_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -08002999
Peter Krystadcec37a62020-01-21 16:56:18 -08003000 subflow = mptcp_subflow_ctx(ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -08003001 sk = subflow->conn;
3002 msk = mptcp_sk(sk);
3003
Mat Martineau648ef4b2020-01-21 16:56:24 -08003004 pr_debug("msk=%p, token=%u", sk, subflow->token);
3005
Mat Martineau6d0060f2020-01-21 16:56:23 -08003006 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3007 ack_seq++;
Mat Martineau648ef4b2020-01-21 16:56:24 -08003008 subflow->map_seq = ack_seq;
3009 subflow->map_subflow_seq = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -08003010
Peter Krystadcec37a62020-01-21 16:56:18 -08003011 /* the socket is not connected yet, no msk/subflow ops can access/race
3012 * accessing the field below
3013 */
3014 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3015 WRITE_ONCE(msk->local_key, subflow->local_key);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003016 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
Paolo Abenieaa2ffa2020-11-16 10:48:08 +01003017 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
Mat Martineau6d0060f2020-01-21 16:56:23 -08003018 WRITE_ONCE(msk->ack_seq, ack_seq);
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08003019 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
Christoph Paaschd22f4982020-01-21 16:56:32 -08003020 WRITE_ONCE(msk->can_ack, 1);
Paolo Abeni7439d682020-11-27 11:10:26 +01003021 WRITE_ONCE(msk->snd_una, msk->write_seq);
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003022
Florian Westphal6c714f12021-02-12 15:59:58 -08003023 mptcp_pm_new_connection(msk, ssk, 0);
Florian Westphala6b118f2020-06-30 21:24:45 +02003024
3025 mptcp_rcv_space_init(msk, ssk);
Mat Martineauf870fa02020-01-21 16:56:15 -08003026}
3027
Paolo Abeni866f26f2021-01-20 15:39:10 +01003028void mptcp_sock_graft(struct sock *sk, struct socket *parent)
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003029{
3030 write_lock_bh(&sk->sk_callback_lock);
3031 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3032 sk_set_socket(sk, parent);
3033 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3034 write_unlock_bh(&sk->sk_callback_lock);
3035}
3036
Paolo Abenie16163b2020-11-16 10:48:09 +01003037bool mptcp_finish_join(struct sock *ssk)
Peter Krystadf2962342020-03-27 14:48:39 -07003038{
Paolo Abenie16163b2020-11-16 10:48:09 +01003039 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Peter Krystadf2962342020-03-27 14:48:39 -07003040 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3041 struct sock *parent = (void *)msk;
3042 struct socket *parent_sock;
Peter Krystadec3edaa2020-03-27 14:48:40 -07003043 bool ret;
Peter Krystadf2962342020-03-27 14:48:39 -07003044
3045 pr_debug("msk=%p, subflow=%p", msk, subflow);
3046
3047 /* mptcp socket already closing? */
Florian Westphaldc87efd2021-04-01 16:19:44 -07003048 if (!mptcp_is_fully_established(parent)) {
3049 subflow->reset_reason = MPTCP_RST_EMPTCP;
Peter Krystadf2962342020-03-27 14:48:39 -07003050 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003051 }
Peter Krystadf2962342020-03-27 14:48:39 -07003052
3053 if (!msk->pm.server_side)
Florian Westphalb911c972021-02-12 16:00:01 -08003054 goto out;
Peter Krystadf2962342020-03-27 14:48:39 -07003055
Florian Westphaldc87efd2021-04-01 16:19:44 -07003056 if (!mptcp_pm_allow_new_subflow(msk)) {
3057 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003058 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003059 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003060
3061 /* active connections are already on conn_list, and we can't acquire
3062 * msk lock here.
3063 * use the join list lock as synchronization point and double-check
Paolo Abenie16163b2020-11-16 10:48:09 +01003064 * msk status to avoid racing with __mptcp_destroy_sock()
Paolo Abeni10f6d462020-05-29 17:43:30 +02003065 */
3066 spin_lock_bh(&msk->join_list_lock);
3067 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
Paolo Abenie16163b2020-11-16 10:48:09 +01003068 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
Paolo Abeni10f6d462020-05-29 17:43:30 +02003069 list_add_tail(&subflow->node, &msk->join_list);
Paolo Abenie16163b2020-11-16 10:48:09 +01003070 sock_hold(ssk);
3071 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003072 spin_unlock_bh(&msk->join_list_lock);
Florian Westphaldc87efd2021-04-01 16:19:44 -07003073 if (!ret) {
3074 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
Paolo Abeni10f6d462020-05-29 17:43:30 +02003075 return false;
Florian Westphaldc87efd2021-04-01 16:19:44 -07003076 }
Paolo Abeni10f6d462020-05-29 17:43:30 +02003077
3078 /* attach to msk socket only after we are sure he will deal with us
3079 * at close time
3080 */
Peter Krystadf2962342020-03-27 14:48:39 -07003081 parent_sock = READ_ONCE(parent->sk_socket);
Paolo Abenie16163b2020-11-16 10:48:09 +01003082 if (parent_sock && !ssk->sk_socket)
3083 mptcp_sock_graft(ssk, parent_sock);
Mat Martineau917944d2020-09-29 15:08:19 -07003084 subflow->map_seq = READ_ONCE(msk->ack_seq);
Florian Westphalb911c972021-02-12 16:00:01 -08003085out:
3086 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
Paolo Abeni10f6d462020-05-29 17:43:30 +02003087 return true;
Peter Krystadf2962342020-03-27 14:48:39 -07003088}
3089
Paolo Abeni76e2a552021-01-12 18:25:24 +01003090static void mptcp_shutdown(struct sock *sk, int how)
3091{
3092 pr_debug("sk=%p, how=%d", sk, how);
3093
3094 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3095 __mptcp_wr_shutdown(sk);
3096}
3097
Mat Martineauf870fa02020-01-21 16:56:15 -08003098static struct proto mptcp_prot = {
3099 .name = "MPTCP",
3100 .owner = THIS_MODULE,
3101 .init = mptcp_init_sock,
Paolo Abeni18b683b2020-03-27 14:48:43 -07003102 .disconnect = mptcp_disconnect,
Mat Martineauf870fa02020-01-21 16:56:15 -08003103 .close = mptcp_close,
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003104 .accept = mptcp_accept,
Peter Krystad717e79c2020-01-21 16:56:22 -08003105 .setsockopt = mptcp_setsockopt,
3106 .getsockopt = mptcp_getsockopt,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003107 .shutdown = mptcp_shutdown,
Peter Krystad79c09492020-01-21 16:56:20 -08003108 .destroy = mptcp_destroy,
Mat Martineauf870fa02020-01-21 16:56:15 -08003109 .sendmsg = mptcp_sendmsg,
3110 .recvmsg = mptcp_recvmsg,
Paolo Abeni14c441b2020-02-26 10:14:52 +01003111 .release_cb = mptcp_release_cb,
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003112 .hash = mptcp_hash,
3113 .unhash = mptcp_unhash,
Peter Krystadcec37a62020-01-21 16:56:18 -08003114 .get_port = mptcp_get_port,
Paolo Abenid0272362020-03-27 14:48:45 -07003115 .sockets_allocated = &mptcp_sockets_allocated,
3116 .memory_allocated = &tcp_memory_allocated,
3117 .memory_pressure = &tcp_memory_pressure,
Paolo Abenid0272362020-03-27 14:48:45 -07003118 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
Paolo Abeni989ef492020-11-08 19:49:59 +01003119 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
Paolo Abenid0272362020-03-27 14:48:45 -07003120 .sysctl_mem = sysctl_tcp_mem,
Mat Martineauf870fa02020-01-21 16:56:15 -08003121 .obj_size = sizeof(struct mptcp_sock),
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003122 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Mat Martineauf870fa02020-01-21 16:56:15 -08003123 .no_autobind = true,
3124};
3125
Peter Krystad2303f992020-01-21 16:56:17 -08003126static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3127{
3128 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3129 struct socket *ssock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003130 int err;
Peter Krystad2303f992020-01-21 16:56:17 -08003131
3132 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003133 ssock = __mptcp_nmpc_socket(msk);
3134 if (!ssock) {
3135 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003136 goto unlock;
3137 }
3138
3139 err = ssock->ops->bind(ssock, uaddr, addr_len);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003140 if (!err)
3141 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08003142
3143unlock:
3144 release_sock(sock->sk);
3145 return err;
3146}
3147
Paolo Abeni0235d072020-07-23 13:02:31 +02003148static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3149 struct mptcp_subflow_context *subflow)
3150{
3151 subflow->request_mptcp = 0;
3152 __mptcp_do_fallback(msk);
3153}
3154
Peter Krystad2303f992020-01-21 16:56:17 -08003155static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3156 int addr_len, int flags)
3157{
3158 struct mptcp_sock *msk = mptcp_sk(sock->sk);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003159 struct mptcp_subflow_context *subflow;
Peter Krystad2303f992020-01-21 16:56:17 -08003160 struct socket *ssock;
3161 int err;
3162
3163 lock_sock(sock->sk);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003164 if (sock->state != SS_UNCONNECTED && msk->subflow) {
3165 /* pending connection or invalid state, let existing subflow
3166 * cope with that
3167 */
3168 ssock = msk->subflow;
3169 goto do_connect;
3170 }
3171
Paolo Abenifa680182020-06-29 22:26:23 +02003172 ssock = __mptcp_nmpc_socket(msk);
3173 if (!ssock) {
3174 err = -EINVAL;
Peter Krystad2303f992020-01-21 16:56:17 -08003175 goto unlock;
3176 }
3177
Paolo Abenifa680182020-06-29 22:26:23 +02003178 mptcp_token_destroy(msk);
3179 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003180 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003181#ifdef CONFIG_TCP_MD5SIG
3182 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3183 * TCP option space.
3184 */
3185 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
Paolo Abeni0235d072020-07-23 13:02:31 +02003186 mptcp_subflow_early_fallback(msk, subflow);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003187#endif
Paolo Abenia16195e2021-04-01 16:19:41 -07003188 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3189 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
Paolo Abeni0235d072020-07-23 13:02:31 +02003190 mptcp_subflow_early_fallback(msk, subflow);
Paolo Abenia16195e2021-04-01 16:19:41 -07003191 }
Paolo Abeni5695eb82021-04-01 16:19:42 -07003192 if (likely(!__mptcp_check_fallback(msk)))
3193 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003194
Paolo Abeni41be81a2020-05-29 17:43:29 +02003195do_connect:
Peter Krystad2303f992020-01-21 16:56:17 -08003196 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
Paolo Abeni41be81a2020-05-29 17:43:29 +02003197 sock->state = ssock->state;
3198
3199 /* on successful connect, the msk state will be moved to established by
3200 * subflow_finish_connect()
3201 */
Matthieu Baerts367fe042020-07-27 12:24:33 +02003202 if (!err || err == -EINPROGRESS)
Paolo Abeni41be81a2020-05-29 17:43:29 +02003203 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3204 else
3205 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
Peter Krystad2303f992020-01-21 16:56:17 -08003206
3207unlock:
3208 release_sock(sock->sk);
3209 return err;
3210}
3211
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003212static int mptcp_listen(struct socket *sock, int backlog)
3213{
3214 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3215 struct socket *ssock;
3216 int err;
3217
3218 pr_debug("msk=%p", msk);
3219
3220 lock_sock(sock->sk);
Paolo Abenifa680182020-06-29 22:26:23 +02003221 ssock = __mptcp_nmpc_socket(msk);
3222 if (!ssock) {
3223 err = -EINVAL;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003224 goto unlock;
3225 }
3226
Paolo Abenifa680182020-06-29 22:26:23 +02003227 mptcp_token_destroy(msk);
3228 inet_sk_state_store(sock->sk, TCP_LISTEN);
Florian Westphal5e200872020-04-20 16:25:04 +02003229 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3230
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003231 err = ssock->ops->listen(ssock, backlog);
3232 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3233 if (!err)
3234 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3235
3236unlock:
3237 release_sock(sock->sk);
3238 return err;
3239}
3240
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003241static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3242 int flags, bool kern)
3243{
3244 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3245 struct socket *ssock;
3246 int err;
3247
3248 pr_debug("msk=%p", msk);
3249
3250 lock_sock(sock->sk);
3251 if (sock->sk->sk_state != TCP_LISTEN)
3252 goto unlock_fail;
3253
3254 ssock = __mptcp_nmpc_socket(msk);
3255 if (!ssock)
3256 goto unlock_fail;
3257
Paolo Abeni8a056612020-06-29 22:26:25 +02003258 clear_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003259 sock_hold(ssock->sk);
3260 release_sock(sock->sk);
3261
3262 err = ssock->ops->accept(sock, newsock, flags, kern);
Paolo Abenid2f77c52020-06-29 22:26:22 +02003263 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003264 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3265 struct mptcp_subflow_context *subflow;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003266 struct sock *newsk = newsock->sk;
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003267
Florian Westphal4d54cc32021-02-12 15:59:59 -08003268 lock_sock(newsk);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003269
3270 /* PM/worker can now acquire the first subflow socket
3271 * lock without racing with listener queue cleanup,
3272 * we can notify it, if needed.
Florian Westphalc8fe62f2021-03-04 13:32:12 -08003273 *
3274 * Even if remote has reset the initial subflow by now
3275 * the refcnt is still at least one.
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003276 */
3277 subflow = mptcp_subflow_ctx(msk->first);
3278 list_add(&subflow->node, &msk->conn_list);
3279 sock_hold(msk->first);
3280 if (mptcp_is_fully_established(newsk))
Florian Westphal6c714f12021-02-12 15:59:58 -08003281 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
Paolo Abeni5b950ff2020-12-09 12:03:29 +01003282
Paolo Abeni0397c6d2020-11-19 11:45:58 -08003283 mptcp_copy_inaddrs(newsk, msk->first);
3284 mptcp_rcv_space_init(msk, msk->first);
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003285 mptcp_propagate_sndbuf(newsk, msk->first);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003286
3287 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3288 * This is needed so NOSPACE flag can be set from tcp stack.
3289 */
Florian Westphal78962482021-04-15 16:44:53 -07003290 mptcp_flush_join_list(msk);
Geliang Tang190f8b02020-08-03 21:00:44 +08003291 mptcp_for_each_subflow(msk, subflow) {
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003292 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3293
3294 if (!ssk->sk_socket)
3295 mptcp_sock_graft(ssk, newsock);
3296 }
Florian Westphal4d54cc32021-02-12 15:59:59 -08003297 release_sock(newsk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003298 }
3299
Paolo Abeni8a056612020-06-29 22:26:25 +02003300 if (inet_csk_listen_poll(ssock->sk))
3301 set_bit(MPTCP_DATA_READY, &msk->flags);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08003302 sock_put(ssock->sk);
3303 return err;
3304
3305unlock_fail:
3306 release_sock(sock->sk);
3307 return -EINVAL;
3308}
3309
Paolo Abeni8a056612020-06-29 22:26:25 +02003310static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3311{
3312 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
3313 0;
3314}
3315
Florian Westphal8edf0862020-11-16 10:48:12 +01003316static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3317{
3318 struct sock *sk = (struct sock *)msk;
Florian Westphal8edf0862020-11-16 10:48:12 +01003319
3320 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
Paolo Abenidd913412021-02-11 15:30:38 -08003321 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003322
3323 if (sk_stream_is_writeable(sk))
3324 return EPOLLOUT | EPOLLWRNORM;
3325
Paolo Abeni5cf92bb2021-01-20 15:39:11 +01003326 mptcp_set_nospace(sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003327 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3328 if (sk_stream_is_writeable(sk))
3329 return EPOLLOUT | EPOLLWRNORM;
Florian Westphal8edf0862020-11-16 10:48:12 +01003330
Paolo Abeni6e628cd2020-11-27 11:10:27 +01003331 return 0;
Florian Westphal8edf0862020-11-16 10:48:12 +01003332}
3333
Peter Krystad2303f992020-01-21 16:56:17 -08003334static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3335 struct poll_table_struct *wait)
3336{
Florian Westphal1891c4a2020-01-21 16:56:25 -08003337 struct sock *sk = sock->sk;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08003338 struct mptcp_sock *msk;
Peter Krystad2303f992020-01-21 16:56:17 -08003339 __poll_t mask = 0;
Paolo Abeni8a056612020-06-29 22:26:25 +02003340 int state;
Peter Krystad2303f992020-01-21 16:56:17 -08003341
Florian Westphal1891c4a2020-01-21 16:56:25 -08003342 msk = mptcp_sk(sk);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003343 sock_poll_wait(file, sock, wait);
Florian Westphal1891c4a2020-01-21 16:56:25 -08003344
Paolo Abeni8a056612020-06-29 22:26:25 +02003345 state = inet_sk_state_load(sk);
Paolo Abeni67193312020-09-14 10:01:09 +02003346 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
Paolo Abeni8a056612020-06-29 22:26:25 +02003347 if (state == TCP_LISTEN)
3348 return mptcp_check_readable(msk);
3349
3350 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3351 mask |= mptcp_check_readable(msk);
Florian Westphal8edf0862020-11-16 10:48:12 +01003352 mask |= mptcp_check_writeable(msk);
Paolo Abeni8a056612020-06-29 22:26:25 +02003353 }
Paolo Abenidd913412021-02-11 15:30:38 -08003354 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3355 mask |= EPOLLHUP;
Florian Westphal1891c4a2020-01-21 16:56:25 -08003356 if (sk->sk_shutdown & RCV_SHUTDOWN)
3357 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3358
Paolo Abeni15cc1042021-02-11 15:30:37 -08003359 /* This barrier is coupled with smp_wmb() in tcp_reset() */
3360 smp_rmb();
3361 if (sk->sk_err)
3362 mask |= EPOLLERR;
3363
Peter Krystad2303f992020-01-21 16:56:17 -08003364 return mask;
3365}
3366
Florian Westphale42f1ac2020-01-24 16:04:02 -08003367static const struct proto_ops mptcp_stream_ops = {
3368 .family = PF_INET,
3369 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003370 .release = inet_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003371 .bind = mptcp_bind,
3372 .connect = mptcp_stream_connect,
3373 .socketpair = sock_no_socketpair,
3374 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003375 .getname = inet_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003376 .poll = mptcp_poll,
3377 .ioctl = inet_ioctl,
3378 .gettstamp = sock_gettstamp,
3379 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003380 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003381 .setsockopt = sock_common_setsockopt,
3382 .getsockopt = sock_common_getsockopt,
3383 .sendmsg = inet_sendmsg,
3384 .recvmsg = inet_recvmsg,
3385 .mmap = sock_no_mmap,
3386 .sendpage = inet_sendpage,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003387};
Peter Krystad2303f992020-01-21 16:56:17 -08003388
Mat Martineauf870fa02020-01-21 16:56:15 -08003389static struct inet_protosw mptcp_protosw = {
3390 .type = SOCK_STREAM,
3391 .protocol = IPPROTO_MPTCP,
3392 .prot = &mptcp_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003393 .ops = &mptcp_stream_ops,
3394 .flags = INET_PROTOSW_ICSK,
Mat Martineauf870fa02020-01-21 16:56:15 -08003395};
3396
Paolo Abenib19bc292021-01-20 15:39:14 +01003397static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3398{
3399 struct mptcp_delegated_action *delegated;
3400 struct mptcp_subflow_context *subflow;
3401 int work_done = 0;
3402
3403 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3404 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3405 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3406
3407 bh_lock_sock_nested(ssk);
3408 if (!sock_owned_by_user(ssk) &&
3409 mptcp_subflow_has_delegated_action(subflow))
3410 mptcp_subflow_process_delegated(ssk);
3411 /* ... elsewhere tcp_release_cb_override already processed
3412 * the action or will do at next release_sock().
3413 * In both case must dequeue the subflow here - on the same
3414 * CPU that scheduled it.
3415 */
3416 bh_unlock_sock(ssk);
3417 sock_put(ssk);
3418
3419 if (++work_done == budget)
3420 return budget;
3421 }
3422
3423 /* always provide a 0 'work_done' argument, so that napi_complete_done
3424 * will not try accessing the NULL napi->dev ptr
3425 */
3426 napi_complete_done(napi, 0);
3427 return work_done;
3428}
3429
Paolo Abenid39dcec2020-06-26 19:29:59 +02003430void __init mptcp_proto_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003431{
Paolo Abenib19bc292021-01-20 15:39:14 +01003432 struct mptcp_delegated_action *delegated;
3433 int cpu;
3434
Peter Krystad2303f992020-01-21 16:56:17 -08003435 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
Peter Krystad2303f992020-01-21 16:56:17 -08003436
Paolo Abenid0272362020-03-27 14:48:45 -07003437 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3438 panic("Failed to allocate MPTCP pcpu counter\n");
3439
Paolo Abenib19bc292021-01-20 15:39:14 +01003440 init_dummy_netdev(&mptcp_napi_dev);
3441 for_each_possible_cpu(cpu) {
3442 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3443 INIT_LIST_HEAD(&delegated->head);
3444 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3445 NAPI_POLL_WEIGHT);
3446 napi_enable(&delegated->napi);
3447 }
3448
Peter Krystad2303f992020-01-21 16:56:17 -08003449 mptcp_subflow_init();
Peter Krystad1b1c7a02020-03-27 14:48:38 -07003450 mptcp_pm_init();
Paolo Abeni2c5ebd02020-06-26 19:30:00 +02003451 mptcp_token_init();
Peter Krystad2303f992020-01-21 16:56:17 -08003452
Mat Martineauf870fa02020-01-21 16:56:15 -08003453 if (proto_register(&mptcp_prot, 1) != 0)
3454 panic("Failed to register MPTCP proto.\n");
3455
3456 inet_register_protosw(&mptcp_protosw);
Florian Westphal6771bfd2020-02-26 10:14:48 +01003457
3458 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
Mat Martineauf870fa02020-01-21 16:56:15 -08003459}
3460
3461#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Florian Westphale42f1ac2020-01-24 16:04:02 -08003462static const struct proto_ops mptcp_v6_stream_ops = {
3463 .family = PF_INET6,
3464 .owner = THIS_MODULE,
Paolo Abeni0a3cc572021-04-01 18:57:45 +02003465 .release = inet6_release,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003466 .bind = mptcp_bind,
3467 .connect = mptcp_stream_connect,
3468 .socketpair = sock_no_socketpair,
3469 .accept = mptcp_stream_accept,
Paolo Abenid2f77c52020-06-29 22:26:22 +02003470 .getname = inet6_getname,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003471 .poll = mptcp_poll,
3472 .ioctl = inet6_ioctl,
3473 .gettstamp = sock_gettstamp,
3474 .listen = mptcp_listen,
Paolo Abeni76e2a552021-01-12 18:25:24 +01003475 .shutdown = inet_shutdown,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003476 .setsockopt = sock_common_setsockopt,
3477 .getsockopt = sock_common_getsockopt,
3478 .sendmsg = inet6_sendmsg,
3479 .recvmsg = inet6_recvmsg,
3480 .mmap = sock_no_mmap,
3481 .sendpage = inet_sendpage,
3482#ifdef CONFIG_COMPAT
Christoph Hellwig39869122020-05-18 08:28:06 +02003483 .compat_ioctl = inet6_compat_ioctl,
Florian Westphale42f1ac2020-01-24 16:04:02 -08003484#endif
3485};
3486
Mat Martineauf870fa02020-01-21 16:56:15 -08003487static struct proto mptcp_v6_prot;
3488
Peter Krystad79c09492020-01-21 16:56:20 -08003489static void mptcp_v6_destroy(struct sock *sk)
3490{
3491 mptcp_destroy(sk);
3492 inet6_destroy_sock(sk);
3493}
3494
Mat Martineauf870fa02020-01-21 16:56:15 -08003495static struct inet_protosw mptcp_v6_protosw = {
3496 .type = SOCK_STREAM,
3497 .protocol = IPPROTO_MPTCP,
3498 .prot = &mptcp_v6_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08003499 .ops = &mptcp_v6_stream_ops,
Mat Martineauf870fa02020-01-21 16:56:15 -08003500 .flags = INET_PROTOSW_ICSK,
3501};
3502
Paolo Abenid39dcec2020-06-26 19:29:59 +02003503int __init mptcp_proto_v6_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08003504{
3505 int err;
3506
3507 mptcp_v6_prot = mptcp_prot;
3508 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3509 mptcp_v6_prot.slab = NULL;
Peter Krystad79c09492020-01-21 16:56:20 -08003510 mptcp_v6_prot.destroy = mptcp_v6_destroy;
Florian Westphalb0519de2020-02-06 00:39:37 +01003511 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
Mat Martineauf870fa02020-01-21 16:56:15 -08003512
3513 err = proto_register(&mptcp_v6_prot, 1);
3514 if (err)
3515 return err;
3516
Mat Martineauf870fa02020-01-21 16:56:15 -08003517 err = inet6_register_protosw(&mptcp_v6_protosw);
3518 if (err)
3519 proto_unregister(&mptcp_v6_prot);
3520
3521 return err;
3522}
3523#endif