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-12-14 13:07:37 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2022-10-05 20:27:36 +03:00
|
|
|
#include <Kernel/InterruptDisabler.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/NetworkAdapter.h>
|
2021-06-04 07:43:16 +03:00
|
|
|
#include <Kernel/Net/NetworkingManagement.h>
|
2020-09-16 19:25:06 +03:00
|
|
|
#include <Kernel/Process.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 {
|
|
|
|
|
2021-09-28 03:49:56 +03:00
|
|
|
NetworkAdapter::NetworkAdapter(NonnullOwnPtr<KString> interface_name)
|
|
|
|
: m_name(move(interface_name))
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:15:15 +03:00
|
|
|
NetworkAdapter::~NetworkAdapter() = default;
|
2019-03-10 17:25:33 +03:00
|
|
|
|
2021-06-04 18:58:55 +03:00
|
|
|
void NetworkAdapter::send_packet(ReadonlyBytes packet)
|
|
|
|
{
|
|
|
|
m_packets_out++;
|
2021-06-04 20:34:17 +03:00
|
|
|
m_bytes_out += packet.size();
|
2021-06-04 18:58:55 +03:00
|
|
|
send_raw(packet);
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void NetworkAdapter::send(MACAddress const& destination, ARPPacket const& packet)
|
2019-03-11 01:40:09 +03:00
|
|
|
{
|
2020-07-28 21:19:22 +03:00
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
|
2021-09-06 01:59:52 +03:00
|
|
|
auto buffer_result = NetworkByteBuffer::create_zeroed(size_in_bytes);
|
2022-01-20 20:47:39 +03:00
|
|
|
if (buffer_result.is_error()) {
|
2021-09-06 01:59:52 +03: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 20:47:39 +03:00
|
|
|
auto* eth = (EthernetFrameHeader*)buffer_result.value().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-03-11 01:40:09 +03:00
|
|
|
memcpy(eth->payload(), &packet, sizeof(ARPPacket));
|
2022-04-01 20:58:27 +03:00
|
|
|
send_packet({ (u8 const*)eth, size_in_bytes });
|
2019-03-11 01:40:09 +03:00
|
|
|
}
|
2019-03-11 14:43:45 +03:00
|
|
|
|
2021-10-27 23:20:24 +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 type_of_service, 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;
|
2021-08-01 15:11:49 +03: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 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-10-27 23:20:24 +03:00
|
|
|
ipv4.set_dscp_and_ecn(type_of_service);
|
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);
|
2023-02-06 00:02:50 +03:00
|
|
|
ipv4.set_length(ipv4_packet_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-08-01 15:11:49 +03:00
|
|
|
memcpy(packet->buffer->data(), payload.data(), payload.size());
|
2021-05-26 05:21:19 +03:00
|
|
|
|
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;
|
2021-08-01 15:11:49 +03:00
|
|
|
size_t packet_size = packet_buffer->size();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(packet_size <= buffer_size);
|
2021-08-01 15:11: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
|
|
|
|
2023-04-11 03:50:15 +03:00
|
|
|
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
2021-05-26 05:21:19 +03:00
|
|
|
{
|
2023-04-11 03:50:15 +03:00
|
|
|
auto packet = m_unused_packets.with([size](auto& unused_packets) -> RefPtr<PacketWithTimestamp> {
|
2022-08-19 11:41:38 +03:00
|
|
|
if (unused_packets.is_empty())
|
|
|
|
return nullptr;
|
2021-05-26 05:21:19 +03:00
|
|
|
|
2022-08-19 11:41:38 +03:00
|
|
|
auto unused_packet = unused_packets.take_first();
|
|
|
|
|
|
|
|
if (unused_packet->buffer->capacity() >= size)
|
|
|
|
return unused_packet;
|
|
|
|
|
2022-08-19 14:06:30 +03:00
|
|
|
unused_packets.append(*unused_packet);
|
2022-08-19 11:41:38 +03:00
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (packet) {
|
2021-05-26 05:21:19 +03:00
|
|
|
packet->timestamp = kgettimeofday();
|
2021-08-01 15:11:49 +03:00
|
|
|
packet->buffer->set_size(size);
|
2021-05-26 05:21:19 +03:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2022-04-11 01:08:07 +03:00
|
|
|
auto buffer_or_error = KBuffer::try_create_with_size("NetworkAdapter: Packet buffer"sv, size, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow);
|
2021-09-07 16:15:08 +03:00
|
|
|
if (buffer_or_error.is_error())
|
2021-08-01 15:11:49 +03:00
|
|
|
return {};
|
2023-04-11 03:50:15 +03:00
|
|
|
packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer_or_error.release_value(), kgettimeofday() });
|
2021-05-26 06:35:05 +03:00
|
|
|
if (!packet)
|
2021-08-01 15:11:49 +03:00
|
|
|
return {};
|
|
|
|
packet->buffer->set_size(size);
|
2021-05-26 05:21:19 +03:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
|
|
|
{
|
2022-08-19 11:41:38 +03:00
|
|
|
m_unused_packets.with([&packet](auto& unused_packets) {
|
|
|
|
unused_packets.append(packet);
|
|
|
|
});
|
2021-05-26 05:21:19 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void NetworkAdapter::set_ipv4_address(IPv4Address const& address)
|
2019-03-12 01:21:38 +03:00
|
|
|
{
|
|
|
|
m_ipv4_address = address;
|
|
|
|
}
|
2019-03-20 19:09:46 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void NetworkAdapter::set_ipv4_netmask(IPv4Address const& netmask)
|
2019-08-28 04:00:39 +03:00
|
|
|
{
|
|
|
|
m_ipv4_netmask = netmask;
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|