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