blob: b4a8517d8eac7f752accf675aacb144a72b4ff7a [file] [log] [blame]
Mat Martineauf870fa02020-01-21 16:56:15 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/netdevice.h>
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -080012#include <linux/sched/signal.h>
13#include <linux/atomic.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080014#include <net/sock.h>
15#include <net/inet_common.h>
16#include <net/inet_hashtables.h>
17#include <net/protocol.h>
18#include <net/tcp.h>
Peter Krystadcf7da0d2020-01-21 16:56:19 -080019#if IS_ENABLED(CONFIG_MPTCP_IPV6)
20#include <net/transp_v6.h>
21#endif
Mat Martineauf870fa02020-01-21 16:56:15 -080022#include <net/mptcp.h>
23#include "protocol.h"
24
Peter Krystad2303f992020-01-21 16:56:17 -080025#define MPTCP_SAME_STATE TCP_MAX_STATES
26
Florian Westphalb0519de2020-02-06 00:39:37 +010027#if IS_ENABLED(CONFIG_MPTCP_IPV6)
28struct mptcp6_sock {
29 struct mptcp_sock msk;
30 struct ipv6_pinfo np;
31};
32#endif
33
Florian Westphal6771bfd2020-02-26 10:14:48 +010034struct mptcp_skb_cb {
35 u32 offset;
36};
37
38#define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
39
Peter Krystad2303f992020-01-21 16:56:17 -080040/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
41 * completed yet or has failed, return the subflow socket.
42 * Otherwise return NULL.
43 */
44static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
45{
Christoph Paaschd22f4982020-01-21 16:56:32 -080046 if (!msk->subflow || READ_ONCE(msk->can_ack))
Peter Krystad2303f992020-01-21 16:56:17 -080047 return NULL;
48
49 return msk->subflow;
50}
51
Paolo Abeni8ab183d2020-01-21 16:56:33 -080052static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk)
53{
54 return msk->first && !sk_is_mptcp(msk->first);
55}
56
Paolo Abeni8ab183d2020-01-21 16:56:33 -080057static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
Peter Krystadcec37a62020-01-21 16:56:18 -080058{
Peter Krystadcec37a62020-01-21 16:56:18 -080059 sock_owned_by_me((const struct sock *)msk);
60
Paolo Abeni8ab183d2020-01-21 16:56:33 -080061 if (likely(!__mptcp_needs_tcp_fallback(msk)))
Peter Krystadcec37a62020-01-21 16:56:18 -080062 return NULL;
63
Paolo Abeni8ab183d2020-01-21 16:56:33 -080064 if (msk->subflow) {
Florian Westphal2c22c062020-02-04 18:12:30 +010065 release_sock((struct sock *)msk);
66 return msk->subflow;
Paolo Abeni8ab183d2020-01-21 16:56:33 -080067 }
68
Florian Westphal2c22c062020-02-04 18:12:30 +010069 return NULL;
Peter Krystadcec37a62020-01-21 16:56:18 -080070}
71
Peter Krystad2303f992020-01-21 16:56:17 -080072static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
73{
Paolo Abeni8ab183d2020-01-21 16:56:33 -080074 return !msk->first;
Peter Krystad2303f992020-01-21 16:56:17 -080075}
76
77static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
78{
79 struct mptcp_subflow_context *subflow;
80 struct sock *sk = (struct sock *)msk;
81 struct socket *ssock;
82 int err;
83
84 ssock = __mptcp_nmpc_socket(msk);
85 if (ssock)
86 goto set_state;
87
88 if (!__mptcp_can_create_subflow(msk))
89 return ERR_PTR(-EINVAL);
90
91 err = mptcp_subflow_create_socket(sk, &ssock);
92 if (err)
93 return ERR_PTR(err);
94
Paolo Abeni8ab183d2020-01-21 16:56:33 -080095 msk->first = ssock->sk;
Peter Krystad2303f992020-01-21 16:56:17 -080096 msk->subflow = ssock;
97 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcec37a62020-01-21 16:56:18 -080098 list_add(&subflow->node, &msk->conn_list);
Peter Krystad2303f992020-01-21 16:56:17 -080099 subflow->request_mptcp = 1;
100
101set_state:
102 if (state != MPTCP_SAME_STATE)
103 inet_sk_state_store(sk, state);
104 return ssock;
105}
106
Peter Krystadcec37a62020-01-21 16:56:18 -0800107static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
108{
109 struct mptcp_subflow_context *subflow;
110
111 sock_owned_by_me((const struct sock *)msk);
112
113 mptcp_for_each_subflow(msk, subflow) {
114 return mptcp_subflow_tcp_sock(subflow);
115 }
116
117 return NULL;
118}
119
Florian Westphal6771bfd2020-02-26 10:14:48 +0100120static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
121 struct sk_buff *skb,
122 unsigned int offset, size_t copy_len)
123{
124 struct sock *sk = (struct sock *)msk;
125
126 __skb_unlink(skb, &ssk->sk_receive_queue);
127 skb_orphan(skb);
128 __skb_queue_tail(&sk->sk_receive_queue, skb);
129
130 msk->ack_seq += copy_len;
131 MPTCP_SKB_CB(skb)->offset = offset;
132}
133
134static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
135 struct sock *ssk,
136 unsigned int *bytes)
137{
138 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
139 unsigned int moved = 0;
140 bool more_data_avail;
141 struct tcp_sock *tp;
142 bool done = false;
143
144 tp = tcp_sk(ssk);
145 do {
146 u32 map_remaining, offset;
147 u32 seq = tp->copied_seq;
148 struct sk_buff *skb;
149 bool fin;
150
151 /* try to move as much data as available */
152 map_remaining = subflow->map_data_len -
153 mptcp_subflow_get_map_offset(subflow);
154
155 skb = skb_peek(&ssk->sk_receive_queue);
156 if (!skb)
157 break;
158
159 offset = seq - TCP_SKB_CB(skb)->seq;
160 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
161 if (fin) {
162 done = true;
163 seq++;
164 }
165
166 if (offset < skb->len) {
167 size_t len = skb->len - offset;
168
169 if (tp->urg_data)
170 done = true;
171
172 __mptcp_move_skb(msk, ssk, skb, offset, len);
173 seq += len;
174 moved += len;
175
176 if (WARN_ON_ONCE(map_remaining < len))
177 break;
178 } else {
179 WARN_ON_ONCE(!fin);
180 sk_eat_skb(ssk, skb);
181 done = true;
182 }
183
184 WRITE_ONCE(tp->copied_seq, seq);
185 more_data_avail = mptcp_subflow_data_available(ssk);
186 } while (more_data_avail);
187
188 *bytes = moved;
189
190 return done;
191}
192
Florian Westphal101f6f82020-02-26 10:14:46 +0100193void mptcp_data_ready(struct sock *sk)
194{
195 struct mptcp_sock *msk = mptcp_sk(sk);
196
197 set_bit(MPTCP_DATA_READY, &msk->flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100198
199 if (schedule_work(&msk->work))
200 sock_hold((struct sock *)msk);
201
Florian Westphal101f6f82020-02-26 10:14:46 +0100202 sk->sk_data_ready(sk);
203}
204
Mat Martineau6d0060f2020-01-21 16:56:23 -0800205static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
206{
207 if (!msk->cached_ext)
208 msk->cached_ext = __skb_ext_alloc();
209
210 return !!msk->cached_ext;
211}
212
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800213static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
214{
215 struct mptcp_subflow_context *subflow;
216 struct sock *sk = (struct sock *)msk;
217
218 sock_owned_by_me(sk);
219
220 mptcp_for_each_subflow(msk, subflow) {
221 if (subflow->data_avail)
222 return mptcp_subflow_tcp_sock(subflow);
223 }
224
225 return NULL;
226}
227
Paolo Abeni57040752020-01-21 16:56:27 -0800228static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
229 const struct sk_buff *skb,
230 const struct mptcp_ext *mpext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800231{
Paolo Abeni57040752020-01-21 16:56:27 -0800232 if (!tcp_skb_can_collapse_to(skb))
233 return false;
234
235 /* can collapse only if MPTCP level sequence is in order */
236 return mpext && mpext->data_seq + mpext->data_len == msk->write_seq;
237}
238
239static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
240 struct msghdr *msg, long *timeo, int *pmss_now,
241 int *ps_goal)
242{
243 int mss_now, avail_size, size_goal, ret;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800244 struct mptcp_sock *msk = mptcp_sk(sk);
245 struct mptcp_ext *mpext = NULL;
Paolo Abeni57040752020-01-21 16:56:27 -0800246 struct sk_buff *skb, *tail;
247 bool can_collapse = false;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800248 struct page_frag *pfrag;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800249 size_t psize;
250
251 /* use the mptcp page cache so that we can easily move the data
252 * from one substream to another, but do per subflow memory accounting
253 */
254 pfrag = sk_page_frag(sk);
255 while (!sk_page_frag_refill(ssk, pfrag) ||
256 !mptcp_ext_cache_refill(msk)) {
257 ret = sk_stream_wait_memory(ssk, timeo);
258 if (ret)
259 return ret;
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800260 if (unlikely(__mptcp_needs_tcp_fallback(msk)))
261 return 0;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800262 }
263
264 /* compute copy limit */
265 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
Paolo Abeni57040752020-01-21 16:56:27 -0800266 *pmss_now = mss_now;
267 *ps_goal = size_goal;
268 avail_size = size_goal;
269 skb = tcp_write_queue_tail(ssk);
270 if (skb) {
271 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800272
Paolo Abeni57040752020-01-21 16:56:27 -0800273 /* Limit the write to the size available in the
274 * current skb, if any, so that we create at most a new skb.
275 * Explicitly tells TCP internals to avoid collapsing on later
276 * queue management operation, to avoid breaking the ext <->
277 * SSN association set here
278 */
279 can_collapse = (size_goal - skb->len > 0) &&
280 mptcp_skb_can_collapse_to(msk, skb, mpext);
281 if (!can_collapse)
282 TCP_SKB_CB(skb)->eor = 1;
283 else
284 avail_size = size_goal - skb->len;
285 }
286 psize = min_t(size_t, pfrag->size - pfrag->offset, avail_size);
287
288 /* Copy to page */
Mat Martineau6d0060f2020-01-21 16:56:23 -0800289 pr_debug("left=%zu", msg_data_left(msg));
290 psize = copy_page_from_iter(pfrag->page, pfrag->offset,
291 min_t(size_t, msg_data_left(msg), psize),
292 &msg->msg_iter);
293 pr_debug("left=%zu", msg_data_left(msg));
294 if (!psize)
295 return -EINVAL;
296
Paolo Abeni57040752020-01-21 16:56:27 -0800297 /* tell the TCP stack to delay the push so that we can safely
298 * access the skb after the sendpages call
Mat Martineau6d0060f2020-01-21 16:56:23 -0800299 */
Mat Martineau6d0060f2020-01-21 16:56:23 -0800300 ret = do_tcp_sendpages(ssk, pfrag->page, pfrag->offset, psize,
301 msg->msg_flags | MSG_SENDPAGE_NOTLAST);
302 if (ret <= 0)
303 return ret;
304 if (unlikely(ret < psize))
305 iov_iter_revert(&msg->msg_iter, psize - ret);
306
Paolo Abeni57040752020-01-21 16:56:27 -0800307 /* if the tail skb extension is still the cached one, collapsing
308 * really happened. Note: we can't check for 'same skb' as the sk_buff
309 * hdr on tail can be transmitted, freed and re-allocated by the
310 * do_tcp_sendpages() call
311 */
312 tail = tcp_write_queue_tail(ssk);
313 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
314 WARN_ON_ONCE(!can_collapse);
315 mpext->data_len += ret;
316 goto out;
317 }
318
Mat Martineau6d0060f2020-01-21 16:56:23 -0800319 skb = tcp_write_queue_tail(ssk);
320 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
321 msk->cached_ext = NULL;
322
323 memset(mpext, 0, sizeof(*mpext));
324 mpext->data_seq = msk->write_seq;
325 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
326 mpext->data_len = ret;
327 mpext->use_map = 1;
328 mpext->dsn64 = 1;
329
330 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
331 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
332 mpext->dsn64);
333
Paolo Abeni57040752020-01-21 16:56:27 -0800334out:
Mat Martineau6d0060f2020-01-21 16:56:23 -0800335 pfrag->offset += ret;
336 msk->write_seq += ret;
337 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
338
Mat Martineau6d0060f2020-01-21 16:56:23 -0800339 return ret;
340}
341
Florian Westphal1891c4a2020-01-21 16:56:25 -0800342static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk)
343{
344 struct socket *sock;
345
346 if (likely(sk_stream_is_writeable(ssk)))
347 return;
348
349 sock = READ_ONCE(ssk->sk_socket);
350
351 if (sock) {
352 clear_bit(MPTCP_SEND_SPACE, &msk->flags);
353 smp_mb__after_atomic();
354 /* set NOSPACE only after clearing SEND_SPACE flag */
355 set_bit(SOCK_NOSPACE, &sock->flags);
356 }
357}
358
Mat Martineauf870fa02020-01-21 16:56:15 -0800359static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
360{
Paolo Abeni57040752020-01-21 16:56:27 -0800361 int mss_now = 0, size_goal = 0, ret = 0;
Mat Martineauf870fa02020-01-21 16:56:15 -0800362 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800363 struct socket *ssock;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800364 size_t copied = 0;
Peter Krystadcec37a62020-01-21 16:56:18 -0800365 struct sock *ssk;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800366 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -0800367
368 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
369 return -EOPNOTSUPP;
370
Peter Krystadcec37a62020-01-21 16:56:18 -0800371 lock_sock(sk);
372 ssock = __mptcp_tcp_fallback(msk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800373 if (unlikely(ssock)) {
374fallback:
Peter Krystadcec37a62020-01-21 16:56:18 -0800375 pr_debug("fallback passthrough");
376 ret = sock_sendmsg(ssock, msg);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800377 return ret >= 0 ? ret + copied : (copied ? copied : ret);
Peter Krystadcec37a62020-01-21 16:56:18 -0800378 }
379
Mat Martineau6d0060f2020-01-21 16:56:23 -0800380 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
381
Peter Krystadcec37a62020-01-21 16:56:18 -0800382 ssk = mptcp_subflow_get(msk);
383 if (!ssk) {
384 release_sock(sk);
385 return -ENOTCONN;
386 }
387
Mat Martineau6d0060f2020-01-21 16:56:23 -0800388 pr_debug("conn_list->subflow=%p", ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800389
Mat Martineau6d0060f2020-01-21 16:56:23 -0800390 lock_sock(ssk);
391 while (msg_data_left(msg)) {
Paolo Abeni57040752020-01-21 16:56:27 -0800392 ret = mptcp_sendmsg_frag(sk, ssk, msg, &timeo, &mss_now,
393 &size_goal);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800394 if (ret < 0)
395 break;
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800396 if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) {
397 release_sock(ssk);
398 ssock = __mptcp_tcp_fallback(msk);
399 goto fallback;
400 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800401
402 copied += ret;
403 }
404
Paolo Abeni57040752020-01-21 16:56:27 -0800405 if (copied) {
Mat Martineau6d0060f2020-01-21 16:56:23 -0800406 ret = copied;
Paolo Abeni57040752020-01-21 16:56:27 -0800407 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
408 size_goal);
409 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800410
Florian Westphal1891c4a2020-01-21 16:56:25 -0800411 ssk_check_wmem(msk, ssk);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800412 release_sock(ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800413 release_sock(sk);
414 return ret;
Mat Martineauf870fa02020-01-21 16:56:15 -0800415}
416
Mat Martineau648ef4b2020-01-21 16:56:24 -0800417int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
418 unsigned int offset, size_t len)
419{
420 struct mptcp_read_arg *arg = desc->arg.data;
421 size_t copy_len;
422
423 copy_len = min(desc->count, len);
424
425 if (likely(arg->msg)) {
426 int err;
427
428 err = skb_copy_datagram_msg(skb, offset, arg->msg, copy_len);
429 if (err) {
430 pr_debug("error path");
431 desc->error = err;
432 return err;
433 }
434 } else {
435 pr_debug("Flushing skb payload");
436 }
437
438 desc->count -= copy_len;
439
440 pr_debug("consumed %zu bytes, %zu left", copy_len, desc->count);
441 return copy_len;
442}
443
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800444static void mptcp_wait_data(struct sock *sk, long *timeo)
445{
446 DEFINE_WAIT_FUNC(wait, woken_wake_function);
447 struct mptcp_sock *msk = mptcp_sk(sk);
448
449 add_wait_queue(sk_sleep(sk), &wait);
450 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
451
452 sk_wait_event(sk, timeo,
453 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
454
455 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
456 remove_wait_queue(sk_sleep(sk), &wait);
457}
458
Florian Westphal6771bfd2020-02-26 10:14:48 +0100459static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
460 struct msghdr *msg,
461 size_t len)
462{
463 struct sock *sk = (struct sock *)msk;
464 struct sk_buff *skb;
465 int copied = 0;
466
467 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
468 u32 offset = MPTCP_SKB_CB(skb)->offset;
469 u32 data_len = skb->len - offset;
470 u32 count = min_t(size_t, len - copied, data_len);
471 int err;
472
473 err = skb_copy_datagram_msg(skb, offset, msg, count);
474 if (unlikely(err < 0)) {
475 if (!copied)
476 return err;
477 break;
478 }
479
480 copied += count;
481
482 if (count < data_len) {
483 MPTCP_SKB_CB(skb)->offset += count;
484 break;
485 }
486
487 __skb_unlink(skb, &sk->sk_receive_queue);
488 __kfree_skb(skb);
489
490 if (copied >= len)
491 break;
492 }
493
494 return copied;
495}
496
497static bool __mptcp_move_skbs(struct mptcp_sock *msk)
498{
499 unsigned int moved = 0;
500 bool done;
501
502 do {
503 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
504
505 if (!ssk)
506 break;
507
508 lock_sock(ssk);
509 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
510 release_sock(ssk);
511 } while (!done);
512
513 return moved > 0;
514}
515
Mat Martineauf870fa02020-01-21 16:56:15 -0800516static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
517 int nonblock, int flags, int *addr_len)
518{
519 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800520 struct socket *ssock;
Peter Krystadcec37a62020-01-21 16:56:18 -0800521 int copied = 0;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800522 int target;
523 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -0800524
525 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
526 return -EOPNOTSUPP;
527
Peter Krystadcec37a62020-01-21 16:56:18 -0800528 lock_sock(sk);
529 ssock = __mptcp_tcp_fallback(msk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800530 if (unlikely(ssock)) {
531fallback:
Peter Krystadcec37a62020-01-21 16:56:18 -0800532 pr_debug("fallback-read subflow=%p",
533 mptcp_subflow_ctx(ssock->sk));
534 copied = sock_recvmsg(ssock, msg, flags);
Peter Krystadcec37a62020-01-21 16:56:18 -0800535 return copied;
536 }
537
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800538 timeo = sock_rcvtimeo(sk, nonblock);
539
540 len = min_t(size_t, len, INT_MAX);
541 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
542
Florian Westphal6771bfd2020-02-26 10:14:48 +0100543 while (len > (size_t)copied) {
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800544 int bytes_read;
545
Florian Westphal6771bfd2020-02-26 10:14:48 +0100546 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
547 if (unlikely(bytes_read < 0)) {
548 if (!copied)
549 copied = bytes_read;
550 goto out_err;
551 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800552
Florian Westphal6771bfd2020-02-26 10:14:48 +0100553 copied += bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800554
Florian Westphal6771bfd2020-02-26 10:14:48 +0100555 if (skb_queue_empty(&sk->sk_receive_queue) &&
556 __mptcp_move_skbs(msk))
557 continue;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800558
559 /* only the master socket status is relevant here. The exit
560 * conditions mirror closely tcp_recvmsg()
561 */
562 if (copied >= target)
563 break;
564
565 if (copied) {
566 if (sk->sk_err ||
567 sk->sk_state == TCP_CLOSE ||
568 (sk->sk_shutdown & RCV_SHUTDOWN) ||
569 !timeo ||
570 signal_pending(current))
571 break;
572 } else {
573 if (sk->sk_err) {
574 copied = sock_error(sk);
575 break;
576 }
577
578 if (sk->sk_shutdown & RCV_SHUTDOWN)
579 break;
580
581 if (sk->sk_state == TCP_CLOSE) {
582 copied = -ENOTCONN;
583 break;
584 }
585
586 if (!timeo) {
587 copied = -EAGAIN;
588 break;
589 }
590
591 if (signal_pending(current)) {
592 copied = sock_intr_errno(timeo);
593 break;
594 }
595 }
596
597 pr_debug("block timeout %ld", timeo);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800598 mptcp_wait_data(sk, &timeo);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800599 if (unlikely(__mptcp_tcp_fallback(msk)))
600 goto fallback;
Peter Krystadcec37a62020-01-21 16:56:18 -0800601 }
602
Florian Westphal6771bfd2020-02-26 10:14:48 +0100603 if (skb_queue_empty(&sk->sk_receive_queue)) {
604 /* entire backlog drained, clear DATA_READY. */
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800605 clear_bit(MPTCP_DATA_READY, &msk->flags);
606
Florian Westphal6771bfd2020-02-26 10:14:48 +0100607 /* .. race-breaker: ssk might have gotten new data
608 * after last __mptcp_move_skbs() returned false.
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800609 */
Florian Westphal6771bfd2020-02-26 10:14:48 +0100610 if (unlikely(__mptcp_move_skbs(msk)))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800611 set_bit(MPTCP_DATA_READY, &msk->flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100612 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
613 /* data to read but mptcp_wait_data() cleared DATA_READY */
614 set_bit(MPTCP_DATA_READY, &msk->flags);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800615 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100616out_err:
Peter Krystadcec37a62020-01-21 16:56:18 -0800617 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800618 return copied;
619}
620
621/* subflow sockets can be either outgoing (connect) or incoming
622 * (accept).
623 *
624 * Outgoing subflows use in-kernel sockets.
625 * Incoming subflows do not have their own 'struct socket' allocated,
626 * so we need to use tcp_close() after detaching them from the mptcp
627 * parent socket.
628 */
629static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
630 struct mptcp_subflow_context *subflow,
631 long timeout)
632{
633 struct socket *sock = READ_ONCE(ssk->sk_socket);
634
635 list_del(&subflow->node);
636
637 if (sock && sock != sk->sk_socket) {
638 /* outgoing subflow */
639 sock_release(sock);
640 } else {
641 /* incoming subflow */
642 tcp_close(ssk, timeout);
643 }
Mat Martineauf870fa02020-01-21 16:56:15 -0800644}
645
Paolo Abeni80992012020-02-26 10:14:47 +0100646static void mptcp_worker(struct work_struct *work)
647{
648 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
649 struct sock *sk = &msk->sk.icsk_inet.sk;
650
651 lock_sock(sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100652 __mptcp_move_skbs(msk);
Paolo Abeni80992012020-02-26 10:14:47 +0100653 release_sock(sk);
654 sock_put(sk);
655}
656
Matthieu Baerts784325e2020-01-21 16:56:28 -0800657static int __mptcp_init_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -0800658{
Peter Krystadcec37a62020-01-21 16:56:18 -0800659 struct mptcp_sock *msk = mptcp_sk(sk);
660
661 INIT_LIST_HEAD(&msk->conn_list);
Florian Westphal1891c4a2020-01-21 16:56:25 -0800662 __set_bit(MPTCP_SEND_SPACE, &msk->flags);
Paolo Abeni80992012020-02-26 10:14:47 +0100663 INIT_WORK(&msk->work, mptcp_worker);
Peter Krystadcec37a62020-01-21 16:56:18 -0800664
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800665 msk->first = NULL;
666
Mat Martineauf870fa02020-01-21 16:56:15 -0800667 return 0;
668}
669
Matthieu Baerts784325e2020-01-21 16:56:28 -0800670static int mptcp_init_sock(struct sock *sk)
671{
672 if (!mptcp_is_enabled(sock_net(sk)))
673 return -ENOPROTOOPT;
674
675 return __mptcp_init_sock(sk);
676}
677
Paolo Abeni80992012020-02-26 10:14:47 +0100678static void mptcp_cancel_work(struct sock *sk)
679{
680 struct mptcp_sock *msk = mptcp_sk(sk);
681
682 if (cancel_work_sync(&msk->work))
683 sock_put(sk);
684}
685
Peter Krystad21498492020-01-21 16:56:21 -0800686static void mptcp_subflow_shutdown(struct sock *ssk, int how)
687{
688 lock_sock(ssk);
689
690 switch (ssk->sk_state) {
691 case TCP_LISTEN:
692 if (!(how & RCV_SHUTDOWN))
693 break;
694 /* fall through */
695 case TCP_SYN_SENT:
696 tcp_disconnect(ssk, O_NONBLOCK);
697 break;
698 default:
699 ssk->sk_shutdown |= how;
700 tcp_shutdown(ssk, how);
701 break;
702 }
703
704 /* Wake up anyone sleeping in poll. */
705 ssk->sk_state_change(ssk);
706 release_sock(ssk);
707}
708
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800709/* Called with msk lock held, releases such lock before returning */
Florian Westphal2c22c062020-02-04 18:12:30 +0100710static void mptcp_close(struct sock *sk, long timeout)
Mat Martineauf870fa02020-01-21 16:56:15 -0800711{
Peter Krystadcec37a62020-01-21 16:56:18 -0800712 struct mptcp_subflow_context *subflow, *tmp;
Mat Martineauf870fa02020-01-21 16:56:15 -0800713 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb2c5b612020-01-29 15:54:45 +0100714 LIST_HEAD(conn_list);
Mat Martineauf870fa02020-01-21 16:56:15 -0800715
Florian Westphal2c22c062020-02-04 18:12:30 +0100716 lock_sock(sk);
717
Peter Krystad79c09492020-01-21 16:56:20 -0800718 mptcp_token_destroy(msk->token);
Mat Martineauf870fa02020-01-21 16:56:15 -0800719 inet_sk_state_store(sk, TCP_CLOSE);
720
Florian Westphalb2c5b612020-01-29 15:54:45 +0100721 list_splice_init(&msk->conn_list, &conn_list);
722
723 release_sock(sk);
724
725 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
Peter Krystadcec37a62020-01-21 16:56:18 -0800726 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
727
728 __mptcp_close_ssk(sk, ssk, subflow, timeout);
Mat Martineauf870fa02020-01-21 16:56:15 -0800729 }
730
Paolo Abeni80992012020-02-26 10:14:47 +0100731 mptcp_cancel_work(sk);
732
Florian Westphal6771bfd2020-02-26 10:14:48 +0100733 __skb_queue_purge(&sk->sk_receive_queue);
734
Peter Krystadcec37a62020-01-21 16:56:18 -0800735 sk_common_release(sk);
Mat Martineauf870fa02020-01-21 16:56:15 -0800736}
737
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800738static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
739{
740#if IS_ENABLED(CONFIG_MPTCP_IPV6)
741 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
742 struct ipv6_pinfo *msk6 = inet6_sk(msk);
743
744 msk->sk_v6_daddr = ssk->sk_v6_daddr;
745 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
746
747 if (msk6 && ssk6) {
748 msk6->saddr = ssk6->saddr;
749 msk6->flow_label = ssk6->flow_label;
750 }
751#endif
752
753 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
754 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
755 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
756 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
757 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
758 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
759}
760
Florian Westphalb0519de2020-02-06 00:39:37 +0100761#if IS_ENABLED(CONFIG_MPTCP_IPV6)
762static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
763{
764 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
765
766 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
767}
768#endif
769
Chen Wandun5609e2b2020-02-10 16:27:59 +0800770static struct sock *mptcp_sk_clone_lock(const struct sock *sk)
Florian Westphalb0519de2020-02-06 00:39:37 +0100771{
772 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
773
774 if (!nsk)
775 return NULL;
776
777#if IS_ENABLED(CONFIG_MPTCP_IPV6)
778 if (nsk->sk_family == AF_INET6)
779 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
780#endif
781
782 return nsk;
783}
784
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800785static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
786 bool kern)
787{
788 struct mptcp_sock *msk = mptcp_sk(sk);
789 struct socket *listener;
790 struct sock *newsk;
791
792 listener = __mptcp_nmpc_socket(msk);
793 if (WARN_ON_ONCE(!listener)) {
794 *err = -EINVAL;
795 return NULL;
796 }
797
798 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
799 newsk = inet_csk_accept(listener->sk, flags, err, kern);
800 if (!newsk)
801 return NULL;
802
803 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
804
805 if (sk_is_mptcp(newsk)) {
806 struct mptcp_subflow_context *subflow;
807 struct sock *new_mptcp_sock;
808 struct sock *ssk = newsk;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800809 u64 ack_seq;
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800810
811 subflow = mptcp_subflow_ctx(newsk);
812 lock_sock(sk);
813
814 local_bh_disable();
Florian Westphalb0519de2020-02-06 00:39:37 +0100815 new_mptcp_sock = mptcp_sk_clone_lock(sk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800816 if (!new_mptcp_sock) {
817 *err = -ENOBUFS;
818 local_bh_enable();
819 release_sock(sk);
Peter Krystad21498492020-01-21 16:56:21 -0800820 mptcp_subflow_shutdown(newsk, SHUT_RDWR + 1);
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800821 tcp_close(newsk, 0);
822 return NULL;
823 }
824
Matthieu Baerts784325e2020-01-21 16:56:28 -0800825 __mptcp_init_sock(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800826
827 msk = mptcp_sk(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800828 msk->local_key = subflow->local_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800829 msk->token = subflow->token;
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800830 msk->subflow = NULL;
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800831 msk->first = newsk;
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800832
Peter Krystad79c09492020-01-21 16:56:20 -0800833 mptcp_token_update_accept(newsk, new_mptcp_sock);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800834
Mat Martineau6d0060f2020-01-21 16:56:23 -0800835 msk->write_seq = subflow->idsn + 1;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800836 if (subflow->can_ack) {
837 msk->can_ack = true;
838 msk->remote_key = subflow->remote_key;
839 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
840 ack_seq++;
841 msk->ack_seq = ack_seq;
842 }
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800843 newsk = new_mptcp_sock;
844 mptcp_copy_inaddrs(newsk, ssk);
845 list_add(&subflow->node, &msk->conn_list);
846
847 /* will be fully established at mptcp_stream_accept()
848 * completion.
849 */
850 inet_sk_state_store(new_mptcp_sock, TCP_SYN_RECV);
851 bh_unlock_sock(new_mptcp_sock);
852 local_bh_enable();
853 release_sock(sk);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800854
855 /* the subflow can already receive packet, avoid racing with
856 * the receive path and process the pending ones
857 */
858 lock_sock(ssk);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800859 subflow->rel_write_seq = 1;
860 subflow->tcp_sock = ssk;
861 subflow->conn = new_mptcp_sock;
862 if (unlikely(!skb_queue_empty(&ssk->sk_receive_queue)))
863 mptcp_subflow_data_available(ssk);
864 release_sock(ssk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800865 }
866
867 return newsk;
868}
869
Peter Krystad79c09492020-01-21 16:56:20 -0800870static void mptcp_destroy(struct sock *sk)
871{
Florian Westphalc9fd9c52020-01-29 15:54:43 +0100872 struct mptcp_sock *msk = mptcp_sk(sk);
873
874 if (msk->cached_ext)
875 __skb_ext_put(msk->cached_ext);
Peter Krystad79c09492020-01-21 16:56:20 -0800876}
877
Peter Krystad717e79c2020-01-21 16:56:22 -0800878static int mptcp_setsockopt(struct sock *sk, int level, int optname,
Florian Westphal50e741b2020-01-29 15:54:44 +0100879 char __user *optval, unsigned int optlen)
Peter Krystad717e79c2020-01-21 16:56:22 -0800880{
881 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystad717e79c2020-01-21 16:56:22 -0800882 struct socket *ssock;
Peter Krystad717e79c2020-01-21 16:56:22 -0800883
884 pr_debug("msk=%p", msk);
885
886 /* @@ the meaning of setsockopt() when the socket is connected and
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800887 * there are multiple subflows is not yet defined. It is up to the
888 * MPTCP-level socket to configure the subflows until the subflow
889 * is in TCP fallback, when TCP socket options are passed through
890 * to the one remaining subflow.
Peter Krystad717e79c2020-01-21 16:56:22 -0800891 */
892 lock_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800893 ssock = __mptcp_tcp_fallback(msk);
894 if (ssock)
895 return tcp_setsockopt(ssock->sk, level, optname, optval,
896 optlen);
Florian Westphal50e741b2020-01-29 15:54:44 +0100897
Peter Krystad717e79c2020-01-21 16:56:22 -0800898 release_sock(sk);
899
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800900 return -EOPNOTSUPP;
Peter Krystad717e79c2020-01-21 16:56:22 -0800901}
902
903static int mptcp_getsockopt(struct sock *sk, int level, int optname,
Florian Westphal50e741b2020-01-29 15:54:44 +0100904 char __user *optval, int __user *option)
Peter Krystad717e79c2020-01-21 16:56:22 -0800905{
906 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystad717e79c2020-01-21 16:56:22 -0800907 struct socket *ssock;
Peter Krystad717e79c2020-01-21 16:56:22 -0800908
909 pr_debug("msk=%p", msk);
910
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800911 /* @@ the meaning of setsockopt() when the socket is connected and
912 * there are multiple subflows is not yet defined. It is up to the
913 * MPTCP-level socket to configure the subflows until the subflow
914 * is in TCP fallback, when socket options are passed through
915 * to the one remaining subflow.
Peter Krystad717e79c2020-01-21 16:56:22 -0800916 */
917 lock_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800918 ssock = __mptcp_tcp_fallback(msk);
919 if (ssock)
920 return tcp_getsockopt(ssock->sk, level, optname, optval,
921 option);
Florian Westphal50e741b2020-01-29 15:54:44 +0100922
Peter Krystad717e79c2020-01-21 16:56:22 -0800923 release_sock(sk);
924
Mat Martineaub6e4a1a2020-02-14 14:14:29 -0800925 return -EOPNOTSUPP;
Peter Krystad717e79c2020-01-21 16:56:22 -0800926}
927
Peter Krystadcec37a62020-01-21 16:56:18 -0800928static int mptcp_get_port(struct sock *sk, unsigned short snum)
Mat Martineauf870fa02020-01-21 16:56:15 -0800929{
930 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800931 struct socket *ssock;
Mat Martineauf870fa02020-01-21 16:56:15 -0800932
Peter Krystadcec37a62020-01-21 16:56:18 -0800933 ssock = __mptcp_nmpc_socket(msk);
934 pr_debug("msk=%p, subflow=%p", msk, ssock);
935 if (WARN_ON_ONCE(!ssock))
936 return -EINVAL;
Mat Martineauf870fa02020-01-21 16:56:15 -0800937
Peter Krystadcec37a62020-01-21 16:56:18 -0800938 return inet_csk_get_port(ssock->sk, snum);
939}
Mat Martineauf870fa02020-01-21 16:56:15 -0800940
Peter Krystadcec37a62020-01-21 16:56:18 -0800941void mptcp_finish_connect(struct sock *ssk)
942{
943 struct mptcp_subflow_context *subflow;
944 struct mptcp_sock *msk;
945 struct sock *sk;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800946 u64 ack_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -0800947
Peter Krystadcec37a62020-01-21 16:56:18 -0800948 subflow = mptcp_subflow_ctx(ssk);
Mat Martineauf870fa02020-01-21 16:56:15 -0800949
Peter Krystadcec37a62020-01-21 16:56:18 -0800950 if (!subflow->mp_capable)
951 return;
952
953 sk = subflow->conn;
954 msk = mptcp_sk(sk);
955
Mat Martineau648ef4b2020-01-21 16:56:24 -0800956 pr_debug("msk=%p, token=%u", sk, subflow->token);
957
Mat Martineau6d0060f2020-01-21 16:56:23 -0800958 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
959 ack_seq++;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800960 subflow->map_seq = ack_seq;
961 subflow->map_subflow_seq = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800962 subflow->rel_write_seq = 1;
963
Peter Krystadcec37a62020-01-21 16:56:18 -0800964 /* the socket is not connected yet, no msk/subflow ops can access/race
965 * accessing the field below
966 */
967 WRITE_ONCE(msk->remote_key, subflow->remote_key);
968 WRITE_ONCE(msk->local_key, subflow->local_key);
Peter Krystad79c09492020-01-21 16:56:20 -0800969 WRITE_ONCE(msk->token, subflow->token);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800970 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
971 WRITE_ONCE(msk->ack_seq, ack_seq);
Christoph Paaschd22f4982020-01-21 16:56:32 -0800972 WRITE_ONCE(msk->can_ack, 1);
Mat Martineauf870fa02020-01-21 16:56:15 -0800973}
974
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800975static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
976{
977 write_lock_bh(&sk->sk_callback_lock);
978 rcu_assign_pointer(sk->sk_wq, &parent->wq);
979 sk_set_socket(sk, parent);
980 sk->sk_uid = SOCK_INODE(parent)->i_uid;
981 write_unlock_bh(&sk->sk_callback_lock);
982}
983
Florian Westphal1891c4a2020-01-21 16:56:25 -0800984static bool mptcp_memory_free(const struct sock *sk, int wake)
985{
986 struct mptcp_sock *msk = mptcp_sk(sk);
987
988 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
989}
990
Mat Martineauf870fa02020-01-21 16:56:15 -0800991static struct proto mptcp_prot = {
992 .name = "MPTCP",
993 .owner = THIS_MODULE,
994 .init = mptcp_init_sock,
995 .close = mptcp_close,
Peter Krystadcf7da0d2020-01-21 16:56:19 -0800996 .accept = mptcp_accept,
Peter Krystad717e79c2020-01-21 16:56:22 -0800997 .setsockopt = mptcp_setsockopt,
998 .getsockopt = mptcp_getsockopt,
Mat Martineauf870fa02020-01-21 16:56:15 -0800999 .shutdown = tcp_shutdown,
Peter Krystad79c09492020-01-21 16:56:20 -08001000 .destroy = mptcp_destroy,
Mat Martineauf870fa02020-01-21 16:56:15 -08001001 .sendmsg = mptcp_sendmsg,
1002 .recvmsg = mptcp_recvmsg,
1003 .hash = inet_hash,
1004 .unhash = inet_unhash,
Peter Krystadcec37a62020-01-21 16:56:18 -08001005 .get_port = mptcp_get_port,
Florian Westphal1891c4a2020-01-21 16:56:25 -08001006 .stream_memory_free = mptcp_memory_free,
Mat Martineauf870fa02020-01-21 16:56:15 -08001007 .obj_size = sizeof(struct mptcp_sock),
1008 .no_autobind = true,
1009};
1010
Peter Krystad2303f992020-01-21 16:56:17 -08001011static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1012{
1013 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1014 struct socket *ssock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001015 int err;
Peter Krystad2303f992020-01-21 16:56:17 -08001016
1017 lock_sock(sock->sk);
1018 ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
1019 if (IS_ERR(ssock)) {
1020 err = PTR_ERR(ssock);
1021 goto unlock;
1022 }
1023
1024 err = ssock->ops->bind(ssock, uaddr, addr_len);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001025 if (!err)
1026 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08001027
1028unlock:
1029 release_sock(sock->sk);
1030 return err;
1031}
1032
1033static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1034 int addr_len, int flags)
1035{
1036 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1037 struct socket *ssock;
1038 int err;
1039
1040 lock_sock(sock->sk);
1041 ssock = __mptcp_socket_create(msk, TCP_SYN_SENT);
1042 if (IS_ERR(ssock)) {
1043 err = PTR_ERR(ssock);
1044 goto unlock;
1045 }
1046
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001047#ifdef CONFIG_TCP_MD5SIG
1048 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
1049 * TCP option space.
1050 */
1051 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
1052 mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0;
1053#endif
1054
Peter Krystad2303f992020-01-21 16:56:17 -08001055 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
1056 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001057 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08001058
1059unlock:
1060 release_sock(sock->sk);
1061 return err;
1062}
1063
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001064static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr,
1065 int peer)
1066{
1067 if (sock->sk->sk_prot == &tcp_prot) {
1068 /* we are being invoked from __sys_accept4, after
1069 * mptcp_accept() has just accepted a non-mp-capable
1070 * flow: sk is a tcp_sk, not an mptcp one.
1071 *
1072 * Hand the socket over to tcp so all further socket ops
1073 * bypass mptcp.
1074 */
1075 sock->ops = &inet_stream_ops;
1076 }
1077
1078 return inet_getname(sock, uaddr, peer);
1079}
1080
1081#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1082static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr,
1083 int peer)
1084{
1085 if (sock->sk->sk_prot == &tcpv6_prot) {
1086 /* we are being invoked from __sys_accept4 after
1087 * mptcp_accept() has accepted a non-mp-capable
1088 * subflow: sk is a tcp_sk, not mptcp.
1089 *
1090 * Hand the socket over to tcp so all further
1091 * socket ops bypass mptcp.
1092 */
1093 sock->ops = &inet6_stream_ops;
1094 }
1095
1096 return inet6_getname(sock, uaddr, peer);
1097}
1098#endif
1099
1100static int mptcp_listen(struct socket *sock, int backlog)
1101{
1102 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1103 struct socket *ssock;
1104 int err;
1105
1106 pr_debug("msk=%p", msk);
1107
1108 lock_sock(sock->sk);
1109 ssock = __mptcp_socket_create(msk, TCP_LISTEN);
1110 if (IS_ERR(ssock)) {
1111 err = PTR_ERR(ssock);
1112 goto unlock;
1113 }
1114
1115 err = ssock->ops->listen(ssock, backlog);
1116 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1117 if (!err)
1118 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1119
1120unlock:
1121 release_sock(sock->sk);
1122 return err;
1123}
1124
1125static bool is_tcp_proto(const struct proto *p)
1126{
1127#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1128 return p == &tcp_prot || p == &tcpv6_prot;
1129#else
1130 return p == &tcp_prot;
1131#endif
1132}
1133
1134static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
1135 int flags, bool kern)
1136{
1137 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1138 struct socket *ssock;
1139 int err;
1140
1141 pr_debug("msk=%p", msk);
1142
1143 lock_sock(sock->sk);
1144 if (sock->sk->sk_state != TCP_LISTEN)
1145 goto unlock_fail;
1146
1147 ssock = __mptcp_nmpc_socket(msk);
1148 if (!ssock)
1149 goto unlock_fail;
1150
1151 sock_hold(ssock->sk);
1152 release_sock(sock->sk);
1153
1154 err = ssock->ops->accept(sock, newsock, flags, kern);
1155 if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
1156 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
1157 struct mptcp_subflow_context *subflow;
1158
1159 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
1160 * This is needed so NOSPACE flag can be set from tcp stack.
1161 */
1162 list_for_each_entry(subflow, &msk->conn_list, node) {
1163 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1164
1165 if (!ssk->sk_socket)
1166 mptcp_sock_graft(ssk, newsock);
1167 }
1168
1169 inet_sk_state_store(newsock->sk, TCP_ESTABLISHED);
1170 }
1171
1172 sock_put(ssock->sk);
1173 return err;
1174
1175unlock_fail:
1176 release_sock(sock->sk);
1177 return -EINVAL;
1178}
1179
Peter Krystad2303f992020-01-21 16:56:17 -08001180static __poll_t mptcp_poll(struct file *file, struct socket *sock,
1181 struct poll_table_struct *wait)
1182{
Florian Westphal1891c4a2020-01-21 16:56:25 -08001183 struct sock *sk = sock->sk;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001184 struct mptcp_sock *msk;
Florian Westphal1891c4a2020-01-21 16:56:25 -08001185 struct socket *ssock;
Peter Krystad2303f992020-01-21 16:56:17 -08001186 __poll_t mask = 0;
1187
Florian Westphal1891c4a2020-01-21 16:56:25 -08001188 msk = mptcp_sk(sk);
1189 lock_sock(sk);
1190 ssock = __mptcp_nmpc_socket(msk);
1191 if (ssock) {
1192 mask = ssock->ops->poll(file, ssock, wait);
1193 release_sock(sk);
1194 return mask;
1195 }
1196
1197 release_sock(sk);
1198 sock_poll_wait(file, sock, wait);
1199 lock_sock(sk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001200 ssock = __mptcp_tcp_fallback(msk);
1201 if (unlikely(ssock))
1202 return ssock->ops->poll(file, ssock, NULL);
Florian Westphal1891c4a2020-01-21 16:56:25 -08001203
1204 if (test_bit(MPTCP_DATA_READY, &msk->flags))
1205 mask = EPOLLIN | EPOLLRDNORM;
1206 if (sk_stream_is_writeable(sk) &&
1207 test_bit(MPTCP_SEND_SPACE, &msk->flags))
1208 mask |= EPOLLOUT | EPOLLWRNORM;
1209 if (sk->sk_shutdown & RCV_SHUTDOWN)
1210 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1211
1212 release_sock(sk);
1213
Peter Krystad2303f992020-01-21 16:56:17 -08001214 return mask;
1215}
1216
Peter Krystad21498492020-01-21 16:56:21 -08001217static int mptcp_shutdown(struct socket *sock, int how)
1218{
1219 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1220 struct mptcp_subflow_context *subflow;
1221 int ret = 0;
1222
1223 pr_debug("sk=%p, how=%d", msk, how);
1224
1225 lock_sock(sock->sk);
1226
1227 if (how == SHUT_WR || how == SHUT_RDWR)
1228 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
1229
1230 how++;
1231
1232 if ((how & ~SHUTDOWN_MASK) || !how) {
1233 ret = -EINVAL;
1234 goto out_unlock;
1235 }
1236
1237 if (sock->state == SS_CONNECTING) {
1238 if ((1 << sock->sk->sk_state) &
1239 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
1240 sock->state = SS_DISCONNECTING;
1241 else
1242 sock->state = SS_CONNECTED;
1243 }
1244
1245 mptcp_for_each_subflow(msk, subflow) {
1246 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
1247
1248 mptcp_subflow_shutdown(tcp_sk, how);
1249 }
1250
1251out_unlock:
1252 release_sock(sock->sk);
1253
1254 return ret;
1255}
1256
Florian Westphale42f1ac2020-01-24 16:04:02 -08001257static const struct proto_ops mptcp_stream_ops = {
1258 .family = PF_INET,
1259 .owner = THIS_MODULE,
1260 .release = inet_release,
1261 .bind = mptcp_bind,
1262 .connect = mptcp_stream_connect,
1263 .socketpair = sock_no_socketpair,
1264 .accept = mptcp_stream_accept,
1265 .getname = mptcp_v4_getname,
1266 .poll = mptcp_poll,
1267 .ioctl = inet_ioctl,
1268 .gettstamp = sock_gettstamp,
1269 .listen = mptcp_listen,
1270 .shutdown = mptcp_shutdown,
1271 .setsockopt = sock_common_setsockopt,
1272 .getsockopt = sock_common_getsockopt,
1273 .sendmsg = inet_sendmsg,
1274 .recvmsg = inet_recvmsg,
1275 .mmap = sock_no_mmap,
1276 .sendpage = inet_sendpage,
1277#ifdef CONFIG_COMPAT
1278 .compat_setsockopt = compat_sock_common_setsockopt,
1279 .compat_getsockopt = compat_sock_common_getsockopt,
1280#endif
1281};
Peter Krystad2303f992020-01-21 16:56:17 -08001282
Mat Martineauf870fa02020-01-21 16:56:15 -08001283static struct inet_protosw mptcp_protosw = {
1284 .type = SOCK_STREAM,
1285 .protocol = IPPROTO_MPTCP,
1286 .prot = &mptcp_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08001287 .ops = &mptcp_stream_ops,
1288 .flags = INET_PROTOSW_ICSK,
Mat Martineauf870fa02020-01-21 16:56:15 -08001289};
1290
Matthieu Baerts784325e2020-01-21 16:56:28 -08001291void mptcp_proto_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08001292{
Peter Krystad2303f992020-01-21 16:56:17 -08001293 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
Peter Krystad2303f992020-01-21 16:56:17 -08001294
1295 mptcp_subflow_init();
1296
Mat Martineauf870fa02020-01-21 16:56:15 -08001297 if (proto_register(&mptcp_prot, 1) != 0)
1298 panic("Failed to register MPTCP proto.\n");
1299
1300 inet_register_protosw(&mptcp_protosw);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001301
1302 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
Mat Martineauf870fa02020-01-21 16:56:15 -08001303}
1304
1305#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Florian Westphale42f1ac2020-01-24 16:04:02 -08001306static const struct proto_ops mptcp_v6_stream_ops = {
1307 .family = PF_INET6,
1308 .owner = THIS_MODULE,
1309 .release = inet6_release,
1310 .bind = mptcp_bind,
1311 .connect = mptcp_stream_connect,
1312 .socketpair = sock_no_socketpair,
1313 .accept = mptcp_stream_accept,
1314 .getname = mptcp_v6_getname,
1315 .poll = mptcp_poll,
1316 .ioctl = inet6_ioctl,
1317 .gettstamp = sock_gettstamp,
1318 .listen = mptcp_listen,
1319 .shutdown = mptcp_shutdown,
1320 .setsockopt = sock_common_setsockopt,
1321 .getsockopt = sock_common_getsockopt,
1322 .sendmsg = inet6_sendmsg,
1323 .recvmsg = inet6_recvmsg,
1324 .mmap = sock_no_mmap,
1325 .sendpage = inet_sendpage,
1326#ifdef CONFIG_COMPAT
1327 .compat_setsockopt = compat_sock_common_setsockopt,
1328 .compat_getsockopt = compat_sock_common_getsockopt,
1329#endif
1330};
1331
Mat Martineauf870fa02020-01-21 16:56:15 -08001332static struct proto mptcp_v6_prot;
1333
Peter Krystad79c09492020-01-21 16:56:20 -08001334static void mptcp_v6_destroy(struct sock *sk)
1335{
1336 mptcp_destroy(sk);
1337 inet6_destroy_sock(sk);
1338}
1339
Mat Martineauf870fa02020-01-21 16:56:15 -08001340static struct inet_protosw mptcp_v6_protosw = {
1341 .type = SOCK_STREAM,
1342 .protocol = IPPROTO_MPTCP,
1343 .prot = &mptcp_v6_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08001344 .ops = &mptcp_v6_stream_ops,
Mat Martineauf870fa02020-01-21 16:56:15 -08001345 .flags = INET_PROTOSW_ICSK,
1346};
1347
Matthieu Baerts784325e2020-01-21 16:56:28 -08001348int mptcp_proto_v6_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08001349{
1350 int err;
1351
1352 mptcp_v6_prot = mptcp_prot;
1353 strcpy(mptcp_v6_prot.name, "MPTCPv6");
1354 mptcp_v6_prot.slab = NULL;
Peter Krystad79c09492020-01-21 16:56:20 -08001355 mptcp_v6_prot.destroy = mptcp_v6_destroy;
Florian Westphalb0519de2020-02-06 00:39:37 +01001356 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
Mat Martineauf870fa02020-01-21 16:56:15 -08001357
1358 err = proto_register(&mptcp_v6_prot, 1);
1359 if (err)
1360 return err;
1361
Mat Martineauf870fa02020-01-21 16:56:15 -08001362 err = inet6_register_protosw(&mptcp_v6_protosw);
1363 if (err)
1364 proto_unregister(&mptcp_v6_prot);
1365
1366 return err;
1367}
1368#endif