Skip to content

Commit 08450b8

Browse files
nvmmaxKernel Patches Daemon
authored andcommitted
bpf: Add selftests for raw syncookie helpers
This commit adds selftests for the new BPF helpers: bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6}. xdp_synproxy_kern.c is a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module. xdp_synproxy.c is a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL. A selftest is added to prog_tests that leverages the above programs to test the functionality of the new helpers. Signed-off-by: Maxim Mikityanskiy <[email protected]> Reviewed-by: Tariq Toukan <[email protected]>
1 parent a61f1d6 commit 08450b8

File tree

5 files changed

+1279
-1
lines changed

5 files changed

+1279
-1
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ test_cpp
4343
*.tmp
4444
xdpxceiver
4545
xdp_redirect_multi
46+
xdp_synproxy

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
8282
TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
8383
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
8484
test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
85-
xdpxceiver xdp_redirect_multi
85+
xdpxceiver xdp_redirect_multi xdp_synproxy
8686

8787
TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
8888

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include <network_helpers.h>
4+
5+
#define SYS(cmd) ({ \
6+
if (!ASSERT_OK(system(cmd), (cmd))) \
7+
goto out; \
8+
})
9+
10+
#define SYS_OUT(cmd) ({ \
11+
FILE *f = popen((cmd), "r"); \
12+
if (!ASSERT_OK_PTR(f, (cmd))) \
13+
goto out; \
14+
f; \
15+
})
16+
17+
static bool expect_str(char *buf, size_t size, const char *str)
18+
{
19+
if (size != strlen(str))
20+
return false;
21+
return !memcmp(buf, str, size);
22+
}
23+
24+
void test_xdp_synproxy(void)
25+
{
26+
int server_fd = -1, client_fd = -1, accept_fd = -1;
27+
struct nstoken *ns = NULL;
28+
FILE *ctrl_file = NULL;
29+
char buf[1024];
30+
size_t size;
31+
32+
SYS("ip netns add synproxy");
33+
34+
SYS("ip link add tmp0 type veth peer name tmp1");
35+
SYS("ip link set tmp1 netns synproxy");
36+
SYS("ip link set tmp0 up");
37+
SYS("ip addr replace 198.18.0.1/24 dev tmp0");
38+
39+
// When checksum offload is enabled, the XDP program sees wrong
40+
// checksums and drops packets.
41+
SYS("ethtool -K tmp0 tx off");
42+
// Workaround required for veth.
43+
SYS("ip link set tmp0 xdp object xdp_dummy.o section xdp 2> /dev/null");
44+
45+
ns = open_netns("synproxy");
46+
if (!ASSERT_OK_PTR(ns, "setns"))
47+
goto out;
48+
49+
SYS("ip link set lo up");
50+
SYS("ip link set tmp1 up");
51+
SYS("ip addr replace 198.18.0.2/24 dev tmp1");
52+
SYS("sysctl -w net.ipv4.tcp_syncookies=2");
53+
SYS("sysctl -w net.ipv4.tcp_timestamps=1");
54+
SYS("sysctl -w net.netfilter.nf_conntrack_tcp_loose=0");
55+
SYS("iptables -t raw -I PREROUTING \
56+
-i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack");
57+
SYS("iptables -t filter -A INPUT \
58+
-i tmp1 -p tcp -m tcp --dport 8080 -m state --state INVALID,UNTRACKED \
59+
-j SYNPROXY --sack-perm --timestamp --wscale 7 --mss 1460");
60+
SYS("iptables -t filter -A INPUT \
61+
-i tmp1 -m state --state INVALID -j DROP");
62+
63+
ctrl_file = SYS_OUT("./xdp_synproxy --iface tmp1 --ports 8080 --single \
64+
--mss4 1460 --mss6 1440 --wscale 7 --ttl 64");
65+
size = fread(buf, 1, sizeof(buf), ctrl_file);
66+
pclose(ctrl_file);
67+
if (!ASSERT_TRUE(expect_str(buf, size, "Total SYNACKs generated: 0\n"),
68+
"initial SYNACKs"))
69+
goto out;
70+
71+
server_fd = start_server(AF_INET, SOCK_STREAM, "198.18.0.2", 8080, 0);
72+
if (!ASSERT_GE(server_fd, 0, "start_server"))
73+
goto out;
74+
75+
close_netns(ns);
76+
ns = NULL;
77+
78+
client_fd = connect_to_fd(server_fd, 10000);
79+
if (!ASSERT_GE(client_fd, 0, "connect_to_fd"))
80+
goto out;
81+
82+
accept_fd = accept(server_fd, NULL, NULL);
83+
if (!ASSERT_GE(accept_fd, 0, "accept"))
84+
goto out;
85+
86+
ns = open_netns("synproxy");
87+
if (!ASSERT_OK_PTR(ns, "setns"))
88+
goto out;
89+
90+
ctrl_file = SYS_OUT("./xdp_synproxy --iface tmp1 --single");
91+
size = fread(buf, 1, sizeof(buf), ctrl_file);
92+
pclose(ctrl_file);
93+
if (!ASSERT_TRUE(expect_str(buf, size, "Total SYNACKs generated: 1\n"),
94+
"SYNACKs after connection"))
95+
goto out;
96+
97+
out:
98+
if (accept_fd >= 0)
99+
close(accept_fd);
100+
if (client_fd >= 0)
101+
close(client_fd);
102+
if (server_fd >= 0)
103+
close(server_fd);
104+
if (ns)
105+
close_netns(ns);
106+
107+
system("ip link del tmp0");
108+
system("ip netns del synproxy");
109+
}

0 commit comments

Comments
 (0)