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;
43 * Fetch the caller's info block.
50 * Loop over the interfaces, and write an info block for each.
54 for_each_netdev(net, dev) {
57 done = inet_gifconf(dev, NULL, 0, size);
59 done = inet_gifconf(dev, pos + total,
67 * All done. Write the updated control block back to the caller.
72 * Both BSD and Solaris return 0 here, so we do too.
78 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
80 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
83 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
89 case SIOCGIFFLAGS: /* Get interface flags */
90 ifr->ifr_flags = (short) dev_get_flags(dev);
93 case SIOCGIFMETRIC: /* Get the metric on the interface
98 case SIOCGIFMTU: /* Get the MTU of a device */
99 ifr->ifr_mtu = dev->mtu;
107 ifr->ifr_map.mem_start = dev->mem_start;
108 ifr->ifr_map.mem_end = dev->mem_end;
109 ifr->ifr_map.base_addr = dev->base_addr;
110 ifr->ifr_map.irq = dev->irq;
111 ifr->ifr_map.dma = dev->dma;
112 ifr->ifr_map.port = dev->if_port;
116 ifr->ifr_ifindex = dev->ifindex;
120 ifr->ifr_qlen = dev->tx_queue_len;
124 /* dev_ioctl() should ensure this case
135 static int net_hwtstamp_validate(struct ifreq *ifr)
137 struct hwtstamp_config cfg;
138 enum hwtstamp_tx_types tx_type;
139 enum hwtstamp_rx_filters rx_filter;
140 int tx_type_valid = 0;
141 int rx_filter_valid = 0;
143 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
146 if (cfg.flags) /* reserved for future extensions */
149 tx_type = cfg.tx_type;
150 rx_filter = cfg.rx_filter;
153 case HWTSTAMP_TX_OFF:
155 case HWTSTAMP_TX_ONESTEP_SYNC:
156 case HWTSTAMP_TX_ONESTEP_P2P:
159 case __HWTSTAMP_TX_CNT:
160 /* not a real value */
165 case HWTSTAMP_FILTER_NONE:
166 case HWTSTAMP_FILTER_ALL:
167 case HWTSTAMP_FILTER_SOME:
168 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
169 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
170 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
171 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
172 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
173 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
174 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
175 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
176 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
177 case HWTSTAMP_FILTER_PTP_V2_EVENT:
178 case HWTSTAMP_FILTER_PTP_V2_SYNC:
179 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
180 case HWTSTAMP_FILTER_NTP_ALL:
183 case __HWTSTAMP_FILTER_CNT:
184 /* not a real value */
188 if (!tx_type_valid || !rx_filter_valid)
194 static int dev_do_ioctl(struct net_device *dev,
195 struct ifreq *ifr, unsigned int cmd)
197 const struct net_device_ops *ops = dev->netdev_ops;
198 int err = -EOPNOTSUPP;
200 err = dsa_ndo_do_ioctl(dev, ifr, cmd);
201 if (err == 0 || err != -EOPNOTSUPP)
204 if (ops->ndo_do_ioctl) {
205 if (netif_device_present(dev))
206 err = ops->ndo_do_ioctl(dev, ifr, cmd);
215 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
217 static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
220 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
221 const struct net_device_ops *ops;
226 ops = dev->netdev_ops;
229 case SIOCSIFFLAGS: /* Set interface flags */
230 return dev_change_flags(dev, ifr->ifr_flags, NULL);
232 case SIOCSIFMETRIC: /* Set the metric on the interface
233 (currently unused) */
236 case SIOCSIFMTU: /* Set the MTU of a device */
237 return dev_set_mtu(dev, ifr->ifr_mtu);
240 if (dev->addr_len > sizeof(struct sockaddr))
242 return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
244 case SIOCSIFHWBROADCAST:
245 if (ifr->ifr_hwaddr.sa_family != dev->type)
247 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
248 min(sizeof(ifr->ifr_hwaddr.sa_data),
249 (size_t)dev->addr_len));
250 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
254 if (ops->ndo_set_config) {
255 if (!netif_device_present(dev))
257 return ops->ndo_set_config(dev, &ifr->ifr_map);
262 if (!ops->ndo_set_rx_mode ||
263 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
265 if (!netif_device_present(dev))
267 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
270 if (!ops->ndo_set_rx_mode ||
271 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
273 if (!netif_device_present(dev))
275 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
278 if (ifr->ifr_qlen < 0)
280 return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
283 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
284 return dev_change_name(dev, ifr->ifr_newname);
287 err = net_hwtstamp_validate(ifr);
293 * Unknown or private ioctl
296 if ((cmd >= SIOCDEVPRIVATE &&
297 cmd <= SIOCDEVPRIVATE + 15) ||
298 cmd == SIOCBONDENSLAVE ||
299 cmd == SIOCBONDRELEASE ||
300 cmd == SIOCBONDSETHWADDR ||
301 cmd == SIOCBONDSLAVEINFOQUERY ||
302 cmd == SIOCBONDINFOQUERY ||
303 cmd == SIOCBONDCHANGEACTIVE ||
304 cmd == SIOCGMIIPHY ||
305 cmd == SIOCGMIIREG ||
306 cmd == SIOCSMIIREG ||
307 cmd == SIOCBRADDIF ||
308 cmd == SIOCBRDELIF ||
309 cmd == SIOCSHWTSTAMP ||
310 cmd == SIOCGHWTSTAMP ||
312 err = dev_do_ioctl(dev, ifr, cmd);
321 * dev_load - load a network module
322 * @net: the applicable net namespace
323 * @name: name of interface
325 * If a network interface is not present and the process has suitable
326 * privileges this function loads the module. If module loading is not
327 * available in this kernel then it becomes a nop.
330 void dev_load(struct net *net, const char *name)
332 struct net_device *dev;
336 dev = dev_get_by_name_rcu(net, name);
340 if (no_module && capable(CAP_NET_ADMIN))
341 no_module = request_module("netdev-%s", name);
342 if (no_module && capable(CAP_SYS_MODULE))
343 request_module("%s", name);
345 EXPORT_SYMBOL(dev_load);
348 * This function handles all "interface"-type I/O control requests. The actual
349 * 'doing' part of this is dev_ifsioc above.
353 * dev_ioctl - network device ioctl
354 * @net: the applicable net namespace
355 * @cmd: command to issue
356 * @ifr: pointer to a struct ifreq in user space
357 * @need_copyout: whether or not copy_to_user() should be called
359 * Issue ioctl functions to devices. This is normally called by the
360 * user space syscall interfaces but can sometimes be useful for
361 * other purposes. The return value is the return from the syscall if
362 * positive or a negative errno code on error.
365 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr, bool *need_copyout)
371 *need_copyout = true;
372 if (cmd == SIOCGIFNAME)
373 return dev_ifname(net, ifr);
375 ifr->ifr_name[IFNAMSIZ-1] = 0;
377 colon = strchr(ifr->ifr_name, ':');
382 * See which interface the caller is talking about.
387 dev_load(net, ifr->ifr_name);
388 ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
394 * - can be done by all.
395 * - atomic and do not require locking.
405 dev_load(net, ifr->ifr_name);
407 ret = dev_ifsioc_locked(net, ifr, cmd);
414 dev_load(net, ifr->ifr_name);
416 ret = dev_ethtool(net, ifr);
424 * - require superuser power.
425 * - require strict serialization.
431 dev_load(net, ifr->ifr_name);
432 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
435 ret = dev_ifsioc(net, ifr, cmd);
443 * - require superuser power.
444 * - require strict serialization.
445 * - do not return a value
449 if (!capable(CAP_NET_ADMIN))
454 * - require local superuser power.
455 * - require strict serialization.
456 * - do not return a value
465 case SIOCSIFHWBROADCAST:
467 case SIOCBONDENSLAVE:
468 case SIOCBONDRELEASE:
469 case SIOCBONDSETHWADDR:
470 case SIOCBONDCHANGEACTIVE:
474 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
477 case SIOCBONDSLAVEINFOQUERY:
478 case SIOCBONDINFOQUERY:
479 dev_load(net, ifr->ifr_name);
481 ret = dev_ifsioc(net, ifr, cmd);
484 *need_copyout = false;
488 /* Get the per device memory space. We can add this but
489 * currently do not support it */
491 /* Set the per device memory buffer space.
492 * Not applicable in our case */
497 * Unknown or private ioctl.
500 if (cmd == SIOCWANDEV ||
501 cmd == SIOCGHWTSTAMP ||
502 (cmd >= SIOCDEVPRIVATE &&
503 cmd <= SIOCDEVPRIVATE + 15)) {
504 dev_load(net, ifr->ifr_name);
506 ret = dev_ifsioc(net, ifr, cmd);