xref: /linux/net/core/dev_ioctl.c (revision 1cc3462159babb69c84c39cb1b4e262aef3ea325)
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 
28 static 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  */
39 int 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 
86 static 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 
113 static int netif_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  */
139 static 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 
188 int 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 
243 /**
244  * dev_get_hwtstamp_phylib() - Get hardware timestamping settings of NIC
245  *	or of attached phylib PHY
246  * @dev: Network device
247  * @cfg: Timestamping configuration structure
248  *
249  * Helper for calling the default hardware provider timestamping.
250  *
251  * Note: phy_mii_ioctl() only handles SIOCSHWTSTAMP (not SIOCGHWTSTAMP), and
252  * there only exists a phydev->mii_ts->hwtstamp() method. So this will return
253  * -EOPNOTSUPP for phylib for now, which is still more accurate than letting
254  * the netdev handle the GET request.
255  */
256 int dev_get_hwtstamp_phylib(struct net_device *dev,
257 			    struct kernel_hwtstamp_config *cfg)
258 {
259 	struct hwtstamp_provider *hwprov;
260 
261 	hwprov = rtnl_dereference(dev->hwprov);
262 	if (hwprov) {
263 		cfg->qualifier = hwprov->desc.qualifier;
264 		if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
265 		    hwprov->phydev)
266 			return phy_hwtstamp_get(hwprov->phydev, cfg);
267 
268 		if (hwprov->source == HWTSTAMP_SOURCE_NETDEV)
269 			return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
270 
271 		return -EOPNOTSUPP;
272 	}
273 
274 	if (phy_is_default_hwtstamp(dev->phydev))
275 		return phy_hwtstamp_get(dev->phydev, cfg);
276 
277 	return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
278 }
279 
280 static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
281 {
282 	const struct net_device_ops *ops = dev->netdev_ops;
283 	struct kernel_hwtstamp_config kernel_cfg = {};
284 	struct hwtstamp_config cfg;
285 	int err;
286 
287 	if (!ops->ndo_hwtstamp_get)
288 		return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); /* legacy */
289 
290 	if (!netif_device_present(dev))
291 		return -ENODEV;
292 
293 	kernel_cfg.ifr = ifr;
294 	netdev_lock_ops(dev);
295 	err = dev_get_hwtstamp_phylib(dev, &kernel_cfg);
296 	netdev_unlock_ops(dev);
297 	if (err)
298 		return err;
299 
300 	/* If the request was resolved through an unconverted driver, omit
301 	 * the copy_to_user(), since the implementation has already done that
302 	 */
303 	if (!kernel_cfg.copied_to_user) {
304 		hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
305 
306 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
307 			return -EFAULT;
308 	}
309 
310 	return 0;
311 }
312 
313 /**
314  * dev_set_hwtstamp_phylib() - Change hardware timestamping of NIC
315  *	or of attached phylib PHY
316  * @dev: Network device
317  * @cfg: Timestamping configuration structure
318  * @extack: Netlink extended ack message structure, for error reporting
319  *
320  * Helper for enforcing a common policy that phylib timestamping, if available,
321  * should take precedence in front of hardware timestamping provided by the
322  * netdev. If the netdev driver needs to perform specific actions even for PHY
323  * timestamping to work properly (a switch port must trap the timestamped
324  * frames and not forward them), it must set dev->see_all_hwtstamp_requests.
325  */
326 int dev_set_hwtstamp_phylib(struct net_device *dev,
327 			    struct kernel_hwtstamp_config *cfg,
328 			    struct netlink_ext_ack *extack)
329 {
330 	const struct net_device_ops *ops = dev->netdev_ops;
331 	struct kernel_hwtstamp_config old_cfg = {};
332 	struct hwtstamp_provider *hwprov;
333 	struct phy_device *phydev;
334 	bool changed = false;
335 	bool phy_ts;
336 	int err;
337 
338 	hwprov = rtnl_dereference(dev->hwprov);
339 	if (hwprov) {
340 		if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
341 		    hwprov->phydev) {
342 			phy_ts = true;
343 			phydev = hwprov->phydev;
344 		} else if (hwprov->source == HWTSTAMP_SOURCE_NETDEV) {
345 			phy_ts = false;
346 		} else {
347 			return -EOPNOTSUPP;
348 		}
349 
350 		cfg->qualifier = hwprov->desc.qualifier;
351 	} else {
352 		phy_ts = phy_is_default_hwtstamp(dev->phydev);
353 		if (phy_ts)
354 			phydev = dev->phydev;
355 	}
356 
357 	cfg->source = phy_ts ? HWTSTAMP_SOURCE_PHYLIB : HWTSTAMP_SOURCE_NETDEV;
358 
359 	if (phy_ts && dev->see_all_hwtstamp_requests) {
360 		err = ops->ndo_hwtstamp_get(dev, &old_cfg);
361 		if (err)
362 			return err;
363 	}
364 
365 	if (!phy_ts || dev->see_all_hwtstamp_requests) {
366 		err = ops->ndo_hwtstamp_set(dev, cfg, extack);
367 		if (err) {
368 			if (extack->_msg)
369 				netdev_err(dev, "%s\n", extack->_msg);
370 			return err;
371 		}
372 	}
373 
374 	if (phy_ts && dev->see_all_hwtstamp_requests)
375 		changed = kernel_hwtstamp_config_changed(&old_cfg, cfg);
376 
377 	if (phy_ts) {
378 		err = phy_hwtstamp_set(phydev, cfg, extack);
379 		if (err) {
380 			if (changed)
381 				ops->ndo_hwtstamp_set(dev, &old_cfg, NULL);
382 			return err;
383 		}
384 	}
385 
386 	return 0;
387 }
388 
389 static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
390 {
391 	const struct net_device_ops *ops = dev->netdev_ops;
392 	struct kernel_hwtstamp_config kernel_cfg = {};
393 	struct netlink_ext_ack extack = {};
394 	struct hwtstamp_config cfg;
395 	int err;
396 
397 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
398 		return -EFAULT;
399 
400 	hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
401 	kernel_cfg.ifr = ifr;
402 
403 	err = net_hwtstamp_validate(&kernel_cfg);
404 	if (err)
405 		return err;
406 
407 	err = dsa_conduit_hwtstamp_validate(dev, &kernel_cfg, &extack);
408 	if (err) {
409 		if (extack._msg)
410 			netdev_err(dev, "%s\n", extack._msg);
411 		return err;
412 	}
413 
414 	if (!ops->ndo_hwtstamp_set)
415 		return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); /* legacy */
416 
417 	if (!netif_device_present(dev))
418 		return -ENODEV;
419 
420 	netdev_lock_ops(dev);
421 	err = dev_set_hwtstamp_phylib(dev, &kernel_cfg, &extack);
422 	netdev_unlock_ops(dev);
423 	if (err)
424 		return err;
425 
426 	/* The driver may have modified the configuration, so copy the
427 	 * updated version of it back to user space
428 	 */
429 	if (!kernel_cfg.copied_to_user) {
430 		hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
431 
432 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
433 			return -EFAULT;
434 	}
435 
436 	return 0;
437 }
438 
439 static int generic_hwtstamp_ioctl_lower(struct net_device *dev, int cmd,
440 					struct kernel_hwtstamp_config *kernel_cfg)
441 {
442 	struct ifreq ifrr;
443 	int err;
444 
445 	strscpy_pad(ifrr.ifr_name, dev->name, IFNAMSIZ);
446 	ifrr.ifr_ifru = kernel_cfg->ifr->ifr_ifru;
447 
448 	err = dev_eth_ioctl(dev, &ifrr, cmd);
449 	if (err)
450 		return err;
451 
452 	kernel_cfg->ifr->ifr_ifru = ifrr.ifr_ifru;
453 	kernel_cfg->copied_to_user = true;
454 
455 	return 0;
456 }
457 
458 int generic_hwtstamp_get_lower(struct net_device *dev,
459 			       struct kernel_hwtstamp_config *kernel_cfg)
460 {
461 	const struct net_device_ops *ops = dev->netdev_ops;
462 
463 	if (!netif_device_present(dev))
464 		return -ENODEV;
465 
466 	if (ops->ndo_hwtstamp_get)
467 		return dev_get_hwtstamp_phylib(dev, kernel_cfg);
468 
469 	/* Legacy path: unconverted lower driver */
470 	return generic_hwtstamp_ioctl_lower(dev, SIOCGHWTSTAMP, kernel_cfg);
471 }
472 EXPORT_SYMBOL(generic_hwtstamp_get_lower);
473 
474 int generic_hwtstamp_set_lower(struct net_device *dev,
475 			       struct kernel_hwtstamp_config *kernel_cfg,
476 			       struct netlink_ext_ack *extack)
477 {
478 	const struct net_device_ops *ops = dev->netdev_ops;
479 
480 	if (!netif_device_present(dev))
481 		return -ENODEV;
482 
483 	if (ops->ndo_hwtstamp_set)
484 		return dev_set_hwtstamp_phylib(dev, kernel_cfg, extack);
485 
486 	/* Legacy path: unconverted lower driver */
487 	return generic_hwtstamp_ioctl_lower(dev, SIOCSHWTSTAMP, kernel_cfg);
488 }
489 EXPORT_SYMBOL(generic_hwtstamp_set_lower);
490 
491 static int dev_siocbond(struct net_device *dev,
492 			struct ifreq *ifr, unsigned int cmd)
493 {
494 	const struct net_device_ops *ops = dev->netdev_ops;
495 
496 	if (ops->ndo_siocbond) {
497 		int ret = -ENODEV;
498 
499 		netdev_lock_ops(dev);
500 		if (netif_device_present(dev))
501 			ret = ops->ndo_siocbond(dev, ifr, cmd);
502 		netdev_unlock_ops(dev);
503 
504 		return ret;
505 	}
506 
507 	return -EOPNOTSUPP;
508 }
509 
510 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
511 			      void __user *data, unsigned int cmd)
512 {
513 	const struct net_device_ops *ops = dev->netdev_ops;
514 
515 	if (ops->ndo_siocdevprivate) {
516 		int ret = -ENODEV;
517 
518 		netdev_lock_ops(dev);
519 		if (netif_device_present(dev))
520 			ret = ops->ndo_siocdevprivate(dev, ifr, data, cmd);
521 		netdev_unlock_ops(dev);
522 
523 		return ret;
524 	}
525 
526 	return -EOPNOTSUPP;
527 }
528 
529 static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
530 {
531 	const struct net_device_ops *ops = dev->netdev_ops;
532 
533 	if (ops->ndo_siocwandev) {
534 		int ret = -ENODEV;
535 
536 		netdev_lock_ops(dev);
537 		if (netif_device_present(dev))
538 			ret = ops->ndo_siocwandev(dev, ifs);
539 		netdev_unlock_ops(dev);
540 
541 		return ret;
542 	}
543 
544 	return -EOPNOTSUPP;
545 }
546 
547 /*
548  *	Perform the SIOCxIFxxx calls, inside rtnl_net_lock()
549  */
550 static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
551 		      unsigned int cmd)
552 {
553 	int err;
554 	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
555 	const struct net_device_ops *ops;
556 	netdevice_tracker dev_tracker;
557 
558 	if (!dev)
559 		return -ENODEV;
560 
561 	ops = dev->netdev_ops;
562 
563 	switch (cmd) {
564 	case SIOCSIFFLAGS:	/* Set interface flags */
565 		return dev_change_flags(dev, ifr->ifr_flags, NULL);
566 
567 	case SIOCSIFMETRIC:	/* Set the metric on the interface
568 				   (currently unused) */
569 		return -EOPNOTSUPP;
570 
571 	case SIOCSIFMTU:	/* Set the MTU of a device */
572 		return dev_set_mtu(dev, ifr->ifr_mtu);
573 
574 	case SIOCSIFHWADDR:
575 		if (dev->addr_len > sizeof(struct sockaddr))
576 			return -EINVAL;
577 		return dev_set_mac_address(dev, &ifr->ifr_hwaddr, NULL);
578 
579 	case SIOCSIFHWBROADCAST:
580 		if (ifr->ifr_hwaddr.sa_family != dev->type)
581 			return -EINVAL;
582 		memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
583 		       min(sizeof(ifr->ifr_hwaddr.sa_data_min),
584 			   (size_t)dev->addr_len));
585 		netdev_lock_ops(dev);
586 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
587 		netdev_unlock_ops(dev);
588 		return 0;
589 
590 	case SIOCSIFMAP:
591 		netdev_lock_ops(dev);
592 		err = netif_setifmap(dev, ifr);
593 		netdev_unlock_ops(dev);
594 		return err;
595 
596 	case SIOCADDMULTI:
597 		if (!ops->ndo_set_rx_mode ||
598 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
599 			return -EINVAL;
600 		if (!netif_device_present(dev))
601 			return -ENODEV;
602 		netdev_lock_ops(dev);
603 		err = dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
604 		netdev_unlock_ops(dev);
605 		return err;
606 
607 	case SIOCDELMULTI:
608 		if (!ops->ndo_set_rx_mode ||
609 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
610 			return -EINVAL;
611 		if (!netif_device_present(dev))
612 			return -ENODEV;
613 		netdev_lock_ops(dev);
614 		err = dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
615 		netdev_unlock_ops(dev);
616 		return err;
617 
618 	case SIOCSIFTXQLEN:
619 		if (ifr->ifr_qlen < 0)
620 			return -EINVAL;
621 		return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
622 
623 	case SIOCSIFNAME:
624 		ifr->ifr_newname[IFNAMSIZ-1] = '\0';
625 		return dev_change_name(dev, ifr->ifr_newname);
626 
627 	case SIOCWANDEV:
628 		return dev_siocwandev(dev, &ifr->ifr_settings);
629 
630 	case SIOCBRADDIF:
631 	case SIOCBRDELIF:
632 		if (!netif_device_present(dev))
633 			return -ENODEV;
634 		if (!netif_is_bridge_master(dev))
635 			return -EOPNOTSUPP;
636 
637 		netdev_hold(dev, &dev_tracker, GFP_KERNEL);
638 		rtnl_net_unlock(net);
639 
640 		err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
641 
642 		netdev_put(dev, &dev_tracker);
643 		rtnl_net_lock(net);
644 		return err;
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 
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 
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 = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
746 		if (colon)
747 			*colon = ':';
748 		return ret;
749 	/*
750 	 *	These ioctl calls:
751 	 *	- can be done by all.
752 	 *	- atomic and do not require locking.
753 	 *	- return a value
754 	 */
755 	case SIOCGIFFLAGS:
756 	case SIOCGIFMETRIC:
757 	case SIOCGIFMTU:
758 	case SIOCGIFSLAVE:
759 	case SIOCGIFMAP:
760 	case SIOCGIFINDEX:
761 	case SIOCGIFTXQLEN:
762 		dev_load(net, ifr->ifr_name);
763 		rcu_read_lock();
764 		ret = dev_ifsioc_locked(net, ifr, cmd);
765 		rcu_read_unlock();
766 		if (colon)
767 			*colon = ':';
768 		return ret;
769 
770 	case SIOCETHTOOL:
771 		dev_load(net, ifr->ifr_name);
772 		ret = dev_ethtool(net, ifr, data);
773 		if (colon)
774 			*colon = ':';
775 		return ret;
776 
777 	/*
778 	 *	These ioctl calls:
779 	 *	- require superuser power.
780 	 *	- require strict serialization.
781 	 *	- return a value
782 	 */
783 	case SIOCGMIIPHY:
784 	case SIOCGMIIREG:
785 	case SIOCSIFNAME:
786 		dev_load(net, ifr->ifr_name);
787 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
788 			return -EPERM;
789 
790 		rtnl_net_lock(net);
791 		ret = dev_ifsioc(net, ifr, data, cmd);
792 		rtnl_net_unlock(net);
793 
794 		if (colon)
795 			*colon = ':';
796 		return ret;
797 
798 	/*
799 	 *	These ioctl calls:
800 	 *	- require superuser power.
801 	 *	- require strict serialization.
802 	 *	- do not return a value
803 	 */
804 	case SIOCSIFMAP:
805 	case SIOCSIFTXQLEN:
806 		if (!capable(CAP_NET_ADMIN))
807 			return -EPERM;
808 		fallthrough;
809 	/*
810 	 *	These ioctl calls:
811 	 *	- require local superuser power.
812 	 *	- require strict serialization.
813 	 *	- do not return a value
814 	 */
815 	case SIOCSIFFLAGS:
816 	case SIOCSIFMETRIC:
817 	case SIOCSIFMTU:
818 	case SIOCSIFHWADDR:
819 	case SIOCSIFSLAVE:
820 	case SIOCADDMULTI:
821 	case SIOCDELMULTI:
822 	case SIOCSIFHWBROADCAST:
823 	case SIOCSMIIREG:
824 	case SIOCBONDENSLAVE:
825 	case SIOCBONDRELEASE:
826 	case SIOCBONDSETHWADDR:
827 	case SIOCBONDCHANGEACTIVE:
828 	case SIOCBRADDIF:
829 	case SIOCBRDELIF:
830 	case SIOCSHWTSTAMP:
831 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
832 			return -EPERM;
833 		fallthrough;
834 	case SIOCBONDSLAVEINFOQUERY:
835 	case SIOCBONDINFOQUERY:
836 		dev_load(net, ifr->ifr_name);
837 
838 		rtnl_net_lock(net);
839 		ret = dev_ifsioc(net, ifr, data, cmd);
840 		rtnl_net_unlock(net);
841 
842 		if (need_copyout)
843 			*need_copyout = false;
844 		return ret;
845 
846 	case SIOCGIFMEM:
847 		/* Get the per device memory space. We can add this but
848 		 * currently do not support it */
849 	case SIOCSIFMEM:
850 		/* Set the per device memory buffer space.
851 		 * Not applicable in our case */
852 	case SIOCSIFLINK:
853 		return -ENOTTY;
854 
855 	/*
856 	 *	Unknown or private ioctl.
857 	 */
858 	default:
859 		if (cmd == SIOCWANDEV ||
860 		    cmd == SIOCGHWTSTAMP ||
861 		    (cmd >= SIOCDEVPRIVATE &&
862 		     cmd <= SIOCDEVPRIVATE + 15)) {
863 			dev_load(net, ifr->ifr_name);
864 
865 			rtnl_net_lock(net);
866 			ret = dev_ifsioc(net, ifr, data, cmd);
867 			rtnl_net_unlock(net);
868 			return ret;
869 		}
870 		return -ENOTTY;
871 	}
872 }
873