2015-03-06 06:43:14 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2016-11-22 01:29:53 +00:00
|
|
|
"errors"
|
2015-03-06 06:43:14 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2015-06-12 01:12:00 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/iptables"
|
2015-03-06 06:43:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DockerChain: DOCKER iptable chain name
|
|
|
|
const (
|
2015-12-15 05:57:56 +00:00
|
|
|
DockerChain = "DOCKER"
|
|
|
|
IsolationChain = "DOCKER-ISOLATION"
|
2015-03-06 06:43:14 +00:00
|
|
|
)
|
|
|
|
|
2015-12-15 05:57:56 +00:00
|
|
|
func setupIPChains(config *configuration) (*iptables.ChainInfo, *iptables.ChainInfo, *iptables.ChainInfo, error) {
|
2015-03-06 06:43:14 +00:00
|
|
|
// Sanity check.
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.EnableIPTables == false {
|
2016-11-22 01:29:53 +00:00
|
|
|
return nil, nil, nil, errors.New("cannot create new chains, EnableIPTable is disabled")
|
2015-03-06 06:43:14 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 23:49:12 +00:00
|
|
|
hairpinMode := !config.EnableUserlandProxy
|
|
|
|
|
2015-06-12 01:12:00 +00:00
|
|
|
natChain, err := iptables.NewChain(DockerChain, iptables.Nat, hairpinMode)
|
|
|
|
if err != nil {
|
2015-12-15 05:57:56 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to create NAT chain: %v", err)
|
2015-06-12 01:12:00 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := iptables.RemoveExistingChain(DockerChain, iptables.Nat); err != nil {
|
2015-12-15 05:57:56 +00:00
|
|
|
logrus.Warnf("failed on removing iptables NAT chain on cleanup: %v", err)
|
2015-06-12 01:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-15 05:57:56 +00:00
|
|
|
filterChain, err := iptables.NewChain(DockerChain, iptables.Filter, false)
|
2015-06-12 01:12:00 +00:00
|
|
|
if err != nil {
|
2015-12-15 05:57:56 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to create FILTER chain: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := iptables.RemoveExistingChain(DockerChain, iptables.Filter); err != nil {
|
|
|
|
logrus.Warnf("failed on removing iptables FILTER chain on cleanup: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
isolationChain, err := iptables.NewChain(IsolationChain, iptables.Filter, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("failed to create FILTER isolation chain: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addReturnRule(IsolationChain); err != nil {
|
|
|
|
return nil, nil, nil, err
|
2015-06-12 01:12:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 05:57:56 +00:00
|
|
|
return natChain, filterChain, isolationChain, nil
|
2015-06-12 01:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *bridgeNetwork) setupIPTables(config *networkConfiguration, i *bridgeInterface) error {
|
2015-10-16 20:07:35 +00:00
|
|
|
var err error
|
|
|
|
|
2015-06-12 01:12:00 +00:00
|
|
|
d := n.driver
|
|
|
|
d.Lock()
|
|
|
|
driverConfig := d.config
|
|
|
|
d.Unlock()
|
|
|
|
|
|
|
|
// Sanity check.
|
|
|
|
if driverConfig.EnableIPTables == false {
|
2016-11-22 01:29:53 +00:00
|
|
|
return errors.New("Cannot program chains, EnableIPTable is disabled")
|
2015-06-12 01:12:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 18:44:45 +00:00
|
|
|
// Pickup this configuration option from driver
|
2015-06-12 01:12:00 +00:00
|
|
|
hairpinMode := !driverConfig.EnableUserlandProxy
|
|
|
|
|
2015-07-13 08:23:02 +00:00
|
|
|
maskedAddrv4 := &net.IPNet{
|
2015-10-16 20:07:35 +00:00
|
|
|
IP: i.bridgeIPv4.IP.Mask(i.bridgeIPv4.Mask),
|
|
|
|
Mask: i.bridgeIPv4.Mask,
|
2015-07-13 08:23:02 +00:00
|
|
|
}
|
2016-01-06 13:06:23 +00:00
|
|
|
if config.Internal {
|
2016-10-28 18:42:50 +00:00
|
|
|
if err = setupInternalNetworkRules(config.BridgeName, maskedAddrv4, config.EnableICC, true); err != nil {
|
2016-01-06 13:06:23 +00:00
|
|
|
return fmt.Errorf("Failed to Setup IP tables: %s", err.Error())
|
|
|
|
}
|
|
|
|
n.registerIptCleanFunc(func() error {
|
2016-10-28 18:42:50 +00:00
|
|
|
return setupInternalNetworkRules(config.BridgeName, maskedAddrv4, config.EnableICC, false)
|
2016-01-06 13:06:23 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if err = setupIPTablesInternal(config.BridgeName, maskedAddrv4, config.EnableICC, config.EnableIPMasquerade, hairpinMode, true); err != nil {
|
|
|
|
return fmt.Errorf("Failed to Setup IP tables: %s", err.Error())
|
|
|
|
}
|
|
|
|
n.registerIptCleanFunc(func() error {
|
|
|
|
return setupIPTablesInternal(config.BridgeName, maskedAddrv4, config.EnableICC, config.EnableIPMasquerade, hairpinMode, false)
|
|
|
|
})
|
|
|
|
natChain, filterChain, _, err := n.getDriverChains()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to setup IP tables, cannot acquire chain info %s", err.Error())
|
|
|
|
}
|
2015-03-06 06:43:14 +00:00
|
|
|
|
2016-01-06 13:06:23 +00:00
|
|
|
err = iptables.ProgramChain(natChain, config.BridgeName, hairpinMode, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to program NAT chain: %s", err.Error())
|
|
|
|
}
|
2015-06-12 01:12:00 +00:00
|
|
|
|
2016-01-06 13:06:23 +00:00
|
|
|
err = iptables.ProgramChain(filterChain, config.BridgeName, hairpinMode, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to program FILTER chain: %s", err.Error())
|
|
|
|
}
|
2015-03-06 06:43:14 +00:00
|
|
|
|
2016-01-06 13:06:23 +00:00
|
|
|
n.registerIptCleanFunc(func() error {
|
|
|
|
return iptables.ProgramChain(filterChain, config.BridgeName, hairpinMode, false)
|
|
|
|
})
|
|
|
|
|
2016-02-09 01:32:06 +00:00
|
|
|
n.portMapper.SetIptablesChain(natChain, n.getNetworkBridgeName())
|
2015-03-06 06:43:14 +00:00
|
|
|
}
|
2015-12-15 05:57:56 +00:00
|
|
|
|
|
|
|
if err := ensureJumpRule("FORWARD", IsolationChain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-06 06:43:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-10 18:59:05 +00:00
|
|
|
type iptRule struct {
|
|
|
|
table iptables.Table
|
|
|
|
chain string
|
|
|
|
preArgs []string
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:49:12 +00:00
|
|
|
func setupIPTablesInternal(bridgeIface string, addr net.Addr, icc, ipmasq, hairpin, enable bool) error {
|
2015-04-10 18:59:05 +00:00
|
|
|
|
2015-03-06 06:43:14 +00:00
|
|
|
var (
|
2015-05-18 23:49:12 +00:00
|
|
|
address = addr.String()
|
|
|
|
natRule = iptRule{table: iptables.Nat, chain: "POSTROUTING", preArgs: []string{"-t", "nat"}, args: []string{"-s", address, "!", "-o", bridgeIface, "-j", "MASQUERADE"}}
|
|
|
|
hpNatRule = iptRule{table: iptables.Nat, chain: "POSTROUTING", preArgs: []string{"-t", "nat"}, args: []string{"-m", "addrtype", "--src-type", "LOCAL", "-o", bridgeIface, "-j", "MASQUERADE"}}
|
2016-02-01 21:56:25 +00:00
|
|
|
skipDNAT = iptRule{table: iptables.Nat, chain: DockerChain, preArgs: []string{"-t", "nat"}, args: []string{"-i", bridgeIface, "-j", "RETURN"}}
|
2015-05-18 23:49:12 +00:00
|
|
|
outRule = iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-i", bridgeIface, "!", "-o", bridgeIface, "-j", "ACCEPT"}}
|
|
|
|
inRule = iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-o", bridgeIface, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}}
|
2015-03-06 06:43:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set NAT.
|
|
|
|
if ipmasq {
|
|
|
|
if err := programChainRule(natRule, "NAT", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 01:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ipmasq && !hairpin {
|
2016-02-01 21:56:25 +00:00
|
|
|
if err := programChainRule(skipDNAT, "SKIP DNAT", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-06 06:43:14 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 23:49:12 +00:00
|
|
|
// In hairpin mode, masquerade traffic from localhost
|
|
|
|
if hairpin {
|
|
|
|
if err := programChainRule(hpNatRule, "MASQ LOCAL HOST", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 06:43:14 +00:00
|
|
|
// Set Inter Container Communication.
|
|
|
|
if err := setIcc(bridgeIface, icc, enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Accept on all non-intercontainer outgoing packets.
|
|
|
|
if err := programChainRule(outRule, "ACCEPT NON_ICC OUTGOING", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Accept on incoming packets for existing connections.
|
|
|
|
if err := programChainRule(inRule, "ACCEPT INCOMING", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-10 18:59:05 +00:00
|
|
|
func programChainRule(rule iptRule, ruleDescr string, insert bool) error {
|
2015-03-06 06:43:14 +00:00
|
|
|
var (
|
|
|
|
prefix []string
|
|
|
|
operation string
|
|
|
|
condition bool
|
2015-04-10 18:59:05 +00:00
|
|
|
doesExist = iptables.Exists(rule.table, rule.chain, rule.args...)
|
2015-03-06 06:43:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if insert {
|
2015-04-10 18:59:05 +00:00
|
|
|
condition = !doesExist
|
|
|
|
prefix = []string{"-I", rule.chain}
|
2015-03-06 06:43:14 +00:00
|
|
|
operation = "enable"
|
|
|
|
} else {
|
2015-04-10 18:59:05 +00:00
|
|
|
condition = doesExist
|
|
|
|
prefix = []string{"-D", rule.chain}
|
2015-03-06 06:43:14 +00:00
|
|
|
operation = "disable"
|
|
|
|
}
|
2015-04-10 18:59:05 +00:00
|
|
|
if rule.preArgs != nil {
|
|
|
|
prefix = append(rule.preArgs, prefix...)
|
|
|
|
}
|
2015-03-06 06:43:14 +00:00
|
|
|
|
|
|
|
if condition {
|
2015-12-15 05:57:56 +00:00
|
|
|
if err := iptables.RawCombinedOutput(append(prefix, rule.args...)...); err != nil {
|
2015-03-06 06:43:14 +00:00
|
|
|
return fmt.Errorf("Unable to %s %s rule: %s", operation, ruleDescr, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIcc(bridgeIface string, iccEnable, insert bool) error {
|
|
|
|
var (
|
2015-04-10 18:59:05 +00:00
|
|
|
table = iptables.Filter
|
|
|
|
chain = "FORWARD"
|
|
|
|
args = []string{"-i", bridgeIface, "-o", bridgeIface, "-j"}
|
2015-03-06 06:43:14 +00:00
|
|
|
acceptArgs = append(args, "ACCEPT")
|
|
|
|
dropArgs = append(args, "DROP")
|
|
|
|
)
|
|
|
|
|
|
|
|
if insert {
|
|
|
|
if !iccEnable {
|
2015-04-10 18:59:05 +00:00
|
|
|
iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
|
2015-03-06 06:43:14 +00:00
|
|
|
|
2015-04-10 18:59:05 +00:00
|
|
|
if !iptables.Exists(table, chain, dropArgs...) {
|
2015-12-15 05:57:56 +00:00
|
|
|
if err := iptables.RawCombinedOutput(append([]string{"-A", chain}, dropArgs...)...); err != nil {
|
2015-03-06 06:43:14 +00:00
|
|
|
return fmt.Errorf("Unable to prevent intercontainer communication: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 18:59:05 +00:00
|
|
|
iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
|
2015-03-06 06:43:14 +00:00
|
|
|
|
2015-04-10 18:59:05 +00:00
|
|
|
if !iptables.Exists(table, chain, acceptArgs...) {
|
2015-12-15 05:57:56 +00:00
|
|
|
if err := iptables.RawCombinedOutput(append([]string{"-I", chain}, acceptArgs...)...); err != nil {
|
2015-03-06 06:43:14 +00:00
|
|
|
return fmt.Errorf("Unable to allow intercontainer communication: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove any ICC rule.
|
|
|
|
if !iccEnable {
|
2015-04-10 18:59:05 +00:00
|
|
|
if iptables.Exists(table, chain, dropArgs...) {
|
|
|
|
iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
|
2015-03-06 06:43:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 18:59:05 +00:00
|
|
|
if iptables.Exists(table, chain, acceptArgs...) {
|
|
|
|
iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
|
2015-03-06 06:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-03 01:40:55 +00:00
|
|
|
|
|
|
|
// Control Inter Network Communication. Install/remove only if it is not/is present.
|
2015-12-15 05:57:56 +00:00
|
|
|
func setINC(iface1, iface2 string, enable bool) error {
|
2015-06-03 01:40:55 +00:00
|
|
|
var (
|
|
|
|
table = iptables.Filter
|
2015-12-15 05:57:56 +00:00
|
|
|
chain = IsolationChain
|
|
|
|
args = [2][]string{{"-i", iface1, "-o", iface2, "-j", "DROP"}, {"-i", iface2, "-o", iface1, "-j", "DROP"}}
|
2015-06-03 01:40:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if enable {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if iptables.Exists(table, chain, args[i]...) {
|
|
|
|
continue
|
|
|
|
}
|
2015-12-15 05:57:56 +00:00
|
|
|
if err := iptables.RawCombinedOutput(append([]string{"-I", chain}, args[i]...)...); err != nil {
|
|
|
|
return fmt.Errorf("unable to add inter-network communication rule: %v", err)
|
2015-06-03 01:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if !iptables.Exists(table, chain, args[i]...) {
|
|
|
|
continue
|
|
|
|
}
|
2015-12-15 05:57:56 +00:00
|
|
|
if err := iptables.RawCombinedOutput(append([]string{"-D", chain}, args[i]...)...); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove inter-network communication rule: %v", err)
|
2015-06-03 01:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-15 05:57:56 +00:00
|
|
|
|
|
|
|
func addReturnRule(chain string) error {
|
|
|
|
var (
|
|
|
|
table = iptables.Filter
|
|
|
|
args = []string{"-j", "RETURN"}
|
|
|
|
)
|
|
|
|
|
|
|
|
if iptables.Exists(table, chain, args...) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := iptables.RawCombinedOutput(append([]string{"-I", chain}, args...)...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to add return rule in %s chain: %s", chain, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the jump rule is on top
|
|
|
|
func ensureJumpRule(fromChain, toChain string) error {
|
|
|
|
var (
|
|
|
|
table = iptables.Filter
|
|
|
|
args = []string{"-j", toChain}
|
|
|
|
)
|
|
|
|
|
|
|
|
if iptables.Exists(table, fromChain, args...) {
|
|
|
|
err := iptables.RawCombinedOutput(append([]string{"-D", fromChain}, args...)...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to remove jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := iptables.RawCombinedOutput(append([]string{"-I", fromChain}, args...)...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to insert jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 08:30:10 +00:00
|
|
|
|
|
|
|
func removeIPChains() {
|
|
|
|
for _, chainInfo := range []iptables.ChainInfo{
|
2016-01-06 13:06:23 +00:00
|
|
|
{Name: DockerChain, Table: iptables.Nat},
|
|
|
|
{Name: DockerChain, Table: iptables.Filter},
|
|
|
|
{Name: IsolationChain, Table: iptables.Filter},
|
2015-09-15 08:30:10 +00:00
|
|
|
} {
|
|
|
|
if err := chainInfo.Remove(); err != nil {
|
|
|
|
logrus.Warnf("Failed to remove existing iptables entries in table %s chain %s : %v", chainInfo.Table, chainInfo.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 13:06:23 +00:00
|
|
|
|
2016-10-28 18:42:50 +00:00
|
|
|
func setupInternalNetworkRules(bridgeIface string, addr net.Addr, icc, insert bool) error {
|
2016-01-06 13:06:23 +00:00
|
|
|
var (
|
|
|
|
inDropRule = iptRule{table: iptables.Filter, chain: IsolationChain, args: []string{"-i", bridgeIface, "!", "-d", addr.String(), "-j", "DROP"}}
|
|
|
|
outDropRule = iptRule{table: iptables.Filter, chain: IsolationChain, args: []string{"-o", bridgeIface, "!", "-s", addr.String(), "-j", "DROP"}}
|
|
|
|
)
|
|
|
|
if err := programChainRule(inDropRule, "DROP INCOMING", insert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := programChainRule(outDropRule, "DROP OUTGOING", insert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-28 18:42:50 +00:00
|
|
|
// Set Inter Container Communication.
|
|
|
|
if err := setIcc(bridgeIface, icc, insert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 13:06:23 +00:00
|
|
|
return nil
|
|
|
|
}
|