nfhook (netfilter) error: assignment from incompatible pointer type

Viewed 1811

I have seen this page with similar error message: Nf_hook_ops returns incompatible pointer when assigning to hook_func -C -Linux -Netfilter

However, it didn't give a clear answer to how to solve the issue. The author for that question says that he found out his netfilter.h is located elsewhere which caused the trouble, but for me I found out that all the four files included are in the correct directory (usr/src/linux-headers-4.8.0-22-generic/include/linux in my case).

Following is my code which should help clarify better.

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

The exact error message is this:

error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types] nfho.hook = hook_func_incoming; ^ cc1: some warnings being treated as errors

Please let me know what I should do to be able to compile my netfilter, any help is appreciated!

2 Answers

Third parameter is this data structure. In the new definition of the hook function, they wanted to combine the old parameters in a single data structure. So, if you need the out device, you can get it from this state parameter.

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 is a field inside the struct nf_hook_ops. You can set it to any value in your own module and access it in your hook function.

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