xref: /openbmc/linux/include/net/genetlink.h (revision 3ddf9b431b931544dc2f94e8f6a055ff501436fa)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H
3482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H
4482a8524SThomas Graf 
5482a8524SThomas Graf #include <linux/genetlink.h>
6482a8524SThomas Graf #include <net/netlink.h>
7134e6375SJohannes Berg #include <net/net_namespace.h>
8482a8524SThomas Graf 
958050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
1058050fceSThomas Graf 
11482a8524SThomas Graf /**
122dbba6f7SJohannes Berg  * struct genl_multicast_group - generic netlink multicast group
132dbba6f7SJohannes Berg  * @name: name of the multicast group, names are per-family
142dbba6f7SJohannes Berg  */
15fd2c3ef7SEric Dumazet struct genl_multicast_group {
162dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
172dbba6f7SJohannes Berg };
182dbba6f7SJohannes Berg 
19ff4c92d8SJohannes Berg struct genl_ops;
20ff4c92d8SJohannes Berg struct genl_info;
21ff4c92d8SJohannes Berg 
222dbba6f7SJohannes Berg /**
23482a8524SThomas Graf  * struct genl_family - generic netlink family
24a07ea4d9SJohannes Berg  * @id: protocol family identifier (private)
25482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
26482a8524SThomas Graf  * @name: name of family
27482a8524SThomas Graf  * @version: protocol version
28482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
293b0f31f2SJohannes Berg  * @policy: netlink policy
30134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
31134e6375SJohannes Berg  *	namespaces and should be presented in all of them
32f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
33f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
34ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
35ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
36ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
37ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
38489111e5SJohannes Berg  * @mcgrps: multicast groups used by this family
39489111e5SJohannes Berg  * @n_mcgrps: number of multicast groups
402a94fe48SJohannes Berg  * @mcgrp_offset: starting number of multicast group IDs in this family
41489111e5SJohannes Berg  *	(private)
42489111e5SJohannes Berg  * @ops: the operations supported by this family
43489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
44482a8524SThomas Graf  */
45fd2c3ef7SEric Dumazet struct genl_family {
4698e4321bSDavid S. Miller 	int			id;		/* private */
47482a8524SThomas Graf 	unsigned int		hdrsize;
48482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
49482a8524SThomas Graf 	unsigned int		version;
50482a8524SThomas Graf 	unsigned int		maxattr;
51134e6375SJohannes Berg 	bool			netnsok;
52def31174SPravin B Shelar 	bool			parallel_ops;
533b0f31f2SJohannes Berg 	const struct nla_policy *policy;
54f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
55ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
56ff4c92d8SJohannes Berg 					    struct genl_info *info);
57f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
58ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
59ff4c92d8SJohannes Berg 					     struct genl_info *info);
60489111e5SJohannes Berg 	const struct genl_ops *	ops;
61489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
62489111e5SJohannes Berg 	unsigned int		n_ops;
63489111e5SJohannes Berg 	unsigned int		n_mcgrps;
642a94fe48SJohannes Berg 	unsigned int		mcgrp_offset;	/* private */
6533c6b1f6SPravin B Shelar 	struct module		*module;
66482a8524SThomas Graf };
67482a8524SThomas Graf 
68482a8524SThomas Graf /**
69482a8524SThomas Graf  * struct genl_info - receiving information
70482a8524SThomas Graf  * @snd_seq: sending sequence number
7115e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
72482a8524SThomas Graf  * @nlhdr: netlink message header
73482a8524SThomas Graf  * @genlhdr: generic netlink message header
74482a8524SThomas Graf  * @userhdr: user specific header
75482a8524SThomas Graf  * @attrs: netlink attributes
76ff4c92d8SJohannes Berg  * @_net: network namespace
77ff4c92d8SJohannes Berg  * @user_ptr: user pointers
787ab606d1SJohannes Berg  * @extack: extended ACK report struct
79482a8524SThomas Graf  */
80fd2c3ef7SEric Dumazet struct genl_info {
81482a8524SThomas Graf 	u32			snd_seq;
8215e47304SEric W. Biederman 	u32			snd_portid;
83482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
84482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
85482a8524SThomas Graf 	void *			userhdr;
86482a8524SThomas Graf 	struct nlattr **	attrs;
870c5c9fb5SEric W. Biederman 	possible_net_t		_net;
88ff4c92d8SJohannes Berg 	void *			user_ptr[2];
897ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
90482a8524SThomas Graf };
91482a8524SThomas Graf 
92134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
93134e6375SJohannes Berg {
94c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
95134e6375SJohannes Berg }
96134e6375SJohannes Berg 
97134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
98134e6375SJohannes Berg {
99c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
100134e6375SJohannes Berg }
101134e6375SJohannes Berg 
1027ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1037ab606d1SJohannes Berg 
1047ab606d1SJohannes Berg static inline int genl_err_attr(struct genl_info *info, int err,
1056fce10f7SMichal Kubecek 				const struct nlattr *attr)
1067ab606d1SJohannes Berg {
1077ab606d1SJohannes Berg 	info->extack->bad_attr = attr;
1087ab606d1SJohannes Berg 
1097ab606d1SJohannes Berg 	return err;
1107ab606d1SJohannes Berg }
1117ab606d1SJohannes Berg 
112ef6243acSJohannes Berg enum genl_validate_flags {
113ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
114ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
115ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
116ef6243acSJohannes Berg };
117ef6243acSJohannes Berg 
118482a8524SThomas Graf /**
1191927f41aSJiri Pirko  * struct genl_info - info that is available during dumpit op call
120bf813b0aSJiri Pirko  * @family: generic netlink family - for internal genl code usage
1211927f41aSJiri Pirko  * @ops: generic netlink ops - for internal genl code usage
122bf813b0aSJiri Pirko  * @attrs: netlink attributes
1231927f41aSJiri Pirko  */
1241927f41aSJiri Pirko struct genl_dumpit_info {
125bf813b0aSJiri Pirko 	const struct genl_family *family;
1261927f41aSJiri Pirko 	const struct genl_ops *ops;
127bf813b0aSJiri Pirko 	struct nlattr **attrs;
1281927f41aSJiri Pirko };
1291927f41aSJiri Pirko 
1301927f41aSJiri Pirko static inline const struct genl_dumpit_info *
1311927f41aSJiri Pirko genl_dumpit_info(struct netlink_callback *cb)
1321927f41aSJiri Pirko {
1331927f41aSJiri Pirko 	return cb->data;
1341927f41aSJiri Pirko }
1351927f41aSJiri Pirko 
1361927f41aSJiri Pirko /**
137482a8524SThomas Graf  * struct genl_ops - generic netlink operations
138482a8524SThomas Graf  * @cmd: command identifier
139ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
140482a8524SThomas Graf  * @flags: flags
141*3ddf9b43SJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
142482a8524SThomas Graf  * @doit: standard command callback
143fc9e50f5STom Herbert  * @start: start callback for dumps
144482a8524SThomas Graf  * @dumpit: callback for dumpers
145a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
146482a8524SThomas Graf  */
147fd2c3ef7SEric Dumazet struct genl_ops {
148482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
149482a8524SThomas Graf 				       struct genl_info *info);
150fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
151482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
152482a8524SThomas Graf 					 struct netlink_callback *cb);
153a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
1543f5ccd06SJohannes Berg 	u8			cmd;
1553f5ccd06SJohannes Berg 	u8			internal_flags;
1563f5ccd06SJohannes Berg 	u8			flags;
157ef6243acSJohannes Berg 	u8			validate;
158482a8524SThomas Graf };
159482a8524SThomas Graf 
160489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
1612ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
1622ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
16392c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
164482a8524SThomas Graf 
16515e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
1662ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
167482a8524SThomas Graf 
168482a8524SThomas Graf /**
169670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
170670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
171670dc283SJohannes Berg  *
172670dc283SJohannes Berg  * Returns pointer to netlink header.
173670dc283SJohannes Berg  */
1740a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
175670dc283SJohannes Berg {
176670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
177670dc283SJohannes Berg 				   GENL_HDRLEN -
178670dc283SJohannes Berg 				   NLMSG_HDRLEN);
179670dc283SJohannes Berg }
180670dc283SJohannes Berg 
181670dc283SJohannes Berg /**
1828cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
1837b1883ceSJoe Stringer  * @nlh: netlink message header
1847b1883ceSJoe Stringer  * @family: genetlink message family
1857b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
1867b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
1877b1883ceSJoe Stringer  * @policy: validation policy
188fceb6435SJohannes Berg  * @extack: extended ACK report struct
189fceb6435SJohannes Berg  */
1908cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
1917b1883ceSJoe Stringer 					   const struct genl_family *family,
1927b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
193fceb6435SJohannes Berg 					   const struct nla_policy *policy,
194fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
1957b1883ceSJoe Stringer {
1968cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
1978cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
1987b1883ceSJoe Stringer }
1997b1883ceSJoe Stringer 
2007b1883ceSJoe Stringer /**
2013de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
2023de64403SJohannes Berg  * @nlh: netlink message header
2033de64403SJohannes Berg  * @family: genetlink message family
2043de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
2053de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
2063de64403SJohannes Berg  * @policy: validation policy
2073de64403SJohannes Berg  * @extack: extended ACK report struct
2083de64403SJohannes Berg  */
2093de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
2103de64403SJohannes Berg 				const struct genl_family *family,
2113de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
2123de64403SJohannes Berg 				const struct nla_policy *policy,
2133de64403SJohannes Berg 				struct netlink_ext_ack *extack)
2143de64403SJohannes Berg {
2153de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2163de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
2173de64403SJohannes Berg }
2183de64403SJohannes Berg 
2193de64403SJohannes Berg /**
220670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
221670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
222670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
223670dc283SJohannes Berg  *
224670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
225670dc283SJohannes Berg  * simpler to use with generic netlink.
226670dc283SJohannes Berg  */
227670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
2280a833c29SMichal Kubecek 					      void *user_hdr)
229670dc283SJohannes Berg {
2300a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
231670dc283SJohannes Berg }
232670dc283SJohannes Berg 
233670dc283SJohannes Berg /**
23417c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
23517c157c8SThomas Graf  * @skb: socket buffer holding the message
23617c157c8SThomas Graf  * @info: receiver info
23717c157c8SThomas Graf  * @family: generic netlink family
23817c157c8SThomas Graf  * @flags: netlink message flags
23917c157c8SThomas Graf  * @cmd: generic netlink command
24017c157c8SThomas Graf  *
24117c157c8SThomas Graf  * Returns pointer to user specific header
24217c157c8SThomas Graf  */
24317c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
24417c157c8SThomas Graf 				      struct genl_info *info,
2452ae0f17dSJohannes Berg 				      const struct genl_family *family,
24617c157c8SThomas Graf 				      int flags, u8 cmd)
24717c157c8SThomas Graf {
24815e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
24917c157c8SThomas Graf 			   flags, cmd);
25017c157c8SThomas Graf }
25117c157c8SThomas Graf 
25217c157c8SThomas Graf /**
253482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
254482a8524SThomas Graf  * @skb: socket buffer the message is stored in
255482a8524SThomas Graf  * @hdr: user specific header
256482a8524SThomas Graf  */
257053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
258482a8524SThomas Graf {
259053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
260482a8524SThomas Graf }
261482a8524SThomas Graf 
262482a8524SThomas Graf /**
263482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
264482a8524SThomas Graf  * @skb: socket buffer the message is stored in
265482a8524SThomas Graf  * @hdr: generic netlink message header
266482a8524SThomas Graf  */
267bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
268482a8524SThomas Graf {
26938db9e1dSJulia Lawall 	if (hdr)
270bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
271482a8524SThomas Graf }
272482a8524SThomas Graf 
273482a8524SThomas Graf /**
274134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
27568eb5503SJohannes Berg  * @family: the generic netlink family
276134e6375SJohannes Berg  * @net: the net namespace
277134e6375SJohannes Berg  * @skb: netlink message as socket buffer
27815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2792a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
280134e6375SJohannes Berg  * @flags: allocation flags
281134e6375SJohannes Berg  */
2822ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
28368eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
28415e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
285134e6375SJohannes Berg {
286220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
2872a94fe48SJohannes Berg 		return -EINVAL;
2882a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
28915e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
290134e6375SJohannes Berg }
291134e6375SJohannes Berg 
292134e6375SJohannes Berg /**
293134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
29468eb5503SJohannes Berg  * @family: the generic netlink family
295482a8524SThomas Graf  * @skb: netlink message as socket buffer
29615e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2972a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
298d387f6adSThomas Graf  * @flags: allocation flags
299482a8524SThomas Graf  */
3002ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
30168eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
302d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
303482a8524SThomas Graf {
30468eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
30568eb5503SJohannes Berg 				       portid, group, flags);
306482a8524SThomas Graf }
307482a8524SThomas Graf 
308482a8524SThomas Graf /**
309134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
31068eb5503SJohannes Berg  * @family: the generic netlink family
311134e6375SJohannes Berg  * @skb: netlink message as socket buffer
31215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3132a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
314134e6375SJohannes Berg  * @flags: allocation flags
315134e6375SJohannes Berg  *
316134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
317134e6375SJohannes Berg  */
3182ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
31968eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
320134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
321134e6375SJohannes Berg 
322134e6375SJohannes Berg /**
323482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
324482a8524SThomas Graf  * @skb: netlink message as socket buffer
32515e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
326482a8524SThomas Graf  */
32715e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
328482a8524SThomas Graf {
32915e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
330482a8524SThomas Graf }
331482a8524SThomas Graf 
332fb0ba6bdSBalbir Singh /**
33381878d27SThomas Graf  * genlmsg_reply - reply to a request
33481878d27SThomas Graf  * @skb: netlink message to be sent back
33581878d27SThomas Graf  * @info: receiver information
33681878d27SThomas Graf  */
33781878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
33881878d27SThomas Graf {
33915e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
34081878d27SThomas Graf }
34181878d27SThomas Graf 
34281878d27SThomas Graf /**
343fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
34470f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
345fb0ba6bdSBalbir Singh  */
346fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
347fb0ba6bdSBalbir Singh {
348fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
349fb0ba6bdSBalbir Singh }
350fb0ba6bdSBalbir Singh 
351fb0ba6bdSBalbir Singh /**
352fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
353fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
354fb0ba6bdSBalbir Singh  */
355fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
356fb0ba6bdSBalbir Singh {
357fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
358fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
359fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
360fb0ba6bdSBalbir Singh }
361fb0ba6bdSBalbir Singh 
36217db952cSBalbir Singh /**
36317db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
36417db952cSBalbir Singh  * @payload: length of message payload
36517db952cSBalbir Singh  */
36617db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
36717db952cSBalbir Singh {
36817db952cSBalbir Singh 	return GENL_HDRLEN + payload;
36917db952cSBalbir Singh }
37017db952cSBalbir Singh 
37117db952cSBalbir Singh /**
37217db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
37317db952cSBalbir Singh  * @payload: length of message payload
37417db952cSBalbir Singh  */
37517db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
37617db952cSBalbir Singh {
37717db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
37817db952cSBalbir Singh }
37917db952cSBalbir Singh 
3803dabc715SThomas Graf /**
3813dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
3823dabc715SThomas Graf  * @payload: size of the message payload
3833dabc715SThomas Graf  * @flags: the type of memory to allocate.
3843dabc715SThomas Graf  */
3853dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
3863dabc715SThomas Graf {
3873dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
3883dabc715SThomas Graf }
3893dabc715SThomas Graf 
39062b68e99SJohannes Berg /**
39162b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
39268eb5503SJohannes Berg  * @family: the generic netlink family
39362b68e99SJohannes Berg  * @net: the network namespace to report the error to
39462b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
39562b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
3962a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
39762b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
39862b68e99SJohannes Berg  *
39962b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
40062b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
40162b68e99SJohannes Berg  */
4022ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
4032ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
4042ae0f17dSJohannes Berg 			       u32 group, int code)
40562b68e99SJohannes Berg {
40691398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
40791398a09SJohannes Berg 		return -EINVAL;
40891398a09SJohannes Berg 	group = family->mcgrp_offset + group;
40962b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
41062b68e99SJohannes Berg }
4113dabc715SThomas Graf 
4122ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
413f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4140d566379SNicolas Dichtel {
4150d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4160d566379SNicolas Dichtel 		return -EINVAL;
4170d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
418f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4190d566379SNicolas Dichtel }
420482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
421