wol.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. struct wol_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct wol_reply_data {
  9. struct ethnl_reply_data base;
  10. struct ethtool_wolinfo wol;
  11. bool show_sopass;
  12. };
  13. #define WOL_REPDATA(__reply_base) \
  14. container_of(__reply_base, struct wol_reply_data, base)
  15. const struct nla_policy ethnl_wol_get_policy[] = {
  16. [ETHTOOL_A_WOL_HEADER] =
  17. NLA_POLICY_NESTED(ethnl_header_policy),
  18. };
  19. static int wol_prepare_data(const struct ethnl_req_info *req_base,
  20. struct ethnl_reply_data *reply_base,
  21. struct genl_info *info)
  22. {
  23. struct wol_reply_data *data = WOL_REPDATA(reply_base);
  24. struct net_device *dev = reply_base->dev;
  25. int ret;
  26. if (!dev->ethtool_ops->get_wol)
  27. return -EOPNOTSUPP;
  28. ret = ethnl_ops_begin(dev);
  29. if (ret < 0)
  30. return ret;
  31. dev->ethtool_ops->get_wol(dev, &data->wol);
  32. ethnl_ops_complete(dev);
  33. /* do not include password in notifications */
  34. data->show_sopass = info && (data->wol.supported & WAKE_MAGICSECURE);
  35. return 0;
  36. }
  37. static int wol_reply_size(const struct ethnl_req_info *req_base,
  38. const struct ethnl_reply_data *reply_base)
  39. {
  40. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  41. const struct wol_reply_data *data = WOL_REPDATA(reply_base);
  42. int len;
  43. len = ethnl_bitset32_size(&data->wol.wolopts, &data->wol.supported,
  44. WOL_MODE_COUNT, wol_mode_names, compact);
  45. if (len < 0)
  46. return len;
  47. if (data->show_sopass)
  48. len += nla_total_size(sizeof(data->wol.sopass));
  49. return len;
  50. }
  51. static int wol_fill_reply(struct sk_buff *skb,
  52. const struct ethnl_req_info *req_base,
  53. const struct ethnl_reply_data *reply_base)
  54. {
  55. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  56. const struct wol_reply_data *data = WOL_REPDATA(reply_base);
  57. int ret;
  58. ret = ethnl_put_bitset32(skb, ETHTOOL_A_WOL_MODES, &data->wol.wolopts,
  59. &data->wol.supported, WOL_MODE_COUNT,
  60. wol_mode_names, compact);
  61. if (ret < 0)
  62. return ret;
  63. if (data->show_sopass &&
  64. nla_put(skb, ETHTOOL_A_WOL_SOPASS, sizeof(data->wol.sopass),
  65. data->wol.sopass))
  66. return -EMSGSIZE;
  67. return 0;
  68. }
  69. const struct ethnl_request_ops ethnl_wol_request_ops = {
  70. .request_cmd = ETHTOOL_MSG_WOL_GET,
  71. .reply_cmd = ETHTOOL_MSG_WOL_GET_REPLY,
  72. .hdr_attr = ETHTOOL_A_WOL_HEADER,
  73. .req_info_size = sizeof(struct wol_req_info),
  74. .reply_data_size = sizeof(struct wol_reply_data),
  75. .prepare_data = wol_prepare_data,
  76. .reply_size = wol_reply_size,
  77. .fill_reply = wol_fill_reply,
  78. };
  79. /* WOL_SET */
  80. const struct nla_policy ethnl_wol_set_policy[] = {
  81. [ETHTOOL_A_WOL_HEADER] =
  82. NLA_POLICY_NESTED(ethnl_header_policy),
  83. [ETHTOOL_A_WOL_MODES] = { .type = NLA_NESTED },
  84. [ETHTOOL_A_WOL_SOPASS] = { .type = NLA_BINARY,
  85. .len = SOPASS_MAX },
  86. };
  87. int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info)
  88. {
  89. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  90. struct ethnl_req_info req_info = {};
  91. struct nlattr **tb = info->attrs;
  92. struct net_device *dev;
  93. bool mod = false;
  94. int ret;
  95. ret = ethnl_parse_header_dev_get(&req_info, tb[ETHTOOL_A_WOL_HEADER],
  96. genl_info_net(info), info->extack,
  97. true);
  98. if (ret < 0)
  99. return ret;
  100. dev = req_info.dev;
  101. ret = -EOPNOTSUPP;
  102. if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
  103. goto out_dev;
  104. rtnl_lock();
  105. ret = ethnl_ops_begin(dev);
  106. if (ret < 0)
  107. goto out_rtnl;
  108. dev->ethtool_ops->get_wol(dev, &wol);
  109. ret = ethnl_update_bitset32(&wol.wolopts, WOL_MODE_COUNT,
  110. tb[ETHTOOL_A_WOL_MODES], wol_mode_names,
  111. info->extack, &mod);
  112. if (ret < 0)
  113. goto out_ops;
  114. if (wol.wolopts & ~wol.supported) {
  115. NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_WOL_MODES],
  116. "cannot enable unsupported WoL mode");
  117. ret = -EINVAL;
  118. goto out_ops;
  119. }
  120. if (tb[ETHTOOL_A_WOL_SOPASS]) {
  121. if (!(wol.supported & WAKE_MAGICSECURE)) {
  122. NL_SET_ERR_MSG_ATTR(info->extack,
  123. tb[ETHTOOL_A_WOL_SOPASS],
  124. "magicsecure not supported, cannot set password");
  125. ret = -EINVAL;
  126. goto out_ops;
  127. }
  128. ethnl_update_binary(wol.sopass, sizeof(wol.sopass),
  129. tb[ETHTOOL_A_WOL_SOPASS], &mod);
  130. }
  131. if (!mod)
  132. goto out_ops;
  133. ret = dev->ethtool_ops->set_wol(dev, &wol);
  134. if (ret)
  135. goto out_ops;
  136. dev->wol_enabled = !!wol.wolopts;
  137. ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
  138. out_ops:
  139. ethnl_ops_complete(dev);
  140. out_rtnl:
  141. rtnl_unlock();
  142. out_dev:
  143. dev_put(dev);
  144. return ret;
  145. }