nfhook(netfilter)错误:从不兼容的指针类型分配

Edd*_* Wu 6 c netfilter kernel-module linux-kernel

我看到此页面有类似的错误消息:Nf_hook_ops在分配给hook_func -C -Linux -Netfilter时返回不兼容的指针

但是,它没有给出如何解决问题的明确答案.该问题的作者说,他发现他的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;
}
Run Code Online (Sandbox Code Playgroud)

确切的错误消息是这样的:

错误:从不兼容的指针类型赋值[-Werror = incompatible-pointer-types] nfho.hook = hook_func_incoming; ^ cc1:某些警告被视为错误

请让我知道我应该怎么做才能编译我的netfilter,任何帮助表示赞赏!

Mat*_*ias 5

在(IMHO)最新(发布)的netfilter 版本中nf_hookfn(的基本类型nf_hook_ops.hook)定义如下:

typedef unsigned int nf_hookfn(void *priv,
                   struct sk_buff *skb,
                   const struct nf_hook_state *state);
Run Code Online (Sandbox Code Playgroud)

您的函数hook_func_incoming与此签名不匹配,应采用它。