xref: /openbmc/linux/include/net/genetlink.h (revision 58050fce3530939372e6c2f4b4beb76fcb4caa65)
1482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H
2482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H
3482a8524SThomas Graf 
4482a8524SThomas Graf #include <linux/genetlink.h>
5482a8524SThomas Graf #include <net/netlink.h>
6134e6375SJohannes Berg #include <net/net_namespace.h>
7482a8524SThomas Graf 
8*58050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
9*58050fceSThomas Graf 
10482a8524SThomas Graf /**
112dbba6f7SJohannes Berg  * struct genl_multicast_group - generic netlink multicast group
122dbba6f7SJohannes Berg  * @name: name of the multicast group, names are per-family
132dbba6f7SJohannes Berg  * @id: multicast group ID, assigned by the core, to use with
142dbba6f7SJohannes Berg  *      genlmsg_multicast().
152dbba6f7SJohannes Berg  * @list: list entry for linking
162dbba6f7SJohannes Berg  * @family: pointer to family, need not be set before registering
172dbba6f7SJohannes Berg  */
18fd2c3ef7SEric Dumazet struct genl_multicast_group {
192dbba6f7SJohannes Berg 	struct genl_family	*family;	/* private */
202dbba6f7SJohannes Berg 	struct list_head	list;		/* private */
212dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
222dbba6f7SJohannes Berg 	u32			id;
232dbba6f7SJohannes Berg };
242dbba6f7SJohannes Berg 
25ff4c92d8SJohannes Berg struct genl_ops;
26ff4c92d8SJohannes Berg struct genl_info;
27ff4c92d8SJohannes Berg 
282dbba6f7SJohannes Berg /**
29482a8524SThomas Graf  * struct genl_family - generic netlink family
30482a8524SThomas Graf  * @id: protocol family idenfitier
31482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
32482a8524SThomas Graf  * @name: name of family
33482a8524SThomas Graf  * @version: protocol version
34482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
35134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
36134e6375SJohannes Berg  *	namespaces and should be presented in all of them
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
41482a8524SThomas Graf  * @attrbuf: buffer to store parsed attributes
42482a8524SThomas Graf  * @ops_list: list of all assigned operations
43482a8524SThomas Graf  * @family_list: family list
442dbba6f7SJohannes Berg  * @mcast_groups: multicast groups list
45482a8524SThomas Graf  */
46fd2c3ef7SEric Dumazet struct genl_family {
47482a8524SThomas Graf 	unsigned int		id;
48482a8524SThomas Graf 	unsigned int		hdrsize;
49482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
50482a8524SThomas Graf 	unsigned int		version;
51482a8524SThomas Graf 	unsigned int		maxattr;
52134e6375SJohannes Berg 	bool			netnsok;
53ff4c92d8SJohannes Berg 	int			(*pre_doit)(struct genl_ops *ops,
54ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
55ff4c92d8SJohannes Berg 					    struct genl_info *info);
56ff4c92d8SJohannes Berg 	void			(*post_doit)(struct genl_ops *ops,
57ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
58ff4c92d8SJohannes Berg 					     struct genl_info *info);
59482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
60482a8524SThomas Graf 	struct list_head	ops_list;	/* private */
61482a8524SThomas Graf 	struct list_head	family_list;	/* private */
622dbba6f7SJohannes Berg 	struct list_head	mcast_groups;	/* private */
63482a8524SThomas Graf };
64482a8524SThomas Graf 
65482a8524SThomas Graf /**
66482a8524SThomas Graf  * struct genl_info - receiving information
67482a8524SThomas Graf  * @snd_seq: sending sequence number
68482a8524SThomas Graf  * @snd_pid: netlink pid of sender
69482a8524SThomas Graf  * @nlhdr: netlink message header
70482a8524SThomas Graf  * @genlhdr: generic netlink message header
71482a8524SThomas Graf  * @userhdr: user specific header
72482a8524SThomas Graf  * @attrs: netlink attributes
73ff4c92d8SJohannes Berg  * @_net: network namespace
74ff4c92d8SJohannes Berg  * @user_ptr: user pointers
75482a8524SThomas Graf  */
76fd2c3ef7SEric Dumazet struct genl_info {
77482a8524SThomas Graf 	u32			snd_seq;
78482a8524SThomas Graf 	u32			snd_pid;
79482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
80482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
81482a8524SThomas Graf 	void *			userhdr;
82482a8524SThomas Graf 	struct nlattr **	attrs;
83134e6375SJohannes Berg #ifdef CONFIG_NET_NS
84134e6375SJohannes Berg 	struct net *		_net;
85134e6375SJohannes Berg #endif
86ff4c92d8SJohannes Berg 	void *			user_ptr[2];
87482a8524SThomas Graf };
88482a8524SThomas Graf 
89134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
90134e6375SJohannes Berg {
91c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
92134e6375SJohannes Berg }
93134e6375SJohannes Berg 
94134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
95134e6375SJohannes Berg {
96c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
97134e6375SJohannes Berg }
98134e6375SJohannes Berg 
99482a8524SThomas Graf /**
100482a8524SThomas Graf  * struct genl_ops - generic netlink operations
101482a8524SThomas Graf  * @cmd: command identifier
102ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
103482a8524SThomas Graf  * @flags: flags
104482a8524SThomas Graf  * @policy: attribute validation policy
105482a8524SThomas Graf  * @doit: standard command callback
106482a8524SThomas Graf  * @dumpit: callback for dumpers
107a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
108482a8524SThomas Graf  * @ops_list: operations list
109482a8524SThomas Graf  */
110fd2c3ef7SEric Dumazet struct genl_ops {
111b461d2f2SPer Liden 	u8			cmd;
112ff4c92d8SJohannes Berg 	u8			internal_flags;
113482a8524SThomas Graf 	unsigned int		flags;
114ef7c79edSPatrick McHardy 	const struct nla_policy	*policy;
115482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
116482a8524SThomas Graf 				       struct genl_info *info);
117482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
118482a8524SThomas Graf 					 struct netlink_callback *cb);
119a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
120482a8524SThomas Graf 	struct list_head	ops_list;
121482a8524SThomas Graf };
122482a8524SThomas Graf 
123482a8524SThomas Graf extern int genl_register_family(struct genl_family *family);
124a7b11d73SMichał Mirosław extern int genl_register_family_with_ops(struct genl_family *family,
125a7b11d73SMichał Mirosław 	struct genl_ops *ops, size_t n_ops);
126482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family);
127482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
128482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
1292dbba6f7SJohannes Berg extern int genl_register_mc_group(struct genl_family *family,
1302dbba6f7SJohannes Berg 				  struct genl_multicast_group *grp);
1312dbba6f7SJohannes Berg extern void genl_unregister_mc_group(struct genl_family *family,
1322dbba6f7SJohannes Berg 				     struct genl_multicast_group *grp);
133263ba61dSPravin B Shelar extern void genl_notify(struct sk_buff *skb, struct net *net, u32 pid,
134263ba61dSPravin B Shelar 			u32 group, struct nlmsghdr *nlh, gfp_t flags);
135482a8524SThomas Graf 
136a46621a3SDenys Vlasenko void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
137a46621a3SDenys Vlasenko 				struct genl_family *family, int flags, u8 cmd);
138482a8524SThomas Graf 
139482a8524SThomas Graf /**
140670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
141670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
142670dc283SJohannes Berg  * @family: generic netlink family
143670dc283SJohannes Berg  *
144670dc283SJohannes Berg  * Returns pointer to netlink header.
145670dc283SJohannes Berg  */
146670dc283SJohannes Berg static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
147670dc283SJohannes Berg 					     struct genl_family *family)
148670dc283SJohannes Berg {
149670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
150670dc283SJohannes Berg 				   family->hdrsize -
151670dc283SJohannes Berg 				   GENL_HDRLEN -
152670dc283SJohannes Berg 				   NLMSG_HDRLEN);
153670dc283SJohannes Berg }
154670dc283SJohannes Berg 
155670dc283SJohannes Berg /**
156670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
157670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
158670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
159670dc283SJohannes Berg  * @family: generic netlink family
160670dc283SJohannes Berg  *
161670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
162670dc283SJohannes Berg  * simpler to use with generic netlink.
163670dc283SJohannes Berg  */
164670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
165670dc283SJohannes Berg 					      void *user_hdr,
166670dc283SJohannes Berg 					      struct genl_family *family)
167670dc283SJohannes Berg {
168670dc283SJohannes Berg 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
169670dc283SJohannes Berg }
170670dc283SJohannes Berg 
171670dc283SJohannes Berg /**
17217c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
17317c157c8SThomas Graf  * @skb: socket buffer holding the message
17417c157c8SThomas Graf  * @info: receiver info
17517c157c8SThomas Graf  * @family: generic netlink family
17617c157c8SThomas Graf  * @flags: netlink message flags
17717c157c8SThomas Graf  * @cmd: generic netlink command
17817c157c8SThomas Graf  *
17917c157c8SThomas Graf  * Returns pointer to user specific header
18017c157c8SThomas Graf  */
18117c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
18217c157c8SThomas Graf 				      struct genl_info *info,
18317c157c8SThomas Graf 				      struct genl_family *family,
18417c157c8SThomas Graf 				      int flags, u8 cmd)
18517c157c8SThomas Graf {
18617c157c8SThomas Graf 	return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
18717c157c8SThomas Graf 			   flags, cmd);
18817c157c8SThomas Graf }
18917c157c8SThomas Graf 
19017c157c8SThomas Graf /**
191482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
192482a8524SThomas Graf  * @skb: socket buffer the message is stored in
193482a8524SThomas Graf  * @hdr: user specific header
194482a8524SThomas Graf  */
195482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
196482a8524SThomas Graf {
197482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
198482a8524SThomas Graf }
199482a8524SThomas Graf 
200482a8524SThomas Graf /**
201482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
202482a8524SThomas Graf  * @skb: socket buffer the message is stored in
203482a8524SThomas Graf  * @hdr: generic netlink message header
204482a8524SThomas Graf  */
205bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
206482a8524SThomas Graf {
20738db9e1dSJulia Lawall 	if (hdr)
208bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
209482a8524SThomas Graf }
210482a8524SThomas Graf 
211482a8524SThomas Graf /**
212134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
213134e6375SJohannes Berg  * @net: the net namespace
214134e6375SJohannes Berg  * @skb: netlink message as socket buffer
215134e6375SJohannes Berg  * @pid: own netlink pid to avoid sending to yourself
216134e6375SJohannes Berg  * @group: multicast group id
217134e6375SJohannes Berg  * @flags: allocation flags
218134e6375SJohannes Berg  */
219134e6375SJohannes Berg static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
220134e6375SJohannes Berg 					  u32 pid, unsigned int group, gfp_t flags)
221134e6375SJohannes Berg {
222134e6375SJohannes Berg 	return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
223134e6375SJohannes Berg }
224134e6375SJohannes Berg 
225134e6375SJohannes Berg /**
226134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
227482a8524SThomas Graf  * @skb: netlink message as socket buffer
228482a8524SThomas Graf  * @pid: own netlink pid to avoid sending to yourself
229482a8524SThomas Graf  * @group: multicast group id
230d387f6adSThomas Graf  * @flags: allocation flags
231482a8524SThomas Graf  */
232482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
233d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
234482a8524SThomas Graf {
235134e6375SJohannes Berg 	return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
236482a8524SThomas Graf }
237482a8524SThomas Graf 
238482a8524SThomas Graf /**
239134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
240134e6375SJohannes Berg  * @skb: netlink message as socket buffer
241134e6375SJohannes Berg  * @pid: own netlink pid to avoid sending to yourself
242134e6375SJohannes Berg  * @group: multicast group id
243134e6375SJohannes Berg  * @flags: allocation flags
244134e6375SJohannes Berg  *
245134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
246134e6375SJohannes Berg  */
247134e6375SJohannes Berg int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
248134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
249134e6375SJohannes Berg 
250134e6375SJohannes Berg /**
251482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
252482a8524SThomas Graf  * @skb: netlink message as socket buffer
253482a8524SThomas Graf  * @pid: netlink pid of the destination socket
254482a8524SThomas Graf  */
255134e6375SJohannes Berg static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
256482a8524SThomas Graf {
257134e6375SJohannes Berg 	return nlmsg_unicast(net->genl_sock, skb, pid);
258482a8524SThomas Graf }
259482a8524SThomas Graf 
260fb0ba6bdSBalbir Singh /**
26181878d27SThomas Graf  * genlmsg_reply - reply to a request
26281878d27SThomas Graf  * @skb: netlink message to be sent back
26381878d27SThomas Graf  * @info: receiver information
26481878d27SThomas Graf  */
26581878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
26681878d27SThomas Graf {
267134e6375SJohannes Berg 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
26881878d27SThomas Graf }
26981878d27SThomas Graf 
27081878d27SThomas Graf /**
271fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
27270f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
273fb0ba6bdSBalbir Singh  */
274fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
275fb0ba6bdSBalbir Singh {
276fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
277fb0ba6bdSBalbir Singh }
278fb0ba6bdSBalbir Singh 
279fb0ba6bdSBalbir Singh /**
280fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
281fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
282fb0ba6bdSBalbir Singh  */
283fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
284fb0ba6bdSBalbir Singh {
285fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
286fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
287fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
288fb0ba6bdSBalbir Singh }
289fb0ba6bdSBalbir Singh 
29017db952cSBalbir Singh /**
29117db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
29217db952cSBalbir Singh  * @payload: length of message payload
29317db952cSBalbir Singh  */
29417db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
29517db952cSBalbir Singh {
29617db952cSBalbir Singh 	return GENL_HDRLEN + payload;
29717db952cSBalbir Singh }
29817db952cSBalbir Singh 
29917db952cSBalbir Singh /**
30017db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
30117db952cSBalbir Singh  * @payload: length of message payload
30217db952cSBalbir Singh  */
30317db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
30417db952cSBalbir Singh {
30517db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
30617db952cSBalbir Singh }
30717db952cSBalbir Singh 
3083dabc715SThomas Graf /**
3093dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
3103dabc715SThomas Graf  * @payload: size of the message payload
3113dabc715SThomas Graf  * @flags: the type of memory to allocate.
3123dabc715SThomas Graf  */
3133dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
3143dabc715SThomas Graf {
3153dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
3163dabc715SThomas Graf }
3173dabc715SThomas Graf 
3183dabc715SThomas Graf 
319482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
320