blob: 3730945ee29489ace6424967812f062c17ac3060 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Cong Wang96b45cb2013-02-15 22:20:46 +00002#include <linux/kmod.h>
3#include <linux/netdevice.h>
Arnd Bergmannb0e99d02021-07-22 16:29:01 +02004#include <linux/inetdevice.h>
Cong Wang96b45cb2013-02-15 22:20:46 +00005#include <linux/etherdevice.h>
6#include <linux/rtnetlink.h>
7#include <linux/net_tstamp.h>
8#include <linux/wireless.h>
Arnd Bergmannad2f99a2021-07-27 15:45:16 +02009#include <linux/if_bridge.h>
Vladimir Oltean5a178182023-04-06 14:42:46 +030010#include <net/dsa_stubs.h>
Cong Wang96b45cb2013-02-15 22:20:46 +000011#include <net/wext.h>
12
Jakub Kicinski6264f582022-04-06 14:37:54 -070013#include "dev.h"
14
Cong Wang96b45cb2013-02-15 22:20:46 +000015/*
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
Al Viro44c02a22017-10-05 12:59:44 -040026static int dev_ifname(struct net *net, struct ifreq *ifr)
Cong Wang96b45cb2013-02-15 22:20:46 +000027{
Al Viro44c02a22017-10-05 12:59:44 -040028 ifr->ifr_name[IFNAMSIZ-1] = 0;
29 return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
Cong Wang96b45cb2013-02-15 22:20:46 +000030}
31
Cong Wang96b45cb2013-02-15 22:20:46 +000032/*
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 */
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020037int dev_ifconf(struct net *net, struct ifconf __user *uifc)
Cong Wang96b45cb2013-02-15 22:20:46 +000038{
Cong Wang96b45cb2013-02-15 22:20:46 +000039 struct net_device *dev;
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020040 void __user *pos;
41 size_t size;
42 int len, total = 0, done;
Cong Wang96b45cb2013-02-15 22:20:46 +000043
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020044 /* both the ifconf and the ifreq structures are slightly different */
45 if (in_compat_syscall()) {
46 struct compat_ifconf ifc32;
Cong Wang96b45cb2013-02-15 22:20:46 +000047
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020048 if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
49 return -EFAULT;
Cong Wang96b45cb2013-02-15 22:20:46 +000050
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020051 pos = compat_ptr(ifc32.ifcbuf);
52 len = ifc32.ifc_len;
53 size = sizeof(struct compat_ifreq);
54 } else {
55 struct ifconf ifc;
Cong Wang96b45cb2013-02-15 22:20:46 +000056
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020057 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();
Cong Wang96b45cb2013-02-15 22:20:46 +000067 for_each_netdev(net, dev) {
Arnd Bergmannb0e99d02021-07-22 16:29:01 +020068 if (!pos)
69 done = inet_gifconf(dev, NULL, 0, size);
70 else
71 done = inet_gifconf(dev, pos + total,
72 len - total, size);
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020073 if (done < 0) {
74 rtnl_unlock();
Arnd Bergmannb0e99d02021-07-22 16:29:01 +020075 return -EFAULT;
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020076 }
Arnd Bergmannb0e99d02021-07-22 16:29:01 +020077 total += done;
Cong Wang96b45cb2013-02-15 22:20:46 +000078 }
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020079 rtnl_unlock();
Cong Wang96b45cb2013-02-15 22:20:46 +000080
Arnd Bergmann876f0bf2021-07-22 16:29:02 +020081 return put_user(total, &uifc->ifc_len);
Cong Wang96b45cb2013-02-15 22:20:46 +000082}
83
Arnd Bergmann709566d2021-07-22 16:29:00 +020084static 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
111static 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
Cong Wang96b45cb2013-02-15 22:20:46 +0000134/*
135 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
136 */
137static 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
Cong Wang96b45cb2013-02-15 22:20:46 +0000159 case SIOCGIFSLAVE:
160 err = -EINVAL;
161 break;
162
163 case SIOCGIFMAP:
Arnd Bergmann709566d2021-07-22 16:29:00 +0200164 return dev_getifmap(dev, ifr);
Cong Wang96b45cb2013-02-15 22:20:46 +0000165
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
Vladimir Olteanc4bffea2023-04-02 15:37:53 +0300186static int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
Cong Wang96b45cb2013-02-15 22:20:46 +0000187{
Cong Wang96b45cb2013-02-15 22:20:46 +0000188 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
Vladimir Olteand5d5fd82023-04-02 15:37:52 +0300193 if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
Cong Wang96b45cb2013-02-15 22:20:46 +0000194 return -EINVAL;
195
Vladimir Olteand5d5fd82023-04-02 15:37:52 +0300196 tx_type = cfg->tx_type;
197 rx_filter = cfg->rx_filter;
Cong Wang96b45cb2013-02-15 22:20:46 +0000198
199 switch (tx_type) {
200 case HWTSTAMP_TX_OFF:
201 case HWTSTAMP_TX_ON:
202 case HWTSTAMP_TX_ONESTEP_SYNC:
Richard Cochranb6fd7b92019-12-25 18:16:19 -0800203 case HWTSTAMP_TX_ONESTEP_P2P:
Cong Wang96b45cb2013-02-15 22:20:46 +0000204 tx_type_valid = 1;
205 break;
Michal Kubecekf76510b2020-03-28 00:01:53 +0100206 case __HWTSTAMP_TX_CNT:
207 /* not a real value */
208 break;
Cong Wang96b45cb2013-02-15 22:20:46 +0000209 }
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:
Miroslav Lichvarb8210a9e2017-05-19 17:52:35 +0200227 case HWTSTAMP_FILTER_NTP_ALL:
Miroslav Lichvare3412572017-05-19 17:52:36 +0200228 rx_filter_valid = 1;
Miroslav Lichvarb8210a9e2017-05-19 17:52:35 +0200229 break;
Michal Kubecekf76510b2020-03-28 00:01:53 +0100230 case __HWTSTAMP_FILTER_CNT:
231 /* not a real value */
232 break;
Cong Wang96b45cb2013-02-15 22:20:46 +0000233 }
234
235 if (!tx_type_valid || !rx_filter_valid)
236 return -ERANGE;
237
238 return 0;
239}
240
Arnd Bergmanna7605372021-07-27 15:45:13 +0200241static int dev_eth_ioctl(struct net_device *dev,
242 struct ifreq *ifr, unsigned int cmd)
Florian Fainelliaad74d82020-07-19 20:49:51 -0700243{
244 const struct net_device_ops *ops = dev->netdev_ops;
Vladimir Oltean4ee58e12023-04-02 15:37:51 +0300245
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
255static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
256{
257 return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
258}
259
260static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
261{
Vladimir Olteanc4bffea2023-04-02 15:37:53 +0300262 struct kernel_hwtstamp_config kernel_cfg;
Vladimir Oltean88c0a6b2023-04-02 15:37:55 +0300263 struct netlink_ext_ack extack = {};
Vladimir Olteand5d5fd82023-04-02 15:37:52 +0300264 struct hwtstamp_config cfg;
Colin Ian King1ca47432020-11-02 12:16:15 +0000265 int err;
Florian Fainelliaad74d82020-07-19 20:49:51 -0700266
Vladimir Olteand5d5fd82023-04-02 15:37:52 +0300267 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
268 return -EFAULT;
269
Vladimir Olteanc4bffea2023-04-02 15:37:53 +0300270 hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
271
272 err = net_hwtstamp_validate(&kernel_cfg);
Vladimir Oltean4ee58e12023-04-02 15:37:51 +0300273 if (err)
274 return err;
275
Vladimir Oltean5a178182023-04-06 14:42:46 +0300276 err = dsa_master_hwtstamp_validate(dev, &kernel_cfg, &extack);
Vladimir Oltean88c0a6b2023-04-02 15:37:55 +0300277 if (err) {
278 if (extack._msg)
279 netdev_err(dev, "%s\n", extack._msg);
Florian Fainelli3369afb2020-07-19 20:49:53 -0700280 return err;
Vladimir Oltean88c0a6b2023-04-02 15:37:55 +0300281 }
Florian Fainelli3369afb2020-07-19 20:49:53 -0700282
Vladimir Oltean4ee58e12023-04-02 15:37:51 +0300283 return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
Florian Fainelliaad74d82020-07-19 20:49:51 -0700284}
285
Arnd Bergmann3d9d00bd2021-07-27 15:45:17 +0200286static int dev_siocbond(struct net_device *dev,
Arnd Bergmanna7605372021-07-27 15:45:13 +0200287 struct ifreq *ifr, unsigned int cmd)
288{
289 const struct net_device_ops *ops = dev->netdev_ops;
290
Arnd Bergmann3d9d00bd2021-07-27 15:45:17 +0200291 if (ops->ndo_siocbond) {
Arnd Bergmanna7605372021-07-27 15:45:13 +0200292 if (netif_device_present(dev))
Arnd Bergmann3d9d00bd2021-07-27 15:45:17 +0200293 return ops->ndo_siocbond(dev, ifr, cmd);
Arnd Bergmanna7605372021-07-27 15:45:13 +0200294 else
295 return -ENODEV;
296 }
297
298 return -EOPNOTSUPP;
299}
300
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200301static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
302 void __user *data, unsigned int cmd)
Arnd Bergmannb9067f52021-07-27 15:44:47 +0200303{
304 const struct net_device_ops *ops = dev->netdev_ops;
Arnd Bergmannb9067f52021-07-27 15:44:47 +0200305
306 if (ops->ndo_siocdevprivate) {
307 if (netif_device_present(dev))
308 return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
309 else
310 return -ENODEV;
311 }
312
Arnd Bergmann88fc0232021-07-27 15:45:15 +0200313 return -EOPNOTSUPP;
Arnd Bergmannb9067f52021-07-27 15:44:47 +0200314}
315
Arnd Bergmannad7eab2a2021-07-27 15:45:14 +0200316static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
317{
318 const struct net_device_ops *ops = dev->netdev_ops;
319
320 if (ops->ndo_siocwandev) {
321 if (netif_device_present(dev))
322 return ops->ndo_siocwandev(dev, ifs);
323 else
324 return -ENODEV;
325 }
326
327 return -EOPNOTSUPP;
328}
329
Cong Wang96b45cb2013-02-15 22:20:46 +0000330/*
331 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
332 */
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200333static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
334 unsigned int cmd)
Cong Wang96b45cb2013-02-15 22:20:46 +0000335{
336 int err;
337 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
338 const struct net_device_ops *ops;
Eric Dumazet14ed0292021-12-04 20:22:01 -0800339 netdevice_tracker dev_tracker;
Cong Wang96b45cb2013-02-15 22:20:46 +0000340
341 if (!dev)
342 return -ENODEV;
343
344 ops = dev->netdev_ops;
345
346 switch (cmd) {
347 case SIOCSIFFLAGS: /* Set interface flags */
Petr Machata567c5e12018-12-06 17:05:42 +0000348 return dev_change_flags(dev, ifr->ifr_flags, NULL);
Cong Wang96b45cb2013-02-15 22:20:46 +0000349
350 case SIOCSIFMETRIC: /* Set the metric on the interface
351 (currently unused) */
352 return -EOPNOTSUPP;
353
354 case SIOCSIFMTU: /* Set the MTU of a device */
355 return dev_set_mtu(dev, ifr->ifr_mtu);
356
357 case SIOCSIFHWADDR:
WANG Cong0254e0c2017-07-26 15:22:06 -0700358 if (dev->addr_len > sizeof(struct sockaddr))
359 return -EINVAL;
Cong Wang3b23a322021-02-11 11:34:10 -0800360 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
Cong Wang96b45cb2013-02-15 22:20:46 +0000361
362 case SIOCSIFHWBROADCAST:
363 if (ifr->ifr_hwaddr.sa_family != dev->type)
364 return -EINVAL;
365 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
Kees Cookb5f0de62022-10-18 02:56:03 -0700366 min(sizeof(ifr->ifr_hwaddr.sa_data_min),
Fabian Frederick54aeba72014-11-17 22:23:17 +0100367 (size_t)dev->addr_len));
Cong Wang96b45cb2013-02-15 22:20:46 +0000368 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
369 return 0;
370
371 case SIOCSIFMAP:
Arnd Bergmann709566d2021-07-22 16:29:00 +0200372 return dev_setifmap(dev, ifr);
Cong Wang96b45cb2013-02-15 22:20:46 +0000373
374 case SIOCADDMULTI:
375 if (!ops->ndo_set_rx_mode ||
376 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
377 return -EINVAL;
378 if (!netif_device_present(dev))
379 return -ENODEV;
380 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
381
382 case SIOCDELMULTI:
383 if (!ops->ndo_set_rx_mode ||
384 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
385 return -EINVAL;
386 if (!netif_device_present(dev))
387 return -ENODEV;
388 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
389
390 case SIOCSIFTXQLEN:
391 if (ifr->ifr_qlen < 0)
392 return -EINVAL;
Tariq Toukan8dd30202018-07-24 13:53:00 +0300393 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
Cong Wang96b45cb2013-02-15 22:20:46 +0000394
395 case SIOCSIFNAME:
396 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
397 return dev_change_name(dev, ifr->ifr_newname);
398
Arnd Bergmannad7eab2a2021-07-27 15:45:14 +0200399 case SIOCWANDEV:
400 return dev_siocwandev(dev, &ifr->ifr_settings);
401
Arnd Bergmannad2f99a2021-07-27 15:45:16 +0200402 case SIOCBRADDIF:
403 case SIOCBRDELIF:
404 if (!netif_device_present(dev))
405 return -ENODEV;
Nikolay Aleksandrov9384eac2021-08-05 11:29:03 +0300406 if (!netif_is_bridge_master(dev))
407 return -EOPNOTSUPP;
Jakub Kicinskid62607c2022-06-07 21:39:55 -0700408 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
Nikolay Aleksandrov893b1952021-08-05 11:29:01 +0300409 rtnl_unlock();
410 err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
Jakub Kicinskid62607c2022-06-07 21:39:55 -0700411 netdev_put(dev, &dev_tracker);
Nikolay Aleksandrov893b1952021-08-05 11:29:01 +0300412 rtnl_lock();
413 return err;
Arnd Bergmannad2f99a2021-07-27 15:45:16 +0200414
Vladimir Oltean00d521b32023-04-02 15:37:49 +0300415 case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
416 return dev_siocdevprivate(dev, ifr, data, cmd);
417
Cong Wang96b45cb2013-02-15 22:20:46 +0000418 case SIOCSHWTSTAMP:
Vladimir Oltean4ee58e12023-04-02 15:37:51 +0300419 return dev_set_hwtstamp(dev, ifr);
Cong Wang96b45cb2013-02-15 22:20:46 +0000420
Vladimir Oltean00d521b32023-04-02 15:37:49 +0300421 case SIOCGHWTSTAMP:
Vladimir Oltean4ee58e12023-04-02 15:37:51 +0300422 return dev_get_hwtstamp(dev, ifr);
423
Vladimir Oltean00d521b32023-04-02 15:37:49 +0300424 case SIOCGMIIPHY:
425 case SIOCGMIIREG:
426 case SIOCSMIIREG:
427 return dev_eth_ioctl(dev, ifr, cmd);
428
429 case SIOCBONDENSLAVE:
430 case SIOCBONDRELEASE:
431 case SIOCBONDSETHWADDR:
432 case SIOCBONDSLAVEINFOQUERY:
433 case SIOCBONDINFOQUERY:
434 case SIOCBONDCHANGEACTIVE:
435 return dev_siocbond(dev, ifr, cmd);
436
437 /* Unknown ioctl */
Cong Wang96b45cb2013-02-15 22:20:46 +0000438 default:
Vladimir Oltean00d521b32023-04-02 15:37:49 +0300439 err = -EINVAL;
Cong Wang96b45cb2013-02-15 22:20:46 +0000440 }
441 return err;
442}
443
444/**
445 * dev_load - load a network module
446 * @net: the applicable net namespace
447 * @name: name of interface
448 *
449 * If a network interface is not present and the process has suitable
450 * privileges this function loads the module. If module loading is not
451 * available in this kernel then it becomes a nop.
452 */
453
454void dev_load(struct net *net, const char *name)
455{
456 struct net_device *dev;
457 int no_module;
458
459 rcu_read_lock();
460 dev = dev_get_by_name_rcu(net, name);
461 rcu_read_unlock();
462
463 no_module = !dev;
464 if (no_module && capable(CAP_NET_ADMIN))
465 no_module = request_module("netdev-%s", name);
Daniel Borkmanne0208362014-09-02 23:30:05 +0200466 if (no_module && capable(CAP_SYS_MODULE))
467 request_module("%s", name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000468}
469EXPORT_SYMBOL(dev_load);
470
471/*
472 * This function handles all "interface"-type I/O control requests. The actual
473 * 'doing' part of this is dev_ifsioc above.
474 */
475
476/**
477 * dev_ioctl - network device ioctl
478 * @net: the applicable net namespace
479 * @cmd: command to issue
Bart Van Asscheb3c0fd62019-03-25 09:17:20 -0700480 * @ifr: pointer to a struct ifreq in user space
Heiner Kallweite5b42482023-03-24 23:11:49 +0100481 * @data: data exchanged with userspace
Bart Van Asscheb3c0fd62019-03-25 09:17:20 -0700482 * @need_copyout: whether or not copy_to_user() should be called
Cong Wang96b45cb2013-02-15 22:20:46 +0000483 *
484 * Issue ioctl functions to devices. This is normally called by the
485 * user space syscall interfaces but can sometimes be useful for
486 * other purposes. The return value is the return from the syscall if
487 * positive or a negative errno code on error.
488 */
489
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200490int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
491 void __user *data, bool *need_copyout)
Cong Wang96b45cb2013-02-15 22:20:46 +0000492{
Cong Wang96b45cb2013-02-15 22:20:46 +0000493 int ret;
494 char *colon;
495
Al Viro44c02a22017-10-05 12:59:44 -0400496 if (need_copyout)
497 *need_copyout = true;
Cong Wang96b45cb2013-02-15 22:20:46 +0000498 if (cmd == SIOCGIFNAME)
Al Viro44c02a22017-10-05 12:59:44 -0400499 return dev_ifname(net, ifr);
Cong Wang96b45cb2013-02-15 22:20:46 +0000500
Al Viro44c02a22017-10-05 12:59:44 -0400501 ifr->ifr_name[IFNAMSIZ-1] = 0;
Cong Wang96b45cb2013-02-15 22:20:46 +0000502
Al Viro44c02a22017-10-05 12:59:44 -0400503 colon = strchr(ifr->ifr_name, ':');
Cong Wang96b45cb2013-02-15 22:20:46 +0000504 if (colon)
505 *colon = 0;
506
507 /*
508 * See which interface the caller is talking about.
509 */
510
511 switch (cmd) {
Cong Wang3b23a322021-02-11 11:34:10 -0800512 case SIOCGIFHWADDR:
513 dev_load(net, ifr->ifr_name);
514 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
515 if (colon)
516 *colon = ':';
517 return ret;
Cong Wang96b45cb2013-02-15 22:20:46 +0000518 /*
519 * These ioctl calls:
520 * - can be done by all.
521 * - atomic and do not require locking.
522 * - return a value
523 */
524 case SIOCGIFFLAGS:
525 case SIOCGIFMETRIC:
526 case SIOCGIFMTU:
Cong Wang96b45cb2013-02-15 22:20:46 +0000527 case SIOCGIFSLAVE:
528 case SIOCGIFMAP:
529 case SIOCGIFINDEX:
530 case SIOCGIFTXQLEN:
Paul Mooreb51f26b2018-03-06 17:27:44 -0500531 dev_load(net, ifr->ifr_name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000532 rcu_read_lock();
Al Viro44c02a22017-10-05 12:59:44 -0400533 ret = dev_ifsioc_locked(net, ifr, cmd);
Cong Wang96b45cb2013-02-15 22:20:46 +0000534 rcu_read_unlock();
Al Viro44c02a22017-10-05 12:59:44 -0400535 if (colon)
536 *colon = ':';
Cong Wang96b45cb2013-02-15 22:20:46 +0000537 return ret;
538
539 case SIOCETHTOOL:
Paul Mooreb51f26b2018-03-06 17:27:44 -0500540 dev_load(net, ifr->ifr_name);
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200541 ret = dev_ethtool(net, ifr, data);
Al Viro44c02a22017-10-05 12:59:44 -0400542 if (colon)
543 *colon = ':';
Cong Wang96b45cb2013-02-15 22:20:46 +0000544 return ret;
545
546 /*
547 * These ioctl calls:
548 * - require superuser power.
549 * - require strict serialization.
550 * - return a value
551 */
552 case SIOCGMIIPHY:
553 case SIOCGMIIREG:
554 case SIOCSIFNAME:
Paul Mooreb51f26b2018-03-06 17:27:44 -0500555 dev_load(net, ifr->ifr_name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000556 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
557 return -EPERM;
Cong Wang96b45cb2013-02-15 22:20:46 +0000558 rtnl_lock();
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200559 ret = dev_ifsioc(net, ifr, data, cmd);
Cong Wang96b45cb2013-02-15 22:20:46 +0000560 rtnl_unlock();
Al Viro44c02a22017-10-05 12:59:44 -0400561 if (colon)
562 *colon = ':';
Cong Wang96b45cb2013-02-15 22:20:46 +0000563 return ret;
564
565 /*
566 * These ioctl calls:
567 * - require superuser power.
568 * - require strict serialization.
569 * - do not return a value
570 */
571 case SIOCSIFMAP:
572 case SIOCSIFTXQLEN:
573 if (!capable(CAP_NET_ADMIN))
574 return -EPERM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500575 fallthrough;
Cong Wang96b45cb2013-02-15 22:20:46 +0000576 /*
577 * These ioctl calls:
578 * - require local superuser power.
579 * - require strict serialization.
580 * - do not return a value
581 */
582 case SIOCSIFFLAGS:
583 case SIOCSIFMETRIC:
584 case SIOCSIFMTU:
585 case SIOCSIFHWADDR:
586 case SIOCSIFSLAVE:
587 case SIOCADDMULTI:
588 case SIOCDELMULTI:
589 case SIOCSIFHWBROADCAST:
590 case SIOCSMIIREG:
591 case SIOCBONDENSLAVE:
592 case SIOCBONDRELEASE:
593 case SIOCBONDSETHWADDR:
594 case SIOCBONDCHANGEACTIVE:
595 case SIOCBRADDIF:
596 case SIOCBRDELIF:
597 case SIOCSHWTSTAMP:
598 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
599 return -EPERM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500600 fallthrough;
Cong Wang96b45cb2013-02-15 22:20:46 +0000601 case SIOCBONDSLAVEINFOQUERY:
602 case SIOCBONDINFOQUERY:
Paul Mooreb51f26b2018-03-06 17:27:44 -0500603 dev_load(net, ifr->ifr_name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000604 rtnl_lock();
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200605 ret = dev_ifsioc(net, ifr, data, cmd);
Cong Wang96b45cb2013-02-15 22:20:46 +0000606 rtnl_unlock();
Al Viro44c02a22017-10-05 12:59:44 -0400607 if (need_copyout)
608 *need_copyout = false;
Cong Wang96b45cb2013-02-15 22:20:46 +0000609 return ret;
610
611 case SIOCGIFMEM:
612 /* Get the per device memory space. We can add this but
613 * currently do not support it */
614 case SIOCSIFMEM:
615 /* Set the per device memory buffer space.
616 * Not applicable in our case */
617 case SIOCSIFLINK:
618 return -ENOTTY;
619
620 /*
621 * Unknown or private ioctl.
622 */
623 default:
624 if (cmd == SIOCWANDEV ||
Ben Hutchingsfd468c72013-11-14 01:19:29 +0000625 cmd == SIOCGHWTSTAMP ||
Cong Wang96b45cb2013-02-15 22:20:46 +0000626 (cmd >= SIOCDEVPRIVATE &&
627 cmd <= SIOCDEVPRIVATE + 15)) {
Paul Mooreb51f26b2018-03-06 17:27:44 -0500628 dev_load(net, ifr->ifr_name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000629 rtnl_lock();
Arnd Bergmanna554bf92021-07-27 15:45:12 +0200630 ret = dev_ifsioc(net, ifr, data, cmd);
Cong Wang96b45cb2013-02-15 22:20:46 +0000631 rtnl_unlock();
Cong Wang96b45cb2013-02-15 22:20:46 +0000632 return ret;
633 }
Cong Wang96b45cb2013-02-15 22:20:46 +0000634 return -ENOTTY;
635 }
636}