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