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)
15 * @cap_sys_admin: whether %CAP_SYS_ADMIN is required for binding
17 struct genl_multicast_group {
18 char name[GENL_NAMSIZ];
23 struct genl_split_ops;
27 * struct genl_family - generic netlink family
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 * @module: pointer to the owning module (set to THIS_MODULE)
42 * @mcgrps: multicast groups used by this family
43 * @n_mcgrps: number of multicast groups
44 * @resv_start_op: first operation for which reserved fields of the header
45 * can be validated and policies are required (see below);
46 * new families should leave this field at zero
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 * @split_ops: the split do/dump form of operation definition
52 * @n_split_ops: number of entries in @split_ops, not that with split do/dump
53 * ops the number of entries is not the same as number of commands
55 * Attribute policies (the combination of @policy and @maxattr fields)
56 * can be attached at the family level or at the operation level.
57 * If both are present the per-operation policy takes precedence.
58 * For operations before @resv_start_op lack of policy means that the core
59 * will perform no attribute parsing or validation. For newer operations
60 * if policy is not provided core will reject all TLV attributes.
64 char name[GENL_NAMSIZ];
74 const struct nla_policy *policy;
75 int (*pre_doit)(const struct genl_split_ops *ops,
77 struct genl_info *info);
78 void (*post_doit)(const struct genl_split_ops *ops,
80 struct genl_info *info);
81 const struct genl_ops * ops;
82 const struct genl_small_ops *small_ops;
83 const struct genl_split_ops *split_ops;
84 const struct genl_multicast_group *mcgrps;
85 struct module *module;
87 /* private: internal use only */
88 /* protocol family identifier */
90 /* starting number of multicast group IDs in this family */
91 unsigned int mcgrp_offset;
95 * struct genl_info - receiving information
96 * @snd_seq: sending sequence number
97 * @snd_portid: netlink portid of sender
98 * @family: generic netlink family
99 * @nlhdr: netlink message header
100 * @genlhdr: generic netlink message header
101 * @attrs: netlink attributes
102 * @_net: network namespace
103 * @user_ptr: user pointers
104 * @extack: extended ACK report struct
109 const struct genl_family *family;
110 const struct nlmsghdr * nlhdr;
111 struct genlmsghdr * genlhdr;
112 struct nlattr ** attrs;
115 struct netlink_ext_ack *extack;
118 static inline struct net *genl_info_net(const struct genl_info *info)
120 return read_pnet(&info->_net);
123 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
125 write_pnet(&info->_net, net);
128 static inline void *genl_info_userhdr(const struct genl_info *info)
130 return (u8 *)info->genlhdr + GENL_HDRLEN;
133 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
135 #define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
136 NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
138 /* Report that a root attribute is missing */
139 #define GENL_REQ_ATTR_CHECK(info, attr) ({ \
140 struct genl_info *__info = (info); \
142 NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
145 enum genl_validate_flags {
146 GENL_DONT_VALIDATE_STRICT = BIT(0),
147 GENL_DONT_VALIDATE_DUMP = BIT(1),
148 GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
152 * struct genl_small_ops - generic netlink operations (small version)
153 * @cmd: command identifier
154 * @internal_flags: flags used by the family
155 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
156 * @validate: validation flags from enum genl_validate_flags
157 * @doit: standard command callback
158 * @dumpit: callback for dumpers
160 * This is a cut-down version of struct genl_ops for users who don't need
161 * most of the ancillary infra and want to save space.
163 struct genl_small_ops {
164 int (*doit)(struct sk_buff *skb, struct genl_info *info);
165 int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
173 * struct genl_ops - generic netlink operations
174 * @cmd: command identifier
175 * @internal_flags: flags used by the family
176 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
177 * @maxattr: maximum number of attributes supported
178 * @policy: netlink policy (takes precedence over family policy)
179 * @validate: validation flags from enum genl_validate_flags
180 * @doit: standard command callback
181 * @start: start callback for dumps
182 * @dumpit: callback for dumpers
183 * @done: completion callback for dumps
186 int (*doit)(struct sk_buff *skb,
187 struct genl_info *info);
188 int (*start)(struct netlink_callback *cb);
189 int (*dumpit)(struct sk_buff *skb,
190 struct netlink_callback *cb);
191 int (*done)(struct netlink_callback *cb);
192 const struct nla_policy *policy;
193 unsigned int maxattr;
201 * struct genl_split_ops - generic netlink operations (do/dump split version)
202 * @cmd: command identifier
203 * @internal_flags: flags used by the family
204 * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
205 * @validate: validation flags from enum genl_validate_flags
206 * @policy: netlink policy (takes precedence over family policy)
207 * @maxattr: maximum number of attributes supported
210 * @pre_doit: called before an operation's @doit callback, it may
211 * do additional, common, filtering and return an error
212 * @doit: standard command callback
213 * @post_doit: called after an operation's @doit callback, it may
214 * undo operations done by pre_doit, for example release locks
217 * @start: start callback for dumps
218 * @dumpit: callback for dumpers
219 * @done: completion callback for dumps
221 * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
222 * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
223 * Exactly one of those flags must be set.
225 struct genl_split_ops {
228 int (*pre_doit)(const struct genl_split_ops *ops,
230 struct genl_info *info);
231 int (*doit)(struct sk_buff *skb,
232 struct genl_info *info);
233 void (*post_doit)(const struct genl_split_ops *ops,
235 struct genl_info *info);
238 int (*start)(struct netlink_callback *cb);
239 int (*dumpit)(struct sk_buff *skb,
240 struct netlink_callback *cb);
241 int (*done)(struct netlink_callback *cb);
244 const struct nla_policy *policy;
245 unsigned int maxattr;
253 * struct genl_dumpit_info - info that is available during dumpit op call
254 * @op: generic netlink ops - for internal genl code usage
255 * @attrs: netlink attributes
256 * @info: struct genl_info describing the request
258 struct genl_dumpit_info {
259 struct genl_split_ops op;
260 struct genl_info info;
263 static inline const struct genl_dumpit_info *
264 genl_dumpit_info(struct netlink_callback *cb)
269 static inline const struct genl_info *
270 genl_info_dump(struct netlink_callback *cb)
272 return &genl_dumpit_info(cb)->info;
276 * genl_info_init_ntf() - initialize genl_info for notifications
277 * @info: genl_info struct to set up
278 * @family: pointer to the genetlink family
279 * @cmd: command to be used in the notification
281 * Initialize a locally declared struct genl_info to pass to various APIs.
282 * Intended to be used when creating notifications.
285 genl_info_init_ntf(struct genl_info *info, const struct genl_family *family,
288 struct genlmsghdr *hdr = (void *) &info->user_ptr[0];
290 memset(info, 0, sizeof(*info));
291 info->family = family;
296 static inline bool genl_info_is_ntf(const struct genl_info *info)
301 int genl_register_family(struct genl_family *family);
302 int genl_unregister_family(const struct genl_family *family);
303 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
304 struct genl_info *info, u32 group, gfp_t flags);
306 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
307 const struct genl_family *family, int flags, u8 cmd);
310 __genlmsg_iput(struct sk_buff *skb, const struct genl_info *info, int flags)
312 return genlmsg_put(skb, info->snd_portid, info->snd_seq, info->family,
313 flags, info->genlhdr->cmd);
317 * genlmsg_iput - start genetlink message based on genl_info
318 * @skb: skb in which message header will be placed
319 * @info: genl_info as provided to do/dump handlers
321 * Convenience wrapper which starts a genetlink message based on
322 * information in user request. @info should be either the struct passed
323 * by genetlink core to do/dump handlers (when constructing replies to
324 * such requests) or a struct initialized by genl_info_init_ntf()
325 * when constructing notifications.
327 * Returns pointer to new genetlink header.
330 genlmsg_iput(struct sk_buff *skb, const struct genl_info *info)
332 return __genlmsg_iput(skb, info, 0);
336 * genlmsg_nlhdr - Obtain netlink header from user specified header
337 * @user_hdr: user header as returned from genlmsg_put()
339 * Returns pointer to netlink header.
341 static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
343 return (struct nlmsghdr *)((char *)user_hdr -
349 * genlmsg_parse_deprecated - parse attributes of a genetlink message
350 * @nlh: netlink message header
351 * @family: genetlink message family
352 * @tb: destination array with maxtype+1 elements
353 * @maxtype: maximum attribute type to be expected
354 * @policy: validation policy
355 * @extack: extended ACK report struct
357 static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
358 const struct genl_family *family,
359 struct nlattr *tb[], int maxtype,
360 const struct nla_policy *policy,
361 struct netlink_ext_ack *extack)
363 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
364 policy, NL_VALIDATE_LIBERAL, extack);
368 * genlmsg_parse - parse attributes of a genetlink message
369 * @nlh: netlink message header
370 * @family: genetlink message family
371 * @tb: destination array with maxtype+1 elements
372 * @maxtype: maximum attribute type to be expected
373 * @policy: validation policy
374 * @extack: extended ACK report struct
376 static inline int genlmsg_parse(const struct nlmsghdr *nlh,
377 const struct genl_family *family,
378 struct nlattr *tb[], int maxtype,
379 const struct nla_policy *policy,
380 struct netlink_ext_ack *extack)
382 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
383 policy, NL_VALIDATE_STRICT, extack);
387 * genl_dump_check_consistent - check if sequence is consistent and advertise if not
388 * @cb: netlink callback structure that stores the sequence number
389 * @user_hdr: user header as returned from genlmsg_put()
391 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
392 * simpler to use with generic netlink.
394 static inline void genl_dump_check_consistent(struct netlink_callback *cb,
397 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
401 * genlmsg_put_reply - Add generic netlink header to a reply message
402 * @skb: socket buffer holding the message
403 * @info: receiver info
404 * @family: generic netlink family
405 * @flags: netlink message flags
406 * @cmd: generic netlink command
408 * Returns pointer to user specific header
410 static inline void *genlmsg_put_reply(struct sk_buff *skb,
411 struct genl_info *info,
412 const struct genl_family *family,
415 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
420 * genlmsg_end - Finalize a generic netlink message
421 * @skb: socket buffer the message is stored in
422 * @hdr: user specific header
424 static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
426 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
430 * genlmsg_cancel - Cancel construction of a generic netlink message
431 * @skb: socket buffer the message is stored in
432 * @hdr: generic netlink message header
434 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
437 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
441 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
442 * @family: the generic netlink family
443 * @net: the net namespace
444 * @skb: netlink message as socket buffer
445 * @portid: own netlink portid to avoid sending to yourself
446 * @group: offset of multicast group in groups array
447 * @flags: allocation flags
449 static inline int genlmsg_multicast_netns(const struct genl_family *family,
450 struct net *net, struct sk_buff *skb,
451 u32 portid, unsigned int group, gfp_t flags)
453 if (WARN_ON_ONCE(group >= family->n_mcgrps))
455 group = family->mcgrp_offset + group;
456 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
460 * genlmsg_multicast - multicast a netlink message to the default netns
461 * @family: the generic netlink family
462 * @skb: netlink message as socket buffer
463 * @portid: own netlink portid to avoid sending to yourself
464 * @group: offset of multicast group in groups array
465 * @flags: allocation flags
467 static inline int genlmsg_multicast(const struct genl_family *family,
468 struct sk_buff *skb, u32 portid,
469 unsigned int group, gfp_t flags)
471 return genlmsg_multicast_netns(family, &init_net, skb,
472 portid, group, flags);
476 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
477 * @family: the generic netlink family
478 * @skb: netlink message as socket buffer
479 * @portid: own netlink portid to avoid sending to yourself
480 * @group: offset of multicast group in groups array
481 * @flags: allocation flags
483 * This function must hold the RTNL or rcu_read_lock().
485 int genlmsg_multicast_allns(const struct genl_family *family,
486 struct sk_buff *skb, u32 portid,
487 unsigned int group, gfp_t flags);
490 * genlmsg_unicast - unicast a netlink message
491 * @net: network namespace to look up @portid in
492 * @skb: netlink message as socket buffer
493 * @portid: netlink portid of the destination socket
495 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
497 return nlmsg_unicast(net->genl_sock, skb, portid);
501 * genlmsg_reply - reply to a request
502 * @skb: netlink message to be sent back
503 * @info: receiver information
505 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
507 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
511 * genlmsg_data - head of message payload
512 * @gnlh: genetlink message header
514 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
516 return ((unsigned char *) gnlh + GENL_HDRLEN);
520 * genlmsg_len - length of message payload
521 * @gnlh: genetlink message header
523 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
525 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
527 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
531 * genlmsg_msg_size - length of genetlink message not including padding
532 * @payload: length of message payload
534 static inline int genlmsg_msg_size(int payload)
536 return GENL_HDRLEN + payload;
540 * genlmsg_total_size - length of genetlink message including padding
541 * @payload: length of message payload
543 static inline int genlmsg_total_size(int payload)
545 return NLMSG_ALIGN(genlmsg_msg_size(payload));
549 * genlmsg_new - Allocate a new generic netlink message
550 * @payload: size of the message payload
551 * @flags: the type of memory to allocate.
553 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
555 return nlmsg_new(genlmsg_total_size(payload), flags);
559 * genl_set_err - report error to genetlink broadcast listeners
560 * @family: the generic netlink family
561 * @net: the network namespace to report the error to
562 * @portid: the PORTID of a process that we want to skip (if any)
563 * @group: the broadcast group that will notice the error
564 * (this is the offset of the multicast group in the groups array)
565 * @code: error code, must be negative (as usual in kernelspace)
567 * This function returns the number of broadcast listeners that have set the
568 * NETLINK_RECV_NO_ENOBUFS socket option.
570 static inline int genl_set_err(const struct genl_family *family,
571 struct net *net, u32 portid,
574 if (WARN_ON_ONCE(group >= family->n_mcgrps))
576 group = family->mcgrp_offset + group;
577 return netlink_set_err(net->genl_sock, portid, group, code);
580 static inline int genl_has_listeners(const struct genl_family *family,
581 struct net *net, unsigned int group)
583 if (WARN_ON_ONCE(group >= family->n_mcgrps))
585 group = family->mcgrp_offset + group;
586 return netlink_has_listeners(net->genl_sock, group);
588 #endif /* __NET_GENERIC_NETLINK_H */