blob: fc9e0b80688982e6087e58216bc1d30a61c4cdfe [file] [log] [blame]
Michal Kubecek2b4a8992019-12-27 15:55:18 +01001// SPDX-License-Identifier: GPL-2.0-only
2
Michal Kubecek041b1c52019-12-27 15:55:23 +01003#include <net/sock.h>
Michal Kubecek2b4a8992019-12-27 15:55:18 +01004#include <linux/ethtool_netlink.h>
5#include "netlink.h"
6
Michal Kubecek041b1c52019-12-27 15:55:23 +01007static struct genl_family ethtool_genl_family;
8
Michal Kubecek6b08d6c2019-12-27 15:55:33 +01009static bool ethnl_ok __read_mostly;
Michal Kubecek5cf2a542019-12-27 15:55:58 +010010static u32 ethnl_bcast_seq;
Michal Kubecek6b08d6c2019-12-27 15:55:33 +010011
Michal Kubecek041b1c52019-12-27 15:55:23 +010012static 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() - 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 */
36int ethnl_parse_header(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;
Michal Kubecek2363d732020-03-15 18:17:53 +010043 u32 flags = 0;
Michal Kubecek041b1c52019-12-27 15:55:23 +010044 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;
Michal Kubecek2363d732020-03-15 18:17:53 +010054 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 }
Michal Kubecek041b1c52019-12-27 15:55:23 +010063
Michal Kubecek2363d732020-03-15 18:17:53 +010064 devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
Michal Kubecek041b1c52019-12-27 15:55:23 +010065 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;
Michal Kubecek2363d732020-03-15 18:17:53 +0100103 req_info->flags = flags;
Michal Kubecek041b1c52019-12-27 15:55:23 +0100104 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 */
117int 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
138nla_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
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100145 * @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()
Michal Kubecek041b1c52019-12-27 15:55:23 +0100151 *
152 * Return: pointer to allocated skb on success, NULL on error
153 */
154struct 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
176err_free:
177 nlmsg_free(skb);
178err:
179 if (info)
180 GENL_SET_ERR_MSG(info, "failed to setup reply message");
181 return NULL;
182}
183
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100184static 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
190static 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
Michal Kubecek728480f2019-12-27 15:55:38 +0100196/* GET request helpers */
197
198/**
199 * struct ethnl_dump_ctx - context structure for generic dumpit() callback
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100200 * @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
Michal Kubecek728480f2019-12-27 15:55:38 +0100205 *
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 */
210struct 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
218static const struct ethnl_request_ops *
219ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
Michal Kubecek71921692019-12-27 15:55:43 +0100220 [ETHTOOL_MSG_STRSET_GET] = &ethnl_strset_request_ops,
Michal Kubecek459e0b82019-12-27 15:55:48 +0100221 [ETHTOOL_MSG_LINKINFO_GET] = &ethnl_linkinfo_request_ops,
Michal Kubecekf625aa92019-12-27 15:56:08 +0100222 [ETHTOOL_MSG_LINKMODES_GET] = &ethnl_linkmodes_request_ops,
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100223 [ETHTOOL_MSG_LINKSTATE_GET] = &ethnl_linkstate_request_ops,
Michal Kubecek6a94b8c2020-01-26 23:11:04 +0100224 [ETHTOOL_MSG_DEBUG_GET] = &ethnl_debug_request_ops,
Michal Kubecek51ea22b2020-01-26 23:11:13 +0100225 [ETHTOOL_MSG_WOL_GET] = &ethnl_wol_request_ops,
Michal Kubecek728480f2019-12-27 15:55:38 +0100226};
227
228static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
229{
230 return (struct ethnl_dump_ctx *)cb->ctx;
231}
232
233/**
234 * ethnl_default_parse() - Parse request message
235 * @req_info: pointer to structure to put data into
236 * @nlhdr: pointer to request message header
237 * @net: request netns
238 * @request_ops: struct request_ops for request type
239 * @extack: netlink extack for error reporting
240 * @require_dev: fail if no device identified in header
241 *
242 * Parse universal request header and call request specific ->parse_request()
243 * callback (if defined) to parse the rest of the message.
244 *
245 * Return: 0 on success or negative error code
246 */
247static int ethnl_default_parse(struct ethnl_req_info *req_info,
248 const struct nlmsghdr *nlhdr, struct net *net,
249 const struct ethnl_request_ops *request_ops,
250 struct netlink_ext_ack *extack, bool require_dev)
251{
252 struct nlattr **tb;
253 int ret;
254
255 tb = kmalloc_array(request_ops->max_attr + 1, sizeof(tb[0]),
256 GFP_KERNEL);
257 if (!tb)
258 return -ENOMEM;
259
260 ret = nlmsg_parse(nlhdr, GENL_HDRLEN, tb, request_ops->max_attr,
261 request_ops->request_policy, extack);
262 if (ret < 0)
263 goto out;
264 ret = ethnl_parse_header(req_info, tb[request_ops->hdr_attr], net,
265 extack, require_dev);
266 if (ret < 0)
267 goto out;
268
269 if (request_ops->parse_request) {
270 ret = request_ops->parse_request(req_info, tb, extack);
271 if (ret < 0)
272 goto out;
273 }
274
275 ret = 0;
276out:
277 kfree(tb);
278 return ret;
279}
280
281/**
282 * ethnl_init_reply_data() - Initialize reply data for GET request
Michal Kubecekd2c4b442020-01-26 23:11:01 +0100283 * @reply_data: pointer to embedded struct ethnl_reply_data
284 * @ops: instance of struct ethnl_request_ops describing the layout
285 * @dev: network device to initialize the reply for
Michal Kubecek728480f2019-12-27 15:55:38 +0100286 *
287 * Fills the reply data part with zeros and sets the dev member. Must be called
288 * before calling the ->fill_reply() callback (for each iteration when handling
289 * dump requests).
290 */
291static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
292 const struct ethnl_request_ops *ops,
293 struct net_device *dev)
294{
295 memset(reply_data, 0, ops->reply_data_size);
296 reply_data->dev = dev;
297}
298
299/* default ->doit() handler for GET type requests */
300static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
301{
302 struct ethnl_reply_data *reply_data = NULL;
303 struct ethnl_req_info *req_info = NULL;
304 const u8 cmd = info->genlhdr->cmd;
305 const struct ethnl_request_ops *ops;
306 struct sk_buff *rskb;
307 void *reply_payload;
308 int reply_len;
309 int ret;
310
311 ops = ethnl_default_requests[cmd];
312 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
313 return -EOPNOTSUPP;
314 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
315 if (!req_info)
316 return -ENOMEM;
317 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
318 if (!reply_data) {
319 kfree(req_info);
320 return -ENOMEM;
321 }
322
323 ret = ethnl_default_parse(req_info, info->nlhdr, genl_info_net(info), ops,
324 info->extack, !ops->allow_nodev_do);
325 if (ret < 0)
326 goto err_dev;
327 ethnl_init_reply_data(reply_data, ops, req_info->dev);
328
329 rtnl_lock();
330 ret = ops->prepare_data(req_info, reply_data, info);
331 rtnl_unlock();
332 if (ret < 0)
333 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300334 ret = ops->reply_size(req_info, reply_data);
Michal Kubecek728480f2019-12-27 15:55:38 +0100335 if (ret < 0)
336 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300337 reply_len = ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100338 ret = -ENOMEM;
339 rskb = ethnl_reply_init(reply_len, req_info->dev, ops->reply_cmd,
340 ops->hdr_attr, info, &reply_payload);
341 if (!rskb)
342 goto err_cleanup;
343 ret = ops->fill_reply(rskb, req_info, reply_data);
344 if (ret < 0)
345 goto err_msg;
346 if (ops->cleanup_data)
347 ops->cleanup_data(reply_data);
348
349 genlmsg_end(rskb, reply_payload);
350 if (req_info->dev)
351 dev_put(req_info->dev);
352 kfree(reply_data);
353 kfree(req_info);
354 return genlmsg_reply(rskb, info);
355
356err_msg:
357 WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
358 nlmsg_free(rskb);
359err_cleanup:
360 if (ops->cleanup_data)
361 ops->cleanup_data(reply_data);
362err_dev:
363 if (req_info->dev)
364 dev_put(req_info->dev);
365 kfree(reply_data);
366 kfree(req_info);
367 return ret;
368}
369
370static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
371 const struct ethnl_dump_ctx *ctx)
372{
373 int ret;
374
375 ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
376 rtnl_lock();
377 ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
378 rtnl_unlock();
379 if (ret < 0)
380 goto out;
381 ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
382 if (ret < 0)
383 goto out;
384 ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
385
386out:
387 if (ctx->ops->cleanup_data)
388 ctx->ops->cleanup_data(ctx->reply_data);
389 ctx->reply_data->dev = NULL;
390 return ret;
391}
392
393/* Default ->dumpit() handler for GET requests. Device iteration copied from
394 * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
395 * persistence as we cannot guarantee to hold RTNL lock through the whole
396 * function as rtnetnlink does.
397 */
398static int ethnl_default_dumpit(struct sk_buff *skb,
399 struct netlink_callback *cb)
400{
401 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
402 struct net *net = sock_net(skb->sk);
403 int s_idx = ctx->pos_idx;
404 int h, idx = 0;
405 int ret = 0;
406 void *ehdr;
407
408 rtnl_lock();
409 for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
410 struct hlist_head *head;
411 struct net_device *dev;
412 unsigned int seq;
413
414 head = &net->dev_index_head[h];
415
416restart_chain:
417 seq = net->dev_base_seq;
418 cb->seq = seq;
419 idx = 0;
420 hlist_for_each_entry(dev, head, index_hlist) {
421 if (idx < s_idx)
422 goto cont;
423 dev_hold(dev);
424 rtnl_unlock();
425
426 ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
427 cb->nlh->nlmsg_seq,
428 &ethtool_genl_family, 0,
429 ctx->ops->reply_cmd);
430 if (!ehdr) {
431 dev_put(dev);
432 ret = -EMSGSIZE;
433 goto out;
434 }
435 ret = ethnl_default_dump_one(skb, dev, ctx);
436 dev_put(dev);
437 if (ret < 0) {
438 genlmsg_cancel(skb, ehdr);
439 if (ret == -EOPNOTSUPP)
440 goto lock_and_cont;
441 if (likely(skb->len))
442 ret = skb->len;
443 goto out;
444 }
445 genlmsg_end(skb, ehdr);
446lock_and_cont:
447 rtnl_lock();
448 if (net->dev_base_seq != seq) {
449 s_idx = idx + 1;
450 goto restart_chain;
451 }
452cont:
453 idx++;
454 }
455
456 }
457 rtnl_unlock();
458
459out:
460 ctx->pos_hash = h;
461 ctx->pos_idx = idx;
462 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
463
464 return ret;
465}
466
467/* generic ->start() handler for GET requests */
468static int ethnl_default_start(struct netlink_callback *cb)
469{
470 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
471 struct ethnl_reply_data *reply_data;
472 const struct ethnl_request_ops *ops;
473 struct ethnl_req_info *req_info;
474 struct genlmsghdr *ghdr;
475 int ret;
476
477 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
478
479 ghdr = nlmsg_data(cb->nlh);
480 ops = ethnl_default_requests[ghdr->cmd];
481 if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
482 return -EOPNOTSUPP;
483 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
484 if (!req_info)
485 return -ENOMEM;
486 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
487 if (!reply_data) {
Dan Carpentera6dd0482020-01-08 08:39:48 +0300488 ret = -ENOMEM;
489 goto free_req_info;
Michal Kubecek728480f2019-12-27 15:55:38 +0100490 }
491
492 ret = ethnl_default_parse(req_info, cb->nlh, sock_net(cb->skb->sk), ops,
493 cb->extack, false);
494 if (req_info->dev) {
495 /* We ignore device specification in dump requests but as the
496 * same parser as for non-dump (doit) requests is used, it
497 * would take reference to the device if it finds one
498 */
499 dev_put(req_info->dev);
500 req_info->dev = NULL;
501 }
502 if (ret < 0)
Dan Carpentera6dd0482020-01-08 08:39:48 +0300503 goto free_reply_data;
Michal Kubecek728480f2019-12-27 15:55:38 +0100504
505 ctx->ops = ops;
506 ctx->req_info = req_info;
507 ctx->reply_data = reply_data;
508 ctx->pos_hash = 0;
509 ctx->pos_idx = 0;
510
511 return 0;
Dan Carpentera6dd0482020-01-08 08:39:48 +0300512
513free_reply_data:
514 kfree(reply_data);
515free_req_info:
516 kfree(req_info);
517
518 return ret;
Michal Kubecek728480f2019-12-27 15:55:38 +0100519}
520
521/* default ->done() handler for GET requests */
522static int ethnl_default_done(struct netlink_callback *cb)
523{
524 struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
525
526 kfree(ctx->reply_data);
527 kfree(ctx->req_info);
528
529 return 0;
530}
531
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100532static const struct ethnl_request_ops *
533ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
Michal Kubecek73286732019-12-27 15:56:03 +0100534 [ETHTOOL_MSG_LINKINFO_NTF] = &ethnl_linkinfo_request_ops,
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100535 [ETHTOOL_MSG_LINKMODES_NTF] = &ethnl_linkmodes_request_ops,
Michal Kubecek0bda7af2020-01-26 23:11:10 +0100536 [ETHTOOL_MSG_DEBUG_NTF] = &ethnl_debug_request_ops,
Michal Kubecek67bffa72020-01-26 23:11:19 +0100537 [ETHTOOL_MSG_WOL_NTF] = &ethnl_wol_request_ops,
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100538};
539
540/* default notification handler */
541static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
542 const void *data)
543{
544 struct ethnl_reply_data *reply_data;
545 const struct ethnl_request_ops *ops;
546 struct ethnl_req_info *req_info;
547 struct sk_buff *skb;
548 void *reply_payload;
549 int reply_len;
550 int ret;
551
552 if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
553 !ethnl_default_notify_ops[cmd],
554 "unexpected notification type %u\n", cmd))
555 return;
556 ops = ethnl_default_notify_ops[cmd];
557 req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
558 if (!req_info)
559 return;
560 reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
561 if (!reply_data) {
562 kfree(req_info);
563 return;
564 }
565
566 req_info->dev = dev;
567 req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
568
569 ethnl_init_reply_data(reply_data, ops, dev);
570 ret = ops->prepare_data(req_info, reply_data, NULL);
571 if (ret < 0)
572 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300573 ret = ops->reply_size(req_info, reply_data);
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100574 if (ret < 0)
575 goto err_cleanup;
Dan Carpenterd97772d2020-01-08 08:41:25 +0300576 reply_len = ret;
Michal Kubecek5cf2a542019-12-27 15:55:58 +0100577 ret = -ENOMEM;
578 skb = genlmsg_new(reply_len, GFP_KERNEL);
579 if (!skb)
580 goto err_cleanup;
581 reply_payload = ethnl_bcastmsg_put(skb, cmd);
582 if (!reply_payload)
583 goto err_skb;
584 ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
585 if (ret < 0)
586 goto err_msg;
587 ret = ops->fill_reply(skb, req_info, reply_data);
588 if (ret < 0)
589 goto err_msg;
590 if (ops->cleanup_data)
591 ops->cleanup_data(reply_data);
592
593 genlmsg_end(skb, reply_payload);
594 kfree(reply_data);
595 kfree(req_info);
596 ethnl_multicast(skb, dev);
597 return;
598
599err_msg:
600 WARN_ONCE(ret == -EMSGSIZE,
601 "calculated message payload length (%d) not sufficient\n",
602 reply_len);
603err_skb:
604 nlmsg_free(skb);
605err_cleanup:
606 if (ops->cleanup_data)
607 ops->cleanup_data(reply_data);
608 kfree(reply_data);
609 kfree(req_info);
610 return;
611}
612
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100613/* notifications */
614
615typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
616 const void *data);
617
618static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
Michal Kubecek73286732019-12-27 15:56:03 +0100619 [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify,
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100620 [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify,
Michal Kubecek0bda7af2020-01-26 23:11:10 +0100621 [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify,
Michal Kubecek67bffa72020-01-26 23:11:19 +0100622 [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify,
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100623};
624
625void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
626{
627 if (unlikely(!ethnl_ok))
628 return;
629 ASSERT_RTNL();
630
631 if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
632 ethnl_notify_handlers[cmd]))
633 ethnl_notify_handlers[cmd](dev, cmd, data);
634 else
635 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
636 cmd, netdev_name(dev));
637}
638EXPORT_SYMBOL(ethtool_notify);
639
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100640/* genetlink setup */
641
642static const struct genl_ops ethtool_genl_ops[] = {
Michal Kubecek71921692019-12-27 15:55:43 +0100643 {
644 .cmd = ETHTOOL_MSG_STRSET_GET,
645 .doit = ethnl_default_doit,
646 .start = ethnl_default_start,
647 .dumpit = ethnl_default_dumpit,
648 .done = ethnl_default_done,
649 },
Michal Kubecek459e0b82019-12-27 15:55:48 +0100650 {
651 .cmd = ETHTOOL_MSG_LINKINFO_GET,
652 .doit = ethnl_default_doit,
653 .start = ethnl_default_start,
654 .dumpit = ethnl_default_dumpit,
655 .done = ethnl_default_done,
656 },
Michal Kubeceka53f3d42019-12-27 15:55:53 +0100657 {
658 .cmd = ETHTOOL_MSG_LINKINFO_SET,
659 .flags = GENL_UNS_ADMIN_PERM,
660 .doit = ethnl_set_linkinfo,
661 },
Michal Kubecekf625aa92019-12-27 15:56:08 +0100662 {
663 .cmd = ETHTOOL_MSG_LINKMODES_GET,
664 .doit = ethnl_default_doit,
665 .start = ethnl_default_start,
666 .dumpit = ethnl_default_dumpit,
667 .done = ethnl_default_done,
668 },
Michal Kubecekbfbcfe22019-12-27 15:56:13 +0100669 {
670 .cmd = ETHTOOL_MSG_LINKMODES_SET,
671 .flags = GENL_UNS_ADMIN_PERM,
672 .doit = ethnl_set_linkmodes,
673 },
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100674 {
675 .cmd = ETHTOOL_MSG_LINKSTATE_GET,
676 .doit = ethnl_default_doit,
677 .start = ethnl_default_start,
678 .dumpit = ethnl_default_dumpit,
679 .done = ethnl_default_done,
680 },
Michal Kubecek6a94b8c2020-01-26 23:11:04 +0100681 {
682 .cmd = ETHTOOL_MSG_DEBUG_GET,
683 .doit = ethnl_default_doit,
684 .start = ethnl_default_start,
685 .dumpit = ethnl_default_dumpit,
686 .done = ethnl_default_done,
687 },
Michal Kubeceke54d04e2020-01-26 23:11:07 +0100688 {
689 .cmd = ETHTOOL_MSG_DEBUG_SET,
690 .flags = GENL_UNS_ADMIN_PERM,
691 .doit = ethnl_set_debug,
692 },
Michal Kubecek51ea22b2020-01-26 23:11:13 +0100693 {
694 .cmd = ETHTOOL_MSG_WOL_GET,
695 .flags = GENL_UNS_ADMIN_PERM,
696 .doit = ethnl_default_doit,
697 .start = ethnl_default_start,
698 .dumpit = ethnl_default_dumpit,
699 .done = ethnl_default_done,
700 },
Michal Kubecek8d425b12020-01-26 23:11:16 +0100701 {
702 .cmd = ETHTOOL_MSG_WOL_SET,
703 .flags = GENL_UNS_ADMIN_PERM,
704 .doit = ethnl_set_wol,
705 },
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100706};
707
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100708static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
709 [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
710};
711
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100712static struct genl_family ethtool_genl_family = {
713 .name = ETHTOOL_GENL_NAME,
714 .version = ETHTOOL_GENL_VERSION,
715 .netnsok = true,
716 .parallel_ops = true,
717 .ops = ethtool_genl_ops,
718 .n_ops = ARRAY_SIZE(ethtool_genl_ops),
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100719 .mcgrps = ethtool_nl_mcgrps,
720 .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps),
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100721};
722
723/* module setup */
724
725static int __init ethnl_init(void)
726{
727 int ret;
728
729 ret = genl_register_family(&ethtool_genl_family);
730 if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
731 return ret;
Michal Kubecek6b08d6c2019-12-27 15:55:33 +0100732 ethnl_ok = true;
Michal Kubecek2b4a8992019-12-27 15:55:18 +0100733
734 return 0;
735}
736
737subsys_initcall(ethnl_init);