request.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Handshake request lifetime events
  4. *
  5. * Author: Chuck Lever <chuck.lever@oracle.com>
  6. *
  7. * Copyright (c) 2023, Oracle and/or its affiliates.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/socket.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/inet.h>
  15. #include <linux/rhashtable.h>
  16. #include <net/sock.h>
  17. #include <net/genetlink.h>
  18. #include <net/netns/generic.h>
  19. #include <kunit/visibility.h>
  20. #include <uapi/linux/handshake.h>
  21. #include "handshake.h"
  22. #include <trace/events/handshake.h>
  23. /*
  24. * We need both a handshake_req -> sock mapping, and a sock ->
  25. * handshake_req mapping. Both are one-to-one.
  26. *
  27. * To avoid adding another pointer field to struct sock, net/handshake
  28. * maintains a hash table, indexed by the memory address of @sock, to
  29. * find the struct handshake_req outstanding for that socket. The
  30. * reverse direction uses a simple pointer field in the handshake_req
  31. * struct.
  32. */
  33. static struct rhashtable handshake_rhashtbl ____cacheline_aligned_in_smp;
  34. static const struct rhashtable_params handshake_rhash_params = {
  35. .key_len = sizeof_field(struct handshake_req, hr_sk),
  36. .key_offset = offsetof(struct handshake_req, hr_sk),
  37. .head_offset = offsetof(struct handshake_req, hr_rhash),
  38. .automatic_shrinking = true,
  39. };
  40. int handshake_req_hash_init(void)
  41. {
  42. return rhashtable_init(&handshake_rhashtbl, &handshake_rhash_params);
  43. }
  44. void handshake_req_hash_destroy(void)
  45. {
  46. rhashtable_destroy(&handshake_rhashtbl);
  47. }
  48. struct handshake_req *handshake_req_hash_lookup(struct sock *sk)
  49. {
  50. return rhashtable_lookup_fast(&handshake_rhashtbl, &sk,
  51. handshake_rhash_params);
  52. }
  53. EXPORT_SYMBOL_IF_KUNIT(handshake_req_hash_lookup);
  54. static bool handshake_req_hash_add(struct handshake_req *req)
  55. {
  56. int ret;
  57. ret = rhashtable_lookup_insert_fast(&handshake_rhashtbl,
  58. &req->hr_rhash,
  59. handshake_rhash_params);
  60. return ret == 0;
  61. }
  62. static void handshake_req_destroy(struct handshake_req *req)
  63. {
  64. if (req->hr_proto->hp_destroy)
  65. req->hr_proto->hp_destroy(req);
  66. rhashtable_remove_fast(&handshake_rhashtbl, &req->hr_rhash,
  67. handshake_rhash_params);
  68. kfree(req);
  69. }
  70. static void handshake_sk_destruct(struct sock *sk)
  71. {
  72. void (*sk_destruct)(struct sock *sk);
  73. struct handshake_req *req;
  74. req = handshake_req_hash_lookup(sk);
  75. if (!req)
  76. return;
  77. trace_handshake_destruct(sock_net(sk), req, sk);
  78. sk_destruct = req->hr_odestruct;
  79. handshake_req_destroy(req);
  80. if (sk_destruct)
  81. sk_destruct(sk);
  82. }
  83. /**
  84. * handshake_req_alloc - Allocate a handshake request
  85. * @proto: security protocol
  86. * @flags: memory allocation flags
  87. *
  88. * Returns an initialized handshake_req or NULL.
  89. */
  90. struct handshake_req *handshake_req_alloc(const struct handshake_proto *proto,
  91. gfp_t flags)
  92. {
  93. struct handshake_req *req;
  94. if (!proto)
  95. return NULL;
  96. if (proto->hp_handler_class <= HANDSHAKE_HANDLER_CLASS_NONE)
  97. return NULL;
  98. if (proto->hp_handler_class >= HANDSHAKE_HANDLER_CLASS_MAX)
  99. return NULL;
  100. if (!proto->hp_accept || !proto->hp_done)
  101. return NULL;
  102. req = kzalloc_flex(*req, hr_priv, proto->hp_privsize, flags);
  103. if (!req)
  104. return NULL;
  105. INIT_LIST_HEAD(&req->hr_list);
  106. req->hr_proto = proto;
  107. return req;
  108. }
  109. EXPORT_SYMBOL(handshake_req_alloc);
  110. /**
  111. * handshake_req_private - Get per-handshake private data
  112. * @req: handshake arguments
  113. *
  114. */
  115. void *handshake_req_private(struct handshake_req *req)
  116. {
  117. return (void *)&req->hr_priv;
  118. }
  119. EXPORT_SYMBOL(handshake_req_private);
  120. static bool __add_pending_locked(struct handshake_net *hn,
  121. struct handshake_req *req)
  122. {
  123. if (WARN_ON_ONCE(!list_empty(&req->hr_list)))
  124. return false;
  125. hn->hn_pending++;
  126. list_add_tail(&req->hr_list, &hn->hn_requests);
  127. return true;
  128. }
  129. static void __remove_pending_locked(struct handshake_net *hn,
  130. struct handshake_req *req)
  131. {
  132. hn->hn_pending--;
  133. list_del_init(&req->hr_list);
  134. }
  135. /*
  136. * Returns %true if the request was found on @net's pending list,
  137. * otherwise %false.
  138. *
  139. * If @req was on a pending list, it has not yet been accepted.
  140. */
  141. static bool remove_pending(struct handshake_net *hn, struct handshake_req *req)
  142. {
  143. bool ret = false;
  144. spin_lock(&hn->hn_lock);
  145. if (!list_empty(&req->hr_list)) {
  146. __remove_pending_locked(hn, req);
  147. ret = true;
  148. }
  149. spin_unlock(&hn->hn_lock);
  150. return ret;
  151. }
  152. struct handshake_req *handshake_req_next(struct handshake_net *hn, int class)
  153. {
  154. struct handshake_req *req, *pos;
  155. req = NULL;
  156. spin_lock(&hn->hn_lock);
  157. list_for_each_entry(pos, &hn->hn_requests, hr_list) {
  158. if (pos->hr_proto->hp_handler_class != class)
  159. continue;
  160. __remove_pending_locked(hn, pos);
  161. req = pos;
  162. break;
  163. }
  164. spin_unlock(&hn->hn_lock);
  165. return req;
  166. }
  167. EXPORT_SYMBOL_IF_KUNIT(handshake_req_next);
  168. /**
  169. * handshake_req_submit - Submit a handshake request
  170. * @sock: open socket on which to perform the handshake
  171. * @req: handshake arguments
  172. * @flags: memory allocation flags
  173. *
  174. * Return values:
  175. * %0: Request queued
  176. * %-EINVAL: Invalid argument
  177. * %-EBUSY: A handshake is already under way for this socket
  178. * %-ESRCH: No handshake agent is available
  179. * %-EAGAIN: Too many pending handshake requests
  180. * %-ENOMEM: Failed to allocate memory
  181. * %-EMSGSIZE: Failed to construct notification message
  182. * %-EOPNOTSUPP: Handshake module not initialized
  183. *
  184. * A zero return value from handshake_req_submit() means that
  185. * exactly one subsequent completion callback is guaranteed.
  186. *
  187. * A negative return value from handshake_req_submit() means that
  188. * no completion callback will be done and that @req has been
  189. * destroyed.
  190. */
  191. int handshake_req_submit(struct socket *sock, struct handshake_req *req,
  192. gfp_t flags)
  193. {
  194. struct handshake_net *hn;
  195. struct net *net;
  196. int ret;
  197. if (!sock || !req || !sock->file) {
  198. kfree(req);
  199. return -EINVAL;
  200. }
  201. req->hr_sk = sock->sk;
  202. if (!req->hr_sk) {
  203. kfree(req);
  204. return -EINVAL;
  205. }
  206. req->hr_odestruct = req->hr_sk->sk_destruct;
  207. req->hr_sk->sk_destruct = handshake_sk_destruct;
  208. ret = -EOPNOTSUPP;
  209. net = sock_net(req->hr_sk);
  210. hn = handshake_pernet(net);
  211. if (!hn)
  212. goto out_err;
  213. ret = -EAGAIN;
  214. if (READ_ONCE(hn->hn_pending) >= hn->hn_pending_max)
  215. goto out_err;
  216. spin_lock(&hn->hn_lock);
  217. ret = -EOPNOTSUPP;
  218. if (test_bit(HANDSHAKE_F_NET_DRAINING, &hn->hn_flags))
  219. goto out_unlock;
  220. ret = -EBUSY;
  221. if (!handshake_req_hash_add(req))
  222. goto out_unlock;
  223. if (!__add_pending_locked(hn, req))
  224. goto out_unlock;
  225. spin_unlock(&hn->hn_lock);
  226. ret = handshake_genl_notify(net, req->hr_proto, flags);
  227. if (ret) {
  228. trace_handshake_notify_err(net, req, req->hr_sk, ret);
  229. if (remove_pending(hn, req))
  230. goto out_err;
  231. }
  232. /* Prevent socket release while a handshake request is pending */
  233. sock_hold(req->hr_sk);
  234. trace_handshake_submit(net, req, req->hr_sk);
  235. return 0;
  236. out_unlock:
  237. spin_unlock(&hn->hn_lock);
  238. out_err:
  239. /* Restore original destructor so socket teardown still runs on failure */
  240. req->hr_sk->sk_destruct = req->hr_odestruct;
  241. trace_handshake_submit_err(net, req, req->hr_sk, ret);
  242. handshake_req_destroy(req);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(handshake_req_submit);
  246. void handshake_complete(struct handshake_req *req, unsigned int status,
  247. struct genl_info *info)
  248. {
  249. struct sock *sk = req->hr_sk;
  250. struct net *net = sock_net(sk);
  251. if (!test_and_set_bit(HANDSHAKE_F_REQ_COMPLETED, &req->hr_flags)) {
  252. trace_handshake_complete(net, req, sk, status);
  253. req->hr_proto->hp_done(req, status, info);
  254. /* Handshake request is no longer pending */
  255. sock_put(sk);
  256. }
  257. }
  258. EXPORT_SYMBOL_IF_KUNIT(handshake_complete);
  259. /**
  260. * handshake_req_cancel - Cancel an in-progress handshake
  261. * @sk: socket on which there is an ongoing handshake
  262. *
  263. * Request cancellation races with request completion. To determine
  264. * who won, callers examine the return value from this function.
  265. *
  266. * Return values:
  267. * %true - Uncompleted handshake request was canceled
  268. * %false - Handshake request already completed or not found
  269. */
  270. bool handshake_req_cancel(struct sock *sk)
  271. {
  272. struct handshake_req *req;
  273. struct handshake_net *hn;
  274. struct net *net;
  275. net = sock_net(sk);
  276. req = handshake_req_hash_lookup(sk);
  277. if (!req) {
  278. trace_handshake_cancel_none(net, req, sk);
  279. return false;
  280. }
  281. hn = handshake_pernet(net);
  282. if (hn && remove_pending(hn, req)) {
  283. /* Request hadn't been accepted - mark cancelled */
  284. if (test_and_set_bit(HANDSHAKE_F_REQ_COMPLETED, &req->hr_flags)) {
  285. trace_handshake_cancel_busy(net, req, sk);
  286. return false;
  287. }
  288. goto out_true;
  289. }
  290. if (test_and_set_bit(HANDSHAKE_F_REQ_COMPLETED, &req->hr_flags)) {
  291. /* Request already completed */
  292. trace_handshake_cancel_busy(net, req, sk);
  293. return false;
  294. }
  295. out_true:
  296. trace_handshake_cancel(net, req, sk);
  297. /* Handshake request is no longer pending */
  298. sock_put(sk);
  299. return true;
  300. }
  301. EXPORT_SYMBOL(handshake_req_cancel);