2017-05-24 72 views
5

任務,我已經看到了類似的錯誤消息此頁:Nf_hook_ops returns incompatible pointer when assigning to hook_func -C -Linux -Netfilternfhook(網絡過濾器)的錯誤:從兼容的指針類型

但是,它並沒有給出明確的答案,如何解決這個問題。這個問題的作者說,他發現他的netfilter.h位於其他地方造成了麻煩,但對我來說,我發現所有包含的四個文件都在正確的目錄(usr/src/linux-headers-4.8)中。 0-22-generic/include/linux在我的情況下)。

以下是我的代碼,應該有助於澄清更好。

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

static struct nf_hook_ops nfho; 

unsigned int hook_func_incoming(unsigned int hooknum, struct sk_buff *sskb, 
const struct net_device *in, const struct net_device *out, int (*okfn) 
(struct sk_buff *)){ 
    return NF_DROP; 
} 

int init_module(){ 
    nfho.hook = hook_func_incoming; 
    nfho.hooknum = NF_INET_PRE_ROUTING; 
    nfho.pf = PF_INET; 
    nfho.priority = NF_IP_PRI_FIRST; 
    nf_register_hook(&nfho); 
    printk(KERN_INFO "SIMPLE FIREWALL LOADED\n"); 
    return 0; 
} 

確切的錯誤信息是這樣的:

錯誤:從兼容的指針類型分配[-Werror =不相容指針類型] nfho.hook = hook_func_incoming; ^ CC1:一些警告被視爲錯誤

請讓我知道我應該做的是能夠編譯我的netfilter的,任何幫助表示讚賞!

回答

3

在(恕我直言)最新(釋放)的netfilter versionnf_hookfn(基本類型的nf_hook_ops.hook)定義如下:

typedef unsigned int nf_hookfn(void *priv, 
        struct sk_buff *skb, 
        const struct nf_hook_state *state); 

你的功能hook_func_incoming不匹配此簽名,您應該採取它。

+0

謝謝!這幫助我解決了這個問題。但是,你能告訴我更多關於第一個和第三個參數的信息嗎?我在網上找不到任何相關文檔(雖然我知道關於第二個參數的信息)。 –

0

第三個參數是這個數據結構。在鉤函數的新定義中,他們希望將舊參數合併到一個數據結構中。所以,如果你需要輸出設備,你可以從這個狀態參數中獲得它。

struct nf_hook_state { 
     unsigned int hook; 
     int thresh; 
     u_int8_t pf; 
     struct net_device *in; 
     struct net_device *out; 
     struct sock *sk; 
     struct net *net; 
     struct nf_hook_entry __rcu *hook_entries; 
     int (*okfn)(struct net *, struct sock *, struct sk_buff *); 
}; 

priv是結構體nf_hook_ops內的字段。您可以將它設置爲您自己的模塊中的任何值並在鉤子函數中訪問它。

struct nf_hook_ops { 
     struct list_head  list; 

     /* User fills in from here down. */ 
     nf_hookfn    *hook; 
     struct net_device  *dev; 
     void     *priv; 
     u_int8_t    pf; 
     unsigned int   hooknum; 
     /* Hooks are ordered in ascending priority. */ 
     int      priority; 
};