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