xref: /linux/net/mptcp/token.c (revision 93a3545d812ae7cfe4426374e00a7d8f64ac02e0)
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 EXPORT_SYMBOL_GPL(mptcp_token_get_sock);
242 
243 /**
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
248  *
249  * This function returns the first mptcp connection structure found inside the
250  * token container starting from the specified position, or NULL.
251  *
252  * On successful iteration, the iterator is move to the next position and the
253  * the acquires a reference to the returned socket.
254  */
255 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
256 					 long *s_num)
257 {
258 	struct mptcp_sock *ret = NULL;
259 	struct hlist_nulls_node *pos;
260 	int slot, num;
261 
262 	for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
263 		struct token_bucket *bucket = &token_hash[slot];
264 		struct sock *sk;
265 
266 		num = 0;
267 
268 		if (hlist_nulls_empty(&bucket->msk_chain))
269 			continue;
270 
271 		rcu_read_lock();
272 		sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
273 			++num;
274 			if (!net_eq(sock_net(sk), net))
275 				continue;
276 
277 			if (num <= *s_num)
278 				continue;
279 
280 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
281 				continue;
282 
283 			if (!net_eq(sock_net(sk), net)) {
284 				sock_put(sk);
285 				continue;
286 			}
287 
288 			ret = mptcp_sk(sk);
289 			rcu_read_unlock();
290 			goto out;
291 		}
292 		rcu_read_unlock();
293 	}
294 
295 out:
296 	*s_slot = slot;
297 	*s_num = num;
298 	return ret;
299 }
300 EXPORT_SYMBOL_GPL(mptcp_token_iter_next);
301 
302 /**
303  * mptcp_token_destroy_request - remove mptcp connection/token
304  * @req: mptcp request socket dropping the token
305  *
306  * Remove the token associated to @req.
307  */
308 void mptcp_token_destroy_request(struct request_sock *req)
309 {
310 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
311 	struct mptcp_subflow_request_sock *pos;
312 	struct token_bucket *bucket;
313 
314 	if (hlist_nulls_unhashed(&subflow_req->token_node))
315 		return;
316 
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);
322 		bucket->chain_len--;
323 	}
324 	spin_unlock_bh(&bucket->lock);
325 }
326 
327 /**
328  * mptcp_token_destroy - remove mptcp connection/token
329  * @msk: mptcp connection dropping the token
330  *
331  * Remove the token associated to @msk
332  */
333 void mptcp_token_destroy(struct mptcp_sock *msk)
334 {
335 	struct token_bucket *bucket;
336 	struct mptcp_sock *pos;
337 
338 	if (sk_unhashed((struct sock *)msk))
339 		return;
340 
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);
346 		bucket->chain_len--;
347 	}
348 	spin_unlock_bh(&bucket->lock);
349 }
350 
351 void __init mptcp_token_init(void)
352 {
353 	int i;
354 
355 	token_hash = alloc_large_system_hash("MPTCP token",
356 					     sizeof(struct token_bucket),
357 					     0,
358 					     20,/* one slot per 1MB of memory */
359 					     0,
360 					     NULL,
361 					     &token_mask,
362 					     0,
363 					     64 * 1024);
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);
368 	}
369 }
370 
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);
377 #endif
378