Nf_hook_ops returns incompatible pointer when assi

2019-07-25 18:05发布

问题:

I am trying to write my own Netfilter kernel module on Ubuntu 16.04 LTS, I am trying to assign hook_func to nfho.hook but I get the following error:

error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types] nfho.hook = hook_func;

I have looked on other solutions, mostly ended up on double checking parameters including changing *skb to **skb. I have read that the parameters might depend on the kernel version but couldn't find how to find out the right parameters to be passed.

How can I get that to work and also how do I check what parameters should be passed in hook_func on my kernel version?

Full code:

#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>

static struct nf_hook_ops nfho;  //struct holding set of hook function options

// function to be called by hook
unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
  struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb); 
  struct tcphdr *tcp_header;
  if (ip_header->protocol == 6)
  {
    printk(KERN_INFO "TCP Packet\n");
    tcp_header = (struct tcphdr *)(skb_transport_header(skb)+20);
    printk(KERN_INFO "Source Port: %u\n", tcp_header->source);
   }

  return NF_ACCEPT;                                                                  
}

int init_module()
{
  nfho.hook = hook_func;
  nfho.hooknum = NF_INET_PRE_ROUTING; 
  nfho.pf = PF_INET;                 
  nfho.priority = NF_IP_PRI_FIRST;   
  nf_register_hook(&nfho);           
  return 0;            
}