1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net-sysfs.c - network device class and attributes
4 *
5 * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
6 */
7
8 #include <linux/capability.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sched/isolation.h>
15 #include <linux/nsproxy.h>
16 #include <net/sock.h>
17 #include <net/net_namespace.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/vmalloc.h>
20 #include <linux/export.h>
21 #include <linux/jiffies.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of.h>
24 #include <linux/of_net.h>
25 #include <linux/cpu.h>
26
27 #include "net-sysfs.h"
28
29 #ifdef CONFIG_SYSFS
30 static const char fmt_hex[] = "%#x\n";
31 static const char fmt_dec[] = "%d\n";
32 static const char fmt_uint[] = "%u\n";
33 static const char fmt_ulong[] = "%lu\n";
34 static const char fmt_u64[] = "%llu\n";
35
dev_isalive(const struct net_device * dev)36 static inline int dev_isalive(const struct net_device *dev)
37 {
38 return dev->reg_state <= NETREG_REGISTERED;
39 }
40
41 /* use same locking rules as GIF* ioctl's */
netdev_show(const struct device * dev,struct device_attribute * attr,char * buf,ssize_t (* format)(const struct net_device *,char *))42 static ssize_t netdev_show(const struct device *dev,
43 struct device_attribute *attr, char *buf,
44 ssize_t (*format)(const struct net_device *, char *))
45 {
46 struct net_device *ndev = to_net_dev(dev);
47 ssize_t ret = -EINVAL;
48
49 read_lock(&dev_base_lock);
50 if (dev_isalive(ndev))
51 ret = (*format)(ndev, buf);
52 read_unlock(&dev_base_lock);
53
54 return ret;
55 }
56
57 /* generate a show function for simple field */
58 #define NETDEVICE_SHOW(field, format_string) \
59 static ssize_t format_##field(const struct net_device *dev, char *buf) \
60 { \
61 return sprintf(buf, format_string, dev->field); \
62 } \
63 static ssize_t field##_show(struct device *dev, \
64 struct device_attribute *attr, char *buf) \
65 { \
66 return netdev_show(dev, attr, buf, format_##field); \
67 } \
68
69 #define NETDEVICE_SHOW_RO(field, format_string) \
70 NETDEVICE_SHOW(field, format_string); \
71 static DEVICE_ATTR_RO(field)
72
73 #define NETDEVICE_SHOW_RW(field, format_string) \
74 NETDEVICE_SHOW(field, format_string); \
75 static DEVICE_ATTR_RW(field)
76
77 /* use same locking and permission rules as SIF* ioctl's */
netdev_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len,int (* set)(struct net_device *,unsigned long))78 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
79 const char *buf, size_t len,
80 int (*set)(struct net_device *, unsigned long))
81 {
82 struct net_device *netdev = to_net_dev(dev);
83 struct net *net = dev_net(netdev);
84 unsigned long new;
85 int ret;
86
87 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
88 return -EPERM;
89
90 ret = kstrtoul(buf, 0, &new);
91 if (ret)
92 goto err;
93
94 if (!rtnl_trylock())
95 return restart_syscall();
96
97 if (dev_isalive(netdev)) {
98 ret = (*set)(netdev, new);
99 if (ret == 0)
100 ret = len;
101 }
102 rtnl_unlock();
103 err:
104 return ret;
105 }
106
107 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
108 NETDEVICE_SHOW_RO(dev_port, fmt_dec);
109 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
110 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
111 NETDEVICE_SHOW_RO(ifindex, fmt_dec);
112 NETDEVICE_SHOW_RO(type, fmt_dec);
113 NETDEVICE_SHOW_RO(link_mode, fmt_dec);
114
iflink_show(struct device * dev,struct device_attribute * attr,char * buf)115 static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
116 char *buf)
117 {
118 struct net_device *ndev = to_net_dev(dev);
119
120 return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
121 }
122 static DEVICE_ATTR_RO(iflink);
123
format_name_assign_type(const struct net_device * dev,char * buf)124 static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
125 {
126 return sprintf(buf, fmt_dec, dev->name_assign_type);
127 }
128
name_assign_type_show(struct device * dev,struct device_attribute * attr,char * buf)129 static ssize_t name_assign_type_show(struct device *dev,
130 struct device_attribute *attr,
131 char *buf)
132 {
133 struct net_device *ndev = to_net_dev(dev);
134 ssize_t ret = -EINVAL;
135
136 if (ndev->name_assign_type != NET_NAME_UNKNOWN)
137 ret = netdev_show(dev, attr, buf, format_name_assign_type);
138
139 return ret;
140 }
141 static DEVICE_ATTR_RO(name_assign_type);
142
143 /* use same locking rules as GIFHWADDR ioctl's */
address_show(struct device * dev,struct device_attribute * attr,char * buf)144 static ssize_t address_show(struct device *dev, struct device_attribute *attr,
145 char *buf)
146 {
147 struct net_device *ndev = to_net_dev(dev);
148 ssize_t ret = -EINVAL;
149
150 read_lock(&dev_base_lock);
151 if (dev_isalive(ndev))
152 ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
153 read_unlock(&dev_base_lock);
154 return ret;
155 }
156 static DEVICE_ATTR_RO(address);
157
broadcast_show(struct device * dev,struct device_attribute * attr,char * buf)158 static ssize_t broadcast_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
160 {
161 struct net_device *ndev = to_net_dev(dev);
162
163 if (dev_isalive(ndev))
164 return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
165 return -EINVAL;
166 }
167 static DEVICE_ATTR_RO(broadcast);
168
change_carrier(struct net_device * dev,unsigned long new_carrier)169 static int change_carrier(struct net_device *dev, unsigned long new_carrier)
170 {
171 if (!netif_running(dev))
172 return -EINVAL;
173 return dev_change_carrier(dev, (bool)new_carrier);
174 }
175
carrier_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)176 static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
177 const char *buf, size_t len)
178 {
179 struct net_device *netdev = to_net_dev(dev);
180
181 /* The check is also done in change_carrier; this helps returning early
182 * without hitting the trylock/restart in netdev_store.
183 */
184 if (!netdev->netdev_ops->ndo_change_carrier)
185 return -EOPNOTSUPP;
186
187 return netdev_store(dev, attr, buf, len, change_carrier);
188 }
189
carrier_show(struct device * dev,struct device_attribute * attr,char * buf)190 static ssize_t carrier_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192 {
193 struct net_device *netdev = to_net_dev(dev);
194
195 if (netif_running(netdev))
196 return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
197
198 return -EINVAL;
199 }
200 static DEVICE_ATTR_RW(carrier);
201
speed_show(struct device * dev,struct device_attribute * attr,char * buf)202 static ssize_t speed_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204 {
205 struct net_device *netdev = to_net_dev(dev);
206 int ret = -EINVAL;
207
208 /* The check is also done in __ethtool_get_link_ksettings; this helps
209 * returning early without hitting the trylock/restart below.
210 */
211 if (!netdev->ethtool_ops->get_link_ksettings)
212 return ret;
213
214 if (!rtnl_trylock())
215 return restart_syscall();
216
217 if (netif_running(netdev)) {
218 struct ethtool_link_ksettings cmd;
219
220 if (!__ethtool_get_link_ksettings(netdev, &cmd))
221 ret = sprintf(buf, fmt_dec, cmd.base.speed);
222 }
223 rtnl_unlock();
224 return ret;
225 }
226 static DEVICE_ATTR_RO(speed);
227
duplex_show(struct device * dev,struct device_attribute * attr,char * buf)228 static ssize_t duplex_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
230 {
231 struct net_device *netdev = to_net_dev(dev);
232 int ret = -EINVAL;
233
234 /* The check is also done in __ethtool_get_link_ksettings; this helps
235 * returning early without hitting the trylock/restart below.
236 */
237 if (!netdev->ethtool_ops->get_link_ksettings)
238 return ret;
239
240 if (!rtnl_trylock())
241 return restart_syscall();
242
243 if (netif_running(netdev)) {
244 struct ethtool_link_ksettings cmd;
245
246 if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
247 const char *duplex;
248
249 switch (cmd.base.duplex) {
250 case DUPLEX_HALF:
251 duplex = "half";
252 break;
253 case DUPLEX_FULL:
254 duplex = "full";
255 break;
256 default:
257 duplex = "unknown";
258 break;
259 }
260 ret = sprintf(buf, "%s\n", duplex);
261 }
262 }
263 rtnl_unlock();
264 return ret;
265 }
266 static DEVICE_ATTR_RO(duplex);
267
testing_show(struct device * dev,struct device_attribute * attr,char * buf)268 static ssize_t testing_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270 {
271 struct net_device *netdev = to_net_dev(dev);
272
273 if (netif_running(netdev))
274 return sprintf(buf, fmt_dec, !!netif_testing(netdev));
275
276 return -EINVAL;
277 }
278 static DEVICE_ATTR_RO(testing);
279
dormant_show(struct device * dev,struct device_attribute * attr,char * buf)280 static ssize_t dormant_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282 {
283 struct net_device *netdev = to_net_dev(dev);
284
285 if (netif_running(netdev))
286 return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
287
288 return -EINVAL;
289 }
290 static DEVICE_ATTR_RO(dormant);
291
292 static const char *const operstates[] = {
293 "unknown",
294 "notpresent", /* currently unused */
295 "down",
296 "lowerlayerdown",
297 "testing",
298 "dormant",
299 "up"
300 };
301
operstate_show(struct device * dev,struct device_attribute * attr,char * buf)302 static ssize_t operstate_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
304 {
305 const struct net_device *netdev = to_net_dev(dev);
306 unsigned char operstate;
307
308 read_lock(&dev_base_lock);
309 operstate = netdev->operstate;
310 if (!netif_running(netdev))
311 operstate = IF_OPER_DOWN;
312 read_unlock(&dev_base_lock);
313
314 if (operstate >= ARRAY_SIZE(operstates))
315 return -EINVAL; /* should not happen */
316
317 return sprintf(buf, "%s\n", operstates[operstate]);
318 }
319 static DEVICE_ATTR_RO(operstate);
320
carrier_changes_show(struct device * dev,struct device_attribute * attr,char * buf)321 static ssize_t carrier_changes_show(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324 {
325 struct net_device *netdev = to_net_dev(dev);
326
327 return sprintf(buf, fmt_dec,
328 atomic_read(&netdev->carrier_up_count) +
329 atomic_read(&netdev->carrier_down_count));
330 }
331 static DEVICE_ATTR_RO(carrier_changes);
332
carrier_up_count_show(struct device * dev,struct device_attribute * attr,char * buf)333 static ssize_t carrier_up_count_show(struct device *dev,
334 struct device_attribute *attr,
335 char *buf)
336 {
337 struct net_device *netdev = to_net_dev(dev);
338
339 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
340 }
341 static DEVICE_ATTR_RO(carrier_up_count);
342
carrier_down_count_show(struct device * dev,struct device_attribute * attr,char * buf)343 static ssize_t carrier_down_count_show(struct device *dev,
344 struct device_attribute *attr,
345 char *buf)
346 {
347 struct net_device *netdev = to_net_dev(dev);
348
349 return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
350 }
351 static DEVICE_ATTR_RO(carrier_down_count);
352
353 /* read-write attributes */
354
change_mtu(struct net_device * dev,unsigned long new_mtu)355 static int change_mtu(struct net_device *dev, unsigned long new_mtu)
356 {
357 return dev_set_mtu(dev, (int)new_mtu);
358 }
359
mtu_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)360 static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t len)
362 {
363 return netdev_store(dev, attr, buf, len, change_mtu);
364 }
365 NETDEVICE_SHOW_RW(mtu, fmt_dec);
366
change_flags(struct net_device * dev,unsigned long new_flags)367 static int change_flags(struct net_device *dev, unsigned long new_flags)
368 {
369 return dev_change_flags(dev, (unsigned int)new_flags, NULL);
370 }
371
flags_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)372 static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t len)
374 {
375 return netdev_store(dev, attr, buf, len, change_flags);
376 }
377 NETDEVICE_SHOW_RW(flags, fmt_hex);
378
tx_queue_len_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)379 static ssize_t tx_queue_len_store(struct device *dev,
380 struct device_attribute *attr,
381 const char *buf, size_t len)
382 {
383 if (!capable(CAP_NET_ADMIN))
384 return -EPERM;
385
386 return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
387 }
388 NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
389
change_gro_flush_timeout(struct net_device * dev,unsigned long val)390 static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
391 {
392 WRITE_ONCE(dev->gro_flush_timeout, val);
393 return 0;
394 }
395
gro_flush_timeout_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)396 static ssize_t gro_flush_timeout_store(struct device *dev,
397 struct device_attribute *attr,
398 const char *buf, size_t len)
399 {
400 if (!capable(CAP_NET_ADMIN))
401 return -EPERM;
402
403 return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
404 }
405 NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
406
change_napi_defer_hard_irqs(struct net_device * dev,unsigned long val)407 static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val)
408 {
409 if (val > S32_MAX)
410 return -ERANGE;
411
412 WRITE_ONCE(dev->napi_defer_hard_irqs, val);
413 return 0;
414 }
415
napi_defer_hard_irqs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)416 static ssize_t napi_defer_hard_irqs_store(struct device *dev,
417 struct device_attribute *attr,
418 const char *buf, size_t len)
419 {
420 if (!capable(CAP_NET_ADMIN))
421 return -EPERM;
422
423 return netdev_store(dev, attr, buf, len, change_napi_defer_hard_irqs);
424 }
425 NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_uint);
426
ifalias_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)427 static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
428 const char *buf, size_t len)
429 {
430 struct net_device *netdev = to_net_dev(dev);
431 struct net *net = dev_net(netdev);
432 size_t count = len;
433 ssize_t ret = 0;
434
435 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
436 return -EPERM;
437
438 /* ignore trailing newline */
439 if (len > 0 && buf[len - 1] == '\n')
440 --count;
441
442 if (!rtnl_trylock())
443 return restart_syscall();
444
445 if (dev_isalive(netdev)) {
446 ret = dev_set_alias(netdev, buf, count);
447 if (ret < 0)
448 goto err;
449 ret = len;
450 netdev_state_change(netdev);
451 }
452 err:
453 rtnl_unlock();
454
455 return ret;
456 }
457
ifalias_show(struct device * dev,struct device_attribute * attr,char * buf)458 static ssize_t ifalias_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
460 {
461 const struct net_device *netdev = to_net_dev(dev);
462 char tmp[IFALIASZ];
463 ssize_t ret = 0;
464
465 ret = dev_get_alias(netdev, tmp, sizeof(tmp));
466 if (ret > 0)
467 ret = sprintf(buf, "%s\n", tmp);
468 return ret;
469 }
470 static DEVICE_ATTR_RW(ifalias);
471
change_group(struct net_device * dev,unsigned long new_group)472 static int change_group(struct net_device *dev, unsigned long new_group)
473 {
474 dev_set_group(dev, (int)new_group);
475 return 0;
476 }
477
group_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)478 static ssize_t group_store(struct device *dev, struct device_attribute *attr,
479 const char *buf, size_t len)
480 {
481 return netdev_store(dev, attr, buf, len, change_group);
482 }
483 NETDEVICE_SHOW(group, fmt_dec);
484 static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
485
change_proto_down(struct net_device * dev,unsigned long proto_down)486 static int change_proto_down(struct net_device *dev, unsigned long proto_down)
487 {
488 return dev_change_proto_down(dev, (bool)proto_down);
489 }
490
proto_down_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)491 static ssize_t proto_down_store(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t len)
494 {
495 struct net_device *netdev = to_net_dev(dev);
496
497 /* The check is also done in change_proto_down; this helps returning
498 * early without hitting the trylock/restart in netdev_store.
499 */
500 if (!netdev->netdev_ops->ndo_change_proto_down)
501 return -EOPNOTSUPP;
502
503 return netdev_store(dev, attr, buf, len, change_proto_down);
504 }
505 NETDEVICE_SHOW_RW(proto_down, fmt_dec);
506
phys_port_id_show(struct device * dev,struct device_attribute * attr,char * buf)507 static ssize_t phys_port_id_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509 {
510 struct net_device *netdev = to_net_dev(dev);
511 ssize_t ret = -EINVAL;
512
513 /* The check is also done in dev_get_phys_port_id; this helps returning
514 * early without hitting the trylock/restart below.
515 */
516 if (!netdev->netdev_ops->ndo_get_phys_port_id)
517 return -EOPNOTSUPP;
518
519 if (!rtnl_trylock())
520 return restart_syscall();
521
522 if (dev_isalive(netdev)) {
523 struct netdev_phys_item_id ppid;
524
525 ret = dev_get_phys_port_id(netdev, &ppid);
526 if (!ret)
527 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
528 }
529 rtnl_unlock();
530
531 return ret;
532 }
533 static DEVICE_ATTR_RO(phys_port_id);
534
phys_port_name_show(struct device * dev,struct device_attribute * attr,char * buf)535 static ssize_t phys_port_name_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
537 {
538 struct net_device *netdev = to_net_dev(dev);
539 ssize_t ret = -EINVAL;
540
541 /* The checks are also done in dev_get_phys_port_name; this helps
542 * returning early without hitting the trylock/restart below.
543 */
544 if (!netdev->netdev_ops->ndo_get_phys_port_name &&
545 !netdev->netdev_ops->ndo_get_devlink_port)
546 return -EOPNOTSUPP;
547
548 if (!rtnl_trylock())
549 return restart_syscall();
550
551 if (dev_isalive(netdev)) {
552 char name[IFNAMSIZ];
553
554 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
555 if (!ret)
556 ret = sprintf(buf, "%s\n", name);
557 }
558 rtnl_unlock();
559
560 return ret;
561 }
562 static DEVICE_ATTR_RO(phys_port_name);
563
phys_switch_id_show(struct device * dev,struct device_attribute * attr,char * buf)564 static ssize_t phys_switch_id_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566 {
567 struct net_device *netdev = to_net_dev(dev);
568 ssize_t ret = -EINVAL;
569
570 /* The checks are also done in dev_get_phys_port_name; this helps
571 * returning early without hitting the trylock/restart below. This works
572 * because recurse is false when calling dev_get_port_parent_id.
573 */
574 if (!netdev->netdev_ops->ndo_get_port_parent_id &&
575 !netdev->netdev_ops->ndo_get_devlink_port)
576 return -EOPNOTSUPP;
577
578 if (!rtnl_trylock())
579 return restart_syscall();
580
581 if (dev_isalive(netdev)) {
582 struct netdev_phys_item_id ppid = { };
583
584 ret = dev_get_port_parent_id(netdev, &ppid, false);
585 if (!ret)
586 ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
587 }
588 rtnl_unlock();
589
590 return ret;
591 }
592 static DEVICE_ATTR_RO(phys_switch_id);
593
594 static struct attribute *net_class_attrs[] __ro_after_init = {
595 &dev_attr_netdev_group.attr,
596 &dev_attr_type.attr,
597 &dev_attr_dev_id.attr,
598 &dev_attr_dev_port.attr,
599 &dev_attr_iflink.attr,
600 &dev_attr_ifindex.attr,
601 &dev_attr_name_assign_type.attr,
602 &dev_attr_addr_assign_type.attr,
603 &dev_attr_addr_len.attr,
604 &dev_attr_link_mode.attr,
605 &dev_attr_address.attr,
606 &dev_attr_broadcast.attr,
607 &dev_attr_speed.attr,
608 &dev_attr_duplex.attr,
609 &dev_attr_dormant.attr,
610 &dev_attr_testing.attr,
611 &dev_attr_operstate.attr,
612 &dev_attr_carrier_changes.attr,
613 &dev_attr_ifalias.attr,
614 &dev_attr_carrier.attr,
615 &dev_attr_mtu.attr,
616 &dev_attr_flags.attr,
617 &dev_attr_tx_queue_len.attr,
618 &dev_attr_gro_flush_timeout.attr,
619 &dev_attr_napi_defer_hard_irqs.attr,
620 &dev_attr_phys_port_id.attr,
621 &dev_attr_phys_port_name.attr,
622 &dev_attr_phys_switch_id.attr,
623 &dev_attr_proto_down.attr,
624 &dev_attr_carrier_up_count.attr,
625 &dev_attr_carrier_down_count.attr,
626 NULL,
627 };
628 ATTRIBUTE_GROUPS(net_class);
629
630 /* Show a given an attribute in the statistics group */
netstat_show(const struct device * d,struct device_attribute * attr,char * buf,unsigned long offset)631 static ssize_t netstat_show(const struct device *d,
632 struct device_attribute *attr, char *buf,
633 unsigned long offset)
634 {
635 struct net_device *dev = to_net_dev(d);
636 ssize_t ret = -EINVAL;
637
638 WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
639 offset % sizeof(u64) != 0);
640
641 read_lock(&dev_base_lock);
642 if (dev_isalive(dev)) {
643 struct rtnl_link_stats64 temp;
644 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
645
646 ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
647 }
648 read_unlock(&dev_base_lock);
649 return ret;
650 }
651
652 /* generate a read-only statistics attribute */
653 #define NETSTAT_ENTRY(name) \
654 static ssize_t name##_show(struct device *d, \
655 struct device_attribute *attr, char *buf) \
656 { \
657 return netstat_show(d, attr, buf, \
658 offsetof(struct rtnl_link_stats64, name)); \
659 } \
660 static DEVICE_ATTR_RO(name)
661
662 NETSTAT_ENTRY(rx_packets);
663 NETSTAT_ENTRY(tx_packets);
664 NETSTAT_ENTRY(rx_bytes);
665 NETSTAT_ENTRY(tx_bytes);
666 NETSTAT_ENTRY(rx_errors);
667 NETSTAT_ENTRY(tx_errors);
668 NETSTAT_ENTRY(rx_dropped);
669 NETSTAT_ENTRY(tx_dropped);
670 NETSTAT_ENTRY(multicast);
671 NETSTAT_ENTRY(collisions);
672 NETSTAT_ENTRY(rx_length_errors);
673 NETSTAT_ENTRY(rx_over_errors);
674 NETSTAT_ENTRY(rx_crc_errors);
675 NETSTAT_ENTRY(rx_frame_errors);
676 NETSTAT_ENTRY(rx_fifo_errors);
677 NETSTAT_ENTRY(rx_missed_errors);
678 NETSTAT_ENTRY(tx_aborted_errors);
679 NETSTAT_ENTRY(tx_carrier_errors);
680 NETSTAT_ENTRY(tx_fifo_errors);
681 NETSTAT_ENTRY(tx_heartbeat_errors);
682 NETSTAT_ENTRY(tx_window_errors);
683 NETSTAT_ENTRY(rx_compressed);
684 NETSTAT_ENTRY(tx_compressed);
685 NETSTAT_ENTRY(rx_nohandler);
686
687 static struct attribute *netstat_attrs[] __ro_after_init = {
688 &dev_attr_rx_packets.attr,
689 &dev_attr_tx_packets.attr,
690 &dev_attr_rx_bytes.attr,
691 &dev_attr_tx_bytes.attr,
692 &dev_attr_rx_errors.attr,
693 &dev_attr_tx_errors.attr,
694 &dev_attr_rx_dropped.attr,
695 &dev_attr_tx_dropped.attr,
696 &dev_attr_multicast.attr,
697 &dev_attr_collisions.attr,
698 &dev_attr_rx_length_errors.attr,
699 &dev_attr_rx_over_errors.attr,
700 &dev_attr_rx_crc_errors.attr,
701 &dev_attr_rx_frame_errors.attr,
702 &dev_attr_rx_fifo_errors.attr,
703 &dev_attr_rx_missed_errors.attr,
704 &dev_attr_tx_aborted_errors.attr,
705 &dev_attr_tx_carrier_errors.attr,
706 &dev_attr_tx_fifo_errors.attr,
707 &dev_attr_tx_heartbeat_errors.attr,
708 &dev_attr_tx_window_errors.attr,
709 &dev_attr_rx_compressed.attr,
710 &dev_attr_tx_compressed.attr,
711 &dev_attr_rx_nohandler.attr,
712 NULL
713 };
714
715 static const struct attribute_group netstat_group = {
716 .name = "statistics",
717 .attrs = netstat_attrs,
718 };
719
720 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
721 static struct attribute *wireless_attrs[] = {
722 NULL
723 };
724
725 static const struct attribute_group wireless_group = {
726 .name = "wireless",
727 .attrs = wireless_attrs,
728 };
729 #endif
730
731 #else /* CONFIG_SYSFS */
732 #define net_class_groups NULL
733 #endif /* CONFIG_SYSFS */
734
735 #ifdef CONFIG_SYSFS
736 #define to_rx_queue_attr(_attr) \
737 container_of(_attr, struct rx_queue_attribute, attr)
738
739 #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
740
rx_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)741 static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
742 char *buf)
743 {
744 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
745 struct netdev_rx_queue *queue = to_rx_queue(kobj);
746
747 if (!attribute->show)
748 return -EIO;
749
750 return attribute->show(queue, buf);
751 }
752
rx_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)753 static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
754 const char *buf, size_t count)
755 {
756 const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
757 struct netdev_rx_queue *queue = to_rx_queue(kobj);
758
759 if (!attribute->store)
760 return -EIO;
761
762 return attribute->store(queue, buf, count);
763 }
764
765 static const struct sysfs_ops rx_queue_sysfs_ops = {
766 .show = rx_queue_attr_show,
767 .store = rx_queue_attr_store,
768 };
769
770 #ifdef CONFIG_RPS
show_rps_map(struct netdev_rx_queue * queue,char * buf)771 static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
772 {
773 struct rps_map *map;
774 cpumask_var_t mask;
775 int i, len;
776
777 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
778 return -ENOMEM;
779
780 rcu_read_lock();
781 map = rcu_dereference(queue->rps_map);
782 if (map)
783 for (i = 0; i < map->len; i++)
784 cpumask_set_cpu(map->cpus[i], mask);
785
786 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
787 rcu_read_unlock();
788 free_cpumask_var(mask);
789
790 return len < PAGE_SIZE ? len : -EINVAL;
791 }
792
store_rps_map(struct netdev_rx_queue * queue,const char * buf,size_t len)793 static ssize_t store_rps_map(struct netdev_rx_queue *queue,
794 const char *buf, size_t len)
795 {
796 struct rps_map *old_map, *map;
797 cpumask_var_t mask;
798 int err, cpu, i, hk_flags;
799 static DEFINE_MUTEX(rps_map_mutex);
800
801 if (!capable(CAP_NET_ADMIN))
802 return -EPERM;
803
804 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
805 return -ENOMEM;
806
807 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
808 if (err) {
809 free_cpumask_var(mask);
810 return err;
811 }
812
813 if (!cpumask_empty(mask)) {
814 hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ;
815 cpumask_and(mask, mask, housekeeping_cpumask(hk_flags));
816 if (cpumask_empty(mask)) {
817 free_cpumask_var(mask);
818 return -EINVAL;
819 }
820 }
821
822 map = kzalloc(max_t(unsigned int,
823 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
824 GFP_KERNEL);
825 if (!map) {
826 free_cpumask_var(mask);
827 return -ENOMEM;
828 }
829
830 i = 0;
831 for_each_cpu_and(cpu, mask, cpu_online_mask)
832 map->cpus[i++] = cpu;
833
834 if (i) {
835 map->len = i;
836 } else {
837 kfree(map);
838 map = NULL;
839 }
840
841 mutex_lock(&rps_map_mutex);
842 old_map = rcu_dereference_protected(queue->rps_map,
843 mutex_is_locked(&rps_map_mutex));
844 rcu_assign_pointer(queue->rps_map, map);
845
846 if (map)
847 static_branch_inc(&rps_needed);
848 if (old_map)
849 static_branch_dec(&rps_needed);
850
851 mutex_unlock(&rps_map_mutex);
852
853 if (old_map)
854 kfree_rcu(old_map, rcu);
855
856 free_cpumask_var(mask);
857 return len;
858 }
859
show_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,char * buf)860 static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
861 char *buf)
862 {
863 struct rps_dev_flow_table *flow_table;
864 unsigned long val = 0;
865
866 rcu_read_lock();
867 flow_table = rcu_dereference(queue->rps_flow_table);
868 if (flow_table)
869 val = (unsigned long)flow_table->mask + 1;
870 rcu_read_unlock();
871
872 return sprintf(buf, "%lu\n", val);
873 }
874
rps_dev_flow_table_release(struct rcu_head * rcu)875 static void rps_dev_flow_table_release(struct rcu_head *rcu)
876 {
877 struct rps_dev_flow_table *table = container_of(rcu,
878 struct rps_dev_flow_table, rcu);
879 vfree(table);
880 }
881
store_rps_dev_flow_table_cnt(struct netdev_rx_queue * queue,const char * buf,size_t len)882 static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
883 const char *buf, size_t len)
884 {
885 unsigned long mask, count;
886 struct rps_dev_flow_table *table, *old_table;
887 static DEFINE_SPINLOCK(rps_dev_flow_lock);
888 int rc;
889
890 if (!capable(CAP_NET_ADMIN))
891 return -EPERM;
892
893 rc = kstrtoul(buf, 0, &count);
894 if (rc < 0)
895 return rc;
896
897 if (count) {
898 mask = count - 1;
899 /* mask = roundup_pow_of_two(count) - 1;
900 * without overflows...
901 */
902 while ((mask | (mask >> 1)) != mask)
903 mask |= (mask >> 1);
904 /* On 64 bit arches, must check mask fits in table->mask (u32),
905 * and on 32bit arches, must check
906 * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
907 */
908 #if BITS_PER_LONG > 32
909 if (mask > (unsigned long)(u32)mask)
910 return -EINVAL;
911 #else
912 if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
913 / sizeof(struct rps_dev_flow)) {
914 /* Enforce a limit to prevent overflow */
915 return -EINVAL;
916 }
917 #endif
918 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
919 if (!table)
920 return -ENOMEM;
921
922 table->mask = mask;
923 for (count = 0; count <= mask; count++)
924 table->flows[count].cpu = RPS_NO_CPU;
925 } else {
926 table = NULL;
927 }
928
929 spin_lock(&rps_dev_flow_lock);
930 old_table = rcu_dereference_protected(queue->rps_flow_table,
931 lockdep_is_held(&rps_dev_flow_lock));
932 rcu_assign_pointer(queue->rps_flow_table, table);
933 spin_unlock(&rps_dev_flow_lock);
934
935 if (old_table)
936 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
937
938 return len;
939 }
940
941 static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
942 = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
943
944 static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
945 = __ATTR(rps_flow_cnt, 0644,
946 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
947 #endif /* CONFIG_RPS */
948
949 static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
950 #ifdef CONFIG_RPS
951 &rps_cpus_attribute.attr,
952 &rps_dev_flow_table_cnt_attribute.attr,
953 #endif
954 NULL
955 };
956 ATTRIBUTE_GROUPS(rx_queue_default);
957
rx_queue_release(struct kobject * kobj)958 static void rx_queue_release(struct kobject *kobj)
959 {
960 struct netdev_rx_queue *queue = to_rx_queue(kobj);
961 #ifdef CONFIG_RPS
962 struct rps_map *map;
963 struct rps_dev_flow_table *flow_table;
964
965 map = rcu_dereference_protected(queue->rps_map, 1);
966 if (map) {
967 RCU_INIT_POINTER(queue->rps_map, NULL);
968 kfree_rcu(map, rcu);
969 }
970
971 flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
972 if (flow_table) {
973 RCU_INIT_POINTER(queue->rps_flow_table, NULL);
974 call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
975 }
976 #endif
977
978 memset(kobj, 0, sizeof(*kobj));
979 dev_put(queue->dev);
980 }
981
rx_queue_namespace(struct kobject * kobj)982 static const void *rx_queue_namespace(struct kobject *kobj)
983 {
984 struct netdev_rx_queue *queue = to_rx_queue(kobj);
985 struct device *dev = &queue->dev->dev;
986 const void *ns = NULL;
987
988 if (dev->class && dev->class->ns_type)
989 ns = dev->class->namespace(dev);
990
991 return ns;
992 }
993
rx_queue_get_ownership(struct kobject * kobj,kuid_t * uid,kgid_t * gid)994 static void rx_queue_get_ownership(struct kobject *kobj,
995 kuid_t *uid, kgid_t *gid)
996 {
997 const struct net *net = rx_queue_namespace(kobj);
998
999 net_ns_get_ownership(net, uid, gid);
1000 }
1001
1002 static struct kobj_type rx_queue_ktype __ro_after_init = {
1003 .sysfs_ops = &rx_queue_sysfs_ops,
1004 .release = rx_queue_release,
1005 .default_groups = rx_queue_default_groups,
1006 .namespace = rx_queue_namespace,
1007 .get_ownership = rx_queue_get_ownership,
1008 };
1009
rx_queue_add_kobject(struct net_device * dev,int index)1010 static int rx_queue_add_kobject(struct net_device *dev, int index)
1011 {
1012 struct netdev_rx_queue *queue = dev->_rx + index;
1013 struct kobject *kobj = &queue->kobj;
1014 int error = 0;
1015
1016 /* Kobject_put later will trigger rx_queue_release call which
1017 * decreases dev refcount: Take that reference here
1018 */
1019 dev_hold(queue->dev);
1020
1021 kobj->kset = dev->queues_kset;
1022 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
1023 "rx-%u", index);
1024 if (error)
1025 goto err;
1026
1027 if (dev->sysfs_rx_queue_group) {
1028 error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
1029 if (error)
1030 goto err;
1031 }
1032
1033 kobject_uevent(kobj, KOBJ_ADD);
1034
1035 return error;
1036
1037 err:
1038 kobject_put(kobj);
1039 return error;
1040 }
1041
rx_queue_change_owner(struct net_device * dev,int index,kuid_t kuid,kgid_t kgid)1042 static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
1043 kgid_t kgid)
1044 {
1045 struct netdev_rx_queue *queue = dev->_rx + index;
1046 struct kobject *kobj = &queue->kobj;
1047 int error;
1048
1049 error = sysfs_change_owner(kobj, kuid, kgid);
1050 if (error)
1051 return error;
1052
1053 if (dev->sysfs_rx_queue_group)
1054 error = sysfs_group_change_owner(
1055 kobj, dev->sysfs_rx_queue_group, kuid, kgid);
1056
1057 return error;
1058 }
1059 #endif /* CONFIG_SYSFS */
1060
1061 int
net_rx_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1062 net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1063 {
1064 #ifdef CONFIG_SYSFS
1065 int i;
1066 int error = 0;
1067
1068 #ifndef CONFIG_RPS
1069 if (!dev->sysfs_rx_queue_group)
1070 return 0;
1071 #endif
1072 for (i = old_num; i < new_num; i++) {
1073 error = rx_queue_add_kobject(dev, i);
1074 if (error) {
1075 new_num = old_num;
1076 break;
1077 }
1078 }
1079
1080 while (--i >= new_num) {
1081 struct kobject *kobj = &dev->_rx[i].kobj;
1082
1083 if (!refcount_read(&dev_net(dev)->count))
1084 kobj->uevent_suppress = 1;
1085 if (dev->sysfs_rx_queue_group)
1086 sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
1087 kobject_put(kobj);
1088 }
1089
1090 return error;
1091 #else
1092 return 0;
1093 #endif
1094 }
1095
net_rx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1096 static int net_rx_queue_change_owner(struct net_device *dev, int num,
1097 kuid_t kuid, kgid_t kgid)
1098 {
1099 #ifdef CONFIG_SYSFS
1100 int error = 0;
1101 int i;
1102
1103 #ifndef CONFIG_RPS
1104 if (!dev->sysfs_rx_queue_group)
1105 return 0;
1106 #endif
1107 for (i = 0; i < num; i++) {
1108 error = rx_queue_change_owner(dev, i, kuid, kgid);
1109 if (error)
1110 break;
1111 }
1112
1113 return error;
1114 #else
1115 return 0;
1116 #endif
1117 }
1118
1119 #ifdef CONFIG_SYSFS
1120 /*
1121 * netdev_queue sysfs structures and functions.
1122 */
1123 struct netdev_queue_attribute {
1124 struct attribute attr;
1125 ssize_t (*show)(struct netdev_queue *queue, char *buf);
1126 ssize_t (*store)(struct netdev_queue *queue,
1127 const char *buf, size_t len);
1128 };
1129 #define to_netdev_queue_attr(_attr) \
1130 container_of(_attr, struct netdev_queue_attribute, attr)
1131
1132 #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
1133
netdev_queue_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1134 static ssize_t netdev_queue_attr_show(struct kobject *kobj,
1135 struct attribute *attr, char *buf)
1136 {
1137 const struct netdev_queue_attribute *attribute
1138 = to_netdev_queue_attr(attr);
1139 struct netdev_queue *queue = to_netdev_queue(kobj);
1140
1141 if (!attribute->show)
1142 return -EIO;
1143
1144 return attribute->show(queue, buf);
1145 }
1146
netdev_queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1147 static ssize_t netdev_queue_attr_store(struct kobject *kobj,
1148 struct attribute *attr,
1149 const char *buf, size_t count)
1150 {
1151 const struct netdev_queue_attribute *attribute
1152 = to_netdev_queue_attr(attr);
1153 struct netdev_queue *queue = to_netdev_queue(kobj);
1154
1155 if (!attribute->store)
1156 return -EIO;
1157
1158 return attribute->store(queue, buf, count);
1159 }
1160
1161 static const struct sysfs_ops netdev_queue_sysfs_ops = {
1162 .show = netdev_queue_attr_show,
1163 .store = netdev_queue_attr_store,
1164 };
1165
tx_timeout_show(struct netdev_queue * queue,char * buf)1166 static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
1167 {
1168 unsigned long trans_timeout;
1169
1170 spin_lock_irq(&queue->_xmit_lock);
1171 trans_timeout = queue->trans_timeout;
1172 spin_unlock_irq(&queue->_xmit_lock);
1173
1174 return sprintf(buf, fmt_ulong, trans_timeout);
1175 }
1176
get_netdev_queue_index(struct netdev_queue * queue)1177 static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1178 {
1179 struct net_device *dev = queue->dev;
1180 unsigned int i;
1181
1182 i = queue - dev->_tx;
1183 BUG_ON(i >= dev->num_tx_queues);
1184
1185 return i;
1186 }
1187
traffic_class_show(struct netdev_queue * queue,char * buf)1188 static ssize_t traffic_class_show(struct netdev_queue *queue,
1189 char *buf)
1190 {
1191 struct net_device *dev = queue->dev;
1192 int index;
1193 int tc;
1194
1195 if (!netif_is_multiqueue(dev))
1196 return -ENOENT;
1197
1198 index = get_netdev_queue_index(queue);
1199
1200 /* If queue belongs to subordinate dev use its TC mapping */
1201 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1202
1203 tc = netdev_txq_to_tc(dev, index);
1204 if (tc < 0)
1205 return -EINVAL;
1206
1207 /* We can report the traffic class one of two ways:
1208 * Subordinate device traffic classes are reported with the traffic
1209 * class first, and then the subordinate class so for example TC0 on
1210 * subordinate device 2 will be reported as "0-2". If the queue
1211 * belongs to the root device it will be reported with just the
1212 * traffic class, so just "0" for TC 0 for example.
1213 */
1214 return dev->num_tc < 0 ? sprintf(buf, "%d%d\n", tc, dev->num_tc) :
1215 sprintf(buf, "%d\n", tc);
1216 }
1217
1218 #ifdef CONFIG_XPS
tx_maxrate_show(struct netdev_queue * queue,char * buf)1219 static ssize_t tx_maxrate_show(struct netdev_queue *queue,
1220 char *buf)
1221 {
1222 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1223 }
1224
tx_maxrate_store(struct netdev_queue * queue,const char * buf,size_t len)1225 static ssize_t tx_maxrate_store(struct netdev_queue *queue,
1226 const char *buf, size_t len)
1227 {
1228 struct net_device *dev = queue->dev;
1229 int err, index = get_netdev_queue_index(queue);
1230 u32 rate = 0;
1231
1232 if (!capable(CAP_NET_ADMIN))
1233 return -EPERM;
1234
1235 /* The check is also done later; this helps returning early without
1236 * hitting the trylock/restart below.
1237 */
1238 if (!dev->netdev_ops->ndo_set_tx_maxrate)
1239 return -EOPNOTSUPP;
1240
1241 err = kstrtou32(buf, 10, &rate);
1242 if (err < 0)
1243 return err;
1244
1245 if (!rtnl_trylock())
1246 return restart_syscall();
1247
1248 err = -EOPNOTSUPP;
1249 if (dev->netdev_ops->ndo_set_tx_maxrate)
1250 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1251
1252 rtnl_unlock();
1253 if (!err) {
1254 queue->tx_maxrate = rate;
1255 return len;
1256 }
1257 return err;
1258 }
1259
1260 static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
1261 = __ATTR_RW(tx_maxrate);
1262 #endif
1263
1264 static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
1265 = __ATTR_RO(tx_timeout);
1266
1267 static struct netdev_queue_attribute queue_traffic_class __ro_after_init
1268 = __ATTR_RO(traffic_class);
1269
1270 #ifdef CONFIG_BQL
1271 /*
1272 * Byte queue limits sysfs structures and functions.
1273 */
bql_show(char * buf,unsigned int value)1274 static ssize_t bql_show(char *buf, unsigned int value)
1275 {
1276 return sprintf(buf, "%u\n", value);
1277 }
1278
bql_set(const char * buf,const size_t count,unsigned int * pvalue)1279 static ssize_t bql_set(const char *buf, const size_t count,
1280 unsigned int *pvalue)
1281 {
1282 unsigned int value;
1283 int err;
1284
1285 if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
1286 value = DQL_MAX_LIMIT;
1287 } else {
1288 err = kstrtouint(buf, 10, &value);
1289 if (err < 0)
1290 return err;
1291 if (value > DQL_MAX_LIMIT)
1292 return -EINVAL;
1293 }
1294
1295 *pvalue = value;
1296
1297 return count;
1298 }
1299
bql_show_hold_time(struct netdev_queue * queue,char * buf)1300 static ssize_t bql_show_hold_time(struct netdev_queue *queue,
1301 char *buf)
1302 {
1303 struct dql *dql = &queue->dql;
1304
1305 return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
1306 }
1307
bql_set_hold_time(struct netdev_queue * queue,const char * buf,size_t len)1308 static ssize_t bql_set_hold_time(struct netdev_queue *queue,
1309 const char *buf, size_t len)
1310 {
1311 struct dql *dql = &queue->dql;
1312 unsigned int value;
1313 int err;
1314
1315 err = kstrtouint(buf, 10, &value);
1316 if (err < 0)
1317 return err;
1318
1319 dql->slack_hold_time = msecs_to_jiffies(value);
1320
1321 return len;
1322 }
1323
1324 static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
1325 = __ATTR(hold_time, 0644,
1326 bql_show_hold_time, bql_set_hold_time);
1327
bql_show_inflight(struct netdev_queue * queue,char * buf)1328 static ssize_t bql_show_inflight(struct netdev_queue *queue,
1329 char *buf)
1330 {
1331 struct dql *dql = &queue->dql;
1332
1333 return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
1334 }
1335
1336 static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
1337 __ATTR(inflight, 0444, bql_show_inflight, NULL);
1338
1339 #define BQL_ATTR(NAME, FIELD) \
1340 static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
1341 char *buf) \
1342 { \
1343 return bql_show(buf, queue->dql.FIELD); \
1344 } \
1345 \
1346 static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
1347 const char *buf, size_t len) \
1348 { \
1349 return bql_set(buf, len, &queue->dql.FIELD); \
1350 } \
1351 \
1352 static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
1353 = __ATTR(NAME, 0644, \
1354 bql_show_ ## NAME, bql_set_ ## NAME)
1355
1356 BQL_ATTR(limit, limit);
1357 BQL_ATTR(limit_max, max_limit);
1358 BQL_ATTR(limit_min, min_limit);
1359
1360 static struct attribute *dql_attrs[] __ro_after_init = {
1361 &bql_limit_attribute.attr,
1362 &bql_limit_max_attribute.attr,
1363 &bql_limit_min_attribute.attr,
1364 &bql_hold_time_attribute.attr,
1365 &bql_inflight_attribute.attr,
1366 NULL
1367 };
1368
1369 static const struct attribute_group dql_group = {
1370 .name = "byte_queue_limits",
1371 .attrs = dql_attrs,
1372 };
1373 #endif /* CONFIG_BQL */
1374
1375 #ifdef CONFIG_XPS
xps_cpus_show(struct netdev_queue * queue,char * buf)1376 static ssize_t xps_cpus_show(struct netdev_queue *queue,
1377 char *buf)
1378 {
1379 int cpu, len, ret, num_tc = 1, tc = 0;
1380 struct net_device *dev = queue->dev;
1381 struct xps_dev_maps *dev_maps;
1382 cpumask_var_t mask;
1383 unsigned long index;
1384
1385 if (!netif_is_multiqueue(dev))
1386 return -ENOENT;
1387
1388 index = get_netdev_queue_index(queue);
1389
1390 if (!rtnl_trylock())
1391 return restart_syscall();
1392
1393 if (dev->num_tc) {
1394 /* Do not allow XPS on subordinate device directly */
1395 num_tc = dev->num_tc;
1396 if (num_tc < 0) {
1397 ret = -EINVAL;
1398 goto err_rtnl_unlock;
1399 }
1400
1401 /* If queue belongs to subordinate dev use its map */
1402 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
1403
1404 tc = netdev_txq_to_tc(dev, index);
1405 if (tc < 0) {
1406 ret = -EINVAL;
1407 goto err_rtnl_unlock;
1408 }
1409 }
1410
1411 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
1412 ret = -ENOMEM;
1413 goto err_rtnl_unlock;
1414 }
1415
1416 rcu_read_lock();
1417 dev_maps = rcu_dereference(dev->xps_cpus_map);
1418 if (dev_maps) {
1419 for_each_possible_cpu(cpu) {
1420 int i, tci = cpu * num_tc + tc;
1421 struct xps_map *map;
1422
1423 map = rcu_dereference(dev_maps->attr_map[tci]);
1424 if (!map)
1425 continue;
1426
1427 for (i = map->len; i--;) {
1428 if (map->queues[i] == index) {
1429 cpumask_set_cpu(cpu, mask);
1430 break;
1431 }
1432 }
1433 }
1434 }
1435 rcu_read_unlock();
1436
1437 rtnl_unlock();
1438
1439 len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
1440 free_cpumask_var(mask);
1441 return len < PAGE_SIZE ? len : -EINVAL;
1442
1443 err_rtnl_unlock:
1444 rtnl_unlock();
1445 return ret;
1446 }
1447
xps_cpus_store(struct netdev_queue * queue,const char * buf,size_t len)1448 static ssize_t xps_cpus_store(struct netdev_queue *queue,
1449 const char *buf, size_t len)
1450 {
1451 struct net_device *dev = queue->dev;
1452 unsigned long index;
1453 cpumask_var_t mask;
1454 int err;
1455
1456 if (!netif_is_multiqueue(dev))
1457 return -ENOENT;
1458
1459 if (!capable(CAP_NET_ADMIN))
1460 return -EPERM;
1461
1462 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1463 return -ENOMEM;
1464
1465 index = get_netdev_queue_index(queue);
1466
1467 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
1468 if (err) {
1469 free_cpumask_var(mask);
1470 return err;
1471 }
1472
1473 if (!rtnl_trylock()) {
1474 free_cpumask_var(mask);
1475 return restart_syscall();
1476 }
1477
1478 err = netif_set_xps_queue(dev, mask, index);
1479 rtnl_unlock();
1480
1481 free_cpumask_var(mask);
1482
1483 return err ? : len;
1484 }
1485
1486 static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
1487 = __ATTR_RW(xps_cpus);
1488
xps_rxqs_show(struct netdev_queue * queue,char * buf)1489 static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
1490 {
1491 int j, len, ret, num_tc = 1, tc = 0;
1492 struct net_device *dev = queue->dev;
1493 struct xps_dev_maps *dev_maps;
1494 unsigned long *mask, index;
1495
1496 index = get_netdev_queue_index(queue);
1497
1498 if (!rtnl_trylock())
1499 return restart_syscall();
1500
1501 if (dev->num_tc) {
1502 num_tc = dev->num_tc;
1503 tc = netdev_txq_to_tc(dev, index);
1504 if (tc < 0) {
1505 ret = -EINVAL;
1506 goto err_rtnl_unlock;
1507 }
1508 }
1509 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1510 if (!mask) {
1511 ret = -ENOMEM;
1512 goto err_rtnl_unlock;
1513 }
1514
1515 rcu_read_lock();
1516 dev_maps = rcu_dereference(dev->xps_rxqs_map);
1517 if (!dev_maps)
1518 goto out_no_maps;
1519
1520 for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues),
1521 j < dev->num_rx_queues;) {
1522 int i, tci = j * num_tc + tc;
1523 struct xps_map *map;
1524
1525 map = rcu_dereference(dev_maps->attr_map[tci]);
1526 if (!map)
1527 continue;
1528
1529 for (i = map->len; i--;) {
1530 if (map->queues[i] == index) {
1531 set_bit(j, mask);
1532 break;
1533 }
1534 }
1535 }
1536 out_no_maps:
1537 rcu_read_unlock();
1538
1539 rtnl_unlock();
1540
1541 len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues);
1542 bitmap_free(mask);
1543
1544 return len < PAGE_SIZE ? len : -EINVAL;
1545
1546 err_rtnl_unlock:
1547 rtnl_unlock();
1548 return ret;
1549 }
1550
xps_rxqs_store(struct netdev_queue * queue,const char * buf,size_t len)1551 static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
1552 size_t len)
1553 {
1554 struct net_device *dev = queue->dev;
1555 struct net *net = dev_net(dev);
1556 unsigned long *mask, index;
1557 int err;
1558
1559 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1560 return -EPERM;
1561
1562 mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
1563 if (!mask)
1564 return -ENOMEM;
1565
1566 index = get_netdev_queue_index(queue);
1567
1568 err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
1569 if (err) {
1570 bitmap_free(mask);
1571 return err;
1572 }
1573
1574 if (!rtnl_trylock()) {
1575 bitmap_free(mask);
1576 return restart_syscall();
1577 }
1578
1579 cpus_read_lock();
1580 err = __netif_set_xps_queue(dev, mask, index, true);
1581 cpus_read_unlock();
1582
1583 rtnl_unlock();
1584
1585 bitmap_free(mask);
1586 return err ? : len;
1587 }
1588
1589 static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
1590 = __ATTR_RW(xps_rxqs);
1591 #endif /* CONFIG_XPS */
1592
1593 static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
1594 &queue_trans_timeout.attr,
1595 &queue_traffic_class.attr,
1596 #ifdef CONFIG_XPS
1597 &xps_cpus_attribute.attr,
1598 &xps_rxqs_attribute.attr,
1599 &queue_tx_maxrate.attr,
1600 #endif
1601 NULL
1602 };
1603 ATTRIBUTE_GROUPS(netdev_queue_default);
1604
netdev_queue_release(struct kobject * kobj)1605 static void netdev_queue_release(struct kobject *kobj)
1606 {
1607 struct netdev_queue *queue = to_netdev_queue(kobj);
1608
1609 memset(kobj, 0, sizeof(*kobj));
1610 dev_put(queue->dev);
1611 }
1612
netdev_queue_namespace(struct kobject * kobj)1613 static const void *netdev_queue_namespace(struct kobject *kobj)
1614 {
1615 struct netdev_queue *queue = to_netdev_queue(kobj);
1616 struct device *dev = &queue->dev->dev;
1617 const void *ns = NULL;
1618
1619 if (dev->class && dev->class->ns_type)
1620 ns = dev->class->namespace(dev);
1621
1622 return ns;
1623 }
1624
netdev_queue_get_ownership(struct kobject * kobj,kuid_t * uid,kgid_t * gid)1625 static void netdev_queue_get_ownership(struct kobject *kobj,
1626 kuid_t *uid, kgid_t *gid)
1627 {
1628 const struct net *net = netdev_queue_namespace(kobj);
1629
1630 net_ns_get_ownership(net, uid, gid);
1631 }
1632
1633 static struct kobj_type netdev_queue_ktype __ro_after_init = {
1634 .sysfs_ops = &netdev_queue_sysfs_ops,
1635 .release = netdev_queue_release,
1636 .default_groups = netdev_queue_default_groups,
1637 .namespace = netdev_queue_namespace,
1638 .get_ownership = netdev_queue_get_ownership,
1639 };
1640
netdev_queue_add_kobject(struct net_device * dev,int index)1641 static int netdev_queue_add_kobject(struct net_device *dev, int index)
1642 {
1643 struct netdev_queue *queue = dev->_tx + index;
1644 struct kobject *kobj = &queue->kobj;
1645 int error = 0;
1646
1647 /* Kobject_put later will trigger netdev_queue_release call
1648 * which decreases dev refcount: Take that reference here
1649 */
1650 dev_hold(queue->dev);
1651
1652 kobj->kset = dev->queues_kset;
1653 error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
1654 "tx-%u", index);
1655 if (error)
1656 goto err;
1657
1658 #ifdef CONFIG_BQL
1659 error = sysfs_create_group(kobj, &dql_group);
1660 if (error)
1661 goto err;
1662 #endif
1663
1664 kobject_uevent(kobj, KOBJ_ADD);
1665 return 0;
1666
1667 err:
1668 kobject_put(kobj);
1669 return error;
1670 }
1671
tx_queue_change_owner(struct net_device * ndev,int index,kuid_t kuid,kgid_t kgid)1672 static int tx_queue_change_owner(struct net_device *ndev, int index,
1673 kuid_t kuid, kgid_t kgid)
1674 {
1675 struct netdev_queue *queue = ndev->_tx + index;
1676 struct kobject *kobj = &queue->kobj;
1677 int error;
1678
1679 error = sysfs_change_owner(kobj, kuid, kgid);
1680 if (error)
1681 return error;
1682
1683 #ifdef CONFIG_BQL
1684 error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
1685 #endif
1686 return error;
1687 }
1688 #endif /* CONFIG_SYSFS */
1689
1690 int
netdev_queue_update_kobjects(struct net_device * dev,int old_num,int new_num)1691 netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
1692 {
1693 #ifdef CONFIG_SYSFS
1694 int i;
1695 int error = 0;
1696
1697 for (i = old_num; i < new_num; i++) {
1698 error = netdev_queue_add_kobject(dev, i);
1699 if (error) {
1700 new_num = old_num;
1701 break;
1702 }
1703 }
1704
1705 while (--i >= new_num) {
1706 struct netdev_queue *queue = dev->_tx + i;
1707
1708 if (!refcount_read(&dev_net(dev)->count))
1709 queue->kobj.uevent_suppress = 1;
1710 #ifdef CONFIG_BQL
1711 sysfs_remove_group(&queue->kobj, &dql_group);
1712 #endif
1713 kobject_put(&queue->kobj);
1714 }
1715
1716 return error;
1717 #else
1718 return 0;
1719 #endif /* CONFIG_SYSFS */
1720 }
1721
net_tx_queue_change_owner(struct net_device * dev,int num,kuid_t kuid,kgid_t kgid)1722 static int net_tx_queue_change_owner(struct net_device *dev, int num,
1723 kuid_t kuid, kgid_t kgid)
1724 {
1725 #ifdef CONFIG_SYSFS
1726 int error = 0;
1727 int i;
1728
1729 for (i = 0; i < num; i++) {
1730 error = tx_queue_change_owner(dev, i, kuid, kgid);
1731 if (error)
1732 break;
1733 }
1734
1735 return error;
1736 #else
1737 return 0;
1738 #endif /* CONFIG_SYSFS */
1739 }
1740
register_queue_kobjects(struct net_device * dev)1741 static int register_queue_kobjects(struct net_device *dev)
1742 {
1743 int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
1744
1745 #ifdef CONFIG_SYSFS
1746 dev->queues_kset = kset_create_and_add("queues",
1747 NULL, &dev->dev.kobj);
1748 if (!dev->queues_kset)
1749 return -ENOMEM;
1750 real_rx = dev->real_num_rx_queues;
1751 #endif
1752 real_tx = dev->real_num_tx_queues;
1753
1754 error = net_rx_queue_update_kobjects(dev, 0, real_rx);
1755 if (error)
1756 goto error;
1757 rxq = real_rx;
1758
1759 error = netdev_queue_update_kobjects(dev, 0, real_tx);
1760 if (error)
1761 goto error;
1762 txq = real_tx;
1763
1764 return 0;
1765
1766 error:
1767 netdev_queue_update_kobjects(dev, txq, 0);
1768 net_rx_queue_update_kobjects(dev, rxq, 0);
1769 #ifdef CONFIG_SYSFS
1770 kset_unregister(dev->queues_kset);
1771 #endif
1772 return error;
1773 }
1774
queue_change_owner(struct net_device * ndev,kuid_t kuid,kgid_t kgid)1775 static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
1776 {
1777 int error = 0, real_rx = 0, real_tx = 0;
1778
1779 #ifdef CONFIG_SYSFS
1780 if (ndev->queues_kset) {
1781 error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
1782 if (error)
1783 return error;
1784 }
1785 real_rx = ndev->real_num_rx_queues;
1786 #endif
1787 real_tx = ndev->real_num_tx_queues;
1788
1789 error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
1790 if (error)
1791 return error;
1792
1793 error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
1794 if (error)
1795 return error;
1796
1797 return 0;
1798 }
1799
remove_queue_kobjects(struct net_device * dev)1800 static void remove_queue_kobjects(struct net_device *dev)
1801 {
1802 int real_rx = 0, real_tx = 0;
1803
1804 #ifdef CONFIG_SYSFS
1805 real_rx = dev->real_num_rx_queues;
1806 #endif
1807 real_tx = dev->real_num_tx_queues;
1808
1809 net_rx_queue_update_kobjects(dev, real_rx, 0);
1810 netdev_queue_update_kobjects(dev, real_tx, 0);
1811
1812 dev->real_num_rx_queues = 0;
1813 dev->real_num_tx_queues = 0;
1814 #ifdef CONFIG_SYSFS
1815 kset_unregister(dev->queues_kset);
1816 #endif
1817 }
1818
net_current_may_mount(void)1819 static bool net_current_may_mount(void)
1820 {
1821 struct net *net = current->nsproxy->net_ns;
1822
1823 return ns_capable(net->user_ns, CAP_SYS_ADMIN);
1824 }
1825
net_grab_current_ns(void)1826 static void *net_grab_current_ns(void)
1827 {
1828 struct net *ns = current->nsproxy->net_ns;
1829 #ifdef CONFIG_NET_NS
1830 if (ns)
1831 refcount_inc(&ns->passive);
1832 #endif
1833 return ns;
1834 }
1835
net_initial_ns(void)1836 static const void *net_initial_ns(void)
1837 {
1838 return &init_net;
1839 }
1840
net_netlink_ns(struct sock * sk)1841 static const void *net_netlink_ns(struct sock *sk)
1842 {
1843 return sock_net(sk);
1844 }
1845
1846 const struct kobj_ns_type_operations net_ns_type_operations = {
1847 .type = KOBJ_NS_TYPE_NET,
1848 .current_may_mount = net_current_may_mount,
1849 .grab_current_ns = net_grab_current_ns,
1850 .netlink_ns = net_netlink_ns,
1851 .initial_ns = net_initial_ns,
1852 .drop_ns = net_drop_ns,
1853 };
1854 EXPORT_SYMBOL_GPL(net_ns_type_operations);
1855
netdev_uevent(struct device * d,struct kobj_uevent_env * env)1856 static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
1857 {
1858 struct net_device *dev = to_net_dev(d);
1859 int retval;
1860
1861 /* pass interface to uevent. */
1862 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
1863 if (retval)
1864 goto exit;
1865
1866 /* pass ifindex to uevent.
1867 * ifindex is useful as it won't change (interface name may change)
1868 * and is what RtNetlink uses natively.
1869 */
1870 retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
1871
1872 exit:
1873 return retval;
1874 }
1875
1876 /*
1877 * netdev_release -- destroy and free a dead device.
1878 * Called when last reference to device kobject is gone.
1879 */
netdev_release(struct device * d)1880 static void netdev_release(struct device *d)
1881 {
1882 struct net_device *dev = to_net_dev(d);
1883
1884 BUG_ON(dev->reg_state != NETREG_RELEASED);
1885
1886 /* no need to wait for rcu grace period:
1887 * device is dead and about to be freed.
1888 */
1889 kfree(rcu_access_pointer(dev->ifalias));
1890 netdev_freemem(dev);
1891 }
1892
net_namespace(struct device * d)1893 static const void *net_namespace(struct device *d)
1894 {
1895 struct net_device *dev = to_net_dev(d);
1896
1897 return dev_net(dev);
1898 }
1899
net_get_ownership(struct device * d,kuid_t * uid,kgid_t * gid)1900 static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
1901 {
1902 struct net_device *dev = to_net_dev(d);
1903 const struct net *net = dev_net(dev);
1904
1905 net_ns_get_ownership(net, uid, gid);
1906 }
1907
1908 static struct class net_class __ro_after_init = {
1909 .name = "net",
1910 .dev_release = netdev_release,
1911 .dev_groups = net_class_groups,
1912 .dev_uevent = netdev_uevent,
1913 .ns_type = &net_ns_type_operations,
1914 .namespace = net_namespace,
1915 .get_ownership = net_get_ownership,
1916 };
1917
1918 #ifdef CONFIG_OF_NET
of_dev_node_match(struct device * dev,const void * data)1919 static int of_dev_node_match(struct device *dev, const void *data)
1920 {
1921 for (; dev; dev = dev->parent) {
1922 if (dev->of_node == data)
1923 return 1;
1924 }
1925
1926 return 0;
1927 }
1928
1929 /*
1930 * of_find_net_device_by_node - lookup the net device for the device node
1931 * @np: OF device node
1932 *
1933 * Looks up the net_device structure corresponding with the device node.
1934 * If successful, returns a pointer to the net_device with the embedded
1935 * struct device refcount incremented by one, or NULL on failure. The
1936 * refcount must be dropped when done with the net_device.
1937 */
of_find_net_device_by_node(struct device_node * np)1938 struct net_device *of_find_net_device_by_node(struct device_node *np)
1939 {
1940 struct device *dev;
1941
1942 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1943 if (!dev)
1944 return NULL;
1945
1946 return to_net_dev(dev);
1947 }
1948 EXPORT_SYMBOL(of_find_net_device_by_node);
1949 #endif
1950
1951 /* Delete sysfs entries but hold kobject reference until after all
1952 * netdev references are gone.
1953 */
netdev_unregister_kobject(struct net_device * ndev)1954 void netdev_unregister_kobject(struct net_device *ndev)
1955 {
1956 struct device *dev = &ndev->dev;
1957
1958 if (!refcount_read(&dev_net(ndev)->count))
1959 dev_set_uevent_suppress(dev, 1);
1960
1961 kobject_get(&dev->kobj);
1962
1963 remove_queue_kobjects(ndev);
1964
1965 pm_runtime_set_memalloc_noio(dev, false);
1966
1967 device_del(dev);
1968 }
1969
1970 /* Create sysfs entries for network device. */
netdev_register_kobject(struct net_device * ndev)1971 int netdev_register_kobject(struct net_device *ndev)
1972 {
1973 struct device *dev = &ndev->dev;
1974 const struct attribute_group **groups = ndev->sysfs_groups;
1975 int error = 0;
1976
1977 device_initialize(dev);
1978 dev->class = &net_class;
1979 dev->platform_data = ndev;
1980 dev->groups = groups;
1981
1982 dev_set_name(dev, "%s", ndev->name);
1983
1984 #ifdef CONFIG_SYSFS
1985 /* Allow for a device specific group */
1986 if (*groups)
1987 groups++;
1988
1989 *groups++ = &netstat_group;
1990
1991 #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
1992 if (ndev->ieee80211_ptr)
1993 *groups++ = &wireless_group;
1994 #if IS_ENABLED(CONFIG_WIRELESS_EXT)
1995 else if (ndev->wireless_handlers)
1996 *groups++ = &wireless_group;
1997 #endif
1998 #endif
1999 #endif /* CONFIG_SYSFS */
2000
2001 error = device_add(dev);
2002 if (error)
2003 return error;
2004
2005 error = register_queue_kobjects(ndev);
2006 if (error) {
2007 device_del(dev);
2008 return error;
2009 }
2010
2011 pm_runtime_set_memalloc_noio(dev, true);
2012
2013 return error;
2014 }
2015
2016 /* Change owner for sysfs entries when moving network devices across network
2017 * namespaces owned by different user namespaces.
2018 */
netdev_change_owner(struct net_device * ndev,const struct net * net_old,const struct net * net_new)2019 int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
2020 const struct net *net_new)
2021 {
2022 kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
2023 kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
2024 struct device *dev = &ndev->dev;
2025 int error;
2026
2027 net_ns_get_ownership(net_old, &old_uid, &old_gid);
2028 net_ns_get_ownership(net_new, &new_uid, &new_gid);
2029
2030 /* The network namespace was changed but the owning user namespace is
2031 * identical so there's no need to change the owner of sysfs entries.
2032 */
2033 if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
2034 return 0;
2035
2036 error = device_change_owner(dev, new_uid, new_gid);
2037 if (error)
2038 return error;
2039
2040 error = queue_change_owner(ndev, new_uid, new_gid);
2041 if (error)
2042 return error;
2043
2044 return 0;
2045 }
2046
netdev_class_create_file_ns(const struct class_attribute * class_attr,const void * ns)2047 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
2048 const void *ns)
2049 {
2050 return class_create_file_ns(&net_class, class_attr, ns);
2051 }
2052 EXPORT_SYMBOL(netdev_class_create_file_ns);
2053
netdev_class_remove_file_ns(const struct class_attribute * class_attr,const void * ns)2054 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
2055 const void *ns)
2056 {
2057 class_remove_file_ns(&net_class, class_attr, ns);
2058 }
2059 EXPORT_SYMBOL(netdev_class_remove_file_ns);
2060
netdev_kobject_init(void)2061 int __init netdev_kobject_init(void)
2062 {
2063 kobj_ns_type_register(&net_ns_type_operations);
2064 return class_register(&net_class);
2065 }
2066