| 1 | // Copyright 2023 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ip |
| 16 | |
| 17 | import ( |
| 18 | "net" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/vishvananda/netlink" |
| 23 | "sigs.k8s.io/knftables" |
| 24 | |
| 25 | "github.com/containernetworking/cni/pkg/types" |
| 26 | ) |
| 27 | |
| 28 | func Test_setupIPMasqNFTables(t *testing.T) { |
| 29 | nft := knftables.NewFake(knftables.InetFamily, ipMasqTableName) |
| 30 | |
| 31 | containers := []struct { |
| 32 | network string |
| 33 | ifname string |
| 34 | containerID string |
| 35 | addrs []string |
| 36 | }{ |
| 37 | { |
| 38 | network: "unit-test", |
| 39 | ifname: "eth0", |
| 40 | containerID: "one", |
| 41 | addrs: []string{"192.168.1.1/24"}, |
| 42 | }, |
| 43 | { |
| 44 | network: "unit-test", |
| 45 | ifname: "eth0", |
| 46 | containerID: "two", |
| 47 | addrs: []string{"192.168.1.2/24", "2001:db8::2/64"}, |
| 48 | }, |
| 49 | { |
| 50 | network: "unit-test", |
| 51 | ifname: "eth0", |
| 52 | containerID: "three", |
| 53 | addrs: []string{"192.168.99.5/24"}, |
| 54 | }, |
| 55 | { |
| 56 | network: "alternate", |
| 57 | ifname: "net1", |
| 58 | containerID: "three", |
| 59 | addrs: []string{ |
| 60 | "10.0.0.5/24", |
| 61 | "10.0.0.6/24", |
| 62 | "10.0.1.7/24", |
| 63 | "2001:db8::5/64", |
| 64 | "2001:db8::6/64", |
| 65 | "2001:db8:1::7/64", |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | for _, c := range containers { |
| 71 | ipns := []*net.IPNet{} |
| 72 | for _, addr := range c.addrs { |
| 73 | nladdr, err := netlink.ParseAddr(addr) |
| 74 | if err != nil { |
| 75 | t.Fatalf("failed to parse test addr: %v", err) |
| 76 | } |
| 77 | ipns = append(ipns, nladdr.IPNet) |
| 78 | } |
| 79 | err := setupIPMasqNFTablesWithInterface(nft, ipns, c.network, c.ifname, c.containerID) |
| 80 | if err != nil { |
| 81 | t.Fatalf("error from setupIPMasqNFTables: %v", err) |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | expected := strings.TrimSpace(` |
| 87 | add table inet cni_plugins_masquerade { comment "Masquerading for plugins from github.com/containernetworking/plugins" ; } |
| 88 | add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; } |
| 89 | add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; } |
| 90 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-287fc69eff0574a2, net: unit-test, if: eth0, id: one" |
| 91 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 92 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 93 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three" |
| 94 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 95 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 96 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 97 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 98 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 99 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 100 | add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return |
| 101 | add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return |
| 102 | add rule inet cni_plugins_masquerade postrouting goto masq_checks |
| 103 | `) |
| 104 | dump := strings.TrimSpace(nft.Dump()) |
| 105 | if dump != expected { |
| 106 | t.Errorf("expected nftables state:\n%s\n\nactual:\n%s\n\n", expected, dump) |
| 107 | } |
| 108 | |
| 109 | // Add a new container reusing "one"'s address, before deleting "one" |
| 110 | c := containers[0] |
| 111 | addr, err := netlink.ParseAddr(c.addrs[0]) |
| 112 | if err != nil { |
| 113 | t.Fatalf("failed to parse test addr: %v", err) |
| 114 | } |
| 115 | err = setupIPMasqNFTablesWithInterface(nft, []*net.IPNet{addr.IPNet}, "unit-test", "eth0", "four") |
| 116 | if err != nil { |
| 117 | t.Fatalf("error from setupIPMasqNFTables: %v", err) |
| 118 | } |
| 119 | |
| 120 | // Remove "one" |
| 121 | err = teardownIPMasqNFTablesWithInterface(nft, []*net.IPNet{addr.IPNet}, c.network, c.ifname, c.containerID) |
| 122 | if err != nil { |
| 123 | t.Fatalf("error from teardownIPMasqNFTables: %v", err) |
| 124 | } |
| 125 | |
| 126 | // Check that "one" was deleted (and "four" wasn't) |
| 127 | expected = strings.TrimSpace(` |
| 128 | add table inet cni_plugins_masquerade { comment "Masquerading for plugins from github.com/containernetworking/plugins" ; } |
| 129 | add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; } |
| 130 | add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; } |
| 131 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 132 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 133 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three" |
| 134 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 135 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 136 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 137 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 138 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 139 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 140 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-e766de567ef6c543, net: unit-test, if: eth0, id: four" |
| 141 | add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return |
| 142 | add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return |
| 143 | add rule inet cni_plugins_masquerade postrouting goto masq_checks |
| 144 | `) |
| 145 | dump = strings.TrimSpace(nft.Dump()) |
| 146 | if dump != expected { |
| 147 | t.Errorf("expected nftables state:\n%s\n\nactual:\n%s\n\n", expected, dump) |
| 148 | } |
| 149 | |
| 150 | // GC "four" from the "unit-test" network |
| 151 | err = gcIPMasqNFTablesWithInterface(nft, "unit-test", []types.GCAttachment{ |
| 152 | {IfName: "eth0", ContainerID: "two"}, |
| 153 | {IfName: "eth0", ContainerID: "three"}, |
| 154 | // (irrelevant extra element) |
| 155 | {IfName: "eth0", ContainerID: "one"}, |
| 156 | }) |
| 157 | if err != nil { |
| 158 | t.Fatalf("error from gcIPMasqNFTables: %v", err) |
| 159 | } |
| 160 | // GC the "alternate" network without removing anything |
| 161 | err = gcIPMasqNFTablesWithInterface(nft, "alternate", []types.GCAttachment{ |
| 162 | {IfName: "net1", ContainerID: "three"}, |
| 163 | }) |
| 164 | if err != nil { |
| 165 | t.Fatalf("error from gcIPMasqNFTables: %v", err) |
| 166 | } |
| 167 | |
| 168 | // Re-dump |
| 169 | expected = strings.TrimSpace(` |
| 170 | add table inet cni_plugins_masquerade { comment "Masquerading for plugins from github.com/containernetworking/plugins" ; } |
| 171 | add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; } |
| 172 | add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; } |
| 173 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 174 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two" |
| 175 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three" |
| 176 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 177 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 178 | add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 179 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 180 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 181 | add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three" |
| 182 | add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return |
| 183 | add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return |
| 184 | add rule inet cni_plugins_masquerade postrouting goto masq_checks |
| 185 | `) |
| 186 | dump = strings.TrimSpace(nft.Dump()) |
| 187 | if dump != expected { |
| 188 | t.Errorf("expected nftables state:\n%s\n\nactual:\n%s\n\n", expected, dump) |
| 189 | } |
| 190 | |
| 191 | // GC everything |
| 192 | err = gcIPMasqNFTablesWithInterface(nft, "unit-test", []types.GCAttachment{}) |
| 193 | if err != nil { |
| 194 | t.Fatalf("error from gcIPMasqNFTables: %v", err) |
| 195 | } |
| 196 | err = gcIPMasqNFTablesWithInterface(nft, "alternate", []types.GCAttachment{}) |
| 197 | if err != nil { |
| 198 | t.Fatalf("error from gcIPMasqNFTables: %v", err) |
| 199 | } |
| 200 | |
| 201 | expected = strings.TrimSpace(` |
| 202 | add table inet cni_plugins_masquerade { comment "Masquerading for plugins from github.com/containernetworking/plugins" ; } |
| 203 | add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; } |
| 204 | add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; } |
| 205 | add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return |
| 206 | add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return |
| 207 | add rule inet cni_plugins_masquerade postrouting goto masq_checks |
| 208 | `) |
| 209 | dump = strings.TrimSpace(nft.Dump()) |
| 210 | if dump != expected { |
| 211 | t.Errorf("expected nftables state:\n%s\n\nactual:\n%s\n\n", expected, dump) |
| 212 | } |
| 213 | } |