xref: /openbmc/linux/include/net/genetlink.h (revision e036a325)
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)
15*e036a325SIdo Schimmel  * @cap_sys_admin: whether %CAP_SYS_ADMIN is required for binding
162dbba6f7SJohannes Berg  */
17fd2c3ef7SEric Dumazet struct genl_multicast_group {
182dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
194d54cc32SFlorian Westphal 	u8			flags;
20*e036a325SIdo Schimmel 	u8			cap_sys_admin:1;
212dbba6f7SJohannes Berg };
222dbba6f7SJohannes Berg 
2320b0b53aSJakub Kicinski struct genl_split_ops;
24ff4c92d8SJohannes Berg struct genl_info;
25ff4c92d8SJohannes Berg 
262dbba6f7SJohannes Berg /**
27482a8524SThomas Graf  * struct genl_family - generic netlink family
28482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
29482a8524SThomas Graf  * @name: name of family
30482a8524SThomas Graf  * @version: protocol version
31482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
323b0f31f2SJohannes Berg  * @policy: netlink policy
33134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
34134e6375SJohannes Berg  *	namespaces and should be presented in all of them
35f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
36f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
37ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
38ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
39ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
40ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
41a1a824f4SJakub Kicinski  * @module: pointer to the owning module (set to THIS_MODULE)
42489111e5SJohannes Berg  * @mcgrps: multicast groups used by this family
43489111e5SJohannes Berg  * @n_mcgrps: number of multicast groups
449c5d03d3SJakub Kicinski  * @resv_start_op: first operation for which reserved fields of the header
454fa86555SJakub Kicinski  *	can be validated and policies are required (see below);
464fa86555SJakub Kicinski  *	new families should leave this field at zero
47489111e5SJohannes Berg  * @ops: the operations supported by this family
48489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
490b588afdSJakub Kicinski  * @small_ops: the small-struct operations supported by this family
500b588afdSJakub Kicinski  * @n_small_ops: number of small-struct operations supported by this family
51b8fd60c3SJakub Kicinski  * @split_ops: the split do/dump form of operation definition
52b8fd60c3SJakub Kicinski  * @n_split_ops: number of entries in @split_ops, not that with split do/dump
53b8fd60c3SJakub Kicinski  *	ops the number of entries is not the same as number of commands
544fa86555SJakub Kicinski  *
554fa86555SJakub Kicinski  * Attribute policies (the combination of @policy and @maxattr fields)
564fa86555SJakub Kicinski  * can be attached at the family level or at the operation level.
574fa86555SJakub Kicinski  * If both are present the per-operation policy takes precedence.
584fa86555SJakub Kicinski  * For operations before @resv_start_op lack of policy means that the core
594fa86555SJakub Kicinski  * will perform no attribute parsing or validation. For newer operations
604fa86555SJakub Kicinski  * if policy is not provided core will reject all TLV attributes.
61482a8524SThomas Graf  */
62fd2c3ef7SEric Dumazet struct genl_family {
63482a8524SThomas Graf 	unsigned int		hdrsize;
64482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
65482a8524SThomas Graf 	unsigned int		version;
66482a8524SThomas Graf 	unsigned int		maxattr;
67e5086736SJakub Kicinski 	u8			netnsok:1;
68e5086736SJakub Kicinski 	u8			parallel_ops:1;
69e5086736SJakub Kicinski 	u8			n_ops;
700b588afdSJakub Kicinski 	u8			n_small_ops;
71b8fd60c3SJakub Kicinski 	u8			n_split_ops;
72e5086736SJakub Kicinski 	u8			n_mcgrps;
739c5d03d3SJakub Kicinski 	u8			resv_start_op;
743b0f31f2SJohannes Berg 	const struct nla_policy *policy;
7520b0b53aSJakub Kicinski 	int			(*pre_doit)(const struct genl_split_ops *ops,
76ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
77ff4c92d8SJohannes Berg 					    struct genl_info *info);
7820b0b53aSJakub Kicinski 	void			(*post_doit)(const struct genl_split_ops *ops,
79ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
80ff4c92d8SJohannes Berg 					     struct genl_info *info);
81489111e5SJohannes Berg 	const struct genl_ops *	ops;
820b588afdSJakub Kicinski 	const struct genl_small_ops *small_ops;
83b8fd60c3SJakub Kicinski 	const struct genl_split_ops *split_ops;
84489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
8533c6b1f6SPravin B Shelar 	struct module		*module;
867c3eaa02SJakub Kicinski 
877c3eaa02SJakub Kicinski /* private: internal use only */
887c3eaa02SJakub Kicinski 	/* protocol family identifier */
897c3eaa02SJakub Kicinski 	int			id;
907c3eaa02SJakub Kicinski 	/* starting number of multicast group IDs in this family */
917c3eaa02SJakub Kicinski 	unsigned int		mcgrp_offset;
92482a8524SThomas Graf };
93482a8524SThomas Graf 
94482a8524SThomas Graf /**
95482a8524SThomas Graf  * struct genl_info - receiving information
96482a8524SThomas Graf  * @snd_seq: sending sequence number
9715e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
985c670a01SJakub Kicinski  * @family: generic netlink family
99482a8524SThomas Graf  * @nlhdr: netlink message header
100482a8524SThomas Graf  * @genlhdr: generic netlink message header
101482a8524SThomas Graf  * @attrs: netlink attributes
102ff4c92d8SJohannes Berg  * @_net: network namespace
103ff4c92d8SJohannes Berg  * @user_ptr: user pointers
1047ab606d1SJohannes Berg  * @extack: extended ACK report struct
105482a8524SThomas Graf  */
106fd2c3ef7SEric Dumazet struct genl_info {
107482a8524SThomas Graf 	u32			snd_seq;
10815e47304SEric W. Biederman 	u32			snd_portid;
1095c670a01SJakub Kicinski 	const struct genl_family *family;
110fde9bd4aSJakub Kicinski 	const struct nlmsghdr *	nlhdr;
111482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
112482a8524SThomas Graf 	struct nlattr **	attrs;
1130c5c9fb5SEric W. Biederman 	possible_net_t		_net;
114ff4c92d8SJohannes Berg 	void *			user_ptr[2];
1157ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
116482a8524SThomas Graf };
117482a8524SThomas Graf 
genl_info_net(const struct genl_info * info)1185aa51d9fSJakub Kicinski static inline struct net *genl_info_net(const struct genl_info *info)
119134e6375SJohannes Berg {
120c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
121134e6375SJohannes Berg }
122134e6375SJohannes Berg 
genl_info_net_set(struct genl_info * info,struct net * net)123134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
124134e6375SJohannes Berg {
125c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
126134e6375SJohannes Berg }
127134e6375SJohannes Berg 
genl_info_userhdr(const struct genl_info * info)128bffcc688SJakub Kicinski static inline void *genl_info_userhdr(const struct genl_info *info)
129bffcc688SJakub Kicinski {
130bffcc688SJakub Kicinski 	return (u8 *)info->genlhdr + GENL_HDRLEN;
131bffcc688SJakub Kicinski }
132bffcc688SJakub Kicinski 
1337ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1347ab606d1SJohannes Berg 
135a3400e87SPaolo Abeni #define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
136a3400e87SPaolo Abeni 	NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
137a3400e87SPaolo Abeni 
13845dca157SJakub Kicinski /* Report that a root attribute is missing */
13945dca157SJakub Kicinski #define GENL_REQ_ATTR_CHECK(info, attr) ({				\
14045dca157SJakub Kicinski 	struct genl_info *__info = (info);				\
14145dca157SJakub Kicinski 									\
14245dca157SJakub Kicinski 	NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
14345dca157SJakub Kicinski })
14445dca157SJakub Kicinski 
145ef6243acSJohannes Berg enum genl_validate_flags {
146ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
147ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
148ef6243acSJohannes Berg 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
149ef6243acSJohannes Berg };
150ef6243acSJohannes Berg 
151482a8524SThomas Graf /**
1520b588afdSJakub Kicinski  * struct genl_small_ops - generic netlink operations (small version)
1530b588afdSJakub Kicinski  * @cmd: command identifier
1540b588afdSJakub Kicinski  * @internal_flags: flags used by the family
1555c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
1560b588afdSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
1570b588afdSJakub Kicinski  * @doit: standard command callback
1580b588afdSJakub Kicinski  * @dumpit: callback for dumpers
1590b588afdSJakub Kicinski  *
1600b588afdSJakub Kicinski  * This is a cut-down version of struct genl_ops for users who don't need
1610b588afdSJakub Kicinski  * most of the ancillary infra and want to save space.
1621927f41aSJiri Pirko  */
1630b588afdSJakub Kicinski struct genl_small_ops {
1640b588afdSJakub Kicinski 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
1650b588afdSJakub Kicinski 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
1660b588afdSJakub Kicinski 	u8	cmd;
1670b588afdSJakub Kicinski 	u8	internal_flags;
1680b588afdSJakub Kicinski 	u8	flags;
1690b588afdSJakub Kicinski 	u8	validate;
1701927f41aSJiri Pirko };
1711927f41aSJiri Pirko 
1721927f41aSJiri Pirko /**
173482a8524SThomas Graf  * struct genl_ops - generic netlink operations
174482a8524SThomas Graf  * @cmd: command identifier
175ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
1765c221f0aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
17748526a0fSJakub Kicinski  * @maxattr: maximum number of attributes supported
17848526a0fSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
1793ddf9b43SJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
180482a8524SThomas Graf  * @doit: standard command callback
181fc9e50f5STom Herbert  * @start: start callback for dumps
182482a8524SThomas Graf  * @dumpit: callback for dumpers
183a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
184482a8524SThomas Graf  */
185fd2c3ef7SEric Dumazet struct genl_ops {
186482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
187482a8524SThomas Graf 				       struct genl_info *info);
188fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
189482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
190482a8524SThomas Graf 					 struct netlink_callback *cb);
191a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
19248526a0fSJakub Kicinski 	const struct nla_policy *policy;
19348526a0fSJakub Kicinski 	unsigned int		maxattr;
1943f5ccd06SJohannes Berg 	u8			cmd;
1953f5ccd06SJohannes Berg 	u8			internal_flags;
1963f5ccd06SJohannes Berg 	u8			flags;
197ef6243acSJohannes Berg 	u8			validate;
198482a8524SThomas Graf };
199482a8524SThomas Graf 
2000b588afdSJakub Kicinski /**
20120b0b53aSJakub Kicinski  * struct genl_split_ops - generic netlink operations (do/dump split version)
20220b0b53aSJakub Kicinski  * @cmd: command identifier
20320b0b53aSJakub Kicinski  * @internal_flags: flags used by the family
20420b0b53aSJakub Kicinski  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
20520b0b53aSJakub Kicinski  * @validate: validation flags from enum genl_validate_flags
20620b0b53aSJakub Kicinski  * @policy: netlink policy (takes precedence over family policy)
20720b0b53aSJakub Kicinski  * @maxattr: maximum number of attributes supported
20820b0b53aSJakub Kicinski  *
20920b0b53aSJakub Kicinski  * Do callbacks:
21020b0b53aSJakub Kicinski  * @pre_doit: called before an operation's @doit callback, it may
21120b0b53aSJakub Kicinski  *	do additional, common, filtering and return an error
21220b0b53aSJakub Kicinski  * @doit: standard command callback
21320b0b53aSJakub Kicinski  * @post_doit: called after an operation's @doit callback, it may
21420b0b53aSJakub Kicinski  *	undo operations done by pre_doit, for example release locks
21520b0b53aSJakub Kicinski  *
21620b0b53aSJakub Kicinski  * Dump callbacks:
21720b0b53aSJakub Kicinski  * @start: start callback for dumps
21820b0b53aSJakub Kicinski  * @dumpit: callback for dumpers
21920b0b53aSJakub Kicinski  * @done: completion callback for dumps
22020b0b53aSJakub Kicinski  *
22120b0b53aSJakub Kicinski  * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
22220b0b53aSJakub Kicinski  * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
22320b0b53aSJakub Kicinski  * Exactly one of those flags must be set.
22420b0b53aSJakub Kicinski  */
22520b0b53aSJakub Kicinski struct genl_split_ops {
22620b0b53aSJakub Kicinski 	union {
22720b0b53aSJakub Kicinski 		struct {
22820b0b53aSJakub Kicinski 			int (*pre_doit)(const struct genl_split_ops *ops,
22920b0b53aSJakub Kicinski 					struct sk_buff *skb,
23020b0b53aSJakub Kicinski 					struct genl_info *info);
23120b0b53aSJakub Kicinski 			int (*doit)(struct sk_buff *skb,
23220b0b53aSJakub Kicinski 				    struct genl_info *info);
23320b0b53aSJakub Kicinski 			void (*post_doit)(const struct genl_split_ops *ops,
23420b0b53aSJakub Kicinski 					  struct sk_buff *skb,
23520b0b53aSJakub Kicinski 					  struct genl_info *info);
23620b0b53aSJakub Kicinski 		};
23720b0b53aSJakub Kicinski 		struct {
23820b0b53aSJakub Kicinski 			int (*start)(struct netlink_callback *cb);
23920b0b53aSJakub Kicinski 			int (*dumpit)(struct sk_buff *skb,
24020b0b53aSJakub Kicinski 				      struct netlink_callback *cb);
24120b0b53aSJakub Kicinski 			int (*done)(struct netlink_callback *cb);
24220b0b53aSJakub Kicinski 		};
24320b0b53aSJakub Kicinski 	};
24420b0b53aSJakub Kicinski 	const struct nla_policy *policy;
24520b0b53aSJakub Kicinski 	unsigned int		maxattr;
24620b0b53aSJakub Kicinski 	u8			cmd;
24720b0b53aSJakub Kicinski 	u8			internal_flags;
24820b0b53aSJakub Kicinski 	u8			flags;
24920b0b53aSJakub Kicinski 	u8			validate;
25020b0b53aSJakub Kicinski };
25120b0b53aSJakub Kicinski 
25220b0b53aSJakub Kicinski /**
253a1a824f4SJakub Kicinski  * struct genl_dumpit_info - info that is available during dumpit op call
254a1a824f4SJakub Kicinski  * @op: generic netlink ops - for internal genl code usage
2550b588afdSJakub Kicinski  * @attrs: netlink attributes
2569272af10SJakub Kicinski  * @info: struct genl_info describing the request
2570b588afdSJakub Kicinski  */
2580b588afdSJakub Kicinski struct genl_dumpit_info {
25920b0b53aSJakub Kicinski 	struct genl_split_ops op;
2609272af10SJakub Kicinski 	struct genl_info info;
2610b588afdSJakub Kicinski };
2620b588afdSJakub Kicinski 
2630b588afdSJakub Kicinski static inline const struct genl_dumpit_info *
genl_dumpit_info(struct netlink_callback * cb)2640b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb)
2650b588afdSJakub Kicinski {
2660b588afdSJakub Kicinski 	return cb->data;
2670b588afdSJakub Kicinski }
2680b588afdSJakub Kicinski 
2699272af10SJakub Kicinski static inline const struct genl_info *
genl_info_dump(struct netlink_callback * cb)2709272af10SJakub Kicinski genl_info_dump(struct netlink_callback *cb)
2719272af10SJakub Kicinski {
2729272af10SJakub Kicinski 	return &genl_dumpit_info(cb)->info;
2739272af10SJakub Kicinski }
2749272af10SJakub Kicinski 
2755aa51d9fSJakub Kicinski /**
2765aa51d9fSJakub Kicinski  * genl_info_init_ntf() - initialize genl_info for notifications
2775aa51d9fSJakub Kicinski  * @info:   genl_info struct to set up
2785aa51d9fSJakub Kicinski  * @family: pointer to the genetlink family
2795aa51d9fSJakub Kicinski  * @cmd:    command to be used in the notification
2805aa51d9fSJakub Kicinski  *
2815aa51d9fSJakub Kicinski  * Initialize a locally declared struct genl_info to pass to various APIs.
2825aa51d9fSJakub Kicinski  * Intended to be used when creating notifications.
2835aa51d9fSJakub Kicinski  */
2845aa51d9fSJakub Kicinski static inline void
genl_info_init_ntf(struct genl_info * info,const struct genl_family * family,u8 cmd)2855aa51d9fSJakub Kicinski genl_info_init_ntf(struct genl_info *info, const struct genl_family *family,
2865aa51d9fSJakub Kicinski 		   u8 cmd)
2875aa51d9fSJakub Kicinski {
2885aa51d9fSJakub Kicinski 	struct genlmsghdr *hdr = (void *) &info->user_ptr[0];
2895aa51d9fSJakub Kicinski 
2905aa51d9fSJakub Kicinski 	memset(info, 0, sizeof(*info));
2915aa51d9fSJakub Kicinski 	info->family = family;
2925aa51d9fSJakub Kicinski 	info->genlhdr = hdr;
2935aa51d9fSJakub Kicinski 	hdr->cmd = cmd;
2945aa51d9fSJakub Kicinski }
2955aa51d9fSJakub Kicinski 
genl_info_is_ntf(const struct genl_info * info)2965aa51d9fSJakub Kicinski static inline bool genl_info_is_ntf(const struct genl_info *info)
2975aa51d9fSJakub Kicinski {
2985aa51d9fSJakub Kicinski 	return !info->nlhdr;
2995aa51d9fSJakub Kicinski }
3005aa51d9fSJakub Kicinski 
301489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
3022ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
3032ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
30492c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
305482a8524SThomas Graf 
30615e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
3072ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
308482a8524SThomas Graf 
3095aa51d9fSJakub Kicinski static inline void *
__genlmsg_iput(struct sk_buff * skb,const struct genl_info * info,int flags)3105aa51d9fSJakub Kicinski __genlmsg_iput(struct sk_buff *skb, const struct genl_info *info, int flags)
3115aa51d9fSJakub Kicinski {
3125aa51d9fSJakub Kicinski 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, info->family,
3135aa51d9fSJakub Kicinski 			   flags, info->genlhdr->cmd);
3145aa51d9fSJakub Kicinski }
3155aa51d9fSJakub Kicinski 
3165aa51d9fSJakub Kicinski /**
3175aa51d9fSJakub Kicinski  * genlmsg_iput - start genetlink message based on genl_info
3185aa51d9fSJakub Kicinski  * @skb: skb in which message header will be placed
3195aa51d9fSJakub Kicinski  * @info: genl_info as provided to do/dump handlers
3205aa51d9fSJakub Kicinski  *
3215aa51d9fSJakub Kicinski  * Convenience wrapper which starts a genetlink message based on
3225aa51d9fSJakub Kicinski  * information in user request. @info should be either the struct passed
3235aa51d9fSJakub Kicinski  * by genetlink core to do/dump handlers (when constructing replies to
3245aa51d9fSJakub Kicinski  * such requests) or a struct initialized by genl_info_init_ntf()
3255aa51d9fSJakub Kicinski  * when constructing notifications.
3265aa51d9fSJakub Kicinski  *
3275aa51d9fSJakub Kicinski  * Returns pointer to new genetlink header.
3285aa51d9fSJakub Kicinski  */
3295aa51d9fSJakub Kicinski static inline void *
genlmsg_iput(struct sk_buff * skb,const struct genl_info * info)3305aa51d9fSJakub Kicinski genlmsg_iput(struct sk_buff *skb, const struct genl_info *info)
3315aa51d9fSJakub Kicinski {
3325aa51d9fSJakub Kicinski 	return __genlmsg_iput(skb, info, 0);
3335aa51d9fSJakub Kicinski }
3345aa51d9fSJakub Kicinski 
335482a8524SThomas Graf /**
336670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
337670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
338670dc283SJohannes Berg  *
339670dc283SJohannes Berg  * Returns pointer to netlink header.
340670dc283SJohannes Berg  */
genlmsg_nlhdr(void * user_hdr)3410a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
342670dc283SJohannes Berg {
343670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
344670dc283SJohannes Berg 				   GENL_HDRLEN -
345670dc283SJohannes Berg 				   NLMSG_HDRLEN);
346670dc283SJohannes Berg }
347670dc283SJohannes Berg 
348670dc283SJohannes Berg /**
3498cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
3507b1883ceSJoe Stringer  * @nlh: netlink message header
3517b1883ceSJoe Stringer  * @family: genetlink message family
3527b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
3537b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
3547b1883ceSJoe Stringer  * @policy: validation policy
355fceb6435SJohannes Berg  * @extack: extended ACK report struct
356fceb6435SJohannes Berg  */
genlmsg_parse_deprecated(const struct nlmsghdr * nlh,const struct genl_family * family,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)3578cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
3587b1883ceSJoe Stringer 					   const struct genl_family *family,
3597b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
360fceb6435SJohannes Berg 					   const struct nla_policy *policy,
361fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
3627b1883ceSJoe Stringer {
3638cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
3648cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
3657b1883ceSJoe Stringer }
3667b1883ceSJoe Stringer 
3677b1883ceSJoe Stringer /**
3683de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
3693de64403SJohannes Berg  * @nlh: netlink message header
3703de64403SJohannes Berg  * @family: genetlink message family
3713de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
3723de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
3733de64403SJohannes Berg  * @policy: validation policy
3743de64403SJohannes Berg  * @extack: extended ACK report struct
3753de64403SJohannes Berg  */
genlmsg_parse(const struct nlmsghdr * nlh,const struct genl_family * family,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)3763de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
3773de64403SJohannes Berg 				const struct genl_family *family,
3783de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
3793de64403SJohannes Berg 				const struct nla_policy *policy,
3803de64403SJohannes Berg 				struct netlink_ext_ack *extack)
3813de64403SJohannes Berg {
3823de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
3833de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
3843de64403SJohannes Berg }
3853de64403SJohannes Berg 
3863de64403SJohannes Berg /**
387670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
388670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
389670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
390670dc283SJohannes Berg  *
391670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
392670dc283SJohannes Berg  * simpler to use with generic netlink.
393670dc283SJohannes Berg  */
genl_dump_check_consistent(struct netlink_callback * cb,void * user_hdr)394670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
3950a833c29SMichal Kubecek 					      void *user_hdr)
396670dc283SJohannes Berg {
3970a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
398670dc283SJohannes Berg }
399670dc283SJohannes Berg 
400670dc283SJohannes Berg /**
40117c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
40217c157c8SThomas Graf  * @skb: socket buffer holding the message
40317c157c8SThomas Graf  * @info: receiver info
40417c157c8SThomas Graf  * @family: generic netlink family
40517c157c8SThomas Graf  * @flags: netlink message flags
40617c157c8SThomas Graf  * @cmd: generic netlink command
40717c157c8SThomas Graf  *
40817c157c8SThomas Graf  * Returns pointer to user specific header
40917c157c8SThomas Graf  */
genlmsg_put_reply(struct sk_buff * skb,struct genl_info * info,const struct genl_family * family,int flags,u8 cmd)41017c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
41117c157c8SThomas Graf 				      struct genl_info *info,
4122ae0f17dSJohannes Berg 				      const struct genl_family *family,
41317c157c8SThomas Graf 				      int flags, u8 cmd)
41417c157c8SThomas Graf {
41515e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
41617c157c8SThomas Graf 			   flags, cmd);
41717c157c8SThomas Graf }
41817c157c8SThomas Graf 
41917c157c8SThomas Graf /**
420482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
421482a8524SThomas Graf  * @skb: socket buffer the message is stored in
422482a8524SThomas Graf  * @hdr: user specific header
423482a8524SThomas Graf  */
genlmsg_end(struct sk_buff * skb,void * hdr)424053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
425482a8524SThomas Graf {
426053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
427482a8524SThomas Graf }
428482a8524SThomas Graf 
429482a8524SThomas Graf /**
430482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
431482a8524SThomas Graf  * @skb: socket buffer the message is stored in
432482a8524SThomas Graf  * @hdr: generic netlink message header
433482a8524SThomas Graf  */
genlmsg_cancel(struct sk_buff * skb,void * hdr)434bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
435482a8524SThomas Graf {
43638db9e1dSJulia Lawall 	if (hdr)
437bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
438482a8524SThomas Graf }
439482a8524SThomas Graf 
440482a8524SThomas Graf /**
441134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
44268eb5503SJohannes Berg  * @family: the generic netlink family
443134e6375SJohannes Berg  * @net: the net namespace
444134e6375SJohannes Berg  * @skb: netlink message as socket buffer
44515e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
4462a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
447134e6375SJohannes Berg  * @flags: allocation flags
448134e6375SJohannes Berg  */
genlmsg_multicast_netns(const struct genl_family * family,struct net * net,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags)4492ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
45068eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
45115e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
452134e6375SJohannes Berg {
453220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4542a94fe48SJohannes Berg 		return -EINVAL;
4552a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
45615e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
457134e6375SJohannes Berg }
458134e6375SJohannes Berg 
459134e6375SJohannes Berg /**
460134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
46168eb5503SJohannes Berg  * @family: the generic netlink family
462482a8524SThomas Graf  * @skb: netlink message as socket buffer
46315e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
4642a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
465d387f6adSThomas Graf  * @flags: allocation flags
466482a8524SThomas Graf  */
genlmsg_multicast(const struct genl_family * family,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags)4672ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
46868eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
469d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
470482a8524SThomas Graf {
47168eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
47268eb5503SJohannes Berg 				       portid, group, flags);
473482a8524SThomas Graf }
474482a8524SThomas Graf 
475482a8524SThomas Graf /**
476134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
47768eb5503SJohannes Berg  * @family: the generic netlink family
478134e6375SJohannes Berg  * @skb: netlink message as socket buffer
47915e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
4802a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
481134e6375SJohannes Berg  * @flags: allocation flags
482134e6375SJohannes Berg  *
483134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
484134e6375SJohannes Berg  */
4852ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
48668eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
487134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
488134e6375SJohannes Berg 
489134e6375SJohannes Berg /**
490482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
491a1a824f4SJakub Kicinski  * @net: network namespace to look up @portid in
492482a8524SThomas Graf  * @skb: netlink message as socket buffer
49315e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
494482a8524SThomas Graf  */
genlmsg_unicast(struct net * net,struct sk_buff * skb,u32 portid)49515e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
496482a8524SThomas Graf {
49715e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
498482a8524SThomas Graf }
499482a8524SThomas Graf 
500fb0ba6bdSBalbir Singh /**
50181878d27SThomas Graf  * genlmsg_reply - reply to a request
50281878d27SThomas Graf  * @skb: netlink message to be sent back
50381878d27SThomas Graf  * @info: receiver information
50481878d27SThomas Graf  */
genlmsg_reply(struct sk_buff * skb,struct genl_info * info)50581878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
50681878d27SThomas Graf {
50715e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
50881878d27SThomas Graf }
50981878d27SThomas Graf 
51081878d27SThomas Graf /**
511a1a824f4SJakub Kicinski  * genlmsg_data - head of message payload
51270f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
513fb0ba6bdSBalbir Singh  */
genlmsg_data(const struct genlmsghdr * gnlh)514fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
515fb0ba6bdSBalbir Singh {
516fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
517fb0ba6bdSBalbir Singh }
518fb0ba6bdSBalbir Singh 
519fb0ba6bdSBalbir Singh /**
520fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
521fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
522fb0ba6bdSBalbir Singh  */
genlmsg_len(const struct genlmsghdr * gnlh)523fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
524fb0ba6bdSBalbir Singh {
525fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
526fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
527fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
528fb0ba6bdSBalbir Singh }
529fb0ba6bdSBalbir Singh 
53017db952cSBalbir Singh /**
53117db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
53217db952cSBalbir Singh  * @payload: length of message payload
53317db952cSBalbir Singh  */
genlmsg_msg_size(int payload)53417db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
53517db952cSBalbir Singh {
53617db952cSBalbir Singh 	return GENL_HDRLEN + payload;
53717db952cSBalbir Singh }
53817db952cSBalbir Singh 
53917db952cSBalbir Singh /**
54017db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
54117db952cSBalbir Singh  * @payload: length of message payload
54217db952cSBalbir Singh  */
genlmsg_total_size(int payload)54317db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
54417db952cSBalbir Singh {
54517db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
54617db952cSBalbir Singh }
54717db952cSBalbir Singh 
5483dabc715SThomas Graf /**
5493dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
5503dabc715SThomas Graf  * @payload: size of the message payload
5513dabc715SThomas Graf  * @flags: the type of memory to allocate.
5523dabc715SThomas Graf  */
genlmsg_new(size_t payload,gfp_t flags)5533dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
5543dabc715SThomas Graf {
5553dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
5563dabc715SThomas Graf }
5573dabc715SThomas Graf 
55862b68e99SJohannes Berg /**
55962b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
56068eb5503SJohannes Berg  * @family: the generic netlink family
56162b68e99SJohannes Berg  * @net: the network namespace to report the error to
56262b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
56362b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
5642a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
56562b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
56662b68e99SJohannes Berg  *
56762b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
56862b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
56962b68e99SJohannes Berg  */
genl_set_err(const struct genl_family * family,struct net * net,u32 portid,u32 group,int code)5702ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
5712ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
5722ae0f17dSJohannes Berg 			       u32 group, int code)
57362b68e99SJohannes Berg {
57491398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
57591398a09SJohannes Berg 		return -EINVAL;
57691398a09SJohannes Berg 	group = family->mcgrp_offset + group;
57762b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
57862b68e99SJohannes Berg }
5793dabc715SThomas Graf 
genl_has_listeners(const struct genl_family * family,struct net * net,unsigned int group)5802ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
581f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
5820d566379SNicolas Dichtel {
5830d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
5840d566379SNicolas Dichtel 		return -EINVAL;
5850d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
586f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
5870d566379SNicolas Dichtel }
588482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
589