xref: /linux/include/net/genetlink.h (revision c6fbb759)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_GENERIC_NETLINK_H
3 #define __NET_GENERIC_NETLINK_H
4 
5 #include <linux/genetlink.h>
6 #include <net/netlink.h>
7 #include <net/net_namespace.h>
8 
9 #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
10 
11 /**
12  * struct genl_multicast_group - generic netlink multicast group
13  * @name: name of the multicast group, names are per-family
14  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
15  */
16 struct genl_multicast_group {
17 	char			name[GENL_NAMSIZ];
18 	u8			flags;
19 };
20 
21 struct genl_ops;
22 struct genl_info;
23 
24 /**
25  * struct genl_family - generic netlink family
26  * @id: protocol family identifier (private)
27  * @hdrsize: length of user specific header in bytes
28  * @name: name of family
29  * @version: protocol version
30  * @maxattr: maximum number of attributes supported
31  * @policy: netlink policy
32  * @netnsok: set to true if the family can handle network
33  *	namespaces and should be presented in all of them
34  * @parallel_ops: operations can be called in parallel and aren't
35  *	synchronized by the core genetlink code
36  * @pre_doit: called before an operation's doit callback, it may
37  *	do additional, common, filtering and return an error
38  * @post_doit: called after an operation's doit callback, it may
39  *	undo operations done by pre_doit, for example release locks
40  * @mcgrps: multicast groups used by this family
41  * @n_mcgrps: number of multicast groups
42  * @resv_start_op: first operation for which reserved fields of the header
43  *	can be validated, new families should leave this field at zero
44  * @mcgrp_offset: starting number of multicast group IDs in this family
45  *	(private)
46  * @ops: the operations supported by this family
47  * @n_ops: number of operations supported by this family
48  * @small_ops: the small-struct operations supported by this family
49  * @n_small_ops: number of small-struct operations supported by this family
50  */
51 struct genl_family {
52 	int			id;		/* private */
53 	unsigned int		hdrsize;
54 	char			name[GENL_NAMSIZ];
55 	unsigned int		version;
56 	unsigned int		maxattr;
57 	unsigned int		mcgrp_offset;	/* private */
58 	u8			netnsok:1;
59 	u8			parallel_ops:1;
60 	u8			n_ops;
61 	u8			n_small_ops;
62 	u8			n_mcgrps;
63 	u8			resv_start_op;
64 	const struct nla_policy *policy;
65 	int			(*pre_doit)(const struct genl_ops *ops,
66 					    struct sk_buff *skb,
67 					    struct genl_info *info);
68 	void			(*post_doit)(const struct genl_ops *ops,
69 					     struct sk_buff *skb,
70 					     struct genl_info *info);
71 	const struct genl_ops *	ops;
72 	const struct genl_small_ops *small_ops;
73 	const struct genl_multicast_group *mcgrps;
74 	struct module		*module;
75 };
76 
77 /**
78  * struct genl_info - receiving information
79  * @snd_seq: sending sequence number
80  * @snd_portid: netlink portid of sender
81  * @nlhdr: netlink message header
82  * @genlhdr: generic netlink message header
83  * @userhdr: user specific header
84  * @attrs: netlink attributes
85  * @_net: network namespace
86  * @user_ptr: user pointers
87  * @extack: extended ACK report struct
88  */
89 struct genl_info {
90 	u32			snd_seq;
91 	u32			snd_portid;
92 	struct nlmsghdr *	nlhdr;
93 	struct genlmsghdr *	genlhdr;
94 	void *			userhdr;
95 	struct nlattr **	attrs;
96 	possible_net_t		_net;
97 	void *			user_ptr[2];
98 	struct netlink_ext_ack *extack;
99 };
100 
101 static inline struct net *genl_info_net(struct genl_info *info)
102 {
103 	return read_pnet(&info->_net);
104 }
105 
106 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
107 {
108 	write_pnet(&info->_net, net);
109 }
110 
111 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
112 
113 /* Report that a root attribute is missing */
114 #define GENL_REQ_ATTR_CHECK(info, attr) ({				\
115 	struct genl_info *__info = (info);				\
116 									\
117 	NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
118 })
119 
120 enum genl_validate_flags {
121 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
122 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
123 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
124 };
125 
126 /**
127  * struct genl_small_ops - generic netlink operations (small version)
128  * @cmd: command identifier
129  * @internal_flags: flags used by the family
130  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
131  * @validate: validation flags from enum genl_validate_flags
132  * @doit: standard command callback
133  * @dumpit: callback for dumpers
134  *
135  * This is a cut-down version of struct genl_ops for users who don't need
136  * most of the ancillary infra and want to save space.
137  */
138 struct genl_small_ops {
139 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
140 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
141 	u8	cmd;
142 	u8	internal_flags;
143 	u8	flags;
144 	u8	validate;
145 };
146 
147 /**
148  * struct genl_ops - generic netlink operations
149  * @cmd: command identifier
150  * @internal_flags: flags used by the family
151  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
152  * @maxattr: maximum number of attributes supported
153  * @policy: netlink policy (takes precedence over family policy)
154  * @validate: validation flags from enum genl_validate_flags
155  * @doit: standard command callback
156  * @start: start callback for dumps
157  * @dumpit: callback for dumpers
158  * @done: completion callback for dumps
159  */
160 struct genl_ops {
161 	int		       (*doit)(struct sk_buff *skb,
162 				       struct genl_info *info);
163 	int		       (*start)(struct netlink_callback *cb);
164 	int		       (*dumpit)(struct sk_buff *skb,
165 					 struct netlink_callback *cb);
166 	int		       (*done)(struct netlink_callback *cb);
167 	const struct nla_policy *policy;
168 	unsigned int		maxattr;
169 	u8			cmd;
170 	u8			internal_flags;
171 	u8			flags;
172 	u8			validate;
173 };
174 
175 /**
176  * struct genl_info - info that is available during dumpit op call
177  * @family: generic netlink family - for internal genl code usage
178  * @ops: generic netlink ops - for internal genl code usage
179  * @attrs: netlink attributes
180  */
181 struct genl_dumpit_info {
182 	const struct genl_family *family;
183 	struct genl_ops op;
184 	struct nlattr **attrs;
185 };
186 
187 static inline const struct genl_dumpit_info *
188 genl_dumpit_info(struct netlink_callback *cb)
189 {
190 	return cb->data;
191 }
192 
193 int genl_register_family(struct genl_family *family);
194 int genl_unregister_family(const struct genl_family *family);
195 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
196 		 struct genl_info *info, u32 group, gfp_t flags);
197 
198 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
199 		  const struct genl_family *family, int flags, u8 cmd);
200 
201 /**
202  * genlmsg_nlhdr - Obtain netlink header from user specified header
203  * @user_hdr: user header as returned from genlmsg_put()
204  *
205  * Returns pointer to netlink header.
206  */
207 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
208 {
209 	return (struct nlmsghdr *)((char *)user_hdr -
210 				   GENL_HDRLEN -
211 				   NLMSG_HDRLEN);
212 }
213 
214 /**
215  * genlmsg_parse_deprecated - parse attributes of a genetlink message
216  * @nlh: netlink message header
217  * @family: genetlink message family
218  * @tb: destination array with maxtype+1 elements
219  * @maxtype: maximum attribute type to be expected
220  * @policy: validation policy
221  * @extack: extended ACK report struct
222  */
223 static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
224 					   const struct genl_family *family,
225 					   struct nlattr *tb[], int maxtype,
226 					   const struct nla_policy *policy,
227 					   struct netlink_ext_ack *extack)
228 {
229 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
230 			     policy, NL_VALIDATE_LIBERAL, extack);
231 }
232 
233 /**
234  * genlmsg_parse - parse attributes of a genetlink message
235  * @nlh: netlink message header
236  * @family: genetlink message family
237  * @tb: destination array with maxtype+1 elements
238  * @maxtype: maximum attribute type to be expected
239  * @policy: validation policy
240  * @extack: extended ACK report struct
241  */
242 static inline int genlmsg_parse(const struct nlmsghdr *nlh,
243 				const struct genl_family *family,
244 				struct nlattr *tb[], int maxtype,
245 				const struct nla_policy *policy,
246 				struct netlink_ext_ack *extack)
247 {
248 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
249 			     policy, NL_VALIDATE_STRICT, extack);
250 }
251 
252 /**
253  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
254  * @cb: netlink callback structure that stores the sequence number
255  * @user_hdr: user header as returned from genlmsg_put()
256  *
257  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
258  * simpler to use with generic netlink.
259  */
260 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
261 					      void *user_hdr)
262 {
263 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
264 }
265 
266 /**
267  * genlmsg_put_reply - Add generic netlink header to a reply message
268  * @skb: socket buffer holding the message
269  * @info: receiver info
270  * @family: generic netlink family
271  * @flags: netlink message flags
272  * @cmd: generic netlink command
273  *
274  * Returns pointer to user specific header
275  */
276 static inline void *genlmsg_put_reply(struct sk_buff *skb,
277 				      struct genl_info *info,
278 				      const struct genl_family *family,
279 				      int flags, u8 cmd)
280 {
281 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
282 			   flags, cmd);
283 }
284 
285 /**
286  * genlmsg_end - Finalize a generic netlink message
287  * @skb: socket buffer the message is stored in
288  * @hdr: user specific header
289  */
290 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
291 {
292 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
293 }
294 
295 /**
296  * genlmsg_cancel - Cancel construction of a generic netlink message
297  * @skb: socket buffer the message is stored in
298  * @hdr: generic netlink message header
299  */
300 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
301 {
302 	if (hdr)
303 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
304 }
305 
306 /**
307  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
308  * @family: the generic netlink family
309  * @net: the net namespace
310  * @skb: netlink message as socket buffer
311  * @portid: own netlink portid to avoid sending to yourself
312  * @group: offset of multicast group in groups array
313  * @flags: allocation flags
314  */
315 static inline int genlmsg_multicast_netns(const struct genl_family *family,
316 					  struct net *net, struct sk_buff *skb,
317 					  u32 portid, unsigned int group, gfp_t flags)
318 {
319 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
320 		return -EINVAL;
321 	group = family->mcgrp_offset + group;
322 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
323 }
324 
325 /**
326  * genlmsg_multicast - multicast a netlink message to the default netns
327  * @family: the generic netlink family
328  * @skb: netlink message as socket buffer
329  * @portid: own netlink portid to avoid sending to yourself
330  * @group: offset of multicast group in groups array
331  * @flags: allocation flags
332  */
333 static inline int genlmsg_multicast(const struct genl_family *family,
334 				    struct sk_buff *skb, u32 portid,
335 				    unsigned int group, gfp_t flags)
336 {
337 	return genlmsg_multicast_netns(family, &init_net, skb,
338 				       portid, group, flags);
339 }
340 
341 /**
342  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
343  * @family: the generic netlink family
344  * @skb: netlink message as socket buffer
345  * @portid: own netlink portid to avoid sending to yourself
346  * @group: offset of multicast group in groups array
347  * @flags: allocation flags
348  *
349  * This function must hold the RTNL or rcu_read_lock().
350  */
351 int genlmsg_multicast_allns(const struct genl_family *family,
352 			    struct sk_buff *skb, u32 portid,
353 			    unsigned int group, gfp_t flags);
354 
355 /**
356  * genlmsg_unicast - unicast a netlink message
357  * @skb: netlink message as socket buffer
358  * @portid: netlink portid of the destination socket
359  */
360 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
361 {
362 	return nlmsg_unicast(net->genl_sock, skb, portid);
363 }
364 
365 /**
366  * genlmsg_reply - reply to a request
367  * @skb: netlink message to be sent back
368  * @info: receiver information
369  */
370 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
371 {
372 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
373 }
374 
375 /**
376  * gennlmsg_data - head of message payload
377  * @gnlh: genetlink message header
378  */
379 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
380 {
381 	return ((unsigned char *) gnlh + GENL_HDRLEN);
382 }
383 
384 /**
385  * genlmsg_len - length of message payload
386  * @gnlh: genetlink message header
387  */
388 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
389 {
390 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
391 							NLMSG_HDRLEN);
392 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
393 }
394 
395 /**
396  * genlmsg_msg_size - length of genetlink message not including padding
397  * @payload: length of message payload
398  */
399 static inline int genlmsg_msg_size(int payload)
400 {
401 	return GENL_HDRLEN + payload;
402 }
403 
404 /**
405  * genlmsg_total_size - length of genetlink message including padding
406  * @payload: length of message payload
407  */
408 static inline int genlmsg_total_size(int payload)
409 {
410 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
411 }
412 
413 /**
414  * genlmsg_new - Allocate a new generic netlink message
415  * @payload: size of the message payload
416  * @flags: the type of memory to allocate.
417  */
418 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
419 {
420 	return nlmsg_new(genlmsg_total_size(payload), flags);
421 }
422 
423 /**
424  * genl_set_err - report error to genetlink broadcast listeners
425  * @family: the generic netlink family
426  * @net: the network namespace to report the error to
427  * @portid: the PORTID of a process that we want to skip (if any)
428  * @group: the broadcast group that will notice the error
429  * 	(this is the offset of the multicast group in the groups array)
430  * @code: error code, must be negative (as usual in kernelspace)
431  *
432  * This function returns the number of broadcast listeners that have set the
433  * NETLINK_RECV_NO_ENOBUFS socket option.
434  */
435 static inline int genl_set_err(const struct genl_family *family,
436 			       struct net *net, u32 portid,
437 			       u32 group, int code)
438 {
439 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
440 		return -EINVAL;
441 	group = family->mcgrp_offset + group;
442 	return netlink_set_err(net->genl_sock, portid, group, code);
443 }
444 
445 static inline int genl_has_listeners(const struct genl_family *family,
446 				     struct net *net, unsigned int group)
447 {
448 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
449 		return -EINVAL;
450 	group = family->mcgrp_offset + group;
451 	return netlink_has_listeners(net->genl_sock, group);
452 }
453 #endif	/* __NET_GENERIC_NETLINK_H */
454