xref: /freebsd/sys/netlink/netlink_var.h (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Ng Peng Nam Sean
5  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef _NETLINK_NETLINK_VAR_H_
29 #define _NETLINK_NETLINK_VAR_H_
30 
31 #ifdef _KERNEL
32 
33 #include <sys/ck.h>
34 #include <sys/epoch.h>
35 #include <sys/sysctl.h>
36 #include <sys/taskqueue.h>
37 #include <net/vnet.h>
38 
39 #define	NLSNDQ  65536 /* Default socket sendspace */
40 #define	NLRCVQ	65536 /* Default socket recvspace */
41 
42 struct ucred;
43 
44 struct nl_io_queue {
45 	STAILQ_HEAD(, mbuf)	head;
46 	int			length;
47 	int			hiwat;
48 };
49 
50 #define	NLP_MAX_GROUPS		128
51 
52 struct nlpcb {
53         struct socket           *nl_socket;
54 	uint64_t	        nl_groups[NLP_MAX_GROUPS / 64];
55 	uint32_t                nl_port;
56 	uint32_t	        nl_flags;
57 	uint32_t	        nl_process_id;
58         int                     nl_proto;
59         bool			nl_active;
60 	bool			nl_bound;
61         bool			nl_task_pending;
62 	bool			nl_tx_blocked; /* No new requests accepted */
63 	bool			nl_linux; /* true if running under compat */
64 	bool			nl_unconstrained_vnet; /* true if running under VNET jail (or without jail) */
65 	bool			nl_need_thread_setup;
66 	struct nl_io_queue	rx_queue;
67 	struct nl_io_queue	tx_queue;
68 	struct taskqueue	*nl_taskqueue;
69 	struct task		nl_task;
70 	struct ucred		*nl_cred; /* Copy of nl_socket->so_cred */
71 	uint64_t		nl_dropped_bytes;
72 	uint64_t		nl_dropped_messages;
73         CK_LIST_ENTRY(nlpcb)    nl_next;
74         CK_LIST_ENTRY(nlpcb)    nl_port_next;
75 	volatile u_int		nl_refcount;
76 	struct mtx		nl_lock;
77 	struct epoch_context	nl_epoch_ctx;
78 };
79 #define sotonlpcb(so)       ((struct nlpcb *)(so)->so_pcb)
80 
81 #define	NLP_LOCK_INIT(_nlp)	mtx_init(&((_nlp)->nl_lock), "nlp mtx", NULL, MTX_DEF)
82 #define	NLP_LOCK_DESTROY(_nlp)	mtx_destroy(&((_nlp)->nl_lock))
83 #define	NLP_LOCK(_nlp)		mtx_lock(&((_nlp)->nl_lock))
84 #define	NLP_UNLOCK(_nlp)	mtx_unlock(&((_nlp)->nl_lock))
85 
86 #define	ALIGNED_NL_SZ(_data)	roundup2((((struct nlmsghdr *)(_data))->nlmsg_len), 16)
87 
88 /* nl_flags */
89 #define NLF_CAP_ACK             0x01 /* Do not send message body with errmsg */
90 #define NLF_EXT_ACK             0x02 /* Allow including extended TLVs in ack */
91 #define	NLF_STRICT		0x04 /* Perform strict header checks */
92 #define	NLF_MSG_INFO		0x08 /* Send caller info along with the notifications */
93 
94 SYSCTL_DECL(_net_netlink);
95 SYSCTL_DECL(_net_netlink_debug);
96 
97 struct nl_io {
98 	struct callout				callout;
99 	struct mbuf				*head;
100 	struct mbuf 				*last;
101 	int64_t					length;
102 };
103 
104 struct nl_control {
105 	CK_LIST_HEAD(nl_pid_head, nlpcb)	ctl_port_head;
106 	CK_LIST_HEAD(nlpcb_head, nlpcb)		ctl_pcb_head;
107 	CK_LIST_ENTRY(nl_control)		ctl_next;
108 	struct nl_io				ctl_io;
109 	struct rmlock				ctl_lock;
110 };
111 VNET_DECLARE(struct nl_control *, nl_ctl);
112 #define	V_nl_ctl	VNET(nl_ctl)
113 
114 
115 struct sockaddr_nl;
116 struct sockaddr;
117 struct nlmsghdr;
118 
119 /* netlink_module.c */
120 struct nl_control *vnet_nl_ctl_init(void);
121 
122 int nl_verify_proto(int proto);
123 const char *nl_get_proto_name(int proto);
124 
125 extern int netlink_unloading;
126 
127 struct nl_proto_handler {
128 	nl_handler_f	cb;
129 	const char	*proto_name;
130 };
131 extern struct nl_proto_handler *nl_handlers;
132 
133 /* netlink_domain.c */
134 void nl_send_group(struct mbuf *m, int cnt, int proto, int group_id);
135 void nl_osd_register(void);
136 void nl_osd_unregister(void);
137 void nl_set_thread_nlp(struct thread *td, struct nlpcb *nlp);
138 
139 /* netlink_io.c */
140 #define	NL_IOF_UNTRANSLATED	0x01
141 #define	NL_IOF_IGNORE_LIMIT	0x02
142 bool nl_send_one(struct mbuf *m, struct nlpcb *nlp, int cnt, int io_flags);
143 void nlmsg_ack(struct nlpcb *nlp, int error, struct nlmsghdr *nlmsg,
144     struct nl_pstate *npt);
145 void nl_on_transmit(struct nlpcb *nlp);
146 void nl_init_io(struct nlpcb *nlp);
147 void nl_free_io(struct nlpcb *nlp);
148 
149 void nl_taskqueue_handler(void *_arg, int pending);
150 int nl_receive_async(struct mbuf *m, struct socket *so);
151 void nl_process_receive_locked(struct nlpcb *nlp);
152 void nl_set_source_metadata(struct mbuf *m, int num_messages);
153 void nl_add_msg_info(struct mbuf *m);
154 
155 /* netlink_generic.c */
156 struct genl_family {
157 	const char	*family_name;
158 	uint16_t	family_hdrsize;
159 	uint16_t	family_id;
160 	uint16_t	family_version;
161 	uint16_t	family_attr_max;
162 	uint16_t	family_cmd_size;
163 	uint16_t	family_num_groups;
164 	struct genl_cmd	*family_cmds;
165 };
166 
167 struct genl_group {
168 	struct genl_family	*group_family;
169 	const char		*group_name;
170 };
171 
172 struct genl_family *genl_get_family(uint32_t family_id);
173 struct genl_group *genl_get_group(uint32_t group_id);
174 
175 #define	MAX_FAMILIES	20
176 #define	MAX_GROUPS	64
177 
178 #define	MIN_GROUP_NUM	48
179 
180 #define	CTRL_FAMILY_NAME	"nlctrl"
181 
182 struct ifnet;
183 struct nl_parsed_link;
184 struct nlattr_bmask;
185 struct nl_pstate;
186 
187 /* Function map */
188 struct nl_function_wrapper {
189 	bool (*nlmsg_add)(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
190 	    uint16_t flags, uint32_t len);
191 	bool (*nlmsg_refill_buffer)(struct nl_writer *nw, int required_len);
192 	bool (*nlmsg_flush)(struct nl_writer *nw);
193 	bool (*nlmsg_end)(struct nl_writer *nw);
194 	void (*nlmsg_abort)(struct nl_writer *nw);
195 	void (*nlmsg_ignore_limit)(struct nl_writer *nw);
196 	bool (*nlmsg_get_unicast_writer)(struct nl_writer *nw, int size, struct nlpcb *nlp);
197 	bool (*nlmsg_get_group_writer)(struct nl_writer *nw, int size, int protocol, int group_id);
198 	bool (*nlmsg_get_chain_writer)(struct nl_writer *nw, int size, struct mbuf **pm);
199 	bool (*nlmsg_end_dump)(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
200 	int (*nl_modify_ifp_generic)(struct ifnet *ifp, struct nl_parsed_link *lattrs,
201 	    const struct nlattr_bmask *bm, struct nl_pstate *npt);
202 	void (*nl_store_ifp_cookie)(struct nl_pstate *npt, struct ifnet *ifp);
203 	struct nlpcb * (*nl_get_thread_nlp)(struct  thread *td);
204 };
205 void nl_set_functions(const struct nl_function_wrapper *nl);
206 
207 
208 
209 #endif
210 #endif
211