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