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