2020-12-26 06:53:51 +08:00
|
|
|
#!/bin/sh
|
2020-12-23 03:37:43 +08:00
|
|
|
|
2021-05-10 07:40:22 +08:00
|
|
|
# Simplified istio iptables
|
|
|
|
# Use a different GID to run iperf3 or tests.
|
2020-12-23 03:37:43 +08:00
|
|
|
|
|
|
|
# Defaults:
|
|
|
|
# - capture all in and out traffic, unless:
|
|
|
|
# - {INBOUND|OUTBOUND}_PORTS_INCLUDE - only those ports will be captured
|
|
|
|
# - {INBOUND|OUTBOUND}_PORTS_EXCLUDE - if INCLUDE is not set, everything except those ports
|
|
|
|
# To turn off, set *_PORTS_INCLUDE to "-".
|
|
|
|
|
|
|
|
# Ranges are not supported: istio used them as a workaround
|
|
|
|
# before transparent proxy worked efficiently.
|
|
|
|
|
|
|
|
# Capturing with tproxy can be done with separate script, should
|
|
|
|
# not be mixed in.
|
|
|
|
|
|
|
|
# For testing iperf3, use:
|
2021-01-25 14:27:23 +08:00
|
|
|
# PROXY_GROUP=costin INBOUND_PORTS_INCLUDE=5201 OUTBOUND_PORTS_INCLUDE=5201
|
2020-12-23 03:37:43 +08:00
|
|
|
|
|
|
|
OUTBOUND_CAPTURE_PORT=${OUTBOUND_CAPTURE_PORT:-15001}
|
|
|
|
INBOUND_CAPTURE_PORT=${INBOUND_CAPTURE_PORT:-15006}
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
set -x
|
|
|
|
|
|
|
|
# Default mode is Istio compatible capture
|
|
|
|
if [ -z "${IN}" ] ; then
|
|
|
|
INBOUND_PORTS_EXCLUDE=${INBOUND_PORTS_EXCLUDE:-"-"}
|
|
|
|
OUTBOUND_PORTS_EXCLUDE=${OUTBOUND_PORTS_EXCLUDE:-"-"}
|
|
|
|
else
|
|
|
|
# Default opt-in list of capture.
|
|
|
|
# May be set to "-" to not capture anything.
|
|
|
|
# If _PORTS_EXCLUDE is set, all ports except excluded are captured.
|
|
|
|
INBOUND_PORTS_INCLUDE=${INBOUND_PORTS_INCLUDE:-${IN}}
|
|
|
|
OUTBOUND_PORTS_INCLUDE=${OUTBOUND_PORTS_INCLUDE:-${OUT}}
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2020-12-23 03:37:43 +08:00
|
|
|
# If INCLUDE is set, only those ports are captured, else
|
|
|
|
# all ports except EXCLUDE are captured
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
if [ -z "${PROXY_GID}" ] ; then
|
2020-12-23 03:37:43 +08:00
|
|
|
PROXY_GID=$(id -g "${PROXY_GROUP:-istio-proxy}")
|
|
|
|
fi
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
ipt_clean() {
|
2020-12-23 03:37:43 +08:00
|
|
|
# Remove the old chains, to generate new configs.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -D PREROUTING -p tcp -j ISTIO_INBOUND 2>/dev/null
|
|
|
|
${IPT} -t mangle -D PREROUTING -p tcp -j ISTIO_INBOUND 2>/dev/null
|
|
|
|
${IPT} -t nat -D OUTPUT -p tcp -j ISTIO_OUTPUT 2>/dev/null
|
2020-12-23 03:37:43 +08:00
|
|
|
|
|
|
|
# Flush and delete the istio chains.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -F ISTIO_OUTPUT 2>/dev/null
|
|
|
|
${IPT} -t nat -X ISTIO_OUTPUT 2>/dev/null
|
|
|
|
${IPT} -t nat -F ISTIO_INBOUND 2>/dev/null
|
|
|
|
${IPT} -t nat -X ISTIO_INBOUND 2>/dev/null
|
2020-12-23 03:37:43 +08:00
|
|
|
|
|
|
|
# Must be last, the others refer to it
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -F ISTIO_REDIRECT 2>/dev/null
|
|
|
|
${IPT} -t nat -X ISTIO_REDIRECT 2>/dev/null
|
|
|
|
${IPT} -t nat -F ISTIO_IN_REDIRECT 2>/dev/null
|
|
|
|
${IPT} -t nat -X ISTIO_IN_REDIRECT 2>/dev/null
|
2020-12-23 03:37:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
ipt_out() {
|
2020-12-23 03:37:43 +08:00
|
|
|
# Create a new chain for redirecting outbound traffic to the common Envoy port.
|
|
|
|
# In both chains, '-j RETURN' bypasses Envoy and '-j ISTIO_REDIRECT'
|
|
|
|
# redirects to Envoy.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -N ISTIO_REDIRECT
|
|
|
|
${IPT} -t nat -A ISTIO_REDIRECT -p tcp -j REDIRECT --to-port "${OUTBOUND_CAPTURE_PORT}"
|
2021-01-06 13:29:04 +08:00
|
|
|
|
2020-12-23 03:37:43 +08:00
|
|
|
# Create a new chain for selectively redirecting outbound packets to Envoy.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -N ISTIO_OUTPUT
|
2020-12-23 03:37:43 +08:00
|
|
|
# Jump to the ISTIO_OUTPUT chain from OUTPUT chain for all tcp traffic.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -A OUTPUT -p tcp -j ISTIO_OUTPUT
|
|
|
|
|
|
|
|
if [ ${IPT} == "iptables" ] ; then
|
|
|
|
# 127.0.0.6 is bind connect from inbound passthrough cluster
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -o lo -s 127.0.0.6/32 -j RETURN
|
|
|
|
# Skip redirection for Envoy-aware applications and
|
|
|
|
# container-to-container traffic both of which explicitly use
|
|
|
|
# localhost.
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -d 127.0.0.1/32 -j RETURN
|
|
|
|
else
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -d ::1/32 -j RETURN
|
|
|
|
# Capture FD00 - VIP for mesh nodes
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -d fd00::/16 -j ISTIO_REDIRECT
|
|
|
|
fi
|
2020-12-23 03:37:43 +08:00
|
|
|
|
2021-01-06 13:29:04 +08:00
|
|
|
# Avoid infinite loops. Don't redirect Envoy traffic directly back to
|
|
|
|
# Envoy for non-loopback traffic.
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -m owner --gid-owner "${PROXY_GID}" -j RETURN
|
|
|
|
|
|
|
|
if [ -n "${OUTBOUND_PORTS_EXCLUDE}" ]; then
|
|
|
|
# BTS port is direct.
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -p tcp --dport 15007 -j RETURN
|
|
|
|
if [ "${OUTBOUND_PORTS_EXCLUDE}" != "-" ]; then
|
|
|
|
IFS=,
|
|
|
|
for port in $OUTBOUND_PORTS_EXCLUDE ; do
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -p tcp --dport "${port}" -j RETURN
|
2020-12-23 03:37:43 +08:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
# Everything else
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -p tcp -j ISTIO_REDIRECT
|
|
|
|
else
|
|
|
|
# For probing iptables
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -p tcp --dport 15201 -j ISTIO_REDIRECT
|
|
|
|
if [ -n "${OUTBOUND_PORTS_INCLUDE}" ]; then
|
|
|
|
IFS=,
|
|
|
|
for port in ${OUTBOUND_PORTS_INCLUDE} ; do
|
|
|
|
${IPT} -t nat -A ISTIO_OUTPUT -p tcp --dport "${port}" -j ISTIO_REDIRECT
|
|
|
|
done
|
|
|
|
fi
|
2020-12-23 03:37:43 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
ipt_in() {
|
2020-12-23 03:37:43 +08:00
|
|
|
# Use this chain also for redirecting inbound traffic to the common Envoy port
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -N ISTIO_IN_REDIRECT
|
|
|
|
${IPT} -t nat -A ISTIO_IN_REDIRECT -p tcp -j REDIRECT --to-port "${INBOUND_CAPTURE_PORT}"
|
|
|
|
|
|
|
|
${IPT} -t nat -N ISTIO_INBOUND
|
|
|
|
${IPT} -t nat -A PREROUTING ${IN_IF:-} -p tcp -j ISTIO_INBOUND
|
|
|
|
|
|
|
|
# Istio uses * to indicate all capture.
|
|
|
|
if [ -n "${INBOUND_PORTS_EXCLUDE}" ]; then
|
|
|
|
${IPT} -t nat -A ISTIO_INBOUND -p tcp --dport 22 -j RETURN
|
|
|
|
${IPT} -t nat -A ISTIO_INBOUND -p tcp --dport 15007 -j RETURN
|
|
|
|
if [ "${INBOUND_PORTS_EXCLUDE}" != "-" ]; then
|
|
|
|
IFS=,
|
|
|
|
for port in ${INBOUND_PORTS_EXCLUDE} ; do
|
|
|
|
${IPT} -t nat -A ISTIO_INBOUND -p tcp --dport "${port}" -j RETURN
|
2020-12-23 03:37:43 +08:00
|
|
|
done
|
|
|
|
fi
|
2021-01-25 14:27:23 +08:00
|
|
|
${IPT} -t nat -A ISTIO_INBOUND -p tcp -j ISTIO_IN_REDIRECT
|
2020-12-23 03:37:43 +08:00
|
|
|
else
|
2021-01-25 14:27:23 +08:00
|
|
|
if [ "${INBOUND_PORTS_INCLUDE}" != "-" ] ; then
|
|
|
|
IFS=,
|
|
|
|
for port in ${INBOUND_PORTS_INCLUDE}; do
|
|
|
|
${IPT} -t nat -A ISTIO_INBOUND -p tcp --dport "${port}" -j ISTIO_IN_REDIRECT
|
2020-12-23 03:37:43 +08:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
IPT=iptables ipt_clean
|
|
|
|
IPT=iptables ipt_in
|
|
|
|
IPT=iptables ipt_out
|
2020-12-23 03:37:43 +08:00
|
|
|
|
2021-01-25 14:27:23 +08:00
|
|
|
IPT=ip6tables ipt_clean
|
|
|
|
IPT=ip6tables ipt_in
|
|
|
|
IPT=ip6tables ipt_out
|
2020-12-23 03:37:43 +08:00
|
|
|
|