License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-02-16 06:20:46 +08:00
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/netdevice.h>
|
2021-07-22 22:29:01 +08:00
|
|
|
#include <linux/inetdevice.h>
|
2013-02-16 06:20:46 +08:00
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <linux/net_tstamp.h>
|
2023-08-01 22:28:24 +08:00
|
|
|
#include <linux/phylib_stubs.h>
|
2013-02-16 06:20:46 +08:00
|
|
|
#include <linux/wireless.h>
|
2021-07-27 21:45:16 +08:00
|
|
|
#include <linux/if_bridge.h>
|
net: dsa: replace NETDEV_PRE_CHANGE_HWTSTAMP notifier with a stub
There was a sort of rush surrounding commit 88c0a6b503b7 ("net: create a
netdev notifier for DSA to reject PTP on DSA master"), due to a desire
to convert DSA's attempt to deny TX timestamping on a DSA master to
something that doesn't block the kernel-wide API conversion from
ndo_eth_ioctl() to ndo_hwtstamp_set().
What was required was a mechanism that did not depend on ndo_eth_ioctl(),
and what was provided was a mechanism that did not depend on
ndo_eth_ioctl(), while at the same time introducing something that
wasn't absolutely necessary - a new netdev notifier.
There have been objections from Jakub Kicinski that using notifiers in
general when they are not absolutely necessary creates complications to
the control flow and difficulties to maintainers who look at the code.
So there is a desire to not use notifiers.
In addition to that, the notifier chain gets called even if there is no
DSA in the system and no one is interested in applying any restriction.
Take the model of udp_tunnel_nic_ops and introduce a stub mechanism,
through which net/core/dev_ioctl.c can call into DSA even when
CONFIG_NET_DSA=m.
Compared to the code that existed prior to the notifier conversion, aka
what was added in commits:
- 4cfab3566710 ("net: dsa: Add wrappers for overloaded ndo_ops")
- 3369afba1e46 ("net: Call into DSA netdevice_ops wrappers")
this is different because we are not overloading any struct
net_device_ops of the DSA master anymore, but rather, we are exposing a
rather specific functionality which is orthogonal to which API is used
to enable it - ndo_eth_ioctl() or ndo_hwtstamp_set().
Also, what is similar is that both approaches use function pointers to
get from built-in code to DSA.
There is no point in replicating the function pointers towards
__dsa_master_hwtstamp_validate() once for every CPU port (dev->dsa_ptr).
Instead, it is sufficient to introduce a singleton struct dsa_stubs,
built into the kernel, which contains a single function pointer to
__dsa_master_hwtstamp_validate().
I find this approach preferable to what we had originally, because
dev->dsa_ptr->netdev_ops->ndo_do_ioctl() used to require going through
struct dsa_port (dev->dsa_ptr), and so, this was incompatible with any
attempts to add any data encapsulation and hide DSA data structures from
the outside world.
Link: https://lore.kernel.org/netdev/20230403083019.120b72fd@kernel.org/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-06 19:42:46 +08:00
|
|
|
#include <net/dsa_stubs.h>
|
2013-02-16 06:20:46 +08:00
|
|
|
#include <net/wext.h>
|
|
|
|
|
2022-04-07 05:37:54 +08:00
|
|
|
#include "dev.h"
|
|
|
|
|
2013-02-16 06:20:46 +08:00
|
|
|
/*
|
|
|
|
* Map an interface index to its name (SIOCGIFNAME)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need this ioctl for efficient implementation of the
|
|
|
|
* if_indextoname() function required by the IPv6 API. Without
|
|
|
|
* it, we would have to search all the interfaces to find a
|
|
|
|
* match. --pb
|
|
|
|
*/
|
|
|
|
|
2017-10-06 00:59:44 +08:00
|
|
|
static int dev_ifname(struct net *net, struct ifreq *ifr)
|
2013-02-16 06:20:46 +08:00
|
|
|
{
|
2017-10-06 00:59:44 +08:00
|
|
|
ifr->ifr_name[IFNAMSIZ-1] = 0;
|
|
|
|
return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a SIOCGIFCONF call. This structure will change
|
|
|
|
* size eventually, and there is nothing I can do about it.
|
|
|
|
* Thus we will need a 'compatibility mode'.
|
|
|
|
*/
|
2021-07-22 22:29:02 +08:00
|
|
|
int dev_ifconf(struct net *net, struct ifconf __user *uifc)
|
2013-02-16 06:20:46 +08:00
|
|
|
{
|
|
|
|
struct net_device *dev;
|
2021-07-22 22:29:02 +08:00
|
|
|
void __user *pos;
|
|
|
|
size_t size;
|
|
|
|
int len, total = 0, done;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2021-07-22 22:29:02 +08:00
|
|
|
/* both the ifconf and the ifreq structures are slightly different */
|
|
|
|
if (in_compat_syscall()) {
|
|
|
|
struct compat_ifconf ifc32;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2021-07-22 22:29:02 +08:00
|
|
|
if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
|
|
|
|
return -EFAULT;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2021-07-22 22:29:02 +08:00
|
|
|
pos = compat_ptr(ifc32.ifcbuf);
|
|
|
|
len = ifc32.ifc_len;
|
|
|
|
size = sizeof(struct compat_ifreq);
|
|
|
|
} else {
|
|
|
|
struct ifconf ifc;
|
|
|
|
|
|
|
|
if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
|
|
|
|
return -EFAULT;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2021-07-22 22:29:02 +08:00
|
|
|
pos = ifc.ifc_buf;
|
|
|
|
len = ifc.ifc_len;
|
|
|
|
size = sizeof(struct ifreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop over the interfaces, and write an info block for each. */
|
|
|
|
rtnl_lock();
|
2013-02-16 06:20:46 +08:00
|
|
|
for_each_netdev(net, dev) {
|
2021-07-22 22:29:01 +08:00
|
|
|
if (!pos)
|
|
|
|
done = inet_gifconf(dev, NULL, 0, size);
|
|
|
|
else
|
|
|
|
done = inet_gifconf(dev, pos + total,
|
|
|
|
len - total, size);
|
2021-07-22 22:29:02 +08:00
|
|
|
if (done < 0) {
|
|
|
|
rtnl_unlock();
|
2021-07-22 22:29:01 +08:00
|
|
|
return -EFAULT;
|
2021-07-22 22:29:02 +08:00
|
|
|
}
|
2021-07-22 22:29:01 +08:00
|
|
|
total += done;
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
2021-07-22 22:29:02 +08:00
|
|
|
rtnl_unlock();
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2021-07-22 22:29:02 +08:00
|
|
|
return put_user(total, &uifc->ifc_len);
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 22:29:00 +08:00
|
|
|
static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
|
|
|
|
{
|
|
|
|
struct ifmap *ifmap = &ifr->ifr_map;
|
|
|
|
|
|
|
|
if (in_compat_syscall()) {
|
|
|
|
struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
|
|
|
|
|
|
|
|
cifmap->mem_start = dev->mem_start;
|
|
|
|
cifmap->mem_end = dev->mem_end;
|
|
|
|
cifmap->base_addr = dev->base_addr;
|
|
|
|
cifmap->irq = dev->irq;
|
|
|
|
cifmap->dma = dev->dma;
|
|
|
|
cifmap->port = dev->if_port;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifmap->mem_start = dev->mem_start;
|
|
|
|
ifmap->mem_end = dev->mem_end;
|
|
|
|
ifmap->base_addr = dev->base_addr;
|
|
|
|
ifmap->irq = dev->irq;
|
|
|
|
ifmap->dma = dev->dma;
|
|
|
|
ifmap->port = dev->if_port;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dev_setifmap(struct net_device *dev, struct ifreq *ifr)
|
|
|
|
{
|
|
|
|
struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
|
|
|
|
|
|
|
|
if (!dev->netdev_ops->ndo_set_config)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
if (in_compat_syscall()) {
|
|
|
|
struct ifmap ifmap = {
|
|
|
|
.mem_start = cifmap->mem_start,
|
|
|
|
.mem_end = cifmap->mem_end,
|
|
|
|
.base_addr = cifmap->base_addr,
|
|
|
|
.irq = cifmap->irq,
|
|
|
|
.dma = cifmap->dma,
|
|
|
|
.port = cifmap->port,
|
|
|
|
};
|
|
|
|
|
|
|
|
return dev->netdev_ops->ndo_set_config(dev, &ifmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
|
|
|
|
}
|
|
|
|
|
2013-02-16 06:20:46 +08:00
|
|
|
/*
|
|
|
|
* Perform the SIOCxIFxxx calls, inside rcu_read_lock()
|
|
|
|
*/
|
|
|
|
static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCGIFFLAGS: /* Get interface flags */
|
|
|
|
ifr->ifr_flags = (short) dev_get_flags(dev);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCGIFMETRIC: /* Get the metric on the interface
|
|
|
|
(currently unused) */
|
|
|
|
ifr->ifr_metric = 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCGIFMTU: /* Get the MTU of a device */
|
|
|
|
ifr->ifr_mtu = dev->mtu;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCGIFSLAVE:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCGIFMAP:
|
2021-07-22 22:29:00 +08:00
|
|
|
return dev_getifmap(dev, ifr);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
case SIOCGIFINDEX:
|
|
|
|
ifr->ifr_ifindex = dev->ifindex;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCGIFTXQLEN:
|
|
|
|
ifr->ifr_qlen = dev->tx_queue_len;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* dev_ioctl() should ensure this case
|
|
|
|
* is never reached
|
|
|
|
*/
|
|
|
|
WARN_ON(1);
|
|
|
|
err = -ENOTTY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it
Jakub Kicinski suggested that we may want to add new UAPI for
controlling hardware timestamping through netlink in the future, and in
that case, we will be limited to the struct hwtstamp_config that is
currently passed in fixed binary format through the SIOCGHWTSTAMP and
SIOCSHWTSTAMP ioctls. It would be good if new kernel code already
started operating on an extensible kernel variant of that structure,
similar in concept to struct kernel_ethtool_coalesce vs struct
ethtool_coalesce.
Since struct hwtstamp_config is in include/uapi/linux/net_tstamp.h, here
we introduce include/linux/net_tstamp.h which shadows that other header,
but also includes it, so that existing includers of this header work as
before. In addition to that, we add the definition for the kernel-only
structure, and a helper which translates all fields by manual copying.
I am doing a manual copy in order to not force the alignment (or type)
of the fields of struct kernel_hwtstamp_config to be the same as of
struct hwtstamp_config, even though now, they are the same.
Link: https://lore.kernel.org/netdev/20230330223519.36ce7d23@kernel.org/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:53 +08:00
|
|
|
static int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
|
2013-02-16 06:20:46 +08:00
|
|
|
{
|
|
|
|
enum hwtstamp_tx_types tx_type;
|
|
|
|
enum hwtstamp_rx_filters rx_filter;
|
|
|
|
int tx_type_valid = 0;
|
|
|
|
int rx_filter_valid = 0;
|
|
|
|
|
2023-04-02 20:37:52 +08:00
|
|
|
if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
|
2013-02-16 06:20:46 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2023-04-02 20:37:52 +08:00
|
|
|
tx_type = cfg->tx_type;
|
|
|
|
rx_filter = cfg->rx_filter;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
switch (tx_type) {
|
|
|
|
case HWTSTAMP_TX_OFF:
|
|
|
|
case HWTSTAMP_TX_ON:
|
|
|
|
case HWTSTAMP_TX_ONESTEP_SYNC:
|
2019-12-26 10:16:19 +08:00
|
|
|
case HWTSTAMP_TX_ONESTEP_P2P:
|
2013-02-16 06:20:46 +08:00
|
|
|
tx_type_valid = 1;
|
|
|
|
break;
|
2020-03-28 07:01:53 +08:00
|
|
|
case __HWTSTAMP_TX_CNT:
|
|
|
|
/* not a real value */
|
|
|
|
break;
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (rx_filter) {
|
|
|
|
case HWTSTAMP_FILTER_NONE:
|
|
|
|
case HWTSTAMP_FILTER_ALL:
|
|
|
|
case HWTSTAMP_FILTER_SOME:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_EVENT:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_SYNC:
|
|
|
|
case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
|
2017-05-19 23:52:35 +08:00
|
|
|
case HWTSTAMP_FILTER_NTP_ALL:
|
2017-05-19 23:52:36 +08:00
|
|
|
rx_filter_valid = 1;
|
2017-05-19 23:52:35 +08:00
|
|
|
break;
|
2020-03-28 07:01:53 +08:00
|
|
|
case __HWTSTAMP_FILTER_CNT:
|
|
|
|
/* not a real value */
|
|
|
|
break;
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tx_type_valid || !rx_filter_valid)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:45:13 +08:00
|
|
|
static int dev_eth_ioctl(struct net_device *dev,
|
|
|
|
struct ifreq *ifr, unsigned int cmd)
|
2020-07-20 11:49:51 +08:00
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
2023-04-02 20:37:51 +08:00
|
|
|
|
|
|
|
if (!ops->ndo_eth_ioctl)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return ops->ndo_eth_ioctl(dev, ifr, cmd);
|
|
|
|
}
|
|
|
|
|
2023-08-01 22:28:24 +08:00
|
|
|
/**
|
|
|
|
* dev_get_hwtstamp_phylib() - Get hardware timestamping settings of NIC
|
|
|
|
* or of attached phylib PHY
|
|
|
|
* @dev: Network device
|
|
|
|
* @cfg: Timestamping configuration structure
|
|
|
|
*
|
2023-11-19 10:38:05 +08:00
|
|
|
* Helper for enforcing a common policy that phylib timestamping, if available,
|
|
|
|
* should take precedence in front of hardware timestamping provided by the
|
|
|
|
* netdev.
|
2023-08-01 22:28:24 +08:00
|
|
|
*
|
|
|
|
* Note: phy_mii_ioctl() only handles SIOCSHWTSTAMP (not SIOCGHWTSTAMP), and
|
|
|
|
* there only exists a phydev->mii_ts->hwtstamp() method. So this will return
|
|
|
|
* -EOPNOTSUPP for phylib for now, which is still more accurate than letting
|
|
|
|
* the netdev handle the GET request.
|
|
|
|
*/
|
|
|
|
static int dev_get_hwtstamp_phylib(struct net_device *dev,
|
|
|
|
struct kernel_hwtstamp_config *cfg)
|
|
|
|
{
|
2023-11-19 10:38:05 +08:00
|
|
|
if (phy_has_hwtstamp(dev->phydev))
|
2023-08-01 22:28:24 +08:00
|
|
|
return phy_hwtstamp_get(dev->phydev, cfg);
|
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
|
2023-08-01 22:28:24 +08:00
|
|
|
}
|
|
|
|
|
2023-04-02 20:37:51 +08:00
|
|
|
static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
|
|
|
|
{
|
2023-08-01 22:28:13 +08:00
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
struct kernel_hwtstamp_config kernel_cfg = {};
|
|
|
|
struct hwtstamp_config cfg;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!ops->ndo_hwtstamp_get)
|
|
|
|
return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); /* legacy */
|
|
|
|
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2023-08-01 22:28:14 +08:00
|
|
|
kernel_cfg.ifr = ifr;
|
2023-08-01 22:28:24 +08:00
|
|
|
err = dev_get_hwtstamp_phylib(dev, &kernel_cfg);
|
2023-08-01 22:28:13 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2023-08-01 22:28:14 +08:00
|
|
|
/* If the request was resolved through an unconverted driver, omit
|
|
|
|
* the copy_to_user(), since the implementation has already done that
|
|
|
|
*/
|
|
|
|
if (!kernel_cfg.copied_to_user) {
|
|
|
|
hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
|
2023-08-01 22:28:13 +08:00
|
|
|
|
2023-08-01 22:28:14 +08:00
|
|
|
if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2023-08-01 22:28:13 +08:00
|
|
|
|
|
|
|
return 0;
|
2023-04-02 20:37:51 +08:00
|
|
|
}
|
|
|
|
|
2023-08-01 22:28:24 +08:00
|
|
|
/**
|
|
|
|
* dev_set_hwtstamp_phylib() - Change hardware timestamping of NIC
|
|
|
|
* or of attached phylib PHY
|
|
|
|
* @dev: Network device
|
|
|
|
* @cfg: Timestamping configuration structure
|
|
|
|
* @extack: Netlink extended ack message structure, for error reporting
|
|
|
|
*
|
2023-11-19 10:38:05 +08:00
|
|
|
* Helper for enforcing a common policy that phylib timestamping, if available,
|
|
|
|
* should take precedence in front of hardware timestamping provided by the
|
|
|
|
* netdev. If the netdev driver needs to perform specific actions even for PHY
|
2023-08-01 22:28:24 +08:00
|
|
|
* timestamping to work properly (a switch port must trap the timestamped
|
|
|
|
* frames and not forward them), it must set IFF_SEE_ALL_HWTSTAMP_REQUESTS in
|
|
|
|
* dev->priv_flags.
|
|
|
|
*/
|
2023-11-14 19:28:33 +08:00
|
|
|
int dev_set_hwtstamp_phylib(struct net_device *dev,
|
|
|
|
struct kernel_hwtstamp_config *cfg,
|
|
|
|
struct netlink_ext_ack *extack)
|
2023-08-01 22:28:24 +08:00
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
2023-11-19 10:38:05 +08:00
|
|
|
bool phy_ts = phy_has_hwtstamp(dev->phydev);
|
2023-08-01 22:28:24 +08:00
|
|
|
struct kernel_hwtstamp_config old_cfg = {};
|
|
|
|
bool changed = false;
|
|
|
|
int err;
|
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
cfg->source = phy_ts ? HWTSTAMP_SOURCE_PHYLIB : HWTSTAMP_SOURCE_NETDEV;
|
2023-08-01 22:28:24 +08:00
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
if (phy_ts && (dev->priv_flags & IFF_SEE_ALL_HWTSTAMP_REQUESTS)) {
|
2023-08-01 22:28:24 +08:00
|
|
|
err = ops->ndo_hwtstamp_get(dev, &old_cfg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
net: omit ndo_hwtstamp_get() call when possible in dev_set_hwtstamp_phylib()
Setting dev->priv_flags & IFF_SEE_ALL_HWTSTAMP_REQUESTS is only legal
for drivers which were converted to ndo_hwtstamp_get() and
ndo_hwtstamp_set(), and it is only there that we call ndo_hwtstamp_set()
for a request that otherwise goes to phylib (for stuff like packet traps,
which need to be undone if phylib failed, hence the old_cfg logic).
The problem is that we end up calling ndo_hwtstamp_get() when we don't
need to (even if the SIOCSHWTSTAMP wasn't intended for phylib, or if it
was, but the driver didn't set IFF_SEE_ALL_HWTSTAMP_REQUESTS). For those
unnecessary conditions, we share a code path with virtual drivers (vlan,
macvlan, bonding) where ndo_hwtstamp_get() is implemented as
generic_hwtstamp_get_lower(), and may be resolved through
generic_hwtstamp_ioctl_lower() if the lower device is unconverted.
I.e. this situation:
$ ip link add link eno0 name eno0.100 type vlan id 100
$ hwstamp_ctl -i eno0.100 -t 1
We are unprepared to deal with this, because if ndo_hwtstamp_get() is
resolved through a legacy ndo_eth_ioctl(SIOCGHWTSTAMP) lower_dev
implementation, that needs a non-NULL old_cfg.ifr pointer, and we don't
have it.
But we don't even need to deal with it either. In the general case,
drivers may not even implement SIOCGHWTSTAMP handling, only SIOCSHWTSTAMP,
so it makes sense to completely avoid a SIOCGHWTSTAMP call if we can.
The solution is to split the single "if" condition into 3 smaller ones,
thus separating the decision to call ndo_hwtstamp_get() from the
decision to call ndo_hwtstamp_set(). The third "if" condition is
identical to the first one, and both are subsets of the second one.
Thus, the "cfg" argument of kernel_hwtstamp_config_changed() is always
valid.
Reported-by: Eric Dumazet <edumazet@google.com>
Closes: https://lore.kernel.org/netdev/CANn89iLOspJsvjPj+y8jikg7erXDomWe8sqHMdfL_2LQSFrPAg@mail.gmail.com/
Fixes: fd770e856e22 ("net: remove phy_has_hwtstamp() -> phy_mii_ioctl() decision from converted drivers")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-08-04 21:49:39 +08:00
|
|
|
}
|
2023-08-01 22:28:24 +08:00
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
if (!phy_ts || (dev->priv_flags & IFF_SEE_ALL_HWTSTAMP_REQUESTS)) {
|
2023-08-01 22:28:24 +08:00
|
|
|
err = ops->ndo_hwtstamp_set(dev, cfg, extack);
|
|
|
|
if (err) {
|
|
|
|
if (extack->_msg)
|
|
|
|
netdev_err(dev, "%s\n", extack->_msg);
|
|
|
|
return err;
|
|
|
|
}
|
net: omit ndo_hwtstamp_get() call when possible in dev_set_hwtstamp_phylib()
Setting dev->priv_flags & IFF_SEE_ALL_HWTSTAMP_REQUESTS is only legal
for drivers which were converted to ndo_hwtstamp_get() and
ndo_hwtstamp_set(), and it is only there that we call ndo_hwtstamp_set()
for a request that otherwise goes to phylib (for stuff like packet traps,
which need to be undone if phylib failed, hence the old_cfg logic).
The problem is that we end up calling ndo_hwtstamp_get() when we don't
need to (even if the SIOCSHWTSTAMP wasn't intended for phylib, or if it
was, but the driver didn't set IFF_SEE_ALL_HWTSTAMP_REQUESTS). For those
unnecessary conditions, we share a code path with virtual drivers (vlan,
macvlan, bonding) where ndo_hwtstamp_get() is implemented as
generic_hwtstamp_get_lower(), and may be resolved through
generic_hwtstamp_ioctl_lower() if the lower device is unconverted.
I.e. this situation:
$ ip link add link eno0 name eno0.100 type vlan id 100
$ hwstamp_ctl -i eno0.100 -t 1
We are unprepared to deal with this, because if ndo_hwtstamp_get() is
resolved through a legacy ndo_eth_ioctl(SIOCGHWTSTAMP) lower_dev
implementation, that needs a non-NULL old_cfg.ifr pointer, and we don't
have it.
But we don't even need to deal with it either. In the general case,
drivers may not even implement SIOCGHWTSTAMP handling, only SIOCSHWTSTAMP,
so it makes sense to completely avoid a SIOCGHWTSTAMP call if we can.
The solution is to split the single "if" condition into 3 smaller ones,
thus separating the decision to call ndo_hwtstamp_get() from the
decision to call ndo_hwtstamp_set(). The third "if" condition is
identical to the first one, and both are subsets of the second one.
Thus, the "cfg" argument of kernel_hwtstamp_config_changed() is always
valid.
Reported-by: Eric Dumazet <edumazet@google.com>
Closes: https://lore.kernel.org/netdev/CANn89iLOspJsvjPj+y8jikg7erXDomWe8sqHMdfL_2LQSFrPAg@mail.gmail.com/
Fixes: fd770e856e22 ("net: remove phy_has_hwtstamp() -> phy_mii_ioctl() decision from converted drivers")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-08-04 21:49:39 +08:00
|
|
|
}
|
2023-08-01 22:28:24 +08:00
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
if (phy_ts && (dev->priv_flags & IFF_SEE_ALL_HWTSTAMP_REQUESTS))
|
2023-08-01 22:28:24 +08:00
|
|
|
changed = kernel_hwtstamp_config_changed(&old_cfg, cfg);
|
|
|
|
|
2023-11-19 10:38:05 +08:00
|
|
|
if (phy_ts) {
|
2023-08-01 22:28:24 +08:00
|
|
|
err = phy_hwtstamp_set(dev->phydev, cfg, extack);
|
|
|
|
if (err) {
|
|
|
|
if (changed)
|
|
|
|
ops->ndo_hwtstamp_set(dev, &old_cfg, NULL);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-11-14 19:28:33 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dev_set_hwtstamp_phylib);
|
2023-08-01 22:28:24 +08:00
|
|
|
|
2023-04-02 20:37:51 +08:00
|
|
|
static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
|
|
|
|
{
|
2023-08-01 22:28:13 +08:00
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
2023-08-01 22:28:14 +08:00
|
|
|
struct kernel_hwtstamp_config kernel_cfg = {};
|
net: create a netdev notifier for DSA to reject PTP on DSA master
The fact that PTP 2-step TX timestamping is broken on DSA switches if
the master also timestamps the same packets is documented by commit
f685e609a301 ("net: dsa: Deny PTP on master if switch supports it").
We attempt to help the users avoid shooting themselves in the foot by
making DSA reject the timestamping ioctls on an interface that is a DSA
master, and the switch tree beneath it contains switches which are aware
of PTP.
The only problem is that there isn't an established way of intercepting
ndo_eth_ioctl calls, so DSA creates avoidable burden upon the network
stack by creating a struct dsa_netdevice_ops with overlaid function
pointers that are manually checked from the relevant call sites. There
used to be 2 such dsa_netdevice_ops, but now, ndo_eth_ioctl is the only
one left.
There is an ongoing effort to migrate driver-visible hardware timestamping
control from the ndo_eth_ioctl() based API to a new ndo_hwtstamp_set()
model, but DSA actively prevents that migration, since dsa_master_ioctl()
is currently coded to manually call the master's legacy ndo_eth_ioctl(),
and so, whenever a network device driver would be converted to the new
API, DSA's restrictions would be circumvented, because any device could
be used as a DSA master.
The established way for unrelated modules to react on a net device event
is via netdevice notifiers. So we create a new notifier which gets
called whenever there is an attempt to change hardware timestamping
settings on a device.
Finally, there is another reason why a netdev notifier will be a good
idea, besides strictly DSA, and this has to do with PHY timestamping.
With ndo_eth_ioctl(), all MAC drivers must manually call
phy_has_hwtstamp() before deciding whether to act upon SIOCSHWTSTAMP,
otherwise they must pass this ioctl to the PHY driver via
phy_mii_ioctl().
With the new ndo_hwtstamp_set() API, it will be desirable to simply not
make any calls into the MAC device driver when timestamping should be
performed at the PHY level.
But there exist drivers, such as the lan966x switch, which need to
install packet traps for PTP regardless of whether they are the layer
that provides the hardware timestamps, or the PHY is. That would be
impossible to support with the new API.
The proposal there, too, is to introduce a netdev notifier which acts as
a better cue for switching drivers to add or remove PTP packet traps,
than ndo_hwtstamp_set(). The one introduced here "almost" works there as
well, except for the fact that packet traps should only be installed if
the PHY driver succeeded to enable hardware timestamping, whereas here,
we need to deny hardware timestamping on the DSA master before it
actually gets enabled. This is why this notifier is called "PRE_", and
the notifier that would get used for PHY timestamping and packet traps
would be called NETDEV_CHANGE_HWTSTAMP. This isn't a new concept, for
example NETDEV_CHANGEUPPER and NETDEV_PRECHANGEUPPER do the same thing.
In expectation of future netlink UAPI, we also pass a non-NULL extack
pointer to the netdev notifier, and we make DSA populate it with an
informative reason for the rejection. To avoid making it go to waste, we
make the ioctl-based dev_set_hwtstamp() create a fake extack and print
the message to the kernel log.
Link: https://lore.kernel.org/netdev/20230401191215.tvveoi3lkawgg6g4@skbuf/
Link: https://lore.kernel.org/netdev/20230310164451.ls7bbs6pdzs4m6pw@skbuf/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:55 +08:00
|
|
|
struct netlink_ext_ack extack = {};
|
2023-04-02 20:37:52 +08:00
|
|
|
struct hwtstamp_config cfg;
|
2020-11-02 20:16:15 +08:00
|
|
|
int err;
|
2020-07-20 11:49:51 +08:00
|
|
|
|
2023-04-02 20:37:52 +08:00
|
|
|
if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it
Jakub Kicinski suggested that we may want to add new UAPI for
controlling hardware timestamping through netlink in the future, and in
that case, we will be limited to the struct hwtstamp_config that is
currently passed in fixed binary format through the SIOCGHWTSTAMP and
SIOCSHWTSTAMP ioctls. It would be good if new kernel code already
started operating on an extensible kernel variant of that structure,
similar in concept to struct kernel_ethtool_coalesce vs struct
ethtool_coalesce.
Since struct hwtstamp_config is in include/uapi/linux/net_tstamp.h, here
we introduce include/linux/net_tstamp.h which shadows that other header,
but also includes it, so that existing includers of this header work as
before. In addition to that, we add the definition for the kernel-only
structure, and a helper which translates all fields by manual copying.
I am doing a manual copy in order to not force the alignment (or type)
of the fields of struct kernel_hwtstamp_config to be the same as of
struct hwtstamp_config, even though now, they are the same.
Link: https://lore.kernel.org/netdev/20230330223519.36ce7d23@kernel.org/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:53 +08:00
|
|
|
hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
|
2023-08-01 22:28:14 +08:00
|
|
|
kernel_cfg.ifr = ifr;
|
net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it
Jakub Kicinski suggested that we may want to add new UAPI for
controlling hardware timestamping through netlink in the future, and in
that case, we will be limited to the struct hwtstamp_config that is
currently passed in fixed binary format through the SIOCGHWTSTAMP and
SIOCSHWTSTAMP ioctls. It would be good if new kernel code already
started operating on an extensible kernel variant of that structure,
similar in concept to struct kernel_ethtool_coalesce vs struct
ethtool_coalesce.
Since struct hwtstamp_config is in include/uapi/linux/net_tstamp.h, here
we introduce include/linux/net_tstamp.h which shadows that other header,
but also includes it, so that existing includers of this header work as
before. In addition to that, we add the definition for the kernel-only
structure, and a helper which translates all fields by manual copying.
I am doing a manual copy in order to not force the alignment (or type)
of the fields of struct kernel_hwtstamp_config to be the same as of
struct hwtstamp_config, even though now, they are the same.
Link: https://lore.kernel.org/netdev/20230330223519.36ce7d23@kernel.org/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:53 +08:00
|
|
|
|
|
|
|
err = net_hwtstamp_validate(&kernel_cfg);
|
2023-04-02 20:37:51 +08:00
|
|
|
if (err)
|
2020-07-20 11:49:53 +08:00
|
|
|
return err;
|
|
|
|
|
2023-10-24 02:17:28 +08:00
|
|
|
err = dsa_conduit_hwtstamp_validate(dev, &kernel_cfg, &extack);
|
net: create a netdev notifier for DSA to reject PTP on DSA master
The fact that PTP 2-step TX timestamping is broken on DSA switches if
the master also timestamps the same packets is documented by commit
f685e609a301 ("net: dsa: Deny PTP on master if switch supports it").
We attempt to help the users avoid shooting themselves in the foot by
making DSA reject the timestamping ioctls on an interface that is a DSA
master, and the switch tree beneath it contains switches which are aware
of PTP.
The only problem is that there isn't an established way of intercepting
ndo_eth_ioctl calls, so DSA creates avoidable burden upon the network
stack by creating a struct dsa_netdevice_ops with overlaid function
pointers that are manually checked from the relevant call sites. There
used to be 2 such dsa_netdevice_ops, but now, ndo_eth_ioctl is the only
one left.
There is an ongoing effort to migrate driver-visible hardware timestamping
control from the ndo_eth_ioctl() based API to a new ndo_hwtstamp_set()
model, but DSA actively prevents that migration, since dsa_master_ioctl()
is currently coded to manually call the master's legacy ndo_eth_ioctl(),
and so, whenever a network device driver would be converted to the new
API, DSA's restrictions would be circumvented, because any device could
be used as a DSA master.
The established way for unrelated modules to react on a net device event
is via netdevice notifiers. So we create a new notifier which gets
called whenever there is an attempt to change hardware timestamping
settings on a device.
Finally, there is another reason why a netdev notifier will be a good
idea, besides strictly DSA, and this has to do with PHY timestamping.
With ndo_eth_ioctl(), all MAC drivers must manually call
phy_has_hwtstamp() before deciding whether to act upon SIOCSHWTSTAMP,
otherwise they must pass this ioctl to the PHY driver via
phy_mii_ioctl().
With the new ndo_hwtstamp_set() API, it will be desirable to simply not
make any calls into the MAC device driver when timestamping should be
performed at the PHY level.
But there exist drivers, such as the lan966x switch, which need to
install packet traps for PTP regardless of whether they are the layer
that provides the hardware timestamps, or the PHY is. That would be
impossible to support with the new API.
The proposal there, too, is to introduce a netdev notifier which acts as
a better cue for switching drivers to add or remove PTP packet traps,
than ndo_hwtstamp_set(). The one introduced here "almost" works there as
well, except for the fact that packet traps should only be installed if
the PHY driver succeeded to enable hardware timestamping, whereas here,
we need to deny hardware timestamping on the DSA master before it
actually gets enabled. This is why this notifier is called "PRE_", and
the notifier that would get used for PHY timestamping and packet traps
would be called NETDEV_CHANGE_HWTSTAMP. This isn't a new concept, for
example NETDEV_CHANGEUPPER and NETDEV_PRECHANGEUPPER do the same thing.
In expectation of future netlink UAPI, we also pass a non-NULL extack
pointer to the netdev notifier, and we make DSA populate it with an
informative reason for the rejection. To avoid making it go to waste, we
make the ioctl-based dev_set_hwtstamp() create a fake extack and print
the message to the kernel log.
Link: https://lore.kernel.org/netdev/20230401191215.tvveoi3lkawgg6g4@skbuf/
Link: https://lore.kernel.org/netdev/20230310164451.ls7bbs6pdzs4m6pw@skbuf/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:55 +08:00
|
|
|
if (err) {
|
|
|
|
if (extack._msg)
|
|
|
|
netdev_err(dev, "%s\n", extack._msg);
|
2023-04-02 20:37:51 +08:00
|
|
|
return err;
|
net: create a netdev notifier for DSA to reject PTP on DSA master
The fact that PTP 2-step TX timestamping is broken on DSA switches if
the master also timestamps the same packets is documented by commit
f685e609a301 ("net: dsa: Deny PTP on master if switch supports it").
We attempt to help the users avoid shooting themselves in the foot by
making DSA reject the timestamping ioctls on an interface that is a DSA
master, and the switch tree beneath it contains switches which are aware
of PTP.
The only problem is that there isn't an established way of intercepting
ndo_eth_ioctl calls, so DSA creates avoidable burden upon the network
stack by creating a struct dsa_netdevice_ops with overlaid function
pointers that are manually checked from the relevant call sites. There
used to be 2 such dsa_netdevice_ops, but now, ndo_eth_ioctl is the only
one left.
There is an ongoing effort to migrate driver-visible hardware timestamping
control from the ndo_eth_ioctl() based API to a new ndo_hwtstamp_set()
model, but DSA actively prevents that migration, since dsa_master_ioctl()
is currently coded to manually call the master's legacy ndo_eth_ioctl(),
and so, whenever a network device driver would be converted to the new
API, DSA's restrictions would be circumvented, because any device could
be used as a DSA master.
The established way for unrelated modules to react on a net device event
is via netdevice notifiers. So we create a new notifier which gets
called whenever there is an attempt to change hardware timestamping
settings on a device.
Finally, there is another reason why a netdev notifier will be a good
idea, besides strictly DSA, and this has to do with PHY timestamping.
With ndo_eth_ioctl(), all MAC drivers must manually call
phy_has_hwtstamp() before deciding whether to act upon SIOCSHWTSTAMP,
otherwise they must pass this ioctl to the PHY driver via
phy_mii_ioctl().
With the new ndo_hwtstamp_set() API, it will be desirable to simply not
make any calls into the MAC device driver when timestamping should be
performed at the PHY level.
But there exist drivers, such as the lan966x switch, which need to
install packet traps for PTP regardless of whether they are the layer
that provides the hardware timestamps, or the PHY is. That would be
impossible to support with the new API.
The proposal there, too, is to introduce a netdev notifier which acts as
a better cue for switching drivers to add or remove PTP packet traps,
than ndo_hwtstamp_set(). The one introduced here "almost" works there as
well, except for the fact that packet traps should only be installed if
the PHY driver succeeded to enable hardware timestamping, whereas here,
we need to deny hardware timestamping on the DSA master before it
actually gets enabled. This is why this notifier is called "PRE_", and
the notifier that would get used for PHY timestamping and packet traps
would be called NETDEV_CHANGE_HWTSTAMP. This isn't a new concept, for
example NETDEV_CHANGEUPPER and NETDEV_PRECHANGEUPPER do the same thing.
In expectation of future netlink UAPI, we also pass a non-NULL extack
pointer to the netdev notifier, and we make DSA populate it with an
informative reason for the rejection. To avoid making it go to waste, we
make the ioctl-based dev_set_hwtstamp() create a fake extack and print
the message to the kernel log.
Link: https://lore.kernel.org/netdev/20230401191215.tvveoi3lkawgg6g4@skbuf/
Link: https://lore.kernel.org/netdev/20230310164451.ls7bbs6pdzs4m6pw@skbuf/
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-04-02 20:37:55 +08:00
|
|
|
}
|
2020-07-20 11:49:51 +08:00
|
|
|
|
2023-08-01 22:28:13 +08:00
|
|
|
if (!ops->ndo_hwtstamp_set)
|
|
|
|
return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); /* legacy */
|
|
|
|
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2023-08-01 22:28:24 +08:00
|
|
|
err = dev_set_hwtstamp_phylib(dev, &kernel_cfg, &extack);
|
|
|
|
if (err)
|
2023-08-01 22:28:13 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
/* The driver may have modified the configuration, so copy the
|
|
|
|
* updated version of it back to user space
|
|
|
|
*/
|
2023-08-01 22:28:14 +08:00
|
|
|
if (!kernel_cfg.copied_to_user) {
|
|
|
|
hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
|
2023-08-01 22:28:13 +08:00
|
|
|
|
2023-08-01 22:28:14 +08:00
|
|
|
if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int generic_hwtstamp_ioctl_lower(struct net_device *dev, int cmd,
|
|
|
|
struct kernel_hwtstamp_config *kernel_cfg)
|
|
|
|
{
|
|
|
|
struct ifreq ifrr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
strscpy_pad(ifrr.ifr_name, dev->name, IFNAMSIZ);
|
|
|
|
ifrr.ifr_ifru = kernel_cfg->ifr->ifr_ifru;
|
|
|
|
|
|
|
|
err = dev_eth_ioctl(dev, &ifrr, cmd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
kernel_cfg->ifr->ifr_ifru = ifrr.ifr_ifru;
|
|
|
|
kernel_cfg->copied_to_user = true;
|
2023-08-01 22:28:13 +08:00
|
|
|
|
|
|
|
return 0;
|
2020-07-20 11:49:51 +08:00
|
|
|
}
|
|
|
|
|
2023-08-01 22:28:14 +08:00
|
|
|
int generic_hwtstamp_get_lower(struct net_device *dev,
|
|
|
|
struct kernel_hwtstamp_config *kernel_cfg)
|
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (ops->ndo_hwtstamp_get)
|
2023-08-01 22:28:24 +08:00
|
|
|
return dev_get_hwtstamp_phylib(dev, kernel_cfg);
|
2023-08-01 22:28:14 +08:00
|
|
|
|
|
|
|
/* Legacy path: unconverted lower driver */
|
|
|
|
return generic_hwtstamp_ioctl_lower(dev, SIOCGHWTSTAMP, kernel_cfg);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(generic_hwtstamp_get_lower);
|
|
|
|
|
|
|
|
int generic_hwtstamp_set_lower(struct net_device *dev,
|
|
|
|
struct kernel_hwtstamp_config *kernel_cfg,
|
|
|
|
struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (ops->ndo_hwtstamp_set)
|
2023-08-01 22:28:24 +08:00
|
|
|
return dev_set_hwtstamp_phylib(dev, kernel_cfg, extack);
|
2023-08-01 22:28:14 +08:00
|
|
|
|
|
|
|
/* Legacy path: unconverted lower driver */
|
|
|
|
return generic_hwtstamp_ioctl_lower(dev, SIOCSHWTSTAMP, kernel_cfg);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(generic_hwtstamp_set_lower);
|
|
|
|
|
2021-07-27 21:45:17 +08:00
|
|
|
static int dev_siocbond(struct net_device *dev,
|
2021-07-27 21:45:13 +08:00
|
|
|
struct ifreq *ifr, unsigned int cmd)
|
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
|
2021-07-27 21:45:17 +08:00
|
|
|
if (ops->ndo_siocbond) {
|
2021-07-27 21:45:13 +08:00
|
|
|
if (netif_device_present(dev))
|
2021-07-27 21:45:17 +08:00
|
|
|
return ops->ndo_siocbond(dev, ifr, cmd);
|
2021-07-27 21:45:13 +08:00
|
|
|
else
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:45:12 +08:00
|
|
|
static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
|
|
|
|
void __user *data, unsigned int cmd)
|
2021-07-27 21:44:47 +08:00
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
|
|
|
|
if (ops->ndo_siocdevprivate) {
|
|
|
|
if (netif_device_present(dev))
|
|
|
|
return ops->ndo_siocdevprivate(dev, ifr, data, cmd);
|
|
|
|
else
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:45:15 +08:00
|
|
|
return -EOPNOTSUPP;
|
2021-07-27 21:44:47 +08:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:45:14 +08:00
|
|
|
static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
|
|
|
|
{
|
|
|
|
const struct net_device_ops *ops = dev->netdev_ops;
|
|
|
|
|
|
|
|
if (ops->ndo_siocwandev) {
|
|
|
|
if (netif_device_present(dev))
|
|
|
|
return ops->ndo_siocwandev(dev, ifs);
|
|
|
|
else
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2013-02-16 06:20:46 +08:00
|
|
|
/*
|
|
|
|
* Perform the SIOCxIFxxx calls, inside rtnl_lock()
|
|
|
|
*/
|
2021-07-27 21:45:12 +08:00
|
|
|
static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
|
|
|
|
unsigned int cmd)
|
2013-02-16 06:20:46 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
|
|
|
|
const struct net_device_ops *ops;
|
2021-12-05 12:22:01 +08:00
|
|
|
netdevice_tracker dev_tracker;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
ops = dev->netdev_ops;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCSIFFLAGS: /* Set interface flags */
|
2018-12-07 01:05:42 +08:00
|
|
|
return dev_change_flags(dev, ifr->ifr_flags, NULL);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
case SIOCSIFMETRIC: /* Set the metric on the interface
|
|
|
|
(currently unused) */
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
case SIOCSIFMTU: /* Set the MTU of a device */
|
|
|
|
return dev_set_mtu(dev, ifr->ifr_mtu);
|
|
|
|
|
|
|
|
case SIOCSIFHWADDR:
|
2017-07-27 06:22:06 +08:00
|
|
|
if (dev->addr_len > sizeof(struct sockaddr))
|
|
|
|
return -EINVAL;
|
net: fix dev_ifsioc_locked() race condition
dev_ifsioc_locked() is called with only RCU read lock, so when
there is a parallel writer changing the mac address, it could
get a partially updated mac address, as shown below:
Thread 1 Thread 2
// eth_commit_mac_addr_change()
memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
// dev_ifsioc_locked()
memcpy(ifr->ifr_hwaddr.sa_data,
dev->dev_addr,...);
Close this race condition by guarding them with a RW semaphore,
like netdev_get_name(). We can not use seqlock here as it does not
allow blocking. The writers already take RTNL anyway, so this does
not affect the slow path. To avoid bothering existing
dev_set_mac_address() callers in drivers, introduce a new wrapper
just for user-facing callers on ioctl and rtnetlink paths.
Note, bonding also changes slave mac addresses but that requires
a separate patch due to the complexity of bonding code.
Fixes: 3710becf8a58 ("net: RCU locking for simple ioctl()")
Reported-by: "Gong, Sishuai" <sishuai@purdue.edu>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2021-02-12 03:34:10 +08:00
|
|
|
return dev_set_mac_address_user(dev, &ifr->ifr_hwaddr, NULL);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
case SIOCSIFHWBROADCAST:
|
|
|
|
if (ifr->ifr_hwaddr.sa_family != dev->type)
|
|
|
|
return -EINVAL;
|
|
|
|
memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
|
net: dev: Convert sa_data to flexible array in struct sockaddr
One of the worst offenders of "fake flexible arrays" is struct sockaddr,
as it is the classic example of why GCC and Clang have been traditionally
forced to treat all trailing arrays as fake flexible arrays: in the
distant misty past, sa_data became too small, and code started just
treating it as a flexible array, even though it was fixed-size. The
special case by the compiler is specifically that sizeof(sa->sa_data)
and FORTIFY_SOURCE (which uses __builtin_object_size(sa->sa_data, 1))
do not agree (14 and -1 respectively), which makes FORTIFY_SOURCE treat
it as a flexible array.
However, the coming -fstrict-flex-arrays compiler flag will remove
these special cases so that FORTIFY_SOURCE can gain coverage over all
the trailing arrays in the kernel that are _not_ supposed to be treated
as a flexible array. To deal with this change, convert sa_data to a true
flexible array. To keep the structure size the same, move sa_data into
a union with a newly introduced sa_data_min with the original size. The
result is that FORTIFY_SOURCE can continue to have no idea how large
sa_data may actually be, but anything using sizeof(sa->sa_data) must
switch to sizeof(sa->sa_data_min).
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Pavel Begunkov <asml.silence@gmail.com>
Cc: David Ahern <dsahern@kernel.org>
Cc: Dylan Yudaken <dylany@fb.com>
Cc: Yajun Deng <yajun.deng@linux.dev>
Cc: Petr Machata <petrm@nvidia.com>
Cc: Hangbin Liu <liuhangbin@gmail.com>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: syzbot <syzkaller@googlegroups.com>
Cc: Willem de Bruijn <willemb@google.com>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20221018095503.never.671-kees@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2022-10-18 17:56:03 +08:00
|
|
|
min(sizeof(ifr->ifr_hwaddr.sa_data_min),
|
2014-11-18 05:23:17 +08:00
|
|
|
(size_t)dev->addr_len));
|
2013-02-16 06:20:46 +08:00
|
|
|
call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCSIFMAP:
|
2021-07-22 22:29:00 +08:00
|
|
|
return dev_setifmap(dev, ifr);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
case SIOCADDMULTI:
|
|
|
|
if (!ops->ndo_set_rx_mode ||
|
|
|
|
ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
|
|
|
|
|
|
|
|
case SIOCDELMULTI:
|
|
|
|
if (!ops->ndo_set_rx_mode ||
|
|
|
|
ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
|
|
|
|
|
|
|
|
case SIOCSIFTXQLEN:
|
|
|
|
if (ifr->ifr_qlen < 0)
|
|
|
|
return -EINVAL;
|
2018-07-24 18:53:00 +08:00
|
|
|
return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
|
|
|
case SIOCSIFNAME:
|
|
|
|
ifr->ifr_newname[IFNAMSIZ-1] = '\0';
|
|
|
|
return dev_change_name(dev, ifr->ifr_newname);
|
|
|
|
|
2021-07-27 21:45:14 +08:00
|
|
|
case SIOCWANDEV:
|
|
|
|
return dev_siocwandev(dev, &ifr->ifr_settings);
|
|
|
|
|
2021-07-27 21:45:16 +08:00
|
|
|
case SIOCBRADDIF:
|
|
|
|
case SIOCBRDELIF:
|
|
|
|
if (!netif_device_present(dev))
|
|
|
|
return -ENODEV;
|
2021-08-05 16:29:03 +08:00
|
|
|
if (!netif_is_bridge_master(dev))
|
|
|
|
return -EOPNOTSUPP;
|
2022-06-08 12:39:55 +08:00
|
|
|
netdev_hold(dev, &dev_tracker, GFP_KERNEL);
|
2021-08-05 16:29:01 +08:00
|
|
|
rtnl_unlock();
|
|
|
|
err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
|
2022-06-08 12:39:55 +08:00
|
|
|
netdev_put(dev, &dev_tracker);
|
2021-08-05 16:29:01 +08:00
|
|
|
rtnl_lock();
|
|
|
|
return err;
|
2021-07-27 21:45:16 +08:00
|
|
|
|
2023-04-02 20:37:49 +08:00
|
|
|
case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
|
|
|
|
return dev_siocdevprivate(dev, ifr, data, cmd);
|
|
|
|
|
2013-02-16 06:20:46 +08:00
|
|
|
case SIOCSHWTSTAMP:
|
2023-04-02 20:37:51 +08:00
|
|
|
return dev_set_hwtstamp(dev, ifr);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2023-04-02 20:37:49 +08:00
|
|
|
case SIOCGHWTSTAMP:
|
2023-04-02 20:37:51 +08:00
|
|
|
return dev_get_hwtstamp(dev, ifr);
|
|
|
|
|
2023-04-02 20:37:49 +08:00
|
|
|
case SIOCGMIIPHY:
|
|
|
|
case SIOCGMIIREG:
|
|
|
|
case SIOCSMIIREG:
|
|
|
|
return dev_eth_ioctl(dev, ifr, cmd);
|
|
|
|
|
|
|
|
case SIOCBONDENSLAVE:
|
|
|
|
case SIOCBONDRELEASE:
|
|
|
|
case SIOCBONDSETHWADDR:
|
|
|
|
case SIOCBONDSLAVEINFOQUERY:
|
|
|
|
case SIOCBONDINFOQUERY:
|
|
|
|
case SIOCBONDCHANGEACTIVE:
|
|
|
|
return dev_siocbond(dev, ifr, cmd);
|
|
|
|
|
|
|
|
/* Unknown ioctl */
|
2013-02-16 06:20:46 +08:00
|
|
|
default:
|
2023-04-02 20:37:49 +08:00
|
|
|
err = -EINVAL;
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dev_load - load a network module
|
|
|
|
* @net: the applicable net namespace
|
|
|
|
* @name: name of interface
|
|
|
|
*
|
|
|
|
* If a network interface is not present and the process has suitable
|
|
|
|
* privileges this function loads the module. If module loading is not
|
|
|
|
* available in this kernel then it becomes a nop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void dev_load(struct net *net, const char *name)
|
|
|
|
{
|
|
|
|
struct net_device *dev;
|
|
|
|
int no_module;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
dev = dev_get_by_name_rcu(net, name);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
no_module = !dev;
|
|
|
|
if (no_module && capable(CAP_NET_ADMIN))
|
|
|
|
no_module = request_module("netdev-%s", name);
|
dev_ioctl: remove dev_load() CAP_SYS_MODULE message
Marcel reported to see the following message when autoloading
is being triggered when adding nlmon device:
Loading kernel module for a network device with
CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias
netdev-nlmon instead.
This false-positive happens despite with having correct
capabilities set, e.g. through issuing `ip link del dev nlmon`
more than once on a valid device with name nlmon, but Marcel
has also seen it on creation time when no nlmon module is
previously compiled-in or loaded as module and the device
name equals a link type name (e.g. nlmon, vxlan, team).
Stephen says:
The netdev module alias is a hold over from the past. For
normal devices, people used to create a alias eth0 to and
point it to the type of network device used, that was back
in the bad old ISA days before real discovery.
Also, the tunnels create module alias for the control device
and ip used to use this to autoload the tunnel device.
The message is bogus and should just be removed, I also see
it in a couple of other cases where tap devices are renamed
for other usese.
As mentioned in 8909c9ad8ff0 ("net: don't allow CAP_NET_ADMIN
to load non-netdev kernel modules"), we nevertheless still
might want to leave the old autoloading behaviour in place
as it could break old scripts, so for now, lets just remove
the log message as Stephen suggests.
Reference: http://thread.gmane.org/gmane.linux.kernel/1105168
Reported-by: Marcel Holtmann <marcel@holtmann.org>
Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Vasiliy Kulikov <segoon@openwall.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-03 05:30:05 +08:00
|
|
|
if (no_module && capable(CAP_SYS_MODULE))
|
|
|
|
request_module("%s", name);
|
2013-02-16 06:20:46 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dev_load);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function handles all "interface"-type I/O control requests. The actual
|
|
|
|
* 'doing' part of this is dev_ifsioc above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dev_ioctl - network device ioctl
|
|
|
|
* @net: the applicable net namespace
|
|
|
|
* @cmd: command to issue
|
2019-03-26 00:17:20 +08:00
|
|
|
* @ifr: pointer to a struct ifreq in user space
|
2023-03-25 06:11:49 +08:00
|
|
|
* @data: data exchanged with userspace
|
2019-03-26 00:17:20 +08:00
|
|
|
* @need_copyout: whether or not copy_to_user() should be called
|
2013-02-16 06:20:46 +08:00
|
|
|
*
|
|
|
|
* Issue ioctl functions to devices. This is normally called by the
|
|
|
|
* user space syscall interfaces but can sometimes be useful for
|
|
|
|
* other purposes. The return value is the return from the syscall if
|
|
|
|
* positive or a negative errno code on error.
|
|
|
|
*/
|
|
|
|
|
2021-07-27 21:45:12 +08:00
|
|
|
int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
|
|
|
|
void __user *data, bool *need_copyout)
|
2013-02-16 06:20:46 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *colon;
|
|
|
|
|
2017-10-06 00:59:44 +08:00
|
|
|
if (need_copyout)
|
|
|
|
*need_copyout = true;
|
2013-02-16 06:20:46 +08:00
|
|
|
if (cmd == SIOCGIFNAME)
|
2017-10-06 00:59:44 +08:00
|
|
|
return dev_ifname(net, ifr);
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2017-10-06 00:59:44 +08:00
|
|
|
ifr->ifr_name[IFNAMSIZ-1] = 0;
|
2013-02-16 06:20:46 +08:00
|
|
|
|
2017-10-06 00:59:44 +08:00
|
|
|
colon = strchr(ifr->ifr_name, ':');
|
2013-02-16 06:20:46 +08:00
|
|
|
if (colon)
|
|
|
|
*colon = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See which interface the caller is talking about.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (cmd) {
|
net: fix dev_ifsioc_locked() race condition
dev_ifsioc_locked() is called with only RCU read lock, so when
there is a parallel writer changing the mac address, it could
get a partially updated mac address, as shown below:
Thread 1 Thread 2
// eth_commit_mac_addr_change()
memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
// dev_ifsioc_locked()
memcpy(ifr->ifr_hwaddr.sa_data,
dev->dev_addr,...);
Close this race condition by guarding them with a RW semaphore,
like netdev_get_name(). We can not use seqlock here as it does not
allow blocking. The writers already take RTNL anyway, so this does
not affect the slow path. To avoid bothering existing
dev_set_mac_address() callers in drivers, introduce a new wrapper
just for user-facing callers on ioctl and rtnetlink paths.
Note, bonding also changes slave mac addresses but that requires
a separate patch due to the complexity of bonding code.
Fixes: 3710becf8a58 ("net: RCU locking for simple ioctl()")
Reported-by: "Gong, Sishuai" <sishuai@purdue.edu>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2021-02-12 03:34:10 +08:00
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
dev_load(net, ifr->ifr_name);
|
|
|
|
ret = dev_get_mac_address(&ifr->ifr_hwaddr, net, ifr->ifr_name);
|
|
|
|
if (colon)
|
|
|
|
*colon = ':';
|
|
|
|
return ret;
|
2013-02-16 06:20:46 +08:00
|
|
|
/*
|
|
|
|
* These ioctl calls:
|
|
|
|
* - can be done by all.
|
|
|
|
* - atomic and do not require locking.
|
|
|
|
* - return a value
|
|
|
|
*/
|
|
|
|
case SIOCGIFFLAGS:
|
|
|
|
case SIOCGIFMETRIC:
|
|
|
|
case SIOCGIFMTU:
|
|
|
|
case SIOCGIFSLAVE:
|
|
|
|
case SIOCGIFMAP:
|
|
|
|
case SIOCGIFINDEX:
|
|
|
|
case SIOCGIFTXQLEN:
|
2018-03-07 06:27:44 +08:00
|
|
|
dev_load(net, ifr->ifr_name);
|
2013-02-16 06:20:46 +08:00
|
|
|
rcu_read_lock();
|
2017-10-06 00:59:44 +08:00
|
|
|
ret = dev_ifsioc_locked(net, ifr, cmd);
|
2013-02-16 06:20:46 +08:00
|
|
|
rcu_read_unlock();
|
2017-10-06 00:59:44 +08:00
|
|
|
if (colon)
|
|
|
|
*colon = ':';
|
2013-02-16 06:20:46 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
case SIOCETHTOOL:
|
2018-03-07 06:27:44 +08:00
|
|
|
dev_load(net, ifr->ifr_name);
|
2021-07-27 21:45:12 +08:00
|
|
|
ret = dev_ethtool(net, ifr, data);
|
2017-10-06 00:59:44 +08:00
|
|
|
if (colon)
|
|
|
|
*colon = ':';
|
2013-02-16 06:20:46 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These ioctl calls:
|
|
|
|
* - require superuser power.
|
|
|
|
* - require strict serialization.
|
|
|
|
* - return a value
|
|
|
|
*/
|
|
|
|
case SIOCGMIIPHY:
|
|
|
|
case SIOCGMIIREG:
|
|
|
|
case SIOCSIFNAME:
|
2018-03-07 06:27:44 +08:00
|
|
|
dev_load(net, ifr->ifr_name);
|
2013-02-16 06:20:46 +08:00
|
|
|
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
rtnl_lock();
|
2021-07-27 21:45:12 +08:00
|
|
|
ret = dev_ifsioc(net, ifr, data, cmd);
|
2013-02-16 06:20:46 +08:00
|
|
|
rtnl_unlock();
|
2017-10-06 00:59:44 +08:00
|
|
|
if (colon)
|
|
|
|
*colon = ':';
|
2013-02-16 06:20:46 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These ioctl calls:
|
|
|
|
* - require superuser power.
|
|
|
|
* - require strict serialization.
|
|
|
|
* - do not return a value
|
|
|
|
*/
|
|
|
|
case SIOCSIFMAP:
|
|
|
|
case SIOCSIFTXQLEN:
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough;
|
2013-02-16 06:20:46 +08:00
|
|
|
/*
|
|
|
|
* These ioctl calls:
|
|
|
|
* - require local superuser power.
|
|
|
|
* - require strict serialization.
|
|
|
|
* - do not return a value
|
|
|
|
*/
|
|
|
|
case SIOCSIFFLAGS:
|
|
|
|
case SIOCSIFMETRIC:
|
|
|
|
case SIOCSIFMTU:
|
|
|
|
case SIOCSIFHWADDR:
|
|
|
|
case SIOCSIFSLAVE:
|
|
|
|
case SIOCADDMULTI:
|
|
|
|
case SIOCDELMULTI:
|
|
|
|
case SIOCSIFHWBROADCAST:
|
|
|
|
case SIOCSMIIREG:
|
|
|
|
case SIOCBONDENSLAVE:
|
|
|
|
case SIOCBONDRELEASE:
|
|
|
|
case SIOCBONDSETHWADDR:
|
|
|
|
case SIOCBONDCHANGEACTIVE:
|
|
|
|
case SIOCBRADDIF:
|
|
|
|
case SIOCBRDELIF:
|
|
|
|
case SIOCSHWTSTAMP:
|
|
|
|
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough;
|
2013-02-16 06:20:46 +08:00
|
|
|
case SIOCBONDSLAVEINFOQUERY:
|
|
|
|
case SIOCBONDINFOQUERY:
|
2018-03-07 06:27:44 +08:00
|
|
|
dev_load(net, ifr->ifr_name);
|
2013-02-16 06:20:46 +08:00
|
|
|
rtnl_lock();
|
2021-07-27 21:45:12 +08:00
|
|
|
ret = dev_ifsioc(net, ifr, data, cmd);
|
2013-02-16 06:20:46 +08:00
|
|
|
rtnl_unlock();
|
2017-10-06 00:59:44 +08:00
|
|
|
if (need_copyout)
|
|
|
|
*need_copyout = false;
|
2013-02-16 06:20:46 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
case SIOCGIFMEM:
|
|
|
|
/* Get the per device memory space. We can add this but
|
|
|
|
* currently do not support it */
|
|
|
|
case SIOCSIFMEM:
|
|
|
|
/* Set the per device memory buffer space.
|
|
|
|
* Not applicable in our case */
|
|
|
|
case SIOCSIFLINK:
|
|
|
|
return -ENOTTY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unknown or private ioctl.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
if (cmd == SIOCWANDEV ||
|
2013-11-14 09:19:29 +08:00
|
|
|
cmd == SIOCGHWTSTAMP ||
|
2013-02-16 06:20:46 +08:00
|
|
|
(cmd >= SIOCDEVPRIVATE &&
|
|
|
|
cmd <= SIOCDEVPRIVATE + 15)) {
|
2018-03-07 06:27:44 +08:00
|
|
|
dev_load(net, ifr->ifr_name);
|
2013-02-16 06:20:46 +08:00
|
|
|
rtnl_lock();
|
2021-07-27 21:45:12 +08:00
|
|
|
ret = dev_ifsioc(net, ifr, data, cmd);
|
2013-02-16 06:20:46 +08:00
|
|
|
rtnl_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|