gpt4 book ai didi

c - AF_PACKET 套接字在 C 中不发送带有 SOCK_RAW 的空 UDP 数据包

转载 作者:行者123 更新时间:2023-12-03 12:02:54 29 4
gpt4 key购买 nike

我正在尝试在 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

我尝试将源 VM 的 'ens18' 接口(interface)设置为混杂模式,但这没有任何区别。不过,我认为这在这种情况下并不重要。我也想用 SOCK_RAW在这种情况下,因为我想获得更多经验,并且我不希望内核对数据包做任何事情(我读到使用 AF_PACKET + SOCK_RAW 将导致内核不会弄乱数据包)。

话虽如此,我还有一个问题。我将如何获取不在网络上/绑定(bind)到接口(interface)的 IP 的 MAC 地址(例如,目标 IP 到我网络外部的服务器)?我假设我必须发送一个 ARP 请求。如果是这样,在通过 AF_PACKET 提交每个数据包之前,我是否只需向目标服务器发送一个 ARP 请求并获取 MAC 地址? + 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 - AF_PACKET 套接字在 C 中不发送带有 SOCK_RAW 的空 UDP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60685437/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com