151ea22b0SMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only
251ea22b0SMichal Kubecek
351ea22b0SMichal Kubecek #include "netlink.h"
451ea22b0SMichal Kubecek #include "common.h"
551ea22b0SMichal Kubecek #include "bitset.h"
651ea22b0SMichal Kubecek
751ea22b0SMichal Kubecek struct wol_req_info {
851ea22b0SMichal Kubecek struct ethnl_req_info base;
951ea22b0SMichal Kubecek };
1051ea22b0SMichal Kubecek
1151ea22b0SMichal Kubecek struct wol_reply_data {
1251ea22b0SMichal Kubecek struct ethnl_reply_data base;
1351ea22b0SMichal Kubecek struct ethtool_wolinfo wol;
1451ea22b0SMichal Kubecek bool show_sopass;
1551ea22b0SMichal Kubecek };
1651ea22b0SMichal Kubecek
1751ea22b0SMichal Kubecek #define WOL_REPDATA(__reply_base) \
1851ea22b0SMichal Kubecek container_of(__reply_base, struct wol_reply_data, base)
1951ea22b0SMichal Kubecek
20ff419afaSJakub Kicinski const struct nla_policy ethnl_wol_get_policy[] = {
21329d9c33SJakub Kicinski [ETHTOOL_A_WOL_HEADER] =
22329d9c33SJakub Kicinski NLA_POLICY_NESTED(ethnl_header_policy),
2351ea22b0SMichal Kubecek };
2451ea22b0SMichal Kubecek
wol_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,const struct genl_info * info)2551ea22b0SMichal Kubecek static int wol_prepare_data(const struct ethnl_req_info *req_base,
2651ea22b0SMichal Kubecek struct ethnl_reply_data *reply_base,
27*f946270dSJakub Kicinski const struct genl_info *info)
2851ea22b0SMichal Kubecek {
2951ea22b0SMichal Kubecek struct wol_reply_data *data = WOL_REPDATA(reply_base);
3051ea22b0SMichal Kubecek struct net_device *dev = reply_base->dev;
3151ea22b0SMichal Kubecek int ret;
3251ea22b0SMichal Kubecek
3351ea22b0SMichal Kubecek if (!dev->ethtool_ops->get_wol)
3451ea22b0SMichal Kubecek return -EOPNOTSUPP;
3551ea22b0SMichal Kubecek
3651ea22b0SMichal Kubecek ret = ethnl_ops_begin(dev);
3751ea22b0SMichal Kubecek if (ret < 0)
3851ea22b0SMichal Kubecek return ret;
3951ea22b0SMichal Kubecek dev->ethtool_ops->get_wol(dev, &data->wol);
4051ea22b0SMichal Kubecek ethnl_ops_complete(dev);
4167bffa79SMichal Kubecek /* do not include password in notifications */
42*f946270dSJakub Kicinski data->show_sopass = !genl_info_is_ntf(info) &&
43*f946270dSJakub Kicinski (data->wol.supported & WAKE_MAGICSECURE);
4451ea22b0SMichal Kubecek
4551ea22b0SMichal Kubecek return 0;
4651ea22b0SMichal Kubecek }
4751ea22b0SMichal Kubecek
wol_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)4851ea22b0SMichal Kubecek static int wol_reply_size(const struct ethnl_req_info *req_base,
4951ea22b0SMichal Kubecek const struct ethnl_reply_data *reply_base)
5051ea22b0SMichal Kubecek {
5151ea22b0SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
5251ea22b0SMichal Kubecek const struct wol_reply_data *data = WOL_REPDATA(reply_base);
5351ea22b0SMichal Kubecek int len;
5451ea22b0SMichal Kubecek
5551ea22b0SMichal Kubecek len = ethnl_bitset32_size(&data->wol.wolopts, &data->wol.supported,
5651ea22b0SMichal Kubecek WOL_MODE_COUNT, wol_mode_names, compact);
5751ea22b0SMichal Kubecek if (len < 0)
5851ea22b0SMichal Kubecek return len;
5951ea22b0SMichal Kubecek if (data->show_sopass)
6051ea22b0SMichal Kubecek len += nla_total_size(sizeof(data->wol.sopass));
6151ea22b0SMichal Kubecek
6251ea22b0SMichal Kubecek return len;
6351ea22b0SMichal Kubecek }
6451ea22b0SMichal Kubecek
wol_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)6551ea22b0SMichal Kubecek static int wol_fill_reply(struct sk_buff *skb,
6651ea22b0SMichal Kubecek const struct ethnl_req_info *req_base,
6751ea22b0SMichal Kubecek const struct ethnl_reply_data *reply_base)
6851ea22b0SMichal Kubecek {
6951ea22b0SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
7051ea22b0SMichal Kubecek const struct wol_reply_data *data = WOL_REPDATA(reply_base);
7151ea22b0SMichal Kubecek int ret;
7251ea22b0SMichal Kubecek
7351ea22b0SMichal Kubecek ret = ethnl_put_bitset32(skb, ETHTOOL_A_WOL_MODES, &data->wol.wolopts,
7451ea22b0SMichal Kubecek &data->wol.supported, WOL_MODE_COUNT,
7551ea22b0SMichal Kubecek wol_mode_names, compact);
7651ea22b0SMichal Kubecek if (ret < 0)
7751ea22b0SMichal Kubecek return ret;
7851ea22b0SMichal Kubecek if (data->show_sopass &&
7951ea22b0SMichal Kubecek nla_put(skb, ETHTOOL_A_WOL_SOPASS, sizeof(data->wol.sopass),
8051ea22b0SMichal Kubecek data->wol.sopass))
8151ea22b0SMichal Kubecek return -EMSGSIZE;
8251ea22b0SMichal Kubecek
8351ea22b0SMichal Kubecek return 0;
8451ea22b0SMichal Kubecek }
8551ea22b0SMichal Kubecek
868d425b19SMichal Kubecek /* WOL_SET */
878d425b19SMichal Kubecek
88ff419afaSJakub Kicinski const struct nla_policy ethnl_wol_set_policy[] = {
89329d9c33SJakub Kicinski [ETHTOOL_A_WOL_HEADER] =
90329d9c33SJakub Kicinski NLA_POLICY_NESTED(ethnl_header_policy),
918d425b19SMichal Kubecek [ETHTOOL_A_WOL_MODES] = { .type = NLA_NESTED },
928d425b19SMichal Kubecek [ETHTOOL_A_WOL_SOPASS] = { .type = NLA_BINARY,
938d425b19SMichal Kubecek .len = SOPASS_MAX },
948d425b19SMichal Kubecek };
958d425b19SMichal Kubecek
9604007961SJakub Kicinski static int
ethnl_set_wol_validate(struct ethnl_req_info * req_info,struct genl_info * info)9704007961SJakub Kicinski ethnl_set_wol_validate(struct ethnl_req_info *req_info, struct genl_info *info)
9804007961SJakub Kicinski {
9904007961SJakub Kicinski const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
10004007961SJakub Kicinski
10104007961SJakub Kicinski return ops->get_wol && ops->set_wol ? 1 : -EOPNOTSUPP;
10204007961SJakub Kicinski }
10304007961SJakub Kicinski
10404007961SJakub Kicinski static int
ethnl_set_wol(struct ethnl_req_info * req_info,struct genl_info * info)10504007961SJakub Kicinski ethnl_set_wol(struct ethnl_req_info *req_info, struct genl_info *info)
1068d425b19SMichal Kubecek {
1078d425b19SMichal Kubecek struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
10804007961SJakub Kicinski struct net_device *dev = req_info->dev;
1095028588bSJakub Kicinski struct nlattr **tb = info->attrs;
1108d425b19SMichal Kubecek bool mod = false;
1118d425b19SMichal Kubecek int ret;
1128d425b19SMichal Kubecek
1138d425b19SMichal Kubecek dev->ethtool_ops->get_wol(dev, &wol);
1148d425b19SMichal Kubecek ret = ethnl_update_bitset32(&wol.wolopts, WOL_MODE_COUNT,
1158d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_MODES], wol_mode_names,
1168d425b19SMichal Kubecek info->extack, &mod);
1178d425b19SMichal Kubecek if (ret < 0)
11804007961SJakub Kicinski return ret;
1198d425b19SMichal Kubecek if (wol.wolopts & ~wol.supported) {
1208d425b19SMichal Kubecek NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_WOL_MODES],
1218d425b19SMichal Kubecek "cannot enable unsupported WoL mode");
12204007961SJakub Kicinski return -EINVAL;
1238d425b19SMichal Kubecek }
1248d425b19SMichal Kubecek if (tb[ETHTOOL_A_WOL_SOPASS]) {
1258d425b19SMichal Kubecek if (!(wol.supported & WAKE_MAGICSECURE)) {
1268d425b19SMichal Kubecek NL_SET_ERR_MSG_ATTR(info->extack,
1278d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_SOPASS],
1288d425b19SMichal Kubecek "magicsecure not supported, cannot set password");
12904007961SJakub Kicinski return -EINVAL;
1308d425b19SMichal Kubecek }
1318d425b19SMichal Kubecek ethnl_update_binary(wol.sopass, sizeof(wol.sopass),
1328d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_SOPASS], &mod);
1338d425b19SMichal Kubecek }
1348d425b19SMichal Kubecek
1358d425b19SMichal Kubecek if (!mod)
13604007961SJakub Kicinski return 0;
1378d425b19SMichal Kubecek ret = dev->ethtool_ops->set_wol(dev, &wol);
1388d425b19SMichal Kubecek if (ret)
1398d425b19SMichal Kubecek return ret;
14004007961SJakub Kicinski dev->wol_enabled = !!wol.wolopts;
14104007961SJakub Kicinski return 1;
1428d425b19SMichal Kubecek }
14304007961SJakub Kicinski
14404007961SJakub Kicinski const struct ethnl_request_ops ethnl_wol_request_ops = {
14504007961SJakub Kicinski .request_cmd = ETHTOOL_MSG_WOL_GET,
14604007961SJakub Kicinski .reply_cmd = ETHTOOL_MSG_WOL_GET_REPLY,
14704007961SJakub Kicinski .hdr_attr = ETHTOOL_A_WOL_HEADER,
14804007961SJakub Kicinski .req_info_size = sizeof(struct wol_req_info),
14904007961SJakub Kicinski .reply_data_size = sizeof(struct wol_reply_data),
15004007961SJakub Kicinski
15104007961SJakub Kicinski .prepare_data = wol_prepare_data,
15204007961SJakub Kicinski .reply_size = wol_reply_size,
15304007961SJakub Kicinski .fill_reply = wol_fill_reply,
15404007961SJakub Kicinski
15504007961SJakub Kicinski .set_validate = ethnl_set_wol_validate,
15604007961SJakub Kicinski .set = ethnl_set_wol,
15704007961SJakub Kicinski .set_ntf_cmd = ETHTOOL_MSG_WOL_NTF,
15804007961SJakub Kicinski };
159