2016-11-09 15:46:49 +01:00
|
|
|
#!/bin/bash
|
2015-07-22 05:53:29 +02:00
|
|
|
|
2016-04-13 12:19:29 +02:00
|
|
|
export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
2016-03-12 20:25:09 +01:00
|
|
|
chain_exists() {
|
|
|
|
[ $# -lt 1 -o $# -gt 2 ] && {
|
2015-08-17 13:13:50 +02:00
|
|
|
echo "Usage: chain_exists <chain_name> [table]" >&2
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
local chain_name="$1" ; shift
|
|
|
|
[ $# -eq 1 ] && local table="--table $1"
|
|
|
|
iptables $table -n --list "$chain_name" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2016-10-26 05:08:35 +02:00
|
|
|
add_to_forward() {
|
|
|
|
local docker_int=$1
|
|
|
|
|
|
|
|
if [ `iptables -nvL FORWARD | grep ${docker_int} | wc -l` -eq 0 ]; then
|
|
|
|
iptables -A FORWARD -o ${docker_int} -j DOCKER
|
|
|
|
iptables -A FORWARD -o ${docker_int} -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
|
|
|
iptables -A FORWARD -i ${docker_int} ! -o ${docker_int} -j ACCEPT
|
|
|
|
iptables -A FORWARD -i ${docker_int} -o ${docker_int} -j ACCEPT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:25:03 +01:00
|
|
|
add_to_nat() {
|
|
|
|
local docker_int=$1
|
|
|
|
local subnet=$2
|
|
|
|
|
|
|
|
iptables -t nat -A POSTROUTING -s ${subnet} ! -o ${docker_int} -j MASQUERADE
|
|
|
|
iptables -t nat -A DOCKER -i ${docker_int} -j RETURN
|
|
|
|
}
|
|
|
|
|
2016-10-26 05:08:35 +02:00
|
|
|
add_to_docker_isolation() {
|
|
|
|
local int_in=$1
|
|
|
|
local int_out=$2
|
|
|
|
|
2016-11-09 15:46:49 +01:00
|
|
|
iptables -A DOCKER-ISOLATION -i ${int_in} -o ${int_out} -j DROP
|
2016-10-26 05:08:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 05:53:29 +02:00
|
|
|
DOCKER_INT="docker0"
|
|
|
|
DOCKER_NETWORK="172.17.0.0/16"
|
|
|
|
|
2015-08-17 13:13:50 +02:00
|
|
|
iptables-save | grep -v -- '-j DOCKER' | iptables-restore
|
|
|
|
chain_exists DOCKER && iptables -X DOCKER
|
|
|
|
chain_exists DOCKER nat && iptables -t nat -X DOCKER
|
|
|
|
|
2015-07-22 05:53:29 +02:00
|
|
|
iptables -N DOCKER
|
2016-10-24 05:46:11 +02:00
|
|
|
iptables -N DOCKER-ISOLATION
|
|
|
|
|
2016-10-26 05:08:35 +02:00
|
|
|
iptables -t nat -N DOCKER
|
|
|
|
|
2016-10-24 05:46:11 +02:00
|
|
|
iptables -A FORWARD -j DOCKER-ISOLATION
|
2016-10-26 05:08:35 +02:00
|
|
|
add_to_forward ${DOCKER_INT}
|
2015-07-22 05:53:29 +02:00
|
|
|
|
|
|
|
iptables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
|
|
|
|
iptables -t nat -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
|
|
|
|
iptables -t nat -A POSTROUTING -s ${DOCKER_NETWORK} ! -o ${DOCKER_INT} -j MASQUERADE
|
|
|
|
|
2016-11-09 15:46:49 +01:00
|
|
|
bridges=`docker network ls -q --filter='Driver=bridge'`
|
|
|
|
|
|
|
|
for bridge in $bridges; do
|
2016-11-09 16:25:03 +01:00
|
|
|
DOCKER_NET_INT="br-$bridge"
|
2016-11-09 15:46:49 +01:00
|
|
|
subnet=`docker network inspect -f '{{(index .IPAM.Config 0).Subnet}}' $bridge`
|
|
|
|
|
2016-11-09 16:25:03 +01:00
|
|
|
add_to_nat ${DOCKER_NET_INT} ${subnet}
|
|
|
|
add_to_forward ${DOCKER_NET_INT}
|
2016-11-09 15:46:49 +01:00
|
|
|
|
|
|
|
for other_bridge in $bridges; do
|
|
|
|
if [ $other_bridge != $bridge ]; then
|
2016-11-09 16:25:03 +01:00
|
|
|
add_to_docker_isolation ${DOCKER_NET_INT} br-$other_bridge
|
2016-11-09 15:46:49 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2015-07-22 05:53:29 +02:00
|
|
|
containers=`docker ps -q`
|
|
|
|
|
2016-10-26 05:08:35 +02:00
|
|
|
if [ `echo ${containers} | wc -c` -gt "1" ]; then
|
|
|
|
for container in ${containers}; do
|
|
|
|
netmode=`docker inspect -f "{{.HostConfig.NetworkMode}}" ${container}`
|
|
|
|
|
|
|
|
if [ $netmode == "default" ]; then
|
|
|
|
DOCKER_NET_INT=${DOCKER_INT}
|
|
|
|
ipaddr=`docker inspect -f "{{.NetworkSettings.IPAddress}}" ${container}`
|
|
|
|
else
|
2016-11-09 15:46:49 +01:00
|
|
|
DOCKER_NET_INT=br-$(docker inspect -f "{{.NetworkSettings.Networks.${netmode}.NetworkID}}" ${container} | cut -c -12)
|
2016-10-26 05:08:35 +02:00
|
|
|
ipaddr=`docker inspect -f "{{.NetworkSettings.Networks.${netmode}.IPAddress}}" ${container}`
|
|
|
|
fi
|
|
|
|
|
|
|
|
rules=`docker port ${container} | sed 's/ //g'`
|
2015-07-22 05:53:29 +02:00
|
|
|
|
2016-10-26 05:08:35 +02:00
|
|
|
if [ `echo ${rules} | wc -c` -gt "1" ]; then
|
|
|
|
for rule in ${rules}; do
|
2015-07-22 05:53:29 +02:00
|
|
|
src=`echo ${rule} | awk -F'->' '{ print $2 }'`
|
|
|
|
dst=`echo ${rule} | awk -F'->' '{ print $1 }'`
|
|
|
|
|
|
|
|
src_ip=`echo ${src} | awk -F':' '{ print $1 }'`
|
|
|
|
src_port=`echo ${src} | awk -F':' '{ print $2 }'`
|
|
|
|
|
|
|
|
dst_port=`echo ${dst} | awk -F'/' '{ print $1 }'`
|
|
|
|
dst_proto=`echo ${dst} | awk -F'/' '{ print $2 }'`
|
|
|
|
|
2016-10-24 05:46:11 +02:00
|
|
|
iptables -A DOCKER -d ${ipaddr}/32 ! -i ${DOCKER_NET_INT} -o ${DOCKER_NET_INT} -p ${dst_proto} -m ${dst_proto} --dport ${dst_port} -j ACCEPT
|
2015-07-22 05:53:29 +02:00
|
|
|
|
|
|
|
iptables -t nat -A POSTROUTING -s ${ipaddr}/32 -d ${ipaddr}/32 -p ${dst_proto} -m ${dst_proto} --dport ${dst_port} -j MASQUERADE
|
2016-04-27 00:58:16 +02:00
|
|
|
|
2016-11-01 22:46:17 +01:00
|
|
|
iptables_opt_src=""
|
|
|
|
if [ ${src_ip} != "0.0.0.0" ]; then
|
|
|
|
iptables_opt_src="-d ${src_ip}/32 "
|
|
|
|
fi
|
|
|
|
iptables -t nat -A DOCKER ${iptables_opt_src}! -i ${DOCKER_NET_INT} -p ${dst_proto} -m ${dst_proto} --dport ${src_port} -j DNAT --to-destination ${ipaddr}:${dst_port}
|
2015-07-22 05:53:29 +02:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2016-11-01 22:48:40 +01:00
|
|
|
|
|
|
|
iptables -A DOCKER-ISOLATION -j RETURN
|
|
|
|
iptables -t nat -I DOCKER -i ${DOCKER_INT} -j RETURN
|