From a33e902409d3728ed727c772941851fbab4e36ba Mon Sep 17 00:00:00 2001 From: Integral Date: Tue, 17 Sep 2024 23:22:49 +0800 Subject: [PATCH 1/2] refactor: reuse existing functions & format code --- control/kern/tproxy.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/control/kern/tproxy.c b/control/kern/tproxy.c index 81bfa8d45a..1cbb7b78b9 100644 --- a/control/kern/tproxy.c +++ b/control/kern/tproxy.c @@ -1639,17 +1639,23 @@ int tproxy_dae0_ingress(struct __sk_buff *skb) struct redirect_tuple redirect_tuple = {}; if (skb->protocol == bpf_htons(ETH_P_IP)) { - bpf_skb_load_bytes(skb, ETH_HLEN + offsetof(struct iphdr, daddr), + bpf_skb_load_bytes(skb, + ETH_HLEN + offsetof(struct iphdr, daddr), &redirect_tuple.sip.u6_addr32[3], sizeof(redirect_tuple.sip.u6_addr32[3])); - bpf_skb_load_bytes(skb, ETH_HLEN + offsetof(struct iphdr, saddr), + bpf_skb_load_bytes(skb, + ETH_HLEN + offsetof(struct iphdr, saddr), &redirect_tuple.dip.u6_addr32[3], sizeof(redirect_tuple.dip.u6_addr32[3])); } else { - bpf_skb_load_bytes(skb, ETH_HLEN + offsetof(struct ipv6hdr, daddr), - &redirect_tuple.sip, sizeof(redirect_tuple.sip)); - bpf_skb_load_bytes(skb, ETH_HLEN + offsetof(struct ipv6hdr, saddr), - &redirect_tuple.dip, sizeof(redirect_tuple.dip)); + bpf_skb_load_bytes(skb, + ETH_HLEN + offsetof(struct ipv6hdr, daddr), + &redirect_tuple.sip, + sizeof(redirect_tuple.sip)); + bpf_skb_load_bytes(skb, + ETH_HLEN + offsetof(struct ipv6hdr, saddr), + &redirect_tuple.dip, + sizeof(redirect_tuple.dip)); } struct redirect_entry *redirect_entry = bpf_map_lookup_elem(&redirect_track, &redirect_tuple); @@ -1871,12 +1877,7 @@ int local_tcp_sockops(struct bpf_sock_ops *skops) case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: // dae sockets { struct tuples_key rev_tuple = {}; - - rev_tuple.l4proto = IPPROTO_TCP; - rev_tuple.sport = tuple.dport; - rev_tuple.dport = tuple.sport; - __builtin_memcpy(&rev_tuple.sip, &tuple.dip, IPV6_BYTE_LENGTH); - __builtin_memcpy(&rev_tuple.dip, &tuple.sip, IPV6_BYTE_LENGTH); + copy_reversed_tuples(&tuple, &rev_tuple); struct routing_result *routing_result; From 231dd8f35ee86197ac5dfd716debc01a209ae0bc Mon Sep 17 00:00:00 2001 From: Integral Date: Tue, 17 Sep 2024 23:29:53 +0800 Subject: [PATCH 2/2] fix lint --- control/kern/tproxy.c | 1 + 1 file changed, 1 insertion(+) diff --git a/control/kern/tproxy.c b/control/kern/tproxy.c index 1cbb7b78b9..370e4f1d76 100644 --- a/control/kern/tproxy.c +++ b/control/kern/tproxy.c @@ -1877,6 +1877,7 @@ int local_tcp_sockops(struct bpf_sock_ops *skops) case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: // dae sockets { struct tuples_key rev_tuple = {}; + copy_reversed_tuples(&tuple, &rev_tuple); struct routing_result *routing_result;