1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <net/netdev_lock.h> 4 #include <net/netdev_queues.h> 5 #include <net/sock.h> 6 #include <linux/ethtool_netlink.h> 7 #include <linux/phy_link_topology.h> 8 #include <linux/pm_runtime.h> 9 10 #include "common.h" 11 #include "module_fw.h" 12 #include "netlink.h" 13 14 static struct genl_family ethtool_genl_family; 15 16 static bool ethnl_ok __read_mostly; 17 18 /* Serializes broadcast notification sequence allocation with the multicast 19 * send, so that userspace observes nlmsg_seq monotonic in receive order 20 * regardless of which lock the caller holds (rtnl or instance lock). 21 */ 22 static DEFINE_MUTEX(ethnl_bcast_lock); 23 static u32 ethnl_bcast_seq; 24 25 #define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS | \ 26 ETHTOOL_FLAG_OMIT_REPLY) 27 #define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS) 28 29 const struct nla_policy ethnl_header_policy[] = { 30 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 }, 31 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING, 32 .len = ALTIFNAMSIZ - 1 }, 33 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32, 34 ETHTOOL_FLAGS_BASIC), 35 }; 36 37 const struct nla_policy ethnl_header_policy_stats[] = { 38 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 }, 39 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING, 40 .len = ALTIFNAMSIZ - 1 }, 41 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32, 42 ETHTOOL_FLAGS_STATS), 43 }; 44 45 const struct nla_policy ethnl_header_policy_phy[] = { 46 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 }, 47 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING, 48 .len = ALTIFNAMSIZ - 1 }, 49 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32, 50 ETHTOOL_FLAGS_BASIC), 51 [ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1), 52 }; 53 54 const struct nla_policy ethnl_header_policy_phy_stats[] = { 55 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 }, 56 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING, 57 .len = ALTIFNAMSIZ - 1 }, 58 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32, 59 ETHTOOL_FLAGS_STATS), 60 [ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1), 61 }; 62 63 int ethnl_sock_priv_set(struct sk_buff *skb, struct net *net, u32 portid, 64 enum ethnl_sock_type type) 65 { 66 struct ethnl_sock_priv *sk_priv; 67 68 sk_priv = genl_sk_priv_get(ðtool_genl_family, NETLINK_CB(skb).sk); 69 if (IS_ERR(sk_priv)) 70 return PTR_ERR(sk_priv); 71 72 sk_priv->net = net; 73 sk_priv->portid = portid; 74 sk_priv->type = type; 75 76 return 0; 77 } 78 79 static void ethnl_sock_priv_destroy(void *priv) 80 { 81 struct ethnl_sock_priv *sk_priv = priv; 82 83 switch (sk_priv->type) { 84 case ETHTOOL_SOCK_TYPE_MODULE_FW_FLASH: 85 ethnl_module_fw_flash_sock_destroy(sk_priv); 86 break; 87 default: 88 break; 89 } 90 } 91 92 int ethnl_ops_begin(struct net_device *dev) 93 { 94 int ret; 95 96 if (!dev) 97 return -ENODEV; 98 99 if (dev->dev.parent) 100 pm_runtime_get_sync(dev->dev.parent); 101 102 netdev_assert_locked_ops_compat(dev); 103 104 if (!netif_device_present(dev) || 105 dev->reg_state >= NETREG_UNREGISTERING) { 106 ret = -ENODEV; 107 goto err; 108 } 109 110 if (dev->ethtool_ops->begin) { 111 ret = dev->ethtool_ops->begin(dev); 112 if (ret) 113 goto err; 114 } 115 116 return 0; 117 err: 118 if (dev->dev.parent) 119 pm_runtime_put(dev->dev.parent); 120 121 return ret; 122 } 123 124 void ethnl_ops_complete(struct net_device *dev) 125 { 126 if (dev->ethtool_ops->complete) 127 dev->ethtool_ops->complete(dev); 128 129 if (dev->dev.parent) 130 pm_runtime_put(dev->dev.parent); 131 } 132 133 /** 134 * ethnl_parse_header_dev_get() - parse request header 135 * @req_info: structure to put results into 136 * @header: nest attribute with request header 137 * @net: request netns 138 * @extack: netlink extack for error reporting 139 * @require_dev: fail if no device identified in header 140 * 141 * Parse request header in nested attribute @nest and puts results into 142 * the structure pointed to by @req_info. Extack from @info is used for error 143 * reporting. If req_info->dev is not null on return, reference to it has 144 * been taken. If error is returned, *req_info is null initialized and no 145 * reference is held. 146 * 147 * Return: 0 on success or negative error code 148 */ 149 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info, 150 const struct nlattr *header, struct net *net, 151 struct netlink_ext_ack *extack, bool require_dev) 152 { 153 struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy_phy)]; 154 const struct nlattr *devname_attr; 155 struct net_device *dev = NULL; 156 u32 flags = 0; 157 int ret; 158 159 if (!header) { 160 if (!require_dev) 161 return 0; 162 NL_SET_ERR_MSG(extack, "request header missing"); 163 return -EINVAL; 164 } 165 /* No validation here, command policy should have a nested policy set 166 * for the header, therefore validation should have already been done. 167 */ 168 ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy_phy) - 1, header, 169 NULL, extack); 170 if (ret < 0) 171 return ret; 172 if (tb[ETHTOOL_A_HEADER_FLAGS]) 173 flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]); 174 175 devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME]; 176 if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) { 177 u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]); 178 179 dev = netdev_get_by_index(net, ifindex, &req_info->dev_tracker, 180 GFP_KERNEL); 181 if (!dev) { 182 NL_SET_ERR_MSG_ATTR(extack, 183 tb[ETHTOOL_A_HEADER_DEV_INDEX], 184 "no device matches ifindex"); 185 return -ENODEV; 186 } 187 /* if both ifindex and ifname are passed, they must match */ 188 if (devname_attr && 189 strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) { 190 netdev_put(dev, &req_info->dev_tracker); 191 NL_SET_ERR_MSG_ATTR(extack, header, 192 "ifindex and name do not match"); 193 return -ENODEV; 194 } 195 } else if (devname_attr) { 196 dev = netdev_get_by_name(net, nla_data(devname_attr), 197 &req_info->dev_tracker, GFP_KERNEL); 198 if (!dev) { 199 NL_SET_ERR_MSG_ATTR(extack, devname_attr, 200 "no device matches name"); 201 return -ENODEV; 202 } 203 } else if (require_dev) { 204 NL_SET_ERR_MSG_ATTR(extack, header, 205 "neither ifindex nor name specified"); 206 return -EINVAL; 207 } 208 209 if (tb[ETHTOOL_A_HEADER_PHY_INDEX]) { 210 if (dev) { 211 req_info->phy_index = nla_get_u32(tb[ETHTOOL_A_HEADER_PHY_INDEX]); 212 } else { 213 NL_SET_ERR_MSG_ATTR(extack, header, 214 "phy_index set without a netdev"); 215 return -EINVAL; 216 } 217 } 218 219 req_info->dev = dev; 220 req_info->flags = flags; 221 return 0; 222 } 223 224 struct phy_device *ethnl_req_get_phydev(const struct ethnl_req_info *req_info, 225 struct nlattr **tb, unsigned int header, 226 struct netlink_ext_ack *extack) 227 { 228 struct phy_device *phydev; 229 230 if (!req_info->dev) 231 return NULL; 232 233 /* If there is no PHY in sight there's no need for assert locking */ 234 if (!phy_link_topo_empty(req_info->dev)) 235 ASSERT_RTNL(); 236 237 if (!req_info->phy_index) 238 return req_info->dev->phydev; 239 240 phydev = phy_link_topo_get_phy(req_info->dev, req_info->phy_index); 241 if (!phydev && tb) { 242 NL_SET_ERR_MSG_ATTR(extack, tb[header], 243 "no phy matching phyindex"); 244 return ERR_PTR(-ENODEV); 245 } 246 247 return phydev; 248 } 249 250 /** 251 * ethnl_fill_reply_header() - Put common header into a reply message 252 * @skb: skb with the message 253 * @dev: network device to describe in header 254 * @attrtype: attribute type to use for the nest 255 * 256 * Create a nested attribute with attributes describing given network device. 257 * 258 * Return: 0 on success, error value (-EMSGSIZE only) on error 259 */ 260 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev, 261 u16 attrtype) 262 { 263 struct nlattr *nest; 264 265 if (!dev) 266 return 0; 267 nest = nla_nest_start(skb, attrtype); 268 if (!nest) 269 return -EMSGSIZE; 270 271 if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) || 272 nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name)) 273 goto nla_put_failure; 274 /* If more attributes are put into reply header, ethnl_header_size() 275 * must be updated to account for them. 276 */ 277 278 nla_nest_end(skb, nest); 279 return 0; 280 281 nla_put_failure: 282 nla_nest_cancel(skb, nest); 283 return -EMSGSIZE; 284 } 285 286 /** 287 * ethnl_reply_init() - Create skb for a reply and fill device identification 288 * @payload: payload length (without netlink and genetlink header) 289 * @dev: device the reply is about (may be null) 290 * @cmd: ETHTOOL_MSG_* message type for reply 291 * @hdr_attrtype: attribute type for common header 292 * @info: genetlink info of the received packet we respond to 293 * @ehdrp: place to store payload pointer returned by genlmsg_new() 294 * 295 * Return: pointer to allocated skb on success, NULL on error 296 */ 297 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd, 298 u16 hdr_attrtype, struct genl_info *info, 299 void **ehdrp) 300 { 301 struct sk_buff *skb; 302 303 skb = genlmsg_new(payload, GFP_KERNEL); 304 if (!skb) 305 goto err; 306 *ehdrp = genlmsg_put_reply(skb, info, ðtool_genl_family, 0, cmd); 307 if (!*ehdrp) 308 goto err_free; 309 310 if (dev) { 311 int ret; 312 313 ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype); 314 if (ret < 0) 315 goto err_free; 316 } 317 return skb; 318 319 err_free: 320 nlmsg_free(skb); 321 err: 322 if (info) 323 GENL_SET_ERR_MSG(info, "failed to setup reply message"); 324 return NULL; 325 } 326 327 void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd) 328 { 329 return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 330 ðtool_genl_family, 0, cmd); 331 } 332 333 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd) 334 { 335 return genlmsg_put(skb, 0, 0, ðtool_genl_family, 0, cmd); 336 } 337 338 void *ethnl_unicast_put(struct sk_buff *skb, u32 portid, u32 seq, u8 cmd) 339 { 340 return genlmsg_put(skb, portid, seq, ðtool_genl_family, 0, cmd); 341 } 342 343 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev) 344 { 345 struct nlmsghdr *nlh = nlmsg_hdr(skb); 346 int ret; 347 348 mutex_lock(ðnl_bcast_lock); 349 nlh->nlmsg_seq = ++ethnl_bcast_seq; 350 ret = genlmsg_multicast_netns(ðtool_genl_family, dev_net(dev), skb, 351 0, ETHNL_MCGRP_MONITOR, GFP_KERNEL); 352 mutex_unlock(ðnl_bcast_lock); 353 return ret; 354 } 355 356 /* GET request helpers */ 357 358 /** 359 * struct ethnl_dump_ctx - context structure for generic dumpit() callback 360 * @ops: request ops of currently processed message type 361 * @req_info: parsed request header of processed request 362 * @reply_data: data needed to compose the reply 363 * @pos_ifindex: saved iteration position - ifindex 364 * 365 * These parameters are kept in struct netlink_callback as context preserved 366 * between iterations. They are initialized by ethnl_default_start() and used 367 * in ethnl_default_dumpit() and ethnl_default_done(). 368 */ 369 struct ethnl_dump_ctx { 370 const struct ethnl_request_ops *ops; 371 struct ethnl_req_info *req_info; 372 struct ethnl_reply_data *reply_data; 373 unsigned long pos_ifindex; 374 }; 375 376 /** 377 * struct ethnl_perphy_dump_ctx - context for dumpit() PHY-aware callbacks 378 * @ethnl_ctx: generic ethnl context 379 * @ifindex: For Filtered DUMP requests, the ifindex of the targeted netdev 380 * @pos_phyindex: iterator position for multi-msg DUMP 381 */ 382 struct ethnl_perphy_dump_ctx { 383 struct ethnl_dump_ctx ethnl_ctx; 384 unsigned int ifindex; 385 unsigned long pos_phyindex; 386 }; 387 388 static const struct ethnl_request_ops * 389 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = { 390 [ETHTOOL_MSG_STRSET_GET] = ðnl_strset_request_ops, 391 [ETHTOOL_MSG_LINKINFO_GET] = ðnl_linkinfo_request_ops, 392 [ETHTOOL_MSG_LINKINFO_SET] = ðnl_linkinfo_request_ops, 393 [ETHTOOL_MSG_LINKMODES_GET] = ðnl_linkmodes_request_ops, 394 [ETHTOOL_MSG_LINKMODES_SET] = ðnl_linkmodes_request_ops, 395 [ETHTOOL_MSG_LINKSTATE_GET] = ðnl_linkstate_request_ops, 396 [ETHTOOL_MSG_DEBUG_GET] = ðnl_debug_request_ops, 397 [ETHTOOL_MSG_DEBUG_SET] = ðnl_debug_request_ops, 398 [ETHTOOL_MSG_WOL_GET] = ðnl_wol_request_ops, 399 [ETHTOOL_MSG_WOL_SET] = ðnl_wol_request_ops, 400 [ETHTOOL_MSG_FEATURES_GET] = ðnl_features_request_ops, 401 [ETHTOOL_MSG_PRIVFLAGS_GET] = ðnl_privflags_request_ops, 402 [ETHTOOL_MSG_PRIVFLAGS_SET] = ðnl_privflags_request_ops, 403 [ETHTOOL_MSG_RINGS_GET] = ðnl_rings_request_ops, 404 [ETHTOOL_MSG_RINGS_SET] = ðnl_rings_request_ops, 405 [ETHTOOL_MSG_CHANNELS_GET] = ðnl_channels_request_ops, 406 [ETHTOOL_MSG_CHANNELS_SET] = ðnl_channels_request_ops, 407 [ETHTOOL_MSG_COALESCE_GET] = ðnl_coalesce_request_ops, 408 [ETHTOOL_MSG_COALESCE_SET] = ðnl_coalesce_request_ops, 409 [ETHTOOL_MSG_PAUSE_GET] = ðnl_pause_request_ops, 410 [ETHTOOL_MSG_PAUSE_SET] = ðnl_pause_request_ops, 411 [ETHTOOL_MSG_EEE_GET] = ðnl_eee_request_ops, 412 [ETHTOOL_MSG_EEE_SET] = ðnl_eee_request_ops, 413 [ETHTOOL_MSG_FEC_GET] = ðnl_fec_request_ops, 414 [ETHTOOL_MSG_FEC_SET] = ðnl_fec_request_ops, 415 [ETHTOOL_MSG_TSINFO_GET] = ðnl_tsinfo_request_ops, 416 [ETHTOOL_MSG_MODULE_EEPROM_GET] = ðnl_module_eeprom_request_ops, 417 [ETHTOOL_MSG_STATS_GET] = ðnl_stats_request_ops, 418 [ETHTOOL_MSG_PHC_VCLOCKS_GET] = ðnl_phc_vclocks_request_ops, 419 [ETHTOOL_MSG_MODULE_GET] = ðnl_module_request_ops, 420 [ETHTOOL_MSG_MODULE_SET] = ðnl_module_request_ops, 421 [ETHTOOL_MSG_PSE_GET] = ðnl_pse_request_ops, 422 [ETHTOOL_MSG_PSE_SET] = ðnl_pse_request_ops, 423 [ETHTOOL_MSG_RSS_GET] = ðnl_rss_request_ops, 424 [ETHTOOL_MSG_RSS_SET] = ðnl_rss_request_ops, 425 [ETHTOOL_MSG_PLCA_GET_CFG] = ðnl_plca_cfg_request_ops, 426 [ETHTOOL_MSG_PLCA_SET_CFG] = ðnl_plca_cfg_request_ops, 427 [ETHTOOL_MSG_PLCA_GET_STATUS] = ðnl_plca_status_request_ops, 428 [ETHTOOL_MSG_MM_GET] = ðnl_mm_request_ops, 429 [ETHTOOL_MSG_MM_SET] = ðnl_mm_request_ops, 430 [ETHTOOL_MSG_TSCONFIG_GET] = ðnl_tsconfig_request_ops, 431 [ETHTOOL_MSG_TSCONFIG_SET] = ðnl_tsconfig_request_ops, 432 [ETHTOOL_MSG_PHY_GET] = ðnl_phy_request_ops, 433 [ETHTOOL_MSG_MSE_GET] = ðnl_mse_request_ops, 434 }; 435 436 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb) 437 { 438 return (struct ethnl_dump_ctx *)cb->ctx; 439 } 440 441 static struct ethnl_perphy_dump_ctx * 442 ethnl_perphy_dump_context(struct netlink_callback *cb) 443 { 444 return (struct ethnl_perphy_dump_ctx *)cb->ctx; 445 } 446 447 /** 448 * ethnl_default_parse() - Parse request message 449 * @req_info: pointer to structure to put data into 450 * @info: genl_info from the request 451 * @request_ops: struct request_ops for request type 452 * @require_dev: fail if no device identified in header 453 * 454 * Parse universal request header and call request specific ->parse_request() 455 * callback (if defined) to parse the rest of the message. 456 * 457 * Return: 0 on success or negative error code 458 */ 459 static int ethnl_default_parse(struct ethnl_req_info *req_info, 460 const struct genl_info *info, 461 const struct ethnl_request_ops *request_ops, 462 bool require_dev) 463 { 464 struct nlattr **tb = info->attrs; 465 int ret; 466 467 ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr], 468 genl_info_net(info), info->extack, 469 require_dev); 470 if (ret < 0) 471 return ret; 472 473 if (request_ops->parse_request) { 474 ret = request_ops->parse_request(req_info, info, tb, 475 info->extack); 476 if (ret < 0) 477 goto err_dev; 478 } 479 480 return 0; 481 482 err_dev: 483 netdev_put(req_info->dev, &req_info->dev_tracker); 484 req_info->dev = NULL; 485 return ret; 486 } 487 488 /** 489 * ethnl_init_reply_data() - Initialize reply data for GET request 490 * @reply_data: pointer to embedded struct ethnl_reply_data 491 * @ops: instance of struct ethnl_request_ops describing the layout 492 * @dev: network device to initialize the reply for 493 * 494 * Fills the reply data part with zeros and sets the dev member. Must be called 495 * before calling the ->fill_reply() callback (for each iteration when handling 496 * dump requests). 497 */ 498 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data, 499 const struct ethnl_request_ops *ops, 500 struct net_device *dev) 501 { 502 memset(reply_data, 0, ops->reply_data_size); 503 reply_data->dev = dev; 504 } 505 506 /* default ->doit() handler for GET type requests */ 507 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info) 508 { 509 struct ethnl_reply_data *reply_data = NULL; 510 struct ethnl_req_info *req_info = NULL; 511 const u8 cmd = info->genlhdr->cmd; 512 const struct ethnl_request_ops *ops; 513 bool need_rtnl = false; 514 int hdr_len, reply_len; 515 struct sk_buff *rskb; 516 void *reply_payload; 517 int ret; 518 519 ops = ethnl_default_requests[cmd]; 520 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd)) 521 return -EOPNOTSUPP; 522 if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr)) 523 return -EINVAL; 524 525 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 526 if (!req_info) 527 return -ENOMEM; 528 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 529 if (!reply_data) { 530 kfree(req_info); 531 return -ENOMEM; 532 } 533 534 ret = ethnl_default_parse(req_info, info, ops, !ops->allow_nodev_do); 535 if (ret < 0) 536 goto err_free; 537 ethnl_init_reply_data(reply_data, ops, req_info->dev); 538 539 if (req_info->dev) { 540 need_rtnl = !netdev_need_ops_lock(req_info->dev) || 541 ethtool_nl_msg_needs_rtnl(req_info->dev, cmd); 542 if (need_rtnl) 543 rtnl_lock(); 544 netdev_lock_ops(req_info->dev); 545 } 546 ret = ops->prepare_data(req_info, reply_data, info); 547 if (req_info->dev) { 548 netdev_unlock_ops(req_info->dev); 549 if (need_rtnl) 550 rtnl_unlock(); 551 } 552 if (ret < 0) 553 goto err_dev; 554 ret = ops->reply_size(req_info, reply_data); 555 if (ret < 0) 556 goto err_cleanup; 557 reply_len = ret; 558 ret = -ENOMEM; 559 rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(), 560 req_info->dev, ops->reply_cmd, 561 ops->hdr_attr, info, &reply_payload); 562 if (!rskb) 563 goto err_cleanup; 564 hdr_len = rskb->len; 565 ret = ops->fill_reply(rskb, req_info, reply_data); 566 if (ret < 0) 567 goto err_msg; 568 WARN_ONCE(rskb->len - hdr_len > reply_len, 569 "ethnl cmd %d: calculated reply length %d, but consumed %d\n", 570 cmd, reply_len, rskb->len - hdr_len); 571 if (ops->cleanup_data) 572 ops->cleanup_data(reply_data); 573 574 genlmsg_end(rskb, reply_payload); 575 netdev_put(req_info->dev, &req_info->dev_tracker); 576 kfree(reply_data); 577 kfree(req_info); 578 return genlmsg_reply(rskb, info); 579 580 err_msg: 581 WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len); 582 nlmsg_free(rskb); 583 err_cleanup: 584 if (ops->cleanup_data) 585 ops->cleanup_data(reply_data); 586 err_dev: 587 netdev_put(req_info->dev, &req_info->dev_tracker); 588 err_free: 589 kfree(reply_data); 590 kfree(req_info); 591 return ret; 592 } 593 594 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev, 595 const struct ethnl_dump_ctx *ctx, 596 const struct genl_info *info) 597 { 598 bool need_rtnl; 599 void *ehdr; 600 int ret; 601 602 ehdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, 603 ðtool_genl_family, NLM_F_MULTI, 604 ctx->ops->reply_cmd); 605 if (!ehdr) 606 return -EMSGSIZE; 607 608 ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev); 609 need_rtnl = !netdev_need_ops_lock(dev) || 610 ethtool_nl_msg_needs_rtnl(dev, ctx->ops->request_cmd); 611 if (need_rtnl) 612 rtnl_lock(); 613 netdev_lock_ops(dev); 614 ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, info); 615 netdev_unlock_ops(dev); 616 if (need_rtnl) 617 rtnl_unlock(); 618 if (ret < 0) 619 goto out_cancel; 620 ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr); 621 if (ret < 0) 622 goto out; 623 ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data); 624 625 out: 626 if (ctx->ops->cleanup_data) 627 ctx->ops->cleanup_data(ctx->reply_data); 628 out_cancel: 629 ctx->reply_data->dev = NULL; 630 if (ret < 0) 631 genlmsg_cancel(skb, ehdr); 632 else 633 genlmsg_end(skb, ehdr); 634 return ret; 635 } 636 637 /* Default ->dumpit() handler for GET requests. */ 638 static int ethnl_default_dumpit(struct sk_buff *skb, 639 struct netlink_callback *cb) 640 { 641 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 642 struct net *net = sock_net(skb->sk); 643 netdevice_tracker dev_tracker; 644 struct net_device *dev; 645 int ret = 0; 646 647 rcu_read_lock(); 648 for_each_netdev_dump(net, dev, ctx->pos_ifindex) { 649 netdev_hold(dev, &dev_tracker, GFP_ATOMIC); 650 rcu_read_unlock(); 651 652 ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb)); 653 654 rcu_read_lock(); 655 netdev_put(dev, &dev_tracker); 656 657 if (ret < 0 && ret != -EOPNOTSUPP) { 658 if (likely(skb->len)) 659 ret = skb->len; 660 break; 661 } 662 ret = 0; 663 } 664 rcu_read_unlock(); 665 666 return ret; 667 } 668 669 /* generic ->start() handler for GET requests */ 670 static int ethnl_default_start(struct netlink_callback *cb) 671 { 672 const struct genl_dumpit_info *info = genl_dumpit_info(cb); 673 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 674 struct ethnl_reply_data *reply_data; 675 const struct ethnl_request_ops *ops; 676 struct ethnl_req_info *req_info; 677 struct genlmsghdr *ghdr; 678 int ret; 679 680 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 681 682 ghdr = nlmsg_data(cb->nlh); 683 ops = ethnl_default_requests[ghdr->cmd]; 684 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd)) 685 return -EOPNOTSUPP; 686 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 687 if (!req_info) 688 return -ENOMEM; 689 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 690 if (!reply_data) { 691 ret = -ENOMEM; 692 goto free_req_info; 693 } 694 695 ret = ethnl_default_parse(req_info, &info->info, ops, false); 696 if (ret < 0) 697 goto free_reply_data; 698 if (req_info->dev) { 699 /* We ignore device specification in dump requests but as the 700 * same parser as for non-dump (doit) requests is used, it 701 * would take reference to the device if it finds one 702 */ 703 netdev_put(req_info->dev, &req_info->dev_tracker); 704 req_info->dev = NULL; 705 } 706 707 ctx->ops = ops; 708 ctx->req_info = req_info; 709 ctx->reply_data = reply_data; 710 ctx->pos_ifindex = 0; 711 712 return 0; 713 714 free_reply_data: 715 kfree(reply_data); 716 free_req_info: 717 kfree(req_info); 718 719 return ret; 720 } 721 722 /* per-PHY ->start() handler for GET requests */ 723 static int ethnl_perphy_start(struct netlink_callback *cb) 724 { 725 struct ethnl_perphy_dump_ctx *phy_ctx = ethnl_perphy_dump_context(cb); 726 const struct genl_dumpit_info *info = genl_dumpit_info(cb); 727 struct ethnl_dump_ctx *ctx = &phy_ctx->ethnl_ctx; 728 struct ethnl_reply_data *reply_data; 729 const struct ethnl_request_ops *ops; 730 struct ethnl_req_info *req_info; 731 struct genlmsghdr *ghdr; 732 int ret; 733 734 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 735 736 ghdr = nlmsg_data(cb->nlh); 737 ops = ethnl_default_requests[ghdr->cmd]; 738 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd)) 739 return -EOPNOTSUPP; 740 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 741 if (!req_info) 742 return -ENOMEM; 743 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 744 if (!reply_data) { 745 ret = -ENOMEM; 746 goto free_req_info; 747 } 748 749 /* Unlike per-dev dump, don't ignore dev. The dump handler 750 * will notice it and dump PHYs from given dev. We only keep track of 751 * the dev's ifindex, .dumpit() will grab and release the netdev itself. 752 */ 753 ret = ethnl_default_parse(req_info, &info->info, ops, false); 754 if (ret < 0) 755 goto free_reply_data; 756 if (req_info->dev) { 757 phy_ctx->ifindex = req_info->dev->ifindex; 758 netdev_put(req_info->dev, &req_info->dev_tracker); 759 req_info->dev = NULL; 760 } 761 762 ctx->ops = ops; 763 ctx->req_info = req_info; 764 ctx->reply_data = reply_data; 765 ctx->pos_ifindex = 0; 766 767 return 0; 768 769 free_reply_data: 770 kfree(reply_data); 771 free_req_info: 772 kfree(req_info); 773 774 return ret; 775 } 776 777 static int ethnl_perphy_dump_one_dev(struct sk_buff *skb, 778 struct ethnl_perphy_dump_ctx *ctx, 779 const struct genl_info *info) 780 { 781 struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx; 782 struct net_device *dev = ethnl_ctx->req_info->dev; 783 struct phy_device_node *pdn; 784 int ret; 785 786 if (!dev->link_topo) 787 return 0; 788 789 xa_for_each_start(&dev->link_topo->phys, ctx->pos_phyindex, pdn, 790 ctx->pos_phyindex) { 791 ethnl_ctx->req_info->phy_index = ctx->pos_phyindex; 792 793 /* We can re-use the original dump_one as ->prepare_data in 794 * commands use ethnl_req_get_phydev(), which gets the PHY from 795 * the req_info->phy_index 796 */ 797 ret = ethnl_default_dump_one(skb, dev, ethnl_ctx, info); 798 if (ret) 799 return ret; 800 } 801 802 ctx->pos_phyindex = 0; 803 804 return 0; 805 } 806 807 static int ethnl_perphy_dump_all_dev(struct sk_buff *skb, 808 struct ethnl_perphy_dump_ctx *ctx, 809 const struct genl_info *info) 810 { 811 struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx; 812 struct net *net = sock_net(skb->sk); 813 netdevice_tracker dev_tracker; 814 struct net_device *dev; 815 int ret = 0; 816 817 rcu_read_lock(); 818 for_each_netdev_dump(net, dev, ethnl_ctx->pos_ifindex) { 819 netdev_hold(dev, &dev_tracker, GFP_ATOMIC); 820 rcu_read_unlock(); 821 822 /* per-PHY commands use ethnl_req_get_phydev(), which needs the 823 * net_device in the req_info 824 */ 825 ethnl_ctx->req_info->dev = dev; 826 ret = ethnl_perphy_dump_one_dev(skb, ctx, info); 827 828 rcu_read_lock(); 829 netdev_put(dev, &dev_tracker); 830 ethnl_ctx->req_info->dev = NULL; 831 832 if (ret < 0 && ret != -EOPNOTSUPP) { 833 if (likely(skb->len)) 834 ret = skb->len; 835 break; 836 } 837 ret = 0; 838 } 839 rcu_read_unlock(); 840 841 return ret; 842 } 843 844 /* per-PHY ->dumpit() handler for GET requests. */ 845 static int ethnl_perphy_dumpit(struct sk_buff *skb, 846 struct netlink_callback *cb) 847 { 848 struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb); 849 const struct genl_dumpit_info *info = genl_dumpit_info(cb); 850 struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx; 851 int ret = 0; 852 853 if (ctx->ifindex) { 854 netdevice_tracker dev_tracker; 855 struct net_device *dev; 856 857 dev = netdev_get_by_index(genl_info_net(&info->info), 858 ctx->ifindex, &dev_tracker, 859 GFP_KERNEL); 860 if (!dev) 861 return -ENODEV; 862 863 ethnl_ctx->req_info->dev = dev; 864 ret = ethnl_perphy_dump_one_dev(skb, ctx, genl_info_dump(cb)); 865 866 if (ret < 0 && ret != -EOPNOTSUPP && likely(skb->len)) 867 ret = skb->len; 868 869 netdev_put(dev, &dev_tracker); 870 } else { 871 ret = ethnl_perphy_dump_all_dev(skb, ctx, genl_info_dump(cb)); 872 } 873 874 return ret; 875 } 876 877 /* per-PHY ->done() handler for GET requests */ 878 static int ethnl_perphy_done(struct netlink_callback *cb) 879 { 880 struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb); 881 struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx; 882 883 kfree(ethnl_ctx->reply_data); 884 kfree(ethnl_ctx->req_info); 885 886 return 0; 887 } 888 889 /* default ->done() handler for GET requests */ 890 static int ethnl_default_done(struct netlink_callback *cb) 891 { 892 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 893 894 kfree(ctx->reply_data); 895 kfree(ctx->req_info); 896 897 return 0; 898 } 899 900 static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info) 901 { 902 const struct ethnl_request_ops *ops; 903 const u8 cmd = info->genlhdr->cmd; 904 struct ethnl_req_info *req_info; 905 struct net_device *dev; 906 int ret; 907 908 ops = ethnl_default_requests[cmd]; 909 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd)) 910 return -EOPNOTSUPP; 911 if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr)) 912 return -EINVAL; 913 914 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 915 if (!req_info) 916 return -ENOMEM; 917 918 ret = ethnl_default_parse(req_info, info, ops, true); 919 if (ret < 0) 920 goto out_free_req; 921 922 if (ops->set_validate) { 923 ret = ops->set_validate(req_info, info); 924 /* 0 means nothing to do */ 925 if (ret <= 0) 926 goto out_dev; 927 } 928 929 dev = req_info->dev; 930 931 rtnl_lock(); 932 netdev_lock_ops(dev); 933 dev->cfg_pending = kmemdup(dev->cfg, sizeof(*dev->cfg), 934 GFP_KERNEL_ACCOUNT); 935 if (!dev->cfg_pending) { 936 ret = -ENOMEM; 937 goto out_tie_cfg; 938 } 939 940 ret = ethnl_ops_begin(dev); 941 if (ret < 0) 942 goto out_free_cfg; 943 944 ret = ops->set(req_info, info); 945 if (ret < 0) 946 goto out_ops; 947 948 swap(dev->cfg, dev->cfg_pending); 949 if (!ret) 950 goto out_ops; 951 ethnl_notify(dev, ops->set_ntf_cmd, req_info); 952 953 ret = 0; 954 out_ops: 955 ethnl_ops_complete(dev); 956 out_free_cfg: 957 kfree(dev->cfg_pending); 958 out_tie_cfg: 959 dev->cfg_pending = dev->cfg; 960 netdev_unlock_ops(dev); 961 rtnl_unlock(); 962 out_dev: 963 ethnl_parse_header_dev_put(req_info); 964 out_free_req: 965 kfree(req_info); 966 return ret; 967 } 968 969 static const struct ethnl_request_ops * 970 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = { 971 [ETHTOOL_MSG_LINKINFO_NTF] = ðnl_linkinfo_request_ops, 972 [ETHTOOL_MSG_LINKMODES_NTF] = ðnl_linkmodes_request_ops, 973 [ETHTOOL_MSG_DEBUG_NTF] = ðnl_debug_request_ops, 974 [ETHTOOL_MSG_WOL_NTF] = ðnl_wol_request_ops, 975 [ETHTOOL_MSG_FEATURES_NTF] = ðnl_features_request_ops, 976 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ðnl_privflags_request_ops, 977 [ETHTOOL_MSG_RINGS_NTF] = ðnl_rings_request_ops, 978 [ETHTOOL_MSG_CHANNELS_NTF] = ðnl_channels_request_ops, 979 [ETHTOOL_MSG_COALESCE_NTF] = ðnl_coalesce_request_ops, 980 [ETHTOOL_MSG_PAUSE_NTF] = ðnl_pause_request_ops, 981 [ETHTOOL_MSG_EEE_NTF] = ðnl_eee_request_ops, 982 [ETHTOOL_MSG_FEC_NTF] = ðnl_fec_request_ops, 983 [ETHTOOL_MSG_MODULE_NTF] = ðnl_module_request_ops, 984 [ETHTOOL_MSG_PLCA_NTF] = ðnl_plca_cfg_request_ops, 985 [ETHTOOL_MSG_MM_NTF] = ðnl_mm_request_ops, 986 [ETHTOOL_MSG_RSS_NTF] = ðnl_rss_request_ops, 987 [ETHTOOL_MSG_RSS_CREATE_NTF] = ðnl_rss_request_ops, 988 }; 989 990 /* default notification handler */ 991 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd, 992 const struct ethnl_req_info *orig_req_info) 993 { 994 struct ethnl_reply_data *reply_data; 995 const struct ethnl_request_ops *ops; 996 struct ethnl_req_info *req_info; 997 struct genl_info info; 998 struct sk_buff *skb; 999 void *reply_payload; 1000 int reply_len; 1001 int ret; 1002 1003 genl_info_init_ntf(&info, ðtool_genl_family, cmd); 1004 1005 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX || 1006 !ethnl_default_notify_ops[cmd], 1007 "unexpected notification type %u\n", cmd)) 1008 return; 1009 ops = ethnl_default_notify_ops[cmd]; 1010 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 1011 if (!req_info) 1012 return; 1013 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 1014 if (!reply_data) { 1015 kfree(req_info); 1016 return; 1017 } 1018 1019 req_info->dev = dev; 1020 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS; 1021 if (orig_req_info) { 1022 req_info->phy_index = orig_req_info->phy_index; 1023 memcpy(&req_info[1], &orig_req_info[1], 1024 ops->req_info_size - sizeof(*req_info)); 1025 } 1026 1027 netdev_assert_locked_ops_compat(dev); 1028 1029 ethnl_init_reply_data(reply_data, ops, dev); 1030 ret = ops->prepare_data(req_info, reply_data, &info); 1031 if (ret < 0) 1032 goto err_rep; 1033 ret = ops->reply_size(req_info, reply_data); 1034 if (ret < 0) 1035 goto err_cleanup; 1036 reply_len = ret + ethnl_reply_header_size(); 1037 skb = genlmsg_new(reply_len, GFP_KERNEL); 1038 if (!skb) 1039 goto err_cleanup; 1040 reply_payload = ethnl_bcastmsg_put(skb, cmd); 1041 if (!reply_payload) 1042 goto err_skb; 1043 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr); 1044 if (ret < 0) 1045 goto err_msg; 1046 ret = ops->fill_reply(skb, req_info, reply_data); 1047 if (ret < 0) 1048 goto err_msg; 1049 if (ops->cleanup_data) 1050 ops->cleanup_data(reply_data); 1051 1052 genlmsg_end(skb, reply_payload); 1053 kfree(reply_data); 1054 kfree(req_info); 1055 ethnl_multicast(skb, dev); 1056 return; 1057 1058 err_msg: 1059 WARN_ONCE(ret == -EMSGSIZE, 1060 "calculated message payload length (%d) not sufficient\n", 1061 reply_len); 1062 err_skb: 1063 nlmsg_free(skb); 1064 err_cleanup: 1065 if (ops->cleanup_data) 1066 ops->cleanup_data(reply_data); 1067 err_rep: 1068 kfree(reply_data); 1069 kfree(req_info); 1070 return; 1071 } 1072 1073 /* notifications */ 1074 1075 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd, 1076 const struct ethnl_req_info *req_info); 1077 1078 static const ethnl_notify_handler_t ethnl_notify_handlers[] = { 1079 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify, 1080 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify, 1081 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify, 1082 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify, 1083 [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify, 1084 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify, 1085 [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify, 1086 [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify, 1087 [ETHTOOL_MSG_COALESCE_NTF] = ethnl_default_notify, 1088 [ETHTOOL_MSG_PAUSE_NTF] = ethnl_default_notify, 1089 [ETHTOOL_MSG_EEE_NTF] = ethnl_default_notify, 1090 [ETHTOOL_MSG_FEC_NTF] = ethnl_default_notify, 1091 [ETHTOOL_MSG_MODULE_NTF] = ethnl_default_notify, 1092 [ETHTOOL_MSG_PLCA_NTF] = ethnl_default_notify, 1093 [ETHTOOL_MSG_MM_NTF] = ethnl_default_notify, 1094 [ETHTOOL_MSG_RSS_NTF] = ethnl_default_notify, 1095 [ETHTOOL_MSG_RSS_CREATE_NTF] = ethnl_default_notify, 1096 }; 1097 1098 void ethnl_notify(struct net_device *dev, unsigned int cmd, 1099 const struct ethnl_req_info *req_info) 1100 { 1101 if (unlikely(!ethnl_ok)) 1102 return; 1103 netdev_assert_locked_ops_compat(dev); 1104 1105 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) && 1106 ethnl_notify_handlers[cmd])) 1107 ethnl_notify_handlers[cmd](dev, cmd, req_info); 1108 else 1109 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n", 1110 cmd, netdev_name(dev)); 1111 } 1112 1113 void ethtool_notify(struct net_device *dev, unsigned int cmd) 1114 { 1115 ethnl_notify(dev, cmd, NULL); 1116 } 1117 EXPORT_SYMBOL(ethtool_notify); 1118 1119 static void ethnl_notify_features(struct netdev_notifier_info *info) 1120 { 1121 struct net_device *dev = netdev_notifier_info_to_dev(info); 1122 1123 ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF); 1124 } 1125 1126 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event, 1127 void *ptr) 1128 { 1129 struct netdev_notifier_info *info = ptr; 1130 struct netlink_ext_ack *extack; 1131 struct net_device *dev; 1132 1133 dev = netdev_notifier_info_to_dev(info); 1134 extack = netdev_notifier_info_to_extack(info); 1135 1136 switch (event) { 1137 case NETDEV_FEAT_CHANGE: 1138 ethnl_notify_features(ptr); 1139 break; 1140 case NETDEV_PRE_UP: 1141 if (dev->ethtool->module_fw_flash_in_progress) { 1142 NL_SET_ERR_MSG(extack, "Can't set port up while flashing module firmware"); 1143 return NOTIFY_BAD; 1144 } 1145 } 1146 1147 return NOTIFY_DONE; 1148 } 1149 1150 static struct notifier_block ethnl_netdev_notifier = { 1151 .notifier_call = ethnl_netdev_event, 1152 }; 1153 1154 /* genetlink setup */ 1155 1156 static const struct genl_ops ethtool_genl_ops[] = { 1157 { 1158 .cmd = ETHTOOL_MSG_STRSET_GET, 1159 .doit = ethnl_default_doit, 1160 .start = ethnl_default_start, 1161 .dumpit = ethnl_default_dumpit, 1162 .done = ethnl_default_done, 1163 .policy = ethnl_strset_get_policy, 1164 .maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1, 1165 }, 1166 { 1167 .cmd = ETHTOOL_MSG_LINKINFO_GET, 1168 .doit = ethnl_default_doit, 1169 .start = ethnl_default_start, 1170 .dumpit = ethnl_default_dumpit, 1171 .done = ethnl_default_done, 1172 .policy = ethnl_linkinfo_get_policy, 1173 .maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1, 1174 }, 1175 { 1176 .cmd = ETHTOOL_MSG_LINKINFO_SET, 1177 .flags = GENL_UNS_ADMIN_PERM, 1178 .doit = ethnl_default_set_doit, 1179 .policy = ethnl_linkinfo_set_policy, 1180 .maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1, 1181 }, 1182 { 1183 .cmd = ETHTOOL_MSG_LINKMODES_GET, 1184 .doit = ethnl_default_doit, 1185 .start = ethnl_default_start, 1186 .dumpit = ethnl_default_dumpit, 1187 .done = ethnl_default_done, 1188 .policy = ethnl_linkmodes_get_policy, 1189 .maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1, 1190 }, 1191 { 1192 .cmd = ETHTOOL_MSG_LINKMODES_SET, 1193 .flags = GENL_UNS_ADMIN_PERM, 1194 .doit = ethnl_default_set_doit, 1195 .policy = ethnl_linkmodes_set_policy, 1196 .maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1, 1197 }, 1198 { 1199 .cmd = ETHTOOL_MSG_LINKSTATE_GET, 1200 .doit = ethnl_default_doit, 1201 .start = ethnl_default_start, 1202 .dumpit = ethnl_default_dumpit, 1203 .done = ethnl_default_done, 1204 .policy = ethnl_linkstate_get_policy, 1205 .maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1, 1206 }, 1207 { 1208 .cmd = ETHTOOL_MSG_DEBUG_GET, 1209 .doit = ethnl_default_doit, 1210 .start = ethnl_default_start, 1211 .dumpit = ethnl_default_dumpit, 1212 .done = ethnl_default_done, 1213 .policy = ethnl_debug_get_policy, 1214 .maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1, 1215 }, 1216 { 1217 .cmd = ETHTOOL_MSG_DEBUG_SET, 1218 .flags = GENL_UNS_ADMIN_PERM, 1219 .doit = ethnl_default_set_doit, 1220 .policy = ethnl_debug_set_policy, 1221 .maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1, 1222 }, 1223 { 1224 .cmd = ETHTOOL_MSG_WOL_GET, 1225 .flags = GENL_UNS_ADMIN_PERM, 1226 .doit = ethnl_default_doit, 1227 .start = ethnl_default_start, 1228 .dumpit = ethnl_default_dumpit, 1229 .done = ethnl_default_done, 1230 .policy = ethnl_wol_get_policy, 1231 .maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1, 1232 }, 1233 { 1234 .cmd = ETHTOOL_MSG_WOL_SET, 1235 .flags = GENL_UNS_ADMIN_PERM, 1236 .doit = ethnl_default_set_doit, 1237 .policy = ethnl_wol_set_policy, 1238 .maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1, 1239 }, 1240 { 1241 .cmd = ETHTOOL_MSG_FEATURES_GET, 1242 .doit = ethnl_default_doit, 1243 .start = ethnl_default_start, 1244 .dumpit = ethnl_default_dumpit, 1245 .done = ethnl_default_done, 1246 .policy = ethnl_features_get_policy, 1247 .maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1, 1248 }, 1249 { 1250 .cmd = ETHTOOL_MSG_FEATURES_SET, 1251 .flags = GENL_UNS_ADMIN_PERM, 1252 .doit = ethnl_set_features, 1253 .policy = ethnl_features_set_policy, 1254 .maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1, 1255 }, 1256 { 1257 .cmd = ETHTOOL_MSG_PRIVFLAGS_GET, 1258 .doit = ethnl_default_doit, 1259 .start = ethnl_default_start, 1260 .dumpit = ethnl_default_dumpit, 1261 .done = ethnl_default_done, 1262 .policy = ethnl_privflags_get_policy, 1263 .maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1, 1264 }, 1265 { 1266 .cmd = ETHTOOL_MSG_PRIVFLAGS_SET, 1267 .flags = GENL_UNS_ADMIN_PERM, 1268 .doit = ethnl_default_set_doit, 1269 .policy = ethnl_privflags_set_policy, 1270 .maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1, 1271 }, 1272 { 1273 .cmd = ETHTOOL_MSG_RINGS_GET, 1274 .doit = ethnl_default_doit, 1275 .start = ethnl_default_start, 1276 .dumpit = ethnl_default_dumpit, 1277 .done = ethnl_default_done, 1278 .policy = ethnl_rings_get_policy, 1279 .maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1, 1280 }, 1281 { 1282 .cmd = ETHTOOL_MSG_RINGS_SET, 1283 .flags = GENL_UNS_ADMIN_PERM, 1284 .doit = ethnl_default_set_doit, 1285 .policy = ethnl_rings_set_policy, 1286 .maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1, 1287 }, 1288 { 1289 .cmd = ETHTOOL_MSG_CHANNELS_GET, 1290 .doit = ethnl_default_doit, 1291 .start = ethnl_default_start, 1292 .dumpit = ethnl_default_dumpit, 1293 .done = ethnl_default_done, 1294 .policy = ethnl_channels_get_policy, 1295 .maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1, 1296 }, 1297 { 1298 .cmd = ETHTOOL_MSG_CHANNELS_SET, 1299 .flags = GENL_UNS_ADMIN_PERM, 1300 .doit = ethnl_default_set_doit, 1301 .policy = ethnl_channels_set_policy, 1302 .maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1, 1303 }, 1304 { 1305 .cmd = ETHTOOL_MSG_COALESCE_GET, 1306 .doit = ethnl_default_doit, 1307 .start = ethnl_default_start, 1308 .dumpit = ethnl_default_dumpit, 1309 .done = ethnl_default_done, 1310 .policy = ethnl_coalesce_get_policy, 1311 .maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1, 1312 }, 1313 { 1314 .cmd = ETHTOOL_MSG_COALESCE_SET, 1315 .flags = GENL_UNS_ADMIN_PERM, 1316 .doit = ethnl_default_set_doit, 1317 .policy = ethnl_coalesce_set_policy, 1318 .maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1, 1319 }, 1320 { 1321 .cmd = ETHTOOL_MSG_PAUSE_GET, 1322 .doit = ethnl_default_doit, 1323 .start = ethnl_default_start, 1324 .dumpit = ethnl_default_dumpit, 1325 .done = ethnl_default_done, 1326 .policy = ethnl_pause_get_policy, 1327 .maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1, 1328 }, 1329 { 1330 .cmd = ETHTOOL_MSG_PAUSE_SET, 1331 .flags = GENL_UNS_ADMIN_PERM, 1332 .doit = ethnl_default_set_doit, 1333 .policy = ethnl_pause_set_policy, 1334 .maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1, 1335 }, 1336 { 1337 .cmd = ETHTOOL_MSG_EEE_GET, 1338 .doit = ethnl_default_doit, 1339 .start = ethnl_default_start, 1340 .dumpit = ethnl_default_dumpit, 1341 .done = ethnl_default_done, 1342 .policy = ethnl_eee_get_policy, 1343 .maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1, 1344 }, 1345 { 1346 .cmd = ETHTOOL_MSG_EEE_SET, 1347 .flags = GENL_UNS_ADMIN_PERM, 1348 .doit = ethnl_default_set_doit, 1349 .policy = ethnl_eee_set_policy, 1350 .maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1, 1351 }, 1352 { 1353 .cmd = ETHTOOL_MSG_TSINFO_GET, 1354 .doit = ethnl_default_doit, 1355 .start = ethnl_tsinfo_start, 1356 .dumpit = ethnl_tsinfo_dumpit, 1357 .done = ethnl_tsinfo_done, 1358 .policy = ethnl_tsinfo_get_policy, 1359 .maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1, 1360 }, 1361 { 1362 .cmd = ETHTOOL_MSG_CABLE_TEST_ACT, 1363 .flags = GENL_UNS_ADMIN_PERM, 1364 .doit = ethnl_act_cable_test, 1365 .policy = ethnl_cable_test_act_policy, 1366 .maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1, 1367 }, 1368 { 1369 .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_ACT, 1370 .flags = GENL_UNS_ADMIN_PERM, 1371 .doit = ethnl_act_cable_test_tdr, 1372 .policy = ethnl_cable_test_tdr_act_policy, 1373 .maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1, 1374 }, 1375 { 1376 .cmd = ETHTOOL_MSG_TUNNEL_INFO_GET, 1377 .doit = ethnl_tunnel_info_doit, 1378 .start = ethnl_tunnel_info_start, 1379 .dumpit = ethnl_tunnel_info_dumpit, 1380 .policy = ethnl_tunnel_info_get_policy, 1381 .maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1, 1382 }, 1383 { 1384 .cmd = ETHTOOL_MSG_FEC_GET, 1385 .doit = ethnl_default_doit, 1386 .start = ethnl_default_start, 1387 .dumpit = ethnl_default_dumpit, 1388 .done = ethnl_default_done, 1389 .policy = ethnl_fec_get_policy, 1390 .maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1, 1391 }, 1392 { 1393 .cmd = ETHTOOL_MSG_FEC_SET, 1394 .flags = GENL_UNS_ADMIN_PERM, 1395 .doit = ethnl_default_set_doit, 1396 .policy = ethnl_fec_set_policy, 1397 .maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1, 1398 }, 1399 { 1400 .cmd = ETHTOOL_MSG_MODULE_EEPROM_GET, 1401 .flags = GENL_UNS_ADMIN_PERM, 1402 .doit = ethnl_default_doit, 1403 .start = ethnl_default_start, 1404 .dumpit = ethnl_default_dumpit, 1405 .done = ethnl_default_done, 1406 .policy = ethnl_module_eeprom_get_policy, 1407 .maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1, 1408 }, 1409 { 1410 .cmd = ETHTOOL_MSG_STATS_GET, 1411 .doit = ethnl_default_doit, 1412 .start = ethnl_default_start, 1413 .dumpit = ethnl_default_dumpit, 1414 .done = ethnl_default_done, 1415 .policy = ethnl_stats_get_policy, 1416 .maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1, 1417 }, 1418 { 1419 .cmd = ETHTOOL_MSG_PHC_VCLOCKS_GET, 1420 .doit = ethnl_default_doit, 1421 .start = ethnl_default_start, 1422 .dumpit = ethnl_default_dumpit, 1423 .done = ethnl_default_done, 1424 .policy = ethnl_phc_vclocks_get_policy, 1425 .maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1, 1426 }, 1427 { 1428 .cmd = ETHTOOL_MSG_MODULE_GET, 1429 .doit = ethnl_default_doit, 1430 .start = ethnl_default_start, 1431 .dumpit = ethnl_default_dumpit, 1432 .done = ethnl_default_done, 1433 .policy = ethnl_module_get_policy, 1434 .maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1, 1435 }, 1436 { 1437 .cmd = ETHTOOL_MSG_MODULE_SET, 1438 .flags = GENL_UNS_ADMIN_PERM, 1439 .doit = ethnl_default_set_doit, 1440 .policy = ethnl_module_set_policy, 1441 .maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1, 1442 }, 1443 { 1444 .cmd = ETHTOOL_MSG_PSE_GET, 1445 .doit = ethnl_default_doit, 1446 .start = ethnl_perphy_start, 1447 .dumpit = ethnl_perphy_dumpit, 1448 .done = ethnl_perphy_done, 1449 .policy = ethnl_pse_get_policy, 1450 .maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1, 1451 }, 1452 { 1453 .cmd = ETHTOOL_MSG_PSE_SET, 1454 .flags = GENL_UNS_ADMIN_PERM, 1455 .doit = ethnl_default_set_doit, 1456 .policy = ethnl_pse_set_policy, 1457 .maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1, 1458 }, 1459 { 1460 .cmd = ETHTOOL_MSG_RSS_GET, 1461 .doit = ethnl_default_doit, 1462 .start = ethnl_rss_dump_start, 1463 .dumpit = ethnl_rss_dumpit, 1464 .policy = ethnl_rss_get_policy, 1465 .maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1, 1466 }, 1467 { 1468 .cmd = ETHTOOL_MSG_PLCA_GET_CFG, 1469 .doit = ethnl_default_doit, 1470 .start = ethnl_perphy_start, 1471 .dumpit = ethnl_perphy_dumpit, 1472 .done = ethnl_perphy_done, 1473 .policy = ethnl_plca_get_cfg_policy, 1474 .maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1, 1475 }, 1476 { 1477 .cmd = ETHTOOL_MSG_PLCA_SET_CFG, 1478 .flags = GENL_UNS_ADMIN_PERM, 1479 .doit = ethnl_default_set_doit, 1480 .policy = ethnl_plca_set_cfg_policy, 1481 .maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1, 1482 }, 1483 { 1484 .cmd = ETHTOOL_MSG_PLCA_GET_STATUS, 1485 .doit = ethnl_default_doit, 1486 .start = ethnl_perphy_start, 1487 .dumpit = ethnl_perphy_dumpit, 1488 .done = ethnl_perphy_done, 1489 .policy = ethnl_plca_get_status_policy, 1490 .maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1, 1491 }, 1492 { 1493 .cmd = ETHTOOL_MSG_MM_GET, 1494 .doit = ethnl_default_doit, 1495 .start = ethnl_default_start, 1496 .dumpit = ethnl_default_dumpit, 1497 .done = ethnl_default_done, 1498 .policy = ethnl_mm_get_policy, 1499 .maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1, 1500 }, 1501 { 1502 .cmd = ETHTOOL_MSG_MM_SET, 1503 .flags = GENL_UNS_ADMIN_PERM, 1504 .doit = ethnl_default_set_doit, 1505 .policy = ethnl_mm_set_policy, 1506 .maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1, 1507 }, 1508 { 1509 .cmd = ETHTOOL_MSG_MODULE_FW_FLASH_ACT, 1510 .flags = GENL_UNS_ADMIN_PERM, 1511 .doit = ethnl_act_module_fw_flash, 1512 .policy = ethnl_module_fw_flash_act_policy, 1513 .maxattr = ARRAY_SIZE(ethnl_module_fw_flash_act_policy) - 1, 1514 }, 1515 { 1516 .cmd = ETHTOOL_MSG_PHY_GET, 1517 .doit = ethnl_default_doit, 1518 .start = ethnl_perphy_start, 1519 .dumpit = ethnl_perphy_dumpit, 1520 .done = ethnl_perphy_done, 1521 .policy = ethnl_phy_get_policy, 1522 .maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1, 1523 }, 1524 { 1525 .cmd = ETHTOOL_MSG_TSCONFIG_GET, 1526 .doit = ethnl_default_doit, 1527 .start = ethnl_default_start, 1528 .dumpit = ethnl_default_dumpit, 1529 .done = ethnl_default_done, 1530 .policy = ethnl_tsconfig_get_policy, 1531 .maxattr = ARRAY_SIZE(ethnl_tsconfig_get_policy) - 1, 1532 }, 1533 { 1534 .cmd = ETHTOOL_MSG_TSCONFIG_SET, 1535 .flags = GENL_UNS_ADMIN_PERM, 1536 .doit = ethnl_default_set_doit, 1537 .policy = ethnl_tsconfig_set_policy, 1538 .maxattr = ARRAY_SIZE(ethnl_tsconfig_set_policy) - 1, 1539 }, 1540 { 1541 .cmd = ETHTOOL_MSG_RSS_SET, 1542 .flags = GENL_UNS_ADMIN_PERM, 1543 .doit = ethnl_default_set_doit, 1544 .policy = ethnl_rss_set_policy, 1545 .maxattr = ARRAY_SIZE(ethnl_rss_set_policy) - 1, 1546 }, 1547 { 1548 .cmd = ETHTOOL_MSG_RSS_CREATE_ACT, 1549 .flags = GENL_UNS_ADMIN_PERM, 1550 .doit = ethnl_rss_create_doit, 1551 .policy = ethnl_rss_create_policy, 1552 .maxattr = ARRAY_SIZE(ethnl_rss_create_policy) - 1, 1553 }, 1554 { 1555 .cmd = ETHTOOL_MSG_RSS_DELETE_ACT, 1556 .flags = GENL_UNS_ADMIN_PERM, 1557 .doit = ethnl_rss_delete_doit, 1558 .policy = ethnl_rss_delete_policy, 1559 .maxattr = ARRAY_SIZE(ethnl_rss_delete_policy) - 1, 1560 }, 1561 { 1562 .cmd = ETHTOOL_MSG_MSE_GET, 1563 .doit = ethnl_default_doit, 1564 .start = ethnl_perphy_start, 1565 .dumpit = ethnl_perphy_dumpit, 1566 .done = ethnl_perphy_done, 1567 .policy = ethnl_mse_get_policy, 1568 .maxattr = ARRAY_SIZE(ethnl_mse_get_policy) - 1, 1569 }, 1570 }; 1571 1572 static const struct genl_multicast_group ethtool_nl_mcgrps[] = { 1573 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME }, 1574 }; 1575 1576 static struct genl_family ethtool_genl_family __ro_after_init = { 1577 .name = ETHTOOL_GENL_NAME, 1578 .version = ETHTOOL_GENL_VERSION, 1579 .netnsok = true, 1580 .parallel_ops = true, 1581 .ops = ethtool_genl_ops, 1582 .n_ops = ARRAY_SIZE(ethtool_genl_ops), 1583 .resv_start_op = ETHTOOL_MSG_MODULE_GET + 1, 1584 .mcgrps = ethtool_nl_mcgrps, 1585 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps), 1586 .sock_priv_size = sizeof(struct ethnl_sock_priv), 1587 .sock_priv_destroy = ethnl_sock_priv_destroy, 1588 }; 1589 1590 /* module setup */ 1591 1592 static int __init ethnl_init(void) 1593 { 1594 int ret; 1595 1596 ret = genl_register_family(ðtool_genl_family); 1597 if (WARN(ret < 0, "ethtool: genetlink family registration failed")) 1598 return ret; 1599 ethnl_ok = true; 1600 1601 ret = register_netdevice_notifier(ðnl_netdev_notifier); 1602 WARN(ret < 0, "ethtool: net device notifier registration failed"); 1603 return ret; 1604 } 1605 1606 subsys_initcall(ethnl_init); 1607