Ubuntu run xdp drop world

本文介绍了如何在不升级Linux内核的情况下,通过BPF(BerkeleyPacketFilter)在XDP(eXpressDataPath)中编写自定义规则,如丢弃所有网络数据包。文章涉及的命令和工具包括apt安装特定包、BPF宏定义、编译及验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sudo apt install clang llvm libelf-dev libpcap-dev gcc-multilib build-essential
# kernal 升级内核,不想升级,可以跳过
sudo apt install linux-tools-$(uname -r)
sudo apt install linux-headers-$(uname -r)
# tool
sudo apt install linux-tools-common linux-tools-generic
sudo apt install tcpdump
vim xdp-drop-world.c
...
#include <linux/bpf.h>

/*
 * Comments from Linux Kernel:
 * Helper macro to place programs, maps, license in
 * different sections in elf_bpf file. Section names
 * are interpreted by elf_bpf loader.
 * End of comments

 * You can either use the helper header file below
 * so that you don't need to define it yourself:
 * #include <bpf/bpf_helpers.h> 
 */
#define SEC(NAME) __attribute__((section(NAME), used))

SEC("xdp")
int xdp_drop_the_world(struct xdp_md *ctx) {
    // drop everything
	// 意思是无论什么网络数据包,都drop丢弃掉
    return XDP_DROP;
}

char _license[] SEC("license") = "GPL";
...
clang -O2 -target bpf -c xdp-drop-world.c -o xdp-drop-world.o
readelf -a xdp-drop-world.o
llvm-objdump -S xdp-drop-world.o
ip a
ip link set dev ens33 xdp obj xdp-drop-world.o sec xdp verbose
// now your pc said goodbye world
// come back command
ip link set dev ens33 xdp off

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值