Linux kernel 内核模块内部strcmp崩溃

Linux kernel 内核模块内部strcmp崩溃,linux-kernel,kernel-module,strcmp,netfilter,Linux Kernel,Kernel Module,Strcmp,Netfilter,我正在尝试检测内核(Netfilter)模块中传出的数据包。我正在使用strcmp函数来实现它。内核总是在用strcmp函数加载我的内核模块后崩溃。我试着删除strcmp函数加载没有任何问题。我希望问题出在所有字符串函数上,我也尝试了strstr()-我的系统崩溃了 这背后的逻辑是,传入数据包将eth[0-9]+分配给“in->name”和“out->name”,反之亦然 有什么洞察来检测传出的数据包吗?我知道另一种选择是使用输出挂钩,而不是预路由和后路由挂钩。但在这里,我想以不同的方式处理传入

我正在尝试检测内核(Netfilter)模块中传出的数据包。我正在使用strcmp函数来实现它。内核总是在用strcmp函数加载我的内核模块后崩溃。我试着删除strcmp函数加载没有任何问题。我希望问题出在所有字符串函数上,我也尝试了strstr()-我的系统崩溃了

这背后的逻辑是,传入数据包将eth[0-9]+分配给“in->name”和“out->name”,反之亦然

有什么洞察来检测传出的数据包吗?我知道另一种选择是使用输出挂钩,而不是预路由和后路由挂钩。但在这里,我想以不同的方式处理传入和传出的数据包。我使用的内核版本是否不支持模块内的字符串函数

$ uname -a
Linux vmdsk01 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 x86_64 GNU/Linux
包括部分

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <linux/skbuff.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/ip.h>

#include <linux/string.h>

您可能在
out
struct指针中有空指针。您可以在主钩中添加一些健全性检查,如:

unsigned int main_hook(unsigned int hooknum,  
                   struct sk_buff *skb,
                   const struct net_device *in,
                   const struct net_device *out,
                   int (*okfn)(struct sk_buff*))
 {
     if (!out)
         return -EINVAL;

     if( strncmp(out->name, "<NULL>", IFNAMSIZ) == 0 ) // Outgoing packet must not have <NULL>
     {
         printk( KERN_INFO "OUTGOING PACKET");
     }
     ....
unsigned int main_hook(unsigned int hooknum,
结构sk_buff*skb,
const struct net_device*in,
const结构网络设备*out,
int(*okfn)(结构sk_buff*))
{
如果(!out)
返回-艾因瓦尔;
if(strncmp(out->name,“,IFNAMSIZ)==0)//传出数据包不能有
{
printk(KERN_信息“传出数据包”);
}
....
因此,我添加了检查
out
指针,并使用
strncmp
代替
strcmp
,其中
IFNAMSIZ
是中定义的
out->name
的大小。此外,
str(n)cmp
不返回
NULL
,它返回
0


检查它,请提供任何崩溃消息。

我理解这个问题,钩子函数是一系列迭代,就像(1)检查数据包一样。迭代可能收到数据包,也可能没有收到数据包。如果迭代收到数据包,结构“out”将是可用的,并且其成员可以访问;我犯了一个错误,试图在不检查struct的可用性的情况下访问成员

以下代码确定了目的和工作条件

if(out)
{
    if( strcmp(out->name, "<NULL>") ) // Outgoing packet must not have <NULL>
    {
        printk( KERN_INFO "Outgoing Packet");
    }
}
if(out)
{
if(strcmp(out->name,“”)//传出数据包不能有
{
printk(KERN_信息“传出数据包”);
}
}

您是否有任何崩溃消息?内核转储?堆栈跟踪?日志?谢谢,我尝试了您的源代码-内核没有崩溃:),但所有数据包都丢失了。我无法打开任何服务(http、ftp、ssh等)通过这台电脑。当然你的数据包丢失了,你返回
-EINVAL
,因为你的
out
指针是空的。我不能告诉你这是否正常,因为我不知道你的代码。你自己想想,回来问一些关于netfilter挂钩的问题,以及内核为什么给你空
out
指针。对于这个问题,你应该接受答案并继续。谢谢,我理解
out
指针并不总是空的,每当Netfilter framework看到数据包时,
out
指针就可用。您的答案给了我线索:)
unsigned int main_hook(unsigned int hooknum,  
                   struct sk_buff *skb,
                   const struct net_device *in,
                   const struct net_device *out,
                   int (*okfn)(struct sk_buff*))
 {
     if (!out)
         return -EINVAL;

     if( strncmp(out->name, "<NULL>", IFNAMSIZ) == 0 ) // Outgoing packet must not have <NULL>
     {
         printk( KERN_INFO "OUTGOING PACKET");
     }
     ....
if(out)
{
    if( strcmp(out->name, "<NULL>") ) // Outgoing packet must not have <NULL>
    {
        printk( KERN_INFO "Outgoing Packet");
    }
}