| 1 | |
| 2 | |
| 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é <[email protected]>
|
| 9 | * Christoph Paasch <[email protected]>
|
| 10 | * Jaakko Korkeaniemi <[email protected]>
|
| 11 | * Gregory Detal <[email protected]>
|
| 12 | * Fabien Duchêne <[email protected]>
|
| 13 | * Andreas Seelinger <[email protected]>
|
| 14 | * Lavkesh Lahngir <[email protected]>
|
| 15 | * Andreas Ripke <[email protected]>
|
| 16 | * Vlad Dogaru <[email protected]>
|
| 17 | * Octavian Purdila <[email protected]>
|
| 18 | * John Ronan <[email protected]>
|
| 19 | * Catalin Nicutar <[email protected]>
|
| 20 | * Brandon Heller <[email protected]>
|
| 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_CHAIN_LEN 4
|
| 37 |
|
| 38 | struct token_bucket {
|
| 39 | spinlock_t lock;
|
| 40 | int chain_len;
|
| 41 | struct hlist_nulls_head req_chain;
|
| 42 | struct hlist_nulls_head msk_chain;
|
| 43 | };
|
| 44 |
|
| 45 | static struct token_bucket *token_hash __read_mostly;
|
| 46 | static unsigned int token_mask __read_mostly;
|
| 47 |
|
| 48 | static struct token_bucket *token_bucket(u32 token)
|
| 49 | {
|
| 50 | return &token_hash[token & token_mask];
|
| 51 | }
|
| 52 |
|
| 53 |
|
| 54 | static struct mptcp_subflow_request_sock *
|
| 55 | __token_lookup_req(struct token_bucket *t, u32 token)
|
| 56 | {
|
| 57 | struct mptcp_subflow_request_sock *req;
|
| 58 | struct hlist_nulls_node *pos;
|
| 59 |
|
| 60 | hlist_nulls_for_each_entry_rcu(req, pos, &t->req_chain, token_node)
|
| 61 | if (req->token == token)
|
| 62 | return req;
|
| 63 | return NULL;
|
| 64 | }
|
| 65 |
|
| 66 |
|
| 67 | static struct mptcp_sock *
|
| 68 | __token_lookup_msk(struct token_bucket *t, u32 token)
|
| 69 | {
|
| 70 | struct hlist_nulls_node *pos;
|
| 71 | struct sock *sk;
|
| 72 |
|
| 73 | sk_nulls_for_each_rcu(sk, pos, &t->msk_chain)
|
| 74 | if (mptcp_sk(sk)->token == token)
|
| 75 | return mptcp_sk(sk);
|
| 76 | return NULL;
|
| 77 | }
|
| 78 |
|
| 79 | static bool __token_bucket_busy(struct token_bucket *t, u32 token)
|
| 80 | {
|
| 81 | return !token || t->chain_len >= TOKEN_MAX_CHAIN_LEN ||
|
| 82 | __token_lookup_req(t, token) || __token_lookup_msk(t, token);
|
| 83 | }
|
| 84 |
|
| 85 | static void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
|
| 86 | {
|
| 87 | |
| 88 | * hash of some information available in the MPTCP socket. Use
|
| 89 | * random data at the moment, as it's probably the safest option
|
| 90 | * in case multiple sockets are opened in different namespaces at
|
| 91 | * the same time.
|
| 92 |
|
| 93 | get_random_bytes(key, sizeof(u64));
|
| 94 | mptcp_crypto_key_sha(*key, token, idsn);
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | * mptcp_token_new_request - create new key/idsn/token for subflow_request
|
| 99 | * @req: the request socket
|
| 100 | *
|
| 101 | * This function is called when a new mptcp connection is coming in.
|
| 102 | *
|
| 103 | * It creates a unique token to identify the new mptcp connection,
|
| 104 | * a secret local key and the initial data sequence number (idsn).
|
| 105 | *
|
| 106 | * Return: 0 on success.
|
| 107 |
|
| 108 | int mptcp_token_new_request(struct request_sock *req)
|
| 109 | {
|
| 110 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
| 111 | struct token_bucket *bucket;
|
| 112 | u32 token;
|
| 113 |
|
| 114 | mptcp_crypto_key_sha(subflow_req->local_key,
|
| 115 | &subflow_req->token,
|
| 116 | &subflow_req->idsn);
|
| 117 | pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n",
|
| 118 | req, subflow_req->local_key, subflow_req->token,
|
| 119 | subflow_req->idsn);
|
| 120 |
|
| 121 | token = subflow_req->token;
|
| 122 | bucket = token_bucket(token);
|
| 123 | spin_lock_bh(&bucket->lock);
|
| 124 | if (__token_bucket_busy(bucket, token)) {
|
| 125 | spin_unlock_bh(&bucket->lock);
|
| 126 | return -EBUSY;
|
| 127 | }
|
| 128 |
|
| 129 | hlist_nulls_add_head_rcu(&subflow_req->token_node, &bucket->req_chain);
|
| 130 | bucket->chain_len++;
|
| 131 | spin_unlock_bh(&bucket->lock);
|
| 132 | return 0;
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | * mptcp_token_new_connect - create new key/idsn/token for subflow
|
| 137 | * @ssk: the socket that will initiate a connection
|
| 138 | *
|
| 139 | * This function is called when a new outgoing mptcp connection is
|
| 140 | * initiated.
|
| 141 | *
|
| 142 | * It creates a unique token to identify the new mptcp connection,
|
| 143 | * a secret local key and the initial data sequence number (idsn).
|
| 144 | *
|
| 145 | * On success, the mptcp connection can be found again using
|
| 146 | * the computed token at a later time, this is needed to process
|
| 147 | * join requests.
|
| 148 | *
|
| 149 | * Return: 0 on success.
|
| 150 |
|
| 151 | int mptcp_token_new_connect(struct sock *ssk)
|
| 152 | {
|
| 153 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
|
| 154 | struct mptcp_sock *msk = mptcp_sk(subflow->conn);
|
| 155 | int retries = MPTCP_TOKEN_MAX_RETRIES;
|
| 156 | struct sock *sk = subflow->conn;
|
| 157 | struct token_bucket *bucket;
|
| 158 |
|
| 159 | again:
|
| 160 | mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token,
|
| 161 | &subflow->idsn);
|
| 162 |
|
| 163 | bucket = token_bucket(subflow->token);
|
| 164 | spin_lock_bh(&bucket->lock);
|
| 165 | if (__token_bucket_busy(bucket, subflow->token)) {
|
| 166 | spin_unlock_bh(&bucket->lock);
|
| 167 | if (!--retries)
|
| 168 | return -EBUSY;
|
| 169 | goto again;
|
| 170 | }
|
| 171 |
|
| 172 | pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
|
| 173 | ssk, subflow->local_key, subflow->token, subflow->idsn);
|
| 174 |
|
| 175 | WRITE_ONCE(msk->token, subflow->token);
|
| 176 | __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
|
| 177 | bucket->chain_len++;
|
| 178 | spin_unlock_bh(&bucket->lock);
|
| 179 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
|
| 180 | return 0;
|
| 181 | }
|
| 182 |
|
| 183 | |
| 184 | * mptcp_token_accept - replace a req sk with full sock in token hash
|
| 185 | * @req: the request socket to be removed
|
| 186 | * @msk: the just cloned socket linked to the new connection
|
| 187 | *
|
| 188 | * Called when a SYN packet creates a new logical connection, i.e.
|
| 189 | * is not a join request.
|
| 190 |
|
| 191 | void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
|
| 192 | struct mptcp_sock *msk)
|
| 193 | {
|
| 194 | struct mptcp_subflow_request_sock *pos;
|
| 195 | struct sock *sk = (struct sock *)msk;
|
| 196 | struct token_bucket *bucket;
|
| 197 |
|
| 198 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
|
| 199 | bucket = token_bucket(req->token);
|
| 200 | spin_lock_bh(&bucket->lock);
|
| 201 |
|
| 202 |
|
| 203 | pos = __token_lookup_req(bucket, req->token);
|
| 204 | if (!WARN_ON_ONCE(pos != req))
|
| 205 | hlist_nulls_del_init_rcu(&req->token_node);
|
| 206 | __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
|
| 207 | spin_unlock_bh(&bucket->lock);
|
| 208 | }
|
| 209 |
|
| 210 | bool mptcp_token_exists(u32 token)
|
| 211 | {
|
| 212 | struct hlist_nulls_node *pos;
|
| 213 | struct token_bucket *bucket;
|
| 214 | struct mptcp_sock *msk;
|
| 215 | struct sock *sk;
|
| 216 |
|
| 217 | rcu_read_lock();
|
| 218 | bucket = token_bucket(token);
|
| 219 |
|
| 220 | again:
|
| 221 | sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
|
| 222 | msk = mptcp_sk(sk);
|
| 223 | if (READ_ONCE(msk->token) == token)
|
| 224 | goto found;
|
| 225 | }
|
| 226 | if (get_nulls_value(pos) != (token & token_mask))
|
| 227 | goto again;
|
| 228 |
|
| 229 | rcu_read_unlock();
|
| 230 | return false;
|
| 231 | found:
|
| 232 | rcu_read_unlock();
|
| 233 | return true;
|
| 234 | }
|
| 235 |
|
| 236 | |
| 237 | * mptcp_token_get_sock - retrieve mptcp connection sock using its token
|
| 238 | * @net: restrict to this namespace
|
| 239 | * @token: token of the mptcp connection to retrieve
|
| 240 | *
|
| 241 | * This function returns the mptcp connection structure with the given token.
|
| 242 | * A reference count on the mptcp socket returned is taken.
|
| 243 | *
|
| 244 | * Return: NULL if no connection with the given token value exists.
|
| 245 |
|
| 246 | struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token)
|
| 247 | {
|
| 248 | struct hlist_nulls_node *pos;
|
| 249 | struct token_bucket *bucket;
|
| 250 | struct mptcp_sock *msk;
|
| 251 | struct sock *sk;
|
| 252 |
|
| 253 | rcu_read_lock();
|
| 254 | bucket = token_bucket(token);
|
| 255 |
|
| 256 | again:
|
| 257 | sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
|
| 258 | msk = mptcp_sk(sk);
|
| 259 | if (READ_ONCE(msk->token) != token ||
|
| 260 | !net_eq(sock_net(sk), net))
|
| 261 | continue;
|
| 262 |
|
| 263 | if (!refcount_inc_not_zero(&sk->sk_refcnt))
|
| 264 | goto not_found;
|
| 265 |
|
| 266 | if (READ_ONCE(msk->token) != token ||
|
| 267 | !net_eq(sock_net(sk), net)) {
|
| 268 | sock_put(sk);
|
| 269 | goto again;
|
| 270 | }
|
| 271 | goto found;
|
| 272 | }
|
| 273 | if (get_nulls_value(pos) != (token & token_mask))
|
| 274 | goto again;
|
| 275 |
|
| 276 | not_found:
|
| 277 | msk = NULL;
|
| 278 |
|
| 279 | found:
|
| 280 | rcu_read_unlock();
|
| 281 | return msk;
|
| 282 | }
|
| 283 | EXPORT_SYMBOL_GPL(mptcp_token_get_sock);
|
| 284 |
|
| 285 | |
| 286 | * mptcp_token_iter_next - iterate over the token container from given pos
|
| 287 | * @net: namespace to be iterated
|
| 288 | * @s_slot: start slot number
|
| 289 | * @s_num: start number inside the given lock
|
| 290 | *
|
| 291 | * Description:
|
| 292 | * On successful iteration, the iterator is moved to the next position and a
|
| 293 | * reference to the returned socket is acquired.
|
| 294 | *
|
| 295 | * Return:
|
| 296 | * The first mptcp connection structure found inside the token container
|
| 297 | * starting from the specified position, or NULL.
|
| 298 |
|
| 299 | struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
|
| 300 | long *s_num)
|
| 301 | {
|
| 302 | struct mptcp_sock *ret = NULL;
|
| 303 | struct hlist_nulls_node *pos;
|
| 304 | int slot, num = 0;
|
| 305 |
|
| 306 | for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
|
| 307 | struct token_bucket *bucket = &token_hash[slot];
|
| 308 | struct sock *sk;
|
| 309 |
|
| 310 | num = 0;
|
| 311 |
|
| 312 | if (hlist_nulls_empty(&bucket->msk_chain))
|
| 313 | continue;
|
| 314 |
|
| 315 | rcu_read_lock();
|
| 316 | sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
|
| 317 | ++num;
|
| 318 | if (!net_eq(sock_net(sk), net))
|
| 319 | continue;
|
| 320 |
|
| 321 | if (num <= *s_num)
|
| 322 | continue;
|
| 323 |
|
| 324 | if (!refcount_inc_not_zero(&sk->sk_refcnt))
|
| 325 | continue;
|
| 326 |
|
| 327 | if (!net_eq(sock_net(sk), net)) {
|
| 328 | sock_put(sk);
|
| 329 | continue;
|
| 330 | }
|
| 331 |
|
| 332 | ret = mptcp_sk(sk);
|
| 333 | rcu_read_unlock();
|
| 334 | goto out;
|
| 335 | }
|
| 336 | rcu_read_unlock();
|
| 337 | }
|
| 338 |
|
| 339 | out:
|
| 340 | *s_slot = slot;
|
| 341 | *s_num = num;
|
| 342 | return ret;
|
| 343 | }
|
| 344 | EXPORT_SYMBOL_GPL(mptcp_token_iter_next);
|
| 345 |
|
| 346 | |
| 347 | * mptcp_token_destroy_request - remove mptcp connection/token
|
| 348 | * @req: mptcp request socket dropping the token
|
| 349 | *
|
| 350 | * Remove the token associated to @req.
|
| 351 |
|
| 352 | void mptcp_token_destroy_request(struct request_sock *req)
|
| 353 | {
|
| 354 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
|
| 355 | struct mptcp_subflow_request_sock *pos;
|
| 356 | struct token_bucket *bucket;
|
| 357 |
|
| 358 | if (hlist_nulls_unhashed(&subflow_req->token_node))
|
| 359 | return;
|
| 360 |
|
| 361 | bucket = token_bucket(subflow_req->token);
|
| 362 | spin_lock_bh(&bucket->lock);
|
| 363 | pos = __token_lookup_req(bucket, subflow_req->token);
|
| 364 | if (!WARN_ON_ONCE(pos != subflow_req)) {
|
| 365 | hlist_nulls_del_init_rcu(&pos->token_node);
|
| 366 | bucket->chain_len--;
|
| 367 | }
|
| 368 | spin_unlock_bh(&bucket->lock);
|
| 369 | }
|
| 370 |
|
| 371 | |
| 372 | * mptcp_token_destroy - remove mptcp connection/token
|
| 373 | * @msk: mptcp connection dropping the token
|
| 374 | *
|
| 375 | * Remove the token associated to @msk
|
| 376 |
|
| 377 | void mptcp_token_destroy(struct mptcp_sock *msk)
|
| 378 | {
|
| 379 | struct sock *sk = (struct sock *)msk;
|
| 380 | struct token_bucket *bucket;
|
| 381 | struct mptcp_sock *pos;
|
| 382 |
|
| 383 | if (sk_unhashed((struct sock *)msk))
|
| 384 | return;
|
| 385 |
|
| 386 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
|
| 387 | bucket = token_bucket(msk->token);
|
| 388 | spin_lock_bh(&bucket->lock);
|
| 389 | pos = __token_lookup_msk(bucket, msk->token);
|
| 390 | if (!WARN_ON_ONCE(pos != msk)) {
|
| 391 | __sk_nulls_del_node_init_rcu((struct sock *)pos);
|
| 392 | bucket->chain_len--;
|
| 393 | }
|
| 394 | spin_unlock_bh(&bucket->lock);
|
| 395 | WRITE_ONCE(msk->token, 0);
|
| 396 | }
|
| 397 |
|
| 398 | void __init mptcp_token_init(void)
|
| 399 | {
|
| 400 | int i;
|
| 401 |
|
| 402 | token_hash = alloc_large_system_hash("MPTCP token",
|
| 403 | sizeof(struct token_bucket),
|
| 404 | 0,
|
| 405 | 20,
|
| 406 | HASH_ZERO,
|
| 407 | NULL,
|
| 408 | &token_mask,
|
| 409 | 0,
|
| 410 | 64 * 1024);
|
| 411 | for (i = 0; i < token_mask + 1; ++i) {
|
| 412 | INIT_HLIST_NULLS_HEAD(&token_hash[i].req_chain, i);
|
| 413 | INIT_HLIST_NULLS_HEAD(&token_hash[i].msk_chain, i);
|
| 414 | spin_lock_init(&token_hash[i].lock);
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
|
| 419 | EXPORT_SYMBOL_GPL(mptcp_token_new_request);
|
| 420 | EXPORT_SYMBOL_GPL(mptcp_token_new_connect);
|
| 421 | EXPORT_SYMBOL_GPL(mptcp_token_accept);
|
| 422 | EXPORT_SYMBOL_GPL(mptcp_token_destroy_request);
|
| 423 | EXPORT_SYMBOL_GPL(mptcp_token_destroy);
|
| 424 | #endif
|