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 static int dev_ifconf(struct net
*net
, char __user
*arg
)
72 struct net_device
*dev
;
79 * Fetch the caller's info block.
82 if (copy_from_user(&ifc
, arg
, sizeof(struct ifconf
)))
89 * Loop over the interfaces, and write an info block for each.
93 for_each_netdev(net
, dev
) {
94 for (i
= 0; i
< NPROTO
; i
++) {
95 if (gifconf_list
[i
]) {
98 done
= gifconf_list
[i
](dev
, NULL
, 0);
100 done
= gifconf_list
[i
](dev
, pos
+ total
,
110 * All done. Write the updated control block back to the caller.
115 * Both BSD and Solaris return 0 here, so we do too.
117 return copy_to_user(arg
, &ifc
, sizeof(struct ifconf
)) ? -EFAULT
: 0;
121 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
123 static int dev_ifsioc_locked(struct net
*net
, struct ifreq
*ifr
, unsigned int cmd
)
126 struct net_device
*dev
= dev_get_by_name_rcu(net
, ifr
->ifr_name
);
132 case SIOCGIFFLAGS
: /* Get interface flags */
133 ifr
->ifr_flags
= (short) dev_get_flags(dev
);
136 case SIOCGIFMETRIC
: /* Get the metric on the interface
137 (currently unused) */
141 case SIOCGIFMTU
: /* Get the MTU of a device */
142 ifr
->ifr_mtu
= dev
->mtu
;
147 memset(ifr
->ifr_hwaddr
.sa_data
, 0,
148 sizeof(ifr
->ifr_hwaddr
.sa_data
));
150 memcpy(ifr
->ifr_hwaddr
.sa_data
, dev
->dev_addr
,
151 min(sizeof(ifr
->ifr_hwaddr
.sa_data
),
152 (size_t)dev
->addr_len
));
153 ifr
->ifr_hwaddr
.sa_family
= dev
->type
;
161 ifr
->ifr_map
.mem_start
= dev
->mem_start
;
162 ifr
->ifr_map
.mem_end
= dev
->mem_end
;
163 ifr
->ifr_map
.base_addr
= dev
->base_addr
;
164 ifr
->ifr_map
.irq
= dev
->irq
;
165 ifr
->ifr_map
.dma
= dev
->dma
;
166 ifr
->ifr_map
.port
= dev
->if_port
;
170 ifr
->ifr_ifindex
= dev
->ifindex
;
174 ifr
->ifr_qlen
= dev
->tx_queue_len
;
178 /* dev_ioctl() should ensure this case
189 static int net_hwtstamp_validate(struct ifreq
*ifr
)
191 struct hwtstamp_config cfg
;
192 enum hwtstamp_tx_types tx_type
;
193 enum hwtstamp_rx_filters rx_filter
;
194 int tx_type_valid
= 0;
195 int rx_filter_valid
= 0;
197 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
200 if (cfg
.flags
) /* reserved for future extensions */
203 tx_type
= cfg
.tx_type
;
204 rx_filter
= cfg
.rx_filter
;
207 case HWTSTAMP_TX_OFF
:
209 case HWTSTAMP_TX_ONESTEP_SYNC
:
215 case HWTSTAMP_FILTER_NONE
:
216 case HWTSTAMP_FILTER_ALL
:
217 case HWTSTAMP_FILTER_SOME
:
218 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
219 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
220 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
221 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
222 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
223 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
224 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
225 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
226 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
227 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
228 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
229 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
230 case HWTSTAMP_FILTER_NTP_ALL
:
235 if (!tx_type_valid
|| !rx_filter_valid
)
242 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
244 static int dev_ifsioc(struct net
*net
, struct ifreq
*ifr
, unsigned int cmd
)
247 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
248 const struct net_device_ops
*ops
;
253 ops
= dev
->netdev_ops
;
256 case SIOCSIFFLAGS
: /* Set interface flags */
257 return dev_change_flags(dev
, ifr
->ifr_flags
);
259 case SIOCSIFMETRIC
: /* Set the metric on the interface
260 (currently unused) */
263 case SIOCSIFMTU
: /* Set the MTU of a device */
264 return dev_set_mtu(dev
, ifr
->ifr_mtu
);
267 if (dev
->addr_len
> sizeof(struct sockaddr
))
269 return dev_set_mac_address(dev
, &ifr
->ifr_hwaddr
);
271 case SIOCSIFHWBROADCAST
:
272 if (ifr
->ifr_hwaddr
.sa_family
!= dev
->type
)
274 memcpy(dev
->broadcast
, ifr
->ifr_hwaddr
.sa_data
,
275 min(sizeof(ifr
->ifr_hwaddr
.sa_data
),
276 (size_t)dev
->addr_len
));
277 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
281 if (ops
->ndo_set_config
) {
282 if (!netif_device_present(dev
))
284 return ops
->ndo_set_config(dev
, &ifr
->ifr_map
);
289 if (!ops
->ndo_set_rx_mode
||
290 ifr
->ifr_hwaddr
.sa_family
!= AF_UNSPEC
)
292 if (!netif_device_present(dev
))
294 return dev_mc_add_global(dev
, ifr
->ifr_hwaddr
.sa_data
);
297 if (!ops
->ndo_set_rx_mode
||
298 ifr
->ifr_hwaddr
.sa_family
!= AF_UNSPEC
)
300 if (!netif_device_present(dev
))
302 return dev_mc_del_global(dev
, ifr
->ifr_hwaddr
.sa_data
);
305 if (ifr
->ifr_qlen
< 0)
307 if (dev
->tx_queue_len
^ ifr
->ifr_qlen
) {
308 unsigned int orig_len
= dev
->tx_queue_len
;
310 dev
->tx_queue_len
= ifr
->ifr_qlen
;
311 err
= call_netdevice_notifiers(
312 NETDEV_CHANGE_TX_QUEUE_LEN
, dev
);
313 err
= notifier_to_errno(err
);
315 dev
->tx_queue_len
= orig_len
;
322 ifr
->ifr_newname
[IFNAMSIZ
-1] = '\0';
323 return dev_change_name(dev
, ifr
->ifr_newname
);
326 err
= net_hwtstamp_validate(ifr
);
332 * Unknown or private ioctl
335 if ((cmd
>= SIOCDEVPRIVATE
&&
336 cmd
<= SIOCDEVPRIVATE
+ 15) ||
337 cmd
== SIOCBONDENSLAVE
||
338 cmd
== SIOCBONDRELEASE
||
339 cmd
== SIOCBONDSETHWADDR
||
340 cmd
== SIOCBONDSLAVEINFOQUERY
||
341 cmd
== SIOCBONDINFOQUERY
||
342 cmd
== SIOCBONDCHANGEACTIVE
||
343 cmd
== SIOCGMIIPHY
||
344 cmd
== SIOCGMIIREG
||
345 cmd
== SIOCSMIIREG
||
346 cmd
== SIOCBRADDIF
||
347 cmd
== SIOCBRDELIF
||
348 cmd
== SIOCSHWTSTAMP
||
349 cmd
== SIOCGHWTSTAMP
||
352 if (ops
->ndo_do_ioctl
) {
353 if (netif_device_present(dev
))
354 err
= ops
->ndo_do_ioctl(dev
, ifr
, cmd
);
366 * dev_load - load a network module
367 * @net: the applicable net namespace
368 * @name: name of interface
370 * If a network interface is not present and the process has suitable
371 * privileges this function loads the module. If module loading is not
372 * available in this kernel then it becomes a nop.
375 void dev_load(struct net
*net
, const char *name
)
377 struct net_device
*dev
;
381 dev
= dev_get_by_name_rcu(net
, name
);
385 if (no_module
&& capable(CAP_NET_ADMIN
))
386 no_module
= request_module("netdev-%s", name
);
387 if (no_module
&& capable(CAP_SYS_MODULE
))
388 request_module("%s", name
);
390 EXPORT_SYMBOL(dev_load
);
393 * This function handles all "interface"-type I/O control requests. The actual
394 * 'doing' part of this is dev_ifsioc above.
398 * dev_ioctl - network device ioctl
399 * @net: the applicable net namespace
400 * @cmd: command to issue
401 * @arg: pointer to a struct ifreq in user space
403 * Issue ioctl functions to devices. This is normally called by the
404 * user space syscall interfaces but can sometimes be useful for
405 * other purposes. The return value is the return from the syscall if
406 * positive or a negative errno code on error.
409 int dev_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
415 /* One special case: SIOCGIFCONF takes ifconf argument
416 and requires shared lock, because it sleeps writing
420 if (cmd
== SIOCGIFCONF
) {
422 ret
= dev_ifconf(net
, (char __user
*) arg
);
426 if (cmd
== SIOCGIFNAME
)
427 return dev_ifname(net
, (struct ifreq __user
*)arg
);
430 * Take care of Wireless Extensions. Unfortunately struct iwreq
431 * isn't a proper subset of struct ifreq (it's 8 byte shorter)
432 * so we need to treat it specially, otherwise applications may
433 * fault if the struct they're passing happens to land at the
434 * end of a mapped page.
436 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
439 if (copy_from_user(&iwr
, arg
, sizeof(iwr
)))
442 iwr
.ifr_name
[sizeof(iwr
.ifr_name
) - 1] = 0;
444 return wext_handle_ioctl(net
, &iwr
, cmd
, arg
);
447 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
450 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
452 colon
= strchr(ifr
.ifr_name
, ':');
457 * See which interface the caller is talking about.
463 * - can be done by all.
464 * - atomic and do not require locking.
475 dev_load(net
, ifr
.ifr_name
);
477 ret
= dev_ifsioc_locked(net
, &ifr
, cmd
);
482 if (copy_to_user(arg
, &ifr
,
483 sizeof(struct ifreq
)))
489 dev_load(net
, ifr
.ifr_name
);
491 ret
= dev_ethtool(net
, &ifr
);
496 if (copy_to_user(arg
, &ifr
,
497 sizeof(struct ifreq
)))
504 * - require superuser power.
505 * - require strict serialization.
511 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
513 dev_load(net
, ifr
.ifr_name
);
515 ret
= dev_ifsioc(net
, &ifr
, cmd
);
520 if (copy_to_user(arg
, &ifr
,
521 sizeof(struct ifreq
)))
528 * - require superuser power.
529 * - require strict serialization.
530 * - do not return a value
534 if (!capable(CAP_NET_ADMIN
))
539 * - require local superuser power.
540 * - require strict serialization.
541 * - do not return a value
550 case SIOCSIFHWBROADCAST
:
552 case SIOCBONDENSLAVE
:
553 case SIOCBONDRELEASE
:
554 case SIOCBONDSETHWADDR
:
555 case SIOCBONDCHANGEACTIVE
:
559 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
562 case SIOCBONDSLAVEINFOQUERY
:
563 case SIOCBONDINFOQUERY
:
564 dev_load(net
, ifr
.ifr_name
);
566 ret
= dev_ifsioc(net
, &ifr
, cmd
);
571 /* Get the per device memory space. We can add this but
572 * currently do not support it */
574 /* Set the per device memory buffer space.
575 * Not applicable in our case */
580 * Unknown or private ioctl.
583 if (cmd
== SIOCWANDEV
||
584 cmd
== SIOCGHWTSTAMP
||
585 (cmd
>= SIOCDEVPRIVATE
&&
586 cmd
<= SIOCDEVPRIVATE
+ 15)) {
587 dev_load(net
, ifr
.ifr_name
);
589 ret
= dev_ifsioc(net
, &ifr
, cmd
);
591 if (!ret
&& copy_to_user(arg
, &ifr
,
592 sizeof(struct ifreq
)))