1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/ethtool.h> 4 #include <linux/phy.h> 5 #include "netlink.h" 6 #include "common.h" 7 8 struct strset_info { 9 bool per_dev; 10 bool free_strings; 11 unsigned int count; 12 const char (*strings)[ETH_GSTRING_LEN]; 13 }; 14 15 static const struct strset_info info_template[] = { 16 [ETH_SS_TEST] = { 17 .per_dev = true, 18 }, 19 [ETH_SS_STATS] = { 20 .per_dev = true, 21 }, 22 [ETH_SS_PRIV_FLAGS] = { 23 .per_dev = true, 24 }, 25 [ETH_SS_FEATURES] = { 26 .per_dev = false, 27 .count = ARRAY_SIZE(netdev_features_strings), 28 .strings = netdev_features_strings, 29 }, 30 [ETH_SS_RSS_HASH_FUNCS] = { 31 .per_dev = false, 32 .count = ARRAY_SIZE(rss_hash_func_strings), 33 .strings = rss_hash_func_strings, 34 }, 35 [ETH_SS_TUNABLES] = { 36 .per_dev = false, 37 .count = ARRAY_SIZE(tunable_strings), 38 .strings = tunable_strings, 39 }, 40 [ETH_SS_PHY_STATS] = { 41 .per_dev = true, 42 }, 43 [ETH_SS_PHY_TUNABLES] = { 44 .per_dev = false, 45 .count = ARRAY_SIZE(phy_tunable_strings), 46 .strings = phy_tunable_strings, 47 }, 48 [ETH_SS_LINK_MODES] = { 49 .per_dev = false, 50 .count = __ETHTOOL_LINK_MODE_MASK_NBITS, 51 .strings = link_mode_names, 52 }, 53 }; 54 55 struct strset_req_info { 56 struct ethnl_req_info base; 57 u32 req_ids; 58 bool counts_only; 59 }; 60 61 #define STRSET_REQINFO(__req_base) \ 62 container_of(__req_base, struct strset_req_info, base) 63 64 struct strset_reply_data { 65 struct ethnl_reply_data base; 66 struct strset_info sets[ETH_SS_COUNT]; 67 }; 68 69 #define STRSET_REPDATA(__reply_base) \ 70 container_of(__reply_base, struct strset_reply_data, base) 71 72 static const struct nla_policy strset_get_policy[ETHTOOL_A_STRSET_MAX + 1] = { 73 [ETHTOOL_A_STRSET_UNSPEC] = { .type = NLA_REJECT }, 74 [ETHTOOL_A_STRSET_HEADER] = { .type = NLA_NESTED }, 75 [ETHTOOL_A_STRSET_STRINGSETS] = { .type = NLA_NESTED }, 76 }; 77 78 static const struct nla_policy 79 get_stringset_policy[ETHTOOL_A_STRINGSET_MAX + 1] = { 80 [ETHTOOL_A_STRINGSET_UNSPEC] = { .type = NLA_REJECT }, 81 [ETHTOOL_A_STRINGSET_ID] = { .type = NLA_U32 }, 82 [ETHTOOL_A_STRINGSET_COUNT] = { .type = NLA_REJECT }, 83 [ETHTOOL_A_STRINGSET_STRINGS] = { .type = NLA_REJECT }, 84 }; 85 86 /** 87 * strset_include() - test if a string set should be included in reply 88 * @data: pointer to request data structure 89 * @id: id of string set to check (ETH_SS_* constants) 90 */ 91 static bool strset_include(const struct strset_req_info *info, 92 const struct strset_reply_data *data, u32 id) 93 { 94 bool per_dev; 95 96 BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids)); 97 98 if (info->req_ids) 99 return info->req_ids & (1U << id); 100 per_dev = data->sets[id].per_dev; 101 if (!per_dev && !data->sets[id].strings) 102 return false; 103 104 return data->base.dev ? per_dev : !per_dev; 105 } 106 107 static int strset_get_id(const struct nlattr *nest, u32 *val, 108 struct netlink_ext_ack *extack) 109 { 110 struct nlattr *tb[ETHTOOL_A_STRINGSET_MAX + 1]; 111 int ret; 112 113 ret = nla_parse_nested(tb, ETHTOOL_A_STRINGSET_MAX, nest, 114 get_stringset_policy, extack); 115 if (ret < 0) 116 return ret; 117 if (!tb[ETHTOOL_A_STRINGSET_ID]) 118 return -EINVAL; 119 120 *val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]); 121 return 0; 122 } 123 124 static const struct nla_policy 125 strset_stringsets_policy[ETHTOOL_A_STRINGSETS_MAX + 1] = { 126 [ETHTOOL_A_STRINGSETS_UNSPEC] = { .type = NLA_REJECT }, 127 [ETHTOOL_A_STRINGSETS_STRINGSET] = { .type = NLA_NESTED }, 128 }; 129 130 static int strset_parse_request(struct ethnl_req_info *req_base, 131 struct nlattr **tb, 132 struct netlink_ext_ack *extack) 133 { 134 struct strset_req_info *req_info = STRSET_REQINFO(req_base); 135 struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS]; 136 struct nlattr *attr; 137 int rem, ret; 138 139 if (!nest) 140 return 0; 141 ret = nla_validate_nested(nest, ETHTOOL_A_STRINGSETS_MAX, 142 strset_stringsets_policy, extack); 143 if (ret < 0) 144 return ret; 145 146 req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY]; 147 nla_for_each_nested(attr, nest, rem) { 148 u32 id; 149 150 if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET, 151 "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n", 152 nla_type(attr))) 153 return -EINVAL; 154 155 ret = strset_get_id(attr, &id, extack); 156 if (ret < 0) 157 return ret; 158 if (ret >= ETH_SS_COUNT) { 159 NL_SET_ERR_MSG_ATTR(extack, attr, 160 "unknown string set id"); 161 return -EOPNOTSUPP; 162 } 163 164 req_info->req_ids |= (1U << id); 165 } 166 167 return 0; 168 } 169 170 static void strset_cleanup_data(struct ethnl_reply_data *reply_base) 171 { 172 struct strset_reply_data *data = STRSET_REPDATA(reply_base); 173 unsigned int i; 174 175 for (i = 0; i < ETH_SS_COUNT; i++) 176 if (data->sets[i].free_strings) { 177 kfree(data->sets[i].strings); 178 data->sets[i].strings = NULL; 179 data->sets[i].free_strings = false; 180 } 181 } 182 183 static int strset_prepare_set(struct strset_info *info, struct net_device *dev, 184 unsigned int id, bool counts_only) 185 { 186 const struct ethtool_ops *ops = dev->ethtool_ops; 187 void *strings; 188 int count, ret; 189 190 if (id == ETH_SS_PHY_STATS && dev->phydev && 191 !ops->get_ethtool_phy_stats) 192 ret = phy_ethtool_get_sset_count(dev->phydev); 193 else if (ops->get_sset_count && ops->get_strings) 194 ret = ops->get_sset_count(dev, id); 195 else 196 ret = -EOPNOTSUPP; 197 if (ret <= 0) { 198 info->count = 0; 199 return 0; 200 } 201 202 count = ret; 203 if (!counts_only) { 204 strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL); 205 if (!strings) 206 return -ENOMEM; 207 if (id == ETH_SS_PHY_STATS && dev->phydev && 208 !ops->get_ethtool_phy_stats) 209 phy_ethtool_get_strings(dev->phydev, strings); 210 else 211 ops->get_strings(dev, id, strings); 212 info->strings = strings; 213 info->free_strings = true; 214 } 215 info->count = count; 216 217 return 0; 218 } 219 220 static int strset_prepare_data(const struct ethnl_req_info *req_base, 221 struct ethnl_reply_data *reply_base, 222 struct genl_info *info) 223 { 224 const struct strset_req_info *req_info = STRSET_REQINFO(req_base); 225 struct strset_reply_data *data = STRSET_REPDATA(reply_base); 226 struct net_device *dev = reply_base->dev; 227 unsigned int i; 228 int ret; 229 230 BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT); 231 memcpy(&data->sets, &info_template, sizeof(data->sets)); 232 233 if (!dev) { 234 for (i = 0; i < ETH_SS_COUNT; i++) { 235 if ((req_info->req_ids & (1U << i)) && 236 data->sets[i].per_dev) { 237 if (info) 238 GENL_SET_ERR_MSG(info, "requested per device strings without dev"); 239 return -EINVAL; 240 } 241 } 242 return 0; 243 } 244 245 ret = ethnl_ops_begin(dev); 246 if (ret < 0) 247 goto err_strset; 248 for (i = 0; i < ETH_SS_COUNT; i++) { 249 if (!strset_include(req_info, data, i) || 250 !data->sets[i].per_dev) 251 continue; 252 253 ret = strset_prepare_set(&data->sets[i], dev, i, 254 req_info->counts_only); 255 if (ret < 0) 256 goto err_ops; 257 } 258 ethnl_ops_complete(dev); 259 260 return 0; 261 err_ops: 262 ethnl_ops_complete(dev); 263 err_strset: 264 strset_cleanup_data(reply_base); 265 return ret; 266 } 267 268 /* calculate size of ETHTOOL_A_STRSET_STRINGSET nest for one string set */ 269 static int strset_set_size(const struct strset_info *info, bool counts_only) 270 { 271 unsigned int len = 0; 272 unsigned int i; 273 274 if (info->count == 0) 275 return 0; 276 if (counts_only) 277 return nla_total_size(2 * nla_total_size(sizeof(u32))); 278 279 for (i = 0; i < info->count; i++) { 280 const char *str = info->strings[i]; 281 282 /* ETHTOOL_A_STRING_INDEX, ETHTOOL_A_STRING_VALUE, nest */ 283 len += nla_total_size(nla_total_size(sizeof(u32)) + 284 ethnl_strz_size(str)); 285 } 286 /* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */ 287 len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len); 288 289 return nla_total_size(len); 290 } 291 292 static int strset_reply_size(const struct ethnl_req_info *req_base, 293 const struct ethnl_reply_data *reply_base) 294 { 295 const struct strset_req_info *req_info = STRSET_REQINFO(req_base); 296 const struct strset_reply_data *data = STRSET_REPDATA(reply_base); 297 unsigned int i; 298 int len = 0; 299 int ret; 300 301 len += ethnl_reply_header_size(); 302 for (i = 0; i < ETH_SS_COUNT; i++) { 303 const struct strset_info *set_info = &data->sets[i]; 304 305 if (!strset_include(req_info, data, i)) 306 continue; 307 308 ret = strset_set_size(set_info, req_info->counts_only); 309 if (ret < 0) 310 return ret; 311 len += ret; 312 } 313 314 return len; 315 } 316 317 /* fill one string into reply */ 318 static int strset_fill_string(struct sk_buff *skb, 319 const struct strset_info *set_info, u32 idx) 320 { 321 struct nlattr *string_attr; 322 const char *value; 323 324 value = set_info->strings[idx]; 325 326 string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING); 327 if (!string_attr) 328 return -EMSGSIZE; 329 if (nla_put_u32(skb, ETHTOOL_A_STRING_INDEX, idx) || 330 ethnl_put_strz(skb, ETHTOOL_A_STRING_VALUE, value)) 331 goto nla_put_failure; 332 nla_nest_end(skb, string_attr); 333 334 return 0; 335 nla_put_failure: 336 nla_nest_cancel(skb, string_attr); 337 return -EMSGSIZE; 338 } 339 340 /* fill one string set into reply */ 341 static int strset_fill_set(struct sk_buff *skb, 342 const struct strset_info *set_info, u32 id, 343 bool counts_only) 344 { 345 struct nlattr *stringset_attr; 346 struct nlattr *strings_attr; 347 unsigned int i; 348 349 if (!set_info->per_dev && !set_info->strings) 350 return -EOPNOTSUPP; 351 if (set_info->count == 0) 352 return 0; 353 stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET); 354 if (!stringset_attr) 355 return -EMSGSIZE; 356 357 if (nla_put_u32(skb, ETHTOOL_A_STRINGSET_ID, id) || 358 nla_put_u32(skb, ETHTOOL_A_STRINGSET_COUNT, set_info->count)) 359 goto nla_put_failure; 360 361 if (!counts_only) { 362 strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS); 363 if (!strings_attr) 364 goto nla_put_failure; 365 for (i = 0; i < set_info->count; i++) { 366 if (strset_fill_string(skb, set_info, i) < 0) 367 goto nla_put_failure; 368 } 369 nla_nest_end(skb, strings_attr); 370 } 371 372 nla_nest_end(skb, stringset_attr); 373 return 0; 374 375 nla_put_failure: 376 nla_nest_cancel(skb, stringset_attr); 377 return -EMSGSIZE; 378 } 379 380 static int strset_fill_reply(struct sk_buff *skb, 381 const struct ethnl_req_info *req_base, 382 const struct ethnl_reply_data *reply_base) 383 { 384 const struct strset_req_info *req_info = STRSET_REQINFO(req_base); 385 const struct strset_reply_data *data = STRSET_REPDATA(reply_base); 386 struct nlattr *nest; 387 unsigned int i; 388 int ret; 389 390 nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS); 391 if (!nest) 392 return -EMSGSIZE; 393 394 for (i = 0; i < ETH_SS_COUNT; i++) { 395 if (strset_include(req_info, data, i)) { 396 ret = strset_fill_set(skb, &data->sets[i], i, 397 req_info->counts_only); 398 if (ret < 0) 399 goto nla_put_failure; 400 } 401 } 402 403 nla_nest_end(skb, nest); 404 return 0; 405 406 nla_put_failure: 407 nla_nest_cancel(skb, nest); 408 return ret; 409 } 410 411 const struct ethnl_request_ops ethnl_strset_request_ops = { 412 .request_cmd = ETHTOOL_MSG_STRSET_GET, 413 .reply_cmd = ETHTOOL_MSG_STRSET_GET_REPLY, 414 .hdr_attr = ETHTOOL_A_STRSET_HEADER, 415 .max_attr = ETHTOOL_A_STRSET_MAX, 416 .req_info_size = sizeof(struct strset_req_info), 417 .reply_data_size = sizeof(struct strset_reply_data), 418 .request_policy = strset_get_policy, 419 .allow_nodev_do = true, 420 421 .parse_request = strset_parse_request, 422 .prepare_data = strset_prepare_data, 423 .reply_size = strset_reply_size, 424 .fill_reply = strset_fill_reply, 425 .cleanup_data = strset_cleanup_data, 426 }; 427