12b4a8990SMichal Kubecek /* SPDX-License-Identifier: GPL-2.0-only */ 22b4a8990SMichal Kubecek 32b4a8990SMichal Kubecek #ifndef _NET_ETHTOOL_NETLINK_H 42b4a8990SMichal Kubecek #define _NET_ETHTOOL_NETLINK_H 52b4a8990SMichal Kubecek 62b4a8990SMichal Kubecek #include <linux/ethtool_netlink.h> 72b4a8990SMichal Kubecek #include <linux/netdevice.h> 82b4a8990SMichal Kubecek #include <net/genetlink.h> 9041b1c5dSMichal Kubecek #include <net/sock.h> 10041b1c5dSMichal Kubecek 11041b1c5dSMichal Kubecek struct ethnl_req_info; 12041b1c5dSMichal Kubecek 1398130546SMichal Kubecek int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info, 14041b1c5dSMichal Kubecek const struct nlattr *nest, struct net *net, 1598130546SMichal Kubecek struct netlink_ext_ack *extack, 1698130546SMichal Kubecek bool require_dev); 17041b1c5dSMichal Kubecek int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev, 18041b1c5dSMichal Kubecek u16 attrtype); 19041b1c5dSMichal Kubecek struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd, 20041b1c5dSMichal Kubecek u16 hdr_attrtype, struct genl_info *info, 21041b1c5dSMichal Kubecek void **ehdrp); 22041b1c5dSMichal Kubecek 23041b1c5dSMichal Kubecek /** 24041b1c5dSMichal Kubecek * ethnl_strz_size() - calculate attribute length for fixed size string 25041b1c5dSMichal Kubecek * @s: ETH_GSTRING_LEN sized string (may not be null terminated) 26041b1c5dSMichal Kubecek * 27041b1c5dSMichal Kubecek * Return: total length of an attribute with null terminated string from @s 28041b1c5dSMichal Kubecek */ 29041b1c5dSMichal Kubecek static inline int ethnl_strz_size(const char *s) 30041b1c5dSMichal Kubecek { 31041b1c5dSMichal Kubecek return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1); 32041b1c5dSMichal Kubecek } 33041b1c5dSMichal Kubecek 34041b1c5dSMichal Kubecek /** 35041b1c5dSMichal Kubecek * ethnl_put_strz() - put string attribute with fixed size string 36041b1c5dSMichal Kubecek * @skb: skb with the message 37041b1c5dSMichal Kubecek * @attrype: attribute type 38041b1c5dSMichal Kubecek * @s: ETH_GSTRING_LEN sized string (may not be null terminated) 39041b1c5dSMichal Kubecek * 40041b1c5dSMichal Kubecek * Puts an attribute with null terminated string from @s into the message. 41041b1c5dSMichal Kubecek * 42041b1c5dSMichal Kubecek * Return: 0 on success, negative error code on failure 43041b1c5dSMichal Kubecek */ 44041b1c5dSMichal Kubecek static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype, 45041b1c5dSMichal Kubecek const char *s) 46041b1c5dSMichal Kubecek { 47041b1c5dSMichal Kubecek unsigned int len = strnlen(s, ETH_GSTRING_LEN); 48041b1c5dSMichal Kubecek struct nlattr *attr; 49041b1c5dSMichal Kubecek 50041b1c5dSMichal Kubecek attr = nla_reserve(skb, attrtype, len + 1); 51041b1c5dSMichal Kubecek if (!attr) 52041b1c5dSMichal Kubecek return -EMSGSIZE; 53041b1c5dSMichal Kubecek 54041b1c5dSMichal Kubecek memcpy(nla_data(attr), s, len); 55041b1c5dSMichal Kubecek ((char *)nla_data(attr))[len] = '\0'; 56041b1c5dSMichal Kubecek return 0; 57041b1c5dSMichal Kubecek } 58041b1c5dSMichal Kubecek 59041b1c5dSMichal Kubecek /** 60041b1c5dSMichal Kubecek * ethnl_update_u32() - update u32 value from NLA_U32 attribute 61041b1c5dSMichal Kubecek * @dst: value to update 62041b1c5dSMichal Kubecek * @attr: netlink attribute with new value or null 63041b1c5dSMichal Kubecek * @mod: pointer to bool for modification tracking 64041b1c5dSMichal Kubecek * 65041b1c5dSMichal Kubecek * Copy the u32 value from NLA_U32 netlink attribute @attr into variable 66041b1c5dSMichal Kubecek * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod 67041b1c5dSMichal Kubecek * is set to true if this function changed the value of *dst, otherwise it 68041b1c5dSMichal Kubecek * is left as is. 69041b1c5dSMichal Kubecek */ 70041b1c5dSMichal Kubecek static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr, 71041b1c5dSMichal Kubecek bool *mod) 72041b1c5dSMichal Kubecek { 73041b1c5dSMichal Kubecek u32 val; 74041b1c5dSMichal Kubecek 75041b1c5dSMichal Kubecek if (!attr) 76041b1c5dSMichal Kubecek return; 77041b1c5dSMichal Kubecek val = nla_get_u32(attr); 78041b1c5dSMichal Kubecek if (*dst == val) 79041b1c5dSMichal Kubecek return; 80041b1c5dSMichal Kubecek 81041b1c5dSMichal Kubecek *dst = val; 82041b1c5dSMichal Kubecek *mod = true; 83041b1c5dSMichal Kubecek } 84041b1c5dSMichal Kubecek 85041b1c5dSMichal Kubecek /** 86041b1c5dSMichal Kubecek * ethnl_update_u8() - update u8 value from NLA_U8 attribute 87041b1c5dSMichal Kubecek * @dst: value to update 88041b1c5dSMichal Kubecek * @attr: netlink attribute with new value or null 89041b1c5dSMichal Kubecek * @mod: pointer to bool for modification tracking 90041b1c5dSMichal Kubecek * 91041b1c5dSMichal Kubecek * Copy the u8 value from NLA_U8 netlink attribute @attr into variable 92041b1c5dSMichal Kubecek * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod 93041b1c5dSMichal Kubecek * is set to true if this function changed the value of *dst, otherwise it 94041b1c5dSMichal Kubecek * is left as is. 95041b1c5dSMichal Kubecek */ 96041b1c5dSMichal Kubecek static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr, 97041b1c5dSMichal Kubecek bool *mod) 98041b1c5dSMichal Kubecek { 99041b1c5dSMichal Kubecek u8 val; 100041b1c5dSMichal Kubecek 101041b1c5dSMichal Kubecek if (!attr) 102041b1c5dSMichal Kubecek return; 103041b1c5dSMichal Kubecek val = nla_get_u8(attr); 104041b1c5dSMichal Kubecek if (*dst == val) 105041b1c5dSMichal Kubecek return; 106041b1c5dSMichal Kubecek 107041b1c5dSMichal Kubecek *dst = val; 108041b1c5dSMichal Kubecek *mod = true; 109041b1c5dSMichal Kubecek } 110041b1c5dSMichal Kubecek 111041b1c5dSMichal Kubecek /** 112041b1c5dSMichal Kubecek * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute 113041b1c5dSMichal Kubecek * @dst: value to update 114041b1c5dSMichal Kubecek * @attr: netlink attribute with new value or null 115041b1c5dSMichal Kubecek * @mod: pointer to bool for modification tracking 116041b1c5dSMichal Kubecek * 117041b1c5dSMichal Kubecek * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable 118041b1c5dSMichal Kubecek * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is 119041b1c5dSMichal Kubecek * null. Bool pointed to by @mod is set to true if this function changed the 120041b1c5dSMichal Kubecek * logical value of *dst, otherwise it is left as is. 121041b1c5dSMichal Kubecek */ 122041b1c5dSMichal Kubecek static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr, 123041b1c5dSMichal Kubecek bool *mod) 124041b1c5dSMichal Kubecek { 125041b1c5dSMichal Kubecek u8 val; 126041b1c5dSMichal Kubecek 127041b1c5dSMichal Kubecek if (!attr) 128041b1c5dSMichal Kubecek return; 129041b1c5dSMichal Kubecek val = !!nla_get_u8(attr); 130041b1c5dSMichal Kubecek if (!!*dst == val) 131041b1c5dSMichal Kubecek return; 132041b1c5dSMichal Kubecek 133041b1c5dSMichal Kubecek *dst = val; 134041b1c5dSMichal Kubecek *mod = true; 135041b1c5dSMichal Kubecek } 136041b1c5dSMichal Kubecek 137041b1c5dSMichal Kubecek /** 138041b1c5dSMichal Kubecek * ethnl_update_binary() - update binary data from NLA_BINARY atribute 139041b1c5dSMichal Kubecek * @dst: value to update 140041b1c5dSMichal Kubecek * @len: destination buffer length 141041b1c5dSMichal Kubecek * @attr: netlink attribute with new value or null 142041b1c5dSMichal Kubecek * @mod: pointer to bool for modification tracking 143041b1c5dSMichal Kubecek * 144041b1c5dSMichal Kubecek * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block 145041b1c5dSMichal Kubecek * of length @len at @dst by attribute payload; do nothing if @attr is null. 146041b1c5dSMichal Kubecek * Bool pointed to by @mod is set to true if this function changed the logical 147041b1c5dSMichal Kubecek * value of *dst, otherwise it is left as is. 148041b1c5dSMichal Kubecek */ 149041b1c5dSMichal Kubecek static inline void ethnl_update_binary(void *dst, unsigned int len, 150041b1c5dSMichal Kubecek const struct nlattr *attr, bool *mod) 151041b1c5dSMichal Kubecek { 152041b1c5dSMichal Kubecek if (!attr) 153041b1c5dSMichal Kubecek return; 154041b1c5dSMichal Kubecek if (nla_len(attr) < len) 155041b1c5dSMichal Kubecek len = nla_len(attr); 156041b1c5dSMichal Kubecek if (!memcmp(dst, nla_data(attr), len)) 157041b1c5dSMichal Kubecek return; 158041b1c5dSMichal Kubecek 159041b1c5dSMichal Kubecek memcpy(dst, nla_data(attr), len); 160041b1c5dSMichal Kubecek *mod = true; 161041b1c5dSMichal Kubecek } 162041b1c5dSMichal Kubecek 163041b1c5dSMichal Kubecek /** 164041b1c5dSMichal Kubecek * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute 165041b1c5dSMichal Kubecek * @dst: value to update 166041b1c5dSMichal Kubecek * @attr: netlink attribute with new value or null 167041b1c5dSMichal Kubecek * @mod: pointer to bool for modification tracking 168041b1c5dSMichal Kubecek * 169041b1c5dSMichal Kubecek * Update bits in u32 value which are set in attribute's mask to values from 170041b1c5dSMichal Kubecek * attribute's value. Do nothing if @attr is null or the value wouldn't change; 171041b1c5dSMichal Kubecek * otherwise, set bool pointed to by @mod to true. 172041b1c5dSMichal Kubecek */ 173041b1c5dSMichal Kubecek static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr, 174041b1c5dSMichal Kubecek bool *mod) 175041b1c5dSMichal Kubecek { 176041b1c5dSMichal Kubecek struct nla_bitfield32 change; 177041b1c5dSMichal Kubecek u32 newval; 178041b1c5dSMichal Kubecek 179041b1c5dSMichal Kubecek if (!attr) 180041b1c5dSMichal Kubecek return; 181041b1c5dSMichal Kubecek change = nla_get_bitfield32(attr); 182041b1c5dSMichal Kubecek newval = (*dst & ~change.selector) | (change.value & change.selector); 183041b1c5dSMichal Kubecek if (*dst == newval) 184041b1c5dSMichal Kubecek return; 185041b1c5dSMichal Kubecek 186041b1c5dSMichal Kubecek *dst = newval; 187041b1c5dSMichal Kubecek *mod = true; 188041b1c5dSMichal Kubecek } 189041b1c5dSMichal Kubecek 190041b1c5dSMichal Kubecek /** 191041b1c5dSMichal Kubecek * ethnl_reply_header_size() - total size of reply header 192041b1c5dSMichal Kubecek * 193041b1c5dSMichal Kubecek * This is an upper estimate so that we do not need to hold RTNL lock longer 194041b1c5dSMichal Kubecek * than necessary (to prevent rename between size estimate and composing the 195041b1c5dSMichal Kubecek * message). Accounts only for device ifindex and name as those are the only 196041b1c5dSMichal Kubecek * attributes ethnl_fill_reply_header() puts into the reply header. 197041b1c5dSMichal Kubecek */ 198041b1c5dSMichal Kubecek static inline unsigned int ethnl_reply_header_size(void) 199041b1c5dSMichal Kubecek { 200041b1c5dSMichal Kubecek return nla_total_size(nla_total_size(sizeof(u32)) + 201041b1c5dSMichal Kubecek nla_total_size(IFNAMSIZ)); 202041b1c5dSMichal Kubecek } 203041b1c5dSMichal Kubecek 204728480f1SMichal Kubecek /* GET request handling */ 205728480f1SMichal Kubecek 206728480f1SMichal Kubecek /* Unified processing of GET requests uses two data structures: request info 207728480f1SMichal Kubecek * and reply data. Request info holds information parsed from client request 208728480f1SMichal Kubecek * and its stays constant through all request processing. Reply data holds data 209728480f1SMichal Kubecek * retrieved from ethtool_ops callbacks or other internal sources which is used 210728480f1SMichal Kubecek * to compose the reply. When processing a dump request, request info is filled 211728480f1SMichal Kubecek * only once (when the request message is parsed) but reply data is filled for 212728480f1SMichal Kubecek * each reply message. 213728480f1SMichal Kubecek * 214728480f1SMichal Kubecek * Both structures consist of part common for all request types (struct 215728480f1SMichal Kubecek * ethnl_req_info and struct ethnl_reply_data defined below) and optional 216728480f1SMichal Kubecek * parts specific for each request type. Common part always starts at offset 0. 217728480f1SMichal Kubecek */ 218728480f1SMichal Kubecek 219041b1c5dSMichal Kubecek /** 220041b1c5dSMichal Kubecek * struct ethnl_req_info - base type of request information for GET requests 221041b1c5dSMichal Kubecek * @dev: network device the request is for (may be null) 222041b1c5dSMichal Kubecek * @flags: request flags common for all request types 223041b1c5dSMichal Kubecek * 224728480f1SMichal Kubecek * This is a common base for request specific structures holding data from 225728480f1SMichal Kubecek * parsed userspace request. These always embed struct ethnl_req_info at 226728480f1SMichal Kubecek * zero offset. 227041b1c5dSMichal Kubecek */ 228041b1c5dSMichal Kubecek struct ethnl_req_info { 229041b1c5dSMichal Kubecek struct net_device *dev; 230041b1c5dSMichal Kubecek u32 flags; 231041b1c5dSMichal Kubecek }; 2322b4a8990SMichal Kubecek 233728480f1SMichal Kubecek /** 234728480f1SMichal Kubecek * struct ethnl_reply_data - base type of reply data for GET requests 235728480f1SMichal Kubecek * @dev: device for current reply message; in single shot requests it is 236728480f1SMichal Kubecek * equal to ðnl_req_info.dev; in dumps it's different for each 237728480f1SMichal Kubecek * reply message 238728480f1SMichal Kubecek * 239728480f1SMichal Kubecek * This is a common base for request specific structures holding data for 240728480f1SMichal Kubecek * kernel reply message. These always embed struct ethnl_reply_data at zero 241728480f1SMichal Kubecek * offset. 242728480f1SMichal Kubecek */ 243728480f1SMichal Kubecek struct ethnl_reply_data { 244728480f1SMichal Kubecek struct net_device *dev; 245728480f1SMichal Kubecek }; 246728480f1SMichal Kubecek 247728480f1SMichal Kubecek static inline int ethnl_ops_begin(struct net_device *dev) 248728480f1SMichal Kubecek { 249728480f1SMichal Kubecek if (dev && dev->ethtool_ops->begin) 250728480f1SMichal Kubecek return dev->ethtool_ops->begin(dev); 251728480f1SMichal Kubecek else 252728480f1SMichal Kubecek return 0; 253728480f1SMichal Kubecek } 254728480f1SMichal Kubecek 255728480f1SMichal Kubecek static inline void ethnl_ops_complete(struct net_device *dev) 256728480f1SMichal Kubecek { 257728480f1SMichal Kubecek if (dev && dev->ethtool_ops->complete) 258728480f1SMichal Kubecek dev->ethtool_ops->complete(dev); 259728480f1SMichal Kubecek } 260728480f1SMichal Kubecek 261728480f1SMichal Kubecek /** 262728480f1SMichal Kubecek * struct ethnl_request_ops - unified handling of GET requests 263728480f1SMichal Kubecek * @request_cmd: command id for request (GET) 264728480f1SMichal Kubecek * @reply_cmd: command id for reply (GET_REPLY) 265728480f1SMichal Kubecek * @hdr_attr: attribute type for request header 266728480f1SMichal Kubecek * @max_attr: maximum (top level) attribute type 267728480f1SMichal Kubecek * @req_info_size: size of request info 268728480f1SMichal Kubecek * @reply_data_size: size of reply data 269728480f1SMichal Kubecek * @request_policy: netlink policy for message contents 270728480f1SMichal Kubecek * @allow_nodev_do: allow non-dump request with no device identification 271728480f1SMichal Kubecek * @parse_request: 272728480f1SMichal Kubecek * Parse request except common header (struct ethnl_req_info). Common 273728480f1SMichal Kubecek * header is already filled on entry, the rest up to @repdata_offset 274728480f1SMichal Kubecek * is zero initialized. This callback should only modify type specific 275728480f1SMichal Kubecek * request info by parsed attributes from request message. 276728480f1SMichal Kubecek * @prepare_data: 277728480f1SMichal Kubecek * Retrieve and prepare data needed to compose a reply message. Calls to 278728480f1SMichal Kubecek * ethtool_ops handlers are limited to this callback. Common reply data 279728480f1SMichal Kubecek * (struct ethnl_reply_data) is filled on entry, type specific part after 280728480f1SMichal Kubecek * it is zero initialized. This callback should only modify the type 281728480f1SMichal Kubecek * specific part of reply data. Device identification from struct 282728480f1SMichal Kubecek * ethnl_reply_data is to be used as for dump requests, it iterates 283728480f1SMichal Kubecek * through network devices while dev member of struct ethnl_req_info 284728480f1SMichal Kubecek * points to the device from client request. 285728480f1SMichal Kubecek * @reply_size: 286728480f1SMichal Kubecek * Estimate reply message size. Returned value must be sufficient for 287728480f1SMichal Kubecek * message payload without common reply header. The callback may returned 288728480f1SMichal Kubecek * estimate higher than actual message size if exact calculation would 289728480f1SMichal Kubecek * not be worth the saved memory space. 290728480f1SMichal Kubecek * @fill_reply: 291728480f1SMichal Kubecek * Fill reply message payload (except for common header) from reply data. 292728480f1SMichal Kubecek * The callback must not generate more payload than previously called 293728480f1SMichal Kubecek * ->reply_size() estimated. 294728480f1SMichal Kubecek * @cleanup_data: 295728480f1SMichal Kubecek * Optional cleanup called when reply data is no longer needed. Can be 296728480f1SMichal Kubecek * used e.g. to free any additional data structures outside the main 297728480f1SMichal Kubecek * structure which were allocated by ->prepare_data(). When processing 298728480f1SMichal Kubecek * dump requests, ->cleanup() is called for each message. 299728480f1SMichal Kubecek * 300728480f1SMichal Kubecek * Description of variable parts of GET request handling when using the 301728480f1SMichal Kubecek * unified infrastructure. When used, a pointer to an instance of this 302728480f1SMichal Kubecek * structure is to be added to ðnl_default_requests array and generic 303728480f1SMichal Kubecek * handlers ethnl_default_doit(), ethnl_default_dumpit(), 3045cf2a548SMichal Kubecek * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops; 3055cf2a548SMichal Kubecek * ethnl_default_notify() can be used in @ethnl_notify_handlers to send 3065cf2a548SMichal Kubecek * notifications of the corresponding type. 307728480f1SMichal Kubecek */ 308728480f1SMichal Kubecek struct ethnl_request_ops { 309728480f1SMichal Kubecek u8 request_cmd; 310728480f1SMichal Kubecek u8 reply_cmd; 311728480f1SMichal Kubecek u16 hdr_attr; 312728480f1SMichal Kubecek unsigned int max_attr; 313728480f1SMichal Kubecek unsigned int req_info_size; 314728480f1SMichal Kubecek unsigned int reply_data_size; 315728480f1SMichal Kubecek const struct nla_policy *request_policy; 316728480f1SMichal Kubecek bool allow_nodev_do; 317728480f1SMichal Kubecek 318728480f1SMichal Kubecek int (*parse_request)(struct ethnl_req_info *req_info, 319728480f1SMichal Kubecek struct nlattr **tb, 320728480f1SMichal Kubecek struct netlink_ext_ack *extack); 321728480f1SMichal Kubecek int (*prepare_data)(const struct ethnl_req_info *req_info, 322728480f1SMichal Kubecek struct ethnl_reply_data *reply_data, 323728480f1SMichal Kubecek struct genl_info *info); 324728480f1SMichal Kubecek int (*reply_size)(const struct ethnl_req_info *req_info, 325728480f1SMichal Kubecek const struct ethnl_reply_data *reply_data); 326728480f1SMichal Kubecek int (*fill_reply)(struct sk_buff *skb, 327728480f1SMichal Kubecek const struct ethnl_req_info *req_info, 328728480f1SMichal Kubecek const struct ethnl_reply_data *reply_data); 329728480f1SMichal Kubecek void (*cleanup_data)(struct ethnl_reply_data *reply_data); 330728480f1SMichal Kubecek }; 331728480f1SMichal Kubecek 33271921690SMichal Kubecek /* request handlers */ 33371921690SMichal Kubecek 33471921690SMichal Kubecek extern const struct ethnl_request_ops ethnl_strset_request_ops; 335459e0b81SMichal Kubecek extern const struct ethnl_request_ops ethnl_linkinfo_request_ops; 336f625aa9bSMichal Kubecek extern const struct ethnl_request_ops ethnl_linkmodes_request_ops; 3373d2b847fSMichal Kubecek extern const struct ethnl_request_ops ethnl_linkstate_request_ops; 3386a94b8ccSMichal Kubecek extern const struct ethnl_request_ops ethnl_debug_request_ops; 33951ea22b0SMichal Kubecek extern const struct ethnl_request_ops ethnl_wol_request_ops; 34071921690SMichal Kubecek 341a53f3d41SMichal Kubecek int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info); 342bfbcfe20SMichal Kubecek int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info); 343e54d04e3SMichal Kubecek int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info); 3448d425b19SMichal Kubecek int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info); 345a53f3d41SMichal Kubecek 3462b4a8990SMichal Kubecek #endif /* _NET_ETHTOOL_NETLINK_H */ 347