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

时间:2017-05-24 05:58:54

标签: c linux-kernel kernel-module netfilter

我在此页面中看到了类似的错误消息:Nf_hook_ops returns incompatible pointer when assigning to 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;
}

确切的错误信息是:

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

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

2 个答案:

答案 0 :(得分:5)

在(IMHO)最新(已发布)netfilter version中,nf_hookfnnf_hook_ops.hook的基本类型)定义如下:

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

您的功能hook_func_incoming与此签名不符,您应该采用它。

答案 1 :(得分:0)

第三个参数是这个数据结构。在钩子函数的新定义中,他们希望将旧参数组合在单个数据结构中。因此,如果您需要out设备,可以从此状态参数中获取它。

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

priv是struct nf_hook_ops中的一个字段。您可以将其设置为您自己模块中的任何值,并在钩子函数中访问它。

{{1}}
相关问题