Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 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> |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
| 13 | #include <linux/atomic.h> |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 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> |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 19 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 20 | #include <net/transp_v6.h> |
| 21 | #endif |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 22 | #include <net/mptcp.h> |
| 23 | #include "protocol.h" |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 24 | #include "mib.h" |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 25 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 26 | #define MPTCP_SAME_STATE TCP_MAX_STATES |
| 27 | |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 28 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 29 | struct mptcp6_sock { |
| 30 | struct mptcp_sock msk; |
| 31 | struct ipv6_pinfo np; |
| 32 | }; |
| 33 | #endif |
| 34 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 35 | struct mptcp_skb_cb { |
| 36 | u32 offset; |
| 37 | }; |
| 38 | |
| 39 | #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) |
| 40 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 41 | static struct percpu_counter mptcp_sockets_allocated; |
| 42 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 43 | /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not |
| 44 | * completed yet or has failed, return the subflow socket. |
| 45 | * Otherwise return NULL. |
| 46 | */ |
| 47 | static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) |
| 48 | { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 49 | if (!msk->subflow || READ_ONCE(msk->can_ack)) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 50 | return NULL; |
| 51 | |
| 52 | return msk->subflow; |
| 53 | } |
| 54 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 55 | static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk) |
| 56 | { |
| 57 | return msk->first && !sk_is_mptcp(msk->first); |
| 58 | } |
| 59 | |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 60 | static struct socket *mptcp_is_tcpsk(struct sock *sk) |
| 61 | { |
| 62 | struct socket *sock = sk->sk_socket; |
| 63 | |
| 64 | if (sock->sk != sk) |
| 65 | return NULL; |
| 66 | |
| 67 | if (unlikely(sk->sk_prot == &tcp_prot)) { |
| 68 | /* we are being invoked after mptcp_accept() has |
| 69 | * accepted a non-mp-capable flow: sk is a tcp_sk, |
| 70 | * not an mptcp one. |
| 71 | * |
| 72 | * Hand the socket over to tcp so all further socket ops |
| 73 | * bypass mptcp. |
| 74 | */ |
| 75 | sock->ops = &inet_stream_ops; |
| 76 | return sock; |
| 77 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 78 | } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { |
| 79 | sock->ops = &inet6_stream_ops; |
| 80 | return sock; |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 87 | static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 88 | { |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 89 | struct socket *sock; |
| 90 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 91 | sock_owned_by_me((const struct sock *)msk); |
| 92 | |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 93 | sock = mptcp_is_tcpsk((struct sock *)msk); |
| 94 | if (unlikely(sock)) |
| 95 | return sock; |
| 96 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 97 | if (likely(!__mptcp_needs_tcp_fallback(msk))) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 98 | return NULL; |
| 99 | |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 100 | return msk->subflow; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 103 | static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk) |
| 104 | { |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 105 | return !msk->first; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) |
| 109 | { |
| 110 | struct mptcp_subflow_context *subflow; |
| 111 | struct sock *sk = (struct sock *)msk; |
| 112 | struct socket *ssock; |
| 113 | int err; |
| 114 | |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 115 | ssock = __mptcp_tcp_fallback(msk); |
| 116 | if (unlikely(ssock)) |
| 117 | return ssock; |
| 118 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 119 | ssock = __mptcp_nmpc_socket(msk); |
| 120 | if (ssock) |
| 121 | goto set_state; |
| 122 | |
| 123 | if (!__mptcp_can_create_subflow(msk)) |
| 124 | return ERR_PTR(-EINVAL); |
| 125 | |
| 126 | err = mptcp_subflow_create_socket(sk, &ssock); |
| 127 | if (err) |
| 128 | return ERR_PTR(err); |
| 129 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 130 | msk->first = ssock->sk; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 131 | msk->subflow = ssock; |
| 132 | subflow = mptcp_subflow_ctx(ssock->sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 133 | list_add(&subflow->node, &msk->conn_list); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 134 | subflow->request_mptcp = 1; |
| 135 | |
| 136 | set_state: |
| 137 | if (state != MPTCP_SAME_STATE) |
| 138 | inet_sk_state_store(sk, state); |
| 139 | return ssock; |
| 140 | } |
| 141 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 142 | static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, |
| 143 | struct sk_buff *skb, |
| 144 | unsigned int offset, size_t copy_len) |
| 145 | { |
| 146 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 147 | struct sk_buff *tail; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 148 | |
| 149 | __skb_unlink(skb, &ssk->sk_receive_queue); |
Florian Westphal | 4e637c7 | 2020-05-25 23:41:13 +0200 | [diff] [blame] | 150 | |
| 151 | skb_ext_reset(skb); |
| 152 | skb_orphan(skb); |
| 153 | msk->ack_seq += copy_len; |
| 154 | |
| 155 | tail = skb_peek_tail(&sk->sk_receive_queue); |
| 156 | if (offset == 0 && tail) { |
| 157 | bool fragstolen; |
| 158 | int delta; |
| 159 | |
| 160 | if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) { |
| 161 | kfree_skb_partial(skb, fragstolen); |
| 162 | atomic_add(delta, &sk->sk_rmem_alloc); |
| 163 | sk_mem_charge(sk, delta); |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 168 | skb_set_owner_r(skb, sk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 169 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 170 | MPTCP_SKB_CB(skb)->offset = offset; |
| 171 | } |
| 172 | |
Florian Westphal | de06f57 | 2020-04-02 13:44:53 +0200 | [diff] [blame] | 173 | /* both sockets must be locked */ |
| 174 | static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, |
| 175 | struct sock *ssk) |
| 176 | { |
| 177 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 178 | u64 dsn = mptcp_subflow_get_mapped_dsn(subflow); |
| 179 | |
| 180 | /* revalidate data sequence number. |
| 181 | * |
| 182 | * mptcp_subflow_data_available() is usually called |
| 183 | * without msk lock. Its unlikely (but possible) |
| 184 | * that msk->ack_seq has been advanced since the last |
| 185 | * call found in-sequence data. |
| 186 | */ |
| 187 | if (likely(dsn == msk->ack_seq)) |
| 188 | return true; |
| 189 | |
| 190 | subflow->data_avail = 0; |
| 191 | return mptcp_subflow_data_available(ssk); |
| 192 | } |
| 193 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 194 | static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, |
| 195 | struct sock *ssk, |
| 196 | unsigned int *bytes) |
| 197 | { |
| 198 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 199 | struct sock *sk = (struct sock *)msk; |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 200 | unsigned int moved = 0; |
| 201 | bool more_data_avail; |
| 202 | struct tcp_sock *tp; |
| 203 | bool done = false; |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 204 | |
Florian Westphal | de06f57 | 2020-04-02 13:44:53 +0200 | [diff] [blame] | 205 | if (!mptcp_subflow_dsn_valid(msk, ssk)) { |
| 206 | *bytes = 0; |
| 207 | return false; |
| 208 | } |
| 209 | |
Florian Westphal | ec33916 | 2020-03-06 21:29:46 +0100 | [diff] [blame] | 210 | if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { |
| 211 | int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf); |
| 212 | |
| 213 | if (rcvbuf > sk->sk_rcvbuf) |
| 214 | sk->sk_rcvbuf = rcvbuf; |
| 215 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 216 | |
| 217 | tp = tcp_sk(ssk); |
| 218 | do { |
| 219 | u32 map_remaining, offset; |
| 220 | u32 seq = tp->copied_seq; |
| 221 | struct sk_buff *skb; |
| 222 | bool fin; |
| 223 | |
| 224 | /* try to move as much data as available */ |
| 225 | map_remaining = subflow->map_data_len - |
| 226 | mptcp_subflow_get_map_offset(subflow); |
| 227 | |
| 228 | skb = skb_peek(&ssk->sk_receive_queue); |
| 229 | if (!skb) |
| 230 | break; |
| 231 | |
| 232 | offset = seq - TCP_SKB_CB(skb)->seq; |
| 233 | fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; |
| 234 | if (fin) { |
| 235 | done = true; |
| 236 | seq++; |
| 237 | } |
| 238 | |
| 239 | if (offset < skb->len) { |
| 240 | size_t len = skb->len - offset; |
| 241 | |
| 242 | if (tp->urg_data) |
| 243 | done = true; |
| 244 | |
| 245 | __mptcp_move_skb(msk, ssk, skb, offset, len); |
| 246 | seq += len; |
| 247 | moved += len; |
| 248 | |
| 249 | if (WARN_ON_ONCE(map_remaining < len)) |
| 250 | break; |
| 251 | } else { |
| 252 | WARN_ON_ONCE(!fin); |
| 253 | sk_eat_skb(ssk, skb); |
| 254 | done = true; |
| 255 | } |
| 256 | |
| 257 | WRITE_ONCE(tp->copied_seq, seq); |
| 258 | more_data_avail = mptcp_subflow_data_available(ssk); |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 259 | |
| 260 | if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) { |
| 261 | done = true; |
| 262 | break; |
| 263 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 264 | } while (more_data_avail); |
| 265 | |
| 266 | *bytes = moved; |
| 267 | |
| 268 | return done; |
| 269 | } |
| 270 | |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 271 | /* In most cases we will be able to lock the mptcp socket. If its already |
| 272 | * owned, we need to defer to the work queue to avoid ABBA deadlock. |
| 273 | */ |
| 274 | static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) |
| 275 | { |
| 276 | struct sock *sk = (struct sock *)msk; |
| 277 | unsigned int moved = 0; |
| 278 | |
| 279 | if (READ_ONCE(sk->sk_lock.owned)) |
| 280 | return false; |
| 281 | |
| 282 | if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) |
| 283 | return false; |
| 284 | |
| 285 | /* must re-check after taking the lock */ |
| 286 | if (!READ_ONCE(sk->sk_lock.owned)) |
| 287 | __mptcp_move_skbs_from_subflow(msk, ssk, &moved); |
| 288 | |
| 289 | spin_unlock_bh(&sk->sk_lock.slock); |
| 290 | |
| 291 | return moved > 0; |
| 292 | } |
| 293 | |
| 294 | void mptcp_data_ready(struct sock *sk, struct sock *ssk) |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 295 | { |
| 296 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 297 | |
| 298 | set_bit(MPTCP_DATA_READY, &msk->flags); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 299 | |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 300 | if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) && |
| 301 | move_skbs_to_msk(msk, ssk)) |
| 302 | goto wake; |
| 303 | |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 304 | /* don't schedule if mptcp sk is (still) over limit */ |
| 305 | if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) |
| 306 | goto wake; |
| 307 | |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 308 | /* mptcp socket is owned, release_cb should retry */ |
| 309 | if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, |
| 310 | &sk->sk_tsq_flags)) { |
| 311 | sock_hold(sk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 312 | |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 313 | /* need to try again, its possible release_cb() has already |
| 314 | * been called after the test_and_set_bit() above. |
| 315 | */ |
| 316 | move_skbs_to_msk(msk, ssk); |
| 317 | } |
Florian Westphal | 600911f | 2020-02-26 10:14:49 +0100 | [diff] [blame] | 318 | wake: |
Florian Westphal | 101f6f8 | 2020-02-26 10:14:46 +0100 | [diff] [blame] | 319 | sk->sk_data_ready(sk); |
| 320 | } |
| 321 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 322 | static void __mptcp_flush_join_list(struct mptcp_sock *msk) |
| 323 | { |
| 324 | if (likely(list_empty(&msk->join_list))) |
| 325 | return; |
| 326 | |
| 327 | spin_lock_bh(&msk->join_list_lock); |
| 328 | list_splice_tail_init(&msk->join_list, &msk->conn_list); |
| 329 | spin_unlock_bh(&msk->join_list_lock); |
| 330 | } |
| 331 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 332 | static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) |
| 333 | { |
| 334 | long tout = ssk && inet_csk(ssk)->icsk_pending ? |
| 335 | inet_csk(ssk)->icsk_timeout - jiffies : 0; |
| 336 | |
| 337 | if (tout <= 0) |
| 338 | tout = mptcp_sk(sk)->timer_ival; |
| 339 | mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; |
| 340 | } |
| 341 | |
| 342 | static bool mptcp_timer_pending(struct sock *sk) |
| 343 | { |
| 344 | return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); |
| 345 | } |
| 346 | |
| 347 | static void mptcp_reset_timer(struct sock *sk) |
| 348 | { |
| 349 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 350 | unsigned long tout; |
| 351 | |
| 352 | /* should never be called with mptcp level timer cleared */ |
| 353 | tout = READ_ONCE(mptcp_sk(sk)->timer_ival); |
| 354 | if (WARN_ON_ONCE(!tout)) |
| 355 | tout = TCP_RTO_MIN; |
| 356 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); |
| 357 | } |
| 358 | |
| 359 | void mptcp_data_acked(struct sock *sk) |
| 360 | { |
| 361 | mptcp_reset_timer(sk); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 362 | |
| 363 | if (!sk_stream_is_writeable(sk) && |
| 364 | schedule_work(&mptcp_sk(sk)->work)) |
| 365 | sock_hold(sk); |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 368 | void mptcp_subflow_eof(struct sock *sk) |
| 369 | { |
| 370 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 371 | |
| 372 | if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) && |
| 373 | schedule_work(&msk->work)) |
| 374 | sock_hold(sk); |
| 375 | } |
| 376 | |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame^] | 377 | static void mptcp_check_for_eof(struct mptcp_sock *msk) |
| 378 | { |
| 379 | struct mptcp_subflow_context *subflow; |
| 380 | struct sock *sk = (struct sock *)msk; |
| 381 | int receivers = 0; |
| 382 | |
| 383 | mptcp_for_each_subflow(msk, subflow) |
| 384 | receivers += !subflow->rx_eof; |
| 385 | |
| 386 | if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 387 | /* hopefully temporary hack: propagate shutdown status |
| 388 | * to msk, when all subflows agree on it |
| 389 | */ |
| 390 | sk->sk_shutdown |= RCV_SHUTDOWN; |
| 391 | |
| 392 | smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ |
| 393 | set_bit(MPTCP_DATA_READY, &msk->flags); |
| 394 | sk->sk_data_ready(sk); |
| 395 | } |
| 396 | } |
| 397 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 398 | static void mptcp_stop_timer(struct sock *sk) |
| 399 | { |
| 400 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 401 | |
| 402 | sk_stop_timer(sk, &icsk->icsk_retransmit_timer); |
| 403 | mptcp_sk(sk)->timer_ival = 0; |
| 404 | } |
| 405 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 406 | static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) |
| 407 | { |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 408 | const struct sock *sk = (const struct sock *)msk; |
| 409 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 410 | if (!msk->cached_ext) |
Florian Westphal | 4930f48 | 2020-05-16 10:46:23 +0200 | [diff] [blame] | 411 | msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 412 | |
| 413 | return !!msk->cached_ext; |
| 414 | } |
| 415 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 416 | static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) |
| 417 | { |
| 418 | struct mptcp_subflow_context *subflow; |
| 419 | struct sock *sk = (struct sock *)msk; |
| 420 | |
| 421 | sock_owned_by_me(sk); |
| 422 | |
| 423 | mptcp_for_each_subflow(msk, subflow) { |
| 424 | if (subflow->data_avail) |
| 425 | return mptcp_subflow_tcp_sock(subflow); |
| 426 | } |
| 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 431 | static bool mptcp_skb_can_collapse_to(u64 write_seq, |
| 432 | const struct sk_buff *skb, |
| 433 | const struct mptcp_ext *mpext) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 434 | { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 435 | if (!tcp_skb_can_collapse_to(skb)) |
| 436 | return false; |
| 437 | |
| 438 | /* can collapse only if MPTCP level sequence is in order */ |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 439 | return mpext && mpext->data_seq + mpext->data_len == write_seq; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 442 | static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, |
| 443 | const struct page_frag *pfrag, |
| 444 | const struct mptcp_data_frag *df) |
| 445 | { |
| 446 | return df && pfrag->page == df->page && |
| 447 | df->data_seq + df->data_len == msk->write_seq; |
| 448 | } |
| 449 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 450 | static void dfrag_uncharge(struct sock *sk, int len) |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 451 | { |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 452 | sk_mem_uncharge(sk, len); |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 453 | sk_wmem_queued_add(sk, -len); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) |
| 457 | { |
| 458 | int len = dfrag->data_len + dfrag->overhead; |
| 459 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 460 | list_del(&dfrag->list); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 461 | dfrag_uncharge(sk, len); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 462 | put_page(dfrag->page); |
| 463 | } |
| 464 | |
| 465 | static void mptcp_clean_una(struct sock *sk) |
| 466 | { |
| 467 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 468 | struct mptcp_data_frag *dtmp, *dfrag; |
| 469 | u64 snd_una = atomic64_read(&msk->snd_una); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 470 | bool cleaned = false; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 471 | |
| 472 | list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { |
| 473 | if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) |
| 474 | break; |
| 475 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 476 | dfrag_clear(sk, dfrag); |
| 477 | cleaned = true; |
| 478 | } |
| 479 | |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 480 | dfrag = mptcp_rtx_head(sk); |
| 481 | if (dfrag && after64(snd_una, dfrag->data_seq)) { |
| 482 | u64 delta = dfrag->data_seq + dfrag->data_len - snd_una; |
| 483 | |
| 484 | dfrag->data_seq += delta; |
| 485 | dfrag->data_len -= delta; |
| 486 | |
| 487 | dfrag_uncharge(sk, delta); |
| 488 | cleaned = true; |
| 489 | } |
| 490 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 491 | if (cleaned) { |
| 492 | sk_mem_reclaim_partial(sk); |
Florian Westphal | 7948f6c | 2020-03-27 14:48:46 -0700 | [diff] [blame] | 493 | |
| 494 | /* Only wake up writers if a subflow is ready */ |
| 495 | if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) |
| 496 | sk_stream_write_space(sk); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /* ensure we get enough memory for the frag hdr, beyond some minimal amount of |
| 501 | * data |
| 502 | */ |
| 503 | static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) |
| 504 | { |
| 505 | if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), |
| 506 | pfrag, sk->sk_allocation))) |
| 507 | return true; |
| 508 | |
| 509 | sk->sk_prot->enter_memory_pressure(sk); |
| 510 | sk_stream_moderate_sndbuf(sk); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | static struct mptcp_data_frag * |
| 515 | mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, |
| 516 | int orig_offset) |
| 517 | { |
| 518 | int offset = ALIGN(orig_offset, sizeof(long)); |
| 519 | struct mptcp_data_frag *dfrag; |
| 520 | |
| 521 | dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); |
| 522 | dfrag->data_len = 0; |
| 523 | dfrag->data_seq = msk->write_seq; |
| 524 | dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); |
| 525 | dfrag->offset = offset + sizeof(struct mptcp_data_frag); |
| 526 | dfrag->page = pfrag->page; |
| 527 | |
| 528 | return dfrag; |
| 529 | } |
| 530 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 531 | static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 532 | struct msghdr *msg, struct mptcp_data_frag *dfrag, |
| 533 | long *timeo, int *pmss_now, |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 534 | int *ps_goal) |
| 535 | { |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 536 | int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0; |
| 537 | bool dfrag_collapsed, can_collapse = false; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 538 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 539 | struct mptcp_ext *mpext = NULL; |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 540 | bool retransmission = !!dfrag; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 541 | struct sk_buff *skb, *tail; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 542 | struct page_frag *pfrag; |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 543 | struct page *page; |
| 544 | u64 *write_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 545 | size_t psize; |
| 546 | |
| 547 | /* use the mptcp page cache so that we can easily move the data |
| 548 | * from one substream to another, but do per subflow memory accounting |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 549 | * Note: pfrag is used only !retransmission, but the compiler if |
| 550 | * fooled into a warning if we don't init here |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 551 | */ |
| 552 | pfrag = sk_page_frag(sk); |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 553 | if (!retransmission) { |
| 554 | write_seq = &msk->write_seq; |
| 555 | page = pfrag->page; |
| 556 | } else { |
| 557 | write_seq = &dfrag->data_seq; |
| 558 | page = dfrag->page; |
| 559 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 560 | |
| 561 | /* compute copy limit */ |
| 562 | mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 563 | *pmss_now = mss_now; |
| 564 | *ps_goal = size_goal; |
| 565 | avail_size = size_goal; |
| 566 | skb = tcp_write_queue_tail(ssk); |
| 567 | if (skb) { |
| 568 | mpext = skb_ext_find(skb, SKB_EXT_MPTCP); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 569 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 570 | /* Limit the write to the size available in the |
| 571 | * current skb, if any, so that we create at most a new skb. |
| 572 | * Explicitly tells TCP internals to avoid collapsing on later |
| 573 | * queue management operation, to avoid breaking the ext <-> |
| 574 | * SSN association set here |
| 575 | */ |
| 576 | can_collapse = (size_goal - skb->len > 0) && |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 577 | mptcp_skb_can_collapse_to(*write_seq, skb, mpext); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 578 | if (!can_collapse) |
| 579 | TCP_SKB_CB(skb)->eor = 1; |
| 580 | else |
| 581 | avail_size = size_goal - skb->len; |
| 582 | } |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 583 | |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 584 | if (!retransmission) { |
| 585 | /* reuse tail pfrag, if possible, or carve a new one from the |
| 586 | * page allocator |
| 587 | */ |
| 588 | dfrag = mptcp_rtx_tail(sk); |
| 589 | offset = pfrag->offset; |
| 590 | dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); |
| 591 | if (!dfrag_collapsed) { |
| 592 | dfrag = mptcp_carve_data_frag(msk, pfrag, offset); |
| 593 | offset = dfrag->offset; |
| 594 | frag_truesize = dfrag->overhead; |
| 595 | } |
| 596 | psize = min_t(size_t, pfrag->size - offset, avail_size); |
| 597 | |
| 598 | /* Copy to page */ |
| 599 | pr_debug("left=%zu", msg_data_left(msg)); |
| 600 | psize = copy_page_from_iter(pfrag->page, offset, |
| 601 | min_t(size_t, msg_data_left(msg), |
| 602 | psize), |
| 603 | &msg->msg_iter); |
| 604 | pr_debug("left=%zu", msg_data_left(msg)); |
| 605 | if (!psize) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) |
| 609 | return -ENOMEM; |
| 610 | } else { |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 611 | offset = dfrag->offset; |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 612 | psize = min_t(size_t, dfrag->data_len, avail_size); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 613 | } |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 614 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 615 | /* tell the TCP stack to delay the push so that we can safely |
| 616 | * access the skb after the sendpages call |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 617 | */ |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 618 | ret = do_tcp_sendpages(ssk, page, offset, psize, |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 619 | msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 620 | if (ret <= 0) |
| 621 | return ret; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 622 | |
| 623 | frag_truesize += ret; |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 624 | if (!retransmission) { |
| 625 | if (unlikely(ret < psize)) |
| 626 | iov_iter_revert(&msg->msg_iter, psize - ret); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 627 | |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 628 | /* send successful, keep track of sent data for mptcp-level |
| 629 | * retransmission |
| 630 | */ |
| 631 | dfrag->data_len += ret; |
| 632 | if (!dfrag_collapsed) { |
| 633 | get_page(dfrag->page); |
| 634 | list_add_tail(&dfrag->list, &msk->rtx_queue); |
| 635 | sk_wmem_queued_add(sk, frag_truesize); |
| 636 | } else { |
| 637 | sk_wmem_queued_add(sk, ret); |
| 638 | } |
| 639 | |
| 640 | /* charge data on mptcp rtx queue to the master socket |
| 641 | * Note: we charge such data both to sk and ssk |
| 642 | */ |
| 643 | sk->sk_forward_alloc -= frag_truesize; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 646 | /* if the tail skb extension is still the cached one, collapsing |
| 647 | * really happened. Note: we can't check for 'same skb' as the sk_buff |
| 648 | * hdr on tail can be transmitted, freed and re-allocated by the |
| 649 | * do_tcp_sendpages() call |
| 650 | */ |
| 651 | tail = tcp_write_queue_tail(ssk); |
| 652 | if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { |
| 653 | WARN_ON_ONCE(!can_collapse); |
| 654 | mpext->data_len += ret; |
| 655 | goto out; |
| 656 | } |
| 657 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 658 | skb = tcp_write_queue_tail(ssk); |
| 659 | mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); |
| 660 | msk->cached_ext = NULL; |
| 661 | |
| 662 | memset(mpext, 0, sizeof(*mpext)); |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 663 | mpext->data_seq = *write_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 664 | mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; |
| 665 | mpext->data_len = ret; |
| 666 | mpext->use_map = 1; |
| 667 | mpext->dsn64 = 1; |
| 668 | |
| 669 | pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", |
| 670 | mpext->data_seq, mpext->subflow_seq, mpext->data_len, |
| 671 | mpext->dsn64); |
| 672 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 673 | out: |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 674 | if (!retransmission) |
| 675 | pfrag->offset += frag_truesize; |
| 676 | *write_seq += ret; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 677 | mptcp_subflow_ctx(ssk)->rel_write_seq += ret; |
| 678 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 679 | return ret; |
| 680 | } |
| 681 | |
Florian Westphal | a0e1706 | 2020-05-16 10:46:17 +0200 | [diff] [blame] | 682 | static void mptcp_nospace(struct mptcp_sock *msk, struct socket *sock) |
| 683 | { |
| 684 | clear_bit(MPTCP_SEND_SPACE, &msk->flags); |
| 685 | smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ |
| 686 | |
| 687 | /* enables sk->write_space() callbacks */ |
| 688 | set_bit(SOCK_NOSPACE, &sock->flags); |
| 689 | } |
| 690 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 691 | static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) |
| 692 | { |
| 693 | struct mptcp_subflow_context *subflow; |
| 694 | struct sock *backup = NULL; |
| 695 | |
| 696 | sock_owned_by_me((const struct sock *)msk); |
| 697 | |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 698 | if (!mptcp_ext_cache_refill(msk)) |
| 699 | return NULL; |
| 700 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 701 | mptcp_for_each_subflow(msk, subflow) { |
| 702 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 703 | |
| 704 | if (!sk_stream_memory_free(ssk)) { |
| 705 | struct socket *sock = ssk->sk_socket; |
| 706 | |
Florian Westphal | a0e1706 | 2020-05-16 10:46:17 +0200 | [diff] [blame] | 707 | if (sock) |
| 708 | mptcp_nospace(msk, sock); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 709 | |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | if (subflow->backup) { |
| 714 | if (!backup) |
| 715 | backup = ssk; |
| 716 | |
| 717 | continue; |
| 718 | } |
| 719 | |
| 720 | return ssk; |
| 721 | } |
| 722 | |
| 723 | return backup; |
| 724 | } |
| 725 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 726 | static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) |
| 727 | { |
| 728 | struct socket *sock; |
| 729 | |
| 730 | if (likely(sk_stream_is_writeable(ssk))) |
| 731 | return; |
| 732 | |
| 733 | sock = READ_ONCE(ssk->sk_socket); |
Florian Westphal | a0e1706 | 2020-05-16 10:46:17 +0200 | [diff] [blame] | 734 | if (sock) |
| 735 | mptcp_nospace(msk, sock); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 738 | static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
| 739 | { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 740 | int mss_now = 0, size_goal = 0, ret = 0; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 741 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 742 | struct page_frag *pfrag; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 743 | struct socket *ssock; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 744 | size_t copied = 0; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 745 | struct sock *ssk; |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 746 | bool tx_ok; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 747 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 748 | |
| 749 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) |
| 750 | return -EOPNOTSUPP; |
| 751 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 752 | lock_sock(sk); |
Mat Martineau | 1954b86 | 2020-02-28 15:47:39 -0800 | [diff] [blame] | 753 | |
| 754 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 755 | |
| 756 | if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { |
| 757 | ret = sk_stream_wait_connect(sk, &timeo); |
| 758 | if (ret) |
| 759 | goto out; |
| 760 | } |
| 761 | |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 762 | fallback: |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 763 | ssock = __mptcp_tcp_fallback(msk); |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 764 | if (unlikely(ssock)) { |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 765 | release_sock(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 766 | pr_debug("fallback passthrough"); |
| 767 | ret = sock_sendmsg(ssock, msg); |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 768 | return ret >= 0 ? ret + copied : (copied ? copied : ret); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 771 | pfrag = sk_page_frag(sk); |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 772 | restart: |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 773 | mptcp_clean_una(sk); |
| 774 | |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 775 | wait_for_sndbuf: |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 776 | __mptcp_flush_join_list(msk); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 777 | ssk = mptcp_subflow_get_send(msk); |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 778 | while (!sk_stream_memory_free(sk) || |
| 779 | !ssk || |
| 780 | !mptcp_page_frag_refill(ssk, pfrag)) { |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 781 | if (ssk) { |
| 782 | /* make sure retransmit timer is |
| 783 | * running before we wait for memory. |
| 784 | * |
| 785 | * The retransmit timer might be needed |
| 786 | * to make the peer send an up-to-date |
| 787 | * MPTCP Ack. |
| 788 | */ |
| 789 | mptcp_set_timeout(sk, ssk); |
| 790 | if (!mptcp_timer_pending(sk)) |
| 791 | mptcp_reset_timer(sk); |
| 792 | } |
| 793 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 794 | ret = sk_stream_wait_memory(sk, &timeo); |
| 795 | if (ret) |
| 796 | goto out; |
| 797 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 798 | mptcp_clean_una(sk); |
| 799 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 800 | ssk = mptcp_subflow_get_send(msk); |
| 801 | if (list_empty(&msk->conn_list)) { |
| 802 | ret = -ENOTCONN; |
| 803 | goto out; |
| 804 | } |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 807 | pr_debug("conn_list->subflow=%p", ssk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 808 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 809 | lock_sock(ssk); |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 810 | tx_ok = msg_data_left(msg); |
| 811 | while (tx_ok) { |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 812 | ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now, |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 813 | &size_goal); |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 814 | if (ret < 0) { |
| 815 | if (ret == -EAGAIN && timeo > 0) { |
| 816 | mptcp_set_timeout(sk, ssk); |
| 817 | release_sock(ssk); |
| 818 | goto restart; |
| 819 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 820 | break; |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 821 | } |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 822 | if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) { |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 823 | /* Can happen for passive sockets: |
| 824 | * 3WHS negotiated MPTCP, but first packet after is |
| 825 | * plain TCP (e.g. due to middlebox filtering unknown |
| 826 | * options). |
| 827 | * |
| 828 | * Fall back to TCP. |
| 829 | */ |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 830 | release_sock(ssk); |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 831 | goto fallback; |
| 832 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 833 | |
| 834 | copied += ret; |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 835 | |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 836 | tx_ok = msg_data_left(msg); |
| 837 | if (!tx_ok) |
| 838 | break; |
| 839 | |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 840 | if (!sk_stream_memory_free(ssk) || |
Florian Westphal | 1709170 | 2020-05-16 10:46:21 +0200 | [diff] [blame] | 841 | !mptcp_page_frag_refill(ssk, pfrag) || |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 842 | !mptcp_ext_cache_refill(msk)) { |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 843 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 844 | tcp_push(ssk, msg->msg_flags, mss_now, |
| 845 | tcp_sk(ssk)->nonagle, size_goal); |
| 846 | mptcp_set_timeout(sk, ssk); |
| 847 | release_sock(ssk); |
| 848 | goto restart; |
| 849 | } |
| 850 | |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 851 | /* memory is charged to mptcp level socket as well, i.e. |
| 852 | * if msg is very large, mptcp socket may run out of buffer |
| 853 | * space. mptcp_clean_una() will release data that has |
| 854 | * been acked at mptcp level in the mean time, so there is |
| 855 | * a good chance we can continue sending data right away. |
Florian Westphal | 72511aab | 2020-05-16 10:46:19 +0200 | [diff] [blame] | 856 | * |
| 857 | * Normally, when the tcp subflow can accept more data, then |
| 858 | * so can the MPTCP socket. However, we need to cope with |
| 859 | * peers that might lag behind in their MPTCP-level |
| 860 | * acknowledgements, i.e. data might have been acked at |
| 861 | * tcp level only. So, we must also check the MPTCP socket |
| 862 | * limits before we send more data. |
Florian Westphal | fb529e6 | 2020-05-16 10:46:18 +0200 | [diff] [blame] | 863 | */ |
| 864 | if (unlikely(!sk_stream_memory_free(sk))) { |
| 865 | tcp_push(ssk, msg->msg_flags, mss_now, |
| 866 | tcp_sk(ssk)->nonagle, size_goal); |
| 867 | mptcp_clean_una(sk); |
| 868 | if (!sk_stream_memory_free(sk)) { |
| 869 | /* can't send more for now, need to wait for |
| 870 | * MPTCP-level ACKs from peer. |
| 871 | * |
| 872 | * Wakeup will happen via mptcp_clean_una(). |
| 873 | */ |
| 874 | mptcp_set_timeout(sk, ssk); |
| 875 | release_sock(ssk); |
| 876 | goto wait_for_sndbuf; |
| 877 | } |
| 878 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 881 | mptcp_set_timeout(sk, ssk); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 882 | if (copied) { |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 883 | ret = copied; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 884 | tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, |
| 885 | size_goal); |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 886 | |
| 887 | /* start the timer, if it's not pending */ |
| 888 | if (!mptcp_timer_pending(sk)) |
| 889 | mptcp_reset_timer(sk); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 890 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 891 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 892 | ssk_check_wmem(msk, ssk); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 893 | release_sock(ssk); |
Mat Martineau | 1954b86 | 2020-02-28 15:47:39 -0800 | [diff] [blame] | 894 | out: |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 895 | release_sock(sk); |
| 896 | return ret; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 899 | static void mptcp_wait_data(struct sock *sk, long *timeo) |
| 900 | { |
| 901 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 902 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 903 | |
| 904 | add_wait_queue(sk_sleep(sk), &wait); |
| 905 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 906 | |
| 907 | sk_wait_event(sk, timeo, |
| 908 | test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); |
| 909 | |
| 910 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 911 | remove_wait_queue(sk_sleep(sk), &wait); |
| 912 | } |
| 913 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 914 | static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, |
| 915 | struct msghdr *msg, |
| 916 | size_t len) |
| 917 | { |
| 918 | struct sock *sk = (struct sock *)msk; |
| 919 | struct sk_buff *skb; |
| 920 | int copied = 0; |
| 921 | |
| 922 | while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { |
| 923 | u32 offset = MPTCP_SKB_CB(skb)->offset; |
| 924 | u32 data_len = skb->len - offset; |
| 925 | u32 count = min_t(size_t, len - copied, data_len); |
| 926 | int err; |
| 927 | |
| 928 | err = skb_copy_datagram_msg(skb, offset, msg, count); |
| 929 | if (unlikely(err < 0)) { |
| 930 | if (!copied) |
| 931 | return err; |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | copied += count; |
| 936 | |
| 937 | if (count < data_len) { |
| 938 | MPTCP_SKB_CB(skb)->offset += count; |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 943 | __kfree_skb(skb); |
| 944 | |
| 945 | if (copied >= len) |
| 946 | break; |
| 947 | } |
| 948 | |
| 949 | return copied; |
| 950 | } |
| 951 | |
| 952 | static bool __mptcp_move_skbs(struct mptcp_sock *msk) |
| 953 | { |
| 954 | unsigned int moved = 0; |
| 955 | bool done; |
| 956 | |
| 957 | do { |
| 958 | struct sock *ssk = mptcp_subflow_recv_lookup(msk); |
| 959 | |
| 960 | if (!ssk) |
| 961 | break; |
| 962 | |
| 963 | lock_sock(ssk); |
| 964 | done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); |
| 965 | release_sock(ssk); |
| 966 | } while (!done); |
| 967 | |
| 968 | return moved > 0; |
| 969 | } |
| 970 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 971 | static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 972 | int nonblock, int flags, int *addr_len) |
| 973 | { |
| 974 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 975 | struct socket *ssock; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 976 | int copied = 0; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 977 | int target; |
| 978 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 979 | |
| 980 | if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) |
| 981 | return -EOPNOTSUPP; |
| 982 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 983 | lock_sock(sk); |
| 984 | ssock = __mptcp_tcp_fallback(msk); |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 985 | if (unlikely(ssock)) { |
| 986 | fallback: |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 987 | release_sock(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 988 | pr_debug("fallback-read subflow=%p", |
| 989 | mptcp_subflow_ctx(ssock->sk)); |
| 990 | copied = sock_recvmsg(ssock, msg, flags); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 991 | return copied; |
| 992 | } |
| 993 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 994 | timeo = sock_rcvtimeo(sk, nonblock); |
| 995 | |
| 996 | len = min_t(size_t, len, INT_MAX); |
| 997 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 998 | __mptcp_flush_join_list(msk); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 999 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1000 | while (len > (size_t)copied) { |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1001 | int bytes_read; |
| 1002 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1003 | bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); |
| 1004 | if (unlikely(bytes_read < 0)) { |
| 1005 | if (!copied) |
| 1006 | copied = bytes_read; |
| 1007 | goto out_err; |
| 1008 | } |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1009 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1010 | copied += bytes_read; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1011 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1012 | if (skb_queue_empty(&sk->sk_receive_queue) && |
| 1013 | __mptcp_move_skbs(msk)) |
| 1014 | continue; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1015 | |
| 1016 | /* only the master socket status is relevant here. The exit |
| 1017 | * conditions mirror closely tcp_recvmsg() |
| 1018 | */ |
| 1019 | if (copied >= target) |
| 1020 | break; |
| 1021 | |
| 1022 | if (copied) { |
| 1023 | if (sk->sk_err || |
| 1024 | sk->sk_state == TCP_CLOSE || |
| 1025 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 1026 | !timeo || |
| 1027 | signal_pending(current)) |
| 1028 | break; |
| 1029 | } else { |
| 1030 | if (sk->sk_err) { |
| 1031 | copied = sock_error(sk); |
| 1032 | break; |
| 1033 | } |
| 1034 | |
Paolo Abeni | 5969856 | 2020-06-10 10:47:41 +0200 | [diff] [blame^] | 1035 | if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) |
| 1036 | mptcp_check_for_eof(msk); |
| 1037 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1038 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1039 | break; |
| 1040 | |
| 1041 | if (sk->sk_state == TCP_CLOSE) { |
| 1042 | copied = -ENOTCONN; |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | if (!timeo) { |
| 1047 | copied = -EAGAIN; |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | if (signal_pending(current)) { |
| 1052 | copied = sock_intr_errno(timeo); |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | pr_debug("block timeout %ld", timeo); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1058 | mptcp_wait_data(sk, &timeo); |
Paolo Abeni | 0a82e23 | 2020-05-25 16:38:47 +0200 | [diff] [blame] | 1059 | ssock = __mptcp_tcp_fallback(msk); |
| 1060 | if (unlikely(ssock)) |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1061 | goto fallback; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1064 | if (skb_queue_empty(&sk->sk_receive_queue)) { |
| 1065 | /* entire backlog drained, clear DATA_READY. */ |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1066 | clear_bit(MPTCP_DATA_READY, &msk->flags); |
| 1067 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1068 | /* .. race-breaker: ssk might have gotten new data |
| 1069 | * after last __mptcp_move_skbs() returned false. |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1070 | */ |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1071 | if (unlikely(__mptcp_move_skbs(msk))) |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1072 | set_bit(MPTCP_DATA_READY, &msk->flags); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1073 | } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { |
| 1074 | /* data to read but mptcp_wait_data() cleared DATA_READY */ |
| 1075 | set_bit(MPTCP_DATA_READY, &msk->flags); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 1076 | } |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1077 | out_err: |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1078 | release_sock(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1079 | return copied; |
| 1080 | } |
| 1081 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1082 | static void mptcp_retransmit_handler(struct sock *sk) |
| 1083 | { |
| 1084 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1085 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1086 | if (atomic64_read(&msk->snd_una) == msk->write_seq) { |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1087 | mptcp_stop_timer(sk); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1088 | } else { |
| 1089 | set_bit(MPTCP_WORK_RTX, &msk->flags); |
| 1090 | if (schedule_work(&msk->work)) |
| 1091 | sock_hold(sk); |
| 1092 | } |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static void mptcp_retransmit_timer(struct timer_list *t) |
| 1096 | { |
| 1097 | struct inet_connection_sock *icsk = from_timer(icsk, t, |
| 1098 | icsk_retransmit_timer); |
| 1099 | struct sock *sk = &icsk->icsk_inet.sk; |
| 1100 | |
| 1101 | bh_lock_sock(sk); |
| 1102 | if (!sock_owned_by_user(sk)) { |
| 1103 | mptcp_retransmit_handler(sk); |
| 1104 | } else { |
| 1105 | /* delegate our work to tcp_release_cb() */ |
| 1106 | if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, |
| 1107 | &sk->sk_tsq_flags)) |
| 1108 | sock_hold(sk); |
| 1109 | } |
| 1110 | bh_unlock_sock(sk); |
| 1111 | sock_put(sk); |
| 1112 | } |
| 1113 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1114 | /* Find an idle subflow. Return NULL if there is unacked data at tcp |
| 1115 | * level. |
| 1116 | * |
| 1117 | * A backup subflow is returned only if that is the only kind available. |
| 1118 | */ |
| 1119 | static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) |
| 1120 | { |
| 1121 | struct mptcp_subflow_context *subflow; |
| 1122 | struct sock *backup = NULL; |
| 1123 | |
| 1124 | sock_owned_by_me((const struct sock *)msk); |
| 1125 | |
| 1126 | mptcp_for_each_subflow(msk, subflow) { |
| 1127 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 1128 | |
| 1129 | /* still data outstanding at TCP level? Don't retransmit. */ |
| 1130 | if (!tcp_write_queue_empty(ssk)) |
| 1131 | return NULL; |
| 1132 | |
| 1133 | if (subflow->backup) { |
| 1134 | if (!backup) |
| 1135 | backup = ssk; |
| 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | return ssk; |
| 1140 | } |
| 1141 | |
| 1142 | return backup; |
| 1143 | } |
| 1144 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1145 | /* subflow sockets can be either outgoing (connect) or incoming |
| 1146 | * (accept). |
| 1147 | * |
| 1148 | * Outgoing subflows use in-kernel sockets. |
| 1149 | * Incoming subflows do not have their own 'struct socket' allocated, |
| 1150 | * so we need to use tcp_close() after detaching them from the mptcp |
| 1151 | * parent socket. |
| 1152 | */ |
| 1153 | static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, |
| 1154 | struct mptcp_subflow_context *subflow, |
| 1155 | long timeout) |
| 1156 | { |
| 1157 | struct socket *sock = READ_ONCE(ssk->sk_socket); |
| 1158 | |
| 1159 | list_del(&subflow->node); |
| 1160 | |
| 1161 | if (sock && sock != sk->sk_socket) { |
| 1162 | /* outgoing subflow */ |
| 1163 | sock_release(sock); |
| 1164 | } else { |
| 1165 | /* incoming subflow */ |
| 1166 | tcp_close(ssk, timeout); |
| 1167 | } |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Paolo Abeni | dc24f8b | 2020-02-26 12:19:03 +0100 | [diff] [blame] | 1170 | static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) |
| 1171 | { |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1175 | static void mptcp_worker(struct work_struct *work) |
| 1176 | { |
| 1177 | struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1178 | struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 1179 | int orig_len, orig_offset, mss_now = 0, size_goal = 0; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1180 | struct mptcp_data_frag *dfrag; |
| 1181 | u64 orig_write_seq; |
| 1182 | size_t copied = 0; |
| 1183 | struct msghdr msg; |
| 1184 | long timeo = 0; |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1185 | |
| 1186 | lock_sock(sk); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1187 | mptcp_clean_una(sk); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1188 | __mptcp_flush_join_list(msk); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1189 | __mptcp_move_skbs(msk); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1190 | |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 1191 | if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) |
| 1192 | mptcp_check_for_eof(msk); |
| 1193 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1194 | if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) |
| 1195 | goto unlock; |
| 1196 | |
| 1197 | dfrag = mptcp_rtx_head(sk); |
| 1198 | if (!dfrag) |
| 1199 | goto unlock; |
| 1200 | |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 1201 | if (!mptcp_ext_cache_refill(msk)) |
| 1202 | goto reset_unlock; |
| 1203 | |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1204 | ssk = mptcp_subflow_get_retrans(msk); |
| 1205 | if (!ssk) |
| 1206 | goto reset_unlock; |
| 1207 | |
| 1208 | lock_sock(ssk); |
| 1209 | |
| 1210 | msg.msg_flags = MSG_DONTWAIT; |
| 1211 | orig_len = dfrag->data_len; |
| 1212 | orig_offset = dfrag->offset; |
| 1213 | orig_write_seq = dfrag->data_seq; |
| 1214 | while (dfrag->data_len > 0) { |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 1215 | int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, |
| 1216 | &mss_now, &size_goal); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1217 | if (ret < 0) |
| 1218 | break; |
| 1219 | |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1220 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1221 | copied += ret; |
| 1222 | dfrag->data_len -= ret; |
| 1223 | dfrag->offset += ret; |
Florian Westphal | 149f7c7 | 2020-05-16 10:46:20 +0200 | [diff] [blame] | 1224 | |
| 1225 | if (!mptcp_ext_cache_refill(msk)) |
| 1226 | break; |
Paolo Abeni | 3b1d621 | 2020-03-27 14:48:48 -0700 | [diff] [blame] | 1227 | } |
| 1228 | if (copied) |
| 1229 | tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle, |
| 1230 | size_goal); |
| 1231 | |
| 1232 | dfrag->data_seq = orig_write_seq; |
| 1233 | dfrag->offset = orig_offset; |
| 1234 | dfrag->data_len = orig_len; |
| 1235 | |
| 1236 | mptcp_set_timeout(sk, ssk); |
| 1237 | release_sock(ssk); |
| 1238 | |
| 1239 | reset_unlock: |
| 1240 | if (!mptcp_timer_pending(sk)) |
| 1241 | mptcp_reset_timer(sk); |
| 1242 | |
| 1243 | unlock: |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1244 | release_sock(sk); |
| 1245 | sock_put(sk); |
| 1246 | } |
| 1247 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 1248 | static int __mptcp_init_sock(struct sock *sk) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1249 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1250 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1251 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1252 | spin_lock_init(&msk->join_list_lock); |
| 1253 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1254 | INIT_LIST_HEAD(&msk->conn_list); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1255 | INIT_LIST_HEAD(&msk->join_list); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1256 | INIT_LIST_HEAD(&msk->rtx_queue); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1257 | __set_bit(MPTCP_SEND_SPACE, &msk->flags); |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1258 | INIT_WORK(&msk->work, mptcp_worker); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1259 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1260 | msk->first = NULL; |
Paolo Abeni | dc24f8b | 2020-02-26 12:19:03 +0100 | [diff] [blame] | 1261 | inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1262 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1263 | mptcp_pm_data_init(msk); |
| 1264 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1265 | /* re-use the csk retrans timer for MPTCP-level retrans */ |
| 1266 | timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); |
| 1267 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
| 1270 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 1271 | static int mptcp_init_sock(struct sock *sk) |
| 1272 | { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1273 | struct net *net = sock_net(sk); |
| 1274 | int ret; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1275 | |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1276 | if (!mptcp_is_enabled(net)) |
| 1277 | return -ENOPROTOOPT; |
| 1278 | |
| 1279 | if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) |
| 1280 | return -ENOMEM; |
| 1281 | |
| 1282 | ret = __mptcp_init_sock(sk); |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1283 | if (ret) |
| 1284 | return ret; |
| 1285 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1286 | sk_sockets_allocated_inc(sk); |
Paolo Abeni | 3f8e0aa | 2020-03-27 14:48:47 -0700 | [diff] [blame] | 1287 | sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2]; |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1288 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static void __mptcp_clear_xmit(struct sock *sk) |
| 1293 | { |
| 1294 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1295 | struct mptcp_data_frag *dtmp, *dfrag; |
| 1296 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1297 | sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); |
| 1298 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1299 | list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1300 | dfrag_clear(sk, dfrag); |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1303 | static void mptcp_cancel_work(struct sock *sk) |
| 1304 | { |
| 1305 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1306 | |
| 1307 | if (cancel_work_sync(&msk->work)) |
| 1308 | sock_put(sk); |
| 1309 | } |
| 1310 | |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 1311 | static void mptcp_subflow_shutdown(struct sock *ssk, int how, |
| 1312 | bool data_fin_tx_enable, u64 data_fin_tx_seq) |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1313 | { |
| 1314 | lock_sock(ssk); |
| 1315 | |
| 1316 | switch (ssk->sk_state) { |
| 1317 | case TCP_LISTEN: |
| 1318 | if (!(how & RCV_SHUTDOWN)) |
| 1319 | break; |
| 1320 | /* fall through */ |
| 1321 | case TCP_SYN_SENT: |
| 1322 | tcp_disconnect(ssk, O_NONBLOCK); |
| 1323 | break; |
| 1324 | default: |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 1325 | if (data_fin_tx_enable) { |
| 1326 | struct mptcp_subflow_context *subflow; |
| 1327 | |
| 1328 | subflow = mptcp_subflow_ctx(ssk); |
| 1329 | subflow->data_fin_tx_seq = data_fin_tx_seq; |
| 1330 | subflow->data_fin_tx_enable = 1; |
| 1331 | } |
| 1332 | |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1333 | ssk->sk_shutdown |= how; |
| 1334 | tcp_shutdown(ssk, how); |
| 1335 | break; |
| 1336 | } |
| 1337 | |
| 1338 | /* Wake up anyone sleeping in poll. */ |
| 1339 | ssk->sk_state_change(ssk); |
| 1340 | release_sock(ssk); |
| 1341 | } |
| 1342 | |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1343 | /* Called with msk lock held, releases such lock before returning */ |
Florian Westphal | 2c22c06 | 2020-02-04 18:12:30 +0100 | [diff] [blame] | 1344 | static void mptcp_close(struct sock *sk, long timeout) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1345 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1346 | struct mptcp_subflow_context *subflow, *tmp; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1347 | struct mptcp_sock *msk = mptcp_sk(sk); |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 1348 | LIST_HEAD(conn_list); |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 1349 | u64 data_fin_tx_seq; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1350 | |
Florian Westphal | 2c22c06 | 2020-02-04 18:12:30 +0100 | [diff] [blame] | 1351 | lock_sock(sk); |
| 1352 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1353 | inet_sk_state_store(sk, TCP_CLOSE); |
| 1354 | |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 1355 | /* be sure to always acquire the join list lock, to sync vs |
| 1356 | * mptcp_finish_join(). |
| 1357 | */ |
| 1358 | spin_lock_bh(&msk->join_list_lock); |
| 1359 | list_splice_tail_init(&msk->join_list, &msk->conn_list); |
| 1360 | spin_unlock_bh(&msk->join_list_lock); |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 1361 | list_splice_init(&msk->conn_list, &conn_list); |
| 1362 | |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 1363 | data_fin_tx_seq = msk->write_seq; |
| 1364 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1365 | __mptcp_clear_xmit(sk); |
| 1366 | |
Florian Westphal | b2c5b61 | 2020-01-29 15:54:45 +0100 | [diff] [blame] | 1367 | release_sock(sk); |
| 1368 | |
| 1369 | list_for_each_entry_safe(subflow, tmp, &conn_list, node) { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1370 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 1371 | |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 1372 | subflow->data_fin_tx_seq = data_fin_tx_seq; |
| 1373 | subflow->data_fin_tx_enable = 1; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1374 | __mptcp_close_ssk(sk, ssk, subflow, timeout); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1377 | mptcp_cancel_work(sk); |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 1378 | mptcp_pm_close(msk); |
Paolo Abeni | 8099201 | 2020-02-26 10:14:47 +0100 | [diff] [blame] | 1379 | |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 1380 | __skb_queue_purge(&sk->sk_receive_queue); |
| 1381 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1382 | sk_common_release(sk); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1385 | static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) |
| 1386 | { |
| 1387 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1388 | const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); |
| 1389 | struct ipv6_pinfo *msk6 = inet6_sk(msk); |
| 1390 | |
| 1391 | msk->sk_v6_daddr = ssk->sk_v6_daddr; |
| 1392 | msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; |
| 1393 | |
| 1394 | if (msk6 && ssk6) { |
| 1395 | msk6->saddr = ssk6->saddr; |
| 1396 | msk6->flow_label = ssk6->flow_label; |
| 1397 | } |
| 1398 | #endif |
| 1399 | |
| 1400 | inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; |
| 1401 | inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; |
| 1402 | inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; |
| 1403 | inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; |
| 1404 | inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; |
| 1405 | inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; |
| 1406 | } |
| 1407 | |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1408 | static int mptcp_disconnect(struct sock *sk, int flags) |
| 1409 | { |
Florian Westphal | 42c556f | 2020-04-29 20:43:20 +0200 | [diff] [blame] | 1410 | /* Should never be called. |
| 1411 | * inet_stream_connect() calls ->disconnect, but that |
| 1412 | * refers to the subflow socket, not the mptcp one. |
| 1413 | */ |
| 1414 | WARN_ON_ONCE(1); |
| 1415 | return 0; |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 1418 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1419 | static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) |
| 1420 | { |
| 1421 | unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); |
| 1422 | |
| 1423 | return (struct ipv6_pinfo *)(((u8 *)sk) + offset); |
| 1424 | } |
| 1425 | #endif |
| 1426 | |
Paolo Abeni | fca5c82 | 2020-04-20 16:25:06 +0200 | [diff] [blame] | 1427 | struct sock *mptcp_sk_clone(const struct sock *sk, |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1428 | const struct mptcp_options_received *mp_opt, |
Paolo Abeni | fca5c82 | 2020-04-20 16:25:06 +0200 | [diff] [blame] | 1429 | struct request_sock *req) |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 1430 | { |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1431 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 1432 | struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1433 | struct mptcp_sock *msk; |
| 1434 | u64 ack_seq; |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 1435 | |
| 1436 | if (!nsk) |
| 1437 | return NULL; |
| 1438 | |
| 1439 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1440 | if (nsk->sk_family == AF_INET6) |
| 1441 | inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); |
| 1442 | #endif |
| 1443 | |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1444 | __mptcp_init_sock(nsk); |
| 1445 | |
| 1446 | msk = mptcp_sk(nsk); |
| 1447 | msk->local_key = subflow_req->local_key; |
| 1448 | msk->token = subflow_req->token; |
| 1449 | msk->subflow = NULL; |
| 1450 | |
| 1451 | if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) { |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 1452 | nsk->sk_state = TCP_CLOSE; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1453 | bh_unlock_sock(nsk); |
| 1454 | |
| 1455 | /* we can't call into mptcp_close() here - possible BH context |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 1456 | * free the sock directly. |
| 1457 | * sk_clone_lock() sets nsk refcnt to two, hence call sk_free() |
| 1458 | * too. |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1459 | */ |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 1460 | sk_common_release(nsk); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1461 | sk_free(nsk); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | |
| 1465 | msk->write_seq = subflow_req->idsn + 1; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1466 | atomic64_set(&msk->snd_una, msk->write_seq); |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1467 | if (mp_opt->mp_capable) { |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1468 | msk->can_ack = true; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1469 | msk->remote_key = mp_opt->sndr_key; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1470 | mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); |
| 1471 | ack_seq++; |
| 1472 | msk->ack_seq = ack_seq; |
| 1473 | } |
Paolo Abeni | 7f20d5f | 2020-03-17 15:53:34 +0100 | [diff] [blame] | 1474 | |
Florian Westphal | 5e20087 | 2020-04-20 16:25:04 +0200 | [diff] [blame] | 1475 | sock_reset_flag(nsk, SOCK_RCU_FREE); |
Paolo Abeni | 7f20d5f | 2020-03-17 15:53:34 +0100 | [diff] [blame] | 1476 | /* will be fully established after successful MPC subflow creation */ |
| 1477 | inet_sk_state_store(nsk, TCP_SYN_RECV); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1478 | bh_unlock_sock(nsk); |
| 1479 | |
| 1480 | /* keep a single reference */ |
| 1481 | __sock_put(nsk); |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 1482 | return nsk; |
| 1483 | } |
| 1484 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1485 | static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, |
| 1486 | bool kern) |
| 1487 | { |
| 1488 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1489 | struct socket *listener; |
| 1490 | struct sock *newsk; |
| 1491 | |
| 1492 | listener = __mptcp_nmpc_socket(msk); |
| 1493 | if (WARN_ON_ONCE(!listener)) { |
| 1494 | *err = -EINVAL; |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); |
| 1499 | newsk = inet_csk_accept(listener->sk, flags, err, kern); |
| 1500 | if (!newsk) |
| 1501 | return NULL; |
| 1502 | |
| 1503 | pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); |
| 1504 | |
| 1505 | if (sk_is_mptcp(newsk)) { |
| 1506 | struct mptcp_subflow_context *subflow; |
| 1507 | struct sock *new_mptcp_sock; |
| 1508 | struct sock *ssk = newsk; |
| 1509 | |
| 1510 | subflow = mptcp_subflow_ctx(newsk); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1511 | new_mptcp_sock = subflow->conn; |
| 1512 | |
| 1513 | /* is_mptcp should be false if subflow->conn is missing, see |
| 1514 | * subflow_syn_recv_sock() |
| 1515 | */ |
| 1516 | if (WARN_ON_ONCE(!new_mptcp_sock)) { |
| 1517 | tcp_sk(newsk)->is_mptcp = 0; |
| 1518 | return newsk; |
| 1519 | } |
| 1520 | |
| 1521 | /* acquire the 2nd reference for the owning socket */ |
| 1522 | sock_hold(new_mptcp_sock); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1523 | |
| 1524 | local_bh_disable(); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1525 | bh_lock_sock(new_mptcp_sock); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1526 | msk = mptcp_sk(new_mptcp_sock); |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1527 | msk->first = newsk; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1528 | |
| 1529 | newsk = new_mptcp_sock; |
| 1530 | mptcp_copy_inaddrs(newsk, ssk); |
| 1531 | list_add(&subflow->node, &msk->conn_list); |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 1532 | inet_sk_state_store(newsk, TCP_ESTABLISHED); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1533 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1534 | bh_unlock_sock(new_mptcp_sock); |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1535 | |
| 1536 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1537 | local_bh_enable(); |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1538 | } else { |
| 1539 | MPTCP_INC_STATS(sock_net(sk), |
| 1540 | MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | return newsk; |
| 1544 | } |
| 1545 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1546 | static void mptcp_destroy(struct sock *sk) |
| 1547 | { |
Florian Westphal | c9fd9c5 | 2020-01-29 15:54:43 +0100 | [diff] [blame] | 1548 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1549 | |
Paolo Abeni | c5c7976 | 2020-05-29 17:43:31 +0200 | [diff] [blame] | 1550 | mptcp_token_destroy(msk->token); |
Florian Westphal | c9fd9c5 | 2020-01-29 15:54:43 +0100 | [diff] [blame] | 1551 | if (msk->cached_ext) |
| 1552 | __skb_ext_put(msk->cached_ext); |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1553 | |
| 1554 | sk_sockets_allocated_dec(sk); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1557 | static int mptcp_setsockopt(struct sock *sk, int level, int optname, |
Florian Westphal | 50e741b | 2020-01-29 15:54:44 +0100 | [diff] [blame] | 1558 | char __user *optval, unsigned int optlen) |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1559 | { |
| 1560 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1561 | struct socket *ssock; |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1562 | |
| 1563 | pr_debug("msk=%p", msk); |
| 1564 | |
| 1565 | /* @@ the meaning of setsockopt() when the socket is connected and |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1566 | * there are multiple subflows is not yet defined. It is up to the |
| 1567 | * MPTCP-level socket to configure the subflows until the subflow |
| 1568 | * is in TCP fallback, when TCP socket options are passed through |
| 1569 | * to the one remaining subflow. |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1570 | */ |
| 1571 | lock_sock(sk); |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1572 | ssock = __mptcp_tcp_fallback(msk); |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 1573 | release_sock(sk); |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1574 | if (ssock) |
| 1575 | return tcp_setsockopt(ssock->sk, level, optname, optval, |
| 1576 | optlen); |
Florian Westphal | 50e741b | 2020-01-29 15:54:44 +0100 | [diff] [blame] | 1577 | |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1578 | return -EOPNOTSUPP; |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static int mptcp_getsockopt(struct sock *sk, int level, int optname, |
Florian Westphal | 50e741b | 2020-01-29 15:54:44 +0100 | [diff] [blame] | 1582 | char __user *optval, int __user *option) |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1583 | { |
| 1584 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1585 | struct socket *ssock; |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1586 | |
| 1587 | pr_debug("msk=%p", msk); |
| 1588 | |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1589 | /* @@ the meaning of setsockopt() when the socket is connected and |
| 1590 | * there are multiple subflows is not yet defined. It is up to the |
| 1591 | * MPTCP-level socket to configure the subflows until the subflow |
| 1592 | * is in TCP fallback, when socket options are passed through |
| 1593 | * to the one remaining subflow. |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1594 | */ |
| 1595 | lock_sock(sk); |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1596 | ssock = __mptcp_tcp_fallback(msk); |
Florian Westphal | e154659 | 2020-04-11 21:05:01 +0200 | [diff] [blame] | 1597 | release_sock(sk); |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1598 | if (ssock) |
| 1599 | return tcp_getsockopt(ssock->sk, level, optname, optval, |
| 1600 | option); |
Florian Westphal | 50e741b | 2020-01-29 15:54:44 +0100 | [diff] [blame] | 1601 | |
Mat Martineau | b6e4a1a | 2020-02-14 14:14:29 -0800 | [diff] [blame] | 1602 | return -EOPNOTSUPP; |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1605 | #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ |
| 1606 | TCPF_WRITE_TIMER_DEFERRED) |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 1607 | |
| 1608 | /* this is very alike tcp_release_cb() but we must handle differently a |
| 1609 | * different set of events |
| 1610 | */ |
| 1611 | static void mptcp_release_cb(struct sock *sk) |
| 1612 | { |
| 1613 | unsigned long flags, nflags; |
| 1614 | |
| 1615 | do { |
| 1616 | flags = sk->sk_tsq_flags; |
| 1617 | if (!(flags & MPTCP_DEFERRED_ALL)) |
| 1618 | return; |
| 1619 | nflags = flags & ~MPTCP_DEFERRED_ALL; |
| 1620 | } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); |
| 1621 | |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1622 | sock_release_ownership(sk); |
| 1623 | |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 1624 | if (flags & TCPF_DELACK_TIMER_DEFERRED) { |
| 1625 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1626 | struct sock *ssk; |
| 1627 | |
| 1628 | ssk = mptcp_subflow_recv_lookup(msk); |
| 1629 | if (!ssk || !schedule_work(&msk->work)) |
| 1630 | __sock_put(sk); |
| 1631 | } |
Paolo Abeni | b51f9b8 | 2020-03-27 14:48:44 -0700 | [diff] [blame] | 1632 | |
| 1633 | if (flags & TCPF_WRITE_TIMER_DEFERRED) { |
| 1634 | mptcp_retransmit_handler(sk); |
| 1635 | __sock_put(sk); |
| 1636 | } |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 1637 | } |
| 1638 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1639 | static int mptcp_get_port(struct sock *sk, unsigned short snum) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1640 | { |
| 1641 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1642 | struct socket *ssock; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1643 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1644 | ssock = __mptcp_nmpc_socket(msk); |
| 1645 | pr_debug("msk=%p, subflow=%p", msk, ssock); |
| 1646 | if (WARN_ON_ONCE(!ssock)) |
| 1647 | return -EINVAL; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1648 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1649 | return inet_csk_get_port(ssock->sk, snum); |
| 1650 | } |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1651 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1652 | void mptcp_finish_connect(struct sock *ssk) |
| 1653 | { |
| 1654 | struct mptcp_subflow_context *subflow; |
| 1655 | struct mptcp_sock *msk; |
| 1656 | struct sock *sk; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1657 | u64 ack_seq; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1658 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1659 | subflow = mptcp_subflow_ctx(ssk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1660 | sk = subflow->conn; |
| 1661 | msk = mptcp_sk(sk); |
| 1662 | |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1663 | if (!subflow->mp_capable) { |
| 1664 | MPTCP_INC_STATS(sock_net(sk), |
| 1665 | MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); |
| 1666 | return; |
| 1667 | } |
| 1668 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1669 | pr_debug("msk=%p, token=%u", sk, subflow->token); |
| 1670 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1671 | mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); |
| 1672 | ack_seq++; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1673 | subflow->map_seq = ack_seq; |
| 1674 | subflow->map_subflow_seq = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1675 | subflow->rel_write_seq = 1; |
| 1676 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1677 | /* the socket is not connected yet, no msk/subflow ops can access/race |
| 1678 | * accessing the field below |
| 1679 | */ |
| 1680 | WRITE_ONCE(msk->remote_key, subflow->remote_key); |
| 1681 | WRITE_ONCE(msk->local_key, subflow->local_key); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1682 | WRITE_ONCE(msk->token, subflow->token); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1683 | WRITE_ONCE(msk->write_seq, subflow->idsn + 1); |
| 1684 | WRITE_ONCE(msk->ack_seq, ack_seq); |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1685 | WRITE_ONCE(msk->can_ack, 1); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1686 | atomic64_set(&msk->snd_una, msk->write_seq); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1687 | |
| 1688 | mptcp_pm_new_connection(msk, 0); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1691 | static void mptcp_sock_graft(struct sock *sk, struct socket *parent) |
| 1692 | { |
| 1693 | write_lock_bh(&sk->sk_callback_lock); |
| 1694 | rcu_assign_pointer(sk->sk_wq, &parent->wq); |
| 1695 | sk_set_socket(sk, parent); |
| 1696 | sk->sk_uid = SOCK_INODE(parent)->i_uid; |
| 1697 | write_unlock_bh(&sk->sk_callback_lock); |
| 1698 | } |
| 1699 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1700 | bool mptcp_finish_join(struct sock *sk) |
| 1701 | { |
| 1702 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 1703 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
| 1704 | struct sock *parent = (void *)msk; |
| 1705 | struct socket *parent_sock; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1706 | bool ret; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1707 | |
| 1708 | pr_debug("msk=%p, subflow=%p", msk, subflow); |
| 1709 | |
| 1710 | /* mptcp socket already closing? */ |
| 1711 | if (inet_sk_state_load(parent) != TCP_ESTABLISHED) |
| 1712 | return false; |
| 1713 | |
| 1714 | if (!msk->pm.server_side) |
| 1715 | return true; |
| 1716 | |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 1717 | if (!mptcp_pm_allow_new_subflow(msk)) |
| 1718 | return false; |
| 1719 | |
| 1720 | /* active connections are already on conn_list, and we can't acquire |
| 1721 | * msk lock here. |
| 1722 | * use the join list lock as synchronization point and double-check |
| 1723 | * msk status to avoid racing with mptcp_close() |
| 1724 | */ |
| 1725 | spin_lock_bh(&msk->join_list_lock); |
| 1726 | ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; |
| 1727 | if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) |
| 1728 | list_add_tail(&subflow->node, &msk->join_list); |
| 1729 | spin_unlock_bh(&msk->join_list_lock); |
| 1730 | if (!ret) |
| 1731 | return false; |
| 1732 | |
| 1733 | /* attach to msk socket only after we are sure he will deal with us |
| 1734 | * at close time |
| 1735 | */ |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1736 | parent_sock = READ_ONCE(parent->sk_socket); |
| 1737 | if (parent_sock && !sk->sk_socket) |
| 1738 | mptcp_sock_graft(sk, parent_sock); |
Paolo Abeni | 10f6d46 | 2020-05-29 17:43:30 +0200 | [diff] [blame] | 1739 | subflow->map_seq = msk->ack_seq; |
| 1740 | return true; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1743 | static bool mptcp_memory_free(const struct sock *sk, int wake) |
| 1744 | { |
| 1745 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1746 | |
| 1747 | return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; |
| 1748 | } |
| 1749 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1750 | static struct proto mptcp_prot = { |
| 1751 | .name = "MPTCP", |
| 1752 | .owner = THIS_MODULE, |
| 1753 | .init = mptcp_init_sock, |
Paolo Abeni | 18b683b | 2020-03-27 14:48:43 -0700 | [diff] [blame] | 1754 | .disconnect = mptcp_disconnect, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1755 | .close = mptcp_close, |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1756 | .accept = mptcp_accept, |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 1757 | .setsockopt = mptcp_setsockopt, |
| 1758 | .getsockopt = mptcp_getsockopt, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1759 | .shutdown = tcp_shutdown, |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1760 | .destroy = mptcp_destroy, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1761 | .sendmsg = mptcp_sendmsg, |
| 1762 | .recvmsg = mptcp_recvmsg, |
Paolo Abeni | 14c441b | 2020-02-26 10:14:52 +0100 | [diff] [blame] | 1763 | .release_cb = mptcp_release_cb, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1764 | .hash = inet_hash, |
| 1765 | .unhash = inet_unhash, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1766 | .get_port = mptcp_get_port, |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1767 | .sockets_allocated = &mptcp_sockets_allocated, |
| 1768 | .memory_allocated = &tcp_memory_allocated, |
| 1769 | .memory_pressure = &tcp_memory_pressure, |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1770 | .stream_memory_free = mptcp_memory_free, |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 1771 | .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), |
| 1772 | .sysctl_mem = sysctl_tcp_mem, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1773 | .obj_size = sizeof(struct mptcp_sock), |
| 1774 | .no_autobind = true, |
| 1775 | }; |
| 1776 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1777 | static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 1778 | { |
| 1779 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 1780 | struct socket *ssock; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1781 | int err; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1782 | |
| 1783 | lock_sock(sock->sk); |
| 1784 | ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); |
| 1785 | if (IS_ERR(ssock)) { |
| 1786 | err = PTR_ERR(ssock); |
| 1787 | goto unlock; |
| 1788 | } |
| 1789 | |
| 1790 | err = ssock->ops->bind(ssock, uaddr, addr_len); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1791 | if (!err) |
| 1792 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1793 | |
| 1794 | unlock: |
| 1795 | release_sock(sock->sk); |
| 1796 | return err; |
| 1797 | } |
| 1798 | |
| 1799 | static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, |
| 1800 | int addr_len, int flags) |
| 1801 | { |
| 1802 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 1803 | struct socket *ssock; |
| 1804 | int err; |
| 1805 | |
| 1806 | lock_sock(sock->sk); |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 1807 | if (sock->state != SS_UNCONNECTED && msk->subflow) { |
| 1808 | /* pending connection or invalid state, let existing subflow |
| 1809 | * cope with that |
| 1810 | */ |
| 1811 | ssock = msk->subflow; |
| 1812 | goto do_connect; |
| 1813 | } |
| 1814 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1815 | ssock = __mptcp_socket_create(msk, TCP_SYN_SENT); |
| 1816 | if (IS_ERR(ssock)) { |
| 1817 | err = PTR_ERR(ssock); |
| 1818 | goto unlock; |
| 1819 | } |
| 1820 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1821 | #ifdef CONFIG_TCP_MD5SIG |
| 1822 | /* no MPTCP if MD5SIG is enabled on this socket or we may run out of |
| 1823 | * TCP option space. |
| 1824 | */ |
| 1825 | if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) |
| 1826 | mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0; |
| 1827 | #endif |
| 1828 | |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 1829 | do_connect: |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1830 | err = ssock->ops->connect(ssock, uaddr, addr_len, flags); |
Paolo Abeni | 41be81a | 2020-05-29 17:43:29 +0200 | [diff] [blame] | 1831 | sock->state = ssock->state; |
| 1832 | |
| 1833 | /* on successful connect, the msk state will be moved to established by |
| 1834 | * subflow_finish_connect() |
| 1835 | */ |
| 1836 | if (!err || err == EINPROGRESS) |
| 1837 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
| 1838 | else |
| 1839 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1840 | |
| 1841 | unlock: |
| 1842 | release_sock(sock->sk); |
| 1843 | return err; |
| 1844 | } |
| 1845 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1846 | static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1847 | int peer) |
| 1848 | { |
| 1849 | if (sock->sk->sk_prot == &tcp_prot) { |
| 1850 | /* we are being invoked from __sys_accept4, after |
| 1851 | * mptcp_accept() has just accepted a non-mp-capable |
| 1852 | * flow: sk is a tcp_sk, not an mptcp one. |
| 1853 | * |
| 1854 | * Hand the socket over to tcp so all further socket ops |
| 1855 | * bypass mptcp. |
| 1856 | */ |
| 1857 | sock->ops = &inet_stream_ops; |
| 1858 | } |
| 1859 | |
| 1860 | return inet_getname(sock, uaddr, peer); |
| 1861 | } |
| 1862 | |
| 1863 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1864 | static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1865 | int peer) |
| 1866 | { |
| 1867 | if (sock->sk->sk_prot == &tcpv6_prot) { |
| 1868 | /* we are being invoked from __sys_accept4 after |
| 1869 | * mptcp_accept() has accepted a non-mp-capable |
| 1870 | * subflow: sk is a tcp_sk, not mptcp. |
| 1871 | * |
| 1872 | * Hand the socket over to tcp so all further |
| 1873 | * socket ops bypass mptcp. |
| 1874 | */ |
| 1875 | sock->ops = &inet6_stream_ops; |
| 1876 | } |
| 1877 | |
| 1878 | return inet6_getname(sock, uaddr, peer); |
| 1879 | } |
| 1880 | #endif |
| 1881 | |
| 1882 | static int mptcp_listen(struct socket *sock, int backlog) |
| 1883 | { |
| 1884 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 1885 | struct socket *ssock; |
| 1886 | int err; |
| 1887 | |
| 1888 | pr_debug("msk=%p", msk); |
| 1889 | |
| 1890 | lock_sock(sock->sk); |
| 1891 | ssock = __mptcp_socket_create(msk, TCP_LISTEN); |
| 1892 | if (IS_ERR(ssock)) { |
| 1893 | err = PTR_ERR(ssock); |
| 1894 | goto unlock; |
| 1895 | } |
| 1896 | |
Florian Westphal | 5e20087 | 2020-04-20 16:25:04 +0200 | [diff] [blame] | 1897 | sock_set_flag(sock->sk, SOCK_RCU_FREE); |
| 1898 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1899 | err = ssock->ops->listen(ssock, backlog); |
| 1900 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
| 1901 | if (!err) |
| 1902 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
| 1903 | |
| 1904 | unlock: |
| 1905 | release_sock(sock->sk); |
| 1906 | return err; |
| 1907 | } |
| 1908 | |
| 1909 | static bool is_tcp_proto(const struct proto *p) |
| 1910 | { |
| 1911 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1912 | return p == &tcp_prot || p == &tcpv6_prot; |
| 1913 | #else |
| 1914 | return p == &tcp_prot; |
| 1915 | #endif |
| 1916 | } |
| 1917 | |
| 1918 | static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, |
| 1919 | int flags, bool kern) |
| 1920 | { |
| 1921 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 1922 | struct socket *ssock; |
| 1923 | int err; |
| 1924 | |
| 1925 | pr_debug("msk=%p", msk); |
| 1926 | |
| 1927 | lock_sock(sock->sk); |
| 1928 | if (sock->sk->sk_state != TCP_LISTEN) |
| 1929 | goto unlock_fail; |
| 1930 | |
| 1931 | ssock = __mptcp_nmpc_socket(msk); |
| 1932 | if (!ssock) |
| 1933 | goto unlock_fail; |
| 1934 | |
| 1935 | sock_hold(ssock->sk); |
| 1936 | release_sock(sock->sk); |
| 1937 | |
| 1938 | err = ssock->ops->accept(sock, newsock, flags, kern); |
| 1939 | if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) { |
| 1940 | struct mptcp_sock *msk = mptcp_sk(newsock->sk); |
| 1941 | struct mptcp_subflow_context *subflow; |
| 1942 | |
| 1943 | /* set ssk->sk_socket of accept()ed flows to mptcp socket. |
| 1944 | * This is needed so NOSPACE flag can be set from tcp stack. |
| 1945 | */ |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1946 | __mptcp_flush_join_list(msk); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1947 | list_for_each_entry(subflow, &msk->conn_list, node) { |
| 1948 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 1949 | |
| 1950 | if (!ssk->sk_socket) |
| 1951 | mptcp_sock_graft(ssk, newsock); |
| 1952 | } |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | sock_put(ssock->sk); |
| 1956 | return err; |
| 1957 | |
| 1958 | unlock_fail: |
| 1959 | release_sock(sock->sk); |
| 1960 | return -EINVAL; |
| 1961 | } |
| 1962 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1963 | static __poll_t mptcp_poll(struct file *file, struct socket *sock, |
| 1964 | struct poll_table_struct *wait) |
| 1965 | { |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1966 | struct sock *sk = sock->sk; |
Paolo Abeni | 8ab183d | 2020-01-21 16:56:33 -0800 | [diff] [blame] | 1967 | struct mptcp_sock *msk; |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1968 | struct socket *ssock; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1969 | __poll_t mask = 0; |
| 1970 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1971 | msk = mptcp_sk(sk); |
| 1972 | lock_sock(sk); |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 1973 | ssock = __mptcp_tcp_fallback(msk); |
| 1974 | if (!ssock) |
| 1975 | ssock = __mptcp_nmpc_socket(msk); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1976 | if (ssock) { |
| 1977 | mask = ssock->ops->poll(file, ssock, wait); |
| 1978 | release_sock(sk); |
| 1979 | return mask; |
| 1980 | } |
| 1981 | |
| 1982 | release_sock(sk); |
| 1983 | sock_poll_wait(file, sock, wait); |
| 1984 | lock_sock(sk); |
| 1985 | |
| 1986 | if (test_bit(MPTCP_DATA_READY, &msk->flags)) |
| 1987 | mask = EPOLLIN | EPOLLRDNORM; |
| 1988 | if (sk_stream_is_writeable(sk) && |
| 1989 | test_bit(MPTCP_SEND_SPACE, &msk->flags)) |
| 1990 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 1991 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1992 | mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
| 1993 | |
| 1994 | release_sock(sk); |
| 1995 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1996 | return mask; |
| 1997 | } |
| 1998 | |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1999 | static int mptcp_shutdown(struct socket *sock, int how) |
| 2000 | { |
| 2001 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 2002 | struct mptcp_subflow_context *subflow; |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 2003 | struct socket *ssock; |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2004 | int ret = 0; |
| 2005 | |
| 2006 | pr_debug("sk=%p, how=%d", msk, how); |
| 2007 | |
| 2008 | lock_sock(sock->sk); |
Florian Westphal | 0b4f33d | 2020-04-02 13:44:51 +0200 | [diff] [blame] | 2009 | ssock = __mptcp_tcp_fallback(msk); |
| 2010 | if (ssock) { |
| 2011 | release_sock(sock->sk); |
| 2012 | return inet_shutdown(ssock, how); |
| 2013 | } |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2014 | |
| 2015 | if (how == SHUT_WR || how == SHUT_RDWR) |
| 2016 | inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); |
| 2017 | |
| 2018 | how++; |
| 2019 | |
| 2020 | if ((how & ~SHUTDOWN_MASK) || !how) { |
| 2021 | ret = -EINVAL; |
| 2022 | goto out_unlock; |
| 2023 | } |
| 2024 | |
| 2025 | if (sock->state == SS_CONNECTING) { |
| 2026 | if ((1 << sock->sk->sk_state) & |
| 2027 | (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) |
| 2028 | sock->state = SS_DISCONNECTING; |
| 2029 | else |
| 2030 | sock->state = SS_CONNECTED; |
| 2031 | } |
| 2032 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 2033 | __mptcp_flush_join_list(msk); |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2034 | mptcp_for_each_subflow(msk, subflow) { |
| 2035 | struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); |
| 2036 | |
Mat Martineau | 76c42a2 | 2020-02-28 15:47:40 -0800 | [diff] [blame] | 2037 | mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq); |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | out_unlock: |
| 2041 | release_sock(sock->sk); |
| 2042 | |
| 2043 | return ret; |
| 2044 | } |
| 2045 | |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 2046 | static const struct proto_ops mptcp_stream_ops = { |
| 2047 | .family = PF_INET, |
| 2048 | .owner = THIS_MODULE, |
| 2049 | .release = inet_release, |
| 2050 | .bind = mptcp_bind, |
| 2051 | .connect = mptcp_stream_connect, |
| 2052 | .socketpair = sock_no_socketpair, |
| 2053 | .accept = mptcp_stream_accept, |
| 2054 | .getname = mptcp_v4_getname, |
| 2055 | .poll = mptcp_poll, |
| 2056 | .ioctl = inet_ioctl, |
| 2057 | .gettstamp = sock_gettstamp, |
| 2058 | .listen = mptcp_listen, |
| 2059 | .shutdown = mptcp_shutdown, |
| 2060 | .setsockopt = sock_common_setsockopt, |
| 2061 | .getsockopt = sock_common_getsockopt, |
| 2062 | .sendmsg = inet_sendmsg, |
| 2063 | .recvmsg = inet_recvmsg, |
| 2064 | .mmap = sock_no_mmap, |
| 2065 | .sendpage = inet_sendpage, |
| 2066 | #ifdef CONFIG_COMPAT |
| 2067 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 2068 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 2069 | #endif |
| 2070 | }; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2071 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2072 | static struct inet_protosw mptcp_protosw = { |
| 2073 | .type = SOCK_STREAM, |
| 2074 | .protocol = IPPROTO_MPTCP, |
| 2075 | .prot = &mptcp_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2076 | .ops = &mptcp_stream_ops, |
| 2077 | .flags = INET_PROTOSW_ICSK, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2078 | }; |
| 2079 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 2080 | void mptcp_proto_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2081 | { |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2082 | mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2083 | |
Paolo Abeni | d027236 | 2020-03-27 14:48:45 -0700 | [diff] [blame] | 2084 | if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) |
| 2085 | panic("Failed to allocate MPTCP pcpu counter\n"); |
| 2086 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2087 | mptcp_subflow_init(); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 2088 | mptcp_pm_init(); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2089 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2090 | if (proto_register(&mptcp_prot, 1) != 0) |
| 2091 | panic("Failed to register MPTCP proto.\n"); |
| 2092 | |
| 2093 | inet_register_protosw(&mptcp_protosw); |
Florian Westphal | 6771bfd | 2020-02-26 10:14:48 +0100 | [diff] [blame] | 2094 | |
| 2095 | BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 2099 | static const struct proto_ops mptcp_v6_stream_ops = { |
| 2100 | .family = PF_INET6, |
| 2101 | .owner = THIS_MODULE, |
| 2102 | .release = inet6_release, |
| 2103 | .bind = mptcp_bind, |
| 2104 | .connect = mptcp_stream_connect, |
| 2105 | .socketpair = sock_no_socketpair, |
| 2106 | .accept = mptcp_stream_accept, |
| 2107 | .getname = mptcp_v6_getname, |
| 2108 | .poll = mptcp_poll, |
| 2109 | .ioctl = inet6_ioctl, |
| 2110 | .gettstamp = sock_gettstamp, |
| 2111 | .listen = mptcp_listen, |
| 2112 | .shutdown = mptcp_shutdown, |
| 2113 | .setsockopt = sock_common_setsockopt, |
| 2114 | .getsockopt = sock_common_getsockopt, |
| 2115 | .sendmsg = inet6_sendmsg, |
| 2116 | .recvmsg = inet6_recvmsg, |
| 2117 | .mmap = sock_no_mmap, |
| 2118 | .sendpage = inet_sendpage, |
| 2119 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 3986912 | 2020-05-18 08:28:06 +0200 | [diff] [blame] | 2120 | .compat_ioctl = inet6_compat_ioctl, |
Florian Westphal | e42f1ac | 2020-01-24 16:04:02 -0800 | [diff] [blame] | 2121 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 2122 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 2123 | #endif |
| 2124 | }; |
| 2125 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2126 | static struct proto mptcp_v6_prot; |
| 2127 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 2128 | static void mptcp_v6_destroy(struct sock *sk) |
| 2129 | { |
| 2130 | mptcp_destroy(sk); |
| 2131 | inet6_destroy_sock(sk); |
| 2132 | } |
| 2133 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2134 | static struct inet_protosw mptcp_v6_protosw = { |
| 2135 | .type = SOCK_STREAM, |
| 2136 | .protocol = IPPROTO_MPTCP, |
| 2137 | .prot = &mptcp_v6_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 2138 | .ops = &mptcp_v6_stream_ops, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2139 | .flags = INET_PROTOSW_ICSK, |
| 2140 | }; |
| 2141 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame] | 2142 | int mptcp_proto_v6_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2143 | { |
| 2144 | int err; |
| 2145 | |
| 2146 | mptcp_v6_prot = mptcp_prot; |
| 2147 | strcpy(mptcp_v6_prot.name, "MPTCPv6"); |
| 2148 | mptcp_v6_prot.slab = NULL; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 2149 | mptcp_v6_prot.destroy = mptcp_v6_destroy; |
Florian Westphal | b0519de | 2020-02-06 00:39:37 +0100 | [diff] [blame] | 2150 | mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2151 | |
| 2152 | err = proto_register(&mptcp_v6_prot, 1); |
| 2153 | if (err) |
| 2154 | return err; |
| 2155 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 2156 | err = inet6_register_protosw(&mptcp_v6_protosw); |
| 2157 | if (err) |
| 2158 | proto_unregister(&mptcp_v6_prot); |
| 2159 | |
| 2160 | return err; |
| 2161 | } |
| 2162 | #endif |