2013-04-30 140 views
2

我想使用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); 
} 

我真的需要一些幫助! 乾杯 本

+0

對不起,問你的隊友,什麼是skb? – 2015-05-22 04:37:27

回答

0

現在回答我自己的問題。某些搜索和進一步的調試後,我設法找到一個類似的問題:

http://forum.kernelnewbies.org/read.php?15,1100,1100

事實證明,因爲「Linux版本> = 2,6,20」鉤子函數不會在同一個定義SKB辦法。現在,它只是一個指針,所以鉤子函數應該是這樣的:

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; 
} 

通知的hook_func(unsigned int類型hooknum,結構的sk_buff ** SKB ...)現在已經改爲hook_func(unsigned int類型hooknum ,結構的sk_buff * SKB ...)

結構的sk_buff * SB = * SKB;現在已經變成struct sk_buff * sb = skb;

希望這可以幫助那些和我有同樣問題的人。

+0

這不再適用於3.13+版本內核的最新更改。 – askb 2014-10-11 08:24:52