Ubuntu新版本的神坑一:
- 以往的版本是使用 nf_register_hook(reg)
- 而新版本是 nf_register_net_hook(&init_net, reg)
- 注销函数也改成了 nf_unregister_net_hook(&init_net, reg)
Ubuntu新版本的神坑二:
- 以往的版本是使用
static unsigned int xxxx(
unsigned int hooknum,
struct sk_buff * skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn) (struct sk_buff *))
- 新版本是使用
unsigned int xxxxx(void *priv, struct sk_buff *skb,
const struct nf_hook_state *state)
Ubuntu新版本的神坑三:
- 貌似也不用module_init(xxx);module_exit(xxx); 反正本人直接报错
本人亲测可以用Ubuntu(16.04 18.04)
#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(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
printk(KERN_INFO "packet dropped\n");
return NF_DROP;
}
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_net_hook(&init_net, &nfho);
return 0;
}
void cleanup_module()
{
nf_unregister_net_hook(&init_net,&nfho);
}