#ifndef __NET_GENERIC_NETLINK_H1#define __NET_GENERIC_NETLINK_H23#include <linux/genetlink.h>4#include <net/netlink.h>5#include <net/net_namespace.h>67/**8* struct genl_multicast_group - generic netlink multicast group9* @name: name of the multicast group, names are per-family10* @id: multicast group ID, assigned by the core, to use with11* genlmsg_multicast().12* @list: list entry for linking13* @family: pointer to family, need not be set before registering14*/15struct genl_multicast_group {16struct genl_family *family; /* private */17struct list_head list; /* private */18char name[GENL_NAMSIZ];19u32 id;20};2122struct genl_ops;23struct genl_info;2425/**26* struct genl_family - generic netlink family27* @id: protocol family idenfitier28* @hdrsize: length of user specific header in bytes29* @name: name of family30* @version: protocol version31* @maxattr: maximum number of attributes supported32* @netnsok: set to true if the family can handle network33* namespaces and should be presented in all of them34* @pre_doit: called before an operation's doit callback, it may35* do additional, common, filtering and return an error36* @post_doit: called after an operation's doit callback, it may37* undo operations done by pre_doit, for example release locks38* @attrbuf: buffer to store parsed attributes39* @ops_list: list of all assigned operations40* @family_list: family list41* @mcast_groups: multicast groups list42*/43struct genl_family {44unsigned int id;45unsigned int hdrsize;46char name[GENL_NAMSIZ];47unsigned int version;48unsigned int maxattr;49bool netnsok;50int (*pre_doit)(struct genl_ops *ops,51struct sk_buff *skb,52struct genl_info *info);53void (*post_doit)(struct genl_ops *ops,54struct sk_buff *skb,55struct genl_info *info);56struct nlattr ** attrbuf; /* private */57struct list_head ops_list; /* private */58struct list_head family_list; /* private */59struct list_head mcast_groups; /* private */60};6162/**63* struct genl_info - receiving information64* @snd_seq: sending sequence number65* @snd_pid: netlink pid of sender66* @nlhdr: netlink message header67* @genlhdr: generic netlink message header68* @userhdr: user specific header69* @attrs: netlink attributes70* @_net: network namespace71* @user_ptr: user pointers72*/73struct genl_info {74u32 snd_seq;75u32 snd_pid;76struct nlmsghdr * nlhdr;77struct genlmsghdr * genlhdr;78void * userhdr;79struct nlattr ** attrs;80#ifdef CONFIG_NET_NS81struct net * _net;82#endif83void * user_ptr[2];84};8586static inline struct net *genl_info_net(struct genl_info *info)87{88return read_pnet(&info->_net);89}9091static inline void genl_info_net_set(struct genl_info *info, struct net *net)92{93write_pnet(&info->_net, net);94}9596/**97* struct genl_ops - generic netlink operations98* @cmd: command identifier99* @internal_flags: flags used by the family100* @flags: flags101* @policy: attribute validation policy102* @doit: standard command callback103* @dumpit: callback for dumpers104* @done: completion callback for dumps105* @ops_list: operations list106*/107struct genl_ops {108u8 cmd;109u8 internal_flags;110unsigned int flags;111const struct nla_policy *policy;112int (*doit)(struct sk_buff *skb,113struct genl_info *info);114int (*dumpit)(struct sk_buff *skb,115struct netlink_callback *cb);116int (*done)(struct netlink_callback *cb);117struct list_head ops_list;118};119120extern int genl_register_family(struct genl_family *family);121extern int genl_register_family_with_ops(struct genl_family *family,122struct genl_ops *ops, size_t n_ops);123extern int genl_unregister_family(struct genl_family *family);124extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);125extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);126extern int genl_register_mc_group(struct genl_family *family,127struct genl_multicast_group *grp);128extern void genl_unregister_mc_group(struct genl_family *family,129struct genl_multicast_group *grp);130131/**132* genlmsg_put - Add generic netlink header to netlink message133* @skb: socket buffer holding the message134* @pid: netlink pid the message is addressed to135* @seq: sequence number (usually the one of the sender)136* @family: generic netlink family137* @flags netlink message flags138* @cmd: generic netlink command139*140* Returns pointer to user specific header141*/142static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,143struct genl_family *family, int flags, u8 cmd)144{145struct nlmsghdr *nlh;146struct genlmsghdr *hdr;147148nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +149family->hdrsize, flags);150if (nlh == NULL)151return NULL;152153hdr = nlmsg_data(nlh);154hdr->cmd = cmd;155hdr->version = family->version;156hdr->reserved = 0;157158return (char *) hdr + GENL_HDRLEN;159}160161/**162* genlmsg_put_reply - Add generic netlink header to a reply message163* @skb: socket buffer holding the message164* @info: receiver info165* @family: generic netlink family166* @flags: netlink message flags167* @cmd: generic netlink command168*169* Returns pointer to user specific header170*/171static inline void *genlmsg_put_reply(struct sk_buff *skb,172struct genl_info *info,173struct genl_family *family,174int flags, u8 cmd)175{176return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,177flags, cmd);178}179180/**181* genlmsg_end - Finalize a generic netlink message182* @skb: socket buffer the message is stored in183* @hdr: user specific header184*/185static inline int genlmsg_end(struct sk_buff *skb, void *hdr)186{187return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);188}189190/**191* genlmsg_cancel - Cancel construction of a generic netlink message192* @skb: socket buffer the message is stored in193* @hdr: generic netlink message header194*/195static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)196{197if (hdr)198nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);199}200201/**202* genlmsg_multicast_netns - multicast a netlink message to a specific netns203* @net: the net namespace204* @skb: netlink message as socket buffer205* @pid: own netlink pid to avoid sending to yourself206* @group: multicast group id207* @flags: allocation flags208*/209static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,210u32 pid, unsigned int group, gfp_t flags)211{212return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);213}214215/**216* genlmsg_multicast - multicast a netlink message to the default netns217* @skb: netlink message as socket buffer218* @pid: own netlink pid to avoid sending to yourself219* @group: multicast group id220* @flags: allocation flags221*/222static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,223unsigned int group, gfp_t flags)224{225return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);226}227228/**229* genlmsg_multicast_allns - multicast a netlink message to all net namespaces230* @skb: netlink message as socket buffer231* @pid: own netlink pid to avoid sending to yourself232* @group: multicast group id233* @flags: allocation flags234*235* This function must hold the RTNL or rcu_read_lock().236*/237int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,238unsigned int group, gfp_t flags);239240/**241* genlmsg_unicast - unicast a netlink message242* @skb: netlink message as socket buffer243* @pid: netlink pid of the destination socket244*/245static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)246{247return nlmsg_unicast(net->genl_sock, skb, pid);248}249250/**251* genlmsg_reply - reply to a request252* @skb: netlink message to be sent back253* @info: receiver information254*/255static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)256{257return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);258}259260/**261* gennlmsg_data - head of message payload262* @gnlh: genetlink message header263*/264static inline void *genlmsg_data(const struct genlmsghdr *gnlh)265{266return ((unsigned char *) gnlh + GENL_HDRLEN);267}268269/**270* genlmsg_len - length of message payload271* @gnlh: genetlink message header272*/273static inline int genlmsg_len(const struct genlmsghdr *gnlh)274{275struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -276NLMSG_HDRLEN);277return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);278}279280/**281* genlmsg_msg_size - length of genetlink message not including padding282* @payload: length of message payload283*/284static inline int genlmsg_msg_size(int payload)285{286return GENL_HDRLEN + payload;287}288289/**290* genlmsg_total_size - length of genetlink message including padding291* @payload: length of message payload292*/293static inline int genlmsg_total_size(int payload)294{295return NLMSG_ALIGN(genlmsg_msg_size(payload));296}297298/**299* genlmsg_new - Allocate a new generic netlink message300* @payload: size of the message payload301* @flags: the type of memory to allocate.302*/303static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)304{305return nlmsg_new(genlmsg_total_size(payload), flags);306}307308309#endif /* __NET_GENERIC_NETLINK_H */310311312