2019-06-07 09:43:58 +00:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <Kernel/Lock.h>
|
2019-04-02 17:54:38 +00:00
|
|
|
#include <Kernel/Net/EtherType.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/Net/EthernetFrameHeader.h>
|
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-03-10 14:25:33 +00:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-03-10 22:40:09 +00:00
|
|
|
#include <Kernel/kmalloc.h>
|
2019-03-12 12:30:36 +00:00
|
|
|
|
|
|
|
static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
|
|
|
|
{
|
|
|
|
static Lockable<HashTable<NetworkAdapter*>>* table;
|
|
|
|
if (!table)
|
|
|
|
table = new Lockable<HashTable<NetworkAdapter*>>;
|
|
|
|
return *table;
|
|
|
|
}
|
|
|
|
|
2019-06-16 05:06:49 +00:00
|
|
|
void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
|
|
|
|
{
|
|
|
|
LOCKER(all_adapters().lock());
|
|
|
|
for (auto& it : all_adapters().resource())
|
|
|
|
callback(*it);
|
|
|
|
}
|
|
|
|
|
2019-03-12 12:30:36 +00:00
|
|
|
NetworkAdapter* NetworkAdapter::from_ipv4_address(const IPv4Address& address)
|
|
|
|
{
|
|
|
|
LOCKER(all_adapters().lock());
|
|
|
|
for (auto* adapter : all_adapters().resource()) {
|
|
|
|
if (adapter->ipv4_address() == address)
|
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-03-10 14:25:33 +00:00
|
|
|
|
|
|
|
NetworkAdapter::NetworkAdapter()
|
2019-03-20 16:09:46 +00:00
|
|
|
: m_packet_queue_alarm(*this)
|
2019-03-10 14:25:33 +00:00
|
|
|
{
|
2019-03-12 12:30:36 +00:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().set(this);
|
2019-03-10 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAdapter::~NetworkAdapter()
|
|
|
|
{
|
2019-03-12 12:30:36 +00:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().remove(this);
|
2019-03-10 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 22:40:09 +00:00
|
|
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
|
|
|
{
|
2019-03-12 10:44:38 +00:00
|
|
|
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2019-03-13 12:12:13 +00:00
|
|
|
auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
|
|
|
|
auto* eth = (EthernetFrameHeader*)buffer.pointer();
|
2019-03-10 22:40:09 +00:00
|
|
|
eth->set_source(mac_address());
|
|
|
|
eth->set_destination(destination);
|
2019-03-11 22:21:38 +00:00
|
|
|
eth->set_ether_type(EtherType::ARP);
|
2019-03-10 22:40:09 +00:00
|
|
|
memcpy(eth->payload(), &packet, sizeof(ARPPacket));
|
|
|
|
send_raw((byte*)eth, size_in_bytes);
|
|
|
|
}
|
2019-03-11 11:43:45 +00:00
|
|
|
|
2019-03-12 11:43:30 +00:00
|
|
|
void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, ByteBuffer&& payload)
|
2019-03-12 03:11:20 +00:00
|
|
|
{
|
2019-03-12 11:43:30 +00:00
|
|
|
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);
|
2019-03-12 03:11:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 11:43:45 +00:00
|
|
|
void NetworkAdapter::did_receive(const byte* data, int length)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_packet_queue.append(ByteBuffer::copy(data, length));
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer NetworkAdapter::dequeue_packet()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
2019-06-07 09:43:58 +00:00
|
|
|
return {};
|
2019-03-11 11:43:45 +00:00
|
|
|
return m_packet_queue.take_first();
|
|
|
|
}
|
2019-03-11 22:21:38 +00:00
|
|
|
|
|
|
|
void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
|
|
|
|
{
|
|
|
|
m_ipv4_address = address;
|
|
|
|
}
|
2019-03-20 16:09:46 +00:00
|
|
|
|
2019-06-16 05:06:49 +00:00
|
|
|
void NetworkAdapter::set_interface_name(const StringView& basename)
|
|
|
|
{
|
|
|
|
// FIXME: Find a unique name for this interface, starting with $basename.
|
|
|
|
m_name = String::format("%s0", basename.characters());
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:09:46 +00:00
|
|
|
bool PacketQueueAlarm::is_ringing() const
|
|
|
|
{
|
|
|
|
return m_adapter.has_queued_packets();
|
|
|
|
}
|