xref: /linux/net/handshake/handshake.h (revision 3e20009988e2470063824c58b19d1c80816cc46d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Generic netlink handshake service
4  *
5  * Author: Chuck Lever <chuck.lever@oracle.com>
6  *
7  * Copyright (c) 2023, Oracle and/or its affiliates.
8  */
9 
10 #ifndef _INTERNAL_HANDSHAKE_H
11 #define _INTERNAL_HANDSHAKE_H
12 
13 /* Per-net namespace context */
14 struct handshake_net {
15 	spinlock_t		hn_lock;	/* protects next 3 fields */
16 	int			hn_pending;
17 	int			hn_pending_max;
18 	struct list_head	hn_requests;
19 
20 	unsigned long		hn_flags;
21 };
22 
23 enum hn_flags_bits {
24 	HANDSHAKE_F_NET_DRAINING,
25 };
26 
27 struct file;
28 struct handshake_proto;
29 
30 /* One handshake request */
31 struct handshake_req {
32 	struct list_head		hr_list;
33 	struct rhash_head		hr_rhash;
34 	unsigned long			hr_flags;
35 	const struct handshake_proto	*hr_proto;
36 	struct file			*hr_file;
37 	struct sock			*hr_sk;
38 	void				(*hr_odestruct)(struct sock *sk);
39 
40 	/* Always the last field */
41 	char				hr_priv[];
42 };
43 
44 enum hr_flags_bits {
45 	HANDSHAKE_F_REQ_COMPLETED,
46 	HANDSHAKE_F_REQ_SESSION,
47 };
48 
49 struct genl_info;
50 
51 /* Invariants for all handshake requests for one transport layer
52  * security protocol
53  */
54 struct handshake_proto {
55 	int			hp_handler_class;
56 	size_t			hp_privsize;
57 	unsigned long		hp_flags;
58 
59 	int			(*hp_accept)(struct handshake_req *req,
60 					     struct genl_info *info, int fd);
61 	void			(*hp_done)(struct handshake_req *req,
62 					   int status,
63 					   struct genl_info *info);
64 	void			(*hp_destroy)(struct handshake_req *req);
65 };
66 
67 enum hp_flags_bits {
68 	HANDSHAKE_F_PROTO_NOTIFY,
69 };
70 
71 /* alert.c */
72 int tls_alert_send(struct socket *sock, u8 level, u8 description);
73 
74 /* netlink.c */
75 int handshake_genl_notify(struct net *net, const struct handshake_proto *proto,
76 			  gfp_t flags);
77 struct nlmsghdr *handshake_genl_put(struct sk_buff *msg,
78 				    struct genl_info *info);
79 struct handshake_net *handshake_pernet(struct net *net);
80 
81 /* request.c */
82 struct handshake_req *handshake_req_alloc(const struct handshake_proto *proto,
83 					  gfp_t flags);
84 int handshake_req_hash_init(void);
85 void handshake_req_hash_destroy(void);
86 void *handshake_req_private(struct handshake_req *req);
87 struct handshake_req *handshake_req_hash_lookup(struct sock *sk);
88 struct handshake_req *handshake_req_next(struct handshake_net *hn, int class);
89 int handshake_req_submit(struct socket *sock, struct handshake_req *req,
90 			 gfp_t flags);
91 void handshake_complete(struct handshake_req *req, int status,
92 			struct genl_info *info);
93 bool handshake_req_cancel(struct sock *sk);
94 
95 #endif /* _INTERNAL_HANDSHAKE_H */
96