Skip to content

Commit 086e374

Browse files
committed
fix(firewall/iptables): shared mutex for both iptables and ip6tables
1 parent 57cf276 commit 086e374

5 files changed

Lines changed: 7 additions & 18 deletions

File tree

internal/firewall/iptables/atomic.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import (
1111
// returns a restore function that can be called to restore the saved rules.
1212
func (c *Config) SaveAndRestore(ctx context.Context) (restore func(context.Context), err error) {
1313
c.iptablesMutex.Lock()
14-
c.ip6tablesMutex.Lock()
1514
defer c.iptablesMutex.Unlock()
16-
defer c.ip6tablesMutex.Unlock()
1715

1816
return c.saveAndRestore(ctx)
1917
}

internal/firewall/iptables/firewall.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
)
77

88
type Config struct {
9-
runner CmdRunner
10-
logger Logger
11-
iptablesMutex sync.Mutex
12-
ip6tablesMutex sync.Mutex
9+
runner CmdRunner
10+
logger Logger
11+
iptablesMutex sync.Mutex
1312

1413
// Fixed state
1514
ipTables string

internal/firewall/iptables/ip6tables.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func findIP6tablesSupported(ctx context.Context, runner CmdRunner) (
2424
}
2525

2626
func (c *Config) runIP6tablesInstructions(ctx context.Context, instructions []string) error {
27-
c.ip6tablesMutex.Lock() // only one ip6tables command at once
28-
defer c.ip6tablesMutex.Unlock()
27+
c.iptablesMutex.Lock() // only one iptables command at once
28+
defer c.iptablesMutex.Unlock()
2929

3030
restore, err := c.saveAndRestoreIPv6(ctx)
3131
if err != nil {
@@ -48,8 +48,8 @@ func (c *Config) runIP6tablesInstructionsNoSave(ctx context.Context, instruction
4848
}
4949

5050
func (c *Config) runIP6tablesInstruction(ctx context.Context, instruction string) error {
51-
c.ip6tablesMutex.Lock() // only one ip6tables command at once
52-
defer c.ip6tablesMutex.Unlock()
51+
c.iptablesMutex.Lock() // only one iptables command at once
52+
defer c.iptablesMutex.Unlock()
5353

5454
restore, err := c.saveAndRestoreIPv6(ctx)
5555
if err != nil {

internal/firewall/iptables/iptables.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,7 @@ func (c *Config) RedirectPort(ctx context.Context, intf string,
229229
}
230230

231231
c.iptablesMutex.Lock()
232-
c.ip6tablesMutex.Lock()
233232
defer c.iptablesMutex.Unlock()
234-
defer c.ip6tablesMutex.Unlock()
235233

236234
restore, err := c.saveAndRestore(ctx)
237235
if err != nil {
@@ -297,9 +295,7 @@ func (c *Config) RunUserPostRules(ctx context.Context, filepath string) error {
297295
lines := strings.Split(string(b), "\n")
298296

299297
c.iptablesMutex.Lock()
300-
c.ip6tablesMutex.Lock()
301298
defer c.iptablesMutex.Unlock()
302-
defer c.ip6tablesMutex.Unlock()
303299

304300
restore, err := c.saveAndRestore(ctx)
305301
if err != nil {

internal/firewall/iptables/iptablesmix.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66

77
func (c *Config) runMixedIptablesInstructions(ctx context.Context, instructions []string) error {
88
c.iptablesMutex.Lock()
9-
c.ip6tablesMutex.Lock()
109
defer c.iptablesMutex.Unlock()
11-
defer c.ip6tablesMutex.Unlock()
1210

1311
restore, err := c.saveAndRestore(ctx)
1412
if err != nil {
@@ -26,9 +24,7 @@ func (c *Config) runMixedIptablesInstructions(ctx context.Context, instructions
2624

2725
func (c *Config) runMixedIptablesInstruction(ctx context.Context, instruction string) error {
2826
c.iptablesMutex.Lock()
29-
c.ip6tablesMutex.Lock()
3027
defer c.iptablesMutex.Unlock()
31-
defer c.ip6tablesMutex.Unlock()
3228

3329
restore, err := c.saveAndRestore(ctx)
3430
if err != nil {

0 commit comments

Comments
 (0)