Linux 内核模块到以太网数据包回送
我开发了一个linux内核模块来重新传输一些以太网数据包做回音。数据包到达后,我检查以太网目标地址,如果是我的,我重新传输。否则我什么也不做 我使用dev_pack_eth定义协议处理程序来接收所有以太网数据包EHT_p_all,并使用dev_queue_xmit来传输接收到的skb buff 它工作正常,回声正常,但 有时候,很多时候。内核崩溃了,我不知道为什么 当我重新发送数据包时,我返回NET\u RX\u success 当我不重新传输时,我使用kfree_skb释放接收到的skb buff并返回NET_RX_DROP 我认为我的问题在于这个问题。你能帮我吗 如果需要,我可以发布内核模块代码 致以最良好的祝愿 ------编辑:添加代码----Linux 内核模块到以太网数据包回送,linux,networking,module,kernel,ethernet,Linux,Networking,Module,Kernel,Ethernet,我开发了一个linux内核模块来重新传输一些以太网数据包做回音。数据包到达后,我检查以太网目标地址,如果是我的,我重新传输。否则我什么也不做 我使用dev_pack_eth定义协议处理程序来接收所有以太网数据包EHT_p_all,并使用dev_queue_xmit来传输接收到的skb buff 它工作正常,回声正常,但 有时候,很多时候。内核崩溃了,我不知道为什么 当我重新发送数据包时,我返回NET\u RX\u success 当我不重新传输时,我使用kfree_skb释放接收到的skb bu
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/skbuff.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_bridge.h>
#include <asm-generic/types.h>
/*Buscar as interfaces de rede*/
struct net_device *dev_eth0;
struct net_device *dev_eth1;
int contador;
static struct packet_type hook; /* Initialisation routine */
void handler_add_config (void);
void handler_remove(void);
void print_mac_hdr(struct ethhdr *eth);
static int hook_func( struct sk_buff *skb)
{
struct ethhdr *eth;
struct ethhdr aux;
eth= eth_hdr(skb)
print_mac_hdr(eth);
/*If destination isn't the same that dev_addr, the packet is not for me: do nothing*/
if(memcmp(eth->h_dest,skb->dev->dev_addr,ETH_ALEN)!=0)
{
printk("Não são iguais!!!\n");
}
else
{
/*Swap addr*/
memcpy(&(aux.h_dest),eth->h_dest,ETH_ALEN);
memcpy(eth->h_dest,eth->h_source,ETH_ALEN);
memcpy(eth->h_source,&(aux.h_dest),ETH_ALEN);
/*Re build ther hearders*/
skb->data = (unsigned char *)skb->mac_header;
skb->len += ETH_HLEN;
skb->pkt_type = PACKET_OUTGOING;
/*Send*/
if(dev_queue_xmit(skb)!= NET_XMIT_SUCCESS)
{
printk("Erro na transmissão\n");
}
else
{
printk("Trama retransmitida com sucesso\n");
return NET_RX_SUCCESS;
}
}
kfree_skb(skb);
return NET_RX_DROP;
}
/*Print eth headers*/
void print_mac_hdr(struct ethhdr *eth)
{
printk("Destino: %02x:%02x:%02x:%02x:%02x:%02x \n",eth->h_dest[0],eth->h_dest[1],eth->h_dest[2],eth->h_dest[3],eth->h_dest[4],eth->h_dest[5]);
printk("Origem: %02x:%02x:%02x:%02x:%02x:%02x\n",eth->h_source[0],eth->h_source[1],eth->h_source[2],eth->h_source[3],eth->h_source[4],eth->h_source[5]);
printk("Proto: 0x%04x\n",ntohs(eth->h_proto));
}
/*Configure Protocol Handler*/
void handler_add_config (void)
{
hook.type = htons(ETH_P_ALL);
hook.func = (void *)hook_func;
hook.dev = NULL;
dev_add_pack(&hook);
printk("Handler Protocol adicionado!!!!\n");
}
/*Unregist protocol handler*/
void handler_remove(void)
{
dev_remove_pack(&hook);
printk("Handler Protocol removido!!!!\n");
synchronize_net();/*Sincronizar a rede!*/
}
/*Init module and protocol handler*/
static int __init hook_init(void)
{
printk("Hello:I'm the hook module!!!!\n");
contador =0;
dev_eth0=dev_get_by_name(&init_net,"eth0");
dev_eth1=dev_get_by_name(&init_net,"eth1");
handler_add_config();
return 0;
}
/*Remove module and protocol handler*/
static void __exit hook_exit(void)
{
printk("Hook module says Goodbye!!!!!\n");
handler_remove();
}
module_init(hook_init);
module_exit(hook_exit);
MODULE_LICENSE("GPL");
我想当你返回时,净收益会下降;这会导致问题,因为钩子中基本上有返回类型
Return Code Meaning
NF_DROP Discard the packet.
NF_ACCEPT Keep the packet.
NF_STOLEN Forget about the packet.
NF_QUEUE Queue packet for userspace.
NF_REPEAT Call this hook function again.
&您正在返回NET\u RX\u DROP,因此请尝试使用NF\u DROP。请查看NET/x25中的af\u x25.c,以获得相同的示例实现,其中即使在一个DROP中,它们也返回0。顺便说一句,你不明白为什么你要增加skb->len,而你所做的只是交换mac地址?也就是说,为什么需要在这种意义上重建HDR?我在这里遗漏了什么吗?您可能正在崩溃,因为您正在通过调用kfree_skbskb释放sku buff的单一副本 请把密码寄出去。调试代码很难,没有代码就无法进行调试。您还可以添加内核错误输出吗?我尝试在/var/log上检查最后一个内核的消息,但没有说什么,或者至少我没有找到它。明天我会努力让自己看起来更好,因为我编程的电脑现在不在我身边。如果可能,我将尝试添加。谢谢。我找不到错误消息。请尝试在没有图形界面(即没有X)的情况下运行。发生错误时,您应该在控制台上看到错误消息。或者,考虑设置一个串行控制台。好的,回答我自己的问题,这是必要的,因为ETH HDR在ETH_type_trans中被剥离,它将由NIC设备驱动程序在其Rx例程中调用。