1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP token management
3 * Copyright (c) 2017 - 2019, Intel Corporation.
5 * Note: This code is based on mptcp_ctrl.c from multipath-tcp.org,
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>
23 #define pr_fmt(fmt) "MPTCP: " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/memblock.h>
29 #include <linux/tcp.h>
31 #include <net/inet_common.h>
32 #include <net/protocol.h>
33 #include <net/mptcp.h>
36 #define TOKEN_MAX_CHAIN_LEN 4
41 struct hlist_nulls_head req_chain;
42 struct hlist_nulls_head msk_chain;
45 static struct token_bucket *token_hash __read_mostly;
46 static unsigned int token_mask __read_mostly;
48 static struct token_bucket *token_bucket(u32 token)
50 return &token_hash[token & token_mask];
53 /* called with bucket lock held */
54 static struct mptcp_subflow_request_sock *
55 __token_lookup_req(struct token_bucket *t, u32 token)
57 struct mptcp_subflow_request_sock *req;
58 struct hlist_nulls_node *pos;
60 hlist_nulls_for_each_entry_rcu(req, pos, &t->req_chain, token_node)
61 if (req->token == token)
66 /* called with bucket lock held */
67 static struct mptcp_sock *
68 __token_lookup_msk(struct token_bucket *t, u32 token)
70 struct hlist_nulls_node *pos;
73 sk_nulls_for_each_rcu(sk, pos, &t->msk_chain)
74 if (mptcp_sk(sk)->token == token)
79 static bool __token_bucket_busy(struct token_bucket *t, u32 token)
81 return !token || t->chain_len >= TOKEN_MAX_CHAIN_LEN ||
82 __token_lookup_req(t, token) || __token_lookup_msk(t, token);
85 static void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
87 /* we might consider a faster version that computes the key as a
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
93 get_random_bytes(key, sizeof(u64));
94 mptcp_crypto_key_sha(*key, token, idsn);
98 * mptcp_token_new_request - create new key/idsn/token for subflow_request
99 * @req: the request socket
101 * This function is called when a new mptcp connection is coming in.
103 * It creates a unique token to identify the new mptcp connection,
104 * a secret local key and the initial data sequence number (idsn).
106 * Returns 0 on success.
108 int mptcp_token_new_request(struct request_sock *req)
110 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
111 struct token_bucket *bucket;
114 mptcp_crypto_key_sha(subflow_req->local_key,
117 pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n",
118 req, subflow_req->local_key, subflow_req->token,
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);
129 hlist_nulls_add_head_rcu(&subflow_req->token_node, &bucket->req_chain);
131 spin_unlock_bh(&bucket->lock);
136 * mptcp_token_new_connect - create new key/idsn/token for subflow
137 * @sk: the socket that will initiate a connection
139 * This function is called when a new outgoing mptcp connection is
142 * It creates a unique token to identify the new mptcp connection,
143 * a secret local key and the initial data sequence number (idsn).
145 * On success, the mptcp connection can be found again using
146 * the computed token at a later time, this is needed to process
149 * returns 0 on success.
151 int mptcp_token_new_connect(struct sock *sk)
153 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
154 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
155 int retries = MPTCP_TOKEN_MAX_RETRIES;
156 struct token_bucket *bucket;
159 mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token,
162 bucket = token_bucket(subflow->token);
163 spin_lock_bh(&bucket->lock);
164 if (__token_bucket_busy(bucket, subflow->token)) {
165 spin_unlock_bh(&bucket->lock);
171 pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
172 sk, subflow->local_key, subflow->token, subflow->idsn);
174 WRITE_ONCE(msk->token, subflow->token);
175 __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
177 spin_unlock_bh(&bucket->lock);
182 * mptcp_token_accept - replace a req sk with full sock in token hash
183 * @req: the request socket to be removed
184 * @msk: the just cloned socket linked to the new connection
186 * Called when a SYN packet creates a new logical connection, i.e.
187 * is not a join request.
189 void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
190 struct mptcp_sock *msk)
192 struct mptcp_subflow_request_sock *pos;
193 struct token_bucket *bucket;
195 bucket = token_bucket(req->token);
196 spin_lock_bh(&bucket->lock);
198 /* pedantic lookup check for the moved token */
199 pos = __token_lookup_req(bucket, req->token);
200 if (!WARN_ON_ONCE(pos != req))
201 hlist_nulls_del_init_rcu(&req->token_node);
202 __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
203 spin_unlock_bh(&bucket->lock);
206 bool mptcp_token_exists(u32 token)
208 struct hlist_nulls_node *pos;
209 struct token_bucket *bucket;
210 struct mptcp_sock *msk;
214 bucket = token_bucket(token);
217 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
219 if (READ_ONCE(msk->token) == token)
222 if (get_nulls_value(pos) != (token & token_mask))
233 * mptcp_token_get_sock - retrieve mptcp connection sock using its token
234 * @token: token of the mptcp connection to retrieve
236 * This function returns the mptcp connection structure with the given token.
237 * A reference count on the mptcp socket returned is taken.
239 * returns NULL if no connection with the given token value exists.
241 struct mptcp_sock *mptcp_token_get_sock(u32 token)
243 struct hlist_nulls_node *pos;
244 struct token_bucket *bucket;
245 struct mptcp_sock *msk;
249 bucket = token_bucket(token);
252 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
254 if (READ_ONCE(msk->token) != token)
256 if (!refcount_inc_not_zero(&sk->sk_refcnt))
258 if (READ_ONCE(msk->token) != token) {
264 if (get_nulls_value(pos) != (token & token_mask))
274 EXPORT_SYMBOL_GPL(mptcp_token_get_sock);
277 * mptcp_token_iter_next - iterate over the token container from given pos
278 * @net: namespace to be iterated
279 * @s_slot: start slot number
280 * @s_num: start number inside the given lock
282 * This function returns the first mptcp connection structure found inside the
283 * token container starting from the specified position, or NULL.
285 * On successful iteration, the iterator is move to the next position and the
286 * the acquires a reference to the returned socket.
288 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
291 struct mptcp_sock *ret = NULL;
292 struct hlist_nulls_node *pos;
295 for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
296 struct token_bucket *bucket = &token_hash[slot];
301 if (hlist_nulls_empty(&bucket->msk_chain))
305 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
307 if (!net_eq(sock_net(sk), net))
313 if (!refcount_inc_not_zero(&sk->sk_refcnt))
316 if (!net_eq(sock_net(sk), net)) {
333 EXPORT_SYMBOL_GPL(mptcp_token_iter_next);
336 * mptcp_token_destroy_request - remove mptcp connection/token
337 * @req: mptcp request socket dropping the token
339 * Remove the token associated to @req.
341 void mptcp_token_destroy_request(struct request_sock *req)
343 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
344 struct mptcp_subflow_request_sock *pos;
345 struct token_bucket *bucket;
347 if (hlist_nulls_unhashed(&subflow_req->token_node))
350 bucket = token_bucket(subflow_req->token);
351 spin_lock_bh(&bucket->lock);
352 pos = __token_lookup_req(bucket, subflow_req->token);
353 if (!WARN_ON_ONCE(pos != subflow_req)) {
354 hlist_nulls_del_init_rcu(&pos->token_node);
357 spin_unlock_bh(&bucket->lock);
361 * mptcp_token_destroy - remove mptcp connection/token
362 * @msk: mptcp connection dropping the token
364 * Remove the token associated to @msk
366 void mptcp_token_destroy(struct mptcp_sock *msk)
368 struct token_bucket *bucket;
369 struct mptcp_sock *pos;
371 if (sk_unhashed((struct sock *)msk))
374 bucket = token_bucket(msk->token);
375 spin_lock_bh(&bucket->lock);
376 pos = __token_lookup_msk(bucket, msk->token);
377 if (!WARN_ON_ONCE(pos != msk)) {
378 __sk_nulls_del_node_init_rcu((struct sock *)pos);
381 spin_unlock_bh(&bucket->lock);
384 void __init mptcp_token_init(void)
388 token_hash = alloc_large_system_hash("MPTCP token",
389 sizeof(struct token_bucket),
391 20,/* one slot per 1MB of memory */
397 for (i = 0; i < token_mask + 1; ++i) {
398 INIT_HLIST_NULLS_HEAD(&token_hash[i].req_chain, i);
399 INIT_HLIST_NULLS_HEAD(&token_hash[i].msk_chain, i);
400 spin_lock_init(&token_hash[i].lock);
404 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
405 EXPORT_SYMBOL_GPL(mptcp_token_new_request);
406 EXPORT_SYMBOL_GPL(mptcp_token_new_connect);
407 EXPORT_SYMBOL_GPL(mptcp_token_accept);
408 EXPORT_SYMBOL_GPL(mptcp_token_destroy_request);
409 EXPORT_SYMBOL_GPL(mptcp_token_destroy);