xref: /openbmc/linux/net/mptcp/protocol.c (revision 8622a0e5)
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 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/transp_v6.h>
21 #endif
22 #include <net/mptcp.h>
23 #include "protocol.h"
24 
25 #define MPTCP_SAME_STATE TCP_MAX_STATES
26 
27 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
28 struct mptcp6_sock {
29 	struct mptcp_sock msk;
30 	struct ipv6_pinfo np;
31 };
32 #endif
33 
34 struct mptcp_skb_cb {
35 	u32 offset;
36 };
37 
38 #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
39 
40 /* 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  */
44 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
45 {
46 	if (!msk->subflow || READ_ONCE(msk->can_ack))
47 		return NULL;
48 
49 	return msk->subflow;
50 }
51 
52 static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk)
53 {
54 	return msk->first && !sk_is_mptcp(msk->first);
55 }
56 
57 static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
58 {
59 	sock_owned_by_me((const struct sock *)msk);
60 
61 	if (likely(!__mptcp_needs_tcp_fallback(msk)))
62 		return NULL;
63 
64 	if (msk->subflow) {
65 		release_sock((struct sock *)msk);
66 		return msk->subflow;
67 	}
68 
69 	return NULL;
70 }
71 
72 static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
73 {
74 	return !msk->first;
75 }
76 
77 static 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 
95 	msk->first = ssock->sk;
96 	msk->subflow = ssock;
97 	subflow = mptcp_subflow_ctx(ssock->sk);
98 	list_add(&subflow->node, &msk->conn_list);
99 	subflow->request_mptcp = 1;
100 
101 set_state:
102 	if (state != MPTCP_SAME_STATE)
103 		inet_sk_state_store(sk, state);
104 	return ssock;
105 }
106 
107 static 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 
120 static 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_set_owner_r(skb, sk);
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 
134 static 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 	struct sock *sk = (struct sock *)msk;
140 	unsigned int moved = 0;
141 	bool more_data_avail;
142 	struct tcp_sock *tp;
143 	bool done = false;
144 
145 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
146 		int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf);
147 
148 		if (rcvbuf > sk->sk_rcvbuf)
149 			sk->sk_rcvbuf = rcvbuf;
150 	}
151 
152 	tp = tcp_sk(ssk);
153 	do {
154 		u32 map_remaining, offset;
155 		u32 seq = tp->copied_seq;
156 		struct sk_buff *skb;
157 		bool fin;
158 
159 		/* try to move as much data as available */
160 		map_remaining = subflow->map_data_len -
161 				mptcp_subflow_get_map_offset(subflow);
162 
163 		skb = skb_peek(&ssk->sk_receive_queue);
164 		if (!skb)
165 			break;
166 
167 		offset = seq - TCP_SKB_CB(skb)->seq;
168 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
169 		if (fin) {
170 			done = true;
171 			seq++;
172 		}
173 
174 		if (offset < skb->len) {
175 			size_t len = skb->len - offset;
176 
177 			if (tp->urg_data)
178 				done = true;
179 
180 			__mptcp_move_skb(msk, ssk, skb, offset, len);
181 			seq += len;
182 			moved += len;
183 
184 			if (WARN_ON_ONCE(map_remaining < len))
185 				break;
186 		} else {
187 			WARN_ON_ONCE(!fin);
188 			sk_eat_skb(ssk, skb);
189 			done = true;
190 		}
191 
192 		WRITE_ONCE(tp->copied_seq, seq);
193 		more_data_avail = mptcp_subflow_data_available(ssk);
194 
195 		if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) {
196 			done = true;
197 			break;
198 		}
199 	} while (more_data_avail);
200 
201 	*bytes = moved;
202 
203 	return done;
204 }
205 
206 /* In most cases we will be able to lock the mptcp socket.  If its already
207  * owned, we need to defer to the work queue to avoid ABBA deadlock.
208  */
209 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
210 {
211 	struct sock *sk = (struct sock *)msk;
212 	unsigned int moved = 0;
213 
214 	if (READ_ONCE(sk->sk_lock.owned))
215 		return false;
216 
217 	if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
218 		return false;
219 
220 	/* must re-check after taking the lock */
221 	if (!READ_ONCE(sk->sk_lock.owned))
222 		__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
223 
224 	spin_unlock_bh(&sk->sk_lock.slock);
225 
226 	return moved > 0;
227 }
228 
229 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
230 {
231 	struct mptcp_sock *msk = mptcp_sk(sk);
232 
233 	set_bit(MPTCP_DATA_READY, &msk->flags);
234 
235 	if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
236 	    move_skbs_to_msk(msk, ssk))
237 		goto wake;
238 
239 	/* don't schedule if mptcp sk is (still) over limit */
240 	if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
241 		goto wake;
242 
243 	/* mptcp socket is owned, release_cb should retry */
244 	if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
245 			      &sk->sk_tsq_flags)) {
246 		sock_hold(sk);
247 
248 		/* need to try again, its possible release_cb() has already
249 		 * been called after the test_and_set_bit() above.
250 		 */
251 		move_skbs_to_msk(msk, ssk);
252 	}
253 wake:
254 	sk->sk_data_ready(sk);
255 }
256 
257 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
258 {
259 	if (!msk->cached_ext)
260 		msk->cached_ext = __skb_ext_alloc();
261 
262 	return !!msk->cached_ext;
263 }
264 
265 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
266 {
267 	struct mptcp_subflow_context *subflow;
268 	struct sock *sk = (struct sock *)msk;
269 
270 	sock_owned_by_me(sk);
271 
272 	mptcp_for_each_subflow(msk, subflow) {
273 		if (subflow->data_avail)
274 			return mptcp_subflow_tcp_sock(subflow);
275 	}
276 
277 	return NULL;
278 }
279 
280 static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
281 					     const struct sk_buff *skb,
282 					     const struct mptcp_ext *mpext)
283 {
284 	if (!tcp_skb_can_collapse_to(skb))
285 		return false;
286 
287 	/* can collapse only if MPTCP level sequence is in order */
288 	return mpext && mpext->data_seq + mpext->data_len == msk->write_seq;
289 }
290 
291 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
292 			      struct msghdr *msg, long *timeo, int *pmss_now,
293 			      int *ps_goal)
294 {
295 	int mss_now, avail_size, size_goal, ret;
296 	struct mptcp_sock *msk = mptcp_sk(sk);
297 	struct mptcp_ext *mpext = NULL;
298 	struct sk_buff *skb, *tail;
299 	bool can_collapse = false;
300 	struct page_frag *pfrag;
301 	size_t psize;
302 
303 	/* use the mptcp page cache so that we can easily move the data
304 	 * from one substream to another, but do per subflow memory accounting
305 	 */
306 	pfrag = sk_page_frag(sk);
307 	while (!sk_page_frag_refill(ssk, pfrag) ||
308 	       !mptcp_ext_cache_refill(msk)) {
309 		ret = sk_stream_wait_memory(ssk, timeo);
310 		if (ret)
311 			return ret;
312 		if (unlikely(__mptcp_needs_tcp_fallback(msk)))
313 			return 0;
314 	}
315 
316 	/* compute copy limit */
317 	mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
318 	*pmss_now = mss_now;
319 	*ps_goal = size_goal;
320 	avail_size = size_goal;
321 	skb = tcp_write_queue_tail(ssk);
322 	if (skb) {
323 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
324 
325 		/* Limit the write to the size available in the
326 		 * current skb, if any, so that we create at most a new skb.
327 		 * Explicitly tells TCP internals to avoid collapsing on later
328 		 * queue management operation, to avoid breaking the ext <->
329 		 * SSN association set here
330 		 */
331 		can_collapse = (size_goal - skb->len > 0) &&
332 			      mptcp_skb_can_collapse_to(msk, skb, mpext);
333 		if (!can_collapse)
334 			TCP_SKB_CB(skb)->eor = 1;
335 		else
336 			avail_size = size_goal - skb->len;
337 	}
338 	psize = min_t(size_t, pfrag->size - pfrag->offset, avail_size);
339 
340 	/* Copy to page */
341 	pr_debug("left=%zu", msg_data_left(msg));
342 	psize = copy_page_from_iter(pfrag->page, pfrag->offset,
343 				    min_t(size_t, msg_data_left(msg), psize),
344 				    &msg->msg_iter);
345 	pr_debug("left=%zu", msg_data_left(msg));
346 	if (!psize)
347 		return -EINVAL;
348 
349 	/* tell the TCP stack to delay the push so that we can safely
350 	 * access the skb after the sendpages call
351 	 */
352 	ret = do_tcp_sendpages(ssk, pfrag->page, pfrag->offset, psize,
353 			       msg->msg_flags | MSG_SENDPAGE_NOTLAST);
354 	if (ret <= 0)
355 		return ret;
356 	if (unlikely(ret < psize))
357 		iov_iter_revert(&msg->msg_iter, psize - ret);
358 
359 	/* if the tail skb extension is still the cached one, collapsing
360 	 * really happened. Note: we can't check for 'same skb' as the sk_buff
361 	 * hdr on tail can be transmitted, freed and re-allocated by the
362 	 * do_tcp_sendpages() call
363 	 */
364 	tail = tcp_write_queue_tail(ssk);
365 	if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
366 		WARN_ON_ONCE(!can_collapse);
367 		mpext->data_len += ret;
368 		goto out;
369 	}
370 
371 	skb = tcp_write_queue_tail(ssk);
372 	mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
373 	msk->cached_ext = NULL;
374 
375 	memset(mpext, 0, sizeof(*mpext));
376 	mpext->data_seq = msk->write_seq;
377 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
378 	mpext->data_len = ret;
379 	mpext->use_map = 1;
380 	mpext->dsn64 = 1;
381 
382 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
383 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
384 		 mpext->dsn64);
385 
386 out:
387 	pfrag->offset += ret;
388 	msk->write_seq += ret;
389 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
390 
391 	return ret;
392 }
393 
394 static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk)
395 {
396 	struct socket *sock;
397 
398 	if (likely(sk_stream_is_writeable(ssk)))
399 		return;
400 
401 	sock = READ_ONCE(ssk->sk_socket);
402 
403 	if (sock) {
404 		clear_bit(MPTCP_SEND_SPACE, &msk->flags);
405 		smp_mb__after_atomic();
406 		/* set NOSPACE only after clearing SEND_SPACE flag */
407 		set_bit(SOCK_NOSPACE, &sock->flags);
408 	}
409 }
410 
411 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
412 {
413 	int mss_now = 0, size_goal = 0, ret = 0;
414 	struct mptcp_sock *msk = mptcp_sk(sk);
415 	struct socket *ssock;
416 	size_t copied = 0;
417 	struct sock *ssk;
418 	long timeo;
419 
420 	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
421 		return -EOPNOTSUPP;
422 
423 	lock_sock(sk);
424 
425 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
426 
427 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
428 		ret = sk_stream_wait_connect(sk, &timeo);
429 		if (ret)
430 			goto out;
431 	}
432 
433 	ssock = __mptcp_tcp_fallback(msk);
434 	if (unlikely(ssock)) {
435 fallback:
436 		pr_debug("fallback passthrough");
437 		ret = sock_sendmsg(ssock, msg);
438 		return ret >= 0 ? ret + copied : (copied ? copied : ret);
439 	}
440 
441 	ssk = mptcp_subflow_get(msk);
442 	if (!ssk) {
443 		release_sock(sk);
444 		return -ENOTCONN;
445 	}
446 
447 	pr_debug("conn_list->subflow=%p", ssk);
448 
449 	lock_sock(ssk);
450 	while (msg_data_left(msg)) {
451 		ret = mptcp_sendmsg_frag(sk, ssk, msg, &timeo, &mss_now,
452 					 &size_goal);
453 		if (ret < 0)
454 			break;
455 		if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) {
456 			release_sock(ssk);
457 			ssock = __mptcp_tcp_fallback(msk);
458 			goto fallback;
459 		}
460 
461 		copied += ret;
462 	}
463 
464 	if (copied) {
465 		ret = copied;
466 		tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
467 			 size_goal);
468 	}
469 
470 	ssk_check_wmem(msk, ssk);
471 	release_sock(ssk);
472 out:
473 	release_sock(sk);
474 	return ret;
475 }
476 
477 static void mptcp_wait_data(struct sock *sk, long *timeo)
478 {
479 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
480 	struct mptcp_sock *msk = mptcp_sk(sk);
481 
482 	add_wait_queue(sk_sleep(sk), &wait);
483 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
484 
485 	sk_wait_event(sk, timeo,
486 		      test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
487 
488 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
489 	remove_wait_queue(sk_sleep(sk), &wait);
490 }
491 
492 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
493 				struct msghdr *msg,
494 				size_t len)
495 {
496 	struct sock *sk = (struct sock *)msk;
497 	struct sk_buff *skb;
498 	int copied = 0;
499 
500 	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
501 		u32 offset = MPTCP_SKB_CB(skb)->offset;
502 		u32 data_len = skb->len - offset;
503 		u32 count = min_t(size_t, len - copied, data_len);
504 		int err;
505 
506 		err = skb_copy_datagram_msg(skb, offset, msg, count);
507 		if (unlikely(err < 0)) {
508 			if (!copied)
509 				return err;
510 			break;
511 		}
512 
513 		copied += count;
514 
515 		if (count < data_len) {
516 			MPTCP_SKB_CB(skb)->offset += count;
517 			break;
518 		}
519 
520 		__skb_unlink(skb, &sk->sk_receive_queue);
521 		__kfree_skb(skb);
522 
523 		if (copied >= len)
524 			break;
525 	}
526 
527 	return copied;
528 }
529 
530 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
531 {
532 	unsigned int moved = 0;
533 	bool done;
534 
535 	do {
536 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
537 
538 		if (!ssk)
539 			break;
540 
541 		lock_sock(ssk);
542 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
543 		release_sock(ssk);
544 	} while (!done);
545 
546 	return moved > 0;
547 }
548 
549 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
550 			 int nonblock, int flags, int *addr_len)
551 {
552 	struct mptcp_sock *msk = mptcp_sk(sk);
553 	struct socket *ssock;
554 	int copied = 0;
555 	int target;
556 	long timeo;
557 
558 	if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
559 		return -EOPNOTSUPP;
560 
561 	lock_sock(sk);
562 	ssock = __mptcp_tcp_fallback(msk);
563 	if (unlikely(ssock)) {
564 fallback:
565 		pr_debug("fallback-read subflow=%p",
566 			 mptcp_subflow_ctx(ssock->sk));
567 		copied = sock_recvmsg(ssock, msg, flags);
568 		return copied;
569 	}
570 
571 	timeo = sock_rcvtimeo(sk, nonblock);
572 
573 	len = min_t(size_t, len, INT_MAX);
574 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
575 
576 	while (len > (size_t)copied) {
577 		int bytes_read;
578 
579 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
580 		if (unlikely(bytes_read < 0)) {
581 			if (!copied)
582 				copied = bytes_read;
583 			goto out_err;
584 		}
585 
586 		copied += bytes_read;
587 
588 		if (skb_queue_empty(&sk->sk_receive_queue) &&
589 		    __mptcp_move_skbs(msk))
590 			continue;
591 
592 		/* only the master socket status is relevant here. The exit
593 		 * conditions mirror closely tcp_recvmsg()
594 		 */
595 		if (copied >= target)
596 			break;
597 
598 		if (copied) {
599 			if (sk->sk_err ||
600 			    sk->sk_state == TCP_CLOSE ||
601 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
602 			    !timeo ||
603 			    signal_pending(current))
604 				break;
605 		} else {
606 			if (sk->sk_err) {
607 				copied = sock_error(sk);
608 				break;
609 			}
610 
611 			if (sk->sk_shutdown & RCV_SHUTDOWN)
612 				break;
613 
614 			if (sk->sk_state == TCP_CLOSE) {
615 				copied = -ENOTCONN;
616 				break;
617 			}
618 
619 			if (!timeo) {
620 				copied = -EAGAIN;
621 				break;
622 			}
623 
624 			if (signal_pending(current)) {
625 				copied = sock_intr_errno(timeo);
626 				break;
627 			}
628 		}
629 
630 		pr_debug("block timeout %ld", timeo);
631 		mptcp_wait_data(sk, &timeo);
632 		if (unlikely(__mptcp_tcp_fallback(msk)))
633 			goto fallback;
634 	}
635 
636 	if (skb_queue_empty(&sk->sk_receive_queue)) {
637 		/* entire backlog drained, clear DATA_READY. */
638 		clear_bit(MPTCP_DATA_READY, &msk->flags);
639 
640 		/* .. race-breaker: ssk might have gotten new data
641 		 * after last __mptcp_move_skbs() returned false.
642 		 */
643 		if (unlikely(__mptcp_move_skbs(msk)))
644 			set_bit(MPTCP_DATA_READY, &msk->flags);
645 	} else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
646 		/* data to read but mptcp_wait_data() cleared DATA_READY */
647 		set_bit(MPTCP_DATA_READY, &msk->flags);
648 	}
649 out_err:
650 	release_sock(sk);
651 	return copied;
652 }
653 
654 /* subflow sockets can be either outgoing (connect) or incoming
655  * (accept).
656  *
657  * Outgoing subflows use in-kernel sockets.
658  * Incoming subflows do not have their own 'struct socket' allocated,
659  * so we need to use tcp_close() after detaching them from the mptcp
660  * parent socket.
661  */
662 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
663 			      struct mptcp_subflow_context *subflow,
664 			      long timeout)
665 {
666 	struct socket *sock = READ_ONCE(ssk->sk_socket);
667 
668 	list_del(&subflow->node);
669 
670 	if (sock && sock != sk->sk_socket) {
671 		/* outgoing subflow */
672 		sock_release(sock);
673 	} else {
674 		/* incoming subflow */
675 		tcp_close(ssk, timeout);
676 	}
677 }
678 
679 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
680 {
681 	return 0;
682 }
683 
684 static void mptcp_worker(struct work_struct *work)
685 {
686 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
687 	struct sock *sk = &msk->sk.icsk_inet.sk;
688 
689 	lock_sock(sk);
690 	__mptcp_move_skbs(msk);
691 	release_sock(sk);
692 	sock_put(sk);
693 }
694 
695 static int __mptcp_init_sock(struct sock *sk)
696 {
697 	struct mptcp_sock *msk = mptcp_sk(sk);
698 
699 	INIT_LIST_HEAD(&msk->conn_list);
700 	__set_bit(MPTCP_SEND_SPACE, &msk->flags);
701 	INIT_WORK(&msk->work, mptcp_worker);
702 
703 	msk->first = NULL;
704 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
705 
706 	return 0;
707 }
708 
709 static int mptcp_init_sock(struct sock *sk)
710 {
711 	if (!mptcp_is_enabled(sock_net(sk)))
712 		return -ENOPROTOOPT;
713 
714 	return __mptcp_init_sock(sk);
715 }
716 
717 static void mptcp_cancel_work(struct sock *sk)
718 {
719 	struct mptcp_sock *msk = mptcp_sk(sk);
720 
721 	if (cancel_work_sync(&msk->work))
722 		sock_put(sk);
723 }
724 
725 static void mptcp_subflow_shutdown(struct sock *ssk, int how,
726 				   bool data_fin_tx_enable, u64 data_fin_tx_seq)
727 {
728 	lock_sock(ssk);
729 
730 	switch (ssk->sk_state) {
731 	case TCP_LISTEN:
732 		if (!(how & RCV_SHUTDOWN))
733 			break;
734 		/* fall through */
735 	case TCP_SYN_SENT:
736 		tcp_disconnect(ssk, O_NONBLOCK);
737 		break;
738 	default:
739 		if (data_fin_tx_enable) {
740 			struct mptcp_subflow_context *subflow;
741 
742 			subflow = mptcp_subflow_ctx(ssk);
743 			subflow->data_fin_tx_seq = data_fin_tx_seq;
744 			subflow->data_fin_tx_enable = 1;
745 		}
746 
747 		ssk->sk_shutdown |= how;
748 		tcp_shutdown(ssk, how);
749 		break;
750 	}
751 
752 	/* Wake up anyone sleeping in poll. */
753 	ssk->sk_state_change(ssk);
754 	release_sock(ssk);
755 }
756 
757 /* Called with msk lock held, releases such lock before returning */
758 static void mptcp_close(struct sock *sk, long timeout)
759 {
760 	struct mptcp_subflow_context *subflow, *tmp;
761 	struct mptcp_sock *msk = mptcp_sk(sk);
762 	LIST_HEAD(conn_list);
763 	u64 data_fin_tx_seq;
764 
765 	lock_sock(sk);
766 
767 	mptcp_token_destroy(msk->token);
768 	inet_sk_state_store(sk, TCP_CLOSE);
769 
770 	list_splice_init(&msk->conn_list, &conn_list);
771 
772 	data_fin_tx_seq = msk->write_seq;
773 
774 	release_sock(sk);
775 
776 	list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
777 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
778 
779 		subflow->data_fin_tx_seq = data_fin_tx_seq;
780 		subflow->data_fin_tx_enable = 1;
781 		__mptcp_close_ssk(sk, ssk, subflow, timeout);
782 	}
783 
784 	mptcp_cancel_work(sk);
785 
786 	__skb_queue_purge(&sk->sk_receive_queue);
787 
788 	sk_common_release(sk);
789 }
790 
791 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
792 {
793 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
794 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
795 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
796 
797 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
798 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
799 
800 	if (msk6 && ssk6) {
801 		msk6->saddr = ssk6->saddr;
802 		msk6->flow_label = ssk6->flow_label;
803 	}
804 #endif
805 
806 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
807 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
808 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
809 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
810 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
811 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
812 }
813 
814 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
815 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
816 {
817 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
818 
819 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
820 }
821 #endif
822 
823 static struct sock *mptcp_sk_clone_lock(const struct sock *sk)
824 {
825 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
826 
827 	if (!nsk)
828 		return NULL;
829 
830 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
831 	if (nsk->sk_family == AF_INET6)
832 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
833 #endif
834 
835 	return nsk;
836 }
837 
838 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
839 				 bool kern)
840 {
841 	struct mptcp_sock *msk = mptcp_sk(sk);
842 	struct socket *listener;
843 	struct sock *newsk;
844 
845 	listener = __mptcp_nmpc_socket(msk);
846 	if (WARN_ON_ONCE(!listener)) {
847 		*err = -EINVAL;
848 		return NULL;
849 	}
850 
851 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
852 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
853 	if (!newsk)
854 		return NULL;
855 
856 	pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
857 
858 	if (sk_is_mptcp(newsk)) {
859 		struct mptcp_subflow_context *subflow;
860 		struct sock *new_mptcp_sock;
861 		struct sock *ssk = newsk;
862 		u64 ack_seq;
863 
864 		subflow = mptcp_subflow_ctx(newsk);
865 		lock_sock(sk);
866 
867 		local_bh_disable();
868 		new_mptcp_sock = mptcp_sk_clone_lock(sk);
869 		if (!new_mptcp_sock) {
870 			*err = -ENOBUFS;
871 			local_bh_enable();
872 			release_sock(sk);
873 			mptcp_subflow_shutdown(newsk, SHUT_RDWR + 1, 0, 0);
874 			tcp_close(newsk, 0);
875 			return NULL;
876 		}
877 
878 		__mptcp_init_sock(new_mptcp_sock);
879 
880 		msk = mptcp_sk(new_mptcp_sock);
881 		msk->local_key = subflow->local_key;
882 		msk->token = subflow->token;
883 		msk->subflow = NULL;
884 		msk->first = newsk;
885 
886 		mptcp_token_update_accept(newsk, new_mptcp_sock);
887 
888 		msk->write_seq = subflow->idsn + 1;
889 		if (subflow->can_ack) {
890 			msk->can_ack = true;
891 			msk->remote_key = subflow->remote_key;
892 			mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
893 			ack_seq++;
894 			msk->ack_seq = ack_seq;
895 		}
896 		newsk = new_mptcp_sock;
897 		mptcp_copy_inaddrs(newsk, ssk);
898 		list_add(&subflow->node, &msk->conn_list);
899 
900 		/* will be fully established at mptcp_stream_accept()
901 		 * completion.
902 		 */
903 		inet_sk_state_store(new_mptcp_sock, TCP_SYN_RECV);
904 		bh_unlock_sock(new_mptcp_sock);
905 		local_bh_enable();
906 		release_sock(sk);
907 
908 		/* the subflow can already receive packet, avoid racing with
909 		 * the receive path and process the pending ones
910 		 */
911 		lock_sock(ssk);
912 		subflow->rel_write_seq = 1;
913 		subflow->tcp_sock = ssk;
914 		subflow->conn = new_mptcp_sock;
915 		if (unlikely(!skb_queue_empty(&ssk->sk_receive_queue)))
916 			mptcp_subflow_data_available(ssk);
917 		release_sock(ssk);
918 	}
919 
920 	return newsk;
921 }
922 
923 static void mptcp_destroy(struct sock *sk)
924 {
925 	struct mptcp_sock *msk = mptcp_sk(sk);
926 
927 	if (msk->cached_ext)
928 		__skb_ext_put(msk->cached_ext);
929 }
930 
931 static int mptcp_setsockopt(struct sock *sk, int level, int optname,
932 			    char __user *optval, unsigned int optlen)
933 {
934 	struct mptcp_sock *msk = mptcp_sk(sk);
935 	struct socket *ssock;
936 
937 	pr_debug("msk=%p", msk);
938 
939 	/* @@ the meaning of setsockopt() when the socket is connected and
940 	 * there are multiple subflows is not yet defined. It is up to the
941 	 * MPTCP-level socket to configure the subflows until the subflow
942 	 * is in TCP fallback, when TCP socket options are passed through
943 	 * to the one remaining subflow.
944 	 */
945 	lock_sock(sk);
946 	ssock = __mptcp_tcp_fallback(msk);
947 	if (ssock)
948 		return tcp_setsockopt(ssock->sk, level, optname, optval,
949 				      optlen);
950 
951 	release_sock(sk);
952 
953 	return -EOPNOTSUPP;
954 }
955 
956 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
957 			    char __user *optval, int __user *option)
958 {
959 	struct mptcp_sock *msk = mptcp_sk(sk);
960 	struct socket *ssock;
961 
962 	pr_debug("msk=%p", msk);
963 
964 	/* @@ the meaning of setsockopt() when the socket is connected and
965 	 * there are multiple subflows is not yet defined. It is up to the
966 	 * MPTCP-level socket to configure the subflows until the subflow
967 	 * is in TCP fallback, when socket options are passed through
968 	 * to the one remaining subflow.
969 	 */
970 	lock_sock(sk);
971 	ssock = __mptcp_tcp_fallback(msk);
972 	if (ssock)
973 		return tcp_getsockopt(ssock->sk, level, optname, optval,
974 				      option);
975 
976 	release_sock(sk);
977 
978 	return -EOPNOTSUPP;
979 }
980 
981 #define MPTCP_DEFERRED_ALL TCPF_DELACK_TIMER_DEFERRED
982 
983 /* this is very alike tcp_release_cb() but we must handle differently a
984  * different set of events
985  */
986 static void mptcp_release_cb(struct sock *sk)
987 {
988 	unsigned long flags, nflags;
989 
990 	do {
991 		flags = sk->sk_tsq_flags;
992 		if (!(flags & MPTCP_DEFERRED_ALL))
993 			return;
994 		nflags = flags & ~MPTCP_DEFERRED_ALL;
995 	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
996 
997 	if (flags & TCPF_DELACK_TIMER_DEFERRED) {
998 		struct mptcp_sock *msk = mptcp_sk(sk);
999 		struct sock *ssk;
1000 
1001 		ssk = mptcp_subflow_recv_lookup(msk);
1002 		if (!ssk || !schedule_work(&msk->work))
1003 			__sock_put(sk);
1004 	}
1005 }
1006 
1007 static int mptcp_get_port(struct sock *sk, unsigned short snum)
1008 {
1009 	struct mptcp_sock *msk = mptcp_sk(sk);
1010 	struct socket *ssock;
1011 
1012 	ssock = __mptcp_nmpc_socket(msk);
1013 	pr_debug("msk=%p, subflow=%p", msk, ssock);
1014 	if (WARN_ON_ONCE(!ssock))
1015 		return -EINVAL;
1016 
1017 	return inet_csk_get_port(ssock->sk, snum);
1018 }
1019 
1020 void mptcp_finish_connect(struct sock *ssk)
1021 {
1022 	struct mptcp_subflow_context *subflow;
1023 	struct mptcp_sock *msk;
1024 	struct sock *sk;
1025 	u64 ack_seq;
1026 
1027 	subflow = mptcp_subflow_ctx(ssk);
1028 
1029 	if (!subflow->mp_capable)
1030 		return;
1031 
1032 	sk = subflow->conn;
1033 	msk = mptcp_sk(sk);
1034 
1035 	pr_debug("msk=%p, token=%u", sk, subflow->token);
1036 
1037 	mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
1038 	ack_seq++;
1039 	subflow->map_seq = ack_seq;
1040 	subflow->map_subflow_seq = 1;
1041 	subflow->rel_write_seq = 1;
1042 
1043 	/* the socket is not connected yet, no msk/subflow ops can access/race
1044 	 * accessing the field below
1045 	 */
1046 	WRITE_ONCE(msk->remote_key, subflow->remote_key);
1047 	WRITE_ONCE(msk->local_key, subflow->local_key);
1048 	WRITE_ONCE(msk->token, subflow->token);
1049 	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
1050 	WRITE_ONCE(msk->ack_seq, ack_seq);
1051 	WRITE_ONCE(msk->can_ack, 1);
1052 	if (inet_sk_state_load(sk) != TCP_ESTABLISHED) {
1053 		inet_sk_state_store(sk, TCP_ESTABLISHED);
1054 		sk->sk_state_change(sk);
1055 	}
1056 }
1057 
1058 static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
1059 {
1060 	write_lock_bh(&sk->sk_callback_lock);
1061 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
1062 	sk_set_socket(sk, parent);
1063 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
1064 	write_unlock_bh(&sk->sk_callback_lock);
1065 }
1066 
1067 static bool mptcp_memory_free(const struct sock *sk, int wake)
1068 {
1069 	struct mptcp_sock *msk = mptcp_sk(sk);
1070 
1071 	return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
1072 }
1073 
1074 static struct proto mptcp_prot = {
1075 	.name		= "MPTCP",
1076 	.owner		= THIS_MODULE,
1077 	.init		= mptcp_init_sock,
1078 	.close		= mptcp_close,
1079 	.accept		= mptcp_accept,
1080 	.setsockopt	= mptcp_setsockopt,
1081 	.getsockopt	= mptcp_getsockopt,
1082 	.shutdown	= tcp_shutdown,
1083 	.destroy	= mptcp_destroy,
1084 	.sendmsg	= mptcp_sendmsg,
1085 	.recvmsg	= mptcp_recvmsg,
1086 	.release_cb	= mptcp_release_cb,
1087 	.hash		= inet_hash,
1088 	.unhash		= inet_unhash,
1089 	.get_port	= mptcp_get_port,
1090 	.stream_memory_free	= mptcp_memory_free,
1091 	.obj_size	= sizeof(struct mptcp_sock),
1092 	.no_autobind	= true,
1093 };
1094 
1095 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1096 {
1097 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
1098 	struct socket *ssock;
1099 	int err;
1100 
1101 	lock_sock(sock->sk);
1102 	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
1103 	if (IS_ERR(ssock)) {
1104 		err = PTR_ERR(ssock);
1105 		goto unlock;
1106 	}
1107 
1108 	err = ssock->ops->bind(ssock, uaddr, addr_len);
1109 	if (!err)
1110 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
1111 
1112 unlock:
1113 	release_sock(sock->sk);
1114 	return err;
1115 }
1116 
1117 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1118 				int addr_len, int flags)
1119 {
1120 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
1121 	struct socket *ssock;
1122 	int err;
1123 
1124 	lock_sock(sock->sk);
1125 	ssock = __mptcp_socket_create(msk, TCP_SYN_SENT);
1126 	if (IS_ERR(ssock)) {
1127 		err = PTR_ERR(ssock);
1128 		goto unlock;
1129 	}
1130 
1131 #ifdef CONFIG_TCP_MD5SIG
1132 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
1133 	 * TCP option space.
1134 	 */
1135 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
1136 		mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0;
1137 #endif
1138 
1139 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
1140 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1141 	mptcp_copy_inaddrs(sock->sk, ssock->sk);
1142 
1143 unlock:
1144 	release_sock(sock->sk);
1145 	return err;
1146 }
1147 
1148 static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr,
1149 			    int peer)
1150 {
1151 	if (sock->sk->sk_prot == &tcp_prot) {
1152 		/* we are being invoked from __sys_accept4, after
1153 		 * mptcp_accept() has just accepted a non-mp-capable
1154 		 * flow: sk is a tcp_sk, not an mptcp one.
1155 		 *
1156 		 * Hand the socket over to tcp so all further socket ops
1157 		 * bypass mptcp.
1158 		 */
1159 		sock->ops = &inet_stream_ops;
1160 	}
1161 
1162 	return inet_getname(sock, uaddr, peer);
1163 }
1164 
1165 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1166 static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr,
1167 			    int peer)
1168 {
1169 	if (sock->sk->sk_prot == &tcpv6_prot) {
1170 		/* we are being invoked from __sys_accept4 after
1171 		 * mptcp_accept() has accepted a non-mp-capable
1172 		 * subflow: sk is a tcp_sk, not mptcp.
1173 		 *
1174 		 * Hand the socket over to tcp so all further
1175 		 * socket ops bypass mptcp.
1176 		 */
1177 		sock->ops = &inet6_stream_ops;
1178 	}
1179 
1180 	return inet6_getname(sock, uaddr, peer);
1181 }
1182 #endif
1183 
1184 static int mptcp_listen(struct socket *sock, int backlog)
1185 {
1186 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
1187 	struct socket *ssock;
1188 	int err;
1189 
1190 	pr_debug("msk=%p", msk);
1191 
1192 	lock_sock(sock->sk);
1193 	ssock = __mptcp_socket_create(msk, TCP_LISTEN);
1194 	if (IS_ERR(ssock)) {
1195 		err = PTR_ERR(ssock);
1196 		goto unlock;
1197 	}
1198 
1199 	err = ssock->ops->listen(ssock, backlog);
1200 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1201 	if (!err)
1202 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
1203 
1204 unlock:
1205 	release_sock(sock->sk);
1206 	return err;
1207 }
1208 
1209 static bool is_tcp_proto(const struct proto *p)
1210 {
1211 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1212 	return p == &tcp_prot || p == &tcpv6_prot;
1213 #else
1214 	return p == &tcp_prot;
1215 #endif
1216 }
1217 
1218 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
1219 			       int flags, bool kern)
1220 {
1221 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
1222 	struct socket *ssock;
1223 	int err;
1224 
1225 	pr_debug("msk=%p", msk);
1226 
1227 	lock_sock(sock->sk);
1228 	if (sock->sk->sk_state != TCP_LISTEN)
1229 		goto unlock_fail;
1230 
1231 	ssock = __mptcp_nmpc_socket(msk);
1232 	if (!ssock)
1233 		goto unlock_fail;
1234 
1235 	sock_hold(ssock->sk);
1236 	release_sock(sock->sk);
1237 
1238 	err = ssock->ops->accept(sock, newsock, flags, kern);
1239 	if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
1240 		struct mptcp_sock *msk = mptcp_sk(newsock->sk);
1241 		struct mptcp_subflow_context *subflow;
1242 
1243 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
1244 		 * This is needed so NOSPACE flag can be set from tcp stack.
1245 		 */
1246 		list_for_each_entry(subflow, &msk->conn_list, node) {
1247 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1248 
1249 			if (!ssk->sk_socket)
1250 				mptcp_sock_graft(ssk, newsock);
1251 		}
1252 
1253 		inet_sk_state_store(newsock->sk, TCP_ESTABLISHED);
1254 	}
1255 
1256 	sock_put(ssock->sk);
1257 	return err;
1258 
1259 unlock_fail:
1260 	release_sock(sock->sk);
1261 	return -EINVAL;
1262 }
1263 
1264 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
1265 			   struct poll_table_struct *wait)
1266 {
1267 	struct sock *sk = sock->sk;
1268 	struct mptcp_sock *msk;
1269 	struct socket *ssock;
1270 	__poll_t mask = 0;
1271 
1272 	msk = mptcp_sk(sk);
1273 	lock_sock(sk);
1274 	ssock = __mptcp_nmpc_socket(msk);
1275 	if (ssock) {
1276 		mask = ssock->ops->poll(file, ssock, wait);
1277 		release_sock(sk);
1278 		return mask;
1279 	}
1280 
1281 	release_sock(sk);
1282 	sock_poll_wait(file, sock, wait);
1283 	lock_sock(sk);
1284 	ssock = __mptcp_tcp_fallback(msk);
1285 	if (unlikely(ssock))
1286 		return ssock->ops->poll(file, ssock, NULL);
1287 
1288 	if (test_bit(MPTCP_DATA_READY, &msk->flags))
1289 		mask = EPOLLIN | EPOLLRDNORM;
1290 	if (sk_stream_is_writeable(sk) &&
1291 	    test_bit(MPTCP_SEND_SPACE, &msk->flags))
1292 		mask |= EPOLLOUT | EPOLLWRNORM;
1293 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1294 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1295 
1296 	release_sock(sk);
1297 
1298 	return mask;
1299 }
1300 
1301 static int mptcp_shutdown(struct socket *sock, int how)
1302 {
1303 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
1304 	struct mptcp_subflow_context *subflow;
1305 	int ret = 0;
1306 
1307 	pr_debug("sk=%p, how=%d", msk, how);
1308 
1309 	lock_sock(sock->sk);
1310 
1311 	if (how == SHUT_WR || how == SHUT_RDWR)
1312 		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
1313 
1314 	how++;
1315 
1316 	if ((how & ~SHUTDOWN_MASK) || !how) {
1317 		ret = -EINVAL;
1318 		goto out_unlock;
1319 	}
1320 
1321 	if (sock->state == SS_CONNECTING) {
1322 		if ((1 << sock->sk->sk_state) &
1323 		    (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
1324 			sock->state = SS_DISCONNECTING;
1325 		else
1326 			sock->state = SS_CONNECTED;
1327 	}
1328 
1329 	mptcp_for_each_subflow(msk, subflow) {
1330 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
1331 
1332 		mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq);
1333 	}
1334 
1335 out_unlock:
1336 	release_sock(sock->sk);
1337 
1338 	return ret;
1339 }
1340 
1341 static const struct proto_ops mptcp_stream_ops = {
1342 	.family		   = PF_INET,
1343 	.owner		   = THIS_MODULE,
1344 	.release	   = inet_release,
1345 	.bind		   = mptcp_bind,
1346 	.connect	   = mptcp_stream_connect,
1347 	.socketpair	   = sock_no_socketpair,
1348 	.accept		   = mptcp_stream_accept,
1349 	.getname	   = mptcp_v4_getname,
1350 	.poll		   = mptcp_poll,
1351 	.ioctl		   = inet_ioctl,
1352 	.gettstamp	   = sock_gettstamp,
1353 	.listen		   = mptcp_listen,
1354 	.shutdown	   = mptcp_shutdown,
1355 	.setsockopt	   = sock_common_setsockopt,
1356 	.getsockopt	   = sock_common_getsockopt,
1357 	.sendmsg	   = inet_sendmsg,
1358 	.recvmsg	   = inet_recvmsg,
1359 	.mmap		   = sock_no_mmap,
1360 	.sendpage	   = inet_sendpage,
1361 #ifdef CONFIG_COMPAT
1362 	.compat_setsockopt = compat_sock_common_setsockopt,
1363 	.compat_getsockopt = compat_sock_common_getsockopt,
1364 #endif
1365 };
1366 
1367 static struct inet_protosw mptcp_protosw = {
1368 	.type		= SOCK_STREAM,
1369 	.protocol	= IPPROTO_MPTCP,
1370 	.prot		= &mptcp_prot,
1371 	.ops		= &mptcp_stream_ops,
1372 	.flags		= INET_PROTOSW_ICSK,
1373 };
1374 
1375 void mptcp_proto_init(void)
1376 {
1377 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
1378 
1379 	mptcp_subflow_init();
1380 
1381 	if (proto_register(&mptcp_prot, 1) != 0)
1382 		panic("Failed to register MPTCP proto.\n");
1383 
1384 	inet_register_protosw(&mptcp_protosw);
1385 
1386 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
1387 }
1388 
1389 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1390 static const struct proto_ops mptcp_v6_stream_ops = {
1391 	.family		   = PF_INET6,
1392 	.owner		   = THIS_MODULE,
1393 	.release	   = inet6_release,
1394 	.bind		   = mptcp_bind,
1395 	.connect	   = mptcp_stream_connect,
1396 	.socketpair	   = sock_no_socketpair,
1397 	.accept		   = mptcp_stream_accept,
1398 	.getname	   = mptcp_v6_getname,
1399 	.poll		   = mptcp_poll,
1400 	.ioctl		   = inet6_ioctl,
1401 	.gettstamp	   = sock_gettstamp,
1402 	.listen		   = mptcp_listen,
1403 	.shutdown	   = mptcp_shutdown,
1404 	.setsockopt	   = sock_common_setsockopt,
1405 	.getsockopt	   = sock_common_getsockopt,
1406 	.sendmsg	   = inet6_sendmsg,
1407 	.recvmsg	   = inet6_recvmsg,
1408 	.mmap		   = sock_no_mmap,
1409 	.sendpage	   = inet_sendpage,
1410 #ifdef CONFIG_COMPAT
1411 	.compat_setsockopt = compat_sock_common_setsockopt,
1412 	.compat_getsockopt = compat_sock_common_getsockopt,
1413 #endif
1414 };
1415 
1416 static struct proto mptcp_v6_prot;
1417 
1418 static void mptcp_v6_destroy(struct sock *sk)
1419 {
1420 	mptcp_destroy(sk);
1421 	inet6_destroy_sock(sk);
1422 }
1423 
1424 static struct inet_protosw mptcp_v6_protosw = {
1425 	.type		= SOCK_STREAM,
1426 	.protocol	= IPPROTO_MPTCP,
1427 	.prot		= &mptcp_v6_prot,
1428 	.ops		= &mptcp_v6_stream_ops,
1429 	.flags		= INET_PROTOSW_ICSK,
1430 };
1431 
1432 int mptcp_proto_v6_init(void)
1433 {
1434 	int err;
1435 
1436 	mptcp_v6_prot = mptcp_prot;
1437 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
1438 	mptcp_v6_prot.slab = NULL;
1439 	mptcp_v6_prot.destroy = mptcp_v6_destroy;
1440 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
1441 
1442 	err = proto_register(&mptcp_v6_prot, 1);
1443 	if (err)
1444 		return err;
1445 
1446 	err = inet6_register_protosw(&mptcp_v6_protosw);
1447 	if (err)
1448 		proto_unregister(&mptcp_v6_prot);
1449 
1450 	return err;
1451 }
1452 #endif
1453