1 #include <linux/kmod.h>
2 #include <linux/netdevice.h>
3 #include <linux/etherdevice.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/net_tstamp.h>
6 #include <linux/wireless.h>
10 * Map an interface index to its name (SIOCGIFNAME)
14 * We need this ioctl for efficient implementation of the
15 * if_indextoname() function required by the IPv6 API. Without
16 * it, we would have to search all the interfaces to find a
20 static int dev_ifname(struct net *net, struct ifreq __user *arg)
22 struct net_device *dev;
27 * Fetch the caller's info block.
30 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
34 seq = read_seqcount_begin(&devnet_rename_seq);
36 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
42 strcpy(ifr.ifr_name, dev->name);
44 if (read_seqcount_retry(&devnet_rename_seq, seq))
47 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
52 static gifconf_func_t *gifconf_list[NPROTO];
55 * register_gifconf - register a SIOCGIF handler
56 * @family: Address family
57 * @gifconf: Function handler
59 * Register protocol dependent address dumping routines. The handler
60 * that is passed must not be freed or reused until it has been replaced
63 int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
67 gifconf_list[family] = gifconf;
70 EXPORT_SYMBOL(register_gifconf);
73 * Perform a SIOCGIFCONF call. This structure will change
74 * size eventually, and there is nothing I can do about it.
75 * Thus we will need a 'compatibility mode'.
78 static int dev_ifconf(struct net *net, char __user *arg)
81 struct net_device *dev;
88 * Fetch the caller's info block.
91 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
98 * Loop over the interfaces, and write an info block for each.
102 for_each_netdev(net, dev) {
103 for (i = 0; i < NPROTO; i++) {
104 if (gifconf_list[i]) {
107 done = gifconf_list[i](dev, NULL, 0);
109 done = gifconf_list[i](dev, pos + total,
119 * All done. Write the updated control block back to the caller.
124 * Both BSD and Solaris return 0 here, so we do too.
126 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
130 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
132 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
135 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
141 case SIOCGIFFLAGS: /* Get interface flags */
142 ifr->ifr_flags = (short) dev_get_flags(dev);
145 case SIOCGIFMETRIC: /* Get the metric on the interface
146 (currently unused) */
150 case SIOCGIFMTU: /* Get the MTU of a device */
151 ifr->ifr_mtu = dev->mtu;
156 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
158 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
159 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
160 ifr->ifr_hwaddr.sa_family = dev->type;
168 ifr->ifr_map.mem_start = dev->mem_start;
169 ifr->ifr_map.mem_end = dev->mem_end;
170 ifr->ifr_map.base_addr = dev->base_addr;
171 ifr->ifr_map.irq = dev->irq;
172 ifr->ifr_map.dma = dev->dma;
173 ifr->ifr_map.port = dev->if_port;
177 ifr->ifr_ifindex = dev->ifindex;
181 ifr->ifr_qlen = dev->tx_queue_len;
185 /* dev_ioctl() should ensure this case
196 static int net_hwtstamp_validate(struct ifreq *ifr)
198 struct hwtstamp_config cfg;
199 enum hwtstamp_tx_types tx_type;
200 enum hwtstamp_rx_filters rx_filter;
201 int tx_type_valid = 0;
202 int rx_filter_valid = 0;
204 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
207 if (cfg.flags) /* reserved for future extensions */
210 tx_type = cfg.tx_type;
211 rx_filter = cfg.rx_filter;
214 case HWTSTAMP_TX_OFF:
216 case HWTSTAMP_TX_ONESTEP_SYNC:
222 case HWTSTAMP_FILTER_NONE:
223 case HWTSTAMP_FILTER_ALL:
224 case HWTSTAMP_FILTER_SOME:
225 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
226 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
227 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
228 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
229 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
230 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
231 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
232 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
233 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
234 case HWTSTAMP_FILTER_PTP_V2_EVENT:
235 case HWTSTAMP_FILTER_PTP_V2_SYNC:
236 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
241 if (!tx_type_valid || !rx_filter_valid)
248 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
250 static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
253 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
254 const struct net_device_ops *ops;
259 ops = dev->netdev_ops;
262 case SIOCSIFFLAGS: /* Set interface flags */
263 return dev_change_flags(dev, ifr->ifr_flags);
265 case SIOCSIFMETRIC: /* Set the metric on the interface
266 (currently unused) */
269 case SIOCSIFMTU: /* Set the MTU of a device */
270 return dev_set_mtu(dev, ifr->ifr_mtu);
273 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
275 case SIOCSIFHWBROADCAST:
276 if (ifr->ifr_hwaddr.sa_family != dev->type)
278 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
279 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
280 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
284 if (ops->ndo_set_config) {
285 if (!netif_device_present(dev))
287 return ops->ndo_set_config(dev, &ifr->ifr_map);
292 if (!ops->ndo_set_rx_mode ||
293 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
295 if (!netif_device_present(dev))
297 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
300 if (!ops->ndo_set_rx_mode ||
301 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
303 if (!netif_device_present(dev))
305 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
308 if (ifr->ifr_qlen < 0)
310 dev->tx_queue_len = ifr->ifr_qlen;
314 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
315 return dev_change_name(dev, ifr->ifr_newname);
318 err = net_hwtstamp_validate(ifr);
324 * Unknown or private ioctl
327 if ((cmd >= SIOCDEVPRIVATE &&
328 cmd <= SIOCDEVPRIVATE + 15) ||
329 cmd == SIOCBONDENSLAVE ||
330 cmd == SIOCBONDRELEASE ||
331 cmd == SIOCBONDSETHWADDR ||
332 cmd == SIOCBONDSLAVEINFOQUERY ||
333 cmd == SIOCBONDINFOQUERY ||
334 cmd == SIOCBONDCHANGEACTIVE ||
335 cmd == SIOCGMIIPHY ||
336 cmd == SIOCGMIIREG ||
337 cmd == SIOCSMIIREG ||
338 cmd == SIOCBRADDIF ||
339 cmd == SIOCBRDELIF ||
340 cmd == SIOCSHWTSTAMP ||
343 if (ops->ndo_do_ioctl) {
344 if (netif_device_present(dev))
345 err = ops->ndo_do_ioctl(dev, ifr, cmd);
357 * dev_load - load a network module
358 * @net: the applicable net namespace
359 * @name: name of interface
361 * If a network interface is not present and the process has suitable
362 * privileges this function loads the module. If module loading is not
363 * available in this kernel then it becomes a nop.
366 void dev_load(struct net *net, const char *name)
368 struct net_device *dev;
372 dev = dev_get_by_name_rcu(net, name);
376 if (no_module && capable(CAP_NET_ADMIN))
377 no_module = request_module("netdev-%s", name);
378 if (no_module && capable(CAP_SYS_MODULE)) {
379 if (!request_module("%s", name))
380 pr_warn("Loading kernel module for a network device with CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias netdev-%s instead.\n",
384 EXPORT_SYMBOL(dev_load);
387 * This function handles all "interface"-type I/O control requests. The actual
388 * 'doing' part of this is dev_ifsioc above.
392 * dev_ioctl - network device ioctl
393 * @net: the applicable net namespace
394 * @cmd: command to issue
395 * @arg: pointer to a struct ifreq in user space
397 * Issue ioctl functions to devices. This is normally called by the
398 * user space syscall interfaces but can sometimes be useful for
399 * other purposes. The return value is the return from the syscall if
400 * positive or a negative errno code on error.
403 int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
409 /* One special case: SIOCGIFCONF takes ifconf argument
410 and requires shared lock, because it sleeps writing
414 if (cmd == SIOCGIFCONF) {
416 ret = dev_ifconf(net, (char __user *) arg);
420 if (cmd == SIOCGIFNAME)
421 return dev_ifname(net, (struct ifreq __user *)arg);
423 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
426 ifr.ifr_name[IFNAMSIZ-1] = 0;
428 colon = strchr(ifr.ifr_name, ':');
433 * See which interface the caller is talking about.
439 * - can be done by all.
440 * - atomic and do not require locking.
451 dev_load(net, ifr.ifr_name);
453 ret = dev_ifsioc_locked(net, &ifr, cmd);
458 if (copy_to_user(arg, &ifr,
459 sizeof(struct ifreq)))
465 dev_load(net, ifr.ifr_name);
467 ret = dev_ethtool(net, &ifr);
472 if (copy_to_user(arg, &ifr,
473 sizeof(struct ifreq)))
480 * - require superuser power.
481 * - require strict serialization.
487 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
489 dev_load(net, ifr.ifr_name);
491 ret = dev_ifsioc(net, &ifr, cmd);
496 if (copy_to_user(arg, &ifr,
497 sizeof(struct ifreq)))
504 * - require superuser power.
505 * - require strict serialization.
506 * - do not return a value
510 if (!capable(CAP_NET_ADMIN))
515 * - require local superuser power.
516 * - require strict serialization.
517 * - do not return a value
526 case SIOCSIFHWBROADCAST:
528 case SIOCBONDENSLAVE:
529 case SIOCBONDRELEASE:
530 case SIOCBONDSETHWADDR:
531 case SIOCBONDCHANGEACTIVE:
535 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
538 case SIOCBONDSLAVEINFOQUERY:
539 case SIOCBONDINFOQUERY:
540 dev_load(net, ifr.ifr_name);
542 ret = dev_ifsioc(net, &ifr, cmd);
547 /* Get the per device memory space. We can add this but
548 * currently do not support it */
550 /* Set the per device memory buffer space.
551 * Not applicable in our case */
556 * Unknown or private ioctl.
559 if (cmd == SIOCWANDEV ||
560 (cmd >= SIOCDEVPRIVATE &&
561 cmd <= SIOCDEVPRIVATE + 15)) {
562 dev_load(net, ifr.ifr_name);
564 ret = dev_ifsioc(net, &ifr, cmd);
566 if (!ret && copy_to_user(arg, &ifr,
567 sizeof(struct ifreq)))
571 /* Take care of Wireless Extensions */
572 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
573 return wext_handle_ioctl(net, &ifr, cmd, arg);