Skip to content

Commit

Permalink
fix: reamap mask
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Feb 7, 2025
1 parent d03e24a commit 47b04b2
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 15 deletions.
56 changes: 56 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions pkg/liqoctl/test/network/check/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
55 changes: 42 additions & 13 deletions pkg/utils/ipam/mapping/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mapping

import (
"context"
"encoding/binary"
"fmt"
"net"

Expand Down Expand Up @@ -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
)

Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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
}
27 changes: 27 additions & 0 deletions pkg/utils/ipam/mapping/ips_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
93 changes: 93 additions & 0 deletions pkg/utils/ipam/mapping/ips_test.go
Original file line number Diff line number Diff line change
@@ -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"),
)
})

0 comments on commit 47b04b2

Please sign in to comment.