diff mbox series

[2/3,RFC] bpf: allow non-TCP skbs for bpf_sock_ops_enable_tx_tstamp

Message ID 4c44029f32c3e6d5e3190e1f5687a604ebeafdff.1743337403.git.pav@iki.fi (mailing list archive)
State New
Headers show
Series bpf: TSTAMP_COMPLETION_CB timestamping + enable it for Bluetooth | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/SubjectPrefix fail "Bluetooth: " prefix is not specified in the subject

Commit Message

Pauli Virtanen March 30, 2025, 12:23 p.m. UTC
Change bpf_sock_ops_enable_tx_tstamp() kfunc to only set SKBTX_BPF flag,
so that it can be used also for non-TCP skbs.  Do not set TCP-specific
fields if the socket is not TCP.

***

Doing it this way requires a valid tskey is set by the socket family,
before BPF_SOCK_OPS_TSTAMP_SENDMSG_CB.  Alternatively, it maybe could be
hardcoded per socket type here, or some new proto_ops added.
---
 net/core/filter.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/net/core/filter.c b/net/core/filter.c
index 46ae8eb7a03c..1300b0ef3620 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -12127,6 +12127,7 @@  __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,
 __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
 					      u64 flags)
 {
+	struct sock *sk;
 	struct sk_buff *skb;
 
 	if (skops->op != BPF_SOCK_OPS_TSTAMP_SENDMSG_CB)
@@ -12135,10 +12136,17 @@  __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
 	if (flags)
 		return -EINVAL;
 
+	sk = skops->sk;
+	if (!sk)
+		return -EINVAL;
+
 	skb = skops->skb;
 	skb_shinfo(skb)->tx_flags |= SKBTX_BPF;
-	TCP_SKB_CB(skb)->txstamp_ack |= TSTAMP_ACK_BPF;
-	skb_shinfo(skb)->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
+
+	if (sk_is_tcp(sk)) {
+		TCP_SKB_CB(skb)->txstamp_ack |= TSTAMP_ACK_BPF;
+		skb_shinfo(skb)->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
+	}
 
 	return 0;
 }