2019-03-11 12:43:45 +01:00
|
|
|
#include <Kernel/E1000NetworkAdapter.h>
|
|
|
|
#include <Kernel/EthernetFrameHeader.h>
|
2019-03-12 12:49:01 +01:00
|
|
|
#include <Kernel/ARP.h>
|
2019-03-12 04:11:20 +01:00
|
|
|
#include <Kernel/ICMP.h>
|
2019-03-13 14:22:27 +01:00
|
|
|
#include <Kernel/UDP.h>
|
2019-03-12 12:49:01 +01:00
|
|
|
#include <Kernel/IPv4.h>
|
2019-03-12 17:27:07 +01:00
|
|
|
#include <Kernel/IPv4Socket.h>
|
2019-03-11 12:43:45 +01:00
|
|
|
#include <Kernel/Process.h>
|
2019-03-11 23:21:38 +01:00
|
|
|
#include <Kernel/EtherType.h>
|
2019-03-12 00:56:33 +01:00
|
|
|
#include <AK/Lock.h>
|
2019-03-11 23:21:38 +01:00
|
|
|
|
2019-03-12 04:11:20 +01:00
|
|
|
//#define ETHERNET_DEBUG
|
|
|
|
//#define IPV4_DEBUG
|
2019-03-13 14:22:27 +01:00
|
|
|
//#define ICMP_DEBUG
|
|
|
|
#define UDP_DEBUG
|
2019-03-12 04:11:20 +01:00
|
|
|
|
2019-03-11 23:21:38 +01:00
|
|
|
static void handle_arp(const EthernetFrameHeader&, int frame_size);
|
|
|
|
static void handle_ipv4(const EthernetFrameHeader&, int frame_size);
|
2019-03-12 04:11:20 +01:00
|
|
|
static void handle_icmp(const EthernetFrameHeader&, int frame_size);
|
2019-03-13 14:22:27 +01:00
|
|
|
static void handle_udp(const EthernetFrameHeader&, int frame_size);
|
2019-03-11 12:43:45 +01:00
|
|
|
|
2019-03-12 00:56:33 +01:00
|
|
|
Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
|
|
|
|
{
|
|
|
|
static Lockable<HashMap<IPv4Address, MACAddress>>* the;
|
|
|
|
if (!the)
|
|
|
|
the = new Lockable<HashMap<IPv4Address, MACAddress>>;
|
|
|
|
return *the;
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:43:45 +01:00
|
|
|
void NetworkTask_main()
|
|
|
|
{
|
|
|
|
auto* e1000_ptr = E1000NetworkAdapter::the();
|
|
|
|
ASSERT(e1000_ptr);
|
|
|
|
auto& e1000 = *e1000_ptr;
|
2019-03-11 23:21:38 +01:00
|
|
|
e1000.set_ipv4_address(IPv4Address(192, 168, 5, 2));
|
2019-03-11 12:43:45 +01:00
|
|
|
|
|
|
|
kprintf("NetworkTask: Enter main loop.\n");
|
|
|
|
for (;;) {
|
|
|
|
auto packet = e1000.dequeue_packet();
|
|
|
|
if (packet.is_null()) {
|
2019-03-12 13:30:36 +01:00
|
|
|
sleep(100);
|
2019-03-11 12:43:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-12 11:44:38 +01:00
|
|
|
if (packet.size() < (int)(sizeof(EthernetFrameHeader))) {
|
2019-03-11 12:43:45 +01:00
|
|
|
kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
auto& eth = *(const EthernetFrameHeader*)packet.pointer();
|
2019-03-12 04:11:20 +01:00
|
|
|
#ifdef ETHERNET_DEBUG
|
2019-03-11 23:21:38 +01:00
|
|
|
kprintf("NetworkTask: From %s to %s, ether_type=%w, packet_length=%u\n",
|
|
|
|
eth.source().to_string().characters(),
|
|
|
|
eth.destination().to_string().characters(),
|
|
|
|
eth.ether_type(),
|
|
|
|
packet.size()
|
|
|
|
);
|
2019-03-12 04:11:20 +01:00
|
|
|
#endif
|
2019-03-11 23:21:38 +01:00
|
|
|
|
|
|
|
switch (eth.ether_type()) {
|
|
|
|
case EtherType::ARP:
|
|
|
|
handle_arp(eth, packet.size());
|
|
|
|
break;
|
|
|
|
case EtherType::IPv4:
|
|
|
|
handle_ipv4(eth, packet.size());
|
|
|
|
break;
|
|
|
|
}
|
2019-03-11 12:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
|
|
|
|
void handle_arp(const EthernetFrameHeader& eth, int frame_size)
|
|
|
|
{
|
2019-03-12 11:44:38 +01:00
|
|
|
constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2019-03-11 23:21:38 +01:00
|
|
|
if (frame_size < minimum_arp_frame_size) {
|
|
|
|
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-12 00:56:33 +01:00
|
|
|
auto& packet = *static_cast<const ARPPacket*>(eth.payload());
|
2019-03-12 00:01:07 +01:00
|
|
|
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(),
|
|
|
|
packet.hardware_address_length()
|
|
|
|
);
|
2019-03-11 23:21:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-03-12 00:01:07 +01:00
|
|
|
if (packet.protocol_type() != EtherType::IPv4 || packet.protocol_address_length() != sizeof(IPv4Address)) {
|
|
|
|
kprintf("handle_arp: Protocol type not IPv4 (%w, len=%u)\n",
|
|
|
|
packet.hardware_type(),
|
|
|
|
packet.protocol_address_length()
|
|
|
|
);
|
2019-03-11 23:21:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
kprintf("handle_arp: operation=%w, sender=%s/%s, target=%s/%s\n",
|
2019-03-12 00:01:07 +01:00
|
|
|
packet.operation(),
|
|
|
|
packet.sender_hardware_address().to_string().characters(),
|
|
|
|
packet.sender_protocol_address().to_string().characters(),
|
|
|
|
packet.target_hardware_address().to_string().characters(),
|
|
|
|
packet.target_protocol_address().to_string().characters()
|
2019-03-11 23:21:38 +01:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2019-03-12 01:30:49 +01:00
|
|
|
if (packet.operation() == ARPOperation::Request) {
|
2019-03-11 23:21:38 +01:00
|
|
|
// Who has this IP address?
|
2019-03-12 13:30:36 +01:00
|
|
|
if (auto* adapter = NetworkAdapter::from_ipv4_address(packet.target_protocol_address())) {
|
2019-03-11 23:21:38 +01:00
|
|
|
// We do!
|
2019-03-12 00:01:07 +01:00
|
|
|
kprintf("handle_arp: Responding to ARP request for my IPv4 address (%s)\n",
|
2019-03-12 13:30:36 +01:00
|
|
|
adapter->ipv4_address().to_string().characters());
|
2019-03-11 23:21:38 +01:00
|
|
|
ARPPacket response;
|
2019-03-12 01:30:49 +01:00
|
|
|
response.set_operation(ARPOperation::Response);
|
2019-03-12 00:01:07 +01:00
|
|
|
response.set_target_hardware_address(packet.sender_hardware_address());
|
|
|
|
response.set_target_protocol_address(packet.sender_protocol_address());
|
2019-03-12 13:30:36 +01:00
|
|
|
response.set_sender_hardware_address(adapter->mac_address());
|
|
|
|
response.set_sender_protocol_address(adapter->ipv4_address());
|
2019-03-11 23:21:38 +01:00
|
|
|
|
2019-03-12 13:30:36 +01:00
|
|
|
adapter->send(packet.sender_hardware_address(), response);
|
2019-03-11 23:21:38 +01:00
|
|
|
}
|
2019-03-12 00:56:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:30:49 +01:00
|
|
|
if (packet.operation() == ARPOperation::Response) {
|
2019-03-12 00:56:33 +01:00
|
|
|
// 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());
|
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_ipv4(const EthernetFrameHeader& eth, int frame_size)
|
|
|
|
{
|
2019-03-12 11:44:38 +01:00
|
|
|
constexpr int minimum_ipv4_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet);
|
2019-03-12 04:11:20 +01:00
|
|
|
if (frame_size < minimum_ipv4_frame_size) {
|
|
|
|
kprintf("handle_ipv4: Frame too small (%d, need %d)\n", frame_size, minimum_ipv4_frame_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& packet = *static_cast<const IPv4Packet*>(eth.payload());
|
2019-03-11 23:21:38 +01:00
|
|
|
|
2019-03-12 04:11:20 +01:00
|
|
|
#ifdef IPV4_DEBUG
|
|
|
|
kprintf("handle_ipv4: source=%s, target=%s\n",
|
|
|
|
packet.source().to_string().characters(),
|
|
|
|
packet.destination().to_string().characters()
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2019-03-12 12:43:30 +01:00
|
|
|
switch ((IPv4Protocol)packet.protocol()) {
|
2019-03-12 04:11:20 +01:00
|
|
|
case IPv4Protocol::ICMP:
|
|
|
|
return handle_icmp(eth, frame_size);
|
2019-03-13 14:22:27 +01:00
|
|
|
case IPv4Protocol::UDP:
|
|
|
|
return handle_udp(eth, frame_size);
|
2019-03-12 04:11:20 +01:00
|
|
|
default:
|
|
|
|
kprintf("handle_ipv4: Unhandled protocol %u\n", packet.protocol());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_icmp(const EthernetFrameHeader& eth, int frame_size)
|
|
|
|
{
|
2019-03-12 04:39:50 +01:00
|
|
|
(void)frame_size;
|
2019-03-12 04:11:20 +01:00
|
|
|
auto& ipv4_packet = *static_cast<const IPv4Packet*>(eth.payload());
|
|
|
|
auto& icmp_header = *static_cast<const ICMPHeader*>(ipv4_packet.payload());
|
2019-03-12 11:44:38 +01:00
|
|
|
#ifdef ICMP_DEBUG
|
2019-03-13 14:22:27 +01:00
|
|
|
kprintf("handle_icmp: source=%s, destination=%s, type=%b, code=%b\n",
|
2019-03-12 11:44:38 +01:00
|
|
|
ipv4_packet.source().to_string().characters(),
|
|
|
|
ipv4_packet.destination().to_string().characters(),
|
|
|
|
icmp_header.type(),
|
|
|
|
icmp_header.code()
|
|
|
|
);
|
|
|
|
#endif
|
2019-03-12 12:43:30 +01:00
|
|
|
|
2019-03-12 17:27:07 +01:00
|
|
|
{
|
|
|
|
LOCKER(IPv4Socket::all_sockets().lock());
|
|
|
|
for (RetainPtr<IPv4Socket> socket : IPv4Socket::all_sockets().resource()) {
|
|
|
|
LOCKER(socket->lock());
|
|
|
|
if (socket->protocol() != (unsigned)IPv4Protocol::ICMP)
|
|
|
|
continue;
|
|
|
|
socket->did_receive(ByteBuffer::copy((const byte*)&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:30:36 +01:00
|
|
|
auto* adapter = NetworkAdapter::from_ipv4_address(ipv4_packet.destination());
|
|
|
|
if (!adapter)
|
2019-03-12 12:43:30 +01:00
|
|
|
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",
|
2019-03-12 11:44:38 +01:00
|
|
|
ipv4_packet.source().to_string().characters(),
|
|
|
|
(word)request.identifier,
|
|
|
|
(word)request.sequence_number
|
2019-03-12 12:43:30 +01:00
|
|
|
);
|
|
|
|
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));
|
2019-03-12 13:30:36 +01:00
|
|
|
adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, move(buffer));
|
2019-03-12 04:11:20 +01:00
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
}
|
2019-03-13 14:22:27 +01:00
|
|
|
|
|
|
|
void handle_udp(const EthernetFrameHeader& eth, int frame_size)
|
|
|
|
{
|
|
|
|
(void)frame_size;
|
|
|
|
auto& ipv4_packet = *static_cast<const IPv4Packet*>(eth.payload());
|
|
|
|
|
|
|
|
auto* adapter = NetworkAdapter::from_ipv4_address(ipv4_packet.destination());
|
|
|
|
if (!adapter) {
|
|
|
|
kprintf("handle_udp: this packet is not for me, it's for %s\n", ipv4_packet.destination().to_string().characters());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
|
|
|
|
#ifdef UDP_DEBUG
|
|
|
|
kprintf("handle_udp: source=%s:%u, destination=%s:%u length=%u\n",
|
|
|
|
ipv4_packet.source().to_string().characters(),
|
|
|
|
udp_packet.source_port(),
|
|
|
|
ipv4_packet.destination().to_string().characters(),
|
|
|
|
udp_packet.destination_port(),
|
|
|
|
udp_packet.length()
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LOCKER(IPv4Socket::all_sockets().lock());
|
|
|
|
for (RetainPtr<IPv4Socket> socket : IPv4Socket::all_sockets().resource()) {
|
|
|
|
LOCKER(socket->lock());
|
2019-03-13 15:40:30 +01:00
|
|
|
if (socket->type() != SOCK_DGRAM)
|
2019-03-13 14:22:27 +01:00
|
|
|
continue;
|
|
|
|
if (socket->source_port() != udp_packet.destination_port())
|
|
|
|
continue;
|
|
|
|
socket->did_receive(ByteBuffer::copy((const byte*)&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|