1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/ethtool_netlink.h>
7 static struct genl_family ethtool_genl_family;
9 static bool ethnl_ok __read_mostly;
10 static u32 ethnl_bcast_seq;
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 },
21 * ethnl_parse_header_dev_get() - parse request header
22 * @req_info: structure to put results into
23 * @header: nest attribute with request header
25 * @extack: netlink extack for error reporting
26 * @require_dev: fail if no device identified in header
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
34 * Return: 0 on success or negative error code
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)
40 struct nlattr *tb[ETHTOOL_A_HEADER_MAX + 1];
41 const struct nlattr *devname_attr;
42 struct net_device *dev = NULL;
46 NL_SET_ERR_MSG(extack, "request header missing");
49 ret = nla_parse_nested(tb, ETHTOOL_A_HEADER_MAX, header,
50 ethnl_header_policy, extack);
53 devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
55 if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
56 u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
58 dev = dev_get_by_index(net, ifindex);
60 NL_SET_ERR_MSG_ATTR(extack,
61 tb[ETHTOOL_A_HEADER_DEV_INDEX],
62 "no device matches ifindex");
65 /* if both ifindex and ifname are passed, they must match */
67 strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
69 NL_SET_ERR_MSG_ATTR(extack, header,
70 "ifindex and name do not match");
73 } else if (devname_attr) {
74 dev = dev_get_by_name(net, nla_data(devname_attr));
76 NL_SET_ERR_MSG_ATTR(extack, devname_attr,
77 "no device matches name");
80 } else if (require_dev) {
81 NL_SET_ERR_MSG_ATTR(extack, header,
82 "neither ifindex nor name specified");
86 if (dev && !netif_device_present(dev)) {
88 NL_SET_ERR_MSG(extack, "device not present");
93 if (tb[ETHTOOL_A_HEADER_FLAGS])
94 req_info->flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
100 * ethnl_fill_reply_header() - Put common header into a reply message
101 * @skb: skb with the message
102 * @dev: network device to describe in header
103 * @attrtype: attribute type to use for the nest
105 * Create a nested attribute with attributes describing given network device.
107 * Return: 0 on success, error value (-EMSGSIZE only) on error
109 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
116 nest = nla_nest_start(skb, attrtype);
120 if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
121 nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
122 goto nla_put_failure;
123 /* If more attributes are put into reply header, ethnl_header_size()
124 * must be updated to account for them.
127 nla_nest_end(skb, nest);
131 nla_nest_cancel(skb, nest);
136 * ethnl_reply_init() - Create skb for a reply and fill device identification
137 * @payload: payload length (without netlink and genetlink header)
138 * @dev: device the reply is about (may be null)
139 * @cmd: ETHTOOL_MSG_* message type for reply
140 * @hdr_attrtype: attribute type for common header
141 * @info: genetlink info of the received packet we respond to
142 * @ehdrp: place to store payload pointer returned by genlmsg_new()
144 * Return: pointer to allocated skb on success, NULL on error
146 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
147 u16 hdr_attrtype, struct genl_info *info,
152 skb = genlmsg_new(payload, GFP_KERNEL);
155 *ehdrp = genlmsg_put_reply(skb, info, ðtool_genl_family, 0, cmd);
162 ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
172 GENL_SET_ERR_MSG(info, "failed to setup reply message");
176 static void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
178 return genlmsg_put(skb, 0, ++ethnl_bcast_seq, ðtool_genl_family, 0,
182 static int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
184 return genlmsg_multicast_netns(ðtool_genl_family, dev_net(dev), skb,
185 0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
188 /* GET request helpers */
191 * struct ethnl_dump_ctx - context structure for generic dumpit() callback
192 * @ops: request ops of currently processed message type
193 * @req_info: parsed request header of processed request
194 * @reply_data: data needed to compose the reply
195 * @pos_hash: saved iteration position - hashbucket
196 * @pos_idx: saved iteration position - index
198 * These parameters are kept in struct netlink_callback as context preserved
199 * between iterations. They are initialized by ethnl_default_start() and used
200 * in ethnl_default_dumpit() and ethnl_default_done().
202 struct ethnl_dump_ctx {
203 const struct ethnl_request_ops *ops;
204 struct ethnl_req_info *req_info;
205 struct ethnl_reply_data *reply_data;
210 static const struct ethnl_request_ops *
211 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
212 [ETHTOOL_MSG_STRSET_GET] = ðnl_strset_request_ops,
213 [ETHTOOL_MSG_LINKINFO_GET] = ðnl_linkinfo_request_ops,
214 [ETHTOOL_MSG_LINKMODES_GET] = ðnl_linkmodes_request_ops,
215 [ETHTOOL_MSG_LINKSTATE_GET] = ðnl_linkstate_request_ops,
216 [ETHTOOL_MSG_DEBUG_GET] = ðnl_debug_request_ops,
217 [ETHTOOL_MSG_WOL_GET] = ðnl_wol_request_ops,
218 [ETHTOOL_MSG_FEATURES_GET] = ðnl_features_request_ops,
219 [ETHTOOL_MSG_PRIVFLAGS_GET] = ðnl_privflags_request_ops,
220 [ETHTOOL_MSG_RINGS_GET] = ðnl_rings_request_ops,
221 [ETHTOOL_MSG_CHANNELS_GET] = ðnl_channels_request_ops,
224 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
226 return (struct ethnl_dump_ctx *)cb->ctx;
230 * ethnl_default_parse() - Parse request message
231 * @req_info: pointer to structure to put data into
232 * @nlhdr: pointer to request message header
233 * @net: request netns
234 * @request_ops: struct request_ops for request type
235 * @extack: netlink extack for error reporting
236 * @require_dev: fail if no device identified in header
238 * Parse universal request header and call request specific ->parse_request()
239 * callback (if defined) to parse the rest of the message.
241 * Return: 0 on success or negative error code
243 static int ethnl_default_parse(struct ethnl_req_info *req_info,
244 const struct nlmsghdr *nlhdr, struct net *net,
245 const struct ethnl_request_ops *request_ops,
246 struct netlink_ext_ack *extack, bool require_dev)
251 tb = kmalloc_array(request_ops->max_attr + 1, sizeof(tb[0]),
256 ret = nlmsg_parse(nlhdr, GENL_HDRLEN, tb, request_ops->max_attr,
257 request_ops->request_policy, extack);
260 ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
261 net, extack, require_dev);
265 if (request_ops->parse_request) {
266 ret = request_ops->parse_request(req_info, tb, extack);
278 * ethnl_init_reply_data() - Initialize reply data for GET request
279 * @reply_data: pointer to embedded struct ethnl_reply_data
280 * @ops: instance of struct ethnl_request_ops describing the layout
281 * @dev: network device to initialize the reply for
283 * Fills the reply data part with zeros and sets the dev member. Must be called
284 * before calling the ->fill_reply() callback (for each iteration when handling
287 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
288 const struct ethnl_request_ops *ops,
289 struct net_device *dev)
291 memset(reply_data, 0, ops->reply_data_size);
292 reply_data->dev = dev;
295 /* default ->doit() handler for GET type requests */
296 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
298 struct ethnl_reply_data *reply_data = NULL;
299 struct ethnl_req_info *req_info = NULL;
300 const u8 cmd = info->genlhdr->cmd;
301 const struct ethnl_request_ops *ops;
302 struct sk_buff *rskb;
307 ops = ethnl_default_requests[cmd];
308 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
310 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
313 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
319 ret = ethnl_default_parse(req_info, info->nlhdr, genl_info_net(info), ops,
320 info->extack, !ops->allow_nodev_do);
323 ethnl_init_reply_data(reply_data, ops, req_info->dev);
326 ret = ops->prepare_data(req_info, reply_data, info);
330 ret = ops->reply_size(req_info, reply_data);
335 rskb = ethnl_reply_init(reply_len, req_info->dev, ops->reply_cmd,
336 ops->hdr_attr, info, &reply_payload);
339 ret = ops->fill_reply(rskb, req_info, reply_data);
342 if (ops->cleanup_data)
343 ops->cleanup_data(reply_data);
345 genlmsg_end(rskb, reply_payload);
347 dev_put(req_info->dev);
350 return genlmsg_reply(rskb, info);
353 WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
356 if (ops->cleanup_data)
357 ops->cleanup_data(reply_data);
360 dev_put(req_info->dev);
366 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
367 const struct ethnl_dump_ctx *ctx)
371 ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
373 ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
377 ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
380 ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
383 if (ctx->ops->cleanup_data)
384 ctx->ops->cleanup_data(ctx->reply_data);
385 ctx->reply_data->dev = NULL;
389 /* Default ->dumpit() handler for GET requests. Device iteration copied from
390 * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
391 * persistence as we cannot guarantee to hold RTNL lock through the whole
392 * function as rtnetnlink does.
394 static int ethnl_default_dumpit(struct sk_buff *skb,
395 struct netlink_callback *cb)
397 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
398 struct net *net = sock_net(skb->sk);
399 int s_idx = ctx->pos_idx;
405 for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
406 struct hlist_head *head;
407 struct net_device *dev;
410 head = &net->dev_index_head[h];
413 seq = net->dev_base_seq;
416 hlist_for_each_entry(dev, head, index_hlist) {
422 ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
424 ðtool_genl_family, 0,
425 ctx->ops->reply_cmd);
431 ret = ethnl_default_dump_one(skb, dev, ctx);
434 genlmsg_cancel(skb, ehdr);
435 if (ret == -EOPNOTSUPP)
437 if (likely(skb->len))
441 genlmsg_end(skb, ehdr);
444 if (net->dev_base_seq != seq) {
458 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
463 /* generic ->start() handler for GET requests */
464 static int ethnl_default_start(struct netlink_callback *cb)
466 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
467 struct ethnl_reply_data *reply_data;
468 const struct ethnl_request_ops *ops;
469 struct ethnl_req_info *req_info;
470 struct genlmsghdr *ghdr;
473 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
475 ghdr = nlmsg_data(cb->nlh);
476 ops = ethnl_default_requests[ghdr->cmd];
477 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
479 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
482 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
488 ret = ethnl_default_parse(req_info, cb->nlh, sock_net(cb->skb->sk), ops,
491 /* We ignore device specification in dump requests but as the
492 * same parser as for non-dump (doit) requests is used, it
493 * would take reference to the device if it finds one
495 dev_put(req_info->dev);
496 req_info->dev = NULL;
499 goto free_reply_data;
502 ctx->req_info = req_info;
503 ctx->reply_data = reply_data;
517 /* default ->done() handler for GET requests */
518 static int ethnl_default_done(struct netlink_callback *cb)
520 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
522 kfree(ctx->reply_data);
523 kfree(ctx->req_info);
528 static const struct ethnl_request_ops *
529 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
530 [ETHTOOL_MSG_LINKINFO_NTF] = ðnl_linkinfo_request_ops,
531 [ETHTOOL_MSG_LINKMODES_NTF] = ðnl_linkmodes_request_ops,
532 [ETHTOOL_MSG_DEBUG_NTF] = ðnl_debug_request_ops,
533 [ETHTOOL_MSG_WOL_NTF] = ðnl_wol_request_ops,
534 [ETHTOOL_MSG_FEATURES_NTF] = ðnl_features_request_ops,
535 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ðnl_privflags_request_ops,
536 [ETHTOOL_MSG_RINGS_NTF] = ðnl_rings_request_ops,
537 [ETHTOOL_MSG_CHANNELS_NTF] = ðnl_channels_request_ops,
540 /* default notification handler */
541 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
544 struct ethnl_reply_data *reply_data;
545 const struct ethnl_request_ops *ops;
546 struct ethnl_req_info *req_info;
552 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
553 !ethnl_default_notify_ops[cmd],
554 "unexpected notification type %u\n", cmd))
556 ops = ethnl_default_notify_ops[cmd];
557 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
560 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
567 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
569 ethnl_init_reply_data(reply_data, ops, dev);
570 ret = ops->prepare_data(req_info, reply_data, NULL);
573 ret = ops->reply_size(req_info, reply_data);
578 skb = genlmsg_new(reply_len, GFP_KERNEL);
581 reply_payload = ethnl_bcastmsg_put(skb, cmd);
584 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
587 ret = ops->fill_reply(skb, req_info, reply_data);
590 if (ops->cleanup_data)
591 ops->cleanup_data(reply_data);
593 genlmsg_end(skb, reply_payload);
596 ethnl_multicast(skb, dev);
600 WARN_ONCE(ret == -EMSGSIZE,
601 "calculated message payload length (%d) not sufficient\n",
606 if (ops->cleanup_data)
607 ops->cleanup_data(reply_data);
615 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
618 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
619 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify,
620 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify,
621 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify,
622 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify,
623 [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify,
624 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify,
625 [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify,
626 [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify,
629 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
631 if (unlikely(!ethnl_ok))
635 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
636 ethnl_notify_handlers[cmd]))
637 ethnl_notify_handlers[cmd](dev, cmd, data);
639 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
640 cmd, netdev_name(dev));
642 EXPORT_SYMBOL(ethtool_notify);
644 static void ethnl_notify_features(struct netdev_notifier_info *info)
646 struct net_device *dev = netdev_notifier_info_to_dev(info);
648 ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
651 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
655 case NETDEV_FEAT_CHANGE:
656 ethnl_notify_features(ptr);
663 static struct notifier_block ethnl_netdev_notifier = {
664 .notifier_call = ethnl_netdev_event,
667 /* genetlink setup */
669 static const struct genl_ops ethtool_genl_ops[] = {
671 .cmd = ETHTOOL_MSG_STRSET_GET,
672 .doit = ethnl_default_doit,
673 .start = ethnl_default_start,
674 .dumpit = ethnl_default_dumpit,
675 .done = ethnl_default_done,
678 .cmd = ETHTOOL_MSG_LINKINFO_GET,
679 .doit = ethnl_default_doit,
680 .start = ethnl_default_start,
681 .dumpit = ethnl_default_dumpit,
682 .done = ethnl_default_done,
685 .cmd = ETHTOOL_MSG_LINKINFO_SET,
686 .flags = GENL_UNS_ADMIN_PERM,
687 .doit = ethnl_set_linkinfo,
690 .cmd = ETHTOOL_MSG_LINKMODES_GET,
691 .doit = ethnl_default_doit,
692 .start = ethnl_default_start,
693 .dumpit = ethnl_default_dumpit,
694 .done = ethnl_default_done,
697 .cmd = ETHTOOL_MSG_LINKMODES_SET,
698 .flags = GENL_UNS_ADMIN_PERM,
699 .doit = ethnl_set_linkmodes,
702 .cmd = ETHTOOL_MSG_LINKSTATE_GET,
703 .doit = ethnl_default_doit,
704 .start = ethnl_default_start,
705 .dumpit = ethnl_default_dumpit,
706 .done = ethnl_default_done,
709 .cmd = ETHTOOL_MSG_DEBUG_GET,
710 .doit = ethnl_default_doit,
711 .start = ethnl_default_start,
712 .dumpit = ethnl_default_dumpit,
713 .done = ethnl_default_done,
716 .cmd = ETHTOOL_MSG_DEBUG_SET,
717 .flags = GENL_UNS_ADMIN_PERM,
718 .doit = ethnl_set_debug,
721 .cmd = ETHTOOL_MSG_WOL_GET,
722 .flags = GENL_UNS_ADMIN_PERM,
723 .doit = ethnl_default_doit,
724 .start = ethnl_default_start,
725 .dumpit = ethnl_default_dumpit,
726 .done = ethnl_default_done,
729 .cmd = ETHTOOL_MSG_WOL_SET,
730 .flags = GENL_UNS_ADMIN_PERM,
731 .doit = ethnl_set_wol,
734 .cmd = ETHTOOL_MSG_FEATURES_GET,
735 .doit = ethnl_default_doit,
736 .start = ethnl_default_start,
737 .dumpit = ethnl_default_dumpit,
738 .done = ethnl_default_done,
741 .cmd = ETHTOOL_MSG_FEATURES_SET,
742 .flags = GENL_UNS_ADMIN_PERM,
743 .doit = ethnl_set_features,
746 .cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
747 .doit = ethnl_default_doit,
748 .start = ethnl_default_start,
749 .dumpit = ethnl_default_dumpit,
750 .done = ethnl_default_done,
753 .cmd = ETHTOOL_MSG_PRIVFLAGS_SET,
754 .flags = GENL_UNS_ADMIN_PERM,
755 .doit = ethnl_set_privflags,
758 .cmd = ETHTOOL_MSG_RINGS_GET,
759 .doit = ethnl_default_doit,
760 .start = ethnl_default_start,
761 .dumpit = ethnl_default_dumpit,
762 .done = ethnl_default_done,
765 .cmd = ETHTOOL_MSG_RINGS_SET,
766 .flags = GENL_UNS_ADMIN_PERM,
767 .doit = ethnl_set_rings,
770 .cmd = ETHTOOL_MSG_CHANNELS_GET,
771 .doit = ethnl_default_doit,
772 .start = ethnl_default_start,
773 .dumpit = ethnl_default_dumpit,
774 .done = ethnl_default_done,
777 .cmd = ETHTOOL_MSG_CHANNELS_SET,
778 .flags = GENL_UNS_ADMIN_PERM,
779 .doit = ethnl_set_channels,
783 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
784 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
787 static struct genl_family ethtool_genl_family = {
788 .name = ETHTOOL_GENL_NAME,
789 .version = ETHTOOL_GENL_VERSION,
791 .parallel_ops = true,
792 .ops = ethtool_genl_ops,
793 .n_ops = ARRAY_SIZE(ethtool_genl_ops),
794 .mcgrps = ethtool_nl_mcgrps,
795 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps),
800 static int __init ethnl_init(void)
804 ret = genl_register_family(ðtool_genl_family);
805 if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
809 ret = register_netdevice_notifier(ðnl_netdev_notifier);
810 WARN(ret < 0, "ethtool: net device notifier registration failed");
814 subsys_initcall(ethnl_init);