2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-21 15:34:09 +00:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2019-12-14 10:07:37 +00:00
|
|
|
#include <Kernel/Heap/kmalloc.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/NetworkAdapter.h>
|
2021-06-04 04:43:16 +00:00
|
|
|
#include <Kernel/Net/NetworkingManagement.h>
|
2020-09-16 16:25:06 +00:00
|
|
|
#include <Kernel/Process.h>
|
2020-05-16 10:00:04 +00:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-03-12 12:30:36 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-28 00:49:56 +00:00
|
|
|
NetworkAdapter::NetworkAdapter(NonnullOwnPtr<KString> interface_name)
|
|
|
|
: m_name(move(interface_name))
|
2019-03-10 14:25:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:15:15 +00:00
|
|
|
NetworkAdapter::~NetworkAdapter() = default;
|
2019-03-10 14:25:33 +00:00
|
|
|
|
2021-06-04 15:58:55 +00:00
|
|
|
void NetworkAdapter::send_packet(ReadonlyBytes packet)
|
|
|
|
{
|
|
|
|
m_packets_out++;
|
2021-06-04 17:34:17 +00:00
|
|
|
m_bytes_out += packet.size();
|
2021-06-04 15:58:55 +00:00
|
|
|
send_raw(packet);
|
|
|
|
}
|
|
|
|
|
2019-03-10 22:40:09 +00:00
|
|
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
|
|
|
{
|
2020-07-28 18:19:22 +00:00
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2021-09-05 22:59:52 +00:00
|
|
|
auto buffer_result = NetworkByteBuffer::create_zeroed(size_in_bytes);
|
2022-01-20 17:47:39 +00:00
|
|
|
if (buffer_result.is_error()) {
|
2021-09-05 22:59:52 +00:00
|
|
|
dbgln("Dropping ARP packet targeted at {} as there is not enough memory to buffer it", packet.target_hardware_address().to_string());
|
|
|
|
return;
|
|
|
|
}
|
2022-01-20 17:47:39 +00:00
|
|
|
auto* eth = (EthernetFrameHeader*)buffer_result.value().data();
|
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));
|
2021-06-04 15:58:55 +00:00
|
|
|
send_packet({ (const u8*)eth, size_in_bytes });
|
2019-03-10 22:40:09 +00:00
|
|
|
}
|
2019-03-11 11:43:45 +00:00
|
|
|
|
2021-10-27 20:20:24 +00: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 type_of_service, u8 ttl)
|
2019-03-12 03:11:20 +00:00
|
|
|
{
|
2020-09-12 03:11:07 +00:00
|
|
|
size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
|
2021-05-26 03:35:05 +00:00
|
|
|
VERIFY(ipv4_packet_size <= mtu());
|
2019-11-28 06:12:05 +00:00
|
|
|
|
2021-05-26 03:35:05 +00:00
|
|
|
size_t ethernet_frame_size = ipv4_payload_offset() + payload_size;
|
2021-08-01 12:11:49 +00:00
|
|
|
VERIFY(packet.buffer->size() == ethernet_frame_size);
|
|
|
|
memset(packet.buffer->data(), 0, ipv4_payload_offset());
|
|
|
|
auto& eth = *(EthernetFrameHeader*)packet.buffer->data();
|
2019-03-12 11:43:30 +00: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-10-27 20:20:24 +00:00
|
|
|
ipv4.set_dscp_and_ecn(type_of_service);
|
2021-05-12 12:36:09 +00:00
|
|
|
ipv4.set_source(source_ipv4);
|
2019-03-12 11:43:30 +00:00
|
|
|
ipv4.set_destination(destination_ipv4);
|
2019-07-03 19:17:35 +00:00
|
|
|
ipv4.set_protocol((u8)protocol);
|
2020-09-12 03:11:07 +00:00
|
|
|
ipv4.set_length(sizeof(IPv4Packet) + payload_size);
|
2019-03-12 11:43:30 +00:00
|
|
|
ipv4.set_ident(1);
|
2019-09-19 19:40:06 +00:00
|
|
|
ipv4.set_ttl(ttl);
|
2019-03-12 11:43:30 +00:00
|
|
|
ipv4.set_checksum(ipv4.compute_checksum());
|
2020-04-02 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 18:19:22 +00:00
|
|
|
void NetworkAdapter::did_receive(ReadonlyBytes payload)
|
2019-03-11 11:43:45 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-08-08 02:32:35 +00:00
|
|
|
m_packets_in++;
|
2020-07-28 18:19:22 +00:00
|
|
|
m_bytes_in += payload.size();
|
2019-12-14 10:07:37 +00:00
|
|
|
|
2021-05-06 18:22:57 +00:00
|
|
|
if (m_packet_queue_size == max_packet_buffers) {
|
|
|
|
// FIXME: Keep track of the number of dropped packets
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 02:21:19 +00:00
|
|
|
auto packet = acquire_packet_buffer(payload.size());
|
2021-05-14 07:52:49 +00:00
|
|
|
if (!packet) {
|
|
|
|
dbgln("Discarding packet because we're out of memory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-01 12:11:49 +00:00
|
|
|
memcpy(packet->buffer->data(), payload.data(), payload.size());
|
2021-05-26 02:21:19 +00:00
|
|
|
|
2021-05-14 07:52:49 +00:00
|
|
|
m_packet_queue.append(*packet);
|
2021-05-06 18:22:57 +00:00
|
|
|
m_packet_queue_size++;
|
2019-12-14 10:07:37 +00:00
|
|
|
|
2019-08-28 12:14:38 +00:00
|
|
|
if (on_receive)
|
|
|
|
on_receive();
|
2019-03-11 11:43:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:48:45 +00:00
|
|
|
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp)
|
2019-03-11 11:43:45 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
2019-12-14 10:07:37 +00:00
|
|
|
return 0;
|
2020-09-16 16:25:06 +00:00
|
|
|
auto packet_with_timestamp = m_packet_queue.take_first();
|
2021-05-06 18:22:57 +00:00
|
|
|
m_packet_queue_size--;
|
2021-05-14 07:52:49 +00:00
|
|
|
packet_timestamp = packet_with_timestamp->timestamp;
|
|
|
|
auto& packet_buffer = packet_with_timestamp->buffer;
|
2021-08-01 12:11:49 +00:00
|
|
|
size_t packet_size = packet_buffer->size();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(packet_size <= buffer_size);
|
2021-08-01 12:11:49 +00:00
|
|
|
memcpy(buffer, packet_buffer->data(), packet_size);
|
2021-05-26 02:21:19 +00:00
|
|
|
release_packet_buffer(*packet_with_timestamp);
|
2019-12-14 10:07:37 +00:00
|
|
|
return packet_size;
|
2019-03-11 11:43:45 +00:00
|
|
|
}
|
2019-03-11 22:21:38 +00:00
|
|
|
|
2021-05-26 02:21:19 +00:00
|
|
|
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_unused_packets.is_empty()) {
|
2021-09-07 13:15:08 +00:00
|
|
|
auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
|
|
|
if (buffer_or_error.is_error())
|
2021-08-01 12:11:49 +00:00
|
|
|
return {};
|
2021-09-07 13:15:08 +00:00
|
|
|
auto buffer = buffer_or_error.release_value();
|
|
|
|
auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
|
2021-05-26 03:35:05 +00:00
|
|
|
if (!packet)
|
2021-08-01 12:11:49 +00:00
|
|
|
return {};
|
|
|
|
packet->buffer->set_size(size);
|
2021-05-26 02:21:19 +00:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto packet = m_unused_packets.take_first();
|
2021-08-01 12:11:49 +00:00
|
|
|
if (packet->buffer->capacity() >= size) {
|
2021-05-26 02:21:19 +00:00
|
|
|
packet->timestamp = kgettimeofday();
|
2021-08-01 12:11:49 +00:00
|
|
|
packet->buffer->set_size(size);
|
2021-05-26 02:21:19 +00:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:15:08 +00:00
|
|
|
auto buffer_or_error = KBuffer::try_create_with_size(size, Memory::Region::Access::ReadWrite, "Packet Buffer", AllocationStrategy::AllocateNow);
|
|
|
|
if (buffer_or_error.is_error())
|
2021-08-01 12:11:49 +00:00
|
|
|
return {};
|
2021-09-07 13:15:08 +00:00
|
|
|
packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer_or_error.release_value(), kgettimeofday() });
|
2021-05-26 03:35:05 +00:00
|
|
|
if (!packet)
|
2021-08-01 12:11:49 +00:00
|
|
|
return {};
|
|
|
|
packet->buffer->set_size(size);
|
2021-05-26 02:21:19 +00:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_unused_packets.append(packet);
|
|
|
|
}
|
|
|
|
|
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-08-28 01:00:39 +00: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;
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|