Skip to content

Commit

Permalink
limit dhcp range with BitMapMax
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Fedchenkov <giggsoff@gmail.com>
(cherry picked from commit cb9debb)
  • Loading branch information
giggsoff authored and eriknordmark committed Jan 19, 2022
1 parent d25180e commit ac7dcc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/pillar/cmd/zedagent/parseconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,12 @@ func parseIpspec(ipspec *zconfig.Ipspec,
return fmt.Errorf("gateway(%s) inside Dhcp Range",
config.Gateway.String())
}
addressesInRange := config.DhcpRange.Size()
// we cannot use more than types.BitMapMax IPs for dynamic allocation
// and should keep some place in the end for static IPs assignment
if addressesInRange > types.BitMapMax {
config.DhcpRange.End = types.AddToIP(config.DhcpRange.Start, types.BitMapMax)
}
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/pillar/types/zedroutertypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package types

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -2053,6 +2054,22 @@ func (ipRange IpRange) Contains(ipAddr net.IP) bool {
return false
}

// Size returns addresses count inside IpRange
func (ipRange IpRange) Size() uint32 {
//TBD:XXX, IPv6 handling
ip1v4 := ipRange.Start.To4()
ip2v4 := ipRange.End.To4()
if ip1v4 == nil || ip2v4 == nil {
return 0
}
ip1Int := binary.BigEndian.Uint32(ip1v4)
ip2Int := binary.BigEndian.Uint32(ip2v4)
if ip1Int > ip2Int {
return ip1Int - ip2Int
}
return ip2Int - ip1Int
}

func (config NetworkXObjectConfig) Key() string {
return config.UUID.String()
}
Expand Down

0 comments on commit ac7dcc1

Please sign in to comment.