xref: /linux/net/ethtool/netlink.c (revision f315296c92fd4b7716bdea17f727ab431891dc3b)
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_cleanup;
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;
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 	ctx->reply_data->dev = NULL;
564 	if (ret < 0)
565 		genlmsg_cancel(skb, ehdr);
566 	else
567 		genlmsg_end(skb, ehdr);
568 	return ret;
569 }
570 
571 /* Default ->dumpit() handler for GET requests. */
ethnl_default_dumpit(struct sk_buff * skb,struct netlink_callback * cb)572 static int ethnl_default_dumpit(struct sk_buff *skb,
573 				struct netlink_callback *cb)
574 {
575 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
576 	struct net *net = sock_net(skb->sk);
577 	struct net_device *dev;
578 	int ret = 0;
579 
580 	rcu_read_lock();
581 	for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
582 		dev_hold(dev);
583 		rcu_read_unlock();
584 
585 		ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
586 
587 		rcu_read_lock();
588 		dev_put(dev);
589 
590 		if (ret < 0 && ret != -EOPNOTSUPP) {
591 			if (likely(skb->len))
592 				ret = skb->len;
593 			break;
594 		}
595 		ret = 0;
596 	}
597 	rcu_read_unlock();
598 
599 	return ret;
600 }
601 
602 /* generic ->start() handler for GET requests */
ethnl_default_start(struct netlink_callback * cb)603 static int ethnl_default_start(struct netlink_callback *cb)
604 {
605 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
606 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
607 	struct ethnl_reply_data *reply_data;
608 	const struct ethnl_request_ops *ops;
609 	struct ethnl_req_info *req_info;
610 	struct genlmsghdr *ghdr;
611 	int ret;
612 
613 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
614 
615 	ghdr = nlmsg_data(cb->nlh);
616 	ops = ethnl_default_requests[ghdr->cmd];
617 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
618 		return -EOPNOTSUPP;
619 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
620 	if (!req_info)
621 		return -ENOMEM;
622 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
623 	if (!reply_data) {
624 		ret = -ENOMEM;
625 		goto free_req_info;
626 	}
627 
628 	ret = ethnl_default_parse(req_info, &info->info, ops, false);
629 	if (req_info->dev) {
630 		/* We ignore device specification in dump requests but as the
631 		 * same parser as for non-dump (doit) requests is used, it
632 		 * would take reference to the device if it finds one
633 		 */
634 		netdev_put(req_info->dev, &req_info->dev_tracker);
635 		req_info->dev = NULL;
636 	}
637 	if (ret < 0)
638 		goto free_reply_data;
639 
640 	ctx->ops = ops;
641 	ctx->req_info = req_info;
642 	ctx->reply_data = reply_data;
643 	ctx->pos_ifindex = 0;
644 
645 	return 0;
646 
647 free_reply_data:
648 	kfree(reply_data);
649 free_req_info:
650 	kfree(req_info);
651 
652 	return ret;
653 }
654 
655 /* default ->done() handler for GET requests */
ethnl_default_done(struct netlink_callback * cb)656 static int ethnl_default_done(struct netlink_callback *cb)
657 {
658 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
659 
660 	kfree(ctx->reply_data);
661 	kfree(ctx->req_info);
662 
663 	return 0;
664 }
665 
ethnl_default_set_doit(struct sk_buff * skb,struct genl_info * info)666 static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)
667 {
668 	const struct ethnl_request_ops *ops;
669 	struct ethnl_req_info req_info = {};
670 	const u8 cmd = info->genlhdr->cmd;
671 	struct net_device *dev;
672 	int ret;
673 
674 	ops = ethnl_default_requests[cmd];
675 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
676 		return -EOPNOTSUPP;
677 	if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
678 		return -EINVAL;
679 
680 	ret = ethnl_parse_header_dev_get(&req_info, info->attrs[ops->hdr_attr],
681 					 genl_info_net(info), info->extack,
682 					 true);
683 	if (ret < 0)
684 		return ret;
685 
686 	if (ops->set_validate) {
687 		ret = ops->set_validate(&req_info, info);
688 		/* 0 means nothing to do */
689 		if (ret <= 0)
690 			goto out_dev;
691 	}
692 
693 	dev = req_info.dev;
694 
695 	rtnl_lock();
696 	dev->cfg_pending = kmemdup(dev->cfg, sizeof(*dev->cfg),
697 				   GFP_KERNEL_ACCOUNT);
698 	if (!dev->cfg_pending) {
699 		ret = -ENOMEM;
700 		goto out_tie_cfg;
701 	}
702 
703 	ret = ethnl_ops_begin(dev);
704 	if (ret < 0)
705 		goto out_free_cfg;
706 
707 	ret = ops->set(&req_info, info);
708 	if (ret < 0)
709 		goto out_ops;
710 
711 	swap(dev->cfg, dev->cfg_pending);
712 	if (!ret)
713 		goto out_ops;
714 	ethtool_notify(dev, ops->set_ntf_cmd, NULL);
715 
716 	ret = 0;
717 out_ops:
718 	ethnl_ops_complete(dev);
719 out_free_cfg:
720 	kfree(dev->cfg_pending);
721 out_tie_cfg:
722 	dev->cfg_pending = dev->cfg;
723 	rtnl_unlock();
724 out_dev:
725 	ethnl_parse_header_dev_put(&req_info);
726 	return ret;
727 }
728 
729 static const struct ethnl_request_ops *
730 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
731 	[ETHTOOL_MSG_LINKINFO_NTF]	= &ethnl_linkinfo_request_ops,
732 	[ETHTOOL_MSG_LINKMODES_NTF]	= &ethnl_linkmodes_request_ops,
733 	[ETHTOOL_MSG_DEBUG_NTF]		= &ethnl_debug_request_ops,
734 	[ETHTOOL_MSG_WOL_NTF]		= &ethnl_wol_request_ops,
735 	[ETHTOOL_MSG_FEATURES_NTF]	= &ethnl_features_request_ops,
736 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= &ethnl_privflags_request_ops,
737 	[ETHTOOL_MSG_RINGS_NTF]		= &ethnl_rings_request_ops,
738 	[ETHTOOL_MSG_CHANNELS_NTF]	= &ethnl_channels_request_ops,
739 	[ETHTOOL_MSG_COALESCE_NTF]	= &ethnl_coalesce_request_ops,
740 	[ETHTOOL_MSG_PAUSE_NTF]		= &ethnl_pause_request_ops,
741 	[ETHTOOL_MSG_EEE_NTF]		= &ethnl_eee_request_ops,
742 	[ETHTOOL_MSG_FEC_NTF]		= &ethnl_fec_request_ops,
743 	[ETHTOOL_MSG_MODULE_NTF]	= &ethnl_module_request_ops,
744 	[ETHTOOL_MSG_PLCA_NTF]		= &ethnl_plca_cfg_request_ops,
745 	[ETHTOOL_MSG_MM_NTF]		= &ethnl_mm_request_ops,
746 };
747 
748 /* default notification handler */
ethnl_default_notify(struct net_device * dev,unsigned int cmd,const void * data)749 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
750 				 const void *data)
751 {
752 	struct ethnl_reply_data *reply_data;
753 	const struct ethnl_request_ops *ops;
754 	struct ethnl_req_info *req_info;
755 	struct genl_info info;
756 	struct sk_buff *skb;
757 	void *reply_payload;
758 	int reply_len;
759 	int ret;
760 
761 	genl_info_init_ntf(&info, &ethtool_genl_family, cmd);
762 
763 	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
764 		      !ethnl_default_notify_ops[cmd],
765 		      "unexpected notification type %u\n", cmd))
766 		return;
767 	ops = ethnl_default_notify_ops[cmd];
768 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
769 	if (!req_info)
770 		return;
771 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
772 	if (!reply_data) {
773 		kfree(req_info);
774 		return;
775 	}
776 
777 	req_info->dev = dev;
778 	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
779 
780 	ethnl_init_reply_data(reply_data, ops, dev);
781 	ret = ops->prepare_data(req_info, reply_data, &info);
782 	if (ret < 0)
783 		goto err_cleanup;
784 	ret = ops->reply_size(req_info, reply_data);
785 	if (ret < 0)
786 		goto err_cleanup;
787 	reply_len = ret + ethnl_reply_header_size();
788 	skb = genlmsg_new(reply_len, GFP_KERNEL);
789 	if (!skb)
790 		goto err_cleanup;
791 	reply_payload = ethnl_bcastmsg_put(skb, cmd);
792 	if (!reply_payload)
793 		goto err_skb;
794 	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
795 	if (ret < 0)
796 		goto err_msg;
797 	ret = ops->fill_reply(skb, req_info, reply_data);
798 	if (ret < 0)
799 		goto err_msg;
800 	if (ops->cleanup_data)
801 		ops->cleanup_data(reply_data);
802 
803 	genlmsg_end(skb, reply_payload);
804 	kfree(reply_data);
805 	kfree(req_info);
806 	ethnl_multicast(skb, dev);
807 	return;
808 
809 err_msg:
810 	WARN_ONCE(ret == -EMSGSIZE,
811 		  "calculated message payload length (%d) not sufficient\n",
812 		  reply_len);
813 err_skb:
814 	nlmsg_free(skb);
815 err_cleanup:
816 	if (ops->cleanup_data)
817 		ops->cleanup_data(reply_data);
818 	kfree(reply_data);
819 	kfree(req_info);
820 	return;
821 }
822 
823 /* notifications */
824 
825 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
826 				       const void *data);
827 
828 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
829 	[ETHTOOL_MSG_LINKINFO_NTF]	= ethnl_default_notify,
830 	[ETHTOOL_MSG_LINKMODES_NTF]	= ethnl_default_notify,
831 	[ETHTOOL_MSG_DEBUG_NTF]		= ethnl_default_notify,
832 	[ETHTOOL_MSG_WOL_NTF]		= ethnl_default_notify,
833 	[ETHTOOL_MSG_FEATURES_NTF]	= ethnl_default_notify,
834 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= ethnl_default_notify,
835 	[ETHTOOL_MSG_RINGS_NTF]		= ethnl_default_notify,
836 	[ETHTOOL_MSG_CHANNELS_NTF]	= ethnl_default_notify,
837 	[ETHTOOL_MSG_COALESCE_NTF]	= ethnl_default_notify,
838 	[ETHTOOL_MSG_PAUSE_NTF]		= ethnl_default_notify,
839 	[ETHTOOL_MSG_EEE_NTF]		= ethnl_default_notify,
840 	[ETHTOOL_MSG_FEC_NTF]		= ethnl_default_notify,
841 	[ETHTOOL_MSG_MODULE_NTF]	= ethnl_default_notify,
842 	[ETHTOOL_MSG_PLCA_NTF]		= ethnl_default_notify,
843 	[ETHTOOL_MSG_MM_NTF]		= ethnl_default_notify,
844 };
845 
ethtool_notify(struct net_device * dev,unsigned int cmd,const void * data)846 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
847 {
848 	if (unlikely(!ethnl_ok))
849 		return;
850 	ASSERT_RTNL();
851 
852 	if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
853 		   ethnl_notify_handlers[cmd]))
854 		ethnl_notify_handlers[cmd](dev, cmd, data);
855 	else
856 		WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
857 			  cmd, netdev_name(dev));
858 }
859 EXPORT_SYMBOL(ethtool_notify);
860 
ethnl_notify_features(struct netdev_notifier_info * info)861 static void ethnl_notify_features(struct netdev_notifier_info *info)
862 {
863 	struct net_device *dev = netdev_notifier_info_to_dev(info);
864 
865 	ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
866 }
867 
ethnl_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)868 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
869 			      void *ptr)
870 {
871 	struct netdev_notifier_info *info = ptr;
872 	struct netlink_ext_ack *extack;
873 	struct net_device *dev;
874 
875 	dev = netdev_notifier_info_to_dev(info);
876 	extack = netdev_notifier_info_to_extack(info);
877 
878 	switch (event) {
879 	case NETDEV_FEAT_CHANGE:
880 		ethnl_notify_features(ptr);
881 		break;
882 	case NETDEV_PRE_UP:
883 		if (dev->ethtool->module_fw_flash_in_progress) {
884 			NL_SET_ERR_MSG(extack, "Can't set port up while flashing module firmware");
885 			return NOTIFY_BAD;
886 		}
887 	}
888 
889 	return NOTIFY_DONE;
890 }
891 
892 static struct notifier_block ethnl_netdev_notifier = {
893 	.notifier_call = ethnl_netdev_event,
894 };
895 
896 /* genetlink setup */
897 
898 static const struct genl_ops ethtool_genl_ops[] = {
899 	{
900 		.cmd	= ETHTOOL_MSG_STRSET_GET,
901 		.doit	= ethnl_default_doit,
902 		.start	= ethnl_default_start,
903 		.dumpit	= ethnl_default_dumpit,
904 		.done	= ethnl_default_done,
905 		.policy = ethnl_strset_get_policy,
906 		.maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
907 	},
908 	{
909 		.cmd	= ETHTOOL_MSG_LINKINFO_GET,
910 		.doit	= ethnl_default_doit,
911 		.start	= ethnl_default_start,
912 		.dumpit	= ethnl_default_dumpit,
913 		.done	= ethnl_default_done,
914 		.policy = ethnl_linkinfo_get_policy,
915 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
916 	},
917 	{
918 		.cmd	= ETHTOOL_MSG_LINKINFO_SET,
919 		.flags	= GENL_UNS_ADMIN_PERM,
920 		.doit	= ethnl_default_set_doit,
921 		.policy = ethnl_linkinfo_set_policy,
922 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
923 	},
924 	{
925 		.cmd	= ETHTOOL_MSG_LINKMODES_GET,
926 		.doit	= ethnl_default_doit,
927 		.start	= ethnl_default_start,
928 		.dumpit	= ethnl_default_dumpit,
929 		.done	= ethnl_default_done,
930 		.policy = ethnl_linkmodes_get_policy,
931 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
932 	},
933 	{
934 		.cmd	= ETHTOOL_MSG_LINKMODES_SET,
935 		.flags	= GENL_UNS_ADMIN_PERM,
936 		.doit	= ethnl_default_set_doit,
937 		.policy = ethnl_linkmodes_set_policy,
938 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
939 	},
940 	{
941 		.cmd	= ETHTOOL_MSG_LINKSTATE_GET,
942 		.doit	= ethnl_default_doit,
943 		.start	= ethnl_default_start,
944 		.dumpit	= ethnl_default_dumpit,
945 		.done	= ethnl_default_done,
946 		.policy = ethnl_linkstate_get_policy,
947 		.maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
948 	},
949 	{
950 		.cmd	= ETHTOOL_MSG_DEBUG_GET,
951 		.doit	= ethnl_default_doit,
952 		.start	= ethnl_default_start,
953 		.dumpit	= ethnl_default_dumpit,
954 		.done	= ethnl_default_done,
955 		.policy = ethnl_debug_get_policy,
956 		.maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
957 	},
958 	{
959 		.cmd	= ETHTOOL_MSG_DEBUG_SET,
960 		.flags	= GENL_UNS_ADMIN_PERM,
961 		.doit	= ethnl_default_set_doit,
962 		.policy = ethnl_debug_set_policy,
963 		.maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
964 	},
965 	{
966 		.cmd	= ETHTOOL_MSG_WOL_GET,
967 		.flags	= GENL_UNS_ADMIN_PERM,
968 		.doit	= ethnl_default_doit,
969 		.start	= ethnl_default_start,
970 		.dumpit	= ethnl_default_dumpit,
971 		.done	= ethnl_default_done,
972 		.policy = ethnl_wol_get_policy,
973 		.maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
974 	},
975 	{
976 		.cmd	= ETHTOOL_MSG_WOL_SET,
977 		.flags	= GENL_UNS_ADMIN_PERM,
978 		.doit	= ethnl_default_set_doit,
979 		.policy = ethnl_wol_set_policy,
980 		.maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
981 	},
982 	{
983 		.cmd	= ETHTOOL_MSG_FEATURES_GET,
984 		.doit	= ethnl_default_doit,
985 		.start	= ethnl_default_start,
986 		.dumpit	= ethnl_default_dumpit,
987 		.done	= ethnl_default_done,
988 		.policy = ethnl_features_get_policy,
989 		.maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
990 	},
991 	{
992 		.cmd	= ETHTOOL_MSG_FEATURES_SET,
993 		.flags	= GENL_UNS_ADMIN_PERM,
994 		.doit	= ethnl_set_features,
995 		.policy = ethnl_features_set_policy,
996 		.maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
997 	},
998 	{
999 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_GET,
1000 		.doit	= ethnl_default_doit,
1001 		.start	= ethnl_default_start,
1002 		.dumpit	= ethnl_default_dumpit,
1003 		.done	= ethnl_default_done,
1004 		.policy = ethnl_privflags_get_policy,
1005 		.maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
1006 	},
1007 	{
1008 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_SET,
1009 		.flags	= GENL_UNS_ADMIN_PERM,
1010 		.doit	= ethnl_default_set_doit,
1011 		.policy = ethnl_privflags_set_policy,
1012 		.maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
1013 	},
1014 	{
1015 		.cmd	= ETHTOOL_MSG_RINGS_GET,
1016 		.doit	= ethnl_default_doit,
1017 		.start	= ethnl_default_start,
1018 		.dumpit	= ethnl_default_dumpit,
1019 		.done	= ethnl_default_done,
1020 		.policy = ethnl_rings_get_policy,
1021 		.maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
1022 	},
1023 	{
1024 		.cmd	= ETHTOOL_MSG_RINGS_SET,
1025 		.flags	= GENL_UNS_ADMIN_PERM,
1026 		.doit	= ethnl_default_set_doit,
1027 		.policy = ethnl_rings_set_policy,
1028 		.maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
1029 	},
1030 	{
1031 		.cmd	= ETHTOOL_MSG_CHANNELS_GET,
1032 		.doit	= ethnl_default_doit,
1033 		.start	= ethnl_default_start,
1034 		.dumpit	= ethnl_default_dumpit,
1035 		.done	= ethnl_default_done,
1036 		.policy = ethnl_channels_get_policy,
1037 		.maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
1038 	},
1039 	{
1040 		.cmd	= ETHTOOL_MSG_CHANNELS_SET,
1041 		.flags	= GENL_UNS_ADMIN_PERM,
1042 		.doit	= ethnl_default_set_doit,
1043 		.policy = ethnl_channels_set_policy,
1044 		.maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
1045 	},
1046 	{
1047 		.cmd	= ETHTOOL_MSG_COALESCE_GET,
1048 		.doit	= ethnl_default_doit,
1049 		.start	= ethnl_default_start,
1050 		.dumpit	= ethnl_default_dumpit,
1051 		.done	= ethnl_default_done,
1052 		.policy = ethnl_coalesce_get_policy,
1053 		.maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
1054 	},
1055 	{
1056 		.cmd	= ETHTOOL_MSG_COALESCE_SET,
1057 		.flags	= GENL_UNS_ADMIN_PERM,
1058 		.doit	= ethnl_default_set_doit,
1059 		.policy = ethnl_coalesce_set_policy,
1060 		.maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
1061 	},
1062 	{
1063 		.cmd	= ETHTOOL_MSG_PAUSE_GET,
1064 		.doit	= ethnl_default_doit,
1065 		.start	= ethnl_default_start,
1066 		.dumpit	= ethnl_default_dumpit,
1067 		.done	= ethnl_default_done,
1068 		.policy = ethnl_pause_get_policy,
1069 		.maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
1070 	},
1071 	{
1072 		.cmd	= ETHTOOL_MSG_PAUSE_SET,
1073 		.flags	= GENL_UNS_ADMIN_PERM,
1074 		.doit	= ethnl_default_set_doit,
1075 		.policy = ethnl_pause_set_policy,
1076 		.maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
1077 	},
1078 	{
1079 		.cmd	= ETHTOOL_MSG_EEE_GET,
1080 		.doit	= ethnl_default_doit,
1081 		.start	= ethnl_default_start,
1082 		.dumpit	= ethnl_default_dumpit,
1083 		.done	= ethnl_default_done,
1084 		.policy = ethnl_eee_get_policy,
1085 		.maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
1086 	},
1087 	{
1088 		.cmd	= ETHTOOL_MSG_EEE_SET,
1089 		.flags	= GENL_UNS_ADMIN_PERM,
1090 		.doit	= ethnl_default_set_doit,
1091 		.policy = ethnl_eee_set_policy,
1092 		.maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
1093 	},
1094 	{
1095 		.cmd	= ETHTOOL_MSG_TSINFO_GET,
1096 		.doit	= ethnl_default_doit,
1097 		.start	= ethnl_tsinfo_start,
1098 		.dumpit	= ethnl_tsinfo_dumpit,
1099 		.done	= ethnl_tsinfo_done,
1100 		.policy = ethnl_tsinfo_get_policy,
1101 		.maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
1102 	},
1103 	{
1104 		.cmd	= ETHTOOL_MSG_CABLE_TEST_ACT,
1105 		.flags	= GENL_UNS_ADMIN_PERM,
1106 		.doit	= ethnl_act_cable_test,
1107 		.policy = ethnl_cable_test_act_policy,
1108 		.maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
1109 	},
1110 	{
1111 		.cmd	= ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
1112 		.flags	= GENL_UNS_ADMIN_PERM,
1113 		.doit	= ethnl_act_cable_test_tdr,
1114 		.policy = ethnl_cable_test_tdr_act_policy,
1115 		.maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
1116 	},
1117 	{
1118 		.cmd	= ETHTOOL_MSG_TUNNEL_INFO_GET,
1119 		.doit	= ethnl_tunnel_info_doit,
1120 		.start	= ethnl_tunnel_info_start,
1121 		.dumpit	= ethnl_tunnel_info_dumpit,
1122 		.policy = ethnl_tunnel_info_get_policy,
1123 		.maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
1124 	},
1125 	{
1126 		.cmd	= ETHTOOL_MSG_FEC_GET,
1127 		.doit	= ethnl_default_doit,
1128 		.start	= ethnl_default_start,
1129 		.dumpit	= ethnl_default_dumpit,
1130 		.done	= ethnl_default_done,
1131 		.policy = ethnl_fec_get_policy,
1132 		.maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
1133 	},
1134 	{
1135 		.cmd	= ETHTOOL_MSG_FEC_SET,
1136 		.flags	= GENL_UNS_ADMIN_PERM,
1137 		.doit	= ethnl_default_set_doit,
1138 		.policy = ethnl_fec_set_policy,
1139 		.maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
1140 	},
1141 	{
1142 		.cmd	= ETHTOOL_MSG_MODULE_EEPROM_GET,
1143 		.flags  = GENL_UNS_ADMIN_PERM,
1144 		.doit	= ethnl_default_doit,
1145 		.start	= ethnl_default_start,
1146 		.dumpit	= ethnl_default_dumpit,
1147 		.done	= ethnl_default_done,
1148 		.policy = ethnl_module_eeprom_get_policy,
1149 		.maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
1150 	},
1151 	{
1152 		.cmd	= ETHTOOL_MSG_STATS_GET,
1153 		.doit	= ethnl_default_doit,
1154 		.start	= ethnl_default_start,
1155 		.dumpit	= ethnl_default_dumpit,
1156 		.done	= ethnl_default_done,
1157 		.policy = ethnl_stats_get_policy,
1158 		.maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
1159 	},
1160 	{
1161 		.cmd	= ETHTOOL_MSG_PHC_VCLOCKS_GET,
1162 		.doit	= ethnl_default_doit,
1163 		.start	= ethnl_default_start,
1164 		.dumpit	= ethnl_default_dumpit,
1165 		.done	= ethnl_default_done,
1166 		.policy = ethnl_phc_vclocks_get_policy,
1167 		.maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
1168 	},
1169 	{
1170 		.cmd	= ETHTOOL_MSG_MODULE_GET,
1171 		.doit	= ethnl_default_doit,
1172 		.start	= ethnl_default_start,
1173 		.dumpit	= ethnl_default_dumpit,
1174 		.done	= ethnl_default_done,
1175 		.policy = ethnl_module_get_policy,
1176 		.maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1,
1177 	},
1178 	{
1179 		.cmd	= ETHTOOL_MSG_MODULE_SET,
1180 		.flags	= GENL_UNS_ADMIN_PERM,
1181 		.doit	= ethnl_default_set_doit,
1182 		.policy = ethnl_module_set_policy,
1183 		.maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1,
1184 	},
1185 	{
1186 		.cmd	= ETHTOOL_MSG_PSE_GET,
1187 		.doit	= ethnl_default_doit,
1188 		.start	= ethnl_default_start,
1189 		.dumpit	= ethnl_default_dumpit,
1190 		.done	= ethnl_default_done,
1191 		.policy = ethnl_pse_get_policy,
1192 		.maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
1193 	},
1194 	{
1195 		.cmd	= ETHTOOL_MSG_PSE_SET,
1196 		.flags	= GENL_UNS_ADMIN_PERM,
1197 		.doit	= ethnl_default_set_doit,
1198 		.policy = ethnl_pse_set_policy,
1199 		.maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1,
1200 	},
1201 	{
1202 		.cmd	= ETHTOOL_MSG_RSS_GET,
1203 		.doit	= ethnl_default_doit,
1204 		.start	= ethnl_rss_dump_start,
1205 		.dumpit	= ethnl_rss_dumpit,
1206 		.policy = ethnl_rss_get_policy,
1207 		.maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1,
1208 	},
1209 	{
1210 		.cmd	= ETHTOOL_MSG_PLCA_GET_CFG,
1211 		.doit	= ethnl_default_doit,
1212 		.start	= ethnl_default_start,
1213 		.dumpit	= ethnl_default_dumpit,
1214 		.done	= ethnl_default_done,
1215 		.policy = ethnl_plca_get_cfg_policy,
1216 		.maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
1217 	},
1218 	{
1219 		.cmd	= ETHTOOL_MSG_PLCA_SET_CFG,
1220 		.flags	= GENL_UNS_ADMIN_PERM,
1221 		.doit	= ethnl_default_set_doit,
1222 		.policy = ethnl_plca_set_cfg_policy,
1223 		.maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1,
1224 	},
1225 	{
1226 		.cmd	= ETHTOOL_MSG_PLCA_GET_STATUS,
1227 		.doit	= ethnl_default_doit,
1228 		.start	= ethnl_default_start,
1229 		.dumpit	= ethnl_default_dumpit,
1230 		.done	= ethnl_default_done,
1231 		.policy = ethnl_plca_get_status_policy,
1232 		.maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
1233 	},
1234 	{
1235 		.cmd	= ETHTOOL_MSG_MM_GET,
1236 		.doit	= ethnl_default_doit,
1237 		.start	= ethnl_default_start,
1238 		.dumpit	= ethnl_default_dumpit,
1239 		.done	= ethnl_default_done,
1240 		.policy = ethnl_mm_get_policy,
1241 		.maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1,
1242 	},
1243 	{
1244 		.cmd	= ETHTOOL_MSG_MM_SET,
1245 		.flags	= GENL_UNS_ADMIN_PERM,
1246 		.doit	= ethnl_default_set_doit,
1247 		.policy = ethnl_mm_set_policy,
1248 		.maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1,
1249 	},
1250 	{
1251 		.cmd	= ETHTOOL_MSG_MODULE_FW_FLASH_ACT,
1252 		.flags	= GENL_UNS_ADMIN_PERM,
1253 		.doit	= ethnl_act_module_fw_flash,
1254 		.policy	= ethnl_module_fw_flash_act_policy,
1255 		.maxattr = ARRAY_SIZE(ethnl_module_fw_flash_act_policy) - 1,
1256 	},
1257 	{
1258 		.cmd	= ETHTOOL_MSG_PHY_GET,
1259 		.doit	= ethnl_phy_doit,
1260 		.start	= ethnl_phy_start,
1261 		.dumpit	= ethnl_phy_dumpit,
1262 		.done	= ethnl_phy_done,
1263 		.policy = ethnl_phy_get_policy,
1264 		.maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1,
1265 	},
1266 	{
1267 		.cmd	= ETHTOOL_MSG_TSCONFIG_GET,
1268 		.doit	= ethnl_default_doit,
1269 		.start	= ethnl_default_start,
1270 		.dumpit	= ethnl_default_dumpit,
1271 		.done	= ethnl_default_done,
1272 		.policy = ethnl_tsconfig_get_policy,
1273 		.maxattr = ARRAY_SIZE(ethnl_tsconfig_get_policy) - 1,
1274 	},
1275 	{
1276 		.cmd	= ETHTOOL_MSG_TSCONFIG_SET,
1277 		.flags	= GENL_UNS_ADMIN_PERM,
1278 		.doit	= ethnl_default_set_doit,
1279 		.policy = ethnl_tsconfig_set_policy,
1280 		.maxattr = ARRAY_SIZE(ethnl_tsconfig_set_policy) - 1,
1281 	},
1282 };
1283 
1284 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
1285 	[ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
1286 };
1287 
1288 static struct genl_family ethtool_genl_family __ro_after_init = {
1289 	.name		= ETHTOOL_GENL_NAME,
1290 	.version	= ETHTOOL_GENL_VERSION,
1291 	.netnsok	= true,
1292 	.parallel_ops	= true,
1293 	.ops		= ethtool_genl_ops,
1294 	.n_ops		= ARRAY_SIZE(ethtool_genl_ops),
1295 	.resv_start_op	= ETHTOOL_MSG_MODULE_GET + 1,
1296 	.mcgrps		= ethtool_nl_mcgrps,
1297 	.n_mcgrps	= ARRAY_SIZE(ethtool_nl_mcgrps),
1298 	.sock_priv_size		= sizeof(struct ethnl_sock_priv),
1299 	.sock_priv_destroy	= ethnl_sock_priv_destroy,
1300 };
1301 
1302 /* module setup */
1303 
ethnl_init(void)1304 static int __init ethnl_init(void)
1305 {
1306 	int ret;
1307 
1308 	ret = genl_register_family(&ethtool_genl_family);
1309 	if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
1310 		return ret;
1311 	ethnl_ok = true;
1312 
1313 	ret = register_netdevice_notifier(&ethnl_netdev_notifier);
1314 	WARN(ret < 0, "ethtool: net device notifier registration failed");
1315 	return ret;
1316 }
1317 
1318 subsys_initcall(ethnl_init);
1319