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