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 <linux/if_bridge.h>
10 #include <net/dsa_stubs.h>
16 * Map an interface index to its name (SIOCGIFNAME)
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
26 static int dev_ifname(struct net *net, struct ifreq *ifr)
28 ifr->ifr_name[IFNAMSIZ-1] = 0;
29 return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
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'.
37 int dev_ifconf(struct net *net, struct ifconf __user *uifc)
39 struct net_device *dev;
42 int len, total = 0, done;
44 /* both the ifconf and the ifreq structures are slightly different */
45 if (in_compat_syscall()) {
46 struct compat_ifconf ifc32;
48 if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
51 pos = compat_ptr(ifc32.ifcbuf);
53 size = sizeof(struct compat_ifreq);
57 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
62 size = sizeof(struct ifreq);
65 /* Loop over the interfaces, and write an info block for each. */
67 for_each_netdev(net, dev) {
69 done = inet_gifconf(dev, NULL, 0, size);
71 done = inet_gifconf(dev, pos + total,
81 return put_user(total, &uifc->ifc_len);
84 static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
86 struct ifmap *ifmap = &ifr->ifr_map;
88 if (in_compat_syscall()) {
89 struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
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;
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;
111 static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
113 struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
115 if (!dev->netdev_ops->ndo_set_config)
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,
125 .port = cifmap->port,
128 return dev->netdev_ops->ndo_set_config(dev, &ifmap);
131 return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
135 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
137 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
140 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
146 case SIOCGIFFLAGS: /* Get interface flags */
147 ifr->ifr_flags = (short) dev_get_flags(dev);
150 case SIOCGIFMETRIC: /* Get the metric on the interface
151 (currently unused) */
155 case SIOCGIFMTU: /* Get the MTU of a device */
156 ifr->ifr_mtu = dev->mtu;
164 return dev_getifmap(dev, ifr);
167 ifr->ifr_ifindex = dev->ifindex;
171 ifr->ifr_qlen = dev->tx_queue_len;
175 /* dev_ioctl() should ensure this case
186 static int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
188 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;
193 if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
196 tx_type = cfg->tx_type;
197 rx_filter = cfg->rx_filter;
200 case HWTSTAMP_TX_OFF:
202 case HWTSTAMP_TX_ONESTEP_SYNC:
203 case HWTSTAMP_TX_ONESTEP_P2P:
206 case __HWTSTAMP_TX_CNT:
207 /* not a real value */
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:
227 case HWTSTAMP_FILTER_NTP_ALL:
230 case __HWTSTAMP_FILTER_CNT:
231 /* not a real value */
235 if (!tx_type_valid || !rx_filter_valid)
241 static int dev_eth_ioctl(struct net_device *dev,
242 struct ifreq *ifr, unsigned int cmd)
244 const struct net_device_ops *ops = dev->netdev_ops;
246 if (!ops->ndo_eth_ioctl)
249 if (!netif_device_present(dev))
252 return ops->ndo_eth_ioctl(dev, ifr, cmd);
255 static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
257 return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
260 static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
262 struct kernel_hwtstamp_config kernel_cfg;
263 struct netlink_ext_ack extack = {};
264 struct hwtstamp_config cfg;
267 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
270 hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
272 err = net_hwtstamp_validate(&kernel_cfg);
276 err = dsa_master_hwtstamp_validate(dev, &kernel_cfg, &extack);
279 netdev_err(dev, "%s\n", extack._msg);
283 return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
286 static int dev_siocbond(struct net_device *dev,
287 struct ifreq *ifr, unsigned int cmd)
289 const struct net_device_ops *ops = dev->netdev_ops;
291 if (ops->ndo_siocbond) {
292 if (netif_device_present(dev))
293 return ops->ndo_siocbond(dev, ifr, cmd);
301 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
302 void __user *data, unsigned int cmd)
304 const struct net_device_ops *ops = dev->netdev_ops;
306 if (ops->ndo_siocdevprivate) {
307 if (netif_device_present(dev))
308 return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
316 static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
318 const struct net_device_ops *ops = dev->netdev_ops;
320 if (ops->ndo_siocwandev) {
321 if (netif_device_present(dev))
322 return ops->ndo_siocwandev(dev, ifs);
331 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
333 static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
337 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
338 const struct net_device_ops *ops;
339 netdevice_tracker dev_tracker;
344 ops = dev->netdev_ops;
347 case SIOCSIFFLAGS: /* Set interface flags */
348 return dev_change_flags(dev, ifr->ifr_flags, NULL);
350 case SIOCSIFMETRIC: /* Set the metric on the interface
351 (currently unused) */
354 case SIOCSIFMTU: /* Set the MTU of a device */
355 return dev_set_mtu(dev, ifr->ifr_mtu);
358 if (dev->addr_len > sizeof(struct sockaddr))
360 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
362 case SIOCSIFHWBROADCAST:
363 if (ifr->ifr_hwaddr.sa_family != dev->type)
365 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
366 min(sizeof(ifr->ifr_hwaddr.sa_data_min),
367 (size_t)dev->addr_len));
368 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
372 return dev_setifmap(dev, ifr);
375 if (!ops->ndo_set_rx_mode ||
376 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
378 if (!netif_device_present(dev))
380 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
383 if (!ops->ndo_set_rx_mode ||
384 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
386 if (!netif_device_present(dev))
388 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
391 if (ifr->ifr_qlen < 0)
393 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
396 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
397 return dev_change_name(dev, ifr->ifr_newname);
400 return dev_siocwandev(dev, &ifr->ifr_settings);
404 if (!netif_device_present(dev))
406 if (!netif_is_bridge_master(dev))
408 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
410 err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
411 netdev_put(dev, &dev_tracker);
415 case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
416 return dev_siocdevprivate(dev, ifr, data, cmd);
419 return dev_set_hwtstamp(dev, ifr);
422 return dev_get_hwtstamp(dev, ifr);
427 return dev_eth_ioctl(dev, ifr, cmd);
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);
445 * dev_load - load a network module
446 * @net: the applicable net namespace
447 * @name: name of interface
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.
454 void dev_load(struct net *net, const char *name)
456 struct net_device *dev;
460 dev = dev_get_by_name_rcu(net, name);
464 if (no_module && capable(CAP_NET_ADMIN))
465 no_module = request_module("netdev-%s", name);
466 if (no_module && capable(CAP_SYS_MODULE))
467 request_module("%s", name);
469 EXPORT_SYMBOL(dev_load);
472 * This function handles all "interface"-type I/O control requests. The actual
473 * 'doing' part of this is dev_ifsioc above.
477 * dev_ioctl - network device ioctl
478 * @net: the applicable net namespace
479 * @cmd: command to issue
480 * @ifr: pointer to a struct ifreq in user space
481 * @data: data exchanged with userspace
482 * @need_copyout: whether or not copy_to_user() should be called
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.
490 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
491 void __user *data, bool *need_copyout)
497 *need_copyout = true;
498 if (cmd == SIOCGIFNAME)
499 return dev_ifname(net, ifr);
501 ifr->ifr_name[IFNAMSIZ-1] = 0;
503 colon = strchr(ifr->ifr_name, ':');
508 * See which interface the caller is talking about.
513 dev_load(net, ifr->ifr_name);
514 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
520 * - can be done by all.
521 * - atomic and do not require locking.
531 dev_load(net, ifr->ifr_name);
533 ret = dev_ifsioc_locked(net, ifr, cmd);
540 dev_load(net, ifr->ifr_name);
541 ret = dev_ethtool(net, ifr, data);
548 * - require superuser power.
549 * - require strict serialization.
555 dev_load(net, ifr->ifr_name);
556 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
559 ret = dev_ifsioc(net, ifr, data, cmd);
567 * - require superuser power.
568 * - require strict serialization.
569 * - do not return a value
573 if (!capable(CAP_NET_ADMIN))
578 * - require local superuser power.
579 * - require strict serialization.
580 * - do not return a value
589 case SIOCSIFHWBROADCAST:
591 case SIOCBONDENSLAVE:
592 case SIOCBONDRELEASE:
593 case SIOCBONDSETHWADDR:
594 case SIOCBONDCHANGEACTIVE:
598 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
601 case SIOCBONDSLAVEINFOQUERY:
602 case SIOCBONDINFOQUERY:
603 dev_load(net, ifr->ifr_name);
605 ret = dev_ifsioc(net, ifr, data, cmd);
608 *need_copyout = false;
612 /* Get the per device memory space. We can add this but
613 * currently do not support it */
615 /* Set the per device memory buffer space.
616 * Not applicable in our case */
621 * Unknown or private ioctl.
624 if (cmd == SIOCWANDEV ||
625 cmd == SIOCGHWTSTAMP ||
626 (cmd >= SIOCDEVPRIVATE &&
627 cmd <= SIOCDEVPRIVATE + 15)) {
628 dev_load(net, ifr->ifr_name);
630 ret = dev_ifsioc(net, ifr, data, cmd);