c - C 中的 AF_PACKET 套接字不使用 SOCK_RAW 发送空 UDP 数据包

标签 c linux sockets

我正在尝试在 Linux (Ubuntu 18.04) 上用 C 创建一个测试程序,该程序使用 SOCK_RAW 通过 AF_PACKET/PF_PACKET 套接字发送一个空的 UDP 数据包。在这个程序中,我知道源和目标 MAC 地址和 IP。但是,它不起作用,我不确定我做错了什么。遗憾的是,我无法在网上找到关于此问题的太多资源,因为我发现的大多数线程更适合在 AF_PACKET 套接字上接收数据包。该程序还声明它向目的地发送了正确数量的字节。不过,在源虚拟机和目标虚拟机上使用 tcpdump 时,我没有看到数据包。

这是程序的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/udp.h>
#include <net/ethernet.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <inttypes.h>

#define REDIRECT_HEADER

#include "csum.h"

#define MAX_PCKT_LENGTH 65535

int main()
{
    int sockfd;
    struct sockaddr_ll dst;
    char pckt[MAX_PCKT_LENGTH];

    sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);

    if (sockfd <= 0)
    {
        perror("socket");

        exit(1);
    }

    dst.sll_family = AF_PACKET;
    dst.sll_protocol = ETH_P_IP;

    if ((dst.sll_ifindex = if_nametoindex("ens18")) == 0)
    {
        fprintf(stdout, "Interface 'ens18' not found.\n");

        exit(1);
    }

    // Do destination ethernet MAC (ae:21:14:4b:3a:6d).
    dst.sll_addr[0] = 0xAE;
    dst.sll_addr[1] = 0x21;
    dst.sll_addr[2] = 0x14;
    dst.sll_addr[3] = 0x4B;
    dst.sll_addr[4] = 0x3A;
    dst.sll_addr[5] = 0x6D;
    dst.sll_halen = 6;

    // I tried doing this with and without bind. Still not working.
    if (bind(sockfd, (struct sockaddr *)&dst, sizeof(dst)) < 0)
    {
        perror("bind");

        exit(1);
    }

    struct ethhdr *ethhdr = (struct ethhdr *) (pckt);
    struct iphdr *iphdr = (struct iphdr *) (pckt + sizeof(struct ethhdr));
    struct udphdr *udphdr = (struct udphdr *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr));
    unsigned char *data = (unsigned char *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr));

    // Do source ethernet MAC (1a:c4:df:70:d8:a6).
    ethhdr->h_source[0] = 0x1A;
    ethhdr->h_source[1] = 0xC4;
    ethhdr->h_source[2] = 0xDF;
    ethhdr->h_source[3] = 0x70;
    ethhdr->h_source[4] = 0xD8;
    ethhdr->h_source[5] = 0xA6;

    // Copy destination MAC to sockaddr_ll.
    memcpy(ethhdr->h_dest, dst.sll_addr, 6);

    // Protocol.
    ethhdr->h_proto = ETH_P_IP;

    // Fill out ip header.
    iphdr->ihl = 5;
    iphdr->version = 4;
    iphdr->frag_off = 0;
    iphdr->id = rand();
    iphdr->protocol = IPPROTO_UDP;
    iphdr->tos = 0x0;
    iphdr->ttl = 64;
    iphdr->saddr = inet_addr("10.50.0.3");
    iphdr->daddr = inet_addr("10.50.0.4");
    iphdr->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr);
    iphdr->check = 0;
    iphdr->check = ip_fast_csum(iphdr, iphdr->ihl);

    // Fill out UDP header.
    udphdr->source = htons(27000);
    udphdr->dest = htons(27015);
    udphdr->len = htons(sizeof(struct udphdr));
    udphdr->check = 0;
    udphdr->check = csum_tcpudp_magic(iphdr->saddr, iphdr->daddr, sizeof(struct udphdr), IPPROTO_UDP, csum_partial(udphdr, sizeof(struct udphdr), 0));

    // Send packet
    uint16_t sent;

    if ((sent = sendto(sockfd, pckt, iphdr->tot_len + sizeof(struct ethhdr), 0, (struct sockaddr *)&dst, sizeof(dst))) < 0)
    {
        perror("sendto");
    }

    fprintf(stdout, "Sent %d of data.\n", sent);

    close(sockfd);

    exit(0);
}

源服务器和目标服务器都是我的家庭服务器上的虚拟机(均运行 Ubuntu 18.04)。

这是我的源虚拟机的主界面:

ens18: flags=323<UP,BROADCAST,RUNNING,PROMISC>  mtu 1500
        inet 10.50.0.3  netmask 255.255.255.0  broadcast 10.50.0.255
        inet6 fe80::18c4:dfff:fe70:d8a6  prefixlen 64  scopeid 0x20<link>
        ether 1a:c4:df:70:d8:a6  txqueuelen 1000  (Ethernet)
        RX packets 1959766813  bytes 1896024793559 (1.8 TB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1936101432  bytes 1333123918522 (1.3 TB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

这是我的目标虚拟机的主界面:

ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.50.0.4  netmask 255.255.255.0  broadcast 10.50.0.255
        inet6 fe80::ac21:14ff:fe4b:3a6d  prefixlen 64  scopeid 0x20<link>
        ether ae:21:14:4b:3a:6d  txqueuelen 1000  (Ethernet)
        RX packets 1032069029  bytes 1251754298166 (1.2 TB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 74446483  bytes 9498785163 (9.4 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我尝试将源虚拟机的“ens18”接口(interface)设置为混杂模式,但这没有任何区别。不过,我认为在这种情况下这并不重要。在这种情况下,我还想使用 SOCK_RAW,因为我想获得更多经验,并且我不希望内核对数据包执行任何操作(我使用 AF_PACKET 读取该数据包) + SOCK_RAW 将导致内核不会弄乱数据包)。

话虽如此,我还有一个问题。我将如何获取不在网络上/绑定(bind)到接口(interface)的 IP 的 MAC 地址(例如,前往网络外部服务器的目标 IP)?我假设我必须发送 ARP 请求。如果是这样,我是否只需向目标服务器发送 ARP 请求并获取 MAC 地址,然后再通过 AF_PACKET + SOCK_RAW 提交每个数据包?

非常感谢您的帮助,并感谢您抽出时间!

最佳答案

我能够解决这个问题。我没有通过 htons() 将以太网协议(protocol) (ETH_P_IP) 转换为网络字节顺序,因为它是大端字节序。话虽如此,我还必须将 iphdr->total_len 转换为网络字节顺序。否则,根据 tcpdump,IP header 的总长度将不正确。我在我制作的其他程序中没有这样做,而且效果很好。因此,我假设内核自动将 IP header 的总长度转换为网络字节顺序。

由于我使用 AF_PACKET 套接字进行发送,因此我必须执行内核通常会执行的操作。

对于任何想知道的人来说,这是该程序的最终代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/udp.h>
#include <net/ethernet.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <inttypes.h>

#define REDIRECT_HEADER

#include "csum.h"

#define MAX_PCKT_LENGTH 65535

int main()
{
    int sockfd;
    struct sockaddr_ll dst;
    char pckt[MAX_PCKT_LENGTH];

    sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);

    if (sockfd <= 0)
    {
        perror("socket");

        exit(1);
    }

    dst.sll_family = PF_PACKET;
    dst.sll_protocol = htons(ETH_P_IP);

    if ((dst.sll_ifindex = if_nametoindex("ens18")) == 0)
    {
        fprintf(stdout, "Interface 'ens18' not found.\n");

        exit(1);
    }

    // Do destination ethernet MAC (ae:21:14:4b:3a:6d).
    dst.sll_addr[0] = 0xAE;
    dst.sll_addr[1] = 0x21;
    dst.sll_addr[2] = 0x14;
    dst.sll_addr[3] = 0x4B;
    dst.sll_addr[4] = 0x3A;
    dst.sll_addr[5] = 0x6D;
    dst.sll_halen = ETH_ALEN;

    // I tried doing this with and without bind. Still not working.
    if (bind(sockfd, (struct sockaddr *)&dst, sizeof(dst)) < 0)
    {
        perror("bind");

        exit(1);
    }

    struct ethhdr *ethhdr = (struct ethhdr *) (pckt);
    struct iphdr *iphdr = (struct iphdr *) (pckt + sizeof(struct ethhdr));
    struct udphdr *udphdr = (struct udphdr *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr));
    unsigned char *data = (unsigned char *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr));

    // Do source ethernet MAC (1a:c4:df:70:d8:a6).
    ethhdr->h_source[0] = 0x1A;
    ethhdr->h_source[1] = 0xC4;
    ethhdr->h_source[2] = 0xDF;
    ethhdr->h_source[3] = 0x70;
    ethhdr->h_source[4] = 0xD8;
    ethhdr->h_source[5] = 0xA6;

    for (int i = 0; i < 30; i++)
    {
        memcpy(data + i, "b", 1);
    }

    // Copy destination MAC to sockaddr_ll.
    memcpy(ethhdr->h_dest, dst.sll_addr, ETH_ALEN);

    // Protocol.
    ethhdr->h_proto = htons(ETH_P_IP);

    // Fill out ip header.
    iphdr->ihl = 5;
    iphdr->version = 4;
    iphdr->frag_off = 0;
    iphdr->id = htons(0);
    iphdr->protocol = IPPROTO_UDP;
    iphdr->tos = 0x0;
    iphdr->ttl = 64;
    iphdr->saddr = inet_addr("10.50.0.3");
    iphdr->daddr = inet_addr("10.50.0.4");
    iphdr->tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr) + 30);
    iphdr->check = 0;
    iphdr->check = ip_fast_csum(iphdr, iphdr->ihl);

    // Fill out UDP header.
    udphdr->source = htons(27000);
    udphdr->dest = htons(27015);
    udphdr->len = htons(sizeof(struct udphdr) + 30);
    udphdr->check = 0;
    udphdr->check = csum_tcpudp_magic(iphdr->saddr, iphdr->daddr, sizeof(struct udphdr) + 30, IPPROTO_UDP, csum_partial(udphdr, sizeof(struct udphdr) + 30, 0));

    // Send packet
    uint16_t sent;
    int len = ntohs(iphdr->tot_len) + sizeof(struct ethhdr) + 30;

    if ((sent = sendto(sockfd, pckt, len, 0, (struct sockaddr *)&dst, sizeof(dst))) < 0)
    //if ((sent = write(sockfd, pckt, len)) < 0)
    {
        perror("sendto");
    }

    fprintf(stdout, "Sent %d of data. %d is IPHdr len. %d is len.\n", sent, iphdr->tot_len, len);

    close(sockfd);

    exit(0);
}

关于c - C 中的 AF_PACKET 套接字不使用 SOCK_RAW 发送空 UDP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60685437/

相关文章:

c - 内存分配的间隔

c - gcc没有优化模算术?

c++ - 将命令行字符串解析为 argv 格式

linux - GFORTRAN_CONVERT_UNIT 环境变量在某些平台上不起作用?

javascript - 在 NodeJS 中,如何与另一台可能已关闭的服务器重新建立套接字连接?

c++ - MUD 服务器和基于文本的客户端

c - 为我的操作系统移植 NewLib : some questions

c - 轻松检查 C 源代码

linux - 如何创建类似 $RANDOM 的 bash 变量

sockets - 同时管理来自两个进程的串口