summaryrefslogtreecommitdiff
path: root/Kernel/NetworkTask.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-12 00:56:33 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-12 00:56:33 +0100
commit90f60d2f650a417a8ffd002d2cfa22605941069f (patch)
tree0dcb188a31c62dd327d75eae1af873dd51e2c997 /Kernel/NetworkTask.cpp
parent05c1a79454cb8170a1503603d6a8dd743215de28 (diff)
downloadserenity-90f60d2f650a417a8ffd002d2cfa22605941069f.zip
Kernel: Cache MAC<->IP mappings (from ARP responses) seen on the wire.
Diffstat (limited to 'Kernel/NetworkTask.cpp')
-rw-r--r--Kernel/NetworkTask.cpp25
1 files changed, 24 insertions, 1 deletions
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 <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());
+ }
}
}