netxfw 支持通过 eBPF Tail Call 机制动态加载第三方插件。这允许开发者在不修改或重新编译核心防火墙代码的情况下,扩展自定义的数据包处理逻辑。
netxfw 的主 XDP 程序在提取完数据包基本信息后,会尝试跳转到一个名为 jmp_table 的 BPF_MAP_TYPE_PROG_ARRAY。
jmp_table 的 2 到 15 号索引位。bpf_tail_call 返回主程序的协议处理器,或直接返回 XDP_PASS/XDP_DROP。clang 和 llvm。netxfw 的 BPF 头文件。创建一个 .c 文件(例如 my_filter.bpf.c):
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "include/plugin.h"
SEC("xdp")
int my_custom_filter(struct xdp_md *ctx) {
// 你的逻辑
// 例如:拦截特定特征的数据包
// 如果想让数据包继续流向 netxfw 核心逻辑
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
使用 netxfw 提供的 Makefile:
make plugins
编译产物将位于 bpf/plugins/out/ 目录下。
使用 netxfw 命令行工具动态管理插件:
将编译好的 .o 文件加载到指定的跳转表索引(例如索引 2):
sudo netxfw plugin load ./bpf/plugins/out/my_filter.o 2
目前可以通过 bpftool 查看 jmp_table 的内容,或通过 netxfw system status 查看统计信息。
sudo netxfw plugin remove 2