Kernel: Cache MAC<->IP mappings (from ARP responses) seen on the wire.

This commit is contained in:
Andreas Kling 2019-03-12 00:56:33 +01:00
parent 05c1a79454
commit 90f60d2f65
Notes: sideshowbarker 2024-07-19 15:05:00 +09:00
5 changed files with 67 additions and 14 deletions

View file

@ -155,20 +155,10 @@ slow_path:
void StringImpl::compute_hash() const
{
if (!length()) {
if (!length())
m_hash = 0;
} else {
unsigned hash = 0;
for (ssize_t i = 0; i < m_length; ++i) {
hash += m_characters[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
m_hash = hash;
}
else
m_hash = string_hash(m_characters, m_length);
m_hasHash = true;
}

View file

@ -47,7 +47,22 @@ private:
char m_inline_buffer[0];
};
inline dword string_hash(const char* characters, int length)
{
dword hash = 0;
for (int i = 0; i < length; ++i) {
hash += (dword)characters[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
}
using AK::StringImpl;
using AK::Chomp;
using AK::string_hash;

View file

@ -42,3 +42,13 @@ private:
};
static_assert(sizeof(IPv4Address) == 4);
namespace AK {
template<>
struct Traits<IPv4Address> {
static unsigned hash(const IPv4Address& address) { return string_hash((const char*)&address, sizeof(address)); }
static void dump(const IPv4Address& address) { kprintf("%s", address.to_string().characters()); }
};
}

View file

@ -20,6 +20,11 @@ public:
return m_data[i];
}
bool operator==(const MACAddress& other) const
{
return !memcmp(m_data, other.m_data, sizeof(m_data));
}
String to_string() const
{
return String::format("%b:%b:%b:%b:%b:%b", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
@ -30,3 +35,13 @@ private:
};
static_assert(sizeof(MACAddress) == 6);
namespace AK {
template<>
struct Traits<MACAddress> {
static unsigned hash(const MACAddress& address) { return string_hash((const char*)&address, sizeof(address)); }
static void dump(const MACAddress& address) { kprintf("%s", address.to_string().characters()); }
};
}

View file

@ -3,10 +3,19 @@
#include <Kernel/ARPPacket.h>
#include <Kernel/Process.h>
#include <Kernel/EtherType.h>
#include <AK/Lock.h>
static void handle_arp(const EthernetFrameHeader&, int frame_size);
static void handle_ipv4(const EthernetFrameHeader&, int frame_size);
Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
{
static Lockable<HashMap<IPv4Address, MACAddress>>* the;
if (!the)
the = new Lockable<HashMap<IPv4Address, MACAddress>>;
return *the;
}
void NetworkTask_main()
{
auto* e1000_ptr = E1000NetworkAdapter::the();
@ -59,7 +68,7 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
return;
}
const ARPPacket& packet = *static_cast<const ARPPacket*>(eth.payload());
auto& packet = *static_cast<const ARPPacket*>(eth.payload());
if (packet.hardware_type() != 1 || packet.hardware_address_length() != sizeof(MACAddress)) {
kprintf("handle_arp: Hardware type not ethernet (%w, len=%u)\n",
packet.hardware_type(),
@ -104,6 +113,20 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
e1000.send(packet.sender_hardware_address(), response);
}
return;
}
if (packet.operation() == 2) {
// Someone has this IPv4 address. I guess we can try to remember that.
// FIXME: Protect against ARP spamming.
// FIXME: Support static ARP table entries.
LOCKER(arp_table().lock());
arp_table().resource().set(packet.sender_protocol_address(), packet.sender_hardware_address());
kprintf("ARP table (%d entries):\n", arp_table().resource().size());
for (auto& it : arp_table().resource()) {
kprintf("%s :: %s\n", it.value.to_string().characters(), it.key.to_string().characters());
}
}
}