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