nfhook(网络过滤器)的错误:从兼容的指针类型

nfhook(网络过滤器)的错误:从兼容的指针类型

问题描述:

任务,我已经看到了类似的错误消息此页: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的,任何帮助表示赞赏!

在(恕我直言)最新(释放)的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

谢谢!这帮助我解决了这个问题。但是,你能告诉我更多关于第一个和第三个参数的信息吗?我在网上找不到任何相关文档(虽然我知道关于第二个参数的信息)。 –

第三个参数是这个数据结构。在钩函数的新定义中,他们希望将旧参数合并到一个数据结构中。所以,如果你需要输出设备,你可以从这个状态参数中获得它。

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