162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci#include "netlink.h" 462306a36Sopenharmony_ci#include "common.h" 562306a36Sopenharmony_ci#include "bitset.h" 662306a36Sopenharmony_ci 762306a36Sopenharmony_cistruct features_req_info { 862306a36Sopenharmony_ci struct ethnl_req_info base; 962306a36Sopenharmony_ci}; 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_cistruct features_reply_data { 1262306a36Sopenharmony_ci struct ethnl_reply_data base; 1362306a36Sopenharmony_ci u32 hw[ETHTOOL_DEV_FEATURE_WORDS]; 1462306a36Sopenharmony_ci u32 wanted[ETHTOOL_DEV_FEATURE_WORDS]; 1562306a36Sopenharmony_ci u32 active[ETHTOOL_DEV_FEATURE_WORDS]; 1662306a36Sopenharmony_ci u32 nochange[ETHTOOL_DEV_FEATURE_WORDS]; 1762306a36Sopenharmony_ci u32 all[ETHTOOL_DEV_FEATURE_WORDS]; 1862306a36Sopenharmony_ci}; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define FEATURES_REPDATA(__reply_base) \ 2162306a36Sopenharmony_ci container_of(__reply_base, struct features_reply_data, base) 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciconst struct nla_policy ethnl_features_get_policy[] = { 2462306a36Sopenharmony_ci [ETHTOOL_A_FEATURES_HEADER] = 2562306a36Sopenharmony_ci NLA_POLICY_NESTED(ethnl_header_policy), 2662306a36Sopenharmony_ci}; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_cistatic void ethnl_features_to_bitmap32(u32 *dest, netdev_features_t src) 2962306a36Sopenharmony_ci{ 3062306a36Sopenharmony_ci unsigned int i; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; i++) 3362306a36Sopenharmony_ci dest[i] = src >> (32 * i); 3462306a36Sopenharmony_ci} 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic int features_prepare_data(const struct ethnl_req_info *req_base, 3762306a36Sopenharmony_ci struct ethnl_reply_data *reply_base, 3862306a36Sopenharmony_ci const struct genl_info *info) 3962306a36Sopenharmony_ci{ 4062306a36Sopenharmony_ci struct features_reply_data *data = FEATURES_REPDATA(reply_base); 4162306a36Sopenharmony_ci struct net_device *dev = reply_base->dev; 4262306a36Sopenharmony_ci netdev_features_t all_features; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci ethnl_features_to_bitmap32(data->hw, dev->hw_features); 4562306a36Sopenharmony_ci ethnl_features_to_bitmap32(data->wanted, dev->wanted_features); 4662306a36Sopenharmony_ci ethnl_features_to_bitmap32(data->active, dev->features); 4762306a36Sopenharmony_ci ethnl_features_to_bitmap32(data->nochange, NETIF_F_NEVER_CHANGE); 4862306a36Sopenharmony_ci all_features = GENMASK_ULL(NETDEV_FEATURE_COUNT - 1, 0); 4962306a36Sopenharmony_ci ethnl_features_to_bitmap32(data->all, all_features); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci return 0; 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic int features_reply_size(const struct ethnl_req_info *req_base, 5562306a36Sopenharmony_ci const struct ethnl_reply_data *reply_base) 5662306a36Sopenharmony_ci{ 5762306a36Sopenharmony_ci const struct features_reply_data *data = FEATURES_REPDATA(reply_base); 5862306a36Sopenharmony_ci bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 5962306a36Sopenharmony_ci unsigned int len = 0; 6062306a36Sopenharmony_ci int ret; 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci ret = ethnl_bitset32_size(data->hw, data->all, NETDEV_FEATURE_COUNT, 6362306a36Sopenharmony_ci netdev_features_strings, compact); 6462306a36Sopenharmony_ci if (ret < 0) 6562306a36Sopenharmony_ci return ret; 6662306a36Sopenharmony_ci len += ret; 6762306a36Sopenharmony_ci ret = ethnl_bitset32_size(data->wanted, NULL, NETDEV_FEATURE_COUNT, 6862306a36Sopenharmony_ci netdev_features_strings, compact); 6962306a36Sopenharmony_ci if (ret < 0) 7062306a36Sopenharmony_ci return ret; 7162306a36Sopenharmony_ci len += ret; 7262306a36Sopenharmony_ci ret = ethnl_bitset32_size(data->active, NULL, NETDEV_FEATURE_COUNT, 7362306a36Sopenharmony_ci netdev_features_strings, compact); 7462306a36Sopenharmony_ci if (ret < 0) 7562306a36Sopenharmony_ci return ret; 7662306a36Sopenharmony_ci len += ret; 7762306a36Sopenharmony_ci ret = ethnl_bitset32_size(data->nochange, NULL, NETDEV_FEATURE_COUNT, 7862306a36Sopenharmony_ci netdev_features_strings, compact); 7962306a36Sopenharmony_ci if (ret < 0) 8062306a36Sopenharmony_ci return ret; 8162306a36Sopenharmony_ci len += ret; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci return len; 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic int features_fill_reply(struct sk_buff *skb, 8762306a36Sopenharmony_ci const struct ethnl_req_info *req_base, 8862306a36Sopenharmony_ci const struct ethnl_reply_data *reply_base) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci const struct features_reply_data *data = FEATURES_REPDATA(reply_base); 9162306a36Sopenharmony_ci bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 9262306a36Sopenharmony_ci int ret; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_HW, data->hw, 9562306a36Sopenharmony_ci data->all, NETDEV_FEATURE_COUNT, 9662306a36Sopenharmony_ci netdev_features_strings, compact); 9762306a36Sopenharmony_ci if (ret < 0) 9862306a36Sopenharmony_ci return ret; 9962306a36Sopenharmony_ci ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_WANTED, data->wanted, 10062306a36Sopenharmony_ci NULL, NETDEV_FEATURE_COUNT, 10162306a36Sopenharmony_ci netdev_features_strings, compact); 10262306a36Sopenharmony_ci if (ret < 0) 10362306a36Sopenharmony_ci return ret; 10462306a36Sopenharmony_ci ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_ACTIVE, data->active, 10562306a36Sopenharmony_ci NULL, NETDEV_FEATURE_COUNT, 10662306a36Sopenharmony_ci netdev_features_strings, compact); 10762306a36Sopenharmony_ci if (ret < 0) 10862306a36Sopenharmony_ci return ret; 10962306a36Sopenharmony_ci return ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_NOCHANGE, 11062306a36Sopenharmony_ci data->nochange, NULL, NETDEV_FEATURE_COUNT, 11162306a36Sopenharmony_ci netdev_features_strings, compact); 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ciconst struct ethnl_request_ops ethnl_features_request_ops = { 11562306a36Sopenharmony_ci .request_cmd = ETHTOOL_MSG_FEATURES_GET, 11662306a36Sopenharmony_ci .reply_cmd = ETHTOOL_MSG_FEATURES_GET_REPLY, 11762306a36Sopenharmony_ci .hdr_attr = ETHTOOL_A_FEATURES_HEADER, 11862306a36Sopenharmony_ci .req_info_size = sizeof(struct features_req_info), 11962306a36Sopenharmony_ci .reply_data_size = sizeof(struct features_reply_data), 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci .prepare_data = features_prepare_data, 12262306a36Sopenharmony_ci .reply_size = features_reply_size, 12362306a36Sopenharmony_ci .fill_reply = features_fill_reply, 12462306a36Sopenharmony_ci}; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci/* FEATURES_SET */ 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciconst struct nla_policy ethnl_features_set_policy[] = { 12962306a36Sopenharmony_ci [ETHTOOL_A_FEATURES_HEADER] = 13062306a36Sopenharmony_ci NLA_POLICY_NESTED(ethnl_header_policy), 13162306a36Sopenharmony_ci [ETHTOOL_A_FEATURES_WANTED] = { .type = NLA_NESTED }, 13262306a36Sopenharmony_ci}; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_cistatic void ethnl_features_to_bitmap(unsigned long *dest, netdev_features_t val) 13562306a36Sopenharmony_ci{ 13662306a36Sopenharmony_ci const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT); 13762306a36Sopenharmony_ci unsigned int i; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci for (i = 0; i < words; i++) 14062306a36Sopenharmony_ci dest[i] = (unsigned long)(val >> (i * BITS_PER_LONG)); 14162306a36Sopenharmony_ci} 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_cistatic netdev_features_t ethnl_bitmap_to_features(unsigned long *src) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci const unsigned int nft_bits = sizeof(netdev_features_t) * BITS_PER_BYTE; 14662306a36Sopenharmony_ci const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT); 14762306a36Sopenharmony_ci netdev_features_t ret = 0; 14862306a36Sopenharmony_ci unsigned int i; 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci for (i = 0; i < words; i++) 15162306a36Sopenharmony_ci ret |= (netdev_features_t)(src[i]) << (i * BITS_PER_LONG); 15262306a36Sopenharmony_ci ret &= ~(netdev_features_t)0 >> (nft_bits - NETDEV_FEATURE_COUNT); 15362306a36Sopenharmony_ci return ret; 15462306a36Sopenharmony_ci} 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_cistatic int features_send_reply(struct net_device *dev, struct genl_info *info, 15762306a36Sopenharmony_ci const unsigned long *wanted, 15862306a36Sopenharmony_ci const unsigned long *wanted_mask, 15962306a36Sopenharmony_ci const unsigned long *active, 16062306a36Sopenharmony_ci const unsigned long *active_mask, bool compact) 16162306a36Sopenharmony_ci{ 16262306a36Sopenharmony_ci struct sk_buff *rskb; 16362306a36Sopenharmony_ci void *reply_payload; 16462306a36Sopenharmony_ci int reply_len = 0; 16562306a36Sopenharmony_ci int ret; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci reply_len = ethnl_reply_header_size(); 16862306a36Sopenharmony_ci ret = ethnl_bitset_size(wanted, wanted_mask, NETDEV_FEATURE_COUNT, 16962306a36Sopenharmony_ci netdev_features_strings, compact); 17062306a36Sopenharmony_ci if (ret < 0) 17162306a36Sopenharmony_ci goto err; 17262306a36Sopenharmony_ci reply_len += ret; 17362306a36Sopenharmony_ci ret = ethnl_bitset_size(active, active_mask, NETDEV_FEATURE_COUNT, 17462306a36Sopenharmony_ci netdev_features_strings, compact); 17562306a36Sopenharmony_ci if (ret < 0) 17662306a36Sopenharmony_ci goto err; 17762306a36Sopenharmony_ci reply_len += ret; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci ret = -ENOMEM; 18062306a36Sopenharmony_ci rskb = ethnl_reply_init(reply_len, dev, ETHTOOL_MSG_FEATURES_SET_REPLY, 18162306a36Sopenharmony_ci ETHTOOL_A_FEATURES_HEADER, info, 18262306a36Sopenharmony_ci &reply_payload); 18362306a36Sopenharmony_ci if (!rskb) 18462306a36Sopenharmony_ci goto err; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_WANTED, wanted, 18762306a36Sopenharmony_ci wanted_mask, NETDEV_FEATURE_COUNT, 18862306a36Sopenharmony_ci netdev_features_strings, compact); 18962306a36Sopenharmony_ci if (ret < 0) 19062306a36Sopenharmony_ci goto nla_put_failure; 19162306a36Sopenharmony_ci ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_ACTIVE, active, 19262306a36Sopenharmony_ci active_mask, NETDEV_FEATURE_COUNT, 19362306a36Sopenharmony_ci netdev_features_strings, compact); 19462306a36Sopenharmony_ci if (ret < 0) 19562306a36Sopenharmony_ci goto nla_put_failure; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci genlmsg_end(rskb, reply_payload); 19862306a36Sopenharmony_ci ret = genlmsg_reply(rskb, info); 19962306a36Sopenharmony_ci return ret; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_cinla_put_failure: 20262306a36Sopenharmony_ci nlmsg_free(rskb); 20362306a36Sopenharmony_ci WARN_ONCE(1, "calculated message payload length (%d) not sufficient\n", 20462306a36Sopenharmony_ci reply_len); 20562306a36Sopenharmony_cierr: 20662306a36Sopenharmony_ci GENL_SET_ERR_MSG(info, "failed to send reply message"); 20762306a36Sopenharmony_ci return ret; 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ciint ethnl_set_features(struct sk_buff *skb, struct genl_info *info) 21162306a36Sopenharmony_ci{ 21262306a36Sopenharmony_ci DECLARE_BITMAP(wanted_diff_mask, NETDEV_FEATURE_COUNT); 21362306a36Sopenharmony_ci DECLARE_BITMAP(active_diff_mask, NETDEV_FEATURE_COUNT); 21462306a36Sopenharmony_ci DECLARE_BITMAP(old_active, NETDEV_FEATURE_COUNT); 21562306a36Sopenharmony_ci DECLARE_BITMAP(old_wanted, NETDEV_FEATURE_COUNT); 21662306a36Sopenharmony_ci DECLARE_BITMAP(new_active, NETDEV_FEATURE_COUNT); 21762306a36Sopenharmony_ci DECLARE_BITMAP(new_wanted, NETDEV_FEATURE_COUNT); 21862306a36Sopenharmony_ci DECLARE_BITMAP(req_wanted, NETDEV_FEATURE_COUNT); 21962306a36Sopenharmony_ci DECLARE_BITMAP(req_mask, NETDEV_FEATURE_COUNT); 22062306a36Sopenharmony_ci struct ethnl_req_info req_info = {}; 22162306a36Sopenharmony_ci struct nlattr **tb = info->attrs; 22262306a36Sopenharmony_ci struct net_device *dev; 22362306a36Sopenharmony_ci bool mod; 22462306a36Sopenharmony_ci int ret; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci if (!tb[ETHTOOL_A_FEATURES_WANTED]) 22762306a36Sopenharmony_ci return -EINVAL; 22862306a36Sopenharmony_ci ret = ethnl_parse_header_dev_get(&req_info, 22962306a36Sopenharmony_ci tb[ETHTOOL_A_FEATURES_HEADER], 23062306a36Sopenharmony_ci genl_info_net(info), info->extack, 23162306a36Sopenharmony_ci true); 23262306a36Sopenharmony_ci if (ret < 0) 23362306a36Sopenharmony_ci return ret; 23462306a36Sopenharmony_ci dev = req_info.dev; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci rtnl_lock(); 23762306a36Sopenharmony_ci ret = ethnl_ops_begin(dev); 23862306a36Sopenharmony_ci if (ret < 0) 23962306a36Sopenharmony_ci goto out_rtnl; 24062306a36Sopenharmony_ci ethnl_features_to_bitmap(old_active, dev->features); 24162306a36Sopenharmony_ci ethnl_features_to_bitmap(old_wanted, dev->wanted_features); 24262306a36Sopenharmony_ci ret = ethnl_parse_bitset(req_wanted, req_mask, NETDEV_FEATURE_COUNT, 24362306a36Sopenharmony_ci tb[ETHTOOL_A_FEATURES_WANTED], 24462306a36Sopenharmony_ci netdev_features_strings, info->extack); 24562306a36Sopenharmony_ci if (ret < 0) 24662306a36Sopenharmony_ci goto out_ops; 24762306a36Sopenharmony_ci if (ethnl_bitmap_to_features(req_mask) & ~NETIF_F_ETHTOOL_BITS) { 24862306a36Sopenharmony_ci GENL_SET_ERR_MSG(info, "attempt to change non-ethtool features"); 24962306a36Sopenharmony_ci ret = -EINVAL; 25062306a36Sopenharmony_ci goto out_ops; 25162306a36Sopenharmony_ci } 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci /* set req_wanted bits not in req_mask from old_wanted */ 25462306a36Sopenharmony_ci bitmap_and(req_wanted, req_wanted, req_mask, NETDEV_FEATURE_COUNT); 25562306a36Sopenharmony_ci bitmap_andnot(new_wanted, old_wanted, req_mask, NETDEV_FEATURE_COUNT); 25662306a36Sopenharmony_ci bitmap_or(req_wanted, new_wanted, req_wanted, NETDEV_FEATURE_COUNT); 25762306a36Sopenharmony_ci if (!bitmap_equal(req_wanted, old_wanted, NETDEV_FEATURE_COUNT)) { 25862306a36Sopenharmony_ci dev->wanted_features &= ~dev->hw_features; 25962306a36Sopenharmony_ci dev->wanted_features |= ethnl_bitmap_to_features(req_wanted) & dev->hw_features; 26062306a36Sopenharmony_ci __netdev_update_features(dev); 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci ethnl_features_to_bitmap(new_active, dev->features); 26362306a36Sopenharmony_ci mod = !bitmap_equal(old_active, new_active, NETDEV_FEATURE_COUNT); 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci ret = 0; 26662306a36Sopenharmony_ci if (!(req_info.flags & ETHTOOL_FLAG_OMIT_REPLY)) { 26762306a36Sopenharmony_ci bool compact = req_info.flags & ETHTOOL_FLAG_COMPACT_BITSETS; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci bitmap_xor(wanted_diff_mask, req_wanted, new_active, 27062306a36Sopenharmony_ci NETDEV_FEATURE_COUNT); 27162306a36Sopenharmony_ci bitmap_xor(active_diff_mask, old_active, new_active, 27262306a36Sopenharmony_ci NETDEV_FEATURE_COUNT); 27362306a36Sopenharmony_ci bitmap_and(wanted_diff_mask, wanted_diff_mask, req_mask, 27462306a36Sopenharmony_ci NETDEV_FEATURE_COUNT); 27562306a36Sopenharmony_ci bitmap_and(req_wanted, req_wanted, wanted_diff_mask, 27662306a36Sopenharmony_ci NETDEV_FEATURE_COUNT); 27762306a36Sopenharmony_ci bitmap_and(new_active, new_active, active_diff_mask, 27862306a36Sopenharmony_ci NETDEV_FEATURE_COUNT); 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci ret = features_send_reply(dev, info, req_wanted, 28162306a36Sopenharmony_ci wanted_diff_mask, new_active, 28262306a36Sopenharmony_ci active_diff_mask, compact); 28362306a36Sopenharmony_ci } 28462306a36Sopenharmony_ci if (mod) 28562306a36Sopenharmony_ci netdev_features_change(dev); 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ciout_ops: 28862306a36Sopenharmony_ci ethnl_ops_complete(dev); 28962306a36Sopenharmony_ciout_rtnl: 29062306a36Sopenharmony_ci rtnl_unlock(); 29162306a36Sopenharmony_ci ethnl_parse_header_dev_put(&req_info); 29262306a36Sopenharmony_ci return ret; 29362306a36Sopenharmony_ci} 294