ethtool: set pause parameters with PAUSE_SET request
[linux-block.git] / net / ethtool / netlink.c
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 static 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 static 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 };
233
234 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
235 {
236         return (struct ethnl_dump_ctx *)cb->ctx;
237 }
238
239 /**
240  * ethnl_default_parse() - Parse request message
241  * @req_info:    pointer to structure to put data into
242  * @nlhdr:       pointer to request message header
243  * @net:         request netns
244  * @request_ops: struct request_ops for request type
245  * @extack:      netlink extack for error reporting
246  * @require_dev: fail if no device identified in header
247  *
248  * Parse universal request header and call request specific ->parse_request()
249  * callback (if defined) to parse the rest of the message.
250  *
251  * Return: 0 on success or negative error code
252  */
253 static int ethnl_default_parse(struct ethnl_req_info *req_info,
254                                const struct nlmsghdr *nlhdr, struct net *net,
255                                const struct ethnl_request_ops *request_ops,
256                                struct netlink_ext_ack *extack, bool require_dev)
257 {
258         struct nlattr **tb;
259         int ret;
260
261         tb = kmalloc_array(request_ops->max_attr + 1, sizeof(tb[0]),
262                            GFP_KERNEL);
263         if (!tb)
264                 return -ENOMEM;
265
266         ret = nlmsg_parse(nlhdr, GENL_HDRLEN, tb, request_ops->max_attr,
267                           request_ops->request_policy, extack);
268         if (ret < 0)
269                 goto out;
270         ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
271                                          net, extack, require_dev);
272         if (ret < 0)
273                 goto out;
274
275         if (request_ops->parse_request) {
276                 ret = request_ops->parse_request(req_info, tb, extack);
277                 if (ret < 0)
278                         goto out;
279         }
280
281         ret = 0;
282 out:
283         kfree(tb);
284         return ret;
285 }
286
287 /**
288  * ethnl_init_reply_data() - Initialize reply data for GET request
289  * @reply_data: pointer to embedded struct ethnl_reply_data
290  * @ops:        instance of struct ethnl_request_ops describing the layout
291  * @dev:        network device to initialize the reply for
292  *
293  * Fills the reply data part with zeros and sets the dev member. Must be called
294  * before calling the ->fill_reply() callback (for each iteration when handling
295  * dump requests).
296  */
297 static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
298                                   const struct ethnl_request_ops *ops,
299                                   struct net_device *dev)
300 {
301         memset(reply_data, 0, ops->reply_data_size);
302         reply_data->dev = dev;
303 }
304
305 /* default ->doit() handler for GET type requests */
306 static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
307 {
308         struct ethnl_reply_data *reply_data = NULL;
309         struct ethnl_req_info *req_info = NULL;
310         const u8 cmd = info->genlhdr->cmd;
311         const struct ethnl_request_ops *ops;
312         struct sk_buff *rskb;
313         void *reply_payload;
314         int reply_len;
315         int ret;
316
317         ops = ethnl_default_requests[cmd];
318         if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
319                 return -EOPNOTSUPP;
320         req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
321         if (!req_info)
322                 return -ENOMEM;
323         reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
324         if (!reply_data) {
325                 kfree(req_info);
326                 return -ENOMEM;
327         }
328
329         ret = ethnl_default_parse(req_info, info->nlhdr, genl_info_net(info), ops,
330                                   info->extack, !ops->allow_nodev_do);
331         if (ret < 0)
332                 goto err_dev;
333         ethnl_init_reply_data(reply_data, ops, req_info->dev);
334
335         rtnl_lock();
336         ret = ops->prepare_data(req_info, reply_data, info);
337         rtnl_unlock();
338         if (ret < 0)
339                 goto err_cleanup;
340         ret = ops->reply_size(req_info, reply_data);
341         if (ret < 0)
342                 goto err_cleanup;
343         reply_len = ret;
344         ret = -ENOMEM;
345         rskb = ethnl_reply_init(reply_len, req_info->dev, ops->reply_cmd,
346                                 ops->hdr_attr, info, &reply_payload);
347         if (!rskb)
348                 goto err_cleanup;
349         ret = ops->fill_reply(rskb, req_info, reply_data);
350         if (ret < 0)
351                 goto err_msg;
352         if (ops->cleanup_data)
353                 ops->cleanup_data(reply_data);
354
355         genlmsg_end(rskb, reply_payload);
356         if (req_info->dev)
357                 dev_put(req_info->dev);
358         kfree(reply_data);
359         kfree(req_info);
360         return genlmsg_reply(rskb, info);
361
362 err_msg:
363         WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
364         nlmsg_free(rskb);
365 err_cleanup:
366         if (ops->cleanup_data)
367                 ops->cleanup_data(reply_data);
368 err_dev:
369         if (req_info->dev)
370                 dev_put(req_info->dev);
371         kfree(reply_data);
372         kfree(req_info);
373         return ret;
374 }
375
376 static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
377                                   const struct ethnl_dump_ctx *ctx)
378 {
379         int ret;
380
381         ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
382         rtnl_lock();
383         ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
384         rtnl_unlock();
385         if (ret < 0)
386                 goto out;
387         ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
388         if (ret < 0)
389                 goto out;
390         ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
391
392 out:
393         if (ctx->ops->cleanup_data)
394                 ctx->ops->cleanup_data(ctx->reply_data);
395         ctx->reply_data->dev = NULL;
396         return ret;
397 }
398
399 /* Default ->dumpit() handler for GET requests. Device iteration copied from
400  * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
401  * persistence as we cannot guarantee to hold RTNL lock through the whole
402  * function as rtnetnlink does.
403  */
404 static int ethnl_default_dumpit(struct sk_buff *skb,
405                                 struct netlink_callback *cb)
406 {
407         struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
408         struct net *net = sock_net(skb->sk);
409         int s_idx = ctx->pos_idx;
410         int h, idx = 0;
411         int ret = 0;
412         void *ehdr;
413
414         rtnl_lock();
415         for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
416                 struct hlist_head *head;
417                 struct net_device *dev;
418                 unsigned int seq;
419
420                 head = &net->dev_index_head[h];
421
422 restart_chain:
423                 seq = net->dev_base_seq;
424                 cb->seq = seq;
425                 idx = 0;
426                 hlist_for_each_entry(dev, head, index_hlist) {
427                         if (idx < s_idx)
428                                 goto cont;
429                         dev_hold(dev);
430                         rtnl_unlock();
431
432                         ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
433                                            cb->nlh->nlmsg_seq,
434                                            &ethtool_genl_family, 0,
435                                            ctx->ops->reply_cmd);
436                         if (!ehdr) {
437                                 dev_put(dev);
438                                 ret = -EMSGSIZE;
439                                 goto out;
440                         }
441                         ret = ethnl_default_dump_one(skb, dev, ctx);
442                         dev_put(dev);
443                         if (ret < 0) {
444                                 genlmsg_cancel(skb, ehdr);
445                                 if (ret == -EOPNOTSUPP)
446                                         goto lock_and_cont;
447                                 if (likely(skb->len))
448                                         ret = skb->len;
449                                 goto out;
450                         }
451                         genlmsg_end(skb, ehdr);
452 lock_and_cont:
453                         rtnl_lock();
454                         if (net->dev_base_seq != seq) {
455                                 s_idx = idx + 1;
456                                 goto restart_chain;
457                         }
458 cont:
459                         idx++;
460                 }
461
462         }
463         rtnl_unlock();
464
465 out:
466         ctx->pos_hash = h;
467         ctx->pos_idx = idx;
468         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
469
470         return ret;
471 }
472
473 /* generic ->start() handler for GET requests */
474 static int ethnl_default_start(struct netlink_callback *cb)
475 {
476         struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
477         struct ethnl_reply_data *reply_data;
478         const struct ethnl_request_ops *ops;
479         struct ethnl_req_info *req_info;
480         struct genlmsghdr *ghdr;
481         int ret;
482
483         BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
484
485         ghdr = nlmsg_data(cb->nlh);
486         ops = ethnl_default_requests[ghdr->cmd];
487         if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
488                 return -EOPNOTSUPP;
489         req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
490         if (!req_info)
491                 return -ENOMEM;
492         reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
493         if (!reply_data) {
494                 ret = -ENOMEM;
495                 goto free_req_info;
496         }
497
498         ret = ethnl_default_parse(req_info, cb->nlh, sock_net(cb->skb->sk), ops,
499                                   cb->extack, false);
500         if (req_info->dev) {
501                 /* We ignore device specification in dump requests but as the
502                  * same parser as for non-dump (doit) requests is used, it
503                  * would take reference to the device if it finds one
504                  */
505                 dev_put(req_info->dev);
506                 req_info->dev = NULL;
507         }
508         if (ret < 0)
509                 goto free_reply_data;
510
511         ctx->ops = ops;
512         ctx->req_info = req_info;
513         ctx->reply_data = reply_data;
514         ctx->pos_hash = 0;
515         ctx->pos_idx = 0;
516
517         return 0;
518
519 free_reply_data:
520         kfree(reply_data);
521 free_req_info:
522         kfree(req_info);
523
524         return ret;
525 }
526
527 /* default ->done() handler for GET requests */
528 static int ethnl_default_done(struct netlink_callback *cb)
529 {
530         struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
531
532         kfree(ctx->reply_data);
533         kfree(ctx->req_info);
534
535         return 0;
536 }
537
538 static const struct ethnl_request_ops *
539 ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
540         [ETHTOOL_MSG_LINKINFO_NTF]      = &ethnl_linkinfo_request_ops,
541         [ETHTOOL_MSG_LINKMODES_NTF]     = &ethnl_linkmodes_request_ops,
542         [ETHTOOL_MSG_DEBUG_NTF]         = &ethnl_debug_request_ops,
543         [ETHTOOL_MSG_WOL_NTF]           = &ethnl_wol_request_ops,
544         [ETHTOOL_MSG_FEATURES_NTF]      = &ethnl_features_request_ops,
545         [ETHTOOL_MSG_PRIVFLAGS_NTF]     = &ethnl_privflags_request_ops,
546         [ETHTOOL_MSG_RINGS_NTF]         = &ethnl_rings_request_ops,
547         [ETHTOOL_MSG_CHANNELS_NTF]      = &ethnl_channels_request_ops,
548         [ETHTOOL_MSG_COALESCE_NTF]      = &ethnl_coalesce_request_ops,
549 };
550
551 /* default notification handler */
552 static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
553                                  const void *data)
554 {
555         struct ethnl_reply_data *reply_data;
556         const struct ethnl_request_ops *ops;
557         struct ethnl_req_info *req_info;
558         struct sk_buff *skb;
559         void *reply_payload;
560         int reply_len;
561         int ret;
562
563         if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
564                       !ethnl_default_notify_ops[cmd],
565                       "unexpected notification type %u\n", cmd))
566                 return;
567         ops = ethnl_default_notify_ops[cmd];
568         req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
569         if (!req_info)
570                 return;
571         reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
572         if (!reply_data) {
573                 kfree(req_info);
574                 return;
575         }
576
577         req_info->dev = dev;
578         req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
579
580         ethnl_init_reply_data(reply_data, ops, dev);
581         ret = ops->prepare_data(req_info, reply_data, NULL);
582         if (ret < 0)
583                 goto err_cleanup;
584         ret = ops->reply_size(req_info, reply_data);
585         if (ret < 0)
586                 goto err_cleanup;
587         reply_len = ret;
588         ret = -ENOMEM;
589         skb = genlmsg_new(reply_len, GFP_KERNEL);
590         if (!skb)
591                 goto err_cleanup;
592         reply_payload = ethnl_bcastmsg_put(skb, cmd);
593         if (!reply_payload)
594                 goto err_skb;
595         ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
596         if (ret < 0)
597                 goto err_msg;
598         ret = ops->fill_reply(skb, req_info, reply_data);
599         if (ret < 0)
600                 goto err_msg;
601         if (ops->cleanup_data)
602                 ops->cleanup_data(reply_data);
603
604         genlmsg_end(skb, reply_payload);
605         kfree(reply_data);
606         kfree(req_info);
607         ethnl_multicast(skb, dev);
608         return;
609
610 err_msg:
611         WARN_ONCE(ret == -EMSGSIZE,
612                   "calculated message payload length (%d) not sufficient\n",
613                   reply_len);
614 err_skb:
615         nlmsg_free(skb);
616 err_cleanup:
617         if (ops->cleanup_data)
618                 ops->cleanup_data(reply_data);
619         kfree(reply_data);
620         kfree(req_info);
621         return;
622 }
623
624 /* notifications */
625
626 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
627                                        const void *data);
628
629 static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
630         [ETHTOOL_MSG_LINKINFO_NTF]      = ethnl_default_notify,
631         [ETHTOOL_MSG_LINKMODES_NTF]     = ethnl_default_notify,
632         [ETHTOOL_MSG_DEBUG_NTF]         = ethnl_default_notify,
633         [ETHTOOL_MSG_WOL_NTF]           = ethnl_default_notify,
634         [ETHTOOL_MSG_FEATURES_NTF]      = ethnl_default_notify,
635         [ETHTOOL_MSG_PRIVFLAGS_NTF]     = ethnl_default_notify,
636         [ETHTOOL_MSG_RINGS_NTF]         = ethnl_default_notify,
637         [ETHTOOL_MSG_CHANNELS_NTF]      = ethnl_default_notify,
638         [ETHTOOL_MSG_COALESCE_NTF]      = ethnl_default_notify,
639 };
640
641 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
642 {
643         if (unlikely(!ethnl_ok))
644                 return;
645         ASSERT_RTNL();
646
647         if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
648                    ethnl_notify_handlers[cmd]))
649                 ethnl_notify_handlers[cmd](dev, cmd, data);
650         else
651                 WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
652                           cmd, netdev_name(dev));
653 }
654 EXPORT_SYMBOL(ethtool_notify);
655
656 static void ethnl_notify_features(struct netdev_notifier_info *info)
657 {
658         struct net_device *dev = netdev_notifier_info_to_dev(info);
659
660         ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
661 }
662
663 static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
664                               void *ptr)
665 {
666         switch (event) {
667         case NETDEV_FEAT_CHANGE:
668                 ethnl_notify_features(ptr);
669                 break;
670         }
671
672         return NOTIFY_DONE;
673 }
674
675 static struct notifier_block ethnl_netdev_notifier = {
676         .notifier_call = ethnl_netdev_event,
677 };
678
679 /* genetlink setup */
680
681 static const struct genl_ops ethtool_genl_ops[] = {
682         {
683                 .cmd    = ETHTOOL_MSG_STRSET_GET,
684                 .doit   = ethnl_default_doit,
685                 .start  = ethnl_default_start,
686                 .dumpit = ethnl_default_dumpit,
687                 .done   = ethnl_default_done,
688         },
689         {
690                 .cmd    = ETHTOOL_MSG_LINKINFO_GET,
691                 .doit   = ethnl_default_doit,
692                 .start  = ethnl_default_start,
693                 .dumpit = ethnl_default_dumpit,
694                 .done   = ethnl_default_done,
695         },
696         {
697                 .cmd    = ETHTOOL_MSG_LINKINFO_SET,
698                 .flags  = GENL_UNS_ADMIN_PERM,
699                 .doit   = ethnl_set_linkinfo,
700         },
701         {
702                 .cmd    = ETHTOOL_MSG_LINKMODES_GET,
703                 .doit   = ethnl_default_doit,
704                 .start  = ethnl_default_start,
705                 .dumpit = ethnl_default_dumpit,
706                 .done   = ethnl_default_done,
707         },
708         {
709                 .cmd    = ETHTOOL_MSG_LINKMODES_SET,
710                 .flags  = GENL_UNS_ADMIN_PERM,
711                 .doit   = ethnl_set_linkmodes,
712         },
713         {
714                 .cmd    = ETHTOOL_MSG_LINKSTATE_GET,
715                 .doit   = ethnl_default_doit,
716                 .start  = ethnl_default_start,
717                 .dumpit = ethnl_default_dumpit,
718                 .done   = ethnl_default_done,
719         },
720         {
721                 .cmd    = ETHTOOL_MSG_DEBUG_GET,
722                 .doit   = ethnl_default_doit,
723                 .start  = ethnl_default_start,
724                 .dumpit = ethnl_default_dumpit,
725                 .done   = ethnl_default_done,
726         },
727         {
728                 .cmd    = ETHTOOL_MSG_DEBUG_SET,
729                 .flags  = GENL_UNS_ADMIN_PERM,
730                 .doit   = ethnl_set_debug,
731         },
732         {
733                 .cmd    = ETHTOOL_MSG_WOL_GET,
734                 .flags  = GENL_UNS_ADMIN_PERM,
735                 .doit   = ethnl_default_doit,
736                 .start  = ethnl_default_start,
737                 .dumpit = ethnl_default_dumpit,
738                 .done   = ethnl_default_done,
739         },
740         {
741                 .cmd    = ETHTOOL_MSG_WOL_SET,
742                 .flags  = GENL_UNS_ADMIN_PERM,
743                 .doit   = ethnl_set_wol,
744         },
745         {
746                 .cmd    = ETHTOOL_MSG_FEATURES_GET,
747                 .doit   = ethnl_default_doit,
748                 .start  = ethnl_default_start,
749                 .dumpit = ethnl_default_dumpit,
750                 .done   = ethnl_default_done,
751         },
752         {
753                 .cmd    = ETHTOOL_MSG_FEATURES_SET,
754                 .flags  = GENL_UNS_ADMIN_PERM,
755                 .doit   = ethnl_set_features,
756         },
757         {
758                 .cmd    = ETHTOOL_MSG_PRIVFLAGS_GET,
759                 .doit   = ethnl_default_doit,
760                 .start  = ethnl_default_start,
761                 .dumpit = ethnl_default_dumpit,
762                 .done   = ethnl_default_done,
763         },
764         {
765                 .cmd    = ETHTOOL_MSG_PRIVFLAGS_SET,
766                 .flags  = GENL_UNS_ADMIN_PERM,
767                 .doit   = ethnl_set_privflags,
768         },
769         {
770                 .cmd    = ETHTOOL_MSG_RINGS_GET,
771                 .doit   = ethnl_default_doit,
772                 .start  = ethnl_default_start,
773                 .dumpit = ethnl_default_dumpit,
774                 .done   = ethnl_default_done,
775         },
776         {
777                 .cmd    = ETHTOOL_MSG_RINGS_SET,
778                 .flags  = GENL_UNS_ADMIN_PERM,
779                 .doit   = ethnl_set_rings,
780         },
781         {
782                 .cmd    = ETHTOOL_MSG_CHANNELS_GET,
783                 .doit   = ethnl_default_doit,
784                 .start  = ethnl_default_start,
785                 .dumpit = ethnl_default_dumpit,
786                 .done   = ethnl_default_done,
787         },
788         {
789                 .cmd    = ETHTOOL_MSG_CHANNELS_SET,
790                 .flags  = GENL_UNS_ADMIN_PERM,
791                 .doit   = ethnl_set_channels,
792         },
793         {
794                 .cmd    = ETHTOOL_MSG_COALESCE_GET,
795                 .doit   = ethnl_default_doit,
796                 .start  = ethnl_default_start,
797                 .dumpit = ethnl_default_dumpit,
798                 .done   = ethnl_default_done,
799         },
800         {
801                 .cmd    = ETHTOOL_MSG_COALESCE_SET,
802                 .flags  = GENL_UNS_ADMIN_PERM,
803                 .doit   = ethnl_set_coalesce,
804         },
805         {
806                 .cmd    = ETHTOOL_MSG_PAUSE_GET,
807                 .doit   = ethnl_default_doit,
808                 .start  = ethnl_default_start,
809                 .dumpit = ethnl_default_dumpit,
810                 .done   = ethnl_default_done,
811         },
812         {
813                 .cmd    = ETHTOOL_MSG_PAUSE_SET,
814                 .flags  = GENL_UNS_ADMIN_PERM,
815                 .doit   = ethnl_set_pause,
816         },
817 };
818
819 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
820         [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
821 };
822
823 static struct genl_family ethtool_genl_family = {
824         .name           = ETHTOOL_GENL_NAME,
825         .version        = ETHTOOL_GENL_VERSION,
826         .netnsok        = true,
827         .parallel_ops   = true,
828         .ops            = ethtool_genl_ops,
829         .n_ops          = ARRAY_SIZE(ethtool_genl_ops),
830         .mcgrps         = ethtool_nl_mcgrps,
831         .n_mcgrps       = ARRAY_SIZE(ethtool_nl_mcgrps),
832 };
833
834 /* module setup */
835
836 static int __init ethnl_init(void)
837 {
838         int ret;
839
840         ret = genl_register_family(&ethtool_genl_family);
841         if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
842                 return ret;
843         ethnl_ok = true;
844
845         ret = register_netdevice_notifier(&ethnl_netdev_notifier);
846         WARN(ret < 0, "ethtool: net device notifier registration failed");
847         return ret;
848 }
849
850 subsys_initcall(ethnl_init);