1

我开发了一个 linux 内核模块来重新传输一些以太网数据包(做回声)。数据包到达,我检查以太网目标地址,如果是给我的,我重新传输。如果不是我什么都不做。

我使用 dev_pack_eth 定义我的协议处理程序以接收所有以太网数据包(EHT_P_ALL)和 dev_queue_xmit 以传输接收到的 skb buff。

它有效,回声功能正常,但......

有时,很多时候。内核崩溃,我不知道为什么。

当我重新传输数据包时,我返回 NET_RX_Sucess。

当我不重新传输时,我使用 kfree_skb 释放收到的 skb buff 并返回 NET_RX_DROP。

我认为我的问题在于这个问题。你能帮助我吗?

如果需要,我可以发布内核模块代码。

此致!

------------编辑:添加代码--------

#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");
4

3 回答 3

0

Look at af_x25.c in net/x25 for a sample implementation of the same where they return 0 even on a drop. BTW didn't understand why are you incrementing skb->len when all you are doing is swapping the mac addresses ? I.e why the need to rebuild hdrs in that sense? Am i missing something here?

于 2014-06-18T09:02:28.330 回答
0

我想当你返回时返回 NET_RX_DROP;它会导致问题,因为钩子中基本上有返回类型......

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_RX_DROP 所以尝试使用NF_DROP

于 2012-11-23T09:38:15.973 回答
-2

您可能正在崩溃,因为您正在通过调用 kfree_skb(skb); 释放 sk_buff 的 slngle 副本;

于 2017-09-01T21:44:54.330 回答