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>
14 * Map an interface index to its name (SIOCGIFNAME)
18 * We need this ioctl for efficient implementation of the
19 * if_indextoname() function required by the IPv6 API. Without
20 * it, we would have to search all the interfaces to find a
24 static int dev_ifname(struct net *net, struct ifreq *ifr)
26 ifr->ifr_name[IFNAMSIZ-1] = 0;
27 return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
31 * Perform a SIOCGIFCONF call. This structure will change
32 * size eventually, and there is nothing I can do about it.
33 * Thus we will need a 'compatibility mode'.
35 int dev_ifconf(struct net *net, struct ifconf __user *uifc)
37 struct net_device *dev;
40 int len, total = 0, done;
42 /* both the ifconf and the ifreq structures are slightly different */
43 if (in_compat_syscall()) {
44 struct compat_ifconf ifc32;
46 if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
49 pos = compat_ptr(ifc32.ifcbuf);
51 size = sizeof(struct compat_ifreq);
55 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
60 size = sizeof(struct ifreq);
63 /* Loop over the interfaces, and write an info block for each. */
65 for_each_netdev(net, dev) {
67 done = inet_gifconf(dev, NULL, 0, size);
69 done = inet_gifconf(dev, pos + total,
79 return put_user(total, &uifc->ifc_len);
82 static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
84 struct ifmap *ifmap = &ifr->ifr_map;
86 if (in_compat_syscall()) {
87 struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
89 cifmap->mem_start = dev->mem_start;
90 cifmap->mem_end = dev->mem_end;
91 cifmap->base_addr = dev->base_addr;
92 cifmap->irq = dev->irq;
93 cifmap->dma = dev->dma;
94 cifmap->port = dev->if_port;
99 ifmap->mem_start = dev->mem_start;
100 ifmap->mem_end = dev->mem_end;
101 ifmap->base_addr = dev->base_addr;
102 ifmap->irq = dev->irq;
103 ifmap->dma = dev->dma;
104 ifmap->port = dev->if_port;
109 static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
111 struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
113 if (!dev->netdev_ops->ndo_set_config)
116 if (in_compat_syscall()) {
117 struct ifmap ifmap = {
118 .mem_start = cifmap->mem_start,
119 .mem_end = cifmap->mem_end,
120 .base_addr = cifmap->base_addr,
123 .port = cifmap->port,
126 return dev->netdev_ops->ndo_set_config(dev, &ifmap);
129 return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
133 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
135 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
138 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
144 case SIOCGIFFLAGS: /* Get interface flags */
145 ifr->ifr_flags = (short) dev_get_flags(dev);
148 case SIOCGIFMETRIC: /* Get the metric on the interface
149 (currently unused) */
153 case SIOCGIFMTU: /* Get the MTU of a device */
154 ifr->ifr_mtu = dev->mtu;
162 return dev_getifmap(dev, ifr);
165 ifr->ifr_ifindex = dev->ifindex;
169 ifr->ifr_qlen = dev->tx_queue_len;
173 /* dev_ioctl() should ensure this case
184 static int net_hwtstamp_validate(struct ifreq *ifr)
186 struct hwtstamp_config cfg;
187 enum hwtstamp_tx_types tx_type;
188 enum hwtstamp_rx_filters rx_filter;
189 int tx_type_valid = 0;
190 int rx_filter_valid = 0;
192 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
195 if (cfg.flags) /* reserved for future extensions */
198 tx_type = cfg.tx_type;
199 rx_filter = cfg.rx_filter;
202 case HWTSTAMP_TX_OFF:
204 case HWTSTAMP_TX_ONESTEP_SYNC:
205 case HWTSTAMP_TX_ONESTEP_P2P:
208 case __HWTSTAMP_TX_CNT:
209 /* not a real value */
214 case HWTSTAMP_FILTER_NONE:
215 case HWTSTAMP_FILTER_ALL:
216 case HWTSTAMP_FILTER_SOME:
217 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
218 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
219 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
220 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
221 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
222 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
223 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
224 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
225 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
226 case HWTSTAMP_FILTER_PTP_V2_EVENT:
227 case HWTSTAMP_FILTER_PTP_V2_SYNC:
228 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
229 case HWTSTAMP_FILTER_NTP_ALL:
232 case __HWTSTAMP_FILTER_CNT:
233 /* not a real value */
237 if (!tx_type_valid || !rx_filter_valid)
243 static int dev_eth_ioctl(struct net_device *dev,
244 struct ifreq *ifr, unsigned int cmd)
246 const struct net_device_ops *ops = dev->netdev_ops;
249 err = dsa_ndo_eth_ioctl(dev, ifr, cmd);
250 if (err == 0 || err != -EOPNOTSUPP)
253 if (ops->ndo_eth_ioctl) {
254 if (netif_device_present(dev))
255 err = ops->ndo_eth_ioctl(dev, ifr, cmd);
263 static int dev_siocbond(struct net_device *dev,
264 struct ifreq *ifr, unsigned int cmd)
266 const struct net_device_ops *ops = dev->netdev_ops;
268 if (ops->ndo_siocbond) {
269 if (netif_device_present(dev))
270 return ops->ndo_siocbond(dev, ifr, cmd);
278 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
279 void __user *data, unsigned int cmd)
281 const struct net_device_ops *ops = dev->netdev_ops;
283 if (ops->ndo_siocdevprivate) {
284 if (netif_device_present(dev))
285 return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
293 static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
295 const struct net_device_ops *ops = dev->netdev_ops;
297 if (ops->ndo_siocwandev) {
298 if (netif_device_present(dev))
299 return ops->ndo_siocwandev(dev, ifs);
308 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
310 static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
314 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
315 const struct net_device_ops *ops;
320 ops = dev->netdev_ops;
323 case SIOCSIFFLAGS: /* Set interface flags */
324 return dev_change_flags(dev, ifr->ifr_flags, NULL);
326 case SIOCSIFMETRIC: /* Set the metric on the interface
327 (currently unused) */
330 case SIOCSIFMTU: /* Set the MTU of a device */
331 return dev_set_mtu(dev, ifr->ifr_mtu);
334 if (dev->addr_len > sizeof(struct sockaddr))
336 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
338 case SIOCSIFHWBROADCAST:
339 if (ifr->ifr_hwaddr.sa_family != dev->type)
341 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
342 min(sizeof(ifr->ifr_hwaddr.sa_data),
343 (size_t)dev->addr_len));
344 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
348 return dev_setifmap(dev, ifr);
351 if (!ops->ndo_set_rx_mode ||
352 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
354 if (!netif_device_present(dev))
356 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
359 if (!ops->ndo_set_rx_mode ||
360 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
362 if (!netif_device_present(dev))
364 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
367 if (ifr->ifr_qlen < 0)
369 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
372 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
373 return dev_change_name(dev, ifr->ifr_newname);
376 return dev_siocwandev(dev, &ifr->ifr_settings);
380 if (!netif_device_present(dev))
382 if (!netif_is_bridge_master(dev))
386 err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
392 err = net_hwtstamp_validate(ifr);
398 * Unknown or private ioctl
401 if (cmd >= SIOCDEVPRIVATE &&
402 cmd <= SIOCDEVPRIVATE + 15)
403 return dev_siocdevprivate(dev, ifr, data, cmd);
405 if (cmd == SIOCGMIIPHY ||
406 cmd == SIOCGMIIREG ||
407 cmd == SIOCSMIIREG ||
408 cmd == SIOCSHWTSTAMP ||
409 cmd == SIOCGHWTSTAMP) {
410 err = dev_eth_ioctl(dev, ifr, cmd);
411 } else if (cmd == SIOCBONDENSLAVE ||
412 cmd == SIOCBONDRELEASE ||
413 cmd == SIOCBONDSETHWADDR ||
414 cmd == SIOCBONDSLAVEINFOQUERY ||
415 cmd == SIOCBONDINFOQUERY ||
416 cmd == SIOCBONDCHANGEACTIVE) {
417 err = dev_siocbond(dev, ifr, cmd);
426 * dev_load - load a network module
427 * @net: the applicable net namespace
428 * @name: name of interface
430 * If a network interface is not present and the process has suitable
431 * privileges this function loads the module. If module loading is not
432 * available in this kernel then it becomes a nop.
435 void dev_load(struct net *net, const char *name)
437 struct net_device *dev;
441 dev = dev_get_by_name_rcu(net, name);
445 if (no_module && capable(CAP_NET_ADMIN))
446 no_module = request_module("netdev-%s", name);
447 if (no_module && capable(CAP_SYS_MODULE))
448 request_module("%s", name);
450 EXPORT_SYMBOL(dev_load);
453 * This function handles all "interface"-type I/O control requests. The actual
454 * 'doing' part of this is dev_ifsioc above.
458 * dev_ioctl - network device ioctl
459 * @net: the applicable net namespace
460 * @cmd: command to issue
461 * @ifr: pointer to a struct ifreq in user space
462 * @need_copyout: whether or not copy_to_user() should be called
464 * Issue ioctl functions to devices. This is normally called by the
465 * user space syscall interfaces but can sometimes be useful for
466 * other purposes. The return value is the return from the syscall if
467 * positive or a negative errno code on error.
470 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
471 void __user *data, bool *need_copyout)
477 *need_copyout = true;
478 if (cmd == SIOCGIFNAME)
479 return dev_ifname(net, ifr);
481 ifr->ifr_name[IFNAMSIZ-1] = 0;
483 colon = strchr(ifr->ifr_name, ':');
488 * See which interface the caller is talking about.
493 dev_load(net, ifr->ifr_name);
494 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
500 * - can be done by all.
501 * - atomic and do not require locking.
511 dev_load(net, ifr->ifr_name);
513 ret = dev_ifsioc_locked(net, ifr, cmd);
520 dev_load(net, ifr->ifr_name);
522 ret = dev_ethtool(net, ifr, data);
530 * - require superuser power.
531 * - require strict serialization.
537 dev_load(net, ifr->ifr_name);
538 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
541 ret = dev_ifsioc(net, ifr, data, cmd);
549 * - require superuser power.
550 * - require strict serialization.
551 * - do not return a value
555 if (!capable(CAP_NET_ADMIN))
560 * - require local superuser power.
561 * - require strict serialization.
562 * - do not return a value
571 case SIOCSIFHWBROADCAST:
573 case SIOCBONDENSLAVE:
574 case SIOCBONDRELEASE:
575 case SIOCBONDSETHWADDR:
576 case SIOCBONDCHANGEACTIVE:
580 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
583 case SIOCBONDSLAVEINFOQUERY:
584 case SIOCBONDINFOQUERY:
585 dev_load(net, ifr->ifr_name);
587 ret = dev_ifsioc(net, ifr, data, cmd);
590 *need_copyout = false;
594 /* Get the per device memory space. We can add this but
595 * currently do not support it */
597 /* Set the per device memory buffer space.
598 * Not applicable in our case */
603 * Unknown or private ioctl.
606 if (cmd == SIOCWANDEV ||
607 cmd == SIOCGHWTSTAMP ||
608 (cmd >= SIOCDEVPRIVATE &&
609 cmd <= SIOCDEVPRIVATE + 15)) {
610 dev_load(net, ifr->ifr_name);
612 ret = dev_ifsioc(net, ifr, data, cmd);