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