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