• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <net/sock.h>
4 #include <linux/ethtool_netlink.h>
5 #include <linux/pm_runtime.h>
6 #include "netlink.h"
7 
8 static struct genl_family ethtool_genl_family;
9 
10 static bool ethnl_ok __read_mostly;
11 static u32 ethnl_bcast_seq;
12 
13 #define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS |	\
14 			     ETHTOOL_FLAG_OMIT_REPLY)
15 #define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS)
16 
17 const struct nla_policy ethnl_header_policy[] = {
18 	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
19 	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
20 					    .len = ALTIFNAMSIZ - 1 },
21 	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
22 							  ETHTOOL_FLAGS_BASIC),
23 };
24 
25 const struct nla_policy ethnl_header_policy_stats[] = {
26 	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
27 	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
28 					    .len = ALTIFNAMSIZ - 1 },
29 	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
30 							  ETHTOOL_FLAGS_STATS),
31 };
32 
ethnl_ops_begin(struct net_device * dev)33 int ethnl_ops_begin(struct net_device *dev)
34 {
35 	int ret;
36 
37 	if (!dev)
38 		return -ENODEV;
39 
40 	if (dev->dev.parent)
41 		pm_runtime_get_sync(dev->dev.parent);
42 
43 	if (!netif_device_present(dev) ||
44 	    dev->reg_state == NETREG_UNREGISTERING) {
45 		ret = -ENODEV;
46 		goto err;
47 	}
48 
49 	if (dev->ethtool_ops->begin) {
50 		ret = dev->ethtool_ops->begin(dev);
51 		if (ret)
52 			goto err;
53 	}
54 
55 	return 0;
56 err:
57 	if (dev->dev.parent)
58 		pm_runtime_put(dev->dev.parent);
59 
60 	return ret;
61 }
62 
ethnl_ops_complete(struct net_device * dev)63 void ethnl_ops_complete(struct net_device *dev)
64 {
65 	if (dev->ethtool_ops->complete)
66 		dev->ethtool_ops->complete(dev);
67 
68 	if (dev->dev.parent)
69 		pm_runtime_put(dev->dev.parent);
70 }
71 
72 /**
73  * ethnl_parse_header_dev_get() - parse request header
74  * @req_info:    structure to put results into
75  * @header:      nest attribute with request header
76  * @net:         request netns
77  * @extack:      netlink extack for error reporting
78  * @require_dev: fail if no device identified in header
79  *
80  * Parse request header in nested attribute @nest and puts results into
81  * the structure pointed to by @req_info. Extack from @info is used for error
82  * reporting. If req_info->dev is not null on return, reference to it has
83  * been taken. If error is returned, *req_info is null initialized and no
84  * reference is held.
85  *
86  * Return: 0 on success or negative error code
87  */
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)88 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
89 			       const struct nlattr *header, struct net *net,
90 			       struct netlink_ext_ack *extack, bool require_dev)
91 {
92 	struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy)];
93 	const struct nlattr *devname_attr;
94 	struct net_device *dev = NULL;
95 	u32 flags = 0;
96 	int ret;
97 
98 	if (!header) {
99 		NL_SET_ERR_MSG(extack, "request header missing");
100 		return -EINVAL;
101 	}
102 	/* No validation here, command policy should have a nested policy set
103 	 * for the header, therefore validation should have already been done.
104 	 */
105 	ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy) - 1, header,
106 			       NULL, extack);
107 	if (ret < 0)
108 		return ret;
109 	if (tb[ETHTOOL_A_HEADER_FLAGS])
110 		flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
111 
112 	devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
113 	if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
114 		u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
115 
116 		dev = dev_get_by_index(net, ifindex);
117 		if (!dev) {
118 			NL_SET_ERR_MSG_ATTR(extack,
119 					    tb[ETHTOOL_A_HEADER_DEV_INDEX],
120 					    "no device matches ifindex");
121 			return -ENODEV;
122 		}
123 		/* if both ifindex and ifname are passed, they must match */
124 		if (devname_attr &&
125 		    strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
126 			dev_put(dev);
127 			NL_SET_ERR_MSG_ATTR(extack, header,
128 					    "ifindex and name do not match");
129 			return -ENODEV;
130 		}
131 	} else if (devname_attr) {
132 		dev = dev_get_by_name(net, nla_data(devname_attr));
133 		if (!dev) {
134 			NL_SET_ERR_MSG_ATTR(extack, devname_attr,
135 					    "no device matches name");
136 			return -ENODEV;
137 		}
138 	} else if (require_dev) {
139 		NL_SET_ERR_MSG_ATTR(extack, header,
140 				    "neither ifindex nor name specified");
141 		return -EINVAL;
142 	}
143 
144 	req_info->dev = dev;
145 	req_info->flags = flags;
146 	return 0;
147 }
148 
149 /**
150  * ethnl_fill_reply_header() - Put common header into a reply message
151  * @skb:      skb with the message
152  * @dev:      network device to describe in header
153  * @attrtype: attribute type to use for the nest
154  *
155  * Create a nested attribute with attributes describing given network device.
156  *
157  * Return: 0 on success, error value (-EMSGSIZE only) on error
158  */
ethnl_fill_reply_header(struct sk_buff * skb,struct net_device * dev,u16 attrtype)159 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
160 			    u16 attrtype)
161 {
162 	struct nlattr *nest;
163 
164 	if (!dev)
165 		return 0;
166 	nest = nla_nest_start(skb, attrtype);
167 	if (!nest)
168 		return -EMSGSIZE;
169 
170 	if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
171 	    nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
172 		goto nla_put_failure;
173 	/* If more attributes are put into reply header, ethnl_header_size()
174 	 * must be updated to account for them.
175 	 */
176 
177 	nla_nest_end(skb, nest);
178 	return 0;
179 
180 nla_put_failure:
181 	nla_nest_cancel(skb, nest);
182 	return -EMSGSIZE;
183 }
184 
185 /**
186  * ethnl_reply_init() - Create skb for a reply and fill device identification
187  * @payload:      payload length (without netlink and genetlink header)
188  * @dev:          device the reply is about (may be null)
189  * @cmd:          ETHTOOL_MSG_* message type for reply
190  * @hdr_attrtype: attribute type for common header
191  * @info:         genetlink info of the received packet we respond to
192  * @ehdrp:        place to store payload pointer returned by genlmsg_new()
193  *
194  * Return: pointer to allocated skb on success, NULL on error
195  */
ethnl_reply_init(size_t payload,struct net_device * dev,u8 cmd,u16 hdr_attrtype,struct genl_info * info,void ** ehdrp)196 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
197 				 u16 hdr_attrtype, struct genl_info *info,
198 				 void **ehdrp)
199 {
200 	struct sk_buff *skb;
201 
202 	skb = genlmsg_new(payload, GFP_KERNEL);
203 	if (!skb)
204 		goto err;
205 	*ehdrp = genlmsg_put_reply(skb, info, &ethtool_genl_family, 0, cmd);
206 	if (!*ehdrp)
207 		goto err_free;
208 
209 	if (dev) {
210 		int ret;
211 
212 		ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
213 		if (ret < 0)
214 			goto err_free;
215 	}
216 	return skb;
217 
218 err_free:
219 	nlmsg_free(skb);
220 err:
221 	if (info)
222 		GENL_SET_ERR_MSG(info, "failed to setup reply message");
223 	return NULL;
224 }
225 
ethnl_dump_put(struct sk_buff * skb,struct netlink_callback * cb,u8 cmd)226 void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd)
227 {
228 	return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
229 			   &ethtool_genl_family, 0, cmd);
230 }
231 
ethnl_bcastmsg_put(struct sk_buff * skb,u8 cmd)232 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
233 {
234 	return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
235 			   cmd);
236 }
237 
ethnl_multicast(struct sk_buff * skb,struct net_device * dev)238 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
239 {
240 	return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
241 				       0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
242 }
243 
244 /* GET request helpers */
245 
246 /**
247  * struct ethnl_dump_ctx - context structure for generic dumpit() callback
248  * @ops:        request ops of currently processed message type
249  * @req_info:   parsed request header of processed request
250  * @reply_data: data needed to compose the reply
251  * @pos_hash:   saved iteration position - hashbucket
252  * @pos_idx:    saved iteration position - index
253  *
254  * These parameters are kept in struct netlink_callback as context preserved
255  * between iterations. They are initialized by ethnl_default_start() and used
256  * in ethnl_default_dumpit() and ethnl_default_done().
257  */
258 struct ethnl_dump_ctx {
259 	const struct ethnl_request_ops	*ops;
260 	struct ethnl_req_info		*req_info;
261 	struct ethnl_reply_data		*reply_data;
262 	int				pos_hash;
263 	int				pos_idx;
264 };
265 
266 static const struct ethnl_request_ops *
267 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
268 	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
269 	[ETHTOOL_MSG_LINKINFO_GET]	= &ethnl_linkinfo_request_ops,
270 	[ETHTOOL_MSG_LINKMODES_GET]	= &ethnl_linkmodes_request_ops,
271 	[ETHTOOL_MSG_LINKSTATE_GET]	= &ethnl_linkstate_request_ops,
272 	[ETHTOOL_MSG_DEBUG_GET]		= &ethnl_debug_request_ops,
273 	[ETHTOOL_MSG_WOL_GET]		= &ethnl_wol_request_ops,
274 	[ETHTOOL_MSG_FEATURES_GET]	= &ethnl_features_request_ops,
275 	[ETHTOOL_MSG_PRIVFLAGS_GET]	= &ethnl_privflags_request_ops,
276 	[ETHTOOL_MSG_RINGS_GET]		= &ethnl_rings_request_ops,
277 	[ETHTOOL_MSG_CHANNELS_GET]	= &ethnl_channels_request_ops,
278 	[ETHTOOL_MSG_COALESCE_GET]	= &ethnl_coalesce_request_ops,
279 	[ETHTOOL_MSG_PAUSE_GET]		= &ethnl_pause_request_ops,
280 	[ETHTOOL_MSG_EEE_GET]		= &ethnl_eee_request_ops,
281 	[ETHTOOL_MSG_FEC_GET]		= &ethnl_fec_request_ops,
282 	[ETHTOOL_MSG_TSINFO_GET]	= &ethnl_tsinfo_request_ops,
283 	[ETHTOOL_MSG_MODULE_EEPROM_GET]	= &ethnl_module_eeprom_request_ops,
284 	[ETHTOOL_MSG_STATS_GET]		= &ethnl_stats_request_ops,
285 	[ETHTOOL_MSG_PHC_VCLOCKS_GET]	= &ethnl_phc_vclocks_request_ops,
286 };
287 
ethnl_dump_context(struct netlink_callback * cb)288 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
289 {
290 	return (struct ethnl_dump_ctx *)cb->ctx;
291 }
292 
293 /**
294  * ethnl_default_parse() - Parse request message
295  * @req_info:    pointer to structure to put data into
296  * @tb:		 parsed attributes
297  * @net:         request netns
298  * @request_ops: struct request_ops for request type
299  * @extack:      netlink extack for error reporting
300  * @require_dev: fail if no device identified in header
301  *
302  * Parse universal request header and call request specific ->parse_request()
303  * callback (if defined) to parse the rest of the message.
304  *
305  * Return: 0 on success or negative error code
306  */
ethnl_default_parse(struct ethnl_req_info * req_info,struct nlattr ** tb,struct net * net,const struct ethnl_request_ops * request_ops,struct netlink_ext_ack * extack,bool require_dev)307 static int ethnl_default_parse(struct ethnl_req_info *req_info,
308 			       struct nlattr **tb, struct net *net,
309 			       const struct ethnl_request_ops *request_ops,
310 			       struct netlink_ext_ack *extack, bool require_dev)
311 {
312 	int ret;
313 
314 	ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
315 					 net, extack, require_dev);
316 	if (ret < 0)
317 		return ret;
318 
319 	if (request_ops->parse_request) {
320 		ret = request_ops->parse_request(req_info, tb, extack);
321 		if (ret < 0)
322 			return ret;
323 	}
324 
325 	return 0;
326 }
327 
328 /**
329  * ethnl_init_reply_data() - Initialize reply data for GET request
330  * @reply_data: pointer to embedded struct ethnl_reply_data
331  * @ops:        instance of struct ethnl_request_ops describing the layout
332  * @dev:        network device to initialize the reply for
333  *
334  * Fills the reply data part with zeros and sets the dev member. Must be called
335  * before calling the ->fill_reply() callback (for each iteration when handling
336  * dump requests).
337  */
ethnl_init_reply_data(struct ethnl_reply_data * reply_data,const struct ethnl_request_ops * ops,struct net_device * dev)338 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
339 				  const struct ethnl_request_ops *ops,
340 				  struct net_device *dev)
341 {
342 	memset(reply_data, 0, ops->reply_data_size);
343 	reply_data->dev = dev;
344 }
345 
346 /* default ->doit() handler for GET type requests */
ethnl_default_doit(struct sk_buff * skb,struct genl_info * info)347 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
348 {
349 	struct ethnl_reply_data *reply_data = NULL;
350 	struct ethnl_req_info *req_info = NULL;
351 	const u8 cmd = info->genlhdr->cmd;
352 	const struct ethnl_request_ops *ops;
353 	int hdr_len, reply_len;
354 	struct sk_buff *rskb;
355 	void *reply_payload;
356 	int ret;
357 
358 	ops = ethnl_default_requests[cmd];
359 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
360 		return -EOPNOTSUPP;
361 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
362 	if (!req_info)
363 		return -ENOMEM;
364 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
365 	if (!reply_data) {
366 		kfree(req_info);
367 		return -ENOMEM;
368 	}
369 
370 	ret = ethnl_default_parse(req_info, info->attrs, genl_info_net(info),
371 				  ops, info->extack, !ops->allow_nodev_do);
372 	if (ret < 0)
373 		goto err_dev;
374 	ethnl_init_reply_data(reply_data, ops, req_info->dev);
375 
376 	rtnl_lock();
377 	ret = ops->prepare_data(req_info, reply_data, info);
378 	rtnl_unlock();
379 	if (ret < 0)
380 		goto err_cleanup;
381 	ret = ops->reply_size(req_info, reply_data);
382 	if (ret < 0)
383 		goto err_cleanup;
384 	reply_len = ret;
385 	ret = -ENOMEM;
386 	rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(),
387 				req_info->dev, ops->reply_cmd,
388 				ops->hdr_attr, info, &reply_payload);
389 	if (!rskb)
390 		goto err_cleanup;
391 	hdr_len = rskb->len;
392 	ret = ops->fill_reply(rskb, req_info, reply_data);
393 	if (ret < 0)
394 		goto err_msg;
395 	WARN_ONCE(rskb->len - hdr_len > reply_len,
396 		  "ethnl cmd %d: calculated reply length %d, but consumed %d\n",
397 		  cmd, reply_len, rskb->len - hdr_len);
398 	if (ops->cleanup_data)
399 		ops->cleanup_data(reply_data);
400 
401 	genlmsg_end(rskb, reply_payload);
402 	dev_put(req_info->dev);
403 	kfree(reply_data);
404 	kfree(req_info);
405 	return genlmsg_reply(rskb, info);
406 
407 err_msg:
408 	WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
409 	nlmsg_free(rskb);
410 err_cleanup:
411 	if (ops->cleanup_data)
412 		ops->cleanup_data(reply_data);
413 err_dev:
414 	dev_put(req_info->dev);
415 	kfree(reply_data);
416 	kfree(req_info);
417 	return ret;
418 }
419 
ethnl_default_dump_one(struct sk_buff * skb,struct net_device * dev,const struct ethnl_dump_ctx * ctx,struct netlink_callback * cb)420 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
421 				  const struct ethnl_dump_ctx *ctx,
422 				  struct netlink_callback *cb)
423 {
424 	void *ehdr;
425 	int ret;
426 
427 	ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
428 			   &ethtool_genl_family, NLM_F_MULTI,
429 			   ctx->ops->reply_cmd);
430 	if (!ehdr)
431 		return -EMSGSIZE;
432 
433 	ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
434 	rtnl_lock();
435 	ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
436 	rtnl_unlock();
437 	if (ret < 0)
438 		goto out;
439 	ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
440 	if (ret < 0)
441 		goto out;
442 	ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
443 
444 out:
445 	if (ctx->ops->cleanup_data)
446 		ctx->ops->cleanup_data(ctx->reply_data);
447 	ctx->reply_data->dev = NULL;
448 	if (ret < 0)
449 		genlmsg_cancel(skb, ehdr);
450 	else
451 		genlmsg_end(skb, ehdr);
452 	return ret;
453 }
454 
455 /* Default ->dumpit() handler for GET requests. Device iteration copied from
456  * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
457  * persistence as we cannot guarantee to hold RTNL lock through the whole
458  * function as rtnetnlink does.
459  */
ethnl_default_dumpit(struct sk_buff * skb,struct netlink_callback * cb)460 static int ethnl_default_dumpit(struct sk_buff *skb,
461 				struct netlink_callback *cb)
462 {
463 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
464 	struct net *net = sock_net(skb->sk);
465 	int s_idx = ctx->pos_idx;
466 	int h, idx = 0;
467 	int ret = 0;
468 
469 	rtnl_lock();
470 	for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
471 		struct hlist_head *head;
472 		struct net_device *dev;
473 		unsigned int seq;
474 
475 		head = &net->dev_index_head[h];
476 
477 restart_chain:
478 		seq = net->dev_base_seq;
479 		cb->seq = seq;
480 		idx = 0;
481 		hlist_for_each_entry(dev, head, index_hlist) {
482 			if (idx < s_idx)
483 				goto cont;
484 			dev_hold(dev);
485 			rtnl_unlock();
486 
487 			ret = ethnl_default_dump_one(skb, dev, ctx, cb);
488 			dev_put(dev);
489 			if (ret < 0) {
490 				if (ret == -EOPNOTSUPP)
491 					goto lock_and_cont;
492 				if (likely(skb->len))
493 					ret = skb->len;
494 				goto out;
495 			}
496 lock_and_cont:
497 			rtnl_lock();
498 			if (net->dev_base_seq != seq) {
499 				s_idx = idx + 1;
500 				goto restart_chain;
501 			}
502 cont:
503 			idx++;
504 		}
505 
506 	}
507 	rtnl_unlock();
508 
509 out:
510 	ctx->pos_hash = h;
511 	ctx->pos_idx = idx;
512 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
513 
514 	return ret;
515 }
516 
517 /* generic ->start() handler for GET requests */
ethnl_default_start(struct netlink_callback * cb)518 static int ethnl_default_start(struct netlink_callback *cb)
519 {
520 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
521 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
522 	struct ethnl_reply_data *reply_data;
523 	const struct ethnl_request_ops *ops;
524 	struct ethnl_req_info *req_info;
525 	struct genlmsghdr *ghdr;
526 	int ret;
527 
528 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
529 
530 	ghdr = nlmsg_data(cb->nlh);
531 	ops = ethnl_default_requests[ghdr->cmd];
532 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
533 		return -EOPNOTSUPP;
534 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
535 	if (!req_info)
536 		return -ENOMEM;
537 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
538 	if (!reply_data) {
539 		ret = -ENOMEM;
540 		goto free_req_info;
541 	}
542 
543 	ret = ethnl_default_parse(req_info, info->attrs, sock_net(cb->skb->sk),
544 				  ops, cb->extack, false);
545 	if (req_info->dev) {
546 		/* We ignore device specification in dump requests but as the
547 		 * same parser as for non-dump (doit) requests is used, it
548 		 * would take reference to the device if it finds one
549 		 */
550 		dev_put(req_info->dev);
551 		req_info->dev = NULL;
552 	}
553 	if (ret < 0)
554 		goto free_reply_data;
555 
556 	ctx->ops = ops;
557 	ctx->req_info = req_info;
558 	ctx->reply_data = reply_data;
559 	ctx->pos_hash = 0;
560 	ctx->pos_idx = 0;
561 
562 	return 0;
563 
564 free_reply_data:
565 	kfree(reply_data);
566 free_req_info:
567 	kfree(req_info);
568 
569 	return ret;
570 }
571 
572 /* default ->done() handler for GET requests */
ethnl_default_done(struct netlink_callback * cb)573 static int ethnl_default_done(struct netlink_callback *cb)
574 {
575 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
576 
577 	kfree(ctx->reply_data);
578 	kfree(ctx->req_info);
579 
580 	return 0;
581 }
582 
583 static const struct ethnl_request_ops *
584 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
585 	[ETHTOOL_MSG_LINKINFO_NTF]	= &ethnl_linkinfo_request_ops,
586 	[ETHTOOL_MSG_LINKMODES_NTF]	= &ethnl_linkmodes_request_ops,
587 	[ETHTOOL_MSG_DEBUG_NTF]		= &ethnl_debug_request_ops,
588 	[ETHTOOL_MSG_WOL_NTF]		= &ethnl_wol_request_ops,
589 	[ETHTOOL_MSG_FEATURES_NTF]	= &ethnl_features_request_ops,
590 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= &ethnl_privflags_request_ops,
591 	[ETHTOOL_MSG_RINGS_NTF]		= &ethnl_rings_request_ops,
592 	[ETHTOOL_MSG_CHANNELS_NTF]	= &ethnl_channels_request_ops,
593 	[ETHTOOL_MSG_COALESCE_NTF]	= &ethnl_coalesce_request_ops,
594 	[ETHTOOL_MSG_PAUSE_NTF]		= &ethnl_pause_request_ops,
595 	[ETHTOOL_MSG_EEE_NTF]		= &ethnl_eee_request_ops,
596 	[ETHTOOL_MSG_FEC_NTF]		= &ethnl_fec_request_ops,
597 };
598 
599 /* default notification handler */
ethnl_default_notify(struct net_device * dev,unsigned int cmd,const void * data)600 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
601 				 const void *data)
602 {
603 	struct ethnl_reply_data *reply_data;
604 	const struct ethnl_request_ops *ops;
605 	struct ethnl_req_info *req_info;
606 	struct sk_buff *skb;
607 	void *reply_payload;
608 	int reply_len;
609 	int ret;
610 
611 	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
612 		      !ethnl_default_notify_ops[cmd],
613 		      "unexpected notification type %u\n", cmd))
614 		return;
615 	ops = ethnl_default_notify_ops[cmd];
616 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
617 	if (!req_info)
618 		return;
619 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
620 	if (!reply_data) {
621 		kfree(req_info);
622 		return;
623 	}
624 
625 	req_info->dev = dev;
626 	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
627 
628 	ethnl_init_reply_data(reply_data, ops, dev);
629 	ret = ops->prepare_data(req_info, reply_data, NULL);
630 	if (ret < 0)
631 		goto err_cleanup;
632 	ret = ops->reply_size(req_info, reply_data);
633 	if (ret < 0)
634 		goto err_cleanup;
635 	reply_len = ret + ethnl_reply_header_size();
636 	ret = -ENOMEM;
637 	skb = genlmsg_new(reply_len, GFP_KERNEL);
638 	if (!skb)
639 		goto err_cleanup;
640 	reply_payload = ethnl_bcastmsg_put(skb, cmd);
641 	if (!reply_payload)
642 		goto err_skb;
643 	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
644 	if (ret < 0)
645 		goto err_msg;
646 	ret = ops->fill_reply(skb, req_info, reply_data);
647 	if (ret < 0)
648 		goto err_msg;
649 	if (ops->cleanup_data)
650 		ops->cleanup_data(reply_data);
651 
652 	genlmsg_end(skb, reply_payload);
653 	kfree(reply_data);
654 	kfree(req_info);
655 	ethnl_multicast(skb, dev);
656 	return;
657 
658 err_msg:
659 	WARN_ONCE(ret == -EMSGSIZE,
660 		  "calculated message payload length (%d) not sufficient\n",
661 		  reply_len);
662 err_skb:
663 	nlmsg_free(skb);
664 err_cleanup:
665 	if (ops->cleanup_data)
666 		ops->cleanup_data(reply_data);
667 	kfree(reply_data);
668 	kfree(req_info);
669 	return;
670 }
671 
672 /* notifications */
673 
674 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
675 				       const void *data);
676 
677 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
678 	[ETHTOOL_MSG_LINKINFO_NTF]	= ethnl_default_notify,
679 	[ETHTOOL_MSG_LINKMODES_NTF]	= ethnl_default_notify,
680 	[ETHTOOL_MSG_DEBUG_NTF]		= ethnl_default_notify,
681 	[ETHTOOL_MSG_WOL_NTF]		= ethnl_default_notify,
682 	[ETHTOOL_MSG_FEATURES_NTF]	= ethnl_default_notify,
683 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= ethnl_default_notify,
684 	[ETHTOOL_MSG_RINGS_NTF]		= ethnl_default_notify,
685 	[ETHTOOL_MSG_CHANNELS_NTF]	= ethnl_default_notify,
686 	[ETHTOOL_MSG_COALESCE_NTF]	= ethnl_default_notify,
687 	[ETHTOOL_MSG_PAUSE_NTF]		= ethnl_default_notify,
688 	[ETHTOOL_MSG_EEE_NTF]		= ethnl_default_notify,
689 	[ETHTOOL_MSG_FEC_NTF]		= ethnl_default_notify,
690 };
691 
ethtool_notify(struct net_device * dev,unsigned int cmd,const void * data)692 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
693 {
694 	if (unlikely(!ethnl_ok))
695 		return;
696 	ASSERT_RTNL();
697 
698 	if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
699 		   ethnl_notify_handlers[cmd]))
700 		ethnl_notify_handlers[cmd](dev, cmd, data);
701 	else
702 		WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
703 			  cmd, netdev_name(dev));
704 }
705 EXPORT_SYMBOL(ethtool_notify);
706 
ethnl_notify_features(struct netdev_notifier_info * info)707 static void ethnl_notify_features(struct netdev_notifier_info *info)
708 {
709 	struct net_device *dev = netdev_notifier_info_to_dev(info);
710 
711 	ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
712 }
713 
ethnl_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)714 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
715 			      void *ptr)
716 {
717 	switch (event) {
718 	case NETDEV_FEAT_CHANGE:
719 		ethnl_notify_features(ptr);
720 		break;
721 	}
722 
723 	return NOTIFY_DONE;
724 }
725 
726 static struct notifier_block ethnl_netdev_notifier = {
727 	.notifier_call = ethnl_netdev_event,
728 };
729 
730 /* genetlink setup */
731 
732 static const struct genl_ops ethtool_genl_ops[] = {
733 	{
734 		.cmd	= ETHTOOL_MSG_STRSET_GET,
735 		.doit	= ethnl_default_doit,
736 		.start	= ethnl_default_start,
737 		.dumpit	= ethnl_default_dumpit,
738 		.done	= ethnl_default_done,
739 		.policy = ethnl_strset_get_policy,
740 		.maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
741 	},
742 	{
743 		.cmd	= ETHTOOL_MSG_LINKINFO_GET,
744 		.doit	= ethnl_default_doit,
745 		.start	= ethnl_default_start,
746 		.dumpit	= ethnl_default_dumpit,
747 		.done	= ethnl_default_done,
748 		.policy = ethnl_linkinfo_get_policy,
749 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
750 	},
751 	{
752 		.cmd	= ETHTOOL_MSG_LINKINFO_SET,
753 		.flags	= GENL_UNS_ADMIN_PERM,
754 		.doit	= ethnl_set_linkinfo,
755 		.policy = ethnl_linkinfo_set_policy,
756 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
757 	},
758 	{
759 		.cmd	= ETHTOOL_MSG_LINKMODES_GET,
760 		.doit	= ethnl_default_doit,
761 		.start	= ethnl_default_start,
762 		.dumpit	= ethnl_default_dumpit,
763 		.done	= ethnl_default_done,
764 		.policy = ethnl_linkmodes_get_policy,
765 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
766 	},
767 	{
768 		.cmd	= ETHTOOL_MSG_LINKMODES_SET,
769 		.flags	= GENL_UNS_ADMIN_PERM,
770 		.doit	= ethnl_set_linkmodes,
771 		.policy = ethnl_linkmodes_set_policy,
772 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
773 	},
774 	{
775 		.cmd	= ETHTOOL_MSG_LINKSTATE_GET,
776 		.doit	= ethnl_default_doit,
777 		.start	= ethnl_default_start,
778 		.dumpit	= ethnl_default_dumpit,
779 		.done	= ethnl_default_done,
780 		.policy = ethnl_linkstate_get_policy,
781 		.maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
782 	},
783 	{
784 		.cmd	= ETHTOOL_MSG_DEBUG_GET,
785 		.doit	= ethnl_default_doit,
786 		.start	= ethnl_default_start,
787 		.dumpit	= ethnl_default_dumpit,
788 		.done	= ethnl_default_done,
789 		.policy = ethnl_debug_get_policy,
790 		.maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
791 	},
792 	{
793 		.cmd	= ETHTOOL_MSG_DEBUG_SET,
794 		.flags	= GENL_UNS_ADMIN_PERM,
795 		.doit	= ethnl_set_debug,
796 		.policy = ethnl_debug_set_policy,
797 		.maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
798 	},
799 	{
800 		.cmd	= ETHTOOL_MSG_WOL_GET,
801 		.flags	= GENL_UNS_ADMIN_PERM,
802 		.doit	= ethnl_default_doit,
803 		.start	= ethnl_default_start,
804 		.dumpit	= ethnl_default_dumpit,
805 		.done	= ethnl_default_done,
806 		.policy = ethnl_wol_get_policy,
807 		.maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
808 	},
809 	{
810 		.cmd	= ETHTOOL_MSG_WOL_SET,
811 		.flags	= GENL_UNS_ADMIN_PERM,
812 		.doit	= ethnl_set_wol,
813 		.policy = ethnl_wol_set_policy,
814 		.maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
815 	},
816 	{
817 		.cmd	= ETHTOOL_MSG_FEATURES_GET,
818 		.doit	= ethnl_default_doit,
819 		.start	= ethnl_default_start,
820 		.dumpit	= ethnl_default_dumpit,
821 		.done	= ethnl_default_done,
822 		.policy = ethnl_features_get_policy,
823 		.maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
824 	},
825 	{
826 		.cmd	= ETHTOOL_MSG_FEATURES_SET,
827 		.flags	= GENL_UNS_ADMIN_PERM,
828 		.doit	= ethnl_set_features,
829 		.policy = ethnl_features_set_policy,
830 		.maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
831 	},
832 	{
833 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_GET,
834 		.doit	= ethnl_default_doit,
835 		.start	= ethnl_default_start,
836 		.dumpit	= ethnl_default_dumpit,
837 		.done	= ethnl_default_done,
838 		.policy = ethnl_privflags_get_policy,
839 		.maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
840 	},
841 	{
842 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_SET,
843 		.flags	= GENL_UNS_ADMIN_PERM,
844 		.doit	= ethnl_set_privflags,
845 		.policy = ethnl_privflags_set_policy,
846 		.maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
847 	},
848 	{
849 		.cmd	= ETHTOOL_MSG_RINGS_GET,
850 		.doit	= ethnl_default_doit,
851 		.start	= ethnl_default_start,
852 		.dumpit	= ethnl_default_dumpit,
853 		.done	= ethnl_default_done,
854 		.policy = ethnl_rings_get_policy,
855 		.maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
856 	},
857 	{
858 		.cmd	= ETHTOOL_MSG_RINGS_SET,
859 		.flags	= GENL_UNS_ADMIN_PERM,
860 		.doit	= ethnl_set_rings,
861 		.policy = ethnl_rings_set_policy,
862 		.maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
863 	},
864 	{
865 		.cmd	= ETHTOOL_MSG_CHANNELS_GET,
866 		.doit	= ethnl_default_doit,
867 		.start	= ethnl_default_start,
868 		.dumpit	= ethnl_default_dumpit,
869 		.done	= ethnl_default_done,
870 		.policy = ethnl_channels_get_policy,
871 		.maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
872 	},
873 	{
874 		.cmd	= ETHTOOL_MSG_CHANNELS_SET,
875 		.flags	= GENL_UNS_ADMIN_PERM,
876 		.doit	= ethnl_set_channels,
877 		.policy = ethnl_channels_set_policy,
878 		.maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
879 	},
880 	{
881 		.cmd	= ETHTOOL_MSG_COALESCE_GET,
882 		.doit	= ethnl_default_doit,
883 		.start	= ethnl_default_start,
884 		.dumpit	= ethnl_default_dumpit,
885 		.done	= ethnl_default_done,
886 		.policy = ethnl_coalesce_get_policy,
887 		.maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
888 	},
889 	{
890 		.cmd	= ETHTOOL_MSG_COALESCE_SET,
891 		.flags	= GENL_UNS_ADMIN_PERM,
892 		.doit	= ethnl_set_coalesce,
893 		.policy = ethnl_coalesce_set_policy,
894 		.maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
895 	},
896 	{
897 		.cmd	= ETHTOOL_MSG_PAUSE_GET,
898 		.doit	= ethnl_default_doit,
899 		.start	= ethnl_default_start,
900 		.dumpit	= ethnl_default_dumpit,
901 		.done	= ethnl_default_done,
902 		.policy = ethnl_pause_get_policy,
903 		.maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
904 	},
905 	{
906 		.cmd	= ETHTOOL_MSG_PAUSE_SET,
907 		.flags	= GENL_UNS_ADMIN_PERM,
908 		.doit	= ethnl_set_pause,
909 		.policy = ethnl_pause_set_policy,
910 		.maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
911 	},
912 	{
913 		.cmd	= ETHTOOL_MSG_EEE_GET,
914 		.doit	= ethnl_default_doit,
915 		.start	= ethnl_default_start,
916 		.dumpit	= ethnl_default_dumpit,
917 		.done	= ethnl_default_done,
918 		.policy = ethnl_eee_get_policy,
919 		.maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
920 	},
921 	{
922 		.cmd	= ETHTOOL_MSG_EEE_SET,
923 		.flags	= GENL_UNS_ADMIN_PERM,
924 		.doit	= ethnl_set_eee,
925 		.policy = ethnl_eee_set_policy,
926 		.maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
927 	},
928 	{
929 		.cmd	= ETHTOOL_MSG_TSINFO_GET,
930 		.doit	= ethnl_default_doit,
931 		.start	= ethnl_default_start,
932 		.dumpit	= ethnl_default_dumpit,
933 		.done	= ethnl_default_done,
934 		.policy = ethnl_tsinfo_get_policy,
935 		.maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
936 	},
937 	{
938 		.cmd	= ETHTOOL_MSG_CABLE_TEST_ACT,
939 		.flags	= GENL_UNS_ADMIN_PERM,
940 		.doit	= ethnl_act_cable_test,
941 		.policy = ethnl_cable_test_act_policy,
942 		.maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
943 	},
944 	{
945 		.cmd	= ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
946 		.flags	= GENL_UNS_ADMIN_PERM,
947 		.doit	= ethnl_act_cable_test_tdr,
948 		.policy = ethnl_cable_test_tdr_act_policy,
949 		.maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
950 	},
951 	{
952 		.cmd	= ETHTOOL_MSG_TUNNEL_INFO_GET,
953 		.doit	= ethnl_tunnel_info_doit,
954 		.start	= ethnl_tunnel_info_start,
955 		.dumpit	= ethnl_tunnel_info_dumpit,
956 		.policy = ethnl_tunnel_info_get_policy,
957 		.maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
958 	},
959 	{
960 		.cmd	= ETHTOOL_MSG_FEC_GET,
961 		.doit	= ethnl_default_doit,
962 		.start	= ethnl_default_start,
963 		.dumpit	= ethnl_default_dumpit,
964 		.done	= ethnl_default_done,
965 		.policy = ethnl_fec_get_policy,
966 		.maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
967 	},
968 	{
969 		.cmd	= ETHTOOL_MSG_FEC_SET,
970 		.flags	= GENL_UNS_ADMIN_PERM,
971 		.doit	= ethnl_set_fec,
972 		.policy = ethnl_fec_set_policy,
973 		.maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
974 	},
975 	{
976 		.cmd	= ETHTOOL_MSG_MODULE_EEPROM_GET,
977 		.flags  = GENL_UNS_ADMIN_PERM,
978 		.doit	= ethnl_default_doit,
979 		.start	= ethnl_default_start,
980 		.dumpit	= ethnl_default_dumpit,
981 		.done	= ethnl_default_done,
982 		.policy = ethnl_module_eeprom_get_policy,
983 		.maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
984 	},
985 	{
986 		.cmd	= ETHTOOL_MSG_STATS_GET,
987 		.doit	= ethnl_default_doit,
988 		.start	= ethnl_default_start,
989 		.dumpit	= ethnl_default_dumpit,
990 		.done	= ethnl_default_done,
991 		.policy = ethnl_stats_get_policy,
992 		.maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
993 	},
994 	{
995 		.cmd	= ETHTOOL_MSG_PHC_VCLOCKS_GET,
996 		.doit	= ethnl_default_doit,
997 		.start	= ethnl_default_start,
998 		.dumpit	= ethnl_default_dumpit,
999 		.done	= ethnl_default_done,
1000 		.policy = ethnl_phc_vclocks_get_policy,
1001 		.maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
1002 	},
1003 };
1004 
1005 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
1006 	[ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
1007 };
1008 
1009 static struct genl_family ethtool_genl_family __ro_after_init = {
1010 	.name		= ETHTOOL_GENL_NAME,
1011 	.version	= ETHTOOL_GENL_VERSION,
1012 	.netnsok	= true,
1013 	.parallel_ops	= true,
1014 	.ops		= ethtool_genl_ops,
1015 	.n_ops		= ARRAY_SIZE(ethtool_genl_ops),
1016 	.mcgrps		= ethtool_nl_mcgrps,
1017 	.n_mcgrps	= ARRAY_SIZE(ethtool_nl_mcgrps),
1018 };
1019 
1020 /* module setup */
1021 
ethnl_init(void)1022 static int __init ethnl_init(void)
1023 {
1024 	int ret;
1025 
1026 	ret = genl_register_family(&ethtool_genl_family);
1027 	if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
1028 		return ret;
1029 	ethnl_ok = true;
1030 
1031 	ret = register_netdevice_notifier(&ethnl_netdev_notifier);
1032 	WARN(ret < 0, "ethtool: net device notifier registration failed");
1033 	return ret;
1034 }
1035 
1036 subsys_initcall(ethnl_init);
1037