xref: /openbmc/linux/include/net/genetlink.h (revision 5c670a010de46687ed27553602d8131ce4d7a9fb)
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 
2120b0b53aSJakub Kicinski struct genl_split_ops;
22ff4c92d8SJohannes Berg struct genl_info;
23ff4c92d8SJohannes Berg 
242dbba6f7SJohannes Berg /**
25482a8524SThomas Graf  * struct genl_family - generic netlink family
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
39a1a824f4SJakub Kicinski  * @module: pointer to the owning module (set to THIS_MODULE)
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
434fa86555SJakub Kicinski  *	can be validated and policies are required (see below);
444fa86555SJakub Kicinski  *	new families should leave this field at zero
45489111e5SJohannes Berg  * @ops: the operations supported by this family
46489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
470b588afdSJakub Kicinski  * @small_ops: the small-struct operations supported by this family
480b588afdSJakub Kicinski  * @n_small_ops: number of small-struct operations supported by this family
49b8fd60c3SJakub Kicinski  * @split_ops: the split do/dump form of operation definition
50b8fd60c3SJakub Kicinski  * @n_split_ops: number of entries in @split_ops, not that with split do/dump
51b8fd60c3SJakub Kicinski  *	ops the number of entries is not the same as number of commands
524fa86555SJakub Kicinski  *
534fa86555SJakub Kicinski  * Attribute policies (the combination of @policy and @maxattr fields)
544fa86555SJakub Kicinski  * can be attached at the family level or at the operation level.
554fa86555SJakub Kicinski  * If both are present the per-operation policy takes precedence.
564fa86555SJakub Kicinski  * For operations before @resv_start_op lack of policy means that the core
574fa86555SJakub Kicinski  * will perform no attribute parsing or validation. For newer operations
584fa86555SJakub Kicinski  * if policy is not provided core will reject all TLV attributes.
59482a8524SThomas Graf  */
60fd2c3ef7SEric Dumazet struct genl_family {
61482a8524SThomas Graf 	unsigned int		hdrsize;
62482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
63482a8524SThomas Graf 	unsigned int		version;
64482a8524SThomas Graf 	unsigned int		maxattr;
65e5086736SJakub Kicinski 	u8			netnsok:1;
66e5086736SJakub Kicinski 	u8			parallel_ops:1;
67e5086736SJakub Kicinski 	u8			n_ops;
680b588afdSJakub Kicinski 	u8			n_small_ops;
69b8fd60c3SJakub Kicinski 	u8			n_split_ops;
70e5086736SJakub Kicinski 	u8			n_mcgrps;
719c5d03d3SJakub Kicinski 	u8			resv_start_op;
723b0f31f2SJohannes Berg 	const struct nla_policy *policy;
7320b0b53aSJakub Kicinski 	int			(*pre_doit)(const struct genl_split_ops *ops,
74ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
75ff4c92d8SJohannes Berg 					    struct genl_info *info);
7620b0b53aSJakub Kicinski 	void			(*post_doit)(const struct genl_split_ops *ops,
77ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
78ff4c92d8SJohannes Berg 					     struct genl_info *info);
79489111e5SJohannes Berg 	const struct genl_ops *	ops;
800b588afdSJakub Kicinski 	const struct genl_small_ops *small_ops;
81b8fd60c3SJakub Kicinski 	const struct genl_split_ops *split_ops;
82489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
8333c6b1f6SPravin B Shelar 	struct module		*module;
847c3eaa02SJakub Kicinski 
857c3eaa02SJakub Kicinski /* private: internal use only */
867c3eaa02SJakub Kicinski 	/* protocol family identifier */
877c3eaa02SJakub Kicinski 	int			id;
887c3eaa02SJakub Kicinski 	/* starting number of multicast group IDs in this family */
897c3eaa02SJakub Kicinski 	unsigned int		mcgrp_offset;
90482a8524SThomas Graf };
91482a8524SThomas Graf 
92482a8524SThomas Graf /**
93482a8524SThomas Graf  * struct genl_info - receiving information
94482a8524SThomas Graf  * @snd_seq: sending sequence number
9515e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
96*5c670a01SJakub Kicinski  * @family: generic netlink family
97482a8524SThomas Graf  * @nlhdr: netlink message header
98482a8524SThomas Graf  * @genlhdr: generic netlink message header
99482a8524SThomas Graf  * @attrs: netlink attributes
100ff4c92d8SJohannes Berg  * @_net: network namespace
101ff4c92d8SJohannes Berg  * @user_ptr: user pointers
1027ab606d1SJohannes Berg  * @extack: extended ACK report struct
103482a8524SThomas Graf  */
104fd2c3ef7SEric Dumazet struct genl_info {
105482a8524SThomas Graf 	u32			snd_seq;
10615e47304SEric W. Biederman 	u32			snd_portid;
107*5c670a01SJakub Kicinski 	const struct genl_family *family;
108fde9bd4aSJakub Kicinski 	const struct nlmsghdr *	nlhdr;
109482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
110482a8524SThomas Graf 	struct nlattr **	attrs;
1110c5c9fb5SEric W. Biederman 	possible_net_t		_net;
112ff4c92d8SJohannes Berg 	void *			user_ptr[2];
1137ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
114482a8524SThomas Graf };
115482a8524SThomas Graf 
116134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
117134e6375SJohannes Berg {
118c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
119134e6375SJohannes Berg }
120134e6375SJohannes Berg 
121134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
122134e6375SJohannes Berg {
123c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
124134e6375SJohannes Berg }
125134e6375SJohannes Berg 
126bffcc688SJakub Kicinski static inline void *genl_info_userhdr(const struct genl_info *info)
127bffcc688SJakub Kicinski {
128bffcc688SJakub Kicinski 	return (u8 *)info->genlhdr + GENL_HDRLEN;
129bffcc688SJakub Kicinski }
130bffcc688SJakub Kicinski 
1317ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1327ab606d1SJohannes Berg 
133a3400e87SPaolo Abeni #define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
134a3400e87SPaolo Abeni 	NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
135a3400e87SPaolo Abeni 
13645dca157SJakub Kicinski /* Report that a root attribute is missing */
13745dca157SJakub Kicinski #define GENL_REQ_ATTR_CHECK(info, attr) ({				\
13845dca157SJakub Kicinski 	struct genl_info *__info = (info);				\
13945dca157SJakub Kicinski 									\
14045dca157SJakub Kicinski 	NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
14145dca157SJakub Kicinski })
14245dca157SJakub Kicinski 
143ef6243acSJohannes Berg enum genl_validate_flags {
144ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
145ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
146ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
147ef6243acSJohannes Berg };
148ef6243acSJohannes Berg 
149482a8524SThomas Graf /**
1500b588afdSJakub Kicinski  * struct genl_small_ops - generic netlink operations (small version)
1510b588afdSJakub Kicinski  * @cmd: command identifier
1520b588afdSJakub Kicinski  * @internal_flags: flags used by the family
1535c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
1540b588afdSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
1550b588afdSJakub Kicinski  * @doit: standard command callback
1560b588afdSJakub Kicinski  * @dumpit: callback for dumpers
1570b588afdSJakub Kicinski  *
1580b588afdSJakub Kicinski  * This is a cut-down version of struct genl_ops for users who don't need
1590b588afdSJakub Kicinski  * most of the ancillary infra and want to save space.
1601927f41aSJiri Pirko  */
1610b588afdSJakub Kicinski struct genl_small_ops {
1620b588afdSJakub Kicinski 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
1630b588afdSJakub Kicinski 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
1640b588afdSJakub Kicinski 	u8	cmd;
1650b588afdSJakub Kicinski 	u8	internal_flags;
1660b588afdSJakub Kicinski 	u8	flags;
1670b588afdSJakub Kicinski 	u8	validate;
1681927f41aSJiri Pirko };
1691927f41aSJiri Pirko 
1701927f41aSJiri Pirko /**
171482a8524SThomas Graf  * struct genl_ops - generic netlink operations
172482a8524SThomas Graf  * @cmd: command identifier
173ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
1745c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
17548526a0fSJakub Kicinski  * @maxattr: maximum number of attributes supported
17648526a0fSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
1773ddf9b43SJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
178482a8524SThomas Graf  * @doit: standard command callback
179fc9e50f5STom Herbert  * @start: start callback for dumps
180482a8524SThomas Graf  * @dumpit: callback for dumpers
181a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
182482a8524SThomas Graf  */
183fd2c3ef7SEric Dumazet struct genl_ops {
184482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
185482a8524SThomas Graf 				       struct genl_info *info);
186fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
187482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
188482a8524SThomas Graf 					 struct netlink_callback *cb);
189a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
19048526a0fSJakub Kicinski 	const struct nla_policy *policy;
19148526a0fSJakub Kicinski 	unsigned int		maxattr;
1923f5ccd06SJohannes Berg 	u8			cmd;
1933f5ccd06SJohannes Berg 	u8			internal_flags;
1943f5ccd06SJohannes Berg 	u8			flags;
195ef6243acSJohannes Berg 	u8			validate;
196482a8524SThomas Graf };
197482a8524SThomas Graf 
1980b588afdSJakub Kicinski /**
19920b0b53aSJakub Kicinski  * struct genl_split_ops - generic netlink operations (do/dump split version)
20020b0b53aSJakub Kicinski  * @cmd: command identifier
20120b0b53aSJakub Kicinski  * @internal_flags: flags used by the family
20220b0b53aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
20320b0b53aSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
20420b0b53aSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
20520b0b53aSJakub Kicinski  * @maxattr: maximum number of attributes supported
20620b0b53aSJakub Kicinski  *
20720b0b53aSJakub Kicinski  * Do callbacks:
20820b0b53aSJakub Kicinski  * @pre_doit: called before an operation's @doit callback, it may
20920b0b53aSJakub Kicinski  *	do additional, common, filtering and return an error
21020b0b53aSJakub Kicinski  * @doit: standard command callback
21120b0b53aSJakub Kicinski  * @post_doit: called after an operation's @doit callback, it may
21220b0b53aSJakub Kicinski  *	undo operations done by pre_doit, for example release locks
21320b0b53aSJakub Kicinski  *
21420b0b53aSJakub Kicinski  * Dump callbacks:
21520b0b53aSJakub Kicinski  * @start: start callback for dumps
21620b0b53aSJakub Kicinski  * @dumpit: callback for dumpers
21720b0b53aSJakub Kicinski  * @done: completion callback for dumps
21820b0b53aSJakub Kicinski  *
21920b0b53aSJakub Kicinski  * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
22020b0b53aSJakub Kicinski  * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
22120b0b53aSJakub Kicinski  * Exactly one of those flags must be set.
22220b0b53aSJakub Kicinski  */
22320b0b53aSJakub Kicinski struct genl_split_ops {
22420b0b53aSJakub Kicinski 	union {
22520b0b53aSJakub Kicinski 		struct {
22620b0b53aSJakub Kicinski 			int (*pre_doit)(const struct genl_split_ops *ops,
22720b0b53aSJakub Kicinski 					struct sk_buff *skb,
22820b0b53aSJakub Kicinski 					struct genl_info *info);
22920b0b53aSJakub Kicinski 			int (*doit)(struct sk_buff *skb,
23020b0b53aSJakub Kicinski 				    struct genl_info *info);
23120b0b53aSJakub Kicinski 			void (*post_doit)(const struct genl_split_ops *ops,
23220b0b53aSJakub Kicinski 					  struct sk_buff *skb,
23320b0b53aSJakub Kicinski 					  struct genl_info *info);
23420b0b53aSJakub Kicinski 		};
23520b0b53aSJakub Kicinski 		struct {
23620b0b53aSJakub Kicinski 			int (*start)(struct netlink_callback *cb);
23720b0b53aSJakub Kicinski 			int (*dumpit)(struct sk_buff *skb,
23820b0b53aSJakub Kicinski 				      struct netlink_callback *cb);
23920b0b53aSJakub Kicinski 			int (*done)(struct netlink_callback *cb);
24020b0b53aSJakub Kicinski 		};
24120b0b53aSJakub Kicinski 	};
24220b0b53aSJakub Kicinski 	const struct nla_policy *policy;
24320b0b53aSJakub Kicinski 	unsigned int		maxattr;
24420b0b53aSJakub Kicinski 	u8			cmd;
24520b0b53aSJakub Kicinski 	u8			internal_flags;
24620b0b53aSJakub Kicinski 	u8			flags;
24720b0b53aSJakub Kicinski 	u8			validate;
24820b0b53aSJakub Kicinski };
24920b0b53aSJakub Kicinski 
25020b0b53aSJakub Kicinski /**
251a1a824f4SJakub Kicinski  * struct genl_dumpit_info - info that is available during dumpit op call
252a1a824f4SJakub Kicinski  * @op: generic netlink ops - for internal genl code usage
2530b588afdSJakub Kicinski  * @attrs: netlink attributes
2549272af10SJakub Kicinski  * @info: struct genl_info describing the request
2550b588afdSJakub Kicinski  */
2560b588afdSJakub Kicinski struct genl_dumpit_info {
25720b0b53aSJakub Kicinski 	struct genl_split_ops op;
2589272af10SJakub Kicinski 	struct genl_info info;
2590b588afdSJakub Kicinski };
2600b588afdSJakub Kicinski 
2610b588afdSJakub Kicinski static inline const struct genl_dumpit_info *
2620b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb)
2630b588afdSJakub Kicinski {
2640b588afdSJakub Kicinski 	return cb->data;
2650b588afdSJakub Kicinski }
2660b588afdSJakub Kicinski 
2679272af10SJakub Kicinski static inline const struct genl_info *
2689272af10SJakub Kicinski genl_info_dump(struct netlink_callback *cb)
2699272af10SJakub Kicinski {
2709272af10SJakub Kicinski 	return &genl_dumpit_info(cb)->info;
2719272af10SJakub Kicinski }
2729272af10SJakub Kicinski 
273489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
2742ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
2752ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
27692c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
277482a8524SThomas Graf 
27815e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
2792ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
280482a8524SThomas Graf 
281482a8524SThomas Graf /**
282670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
283670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
284670dc283SJohannes Berg  *
285670dc283SJohannes Berg  * Returns pointer to netlink header.
286670dc283SJohannes Berg  */
2870a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
288670dc283SJohannes Berg {
289670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
290670dc283SJohannes Berg 				   GENL_HDRLEN -
291670dc283SJohannes Berg 				   NLMSG_HDRLEN);
292670dc283SJohannes Berg }
293670dc283SJohannes Berg 
294670dc283SJohannes Berg /**
2958cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
2967b1883ceSJoe Stringer  * @nlh: netlink message header
2977b1883ceSJoe Stringer  * @family: genetlink message family
2987b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
2997b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
3007b1883ceSJoe Stringer  * @policy: validation policy
301fceb6435SJohannes Berg  * @extack: extended ACK report struct
302fceb6435SJohannes Berg  */
3038cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
3047b1883ceSJoe Stringer 					   const struct genl_family *family,
3057b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
306fceb6435SJohannes Berg 					   const struct nla_policy *policy,
307fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
3087b1883ceSJoe Stringer {
3098cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
3108cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
3117b1883ceSJoe Stringer }
3127b1883ceSJoe Stringer 
3137b1883ceSJoe Stringer /**
3143de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
3153de64403SJohannes Berg  * @nlh: netlink message header
3163de64403SJohannes Berg  * @family: genetlink message family
3173de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
3183de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
3193de64403SJohannes Berg  * @policy: validation policy
3203de64403SJohannes Berg  * @extack: extended ACK report struct
3213de64403SJohannes Berg  */
3223de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
3233de64403SJohannes Berg 				const struct genl_family *family,
3243de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
3253de64403SJohannes Berg 				const struct nla_policy *policy,
3263de64403SJohannes Berg 				struct netlink_ext_ack *extack)
3273de64403SJohannes Berg {
3283de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
3293de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
3303de64403SJohannes Berg }
3313de64403SJohannes Berg 
3323de64403SJohannes Berg /**
333670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
334670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
335670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
336670dc283SJohannes Berg  *
337670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
338670dc283SJohannes Berg  * simpler to use with generic netlink.
339670dc283SJohannes Berg  */
340670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
3410a833c29SMichal Kubecek 					      void *user_hdr)
342670dc283SJohannes Berg {
3430a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
344670dc283SJohannes Berg }
345670dc283SJohannes Berg 
346670dc283SJohannes Berg /**
34717c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
34817c157c8SThomas Graf  * @skb: socket buffer holding the message
34917c157c8SThomas Graf  * @info: receiver info
35017c157c8SThomas Graf  * @family: generic netlink family
35117c157c8SThomas Graf  * @flags: netlink message flags
35217c157c8SThomas Graf  * @cmd: generic netlink command
35317c157c8SThomas Graf  *
35417c157c8SThomas Graf  * Returns pointer to user specific header
35517c157c8SThomas Graf  */
35617c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
35717c157c8SThomas Graf 				      struct genl_info *info,
3582ae0f17dSJohannes Berg 				      const struct genl_family *family,
35917c157c8SThomas Graf 				      int flags, u8 cmd)
36017c157c8SThomas Graf {
36115e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
36217c157c8SThomas Graf 			   flags, cmd);
36317c157c8SThomas Graf }
36417c157c8SThomas Graf 
36517c157c8SThomas Graf /**
366482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
367482a8524SThomas Graf  * @skb: socket buffer the message is stored in
368482a8524SThomas Graf  * @hdr: user specific header
369482a8524SThomas Graf  */
370053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
371482a8524SThomas Graf {
372053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
373482a8524SThomas Graf }
374482a8524SThomas Graf 
375482a8524SThomas Graf /**
376482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
377482a8524SThomas Graf  * @skb: socket buffer the message is stored in
378482a8524SThomas Graf  * @hdr: generic netlink message header
379482a8524SThomas Graf  */
380bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
381482a8524SThomas Graf {
38238db9e1dSJulia Lawall 	if (hdr)
383bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
384482a8524SThomas Graf }
385482a8524SThomas Graf 
386482a8524SThomas Graf /**
387134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
38868eb5503SJohannes Berg  * @family: the generic netlink family
389134e6375SJohannes Berg  * @net: the net namespace
390134e6375SJohannes Berg  * @skb: netlink message as socket buffer
39115e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
3922a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
393134e6375SJohannes Berg  * @flags: allocation flags
394134e6375SJohannes Berg  */
3952ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
39668eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
39715e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
398134e6375SJohannes Berg {
399220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4002a94fe48SJohannes Berg 		return -EINVAL;
4012a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
40215e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
403134e6375SJohannes Berg }
404134e6375SJohannes Berg 
405134e6375SJohannes Berg /**
406134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
40768eb5503SJohannes Berg  * @family: the generic netlink family
408482a8524SThomas Graf  * @skb: netlink message as socket buffer
40915e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
4102a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
411d387f6adSThomas Graf  * @flags: allocation flags
412482a8524SThomas Graf  */
4132ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
41468eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
415d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
416482a8524SThomas Graf {
41768eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
41868eb5503SJohannes Berg 				       portid, group, flags);
419482a8524SThomas Graf }
420482a8524SThomas Graf 
421482a8524SThomas Graf /**
422134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
42368eb5503SJohannes Berg  * @family: the generic netlink family
424134e6375SJohannes Berg  * @skb: netlink message as socket buffer
42515e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
4262a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
427134e6375SJohannes Berg  * @flags: allocation flags
428134e6375SJohannes Berg  *
429134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
430134e6375SJohannes Berg  */
4312ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
43268eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
433134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
434134e6375SJohannes Berg 
435134e6375SJohannes Berg /**
436482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
437a1a824f4SJakub Kicinski  * @net: network namespace to look up @portid in
438482a8524SThomas Graf  * @skb: netlink message as socket buffer
43915e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
440482a8524SThomas Graf  */
44115e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
442482a8524SThomas Graf {
44315e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
444482a8524SThomas Graf }
445482a8524SThomas Graf 
446fb0ba6bdSBalbir Singh /**
44781878d27SThomas Graf  * genlmsg_reply - reply to a request
44881878d27SThomas Graf  * @skb: netlink message to be sent back
44981878d27SThomas Graf  * @info: receiver information
45081878d27SThomas Graf  */
45181878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
45281878d27SThomas Graf {
45315e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
45481878d27SThomas Graf }
45581878d27SThomas Graf 
45681878d27SThomas Graf /**
457a1a824f4SJakub Kicinski  * genlmsg_data - head of message payload
45870f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
459fb0ba6bdSBalbir Singh  */
460fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
461fb0ba6bdSBalbir Singh {
462fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
463fb0ba6bdSBalbir Singh }
464fb0ba6bdSBalbir Singh 
465fb0ba6bdSBalbir Singh /**
466fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
467fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
468fb0ba6bdSBalbir Singh  */
469fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
470fb0ba6bdSBalbir Singh {
471fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
472fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
473fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
474fb0ba6bdSBalbir Singh }
475fb0ba6bdSBalbir Singh 
47617db952cSBalbir Singh /**
47717db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
47817db952cSBalbir Singh  * @payload: length of message payload
47917db952cSBalbir Singh  */
48017db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
48117db952cSBalbir Singh {
48217db952cSBalbir Singh 	return GENL_HDRLEN + payload;
48317db952cSBalbir Singh }
48417db952cSBalbir Singh 
48517db952cSBalbir Singh /**
48617db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
48717db952cSBalbir Singh  * @payload: length of message payload
48817db952cSBalbir Singh  */
48917db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
49017db952cSBalbir Singh {
49117db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
49217db952cSBalbir Singh }
49317db952cSBalbir Singh 
4943dabc715SThomas Graf /**
4953dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
4963dabc715SThomas Graf  * @payload: size of the message payload
4973dabc715SThomas Graf  * @flags: the type of memory to allocate.
4983dabc715SThomas Graf  */
4993dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
5003dabc715SThomas Graf {
5013dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
5023dabc715SThomas Graf }
5033dabc715SThomas Graf 
50462b68e99SJohannes Berg /**
50562b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
50668eb5503SJohannes Berg  * @family: the generic netlink family
50762b68e99SJohannes Berg  * @net: the network namespace to report the error to
50862b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
50962b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
5102a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
51162b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
51262b68e99SJohannes Berg  *
51362b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
51462b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
51562b68e99SJohannes Berg  */
5162ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
5172ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
5182ae0f17dSJohannes Berg 			       u32 group, int code)
51962b68e99SJohannes Berg {
52091398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
52191398a09SJohannes Berg 		return -EINVAL;
52291398a09SJohannes Berg 	group = family->mcgrp_offset + group;
52362b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
52462b68e99SJohannes Berg }
5253dabc715SThomas Graf 
5262ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
527f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
5280d566379SNicolas Dichtel {
5290d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
5300d566379SNicolas Dichtel 		return -EINVAL;
5310d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
532f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
5330d566379SNicolas Dichtel }
534482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
535