xref: /openbmc/linux/include/net/genetlink.h (revision c53ed7423619b4e8108914a9f31b426dd58ad591)
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 
858050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
958050fceSThomas 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  * @family_list: family list
432dbba6f7SJohannes Berg  * @mcast_groups: multicast groups list
44d91824c0SJohannes Berg  * @ops: the operations supported by this family (private)
45d91824c0SJohannes Berg  * @n_ops: number of operations supported by this family (private)
46482a8524SThomas Graf  */
47fd2c3ef7SEric Dumazet struct genl_family {
48482a8524SThomas Graf 	unsigned int		id;
49482a8524SThomas Graf 	unsigned int		hdrsize;
50482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
51482a8524SThomas Graf 	unsigned int		version;
52482a8524SThomas Graf 	unsigned int		maxattr;
53134e6375SJohannes Berg 	bool			netnsok;
54def31174SPravin B Shelar 	bool			parallel_ops;
55f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
56ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
57ff4c92d8SJohannes Berg 					    struct genl_info *info);
58f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
59ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
60ff4c92d8SJohannes Berg 					     struct genl_info *info);
61482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
62f84f771dSJohannes Berg 	const struct genl_ops *	ops;		/* private */
63d91824c0SJohannes Berg 	unsigned int		n_ops;		/* private */
64482a8524SThomas Graf 	struct list_head	family_list;	/* private */
652dbba6f7SJohannes Berg 	struct list_head	mcast_groups;	/* private */
6633c6b1f6SPravin B Shelar 	struct module		*module;
67482a8524SThomas Graf };
68482a8524SThomas Graf 
69482a8524SThomas Graf /**
70482a8524SThomas Graf  * struct genl_info - receiving information
71482a8524SThomas Graf  * @snd_seq: sending sequence number
7215e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
73482a8524SThomas Graf  * @nlhdr: netlink message header
74482a8524SThomas Graf  * @genlhdr: generic netlink message header
75482a8524SThomas Graf  * @userhdr: user specific header
76482a8524SThomas Graf  * @attrs: netlink attributes
77ff4c92d8SJohannes Berg  * @_net: network namespace
78ff4c92d8SJohannes Berg  * @user_ptr: user pointers
79482a8524SThomas Graf  */
80fd2c3ef7SEric Dumazet struct genl_info {
81482a8524SThomas Graf 	u32			snd_seq;
8215e47304SEric W. Biederman 	u32			snd_portid;
83482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
84482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
85482a8524SThomas Graf 	void *			userhdr;
86482a8524SThomas Graf 	struct nlattr **	attrs;
87134e6375SJohannes Berg #ifdef CONFIG_NET_NS
88134e6375SJohannes Berg 	struct net *		_net;
89134e6375SJohannes Berg #endif
90ff4c92d8SJohannes Berg 	void *			user_ptr[2];
91482a8524SThomas Graf };
92482a8524SThomas Graf 
93134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
94134e6375SJohannes Berg {
95c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
96134e6375SJohannes Berg }
97134e6375SJohannes Berg 
98134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
99134e6375SJohannes Berg {
100c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
101134e6375SJohannes Berg }
102134e6375SJohannes Berg 
103482a8524SThomas Graf /**
104482a8524SThomas Graf  * struct genl_ops - generic netlink operations
105482a8524SThomas Graf  * @cmd: command identifier
106ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
107482a8524SThomas Graf  * @flags: flags
108482a8524SThomas Graf  * @policy: attribute validation policy
109482a8524SThomas Graf  * @doit: standard command callback
110482a8524SThomas Graf  * @dumpit: callback for dumpers
111a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
112482a8524SThomas Graf  * @ops_list: operations list
113482a8524SThomas Graf  */
114fd2c3ef7SEric Dumazet struct genl_ops {
115ef7c79edSPatrick McHardy 	const struct nla_policy	*policy;
116482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
117482a8524SThomas Graf 				       struct genl_info *info);
118482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
119482a8524SThomas Graf 					 struct netlink_callback *cb);
120a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
1213f5ccd06SJohannes Berg 	u8			cmd;
1223f5ccd06SJohannes Berg 	u8			internal_flags;
1233f5ccd06SJohannes Berg 	u8			flags;
124482a8524SThomas Graf };
125482a8524SThomas Graf 
126ff2b94d2SJoe Perches int __genl_register_family(struct genl_family *family);
12733c6b1f6SPravin B Shelar 
12833c6b1f6SPravin B Shelar static inline int genl_register_family(struct genl_family *family)
12933c6b1f6SPravin B Shelar {
13033c6b1f6SPravin B Shelar 	family->module = THIS_MODULE;
13133c6b1f6SPravin B Shelar 	return __genl_register_family(family);
13233c6b1f6SPravin B Shelar }
13333c6b1f6SPravin B Shelar 
134568508aaSJohannes Berg /**
135568508aaSJohannes Berg  * genl_register_family_with_ops - register a generic netlink family with ops
136568508aaSJohannes Berg  * @family: generic netlink family
137568508aaSJohannes Berg  * @ops: operations to be registered
138568508aaSJohannes Berg  * @n_ops: number of elements to register
139568508aaSJohannes Berg  *
140568508aaSJohannes Berg  * Registers the specified family and operations from the specified table.
141568508aaSJohannes Berg  * Only one family may be registered with the same family name or identifier.
142568508aaSJohannes Berg  *
143568508aaSJohannes Berg  * The family id may equal GENL_ID_GENERATE causing an unique id to
144568508aaSJohannes Berg  * be automatically generated and assigned.
145568508aaSJohannes Berg  *
146568508aaSJohannes Berg  * Either a doit or dumpit callback must be specified for every registered
147568508aaSJohannes Berg  * operation or the function will fail. Only one operation structure per
148568508aaSJohannes Berg  * command identifier may be registered.
149568508aaSJohannes Berg  *
150568508aaSJohannes Berg  * See include/net/genetlink.h for more documenation on the operations
151568508aaSJohannes Berg  * structure.
152568508aaSJohannes Berg  *
153568508aaSJohannes Berg  * Return 0 on success or a negative error code.
154568508aaSJohannes Berg  */
155*c53ed742SJohannes Berg static inline int _genl_register_family_with_ops(struct genl_family *family,
156*c53ed742SJohannes Berg 						 const struct genl_ops *ops,
157*c53ed742SJohannes Berg 						 size_t n_ops)
15833c6b1f6SPravin B Shelar {
15933c6b1f6SPravin B Shelar 	family->module = THIS_MODULE;
160568508aaSJohannes Berg 	family->ops = ops;
161568508aaSJohannes Berg 	family->n_ops = n_ops;
162568508aaSJohannes Berg 	return __genl_register_family(family);
16333c6b1f6SPravin B Shelar }
16433c6b1f6SPravin B Shelar 
165*c53ed742SJohannes Berg #define genl_register_family_with_ops(family, ops)	\
166*c53ed742SJohannes Berg 	_genl_register_family_with_ops((family), (ops), ARRAY_SIZE(ops))
167*c53ed742SJohannes Berg 
168ff2b94d2SJoe Perches int genl_unregister_family(struct genl_family *family);
169ff2b94d2SJoe Perches int genl_register_mc_group(struct genl_family *family,
1702dbba6f7SJohannes Berg 			   struct genl_multicast_group *grp);
171ff2b94d2SJoe Perches void genl_unregister_mc_group(struct genl_family *family,
1722dbba6f7SJohannes Berg 			      struct genl_multicast_group *grp);
173ff2b94d2SJoe Perches void genl_notify(struct sk_buff *skb, struct net *net, u32 portid,
174263ba61dSPravin B Shelar 		 u32 group, struct nlmsghdr *nlh, gfp_t flags);
175482a8524SThomas Graf 
17615e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
177a46621a3SDenys Vlasenko 		  struct genl_family *family, int flags, u8 cmd);
178482a8524SThomas Graf 
179482a8524SThomas Graf /**
180670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
181670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
182670dc283SJohannes Berg  * @family: generic netlink family
183670dc283SJohannes Berg  *
184670dc283SJohannes Berg  * Returns pointer to netlink header.
185670dc283SJohannes Berg  */
186670dc283SJohannes Berg static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
187670dc283SJohannes Berg 					     struct genl_family *family)
188670dc283SJohannes Berg {
189670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
190670dc283SJohannes Berg 				   family->hdrsize -
191670dc283SJohannes Berg 				   GENL_HDRLEN -
192670dc283SJohannes Berg 				   NLMSG_HDRLEN);
193670dc283SJohannes Berg }
194670dc283SJohannes Berg 
195670dc283SJohannes Berg /**
196670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
197670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
198670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
199670dc283SJohannes Berg  * @family: generic netlink family
200670dc283SJohannes Berg  *
201670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
202670dc283SJohannes Berg  * simpler to use with generic netlink.
203670dc283SJohannes Berg  */
204670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
205670dc283SJohannes Berg 					      void *user_hdr,
206670dc283SJohannes Berg 					      struct genl_family *family)
207670dc283SJohannes Berg {
208670dc283SJohannes Berg 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
209670dc283SJohannes Berg }
210670dc283SJohannes Berg 
211670dc283SJohannes Berg /**
21217c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
21317c157c8SThomas Graf  * @skb: socket buffer holding the message
21417c157c8SThomas Graf  * @info: receiver info
21517c157c8SThomas Graf  * @family: generic netlink family
21617c157c8SThomas Graf  * @flags: netlink message flags
21717c157c8SThomas Graf  * @cmd: generic netlink command
21817c157c8SThomas Graf  *
21917c157c8SThomas Graf  * Returns pointer to user specific header
22017c157c8SThomas Graf  */
22117c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
22217c157c8SThomas Graf 				      struct genl_info *info,
22317c157c8SThomas Graf 				      struct genl_family *family,
22417c157c8SThomas Graf 				      int flags, u8 cmd)
22517c157c8SThomas Graf {
22615e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
22717c157c8SThomas Graf 			   flags, cmd);
22817c157c8SThomas Graf }
22917c157c8SThomas Graf 
23017c157c8SThomas Graf /**
231482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
232482a8524SThomas Graf  * @skb: socket buffer the message is stored in
233482a8524SThomas Graf  * @hdr: user specific header
234482a8524SThomas Graf  */
235482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
236482a8524SThomas Graf {
237482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
238482a8524SThomas Graf }
239482a8524SThomas Graf 
240482a8524SThomas Graf /**
241482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
242482a8524SThomas Graf  * @skb: socket buffer the message is stored in
243482a8524SThomas Graf  * @hdr: generic netlink message header
244482a8524SThomas Graf  */
245bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
246482a8524SThomas Graf {
24738db9e1dSJulia Lawall 	if (hdr)
248bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
249482a8524SThomas Graf }
250482a8524SThomas Graf 
251482a8524SThomas Graf /**
252134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
253134e6375SJohannes Berg  * @net: the net namespace
254134e6375SJohannes Berg  * @skb: netlink message as socket buffer
25515e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
256134e6375SJohannes Berg  * @group: multicast group id
257134e6375SJohannes Berg  * @flags: allocation flags
258134e6375SJohannes Berg  */
259134e6375SJohannes Berg static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
26015e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
261134e6375SJohannes Berg {
26215e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
263134e6375SJohannes Berg }
264134e6375SJohannes Berg 
265134e6375SJohannes Berg /**
266134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
267482a8524SThomas Graf  * @skb: netlink message as socket buffer
26815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
269482a8524SThomas Graf  * @group: multicast group id
270d387f6adSThomas Graf  * @flags: allocation flags
271482a8524SThomas Graf  */
27215e47304SEric W. Biederman static inline int genlmsg_multicast(struct sk_buff *skb, u32 portid,
273d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
274482a8524SThomas Graf {
27515e47304SEric W. Biederman 	return genlmsg_multicast_netns(&init_net, skb, portid, group, flags);
276482a8524SThomas Graf }
277482a8524SThomas Graf 
278482a8524SThomas Graf /**
279134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
280134e6375SJohannes Berg  * @skb: netlink message as socket buffer
28115e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
282134e6375SJohannes Berg  * @group: multicast group id
283134e6375SJohannes Berg  * @flags: allocation flags
284134e6375SJohannes Berg  *
285134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
286134e6375SJohannes Berg  */
28715e47304SEric W. Biederman int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid,
288134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
289134e6375SJohannes Berg 
290134e6375SJohannes Berg /**
291482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
292482a8524SThomas Graf  * @skb: netlink message as socket buffer
29315e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
294482a8524SThomas Graf  */
29515e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
296482a8524SThomas Graf {
29715e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
298482a8524SThomas Graf }
299482a8524SThomas Graf 
300fb0ba6bdSBalbir Singh /**
30181878d27SThomas Graf  * genlmsg_reply - reply to a request
30281878d27SThomas Graf  * @skb: netlink message to be sent back
30381878d27SThomas Graf  * @info: receiver information
30481878d27SThomas Graf  */
30581878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
30681878d27SThomas Graf {
30715e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
30881878d27SThomas Graf }
30981878d27SThomas Graf 
31081878d27SThomas Graf /**
311fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
31270f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
313fb0ba6bdSBalbir Singh  */
314fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
315fb0ba6bdSBalbir Singh {
316fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
317fb0ba6bdSBalbir Singh }
318fb0ba6bdSBalbir Singh 
319fb0ba6bdSBalbir Singh /**
320fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
321fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
322fb0ba6bdSBalbir Singh  */
323fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
324fb0ba6bdSBalbir Singh {
325fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
326fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
327fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
328fb0ba6bdSBalbir Singh }
329fb0ba6bdSBalbir Singh 
33017db952cSBalbir Singh /**
33117db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
33217db952cSBalbir Singh  * @payload: length of message payload
33317db952cSBalbir Singh  */
33417db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
33517db952cSBalbir Singh {
33617db952cSBalbir Singh 	return GENL_HDRLEN + payload;
33717db952cSBalbir Singh }
33817db952cSBalbir Singh 
33917db952cSBalbir Singh /**
34017db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
34117db952cSBalbir Singh  * @payload: length of message payload
34217db952cSBalbir Singh  */
34317db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
34417db952cSBalbir Singh {
34517db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
34617db952cSBalbir Singh }
34717db952cSBalbir Singh 
3483dabc715SThomas Graf /**
3493dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
3503dabc715SThomas Graf  * @payload: size of the message payload
3513dabc715SThomas Graf  * @flags: the type of memory to allocate.
3523dabc715SThomas Graf  */
3533dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
3543dabc715SThomas Graf {
3553dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
3563dabc715SThomas Graf }
3573dabc715SThomas Graf 
3583dabc715SThomas Graf 
359482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
360