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 2551ea22b0SMichal Kubecek static int wol_prepare_data(const struct ethnl_req_info *req_base, 2651ea22b0SMichal Kubecek struct ethnl_reply_data *reply_base, 2751ea22b0SMichal Kubecek 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 */ 4267bffa79SMichal Kubecek data->show_sopass = info && (data->wol.supported & WAKE_MAGICSECURE); 4351ea22b0SMichal Kubecek 4451ea22b0SMichal Kubecek return 0; 4551ea22b0SMichal Kubecek } 4651ea22b0SMichal Kubecek 4751ea22b0SMichal Kubecek static int wol_reply_size(const struct ethnl_req_info *req_base, 4851ea22b0SMichal Kubecek const struct ethnl_reply_data *reply_base) 4951ea22b0SMichal Kubecek { 5051ea22b0SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 5151ea22b0SMichal Kubecek const struct wol_reply_data *data = WOL_REPDATA(reply_base); 5251ea22b0SMichal Kubecek int len; 5351ea22b0SMichal Kubecek 5451ea22b0SMichal Kubecek len = ethnl_bitset32_size(&data->wol.wolopts, &data->wol.supported, 5551ea22b0SMichal Kubecek WOL_MODE_COUNT, wol_mode_names, compact); 5651ea22b0SMichal Kubecek if (len < 0) 5751ea22b0SMichal Kubecek return len; 5851ea22b0SMichal Kubecek if (data->show_sopass) 5951ea22b0SMichal Kubecek len += nla_total_size(sizeof(data->wol.sopass)); 6051ea22b0SMichal Kubecek 6151ea22b0SMichal Kubecek return len; 6251ea22b0SMichal Kubecek } 6351ea22b0SMichal Kubecek 6451ea22b0SMichal Kubecek static int wol_fill_reply(struct sk_buff *skb, 6551ea22b0SMichal Kubecek const struct ethnl_req_info *req_base, 6651ea22b0SMichal Kubecek const struct ethnl_reply_data *reply_base) 6751ea22b0SMichal Kubecek { 6851ea22b0SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 6951ea22b0SMichal Kubecek const struct wol_reply_data *data = WOL_REPDATA(reply_base); 7051ea22b0SMichal Kubecek int ret; 7151ea22b0SMichal Kubecek 7251ea22b0SMichal Kubecek ret = ethnl_put_bitset32(skb, ETHTOOL_A_WOL_MODES, &data->wol.wolopts, 7351ea22b0SMichal Kubecek &data->wol.supported, WOL_MODE_COUNT, 7451ea22b0SMichal Kubecek wol_mode_names, compact); 7551ea22b0SMichal Kubecek if (ret < 0) 7651ea22b0SMichal Kubecek return ret; 7751ea22b0SMichal Kubecek if (data->show_sopass && 7851ea22b0SMichal Kubecek nla_put(skb, ETHTOOL_A_WOL_SOPASS, sizeof(data->wol.sopass), 7951ea22b0SMichal Kubecek data->wol.sopass)) 8051ea22b0SMichal Kubecek return -EMSGSIZE; 8151ea22b0SMichal Kubecek 8251ea22b0SMichal Kubecek return 0; 8351ea22b0SMichal Kubecek } 8451ea22b0SMichal Kubecek 8551ea22b0SMichal Kubecek const struct ethnl_request_ops ethnl_wol_request_ops = { 8651ea22b0SMichal Kubecek .request_cmd = ETHTOOL_MSG_WOL_GET, 8751ea22b0SMichal Kubecek .reply_cmd = ETHTOOL_MSG_WOL_GET_REPLY, 8851ea22b0SMichal Kubecek .hdr_attr = ETHTOOL_A_WOL_HEADER, 8951ea22b0SMichal Kubecek .req_info_size = sizeof(struct wol_req_info), 9051ea22b0SMichal Kubecek .reply_data_size = sizeof(struct wol_reply_data), 9151ea22b0SMichal Kubecek 9251ea22b0SMichal Kubecek .prepare_data = wol_prepare_data, 9351ea22b0SMichal Kubecek .reply_size = wol_reply_size, 9451ea22b0SMichal Kubecek .fill_reply = wol_fill_reply, 9551ea22b0SMichal Kubecek }; 968d425b19SMichal Kubecek 978d425b19SMichal Kubecek /* WOL_SET */ 988d425b19SMichal Kubecek 99ff419afaSJakub Kicinski const struct nla_policy ethnl_wol_set_policy[] = { 100329d9c33SJakub Kicinski [ETHTOOL_A_WOL_HEADER] = 101329d9c33SJakub Kicinski NLA_POLICY_NESTED(ethnl_header_policy), 1028d425b19SMichal Kubecek [ETHTOOL_A_WOL_MODES] = { .type = NLA_NESTED }, 1038d425b19SMichal Kubecek [ETHTOOL_A_WOL_SOPASS] = { .type = NLA_BINARY, 1048d425b19SMichal Kubecek .len = SOPASS_MAX }, 1058d425b19SMichal Kubecek }; 1068d425b19SMichal Kubecek 1078d425b19SMichal Kubecek int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info) 1088d425b19SMichal Kubecek { 1098d425b19SMichal Kubecek struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 1108d425b19SMichal Kubecek struct ethnl_req_info req_info = {}; 1115028588bSJakub Kicinski struct nlattr **tb = info->attrs; 1128d425b19SMichal Kubecek struct net_device *dev; 1138d425b19SMichal Kubecek bool mod = false; 1148d425b19SMichal Kubecek int ret; 1158d425b19SMichal Kubecek 11698130546SMichal Kubecek ret = ethnl_parse_header_dev_get(&req_info, tb[ETHTOOL_A_WOL_HEADER], 11798130546SMichal Kubecek genl_info_net(info), info->extack, 11898130546SMichal Kubecek true); 1198d425b19SMichal Kubecek if (ret < 0) 1208d425b19SMichal Kubecek return ret; 1218d425b19SMichal Kubecek dev = req_info.dev; 1222f599ec4SMichal Kubecek ret = -EOPNOTSUPP; 1238d425b19SMichal Kubecek if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol) 1242f599ec4SMichal Kubecek goto out_dev; 1258d425b19SMichal Kubecek 1268d425b19SMichal Kubecek rtnl_lock(); 1278d425b19SMichal Kubecek ret = ethnl_ops_begin(dev); 1288d425b19SMichal Kubecek if (ret < 0) 1298d425b19SMichal Kubecek goto out_rtnl; 1308d425b19SMichal Kubecek 1318d425b19SMichal Kubecek dev->ethtool_ops->get_wol(dev, &wol); 1328d425b19SMichal Kubecek ret = ethnl_update_bitset32(&wol.wolopts, WOL_MODE_COUNT, 1338d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_MODES], wol_mode_names, 1348d425b19SMichal Kubecek info->extack, &mod); 1358d425b19SMichal Kubecek if (ret < 0) 1368d425b19SMichal Kubecek goto out_ops; 1378d425b19SMichal Kubecek if (wol.wolopts & ~wol.supported) { 1388d425b19SMichal Kubecek NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_WOL_MODES], 1398d425b19SMichal Kubecek "cannot enable unsupported WoL mode"); 1408d425b19SMichal Kubecek ret = -EINVAL; 1418d425b19SMichal Kubecek goto out_ops; 1428d425b19SMichal Kubecek } 1438d425b19SMichal Kubecek if (tb[ETHTOOL_A_WOL_SOPASS]) { 1448d425b19SMichal Kubecek if (!(wol.supported & WAKE_MAGICSECURE)) { 1458d425b19SMichal Kubecek NL_SET_ERR_MSG_ATTR(info->extack, 1468d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_SOPASS], 1478d425b19SMichal Kubecek "magicsecure not supported, cannot set password"); 1488d425b19SMichal Kubecek ret = -EINVAL; 1498d425b19SMichal Kubecek goto out_ops; 1508d425b19SMichal Kubecek } 1518d425b19SMichal Kubecek ethnl_update_binary(wol.sopass, sizeof(wol.sopass), 1528d425b19SMichal Kubecek tb[ETHTOOL_A_WOL_SOPASS], &mod); 1538d425b19SMichal Kubecek } 1548d425b19SMichal Kubecek 1558d425b19SMichal Kubecek if (!mod) 1568d425b19SMichal Kubecek goto out_ops; 1578d425b19SMichal Kubecek ret = dev->ethtool_ops->set_wol(dev, &wol); 1588d425b19SMichal Kubecek if (ret) 1598d425b19SMichal Kubecek goto out_ops; 1608d425b19SMichal Kubecek dev->wol_enabled = !!wol.wolopts; 16167bffa79SMichal Kubecek ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL); 1628d425b19SMichal Kubecek 1638d425b19SMichal Kubecek out_ops: 1648d425b19SMichal Kubecek ethnl_ops_complete(dev); 1658d425b19SMichal Kubecek out_rtnl: 1668d425b19SMichal Kubecek rtnl_unlock(); 1672f599ec4SMichal Kubecek out_dev: 1688d425b19SMichal Kubecek dev_put(dev); 1698d425b19SMichal Kubecek return ret; 1708d425b19SMichal Kubecek } 171