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