linux/include/net/genetlink.h
<<
>>
Prefs
   1#ifndef __NET_GENERIC_NETLINK_H
   2#define __NET_GENERIC_NETLINK_H
   3
   4#include <linux/genetlink.h>
   5#include <net/netlink.h>
   6#include <net/net_namespace.h>
   7
   8/**
   9 * struct genl_multicast_group - generic netlink multicast group
  10 * @name: name of the multicast group, names are per-family
  11 * @id: multicast group ID, assigned by the core, to use with
  12 *      genlmsg_multicast().
  13 * @list: list entry for linking
  14 * @family: pointer to family, need not be set before registering
  15 */
  16struct genl_multicast_group
  17{
  18        struct genl_family      *family;        /* private */
  19        struct list_head        list;           /* private */
  20        char                    name[GENL_NAMSIZ];
  21        u32                     id;
  22};
  23
  24/**
  25 * struct genl_family - generic netlink family
  26 * @id: protocol family idenfitier
  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 * @netnsok: set to true if the family can handle network
  32 *      namespaces and should be presented in all of them
  33 * @attrbuf: buffer to store parsed attributes
  34 * @ops_list: list of all assigned operations
  35 * @family_list: family list
  36 * @mcast_groups: multicast groups list
  37 */
  38struct genl_family
  39{
  40        unsigned int            id;
  41        unsigned int            hdrsize;
  42        char                    name[GENL_NAMSIZ];
  43        unsigned int            version;
  44        unsigned int            maxattr;
  45        bool                    netnsok;
  46        struct nlattr **        attrbuf;        /* private */
  47        struct list_head        ops_list;       /* private */
  48        struct list_head        family_list;    /* private */
  49        struct list_head        mcast_groups;   /* private */
  50};
  51
  52/**
  53 * struct genl_info - receiving information
  54 * @snd_seq: sending sequence number
  55 * @snd_pid: netlink pid of sender
  56 * @nlhdr: netlink message header
  57 * @genlhdr: generic netlink message header
  58 * @userhdr: user specific header
  59 * @attrs: netlink attributes
  60 */
  61struct genl_info
  62{
  63        u32                     snd_seq;
  64        u32                     snd_pid;
  65        struct nlmsghdr *       nlhdr;
  66        struct genlmsghdr *     genlhdr;
  67        void *                  userhdr;
  68        struct nlattr **        attrs;
  69#ifdef CONFIG_NET_NS
  70        struct net *            _net;
  71#endif
  72};
  73
  74#ifdef CONFIG_NET_NS
  75static inline struct net *genl_info_net(struct genl_info *info)
  76{
  77        return info->_net;
  78}
  79
  80static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  81{
  82        info->_net = net;
  83}
  84#else
  85static inline struct net *genl_info_net(struct genl_info *info)
  86{
  87        return &init_net;
  88}
  89
  90static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  91{
  92}
  93#endif
  94
  95/**
  96 * struct genl_ops - generic netlink operations
  97 * @cmd: command identifier
  98 * @flags: flags
  99 * @policy: attribute validation policy
 100 * @doit: standard command callback
 101 * @dumpit: callback for dumpers
 102 * @done: completion callback for dumps
 103 * @ops_list: operations list
 104 */
 105struct genl_ops
 106{
 107        u8                      cmd;
 108        unsigned int            flags;
 109        const struct nla_policy *policy;
 110        int                    (*doit)(struct sk_buff *skb,
 111                                       struct genl_info *info);
 112        int                    (*dumpit)(struct sk_buff *skb,
 113                                         struct netlink_callback *cb);
 114        int                    (*done)(struct netlink_callback *cb);
 115        struct list_head        ops_list;
 116};
 117
 118extern int genl_register_family(struct genl_family *family);
 119extern int genl_register_family_with_ops(struct genl_family *family,
 120        struct genl_ops *ops, size_t n_ops);
 121extern int genl_unregister_family(struct genl_family *family);
 122extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
 123extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
 124extern int genl_register_mc_group(struct genl_family *family,
 125                                  struct genl_multicast_group *grp);
 126extern void genl_unregister_mc_group(struct genl_family *family,
 127                                     struct genl_multicast_group *grp);
 128
 129/**
 130 * genlmsg_put - Add generic netlink header to netlink message
 131 * @skb: socket buffer holding the message
 132 * @pid: netlink pid the message is addressed to
 133 * @seq: sequence number (usually the one of the sender)
 134 * @family: generic netlink family
 135 * @flags netlink message flags
 136 * @cmd: generic netlink command
 137 *
 138 * Returns pointer to user specific header
 139 */
 140static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
 141                                struct genl_family *family, int flags, u8 cmd)
 142{
 143        struct nlmsghdr *nlh;
 144        struct genlmsghdr *hdr;
 145
 146        nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
 147                        family->hdrsize, flags);
 148        if (nlh == NULL)
 149                return NULL;
 150
 151        hdr = nlmsg_data(nlh);
 152        hdr->cmd = cmd;
 153        hdr->version = family->version;
 154        hdr->reserved = 0;
 155
 156        return (char *) hdr + GENL_HDRLEN;
 157}
 158
 159/**
 160 * genlmsg_put_reply - Add generic netlink header to a reply message
 161 * @skb: socket buffer holding the message
 162 * @info: receiver info
 163 * @family: generic netlink family
 164 * @flags: netlink message flags
 165 * @cmd: generic netlink command
 166 *
 167 * Returns pointer to user specific header
 168 */
 169static inline void *genlmsg_put_reply(struct sk_buff *skb,
 170                                      struct genl_info *info,
 171                                      struct genl_family *family,
 172                                      int flags, u8 cmd)
 173{
 174        return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
 175                           flags, cmd);
 176}
 177
 178/**
 179 * genlmsg_end - Finalize a generic netlink message
 180 * @skb: socket buffer the message is stored in
 181 * @hdr: user specific header
 182 */
 183static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
 184{
 185        return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
 186}
 187
 188/**
 189 * genlmsg_cancel - Cancel construction of a generic netlink message
 190 * @skb: socket buffer the message is stored in
 191 * @hdr: generic netlink message header
 192 */
 193static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
 194{
 195        nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
 196}
 197
 198/**
 199 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
 200 * @net: the net namespace
 201 * @skb: netlink message as socket buffer
 202 * @pid: own netlink pid to avoid sending to yourself
 203 * @group: multicast group id
 204 * @flags: allocation flags
 205 */
 206static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
 207                                          u32 pid, unsigned int group, gfp_t flags)
 208{
 209        return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
 210}
 211
 212/**
 213 * genlmsg_multicast - multicast a netlink message to the default netns
 214 * @skb: netlink message as socket buffer
 215 * @pid: own netlink pid to avoid sending to yourself
 216 * @group: multicast group id
 217 * @flags: allocation flags
 218 */
 219static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
 220                                    unsigned int group, gfp_t flags)
 221{
 222        return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
 223}
 224
 225/**
 226 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
 227 * @skb: netlink message as socket buffer
 228 * @pid: own netlink pid to avoid sending to yourself
 229 * @group: multicast group id
 230 * @flags: allocation flags
 231 *
 232 * This function must hold the RTNL or rcu_read_lock().
 233 */
 234int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
 235                            unsigned int group, gfp_t flags);
 236
 237/**
 238 * genlmsg_unicast - unicast a netlink message
 239 * @skb: netlink message as socket buffer
 240 * @pid: netlink pid of the destination socket
 241 */
 242static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
 243{
 244        return nlmsg_unicast(net->genl_sock, skb, pid);
 245}
 246
 247/**
 248 * genlmsg_reply - reply to a request
 249 * @skb: netlink message to be sent back
 250 * @info: receiver information
 251 */
 252static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
 253{
 254        return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
 255}
 256
 257/**
 258 * gennlmsg_data - head of message payload
 259 * @gnlh: genetlink messsage header
 260 */
 261static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
 262{
 263        return ((unsigned char *) gnlh + GENL_HDRLEN);
 264}
 265
 266/**
 267 * genlmsg_len - length of message payload
 268 * @gnlh: genetlink message header
 269 */
 270static inline int genlmsg_len(const struct genlmsghdr *gnlh)
 271{
 272        struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
 273                                                        NLMSG_HDRLEN);
 274        return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
 275}
 276
 277/**
 278 * genlmsg_msg_size - length of genetlink message not including padding
 279 * @payload: length of message payload
 280 */
 281static inline int genlmsg_msg_size(int payload)
 282{
 283        return GENL_HDRLEN + payload;
 284}
 285
 286/**
 287 * genlmsg_total_size - length of genetlink message including padding
 288 * @payload: length of message payload
 289 */
 290static inline int genlmsg_total_size(int payload)
 291{
 292        return NLMSG_ALIGN(genlmsg_msg_size(payload));
 293}
 294
 295/**
 296 * genlmsg_new - Allocate a new generic netlink message
 297 * @payload: size of the message payload
 298 * @flags: the type of memory to allocate.
 299 */
 300static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
 301{
 302        return nlmsg_new(genlmsg_total_size(payload), flags);
 303}
 304
 305
 306#endif  /* __NET_GENERIC_NETLINK_H */
 307