Nf_hook_ops returns incompatible pointer when assigning to hook_func -C -Linux -Netfilter

蓝咒 提交于 2019-12-24 09:31:18

问题


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

来源:https://stackoverflow.com/questions/43741943/nf-hook-ops-returns-incompatible-pointer-when-assigning-to-hook-func-c-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!