#!/bin/bash # mats-net: Privileged network initialization helper for mats-proxy Netstack. # # Sets up the TUN device, configures iptables/nftables DNAT forwarding, # drops privileges back to the invoking user, and cleans up on exit. # # Usage: # sudo bin/mats-net bin/mats-proxy-netstack [proxy-arguments...] set -o errexit set -o nounset set -o pipefail if [[ $EUID -ne 0 ]]; then echo "mats-net: must be run with root privileges (e.g. sudo bin/mats-net bin/mats-proxy-netstack)" >&2 exit 1 fi if [[ $# -eq 0 ]]; then echo "Usage: sudo $0 [args...]" >&2 echo "Example: sudo $0 bin/mats-proxy-netstack" >&2 exit 1 fi # Detect invoking non-root user TARGET_USER="${SUDO_USER:-$(id -un)}" TARGET_UID="${SUDO_UID:-$(id -u)}" TARGET_GID="${SUDO_GID:-$(id -g)}" TUN_DEV="${MATS_NET_TUN:-mats-tun0}" TUN_HOST_IP="${MATS_NET_HOST_IP:-10.254.0.1}" TUN_PEER_IP="${MATS_NET_PEER_IP:-10.254.0.2}" PORT="${MATS_NET_PORT:-38088}" cleanup() { local exit_code=$? echo "" echo "[mats-net] Cleaning up network rules and TUN interface ${TUN_DEV}..." iptables -t mangle -D FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true iptables -t nat -D PREROUTING -p tcp --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" 2>/dev/null || true iptables -t nat -D OUTPUT -p tcp -m addrtype --dst-type LOCAL --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" 2>/dev/null || true iptables -t nat -D POSTROUTING -o "$TUN_DEV" -j MASQUERADE 2>/dev/null || true iptables -D FORWARD -p tcp -d "$TUN_PEER_IP" --dport "$PORT" -j ACCEPT 2>/dev/null || true iptables -D FORWARD -i "$TUN_DEV" -j ACCEPT 2>/dev/null || true iptables -D FORWARD -o "$TUN_DEV" -j ACCEPT 2>/dev/null || true ip link set "$TUN_DEV" down 2>/dev/null || true ip tuntap del dev "$TUN_DEV" mode tun 2>/dev/null || true echo "[mats-net] Teardown complete." exit "$exit_code" } trap cleanup EXIT INT TERM # 1. Create TUN device owned by the target unprivileged user echo "[mats-net] Creating TUN device ${TUN_DEV} owned by user ${TARGET_USER} (${TARGET_UID}:${TARGET_GID})..." ip tuntap add dev "$TUN_DEV" mode tun user "$TARGET_USER" ip addr add "${TUN_HOST_IP}/30" dev "$TUN_DEV" ip link set "$TUN_DEV" up # 2. Configure kernel forwarding and DNAT rules to userspace Netstack echo "[mats-net] Forwarding TCP port ${PORT} (external and local) to Netstack at ${TUN_PEER_IP}:${PORT}..." sysctl -q -w net.ipv4.ip_forward=1 sysctl -q -w net.ipv4.conf.all.route_localnet=1 sysctl -q -w net.ipv4.conf.default.route_localnet=1 # External traffic DNAT iptables -t nat -A PREROUTING -p tcp --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" # Local host traffic DNAT (localhost and host IP) iptables -t nat -A OUTPUT -p tcp -m addrtype --dst-type LOCAL --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" # Outgoing masquerade on the TUN device for clean reply routing iptables -t nat -A POSTROUTING -o "$TUN_DEV" -j MASQUERADE # Forwarding rules inserted at position 1 to precede any firewall (e.g. UFW/Docker) reject rules iptables -I FORWARD 1 -i "$TUN_DEV" -j ACCEPT iptables -I FORWARD 1 -o "$TUN_DEV" -j ACCEPT # Clamp TCP MSS to path MTU to prevent packet truncation on tunnel routing iptables -t mangle -I FORWARD 1 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true # 3. Export configuration to target process export MATS_PROXY_TUN_DEV="$TUN_DEV" export MATS_PROXY_LISTEN="${TUN_PEER_IP}:${PORT}" # 4. Drop privileges and execute unprivileged command echo "[mats-net] Dropping privileges to ${TARGET_USER} and launching: $*" if command -v setpriv >/dev/null 2>&1; then setpriv --reuid="$TARGET_UID" --regid="$TARGET_GID" --init-groups -- "$@" & CHILD_PID=$! wait "$CHILD_PID" else su -s /bin/bash "$TARGET_USER" -c '"$@"' -- "$@" & CHILD_PID=$! wait "$CHILD_PID" fi