2019-06-07 12:43:58 +03:00
|
|
|
#include <AK/HashTable.h>
|
2019-07-08 16:38:44 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Lock.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/EtherType.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/EthernetFrameHeader.h>
|
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-03-10 17:25:33 +03:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-09-16 10:01:44 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-03-12 15:30:36 +03:00
|
|
|
|
|
|
|
static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
|
|
|
|
{
|
|
|
|
static Lockable<HashTable<NetworkAdapter*>>* table;
|
|
|
|
if (!table)
|
|
|
|
table = new Lockable<HashTable<NetworkAdapter*>>;
|
|
|
|
return *table;
|
|
|
|
}
|
|
|
|
|
2019-06-16 08:06:49 +03:00
|
|
|
void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
|
|
|
|
{
|
|
|
|
LOCKER(all_adapters().lock());
|
|
|
|
for (auto& it : all_adapters().resource())
|
|
|
|
callback(*it);
|
|
|
|
}
|
|
|
|
|
2019-08-09 05:34:32 +03:00
|
|
|
WeakPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
|
2019-03-12 15:30:36 +03:00
|
|
|
{
|
|
|
|
LOCKER(all_adapters().lock());
|
|
|
|
for (auto* adapter : all_adapters().resource()) {
|
|
|
|
if (adapter->ipv4_address() == address)
|
2019-08-09 05:34:32 +03:00
|
|
|
return adapter->make_weak_ptr();
|
2019-03-12 15:30:36 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-03-10 17:25:33 +03:00
|
|
|
|
|
|
|
NetworkAdapter::NetworkAdapter()
|
|
|
|
{
|
2019-03-12 15:30:36 +03:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().set(this);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAdapter::~NetworkAdapter()
|
|
|
|
{
|
2019-03-12 15:30:36 +03:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().remove(this);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-03-11 01:40:09 +03:00
|
|
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
|
|
|
{
|
2019-03-12 13:44:38 +03:00
|
|
|
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2019-03-13 15:12:13 +03:00
|
|
|
auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
|
|
|
|
auto* eth = (EthernetFrameHeader*)buffer.pointer();
|
2019-03-11 01:40:09 +03:00
|
|
|
eth->set_source(mac_address());
|
|
|
|
eth->set_destination(destination);
|
2019-03-12 01:21:38 +03:00
|
|
|
eth->set_ether_type(EtherType::ARP);
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += size_in_bytes;
|
2019-03-11 01:40:09 +03:00
|
|
|
memcpy(eth->payload(), &packet, sizeof(ARPPacket));
|
2019-08-28 04:22:10 +03:00
|
|
|
send_raw((const u8*)eth, size_in_bytes);
|
2019-03-11 01:40:09 +03:00
|
|
|
}
|
2019-03-11 14:43:45 +03:00
|
|
|
|
2019-08-05 11:37:09 +03:00
|
|
|
void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const u8* payload, size_t payload_size)
|
2019-03-12 06:11:20 +03:00
|
|
|
{
|
2019-08-05 11:37:09 +03:00
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
|
2019-03-12 14:43:30 +03:00
|
|
|
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);
|
2019-07-03 22:17:35 +03:00
|
|
|
ipv4.set_protocol((u8)protocol);
|
2019-08-05 11:37:09 +03:00
|
|
|
ipv4.set_length(sizeof(IPv4Packet) + payload_size);
|
2019-03-12 14:43:30 +03:00
|
|
|
ipv4.set_ident(1);
|
|
|
|
ipv4.set_ttl(64);
|
|
|
|
ipv4.set_checksum(ipv4.compute_checksum());
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += size_in_bytes;
|
2019-08-05 11:37:09 +03:00
|
|
|
memcpy(ipv4.payload(), payload, payload_size);
|
2019-07-03 22:17:35 +03:00
|
|
|
send_raw((const u8*)ð, size_in_bytes);
|
2019-03-12 06:11:20 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void NetworkAdapter::did_receive(const u8* data, int length)
|
2019-03-11 14:43:45 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_in++;
|
|
|
|
m_bytes_in += length;
|
2019-08-04 22:22:22 +03:00
|
|
|
m_packet_queue.append(KBuffer::copy(data, length));
|
2019-08-28 15:14:38 +03:00
|
|
|
if (on_receive)
|
|
|
|
on_receive();
|
2019-03-11 14:43:45 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 12:06:21 +03:00
|
|
|
Optional<KBuffer> NetworkAdapter::dequeue_packet()
|
2019-03-11 14:43:45 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
2019-06-07 12:43:58 +03:00
|
|
|
return {};
|
2019-03-11 14:43:45 +03:00
|
|
|
return m_packet_queue.take_first();
|
|
|
|
}
|
2019-03-12 01:21:38 +03:00
|
|
|
|
|
|
|
void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
|
|
|
|
{
|
|
|
|
m_ipv4_address = address;
|
|
|
|
}
|
2019-03-20 19:09:46 +03:00
|
|
|
|
2019-08-28 04:00:39 +03:00
|
|
|
void NetworkAdapter::set_ipv4_netmask(const IPv4Address& netmask)
|
|
|
|
{
|
|
|
|
m_ipv4_netmask = netmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAdapter::set_ipv4_gateway(const IPv4Address& gateway)
|
|
|
|
{
|
|
|
|
m_ipv4_gateway = gateway;
|
|
|
|
}
|
|
|
|
|
2019-06-16 08:06:49 +03:00
|
|
|
void NetworkAdapter::set_interface_name(const StringView& basename)
|
|
|
|
{
|
|
|
|
// FIXME: Find a unique name for this interface, starting with $basename.
|
2019-07-08 16:38:44 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(basename);
|
|
|
|
builder.append('0');
|
|
|
|
m_name = builder.to_string();
|
2019-06-16 08:06:49 +03:00
|
|
|
}
|