blob: 73e0f5b626bfba4d5b575470a7e69a0039882ce8 [file] [log] [blame]
Michal Kubecek2b4a8992019-12-27 15:55:18 +01001// SPDX-License-Identifier: GPL-2.0-only
2
Michal Kubecek041b1c52019-12-27 15:55:23 +01003#include <net/sock.h>
Michal Kubecek2b4a8992019-12-27 15:55:18 +01004#include <linux/ethtool_netlink.h>
5#include "netlink.h"
6
Michal Kubecek041b1c52019-12-27 15:55:23 +01007static struct genl_family ethtool_genl_family;
8
Michal Kubecek6b08d6c2019-12-27 15:55:33 +01009static bool ethnl_ok __read_mostly;
Michal Kubecek5cf2a542019-12-27 15:55:58 +010010static u32 ethnl_bcast_seq;
Michal Kubecek6b08d6c2019-12-27 15:55:33 +010011
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070012#define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS | \
13 ETHTOOL_FLAG_OMIT_REPLY)
14#define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS)
15
Jakub Kicinski329d9c32020-10-05 15:07:36 -070016const struct nla_policy ethnl_header_policy[] = {
Michal Kubecek041b1c52019-12-27 15:55:23 +010017 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
18 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
19 .len = ALTIFNAMSIZ - 1 },
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070020 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
21 ETHTOOL_FLAGS_BASIC),
22};
23
24const struct nla_policy ethnl_header_policy_stats[] = {
25 [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
26 [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
27 .len = ALTIFNAMSIZ - 1 },
28 [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
29 ETHTOOL_FLAGS_STATS),
Michal Kubecek041b1c52019-12-27 15:55:23 +010030};
31
32/**
Michal Kubecek98130542020-03-12 21:07:38 +010033 * ethnl_parse_header_dev_get() - parse request header
Michal Kubecek041b1c52019-12-27 15:55:23 +010034 * @req_info: structure to put results into
35 * @header: nest attribute with request header
36 * @net: request netns
37 * @extack: netlink extack for error reporting
38 * @require_dev: fail if no device identified in header
39 *
40 * Parse request header in nested attribute @nest and puts results into
41 * the structure pointed to by @req_info. Extack from @info is used for error
42 * reporting. If req_info->dev is not null on return, reference to it has
43 * been taken. If error is returned, *req_info is null initialized and no
44 * reference is held.
45 *
46 * Return: 0 on success or negative error code
47 */
Michal Kubecek98130542020-03-12 21:07:38 +010048int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
49 const struct nlattr *header, struct net *net,
50 struct netlink_ext_ack *extack, bool require_dev)
Michal Kubecek041b1c52019-12-27 15:55:23 +010051{
Jakub Kicinskiff419af2020-10-05 15:07:35 -070052 struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy)];
Michal Kubecek041b1c52019-12-27 15:55:23 +010053 const struct nlattr *devname_attr;
54 struct net_device *dev = NULL;
Michal Kubecek2363d732020-03-15 18:17:53 +010055 u32 flags = 0;
Michal Kubecek041b1c52019-12-27 15:55:23 +010056 int ret;
57
58 if (!header) {
59 NL_SET_ERR_MSG(extack, "request header missing");
60 return -EINVAL;
61 }
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070062 /* No validation here, command policy should have a nested policy set
63 * for the header, therefore validation should have already been done.
64 */
Jakub Kicinskiff419af2020-10-05 15:07:35 -070065 ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy) - 1, header,
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070066 NULL, extack);
Michal Kubecek041b1c52019-12-27 15:55:23 +010067 if (ret < 0)
68 return ret;
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070069 if (tb[ETHTOOL_A_HEADER_FLAGS])
Michal Kubecek2363d732020-03-15 18:17:53 +010070 flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
Michal Kubecek041b1c52019-12-27 15:55:23 +010071
Michal Kubecek2363d732020-03-15 18:17:53 +010072 devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
Michal Kubecek041b1c52019-12-27 15:55:23 +010073 if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
74 u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
75
76 dev = dev_get_by_index(net, ifindex);
77 if (!dev) {
78 NL_SET_ERR_MSG_ATTR(extack,
79 tb[ETHTOOL_A_HEADER_DEV_INDEX],
80 "no device matches ifindex");
81 return -ENODEV;
82 }
83 /* if both ifindex and ifname are passed, they must match */
84 if (devname_attr &&
85 strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
86 dev_put(dev);
87 NL_SET_ERR_MSG_ATTR(extack, header,
88 "ifindex and name do not match");
89 return -ENODEV;
90 }
91 } else if (devname_attr) {
92 dev = dev_get_by_name(net, nla_data(devname_attr));
93 if (!dev) {
94 NL_SET_ERR_MSG_ATTR(extack, devname_attr,
95 "no device matches name");
96 return -ENODEV;
97 }
98 } else if (require_dev) {
99 NL_SET_ERR_MSG_ATTR(extack, header,
100 "neither ifindex nor name specified");
101 return -EINVAL;
102 }
103
104 if (dev && !netif_device_present(dev)) {
105 dev_put(dev);
106 NL_SET_ERR_MSG(extack, "device not present");
107 return -ENODEV;
108 }
109
110 req_info->dev = dev;
Michal Kubecek2363d732020-03-15 18:17:53 +0100111 req_info->flags = flags;
Michal Kubecek041b1c52019-12-27 15:55:23 +0100112 return 0;
113}
114
115/**
116 * ethnl_fill_reply_header() - Put common header into a reply message
117 * @skb: skb with the message
118 * @dev: network device to describe in header
119 * @attrtype: attribute type to use for the nest
120 *
121 * Create a nested attribute with attributes describing given network device.
122 *
123 * Return: 0 on success, error value (-EMSGSIZE only) on error
124 */
125int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
126 u16 attrtype)
127{
128 struct nlattr *nest;
129
130 if (!dev)
131 return 0;
132 nest = nla_nest_start(skb, attrtype);
133 if (!nest)
134 return -EMSGSIZE;
135
136 if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
137 nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
138 goto nla_put_failure;
139 /* If more attributes are put into reply header, ethnl_header_size()
140 * must be updated to account for them.
141 */
142
143 nla_nest_end(skb, nest);
144 return 0;
145
146nla_put_failure:
147 nla_nest_cancel(skb, nest);
148 return -EMSGSIZE;
149}
150
151/**
152 * ethnl_reply_init() - Create skb for a reply and fill device identification
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100153 * @payload: payload length (without netlink and genetlink header)
154 * @dev: device the reply is about (may be null)
155 * @cmd: ETHTOOL_MSG_* message type for reply
156 * @hdr_attrtype: attribute type for common header
157 * @info: genetlink info of the received packet we respond to
158 * @ehdrp: place to store payload pointer returned by genlmsg_new()
Michal Kubecek041b1c52019-12-27 15:55:23 +0100159 *
160 * Return: pointer to allocated skb on success, NULL on error
161 */
162struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
163 u16 hdr_attrtype, struct genl_info *info,
164 void **ehdrp)
165{
166 struct sk_buff *skb;
167
168 skb = genlmsg_new(payload, GFP_KERNEL);
169 if (!skb)
170 goto err;
171 *ehdrp = genlmsg_put_reply(skb, info, &ethtool_genl_family, 0, cmd);
172 if (!*ehdrp)
173 goto err_free;
174
175 if (dev) {
176 int ret;
177
178 ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
179 if (ret < 0)
180 goto err_free;
181 }
182 return skb;
183
184err_free:
185 nlmsg_free(skb);
186err:
187 if (info)
188 GENL_SET_ERR_MSG(info, "failed to setup reply message");
189 return NULL;
190}
191
Jakub Kicinskic7d759e2020-07-09 17:42:47 -0700192void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd)
193{
194 return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
195 &ethtool_genl_family, 0, cmd);
196}
197
Andrew Lunn0df960f2020-05-10 21:12:35 +0200198void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100199{
200 return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
201 cmd);
202}
203
Andrew Lunn0df960f2020-05-10 21:12:35 +0200204int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100205{
206 return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
207 0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
208}
209
Michal Kubecek728480f2019-12-27 15:55:38 +0100210/* GET request helpers */
211
212/**
213 * struct ethnl_dump_ctx - context structure for generic dumpit() callback
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100214 * @ops: request ops of currently processed message type
215 * @req_info: parsed request header of processed request
216 * @reply_data: data needed to compose the reply
217 * @pos_hash: saved iteration position - hashbucket
218 * @pos_idx: saved iteration position - index
Michal Kubecek728480f2019-12-27 15:55:38 +0100219 *
220 * These parameters are kept in struct netlink_callback as context preserved
221 * between iterations. They are initialized by ethnl_default_start() and used
222 * in ethnl_default_dumpit() and ethnl_default_done().
223 */
224struct ethnl_dump_ctx {
225 const struct ethnl_request_ops *ops;
226 struct ethnl_req_info *req_info;
227 struct ethnl_reply_data *reply_data;
228 int pos_hash;
229 int pos_idx;
230};
231
232static const struct ethnl_request_ops *
233ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
Michal Kubecek71921692019-12-27 15:55:43 +0100234 [ETHTOOL_MSG_STRSET_GET] = &ethnl_strset_request_ops,
Michal Kubecek459e0b82019-12-27 15:55:48 +0100235 [ETHTOOL_MSG_LINKINFO_GET] = &ethnl_linkinfo_request_ops,
Michal Kubecekf625aa92019-12-27 15:56:08 +0100236 [ETHTOOL_MSG_LINKMODES_GET] = &ethnl_linkmodes_request_ops,
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100237 [ETHTOOL_MSG_LINKSTATE_GET] = &ethnl_linkstate_request_ops,
Michal Kubecek6a94b8c2020-01-26 23:11:04 +0100238 [ETHTOOL_MSG_DEBUG_GET] = &ethnl_debug_request_ops,
Michal Kubecek51ea22b2020-01-26 23:11:13 +0100239 [ETHTOOL_MSG_WOL_GET] = &ethnl_wol_request_ops,
Michal Kubecek05243992020-03-12 21:07:48 +0100240 [ETHTOOL_MSG_FEATURES_GET] = &ethnl_features_request_ops,
Michal Kubeceke16c3382020-03-12 21:08:08 +0100241 [ETHTOOL_MSG_PRIVFLAGS_GET] = &ethnl_privflags_request_ops,
Michal Kubeceke4a17172020-03-12 21:08:23 +0100242 [ETHTOOL_MSG_RINGS_GET] = &ethnl_rings_request_ops,
Michal Kubecek0c849792020-03-12 21:08:38 +0100243 [ETHTOOL_MSG_CHANNELS_GET] = &ethnl_channels_request_ops,
Michal Kubecek21727542020-03-28 00:01:08 +0100244 [ETHTOOL_MSG_COALESCE_GET] = &ethnl_coalesce_request_ops,
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100245 [ETHTOOL_MSG_PAUSE_GET] = &ethnl_pause_request_ops,
Michal Kubecekb7eeefe2020-03-28 00:01:38 +0100246 [ETHTOOL_MSG_EEE_GET] = &ethnl_eee_request_ops,
Jakub Kicinski1e5d1f62021-03-29 20:59:52 -0700247 [ETHTOOL_MSG_FEC_GET] = &ethnl_fec_request_ops,
Michal Kubecek5b071c52020-03-28 00:01:58 +0100248 [ETHTOOL_MSG_TSINFO_GET] = &ethnl_tsinfo_request_ops,
Vladyslav Tarasiukc781ff12021-04-09 11:06:34 +0300249 [ETHTOOL_MSG_MODULE_EEPROM_GET] = &ethnl_module_eeprom_request_ops,
Jakub Kicinskif09ea6f2021-04-16 12:27:39 -0700250 [ETHTOOL_MSG_STATS_GET] = &ethnl_stats_request_ops,
Yangbo Luc156174a2021-06-30 16:11:56 +0800251 [ETHTOOL_MSG_PHC_VCLOCKS_GET] = &ethnl_phc_vclocks_request_ops,
Michal Kubecek728480f2019-12-27 15:55:38 +0100252};
253
254static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
255{
256 return (struct ethnl_dump_ctx *)cb->ctx;
257}
258
259/**
260 * ethnl_default_parse() - Parse request message
261 * @req_info: pointer to structure to put data into
Jakub Kicinski4f309742020-10-05 15:07:33 -0700262 * @tb: parsed attributes
Michal Kubecek728480f2019-12-27 15:55:38 +0100263 * @net: request netns
264 * @request_ops: struct request_ops for request type
265 * @extack: netlink extack for error reporting
266 * @require_dev: fail if no device identified in header
267 *
268 * Parse universal request header and call request specific ->parse_request()
269 * callback (if defined) to parse the rest of the message.
270 *
271 * Return: 0 on success or negative error code
272 */
273static int ethnl_default_parse(struct ethnl_req_info *req_info,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700274 struct nlattr **tb, struct net *net,
Michal Kubecek728480f2019-12-27 15:55:38 +0100275 const struct ethnl_request_ops *request_ops,
276 struct netlink_ext_ack *extack, bool require_dev)
277{
Michal Kubecek728480f2019-12-27 15:55:38 +0100278 int ret;
279
Michal Kubecek98130542020-03-12 21:07:38 +0100280 ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
281 net, extack, require_dev);
Michal Kubecek728480f2019-12-27 15:55:38 +0100282 if (ret < 0)
Jakub Kicinski4f309742020-10-05 15:07:33 -0700283 return ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100284
285 if (request_ops->parse_request) {
286 ret = request_ops->parse_request(req_info, tb, extack);
287 if (ret < 0)
Jakub Kicinski4f309742020-10-05 15:07:33 -0700288 return ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100289 }
290
Jakub Kicinski4f309742020-10-05 15:07:33 -0700291 return 0;
Michal Kubecek728480f2019-12-27 15:55:38 +0100292}
293
294/**
295 * ethnl_init_reply_data() - Initialize reply data for GET request
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100296 * @reply_data: pointer to embedded struct ethnl_reply_data
297 * @ops: instance of struct ethnl_request_ops describing the layout
298 * @dev: network device to initialize the reply for
Michal Kubecek728480f2019-12-27 15:55:38 +0100299 *
300 * Fills the reply data part with zeros and sets the dev member. Must be called
301 * before calling the ->fill_reply() callback (for each iteration when handling
302 * dump requests).
303 */
304static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
305 const struct ethnl_request_ops *ops,
306 struct net_device *dev)
307{
308 memset(reply_data, 0, ops->reply_data_size);
309 reply_data->dev = dev;
310}
311
312/* default ->doit() handler for GET type requests */
313static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
314{
315 struct ethnl_reply_data *reply_data = NULL;
316 struct ethnl_req_info *req_info = NULL;
317 const u8 cmd = info->genlhdr->cmd;
318 const struct ethnl_request_ops *ops;
Jakub Kicinski4d1fb7c2021-06-15 20:33:38 -0700319 int hdr_len, reply_len;
Michal Kubecek728480f2019-12-27 15:55:38 +0100320 struct sk_buff *rskb;
321 void *reply_payload;
Michal Kubecek728480f2019-12-27 15:55:38 +0100322 int ret;
323
324 ops = ethnl_default_requests[cmd];
325 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
326 return -EOPNOTSUPP;
327 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
328 if (!req_info)
329 return -ENOMEM;
330 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
331 if (!reply_data) {
332 kfree(req_info);
333 return -ENOMEM;
334 }
335
Jakub Kicinski4f309742020-10-05 15:07:33 -0700336 ret = ethnl_default_parse(req_info, info->attrs, genl_info_net(info),
337 ops, info->extack, !ops->allow_nodev_do);
Michal Kubecek728480f2019-12-27 15:55:38 +0100338 if (ret < 0)
339 goto err_dev;
340 ethnl_init_reply_data(reply_data, ops, req_info->dev);
341
342 rtnl_lock();
343 ret = ops->prepare_data(req_info, reply_data, info);
344 rtnl_unlock();
345 if (ret < 0)
346 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300347 ret = ops->reply_size(req_info, reply_data);
Michal Kubecek728480f2019-12-27 15:55:38 +0100348 if (ret < 0)
349 goto err_cleanup;
Jakub Kicinski4d1fb7c2021-06-15 20:33:38 -0700350 reply_len = ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100351 ret = -ENOMEM;
Jakub Kicinski4d1fb7c2021-06-15 20:33:38 -0700352 rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(),
353 req_info->dev, ops->reply_cmd,
Michal Kubecek728480f2019-12-27 15:55:38 +0100354 ops->hdr_attr, info, &reply_payload);
355 if (!rskb)
356 goto err_cleanup;
Jakub Kicinski4d1fb7c2021-06-15 20:33:38 -0700357 hdr_len = rskb->len;
Michal Kubecek728480f2019-12-27 15:55:38 +0100358 ret = ops->fill_reply(rskb, req_info, reply_data);
359 if (ret < 0)
360 goto err_msg;
Jakub Kicinski4d1fb7c2021-06-15 20:33:38 -0700361 WARN_ONCE(rskb->len - hdr_len > reply_len,
362 "ethnl cmd %d: calculated reply length %d, but consumed %d\n",
363 cmd, reply_len, rskb->len - hdr_len);
Michal Kubecek728480f2019-12-27 15:55:38 +0100364 if (ops->cleanup_data)
365 ops->cleanup_data(reply_data);
366
367 genlmsg_end(rskb, reply_payload);
368 if (req_info->dev)
369 dev_put(req_info->dev);
370 kfree(reply_data);
371 kfree(req_info);
372 return genlmsg_reply(rskb, info);
373
374err_msg:
375 WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
376 nlmsg_free(rskb);
377err_cleanup:
378 if (ops->cleanup_data)
379 ops->cleanup_data(reply_data);
380err_dev:
381 if (req_info->dev)
382 dev_put(req_info->dev);
383 kfree(reply_data);
384 kfree(req_info);
385 return ret;
386}
387
388static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200389 const struct ethnl_dump_ctx *ctx,
390 struct netlink_callback *cb)
Michal Kubecek728480f2019-12-27 15:55:38 +0100391{
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200392 void *ehdr;
Michal Kubecek728480f2019-12-27 15:55:38 +0100393 int ret;
394
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200395 ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Fernando Fernandez Manceracf754ae2021-05-05 00:47:14 +0200396 &ethtool_genl_family, NLM_F_MULTI,
397 ctx->ops->reply_cmd);
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200398 if (!ehdr)
399 return -EMSGSIZE;
400
Michal Kubecek728480f2019-12-27 15:55:38 +0100401 ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
402 rtnl_lock();
403 ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
404 rtnl_unlock();
405 if (ret < 0)
406 goto out;
407 ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
408 if (ret < 0)
409 goto out;
410 ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
411
412out:
413 if (ctx->ops->cleanup_data)
414 ctx->ops->cleanup_data(ctx->reply_data);
415 ctx->reply_data->dev = NULL;
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200416 if (ret < 0)
417 genlmsg_cancel(skb, ehdr);
418 else
419 genlmsg_end(skb, ehdr);
Michal Kubecek728480f2019-12-27 15:55:38 +0100420 return ret;
421}
422
423/* Default ->dumpit() handler for GET requests. Device iteration copied from
424 * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
425 * persistence as we cannot guarantee to hold RTNL lock through the whole
426 * function as rtnetnlink does.
427 */
428static int ethnl_default_dumpit(struct sk_buff *skb,
429 struct netlink_callback *cb)
430{
431 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
432 struct net *net = sock_net(skb->sk);
433 int s_idx = ctx->pos_idx;
434 int h, idx = 0;
435 int ret = 0;
Michal Kubecek728480f2019-12-27 15:55:38 +0100436
437 rtnl_lock();
438 for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
439 struct hlist_head *head;
440 struct net_device *dev;
441 unsigned int seq;
442
443 head = &net->dev_index_head[h];
444
445restart_chain:
446 seq = net->dev_base_seq;
447 cb->seq = seq;
448 idx = 0;
449 hlist_for_each_entry(dev, head, index_hlist) {
450 if (idx < s_idx)
451 goto cont;
452 dev_hold(dev);
453 rtnl_unlock();
454
Michal Kubecek365f9ae2020-07-09 12:11:50 +0200455 ret = ethnl_default_dump_one(skb, dev, ctx, cb);
Michal Kubecek728480f2019-12-27 15:55:38 +0100456 dev_put(dev);
457 if (ret < 0) {
Michal Kubecek728480f2019-12-27 15:55:38 +0100458 if (ret == -EOPNOTSUPP)
459 goto lock_and_cont;
460 if (likely(skb->len))
461 ret = skb->len;
462 goto out;
463 }
Michal Kubecek728480f2019-12-27 15:55:38 +0100464lock_and_cont:
465 rtnl_lock();
466 if (net->dev_base_seq != seq) {
467 s_idx = idx + 1;
468 goto restart_chain;
469 }
470cont:
471 idx++;
472 }
473
474 }
475 rtnl_unlock();
476
477out:
478 ctx->pos_hash = h;
479 ctx->pos_idx = idx;
480 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
481
482 return ret;
483}
484
485/* generic ->start() handler for GET requests */
486static int ethnl_default_start(struct netlink_callback *cb)
487{
Jakub Kicinski4f309742020-10-05 15:07:33 -0700488 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Michal Kubecek728480f2019-12-27 15:55:38 +0100489 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
490 struct ethnl_reply_data *reply_data;
491 const struct ethnl_request_ops *ops;
492 struct ethnl_req_info *req_info;
493 struct genlmsghdr *ghdr;
494 int ret;
495
496 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
497
498 ghdr = nlmsg_data(cb->nlh);
499 ops = ethnl_default_requests[ghdr->cmd];
500 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
501 return -EOPNOTSUPP;
502 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
503 if (!req_info)
504 return -ENOMEM;
505 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
506 if (!reply_data) {
Dan Carpentera6dd0482020-01-08 08:39:48 +0300507 ret = -ENOMEM;
508 goto free_req_info;
Michal Kubecek728480f2019-12-27 15:55:38 +0100509 }
510
Jakub Kicinski4f309742020-10-05 15:07:33 -0700511 ret = ethnl_default_parse(req_info, info->attrs, sock_net(cb->skb->sk),
512 ops, cb->extack, false);
Michal Kubecek728480f2019-12-27 15:55:38 +0100513 if (req_info->dev) {
514 /* We ignore device specification in dump requests but as the
515 * same parser as for non-dump (doit) requests is used, it
516 * would take reference to the device if it finds one
517 */
518 dev_put(req_info->dev);
519 req_info->dev = NULL;
520 }
521 if (ret < 0)
Dan Carpentera6dd0482020-01-08 08:39:48 +0300522 goto free_reply_data;
Michal Kubecek728480f2019-12-27 15:55:38 +0100523
524 ctx->ops = ops;
525 ctx->req_info = req_info;
526 ctx->reply_data = reply_data;
527 ctx->pos_hash = 0;
528 ctx->pos_idx = 0;
529
530 return 0;
Dan Carpentera6dd0482020-01-08 08:39:48 +0300531
532free_reply_data:
533 kfree(reply_data);
534free_req_info:
535 kfree(req_info);
536
537 return ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100538}
539
540/* default ->done() handler for GET requests */
541static int ethnl_default_done(struct netlink_callback *cb)
542{
543 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
544
545 kfree(ctx->reply_data);
546 kfree(ctx->req_info);
547
548 return 0;
549}
550
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100551static const struct ethnl_request_ops *
552ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
Michal Kubecek73286732019-12-27 15:56:03 +0100553 [ETHTOOL_MSG_LINKINFO_NTF] = &ethnl_linkinfo_request_ops,
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100554 [ETHTOOL_MSG_LINKMODES_NTF] = &ethnl_linkmodes_request_ops,
Michal Kubecek0bda7af2020-01-26 23:11:10 +0100555 [ETHTOOL_MSG_DEBUG_NTF] = &ethnl_debug_request_ops,
Michal Kubecek67bffa72020-01-26 23:11:19 +0100556 [ETHTOOL_MSG_WOL_NTF] = &ethnl_wol_request_ops,
Michal Kubecek9c6451e2020-03-12 21:08:03 +0100557 [ETHTOOL_MSG_FEATURES_NTF] = &ethnl_features_request_ops,
Michal Kubecek111dcba2020-03-12 21:08:18 +0100558 [ETHTOOL_MSG_PRIVFLAGS_NTF] = &ethnl_privflags_request_ops,
Michal Kubecekbc9d1c92020-03-12 21:08:33 +0100559 [ETHTOOL_MSG_RINGS_NTF] = &ethnl_rings_request_ops,
Michal Kubecek546379b2020-03-12 21:08:48 +0100560 [ETHTOOL_MSG_CHANNELS_NTF] = &ethnl_channels_request_ops,
Michal Kubecek0cf3eac2020-03-28 00:01:18 +0100561 [ETHTOOL_MSG_COALESCE_NTF] = &ethnl_coalesce_request_ops,
Michal Kubecekbf37faa2020-03-28 00:01:33 +0100562 [ETHTOOL_MSG_PAUSE_NTF] = &ethnl_pause_request_ops,
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +0100563 [ETHTOOL_MSG_EEE_NTF] = &ethnl_eee_request_ops,
Jakub Kicinski1e5d1f62021-03-29 20:59:52 -0700564 [ETHTOOL_MSG_FEC_NTF] = &ethnl_fec_request_ops,
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100565};
566
567/* default notification handler */
568static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
569 const void *data)
570{
571 struct ethnl_reply_data *reply_data;
572 const struct ethnl_request_ops *ops;
573 struct ethnl_req_info *req_info;
574 struct sk_buff *skb;
575 void *reply_payload;
576 int reply_len;
577 int ret;
578
579 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
580 !ethnl_default_notify_ops[cmd],
581 "unexpected notification type %u\n", cmd))
582 return;
583 ops = ethnl_default_notify_ops[cmd];
584 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
585 if (!req_info)
586 return;
587 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
588 if (!reply_data) {
589 kfree(req_info);
590 return;
591 }
592
593 req_info->dev = dev;
594 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
595
596 ethnl_init_reply_data(reply_data, ops, dev);
597 ret = ops->prepare_data(req_info, reply_data, NULL);
598 if (ret < 0)
599 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300600 ret = ops->reply_size(req_info, reply_data);
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100601 if (ret < 0)
602 goto err_cleanup;
Michal Kubecek7c87e322020-05-10 21:04:09 +0200603 reply_len = ret + ethnl_reply_header_size();
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100604 ret = -ENOMEM;
605 skb = genlmsg_new(reply_len, GFP_KERNEL);
606 if (!skb)
607 goto err_cleanup;
608 reply_payload = ethnl_bcastmsg_put(skb, cmd);
609 if (!reply_payload)
610 goto err_skb;
611 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
612 if (ret < 0)
613 goto err_msg;
614 ret = ops->fill_reply(skb, req_info, reply_data);
615 if (ret < 0)
616 goto err_msg;
617 if (ops->cleanup_data)
618 ops->cleanup_data(reply_data);
619
620 genlmsg_end(skb, reply_payload);
621 kfree(reply_data);
622 kfree(req_info);
623 ethnl_multicast(skb, dev);
624 return;
625
626err_msg:
627 WARN_ONCE(ret == -EMSGSIZE,
628 "calculated message payload length (%d) not sufficient\n",
629 reply_len);
630err_skb:
631 nlmsg_free(skb);
632err_cleanup:
633 if (ops->cleanup_data)
634 ops->cleanup_data(reply_data);
635 kfree(reply_data);
636 kfree(req_info);
637 return;
638}
639
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100640/* notifications */
641
642typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
643 const void *data);
644
645static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
Michal Kubecek73286732019-12-27 15:56:03 +0100646 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify,
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100647 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify,
Michal Kubecek0bda7af2020-01-26 23:11:10 +0100648 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify,
Michal Kubecek67bffa72020-01-26 23:11:19 +0100649 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify,
Michal Kubecek9c6451e2020-03-12 21:08:03 +0100650 [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify,
Michal Kubecek111dcba2020-03-12 21:08:18 +0100651 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify,
Michal Kubecekbc9d1c92020-03-12 21:08:33 +0100652 [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify,
Michal Kubecek546379b2020-03-12 21:08:48 +0100653 [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify,
Michal Kubecek0cf3eac2020-03-28 00:01:18 +0100654 [ETHTOOL_MSG_COALESCE_NTF] = ethnl_default_notify,
Michal Kubecekbf37faa2020-03-28 00:01:33 +0100655 [ETHTOOL_MSG_PAUSE_NTF] = ethnl_default_notify,
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +0100656 [ETHTOOL_MSG_EEE_NTF] = ethnl_default_notify,
Jakub Kicinski1e5d1f62021-03-29 20:59:52 -0700657 [ETHTOOL_MSG_FEC_NTF] = ethnl_default_notify,
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100658};
659
660void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
661{
662 if (unlikely(!ethnl_ok))
663 return;
664 ASSERT_RTNL();
665
666 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
667 ethnl_notify_handlers[cmd]))
668 ethnl_notify_handlers[cmd](dev, cmd, data);
669 else
670 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
671 cmd, netdev_name(dev));
672}
673EXPORT_SYMBOL(ethtool_notify);
674
Michal Kubecek9c6451e2020-03-12 21:08:03 +0100675static void ethnl_notify_features(struct netdev_notifier_info *info)
676{
677 struct net_device *dev = netdev_notifier_info_to_dev(info);
678
679 ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
680}
681
682static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
683 void *ptr)
684{
685 switch (event) {
686 case NETDEV_FEAT_CHANGE:
687 ethnl_notify_features(ptr);
688 break;
689 }
690
691 return NOTIFY_DONE;
692}
693
694static struct notifier_block ethnl_netdev_notifier = {
695 .notifier_call = ethnl_netdev_event,
696};
697
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100698/* genetlink setup */
699
700static const struct genl_ops ethtool_genl_ops[] = {
Michal Kubecek71921692019-12-27 15:55:43 +0100701 {
702 .cmd = ETHTOOL_MSG_STRSET_GET,
703 .doit = ethnl_default_doit,
704 .start = ethnl_default_start,
705 .dumpit = ethnl_default_dumpit,
706 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700707 .policy = ethnl_strset_get_policy,
708 .maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
Michal Kubecek71921692019-12-27 15:55:43 +0100709 },
Michal Kubecek459e0b82019-12-27 15:55:48 +0100710 {
711 .cmd = ETHTOOL_MSG_LINKINFO_GET,
712 .doit = ethnl_default_doit,
713 .start = ethnl_default_start,
714 .dumpit = ethnl_default_dumpit,
715 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700716 .policy = ethnl_linkinfo_get_policy,
717 .maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
Michal Kubecek459e0b82019-12-27 15:55:48 +0100718 },
Michal Kubeceka53f3d42019-12-27 15:55:53 +0100719 {
720 .cmd = ETHTOOL_MSG_LINKINFO_SET,
721 .flags = GENL_UNS_ADMIN_PERM,
722 .doit = ethnl_set_linkinfo,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700723 .policy = ethnl_linkinfo_set_policy,
724 .maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
Michal Kubeceka53f3d42019-12-27 15:55:53 +0100725 },
Michal Kubecekf625aa92019-12-27 15:56:08 +0100726 {
727 .cmd = ETHTOOL_MSG_LINKMODES_GET,
728 .doit = ethnl_default_doit,
729 .start = ethnl_default_start,
730 .dumpit = ethnl_default_dumpit,
731 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700732 .policy = ethnl_linkmodes_get_policy,
733 .maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
Michal Kubecekf625aa92019-12-27 15:56:08 +0100734 },
Michal Kubecekbfbcfe22019-12-27 15:56:13 +0100735 {
736 .cmd = ETHTOOL_MSG_LINKMODES_SET,
737 .flags = GENL_UNS_ADMIN_PERM,
738 .doit = ethnl_set_linkmodes,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700739 .policy = ethnl_linkmodes_set_policy,
740 .maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
Michal Kubecekbfbcfe22019-12-27 15:56:13 +0100741 },
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100742 {
743 .cmd = ETHTOOL_MSG_LINKSTATE_GET,
744 .doit = ethnl_default_doit,
745 .start = ethnl_default_start,
746 .dumpit = ethnl_default_dumpit,
747 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700748 .policy = ethnl_linkstate_get_policy,
749 .maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100750 },
Michal Kubecek6a94b8c2020-01-26 23:11:04 +0100751 {
752 .cmd = ETHTOOL_MSG_DEBUG_GET,
753 .doit = ethnl_default_doit,
754 .start = ethnl_default_start,
755 .dumpit = ethnl_default_dumpit,
756 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700757 .policy = ethnl_debug_get_policy,
758 .maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
Michal Kubecek6a94b8c2020-01-26 23:11:04 +0100759 },
Michal Kubeceke54d04e2020-01-26 23:11:07 +0100760 {
761 .cmd = ETHTOOL_MSG_DEBUG_SET,
762 .flags = GENL_UNS_ADMIN_PERM,
763 .doit = ethnl_set_debug,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700764 .policy = ethnl_debug_set_policy,
765 .maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
Michal Kubeceke54d04e2020-01-26 23:11:07 +0100766 },
Michal Kubecek51ea22b2020-01-26 23:11:13 +0100767 {
768 .cmd = ETHTOOL_MSG_WOL_GET,
769 .flags = GENL_UNS_ADMIN_PERM,
770 .doit = ethnl_default_doit,
771 .start = ethnl_default_start,
772 .dumpit = ethnl_default_dumpit,
773 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700774 .policy = ethnl_wol_get_policy,
775 .maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
Michal Kubecek51ea22b2020-01-26 23:11:13 +0100776 },
Michal Kubecek8d425b12020-01-26 23:11:16 +0100777 {
778 .cmd = ETHTOOL_MSG_WOL_SET,
779 .flags = GENL_UNS_ADMIN_PERM,
780 .doit = ethnl_set_wol,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700781 .policy = ethnl_wol_set_policy,
782 .maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
Michal Kubecek8d425b12020-01-26 23:11:16 +0100783 },
Michal Kubecek05243992020-03-12 21:07:48 +0100784 {
785 .cmd = ETHTOOL_MSG_FEATURES_GET,
786 .doit = ethnl_default_doit,
787 .start = ethnl_default_start,
788 .dumpit = ethnl_default_dumpit,
789 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700790 .policy = ethnl_features_get_policy,
791 .maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
Michal Kubecek05243992020-03-12 21:07:48 +0100792 },
Michal Kubecek0980bfc2020-03-12 21:07:58 +0100793 {
794 .cmd = ETHTOOL_MSG_FEATURES_SET,
795 .flags = GENL_UNS_ADMIN_PERM,
796 .doit = ethnl_set_features,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700797 .policy = ethnl_features_set_policy,
798 .maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
Michal Kubecek0980bfc2020-03-12 21:07:58 +0100799 },
Michal Kubeceke16c3382020-03-12 21:08:08 +0100800 {
801 .cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
802 .doit = ethnl_default_doit,
803 .start = ethnl_default_start,
804 .dumpit = ethnl_default_dumpit,
805 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700806 .policy = ethnl_privflags_get_policy,
807 .maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
Michal Kubeceke16c3382020-03-12 21:08:08 +0100808 },
Michal Kubecekf265d792020-03-12 21:08:13 +0100809 {
810 .cmd = ETHTOOL_MSG_PRIVFLAGS_SET,
811 .flags = GENL_UNS_ADMIN_PERM,
812 .doit = ethnl_set_privflags,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700813 .policy = ethnl_privflags_set_policy,
814 .maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
Michal Kubecekf265d792020-03-12 21:08:13 +0100815 },
Michal Kubeceke4a17172020-03-12 21:08:23 +0100816 {
817 .cmd = ETHTOOL_MSG_RINGS_GET,
818 .doit = ethnl_default_doit,
819 .start = ethnl_default_start,
820 .dumpit = ethnl_default_dumpit,
821 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700822 .policy = ethnl_rings_get_policy,
823 .maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
Michal Kubeceke4a17172020-03-12 21:08:23 +0100824 },
Michal Kubecek2fc29292020-03-12 21:08:28 +0100825 {
826 .cmd = ETHTOOL_MSG_RINGS_SET,
827 .flags = GENL_UNS_ADMIN_PERM,
828 .doit = ethnl_set_rings,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700829 .policy = ethnl_rings_set_policy,
830 .maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
Michal Kubecek2fc29292020-03-12 21:08:28 +0100831 },
Michal Kubecek0c849792020-03-12 21:08:38 +0100832 {
833 .cmd = ETHTOOL_MSG_CHANNELS_GET,
834 .doit = ethnl_default_doit,
835 .start = ethnl_default_start,
836 .dumpit = ethnl_default_dumpit,
837 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700838 .policy = ethnl_channels_get_policy,
839 .maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
Michal Kubecek0c849792020-03-12 21:08:38 +0100840 },
Michal Kubeceke19c5912020-03-12 21:08:43 +0100841 {
842 .cmd = ETHTOOL_MSG_CHANNELS_SET,
843 .flags = GENL_UNS_ADMIN_PERM,
844 .doit = ethnl_set_channels,
Johannes Bergfd15dd02020-10-07 12:53:51 +0200845 .policy = ethnl_channels_set_policy,
846 .maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
Michal Kubeceke19c5912020-03-12 21:08:43 +0100847 },
Michal Kubecek21727542020-03-28 00:01:08 +0100848 {
849 .cmd = ETHTOOL_MSG_COALESCE_GET,
850 .doit = ethnl_default_doit,
851 .start = ethnl_default_start,
852 .dumpit = ethnl_default_dumpit,
853 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700854 .policy = ethnl_coalesce_get_policy,
855 .maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
Michal Kubecek21727542020-03-28 00:01:08 +0100856 },
Michal Kubecek98814182020-03-28 00:01:13 +0100857 {
858 .cmd = ETHTOOL_MSG_COALESCE_SET,
859 .flags = GENL_UNS_ADMIN_PERM,
860 .doit = ethnl_set_coalesce,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700861 .policy = ethnl_coalesce_set_policy,
862 .maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
Michal Kubecek98814182020-03-28 00:01:13 +0100863 },
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100864 {
865 .cmd = ETHTOOL_MSG_PAUSE_GET,
866 .doit = ethnl_default_doit,
867 .start = ethnl_default_start,
868 .dumpit = ethnl_default_dumpit,
869 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700870 .policy = ethnl_pause_get_policy,
871 .maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100872 },
Michal Kubecek3ab87992020-03-28 00:01:28 +0100873 {
874 .cmd = ETHTOOL_MSG_PAUSE_SET,
875 .flags = GENL_UNS_ADMIN_PERM,
876 .doit = ethnl_set_pause,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700877 .policy = ethnl_pause_set_policy,
878 .maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
Michal Kubecek3ab87992020-03-28 00:01:28 +0100879 },
Michal Kubecekb7eeefe2020-03-28 00:01:38 +0100880 {
881 .cmd = ETHTOOL_MSG_EEE_GET,
882 .doit = ethnl_default_doit,
883 .start = ethnl_default_start,
884 .dumpit = ethnl_default_dumpit,
885 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700886 .policy = ethnl_eee_get_policy,
887 .maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
Michal Kubecekb7eeefe2020-03-28 00:01:38 +0100888 },
Michal Kubecekfd77be72020-03-28 00:01:43 +0100889 {
890 .cmd = ETHTOOL_MSG_EEE_SET,
891 .flags = GENL_UNS_ADMIN_PERM,
892 .doit = ethnl_set_eee,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700893 .policy = ethnl_eee_set_policy,
894 .maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
Michal Kubecekfd77be72020-03-28 00:01:43 +0100895 },
Michal Kubecek5b071c52020-03-28 00:01:58 +0100896 {
897 .cmd = ETHTOOL_MSG_TSINFO_GET,
898 .doit = ethnl_default_doit,
899 .start = ethnl_default_start,
900 .dumpit = ethnl_default_dumpit,
901 .done = ethnl_default_done,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700902 .policy = ethnl_tsinfo_get_policy,
903 .maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
Michal Kubecek5b071c52020-03-28 00:01:58 +0100904 },
Andrew Lunn11ca3c422020-05-10 21:12:33 +0200905 {
906 .cmd = ETHTOOL_MSG_CABLE_TEST_ACT,
907 .flags = GENL_UNS_ADMIN_PERM,
908 .doit = ethnl_act_cable_test,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700909 .policy = ethnl_cable_test_act_policy,
910 .maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
Andrew Lunn11ca3c422020-05-10 21:12:33 +0200911 },
Andrew Lunn1a644de2020-05-27 00:21:38 +0200912 {
913 .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
914 .flags = GENL_UNS_ADMIN_PERM,
915 .doit = ethnl_act_cable_test_tdr,
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700916 .policy = ethnl_cable_test_tdr_act_policy,
917 .maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
Andrew Lunn1a644de2020-05-27 00:21:38 +0200918 },
Jakub Kicinskic7d759e2020-07-09 17:42:47 -0700919 {
920 .cmd = ETHTOOL_MSG_TUNNEL_INFO_GET,
921 .doit = ethnl_tunnel_info_doit,
922 .start = ethnl_tunnel_info_start,
923 .dumpit = ethnl_tunnel_info_dumpit,
Jakub Kicinski4f309742020-10-05 15:07:33 -0700924 .policy = ethnl_tunnel_info_get_policy,
925 .maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
Jakub Kicinskic7d759e2020-07-09 17:42:47 -0700926 },
Jakub Kicinski1e5d1f62021-03-29 20:59:52 -0700927 {
928 .cmd = ETHTOOL_MSG_FEC_GET,
929 .doit = ethnl_default_doit,
930 .start = ethnl_default_start,
931 .dumpit = ethnl_default_dumpit,
932 .done = ethnl_default_done,
933 .policy = ethnl_fec_get_policy,
934 .maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
935 },
936 {
937 .cmd = ETHTOOL_MSG_FEC_SET,
938 .flags = GENL_UNS_ADMIN_PERM,
939 .doit = ethnl_set_fec,
940 .policy = ethnl_fec_set_policy,
941 .maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
942 },
Vladyslav Tarasiukc781ff12021-04-09 11:06:34 +0300943 {
944 .cmd = ETHTOOL_MSG_MODULE_EEPROM_GET,
945 .flags = GENL_UNS_ADMIN_PERM,
946 .doit = ethnl_default_doit,
947 .start = ethnl_default_start,
948 .dumpit = ethnl_default_dumpit,
949 .done = ethnl_default_done,
950 .policy = ethnl_module_eeprom_get_policy,
951 .maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
952 },
Jakub Kicinskif09ea6f2021-04-16 12:27:39 -0700953 {
954 .cmd = ETHTOOL_MSG_STATS_GET,
955 .doit = ethnl_default_doit,
956 .start = ethnl_default_start,
957 .dumpit = ethnl_default_dumpit,
958 .done = ethnl_default_done,
959 .policy = ethnl_stats_get_policy,
960 .maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
961 },
Yangbo Luc156174a2021-06-30 16:11:56 +0800962 {
963 .cmd = ETHTOOL_MSG_PHC_VCLOCKS_GET,
964 .doit = ethnl_default_doit,
965 .start = ethnl_default_start,
966 .dumpit = ethnl_default_dumpit,
967 .done = ethnl_default_done,
968 .policy = ethnl_phc_vclocks_get_policy,
969 .maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
970 },
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100971};
972
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100973static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
974 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
975};
976
Jakub Kicinski78b70152020-09-28 17:58:41 -0700977static struct genl_family ethtool_genl_family __ro_after_init = {
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100978 .name = ETHTOOL_GENL_NAME,
979 .version = ETHTOOL_GENL_VERSION,
980 .netnsok = true,
981 .parallel_ops = true,
982 .ops = ethtool_genl_ops,
983 .n_ops = ARRAY_SIZE(ethtool_genl_ops),
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100984 .mcgrps = ethtool_nl_mcgrps,
985 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps),
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100986};
987
988/* module setup */
989
990static int __init ethnl_init(void)
991{
992 int ret;
993
994 ret = genl_register_family(&ethtool_genl_family);
995 if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
996 return ret;
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100997 ethnl_ok = true;
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100998
Michal Kubecek9c6451e2020-03-12 21:08:03 +0100999 ret = register_netdevice_notifier(&ethnl_netdev_notifier);
1000 WARN(ret < 0, "ethtool: net device notifier registration failed");
1001 return ret;
Michal Kubecek2b4a8992019-12-27 15:55:18 +01001002}
1003
1004subsys_initcall(ethnl_init);