1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_GENERIC_NETLINK_H
3 #define __NET_GENERIC_NETLINK_H
5 #include <linux/genetlink.h>
6 #include <net/netlink.h>
7 #include <net/net_namespace.h>
9 #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
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)
16 struct genl_multicast_group {
17 char name[GENL_NAMSIZ];
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
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
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.
63 char name[GENL_NAMSIZ];
66 unsigned int mcgrp_offset; /* private */
73 const struct nla_policy *policy;
74 int (*pre_doit)(const struct genl_ops *ops,
76 struct genl_info *info);
77 void (*post_doit)(const struct genl_ops *ops,
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;
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
101 struct nlmsghdr * nlhdr;
102 struct genlmsghdr * genlhdr;
104 struct nlattr ** attrs;
107 struct netlink_ext_ack *extack;
110 static inline struct net *genl_info_net(struct genl_info *info)
112 return read_pnet(&info->_net);
115 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
117 write_pnet(&info->_net, net);
120 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
122 /* Report that a root attribute is missing */
123 #define GENL_REQ_ATTR_CHECK(info, attr) ({ \
124 struct genl_info *__info = (info); \
126 NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
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),
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
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.
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);
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
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;
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
190 struct genl_dumpit_info {
191 const struct genl_family *family;
193 struct nlattr **attrs;
196 static inline const struct genl_dumpit_info *
197 genl_dumpit_info(struct netlink_callback *cb)
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);
207 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
208 const struct genl_family *family, int flags, u8 cmd);
211 * genlmsg_nlhdr - Obtain netlink header from user specified header
212 * @user_hdr: user header as returned from genlmsg_put()
214 * Returns pointer to netlink header.
216 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
218 return (struct nlmsghdr *)((char *)user_hdr -
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
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)
238 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
239 policy, NL_VALIDATE_LIBERAL, extack);
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
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)
257 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
258 policy, NL_VALIDATE_STRICT, extack);
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()
266 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
267 * simpler to use with generic netlink.
269 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
272 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
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
283 * Returns pointer to user specific header
285 static inline void *genlmsg_put_reply(struct sk_buff *skb,
286 struct genl_info *info,
287 const struct genl_family *family,
290 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
295 * genlmsg_end - Finalize a generic netlink message
296 * @skb: socket buffer the message is stored in
297 * @hdr: user specific header
299 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
301 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
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
309 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
312 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
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
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)
328 if (WARN_ON_ONCE(group >= family->n_mcgrps))
330 group = family->mcgrp_offset + group;
331 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
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
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)
346 return genlmsg_multicast_netns(family, &init_net, skb,
347 portid, group, flags);
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
358 * This function must hold the RTNL or rcu_read_lock().
360 int genlmsg_multicast_allns(const struct genl_family *family,
361 struct sk_buff *skb, u32 portid,
362 unsigned int group, gfp_t flags);
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
370 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
372 return nlmsg_unicast(net->genl_sock, skb, portid);
376 * genlmsg_reply - reply to a request
377 * @skb: netlink message to be sent back
378 * @info: receiver information
380 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
382 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
386 * genlmsg_data - head of message payload
387 * @gnlh: genetlink message header
389 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
391 return ((unsigned char *) gnlh + GENL_HDRLEN);
395 * genlmsg_len - length of message payload
396 * @gnlh: genetlink message header
398 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
400 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
402 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
406 * genlmsg_msg_size - length of genetlink message not including padding
407 * @payload: length of message payload
409 static inline int genlmsg_msg_size(int payload)
411 return GENL_HDRLEN + payload;
415 * genlmsg_total_size - length of genetlink message including padding
416 * @payload: length of message payload
418 static inline int genlmsg_total_size(int payload)
420 return NLMSG_ALIGN(genlmsg_msg_size(payload));
424 * genlmsg_new - Allocate a new generic netlink message
425 * @payload: size of the message payload
426 * @flags: the type of memory to allocate.
428 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
430 return nlmsg_new(genlmsg_total_size(payload), flags);
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)
442 * This function returns the number of broadcast listeners that have set the
443 * NETLINK_RECV_NO_ENOBUFS socket option.
445 static inline int genl_set_err(const struct genl_family *family,
446 struct net *net, u32 portid,
449 if (WARN_ON_ONCE(group >= family->n_mcgrps))
451 group = family->mcgrp_offset + group;
452 return netlink_set_err(net->genl_sock, portid, group, code);
455 static inline int genl_has_listeners(const struct genl_family *family,
456 struct net *net, unsigned int group)
458 if (WARN_ON_ONCE(group >= family->n_mcgrps))
460 group = family->mcgrp_offset + group;
461 return netlink_has_listeners(net->genl_sock, group);
463 #endif /* __NET_GENERIC_NETLINK_H */