【问题标题】:nfhook (netfilter) error: assignment from incompatible pointer typenfhook (netfilter) 错误:来自不兼容的指针类型的赋值
【发布时间】:2017-10-24 07:24:42
【问题描述】:

我看到这个页面有类似的错误信息: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,感谢任何帮助!

【问题讨论】:

    标签: c linux-kernel kernel-module netfilter


    【解决方案1】:

    在(恕我直言)最新(发布)的netfilterversion中,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与这个签名不匹配,你应该采用它。

    【讨论】:

    • 谢谢!这帮助我解决了这个问题。但是,你能告诉我更多关于第一个和第三个参数的信息吗?我在网上找不到任何相关文档(不过我确实知道有关第二个参数的信息)。
    【解决方案2】:

    第三个参数就是这个数据结构。在钩子函数的新定义中,他们想将旧参数组合在一个数据结构中。所以,如果你需要 out 设备,你可以从这个 state 参数中得到它。

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

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2019-06-05
      • 2014-07-27
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      相关资源
      最近更新 更多