From 49059cc4ec302977473083445498523270f90525 Mon Sep 17 00:00:00 2001 From: zhuanlan Date: Sun, 22 Sep 2024 19:38:42 +0800 Subject: [PATCH] Check network mask when init subnet Signed-off-by: zhuanlan --- pkg/controller/init.go | 3 +++ pkg/util/net.go | 21 +++++++++++++++++++++ pkg/util/validator.go | 11 +++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/controller/init.go b/pkg/controller/init.go index 1878e31207f..489b799a0be 100644 --- a/pkg/controller/init.go +++ b/pkg/controller/init.go @@ -301,6 +301,9 @@ func (c *Controller) InitIPAM() error { subnetProviderMaps := make(map[string]string, len(subnets)) for _, subnet := range subnets { subnetProviderMaps[subnet.Name] = subnet.Spec.Provider + + klog.Infof("Init subnet %s", subnet.Name) + if err := c.ipam.AddOrUpdateSubnet(subnet.Name, subnet.Spec.CIDRBlock, subnet.Spec.Gateway, subnet.Spec.ExcludeIps); err != nil { klog.Errorf("failed to init subnet %s: %v", subnet.Name, err) } diff --git a/pkg/util/net.go b/pkg/util/net.go index 802efe79b94..d11f8f4b895 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -712,3 +712,24 @@ func InvalidCIDR(s string) error { } return nil } + +func InvalidNetworkMask(network *net.IPNet) error { + mask := net.IP(network.Mask).String() + ip := network.IP + if ip.To4() != nil { + if mask == "255.255.255.255" { + err := errors.New("invalid ipv4 mask 32") + klog.Error(err) + return err + } + } + if ip.To16() != nil { + if mask == "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" { + err := errors.New("invalid ipv6 mask 128") + klog.Error(err) + return err + } + } + + return nil +} diff --git a/pkg/util/validator.go b/pkg/util/validator.go index b4d2d25d350..77688ea4fd8 100644 --- a/pkg/util/validator.go +++ b/pkg/util/validator.go @@ -78,8 +78,15 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error { klog.Errorf("invalid subnet %s cidr %s, %s", subnet.Name, cidr, err) return err } - if _, _, err := net.ParseCIDR(cidr); err != nil { - return fmt.Errorf("subnet %s cidr %s is invalid", subnet.Name, cidr) + var network *net.IPNet + _, network, err := net.ParseCIDR(cidr) + if err != nil { + return fmt.Errorf("subnet %s cidr %s is invalid, due to %w", subnet.Name, cidr, err) + } + // check network mask is 32 in ipv4 or 128 in ipv6 + err = InvalidNetworkMask(network) + if err != nil { + return fmt.Errorf("subnet %s cidr %s mask is invalid, due to %w", subnet.Name, cidr, err) } }