2015-03-06 01:43:14 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/iptables"
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-03-06 01:43:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// DockerChain: DOCKER iptable chain name
|
|
|
|
const (
|
|
|
|
DockerChain = "DOCKER"
|
|
|
|
)
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
func setupIPChains(config *configuration) (*iptables.ChainInfo, *iptables.ChainInfo, error) {
|
2015-03-06 01:43:14 -05:00
|
|
|
// Sanity check.
|
2015-04-15 01:25:42 -04:00
|
|
|
if config.EnableIPTables == false {
|
2015-06-11 21:12:00 -04:00
|
|
|
return nil, nil, fmt.Errorf("Cannot create new chains, EnableIPTable is disabled")
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04:00
|
|
|
hairpinMode := !config.EnableUserlandProxy
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
natChain, err := iptables.NewChain(DockerChain, iptables.Nat, hairpinMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to create NAT chain: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := iptables.RemoveExistingChain(DockerChain, iptables.Nat); err != nil {
|
|
|
|
logrus.Warnf("Failed on removing iptables NAT chain on cleanup: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
filterChain, err := iptables.NewChain(DockerChain, iptables.Filter, hairpinMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to create FILTER chain: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return natChain, filterChain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *bridgeNetwork) setupIPTables(config *networkConfiguration, i *bridgeInterface) error {
|
|
|
|
d := n.driver
|
|
|
|
d.Lock()
|
|
|
|
driverConfig := d.config
|
|
|
|
d.Unlock()
|
|
|
|
|
|
|
|
// Sanity check.
|
|
|
|
if driverConfig.EnableIPTables == false {
|
|
|
|
return fmt.Errorf("Cannot program chains, EnableIPTable is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pickup this configuraton option from driver
|
|
|
|
hairpinMode := !driverConfig.EnableUserlandProxy
|
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
addrv4, _, err := netutils.GetIfaceAddr(config.BridgeName)
|
2015-03-06 01:43:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to setup IP tables, cannot acquire Interface address: %s", err.Error())
|
|
|
|
}
|
2015-07-13 04:23:02 -04:00
|
|
|
ipnet := addrv4.(*net.IPNet)
|
|
|
|
maskedAddrv4 := &net.IPNet{
|
|
|
|
IP: ipnet.IP.Mask(ipnet.Mask),
|
|
|
|
Mask: ipnet.Mask,
|
|
|
|
}
|
|
|
|
if err = setupIPTablesInternal(config.BridgeName, maskedAddrv4, config.EnableICC, config.EnableIPMasquerade, hairpinMode, true); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
return fmt.Errorf("Failed to Setup IP tables: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
natChain, filterChain, err := n.getDriverChains()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to setup IP tables, cannot acquire chain info %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iptables.ProgramChain(natChain, config.BridgeName, hairpinMode)
|
2015-03-06 01:43:14 -05:00
|
|
|
if err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
return fmt.Errorf("Failed to program NAT chain: %s", err.Error())
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
err = iptables.ProgramChain(filterChain, config.BridgeName, hairpinMode)
|
2015-03-06 01:43:14 -05:00
|
|
|
if err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
return fmt.Errorf("Failed to program FILTER chain: %s", err.Error())
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
n.portMapper.SetIptablesChain(filterChain, n.getNetworkBridgeName())
|
2015-03-06 01:43:14 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
type iptRule struct {
|
|
|
|
table iptables.Table
|
|
|
|
chain string
|
|
|
|
preArgs []string
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04:00
|
|
|
func setupIPTablesInternal(bridgeIface string, addr net.Addr, icc, ipmasq, hairpin, enable bool) error {
|
2015-04-10 14:59:05 -04:00
|
|
|
|
2015-03-06 01:43:14 -05:00
|
|
|
var (
|
2015-05-18 19:49:12 -04: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"}}
|
|
|
|
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 01:43:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set NAT.
|
|
|
|
if ipmasq {
|
|
|
|
if err := programChainRule(natRule, "NAT", enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04: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 01:43:14 -05: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 14:59:05 -04:00
|
|
|
func programChainRule(rule iptRule, ruleDescr string, insert bool) error {
|
2015-03-06 01:43:14 -05:00
|
|
|
var (
|
|
|
|
prefix []string
|
|
|
|
operation string
|
|
|
|
condition bool
|
2015-04-10 14:59:05 -04:00
|
|
|
doesExist = iptables.Exists(rule.table, rule.chain, rule.args...)
|
2015-03-06 01:43:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if insert {
|
2015-04-10 14:59:05 -04:00
|
|
|
condition = !doesExist
|
|
|
|
prefix = []string{"-I", rule.chain}
|
2015-03-06 01:43:14 -05:00
|
|
|
operation = "enable"
|
|
|
|
} else {
|
2015-04-10 14:59:05 -04:00
|
|
|
condition = doesExist
|
|
|
|
prefix = []string{"-D", rule.chain}
|
2015-03-06 01:43:14 -05:00
|
|
|
operation = "disable"
|
|
|
|
}
|
2015-04-10 14:59:05 -04:00
|
|
|
if rule.preArgs != nil {
|
|
|
|
prefix = append(rule.preArgs, prefix...)
|
|
|
|
}
|
2015-03-06 01:43:14 -05:00
|
|
|
|
|
|
|
if condition {
|
2015-04-10 14:59:05 -04:00
|
|
|
if output, err := iptables.Raw(append(prefix, rule.args...)...); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
return fmt.Errorf("Unable to %s %s rule: %s", operation, ruleDescr, err.Error())
|
|
|
|
} else if len(output) != 0 {
|
2015-04-10 14:59:05 -04:00
|
|
|
return &iptables.ChainError{Chain: rule.chain, Output: output}
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIcc(bridgeIface string, iccEnable, insert bool) error {
|
|
|
|
var (
|
2015-04-10 14:59:05 -04:00
|
|
|
table = iptables.Filter
|
|
|
|
chain = "FORWARD"
|
|
|
|
args = []string{"-i", bridgeIface, "-o", bridgeIface, "-j"}
|
2015-03-06 01:43:14 -05:00
|
|
|
acceptArgs = append(args, "ACCEPT")
|
|
|
|
dropArgs = append(args, "DROP")
|
|
|
|
)
|
|
|
|
|
|
|
|
if insert {
|
|
|
|
if !iccEnable {
|
2015-04-10 14:59:05 -04:00
|
|
|
iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
if !iptables.Exists(table, chain, dropArgs...) {
|
2015-04-29 14:46:36 -04:00
|
|
|
if output, err := iptables.Raw(append([]string{"-A", chain}, dropArgs...)...); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
return fmt.Errorf("Unable to prevent intercontainer communication: %s", err.Error())
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("Error disabling intercontainer communication: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 14:59:05 -04:00
|
|
|
iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
if !iptables.Exists(table, chain, acceptArgs...) {
|
2015-07-24 13:20:48 -04:00
|
|
|
if output, err := iptables.Raw(append([]string{"-I", chain}, acceptArgs...)...); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
return fmt.Errorf("Unable to allow intercontainer communication: %s", err.Error())
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("Error enabling intercontainer communication: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove any ICC rule.
|
|
|
|
if !iccEnable {
|
2015-04-10 14:59:05 -04:00
|
|
|
if iptables.Exists(table, chain, dropArgs...) {
|
|
|
|
iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 14:59:05 -04:00
|
|
|
if iptables.Exists(table, chain, acceptArgs...) {
|
|
|
|
iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-02 21:40:55 -04:00
|
|
|
|
|
|
|
// Control Inter Network Communication. Install/remove only if it is not/is present.
|
|
|
|
func setINC(network1, network2 string, enable bool) error {
|
|
|
|
var (
|
|
|
|
table = iptables.Filter
|
|
|
|
chain = "FORWARD"
|
|
|
|
args = [2][]string{{"-s", network1, "-d", network2, "-j", "DROP"}, {"-s", network2, "-d", network1, "-j", "DROP"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
if enable {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if iptables.Exists(table, chain, args[i]...) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if output, err := iptables.Raw(append([]string{"-I", chain}, args[i]...)...); err != nil {
|
|
|
|
return fmt.Errorf("unable to add inter-network communication rule: %s", err.Error())
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("error adding inter-network communication rule: %s", string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if !iptables.Exists(table, chain, args[i]...) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if output, err := iptables.Raw(append([]string{"-D", chain}, args[i]...)...); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove inter-network communication rule: %s", err.Error())
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("error removing inter-network communication rule: %s", string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|