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 858050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN) 958050fceSThomas Graf 10482a8524SThomas Graf /** 112dbba6f7SJohannes Berg * struct genl_multicast_group - generic netlink multicast group 122dbba6f7SJohannes Berg * @name: name of the multicast group, names are per-family 132dbba6f7SJohannes Berg * @id: multicast group ID, assigned by the core, to use with 142dbba6f7SJohannes Berg * genlmsg_multicast(). 152dbba6f7SJohannes Berg * @list: list entry for linking 162dbba6f7SJohannes Berg * @family: pointer to family, need not be set before registering 172dbba6f7SJohannes Berg */ 18fd2c3ef7SEric Dumazet struct genl_multicast_group { 192dbba6f7SJohannes Berg struct genl_family *family; /* private */ 202dbba6f7SJohannes Berg struct list_head list; /* private */ 212dbba6f7SJohannes Berg char name[GENL_NAMSIZ]; 222dbba6f7SJohannes Berg u32 id; 232dbba6f7SJohannes Berg }; 242dbba6f7SJohannes Berg 25ff4c92d8SJohannes Berg struct genl_ops; 26ff4c92d8SJohannes Berg struct genl_info; 27ff4c92d8SJohannes Berg 282dbba6f7SJohannes Berg /** 29482a8524SThomas Graf * struct genl_family - generic netlink family 30482a8524SThomas Graf * @id: protocol family idenfitier 31482a8524SThomas Graf * @hdrsize: length of user specific header in bytes 32482a8524SThomas Graf * @name: name of family 33482a8524SThomas Graf * @version: protocol version 34482a8524SThomas Graf * @maxattr: maximum number of attributes supported 35134e6375SJohannes Berg * @netnsok: set to true if the family can handle network 36134e6375SJohannes Berg * namespaces and should be presented in all of them 37ff4c92d8SJohannes Berg * @pre_doit: called before an operation's doit callback, it may 38ff4c92d8SJohannes Berg * do additional, common, filtering and return an error 39ff4c92d8SJohannes Berg * @post_doit: called after an operation's doit callback, it may 40ff4c92d8SJohannes Berg * undo operations done by pre_doit, for example release locks 41482a8524SThomas Graf * @attrbuf: buffer to store parsed attributes 42482a8524SThomas Graf * @ops_list: list of all assigned operations 43482a8524SThomas Graf * @family_list: family list 442dbba6f7SJohannes Berg * @mcast_groups: multicast groups list 45482a8524SThomas Graf */ 46fd2c3ef7SEric Dumazet struct genl_family { 47482a8524SThomas Graf unsigned int id; 48482a8524SThomas Graf unsigned int hdrsize; 49482a8524SThomas Graf char name[GENL_NAMSIZ]; 50482a8524SThomas Graf unsigned int version; 51482a8524SThomas Graf unsigned int maxattr; 52134e6375SJohannes Berg bool netnsok; 53def31174SPravin B Shelar bool parallel_ops; 54ff4c92d8SJohannes Berg int (*pre_doit)(struct genl_ops *ops, 55ff4c92d8SJohannes Berg struct sk_buff *skb, 56ff4c92d8SJohannes Berg struct genl_info *info); 57ff4c92d8SJohannes Berg void (*post_doit)(struct genl_ops *ops, 58ff4c92d8SJohannes Berg struct sk_buff *skb, 59ff4c92d8SJohannes Berg struct genl_info *info); 60482a8524SThomas Graf struct nlattr ** attrbuf; /* private */ 61482a8524SThomas Graf struct list_head ops_list; /* private */ 62482a8524SThomas Graf struct list_head family_list; /* private */ 632dbba6f7SJohannes Berg struct list_head mcast_groups; /* private */ 6433c6b1f6SPravin B Shelar struct module *module; 65482a8524SThomas Graf }; 66482a8524SThomas Graf 67482a8524SThomas Graf /** 68482a8524SThomas Graf * struct genl_info - receiving information 69482a8524SThomas Graf * @snd_seq: sending sequence number 7015e47304SEric W. Biederman * @snd_portid: netlink portid of sender 71482a8524SThomas Graf * @nlhdr: netlink message header 72482a8524SThomas Graf * @genlhdr: generic netlink message header 73482a8524SThomas Graf * @userhdr: user specific header 74482a8524SThomas Graf * @attrs: netlink attributes 75ff4c92d8SJohannes Berg * @_net: network namespace 76ff4c92d8SJohannes Berg * @user_ptr: user pointers 77482a8524SThomas Graf */ 78fd2c3ef7SEric Dumazet struct genl_info { 79482a8524SThomas Graf u32 snd_seq; 8015e47304SEric W. Biederman u32 snd_portid; 81482a8524SThomas Graf struct nlmsghdr * nlhdr; 82482a8524SThomas Graf struct genlmsghdr * genlhdr; 83482a8524SThomas Graf void * userhdr; 84482a8524SThomas Graf struct nlattr ** attrs; 85134e6375SJohannes Berg #ifdef CONFIG_NET_NS 86134e6375SJohannes Berg struct net * _net; 87134e6375SJohannes Berg #endif 88ff4c92d8SJohannes Berg void * user_ptr[2]; 89482a8524SThomas Graf }; 90482a8524SThomas Graf 91134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 92134e6375SJohannes Berg { 93c2d9ba9bSEric Dumazet return read_pnet(&info->_net); 94134e6375SJohannes Berg } 95134e6375SJohannes Berg 96134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 97134e6375SJohannes Berg { 98c2d9ba9bSEric Dumazet write_pnet(&info->_net, net); 99134e6375SJohannes Berg } 100134e6375SJohannes Berg 101482a8524SThomas Graf /** 102482a8524SThomas Graf * struct genl_ops - generic netlink operations 103482a8524SThomas Graf * @cmd: command identifier 104ff4c92d8SJohannes Berg * @internal_flags: flags used by the family 105482a8524SThomas Graf * @flags: flags 106482a8524SThomas Graf * @policy: attribute validation policy 107482a8524SThomas Graf * @doit: standard command callback 108482a8524SThomas Graf * @dumpit: callback for dumpers 109a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 110482a8524SThomas Graf * @ops_list: operations list 111482a8524SThomas Graf */ 112fd2c3ef7SEric Dumazet struct genl_ops { 113b461d2f2SPer Liden u8 cmd; 114ff4c92d8SJohannes Berg u8 internal_flags; 115482a8524SThomas Graf unsigned int flags; 116ef7c79edSPatrick McHardy const struct nla_policy *policy; 117482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 118482a8524SThomas Graf struct genl_info *info); 119482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 120482a8524SThomas Graf struct netlink_callback *cb); 121a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 122482a8524SThomas Graf struct list_head ops_list; 123482a8524SThomas Graf }; 124482a8524SThomas Graf 12533c6b1f6SPravin B Shelar extern int __genl_register_family(struct genl_family *family); 12633c6b1f6SPravin B Shelar 12733c6b1f6SPravin B Shelar static inline int genl_register_family(struct genl_family *family) 12833c6b1f6SPravin B Shelar { 12933c6b1f6SPravin B Shelar family->module = THIS_MODULE; 13033c6b1f6SPravin B Shelar return __genl_register_family(family); 13133c6b1f6SPravin B Shelar } 13233c6b1f6SPravin B Shelar 13333c6b1f6SPravin B Shelar extern int __genl_register_family_with_ops(struct genl_family *family, 134a7b11d73SMichał Mirosław struct genl_ops *ops, size_t n_ops); 13533c6b1f6SPravin B Shelar 13633c6b1f6SPravin B Shelar static inline int genl_register_family_with_ops(struct genl_family *family, 13733c6b1f6SPravin B Shelar struct genl_ops *ops, size_t n_ops) 13833c6b1f6SPravin B Shelar { 13933c6b1f6SPravin B Shelar family->module = THIS_MODULE; 14033c6b1f6SPravin B Shelar return __genl_register_family_with_ops(family, ops, n_ops); 14133c6b1f6SPravin B Shelar } 14233c6b1f6SPravin B Shelar 143482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family); 144482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops); 145482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops); 1462dbba6f7SJohannes Berg extern int genl_register_mc_group(struct genl_family *family, 1472dbba6f7SJohannes Berg struct genl_multicast_group *grp); 1482dbba6f7SJohannes Berg extern void genl_unregister_mc_group(struct genl_family *family, 1492dbba6f7SJohannes Berg struct genl_multicast_group *grp); 15015e47304SEric W. Biederman extern void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, 151263ba61dSPravin B Shelar u32 group, struct nlmsghdr *nlh, gfp_t flags); 152482a8524SThomas Graf 15315e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 154a46621a3SDenys Vlasenko struct genl_family *family, int flags, u8 cmd); 155482a8524SThomas Graf 156482a8524SThomas Graf /** 157670dc283SJohannes Berg * genlmsg_nlhdr - Obtain netlink header from user specified header 158670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 159670dc283SJohannes Berg * @family: generic netlink family 160670dc283SJohannes Berg * 161670dc283SJohannes Berg * Returns pointer to netlink header. 162670dc283SJohannes Berg */ 163670dc283SJohannes Berg static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr, 164670dc283SJohannes Berg struct genl_family *family) 165670dc283SJohannes Berg { 166670dc283SJohannes Berg return (struct nlmsghdr *)((char *)user_hdr - 167670dc283SJohannes Berg family->hdrsize - 168670dc283SJohannes Berg GENL_HDRLEN - 169670dc283SJohannes Berg NLMSG_HDRLEN); 170670dc283SJohannes Berg } 171670dc283SJohannes Berg 172670dc283SJohannes Berg /** 173670dc283SJohannes Berg * genl_dump_check_consistent - check if sequence is consistent and advertise if not 174670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 175670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 176670dc283SJohannes Berg * @family: generic netlink family 177670dc283SJohannes Berg * 178670dc283SJohannes Berg * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it 179670dc283SJohannes Berg * simpler to use with generic netlink. 180670dc283SJohannes Berg */ 181670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb, 182670dc283SJohannes Berg void *user_hdr, 183670dc283SJohannes Berg struct genl_family *family) 184670dc283SJohannes Berg { 185670dc283SJohannes Berg nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family)); 186670dc283SJohannes Berg } 187670dc283SJohannes Berg 188670dc283SJohannes Berg /** 18917c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 19017c157c8SThomas Graf * @skb: socket buffer holding the message 19117c157c8SThomas Graf * @info: receiver info 19217c157c8SThomas Graf * @family: generic netlink family 19317c157c8SThomas Graf * @flags: netlink message flags 19417c157c8SThomas Graf * @cmd: generic netlink command 19517c157c8SThomas Graf * 19617c157c8SThomas Graf * Returns pointer to user specific header 19717c157c8SThomas Graf */ 19817c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 19917c157c8SThomas Graf struct genl_info *info, 20017c157c8SThomas Graf struct genl_family *family, 20117c157c8SThomas Graf int flags, u8 cmd) 20217c157c8SThomas Graf { 20315e47304SEric W. Biederman return genlmsg_put(skb, info->snd_portid, info->snd_seq, family, 20417c157c8SThomas Graf flags, cmd); 20517c157c8SThomas Graf } 20617c157c8SThomas Graf 20717c157c8SThomas Graf /** 208482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 209482a8524SThomas Graf * @skb: socket buffer the message is stored in 210482a8524SThomas Graf * @hdr: user specific header 211482a8524SThomas Graf */ 212482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr) 213482a8524SThomas Graf { 214482a8524SThomas Graf return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 215482a8524SThomas Graf } 216482a8524SThomas Graf 217482a8524SThomas Graf /** 218482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 219482a8524SThomas Graf * @skb: socket buffer the message is stored in 220482a8524SThomas Graf * @hdr: generic netlink message header 221482a8524SThomas Graf */ 222bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 223482a8524SThomas Graf { 22438db9e1dSJulia Lawall if (hdr) 225bc3ed28cSThomas Graf nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 226482a8524SThomas Graf } 227482a8524SThomas Graf 228482a8524SThomas Graf /** 229134e6375SJohannes Berg * genlmsg_multicast_netns - multicast a netlink message to a specific netns 230134e6375SJohannes Berg * @net: the net namespace 231134e6375SJohannes Berg * @skb: netlink message as socket buffer 23215e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 233134e6375SJohannes Berg * @group: multicast group id 234134e6375SJohannes Berg * @flags: allocation flags 235134e6375SJohannes Berg */ 236134e6375SJohannes Berg static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb, 23715e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 238134e6375SJohannes Berg { 23915e47304SEric W. Biederman return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); 240134e6375SJohannes Berg } 241134e6375SJohannes Berg 242134e6375SJohannes Berg /** 243134e6375SJohannes Berg * genlmsg_multicast - multicast a netlink message to the default netns 244482a8524SThomas Graf * @skb: netlink message as socket buffer 24515e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 246482a8524SThomas Graf * @group: multicast group id 247d387f6adSThomas Graf * @flags: allocation flags 248482a8524SThomas Graf */ 24915e47304SEric W. Biederman static inline int genlmsg_multicast(struct sk_buff *skb, u32 portid, 250d387f6adSThomas Graf unsigned int group, gfp_t flags) 251482a8524SThomas Graf { 25215e47304SEric W. Biederman return genlmsg_multicast_netns(&init_net, skb, portid, group, flags); 253482a8524SThomas Graf } 254482a8524SThomas Graf 255482a8524SThomas Graf /** 256134e6375SJohannes Berg * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 257134e6375SJohannes Berg * @skb: netlink message as socket buffer 25815e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 259134e6375SJohannes Berg * @group: multicast group id 260134e6375SJohannes Berg * @flags: allocation flags 261134e6375SJohannes Berg * 262134e6375SJohannes Berg * This function must hold the RTNL or rcu_read_lock(). 263134e6375SJohannes Berg */ 26415e47304SEric W. Biederman int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, 265134e6375SJohannes Berg unsigned int group, gfp_t flags); 266134e6375SJohannes Berg 267134e6375SJohannes Berg /** 268482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 269482a8524SThomas Graf * @skb: netlink message as socket buffer 27015e47304SEric W. Biederman * @portid: netlink portid of the destination socket 271482a8524SThomas Graf */ 27215e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid) 273482a8524SThomas Graf { 27415e47304SEric W. Biederman return nlmsg_unicast(net->genl_sock, skb, portid); 275482a8524SThomas Graf } 276482a8524SThomas Graf 277fb0ba6bdSBalbir Singh /** 27881878d27SThomas Graf * genlmsg_reply - reply to a request 27981878d27SThomas Graf * @skb: netlink message to be sent back 28081878d27SThomas Graf * @info: receiver information 28181878d27SThomas Graf */ 28281878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 28381878d27SThomas Graf { 28415e47304SEric W. Biederman return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid); 28581878d27SThomas Graf } 28681878d27SThomas Graf 28781878d27SThomas Graf /** 288fb0ba6bdSBalbir Singh * gennlmsg_data - head of message payload 28970f23fd6SJustin P. Mattock * @gnlh: genetlink message header 290fb0ba6bdSBalbir Singh */ 291fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 292fb0ba6bdSBalbir Singh { 293fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 294fb0ba6bdSBalbir Singh } 295fb0ba6bdSBalbir Singh 296fb0ba6bdSBalbir Singh /** 297fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 298fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 299fb0ba6bdSBalbir Singh */ 300fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 301fb0ba6bdSBalbir Singh { 302fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 303fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 304fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 305fb0ba6bdSBalbir Singh } 306fb0ba6bdSBalbir Singh 30717db952cSBalbir Singh /** 30817db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 30917db952cSBalbir Singh * @payload: length of message payload 31017db952cSBalbir Singh */ 31117db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 31217db952cSBalbir Singh { 31317db952cSBalbir Singh return GENL_HDRLEN + payload; 31417db952cSBalbir Singh } 31517db952cSBalbir Singh 31617db952cSBalbir Singh /** 31717db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 31817db952cSBalbir Singh * @payload: length of message payload 31917db952cSBalbir Singh */ 32017db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 32117db952cSBalbir Singh { 32217db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 32317db952cSBalbir Singh } 32417db952cSBalbir Singh 3253dabc715SThomas Graf /** 3263dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 3273dabc715SThomas Graf * @payload: size of the message payload 3283dabc715SThomas Graf * @flags: the type of memory to allocate. 3293dabc715SThomas Graf */ 3303dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 3313dabc715SThomas Graf { 3323dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 3333dabc715SThomas Graf } 3343dabc715SThomas Graf 3353dabc715SThomas Graf 336482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 337