From 663dcde32aefa06c1cfe4fa56f70c23c4ea45e24 Mon Sep 17 00:00:00 2001 From: Francesco Cheinasso Date: Fri, 7 Feb 2025 09:12:02 +0100 Subject: [PATCH] feat: reamap mask --- main.go | 56 ++++++++++++++ pkg/liqoctl/test/network/check/ip.go | 3 +- pkg/utils/ipam/mapping/ips.go | 55 ++++++++++---- pkg/utils/ipam/mapping/ips_suite_test.go | 27 +++++++ pkg/utils/ipam/mapping/ips_test.go | 93 ++++++++++++++++++++++++ 5 files changed, 219 insertions(+), 15 deletions(-) create mode 100644 main.go create mode 100644 pkg/utils/ipam/mapping/ips_suite_test.go create mode 100644 pkg/utils/ipam/mapping/ips_test.go diff --git a/main.go b/main.go new file mode 100644 index 0000000000..6083ac38a2 --- /dev/null +++ b/main.go @@ -0,0 +1,56 @@ +// Copyright 2019-2025 The Liqo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + + "github.com/liqotech/liqo/apis/networking/v1beta1" + "github.com/liqotech/liqo/pkg/utils/ipam/mapping" +) + +func main() { + cfg := v1beta1.Configuration{ + Spec: v1beta1.ConfigurationSpec{ + Local: &v1beta1.ClusterConfig{ + CIDR: v1beta1.ClusterConfigCIDR{ + Pod: []v1beta1.CIDR{"10.71.0.0/18"}, + External: []v1beta1.CIDR{"10.70.0.0/16"}, + }, + }, + Remote: v1beta1.ClusterConfig{ + CIDR: v1beta1.ClusterConfigCIDR{ + Pod: []v1beta1.CIDR{"10.71.0.0/18"}, + External: []v1beta1.CIDR{"10.70.0.0/16"}, + }, + }, + }, + Status: v1beta1.ConfigurationStatus{ + Remote: &v1beta1.ClusterConfig{ + CIDR: v1beta1.ClusterConfigCIDR{ + Pod: []v1beta1.CIDR{"10.71.64.0/18"}, + External: []v1beta1.CIDR{"10.68.0.0/16"}, + }, + }, + }, + } + + result, err := mapping.MapAddressWithConfiguration(&cfg, "10.71.1.53") + if err != nil { + panic(err) + } + + fmt.Println(result) +} diff --git a/pkg/liqoctl/test/network/check/ip.go b/pkg/liqoctl/test/network/check/ip.go index 166f6bca20..4725fb1965 100644 --- a/pkg/liqoctl/test/network/check/ip.go +++ b/pkg/liqoctl/test/network/check/ip.go @@ -127,8 +127,7 @@ func forgeIPTarget(ctx context.Context, cl clientctrl.Client, localIPRemapped ma if !needsRemap { target = append(target, ipnet.String()) } else { - maskLen, _ := cidrtarget.Mask.Size() - mapping.RemapMask(ipnet, *cidrtarget, maskLen) + mapping.RemapMask(ipnet, *cidrtarget) target = append(target, ipnet.String()) } } diff --git a/pkg/utils/ipam/mapping/ips.go b/pkg/utils/ipam/mapping/ips.go index 406dc6f019..9947b9f504 100644 --- a/pkg/utils/ipam/mapping/ips.go +++ b/pkg/utils/ipam/mapping/ips.go @@ -16,6 +16,7 @@ package mapping import ( "context" + "encoding/binary" "fmt" "net" @@ -134,7 +135,6 @@ func MapAddress(ctx context.Context, cl client.Client, func MapAddressWithConfiguration(cfg *networkingv1beta1.Configuration, address string) (string, error) { var ( podnet, podnetMapped, extnet, extnetMapped *net.IPNet - podNetMaskLen, extNetMaskLen int err error ) @@ -150,7 +150,6 @@ func MapAddressWithConfiguration(cfg *networkingv1beta1.Configuration, address s if err != nil { return "", err } - podNetMaskLen, _ = podnetMapped.Mask.Size() } _, extnet, err = net.ParseCIDR(cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.External).String()) @@ -162,15 +161,14 @@ func MapAddressWithConfiguration(cfg *networkingv1beta1.Configuration, address s if err != nil { return "", err } - extNetMaskLen, _ = extnetMapped.Mask.Size() } paddr := net.ParseIP(address) if podNeedsRemap && podnet.Contains(paddr) { - return RemapMask(paddr, *podnetMapped, podNetMaskLen).String(), nil + return RemapMask(paddr, *podnetMapped).String(), nil } if extNeedsRemap && extnet.Contains(paddr) { - return RemapMask(paddr, *extnetMapped, extNetMaskLen).String(), nil + return RemapMask(paddr, *extnetMapped).String(), nil } return address, nil @@ -179,12 +177,43 @@ func MapAddressWithConfiguration(cfg *networkingv1beta1.Configuration, address s // RemapMask remaps the mask of the address. // Consider that net.IP is always a slice of 16 bytes (big-endian). // The mask is a slice of 4 or 16 bytes (big-endian). -func RemapMask(addr net.IP, mask net.IPNet, maskLen int) net.IP { - maskLenBytes := maskLen / 8 - for i := 0; i < maskLenBytes; i++ { - // i+(len(addr)-len(mask.IP)) allows to start from the rightmost byte of the address. - // e.g if addr is ipv4 len(addr) = 16, and mask is ipv4 len(mask.IP) = 4, then we start from addr[12]. - addr[i+(len(addr)-len(mask.IP))] = mask.IP[i] - } - return addr +func RemapMask(addr net.IP, mask net.IPNet) net.IP { + switch len(mask.IP) { + case net.IPv4len: + addr = addr.To4() + + addrBin := binary.BigEndian.Uint32(addr) + maskBin := binary.BigEndian.Uint32(mask.Mask) + netBin := binary.BigEndian.Uint32(mask.IP) + + hostBin := addrBin & (^maskBin) + + result := netBin | hostBin + + resultBytes := make([]byte, 4) + binary.BigEndian.PutUint32(resultBytes, result) + + return resultBytes + case net.IPv6len: + addr = addr.To16() + + addrBin1 := binary.BigEndian.Uint64(addr[:8]) + addrBin2 := binary.BigEndian.Uint64(addr[8:]) + maskBin1 := binary.BigEndian.Uint64(mask.IP[:8]) + maskBin2 := binary.BigEndian.Uint64(mask.IP[8:]) + + hostBin1 := addrBin1 & (^maskBin1) + hostBin2 := addrBin2 & (^maskBin2) + + result1 := maskBin1 | hostBin1 + result2 := maskBin2 | hostBin2 + + resultBytes := make([]byte, 16) + + binary.BigEndian.PutUint64(resultBytes[:8], result1) + binary.BigEndian.PutUint64(resultBytes[8:], result2) + + return resultBytes + } + return nil } diff --git a/pkg/utils/ipam/mapping/ips_suite_test.go b/pkg/utils/ipam/mapping/ips_suite_test.go new file mode 100644 index 0000000000..739e772e37 --- /dev/null +++ b/pkg/utils/ipam/mapping/ips_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2019-2025 The Liqo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mapping + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestMapping(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Mapping Suite") +} diff --git a/pkg/utils/ipam/mapping/ips_test.go b/pkg/utils/ipam/mapping/ips_test.go new file mode 100644 index 0000000000..abca293081 --- /dev/null +++ b/pkg/utils/ipam/mapping/ips_test.go @@ -0,0 +1,93 @@ +// Copyright 2019-2025 The Liqo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mapping + +import ( + "net" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("RemapMask", func() { + DescribeTable("IPv4 address remapping", + func(ipStr, cidrStr string, expectedStr string) { + addr := net.ParseIP(ipStr) + Expect(addr).NotTo(BeNil()) + _, mask, err := net.ParseCIDR(cidrStr) + Expect(err).NotTo(HaveOccurred()) // Check no error occurs + Expect(mask).NotTo(BeNil()) // Check mask is not nil + expected := net.ParseIP(expectedStr).To4() + + result := RemapMask(addr, *mask) + Expect(result).To(Equal(expected)) + }, + Entry("valid IPv4 remapping", "192.168.1.20", "10.0.0.0/27", "10.0.0.20"), + Entry("valid IPv4 remapping", "192.168.2.20", "10.0.0.0/27", "10.0.0.20"), + Entry("valid IPv4 remapping", "192.168.1.50", "10.0.0.0/27", "10.0.0.18"), + Entry("valid IPv4 remapping", "192.168.2.50", "10.0.0.0/27", "10.0.0.18"), + Entry("valid IPv4 remapping", "192.168.1.1", "255.255.255.128/25", "255.255.255.129"), + Entry("valid IPv4 remapping", "172.16.0.1", "255.255.255.128/25", "255.255.255.129"), + Entry("valid IPv4 remapping", "192.168.1.200", "255.255.255.128/25", "255.255.255.200"), + Entry("valid IPv4 remapping", "192.168.1.10", "255.255.255.0/24", "255.255.255.10"), + Entry("valid IPv4 remapping", "192.168.1.100", "255.255.255.0/24", "255.255.255.100"), + Entry("valid IPv4 remapping", "172.16.0.1", "255.255.240.0/20", "255.255.240.1"), + Entry("valid IPv4 remapping", "172.16.1.1", "255.255.240.0/20", "255.255.241.1"), + Entry("valid IPv4 remapping", "192.168.1.10", "255.4.224.0/19", "255.4.225.10"), + Entry("valid IPv4 remapping", "192.168.2.10", "255.4.224.0/19", "255.4.226.10"), + Entry("valid IPv4 remapping", "10.0.0.1", "255.255.192.0/18", "255.255.192.1"), + Entry("valid IPv4 remapping", "10.255.1.1", "78.5.78.0/18", "78.5.65.1"), + Entry("valid IPv4 remapping", "192.168.1.20", "192.168.0.0/16", "192.168.1.20"), + Entry("valid IPv4 remapping", "192.168.2.20", "192.168.0.0/16", "192.168.2.20"), + Entry("valid IPv4 remapping", "172.16.1.1", "172.16.0.0/12", "172.16.1.1"), + Entry("valid IPv4 remapping", "172.16.2.1", "172.16.0.0/12", "172.16.2.1"), + Entry("valid IPv4 remapping", "10.0.0.1", "255.192.0.0/10", "255.192.0.1"), + Entry("valid IPv4 remapping", "10.1.0.1", "40.32.0.0/10", "40.1.0.1"), + Entry("valid IPv4 remapping", "10.0.0.1", "255.0.0.0/8", "255.0.0.1"), + Entry("valid IPv4 remapping", "10.0.0.50", "255.0.0.0/8", "255.0.0.50"), + Entry("valid IPv4 remapping", "10.1.1.1", "20.0.0.0/8", "20.1.1.1"), + Entry("valid IPv4 remapping", "10.2.1.1", "20.0.0.0/8", "20.2.1.1"), + Entry("valid IPv4 remapping", "192.168.1.10", "240.0.0.0/4", "240.168.1.10"), + Entry("valid IPv4 remapping", "192.168.2.10", "240.0.0.0/4", "240.168.2.10"), + ) + + DescribeTable("IPv6 address remapping", + func(ipStr, cidrStr string, expectedStr string) { + addr := net.ParseIP(ipStr) + Expect(addr).NotTo(BeNil()) + _, mask, err := net.ParseCIDR(cidrStr) + Expect(err).NotTo(HaveOccurred()) // Check no error occurs + Expect(mask).NotTo(BeNil()) // Check mask is not nil + expected := net.ParseIP(expectedStr).To16() + + result := RemapMask(addr, *mask) + Expect(result).To(Equal(expected)) + }, + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8::/32", "2001:db8::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8::/32", "2001:db8:1::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1::/48", "2001:db8:1::1"), + Entry("valid IPv6 remapping", "2001:db8:2::1", "2001:db8:1::/48", "2001:db8:1:2::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1:2::/64", "2001:db8:1:2::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8:1:2::/64", "2001:db8:1:2:1::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1:2:3::/80", "2001:db8:1:2:3::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8:1:2:3::/80", "2001:db8:1:2:3:1::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1:2:3:4::/96", "2001:db8:1:2:3:4::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8:1:2:3:4::/96", "2001:db8:1:2:3:4:1::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1:2:3:4:5::/112", "2001:db8:1:2:3:4:5::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8:1:2:3:4:5::/112", "2001:db8:1:2:3:4:5:1::1"), + Entry("valid IPv6 remapping", "2001:db8::1", "2001:db8:1:2:3:4:5:6::/128", "2001:db8:1:2:3:4:5:6::1"), + Entry("valid IPv6 remapping", "2001:db8:1::1", "2001:db8:1:2:3:4:5:6::/128", "2001:db8:1:2:3:4:5:6:1::1"), + ) +})