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