mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
Kernel: Fix up the ICMP implementation to generate correct Echo replies.
Serenity now responds to ping. :^)
This commit is contained in:
parent
75e0ddd46a
commit
9858be636f
5 changed files with 66 additions and 28 deletions
|
@ -26,7 +26,5 @@ private:
|
||||||
dword m_payload[0];
|
dword m_payload[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef dword EthernetFrameCheckSequence;
|
|
||||||
|
|
||||||
static_assert(sizeof(EthernetFrameHeader) == 14);
|
static_assert(sizeof(EthernetFrameHeader) == 14);
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
void set_code(byte b) { m_code = b; }
|
void set_code(byte b) { m_code = b; }
|
||||||
|
|
||||||
word checksum() const { return ntohs(m_checksum); }
|
word checksum() const { return ntohs(m_checksum); }
|
||||||
void set_checksum(word w) { m_checksum = htons(w); }
|
void set_checksum(word w) { m_checksum = w; }
|
||||||
|
|
||||||
const void* payload() const { return this + 1; }
|
const void* payload() const { return this + 1; }
|
||||||
void* payload() { return this + 1; }
|
void* payload() { return this + 1; }
|
||||||
|
@ -31,7 +31,7 @@ public:
|
||||||
private:
|
private:
|
||||||
byte m_type { 0 };
|
byte m_type { 0 };
|
||||||
byte m_code { 0 };
|
byte m_code { 0 };
|
||||||
word m_checksum { 0 };
|
NetworkOrdered<word> m_checksum { 0 };
|
||||||
// NOTE: The rest of the header is 4 bytes
|
// NOTE: The rest of the header is 4 bytes
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <Kernel/IPv4Address.h>
|
#include <Kernel/IPv4Address.h>
|
||||||
|
#include <Kernel/NetworkOrdered.h>
|
||||||
|
|
||||||
struct IPv4Protocol {
|
struct IPv4Protocol {
|
||||||
enum {
|
enum {
|
||||||
|
@ -9,6 +10,8 @@ enum {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
NetworkOrdered<word> internet_checksum(const void*, size_t);
|
||||||
|
|
||||||
class [[gnu::packed]] IPv4Packet {
|
class [[gnu::packed]] IPv4Packet {
|
||||||
public:
|
public:
|
||||||
byte version() const { return (m_version_and_ihl >> 4) & 0xf; }
|
byte version() const { return (m_version_and_ihl >> 4) & 0xf; }
|
||||||
|
@ -17,11 +20,11 @@ public:
|
||||||
byte internet_header_length() const { return m_version_and_ihl & 0xf; }
|
byte internet_header_length() const { return m_version_and_ihl & 0xf; }
|
||||||
void set_internet_header_length(byte ihl) { m_version_and_ihl = (m_version_and_ihl & 0xf0) | (ihl & 0x0f); }
|
void set_internet_header_length(byte ihl) { m_version_and_ihl = (m_version_and_ihl & 0xf0) | (ihl & 0x0f); }
|
||||||
|
|
||||||
word length() const { return ntohs(m_length); }
|
word length() const { return m_length; }
|
||||||
void set_length(word length) { m_length = htons(length); }
|
void set_length(word length) { m_length = length; }
|
||||||
|
|
||||||
word ident() const { return ntohs(m_ident); }
|
word ident() const { return m_ident; }
|
||||||
void set_ident(word ident) { m_ident = htons(ident); }
|
void set_ident(word ident) { m_ident = ident; }
|
||||||
|
|
||||||
byte ttl() const { return m_ttl; }
|
byte ttl() const { return m_ttl; }
|
||||||
void set_ttl(byte ttl) { m_ttl = ttl; }
|
void set_ttl(byte ttl) { m_ttl = ttl; }
|
||||||
|
@ -29,8 +32,8 @@ public:
|
||||||
byte protocol() const { return m_protocol; }
|
byte protocol() const { return m_protocol; }
|
||||||
void set_protocol(byte protocol) { m_protocol = protocol; }
|
void set_protocol(byte protocol) { m_protocol = protocol; }
|
||||||
|
|
||||||
word checksum() const { return ntohs(m_checksum); }
|
word checksum() const { return m_checksum; }
|
||||||
void set_checksum(word checksum) { m_checksum = htons(checksum); }
|
void set_checksum(word checksum) { m_checksum = checksum; }
|
||||||
|
|
||||||
const IPv4Address& source() const { return m_source; }
|
const IPv4Address& source() const { return m_source; }
|
||||||
void set_source(const IPv4Address& address) { m_source = address; }
|
void set_source(const IPv4Address& address) { m_source = address; }
|
||||||
|
@ -41,17 +44,38 @@ public:
|
||||||
void* payload() { return this + 1; }
|
void* payload() { return this + 1; }
|
||||||
const void* payload() const { return this + 1; }
|
const void* payload() const { return this + 1; }
|
||||||
|
|
||||||
|
NetworkOrdered<word> compute_checksum() const
|
||||||
|
{
|
||||||
|
ASSERT(!m_checksum);
|
||||||
|
return internet_checksum(this, sizeof(IPv4Packet));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
byte m_version_and_ihl;
|
byte m_version_and_ihl { 0 };
|
||||||
byte m_dscp_and_ecn;
|
byte m_dscp_and_ecn { 0 };
|
||||||
word m_length;
|
NetworkOrdered<word> m_length;
|
||||||
word m_ident;
|
NetworkOrdered<word> m_ident;
|
||||||
word m_flags_and_fragment;
|
NetworkOrdered<word> m_flags_and_fragment;
|
||||||
byte m_ttl;
|
byte m_ttl { 0 };
|
||||||
byte m_protocol;
|
NetworkOrdered<byte> m_protocol;
|
||||||
word m_checksum;
|
NetworkOrdered<word> m_checksum;
|
||||||
IPv4Address m_source;
|
IPv4Address m_source;
|
||||||
IPv4Address m_destination;
|
IPv4Address m_destination;
|
||||||
};
|
};
|
||||||
|
|
||||||
static_assert(sizeof(IPv4Packet) == 20);
|
static_assert(sizeof(IPv4Packet) == 20);
|
||||||
|
|
||||||
|
inline NetworkOrdered<word> internet_checksum(const void* ptr, size_t count)
|
||||||
|
{
|
||||||
|
dword checksum = 0;
|
||||||
|
auto* w = (const word*)ptr;
|
||||||
|
while (count > 1) {
|
||||||
|
checksum += convert_between_host_and_network(*w++);
|
||||||
|
if (checksum & 0x80000000)
|
||||||
|
checksum = (checksum & 0xffff) | (checksum >> 16);
|
||||||
|
count -= 2;
|
||||||
|
}
|
||||||
|
while (checksum >> 16)
|
||||||
|
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||||
|
return ~checksum & 0xffff;
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ NetworkAdapter::~NetworkAdapter()
|
||||||
|
|
||||||
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
||||||
{
|
{
|
||||||
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + sizeof(EthernetFrameCheckSequence);
|
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
||||||
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
||||||
eth->set_source(mac_address());
|
eth->set_source(mac_address());
|
||||||
eth->set_destination(destination);
|
eth->set_destination(destination);
|
||||||
|
@ -26,7 +26,7 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
|
||||||
|
|
||||||
void NetworkAdapter::send_ipv4(const MACAddress& destination, const void* packet, size_t packet_size)
|
void NetworkAdapter::send_ipv4(const MACAddress& destination, const void* packet, size_t packet_size)
|
||||||
{
|
{
|
||||||
size_t size_in_bytes = sizeof(EthernetFrameHeader) + packet_size + sizeof(EthernetFrameCheckSequence);
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + packet_size;
|
||||||
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
||||||
eth->set_source(mac_address());
|
eth->set_source(mac_address());
|
||||||
eth->set_destination(destination);
|
eth->set_destination(destination);
|
||||||
|
|
|
@ -36,7 +36,7 @@ void NetworkTask_main()
|
||||||
sleep(100);
|
sleep(100);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (packet.size() < (int)(sizeof(EthernetFrameHeader) + sizeof(EthernetFrameCheckSequence))) {
|
if (packet.size() < (int)(sizeof(EthernetFrameHeader))) {
|
||||||
kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
|
kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ void NetworkTask_main()
|
||||||
|
|
||||||
void handle_arp(const EthernetFrameHeader& eth, int frame_size)
|
void handle_arp(const EthernetFrameHeader& eth, int frame_size)
|
||||||
{
|
{
|
||||||
constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + sizeof(EthernetFrameCheckSequence);
|
constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
||||||
if (frame_size < minimum_arp_frame_size) {
|
if (frame_size < minimum_arp_frame_size) {
|
||||||
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
|
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
|
||||||
return;
|
return;
|
||||||
|
@ -131,7 +131,7 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
|
||||||
|
|
||||||
void handle_ipv4(const EthernetFrameHeader& eth, int frame_size)
|
void handle_ipv4(const EthernetFrameHeader& eth, int frame_size)
|
||||||
{
|
{
|
||||||
constexpr int minimum_ipv4_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + sizeof(EthernetFrameCheckSequence);
|
constexpr int minimum_ipv4_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet);
|
||||||
if (frame_size < minimum_ipv4_frame_size) {
|
if (frame_size < minimum_ipv4_frame_size) {
|
||||||
kprintf("handle_ipv4: Frame too small (%d, need %d)\n", frame_size, minimum_ipv4_frame_size);
|
kprintf("handle_ipv4: Frame too small (%d, need %d)\n", frame_size, minimum_ipv4_frame_size);
|
||||||
return;
|
return;
|
||||||
|
@ -159,14 +159,23 @@ void handle_icmp(const EthernetFrameHeader& eth, int frame_size)
|
||||||
(void)frame_size;
|
(void)frame_size;
|
||||||
auto& ipv4_packet = *static_cast<const IPv4Packet*>(eth.payload());
|
auto& ipv4_packet = *static_cast<const IPv4Packet*>(eth.payload());
|
||||||
auto& icmp_header = *static_cast<const ICMPHeader*>(ipv4_packet.payload());
|
auto& icmp_header = *static_cast<const ICMPHeader*>(ipv4_packet.payload());
|
||||||
kprintf("handle_icmp: type=%b, code=%b\n", icmp_header.type(), icmp_header.code());
|
#ifdef ICMP_DEBUG
|
||||||
|
kprintf("handle_icmp: source=%s, destination=%d type=%b, code=%b\n",
|
||||||
|
ipv4_packet.source().to_string().characters(),
|
||||||
|
ipv4_packet.destination().to_string().characters(),
|
||||||
|
icmp_header.type(),
|
||||||
|
icmp_header.code()
|
||||||
|
);
|
||||||
|
#endif
|
||||||
auto& e1000 = *E1000NetworkAdapter::the();
|
auto& e1000 = *E1000NetworkAdapter::the();
|
||||||
if (ipv4_packet.destination() == e1000.ipv4_address()) {
|
if (ipv4_packet.destination() == e1000.ipv4_address()) {
|
||||||
// It's for me!
|
|
||||||
if (icmp_header.type() == ICMPType::EchoRequest) {
|
if (icmp_header.type() == ICMPType::EchoRequest) {
|
||||||
auto& request = reinterpret_cast<const ICMPEchoPacket&>(icmp_header);
|
auto& request = reinterpret_cast<const ICMPEchoPacket&>(icmp_header);
|
||||||
kprintf("ICMP echo request: id=%u, seq=%u, len=%u\n", (int)request.identifier, (int)request.sequence_number, ipv4_packet.length() - sizeof(ICMPEchoPacket));
|
kprintf("handle_icmp: EchoRequest from %s: id=%u, seq=%u\n",
|
||||||
|
ipv4_packet.source().to_string().characters(),
|
||||||
|
(word)request.identifier,
|
||||||
|
(word)request.sequence_number
|
||||||
|
);
|
||||||
byte* response_buffer = (byte*)kmalloc(ipv4_packet.length());
|
byte* response_buffer = (byte*)kmalloc(ipv4_packet.length());
|
||||||
memset(response_buffer, 0, ipv4_packet.length());
|
memset(response_buffer, 0, ipv4_packet.length());
|
||||||
struct [[gnu::packed]] EchoResponse {
|
struct [[gnu::packed]] EchoResponse {
|
||||||
|
@ -180,11 +189,18 @@ void handle_icmp(const EthernetFrameHeader& eth, int frame_size)
|
||||||
response.ipv4.set_destination(ipv4_packet.source());
|
response.ipv4.set_destination(ipv4_packet.source());
|
||||||
response.ipv4.set_protocol(IPv4Protocol::ICMP);
|
response.ipv4.set_protocol(IPv4Protocol::ICMP);
|
||||||
response.ipv4.set_length(ipv4_packet.length());
|
response.ipv4.set_length(ipv4_packet.length());
|
||||||
|
response.ipv4.set_ident(1);
|
||||||
|
response.ipv4.set_ttl(64);
|
||||||
|
response.ipv4.set_checksum(response.ipv4.compute_checksum());
|
||||||
response.icmp_echo.header.set_type(ICMPType::EchoReply);
|
response.icmp_echo.header.set_type(ICMPType::EchoReply);
|
||||||
response.icmp_echo.header.set_code(0);
|
response.icmp_echo.header.set_code(0);
|
||||||
response.icmp_echo.identifier = request.identifier;
|
response.icmp_echo.identifier = request.identifier;
|
||||||
response.icmp_echo.sequence_number = request.sequence_number;
|
response.icmp_echo.sequence_number = request.sequence_number;
|
||||||
memcpy(response.icmp_echo.payload(), request.payload(), ipv4_packet.length() - sizeof(EchoResponse));
|
size_t icmp_packet_length = ipv4_packet.length() - sizeof(IPv4Packet);
|
||||||
|
size_t icmp_payload_length = ipv4_packet.length() - sizeof(EchoResponse);
|
||||||
|
memcpy(response.icmp_echo.payload(), request.payload(), icmp_payload_length);
|
||||||
|
|
||||||
|
response.icmp_echo.header.set_checksum(internet_checksum(&response.icmp_echo, icmp_packet_length));
|
||||||
e1000.send_ipv4(eth.source(), response_buffer, ipv4_packet.length());
|
e1000.send_ipv4(eth.source(), response_buffer, ipv4_packet.length());
|
||||||
kfree(response_buffer);
|
kfree(response_buffer);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue