gpt4 book ai didi

xdp-bpf - 如何使用XDP在NIC和WIFI之间转发数据包

转载 作者:行者123 更新时间:2023-12-05 06:19:29 64 4
gpt4 key购买 nike

我正在尝试重定向 NIC 和 WIFI 之间的流量。我正在尝试从 eth0 转发数据包,通过 wlan0 发送偶数数据包,通过 wlan1 发送奇数数据包。

我无法成功地将数据包从一个接口(interface)重定向到另一个接口(interface),除非这些接口(interface)是虚拟的(例如在 xdp-tutorial 中创建的接口(interface))。

是否有一个简单的例子可以将入口数据包从 eth0 的 MAC 28:f1:f1:f1:f1:f1 重定向到 wlan0 的 MAC e4:f1:f1:f1:f1:f1? (示例 MAC)因此,如果我通过以太网端口连接第二台计算机(假设路由正确)并 ping 8.8.8.8,它会通过 wlan0 发送数据包吗?

在这方面,我将不胜感激。

编辑:

我使用的代码来自xdp-tutorial

逐步设置:

# Mount map directory
sudo mount -t bpf bpf /sys/fs/bpf/

# Load the programs
sudo ./xdp_loader -d eth0 -S --filename xdp_prog_kern_03.o --progsec xdp_redirect_map -F
sudo ./xdp_loader -d wlan0 -S --filename xdp_prog_kern_03.o --progsec xdp_pass -F

# Set up redirect map
sudo ./xdp_prog_user -d eth0 -r wlan0 --src-mac 28:f1:f1:f1:f1:f1 --dest-mac e4:f1:f1:f1:f1:f1

最后一条命令输出:

map dir: /sys/fs/bpf/eth0
redirect from ifnum=5 to ifnum=3
forward: 28:f1:f1:f1:f1:f1 -> e4:f1:f1:f1:f1:f1

相关的BPF代码是

struct bpf_map_def SEC("maps") tx_port = {
.type = BPF_MAP_TYPE_DEVMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 256,
};

struct bpf_map_def SEC("maps") redirect_params = {
.type = BPF_MAP_TYPE_HASH,
.key_size = ETH_ALEN,
.value_size = ETH_ALEN,
.max_entries = 1,
};

SEC("xdp_redirect_map")
int xdp_redirect_map_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct hdr_cursor nh;
struct ethhdr *eth;
int eth_type;
int action = XDP_PASS;
unsigned char *dst;

/* These keep track of the next header type and iterator pointer */
nh.pos = data;

/* Parse Ethernet and IP/IPv6 headers */
eth_type = parse_ethhdr(&nh, data_end, &eth);
if (eth_type == -1)
return xdp_stats_record_action(ctx, XDP_DROP);

/* Do we know where to redirect this packet? */
dst = bpf_map_lookup_elem(&redirect_params, eth->h_source);

if (!dst)
goto out;

/* Set a proper destination address */
memcpy(eth->h_dest, dst, ETH_ALEN);

action = bpf_redirect_map(&tx_port, 0, 0);

out:
return xdp_stats_record_action(ctx, action);
}

SEC("xdp_pass")
int xdp_pass_func(struct xdp_md *ctx)
{
return xdp_stats_record_action(ctx, XDP_PASS);
}

map 的内容是:

# Output of redirect_params
sudo bpftool map dump id 33
key: 28 f1 f1 f1 f1 f1 value: e4 f1 f1 f1 f1 f1
Found 1 element

# Output of tx_port
key: 00 00 00 00 value: 03 00 00 00
key:
01 00 00 00
value:
No such file or directory

# And then the No such file or directory repeats with every key

最佳答案

截至目前,Linux Kernel 5.5 包含对 TUN 和 NIC 驱动程序的 bpf_prog_run_xdp 函数调用。

drivers/net/tun.c
drivers/net/ethernet
/broadcom/bnxt/bnxt_xdp.c, line 144
cavium/thunder/nicvf_main.c, line 559
freescale/dpaa2/dpaa2-eth.c, line 305
intel/i40e/i40e_txrx.c, line 2212
intel/ice/ice_txrx.c, line 442
ixgbe/ixgbe_main.c, line 2213
marvell/mvneta.c, line 2099
mellanox/mlx4/en_rx.c, line 785
mellanox/mlx5/core/en/xdp.c, line 141
netronome/nfp/nfp_net_common.c, line 1917
qlogic/qede/qede_fp.c, line 1075
sfc/rx.c, line 700
socionext/netsec.c, line 890
ti/cpsw_priv.c, line 1337

因此,正如评论中所讨论的那样,似乎没有 XDP 本地调用 wifi 驱动程序。

注意:Linux 内核版本、自定义驱动程序或 ip 或 'bpftool` 的结果不会共享给调试。此外,我也无法找到允许 802MAC header 的 XDP 代码更改。

关于xdp-bpf - 如何使用XDP在NIC和WIFI之间转发数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60828999/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com