1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kmod.h>
3 #include <linux/netdevice.h>
4 #include <linux/etherdevice.h>
5 #include <linux/rtnetlink.h>
6 #include <linux/net_tstamp.h>
7 #include <linux/wireless.h>
11 * Map an interface index to its name (SIOCGIFNAME)
15 * We need this ioctl for efficient implementation of the
16 * if_indextoname() function required by the IPv6 API. Without
17 * it, we would have to search all the interfaces to find a
21 static int dev_ifname(struct net *net, struct ifreq __user *arg)
27 * Fetch the caller's info block.
30 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
32 ifr.ifr_name[IFNAMSIZ-1] = 0;
34 error = netdev_get_name(net, ifr.ifr_name, ifr.ifr_ifindex);
38 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
43 static gifconf_func_t *gifconf_list[NPROTO];
46 * register_gifconf - register a SIOCGIF handler
47 * @family: Address family
48 * @gifconf: Function handler
50 * Register protocol dependent address dumping routines. The handler
51 * that is passed must not be freed or reused until it has been replaced
54 int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
58 gifconf_list[family] = gifconf;
61 EXPORT_SYMBOL(register_gifconf);
64 * Perform a SIOCGIFCONF call. This structure will change
65 * size eventually, and there is nothing I can do about it.
66 * Thus we will need a 'compatibility mode'.
69 int dev_ifconf(struct net *net, struct ifconf *ifc, int size)
71 struct net_device *dev;
78 * Fetch the caller's info block.
85 * Loop over the interfaces, and write an info block for each.
89 for_each_netdev(net, dev) {
90 for (i = 0; i < NPROTO; i++) {
91 if (gifconf_list[i]) {
94 done = gifconf_list[i](dev, NULL, 0, size);
96 done = gifconf_list[i](dev, pos + total,
106 * All done. Write the updated control block back to the caller.
108 ifc->ifc_len = total;
111 * Both BSD and Solaris return 0 here, so we do too.
117 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
119 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
122 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
128 case SIOCGIFFLAGS: /* Get interface flags */
129 ifr->ifr_flags = (short) dev_get_flags(dev);
132 case SIOCGIFMETRIC: /* Get the metric on the interface
133 (currently unused) */
137 case SIOCGIFMTU: /* Get the MTU of a device */
138 ifr->ifr_mtu = dev->mtu;
143 memset(ifr->ifr_hwaddr.sa_data, 0,
144 sizeof(ifr->ifr_hwaddr.sa_data));
146 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
147 min(sizeof(ifr->ifr_hwaddr.sa_data),
148 (size_t)dev->addr_len));
149 ifr->ifr_hwaddr.sa_family = dev->type;
157 ifr->ifr_map.mem_start = dev->mem_start;
158 ifr->ifr_map.mem_end = dev->mem_end;
159 ifr->ifr_map.base_addr = dev->base_addr;
160 ifr->ifr_map.irq = dev->irq;
161 ifr->ifr_map.dma = dev->dma;
162 ifr->ifr_map.port = dev->if_port;
166 ifr->ifr_ifindex = dev->ifindex;
170 ifr->ifr_qlen = dev->tx_queue_len;
174 /* dev_ioctl() should ensure this case
185 static int net_hwtstamp_validate(struct ifreq *ifr)
187 struct 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 (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
196 if (cfg.flags) /* reserved for future extensions */
199 tx_type = cfg.tx_type;
200 rx_filter = cfg.rx_filter;
203 case HWTSTAMP_TX_OFF:
205 case HWTSTAMP_TX_ONESTEP_SYNC:
211 case HWTSTAMP_FILTER_NONE:
212 case HWTSTAMP_FILTER_ALL:
213 case HWTSTAMP_FILTER_SOME:
214 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
215 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
216 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
217 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
218 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
219 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
220 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
221 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
222 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
223 case HWTSTAMP_FILTER_PTP_V2_EVENT:
224 case HWTSTAMP_FILTER_PTP_V2_SYNC:
225 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
226 case HWTSTAMP_FILTER_NTP_ALL:
231 if (!tx_type_valid || !rx_filter_valid)
238 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
240 static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
243 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
244 const struct net_device_ops *ops;
249 ops = dev->netdev_ops;
252 case SIOCSIFFLAGS: /* Set interface flags */
253 return dev_change_flags(dev, ifr->ifr_flags);
255 case SIOCSIFMETRIC: /* Set the metric on the interface
256 (currently unused) */
259 case SIOCSIFMTU: /* Set the MTU of a device */
260 return dev_set_mtu(dev, ifr->ifr_mtu);
263 if (dev->addr_len > sizeof(struct sockaddr))
265 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
267 case SIOCSIFHWBROADCAST:
268 if (ifr->ifr_hwaddr.sa_family != dev->type)
270 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
271 min(sizeof(ifr->ifr_hwaddr.sa_data),
272 (size_t)dev->addr_len));
273 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
277 if (ops->ndo_set_config) {
278 if (!netif_device_present(dev))
280 return ops->ndo_set_config(dev, &ifr->ifr_map);
285 if (!ops->ndo_set_rx_mode ||
286 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
288 if (!netif_device_present(dev))
290 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
293 if (!ops->ndo_set_rx_mode ||
294 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
296 if (!netif_device_present(dev))
298 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
301 if (ifr->ifr_qlen < 0)
303 if (dev->tx_queue_len ^ ifr->ifr_qlen) {
304 unsigned int orig_len = dev->tx_queue_len;
306 dev->tx_queue_len = ifr->ifr_qlen;
307 err = call_netdevice_notifiers(
308 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
309 err = notifier_to_errno(err);
311 dev->tx_queue_len = orig_len;
318 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
319 return dev_change_name(dev, ifr->ifr_newname);
322 err = net_hwtstamp_validate(ifr);
328 * Unknown or private ioctl
331 if ((cmd >= SIOCDEVPRIVATE &&
332 cmd <= SIOCDEVPRIVATE + 15) ||
333 cmd == SIOCBONDENSLAVE ||
334 cmd == SIOCBONDRELEASE ||
335 cmd == SIOCBONDSETHWADDR ||
336 cmd == SIOCBONDSLAVEINFOQUERY ||
337 cmd == SIOCBONDINFOQUERY ||
338 cmd == SIOCBONDCHANGEACTIVE ||
339 cmd == SIOCGMIIPHY ||
340 cmd == SIOCGMIIREG ||
341 cmd == SIOCSMIIREG ||
342 cmd == SIOCBRADDIF ||
343 cmd == SIOCBRDELIF ||
344 cmd == SIOCSHWTSTAMP ||
345 cmd == SIOCGHWTSTAMP ||
348 if (ops->ndo_do_ioctl) {
349 if (netif_device_present(dev))
350 err = ops->ndo_do_ioctl(dev, ifr, cmd);
362 * dev_load - load a network module
363 * @net: the applicable net namespace
364 * @name: name of interface
366 * If a network interface is not present and the process has suitable
367 * privileges this function loads the module. If module loading is not
368 * available in this kernel then it becomes a nop.
371 void dev_load(struct net *net, const char *name)
373 struct net_device *dev;
377 dev = dev_get_by_name_rcu(net, name);
381 if (no_module && capable(CAP_NET_ADMIN))
382 no_module = request_module("netdev-%s", name);
383 if (no_module && capable(CAP_SYS_MODULE))
384 request_module("%s", name);
386 EXPORT_SYMBOL(dev_load);
389 * This function handles all "interface"-type I/O control requests. The actual
390 * 'doing' part of this is dev_ifsioc above.
394 * dev_ioctl - network device ioctl
395 * @net: the applicable net namespace
396 * @cmd: command to issue
397 * @arg: pointer to a struct ifreq in user space
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, void __user *arg)
411 if (cmd == SIOCGIFNAME)
412 return dev_ifname(net, (struct ifreq __user *)arg);
415 * Take care of Wireless Extensions. Unfortunately struct iwreq
416 * isn't a proper subset of struct ifreq (it's 8 byte shorter)
417 * so we need to treat it specially, otherwise applications may
418 * fault if the struct they're passing happens to land at the
419 * end of a mapped page.
421 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
424 if (copy_from_user(&iwr, arg, sizeof(iwr)))
427 iwr.ifr_name[sizeof(iwr.ifr_name) - 1] = 0;
429 return wext_handle_ioctl(net, &iwr, cmd, arg);
432 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
435 ifr.ifr_name[IFNAMSIZ-1] = 0;
437 colon = strchr(ifr.ifr_name, ':');
442 * See which interface the caller is talking about.
448 * - can be done by all.
449 * - atomic and do not require locking.
460 dev_load(net, ifr.ifr_name);
462 ret = dev_ifsioc_locked(net, &ifr, cmd);
467 if (copy_to_user(arg, &ifr,
468 sizeof(struct ifreq)))
474 dev_load(net, ifr.ifr_name);
476 ret = dev_ethtool(net, &ifr);
481 if (copy_to_user(arg, &ifr,
482 sizeof(struct ifreq)))
489 * - require superuser power.
490 * - require strict serialization.
496 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
498 dev_load(net, ifr.ifr_name);
500 ret = dev_ifsioc(net, &ifr, cmd);
505 if (copy_to_user(arg, &ifr,
506 sizeof(struct ifreq)))
513 * - require superuser power.
514 * - require strict serialization.
515 * - do not return a value
519 if (!capable(CAP_NET_ADMIN))
524 * - require local superuser power.
525 * - require strict serialization.
526 * - do not return a value
535 case SIOCSIFHWBROADCAST:
537 case SIOCBONDENSLAVE:
538 case SIOCBONDRELEASE:
539 case SIOCBONDSETHWADDR:
540 case SIOCBONDCHANGEACTIVE:
544 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
547 case SIOCBONDSLAVEINFOQUERY:
548 case SIOCBONDINFOQUERY:
549 dev_load(net, ifr.ifr_name);
551 ret = dev_ifsioc(net, &ifr, cmd);
556 /* Get the per device memory space. We can add this but
557 * currently do not support it */
559 /* Set the per device memory buffer space.
560 * Not applicable in our case */
565 * Unknown or private ioctl.
568 if (cmd == SIOCWANDEV ||
569 cmd == SIOCGHWTSTAMP ||
570 (cmd >= SIOCDEVPRIVATE &&
571 cmd <= SIOCDEVPRIVATE + 15)) {
572 dev_load(net, ifr.ifr_name);
574 ret = dev_ifsioc(net, &ifr, cmd);
576 if (!ret && copy_to_user(arg, &ifr,
577 sizeof(struct ifreq)))