xref: /openbmc/linux/include/net/genetlink.h (revision 48526a0f4ca2b484cab4318dc0b2c2be1d8685b7)
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
440b588afdSJakub Kicinski  * @small_ops: the small-struct operations supported by this family
450b588afdSJakub Kicinski  * @n_small_ops: number of small-struct operations supported by this family
46482a8524SThomas Graf  */
47fd2c3ef7SEric Dumazet struct genl_family {
4898e4321bSDavid S. Miller 	int			id;		/* private */
49482a8524SThomas Graf 	unsigned int		hdrsize;
50482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
51482a8524SThomas Graf 	unsigned int		version;
52482a8524SThomas Graf 	unsigned int		maxattr;
53e5086736SJakub Kicinski 	unsigned int		mcgrp_offset;	/* private */
54e5086736SJakub Kicinski 	u8			netnsok:1;
55e5086736SJakub Kicinski 	u8			parallel_ops:1;
56e5086736SJakub Kicinski 	u8			n_ops;
570b588afdSJakub Kicinski 	u8			n_small_ops;
58e5086736SJakub Kicinski 	u8			n_mcgrps;
593b0f31f2SJohannes Berg 	const struct nla_policy *policy;
60f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
61ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
62ff4c92d8SJohannes Berg 					    struct genl_info *info);
63f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
64ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
65ff4c92d8SJohannes Berg 					     struct genl_info *info);
66489111e5SJohannes Berg 	const struct genl_ops *	ops;
670b588afdSJakub Kicinski 	const struct genl_small_ops *small_ops;
68489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
6933c6b1f6SPravin B Shelar 	struct module		*module;
70482a8524SThomas Graf };
71482a8524SThomas Graf 
72482a8524SThomas Graf /**
73482a8524SThomas Graf  * struct genl_info - receiving information
74482a8524SThomas Graf  * @snd_seq: sending sequence number
7515e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
76482a8524SThomas Graf  * @nlhdr: netlink message header
77482a8524SThomas Graf  * @genlhdr: generic netlink message header
78482a8524SThomas Graf  * @userhdr: user specific header
79482a8524SThomas Graf  * @attrs: netlink attributes
80ff4c92d8SJohannes Berg  * @_net: network namespace
81ff4c92d8SJohannes Berg  * @user_ptr: user pointers
827ab606d1SJohannes Berg  * @extack: extended ACK report struct
83482a8524SThomas Graf  */
84fd2c3ef7SEric Dumazet struct genl_info {
85482a8524SThomas Graf 	u32			snd_seq;
8615e47304SEric W. Biederman 	u32			snd_portid;
87482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
88482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
89482a8524SThomas Graf 	void *			userhdr;
90482a8524SThomas Graf 	struct nlattr **	attrs;
910c5c9fb5SEric W. Biederman 	possible_net_t		_net;
92ff4c92d8SJohannes Berg 	void *			user_ptr[2];
937ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
94482a8524SThomas Graf };
95482a8524SThomas Graf 
96134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
97134e6375SJohannes Berg {
98c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
99134e6375SJohannes Berg }
100134e6375SJohannes Berg 
101134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
102134e6375SJohannes Berg {
103c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
104134e6375SJohannes Berg }
105134e6375SJohannes Berg 
1067ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1077ab606d1SJohannes Berg 
108ef6243acSJohannes Berg enum genl_validate_flags {
109ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
110ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
111ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
112ef6243acSJohannes Berg };
113ef6243acSJohannes Berg 
114482a8524SThomas Graf /**
1150b588afdSJakub Kicinski  * struct genl_small_ops - generic netlink operations (small version)
1160b588afdSJakub Kicinski  * @cmd: command identifier
1170b588afdSJakub Kicinski  * @internal_flags: flags used by the family
1180b588afdSJakub Kicinski  * @flags: flags
1190b588afdSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
1200b588afdSJakub Kicinski  * @doit: standard command callback
1210b588afdSJakub Kicinski  * @dumpit: callback for dumpers
1220b588afdSJakub Kicinski  *
1230b588afdSJakub Kicinski  * This is a cut-down version of struct genl_ops for users who don't need
1240b588afdSJakub Kicinski  * most of the ancillary infra and want to save space.
1251927f41aSJiri Pirko  */
1260b588afdSJakub Kicinski struct genl_small_ops {
1270b588afdSJakub Kicinski 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
1280b588afdSJakub Kicinski 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
1290b588afdSJakub Kicinski 	u8	cmd;
1300b588afdSJakub Kicinski 	u8	internal_flags;
1310b588afdSJakub Kicinski 	u8	flags;
1320b588afdSJakub Kicinski 	u8	validate;
1331927f41aSJiri Pirko };
1341927f41aSJiri Pirko 
1351927f41aSJiri Pirko /**
136482a8524SThomas Graf  * struct genl_ops - generic netlink operations
137482a8524SThomas Graf  * @cmd: command identifier
138ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
139482a8524SThomas Graf  * @flags: flags
140*48526a0fSJakub Kicinski  * @maxattr: maximum number of attributes supported
141*48526a0fSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
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);
154*48526a0fSJakub Kicinski 	const struct nla_policy *policy;
155*48526a0fSJakub Kicinski 	unsigned int		maxattr;
1563f5ccd06SJohannes Berg 	u8			cmd;
1573f5ccd06SJohannes Berg 	u8			internal_flags;
1583f5ccd06SJohannes Berg 	u8			flags;
159ef6243acSJohannes Berg 	u8			validate;
160482a8524SThomas Graf };
161482a8524SThomas Graf 
1620b588afdSJakub Kicinski /**
1630b588afdSJakub Kicinski  * struct genl_info - info that is available during dumpit op call
1640b588afdSJakub Kicinski  * @family: generic netlink family - for internal genl code usage
1650b588afdSJakub Kicinski  * @ops: generic netlink ops - for internal genl code usage
1660b588afdSJakub Kicinski  * @attrs: netlink attributes
1670b588afdSJakub Kicinski  */
1680b588afdSJakub Kicinski struct genl_dumpit_info {
1690b588afdSJakub Kicinski 	const struct genl_family *family;
1700b588afdSJakub Kicinski 	struct genl_ops op;
1710b588afdSJakub Kicinski 	struct nlattr **attrs;
1720b588afdSJakub Kicinski };
1730b588afdSJakub Kicinski 
1740b588afdSJakub Kicinski static inline const struct genl_dumpit_info *
1750b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb)
1760b588afdSJakub Kicinski {
1770b588afdSJakub Kicinski 	return cb->data;
1780b588afdSJakub Kicinski }
1790b588afdSJakub Kicinski 
180489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
1812ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
1822ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
18392c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
184482a8524SThomas Graf 
18515e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
1862ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
187482a8524SThomas Graf 
188482a8524SThomas Graf /**
189670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
190670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
191670dc283SJohannes Berg  *
192670dc283SJohannes Berg  * Returns pointer to netlink header.
193670dc283SJohannes Berg  */
1940a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
195670dc283SJohannes Berg {
196670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
197670dc283SJohannes Berg 				   GENL_HDRLEN -
198670dc283SJohannes Berg 				   NLMSG_HDRLEN);
199670dc283SJohannes Berg }
200670dc283SJohannes Berg 
201670dc283SJohannes Berg /**
2028cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
2037b1883ceSJoe Stringer  * @nlh: netlink message header
2047b1883ceSJoe Stringer  * @family: genetlink message family
2057b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
2067b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
2077b1883ceSJoe Stringer  * @policy: validation policy
208fceb6435SJohannes Berg  * @extack: extended ACK report struct
209fceb6435SJohannes Berg  */
2108cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
2117b1883ceSJoe Stringer 					   const struct genl_family *family,
2127b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
213fceb6435SJohannes Berg 					   const struct nla_policy *policy,
214fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
2157b1883ceSJoe Stringer {
2168cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2178cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
2187b1883ceSJoe Stringer }
2197b1883ceSJoe Stringer 
2207b1883ceSJoe Stringer /**
2213de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
2223de64403SJohannes Berg  * @nlh: netlink message header
2233de64403SJohannes Berg  * @family: genetlink message family
2243de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
2253de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
2263de64403SJohannes Berg  * @policy: validation policy
2273de64403SJohannes Berg  * @extack: extended ACK report struct
2283de64403SJohannes Berg  */
2293de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
2303de64403SJohannes Berg 				const struct genl_family *family,
2313de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
2323de64403SJohannes Berg 				const struct nla_policy *policy,
2333de64403SJohannes Berg 				struct netlink_ext_ack *extack)
2343de64403SJohannes Berg {
2353de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2363de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
2373de64403SJohannes Berg }
2383de64403SJohannes Berg 
2393de64403SJohannes Berg /**
240670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
241670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
242670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
243670dc283SJohannes Berg  *
244670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
245670dc283SJohannes Berg  * simpler to use with generic netlink.
246670dc283SJohannes Berg  */
247670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
2480a833c29SMichal Kubecek 					      void *user_hdr)
249670dc283SJohannes Berg {
2500a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
251670dc283SJohannes Berg }
252670dc283SJohannes Berg 
253670dc283SJohannes Berg /**
25417c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
25517c157c8SThomas Graf  * @skb: socket buffer holding the message
25617c157c8SThomas Graf  * @info: receiver info
25717c157c8SThomas Graf  * @family: generic netlink family
25817c157c8SThomas Graf  * @flags: netlink message flags
25917c157c8SThomas Graf  * @cmd: generic netlink command
26017c157c8SThomas Graf  *
26117c157c8SThomas Graf  * Returns pointer to user specific header
26217c157c8SThomas Graf  */
26317c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
26417c157c8SThomas Graf 				      struct genl_info *info,
2652ae0f17dSJohannes Berg 				      const struct genl_family *family,
26617c157c8SThomas Graf 				      int flags, u8 cmd)
26717c157c8SThomas Graf {
26815e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
26917c157c8SThomas Graf 			   flags, cmd);
27017c157c8SThomas Graf }
27117c157c8SThomas Graf 
27217c157c8SThomas Graf /**
273482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
274482a8524SThomas Graf  * @skb: socket buffer the message is stored in
275482a8524SThomas Graf  * @hdr: user specific header
276482a8524SThomas Graf  */
277053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
278482a8524SThomas Graf {
279053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
280482a8524SThomas Graf }
281482a8524SThomas Graf 
282482a8524SThomas Graf /**
283482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
284482a8524SThomas Graf  * @skb: socket buffer the message is stored in
285482a8524SThomas Graf  * @hdr: generic netlink message header
286482a8524SThomas Graf  */
287bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
288482a8524SThomas Graf {
28938db9e1dSJulia Lawall 	if (hdr)
290bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
291482a8524SThomas Graf }
292482a8524SThomas Graf 
293482a8524SThomas Graf /**
294134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
29568eb5503SJohannes Berg  * @family: the generic netlink family
296134e6375SJohannes Berg  * @net: the net namespace
297134e6375SJohannes Berg  * @skb: netlink message as socket buffer
29815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2992a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
300134e6375SJohannes Berg  * @flags: allocation flags
301134e6375SJohannes Berg  */
3022ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
30368eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
30415e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
305134e6375SJohannes Berg {
306220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
3072a94fe48SJohannes Berg 		return -EINVAL;
3082a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
30915e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
310134e6375SJohannes Berg }
311134e6375SJohannes Berg 
312134e6375SJohannes Berg /**
313134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
31468eb5503SJohannes Berg  * @family: the generic netlink family
315482a8524SThomas Graf  * @skb: netlink message as socket buffer
31615e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3172a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
318d387f6adSThomas Graf  * @flags: allocation flags
319482a8524SThomas Graf  */
3202ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
32168eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
322d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
323482a8524SThomas Graf {
32468eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
32568eb5503SJohannes Berg 				       portid, group, flags);
326482a8524SThomas Graf }
327482a8524SThomas Graf 
328482a8524SThomas Graf /**
329134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
33068eb5503SJohannes Berg  * @family: the generic netlink family
331134e6375SJohannes Berg  * @skb: netlink message as socket buffer
33215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3332a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
334134e6375SJohannes Berg  * @flags: allocation flags
335134e6375SJohannes Berg  *
336134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
337134e6375SJohannes Berg  */
3382ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
33968eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
340134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
341134e6375SJohannes Berg 
342134e6375SJohannes Berg /**
343482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
344482a8524SThomas Graf  * @skb: netlink message as socket buffer
34515e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
346482a8524SThomas Graf  */
34715e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
348482a8524SThomas Graf {
34915e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
350482a8524SThomas Graf }
351482a8524SThomas Graf 
352fb0ba6bdSBalbir Singh /**
35381878d27SThomas Graf  * genlmsg_reply - reply to a request
35481878d27SThomas Graf  * @skb: netlink message to be sent back
35581878d27SThomas Graf  * @info: receiver information
35681878d27SThomas Graf  */
35781878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
35881878d27SThomas Graf {
35915e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
36081878d27SThomas Graf }
36181878d27SThomas Graf 
36281878d27SThomas Graf /**
363fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
36470f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
365fb0ba6bdSBalbir Singh  */
366fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
367fb0ba6bdSBalbir Singh {
368fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
369fb0ba6bdSBalbir Singh }
370fb0ba6bdSBalbir Singh 
371fb0ba6bdSBalbir Singh /**
372fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
373fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
374fb0ba6bdSBalbir Singh  */
375fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
376fb0ba6bdSBalbir Singh {
377fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
378fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
379fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
380fb0ba6bdSBalbir Singh }
381fb0ba6bdSBalbir Singh 
38217db952cSBalbir Singh /**
38317db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
38417db952cSBalbir Singh  * @payload: length of message payload
38517db952cSBalbir Singh  */
38617db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
38717db952cSBalbir Singh {
38817db952cSBalbir Singh 	return GENL_HDRLEN + payload;
38917db952cSBalbir Singh }
39017db952cSBalbir Singh 
39117db952cSBalbir Singh /**
39217db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
39317db952cSBalbir Singh  * @payload: length of message payload
39417db952cSBalbir Singh  */
39517db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
39617db952cSBalbir Singh {
39717db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
39817db952cSBalbir Singh }
39917db952cSBalbir Singh 
4003dabc715SThomas Graf /**
4013dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
4023dabc715SThomas Graf  * @payload: size of the message payload
4033dabc715SThomas Graf  * @flags: the type of memory to allocate.
4043dabc715SThomas Graf  */
4053dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
4063dabc715SThomas Graf {
4073dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
4083dabc715SThomas Graf }
4093dabc715SThomas Graf 
41062b68e99SJohannes Berg /**
41162b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
41268eb5503SJohannes Berg  * @family: the generic netlink family
41362b68e99SJohannes Berg  * @net: the network namespace to report the error to
41462b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
41562b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
4162a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
41762b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
41862b68e99SJohannes Berg  *
41962b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
42062b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
42162b68e99SJohannes Berg  */
4222ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
4232ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
4242ae0f17dSJohannes Berg 			       u32 group, int code)
42562b68e99SJohannes Berg {
42691398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
42791398a09SJohannes Berg 		return -EINVAL;
42891398a09SJohannes Berg 	group = family->mcgrp_offset + group;
42962b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
43062b68e99SJohannes Berg }
4313dabc715SThomas Graf 
4322ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
433f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4340d566379SNicolas Dichtel {
4350d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4360d566379SNicolas Dichtel 		return -EINVAL;
4370d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
438f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4390d566379SNicolas Dichtel }
440482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
441