xref: /linux/net/ethtool/netlink.c (revision c5fbdf0ba7c1a6ed52dc3650bee73ce00c86cf7f)
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 
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(&ethtool_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 
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 
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 
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  */
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 
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  */
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  */
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, &ethtool_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 
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 			   &ethtool_genl_family, 0, cmd);
321 }
322 
323 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
324 {
325 	return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
326 			   cmd);
327 }
328 
329 void *ethnl_unicast_put(struct sk_buff *skb, u32 portid, u32 seq, u8 cmd)
330 {
331 	return genlmsg_put(skb, portid, seq, &ethtool_genl_family, 0, cmd);
332 }
333 
334 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
335 {
336 	return genlmsg_multicast_netns(&ethtool_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 /**
361  * struct ethnl_perphy_dump_ctx - context for dumpit() PHY-aware callbacks
362  * @ethnl_ctx: generic ethnl context
363  * @ifindex: For Filtered DUMP requests, the ifindex of the targeted netdev
364  * @pos_phyindex: iterator position for multi-msg DUMP
365  */
366 struct ethnl_perphy_dump_ctx {
367 	struct ethnl_dump_ctx	ethnl_ctx;
368 	unsigned int		ifindex;
369 	unsigned long		pos_phyindex;
370 };
371 
372 static const struct ethnl_request_ops *
373 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
374 	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
375 	[ETHTOOL_MSG_LINKINFO_GET]	= &ethnl_linkinfo_request_ops,
376 	[ETHTOOL_MSG_LINKINFO_SET]	= &ethnl_linkinfo_request_ops,
377 	[ETHTOOL_MSG_LINKMODES_GET]	= &ethnl_linkmodes_request_ops,
378 	[ETHTOOL_MSG_LINKMODES_SET]	= &ethnl_linkmodes_request_ops,
379 	[ETHTOOL_MSG_LINKSTATE_GET]	= &ethnl_linkstate_request_ops,
380 	[ETHTOOL_MSG_DEBUG_GET]		= &ethnl_debug_request_ops,
381 	[ETHTOOL_MSG_DEBUG_SET]		= &ethnl_debug_request_ops,
382 	[ETHTOOL_MSG_WOL_GET]		= &ethnl_wol_request_ops,
383 	[ETHTOOL_MSG_WOL_SET]		= &ethnl_wol_request_ops,
384 	[ETHTOOL_MSG_FEATURES_GET]	= &ethnl_features_request_ops,
385 	[ETHTOOL_MSG_PRIVFLAGS_GET]	= &ethnl_privflags_request_ops,
386 	[ETHTOOL_MSG_PRIVFLAGS_SET]	= &ethnl_privflags_request_ops,
387 	[ETHTOOL_MSG_RINGS_GET]		= &ethnl_rings_request_ops,
388 	[ETHTOOL_MSG_RINGS_SET]		= &ethnl_rings_request_ops,
389 	[ETHTOOL_MSG_CHANNELS_GET]	= &ethnl_channels_request_ops,
390 	[ETHTOOL_MSG_CHANNELS_SET]	= &ethnl_channels_request_ops,
391 	[ETHTOOL_MSG_COALESCE_GET]	= &ethnl_coalesce_request_ops,
392 	[ETHTOOL_MSG_COALESCE_SET]	= &ethnl_coalesce_request_ops,
393 	[ETHTOOL_MSG_PAUSE_GET]		= &ethnl_pause_request_ops,
394 	[ETHTOOL_MSG_PAUSE_SET]		= &ethnl_pause_request_ops,
395 	[ETHTOOL_MSG_EEE_GET]		= &ethnl_eee_request_ops,
396 	[ETHTOOL_MSG_EEE_SET]		= &ethnl_eee_request_ops,
397 	[ETHTOOL_MSG_FEC_GET]		= &ethnl_fec_request_ops,
398 	[ETHTOOL_MSG_FEC_SET]		= &ethnl_fec_request_ops,
399 	[ETHTOOL_MSG_TSINFO_GET]	= &ethnl_tsinfo_request_ops,
400 	[ETHTOOL_MSG_MODULE_EEPROM_GET]	= &ethnl_module_eeprom_request_ops,
401 	[ETHTOOL_MSG_STATS_GET]		= &ethnl_stats_request_ops,
402 	[ETHTOOL_MSG_PHC_VCLOCKS_GET]	= &ethnl_phc_vclocks_request_ops,
403 	[ETHTOOL_MSG_MODULE_GET]	= &ethnl_module_request_ops,
404 	[ETHTOOL_MSG_MODULE_SET]	= &ethnl_module_request_ops,
405 	[ETHTOOL_MSG_PSE_GET]		= &ethnl_pse_request_ops,
406 	[ETHTOOL_MSG_PSE_SET]		= &ethnl_pse_request_ops,
407 	[ETHTOOL_MSG_RSS_GET]		= &ethnl_rss_request_ops,
408 	[ETHTOOL_MSG_PLCA_GET_CFG]	= &ethnl_plca_cfg_request_ops,
409 	[ETHTOOL_MSG_PLCA_SET_CFG]	= &ethnl_plca_cfg_request_ops,
410 	[ETHTOOL_MSG_PLCA_GET_STATUS]	= &ethnl_plca_status_request_ops,
411 	[ETHTOOL_MSG_MM_GET]		= &ethnl_mm_request_ops,
412 	[ETHTOOL_MSG_MM_SET]		= &ethnl_mm_request_ops,
413 	[ETHTOOL_MSG_TSCONFIG_GET]	= &ethnl_tsconfig_request_ops,
414 	[ETHTOOL_MSG_TSCONFIG_SET]	= &ethnl_tsconfig_request_ops,
415 	[ETHTOOL_MSG_PHY_GET]		= &ethnl_phy_request_ops,
416 };
417 
418 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
419 {
420 	return (struct ethnl_dump_ctx *)cb->ctx;
421 }
422 
423 static struct ethnl_perphy_dump_ctx *
424 ethnl_perphy_dump_context(struct netlink_callback *cb)
425 {
426 	return (struct ethnl_perphy_dump_ctx *)cb->ctx;
427 }
428 
429 /**
430  * ethnl_default_parse() - Parse request message
431  * @req_info:    pointer to structure to put data into
432  * @info:	 genl_info from the request
433  * @request_ops: struct request_ops for request type
434  * @require_dev: fail if no device identified in header
435  *
436  * Parse universal request header and call request specific ->parse_request()
437  * callback (if defined) to parse the rest of the message.
438  *
439  * Return: 0 on success or negative error code
440  */
441 static int ethnl_default_parse(struct ethnl_req_info *req_info,
442 			       const struct genl_info *info,
443 			       const struct ethnl_request_ops *request_ops,
444 			       bool require_dev)
445 {
446 	struct nlattr **tb = info->attrs;
447 	int ret;
448 
449 	ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
450 					 genl_info_net(info), info->extack,
451 					 require_dev);
452 	if (ret < 0)
453 		return ret;
454 
455 	if (request_ops->parse_request) {
456 		ret = request_ops->parse_request(req_info, tb, info->extack);
457 		if (ret < 0)
458 			return ret;
459 	}
460 
461 	return 0;
462 }
463 
464 /**
465  * ethnl_init_reply_data() - Initialize reply data for GET request
466  * @reply_data: pointer to embedded struct ethnl_reply_data
467  * @ops:        instance of struct ethnl_request_ops describing the layout
468  * @dev:        network device to initialize the reply for
469  *
470  * Fills the reply data part with zeros and sets the dev member. Must be called
471  * before calling the ->fill_reply() callback (for each iteration when handling
472  * dump requests).
473  */
474 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
475 				  const struct ethnl_request_ops *ops,
476 				  struct net_device *dev)
477 {
478 	memset(reply_data, 0, ops->reply_data_size);
479 	reply_data->dev = dev;
480 }
481 
482 /* default ->doit() handler for GET type requests */
483 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
484 {
485 	struct ethnl_reply_data *reply_data = NULL;
486 	struct ethnl_req_info *req_info = NULL;
487 	const u8 cmd = info->genlhdr->cmd;
488 	const struct ethnl_request_ops *ops;
489 	int hdr_len, reply_len;
490 	struct sk_buff *rskb;
491 	void *reply_payload;
492 	int ret;
493 
494 	ops = ethnl_default_requests[cmd];
495 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
496 		return -EOPNOTSUPP;
497 	if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
498 		return -EINVAL;
499 
500 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
501 	if (!req_info)
502 		return -ENOMEM;
503 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
504 	if (!reply_data) {
505 		kfree(req_info);
506 		return -ENOMEM;
507 	}
508 
509 	ret = ethnl_default_parse(req_info, info, ops, !ops->allow_nodev_do);
510 	if (ret < 0)
511 		goto err_dev;
512 	ethnl_init_reply_data(reply_data, ops, req_info->dev);
513 
514 	rtnl_lock();
515 	if (req_info->dev)
516 		netdev_lock_ops(req_info->dev);
517 	ret = ops->prepare_data(req_info, reply_data, info);
518 	if (req_info->dev)
519 		netdev_unlock_ops(req_info->dev);
520 	rtnl_unlock();
521 	if (ret < 0)
522 		goto err_dev;
523 	ret = ops->reply_size(req_info, reply_data);
524 	if (ret < 0)
525 		goto err_cleanup;
526 	reply_len = ret;
527 	ret = -ENOMEM;
528 	rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(),
529 				req_info->dev, ops->reply_cmd,
530 				ops->hdr_attr, info, &reply_payload);
531 	if (!rskb)
532 		goto err_cleanup;
533 	hdr_len = rskb->len;
534 	ret = ops->fill_reply(rskb, req_info, reply_data);
535 	if (ret < 0)
536 		goto err_msg;
537 	WARN_ONCE(rskb->len - hdr_len > reply_len,
538 		  "ethnl cmd %d: calculated reply length %d, but consumed %d\n",
539 		  cmd, reply_len, rskb->len - hdr_len);
540 	if (ops->cleanup_data)
541 		ops->cleanup_data(reply_data);
542 
543 	genlmsg_end(rskb, reply_payload);
544 	netdev_put(req_info->dev, &req_info->dev_tracker);
545 	kfree(reply_data);
546 	kfree(req_info);
547 	return genlmsg_reply(rskb, info);
548 
549 err_msg:
550 	WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
551 	nlmsg_free(rskb);
552 err_cleanup:
553 	if (ops->cleanup_data)
554 		ops->cleanup_data(reply_data);
555 err_dev:
556 	netdev_put(req_info->dev, &req_info->dev_tracker);
557 	kfree(reply_data);
558 	kfree(req_info);
559 	return ret;
560 }
561 
562 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
563 				  const struct ethnl_dump_ctx *ctx,
564 				  const struct genl_info *info)
565 {
566 	void *ehdr;
567 	int ret;
568 
569 	ehdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
570 			   &ethtool_genl_family, NLM_F_MULTI,
571 			   ctx->ops->reply_cmd);
572 	if (!ehdr)
573 		return -EMSGSIZE;
574 
575 	ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
576 	rtnl_lock();
577 	netdev_lock_ops(dev);
578 	ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, info);
579 	netdev_unlock_ops(dev);
580 	rtnl_unlock();
581 	if (ret < 0)
582 		goto out_cancel;
583 	ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
584 	if (ret < 0)
585 		goto out;
586 	ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
587 
588 out:
589 	if (ctx->ops->cleanup_data)
590 		ctx->ops->cleanup_data(ctx->reply_data);
591 out_cancel:
592 	ctx->reply_data->dev = NULL;
593 	if (ret < 0)
594 		genlmsg_cancel(skb, ehdr);
595 	else
596 		genlmsg_end(skb, ehdr);
597 	return ret;
598 }
599 
600 /* Default ->dumpit() handler for GET requests. */
601 static int ethnl_default_dumpit(struct sk_buff *skb,
602 				struct netlink_callback *cb)
603 {
604 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
605 	struct net *net = sock_net(skb->sk);
606 	netdevice_tracker dev_tracker;
607 	struct net_device *dev;
608 	int ret = 0;
609 
610 	rcu_read_lock();
611 	for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
612 		netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
613 		rcu_read_unlock();
614 
615 		ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
616 
617 		rcu_read_lock();
618 		netdev_put(dev, &dev_tracker);
619 
620 		if (ret < 0 && ret != -EOPNOTSUPP) {
621 			if (likely(skb->len))
622 				ret = skb->len;
623 			break;
624 		}
625 		ret = 0;
626 	}
627 	rcu_read_unlock();
628 
629 	return ret;
630 }
631 
632 /* generic ->start() handler for GET requests */
633 static int ethnl_default_start(struct netlink_callback *cb)
634 {
635 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
636 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
637 	struct ethnl_reply_data *reply_data;
638 	const struct ethnl_request_ops *ops;
639 	struct ethnl_req_info *req_info;
640 	struct genlmsghdr *ghdr;
641 	int ret;
642 
643 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
644 
645 	ghdr = nlmsg_data(cb->nlh);
646 	ops = ethnl_default_requests[ghdr->cmd];
647 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
648 		return -EOPNOTSUPP;
649 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
650 	if (!req_info)
651 		return -ENOMEM;
652 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
653 	if (!reply_data) {
654 		ret = -ENOMEM;
655 		goto free_req_info;
656 	}
657 
658 	ret = ethnl_default_parse(req_info, &info->info, ops, false);
659 	if (req_info->dev) {
660 		/* We ignore device specification in dump requests but as the
661 		 * same parser as for non-dump (doit) requests is used, it
662 		 * would take reference to the device if it finds one
663 		 */
664 		netdev_put(req_info->dev, &req_info->dev_tracker);
665 		req_info->dev = NULL;
666 	}
667 	if (ret < 0)
668 		goto free_reply_data;
669 
670 	ctx->ops = ops;
671 	ctx->req_info = req_info;
672 	ctx->reply_data = reply_data;
673 	ctx->pos_ifindex = 0;
674 
675 	return 0;
676 
677 free_reply_data:
678 	kfree(reply_data);
679 free_req_info:
680 	kfree(req_info);
681 
682 	return ret;
683 }
684 
685 /* per-PHY ->start() handler for GET requests */
686 static int ethnl_perphy_start(struct netlink_callback *cb)
687 {
688 	struct ethnl_perphy_dump_ctx *phy_ctx = ethnl_perphy_dump_context(cb);
689 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
690 	struct ethnl_dump_ctx *ctx = &phy_ctx->ethnl_ctx;
691 	struct ethnl_reply_data *reply_data;
692 	const struct ethnl_request_ops *ops;
693 	struct ethnl_req_info *req_info;
694 	struct genlmsghdr *ghdr;
695 	int ret;
696 
697 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
698 
699 	ghdr = nlmsg_data(cb->nlh);
700 	ops = ethnl_default_requests[ghdr->cmd];
701 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
702 		return -EOPNOTSUPP;
703 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
704 	if (!req_info)
705 		return -ENOMEM;
706 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
707 	if (!reply_data) {
708 		ret = -ENOMEM;
709 		goto free_req_info;
710 	}
711 
712 	/* Unlike per-dev dump, don't ignore dev. The dump handler
713 	 * will notice it and dump PHYs from given dev. We only keep track of
714 	 * the dev's ifindex, .dumpit() will grab and release the netdev itself.
715 	 */
716 	ret = ethnl_default_parse(req_info, &info->info, ops, false);
717 	if (req_info->dev) {
718 		phy_ctx->ifindex = req_info->dev->ifindex;
719 		netdev_put(req_info->dev, &req_info->dev_tracker);
720 		req_info->dev = NULL;
721 	}
722 	if (ret < 0)
723 		goto free_reply_data;
724 
725 	ctx->ops = ops;
726 	ctx->req_info = req_info;
727 	ctx->reply_data = reply_data;
728 	ctx->pos_ifindex = 0;
729 
730 	return 0;
731 
732 free_reply_data:
733 	kfree(reply_data);
734 free_req_info:
735 	kfree(req_info);
736 
737 	return ret;
738 }
739 
740 static int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
741 				     struct ethnl_perphy_dump_ctx *ctx,
742 				     const struct genl_info *info)
743 {
744 	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
745 	struct net_device *dev = ethnl_ctx->req_info->dev;
746 	struct phy_device_node *pdn;
747 	int ret;
748 
749 	if (!dev->link_topo)
750 		return 0;
751 
752 	xa_for_each_start(&dev->link_topo->phys, ctx->pos_phyindex, pdn,
753 			  ctx->pos_phyindex) {
754 		ethnl_ctx->req_info->phy_index = ctx->pos_phyindex;
755 
756 		/* We can re-use the original dump_one as ->prepare_data in
757 		 * commands use ethnl_req_get_phydev(), which gets the PHY from
758 		 * the req_info->phy_index
759 		 */
760 		ret = ethnl_default_dump_one(skb, dev, ethnl_ctx, info);
761 		if (ret)
762 			return ret;
763 	}
764 
765 	ctx->pos_phyindex = 0;
766 
767 	return 0;
768 }
769 
770 static int ethnl_perphy_dump_all_dev(struct sk_buff *skb,
771 				     struct ethnl_perphy_dump_ctx *ctx,
772 				     const struct genl_info *info)
773 {
774 	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
775 	struct net *net = sock_net(skb->sk);
776 	netdevice_tracker dev_tracker;
777 	struct net_device *dev;
778 	int ret = 0;
779 
780 	rcu_read_lock();
781 	for_each_netdev_dump(net, dev, ethnl_ctx->pos_ifindex) {
782 		netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
783 		rcu_read_unlock();
784 
785 		/* per-PHY commands use ethnl_req_get_phydev(), which needs the
786 		 * net_device in the req_info
787 		 */
788 		ethnl_ctx->req_info->dev = dev;
789 		ret = ethnl_perphy_dump_one_dev(skb, ctx, info);
790 
791 		rcu_read_lock();
792 		netdev_put(dev, &dev_tracker);
793 		ethnl_ctx->req_info->dev = NULL;
794 
795 		if (ret < 0 && ret != -EOPNOTSUPP) {
796 			if (likely(skb->len))
797 				ret = skb->len;
798 			break;
799 		}
800 		ret = 0;
801 	}
802 	rcu_read_unlock();
803 
804 	return ret;
805 }
806 
807 /* per-PHY ->dumpit() handler for GET requests. */
808 static int ethnl_perphy_dumpit(struct sk_buff *skb,
809 			       struct netlink_callback *cb)
810 {
811 	struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb);
812 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
813 	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
814 	int ret = 0;
815 
816 	if (ctx->ifindex) {
817 		netdevice_tracker dev_tracker;
818 		struct net_device *dev;
819 
820 		dev = netdev_get_by_index(genl_info_net(&info->info),
821 					  ctx->ifindex, &dev_tracker,
822 					  GFP_KERNEL);
823 		if (!dev)
824 			return -ENODEV;
825 
826 		ethnl_ctx->req_info->dev = dev;
827 		ret = ethnl_perphy_dump_one_dev(skb, ctx, genl_info_dump(cb));
828 
829 		if (ret < 0 && ret != -EOPNOTSUPP && likely(skb->len))
830 			ret = skb->len;
831 
832 		netdev_put(dev, &dev_tracker);
833 	} else {
834 		ret = ethnl_perphy_dump_all_dev(skb, ctx, genl_info_dump(cb));
835 	}
836 
837 	return ret;
838 }
839 
840 /* per-PHY ->done() handler for GET requests */
841 static int ethnl_perphy_done(struct netlink_callback *cb)
842 {
843 	struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb);
844 	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
845 
846 	kfree(ethnl_ctx->reply_data);
847 	kfree(ethnl_ctx->req_info);
848 
849 	return 0;
850 }
851 
852 /* default ->done() handler for GET requests */
853 static int ethnl_default_done(struct netlink_callback *cb)
854 {
855 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
856 
857 	kfree(ctx->reply_data);
858 	kfree(ctx->req_info);
859 
860 	return 0;
861 }
862 
863 static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)
864 {
865 	const struct ethnl_request_ops *ops;
866 	const u8 cmd = info->genlhdr->cmd;
867 	struct ethnl_req_info *req_info;
868 	struct net_device *dev;
869 	int ret;
870 
871 	ops = ethnl_default_requests[cmd];
872 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
873 		return -EOPNOTSUPP;
874 	if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
875 		return -EINVAL;
876 
877 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
878 	if (!req_info)
879 		return -ENOMEM;
880 
881 	ret = ethnl_default_parse(req_info, info,  ops, true);
882 	if (ret < 0)
883 		goto out_free_req;
884 
885 	if (ops->set_validate) {
886 		ret = ops->set_validate(req_info, info);
887 		/* 0 means nothing to do */
888 		if (ret <= 0)
889 			goto out_dev;
890 	}
891 
892 	dev = req_info->dev;
893 
894 	rtnl_lock();
895 	netdev_lock_ops(dev);
896 	dev->cfg_pending = kmemdup(dev->cfg, sizeof(*dev->cfg),
897 				   GFP_KERNEL_ACCOUNT);
898 	if (!dev->cfg_pending) {
899 		ret = -ENOMEM;
900 		goto out_tie_cfg;
901 	}
902 
903 	ret = ethnl_ops_begin(dev);
904 	if (ret < 0)
905 		goto out_free_cfg;
906 
907 	ret = ops->set(req_info, info);
908 	if (ret < 0)
909 		goto out_ops;
910 
911 	swap(dev->cfg, dev->cfg_pending);
912 	if (!ret)
913 		goto out_ops;
914 	ethnl_notify(dev, ops->set_ntf_cmd, req_info);
915 
916 	ret = 0;
917 out_ops:
918 	ethnl_ops_complete(dev);
919 out_free_cfg:
920 	kfree(dev->cfg_pending);
921 out_tie_cfg:
922 	dev->cfg_pending = dev->cfg;
923 	netdev_unlock_ops(dev);
924 	rtnl_unlock();
925 out_dev:
926 	ethnl_parse_header_dev_put(req_info);
927 out_free_req:
928 	kfree(req_info);
929 	return ret;
930 }
931 
932 static const struct ethnl_request_ops *
933 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
934 	[ETHTOOL_MSG_LINKINFO_NTF]	= &ethnl_linkinfo_request_ops,
935 	[ETHTOOL_MSG_LINKMODES_NTF]	= &ethnl_linkmodes_request_ops,
936 	[ETHTOOL_MSG_DEBUG_NTF]		= &ethnl_debug_request_ops,
937 	[ETHTOOL_MSG_WOL_NTF]		= &ethnl_wol_request_ops,
938 	[ETHTOOL_MSG_FEATURES_NTF]	= &ethnl_features_request_ops,
939 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= &ethnl_privflags_request_ops,
940 	[ETHTOOL_MSG_RINGS_NTF]		= &ethnl_rings_request_ops,
941 	[ETHTOOL_MSG_CHANNELS_NTF]	= &ethnl_channels_request_ops,
942 	[ETHTOOL_MSG_COALESCE_NTF]	= &ethnl_coalesce_request_ops,
943 	[ETHTOOL_MSG_PAUSE_NTF]		= &ethnl_pause_request_ops,
944 	[ETHTOOL_MSG_EEE_NTF]		= &ethnl_eee_request_ops,
945 	[ETHTOOL_MSG_FEC_NTF]		= &ethnl_fec_request_ops,
946 	[ETHTOOL_MSG_MODULE_NTF]	= &ethnl_module_request_ops,
947 	[ETHTOOL_MSG_PLCA_NTF]		= &ethnl_plca_cfg_request_ops,
948 	[ETHTOOL_MSG_MM_NTF]		= &ethnl_mm_request_ops,
949 	[ETHTOOL_MSG_RSS_NTF]		= &ethnl_rss_request_ops,
950 };
951 
952 /* default notification handler */
953 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
954 				 const struct ethnl_req_info *orig_req_info)
955 {
956 	struct ethnl_reply_data *reply_data;
957 	const struct ethnl_request_ops *ops;
958 	struct ethnl_req_info *req_info;
959 	struct genl_info info;
960 	struct sk_buff *skb;
961 	void *reply_payload;
962 	int reply_len;
963 	int ret;
964 
965 	genl_info_init_ntf(&info, &ethtool_genl_family, cmd);
966 
967 	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
968 		      !ethnl_default_notify_ops[cmd],
969 		      "unexpected notification type %u\n", cmd))
970 		return;
971 	ops = ethnl_default_notify_ops[cmd];
972 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
973 	if (!req_info)
974 		return;
975 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
976 	if (!reply_data) {
977 		kfree(req_info);
978 		return;
979 	}
980 
981 	req_info->dev = dev;
982 	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
983 	if (orig_req_info) {
984 		req_info->phy_index = orig_req_info->phy_index;
985 		memcpy(&req_info[1], &orig_req_info[1],
986 		       ops->req_info_size - sizeof(*req_info));
987 	}
988 
989 	netdev_ops_assert_locked(dev);
990 
991 	ethnl_init_reply_data(reply_data, ops, dev);
992 	ret = ops->prepare_data(req_info, reply_data, &info);
993 	if (ret < 0)
994 		goto err_rep;
995 	ret = ops->reply_size(req_info, reply_data);
996 	if (ret < 0)
997 		goto err_cleanup;
998 	reply_len = ret + ethnl_reply_header_size();
999 	skb = genlmsg_new(reply_len, GFP_KERNEL);
1000 	if (!skb)
1001 		goto err_cleanup;
1002 	reply_payload = ethnl_bcastmsg_put(skb, cmd);
1003 	if (!reply_payload)
1004 		goto err_skb;
1005 	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
1006 	if (ret < 0)
1007 		goto err_msg;
1008 	ret = ops->fill_reply(skb, req_info, reply_data);
1009 	if (ret < 0)
1010 		goto err_msg;
1011 	if (ops->cleanup_data)
1012 		ops->cleanup_data(reply_data);
1013 
1014 	genlmsg_end(skb, reply_payload);
1015 	kfree(reply_data);
1016 	kfree(req_info);
1017 	ethnl_multicast(skb, dev);
1018 	return;
1019 
1020 err_msg:
1021 	WARN_ONCE(ret == -EMSGSIZE,
1022 		  "calculated message payload length (%d) not sufficient\n",
1023 		  reply_len);
1024 err_skb:
1025 	nlmsg_free(skb);
1026 err_cleanup:
1027 	if (ops->cleanup_data)
1028 		ops->cleanup_data(reply_data);
1029 err_rep:
1030 	kfree(reply_data);
1031 	kfree(req_info);
1032 	return;
1033 }
1034 
1035 /* notifications */
1036 
1037 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
1038 				       const struct ethnl_req_info *req_info);
1039 
1040 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
1041 	[ETHTOOL_MSG_LINKINFO_NTF]	= ethnl_default_notify,
1042 	[ETHTOOL_MSG_LINKMODES_NTF]	= ethnl_default_notify,
1043 	[ETHTOOL_MSG_DEBUG_NTF]		= ethnl_default_notify,
1044 	[ETHTOOL_MSG_WOL_NTF]		= ethnl_default_notify,
1045 	[ETHTOOL_MSG_FEATURES_NTF]	= ethnl_default_notify,
1046 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= ethnl_default_notify,
1047 	[ETHTOOL_MSG_RINGS_NTF]		= ethnl_default_notify,
1048 	[ETHTOOL_MSG_CHANNELS_NTF]	= ethnl_default_notify,
1049 	[ETHTOOL_MSG_COALESCE_NTF]	= ethnl_default_notify,
1050 	[ETHTOOL_MSG_PAUSE_NTF]		= ethnl_default_notify,
1051 	[ETHTOOL_MSG_EEE_NTF]		= ethnl_default_notify,
1052 	[ETHTOOL_MSG_FEC_NTF]		= ethnl_default_notify,
1053 	[ETHTOOL_MSG_MODULE_NTF]	= ethnl_default_notify,
1054 	[ETHTOOL_MSG_PLCA_NTF]		= ethnl_default_notify,
1055 	[ETHTOOL_MSG_MM_NTF]		= ethnl_default_notify,
1056 	[ETHTOOL_MSG_RSS_NTF]		= ethnl_default_notify,
1057 };
1058 
1059 void ethnl_notify(struct net_device *dev, unsigned int cmd,
1060 		  const struct ethnl_req_info *req_info)
1061 {
1062 	if (unlikely(!ethnl_ok))
1063 		return;
1064 	ASSERT_RTNL();
1065 
1066 	if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
1067 		   ethnl_notify_handlers[cmd]))
1068 		ethnl_notify_handlers[cmd](dev, cmd, req_info);
1069 	else
1070 		WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
1071 			  cmd, netdev_name(dev));
1072 }
1073 
1074 void ethtool_notify(struct net_device *dev, unsigned int cmd)
1075 {
1076 	ethnl_notify(dev, cmd, NULL);
1077 }
1078 EXPORT_SYMBOL(ethtool_notify);
1079 
1080 static void ethnl_notify_features(struct netdev_notifier_info *info)
1081 {
1082 	struct net_device *dev = netdev_notifier_info_to_dev(info);
1083 
1084 	ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF);
1085 }
1086 
1087 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
1088 			      void *ptr)
1089 {
1090 	struct netdev_notifier_info *info = ptr;
1091 	struct netlink_ext_ack *extack;
1092 	struct net_device *dev;
1093 
1094 	dev = netdev_notifier_info_to_dev(info);
1095 	extack = netdev_notifier_info_to_extack(info);
1096 
1097 	switch (event) {
1098 	case NETDEV_FEAT_CHANGE:
1099 		ethnl_notify_features(ptr);
1100 		break;
1101 	case NETDEV_PRE_UP:
1102 		if (dev->ethtool->module_fw_flash_in_progress) {
1103 			NL_SET_ERR_MSG(extack, "Can't set port up while flashing module firmware");
1104 			return NOTIFY_BAD;
1105 		}
1106 	}
1107 
1108 	return NOTIFY_DONE;
1109 }
1110 
1111 static struct notifier_block ethnl_netdev_notifier = {
1112 	.notifier_call = ethnl_netdev_event,
1113 };
1114 
1115 /* genetlink setup */
1116 
1117 static const struct genl_ops ethtool_genl_ops[] = {
1118 	{
1119 		.cmd	= ETHTOOL_MSG_STRSET_GET,
1120 		.doit	= ethnl_default_doit,
1121 		.start	= ethnl_default_start,
1122 		.dumpit	= ethnl_default_dumpit,
1123 		.done	= ethnl_default_done,
1124 		.policy = ethnl_strset_get_policy,
1125 		.maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
1126 	},
1127 	{
1128 		.cmd	= ETHTOOL_MSG_LINKINFO_GET,
1129 		.doit	= ethnl_default_doit,
1130 		.start	= ethnl_default_start,
1131 		.dumpit	= ethnl_default_dumpit,
1132 		.done	= ethnl_default_done,
1133 		.policy = ethnl_linkinfo_get_policy,
1134 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
1135 	},
1136 	{
1137 		.cmd	= ETHTOOL_MSG_LINKINFO_SET,
1138 		.flags	= GENL_UNS_ADMIN_PERM,
1139 		.doit	= ethnl_default_set_doit,
1140 		.policy = ethnl_linkinfo_set_policy,
1141 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
1142 	},
1143 	{
1144 		.cmd	= ETHTOOL_MSG_LINKMODES_GET,
1145 		.doit	= ethnl_default_doit,
1146 		.start	= ethnl_default_start,
1147 		.dumpit	= ethnl_default_dumpit,
1148 		.done	= ethnl_default_done,
1149 		.policy = ethnl_linkmodes_get_policy,
1150 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
1151 	},
1152 	{
1153 		.cmd	= ETHTOOL_MSG_LINKMODES_SET,
1154 		.flags	= GENL_UNS_ADMIN_PERM,
1155 		.doit	= ethnl_default_set_doit,
1156 		.policy = ethnl_linkmodes_set_policy,
1157 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
1158 	},
1159 	{
1160 		.cmd	= ETHTOOL_MSG_LINKSTATE_GET,
1161 		.doit	= ethnl_default_doit,
1162 		.start	= ethnl_default_start,
1163 		.dumpit	= ethnl_default_dumpit,
1164 		.done	= ethnl_default_done,
1165 		.policy = ethnl_linkstate_get_policy,
1166 		.maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
1167 	},
1168 	{
1169 		.cmd	= ETHTOOL_MSG_DEBUG_GET,
1170 		.doit	= ethnl_default_doit,
1171 		.start	= ethnl_default_start,
1172 		.dumpit	= ethnl_default_dumpit,
1173 		.done	= ethnl_default_done,
1174 		.policy = ethnl_debug_get_policy,
1175 		.maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
1176 	},
1177 	{
1178 		.cmd	= ETHTOOL_MSG_DEBUG_SET,
1179 		.flags	= GENL_UNS_ADMIN_PERM,
1180 		.doit	= ethnl_default_set_doit,
1181 		.policy = ethnl_debug_set_policy,
1182 		.maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
1183 	},
1184 	{
1185 		.cmd	= ETHTOOL_MSG_WOL_GET,
1186 		.flags	= GENL_UNS_ADMIN_PERM,
1187 		.doit	= ethnl_default_doit,
1188 		.start	= ethnl_default_start,
1189 		.dumpit	= ethnl_default_dumpit,
1190 		.done	= ethnl_default_done,
1191 		.policy = ethnl_wol_get_policy,
1192 		.maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
1193 	},
1194 	{
1195 		.cmd	= ETHTOOL_MSG_WOL_SET,
1196 		.flags	= GENL_UNS_ADMIN_PERM,
1197 		.doit	= ethnl_default_set_doit,
1198 		.policy = ethnl_wol_set_policy,
1199 		.maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
1200 	},
1201 	{
1202 		.cmd	= ETHTOOL_MSG_FEATURES_GET,
1203 		.doit	= ethnl_default_doit,
1204 		.start	= ethnl_default_start,
1205 		.dumpit	= ethnl_default_dumpit,
1206 		.done	= ethnl_default_done,
1207 		.policy = ethnl_features_get_policy,
1208 		.maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
1209 	},
1210 	{
1211 		.cmd	= ETHTOOL_MSG_FEATURES_SET,
1212 		.flags	= GENL_UNS_ADMIN_PERM,
1213 		.doit	= ethnl_set_features,
1214 		.policy = ethnl_features_set_policy,
1215 		.maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
1216 	},
1217 	{
1218 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_GET,
1219 		.doit	= ethnl_default_doit,
1220 		.start	= ethnl_default_start,
1221 		.dumpit	= ethnl_default_dumpit,
1222 		.done	= ethnl_default_done,
1223 		.policy = ethnl_privflags_get_policy,
1224 		.maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
1225 	},
1226 	{
1227 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_SET,
1228 		.flags	= GENL_UNS_ADMIN_PERM,
1229 		.doit	= ethnl_default_set_doit,
1230 		.policy = ethnl_privflags_set_policy,
1231 		.maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
1232 	},
1233 	{
1234 		.cmd	= ETHTOOL_MSG_RINGS_GET,
1235 		.doit	= ethnl_default_doit,
1236 		.start	= ethnl_default_start,
1237 		.dumpit	= ethnl_default_dumpit,
1238 		.done	= ethnl_default_done,
1239 		.policy = ethnl_rings_get_policy,
1240 		.maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
1241 	},
1242 	{
1243 		.cmd	= ETHTOOL_MSG_RINGS_SET,
1244 		.flags	= GENL_UNS_ADMIN_PERM,
1245 		.doit	= ethnl_default_set_doit,
1246 		.policy = ethnl_rings_set_policy,
1247 		.maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
1248 	},
1249 	{
1250 		.cmd	= ETHTOOL_MSG_CHANNELS_GET,
1251 		.doit	= ethnl_default_doit,
1252 		.start	= ethnl_default_start,
1253 		.dumpit	= ethnl_default_dumpit,
1254 		.done	= ethnl_default_done,
1255 		.policy = ethnl_channels_get_policy,
1256 		.maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
1257 	},
1258 	{
1259 		.cmd	= ETHTOOL_MSG_CHANNELS_SET,
1260 		.flags	= GENL_UNS_ADMIN_PERM,
1261 		.doit	= ethnl_default_set_doit,
1262 		.policy = ethnl_channels_set_policy,
1263 		.maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
1264 	},
1265 	{
1266 		.cmd	= ETHTOOL_MSG_COALESCE_GET,
1267 		.doit	= ethnl_default_doit,
1268 		.start	= ethnl_default_start,
1269 		.dumpit	= ethnl_default_dumpit,
1270 		.done	= ethnl_default_done,
1271 		.policy = ethnl_coalesce_get_policy,
1272 		.maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
1273 	},
1274 	{
1275 		.cmd	= ETHTOOL_MSG_COALESCE_SET,
1276 		.flags	= GENL_UNS_ADMIN_PERM,
1277 		.doit	= ethnl_default_set_doit,
1278 		.policy = ethnl_coalesce_set_policy,
1279 		.maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
1280 	},
1281 	{
1282 		.cmd	= ETHTOOL_MSG_PAUSE_GET,
1283 		.doit	= ethnl_default_doit,
1284 		.start	= ethnl_default_start,
1285 		.dumpit	= ethnl_default_dumpit,
1286 		.done	= ethnl_default_done,
1287 		.policy = ethnl_pause_get_policy,
1288 		.maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
1289 	},
1290 	{
1291 		.cmd	= ETHTOOL_MSG_PAUSE_SET,
1292 		.flags	= GENL_UNS_ADMIN_PERM,
1293 		.doit	= ethnl_default_set_doit,
1294 		.policy = ethnl_pause_set_policy,
1295 		.maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
1296 	},
1297 	{
1298 		.cmd	= ETHTOOL_MSG_EEE_GET,
1299 		.doit	= ethnl_default_doit,
1300 		.start	= ethnl_default_start,
1301 		.dumpit	= ethnl_default_dumpit,
1302 		.done	= ethnl_default_done,
1303 		.policy = ethnl_eee_get_policy,
1304 		.maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
1305 	},
1306 	{
1307 		.cmd	= ETHTOOL_MSG_EEE_SET,
1308 		.flags	= GENL_UNS_ADMIN_PERM,
1309 		.doit	= ethnl_default_set_doit,
1310 		.policy = ethnl_eee_set_policy,
1311 		.maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
1312 	},
1313 	{
1314 		.cmd	= ETHTOOL_MSG_TSINFO_GET,
1315 		.doit	= ethnl_default_doit,
1316 		.start	= ethnl_tsinfo_start,
1317 		.dumpit	= ethnl_tsinfo_dumpit,
1318 		.done	= ethnl_tsinfo_done,
1319 		.policy = ethnl_tsinfo_get_policy,
1320 		.maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
1321 	},
1322 	{
1323 		.cmd	= ETHTOOL_MSG_CABLE_TEST_ACT,
1324 		.flags	= GENL_UNS_ADMIN_PERM,
1325 		.doit	= ethnl_act_cable_test,
1326 		.policy = ethnl_cable_test_act_policy,
1327 		.maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
1328 	},
1329 	{
1330 		.cmd	= ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
1331 		.flags	= GENL_UNS_ADMIN_PERM,
1332 		.doit	= ethnl_act_cable_test_tdr,
1333 		.policy = ethnl_cable_test_tdr_act_policy,
1334 		.maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
1335 	},
1336 	{
1337 		.cmd	= ETHTOOL_MSG_TUNNEL_INFO_GET,
1338 		.doit	= ethnl_tunnel_info_doit,
1339 		.start	= ethnl_tunnel_info_start,
1340 		.dumpit	= ethnl_tunnel_info_dumpit,
1341 		.policy = ethnl_tunnel_info_get_policy,
1342 		.maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
1343 	},
1344 	{
1345 		.cmd	= ETHTOOL_MSG_FEC_GET,
1346 		.doit	= ethnl_default_doit,
1347 		.start	= ethnl_default_start,
1348 		.dumpit	= ethnl_default_dumpit,
1349 		.done	= ethnl_default_done,
1350 		.policy = ethnl_fec_get_policy,
1351 		.maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
1352 	},
1353 	{
1354 		.cmd	= ETHTOOL_MSG_FEC_SET,
1355 		.flags	= GENL_UNS_ADMIN_PERM,
1356 		.doit	= ethnl_default_set_doit,
1357 		.policy = ethnl_fec_set_policy,
1358 		.maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
1359 	},
1360 	{
1361 		.cmd	= ETHTOOL_MSG_MODULE_EEPROM_GET,
1362 		.flags  = GENL_UNS_ADMIN_PERM,
1363 		.doit	= ethnl_default_doit,
1364 		.start	= ethnl_default_start,
1365 		.dumpit	= ethnl_default_dumpit,
1366 		.done	= ethnl_default_done,
1367 		.policy = ethnl_module_eeprom_get_policy,
1368 		.maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
1369 	},
1370 	{
1371 		.cmd	= ETHTOOL_MSG_STATS_GET,
1372 		.doit	= ethnl_default_doit,
1373 		.start	= ethnl_default_start,
1374 		.dumpit	= ethnl_default_dumpit,
1375 		.done	= ethnl_default_done,
1376 		.policy = ethnl_stats_get_policy,
1377 		.maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
1378 	},
1379 	{
1380 		.cmd	= ETHTOOL_MSG_PHC_VCLOCKS_GET,
1381 		.doit	= ethnl_default_doit,
1382 		.start	= ethnl_default_start,
1383 		.dumpit	= ethnl_default_dumpit,
1384 		.done	= ethnl_default_done,
1385 		.policy = ethnl_phc_vclocks_get_policy,
1386 		.maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
1387 	},
1388 	{
1389 		.cmd	= ETHTOOL_MSG_MODULE_GET,
1390 		.doit	= ethnl_default_doit,
1391 		.start	= ethnl_default_start,
1392 		.dumpit	= ethnl_default_dumpit,
1393 		.done	= ethnl_default_done,
1394 		.policy = ethnl_module_get_policy,
1395 		.maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1,
1396 	},
1397 	{
1398 		.cmd	= ETHTOOL_MSG_MODULE_SET,
1399 		.flags	= GENL_UNS_ADMIN_PERM,
1400 		.doit	= ethnl_default_set_doit,
1401 		.policy = ethnl_module_set_policy,
1402 		.maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1,
1403 	},
1404 	{
1405 		.cmd	= ETHTOOL_MSG_PSE_GET,
1406 		.doit	= ethnl_default_doit,
1407 		.start	= ethnl_perphy_start,
1408 		.dumpit	= ethnl_perphy_dumpit,
1409 		.done	= ethnl_perphy_done,
1410 		.policy = ethnl_pse_get_policy,
1411 		.maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
1412 	},
1413 	{
1414 		.cmd	= ETHTOOL_MSG_PSE_SET,
1415 		.flags	= GENL_UNS_ADMIN_PERM,
1416 		.doit	= ethnl_default_set_doit,
1417 		.policy = ethnl_pse_set_policy,
1418 		.maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1,
1419 	},
1420 	{
1421 		.cmd	= ETHTOOL_MSG_RSS_GET,
1422 		.doit	= ethnl_default_doit,
1423 		.start	= ethnl_rss_dump_start,
1424 		.dumpit	= ethnl_rss_dumpit,
1425 		.policy = ethnl_rss_get_policy,
1426 		.maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1,
1427 	},
1428 	{
1429 		.cmd	= ETHTOOL_MSG_PLCA_GET_CFG,
1430 		.doit	= ethnl_default_doit,
1431 		.start	= ethnl_perphy_start,
1432 		.dumpit	= ethnl_perphy_dumpit,
1433 		.done	= ethnl_perphy_done,
1434 		.policy = ethnl_plca_get_cfg_policy,
1435 		.maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
1436 	},
1437 	{
1438 		.cmd	= ETHTOOL_MSG_PLCA_SET_CFG,
1439 		.flags	= GENL_UNS_ADMIN_PERM,
1440 		.doit	= ethnl_default_set_doit,
1441 		.policy = ethnl_plca_set_cfg_policy,
1442 		.maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1,
1443 	},
1444 	{
1445 		.cmd	= ETHTOOL_MSG_PLCA_GET_STATUS,
1446 		.doit	= ethnl_default_doit,
1447 		.start	= ethnl_perphy_start,
1448 		.dumpit	= ethnl_perphy_dumpit,
1449 		.done	= ethnl_perphy_done,
1450 		.policy = ethnl_plca_get_status_policy,
1451 		.maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
1452 	},
1453 	{
1454 		.cmd	= ETHTOOL_MSG_MM_GET,
1455 		.doit	= ethnl_default_doit,
1456 		.start	= ethnl_default_start,
1457 		.dumpit	= ethnl_default_dumpit,
1458 		.done	= ethnl_default_done,
1459 		.policy = ethnl_mm_get_policy,
1460 		.maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1,
1461 	},
1462 	{
1463 		.cmd	= ETHTOOL_MSG_MM_SET,
1464 		.flags	= GENL_UNS_ADMIN_PERM,
1465 		.doit	= ethnl_default_set_doit,
1466 		.policy = ethnl_mm_set_policy,
1467 		.maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1,
1468 	},
1469 	{
1470 		.cmd	= ETHTOOL_MSG_MODULE_FW_FLASH_ACT,
1471 		.flags	= GENL_UNS_ADMIN_PERM,
1472 		.doit	= ethnl_act_module_fw_flash,
1473 		.policy	= ethnl_module_fw_flash_act_policy,
1474 		.maxattr = ARRAY_SIZE(ethnl_module_fw_flash_act_policy) - 1,
1475 	},
1476 	{
1477 		.cmd	= ETHTOOL_MSG_PHY_GET,
1478 		.doit	= ethnl_default_doit,
1479 		.start	= ethnl_perphy_start,
1480 		.dumpit	= ethnl_perphy_dumpit,
1481 		.done	= ethnl_perphy_done,
1482 		.policy = ethnl_phy_get_policy,
1483 		.maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1,
1484 	},
1485 	{
1486 		.cmd	= ETHTOOL_MSG_TSCONFIG_GET,
1487 		.doit	= ethnl_default_doit,
1488 		.start	= ethnl_default_start,
1489 		.dumpit	= ethnl_default_dumpit,
1490 		.done	= ethnl_default_done,
1491 		.policy = ethnl_tsconfig_get_policy,
1492 		.maxattr = ARRAY_SIZE(ethnl_tsconfig_get_policy) - 1,
1493 	},
1494 	{
1495 		.cmd	= ETHTOOL_MSG_TSCONFIG_SET,
1496 		.flags	= GENL_UNS_ADMIN_PERM,
1497 		.doit	= ethnl_default_set_doit,
1498 		.policy = ethnl_tsconfig_set_policy,
1499 		.maxattr = ARRAY_SIZE(ethnl_tsconfig_set_policy) - 1,
1500 	},
1501 };
1502 
1503 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
1504 	[ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
1505 };
1506 
1507 static struct genl_family ethtool_genl_family __ro_after_init = {
1508 	.name		= ETHTOOL_GENL_NAME,
1509 	.version	= ETHTOOL_GENL_VERSION,
1510 	.netnsok	= true,
1511 	.parallel_ops	= true,
1512 	.ops		= ethtool_genl_ops,
1513 	.n_ops		= ARRAY_SIZE(ethtool_genl_ops),
1514 	.resv_start_op	= ETHTOOL_MSG_MODULE_GET + 1,
1515 	.mcgrps		= ethtool_nl_mcgrps,
1516 	.n_mcgrps	= ARRAY_SIZE(ethtool_nl_mcgrps),
1517 	.sock_priv_size		= sizeof(struct ethnl_sock_priv),
1518 	.sock_priv_destroy	= ethnl_sock_priv_destroy,
1519 };
1520 
1521 /* module setup */
1522 
1523 static int __init ethnl_init(void)
1524 {
1525 	int ret;
1526 
1527 	ret = genl_register_family(&ethtool_genl_family);
1528 	if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
1529 		return ret;
1530 	ethnl_ok = true;
1531 
1532 	ret = register_netdevice_notifier(&ethnl_netdev_notifier);
1533 	WARN(ret < 0, "ethtool: net device notifier registration failed");
1534 	return ret;
1535 }
1536 
1537 subsys_initcall(ethnl_init);
1538