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 * @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
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
52 char name[GENL_NAMSIZ];
55 unsigned int mcgrp_offset; /* private */
61 const struct nla_policy *policy;
62 int (*pre_doit)(const struct genl_ops *ops,
64 struct genl_info *info);
65 void (*post_doit)(const struct genl_ops *ops,
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;
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
89 struct nlmsghdr * nlhdr;
90 struct genlmsghdr * genlhdr;
92 struct nlattr ** attrs;
95 struct netlink_ext_ack *extack;
98 static inline struct net *genl_info_net(struct genl_info *info)
100 return read_pnet(&info->_net);
103 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
105 write_pnet(&info->_net, net);
108 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
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),
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
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.
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);
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
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;
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
171 struct genl_dumpit_info {
172 const struct genl_family *family;
174 struct nlattr **attrs;
177 static inline const struct genl_dumpit_info *
178 genl_dumpit_info(struct netlink_callback *cb)
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);
188 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
189 const struct genl_family *family, int flags, u8 cmd);
192 * genlmsg_nlhdr - Obtain netlink header from user specified header
193 * @user_hdr: user header as returned from genlmsg_put()
195 * Returns pointer to netlink header.
197 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
199 return (struct nlmsghdr *)((char *)user_hdr -
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
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)
219 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
220 policy, NL_VALIDATE_LIBERAL, extack);
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
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)
238 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
239 policy, NL_VALIDATE_STRICT, extack);
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()
247 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
248 * simpler to use with generic netlink.
250 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
253 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
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
264 * Returns pointer to user specific header
266 static inline void *genlmsg_put_reply(struct sk_buff *skb,
267 struct genl_info *info,
268 const struct genl_family *family,
271 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
276 * genlmsg_end - Finalize a generic netlink message
277 * @skb: socket buffer the message is stored in
278 * @hdr: user specific header
280 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
282 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
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
290 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
293 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
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
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)
309 if (WARN_ON_ONCE(group >= family->n_mcgrps))
311 group = family->mcgrp_offset + group;
312 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
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
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)
327 return genlmsg_multicast_netns(family, &init_net, skb,
328 portid, group, flags);
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
339 * This function must hold the RTNL or rcu_read_lock().
341 int genlmsg_multicast_allns(const struct genl_family *family,
342 struct sk_buff *skb, u32 portid,
343 unsigned int group, gfp_t flags);
346 * genlmsg_unicast - unicast a netlink message
347 * @skb: netlink message as socket buffer
348 * @portid: netlink portid of the destination socket
350 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
352 return nlmsg_unicast(net->genl_sock, skb, portid);
356 * genlmsg_reply - reply to a request
357 * @skb: netlink message to be sent back
358 * @info: receiver information
360 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
362 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
366 * gennlmsg_data - head of message payload
367 * @gnlh: genetlink message header
369 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
371 return ((unsigned char *) gnlh + GENL_HDRLEN);
375 * genlmsg_len - length of message payload
376 * @gnlh: genetlink message header
378 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
380 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
382 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
386 * genlmsg_msg_size - length of genetlink message not including padding
387 * @payload: length of message payload
389 static inline int genlmsg_msg_size(int payload)
391 return GENL_HDRLEN + payload;
395 * genlmsg_total_size - length of genetlink message including padding
396 * @payload: length of message payload
398 static inline int genlmsg_total_size(int payload)
400 return NLMSG_ALIGN(genlmsg_msg_size(payload));
404 * genlmsg_new - Allocate a new generic netlink message
405 * @payload: size of the message payload
406 * @flags: the type of memory to allocate.
408 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
410 return nlmsg_new(genlmsg_total_size(payload), flags);
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)
422 * This function returns the number of broadcast listeners that have set the
423 * NETLINK_RECV_NO_ENOBUFS socket option.
425 static inline int genl_set_err(const struct genl_family *family,
426 struct net *net, u32 portid,
429 if (WARN_ON_ONCE(group >= family->n_mcgrps))
431 group = family->mcgrp_offset + group;
432 return netlink_set_err(net->genl_sock, portid, group, code);
435 static inline int genl_has_listeners(const struct genl_family *family,
436 struct net *net, unsigned int group)
438 if (WARN_ON_ONCE(group >= family->n_mcgrps))
440 group = family->mcgrp_offset + group;
441 return netlink_has_listeners(net->genl_sock, group);
443 #endif /* __NET_GENERIC_NETLINK_H */