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