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