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>
13 * Map an interface index to its name (SIOCGIFNAME)
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
23 static int dev_ifname(struct net *net, struct ifreq *ifr)
25 ifr->ifr_name[IFNAMSIZ-1] = 0;
26 return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
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'.
35 int dev_ifconf(struct net *net, struct ifconf *ifc, int size)
37 struct net_device *dev;
44 * Fetch the caller's info block.
51 * Loop over the interfaces, and write an info block for each.
55 for_each_netdev(net, dev) {
58 done = inet_gifconf(dev, NULL, 0, size);
60 done = inet_gifconf(dev, pos + total,
68 * All done. Write the updated control block back to the caller.
73 * Both BSD and Solaris return 0 here, so we do too.
78 static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
80 struct ifmap *ifmap = &ifr->ifr_map;
82 if (in_compat_syscall()) {
83 struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
85 cifmap->mem_start = dev->mem_start;
86 cifmap->mem_end = dev->mem_end;
87 cifmap->base_addr = dev->base_addr;
88 cifmap->irq = dev->irq;
89 cifmap->dma = dev->dma;
90 cifmap->port = dev->if_port;
95 ifmap->mem_start = dev->mem_start;
96 ifmap->mem_end = dev->mem_end;
97 ifmap->base_addr = dev->base_addr;
98 ifmap->irq = dev->irq;
99 ifmap->dma = dev->dma;
100 ifmap->port = dev->if_port;
105 static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
107 struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
109 if (!dev->netdev_ops->ndo_set_config)
112 if (in_compat_syscall()) {
113 struct ifmap ifmap = {
114 .mem_start = cifmap->mem_start,
115 .mem_end = cifmap->mem_end,
116 .base_addr = cifmap->base_addr,
119 .port = cifmap->port,
122 return dev->netdev_ops->ndo_set_config(dev, &ifmap);
125 return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
129 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
131 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
134 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
140 case SIOCGIFFLAGS: /* Get interface flags */
141 ifr->ifr_flags = (short) dev_get_flags(dev);
144 case SIOCGIFMETRIC: /* Get the metric on the interface
145 (currently unused) */
149 case SIOCGIFMTU: /* Get the MTU of a device */
150 ifr->ifr_mtu = dev->mtu;
158 return dev_getifmap(dev, ifr);
161 ifr->ifr_ifindex = dev->ifindex;
165 ifr->ifr_qlen = dev->tx_queue_len;
169 /* dev_ioctl() should ensure this case
180 static int net_hwtstamp_validate(struct ifreq *ifr)
182 struct hwtstamp_config cfg;
183 enum hwtstamp_tx_types tx_type;
184 enum hwtstamp_rx_filters rx_filter;
185 int tx_type_valid = 0;
186 int rx_filter_valid = 0;
188 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
191 if (cfg.flags) /* reserved for future extensions */
194 tx_type = cfg.tx_type;
195 rx_filter = cfg.rx_filter;
198 case HWTSTAMP_TX_OFF:
200 case HWTSTAMP_TX_ONESTEP_SYNC:
201 case HWTSTAMP_TX_ONESTEP_P2P:
204 case __HWTSTAMP_TX_CNT:
205 /* not a real value */
210 case HWTSTAMP_FILTER_NONE:
211 case HWTSTAMP_FILTER_ALL:
212 case HWTSTAMP_FILTER_SOME:
213 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
214 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
215 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
216 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
217 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
218 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
219 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
220 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
221 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
222 case HWTSTAMP_FILTER_PTP_V2_EVENT:
223 case HWTSTAMP_FILTER_PTP_V2_SYNC:
224 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
225 case HWTSTAMP_FILTER_NTP_ALL:
228 case __HWTSTAMP_FILTER_CNT:
229 /* not a real value */
233 if (!tx_type_valid || !rx_filter_valid)
239 static int dev_do_ioctl(struct net_device *dev,
240 struct ifreq *ifr, unsigned int cmd)
242 const struct net_device_ops *ops = dev->netdev_ops;
245 err = dsa_ndo_do_ioctl(dev, ifr, cmd);
246 if (err == 0 || err != -EOPNOTSUPP)
249 if (ops->ndo_do_ioctl) {
250 if (netif_device_present(dev))
251 err = ops->ndo_do_ioctl(dev, ifr, cmd);
260 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
262 static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
265 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
266 const struct net_device_ops *ops;
271 ops = dev->netdev_ops;
274 case SIOCSIFFLAGS: /* Set interface flags */
275 return dev_change_flags(dev, ifr->ifr_flags, NULL);
277 case SIOCSIFMETRIC: /* Set the metric on the interface
278 (currently unused) */
281 case SIOCSIFMTU: /* Set the MTU of a device */
282 return dev_set_mtu(dev, ifr->ifr_mtu);
285 if (dev->addr_len > sizeof(struct sockaddr))
287 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
289 case SIOCSIFHWBROADCAST:
290 if (ifr->ifr_hwaddr.sa_family != dev->type)
292 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
293 min(sizeof(ifr->ifr_hwaddr.sa_data),
294 (size_t)dev->addr_len));
295 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
299 return dev_setifmap(dev, ifr);
302 if (!ops->ndo_set_rx_mode ||
303 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
305 if (!netif_device_present(dev))
307 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
310 if (!ops->ndo_set_rx_mode ||
311 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
313 if (!netif_device_present(dev))
315 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
318 if (ifr->ifr_qlen < 0)
320 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
323 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
324 return dev_change_name(dev, ifr->ifr_newname);
327 err = net_hwtstamp_validate(ifr);
333 * Unknown or private ioctl
336 if ((cmd >= SIOCDEVPRIVATE &&
337 cmd <= SIOCDEVPRIVATE + 15) ||
338 cmd == SIOCBONDENSLAVE ||
339 cmd == SIOCBONDRELEASE ||
340 cmd == SIOCBONDSETHWADDR ||
341 cmd == SIOCBONDSLAVEINFOQUERY ||
342 cmd == SIOCBONDINFOQUERY ||
343 cmd == SIOCBONDCHANGEACTIVE ||
344 cmd == SIOCGMIIPHY ||
345 cmd == SIOCGMIIREG ||
346 cmd == SIOCSMIIREG ||
347 cmd == SIOCBRADDIF ||
348 cmd == SIOCBRDELIF ||
349 cmd == SIOCSHWTSTAMP ||
350 cmd == SIOCGHWTSTAMP ||
352 err = dev_do_ioctl(dev, ifr, cmd);
361 * dev_load - load a network module
362 * @net: the applicable net namespace
363 * @name: name of interface
365 * If a network interface is not present and the process has suitable
366 * privileges this function loads the module. If module loading is not
367 * available in this kernel then it becomes a nop.
370 void dev_load(struct net *net, const char *name)
372 struct net_device *dev;
376 dev = dev_get_by_name_rcu(net, name);
380 if (no_module && capable(CAP_NET_ADMIN))
381 no_module = request_module("netdev-%s", name);
382 if (no_module && capable(CAP_SYS_MODULE))
383 request_module("%s", name);
385 EXPORT_SYMBOL(dev_load);
388 * This function handles all "interface"-type I/O control requests. The actual
389 * 'doing' part of this is dev_ifsioc above.
393 * dev_ioctl - network device ioctl
394 * @net: the applicable net namespace
395 * @cmd: command to issue
396 * @ifr: pointer to a struct ifreq in user space
397 * @need_copyout: whether or not copy_to_user() should be called
399 * Issue ioctl functions to devices. This is normally called by the
400 * user space syscall interfaces but can sometimes be useful for
401 * other purposes. The return value is the return from the syscall if
402 * positive or a negative errno code on error.
405 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr, bool *need_copyout)
411 *need_copyout = true;
412 if (cmd == SIOCGIFNAME)
413 return dev_ifname(net, ifr);
415 ifr->ifr_name[IFNAMSIZ-1] = 0;
417 colon = strchr(ifr->ifr_name, ':');
422 * See which interface the caller is talking about.
427 dev_load(net, ifr->ifr_name);
428 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
434 * - can be done by all.
435 * - atomic and do not require locking.
445 dev_load(net, ifr->ifr_name);
447 ret = dev_ifsioc_locked(net, ifr, cmd);
454 dev_load(net, ifr->ifr_name);
456 ret = dev_ethtool(net, ifr);
464 * - require superuser power.
465 * - require strict serialization.
471 dev_load(net, ifr->ifr_name);
472 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
475 ret = dev_ifsioc(net, ifr, cmd);
483 * - require superuser power.
484 * - require strict serialization.
485 * - do not return a value
489 if (!capable(CAP_NET_ADMIN))
494 * - require local superuser power.
495 * - require strict serialization.
496 * - do not return a value
505 case SIOCSIFHWBROADCAST:
507 case SIOCBONDENSLAVE:
508 case SIOCBONDRELEASE:
509 case SIOCBONDSETHWADDR:
510 case SIOCBONDCHANGEACTIVE:
514 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
517 case SIOCBONDSLAVEINFOQUERY:
518 case SIOCBONDINFOQUERY:
519 dev_load(net, ifr->ifr_name);
521 ret = dev_ifsioc(net, ifr, cmd);
524 *need_copyout = false;
528 /* Get the per device memory space. We can add this but
529 * currently do not support it */
531 /* Set the per device memory buffer space.
532 * Not applicable in our case */
537 * Unknown or private ioctl.
540 if (cmd == SIOCWANDEV ||
541 cmd == SIOCGHWTSTAMP ||
542 (cmd >= SIOCDEVPRIVATE &&
543 cmd <= SIOCDEVPRIVATE + 15)) {
544 dev_load(net, ifr->ifr_name);
546 ret = dev_ifsioc(net, ifr, cmd);