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