1 // SPDX-License-Identifier: GPL-2.0-only
3 * Handshake request lifetime events
5 * Author: Chuck Lever <chuck.lever@oracle.com>
7 * Copyright (c) 2023, Oracle and/or its affiliates.
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet.h>
16 #include <linux/rhashtable.h>
19 #include <net/genetlink.h>
20 #include <net/netns/generic.h>
22 #include <kunit/visibility.h>
24 #include <uapi/linux/handshake.h>
25 #include "handshake.h"
27 #include <trace/events/handshake.h>
30 * We need both a handshake_req -> sock mapping, and a sock ->
31 * handshake_req mapping. Both are one-to-one.
33 * To avoid adding another pointer field to struct sock, net/handshake
34 * maintains a hash table, indexed by the memory address of @sock, to
35 * find the struct handshake_req outstanding for that socket. The
36 * reverse direction uses a simple pointer field in the handshake_req
40 static struct rhashtable handshake_rhashtbl ____cacheline_aligned_in_smp
;
42 static const struct rhashtable_params handshake_rhash_params
= {
43 .key_len
= sizeof_field(struct handshake_req
, hr_sk
),
44 .key_offset
= offsetof(struct handshake_req
, hr_sk
),
45 .head_offset
= offsetof(struct handshake_req
, hr_rhash
),
46 .automatic_shrinking
= true,
49 int handshake_req_hash_init(void)
51 return rhashtable_init(&handshake_rhashtbl
, &handshake_rhash_params
);
54 void handshake_req_hash_destroy(void)
56 rhashtable_destroy(&handshake_rhashtbl
);
59 struct handshake_req
*handshake_req_hash_lookup(struct sock
*sk
)
61 return rhashtable_lookup_fast(&handshake_rhashtbl
, &sk
,
62 handshake_rhash_params
);
64 EXPORT_SYMBOL_IF_KUNIT(handshake_req_hash_lookup
);
66 static bool handshake_req_hash_add(struct handshake_req
*req
)
70 ret
= rhashtable_lookup_insert_fast(&handshake_rhashtbl
,
72 handshake_rhash_params
);
76 static void handshake_req_destroy(struct handshake_req
*req
)
78 if (req
->hr_proto
->hp_destroy
)
79 req
->hr_proto
->hp_destroy(req
);
80 rhashtable_remove_fast(&handshake_rhashtbl
, &req
->hr_rhash
,
81 handshake_rhash_params
);
85 static void handshake_sk_destruct(struct sock
*sk
)
87 void (*sk_destruct
)(struct sock
*sk
);
88 struct handshake_req
*req
;
90 req
= handshake_req_hash_lookup(sk
);
94 trace_handshake_destruct(sock_net(sk
), req
, sk
);
95 sk_destruct
= req
->hr_odestruct
;
96 handshake_req_destroy(req
);
102 * handshake_req_alloc - Allocate a handshake request
103 * @proto: security protocol
104 * @flags: memory allocation flags
106 * Returns an initialized handshake_req or NULL.
108 struct handshake_req
*handshake_req_alloc(const struct handshake_proto
*proto
,
111 struct handshake_req
*req
;
115 if (proto
->hp_handler_class
<= HANDSHAKE_HANDLER_CLASS_NONE
)
117 if (proto
->hp_handler_class
>= HANDSHAKE_HANDLER_CLASS_MAX
)
119 if (!proto
->hp_accept
|| !proto
->hp_done
)
122 req
= kzalloc(struct_size(req
, hr_priv
, proto
->hp_privsize
), flags
);
126 INIT_LIST_HEAD(&req
->hr_list
);
127 req
->hr_proto
= proto
;
130 EXPORT_SYMBOL(handshake_req_alloc
);
133 * handshake_req_private - Get per-handshake private data
134 * @req: handshake arguments
137 void *handshake_req_private(struct handshake_req
*req
)
139 return (void *)&req
->hr_priv
;
141 EXPORT_SYMBOL(handshake_req_private
);
143 static bool __add_pending_locked(struct handshake_net
*hn
,
144 struct handshake_req
*req
)
146 if (WARN_ON_ONCE(!list_empty(&req
->hr_list
)))
149 list_add_tail(&req
->hr_list
, &hn
->hn_requests
);
153 static void __remove_pending_locked(struct handshake_net
*hn
,
154 struct handshake_req
*req
)
157 list_del_init(&req
->hr_list
);
161 * Returns %true if the request was found on @net's pending list,
164 * If @req was on a pending list, it has not yet been accepted.
166 static bool remove_pending(struct handshake_net
*hn
, struct handshake_req
*req
)
170 spin_lock(&hn
->hn_lock
);
171 if (!list_empty(&req
->hr_list
)) {
172 __remove_pending_locked(hn
, req
);
175 spin_unlock(&hn
->hn_lock
);
180 struct handshake_req
*handshake_req_next(struct handshake_net
*hn
, int class)
182 struct handshake_req
*req
, *pos
;
185 spin_lock(&hn
->hn_lock
);
186 list_for_each_entry(pos
, &hn
->hn_requests
, hr_list
) {
187 if (pos
->hr_proto
->hp_handler_class
!= class)
189 __remove_pending_locked(hn
, pos
);
193 spin_unlock(&hn
->hn_lock
);
197 EXPORT_SYMBOL_IF_KUNIT(handshake_req_next
);
200 * handshake_req_submit - Submit a handshake request
201 * @sock: open socket on which to perform the handshake
202 * @req: handshake arguments
203 * @flags: memory allocation flags
207 * %-EINVAL: Invalid argument
208 * %-EBUSY: A handshake is already under way for this socket
209 * %-ESRCH: No handshake agent is available
210 * %-EAGAIN: Too many pending handshake requests
211 * %-ENOMEM: Failed to allocate memory
212 * %-EMSGSIZE: Failed to construct notification message
213 * %-EOPNOTSUPP: Handshake module not initialized
215 * A zero return value from handshake_req_submit() means that
216 * exactly one subsequent completion callback is guaranteed.
218 * A negative return value from handshake_req_submit() means that
219 * no completion callback will be done and that @req has been
222 int handshake_req_submit(struct socket
*sock
, struct handshake_req
*req
,
225 struct handshake_net
*hn
;
229 if (!sock
|| !req
|| !sock
->file
) {
234 req
->hr_sk
= sock
->sk
;
239 req
->hr_odestruct
= req
->hr_sk
->sk_destruct
;
240 req
->hr_sk
->sk_destruct
= handshake_sk_destruct
;
243 net
= sock_net(req
->hr_sk
);
244 hn
= handshake_pernet(net
);
249 if (READ_ONCE(hn
->hn_pending
) >= hn
->hn_pending_max
)
252 spin_lock(&hn
->hn_lock
);
254 if (test_bit(HANDSHAKE_F_NET_DRAINING
, &hn
->hn_flags
))
257 if (!handshake_req_hash_add(req
))
259 if (!__add_pending_locked(hn
, req
))
261 spin_unlock(&hn
->hn_lock
);
263 ret
= handshake_genl_notify(net
, req
->hr_proto
, flags
);
265 trace_handshake_notify_err(net
, req
, req
->hr_sk
, ret
);
266 if (remove_pending(hn
, req
))
270 /* Prevent socket release while a handshake request is pending */
271 sock_hold(req
->hr_sk
);
273 trace_handshake_submit(net
, req
, req
->hr_sk
);
277 spin_unlock(&hn
->hn_lock
);
279 trace_handshake_submit_err(net
, req
, req
->hr_sk
, ret
);
280 handshake_req_destroy(req
);
283 EXPORT_SYMBOL(handshake_req_submit
);
285 void handshake_complete(struct handshake_req
*req
, unsigned int status
,
286 struct genl_info
*info
)
288 struct sock
*sk
= req
->hr_sk
;
289 struct net
*net
= sock_net(sk
);
291 if (!test_and_set_bit(HANDSHAKE_F_REQ_COMPLETED
, &req
->hr_flags
)) {
292 trace_handshake_complete(net
, req
, sk
, status
);
293 req
->hr_proto
->hp_done(req
, status
, info
);
295 /* Handshake request is no longer pending */
299 EXPORT_SYMBOL_IF_KUNIT(handshake_complete
);
302 * handshake_req_cancel - Cancel an in-progress handshake
303 * @sk: socket on which there is an ongoing handshake
305 * Request cancellation races with request completion. To determine
306 * who won, callers examine the return value from this function.
309 * %true - Uncompleted handshake request was canceled
310 * %false - Handshake request already completed or not found
312 bool handshake_req_cancel(struct sock
*sk
)
314 struct handshake_req
*req
;
315 struct handshake_net
*hn
;
319 req
= handshake_req_hash_lookup(sk
);
321 trace_handshake_cancel_none(net
, req
, sk
);
325 hn
= handshake_pernet(net
);
326 if (hn
&& remove_pending(hn
, req
)) {
327 /* Request hadn't been accepted */
330 if (test_and_set_bit(HANDSHAKE_F_REQ_COMPLETED
, &req
->hr_flags
)) {
331 /* Request already completed */
332 trace_handshake_cancel_busy(net
, req
, sk
);
337 trace_handshake_cancel(net
, req
, sk
);
339 /* Handshake request is no longer pending */
343 EXPORT_SYMBOL(handshake_req_cancel
);