|
| 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