hulk inclusion category: featrue bugzilla: https://atomgit.com/openeuler/kernel/issues/7116 -------------------------------- Add ipv4-mapped ipv6 addr support for hisock. Signed-off-by: Pu Lehui <pulehui@huawei.com> --- samples/bpf/hisock/bpf.c | 54 +++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/samples/bpf/hisock/bpf.c b/samples/bpf/hisock/bpf.c index b76bba218304..777d1c19d38f 100644 --- a/samples/bpf/hisock/bpf.c +++ b/samples/bpf/hisock/bpf.c @@ -82,6 +82,12 @@ static inline bool is_speed_flow(u16 port) return false; } +static inline bool is_ipv6_addr_mapped(u32 *addr6) +{ + return addr6[0] == 0 && addr6[1] == 0 && + addr6[2] == bpf_htonl(0x0000ffff); +} + static inline unsigned long parse_ingress_dev(struct bpf_sock_ops *skops) { struct sk_buff *skb; @@ -120,13 +126,14 @@ static void handle_listen_cb(struct bpf_sock_ops *skops) } } -static void handle_establish_inet_cb(struct bpf_sock_ops *skops) +static inline void +handle_establish_cb(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; struct sock_value val = { 0 }; - key.saddr = skops->remote_ip4; - key.daddr = skops->local_ip4; + key.saddr = raddr; + key.daddr = laddr; key.sport = bpf_ntohl(skops->remote_port); key.dport = skops->local_port; @@ -138,7 +145,18 @@ static void handle_establish_inet_cb(struct bpf_sock_ops *skops) bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG); } -static void handle_terminate_inet_cb(struct bpf_sock_ops *skops) +static void handle_establish_inet_cb(struct bpf_sock_ops *skops) +{ + handle_establish_cb(skops, skops->local_ip4, skops->remote_ip4); +} + +static void handle_establish_inet6_cb(struct bpf_sock_ops *skops) +{ + handle_establish_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); +} + +static inline void +handle_terminate_cb(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; @@ -147,8 +165,8 @@ static void handle_terminate_inet_cb(struct bpf_sock_ops *skops) skops->args[1] != BPF_TCP_CLOSE) return; - key.saddr = skops->remote_ip4; - key.daddr = skops->local_ip4; + key.saddr = raddr; + key.daddr = laddr; key.sport = bpf_ntohl(skops->remote_port); key.dport = skops->local_port; @@ -158,6 +176,16 @@ static void handle_terminate_inet_cb(struct bpf_sock_ops *skops) skops->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_STATE_CB_FLAG); } +static void handle_terminate_inet_cb(struct bpf_sock_ops *skops) +{ + handle_terminate_cb(skops, skops->local_ip4, skops->remote_ip4); +} + +static void handle_terminate_inet6_cb(struct bpf_sock_ops *skops) +{ + handle_terminate_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); +} + SEC("hisock_sockops") int hisock_sockops_prog(struct bpf_sock_ops *skops) { @@ -179,6 +207,20 @@ int hisock_sockops_prog(struct bpf_sock_ops *skops) default: break; } + } else if (skops->family == AF_INET6 && + is_ipv6_addr_mapped(skops->local_ip6)) { + switch (skops->op) { + case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: + if (!is_speed_flow(skops->local_port)) + break; + handle_establish_inet6_cb(skops); + break; + case BPF_SOCK_OPS_STATE_CB: + handle_terminate_inet6_cb(skops); + break; + default: + break; + } } return 1; -- 2.34.1