Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP |
| 3 | * |
| 4 | * Copyright (c) 2017 - 2019, Intel Corporation. |
| 5 | */ |
| 6 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 7 | #define pr_fmt(fmt) "MPTCP: " fmt |
| 8 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/netdevice.h> |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 12 | #include <crypto/algapi.h> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 13 | #include <crypto/sha2.h> |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -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 | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 19 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 20 | #include <net/ip6_route.h> |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 21 | #include <net/transp_v6.h> |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 22 | #endif |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 23 | #include <net/mptcp.h> |
Paolo Abeni | 4596a2c | 2020-09-14 10:01:16 +0200 | [diff] [blame] | 24 | #include <uapi/linux/mptcp.h> |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 25 | #include "protocol.h" |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 26 | #include "mib.h" |
| 27 | |
Geliang Tang | 0918e34 | 2021-04-16 15:38:05 -0700 | [diff] [blame] | 28 | #include <trace/events/mptcp.h> |
| 29 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 30 | static void mptcp_subflow_ops_undo_override(struct sock *ssk); |
| 31 | |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 32 | static void SUBFLOW_REQ_INC_STATS(struct request_sock *req, |
| 33 | enum linux_mptcp_mib_field field) |
| 34 | { |
| 35 | MPTCP_INC_STATS(sock_net(req_to_sk(req)), field); |
| 36 | } |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 37 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 38 | static void subflow_req_destructor(struct request_sock *req) |
| 39 | { |
| 40 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 41 | |
| 42 | pr_debug("subflow_req=%p", subflow_req); |
| 43 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 44 | if (subflow_req->msk) |
| 45 | sock_put((struct sock *)subflow_req->msk); |
| 46 | |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 47 | mptcp_token_destroy_request(req); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 48 | tcp_request_sock_ops.destructor(req); |
| 49 | } |
| 50 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 51 | static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2, |
| 52 | void *hmac) |
| 53 | { |
| 54 | u8 msg[8]; |
| 55 | |
| 56 | put_unaligned_be32(nonce1, &msg[0]); |
| 57 | put_unaligned_be32(nonce2, &msg[4]); |
| 58 | |
| 59 | mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac); |
| 60 | } |
| 61 | |
Paolo Abeni | 4cf8b7e | 2020-07-23 13:02:36 +0200 | [diff] [blame] | 62 | static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk) |
| 63 | { |
| 64 | return mptcp_is_fully_established((void *)msk) && |
| 65 | READ_ONCE(msk->pm.accept_subflow); |
| 66 | } |
| 67 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 68 | /* validate received token and create truncated hmac and nonce for SYN-ACK */ |
Geliang Tang | ec20e14 | 2021-02-01 15:09:14 -0800 | [diff] [blame] | 69 | static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req) |
| 70 | { |
| 71 | struct mptcp_sock *msk = subflow_req->msk; |
| 72 | u8 hmac[SHA256_DIGEST_SIZE]; |
| 73 | |
| 74 | get_random_bytes(&subflow_req->local_nonce, sizeof(u32)); |
| 75 | |
| 76 | subflow_generate_hmac(msk->local_key, msk->remote_key, |
| 77 | subflow_req->local_nonce, |
| 78 | subflow_req->remote_nonce, hmac); |
| 79 | |
| 80 | subflow_req->thmac = get_unaligned_be64(hmac); |
| 81 | } |
| 82 | |
Geliang Tang | b5e2e42 | 2021-02-01 15:09:13 -0800 | [diff] [blame] | 83 | static struct mptcp_sock *subflow_token_join_request(struct request_sock *req) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 84 | { |
| 85 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 86 | struct mptcp_sock *msk; |
| 87 | int local_id; |
| 88 | |
Florian Westphal | ea1300b | 2021-09-23 17:04:11 -0700 | [diff] [blame] | 89 | msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 90 | if (!msk) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 91 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN); |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 92 | return NULL; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req); |
| 96 | if (local_id < 0) { |
| 97 | sock_put((struct sock *)msk); |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 98 | return NULL; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 99 | } |
| 100 | subflow_req->local_id = local_id; |
| 101 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 102 | return msk; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 105 | static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 106 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 107 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 108 | |
| 109 | subflow_req->mp_capable = 0; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 110 | subflow_req->mp_join = 0; |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 111 | subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener)); |
Geliang Tang | bab6b88 | 2021-06-22 12:25:19 -0700 | [diff] [blame] | 112 | subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener)); |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 113 | subflow_req->msk = NULL; |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 114 | mptcp_token_init_request(req); |
Florian Westphal | 78d8b7b | 2020-07-30 21:25:52 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 117 | static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk) |
| 118 | { |
| 119 | return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport; |
| 120 | } |
| 121 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 122 | static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason) |
| 123 | { |
| 124 | struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP); |
| 125 | |
| 126 | if (mpext) { |
| 127 | memset(mpext, 0, sizeof(*mpext)); |
| 128 | mpext->reset_reason = reason; |
| 129 | } |
| 130 | } |
| 131 | |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 132 | /* Init mptcp request socket. |
| 133 | * |
| 134 | * Returns an error code if a JOIN has failed and a TCP reset |
| 135 | * should be sent. |
| 136 | */ |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 137 | static int subflow_check_req(struct request_sock *req, |
| 138 | const struct sock *sk_listener, |
| 139 | struct sk_buff *skb) |
Florian Westphal | 78d8b7b | 2020-07-30 21:25:52 +0200 | [diff] [blame] | 140 | { |
| 141 | struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); |
| 142 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 143 | struct mptcp_options_received mp_opt; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 144 | bool opt_mp_capable, opt_mp_join; |
Florian Westphal | 78d8b7b | 2020-07-30 21:25:52 +0200 | [diff] [blame] | 145 | |
| 146 | pr_debug("subflow_req=%p, listener=%p", subflow_req, listener); |
| 147 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 148 | #ifdef CONFIG_TCP_MD5SIG |
| 149 | /* no MPTCP if MD5SIG is enabled on this socket or we may run out of |
| 150 | * TCP option space. |
| 151 | */ |
| 152 | if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) |
| 153 | return -EINVAL; |
| 154 | #endif |
Florian Westphal | 78d8b7b | 2020-07-30 21:25:52 +0200 | [diff] [blame] | 155 | |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 156 | mptcp_get_options(sk_listener, skb, &mp_opt); |
Florian Westphal | 78d8b7b | 2020-07-30 21:25:52 +0200 | [diff] [blame] | 157 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 158 | opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC); |
| 159 | opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ); |
| 160 | if (opt_mp_capable) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 161 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE); |
| 162 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 163 | if (opt_mp_join) |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 164 | return 0; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 165 | } else if (opt_mp_join) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 166 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX); |
| 167 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 168 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 169 | if (opt_mp_capable && listener->request_mptcp) { |
Jianguo Wu | c68a0cd | 2021-05-27 16:54:25 -0700 | [diff] [blame] | 170 | int err, retries = MPTCP_TOKEN_MAX_RETRIES; |
Florian Westphal | 535fb81 | 2020-07-30 21:25:51 +0200 | [diff] [blame] | 171 | |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 172 | subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; |
Florian Westphal | 535fb81 | 2020-07-30 21:25:51 +0200 | [diff] [blame] | 173 | again: |
| 174 | do { |
| 175 | get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key)); |
| 176 | } while (subflow_req->local_key == 0); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 177 | |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 178 | if (unlikely(req->syncookie)) { |
| 179 | mptcp_crypto_key_sha(subflow_req->local_key, |
| 180 | &subflow_req->token, |
| 181 | &subflow_req->idsn); |
| 182 | if (mptcp_token_exists(subflow_req->token)) { |
| 183 | if (retries-- > 0) |
| 184 | goto again; |
Paolo Abeni | a16195e | 2021-04-01 16:19:41 -0700 | [diff] [blame] | 185 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 186 | } else { |
| 187 | subflow_req->mp_capable = 1; |
| 188 | } |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 189 | return 0; |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 192 | err = mptcp_token_new_request(req); |
| 193 | if (err == 0) |
| 194 | subflow_req->mp_capable = 1; |
Florian Westphal | 535fb81 | 2020-07-30 21:25:51 +0200 | [diff] [blame] | 195 | else if (retries-- > 0) |
| 196 | goto again; |
Paolo Abeni | a16195e | 2021-04-01 16:19:41 -0700 | [diff] [blame] | 197 | else |
| 198 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 199 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 200 | } else if (opt_mp_join && listener->request_mptcp) { |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 201 | subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 202 | subflow_req->mp_join = 1; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 203 | subflow_req->backup = mp_opt.backup; |
| 204 | subflow_req->remote_id = mp_opt.join_id; |
| 205 | subflow_req->token = mp_opt.token; |
| 206 | subflow_req->remote_nonce = mp_opt.nonce; |
Geliang Tang | b5e2e42 | 2021-02-01 15:09:13 -0800 | [diff] [blame] | 207 | subflow_req->msk = subflow_token_join_request(req); |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 208 | |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 209 | /* Can't fall back to TCP in this case. */ |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 210 | if (!subflow_req->msk) { |
| 211 | subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 212 | return -EPERM; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 213 | } |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 214 | |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 215 | if (subflow_use_different_sport(subflow_req->msk, sk_listener)) { |
| 216 | pr_debug("syn inet_sport=%d %d", |
| 217 | ntohs(inet_sk(sk_listener)->inet_sport), |
| 218 | ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport)); |
| 219 | if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) { |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 220 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX); |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 221 | return -EPERM; |
| 222 | } |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 223 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX); |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Geliang Tang | ec20e14 | 2021-02-01 15:09:14 -0800 | [diff] [blame] | 226 | subflow_req_create_thmac(subflow_req); |
| 227 | |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 228 | if (unlikely(req->syncookie)) { |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 229 | if (mptcp_can_accept_new_subflow(subflow_req->msk)) |
| 230 | subflow_init_req_cookie_join_save(subflow_req, skb); |
Jianguo Wu | 8547ea5 | 2021-07-09 17:20:48 -0700 | [diff] [blame] | 231 | else |
| 232 | return -EPERM; |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 235 | pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token, |
| 236 | subflow_req->remote_nonce, subflow_req->msk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 237 | } |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 238 | |
| 239 | return 0; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 242 | int mptcp_subflow_init_cookie_req(struct request_sock *req, |
| 243 | const struct sock *sk_listener, |
| 244 | struct sk_buff *skb) |
| 245 | { |
| 246 | struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); |
| 247 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 248 | struct mptcp_options_received mp_opt; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 249 | bool opt_mp_capable, opt_mp_join; |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 250 | int err; |
| 251 | |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 252 | subflow_init_req(req, sk_listener); |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 253 | mptcp_get_options(sk_listener, skb, &mp_opt); |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 254 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 255 | opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC); |
| 256 | opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ); |
| 257 | if (opt_mp_capable && opt_mp_join) |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 258 | return -EINVAL; |
| 259 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 260 | if (opt_mp_capable && listener->request_mptcp) { |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 261 | if (mp_opt.sndr_key == 0) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | subflow_req->local_key = mp_opt.rcvr_key; |
| 265 | err = mptcp_token_new_request(req); |
| 266 | if (err) |
| 267 | return err; |
| 268 | |
| 269 | subflow_req->mp_capable = 1; |
| 270 | subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 271 | } else if (opt_mp_join && listener->request_mptcp) { |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 272 | if (!mptcp_token_join_cookie_init_state(subflow_req, skb)) |
| 273 | return -EINVAL; |
| 274 | |
Jianguo Wu | 8547ea5 | 2021-07-09 17:20:48 -0700 | [diff] [blame] | 275 | subflow_req->mp_join = 1; |
Florian Westphal | 9466a1c | 2020-07-30 21:25:56 +0200 | [diff] [blame] | 276 | subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; |
Florian Westphal | c83a47e | 2020-07-30 21:25:54 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req); |
| 282 | |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 283 | static struct dst_entry *subflow_v4_route_req(const struct sock *sk, |
| 284 | struct sk_buff *skb, |
| 285 | struct flowi *fl, |
| 286 | struct request_sock *req) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 287 | { |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 288 | struct dst_entry *dst; |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 289 | int err; |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 290 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 291 | tcp_rsk(req)->is_mptcp = 1; |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 292 | subflow_init_req(req, sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 293 | |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 294 | dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req); |
| 295 | if (!dst) |
| 296 | return NULL; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 297 | |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 298 | err = subflow_check_req(req, sk, skb); |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 299 | if (err == 0) |
| 300 | return dst; |
| 301 | |
| 302 | dst_release(dst); |
| 303 | if (!req->syncookie) |
| 304 | tcp_request_sock_ops.send_reset(sk, skb); |
| 305 | return NULL; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 309 | static struct dst_entry *subflow_v6_route_req(const struct sock *sk, |
| 310 | struct sk_buff *skb, |
| 311 | struct flowi *fl, |
| 312 | struct request_sock *req) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 313 | { |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 314 | struct dst_entry *dst; |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 315 | int err; |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 316 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 317 | tcp_rsk(req)->is_mptcp = 1; |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 318 | subflow_init_req(req, sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 319 | |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 320 | dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req); |
| 321 | if (!dst) |
| 322 | return NULL; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 323 | |
Paolo Abeni | d8b59ef | 2021-02-11 15:30:40 -0800 | [diff] [blame] | 324 | err = subflow_check_req(req, sk, skb); |
Florian Westphal | 3ecfbe3e | 2020-11-30 16:36:31 +0100 | [diff] [blame] | 325 | if (err == 0) |
| 326 | return dst; |
| 327 | |
| 328 | dst_release(dst); |
| 329 | if (!req->syncookie) |
| 330 | tcp6_request_sock_ops.send_reset(sk, skb); |
| 331 | return NULL; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 332 | } |
| 333 | #endif |
| 334 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 335 | /* validate received truncated hmac and create hmac for third ACK */ |
| 336 | static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow) |
| 337 | { |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 338 | u8 hmac[SHA256_DIGEST_SIZE]; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 339 | u64 thmac; |
| 340 | |
| 341 | subflow_generate_hmac(subflow->remote_key, subflow->local_key, |
| 342 | subflow->remote_nonce, subflow->local_nonce, |
| 343 | hmac); |
| 344 | |
| 345 | thmac = get_unaligned_be64(hmac); |
| 346 | pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n", |
| 347 | subflow, subflow->token, |
| 348 | (unsigned long long)thmac, |
| 349 | (unsigned long long)subflow->thmac); |
| 350 | |
| 351 | return thmac == subflow->thmac; |
| 352 | } |
| 353 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 354 | void mptcp_subflow_reset(struct sock *ssk) |
| 355 | { |
Paolo Abeni | 0e4f35d | 2020-10-09 19:00:01 +0200 | [diff] [blame] | 356 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 357 | struct sock *sk = subflow->conn; |
| 358 | |
Florian Westphal | ab82e99 | 2020-12-10 14:25:02 -0800 | [diff] [blame] | 359 | /* must hold: tcp_done() could drop last reference on parent */ |
| 360 | sock_hold(sk); |
| 361 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 362 | tcp_set_state(ssk, TCP_CLOSE); |
| 363 | tcp_send_active_reset(ssk, GFP_ATOMIC); |
| 364 | tcp_done(ssk); |
Paolo Abeni | 0e4f35d | 2020-10-09 19:00:01 +0200 | [diff] [blame] | 365 | if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) && |
| 366 | schedule_work(&mptcp_sk(sk)->work)) |
Florian Westphal | ab82e99 | 2020-12-10 14:25:02 -0800 | [diff] [blame] | 367 | return; /* worker will put sk for us */ |
| 368 | |
| 369 | sock_put(sk); |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 372 | static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk) |
| 373 | { |
| 374 | return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport; |
| 375 | } |
| 376 | |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 377 | void __mptcp_set_connected(struct sock *sk) |
| 378 | { |
| 379 | if (sk->sk_state == TCP_SYN_SENT) { |
| 380 | inet_sk_state_store(sk, TCP_ESTABLISHED); |
| 381 | sk->sk_state_change(sk); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void mptcp_set_connected(struct sock *sk) |
| 386 | { |
| 387 | mptcp_data_lock(sk); |
| 388 | if (!sock_owned_by_user(sk)) |
| 389 | __mptcp_set_connected(sk); |
| 390 | else |
Paolo Abeni | e9d09ba | 2022-01-06 16:20:26 -0800 | [diff] [blame] | 391 | __set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags); |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 392 | mptcp_data_unlock(sk); |
| 393 | } |
| 394 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 395 | static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) |
| 396 | { |
| 397 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 398 | struct mptcp_options_received mp_opt; |
Davide Caratti | c3c123d | 2020-03-19 22:45:37 +0100 | [diff] [blame] | 399 | struct sock *parent = subflow->conn; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 400 | |
| 401 | subflow->icsk_af_ops->sk_rx_dst_set(sk, skb); |
| 402 | |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 403 | /* be sure no special action on any packet other than syn-ack */ |
| 404 | if (subflow->conn_finished) |
| 405 | return; |
| 406 | |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 407 | mptcp_propagate_sndbuf(parent, sk); |
Paolo Abeni | b0977bb | 2020-07-23 13:02:29 +0200 | [diff] [blame] | 408 | subflow->rel_write_seq = 1; |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 409 | subflow->conn_finished = 1; |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 410 | subflow->ssn_offset = TCP_SKB_CB(skb)->seq; |
| 411 | pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset); |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 412 | |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 413 | mptcp_get_options(sk, skb, &mp_opt); |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 414 | if (subflow->request_mptcp) { |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 415 | if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) { |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 416 | MPTCP_INC_STATS(sock_net(sk), |
| 417 | MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); |
| 418 | mptcp_do_fallback(sk); |
| 419 | pr_fallback(mptcp_sk(subflow->conn)); |
| 420 | goto fallback; |
| 421 | } |
| 422 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 423 | if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD) |
Geliang Tang | 0625118 | 2021-06-17 16:46:13 -0700 | [diff] [blame] | 424 | WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true); |
Geliang Tang | df377be | 2021-06-22 12:25:20 -0700 | [diff] [blame] | 425 | if (mp_opt.deny_join_id0) |
| 426 | WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true); |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 427 | subflow->mp_capable = 1; |
| 428 | subflow->can_ack = 1; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 429 | subflow->remote_key = mp_opt.sndr_key; |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 430 | pr_debug("subflow=%p, remote_key=%llu", subflow, |
| 431 | subflow->remote_key); |
Paolo Abeni | 5695eb8 | 2021-04-01 16:19:42 -0700 | [diff] [blame] | 432 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK); |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 433 | mptcp_finish_connect(sk); |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 434 | mptcp_set_connected(parent); |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 435 | } else if (subflow->request_join) { |
| 436 | u8 hmac[SHA256_DIGEST_SIZE]; |
| 437 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 438 | if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ)) { |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 439 | subflow->reset_reason = MPTCP_RST_EMPTCP; |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 440 | goto do_reset; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 441 | } |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 442 | |
Paolo Abeni | 0460ce2 | 2021-08-13 15:15:47 -0700 | [diff] [blame] | 443 | subflow->backup = mp_opt.backup; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 444 | subflow->thmac = mp_opt.thmac; |
| 445 | subflow->remote_nonce = mp_opt.nonce; |
Paolo Abeni | 0460ce2 | 2021-08-13 15:15:47 -0700 | [diff] [blame] | 446 | pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d", |
| 447 | subflow, subflow->thmac, subflow->remote_nonce, |
| 448 | subflow->backup); |
Paolo Abeni | 263e120 | 2020-04-30 15:01:51 +0200 | [diff] [blame] | 449 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 450 | if (!subflow_thmac_valid(subflow)) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 451 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC); |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 452 | subflow->reset_reason = MPTCP_RST_EMPTCP; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 453 | goto do_reset; |
| 454 | } |
| 455 | |
Jianguo Wu | 0a4d8e9 | 2021-05-27 16:54:26 -0700 | [diff] [blame] | 456 | if (!mptcp_finish_join(sk)) |
| 457 | goto do_reset; |
| 458 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 459 | subflow_generate_hmac(subflow->local_key, subflow->remote_key, |
| 460 | subflow->local_nonce, |
| 461 | subflow->remote_nonce, |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 462 | hmac); |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 463 | memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 464 | |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 465 | subflow->mp_join = 1; |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 466 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX); |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 467 | |
| 468 | if (subflow_use_different_dport(mptcp_sk(parent), sk)) { |
| 469 | pr_debug("synack inet_dport=%d %d", |
| 470 | ntohs(inet_sk(sk)->inet_dport), |
| 471 | ntohs(inet_sk(parent)->inet_dport)); |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 472 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX); |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 473 | } |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 474 | } else if (mptcp_check_fallback(sk)) { |
| 475 | fallback: |
| 476 | mptcp_rcv_space_init(mptcp_sk(parent), sk); |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 477 | mptcp_set_connected(parent); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 478 | } |
Paolo Abeni | fa25e81 | 2020-07-23 13:02:33 +0200 | [diff] [blame] | 479 | return; |
| 480 | |
| 481 | do_reset: |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 482 | subflow->reset_transient = 0; |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 483 | mptcp_subflow_reset(sk); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Florian Westphal | 08b8d08 | 2020-07-30 21:25:53 +0200 | [diff] [blame] | 486 | struct request_sock_ops mptcp_subflow_request_sock_ops; |
| 487 | EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 488 | static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops; |
| 489 | |
| 490 | static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb) |
| 491 | { |
| 492 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 493 | |
| 494 | pr_debug("subflow=%p", subflow); |
| 495 | |
| 496 | /* Never answer to SYNs sent to broadcast or multicast */ |
| 497 | if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) |
| 498 | goto drop; |
| 499 | |
Florian Westphal | 08b8d08 | 2020-07-30 21:25:53 +0200 | [diff] [blame] | 500 | return tcp_conn_request(&mptcp_subflow_request_sock_ops, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 501 | &subflow_request_sock_ipv4_ops, |
| 502 | sk, skb); |
| 503 | drop: |
| 504 | tcp_listendrop(sk); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 509 | static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops; |
| 510 | static struct inet_connection_sock_af_ops subflow_v6_specific; |
| 511 | static struct inet_connection_sock_af_ops subflow_v6m_specific; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 512 | static struct proto tcpv6_prot_override; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 513 | |
| 514 | static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb) |
| 515 | { |
| 516 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 517 | |
| 518 | pr_debug("subflow=%p", subflow); |
| 519 | |
| 520 | if (skb->protocol == htons(ETH_P_IP)) |
| 521 | return subflow_v4_conn_request(sk, skb); |
| 522 | |
| 523 | if (!ipv6_unicast_destination(skb)) |
| 524 | goto drop; |
| 525 | |
Jakub Kicinski | dcc32f4f | 2021-03-17 09:55:15 -0700 | [diff] [blame] | 526 | if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) { |
| 527 | __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Florian Westphal | 08b8d08 | 2020-07-30 21:25:53 +0200 | [diff] [blame] | 531 | return tcp_conn_request(&mptcp_subflow_request_sock_ops, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 532 | &subflow_request_sock_ipv6_ops, sk, skb); |
| 533 | |
| 534 | drop: |
| 535 | tcp_listendrop(sk); |
| 536 | return 0; /* don't send reset */ |
| 537 | } |
| 538 | #endif |
| 539 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 540 | /* validate hmac received in third ACK */ |
| 541 | static bool subflow_hmac_valid(const struct request_sock *req, |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 542 | const struct mptcp_options_received *mp_opt) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 543 | { |
| 544 | const struct mptcp_subflow_request_sock *subflow_req; |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 545 | u8 hmac[SHA256_DIGEST_SIZE]; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 546 | struct mptcp_sock *msk; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 547 | |
| 548 | subflow_req = mptcp_subflow_rsk(req); |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 549 | msk = subflow_req->msk; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 550 | if (!msk) |
| 551 | return false; |
| 552 | |
| 553 | subflow_generate_hmac(msk->remote_key, msk->local_key, |
| 554 | subflow_req->remote_nonce, |
| 555 | subflow_req->local_nonce, hmac); |
| 556 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 557 | return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 560 | static void mptcp_sock_destruct(struct sock *sk) |
| 561 | { |
| 562 | /* if new mptcp socket isn't accepted, it is free'd |
| 563 | * from the tcp listener sockets request queue, linked |
| 564 | * from req->sk. The tcp socket is released. |
| 565 | * This calls the ULP release function which will |
| 566 | * also remove the mptcp socket, via |
| 567 | * sock_put(ctx->conn). |
| 568 | * |
Paolo Abeni | 7ee2492 | 2020-08-07 19:03:53 +0200 | [diff] [blame] | 569 | * Problem is that the mptcp socket will be in |
| 570 | * ESTABLISHED state and will not have the SOCK_DEAD flag. |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 571 | * Both result in warnings from inet_sock_destruct. |
| 572 | */ |
Paolo Abeni | 578c18ef | 2021-05-06 17:16:38 -0700 | [diff] [blame] | 573 | if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 574 | sk->sk_state = TCP_CLOSE; |
| 575 | WARN_ON_ONCE(sk->sk_socket); |
| 576 | sock_orphan(sk); |
| 577 | } |
| 578 | |
Geliang Tang | 5c8c164 | 2020-09-24 08:29:57 +0800 | [diff] [blame] | 579 | mptcp_destroy_common(mptcp_sk(sk)); |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 580 | inet_sock_destruct(sk); |
| 581 | } |
| 582 | |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 583 | static void mptcp_force_close(struct sock *sk) |
| 584 | { |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 585 | /* the msk is not yet exposed to user-space */ |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 586 | inet_sk_state_store(sk, TCP_CLOSE); |
| 587 | sk_common_release(sk); |
| 588 | } |
| 589 | |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 590 | static void subflow_ulp_fallback(struct sock *sk, |
| 591 | struct mptcp_subflow_context *old_ctx) |
| 592 | { |
| 593 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 594 | |
| 595 | mptcp_subflow_tcp_fallback(sk, old_ctx); |
| 596 | icsk->icsk_ulp_ops = NULL; |
| 597 | rcu_assign_pointer(icsk->icsk_ulp_data, NULL); |
| 598 | tcp_sk(sk)->is_mptcp = 0; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 599 | |
| 600 | mptcp_subflow_ops_undo_override(sk); |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Paolo Abeni | 3988460 | 2020-05-29 17:49:18 +0200 | [diff] [blame] | 603 | static void subflow_drop_ctx(struct sock *ssk) |
| 604 | { |
| 605 | struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); |
| 606 | |
| 607 | if (!ctx) |
| 608 | return; |
| 609 | |
| 610 | subflow_ulp_fallback(ssk, ctx); |
| 611 | if (ctx->conn) |
| 612 | sock_put(ctx->conn); |
| 613 | |
| 614 | kfree_rcu(ctx, rcu); |
| 615 | } |
| 616 | |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 617 | void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow, |
| 618 | struct mptcp_options_received *mp_opt) |
| 619 | { |
| 620 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
| 621 | |
| 622 | subflow->remote_key = mp_opt->sndr_key; |
| 623 | subflow->fully_established = 1; |
| 624 | subflow->can_ack = 1; |
| 625 | WRITE_ONCE(msk->fully_established, true); |
| 626 | } |
| 627 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 628 | static struct sock *subflow_syn_recv_sock(const struct sock *sk, |
| 629 | struct sk_buff *skb, |
| 630 | struct request_sock *req, |
| 631 | struct dst_entry *dst, |
| 632 | struct request_sock *req_unhash, |
| 633 | bool *own_req) |
| 634 | { |
| 635 | struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 636 | struct mptcp_subflow_request_sock *subflow_req; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 637 | struct mptcp_options_received mp_opt; |
Paolo Abeni | 9e365ff | 2020-06-17 12:08:57 +0200 | [diff] [blame] | 638 | bool fallback, fallback_is_fatal; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 639 | struct sock *new_msk = NULL; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 640 | struct sock *child; |
| 641 | |
| 642 | pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn); |
| 643 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 644 | /* After child creation we must look for MPC even when options |
Paolo Abeni | 9e365ff | 2020-06-17 12:08:57 +0200 | [diff] [blame] | 645 | * are not parsed |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 646 | */ |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 647 | mp_opt.suboptions = 0; |
Paolo Abeni | 9e365ff | 2020-06-17 12:08:57 +0200 | [diff] [blame] | 648 | |
| 649 | /* hopefully temporary handling for MP_JOIN+syncookie */ |
| 650 | subflow_req = mptcp_subflow_rsk(req); |
Paolo Abeni | b751469 | 2020-07-23 13:02:34 +0200 | [diff] [blame] | 651 | fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join; |
Paolo Abeni | 9e365ff | 2020-06-17 12:08:57 +0200 | [diff] [blame] | 652 | fallback = !tcp_rsk(req)->is_mptcp; |
| 653 | if (fallback) |
Florian Westphal | ae2dd71 | 2020-01-29 15:54:46 +0100 | [diff] [blame] | 654 | goto create_child; |
| 655 | |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 656 | /* if the sk is MP_CAPABLE, we try to fetch the client key */ |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 657 | if (subflow_req->mp_capable) { |
Paolo Abeni | 06f9a43 | 2021-05-27 16:31:38 -0700 | [diff] [blame] | 658 | /* we can receive and accept an in-window, out-of-order pkt, |
| 659 | * which may not carry the MP_CAPABLE opt even on mptcp enabled |
| 660 | * paths: always try to extract the peer key, and fallback |
| 661 | * for packets missing it. |
| 662 | * Even OoO DSS packets coming legitly after dropped or |
| 663 | * reordered MPC will cause fallback, but we don't have other |
| 664 | * options. |
| 665 | */ |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 666 | mptcp_get_options(sk, skb, &mp_opt); |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 667 | if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) { |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 668 | fallback = true; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 669 | goto create_child; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 670 | } |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 671 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 672 | new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 673 | if (!new_msk) |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 674 | fallback = true; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 675 | } else if (subflow_req->mp_join) { |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 676 | mptcp_get_options(sk, skb, &mp_opt); |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 677 | if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ) || |
| 678 | !subflow_hmac_valid(req, &mp_opt) || |
Paolo Abeni | d3ab788 | 2020-11-26 15:17:53 +0100 | [diff] [blame] | 679 | !mptcp_can_accept_new_subflow(subflow_req->msk)) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 680 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC); |
Paolo Abeni | 9e365ff | 2020-06-17 12:08:57 +0200 | [diff] [blame] | 681 | fallback = true; |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 682 | } |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 683 | } |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 684 | |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 685 | create_child: |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 686 | child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst, |
| 687 | req_unhash, own_req); |
| 688 | |
| 689 | if (child && *own_req) { |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 690 | struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child); |
| 691 | |
Paolo Abeni | 90bf451 | 2020-05-15 19:22:15 +0200 | [diff] [blame] | 692 | tcp_rsk(req)->drop_req = false; |
| 693 | |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 694 | /* we need to fallback on ctx allocation failure and on pre-reqs |
| 695 | * checking above. In the latter scenario we additionally need |
| 696 | * to reset the context to non MPTCP status. |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 697 | */ |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 698 | if (!ctx || fallback) { |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 699 | if (fallback_is_fatal) { |
| 700 | subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 701 | goto dispose_child; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 702 | } |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 703 | |
Paolo Abeni | 3988460 | 2020-05-29 17:49:18 +0200 | [diff] [blame] | 704 | subflow_drop_ctx(child); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 705 | goto out; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 706 | } |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 707 | |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 708 | /* ssk inherits options of listener sk */ |
| 709 | ctx->setsockopt_seq = listener->setsockopt_seq; |
| 710 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 711 | if (ctx->mp_capable) { |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 712 | /* this can't race with mptcp_close(), as the msk is |
| 713 | * not yet exposted to user-space |
| 714 | */ |
| 715 | inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED); |
| 716 | |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 717 | /* record the newly created socket as the first msk |
| 718 | * subflow, but don't link it yet into conn_list |
| 719 | */ |
Paolo Abeni | 0397c6d | 2020-11-19 11:45:58 -0800 | [diff] [blame] | 720 | WRITE_ONCE(mptcp_sk(new_msk)->first, child); |
| 721 | |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 722 | /* new mpc subflow takes ownership of the newly |
| 723 | * created mptcp socket |
| 724 | */ |
Florian Westphal | df1036d | 2020-04-17 09:28:22 +0200 | [diff] [blame] | 725 | new_msk->sk_destruct = mptcp_sock_destruct; |
Florian Westphal | df00b08 | 2021-04-15 16:44:54 -0700 | [diff] [blame] | 726 | mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq; |
Florian Westphal | 6c714f1 | 2021-02-12 15:59:58 -0800 | [diff] [blame] | 727 | mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1); |
Paolo Abeni | 2c5ebd0 | 2020-06-26 19:30:00 +0200 | [diff] [blame] | 728 | mptcp_token_accept(subflow_req, mptcp_sk(new_msk)); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 729 | ctx->conn = new_msk; |
| 730 | new_msk = NULL; |
Paolo Abeni | fca5c82 | 2020-04-20 16:25:06 +0200 | [diff] [blame] | 731 | |
| 732 | /* with OoO packets we can reach here without ingress |
| 733 | * mpc option |
| 734 | */ |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 735 | if (mp_opt.suboptions & OPTIONS_MPTCP_MPC) |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 736 | mptcp_subflow_fully_established(ctx, &mp_opt); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 737 | } else if (ctx->mp_join) { |
| 738 | struct mptcp_sock *owner; |
| 739 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 740 | owner = subflow_req->msk; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 741 | if (!owner) { |
| 742 | subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 743 | goto dispose_child; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 744 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 745 | |
Paolo Abeni | 8fd4de1 | 2020-06-17 12:08:56 +0200 | [diff] [blame] | 746 | /* move the msk reference ownership to the subflow */ |
| 747 | subflow_req->msk = NULL; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 748 | ctx->conn = (struct sock *)owner; |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 749 | |
| 750 | if (subflow_use_different_sport(owner, sk)) { |
| 751 | pr_debug("ack inet_sport=%d %d", |
| 752 | ntohs(inet_sk(sk)->inet_sport), |
| 753 | ntohs(inet_sk((struct sock *)owner)->inet_sport)); |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 754 | if (!mptcp_pm_sport_in_anno_list(owner, sk)) { |
| 755 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX); |
Geliang Tang | 9238e90 | 2021-03-04 13:32:16 -0800 | [diff] [blame] | 756 | goto dispose_child; |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 757 | } |
| 758 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX); |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 759 | } |
Geliang Tang | 9238e90 | 2021-03-04 13:32:16 -0800 | [diff] [blame] | 760 | |
| 761 | if (!mptcp_finish_join(child)) |
| 762 | goto dispose_child; |
| 763 | |
| 764 | SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX); |
| 765 | tcp_rsk(req)->drop_req = true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 769 | out: |
| 770 | /* dispose of the left over mptcp master, if any */ |
| 771 | if (unlikely(new_msk)) |
Florian Westphal | 9f5ca6a | 2020-04-17 09:28:23 +0200 | [diff] [blame] | 772 | mptcp_force_close(new_msk); |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 773 | |
| 774 | /* check for expected invariant - should never trigger, just help |
| 775 | * catching eariler subtle bugs |
| 776 | */ |
Paolo Abeni | ac2b47f | 2020-04-30 15:03:22 +0200 | [diff] [blame] | 777 | WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp && |
Paolo Abeni | 4c8941d | 2020-04-20 16:25:05 +0200 | [diff] [blame] | 778 | (!mptcp_subflow_ctx(child) || |
| 779 | !mptcp_subflow_ctx(child)->conn)); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 780 | return child; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 781 | |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 782 | dispose_child: |
Paolo Abeni | 3988460 | 2020-05-29 17:49:18 +0200 | [diff] [blame] | 783 | subflow_drop_ctx(child); |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 784 | tcp_rsk(req)->drop_req = true; |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 785 | inet_csk_prepare_for_destroy_sock(child); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 786 | tcp_done(child); |
Paolo Abeni | 97e6175 | 2020-07-23 13:02:35 +0200 | [diff] [blame] | 787 | req->rsk_ops->send_reset(sk, skb); |
Paolo Abeni | 729cd64 | 2020-05-15 19:22:17 +0200 | [diff] [blame] | 788 | |
| 789 | /* The last child reference will be released by the caller */ |
| 790 | return child; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | static struct inet_connection_sock_af_ops subflow_specific; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 794 | static struct proto tcp_prot_override; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 795 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 796 | enum mapping_status { |
| 797 | MAPPING_OK, |
| 798 | MAPPING_INVALID, |
| 799 | MAPPING_EMPTY, |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 800 | MAPPING_DATA_FIN, |
| 801 | MAPPING_DUMMY |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 802 | }; |
| 803 | |
Paolo Abeni | 61e7102 | 2021-06-10 15:59:42 -0700 | [diff] [blame] | 804 | static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn) |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 805 | { |
Paolo Abeni | 61e7102 | 2021-06-10 15:59:42 -0700 | [diff] [blame] | 806 | pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d", |
| 807 | ssn, subflow->map_subflow_seq, subflow->map_data_len); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb) |
| 811 | { |
| 812 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 813 | unsigned int skb_consumed; |
| 814 | |
| 815 | skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq; |
| 816 | if (WARN_ON_ONCE(skb_consumed >= skb->len)) |
| 817 | return true; |
| 818 | |
| 819 | return skb->len - skb_consumed <= subflow->map_data_len - |
| 820 | mptcp_subflow_get_map_offset(subflow); |
| 821 | } |
| 822 | |
| 823 | static bool validate_mapping(struct sock *ssk, struct sk_buff *skb) |
| 824 | { |
| 825 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 826 | u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; |
| 827 | |
| 828 | if (unlikely(before(ssn, subflow->map_subflow_seq))) { |
| 829 | /* Mapping covers data later in the subflow stream, |
| 830 | * currently unsupported. |
| 831 | */ |
Paolo Abeni | 61e7102 | 2021-06-10 15:59:42 -0700 | [diff] [blame] | 832 | dbg_bad_map(subflow, ssn); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | if (unlikely(!before(ssn, subflow->map_subflow_seq + |
| 836 | subflow->map_data_len))) { |
| 837 | /* Mapping does covers past subflow data, invalid */ |
Paolo Abeni | 61e7102 | 2021-06-10 15:59:42 -0700 | [diff] [blame] | 838 | dbg_bad_map(subflow, ssn); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | return true; |
| 842 | } |
| 843 | |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 844 | static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb, |
| 845 | bool csum_reqd) |
| 846 | { |
| 847 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 848 | u32 offset, seq, delta; |
Geliang Tang | 8401e87f | 2022-01-07 11:25:24 -0800 | [diff] [blame] | 849 | u16 csum; |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 850 | int len; |
| 851 | |
| 852 | if (!csum_reqd) |
| 853 | return MAPPING_OK; |
| 854 | |
| 855 | /* mapping already validated on previous traversal */ |
| 856 | if (subflow->map_csum_len == subflow->map_data_len) |
| 857 | return MAPPING_OK; |
| 858 | |
| 859 | /* traverse the receive queue, ensuring it contains a full |
| 860 | * DSS mapping and accumulating the related csum. |
| 861 | * Preserve the accoumlate csum across multiple calls, to compute |
| 862 | * the csum only once |
| 863 | */ |
| 864 | delta = subflow->map_data_len - subflow->map_csum_len; |
| 865 | for (;;) { |
| 866 | seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len; |
| 867 | offset = seq - TCP_SKB_CB(skb)->seq; |
| 868 | |
| 869 | /* if the current skb has not been accounted yet, csum its contents |
| 870 | * up to the amount covered by the current DSS |
| 871 | */ |
| 872 | if (offset < skb->len) { |
| 873 | __wsum csum; |
| 874 | |
| 875 | len = min(skb->len - offset, delta); |
| 876 | csum = skb_checksum(skb, offset, len, 0); |
| 877 | subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum, |
| 878 | subflow->map_csum_len); |
| 879 | |
| 880 | delta -= len; |
| 881 | subflow->map_csum_len += len; |
| 882 | } |
| 883 | if (delta == 0) |
| 884 | break; |
| 885 | |
| 886 | if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) { |
| 887 | /* if this subflow is closed, the partial mapping |
| 888 | * will be never completed; flush the pending skbs, so |
| 889 | * that subflow_sched_work_if_closed() can kick in |
| 890 | */ |
| 891 | if (unlikely(ssk->sk_state == TCP_CLOSE)) |
| 892 | while ((skb = skb_peek(&ssk->sk_receive_queue))) |
| 893 | sk_eat_skb(ssk, skb); |
| 894 | |
| 895 | /* not enough data to validate the csum */ |
| 896 | return MAPPING_EMPTY; |
| 897 | } |
| 898 | |
| 899 | /* the DSS mapping for next skbs will be validated later, |
| 900 | * when a get_mapping_status call will process such skb |
| 901 | */ |
| 902 | skb = skb->next; |
| 903 | } |
| 904 | |
| 905 | /* note that 'map_data_len' accounts only for the carried data, does |
| 906 | * not include the eventual seq increment due to the data fin, |
| 907 | * while the pseudo header requires the original DSS data len, |
| 908 | * including that |
| 909 | */ |
Geliang Tang | 8401e87f | 2022-01-07 11:25:24 -0800 | [diff] [blame] | 910 | csum = __mptcp_make_csum(subflow->map_seq, |
| 911 | subflow->map_subflow_seq, |
| 912 | subflow->map_data_len + subflow->map_data_fin, |
| 913 | subflow->map_data_csum); |
| 914 | if (unlikely(csum)) { |
Geliang Tang | fe3ab1c | 2021-06-17 16:46:18 -0700 | [diff] [blame] | 915 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR); |
Geliang Tang | 478d770 | 2021-08-24 16:26:17 -0700 | [diff] [blame] | 916 | subflow->send_mp_fail = 1; |
Geliang Tang | eb7f336 | 2021-08-24 16:26:18 -0700 | [diff] [blame] | 917 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPFAILTX); |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 918 | return subflow->mp_join ? MAPPING_INVALID : MAPPING_DUMMY; |
Geliang Tang | fe3ab1c | 2021-06-17 16:46:18 -0700 | [diff] [blame] | 919 | } |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 920 | |
| 921 | return MAPPING_OK; |
| 922 | } |
| 923 | |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 924 | static enum mapping_status get_mapping_status(struct sock *ssk, |
| 925 | struct mptcp_sock *msk) |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 926 | { |
| 927 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 928 | bool csum_reqd = READ_ONCE(msk->csum_enabled); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 929 | struct mptcp_ext *mpext; |
| 930 | struct sk_buff *skb; |
| 931 | u16 data_len; |
| 932 | u64 map_seq; |
| 933 | |
| 934 | skb = skb_peek(&ssk->sk_receive_queue); |
| 935 | if (!skb) |
| 936 | return MAPPING_EMPTY; |
| 937 | |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 938 | if (mptcp_check_fallback(ssk)) |
| 939 | return MAPPING_DUMMY; |
| 940 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 941 | mpext = mptcp_get_ext(skb); |
| 942 | if (!mpext || !mpext->use_map) { |
| 943 | if (!subflow->map_valid && !skb->len) { |
| 944 | /* the TCP stack deliver 0 len FIN pkt to the receive |
| 945 | * queue, that is the only 0len pkts ever expected here, |
| 946 | * and we can admit no mapping only for 0 len pkts |
| 947 | */ |
| 948 | if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) |
| 949 | WARN_ONCE(1, "0len seq %d:%d flags %x", |
| 950 | TCP_SKB_CB(skb)->seq, |
| 951 | TCP_SKB_CB(skb)->end_seq, |
| 952 | TCP_SKB_CB(skb)->tcp_flags); |
| 953 | sk_eat_skb(ssk, skb); |
| 954 | return MAPPING_EMPTY; |
| 955 | } |
| 956 | |
| 957 | if (!subflow->map_valid) |
| 958 | return MAPPING_INVALID; |
| 959 | |
| 960 | goto validate_seq; |
| 961 | } |
| 962 | |
Geliang Tang | 0918e34 | 2021-04-16 15:38:05 -0700 | [diff] [blame] | 963 | trace_get_mapping_status(mpext); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 964 | |
| 965 | data_len = mpext->data_len; |
| 966 | if (data_len == 0) { |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 967 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 968 | return MAPPING_INVALID; |
| 969 | } |
| 970 | |
| 971 | if (mpext->data_fin == 1) { |
| 972 | if (data_len == 1) { |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 973 | bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq, |
| 974 | mpext->dsn64); |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 975 | pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 976 | if (subflow->map_valid) { |
| 977 | /* A DATA_FIN might arrive in a DSS |
| 978 | * option before the previous mapping |
| 979 | * has been fully consumed. Continue |
| 980 | * handling the existing mapping. |
| 981 | */ |
| 982 | skb_ext_del(skb, SKB_EXT_MPTCP); |
| 983 | return MAPPING_OK; |
| 984 | } else { |
Mat Martineau | ef59b19 | 2020-09-21 16:57:58 +0200 | [diff] [blame] | 985 | if (updated && schedule_work(&msk->work)) |
| 986 | sock_hold((struct sock *)msk); |
| 987 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 988 | return MAPPING_DATA_FIN; |
| 989 | } |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 990 | } else { |
Paolo Abeni | 017512a | 2020-10-05 12:01:06 +0200 | [diff] [blame] | 991 | u64 data_fin_seq = mpext->data_seq + data_len - 1; |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 992 | |
| 993 | /* If mpext->data_seq is a 32-bit value, data_fin_seq |
| 994 | * must also be limited to 32 bits. |
| 995 | */ |
| 996 | if (!mpext->dsn64) |
| 997 | data_fin_seq &= GENMASK_ULL(31, 0); |
| 998 | |
| 999 | mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64); |
| 1000 | pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d", |
| 1001 | data_fin_seq, mpext->dsn64); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* Adjust for DATA_FIN using 1 byte of sequence space */ |
| 1005 | data_len--; |
| 1006 | } |
| 1007 | |
Paolo Abeni | 5957a89 | 2021-06-18 15:02:21 -0700 | [diff] [blame] | 1008 | map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64); |
Davide Caratti | 37198e9 | 2020-10-06 18:26:17 +0200 | [diff] [blame] | 1009 | WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1010 | |
| 1011 | if (subflow->map_valid) { |
| 1012 | /* Allow replacing only with an identical map */ |
| 1013 | if (subflow->map_seq == map_seq && |
| 1014 | subflow->map_subflow_seq == mpext->subflow_seq && |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1015 | subflow->map_data_len == data_len && |
| 1016 | subflow->map_csum_reqd == mpext->csum_reqd) { |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1017 | skb_ext_del(skb, SKB_EXT_MPTCP); |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1018 | goto validate_csum; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* If this skb data are fully covered by the current mapping, |
| 1022 | * the new map would need caching, which is not supported |
| 1023 | */ |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1024 | if (skb_is_fully_mapped(ssk, skb)) { |
| 1025 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1026 | return MAPPING_INVALID; |
Florian Westphal | fc51895 | 2020-03-27 14:48:50 -0700 | [diff] [blame] | 1027 | } |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1028 | |
| 1029 | /* will validate the next map after consuming the current one */ |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1030 | goto validate_csum; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | subflow->map_seq = map_seq; |
| 1034 | subflow->map_subflow_seq = mpext->subflow_seq; |
| 1035 | subflow->map_data_len = data_len; |
| 1036 | subflow->map_valid = 1; |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1037 | subflow->map_data_fin = mpext->data_fin; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1038 | subflow->mpc_map = mpext->mpc_map; |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1039 | subflow->map_csum_reqd = mpext->csum_reqd; |
| 1040 | subflow->map_csum_len = 0; |
| 1041 | subflow->map_data_csum = csum_unfold(mpext->csum); |
| 1042 | |
| 1043 | /* Cfr RFC 8684 Section 3.3.0 */ |
| 1044 | if (unlikely(subflow->map_csum_reqd != csum_reqd)) |
| 1045 | return MAPPING_INVALID; |
| 1046 | |
| 1047 | pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u", |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1048 | subflow->map_seq, subflow->map_subflow_seq, |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1049 | subflow->map_data_len, subflow->map_csum_reqd, |
| 1050 | subflow->map_data_csum); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1051 | |
| 1052 | validate_seq: |
| 1053 | /* we revalidate valid mapping on new skb, because we must ensure |
| 1054 | * the current skb is completely covered by the available mapping |
| 1055 | */ |
Paolo Abeni | 06285da | 2021-06-21 15:54:37 -0700 | [diff] [blame] | 1056 | if (!validate_mapping(ssk, skb)) { |
| 1057 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1058 | return MAPPING_INVALID; |
Paolo Abeni | 06285da | 2021-06-21 15:54:37 -0700 | [diff] [blame] | 1059 | } |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1060 | |
| 1061 | skb_ext_del(skb, SKB_EXT_MPTCP); |
Paolo Abeni | dd8bcd1 | 2021-06-17 16:46:16 -0700 | [diff] [blame] | 1062 | |
| 1063 | validate_csum: |
| 1064 | return validate_data_csum(ssk, skb, csum_reqd); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Paolo Abeni | 04e4cd4 | 2020-09-14 10:01:13 +0200 | [diff] [blame] | 1067 | static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb, |
Paolo Abeni | 1d39cd8 | 2020-09-17 23:07:24 +0200 | [diff] [blame] | 1068 | u64 limit) |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 1069 | { |
| 1070 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
Paolo Abeni | 04e4cd4 | 2020-09-14 10:01:13 +0200 | [diff] [blame] | 1071 | bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; |
| 1072 | u32 incr; |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 1073 | |
Paolo Abeni | 04e4cd4 | 2020-09-14 10:01:13 +0200 | [diff] [blame] | 1074 | incr = limit >= skb->len ? skb->len + fin : limit; |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 1075 | |
Paolo Abeni | 04e4cd4 | 2020-09-14 10:01:13 +0200 | [diff] [blame] | 1076 | pr_debug("discarding=%d len=%d seq=%d", incr, skb->len, |
| 1077 | subflow->map_subflow_seq); |
Paolo Abeni | 06242e4 | 2020-09-14 10:01:14 +0200 | [diff] [blame] | 1078 | MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA); |
Paolo Abeni | 04e4cd4 | 2020-09-14 10:01:13 +0200 | [diff] [blame] | 1079 | tcp_sk(ssk)->copied_seq += incr; |
| 1080 | if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq)) |
| 1081 | sk_eat_skb(ssk, skb); |
| 1082 | if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) |
| 1083 | subflow->map_valid = 0; |
Paolo Abeni | 6719331 | 2020-09-14 10:01:09 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 1086 | /* sched mptcp worker to remove the subflow if no more data is pending */ |
| 1087 | static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk) |
| 1088 | { |
| 1089 | struct sock *sk = (struct sock *)msk; |
| 1090 | |
| 1091 | if (likely(ssk->sk_state != TCP_CLOSE)) |
| 1092 | return; |
| 1093 | |
| 1094 | if (skb_queue_empty(&ssk->sk_receive_queue) && |
| 1095 | !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) { |
| 1096 | sock_hold(sk); |
| 1097 | if (!schedule_work(&msk->work)) |
| 1098 | sock_put(sk); |
| 1099 | } |
| 1100 | } |
| 1101 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1102 | static bool subflow_check_data_avail(struct sock *ssk) |
| 1103 | { |
| 1104 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 1105 | enum mapping_status status; |
| 1106 | struct mptcp_sock *msk; |
| 1107 | struct sk_buff *skb; |
| 1108 | |
Paolo Abeni | 47bebdf | 2020-09-14 10:01:08 +0200 | [diff] [blame] | 1109 | if (!skb_peek(&ssk->sk_receive_queue)) |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1110 | WRITE_ONCE(subflow->data_avail, 0); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1111 | if (subflow->data_avail) |
| 1112 | return true; |
| 1113 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1114 | msk = mptcp_sk(subflow->conn); |
| 1115 | for (;;) { |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1116 | u64 ack_seq; |
| 1117 | u64 old_ack; |
| 1118 | |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1119 | status = get_mapping_status(ssk, msk); |
Geliang Tang | d96a838 | 2021-04-16 15:38:07 -0700 | [diff] [blame] | 1120 | trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue)); |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1121 | if (unlikely(status == MAPPING_INVALID)) |
| 1122 | goto fallback; |
| 1123 | |
| 1124 | if (unlikely(status == MAPPING_DUMMY)) |
| 1125 | goto fallback; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1126 | |
| 1127 | if (status != MAPPING_OK) |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 1128 | goto no_data; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1129 | |
| 1130 | skb = skb_peek(&ssk->sk_receive_queue); |
| 1131 | if (WARN_ON_ONCE(!skb)) |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 1132 | goto no_data; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1133 | |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1134 | /* if msk lacks the remote key, this subflow must provide an |
| 1135 | * MP_CAPABLE-based mapping |
| 1136 | */ |
| 1137 | if (unlikely(!READ_ONCE(msk->can_ack))) { |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1138 | if (!subflow->mpc_map) |
| 1139 | goto fallback; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1140 | WRITE_ONCE(msk->remote_key, subflow->remote_key); |
| 1141 | WRITE_ONCE(msk->ack_seq, subflow->map_seq); |
| 1142 | WRITE_ONCE(msk->can_ack, true); |
| 1143 | } |
| 1144 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1145 | old_ack = READ_ONCE(msk->ack_seq); |
| 1146 | ack_seq = mptcp_subflow_get_mapped_dsn(subflow); |
| 1147 | pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack, |
| 1148 | ack_seq); |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1149 | if (unlikely(before64(ack_seq, old_ack))) { |
| 1150 | mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq); |
| 1151 | continue; |
Paolo Abeni | 47bebdf | 2020-09-14 10:01:08 +0200 | [diff] [blame] | 1152 | } |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1153 | |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1154 | WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); |
| 1155 | break; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1156 | } |
| 1157 | return true; |
| 1158 | |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 1159 | no_data: |
| 1160 | subflow_sched_work_if_closed(msk, ssk); |
| 1161 | return false; |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1162 | |
| 1163 | fallback: |
| 1164 | /* RFC 8684 section 3.7. */ |
Geliang Tang | 478d770 | 2021-08-24 16:26:17 -0700 | [diff] [blame] | 1165 | if (subflow->send_mp_fail) { |
| 1166 | if (mptcp_has_another_subflow(ssk)) { |
| 1167 | while ((skb = skb_peek(&ssk->sk_receive_queue))) |
| 1168 | sk_eat_skb(ssk, skb); |
| 1169 | } |
| 1170 | ssk->sk_err = EBADMSG; |
| 1171 | tcp_set_state(ssk, TCP_CLOSE); |
| 1172 | subflow->reset_transient = 0; |
| 1173 | subflow->reset_reason = MPTCP_RST_EMIDDLEBOX; |
| 1174 | tcp_send_active_reset(ssk, GFP_ATOMIC); |
| 1175 | WRITE_ONCE(subflow->data_avail, 0); |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1179 | if (subflow->mp_join || subflow->fully_established) { |
| 1180 | /* fatal protocol error, close the socket. |
| 1181 | * subflow_error_report() will introduce the appropriate barriers |
| 1182 | */ |
| 1183 | ssk->sk_err = EBADMSG; |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1184 | tcp_set_state(ssk, TCP_CLOSE); |
| 1185 | subflow->reset_transient = 0; |
| 1186 | subflow->reset_reason = MPTCP_RST_EMPTCP; |
| 1187 | tcp_send_active_reset(ssk, GFP_ATOMIC); |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1188 | WRITE_ONCE(subflow->data_avail, 0); |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | __mptcp_do_fallback(msk); |
| 1193 | skb = skb_peek(&ssk->sk_receive_queue); |
| 1194 | subflow->map_valid = 1; |
| 1195 | subflow->map_seq = READ_ONCE(msk->ack_seq); |
| 1196 | subflow->map_data_len = skb->len; |
| 1197 | subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1198 | WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); |
Paolo Abeni | dea2b1e | 2021-05-27 16:31:39 -0700 | [diff] [blame] | 1199 | return true; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | bool mptcp_subflow_data_available(struct sock *sk) |
| 1203 | { |
| 1204 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1205 | |
| 1206 | /* check if current mapping is still valid */ |
| 1207 | if (subflow->map_valid && |
| 1208 | mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) { |
| 1209 | subflow->map_valid = 0; |
Paolo Abeni | 99d1055 | 2021-06-10 15:59:41 -0700 | [diff] [blame] | 1210 | WRITE_ONCE(subflow->data_avail, 0); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1211 | |
| 1212 | pr_debug("Done with mapping: seq=%u data_len=%u", |
| 1213 | subflow->map_subflow_seq, |
| 1214 | subflow->map_data_len); |
| 1215 | } |
| 1216 | |
Paolo Abeni | 47bebdf | 2020-09-14 10:01:08 +0200 | [diff] [blame] | 1217 | return subflow_check_data_avail(sk); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Florian Westphal | 071c8ed | 2020-04-24 12:31:50 +0200 | [diff] [blame] | 1220 | /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy, |
| 1221 | * not the ssk one. |
| 1222 | * |
| 1223 | * In mptcp, rwin is about the mptcp-level connection data. |
| 1224 | * |
| 1225 | * Data that is still on the ssk rx queue can thus be ignored, |
Bhaskar Chowdhury | 55320b8 | 2021-03-27 04:42:46 +0530 | [diff] [blame] | 1226 | * as far as mptcp peer is concerned that data is still inflight. |
Florian Westphal | 071c8ed | 2020-04-24 12:31:50 +0200 | [diff] [blame] | 1227 | * DSS ACK is updated when skb is moved to the mptcp rx queue. |
| 1228 | */ |
| 1229 | void mptcp_space(const struct sock *ssk, int *space, int *full_space) |
| 1230 | { |
| 1231 | const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 1232 | const struct sock *sk = subflow->conn; |
| 1233 | |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 1234 | *space = __mptcp_space(sk); |
Florian Westphal | 071c8ed | 2020-04-24 12:31:50 +0200 | [diff] [blame] | 1235 | *full_space = tcp_full_space(sk); |
| 1236 | } |
| 1237 | |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1238 | void __mptcp_error_report(struct sock *sk) |
| 1239 | { |
| 1240 | struct mptcp_subflow_context *subflow; |
| 1241 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1242 | |
| 1243 | mptcp_for_each_subflow(msk, subflow) { |
| 1244 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 1245 | int err = sock_error(ssk); |
| 1246 | |
| 1247 | if (!err) |
| 1248 | continue; |
| 1249 | |
| 1250 | /* only propagate errors on fallen-back sockets or |
| 1251 | * on MPC connect |
| 1252 | */ |
| 1253 | if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk)) |
| 1254 | continue; |
| 1255 | |
| 1256 | inet_sk_state_store(sk, inet_sk_state_load(ssk)); |
| 1257 | sk->sk_err = -err; |
| 1258 | |
| 1259 | /* This barrier is coupled with smp_rmb() in mptcp_poll() */ |
| 1260 | smp_wmb(); |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 1261 | sk_error_report(sk); |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | static void subflow_error_report(struct sock *ssk) |
| 1267 | { |
| 1268 | struct sock *sk = mptcp_subflow_ctx(ssk)->conn; |
| 1269 | |
| 1270 | mptcp_data_lock(sk); |
| 1271 | if (!sock_owned_by_user(sk)) |
| 1272 | __mptcp_error_report(sk); |
| 1273 | else |
Paolo Abeni | e9d09ba | 2022-01-06 16:20:26 -0800 | [diff] [blame] | 1274 | __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags); |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1275 | mptcp_data_unlock(sk); |
| 1276 | } |
| 1277 | |
Paolo Abeni | 499ada5 | 2021-06-10 15:59:44 -0700 | [diff] [blame] | 1278 | static void subflow_data_ready(struct sock *sk) |
| 1279 | { |
| 1280 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 1281 | u16 state = 1 << inet_sk_state_load(sk); |
| 1282 | struct sock *parent = subflow->conn; |
| 1283 | struct mptcp_sock *msk; |
| 1284 | |
| 1285 | msk = mptcp_sk(parent); |
| 1286 | if (state & TCPF_LISTEN) { |
| 1287 | /* MPJ subflow are removed from accept queue before reaching here, |
| 1288 | * avoid stray wakeups |
| 1289 | */ |
| 1290 | if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue)) |
| 1291 | return; |
| 1292 | |
Paolo Abeni | 499ada5 | 2021-06-10 15:59:44 -0700 | [diff] [blame] | 1293 | parent->sk_data_ready(parent); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable && |
| 1298 | !subflow->mp_join && !(state & TCPF_CLOSE)); |
| 1299 | |
| 1300 | if (mptcp_subflow_data_available(sk)) |
| 1301 | mptcp_data_ready(parent, sk); |
| 1302 | else if (unlikely(sk->sk_err)) |
| 1303 | subflow_error_report(sk); |
| 1304 | } |
| 1305 | |
| 1306 | static void subflow_write_space(struct sock *ssk) |
| 1307 | { |
| 1308 | struct sock *sk = mptcp_subflow_ctx(ssk)->conn; |
| 1309 | |
| 1310 | mptcp_propagate_sndbuf(sk, ssk); |
| 1311 | mptcp_write_space(sk); |
| 1312 | } |
| 1313 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1314 | static struct inet_connection_sock_af_ops * |
| 1315 | subflow_default_af_ops(struct sock *sk) |
| 1316 | { |
| 1317 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1318 | if (sk->sk_family == AF_INET6) |
| 1319 | return &subflow_v6_specific; |
| 1320 | #endif |
| 1321 | return &subflow_specific; |
| 1322 | } |
| 1323 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1324 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Geert Uytterhoeven | 31484d5 | 2020-01-30 10:45:26 +0100 | [diff] [blame] | 1325 | void mptcpv6_handle_mapped(struct sock *sk, bool mapped) |
| 1326 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1327 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 1328 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 1329 | struct inet_connection_sock_af_ops *target; |
| 1330 | |
| 1331 | target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk); |
| 1332 | |
| 1333 | pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d", |
Mat Martineau | edc7e48 | 2020-01-24 16:04:03 -0800 | [diff] [blame] | 1334 | subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1335 | |
| 1336 | if (likely(icsk->icsk_af_ops == target)) |
| 1337 | return; |
| 1338 | |
| 1339 | subflow->icsk_af_ops = icsk->icsk_af_ops; |
| 1340 | icsk->icsk_af_ops = target; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1341 | } |
Geert Uytterhoeven | 31484d5 | 2020-01-30 10:45:26 +0100 | [diff] [blame] | 1342 | #endif |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1343 | |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1344 | void mptcp_info2sockaddr(const struct mptcp_addr_info *info, |
| 1345 | struct sockaddr_storage *addr, |
| 1346 | unsigned short family) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1347 | { |
| 1348 | memset(addr, 0, sizeof(*addr)); |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1349 | addr->ss_family = family; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1350 | if (addr->ss_family == AF_INET) { |
| 1351 | struct sockaddr_in *in_addr = (struct sockaddr_in *)addr; |
| 1352 | |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1353 | if (info->family == AF_INET) |
| 1354 | in_addr->sin_addr = info->addr; |
| 1355 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1356 | else if (ipv6_addr_v4mapped(&info->addr6)) |
| 1357 | in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3]; |
| 1358 | #endif |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1359 | in_addr->sin_port = info->port; |
| 1360 | } |
| 1361 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1362 | else if (addr->ss_family == AF_INET6) { |
| 1363 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr; |
| 1364 | |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1365 | if (info->family == AF_INET) |
| 1366 | ipv6_addr_set_v4mapped(info->addr.s_addr, |
| 1367 | &in6_addr->sin6_addr); |
| 1368 | else |
| 1369 | in6_addr->sin6_addr = info->addr6; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1370 | in6_addr->sin6_port = info->port; |
| 1371 | } |
| 1372 | #endif |
| 1373 | } |
| 1374 | |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 1375 | int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc, |
Geliang Tang | ee28525 | 2021-08-17 15:07:22 -0700 | [diff] [blame] | 1376 | const struct mptcp_addr_info *remote) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1377 | { |
| 1378 | struct mptcp_sock *msk = mptcp_sk(sk); |
| 1379 | struct mptcp_subflow_context *subflow; |
| 1380 | struct sockaddr_storage addr; |
Geliang Tang | 2ff0e56 | 2020-09-08 10:49:39 +0800 | [diff] [blame] | 1381 | int remote_id = remote->id; |
Paolo Abeni | 6bad912 | 2020-06-30 16:38:26 +0200 | [diff] [blame] | 1382 | int local_id = loc->id; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1383 | struct socket *sf; |
Paolo Abeni | 6bad912 | 2020-06-30 16:38:26 +0200 | [diff] [blame] | 1384 | struct sock *ssk; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1385 | u32 remote_token; |
| 1386 | int addrlen; |
Geliang Tang | ee28525 | 2021-08-17 15:07:22 -0700 | [diff] [blame] | 1387 | int ifindex; |
| 1388 | u8 flags; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1389 | int err; |
| 1390 | |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 1391 | if (!mptcp_is_fully_established(sk)) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1392 | return -ENOTCONN; |
| 1393 | |
| 1394 | err = mptcp_subflow_create_socket(sk, &sf); |
| 1395 | if (err) |
| 1396 | return err; |
| 1397 | |
Paolo Abeni | 6bad912 | 2020-06-30 16:38:26 +0200 | [diff] [blame] | 1398 | ssk = sf->sk; |
| 1399 | subflow = mptcp_subflow_ctx(ssk); |
| 1400 | do { |
| 1401 | get_random_bytes(&subflow->local_nonce, sizeof(u32)); |
| 1402 | } while (!subflow->local_nonce); |
| 1403 | |
| 1404 | if (!local_id) { |
| 1405 | err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk); |
| 1406 | if (err < 0) |
| 1407 | goto failed; |
| 1408 | |
| 1409 | local_id = err; |
| 1410 | } |
| 1411 | |
Geliang Tang | ee28525 | 2021-08-17 15:07:22 -0700 | [diff] [blame] | 1412 | mptcp_pm_get_flags_and_ifindex_by_id(sock_net(sk), local_id, |
| 1413 | &flags, &ifindex); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1414 | subflow->remote_key = msk->remote_key; |
| 1415 | subflow->local_key = msk->local_key; |
| 1416 | subflow->token = msk->token; |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1417 | mptcp_info2sockaddr(loc, &addr, ssk->sk_family); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1418 | |
| 1419 | addrlen = sizeof(struct sockaddr_in); |
| 1420 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1421 | if (addr.ss_family == AF_INET6) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1422 | addrlen = sizeof(struct sockaddr_in6); |
| 1423 | #endif |
Florian Westphal | c9406a2 | 2021-11-19 12:41:36 -0800 | [diff] [blame] | 1424 | mptcp_sockopt_sync(msk, ssk); |
| 1425 | |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1426 | ssk->sk_bound_dev_if = ifindex; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1427 | err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen); |
| 1428 | if (err) |
| 1429 | goto failed; |
| 1430 | |
| 1431 | mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL); |
Geliang Tang | 2ff0e56 | 2020-09-08 10:49:39 +0800 | [diff] [blame] | 1432 | pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk, |
| 1433 | remote_token, local_id, remote_id); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1434 | subflow->remote_token = remote_token; |
Paolo Abeni | 6bad912 | 2020-06-30 16:38:26 +0200 | [diff] [blame] | 1435 | subflow->local_id = local_id; |
Geliang Tang | 2ff0e56 | 2020-09-08 10:49:39 +0800 | [diff] [blame] | 1436 | subflow->remote_id = remote_id; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1437 | subflow->request_join = 1; |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1438 | subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP); |
Matthieu Baerts | 50a13bc | 2021-01-25 10:59:00 -0800 | [diff] [blame] | 1439 | mptcp_info2sockaddr(remote, &addr, ssk->sk_family); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1440 | |
Paolo Abeni | 3e50149 | 2022-01-06 16:20:25 -0800 | [diff] [blame] | 1441 | sock_hold(ssk); |
| 1442 | list_add_tail(&subflow->node, &msk->conn_list); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1443 | err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK); |
| 1444 | if (err && err != -EINPROGRESS) |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 1445 | goto failed_unlink; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1446 | |
Paolo Abeni | 866f26f | 2021-01-20 15:39:10 +0100 | [diff] [blame] | 1447 | /* discard the subflow socket */ |
| 1448 | mptcp_sock_graft(ssk, sk->sk_socket); |
| 1449 | iput(SOCK_INODE(sf)); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1450 | return err; |
| 1451 | |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 1452 | failed_unlink: |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 1453 | list_del(&subflow->node); |
Florian Westphal | f071577 | 2021-03-04 13:32:09 -0800 | [diff] [blame] | 1454 | sock_put(mptcp_subflow_tcp_sock(subflow)); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1455 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1456 | failed: |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1457 | subflow->disposable = 1; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1458 | sock_release(sf); |
| 1459 | return err; |
| 1460 | } |
| 1461 | |
Nicolas Rybowski | 3764b0c | 2020-12-10 14:24:58 -0800 | [diff] [blame] | 1462 | static void mptcp_attach_cgroup(struct sock *parent, struct sock *child) |
| 1463 | { |
| 1464 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 1465 | struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, |
| 1466 | *child_skcd = &child->sk_cgrp_data; |
| 1467 | |
| 1468 | /* only the additional subflows created by kworkers have to be modified */ |
| 1469 | if (cgroup_id(sock_cgroup_ptr(parent_skcd)) != |
| 1470 | cgroup_id(sock_cgroup_ptr(child_skcd))) { |
| 1471 | #ifdef CONFIG_MEMCG |
| 1472 | struct mem_cgroup *memcg = parent->sk_memcg; |
| 1473 | |
| 1474 | mem_cgroup_sk_free(child); |
| 1475 | if (memcg && css_tryget(&memcg->css)) |
| 1476 | child->sk_memcg = memcg; |
| 1477 | #endif /* CONFIG_MEMCG */ |
| 1478 | |
| 1479 | cgroup_sk_free(child_skcd); |
| 1480 | *child_skcd = *parent_skcd; |
| 1481 | cgroup_sk_clone(child_skcd); |
| 1482 | } |
| 1483 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 1484 | } |
| 1485 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1486 | static void mptcp_subflow_ops_override(struct sock *ssk) |
| 1487 | { |
| 1488 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1489 | if (ssk->sk_prot == &tcpv6_prot) |
| 1490 | ssk->sk_prot = &tcpv6_prot_override; |
| 1491 | else |
| 1492 | #endif |
| 1493 | ssk->sk_prot = &tcp_prot_override; |
| 1494 | } |
| 1495 | |
| 1496 | static void mptcp_subflow_ops_undo_override(struct sock *ssk) |
| 1497 | { |
| 1498 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1499 | if (ssk->sk_prot == &tcpv6_prot_override) |
| 1500 | ssk->sk_prot = &tcpv6_prot; |
| 1501 | else |
| 1502 | #endif |
| 1503 | ssk->sk_prot = &tcp_prot; |
| 1504 | } |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1505 | int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock) |
| 1506 | { |
| 1507 | struct mptcp_subflow_context *subflow; |
| 1508 | struct net *net = sock_net(sk); |
| 1509 | struct socket *sf; |
| 1510 | int err; |
| 1511 | |
Paolo Abeni | adf7341 | 2020-08-04 18:31:06 +0200 | [diff] [blame] | 1512 | /* un-accepted server sockets can reach here - on bad configuration |
| 1513 | * bail early to avoid greater trouble later |
| 1514 | */ |
| 1515 | if (unlikely(!sk->sk_socket)) |
| 1516 | return -EINVAL; |
| 1517 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1518 | err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP, |
| 1519 | &sf); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1520 | if (err) |
| 1521 | return err; |
| 1522 | |
| 1523 | lock_sock(sf->sk); |
| 1524 | |
Nicolas Rybowski | 3764b0c | 2020-12-10 14:24:58 -0800 | [diff] [blame] | 1525 | /* the newly created socket has to be in the same cgroup as its parent */ |
| 1526 | mptcp_attach_cgroup(sk, sf->sk); |
| 1527 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1528 | /* kernel sockets do not by default acquire net ref, but TCP timer |
| 1529 | * needs it. |
| 1530 | */ |
| 1531 | sf->sk->sk_net_refcnt = 1; |
Eric Dumazet | 1d2f3d3 | 2021-12-13 20:32:08 -0800 | [diff] [blame] | 1532 | get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL); |
Eric Dumazet | d477eb9 | 2021-11-15 09:11:48 -0800 | [diff] [blame] | 1533 | sock_inuse_add(net, 1); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1534 | err = tcp_set_ulp(sf->sk, "mptcp"); |
| 1535 | release_sock(sf->sk); |
| 1536 | |
Wei Yongjun | b8ad540 | 2020-06-15 09:35:22 +0800 | [diff] [blame] | 1537 | if (err) { |
| 1538 | sock_release(sf); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1539 | return err; |
Wei Yongjun | b8ad540 | 2020-06-15 09:35:22 +0800 | [diff] [blame] | 1540 | } |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1541 | |
Paolo Abeni | 7d14b0d | 2020-05-07 18:53:24 +0200 | [diff] [blame] | 1542 | /* the newly created socket really belongs to the owning MPTCP master |
| 1543 | * socket, even if for additional subflows the allocation is performed |
| 1544 | * by a kernel workqueue. Adjust inode references, so that the |
| 1545 | * procfs/diag interaces really show this one belonging to the correct |
| 1546 | * user. |
| 1547 | */ |
| 1548 | SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino; |
| 1549 | SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid; |
| 1550 | SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid; |
| 1551 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1552 | subflow = mptcp_subflow_ctx(sf->sk); |
| 1553 | pr_debug("subflow=%p", subflow); |
| 1554 | |
| 1555 | *new_sock = sf; |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1556 | sock_hold(sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1557 | subflow->conn = sk; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1558 | mptcp_subflow_ops_override(sf->sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1559 | |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, |
| 1564 | gfp_t priority) |
| 1565 | { |
| 1566 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 1567 | struct mptcp_subflow_context *ctx; |
| 1568 | |
| 1569 | ctx = kzalloc(sizeof(*ctx), priority); |
| 1570 | if (!ctx) |
| 1571 | return NULL; |
| 1572 | |
| 1573 | rcu_assign_pointer(icsk->icsk_ulp_data, ctx); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1574 | INIT_LIST_HEAD(&ctx->node); |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1575 | INIT_LIST_HEAD(&ctx->delegated_node); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1576 | |
| 1577 | pr_debug("subflow=%p", ctx); |
| 1578 | |
| 1579 | ctx->tcp_sock = sk; |
| 1580 | |
| 1581 | return ctx; |
| 1582 | } |
| 1583 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1584 | static void __subflow_state_change(struct sock *sk) |
| 1585 | { |
| 1586 | struct socket_wq *wq; |
| 1587 | |
| 1588 | rcu_read_lock(); |
| 1589 | wq = rcu_dereference(sk->sk_wq); |
| 1590 | if (skwq_has_sleeper(wq)) |
| 1591 | wake_up_interruptible_all(&wq->wait); |
| 1592 | rcu_read_unlock(); |
| 1593 | } |
| 1594 | |
| 1595 | static bool subflow_is_done(const struct sock *sk) |
| 1596 | { |
| 1597 | return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE; |
| 1598 | } |
| 1599 | |
| 1600 | static void subflow_state_change(struct sock *sk) |
| 1601 | { |
| 1602 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Paolo Abeni | dc093db | 2020-03-13 16:52:42 +0100 | [diff] [blame] | 1603 | struct sock *parent = subflow->conn; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1604 | |
| 1605 | __subflow_state_change(sk); |
| 1606 | |
Davide Caratti | 8fd7380 | 2020-06-29 22:26:21 +0200 | [diff] [blame] | 1607 | if (subflow_simultaneous_connect(sk)) { |
Paolo Abeni | 5cf92bb | 2021-01-20 15:39:11 +0100 | [diff] [blame] | 1608 | mptcp_propagate_sndbuf(parent, sk); |
Davide Caratti | 8fd7380 | 2020-06-29 22:26:21 +0200 | [diff] [blame] | 1609 | mptcp_do_fallback(sk); |
Florian Westphal | a6b118f | 2020-06-30 21:24:45 +0200 | [diff] [blame] | 1610 | mptcp_rcv_space_init(mptcp_sk(parent), sk); |
Davide Caratti | 8fd7380 | 2020-06-29 22:26:21 +0200 | [diff] [blame] | 1611 | pr_fallback(mptcp_sk(parent)); |
| 1612 | subflow->conn_finished = 1; |
Paolo Abeni | 490274b | 2021-06-21 17:33:08 -0700 | [diff] [blame] | 1613 | mptcp_set_connected(parent); |
Davide Caratti | 8fd7380 | 2020-06-29 22:26:21 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1616 | /* as recvmsg() does not acquire the subflow socket for ssk selection |
| 1617 | * a fin packet carrying a DSS can be unnoticed if we don't trigger |
| 1618 | * the data available machinery here. |
| 1619 | */ |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 1620 | if (mptcp_subflow_data_available(sk)) |
Florian Westphal | 2e52213 | 2020-02-26 10:14:51 +0100 | [diff] [blame] | 1621 | mptcp_data_ready(parent, sk); |
Paolo Abeni | 499ada5 | 2021-06-10 15:59:44 -0700 | [diff] [blame] | 1622 | else if (unlikely(sk->sk_err)) |
| 1623 | subflow_error_report(sk); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1624 | |
Florian Westphal | 40947e1 | 2021-02-12 15:59:56 -0800 | [diff] [blame] | 1625 | subflow_sched_work_if_closed(mptcp_sk(parent), sk); |
| 1626 | |
Mat Martineau | 067a0b3 | 2020-07-28 15:12:07 -0700 | [diff] [blame] | 1627 | if (__mptcp_check_fallback(mptcp_sk(parent)) && |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1628 | !subflow->rx_eof && subflow_is_done(sk)) { |
| 1629 | subflow->rx_eof = 1; |
Florian Westphal | 59832e2 | 2020-04-02 13:44:52 +0200 | [diff] [blame] | 1630 | mptcp_subflow_eof(parent); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1634 | static int subflow_ulp_init(struct sock *sk) |
| 1635 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1636 | struct inet_connection_sock *icsk = inet_csk(sk); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1637 | struct mptcp_subflow_context *ctx; |
| 1638 | struct tcp_sock *tp = tcp_sk(sk); |
| 1639 | int err = 0; |
| 1640 | |
| 1641 | /* disallow attaching ULP to a socket unless it has been |
| 1642 | * created with sock_create_kern() |
| 1643 | */ |
| 1644 | if (!sk->sk_kern_sock) { |
| 1645 | err = -EOPNOTSUPP; |
| 1646 | goto out; |
| 1647 | } |
| 1648 | |
| 1649 | ctx = subflow_create_ctx(sk, GFP_KERNEL); |
| 1650 | if (!ctx) { |
| 1651 | err = -ENOMEM; |
| 1652 | goto out; |
| 1653 | } |
| 1654 | |
| 1655 | pr_debug("subflow=%p, family=%d", ctx, sk->sk_family); |
| 1656 | |
| 1657 | tp->is_mptcp = 1; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1658 | ctx->icsk_af_ops = icsk->icsk_af_ops; |
| 1659 | icsk->icsk_af_ops = subflow_default_af_ops(sk); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1660 | ctx->tcp_data_ready = sk->sk_data_ready; |
| 1661 | ctx->tcp_state_change = sk->sk_state_change; |
| 1662 | ctx->tcp_write_space = sk->sk_write_space; |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1663 | ctx->tcp_error_report = sk->sk_error_report; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1664 | sk->sk_data_ready = subflow_data_ready; |
| 1665 | sk->sk_write_space = subflow_write_space; |
| 1666 | sk->sk_state_change = subflow_state_change; |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1667 | sk->sk_error_report = subflow_error_report; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1668 | out: |
| 1669 | return err; |
| 1670 | } |
| 1671 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1672 | static void subflow_ulp_release(struct sock *ssk) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1673 | { |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1674 | struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); |
| 1675 | bool release = true; |
| 1676 | struct sock *sk; |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1677 | |
| 1678 | if (!ctx) |
| 1679 | return; |
| 1680 | |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1681 | sk = ctx->conn; |
| 1682 | if (sk) { |
| 1683 | /* if the msk has been orphaned, keep the ctx |
Paolo Abeni | 0597d0f | 2020-12-09 12:03:30 +0100 | [diff] [blame] | 1684 | * alive, will be freed by __mptcp_close_ssk(), |
| 1685 | * when the subflow is still unaccepted |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1686 | */ |
Paolo Abeni | 0597d0f | 2020-12-09 12:03:30 +0100 | [diff] [blame] | 1687 | release = ctx->disposable || list_empty(&ctx->node); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1688 | sock_put(sk); |
| 1689 | } |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1690 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1691 | mptcp_subflow_ops_undo_override(ssk); |
Paolo Abeni | e16163b | 2020-11-16 10:48:09 +0100 | [diff] [blame] | 1692 | if (release) |
| 1693 | kfree_rcu(ctx, rcu); |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1696 | static void subflow_ulp_clone(const struct request_sock *req, |
| 1697 | struct sock *newsk, |
| 1698 | const gfp_t priority) |
| 1699 | { |
| 1700 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 1701 | struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk); |
| 1702 | struct mptcp_subflow_context *new_ctx; |
| 1703 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1704 | if (!tcp_rsk(req)->is_mptcp || |
| 1705 | (!subflow_req->mp_capable && !subflow_req->mp_join)) { |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1706 | subflow_ulp_fallback(newsk, old_ctx); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1707 | return; |
| 1708 | } |
| 1709 | |
| 1710 | new_ctx = subflow_create_ctx(newsk, priority); |
Mat Martineau | edc7e48 | 2020-01-24 16:04:03 -0800 | [diff] [blame] | 1711 | if (!new_ctx) { |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1712 | subflow_ulp_fallback(newsk, old_ctx); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1713 | return; |
| 1714 | } |
| 1715 | |
| 1716 | new_ctx->conn_finished = 1; |
| 1717 | new_ctx->icsk_af_ops = old_ctx->icsk_af_ops; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1718 | new_ctx->tcp_data_ready = old_ctx->tcp_data_ready; |
| 1719 | new_ctx->tcp_state_change = old_ctx->tcp_state_change; |
| 1720 | new_ctx->tcp_write_space = old_ctx->tcp_write_space; |
Paolo Abeni | 15cc104 | 2021-02-11 15:30:37 -0800 | [diff] [blame] | 1721 | new_ctx->tcp_error_report = old_ctx->tcp_error_report; |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 1722 | new_ctx->rel_write_seq = 1; |
| 1723 | new_ctx->tcp_sock = newsk; |
| 1724 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1725 | if (subflow_req->mp_capable) { |
| 1726 | /* see comments in subflow_syn_recv_sock(), MPTCP connection |
| 1727 | * is fully established only after we receive the remote key |
| 1728 | */ |
| 1729 | new_ctx->mp_capable = 1; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1730 | new_ctx->local_key = subflow_req->local_key; |
| 1731 | new_ctx->token = subflow_req->token; |
| 1732 | new_ctx->ssn_offset = subflow_req->ssn_offset; |
| 1733 | new_ctx->idsn = subflow_req->idsn; |
| 1734 | } else if (subflow_req->mp_join) { |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1735 | new_ctx->ssn_offset = subflow_req->ssn_offset; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1736 | new_ctx->mp_join = 1; |
| 1737 | new_ctx->fully_established = 1; |
| 1738 | new_ctx->backup = subflow_req->backup; |
| 1739 | new_ctx->local_id = subflow_req->local_id; |
Geliang Tang | 2ff0e56 | 2020-09-08 10:49:39 +0800 | [diff] [blame] | 1740 | new_ctx->remote_id = subflow_req->remote_id; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1741 | new_ctx->token = subflow_req->token; |
| 1742 | new_ctx->thmac = subflow_req->thmac; |
| 1743 | } |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1746 | static void tcp_release_cb_override(struct sock *ssk) |
| 1747 | { |
| 1748 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); |
| 1749 | |
| 1750 | if (mptcp_subflow_has_delegated_action(subflow)) |
| 1751 | mptcp_subflow_process_delegated(ssk); |
| 1752 | |
| 1753 | tcp_release_cb(ssk); |
| 1754 | } |
| 1755 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1756 | static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = { |
| 1757 | .name = "mptcp", |
| 1758 | .owner = THIS_MODULE, |
| 1759 | .init = subflow_ulp_init, |
| 1760 | .release = subflow_ulp_release, |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1761 | .clone = subflow_ulp_clone, |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1762 | }; |
| 1763 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1764 | static int subflow_ops_init(struct request_sock_ops *subflow_ops) |
| 1765 | { |
| 1766 | subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock); |
| 1767 | subflow_ops->slab_name = "request_sock_subflow"; |
| 1768 | |
| 1769 | subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name, |
| 1770 | subflow_ops->obj_size, 0, |
| 1771 | SLAB_ACCOUNT | |
| 1772 | SLAB_TYPESAFE_BY_RCU, |
| 1773 | NULL); |
| 1774 | if (!subflow_ops->slab) |
| 1775 | return -ENOMEM; |
| 1776 | |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1777 | subflow_ops->destructor = subflow_req_destructor; |
| 1778 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1779 | return 0; |
| 1780 | } |
| 1781 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 1782 | void __init mptcp_subflow_init(void) |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1783 | { |
Florian Westphal | 08b8d08 | 2020-07-30 21:25:53 +0200 | [diff] [blame] | 1784 | mptcp_subflow_request_sock_ops = tcp_request_sock_ops; |
| 1785 | if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1786 | panic("MPTCP: failed to init subflow request sock ops\n"); |
| 1787 | |
| 1788 | subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops; |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 1789 | subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1790 | |
| 1791 | subflow_specific = ipv4_specific; |
| 1792 | subflow_specific.conn_request = subflow_v4_conn_request; |
| 1793 | subflow_specific.syn_recv_sock = subflow_syn_recv_sock; |
| 1794 | subflow_specific.sk_rx_dst_set = subflow_finish_connect; |
| 1795 | |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1796 | tcp_prot_override = tcp_prot; |
| 1797 | tcp_prot_override.release_cb = tcp_release_cb_override; |
| 1798 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1799 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1800 | subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops; |
Florian Westphal | 7ea851d | 2020-11-30 16:36:30 +0100 | [diff] [blame] | 1801 | subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1802 | |
| 1803 | subflow_v6_specific = ipv6_specific; |
| 1804 | subflow_v6_specific.conn_request = subflow_v6_conn_request; |
| 1805 | subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock; |
| 1806 | subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect; |
| 1807 | |
| 1808 | subflow_v6m_specific = subflow_v6_specific; |
| 1809 | subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit; |
| 1810 | subflow_v6m_specific.send_check = ipv4_specific.send_check; |
| 1811 | subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len; |
| 1812 | subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced; |
| 1813 | subflow_v6m_specific.net_frag_header_len = 0; |
Paolo Abeni | b19bc29 | 2021-01-20 15:39:14 +0100 | [diff] [blame] | 1814 | |
| 1815 | tcpv6_prot_override = tcpv6_prot; |
| 1816 | tcpv6_prot_override.release_cb = tcp_release_cb_override; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1817 | #endif |
| 1818 | |
Davide Caratti | 5147dfb | 2020-03-27 14:48:49 -0700 | [diff] [blame] | 1819 | mptcp_diag_subflow_init(&subflow_ulp_ops); |
| 1820 | |
Peter Krystad | 2303f99 | 2020-01-21 16:56:17 -0800 | [diff] [blame] | 1821 | if (tcp_register_ulp(&subflow_ulp_ops) != 0) |
| 1822 | panic("MPTCP: failed to register subflows to ULP\n"); |
| 1823 | } |