1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H 3482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H 4482a8524SThomas Graf 5482a8524SThomas Graf #include <linux/genetlink.h> 6482a8524SThomas Graf #include <net/netlink.h> 7134e6375SJohannes Berg #include <net/net_namespace.h> 8482a8524SThomas Graf 958050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN) 1058050fceSThomas Graf 11482a8524SThomas Graf /** 122dbba6f7SJohannes Berg * struct genl_multicast_group - generic netlink multicast group 132dbba6f7SJohannes Berg * @name: name of the multicast group, names are per-family 145c221f0aSJakub Kicinski * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM) 152dbba6f7SJohannes Berg */ 16fd2c3ef7SEric Dumazet struct genl_multicast_group { 172dbba6f7SJohannes Berg char name[GENL_NAMSIZ]; 184d54cc32SFlorian Westphal u8 flags; 192dbba6f7SJohannes Berg }; 202dbba6f7SJohannes Berg 21ff4c92d8SJohannes Berg struct genl_ops; 22ff4c92d8SJohannes Berg struct genl_info; 23ff4c92d8SJohannes Berg 242dbba6f7SJohannes Berg /** 25482a8524SThomas Graf * struct genl_family - generic netlink family 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 303b0f31f2SJohannes Berg * @policy: netlink policy 31134e6375SJohannes Berg * @netnsok: set to true if the family can handle network 32134e6375SJohannes Berg * namespaces and should be presented in all of them 33f555f3d7SJohannes Berg * @parallel_ops: operations can be called in parallel and aren't 34f555f3d7SJohannes Berg * synchronized by the core genetlink code 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 39a1a824f4SJakub Kicinski * @module: pointer to the owning module (set to THIS_MODULE) 40489111e5SJohannes Berg * @mcgrps: multicast groups used by this family 41489111e5SJohannes Berg * @n_mcgrps: number of multicast groups 429c5d03d3SJakub Kicinski * @resv_start_op: first operation for which reserved fields of the header 434fa86555SJakub Kicinski * can be validated and policies are required (see below); 444fa86555SJakub Kicinski * new families should leave this field at zero 45489111e5SJohannes Berg * @ops: the operations supported by this family 46489111e5SJohannes Berg * @n_ops: number of operations supported by this family 470b588afdSJakub Kicinski * @small_ops: the small-struct operations supported by this family 480b588afdSJakub Kicinski * @n_small_ops: number of small-struct operations supported by this family 494fa86555SJakub Kicinski * 504fa86555SJakub Kicinski * Attribute policies (the combination of @policy and @maxattr fields) 514fa86555SJakub Kicinski * can be attached at the family level or at the operation level. 524fa86555SJakub Kicinski * If both are present the per-operation policy takes precedence. 534fa86555SJakub Kicinski * For operations before @resv_start_op lack of policy means that the core 544fa86555SJakub Kicinski * will perform no attribute parsing or validation. For newer operations 554fa86555SJakub Kicinski * if policy is not provided core will reject all TLV attributes. 56482a8524SThomas Graf */ 57fd2c3ef7SEric Dumazet struct genl_family { 58482a8524SThomas Graf unsigned int hdrsize; 59482a8524SThomas Graf char name[GENL_NAMSIZ]; 60482a8524SThomas Graf unsigned int version; 61482a8524SThomas Graf unsigned int maxattr; 62e5086736SJakub Kicinski u8 netnsok:1; 63e5086736SJakub Kicinski u8 parallel_ops:1; 64e5086736SJakub Kicinski u8 n_ops; 650b588afdSJakub Kicinski u8 n_small_ops; 66e5086736SJakub Kicinski u8 n_mcgrps; 679c5d03d3SJakub Kicinski u8 resv_start_op; 683b0f31f2SJohannes Berg const struct nla_policy *policy; 69f84f771dSJohannes Berg int (*pre_doit)(const struct genl_ops *ops, 70ff4c92d8SJohannes Berg struct sk_buff *skb, 71ff4c92d8SJohannes Berg struct genl_info *info); 72f84f771dSJohannes Berg void (*post_doit)(const struct genl_ops *ops, 73ff4c92d8SJohannes Berg struct sk_buff *skb, 74ff4c92d8SJohannes Berg struct genl_info *info); 75489111e5SJohannes Berg const struct genl_ops * ops; 760b588afdSJakub Kicinski const struct genl_small_ops *small_ops; 77489111e5SJohannes Berg const struct genl_multicast_group *mcgrps; 7833c6b1f6SPravin B Shelar struct module *module; 79*7c3eaa02SJakub Kicinski 80*7c3eaa02SJakub Kicinski /* private: internal use only */ 81*7c3eaa02SJakub Kicinski /* protocol family identifier */ 82*7c3eaa02SJakub Kicinski int id; 83*7c3eaa02SJakub Kicinski /* starting number of multicast group IDs in this family */ 84*7c3eaa02SJakub Kicinski unsigned int mcgrp_offset; 85482a8524SThomas Graf }; 86482a8524SThomas Graf 87482a8524SThomas Graf /** 88482a8524SThomas Graf * struct genl_info - receiving information 89482a8524SThomas Graf * @snd_seq: sending sequence number 9015e47304SEric W. Biederman * @snd_portid: netlink portid of sender 91482a8524SThomas Graf * @nlhdr: netlink message header 92482a8524SThomas Graf * @genlhdr: generic netlink message header 93482a8524SThomas Graf * @userhdr: user specific header 94482a8524SThomas Graf * @attrs: netlink attributes 95ff4c92d8SJohannes Berg * @_net: network namespace 96ff4c92d8SJohannes Berg * @user_ptr: user pointers 977ab606d1SJohannes Berg * @extack: extended ACK report struct 98482a8524SThomas Graf */ 99fd2c3ef7SEric Dumazet struct genl_info { 100482a8524SThomas Graf u32 snd_seq; 10115e47304SEric W. Biederman u32 snd_portid; 102482a8524SThomas Graf struct nlmsghdr * nlhdr; 103482a8524SThomas Graf struct genlmsghdr * genlhdr; 104482a8524SThomas Graf void * userhdr; 105482a8524SThomas Graf struct nlattr ** attrs; 1060c5c9fb5SEric W. Biederman possible_net_t _net; 107ff4c92d8SJohannes Berg void * user_ptr[2]; 1087ab606d1SJohannes Berg struct netlink_ext_ack *extack; 109482a8524SThomas Graf }; 110482a8524SThomas Graf 111134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info) 112134e6375SJohannes Berg { 113c2d9ba9bSEric Dumazet return read_pnet(&info->_net); 114134e6375SJohannes Berg } 115134e6375SJohannes Berg 116134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net) 117134e6375SJohannes Berg { 118c2d9ba9bSEric Dumazet write_pnet(&info->_net, net); 119134e6375SJohannes Berg } 120134e6375SJohannes Berg 1217ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg) 1227ab606d1SJohannes Berg 12345dca157SJakub Kicinski /* Report that a root attribute is missing */ 12445dca157SJakub Kicinski #define GENL_REQ_ATTR_CHECK(info, attr) ({ \ 12545dca157SJakub Kicinski struct genl_info *__info = (info); \ 12645dca157SJakub Kicinski \ 12745dca157SJakub Kicinski NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \ 12845dca157SJakub Kicinski }) 12945dca157SJakub Kicinski 130ef6243acSJohannes Berg enum genl_validate_flags { 131ef6243acSJohannes Berg GENL_DONT_VALIDATE_STRICT = BIT(0), 132ef6243acSJohannes Berg GENL_DONT_VALIDATE_DUMP = BIT(1), 133ef6243acSJohannes Berg GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2), 134ef6243acSJohannes Berg }; 135ef6243acSJohannes Berg 136482a8524SThomas Graf /** 1370b588afdSJakub Kicinski * struct genl_small_ops - generic netlink operations (small version) 1380b588afdSJakub Kicinski * @cmd: command identifier 1390b588afdSJakub Kicinski * @internal_flags: flags used by the family 1405c221f0aSJakub Kicinski * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM) 1410b588afdSJakub Kicinski * @validate: validation flags from enum genl_validate_flags 1420b588afdSJakub Kicinski * @doit: standard command callback 1430b588afdSJakub Kicinski * @dumpit: callback for dumpers 1440b588afdSJakub Kicinski * 1450b588afdSJakub Kicinski * This is a cut-down version of struct genl_ops for users who don't need 1460b588afdSJakub Kicinski * most of the ancillary infra and want to save space. 1471927f41aSJiri Pirko */ 1480b588afdSJakub Kicinski struct genl_small_ops { 1490b588afdSJakub Kicinski int (*doit)(struct sk_buff *skb, struct genl_info *info); 1500b588afdSJakub Kicinski int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb); 1510b588afdSJakub Kicinski u8 cmd; 1520b588afdSJakub Kicinski u8 internal_flags; 1530b588afdSJakub Kicinski u8 flags; 1540b588afdSJakub Kicinski u8 validate; 1551927f41aSJiri Pirko }; 1561927f41aSJiri Pirko 1571927f41aSJiri Pirko /** 158482a8524SThomas Graf * struct genl_ops - generic netlink operations 159482a8524SThomas Graf * @cmd: command identifier 160ff4c92d8SJohannes Berg * @internal_flags: flags used by the family 1615c221f0aSJakub Kicinski * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM) 16248526a0fSJakub Kicinski * @maxattr: maximum number of attributes supported 16348526a0fSJakub Kicinski * @policy: netlink policy (takes precedence over family policy) 1643ddf9b43SJakub Kicinski * @validate: validation flags from enum genl_validate_flags 165482a8524SThomas Graf * @doit: standard command callback 166fc9e50f5STom Herbert * @start: start callback for dumps 167482a8524SThomas Graf * @dumpit: callback for dumpers 168a4d1366dSJamal Hadi Salim * @done: completion callback for dumps 169482a8524SThomas Graf */ 170fd2c3ef7SEric Dumazet struct genl_ops { 171482a8524SThomas Graf int (*doit)(struct sk_buff *skb, 172482a8524SThomas Graf struct genl_info *info); 173fc9e50f5STom Herbert int (*start)(struct netlink_callback *cb); 174482a8524SThomas Graf int (*dumpit)(struct sk_buff *skb, 175482a8524SThomas Graf struct netlink_callback *cb); 176a4d1366dSJamal Hadi Salim int (*done)(struct netlink_callback *cb); 17748526a0fSJakub Kicinski const struct nla_policy *policy; 17848526a0fSJakub Kicinski unsigned int maxattr; 1793f5ccd06SJohannes Berg u8 cmd; 1803f5ccd06SJohannes Berg u8 internal_flags; 1813f5ccd06SJohannes Berg u8 flags; 182ef6243acSJohannes Berg u8 validate; 183482a8524SThomas Graf }; 184482a8524SThomas Graf 1850b588afdSJakub Kicinski /** 186a1a824f4SJakub Kicinski * struct genl_dumpit_info - info that is available during dumpit op call 1870b588afdSJakub Kicinski * @family: generic netlink family - for internal genl code usage 188a1a824f4SJakub Kicinski * @op: generic netlink ops - for internal genl code usage 1890b588afdSJakub Kicinski * @attrs: netlink attributes 1900b588afdSJakub Kicinski */ 1910b588afdSJakub Kicinski struct genl_dumpit_info { 1920b588afdSJakub Kicinski const struct genl_family *family; 1930b588afdSJakub Kicinski struct genl_ops op; 1940b588afdSJakub Kicinski struct nlattr **attrs; 1950b588afdSJakub Kicinski }; 1960b588afdSJakub Kicinski 1970b588afdSJakub Kicinski static inline const struct genl_dumpit_info * 1980b588afdSJakub Kicinski genl_dumpit_info(struct netlink_callback *cb) 1990b588afdSJakub Kicinski { 2000b588afdSJakub Kicinski return cb->data; 2010b588afdSJakub Kicinski } 2020b588afdSJakub Kicinski 203489111e5SJohannes Berg int genl_register_family(struct genl_family *family); 2042ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family); 2052ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb, 20692c14d9bSJiri Benc struct genl_info *info, u32 group, gfp_t flags); 207482a8524SThomas Graf 20815e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 2092ae0f17dSJohannes Berg const struct genl_family *family, int flags, u8 cmd); 210482a8524SThomas Graf 211482a8524SThomas Graf /** 212670dc283SJohannes Berg * genlmsg_nlhdr - Obtain netlink header from user specified header 213670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 214670dc283SJohannes Berg * 215670dc283SJohannes Berg * Returns pointer to netlink header. 216670dc283SJohannes Berg */ 2170a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr) 218670dc283SJohannes Berg { 219670dc283SJohannes Berg return (struct nlmsghdr *)((char *)user_hdr - 220670dc283SJohannes Berg GENL_HDRLEN - 221670dc283SJohannes Berg NLMSG_HDRLEN); 222670dc283SJohannes Berg } 223670dc283SJohannes Berg 224670dc283SJohannes Berg /** 2258cb08174SJohannes Berg * genlmsg_parse_deprecated - parse attributes of a genetlink message 2267b1883ceSJoe Stringer * @nlh: netlink message header 2277b1883ceSJoe Stringer * @family: genetlink message family 2287b1883ceSJoe Stringer * @tb: destination array with maxtype+1 elements 2297b1883ceSJoe Stringer * @maxtype: maximum attribute type to be expected 2307b1883ceSJoe Stringer * @policy: validation policy 231fceb6435SJohannes Berg * @extack: extended ACK report struct 232fceb6435SJohannes Berg */ 2338cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh, 2347b1883ceSJoe Stringer const struct genl_family *family, 2357b1883ceSJoe Stringer struct nlattr *tb[], int maxtype, 236fceb6435SJohannes Berg const struct nla_policy *policy, 237fceb6435SJohannes Berg struct netlink_ext_ack *extack) 2387b1883ceSJoe Stringer { 2398cb08174SJohannes Berg return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, 2408cb08174SJohannes Berg policy, NL_VALIDATE_LIBERAL, extack); 2417b1883ceSJoe Stringer } 2427b1883ceSJoe Stringer 2437b1883ceSJoe Stringer /** 2443de64403SJohannes Berg * genlmsg_parse - parse attributes of a genetlink message 2453de64403SJohannes Berg * @nlh: netlink message header 2463de64403SJohannes Berg * @family: genetlink message family 2473de64403SJohannes Berg * @tb: destination array with maxtype+1 elements 2483de64403SJohannes Berg * @maxtype: maximum attribute type to be expected 2493de64403SJohannes Berg * @policy: validation policy 2503de64403SJohannes Berg * @extack: extended ACK report struct 2513de64403SJohannes Berg */ 2523de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh, 2533de64403SJohannes Berg const struct genl_family *family, 2543de64403SJohannes Berg struct nlattr *tb[], int maxtype, 2553de64403SJohannes Berg const struct nla_policy *policy, 2563de64403SJohannes Berg struct netlink_ext_ack *extack) 2573de64403SJohannes Berg { 2583de64403SJohannes Berg return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, 2593de64403SJohannes Berg policy, NL_VALIDATE_STRICT, extack); 2603de64403SJohannes Berg } 2613de64403SJohannes Berg 2623de64403SJohannes Berg /** 263670dc283SJohannes Berg * genl_dump_check_consistent - check if sequence is consistent and advertise if not 264670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 265670dc283SJohannes Berg * @user_hdr: user header as returned from genlmsg_put() 266670dc283SJohannes Berg * 267670dc283SJohannes Berg * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it 268670dc283SJohannes Berg * simpler to use with generic netlink. 269670dc283SJohannes Berg */ 270670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb, 2710a833c29SMichal Kubecek void *user_hdr) 272670dc283SJohannes Berg { 2730a833c29SMichal Kubecek nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr)); 274670dc283SJohannes Berg } 275670dc283SJohannes Berg 276670dc283SJohannes Berg /** 27717c157c8SThomas Graf * genlmsg_put_reply - Add generic netlink header to a reply message 27817c157c8SThomas Graf * @skb: socket buffer holding the message 27917c157c8SThomas Graf * @info: receiver info 28017c157c8SThomas Graf * @family: generic netlink family 28117c157c8SThomas Graf * @flags: netlink message flags 28217c157c8SThomas Graf * @cmd: generic netlink command 28317c157c8SThomas Graf * 28417c157c8SThomas Graf * Returns pointer to user specific header 28517c157c8SThomas Graf */ 28617c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb, 28717c157c8SThomas Graf struct genl_info *info, 2882ae0f17dSJohannes Berg const struct genl_family *family, 28917c157c8SThomas Graf int flags, u8 cmd) 29017c157c8SThomas Graf { 29115e47304SEric W. Biederman return genlmsg_put(skb, info->snd_portid, info->snd_seq, family, 29217c157c8SThomas Graf flags, cmd); 29317c157c8SThomas Graf } 29417c157c8SThomas Graf 29517c157c8SThomas Graf /** 296482a8524SThomas Graf * genlmsg_end - Finalize a generic netlink message 297482a8524SThomas Graf * @skb: socket buffer the message is stored in 298482a8524SThomas Graf * @hdr: user specific header 299482a8524SThomas Graf */ 300053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr) 301482a8524SThomas Graf { 302053c095aSJohannes Berg nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 303482a8524SThomas Graf } 304482a8524SThomas Graf 305482a8524SThomas Graf /** 306482a8524SThomas Graf * genlmsg_cancel - Cancel construction of a generic netlink message 307482a8524SThomas Graf * @skb: socket buffer the message is stored in 308482a8524SThomas Graf * @hdr: generic netlink message header 309482a8524SThomas Graf */ 310bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 311482a8524SThomas Graf { 31238db9e1dSJulia Lawall if (hdr) 313bc3ed28cSThomas Graf nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 314482a8524SThomas Graf } 315482a8524SThomas Graf 316482a8524SThomas Graf /** 317134e6375SJohannes Berg * genlmsg_multicast_netns - multicast a netlink message to a specific netns 31868eb5503SJohannes Berg * @family: the generic netlink family 319134e6375SJohannes Berg * @net: the net namespace 320134e6375SJohannes Berg * @skb: netlink message as socket buffer 32115e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 3222a94fe48SJohannes Berg * @group: offset of multicast group in groups array 323134e6375SJohannes Berg * @flags: allocation flags 324134e6375SJohannes Berg */ 3252ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family, 32668eb5503SJohannes Berg struct net *net, struct sk_buff *skb, 32715e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 328134e6375SJohannes Berg { 329220815a9SJohannes Berg if (WARN_ON_ONCE(group >= family->n_mcgrps)) 3302a94fe48SJohannes Berg return -EINVAL; 3312a94fe48SJohannes Berg group = family->mcgrp_offset + group; 33215e47304SEric W. Biederman return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); 333134e6375SJohannes Berg } 334134e6375SJohannes Berg 335134e6375SJohannes Berg /** 336134e6375SJohannes Berg * genlmsg_multicast - multicast a netlink message to the default netns 33768eb5503SJohannes Berg * @family: the generic netlink family 338482a8524SThomas Graf * @skb: netlink message as socket buffer 33915e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 3402a94fe48SJohannes Berg * @group: offset of multicast group in groups array 341d387f6adSThomas Graf * @flags: allocation flags 342482a8524SThomas Graf */ 3432ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family, 34468eb5503SJohannes Berg struct sk_buff *skb, u32 portid, 345d387f6adSThomas Graf unsigned int group, gfp_t flags) 346482a8524SThomas Graf { 34768eb5503SJohannes Berg return genlmsg_multicast_netns(family, &init_net, skb, 34868eb5503SJohannes Berg portid, group, flags); 349482a8524SThomas Graf } 350482a8524SThomas Graf 351482a8524SThomas Graf /** 352134e6375SJohannes Berg * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 35368eb5503SJohannes Berg * @family: the generic netlink family 354134e6375SJohannes Berg * @skb: netlink message as socket buffer 35515e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 3562a94fe48SJohannes Berg * @group: offset of multicast group in groups array 357134e6375SJohannes Berg * @flags: allocation flags 358134e6375SJohannes Berg * 359134e6375SJohannes Berg * This function must hold the RTNL or rcu_read_lock(). 360134e6375SJohannes Berg */ 3612ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family, 36268eb5503SJohannes Berg struct sk_buff *skb, u32 portid, 363134e6375SJohannes Berg unsigned int group, gfp_t flags); 364134e6375SJohannes Berg 365134e6375SJohannes Berg /** 366482a8524SThomas Graf * genlmsg_unicast - unicast a netlink message 367a1a824f4SJakub Kicinski * @net: network namespace to look up @portid in 368482a8524SThomas Graf * @skb: netlink message as socket buffer 36915e47304SEric W. Biederman * @portid: netlink portid of the destination socket 370482a8524SThomas Graf */ 37115e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid) 372482a8524SThomas Graf { 37315e47304SEric W. Biederman return nlmsg_unicast(net->genl_sock, skb, portid); 374482a8524SThomas Graf } 375482a8524SThomas Graf 376fb0ba6bdSBalbir Singh /** 37781878d27SThomas Graf * genlmsg_reply - reply to a request 37881878d27SThomas Graf * @skb: netlink message to be sent back 37981878d27SThomas Graf * @info: receiver information 38081878d27SThomas Graf */ 38181878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 38281878d27SThomas Graf { 38315e47304SEric W. Biederman return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid); 38481878d27SThomas Graf } 38581878d27SThomas Graf 38681878d27SThomas Graf /** 387a1a824f4SJakub Kicinski * genlmsg_data - head of message payload 38870f23fd6SJustin P. Mattock * @gnlh: genetlink message header 389fb0ba6bdSBalbir Singh */ 390fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 391fb0ba6bdSBalbir Singh { 392fb0ba6bdSBalbir Singh return ((unsigned char *) gnlh + GENL_HDRLEN); 393fb0ba6bdSBalbir Singh } 394fb0ba6bdSBalbir Singh 395fb0ba6bdSBalbir Singh /** 396fb0ba6bdSBalbir Singh * genlmsg_len - length of message payload 397fb0ba6bdSBalbir Singh * @gnlh: genetlink message header 398fb0ba6bdSBalbir Singh */ 399fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh) 400fb0ba6bdSBalbir Singh { 401fb0ba6bdSBalbir Singh struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 402fb0ba6bdSBalbir Singh NLMSG_HDRLEN); 403fb0ba6bdSBalbir Singh return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 404fb0ba6bdSBalbir Singh } 405fb0ba6bdSBalbir Singh 40617db952cSBalbir Singh /** 40717db952cSBalbir Singh * genlmsg_msg_size - length of genetlink message not including padding 40817db952cSBalbir Singh * @payload: length of message payload 40917db952cSBalbir Singh */ 41017db952cSBalbir Singh static inline int genlmsg_msg_size(int payload) 41117db952cSBalbir Singh { 41217db952cSBalbir Singh return GENL_HDRLEN + payload; 41317db952cSBalbir Singh } 41417db952cSBalbir Singh 41517db952cSBalbir Singh /** 41617db952cSBalbir Singh * genlmsg_total_size - length of genetlink message including padding 41717db952cSBalbir Singh * @payload: length of message payload 41817db952cSBalbir Singh */ 41917db952cSBalbir Singh static inline int genlmsg_total_size(int payload) 42017db952cSBalbir Singh { 42117db952cSBalbir Singh return NLMSG_ALIGN(genlmsg_msg_size(payload)); 42217db952cSBalbir Singh } 42317db952cSBalbir Singh 4243dabc715SThomas Graf /** 4253dabc715SThomas Graf * genlmsg_new - Allocate a new generic netlink message 4263dabc715SThomas Graf * @payload: size of the message payload 4273dabc715SThomas Graf * @flags: the type of memory to allocate. 4283dabc715SThomas Graf */ 4293dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 4303dabc715SThomas Graf { 4313dabc715SThomas Graf return nlmsg_new(genlmsg_total_size(payload), flags); 4323dabc715SThomas Graf } 4333dabc715SThomas Graf 43462b68e99SJohannes Berg /** 43562b68e99SJohannes Berg * genl_set_err - report error to genetlink broadcast listeners 43668eb5503SJohannes Berg * @family: the generic netlink family 43762b68e99SJohannes Berg * @net: the network namespace to report the error to 43862b68e99SJohannes Berg * @portid: the PORTID of a process that we want to skip (if any) 43962b68e99SJohannes Berg * @group: the broadcast group that will notice the error 4402a94fe48SJohannes Berg * (this is the offset of the multicast group in the groups array) 44162b68e99SJohannes Berg * @code: error code, must be negative (as usual in kernelspace) 44262b68e99SJohannes Berg * 44362b68e99SJohannes Berg * This function returns the number of broadcast listeners that have set the 44462b68e99SJohannes Berg * NETLINK_RECV_NO_ENOBUFS socket option. 44562b68e99SJohannes Berg */ 4462ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family, 4472ae0f17dSJohannes Berg struct net *net, u32 portid, 4482ae0f17dSJohannes Berg u32 group, int code) 44962b68e99SJohannes Berg { 45091398a09SJohannes Berg if (WARN_ON_ONCE(group >= family->n_mcgrps)) 45191398a09SJohannes Berg return -EINVAL; 45291398a09SJohannes Berg group = family->mcgrp_offset + group; 45362b68e99SJohannes Berg return netlink_set_err(net->genl_sock, portid, group, code); 45462b68e99SJohannes Berg } 4553dabc715SThomas Graf 4562ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family, 457f8403a2eSJohannes Berg struct net *net, unsigned int group) 4580d566379SNicolas Dichtel { 4590d566379SNicolas Dichtel if (WARN_ON_ONCE(group >= family->n_mcgrps)) 4600d566379SNicolas Dichtel return -EINVAL; 4610d566379SNicolas Dichtel group = family->mcgrp_offset + group; 462f8403a2eSJohannes Berg return netlink_has_listeners(net->genl_sock, group); 4630d566379SNicolas Dichtel } 464482a8524SThomas Graf #endif /* __NET_GENERIC_NETLINK_H */ 465