2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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-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-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/EthernetFrameHeader.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-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 {
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
LOCKER(all_adapters().lock());
|
|
|
|
for (auto* adapter : all_adapters().resource()) {
|
|
|
|
if (adapter->ipv4_address() == address)
|
2020-02-08 02:19:46 +03:00
|
|
|
return adapter;
|
2019-03-12 15:30:36 +03:00
|
|
|
}
|
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)
|
|
|
|
{
|
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);
|
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));
|
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-09-19 22:40:06 +03:00
|
|
|
void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const u8* payload, size_t payload_size, u8 ttl)
|
2019-03-12 06:11:20 +03:00
|
|
|
{
|
2019-11-28 09:12:05 +03:00
|
|
|
size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
|
|
|
|
if (ipv4_packet_size > mtu()) {
|
2020-04-02 04:27:49 +03:00
|
|
|
send_ipv4_fragmented(destination_mac, destination_ipv4, protocol, payload, payload_size, ttl);
|
|
|
|
return;
|
2019-11-28 09:12:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ethernet_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
|
|
|
|
auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
|
2019-09-30 09:57:01 +03:00
|
|
|
auto& eth = *(EthernetFrameHeader*)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);
|
|
|
|
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);
|
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());
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_out++;
|
2019-11-28 09:12:05 +03:00
|
|
|
m_bytes_out += ethernet_frame_size;
|
2019-08-05 11:37:09 +03:00
|
|
|
memcpy(ipv4.payload(), payload, payload_size);
|
2019-11-28 09:12:05 +03:00
|
|
|
send_raw((const u8*)ð, ethernet_frame_size);
|
2019-03-12 06:11:20 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 04:27:49 +03:00
|
|
|
void NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const u8* payload, size_t payload_size, u8 ttl)
|
|
|
|
{
|
|
|
|
// packets must be split on the 64-bit boundary
|
|
|
|
auto packet_boundary_size = (mtu() - sizeof(IPv4Packet) - sizeof(EthernetFrameHeader)) & 0xfffffff8;
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
|
|
|
|
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);
|
|
|
|
ipv4.set_source(ipv4_address());
|
|
|
|
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;
|
|
|
|
memcpy(ipv4.payload(), payload + packet_index * packet_boundary_size, packet_payload_size);
|
|
|
|
send_raw((const u8*)ð, ethernet_frame_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:29:04 +03:00
|
|
|
void NetworkAdapter::did_receive(const u8* data, size_t 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-12-14 13:07:37 +03:00
|
|
|
|
|
|
|
Optional<KBuffer> buffer;
|
|
|
|
|
|
|
|
if (m_unused_packet_buffers.is_empty()) {
|
|
|
|
buffer = KBuffer::copy(data, length);
|
|
|
|
} else {
|
|
|
|
buffer = m_unused_packet_buffers.take_first();
|
|
|
|
--m_unused_packet_buffers_count;
|
2020-01-29 20:29:04 +03:00
|
|
|
if (length <= buffer.value().size()) {
|
2019-12-14 13:07:37 +03:00
|
|
|
memcpy(buffer.value().data(), data, length);
|
|
|
|
buffer.value().set_size(length);
|
|
|
|
} else {
|
|
|
|
buffer = KBuffer::copy(data, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_packet_queue.append(buffer.value());
|
|
|
|
|
2019-08-28 15:14:38 +03:00
|
|
|
if (on_receive)
|
|
|
|
on_receive();
|
2019-03-11 14:43:45 +03:00
|
|
|
}
|
|
|
|
|
2019-12-14 13:07:37 +03:00
|
|
|
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size)
|
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;
|
|
|
|
auto packet = m_packet_queue.take_first();
|
|
|
|
size_t packet_size = packet.size();
|
|
|
|
ASSERT(packet_size <= buffer_size);
|
|
|
|
memcpy(buffer, packet.data(), packet_size);
|
|
|
|
if (m_unused_packet_buffers_count < 100) {
|
|
|
|
m_unused_packet_buffers.append(packet);
|
|
|
|
++m_unused_packet_buffers_count;
|
|
|
|
}
|
|
|
|
return packet_size;
|
2019-03-11 14:43:45 +03:00
|
|
|
}
|
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
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|