#!/bin/bash if [ -z "$sip" ] || [ -z "$dip" ] || [ -z "$dprefix" ] || [ -z "$sport" ] || [ -z "$dport" ]; then >&2 echo 'missing required env var' exit 1 fi NAME="F$(echo "$sip:$sport -> $dip/$dprefix:$dport" | sha256sum | head -c 15)" for kind in INPUT FORWARD ACCEPT; do if ! iptables -C $kind -j "${NAME}_${kind}" 2> /dev/null; then iptables -N "${NAME}_${kind}" 2> /dev/null iptables -A $kind -j "${NAME}_${kind}" fi done for kind in PREROUTING INPUT OUTPUT POSTROUTING; do if ! iptables -t nat -C $kind -j "${NAME}_${kind}" 2> /dev/null; then iptables -t nat -N "${NAME}_${kind}" 2> /dev/null iptables -t nat -A $kind -j "${NAME}_${kind}" fi done err=0 trap 'err=1' ERR for kind in INPUT FORWARD ACCEPT; do iptables -F "${NAME}_${kind}" 2> /dev/null done for kind in PREROUTING INPUT OUTPUT POSTROUTING; do iptables -t nat -F "${NAME}_${kind}" 2> /dev/null done if [ "$UNDO" = 1 ]; then conntrack -D -p tcp -d $sip --dport $sport || true # conntrack returns exit 1 if no connections are active conntrack -D -p udp -d $sip --dport $sport || true # conntrack returns exit 1 if no connections are active exit $err fi # DNAT: rewrite destination for incoming packets (external traffic) iptables -t nat -A ${NAME}_PREROUTING -d "$sip" -p tcp --dport "$sport" -j DNAT --to-destination "$dip:$dport" iptables -t nat -A ${NAME}_PREROUTING -d "$sip" -p udp --dport "$sport" -j DNAT --to-destination "$dip:$dport" # DNAT: rewrite destination for locally-originated packets (hairpin from host itself) iptables -t nat -A ${NAME}_OUTPUT -d "$sip" -p tcp --dport "$sport" -j DNAT --to-destination "$dip:$dport" iptables -t nat -A ${NAME}_OUTPUT -d "$sip" -p udp --dport "$sport" -j DNAT --to-destination "$dip:$dport" # MASQUERADE: rewrite source for all forwarded traffic to the destination # This ensures responses are routed back through the host regardless of source IP iptables -t nat -A ${NAME}_POSTROUTING -d "$dip" -p tcp --dport "$dport" -j MASQUERADE iptables -t nat -A ${NAME}_POSTROUTING -d "$dip" -p udp --dport "$dport" -j MASQUERADE # Allow new connections to be forwarded to the destination iptables -A ${NAME}_FORWARD -d $dip -p tcp --dport $dport -m state --state NEW -j ACCEPT iptables -A ${NAME}_FORWARD -d $dip -p udp --dport $dport -m state --state NEW -j ACCEPT exit $err