Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use skip-subnets setting for outbound network traffic #336

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func (fc FirewallConfiguration) addOutgoingTrafficRules(existingRules []byte, co

// Ignore loopback
commands = append(commands, fc.makeIgnoreLoopback(outputChainName, "ignore-loopback"))
// Ignore subnets
commands = fc.addRulesForIgnoredSubnets(outputChainName, "d", commands)
// Ignore ports
commands = fc.addRulesForIgnoredPorts(fc.OutboundPortsToIgnore, outputChainName, commands)

Expand All @@ -145,7 +147,7 @@ func (fc FirewallConfiguration) addIncomingTrafficRules(existingRules []byte, co
commands = append(commands, fc.makeFlushChain(redirectChainName))
}
commands = fc.addRulesForIgnoredPorts(fc.InboundPortsToIgnore, redirectChainName, commands)
commands = fc.addRulesForIgnoredSubnets(redirectChainName, commands)
commands = fc.addRulesForIgnoredSubnets(redirectChainName, "s", commands)
commands = fc.addRulesForInboundPortRedirect(redirectChainName, commands)

if preroutingRuleRegex.Find(existingRules) == nil {
Expand Down Expand Up @@ -191,9 +193,9 @@ func (fc FirewallConfiguration) addRulesForIgnoredPorts(portsToIgnore []string,
return commands
}

func (fc FirewallConfiguration) addRulesForIgnoredSubnets(chainName string, commands []*exec.Cmd) []*exec.Cmd {
func (fc FirewallConfiguration) addRulesForIgnoredSubnets(chainName string, dFlag string, commands []*exec.Cmd) []*exec.Cmd {
for _, subnet := range fc.SubnetsToIgnore {
commands = append(commands, fc.makeIgnoreSubnet(chainName, subnet, fmt.Sprintf("ignore-subnet-%s", subnet)))
commands = append(commands, fc.makeIgnoreSubnet(chainName, subnet, dFlag, fmt.Sprintf("ignore-subnet-%s", subnet)))
}
return commands
}
Expand Down Expand Up @@ -299,13 +301,13 @@ func (fc FirewallConfiguration) makeIgnorePorts(chainName string, destinations [
"--comment", formatComment(comment))
}

func (fc FirewallConfiguration) makeIgnoreSubnet(chainName string, subnet string, comment string) *exec.Cmd {
func (fc FirewallConfiguration) makeIgnoreSubnet(chainName string, subnet string, dFlag string, comment string) *exec.Cmd {
return exec.Command(fc.BinPath,
"-t", "nat",
"-A", chainName,
"-p", "all",
"-j", "RETURN",
"-s", subnet,
"-"+dFlag, subnet,
"-m", "comment",
"--comment", formatComment(comment))
}
Expand Down