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_RETRIES 4
37 #define TOKEN_MAX_CHAIN_LEN 4
42 struct hlist_nulls_head req_chain;
43 struct hlist_nulls_head msk_chain;
46 static struct token_bucket *token_hash __read_mostly;
47 static unsigned int token_mask __read_mostly;
49 static struct token_bucket *token_bucket(u32 token)
51 return &token_hash[token & token_mask];
54 /* called with bucket lock held */
55 static struct mptcp_subflow_request_sock *
56 __token_lookup_req(struct token_bucket *t, u32 token)
58 struct mptcp_subflow_request_sock *req;
59 struct hlist_nulls_node *pos;
61 hlist_nulls_for_each_entry_rcu(req, pos, &t->req_chain, token_node)
62 if (req->token == token)
67 /* called with bucket lock held */
68 static struct mptcp_sock *
69 __token_lookup_msk(struct token_bucket *t, u32 token)
71 struct hlist_nulls_node *pos;
74 sk_nulls_for_each_rcu(sk, pos, &t->msk_chain)
75 if (mptcp_sk(sk)->token == token)
80 static bool __token_bucket_busy(struct token_bucket *t, u32 token)
82 return !token || t->chain_len >= TOKEN_MAX_CHAIN_LEN ||
83 __token_lookup_req(t, token) || __token_lookup_msk(t, token);
87 * mptcp_token_new_request - create new key/idsn/token for subflow_request
88 * @req: the request socket
90 * This function is called when a new mptcp connection is coming in.
92 * It creates a unique token to identify the new mptcp connection,
93 * a secret local key and the initial data sequence number (idsn).
95 * Returns 0 on success.
97 int mptcp_token_new_request(struct request_sock *req)
99 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
100 int retries = TOKEN_MAX_RETRIES;
101 struct token_bucket *bucket;
105 mptcp_crypto_key_gen_sha(&subflow_req->local_key,
108 pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n",
109 req, subflow_req->local_key, subflow_req->token,
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);
122 hlist_nulls_add_head_rcu(&subflow_req->token_node, &bucket->req_chain);
124 spin_unlock_bh(&bucket->lock);
129 * mptcp_token_new_connect - create new key/idsn/token for subflow
130 * @sk: the socket that will initiate a connection
132 * This function is called when a new outgoing mptcp connection is
135 * It creates a unique token to identify the new mptcp connection,
136 * a secret local key and the initial data sequence number (idsn).
138 * On success, the mptcp connection can be found again using
139 * the computed token at a later time, this is needed to process
142 * returns 0 on success.
144 int mptcp_token_new_connect(struct sock *sk)
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;
151 pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
152 sk, subflow->local_key, subflow->token, subflow->idsn);
155 mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token,
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);
167 WRITE_ONCE(msk->token, subflow->token);
168 __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
170 spin_unlock_bh(&bucket->lock);
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
179 * Called when a SYN packet creates a new logical connection, i.e.
180 * is not a join request.
182 void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
183 struct mptcp_sock *msk)
185 struct mptcp_subflow_request_sock *pos;
186 struct token_bucket *bucket;
188 bucket = token_bucket(req->token);
189 spin_lock_bh(&bucket->lock);
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);
200 * mptcp_token_get_sock - retrieve mptcp connection sock using its token
201 * @token: token of the mptcp connection to retrieve
203 * This function returns the mptcp connection structure with the given token.
204 * A reference count on the mptcp socket returned is taken.
206 * returns NULL if no connection with the given token value exists.
208 struct mptcp_sock *mptcp_token_get_sock(u32 token)
210 struct hlist_nulls_node *pos;
211 struct token_bucket *bucket;
212 struct mptcp_sock *msk;
216 bucket = token_bucket(token);
219 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
221 if (READ_ONCE(msk->token) != token)
223 if (!refcount_inc_not_zero(&sk->sk_refcnt))
225 if (READ_ONCE(msk->token) != token) {
231 if (get_nulls_value(pos) != (token & token_mask))
241 EXPORT_SYMBOL_GPL(mptcp_token_get_sock);
244 * mptcp_token_iter_next - iterate over the token container from given pos
245 * @net: namespace to be iterated
246 * @s_slot: start slot number
247 * @s_num: start number inside the given lock
249 * This function returns the first mptcp connection structure found inside the
250 * token container starting from the specified position, or NULL.
252 * On successful iteration, the iterator is move to the next position and the
253 * the acquires a reference to the returned socket.
255 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
258 struct mptcp_sock *ret = NULL;
259 struct hlist_nulls_node *pos;
262 for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
263 struct token_bucket *bucket = &token_hash[slot];
268 if (hlist_nulls_empty(&bucket->msk_chain))
272 sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
274 if (!net_eq(sock_net(sk), net))
280 if (!refcount_inc_not_zero(&sk->sk_refcnt))
283 if (!net_eq(sock_net(sk), net)) {
300 EXPORT_SYMBOL_GPL(mptcp_token_iter_next);
303 * mptcp_token_destroy_request - remove mptcp connection/token
304 * @req: mptcp request socket dropping the token
306 * Remove the token associated to @req.
308 void mptcp_token_destroy_request(struct request_sock *req)
310 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
311 struct mptcp_subflow_request_sock *pos;
312 struct token_bucket *bucket;
314 if (hlist_nulls_unhashed(&subflow_req->token_node))
317 bucket = token_bucket(subflow_req->token);
318 spin_lock_bh(&bucket->lock);
319 pos = __token_lookup_req(bucket, subflow_req->token);
320 if (!WARN_ON_ONCE(pos != subflow_req)) {
321 hlist_nulls_del_init_rcu(&pos->token_node);
324 spin_unlock_bh(&bucket->lock);
328 * mptcp_token_destroy - remove mptcp connection/token
329 * @msk: mptcp connection dropping the token
331 * Remove the token associated to @msk
333 void mptcp_token_destroy(struct mptcp_sock *msk)
335 struct token_bucket *bucket;
336 struct mptcp_sock *pos;
338 if (sk_unhashed((struct sock *)msk))
341 bucket = token_bucket(msk->token);
342 spin_lock_bh(&bucket->lock);
343 pos = __token_lookup_msk(bucket, msk->token);
344 if (!WARN_ON_ONCE(pos != msk)) {
345 __sk_nulls_del_node_init_rcu((struct sock *)pos);
348 spin_unlock_bh(&bucket->lock);
351 void __init mptcp_token_init(void)
355 token_hash = alloc_large_system_hash("MPTCP token",
356 sizeof(struct token_bucket),
358 20,/* one slot per 1MB of memory */
364 for (i = 0; i < token_mask + 1; ++i) {
365 INIT_HLIST_NULLS_HEAD(&token_hash[i].req_chain, i);
366 INIT_HLIST_NULLS_HEAD(&token_hash[i].msk_chain, i);
367 spin_lock_init(&token_hash[i].lock);
371 #if IS_MODULE(CONFIG_MPTCP_KUNIT_TESTS)
372 EXPORT_SYMBOL_GPL(mptcp_token_new_request);
373 EXPORT_SYMBOL_GPL(mptcp_token_new_connect);
374 EXPORT_SYMBOL_GPL(mptcp_token_accept);
375 EXPORT_SYMBOL_GPL(mptcp_token_destroy_request);
376 EXPORT_SYMBOL_GPL(mptcp_token_destroy);