1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic netlink handshake service
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>
18 #include <net/genetlink.h>
19 #include <net/netns/generic.h>
21 #include <kunit/visibility.h>
23 #include <uapi/linux/handshake.h>
24 #include "handshake.h"
27 #include <trace/events/handshake.h>
30 * handshake_genl_notify - Notify handlers that a request is waiting
31 * @net: target network namespace
32 * @proto: handshake protocol
33 * @flags: memory allocation control flags
35 * Returns zero on success or a negative errno if notification failed.
37 int handshake_genl_notify(struct net *net, const struct handshake_proto *proto,
43 /* Disable notifications during unit testing */
44 if (!test_bit(HANDSHAKE_F_PROTO_NOTIFY, &proto->hp_flags))
47 if (!genl_has_listeners(&handshake_nl_family, net,
48 proto->hp_handler_class))
51 msg = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
55 hdr = genlmsg_put(msg, 0, 0, &handshake_nl_family, 0,
60 if (nla_put_u32(msg, HANDSHAKE_A_ACCEPT_HANDLER_CLASS,
61 proto->hp_handler_class) < 0) {
62 genlmsg_cancel(msg, hdr);
66 genlmsg_end(msg, hdr);
67 return genlmsg_multicast_netns(&handshake_nl_family, net, msg,
68 0, proto->hp_handler_class, flags);
76 * handshake_genl_put - Create a generic netlink message header
77 * @msg: buffer in which to create the header
78 * @info: generic netlink message context
80 * Returns a ready-to-use header, or NULL.
82 struct nlmsghdr *handshake_genl_put(struct sk_buff *msg,
83 struct genl_info *info)
85 return genlmsg_put(msg, info->snd_portid, info->snd_seq,
86 &handshake_nl_family, 0, info->genlhdr->cmd);
88 EXPORT_SYMBOL(handshake_genl_put);
91 * dup() a kernel socket for use as a user space file descriptor
92 * in the current process. The kernel socket must have an
93 * instatiated struct file.
95 * Implicit argument: "current()"
97 static int handshake_dup(struct socket *sock)
105 file = get_file(sock->file);
106 newfd = get_unused_fd_flags(O_CLOEXEC);
112 fd_install(newfd, file);
116 int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info)
118 struct net *net = sock_net(skb->sk);
119 struct handshake_net *hn = handshake_pernet(net);
120 struct handshake_req *req = NULL;
129 if (GENL_REQ_ATTR_CHECK(info, HANDSHAKE_A_ACCEPT_HANDLER_CLASS))
131 class = nla_get_u32(info->attrs[HANDSHAKE_A_ACCEPT_HANDLER_CLASS]);
134 req = handshake_req_next(hn, class);
138 sock = req->hr_sk->sk_socket;
139 fd = handshake_dup(sock);
144 err = req->hr_proto->hp_accept(req, info, fd);
148 trace_handshake_cmd_accept(net, req, req->hr_sk, fd);
152 handshake_complete(req, -EIO, NULL);
155 trace_handshake_cmd_accept_err(net, req, NULL, err);
159 int handshake_nl_done_doit(struct sk_buff *skb, struct genl_info *info)
161 struct net *net = sock_net(skb->sk);
162 struct socket *sock = NULL;
163 struct handshake_req *req;
166 if (GENL_REQ_ATTR_CHECK(info, HANDSHAKE_A_DONE_SOCKFD))
168 fd = nla_get_u32(info->attrs[HANDSHAKE_A_DONE_SOCKFD]);
171 sock = sockfd_lookup(fd, &err);
177 req = handshake_req_hash_lookup(sock->sk);
184 trace_handshake_cmd_done(net, req, sock->sk, fd);
187 if (info->attrs[HANDSHAKE_A_DONE_STATUS])
188 status = nla_get_u32(info->attrs[HANDSHAKE_A_DONE_STATUS]);
190 handshake_complete(req, status, info);
195 trace_handshake_cmd_done_err(net, req, sock->sk, err);
199 static unsigned int handshake_net_id;
201 static int __net_init handshake_net_init(struct net *net)
203 struct handshake_net *hn = net_generic(net, handshake_net_id);
208 * Arbitrary limit to prevent handshakes that do not make
209 * progress from clogging up the system. The cap scales up
210 * with the amount of physical memory on the system.
213 tmp = si.totalram / (25 * si.mem_unit);
214 hn->hn_pending_max = clamp(tmp, 3UL, 50UL);
216 spin_lock_init(&hn->hn_lock);
219 INIT_LIST_HEAD(&hn->hn_requests);
223 static void __net_exit handshake_net_exit(struct net *net)
225 struct handshake_net *hn = net_generic(net, handshake_net_id);
226 struct handshake_req *req;
230 * Drain the net's pending list. Requests that have been
231 * accepted and are in progress will be destroyed when
232 * the socket is closed.
234 spin_lock(&hn->hn_lock);
235 set_bit(HANDSHAKE_F_NET_DRAINING, &hn->hn_flags);
236 list_splice_init(&requests, &hn->hn_requests);
237 spin_unlock(&hn->hn_lock);
239 while (!list_empty(&requests)) {
240 req = list_first_entry(&requests, struct handshake_req, hr_list);
241 list_del(&req->hr_list);
244 * Requests on this list have not yet been
245 * accepted, so they do not have an fd to put.
248 handshake_complete(req, -ETIMEDOUT, NULL);
252 static struct pernet_operations handshake_genl_net_ops = {
253 .init = handshake_net_init,
254 .exit = handshake_net_exit,
255 .id = &handshake_net_id,
256 .size = sizeof(struct handshake_net),
260 * handshake_pernet - Get the handshake private per-net structure
261 * @net: network namespace
263 * Returns a pointer to the net's private per-net structure for the
264 * handshake module, or NULL if handshake_init() failed.
266 struct handshake_net *handshake_pernet(struct net *net)
268 return handshake_net_id ?
269 net_generic(net, handshake_net_id) : NULL;
271 EXPORT_SYMBOL_IF_KUNIT(handshake_pernet);
273 static int __init handshake_init(void)
277 ret = handshake_req_hash_init();
279 pr_warn("handshake: hash initialization failed (%d)\n", ret);
283 ret = genl_register_family(&handshake_nl_family);
285 pr_warn("handshake: netlink registration failed (%d)\n", ret);
286 handshake_req_hash_destroy();
291 * ORDER: register_pernet_subsys must be done last.
293 * If initialization does not make it past pernet_subsys
294 * registration, then handshake_net_id will remain 0. That
295 * shunts the handshake consumer API to return ENOTSUPP
296 * to prevent it from dereferencing something that hasn't
299 ret = register_pernet_subsys(&handshake_genl_net_ops);
301 pr_warn("handshake: pernet registration failed (%d)\n", ret);
302 genl_unregister_family(&handshake_nl_family);
303 handshake_req_hash_destroy();
309 static void __exit handshake_exit(void)
311 unregister_pernet_subsys(&handshake_genl_net_ops);
312 handshake_net_id = 0;
314 handshake_req_hash_destroy();
315 genl_unregister_family(&handshake_nl_family);
318 module_init(handshake_init);
319 module_exit(handshake_exit);