2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <AK/HashTable.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-07-08 16:38:44 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-12-14 13:07:37 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.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-12-28 02:59:52 +03:00
|
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2020-09-16 19:25:06 +03:00
|
|
|
#include <Kernel/Process.h>
|
2020-04-02 04:27:49 +03:00
|
|
|
#include <Kernel/Random.h>
|
2020-05-16 13:00:04 +03:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-03-12 15:30:36 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
static AK::Singleton<Lockable<HashTable<NetworkAdapter*>>> s_table;
|
|
|
|
|
2021-05-12 18:27:37 +03:00
|
|
|
Lockable<HashTable<NetworkAdapter*>>& NetworkAdapter::all_adapters()
|
2019-03-12 15:30:36 +03:00
|
|
|
{
|
2020-08-25 04:35:19 +03:00
|
|
|
return *s_table;
|
2019-03-12 15:30:36 +03:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:19:46 +03:00
|
|
|
RefPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
|
2019-03-12 15:30:36 +03:00
|
|
|
{
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker locker(all_adapters().lock());
|
2019-03-12 15:30:36 +03:00
|
|
|
for (auto* adapter : all_adapters().resource()) {
|
2021-04-26 16:38:26 +03:00
|
|
|
if (adapter->ipv4_address() == address || adapter->ipv4_broadcast() == address)
|
2020-02-08 02:19:46 +03:00
|
|
|
return adapter;
|
2019-03-12 15:30:36 +03:00
|
|
|
}
|
2021-05-12 13:14:03 +03:00
|
|
|
if (address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0)
|
|
|
|
return LoopbackAdapter::the();
|
2019-12-28 02:59:52 +03:00
|
|
|
if (address[0] == 127)
|
2020-02-08 02:19:46 +03:00
|
|
|
return LoopbackAdapter::the();
|
2019-03-12 15:30:36 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-03-10 17:25:33 +03:00
|
|
|
|
2020-02-08 02:19:46 +03:00
|
|
|
RefPtr<NetworkAdapter> NetworkAdapter::lookup_by_name(const StringView& name)
|
2019-09-23 20:06:03 +03:00
|
|
|
{
|
|
|
|
NetworkAdapter* found_adapter = nullptr;
|
|
|
|
for_each([&](auto& adapter) {
|
|
|
|
if (adapter.name() == name)
|
|
|
|
found_adapter = &adapter;
|
|
|
|
});
|
2020-02-08 02:19:46 +03:00
|
|
|
return found_adapter;
|
2019-09-23 20:06:03 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-07-28 21:19:22 +03:00
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2021-05-15 23:45:22 +03:00
|
|
|
auto buffer = NetworkByteBuffer::create_zeroed(size_in_bytes);
|
2019-09-30 09:57:01 +03:00
|
|
|
auto* eth = (EthernetFrameHeader*)buffer.data();
|
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));
|
2020-07-28 21:19:22 +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
|
|
|
|
2021-05-26 06:35:05 +03:00
|
|
|
void NetworkAdapter::fill_in_ipv4_header(PacketWithTimestamp& packet, IPv4Address const& source_ipv4, MACAddress const& destination_mac, IPv4Address const& destination_ipv4, IPv4Protocol protocol, size_t payload_size, u8 ttl)
|
2019-03-12 06:11:20 +03:00
|
|
|
{
|
2020-09-12 06:11:07 +03:00
|
|
|
size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
|
2021-05-26 06:35:05 +03:00
|
|
|
VERIFY(ipv4_packet_size <= mtu());
|
2019-11-28 09:12:05 +03:00
|
|
|
|
2021-05-26 06:35:05 +03:00
|
|
|
size_t ethernet_frame_size = ipv4_payload_offset() + payload_size;
|
|
|
|
VERIFY(packet.buffer.size() == ethernet_frame_size);
|
|
|
|
memset(packet.buffer.data(), 0, ipv4_payload_offset());
|
|
|
|
auto& eth = *(EthernetFrameHeader*)packet.buffer.data();
|
2019-03-12 14:43:30 +03:00
|
|
|
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);
|
2021-05-12 15:36:09 +03:00
|
|
|
ipv4.set_source(source_ipv4);
|
2019-03-12 14:43:30 +03:00
|
|
|
ipv4.set_destination(destination_ipv4);
|
2019-07-03 22:17:35 +03:00
|
|
|
ipv4.set_protocol((u8)protocol);
|
2020-09-12 06:11:07 +03:00
|
|
|
ipv4.set_length(sizeof(IPv4Packet) + payload_size);
|
2019-03-12 14:43:30 +03:00
|
|
|
ipv4.set_ident(1);
|
2019-09-19 22:40:06 +03:00
|
|
|
ipv4.set_ttl(ttl);
|
2019-03-12 14:43:30 +03:00
|
|
|
ipv4.set_checksum(ipv4.compute_checksum());
|
2020-04-02 04:27:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:19:22 +03:00
|
|
|
void NetworkAdapter::did_receive(ReadonlyBytes payload)
|
2019-03-11 14:43:45 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_in++;
|
2020-07-28 21:19:22 +03:00
|
|
|
m_bytes_in += payload.size();
|
2019-12-14 13:07:37 +03:00
|
|
|
|
2021-05-06 21:22:57 +03:00
|
|
|
if (m_packet_queue_size == max_packet_buffers) {
|
|
|
|
// FIXME: Keep track of the number of dropped packets
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:21:19 +03:00
|
|
|
auto packet = acquire_packet_buffer(payload.size());
|
2021-05-14 10:52:49 +03:00
|
|
|
if (!packet) {
|
|
|
|
dbgln("Discarding packet because we're out of memory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:21:19 +03:00
|
|
|
memcpy(packet->buffer.data(), payload.data(), payload.size());
|
|
|
|
|
2021-05-14 10:52:49 +03:00
|
|
|
m_packet_queue.append(*packet);
|
2021-05-06 21:22:57 +03:00
|
|
|
m_packet_queue_size++;
|
2019-12-14 13:07:37 +03:00
|
|
|
|
2019-08-28 15:14:38 +03:00
|
|
|
if (on_receive)
|
|
|
|
on_receive();
|
2019-03-11 14:43:45 +03:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp)
|
2019-03-11 14:43:45 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
2019-12-14 13:07:37 +03:00
|
|
|
return 0;
|
2020-09-16 19:25:06 +03:00
|
|
|
auto packet_with_timestamp = m_packet_queue.take_first();
|
2021-05-06 21:22:57 +03:00
|
|
|
m_packet_queue_size--;
|
2021-05-14 10:52:49 +03:00
|
|
|
packet_timestamp = packet_with_timestamp->timestamp;
|
|
|
|
auto& packet_buffer = packet_with_timestamp->buffer;
|
|
|
|
size_t packet_size = packet_buffer.size();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(packet_size <= buffer_size);
|
2021-05-14 10:52:49 +03:00
|
|
|
memcpy(buffer, packet_buffer.data(), packet_size);
|
2021-05-26 05:21:19 +03:00
|
|
|
release_packet_buffer(*packet_with_timestamp);
|
2019-12-14 13:07:37 +03:00
|
|
|
return packet_size;
|
2019-03-11 14:43:45 +03:00
|
|
|
}
|
2019-03-12 01:21:38 +03:00
|
|
|
|
2021-05-26 05:21:19 +03:00
|
|
|
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_unused_packets.is_empty()) {
|
|
|
|
auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
|
|
|
|
auto packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() });
|
2021-05-26 06:35:05 +03:00
|
|
|
if (!packet)
|
|
|
|
return nullptr;
|
2021-05-26 05:21:19 +03:00
|
|
|
packet->buffer.set_size(size);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto packet = m_unused_packets.take_first();
|
|
|
|
if (packet->buffer.capacity() >= size) {
|
|
|
|
packet->timestamp = kgettimeofday();
|
|
|
|
packet->buffer.set_size(size);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
|
|
|
|
packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() });
|
2021-05-26 06:35:05 +03:00
|
|
|
if (!packet)
|
|
|
|
return nullptr;
|
2021-05-26 05:21:19 +03:00
|
|
|
packet->buffer.set_size(size);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_unused_packets.append(packet);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-22 01:35:46 +03:00
|
|
|
void NetworkAdapter::set_interface_name(const PCI::Address& pci_address)
|
2019-06-16 08:06:49 +03:00
|
|
|
{
|
2021-05-22 01:35:46 +03:00
|
|
|
// Note: This stands for e - "Ethernet", p - "Port" as for PCI bus, "s" for slot as for PCI slot
|
|
|
|
auto name = String::formatted("ep{}s{}", pci_address.bus(), pci_address.device());
|
|
|
|
VERIFY(!lookup_by_name(name));
|
|
|
|
m_name = move(name);
|
2019-06-16 08:06:49 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-05-22 01:35:46 +03:00
|
|
|
void NetworkAdapter::set_loopback_name()
|
|
|
|
{
|
|
|
|
auto name = String("loop");
|
|
|
|
VERIFY(!lookup_by_name(name));
|
|
|
|
m_name = move(name);
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|