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