Skip to content
Draft
Show file tree
Hide file tree
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
84 changes: 68 additions & 16 deletions pkg/nftables/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nftables

import (
"fmt"
"net/netip"
"strconv"
"strings"

Expand Down Expand Up @@ -72,6 +73,11 @@ func clusterwideNetworkPolicyEgressDNSCacheRules(cache FQDNCache, logAcceptedCon
}, nil
}

var (
ipv4any = netip.MustParsePrefix("0.0.0.0/0")
ipv6any = netip.MustParsePrefix("::/0")
)

func clusterwideNetworkPolicyEgressRules(
cache FQDNCache,
np firewallv1.ClusterwideNetworkPolicy,
Expand All @@ -82,17 +88,7 @@ func clusterwideNetworkPolicyEgressRules(
tcpPorts, udpPorts := calculatePorts(e.Ports)
ruleBases := []ruleBase{}
if len(e.To) > 0 {
allow, except := clusterwideNetworkPolicyEgressToRules(e)
rb := []string{"ip saddr == @cluster_prefixes"}
if len(except) > 0 {
rb = append(rb, fmt.Sprintf("ip daddr != { %s }", strings.Join(except, ", ")))
}
if len(allow) > 0 {
if allow[0] != "0.0.0.0/0" {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is only the first allow entry considered ??

rb = append(rb, fmt.Sprintf("ip daddr { %s }", strings.Join(allow, ", ")))
}
}
ruleBases = append(ruleBases, ruleBase{base: rb})
ruleBases = append(ruleBases, clusterwideNetworkPolicyEgressToRules(e)...)
} else if len(e.ToFQDNs) > 0 && cache.IsInitialized() {
rbs, u := clusterwideNetworkPolicyEgressToFQDNRules(cache, fqdnState, e)
ruleBases = append(ruleBases, rbs...)
Expand All @@ -114,13 +110,69 @@ func clusterwideNetworkPolicyEgressRules(
return uniqueSorted(rules), np
}

func clusterwideNetworkPolicyEgressToRules(e firewallv1.EgressRule) (allow, except []string) {
// clusterwideNetworkPolicyEgressToRules assembles the rule bases for an egress
// rule with IPBlock destinations. IPv4 and IPv6 destinations are kept separate
// so that the correct address family keyword (ip/ip6) is used for the
// destination match. A rule base is emitted for each family that is present.
// If a family contains its "any" prefix (0.0.0.0/0 or ::/0), no destination set
// is emitted for that family, but its exceptions are still honoured.
func clusterwideNetworkPolicyEgressToRules(e firewallv1.EgressRule) (rules []ruleBase) {
var (
v4Allow, v6Allow []string
v4Except, v6Except []string
v4Any, v6Any bool
)
for _, ipBlock := range e.To {
allow = append(allow, ipBlock.CIDR)
except = append(except, ipBlock.Except...)
// contains only already validated cidrs
prefix := netip.MustParsePrefix(ipBlock.CIDR)
if prefix.Addr().Is4() {
if prefix == ipv4any {
v4Any = true
} else {
v4Allow = append(v4Allow, ipBlock.CIDR)
}
v4Except = append(v4Except, ipBlock.Except...)
continue
}
if prefix == ipv6any {
v6Any = true
} else {
v6Allow = append(v6Allow, ipBlock.CIDR)
}
v6Except = append(v6Except, ipBlock.Except...)
}

return
if v4Any || len(v4Allow) > 0 {
rules = append(rules, ruleBase{base: egressDestinationBase("ip", v4Any, v4Allow, v4Except)})
}
if v6Any || len(v6Allow) > 0 {
rules = append(rules, ruleBase{base: egressDestinationBase("ip6", v6Any, v6Allow, v6Except)})
}

return rules
}

// egressDestinationBase builds the match part of an egress destination rule for
// a single address family. The destination set is omitted when the family's
// "any" prefix is allowed.
func egressDestinationBase(family string, anyDestination bool, allow, except []string) []string {
base := []string{clusterPrefixMatch(family)}
if len(except) > 0 {
base = append(base, fmt.Sprintf("%s daddr != { %s }", family, strings.Join(except, ", ")))
}
if !anyDestination && len(allow) > 0 {
base = append(base, fmt.Sprintf("%s daddr { %s }", family, strings.Join(allow, ", ")))
}
return base
}

// clusterPrefixMatch returns the match against the cluster prefixes of the
// given address family.
func clusterPrefixMatch(family string) string {
if family == string(firewallv1.IPv6) {
return "ip6 saddr == @cluster_prefixes6"
}
return "ip saddr == @cluster_prefixes"
}

func clusterwideNetworkPolicyEgressToFQDNRules(
Expand All @@ -140,7 +192,7 @@ func clusterwideNetworkPolicyEgressToFQDNRules(

fqdnState[fqdnName] = cache.GetSetsForFQDN(fqdn)
for _, set := range fqdnState[fqdnName] {
rb := []string{"ip saddr == @cluster_prefixes"}
rb := []string{clusterPrefixMatch(string(set.Version))}
rb = append(rb, fmt.Sprintf(string(set.Version)+" daddr @%s", set.SetName))
rules = append(rules, ruleBase{comment: fmt.Sprintf(", fqdn: %s", fqdn.GetName()), base: rb})
}
Expand Down
140 changes: 138 additions & 2 deletions pkg/nftables/networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,144 @@ func TestClusterwideNetworkPolicyEgressRules(t *testing.T) {
egress: nftablesRules{
`ip saddr == @cluster_prefixes ip daddr @test tcp dport { 53 } counter accept comment "accept traffic for np tcp, fqdn: test.com"`,
`ip saddr == @cluster_prefixes ip daddr @test udp dport { 53 } counter accept comment "accept traffic for np udp, fqdn: test.com"`,
`ip saddr == @cluster_prefixes ip6 daddr @test2 tcp dport { 53 } counter accept comment "accept traffic for np tcp, fqdn: *.test.com"`,
`ip saddr == @cluster_prefixes ip6 daddr @test2 udp dport { 53 } counter accept comment "accept traffic for np udp, fqdn: *.test.com"`,
`ip6 saddr == @cluster_prefixes6 ip6 daddr @test2 tcp dport { 53 } counter accept comment "accept traffic for np tcp, fqdn: *.test.com"`,
`ip6 saddr == @cluster_prefixes6 ip6 daddr @test2 udp dport { 53 } counter accept comment "accept traffic for np udp, fqdn: *.test.com"`,
},
},
},

{
name: "allow to any ipv4",
input: firewallv1.ClusterwideNetworkPolicy{
Spec: firewallv1.PolicySpec{
Egress: []firewallv1.EgressRule{
{
To: []networking.IPBlock{
{
CIDR: "0.0.0.0/0",
Except: []string{"1.1.0.1"},
},
{
CIDR: "1.1.0.0/24",
},
},
Ports: []firewallv1.NetworkPolicyPort{
{
Protocol: &tcp,
Port: int32(443),
},
},
},
},
},
},
record: func(cache *mocks.FQDNCache) {},
want: want{
// an IPv4 any destination must not be added as a daddr set
egress: nftablesRules{
`ip saddr == @cluster_prefixes ip daddr != { 1.1.0.1 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
egressAL: nftablesRules{
`ip saddr == @cluster_prefixes ip daddr != { 1.1.0.1 } tcp dport { 443 } log prefix "nftables-firewall-accepted: " limit rate 10/second` + "\n" + `ip saddr == @cluster_prefixes ip daddr != { 1.1.0.1 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
},
},
{
name: "allow to any ipv6",
input: firewallv1.ClusterwideNetworkPolicy{
Spec: firewallv1.PolicySpec{
Egress: []firewallv1.EgressRule{
{
To: []networking.IPBlock{
{
CIDR: "::/0",
Except: []string{"2001:db8::/32"},
},
},
Ports: []firewallv1.NetworkPolicyPort{
{
Protocol: &tcp,
Port: int32(443),
},
},
},
},
},
},
record: func(cache *mocks.FQDNCache) {},
want: want{
// an IPv6 any destination must not be added as a daddr set, and
// the exception must use the ip6 keyword
egress: nftablesRules{
`ip6 saddr == @cluster_prefixes6 ip6 daddr != { 2001:db8::/32 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
egressAL: nftablesRules{
`ip6 saddr == @cluster_prefixes6 ip6 daddr != { 2001:db8::/32 } tcp dport { 443 } log prefix "nftables-firewall-accepted: " limit rate 10/second` + "\n" + `ip6 saddr == @cluster_prefixes6 ip6 daddr != { 2001:db8::/32 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
},
},
{
name: "multiple ipv6 ip blocks with exception",
input: firewallv1.ClusterwideNetworkPolicy{
Spec: firewallv1.PolicySpec{
Egress: []firewallv1.EgressRule{
{
To: []networking.IPBlock{
{
CIDR: "2001:db8::/32",
Except: []string{"2001:db8:1::1"},
},
{
CIDR: "2001:db9::/32",
},
},
Ports: []firewallv1.NetworkPolicyPort{
{
Protocol: &tcp,
Port: int32(443),
},
},
},
},
},
},
record: func(cache *mocks.FQDNCache) {},
want: want{
egress: nftablesRules{
`ip6 saddr == @cluster_prefixes6 ip6 daddr != { 2001:db8:1::1 } ip6 daddr { 2001:db8::/32, 2001:db9::/32 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
},
},
{
name: "mixed ipv4 and ipv6 destinations",
input: firewallv1.ClusterwideNetworkPolicy{
Spec: firewallv1.PolicySpec{
Egress: []firewallv1.EgressRule{
{
To: []networking.IPBlock{
{
CIDR: "1.1.0.0/24",
},
{
CIDR: "2001:db8::/32",
},
},
Ports: []firewallv1.NetworkPolicyPort{
{
Protocol: &tcp,
Port: int32(443),
},
},
},
},
},
},
record: func(cache *mocks.FQDNCache) {},
want: want{
// one rule per address family, each with the correct keyword
egress: nftablesRules{
`ip saddr == @cluster_prefixes ip daddr { 1.1.0.0/24 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
`ip6 saddr == @cluster_prefixes6 ip6 daddr { 2001:db8::/32 } tcp dport { 443 } counter accept comment "accept traffic for np tcp"`,
},
},
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/nftables/nftables.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ table inet firewall {
auto-merge
elements = { 10.0.0.0/8 }
}

# IPv6 prefixes in the cluster, typically fd00::/8
# FIXME Should be filled with nodeCidr
set cluster_prefixes6 {
type ipv6_addr
flags interval
auto-merge
elements = { fd00::/8 }
}
{{- range .Sets }}

set {{ .SetName }} {
Expand Down
9 changes: 9 additions & 0 deletions pkg/nftables/test_data/more-rules.nftable.v4
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ table inet firewall {
elements = { 10.0.0.0/8 }
}

# IPv6 prefixes in the cluster, typically fd00::/8
# FIXME Should be filled with nodeCidr
set cluster_prefixes6 {
type ipv6_addr
flags interval
auto-merge
elements = { fd00::/8 }
}

# counters
counter internal_in { }
counter internal_out { }
Expand Down
9 changes: 9 additions & 0 deletions pkg/nftables/test_data/sets.nftable.v4
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ table inet firewall {
elements = { 10.0.0.0/8 }
}

# IPv6 prefixes in the cluster, typically fd00::/8
# FIXME Should be filled with nodeCidr
set cluster_prefixes6 {
type ipv6_addr
flags interval
auto-merge
elements = { fd00::/8 }
}

set test {
type ipv4_addr

Expand Down
9 changes: 9 additions & 0 deletions pkg/nftables/test_data/simple.nftable.v4
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ table inet firewall {
elements = { 10.0.0.0/8 }
}

# IPv6 prefixes in the cluster, typically fd00::/8
# FIXME Should be filled with nodeCidr
set cluster_prefixes6 {
type ipv6_addr
flags interval
auto-merge
elements = { fd00::/8 }
}

# counters
counter internal_in { }
counter internal_out { }
Expand Down
9 changes: 9 additions & 0 deletions pkg/nftables/test_data/validated.nftable.v4
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ table inet firewall {
elements = { 10.0.0.0/8 }
}

# IPv6 prefixes in the cluster, typically fd00::/8
# FIXME Should be filled with nodeCidr
set cluster_prefixes6 {
type ipv6_addr
flags interval
auto-merge
elements = { fd00::/8 }
}

# counters
counter internal_in { }
counter internal_out { }
Expand Down
Loading