fix MWP hole punch for production use. bind to 0.0.0.0 instead of

published IP, resend packets every 500ms instead of single-shot, fix
Docker iptables-legacy chains so miniupnpd port mapping fully works
without warnings
This commit is contained in:
2026-02-10 11:26:57 -03:00
parent 33730fed8a
commit 2fe03cb855
12 changed files with 356 additions and 56 deletions

View File

@@ -4,6 +4,7 @@ FROM alpine:3.19
RUN apk add --no-cache \
miniupnpd \
iptables \
iptables-legacy \
iproute2 \
bash

View File

@@ -34,7 +34,7 @@ This test uses `miniupnpd` as the NAT-PMP daemon running on a simulated router.
The router:
- Runs `miniupnpd` (NAT-PMP + UPnP IGD daemon)
- Does NAT (iptables MASQUERADE) for internal clients
- Reports a fake "external" IP (93.184.216.34) for testing
- Uses a public-looking "external" IP (93.184.216.34) added as alias on the external interface (miniupnpd rejects private IPs)
## Quick Start
@@ -66,19 +66,16 @@ info: NAT-PMP: External address is 93.184.216.34
Using NAT-PMP: 93.184.216.34:18201
```
### Port Mapping: PARTIAL
Port mapping requests are sent correctly, but miniupnpd returns "Network Failure" because:
- The fake external IP doesn't match the actual interface
- iptables rules can't be created for a non-local address
### Port Mapping: PASS
miniupnpd creates DNAT and masquerade rules via libiptc (iptables-legacy).
This is expected in the Docker test environment. On a real router with NAT-PMP support, port mapping would succeed.
```
info: NAT-PMP: Mapped UDP port 18101 -> 93.184.216.34:18101 (lifetime: 3600s)
```
### Connection: LIMITED
Full MWP connection can't complete because:
- The fake external IP can't be bound locally
- No actual internet routing exists
The test validates the protocol implementation, not end-to-end connectivity.
### Connection: PASS
Full MWP hole punch completes. Both peers establish a verified connection through
the NAT router using DNAT port forwarding and hairpin masquerade.
## Files
@@ -104,7 +101,9 @@ Common issues:
- Interface detection problems
### "NAT-PMP: Gateway returned error: Network Failure"
This is expected when port mapping fails. External address discovery should still work.
miniupnpd needs the `MINIUPNPD-POSTROUTING` chain in the nat table for masquerade
rules. If missing, the DNAT rule is created but miniupnpd reports failure. Ensure
`router-entrypoint.sh` creates all three chains: `MINIUPNPD` (nat), `MINIUPNPD-POSTROUTING` (nat), `MINIUPNPD` (filter).
### "Cannot determine gateway"
The NAT-PMP client probes common gateway IPs. Ensure the router is at 10.100.0.1.

View File

@@ -32,14 +32,28 @@ echo "External interface: $EXTERNAL_IF ($EXTERNAL_IP)"
# Set up NAT (masquerade internal traffic going out external interface)
echo "Setting up NAT..."
iptables -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
iptables -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
iptables -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
# miniupnpd uses libiptc (legacy iptables API), so all rules must use iptables-legacy
iptables-legacy -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
iptables-legacy -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
iptables-legacy -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
# Create MINIUPNPD chains that miniupnpd needs for port mapping
# PREROUTING: DNAT rules to redirect external ports to internal clients
iptables-legacy -t nat -N MINIUPNPD
iptables-legacy -t nat -A PREROUTING -j MINIUPNPD
# POSTROUTING: masquerade rules for hairpin NAT (internal-to-internal via external IP)
iptables-legacy -t nat -N MINIUPNPD-POSTROUTING
iptables-legacy -t nat -A POSTROUTING -j MINIUPNPD-POSTROUTING
# FORWARD: filter rules for forwarded traffic
iptables-legacy -N MINIUPNPD
iptables-legacy -A FORWARD -j MINIUPNPD
# Create miniupnpd config with correct interfaces
# Note: miniupnpd config format is very specific
# ext_ip uses a public-looking IP for testing (doesn't need to route)
FAKE_PUBLIC_IP="93.184.216.34" # example.com IP - just needs to look public
# miniupnpd rejects private IPs for ext_ip, so we use a public-looking IP
# and add it as an alias on the external interface so it's actually routable
FAKE_PUBLIC_IP="93.184.216.34"
ip addr add $FAKE_PUBLIC_IP/32 dev $EXTERNAL_IF
echo "Added $FAKE_PUBLIC_IP as alias on $EXTERNAL_IF"
cat > /tmp/miniupnpd.conf << EOF
ext_ifname=$EXTERNAL_IF
ext_ip=$FAKE_PUBLIC_IP

View File

@@ -44,7 +44,7 @@ echo "miniupnpd is running"
# Show router state
echo ""
echo "Router NAT table:"
docker compose exec router iptables -t nat -L -n | head -20
docker compose exec router iptables-legacy -t nat -L -n | head -20
echo ""
echo "[4/4] Running peer tests..."
@@ -99,7 +99,7 @@ docker logs test-peer-b 2>&1 | tail -30
# Check for success indicators
ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "NAT-PMP:" || true)
BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "NAT-PMP:" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "Connected\|SUCCESS" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "CONNECTION ESTABLISHED" || true)
echo ""
echo "=========================================="
@@ -122,4 +122,13 @@ else
fi
echo ""
echo "Test complete."
# Final pass/fail
ALICE_CONNECTED=$(docker logs test-peer-a 2>&1 | grep -c "CONNECTION ESTABLISHED" || true)
if [ $ALICE_CONNECTED -gt 0 ] && [ $CONNECTION -gt 0 ]; then
echo "PASS: Both peers established connection through NAT"
elif [ $ALICE_CONNECTED -gt 0 ] || [ $CONNECTION -gt 0 ]; then
echo "PASS: At least one peer established connection through NAT"
else
echo "FAIL: No connection established"
exit 1
fi

View File

@@ -4,6 +4,7 @@ FROM alpine:3.19
RUN apk add --no-cache \
miniupnpd \
iptables \
iptables-legacy \
iproute2 \
bash

View File

@@ -36,7 +36,7 @@ This test uses `miniupnpd` as the UPnP daemon with **NAT-PMP disabled**, so the
The router:
- Runs `miniupnpd` with UPnP IGD enabled, NAT-PMP disabled
- Does NAT (iptables MASQUERADE) for internal clients
- Reports a fake "external" IP (93.184.216.34) for testing
- Uses a public-looking "external" IP (93.184.216.34) added as alias on the external interface (miniupnpd rejects private IPs)
## Quick Start
@@ -70,12 +70,16 @@ UPnP: External address is 93.184.216.34
Using UPnP IGD: 93.184.216.34:18101
```
### Port Mapping: PARTIAL
Port mapping requests are sent correctly, but miniupnpd may return errors because:
- The fake external IP doesn't match the actual interface
- iptables rules can't be created for a non-local address
### Port Mapping: PASS
miniupnpd creates DNAT and masquerade rules via libiptc (iptables-legacy).
This is expected in the Docker test environment.
```
info: UPnP: Mapped UDP port 18101 -> 93.184.216.34:18101 (lifetime: 3600s)
```
### Connection: PASS
Full MWP hole punch completes. Both peers establish a verified connection through
the NAT router using DNAT port forwarding and hairpin masquerade.
### NAT-PMP: SKIPPED
NAT-PMP is disabled on the router (`enable_natpmp=no`), so the mwp-demo's NAT-PMP attempt fails silently and falls through to UPnP.

View File

@@ -32,13 +32,29 @@ echo "External interface: $EXTERNAL_IF ($EXTERNAL_IP)"
# Set up NAT (masquerade internal traffic going out external interface)
echo "Setting up NAT..."
iptables -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
iptables -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
iptables -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
# miniupnpd uses libiptc (legacy iptables API), so all rules must use iptables-legacy
iptables-legacy -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
iptables-legacy -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
iptables-legacy -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
# Create MINIUPNPD chains that miniupnpd needs for port mapping
# PREROUTING: DNAT rules to redirect external ports to internal clients
iptables-legacy -t nat -N MINIUPNPD
iptables-legacy -t nat -A PREROUTING -j MINIUPNPD
# POSTROUTING: masquerade rules for hairpin NAT (internal-to-internal via external IP)
iptables-legacy -t nat -N MINIUPNPD-POSTROUTING
iptables-legacy -t nat -A POSTROUTING -j MINIUPNPD-POSTROUTING
# FORWARD: filter rules for forwarded traffic
iptables-legacy -N MINIUPNPD
iptables-legacy -A FORWARD -j MINIUPNPD
# Create miniupnpd config with UPnP ENABLED and NAT-PMP DISABLED
# This forces peers to use UPnP IGD (SSDP + SOAP) instead of NAT-PMP
FAKE_PUBLIC_IP="93.184.216.34" # example.com IP - just needs to look public
# miniupnpd rejects private IPs for ext_ip, so we use a public-looking IP
# and add it as an alias on the external interface so it's actually routable
FAKE_PUBLIC_IP="93.184.216.34"
ip addr add $FAKE_PUBLIC_IP/32 dev $EXTERNAL_IF
echo "Added $FAKE_PUBLIC_IP as alias on $EXTERNAL_IF"
cat > /tmp/miniupnpd.conf << EOF
ext_ifname=$EXTERNAL_IF
ext_ip=$FAKE_PUBLIC_IP

View File

@@ -47,7 +47,7 @@ echo "miniupnpd is running (UPnP mode)"
# Show router state
echo ""
echo "Router NAT table:"
docker compose exec router iptables -t nat -L -n | head -20
docker compose exec router iptables-legacy -t nat -L -n | head -20
echo ""
echo "[4/4] Running peer tests..."
@@ -104,7 +104,7 @@ ALICE_UPNP=$(docker logs test-peer-a 2>&1 | grep -c "UPnP" || true)
BOB_UPNP=$(docker logs test-peer-b 2>&1 | grep -c "UPnP" || true)
ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "Using NAT-PMP:" || true)
BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "Using NAT-PMP:" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "Connected\|SUCCESS" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "CONNECTION ESTABLISHED" || true)
echo ""
echo "=========================================="
@@ -140,4 +140,13 @@ if [ $ALICE_NATPMP -gt 0 ] || [ $BOB_NATPMP -gt 0 ]; then
fi
echo ""
echo "Test complete."
# Final pass/fail
ALICE_CONNECTED=$(docker logs test-peer-a 2>&1 | grep -c "CONNECTION ESTABLISHED" || true)
if [ $ALICE_CONNECTED -gt 0 ] && [ $CONNECTION -gt 0 ]; then
echo "PASS: Both peers established connection through NAT"
elif [ $ALICE_CONNECTED -gt 0 ] || [ $CONNECTION -gt 0 ]; then
echo "PASS: At least one peer established connection through NAT"
else
echo "FAIL: No connection established"
exit 1
fi