2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/HashTable.h>
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-07-08 15:38:44 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-12-14 11:07:37 +01:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Lock.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/EtherType.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/EthernetFrameHeader.h>
|
2019-12-28 10:59:52 +11:00
|
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2020-09-16 12:25:06 -04:00
|
|
|
#include <Kernel/Process.h>
|
2020-04-02 05:57:49 +04:30
|
|
|
#include <Kernel/Random.h>
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-03-12 13:30:36 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
static AK::Singleton<Lockable<HashTable<NetworkAdapter*>>> s_table;
|
|
|
|
|
2021-05-12 17:27:37 +02:00
|
|
|
Lockable<HashTable<NetworkAdapter*>>& NetworkAdapter::all_adapters()
|
2019-03-12 13:30:36 +01:00
|
|
|
{
|
2020-08-24 19:35:19 -06:00
|
|
|
return *s_table;
|
2019-03-12 13:30:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 00:19:46 +01:00
|
|
|
RefPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
|
2019-03-12 13:30:36 +01:00
|
|
|
{
|
2021-04-24 15:27:32 -07:00
|
|
|
Locker locker(all_adapters().lock());
|
2019-03-12 13:30:36 +01:00
|
|
|
for (auto* adapter : all_adapters().resource()) {
|
2021-04-26 15:38:26 +02:00
|
|
|
if (adapter->ipv4_address() == address || adapter->ipv4_broadcast() == address)
|
2020-02-08 00:19:46 +01:00
|
|
|
return adapter;
|
2019-03-12 13:30:36 +01:00
|
|
|
}
|
2021-05-12 12:14:03 +02:00
|
|
|
if (address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0)
|
|
|
|
return LoopbackAdapter::the();
|
2019-12-28 10:59:52 +11:00
|
|
|
if (address[0] == 127)
|
2020-02-08 00:19:46 +01:00
|
|
|
return LoopbackAdapter::the();
|
2019-03-12 13:30:36 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-03-10 15:25:33 +01:00
|
|
|
|
2020-02-08 00:19:46 +01:00
|
|
|
RefPtr<NetworkAdapter> NetworkAdapter::lookup_by_name(const StringView& name)
|
2019-09-23 19:06:03 +02:00
|
|
|
{
|
|
|
|
NetworkAdapter* found_adapter = nullptr;
|
|
|
|
for_each([&](auto& adapter) {
|
|
|
|
if (adapter.name() == name)
|
|
|
|
found_adapter = &adapter;
|
|
|
|
});
|
2020-02-08 00:19:46 +01:00
|
|
|
return found_adapter;
|
2019-09-23 19:06:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:25:33 +01:00
|
|
|
NetworkAdapter::NetworkAdapter()
|
|
|
|
{
|
2019-03-12 13:30:36 +01:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().set(this);
|
2019-03-10 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAdapter::~NetworkAdapter()
|
|
|
|
{
|
2019-03-12 13:30:36 +01:00
|
|
|
// FIXME: I wanna lock :(
|
|
|
|
all_adapters().resource().remove(this);
|
2019-03-10 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 23:40:09 +01:00
|
|
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
|
|
|
{
|
2020-07-28 20:19:22 +02:00
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2021-05-15 22:45:22 +02:00
|
|
|
auto buffer = NetworkByteBuffer::create_zeroed(size_in_bytes);
|
2019-09-30 08:57:01 +02:00
|
|
|
auto* eth = (EthernetFrameHeader*)buffer.data();
|
2019-03-10 23:40:09 +01:00
|
|
|
eth->set_source(mac_address());
|
|
|
|
eth->set_destination(destination);
|
2019-03-11 23:21:38 +01:00
|
|
|
eth->set_ether_type(EtherType::ARP);
|
2019-08-08 12:32:35 +10:00
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += size_in_bytes;
|
2019-03-10 23:40:09 +01:00
|
|
|
memcpy(eth->payload(), &packet, sizeof(ARPPacket));
|
2020-07-28 20:19:22 +02:00
|
|
|
send_raw({ (const u8*)eth, size_in_bytes });
|
2019-03-10 23:40:09 +01:00
|
|
|
}
|
2019-03-11 12:43:45 +01:00
|
|
|
|
2021-05-12 14:36:09 +02:00
|
|
|
KResult NetworkAdapter::send_ipv4(const IPv4Address& source_ipv4, const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
|
2019-03-12 04:11:20 +01:00
|
|
|
{
|
2020-09-11 21:11:07 -06:00
|
|
|
size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
|
|
|
|
if (ipv4_packet_size > mtu())
|
2021-05-12 14:36:09 +02:00
|
|
|
return send_ipv4_fragmented(source_ipv4, destination_mac, destination_ipv4, protocol, payload, payload_size, ttl);
|
2019-11-28 07:12:05 +01:00
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
size_t ethernet_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
|
2021-05-15 22:45:22 +02:00
|
|
|
auto buffer = NetworkByteBuffer::create_zeroed(ethernet_frame_size);
|
2019-09-30 08:57:01 +02:00
|
|
|
auto& eth = *(EthernetFrameHeader*)buffer.data();
|
2019-03-12 12:43:30 +01: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 14:36:09 +02:00
|
|
|
ipv4.set_source(source_ipv4);
|
2019-03-12 12:43:30 +01:00
|
|
|
ipv4.set_destination(destination_ipv4);
|
2019-07-03 21:17:35 +02:00
|
|
|
ipv4.set_protocol((u8)protocol);
|
2020-09-11 21:11:07 -06:00
|
|
|
ipv4.set_length(sizeof(IPv4Packet) + payload_size);
|
2019-03-12 12:43:30 +01:00
|
|
|
ipv4.set_ident(1);
|
2019-09-19 21:40:06 +02:00
|
|
|
ipv4.set_ttl(ttl);
|
2019-03-12 12:43:30 +01:00
|
|
|
ipv4.set_checksum(ipv4.compute_checksum());
|
2019-08-08 12:32:35 +10:00
|
|
|
m_packets_out++;
|
2019-11-28 07:12:05 +01:00
|
|
|
m_bytes_out += ethernet_frame_size;
|
2020-09-18 09:49:51 +02:00
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!payload.read(ipv4.payload(), payload_size))
|
2021-01-31 12:13:16 +01:00
|
|
|
return EFAULT;
|
2020-07-28 20:19:22 +02:00
|
|
|
send_raw({ (const u8*)ð, ethernet_frame_size });
|
2021-01-31 12:13:16 +01:00
|
|
|
return KSuccess;
|
2019-03-12 04:11:20 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:36:09 +02:00
|
|
|
KResult NetworkAdapter::send_ipv4_fragmented(const IPv4Address& source_ipv4, const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
|
2020-04-02 05:57:49 +04:30
|
|
|
{
|
|
|
|
// packets must be split on the 64-bit boundary
|
|
|
|
auto packet_boundary_size = (mtu() - sizeof(IPv4Packet) - sizeof(EthernetFrameHeader)) & 0xfffffff8;
|
2020-09-11 21:11:07 -06:00
|
|
|
auto fragment_block_count = (payload_size + packet_boundary_size) / packet_boundary_size;
|
|
|
|
auto last_block_size = payload_size - packet_boundary_size * (fragment_block_count - 1);
|
2020-04-02 05:57:49 +04:30
|
|
|
auto number_of_blocks_in_fragment = packet_boundary_size / 8;
|
|
|
|
|
|
|
|
auto identification = get_good_random<u16>();
|
|
|
|
|
|
|
|
size_t ethernet_frame_size = mtu();
|
|
|
|
for (size_t packet_index = 0; packet_index < fragment_block_count; ++packet_index) {
|
|
|
|
auto is_last_block = packet_index + 1 == fragment_block_count;
|
|
|
|
auto packet_payload_size = is_last_block ? last_block_size : packet_boundary_size;
|
2021-05-15 22:45:22 +02:00
|
|
|
auto buffer = NetworkByteBuffer::create_zeroed(ethernet_frame_size);
|
2020-04-02 05:57:49 +04:30
|
|
|
auto& eth = *(EthernetFrameHeader*)buffer.data();
|
|
|
|
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 14:36:09 +02:00
|
|
|
ipv4.set_source(source_ipv4);
|
2020-04-02 05:57:49 +04:30
|
|
|
ipv4.set_destination(destination_ipv4);
|
|
|
|
ipv4.set_protocol((u8)protocol);
|
|
|
|
ipv4.set_length(sizeof(IPv4Packet) + packet_payload_size);
|
|
|
|
ipv4.set_has_more_fragments(!is_last_block);
|
|
|
|
ipv4.set_ident(identification);
|
|
|
|
ipv4.set_ttl(ttl);
|
|
|
|
ipv4.set_fragment_offset(packet_index * number_of_blocks_in_fragment);
|
|
|
|
ipv4.set_checksum(ipv4.compute_checksum());
|
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += ethernet_frame_size;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!payload.read(ipv4.payload(), packet_index * packet_boundary_size, packet_payload_size))
|
2021-01-31 12:13:16 +01:00
|
|
|
return EFAULT;
|
2020-07-28 20:19:22 +02:00
|
|
|
send_raw({ (const u8*)ð, ethernet_frame_size });
|
2020-04-02 05:57:49 +04:30
|
|
|
}
|
2021-01-31 12:13:16 +01:00
|
|
|
return KSuccess;
|
2020-04-02 05:57:49 +04:30
|
|
|
}
|
|
|
|
|
2020-07-28 20:19:22 +02:00
|
|
|
void NetworkAdapter::did_receive(ReadonlyBytes payload)
|
2019-03-11 12:43:45 +01:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-08-08 12:32:35 +10:00
|
|
|
m_packets_in++;
|
2020-07-28 20:19:22 +02:00
|
|
|
m_bytes_in += payload.size();
|
2019-12-14 11:07:37 +01:00
|
|
|
|
2021-05-06 20:22:57 +02:00
|
|
|
if (m_packet_queue_size == max_packet_buffers) {
|
|
|
|
// FIXME: Keep track of the number of dropped packets
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:21:19 +02:00
|
|
|
auto packet = acquire_packet_buffer(payload.size());
|
2021-05-14 09:52:49 +02:00
|
|
|
if (!packet) {
|
|
|
|
dbgln("Discarding packet because we're out of memory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:21:19 +02:00
|
|
|
memcpy(packet->buffer.data(), payload.data(), payload.size());
|
|
|
|
|
2021-05-14 09:52:49 +02:00
|
|
|
m_packet_queue.append(*packet);
|
2021-05-06 20:22:57 +02:00
|
|
|
m_packet_queue_size++;
|
2019-12-14 11:07:37 +01:00
|
|
|
|
2019-08-28 22:14:38 +10:00
|
|
|
if (on_receive)
|
|
|
|
on_receive();
|
2019-03-11 12:43:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:48:45 +01:00
|
|
|
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp)
|
2019-03-11 12:43:45 +01:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
2019-12-14 11:07:37 +01:00
|
|
|
return 0;
|
2020-09-16 12:25:06 -04:00
|
|
|
auto packet_with_timestamp = m_packet_queue.take_first();
|
2021-05-06 20:22:57 +02:00
|
|
|
m_packet_queue_size--;
|
2021-05-14 09:52:49 +02:00
|
|
|
packet_timestamp = packet_with_timestamp->timestamp;
|
|
|
|
auto& packet_buffer = packet_with_timestamp->buffer;
|
|
|
|
size_t packet_size = packet_buffer.size();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(packet_size <= buffer_size);
|
2021-05-14 09:52:49 +02:00
|
|
|
memcpy(buffer, packet_buffer.data(), packet_size);
|
2021-05-26 04:21:19 +02:00
|
|
|
release_packet_buffer(*packet_with_timestamp);
|
2019-12-14 11:07:37 +01:00
|
|
|
return packet_size;
|
2019-03-11 12:43:45 +01:00
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
|
2021-05-26 04:21:19 +02: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() });
|
|
|
|
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() });
|
|
|
|
packet->buffer.set_size(size);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_unused_packets.append(packet);
|
|
|
|
}
|
|
|
|
|
2019-03-11 23:21:38 +01:00
|
|
|
void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
|
|
|
|
{
|
|
|
|
m_ipv4_address = address;
|
|
|
|
}
|
2019-03-20 17:09:46 +01:00
|
|
|
|
2019-08-28 11:00:39 +10: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 07:06:49 +02: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 07:06:49 +02:00
|
|
|
}
|
2020-02-16 01:27:42 +01: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 01:27:42 +01:00
|
|
|
}
|