1

いくつかのイーサネット パケットを再送信する (エコーを実行する) Linux カーネル モジュールを開発します。パケットが到着し、イーサネットの宛先アドレスを確認し、自分宛てであれば再送信します。そうでなければ、私は何もしません。

dev_pack_eth を使用してプロトコル ハンドラを定義し、すべてのイーサネット パケット (EHT_P_ALL) を受信し、dev_queue_xmit を使用して受信した skb バフを送信しました。

それは機能します、エコーは機能しますが...

時々、非常に頻繁に。カーネルがクラッシュし、その理由がわかりません。

パケットを再送信すると、NET_RX_Sucess が返されます。

再送信しない場合は、kfree_skb を使用して受信した skb バフを解放し、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

net/x25 の af_x25.c を参照して、ドロップでも 0 を返す同じ実装のサンプルを確認してください。ところで、Mac アドレスを交換するだけなのに、なぜ skb->len をインクリメントするのか理解できませんでした。つまり、なぜその意味で hdrs を再構築する必要があるのでしょうか? ここで何か不足していますか?

于 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.

& u は NET_RX_DROP を返しているので、NF_DROPを使用してみてください。

于 2012-11-23T09:38:15.973 に答える