token.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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é <[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. #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. * @sk: 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 *sk)
  135. {
  136. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  137. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  138. int retries = MPTCP_TOKEN_MAX_RETRIES;
  139. struct token_bucket *bucket;
  140. again:
  141. mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token,
  142. &subflow->idsn);
  143. bucket = token_bucket(subflow->token);
  144. spin_lock_bh(&bucket->lock);
  145. if (__token_bucket_busy(bucket, subflow->token)) {
  146. spin_unlock_bh(&bucket->lock);
  147. if (!--retries)
  148. return -EBUSY;
  149. goto again;
  150. }
  151. pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
  152. sk, subflow->local_key, subflow->token, subflow->idsn);
  153. WRITE_ONCE(msk->token, subflow->token);
  154. __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
  155. bucket->chain_len++;
  156. spin_unlock_bh(&bucket->lock);
  157. return 0;
  158. }
  159. /**
  160. * mptcp_token_accept - replace a req sk with full sock in token hash
  161. * @req: the request socket to be removed
  162. * @msk: the just cloned socket linked to the new connection
  163. *
  164. * Called when a SYN packet creates a new logical connection, i.e.
  165. * is not a join request.
  166. */
  167. void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
  168. struct mptcp_sock *msk)
  169. {
  170. struct mptcp_subflow_request_sock *pos;
  171. struct token_bucket *bucket;
  172. bucket = token_bucket(req->token);
  173. spin_lock_bh(&bucket->lock);
  174. /* pedantic lookup check for the moved token */
  175. pos = __token_lookup_req(bucket, req->token);
  176. if (!WARN_ON_ONCE(pos != req))
  177. hlist_nulls_del_init_rcu(&req->token_node);
  178. __sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
  179. spin_unlock_bh(&bucket->lock);
  180. }
  181. bool mptcp_token_exists(u32 token)
  182. {
  183. struct hlist_nulls_node *pos;
  184. struct token_bucket *bucket;
  185. struct mptcp_sock *msk;
  186. struct sock *sk;
  187. rcu_read_lock();
  188. bucket = token_bucket(token);
  189. again:
  190. sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
  191. msk = mptcp_sk(sk);
  192. if (READ_ONCE(msk->token) == token)
  193. goto found;
  194. }
  195. if (get_nulls_value(pos) != (token & token_mask))
  196. goto again;
  197. rcu_read_unlock();
  198. return false;
  199. found:
  200. rcu_read_unlock();
  201. return true;
  202. }
  203. /**
  204. * mptcp_token_get_sock - retrieve mptcp connection sock using its token
  205. * @net: restrict to this namespace
  206. * @token: token of the mptcp connection to retrieve
  207. *
  208. * This function returns the mptcp connection structure with the given token.
  209. * A reference count on the mptcp socket returned is taken.
  210. *
  211. * returns NULL if no connection with the given token value exists.
  212. */
  213. struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token)
  214. {
  215. struct hlist_nulls_node *pos;
  216. struct token_bucket *bucket;
  217. struct mptcp_sock *msk;
  218. struct sock *sk;
  219. rcu_read_lock();
  220. bucket = token_bucket(token);
  221. again:
  222. sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
  223. msk = mptcp_sk(sk);
  224. if (READ_ONCE(msk->token) != token ||
  225. !net_eq(sock_net(sk), net))
  226. continue;
  227. if (!refcount_inc_not_zero(&sk->sk_refcnt))
  228. goto not_found;
  229. if (READ_ONCE(msk->token) != token ||
  230. !net_eq(sock_net(sk), net)) {
  231. sock_put(sk);
  232. goto again;
  233. }
  234. goto found;
  235. }
  236. if (get_nulls_value(pos) != (token & token_mask))
  237. goto again;
  238. not_found:
  239. msk = NULL;
  240. found:
  241. rcu_read_unlock();
  242. return msk;
  243. }
  244. EXPORT_SYMBOL_GPL(mptcp_token_get_sock);
  245. /**
  246. * mptcp_token_iter_next - iterate over the token container from given pos
  247. * @net: namespace to be iterated
  248. * @s_slot: start slot number
  249. * @s_num: start number inside the given lock
  250. *
  251. * This function returns the first mptcp connection structure found inside the
  252. * token container starting from the specified position, or NULL.
  253. *
  254. * On successful iteration, the iterator is move to the next position and the
  255. * the acquires a reference to the returned socket.
  256. */
  257. struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
  258. long *s_num)
  259. {
  260. struct mptcp_sock *ret = NULL;
  261. struct hlist_nulls_node *pos;
  262. int slot, num = 0;
  263. for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
  264. struct token_bucket *bucket = &token_hash[slot];
  265. struct sock *sk;
  266. num = 0;
  267. if (hlist_nulls_empty(&bucket->msk_chain))
  268. continue;
  269. rcu_read_lock();
  270. sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
  271. ++num;
  272. if (!net_eq(sock_net(sk), net))
  273. continue;
  274. if (num <= *s_num)
  275. continue;
  276. if (!refcount_inc_not_zero(&sk->sk_refcnt))
  277. continue;
  278. if (!net_eq(sock_net(sk), net)) {
  279. sock_put(sk);
  280. continue;
  281. }
  282. ret = mptcp_sk(sk);
  283. rcu_read_unlock();
  284. goto out;
  285. }
  286. rcu_read_unlock();
  287. }
  288. out:
  289. *s_slot = slot;
  290. *s_num = num;
  291. return ret;
  292. }
  293. EXPORT_SYMBOL_GPL(mptcp_token_iter_next);
  294. /**
  295. * mptcp_token_destroy_request - remove mptcp connection/token
  296. * @req: mptcp request socket dropping the token
  297. *
  298. * Remove the token associated to @req.
  299. */
  300. void mptcp_token_destroy_request(struct request_sock *req)
  301. {
  302. struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
  303. struct mptcp_subflow_request_sock *pos;
  304. struct token_bucket *bucket;
  305. if (hlist_nulls_unhashed(&subflow_req->token_node))
  306. return;
  307. bucket = token_bucket(subflow_req->token);
  308. spin_lock_bh(&bucket->lock);
  309. pos = __token_lookup_req(bucket, subflow_req->token);
  310. if (!WARN_ON_ONCE(pos != subflow_req)) {
  311. hlist_nulls_del_init_rcu(&pos->token_node);
  312. bucket->chain_len--;
  313. }
  314. spin_unlock_bh(&bucket->lock);
  315. }
  316. /**
  317. * mptcp_token_destroy - remove mptcp connection/token
  318. * @msk: mptcp connection dropping the token
  319. *
  320. * Remove the token associated to @msk
  321. */
  322. void mptcp_token_destroy(struct mptcp_sock *msk)
  323. {
  324. struct token_bucket *bucket;
  325. struct mptcp_sock *pos;
  326. if (sk_unhashed((struct sock *)msk))
  327. return;
  328. bucket = token_bucket(msk->token);
  329. spin_lock_bh(&bucket->lock);
  330. pos = __token_lookup_msk(bucket, msk->token);
  331. if (!WARN_ON_ONCE(pos != msk)) {
  332. __sk_nulls_del_node_init_rcu((struct sock *)pos);
  333. bucket->chain_len--;
  334. }
  335. spin_unlock_bh(&bucket->lock);
  336. WRITE_ONCE(msk->token, 0);
  337. }
  338. void __init mptcp_token_init(void)
  339. {
  340. int i;
  341. token_hash = alloc_large_system_hash("MPTCP token",
  342. sizeof(struct token_bucket),
  343. 0,
  344. 20,/* one slot per 1MB of memory */
  345. HASH_ZERO,
  346. NULL,
  347. &token_mask,
  348. 0,
  349. 64 * 1024);
  350. for (i = 0; i < token_mask + 1; ++i) {
  351. INIT_HLIST_NULLS_HEAD(&token_hash[i].req_chain, i);
  352. INIT_HLIST_NULLS_HEAD(&token_hash[i].msk_chain, i);
  353. spin_lock_init(&token_hash[i].lock);
  354. }
  355. }
  356. #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST)
  357. EXPORT_SYMBOL_GPL(mptcp_token_new_request);
  358. EXPORT_SYMBOL_GPL(mptcp_token_new_connect);
  359. EXPORT_SYMBOL_GPL(mptcp_token_accept);
  360. EXPORT_SYMBOL_GPL(mptcp_token_destroy_request);
  361. EXPORT_SYMBOL_GPL(mptcp_token_destroy);
  362. #endif