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