【问题标题】:Netfilter hook is not called in Ubuntu 18.04 - kernel 4.18在 Ubuntu 18.04 - 内核 4.18 中未调用 Netfilter 挂钩
【发布时间】:2019-12-16 23:31:39
【问题描述】:

我正在尝试创建一个简单地捕获 ICMP 数据包的 netfilter 钩子。我一生都无法弄清楚我做错了什么(我是编写内核模块的新手)。我到目前为止的代码是

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/ip.h>
#include <linux/inet.h>

#define DIP "1.2.3.4"

static struct nf_hook_ops nfho;
static struct net n;

MODULE_DESCRIPTION("Monitor packets");
MODULE_AUTHOR("john");
MODULE_LICENSE("GPL");

unsigned int hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
    printk(KERN_INFO "hook!\n");
    return NF_ACCEPT;
}

int init_module()
{
    int ret;
    nfho.hook = hook_func;
    nfho.hooknum = NF_INET_PRE_ROUTING; 
    nfho.pf = AF_INET;
    nfho.priority = NF_IP_PRI_FIRST;

    ret = nf_register_net_hook(&n, &nfho);

    if (ret != 0)
    {
        printk(KERN_INFO "module is NOT loaded into the kernel\n");    
        return -1;    
    }
    else
    {
        printk(KERN_INFO "module IS loaded into the kernel\n");      
        return 0;  
    }
}

void cleanup_module()
{
    nf_unregister_net_hook(&n, &nfho);
    printk(KERN_INFO "module has been unloaded\n");  
}

从我的系统日志看来,内核模块已正确加载/卸载(从打印消息中)。我没有收到“钩子”! ping 我的机器时的消息。是不是pf错了?错误的钩子优先级?我可以以某种方式调试吗?

应该说我在虚拟机中,并且我正在从另一台不是主机的物理机上 ping 虚拟机。我的操作系统是 Ubuntu 18.04 LTS,“uname -a”的结果:Linux ubuntu 4.18.0-20-generic #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

我知道有一些未使用的包含。这是改编自另一个内核版本的代码示例,我计划稍后填写。我真的希望有人能给我一个从哪里开始寻找的想法,或者看看我做错了什么。

提前谢谢大家

【问题讨论】:

    标签: ubuntu hook icmp netfilter


    【解决方案1】:

    似乎缺少 LKM 入口/出口点:

    module_init(init_module)  //for insmod
    module_exit(cleanup_module) //for rmmod
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2013-02-26
      • 2016-04-01
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多