1482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H 2482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H 3482a8524SThomas Graf 4482a8524SThomas Graf #include <linux/genetlink.h> 5482a8524SThomas Graf #include <net/netlink.h> 6134e6375SJohannes Berg #include <net/net_namespace.h> 7482a8524SThomas Graf 8482a8524SThomas Graf /** 92dbba6f7SJohannes Berg * struct genl_multicast_group - generic netlink multicast group 102dbba6f7SJohannes Berg * @name: name of the multicast group, names are per-family 112dbba6f7SJohannes Berg * @id: multicast group ID, assigned by the core, to use with 122dbba6f7SJohannes Berg * genlmsg_multicast(). 132dbba6f7SJohannes Berg * @list: list entry for linking 142dbba6f7SJohannes Berg * @family: pointer to family, need not be set before registering 152dbba6f7SJohannes Berg */ 16*fd2c3ef7SEric Dumazet struct genl_multicast_group { 172dbba6f7SJohannes Berg struct genl_family *family; /* private */ 182dbba6f7SJohannes Berg struct list_head list; /* private */ 192dbba6f7SJohannes Berg char name[GENL_NAMSIZ]; 202dbba6f7SJohannes Berg u32 id; 212dbba6f7SJohannes Berg }; 222dbba6f7SJohannes Berg 232dbba6f7SJohannes Berg /** 24482a8524SThomas Graf * struct genl_family - generic netlink family 25482a8524SThomas Graf * @id: protocol family idenfitier 26482a8524SThomas Graf * @hdrsize: length of user specific header in bytes 27482a8524SThomas Graf * @name: name of family 28482a8524SThomas Graf * @version: protocol version 29482a8524SThomas Graf * @maxattr: maximum number of attributes supported 30134e6375SJohannes Berg * @netnsok: set to true if the family can handle network 31134e6375SJohannes Berg * namespaces and should be presented in all of them 32482a8524SThomas Graf * @attrbuf: buffer to store parsed attributes 33482a8524SThomas Graf * @ops_list: list of all assigned operations 34482a8524SThomas Graf * @family_list: family list 352dbba6f7SJohannes Berg * @mcast_groups: multicast groups list 36482a8524SThomas Graf */ 37*fd2c3ef7SEric Dumazet struct genl_family { 38482a8524SThomas Graf unsigned int id; 39482a8524SThomas Graf unsigned int hdrsize; 40482a8524SThomas Graf char name[GENL_NAMSIZ]; 41482a8524SThomas Graf unsigned int version; 42482a8524SThomas Graf unsigned int maxattr; 43134e6375SJohannes Berg bool netnsok; 44482a8524SThomas Graf struct nlattr ** attrbuf; /* private */ 45482a8524SThomas Graf struct list_head ops_list; /* private */ 46482a8524SThomas Graf struct list_head family_list; /* private */ 472dbba6f7SJohannes Berg struct list_head mcast_groups; /* private */ 48482a8524SThomas Graf }; 49482a8524SThomas Graf 50482a8524SThomas Graf /** 51482a8524SThomas Graf * struct genl_info - receiving information 52482a8524SThomas Graf * @snd_seq: sending sequence number 53482a8524SThomas Graf * @snd_pid: netlink pid of sender 54482a8524SThomas Graf * @nlhdr: netlink message header 55482a8524SThomas Graf * @genlhdr: generic netlink message header 56482a8524SThomas Graf * @userhdr: user specific header 57482a8524SThomas Graf * @attrs: netlink attributes 58482a8524SThomas Graf */ 59*fd2c3ef7SEric Dumazet struct genl_info { 60482a8524SThomas Graf u32 snd_seq; 61482a8524SThomas Graf u32 snd_pid; 62482a8524SThomas Graf struct nlmsghdr * nlhdr; 63482a8524SThomas Graf struct genlmsghdr * genlhdr; 64482a8524SThomas Graf void * userhdr; 65482a8524SThomas Graf struct nlattr ** attrs; 66134e6375SJohannes Berg #ifdef CONFIG_NET_NS 67134e6375SJohannes Berg struct net * _net; 68134e6375SJohannes Berg #endif 69482a8524SThomas Graf }; 70482a8524SThomas Graf 71134e6375SJohannes Berg #ifdef CONFIG_NET_NS 72134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 73134e6375SJohannes Berg { 74134e6375SJohannes Berg return info->_net; 75134e6375SJohannes Berg } 76134e6375SJohannes Berg 77134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 78134e6375SJohannes Berg { 79134e6375SJohannes Berg info->_net = net; 80134e6375SJohannes Berg } 81134e6375SJohannes Berg #else 82134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 83134e6375SJohannes Berg { 84134e6375SJohannes Berg return &init_net; 85134e6375SJohannes Berg } 86134e6375SJohannes Berg 87134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 88134e6375SJohannes Berg { 89134e6375SJohannes Berg } 90134e6375SJohannes Berg #endif 91134e6375SJohannes Berg 92482a8524SThomas Graf /** 93482a8524SThomas Graf * struct genl_ops - generic netlink operations 94482a8524SThomas Graf * @cmd: command identifier 95482a8524SThomas Graf * @flags: flags 96482a8524SThomas Graf * @policy: attribute validation policy 97482a8524SThomas Graf * @doit: standard command callback 98482a8524SThomas Graf * @dumpit: callback for dumpers 99a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 100482a8524SThomas Graf * @ops_list: operations list 101482a8524SThomas Graf */ 102*fd2c3ef7SEric Dumazet struct genl_ops { 103b461d2f2SPer Liden u8 cmd; 104482a8524SThomas Graf unsigned int flags; 105ef7c79edSPatrick McHardy const struct nla_policy *policy; 106482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 107482a8524SThomas Graf struct genl_info *info); 108482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 109482a8524SThomas Graf struct netlink_callback *cb); 110a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 111482a8524SThomas Graf struct list_head ops_list; 112482a8524SThomas Graf }; 113482a8524SThomas Graf 114482a8524SThomas Graf extern int genl_register_family(struct genl_family *family); 115a7b11d73SMichał Mirosław extern int genl_register_family_with_ops(struct genl_family *family, 116a7b11d73SMichał Mirosław struct genl_ops *ops, size_t n_ops); 117482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family); 118482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops); 119482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops); 1202dbba6f7SJohannes Berg extern int genl_register_mc_group(struct genl_family *family, 1212dbba6f7SJohannes Berg struct genl_multicast_group *grp); 1222dbba6f7SJohannes Berg extern void genl_unregister_mc_group(struct genl_family *family, 1232dbba6f7SJohannes Berg struct genl_multicast_group *grp); 124482a8524SThomas Graf 125482a8524SThomas Graf /** 126482a8524SThomas Graf * genlmsg_put - Add generic netlink header to netlink message 127482a8524SThomas Graf * @skb: socket buffer holding the message 128482a8524SThomas Graf * @pid: netlink pid the message is addressed to 129482a8524SThomas Graf * @seq: sequence number (usually the one of the sender) 13017c157c8SThomas Graf * @family: generic netlink family 131482a8524SThomas Graf * @flags netlink message flags 132482a8524SThomas Graf * @cmd: generic netlink command 133482a8524SThomas Graf * 134482a8524SThomas Graf * Returns pointer to user specific header 135482a8524SThomas Graf */ 136482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, 13717c157c8SThomas Graf struct genl_family *family, int flags, u8 cmd) 138482a8524SThomas Graf { 139482a8524SThomas Graf struct nlmsghdr *nlh; 140482a8524SThomas Graf struct genlmsghdr *hdr; 141482a8524SThomas Graf 14217c157c8SThomas Graf nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN + 14317c157c8SThomas Graf family->hdrsize, flags); 144482a8524SThomas Graf if (nlh == NULL) 145482a8524SThomas Graf return NULL; 146482a8524SThomas Graf 147482a8524SThomas Graf hdr = nlmsg_data(nlh); 148482a8524SThomas Graf hdr->cmd = cmd; 14917c157c8SThomas Graf hdr->version = family->version; 150482a8524SThomas Graf hdr->reserved = 0; 151482a8524SThomas Graf 152482a8524SThomas Graf return (char *) hdr + GENL_HDRLEN; 153482a8524SThomas Graf } 154482a8524SThomas Graf 155482a8524SThomas Graf /** 15617c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 15717c157c8SThomas Graf * @skb: socket buffer holding the message 15817c157c8SThomas Graf * @info: receiver info 15917c157c8SThomas Graf * @family: generic netlink family 16017c157c8SThomas Graf * @flags: netlink message flags 16117c157c8SThomas Graf * @cmd: generic netlink command 16217c157c8SThomas Graf * 16317c157c8SThomas Graf * Returns pointer to user specific header 16417c157c8SThomas Graf */ 16517c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 16617c157c8SThomas Graf struct genl_info *info, 16717c157c8SThomas Graf struct genl_family *family, 16817c157c8SThomas Graf int flags, u8 cmd) 16917c157c8SThomas Graf { 17017c157c8SThomas Graf return genlmsg_put(skb, info->snd_pid, info->snd_seq, family, 17117c157c8SThomas Graf flags, cmd); 17217c157c8SThomas Graf } 17317c157c8SThomas Graf 17417c157c8SThomas Graf /** 175482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 176482a8524SThomas Graf * @skb: socket buffer the message is stored in 177482a8524SThomas Graf * @hdr: user specific header 178482a8524SThomas Graf */ 179482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr) 180482a8524SThomas Graf { 181482a8524SThomas Graf return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 182482a8524SThomas Graf } 183482a8524SThomas Graf 184482a8524SThomas Graf /** 185482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 186482a8524SThomas Graf * @skb: socket buffer the message is stored in 187482a8524SThomas Graf * @hdr: generic netlink message header 188482a8524SThomas Graf */ 189bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 190482a8524SThomas Graf { 191bc3ed28cSThomas Graf nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 192482a8524SThomas Graf } 193482a8524SThomas Graf 194482a8524SThomas Graf /** 195134e6375SJohannes Berg * genlmsg_multicast_netns - multicast a netlink message to a specific netns 196134e6375SJohannes Berg * @net: the net namespace 197134e6375SJohannes Berg * @skb: netlink message as socket buffer 198134e6375SJohannes Berg * @pid: own netlink pid to avoid sending to yourself 199134e6375SJohannes Berg * @group: multicast group id 200134e6375SJohannes Berg * @flags: allocation flags 201134e6375SJohannes Berg */ 202134e6375SJohannes Berg static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb, 203134e6375SJohannes Berg u32 pid, unsigned int group, gfp_t flags) 204134e6375SJohannes Berg { 205134e6375SJohannes Berg return nlmsg_multicast(net->genl_sock, skb, pid, group, flags); 206134e6375SJohannes Berg } 207134e6375SJohannes Berg 208134e6375SJohannes Berg /** 209134e6375SJohannes Berg * genlmsg_multicast - multicast a netlink message to the default netns 210482a8524SThomas Graf * @skb: netlink message as socket buffer 211482a8524SThomas Graf * @pid: own netlink pid to avoid sending to yourself 212482a8524SThomas Graf * @group: multicast group id 213d387f6adSThomas Graf * @flags: allocation flags 214482a8524SThomas Graf */ 215482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid, 216d387f6adSThomas Graf unsigned int group, gfp_t flags) 217482a8524SThomas Graf { 218134e6375SJohannes Berg return genlmsg_multicast_netns(&init_net, skb, pid, group, flags); 219482a8524SThomas Graf } 220482a8524SThomas Graf 221482a8524SThomas Graf /** 222134e6375SJohannes Berg * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 223134e6375SJohannes Berg * @skb: netlink message as socket buffer 224134e6375SJohannes Berg * @pid: own netlink pid to avoid sending to yourself 225134e6375SJohannes Berg * @group: multicast group id 226134e6375SJohannes Berg * @flags: allocation flags 227134e6375SJohannes Berg * 228134e6375SJohannes Berg * This function must hold the RTNL or rcu_read_lock(). 229134e6375SJohannes Berg */ 230134e6375SJohannes Berg int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, 231134e6375SJohannes Berg unsigned int group, gfp_t flags); 232134e6375SJohannes Berg 233134e6375SJohannes Berg /** 234482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 235482a8524SThomas Graf * @skb: netlink message as socket buffer 236482a8524SThomas Graf * @pid: netlink pid of the destination socket 237482a8524SThomas Graf */ 238134e6375SJohannes Berg static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid) 239482a8524SThomas Graf { 240134e6375SJohannes Berg return nlmsg_unicast(net->genl_sock, skb, pid); 241482a8524SThomas Graf } 242482a8524SThomas Graf 243fb0ba6bdSBalbir Singh /** 24481878d27SThomas Graf * genlmsg_reply - reply to a request 24581878d27SThomas Graf * @skb: netlink message to be sent back 24681878d27SThomas Graf * @info: receiver information 24781878d27SThomas Graf */ 24881878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 24981878d27SThomas Graf { 250134e6375SJohannes Berg return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); 25181878d27SThomas Graf } 25281878d27SThomas Graf 25381878d27SThomas Graf /** 254fb0ba6bdSBalbir Singh * gennlmsg_data - head of message payload 255fb0ba6bdSBalbir Singh * @gnlh: genetlink messsage header 256fb0ba6bdSBalbir Singh */ 257fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 258fb0ba6bdSBalbir Singh { 259fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 260fb0ba6bdSBalbir Singh } 261fb0ba6bdSBalbir Singh 262fb0ba6bdSBalbir Singh /** 263fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 264fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 265fb0ba6bdSBalbir Singh */ 266fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 267fb0ba6bdSBalbir Singh { 268fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 269fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 270fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 271fb0ba6bdSBalbir Singh } 272fb0ba6bdSBalbir Singh 27317db952cSBalbir Singh /** 27417db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 27517db952cSBalbir Singh * @payload: length of message payload 27617db952cSBalbir Singh */ 27717db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 27817db952cSBalbir Singh { 27917db952cSBalbir Singh return GENL_HDRLEN + payload; 28017db952cSBalbir Singh } 28117db952cSBalbir Singh 28217db952cSBalbir Singh /** 28317db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 28417db952cSBalbir Singh * @payload: length of message payload 28517db952cSBalbir Singh */ 28617db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 28717db952cSBalbir Singh { 28817db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 28917db952cSBalbir Singh } 29017db952cSBalbir Singh 2913dabc715SThomas Graf /** 2923dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 2933dabc715SThomas Graf * @payload: size of the message payload 2943dabc715SThomas Graf * @flags: the type of memory to allocate. 2953dabc715SThomas Graf */ 2963dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 2973dabc715SThomas Graf { 2983dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 2993dabc715SThomas Graf } 3003dabc715SThomas Graf 3013dabc715SThomas Graf 302482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 303