Kernel: Fix IPv4 header checksum calculation

This commit is contained in:
Bananymous 2024-02-05 17:31:15 +02:00
parent b8cf6432ef
commit d15cbb2d6a
3 changed files with 10 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <BAN/Endianness.h>
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <BAN/Hash.h> #include <BAN/Hash.h>

View File

@ -23,12 +23,13 @@ namespace Kernel
constexpr uint16_t calculate_checksum() const constexpr uint16_t calculate_checksum() const
{ {
return 0xFFFF uint32_t total_sum = 0;
- (((uint16_t)version_IHL << 8) | DSCP_ECN) for (size_t i = 0; i < sizeof(IPv4Header) / sizeof(uint16_t); i++)
- total_length total_sum += reinterpret_cast<const BAN::NetworkEndian<uint16_t>*>(this)[i];
- identification total_sum -= checksum;
- flags_frament while (total_sum >> 16)
- (((uint16_t)time_to_live << 8) | protocol); total_sum = (total_sum >> 16) + (total_sum & 0xFFFF);
return ~(uint16_t)total_sum;
} }
}; };
static_assert(sizeof(IPv4Header) == 20); static_assert(sizeof(IPv4Header) == 20);

View File

@ -8,15 +8,15 @@ namespace Kernel
{ {
auto& header = packet.as<IPv4Header>(); auto& header = packet.as<IPv4Header>();
header.version_IHL = 0x45; header.version_IHL = 0x45;
header.DSCP_ECN = 0x10; header.DSCP_ECN = 0x00;
header.total_length = packet.size(); header.total_length = packet.size();
header.identification = 1; header.identification = 1;
header.flags_frament = 0x00; header.flags_frament = 0x00;
header.time_to_live = 0x40; header.time_to_live = 0x40;
header.protocol = protocol; header.protocol = protocol;
header.checksum = header.calculate_checksum();
header.src_address = src_ipv4; header.src_address = src_ipv4;
header.dst_address = dst_ipv4; header.dst_address = dst_ipv4;
header.checksum = header.calculate_checksum();
} }
} }