| 1 | // Copyright 2015 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 ipam |
| 16 | |
| 17 | import ( |
| 18 | "net" |
| 19 | "syscall" |
| 20 | |
| 21 | . "github.com/onsi/ginkgo/v2" |
| 22 | . "github.com/onsi/gomega" |
| 23 | "github.com/vishvananda/netlink" |
| 24 | |
| 25 | "github.com/containernetworking/cni/pkg/types" |
| 26 | current "github.com/containernetworking/cni/pkg/types/100" |
| 27 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 28 | "github.com/containernetworking/plugins/pkg/ns" |
| 29 | "github.com/containernetworking/plugins/pkg/testutils" |
| 30 | ) |
| 31 | |
| 32 | const LINK_NAME = "eth0" |
| 33 | |
| 34 | func ipNetEqual(a, b *net.IPNet) bool { |
| 35 | aPrefix, aBits := a.Mask.Size() |
| 36 | bPrefix, bBits := b.Mask.Size() |
| 37 | if aPrefix != bPrefix || aBits != bBits { |
| 38 | return false |
| 39 | } |
| 40 | return a.IP.Equal(b.IP) |
| 41 | } |
| 42 | |
| 43 | var _ = Describe("ConfigureIface", func() { |
| 44 | var originalNS ns.NetNS |
| 45 | var ipv4, ipv6, routev4, routev6, routev4Scope *net.IPNet |
| 46 | var ipgw4, ipgw6, routegwv4, routegwv6 net.IP |
| 47 | var routeScope int |
| 48 | var result *current.Result |
| 49 | var routeTable int |
| 50 | |
| 51 | BeforeEach(func() { |
| 52 | // Create a new NetNS so we don't modify the host |
| 53 | var err error |
| 54 | originalNS, err = testutils.NewNS() |
| 55 | Expect(err).NotTo(HaveOccurred()) |
| 56 | |
| 57 | err = originalNS.Do(func(ns.NetNS) error { |
| 58 | defer GinkgoRecover() |
| 59 | |
| 60 | linkAttrs := netlink.NewLinkAttrs() |
| 61 | linkAttrs.Name = LINK_NAME |
| 62 | |
| 63 | // Add master |
| 64 | err = netlink.LinkAdd(&netlink.Dummy{ |
| 65 | LinkAttrs: linkAttrs, |
| 66 | }) |
| 67 | Expect(err).NotTo(HaveOccurred()) |
| 68 | _, err = netlinksafe.LinkByName(LINK_NAME) |
| 69 | Expect(err).NotTo(HaveOccurred()) |
| 70 | return nil |
| 71 | }) |
| 72 | Expect(err).NotTo(HaveOccurred()) |
| 73 | |
| 74 | ipv4, err = types.ParseCIDR("1.2.3.30/24") |
| 75 | Expect(err).NotTo(HaveOccurred()) |
| 76 | Expect(ipv4).NotTo(BeNil()) |
| 77 | |
| 78 | _, routev4, err = net.ParseCIDR("15.5.6.8/24") |
| 79 | Expect(err).NotTo(HaveOccurred()) |
| 80 | Expect(routev4).NotTo(BeNil()) |
| 81 | routegwv4 = net.ParseIP("1.2.3.5") |
| 82 | Expect(routegwv4).NotTo(BeNil()) |
| 83 | |
| 84 | _, routev4Scope, err = net.ParseCIDR("1.2.3.4/32") |
| 85 | Expect(err).NotTo(HaveOccurred()) |
| 86 | Expect(routev4Scope).NotTo(BeNil()) |
| 87 | |
| 88 | ipgw4 = net.ParseIP("1.2.3.1") |
| 89 | Expect(ipgw4).NotTo(BeNil()) |
| 90 | |
| 91 | ipv6, err = types.ParseCIDR("abcd:1234:ffff::cdde/64") |
| 92 | Expect(err).NotTo(HaveOccurred()) |
| 93 | Expect(ipv6).NotTo(BeNil()) |
| 94 | |
| 95 | _, routev6, err = net.ParseCIDR("1111:dddd::aaaa/80") |
| 96 | Expect(err).NotTo(HaveOccurred()) |
| 97 | Expect(routev6).NotTo(BeNil()) |
| 98 | routegwv6 = net.ParseIP("abcd:1234:ffff::10") |
| 99 | Expect(routegwv6).NotTo(BeNil()) |
| 100 | |
| 101 | ipgw6 = net.ParseIP("abcd:1234:ffff::1") |
| 102 | Expect(ipgw6).NotTo(BeNil()) |
| 103 | |
| 104 | routeTable := 5000 |
| 105 | routeScope = 200 |
| 106 | |
| 107 | result = ¤t.Result{ |
| 108 | Interfaces: []*current.Interface{ |
| 109 | { |
| 110 | Name: "eth0", |
| 111 | Mac: "00:11:22:33:44:55", |
| 112 | Sandbox: "/proc/3553/ns/net", |
| 113 | }, |
| 114 | { |
| 115 | Name: "fake0", |
| 116 | Mac: "00:33:44:55:66:77", |
| 117 | Sandbox: "/proc/1234/ns/net", |
| 118 | }, |
| 119 | }, |
| 120 | IPs: []*current.IPConfig{ |
| 121 | { |
| 122 | Interface: current.Int(0), |
| 123 | Address: *ipv4, |
| 124 | Gateway: ipgw4, |
| 125 | }, |
| 126 | { |
| 127 | Interface: current.Int(0), |
| 128 | Address: *ipv6, |
| 129 | Gateway: ipgw6, |
| 130 | }, |
| 131 | }, |
| 132 | Routes: []*types.Route{ |
| 133 | {Dst: *routev4, GW: routegwv4}, |
| 134 | {Dst: *routev6, GW: routegwv6}, |
| 135 | {Dst: *routev4, GW: routegwv4, Table: &routeTable}, |
| 136 | {Dst: *routev4Scope, Scope: &routeScope}, |
| 137 | }, |
| 138 | } |
| 139 | }) |
| 140 | |
| 141 | AfterEach(func() { |
| 142 | Expect(originalNS.Close()).To(Succeed()) |
| 143 | }) |
| 144 | |
| 145 | It("configures a link with addresses and routes", func() { |
| 146 | err := originalNS.Do(func(ns.NetNS) error { |
| 147 | defer GinkgoRecover() |
| 148 | |
| 149 | err := ConfigureIface(LINK_NAME, result) |
| 150 | Expect(err).NotTo(HaveOccurred()) |
| 151 | |
| 152 | link, err := netlinksafe.LinkByName(LINK_NAME) |
| 153 | Expect(err).NotTo(HaveOccurred()) |
| 154 | Expect(link.Attrs().Name).To(Equal(LINK_NAME)) |
| 155 | |
| 156 | v4addrs, err := netlinksafe.AddrList(link, syscall.AF_INET) |
| 157 | Expect(err).NotTo(HaveOccurred()) |
| 158 | Expect(v4addrs).To(HaveLen(1)) |
| 159 | Expect(ipNetEqual(v4addrs[0].IPNet, ipv4)).To(BeTrue()) |
| 160 | |
| 161 | v6addrs, err := netlinksafe.AddrList(link, syscall.AF_INET6) |
| 162 | Expect(err).NotTo(HaveOccurred()) |
| 163 | Expect(v6addrs).To(HaveLen(2)) |
| 164 | |
| 165 | var found bool |
| 166 | for _, a := range v6addrs { |
| 167 | if ipNetEqual(a.IPNet, ipv6) { |
| 168 | found = true |
| 169 | break |
| 170 | } |
| 171 | } |
| 172 | Expect(found).To(BeTrue()) |
| 173 | |
| 174 | // Ensure the v4 route, v6 route, and subnet route |
| 175 | routes, err := netlinksafe.RouteList(link, 0) |
| 176 | Expect(err).NotTo(HaveOccurred()) |
| 177 | |
| 178 | var v4found, v6found, v4Scopefound bool |
| 179 | for _, route := range routes { |
| 180 | isv4 := route.Dst.IP.To4() != nil |
| 181 | if isv4 && ipNetEqual(route.Dst, routev4) && route.Gw.Equal(routegwv4) { |
| 182 | v4found = true |
| 183 | } |
| 184 | if !isv4 && ipNetEqual(route.Dst, routev6) && route.Gw.Equal(routegwv6) { |
| 185 | v6found = true |
| 186 | } |
| 187 | if isv4 && ipNetEqual(route.Dst, routev4Scope) && int(route.Scope) == routeScope { |
| 188 | v4Scopefound = true |
| 189 | } |
| 190 | |
| 191 | if v4found && v6found && v4Scopefound { |
| 192 | break |
| 193 | } |
| 194 | } |
| 195 | Expect(v4found).To(BeTrue()) |
| 196 | Expect(v6found).To(BeTrue()) |
| 197 | Expect(v4Scopefound).To(BeTrue()) |
| 198 | |
| 199 | return nil |
| 200 | }) |
| 201 | Expect(err).NotTo(HaveOccurred()) |
| 202 | }) |
| 203 | |
| 204 | It("keeps IPV6 addresses after the interface is brought down", func() { |
| 205 | err := originalNS.Do(func(ns.NetNS) error { |
| 206 | defer GinkgoRecover() |
| 207 | |
| 208 | By("Configuring the interface") |
| 209 | |
| 210 | err := ConfigureIface(LINK_NAME, result) |
| 211 | Expect(err).NotTo(HaveOccurred()) |
| 212 | |
| 213 | By("Verifying the IPV6 address is present") |
| 214 | |
| 215 | link, err := netlinksafe.LinkByName(LINK_NAME) |
| 216 | Expect(err).NotTo(HaveOccurred()) |
| 217 | Expect(link.Attrs().Name).To(Equal(LINK_NAME)) |
| 218 | |
| 219 | v6addrs, err := netlinksafe.AddrList(link, syscall.AF_INET6) |
| 220 | Expect(err).NotTo(HaveOccurred()) |
| 221 | Expect(v6addrs).To(HaveLen(2)) |
| 222 | |
| 223 | var found bool |
| 224 | for _, a := range v6addrs { |
| 225 | if ipNetEqual(a.IPNet, ipv6) { |
| 226 | found = true |
| 227 | break |
| 228 | } |
| 229 | } |
| 230 | Expect(found).To(BeTrue()) |
| 231 | |
| 232 | By("Bringing the interface down") |
| 233 | err = netlink.LinkSetDown(link) |
| 234 | Expect(err).NotTo(HaveOccurred()) |
| 235 | |
| 236 | By("Verifying the IPV6 address is still present") |
| 237 | v6addrs, err = netlinksafe.AddrList(link, syscall.AF_INET6) |
| 238 | Expect(err).NotTo(HaveOccurred()) |
| 239 | Expect(v6addrs).To(HaveLen(1)) |
| 240 | Expect(ipNetEqual(v6addrs[0].IPNet, ipv6)).To(BeTrue()) |
| 241 | |
| 242 | return nil |
| 243 | }) |
| 244 | Expect(err).NotTo(HaveOccurred()) |
| 245 | }) |
| 246 | |
| 247 | It("configures a link with routes using address gateways", func() { |
| 248 | result.Routes[0].GW = nil |
| 249 | result.Routes[1].GW = nil |
| 250 | err := originalNS.Do(func(ns.NetNS) error { |
| 251 | defer GinkgoRecover() |
| 252 | |
| 253 | err := ConfigureIface(LINK_NAME, result) |
| 254 | Expect(err).NotTo(HaveOccurred()) |
| 255 | |
| 256 | link, err := netlinksafe.LinkByName(LINK_NAME) |
| 257 | Expect(err).NotTo(HaveOccurred()) |
| 258 | Expect(link.Attrs().Name).To(Equal(LINK_NAME)) |
| 259 | |
| 260 | // Ensure the v4 route, v6 route, and subnet route |
| 261 | routes, err := netlinksafe.RouteList(link, 0) |
| 262 | Expect(err).NotTo(HaveOccurred()) |
| 263 | |
| 264 | var v4found, v6found, v4Tablefound bool |
| 265 | for _, route := range routes { |
| 266 | isv4 := route.Dst.IP.To4() != nil |
| 267 | if isv4 && ipNetEqual(route.Dst, routev4) && route.Gw.Equal(ipgw4) { |
| 268 | v4found = true |
| 269 | } |
| 270 | if !isv4 && ipNetEqual(route.Dst, routev6) && route.Gw.Equal(ipgw6) { |
| 271 | v6found = true |
| 272 | } |
| 273 | |
| 274 | if v4found && v6found { |
| 275 | break |
| 276 | } |
| 277 | } |
| 278 | Expect(v4found).To(BeTrue()) |
| 279 | Expect(v6found).To(BeTrue()) |
| 280 | |
| 281 | // Need to read all tables, so cannot use RouteList |
| 282 | routeFilter := &netlink.Route{ |
| 283 | Table: routeTable, |
| 284 | } |
| 285 | |
| 286 | routes, err = netlinksafe.RouteListFiltered(netlink.FAMILY_ALL, |
| 287 | routeFilter, |
| 288 | netlink.RT_FILTER_TABLE) |
| 289 | Expect(err).NotTo(HaveOccurred()) |
| 290 | |
| 291 | for _, route := range routes { |
| 292 | isv4 := route.Dst.IP.To4() != nil |
| 293 | if isv4 && ipNetEqual(route.Dst, routev4) && route.Gw.Equal(ipgw4) { |
| 294 | v4Tablefound = true |
| 295 | } |
| 296 | |
| 297 | if v4Tablefound { |
| 298 | break |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | Expect(v4Tablefound).To(BeTrue()) |
| 303 | |
| 304 | return nil |
| 305 | }) |
| 306 | Expect(err).NotTo(HaveOccurred()) |
| 307 | }) |
| 308 | |
| 309 | It("returns an error when the interface index doesn't match the link name", func() { |
| 310 | result.IPs[0].Interface = current.Int(1) |
| 311 | err := originalNS.Do(func(ns.NetNS) error { |
| 312 | return ConfigureIface(LINK_NAME, result) |
| 313 | }) |
| 314 | Expect(err).To(HaveOccurred()) |
| 315 | }) |
| 316 | |
| 317 | It("returns an error when the interface index is too big", func() { |
| 318 | result.IPs[0].Interface = current.Int(2) |
| 319 | err := originalNS.Do(func(ns.NetNS) error { |
| 320 | return ConfigureIface(LINK_NAME, result) |
| 321 | }) |
| 322 | Expect(err).To(HaveOccurred()) |
| 323 | }) |
| 324 | |
| 325 | It("returns an error when the interface index is too small", func() { |
| 326 | result.IPs[0].Interface = current.Int(-1) |
| 327 | err := originalNS.Do(func(ns.NetNS) error { |
| 328 | return ConfigureIface(LINK_NAME, result) |
| 329 | }) |
| 330 | Expect(err).To(HaveOccurred()) |
| 331 | }) |
| 332 | |
| 333 | It("returns an error when there are no interfaces to configure", func() { |
| 334 | result.Interfaces = []*current.Interface{} |
| 335 | err := originalNS.Do(func(ns.NetNS) error { |
| 336 | return ConfigureIface(LINK_NAME, result) |
| 337 | }) |
| 338 | Expect(err).To(HaveOccurred()) |
| 339 | }) |
| 340 | |
| 341 | It("returns an error when configuring the wrong interface", func() { |
| 342 | err := originalNS.Do(func(ns.NetNS) error { |
| 343 | return ConfigureIface("asdfasdf", result) |
| 344 | }) |
| 345 | Expect(err).To(HaveOccurred()) |
| 346 | }) |
| 347 | |
| 348 | It("does not panic when interface is not specified", func() { |
| 349 | result = ¤t.Result{ |
| 350 | Interfaces: []*current.Interface{ |
| 351 | { |
| 352 | Name: "eth0", |
| 353 | Mac: "00:11:22:33:44:55", |
| 354 | Sandbox: "/proc/3553/ns/net", |
| 355 | }, |
| 356 | { |
| 357 | Name: "fake0", |
| 358 | Mac: "00:33:44:55:66:77", |
| 359 | Sandbox: "/proc/1234/ns/net", |
| 360 | }, |
| 361 | }, |
| 362 | IPs: []*current.IPConfig{ |
| 363 | { |
| 364 | Address: *ipv4, |
| 365 | Gateway: ipgw4, |
| 366 | }, |
| 367 | { |
| 368 | Address: *ipv6, |
| 369 | Gateway: ipgw6, |
| 370 | }, |
| 371 | }, |
| 372 | } |
| 373 | err := originalNS.Do(func(ns.NetNS) error { |
| 374 | return ConfigureIface(LINK_NAME, result) |
| 375 | }) |
| 376 | Expect(err).NotTo(HaveOccurred()) |
| 377 | }) |
| 378 | }) |