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 looked at other solutions, mainly as a result of double control parameters, including changing *skb to **skb . I read that the parameters may depend on the kernel version, but could not find how to find the necessary parameters that need to be passed.
How can I make this work, as well as how to check which parameters should be passed to hook_func in 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; }
Fiks source share