Skip to content

Commit

Permalink
[client] Exclude loopback from NAT (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal authored Oct 16, 2024
1 parent cee9546 commit 8c8900b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/firewall/iptables/router_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,12 @@ func (r *router) removeNatRule(pair firewall.RouterPair) error {

func genRuleSpec(jump string, source, destination netip.Prefix, intf string, inverse bool) []string {
intdir := "-i"
lointdir := "-o"
if inverse {
intdir = "-o"
lointdir = "-i"
}
return []string{intdir, intf, "-s", source.String(), "-d", destination.String(), "-j", jump}
return []string{intdir, intf, "!", lointdir, "lo", "-s", source.String(), "-d", destination.String(), "-j", jump}
}

func genRouteFilteringRuleSpec(params routeFilteringRuleParams) []string {
Expand Down
15 changes: 15 additions & 0 deletions client/firewall/nftables/router_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,15 @@ func (r *router) addNatRule(pair firewall.RouterPair) error {
destExp := generateCIDRMatcherExpressions(false, pair.Destination)

dir := expr.MetaKeyIIFNAME
notDir := expr.MetaKeyOIFNAME
if pair.Inverse {
dir = expr.MetaKeyOIFNAME
notDir = expr.MetaKeyIIFNAME
}

lo := ifname("lo")
intf := ifname(r.wgIface.Name())

exprs := []expr.Any{
&expr.Meta{
Key: dir,
Expand All @@ -440,6 +444,17 @@ func (r *router) addNatRule(pair firewall.RouterPair) error {
Register: 1,
Data: intf,
},

// We need to exclude the loopback interface as this changes the ebpf proxy port
&expr.Meta{
Key: notDir,
Register: 1,
},
&expr.Cmp{
Op: expr.CmpOpNeq,
Register: 1,
Data: lo,
},
}

exprs = append(exprs, sourceExp...)
Expand Down
12 changes: 12 additions & 0 deletions client/firewall/nftables/router_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func TestNftablesManager_AddNatRule(t *testing.T) {
Register: 1,
Data: ifname(ifaceMock.Name()),
},
&expr.Meta{Key: expr.MetaKeyOIFNAME, Register: 1},
&expr.Cmp{
Op: expr.CmpOpNeq,
Register: 1,
Data: ifname("lo"),
},
)

natRuleKey := firewall.GenKey(firewall.NatFormat, testCase.InputPair)
Expand Down Expand Up @@ -97,6 +103,12 @@ func TestNftablesManager_AddNatRule(t *testing.T) {
Register: 1,
Data: ifname(ifaceMock.Name()),
},
&expr.Meta{Key: expr.MetaKeyIIFNAME, Register: 1},
&expr.Cmp{
Op: expr.CmpOpNeq,
Register: 1,
Data: ifname("lo"),
},
)

inNatRuleKey := firewall.GenKey(firewall.NatFormat, firewall.GetInversePair(testCase.InputPair))
Expand Down

0 comments on commit 8c8900b

Please sign in to comment.