xref: /linux-6.15/include/linux/netlink.h (revision d3003d9e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETLINK_H
3 #define __LINUX_NETLINK_H
4 
5 
6 #include <linux/capability.h>
7 #include <linux/skbuff.h>
8 #include <linux/export.h>
9 #include <net/scm.h>
10 #include <uapi/linux/netlink.h>
11 
12 struct net;
13 
14 void do_trace_netlink_extack(const char *msg);
15 
16 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
17 {
18 	return (struct nlmsghdr *)skb->data;
19 }
20 
21 enum netlink_skb_flags {
22 	NETLINK_SKB_DST		= 0x8,	/* Dst set in sendto or sendmsg */
23 };
24 
25 struct netlink_skb_parms {
26 	struct scm_creds	creds;		/* Skb credentials	*/
27 	__u32			portid;
28 	__u32			dst_group;
29 	__u32			flags;
30 	struct sock		*sk;
31 	bool			nsid_is_set;
32 	int			nsid;
33 };
34 
35 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
36 #define NETLINK_CREDS(skb)	(&NETLINK_CB((skb)).creds)
37 
38 
39 void netlink_table_grab(void);
40 void netlink_table_ungrab(void);
41 
42 #define NL_CFG_F_NONROOT_RECV	(1 << 0)
43 #define NL_CFG_F_NONROOT_SEND	(1 << 1)
44 
45 /* optional Netlink kernel configuration parameters */
46 struct netlink_kernel_cfg {
47 	unsigned int	groups;
48 	unsigned int	flags;
49 	void		(*input)(struct sk_buff *skb);
50 	struct mutex	*cb_mutex;
51 	int		(*bind)(struct net *net, int group);
52 	void		(*unbind)(struct net *net, int group);
53 	bool		(*compare)(struct net *net, struct sock *sk);
54 };
55 
56 struct sock *__netlink_kernel_create(struct net *net, int unit,
57 					    struct module *module,
58 					    struct netlink_kernel_cfg *cfg);
59 static inline struct sock *
60 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
61 {
62 	return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
63 }
64 
65 /* this can be increased when necessary - don't expose to userland */
66 #define NETLINK_MAX_COOKIE_LEN	20
67 
68 /**
69  * struct netlink_ext_ack - netlink extended ACK report struct
70  * @_msg: message string to report - don't access directly, use
71  *	%NL_SET_ERR_MSG
72  * @bad_attr: attribute with error
73  * @policy: policy for a bad attribute
74  * @cookie: cookie data to return to userspace (for success)
75  * @cookie_len: actual cookie data length
76  */
77 struct netlink_ext_ack {
78 	const char *_msg;
79 	const struct nlattr *bad_attr;
80 	const struct nla_policy *policy;
81 	u8 cookie[NETLINK_MAX_COOKIE_LEN];
82 	u8 cookie_len;
83 };
84 
85 /* Always use this macro, this allows later putting the
86  * message into a separate section or such for things
87  * like translation or listing all possible messages.
88  * Currently string formatting is not supported (due
89  * to the lack of an output buffer.)
90  */
91 #define NL_SET_ERR_MSG(extack, msg) do {		\
92 	static const char __msg[] = msg;		\
93 	struct netlink_ext_ack *__extack = (extack);	\
94 							\
95 	do_trace_netlink_extack(__msg);			\
96 							\
97 	if (__extack)					\
98 		__extack->_msg = __msg;			\
99 } while (0)
100 
101 #define NL_SET_ERR_MSG_MOD(extack, msg)			\
102 	NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
103 
104 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do {	\
105 	if ((extack)) {					\
106 		(extack)->bad_attr = (attr);		\
107 		(extack)->policy = (pol);		\
108 	}						\
109 } while (0)
110 
111 #define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
112 
113 #define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do {	\
114 	static const char __msg[] = msg;			\
115 	struct netlink_ext_ack *__extack = (extack);		\
116 								\
117 	do_trace_netlink_extack(__msg);				\
118 								\
119 	if (__extack) {						\
120 		__extack->_msg = __msg;				\
121 		__extack->bad_attr = (attr);			\
122 		__extack->policy = (pol);			\
123 	}							\
124 } while (0)
125 
126 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg)		\
127 	NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
128 
129 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
130 					    u64 cookie)
131 {
132 	u64 __cookie = cookie;
133 
134 	if (!extack)
135 		return;
136 	memcpy(extack->cookie, &__cookie, sizeof(__cookie));
137 	extack->cookie_len = sizeof(__cookie);
138 }
139 
140 static inline void nl_set_extack_cookie_u32(struct netlink_ext_ack *extack,
141 					    u32 cookie)
142 {
143 	u32 __cookie = cookie;
144 
145 	if (!extack)
146 		return;
147 	memcpy(extack->cookie, &__cookie, sizeof(__cookie));
148 	extack->cookie_len = sizeof(__cookie);
149 }
150 
151 void netlink_kernel_release(struct sock *sk);
152 int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
153 int netlink_change_ngroups(struct sock *sk, unsigned int groups);
154 void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
155 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
156 		 const struct netlink_ext_ack *extack);
157 int netlink_has_listeners(struct sock *sk, unsigned int group);
158 bool netlink_strict_get_check(struct sk_buff *skb);
159 
160 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
161 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
162 		      __u32 group, gfp_t allocation);
163 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
164 			       __u32 portid, __u32 group, gfp_t allocation,
165 			       int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
166 			       void *filter_data);
167 int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
168 int netlink_register_notifier(struct notifier_block *nb);
169 int netlink_unregister_notifier(struct notifier_block *nb);
170 
171 /* finegrained unicast helpers: */
172 struct sock *netlink_getsockbyfilp(struct file *filp);
173 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
174 		      long *timeo, struct sock *ssk);
175 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
176 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
177 
178 static inline struct sk_buff *
179 netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
180 {
181 	struct sk_buff *nskb;
182 
183 	nskb = skb_clone(skb, gfp_mask);
184 	if (!nskb)
185 		return NULL;
186 
187 	/* This is a large skb, set destructor callback to release head */
188 	if (is_vmalloc_addr(skb->head))
189 		nskb->destructor = skb->destructor;
190 
191 	return nskb;
192 }
193 
194 /*
195  *	skb should fit one page. This choice is good for headerless malloc.
196  *	But we should limit to 8K so that userspace does not have to
197  *	use enormous buffer sizes on recvmsg() calls just to avoid
198  *	MSG_TRUNC when PAGE_SIZE is very large.
199  */
200 #if PAGE_SIZE < 8192UL
201 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(PAGE_SIZE)
202 #else
203 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(8192UL)
204 #endif
205 
206 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
207 
208 
209 struct netlink_callback {
210 	struct sk_buff		*skb;
211 	const struct nlmsghdr	*nlh;
212 	int			(*dump)(struct sk_buff * skb,
213 					struct netlink_callback *cb);
214 	int			(*done)(struct netlink_callback *cb);
215 	void			*data;
216 	/* the module that dump function belong to */
217 	struct module		*module;
218 	struct netlink_ext_ack	*extack;
219 	u16			family;
220 	u16			answer_flags;
221 	u32			min_dump_alloc;
222 	unsigned int		prev_seq, seq;
223 	bool			strict_check;
224 	union {
225 		u8		ctx[48];
226 
227 		/* args is deprecated. Cast a struct over ctx instead
228 		 * for proper type safety.
229 		 */
230 		long		args[6];
231 	};
232 };
233 
234 struct netlink_notify {
235 	struct net *net;
236 	u32 portid;
237 	int protocol;
238 };
239 
240 struct nlmsghdr *
241 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
242 
243 struct netlink_dump_control {
244 	int (*start)(struct netlink_callback *);
245 	int (*dump)(struct sk_buff *skb, struct netlink_callback *);
246 	int (*done)(struct netlink_callback *);
247 	void *data;
248 	struct module *module;
249 	u32 min_dump_alloc;
250 };
251 
252 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
253 				const struct nlmsghdr *nlh,
254 				struct netlink_dump_control *control);
255 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
256 				     const struct nlmsghdr *nlh,
257 				     struct netlink_dump_control *control)
258 {
259 	if (!control->module)
260 		control->module = THIS_MODULE;
261 
262 	return __netlink_dump_start(ssk, skb, nlh, control);
263 }
264 
265 struct netlink_tap {
266 	struct net_device *dev;
267 	struct module *module;
268 	struct list_head list;
269 };
270 
271 int netlink_add_tap(struct netlink_tap *nt);
272 int netlink_remove_tap(struct netlink_tap *nt);
273 
274 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
275 			  struct user_namespace *ns, int cap);
276 bool netlink_ns_capable(const struct sk_buff *skb,
277 			struct user_namespace *ns, int cap);
278 bool netlink_capable(const struct sk_buff *skb, int cap);
279 bool netlink_net_capable(const struct sk_buff *skb, int cap);
280 
281 #endif	/* __LINUX_NETLINK_H */
282