Skip to content

Commit 0ff9bc1

Browse files
committed
Make sure the firewall rules are created even if the bridge interface is already created
1 parent 6344e6f commit 0ff9bc1

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

network.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,6 @@ func CreateBridgeIface(config *DaemonConfig) error {
167167
return fmt.Errorf("Unable to start network bridge: %s", err)
168168
}
169169

170-
if config.EnableIptables {
171-
// Enable NAT
172-
if output, err := iptables.Raw("-t", "nat", "-A", "POSTROUTING", "-s", ifaceAddr,
173-
"!", "-d", ifaceAddr, "-j", "MASQUERADE"); err != nil {
174-
return fmt.Errorf("Unable to enable network bridge NAT: %s", err)
175-
} else if len(output) != 0 {
176-
return fmt.Errorf("Error iptables postrouting: %s", output)
177-
}
178-
179-
// Accept incoming packets for existing connections
180-
if output, err := iptables.Raw("-I", "FORWARD", "-o", config.BridgeIface, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"); err != nil {
181-
return fmt.Errorf("Unable to allow incoming packets: %s", err)
182-
} else if len(output) != 0 {
183-
return fmt.Errorf("Error iptables allow incoming: %s", output)
184-
}
185-
186-
// Accept all non-intercontainer outgoing packets
187-
if output, err := iptables.Raw("-I", "FORWARD", "-i", config.BridgeIface, "!", "-o", config.BridgeIface, "-j", "ACCEPT"); err != nil {
188-
return fmt.Errorf("Unable to allow outgoing packets: %s", err)
189-
} else if len(output) != 0 {
190-
return fmt.Errorf("Error iptables allow outgoing: %s", output)
191-
}
192-
193-
}
194170
return nil
195171
}
196172

@@ -699,6 +675,40 @@ func newNetworkManager(config *DaemonConfig) (*NetworkManager, error) {
699675

700676
// Configure iptables for link support
701677
if config.EnableIptables {
678+
679+
// Enable NAT
680+
natArgs := []string{"POSTROUTING", "-t", "nat", "-s", addr.String(), "!", "-d", addr.String(), "-j", "MASQUERADE"}
681+
682+
if !iptables.Exists(natArgs...) {
683+
if output, err := iptables.Raw(append([]string{"-A"}, natArgs...)...); err != nil {
684+
return nil, fmt.Errorf("Unable to enable network bridge NAT: %s", err)
685+
} else if len(output) != 0 {
686+
return nil, fmt.Errorf("Error iptables postrouting: %s", output)
687+
}
688+
}
689+
690+
// Accept incoming packets for existing connections
691+
existingArgs := []string{"FORWARD", "-o", config.BridgeIface, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}
692+
693+
if !iptables.Exists(existingArgs...) {
694+
if output, err := iptables.Raw(append([]string{"-I"}, existingArgs...)...); err != nil {
695+
return nil, fmt.Errorf("Unable to allow incoming packets: %s", err)
696+
} else if len(output) != 0 {
697+
return nil, fmt.Errorf("Error iptables allow incoming: %s", output)
698+
}
699+
}
700+
701+
// Accept all non-intercontainer outgoing packets
702+
outgoingArgs := []string{"FORWARD", "-i", config.BridgeIface, "!", "-o", config.BridgeIface, "-j", "ACCEPT"}
703+
704+
if !iptables.Exists(outgoingArgs...) {
705+
if output, err := iptables.Raw(append([]string{"-I"}, outgoingArgs...)...); err != nil {
706+
return nil, fmt.Errorf("Unable to allow outgoing packets: %s", err)
707+
} else if len(output) != 0 {
708+
return nil, fmt.Errorf("Error iptables allow outgoing: %s", output)
709+
}
710+
}
711+
702712
args := []string{"FORWARD", "-i", config.BridgeIface, "-o", config.BridgeIface, "-j"}
703713
acceptArgs := append(args, "ACCEPT")
704714
dropArgs := append(args, "DROP")

0 commit comments

Comments
 (0)