mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
Kernel: More work on the ICMP and IPv4 support.
This commit is contained in:
parent
9858be636f
commit
87ecf290f4
Notes:
sideshowbarker
2024-07-19 15:04:48 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/87ecf290f47
9 changed files with 63 additions and 53 deletions
|
@ -12,6 +12,7 @@ template<typename T>
|
|||
class Buffer : public Retainable<Buffer<T>> {
|
||||
public:
|
||||
static Retained<Buffer> create_uninitialized(ssize_t count);
|
||||
static Retained<Buffer> create_zeroed(ssize_t count);
|
||||
static Retained<Buffer> copy(const T*, ssize_t count);
|
||||
static Retained<Buffer> wrap(T*, ssize_t count);
|
||||
static Retained<Buffer> adopt(T*, ssize_t count);
|
||||
|
@ -110,6 +111,14 @@ inline Retained<Buffer<T>> Buffer<T>::create_uninitialized(ssize_t size)
|
|||
return ::adopt(*new Buffer<T>(size));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Retained<Buffer<T>> Buffer<T>::create_zeroed(ssize_t size)
|
||||
{
|
||||
auto buffer = ::adopt(*new Buffer<T>(size));
|
||||
memset(buffer->pointer(), 0, size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Retained<Buffer<T>> Buffer<T>::copy(const T* elements, ssize_t size)
|
||||
{
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
}
|
||||
|
||||
static ByteBuffer create_uninitialized(ssize_t size) { return ByteBuffer(Buffer<byte>::create_uninitialized(size)); }
|
||||
static ByteBuffer create_zeroed(ssize_t size) { return ByteBuffer(Buffer<byte>::create_zeroed(size)); }
|
||||
static ByteBuffer copy(const byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::copy(data, size)); }
|
||||
static ByteBuffer wrap(byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::wrap(data, size)); }
|
||||
static ByteBuffer adopt(byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::adopt(data, size)); }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/MACAddress.h>
|
||||
#include <Kernel/NetworkOrdered.h>
|
||||
|
||||
class [[gnu::packed]] EthernetFrameHeader {
|
||||
public:
|
||||
|
@ -13,8 +14,8 @@ public:
|
|||
MACAddress source() const { return m_source; }
|
||||
void set_source(const MACAddress& address) { m_source = address; }
|
||||
|
||||
word ether_type() const { return ntohs(m_ether_type); }
|
||||
void set_ether_type(word ether_type) { m_ether_type = htons(ether_type); }
|
||||
word ether_type() const { return m_ether_type; }
|
||||
void set_ether_type(word ether_type) { m_ether_type = ether_type; }
|
||||
|
||||
const void* payload() const { return &m_payload[0]; }
|
||||
void* payload() { return &m_payload[0]; }
|
||||
|
@ -22,7 +23,7 @@ public:
|
|||
private:
|
||||
MACAddress m_destination;
|
||||
MACAddress m_source;
|
||||
word m_ether_type { 0 };
|
||||
NetworkOrdered<word> m_ether_type;
|
||||
dword m_payload[0];
|
||||
};
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ public:
|
|||
byte code() const { return m_code; }
|
||||
void set_code(byte b) { m_code = b; }
|
||||
|
||||
word checksum() const { return ntohs(m_checksum); }
|
||||
word checksum() const { return m_checksum; }
|
||||
void set_checksum(word w) { m_checksum = w; }
|
||||
|
||||
const void* payload() const { return this + 1; }
|
||||
|
|
|
@ -33,6 +33,7 @@ public:
|
|||
}
|
||||
|
||||
bool operator==(const IPv4Address& other) const { return m_data_as_dword == other.m_data_as_dword; }
|
||||
bool operator!=(const IPv4Address& other) const { return m_data_as_dword != other.m_data_as_dword; }
|
||||
|
||||
private:
|
||||
union {
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
#include <Kernel/IPv4Address.h>
|
||||
#include <Kernel/NetworkOrdered.h>
|
||||
|
||||
struct IPv4Protocol {
|
||||
enum {
|
||||
enum class IPv4Protocol : word {
|
||||
ICMP = 1,
|
||||
};
|
||||
TCP = 6,
|
||||
UDP = 17,
|
||||
};
|
||||
|
||||
NetworkOrdered<word> internet_checksum(const void*, size_t);
|
||||
|
@ -44,6 +44,8 @@ public:
|
|||
void* payload() { return this + 1; }
|
||||
const void* payload() const { return this + 1; }
|
||||
|
||||
word payload_size() const { return m_length - sizeof(IPv4Packet); }
|
||||
|
||||
NetworkOrdered<word> compute_checksum() const
|
||||
{
|
||||
ASSERT(!m_checksum);
|
||||
|
|
|
@ -24,19 +24,28 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
|
|||
kfree(eth);
|
||||
}
|
||||
|
||||
void NetworkAdapter::send_ipv4(const MACAddress& destination, const void* packet, size_t packet_size)
|
||||
void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, ByteBuffer&& payload)
|
||||
{
|
||||
size_t size_in_bytes = sizeof(EthernetFrameHeader) + packet_size;
|
||||
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
||||
eth->set_source(mac_address());
|
||||
eth->set_destination(destination);
|
||||
eth->set_ether_type(EtherType::IPv4);
|
||||
memcpy(eth->payload(), packet, packet_size);
|
||||
send_raw((byte*)eth, size_in_bytes);
|
||||
kfree(eth);
|
||||
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload.size();
|
||||
auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
|
||||
auto& eth = *(EthernetFrameHeader*)buffer.pointer();
|
||||
eth.set_source(mac_address());
|
||||
eth.set_destination(destination_mac);
|
||||
eth.set_ether_type(EtherType::IPv4);
|
||||
auto& ipv4 = *(IPv4Packet*)eth.payload();
|
||||
ipv4.set_version(4);
|
||||
ipv4.set_internet_header_length(5);
|
||||
ipv4.set_source(ipv4_address());
|
||||
ipv4.set_destination(destination_ipv4);
|
||||
ipv4.set_protocol((byte)protocol);
|
||||
ipv4.set_length(sizeof(IPv4Packet) + payload.size());
|
||||
ipv4.set_ident(1);
|
||||
ipv4.set_ttl(64);
|
||||
ipv4.set_checksum(ipv4.compute_checksum());
|
||||
memcpy(ipv4.payload(), payload.pointer(), payload.size());
|
||||
send_raw((const byte*)ð, size_in_bytes);
|
||||
}
|
||||
|
||||
|
||||
void NetworkAdapter::did_receive(const byte* data, int length)
|
||||
{
|
||||
InterruptDisabler disabler;
|
||||
|
|
|
@ -20,7 +20,7 @@ public:
|
|||
void set_ipv4_address(const IPv4Address&);
|
||||
|
||||
void send(const MACAddress&, const ARPPacket&);
|
||||
void send_ipv4(const MACAddress&, const void*, size_t);
|
||||
void send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, ByteBuffer&& payload);
|
||||
|
||||
ByteBuffer dequeue_packet();
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ void handle_ipv4(const EthernetFrameHeader& eth, int frame_size)
|
|||
);
|
||||
#endif
|
||||
|
||||
switch (packet.protocol()) {
|
||||
switch ((IPv4Protocol)packet.protocol()) {
|
||||
case IPv4Protocol::ICMP:
|
||||
return handle_icmp(eth, frame_size);
|
||||
default:
|
||||
|
@ -167,42 +167,29 @@ void handle_icmp(const EthernetFrameHeader& eth, int frame_size)
|
|||
icmp_header.code()
|
||||
);
|
||||
#endif
|
||||
auto& e1000 = *E1000NetworkAdapter::the();
|
||||
if (ipv4_packet.destination() == e1000.ipv4_address()) {
|
||||
if (icmp_header.type() == ICMPType::EchoRequest) {
|
||||
auto& request = reinterpret_cast<const ICMPEchoPacket&>(icmp_header);
|
||||
kprintf("handle_icmp: EchoRequest from %s: id=%u, seq=%u\n",
|
||||
|
||||
// FIXME: Get adapater via lookup.
|
||||
auto& adapter = *E1000NetworkAdapter::the();
|
||||
if (ipv4_packet.destination() != adapter.ipv4_address())
|
||||
return;
|
||||
|
||||
if (icmp_header.type() == ICMPType::EchoRequest) {
|
||||
auto& request = reinterpret_cast<const ICMPEchoPacket&>(icmp_header);
|
||||
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());
|
||||
memset(response_buffer, 0, ipv4_packet.length());
|
||||
struct [[gnu::packed]] EchoResponse {
|
||||
IPv4Packet ipv4;
|
||||
ICMPEchoPacket icmp_echo;
|
||||
};
|
||||
auto& response = *(EchoResponse*)response_buffer;
|
||||
response.ipv4.set_version(4);
|
||||
response.ipv4.set_internet_header_length(5);
|
||||
response.ipv4.set_source(e1000.ipv4_address());
|
||||
response.ipv4.set_destination(ipv4_packet.source());
|
||||
response.ipv4.set_protocol(IPv4Protocol::ICMP);
|
||||
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_code(0);
|
||||
response.icmp_echo.identifier = request.identifier;
|
||||
response.icmp_echo.sequence_number = request.sequence_number;
|
||||
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());
|
||||
kfree(response_buffer);
|
||||
}
|
||||
);
|
||||
size_t icmp_packet_size = ipv4_packet.payload_size();
|
||||
auto buffer = ByteBuffer::create_zeroed(icmp_packet_size);
|
||||
auto& response = *(ICMPEchoPacket*)buffer.pointer();
|
||||
response.header.set_type(ICMPType::EchoReply);
|
||||
response.header.set_code(0);
|
||||
response.identifier = request.identifier;
|
||||
response.sequence_number = request.sequence_number;
|
||||
if (size_t icmp_payload_size = icmp_packet_size - sizeof(ICMPEchoPacket))
|
||||
memcpy(response.payload(), request.payload(), icmp_payload_size);
|
||||
response.header.set_checksum(internet_checksum(&response, icmp_packet_size));
|
||||
adapter.send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, move(buffer));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue