xref: /openbmc/linux/include/net/genetlink.h (revision ee1c244219fd652964710a6cc3e4f922e86aa492)
1482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H
2482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H
3482a8524SThomas Graf 
4482a8524SThomas Graf #include <linux/genetlink.h>
5482a8524SThomas Graf #include <net/netlink.h>
6134e6375SJohannes Berg #include <net/net_namespace.h>
7482a8524SThomas Graf 
858050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
958050fceSThomas Graf 
10482a8524SThomas Graf /**
112dbba6f7SJohannes Berg  * struct genl_multicast_group - generic netlink multicast group
122dbba6f7SJohannes Berg  * @name: name of the multicast group, names are per-family
132dbba6f7SJohannes Berg  */
14fd2c3ef7SEric Dumazet struct genl_multicast_group {
152dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
162dbba6f7SJohannes Berg };
172dbba6f7SJohannes Berg 
18ff4c92d8SJohannes Berg struct genl_ops;
19ff4c92d8SJohannes Berg struct genl_info;
20ff4c92d8SJohannes Berg 
212dbba6f7SJohannes Berg /**
22482a8524SThomas Graf  * struct genl_family - generic netlink family
23482a8524SThomas Graf  * @id: protocol family idenfitier
24482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
25482a8524SThomas Graf  * @name: name of family
26482a8524SThomas Graf  * @version: protocol version
27482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
28134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
29134e6375SJohannes Berg  *	namespaces and should be presented in all of them
30f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
31f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
32ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
33ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
34ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
35ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
36c380d9a7SJohannes Berg  * @mcast_bind: a socket bound to the given multicast group (which
37c380d9a7SJohannes Berg  *	is given as the offset into the groups array)
38*ee1c2442SJohannes Berg  * @mcast_unbind: a socket was unbound from the given multicast group.
39*ee1c2442SJohannes Berg  *	Note that unbind() will not be called symmetrically if the
40*ee1c2442SJohannes Berg  *	generic netlink family is removed while there are still open
41*ee1c2442SJohannes Berg  *	sockets.
42482a8524SThomas Graf  * @attrbuf: buffer to store parsed attributes
43482a8524SThomas Graf  * @family_list: family list
442a94fe48SJohannes Berg  * @mcgrps: multicast groups used by this family (private)
452a94fe48SJohannes Berg  * @n_mcgrps: number of multicast groups (private)
462a94fe48SJohannes Berg  * @mcgrp_offset: starting number of multicast group IDs in this family
47d91824c0SJohannes Berg  * @ops: the operations supported by this family (private)
48d91824c0SJohannes Berg  * @n_ops: number of operations supported by this family (private)
49482a8524SThomas Graf  */
50fd2c3ef7SEric Dumazet struct genl_family {
51482a8524SThomas Graf 	unsigned int		id;
52482a8524SThomas Graf 	unsigned int		hdrsize;
53482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
54482a8524SThomas Graf 	unsigned int		version;
55482a8524SThomas Graf 	unsigned int		maxattr;
56134e6375SJohannes Berg 	bool			netnsok;
57def31174SPravin B Shelar 	bool			parallel_ops;
58f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
59ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
60ff4c92d8SJohannes Berg 					    struct genl_info *info);
61f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
62ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
63ff4c92d8SJohannes Berg 					     struct genl_info *info);
64023e2cfaSJohannes Berg 	int			(*mcast_bind)(struct net *net, int group);
65023e2cfaSJohannes Berg 	void			(*mcast_unbind)(struct net *net, int group);
66482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
67f84f771dSJohannes Berg 	const struct genl_ops *	ops;		/* private */
682a94fe48SJohannes Berg 	const struct genl_multicast_group *mcgrps; /* private */
69d91824c0SJohannes Berg 	unsigned int		n_ops;		/* private */
702a94fe48SJohannes Berg 	unsigned int		n_mcgrps;	/* private */
712a94fe48SJohannes Berg 	unsigned int		mcgrp_offset;	/* private */
72482a8524SThomas Graf 	struct list_head	family_list;	/* private */
7333c6b1f6SPravin B Shelar 	struct module		*module;
74482a8524SThomas Graf };
75482a8524SThomas Graf 
76482a8524SThomas Graf /**
77482a8524SThomas Graf  * struct genl_info - receiving information
78482a8524SThomas Graf  * @snd_seq: sending sequence number
7915e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
80482a8524SThomas Graf  * @nlhdr: netlink message header
81482a8524SThomas Graf  * @genlhdr: generic netlink message header
82482a8524SThomas Graf  * @userhdr: user specific header
83482a8524SThomas Graf  * @attrs: netlink attributes
84ff4c92d8SJohannes Berg  * @_net: network namespace
85ff4c92d8SJohannes Berg  * @user_ptr: user pointers
86bb9b18fbSThomas Graf  * @dst_sk: destination socket
87482a8524SThomas Graf  */
88fd2c3ef7SEric Dumazet struct genl_info {
89482a8524SThomas Graf 	u32			snd_seq;
9015e47304SEric W. Biederman 	u32			snd_portid;
91482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
92482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
93482a8524SThomas Graf 	void *			userhdr;
94482a8524SThomas Graf 	struct nlattr **	attrs;
95134e6375SJohannes Berg #ifdef CONFIG_NET_NS
96134e6375SJohannes Berg 	struct net *		_net;
97134e6375SJohannes Berg #endif
98ff4c92d8SJohannes Berg 	void *			user_ptr[2];
99bb9b18fbSThomas Graf 	struct sock *		dst_sk;
100482a8524SThomas Graf };
101482a8524SThomas Graf 
102134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
103134e6375SJohannes Berg {
104c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
105134e6375SJohannes Berg }
106134e6375SJohannes Berg 
107134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
108134e6375SJohannes Berg {
109c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
110134e6375SJohannes Berg }
111134e6375SJohannes Berg 
112482a8524SThomas Graf /**
113482a8524SThomas Graf  * struct genl_ops - generic netlink operations
114482a8524SThomas Graf  * @cmd: command identifier
115ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
116482a8524SThomas Graf  * @flags: flags
117482a8524SThomas Graf  * @policy: attribute validation policy
118482a8524SThomas Graf  * @doit: standard command callback
119482a8524SThomas Graf  * @dumpit: callback for dumpers
120a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
121482a8524SThomas Graf  * @ops_list: operations list
122482a8524SThomas Graf  */
123fd2c3ef7SEric Dumazet struct genl_ops {
124ef7c79edSPatrick McHardy 	const struct nla_policy	*policy;
125482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
126482a8524SThomas Graf 				       struct genl_info *info);
127482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
128482a8524SThomas Graf 					 struct netlink_callback *cb);
129a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
1303f5ccd06SJohannes Berg 	u8			cmd;
1313f5ccd06SJohannes Berg 	u8			internal_flags;
1323f5ccd06SJohannes Berg 	u8			flags;
133482a8524SThomas Graf };
134482a8524SThomas Graf 
135ff2b94d2SJoe Perches int __genl_register_family(struct genl_family *family);
13633c6b1f6SPravin B Shelar 
13733c6b1f6SPravin B Shelar static inline int genl_register_family(struct genl_family *family)
13833c6b1f6SPravin B Shelar {
13933c6b1f6SPravin B Shelar 	family->module = THIS_MODULE;
14033c6b1f6SPravin B Shelar 	return __genl_register_family(family);
14133c6b1f6SPravin B Shelar }
14233c6b1f6SPravin B Shelar 
143568508aaSJohannes Berg /**
144568508aaSJohannes Berg  * genl_register_family_with_ops - register a generic netlink family with ops
145568508aaSJohannes Berg  * @family: generic netlink family
146568508aaSJohannes Berg  * @ops: operations to be registered
147568508aaSJohannes Berg  * @n_ops: number of elements to register
148568508aaSJohannes Berg  *
149568508aaSJohannes Berg  * Registers the specified family and operations from the specified table.
150568508aaSJohannes Berg  * Only one family may be registered with the same family name or identifier.
151568508aaSJohannes Berg  *
152568508aaSJohannes Berg  * The family id may equal GENL_ID_GENERATE causing an unique id to
153568508aaSJohannes Berg  * be automatically generated and assigned.
154568508aaSJohannes Berg  *
155568508aaSJohannes Berg  * Either a doit or dumpit callback must be specified for every registered
156568508aaSJohannes Berg  * operation or the function will fail. Only one operation structure per
157568508aaSJohannes Berg  * command identifier may be registered.
158568508aaSJohannes Berg  *
159568508aaSJohannes Berg  * See include/net/genetlink.h for more documenation on the operations
160568508aaSJohannes Berg  * structure.
161568508aaSJohannes Berg  *
162568508aaSJohannes Berg  * Return 0 on success or a negative error code.
163568508aaSJohannes Berg  */
1642a94fe48SJohannes Berg static inline int
1652a94fe48SJohannes Berg _genl_register_family_with_ops_grps(struct genl_family *family,
1662a94fe48SJohannes Berg 				    const struct genl_ops *ops, size_t n_ops,
1672a94fe48SJohannes Berg 				    const struct genl_multicast_group *mcgrps,
1682a94fe48SJohannes Berg 				    size_t n_mcgrps)
16933c6b1f6SPravin B Shelar {
17033c6b1f6SPravin B Shelar 	family->module = THIS_MODULE;
171568508aaSJohannes Berg 	family->ops = ops;
172568508aaSJohannes Berg 	family->n_ops = n_ops;
1732a94fe48SJohannes Berg 	family->mcgrps = mcgrps;
1742a94fe48SJohannes Berg 	family->n_mcgrps = n_mcgrps;
175568508aaSJohannes Berg 	return __genl_register_family(family);
17633c6b1f6SPravin B Shelar }
17733c6b1f6SPravin B Shelar 
178c53ed742SJohannes Berg #define genl_register_family_with_ops(family, ops)			\
1792a94fe48SJohannes Berg 	_genl_register_family_with_ops_grps((family),			\
1802a94fe48SJohannes Berg 					    (ops), ARRAY_SIZE(ops),	\
1812a94fe48SJohannes Berg 					    NULL, 0)
1822a94fe48SJohannes Berg #define genl_register_family_with_ops_groups(family, ops, grps)	\
1832a94fe48SJohannes Berg 	_genl_register_family_with_ops_grps((family),			\
1842a94fe48SJohannes Berg 					    (ops), ARRAY_SIZE(ops),	\
1852a94fe48SJohannes Berg 					    (grps), ARRAY_SIZE(grps))
186c53ed742SJohannes Berg 
187ff2b94d2SJoe Perches int genl_unregister_family(struct genl_family *family);
18868eb5503SJohannes Berg void genl_notify(struct genl_family *family,
18968eb5503SJohannes Berg 		 struct sk_buff *skb, struct net *net, u32 portid,
190263ba61dSPravin B Shelar 		 u32 group, struct nlmsghdr *nlh, gfp_t flags);
191482a8524SThomas Graf 
192bb9b18fbSThomas Graf struct sk_buff *genlmsg_new_unicast(size_t payload, struct genl_info *info,
193bb9b18fbSThomas Graf 				    gfp_t flags);
19415e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
195a46621a3SDenys Vlasenko 		  struct genl_family *family, int flags, u8 cmd);
196482a8524SThomas Graf 
197482a8524SThomas Graf /**
198670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
199670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
200670dc283SJohannes Berg  * @family: generic netlink family
201670dc283SJohannes Berg  *
202670dc283SJohannes Berg  * Returns pointer to netlink header.
203670dc283SJohannes Berg  */
204670dc283SJohannes Berg static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
205670dc283SJohannes Berg 					     struct genl_family *family)
206670dc283SJohannes Berg {
207670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
208670dc283SJohannes Berg 				   family->hdrsize -
209670dc283SJohannes Berg 				   GENL_HDRLEN -
210670dc283SJohannes Berg 				   NLMSG_HDRLEN);
211670dc283SJohannes Berg }
212670dc283SJohannes Berg 
213670dc283SJohannes Berg /**
214670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
215670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
216670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
217670dc283SJohannes Berg  * @family: generic netlink family
218670dc283SJohannes Berg  *
219670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
220670dc283SJohannes Berg  * simpler to use with generic netlink.
221670dc283SJohannes Berg  */
222670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
223670dc283SJohannes Berg 					      void *user_hdr,
224670dc283SJohannes Berg 					      struct genl_family *family)
225670dc283SJohannes Berg {
226670dc283SJohannes Berg 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
227670dc283SJohannes Berg }
228670dc283SJohannes Berg 
229670dc283SJohannes Berg /**
23017c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
23117c157c8SThomas Graf  * @skb: socket buffer holding the message
23217c157c8SThomas Graf  * @info: receiver info
23317c157c8SThomas Graf  * @family: generic netlink family
23417c157c8SThomas Graf  * @flags: netlink message flags
23517c157c8SThomas Graf  * @cmd: generic netlink command
23617c157c8SThomas Graf  *
23717c157c8SThomas Graf  * Returns pointer to user specific header
23817c157c8SThomas Graf  */
23917c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
24017c157c8SThomas Graf 				      struct genl_info *info,
24117c157c8SThomas Graf 				      struct genl_family *family,
24217c157c8SThomas Graf 				      int flags, u8 cmd)
24317c157c8SThomas Graf {
24415e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
24517c157c8SThomas Graf 			   flags, cmd);
24617c157c8SThomas Graf }
24717c157c8SThomas Graf 
24817c157c8SThomas Graf /**
249482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
250482a8524SThomas Graf  * @skb: socket buffer the message is stored in
251482a8524SThomas Graf  * @hdr: user specific header
252482a8524SThomas Graf  */
253482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
254482a8524SThomas Graf {
255482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
256482a8524SThomas Graf }
257482a8524SThomas Graf 
258482a8524SThomas Graf /**
259482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
260482a8524SThomas Graf  * @skb: socket buffer the message is stored in
261482a8524SThomas Graf  * @hdr: generic netlink message header
262482a8524SThomas Graf  */
263bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
264482a8524SThomas Graf {
26538db9e1dSJulia Lawall 	if (hdr)
266bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
267482a8524SThomas Graf }
268482a8524SThomas Graf 
269482a8524SThomas Graf /**
270134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
27168eb5503SJohannes Berg  * @family: the generic netlink family
272134e6375SJohannes Berg  * @net: the net namespace
273134e6375SJohannes Berg  * @skb: netlink message as socket buffer
27415e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2752a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
276134e6375SJohannes Berg  * @flags: allocation flags
277134e6375SJohannes Berg  */
27868eb5503SJohannes Berg static inline int genlmsg_multicast_netns(struct genl_family *family,
27968eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
28015e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
281134e6375SJohannes Berg {
282220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
2832a94fe48SJohannes Berg 		return -EINVAL;
2842a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
28515e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
286134e6375SJohannes Berg }
287134e6375SJohannes Berg 
288134e6375SJohannes Berg /**
289134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
29068eb5503SJohannes Berg  * @family: the generic netlink family
291482a8524SThomas Graf  * @skb: netlink message as socket buffer
29215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2932a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
294d387f6adSThomas Graf  * @flags: allocation flags
295482a8524SThomas Graf  */
29668eb5503SJohannes Berg static inline int genlmsg_multicast(struct genl_family *family,
29768eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
298d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
299482a8524SThomas Graf {
30068eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
30168eb5503SJohannes Berg 				       portid, group, flags);
302482a8524SThomas Graf }
303482a8524SThomas Graf 
304482a8524SThomas Graf /**
305134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
30668eb5503SJohannes Berg  * @family: the generic netlink family
307134e6375SJohannes Berg  * @skb: netlink message as socket buffer
30815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3092a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
310134e6375SJohannes Berg  * @flags: allocation flags
311134e6375SJohannes Berg  *
312134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
313134e6375SJohannes Berg  */
31468eb5503SJohannes Berg int genlmsg_multicast_allns(struct genl_family *family,
31568eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
316134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
317134e6375SJohannes Berg 
318134e6375SJohannes Berg /**
319482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
320482a8524SThomas Graf  * @skb: netlink message as socket buffer
32115e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
322482a8524SThomas Graf  */
32315e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
324482a8524SThomas Graf {
32515e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
326482a8524SThomas Graf }
327482a8524SThomas Graf 
328fb0ba6bdSBalbir Singh /**
32981878d27SThomas Graf  * genlmsg_reply - reply to a request
33081878d27SThomas Graf  * @skb: netlink message to be sent back
33181878d27SThomas Graf  * @info: receiver information
33281878d27SThomas Graf  */
33381878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
33481878d27SThomas Graf {
33515e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
33681878d27SThomas Graf }
33781878d27SThomas Graf 
33881878d27SThomas Graf /**
339fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
34070f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
341fb0ba6bdSBalbir Singh  */
342fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
343fb0ba6bdSBalbir Singh {
344fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
345fb0ba6bdSBalbir Singh }
346fb0ba6bdSBalbir Singh 
347fb0ba6bdSBalbir Singh /**
348fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
349fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
350fb0ba6bdSBalbir Singh  */
351fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
352fb0ba6bdSBalbir Singh {
353fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
354fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
355fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
356fb0ba6bdSBalbir Singh }
357fb0ba6bdSBalbir Singh 
35817db952cSBalbir Singh /**
35917db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
36017db952cSBalbir Singh  * @payload: length of message payload
36117db952cSBalbir Singh  */
36217db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
36317db952cSBalbir Singh {
36417db952cSBalbir Singh 	return GENL_HDRLEN + payload;
36517db952cSBalbir Singh }
36617db952cSBalbir Singh 
36717db952cSBalbir Singh /**
36817db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
36917db952cSBalbir Singh  * @payload: length of message payload
37017db952cSBalbir Singh  */
37117db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
37217db952cSBalbir Singh {
37317db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
37417db952cSBalbir Singh }
37517db952cSBalbir Singh 
3763dabc715SThomas Graf /**
3773dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
3783dabc715SThomas Graf  * @payload: size of the message payload
3793dabc715SThomas Graf  * @flags: the type of memory to allocate.
3803dabc715SThomas Graf  */
3813dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
3823dabc715SThomas Graf {
3833dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
3843dabc715SThomas Graf }
3853dabc715SThomas Graf 
38662b68e99SJohannes Berg /**
38762b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
38868eb5503SJohannes Berg  * @family: the generic netlink family
38962b68e99SJohannes Berg  * @net: the network namespace to report the error to
39062b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
39162b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
3922a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
39362b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
39462b68e99SJohannes Berg  *
39562b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
39662b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
39762b68e99SJohannes Berg  */
39868eb5503SJohannes Berg static inline int genl_set_err(struct genl_family *family, struct net *net,
39968eb5503SJohannes Berg 			       u32 portid, u32 group, int code)
40062b68e99SJohannes Berg {
40191398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
40291398a09SJohannes Berg 		return -EINVAL;
40391398a09SJohannes Berg 	group = family->mcgrp_offset + group;
40462b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
40562b68e99SJohannes Berg }
4063dabc715SThomas Graf 
4070d566379SNicolas Dichtel static inline int genl_has_listeners(struct genl_family *family,
408f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4090d566379SNicolas Dichtel {
4100d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4110d566379SNicolas Dichtel 		return -EINVAL;
4120d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
413f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4140d566379SNicolas Dichtel }
415482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
416