xref: /linux/net/core/dev_ioctl.c (revision 036778a4fad19b54b73ae229a1901665aa8750a2)
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 
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  */
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 
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 
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  */
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 
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), but
253  * phydev->mii_ts has both hwtstamp_get() and hwtstamp_set() methods. So this
254  * will return -EOPNOTSUPP for phylib only if hwtstamp_get() is not
255  * implemented for now, which is still more accurate than letting the netdev
256  * handle the GET request.
257  */
258 int dev_get_hwtstamp_phylib(struct net_device *dev,
259 			    struct kernel_hwtstamp_config *cfg)
260 {
261 	struct hwtstamp_provider *hwprov;
262 
263 	hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
264 	if (hwprov) {
265 		cfg->qualifier = hwprov->desc.qualifier;
266 		if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
267 		    hwprov->phydev)
268 			return phy_hwtstamp_get(hwprov->phydev, cfg);
269 
270 		if (hwprov->source == HWTSTAMP_SOURCE_NETDEV &&
271 		    dev->netdev_ops->ndo_hwtstamp_get)
272 			return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
273 
274 		return -EOPNOTSUPP;
275 	}
276 
277 	if (phy_is_default_hwtstamp(dev->phydev))
278 		return phy_hwtstamp_get(dev->phydev, cfg);
279 
280 	if (!dev->netdev_ops->ndo_hwtstamp_get)
281 		return -EOPNOTSUPP;
282 
283 	return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
284 }
285 
286 static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
287 {
288 	struct kernel_hwtstamp_config kernel_cfg = {};
289 	struct hwtstamp_config cfg;
290 	int err;
291 
292 	if (!netif_device_present(dev))
293 		return -ENODEV;
294 
295 	kernel_cfg.ifr = ifr;
296 	netdev_lock_ops(dev);
297 	err = dev_get_hwtstamp_phylib(dev, &kernel_cfg);
298 	netdev_unlock_ops(dev);
299 	if (err)
300 		return err;
301 
302 	/* If the request was resolved through an unconverted driver, omit
303 	 * the copy_to_user(), since the implementation has already done that
304 	 */
305 	if (!kernel_cfg.copied_to_user) {
306 		hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
307 
308 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
309 			return -EFAULT;
310 	}
311 
312 	return 0;
313 }
314 
315 /**
316  * dev_set_hwtstamp_phylib() - Change hardware timestamping of NIC
317  *	or of attached phylib PHY
318  * @dev: Network device
319  * @cfg: Timestamping configuration structure
320  * @extack: Netlink extended ack message structure, for error reporting
321  *
322  * Helper for enforcing a common policy that phylib timestamping, if available,
323  * should take precedence in front of hardware timestamping provided by the
324  * netdev. If the netdev driver needs to perform specific actions even for PHY
325  * timestamping to work properly (a switch port must trap the timestamped
326  * frames and not forward them), it must set dev->see_all_hwtstamp_requests.
327  */
328 int dev_set_hwtstamp_phylib(struct net_device *dev,
329 			    struct kernel_hwtstamp_config *cfg,
330 			    struct netlink_ext_ack *extack)
331 {
332 	const struct net_device_ops *ops = dev->netdev_ops;
333 	struct kernel_hwtstamp_config old_cfg = {};
334 	struct hwtstamp_provider *hwprov;
335 	struct phy_device *phydev;
336 	bool changed = false;
337 	bool phy_ts;
338 	int err;
339 
340 	hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
341 	if (hwprov) {
342 		if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
343 		    hwprov->phydev) {
344 			phy_ts = true;
345 			phydev = hwprov->phydev;
346 		} else if (hwprov->source == HWTSTAMP_SOURCE_NETDEV) {
347 			phy_ts = false;
348 		} else {
349 			return -EOPNOTSUPP;
350 		}
351 
352 		cfg->qualifier = hwprov->desc.qualifier;
353 	} else {
354 		phy_ts = phy_is_default_hwtstamp(dev->phydev);
355 		if (phy_ts)
356 			phydev = dev->phydev;
357 	}
358 
359 	cfg->source = phy_ts ? HWTSTAMP_SOURCE_PHYLIB : HWTSTAMP_SOURCE_NETDEV;
360 
361 	if (phy_ts && dev->see_all_hwtstamp_requests) {
362 		if (!ops->ndo_hwtstamp_get)
363 			return -EOPNOTSUPP;
364 
365 		err = ops->ndo_hwtstamp_get(dev, &old_cfg);
366 		if (err)
367 			return err;
368 	}
369 
370 	if (!phy_ts || dev->see_all_hwtstamp_requests) {
371 		if (!ops->ndo_hwtstamp_set)
372 			return -EOPNOTSUPP;
373 
374 		err = ops->ndo_hwtstamp_set(dev, cfg, extack);
375 		if (err) {
376 			if (extack->_msg)
377 				netdev_err(dev, "%s\n", extack->_msg);
378 			return err;
379 		}
380 	}
381 
382 	if (phy_ts && dev->see_all_hwtstamp_requests)
383 		changed = kernel_hwtstamp_config_changed(&old_cfg, cfg);
384 
385 	if (phy_ts) {
386 		err = phy_hwtstamp_set(phydev, cfg, extack);
387 		if (err) {
388 			if (changed)
389 				ops->ndo_hwtstamp_set(dev, &old_cfg, NULL);
390 			return err;
391 		}
392 	}
393 
394 	return 0;
395 }
396 
397 static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
398 {
399 	struct kernel_hwtstamp_config kernel_cfg = {};
400 	struct netlink_ext_ack extack = {};
401 	struct hwtstamp_config cfg;
402 	int err;
403 
404 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
405 		return -EFAULT;
406 
407 	hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
408 	kernel_cfg.ifr = ifr;
409 
410 	err = net_hwtstamp_validate(&kernel_cfg);
411 	if (err)
412 		return err;
413 
414 	err = dsa_conduit_hwtstamp_validate(dev, &kernel_cfg, &extack);
415 	if (err) {
416 		if (extack._msg)
417 			netdev_err(dev, "%s\n", extack._msg);
418 		return err;
419 	}
420 
421 	if (!netif_device_present(dev))
422 		return -ENODEV;
423 
424 	netdev_lock_ops(dev);
425 	err = dev_set_hwtstamp_phylib(dev, &kernel_cfg, &extack);
426 	netdev_unlock_ops(dev);
427 	if (err)
428 		return err;
429 
430 	/* The driver may have modified the configuration, so copy the
431 	 * updated version of it back to user space
432 	 */
433 	if (!kernel_cfg.copied_to_user) {
434 		hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
435 
436 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
437 			return -EFAULT;
438 	}
439 
440 	return 0;
441 }
442 
443 int generic_hwtstamp_get_lower(struct net_device *dev,
444 			       struct kernel_hwtstamp_config *kernel_cfg)
445 {
446 	int err;
447 
448 	if (!netif_device_present(dev))
449 		return -ENODEV;
450 
451 	netdev_lock_ops(dev);
452 	err = dev_get_hwtstamp_phylib(dev, kernel_cfg);
453 	netdev_unlock_ops(dev);
454 
455 	return err;
456 }
457 EXPORT_SYMBOL(generic_hwtstamp_get_lower);
458 
459 int generic_hwtstamp_set_lower(struct net_device *dev,
460 			       struct kernel_hwtstamp_config *kernel_cfg,
461 			       struct netlink_ext_ack *extack)
462 {
463 	int err;
464 
465 	if (!netif_device_present(dev))
466 		return -ENODEV;
467 
468 	netdev_lock_ops(dev);
469 	err = dev_set_hwtstamp_phylib(dev, kernel_cfg, extack);
470 	netdev_unlock_ops(dev);
471 
472 	return err;
473 }
474 EXPORT_SYMBOL(generic_hwtstamp_set_lower);
475 
476 static int dev_siocbond(struct net_device *dev,
477 			struct ifreq *ifr, unsigned int cmd)
478 {
479 	const struct net_device_ops *ops = dev->netdev_ops;
480 
481 	if (ops->ndo_siocbond) {
482 		int ret = -ENODEV;
483 
484 		netdev_lock_ops(dev);
485 		if (netif_device_present(dev))
486 			ret = ops->ndo_siocbond(dev, ifr, cmd);
487 		netdev_unlock_ops(dev);
488 
489 		return ret;
490 	}
491 
492 	return -EOPNOTSUPP;
493 }
494 
495 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
496 			      void __user *data, unsigned int cmd)
497 {
498 	const struct net_device_ops *ops = dev->netdev_ops;
499 
500 	if (ops->ndo_siocdevprivate) {
501 		int ret = -ENODEV;
502 
503 		netdev_lock_ops(dev);
504 		if (netif_device_present(dev))
505 			ret = ops->ndo_siocdevprivate(dev, ifr, data, cmd);
506 		netdev_unlock_ops(dev);
507 
508 		return ret;
509 	}
510 
511 	return -EOPNOTSUPP;
512 }
513 
514 static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
515 {
516 	const struct net_device_ops *ops = dev->netdev_ops;
517 
518 	if (ops->ndo_siocwandev) {
519 		int ret = -ENODEV;
520 
521 		netdev_lock_ops(dev);
522 		if (netif_device_present(dev))
523 			ret = ops->ndo_siocwandev(dev, ifs);
524 		netdev_unlock_ops(dev);
525 
526 		return ret;
527 	}
528 
529 	return -EOPNOTSUPP;
530 }
531 
532 /*
533  *	Perform the SIOCxIFxxx calls, inside rtnl_net_lock()
534  */
535 static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
536 		      unsigned int cmd)
537 {
538 	int err;
539 	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
540 	const struct net_device_ops *ops;
541 
542 	if (!dev)
543 		return -ENODEV;
544 
545 	ops = dev->netdev_ops;
546 
547 	switch (cmd) {
548 	case SIOCSIFFLAGS:	/* Set interface flags */
549 		return dev_change_flags(dev, ifr->ifr_flags, NULL);
550 
551 	case SIOCSIFMETRIC:	/* Set the metric on the interface
552 				   (currently unused) */
553 		return -EOPNOTSUPP;
554 
555 	case SIOCSIFMTU:	/* Set the MTU of a device */
556 		return dev_set_mtu(dev, ifr->ifr_mtu);
557 
558 	case SIOCSIFHWADDR:
559 		if (dev->addr_len > sizeof(ifr->ifr_hwaddr))
560 			return -EINVAL;
561 		return dev_set_mac_address_user(dev,
562 						(struct sockaddr_storage *)&ifr->ifr_hwaddr,
563 						NULL);
564 
565 	case SIOCSIFHWBROADCAST:
566 		if (ifr->ifr_hwaddr.sa_family != dev->type)
567 			return -EINVAL;
568 		memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
569 		       min(sizeof(ifr->ifr_hwaddr.sa_data),
570 			   (size_t)dev->addr_len));
571 		netdev_lock_ops(dev);
572 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
573 		netdev_unlock_ops(dev);
574 		return 0;
575 
576 	case SIOCSIFMAP:
577 		netdev_lock_ops(dev);
578 		err = netif_setifmap(dev, ifr);
579 		netdev_unlock_ops(dev);
580 		return err;
581 
582 	case SIOCADDMULTI:
583 		if ((!ops->ndo_set_rx_mode && !ops->ndo_set_rx_mode_async) ||
584 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
585 			return -EINVAL;
586 		if (!netif_device_present(dev))
587 			return -ENODEV;
588 		netdev_lock_ops(dev);
589 		err = dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
590 		netif_rx_mode_sync(dev);
591 		netdev_unlock_ops(dev);
592 		return err;
593 
594 	case SIOCDELMULTI:
595 		if ((!ops->ndo_set_rx_mode && !ops->ndo_set_rx_mode_async) ||
596 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
597 			return -EINVAL;
598 		if (!netif_device_present(dev))
599 			return -ENODEV;
600 		netdev_lock_ops(dev);
601 		err = dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
602 		netif_rx_mode_sync(dev);
603 		netdev_unlock_ops(dev);
604 		return err;
605 
606 	case SIOCSIFTXQLEN:
607 		if (ifr->ifr_qlen < 0)
608 			return -EINVAL;
609 		return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
610 
611 	case SIOCSIFNAME:
612 		ifr->ifr_newname[IFNAMSIZ-1] = '\0';
613 		return dev_change_name(dev, ifr->ifr_newname);
614 
615 	case SIOCWANDEV:
616 		return dev_siocwandev(dev, &ifr->ifr_settings);
617 
618 	case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
619 		return dev_siocdevprivate(dev, ifr, data, cmd);
620 
621 	case SIOCSHWTSTAMP:
622 		return dev_set_hwtstamp(dev, ifr);
623 
624 	case SIOCGHWTSTAMP:
625 		return dev_get_hwtstamp(dev, ifr);
626 
627 	case SIOCGMIIPHY:
628 	case SIOCGMIIREG:
629 	case SIOCSMIIREG:
630 		return dev_eth_ioctl(dev, ifr, cmd);
631 
632 	case SIOCBONDENSLAVE:
633 	case SIOCBONDRELEASE:
634 	case SIOCBONDSETHWADDR:
635 	case SIOCBONDSLAVEINFOQUERY:
636 	case SIOCBONDINFOQUERY:
637 	case SIOCBONDCHANGEACTIVE:
638 		return dev_siocbond(dev, ifr, cmd);
639 
640 	/* Unknown ioctl */
641 	default:
642 		err = -EINVAL;
643 	}
644 	return err;
645 }
646 
647 /**
648  *	dev_load 	- load a network module
649  *	@net: the applicable net namespace
650  *	@name: name of interface
651  *
652  *	If a network interface is not present and the process has suitable
653  *	privileges this function loads the module. If module loading is not
654  *	available in this kernel then it becomes a nop.
655  */
656 
657 void dev_load(struct net *net, const char *name)
658 {
659 	struct net_device *dev;
660 	int no_module;
661 
662 	rcu_read_lock();
663 	dev = dev_get_by_name_rcu(net, name);
664 	rcu_read_unlock();
665 
666 	no_module = !dev;
667 	if (no_module && capable(CAP_NET_ADMIN))
668 		no_module = request_module("netdev-%s", name);
669 	if (no_module && capable(CAP_SYS_MODULE))
670 		request_module("%s", name);
671 }
672 EXPORT_SYMBOL(dev_load);
673 
674 /*
675  *	This function handles all "interface"-type I/O control requests. The actual
676  *	'doing' part of this is dev_ifsioc above.
677  */
678 
679 /**
680  *	dev_ioctl	-	network device ioctl
681  *	@net: the applicable net namespace
682  *	@cmd: command to issue
683  *	@ifr: pointer to a struct ifreq in user space
684  *	@data: data exchanged with userspace
685  *	@need_copyout: whether or not copy_to_user() should be called
686  *
687  *	Issue ioctl functions to devices. This is normally called by the
688  *	user space syscall interfaces but can sometimes be useful for
689  *	other purposes. The return value is the return from the syscall if
690  *	positive or a negative errno code on error.
691  */
692 
693 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
694 	      void __user *data, bool *need_copyout)
695 {
696 	int ret;
697 	char *colon;
698 
699 	if (need_copyout)
700 		*need_copyout = true;
701 	if (cmd == SIOCGIFNAME)
702 		return dev_ifname(net, ifr);
703 
704 	ifr->ifr_name[IFNAMSIZ-1] = 0;
705 
706 	colon = strchr(ifr->ifr_name, ':');
707 	if (colon)
708 		*colon = 0;
709 
710 	/*
711 	 *	See which interface the caller is talking about.
712 	 */
713 
714 	switch (cmd) {
715 	case SIOCGIFHWADDR:
716 		dev_load(net, ifr->ifr_name);
717 		ret = netif_get_mac_address(&ifr->ifr_hwaddr, net,
718 					    ifr->ifr_name);
719 		if (colon)
720 			*colon = ':';
721 		return ret;
722 	/*
723 	 *	These ioctl calls:
724 	 *	- can be done by all.
725 	 *	- atomic and do not require locking.
726 	 *	- return a value
727 	 */
728 	case SIOCGIFFLAGS:
729 	case SIOCGIFMETRIC:
730 	case SIOCGIFMTU:
731 	case SIOCGIFSLAVE:
732 	case SIOCGIFMAP:
733 	case SIOCGIFINDEX:
734 	case SIOCGIFTXQLEN:
735 		dev_load(net, ifr->ifr_name);
736 		rcu_read_lock();
737 		ret = dev_ifsioc_locked(net, ifr, cmd);
738 		rcu_read_unlock();
739 		if (colon)
740 			*colon = ':';
741 		return ret;
742 
743 	case SIOCETHTOOL:
744 		dev_load(net, ifr->ifr_name);
745 		ret = dev_ethtool(net, ifr, data);
746 		if (colon)
747 			*colon = ':';
748 		return ret;
749 
750 	/*
751 	 *	These ioctl calls:
752 	 *	- require superuser power.
753 	 *	- require strict serialization.
754 	 *	- return a value
755 	 */
756 	case SIOCGMIIPHY:
757 	case SIOCGMIIREG:
758 	case SIOCSIFNAME:
759 		dev_load(net, ifr->ifr_name);
760 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
761 			return -EPERM;
762 
763 		rtnl_net_lock(net);
764 		ret = dev_ifsioc(net, ifr, data, cmd);
765 		rtnl_net_unlock(net);
766 
767 		if (colon)
768 			*colon = ':';
769 		return ret;
770 
771 	/*
772 	 *	These ioctl calls:
773 	 *	- require superuser power.
774 	 *	- require strict serialization.
775 	 *	- do not return a value
776 	 */
777 	case SIOCSIFMAP:
778 	case SIOCSIFTXQLEN:
779 		if (!capable(CAP_NET_ADMIN))
780 			return -EPERM;
781 		fallthrough;
782 	/*
783 	 *	These ioctl calls:
784 	 *	- require local superuser power.
785 	 *	- require strict serialization.
786 	 *	- do not return a value
787 	 */
788 	case SIOCSIFFLAGS:
789 	case SIOCSIFMETRIC:
790 	case SIOCSIFMTU:
791 	case SIOCSIFHWADDR:
792 	case SIOCSIFSLAVE:
793 	case SIOCADDMULTI:
794 	case SIOCDELMULTI:
795 	case SIOCSIFHWBROADCAST:
796 	case SIOCSMIIREG:
797 	case SIOCBONDENSLAVE:
798 	case SIOCBONDRELEASE:
799 	case SIOCBONDSETHWADDR:
800 	case SIOCBONDCHANGEACTIVE:
801 	case SIOCSHWTSTAMP:
802 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
803 			return -EPERM;
804 		fallthrough;
805 	case SIOCBONDSLAVEINFOQUERY:
806 	case SIOCBONDINFOQUERY:
807 		dev_load(net, ifr->ifr_name);
808 
809 		rtnl_net_lock(net);
810 		ret = dev_ifsioc(net, ifr, data, cmd);
811 		rtnl_net_unlock(net);
812 
813 		if (need_copyout)
814 			*need_copyout = false;
815 		return ret;
816 
817 	case SIOCGIFMEM:
818 		/* Get the per device memory space. We can add this but
819 		 * currently do not support it */
820 	case SIOCSIFMEM:
821 		/* Set the per device memory buffer space.
822 		 * Not applicable in our case */
823 	case SIOCSIFLINK:
824 		return -ENOTTY;
825 
826 	/*
827 	 *	Unknown or private ioctl.
828 	 */
829 	default:
830 		if (cmd == SIOCWANDEV ||
831 		    cmd == SIOCGHWTSTAMP ||
832 		    (cmd >= SIOCDEVPRIVATE &&
833 		     cmd <= SIOCDEVPRIVATE + 15)) {
834 			dev_load(net, ifr->ifr_name);
835 
836 			rtnl_net_lock(net);
837 			ret = dev_ifsioc(net, ifr, data, cmd);
838 			rtnl_net_unlock(net);
839 			return ret;
840 		}
841 		return -ENOTTY;
842 	}
843 }
844