Subject: [PATCH net-next v4 3/4] net-timestamp: extend SOF_TIMESTAMPING_OPT_RX_FILTER for hardware use

From: Jason Xing <[email-masked]>
Date: Thu, 5 Sep 2024 15:17:36 +0800

From: Jason Xing <[email-masked]>

In the previous patch, we found things could happen in the rx software
timestamp. Here, we also noticed that, for rx hardware timestamp case,
it could happen when one process enables the rx hardware timestamp
generating flag first, then another process only setting
SOF_TIMESTAMPING_RAW_HARDWARE report flag can still get the hardware
timestamp.

In this patch, we extend the OPT_RX_FILTER flag to filter out the
above case for hardware use.

Suggested-by: Jakub Kicinski <[email-masked]>
Signed-off-by: Jason Xing <[email-masked]>
---
Link: https://lore.kernel.org/all/[email-masked]/
---
 Documentation/networking/timestamping.rst | 15 +++++++++------
 net/core/sock.c                           |  5 +++--
 net/ipv4/tcp.c                            |  3 ++-
 net/socket.c                              |  3 ++-
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/Documentation/networking/timestamping.rst b/Documentation/networking/timestamping.rst
index ac57d9de2f11..55e79ea71f3e 100644
--- a/Documentation/networking/timestamping.rst
+++ b/Documentation/networking/timestamping.rst
@@ -268,12 +268,15 @@ SOF_TIMESTAMPING_OPT_TX_SWHW:
   each containing just one timestamp.
 
 SOF_TIMESTAMPING_OPT_RX_FILTER:
-  Used in the receive software timestamp. Enabling the flag along with
-  SOF_TIMESTAMPING_SOFTWARE will not report the rx timestamp to the
-  userspace so that it can filter out the case where one process starts
-  first which turns on netstamp_needed_key through setting generation
-  flags like SOF_TIMESTAMPING_RX_SOFTWARE, then another one only passing
-  SOF_TIMESTAMPING_SOFTWARE report flag could also get the rx timestamp.
+  Used in the receive software/hardware timestamp. Enabling the flag
+  along with SOF_TIMESTAMPING_SOFTWARE/SOF_TIMESTAMPING_RAW_HARDWARE
+  will not report the rx timestamp to the userspace so that it can
+  filter out the cases where 1) one process starts first which turns
+  on netstamp_needed_key through setting generation flags like
+  SOF_TIMESTAMPING_RX_SOFTWARE, or 2) similarly one process enables
+  generating the hardware timestamp already, then another one only
+  passing SOF_TIMESTAMPING_SOFTWARE report flag could also get the
+  rx timestamp.
 
   SOF_TIMESTAMPING_OPT_RX_FILTER prevents the application from being
   influenced by others and let the application choose whether to report
diff --git a/net/core/sock.c b/net/core/sock.c
index 6a93344e21cf..dc4a43cfff59 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -908,8 +908,9 @@ int sock_set_timestamping(struct sock *sk, int optname,
 	    !(val & SOF_TIMESTAMPING_OPT_ID))
 		return -EINVAL;
 
-	if (val & SOF_TIMESTAMPING_RX_SOFTWARE &&
-	    val & SOF_TIMESTAMPING_OPT_RX_FILTER)
+	if (val & SOF_TIMESTAMPING_OPT_RX_FILTER &&
+	    (val & SOF_TIMESTAMPING_RX_SOFTWARE ||
+	     val & SOF_TIMESTAMPING_RX_HARDWARE))
 		return -EINVAL;
 
 	if (val & SOF_TIMESTAMPING_OPT_ID &&
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index a0c57c8b77bd..23f0722aa801 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2283,7 +2283,8 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
 	}
 
 	if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) {
-		if (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)
+		if (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
+		    !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))
 			has_timestamping = true;
 		else
 			tss->ts[2] = (struct timespec64) {0};
diff --git a/net/socket.c b/net/socket.c
index f8609d649ed3..bfbae2069fbb 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -952,7 +952,8 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 	    ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
 		empty = 0;
 	if (shhwtstamps &&
-	    (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
+	    (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
+	    !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER)) &&
 	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
 		if_index = 0;
 		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-- 
2.37.3