xref: /openbmc/linux/include/net/genetlink.h (revision 45dca15759643806660e9285e6af8a1ba3c76c82)
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
145c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
152dbba6f7SJohannes Berg  */
16fd2c3ef7SEric Dumazet struct genl_multicast_group {
172dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
184d54cc32SFlorian Westphal 	u8			flags;
192dbba6f7SJohannes Berg };
202dbba6f7SJohannes Berg 
21ff4c92d8SJohannes Berg struct genl_ops;
22ff4c92d8SJohannes Berg struct genl_info;
23ff4c92d8SJohannes Berg 
242dbba6f7SJohannes Berg /**
25482a8524SThomas Graf  * struct genl_family - generic netlink family
26a07ea4d9SJohannes Berg  * @id: protocol family identifier (private)
27482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
28482a8524SThomas Graf  * @name: name of family
29482a8524SThomas Graf  * @version: protocol version
30482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
313b0f31f2SJohannes Berg  * @policy: netlink policy
32134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
33134e6375SJohannes Berg  *	namespaces and should be presented in all of them
34f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
35f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
36ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
37ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
38ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
39ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
40489111e5SJohannes Berg  * @mcgrps: multicast groups used by this family
41489111e5SJohannes Berg  * @n_mcgrps: number of multicast groups
429c5d03d3SJakub Kicinski  * @resv_start_op: first operation for which reserved fields of the header
439c5d03d3SJakub Kicinski  *	can be validated, new families should leave this field at zero
442a94fe48SJohannes Berg  * @mcgrp_offset: starting number of multicast group IDs in this family
45489111e5SJohannes Berg  *	(private)
46489111e5SJohannes Berg  * @ops: the operations supported by this family
47489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
480b588afdSJakub Kicinski  * @small_ops: the small-struct operations supported by this family
490b588afdSJakub Kicinski  * @n_small_ops: number of small-struct operations supported by this family
50482a8524SThomas Graf  */
51fd2c3ef7SEric Dumazet struct genl_family {
5298e4321bSDavid S. Miller 	int			id;		/* private */
53482a8524SThomas Graf 	unsigned int		hdrsize;
54482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
55482a8524SThomas Graf 	unsigned int		version;
56482a8524SThomas Graf 	unsigned int		maxattr;
57e5086736SJakub Kicinski 	unsigned int		mcgrp_offset;	/* private */
58e5086736SJakub Kicinski 	u8			netnsok:1;
59e5086736SJakub Kicinski 	u8			parallel_ops:1;
60e5086736SJakub Kicinski 	u8			n_ops;
610b588afdSJakub Kicinski 	u8			n_small_ops;
62e5086736SJakub Kicinski 	u8			n_mcgrps;
639c5d03d3SJakub Kicinski 	u8			resv_start_op;
643b0f31f2SJohannes Berg 	const struct nla_policy *policy;
65f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
66ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
67ff4c92d8SJohannes Berg 					    struct genl_info *info);
68f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
69ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
70ff4c92d8SJohannes Berg 					     struct genl_info *info);
71489111e5SJohannes Berg 	const struct genl_ops *	ops;
720b588afdSJakub Kicinski 	const struct genl_small_ops *small_ops;
73489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
7433c6b1f6SPravin B Shelar 	struct module		*module;
75482a8524SThomas Graf };
76482a8524SThomas Graf 
77482a8524SThomas Graf /**
78482a8524SThomas Graf  * struct genl_info - receiving information
79482a8524SThomas Graf  * @snd_seq: sending sequence number
8015e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
81482a8524SThomas Graf  * @nlhdr: netlink message header
82482a8524SThomas Graf  * @genlhdr: generic netlink message header
83482a8524SThomas Graf  * @userhdr: user specific header
84482a8524SThomas Graf  * @attrs: netlink attributes
85ff4c92d8SJohannes Berg  * @_net: network namespace
86ff4c92d8SJohannes Berg  * @user_ptr: user pointers
877ab606d1SJohannes Berg  * @extack: extended ACK report struct
88482a8524SThomas Graf  */
89fd2c3ef7SEric Dumazet struct genl_info {
90482a8524SThomas Graf 	u32			snd_seq;
9115e47304SEric W. Biederman 	u32			snd_portid;
92482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
93482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
94482a8524SThomas Graf 	void *			userhdr;
95482a8524SThomas Graf 	struct nlattr **	attrs;
960c5c9fb5SEric W. Biederman 	possible_net_t		_net;
97ff4c92d8SJohannes Berg 	void *			user_ptr[2];
987ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
99482a8524SThomas Graf };
100482a8524SThomas Graf 
101134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
102134e6375SJohannes Berg {
103c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
104134e6375SJohannes Berg }
105134e6375SJohannes Berg 
106134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
107134e6375SJohannes Berg {
108c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
109134e6375SJohannes Berg }
110134e6375SJohannes Berg 
1117ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1127ab606d1SJohannes Berg 
113*45dca157SJakub Kicinski /* Report that a root attribute is missing */
114*45dca157SJakub Kicinski #define GENL_REQ_ATTR_CHECK(info, attr) ({				\
115*45dca157SJakub Kicinski 	struct genl_info *__info = (info);				\
116*45dca157SJakub Kicinski 									\
117*45dca157SJakub Kicinski 	NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
118*45dca157SJakub Kicinski })
119*45dca157SJakub Kicinski 
120ef6243acSJohannes Berg enum genl_validate_flags {
121ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
122ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
123ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
124ef6243acSJohannes Berg };
125ef6243acSJohannes Berg 
126482a8524SThomas Graf /**
1270b588afdSJakub Kicinski  * struct genl_small_ops - generic netlink operations (small version)
1280b588afdSJakub Kicinski  * @cmd: command identifier
1290b588afdSJakub Kicinski  * @internal_flags: flags used by the family
1305c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
1310b588afdSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
1320b588afdSJakub Kicinski  * @doit: standard command callback
1330b588afdSJakub Kicinski  * @dumpit: callback for dumpers
1340b588afdSJakub Kicinski  *
1350b588afdSJakub Kicinski  * This is a cut-down version of struct genl_ops for users who don't need
1360b588afdSJakub Kicinski  * most of the ancillary infra and want to save space.
1371927f41aSJiri Pirko  */
1380b588afdSJakub Kicinski struct genl_small_ops {
1390b588afdSJakub Kicinski 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
1400b588afdSJakub Kicinski 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
1410b588afdSJakub Kicinski 	u8	cmd;
1420b588afdSJakub Kicinski 	u8	internal_flags;
1430b588afdSJakub Kicinski 	u8	flags;
1440b588afdSJakub Kicinski 	u8	validate;
1451927f41aSJiri Pirko };
1461927f41aSJiri Pirko 
1471927f41aSJiri Pirko /**
148482a8524SThomas Graf  * struct genl_ops - generic netlink operations
149482a8524SThomas Graf  * @cmd: command identifier
150ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
1515c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
15248526a0fSJakub Kicinski  * @maxattr: maximum number of attributes supported
15348526a0fSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
1543ddf9b43SJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
155482a8524SThomas Graf  * @doit: standard command callback
156fc9e50f5STom Herbert  * @start: start callback for dumps
157482a8524SThomas Graf  * @dumpit: callback for dumpers
158a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
159482a8524SThomas Graf  */
160fd2c3ef7SEric Dumazet struct genl_ops {
161482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
162482a8524SThomas Graf 				       struct genl_info *info);
163fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
164482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
165482a8524SThomas Graf 					 struct netlink_callback *cb);
166a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
16748526a0fSJakub Kicinski 	const struct nla_policy *policy;
16848526a0fSJakub Kicinski 	unsigned int		maxattr;
1693f5ccd06SJohannes Berg 	u8			cmd;
1703f5ccd06SJohannes Berg 	u8			internal_flags;
1713f5ccd06SJohannes Berg 	u8			flags;
172ef6243acSJohannes Berg 	u8			validate;
173482a8524SThomas Graf };
174482a8524SThomas Graf 
1750b588afdSJakub Kicinski /**
1760b588afdSJakub Kicinski  * struct genl_info - info that is available during dumpit op call
1770b588afdSJakub Kicinski  * @family: generic netlink family - for internal genl code usage
1780b588afdSJakub Kicinski  * @ops: generic netlink ops - for internal genl code usage
1790b588afdSJakub Kicinski  * @attrs: netlink attributes
1800b588afdSJakub Kicinski  */
1810b588afdSJakub Kicinski struct genl_dumpit_info {
1820b588afdSJakub Kicinski 	const struct genl_family *family;
1830b588afdSJakub Kicinski 	struct genl_ops op;
1840b588afdSJakub Kicinski 	struct nlattr **attrs;
1850b588afdSJakub Kicinski };
1860b588afdSJakub Kicinski 
1870b588afdSJakub Kicinski static inline const struct genl_dumpit_info *
1880b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb)
1890b588afdSJakub Kicinski {
1900b588afdSJakub Kicinski 	return cb->data;
1910b588afdSJakub Kicinski }
1920b588afdSJakub Kicinski 
193489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
1942ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
1952ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
19692c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
197482a8524SThomas Graf 
19815e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
1992ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
200482a8524SThomas Graf 
201482a8524SThomas Graf /**
202670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
203670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
204670dc283SJohannes Berg  *
205670dc283SJohannes Berg  * Returns pointer to netlink header.
206670dc283SJohannes Berg  */
2070a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
208670dc283SJohannes Berg {
209670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
210670dc283SJohannes Berg 				   GENL_HDRLEN -
211670dc283SJohannes Berg 				   NLMSG_HDRLEN);
212670dc283SJohannes Berg }
213670dc283SJohannes Berg 
214670dc283SJohannes Berg /**
2158cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
2167b1883ceSJoe Stringer  * @nlh: netlink message header
2177b1883ceSJoe Stringer  * @family: genetlink message family
2187b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
2197b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
2207b1883ceSJoe Stringer  * @policy: validation policy
221fceb6435SJohannes Berg  * @extack: extended ACK report struct
222fceb6435SJohannes Berg  */
2238cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
2247b1883ceSJoe Stringer 					   const struct genl_family *family,
2257b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
226fceb6435SJohannes Berg 					   const struct nla_policy *policy,
227fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
2287b1883ceSJoe Stringer {
2298cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2308cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
2317b1883ceSJoe Stringer }
2327b1883ceSJoe Stringer 
2337b1883ceSJoe Stringer /**
2343de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
2353de64403SJohannes Berg  * @nlh: netlink message header
2363de64403SJohannes Berg  * @family: genetlink message family
2373de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
2383de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
2393de64403SJohannes Berg  * @policy: validation policy
2403de64403SJohannes Berg  * @extack: extended ACK report struct
2413de64403SJohannes Berg  */
2423de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
2433de64403SJohannes Berg 				const struct genl_family *family,
2443de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
2453de64403SJohannes Berg 				const struct nla_policy *policy,
2463de64403SJohannes Berg 				struct netlink_ext_ack *extack)
2473de64403SJohannes Berg {
2483de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
2493de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
2503de64403SJohannes Berg }
2513de64403SJohannes Berg 
2523de64403SJohannes Berg /**
253670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
254670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
255670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
256670dc283SJohannes Berg  *
257670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
258670dc283SJohannes Berg  * simpler to use with generic netlink.
259670dc283SJohannes Berg  */
260670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
2610a833c29SMichal Kubecek 					      void *user_hdr)
262670dc283SJohannes Berg {
2630a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
264670dc283SJohannes Berg }
265670dc283SJohannes Berg 
266670dc283SJohannes Berg /**
26717c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
26817c157c8SThomas Graf  * @skb: socket buffer holding the message
26917c157c8SThomas Graf  * @info: receiver info
27017c157c8SThomas Graf  * @family: generic netlink family
27117c157c8SThomas Graf  * @flags: netlink message flags
27217c157c8SThomas Graf  * @cmd: generic netlink command
27317c157c8SThomas Graf  *
27417c157c8SThomas Graf  * Returns pointer to user specific header
27517c157c8SThomas Graf  */
27617c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
27717c157c8SThomas Graf 				      struct genl_info *info,
2782ae0f17dSJohannes Berg 				      const struct genl_family *family,
27917c157c8SThomas Graf 				      int flags, u8 cmd)
28017c157c8SThomas Graf {
28115e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
28217c157c8SThomas Graf 			   flags, cmd);
28317c157c8SThomas Graf }
28417c157c8SThomas Graf 
28517c157c8SThomas Graf /**
286482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
287482a8524SThomas Graf  * @skb: socket buffer the message is stored in
288482a8524SThomas Graf  * @hdr: user specific header
289482a8524SThomas Graf  */
290053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
291482a8524SThomas Graf {
292053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
293482a8524SThomas Graf }
294482a8524SThomas Graf 
295482a8524SThomas Graf /**
296482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
297482a8524SThomas Graf  * @skb: socket buffer the message is stored in
298482a8524SThomas Graf  * @hdr: generic netlink message header
299482a8524SThomas Graf  */
300bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
301482a8524SThomas Graf {
30238db9e1dSJulia Lawall 	if (hdr)
303bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
304482a8524SThomas Graf }
305482a8524SThomas Graf 
306482a8524SThomas Graf /**
307134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
30868eb5503SJohannes Berg  * @family: the generic netlink family
309134e6375SJohannes Berg  * @net: the net namespace
310134e6375SJohannes Berg  * @skb: netlink message as socket buffer
31115e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3122a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
313134e6375SJohannes Berg  * @flags: allocation flags
314134e6375SJohannes Berg  */
3152ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
31668eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
31715e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
318134e6375SJohannes Berg {
319220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
3202a94fe48SJohannes Berg 		return -EINVAL;
3212a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
32215e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
323134e6375SJohannes Berg }
324134e6375SJohannes Berg 
325134e6375SJohannes Berg /**
326134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
32768eb5503SJohannes Berg  * @family: the generic netlink family
328482a8524SThomas Graf  * @skb: netlink message as socket buffer
32915e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3302a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
331d387f6adSThomas Graf  * @flags: allocation flags
332482a8524SThomas Graf  */
3332ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
33468eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
335d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
336482a8524SThomas Graf {
33768eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
33868eb5503SJohannes Berg 				       portid, group, flags);
339482a8524SThomas Graf }
340482a8524SThomas Graf 
341482a8524SThomas Graf /**
342134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
34368eb5503SJohannes Berg  * @family: the generic netlink family
344134e6375SJohannes Berg  * @skb: netlink message as socket buffer
34515e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3462a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
347134e6375SJohannes Berg  * @flags: allocation flags
348134e6375SJohannes Berg  *
349134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
350134e6375SJohannes Berg  */
3512ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
35268eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
353134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
354134e6375SJohannes Berg 
355134e6375SJohannes Berg /**
356482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
357482a8524SThomas Graf  * @skb: netlink message as socket buffer
35815e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
359482a8524SThomas Graf  */
36015e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
361482a8524SThomas Graf {
36215e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
363482a8524SThomas Graf }
364482a8524SThomas Graf 
365fb0ba6bdSBalbir Singh /**
36681878d27SThomas Graf  * genlmsg_reply - reply to a request
36781878d27SThomas Graf  * @skb: netlink message to be sent back
36881878d27SThomas Graf  * @info: receiver information
36981878d27SThomas Graf  */
37081878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
37181878d27SThomas Graf {
37215e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
37381878d27SThomas Graf }
37481878d27SThomas Graf 
37581878d27SThomas Graf /**
376fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
37770f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
378fb0ba6bdSBalbir Singh  */
379fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
380fb0ba6bdSBalbir Singh {
381fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
382fb0ba6bdSBalbir Singh }
383fb0ba6bdSBalbir Singh 
384fb0ba6bdSBalbir Singh /**
385fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
386fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
387fb0ba6bdSBalbir Singh  */
388fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
389fb0ba6bdSBalbir Singh {
390fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
391fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
392fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
393fb0ba6bdSBalbir Singh }
394fb0ba6bdSBalbir Singh 
39517db952cSBalbir Singh /**
39617db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
39717db952cSBalbir Singh  * @payload: length of message payload
39817db952cSBalbir Singh  */
39917db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
40017db952cSBalbir Singh {
40117db952cSBalbir Singh 	return GENL_HDRLEN + payload;
40217db952cSBalbir Singh }
40317db952cSBalbir Singh 
40417db952cSBalbir Singh /**
40517db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
40617db952cSBalbir Singh  * @payload: length of message payload
40717db952cSBalbir Singh  */
40817db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
40917db952cSBalbir Singh {
41017db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
41117db952cSBalbir Singh }
41217db952cSBalbir Singh 
4133dabc715SThomas Graf /**
4143dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
4153dabc715SThomas Graf  * @payload: size of the message payload
4163dabc715SThomas Graf  * @flags: the type of memory to allocate.
4173dabc715SThomas Graf  */
4183dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
4193dabc715SThomas Graf {
4203dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
4213dabc715SThomas Graf }
4223dabc715SThomas Graf 
42362b68e99SJohannes Berg /**
42462b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
42568eb5503SJohannes Berg  * @family: the generic netlink family
42662b68e99SJohannes Berg  * @net: the network namespace to report the error to
42762b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
42862b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
4292a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
43062b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
43162b68e99SJohannes Berg  *
43262b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
43362b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
43462b68e99SJohannes Berg  */
4352ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
4362ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
4372ae0f17dSJohannes Berg 			       u32 group, int code)
43862b68e99SJohannes Berg {
43991398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
44091398a09SJohannes Berg 		return -EINVAL;
44191398a09SJohannes Berg 	group = family->mcgrp_offset + group;
44262b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
44362b68e99SJohannes Berg }
4443dabc715SThomas Graf 
4452ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
446f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4470d566379SNicolas Dichtel {
4480d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4490d566379SNicolas Dichtel 		return -EINVAL;
4500d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
451f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4520d566379SNicolas Dichtel }
453482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
454