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