2015-03-06 01:43:14 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/iptables"
|
2015-06-01 16:14:29 -04:00
|
|
|
"github.com/docker/libnetwork/portmapper"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2016-05-16 14:51:40 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-03-06 01:43:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
iptablesTestBridgeIP = "192.168.42.1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProgramIPTable(t *testing.T) {
|
|
|
|
// Create a test bridge with a basic bridge configuration (name + IPv4).
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2016-05-16 14:51:40 -04:00
|
|
|
|
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createTestBridge(getBasicTestConfig(), &bridgeInterface{nlh: nh}, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
|
|
|
// Store various iptables chain rules we care for.
|
|
|
|
rules := []struct {
|
2015-04-10 14:59:05 -04:00
|
|
|
rule iptRule
|
|
|
|
descr string
|
|
|
|
}{
|
|
|
|
{iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-d", "127.1.2.3", "-i", "lo", "-o", "lo", "-j", "DROP"}}, "Test Loopback"},
|
|
|
|
{iptRule{table: iptables.Nat, chain: "POSTROUTING", preArgs: []string{"-t", "nat"}, args: []string{"-s", iptablesTestBridgeIP, "!", "-o", DefaultBridgeName, "-j", "MASQUERADE"}}, "NAT Test"},
|
|
|
|
{iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-i", DefaultBridgeName, "!", "-o", DefaultBridgeName, "-j", "ACCEPT"}}, "Test ACCEPT NON_ICC OUTGOING"},
|
|
|
|
{iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-o", DefaultBridgeName, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}}, "Test ACCEPT INCOMING"},
|
|
|
|
{iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-i", DefaultBridgeName, "-o", DefaultBridgeName, "-j", "ACCEPT"}}, "Test enable ICC"},
|
|
|
|
{iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-i", DefaultBridgeName, "-o", DefaultBridgeName, "-j", "DROP"}}, "Test disable ICC"},
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the chain rules' insertion and removal.
|
|
|
|
for _, c := range rules {
|
2015-04-10 14:59:05 -04:00
|
|
|
assertIPTableChainProgramming(c.rule, c.descr, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
func TestSetupIPChains(t *testing.T) {
|
2015-03-06 01:43:14 -05:00
|
|
|
// Create a test bridge with a basic bridge configuration (name + IPv4).
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-11 21:12:00 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
driverconfig := &configuration{
|
|
|
|
EnableIPTables: true,
|
|
|
|
}
|
|
|
|
d := &driver{
|
|
|
|
config: driverconfig,
|
|
|
|
}
|
|
|
|
assertChainConfig(d, t)
|
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
config := getBasicTestConfig()
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 01:25:42 -04:00
|
|
|
createTestBridge(config, br, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
assertBridgeConfig(config, br, d, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
config.EnableIPMasquerade = true
|
2015-06-11 21:12:00 -04:00
|
|
|
assertBridgeConfig(config, br, d, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
config.EnableICC = true
|
2015-06-11 21:12:00 -04:00
|
|
|
assertBridgeConfig(config, br, d, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
config.EnableIPMasquerade = false
|
2015-06-11 21:12:00 -04:00
|
|
|
assertBridgeConfig(config, br, d, t)
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func getBasicTestConfig() *networkConfiguration {
|
|
|
|
config := &networkConfiguration{
|
2015-04-15 01:25:42 -04:00
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
AddressIPv4: &net.IPNet{IP: net.ParseIP(iptablesTestBridgeIP), Mask: net.CIDRMask(16, 32)}}
|
|
|
|
return config
|
2015-03-06 01:43:14 -05:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func createTestBridge(config *networkConfiguration, br *bridgeInterface, t *testing.T) {
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to create the testing Bridge: %s", err.Error())
|
|
|
|
}
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := setupBridgeIPv4(config, br); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to bring up the testing Bridge: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert base function which pushes iptables chain rules on insertion and removal.
|
2015-04-10 14:59:05 -04:00
|
|
|
func assertIPTableChainProgramming(rule iptRule, descr string, t *testing.T) {
|
2015-03-06 01:43:14 -05:00
|
|
|
// Add
|
2015-04-10 14:59:05 -04:00
|
|
|
if err := programChainRule(rule, descr, true); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to program iptable rule %s: %s", descr, err.Error())
|
|
|
|
}
|
2015-04-10 14:59:05 -04:00
|
|
|
if iptables.Exists(rule.table, rule.chain, rule.args...) == false {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to effectively program iptable rule: %s", descr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove
|
2015-04-10 14:59:05 -04:00
|
|
|
if err := programChainRule(rule, descr, false); err != nil {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to remove iptable rule %s: %s", descr, err.Error())
|
|
|
|
}
|
2015-04-10 14:59:05 -04:00
|
|
|
if iptables.Exists(rule.table, rule.chain, rule.args...) == true {
|
2015-03-06 01:43:14 -05:00
|
|
|
t.Fatalf("Failed to effectively remove iptable rule: %s", descr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
// Assert function which create chains.
|
|
|
|
func assertChainConfig(d *driver, t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2015-12-15 00:57:56 -05:00
|
|
|
d.natChain, d.filterChain, d.isolationChain, err = setupIPChains(d.config)
|
2015-06-11 21:12:00 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 01:43:14 -05:00
|
|
|
// Assert function which pushes chains based on bridge config parameters.
|
2015-06-11 21:12:00 -04:00
|
|
|
func assertBridgeConfig(config *networkConfiguration, br *bridgeInterface, d *driver, t *testing.T) {
|
|
|
|
nw := bridgeNetwork{portMapper: portmapper.New(),
|
|
|
|
config: config}
|
|
|
|
nw.driver = d
|
|
|
|
|
2015-03-06 01:43:14 -05:00
|
|
|
// Attempt programming of ip tables.
|
2015-06-01 16:14:29 -04:00
|
|
|
err := nw.setupIPTables(config, br)
|
2015-03-06 01:43:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
}
|