Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kmod.h>
3#include <linux/netdevice.h>
4#include <linux/inetdevice.h>
5#include <linux/etherdevice.h>
6#include <linux/rtnetlink.h>
7#include <linux/net_tstamp.h>
8#include <linux/phylib_stubs.h>
9#include <linux/ptp_clock_kernel.h>
10#include <linux/wireless.h>
11#include <linux/if_bridge.h>
12#include <net/dsa_stubs.h>
13#include <net/wext.h>
14
15#include "dev.h"
16
17/*
18 * Map an interface index to its name (SIOCGIFNAME)
19 */
20
21/*
22 * We need this ioctl for efficient implementation of the
23 * if_indextoname() function required by the IPv6 API. Without
24 * it, we would have to search all the interfaces to find a
25 * match. --pb
26 */
27
28static int dev_ifname(struct net *net, struct ifreq *ifr)
29{
30 ifr->ifr_name[IFNAMSIZ-1] = 0;
31 return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
32}
33
34/*
35 * Perform a SIOCGIFCONF call. This structure will change
36 * size eventually, and there is nothing I can do about it.
37 * Thus we will need a 'compatibility mode'.
38 */
39int dev_ifconf(struct net *net, struct ifconf __user *uifc)
40{
41 struct net_device *dev;
42 void __user *pos;
43 size_t size;
44 int len, total = 0, done;
45
46 /* both the ifconf and the ifreq structures are slightly different */
47 if (in_compat_syscall()) {
48 struct compat_ifconf ifc32;
49
50 if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
51 return -EFAULT;
52
53 pos = compat_ptr(ifc32.ifcbuf);
54 len = ifc32.ifc_len;
55 size = sizeof(struct compat_ifreq);
56 } else {
57 struct ifconf ifc;
58
59 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
60 return -EFAULT;
61
62 pos = ifc.ifc_buf;
63 len = ifc.ifc_len;
64 size = sizeof(struct ifreq);
65 }
66
67 /* Loop over the interfaces, and write an info block for each. */
68 rtnl_net_lock(net);
69 for_each_netdev(net, dev) {
70 if (!pos)
71 done = inet_gifconf(dev, NULL, 0, size);
72 else
73 done = inet_gifconf(dev, pos + total,
74 len - total, size);
75 if (done < 0) {
76 rtnl_net_unlock(net);
77 return -EFAULT;
78 }
79 total += done;
80 }
81 rtnl_net_unlock(net);
82
83 return put_user(total, &uifc->ifc_len);
84}
85
86static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
87{
88 struct ifmap *ifmap = &ifr->ifr_map;
89
90 if (in_compat_syscall()) {
91 struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
92
93 cifmap->mem_start = dev->mem_start;
94 cifmap->mem_end = dev->mem_end;
95 cifmap->base_addr = dev->base_addr;
96 cifmap->irq = dev->irq;
97 cifmap->dma = dev->dma;
98 cifmap->port = dev->if_port;
99
100 return 0;
101 }
102
103 ifmap->mem_start = dev->mem_start;
104 ifmap->mem_end = dev->mem_end;
105 ifmap->base_addr = dev->base_addr;
106 ifmap->irq = dev->irq;
107 ifmap->dma = dev->dma;
108 ifmap->port = dev->if_port;
109
110 return 0;
111}
112
113static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
114{
115 struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
116
117 if (!dev->netdev_ops->ndo_set_config)
118 return -EOPNOTSUPP;
119
120 if (in_compat_syscall()) {
121 struct ifmap ifmap = {
122 .mem_start = cifmap->mem_start,
123 .mem_end = cifmap->mem_end,
124 .base_addr = cifmap->base_addr,
125 .irq = cifmap->irq,
126 .dma = cifmap->dma,
127 .port = cifmap->port,
128 };
129
130 return dev->netdev_ops->ndo_set_config(dev, &ifmap);
131 }
132
133 return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
134}
135
136/*
137 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
138 */
139static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
140{
141 int err;
142 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
143
144 if (!dev)
145 return -ENODEV;
146
147 switch (cmd) {
148 case SIOCGIFFLAGS: /* Get interface flags */
149 ifr->ifr_flags = (short) dev_get_flags(dev);
150 return 0;
151
152 case SIOCGIFMETRIC: /* Get the metric on the interface
153 (currently unused) */
154 ifr->ifr_metric = 0;
155 return 0;
156
157 case SIOCGIFMTU: /* Get the MTU of a device */
158 ifr->ifr_mtu = dev->mtu;
159 return 0;
160
161 case SIOCGIFSLAVE:
162 err = -EINVAL;
163 break;
164
165 case SIOCGIFMAP:
166 return dev_getifmap(dev, ifr);
167
168 case SIOCGIFINDEX:
169 ifr->ifr_ifindex = dev->ifindex;
170 return 0;
171
172 case SIOCGIFTXQLEN:
173 ifr->ifr_qlen = dev->tx_queue_len;
174 return 0;
175
176 default:
177 /* dev_ioctl() should ensure this case
178 * is never reached
179 */
180 WARN_ON(1);
181 err = -ENOTTY;
182 break;
183
184 }
185 return err;
186}
187
188int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
189{
190 enum hwtstamp_tx_types tx_type;
191 enum hwtstamp_rx_filters rx_filter;
192 int tx_type_valid = 0;
193 int rx_filter_valid = 0;
194
195 if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
196 return -EINVAL;
197
198 tx_type = cfg->tx_type;
199 rx_filter = cfg->rx_filter;
200
201 switch (tx_type) {
202 case HWTSTAMP_TX_OFF:
203 case HWTSTAMP_TX_ON:
204 case HWTSTAMP_TX_ONESTEP_SYNC:
205 case HWTSTAMP_TX_ONESTEP_P2P:
206 tx_type_valid = 1;
207 break;
208 case __HWTSTAMP_TX_CNT:
209 /* not a real value */
210 break;
211 }
212
213 switch (rx_filter) {
214 case HWTSTAMP_FILTER_NONE:
215 case HWTSTAMP_FILTER_ALL:
216 case HWTSTAMP_FILTER_SOME:
217 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
218 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
219 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
220 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
221 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
222 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
223 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
224 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
225 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
226 case HWTSTAMP_FILTER_PTP_V2_EVENT:
227 case HWTSTAMP_FILTER_PTP_V2_SYNC:
228 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
229 case HWTSTAMP_FILTER_NTP_ALL:
230 rx_filter_valid = 1;
231 break;
232 case __HWTSTAMP_FILTER_CNT:
233 /* not a real value */
234 break;
235 }
236
237 if (!tx_type_valid || !rx_filter_valid)
238 return -ERANGE;
239
240 return 0;
241}
242
243static int dev_eth_ioctl(struct net_device *dev,
244 struct ifreq *ifr, unsigned int cmd)
245{
246 const struct net_device_ops *ops = dev->netdev_ops;
247
248 if (!ops->ndo_eth_ioctl)
249 return -EOPNOTSUPP;
250
251 if (!netif_device_present(dev))
252 return -ENODEV;
253
254 return ops->ndo_eth_ioctl(dev, ifr, cmd);
255}
256
257/**
258 * dev_get_hwtstamp_phylib() - Get hardware timestamping settings of NIC
259 * or of attached phylib PHY
260 * @dev: Network device
261 * @cfg: Timestamping configuration structure
262 *
263 * Helper for calling the default hardware provider timestamping.
264 *
265 * Note: phy_mii_ioctl() only handles SIOCSHWTSTAMP (not SIOCGHWTSTAMP), and
266 * there only exists a phydev->mii_ts->hwtstamp() method. So this will return
267 * -EOPNOTSUPP for phylib for now, which is still more accurate than letting
268 * the netdev handle the GET request.
269 */
270int dev_get_hwtstamp_phylib(struct net_device *dev,
271 struct kernel_hwtstamp_config *cfg)
272{
273 struct hwtstamp_provider *hwprov;
274
275 hwprov = rtnl_dereference(dev->hwprov);
276 if (hwprov) {
277 cfg->qualifier = hwprov->desc.qualifier;
278 if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
279 hwprov->phydev)
280 return phy_hwtstamp_get(hwprov->phydev, cfg);
281
282 if (hwprov->source == HWTSTAMP_SOURCE_NETDEV)
283 return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
284
285 return -EOPNOTSUPP;
286 }
287
288 if (phy_is_default_hwtstamp(dev->phydev))
289 return phy_hwtstamp_get(dev->phydev, cfg);
290
291 return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
292}
293
294static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
295{
296 const struct net_device_ops *ops = dev->netdev_ops;
297 struct kernel_hwtstamp_config kernel_cfg = {};
298 struct hwtstamp_config cfg;
299 int err;
300
301 if (!ops->ndo_hwtstamp_get)
302 return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); /* legacy */
303
304 if (!netif_device_present(dev))
305 return -ENODEV;
306
307 kernel_cfg.ifr = ifr;
308 err = dev_get_hwtstamp_phylib(dev, &kernel_cfg);
309 if (err)
310 return err;
311
312 /* If the request was resolved through an unconverted driver, omit
313 * the copy_to_user(), since the implementation has already done that
314 */
315 if (!kernel_cfg.copied_to_user) {
316 hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
317
318 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
319 return -EFAULT;
320 }
321
322 return 0;
323}
324
325/**
326 * dev_set_hwtstamp_phylib() - Change hardware timestamping of NIC
327 * or of attached phylib PHY
328 * @dev: Network device
329 * @cfg: Timestamping configuration structure
330 * @extack: Netlink extended ack message structure, for error reporting
331 *
332 * Helper for enforcing a common policy that phylib timestamping, if available,
333 * should take precedence in front of hardware timestamping provided by the
334 * netdev. If the netdev driver needs to perform specific actions even for PHY
335 * timestamping to work properly (a switch port must trap the timestamped
336 * frames and not forward them), it must set dev->see_all_hwtstamp_requests.
337 */
338int dev_set_hwtstamp_phylib(struct net_device *dev,
339 struct kernel_hwtstamp_config *cfg,
340 struct netlink_ext_ack *extack)
341{
342 const struct net_device_ops *ops = dev->netdev_ops;
343 struct kernel_hwtstamp_config old_cfg = {};
344 struct hwtstamp_provider *hwprov;
345 struct phy_device *phydev;
346 bool changed = false;
347 bool phy_ts;
348 int err;
349
350 hwprov = rtnl_dereference(dev->hwprov);
351 if (hwprov) {
352 if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
353 hwprov->phydev) {
354 phy_ts = true;
355 phydev = hwprov->phydev;
356 } else if (hwprov->source == HWTSTAMP_SOURCE_NETDEV) {
357 phy_ts = false;
358 } else {
359 return -EOPNOTSUPP;
360 }
361
362 cfg->qualifier = hwprov->desc.qualifier;
363 } else {
364 phy_ts = phy_is_default_hwtstamp(dev->phydev);
365 if (phy_ts)
366 phydev = dev->phydev;
367 }
368
369 cfg->source = phy_ts ? HWTSTAMP_SOURCE_PHYLIB : HWTSTAMP_SOURCE_NETDEV;
370
371 if (phy_ts && dev->see_all_hwtstamp_requests) {
372 err = ops->ndo_hwtstamp_get(dev, &old_cfg);
373 if (err)
374 return err;
375 }
376
377 if (!phy_ts || dev->see_all_hwtstamp_requests) {
378 err = ops->ndo_hwtstamp_set(dev, cfg, extack);
379 if (err) {
380 if (extack->_msg)
381 netdev_err(dev, "%s\n", extack->_msg);
382 return err;
383 }
384 }
385
386 if (phy_ts && dev->see_all_hwtstamp_requests)
387 changed = kernel_hwtstamp_config_changed(&old_cfg, cfg);
388
389 if (phy_ts) {
390 err = phy_hwtstamp_set(phydev, cfg, extack);
391 if (err) {
392 if (changed)
393 ops->ndo_hwtstamp_set(dev, &old_cfg, NULL);
394 return err;
395 }
396 }
397
398 return 0;
399}
400
401static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
402{
403 const struct net_device_ops *ops = dev->netdev_ops;
404 struct kernel_hwtstamp_config kernel_cfg = {};
405 struct netlink_ext_ack extack = {};
406 struct hwtstamp_config cfg;
407 int err;
408
409 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
410 return -EFAULT;
411
412 hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
413 kernel_cfg.ifr = ifr;
414
415 err = net_hwtstamp_validate(&kernel_cfg);
416 if (err)
417 return err;
418
419 err = dsa_conduit_hwtstamp_validate(dev, &kernel_cfg, &extack);
420 if (err) {
421 if (extack._msg)
422 netdev_err(dev, "%s\n", extack._msg);
423 return err;
424 }
425
426 if (!ops->ndo_hwtstamp_set)
427 return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); /* legacy */
428
429 if (!netif_device_present(dev))
430 return -ENODEV;
431
432 err = dev_set_hwtstamp_phylib(dev, &kernel_cfg, &extack);
433 if (err)
434 return err;
435
436 /* The driver may have modified the configuration, so copy the
437 * updated version of it back to user space
438 */
439 if (!kernel_cfg.copied_to_user) {
440 hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
441
442 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
443 return -EFAULT;
444 }
445
446 return 0;
447}
448
449static int generic_hwtstamp_ioctl_lower(struct net_device *dev, int cmd,
450 struct kernel_hwtstamp_config *kernel_cfg)
451{
452 struct ifreq ifrr;
453 int err;
454
455 strscpy_pad(ifrr.ifr_name, dev->name, IFNAMSIZ);
456 ifrr.ifr_ifru = kernel_cfg->ifr->ifr_ifru;
457
458 err = dev_eth_ioctl(dev, &ifrr, cmd);
459 if (err)
460 return err;
461
462 kernel_cfg->ifr->ifr_ifru = ifrr.ifr_ifru;
463 kernel_cfg->copied_to_user = true;
464
465 return 0;
466}
467
468int generic_hwtstamp_get_lower(struct net_device *dev,
469 struct kernel_hwtstamp_config *kernel_cfg)
470{
471 const struct net_device_ops *ops = dev->netdev_ops;
472
473 if (!netif_device_present(dev))
474 return -ENODEV;
475
476 if (ops->ndo_hwtstamp_get)
477 return dev_get_hwtstamp_phylib(dev, kernel_cfg);
478
479 /* Legacy path: unconverted lower driver */
480 return generic_hwtstamp_ioctl_lower(dev, SIOCGHWTSTAMP, kernel_cfg);
481}
482EXPORT_SYMBOL(generic_hwtstamp_get_lower);
483
484int generic_hwtstamp_set_lower(struct net_device *dev,
485 struct kernel_hwtstamp_config *kernel_cfg,
486 struct netlink_ext_ack *extack)
487{
488 const struct net_device_ops *ops = dev->netdev_ops;
489
490 if (!netif_device_present(dev))
491 return -ENODEV;
492
493 if (ops->ndo_hwtstamp_set)
494 return dev_set_hwtstamp_phylib(dev, kernel_cfg, extack);
495
496 /* Legacy path: unconverted lower driver */
497 return generic_hwtstamp_ioctl_lower(dev, SIOCSHWTSTAMP, kernel_cfg);
498}
499EXPORT_SYMBOL(generic_hwtstamp_set_lower);
500
501static int dev_siocbond(struct net_device *dev,
502 struct ifreq *ifr, unsigned int cmd)
503{
504 const struct net_device_ops *ops = dev->netdev_ops;
505
506 if (ops->ndo_siocbond) {
507 if (netif_device_present(dev))
508 return ops->ndo_siocbond(dev, ifr, cmd);
509 else
510 return -ENODEV;
511 }
512
513 return -EOPNOTSUPP;
514}
515
516static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
517 void __user *data, unsigned int cmd)
518{
519 const struct net_device_ops *ops = dev->netdev_ops;
520
521 if (ops->ndo_siocdevprivate) {
522 if (netif_device_present(dev))
523 return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
524 else
525 return -ENODEV;
526 }
527
528 return -EOPNOTSUPP;
529}
530
531static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
532{
533 const struct net_device_ops *ops = dev->netdev_ops;
534
535 if (ops->ndo_siocwandev) {
536 if (netif_device_present(dev))
537 return ops->ndo_siocwandev(dev, ifs);
538 else
539 return -ENODEV;
540 }
541
542 return -EOPNOTSUPP;
543}
544
545/*
546 * Perform the SIOCxIFxxx calls, inside rtnl_net_lock()
547 */
548static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
549 unsigned int cmd)
550{
551 int err;
552 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
553 const struct net_device_ops *ops;
554 netdevice_tracker dev_tracker;
555
556 if (!dev)
557 return -ENODEV;
558
559 ops = dev->netdev_ops;
560
561 switch (cmd) {
562 case SIOCSIFFLAGS: /* Set interface flags */
563 return dev_change_flags(dev, ifr->ifr_flags, NULL);
564
565 case SIOCSIFMETRIC: /* Set the metric on the interface
566 (currently unused) */
567 return -EOPNOTSUPP;
568
569 case SIOCSIFMTU: /* Set the MTU of a device */
570 return dev_set_mtu(dev, ifr->ifr_mtu);
571
572 case SIOCSIFHWADDR:
573 if (dev->addr_len > sizeof(struct sockaddr))
574 return -EINVAL;
575 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
576
577 case SIOCSIFHWBROADCAST:
578 if (ifr->ifr_hwaddr.sa_family != dev->type)
579 return -EINVAL;
580 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
581 min(sizeof(ifr->ifr_hwaddr.sa_data_min),
582 (size_t)dev->addr_len));
583 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
584 return 0;
585
586 case SIOCSIFMAP:
587 return dev_setifmap(dev, ifr);
588
589 case SIOCADDMULTI:
590 if (!ops->ndo_set_rx_mode ||
591 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
592 return -EINVAL;
593 if (!netif_device_present(dev))
594 return -ENODEV;
595 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
596
597 case SIOCDELMULTI:
598 if (!ops->ndo_set_rx_mode ||
599 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
600 return -EINVAL;
601 if (!netif_device_present(dev))
602 return -ENODEV;
603 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
604
605 case SIOCSIFTXQLEN:
606 if (ifr->ifr_qlen < 0)
607 return -EINVAL;
608 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
609
610 case SIOCSIFNAME:
611 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
612 return dev_change_name(dev, ifr->ifr_newname);
613
614 case SIOCWANDEV:
615 return dev_siocwandev(dev, &ifr->ifr_settings);
616
617 case SIOCBRADDIF:
618 case SIOCBRDELIF:
619 if (!netif_device_present(dev))
620 return -ENODEV;
621 if (!netif_is_bridge_master(dev))
622 return -EOPNOTSUPP;
623
624 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
625 rtnl_net_unlock(net);
626
627 err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
628
629 netdev_put(dev, &dev_tracker);
630 rtnl_net_lock(net);
631 return err;
632
633 case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
634 return dev_siocdevprivate(dev, ifr, data, cmd);
635
636 case SIOCSHWTSTAMP:
637 return dev_set_hwtstamp(dev, ifr);
638
639 case SIOCGHWTSTAMP:
640 return dev_get_hwtstamp(dev, ifr);
641
642 case SIOCGMIIPHY:
643 case SIOCGMIIREG:
644 case SIOCSMIIREG:
645 return dev_eth_ioctl(dev, ifr, cmd);
646
647 case SIOCBONDENSLAVE:
648 case SIOCBONDRELEASE:
649 case SIOCBONDSETHWADDR:
650 case SIOCBONDSLAVEINFOQUERY:
651 case SIOCBONDINFOQUERY:
652 case SIOCBONDCHANGEACTIVE:
653 return dev_siocbond(dev, ifr, cmd);
654
655 /* Unknown ioctl */
656 default:
657 err = -EINVAL;
658 }
659 return err;
660}
661
662/**
663 * dev_load - load a network module
664 * @net: the applicable net namespace
665 * @name: name of interface
666 *
667 * If a network interface is not present and the process has suitable
668 * privileges this function loads the module. If module loading is not
669 * available in this kernel then it becomes a nop.
670 */
671
672void dev_load(struct net *net, const char *name)
673{
674 struct net_device *dev;
675 int no_module;
676
677 rcu_read_lock();
678 dev = dev_get_by_name_rcu(net, name);
679 rcu_read_unlock();
680
681 no_module = !dev;
682 if (no_module && capable(CAP_NET_ADMIN))
683 no_module = request_module("netdev-%s", name);
684 if (no_module && capable(CAP_SYS_MODULE))
685 request_module("%s", name);
686}
687EXPORT_SYMBOL(dev_load);
688
689/*
690 * This function handles all "interface"-type I/O control requests. The actual
691 * 'doing' part of this is dev_ifsioc above.
692 */
693
694/**
695 * dev_ioctl - network device ioctl
696 * @net: the applicable net namespace
697 * @cmd: command to issue
698 * @ifr: pointer to a struct ifreq in user space
699 * @data: data exchanged with userspace
700 * @need_copyout: whether or not copy_to_user() should be called
701 *
702 * Issue ioctl functions to devices. This is normally called by the
703 * user space syscall interfaces but can sometimes be useful for
704 * other purposes. The return value is the return from the syscall if
705 * positive or a negative errno code on error.
706 */
707
708int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
709 void __user *data, bool *need_copyout)
710{
711 int ret;
712 char *colon;
713
714 if (need_copyout)
715 *need_copyout = true;
716 if (cmd == SIOCGIFNAME)
717 return dev_ifname(net, ifr);
718
719 ifr->ifr_name[IFNAMSIZ-1] = 0;
720
721 colon = strchr(ifr->ifr_name, ':');
722 if (colon)
723 *colon = 0;
724
725 /*
726 * See which interface the caller is talking about.
727 */
728
729 switch (cmd) {
730 case SIOCGIFHWADDR:
731 dev_load(net, ifr->ifr_name);
732 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
733 if (colon)
734 *colon = ':';
735 return ret;
736 /*
737 * These ioctl calls:
738 * - can be done by all.
739 * - atomic and do not require locking.
740 * - return a value
741 */
742 case SIOCGIFFLAGS:
743 case SIOCGIFMETRIC:
744 case SIOCGIFMTU:
745 case SIOCGIFSLAVE:
746 case SIOCGIFMAP:
747 case SIOCGIFINDEX:
748 case SIOCGIFTXQLEN:
749 dev_load(net, ifr->ifr_name);
750 rcu_read_lock();
751 ret = dev_ifsioc_locked(net, ifr, cmd);
752 rcu_read_unlock();
753 if (colon)
754 *colon = ':';
755 return ret;
756
757 case SIOCETHTOOL:
758 dev_load(net, ifr->ifr_name);
759 ret = dev_ethtool(net, ifr, data);
760 if (colon)
761 *colon = ':';
762 return ret;
763
764 /*
765 * These ioctl calls:
766 * - require superuser power.
767 * - require strict serialization.
768 * - return a value
769 */
770 case SIOCGMIIPHY:
771 case SIOCGMIIREG:
772 case SIOCSIFNAME:
773 dev_load(net, ifr->ifr_name);
774 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
775 return -EPERM;
776
777 rtnl_net_lock(net);
778 ret = dev_ifsioc(net, ifr, data, cmd);
779 rtnl_net_unlock(net);
780
781 if (colon)
782 *colon = ':';
783 return ret;
784
785 /*
786 * These ioctl calls:
787 * - require superuser power.
788 * - require strict serialization.
789 * - do not return a value
790 */
791 case SIOCSIFMAP:
792 case SIOCSIFTXQLEN:
793 if (!capable(CAP_NET_ADMIN))
794 return -EPERM;
795 fallthrough;
796 /*
797 * These ioctl calls:
798 * - require local superuser power.
799 * - require strict serialization.
800 * - do not return a value
801 */
802 case SIOCSIFFLAGS:
803 case SIOCSIFMETRIC:
804 case SIOCSIFMTU:
805 case SIOCSIFHWADDR:
806 case SIOCSIFSLAVE:
807 case SIOCADDMULTI:
808 case SIOCDELMULTI:
809 case SIOCSIFHWBROADCAST:
810 case SIOCSMIIREG:
811 case SIOCBONDENSLAVE:
812 case SIOCBONDRELEASE:
813 case SIOCBONDSETHWADDR:
814 case SIOCBONDCHANGEACTIVE:
815 case SIOCBRADDIF:
816 case SIOCBRDELIF:
817 case SIOCSHWTSTAMP:
818 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
819 return -EPERM;
820 fallthrough;
821 case SIOCBONDSLAVEINFOQUERY:
822 case SIOCBONDINFOQUERY:
823 dev_load(net, ifr->ifr_name);
824
825 rtnl_net_lock(net);
826 ret = dev_ifsioc(net, ifr, data, cmd);
827 rtnl_net_unlock(net);
828
829 if (need_copyout)
830 *need_copyout = false;
831 return ret;
832
833 case SIOCGIFMEM:
834 /* Get the per device memory space. We can add this but
835 * currently do not support it */
836 case SIOCSIFMEM:
837 /* Set the per device memory buffer space.
838 * Not applicable in our case */
839 case SIOCSIFLINK:
840 return -ENOTTY;
841
842 /*
843 * Unknown or private ioctl.
844 */
845 default:
846 if (cmd == SIOCWANDEV ||
847 cmd == SIOCGHWTSTAMP ||
848 (cmd >= SIOCDEVPRIVATE &&
849 cmd <= SIOCDEVPRIVATE + 15)) {
850 dev_load(net, ifr->ifr_name);
851
852 rtnl_net_lock(net);
853 ret = dev_ifsioc(net, ifr, data, cmd);
854 rtnl_net_unlock(net);
855 return ret;
856 }
857 return -ENOTTY;
858 }
859}