Purpose: a switch, a DHCP server and a router that give emulated machines the host's network, with no root and no TAP device.
This is QEMU's user-mode networking (SLIRP) rebuilt from UDE parts: the guests see the same addresses QEMU gives them (a gateway at 10.0.2.2, DNS at 10.0.2.3, leases from 10.0.2.15), but they share one switch, so they can also reach each other, and any UDE tool can join in.
S: box "udeswitch" ":7500" fit D: box "udedhcpd" "10.0.2.4" fit at S + (-2.4, 0.9) R: box "udeslirp" "10.0.2.2, DNS .3" fit at S + (-2.4, -0.9) Q1: box "QEMU guest" "10.0.2.15 (DHCP)" fit at S + (2.6, 0.9) Q2: box "86Box, udenc..." "10.0.2.16 (DHCP)" fit at S + (2.6, -0.9) H: box "host sockets" fit at R + (0, -1.1) line from D.e to S.w line from R.e to S.w line from Q1.w to S.e line from Q2.w to S.e arrow <-> from R.s to H.n text "the host's network" at H.s + (0, -0.25)→ /pikchrshow
The three programs
udeswitch -T 0 &
udedhcpd -a 10.0.2.4/24 -g 10.0.2.2 -N 10.0.2.3 -p 15-99 &
udeslirp -v &
udeswitch -T 0: emulators such as QEMU send no keepalives, so the idle timeout is turned off.udedhcpdtakes an address of its own on the router's subnet (here 10.0.2.4) and hands out the router (-g) and the DNS address (-N) from a pool (-p 15-99) that leaves out both.udeslirpis the gateway 10.0.2.2/24 with the DNS relay at 10.0.2.3, its defaults;-vlogs each connection.
DHCP and the router are separate programs, so a guest can use
udedhcpd, a static address, or another DHCP server running in a VM.
They agree only through the addresses: give udedhcpd an address of its
own, the gateway as -g and the DNS address as -N. Statically
configured guests need an address outside the pool, gateway 10.0.2.2 and
DNS 10.0.2.3, or a reservation (udedhcpd -r 52:54:00:12:34:56=10.0.2.10)
so that the address is fixed but still comes from DHCP.
A QEMU guest
QEMU 7.2 and later:
qemu-system-x86_64 -m 512 -hda guest.img \
-netdev dgram,id=n0,local.type=inet,local.host=127.0.0.1,local.port=7701,remote.type=inet,remote.host=127.0.0.1,remote.port=7500 \
-device virtio-net-pci,netdev=n0
Each guest NIC needs its own local port. The guest gets 10.0.2.15 by DHCP, and the router log shows its connections:
udeslirp: gateway 10.0.2.2/24 on fa:5a:32:08:1e:94, DNS 10.0.2.3 -> 10.0.0.2:53
udeslirp: udp 10.0.2.15:60593 mapped (DNS)
udeslirp: tcp 10.0.2.15:42142 -> 172.30.0.2:18080 connected
udeslirp: tcp 10.0.2.15:42142 -> 172.30.0.2:18080 closed (99 bytes from the guest, 3000205 to it)
This was tested with an Alpine Linux 3.20 guest (kernel 6.6, virtio-net,
busybox): udhcpc got 10.0.2.15 with router 10.0.2.2 and DNS 10.0.2.3;
ping of the gateway and the DNS address worked; nslookup was relayed
to the host's resolver; wget fetched 3 MB from a web server on the host,
both by the host's address and (with --host-loopback) as
10.0.2.2, with matching checksums; a closed port was "Connection
refused"; traceroute showed 10.0.2.2 as the first hop; and a
-R tcp:18022:10.0.2.15:22 forward carried a connection from the host
into the guest's nc -l.
A guest without DHCP
udenc --addr 10.0.2.9/24 --gateway 10.0.2.2 --dns 10.0.2.3 example.org 80
Reaching a guest's server
A forward listens on every address, like QEMU's SLIRP. Use -b 127.0.0.1
or tcp:127.0.0.1:2222:... to keep it on the host only.
udeslirp -R tcp:2222:10.0.2.15:22 &
ssh -p 2222 user@127.0.0.1
Use a reservation so that the guest keeps 10.0.2.15.
The same in a configuration file
The three programs can share one configuration file:
[global]
connection = "udp://127.0.0.1:7500"
[udedhcpd]
subnet = "10.0.2.4/24"
range = "15-99"
router = "10.0.2.2"
dns = ["10.0.2.3"]
[udeslirp]
forward = ["tcp:127.0.0.1:2222:10.0.2.15:22"]
udeswitch -C lab.toml &
udedhcpd -C lab.toml &
udeslirp -C lab.toml -v &
What does not work
- Ping to the outside, unless the host allows unprivileged ICMP sockets: see "Ping" in udeslirp. Windows hosts never do. The guest gets "administratively prohibited", not silence.
- ICMP errors from the outside: a traceroute beyond the first hop, or an ICMP port unreachable for a guest's UDP datagram, do not come back.
- Inbound connections without a forward: like any NAT, the guests
can be reached from outside only through
-R. - IP fragments, and protocols other than TCP, UDP and ICMP echo.
- The host's own 127.0.0.1, unless
--host-loopback: see its security implications in udeslirp.