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