features.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. bitmap_zero(dest, NETDEV_FEATURE_COUNT);
  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. ethnl_features_to_bitmap(old_active, dev->features);
  207. ethnl_features_to_bitmap(old_wanted, dev->wanted_features);
  208. ret = ethnl_parse_bitset(req_wanted, req_mask, NETDEV_FEATURE_COUNT,
  209. tb[ETHTOOL_A_FEATURES_WANTED],
  210. netdev_features_strings, info->extack);
  211. if (ret < 0)
  212. goto out_rtnl;
  213. if (ethnl_bitmap_to_features(req_mask) & ~NETIF_F_ETHTOOL_BITS) {
  214. GENL_SET_ERR_MSG(info, "attempt to change non-ethtool features");
  215. ret = -EINVAL;
  216. goto out_rtnl;
  217. }
  218. /* set req_wanted bits not in req_mask from old_wanted */
  219. bitmap_and(req_wanted, req_wanted, req_mask, NETDEV_FEATURE_COUNT);
  220. bitmap_andnot(new_wanted, old_wanted, req_mask, NETDEV_FEATURE_COUNT);
  221. bitmap_or(req_wanted, new_wanted, req_wanted, NETDEV_FEATURE_COUNT);
  222. if (!bitmap_equal(req_wanted, old_wanted, NETDEV_FEATURE_COUNT)) {
  223. dev->wanted_features &= ~dev->hw_features;
  224. dev->wanted_features |= ethnl_bitmap_to_features(req_wanted) & dev->hw_features;
  225. __netdev_update_features(dev);
  226. }
  227. ethnl_features_to_bitmap(new_active, dev->features);
  228. mod = !bitmap_equal(old_active, new_active, NETDEV_FEATURE_COUNT);
  229. ret = 0;
  230. if (!(req_info.flags & ETHTOOL_FLAG_OMIT_REPLY)) {
  231. bool compact = req_info.flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  232. bitmap_xor(wanted_diff_mask, req_wanted, new_active,
  233. NETDEV_FEATURE_COUNT);
  234. bitmap_xor(active_diff_mask, old_active, new_active,
  235. NETDEV_FEATURE_COUNT);
  236. bitmap_and(wanted_diff_mask, wanted_diff_mask, req_mask,
  237. NETDEV_FEATURE_COUNT);
  238. bitmap_and(req_wanted, req_wanted, wanted_diff_mask,
  239. NETDEV_FEATURE_COUNT);
  240. bitmap_and(new_active, new_active, active_diff_mask,
  241. NETDEV_FEATURE_COUNT);
  242. ret = features_send_reply(dev, info, req_wanted,
  243. wanted_diff_mask, new_active,
  244. active_diff_mask, compact);
  245. }
  246. if (mod)
  247. netdev_features_change(dev);
  248. out_rtnl:
  249. rtnl_unlock();
  250. dev_put(dev);
  251. return ret;
  252. }