Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 
0003 #ifndef _NET_ETHTOOL_NETLINK_H
0004 #define _NET_ETHTOOL_NETLINK_H
0005 
0006 #include <linux/ethtool_netlink.h>
0007 #include <linux/netdevice.h>
0008 #include <net/genetlink.h>
0009 #include <net/sock.h>
0010 
0011 struct ethnl_req_info;
0012 
0013 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
0014                    const struct nlattr *nest, struct net *net,
0015                    struct netlink_ext_ack *extack,
0016                    bool require_dev);
0017 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
0018                 u16 attrtype);
0019 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
0020                  u16 hdr_attrtype, struct genl_info *info,
0021                  void **ehdrp);
0022 void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd);
0023 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd);
0024 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev);
0025 
0026 /**
0027  * ethnl_strz_size() - calculate attribute length for fixed size string
0028  * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
0029  *
0030  * Return: total length of an attribute with null terminated string from @s
0031  */
0032 static inline int ethnl_strz_size(const char *s)
0033 {
0034     return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1);
0035 }
0036 
0037 /**
0038  * ethnl_put_strz() - put string attribute with fixed size string
0039  * @skb:      skb with the message
0040  * @attrtype: attribute type
0041  * @s:        ETH_GSTRING_LEN sized string (may not be null terminated)
0042  *
0043  * Puts an attribute with null terminated string from @s into the message.
0044  *
0045  * Return: 0 on success, negative error code on failure
0046  */
0047 static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype,
0048                  const char *s)
0049 {
0050     unsigned int len = strnlen(s, ETH_GSTRING_LEN);
0051     struct nlattr *attr;
0052 
0053     attr = nla_reserve(skb, attrtype, len + 1);
0054     if (!attr)
0055         return -EMSGSIZE;
0056 
0057     memcpy(nla_data(attr), s, len);
0058     ((char *)nla_data(attr))[len] = '\0';
0059     return 0;
0060 }
0061 
0062 /**
0063  * ethnl_update_u32() - update u32 value from NLA_U32 attribute
0064  * @dst:  value to update
0065  * @attr: netlink attribute with new value or null
0066  * @mod:  pointer to bool for modification tracking
0067  *
0068  * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
0069  * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
0070  * is set to true if this function changed the value of *dst, otherwise it
0071  * is left as is.
0072  */
0073 static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
0074                     bool *mod)
0075 {
0076     u32 val;
0077 
0078     if (!attr)
0079         return;
0080     val = nla_get_u32(attr);
0081     if (*dst == val)
0082         return;
0083 
0084     *dst = val;
0085     *mod = true;
0086 }
0087 
0088 /**
0089  * ethnl_update_u8() - update u8 value from NLA_U8 attribute
0090  * @dst:  value to update
0091  * @attr: netlink attribute with new value or null
0092  * @mod:  pointer to bool for modification tracking
0093  *
0094  * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
0095  * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
0096  * is set to true if this function changed the value of *dst, otherwise it
0097  * is left as is.
0098  */
0099 static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
0100                    bool *mod)
0101 {
0102     u8 val;
0103 
0104     if (!attr)
0105         return;
0106     val = nla_get_u8(attr);
0107     if (*dst == val)
0108         return;
0109 
0110     *dst = val;
0111     *mod = true;
0112 }
0113 
0114 /**
0115  * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute
0116  * @dst:  value to update
0117  * @attr: netlink attribute with new value or null
0118  * @mod:  pointer to bool for modification tracking
0119  *
0120  * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable
0121  * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
0122  * null. Bool pointed to by @mod is set to true if this function changed the
0123  * logical value of *dst, otherwise it is left as is.
0124  */
0125 static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr,
0126                        bool *mod)
0127 {
0128     u8 val;
0129 
0130     if (!attr)
0131         return;
0132     val = !!nla_get_u8(attr);
0133     if (!!*dst == val)
0134         return;
0135 
0136     *dst = val;
0137     *mod = true;
0138 }
0139 
0140 /**
0141  * ethnl_update_binary() - update binary data from NLA_BINARY attribute
0142  * @dst:  value to update
0143  * @len:  destination buffer length
0144  * @attr: netlink attribute with new value or null
0145  * @mod:  pointer to bool for modification tracking
0146  *
0147  * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
0148  * of length @len at @dst by attribute payload; do nothing if @attr is null.
0149  * Bool pointed to by @mod is set to true if this function changed the logical
0150  * value of *dst, otherwise it is left as is.
0151  */
0152 static inline void ethnl_update_binary(void *dst, unsigned int len,
0153                        const struct nlattr *attr, bool *mod)
0154 {
0155     if (!attr)
0156         return;
0157     if (nla_len(attr) < len)
0158         len = nla_len(attr);
0159     if (!memcmp(dst, nla_data(attr), len))
0160         return;
0161 
0162     memcpy(dst, nla_data(attr), len);
0163     *mod = true;
0164 }
0165 
0166 /**
0167  * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
0168  * @dst:  value to update
0169  * @attr: netlink attribute with new value or null
0170  * @mod:  pointer to bool for modification tracking
0171  *
0172  * Update bits in u32 value which are set in attribute's mask to values from
0173  * attribute's value. Do nothing if @attr is null or the value wouldn't change;
0174  * otherwise, set bool pointed to by @mod to true.
0175  */
0176 static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
0177                        bool *mod)
0178 {
0179     struct nla_bitfield32 change;
0180     u32 newval;
0181 
0182     if (!attr)
0183         return;
0184     change = nla_get_bitfield32(attr);
0185     newval = (*dst & ~change.selector) | (change.value & change.selector);
0186     if (*dst == newval)
0187         return;
0188 
0189     *dst = newval;
0190     *mod = true;
0191 }
0192 
0193 /**
0194  * ethnl_reply_header_size() - total size of reply header
0195  *
0196  * This is an upper estimate so that we do not need to hold RTNL lock longer
0197  * than necessary (to prevent rename between size estimate and composing the
0198  * message). Accounts only for device ifindex and name as those are the only
0199  * attributes ethnl_fill_reply_header() puts into the reply header.
0200  */
0201 static inline unsigned int ethnl_reply_header_size(void)
0202 {
0203     return nla_total_size(nla_total_size(sizeof(u32)) +
0204                   nla_total_size(IFNAMSIZ));
0205 }
0206 
0207 /* GET request handling */
0208 
0209 /* Unified processing of GET requests uses two data structures: request info
0210  * and reply data. Request info holds information parsed from client request
0211  * and its stays constant through all request processing. Reply data holds data
0212  * retrieved from ethtool_ops callbacks or other internal sources which is used
0213  * to compose the reply. When processing a dump request, request info is filled
0214  * only once (when the request message is parsed) but reply data is filled for
0215  * each reply message.
0216  *
0217  * Both structures consist of part common for all request types (struct
0218  * ethnl_req_info and struct ethnl_reply_data defined below) and optional
0219  * parts specific for each request type. Common part always starts at offset 0.
0220  */
0221 
0222 /**
0223  * struct ethnl_req_info - base type of request information for GET requests
0224  * @dev:   network device the request is for (may be null)
0225  * @dev_tracker: refcount tracker for @dev reference
0226  * @flags: request flags common for all request types
0227  *
0228  * This is a common base for request specific structures holding data from
0229  * parsed userspace request. These always embed struct ethnl_req_info at
0230  * zero offset.
0231  */
0232 struct ethnl_req_info {
0233     struct net_device   *dev;
0234     netdevice_tracker   dev_tracker;
0235     u32         flags;
0236 };
0237 
0238 static inline void ethnl_parse_header_dev_put(struct ethnl_req_info *req_info)
0239 {
0240     netdev_put(req_info->dev, &req_info->dev_tracker);
0241 }
0242 
0243 /**
0244  * struct ethnl_reply_data - base type of reply data for GET requests
0245  * @dev:       device for current reply message; in single shot requests it is
0246  *             equal to &ethnl_req_info.dev; in dumps it's different for each
0247  *             reply message
0248  *
0249  * This is a common base for request specific structures holding data for
0250  * kernel reply message. These always embed struct ethnl_reply_data at zero
0251  * offset.
0252  */
0253 struct ethnl_reply_data {
0254     struct net_device       *dev;
0255 };
0256 
0257 int ethnl_ops_begin(struct net_device *dev);
0258 void ethnl_ops_complete(struct net_device *dev);
0259 
0260 /**
0261  * struct ethnl_request_ops - unified handling of GET requests
0262  * @request_cmd:      command id for request (GET)
0263  * @reply_cmd:        command id for reply (GET_REPLY)
0264  * @hdr_attr:         attribute type for request header
0265  * @req_info_size:    size of request info
0266  * @reply_data_size:  size of reply data
0267  * @allow_nodev_do:   allow non-dump request with no device identification
0268  * @parse_request:
0269  *  Parse request except common header (struct ethnl_req_info). Common
0270  *  header is already filled on entry, the rest up to @repdata_offset
0271  *  is zero initialized. This callback should only modify type specific
0272  *  request info by parsed attributes from request message.
0273  * @prepare_data:
0274  *  Retrieve and prepare data needed to compose a reply message. Calls to
0275  *  ethtool_ops handlers are limited to this callback. Common reply data
0276  *  (struct ethnl_reply_data) is filled on entry, type specific part after
0277  *  it is zero initialized. This callback should only modify the type
0278  *  specific part of reply data. Device identification from struct
0279  *  ethnl_reply_data is to be used as for dump requests, it iterates
0280  *  through network devices while dev member of struct ethnl_req_info
0281  *  points to the device from client request.
0282  * @reply_size:
0283  *  Estimate reply message size. Returned value must be sufficient for
0284  *  message payload without common reply header. The callback may returned
0285  *  estimate higher than actual message size if exact calculation would
0286  *  not be worth the saved memory space.
0287  * @fill_reply:
0288  *  Fill reply message payload (except for common header) from reply data.
0289  *  The callback must not generate more payload than previously called
0290  *  ->reply_size() estimated.
0291  * @cleanup_data:
0292  *  Optional cleanup called when reply data is no longer needed. Can be
0293  *  used e.g. to free any additional data structures outside the main
0294  *  structure which were allocated by ->prepare_data(). When processing
0295  *  dump requests, ->cleanup() is called for each message.
0296  *
0297  * Description of variable parts of GET request handling when using the
0298  * unified infrastructure. When used, a pointer to an instance of this
0299  * structure is to be added to &ethnl_default_requests array and generic
0300  * handlers ethnl_default_doit(), ethnl_default_dumpit(),
0301  * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
0302  * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
0303  * notifications of the corresponding type.
0304  */
0305 struct ethnl_request_ops {
0306     u8          request_cmd;
0307     u8          reply_cmd;
0308     u16         hdr_attr;
0309     unsigned int        req_info_size;
0310     unsigned int        reply_data_size;
0311     bool            allow_nodev_do;
0312 
0313     int (*parse_request)(struct ethnl_req_info *req_info,
0314                  struct nlattr **tb,
0315                  struct netlink_ext_ack *extack);
0316     int (*prepare_data)(const struct ethnl_req_info *req_info,
0317                 struct ethnl_reply_data *reply_data,
0318                 struct genl_info *info);
0319     int (*reply_size)(const struct ethnl_req_info *req_info,
0320               const struct ethnl_reply_data *reply_data);
0321     int (*fill_reply)(struct sk_buff *skb,
0322               const struct ethnl_req_info *req_info,
0323               const struct ethnl_reply_data *reply_data);
0324     void (*cleanup_data)(struct ethnl_reply_data *reply_data);
0325 };
0326 
0327 /* request handlers */
0328 
0329 extern const struct ethnl_request_ops ethnl_strset_request_ops;
0330 extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
0331 extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
0332 extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
0333 extern const struct ethnl_request_ops ethnl_debug_request_ops;
0334 extern const struct ethnl_request_ops ethnl_wol_request_ops;
0335 extern const struct ethnl_request_ops ethnl_features_request_ops;
0336 extern const struct ethnl_request_ops ethnl_privflags_request_ops;
0337 extern const struct ethnl_request_ops ethnl_rings_request_ops;
0338 extern const struct ethnl_request_ops ethnl_channels_request_ops;
0339 extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
0340 extern const struct ethnl_request_ops ethnl_pause_request_ops;
0341 extern const struct ethnl_request_ops ethnl_eee_request_ops;
0342 extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
0343 extern const struct ethnl_request_ops ethnl_fec_request_ops;
0344 extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops;
0345 extern const struct ethnl_request_ops ethnl_stats_request_ops;
0346 extern const struct ethnl_request_ops ethnl_phc_vclocks_request_ops;
0347 extern const struct ethnl_request_ops ethnl_module_request_ops;
0348 
0349 extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
0350 extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
0351 extern const struct nla_policy ethnl_strset_get_policy[ETHTOOL_A_STRSET_COUNTS_ONLY + 1];
0352 extern const struct nla_policy ethnl_linkinfo_get_policy[ETHTOOL_A_LINKINFO_HEADER + 1];
0353 extern const struct nla_policy ethnl_linkinfo_set_policy[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL + 1];
0354 extern const struct nla_policy ethnl_linkmodes_get_policy[ETHTOOL_A_LINKMODES_HEADER + 1];
0355 extern const struct nla_policy ethnl_linkmodes_set_policy[ETHTOOL_A_LINKMODES_LANES + 1];
0356 extern const struct nla_policy ethnl_linkstate_get_policy[ETHTOOL_A_LINKSTATE_HEADER + 1];
0357 extern const struct nla_policy ethnl_debug_get_policy[ETHTOOL_A_DEBUG_HEADER + 1];
0358 extern const struct nla_policy ethnl_debug_set_policy[ETHTOOL_A_DEBUG_MSGMASK + 1];
0359 extern const struct nla_policy ethnl_wol_get_policy[ETHTOOL_A_WOL_HEADER + 1];
0360 extern const struct nla_policy ethnl_wol_set_policy[ETHTOOL_A_WOL_SOPASS + 1];
0361 extern const struct nla_policy ethnl_features_get_policy[ETHTOOL_A_FEATURES_HEADER + 1];
0362 extern const struct nla_policy ethnl_features_set_policy[ETHTOOL_A_FEATURES_WANTED + 1];
0363 extern const struct nla_policy ethnl_privflags_get_policy[ETHTOOL_A_PRIVFLAGS_HEADER + 1];
0364 extern const struct nla_policy ethnl_privflags_set_policy[ETHTOOL_A_PRIVFLAGS_FLAGS + 1];
0365 extern const struct nla_policy ethnl_rings_get_policy[ETHTOOL_A_RINGS_HEADER + 1];
0366 extern const struct nla_policy ethnl_rings_set_policy[ETHTOOL_A_RINGS_TX_PUSH + 1];
0367 extern const struct nla_policy ethnl_channels_get_policy[ETHTOOL_A_CHANNELS_HEADER + 1];
0368 extern const struct nla_policy ethnl_channels_set_policy[ETHTOOL_A_CHANNELS_COMBINED_COUNT + 1];
0369 extern const struct nla_policy ethnl_coalesce_get_policy[ETHTOOL_A_COALESCE_HEADER + 1];
0370 extern const struct nla_policy ethnl_coalesce_set_policy[ETHTOOL_A_COALESCE_MAX + 1];
0371 extern const struct nla_policy ethnl_pause_get_policy[ETHTOOL_A_PAUSE_HEADER + 1];
0372 extern const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_TX + 1];
0373 extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1];
0374 extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1];
0375 extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1];
0376 extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1];
0377 extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1];
0378 extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1];
0379 extern const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1];
0380 extern const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1];
0381 extern const struct nla_policy ethnl_module_eeprom_get_policy[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS + 1];
0382 extern const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1];
0383 extern const struct nla_policy ethnl_phc_vclocks_get_policy[ETHTOOL_A_PHC_VCLOCKS_HEADER + 1];
0384 extern const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1];
0385 extern const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1];
0386 
0387 int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
0388 int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
0389 int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info);
0390 int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info);
0391 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
0392 int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info);
0393 int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info);
0394 int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info);
0395 int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info);
0396 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info);
0397 int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info);
0398 int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
0399 int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
0400 int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info);
0401 int ethnl_tunnel_info_start(struct netlink_callback *cb);
0402 int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
0403 int ethnl_set_fec(struct sk_buff *skb, struct genl_info *info);
0404 int ethnl_set_module(struct sk_buff *skb, struct genl_info *info);
0405 
0406 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
0407 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
0408 extern const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN];
0409 extern const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN];
0410 extern const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN];
0411 
0412 #endif /* _NET_ETHTOOL_NETLINK_H */