mirror of
https://github.com/ukui/kernel.git
synced 2026-03-09 10:07:04 -07:00
selftests/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 <maximmi@nvidia.com>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
Link: https://lore.kernel.org/r/20220615134847.3753567-5-maximmi@nvidia.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
committed by
Alexei Starovoitov
parent
33bf988504
commit
fb5cd0ce70
@@ -43,3 +43,4 @@ test_cpp
|
||||
*.tmp
|
||||
xdpxceiver
|
||||
xdp_redirect_multi
|
||||
xdp_synproxy
|
||||
|
||||
@@ -82,7 +82,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
|
||||
TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
|
||||
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
|
||||
test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
|
||||
xdpxceiver xdp_redirect_multi
|
||||
xdpxceiver xdp_redirect_multi xdp_synproxy
|
||||
|
||||
TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
|
||||
|
||||
@@ -504,6 +504,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
|
||||
cap_helpers.c
|
||||
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
|
||||
$(OUTPUT)/liburandom_read.so \
|
||||
$(OUTPUT)/xdp_synproxy \
|
||||
ima_setup.sh \
|
||||
$(wildcard progs/btf_dump_test_case_*.c)
|
||||
TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
|
||||
|
||||
#include <test_progs.h>
|
||||
#include <network_helpers.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define CMD_OUT_BUF_SIZE 1023
|
||||
|
||||
#define SYS(cmd) ({ \
|
||||
if (!ASSERT_OK(system(cmd), (cmd))) \
|
||||
goto out; \
|
||||
})
|
||||
|
||||
#define SYS_OUT(cmd) ({ \
|
||||
FILE *f = popen((cmd), "r"); \
|
||||
if (!ASSERT_OK_PTR(f, (cmd))) \
|
||||
goto out; \
|
||||
f; \
|
||||
})
|
||||
|
||||
/* out must be at least `size * 4 + 1` bytes long */
|
||||
static void escape_str(char *out, const char *in, size_t size)
|
||||
{
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
if (isprint(in[i]) && in[i] != '\\' && in[i] != '\'') {
|
||||
*out++ = in[i];
|
||||
} else {
|
||||
*out++ = '\\';
|
||||
*out++ = 'x';
|
||||
*out++ = hex[(in[i] >> 4) & 0xf];
|
||||
*out++ = hex[in[i] & 0xf];
|
||||
}
|
||||
}
|
||||
*out++ = '\0';
|
||||
}
|
||||
|
||||
static bool expect_str(char *buf, size_t size, const char *str, const char *name)
|
||||
{
|
||||
static char escbuf_expected[CMD_OUT_BUF_SIZE * 4];
|
||||
static char escbuf_actual[CMD_OUT_BUF_SIZE * 4];
|
||||
static int duration = 0;
|
||||
bool ok;
|
||||
|
||||
ok = size == strlen(str) && !memcmp(buf, str, size);
|
||||
|
||||
if (!ok) {
|
||||
escape_str(escbuf_expected, str, strlen(str));
|
||||
escape_str(escbuf_actual, buf, size);
|
||||
}
|
||||
CHECK(!ok, name, "unexpected %s: actual '%s' != expected '%s'\n",
|
||||
name, escbuf_actual, escbuf_expected);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
void test_xdp_synproxy(void)
|
||||
{
|
||||
int server_fd = -1, client_fd = -1, accept_fd = -1;
|
||||
struct nstoken *ns = NULL;
|
||||
FILE *ctrl_file = NULL;
|
||||
char buf[CMD_OUT_BUF_SIZE];
|
||||
size_t size;
|
||||
|
||||
SYS("ip netns add synproxy");
|
||||
|
||||
SYS("ip link add tmp0 type veth peer name tmp1");
|
||||
SYS("ip link set tmp1 netns synproxy");
|
||||
SYS("ip link set tmp0 up");
|
||||
SYS("ip addr replace 198.18.0.1/24 dev tmp0");
|
||||
|
||||
/* When checksum offload is enabled, the XDP program sees wrong
|
||||
* checksums and drops packets.
|
||||
*/
|
||||
SYS("ethtool -K tmp0 tx off");
|
||||
/* Workaround required for veth. */
|
||||
SYS("ip link set tmp0 xdp object xdp_dummy.o section xdp 2> /dev/null");
|
||||
|
||||
ns = open_netns("synproxy");
|
||||
if (!ASSERT_OK_PTR(ns, "setns"))
|
||||
goto out;
|
||||
|
||||
SYS("ip link set lo up");
|
||||
SYS("ip link set tmp1 up");
|
||||
SYS("ip addr replace 198.18.0.2/24 dev tmp1");
|
||||
SYS("sysctl -w net.ipv4.tcp_syncookies=2");
|
||||
SYS("sysctl -w net.ipv4.tcp_timestamps=1");
|
||||
SYS("sysctl -w net.netfilter.nf_conntrack_tcp_loose=0");
|
||||
SYS("iptables -t raw -I PREROUTING \
|
||||
-i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack");
|
||||
SYS("iptables -t filter -A INPUT \
|
||||
-i tmp1 -p tcp -m tcp --dport 8080 -m state --state INVALID,UNTRACKED \
|
||||
-j SYNPROXY --sack-perm --timestamp --wscale 7 --mss 1460");
|
||||
SYS("iptables -t filter -A INPUT \
|
||||
-i tmp1 -m state --state INVALID -j DROP");
|
||||
|
||||
ctrl_file = SYS_OUT("./xdp_synproxy --iface tmp1 --ports 8080 --single \
|
||||
--mss4 1460 --mss6 1440 --wscale 7 --ttl 64");
|
||||
size = fread(buf, 1, sizeof(buf), ctrl_file);
|
||||
pclose(ctrl_file);
|
||||
if (!expect_str(buf, size, "Total SYNACKs generated: 0\n",
|
||||
"initial SYNACKs"))
|
||||
goto out;
|
||||
|
||||
server_fd = start_server(AF_INET, SOCK_STREAM, "198.18.0.2", 8080, 0);
|
||||
if (!ASSERT_GE(server_fd, 0, "start_server"))
|
||||
goto out;
|
||||
|
||||
close_netns(ns);
|
||||
ns = NULL;
|
||||
|
||||
client_fd = connect_to_fd(server_fd, 10000);
|
||||
if (!ASSERT_GE(client_fd, 0, "connect_to_fd"))
|
||||
goto out;
|
||||
|
||||
accept_fd = accept(server_fd, NULL, NULL);
|
||||
if (!ASSERT_GE(accept_fd, 0, "accept"))
|
||||
goto out;
|
||||
|
||||
ns = open_netns("synproxy");
|
||||
if (!ASSERT_OK_PTR(ns, "setns"))
|
||||
goto out;
|
||||
|
||||
ctrl_file = SYS_OUT("./xdp_synproxy --iface tmp1 --single");
|
||||
size = fread(buf, 1, sizeof(buf), ctrl_file);
|
||||
pclose(ctrl_file);
|
||||
if (!expect_str(buf, size, "Total SYNACKs generated: 1\n",
|
||||
"SYNACKs after connection"))
|
||||
goto out;
|
||||
|
||||
out:
|
||||
if (accept_fd >= 0)
|
||||
close(accept_fd);
|
||||
if (client_fd >= 0)
|
||||
close(client_fd);
|
||||
if (server_fd >= 0)
|
||||
close(server_fd);
|
||||
if (ns)
|
||||
close_netns(ns);
|
||||
|
||||
system("ip link del tmp0");
|
||||
system("ip netns del synproxy");
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,418 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
|
||||
|
||||
#include <stdnoreturn.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <bpf/bpf.h>
|
||||
#include <bpf/libbpf.h>
|
||||
#include <net/if.h>
|
||||
#include <linux/if_link.h>
|
||||
#include <linux/limits.h>
|
||||
|
||||
static unsigned int ifindex;
|
||||
static __u32 attached_prog_id;
|
||||
|
||||
static void noreturn cleanup(int sig)
|
||||
{
|
||||
DECLARE_LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
|
||||
int prog_fd;
|
||||
int err;
|
||||
|
||||
if (attached_prog_id == 0)
|
||||
exit(0);
|
||||
|
||||
prog_fd = bpf_prog_get_fd_by_id(attached_prog_id);
|
||||
if (prog_fd < 0) {
|
||||
fprintf(stderr, "Error: bpf_prog_get_fd_by_id: %s\n", strerror(-prog_fd));
|
||||
err = bpf_xdp_attach(ifindex, -1, 0, NULL);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_set_link_xdp_fd: %s\n", strerror(-err));
|
||||
fprintf(stderr, "Failed to detach XDP program\n");
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
opts.old_prog_fd = prog_fd;
|
||||
err = bpf_xdp_attach(ifindex, -1, XDP_FLAGS_REPLACE, &opts);
|
||||
close(prog_fd);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_set_link_xdp_fd_opts: %s\n", strerror(-err));
|
||||
/* Not an error if already replaced by someone else. */
|
||||
if (err != -EEXIST) {
|
||||
fprintf(stderr, "Failed to detach XDP program\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static noreturn void usage(const char *progname)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [--iface <iface>|--prog <prog_id>] [--mss4 <mss ipv4> --mss6 <mss ipv6> --wscale <wscale> --ttl <ttl>] [--ports <port1>,<port2>,...] [--single]\n",
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static unsigned long parse_arg_ul(const char *progname, const char *arg, unsigned long limit)
|
||||
{
|
||||
unsigned long res;
|
||||
char *endptr;
|
||||
|
||||
errno = 0;
|
||||
res = strtoul(arg, &endptr, 10);
|
||||
if (errno != 0 || *endptr != '\0' || arg[0] == '\0' || res > limit)
|
||||
usage(progname);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void parse_options(int argc, char *argv[], unsigned int *ifindex, __u32 *prog_id,
|
||||
__u64 *tcpipopts, char **ports, bool *single)
|
||||
{
|
||||
static struct option long_options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "iface", required_argument, NULL, 'i' },
|
||||
{ "prog", required_argument, NULL, 'x' },
|
||||
{ "mss4", required_argument, NULL, 4 },
|
||||
{ "mss6", required_argument, NULL, 6 },
|
||||
{ "wscale", required_argument, NULL, 'w' },
|
||||
{ "ttl", required_argument, NULL, 't' },
|
||||
{ "ports", required_argument, NULL, 'p' },
|
||||
{ "single", no_argument, NULL, 's' },
|
||||
{ NULL, 0, NULL, 0 },
|
||||
};
|
||||
unsigned long mss4, mss6, wscale, ttl;
|
||||
unsigned int tcpipopts_mask = 0;
|
||||
|
||||
if (argc < 2)
|
||||
usage(argv[0]);
|
||||
|
||||
*ifindex = 0;
|
||||
*prog_id = 0;
|
||||
*tcpipopts = 0;
|
||||
*ports = NULL;
|
||||
*single = false;
|
||||
|
||||
while (true) {
|
||||
int opt;
|
||||
|
||||
opt = getopt_long(argc, argv, "", long_options, NULL);
|
||||
if (opt == -1)
|
||||
break;
|
||||
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
break;
|
||||
case 'i':
|
||||
*ifindex = if_nametoindex(optarg);
|
||||
if (*ifindex == 0)
|
||||
usage(argv[0]);
|
||||
break;
|
||||
case 'x':
|
||||
*prog_id = parse_arg_ul(argv[0], optarg, UINT32_MAX);
|
||||
if (*prog_id == 0)
|
||||
usage(argv[0]);
|
||||
break;
|
||||
case 4:
|
||||
mss4 = parse_arg_ul(argv[0], optarg, UINT16_MAX);
|
||||
tcpipopts_mask |= 1 << 0;
|
||||
break;
|
||||
case 6:
|
||||
mss6 = parse_arg_ul(argv[0], optarg, UINT16_MAX);
|
||||
tcpipopts_mask |= 1 << 1;
|
||||
break;
|
||||
case 'w':
|
||||
wscale = parse_arg_ul(argv[0], optarg, 14);
|
||||
tcpipopts_mask |= 1 << 2;
|
||||
break;
|
||||
case 't':
|
||||
ttl = parse_arg_ul(argv[0], optarg, UINT8_MAX);
|
||||
tcpipopts_mask |= 1 << 3;
|
||||
break;
|
||||
case 'p':
|
||||
*ports = optarg;
|
||||
break;
|
||||
case 's':
|
||||
*single = true;
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
if (optind < argc)
|
||||
usage(argv[0]);
|
||||
|
||||
if (tcpipopts_mask == 0xf) {
|
||||
if (mss4 == 0 || mss6 == 0 || wscale == 0 || ttl == 0)
|
||||
usage(argv[0]);
|
||||
*tcpipopts = (mss6 << 32) | (ttl << 24) | (wscale << 16) | mss4;
|
||||
} else if (tcpipopts_mask != 0) {
|
||||
usage(argv[0]);
|
||||
}
|
||||
|
||||
if (*ifindex != 0 && *prog_id != 0)
|
||||
usage(argv[0]);
|
||||
if (*ifindex == 0 && *prog_id == 0)
|
||||
usage(argv[0]);
|
||||
}
|
||||
|
||||
static int syncookie_attach(const char *argv0, unsigned int ifindex)
|
||||
{
|
||||
struct bpf_prog_info info = {};
|
||||
__u32 info_len = sizeof(info);
|
||||
char xdp_filename[PATH_MAX];
|
||||
struct bpf_program *prog;
|
||||
struct bpf_object *obj;
|
||||
int prog_fd;
|
||||
int err;
|
||||
|
||||
snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv0);
|
||||
obj = bpf_object__open_file(xdp_filename, NULL);
|
||||
err = libbpf_get_error(obj);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_object__open_file: %s\n", strerror(-err));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = bpf_object__load(obj);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_object__open_file: %s\n", strerror(-err));
|
||||
return err;
|
||||
}
|
||||
|
||||
prog = bpf_object__find_program_by_name(obj, "syncookie_xdp");
|
||||
if (!prog) {
|
||||
fprintf(stderr, "Error: bpf_object__find_program_by_name: program syncookie_xdp was not found\n");
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
prog_fd = bpf_program__fd(prog);
|
||||
|
||||
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_obj_get_info_by_fd: %s\n", strerror(-err));
|
||||
goto out;
|
||||
}
|
||||
attached_prog_id = info.id;
|
||||
signal(SIGINT, cleanup);
|
||||
signal(SIGTERM, cleanup);
|
||||
err = bpf_xdp_attach(ifindex, prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST, NULL);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_set_link_xdp_fd: %s\n", strerror(-err));
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGTERM, SIG_DFL);
|
||||
attached_prog_id = 0;
|
||||
goto out;
|
||||
}
|
||||
err = 0;
|
||||
out:
|
||||
bpf_object__close(obj);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int syncookie_open_bpf_maps(__u32 prog_id, int *values_map_fd, int *ports_map_fd)
|
||||
{
|
||||
struct bpf_prog_info prog_info;
|
||||
__u32 map_ids[8];
|
||||
__u32 info_len;
|
||||
int prog_fd;
|
||||
int err;
|
||||
int i;
|
||||
|
||||
*values_map_fd = -1;
|
||||
*ports_map_fd = -1;
|
||||
|
||||
prog_fd = bpf_prog_get_fd_by_id(prog_id);
|
||||
if (prog_fd < 0) {
|
||||
fprintf(stderr, "Error: bpf_prog_get_fd_by_id: %s\n", strerror(-prog_fd));
|
||||
return prog_fd;
|
||||
}
|
||||
|
||||
prog_info = (struct bpf_prog_info) {
|
||||
.nr_map_ids = 8,
|
||||
.map_ids = (__u64)map_ids,
|
||||
};
|
||||
info_len = sizeof(prog_info);
|
||||
|
||||
err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_obj_get_info_by_fd: %s\n", strerror(-err));
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (prog_info.type != BPF_PROG_TYPE_XDP) {
|
||||
fprintf(stderr, "Error: BPF prog type is not BPF_PROG_TYPE_XDP\n");
|
||||
err = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
if (prog_info.nr_map_ids < 2) {
|
||||
fprintf(stderr, "Error: Found %u BPF maps, expected at least 2\n",
|
||||
prog_info.nr_map_ids);
|
||||
err = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (i = 0; i < prog_info.nr_map_ids; i++) {
|
||||
struct bpf_map_info map_info = {};
|
||||
int map_fd;
|
||||
|
||||
err = bpf_map_get_fd_by_id(map_ids[i]);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_map_get_fd_by_id: %s\n", strerror(-err));
|
||||
goto err_close_map_fds;
|
||||
}
|
||||
map_fd = err;
|
||||
|
||||
info_len = sizeof(map_info);
|
||||
err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_obj_get_info_by_fd: %s\n", strerror(-err));
|
||||
close(map_fd);
|
||||
goto err_close_map_fds;
|
||||
}
|
||||
if (strcmp(map_info.name, "values") == 0) {
|
||||
*values_map_fd = map_fd;
|
||||
continue;
|
||||
}
|
||||
if (strcmp(map_info.name, "allowed_ports") == 0) {
|
||||
*ports_map_fd = map_fd;
|
||||
continue;
|
||||
}
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
if (*values_map_fd != -1 && *ports_map_fd != -1) {
|
||||
err = 0;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = -ENOENT;
|
||||
|
||||
err_close_map_fds:
|
||||
if (*values_map_fd != -1)
|
||||
close(*values_map_fd);
|
||||
if (*ports_map_fd != -1)
|
||||
close(*ports_map_fd);
|
||||
*values_map_fd = -1;
|
||||
*ports_map_fd = -1;
|
||||
|
||||
out:
|
||||
close(prog_fd);
|
||||
return err;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int values_map_fd, ports_map_fd;
|
||||
__u64 tcpipopts;
|
||||
bool firstiter;
|
||||
__u64 prevcnt;
|
||||
__u32 prog_id;
|
||||
char *ports;
|
||||
bool single;
|
||||
int err = 0;
|
||||
|
||||
parse_options(argc, argv, &ifindex, &prog_id, &tcpipopts, &ports, &single);
|
||||
|
||||
if (prog_id == 0) {
|
||||
err = bpf_xdp_query_id(ifindex, 0, &prog_id);
|
||||
if (err < 0) {
|
||||
fprintf(stderr, "Error: bpf_get_link_xdp_id: %s\n", strerror(-err));
|
||||
goto out;
|
||||
}
|
||||
if (prog_id == 0) {
|
||||
err = syncookie_attach(argv[0], ifindex);
|
||||
if (err < 0)
|
||||
goto out;
|
||||
prog_id = attached_prog_id;
|
||||
}
|
||||
}
|
||||
|
||||
err = syncookie_open_bpf_maps(prog_id, &values_map_fd, &ports_map_fd);
|
||||
if (err < 0)
|
||||
goto out;
|
||||
|
||||
if (ports) {
|
||||
__u16 port_last = 0;
|
||||
__u32 port_idx = 0;
|
||||
char *p = ports;
|
||||
|
||||
fprintf(stderr, "Replacing allowed ports\n");
|
||||
|
||||
while (p && *p != '\0') {
|
||||
char *token = strsep(&p, ",");
|
||||
__u16 port;
|
||||
|
||||
port = parse_arg_ul(argv[0], token, UINT16_MAX);
|
||||
err = bpf_map_update_elem(ports_map_fd, &port_idx, &port, BPF_ANY);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
|
||||
fprintf(stderr, "Failed to add port %u (index %u)\n",
|
||||
port, port_idx);
|
||||
goto out_close_maps;
|
||||
}
|
||||
fprintf(stderr, "Added port %u\n", port);
|
||||
port_idx++;
|
||||
}
|
||||
err = bpf_map_update_elem(ports_map_fd, &port_idx, &port_last, BPF_ANY);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
|
||||
fprintf(stderr, "Failed to add the terminator value 0 (index %u)\n",
|
||||
port_idx);
|
||||
goto out_close_maps;
|
||||
}
|
||||
}
|
||||
|
||||
if (tcpipopts) {
|
||||
__u32 key = 0;
|
||||
|
||||
fprintf(stderr, "Replacing TCP/IP options\n");
|
||||
|
||||
err = bpf_map_update_elem(values_map_fd, &key, &tcpipopts, BPF_ANY);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
|
||||
goto out_close_maps;
|
||||
}
|
||||
}
|
||||
|
||||
if ((ports || tcpipopts) && attached_prog_id == 0 && !single)
|
||||
goto out_close_maps;
|
||||
|
||||
prevcnt = 0;
|
||||
firstiter = true;
|
||||
while (true) {
|
||||
__u32 key = 1;
|
||||
__u64 value;
|
||||
|
||||
err = bpf_map_lookup_elem(values_map_fd, &key, &value);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "Error: bpf_map_lookup_elem: %s\n", strerror(-err));
|
||||
goto out_close_maps;
|
||||
}
|
||||
if (firstiter) {
|
||||
prevcnt = value;
|
||||
firstiter = false;
|
||||
}
|
||||
if (single) {
|
||||
printf("Total SYNACKs generated: %llu\n", value);
|
||||
break;
|
||||
}
|
||||
printf("SYNACKs generated: %llu (total %llu)\n", value - prevcnt, value);
|
||||
prevcnt = value;
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
out_close_maps:
|
||||
close(values_map_fd);
|
||||
close(ports_map_fd);
|
||||
out:
|
||||
return err == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user