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" |
| 24 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 25 | #define MPTCP_SAME_STATE TCP_MAX_STATES |
| 26 | |
| 27 | /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not |
| 28 | * completed yet or has failed, return the subflow socket. |
| 29 | * Otherwise return NULL. |
| 30 | */ |
| 31 | static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) |
| 32 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 33 | if (!msk->subflow || mptcp_subflow_ctx(msk->subflow->sk)->fourth_ack) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 34 | return NULL; |
| 35 | |
| 36 | return msk->subflow; |
| 37 | } |
| 38 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 39 | /* if msk has a single subflow, and the mp_capable handshake is failed, |
| 40 | * return it. |
| 41 | * Otherwise returns NULL |
| 42 | */ |
| 43 | static struct socket *__mptcp_tcp_fallback(const struct mptcp_sock *msk) |
| 44 | { |
| 45 | struct socket *ssock = __mptcp_nmpc_socket(msk); |
| 46 | |
| 47 | sock_owned_by_me((const struct sock *)msk); |
| 48 | |
| 49 | if (!ssock || sk_is_mptcp(ssock->sk)) |
| 50 | return NULL; |
| 51 | |
| 52 | return ssock; |
| 53 | } |
| 54 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 55 | static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk) |
| 56 | { |
| 57 | return ((struct sock *)msk)->sk_state == TCP_CLOSE; |
| 58 | } |
| 59 | |
| 60 | static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) |
| 61 | { |
| 62 | struct mptcp_subflow_context *subflow; |
| 63 | struct sock *sk = (struct sock *)msk; |
| 64 | struct socket *ssock; |
| 65 | int err; |
| 66 | |
| 67 | ssock = __mptcp_nmpc_socket(msk); |
| 68 | if (ssock) |
| 69 | goto set_state; |
| 70 | |
| 71 | if (!__mptcp_can_create_subflow(msk)) |
| 72 | return ERR_PTR(-EINVAL); |
| 73 | |
| 74 | err = mptcp_subflow_create_socket(sk, &ssock); |
| 75 | if (err) |
| 76 | return ERR_PTR(err); |
| 77 | |
| 78 | msk->subflow = ssock; |
| 79 | subflow = mptcp_subflow_ctx(ssock->sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 80 | list_add(&subflow->node, &msk->conn_list); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 81 | subflow->request_mptcp = 1; |
| 82 | |
| 83 | set_state: |
| 84 | if (state != MPTCP_SAME_STATE) |
| 85 | inet_sk_state_store(sk, state); |
| 86 | return ssock; |
| 87 | } |
| 88 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 89 | static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk) |
| 90 | { |
| 91 | struct mptcp_subflow_context *subflow; |
| 92 | |
| 93 | sock_owned_by_me((const struct sock *)msk); |
| 94 | |
| 95 | mptcp_for_each_subflow(msk, subflow) { |
| 96 | return mptcp_subflow_tcp_sock(subflow); |
| 97 | } |
| 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 102 | static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) |
| 103 | { |
| 104 | if (!msk->cached_ext) |
| 105 | msk->cached_ext = __skb_ext_alloc(); |
| 106 | |
| 107 | return !!msk->cached_ext; |
| 108 | } |
| 109 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 110 | static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) |
| 111 | { |
| 112 | struct mptcp_subflow_context *subflow; |
| 113 | struct sock *sk = (struct sock *)msk; |
| 114 | |
| 115 | sock_owned_by_me(sk); |
| 116 | |
| 117 | mptcp_for_each_subflow(msk, subflow) { |
| 118 | if (subflow->data_avail) |
| 119 | return mptcp_subflow_tcp_sock(subflow); |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 125 | static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk, |
| 126 | const struct sk_buff *skb, |
| 127 | const struct mptcp_ext *mpext) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 128 | { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 129 | if (!tcp_skb_can_collapse_to(skb)) |
| 130 | return false; |
| 131 | |
| 132 | /* can collapse only if MPTCP level sequence is in order */ |
| 133 | return mpext && mpext->data_seq + mpext->data_len == msk->write_seq; |
| 134 | } |
| 135 | |
| 136 | static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, |
| 137 | struct msghdr *msg, long *timeo, int *pmss_now, |
| 138 | int *ps_goal) |
| 139 | { |
| 140 | int mss_now, avail_size, size_goal, ret; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 141 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 142 | struct mptcp_ext *mpext = NULL; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 143 | struct sk_buff *skb, *tail; |
| 144 | bool can_collapse = false; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 145 | struct page_frag *pfrag; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 146 | size_t psize; |
| 147 | |
| 148 | /* use the mptcp page cache so that we can easily move the data |
| 149 | * from one substream to another, but do per subflow memory accounting |
| 150 | */ |
| 151 | pfrag = sk_page_frag(sk); |
| 152 | while (!sk_page_frag_refill(ssk, pfrag) || |
| 153 | !mptcp_ext_cache_refill(msk)) { |
| 154 | ret = sk_stream_wait_memory(ssk, timeo); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /* compute copy limit */ |
| 160 | mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 161 | *pmss_now = mss_now; |
| 162 | *ps_goal = size_goal; |
| 163 | avail_size = size_goal; |
| 164 | skb = tcp_write_queue_tail(ssk); |
| 165 | if (skb) { |
| 166 | mpext = skb_ext_find(skb, SKB_EXT_MPTCP); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 167 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 168 | /* Limit the write to the size available in the |
| 169 | * current skb, if any, so that we create at most a new skb. |
| 170 | * Explicitly tells TCP internals to avoid collapsing on later |
| 171 | * queue management operation, to avoid breaking the ext <-> |
| 172 | * SSN association set here |
| 173 | */ |
| 174 | can_collapse = (size_goal - skb->len > 0) && |
| 175 | mptcp_skb_can_collapse_to(msk, skb, mpext); |
| 176 | if (!can_collapse) |
| 177 | TCP_SKB_CB(skb)->eor = 1; |
| 178 | else |
| 179 | avail_size = size_goal - skb->len; |
| 180 | } |
| 181 | psize = min_t(size_t, pfrag->size - pfrag->offset, avail_size); |
| 182 | |
| 183 | /* Copy to page */ |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 184 | pr_debug("left=%zu", msg_data_left(msg)); |
| 185 | psize = copy_page_from_iter(pfrag->page, pfrag->offset, |
| 186 | min_t(size_t, msg_data_left(msg), psize), |
| 187 | &msg->msg_iter); |
| 188 | pr_debug("left=%zu", msg_data_left(msg)); |
| 189 | if (!psize) |
| 190 | return -EINVAL; |
| 191 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 192 | /* tell the TCP stack to delay the push so that we can safely |
| 193 | * access the skb after the sendpages call |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 194 | */ |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 195 | ret = do_tcp_sendpages(ssk, pfrag->page, pfrag->offset, psize, |
| 196 | msg->msg_flags | MSG_SENDPAGE_NOTLAST); |
| 197 | if (ret <= 0) |
| 198 | return ret; |
| 199 | if (unlikely(ret < psize)) |
| 200 | iov_iter_revert(&msg->msg_iter, psize - ret); |
| 201 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 202 | /* if the tail skb extension is still the cached one, collapsing |
| 203 | * really happened. Note: we can't check for 'same skb' as the sk_buff |
| 204 | * hdr on tail can be transmitted, freed and re-allocated by the |
| 205 | * do_tcp_sendpages() call |
| 206 | */ |
| 207 | tail = tcp_write_queue_tail(ssk); |
| 208 | if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { |
| 209 | WARN_ON_ONCE(!can_collapse); |
| 210 | mpext->data_len += ret; |
| 211 | goto out; |
| 212 | } |
| 213 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 214 | skb = tcp_write_queue_tail(ssk); |
| 215 | mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); |
| 216 | msk->cached_ext = NULL; |
| 217 | |
| 218 | memset(mpext, 0, sizeof(*mpext)); |
| 219 | mpext->data_seq = msk->write_seq; |
| 220 | mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; |
| 221 | mpext->data_len = ret; |
| 222 | mpext->use_map = 1; |
| 223 | mpext->dsn64 = 1; |
| 224 | |
| 225 | pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", |
| 226 | mpext->data_seq, mpext->subflow_seq, mpext->data_len, |
| 227 | mpext->dsn64); |
| 228 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 229 | out: |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 230 | pfrag->offset += ret; |
| 231 | msk->write_seq += ret; |
| 232 | mptcp_subflow_ctx(ssk)->rel_write_seq += ret; |
| 233 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 237 | static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) |
| 238 | { |
| 239 | struct socket *sock; |
| 240 | |
| 241 | if (likely(sk_stream_is_writeable(ssk))) |
| 242 | return; |
| 243 | |
| 244 | sock = READ_ONCE(ssk->sk_socket); |
| 245 | |
| 246 | if (sock) { |
| 247 | clear_bit(MPTCP_SEND_SPACE, &msk->flags); |
| 248 | smp_mb__after_atomic(); |
| 249 | /* set NOSPACE only after clearing SEND_SPACE flag */ |
| 250 | set_bit(SOCK_NOSPACE, &sock->flags); |
| 251 | } |
| 252 | } |
| 253 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 254 | static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
| 255 | { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 256 | int mss_now = 0, size_goal = 0, ret = 0; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 257 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 258 | struct socket *ssock; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 259 | size_t copied = 0; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 260 | struct sock *ssk; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 261 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 262 | |
| 263 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) |
| 264 | return -EOPNOTSUPP; |
| 265 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 266 | lock_sock(sk); |
| 267 | ssock = __mptcp_tcp_fallback(msk); |
| 268 | if (ssock) { |
| 269 | pr_debug("fallback passthrough"); |
| 270 | ret = sock_sendmsg(ssock, msg); |
| 271 | release_sock(sk); |
| 272 | return ret; |
| 273 | } |
| 274 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 275 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 276 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 277 | ssk = mptcp_subflow_get(msk); |
| 278 | if (!ssk) { |
| 279 | release_sock(sk); |
| 280 | return -ENOTCONN; |
| 281 | } |
| 282 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 283 | pr_debug("conn_list->subflow=%p", ssk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 284 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 285 | lock_sock(ssk); |
| 286 | while (msg_data_left(msg)) { |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 287 | ret = mptcp_sendmsg_frag(sk, ssk, msg, &timeo, &mss_now, |
| 288 | &size_goal); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 289 | if (ret < 0) |
| 290 | break; |
| 291 | |
| 292 | copied += ret; |
| 293 | } |
| 294 | |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 295 | if (copied) { |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 296 | ret = copied; |
Paolo Abeni | 5704075 | 2020-01-21 16:56:27 -0800 | [diff] [blame] | 297 | tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, |
| 298 | size_goal); |
| 299 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 300 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 301 | ssk_check_wmem(msk, ssk); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 302 | release_sock(ssk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 303 | release_sock(sk); |
| 304 | return ret; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 307 | int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb, |
| 308 | unsigned int offset, size_t len) |
| 309 | { |
| 310 | struct mptcp_read_arg *arg = desc->arg.data; |
| 311 | size_t copy_len; |
| 312 | |
| 313 | copy_len = min(desc->count, len); |
| 314 | |
| 315 | if (likely(arg->msg)) { |
| 316 | int err; |
| 317 | |
| 318 | err = skb_copy_datagram_msg(skb, offset, arg->msg, copy_len); |
| 319 | if (err) { |
| 320 | pr_debug("error path"); |
| 321 | desc->error = err; |
| 322 | return err; |
| 323 | } |
| 324 | } else { |
| 325 | pr_debug("Flushing skb payload"); |
| 326 | } |
| 327 | |
| 328 | desc->count -= copy_len; |
| 329 | |
| 330 | pr_debug("consumed %zu bytes, %zu left", copy_len, desc->count); |
| 331 | return copy_len; |
| 332 | } |
| 333 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 334 | static void mptcp_wait_data(struct sock *sk, long *timeo) |
| 335 | { |
| 336 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 337 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 338 | |
| 339 | add_wait_queue(sk_sleep(sk), &wait); |
| 340 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 341 | |
| 342 | sk_wait_event(sk, timeo, |
| 343 | test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); |
| 344 | |
| 345 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 346 | remove_wait_queue(sk_sleep(sk), &wait); |
| 347 | } |
| 348 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 349 | static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 350 | int nonblock, int flags, int *addr_len) |
| 351 | { |
| 352 | struct mptcp_sock *msk = mptcp_sk(sk); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 353 | struct mptcp_subflow_context *subflow; |
| 354 | bool more_data_avail = false; |
| 355 | struct mptcp_read_arg arg; |
| 356 | read_descriptor_t desc; |
| 357 | bool wait_data = false; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 358 | struct socket *ssock; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 359 | struct tcp_sock *tp; |
| 360 | bool done = false; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 361 | struct sock *ssk; |
| 362 | int copied = 0; |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 363 | int target; |
| 364 | long timeo; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 365 | |
| 366 | if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) |
| 367 | return -EOPNOTSUPP; |
| 368 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 369 | lock_sock(sk); |
| 370 | ssock = __mptcp_tcp_fallback(msk); |
| 371 | if (ssock) { |
| 372 | pr_debug("fallback-read subflow=%p", |
| 373 | mptcp_subflow_ctx(ssock->sk)); |
| 374 | copied = sock_recvmsg(ssock, msg, flags); |
| 375 | release_sock(sk); |
| 376 | return copied; |
| 377 | } |
| 378 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 379 | arg.msg = msg; |
| 380 | desc.arg.data = &arg; |
| 381 | desc.error = 0; |
| 382 | |
| 383 | timeo = sock_rcvtimeo(sk, nonblock); |
| 384 | |
| 385 | len = min_t(size_t, len, INT_MAX); |
| 386 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
| 387 | |
| 388 | while (!done) { |
| 389 | u32 map_remaining; |
| 390 | int bytes_read; |
| 391 | |
| 392 | ssk = mptcp_subflow_recv_lookup(msk); |
| 393 | pr_debug("msk=%p ssk=%p", msk, ssk); |
| 394 | if (!ssk) |
| 395 | goto wait_for_data; |
| 396 | |
| 397 | subflow = mptcp_subflow_ctx(ssk); |
| 398 | tp = tcp_sk(ssk); |
| 399 | |
| 400 | lock_sock(ssk); |
| 401 | do { |
| 402 | /* try to read as much data as available */ |
| 403 | map_remaining = subflow->map_data_len - |
| 404 | mptcp_subflow_get_map_offset(subflow); |
| 405 | desc.count = min_t(size_t, len - copied, map_remaining); |
| 406 | pr_debug("reading %zu bytes, copied %d", desc.count, |
| 407 | copied); |
| 408 | bytes_read = tcp_read_sock(ssk, &desc, |
| 409 | mptcp_read_actor); |
| 410 | if (bytes_read < 0) { |
| 411 | if (!copied) |
| 412 | copied = bytes_read; |
| 413 | done = true; |
| 414 | goto next; |
| 415 | } |
| 416 | |
| 417 | pr_debug("msk ack_seq=%llx -> %llx", msk->ack_seq, |
| 418 | msk->ack_seq + bytes_read); |
| 419 | msk->ack_seq += bytes_read; |
| 420 | copied += bytes_read; |
| 421 | if (copied >= len) { |
| 422 | done = true; |
| 423 | goto next; |
| 424 | } |
| 425 | if (tp->urg_data && tp->urg_seq == tp->copied_seq) { |
| 426 | pr_err("Urgent data present, cannot proceed"); |
| 427 | done = true; |
| 428 | goto next; |
| 429 | } |
| 430 | next: |
| 431 | more_data_avail = mptcp_subflow_data_available(ssk); |
| 432 | } while (more_data_avail && !done); |
| 433 | release_sock(ssk); |
| 434 | continue; |
| 435 | |
| 436 | wait_for_data: |
| 437 | more_data_avail = false; |
| 438 | |
| 439 | /* only the master socket status is relevant here. The exit |
| 440 | * conditions mirror closely tcp_recvmsg() |
| 441 | */ |
| 442 | if (copied >= target) |
| 443 | break; |
| 444 | |
| 445 | if (copied) { |
| 446 | if (sk->sk_err || |
| 447 | sk->sk_state == TCP_CLOSE || |
| 448 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 449 | !timeo || |
| 450 | signal_pending(current)) |
| 451 | break; |
| 452 | } else { |
| 453 | if (sk->sk_err) { |
| 454 | copied = sock_error(sk); |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 459 | break; |
| 460 | |
| 461 | if (sk->sk_state == TCP_CLOSE) { |
| 462 | copied = -ENOTCONN; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | if (!timeo) { |
| 467 | copied = -EAGAIN; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | if (signal_pending(current)) { |
| 472 | copied = sock_intr_errno(timeo); |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | pr_debug("block timeout %ld", timeo); |
| 478 | wait_data = true; |
| 479 | mptcp_wait_data(sk, &timeo); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 482 | if (more_data_avail) { |
| 483 | if (!test_bit(MPTCP_DATA_READY, &msk->flags)) |
| 484 | set_bit(MPTCP_DATA_READY, &msk->flags); |
| 485 | } else if (!wait_data) { |
| 486 | clear_bit(MPTCP_DATA_READY, &msk->flags); |
| 487 | |
| 488 | /* .. race-breaker: ssk might get new data after last |
| 489 | * data_available() returns false. |
| 490 | */ |
| 491 | ssk = mptcp_subflow_recv_lookup(msk); |
| 492 | if (unlikely(ssk)) |
| 493 | set_bit(MPTCP_DATA_READY, &msk->flags); |
| 494 | } |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 495 | |
| 496 | release_sock(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 497 | return copied; |
| 498 | } |
| 499 | |
| 500 | /* subflow sockets can be either outgoing (connect) or incoming |
| 501 | * (accept). |
| 502 | * |
| 503 | * Outgoing subflows use in-kernel sockets. |
| 504 | * Incoming subflows do not have their own 'struct socket' allocated, |
| 505 | * so we need to use tcp_close() after detaching them from the mptcp |
| 506 | * parent socket. |
| 507 | */ |
| 508 | static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, |
| 509 | struct mptcp_subflow_context *subflow, |
| 510 | long timeout) |
| 511 | { |
| 512 | struct socket *sock = READ_ONCE(ssk->sk_socket); |
| 513 | |
| 514 | list_del(&subflow->node); |
| 515 | |
| 516 | if (sock && sock != sk->sk_socket) { |
| 517 | /* outgoing subflow */ |
| 518 | sock_release(sock); |
| 519 | } else { |
| 520 | /* incoming subflow */ |
| 521 | tcp_close(ssk, timeout); |
| 522 | } |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame^] | 525 | static int __mptcp_init_sock(struct sock *sk) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 526 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 527 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 528 | |
| 529 | INIT_LIST_HEAD(&msk->conn_list); |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 530 | __set_bit(MPTCP_SEND_SPACE, &msk->flags); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 531 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame^] | 535 | static int mptcp_init_sock(struct sock *sk) |
| 536 | { |
| 537 | if (!mptcp_is_enabled(sock_net(sk))) |
| 538 | return -ENOPROTOOPT; |
| 539 | |
| 540 | return __mptcp_init_sock(sk); |
| 541 | } |
| 542 | |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 543 | static void mptcp_subflow_shutdown(struct sock *ssk, int how) |
| 544 | { |
| 545 | lock_sock(ssk); |
| 546 | |
| 547 | switch (ssk->sk_state) { |
| 548 | case TCP_LISTEN: |
| 549 | if (!(how & RCV_SHUTDOWN)) |
| 550 | break; |
| 551 | /* fall through */ |
| 552 | case TCP_SYN_SENT: |
| 553 | tcp_disconnect(ssk, O_NONBLOCK); |
| 554 | break; |
| 555 | default: |
| 556 | ssk->sk_shutdown |= how; |
| 557 | tcp_shutdown(ssk, how); |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | /* Wake up anyone sleeping in poll. */ |
| 562 | ssk->sk_state_change(ssk); |
| 563 | release_sock(ssk); |
| 564 | } |
| 565 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 566 | static void mptcp_close(struct sock *sk, long timeout) |
| 567 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 568 | struct mptcp_subflow_context *subflow, *tmp; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 569 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 570 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 571 | mptcp_token_destroy(msk->token); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 572 | inet_sk_state_store(sk, TCP_CLOSE); |
| 573 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 574 | lock_sock(sk); |
| 575 | |
| 576 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 577 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 578 | |
| 579 | __mptcp_close_ssk(sk, ssk, subflow, timeout); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 582 | if (msk->cached_ext) |
| 583 | __skb_ext_put(msk->cached_ext); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 584 | release_sock(sk); |
| 585 | sk_common_release(sk); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 588 | static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) |
| 589 | { |
| 590 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 591 | const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); |
| 592 | struct ipv6_pinfo *msk6 = inet6_sk(msk); |
| 593 | |
| 594 | msk->sk_v6_daddr = ssk->sk_v6_daddr; |
| 595 | msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; |
| 596 | |
| 597 | if (msk6 && ssk6) { |
| 598 | msk6->saddr = ssk6->saddr; |
| 599 | msk6->flow_label = ssk6->flow_label; |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; |
| 604 | inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; |
| 605 | inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; |
| 606 | inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; |
| 607 | inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; |
| 608 | inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; |
| 609 | } |
| 610 | |
| 611 | static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, |
| 612 | bool kern) |
| 613 | { |
| 614 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 615 | struct socket *listener; |
| 616 | struct sock *newsk; |
| 617 | |
| 618 | listener = __mptcp_nmpc_socket(msk); |
| 619 | if (WARN_ON_ONCE(!listener)) { |
| 620 | *err = -EINVAL; |
| 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); |
| 625 | newsk = inet_csk_accept(listener->sk, flags, err, kern); |
| 626 | if (!newsk) |
| 627 | return NULL; |
| 628 | |
| 629 | pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); |
| 630 | |
| 631 | if (sk_is_mptcp(newsk)) { |
| 632 | struct mptcp_subflow_context *subflow; |
| 633 | struct sock *new_mptcp_sock; |
| 634 | struct sock *ssk = newsk; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 635 | u64 ack_seq; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 636 | |
| 637 | subflow = mptcp_subflow_ctx(newsk); |
| 638 | lock_sock(sk); |
| 639 | |
| 640 | local_bh_disable(); |
| 641 | new_mptcp_sock = sk_clone_lock(sk, GFP_ATOMIC); |
| 642 | if (!new_mptcp_sock) { |
| 643 | *err = -ENOBUFS; |
| 644 | local_bh_enable(); |
| 645 | release_sock(sk); |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 646 | mptcp_subflow_shutdown(newsk, SHUT_RDWR + 1); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 647 | tcp_close(newsk, 0); |
| 648 | return NULL; |
| 649 | } |
| 650 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame^] | 651 | __mptcp_init_sock(new_mptcp_sock); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 652 | |
| 653 | msk = mptcp_sk(new_mptcp_sock); |
| 654 | msk->remote_key = subflow->remote_key; |
| 655 | msk->local_key = subflow->local_key; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 656 | msk->token = subflow->token; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 657 | msk->subflow = NULL; |
| 658 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 659 | mptcp_token_update_accept(newsk, new_mptcp_sock); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 660 | |
| 661 | mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); |
| 662 | msk->write_seq = subflow->idsn + 1; |
| 663 | ack_seq++; |
| 664 | msk->ack_seq = ack_seq; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 665 | newsk = new_mptcp_sock; |
| 666 | mptcp_copy_inaddrs(newsk, ssk); |
| 667 | list_add(&subflow->node, &msk->conn_list); |
| 668 | |
| 669 | /* will be fully established at mptcp_stream_accept() |
| 670 | * completion. |
| 671 | */ |
| 672 | inet_sk_state_store(new_mptcp_sock, TCP_SYN_RECV); |
| 673 | bh_unlock_sock(new_mptcp_sock); |
| 674 | local_bh_enable(); |
| 675 | release_sock(sk); |
Paolo Abeni | 7a6a6cb | 2020-01-21 16:56:26 -0800 | [diff] [blame] | 676 | |
| 677 | /* the subflow can already receive packet, avoid racing with |
| 678 | * the receive path and process the pending ones |
| 679 | */ |
| 680 | lock_sock(ssk); |
| 681 | subflow->map_seq = ack_seq; |
| 682 | subflow->map_subflow_seq = 1; |
| 683 | subflow->rel_write_seq = 1; |
| 684 | subflow->tcp_sock = ssk; |
| 685 | subflow->conn = new_mptcp_sock; |
| 686 | if (unlikely(!skb_queue_empty(&ssk->sk_receive_queue))) |
| 687 | mptcp_subflow_data_available(ssk); |
| 688 | release_sock(ssk); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | return newsk; |
| 692 | } |
| 693 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 694 | static void mptcp_destroy(struct sock *sk) |
| 695 | { |
| 696 | } |
| 697 | |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 698 | static int mptcp_setsockopt(struct sock *sk, int level, int optname, |
| 699 | char __user *uoptval, unsigned int optlen) |
| 700 | { |
| 701 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 702 | char __kernel *optval; |
| 703 | int ret = -EOPNOTSUPP; |
| 704 | struct socket *ssock; |
| 705 | |
| 706 | /* will be treated as __user in tcp_setsockopt */ |
| 707 | optval = (char __kernel __force *)uoptval; |
| 708 | |
| 709 | pr_debug("msk=%p", msk); |
| 710 | |
| 711 | /* @@ the meaning of setsockopt() when the socket is connected and |
| 712 | * there are multiple subflows is not defined. |
| 713 | */ |
| 714 | lock_sock(sk); |
| 715 | ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); |
| 716 | if (!IS_ERR(ssock)) { |
| 717 | pr_debug("subflow=%p", ssock->sk); |
| 718 | ret = kernel_setsockopt(ssock, level, optname, optval, optlen); |
| 719 | } |
| 720 | release_sock(sk); |
| 721 | |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | static int mptcp_getsockopt(struct sock *sk, int level, int optname, |
| 726 | char __user *uoptval, int __user *uoption) |
| 727 | { |
| 728 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 729 | char __kernel *optval; |
| 730 | int ret = -EOPNOTSUPP; |
| 731 | int __kernel *option; |
| 732 | struct socket *ssock; |
| 733 | |
| 734 | /* will be treated as __user in tcp_getsockopt */ |
| 735 | optval = (char __kernel __force *)uoptval; |
| 736 | option = (int __kernel __force *)uoption; |
| 737 | |
| 738 | pr_debug("msk=%p", msk); |
| 739 | |
| 740 | /* @@ the meaning of getsockopt() when the socket is connected and |
| 741 | * there are multiple subflows is not defined. |
| 742 | */ |
| 743 | lock_sock(sk); |
| 744 | ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); |
| 745 | if (!IS_ERR(ssock)) { |
| 746 | pr_debug("subflow=%p", ssock->sk); |
| 747 | ret = kernel_getsockopt(ssock, level, optname, optval, option); |
| 748 | } |
| 749 | release_sock(sk); |
| 750 | |
| 751 | return ret; |
| 752 | } |
| 753 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 754 | static int mptcp_get_port(struct sock *sk, unsigned short snum) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 755 | { |
| 756 | struct mptcp_sock *msk = mptcp_sk(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 757 | struct socket *ssock; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 758 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 759 | ssock = __mptcp_nmpc_socket(msk); |
| 760 | pr_debug("msk=%p, subflow=%p", msk, ssock); |
| 761 | if (WARN_ON_ONCE(!ssock)) |
| 762 | return -EINVAL; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 763 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 764 | return inet_csk_get_port(ssock->sk, snum); |
| 765 | } |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 766 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 767 | void mptcp_finish_connect(struct sock *ssk) |
| 768 | { |
| 769 | struct mptcp_subflow_context *subflow; |
| 770 | struct mptcp_sock *msk; |
| 771 | struct sock *sk; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 772 | u64 ack_seq; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 773 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 774 | subflow = mptcp_subflow_ctx(ssk); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 775 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 776 | if (!subflow->mp_capable) |
| 777 | return; |
| 778 | |
| 779 | sk = subflow->conn; |
| 780 | msk = mptcp_sk(sk); |
| 781 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 782 | pr_debug("msk=%p, token=%u", sk, subflow->token); |
| 783 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 784 | mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); |
| 785 | ack_seq++; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 786 | subflow->map_seq = ack_seq; |
| 787 | subflow->map_subflow_seq = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 788 | subflow->rel_write_seq = 1; |
| 789 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 790 | /* the socket is not connected yet, no msk/subflow ops can access/race |
| 791 | * accessing the field below |
| 792 | */ |
| 793 | WRITE_ONCE(msk->remote_key, subflow->remote_key); |
| 794 | WRITE_ONCE(msk->local_key, subflow->local_key); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 795 | WRITE_ONCE(msk->token, subflow->token); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 796 | WRITE_ONCE(msk->write_seq, subflow->idsn + 1); |
| 797 | WRITE_ONCE(msk->ack_seq, ack_seq); |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 800 | static void mptcp_sock_graft(struct sock *sk, struct socket *parent) |
| 801 | { |
| 802 | write_lock_bh(&sk->sk_callback_lock); |
| 803 | rcu_assign_pointer(sk->sk_wq, &parent->wq); |
| 804 | sk_set_socket(sk, parent); |
| 805 | sk->sk_uid = SOCK_INODE(parent)->i_uid; |
| 806 | write_unlock_bh(&sk->sk_callback_lock); |
| 807 | } |
| 808 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 809 | static bool mptcp_memory_free(const struct sock *sk, int wake) |
| 810 | { |
| 811 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 812 | |
| 813 | return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; |
| 814 | } |
| 815 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 816 | static struct proto mptcp_prot = { |
| 817 | .name = "MPTCP", |
| 818 | .owner = THIS_MODULE, |
| 819 | .init = mptcp_init_sock, |
| 820 | .close = mptcp_close, |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 821 | .accept = mptcp_accept, |
Peter Krystad | 717e79c | 2020-01-21 16:56:22 -0800 | [diff] [blame] | 822 | .setsockopt = mptcp_setsockopt, |
| 823 | .getsockopt = mptcp_getsockopt, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 824 | .shutdown = tcp_shutdown, |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 825 | .destroy = mptcp_destroy, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 826 | .sendmsg = mptcp_sendmsg, |
| 827 | .recvmsg = mptcp_recvmsg, |
| 828 | .hash = inet_hash, |
| 829 | .unhash = inet_unhash, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 830 | .get_port = mptcp_get_port, |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 831 | .stream_memory_free = mptcp_memory_free, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 832 | .obj_size = sizeof(struct mptcp_sock), |
| 833 | .no_autobind = true, |
| 834 | }; |
| 835 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 836 | static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 837 | { |
| 838 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 839 | struct socket *ssock; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 840 | int err; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 841 | |
| 842 | lock_sock(sock->sk); |
| 843 | ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); |
| 844 | if (IS_ERR(ssock)) { |
| 845 | err = PTR_ERR(ssock); |
| 846 | goto unlock; |
| 847 | } |
| 848 | |
| 849 | err = ssock->ops->bind(ssock, uaddr, addr_len); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 850 | if (!err) |
| 851 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 852 | |
| 853 | unlock: |
| 854 | release_sock(sock->sk); |
| 855 | return err; |
| 856 | } |
| 857 | |
| 858 | static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, |
| 859 | int addr_len, int flags) |
| 860 | { |
| 861 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 862 | struct socket *ssock; |
| 863 | int err; |
| 864 | |
| 865 | lock_sock(sock->sk); |
| 866 | ssock = __mptcp_socket_create(msk, TCP_SYN_SENT); |
| 867 | if (IS_ERR(ssock)) { |
| 868 | err = PTR_ERR(ssock); |
| 869 | goto unlock; |
| 870 | } |
| 871 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 872 | #ifdef CONFIG_TCP_MD5SIG |
| 873 | /* no MPTCP if MD5SIG is enabled on this socket or we may run out of |
| 874 | * TCP option space. |
| 875 | */ |
| 876 | if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) |
| 877 | mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0; |
| 878 | #endif |
| 879 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 880 | err = ssock->ops->connect(ssock, uaddr, addr_len, flags); |
| 881 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 882 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 883 | |
| 884 | unlock: |
| 885 | release_sock(sock->sk); |
| 886 | return err; |
| 887 | } |
| 888 | |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 889 | static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr, |
| 890 | int peer) |
| 891 | { |
| 892 | if (sock->sk->sk_prot == &tcp_prot) { |
| 893 | /* we are being invoked from __sys_accept4, after |
| 894 | * mptcp_accept() has just accepted a non-mp-capable |
| 895 | * flow: sk is a tcp_sk, not an mptcp one. |
| 896 | * |
| 897 | * Hand the socket over to tcp so all further socket ops |
| 898 | * bypass mptcp. |
| 899 | */ |
| 900 | sock->ops = &inet_stream_ops; |
| 901 | } |
| 902 | |
| 903 | return inet_getname(sock, uaddr, peer); |
| 904 | } |
| 905 | |
| 906 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 907 | static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr, |
| 908 | int peer) |
| 909 | { |
| 910 | if (sock->sk->sk_prot == &tcpv6_prot) { |
| 911 | /* we are being invoked from __sys_accept4 after |
| 912 | * mptcp_accept() has accepted a non-mp-capable |
| 913 | * subflow: sk is a tcp_sk, not mptcp. |
| 914 | * |
| 915 | * Hand the socket over to tcp so all further |
| 916 | * socket ops bypass mptcp. |
| 917 | */ |
| 918 | sock->ops = &inet6_stream_ops; |
| 919 | } |
| 920 | |
| 921 | return inet6_getname(sock, uaddr, peer); |
| 922 | } |
| 923 | #endif |
| 924 | |
| 925 | static int mptcp_listen(struct socket *sock, int backlog) |
| 926 | { |
| 927 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 928 | struct socket *ssock; |
| 929 | int err; |
| 930 | |
| 931 | pr_debug("msk=%p", msk); |
| 932 | |
| 933 | lock_sock(sock->sk); |
| 934 | ssock = __mptcp_socket_create(msk, TCP_LISTEN); |
| 935 | if (IS_ERR(ssock)) { |
| 936 | err = PTR_ERR(ssock); |
| 937 | goto unlock; |
| 938 | } |
| 939 | |
| 940 | err = ssock->ops->listen(ssock, backlog); |
| 941 | inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); |
| 942 | if (!err) |
| 943 | mptcp_copy_inaddrs(sock->sk, ssock->sk); |
| 944 | |
| 945 | unlock: |
| 946 | release_sock(sock->sk); |
| 947 | return err; |
| 948 | } |
| 949 | |
| 950 | static bool is_tcp_proto(const struct proto *p) |
| 951 | { |
| 952 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 953 | return p == &tcp_prot || p == &tcpv6_prot; |
| 954 | #else |
| 955 | return p == &tcp_prot; |
| 956 | #endif |
| 957 | } |
| 958 | |
| 959 | static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, |
| 960 | int flags, bool kern) |
| 961 | { |
| 962 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 963 | struct socket *ssock; |
| 964 | int err; |
| 965 | |
| 966 | pr_debug("msk=%p", msk); |
| 967 | |
| 968 | lock_sock(sock->sk); |
| 969 | if (sock->sk->sk_state != TCP_LISTEN) |
| 970 | goto unlock_fail; |
| 971 | |
| 972 | ssock = __mptcp_nmpc_socket(msk); |
| 973 | if (!ssock) |
| 974 | goto unlock_fail; |
| 975 | |
| 976 | sock_hold(ssock->sk); |
| 977 | release_sock(sock->sk); |
| 978 | |
| 979 | err = ssock->ops->accept(sock, newsock, flags, kern); |
| 980 | if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) { |
| 981 | struct mptcp_sock *msk = mptcp_sk(newsock->sk); |
| 982 | struct mptcp_subflow_context *subflow; |
| 983 | |
| 984 | /* set ssk->sk_socket of accept()ed flows to mptcp socket. |
| 985 | * This is needed so NOSPACE flag can be set from tcp stack. |
| 986 | */ |
| 987 | list_for_each_entry(subflow, &msk->conn_list, node) { |
| 988 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 989 | |
| 990 | if (!ssk->sk_socket) |
| 991 | mptcp_sock_graft(ssk, newsock); |
| 992 | } |
| 993 | |
| 994 | inet_sk_state_store(newsock->sk, TCP_ESTABLISHED); |
| 995 | } |
| 996 | |
| 997 | sock_put(ssock->sk); |
| 998 | return err; |
| 999 | |
| 1000 | unlock_fail: |
| 1001 | release_sock(sock->sk); |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1005 | static __poll_t mptcp_poll(struct file *file, struct socket *sock, |
| 1006 | struct poll_table_struct *wait) |
| 1007 | { |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1008 | const struct mptcp_sock *msk; |
| 1009 | struct sock *sk = sock->sk; |
| 1010 | struct socket *ssock; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1011 | __poll_t mask = 0; |
| 1012 | |
Florian Westphal | 1891c4a | 2020-01-21 16:56:25 -0800 | [diff] [blame] | 1013 | msk = mptcp_sk(sk); |
| 1014 | lock_sock(sk); |
| 1015 | ssock = __mptcp_nmpc_socket(msk); |
| 1016 | if (ssock) { |
| 1017 | mask = ssock->ops->poll(file, ssock, wait); |
| 1018 | release_sock(sk); |
| 1019 | return mask; |
| 1020 | } |
| 1021 | |
| 1022 | release_sock(sk); |
| 1023 | sock_poll_wait(file, sock, wait); |
| 1024 | lock_sock(sk); |
| 1025 | |
| 1026 | if (test_bit(MPTCP_DATA_READY, &msk->flags)) |
| 1027 | mask = EPOLLIN | EPOLLRDNORM; |
| 1028 | if (sk_stream_is_writeable(sk) && |
| 1029 | test_bit(MPTCP_SEND_SPACE, &msk->flags)) |
| 1030 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 1031 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1032 | mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
| 1033 | |
| 1034 | release_sock(sk); |
| 1035 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1036 | return mask; |
| 1037 | } |
| 1038 | |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1039 | static int mptcp_shutdown(struct socket *sock, int how) |
| 1040 | { |
| 1041 | struct mptcp_sock *msk = mptcp_sk(sock->sk); |
| 1042 | struct mptcp_subflow_context *subflow; |
| 1043 | int ret = 0; |
| 1044 | |
| 1045 | pr_debug("sk=%p, how=%d", msk, how); |
| 1046 | |
| 1047 | lock_sock(sock->sk); |
| 1048 | |
| 1049 | if (how == SHUT_WR || how == SHUT_RDWR) |
| 1050 | inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); |
| 1051 | |
| 1052 | how++; |
| 1053 | |
| 1054 | if ((how & ~SHUTDOWN_MASK) || !how) { |
| 1055 | ret = -EINVAL; |
| 1056 | goto out_unlock; |
| 1057 | } |
| 1058 | |
| 1059 | if (sock->state == SS_CONNECTING) { |
| 1060 | if ((1 << sock->sk->sk_state) & |
| 1061 | (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) |
| 1062 | sock->state = SS_DISCONNECTING; |
| 1063 | else |
| 1064 | sock->state = SS_CONNECTED; |
| 1065 | } |
| 1066 | |
| 1067 | mptcp_for_each_subflow(msk, subflow) { |
| 1068 | struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); |
| 1069 | |
| 1070 | mptcp_subflow_shutdown(tcp_sk, how); |
| 1071 | } |
| 1072 | |
| 1073 | out_unlock: |
| 1074 | release_sock(sock->sk); |
| 1075 | |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1079 | static struct proto_ops mptcp_stream_ops; |
| 1080 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1081 | static struct inet_protosw mptcp_protosw = { |
| 1082 | .type = SOCK_STREAM, |
| 1083 | .protocol = IPPROTO_MPTCP, |
| 1084 | .prot = &mptcp_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1085 | .ops = &mptcp_stream_ops, |
| 1086 | .flags = INET_PROTOSW_ICSK, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1087 | }; |
| 1088 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame^] | 1089 | void mptcp_proto_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1090 | { |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1091 | mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; |
| 1092 | mptcp_stream_ops = inet_stream_ops; |
| 1093 | mptcp_stream_ops.bind = mptcp_bind; |
| 1094 | mptcp_stream_ops.connect = mptcp_stream_connect; |
| 1095 | mptcp_stream_ops.poll = mptcp_poll; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1096 | mptcp_stream_ops.accept = mptcp_stream_accept; |
| 1097 | mptcp_stream_ops.getname = mptcp_v4_getname; |
| 1098 | mptcp_stream_ops.listen = mptcp_listen; |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1099 | mptcp_stream_ops.shutdown = mptcp_shutdown; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1100 | |
| 1101 | mptcp_subflow_init(); |
| 1102 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1103 | if (proto_register(&mptcp_prot, 1) != 0) |
| 1104 | panic("Failed to register MPTCP proto.\n"); |
| 1105 | |
| 1106 | inet_register_protosw(&mptcp_protosw); |
| 1107 | } |
| 1108 | |
| 1109 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1110 | static struct proto_ops mptcp_v6_stream_ops; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1111 | static struct proto mptcp_v6_prot; |
| 1112 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1113 | static void mptcp_v6_destroy(struct sock *sk) |
| 1114 | { |
| 1115 | mptcp_destroy(sk); |
| 1116 | inet6_destroy_sock(sk); |
| 1117 | } |
| 1118 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1119 | static struct inet_protosw mptcp_v6_protosw = { |
| 1120 | .type = SOCK_STREAM, |
| 1121 | .protocol = IPPROTO_MPTCP, |
| 1122 | .prot = &mptcp_v6_prot, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1123 | .ops = &mptcp_v6_stream_ops, |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1124 | .flags = INET_PROTOSW_ICSK, |
| 1125 | }; |
| 1126 | |
Matthieu Baerts | 784325e | 2020-01-21 16:56:28 -0800 | [diff] [blame^] | 1127 | int mptcp_proto_v6_init(void) |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1128 | { |
| 1129 | int err; |
| 1130 | |
| 1131 | mptcp_v6_prot = mptcp_prot; |
| 1132 | strcpy(mptcp_v6_prot.name, "MPTCPv6"); |
| 1133 | mptcp_v6_prot.slab = NULL; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1134 | mptcp_v6_prot.destroy = mptcp_v6_destroy; |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1135 | mptcp_v6_prot.obj_size = sizeof(struct mptcp_sock) + |
| 1136 | sizeof(struct ipv6_pinfo); |
| 1137 | |
| 1138 | err = proto_register(&mptcp_v6_prot, 1); |
| 1139 | if (err) |
| 1140 | return err; |
| 1141 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1142 | mptcp_v6_stream_ops = inet6_stream_ops; |
| 1143 | mptcp_v6_stream_ops.bind = mptcp_bind; |
| 1144 | mptcp_v6_stream_ops.connect = mptcp_stream_connect; |
| 1145 | mptcp_v6_stream_ops.poll = mptcp_poll; |
Peter Krystad | cf7da0d | 2020-01-21 16:56:19 -0800 | [diff] [blame] | 1146 | mptcp_v6_stream_ops.accept = mptcp_stream_accept; |
| 1147 | mptcp_v6_stream_ops.getname = mptcp_v6_getname; |
| 1148 | mptcp_v6_stream_ops.listen = mptcp_listen; |
Peter Krystad | 2149849 | 2020-01-21 16:56:21 -0800 | [diff] [blame] | 1149 | mptcp_v6_stream_ops.shutdown = mptcp_shutdown; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1150 | |
Mat Martineau | f870fa0 | 2020-01-21 16:56:15 -0800 | [diff] [blame] | 1151 | err = inet6_register_protosw(&mptcp_v6_protosw); |
| 1152 | if (err) |
| 1153 | proto_unregister(&mptcp_v6_prot); |
| 1154 | |
| 1155 | return err; |
| 1156 | } |
| 1157 | #endif |