lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date:   Fri, 18 Jan 2019 13:21:37 +0000
From:   Jose Abreu <jose.abreu@...opsys.com>
To:     Artem Panfilov <panfilov.artyom@...il.com>
CC:     Tema <panfilovartyom@...il.com>,
        Giuseppe Cavallaro <peppe.cavallaro@...com>,
        Alexandre Torgue <alexandre.torgue@...com>,
        "Jose Abreu" <jose.abreu@...opsys.com>,
        "David S. Miller" <davem@...emloft.net>,
        Maxime Coquelin <mcoquelin.stm32@...il.com>,
        <netdev@...r.kernel.org>,
        <linux-stm32@...md-mailman.stormreply.com>,
        <linux-arm-kernel@...ts.infradead.org>,
        <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 1/1] net: stmmac: implement the SIOCGHWTSTAMP ioctl

Hello Artem,

Please specify in patch subject whether this is for -net or
-net-next. More comments inline.

On 1/17/2019 7:15 PM, Artem Panfilov wrote:
> From: Tema <panfilovartyom@...il.com>
> 
> This patch adds support for the SIOCGHWTSTAMP ioctl which enables user
> processes to read the current hwtstamp_config settings
> non-destructively.

I think you should only save the config structure *after*
validating the parameters. i.e. use a local config and save it in
priv only if ret == 0.

> 
> Signed-off-by: Artem Panfilov <panfilov.artyom@...il.com>
> ---
>  drivers/net/ethernet/stmicro/stmmac/stmmac.h  |  2 +
>  .../net/ethernet/stmicro/stmmac/stmmac_main.c | 77 ++++++++++++-------
>  2 files changed, 52 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> index 63e1064b27a2..f9b42b0c20f4 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> @@ -28,6 +28,7 @@
>  #include <linux/pci.h>
>  #include "common.h"
>  #include <linux/ptp_clock_kernel.h>
> +#include <linux/net_tstamp.h>
>  #include <linux/reset.h>
>  
>  struct stmmac_resources {
> @@ -174,6 +175,7 @@ struct stmmac_priv {
>  	unsigned int mode;
>  	unsigned int chain_mode;
>  	int extend_desc;
> +	struct hwtstamp_config tstamp_config;
>  	struct ptp_clock *ptp_clock;
>  	struct ptp_clock_info ptp_clock_ops;
>  	unsigned int default_addend;
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> index 5afba69981cf..a6dc40a70428 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> @@ -534,7 +534,7 @@ static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
>  }
>  
>  /**
> - *  stmmac_hwtstamp_ioctl - control hardware timestamping.
> + *  stmmac_hwtstamp_set - control hardware timestamping.
>   *  @dev: device pointer.
>   *  @ifr: An IOCTL specific structure, that can contain a pointer to
>   *  a proprietary structure used to pass information to the driver.
> @@ -544,10 +544,10 @@ static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
>   *  Return Value:
>   *  0 on success and an appropriate -ve integer on failure.
>   */
> -static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
> +static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
>  {
>  	struct stmmac_priv *priv = netdev_priv(dev);
> -	struct hwtstamp_config config;
> +	struct hwtstamp_config *config =  &priv->tstamp_config;
>  	struct timespec64 now;
>  	u64 temp = 0;
>  	u32 ptp_v2 = 0;
> @@ -572,31 +572,31 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  		return -EOPNOTSUPP;
>  	}
>  
> -	if (copy_from_user(&config, ifr->ifr_data,
> +	if (copy_from_user(config, ifr->ifr_data,
>  			   sizeof(struct hwtstamp_config)))

Please use "sizeof(*config)" instead. It makes the code more
readable and occupy less characters.

>  		return -EFAULT;
>  
>  	netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
> -		   __func__, config.flags, config.tx_type, config.rx_filter);
> +		   __func__, config->flags, config->tx_type, config->rx_filter);
>  
>  	/* reserved for future extensions */
> -	if (config.flags)
> +	if (config->flags)
>  		return -EINVAL;
>  
> -	if (config.tx_type != HWTSTAMP_TX_OFF &&
> -	    config.tx_type != HWTSTAMP_TX_ON)
> +	if (config->tx_type != HWTSTAMP_TX_OFF &&
> +	    config->tx_type != HWTSTAMP_TX_ON)
>  		return -ERANGE;
>  
>  	if (priv->adv_ts) {
> -		switch (config.rx_filter) {
> +		switch (config->rx_filter) {
>  		case HWTSTAMP_FILTER_NONE:
>  			/* time stamp no incoming packet at all */
> -			config.rx_filter = HWTSTAMP_FILTER_NONE;
> +			config->rx_filter = HWTSTAMP_FILTER_NONE;
>  			break;
>  
>  		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
>  			/* PTP v1, UDP, any kind of event packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
>  			/* take time stamp for all event messages */
>  			if (xmac)
>  				snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
> @@ -609,7 +609,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
>  			/* PTP v1, UDP, Sync packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
>  			/* take time stamp for SYNC messages only */
>  			ts_event_en = PTP_TCR_TSEVNTENA;
>  
> @@ -619,7 +619,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
>  			/* PTP v1, UDP, Delay_req packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
>  			/* take time stamp for Delay_Req messages only */
>  			ts_master_en = PTP_TCR_TSMSTRENA;
>  			ts_event_en = PTP_TCR_TSEVNTENA;
> @@ -630,7 +630,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
>  			/* PTP v2, UDP, any kind of event packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for all event messages */
>  			if (xmac)
> @@ -644,7 +644,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
>  			/* PTP v2, UDP, Sync packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for SYNC messages only */
>  			ts_event_en = PTP_TCR_TSEVNTENA;
> @@ -655,7 +655,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
>  			/* PTP v2, UDP, Delay_req packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for Delay_Req messages only */
>  			ts_master_en = PTP_TCR_TSMSTRENA;
> @@ -667,7 +667,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_EVENT:
>  			/* PTP v2/802.AS1 any layer, any kind of event packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for all event messages */
>  			if (xmac)
> @@ -682,7 +682,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_SYNC:
>  			/* PTP v2/802.AS1, any layer, Sync packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for SYNC messages only */
>  			ts_event_en = PTP_TCR_TSEVNTENA;
> @@ -694,7 +694,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  
>  		case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
>  			/* PTP v2/802.AS1, any layer, Delay_req packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
>  			ptp_v2 = PTP_TCR_TSVER2ENA;
>  			/* take time stamp for Delay_Req messages only */
>  			ts_master_en = PTP_TCR_TSMSTRENA;
> @@ -708,7 +708,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  		case HWTSTAMP_FILTER_NTP_ALL:
>  		case HWTSTAMP_FILTER_ALL:
>  			/* time stamp any incoming packet */
> -			config.rx_filter = HWTSTAMP_FILTER_ALL;
> +			config->rx_filter = HWTSTAMP_FILTER_ALL;
>  			tstamp_all = PTP_TCR_TSENALL;
>  			break;
>  
> @@ -716,18 +716,18 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  			return -ERANGE;
>  		}
>  	} else {
> -		switch (config.rx_filter) {
> +		switch (config->rx_filter) {
>  		case HWTSTAMP_FILTER_NONE:
> -			config.rx_filter = HWTSTAMP_FILTER_NONE;
> +			config->rx_filter = HWTSTAMP_FILTER_NONE;
>  			break;
>  		default:
>  			/* PTP v1, UDP, any kind of event packet */
> -			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
> +			config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
>  			break;
>  		}
>  	}
> -	priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
> -	priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
> +	priv->hwts_rx_en = ((config->rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
> +	priv->hwts_tx_en = config->tx_type == HWTSTAMP_TX_ON;
>  
>  	if (!priv->hwts_tx_en && !priv->hwts_rx_en)
>  		stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
> @@ -765,7 +765,28 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
>  				(u32)now.tv_sec, now.tv_nsec);
>  	}
>  
> -	return copy_to_user(ifr->ifr_data, &config,
> +	return copy_to_user(ifr->ifr_data, config,
> +			    sizeof(struct hwtstamp_config)) ? -EFAULT : 0;

sizeof(*config) also.

> +}
> +
> +/**
> + *  stmmac_hwtstamp_get - read hardware timestamping.
> + *  @dev: device pointer.
> + *  @ifr: An IOCTL specific structure, that can contain a pointer to
> + *  a proprietary structure used to pass information to the driver.
> + *  Description:
> + *  This function obtain the current hardware timestamping settings
> +    as requested.
> + */
> +static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
> +{
> +	struct stmmac_priv *priv = netdev_priv(dev);
> +	struct hwtstamp_config *config = &priv->tstamp_config;
> +
> +	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
> +		return -EOPNOTSUPP;
> +
> +	return copy_to_user(ifr->ifr_data, config,
>  			    sizeof(struct hwtstamp_config)) ? -EFAULT : 0;

sizeof(*config).

>  }
>  
> @@ -3767,7 +3788,9 @@ static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
>  		ret = phy_mii_ioctl(dev->phydev, rq, cmd);
>  		break;
>  	case SIOCSHWTSTAMP:
> -		ret = stmmac_hwtstamp_ioctl(dev, rq);
> +		ret = stmmac_hwtstamp_set(dev, rq);

Missing break.

Thanks,
Jose Miguel Abreu

> +	case SIOCGHWTSTAMP:
> +		ret = stmmac_hwtstamp_get(dev, rq);
>  		break;
>  	default:
>  		break;
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ