netfilter_queue 使用示例

2024-02-24 04:18
文章标签 使用 示例 queue netfilter

本文主要是介绍netfilter_queue 使用示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 简介
  • 感谢
  • 环境依赖
  • 示例
  • 测试
    • 编译
    • 配置iptables规则
    • ping本机loopback地址
    • 启动nf-queue程序

简介

之前写过一篇文章《iptables queue 应用示例》,介绍使用libipq实现用户态数据包处理。最近有遇到国产麒麟系统不支持libipq的问题,好在还支持netfilter_queue,整理一个关于NFQUEUE的版本出来。

感谢

本文大部分内容参考_Raymond_的《netfilter_queue 总结》,作者对netfilter做了详细的介绍,原文链接:https://blog.csdn.net/weixin_43745072/article/details/110096900

环境依赖

需要安装libnetfilter_queue,libnetfilter_queue-devel,iptables,iptables-devel

yum install -y libnetfilter_queue libnetfilter_queue-devel iptables iptables-devel

示例

nf-queue.c

 /*     test.c      */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>		/* for NF_ACCEPT */
#include <errno.h> 
#include <libnetfilter_queue/libnetfilter_queue.h>/* returns packet id */
static u_int32_t print_pkt (struct nfq_data *tb)
{int id = 0;struct nfqnl_msg_packet_hdr *ph;struct nfqnl_msg_packet_hw *hwph;u_int32_t mark,ifi; int ret;unsigned char *data;// 提取数据包头信息,包括id,协议和hook点信息ph = nfq_get_msg_packet_hdr(tb);if (ph) {id = ntohl(ph->packet_id);printf("hw_protocol=0x%04x hook=%u id=%u ",ntohs(ph->hw_protocol), ph->hook, id);}hwph = nfq_get_packet_hw(tb);if (hwph) {int i, hlen = ntohs(hwph->hw_addrlen); printf("hw_src_addr=");for (i = 0; i < hlen-1; i++)printf("%02x:", hwph->hw_addr[i]);printf("%02x ", hwph->hw_addr[hlen-1]);}mark = nfq_get_nfmark(tb);if (mark)printf("mark=%u ", mark); ifi = nfq_get_indev(tb);if (ifi)printf("indev=%u ", ifi); ifi = nfq_get_outdev(tb);if (ifi)printf("outdev=%u ", ifi);ifi = nfq_get_physindev(tb);if (ifi)printf("physindev=%u ", ifi); ifi = nfq_get_physoutdev(tb);if (ifi)printf("physoutdev=%u ", ifi);// 获取数据包载荷,data指针指向载荷,从实际的IP头开始ret = nfq_get_payload(tb, &data);if (ret >= 0)printf("payload_len=%d ", ret); fputc('\n', stdout); return id;
}static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,struct nfq_data *nfa, void *data)
{printf("entering callback\n");//进入到回调函数u_int32_t id = print_pkt(nfa);/*** 函数功能:对一个数据包发表裁决。* 函数参数:qh:通过调用nfq_create_queue()获得的Netfilter队列句柄。id:由netfilter分配给数据包的IDverdict:决定返回到netfilterdata_len: buf缓冲区的字节数buf:包含数据包数据的缓冲区* 函数返回值:出错返回-1,否则返回值大于等于0。*/return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}int main(int argc, char **argv)
{struct nfq_handle *h;struct nfq_q_handle *qh;struct nfnl_handle *nh;int fd;int rv;char buf[4096] __attribute__ ((aligned)); printf("opening library handle\n");h = nfq_open();//创建 netfilter_queueif (!h) {//创建失败fprintf(stderr, "error during nfq_open()\n");exit(1);} printf("unbinding existing nf_queue handler for AF_INET (if any)\n");//解绑已经存在的队列if (nfq_unbind_pf(h, AF_INET) < 0) {fprintf(stderr, "error during nfq_unbind_pf()\n");exit(1);} printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");//绑定上我们创建的队列if (nfq_bind_pf(h, AF_INET) < 0) {fprintf(stderr, "error during nfq_bind_pf()\n");exit(1);} printf("binding this socket to queue '0'\n");//cb是回调函数qh = nfq_create_queue(h,  0, &cb, NULL);if (!qh) {fprintf(stderr, "error during nfq_create_queue()\n");exit(1);} printf("setting copy_packet mode\n");if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {//设置的包处理模式fprintf(stderr, "can't set packet_copy mode\n");exit(1);} fd = nfq_fd(h); for (;;) {if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {printf("pkt received\n");nfq_handle_packet(h, buf, rv);continue;}/* if your application is too slow to digest the packets that* are sent from kernel-space, the socket buffer that we use* to enqueue packets may fill up returning ENOBUFS. Depending* on your application, this error may be ignored. Please, see* the doxygen documentation of this library on how to improve* this situation.*/if (rv < 0 && errno == ENOBUFS) {printf("losing packets!\n");continue;}perror("recv failed");break;} printf("unbinding from queue 0\n");nfq_destroy_queue(qh);//摧毁队列,退出 #ifdef INSANE/* normally, applications SHOULD NOT issue this command, since* it detaches other programs/sockets from AF_INET, too ! */printf("unbinding from AF_INET\n");nfq_unbind_pf(h, AF_INET);
#endif printf("closing library handle\n");nfq_close(h); exit(0);
}
  • nfq_open():打开一个nfqueue的句柄。
struct nfq_handle *nfq_open(void);

参数
return:生成的句柄

  • nfq_close():关闭nfq_open()创建的句柄。
int nfq_close(struct nfq_handle *h);

参数
h:nfq_open()创建的句柄
return: 0 成功,非0失败

  • nfq_unbind_pf() 将给定队列连接句柄与处理中的属于某个特定协议家族的数据包解除绑定。
int nfq_unbind_pf(struct nfq_handle *h, uint16_t pf);

参数
h:nfq_open()创建的句柄
pf:将被解绑的协议家族
return:0 成功,非0失败

  • nfq_unbind_pf() 将给定队列连接句柄与处理中的属于某个特定协议家族的数据包进行绑定。
int nfq_bind_pf(struct nfq_handle *h, uint16_t pf);

参数:
h:nfq_open()创建的句柄
pf:将被绑定的协议家族
return:0 成功,非0失败

  • nfq_create_queue()
struct nfq_q_handle *nfq_create_queue(struct nfq_handle *h, uint16_t num, nfq_callback *cb, void *data);

参数:
h:nfq_open()创建的句柄
num带绑定的队列的编号
cb: 回调函数
data:传递给回调函数的参数
return:一个新创建的nfq_q_handle的指针

  • nfq_set_mode() 复制到用户的方式,丢弃,复制元数据,复制整个数据包
int nfq_set_mode(struct nfq_q_handle *qh, uint8_t mode, uint32_t range);

参数:
qh:nfq_create_queue()创建的句柄。
mode想要使用数据包的模式
NFQNL_COPY_NONE- 丢弃
NFQNL_COPY_META- 复制元数据
NFQNL_COPY_PACKET- 复制整个数据包
range:我们期望得到的数据包的大小
return: -1失败,>0其他情况。

  • nfq_fd()得到nfqueue句柄相关的文件描述符。
int nfq_fd(struct nfq_handle *h);

参数:
h:nfq_open()获得的句柄
return:与nfqueue句柄相关的文件描述符。

  • nfq_handle_packet() 处理从nfqueue子系统收到的数据包
int nfq_handle_packet(struct nfq_handle *h, char *buf, int len);

参数
h:nfq_open()获得的数据包
buf:传递给回调函数的数据
len:buf中packet数据的大小

  • nfq_destroy_queue() 摧毁nfq_create_queue()创建的队列。摧毁的同时会自动解绑,所以不需要调用nfq_unbind_pf()
int nfq_destroy_queue(struct nfq_q_handle *qh);

参数:
qh:被摧毁的nfq_create_queue()句柄
return:

  • nfq_get_msg_packet_hdr() 解析消息的函数,返回包装数据的头部部分。
struct nfqnl_msg_packet_hdr *nfq_get_msg_packet_hdr(struct nfq_data *nfad);

参数:
nfq_data:将要传递给回调函数的Netlink数据包的句柄

struct nfqnl_msg_packet_hdr {uint32_t	packet_id;	// 数据包在队列中的唯一id标志符uint16_t	hw_protocol;	// hw协议uint8_t		hook;		// netfilter 钩子} __attribute__ ((packed));
  • get hardware address() 获得硬件地址
struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包的句柄
return:硬件地址。在以太网中就是MAC地址。 直到POSTROUTING和成功的ARP请求之后,才知道目标MAC地址,因此当前无法获得。

  • nfq_get_nfmark() 获得数据包的标志
uint32_t nfq_get_nfmark(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包
return:分配给这个数据包的标志

  • nfq_get_indev() 获取接受数据包的索引信息。
uint32_t nfq_get_indev(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包
return:接收排队数据包的设备的索引。 如果返回的索引为0,则说明该数据包是在本地生成的,或者输入接口未知(即OSTROUTING未知)。

  • nfq_get_outdev() 获取接受数据包的索引信息。
uint32_t nfq_get_outdev(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包
return:排队的数据包将被发送出去的设备的索引。 如果返回的索引为0,则该数据包将发送到本机或尚不知道输出接口(即REROUTING未知)。

  • nfq_get_physindev() 获取接受的数据包的物理层接口信息。
uint32_t nfq_get_physindev(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包
return:接收排队数据包的物理设备的索引。 如果返回的索引为0,则说明该数据包是在本地生成的,或者输入接口未知(即OSTROUTING未知)。

  • nfq_get_outdev() 获取即将发送的数据包的物理层接口信息。
uint32_t nfq_get_outdev(struct nfq_data *nfad);

参数:
nfad:将要传递给回调函数的Netlink数据包
return:将要发送的排队数据包的物理设备的索引。 如果返回的索引为0,则说明该数据包将发往本机,或者输入接口未知(即OSTROUTING未知)。

  • nfq_get_payload() 获取有效载荷
int nfq_get_payload(struct nfq_data *nfad, unsigned char **data);

参数:

nfad:将要传递给回调函数的Netlink数据包
data:指向payload的指针的指针
return: -1错误,其余情况>0

  • nfq_set_verdict()
int nfq_set_verdict(struct nfq_q_handle *qh, uint32_t id, uint32_t verdict, uint32_t data_len, const unsigned char *buf)

参数:
qh:nfq_create_queue()创建的句柄。
id:唯一标志符
verdict: 对数据包操作的办法,主要有如下几种:

  1. NF_DROP: 丢弃该报文,释放所有与该报文相关的资源;
  2. NF_ACCEPT: 接受该报文,并继续处理;
  3. NF_STOLEN: 该报文已经被HOOK函数接管,协议栈无须继续处理;
  4. NF_QUEUE: 将该报文传递到用户态去做进一步的处理;
  5. NF_REPEAT: 再次调用本HOOK函数。
  6. NF_STOP : 与NF_ACCEPT类似但强于NF_ACCEPT,一旦挂接链表中某个hook节点返回NF_STOP,该skb包就立即结束检查而接受,不再进入链表中后续的hook节点,而NF_ACCEPT则还需要进入后续hook点检查。

data_len:数据长度
buf:数据

测试

编译

gcc -o nf-queue nf-queue.c -lnetfilter_queue

配置iptables规则

在用户态对数据包进行处理,需要配置iptables规则,将符合条件的数据包转到netfilter提供的queue中。

ip6tables -I INPUT -p icmpv6 --icmpv6-type 128 -m length --length :1280 -j NFQUEUE --queue-num 0

ping本机loopback地址

ping6 -s 1120 fe80::8261:5fff:fe03:fd3d%lo

此时没有数据包回复
在这里插入图片描述

启动nf-queue程序

./nf-queue 

此时ping包能够收到回复数据
在这里插入图片描述

这篇关于netfilter_queue 使用示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/740923

相关文章

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND