This test is similar to the one with cgroup_skb programs but uses tc egress instead. Signed-off-by: Mahe Tardy --- .../bpf/prog_tests/icmp_send_kfunc.c | 25 ++++++++ tools/testing/selftests/bpf/progs/icmp_send.c | 60 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c b/tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c index 1d6900d6a8f8..51f809ea6896 100644 --- a/tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c +++ b/tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c @@ -176,3 +176,28 @@ void test_icmp_send_unreach_cgroup(void) icmp_send__destroy(skel); close(cgroup_fd); } + +void test_icmp_send_unreach_tc(void) +{ + LIBBPF_OPTS(bpf_tcx_opts, opts); + struct icmp_send *skel; + struct bpf_link *link = NULL; + + skel = icmp_send__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + goto cleanup; + + link = bpf_program__attach_tcx(skel->progs.tc_egress, 1, &opts); + if (!ASSERT_OK_PTR(link, "prog_attach")) + goto cleanup; + + if (test__start_subtest("ipv4")) + run_icmp_test(skel, AF_INET, "127.0.0.1", NR_ICMP_UNREACH); + + if (test__start_subtest("ipv6")) + run_icmp_test(skel, AF_INET6, "::1", ICMPV6_REJECT_ROUTE); + +cleanup: + bpf_link__destroy(link); + icmp_send__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/icmp_send.c b/tools/testing/selftests/bpf/progs/icmp_send.c index 6e1ba539eeb0..5fa5467bdb70 100644 --- a/tools/testing/selftests/bpf/progs/icmp_send.c +++ b/tools/testing/selftests/bpf/progs/icmp_send.c @@ -2,6 +2,7 @@ #include "vmlinux.h" #include #include +#include "bpf_tracing_net.h" /* 127.0.0.1 in host byte order */ #define SERVER_IP 0x7F000001 @@ -65,4 +66,63 @@ int egress(struct __sk_buff *skb) return SK_DROP; } +SEC("tc/egress") +int tc_egress(struct __sk_buff *skb) +{ + void *data = (void *)(long)skb->data; + void *data_end = (void *)(long)skb->data_end; + struct ethhdr *eth; + struct iphdr *iph; + struct ipv6hdr *ip6h; + struct tcphdr *tcph; + + eth = data; + if ((void *)(eth + 1) > data_end) + return TCX_PASS; + + if (eth->h_proto == bpf_htons(ETH_P_IP)) { + iph = (void *)(eth + 1); + if ((void *)(iph + 1) > data_end) + return TCX_PASS; + + if (iph->protocol != IPPROTO_TCP || + iph->daddr != bpf_htonl(SERVER_IP)) + return TCX_PASS; + + tcph = (void *)iph + iph->ihl * 4; + if ((void *)(tcph + 1) > data_end) + return TCX_PASS; + + if (tcph->dest != bpf_htons(server_port)) + return TCX_PASS; + + } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) { + ip6h = (void *)(eth + 1); + if ((void *)(ip6h + 1) > data_end) + return TCX_PASS; + + if (ip6h->nexthdr != IPPROTO_TCP) + return TCX_PASS; + + if (ip6h->daddr.in6_u.u6_addr32[0] != 0 || + ip6h->daddr.in6_u.u6_addr32[1] != 0 || + ip6h->daddr.in6_u.u6_addr32[2] != 0 || + ip6h->daddr.in6_u.u6_addr32[3] != bpf_htonl(SERVER_IP6_LO)) + return TCX_PASS; + + tcph = (void *)(ip6h + 1); + if ((void *)(tcph + 1) > data_end) + return TCX_PASS; + + if (tcph->dest != bpf_htons(server_port)) + return TCX_PASS; + } else { + return TCX_PASS; + } + + kfunc_ret = bpf_icmp_send(skb, unreach_type, unreach_code); + + return TCX_DROP; +} + char LICENSE[] SEC("license") = "Dual BSD/GPL"; -- 2.34.1