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_dev;
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_cancel;
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 out_cancel:
573 ctx->reply_data->dev = NULL;
574 if (ret < 0)
575 genlmsg_cancel(skb, ehdr);
576 else
577 genlmsg_end(skb, ehdr);
578 return ret;
579 }
580
581 /* Default ->dumpit() handler for GET requests. */
ethnl_default_dumpit(struct sk_buff * skb,struct netlink_callback * cb)582 static int ethnl_default_dumpit(struct sk_buff *skb,
583 struct netlink_callback *cb)
584 {
585 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
586 struct net *net = sock_net(skb->sk);
587 struct net_device *dev;
588 int ret = 0;
589
590 rcu_read_lock();
591 for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
592 dev_hold(dev);
593 rcu_read_unlock();
594
595 ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
596
597 rcu_read_lock();
598 dev_put(dev);
599
600 if (ret < 0 && ret != -EOPNOTSUPP) {
601 if (likely(skb->len))
602 ret = skb->len;
603 break;
604 }
605 ret = 0;
606 }
607 rcu_read_unlock();
608
609 return ret;
610 }
611
612 /* generic ->start() handler for GET requests */
ethnl_default_start(struct netlink_callback * cb)613 static int ethnl_default_start(struct netlink_callback *cb)
614 {
615 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
616 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
617 struct ethnl_reply_data *reply_data;
618 const struct ethnl_request_ops *ops;
619 struct ethnl_req_info *req_info;
620 struct genlmsghdr *ghdr;
621 int ret;
622
623 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
624
625 ghdr = nlmsg_data(cb->nlh);
626 ops = ethnl_default_requests[ghdr->cmd];
627 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
628 return -EOPNOTSUPP;
629 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
630 if (!req_info)
631 return -ENOMEM;
632 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
633 if (!reply_data) {
634 ret = -ENOMEM;
635 goto free_req_info;
636 }
637
638 ret = ethnl_default_parse(req_info, &info->info, ops, false);
639 if (req_info->dev) {
640 /* We ignore device specification in dump requests but as the
641 * same parser as for non-dump (doit) requests is used, it
642 * would take reference to the device if it finds one
643 */
644 netdev_put(req_info->dev, &req_info->dev_tracker);
645 req_info->dev = NULL;
646 }
647 if (ret < 0)
648 goto free_reply_data;
649
650 ctx->ops = ops;
651 ctx->req_info = req_info;
652 ctx->reply_data = reply_data;
653 ctx->pos_ifindex = 0;
654
655 return 0;
656
657 free_reply_data:
658 kfree(reply_data);
659 free_req_info:
660 kfree(req_info);
661
662 return ret;
663 }
664
665 /* default ->done() handler for GET requests */
ethnl_default_done(struct netlink_callback * cb)666 static int ethnl_default_done(struct netlink_callback *cb)
667 {
668 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
669
670 kfree(ctx->reply_data);
671 kfree(ctx->req_info);
672
673 return 0;
674 }
675
ethnl_default_set_doit(struct sk_buff * skb,struct genl_info * info)676 static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)
677 {
678 const struct ethnl_request_ops *ops;
679 struct ethnl_req_info req_info = {};
680 const u8 cmd = info->genlhdr->cmd;
681 struct net_device *dev;
682 int ret;
683
684 ops = ethnl_default_requests[cmd];
685 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
686 return -EOPNOTSUPP;
687 if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
688 return -EINVAL;
689
690 ret = ethnl_parse_header_dev_get(&req_info, info->attrs[ops->hdr_attr],
691 genl_info_net(info), info->extack,
692 true);
693 if (ret < 0)
694 return ret;
695
696 if (ops->set_validate) {
697 ret = ops->set_validate(&req_info, info);
698 /* 0 means nothing to do */
699 if (ret <= 0)
700 goto out_dev;
701 }
702
703 dev = req_info.dev;
704
705 rtnl_lock();
706 netdev_lock_ops(dev);
707 dev->cfg_pending = kmemdup(dev->cfg, sizeof(*dev->cfg),
708 GFP_KERNEL_ACCOUNT);
709 if (!dev->cfg_pending) {
710 ret = -ENOMEM;
711 goto out_tie_cfg;
712 }
713
714 ret = ethnl_ops_begin(dev);
715 if (ret < 0)
716 goto out_free_cfg;
717
718 ret = ops->set(&req_info, info);
719 if (ret < 0)
720 goto out_ops;
721
722 swap(dev->cfg, dev->cfg_pending);
723 if (!ret)
724 goto out_ops;
725 ethtool_notify(dev, ops->set_ntf_cmd, NULL);
726
727 ret = 0;
728 out_ops:
729 ethnl_ops_complete(dev);
730 out_free_cfg:
731 kfree(dev->cfg_pending);
732 out_tie_cfg:
733 dev->cfg_pending = dev->cfg;
734 netdev_unlock_ops(dev);
735 rtnl_unlock();
736 out_dev:
737 ethnl_parse_header_dev_put(&req_info);
738 return ret;
739 }
740
741 static const struct ethnl_request_ops *
742 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
743 [ETHTOOL_MSG_LINKINFO_NTF] = ðnl_linkinfo_request_ops,
744 [ETHTOOL_MSG_LINKMODES_NTF] = ðnl_linkmodes_request_ops,
745 [ETHTOOL_MSG_DEBUG_NTF] = ðnl_debug_request_ops,
746 [ETHTOOL_MSG_WOL_NTF] = ðnl_wol_request_ops,
747 [ETHTOOL_MSG_FEATURES_NTF] = ðnl_features_request_ops,
748 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ðnl_privflags_request_ops,
749 [ETHTOOL_MSG_RINGS_NTF] = ðnl_rings_request_ops,
750 [ETHTOOL_MSG_CHANNELS_NTF] = ðnl_channels_request_ops,
751 [ETHTOOL_MSG_COALESCE_NTF] = ðnl_coalesce_request_ops,
752 [ETHTOOL_MSG_PAUSE_NTF] = ðnl_pause_request_ops,
753 [ETHTOOL_MSG_EEE_NTF] = ðnl_eee_request_ops,
754 [ETHTOOL_MSG_FEC_NTF] = ðnl_fec_request_ops,
755 [ETHTOOL_MSG_MODULE_NTF] = ðnl_module_request_ops,
756 [ETHTOOL_MSG_PLCA_NTF] = ðnl_plca_cfg_request_ops,
757 [ETHTOOL_MSG_MM_NTF] = ðnl_mm_request_ops,
758 };
759
760 /* default notification handler */
ethnl_default_notify(struct net_device * dev,unsigned int cmd,const void * data)761 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
762 const void *data)
763 {
764 struct ethnl_reply_data *reply_data;
765 const struct ethnl_request_ops *ops;
766 struct ethnl_req_info *req_info;
767 struct genl_info info;
768 struct sk_buff *skb;
769 void *reply_payload;
770 int reply_len;
771 int ret;
772
773 genl_info_init_ntf(&info, ðtool_genl_family, cmd);
774
775 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
776 !ethnl_default_notify_ops[cmd],
777 "unexpected notification type %u\n", cmd))
778 return;
779 ops = ethnl_default_notify_ops[cmd];
780 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
781 if (!req_info)
782 return;
783 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
784 if (!reply_data) {
785 kfree(req_info);
786 return;
787 }
788
789 req_info->dev = dev;
790 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
791
792 netdev_ops_assert_locked(dev);
793
794 ethnl_init_reply_data(reply_data, ops, dev);
795 ret = ops->prepare_data(req_info, reply_data, &info);
796 if (ret < 0)
797 goto err_rep;
798 ret = ops->reply_size(req_info, reply_data);
799 if (ret < 0)
800 goto err_cleanup;
801 reply_len = ret + ethnl_reply_header_size();
802 skb = genlmsg_new(reply_len, GFP_KERNEL);
803 if (!skb)
804 goto err_cleanup;
805 reply_payload = ethnl_bcastmsg_put(skb, cmd);
806 if (!reply_payload)
807 goto err_skb;
808 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
809 if (ret < 0)
810 goto err_msg;
811 ret = ops->fill_reply(skb, req_info, reply_data);
812 if (ret < 0)
813 goto err_msg;
814 if (ops->cleanup_data)
815 ops->cleanup_data(reply_data);
816
817 genlmsg_end(skb, reply_payload);
818 kfree(reply_data);
819 kfree(req_info);
820 ethnl_multicast(skb, dev);
821 return;
822
823 err_msg:
824 WARN_ONCE(ret == -EMSGSIZE,
825 "calculated message payload length (%d) not sufficient\n",
826 reply_len);
827 err_skb:
828 nlmsg_free(skb);
829 err_cleanup:
830 if (ops->cleanup_data)
831 ops->cleanup_data(reply_data);
832 err_rep:
833 kfree(reply_data);
834 kfree(req_info);
835 return;
836 }
837
838 /* notifications */
839
840 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
841 const void *data);
842
843 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
844 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify,
845 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify,
846 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify,
847 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify,
848 [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify,
849 [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify,
850 [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify,
851 [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify,
852 [ETHTOOL_MSG_COALESCE_NTF] = ethnl_default_notify,
853 [ETHTOOL_MSG_PAUSE_NTF] = ethnl_default_notify,
854 [ETHTOOL_MSG_EEE_NTF] = ethnl_default_notify,
855 [ETHTOOL_MSG_FEC_NTF] = ethnl_default_notify,
856 [ETHTOOL_MSG_MODULE_NTF] = ethnl_default_notify,
857 [ETHTOOL_MSG_PLCA_NTF] = ethnl_default_notify,
858 [ETHTOOL_MSG_MM_NTF] = ethnl_default_notify,
859 };
860
ethtool_notify(struct net_device * dev,unsigned int cmd,const void * data)861 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
862 {
863 if (unlikely(!ethnl_ok))
864 return;
865 ASSERT_RTNL();
866
867 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
868 ethnl_notify_handlers[cmd]))
869 ethnl_notify_handlers[cmd](dev, cmd, data);
870 else
871 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
872 cmd, netdev_name(dev));
873 }
874 EXPORT_SYMBOL(ethtool_notify);
875
ethnl_notify_features(struct netdev_notifier_info * info)876 static void ethnl_notify_features(struct netdev_notifier_info *info)
877 {
878 struct net_device *dev = netdev_notifier_info_to_dev(info);
879
880 ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
881 }
882
ethnl_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)883 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
884 void *ptr)
885 {
886 struct netdev_notifier_info *info = ptr;
887 struct netlink_ext_ack *extack;
888 struct net_device *dev;
889
890 dev = netdev_notifier_info_to_dev(info);
891 extack = netdev_notifier_info_to_extack(info);
892
893 switch (event) {
894 case NETDEV_FEAT_CHANGE:
895 ethnl_notify_features(ptr);
896 break;
897 case NETDEV_PRE_UP:
898 if (dev->ethtool->module_fw_flash_in_progress) {
899 NL_SET_ERR_MSG(extack, "Can't set port up while flashing module firmware");
900 return NOTIFY_BAD;
901 }
902 }
903
904 return NOTIFY_DONE;
905 }
906
907 static struct notifier_block ethnl_netdev_notifier = {
908 .notifier_call = ethnl_netdev_event,
909 };
910
911 /* genetlink setup */
912
913 static const struct genl_ops ethtool_genl_ops[] = {
914 {
915 .cmd = ETHTOOL_MSG_STRSET_GET,
916 .doit = ethnl_default_doit,
917 .start = ethnl_default_start,
918 .dumpit = ethnl_default_dumpit,
919 .done = ethnl_default_done,
920 .policy = ethnl_strset_get_policy,
921 .maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
922 },
923 {
924 .cmd = ETHTOOL_MSG_LINKINFO_GET,
925 .doit = ethnl_default_doit,
926 .start = ethnl_default_start,
927 .dumpit = ethnl_default_dumpit,
928 .done = ethnl_default_done,
929 .policy = ethnl_linkinfo_get_policy,
930 .maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
931 },
932 {
933 .cmd = ETHTOOL_MSG_LINKINFO_SET,
934 .flags = GENL_UNS_ADMIN_PERM,
935 .doit = ethnl_default_set_doit,
936 .policy = ethnl_linkinfo_set_policy,
937 .maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
938 },
939 {
940 .cmd = ETHTOOL_MSG_LINKMODES_GET,
941 .doit = ethnl_default_doit,
942 .start = ethnl_default_start,
943 .dumpit = ethnl_default_dumpit,
944 .done = ethnl_default_done,
945 .policy = ethnl_linkmodes_get_policy,
946 .maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
947 },
948 {
949 .cmd = ETHTOOL_MSG_LINKMODES_SET,
950 .flags = GENL_UNS_ADMIN_PERM,
951 .doit = ethnl_default_set_doit,
952 .policy = ethnl_linkmodes_set_policy,
953 .maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
954 },
955 {
956 .cmd = ETHTOOL_MSG_LINKSTATE_GET,
957 .doit = ethnl_default_doit,
958 .start = ethnl_default_start,
959 .dumpit = ethnl_default_dumpit,
960 .done = ethnl_default_done,
961 .policy = ethnl_linkstate_get_policy,
962 .maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
963 },
964 {
965 .cmd = ETHTOOL_MSG_DEBUG_GET,
966 .doit = ethnl_default_doit,
967 .start = ethnl_default_start,
968 .dumpit = ethnl_default_dumpit,
969 .done = ethnl_default_done,
970 .policy = ethnl_debug_get_policy,
971 .maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
972 },
973 {
974 .cmd = ETHTOOL_MSG_DEBUG_SET,
975 .flags = GENL_UNS_ADMIN_PERM,
976 .doit = ethnl_default_set_doit,
977 .policy = ethnl_debug_set_policy,
978 .maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
979 },
980 {
981 .cmd = ETHTOOL_MSG_WOL_GET,
982 .flags = GENL_UNS_ADMIN_PERM,
983 .doit = ethnl_default_doit,
984 .start = ethnl_default_start,
985 .dumpit = ethnl_default_dumpit,
986 .done = ethnl_default_done,
987 .policy = ethnl_wol_get_policy,
988 .maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
989 },
990 {
991 .cmd = ETHTOOL_MSG_WOL_SET,
992 .flags = GENL_UNS_ADMIN_PERM,
993 .doit = ethnl_default_set_doit,
994 .policy = ethnl_wol_set_policy,
995 .maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
996 },
997 {
998 .cmd = ETHTOOL_MSG_FEATURES_GET,
999 .doit = ethnl_default_doit,
1000 .start = ethnl_default_start,
1001 .dumpit = ethnl_default_dumpit,
1002 .done = ethnl_default_done,
1003 .policy = ethnl_features_get_policy,
1004 .maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
1005 },
1006 {
1007 .cmd = ETHTOOL_MSG_FEATURES_SET,
1008 .flags = GENL_UNS_ADMIN_PERM,
1009 .doit = ethnl_set_features,
1010 .policy = ethnl_features_set_policy,
1011 .maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
1012 },
1013 {
1014 .cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
1015 .doit = ethnl_default_doit,
1016 .start = ethnl_default_start,
1017 .dumpit = ethnl_default_dumpit,
1018 .done = ethnl_default_done,
1019 .policy = ethnl_privflags_get_policy,
1020 .maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
1021 },
1022 {
1023 .cmd = ETHTOOL_MSG_PRIVFLAGS_SET,
1024 .flags = GENL_UNS_ADMIN_PERM,
1025 .doit = ethnl_default_set_doit,
1026 .policy = ethnl_privflags_set_policy,
1027 .maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
1028 },
1029 {
1030 .cmd = ETHTOOL_MSG_RINGS_GET,
1031 .doit = ethnl_default_doit,
1032 .start = ethnl_default_start,
1033 .dumpit = ethnl_default_dumpit,
1034 .done = ethnl_default_done,
1035 .policy = ethnl_rings_get_policy,
1036 .maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
1037 },
1038 {
1039 .cmd = ETHTOOL_MSG_RINGS_SET,
1040 .flags = GENL_UNS_ADMIN_PERM,
1041 .doit = ethnl_default_set_doit,
1042 .policy = ethnl_rings_set_policy,
1043 .maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
1044 },
1045 {
1046 .cmd = ETHTOOL_MSG_CHANNELS_GET,
1047 .doit = ethnl_default_doit,
1048 .start = ethnl_default_start,
1049 .dumpit = ethnl_default_dumpit,
1050 .done = ethnl_default_done,
1051 .policy = ethnl_channels_get_policy,
1052 .maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
1053 },
1054 {
1055 .cmd = ETHTOOL_MSG_CHANNELS_SET,
1056 .flags = GENL_UNS_ADMIN_PERM,
1057 .doit = ethnl_default_set_doit,
1058 .policy = ethnl_channels_set_policy,
1059 .maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
1060 },
1061 {
1062 .cmd = ETHTOOL_MSG_COALESCE_GET,
1063 .doit = ethnl_default_doit,
1064 .start = ethnl_default_start,
1065 .dumpit = ethnl_default_dumpit,
1066 .done = ethnl_default_done,
1067 .policy = ethnl_coalesce_get_policy,
1068 .maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
1069 },
1070 {
1071 .cmd = ETHTOOL_MSG_COALESCE_SET,
1072 .flags = GENL_UNS_ADMIN_PERM,
1073 .doit = ethnl_default_set_doit,
1074 .policy = ethnl_coalesce_set_policy,
1075 .maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
1076 },
1077 {
1078 .cmd = ETHTOOL_MSG_PAUSE_GET,
1079 .doit = ethnl_default_doit,
1080 .start = ethnl_default_start,
1081 .dumpit = ethnl_default_dumpit,
1082 .done = ethnl_default_done,
1083 .policy = ethnl_pause_get_policy,
1084 .maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
1085 },
1086 {
1087 .cmd = ETHTOOL_MSG_PAUSE_SET,
1088 .flags = GENL_UNS_ADMIN_PERM,
1089 .doit = ethnl_default_set_doit,
1090 .policy = ethnl_pause_set_policy,
1091 .maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
1092 },
1093 {
1094 .cmd = ETHTOOL_MSG_EEE_GET,
1095 .doit = ethnl_default_doit,
1096 .start = ethnl_default_start,
1097 .dumpit = ethnl_default_dumpit,
1098 .done = ethnl_default_done,
1099 .policy = ethnl_eee_get_policy,
1100 .maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
1101 },
1102 {
1103 .cmd = ETHTOOL_MSG_EEE_SET,
1104 .flags = GENL_UNS_ADMIN_PERM,
1105 .doit = ethnl_default_set_doit,
1106 .policy = ethnl_eee_set_policy,
1107 .maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
1108 },
1109 {
1110 .cmd = ETHTOOL_MSG_TSINFO_GET,
1111 .doit = ethnl_default_doit,
1112 .start = ethnl_tsinfo_start,
1113 .dumpit = ethnl_tsinfo_dumpit,
1114 .done = ethnl_tsinfo_done,
1115 .policy = ethnl_tsinfo_get_policy,
1116 .maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
1117 },
1118 {
1119 .cmd = ETHTOOL_MSG_CABLE_TEST_ACT,
1120 .flags = GENL_UNS_ADMIN_PERM,
1121 .doit = ethnl_act_cable_test,
1122 .policy = ethnl_cable_test_act_policy,
1123 .maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
1124 },
1125 {
1126 .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
1127 .flags = GENL_UNS_ADMIN_PERM,
1128 .doit = ethnl_act_cable_test_tdr,
1129 .policy = ethnl_cable_test_tdr_act_policy,
1130 .maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
1131 },
1132 {
1133 .cmd = ETHTOOL_MSG_TUNNEL_INFO_GET,
1134 .doit = ethnl_tunnel_info_doit,
1135 .start = ethnl_tunnel_info_start,
1136 .dumpit = ethnl_tunnel_info_dumpit,
1137 .policy = ethnl_tunnel_info_get_policy,
1138 .maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
1139 },
1140 {
1141 .cmd = ETHTOOL_MSG_FEC_GET,
1142 .doit = ethnl_default_doit,
1143 .start = ethnl_default_start,
1144 .dumpit = ethnl_default_dumpit,
1145 .done = ethnl_default_done,
1146 .policy = ethnl_fec_get_policy,
1147 .maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
1148 },
1149 {
1150 .cmd = ETHTOOL_MSG_FEC_SET,
1151 .flags = GENL_UNS_ADMIN_PERM,
1152 .doit = ethnl_default_set_doit,
1153 .policy = ethnl_fec_set_policy,
1154 .maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
1155 },
1156 {
1157 .cmd = ETHTOOL_MSG_MODULE_EEPROM_GET,
1158 .flags = GENL_UNS_ADMIN_PERM,
1159 .doit = ethnl_default_doit,
1160 .start = ethnl_default_start,
1161 .dumpit = ethnl_default_dumpit,
1162 .done = ethnl_default_done,
1163 .policy = ethnl_module_eeprom_get_policy,
1164 .maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
1165 },
1166 {
1167 .cmd = ETHTOOL_MSG_STATS_GET,
1168 .doit = ethnl_default_doit,
1169 .start = ethnl_default_start,
1170 .dumpit = ethnl_default_dumpit,
1171 .done = ethnl_default_done,
1172 .policy = ethnl_stats_get_policy,
1173 .maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
1174 },
1175 {
1176 .cmd = ETHTOOL_MSG_PHC_VCLOCKS_GET,
1177 .doit = ethnl_default_doit,
1178 .start = ethnl_default_start,
1179 .dumpit = ethnl_default_dumpit,
1180 .done = ethnl_default_done,
1181 .policy = ethnl_phc_vclocks_get_policy,
1182 .maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
1183 },
1184 {
1185 .cmd = ETHTOOL_MSG_MODULE_GET,
1186 .doit = ethnl_default_doit,
1187 .start = ethnl_default_start,
1188 .dumpit = ethnl_default_dumpit,
1189 .done = ethnl_default_done,
1190 .policy = ethnl_module_get_policy,
1191 .maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1,
1192 },
1193 {
1194 .cmd = ETHTOOL_MSG_MODULE_SET,
1195 .flags = GENL_UNS_ADMIN_PERM,
1196 .doit = ethnl_default_set_doit,
1197 .policy = ethnl_module_set_policy,
1198 .maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1,
1199 },
1200 {
1201 .cmd = ETHTOOL_MSG_PSE_GET,
1202 .doit = ethnl_default_doit,
1203 .start = ethnl_default_start,
1204 .dumpit = ethnl_default_dumpit,
1205 .done = ethnl_default_done,
1206 .policy = ethnl_pse_get_policy,
1207 .maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
1208 },
1209 {
1210 .cmd = ETHTOOL_MSG_PSE_SET,
1211 .flags = GENL_UNS_ADMIN_PERM,
1212 .doit = ethnl_default_set_doit,
1213 .policy = ethnl_pse_set_policy,
1214 .maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1,
1215 },
1216 {
1217 .cmd = ETHTOOL_MSG_RSS_GET,
1218 .doit = ethnl_default_doit,
1219 .start = ethnl_rss_dump_start,
1220 .dumpit = ethnl_rss_dumpit,
1221 .policy = ethnl_rss_get_policy,
1222 .maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1,
1223 },
1224 {
1225 .cmd = ETHTOOL_MSG_PLCA_GET_CFG,
1226 .doit = ethnl_default_doit,
1227 .start = ethnl_default_start,
1228 .dumpit = ethnl_default_dumpit,
1229 .done = ethnl_default_done,
1230 .policy = ethnl_plca_get_cfg_policy,
1231 .maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
1232 },
1233 {
1234 .cmd = ETHTOOL_MSG_PLCA_SET_CFG,
1235 .flags = GENL_UNS_ADMIN_PERM,
1236 .doit = ethnl_default_set_doit,
1237 .policy = ethnl_plca_set_cfg_policy,
1238 .maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1,
1239 },
1240 {
1241 .cmd = ETHTOOL_MSG_PLCA_GET_STATUS,
1242 .doit = ethnl_default_doit,
1243 .start = ethnl_default_start,
1244 .dumpit = ethnl_default_dumpit,
1245 .done = ethnl_default_done,
1246 .policy = ethnl_plca_get_status_policy,
1247 .maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
1248 },
1249 {
1250 .cmd = ETHTOOL_MSG_MM_GET,
1251 .doit = ethnl_default_doit,
1252 .start = ethnl_default_start,
1253 .dumpit = ethnl_default_dumpit,
1254 .done = ethnl_default_done,
1255 .policy = ethnl_mm_get_policy,
1256 .maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1,
1257 },
1258 {
1259 .cmd = ETHTOOL_MSG_MM_SET,
1260 .flags = GENL_UNS_ADMIN_PERM,
1261 .doit = ethnl_default_set_doit,
1262 .policy = ethnl_mm_set_policy,
1263 .maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1,
1264 },
1265 {
1266 .cmd = ETHTOOL_MSG_MODULE_FW_FLASH_ACT,
1267 .flags = GENL_UNS_ADMIN_PERM,
1268 .doit = ethnl_act_module_fw_flash,
1269 .policy = ethnl_module_fw_flash_act_policy,
1270 .maxattr = ARRAY_SIZE(ethnl_module_fw_flash_act_policy) - 1,
1271 },
1272 {
1273 .cmd = ETHTOOL_MSG_PHY_GET,
1274 .doit = ethnl_phy_doit,
1275 .start = ethnl_phy_start,
1276 .dumpit = ethnl_phy_dumpit,
1277 .done = ethnl_phy_done,
1278 .policy = ethnl_phy_get_policy,
1279 .maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1,
1280 },
1281 {
1282 .cmd = ETHTOOL_MSG_TSCONFIG_GET,
1283 .doit = ethnl_default_doit,
1284 .start = ethnl_default_start,
1285 .dumpit = ethnl_default_dumpit,
1286 .done = ethnl_default_done,
1287 .policy = ethnl_tsconfig_get_policy,
1288 .maxattr = ARRAY_SIZE(ethnl_tsconfig_get_policy) - 1,
1289 },
1290 {
1291 .cmd = ETHTOOL_MSG_TSCONFIG_SET,
1292 .flags = GENL_UNS_ADMIN_PERM,
1293 .doit = ethnl_default_set_doit,
1294 .policy = ethnl_tsconfig_set_policy,
1295 .maxattr = ARRAY_SIZE(ethnl_tsconfig_set_policy) - 1,
1296 },
1297 };
1298
1299 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
1300 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
1301 };
1302
1303 static struct genl_family ethtool_genl_family __ro_after_init = {
1304 .name = ETHTOOL_GENL_NAME,
1305 .version = ETHTOOL_GENL_VERSION,
1306 .netnsok = true,
1307 .parallel_ops = true,
1308 .ops = ethtool_genl_ops,
1309 .n_ops = ARRAY_SIZE(ethtool_genl_ops),
1310 .resv_start_op = ETHTOOL_MSG_MODULE_GET + 1,
1311 .mcgrps = ethtool_nl_mcgrps,
1312 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps),
1313 .sock_priv_size = sizeof(struct ethnl_sock_priv),
1314 .sock_priv_destroy = ethnl_sock_priv_destroy,
1315 };
1316
1317 /* module setup */
1318
ethnl_init(void)1319 static int __init ethnl_init(void)
1320 {
1321 int ret;
1322
1323 ret = genl_register_family(ðtool_genl_family);
1324 if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
1325 return ret;
1326 ethnl_ok = true;
1327
1328 ret = register_netdevice_notifier(ðnl_netdev_notifier);
1329 WARN(ret < 0, "ethtool: net device notifier registration failed");
1330 return ret;
1331 }
1332
1333 subsys_initcall(ethnl_init);
1334