File Index
Files and directories
Sign in
FILE

dev_ioctl.c

File preview with syntax highlighting.

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