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