Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,23 @@ segment cannot advance it and make PAWS drop the real ones. `send()` is bounded
accepted, and `listen()` takes a backlog so the accept queue cannot be grown without
limit by a peer.

**Loopback.** A datagram to `127.0.0.0/8`, to `::1`, or to one of the stack's own addresses never
reaches routing, ARP or a channel. It is built as for the wire and put on a bounded queue
(1024 datagrams; one that finds it full is dropped and counted in `loopback_drops()`, which TCP
recovers by retransmitting). `poll()` delivers the queue - before the channels, at most half
its frame budget per call, and only what was queued when it started, so a send never
re-enters a receive path and a local ping-pong cannot hold `poll()` - and returns `false`
until the queue is empty. `ms_until_next_timer()` reads 0 while it is not, because there is
no fd to wake a driver that sleeps. There is no pseudo-interface: nothing that iterates
interfaces sees one, and DHCP, ARP and the neighbour caches are untouched. The source of a
local datagram is its destination (as on Linux), the loopback MTU is 65535 (the most a 16-bit
IPv4 total length or IPv6 payload length can say, so loopback never fragments) and a local
connection's MSS comes from it (65495 for v4, 65475 for v6). On a real interface a `127/8` or
`::1` source or destination, or one of the stack's own addresses as a source, is dropped and
counted in `loopback_martian_drops()` (RFC 1122 3.2.1.3; Linux's martian-source rule, which
also stops a spoofed own-address SYN from making the stack talk to itself) - so forwarding
can never route to or from loopback. ustack's `bind()` accepts any `127/8` address and `::1`.

## Deliberately out of scope

These are documented decisions, not gaps that were missed:
Expand Down
Binary file added fuzz/corpus/stack/loopback_app_traffic
Binary file not shown.
Binary file added fuzz/corpus/stack/loopback_destination_syn
Binary file not shown.
Binary file added fuzz/corpus/stack/loopback_destination_udp
Binary file not shown.
Binary file added fuzz/corpus/stack/loopback_source_udp
Binary file not shown.
Binary file added fuzz/corpus/stack/own_source_land_syn
Binary file not shown.
Binary file added fuzz/corpus/stack/own_source_ping
Binary file not shown.
37 changes: 34 additions & 3 deletions fuzz/gen_corpus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ namespace
}

// An Ethernet frame carrying `payload` as IPv4 with protocol `protocol`.
Bytes ip_frame(uint8_t protocol, const Bytes& payload, uint16_t flags_and_offset = 0)
Bytes ip_frame(uint8_t protocol, const Bytes& payload, uint16_t flags_and_offset = 0,
const IPv4Address& source = IP_B, const IPv4Address& destination = IP_A)
{
Bytes ip;
ip.push_back(0x45);
Expand All @@ -83,8 +84,8 @@ namespace
ip.push_back(64);
ip.push_back(protocol);
ip.append_int<uint16_t>(0);
ip |= IP_B.get_address();
ip |= IP_A.get_address();
ip |= source.get_address();
ip |= destination.get_address();
uint16_t sum = internet_checksum(ip);
ip[10] = static_cast<byte_t>(sum >> 8);
ip[11] = static_cast<byte_t>(sum);
Expand Down Expand Up @@ -392,6 +393,36 @@ int main(int argc, char** argv)
save("stack", "fragments", stack_item(frag, 0x00) | stack_item(last, 0x1f));
}
save("stack", "idle_then_close", stack_item(Bytes(), 0x7f) | stack_item(data, 0x7f));
{
// Local addresses on the wire: loopback sources and destinations, and
// the stack's own address as a source (LAND: source == destination, equal
// ports). All are martians the stack must drop without a trace; the
// harness's own loopback sends (see World::application) share the queue.
const IPv4Address loopback("127.0.0.1");
Bytes udp_to_dns = udp_datagram(1234, 53, Bytes(16));
Bytes land = tcp_frame::build(
[&]
{
tcp_frame::Segment s = segment(1000, 0, tcp_frame::SYN);
s.src_ip = IP_A;
s.src_port = 80;
return s;
}());
Bytes loopback_syn = tcp_frame::build(
[&]
{
tcp_frame::Segment s = segment(1000, 0, tcp_frame::SYN);
s.dst_ip = loopback;
return s;
}());
save("stack", "loopback_source_udp", stack_item(ip_frame(17, udp_to_dns, 0, loopback, IP_A), 0x01));
save("stack", "loopback_destination_udp", stack_item(ip_frame(17, udp_to_dns, 0, IP_B, loopback), 0x01));
save("stack", "loopback_destination_syn", stack_item(loopback_syn, 0x01));
save("stack", "own_source_land_syn",
stack_item(land, 0x01) | stack_item(ip_frame(17, udp_to_dns, 0, IP_A, IP_A), 0x01));
save("stack", "own_source_ping", stack_item(ip_frame(1, icmp_echo(8, Bytes(10)), 0, IP_A, IP_A), 0x02));
save("stack", "loopback_app_traffic", stack_item(Bytes(), 0x41) | stack_item(Bytes(), 0x7f));
}
{
// A lying receiver's D-SACKs (RFC 2883), against a connection with a
// timed-out segment: the application writes 200 bytes, 1.25 s pass
Expand Down
17 changes: 17 additions & 0 deletions fuzz/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ namespace
std::unique_ptr<NetworkStack> stack;
uint64_t connection_id = 0;
bool have_connection = false;
bool loopback_connected = false;

World()
{
Expand Down Expand Up @@ -203,8 +204,24 @@ namespace
channel->clear_outbound();
}

// The stack talking to itself: datagrams and one connection over
// 127.0.0.1 and ::1, through the loopback queue the frames above
// share it with. poll() between frames delivers them.
void loopback_traffic()
{
UdpSocket* socket = stack->bind_udp(54);
socket->send_to(IPv4Address("127.0.0.1"), 53, Bytes(8));
socket->send_to(IPv6Address("::1"), 53, Bytes(8));
if (!loopback_connected)
{
loopback_connected = true;
stack->connect(IPv4Address("127.0.0.1"), LOCAL_PORT);
}
}

void application(bool closing)
{
loopback_traffic();
if (!have_connection)
return;
TcpConnection* c = stack->find_connection(connection_id);
Expand Down
78 changes: 75 additions & 3 deletions include/network_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,37 @@ class NetworkStack
// where it shows instead. Fatal channel errors are not counted here: they
// propagate out of poll()/on_time_passed()/send().
uint64_t transmit_drops() const { return _transmit_drops; }

// ---- Loopback ----------------------------------------------------------
//
// A datagram to 127.0.0.0/8, to ::1, or to one of this stack's own
// addresses is local: it never reaches routing, ARP or a channel. It is
// queued at the IP layer and delivered by the next poll(), so no send
// path ever re-enters a receive path.
//
// The queue is bounded; a datagram that finds it full is dropped and
// counted, which TCP treats as loss and retransmits. Delivery is deferred
// (never from inside send()), and ms_until_next_timer() reads 0 while
// anything is queued, so a driver that sleeps on it never sleeps on
// pending loopback traffic.
static constexpr size_t LOOPBACK_QUEUE_LIMIT = 1024;
// The largest value both IP total-length (16 bits, IPv4 header included)
// and the IPv6 payload length (16 bits, header excluded) can carry: a
// loopback segment is never fragmented, and TCP's MSS on a loopback route
// is derived from it (MTU less 40 for v4, less 60 for v6).
// And in bytes: 1024 datagrams of 64 KiB would otherwise be 64 MiB held
// for a loop that can be fed faster than it is drained. Either limit drops.
static constexpr size_t LOOPBACK_QUEUE_BYTE_LIMIT = size_t{4} * 1024 * 1024;
static constexpr uint16_t LOOPBACK_MTU = 65535;
uint64_t loopback_drops() const { return _loopback_drops; }
size_t loopback_queued_bytes() const { return _loopback_queued_bytes; }
// Packets that arrived on a real interface with a loopback source or
// destination and were dropped (RFC 1122 3.2.1.3; RFC 4291 2.5.3).
uint64_t loopback_martian_drops() const { return _loopback_martian_drops; }
// Whether a datagram to this address stays in the host: 127.0.0.0/8, ::1, or
// an address configured on this stack. What the socket layer asks to accept
// a bind() and to size a datagram by the loopback MTU.
bool is_local_address(const IpAddress& address) const;
uint64_t challenge_acks_sent() const { return _challenge_acks_sent; }
uint64_t challenge_acks_suppressed() const { return _challenge_acks_suppressed; }
// SYNs that ended a TIME_WAIT early to open a new connection on the same
Expand Down Expand Up @@ -702,8 +733,11 @@ class NetworkStack
// not, because this stack fragments oversized datagrams itself and there is
// no per-connection state to adjust in response.
// `ecn` goes in the TOS octet's ECN field (RFC 3168); DSCP is 0.
void _send_ip_packet(const IPv4Address& dest_ip, uint8_t protocol, const Bytes& payload, bool dont_fragment = false,
Ecn ecn = Ecn::NOT_ECT);
// `local`: dest_ip is this host (_is_local_ipv4), decided once by the
// caller because the source address, the ARP hold-back and this function
// all need the answer.
void _send_ip_packet(const IPv4Address& dest_ip, bool local, uint8_t protocol, const Bytes& payload,
bool dont_fragment = false, Ecn ecn = Ecn::NOT_ECT);
// local_v6 pins the source of a v6 segment - a connection's own address;
// without it one is chosen for the destination.
void _send_tcp_segment(const IpAddress& destination, const Tcp& header, const Bytes& payload,
Expand Down Expand Up @@ -805,7 +839,7 @@ class NetworkStack
// The address a packet to this destination will carry. Used by both the IP
// header and the transport pseudo-header checksums, so that the two cannot
// be computed from different answers - see its definition.
IPv4Address _source_address_for(const IPv4Address& destination) const;
IPv4Address _source_address_for(const IPv4Address& destination, bool local) const;
// True if this address is the limited broadcast, or the directed broadcast
// of ANY of this stack's links.
//
Expand Down Expand Up @@ -995,6 +1029,44 @@ class NetworkStack
uint64_t _time_wait_reuses = 0;
uint64_t _transmit_drops = 0;

// ---- Loopback internals ----
//
// Whole IP packets, built and checksummed, waiting for the next poll().
// `v6` says which receive path takes it. No Ethernet header: there is no
// link, so nothing in a packet depends on one.
struct LoopbackDatagram
{
bool v6 = false;
Bytes packet;
};
std::deque<LoopbackDatagram> _loopback_queue;
uint64_t _loopback_drops = 0;
size_t _loopback_queued_bytes = 0;
uint64_t _loopback_martian_drops = 0;
// The "ingress interface" of a datagram taken off the loopback queue. Not
// an index into _interfaces on purpose: there is no pseudo-interface (code
// that iterates interfaces or assumes interface 0 must not see one), and a
// value that cannot be mistaken for an index makes any at() on it throw
// rather than quietly use the wrong link.
static constexpr size_t LOOPBACK_INGRESS = static_cast<size_t>(-1);
// 127.0.0.0/8 or an address configured on an interface.
bool _is_local_ipv4(const IPv4Address& address) const;
// The same test on the four raw octets, with no IPv4Address (and so no
// allocation) built - what the per-packet receive check uses.
bool _is_local_octets(const Bytes& octets) const;
bool _is_local_ipv6(const IPv6Address& address) const;
// The v6 receive path for a datagram off the loopback queue.
void _handle_loopback_ipv6(const Bytes& packet);
// Everything _handle_ipv6 does once the link layer is out of the picture:
// address checks and transport demux. Shared with the loopback path so a
// local datagram is handled by the code that handles any other.
void _deliver_ipv6(const Ipv6& ip, const Bytes& packet, bool link_layer_group, bool from_loopback);
// Queues a built packet, or drops and counts it when the queue is full.
void _enqueue_loopback(bool v6, Bytes&& packet);
// Delivers up to `limit` of the datagrams queued now - not those queued
// by delivering them - and returns how many it took.
size_t _drain_loopback(size_t limit);

// Null until start_dhcp(). Owned here rather than by the application
// because it reconfigures the interface, which is this class's state.
std::unique_ptr<DhcpClient> _dhcp_client;
Expand Down
Loading
Loading