xref: /linux-6.15/net/core/dev_ioctl.c (revision c4bffeaa)
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/wireless.h>
9 #include <linux/if_bridge.h>
10 #include <net/dsa.h>
11 #include <net/wext.h>
12 
13 #include "dev.h"
14 
15 /*
16  *	Map an interface index to its name (SIOCGIFNAME)
17  */
18 
19 /*
20  *	We need this ioctl for efficient implementation of the
21  *	if_indextoname() function required by the IPv6 API.  Without
22  *	it, we would have to search all the interfaces to find a
23  *	match.  --pb
24  */
25 
26 static int dev_ifname(struct net *net, struct ifreq *ifr)
27 {
28 	ifr->ifr_name[IFNAMSIZ-1] = 0;
29 	return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
30 }
31 
32 /*
33  *	Perform a SIOCGIFCONF call. This structure will change
34  *	size eventually, and there is nothing I can do about it.
35  *	Thus we will need a 'compatibility mode'.
36  */
37 int dev_ifconf(struct net *net, struct ifconf __user *uifc)
38 {
39 	struct net_device *dev;
40 	void __user *pos;
41 	size_t size;
42 	int len, total = 0, done;
43 
44 	/* both the ifconf and the ifreq structures are slightly different */
45 	if (in_compat_syscall()) {
46 		struct compat_ifconf ifc32;
47 
48 		if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
49 			return -EFAULT;
50 
51 		pos = compat_ptr(ifc32.ifcbuf);
52 		len = ifc32.ifc_len;
53 		size = sizeof(struct compat_ifreq);
54 	} else {
55 		struct ifconf ifc;
56 
57 		if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
58 			return -EFAULT;
59 
60 		pos = ifc.ifc_buf;
61 		len = ifc.ifc_len;
62 		size = sizeof(struct ifreq);
63 	}
64 
65 	/* Loop over the interfaces, and write an info block for each. */
66 	rtnl_lock();
67 	for_each_netdev(net, dev) {
68 		if (!pos)
69 			done = inet_gifconf(dev, NULL, 0, size);
70 		else
71 			done = inet_gifconf(dev, pos + total,
72 					    len - total, size);
73 		if (done < 0) {
74 			rtnl_unlock();
75 			return -EFAULT;
76 		}
77 		total += done;
78 	}
79 	rtnl_unlock();
80 
81 	return put_user(total, &uifc->ifc_len);
82 }
83 
84 static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
85 {
86 	struct ifmap *ifmap = &ifr->ifr_map;
87 
88 	if (in_compat_syscall()) {
89 		struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
90 
91 		cifmap->mem_start = dev->mem_start;
92 		cifmap->mem_end   = dev->mem_end;
93 		cifmap->base_addr = dev->base_addr;
94 		cifmap->irq       = dev->irq;
95 		cifmap->dma       = dev->dma;
96 		cifmap->port      = dev->if_port;
97 
98 		return 0;
99 	}
100 
101 	ifmap->mem_start  = dev->mem_start;
102 	ifmap->mem_end    = dev->mem_end;
103 	ifmap->base_addr  = dev->base_addr;
104 	ifmap->irq        = dev->irq;
105 	ifmap->dma        = dev->dma;
106 	ifmap->port       = dev->if_port;
107 
108 	return 0;
109 }
110 
111 static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
112 {
113 	struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
114 
115 	if (!dev->netdev_ops->ndo_set_config)
116 		return -EOPNOTSUPP;
117 
118 	if (in_compat_syscall()) {
119 		struct ifmap ifmap = {
120 			.mem_start  = cifmap->mem_start,
121 			.mem_end    = cifmap->mem_end,
122 			.base_addr  = cifmap->base_addr,
123 			.irq        = cifmap->irq,
124 			.dma        = cifmap->dma,
125 			.port       = cifmap->port,
126 		};
127 
128 		return dev->netdev_ops->ndo_set_config(dev, &ifmap);
129 	}
130 
131 	return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
132 }
133 
134 /*
135  *	Perform the SIOCxIFxxx calls, inside rcu_read_lock()
136  */
137 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
138 {
139 	int err;
140 	struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
141 
142 	if (!dev)
143 		return -ENODEV;
144 
145 	switch (cmd) {
146 	case SIOCGIFFLAGS:	/* Get interface flags */
147 		ifr->ifr_flags = (short) dev_get_flags(dev);
148 		return 0;
149 
150 	case SIOCGIFMETRIC:	/* Get the metric on the interface
151 				   (currently unused) */
152 		ifr->ifr_metric = 0;
153 		return 0;
154 
155 	case SIOCGIFMTU:	/* Get the MTU of a device */
156 		ifr->ifr_mtu = dev->mtu;
157 		return 0;
158 
159 	case SIOCGIFSLAVE:
160 		err = -EINVAL;
161 		break;
162 
163 	case SIOCGIFMAP:
164 		return dev_getifmap(dev, ifr);
165 
166 	case SIOCGIFINDEX:
167 		ifr->ifr_ifindex = dev->ifindex;
168 		return 0;
169 
170 	case SIOCGIFTXQLEN:
171 		ifr->ifr_qlen = dev->tx_queue_len;
172 		return 0;
173 
174 	default:
175 		/* dev_ioctl() should ensure this case
176 		 * is never reached
177 		 */
178 		WARN_ON(1);
179 		err = -ENOTTY;
180 		break;
181 
182 	}
183 	return err;
184 }
185 
186 static int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
187 {
188 	enum hwtstamp_tx_types tx_type;
189 	enum hwtstamp_rx_filters rx_filter;
190 	int tx_type_valid = 0;
191 	int rx_filter_valid = 0;
192 
193 	if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
194 		return -EINVAL;
195 
196 	tx_type = cfg->tx_type;
197 	rx_filter = cfg->rx_filter;
198 
199 	switch (tx_type) {
200 	case HWTSTAMP_TX_OFF:
201 	case HWTSTAMP_TX_ON:
202 	case HWTSTAMP_TX_ONESTEP_SYNC:
203 	case HWTSTAMP_TX_ONESTEP_P2P:
204 		tx_type_valid = 1;
205 		break;
206 	case __HWTSTAMP_TX_CNT:
207 		/* not a real value */
208 		break;
209 	}
210 
211 	switch (rx_filter) {
212 	case HWTSTAMP_FILTER_NONE:
213 	case HWTSTAMP_FILTER_ALL:
214 	case HWTSTAMP_FILTER_SOME:
215 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
216 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
217 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
218 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
219 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
220 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
221 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
222 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
223 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
224 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
225 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
226 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
227 	case HWTSTAMP_FILTER_NTP_ALL:
228 		rx_filter_valid = 1;
229 		break;
230 	case __HWTSTAMP_FILTER_CNT:
231 		/* not a real value */
232 		break;
233 	}
234 
235 	if (!tx_type_valid || !rx_filter_valid)
236 		return -ERANGE;
237 
238 	return 0;
239 }
240 
241 static int dev_eth_ioctl(struct net_device *dev,
242 			 struct ifreq *ifr, unsigned int cmd)
243 {
244 	const struct net_device_ops *ops = dev->netdev_ops;
245 
246 	if (!ops->ndo_eth_ioctl)
247 		return -EOPNOTSUPP;
248 
249 	if (!netif_device_present(dev))
250 		return -ENODEV;
251 
252 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
253 }
254 
255 static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
256 {
257 	return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
258 }
259 
260 static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
261 {
262 	struct kernel_hwtstamp_config kernel_cfg;
263 	struct hwtstamp_config cfg;
264 	int err;
265 
266 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
267 		return -EFAULT;
268 
269 	hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
270 
271 	err = net_hwtstamp_validate(&kernel_cfg);
272 	if (err)
273 		return err;
274 
275 	err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
276 	if (err != -EOPNOTSUPP)
277 		return err;
278 
279 	return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
280 }
281 
282 static int dev_siocbond(struct net_device *dev,
283 			struct ifreq *ifr, unsigned int cmd)
284 {
285 	const struct net_device_ops *ops = dev->netdev_ops;
286 
287 	if (ops->ndo_siocbond) {
288 		if (netif_device_present(dev))
289 			return ops->ndo_siocbond(dev, ifr, cmd);
290 		else
291 			return -ENODEV;
292 	}
293 
294 	return -EOPNOTSUPP;
295 }
296 
297 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
298 			      void __user *data, unsigned int cmd)
299 {
300 	const struct net_device_ops *ops = dev->netdev_ops;
301 
302 	if (ops->ndo_siocdevprivate) {
303 		if (netif_device_present(dev))
304 			return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
305 		else
306 			return -ENODEV;
307 	}
308 
309 	return -EOPNOTSUPP;
310 }
311 
312 static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
313 {
314 	const struct net_device_ops *ops = dev->netdev_ops;
315 
316 	if (ops->ndo_siocwandev) {
317 		if (netif_device_present(dev))
318 			return ops->ndo_siocwandev(dev, ifs);
319 		else
320 			return -ENODEV;
321 	}
322 
323 	return -EOPNOTSUPP;
324 }
325 
326 /*
327  *	Perform the SIOCxIFxxx calls, inside rtnl_lock()
328  */
329 static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
330 		      unsigned int cmd)
331 {
332 	int err;
333 	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
334 	const struct net_device_ops *ops;
335 	netdevice_tracker dev_tracker;
336 
337 	if (!dev)
338 		return -ENODEV;
339 
340 	ops = dev->netdev_ops;
341 
342 	switch (cmd) {
343 	case SIOCSIFFLAGS:	/* Set interface flags */
344 		return dev_change_flags(dev, ifr->ifr_flags, NULL);
345 
346 	case SIOCSIFMETRIC:	/* Set the metric on the interface
347 				   (currently unused) */
348 		return -EOPNOTSUPP;
349 
350 	case SIOCSIFMTU:	/* Set the MTU of a device */
351 		return dev_set_mtu(dev, ifr->ifr_mtu);
352 
353 	case SIOCSIFHWADDR:
354 		if (dev->addr_len > sizeof(struct sockaddr))
355 			return -EINVAL;
356 		return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
357 
358 	case SIOCSIFHWBROADCAST:
359 		if (ifr->ifr_hwaddr.sa_family != dev->type)
360 			return -EINVAL;
361 		memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
362 		       min(sizeof(ifr->ifr_hwaddr.sa_data_min),
363 			   (size_t)dev->addr_len));
364 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
365 		return 0;
366 
367 	case SIOCSIFMAP:
368 		return dev_setifmap(dev, ifr);
369 
370 	case SIOCADDMULTI:
371 		if (!ops->ndo_set_rx_mode ||
372 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
373 			return -EINVAL;
374 		if (!netif_device_present(dev))
375 			return -ENODEV;
376 		return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
377 
378 	case SIOCDELMULTI:
379 		if (!ops->ndo_set_rx_mode ||
380 		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
381 			return -EINVAL;
382 		if (!netif_device_present(dev))
383 			return -ENODEV;
384 		return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
385 
386 	case SIOCSIFTXQLEN:
387 		if (ifr->ifr_qlen < 0)
388 			return -EINVAL;
389 		return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
390 
391 	case SIOCSIFNAME:
392 		ifr->ifr_newname[IFNAMSIZ-1] = '\0';
393 		return dev_change_name(dev, ifr->ifr_newname);
394 
395 	case SIOCWANDEV:
396 		return dev_siocwandev(dev, &ifr->ifr_settings);
397 
398 	case SIOCBRADDIF:
399 	case SIOCBRDELIF:
400 		if (!netif_device_present(dev))
401 			return -ENODEV;
402 		if (!netif_is_bridge_master(dev))
403 			return -EOPNOTSUPP;
404 		netdev_hold(dev, &dev_tracker, GFP_KERNEL);
405 		rtnl_unlock();
406 		err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
407 		netdev_put(dev, &dev_tracker);
408 		rtnl_lock();
409 		return err;
410 
411 	case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
412 		return dev_siocdevprivate(dev, ifr, data, cmd);
413 
414 	case SIOCSHWTSTAMP:
415 		return dev_set_hwtstamp(dev, ifr);
416 
417 	case SIOCGHWTSTAMP:
418 		return dev_get_hwtstamp(dev, ifr);
419 
420 	case SIOCGMIIPHY:
421 	case SIOCGMIIREG:
422 	case SIOCSMIIREG:
423 		return dev_eth_ioctl(dev, ifr, cmd);
424 
425 	case SIOCBONDENSLAVE:
426 	case SIOCBONDRELEASE:
427 	case SIOCBONDSETHWADDR:
428 	case SIOCBONDSLAVEINFOQUERY:
429 	case SIOCBONDINFOQUERY:
430 	case SIOCBONDCHANGEACTIVE:
431 		return dev_siocbond(dev, ifr, cmd);
432 
433 	/* Unknown ioctl */
434 	default:
435 		err = -EINVAL;
436 	}
437 	return err;
438 }
439 
440 /**
441  *	dev_load 	- load a network module
442  *	@net: the applicable net namespace
443  *	@name: name of interface
444  *
445  *	If a network interface is not present and the process has suitable
446  *	privileges this function loads the module. If module loading is not
447  *	available in this kernel then it becomes a nop.
448  */
449 
450 void dev_load(struct net *net, const char *name)
451 {
452 	struct net_device *dev;
453 	int no_module;
454 
455 	rcu_read_lock();
456 	dev = dev_get_by_name_rcu(net, name);
457 	rcu_read_unlock();
458 
459 	no_module = !dev;
460 	if (no_module && capable(CAP_NET_ADMIN))
461 		no_module = request_module("netdev-%s", name);
462 	if (no_module && capable(CAP_SYS_MODULE))
463 		request_module("%s", name);
464 }
465 EXPORT_SYMBOL(dev_load);
466 
467 /*
468  *	This function handles all "interface"-type I/O control requests. The actual
469  *	'doing' part of this is dev_ifsioc above.
470  */
471 
472 /**
473  *	dev_ioctl	-	network device ioctl
474  *	@net: the applicable net namespace
475  *	@cmd: command to issue
476  *	@ifr: pointer to a struct ifreq in user space
477  *	@data: data exchanged with userspace
478  *	@need_copyout: whether or not copy_to_user() should be called
479  *
480  *	Issue ioctl functions to devices. This is normally called by the
481  *	user space syscall interfaces but can sometimes be useful for
482  *	other purposes. The return value is the return from the syscall if
483  *	positive or a negative errno code on error.
484  */
485 
486 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
487 	      void __user *data, bool *need_copyout)
488 {
489 	int ret;
490 	char *colon;
491 
492 	if (need_copyout)
493 		*need_copyout = true;
494 	if (cmd == SIOCGIFNAME)
495 		return dev_ifname(net, ifr);
496 
497 	ifr->ifr_name[IFNAMSIZ-1] = 0;
498 
499 	colon = strchr(ifr->ifr_name, ':');
500 	if (colon)
501 		*colon = 0;
502 
503 	/*
504 	 *	See which interface the caller is talking about.
505 	 */
506 
507 	switch (cmd) {
508 	case SIOCGIFHWADDR:
509 		dev_load(net, ifr->ifr_name);
510 		ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
511 		if (colon)
512 			*colon = ':';
513 		return ret;
514 	/*
515 	 *	These ioctl calls:
516 	 *	- can be done by all.
517 	 *	- atomic and do not require locking.
518 	 *	- return a value
519 	 */
520 	case SIOCGIFFLAGS:
521 	case SIOCGIFMETRIC:
522 	case SIOCGIFMTU:
523 	case SIOCGIFSLAVE:
524 	case SIOCGIFMAP:
525 	case SIOCGIFINDEX:
526 	case SIOCGIFTXQLEN:
527 		dev_load(net, ifr->ifr_name);
528 		rcu_read_lock();
529 		ret = dev_ifsioc_locked(net, ifr, cmd);
530 		rcu_read_unlock();
531 		if (colon)
532 			*colon = ':';
533 		return ret;
534 
535 	case SIOCETHTOOL:
536 		dev_load(net, ifr->ifr_name);
537 		ret = dev_ethtool(net, ifr, data);
538 		if (colon)
539 			*colon = ':';
540 		return ret;
541 
542 	/*
543 	 *	These ioctl calls:
544 	 *	- require superuser power.
545 	 *	- require strict serialization.
546 	 *	- return a value
547 	 */
548 	case SIOCGMIIPHY:
549 	case SIOCGMIIREG:
550 	case SIOCSIFNAME:
551 		dev_load(net, ifr->ifr_name);
552 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
553 			return -EPERM;
554 		rtnl_lock();
555 		ret = dev_ifsioc(net, ifr, data, cmd);
556 		rtnl_unlock();
557 		if (colon)
558 			*colon = ':';
559 		return ret;
560 
561 	/*
562 	 *	These ioctl calls:
563 	 *	- require superuser power.
564 	 *	- require strict serialization.
565 	 *	- do not return a value
566 	 */
567 	case SIOCSIFMAP:
568 	case SIOCSIFTXQLEN:
569 		if (!capable(CAP_NET_ADMIN))
570 			return -EPERM;
571 		fallthrough;
572 	/*
573 	 *	These ioctl calls:
574 	 *	- require local superuser power.
575 	 *	- require strict serialization.
576 	 *	- do not return a value
577 	 */
578 	case SIOCSIFFLAGS:
579 	case SIOCSIFMETRIC:
580 	case SIOCSIFMTU:
581 	case SIOCSIFHWADDR:
582 	case SIOCSIFSLAVE:
583 	case SIOCADDMULTI:
584 	case SIOCDELMULTI:
585 	case SIOCSIFHWBROADCAST:
586 	case SIOCSMIIREG:
587 	case SIOCBONDENSLAVE:
588 	case SIOCBONDRELEASE:
589 	case SIOCBONDSETHWADDR:
590 	case SIOCBONDCHANGEACTIVE:
591 	case SIOCBRADDIF:
592 	case SIOCBRDELIF:
593 	case SIOCSHWTSTAMP:
594 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
595 			return -EPERM;
596 		fallthrough;
597 	case SIOCBONDSLAVEINFOQUERY:
598 	case SIOCBONDINFOQUERY:
599 		dev_load(net, ifr->ifr_name);
600 		rtnl_lock();
601 		ret = dev_ifsioc(net, ifr, data, cmd);
602 		rtnl_unlock();
603 		if (need_copyout)
604 			*need_copyout = false;
605 		return ret;
606 
607 	case SIOCGIFMEM:
608 		/* Get the per device memory space. We can add this but
609 		 * currently do not support it */
610 	case SIOCSIFMEM:
611 		/* Set the per device memory buffer space.
612 		 * Not applicable in our case */
613 	case SIOCSIFLINK:
614 		return -ENOTTY;
615 
616 	/*
617 	 *	Unknown or private ioctl.
618 	 */
619 	default:
620 		if (cmd == SIOCWANDEV ||
621 		    cmd == SIOCGHWTSTAMP ||
622 		    (cmd >= SIOCDEVPRIVATE &&
623 		     cmd <= SIOCDEVPRIVATE + 15)) {
624 			dev_load(net, ifr->ifr_name);
625 			rtnl_lock();
626 			ret = dev_ifsioc(net, ifr, data, cmd);
627 			rtnl_unlock();
628 			return ret;
629 		}
630 		return -ENOTTY;
631 	}
632 }
633