Kernel: Use KResult a bit more in the IPv4 networking code
This commit is contained in:
parent
b00799b9ce
commit
9984201634
Notes:
sideshowbarker
2024-07-18 22:41:57 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/99842016349
7 changed files with 23 additions and 21 deletions
|
@ -225,9 +225,9 @@ KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer&
|
|||
#endif
|
||||
|
||||
if (type() == SOCK_RAW) {
|
||||
int err = routing_decision.adapter->send_ipv4(routing_decision.next_hop, m_peer_address, (IPv4Protocol)protocol(), data, data_length, m_ttl);
|
||||
if (err < 0)
|
||||
return KResult((ErrnoCode)-err);
|
||||
auto result = routing_decision.adapter->send_ipv4(routing_decision.next_hop, m_peer_address, (IPv4Protocol)protocol(), data, data_length, m_ttl);
|
||||
if (result.is_error())
|
||||
return result;
|
||||
return data_length;
|
||||
}
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
|
|||
send_raw({ (const u8*)eth, size_in_bytes });
|
||||
}
|
||||
|
||||
int NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
|
||||
KResult NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
|
||||
{
|
||||
size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
|
||||
if (ipv4_packet_size > mtu())
|
||||
|
@ -127,12 +127,12 @@ int NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Addre
|
|||
m_bytes_out += ethernet_frame_size;
|
||||
|
||||
if (!payload.read(ipv4.payload(), payload_size))
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
send_raw({ (const u8*)ð, ethernet_frame_size });
|
||||
return 0;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
int NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
|
||||
KResult NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& 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;
|
||||
|
@ -166,10 +166,10 @@ int NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, cons
|
|||
m_packets_out++;
|
||||
m_bytes_out += ethernet_frame_size;
|
||||
if (!payload.read(ipv4.payload(), packet_index * packet_boundary_size, packet_payload_size))
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
send_raw({ (const u8*)ð, ethernet_frame_size });
|
||||
}
|
||||
return 0;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
void NetworkAdapter::did_receive(ReadonlyBytes payload)
|
||||
|
|
|
@ -64,8 +64,8 @@ public:
|
|||
void set_ipv4_gateway(const IPv4Address&);
|
||||
|
||||
void send(const MACAddress&, const ARPPacket&);
|
||||
int send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
|
||||
int send_ipv4_fragmented(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
|
||||
KResult send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
|
||||
KResult send_ipv4_fragmented(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
|
||||
|
||||
size_t dequeue_packet(u8* buffer, size_t buffer_size, timeval& packet_timestamp);
|
||||
|
||||
|
|
|
@ -282,7 +282,7 @@ void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet,
|
|||
response.header.set_checksum(internet_checksum(&response, icmp_packet_size));
|
||||
// FIXME: What is the right TTL value here? Is 64 ok? Should we use the same TTL as the echo request?
|
||||
auto response_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&response);
|
||||
adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, response_buffer, buffer.size(), 64);
|
||||
[[maybe_unused]] auto result = adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, response_buffer, buffer.size(), 64);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -184,7 +184,7 @@ KResultOr<size_t> TCPSocket::protocol_send(const UserOrKernelBuffer& data, size_
|
|||
return data_length;
|
||||
}
|
||||
|
||||
int TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, size_t payload_size)
|
||||
KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, size_t payload_size)
|
||||
{
|
||||
const size_t buffer_size = sizeof(TCPPacket) + payload_size;
|
||||
alignas(TCPPacket) u8 buffer[buffer_size];
|
||||
|
@ -202,7 +202,7 @@ int TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, siz
|
|||
tcp_packet.set_ack_number(m_ack_number);
|
||||
|
||||
if (payload && !payload->read(tcp_packet.payload(), payload_size))
|
||||
return -EFAULT;
|
||||
return EFAULT;
|
||||
|
||||
if (flags & TCPFlags::SYN) {
|
||||
++m_sequence_number;
|
||||
|
@ -216,22 +216,22 @@ int TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, siz
|
|||
LOCKER(m_not_acked_lock);
|
||||
m_not_acked.append({ m_sequence_number, ByteBuffer::copy(buffer, buffer_size) });
|
||||
send_outgoing_packets();
|
||||
return 0;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
|
||||
ASSERT(!routing_decision.is_zero());
|
||||
|
||||
auto packet_buffer = UserOrKernelBuffer::for_kernel_buffer(buffer);
|
||||
int err = routing_decision.adapter->send_ipv4(
|
||||
auto result = routing_decision.adapter->send_ipv4(
|
||||
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
|
||||
packet_buffer, buffer_size, ttl());
|
||||
if (err < 0)
|
||||
return err;
|
||||
if (result.is_error())
|
||||
return result;
|
||||
|
||||
m_packets_out++;
|
||||
m_bytes_out += buffer_size;
|
||||
return 0;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
void TCPSocket::send_outgoing_packets()
|
||||
|
|
|
@ -148,7 +148,7 @@ public:
|
|||
u32 packets_out() const { return m_packets_out; }
|
||||
u32 bytes_out() const { return m_bytes_out; }
|
||||
|
||||
[[nodiscard]] int send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0);
|
||||
KResult send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0);
|
||||
void send_outgoing_packets();
|
||||
void receive_tcp_packet(const TCPPacket&, u16 size);
|
||||
|
||||
|
|
|
@ -107,7 +107,9 @@ KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_
|
|||
if (!data.read(udp_packet.payload(), data_length))
|
||||
return EFAULT;
|
||||
|
||||
routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, UserOrKernelBuffer::for_kernel_buffer(buffer), buffer_size, ttl());
|
||||
auto result = routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, UserOrKernelBuffer::for_kernel_buffer(buffer), buffer_size, ttl());
|
||||
if (result.is_error())
|
||||
return result;
|
||||
return data_length;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue