From 90f60d2f650a417a8ffd002d2cfa22605941069f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 12 Mar 2019 00:56:33 +0100 Subject: Kernel: Cache MAC<->IP mappings (from ARP responses) seen on the wire. --- Kernel/NetworkTask.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'Kernel/NetworkTask.cpp') diff --git a/Kernel/NetworkTask.cpp b/Kernel/NetworkTask.cpp index 867266d855..e8a5404bb4 100644 --- a/Kernel/NetworkTask.cpp +++ b/Kernel/NetworkTask.cpp @@ -3,10 +3,19 @@ #include #include #include +#include static void handle_arp(const EthernetFrameHeader&, int frame_size); static void handle_ipv4(const EthernetFrameHeader&, int frame_size); +Lockable>& arp_table() +{ + static Lockable>* the; + if (!the) + the = new Lockable>; + 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(eth.payload()); + auto& packet = *static_cast(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()); + } } } -- cgit v1.2.3