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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user