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