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> 6482a8524SThomas Graf 7482a8524SThomas Graf /** 8482a8524SThomas Graf * struct genl_family - generic netlink family 9482a8524SThomas Graf * @id: protocol family idenfitier 10482a8524SThomas Graf * @hdrsize: length of user specific header in bytes 11482a8524SThomas Graf * @name: name of family 12482a8524SThomas Graf * @version: protocol version 13482a8524SThomas Graf * @maxattr: maximum number of attributes supported 14482a8524SThomas Graf * @attrbuf: buffer to store parsed attributes 15482a8524SThomas Graf * @ops_list: list of all assigned operations 16482a8524SThomas Graf * @family_list: family list 17482a8524SThomas Graf */ 18482a8524SThomas Graf struct genl_family 19482a8524SThomas Graf { 20482a8524SThomas Graf unsigned int id; 21482a8524SThomas Graf unsigned int hdrsize; 22482a8524SThomas Graf char name[GENL_NAMSIZ]; 23482a8524SThomas Graf unsigned int version; 24482a8524SThomas Graf unsigned int maxattr; 25482a8524SThomas Graf struct nlattr ** attrbuf; /* private */ 26482a8524SThomas Graf struct list_head ops_list; /* private */ 27482a8524SThomas Graf struct list_head family_list; /* private */ 28482a8524SThomas Graf }; 29482a8524SThomas Graf 30482a8524SThomas Graf /** 31482a8524SThomas Graf * struct genl_info - receiving information 32482a8524SThomas Graf * @snd_seq: sending sequence number 33482a8524SThomas Graf * @snd_pid: netlink pid of sender 34482a8524SThomas Graf * @nlhdr: netlink message header 35482a8524SThomas Graf * @genlhdr: generic netlink message header 36482a8524SThomas Graf * @userhdr: user specific header 37482a8524SThomas Graf * @attrs: netlink attributes 38482a8524SThomas Graf */ 39482a8524SThomas Graf struct genl_info 40482a8524SThomas Graf { 41482a8524SThomas Graf u32 snd_seq; 42482a8524SThomas Graf u32 snd_pid; 43482a8524SThomas Graf struct nlmsghdr * nlhdr; 44482a8524SThomas Graf struct genlmsghdr * genlhdr; 45482a8524SThomas Graf void * userhdr; 46482a8524SThomas Graf struct nlattr ** attrs; 47482a8524SThomas Graf }; 48482a8524SThomas Graf 49482a8524SThomas Graf /** 50482a8524SThomas Graf * struct genl_ops - generic netlink operations 51482a8524SThomas Graf * @cmd: command identifier 52482a8524SThomas Graf * @flags: flags 53482a8524SThomas Graf * @policy: attribute validation policy 54482a8524SThomas Graf * @doit: standard command callback 55482a8524SThomas Graf * @dumpit: callback for dumpers 56*a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 57482a8524SThomas Graf * @ops_list: operations list 58482a8524SThomas Graf */ 59482a8524SThomas Graf struct genl_ops 60482a8524SThomas Graf { 61b461d2f2SPer Liden u8 cmd; 62482a8524SThomas Graf unsigned int flags; 63482a8524SThomas Graf struct nla_policy *policy; 64482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 65482a8524SThomas Graf struct genl_info *info); 66482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 67482a8524SThomas Graf struct netlink_callback *cb); 68*a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 69482a8524SThomas Graf struct list_head ops_list; 70482a8524SThomas Graf }; 71482a8524SThomas Graf 72482a8524SThomas Graf extern int genl_register_family(struct genl_family *family); 73482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family); 74482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops); 75482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops); 76482a8524SThomas Graf 77482a8524SThomas Graf extern struct sock *genl_sock; 78482a8524SThomas Graf 79482a8524SThomas Graf /** 80482a8524SThomas Graf * genlmsg_put - Add generic netlink header to netlink message 81482a8524SThomas Graf * @skb: socket buffer holding the message 82482a8524SThomas Graf * @pid: netlink pid the message is addressed to 83482a8524SThomas Graf * @seq: sequence number (usually the one of the sender) 8417c157c8SThomas Graf * @family: generic netlink family 85482a8524SThomas Graf * @flags netlink message flags 86482a8524SThomas Graf * @cmd: generic netlink command 87482a8524SThomas Graf * 88482a8524SThomas Graf * Returns pointer to user specific header 89482a8524SThomas Graf */ 90482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, 9117c157c8SThomas Graf struct genl_family *family, int flags, u8 cmd) 92482a8524SThomas Graf { 93482a8524SThomas Graf struct nlmsghdr *nlh; 94482a8524SThomas Graf struct genlmsghdr *hdr; 95482a8524SThomas Graf 9617c157c8SThomas Graf nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN + 9717c157c8SThomas Graf family->hdrsize, flags); 98482a8524SThomas Graf if (nlh == NULL) 99482a8524SThomas Graf return NULL; 100482a8524SThomas Graf 101482a8524SThomas Graf hdr = nlmsg_data(nlh); 102482a8524SThomas Graf hdr->cmd = cmd; 10317c157c8SThomas Graf hdr->version = family->version; 104482a8524SThomas Graf hdr->reserved = 0; 105482a8524SThomas Graf 106482a8524SThomas Graf return (char *) hdr + GENL_HDRLEN; 107482a8524SThomas Graf } 108482a8524SThomas Graf 109482a8524SThomas Graf /** 11017c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 11117c157c8SThomas Graf * @skb: socket buffer holding the message 11217c157c8SThomas Graf * @info: receiver info 11317c157c8SThomas Graf * @family: generic netlink family 11417c157c8SThomas Graf * @flags: netlink message flags 11517c157c8SThomas Graf * @cmd: generic netlink command 11617c157c8SThomas Graf * 11717c157c8SThomas Graf * Returns pointer to user specific header 11817c157c8SThomas Graf */ 11917c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 12017c157c8SThomas Graf struct genl_info *info, 12117c157c8SThomas Graf struct genl_family *family, 12217c157c8SThomas Graf int flags, u8 cmd) 12317c157c8SThomas Graf { 12417c157c8SThomas Graf return genlmsg_put(skb, info->snd_pid, info->snd_seq, family, 12517c157c8SThomas Graf flags, cmd); 12617c157c8SThomas Graf } 12717c157c8SThomas Graf 12817c157c8SThomas Graf /** 129482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 130482a8524SThomas Graf * @skb: socket buffer the message is stored in 131482a8524SThomas Graf * @hdr: user specific header 132482a8524SThomas Graf */ 133482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr) 134482a8524SThomas Graf { 135482a8524SThomas Graf return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 136482a8524SThomas Graf } 137482a8524SThomas Graf 138482a8524SThomas Graf /** 139482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 140482a8524SThomas Graf * @skb: socket buffer the message is stored in 141482a8524SThomas Graf * @hdr: generic netlink message header 142482a8524SThomas Graf */ 143482a8524SThomas Graf static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr) 144482a8524SThomas Graf { 145482a8524SThomas Graf return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 146482a8524SThomas Graf } 147482a8524SThomas Graf 148482a8524SThomas Graf /** 149482a8524SThomas Graf * genlmsg_multicast - multicast a netlink message 150482a8524SThomas Graf * @skb: netlink message as socket buffer 151482a8524SThomas Graf * @pid: own netlink pid to avoid sending to yourself 152482a8524SThomas Graf * @group: multicast group id 153d387f6adSThomas Graf * @flags: allocation flags 154482a8524SThomas Graf */ 155482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid, 156d387f6adSThomas Graf unsigned int group, gfp_t flags) 157482a8524SThomas Graf { 158d387f6adSThomas Graf return nlmsg_multicast(genl_sock, skb, pid, group, flags); 159482a8524SThomas Graf } 160482a8524SThomas Graf 161482a8524SThomas Graf /** 162482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 163482a8524SThomas Graf * @skb: netlink message as socket buffer 164482a8524SThomas Graf * @pid: netlink pid of the destination socket 165482a8524SThomas Graf */ 166482a8524SThomas Graf static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid) 167482a8524SThomas Graf { 168482a8524SThomas Graf return nlmsg_unicast(genl_sock, skb, pid); 169482a8524SThomas Graf } 170482a8524SThomas Graf 171fb0ba6bdSBalbir Singh /** 17281878d27SThomas Graf * genlmsg_reply - reply to a request 17381878d27SThomas Graf * @skb: netlink message to be sent back 17481878d27SThomas Graf * @info: receiver information 17581878d27SThomas Graf */ 17681878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 17781878d27SThomas Graf { 17881878d27SThomas Graf return genlmsg_unicast(skb, info->snd_pid); 17981878d27SThomas Graf } 18081878d27SThomas Graf 18181878d27SThomas Graf /** 182fb0ba6bdSBalbir Singh * gennlmsg_data - head of message payload 183fb0ba6bdSBalbir Singh * @gnlh: genetlink messsage header 184fb0ba6bdSBalbir Singh */ 185fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 186fb0ba6bdSBalbir Singh { 187fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 188fb0ba6bdSBalbir Singh } 189fb0ba6bdSBalbir Singh 190fb0ba6bdSBalbir Singh /** 191fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 192fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 193fb0ba6bdSBalbir Singh */ 194fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 195fb0ba6bdSBalbir Singh { 196fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 197fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 198fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 199fb0ba6bdSBalbir Singh } 200fb0ba6bdSBalbir Singh 20117db952cSBalbir Singh /** 20217db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 20317db952cSBalbir Singh * @payload: length of message payload 20417db952cSBalbir Singh */ 20517db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 20617db952cSBalbir Singh { 20717db952cSBalbir Singh return GENL_HDRLEN + payload; 20817db952cSBalbir Singh } 20917db952cSBalbir Singh 21017db952cSBalbir Singh /** 21117db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 21217db952cSBalbir Singh * @payload: length of message payload 21317db952cSBalbir Singh */ 21417db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 21517db952cSBalbir Singh { 21617db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 21717db952cSBalbir Singh } 21817db952cSBalbir Singh 2193dabc715SThomas Graf /** 2203dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 2213dabc715SThomas Graf * @payload: size of the message payload 2223dabc715SThomas Graf * @flags: the type of memory to allocate. 2233dabc715SThomas Graf */ 2243dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 2253dabc715SThomas Graf { 2263dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 2273dabc715SThomas Graf } 2283dabc715SThomas Graf 2293dabc715SThomas Graf 230482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 231