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