features.c 9.0 KB

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