xref: /kernel/linux/linux-6.6/include/net/genetlink.h (revision 62306a36)
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 * @cap_sys_admin: whether %CAP_SYS_ADMIN is required for binding
16 */
17struct genl_multicast_group {
18	char			name[GENL_NAMSIZ];
19	u8			flags;
20	u8			cap_sys_admin:1;
21};
22
23struct genl_split_ops;
24struct genl_info;
25
26/**
27 * struct genl_family - generic netlink family
28 * @hdrsize: length of user specific header in bytes
29 * @name: name of family
30 * @version: protocol version
31 * @maxattr: maximum number of attributes supported
32 * @policy: netlink policy
33 * @netnsok: set to true if the family can handle network
34 *	namespaces and should be presented in all of them
35 * @parallel_ops: operations can be called in parallel and aren't
36 *	synchronized by the core genetlink code
37 * @pre_doit: called before an operation's doit callback, it may
38 *	do additional, common, filtering and return an error
39 * @post_doit: called after an operation's doit callback, it may
40 *	undo operations done by pre_doit, for example release locks
41 * @module: pointer to the owning module (set to THIS_MODULE)
42 * @mcgrps: multicast groups used by this family
43 * @n_mcgrps: number of multicast groups
44 * @resv_start_op: first operation for which reserved fields of the header
45 *	can be validated and policies are required (see below);
46 *	new families should leave this field at zero
47 * @ops: the operations supported by this family
48 * @n_ops: number of operations supported by this family
49 * @small_ops: the small-struct operations supported by this family
50 * @n_small_ops: number of small-struct operations supported by this family
51 * @split_ops: the split do/dump form of operation definition
52 * @n_split_ops: number of entries in @split_ops, not that with split do/dump
53 *	ops the number of entries is not the same as number of commands
54 *
55 * Attribute policies (the combination of @policy and @maxattr fields)
56 * can be attached at the family level or at the operation level.
57 * If both are present the per-operation policy takes precedence.
58 * For operations before @resv_start_op lack of policy means that the core
59 * will perform no attribute parsing or validation. For newer operations
60 * if policy is not provided core will reject all TLV attributes.
61 */
62struct genl_family {
63	unsigned int		hdrsize;
64	char			name[GENL_NAMSIZ];
65	unsigned int		version;
66	unsigned int		maxattr;
67	u8			netnsok:1;
68	u8			parallel_ops:1;
69	u8			n_ops;
70	u8			n_small_ops;
71	u8			n_split_ops;
72	u8			n_mcgrps;
73	u8			resv_start_op;
74	const struct nla_policy *policy;
75	int			(*pre_doit)(const struct genl_split_ops *ops,
76					    struct sk_buff *skb,
77					    struct genl_info *info);
78	void			(*post_doit)(const struct genl_split_ops *ops,
79					     struct sk_buff *skb,
80					     struct genl_info *info);
81	const struct genl_ops *	ops;
82	const struct genl_small_ops *small_ops;
83	const struct genl_split_ops *split_ops;
84	const struct genl_multicast_group *mcgrps;
85	struct module		*module;
86
87/* private: internal use only */
88	/* protocol family identifier */
89	int			id;
90	/* starting number of multicast group IDs in this family */
91	unsigned int		mcgrp_offset;
92};
93
94/**
95 * struct genl_info - receiving information
96 * @snd_seq: sending sequence number
97 * @snd_portid: netlink portid of sender
98 * @family: generic netlink family
99 * @nlhdr: netlink message header
100 * @genlhdr: generic netlink message header
101 * @attrs: netlink attributes
102 * @_net: network namespace
103 * @user_ptr: user pointers
104 * @extack: extended ACK report struct
105 */
106struct genl_info {
107	u32			snd_seq;
108	u32			snd_portid;
109	const struct genl_family *family;
110	const struct nlmsghdr *	nlhdr;
111	struct genlmsghdr *	genlhdr;
112	struct nlattr **	attrs;
113	possible_net_t		_net;
114	void *			user_ptr[2];
115	struct netlink_ext_ack *extack;
116};
117
118static inline struct net *genl_info_net(const struct genl_info *info)
119{
120	return read_pnet(&info->_net);
121}
122
123static inline void genl_info_net_set(struct genl_info *info, struct net *net)
124{
125	write_pnet(&info->_net, net);
126}
127
128static inline void *genl_info_userhdr(const struct genl_info *info)
129{
130	return (u8 *)info->genlhdr + GENL_HDRLEN;
131}
132
133#define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
134
135#define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
136	NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
137
138/* Report that a root attribute is missing */
139#define GENL_REQ_ATTR_CHECK(info, attr) ({				\
140	struct genl_info *__info = (info);				\
141									\
142	NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
143})
144
145enum genl_validate_flags {
146	GENL_DONT_VALIDATE_STRICT		= BIT(0),
147	GENL_DONT_VALIDATE_DUMP			= BIT(1),
148	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
149};
150
151/**
152 * struct genl_small_ops - generic netlink operations (small version)
153 * @cmd: command identifier
154 * @internal_flags: flags used by the family
155 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
156 * @validate: validation flags from enum genl_validate_flags
157 * @doit: standard command callback
158 * @dumpit: callback for dumpers
159 *
160 * This is a cut-down version of struct genl_ops for users who don't need
161 * most of the ancillary infra and want to save space.
162 */
163struct genl_small_ops {
164	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
165	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
166	u8	cmd;
167	u8	internal_flags;
168	u8	flags;
169	u8	validate;
170};
171
172/**
173 * struct genl_ops - generic netlink operations
174 * @cmd: command identifier
175 * @internal_flags: flags used by the family
176 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
177 * @maxattr: maximum number of attributes supported
178 * @policy: netlink policy (takes precedence over family policy)
179 * @validate: validation flags from enum genl_validate_flags
180 * @doit: standard command callback
181 * @start: start callback for dumps
182 * @dumpit: callback for dumpers
183 * @done: completion callback for dumps
184 */
185struct genl_ops {
186	int		       (*doit)(struct sk_buff *skb,
187				       struct genl_info *info);
188	int		       (*start)(struct netlink_callback *cb);
189	int		       (*dumpit)(struct sk_buff *skb,
190					 struct netlink_callback *cb);
191	int		       (*done)(struct netlink_callback *cb);
192	const struct nla_policy *policy;
193	unsigned int		maxattr;
194	u8			cmd;
195	u8			internal_flags;
196	u8			flags;
197	u8			validate;
198};
199
200/**
201 * struct genl_split_ops - generic netlink operations (do/dump split version)
202 * @cmd: command identifier
203 * @internal_flags: flags used by the family
204 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
205 * @validate: validation flags from enum genl_validate_flags
206 * @policy: netlink policy (takes precedence over family policy)
207 * @maxattr: maximum number of attributes supported
208 *
209 * Do callbacks:
210 * @pre_doit: called before an operation's @doit callback, it may
211 *	do additional, common, filtering and return an error
212 * @doit: standard command callback
213 * @post_doit: called after an operation's @doit callback, it may
214 *	undo operations done by pre_doit, for example release locks
215 *
216 * Dump callbacks:
217 * @start: start callback for dumps
218 * @dumpit: callback for dumpers
219 * @done: completion callback for dumps
220 *
221 * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
222 * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
223 * Exactly one of those flags must be set.
224 */
225struct genl_split_ops {
226	union {
227		struct {
228			int (*pre_doit)(const struct genl_split_ops *ops,
229					struct sk_buff *skb,
230					struct genl_info *info);
231			int (*doit)(struct sk_buff *skb,
232				    struct genl_info *info);
233			void (*post_doit)(const struct genl_split_ops *ops,
234					  struct sk_buff *skb,
235					  struct genl_info *info);
236		};
237		struct {
238			int (*start)(struct netlink_callback *cb);
239			int (*dumpit)(struct sk_buff *skb,
240				      struct netlink_callback *cb);
241			int (*done)(struct netlink_callback *cb);
242		};
243	};
244	const struct nla_policy *policy;
245	unsigned int		maxattr;
246	u8			cmd;
247	u8			internal_flags;
248	u8			flags;
249	u8			validate;
250};
251
252/**
253 * struct genl_dumpit_info - info that is available during dumpit op call
254 * @op: generic netlink ops - for internal genl code usage
255 * @attrs: netlink attributes
256 * @info: struct genl_info describing the request
257 */
258struct genl_dumpit_info {
259	struct genl_split_ops op;
260	struct genl_info info;
261};
262
263static inline const struct genl_dumpit_info *
264genl_dumpit_info(struct netlink_callback *cb)
265{
266	return cb->data;
267}
268
269static inline const struct genl_info *
270genl_info_dump(struct netlink_callback *cb)
271{
272	return &genl_dumpit_info(cb)->info;
273}
274
275/**
276 * genl_info_init_ntf() - initialize genl_info for notifications
277 * @info:   genl_info struct to set up
278 * @family: pointer to the genetlink family
279 * @cmd:    command to be used in the notification
280 *
281 * Initialize a locally declared struct genl_info to pass to various APIs.
282 * Intended to be used when creating notifications.
283 */
284static inline void
285genl_info_init_ntf(struct genl_info *info, const struct genl_family *family,
286		   u8 cmd)
287{
288	struct genlmsghdr *hdr = (void *) &info->user_ptr[0];
289
290	memset(info, 0, sizeof(*info));
291	info->family = family;
292	info->genlhdr = hdr;
293	hdr->cmd = cmd;
294}
295
296static inline bool genl_info_is_ntf(const struct genl_info *info)
297{
298	return !info->nlhdr;
299}
300
301int genl_register_family(struct genl_family *family);
302int genl_unregister_family(const struct genl_family *family);
303void genl_notify(const struct genl_family *family, struct sk_buff *skb,
304		 struct genl_info *info, u32 group, gfp_t flags);
305
306void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
307		  const struct genl_family *family, int flags, u8 cmd);
308
309static inline void *
310__genlmsg_iput(struct sk_buff *skb, const struct genl_info *info, int flags)
311{
312	return genlmsg_put(skb, info->snd_portid, info->snd_seq, info->family,
313			   flags, info->genlhdr->cmd);
314}
315
316/**
317 * genlmsg_iput - start genetlink message based on genl_info
318 * @skb: skb in which message header will be placed
319 * @info: genl_info as provided to do/dump handlers
320 *
321 * Convenience wrapper which starts a genetlink message based on
322 * information in user request. @info should be either the struct passed
323 * by genetlink core to do/dump handlers (when constructing replies to
324 * such requests) or a struct initialized by genl_info_init_ntf()
325 * when constructing notifications.
326 *
327 * Returns pointer to new genetlink header.
328 */
329static inline void *
330genlmsg_iput(struct sk_buff *skb, const struct genl_info *info)
331{
332	return __genlmsg_iput(skb, info, 0);
333}
334
335/**
336 * genlmsg_nlhdr - Obtain netlink header from user specified header
337 * @user_hdr: user header as returned from genlmsg_put()
338 *
339 * Returns pointer to netlink header.
340 */
341static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
342{
343	return (struct nlmsghdr *)((char *)user_hdr -
344				   GENL_HDRLEN -
345				   NLMSG_HDRLEN);
346}
347
348/**
349 * genlmsg_parse_deprecated - parse attributes of a genetlink message
350 * @nlh: netlink message header
351 * @family: genetlink message family
352 * @tb: destination array with maxtype+1 elements
353 * @maxtype: maximum attribute type to be expected
354 * @policy: validation policy
355 * @extack: extended ACK report struct
356 */
357static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
358					   const struct genl_family *family,
359					   struct nlattr *tb[], int maxtype,
360					   const struct nla_policy *policy,
361					   struct netlink_ext_ack *extack)
362{
363	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
364			     policy, NL_VALIDATE_LIBERAL, extack);
365}
366
367/**
368 * genlmsg_parse - parse attributes of a genetlink message
369 * @nlh: netlink message header
370 * @family: genetlink message family
371 * @tb: destination array with maxtype+1 elements
372 * @maxtype: maximum attribute type to be expected
373 * @policy: validation policy
374 * @extack: extended ACK report struct
375 */
376static inline int genlmsg_parse(const struct nlmsghdr *nlh,
377				const struct genl_family *family,
378				struct nlattr *tb[], int maxtype,
379				const struct nla_policy *policy,
380				struct netlink_ext_ack *extack)
381{
382	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
383			     policy, NL_VALIDATE_STRICT, extack);
384}
385
386/**
387 * genl_dump_check_consistent - check if sequence is consistent and advertise if not
388 * @cb: netlink callback structure that stores the sequence number
389 * @user_hdr: user header as returned from genlmsg_put()
390 *
391 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
392 * simpler to use with generic netlink.
393 */
394static inline void genl_dump_check_consistent(struct netlink_callback *cb,
395					      void *user_hdr)
396{
397	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
398}
399
400/**
401 * genlmsg_put_reply - Add generic netlink header to a reply message
402 * @skb: socket buffer holding the message
403 * @info: receiver info
404 * @family: generic netlink family
405 * @flags: netlink message flags
406 * @cmd: generic netlink command
407 *
408 * Returns pointer to user specific header
409 */
410static inline void *genlmsg_put_reply(struct sk_buff *skb,
411				      struct genl_info *info,
412				      const struct genl_family *family,
413				      int flags, u8 cmd)
414{
415	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
416			   flags, cmd);
417}
418
419/**
420 * genlmsg_end - Finalize a generic netlink message
421 * @skb: socket buffer the message is stored in
422 * @hdr: user specific header
423 */
424static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
425{
426	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
427}
428
429/**
430 * genlmsg_cancel - Cancel construction of a generic netlink message
431 * @skb: socket buffer the message is stored in
432 * @hdr: generic netlink message header
433 */
434static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
435{
436	if (hdr)
437		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
438}
439
440/**
441 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
442 * @family: the generic netlink family
443 * @net: the net namespace
444 * @skb: netlink message as socket buffer
445 * @portid: own netlink portid to avoid sending to yourself
446 * @group: offset of multicast group in groups array
447 * @flags: allocation flags
448 */
449static inline int genlmsg_multicast_netns(const struct genl_family *family,
450					  struct net *net, struct sk_buff *skb,
451					  u32 portid, unsigned int group, gfp_t flags)
452{
453	if (WARN_ON_ONCE(group >= family->n_mcgrps))
454		return -EINVAL;
455	group = family->mcgrp_offset + group;
456	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
457}
458
459/**
460 * genlmsg_multicast - multicast a netlink message to the default netns
461 * @family: the generic netlink family
462 * @skb: netlink message as socket buffer
463 * @portid: own netlink portid to avoid sending to yourself
464 * @group: offset of multicast group in groups array
465 * @flags: allocation flags
466 */
467static inline int genlmsg_multicast(const struct genl_family *family,
468				    struct sk_buff *skb, u32 portid,
469				    unsigned int group, gfp_t flags)
470{
471	return genlmsg_multicast_netns(family, &init_net, skb,
472				       portid, group, flags);
473}
474
475/**
476 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
477 * @family: the generic netlink family
478 * @skb: netlink message as socket buffer
479 * @portid: own netlink portid to avoid sending to yourself
480 * @group: offset of multicast group in groups array
481 * @flags: allocation flags
482 *
483 * This function must hold the RTNL or rcu_read_lock().
484 */
485int genlmsg_multicast_allns(const struct genl_family *family,
486			    struct sk_buff *skb, u32 portid,
487			    unsigned int group, gfp_t flags);
488
489/**
490 * genlmsg_unicast - unicast a netlink message
491 * @net: network namespace to look up @portid in
492 * @skb: netlink message as socket buffer
493 * @portid: netlink portid of the destination socket
494 */
495static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
496{
497	return nlmsg_unicast(net->genl_sock, skb, portid);
498}
499
500/**
501 * genlmsg_reply - reply to a request
502 * @skb: netlink message to be sent back
503 * @info: receiver information
504 */
505static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
506{
507	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
508}
509
510/**
511 * genlmsg_data - head of message payload
512 * @gnlh: genetlink message header
513 */
514static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
515{
516	return ((unsigned char *) gnlh + GENL_HDRLEN);
517}
518
519/**
520 * genlmsg_len - length of message payload
521 * @gnlh: genetlink message header
522 */
523static inline int genlmsg_len(const struct genlmsghdr *gnlh)
524{
525	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
526							NLMSG_HDRLEN);
527	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
528}
529
530/**
531 * genlmsg_msg_size - length of genetlink message not including padding
532 * @payload: length of message payload
533 */
534static inline int genlmsg_msg_size(int payload)
535{
536	return GENL_HDRLEN + payload;
537}
538
539/**
540 * genlmsg_total_size - length of genetlink message including padding
541 * @payload: length of message payload
542 */
543static inline int genlmsg_total_size(int payload)
544{
545	return NLMSG_ALIGN(genlmsg_msg_size(payload));
546}
547
548/**
549 * genlmsg_new - Allocate a new generic netlink message
550 * @payload: size of the message payload
551 * @flags: the type of memory to allocate.
552 */
553static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
554{
555	return nlmsg_new(genlmsg_total_size(payload), flags);
556}
557
558/**
559 * genl_set_err - report error to genetlink broadcast listeners
560 * @family: the generic netlink family
561 * @net: the network namespace to report the error to
562 * @portid: the PORTID of a process that we want to skip (if any)
563 * @group: the broadcast group that will notice the error
564 * 	(this is the offset of the multicast group in the groups array)
565 * @code: error code, must be negative (as usual in kernelspace)
566 *
567 * This function returns the number of broadcast listeners that have set the
568 * NETLINK_RECV_NO_ENOBUFS socket option.
569 */
570static inline int genl_set_err(const struct genl_family *family,
571			       struct net *net, u32 portid,
572			       u32 group, int code)
573{
574	if (WARN_ON_ONCE(group >= family->n_mcgrps))
575		return -EINVAL;
576	group = family->mcgrp_offset + group;
577	return netlink_set_err(net->genl_sock, portid, group, code);
578}
579
580static inline int genl_has_listeners(const struct genl_family *family,
581				     struct net *net, unsigned int group)
582{
583	if (WARN_ON_ONCE(group >= family->n_mcgrps))
584		return -EINVAL;
585	group = family->mcgrp_offset + group;
586	return netlink_has_listeners(net->genl_sock, group);
587}
588#endif	/* __NET_GENERIC_NETLINK_H */
589