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 
dev_ifname(struct net * net,struct ifreq * ifr)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  */
dev_ifconf(struct net * net,struct ifconf __user * uifc)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 
dev_getifmap(struct net_device * dev,struct ifreq * ifr)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 
dev_setifmap(struct net_device * dev,struct ifreq * ifr)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  */
dev_ifsioc_locked(struct net * net,struct ifreq * ifr,unsigned int cmd)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 
net_hwtstamp_validate(const struct kernel_hwtstamp_config * cfg)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 
dev_eth_ioctl(struct net_device * dev,struct ifreq * ifr,unsigned int cmd)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  */
dev_get_hwtstamp_phylib(struct net_device * dev,struct kernel_hwtstamp_config * cfg)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 
dev_get_hwtstamp(struct net_device * dev,struct ifreq * ifr)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  */
dev_set_hwtstamp_phylib(struct net_device * dev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)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 
dev_set_hwtstamp(struct net_device * dev,struct ifreq * ifr)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 
generic_hwtstamp_ioctl_lower(struct net_device * dev,int cmd,struct kernel_hwtstamp_config * kernel_cfg)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 
generic_hwtstamp_get_lower(struct net_device * dev,struct kernel_hwtstamp_config * kernel_cfg)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 
generic_hwtstamp_set_lower(struct net_device * dev,struct kernel_hwtstamp_config * kernel_cfg,struct netlink_ext_ack * extack)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 
dev_siocbond(struct net_device * dev,struct ifreq * ifr,unsigned int cmd)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 
dev_siocdevprivate(struct net_device * dev,struct ifreq * ifr,void __user * data,unsigned int cmd)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 
dev_siocwandev(struct net_device * dev,struct if_settings * ifs)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_net_lock()
547  */
dev_ifsioc(struct net * net,struct ifreq * ifr,void __user * data,unsigned int cmd)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 
555 	if (!dev)
556 		return -ENODEV;
557 
558 	ops = dev->netdev_ops;
559 
560 	switch (cmd) {
561 	case SIOCSIFFLAGS:	/* Set interface flags */
562 		return dev_change_flags(dev, ifr->ifr_flags, NULL);
563 
564 	case SIOCSIFMETRIC:	/* Set the metric on the interface
565 				   (currently unused) */
566 		return -EOPNOTSUPP;
567 
568 	case SIOCSIFMTU:	/* Set the MTU of a device */
569 		return dev_set_mtu(dev, ifr->ifr_mtu);
570 
571 	case SIOCSIFHWADDR:
572 		if (dev->addr_len > sizeof(struct sockaddr))
573 			return -EINVAL;
574 		return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
575 
576 	case SIOCSIFHWBROADCAST:
577 		if (ifr->ifr_hwaddr.sa_family != dev->type)
578 			return -EINVAL;
579 		memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
580 		       min(sizeof(ifr->ifr_hwaddr.sa_data_min),
581 			   (size_t)dev->addr_len));
582 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
583 		return 0;
584 
585 	case SIOCSIFMAP:
586 		return dev_setifmap(dev, ifr);
587 
588 	case SIOCADDMULTI:
589 		if (!ops->ndo_set_rx_mode ||
590 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
591 			return -EINVAL;
592 		if (!netif_device_present(dev))
593 			return -ENODEV;
594 		return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
595 
596 	case SIOCDELMULTI:
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 		return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
603 
604 	case SIOCSIFTXQLEN:
605 		if (ifr->ifr_qlen < 0)
606 			return -EINVAL;
607 		return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
608 
609 	case SIOCSIFNAME:
610 		ifr->ifr_newname[IFNAMSIZ-1] = '\0';
611 		return dev_change_name(dev, ifr->ifr_newname);
612 
613 	case SIOCWANDEV:
614 		return dev_siocwandev(dev, &ifr->ifr_settings);
615 
616 	case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
617 		return dev_siocdevprivate(dev, ifr, data, cmd);
618 
619 	case SIOCSHWTSTAMP:
620 		return dev_set_hwtstamp(dev, ifr);
621 
622 	case SIOCGHWTSTAMP:
623 		return dev_get_hwtstamp(dev, ifr);
624 
625 	case SIOCGMIIPHY:
626 	case SIOCGMIIREG:
627 	case SIOCSMIIREG:
628 		return dev_eth_ioctl(dev, ifr, cmd);
629 
630 	case SIOCBONDENSLAVE:
631 	case SIOCBONDRELEASE:
632 	case SIOCBONDSETHWADDR:
633 	case SIOCBONDSLAVEINFOQUERY:
634 	case SIOCBONDINFOQUERY:
635 	case SIOCBONDCHANGEACTIVE:
636 		return dev_siocbond(dev, ifr, cmd);
637 
638 	/* Unknown ioctl */
639 	default:
640 		err = -EINVAL;
641 	}
642 	return err;
643 }
644 
645 /**
646  *	dev_load 	- load a network module
647  *	@net: the applicable net namespace
648  *	@name: name of interface
649  *
650  *	If a network interface is not present and the process has suitable
651  *	privileges this function loads the module. If module loading is not
652  *	available in this kernel then it becomes a nop.
653  */
654 
dev_load(struct net * net,const char * name)655 void dev_load(struct net *net, const char *name)
656 {
657 	struct net_device *dev;
658 	int no_module;
659 
660 	rcu_read_lock();
661 	dev = dev_get_by_name_rcu(net, name);
662 	rcu_read_unlock();
663 
664 	no_module = !dev;
665 	if (no_module && capable(CAP_NET_ADMIN))
666 		no_module = request_module("netdev-%s", name);
667 	if (no_module && capable(CAP_SYS_MODULE))
668 		request_module("%s", name);
669 }
670 EXPORT_SYMBOL(dev_load);
671 
672 /*
673  *	This function handles all "interface"-type I/O control requests. The actual
674  *	'doing' part of this is dev_ifsioc above.
675  */
676 
677 /**
678  *	dev_ioctl	-	network device ioctl
679  *	@net: the applicable net namespace
680  *	@cmd: command to issue
681  *	@ifr: pointer to a struct ifreq in user space
682  *	@data: data exchanged with userspace
683  *	@need_copyout: whether or not copy_to_user() should be called
684  *
685  *	Issue ioctl functions to devices. This is normally called by the
686  *	user space syscall interfaces but can sometimes be useful for
687  *	other purposes. The return value is the return from the syscall if
688  *	positive or a negative errno code on error.
689  */
690 
dev_ioctl(struct net * net,unsigned int cmd,struct ifreq * ifr,void __user * data,bool * need_copyout)691 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
692 	      void __user *data, bool *need_copyout)
693 {
694 	int ret;
695 	char *colon;
696 
697 	if (need_copyout)
698 		*need_copyout = true;
699 	if (cmd == SIOCGIFNAME)
700 		return dev_ifname(net, ifr);
701 
702 	ifr->ifr_name[IFNAMSIZ-1] = 0;
703 
704 	colon = strchr(ifr->ifr_name, ':');
705 	if (colon)
706 		*colon = 0;
707 
708 	/*
709 	 *	See which interface the caller is talking about.
710 	 */
711 
712 	switch (cmd) {
713 	case SIOCGIFHWADDR:
714 		dev_load(net, ifr->ifr_name);
715 		ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
716 		if (colon)
717 			*colon = ':';
718 		return ret;
719 	/*
720 	 *	These ioctl calls:
721 	 *	- can be done by all.
722 	 *	- atomic and do not require locking.
723 	 *	- return a value
724 	 */
725 	case SIOCGIFFLAGS:
726 	case SIOCGIFMETRIC:
727 	case SIOCGIFMTU:
728 	case SIOCGIFSLAVE:
729 	case SIOCGIFMAP:
730 	case SIOCGIFINDEX:
731 	case SIOCGIFTXQLEN:
732 		dev_load(net, ifr->ifr_name);
733 		rcu_read_lock();
734 		ret = dev_ifsioc_locked(net, ifr, cmd);
735 		rcu_read_unlock();
736 		if (colon)
737 			*colon = ':';
738 		return ret;
739 
740 	case SIOCETHTOOL:
741 		dev_load(net, ifr->ifr_name);
742 		ret = dev_ethtool(net, ifr, data);
743 		if (colon)
744 			*colon = ':';
745 		return ret;
746 
747 	/*
748 	 *	These ioctl calls:
749 	 *	- require superuser power.
750 	 *	- require strict serialization.
751 	 *	- return a value
752 	 */
753 	case SIOCGMIIPHY:
754 	case SIOCGMIIREG:
755 	case SIOCSIFNAME:
756 		dev_load(net, ifr->ifr_name);
757 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
758 			return -EPERM;
759 
760 		rtnl_net_lock(net);
761 		ret = dev_ifsioc(net, ifr, data, cmd);
762 		rtnl_net_unlock(net);
763 
764 		if (colon)
765 			*colon = ':';
766 		return ret;
767 
768 	/*
769 	 *	These ioctl calls:
770 	 *	- require superuser power.
771 	 *	- require strict serialization.
772 	 *	- do not return a value
773 	 */
774 	case SIOCSIFMAP:
775 	case SIOCSIFTXQLEN:
776 		if (!capable(CAP_NET_ADMIN))
777 			return -EPERM;
778 		fallthrough;
779 	/*
780 	 *	These ioctl calls:
781 	 *	- require local superuser power.
782 	 *	- require strict serialization.
783 	 *	- do not return a value
784 	 */
785 	case SIOCSIFFLAGS:
786 	case SIOCSIFMETRIC:
787 	case SIOCSIFMTU:
788 	case SIOCSIFHWADDR:
789 	case SIOCSIFSLAVE:
790 	case SIOCADDMULTI:
791 	case SIOCDELMULTI:
792 	case SIOCSIFHWBROADCAST:
793 	case SIOCSMIIREG:
794 	case SIOCBONDENSLAVE:
795 	case SIOCBONDRELEASE:
796 	case SIOCBONDSETHWADDR:
797 	case SIOCBONDCHANGEACTIVE:
798 	case SIOCSHWTSTAMP:
799 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
800 			return -EPERM;
801 		fallthrough;
802 	case SIOCBONDSLAVEINFOQUERY:
803 	case SIOCBONDINFOQUERY:
804 		dev_load(net, ifr->ifr_name);
805 
806 		rtnl_net_lock(net);
807 		ret = dev_ifsioc(net, ifr, data, cmd);
808 		rtnl_net_unlock(net);
809 
810 		if (need_copyout)
811 			*need_copyout = false;
812 		return ret;
813 
814 	case SIOCGIFMEM:
815 		/* Get the per device memory space. We can add this but
816 		 * currently do not support it */
817 	case SIOCSIFMEM:
818 		/* Set the per device memory buffer space.
819 		 * Not applicable in our case */
820 	case SIOCSIFLINK:
821 		return -ENOTTY;
822 
823 	/*
824 	 *	Unknown or private ioctl.
825 	 */
826 	default:
827 		if (cmd == SIOCWANDEV ||
828 		    cmd == SIOCGHWTSTAMP ||
829 		    (cmd >= SIOCDEVPRIVATE &&
830 		     cmd <= SIOCDEVPRIVATE + 15)) {
831 			dev_load(net, ifr->ifr_name);
832 
833 			rtnl_net_lock(net);
834 			ret = dev_ifsioc(net, ifr, data, cmd);
835 			rtnl_net_unlock(net);
836 			return ret;
837 		}
838 		return -ENOTTY;
839 	}
840 }
841