5
我想在幀被某個NIC接收時捕獲幀;從中提取一些信息(目前我需要捕獲源MAC和源IP地址);將這些信息保存在一些公共數據結構中;並讓框架進入TCP/IP協議棧。
如何在內核模塊中捕獲網絡幀
我以前使用Netfilter,但顯然它不提供鏈接層掛鉤。
有什麼辦法可以做到這一點?
我正在寫這個作爲內核模塊;運行Linux內核2.6.32
我想在幀被某個NIC接收時捕獲幀;從中提取一些信息(目前我需要捕獲源MAC和源IP地址);將這些信息保存在一些公共數據結構中;並讓框架進入TCP/IP協議棧。
如何在內核模塊中捕獲網絡幀
我以前使用Netfilter,但顯然它不提供鏈接層掛鉤。
有什麼辦法可以做到這一點?
我正在寫這個作爲內核模塊;運行Linux內核2.6.32
實際上Netfilter應該可以正常工作,因爲它接收整個數據包(內部存儲爲包含鏈路層信息的sk_buff)。這裏有一些示例代碼應該讓你開始。該代碼攔截給定設備的所有傳入數據包,並打印src MAC和src IP。
static struct nf_hook_ops nfin;
static unsigned int hook_func_in(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct ethhdr *eth;
struct iphdr *ip_header;
/* check *in is the correct device */
if (in is not the correct device)
return NF_ACCEPT;
eth = (struct ethhdr*)skb_mac_header(skb);
ip_header = (struct iphdr *)skb_network_header(skb);
printk("src mac %pM, dst mac %pM\n", eth->h_source, eth->h_dest);
printk("src IP addr:=%d.%d.%d.%d:%d\n", NIPQUAD(ip_headr->saddr));
return NF_ACCEPT;
}
static int __init init_main(void)
{
nfin.hook = hook_func_in;
nfin.hooknum = NF_IP_LOCAL_IN;
nfin.pf = PF_INET;
nfin.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfin);
return 0;
}
static void __exit cleanup_main(void)
{
nf_unregister_hook(&nfin);
}
module_init(init_main);
module_exit(cleanup_main);
大聲笑,我有點覺得愚蠢的不試試這個;非常感謝好先生^^ – Fingolfin
@Adel很高興它適合你! – ajpyles
應該是:printk(「src IP地址:=%d。%d。%d。%d:%d \ n」,NIPQUAD(ip_header-> saddr)); – Raman