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