xref: /openbmc/linux/include/net/genetlink.h (revision 4d54cc32112d8d8b0667559c9309f1a6f764f70b)
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];
17*4d54cc32SFlorian Westphal 	u8			flags;
182dbba6f7SJohannes Berg };
192dbba6f7SJohannes Berg 
20ff4c92d8SJohannes Berg struct genl_ops;
21ff4c92d8SJohannes Berg struct genl_info;
22ff4c92d8SJohannes Berg 
232dbba6f7SJohannes Berg /**
24482a8524SThomas Graf  * struct genl_family - generic netlink family
25a07ea4d9SJohannes Berg  * @id: protocol family identifier (private)
26482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
27482a8524SThomas Graf  * @name: name of family
28482a8524SThomas Graf  * @version: protocol version
29482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
303b0f31f2SJohannes Berg  * @policy: netlink policy
31134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
32134e6375SJohannes Berg  *	namespaces and should be presented in all of them
33f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
34f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
35ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
36ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
37ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
38ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
39489111e5SJohannes Berg  * @mcgrps: multicast groups used by this family
40489111e5SJohannes Berg  * @n_mcgrps: number of multicast groups
412a94fe48SJohannes Berg  * @mcgrp_offset: starting number of multicast group IDs in this family
42489111e5SJohannes Berg  *	(private)
43489111e5SJohannes Berg  * @ops: the operations supported by this family
44489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
450b588afdSJakub Kicinski  * @small_ops: the small-struct operations supported by this family
460b588afdSJakub Kicinski  * @n_small_ops: number of small-struct operations supported by this family
47482a8524SThomas Graf  */
48fd2c3ef7SEric Dumazet struct genl_family {
4998e4321bSDavid S. Miller 	int			id;		/* private */
50482a8524SThomas Graf 	unsigned int		hdrsize;
51482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
52482a8524SThomas Graf 	unsigned int		version;
53482a8524SThomas Graf 	unsigned int		maxattr;
54e5086736SJakub Kicinski 	unsigned int		mcgrp_offset;	/* private */
55e5086736SJakub Kicinski 	u8			netnsok:1;
56e5086736SJakub Kicinski 	u8			parallel_ops:1;
57e5086736SJakub Kicinski 	u8			n_ops;
580b588afdSJakub Kicinski 	u8			n_small_ops;
59e5086736SJakub Kicinski 	u8			n_mcgrps;
603b0f31f2SJohannes Berg 	const struct nla_policy *policy;
61f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
62ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
63ff4c92d8SJohannes Berg 					    struct genl_info *info);
64f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
65ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
66ff4c92d8SJohannes Berg 					     struct genl_info *info);
67489111e5SJohannes Berg 	const struct genl_ops *	ops;
680b588afdSJakub Kicinski 	const struct genl_small_ops *small_ops;
69489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
7033c6b1f6SPravin B Shelar 	struct module		*module;
71482a8524SThomas Graf };
72482a8524SThomas Graf 
73482a8524SThomas Graf /**
74482a8524SThomas Graf  * struct genl_info - receiving information
75482a8524SThomas Graf  * @snd_seq: sending sequence number
7615e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
77482a8524SThomas Graf  * @nlhdr: netlink message header
78482a8524SThomas Graf  * @genlhdr: generic netlink message header
79482a8524SThomas Graf  * @userhdr: user specific header
80482a8524SThomas Graf  * @attrs: netlink attributes
81ff4c92d8SJohannes Berg  * @_net: network namespace
82ff4c92d8SJohannes Berg  * @user_ptr: user pointers
837ab606d1SJohannes Berg  * @extack: extended ACK report struct
84482a8524SThomas Graf  */
85fd2c3ef7SEric Dumazet struct genl_info {
86482a8524SThomas Graf 	u32			snd_seq;
8715e47304SEric W. Biederman 	u32			snd_portid;
88482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
89482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
90482a8524SThomas Graf 	void *			userhdr;
91482a8524SThomas Graf 	struct nlattr **	attrs;
920c5c9fb5SEric W. Biederman 	possible_net_t		_net;
93ff4c92d8SJohannes Berg 	void *			user_ptr[2];
947ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
95482a8524SThomas Graf };
96482a8524SThomas Graf 
97134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
98134e6375SJohannes Berg {
99c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
100134e6375SJohannes Berg }
101134e6375SJohannes Berg 
102134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
103134e6375SJohannes Berg {
104c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
105134e6375SJohannes Berg }
106134e6375SJohannes Berg 
1077ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1087ab606d1SJohannes Berg 
109ef6243acSJohannes Berg enum genl_validate_flags {
110ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
111ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
112ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
113ef6243acSJohannes Berg };
114ef6243acSJohannes Berg 
115482a8524SThomas Graf /**
1160b588afdSJakub Kicinski  * struct genl_small_ops - generic netlink operations (small version)
1170b588afdSJakub Kicinski  * @cmd: command identifier
1180b588afdSJakub Kicinski  * @internal_flags: flags used by the family
1190b588afdSJakub Kicinski  * @flags: flags
1200b588afdSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
1210b588afdSJakub Kicinski  * @doit: standard command callback
1220b588afdSJakub Kicinski  * @dumpit: callback for dumpers
1230b588afdSJakub Kicinski  *
1240b588afdSJakub Kicinski  * This is a cut-down version of struct genl_ops for users who don't need
1250b588afdSJakub Kicinski  * most of the ancillary infra and want to save space.
1261927f41aSJiri Pirko  */
1270b588afdSJakub Kicinski struct genl_small_ops {
1280b588afdSJakub Kicinski 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
1290b588afdSJakub Kicinski 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
1300b588afdSJakub Kicinski 	u8	cmd;
1310b588afdSJakub Kicinski 	u8	internal_flags;
1320b588afdSJakub Kicinski 	u8	flags;
1330b588afdSJakub Kicinski 	u8	validate;
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
14148526a0fSJakub Kicinski  * @maxattr: maximum number of attributes supported
14248526a0fSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
1433ddf9b43SJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
144482a8524SThomas Graf  * @doit: standard command callback
145fc9e50f5STom Herbert  * @start: start callback for dumps
146482a8524SThomas Graf  * @dumpit: callback for dumpers
147a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
148482a8524SThomas Graf  */
149fd2c3ef7SEric Dumazet struct genl_ops {
150482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
151482a8524SThomas Graf 				       struct genl_info *info);
152fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
153482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
154482a8524SThomas Graf 					 struct netlink_callback *cb);
155a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
15648526a0fSJakub Kicinski 	const struct nla_policy *policy;
15748526a0fSJakub Kicinski 	unsigned int		maxattr;
1583f5ccd06SJohannes Berg 	u8			cmd;
1593f5ccd06SJohannes Berg 	u8			internal_flags;
1603f5ccd06SJohannes Berg 	u8			flags;
161ef6243acSJohannes Berg 	u8			validate;
162482a8524SThomas Graf };
163482a8524SThomas Graf 
1640b588afdSJakub Kicinski /**
1650b588afdSJakub Kicinski  * struct genl_info - info that is available during dumpit op call
1660b588afdSJakub Kicinski  * @family: generic netlink family - for internal genl code usage
1670b588afdSJakub Kicinski  * @ops: generic netlink ops - for internal genl code usage
1680b588afdSJakub Kicinski  * @attrs: netlink attributes
1690b588afdSJakub Kicinski  */
1700b588afdSJakub Kicinski struct genl_dumpit_info {
1710b588afdSJakub Kicinski 	const struct genl_family *family;
1720b588afdSJakub Kicinski 	struct genl_ops op;
1730b588afdSJakub Kicinski 	struct nlattr **attrs;
1740b588afdSJakub Kicinski };
1750b588afdSJakub Kicinski 
1760b588afdSJakub Kicinski static inline const struct genl_dumpit_info *
1770b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb)
1780b588afdSJakub Kicinski {
1790b588afdSJakub Kicinski 	return cb->data;
1800b588afdSJakub Kicinski }
1810b588afdSJakub Kicinski 
182489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
1832ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
1842ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
18592c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
186482a8524SThomas Graf 
18715e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
1882ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
189482a8524SThomas Graf 
190482a8524SThomas Graf /**
191670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
192670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
193670dc283SJohannes Berg  *
194670dc283SJohannes Berg  * Returns pointer to netlink header.
195670dc283SJohannes Berg  */
1960a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
197670dc283SJohannes Berg {
198670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
199670dc283SJohannes Berg 				   GENL_HDRLEN -
200670dc283SJohannes Berg 				   NLMSG_HDRLEN);
201670dc283SJohannes Berg }
202670dc283SJohannes Berg 
203670dc283SJohannes Berg /**
2048cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
2057b1883ceSJoe Stringer  * @nlh: netlink message header
2067b1883ceSJoe Stringer  * @family: genetlink message family
2077b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
2087b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
2097b1883ceSJoe Stringer  * @policy: validation policy
210fceb6435SJohannes Berg  * @extack: extended ACK report struct
211fceb6435SJohannes Berg  */
2128cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
2137b1883ceSJoe Stringer 					   const struct genl_family *family,
2147b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
215fceb6435SJohannes Berg 					   const struct nla_policy *policy,
216fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
2177b1883ceSJoe Stringer {
2188cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2198cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
2207b1883ceSJoe Stringer }
2217b1883ceSJoe Stringer 
2227b1883ceSJoe Stringer /**
2233de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
2243de64403SJohannes Berg  * @nlh: netlink message header
2253de64403SJohannes Berg  * @family: genetlink message family
2263de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
2273de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
2283de64403SJohannes Berg  * @policy: validation policy
2293de64403SJohannes Berg  * @extack: extended ACK report struct
2303de64403SJohannes Berg  */
2313de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
2323de64403SJohannes Berg 				const struct genl_family *family,
2333de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
2343de64403SJohannes Berg 				const struct nla_policy *policy,
2353de64403SJohannes Berg 				struct netlink_ext_ack *extack)
2363de64403SJohannes Berg {
2373de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2383de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
2393de64403SJohannes Berg }
2403de64403SJohannes Berg 
2413de64403SJohannes Berg /**
242670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
243670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
244670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
245670dc283SJohannes Berg  *
246670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
247670dc283SJohannes Berg  * simpler to use with generic netlink.
248670dc283SJohannes Berg  */
249670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
2500a833c29SMichal Kubecek 					      void *user_hdr)
251670dc283SJohannes Berg {
2520a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
253670dc283SJohannes Berg }
254670dc283SJohannes Berg 
255670dc283SJohannes Berg /**
25617c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
25717c157c8SThomas Graf  * @skb: socket buffer holding the message
25817c157c8SThomas Graf  * @info: receiver info
25917c157c8SThomas Graf  * @family: generic netlink family
26017c157c8SThomas Graf  * @flags: netlink message flags
26117c157c8SThomas Graf  * @cmd: generic netlink command
26217c157c8SThomas Graf  *
26317c157c8SThomas Graf  * Returns pointer to user specific header
26417c157c8SThomas Graf  */
26517c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
26617c157c8SThomas Graf 				      struct genl_info *info,
2672ae0f17dSJohannes Berg 				      const struct genl_family *family,
26817c157c8SThomas Graf 				      int flags, u8 cmd)
26917c157c8SThomas Graf {
27015e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
27117c157c8SThomas Graf 			   flags, cmd);
27217c157c8SThomas Graf }
27317c157c8SThomas Graf 
27417c157c8SThomas Graf /**
275482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
276482a8524SThomas Graf  * @skb: socket buffer the message is stored in
277482a8524SThomas Graf  * @hdr: user specific header
278482a8524SThomas Graf  */
279053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
280482a8524SThomas Graf {
281053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
282482a8524SThomas Graf }
283482a8524SThomas Graf 
284482a8524SThomas Graf /**
285482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
286482a8524SThomas Graf  * @skb: socket buffer the message is stored in
287482a8524SThomas Graf  * @hdr: generic netlink message header
288482a8524SThomas Graf  */
289bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
290482a8524SThomas Graf {
29138db9e1dSJulia Lawall 	if (hdr)
292bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
293482a8524SThomas Graf }
294482a8524SThomas Graf 
295482a8524SThomas Graf /**
296134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
29768eb5503SJohannes Berg  * @family: the generic netlink family
298134e6375SJohannes Berg  * @net: the net namespace
299134e6375SJohannes Berg  * @skb: netlink message as socket buffer
30015e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3012a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
302134e6375SJohannes Berg  * @flags: allocation flags
303134e6375SJohannes Berg  */
3042ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
30568eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
30615e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
307134e6375SJohannes Berg {
308220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
3092a94fe48SJohannes Berg 		return -EINVAL;
3102a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
31115e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
312134e6375SJohannes Berg }
313134e6375SJohannes Berg 
314134e6375SJohannes Berg /**
315134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
31668eb5503SJohannes Berg  * @family: the generic netlink family
317482a8524SThomas Graf  * @skb: netlink message as socket buffer
31815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3192a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
320d387f6adSThomas Graf  * @flags: allocation flags
321482a8524SThomas Graf  */
3222ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
32368eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
324d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
325482a8524SThomas Graf {
32668eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
32768eb5503SJohannes Berg 				       portid, group, flags);
328482a8524SThomas Graf }
329482a8524SThomas Graf 
330482a8524SThomas Graf /**
331134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
33268eb5503SJohannes Berg  * @family: the generic netlink family
333134e6375SJohannes Berg  * @skb: netlink message as socket buffer
33415e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3352a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
336134e6375SJohannes Berg  * @flags: allocation flags
337134e6375SJohannes Berg  *
338134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
339134e6375SJohannes Berg  */
3402ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
34168eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
342134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
343134e6375SJohannes Berg 
344134e6375SJohannes Berg /**
345482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
346482a8524SThomas Graf  * @skb: netlink message as socket buffer
34715e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
348482a8524SThomas Graf  */
34915e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
350482a8524SThomas Graf {
35115e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
352482a8524SThomas Graf }
353482a8524SThomas Graf 
354fb0ba6bdSBalbir Singh /**
35581878d27SThomas Graf  * genlmsg_reply - reply to a request
35681878d27SThomas Graf  * @skb: netlink message to be sent back
35781878d27SThomas Graf  * @info: receiver information
35881878d27SThomas Graf  */
35981878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
36081878d27SThomas Graf {
36115e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
36281878d27SThomas Graf }
36381878d27SThomas Graf 
36481878d27SThomas Graf /**
365fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
36670f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
367fb0ba6bdSBalbir Singh  */
368fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
369fb0ba6bdSBalbir Singh {
370fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
371fb0ba6bdSBalbir Singh }
372fb0ba6bdSBalbir Singh 
373fb0ba6bdSBalbir Singh /**
374fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
375fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
376fb0ba6bdSBalbir Singh  */
377fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
378fb0ba6bdSBalbir Singh {
379fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
380fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
381fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
382fb0ba6bdSBalbir Singh }
383fb0ba6bdSBalbir Singh 
38417db952cSBalbir Singh /**
38517db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
38617db952cSBalbir Singh  * @payload: length of message payload
38717db952cSBalbir Singh  */
38817db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
38917db952cSBalbir Singh {
39017db952cSBalbir Singh 	return GENL_HDRLEN + payload;
39117db952cSBalbir Singh }
39217db952cSBalbir Singh 
39317db952cSBalbir Singh /**
39417db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
39517db952cSBalbir Singh  * @payload: length of message payload
39617db952cSBalbir Singh  */
39717db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
39817db952cSBalbir Singh {
39917db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
40017db952cSBalbir Singh }
40117db952cSBalbir Singh 
4023dabc715SThomas Graf /**
4033dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
4043dabc715SThomas Graf  * @payload: size of the message payload
4053dabc715SThomas Graf  * @flags: the type of memory to allocate.
4063dabc715SThomas Graf  */
4073dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
4083dabc715SThomas Graf {
4093dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
4103dabc715SThomas Graf }
4113dabc715SThomas Graf 
41262b68e99SJohannes Berg /**
41362b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
41468eb5503SJohannes Berg  * @family: the generic netlink family
41562b68e99SJohannes Berg  * @net: the network namespace to report the error to
41662b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
41762b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
4182a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
41962b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
42062b68e99SJohannes Berg  *
42162b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
42262b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
42362b68e99SJohannes Berg  */
4242ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
4252ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
4262ae0f17dSJohannes Berg 			       u32 group, int code)
42762b68e99SJohannes Berg {
42891398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
42991398a09SJohannes Berg 		return -EINVAL;
43091398a09SJohannes Berg 	group = family->mcgrp_offset + group;
43162b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
43262b68e99SJohannes Berg }
4333dabc715SThomas Graf 
4342ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
435f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4360d566379SNicolas Dichtel {
4370d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4380d566379SNicolas Dichtel 		return -EINVAL;
4390d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
440f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4410d566379SNicolas Dichtel }
442482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
443