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