Libnet TCP 校验和错误

发布于 2024-12-11 07:02:34 字数 164 浏览 0 评论 0原文

我正在尝试使用 libnet 库构建 TCP 数据包。我在 libnet_build_tcp 函数中使用 '0' 自动计算校验和值。然而,在计算时,校验和似乎忽略了伪标头,导致由于校验和错误而导致无用的数据包。有谁知道如何解决这个问题?

I am trying to build a TCP packet using libnet library. I use '0' for autocomputation of checksum value in the libnet_build_tcp function. However, it seems the checksum ignores the pseudo-header when being computed resulting in a useless packet because of checksum error. Does anyone know how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2024-12-18 07:02:34

据我在代码中看到的,只要您没有在 sum< 中使用 libnet_toggle_checksum(l, ptag, 1); 您的 0 libnet_build_tcp() 的 /code> 参数应该会通过调用 libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM)

我无法真正告诉您如何解决它,但既然您是构建数据包的人,也许您可​​以选择创建自己的校验和?

TCP 伪报头是一个 12 字节字段,包含源 IP 地址和目标 IP 地址(均为 4 字节)、保留字段(2 字节且始终设置为零)以及 4 字节 TCP 段长度(等于 IP 地址大小)。 TCP 标头 + 负载长度。

你可以创建这样的东西:

首先创建你的变量:

int bytesRead;
int pseudoHeaderLength;
int payloadLength;
int tcpHdrLength;
int tcpSegmentLength;
int ipHdrLength;
int headerLengths;
int ethIPHdrLengths;

struct ethhdr *ethHdr;
struct iphdr *ipHdr;
struct tcphdr *tcpHdr;

u_int8_t *pkt_data;
u_int8_t pseudoHeader[12];
u_int8_t packetBuffer[1600];
u_int16_t newChecksum;

然后创建实际的函数,其中 b->len 是数据包的总大小(只需添加所有标头+有效负载并获取总大小),你就可以了将标头和数据 memcpy 到 pkt_data:

ethHdr = (struct ethhdr*)pkt_data;
ipHdr = (struct iphdr*)(pkt_data + ETH_HLEN);

ipHdrLength = ipHdr->ihl * 4;

ethIPHdrLengths = ETH_HLEN + ipHdrLength;

tcpHdr = (struct tcphdr*)(pkt_data + ethIPHdrLengths);

tcpHdrLength = tcpHdr->doff * 4;

headerLengths = ethIPHdrLengths + tcpHdrLength;

payloadLength = b->len - headerLengths;

tcpSegmentLength = tcpHdrLength + payloadLength;

pseudoHeaderLength = tcpSegmentLength + 12;

pseudoHeader[0] = pkt_data[30];
pseudoHeader[1] = pkt_data[31];
pseudoHeader[2] = pkt_data[32];
pseudoHeader[3] = pkt_data[33];
pseudoHeader[4] = pkt_data[26];
pseudoHeader[5] = pkt_data[27];
pseudoHeader[6] = pkt_data[28];
pseudoHeader[7] = pkt_data[29];
pseudoHeader[8] = 0x00;
pseudoHeader[9] = 0x06;
pseudoHeader[10] = (tcpSegmentLength >> 8) & 0xFF;
pseudoHeader[11] = tcpSegmentLength & 0xFF;

bytesRead = 0;

for(i = 0; i < 12; i++) {
    packetBuffer[bytesRead] = pseudoHeader[i];
    bytesRead++;
}

for(i = ethIPHdrLengths; i < headerLengths; i++) {
    packetBuffer[bytesRead] = pkt_data[i];
    bytesRead++;
}

for(i = b->len - payloadLength; i < b->len; i++) {
    packetBuffer[bytesRead] = pkt_data[i];
    bytesRead++;
}

newChecksum = checksum((uint16_t *)packetBuffer, pseudoHeaderLength);

只需使用 https://www.rfc-editor.org/rfc/rfc1071 计算缓冲区上的校验和:

uint16_t checksum(uint16_t *addr, int len)
{

int count = len;
register uint32_t sum = 0;
uint16_t answer = 0;

while (count > 1) {
    sum += *(addr++);
    count -= 2;
}

if (count > 0) {
    sum += *(uint8_t *) addr;
}

while (sum >> 16) {
    sum = (sum & 0xffff) + (sum >> 16);
}

answer = ~sum;

return (answer);
}

我在现实环境中使用它以 500 万 PPS 计算校验和。

As far as I can see in the code, as long as you haven't used libnet_toggle_checksum(l, ptag, 1); your 0 in the sum parameter of libnet_build_tcp() should be causing it to autocompute a checksum for you by calling libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM).

I couldn't really tell you how to solve it but since you are the one building the packet maybe you could opt for creating your own checksum?

The TCP pseudoheader is a 12 byte field containing the source and destination IP addresses which are both 4 bytes each, a reserved field which is 2 bytes and always set to zeros, and a 4 byte TCP segment length which is equal to the size of the TCP header + the payload length.

You could create something like this:

First create your variables:

int bytesRead;
int pseudoHeaderLength;
int payloadLength;
int tcpHdrLength;
int tcpSegmentLength;
int ipHdrLength;
int headerLengths;
int ethIPHdrLengths;

struct ethhdr *ethHdr;
struct iphdr *ipHdr;
struct tcphdr *tcpHdr;

u_int8_t *pkt_data;
u_int8_t pseudoHeader[12];
u_int8_t packetBuffer[1600];
u_int16_t newChecksum;

Then create the actual function where b->len is the total size of the packet (just add all the headers + the payload and get the total size) and you would just have to memcpy your header and data to pkt_data:

ethHdr = (struct ethhdr*)pkt_data;
ipHdr = (struct iphdr*)(pkt_data + ETH_HLEN);

ipHdrLength = ipHdr->ihl * 4;

ethIPHdrLengths = ETH_HLEN + ipHdrLength;

tcpHdr = (struct tcphdr*)(pkt_data + ethIPHdrLengths);

tcpHdrLength = tcpHdr->doff * 4;

headerLengths = ethIPHdrLengths + tcpHdrLength;

payloadLength = b->len - headerLengths;

tcpSegmentLength = tcpHdrLength + payloadLength;

pseudoHeaderLength = tcpSegmentLength + 12;

pseudoHeader[0] = pkt_data[30];
pseudoHeader[1] = pkt_data[31];
pseudoHeader[2] = pkt_data[32];
pseudoHeader[3] = pkt_data[33];
pseudoHeader[4] = pkt_data[26];
pseudoHeader[5] = pkt_data[27];
pseudoHeader[6] = pkt_data[28];
pseudoHeader[7] = pkt_data[29];
pseudoHeader[8] = 0x00;
pseudoHeader[9] = 0x06;
pseudoHeader[10] = (tcpSegmentLength >> 8) & 0xFF;
pseudoHeader[11] = tcpSegmentLength & 0xFF;

bytesRead = 0;

for(i = 0; i < 12; i++) {
    packetBuffer[bytesRead] = pseudoHeader[i];
    bytesRead++;
}

for(i = ethIPHdrLengths; i < headerLengths; i++) {
    packetBuffer[bytesRead] = pkt_data[i];
    bytesRead++;
}

for(i = b->len - payloadLength; i < b->len; i++) {
    packetBuffer[bytesRead] = pkt_data[i];
    bytesRead++;
}

newChecksum = checksum((uint16_t *)packetBuffer, pseudoHeaderLength);

And just use the checksum function provided by https://www.rfc-editor.org/rfc/rfc1071 to calculate the checksum over the buffer:

uint16_t checksum(uint16_t *addr, int len)
{

int count = len;
register uint32_t sum = 0;
uint16_t answer = 0;

while (count > 1) {
    sum += *(addr++);
    count -= 2;
}

if (count > 0) {
    sum += *(uint8_t *) addr;
}

while (sum >> 16) {
    sum = (sum & 0xffff) + (sum >> 16);
}

answer = ~sum;

return (answer);
}

I use this in a realistic environment to calculate checksums at 5 million PPS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文