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 features_req_info {
8*4882a593Smuzhiyun struct ethnl_req_info base;
9*4882a593Smuzhiyun };
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun struct features_reply_data {
12*4882a593Smuzhiyun struct ethnl_reply_data base;
13*4882a593Smuzhiyun u32 hw[ETHTOOL_DEV_FEATURE_WORDS];
14*4882a593Smuzhiyun u32 wanted[ETHTOOL_DEV_FEATURE_WORDS];
15*4882a593Smuzhiyun u32 active[ETHTOOL_DEV_FEATURE_WORDS];
16*4882a593Smuzhiyun u32 nochange[ETHTOOL_DEV_FEATURE_WORDS];
17*4882a593Smuzhiyun u32 all[ETHTOOL_DEV_FEATURE_WORDS];
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define FEATURES_REPDATA(__reply_base) \
21*4882a593Smuzhiyun container_of(__reply_base, struct features_reply_data, base)
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun const struct nla_policy ethnl_features_get_policy[] = {
24*4882a593Smuzhiyun [ETHTOOL_A_FEATURES_HEADER] =
25*4882a593Smuzhiyun NLA_POLICY_NESTED(ethnl_header_policy),
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
ethnl_features_to_bitmap32(u32 * dest,netdev_features_t src)28*4882a593Smuzhiyun static void ethnl_features_to_bitmap32(u32 *dest, netdev_features_t src)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun unsigned int i;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; i++)
33*4882a593Smuzhiyun dest[i] = src >> (32 * i);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
features_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,struct genl_info * info)36*4882a593Smuzhiyun static int features_prepare_data(const struct ethnl_req_info *req_base,
37*4882a593Smuzhiyun struct ethnl_reply_data *reply_base,
38*4882a593Smuzhiyun struct genl_info *info)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun struct features_reply_data *data = FEATURES_REPDATA(reply_base);
41*4882a593Smuzhiyun struct net_device *dev = reply_base->dev;
42*4882a593Smuzhiyun netdev_features_t all_features;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun ethnl_features_to_bitmap32(data->hw, dev->hw_features);
45*4882a593Smuzhiyun ethnl_features_to_bitmap32(data->wanted, dev->wanted_features);
46*4882a593Smuzhiyun ethnl_features_to_bitmap32(data->active, dev->features);
47*4882a593Smuzhiyun ethnl_features_to_bitmap32(data->nochange, NETIF_F_NEVER_CHANGE);
48*4882a593Smuzhiyun all_features = GENMASK_ULL(NETDEV_FEATURE_COUNT - 1, 0);
49*4882a593Smuzhiyun ethnl_features_to_bitmap32(data->all, all_features);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
features_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)54*4882a593Smuzhiyun static int features_reply_size(const struct ethnl_req_info *req_base,
55*4882a593Smuzhiyun const struct ethnl_reply_data *reply_base)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
58*4882a593Smuzhiyun bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
59*4882a593Smuzhiyun unsigned int len = 0;
60*4882a593Smuzhiyun int ret;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun ret = ethnl_bitset32_size(data->hw, data->all, NETDEV_FEATURE_COUNT,
63*4882a593Smuzhiyun netdev_features_strings, compact);
64*4882a593Smuzhiyun if (ret < 0)
65*4882a593Smuzhiyun return ret;
66*4882a593Smuzhiyun len += ret;
67*4882a593Smuzhiyun ret = ethnl_bitset32_size(data->wanted, NULL, NETDEV_FEATURE_COUNT,
68*4882a593Smuzhiyun netdev_features_strings, compact);
69*4882a593Smuzhiyun if (ret < 0)
70*4882a593Smuzhiyun return ret;
71*4882a593Smuzhiyun len += ret;
72*4882a593Smuzhiyun ret = ethnl_bitset32_size(data->active, NULL, NETDEV_FEATURE_COUNT,
73*4882a593Smuzhiyun netdev_features_strings, compact);
74*4882a593Smuzhiyun if (ret < 0)
75*4882a593Smuzhiyun return ret;
76*4882a593Smuzhiyun len += ret;
77*4882a593Smuzhiyun ret = ethnl_bitset32_size(data->nochange, NULL, NETDEV_FEATURE_COUNT,
78*4882a593Smuzhiyun netdev_features_strings, compact);
79*4882a593Smuzhiyun if (ret < 0)
80*4882a593Smuzhiyun return ret;
81*4882a593Smuzhiyun len += ret;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return len;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
features_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)86*4882a593Smuzhiyun static int features_fill_reply(struct sk_buff *skb,
87*4882a593Smuzhiyun const struct ethnl_req_info *req_base,
88*4882a593Smuzhiyun const struct ethnl_reply_data *reply_base)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
91*4882a593Smuzhiyun bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
92*4882a593Smuzhiyun int ret;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_HW, data->hw,
95*4882a593Smuzhiyun data->all, NETDEV_FEATURE_COUNT,
96*4882a593Smuzhiyun netdev_features_strings, compact);
97*4882a593Smuzhiyun if (ret < 0)
98*4882a593Smuzhiyun return ret;
99*4882a593Smuzhiyun ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_WANTED, data->wanted,
100*4882a593Smuzhiyun NULL, NETDEV_FEATURE_COUNT,
101*4882a593Smuzhiyun netdev_features_strings, compact);
102*4882a593Smuzhiyun if (ret < 0)
103*4882a593Smuzhiyun return ret;
104*4882a593Smuzhiyun ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_ACTIVE, data->active,
105*4882a593Smuzhiyun NULL, NETDEV_FEATURE_COUNT,
106*4882a593Smuzhiyun netdev_features_strings, compact);
107*4882a593Smuzhiyun if (ret < 0)
108*4882a593Smuzhiyun return ret;
109*4882a593Smuzhiyun return ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_NOCHANGE,
110*4882a593Smuzhiyun data->nochange, NULL, NETDEV_FEATURE_COUNT,
111*4882a593Smuzhiyun netdev_features_strings, compact);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun const struct ethnl_request_ops ethnl_features_request_ops = {
115*4882a593Smuzhiyun .request_cmd = ETHTOOL_MSG_FEATURES_GET,
116*4882a593Smuzhiyun .reply_cmd = ETHTOOL_MSG_FEATURES_GET_REPLY,
117*4882a593Smuzhiyun .hdr_attr = ETHTOOL_A_FEATURES_HEADER,
118*4882a593Smuzhiyun .req_info_size = sizeof(struct features_req_info),
119*4882a593Smuzhiyun .reply_data_size = sizeof(struct features_reply_data),
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun .prepare_data = features_prepare_data,
122*4882a593Smuzhiyun .reply_size = features_reply_size,
123*4882a593Smuzhiyun .fill_reply = features_fill_reply,
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* FEATURES_SET */
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun const struct nla_policy ethnl_features_set_policy[] = {
129*4882a593Smuzhiyun [ETHTOOL_A_FEATURES_HEADER] =
130*4882a593Smuzhiyun NLA_POLICY_NESTED(ethnl_header_policy),
131*4882a593Smuzhiyun [ETHTOOL_A_FEATURES_WANTED] = { .type = NLA_NESTED },
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
ethnl_features_to_bitmap(unsigned long * dest,netdev_features_t val)134*4882a593Smuzhiyun static void ethnl_features_to_bitmap(unsigned long *dest, netdev_features_t val)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT);
137*4882a593Smuzhiyun unsigned int i;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun bitmap_zero(dest, NETDEV_FEATURE_COUNT);
140*4882a593Smuzhiyun for (i = 0; i < words; i++)
141*4882a593Smuzhiyun dest[i] = (unsigned long)(val >> (i * BITS_PER_LONG));
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
ethnl_bitmap_to_features(unsigned long * src)144*4882a593Smuzhiyun static netdev_features_t ethnl_bitmap_to_features(unsigned long *src)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun const unsigned int nft_bits = sizeof(netdev_features_t) * BITS_PER_BYTE;
147*4882a593Smuzhiyun const unsigned int words = BITS_TO_LONGS(NETDEV_FEATURE_COUNT);
148*4882a593Smuzhiyun netdev_features_t ret = 0;
149*4882a593Smuzhiyun unsigned int i;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun for (i = 0; i < words; i++)
152*4882a593Smuzhiyun ret |= (netdev_features_t)(src[i]) << (i * BITS_PER_LONG);
153*4882a593Smuzhiyun ret &= ~(netdev_features_t)0 >> (nft_bits - NETDEV_FEATURE_COUNT);
154*4882a593Smuzhiyun return ret;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
features_send_reply(struct net_device * dev,struct genl_info * info,const unsigned long * wanted,const unsigned long * wanted_mask,const unsigned long * active,const unsigned long * active_mask,bool compact)157*4882a593Smuzhiyun static int features_send_reply(struct net_device *dev, struct genl_info *info,
158*4882a593Smuzhiyun const unsigned long *wanted,
159*4882a593Smuzhiyun const unsigned long *wanted_mask,
160*4882a593Smuzhiyun const unsigned long *active,
161*4882a593Smuzhiyun const unsigned long *active_mask, bool compact)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct sk_buff *rskb;
164*4882a593Smuzhiyun void *reply_payload;
165*4882a593Smuzhiyun int reply_len = 0;
166*4882a593Smuzhiyun int ret;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun reply_len = ethnl_reply_header_size();
169*4882a593Smuzhiyun ret = ethnl_bitset_size(wanted, wanted_mask, NETDEV_FEATURE_COUNT,
170*4882a593Smuzhiyun netdev_features_strings, compact);
171*4882a593Smuzhiyun if (ret < 0)
172*4882a593Smuzhiyun goto err;
173*4882a593Smuzhiyun reply_len += ret;
174*4882a593Smuzhiyun ret = ethnl_bitset_size(active, active_mask, NETDEV_FEATURE_COUNT,
175*4882a593Smuzhiyun netdev_features_strings, compact);
176*4882a593Smuzhiyun if (ret < 0)
177*4882a593Smuzhiyun goto err;
178*4882a593Smuzhiyun reply_len += ret;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun ret = -ENOMEM;
181*4882a593Smuzhiyun rskb = ethnl_reply_init(reply_len, dev, ETHTOOL_MSG_FEATURES_SET_REPLY,
182*4882a593Smuzhiyun ETHTOOL_A_FEATURES_HEADER, info,
183*4882a593Smuzhiyun &reply_payload);
184*4882a593Smuzhiyun if (!rskb)
185*4882a593Smuzhiyun goto err;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_WANTED, wanted,
188*4882a593Smuzhiyun wanted_mask, NETDEV_FEATURE_COUNT,
189*4882a593Smuzhiyun netdev_features_strings, compact);
190*4882a593Smuzhiyun if (ret < 0)
191*4882a593Smuzhiyun goto nla_put_failure;
192*4882a593Smuzhiyun ret = ethnl_put_bitset(rskb, ETHTOOL_A_FEATURES_ACTIVE, active,
193*4882a593Smuzhiyun active_mask, NETDEV_FEATURE_COUNT,
194*4882a593Smuzhiyun netdev_features_strings, compact);
195*4882a593Smuzhiyun if (ret < 0)
196*4882a593Smuzhiyun goto nla_put_failure;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun genlmsg_end(rskb, reply_payload);
199*4882a593Smuzhiyun ret = genlmsg_reply(rskb, info);
200*4882a593Smuzhiyun return ret;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun nla_put_failure:
203*4882a593Smuzhiyun nlmsg_free(rskb);
204*4882a593Smuzhiyun WARN_ONCE(1, "calculated message payload length (%d) not sufficient\n",
205*4882a593Smuzhiyun reply_len);
206*4882a593Smuzhiyun err:
207*4882a593Smuzhiyun GENL_SET_ERR_MSG(info, "failed to send reply message");
208*4882a593Smuzhiyun return ret;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
ethnl_set_features(struct sk_buff * skb,struct genl_info * info)211*4882a593Smuzhiyun int ethnl_set_features(struct sk_buff *skb, struct genl_info *info)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun DECLARE_BITMAP(wanted_diff_mask, NETDEV_FEATURE_COUNT);
214*4882a593Smuzhiyun DECLARE_BITMAP(active_diff_mask, NETDEV_FEATURE_COUNT);
215*4882a593Smuzhiyun DECLARE_BITMAP(old_active, NETDEV_FEATURE_COUNT);
216*4882a593Smuzhiyun DECLARE_BITMAP(old_wanted, NETDEV_FEATURE_COUNT);
217*4882a593Smuzhiyun DECLARE_BITMAP(new_active, NETDEV_FEATURE_COUNT);
218*4882a593Smuzhiyun DECLARE_BITMAP(new_wanted, NETDEV_FEATURE_COUNT);
219*4882a593Smuzhiyun DECLARE_BITMAP(req_wanted, NETDEV_FEATURE_COUNT);
220*4882a593Smuzhiyun DECLARE_BITMAP(req_mask, NETDEV_FEATURE_COUNT);
221*4882a593Smuzhiyun struct ethnl_req_info req_info = {};
222*4882a593Smuzhiyun struct nlattr **tb = info->attrs;
223*4882a593Smuzhiyun struct net_device *dev;
224*4882a593Smuzhiyun bool mod;
225*4882a593Smuzhiyun int ret;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun if (!tb[ETHTOOL_A_FEATURES_WANTED])
228*4882a593Smuzhiyun return -EINVAL;
229*4882a593Smuzhiyun ret = ethnl_parse_header_dev_get(&req_info,
230*4882a593Smuzhiyun tb[ETHTOOL_A_FEATURES_HEADER],
231*4882a593Smuzhiyun genl_info_net(info), info->extack,
232*4882a593Smuzhiyun true);
233*4882a593Smuzhiyun if (ret < 0)
234*4882a593Smuzhiyun return ret;
235*4882a593Smuzhiyun dev = req_info.dev;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun rtnl_lock();
238*4882a593Smuzhiyun ethnl_features_to_bitmap(old_active, dev->features);
239*4882a593Smuzhiyun ethnl_features_to_bitmap(old_wanted, dev->wanted_features);
240*4882a593Smuzhiyun ret = ethnl_parse_bitset(req_wanted, req_mask, NETDEV_FEATURE_COUNT,
241*4882a593Smuzhiyun tb[ETHTOOL_A_FEATURES_WANTED],
242*4882a593Smuzhiyun netdev_features_strings, info->extack);
243*4882a593Smuzhiyun if (ret < 0)
244*4882a593Smuzhiyun goto out_rtnl;
245*4882a593Smuzhiyun if (ethnl_bitmap_to_features(req_mask) & ~NETIF_F_ETHTOOL_BITS) {
246*4882a593Smuzhiyun GENL_SET_ERR_MSG(info, "attempt to change non-ethtool features");
247*4882a593Smuzhiyun ret = -EINVAL;
248*4882a593Smuzhiyun goto out_rtnl;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* set req_wanted bits not in req_mask from old_wanted */
252*4882a593Smuzhiyun bitmap_and(req_wanted, req_wanted, req_mask, NETDEV_FEATURE_COUNT);
253*4882a593Smuzhiyun bitmap_andnot(new_wanted, old_wanted, req_mask, NETDEV_FEATURE_COUNT);
254*4882a593Smuzhiyun bitmap_or(req_wanted, new_wanted, req_wanted, NETDEV_FEATURE_COUNT);
255*4882a593Smuzhiyun if (!bitmap_equal(req_wanted, old_wanted, NETDEV_FEATURE_COUNT)) {
256*4882a593Smuzhiyun dev->wanted_features &= ~dev->hw_features;
257*4882a593Smuzhiyun dev->wanted_features |= ethnl_bitmap_to_features(req_wanted) & dev->hw_features;
258*4882a593Smuzhiyun __netdev_update_features(dev);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun ethnl_features_to_bitmap(new_active, dev->features);
261*4882a593Smuzhiyun mod = !bitmap_equal(old_active, new_active, NETDEV_FEATURE_COUNT);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun ret = 0;
264*4882a593Smuzhiyun if (!(req_info.flags & ETHTOOL_FLAG_OMIT_REPLY)) {
265*4882a593Smuzhiyun bool compact = req_info.flags & ETHTOOL_FLAG_COMPACT_BITSETS;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun bitmap_xor(wanted_diff_mask, req_wanted, new_active,
268*4882a593Smuzhiyun NETDEV_FEATURE_COUNT);
269*4882a593Smuzhiyun bitmap_xor(active_diff_mask, old_active, new_active,
270*4882a593Smuzhiyun NETDEV_FEATURE_COUNT);
271*4882a593Smuzhiyun bitmap_and(wanted_diff_mask, wanted_diff_mask, req_mask,
272*4882a593Smuzhiyun NETDEV_FEATURE_COUNT);
273*4882a593Smuzhiyun bitmap_and(req_wanted, req_wanted, wanted_diff_mask,
274*4882a593Smuzhiyun NETDEV_FEATURE_COUNT);
275*4882a593Smuzhiyun bitmap_and(new_active, new_active, active_diff_mask,
276*4882a593Smuzhiyun NETDEV_FEATURE_COUNT);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun ret = features_send_reply(dev, info, req_wanted,
279*4882a593Smuzhiyun wanted_diff_mask, new_active,
280*4882a593Smuzhiyun active_diff_mask, compact);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun if (mod)
283*4882a593Smuzhiyun netdev_features_change(dev);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun out_rtnl:
286*4882a593Smuzhiyun rtnl_unlock();
287*4882a593Smuzhiyun dev_put(dev);
288*4882a593Smuzhiyun return ret;
289*4882a593Smuzhiyun }
290