1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <net/sock.h> 4 #include <linux/ethtool_netlink.h> 5 #include "netlink.h" 6 7 static struct genl_family ethtool_genl_family; 8 9 static bool ethnl_ok __read_mostly; 10 static u32 ethnl_bcast_seq; 11 12 static const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_MAX + 1] = { 13 [ETHTOOL_A_HEADER_UNSPEC] = { .type = NLA_REJECT }, 14 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 }, 15 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING, 16 .len = ALTIFNAMSIZ - 1 }, 17 [ETHTOOL_A_HEADER_FLAGS] = { .type = NLA_U32 }, 18 }; 19 20 /** 21 * ethnl_parse_header_dev_get() - parse request header 22 * @req_info: structure to put results into 23 * @header: nest attribute with request header 24 * @net: request netns 25 * @extack: netlink extack for error reporting 26 * @require_dev: fail if no device identified in header 27 * 28 * Parse request header in nested attribute @nest and puts results into 29 * the structure pointed to by @req_info. Extack from @info is used for error 30 * reporting. If req_info->dev is not null on return, reference to it has 31 * been taken. If error is returned, *req_info is null initialized and no 32 * reference is held. 33 * 34 * Return: 0 on success or negative error code 35 */ 36 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info, 37 const struct nlattr *header, struct net *net, 38 struct netlink_ext_ack *extack, bool require_dev) 39 { 40 struct nlattr *tb[ETHTOOL_A_HEADER_MAX + 1]; 41 const struct nlattr *devname_attr; 42 struct net_device *dev = NULL; 43 u32 flags = 0; 44 int ret; 45 46 if (!header) { 47 NL_SET_ERR_MSG(extack, "request header missing"); 48 return -EINVAL; 49 } 50 ret = nla_parse_nested(tb, ETHTOOL_A_HEADER_MAX, header, 51 ethnl_header_policy, extack); 52 if (ret < 0) 53 return ret; 54 if (tb[ETHTOOL_A_HEADER_FLAGS]) { 55 flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]); 56 if (flags & ~ETHTOOL_FLAG_ALL) { 57 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_HEADER_FLAGS], 58 "unrecognized request flags"); 59 nl_set_extack_cookie_u32(extack, ETHTOOL_FLAG_ALL); 60 return -EOPNOTSUPP; 61 } 62 } 63 64 devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME]; 65 if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) { 66 u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]); 67 68 dev = dev_get_by_index(net, ifindex); 69 if (!dev) { 70 NL_SET_ERR_MSG_ATTR(extack, 71 tb[ETHTOOL_A_HEADER_DEV_INDEX], 72 "no device matches ifindex"); 73 return -ENODEV; 74 } 75 /* if both ifindex and ifname are passed, they must match */ 76 if (devname_attr && 77 strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) { 78 dev_put(dev); 79 NL_SET_ERR_MSG_ATTR(extack, header, 80 "ifindex and name do not match"); 81 return -ENODEV; 82 } 83 } else if (devname_attr) { 84 dev = dev_get_by_name(net, nla_data(devname_attr)); 85 if (!dev) { 86 NL_SET_ERR_MSG_ATTR(extack, devname_attr, 87 "no device matches name"); 88 return -ENODEV; 89 } 90 } else if (require_dev) { 91 NL_SET_ERR_MSG_ATTR(extack, header, 92 "neither ifindex nor name specified"); 93 return -EINVAL; 94 } 95 96 if (dev && !netif_device_present(dev)) { 97 dev_put(dev); 98 NL_SET_ERR_MSG(extack, "device not present"); 99 return -ENODEV; 100 } 101 102 req_info->dev = dev; 103 req_info->flags = flags; 104 return 0; 105 } 106 107 /** 108 * ethnl_fill_reply_header() - Put common header into a reply message 109 * @skb: skb with the message 110 * @dev: network device to describe in header 111 * @attrtype: attribute type to use for the nest 112 * 113 * Create a nested attribute with attributes describing given network device. 114 * 115 * Return: 0 on success, error value (-EMSGSIZE only) on error 116 */ 117 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev, 118 u16 attrtype) 119 { 120 struct nlattr *nest; 121 122 if (!dev) 123 return 0; 124 nest = nla_nest_start(skb, attrtype); 125 if (!nest) 126 return -EMSGSIZE; 127 128 if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) || 129 nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name)) 130 goto nla_put_failure; 131 /* If more attributes are put into reply header, ethnl_header_size() 132 * must be updated to account for them. 133 */ 134 135 nla_nest_end(skb, nest); 136 return 0; 137 138 nla_put_failure: 139 nla_nest_cancel(skb, nest); 140 return -EMSGSIZE; 141 } 142 143 /** 144 * ethnl_reply_init() - Create skb for a reply and fill device identification 145 * @payload: payload length (without netlink and genetlink header) 146 * @dev: device the reply is about (may be null) 147 * @cmd: ETHTOOL_MSG_* message type for reply 148 * @hdr_attrtype: attribute type for common header 149 * @info: genetlink info of the received packet we respond to 150 * @ehdrp: place to store payload pointer returned by genlmsg_new() 151 * 152 * Return: pointer to allocated skb on success, NULL on error 153 */ 154 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd, 155 u16 hdr_attrtype, struct genl_info *info, 156 void **ehdrp) 157 { 158 struct sk_buff *skb; 159 160 skb = genlmsg_new(payload, GFP_KERNEL); 161 if (!skb) 162 goto err; 163 *ehdrp = genlmsg_put_reply(skb, info, ðtool_genl_family, 0, cmd); 164 if (!*ehdrp) 165 goto err_free; 166 167 if (dev) { 168 int ret; 169 170 ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype); 171 if (ret < 0) 172 goto err_free; 173 } 174 return skb; 175 176 err_free: 177 nlmsg_free(skb); 178 err: 179 if (info) 180 GENL_SET_ERR_MSG(info, "failed to setup reply message"); 181 return NULL; 182 } 183 184 static void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd) 185 { 186 return genlmsg_put(skb, 0, ++ethnl_bcast_seq, ðtool_genl_family, 0, 187 cmd); 188 } 189 190 static int ethnl_multicast(struct sk_buff *skb, struct net_device *dev) 191 { 192 return genlmsg_multicast_netns(ðtool_genl_family, dev_net(dev), skb, 193 0, ETHNL_MCGRP_MONITOR, GFP_KERNEL); 194 } 195 196 /* GET request helpers */ 197 198 /** 199 * struct ethnl_dump_ctx - context structure for generic dumpit() callback 200 * @ops: request ops of currently processed message type 201 * @req_info: parsed request header of processed request 202 * @reply_data: data needed to compose the reply 203 * @pos_hash: saved iteration position - hashbucket 204 * @pos_idx: saved iteration position - index 205 * 206 * These parameters are kept in struct netlink_callback as context preserved 207 * between iterations. They are initialized by ethnl_default_start() and used 208 * in ethnl_default_dumpit() and ethnl_default_done(). 209 */ 210 struct ethnl_dump_ctx { 211 const struct ethnl_request_ops *ops; 212 struct ethnl_req_info *req_info; 213 struct ethnl_reply_data *reply_data; 214 int pos_hash; 215 int pos_idx; 216 }; 217 218 static const struct ethnl_request_ops * 219 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = { 220 [ETHTOOL_MSG_STRSET_GET] = ðnl_strset_request_ops, 221 [ETHTOOL_MSG_LINKINFO_GET] = ðnl_linkinfo_request_ops, 222 [ETHTOOL_MSG_LINKMODES_GET] = ðnl_linkmodes_request_ops, 223 [ETHTOOL_MSG_LINKSTATE_GET] = ðnl_linkstate_request_ops, 224 [ETHTOOL_MSG_DEBUG_GET] = ðnl_debug_request_ops, 225 [ETHTOOL_MSG_WOL_GET] = ðnl_wol_request_ops, 226 [ETHTOOL_MSG_FEATURES_GET] = ðnl_features_request_ops, 227 [ETHTOOL_MSG_PRIVFLAGS_GET] = ðnl_privflags_request_ops, 228 [ETHTOOL_MSG_RINGS_GET] = ðnl_rings_request_ops, 229 [ETHTOOL_MSG_CHANNELS_GET] = ðnl_channels_request_ops, 230 }; 231 232 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb) 233 { 234 return (struct ethnl_dump_ctx *)cb->ctx; 235 } 236 237 /** 238 * ethnl_default_parse() - Parse request message 239 * @req_info: pointer to structure to put data into 240 * @nlhdr: pointer to request message header 241 * @net: request netns 242 * @request_ops: struct request_ops for request type 243 * @extack: netlink extack for error reporting 244 * @require_dev: fail if no device identified in header 245 * 246 * Parse universal request header and call request specific ->parse_request() 247 * callback (if defined) to parse the rest of the message. 248 * 249 * Return: 0 on success or negative error code 250 */ 251 static int ethnl_default_parse(struct ethnl_req_info *req_info, 252 const struct nlmsghdr *nlhdr, struct net *net, 253 const struct ethnl_request_ops *request_ops, 254 struct netlink_ext_ack *extack, bool require_dev) 255 { 256 struct nlattr **tb; 257 int ret; 258 259 tb = kmalloc_array(request_ops->max_attr + 1, sizeof(tb[0]), 260 GFP_KERNEL); 261 if (!tb) 262 return -ENOMEM; 263 264 ret = nlmsg_parse(nlhdr, GENL_HDRLEN, tb, request_ops->max_attr, 265 request_ops->request_policy, extack); 266 if (ret < 0) 267 goto out; 268 ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr], 269 net, extack, require_dev); 270 if (ret < 0) 271 goto out; 272 273 if (request_ops->parse_request) { 274 ret = request_ops->parse_request(req_info, tb, extack); 275 if (ret < 0) 276 goto out; 277 } 278 279 ret = 0; 280 out: 281 kfree(tb); 282 return ret; 283 } 284 285 /** 286 * ethnl_init_reply_data() - Initialize reply data for GET request 287 * @reply_data: pointer to embedded struct ethnl_reply_data 288 * @ops: instance of struct ethnl_request_ops describing the layout 289 * @dev: network device to initialize the reply for 290 * 291 * Fills the reply data part with zeros and sets the dev member. Must be called 292 * before calling the ->fill_reply() callback (for each iteration when handling 293 * dump requests). 294 */ 295 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data, 296 const struct ethnl_request_ops *ops, 297 struct net_device *dev) 298 { 299 memset(reply_data, 0, ops->reply_data_size); 300 reply_data->dev = dev; 301 } 302 303 /* default ->doit() handler for GET type requests */ 304 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info) 305 { 306 struct ethnl_reply_data *reply_data = NULL; 307 struct ethnl_req_info *req_info = NULL; 308 const u8 cmd = info->genlhdr->cmd; 309 const struct ethnl_request_ops *ops; 310 struct sk_buff *rskb; 311 void *reply_payload; 312 int reply_len; 313 int ret; 314 315 ops = ethnl_default_requests[cmd]; 316 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd)) 317 return -EOPNOTSUPP; 318 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 319 if (!req_info) 320 return -ENOMEM; 321 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 322 if (!reply_data) { 323 kfree(req_info); 324 return -ENOMEM; 325 } 326 327 ret = ethnl_default_parse(req_info, info->nlhdr, genl_info_net(info), ops, 328 info->extack, !ops->allow_nodev_do); 329 if (ret < 0) 330 goto err_dev; 331 ethnl_init_reply_data(reply_data, ops, req_info->dev); 332 333 rtnl_lock(); 334 ret = ops->prepare_data(req_info, reply_data, info); 335 rtnl_unlock(); 336 if (ret < 0) 337 goto err_cleanup; 338 ret = ops->reply_size(req_info, reply_data); 339 if (ret < 0) 340 goto err_cleanup; 341 reply_len = ret; 342 ret = -ENOMEM; 343 rskb = ethnl_reply_init(reply_len, req_info->dev, ops->reply_cmd, 344 ops->hdr_attr, info, &reply_payload); 345 if (!rskb) 346 goto err_cleanup; 347 ret = ops->fill_reply(rskb, req_info, reply_data); 348 if (ret < 0) 349 goto err_msg; 350 if (ops->cleanup_data) 351 ops->cleanup_data(reply_data); 352 353 genlmsg_end(rskb, reply_payload); 354 if (req_info->dev) 355 dev_put(req_info->dev); 356 kfree(reply_data); 357 kfree(req_info); 358 return genlmsg_reply(rskb, info); 359 360 err_msg: 361 WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len); 362 nlmsg_free(rskb); 363 err_cleanup: 364 if (ops->cleanup_data) 365 ops->cleanup_data(reply_data); 366 err_dev: 367 if (req_info->dev) 368 dev_put(req_info->dev); 369 kfree(reply_data); 370 kfree(req_info); 371 return ret; 372 } 373 374 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev, 375 const struct ethnl_dump_ctx *ctx) 376 { 377 int ret; 378 379 ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev); 380 rtnl_lock(); 381 ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL); 382 rtnl_unlock(); 383 if (ret < 0) 384 goto out; 385 ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr); 386 if (ret < 0) 387 goto out; 388 ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data); 389 390 out: 391 if (ctx->ops->cleanup_data) 392 ctx->ops->cleanup_data(ctx->reply_data); 393 ctx->reply_data->dev = NULL; 394 return ret; 395 } 396 397 /* Default ->dumpit() handler for GET requests. Device iteration copied from 398 * rtnl_dump_ifinfo(); we have to be more careful about device hashtable 399 * persistence as we cannot guarantee to hold RTNL lock through the whole 400 * function as rtnetnlink does. 401 */ 402 static int ethnl_default_dumpit(struct sk_buff *skb, 403 struct netlink_callback *cb) 404 { 405 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 406 struct net *net = sock_net(skb->sk); 407 int s_idx = ctx->pos_idx; 408 int h, idx = 0; 409 int ret = 0; 410 void *ehdr; 411 412 rtnl_lock(); 413 for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 414 struct hlist_head *head; 415 struct net_device *dev; 416 unsigned int seq; 417 418 head = &net->dev_index_head[h]; 419 420 restart_chain: 421 seq = net->dev_base_seq; 422 cb->seq = seq; 423 idx = 0; 424 hlist_for_each_entry(dev, head, index_hlist) { 425 if (idx < s_idx) 426 goto cont; 427 dev_hold(dev); 428 rtnl_unlock(); 429 430 ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 431 cb->nlh->nlmsg_seq, 432 ðtool_genl_family, 0, 433 ctx->ops->reply_cmd); 434 if (!ehdr) { 435 dev_put(dev); 436 ret = -EMSGSIZE; 437 goto out; 438 } 439 ret = ethnl_default_dump_one(skb, dev, ctx); 440 dev_put(dev); 441 if (ret < 0) { 442 genlmsg_cancel(skb, ehdr); 443 if (ret == -EOPNOTSUPP) 444 goto lock_and_cont; 445 if (likely(skb->len)) 446 ret = skb->len; 447 goto out; 448 } 449 genlmsg_end(skb, ehdr); 450 lock_and_cont: 451 rtnl_lock(); 452 if (net->dev_base_seq != seq) { 453 s_idx = idx + 1; 454 goto restart_chain; 455 } 456 cont: 457 idx++; 458 } 459 460 } 461 rtnl_unlock(); 462 463 out: 464 ctx->pos_hash = h; 465 ctx->pos_idx = idx; 466 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 467 468 return ret; 469 } 470 471 /* generic ->start() handler for GET requests */ 472 static int ethnl_default_start(struct netlink_callback *cb) 473 { 474 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 475 struct ethnl_reply_data *reply_data; 476 const struct ethnl_request_ops *ops; 477 struct ethnl_req_info *req_info; 478 struct genlmsghdr *ghdr; 479 int ret; 480 481 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 482 483 ghdr = nlmsg_data(cb->nlh); 484 ops = ethnl_default_requests[ghdr->cmd]; 485 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd)) 486 return -EOPNOTSUPP; 487 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 488 if (!req_info) 489 return -ENOMEM; 490 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 491 if (!reply_data) { 492 ret = -ENOMEM; 493 goto free_req_info; 494 } 495 496 ret = ethnl_default_parse(req_info, cb->nlh, sock_net(cb->skb->sk), ops, 497 cb->extack, false); 498 if (req_info->dev) { 499 /* We ignore device specification in dump requests but as the 500 * same parser as for non-dump (doit) requests is used, it 501 * would take reference to the device if it finds one 502 */ 503 dev_put(req_info->dev); 504 req_info->dev = NULL; 505 } 506 if (ret < 0) 507 goto free_reply_data; 508 509 ctx->ops = ops; 510 ctx->req_info = req_info; 511 ctx->reply_data = reply_data; 512 ctx->pos_hash = 0; 513 ctx->pos_idx = 0; 514 515 return 0; 516 517 free_reply_data: 518 kfree(reply_data); 519 free_req_info: 520 kfree(req_info); 521 522 return ret; 523 } 524 525 /* default ->done() handler for GET requests */ 526 static int ethnl_default_done(struct netlink_callback *cb) 527 { 528 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb); 529 530 kfree(ctx->reply_data); 531 kfree(ctx->req_info); 532 533 return 0; 534 } 535 536 static const struct ethnl_request_ops * 537 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = { 538 [ETHTOOL_MSG_LINKINFO_NTF] = ðnl_linkinfo_request_ops, 539 [ETHTOOL_MSG_LINKMODES_NTF] = ðnl_linkmodes_request_ops, 540 [ETHTOOL_MSG_DEBUG_NTF] = ðnl_debug_request_ops, 541 [ETHTOOL_MSG_WOL_NTF] = ðnl_wol_request_ops, 542 [ETHTOOL_MSG_FEATURES_NTF] = ðnl_features_request_ops, 543 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ðnl_privflags_request_ops, 544 [ETHTOOL_MSG_RINGS_NTF] = ðnl_rings_request_ops, 545 [ETHTOOL_MSG_CHANNELS_NTF] = ðnl_channels_request_ops, 546 }; 547 548 /* default notification handler */ 549 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd, 550 const void *data) 551 { 552 struct ethnl_reply_data *reply_data; 553 const struct ethnl_request_ops *ops; 554 struct ethnl_req_info *req_info; 555 struct sk_buff *skb; 556 void *reply_payload; 557 int reply_len; 558 int ret; 559 560 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX || 561 !ethnl_default_notify_ops[cmd], 562 "unexpected notification type %u\n", cmd)) 563 return; 564 ops = ethnl_default_notify_ops[cmd]; 565 req_info = kzalloc(ops->req_info_size, GFP_KERNEL); 566 if (!req_info) 567 return; 568 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL); 569 if (!reply_data) { 570 kfree(req_info); 571 return; 572 } 573 574 req_info->dev = dev; 575 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS; 576 577 ethnl_init_reply_data(reply_data, ops, dev); 578 ret = ops->prepare_data(req_info, reply_data, NULL); 579 if (ret < 0) 580 goto err_cleanup; 581 ret = ops->reply_size(req_info, reply_data); 582 if (ret < 0) 583 goto err_cleanup; 584 reply_len = ret; 585 ret = -ENOMEM; 586 skb = genlmsg_new(reply_len, GFP_KERNEL); 587 if (!skb) 588 goto err_cleanup; 589 reply_payload = ethnl_bcastmsg_put(skb, cmd); 590 if (!reply_payload) 591 goto err_skb; 592 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr); 593 if (ret < 0) 594 goto err_msg; 595 ret = ops->fill_reply(skb, req_info, reply_data); 596 if (ret < 0) 597 goto err_msg; 598 if (ops->cleanup_data) 599 ops->cleanup_data(reply_data); 600 601 genlmsg_end(skb, reply_payload); 602 kfree(reply_data); 603 kfree(req_info); 604 ethnl_multicast(skb, dev); 605 return; 606 607 err_msg: 608 WARN_ONCE(ret == -EMSGSIZE, 609 "calculated message payload length (%d) not sufficient\n", 610 reply_len); 611 err_skb: 612 nlmsg_free(skb); 613 err_cleanup: 614 if (ops->cleanup_data) 615 ops->cleanup_data(reply_data); 616 kfree(reply_data); 617 kfree(req_info); 618 return; 619 } 620 621 /* notifications */ 622 623 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd, 624 const void *data); 625 626 static const ethnl_notify_handler_t ethnl_notify_handlers[] = { 627 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify, 628 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify, 629 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify, 630 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify, 631 [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify, 632 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify, 633 [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify, 634 [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify, 635 }; 636 637 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data) 638 { 639 if (unlikely(!ethnl_ok)) 640 return; 641 ASSERT_RTNL(); 642 643 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) && 644 ethnl_notify_handlers[cmd])) 645 ethnl_notify_handlers[cmd](dev, cmd, data); 646 else 647 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n", 648 cmd, netdev_name(dev)); 649 } 650 EXPORT_SYMBOL(ethtool_notify); 651 652 static void ethnl_notify_features(struct netdev_notifier_info *info) 653 { 654 struct net_device *dev = netdev_notifier_info_to_dev(info); 655 656 ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL); 657 } 658 659 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event, 660 void *ptr) 661 { 662 switch (event) { 663 case NETDEV_FEAT_CHANGE: 664 ethnl_notify_features(ptr); 665 break; 666 } 667 668 return NOTIFY_DONE; 669 } 670 671 static struct notifier_block ethnl_netdev_notifier = { 672 .notifier_call = ethnl_netdev_event, 673 }; 674 675 /* genetlink setup */ 676 677 static const struct genl_ops ethtool_genl_ops[] = { 678 { 679 .cmd = ETHTOOL_MSG_STRSET_GET, 680 .doit = ethnl_default_doit, 681 .start = ethnl_default_start, 682 .dumpit = ethnl_default_dumpit, 683 .done = ethnl_default_done, 684 }, 685 { 686 .cmd = ETHTOOL_MSG_LINKINFO_GET, 687 .doit = ethnl_default_doit, 688 .start = ethnl_default_start, 689 .dumpit = ethnl_default_dumpit, 690 .done = ethnl_default_done, 691 }, 692 { 693 .cmd = ETHTOOL_MSG_LINKINFO_SET, 694 .flags = GENL_UNS_ADMIN_PERM, 695 .doit = ethnl_set_linkinfo, 696 }, 697 { 698 .cmd = ETHTOOL_MSG_LINKMODES_GET, 699 .doit = ethnl_default_doit, 700 .start = ethnl_default_start, 701 .dumpit = ethnl_default_dumpit, 702 .done = ethnl_default_done, 703 }, 704 { 705 .cmd = ETHTOOL_MSG_LINKMODES_SET, 706 .flags = GENL_UNS_ADMIN_PERM, 707 .doit = ethnl_set_linkmodes, 708 }, 709 { 710 .cmd = ETHTOOL_MSG_LINKSTATE_GET, 711 .doit = ethnl_default_doit, 712 .start = ethnl_default_start, 713 .dumpit = ethnl_default_dumpit, 714 .done = ethnl_default_done, 715 }, 716 { 717 .cmd = ETHTOOL_MSG_DEBUG_GET, 718 .doit = ethnl_default_doit, 719 .start = ethnl_default_start, 720 .dumpit = ethnl_default_dumpit, 721 .done = ethnl_default_done, 722 }, 723 { 724 .cmd = ETHTOOL_MSG_DEBUG_SET, 725 .flags = GENL_UNS_ADMIN_PERM, 726 .doit = ethnl_set_debug, 727 }, 728 { 729 .cmd = ETHTOOL_MSG_WOL_GET, 730 .flags = GENL_UNS_ADMIN_PERM, 731 .doit = ethnl_default_doit, 732 .start = ethnl_default_start, 733 .dumpit = ethnl_default_dumpit, 734 .done = ethnl_default_done, 735 }, 736 { 737 .cmd = ETHTOOL_MSG_WOL_SET, 738 .flags = GENL_UNS_ADMIN_PERM, 739 .doit = ethnl_set_wol, 740 }, 741 { 742 .cmd = ETHTOOL_MSG_FEATURES_GET, 743 .doit = ethnl_default_doit, 744 .start = ethnl_default_start, 745 .dumpit = ethnl_default_dumpit, 746 .done = ethnl_default_done, 747 }, 748 { 749 .cmd = ETHTOOL_MSG_FEATURES_SET, 750 .flags = GENL_UNS_ADMIN_PERM, 751 .doit = ethnl_set_features, 752 }, 753 { 754 .cmd = ETHTOOL_MSG_PRIVFLAGS_GET, 755 .doit = ethnl_default_doit, 756 .start = ethnl_default_start, 757 .dumpit = ethnl_default_dumpit, 758 .done = ethnl_default_done, 759 }, 760 { 761 .cmd = ETHTOOL_MSG_PRIVFLAGS_SET, 762 .flags = GENL_UNS_ADMIN_PERM, 763 .doit = ethnl_set_privflags, 764 }, 765 { 766 .cmd = ETHTOOL_MSG_RINGS_GET, 767 .doit = ethnl_default_doit, 768 .start = ethnl_default_start, 769 .dumpit = ethnl_default_dumpit, 770 .done = ethnl_default_done, 771 }, 772 { 773 .cmd = ETHTOOL_MSG_RINGS_SET, 774 .flags = GENL_UNS_ADMIN_PERM, 775 .doit = ethnl_set_rings, 776 }, 777 { 778 .cmd = ETHTOOL_MSG_CHANNELS_GET, 779 .doit = ethnl_default_doit, 780 .start = ethnl_default_start, 781 .dumpit = ethnl_default_dumpit, 782 .done = ethnl_default_done, 783 }, 784 { 785 .cmd = ETHTOOL_MSG_CHANNELS_SET, 786 .flags = GENL_UNS_ADMIN_PERM, 787 .doit = ethnl_set_channels, 788 }, 789 }; 790 791 static const struct genl_multicast_group ethtool_nl_mcgrps[] = { 792 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME }, 793 }; 794 795 static struct genl_family ethtool_genl_family = { 796 .name = ETHTOOL_GENL_NAME, 797 .version = ETHTOOL_GENL_VERSION, 798 .netnsok = true, 799 .parallel_ops = true, 800 .ops = ethtool_genl_ops, 801 .n_ops = ARRAY_SIZE(ethtool_genl_ops), 802 .mcgrps = ethtool_nl_mcgrps, 803 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps), 804 }; 805 806 /* module setup */ 807 808 static int __init ethnl_init(void) 809 { 810 int ret; 811 812 ret = genl_register_family(ðtool_genl_family); 813 if (WARN(ret < 0, "ethtool: genetlink family registration failed")) 814 return ret; 815 ethnl_ok = true; 816 817 ret = register_netdevice_notifier(ðnl_netdev_notifier); 818 WARN(ret < 0, "ethtool: net device notifier registration failed"); 819 return ret; 820 } 821 822 subsys_initcall(ethnl_init); 823