privflags.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. struct privflags_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct privflags_reply_data {
  9. struct ethnl_reply_data base;
  10. const char (*priv_flag_names)[ETH_GSTRING_LEN];
  11. unsigned int n_priv_flags;
  12. u32 priv_flags;
  13. };
  14. #define PRIVFLAGS_REPDATA(__reply_base) \
  15. container_of(__reply_base, struct privflags_reply_data, base)
  16. const struct nla_policy ethnl_privflags_get_policy[] = {
  17. [ETHTOOL_A_PRIVFLAGS_HEADER] =
  18. NLA_POLICY_NESTED(ethnl_header_policy),
  19. };
  20. static int ethnl_get_priv_flags_info(struct net_device *dev,
  21. unsigned int *count,
  22. const char (**names)[ETH_GSTRING_LEN])
  23. {
  24. const struct ethtool_ops *ops = dev->ethtool_ops;
  25. int nflags;
  26. nflags = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  27. if (nflags < 0)
  28. return nflags;
  29. if (names) {
  30. *names = kcalloc(nflags, ETH_GSTRING_LEN, GFP_KERNEL);
  31. if (!*names)
  32. return -ENOMEM;
  33. ops->get_strings(dev, ETH_SS_PRIV_FLAGS, (u8 *)*names);
  34. }
  35. /* We can pass more than 32 private flags to userspace via netlink but
  36. * we cannot get more with ethtool_ops::get_priv_flags(). Note that we
  37. * must not adjust nflags before allocating the space for flag names
  38. * as the buffer must be large enough for all flags.
  39. */
  40. if (WARN_ONCE(nflags > 32,
  41. "device %s reports more than 32 private flags (%d)\n",
  42. netdev_name(dev), nflags))
  43. nflags = 32;
  44. *count = nflags;
  45. return 0;
  46. }
  47. static int privflags_prepare_data(const struct ethnl_req_info *req_base,
  48. struct ethnl_reply_data *reply_base,
  49. struct genl_info *info)
  50. {
  51. struct privflags_reply_data *data = PRIVFLAGS_REPDATA(reply_base);
  52. struct net_device *dev = reply_base->dev;
  53. const char (*names)[ETH_GSTRING_LEN];
  54. const struct ethtool_ops *ops;
  55. unsigned int nflags;
  56. int ret;
  57. ops = dev->ethtool_ops;
  58. if (!ops->get_priv_flags || !ops->get_sset_count || !ops->get_strings)
  59. return -EOPNOTSUPP;
  60. ret = ethnl_ops_begin(dev);
  61. if (ret < 0)
  62. return ret;
  63. ret = ethnl_get_priv_flags_info(dev, &nflags, &names);
  64. if (ret < 0)
  65. goto out_ops;
  66. data->priv_flags = ops->get_priv_flags(dev);
  67. data->priv_flag_names = names;
  68. data->n_priv_flags = nflags;
  69. out_ops:
  70. ethnl_ops_complete(dev);
  71. return ret;
  72. }
  73. static int privflags_reply_size(const struct ethnl_req_info *req_base,
  74. const struct ethnl_reply_data *reply_base)
  75. {
  76. const struct privflags_reply_data *data = PRIVFLAGS_REPDATA(reply_base);
  77. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  78. const u32 all_flags = ~(u32)0 >> (32 - data->n_priv_flags);
  79. return ethnl_bitset32_size(&data->priv_flags, &all_flags,
  80. data->n_priv_flags,
  81. data->priv_flag_names, compact);
  82. }
  83. static int privflags_fill_reply(struct sk_buff *skb,
  84. const struct ethnl_req_info *req_base,
  85. const struct ethnl_reply_data *reply_base)
  86. {
  87. const struct privflags_reply_data *data = PRIVFLAGS_REPDATA(reply_base);
  88. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  89. const u32 all_flags = ~(u32)0 >> (32 - data->n_priv_flags);
  90. return ethnl_put_bitset32(skb, ETHTOOL_A_PRIVFLAGS_FLAGS,
  91. &data->priv_flags, &all_flags,
  92. data->n_priv_flags, data->priv_flag_names,
  93. compact);
  94. }
  95. static void privflags_cleanup_data(struct ethnl_reply_data *reply_data)
  96. {
  97. struct privflags_reply_data *data = PRIVFLAGS_REPDATA(reply_data);
  98. kfree(data->priv_flag_names);
  99. }
  100. const struct ethnl_request_ops ethnl_privflags_request_ops = {
  101. .request_cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
  102. .reply_cmd = ETHTOOL_MSG_PRIVFLAGS_GET_REPLY,
  103. .hdr_attr = ETHTOOL_A_PRIVFLAGS_HEADER,
  104. .req_info_size = sizeof(struct privflags_req_info),
  105. .reply_data_size = sizeof(struct privflags_reply_data),
  106. .prepare_data = privflags_prepare_data,
  107. .reply_size = privflags_reply_size,
  108. .fill_reply = privflags_fill_reply,
  109. .cleanup_data = privflags_cleanup_data,
  110. };
  111. /* PRIVFLAGS_SET */
  112. const struct nla_policy ethnl_privflags_set_policy[] = {
  113. [ETHTOOL_A_PRIVFLAGS_HEADER] =
  114. NLA_POLICY_NESTED(ethnl_header_policy),
  115. [ETHTOOL_A_PRIVFLAGS_FLAGS] = { .type = NLA_NESTED },
  116. };
  117. int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info)
  118. {
  119. const char (*names)[ETH_GSTRING_LEN] = NULL;
  120. struct ethnl_req_info req_info = {};
  121. struct nlattr **tb = info->attrs;
  122. const struct ethtool_ops *ops;
  123. struct net_device *dev;
  124. unsigned int nflags;
  125. bool mod = false;
  126. bool compact;
  127. u32 flags;
  128. int ret;
  129. if (!tb[ETHTOOL_A_PRIVFLAGS_FLAGS])
  130. return -EINVAL;
  131. ret = ethnl_bitset_is_compact(tb[ETHTOOL_A_PRIVFLAGS_FLAGS], &compact);
  132. if (ret < 0)
  133. return ret;
  134. ret = ethnl_parse_header_dev_get(&req_info,
  135. tb[ETHTOOL_A_PRIVFLAGS_HEADER],
  136. genl_info_net(info), info->extack,
  137. true);
  138. if (ret < 0)
  139. return ret;
  140. dev = req_info.dev;
  141. ops = dev->ethtool_ops;
  142. ret = -EOPNOTSUPP;
  143. if (!ops->get_priv_flags || !ops->set_priv_flags ||
  144. !ops->get_sset_count || !ops->get_strings)
  145. goto out_dev;
  146. rtnl_lock();
  147. ret = ethnl_ops_begin(dev);
  148. if (ret < 0)
  149. goto out_rtnl;
  150. ret = ethnl_get_priv_flags_info(dev, &nflags, compact ? NULL : &names);
  151. if (ret < 0)
  152. goto out_ops;
  153. flags = ops->get_priv_flags(dev);
  154. ret = ethnl_update_bitset32(&flags, nflags,
  155. tb[ETHTOOL_A_PRIVFLAGS_FLAGS], names,
  156. info->extack, &mod);
  157. if (ret < 0 || !mod)
  158. goto out_free;
  159. ret = ops->set_priv_flags(dev, flags);
  160. if (ret < 0)
  161. goto out_free;
  162. ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
  163. out_free:
  164. kfree(names);
  165. out_ops:
  166. ethnl_ops_complete(dev);
  167. out_rtnl:
  168. rtnl_unlock();
  169. out_dev:
  170. ethnl_parse_header_dev_put(&req_info);
  171. return ret;
  172. }