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