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 */ 14fd2c3ef7SEric Dumazet struct genl_multicast_group { 152dbba6f7SJohannes Berg char name[GENL_NAMSIZ]; 162dbba6f7SJohannes Berg }; 172dbba6f7SJohannes Berg 18ff4c92d8SJohannes Berg struct genl_ops; 19ff4c92d8SJohannes Berg struct genl_info; 20ff4c92d8SJohannes Berg 212dbba6f7SJohannes Berg /** 22482a8524SThomas Graf * struct genl_family - generic netlink family 23a07ea4d9SJohannes Berg * @id: protocol family identifier (private) 24482a8524SThomas Graf * @hdrsize: length of user specific header in bytes 25482a8524SThomas Graf * @name: name of family 26482a8524SThomas Graf * @version: protocol version 27482a8524SThomas Graf * @maxattr: maximum number of attributes supported 28134e6375SJohannes Berg * @netnsok: set to true if the family can handle network 29134e6375SJohannes Berg * namespaces and should be presented in all of them 30f555f3d7SJohannes Berg * @parallel_ops: operations can be called in parallel and aren't 31f555f3d7SJohannes Berg * synchronized by the core genetlink code 32ff4c92d8SJohannes Berg * @pre_doit: called before an operation's doit callback, it may 33ff4c92d8SJohannes Berg * do additional, common, filtering and return an error 34ff4c92d8SJohannes Berg * @post_doit: called after an operation's doit callback, it may 35ff4c92d8SJohannes Berg * undo operations done by pre_doit, for example release locks 36c380d9a7SJohannes Berg * @mcast_bind: a socket bound to the given multicast group (which 37c380d9a7SJohannes Berg * is given as the offset into the groups array) 38ee1c2442SJohannes Berg * @mcast_unbind: a socket was unbound from the given multicast group. 39ee1c2442SJohannes Berg * Note that unbind() will not be called symmetrically if the 40ee1c2442SJohannes Berg * generic netlink family is removed while there are still open 41ee1c2442SJohannes Berg * sockets. 42*489111e5SJohannes Berg * @attrbuf: buffer to store parsed attributes (private) 43*489111e5SJohannes Berg * @family_list: family list (private) 44*489111e5SJohannes Berg * @mcgrps: multicast groups used by this family 45*489111e5SJohannes Berg * @n_mcgrps: number of multicast groups 462a94fe48SJohannes Berg * @mcgrp_offset: starting number of multicast group IDs in this family 47*489111e5SJohannes Berg * (private) 48*489111e5SJohannes Berg * @ops: the operations supported by this family 49*489111e5SJohannes Berg * @n_ops: number of operations supported by this family 50482a8524SThomas Graf */ 51fd2c3ef7SEric Dumazet struct genl_family { 52a07ea4d9SJohannes Berg unsigned int id; /* private */ 53482a8524SThomas Graf unsigned int hdrsize; 54482a8524SThomas Graf char name[GENL_NAMSIZ]; 55482a8524SThomas Graf unsigned int version; 56482a8524SThomas Graf unsigned int maxattr; 57134e6375SJohannes Berg bool netnsok; 58def31174SPravin B Shelar bool parallel_ops; 59f84f771dSJohannes Berg int (*pre_doit)(const struct genl_ops *ops, 60ff4c92d8SJohannes Berg struct sk_buff *skb, 61ff4c92d8SJohannes Berg struct genl_info *info); 62f84f771dSJohannes Berg void (*post_doit)(const struct genl_ops *ops, 63ff4c92d8SJohannes Berg struct sk_buff *skb, 64ff4c92d8SJohannes Berg struct genl_info *info); 65023e2cfaSJohannes Berg int (*mcast_bind)(struct net *net, int group); 66023e2cfaSJohannes Berg void (*mcast_unbind)(struct net *net, int group); 67482a8524SThomas Graf struct nlattr ** attrbuf; /* private */ 68*489111e5SJohannes Berg const struct genl_ops * ops; 69*489111e5SJohannes Berg const struct genl_multicast_group *mcgrps; 70*489111e5SJohannes Berg unsigned int n_ops; 71*489111e5SJohannes Berg unsigned int n_mcgrps; 722a94fe48SJohannes Berg unsigned int mcgrp_offset; /* private */ 73482a8524SThomas Graf struct list_head family_list; /* private */ 7433c6b1f6SPravin B Shelar struct module *module; 75482a8524SThomas Graf }; 76482a8524SThomas Graf 77c90c39daSJohannes Berg struct nlattr **genl_family_attrbuf(struct genl_family *family); 78c90c39daSJohannes Berg 79482a8524SThomas Graf /** 80482a8524SThomas Graf * struct genl_info - receiving information 81482a8524SThomas Graf * @snd_seq: sending sequence number 8215e47304SEric W. Biederman * @snd_portid: netlink portid of sender 83482a8524SThomas Graf * @nlhdr: netlink message header 84482a8524SThomas Graf * @genlhdr: generic netlink message header 85482a8524SThomas Graf * @userhdr: user specific header 86482a8524SThomas Graf * @attrs: netlink attributes 87ff4c92d8SJohannes Berg * @_net: network namespace 88ff4c92d8SJohannes Berg * @user_ptr: user pointers 89482a8524SThomas Graf */ 90fd2c3ef7SEric Dumazet struct genl_info { 91482a8524SThomas Graf u32 snd_seq; 9215e47304SEric W. Biederman u32 snd_portid; 93482a8524SThomas Graf struct nlmsghdr * nlhdr; 94482a8524SThomas Graf struct genlmsghdr * genlhdr; 95482a8524SThomas Graf void * userhdr; 96482a8524SThomas Graf struct nlattr ** attrs; 970c5c9fb5SEric W. Biederman possible_net_t _net; 98ff4c92d8SJohannes Berg void * user_ptr[2]; 99482a8524SThomas Graf }; 100482a8524SThomas Graf 101134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 102134e6375SJohannes Berg { 103c2d9ba9bSEric Dumazet return read_pnet(&info->_net); 104134e6375SJohannes Berg } 105134e6375SJohannes Berg 106134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 107134e6375SJohannes Berg { 108c2d9ba9bSEric Dumazet write_pnet(&info->_net, net); 109134e6375SJohannes Berg } 110134e6375SJohannes Berg 111482a8524SThomas Graf /** 112482a8524SThomas Graf * struct genl_ops - generic netlink operations 113482a8524SThomas Graf * @cmd: command identifier 114ff4c92d8SJohannes Berg * @internal_flags: flags used by the family 115482a8524SThomas Graf * @flags: flags 116482a8524SThomas Graf * @policy: attribute validation policy 117482a8524SThomas Graf * @doit: standard command callback 118fc9e50f5STom Herbert * @start: start callback for dumps 119482a8524SThomas Graf * @dumpit: callback for dumpers 120a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 121482a8524SThomas Graf * @ops_list: operations list 122482a8524SThomas Graf */ 123fd2c3ef7SEric Dumazet struct genl_ops { 124ef7c79edSPatrick McHardy const struct nla_policy *policy; 125482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 126482a8524SThomas Graf struct genl_info *info); 127fc9e50f5STom Herbert int (*start)(struct netlink_callback *cb); 128482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 129482a8524SThomas Graf struct netlink_callback *cb); 130a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 1313f5ccd06SJohannes Berg u8 cmd; 1323f5ccd06SJohannes Berg u8 internal_flags; 1333f5ccd06SJohannes Berg u8 flags; 134482a8524SThomas Graf }; 135482a8524SThomas Graf 136*489111e5SJohannes Berg int genl_register_family(struct genl_family *family); 137ff2b94d2SJoe Perches int genl_unregister_family(struct genl_family *family); 13892c14d9bSJiri Benc void genl_notify(struct genl_family *family, struct sk_buff *skb, 13992c14d9bSJiri Benc struct genl_info *info, u32 group, gfp_t flags); 140482a8524SThomas Graf 14115e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 142a46621a3SDenys Vlasenko struct genl_family *family, int flags, u8 cmd); 143482a8524SThomas Graf 144482a8524SThomas Graf /** 145670dc283SJohannes Berg * genlmsg_nlhdr - Obtain netlink header from user specified header 146670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 147670dc283SJohannes Berg * @family: generic netlink family 148670dc283SJohannes Berg * 149670dc283SJohannes Berg * Returns pointer to netlink header. 150670dc283SJohannes Berg */ 151670dc283SJohannes Berg static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr, 152670dc283SJohannes Berg struct genl_family *family) 153670dc283SJohannes Berg { 154670dc283SJohannes Berg return (struct nlmsghdr *)((char *)user_hdr - 155670dc283SJohannes Berg family->hdrsize - 156670dc283SJohannes Berg GENL_HDRLEN - 157670dc283SJohannes Berg NLMSG_HDRLEN); 158670dc283SJohannes Berg } 159670dc283SJohannes Berg 160670dc283SJohannes Berg /** 1617b1883ceSJoe Stringer * genlmsg_parse - parse attributes of a genetlink message 1627b1883ceSJoe Stringer * @nlh: netlink message header 1637b1883ceSJoe Stringer * @family: genetlink message family 1647b1883ceSJoe Stringer * @tb: destination array with maxtype+1 elements 1657b1883ceSJoe Stringer * @maxtype: maximum attribute type to be expected 1667b1883ceSJoe Stringer * @policy: validation policy 1677b1883ceSJoe Stringer * */ 1687b1883ceSJoe Stringer static inline int genlmsg_parse(const struct nlmsghdr *nlh, 1697b1883ceSJoe Stringer const struct genl_family *family, 1707b1883ceSJoe Stringer struct nlattr *tb[], int maxtype, 1717b1883ceSJoe Stringer const struct nla_policy *policy) 1727b1883ceSJoe Stringer { 1737b1883ceSJoe Stringer return nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, 1747b1883ceSJoe Stringer policy); 1757b1883ceSJoe Stringer } 1767b1883ceSJoe Stringer 1777b1883ceSJoe Stringer /** 178670dc283SJohannes Berg * genl_dump_check_consistent - check if sequence is consistent and advertise if not 179670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 180670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 181670dc283SJohannes Berg * @family: generic netlink family 182670dc283SJohannes Berg * 183670dc283SJohannes Berg * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it 184670dc283SJohannes Berg * simpler to use with generic netlink. 185670dc283SJohannes Berg */ 186670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb, 187670dc283SJohannes Berg void *user_hdr, 188670dc283SJohannes Berg struct genl_family *family) 189670dc283SJohannes Berg { 190670dc283SJohannes Berg nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family)); 191670dc283SJohannes Berg } 192670dc283SJohannes Berg 193670dc283SJohannes Berg /** 19417c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 19517c157c8SThomas Graf * @skb: socket buffer holding the message 19617c157c8SThomas Graf * @info: receiver info 19717c157c8SThomas Graf * @family: generic netlink family 19817c157c8SThomas Graf * @flags: netlink message flags 19917c157c8SThomas Graf * @cmd: generic netlink command 20017c157c8SThomas Graf * 20117c157c8SThomas Graf * Returns pointer to user specific header 20217c157c8SThomas Graf */ 20317c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 20417c157c8SThomas Graf struct genl_info *info, 20517c157c8SThomas Graf struct genl_family *family, 20617c157c8SThomas Graf int flags, u8 cmd) 20717c157c8SThomas Graf { 20815e47304SEric W. Biederman return genlmsg_put(skb, info->snd_portid, info->snd_seq, family, 20917c157c8SThomas Graf flags, cmd); 21017c157c8SThomas Graf } 21117c157c8SThomas Graf 21217c157c8SThomas Graf /** 213482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 214482a8524SThomas Graf * @skb: socket buffer the message is stored in 215482a8524SThomas Graf * @hdr: user specific header 216482a8524SThomas Graf */ 217053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr) 218482a8524SThomas Graf { 219053c095aSJohannes Berg nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 220482a8524SThomas Graf } 221482a8524SThomas Graf 222482a8524SThomas Graf /** 223482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 224482a8524SThomas Graf * @skb: socket buffer the message is stored in 225482a8524SThomas Graf * @hdr: generic netlink message header 226482a8524SThomas Graf */ 227bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 228482a8524SThomas Graf { 22938db9e1dSJulia Lawall if (hdr) 230bc3ed28cSThomas Graf nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 231482a8524SThomas Graf } 232482a8524SThomas Graf 233482a8524SThomas Graf /** 234134e6375SJohannes Berg * genlmsg_multicast_netns - multicast a netlink message to a specific netns 23568eb5503SJohannes Berg * @family: the generic netlink family 236134e6375SJohannes Berg * @net: the net namespace 237134e6375SJohannes Berg * @skb: netlink message as socket buffer 23815e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 2392a94fe48SJohannes Berg * @group: offset of multicast group in groups array 240134e6375SJohannes Berg * @flags: allocation flags 241134e6375SJohannes Berg */ 24268eb5503SJohannes Berg static inline int genlmsg_multicast_netns(struct genl_family *family, 24368eb5503SJohannes Berg struct net *net, struct sk_buff *skb, 24415e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 245134e6375SJohannes Berg { 246220815a9SJohannes Berg if (WARN_ON_ONCE(group >= family->n_mcgrps)) 2472a94fe48SJohannes Berg return -EINVAL; 2482a94fe48SJohannes Berg group = family->mcgrp_offset + group; 24915e47304SEric W. Biederman return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); 250134e6375SJohannes Berg } 251134e6375SJohannes Berg 252134e6375SJohannes Berg /** 253134e6375SJohannes Berg * genlmsg_multicast - multicast a netlink message to the default netns 25468eb5503SJohannes Berg * @family: the generic netlink family 255482a8524SThomas Graf * @skb: netlink message as socket buffer 25615e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 2572a94fe48SJohannes Berg * @group: offset of multicast group in groups array 258d387f6adSThomas Graf * @flags: allocation flags 259482a8524SThomas Graf */ 26068eb5503SJohannes Berg static inline int genlmsg_multicast(struct genl_family *family, 26168eb5503SJohannes Berg struct sk_buff *skb, u32 portid, 262d387f6adSThomas Graf unsigned int group, gfp_t flags) 263482a8524SThomas Graf { 26468eb5503SJohannes Berg return genlmsg_multicast_netns(family, &init_net, skb, 26568eb5503SJohannes Berg portid, group, flags); 266482a8524SThomas Graf } 267482a8524SThomas Graf 268482a8524SThomas Graf /** 269134e6375SJohannes Berg * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 27068eb5503SJohannes Berg * @family: the generic netlink family 271134e6375SJohannes Berg * @skb: netlink message as socket buffer 27215e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 2732a94fe48SJohannes Berg * @group: offset of multicast group in groups array 274134e6375SJohannes Berg * @flags: allocation flags 275134e6375SJohannes Berg * 276134e6375SJohannes Berg * This function must hold the RTNL or rcu_read_lock(). 277134e6375SJohannes Berg */ 27868eb5503SJohannes Berg int genlmsg_multicast_allns(struct genl_family *family, 27968eb5503SJohannes Berg struct sk_buff *skb, u32 portid, 280134e6375SJohannes Berg unsigned int group, gfp_t flags); 281134e6375SJohannes Berg 282134e6375SJohannes Berg /** 283482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 284482a8524SThomas Graf * @skb: netlink message as socket buffer 28515e47304SEric W. Biederman * @portid: netlink portid of the destination socket 286482a8524SThomas Graf */ 28715e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid) 288482a8524SThomas Graf { 28915e47304SEric W. Biederman return nlmsg_unicast(net->genl_sock, skb, portid); 290482a8524SThomas Graf } 291482a8524SThomas Graf 292fb0ba6bdSBalbir Singh /** 29381878d27SThomas Graf * genlmsg_reply - reply to a request 29481878d27SThomas Graf * @skb: netlink message to be sent back 29581878d27SThomas Graf * @info: receiver information 29681878d27SThomas Graf */ 29781878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 29881878d27SThomas Graf { 29915e47304SEric W. Biederman return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid); 30081878d27SThomas Graf } 30181878d27SThomas Graf 30281878d27SThomas Graf /** 303fb0ba6bdSBalbir Singh * gennlmsg_data - head of message payload 30470f23fd6SJustin P. Mattock * @gnlh: genetlink message header 305fb0ba6bdSBalbir Singh */ 306fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 307fb0ba6bdSBalbir Singh { 308fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 309fb0ba6bdSBalbir Singh } 310fb0ba6bdSBalbir Singh 311fb0ba6bdSBalbir Singh /** 312fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 313fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 314fb0ba6bdSBalbir Singh */ 315fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 316fb0ba6bdSBalbir Singh { 317fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 318fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 319fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 320fb0ba6bdSBalbir Singh } 321fb0ba6bdSBalbir Singh 32217db952cSBalbir Singh /** 32317db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 32417db952cSBalbir Singh * @payload: length of message payload 32517db952cSBalbir Singh */ 32617db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 32717db952cSBalbir Singh { 32817db952cSBalbir Singh return GENL_HDRLEN + payload; 32917db952cSBalbir Singh } 33017db952cSBalbir Singh 33117db952cSBalbir Singh /** 33217db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 33317db952cSBalbir Singh * @payload: length of message payload 33417db952cSBalbir Singh */ 33517db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 33617db952cSBalbir Singh { 33717db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 33817db952cSBalbir Singh } 33917db952cSBalbir Singh 3403dabc715SThomas Graf /** 3413dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 3423dabc715SThomas Graf * @payload: size of the message payload 3433dabc715SThomas Graf * @flags: the type of memory to allocate. 3443dabc715SThomas Graf */ 3453dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 3463dabc715SThomas Graf { 3473dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 3483dabc715SThomas Graf } 3493dabc715SThomas Graf 35062b68e99SJohannes Berg /** 35162b68e99SJohannes Berg * genl_set_err - report error to genetlink broadcast listeners 35268eb5503SJohannes Berg * @family: the generic netlink family 35362b68e99SJohannes Berg * @net: the network namespace to report the error to 35462b68e99SJohannes Berg * @portid: the PORTID of a process that we want to skip (if any) 35562b68e99SJohannes Berg * @group: the broadcast group that will notice the error 3562a94fe48SJohannes Berg * (this is the offset of the multicast group in the groups array) 35762b68e99SJohannes Berg * @code: error code, must be negative (as usual in kernelspace) 35862b68e99SJohannes Berg * 35962b68e99SJohannes Berg * This function returns the number of broadcast listeners that have set the 36062b68e99SJohannes Berg * NETLINK_RECV_NO_ENOBUFS socket option. 36162b68e99SJohannes Berg */ 36268eb5503SJohannes Berg static inline int genl_set_err(struct genl_family *family, struct net *net, 36368eb5503SJohannes Berg u32 portid, u32 group, int code) 36462b68e99SJohannes Berg { 36591398a09SJohannes Berg if (WARN_ON_ONCE(group >= family->n_mcgrps)) 36691398a09SJohannes Berg return -EINVAL; 36791398a09SJohannes Berg group = family->mcgrp_offset + group; 36862b68e99SJohannes Berg return netlink_set_err(net->genl_sock, portid, group, code); 36962b68e99SJohannes Berg } 3703dabc715SThomas Graf 3710d566379SNicolas Dichtel static inline int genl_has_listeners(struct genl_family *family, 372f8403a2eSJohannes Berg struct net *net, unsigned int group) 3730d566379SNicolas Dichtel { 3740d566379SNicolas Dichtel if (WARN_ON_ONCE(group >= family->n_mcgrps)) 3750d566379SNicolas Dichtel return -EINVAL; 3760d566379SNicolas Dichtel group = family->mcgrp_offset + group; 377f8403a2eSJohannes Berg return netlink_has_listeners(net->genl_sock, group); 3780d566379SNicolas Dichtel } 379482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 380