xref: /kernel/linux/linux-6.6/net/ethtool/netlink.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include <net/sock.h>
462306a36Sopenharmony_ci#include <linux/ethtool_netlink.h>
562306a36Sopenharmony_ci#include <linux/pm_runtime.h>
662306a36Sopenharmony_ci#include "netlink.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_cistatic struct genl_family ethtool_genl_family;
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistatic bool ethnl_ok __read_mostly;
1162306a36Sopenharmony_cistatic u32 ethnl_bcast_seq;
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS |	\
1462306a36Sopenharmony_ci			     ETHTOOL_FLAG_OMIT_REPLY)
1562306a36Sopenharmony_ci#define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS)
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciconst struct nla_policy ethnl_header_policy[] = {
1862306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
1962306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
2062306a36Sopenharmony_ci					    .len = ALTIFNAMSIZ - 1 },
2162306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
2262306a36Sopenharmony_ci							  ETHTOOL_FLAGS_BASIC),
2362306a36Sopenharmony_ci};
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciconst struct nla_policy ethnl_header_policy_stats[] = {
2662306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
2762306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
2862306a36Sopenharmony_ci					    .len = ALTIFNAMSIZ - 1 },
2962306a36Sopenharmony_ci	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
3062306a36Sopenharmony_ci							  ETHTOOL_FLAGS_STATS),
3162306a36Sopenharmony_ci};
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciint ethnl_ops_begin(struct net_device *dev)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	int ret;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	if (!dev)
3862306a36Sopenharmony_ci		return -ENODEV;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	if (dev->dev.parent)
4162306a36Sopenharmony_ci		pm_runtime_get_sync(dev->dev.parent);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	if (!netif_device_present(dev) ||
4462306a36Sopenharmony_ci	    dev->reg_state == NETREG_UNREGISTERING) {
4562306a36Sopenharmony_ci		ret = -ENODEV;
4662306a36Sopenharmony_ci		goto err;
4762306a36Sopenharmony_ci	}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	if (dev->ethtool_ops->begin) {
5062306a36Sopenharmony_ci		ret = dev->ethtool_ops->begin(dev);
5162306a36Sopenharmony_ci		if (ret)
5262306a36Sopenharmony_ci			goto err;
5362306a36Sopenharmony_ci	}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	return 0;
5662306a36Sopenharmony_cierr:
5762306a36Sopenharmony_ci	if (dev->dev.parent)
5862306a36Sopenharmony_ci		pm_runtime_put(dev->dev.parent);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	return ret;
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_civoid ethnl_ops_complete(struct net_device *dev)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	if (dev->ethtool_ops->complete)
6662306a36Sopenharmony_ci		dev->ethtool_ops->complete(dev);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (dev->dev.parent)
6962306a36Sopenharmony_ci		pm_runtime_put(dev->dev.parent);
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/**
7362306a36Sopenharmony_ci * ethnl_parse_header_dev_get() - parse request header
7462306a36Sopenharmony_ci * @req_info:    structure to put results into
7562306a36Sopenharmony_ci * @header:      nest attribute with request header
7662306a36Sopenharmony_ci * @net:         request netns
7762306a36Sopenharmony_ci * @extack:      netlink extack for error reporting
7862306a36Sopenharmony_ci * @require_dev: fail if no device identified in header
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * Parse request header in nested attribute @nest and puts results into
8162306a36Sopenharmony_ci * the structure pointed to by @req_info. Extack from @info is used for error
8262306a36Sopenharmony_ci * reporting. If req_info->dev is not null on return, reference to it has
8362306a36Sopenharmony_ci * been taken. If error is returned, *req_info is null initialized and no
8462306a36Sopenharmony_ci * reference is held.
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci * Return: 0 on success or negative error code
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ciint ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
8962306a36Sopenharmony_ci			       const struct nlattr *header, struct net *net,
9062306a36Sopenharmony_ci			       struct netlink_ext_ack *extack, bool require_dev)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy)];
9362306a36Sopenharmony_ci	const struct nlattr *devname_attr;
9462306a36Sopenharmony_ci	struct net_device *dev = NULL;
9562306a36Sopenharmony_ci	u32 flags = 0;
9662306a36Sopenharmony_ci	int ret;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	if (!header) {
9962306a36Sopenharmony_ci		if (!require_dev)
10062306a36Sopenharmony_ci			return 0;
10162306a36Sopenharmony_ci		NL_SET_ERR_MSG(extack, "request header missing");
10262306a36Sopenharmony_ci		return -EINVAL;
10362306a36Sopenharmony_ci	}
10462306a36Sopenharmony_ci	/* No validation here, command policy should have a nested policy set
10562306a36Sopenharmony_ci	 * for the header, therefore validation should have already been done.
10662306a36Sopenharmony_ci	 */
10762306a36Sopenharmony_ci	ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy) - 1, header,
10862306a36Sopenharmony_ci			       NULL, extack);
10962306a36Sopenharmony_ci	if (ret < 0)
11062306a36Sopenharmony_ci		return ret;
11162306a36Sopenharmony_ci	if (tb[ETHTOOL_A_HEADER_FLAGS])
11262306a36Sopenharmony_ci		flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
11562306a36Sopenharmony_ci	if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
11662306a36Sopenharmony_ci		u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci		dev = netdev_get_by_index(net, ifindex, &req_info->dev_tracker,
11962306a36Sopenharmony_ci					  GFP_KERNEL);
12062306a36Sopenharmony_ci		if (!dev) {
12162306a36Sopenharmony_ci			NL_SET_ERR_MSG_ATTR(extack,
12262306a36Sopenharmony_ci					    tb[ETHTOOL_A_HEADER_DEV_INDEX],
12362306a36Sopenharmony_ci					    "no device matches ifindex");
12462306a36Sopenharmony_ci			return -ENODEV;
12562306a36Sopenharmony_ci		}
12662306a36Sopenharmony_ci		/* if both ifindex and ifname are passed, they must match */
12762306a36Sopenharmony_ci		if (devname_attr &&
12862306a36Sopenharmony_ci		    strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
12962306a36Sopenharmony_ci			netdev_put(dev, &req_info->dev_tracker);
13062306a36Sopenharmony_ci			NL_SET_ERR_MSG_ATTR(extack, header,
13162306a36Sopenharmony_ci					    "ifindex and name do not match");
13262306a36Sopenharmony_ci			return -ENODEV;
13362306a36Sopenharmony_ci		}
13462306a36Sopenharmony_ci	} else if (devname_attr) {
13562306a36Sopenharmony_ci		dev = netdev_get_by_name(net, nla_data(devname_attr),
13662306a36Sopenharmony_ci					 &req_info->dev_tracker, GFP_KERNEL);
13762306a36Sopenharmony_ci		if (!dev) {
13862306a36Sopenharmony_ci			NL_SET_ERR_MSG_ATTR(extack, devname_attr,
13962306a36Sopenharmony_ci					    "no device matches name");
14062306a36Sopenharmony_ci			return -ENODEV;
14162306a36Sopenharmony_ci		}
14262306a36Sopenharmony_ci	} else if (require_dev) {
14362306a36Sopenharmony_ci		NL_SET_ERR_MSG_ATTR(extack, header,
14462306a36Sopenharmony_ci				    "neither ifindex nor name specified");
14562306a36Sopenharmony_ci		return -EINVAL;
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	req_info->dev = dev;
14962306a36Sopenharmony_ci	req_info->flags = flags;
15062306a36Sopenharmony_ci	return 0;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/**
15462306a36Sopenharmony_ci * ethnl_fill_reply_header() - Put common header into a reply message
15562306a36Sopenharmony_ci * @skb:      skb with the message
15662306a36Sopenharmony_ci * @dev:      network device to describe in header
15762306a36Sopenharmony_ci * @attrtype: attribute type to use for the nest
15862306a36Sopenharmony_ci *
15962306a36Sopenharmony_ci * Create a nested attribute with attributes describing given network device.
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * Return: 0 on success, error value (-EMSGSIZE only) on error
16262306a36Sopenharmony_ci */
16362306a36Sopenharmony_ciint ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
16462306a36Sopenharmony_ci			    u16 attrtype)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	struct nlattr *nest;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	if (!dev)
16962306a36Sopenharmony_ci		return 0;
17062306a36Sopenharmony_ci	nest = nla_nest_start(skb, attrtype);
17162306a36Sopenharmony_ci	if (!nest)
17262306a36Sopenharmony_ci		return -EMSGSIZE;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
17562306a36Sopenharmony_ci	    nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
17662306a36Sopenharmony_ci		goto nla_put_failure;
17762306a36Sopenharmony_ci	/* If more attributes are put into reply header, ethnl_header_size()
17862306a36Sopenharmony_ci	 * must be updated to account for them.
17962306a36Sopenharmony_ci	 */
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	nla_nest_end(skb, nest);
18262306a36Sopenharmony_ci	return 0;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cinla_put_failure:
18562306a36Sopenharmony_ci	nla_nest_cancel(skb, nest);
18662306a36Sopenharmony_ci	return -EMSGSIZE;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/**
19062306a36Sopenharmony_ci * ethnl_reply_init() - Create skb for a reply and fill device identification
19162306a36Sopenharmony_ci * @payload:      payload length (without netlink and genetlink header)
19262306a36Sopenharmony_ci * @dev:          device the reply is about (may be null)
19362306a36Sopenharmony_ci * @cmd:          ETHTOOL_MSG_* message type for reply
19462306a36Sopenharmony_ci * @hdr_attrtype: attribute type for common header
19562306a36Sopenharmony_ci * @info:         genetlink info of the received packet we respond to
19662306a36Sopenharmony_ci * @ehdrp:        place to store payload pointer returned by genlmsg_new()
19762306a36Sopenharmony_ci *
19862306a36Sopenharmony_ci * Return: pointer to allocated skb on success, NULL on error
19962306a36Sopenharmony_ci */
20062306a36Sopenharmony_cistruct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
20162306a36Sopenharmony_ci				 u16 hdr_attrtype, struct genl_info *info,
20262306a36Sopenharmony_ci				 void **ehdrp)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	struct sk_buff *skb;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	skb = genlmsg_new(payload, GFP_KERNEL);
20762306a36Sopenharmony_ci	if (!skb)
20862306a36Sopenharmony_ci		goto err;
20962306a36Sopenharmony_ci	*ehdrp = genlmsg_put_reply(skb, info, &ethtool_genl_family, 0, cmd);
21062306a36Sopenharmony_ci	if (!*ehdrp)
21162306a36Sopenharmony_ci		goto err_free;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	if (dev) {
21462306a36Sopenharmony_ci		int ret;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci		ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
21762306a36Sopenharmony_ci		if (ret < 0)
21862306a36Sopenharmony_ci			goto err_free;
21962306a36Sopenharmony_ci	}
22062306a36Sopenharmony_ci	return skb;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_cierr_free:
22362306a36Sopenharmony_ci	nlmsg_free(skb);
22462306a36Sopenharmony_cierr:
22562306a36Sopenharmony_ci	if (info)
22662306a36Sopenharmony_ci		GENL_SET_ERR_MSG(info, "failed to setup reply message");
22762306a36Sopenharmony_ci	return NULL;
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_civoid *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
23362306a36Sopenharmony_ci			   &ethtool_genl_family, 0, cmd);
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_civoid *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
23962306a36Sopenharmony_ci			   cmd);
24062306a36Sopenharmony_ci}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciint ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
24362306a36Sopenharmony_ci{
24462306a36Sopenharmony_ci	return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
24562306a36Sopenharmony_ci				       0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/* GET request helpers */
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci/**
25162306a36Sopenharmony_ci * struct ethnl_dump_ctx - context structure for generic dumpit() callback
25262306a36Sopenharmony_ci * @ops:        request ops of currently processed message type
25362306a36Sopenharmony_ci * @req_info:   parsed request header of processed request
25462306a36Sopenharmony_ci * @reply_data: data needed to compose the reply
25562306a36Sopenharmony_ci * @pos_ifindex: saved iteration position - ifindex
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci * These parameters are kept in struct netlink_callback as context preserved
25862306a36Sopenharmony_ci * between iterations. They are initialized by ethnl_default_start() and used
25962306a36Sopenharmony_ci * in ethnl_default_dumpit() and ethnl_default_done().
26062306a36Sopenharmony_ci */
26162306a36Sopenharmony_cistruct ethnl_dump_ctx {
26262306a36Sopenharmony_ci	const struct ethnl_request_ops	*ops;
26362306a36Sopenharmony_ci	struct ethnl_req_info		*req_info;
26462306a36Sopenharmony_ci	struct ethnl_reply_data		*reply_data;
26562306a36Sopenharmony_ci	unsigned long			pos_ifindex;
26662306a36Sopenharmony_ci};
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_cistatic const struct ethnl_request_ops *
26962306a36Sopenharmony_ciethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
27062306a36Sopenharmony_ci	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
27162306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKINFO_GET]	= &ethnl_linkinfo_request_ops,
27262306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKINFO_SET]	= &ethnl_linkinfo_request_ops,
27362306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKMODES_GET]	= &ethnl_linkmodes_request_ops,
27462306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKMODES_SET]	= &ethnl_linkmodes_request_ops,
27562306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKSTATE_GET]	= &ethnl_linkstate_request_ops,
27662306a36Sopenharmony_ci	[ETHTOOL_MSG_DEBUG_GET]		= &ethnl_debug_request_ops,
27762306a36Sopenharmony_ci	[ETHTOOL_MSG_DEBUG_SET]		= &ethnl_debug_request_ops,
27862306a36Sopenharmony_ci	[ETHTOOL_MSG_WOL_GET]		= &ethnl_wol_request_ops,
27962306a36Sopenharmony_ci	[ETHTOOL_MSG_WOL_SET]		= &ethnl_wol_request_ops,
28062306a36Sopenharmony_ci	[ETHTOOL_MSG_FEATURES_GET]	= &ethnl_features_request_ops,
28162306a36Sopenharmony_ci	[ETHTOOL_MSG_PRIVFLAGS_GET]	= &ethnl_privflags_request_ops,
28262306a36Sopenharmony_ci	[ETHTOOL_MSG_PRIVFLAGS_SET]	= &ethnl_privflags_request_ops,
28362306a36Sopenharmony_ci	[ETHTOOL_MSG_RINGS_GET]		= &ethnl_rings_request_ops,
28462306a36Sopenharmony_ci	[ETHTOOL_MSG_RINGS_SET]		= &ethnl_rings_request_ops,
28562306a36Sopenharmony_ci	[ETHTOOL_MSG_CHANNELS_GET]	= &ethnl_channels_request_ops,
28662306a36Sopenharmony_ci	[ETHTOOL_MSG_CHANNELS_SET]	= &ethnl_channels_request_ops,
28762306a36Sopenharmony_ci	[ETHTOOL_MSG_COALESCE_GET]	= &ethnl_coalesce_request_ops,
28862306a36Sopenharmony_ci	[ETHTOOL_MSG_COALESCE_SET]	= &ethnl_coalesce_request_ops,
28962306a36Sopenharmony_ci	[ETHTOOL_MSG_PAUSE_GET]		= &ethnl_pause_request_ops,
29062306a36Sopenharmony_ci	[ETHTOOL_MSG_PAUSE_SET]		= &ethnl_pause_request_ops,
29162306a36Sopenharmony_ci	[ETHTOOL_MSG_EEE_GET]		= &ethnl_eee_request_ops,
29262306a36Sopenharmony_ci	[ETHTOOL_MSG_EEE_SET]		= &ethnl_eee_request_ops,
29362306a36Sopenharmony_ci	[ETHTOOL_MSG_FEC_GET]		= &ethnl_fec_request_ops,
29462306a36Sopenharmony_ci	[ETHTOOL_MSG_FEC_SET]		= &ethnl_fec_request_ops,
29562306a36Sopenharmony_ci	[ETHTOOL_MSG_TSINFO_GET]	= &ethnl_tsinfo_request_ops,
29662306a36Sopenharmony_ci	[ETHTOOL_MSG_MODULE_EEPROM_GET]	= &ethnl_module_eeprom_request_ops,
29762306a36Sopenharmony_ci	[ETHTOOL_MSG_STATS_GET]		= &ethnl_stats_request_ops,
29862306a36Sopenharmony_ci	[ETHTOOL_MSG_PHC_VCLOCKS_GET]	= &ethnl_phc_vclocks_request_ops,
29962306a36Sopenharmony_ci	[ETHTOOL_MSG_MODULE_GET]	= &ethnl_module_request_ops,
30062306a36Sopenharmony_ci	[ETHTOOL_MSG_MODULE_SET]	= &ethnl_module_request_ops,
30162306a36Sopenharmony_ci	[ETHTOOL_MSG_PSE_GET]		= &ethnl_pse_request_ops,
30262306a36Sopenharmony_ci	[ETHTOOL_MSG_PSE_SET]		= &ethnl_pse_request_ops,
30362306a36Sopenharmony_ci	[ETHTOOL_MSG_RSS_GET]		= &ethnl_rss_request_ops,
30462306a36Sopenharmony_ci	[ETHTOOL_MSG_PLCA_GET_CFG]	= &ethnl_plca_cfg_request_ops,
30562306a36Sopenharmony_ci	[ETHTOOL_MSG_PLCA_SET_CFG]	= &ethnl_plca_cfg_request_ops,
30662306a36Sopenharmony_ci	[ETHTOOL_MSG_PLCA_GET_STATUS]	= &ethnl_plca_status_request_ops,
30762306a36Sopenharmony_ci	[ETHTOOL_MSG_MM_GET]		= &ethnl_mm_request_ops,
30862306a36Sopenharmony_ci	[ETHTOOL_MSG_MM_SET]		= &ethnl_mm_request_ops,
30962306a36Sopenharmony_ci};
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	return (struct ethnl_dump_ctx *)cb->ctx;
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci/**
31762306a36Sopenharmony_ci * ethnl_default_parse() - Parse request message
31862306a36Sopenharmony_ci * @req_info:    pointer to structure to put data into
31962306a36Sopenharmony_ci * @info:	 genl_info from the request
32062306a36Sopenharmony_ci * @request_ops: struct request_ops for request type
32162306a36Sopenharmony_ci * @require_dev: fail if no device identified in header
32262306a36Sopenharmony_ci *
32362306a36Sopenharmony_ci * Parse universal request header and call request specific ->parse_request()
32462306a36Sopenharmony_ci * callback (if defined) to parse the rest of the message.
32562306a36Sopenharmony_ci *
32662306a36Sopenharmony_ci * Return: 0 on success or negative error code
32762306a36Sopenharmony_ci */
32862306a36Sopenharmony_cistatic int ethnl_default_parse(struct ethnl_req_info *req_info,
32962306a36Sopenharmony_ci			       const struct genl_info *info,
33062306a36Sopenharmony_ci			       const struct ethnl_request_ops *request_ops,
33162306a36Sopenharmony_ci			       bool require_dev)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	struct nlattr **tb = info->attrs;
33462306a36Sopenharmony_ci	int ret;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
33762306a36Sopenharmony_ci					 genl_info_net(info), info->extack,
33862306a36Sopenharmony_ci					 require_dev);
33962306a36Sopenharmony_ci	if (ret < 0)
34062306a36Sopenharmony_ci		return ret;
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	if (request_ops->parse_request) {
34362306a36Sopenharmony_ci		ret = request_ops->parse_request(req_info, tb, info->extack);
34462306a36Sopenharmony_ci		if (ret < 0)
34562306a36Sopenharmony_ci			return ret;
34662306a36Sopenharmony_ci	}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	return 0;
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci/**
35262306a36Sopenharmony_ci * ethnl_init_reply_data() - Initialize reply data for GET request
35362306a36Sopenharmony_ci * @reply_data: pointer to embedded struct ethnl_reply_data
35462306a36Sopenharmony_ci * @ops:        instance of struct ethnl_request_ops describing the layout
35562306a36Sopenharmony_ci * @dev:        network device to initialize the reply for
35662306a36Sopenharmony_ci *
35762306a36Sopenharmony_ci * Fills the reply data part with zeros and sets the dev member. Must be called
35862306a36Sopenharmony_ci * before calling the ->fill_reply() callback (for each iteration when handling
35962306a36Sopenharmony_ci * dump requests).
36062306a36Sopenharmony_ci */
36162306a36Sopenharmony_cistatic void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
36262306a36Sopenharmony_ci				  const struct ethnl_request_ops *ops,
36362306a36Sopenharmony_ci				  struct net_device *dev)
36462306a36Sopenharmony_ci{
36562306a36Sopenharmony_ci	memset(reply_data, 0, ops->reply_data_size);
36662306a36Sopenharmony_ci	reply_data->dev = dev;
36762306a36Sopenharmony_ci}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci/* default ->doit() handler for GET type requests */
37062306a36Sopenharmony_cistatic int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
37162306a36Sopenharmony_ci{
37262306a36Sopenharmony_ci	struct ethnl_reply_data *reply_data = NULL;
37362306a36Sopenharmony_ci	struct ethnl_req_info *req_info = NULL;
37462306a36Sopenharmony_ci	const u8 cmd = info->genlhdr->cmd;
37562306a36Sopenharmony_ci	const struct ethnl_request_ops *ops;
37662306a36Sopenharmony_ci	int hdr_len, reply_len;
37762306a36Sopenharmony_ci	struct sk_buff *rskb;
37862306a36Sopenharmony_ci	void *reply_payload;
37962306a36Sopenharmony_ci	int ret;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	ops = ethnl_default_requests[cmd];
38262306a36Sopenharmony_ci	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
38362306a36Sopenharmony_ci		return -EOPNOTSUPP;
38462306a36Sopenharmony_ci	if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
38562306a36Sopenharmony_ci		return -EINVAL;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
38862306a36Sopenharmony_ci	if (!req_info)
38962306a36Sopenharmony_ci		return -ENOMEM;
39062306a36Sopenharmony_ci	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
39162306a36Sopenharmony_ci	if (!reply_data) {
39262306a36Sopenharmony_ci		kfree(req_info);
39362306a36Sopenharmony_ci		return -ENOMEM;
39462306a36Sopenharmony_ci	}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	ret = ethnl_default_parse(req_info, info, ops, !ops->allow_nodev_do);
39762306a36Sopenharmony_ci	if (ret < 0)
39862306a36Sopenharmony_ci		goto err_dev;
39962306a36Sopenharmony_ci	ethnl_init_reply_data(reply_data, ops, req_info->dev);
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	rtnl_lock();
40262306a36Sopenharmony_ci	ret = ops->prepare_data(req_info, reply_data, info);
40362306a36Sopenharmony_ci	rtnl_unlock();
40462306a36Sopenharmony_ci	if (ret < 0)
40562306a36Sopenharmony_ci		goto err_cleanup;
40662306a36Sopenharmony_ci	ret = ops->reply_size(req_info, reply_data);
40762306a36Sopenharmony_ci	if (ret < 0)
40862306a36Sopenharmony_ci		goto err_cleanup;
40962306a36Sopenharmony_ci	reply_len = ret;
41062306a36Sopenharmony_ci	ret = -ENOMEM;
41162306a36Sopenharmony_ci	rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(),
41262306a36Sopenharmony_ci				req_info->dev, ops->reply_cmd,
41362306a36Sopenharmony_ci				ops->hdr_attr, info, &reply_payload);
41462306a36Sopenharmony_ci	if (!rskb)
41562306a36Sopenharmony_ci		goto err_cleanup;
41662306a36Sopenharmony_ci	hdr_len = rskb->len;
41762306a36Sopenharmony_ci	ret = ops->fill_reply(rskb, req_info, reply_data);
41862306a36Sopenharmony_ci	if (ret < 0)
41962306a36Sopenharmony_ci		goto err_msg;
42062306a36Sopenharmony_ci	WARN_ONCE(rskb->len - hdr_len > reply_len,
42162306a36Sopenharmony_ci		  "ethnl cmd %d: calculated reply length %d, but consumed %d\n",
42262306a36Sopenharmony_ci		  cmd, reply_len, rskb->len - hdr_len);
42362306a36Sopenharmony_ci	if (ops->cleanup_data)
42462306a36Sopenharmony_ci		ops->cleanup_data(reply_data);
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	genlmsg_end(rskb, reply_payload);
42762306a36Sopenharmony_ci	netdev_put(req_info->dev, &req_info->dev_tracker);
42862306a36Sopenharmony_ci	kfree(reply_data);
42962306a36Sopenharmony_ci	kfree(req_info);
43062306a36Sopenharmony_ci	return genlmsg_reply(rskb, info);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_cierr_msg:
43362306a36Sopenharmony_ci	WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
43462306a36Sopenharmony_ci	nlmsg_free(rskb);
43562306a36Sopenharmony_cierr_cleanup:
43662306a36Sopenharmony_ci	if (ops->cleanup_data)
43762306a36Sopenharmony_ci		ops->cleanup_data(reply_data);
43862306a36Sopenharmony_cierr_dev:
43962306a36Sopenharmony_ci	netdev_put(req_info->dev, &req_info->dev_tracker);
44062306a36Sopenharmony_ci	kfree(reply_data);
44162306a36Sopenharmony_ci	kfree(req_info);
44262306a36Sopenharmony_ci	return ret;
44362306a36Sopenharmony_ci}
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_cistatic int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
44662306a36Sopenharmony_ci				  const struct ethnl_dump_ctx *ctx,
44762306a36Sopenharmony_ci				  const struct genl_info *info)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	void *ehdr;
45062306a36Sopenharmony_ci	int ret;
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	ehdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
45362306a36Sopenharmony_ci			   &ethtool_genl_family, NLM_F_MULTI,
45462306a36Sopenharmony_ci			   ctx->ops->reply_cmd);
45562306a36Sopenharmony_ci	if (!ehdr)
45662306a36Sopenharmony_ci		return -EMSGSIZE;
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
45962306a36Sopenharmony_ci	rtnl_lock();
46062306a36Sopenharmony_ci	ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, info);
46162306a36Sopenharmony_ci	rtnl_unlock();
46262306a36Sopenharmony_ci	if (ret < 0)
46362306a36Sopenharmony_ci		goto out;
46462306a36Sopenharmony_ci	ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
46562306a36Sopenharmony_ci	if (ret < 0)
46662306a36Sopenharmony_ci		goto out;
46762306a36Sopenharmony_ci	ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ciout:
47062306a36Sopenharmony_ci	if (ctx->ops->cleanup_data)
47162306a36Sopenharmony_ci		ctx->ops->cleanup_data(ctx->reply_data);
47262306a36Sopenharmony_ci	ctx->reply_data->dev = NULL;
47362306a36Sopenharmony_ci	if (ret < 0)
47462306a36Sopenharmony_ci		genlmsg_cancel(skb, ehdr);
47562306a36Sopenharmony_ci	else
47662306a36Sopenharmony_ci		genlmsg_end(skb, ehdr);
47762306a36Sopenharmony_ci	return ret;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/* Default ->dumpit() handler for GET requests. Device iteration copied from
48162306a36Sopenharmony_ci * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
48262306a36Sopenharmony_ci * persistence as we cannot guarantee to hold RTNL lock through the whole
48362306a36Sopenharmony_ci * function as rtnetnlink does.
48462306a36Sopenharmony_ci */
48562306a36Sopenharmony_cistatic int ethnl_default_dumpit(struct sk_buff *skb,
48662306a36Sopenharmony_ci				struct netlink_callback *cb)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
48962306a36Sopenharmony_ci	struct net *net = sock_net(skb->sk);
49062306a36Sopenharmony_ci	struct net_device *dev;
49162306a36Sopenharmony_ci	int ret = 0;
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	rtnl_lock();
49462306a36Sopenharmony_ci	for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
49562306a36Sopenharmony_ci		dev_hold(dev);
49662306a36Sopenharmony_ci		rtnl_unlock();
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci		ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci		rtnl_lock();
50162306a36Sopenharmony_ci		dev_put(dev);
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci		if (ret < 0 && ret != -EOPNOTSUPP) {
50462306a36Sopenharmony_ci			if (likely(skb->len))
50562306a36Sopenharmony_ci				ret = skb->len;
50662306a36Sopenharmony_ci			break;
50762306a36Sopenharmony_ci		}
50862306a36Sopenharmony_ci		ret = 0;
50962306a36Sopenharmony_ci	}
51062306a36Sopenharmony_ci	rtnl_unlock();
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci	return ret;
51362306a36Sopenharmony_ci}
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci/* generic ->start() handler for GET requests */
51662306a36Sopenharmony_cistatic int ethnl_default_start(struct netlink_callback *cb)
51762306a36Sopenharmony_ci{
51862306a36Sopenharmony_ci	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
51962306a36Sopenharmony_ci	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
52062306a36Sopenharmony_ci	struct ethnl_reply_data *reply_data;
52162306a36Sopenharmony_ci	const struct ethnl_request_ops *ops;
52262306a36Sopenharmony_ci	struct ethnl_req_info *req_info;
52362306a36Sopenharmony_ci	struct genlmsghdr *ghdr;
52462306a36Sopenharmony_ci	int ret;
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	ghdr = nlmsg_data(cb->nlh);
52962306a36Sopenharmony_ci	ops = ethnl_default_requests[ghdr->cmd];
53062306a36Sopenharmony_ci	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
53162306a36Sopenharmony_ci		return -EOPNOTSUPP;
53262306a36Sopenharmony_ci	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
53362306a36Sopenharmony_ci	if (!req_info)
53462306a36Sopenharmony_ci		return -ENOMEM;
53562306a36Sopenharmony_ci	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
53662306a36Sopenharmony_ci	if (!reply_data) {
53762306a36Sopenharmony_ci		ret = -ENOMEM;
53862306a36Sopenharmony_ci		goto free_req_info;
53962306a36Sopenharmony_ci	}
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci	ret = ethnl_default_parse(req_info, &info->info, ops, false);
54262306a36Sopenharmony_ci	if (req_info->dev) {
54362306a36Sopenharmony_ci		/* We ignore device specification in dump requests but as the
54462306a36Sopenharmony_ci		 * same parser as for non-dump (doit) requests is used, it
54562306a36Sopenharmony_ci		 * would take reference to the device if it finds one
54662306a36Sopenharmony_ci		 */
54762306a36Sopenharmony_ci		netdev_put(req_info->dev, &req_info->dev_tracker);
54862306a36Sopenharmony_ci		req_info->dev = NULL;
54962306a36Sopenharmony_ci	}
55062306a36Sopenharmony_ci	if (ret < 0)
55162306a36Sopenharmony_ci		goto free_reply_data;
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	ctx->ops = ops;
55462306a36Sopenharmony_ci	ctx->req_info = req_info;
55562306a36Sopenharmony_ci	ctx->reply_data = reply_data;
55662306a36Sopenharmony_ci	ctx->pos_ifindex = 0;
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	return 0;
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_cifree_reply_data:
56162306a36Sopenharmony_ci	kfree(reply_data);
56262306a36Sopenharmony_cifree_req_info:
56362306a36Sopenharmony_ci	kfree(req_info);
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci	return ret;
56662306a36Sopenharmony_ci}
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci/* default ->done() handler for GET requests */
56962306a36Sopenharmony_cistatic int ethnl_default_done(struct netlink_callback *cb)
57062306a36Sopenharmony_ci{
57162306a36Sopenharmony_ci	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	kfree(ctx->reply_data);
57462306a36Sopenharmony_ci	kfree(ctx->req_info);
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	return 0;
57762306a36Sopenharmony_ci}
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_cistatic int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)
58062306a36Sopenharmony_ci{
58162306a36Sopenharmony_ci	const struct ethnl_request_ops *ops;
58262306a36Sopenharmony_ci	struct ethnl_req_info req_info = {};
58362306a36Sopenharmony_ci	const u8 cmd = info->genlhdr->cmd;
58462306a36Sopenharmony_ci	int ret;
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci	ops = ethnl_default_requests[cmd];
58762306a36Sopenharmony_ci	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
58862306a36Sopenharmony_ci		return -EOPNOTSUPP;
58962306a36Sopenharmony_ci	if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
59062306a36Sopenharmony_ci		return -EINVAL;
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci	ret = ethnl_parse_header_dev_get(&req_info, info->attrs[ops->hdr_attr],
59362306a36Sopenharmony_ci					 genl_info_net(info), info->extack,
59462306a36Sopenharmony_ci					 true);
59562306a36Sopenharmony_ci	if (ret < 0)
59662306a36Sopenharmony_ci		return ret;
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	if (ops->set_validate) {
59962306a36Sopenharmony_ci		ret = ops->set_validate(&req_info, info);
60062306a36Sopenharmony_ci		/* 0 means nothing to do */
60162306a36Sopenharmony_ci		if (ret <= 0)
60262306a36Sopenharmony_ci			goto out_dev;
60362306a36Sopenharmony_ci	}
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	rtnl_lock();
60662306a36Sopenharmony_ci	ret = ethnl_ops_begin(req_info.dev);
60762306a36Sopenharmony_ci	if (ret < 0)
60862306a36Sopenharmony_ci		goto out_rtnl;
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	ret = ops->set(&req_info, info);
61162306a36Sopenharmony_ci	if (ret <= 0)
61262306a36Sopenharmony_ci		goto out_ops;
61362306a36Sopenharmony_ci	ethtool_notify(req_info.dev, ops->set_ntf_cmd, NULL);
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci	ret = 0;
61662306a36Sopenharmony_ciout_ops:
61762306a36Sopenharmony_ci	ethnl_ops_complete(req_info.dev);
61862306a36Sopenharmony_ciout_rtnl:
61962306a36Sopenharmony_ci	rtnl_unlock();
62062306a36Sopenharmony_ciout_dev:
62162306a36Sopenharmony_ci	ethnl_parse_header_dev_put(&req_info);
62262306a36Sopenharmony_ci	return ret;
62362306a36Sopenharmony_ci}
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_cistatic const struct ethnl_request_ops *
62662306a36Sopenharmony_ciethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
62762306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKINFO_NTF]	= &ethnl_linkinfo_request_ops,
62862306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKMODES_NTF]	= &ethnl_linkmodes_request_ops,
62962306a36Sopenharmony_ci	[ETHTOOL_MSG_DEBUG_NTF]		= &ethnl_debug_request_ops,
63062306a36Sopenharmony_ci	[ETHTOOL_MSG_WOL_NTF]		= &ethnl_wol_request_ops,
63162306a36Sopenharmony_ci	[ETHTOOL_MSG_FEATURES_NTF]	= &ethnl_features_request_ops,
63262306a36Sopenharmony_ci	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= &ethnl_privflags_request_ops,
63362306a36Sopenharmony_ci	[ETHTOOL_MSG_RINGS_NTF]		= &ethnl_rings_request_ops,
63462306a36Sopenharmony_ci	[ETHTOOL_MSG_CHANNELS_NTF]	= &ethnl_channels_request_ops,
63562306a36Sopenharmony_ci	[ETHTOOL_MSG_COALESCE_NTF]	= &ethnl_coalesce_request_ops,
63662306a36Sopenharmony_ci	[ETHTOOL_MSG_PAUSE_NTF]		= &ethnl_pause_request_ops,
63762306a36Sopenharmony_ci	[ETHTOOL_MSG_EEE_NTF]		= &ethnl_eee_request_ops,
63862306a36Sopenharmony_ci	[ETHTOOL_MSG_FEC_NTF]		= &ethnl_fec_request_ops,
63962306a36Sopenharmony_ci	[ETHTOOL_MSG_MODULE_NTF]	= &ethnl_module_request_ops,
64062306a36Sopenharmony_ci	[ETHTOOL_MSG_PLCA_NTF]		= &ethnl_plca_cfg_request_ops,
64162306a36Sopenharmony_ci	[ETHTOOL_MSG_MM_NTF]		= &ethnl_mm_request_ops,
64262306a36Sopenharmony_ci};
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci/* default notification handler */
64562306a36Sopenharmony_cistatic void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
64662306a36Sopenharmony_ci				 const void *data)
64762306a36Sopenharmony_ci{
64862306a36Sopenharmony_ci	struct ethnl_reply_data *reply_data;
64962306a36Sopenharmony_ci	const struct ethnl_request_ops *ops;
65062306a36Sopenharmony_ci	struct ethnl_req_info *req_info;
65162306a36Sopenharmony_ci	struct genl_info info;
65262306a36Sopenharmony_ci	struct sk_buff *skb;
65362306a36Sopenharmony_ci	void *reply_payload;
65462306a36Sopenharmony_ci	int reply_len;
65562306a36Sopenharmony_ci	int ret;
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	genl_info_init_ntf(&info, &ethtool_genl_family, cmd);
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ci	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
66062306a36Sopenharmony_ci		      !ethnl_default_notify_ops[cmd],
66162306a36Sopenharmony_ci		      "unexpected notification type %u\n", cmd))
66262306a36Sopenharmony_ci		return;
66362306a36Sopenharmony_ci	ops = ethnl_default_notify_ops[cmd];
66462306a36Sopenharmony_ci	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
66562306a36Sopenharmony_ci	if (!req_info)
66662306a36Sopenharmony_ci		return;
66762306a36Sopenharmony_ci	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
66862306a36Sopenharmony_ci	if (!reply_data) {
66962306a36Sopenharmony_ci		kfree(req_info);
67062306a36Sopenharmony_ci		return;
67162306a36Sopenharmony_ci	}
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	req_info->dev = dev;
67462306a36Sopenharmony_ci	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	ethnl_init_reply_data(reply_data, ops, dev);
67762306a36Sopenharmony_ci	ret = ops->prepare_data(req_info, reply_data, &info);
67862306a36Sopenharmony_ci	if (ret < 0)
67962306a36Sopenharmony_ci		goto err_cleanup;
68062306a36Sopenharmony_ci	ret = ops->reply_size(req_info, reply_data);
68162306a36Sopenharmony_ci	if (ret < 0)
68262306a36Sopenharmony_ci		goto err_cleanup;
68362306a36Sopenharmony_ci	reply_len = ret + ethnl_reply_header_size();
68462306a36Sopenharmony_ci	skb = genlmsg_new(reply_len, GFP_KERNEL);
68562306a36Sopenharmony_ci	if (!skb)
68662306a36Sopenharmony_ci		goto err_cleanup;
68762306a36Sopenharmony_ci	reply_payload = ethnl_bcastmsg_put(skb, cmd);
68862306a36Sopenharmony_ci	if (!reply_payload)
68962306a36Sopenharmony_ci		goto err_skb;
69062306a36Sopenharmony_ci	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
69162306a36Sopenharmony_ci	if (ret < 0)
69262306a36Sopenharmony_ci		goto err_msg;
69362306a36Sopenharmony_ci	ret = ops->fill_reply(skb, req_info, reply_data);
69462306a36Sopenharmony_ci	if (ret < 0)
69562306a36Sopenharmony_ci		goto err_msg;
69662306a36Sopenharmony_ci	if (ops->cleanup_data)
69762306a36Sopenharmony_ci		ops->cleanup_data(reply_data);
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	genlmsg_end(skb, reply_payload);
70062306a36Sopenharmony_ci	kfree(reply_data);
70162306a36Sopenharmony_ci	kfree(req_info);
70262306a36Sopenharmony_ci	ethnl_multicast(skb, dev);
70362306a36Sopenharmony_ci	return;
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_cierr_msg:
70662306a36Sopenharmony_ci	WARN_ONCE(ret == -EMSGSIZE,
70762306a36Sopenharmony_ci		  "calculated message payload length (%d) not sufficient\n",
70862306a36Sopenharmony_ci		  reply_len);
70962306a36Sopenharmony_cierr_skb:
71062306a36Sopenharmony_ci	nlmsg_free(skb);
71162306a36Sopenharmony_cierr_cleanup:
71262306a36Sopenharmony_ci	if (ops->cleanup_data)
71362306a36Sopenharmony_ci		ops->cleanup_data(reply_data);
71462306a36Sopenharmony_ci	kfree(reply_data);
71562306a36Sopenharmony_ci	kfree(req_info);
71662306a36Sopenharmony_ci	return;
71762306a36Sopenharmony_ci}
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci/* notifications */
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_citypedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
72262306a36Sopenharmony_ci				       const void *data);
72362306a36Sopenharmony_ci
72462306a36Sopenharmony_cistatic const ethnl_notify_handler_t ethnl_notify_handlers[] = {
72562306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKINFO_NTF]	= ethnl_default_notify,
72662306a36Sopenharmony_ci	[ETHTOOL_MSG_LINKMODES_NTF]	= ethnl_default_notify,
72762306a36Sopenharmony_ci	[ETHTOOL_MSG_DEBUG_NTF]		= ethnl_default_notify,
72862306a36Sopenharmony_ci	[ETHTOOL_MSG_WOL_NTF]		= ethnl_default_notify,
72962306a36Sopenharmony_ci	[ETHTOOL_MSG_FEATURES_NTF]	= ethnl_default_notify,
73062306a36Sopenharmony_ci	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= ethnl_default_notify,
73162306a36Sopenharmony_ci	[ETHTOOL_MSG_RINGS_NTF]		= ethnl_default_notify,
73262306a36Sopenharmony_ci	[ETHTOOL_MSG_CHANNELS_NTF]	= ethnl_default_notify,
73362306a36Sopenharmony_ci	[ETHTOOL_MSG_COALESCE_NTF]	= ethnl_default_notify,
73462306a36Sopenharmony_ci	[ETHTOOL_MSG_PAUSE_NTF]		= ethnl_default_notify,
73562306a36Sopenharmony_ci	[ETHTOOL_MSG_EEE_NTF]		= ethnl_default_notify,
73662306a36Sopenharmony_ci	[ETHTOOL_MSG_FEC_NTF]		= ethnl_default_notify,
73762306a36Sopenharmony_ci	[ETHTOOL_MSG_MODULE_NTF]	= ethnl_default_notify,
73862306a36Sopenharmony_ci	[ETHTOOL_MSG_PLCA_NTF]		= ethnl_default_notify,
73962306a36Sopenharmony_ci	[ETHTOOL_MSG_MM_NTF]		= ethnl_default_notify,
74062306a36Sopenharmony_ci};
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_civoid ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
74362306a36Sopenharmony_ci{
74462306a36Sopenharmony_ci	if (unlikely(!ethnl_ok))
74562306a36Sopenharmony_ci		return;
74662306a36Sopenharmony_ci	ASSERT_RTNL();
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
74962306a36Sopenharmony_ci		   ethnl_notify_handlers[cmd]))
75062306a36Sopenharmony_ci		ethnl_notify_handlers[cmd](dev, cmd, data);
75162306a36Sopenharmony_ci	else
75262306a36Sopenharmony_ci		WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
75362306a36Sopenharmony_ci			  cmd, netdev_name(dev));
75462306a36Sopenharmony_ci}
75562306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_notify);
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_cistatic void ethnl_notify_features(struct netdev_notifier_info *info)
75862306a36Sopenharmony_ci{
75962306a36Sopenharmony_ci	struct net_device *dev = netdev_notifier_info_to_dev(info);
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
76262306a36Sopenharmony_ci}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_cistatic int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
76562306a36Sopenharmony_ci			      void *ptr)
76662306a36Sopenharmony_ci{
76762306a36Sopenharmony_ci	switch (event) {
76862306a36Sopenharmony_ci	case NETDEV_FEAT_CHANGE:
76962306a36Sopenharmony_ci		ethnl_notify_features(ptr);
77062306a36Sopenharmony_ci		break;
77162306a36Sopenharmony_ci	}
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	return NOTIFY_DONE;
77462306a36Sopenharmony_ci}
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_cistatic struct notifier_block ethnl_netdev_notifier = {
77762306a36Sopenharmony_ci	.notifier_call = ethnl_netdev_event,
77862306a36Sopenharmony_ci};
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci/* genetlink setup */
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_cistatic const struct genl_ops ethtool_genl_ops[] = {
78362306a36Sopenharmony_ci	{
78462306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_STRSET_GET,
78562306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
78662306a36Sopenharmony_ci		.start	= ethnl_default_start,
78762306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
78862306a36Sopenharmony_ci		.done	= ethnl_default_done,
78962306a36Sopenharmony_ci		.policy = ethnl_strset_get_policy,
79062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
79162306a36Sopenharmony_ci	},
79262306a36Sopenharmony_ci	{
79362306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_LINKINFO_GET,
79462306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
79562306a36Sopenharmony_ci		.start	= ethnl_default_start,
79662306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
79762306a36Sopenharmony_ci		.done	= ethnl_default_done,
79862306a36Sopenharmony_ci		.policy = ethnl_linkinfo_get_policy,
79962306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
80062306a36Sopenharmony_ci	},
80162306a36Sopenharmony_ci	{
80262306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_LINKINFO_SET,
80362306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
80462306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
80562306a36Sopenharmony_ci		.policy = ethnl_linkinfo_set_policy,
80662306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
80762306a36Sopenharmony_ci	},
80862306a36Sopenharmony_ci	{
80962306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_LINKMODES_GET,
81062306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
81162306a36Sopenharmony_ci		.start	= ethnl_default_start,
81262306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
81362306a36Sopenharmony_ci		.done	= ethnl_default_done,
81462306a36Sopenharmony_ci		.policy = ethnl_linkmodes_get_policy,
81562306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
81662306a36Sopenharmony_ci	},
81762306a36Sopenharmony_ci	{
81862306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_LINKMODES_SET,
81962306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
82062306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
82162306a36Sopenharmony_ci		.policy = ethnl_linkmodes_set_policy,
82262306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
82362306a36Sopenharmony_ci	},
82462306a36Sopenharmony_ci	{
82562306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_LINKSTATE_GET,
82662306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
82762306a36Sopenharmony_ci		.start	= ethnl_default_start,
82862306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
82962306a36Sopenharmony_ci		.done	= ethnl_default_done,
83062306a36Sopenharmony_ci		.policy = ethnl_linkstate_get_policy,
83162306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
83262306a36Sopenharmony_ci	},
83362306a36Sopenharmony_ci	{
83462306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_DEBUG_GET,
83562306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
83662306a36Sopenharmony_ci		.start	= ethnl_default_start,
83762306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
83862306a36Sopenharmony_ci		.done	= ethnl_default_done,
83962306a36Sopenharmony_ci		.policy = ethnl_debug_get_policy,
84062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
84162306a36Sopenharmony_ci	},
84262306a36Sopenharmony_ci	{
84362306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_DEBUG_SET,
84462306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
84562306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
84662306a36Sopenharmony_ci		.policy = ethnl_debug_set_policy,
84762306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
84862306a36Sopenharmony_ci	},
84962306a36Sopenharmony_ci	{
85062306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_WOL_GET,
85162306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
85262306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
85362306a36Sopenharmony_ci		.start	= ethnl_default_start,
85462306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
85562306a36Sopenharmony_ci		.done	= ethnl_default_done,
85662306a36Sopenharmony_ci		.policy = ethnl_wol_get_policy,
85762306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
85862306a36Sopenharmony_ci	},
85962306a36Sopenharmony_ci	{
86062306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_WOL_SET,
86162306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
86262306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
86362306a36Sopenharmony_ci		.policy = ethnl_wol_set_policy,
86462306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
86562306a36Sopenharmony_ci	},
86662306a36Sopenharmony_ci	{
86762306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_FEATURES_GET,
86862306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
86962306a36Sopenharmony_ci		.start	= ethnl_default_start,
87062306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
87162306a36Sopenharmony_ci		.done	= ethnl_default_done,
87262306a36Sopenharmony_ci		.policy = ethnl_features_get_policy,
87362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
87462306a36Sopenharmony_ci	},
87562306a36Sopenharmony_ci	{
87662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_FEATURES_SET,
87762306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
87862306a36Sopenharmony_ci		.doit	= ethnl_set_features,
87962306a36Sopenharmony_ci		.policy = ethnl_features_set_policy,
88062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
88162306a36Sopenharmony_ci	},
88262306a36Sopenharmony_ci	{
88362306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PRIVFLAGS_GET,
88462306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
88562306a36Sopenharmony_ci		.start	= ethnl_default_start,
88662306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
88762306a36Sopenharmony_ci		.done	= ethnl_default_done,
88862306a36Sopenharmony_ci		.policy = ethnl_privflags_get_policy,
88962306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
89062306a36Sopenharmony_ci	},
89162306a36Sopenharmony_ci	{
89262306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PRIVFLAGS_SET,
89362306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
89462306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
89562306a36Sopenharmony_ci		.policy = ethnl_privflags_set_policy,
89662306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
89762306a36Sopenharmony_ci	},
89862306a36Sopenharmony_ci	{
89962306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_RINGS_GET,
90062306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
90162306a36Sopenharmony_ci		.start	= ethnl_default_start,
90262306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
90362306a36Sopenharmony_ci		.done	= ethnl_default_done,
90462306a36Sopenharmony_ci		.policy = ethnl_rings_get_policy,
90562306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
90662306a36Sopenharmony_ci	},
90762306a36Sopenharmony_ci	{
90862306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_RINGS_SET,
90962306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
91062306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
91162306a36Sopenharmony_ci		.policy = ethnl_rings_set_policy,
91262306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
91362306a36Sopenharmony_ci	},
91462306a36Sopenharmony_ci	{
91562306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_CHANNELS_GET,
91662306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
91762306a36Sopenharmony_ci		.start	= ethnl_default_start,
91862306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
91962306a36Sopenharmony_ci		.done	= ethnl_default_done,
92062306a36Sopenharmony_ci		.policy = ethnl_channels_get_policy,
92162306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
92262306a36Sopenharmony_ci	},
92362306a36Sopenharmony_ci	{
92462306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_CHANNELS_SET,
92562306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
92662306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
92762306a36Sopenharmony_ci		.policy = ethnl_channels_set_policy,
92862306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
92962306a36Sopenharmony_ci	},
93062306a36Sopenharmony_ci	{
93162306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_COALESCE_GET,
93262306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
93362306a36Sopenharmony_ci		.start	= ethnl_default_start,
93462306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
93562306a36Sopenharmony_ci		.done	= ethnl_default_done,
93662306a36Sopenharmony_ci		.policy = ethnl_coalesce_get_policy,
93762306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
93862306a36Sopenharmony_ci	},
93962306a36Sopenharmony_ci	{
94062306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_COALESCE_SET,
94162306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
94262306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
94362306a36Sopenharmony_ci		.policy = ethnl_coalesce_set_policy,
94462306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
94562306a36Sopenharmony_ci	},
94662306a36Sopenharmony_ci	{
94762306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PAUSE_GET,
94862306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
94962306a36Sopenharmony_ci		.start	= ethnl_default_start,
95062306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
95162306a36Sopenharmony_ci		.done	= ethnl_default_done,
95262306a36Sopenharmony_ci		.policy = ethnl_pause_get_policy,
95362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
95462306a36Sopenharmony_ci	},
95562306a36Sopenharmony_ci	{
95662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PAUSE_SET,
95762306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
95862306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
95962306a36Sopenharmony_ci		.policy = ethnl_pause_set_policy,
96062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
96162306a36Sopenharmony_ci	},
96262306a36Sopenharmony_ci	{
96362306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_EEE_GET,
96462306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
96562306a36Sopenharmony_ci		.start	= ethnl_default_start,
96662306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
96762306a36Sopenharmony_ci		.done	= ethnl_default_done,
96862306a36Sopenharmony_ci		.policy = ethnl_eee_get_policy,
96962306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
97062306a36Sopenharmony_ci	},
97162306a36Sopenharmony_ci	{
97262306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_EEE_SET,
97362306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
97462306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
97562306a36Sopenharmony_ci		.policy = ethnl_eee_set_policy,
97662306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
97762306a36Sopenharmony_ci	},
97862306a36Sopenharmony_ci	{
97962306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_TSINFO_GET,
98062306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
98162306a36Sopenharmony_ci		.start	= ethnl_default_start,
98262306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
98362306a36Sopenharmony_ci		.done	= ethnl_default_done,
98462306a36Sopenharmony_ci		.policy = ethnl_tsinfo_get_policy,
98562306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
98662306a36Sopenharmony_ci	},
98762306a36Sopenharmony_ci	{
98862306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_CABLE_TEST_ACT,
98962306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
99062306a36Sopenharmony_ci		.doit	= ethnl_act_cable_test,
99162306a36Sopenharmony_ci		.policy = ethnl_cable_test_act_policy,
99262306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
99362306a36Sopenharmony_ci	},
99462306a36Sopenharmony_ci	{
99562306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
99662306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
99762306a36Sopenharmony_ci		.doit	= ethnl_act_cable_test_tdr,
99862306a36Sopenharmony_ci		.policy = ethnl_cable_test_tdr_act_policy,
99962306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
100062306a36Sopenharmony_ci	},
100162306a36Sopenharmony_ci	{
100262306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_TUNNEL_INFO_GET,
100362306a36Sopenharmony_ci		.doit	= ethnl_tunnel_info_doit,
100462306a36Sopenharmony_ci		.start	= ethnl_tunnel_info_start,
100562306a36Sopenharmony_ci		.dumpit	= ethnl_tunnel_info_dumpit,
100662306a36Sopenharmony_ci		.policy = ethnl_tunnel_info_get_policy,
100762306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
100862306a36Sopenharmony_ci	},
100962306a36Sopenharmony_ci	{
101062306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_FEC_GET,
101162306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
101262306a36Sopenharmony_ci		.start	= ethnl_default_start,
101362306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
101462306a36Sopenharmony_ci		.done	= ethnl_default_done,
101562306a36Sopenharmony_ci		.policy = ethnl_fec_get_policy,
101662306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
101762306a36Sopenharmony_ci	},
101862306a36Sopenharmony_ci	{
101962306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_FEC_SET,
102062306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
102162306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
102262306a36Sopenharmony_ci		.policy = ethnl_fec_set_policy,
102362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
102462306a36Sopenharmony_ci	},
102562306a36Sopenharmony_ci	{
102662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_MODULE_EEPROM_GET,
102762306a36Sopenharmony_ci		.flags  = GENL_UNS_ADMIN_PERM,
102862306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
102962306a36Sopenharmony_ci		.start	= ethnl_default_start,
103062306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
103162306a36Sopenharmony_ci		.done	= ethnl_default_done,
103262306a36Sopenharmony_ci		.policy = ethnl_module_eeprom_get_policy,
103362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
103462306a36Sopenharmony_ci	},
103562306a36Sopenharmony_ci	{
103662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_STATS_GET,
103762306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
103862306a36Sopenharmony_ci		.start	= ethnl_default_start,
103962306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
104062306a36Sopenharmony_ci		.done	= ethnl_default_done,
104162306a36Sopenharmony_ci		.policy = ethnl_stats_get_policy,
104262306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
104362306a36Sopenharmony_ci	},
104462306a36Sopenharmony_ci	{
104562306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PHC_VCLOCKS_GET,
104662306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
104762306a36Sopenharmony_ci		.start	= ethnl_default_start,
104862306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
104962306a36Sopenharmony_ci		.done	= ethnl_default_done,
105062306a36Sopenharmony_ci		.policy = ethnl_phc_vclocks_get_policy,
105162306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
105262306a36Sopenharmony_ci	},
105362306a36Sopenharmony_ci	{
105462306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_MODULE_GET,
105562306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
105662306a36Sopenharmony_ci		.start	= ethnl_default_start,
105762306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
105862306a36Sopenharmony_ci		.done	= ethnl_default_done,
105962306a36Sopenharmony_ci		.policy = ethnl_module_get_policy,
106062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1,
106162306a36Sopenharmony_ci	},
106262306a36Sopenharmony_ci	{
106362306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_MODULE_SET,
106462306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
106562306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
106662306a36Sopenharmony_ci		.policy = ethnl_module_set_policy,
106762306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1,
106862306a36Sopenharmony_ci	},
106962306a36Sopenharmony_ci	{
107062306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PSE_GET,
107162306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
107262306a36Sopenharmony_ci		.start	= ethnl_default_start,
107362306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
107462306a36Sopenharmony_ci		.done	= ethnl_default_done,
107562306a36Sopenharmony_ci		.policy = ethnl_pse_get_policy,
107662306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
107762306a36Sopenharmony_ci	},
107862306a36Sopenharmony_ci	{
107962306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PSE_SET,
108062306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
108162306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
108262306a36Sopenharmony_ci		.policy = ethnl_pse_set_policy,
108362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1,
108462306a36Sopenharmony_ci	},
108562306a36Sopenharmony_ci	{
108662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_RSS_GET,
108762306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
108862306a36Sopenharmony_ci		.policy = ethnl_rss_get_policy,
108962306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1,
109062306a36Sopenharmony_ci	},
109162306a36Sopenharmony_ci	{
109262306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PLCA_GET_CFG,
109362306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
109462306a36Sopenharmony_ci		.start	= ethnl_default_start,
109562306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
109662306a36Sopenharmony_ci		.done	= ethnl_default_done,
109762306a36Sopenharmony_ci		.policy = ethnl_plca_get_cfg_policy,
109862306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
109962306a36Sopenharmony_ci	},
110062306a36Sopenharmony_ci	{
110162306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PLCA_SET_CFG,
110262306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
110362306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
110462306a36Sopenharmony_ci		.policy = ethnl_plca_set_cfg_policy,
110562306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1,
110662306a36Sopenharmony_ci	},
110762306a36Sopenharmony_ci	{
110862306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_PLCA_GET_STATUS,
110962306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
111062306a36Sopenharmony_ci		.start	= ethnl_default_start,
111162306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
111262306a36Sopenharmony_ci		.done	= ethnl_default_done,
111362306a36Sopenharmony_ci		.policy = ethnl_plca_get_status_policy,
111462306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
111562306a36Sopenharmony_ci	},
111662306a36Sopenharmony_ci	{
111762306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_MM_GET,
111862306a36Sopenharmony_ci		.doit	= ethnl_default_doit,
111962306a36Sopenharmony_ci		.start	= ethnl_default_start,
112062306a36Sopenharmony_ci		.dumpit	= ethnl_default_dumpit,
112162306a36Sopenharmony_ci		.done	= ethnl_default_done,
112262306a36Sopenharmony_ci		.policy = ethnl_mm_get_policy,
112362306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1,
112462306a36Sopenharmony_ci	},
112562306a36Sopenharmony_ci	{
112662306a36Sopenharmony_ci		.cmd	= ETHTOOL_MSG_MM_SET,
112762306a36Sopenharmony_ci		.flags	= GENL_UNS_ADMIN_PERM,
112862306a36Sopenharmony_ci		.doit	= ethnl_default_set_doit,
112962306a36Sopenharmony_ci		.policy = ethnl_mm_set_policy,
113062306a36Sopenharmony_ci		.maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1,
113162306a36Sopenharmony_ci	},
113262306a36Sopenharmony_ci};
113362306a36Sopenharmony_ci
113462306a36Sopenharmony_cistatic const struct genl_multicast_group ethtool_nl_mcgrps[] = {
113562306a36Sopenharmony_ci	[ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
113662306a36Sopenharmony_ci};
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_cistatic struct genl_family ethtool_genl_family __ro_after_init = {
113962306a36Sopenharmony_ci	.name		= ETHTOOL_GENL_NAME,
114062306a36Sopenharmony_ci	.version	= ETHTOOL_GENL_VERSION,
114162306a36Sopenharmony_ci	.netnsok	= true,
114262306a36Sopenharmony_ci	.parallel_ops	= true,
114362306a36Sopenharmony_ci	.ops		= ethtool_genl_ops,
114462306a36Sopenharmony_ci	.n_ops		= ARRAY_SIZE(ethtool_genl_ops),
114562306a36Sopenharmony_ci	.resv_start_op	= ETHTOOL_MSG_MODULE_GET + 1,
114662306a36Sopenharmony_ci	.mcgrps		= ethtool_nl_mcgrps,
114762306a36Sopenharmony_ci	.n_mcgrps	= ARRAY_SIZE(ethtool_nl_mcgrps),
114862306a36Sopenharmony_ci};
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci/* module setup */
115162306a36Sopenharmony_ci
115262306a36Sopenharmony_cistatic int __init ethnl_init(void)
115362306a36Sopenharmony_ci{
115462306a36Sopenharmony_ci	int ret;
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci	ret = genl_register_family(&ethtool_genl_family);
115762306a36Sopenharmony_ci	if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
115862306a36Sopenharmony_ci		return ret;
115962306a36Sopenharmony_ci	ethnl_ok = true;
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci	ret = register_netdevice_notifier(&ethnl_netdev_notifier);
116262306a36Sopenharmony_ci	WARN(ret < 0, "ethtool: net device notifier registration failed");
116362306a36Sopenharmony_ci	return ret;
116462306a36Sopenharmony_ci}
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_cisubsys_initcall(ethnl_init);
1167