c - nfhook (netfilter) 错误 : assignment from incompatible pointer type

标签 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.h) 中。在我的例子中是 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,我们将不胜感激!

最佳答案

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

关于c - nfhook (netfilter) 错误 : assignment from incompatible pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44150093/

相关文章:

linux - "Kernel header files not in any of expected locations"编译ixgbe驱动时

linux - kgdboe kgdb 启动时内核调试

c - 打印链表中链接的值

c - C中的递归,需要一些解释

android - 内核 bash 脚本编译错误

linux-kernel - 如何在 Linux 中分配大的连续内存区域

linux - 如何将 Action 直接发送到输入事件?

c - 查找用户输入的数字中最大的素数 - C

使用指针从 csv 文件复制行

c - 我们需要在调用 schedule 之前调用 set_current_state(TASK_INTERRUPTIBLE) 吗?