xref: /openbmc/linux/include/net/genetlink.h (revision 12cecbf9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_GENERIC_NETLINK_H
3 #define __NET_GENERIC_NETLINK_H
4 
5 #include <linux/genetlink.h>
6 #include <net/netlink.h>
7 #include <net/net_namespace.h>
8 
9 #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
10 
11 /**
12  * struct genl_multicast_group - generic netlink multicast group
13  * @name: name of the multicast group, names are per-family
14  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
15  */
16 struct genl_multicast_group {
17 	char			name[GENL_NAMSIZ];
18 	u8			flags;
19 };
20 
21 struct genl_ops;
22 struct genl_info;
23 
24 /**
25  * struct genl_family - generic netlink family
26  * @id: protocol family identifier (private)
27  * @hdrsize: length of user specific header in bytes
28  * @name: name of family
29  * @version: protocol version
30  * @maxattr: maximum number of attributes supported
31  * @policy: netlink policy
32  * @netnsok: set to true if the family can handle network
33  *	namespaces and should be presented in all of them
34  * @parallel_ops: operations can be called in parallel and aren't
35  *	synchronized by the core genetlink code
36  * @pre_doit: called before an operation's doit callback, it may
37  *	do additional, common, filtering and return an error
38  * @post_doit: called after an operation's doit callback, it may
39  *	undo operations done by pre_doit, for example release locks
40  * @mcgrps: multicast groups used by this family
41  * @n_mcgrps: number of multicast groups
42  * @mcgrp_offset: starting number of multicast group IDs in this family
43  *	(private)
44  * @ops: the operations supported by this family
45  * @n_ops: number of operations supported by this family
46  * @small_ops: the small-struct operations supported by this family
47  * @n_small_ops: number of small-struct operations supported by this family
48  */
49 struct genl_family {
50 	int			id;		/* private */
51 	unsigned int		hdrsize;
52 	char			name[GENL_NAMSIZ];
53 	unsigned int		version;
54 	unsigned int		maxattr;
55 	unsigned int		mcgrp_offset;	/* private */
56 	u8			netnsok:1;
57 	u8			parallel_ops:1;
58 	u8			n_ops;
59 	u8			n_small_ops;
60 	u8			n_mcgrps;
61 	const struct nla_policy *policy;
62 	int			(*pre_doit)(const struct genl_ops *ops,
63 					    struct sk_buff *skb,
64 					    struct genl_info *info);
65 	void			(*post_doit)(const struct genl_ops *ops,
66 					     struct sk_buff *skb,
67 					     struct genl_info *info);
68 	const struct genl_ops *	ops;
69 	const struct genl_small_ops *small_ops;
70 	const struct genl_multicast_group *mcgrps;
71 	struct module		*module;
72 };
73 
74 /**
75  * struct genl_info - receiving information
76  * @snd_seq: sending sequence number
77  * @snd_portid: netlink portid of sender
78  * @nlhdr: netlink message header
79  * @genlhdr: generic netlink message header
80  * @userhdr: user specific header
81  * @attrs: netlink attributes
82  * @_net: network namespace
83  * @user_ptr: user pointers
84  * @extack: extended ACK report struct
85  */
86 struct genl_info {
87 	u32			snd_seq;
88 	u32			snd_portid;
89 	struct nlmsghdr *	nlhdr;
90 	struct genlmsghdr *	genlhdr;
91 	void *			userhdr;
92 	struct nlattr **	attrs;
93 	possible_net_t		_net;
94 	void *			user_ptr[2];
95 	struct netlink_ext_ack *extack;
96 };
97 
98 static inline struct net *genl_info_net(struct genl_info *info)
99 {
100 	return read_pnet(&info->_net);
101 }
102 
103 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
104 {
105 	write_pnet(&info->_net, net);
106 }
107 
108 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
109 
110 enum genl_validate_flags {
111 	GENL_DONT_VALIDATE_STRICT		= BIT(0),
112 	GENL_DONT_VALIDATE_DUMP			= BIT(1),
113 	GENL_DONT_VALIDATE_DUMP_STRICT		= BIT(2),
114 };
115 
116 /**
117  * struct genl_small_ops - generic netlink operations (small version)
118  * @cmd: command identifier
119  * @internal_flags: flags used by the family
120  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
121  * @validate: validation flags from enum genl_validate_flags
122  * @doit: standard command callback
123  * @dumpit: callback for dumpers
124  *
125  * This is a cut-down version of struct genl_ops for users who don't need
126  * most of the ancillary infra and want to save space.
127  */
128 struct genl_small_ops {
129 	int	(*doit)(struct sk_buff *skb, struct genl_info *info);
130 	int	(*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
131 	u8	cmd;
132 	u8	internal_flags;
133 	u8	flags;
134 	u8	validate;
135 };
136 
137 /**
138  * struct genl_ops - generic netlink operations
139  * @cmd: command identifier
140  * @internal_flags: flags used by the family
141  * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
142  * @maxattr: maximum number of attributes supported
143  * @policy: netlink policy (takes precedence over family policy)
144  * @validate: validation flags from enum genl_validate_flags
145  * @doit: standard command callback
146  * @start: start callback for dumps
147  * @dumpit: callback for dumpers
148  * @done: completion callback for dumps
149  */
150 struct genl_ops {
151 	int		       (*doit)(struct sk_buff *skb,
152 				       struct genl_info *info);
153 	int		       (*start)(struct netlink_callback *cb);
154 	int		       (*dumpit)(struct sk_buff *skb,
155 					 struct netlink_callback *cb);
156 	int		       (*done)(struct netlink_callback *cb);
157 	const struct nla_policy *policy;
158 	unsigned int		maxattr;
159 	u8			cmd;
160 	u8			internal_flags;
161 	u8			flags;
162 	u8			validate;
163 };
164 
165 /**
166  * struct genl_info - info that is available during dumpit op call
167  * @family: generic netlink family - for internal genl code usage
168  * @ops: generic netlink ops - for internal genl code usage
169  * @attrs: netlink attributes
170  */
171 struct genl_dumpit_info {
172 	const struct genl_family *family;
173 	struct genl_ops op;
174 	struct nlattr **attrs;
175 };
176 
177 static inline const struct genl_dumpit_info *
178 genl_dumpit_info(struct netlink_callback *cb)
179 {
180 	return cb->data;
181 }
182 
183 int genl_register_family(struct genl_family *family);
184 int genl_unregister_family(const struct genl_family *family);
185 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
186 		 struct genl_info *info, u32 group, gfp_t flags);
187 
188 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
189 		  const struct genl_family *family, int flags, u8 cmd);
190 
191 /**
192  * genlmsg_nlhdr - Obtain netlink header from user specified header
193  * @user_hdr: user header as returned from genlmsg_put()
194  *
195  * Returns pointer to netlink header.
196  */
197 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
198 {
199 	return (struct nlmsghdr *)((char *)user_hdr -
200 				   GENL_HDRLEN -
201 				   NLMSG_HDRLEN);
202 }
203 
204 /**
205  * genlmsg_parse_deprecated - parse attributes of a genetlink message
206  * @nlh: netlink message header
207  * @family: genetlink message family
208  * @tb: destination array with maxtype+1 elements
209  * @maxtype: maximum attribute type to be expected
210  * @policy: validation policy
211  * @extack: extended ACK report struct
212  */
213 static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
214 					   const struct genl_family *family,
215 					   struct nlattr *tb[], int maxtype,
216 					   const struct nla_policy *policy,
217 					   struct netlink_ext_ack *extack)
218 {
219 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
220 			     policy, NL_VALIDATE_LIBERAL, extack);
221 }
222 
223 /**
224  * genlmsg_parse - parse attributes of a genetlink message
225  * @nlh: netlink message header
226  * @family: genetlink message family
227  * @tb: destination array with maxtype+1 elements
228  * @maxtype: maximum attribute type to be expected
229  * @policy: validation policy
230  * @extack: extended ACK report struct
231  */
232 static inline int genlmsg_parse(const struct nlmsghdr *nlh,
233 				const struct genl_family *family,
234 				struct nlattr *tb[], int maxtype,
235 				const struct nla_policy *policy,
236 				struct netlink_ext_ack *extack)
237 {
238 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
239 			     policy, NL_VALIDATE_STRICT, extack);
240 }
241 
242 /**
243  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
244  * @cb: netlink callback structure that stores the sequence number
245  * @user_hdr: user header as returned from genlmsg_put()
246  *
247  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
248  * simpler to use with generic netlink.
249  */
250 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
251 					      void *user_hdr)
252 {
253 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
254 }
255 
256 /**
257  * genlmsg_put_reply - Add generic netlink header to a reply message
258  * @skb: socket buffer holding the message
259  * @info: receiver info
260  * @family: generic netlink family
261  * @flags: netlink message flags
262  * @cmd: generic netlink command
263  *
264  * Returns pointer to user specific header
265  */
266 static inline void *genlmsg_put_reply(struct sk_buff *skb,
267 				      struct genl_info *info,
268 				      const struct genl_family *family,
269 				      int flags, u8 cmd)
270 {
271 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
272 			   flags, cmd);
273 }
274 
275 /**
276  * genlmsg_end - Finalize a generic netlink message
277  * @skb: socket buffer the message is stored in
278  * @hdr: user specific header
279  */
280 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
281 {
282 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
283 }
284 
285 /**
286  * genlmsg_cancel - Cancel construction of a generic netlink message
287  * @skb: socket buffer the message is stored in
288  * @hdr: generic netlink message header
289  */
290 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
291 {
292 	if (hdr)
293 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
294 }
295 
296 /**
297  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
298  * @family: the generic netlink family
299  * @net: the net namespace
300  * @skb: netlink message as socket buffer
301  * @portid: own netlink portid to avoid sending to yourself
302  * @group: offset of multicast group in groups array
303  * @flags: allocation flags
304  */
305 static inline int genlmsg_multicast_netns(const struct genl_family *family,
306 					  struct net *net, struct sk_buff *skb,
307 					  u32 portid, unsigned int group, gfp_t flags)
308 {
309 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
310 		return -EINVAL;
311 	group = family->mcgrp_offset + group;
312 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
313 }
314 
315 /**
316  * genlmsg_multicast - multicast a netlink message to the default netns
317  * @family: the generic netlink family
318  * @skb: netlink message as socket buffer
319  * @portid: own netlink portid to avoid sending to yourself
320  * @group: offset of multicast group in groups array
321  * @flags: allocation flags
322  */
323 static inline int genlmsg_multicast(const struct genl_family *family,
324 				    struct sk_buff *skb, u32 portid,
325 				    unsigned int group, gfp_t flags)
326 {
327 	return genlmsg_multicast_netns(family, &init_net, skb,
328 				       portid, group, flags);
329 }
330 
331 /**
332  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
333  * @family: the generic netlink family
334  * @skb: netlink message as socket buffer
335  * @portid: own netlink portid to avoid sending to yourself
336  * @group: offset of multicast group in groups array
337  * @flags: allocation flags
338  *
339  * This function must hold the RTNL or rcu_read_lock().
340  */
341 int genlmsg_multicast_allns(const struct genl_family *family,
342 			    struct sk_buff *skb, u32 portid,
343 			    unsigned int group, gfp_t flags);
344 
345 /**
346  * genlmsg_unicast - unicast a netlink message
347  * @skb: netlink message as socket buffer
348  * @portid: netlink portid of the destination socket
349  */
350 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
351 {
352 	return nlmsg_unicast(net->genl_sock, skb, portid);
353 }
354 
355 /**
356  * genlmsg_reply - reply to a request
357  * @skb: netlink message to be sent back
358  * @info: receiver information
359  */
360 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
361 {
362 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
363 }
364 
365 /**
366  * gennlmsg_data - head of message payload
367  * @gnlh: genetlink message header
368  */
369 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
370 {
371 	return ((unsigned char *) gnlh + GENL_HDRLEN);
372 }
373 
374 /**
375  * genlmsg_len - length of message payload
376  * @gnlh: genetlink message header
377  */
378 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
379 {
380 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
381 							NLMSG_HDRLEN);
382 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
383 }
384 
385 /**
386  * genlmsg_msg_size - length of genetlink message not including padding
387  * @payload: length of message payload
388  */
389 static inline int genlmsg_msg_size(int payload)
390 {
391 	return GENL_HDRLEN + payload;
392 }
393 
394 /**
395  * genlmsg_total_size - length of genetlink message including padding
396  * @payload: length of message payload
397  */
398 static inline int genlmsg_total_size(int payload)
399 {
400 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
401 }
402 
403 /**
404  * genlmsg_new - Allocate a new generic netlink message
405  * @payload: size of the message payload
406  * @flags: the type of memory to allocate.
407  */
408 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
409 {
410 	return nlmsg_new(genlmsg_total_size(payload), flags);
411 }
412 
413 /**
414  * genl_set_err - report error to genetlink broadcast listeners
415  * @family: the generic netlink family
416  * @net: the network namespace to report the error to
417  * @portid: the PORTID of a process that we want to skip (if any)
418  * @group: the broadcast group that will notice the error
419  * 	(this is the offset of the multicast group in the groups array)
420  * @code: error code, must be negative (as usual in kernelspace)
421  *
422  * This function returns the number of broadcast listeners that have set the
423  * NETLINK_RECV_NO_ENOBUFS socket option.
424  */
425 static inline int genl_set_err(const struct genl_family *family,
426 			       struct net *net, u32 portid,
427 			       u32 group, int code)
428 {
429 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
430 		return -EINVAL;
431 	group = family->mcgrp_offset + group;
432 	return netlink_set_err(net->genl_sock, portid, group, code);
433 }
434 
435 static inline int genl_has_listeners(const struct genl_family *family,
436 				     struct net *net, unsigned int group)
437 {
438 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
439 		return -EINVAL;
440 	group = family->mcgrp_offset + group;
441 	return netlink_has_listeners(net->genl_sock, group);
442 }
443 #endif	/* __NET_GENERIC_NETLINK_H */
444