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:
@@ -4,6 +4,7 @@ FROM alpine:3.19
|
||||
RUN apk add --no-cache \
|
||||
miniupnpd \
|
||||
iptables \
|
||||
iptables-legacy \
|
||||
iproute2 \
|
||||
bash
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user