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