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 * @cap_sys_admin: whether %CAP_SYS_ADMIN is required for binding
16 struct genl_multicast_group {
17 char name[GENL_NAMSIZ];
26 * struct genl_family - generic netlink family
27 * @id: protocol family identifier (private)
28 * @hdrsize: length of user specific header in bytes
29 * @name: name of family
30 * @version: protocol version
31 * @maxattr: maximum number of attributes supported
32 * @policy: netlink policy
33 * @netnsok: set to true if the family can handle network
34 * namespaces and should be presented in all of them
35 * @parallel_ops: operations can be called in parallel and aren't
36 * synchronized by the core genetlink code
37 * @pre_doit: called before an operation's doit callback, it may
38 * do additional, common, filtering and return an error
39 * @post_doit: called after an operation's doit callback, it may
40 * undo operations done by pre_doit, for example release locks
41 * @mcgrps: multicast groups used by this family
42 * @n_mcgrps: number of multicast groups
43 * @mcgrp_offset: starting number of multicast group IDs in this family
45 * @ops: the operations supported by this family
46 * @n_ops: number of operations supported by this family
47 * @small_ops: the small-struct operations supported by this family
48 * @n_small_ops: number of small-struct operations supported by this family
53 char name[GENL_NAMSIZ];
56 unsigned int mcgrp_offset; /* private */
62 const struct nla_policy *policy;
63 int (*pre_doit)(const struct genl_ops *ops,
65 struct genl_info *info);
66 void (*post_doit)(const struct genl_ops *ops,
68 struct genl_info *info);
69 const struct genl_ops * ops;
70 const struct genl_small_ops *small_ops;
71 const struct genl_multicast_group *mcgrps;
72 struct module *module;
76 * struct genl_info - receiving information
77 * @snd_seq: sending sequence number
78 * @snd_portid: netlink portid of sender
79 * @nlhdr: netlink message header
80 * @genlhdr: generic netlink message header
81 * @userhdr: user specific header
82 * @attrs: netlink attributes
83 * @_net: network namespace
84 * @user_ptr: user pointers
85 * @extack: extended ACK report struct
90 struct nlmsghdr * nlhdr;
91 struct genlmsghdr * genlhdr;
93 struct nlattr ** attrs;
96 struct netlink_ext_ack *extack;
99 static inline struct net *genl_info_net(struct genl_info *info)
101 return read_pnet(&info->_net);
104 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
106 write_pnet(&info->_net, net);
109 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
111 enum genl_validate_flags {
112 GENL_DONT_VALIDATE_STRICT = BIT(0),
113 GENL_DONT_VALIDATE_DUMP = BIT(1),
114 GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
118 * struct genl_small_ops - generic netlink operations (small version)
119 * @cmd: command identifier
120 * @internal_flags: flags used by the family
122 * @validate: validation flags from enum genl_validate_flags
123 * @doit: standard command callback
124 * @dumpit: callback for dumpers
126 * This is a cut-down version of struct genl_ops for users who don't need
127 * most of the ancillary infra and want to save space.
129 struct genl_small_ops {
130 int (*doit)(struct sk_buff *skb, struct genl_info *info);
131 int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
139 * struct genl_ops - generic netlink operations
140 * @cmd: command identifier
141 * @internal_flags: flags used by the family
143 * @maxattr: maximum number of attributes supported
144 * @policy: netlink policy (takes precedence over family policy)
145 * @validate: validation flags from enum genl_validate_flags
146 * @doit: standard command callback
147 * @start: start callback for dumps
148 * @dumpit: callback for dumpers
149 * @done: completion callback for dumps
152 int (*doit)(struct sk_buff *skb,
153 struct genl_info *info);
154 int (*start)(struct netlink_callback *cb);
155 int (*dumpit)(struct sk_buff *skb,
156 struct netlink_callback *cb);
157 int (*done)(struct netlink_callback *cb);
158 const struct nla_policy *policy;
159 unsigned int maxattr;
167 * struct genl_info - info that is available during dumpit op call
168 * @family: generic netlink family - for internal genl code usage
169 * @ops: generic netlink ops - for internal genl code usage
170 * @attrs: netlink attributes
172 struct genl_dumpit_info {
173 const struct genl_family *family;
175 struct nlattr **attrs;
178 static inline const struct genl_dumpit_info *
179 genl_dumpit_info(struct netlink_callback *cb)
184 int genl_register_family(struct genl_family *family);
185 int genl_unregister_family(const struct genl_family *family);
186 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
187 struct genl_info *info, u32 group, gfp_t flags);
189 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
190 const struct genl_family *family, int flags, u8 cmd);
193 * genlmsg_nlhdr - Obtain netlink header from user specified header
194 * @user_hdr: user header as returned from genlmsg_put()
196 * Returns pointer to netlink header.
198 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
200 return (struct nlmsghdr *)((char *)user_hdr -
206 * genlmsg_parse_deprecated - parse attributes of a genetlink message
207 * @nlh: netlink message header
208 * @family: genetlink message family
209 * @tb: destination array with maxtype+1 elements
210 * @maxtype: maximum attribute type to be expected
211 * @policy: validation policy
212 * @extack: extended ACK report struct
214 static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
215 const struct genl_family *family,
216 struct nlattr *tb[], int maxtype,
217 const struct nla_policy *policy,
218 struct netlink_ext_ack *extack)
220 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
221 policy, NL_VALIDATE_LIBERAL, extack);
225 * genlmsg_parse - parse attributes of a genetlink message
226 * @nlh: netlink message header
227 * @family: genetlink message family
228 * @tb: destination array with maxtype+1 elements
229 * @maxtype: maximum attribute type to be expected
230 * @policy: validation policy
231 * @extack: extended ACK report struct
233 static inline int genlmsg_parse(const struct nlmsghdr *nlh,
234 const struct genl_family *family,
235 struct nlattr *tb[], int maxtype,
236 const struct nla_policy *policy,
237 struct netlink_ext_ack *extack)
239 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
240 policy, NL_VALIDATE_STRICT, extack);
244 * genl_dump_check_consistent - check if sequence is consistent and advertise if not
245 * @cb: netlink callback structure that stores the sequence number
246 * @user_hdr: user header as returned from genlmsg_put()
248 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
249 * simpler to use with generic netlink.
251 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
254 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
258 * genlmsg_put_reply - Add generic netlink header to a reply message
259 * @skb: socket buffer holding the message
260 * @info: receiver info
261 * @family: generic netlink family
262 * @flags: netlink message flags
263 * @cmd: generic netlink command
265 * Returns pointer to user specific header
267 static inline void *genlmsg_put_reply(struct sk_buff *skb,
268 struct genl_info *info,
269 const struct genl_family *family,
272 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
277 * genlmsg_end - Finalize a generic netlink message
278 * @skb: socket buffer the message is stored in
279 * @hdr: user specific header
281 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
283 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
287 * genlmsg_cancel - Cancel construction of a generic netlink message
288 * @skb: socket buffer the message is stored in
289 * @hdr: generic netlink message header
291 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
294 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
298 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
299 * @family: the generic netlink family
300 * @net: the net namespace
301 * @skb: netlink message as socket buffer
302 * @portid: own netlink portid to avoid sending to yourself
303 * @group: offset of multicast group in groups array
304 * @flags: allocation flags
306 static inline int genlmsg_multicast_netns(const struct genl_family *family,
307 struct net *net, struct sk_buff *skb,
308 u32 portid, unsigned int group, gfp_t flags)
310 if (WARN_ON_ONCE(group >= family->n_mcgrps))
312 group = family->mcgrp_offset + group;
313 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
317 * genlmsg_multicast - multicast a netlink message to the default netns
318 * @family: the generic netlink family
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(const struct genl_family *family,
325 struct sk_buff *skb, u32 portid,
326 unsigned int group, gfp_t flags)
328 return genlmsg_multicast_netns(family, &init_net, skb,
329 portid, group, flags);
333 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
334 * @family: the generic netlink family
335 * @skb: netlink message as socket buffer
336 * @portid: own netlink portid to avoid sending to yourself
337 * @group: offset of multicast group in groups array
338 * @flags: allocation flags
340 * This function must hold the RTNL or rcu_read_lock().
342 int genlmsg_multicast_allns(const struct genl_family *family,
343 struct sk_buff *skb, u32 portid,
344 unsigned int group, gfp_t flags);
347 * genlmsg_unicast - unicast a netlink message
348 * @skb: netlink message as socket buffer
349 * @portid: netlink portid of the destination socket
351 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
353 return nlmsg_unicast(net->genl_sock, skb, portid);
357 * genlmsg_reply - reply to a request
358 * @skb: netlink message to be sent back
359 * @info: receiver information
361 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
363 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
367 * gennlmsg_data - head of message payload
368 * @gnlh: genetlink message header
370 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
372 return ((unsigned char *) gnlh + GENL_HDRLEN);
376 * genlmsg_len - length of message payload
377 * @gnlh: genetlink message header
379 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
381 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
383 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
387 * genlmsg_msg_size - length of genetlink message not including padding
388 * @payload: length of message payload
390 static inline int genlmsg_msg_size(int payload)
392 return GENL_HDRLEN + payload;
396 * genlmsg_total_size - length of genetlink message including padding
397 * @payload: length of message payload
399 static inline int genlmsg_total_size(int payload)
401 return NLMSG_ALIGN(genlmsg_msg_size(payload));
405 * genlmsg_new - Allocate a new generic netlink message
406 * @payload: size of the message payload
407 * @flags: the type of memory to allocate.
409 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
411 return nlmsg_new(genlmsg_total_size(payload), flags);
415 * genl_set_err - report error to genetlink broadcast listeners
416 * @family: the generic netlink family
417 * @net: the network namespace to report the error to
418 * @portid: the PORTID of a process that we want to skip (if any)
419 * @group: the broadcast group that will notice the error
420 * (this is the offset of the multicast group in the groups array)
421 * @code: error code, must be negative (as usual in kernelspace)
423 * This function returns the number of broadcast listeners that have set the
424 * NETLINK_RECV_NO_ENOBUFS socket option.
426 static inline int genl_set_err(const struct genl_family *family,
427 struct net *net, u32 portid,
430 if (WARN_ON_ONCE(group >= family->n_mcgrps))
432 group = family->mcgrp_offset + group;
433 return netlink_set_err(net->genl_sock, portid, group, code);
436 static inline int genl_has_listeners(const struct genl_family *family,
437 struct net *net, unsigned int group)
439 if (WARN_ON_ONCE(group >= family->n_mcgrps))
441 group = family->mcgrp_offset + group;
442 return netlink_has_listeners(net->genl_sock, group);
444 #endif /* __NET_GENERIC_NETLINK_H */