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