privflags.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. const 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. /* PRIVFLAGS_SET */
  101. const struct nla_policy ethnl_privflags_set_policy[] = {
  102. [ETHTOOL_A_PRIVFLAGS_HEADER] =
  103. NLA_POLICY_NESTED(ethnl_header_policy),
  104. [ETHTOOL_A_PRIVFLAGS_FLAGS] = { .type = NLA_NESTED },
  105. };
  106. static int
  107. ethnl_set_privflags_validate(struct ethnl_req_info *req_info,
  108. struct genl_info *info)
  109. {
  110. const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
  111. if (!info->attrs[ETHTOOL_A_PRIVFLAGS_FLAGS])
  112. return -EINVAL;
  113. if (!ops->get_priv_flags || !ops->set_priv_flags ||
  114. !ops->get_sset_count || !ops->get_strings)
  115. return -EOPNOTSUPP;
  116. return 1;
  117. }
  118. static int
  119. ethnl_set_privflags(struct ethnl_req_info *req_info, struct genl_info *info)
  120. {
  121. const char (*names)[ETH_GSTRING_LEN] = NULL;
  122. struct net_device *dev = req_info->dev;
  123. struct nlattr **tb = info->attrs;
  124. unsigned int nflags;
  125. bool mod = false;
  126. bool compact;
  127. u32 flags;
  128. int ret;
  129. ret = ethnl_bitset_is_compact(tb[ETHTOOL_A_PRIVFLAGS_FLAGS], &compact);
  130. if (ret < 0)
  131. return ret;
  132. ret = ethnl_get_priv_flags_info(dev, &nflags, compact ? NULL : &names);
  133. if (ret < 0)
  134. return ret;
  135. flags = dev->ethtool_ops->get_priv_flags(dev);
  136. ret = ethnl_update_bitset32(&flags, nflags,
  137. tb[ETHTOOL_A_PRIVFLAGS_FLAGS], names,
  138. info->extack, &mod);
  139. if (ret < 0 || !mod)
  140. goto out_free;
  141. ret = dev->ethtool_ops->set_priv_flags(dev, flags);
  142. if (ret < 0)
  143. goto out_free;
  144. ret = 1;
  145. out_free:
  146. kfree(names);
  147. return ret;
  148. }
  149. const struct ethnl_request_ops ethnl_privflags_request_ops = {
  150. .request_cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
  151. .reply_cmd = ETHTOOL_MSG_PRIVFLAGS_GET_REPLY,
  152. .hdr_attr = ETHTOOL_A_PRIVFLAGS_HEADER,
  153. .req_info_size = sizeof(struct privflags_req_info),
  154. .reply_data_size = sizeof(struct privflags_reply_data),
  155. .prepare_data = privflags_prepare_data,
  156. .reply_size = privflags_reply_size,
  157. .fill_reply = privflags_fill_reply,
  158. .cleanup_data = privflags_cleanup_data,
  159. .set_validate = ethnl_set_privflags_validate,
  160. .set = ethnl_set_privflags,
  161. .set_ntf_cmd = ETHTOOL_MSG_PRIVFLAGS_NTF,
  162. };