features.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. struct features_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct features_reply_data {
  9. struct ethnl_reply_data base;
  10. u32 hw[ETHTOOL_DEV_FEATURE_WORDS];
  11. u32 wanted[ETHTOOL_DEV_FEATURE_WORDS];
  12. u32 active[ETHTOOL_DEV_FEATURE_WORDS];
  13. u32 nochange[ETHTOOL_DEV_FEATURE_WORDS];
  14. u32 all[ETHTOOL_DEV_FEATURE_WORDS];
  15. };
  16. #define FEATURES_REPDATA(__reply_base) \
  17. container_of(__reply_base, struct features_reply_data, base)
  18. const struct nla_policy ethnl_features_get_policy[] = {
  19. [ETHTOOL_A_FEATURES_HEADER] =
  20. NLA_POLICY_NESTED(ethnl_header_policy),
  21. };
  22. static void ethnl_features_to_bitmap32(u32 *dest, netdev_features_t src)
  23. {
  24. unsigned int i;
  25. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; i++)
  26. dest[i] = src >> (32 * i);
  27. }
  28. static int features_prepare_data(const struct ethnl_req_info *req_base,
  29. struct ethnl_reply_data *reply_base,
  30. struct genl_info *info)
  31. {
  32. struct features_reply_data *data = FEATURES_REPDATA(reply_base);
  33. struct net_device *dev = reply_base->dev;
  34. netdev_features_t all_features;
  35. ethnl_features_to_bitmap32(data->hw, dev->hw_features);
  36. ethnl_features_to_bitmap32(data->wanted, dev->wanted_features);
  37. ethnl_features_to_bitmap32(data->active, dev->features);
  38. ethnl_features_to_bitmap32(data->nochange, NETIF_F_NEVER_CHANGE);
  39. all_features = GENMASK_ULL(NETDEV_FEATURE_COUNT - 1, 0);
  40. ethnl_features_to_bitmap32(data->all, all_features);
  41. return 0;
  42. }
  43. static int features_reply_size(const struct ethnl_req_info *req_base,
  44. const struct ethnl_reply_data *reply_base)
  45. {
  46. const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
  47. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  48. unsigned int len = 0;
  49. int ret;
  50. ret = ethnl_bitset32_size(data->hw, data->all, NETDEV_FEATURE_COUNT,
  51. netdev_features_strings, compact);
  52. if (ret < 0)
  53. return ret;
  54. len += ret;
  55. ret = ethnl_bitset32_size(data->wanted, NULL, NETDEV_FEATURE_COUNT,
  56. netdev_features_strings, compact);
  57. if (ret < 0)
  58. return ret;
  59. len += ret;
  60. ret = ethnl_bitset32_size(data->active, NULL, NETDEV_FEATURE_COUNT,
  61. netdev_features_strings, compact);
  62. if (ret < 0)
  63. return ret;
  64. len += ret;
  65. ret = ethnl_bitset32_size(data->nochange, NULL, NETDEV_FEATURE_COUNT,
  66. netdev_features_strings, compact);
  67. if (ret < 0)
  68. return ret;
  69. len += ret;
  70. return len;
  71. }
  72. static int features_fill_reply(struct sk_buff *skb,
  73. const struct ethnl_req_info *req_base,
  74. const struct ethnl_reply_data *reply_base)
  75. {
  76. const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
  77. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  78. int ret;
  79. ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_HW, data->hw,
  80. data->all, NETDEV_FEATURE_COUNT,
  81. netdev_features_strings, compact);
  82. if (ret < 0)
  83. return ret;
  84. ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_WANTED, data->wanted,
  85. NULL, NETDEV_FEATURE_COUNT,
  86. netdev_features_strings, compact);
  87. if (ret < 0)
  88. return ret;
  89. ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_ACTIVE, data->active,
  90. NULL, NETDEV_FEATURE_COUNT,
  91. netdev_features_strings, compact);
  92. if (ret < 0)
  93. return ret;
  94. return ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_NOCHANGE,
  95. data->nochange, NULL, NETDEV_FEATURE_COUNT,
  96. netdev_features_strings, compact);
  97. }
  98. const struct ethnl_request_ops ethnl_features_request_ops = {
  99. .request_cmd = ETHTOOL_MSG_FEATURES_GET,
  100. .reply_cmd = ETHTOOL_MSG_FEATURES_GET_REPLY,
  101. .hdr_attr = ETHTOOL_A_FEATURES_HEADER,
  102. .req_info_size = sizeof(struct features_req_info),
  103. .reply_data_size = sizeof(struct features_reply_data),
  104. .prepare_data = features_prepare_data,
  105. .reply_size = features_reply_size,
  106. .fill_reply = features_fill_reply,
  107. };
  108. /* FEATURES_SET */
  109. const struct nla_policy ethnl_features_set_policy[] = {
  110. [ETHTOOL_A_FEATURES_HEADER] =
  111. NLA_POLICY_NESTED(ethnl_header_policy),
  112. [ETHTOOL_A_FEATURES_WANTED] = { .type = NLA_NESTED },
  113. };
  114. static void ethnl_features_to_bitmap(unsigned long *dest, netdev_features_t val)
  115. {
  116. const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT);
  117. unsigned int i;
  118. for (i = 0; i < words; i++)
  119. dest[i] = (unsigned long)(val >> (i * BITS_PER_LONG));
  120. }
  121. static netdev_features_t ethnl_bitmap_to_features(unsigned long *src)
  122. {
  123. const unsigned int nft_bits = sizeof(netdev_features_t) * BITS_PER_BYTE;
  124. const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT);
  125. netdev_features_t ret = 0;
  126. unsigned int i;
  127. for (i = 0; i < words; i++)
  128. ret |= (netdev_features_t)(src[i]) << (i * BITS_PER_LONG);
  129. ret &= ~(netdev_features_t)0 >> (nft_bits - NETDEV_FEATURE_COUNT);
  130. return ret;
  131. }
  132. static int features_send_reply(struct net_device *dev, struct genl_info *info,
  133. const unsigned long *wanted,
  134. const unsigned long *wanted_mask,
  135. const unsigned long *active,
  136. const unsigned long *active_mask, bool compact)
  137. {
  138. struct sk_buff *rskb;
  139. void *reply_payload;
  140. int reply_len = 0;
  141. int ret;
  142. reply_len = ethnl_reply_header_size();
  143. ret = ethnl_bitset_size(wanted, wanted_mask, NETDEV_FEATURE_COUNT,
  144. netdev_features_strings, compact);
  145. if (ret < 0)
  146. goto err;
  147. reply_len += ret;
  148. ret = ethnl_bitset_size(active, active_mask, NETDEV_FEATURE_COUNT,
  149. netdev_features_strings, compact);
  150. if (ret < 0)
  151. goto err;
  152. reply_len += ret;
  153. ret = -ENOMEM;
  154. rskb = ethnl_reply_init(reply_len, dev, ETHTOOL_MSG_FEATURES_SET_REPLY,
  155. ETHTOOL_A_FEATURES_HEADER, info,
  156. &reply_payload);
  157. if (!rskb)
  158. goto err;
  159. ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_WANTED, wanted,
  160. wanted_mask, NETDEV_FEATURE_COUNT,
  161. netdev_features_strings, compact);
  162. if (ret < 0)
  163. goto nla_put_failure;
  164. ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_ACTIVE, active,
  165. active_mask, NETDEV_FEATURE_COUNT,
  166. netdev_features_strings, compact);
  167. if (ret < 0)
  168. goto nla_put_failure;
  169. genlmsg_end(rskb, reply_payload);
  170. ret = genlmsg_reply(rskb, info);
  171. return ret;
  172. nla_put_failure:
  173. nlmsg_free(rskb);
  174. WARN_ONCE(1, "calculated message payload length (%d) not sufficient\n",
  175. reply_len);
  176. err:
  177. GENL_SET_ERR_MSG(info, "failed to send reply message");
  178. return ret;
  179. }
  180. int ethnl_set_features(struct sk_buff *skb, struct genl_info *info)
  181. {
  182. DECLARE_BITMAP(wanted_diff_mask, NETDEV_FEATURE_COUNT);
  183. DECLARE_BITMAP(active_diff_mask, NETDEV_FEATURE_COUNT);
  184. DECLARE_BITMAP(old_active, NETDEV_FEATURE_COUNT);
  185. DECLARE_BITMAP(old_wanted, NETDEV_FEATURE_COUNT);
  186. DECLARE_BITMAP(new_active, NETDEV_FEATURE_COUNT);
  187. DECLARE_BITMAP(new_wanted, NETDEV_FEATURE_COUNT);
  188. DECLARE_BITMAP(req_wanted, NETDEV_FEATURE_COUNT);
  189. DECLARE_BITMAP(req_mask, NETDEV_FEATURE_COUNT);
  190. struct ethnl_req_info req_info = {};
  191. struct nlattr **tb = info->attrs;
  192. struct net_device *dev;
  193. bool mod;
  194. int ret;
  195. if (!tb[ETHTOOL_A_FEATURES_WANTED])
  196. return -EINVAL;
  197. ret = ethnl_parse_header_dev_get(&req_info,
  198. tb[ETHTOOL_A_FEATURES_HEADER],
  199. genl_info_net(info), info->extack,
  200. true);
  201. if (ret < 0)
  202. return ret;
  203. dev = req_info.dev;
  204. rtnl_lock();
  205. ethnl_features_to_bitmap(old_active, dev->features);
  206. ethnl_features_to_bitmap(old_wanted, dev->wanted_features);
  207. ret = ethnl_parse_bitset(req_wanted, req_mask, NETDEV_FEATURE_COUNT,
  208. tb[ETHTOOL_A_FEATURES_WANTED],
  209. netdev_features_strings, info->extack);
  210. if (ret < 0)
  211. goto out_rtnl;
  212. if (ethnl_bitmap_to_features(req_mask) & ~NETIF_F_ETHTOOL_BITS) {
  213. GENL_SET_ERR_MSG(info, "attempt to change non-ethtool features");
  214. ret = -EINVAL;
  215. goto out_rtnl;
  216. }
  217. /* set req_wanted bits not in req_mask from old_wanted */
  218. bitmap_and(req_wanted, req_wanted, req_mask, NETDEV_FEATURE_COUNT);
  219. bitmap_andnot(new_wanted, old_wanted, req_mask, NETDEV_FEATURE_COUNT);
  220. bitmap_or(req_wanted, new_wanted, req_wanted, NETDEV_FEATURE_COUNT);
  221. if (!bitmap_equal(req_wanted, old_wanted, NETDEV_FEATURE_COUNT)) {
  222. dev->wanted_features &= ~dev->hw_features;
  223. dev->wanted_features |= ethnl_bitmap_to_features(req_wanted) & dev->hw_features;
  224. __netdev_update_features(dev);
  225. }
  226. ethnl_features_to_bitmap(new_active, dev->features);
  227. mod = !bitmap_equal(old_active, new_active, NETDEV_FEATURE_COUNT);
  228. ret = 0;
  229. if (!(req_info.flags & ETHTOOL_FLAG_OMIT_REPLY)) {
  230. bool compact = req_info.flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  231. bitmap_xor(wanted_diff_mask, req_wanted, new_active,
  232. NETDEV_FEATURE_COUNT);
  233. bitmap_xor(active_diff_mask, old_active, new_active,
  234. NETDEV_FEATURE_COUNT);
  235. bitmap_and(wanted_diff_mask, wanted_diff_mask, req_mask,
  236. NETDEV_FEATURE_COUNT);
  237. bitmap_and(req_wanted, req_wanted, wanted_diff_mask,
  238. NETDEV_FEATURE_COUNT);
  239. bitmap_and(new_active, new_active, active_diff_mask,
  240. NETDEV_FEATURE_COUNT);
  241. ret = features_send_reply(dev, info, req_wanted,
  242. wanted_diff_mask, new_active,
  243. active_diff_mask, compact);
  244. }
  245. if (mod)
  246. netdev_features_change(dev);
  247. out_rtnl:
  248. rtnl_unlock();
  249. ethnl_parse_header_dev_put(&req_info);
  250. return ret;
  251. }