diff --git a/internal/dhcpd/v4.go b/internal/dhcpd/v4.go index 0fedc624c95..21345153b88 100644 --- a/internal/dhcpd/v4.go +++ b/internal/dhcpd/v4.go @@ -342,7 +342,7 @@ func (s *v4Server) AddStaticLease(l Lease) (err error) { } if hostname := l.Hostname; hostname != "" { - hostname, err = normalizeHostname(l.Hostname) + hostname, err = normalizeHostname(hostname) if err != nil { return err } @@ -636,6 +636,33 @@ func (o *optFQDN) ToBytes() []byte { return b } +// checkLease checks if the pair of mac and ip is already leased. The mismatch +// is true when the existing lease has the same hardware address but differs in +// its IP address. +func (s *v4Server) checkLease(mac net.HardwareAddr, ip net.IP) (lease *Lease, mismatch bool) { + s.leasesLock.Lock() + defer s.leasesLock.Unlock() + + for _, l := range s.leases { + if !bytes.Equal(l.HWAddr, mac) { + continue + } + + if l.IP.Equal(ip) { + return l, false + } + + log.Debug( + `dhcpv4: mismatched OptionRequestedIPAddress in req msg for %s`, + mac, + ) + + return nil, true + } + + return nil, false +} + // processRequest is the handler for the DHCP Request request. func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needsReply bool) { mac := req.ClientHWAddr @@ -657,31 +684,8 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needs return nil, false } - if func() (mismatch bool) { - s.leasesLock.Lock() - defer s.leasesLock.Unlock() - - for _, l := range s.leases { - if !bytes.Equal(l.HWAddr, mac) { - continue - } - - if l.IP.Equal(reqIP) { - lease = l - - return false - } - - log.Debug( - `dhcpv4: mismatched OptionRequestedIPAddress in req msg for %s`, - mac, - ) - - return true - } - - return false - }() { + var mismatch bool + if lease, mismatch = s.checkLease(mac, reqIP); mismatch { return nil, true }