1 // SPDX-License-Identifier: GPL-2.0 2 /* Multipath TCP token management 3 * Copyright (c) 2017 - 2019, Intel Corporation. 4 * 5 * Note: This code is based on mptcp_ctrl.c from multipath-tcp.org, 6 * authored by: 7 * 8 * Sébastien Barré <sebastien.barre@uclouvain.be> 9 * Christoph Paasch <christoph.paasch@uclouvain.be> 10 * Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi> 11 * Gregory Detal <gregory.detal@uclouvain.be> 12 * Fabien Duchêne <fabien.duchene@uclouvain.be> 13 * Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de> 14 * Lavkesh Lahngir <lavkesh51@gmail.com> 15 * Andreas Ripke <ripke@neclab.eu> 16 * Vlad Dogaru <vlad.dogaru@intel.com> 17 * Octavian Purdila <octavian.purdila@intel.com> 18 * John Ronan <jronan@tssg.org> 19 * Catalin Nicutar <catalin.nicutar@gmail.com> 20 * Brandon Heller <brandonh@stanford.edu> 21 */ 22 23 #define pr_fmt(fmt) "MPTCP: " fmt 24 25 #include <linux/kernel.h> 26 #include <linux/module.h> 27 #include <linux/memblock.h> 28 #include <linux/ip.h> 29 #include <linux/tcp.h> 30 #include <net/sock.h> 31 #include <net/inet_common.h> 32 #include <net/protocol.h> 33 #include <net/mptcp.h> 34 #include "protocol.h" 35 36 #define TOKEN_MAX_RETRIES 4 37 #define TOKEN_MAX_CHAIN_LEN 4 38 39 struct token_bucket { 40 spinlock_t lock; 41 int chain_len; 42 struct hlist_nulls_head req_chain; 43 struct hlist_nulls_head msk_chain; 44 }; 45 46 static struct token_bucket *token_hash __read_mostly; 47 static unsigned int token_mask __read_mostly; 48 49 static struct token_bucket *token_bucket(u32 token) 50 { 51 return &token_hash[token & token_mask]; 52 } 53 54 /* called with bucket lock held */ 55 static struct mptcp_subflow_request_sock * 56 __token_lookup_req(struct token_bucket *t, u32 token) 57 { 58 struct mptcp_subflow_request_sock *req; 59 struct hlist_nulls_node *pos; 60 61 hlist_nulls_for_each_entry_rcu(req, pos, &t->req_chain, token_node) 62 if (req->token == token) 63 return req; 64 return NULL; 65 } 66 67 /* called with bucket lock held */ 68 static struct mptcp_sock * 69 __token_lookup_msk(struct token_bucket *t, u32 token) 70 { 71 struct hlist_nulls_node *pos; 72 struct sock *sk; 73 74 sk_nulls_for_each_rcu(sk, pos, &t->msk_chain) 75 if (mptcp_sk(sk)->token == token) 76 return mptcp_sk(sk); 77 return NULL; 78 } 79 80 static bool __token_bucket_busy(struct token_bucket *t, u32 token) 81 { 82 return !token || t->chain_len >= TOKEN_MAX_CHAIN_LEN || 83 __token_lookup_req(t, token) || __token_lookup_msk(t, token); 84 } 85 86 /** 87 * mptcp_token_new_request - create new key/idsn/token for subflow_request 88 * @req: the request socket 89 * 90 * This function is called when a new mptcp connection is coming in. 91 * 92 * It creates a unique token to identify the new mptcp connection, 93 * a secret local key and the initial data sequence number (idsn). 94 * 95 * Returns 0 on success. 96 */ 97 int mptcp_token_new_request(struct request_sock *req) 98 { 99 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 100 int retries = TOKEN_MAX_RETRIES; 101 struct token_bucket *bucket; 102 u32 token; 103 104 again: 105 mptcp_crypto_key_gen_sha(&subflow_req->local_key, 106 &subflow_req->token, 107 &subflow_req->idsn); 108 pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n", 109 req, subflow_req->local_key, subflow_req->token, 110 subflow_req->idsn); 111 112 token = subflow_req->token; 113 bucket = token_bucket(token); 114 spin_lock_bh(&bucket->lock); 115 if (__token_bucket_busy(bucket, token)) { 116 spin_unlock_bh(&bucket->lock); 117 if (!--retries) 118 return -EBUSY; 119 goto again; 120 } 121 122 hlist_nulls_add_head_rcu(&subflow_req->token_node, &bucket->req_chain); 123 bucket->chain_len++; 124 spin_unlock_bh(&bucket->lock); 125 return 0; 126 } 127 128 /** 129 * mptcp_token_new_connect - create new key/idsn/token for subflow 130 * @sk: the socket that will initiate a connection 131 * 132 * This function is called when a new outgoing mptcp connection is 133 * initiated. 134 * 135 * It creates a unique token to identify the new mptcp connection, 136 * a secret local key and the initial data sequence number (idsn). 137 * 138 * On success, the mptcp connection can be found again using 139 * the computed token at a later time, this is needed to process 140 * join requests. 141 * 142 * returns 0 on success. 143 */ 144 int mptcp_token_new_connect(struct sock *sk) 145 { 146 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 147 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 148 int retries = TOKEN_MAX_RETRIES; 149 struct token_bucket *bucket; 150 151 pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n", 152 sk, subflow->local_key, subflow->token, subflow->idsn); 153 154 again: 155 mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token, 156 &subflow->idsn); 157 158 bucket = token_bucket(subflow->token); 159 spin_lock_bh(&bucket->lock); 160 if (__token_bucket_busy(bucket, subflow->token)) { 161 spin_unlock_bh(&bucket->lock); 162 if (!--retries) 163 return -EBUSY; 164 goto again; 165 } 166 167 WRITE_ONCE(msk->token, subflow->token); 168 __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain); 169 bucket->chain_len++; 170 spin_unlock_bh(&bucket->lock); 171 return 0; 172 } 173 174 /** 175 * mptcp_token_accept - replace a req sk with full sock in token hash 176 * @req: the request socket to be removed 177 * @msk: the just cloned socket linked to the new connection 178 * 179 * Called when a SYN packet creates a new logical connection, i.e. 180 * is not a join request. 181 */ 182 void mptcp_token_accept(struct mptcp_subflow_request_sock *req, 183 struct mptcp_sock *msk) 184 { 185 struct mptcp_subflow_request_sock *pos; 186 struct token_bucket *bucket; 187 188 bucket = token_bucket(req->token); 189 spin_lock_bh(&bucket->lock); 190 191 /* pedantic lookup check for the moved token */ 192 pos = __token_lookup_req(bucket, req->token); 193 if (!WARN_ON_ONCE(pos != req)) 194 hlist_nulls_del_init_rcu(&req->token_node); 195 __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain); 196 spin_unlock_bh(&bucket->lock); 197 } 198 199 /** 200 * mptcp_token_get_sock - retrieve mptcp connection sock using its token 201 * @token: token of the mptcp connection to retrieve 202 * 203 * This function returns the mptcp connection structure with the given token. 204 * A reference count on the mptcp socket returned is taken. 205 * 206 * returns NULL if no connection with the given token value exists. 207 */ 208 struct mptcp_sock *mptcp_token_get_sock(u32 token) 209 { 210 struct hlist_nulls_node *pos; 211 struct token_bucket *bucket; 212 struct mptcp_sock *msk; 213 struct sock *sk; 214 215 rcu_read_lock(); 216 bucket = token_bucket(token); 217 218 again: 219 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) { 220 msk = mptcp_sk(sk); 221 if (READ_ONCE(msk->token) != token) 222 continue; 223 if (!refcount_inc_not_zero(&sk->sk_refcnt)) 224 goto not_found; 225 if (READ_ONCE(msk->token) != token) { 226 sock_put(sk); 227 goto again; 228 } 229 goto found; 230 } 231 if (get_nulls_value(pos) != (token & token_mask)) 232 goto again; 233 234 not_found: 235 msk = NULL; 236 237 found: 238 rcu_read_unlock(); 239 return msk; 240 } 241 242 /** 243 * mptcp_token_destroy_request - remove mptcp connection/token 244 * @req: mptcp request socket dropping the token 245 * 246 * Remove the token associated to @req. 247 */ 248 void mptcp_token_destroy_request(struct request_sock *req) 249 { 250 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 251 struct mptcp_subflow_request_sock *pos; 252 struct token_bucket *bucket; 253 254 if (hlist_nulls_unhashed(&subflow_req->token_node)) 255 return; 256 257 bucket = token_bucket(subflow_req->token); 258 spin_lock_bh(&bucket->lock); 259 pos = __token_lookup_req(bucket, subflow_req->token); 260 if (!WARN_ON_ONCE(pos != subflow_req)) { 261 hlist_nulls_del_init_rcu(&pos->token_node); 262 bucket->chain_len--; 263 } 264 spin_unlock_bh(&bucket->lock); 265 } 266 267 /** 268 * mptcp_token_destroy - remove mptcp connection/token 269 * @msk: mptcp connection dropping the token 270 * 271 * Remove the token associated to @msk 272 */ 273 void mptcp_token_destroy(struct mptcp_sock *msk) 274 { 275 struct token_bucket *bucket; 276 struct mptcp_sock *pos; 277 278 if (sk_unhashed((struct sock *)msk)) 279 return; 280 281 bucket = token_bucket(msk->token); 282 spin_lock_bh(&bucket->lock); 283 pos = __token_lookup_msk(bucket, msk->token); 284 if (!WARN_ON_ONCE(pos != msk)) { 285 __sk_nulls_del_node_init_rcu((struct sock *)pos); 286 bucket->chain_len--; 287 } 288 spin_unlock_bh(&bucket->lock); 289 } 290 291 void __init mptcp_token_init(void) 292 { 293 int i; 294 295 token_hash = alloc_large_system_hash("MPTCP token", 296 sizeof(struct token_bucket), 297 0, 298 20,/* one slot per 1MB of memory */ 299 0, 300 NULL, 301 &token_mask, 302 0, 303 64 * 1024); 304 for (i = 0; i < token_mask + 1; ++i) { 305 INIT_HLIST_NULLS_HEAD(&token_hash[i].req_chain, i); 306 INIT_HLIST_NULLS_HEAD(&token_hash[i].msk_chain, i); 307 spin_lock_init(&token_hash[i].lock); 308 } 309 } 310 311 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TESTS) 312 EXPORT_SYMBOL_GPL(mptcp_token_new_request); 313 EXPORT_SYMBOL_GPL(mptcp_token_new_connect); 314 EXPORT_SYMBOL_GPL(mptcp_token_accept); 315 EXPORT_SYMBOL_GPL(mptcp_token_get_sock); 316 EXPORT_SYMBOL_GPL(mptcp_token_destroy_request); 317 EXPORT_SYMBOL_GPL(mptcp_token_destroy); 318 #endif 319