From cb3649cda6826684edf6c9faf70d56a276ca991e Mon Sep 17 00:00:00 2001 From: Eric Long Date: Tue, 8 Oct 2024 19:55:38 +0800 Subject: [PATCH] bpf: move random padding into function --- bpf/egress.c | 3 +-- bpf/ingress.c | 4 +--- common/defs.h | 4 ++++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bpf/egress.c b/bpf/egress.c index a209f3c..419a992 100644 --- a/bpf/egress.c +++ b/bpf/egress.c @@ -129,8 +129,7 @@ int egress_handler(struct __sk_buff* skb) { if (likely(conn->state == CONN_ESTABLISHED)) { seq = conn->seq; ack_seq = conn->ack_seq; - padding = - conn->settings.padding == PADDING_RANDOM ? (seq + ack_seq) % 11 : conn->settings.padding; + padding = conn_padding(conn, seq, ack_seq); conn->seq += payload_len + padding; } else { if (conn->state == CONN_IDLE) { diff --git a/bpf/ingress.c b/bpf/ingress.c index 63d07bb..effabcd 100644 --- a/bpf/ingress.c +++ b/bpf/ingress.c @@ -305,9 +305,7 @@ int ingress_handler(struct xdp_md* xdp) { } if (will_drop) return XDP_DROP; - __u32 padding = conn->settings.padding == PADDING_RANDOM - ? (ntohl(tcp->seq) + ntohl(tcp->ack_seq)) % 11 - : conn->settings.padding; + __u32 padding = conn_padding(conn, ntohl(tcp->seq), ntohl(tcp->ack)); size_t reserve_len = TCP_UDP_HEADER_DIFF + padding; if (ipv4) { __be16 old_len = ipv4->tot_len; diff --git a/common/defs.h b/common/defs.h index d1ac961..8811cee 100644 --- a/common/defs.h +++ b/common/defs.h @@ -307,6 +307,10 @@ static __always_inline __u32 conn_cooldown_display(struct connection* conn) { return conn->initiator ? conn_cooldown(conn) : 0; } +static __always_inline __u32 conn_padding(struct connection* conn, __u32 seq, __u32 ack_seq) { + return conn->settings.padding == PADDING_RANDOM ? (seq + ack_seq) % 11 : conn->settings.padding; +} + static __always_inline int time_diff_sec(__u64 a, __u64 b) { if (a <= b) return 0; if ((a - b) % SECOND < SECOND / 2)