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