Purpose: how uderouter forwards between switches, how udeslirp
turns a guest's packets into the host's sockets, and why they behave as
they do.
A UDE switch carries Ethernet frames; it knows nothing of IP. Routing is done by programs on the switches:
- uderouter joins two or more switches, each a subnet of its own, and forwards IPv4 between them unchanged but for the TTL. Every option is in uderouter(8).
- udeslirp is the way out: an IPv4 host like any other (libude-ipv4, libude-tcp), with two hooks that make it a NAT gateway to the host's sockets. Every option is in udeslirp(8).
The Linux host can be a router too, through udetap; Several networks puts all three together.
uderouter: a router made of hosts
uderouter is one process with a libude connection and a libude-ipv4
interface per switch. libude-ipv4 does what each interface needs on its
own: it answers ARP for the interface's address, keeps the ARP cache and
queues packets while a next hop is looked up (ude_ipv4_send_packet),
and reports a next hop that never answers (ude_ipv4_set_arp_fail).
IPv4 frames never reach it: uderouter takes them first, because a
host's input path would drop fragments and answer packets that are not
its own.
I: box "frame in" "on ifN" fit C: diamond "for us?" fit at I + (1.9, 0) L: box "answer ping," "port unreachable" fit at C + (0, -1.1) T: box "TTL - 1," "longest prefix" fit at C + (2.1, 0) O: box "ARP, send" "on ifM" fit at T + (2.1, 0) E: box "ICMP error" "to the source" fit at T + (0, -1.1) arrow from I.e to C.w arrow from C.s to L.n "yes" ljust arrow from C.e to T.w "no" above arrow from T.e to O.w arrow from T.s to E.n "TTL out, no route," rjust "too big with DF" rjust→ /pikchrshow
- The table is the connected subnet of each interface plus the static
routes; the longest prefix wins, so
default:is only a last resort. - The TTL is decremented and the header checksum patched rather than recomputed (RFC 1624).
- A packet over 1500 bytes is cut into fragments, each with a copy of the options that must be copied (RFC 791); with DF set, the sender gets fragmentation needed with the MTU instead (RFC 1191).
- ICMP errors follow RFC 1812: never about an ICMP error, a later fragment or a broadcast; at most 576 bytes; at most 100 a second.
- The MAC address of an interface is derived from its address
(
02:55:and the four bytes), so it survives a restart.
Each switch stays its own broadcast domain: ARP, DHCP and other broadcasts stop at the router, which is why each switch needs its own DHCP server.
udeslirp: routes back through a router
udeslirp serves the subnet it is on. --route NET/LEN:GATEWAY adds
subnets behind a router on that subnet: their packets are accepted like
its own guests' (they arrive at its MAC address anyway), and everything
it sends to them, replies, ICMP errors and port forwards alike, goes to
the gateway's MAC address instead of to the destination's. A packet from
a guest on its own subnet to a routed subnet is passed on to the
gateway, its TTL decremented, as a router on the same wire would.
udeslirp: the gateway is a host with a route hook
Guests send everything for other subnets to their default gateway's MAC
address. libude-ipv4's route hook (ude_ipv4_set_route_hook) sees each
unicast packet that arrives for udeslirp's MAC, whatever its
destination address, and either takes it or has it delivered locally "as
if for us", keeping its real destination. Replies and ICMP errors are
then sent from the address the guest used, so a guest that talks to
192.0.2.80 hears from 192.0.2.80. A proxy ARP hook
(ude_ipv4_set_arp_proxy) lets the same host also answer ARP for the
DNS address, 10.0.2.3.
Address translation without a translation table in the kernel
A kernel NAT rewrites addresses in packets. udeslirp cannot touch the
host's packets, so it translates at the socket level, which is what SLIRP
does too:
| Guest sends | udeslirp does |
The host's network sees |
|---|---|---|
| TCP SYN to A:p | holds the SYN, connect()s to A:p |
a connection from the host |
| TCP data, FIN, RST | copies data, half-closes, resets | the same, on that socket |
| UDP from G:g to A:p | one socket per G:g, sendto(A:p) |
datagrams from the host's port |
| ICMP echo to A | an ICMP "ping socket", where allowed | a ping from the host |
| anything else | ICMP "administratively prohibited" | nothing |
The table is the list of these sockets: TCP flows, UDP mappings and
ping sockets, each with an idle timer. A full table refuses new entries
(--max-connections, --max-udp), so a guest cannot exhaust the host's
descriptors.
TCP: answering only when the host has
libude-tcp's listener for any address and port
(UDE_TCP_LISTEN_ANY) takes every SYN that reaches the stack. With
UDE_TCP_LISTEN_DEFER it hands the connection over before answering:
udeslirp starts a non-blocking connect() and only then decides.
G: box "guest" fit R: box "udeslirp" fit at G + (2.4, 0) H: box "server" fit at R + (2.4, 0) arrow from G.e to R.w "SYN" above arrow from R.e to H.w "connect()" above arrow from H.w + (0, -0.25) to R.e + (0, -0.25) "accepted" below arrow from R.w + (0, -0.25) to G.e + (0, -0.25) "SYN-ACK" below text "refused: RST; unreachable: ICMP; no answer: silence" at R.s + (0, -0.6)→ /pikchrshow
- Accepted:
ude_tcp_confirm()sends the SYN-ACK, and data flows. - Refused or reset:
ude_tcp_abort()sends a reset, as a closed port would. - Network or host unreachable: an ICMP destination unreachable from the gateway, and the SYN is dropped.
- No answer: nothing, so the guest's own SYN timer runs out, as it would.
While undecided, the guest's retransmitted SYNs are ignored. Once open,
each direction is copied as far as the other side accepts it (the host
socket's buffer, the guest's 64 KB window), a FIN becomes a
shutdown() and a shutdown() a FIN, and a reset on either side resets
the other.
UDP: mappings and filtering
A guest address and port get one host socket, used for every destination
the guest sends to (endpoint-independent mapping in the terms of RFC
4787), so a protocol that tells its peer its address sees one consistent
address. Replies are let through from the addresses the guest sent to
(address-dependent filtering), up to 8 of them. A mapping idle for
--udp-timeout seconds is closed.
ICMP
An unprivileged program cannot send raw ICMP. Linux (for the groups in
net.ipv4.ping_group_range) and macOS offer ping sockets, which can send
echo requests and receive the replies; udeslirp uses one per pinging
guest identifier, and restores the guest's identifier, which Linux
rewrites, in the replies. Where they do not exist the guest is told so
with "communication administratively prohibited" rather than left
waiting. ICMP errors the host receives are not passed on.
udeslirp itself sends what a router should: time exceeded when a TTL
runs out at the gateway, unreachable for denied destinations and failed
connections, and port unreachable for UDP to the gateway or DNS address
where nothing is behind it.
Forwards in the other direction
A port forward is a host socket that udeslirp listens on. A host
connection to it becomes an ordinary libude-tcp connection from the
gateway to the guest, and a UDP peer gets a gateway port of its own, so
the guest's replies can be told apart.