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 */ 16fd2c3ef7SEric 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 23ff4c92d8SJohannes Berg struct genl_ops; 24ff4c92d8SJohannes Berg struct genl_info; 25ff4c92d8SJohannes Berg 262dbba6f7SJohannes Berg /** 27482a8524SThomas Graf * struct genl_family - generic netlink family 28482a8524SThomas Graf * @id: protocol family idenfitier 29482a8524SThomas Graf * @hdrsize: length of user specific header in bytes 30482a8524SThomas Graf * @name: name of family 31482a8524SThomas Graf * @version: protocol version 32482a8524SThomas Graf * @maxattr: maximum number of attributes supported 33134e6375SJohannes Berg * @netnsok: set to true if the family can handle network 34134e6375SJohannes Berg * namespaces and should be presented in all of them 35ff4c92d8SJohannes Berg * @pre_doit: called before an operation's doit callback, it may 36ff4c92d8SJohannes Berg * do additional, common, filtering and return an error 37ff4c92d8SJohannes Berg * @post_doit: called after an operation's doit callback, it may 38ff4c92d8SJohannes Berg * undo operations done by pre_doit, for example release locks 39482a8524SThomas Graf * @attrbuf: buffer to store parsed attributes 40482a8524SThomas Graf * @ops_list: list of all assigned operations 41482a8524SThomas Graf * @family_list: family list 422dbba6f7SJohannes Berg * @mcast_groups: multicast groups list 43482a8524SThomas Graf */ 44fd2c3ef7SEric Dumazet struct genl_family { 45482a8524SThomas Graf unsigned int id; 46482a8524SThomas Graf unsigned int hdrsize; 47482a8524SThomas Graf char name[GENL_NAMSIZ]; 48482a8524SThomas Graf unsigned int version; 49482a8524SThomas Graf unsigned int maxattr; 50134e6375SJohannes Berg bool netnsok; 51ff4c92d8SJohannes Berg int (*pre_doit)(struct genl_ops *ops, 52ff4c92d8SJohannes Berg struct sk_buff *skb, 53ff4c92d8SJohannes Berg struct genl_info *info); 54ff4c92d8SJohannes Berg void (*post_doit)(struct genl_ops *ops, 55ff4c92d8SJohannes Berg struct sk_buff *skb, 56ff4c92d8SJohannes Berg struct genl_info *info); 57482a8524SThomas Graf struct nlattr ** attrbuf; /* private */ 58482a8524SThomas Graf struct list_head ops_list; /* private */ 59482a8524SThomas Graf struct list_head family_list; /* private */ 602dbba6f7SJohannes Berg struct list_head mcast_groups; /* private */ 61482a8524SThomas Graf }; 62482a8524SThomas Graf 63482a8524SThomas Graf /** 64482a8524SThomas Graf * struct genl_info - receiving information 65482a8524SThomas Graf * @snd_seq: sending sequence number 66482a8524SThomas Graf * @snd_pid: netlink pid of sender 67482a8524SThomas Graf * @nlhdr: netlink message header 68482a8524SThomas Graf * @genlhdr: generic netlink message header 69482a8524SThomas Graf * @userhdr: user specific header 70482a8524SThomas Graf * @attrs: netlink attributes 71ff4c92d8SJohannes Berg * @_net: network namespace 72ff4c92d8SJohannes Berg * @user_ptr: user pointers 73482a8524SThomas Graf */ 74fd2c3ef7SEric Dumazet struct genl_info { 75482a8524SThomas Graf u32 snd_seq; 76482a8524SThomas Graf u32 snd_pid; 77482a8524SThomas Graf struct nlmsghdr * nlhdr; 78482a8524SThomas Graf struct genlmsghdr * genlhdr; 79482a8524SThomas Graf void * userhdr; 80482a8524SThomas Graf struct nlattr ** attrs; 81134e6375SJohannes Berg #ifdef CONFIG_NET_NS 82134e6375SJohannes Berg struct net * _net; 83134e6375SJohannes Berg #endif 84ff4c92d8SJohannes Berg void * user_ptr[2]; 85482a8524SThomas Graf }; 86482a8524SThomas Graf 87134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 88134e6375SJohannes Berg { 89c2d9ba9bSEric Dumazet return read_pnet(&info->_net); 90134e6375SJohannes Berg } 91134e6375SJohannes Berg 92134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 93134e6375SJohannes Berg { 94c2d9ba9bSEric Dumazet write_pnet(&info->_net, net); 95134e6375SJohannes Berg } 96134e6375SJohannes Berg 97482a8524SThomas Graf /** 98482a8524SThomas Graf * struct genl_ops - generic netlink operations 99482a8524SThomas Graf * @cmd: command identifier 100ff4c92d8SJohannes Berg * @internal_flags: flags used by the family 101482a8524SThomas Graf * @flags: flags 102482a8524SThomas Graf * @policy: attribute validation policy 103482a8524SThomas Graf * @doit: standard command callback 104482a8524SThomas Graf * @dumpit: callback for dumpers 105a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 106482a8524SThomas Graf * @ops_list: operations list 107482a8524SThomas Graf */ 108fd2c3ef7SEric Dumazet struct genl_ops { 109b461d2f2SPer Liden u8 cmd; 110ff4c92d8SJohannes Berg u8 internal_flags; 111482a8524SThomas Graf unsigned int flags; 112ef7c79edSPatrick McHardy const struct nla_policy *policy; 113482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 114482a8524SThomas Graf struct genl_info *info); 115482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 116482a8524SThomas Graf struct netlink_callback *cb); 117a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 118482a8524SThomas Graf struct list_head ops_list; 119482a8524SThomas Graf }; 120482a8524SThomas Graf 121482a8524SThomas Graf extern int genl_register_family(struct genl_family *family); 122a7b11d73SMichał Mirosław extern int genl_register_family_with_ops(struct genl_family *family, 123a7b11d73SMichał Mirosław struct genl_ops *ops, size_t n_ops); 124482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family); 125482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops); 126482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops); 1272dbba6f7SJohannes Berg extern int genl_register_mc_group(struct genl_family *family, 1282dbba6f7SJohannes Berg struct genl_multicast_group *grp); 1292dbba6f7SJohannes Berg extern void genl_unregister_mc_group(struct genl_family *family, 1302dbba6f7SJohannes Berg struct genl_multicast_group *grp); 131482a8524SThomas Graf 132482a8524SThomas Graf /** 133482a8524SThomas Graf * genlmsg_put - Add generic netlink header to netlink message 134482a8524SThomas Graf * @skb: socket buffer holding the message 135482a8524SThomas Graf * @pid: netlink pid the message is addressed to 136482a8524SThomas Graf * @seq: sequence number (usually the one of the sender) 13717c157c8SThomas Graf * @family: generic netlink family 138482a8524SThomas Graf * @flags netlink message flags 139482a8524SThomas Graf * @cmd: generic netlink command 140482a8524SThomas Graf * 141482a8524SThomas Graf * Returns pointer to user specific header 142482a8524SThomas Graf */ 143482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, 14417c157c8SThomas Graf struct genl_family *family, int flags, u8 cmd) 145482a8524SThomas Graf { 146482a8524SThomas Graf struct nlmsghdr *nlh; 147482a8524SThomas Graf struct genlmsghdr *hdr; 148482a8524SThomas Graf 14917c157c8SThomas Graf nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN + 15017c157c8SThomas Graf family->hdrsize, flags); 151482a8524SThomas Graf if (nlh == NULL) 152482a8524SThomas Graf return NULL; 153482a8524SThomas Graf 154482a8524SThomas Graf hdr = nlmsg_data(nlh); 155482a8524SThomas Graf hdr->cmd = cmd; 15617c157c8SThomas Graf hdr->version = family->version; 157482a8524SThomas Graf hdr->reserved = 0; 158482a8524SThomas Graf 159482a8524SThomas Graf return (char *) hdr + GENL_HDRLEN; 160482a8524SThomas Graf } 161482a8524SThomas Graf 162482a8524SThomas Graf /** 16317c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 16417c157c8SThomas Graf * @skb: socket buffer holding the message 16517c157c8SThomas Graf * @info: receiver info 16617c157c8SThomas Graf * @family: generic netlink family 16717c157c8SThomas Graf * @flags: netlink message flags 16817c157c8SThomas Graf * @cmd: generic netlink command 16917c157c8SThomas Graf * 17017c157c8SThomas Graf * Returns pointer to user specific header 17117c157c8SThomas Graf */ 17217c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 17317c157c8SThomas Graf struct genl_info *info, 17417c157c8SThomas Graf struct genl_family *family, 17517c157c8SThomas Graf int flags, u8 cmd) 17617c157c8SThomas Graf { 17717c157c8SThomas Graf return genlmsg_put(skb, info->snd_pid, info->snd_seq, family, 17817c157c8SThomas Graf flags, cmd); 17917c157c8SThomas Graf } 18017c157c8SThomas Graf 18117c157c8SThomas Graf /** 182482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 183482a8524SThomas Graf * @skb: socket buffer the message is stored in 184482a8524SThomas Graf * @hdr: user specific header 185482a8524SThomas Graf */ 186482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr) 187482a8524SThomas Graf { 188482a8524SThomas Graf return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 189482a8524SThomas Graf } 190482a8524SThomas Graf 191482a8524SThomas Graf /** 192482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 193482a8524SThomas Graf * @skb: socket buffer the message is stored in 194482a8524SThomas Graf * @hdr: generic netlink message header 195482a8524SThomas Graf */ 196bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 197482a8524SThomas Graf { 198*38db9e1dSJulia Lawall if (hdr) 199bc3ed28cSThomas Graf nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 200482a8524SThomas Graf } 201482a8524SThomas Graf 202482a8524SThomas Graf /** 203134e6375SJohannes Berg * genlmsg_multicast_netns - multicast a netlink message to a specific netns 204134e6375SJohannes Berg * @net: the net namespace 205134e6375SJohannes Berg * @skb: netlink message as socket buffer 206134e6375SJohannes Berg * @pid: own netlink pid to avoid sending to yourself 207134e6375SJohannes Berg * @group: multicast group id 208134e6375SJohannes Berg * @flags: allocation flags 209134e6375SJohannes Berg */ 210134e6375SJohannes Berg static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb, 211134e6375SJohannes Berg u32 pid, unsigned int group, gfp_t flags) 212134e6375SJohannes Berg { 213134e6375SJohannes Berg return nlmsg_multicast(net->genl_sock, skb, pid, group, flags); 214134e6375SJohannes Berg } 215134e6375SJohannes Berg 216134e6375SJohannes Berg /** 217134e6375SJohannes Berg * genlmsg_multicast - multicast a netlink message to the default netns 218482a8524SThomas Graf * @skb: netlink message as socket buffer 219482a8524SThomas Graf * @pid: own netlink pid to avoid sending to yourself 220482a8524SThomas Graf * @group: multicast group id 221d387f6adSThomas Graf * @flags: allocation flags 222482a8524SThomas Graf */ 223482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid, 224d387f6adSThomas Graf unsigned int group, gfp_t flags) 225482a8524SThomas Graf { 226134e6375SJohannes Berg return genlmsg_multicast_netns(&init_net, skb, pid, group, flags); 227482a8524SThomas Graf } 228482a8524SThomas Graf 229482a8524SThomas Graf /** 230134e6375SJohannes Berg * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 231134e6375SJohannes Berg * @skb: netlink message as socket buffer 232134e6375SJohannes Berg * @pid: own netlink pid to avoid sending to yourself 233134e6375SJohannes Berg * @group: multicast group id 234134e6375SJohannes Berg * @flags: allocation flags 235134e6375SJohannes Berg * 236134e6375SJohannes Berg * This function must hold the RTNL or rcu_read_lock(). 237134e6375SJohannes Berg */ 238134e6375SJohannes Berg int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, 239134e6375SJohannes Berg unsigned int group, gfp_t flags); 240134e6375SJohannes Berg 241134e6375SJohannes Berg /** 242482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 243482a8524SThomas Graf * @skb: netlink message as socket buffer 244482a8524SThomas Graf * @pid: netlink pid of the destination socket 245482a8524SThomas Graf */ 246134e6375SJohannes Berg static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid) 247482a8524SThomas Graf { 248134e6375SJohannes Berg return nlmsg_unicast(net->genl_sock, skb, pid); 249482a8524SThomas Graf } 250482a8524SThomas Graf 251fb0ba6bdSBalbir Singh /** 25281878d27SThomas Graf * genlmsg_reply - reply to a request 25381878d27SThomas Graf * @skb: netlink message to be sent back 25481878d27SThomas Graf * @info: receiver information 25581878d27SThomas Graf */ 25681878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 25781878d27SThomas Graf { 258134e6375SJohannes Berg return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); 25981878d27SThomas Graf } 26081878d27SThomas Graf 26181878d27SThomas Graf /** 262fb0ba6bdSBalbir Singh * gennlmsg_data - head of message payload 263fb0ba6bdSBalbir Singh * @gnlh: genetlink messsage header 264fb0ba6bdSBalbir Singh */ 265fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 266fb0ba6bdSBalbir Singh { 267fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 268fb0ba6bdSBalbir Singh } 269fb0ba6bdSBalbir Singh 270fb0ba6bdSBalbir Singh /** 271fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 272fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 273fb0ba6bdSBalbir Singh */ 274fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 275fb0ba6bdSBalbir Singh { 276fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 277fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 278fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 279fb0ba6bdSBalbir Singh } 280fb0ba6bdSBalbir Singh 28117db952cSBalbir Singh /** 28217db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 28317db952cSBalbir Singh * @payload: length of message payload 28417db952cSBalbir Singh */ 28517db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 28617db952cSBalbir Singh { 28717db952cSBalbir Singh return GENL_HDRLEN + payload; 28817db952cSBalbir Singh } 28917db952cSBalbir Singh 29017db952cSBalbir Singh /** 29117db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 29217db952cSBalbir Singh * @payload: length of message payload 29317db952cSBalbir Singh */ 29417db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 29517db952cSBalbir Singh { 29617db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 29717db952cSBalbir Singh } 29817db952cSBalbir Singh 2993dabc715SThomas Graf /** 3003dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 3013dabc715SThomas Graf * @payload: size of the message payload 3023dabc715SThomas Graf * @flags: the type of memory to allocate. 3033dabc715SThomas Graf */ 3043dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 3053dabc715SThomas Graf { 3063dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 3073dabc715SThomas Graf } 3083dabc715SThomas Graf 3093dabc715SThomas Graf 310482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 311