Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4429078
  • 博文数量: 252
  • 博客积分: 5347
  • 博客等级: 大校
  • 技术积分: 13838
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-30 10:13
文章分类
文章存档

2022年(12)

2017年(11)

2016年(7)

2015年(14)

2014年(20)

2012年(9)

2011年(20)

2010年(153)

2009年(6)

分类: LINUX

2022-04-19 18:54:29

本篇文章参考《Linux  Observability with BPF》中第7章的例子,主要功能是借助于ip命令作为前端,对其他主机访问tcp8000端口进行限制,这里需要使用较新版本的iproute2软件工具包. 

1. 下载编译iproute2 工具包,使用最新的ip命令,支持配置xdp

   git clone git://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git 

 在编译iproute2时,需要开启支持libbpf的选项信息,在iproute2目录下配置使用下面的配置选项信息 

  ./configure --libbpf_force=on --LIBBPF_DIR=/usr/local/lib64

执行上面的命令,可能出现下面的错误信息,

确定libbpfpk-config配置文件位置,/usr/local/lib64/pkgconfig/libbpf.pc  使用

export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/ 导入libbpf的配置文件,

使用pkg-config --list-all | grep libbpf 查看是否配置libbpf

然后执行,make && make install 进行安装。

2. 编写bpf程序

       bpf程序对于访问本机的tcp的协议的8000端口进行限制,

点击(此处)折叠或打开

  1. #include <linux/bpf.h>

  2. #include <linux/if_ether.h>

  3. #include <linux/in.h>

  4. #include <linux/ip.h>

  5. #include <linux/if_packet.h>

  6. #include <bpf/bpf_helpers.h>

  7. #include <linux/if_vlan.h>

  8. #include <linux/types.h>

  9. #include <linux/tcp.h>

  10. #include <linux/udp.h>

  11. #include <bpf/bpf_helpers.h>

  12. #include <bpf/bpf_endian.h>



  13. static __always_inline int get_dport(void *trans_data, void *data_end, int protocol)

  14. {

  15.     struct tcphdr *th;

  16.     struct udphdr *uh;


  17.     switch (protocol) {

  18.         case IPPROTO_TCP:

  19.             th = (struct tcphdr *)trans_data;

  20.             if ((void*)(th + 1) > data_end)

  21.                 return -1;

  22.             return th->dest;

  23.         case IPPROTO_UDP:

  24.             uh = (struct udphdr *)trans_data;

  25.             if ((void *)(uh + 1) > data_end)

  26.                 return -1;

  27.             return uh->dest;

  28.         default:

  29.             return 0;

  30.     }


  31. }


  32. SEC("mysection")

  33. int myprogram(struct xdp_md *ctx) {

  34.   void *data = (void *)(long)ctx->data;

  35.   void *data_end = (void *)(long)ctx->data_end;

  36.   char fmt[] = "source = %d \n";

  37.   struct iphdr *ip;

  38.   int dport;

  39.   int hdport;

  40.   struct ethhdr *eth = data;

  41.   struct iphdr *iph = data + sizeof(struct ethhdr);


  42.     if ((void *)(iph + 1) > data_end) {

  43.         return XDP_DROP;

  44.     }


  45.   dport = get_dport(iph + 1, data_end,iph->protocol);


  46.   if (dport == -1 || dport == bpf_htons(8000)) {

  47.         bpf_trace_printk(fmt,sizeof(fmt),bpf_ntohs(dport));

  48.       return XDP_DROP;

  49.   }


  50.   return XDP_PASS;

  51. }

  52. char _license [] SEC ("license") = "GPL";


使用下面的命令进行编译:clang -g -c -O2 -target bpf -c program.c -o program.o

编译完成后,使用下面的ip命令对某个网卡进行价值xdp文件。

ip link set dev eth0 xdp obj program.o sec mysection   

通过上面的命令加载后,在接口上出现加载的xdp的类型和ID,表明加载成功。


使用python3 -m http.server在本地主机上其中http服务器,并监听8000端口。


使用另一台主机上使用nmap命令扫描对方监听的端口。nmap -sS 10.9.4.222,扫描结果如下:


ip link set dev eth0 xdp off 关闭加载的xdp程序。再次使用nmap扫描



阅读(802) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~