token.c 11 KB

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