Do not assume IPv4 during address generation

This commit is contained in:
Csaba Sarkadi 2021-10-31 16:05:08 +01:00
parent b6d0c4f2aa
commit 46cdce00af
2 changed files with 15 additions and 19 deletions

View file

@ -141,36 +141,32 @@ func (h *Headscale) getAvailableIP() (*netaddr.IP, error) {
return nil, err
}
ipPrefixNetworkAddress, ipPrefixBroadcastAddress := func() (netaddr.IP, netaddr.IP) {
ipRange := ipPrefix.Range()
return ipRange.From(), ipRange.To()
}()
// Get the first IP in our prefix
ip := ipPrefix.IP()
ip := ipPrefixNetworkAddress.Next()
for {
if !ipPrefix.Contains(ip) {
return nil, errCouldNotAllocateIP
}
// Some OS (including Linux) does not like when IPs ends with 0 or 255, which
// is typically called network or broadcast. Lets avoid them and continue
// to look when we get one of those traditionally reserved IPs.
ipRaw := ip.As4()
if ipRaw[3] == 0 || ipRaw[3] == 255 {
switch {
case ip.Compare(ipPrefixBroadcastAddress) == 0:
fallthrough
case containsIPs(usedIps, ip):
fallthrough
case ip.IsZero() || ip.IsLoopback():
ip = ip.Next()
continue
}
if ip.IsZero() &&
ip.IsLoopback() {
ip = ip.Next()
continue
}
if !containsIPs(usedIps, ip) {
default:
return &ip, nil
}
ip = ip.Next()
}
}