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