【问题标题】:UDP packet send with linux-kernel module without using sockets使用 linux-kernel 模块发送 UDP 数据包而不使用套接字
【发布时间】:2012-10-25 00:08:34
【问题描述】:

我正在编写一个创建 IP 数据包的内核模块。现在我想发送这个数据包,但还没有为它的传输创建任何套接字。

有没有办法借助内核例程直接发送。

因为我已经跟踪了用于数据包传输的 linux 内核代码,所以在 net/core/dev.c 中定义了一个名为 dev_hard_start_xmit() 的函数。我们可以使用它吗?

其实我不想用套接字。

【问题讨论】:

    标签: c linux sockets linux-kernel kernel-module


    【解决方案1】:

    内核发送UDP包不需要socket,只需要分配一个skb,自己构造IP头和UDP头,最后通过dev_queue_xmit()发送出去。

    skb = alloc_skb(len, GFP_ATOMIC);
    if (!skb)
            return;
    
    skb_put(skb, len);
    
    skb_push(skb, sizeof(*udph));
    skb_reset_transport_header(skb);
    udph = udp_hdr(skb);
    udph->source = htons(....);
    udph->dest = htons(...);
    udph->len = htons(udp_len);
    udph->check = 0;
    udph->check = csum_tcpudp_magic(local_ip,
                                    remote_ip,
                                    udp_len, IPPROTO_UDP,
                                    csum_partial(udph, udp_len, 0));
    
    if (udph->check == 0)
            udph->check = CSUM_MANGLED_0;
    
    skb_push(skb, sizeof(*iph));
    skb_reset_network_header(skb);
    iph = ip_hdr(skb);
    
    /* iph->version = 4; iph->ihl = 5; */
    put_unaligned(0x45, (unsigned char *)iph);
    iph->tos      = 0;
    put_unaligned(htons(ip_len), &(iph->tot_len));
    iph->id       = htons(atomic_inc_return(&ip_ident));
    iph->frag_off = 0;
    iph->ttl      = 64;
    iph->protocol = IPPROTO_UDP;
    iph->check    = 0;
    put_unaligned(local_ip, &(iph->saddr));
    put_unaligned(remote_ip, &(iph->daddr));
    iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
    
    eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
    skb_reset_mac_header(skb);
    skb->protocol = eth->h_proto = htons(ETH_P_IP);
    memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);
    memcpy(eth->h_dest, remote_mac, ETH_ALEN);
    
    skb->dev = dev;
    
    
    dev_queue_xmit(skb);
    

    【讨论】:

    • 我正在做同样的过程,但在传输过程中系统变得有点慢..为什么会发生这种情况我无法找出...请帮助???
    • 我也有同样的问题。
    • 你从哪里得到开发结构?
    • @Gaurav 可能,开发人员已经知道它或者 git 从查找到路由表。
    • 传输很慢..它比使用原始套接字要低..这是什么原因?任何想法..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2012-10-07
    • 2013-07-04
    • 2013-03-06
    相关资源
    最近更新 更多