|
@@ -22,15 +22,21 @@ func rawIPTables(args ...string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func chainExists(cname string) bool {
|
|
|
+ if err := rawIPTables("-L", cname); err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
func setupGlobalChain() {
|
|
|
if err := rawIPTables("-N", globalChain); err != nil {
|
|
|
- logrus.Errorf("could not create global overlay chain: %v", err)
|
|
|
- return
|
|
|
+ logrus.Debugf("could not create global overlay chain: %v", err)
|
|
|
}
|
|
|
|
|
|
if err := rawIPTables("-A", globalChain, "-j", "RETURN"); err != nil {
|
|
|
- logrus.Errorf("could not install default return chain in the overlay global chain: %v", err)
|
|
|
- return
|
|
|
+ logrus.Debugf("could not install default return chain in the overlay global chain: %v", err)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -38,22 +44,28 @@ func setNetworkChain(cname string, remove bool) error {
|
|
|
// Initialize the onetime global overlay chain
|
|
|
filterOnce.Do(setupGlobalChain)
|
|
|
|
|
|
+ exists := chainExists(cname)
|
|
|
+
|
|
|
opt := "-N"
|
|
|
// In case of remove, make sure to flush the rules in the chain
|
|
|
- if remove {
|
|
|
+ if remove && exists {
|
|
|
if err := rawIPTables("-F", cname); err != nil {
|
|
|
return fmt.Errorf("failed to flush overlay network chain %s rules: %v", cname, err)
|
|
|
}
|
|
|
opt = "-X"
|
|
|
}
|
|
|
|
|
|
- if err := rawIPTables(opt, cname); err != nil {
|
|
|
- return fmt.Errorf("failed network chain operation %q for chain %s: %v", opt, cname, err)
|
|
|
+ if (!remove && !exists) || (remove && exists) {
|
|
|
+ if err := rawIPTables(opt, cname); err != nil {
|
|
|
+ return fmt.Errorf("failed network chain operation %q for chain %s: %v", opt, cname, err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if !remove {
|
|
|
- if err := rawIPTables("-A", cname, "-j", "DROP"); err != nil {
|
|
|
- return fmt.Errorf("failed adding default drop rule to overlay network chain %s: %v", cname, err)
|
|
|
+ if !iptables.Exists(iptables.Filter, cname, "-j", "DROP") {
|
|
|
+ if err := rawIPTables("-A", cname, "-j", "DROP"); err != nil {
|
|
|
+ return fmt.Errorf("failed adding default drop rule to overlay network chain %s: %v", cname, err)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|