Tag: netfilter

Nf_hook_ops在分配给hook_func -C -Linux -Netfilter时返回不兼容的指针

我试图在Ubuntu 16.04 LTS上编写自己的Netfilter内核模块,我试图将hook_func分配给nfho.hook,但是我收到以下错误: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types] nfho.hook = hook_func; 我已经看过其他解决方案,大多数都是双重检查参数,包括将*skb更改为**skb 。 我已经读过,参数可能取决于内核版本,但无法找到如何找到要传递的正确参数。 我怎样才能使它工作,我如何检查应该在我的内核版本的hook_func中传递什么参数? 完整代码: #include #include #include #include #include #include #include static struct nf_hook_ops nfho; //struct holding set of hook function options // function to be called by hook unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, […]

使用Netfilter封装和解封装IPv4数据包

我在netfilter框架中有两个钩子。 一个在NF_IP_PRE_ROUTING用于传入数据包,另一个在NF_IP_LOCAL_OUT用于传出数据包。 传出包: 现在,从特定IP地址发出的所有IPv4数据包都封装在另一个IPv4-UDP数据包中。 我使用pskb_expand_head API为封装提供了更多的空间。 然后,用ip_route_output_key找到合适的rtable 。 使用rtable ,我重新分配skb->dst和skb->dev 。 然后,我继续使用NF_ACCEPT接受数据包。 skb_dst_drop(skb); skb_dst_set(skb, &rt->dst); skb->dev = skb_dst(skb)->dev; 传入数据包: 现在,接收所有Encapsulated Packets并根据端口号识别。 并且,拉出封装(IP+UDP+XYZ HEADER) 。 和传出数据包类似,我使用ip_route_output_key来获取rt(rtable)。 使用rtable重新分配skb->dst和skb->dev 。 然后我接受带有NF_ACCEPT的数据包 所以,碰巧我也收到了defrags传入的数据包,我有点混乱他们应该如何处理。 我希望碎片整理数据包排队,然后接收整个数据包。 任何想法。 我一直在经历可用的function ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER); 但这似乎是用于在NF_IP_LOCAL_IN阶段组装数据包,但我希望在NF_IP_PRE_ROUING阶段组装数据包。 任何有关这方面的帮助将不胜感激。

如何从debian 64位上的tcphdr(sk_buff)结构访问数据/有效负载?

我正在使用一个小型防火墙,我必须从端口80(http)中的每个tcp数据包中检索数据以进行解析。 这个代码适用于debian 32位虚拟机,我可以打印每个网页的标题,但是当我尝试加载我的内核模块并通过http端口传输一些数据时,它不打印任何数据。 当我编译时,它只在我的64位计算机上显示那些警告: /home/dev3/C/FIREWALL/firewall.c: In function ‘hook_func’: /home/dev3/C/FIREWALL/firewall.c:179: warning: cast from pointer to integer of different size /home/dev3/C/FIREWALL/firewall.c:179: warning: cast to pointer from integer of different size 有人有任何想法吗? 谢谢。 代码: #include #include #include #undef __KERNEL__ #include #define __KERNEL__ #include #include static struct nf_hook_ops nfho; unsigned int hook_func( unsigned int hooknum, struct sk_buff * skb, […]

Netfilter字符串模块示例用法

任何人都可以指出一些使用xt_string模块和netfilter的例子或提供一个例子。 我想要做的是编写netfilter模块,它将在skb-> data字段中丢弃包含某个字符串的数据包。 我最初尝试简单地strnstr(skb->data, “mystring”, strlen(“mystring”))但这似乎是不正确的方法解决这个问题(它似乎没有工作,因为我没有看到任何数据包被丢弃) 。 提前致谢

使用sk_buff添加以太网帧头

我有一个内核模块,可以捕获传出的Internet流量(Netfilter hook:LOCAL_OUT)在这个钩子上,仍然没有以太网头。 我构建了以太网头,它已经可以使用了,但是如何将它附加到skb以便我可以将整个skb结构发送到dev_queue_xmit() ? 是否有关于如何操作sk_buff数据的指南,您可以提供给我以获取更多信息? 作为样本,我尝试在所有ECHO ICMP流量上做我想做的事情; 这是我的代码。 但是当在另一台机器上检查Wireshark时,我得到了一个工作的以太网头但是一个EMPTY IP数据包……为什么会这样? static unsigned int post_icmp_check(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) { struct iphdr *iph; struct icmphdr *icmph; struct sk_buff *sb; struct ethhdr *ethh; unsigned char *frame = NULL; //Used just to copy the skb->data […]

如何使用net_dev_add()API过滤和拦截Linux数据包?

我正在为linux编写以太网网络驱动程序。 我想接收数据包,编辑并重新发送它们。 我知道如何在packet_interceptor函数中编辑数据包,但是如何在此函数中丢弃传入的数据包? #include #include #include #include struct packet_type my_proto; int packet_interceptor(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) { // I dont want certain packets go to upper in net_devices for further processing. // How can I drop sk_buff here?! return 0; } static int hello_init( void ) { printk(KERN_INFO […]

打印TCP数据包数据

在TCP通信中,当数据包从以太网传输到网络(IP)层时,我想打印该数据包中的数据? 我正在研究linux。 我得到了一些信息,可以借助linux内核代码,即在linux NAT防火墙代码中完成。 但是我会在哪里获得内核源代码? 这些编码在哪里?