Kernel/Net: Move internet_checksum to a more general file

Up until now, the internet_checksum function lived in IPv4.h. Due to
its use in IPv6, a better place for it nowadays would be in IP.h.

Due to how it gets used in IPv6 scenarios, it makes sense to extend it
to run over multiple smaller structs instead of one big continous chunk
of memory. This change converts internet_checksum into a class with
methods "add" and "finish", which process data and return the final
result, respectively.

This commit also fixes proper hash computation for payloads that have
an odd length. Furthermore, the function was refactored to use
a ReadonlyBytes object instead of separate data and size.

Co-Authored-By: Wanda <wanda@phinode.net>
This commit is contained in:
sdomi
2024-09-09 19:39:06 +02:00
committed by Tim Schumacher
parent 17e0ba4914
commit 2a0af461b2
3 changed files with 36 additions and 19 deletions

View File

@@ -273,7 +273,9 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet,
if (icmp_packet_size > icmp_header_size)
memcpy(response.payload, request.payload, icmp_packet_size - icmp_header_size);
response.header.checksum = internet_checksum(&response, icmp_packet_size);
InternetChecksum checksum;
checksum.add({ &response, icmp_packet_size });
response.header.checksum = checksum.finish();
adapter->send_packet(packet->bytes());
adapter->release_packet_buffer(*packet);