我想使用netfilter鉤子功能打印skbuffs數據。唯一的問題是當我運行這段代碼時,我的操作系統凍結,必須強制關機。我是新的內核編碼,所以我希望有人可以看看這個,並解釋如何解決。使用netfilter鉤子打印skb數據崩潰的計算機
我注意到的一件事是,如果我在我的hook_func的最開始處添加一行「if(!sb)return NF_ACCEPT;」那麼程序運行良好。編輯:接受沒有skb數據打印,因爲if語句始終啓動。
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/netfilter_ipv4.h>
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct sk_buff *sb = *skb;
int kk=0;
for (kk=0; (kk<sb->len) && (kk < 300); kk++)
printk("%c", &sb->data+kk);
printk("\n----------------\n");
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func;
/* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our func first */
nf_register_hook(&nfho);
return 0;
}
/*Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
我真的需要一些幫助! 乾杯 本
對不起,問你的隊友,什麼是skb? – 2015-05-22 04:37:27