Browse Source

Merge pull request #894 from tomdee/align-subnets

subnet/config.go: Ensure that Subnets are aligned
Gunjan Patel 7 years ago
parent
commit
7257bcc863
1 changed files with 11 additions and 1 deletions
  1. 11 1
      subnet/config.go

+ 11 - 1
subnet/config.go

@@ -71,7 +71,7 @@ func ParseConfig(s string) (*Config, error) {
 	if cfg.SubnetMin == ip.IP4(0) {
 		// skip over the first subnet otherwise it causes problems. e.g.
 		// if Network is 10.100.0.0/16, having an interface with 10.0.0.0
-		// makes ping think it's a broadcast address (not sure why)
+		// conflicts with the broadcast address.
 		cfg.SubnetMin = cfg.Network.IP + subnetSize
 	} else if !cfg.Network.Contains(cfg.SubnetMin) {
 		return nil, errors.New("SubnetMin is not in the range of the Network")
@@ -83,6 +83,16 @@ func ParseConfig(s string) (*Config, error) {
 		return nil, errors.New("SubnetMax is not in the range of the Network")
 	}
 
+	// The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
+	mask := ip.IP4(0xFFFFFFFF << (32 - cfg.SubnetLen))
+	if cfg.SubnetMin != cfg.SubnetMin&mask {
+		return nil, fmt.Errorf("SubnetMin is not on a SubnetLen boundary: %v", cfg.SubnetMin)
+	}
+
+	if cfg.SubnetMax != cfg.SubnetMax&mask {
+		return nil, fmt.Errorf("SubnetMax is not on a SubnetLen boundary: %v", cfg.SubnetMax)
+	}
+
 	bt, err := parseBackendType(cfg.Backend)
 	if err != nil {
 		return nil, err