2021-05-21 14:55:42 +03:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 19:42:41 +03:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-13 16:00:09 +03:00
|
|
|
"strings"
|
2020-07-03 18:20:01 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-03-31 12:36:57 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-04-20 15:07:57 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-07-03 18:20:01 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-11-20 13:44:21 +03:00
|
|
|
"github.com/go-ping/ping"
|
2020-07-03 18:20:01 +03:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4/server4"
|
|
|
|
)
|
|
|
|
|
2020-11-27 14:39:43 +03:00
|
|
|
// v4Server is a DHCPv4 server.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Think about unifying this and v6Server.
|
2020-07-03 18:20:01 +03:00
|
|
|
type v4Server struct {
|
|
|
|
conf V4ServerConf
|
2021-03-16 19:11:32 +03:00
|
|
|
srv *server4.Server
|
|
|
|
|
|
|
|
// leasedOffsets contains offsets from conf.ipRange.start that have been
|
|
|
|
// leased.
|
2021-03-18 17:07:13 +03:00
|
|
|
leasedOffsets *bitSet
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-04-20 15:07:57 +03:00
|
|
|
// leaseHosts is the set of all hostnames of all known DHCP clients.
|
|
|
|
leaseHosts *aghstrings.Set
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
// leases contains all dynamic and static leases.
|
|
|
|
leases []*Lease
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// leasesLock protects leases, leaseHosts, and leasedOffsets.
|
2021-03-16 19:11:32 +03:00
|
|
|
leasesLock sync.Mutex
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig4 - write configuration
|
|
|
|
func (s *v4Server) WriteDiskConfig4(c *V4ServerConf) {
|
|
|
|
*c = s.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig6 - write configuration
|
|
|
|
func (s *v4Server) WriteDiskConfig6(c *V6ServerConf) {
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// normalizeHostname normalizes a hostname sent by the client. If err is not
|
|
|
|
// nil, norm is an empty string.
|
|
|
|
func normalizeHostname(hostname string) (norm string, err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "normalizing %q: %w", hostname) }()
|
2021-05-06 13:02:48 +03:00
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
norm = strings.ToLower(hostname)
|
|
|
|
parts := strings.FieldsFunc(norm, func(c rune) (ok bool) {
|
|
|
|
return c != '.' && !aghnet.IsValidHostOuterRune(c)
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return "", fmt.Errorf("no valid parts")
|
|
|
|
}
|
|
|
|
|
|
|
|
norm = strings.Join(parts, "-")
|
|
|
|
norm = strings.TrimSuffix(norm, "-")
|
|
|
|
|
|
|
|
return norm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
// validHostnameForClient accepts the hostname sent by the client and its IP and
|
|
|
|
// returns either a normalized version of that hostname, or a new hostname
|
|
|
|
// generated from the IP address, or an empty string.
|
|
|
|
func (s *v4Server) validHostnameForClient(cliHostname string, ip net.IP) (hostname string) {
|
|
|
|
hostname, err := normalizeHostname(cliHostname)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("dhcpv4: %s", err)
|
2021-05-06 13:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = aghnet.GenerateHostname(ip)
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
err = aghnet.ValidateDomainName(hostname)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("dhcpv4: %s", err)
|
|
|
|
hostname = ""
|
2021-05-06 13:02:48 +03:00
|
|
|
}
|
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
return hostname
|
2021-05-06 13:02:48 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
// ResetLeases resets leases.
|
|
|
|
func (s *v4Server) ResetLeases(leases []*Lease) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-04-29 14:52:22 +03:00
|
|
|
|
2021-04-21 14:42:19 +03:00
|
|
|
if !s.conf.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.leasedOffsets = newBitSet()
|
|
|
|
s.leaseHosts = aghstrings.NewSet()
|
2020-07-03 18:20:01 +03:00
|
|
|
s.leases = nil
|
|
|
|
|
|
|
|
for _, l := range leases {
|
2021-06-10 14:54:47 +03:00
|
|
|
if !l.IsStatic() {
|
|
|
|
l.Hostname = s.validHostnameForClient(l.Hostname, l.IP)
|
|
|
|
}
|
2021-04-29 14:52:22 +03:00
|
|
|
err = s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(a.garipov): Wrap and bubble up the error.
|
2021-04-20 15:07:57 +03:00
|
|
|
log.Error(
|
|
|
|
"dhcpv4: reset: re-adding a lease for %s (%s): %s",
|
|
|
|
l.IP,
|
|
|
|
l.HWAddr,
|
|
|
|
err,
|
|
|
|
)
|
2021-03-18 17:07:13 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
// getLeasesRef returns the actual leases slice. For internal use only.
|
|
|
|
func (s *v4Server) getLeasesRef() []*Lease {
|
2020-07-03 18:20:01 +03:00
|
|
|
return s.leases
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
// isBlocklisted returns true if this lease holds a blocklisted IP.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make a method of *Lease?
|
|
|
|
func (s *v4Server) isBlocklisted(l *Lease) (ok bool) {
|
|
|
|
if len(l.HWAddr) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = true
|
|
|
|
for _, b := range l.HWAddr {
|
|
|
|
if b != 0 {
|
|
|
|
ok = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
// GetLeases returns the list of current DHCP leases. It is safe for concurrent
|
|
|
|
// use.
|
2021-06-16 16:48:46 +03:00
|
|
|
func (s *v4Server) GetLeases(flags GetLeasesFlags) (leases []*Lease) {
|
2021-03-18 17:07:13 +03:00
|
|
|
// The function shouldn't return nil, because zero-length slice behaves
|
|
|
|
// differently in cases like marshalling. Our front-end also requires
|
|
|
|
// a non-nil value in the response.
|
2021-06-16 16:48:46 +03:00
|
|
|
leases = []*Lease{}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
|
|
|
getDynamic := flags&LeasesDynamic != 0
|
|
|
|
getStatic := flags&LeasesStatic != 0
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
2021-03-18 17:07:13 +03:00
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if getDynamic && l.Expiry.After(now) && !s.isBlocklisted(l) {
|
2021-06-16 16:48:46 +03:00
|
|
|
leases = append(leases, l.Clone())
|
2021-03-18 17:07:13 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if getStatic && l.IsStatic() {
|
2021-06-16 16:48:46 +03:00
|
|
|
leases = append(leases, l.Clone())
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
return leases
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindMACbyIP - find a MAC address by IP address in the currently active DHCP leases
|
|
|
|
func (s *v4Server) FindMACbyIP(ip net.IP) net.HardwareAddr {
|
2021-03-18 17:07:13 +03:00
|
|
|
now := time.Now()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
ip4 := ip.To4()
|
|
|
|
if ip4 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if l.IP.Equal(ip4) {
|
2021-03-18 17:07:13 +03:00
|
|
|
if l.Expiry.After(now) || l.IsStatic() {
|
2020-07-03 18:20:01 +03:00
|
|
|
return l.HWAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:02:17 +03:00
|
|
|
// defaultHwAddrLen is the default length of a hardware (MAC) address.
|
|
|
|
const defaultHwAddrLen = 6
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
// Add the specified IP to the black list for a time period
|
2021-03-17 15:02:17 +03:00
|
|
|
func (s *v4Server) blocklistLease(l *Lease) {
|
|
|
|
l.HWAddr = make(net.HardwareAddr, defaultHwAddrLen)
|
|
|
|
l.Hostname = ""
|
|
|
|
l.Expiry = time.Now().Add(s.conf.leaseTime)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
// rmLeaseByIndex removes a lease by its index in the leases slice.
|
|
|
|
func (s *v4Server) rmLeaseByIndex(i int) {
|
2021-03-17 15:02:17 +03:00
|
|
|
n := len(s.leases)
|
|
|
|
if i >= n {
|
|
|
|
// TODO(a.garipov): Better error handling.
|
|
|
|
log.Debug("dhcpv4: can't remove lease at index %d: no such lease", i)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
l := s.leases[i]
|
|
|
|
s.leases = append(s.leases[:i], s.leases[i+1:]...)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
r := s.conf.ipRange
|
|
|
|
offset, ok := r.offset(l.IP)
|
|
|
|
if ok {
|
2021-03-18 17:07:13 +03:00
|
|
|
s.leasedOffsets.set(offset, false)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-04-20 15:07:57 +03:00
|
|
|
s.leaseHosts.Del(l.Hostname)
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
log.Debug("dhcpv4: removed lease %s (%s)", l.IP, l.HWAddr)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a dynamic lease with the same properties
|
|
|
|
// Return error if a static lease is found
|
2021-03-18 17:07:13 +03:00
|
|
|
func (s *v4Server) rmDynamicLease(lease *Lease) (err error) {
|
2020-07-03 18:20:01 +03:00
|
|
|
for i := 0; i < len(s.leases); i++ {
|
|
|
|
l := s.leases[i]
|
|
|
|
|
|
|
|
if bytes.Equal(l.HWAddr, lease.HWAddr) {
|
2021-03-18 17:07:13 +03:00
|
|
|
if l.IsStatic() {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("static lease already exists")
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
s.rmLeaseByIndex(i)
|
2020-08-25 13:38:52 +03:00
|
|
|
if i == len(s.leases) {
|
|
|
|
break
|
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
l = s.leases[i]
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
if l.IP.Equal(lease.IP) {
|
2021-03-18 17:07:13 +03:00
|
|
|
if l.IsStatic() {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("static lease already exists")
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
s.rmLeaseByIndex(i)
|
2021-05-13 15:17:42 +03:00
|
|
|
if i == len(s.leases) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
l = s.leases[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Hostname == lease.Hostname {
|
|
|
|
l.Hostname = ""
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// addLease adds a dynamic or static lease.
|
|
|
|
func (s *v4Server) addLease(l *Lease) (err error) {
|
2021-03-16 19:11:32 +03:00
|
|
|
r := s.conf.ipRange
|
2021-05-06 13:02:48 +03:00
|
|
|
offset, inOffset := r.offset(l.IP)
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
if l.IsStatic() {
|
|
|
|
if sn := s.conf.subnet; !sn.Contains(l.IP) {
|
|
|
|
return fmt.Errorf("subnet %s does not contain the ip %q", sn, l.IP)
|
|
|
|
}
|
|
|
|
} else if !inOffset {
|
2021-03-18 17:07:13 +03:00
|
|
|
return fmt.Errorf("lease %s (%s) out of range, not adding", l.IP, l.HWAddr)
|
2021-03-16 19:11:32 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
s.leases = append(s.leases, l)
|
2021-03-18 17:07:13 +03:00
|
|
|
s.leasedOffsets.set(offset, true)
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
if l.Hostname != "" {
|
|
|
|
s.leaseHosts.Add(l.Hostname)
|
2021-03-18 17:07:13 +03:00
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
return nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// rmLease removes a lease with the same properties.
|
2021-06-16 16:48:46 +03:00
|
|
|
func (s *v4Server) rmLease(lease *Lease) (err error) {
|
2021-03-18 17:07:13 +03:00
|
|
|
if len(s.leases) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
for i, l := range s.leases {
|
2021-03-16 19:11:32 +03:00
|
|
|
if l.IP.Equal(lease.IP) {
|
2021-03-18 17:07:13 +03:00
|
|
|
if !bytes.Equal(l.HWAddr, lease.HWAddr) || l.Hostname != lease.Hostname {
|
|
|
|
return fmt.Errorf("lease for ip %s is different: %+v", lease.IP, l)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
s.rmLeaseByIndex(i)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("lease not found")
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
// AddStaticLease adds a static lease. It is safe for concurrent use.
|
2021-06-16 16:48:46 +03:00
|
|
|
func (s *v4Server) AddStaticLease(l *Lease) (err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: adding static lease: %w") }()
|
2021-03-18 17:07:13 +03:00
|
|
|
|
|
|
|
if ip4 := l.IP.To4(); ip4 == nil {
|
|
|
|
return fmt.Errorf("invalid ip %q, only ipv4 is supported", l.IP)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2021-04-20 15:07:57 +03:00
|
|
|
l.Expiry = time.Unix(leaseExpireStatic, 0)
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
err = aghnet.ValidateHardwareAddress(l.HWAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:54:47 +03:00
|
|
|
if hostname := l.Hostname; hostname != "" {
|
|
|
|
hostname, err = normalizeHostname(hostname)
|
2021-05-13 15:17:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
err = aghnet.ValidateDomainName(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating hostname: %w", err)
|
|
|
|
}
|
2021-05-06 13:02:48 +03:00
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
// Don't check for hostname uniqueness, since we try to emulate
|
|
|
|
// dnsmasq here, which means that rmDynamicLease below will
|
|
|
|
// simply empty the hostname of the dynamic lease if there even
|
|
|
|
// is one.
|
2021-05-06 13:02:48 +03:00
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
l.Hostname = hostname
|
|
|
|
}
|
2021-05-06 13:02:48 +03:00
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
// Perform the following actions in an anonymous function to make sure
|
|
|
|
// that the lock gets unlocked before the notification step.
|
|
|
|
func() {
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.rmDynamicLease(l)
|
2021-03-18 17:07:13 +03:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"removing dynamic leases for %s (%s): %w",
|
|
|
|
l.IP,
|
|
|
|
l.HWAddr,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.addLease(l)
|
2021-03-18 17:07:13 +03:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("adding static lease for %s (%s): %w", l.IP, l.HWAddr, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2020-07-03 18:20:01 +03:00
|
|
|
s.conf.notify(LeaseChangedAddedStatic)
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:36:57 +03:00
|
|
|
// RemoveStaticLease removes a static lease. It is safe for concurrent use.
|
2021-06-16 16:48:46 +03:00
|
|
|
func (s *v4Server) RemoveStaticLease(l *Lease) (err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-03-31 12:36:57 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
if len(l.IP) != 4 {
|
|
|
|
return fmt.Errorf("invalid IP")
|
|
|
|
}
|
2021-03-31 12:36:57 +03:00
|
|
|
|
|
|
|
err = aghnet.ValidateHardwareAddress(l.HWAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating lease: %w", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
2021-03-31 12:36:57 +03:00
|
|
|
err = s.rmLease(l)
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
|
|
|
s.leasesLock.Unlock()
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.leasesLock.Unlock()
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2020-07-03 18:20:01 +03:00
|
|
|
s.conf.notify(LeaseChangedRemovedStatic)
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// addrAvailable sends an ICP request to the specified IP address. It returns
|
|
|
|
// true if the remote host doesn't reply, which probably means that the IP
|
|
|
|
// address is available.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): I'm not sure that this is the best way to do this.
|
|
|
|
func (s *v4Server) addrAvailable(target net.IP) (avail bool) {
|
2020-07-03 18:20:01 +03:00
|
|
|
if s.conf.ICMPTimeout == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger, err := ping.NewPinger(target.String())
|
|
|
|
if err != nil {
|
2021-05-06 13:02:48 +03:00
|
|
|
log.Error("dhcpv4: ping.NewPinger(): %s", err)
|
2020-11-20 13:44:21 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger.SetPrivileged(true)
|
|
|
|
pinger.Timeout = time.Duration(s.conf.ICMPTimeout) * time.Millisecond
|
|
|
|
pinger.Count = 1
|
|
|
|
reply := false
|
2021-03-16 19:11:32 +03:00
|
|
|
pinger.OnRecv = func(_ *ping.Packet) {
|
2020-07-03 18:20:01 +03:00
|
|
|
reply = true
|
|
|
|
}
|
2021-05-06 13:02:48 +03:00
|
|
|
|
|
|
|
log.Debug("dhcpv4: sending icmp echo to %s", target)
|
2020-11-20 13:44:21 +03:00
|
|
|
|
|
|
|
err = pinger.Run()
|
|
|
|
if err != nil {
|
2021-05-06 13:02:48 +03:00
|
|
|
log.Error("dhcpv4: pinger.Run(): %s", err)
|
|
|
|
|
2020-11-20 13:44:21 +03:00
|
|
|
return true
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
if reply {
|
2021-05-06 13:02:48 +03:00
|
|
|
log.Info("dhcpv4: ip conflict: %s is already used by another device", target)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
log.Debug("dhcpv4: icmp procedure is complete: %q", target)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
// findLease finds a lease by its MAC-address.
|
|
|
|
func (s *v4Server) findLease(mac net.HardwareAddr) (l *Lease) {
|
|
|
|
for _, l = range s.leases {
|
|
|
|
if bytes.Equal(mac, l.HWAddr) {
|
|
|
|
return l
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
// nextIP generates a new free IP.
|
|
|
|
func (s *v4Server) nextIP() (ip net.IP) {
|
|
|
|
r := s.conf.ipRange
|
|
|
|
ip = r.find(func(next net.IP) (ok bool) {
|
|
|
|
offset, ok := r.offset(next)
|
|
|
|
if !ok {
|
|
|
|
// Shouldn't happen.
|
|
|
|
return false
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
return !s.leasedOffsets.isSet(offset)
|
2021-03-16 19:11:32 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return ip.To4()
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find an expired lease and return its index or -1
|
|
|
|
func (s *v4Server) findExpiredLease() int {
|
2021-03-18 17:07:13 +03:00
|
|
|
now := time.Now()
|
2020-07-03 18:20:01 +03:00
|
|
|
for i, lease := range s.leases {
|
2021-03-18 17:07:13 +03:00
|
|
|
if !lease.IsStatic() && lease.Expiry.Before(now) {
|
2020-07-03 18:20:01 +03:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
// reserveLease reserves a lease for a client by its MAC-address. It returns
|
|
|
|
// nil if it couldn't allocate a new lease.
|
2021-03-18 17:07:13 +03:00
|
|
|
func (s *v4Server) reserveLease(mac net.HardwareAddr) (l *Lease, err error) {
|
2021-03-16 19:11:32 +03:00
|
|
|
l = &Lease{
|
2021-03-17 15:02:17 +03:00
|
|
|
HWAddr: make([]byte, len(mac)),
|
2021-03-16 19:11:32 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
copy(l.HWAddr, mac)
|
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
l.IP = s.nextIP()
|
2020-07-03 18:20:01 +03:00
|
|
|
if l.IP == nil {
|
|
|
|
i := s.findExpiredLease()
|
|
|
|
if i < 0 {
|
2021-03-18 17:07:13 +03:00
|
|
|
return nil, nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
copy(s.leases[i].HWAddr, mac)
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
return s.leases[i], nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
err = s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-16 19:11:32 +03:00
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
return l, nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v4Server) commitLease(l *Lease) {
|
|
|
|
l.Expiry = time.Now().Add(s.conf.leaseTime)
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
func() {
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2021-05-13 15:17:42 +03:00
|
|
|
|
|
|
|
if l.Hostname != "" {
|
|
|
|
s.leaseHosts.Add(l.Hostname)
|
|
|
|
}
|
2021-05-06 13:02:48 +03:00
|
|
|
}()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
s.conf.notify(LeaseChangedAdded)
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
// allocateLease allocates a new lease for the MAC address. If there are no IP
|
|
|
|
// addresses left, both l and err are nil.
|
|
|
|
func (s *v4Server) allocateLease(mac net.HardwareAddr) (l *Lease, err error) {
|
|
|
|
for {
|
|
|
|
l, err = s.reserveLease(mac)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reserving a lease: %w", err)
|
|
|
|
} else if l == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.addrAvailable(l.IP) {
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.blocklistLease(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
// processDiscover is the handler for the DHCP Discover request.
|
2021-03-18 17:07:13 +03:00
|
|
|
func (s *v4Server) processDiscover(req, resp *dhcpv4.DHCPv4) (l *Lease, err error) {
|
2020-07-03 18:20:01 +03:00
|
|
|
mac := req.ClientHWAddr
|
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
defer s.conf.notify(LeaseChangedDBStore)
|
2021-05-06 13:02:48 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
l = s.findLease(mac)
|
2021-05-06 13:02:48 +03:00
|
|
|
if l != nil {
|
|
|
|
reqIP := req.RequestedIPAddress()
|
|
|
|
if len(reqIP) != 0 && !reqIP.Equal(l.IP) {
|
|
|
|
log.Debug("dhcpv4: different RequestedIP: %s != %s", reqIP, l.IP)
|
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2021-05-06 13:02:48 +03:00
|
|
|
return l, nil
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
l, err = s.allocateLease(mac)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if l == nil {
|
|
|
|
log.Debug("dhcpv4: no more ip addresses")
|
2021-05-06 13:02:48 +03:00
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
return nil, nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
|
2021-03-18 17:07:13 +03:00
|
|
|
|
|
|
|
return l, nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 13:01:55 +03:00
|
|
|
type optFQDN struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *optFQDN) String() string {
|
|
|
|
return "optFQDN"
|
|
|
|
}
|
|
|
|
|
|
|
|
// flags[1]
|
|
|
|
// A-RR[1]
|
|
|
|
// PTR-RR[1]
|
|
|
|
// name[]
|
|
|
|
func (o *optFQDN) ToBytes() []byte {
|
|
|
|
b := make([]byte, 3+len(o.name))
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
b[i] = 0x03 // f_server_overrides | f_server
|
|
|
|
i++
|
|
|
|
|
|
|
|
b[i] = 255 // A-RR
|
|
|
|
i++
|
|
|
|
|
|
|
|
b[i] = 255 // PTR-RR
|
|
|
|
i++
|
|
|
|
|
|
|
|
copy(b[i:], []byte(o.name))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:54:47 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:17:42 +03:00
|
|
|
// processRequest is the handler for the DHCP Request request.
|
2021-05-06 13:02:48 +03:00
|
|
|
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needsReply bool) {
|
|
|
|
mac := req.ClientHWAddr
|
2021-03-12 13:34:43 +03:00
|
|
|
reqIP := req.RequestedIPAddress()
|
2020-07-03 18:20:01 +03:00
|
|
|
if reqIP == nil {
|
|
|
|
reqIP = req.ClientIPAddr
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:34:43 +03:00
|
|
|
sid := req.ServerIdentifier()
|
|
|
|
if len(sid) != 0 && !sid.Equal(s.conf.dnsIPAddrs[0]) {
|
2021-05-13 15:17:42 +03:00
|
|
|
log.Debug("dhcpv4: bad OptionServerIdentifier in req msg for %s", mac)
|
2021-03-12 13:34:43 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:34:43 +03:00
|
|
|
if ip4 := reqIP.To4(); ip4 == nil {
|
2021-05-13 15:17:42 +03:00
|
|
|
log.Debug("dhcpv4: bad OptionRequestedIPAddress in req msg for %s", mac)
|
2021-03-12 13:34:43 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:54:47 +03:00
|
|
|
var mismatch bool
|
|
|
|
if lease, mismatch = s.checkLease(mac, reqIP); mismatch {
|
2021-05-06 13:02:48 +03:00
|
|
|
return nil, true
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if lease == nil {
|
2021-05-06 13:02:48 +03:00
|
|
|
log.Debug("dhcpv4: no reserved lease for %s", mac)
|
2021-03-12 13:34:43 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
if !lease.IsStatic() {
|
2021-04-19 16:04:40 +03:00
|
|
|
cliHostname := req.HostName()
|
2021-05-13 15:17:42 +03:00
|
|
|
hostname := s.validHostnameForClient(cliHostname, reqIP)
|
|
|
|
if hostname != lease.Hostname && s.leaseHosts.Has(hostname) {
|
|
|
|
log.Info("dhcpv4: hostname %q already exists", hostname)
|
|
|
|
lease.Hostname = ""
|
|
|
|
} else {
|
|
|
|
lease.Hostname = hostname
|
2021-04-15 16:59:05 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
s.commitLease(lease)
|
2021-05-14 19:16:07 +03:00
|
|
|
} else if lease.Hostname != "" {
|
2020-08-24 13:01:55 +03:00
|
|
|
o := &optFQDN{
|
2020-08-25 17:44:30 +03:00
|
|
|
name: lease.Hostname,
|
2020-08-24 13:01:55 +03:00
|
|
|
}
|
|
|
|
fqdn := dhcpv4.Option{
|
|
|
|
Code: dhcpv4.OptionFQDN,
|
|
|
|
Value: o,
|
|
|
|
}
|
2021-03-12 13:34:43 +03:00
|
|
|
|
2020-08-24 13:01:55 +03:00
|
|
|
resp.UpdateOption(fqdn)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
2021-03-12 13:34:43 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return lease, true
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
// processRequest is the handler for the DHCP Decline request.
|
|
|
|
func (s *v4Server) processDecline(req, resp *dhcpv4.DHCPv4) (err error) {
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
mac := req.ClientHWAddr
|
|
|
|
reqIP := req.RequestedIPAddress()
|
|
|
|
if reqIP == nil {
|
|
|
|
reqIP = req.ClientIPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldLease *Lease
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if bytes.Equal(l.HWAddr, mac) && l.IP.Equal(reqIP) {
|
|
|
|
oldLease = l
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldLease == nil {
|
|
|
|
log.Info("dhcpv4: lease with ip %s for %s not found", reqIP, mac)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.rmDynamicLease(oldLease)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("removing old lease for %s: %w", mac, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newLease, err := s.allocateLease(mac)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("allocating new lease for %s: %w", mac, err)
|
|
|
|
} else if newLease == nil {
|
|
|
|
log.Info("dhcpv4: allocating new lease for %s: no more ip addresses", mac)
|
|
|
|
|
|
|
|
resp.YourIPAddr = make([]byte, 4)
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newLease.Hostname = oldLease.Hostname
|
|
|
|
newLease.Expiry = time.Now().Add(s.conf.leaseTime)
|
|
|
|
|
|
|
|
err = s.addLease(newLease)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("adding new lease for %s: %w", mac, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("dhcpv4: changed ip from %s to %s for %s", reqIP, newLease.IP, mac)
|
|
|
|
|
|
|
|
resp.YourIPAddr = make([]byte, 4)
|
|
|
|
copy(resp.YourIPAddr, newLease.IP)
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processRelease is the handler for the DHCP Release request.
|
|
|
|
func (s *v4Server) processRelease(req, resp *dhcpv4.DHCPv4) (err error) {
|
|
|
|
mac := req.ClientHWAddr
|
|
|
|
reqIP := req.RequestedIPAddress()
|
|
|
|
if reqIP == nil {
|
|
|
|
reqIP = req.ClientIPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(a.garipov): Add a separate notification type for dynamic lease
|
|
|
|
// removal?
|
|
|
|
defer s.conf.notify(LeaseChangedDBStore)
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if !bytes.Equal(l.HWAddr, mac) || !l.IP.Equal(reqIP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.rmDynamicLease(l)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("removing dynamic lease for %s: %w", mac, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("dhcpv4: released %d dynamic leases for %s", n, mac)
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
// Find a lease associated with MAC and prepare response
|
|
|
|
// Return 1: OK
|
|
|
|
// Return 0: error; reply with Nak
|
|
|
|
// Return -1: error; don't reply
|
2020-11-09 19:27:04 +03:00
|
|
|
func (s *v4Server) process(req, resp *dhcpv4.DHCPv4) int {
|
2021-03-18 17:07:13 +03:00
|
|
|
var err error
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptServerIdentifier(s.conf.dnsIPAddrs[0]))
|
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
// TODO(a.garipov): Refactor this into handlers.
|
2021-03-18 17:07:13 +03:00
|
|
|
var l *Lease
|
2020-07-03 18:20:01 +03:00
|
|
|
switch req.MessageType() {
|
|
|
|
case dhcpv4.MessageTypeDiscover:
|
2021-03-18 17:07:13 +03:00
|
|
|
l, err = s.processDiscover(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: processing discover: %s", err)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
if l == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
case dhcpv4.MessageTypeRequest:
|
|
|
|
var toReply bool
|
2021-03-18 17:07:13 +03:00
|
|
|
l, toReply = s.processRequest(req, resp)
|
|
|
|
if l == nil {
|
2020-07-03 18:20:01 +03:00
|
|
|
if toReply {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return -1 // drop packet
|
|
|
|
}
|
2021-05-14 19:16:07 +03:00
|
|
|
case dhcpv4.MessageTypeDecline:
|
|
|
|
err = s.processDecline(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: processing decline: %s", err)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
case dhcpv4.MessageTypeRelease:
|
|
|
|
err = s.processRelease(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: processing release: %s", err)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:16:07 +03:00
|
|
|
if l != nil {
|
|
|
|
resp.YourIPAddr = make([]byte, 4)
|
|
|
|
copy(resp.YourIPAddr, l.IP)
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptIPAddressLeaseTime(s.conf.leaseTime))
|
2021-04-20 15:07:57 +03:00
|
|
|
resp.UpdateOption(dhcpv4.OptRouter(s.conf.subnet.IP))
|
|
|
|
resp.UpdateOption(dhcpv4.OptSubnetMask(s.conf.subnet.Mask))
|
2020-07-03 18:20:01 +03:00
|
|
|
resp.UpdateOption(dhcpv4.OptDNS(s.conf.dnsIPAddrs...))
|
2020-08-25 14:07:11 +03:00
|
|
|
|
|
|
|
for _, opt := range s.conf.options {
|
2021-03-15 19:24:26 +03:00
|
|
|
resp.Options[opt.code] = opt.data
|
2020-08-25 14:07:11 +03:00
|
|
|
}
|
2021-03-18 17:07:13 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// client(0.0.0.0:68) -> (Request:ClientMAC,Type=Discover,ClientID,ReqIP,HostName) -> server(255.255.255.255:67)
|
|
|
|
// client(255.255.255.255:68) <- (Reply:YourIP,ClientMAC,Type=Offer,ServerID,SubnetMask,LeaseTime) <- server(<IP>:67)
|
|
|
|
// client(0.0.0.0:68) -> (Request:ClientMAC,Type=Request,ClientID,ReqIP||ClientIP,HostName,ServerID,ParamReqList) -> server(255.255.255.255:67)
|
|
|
|
// client(255.255.255.255:68) <- (Reply:YourIP,ClientMAC,Type=ACK,ServerID,SubnetMask,LeaseTime) <- server(<IP>:67)
|
|
|
|
func (s *v4Server) packetHandler(conn net.PacketConn, peer net.Addr, req *dhcpv4.DHCPv4) {
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: received message: %s", req.Summary())
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
switch req.MessageType() {
|
|
|
|
case dhcpv4.MessageTypeDiscover,
|
2021-05-14 19:16:07 +03:00
|
|
|
dhcpv4.MessageTypeRequest,
|
|
|
|
dhcpv4.MessageTypeDecline,
|
|
|
|
dhcpv4.MessageTypeRelease:
|
|
|
|
// Go on.
|
2020-07-03 18:20:01 +03:00
|
|
|
default:
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: unsupported message type %d", req.MessageType())
|
2021-05-14 19:16:07 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := dhcpv4.NewReplyFromRequest(req)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: dhcpv4.New: %s", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:36:57 +03:00
|
|
|
err = aghnet.ValidateHardwareAddress(req.ClientHWAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: invalid ClientHWAddr: %s", err)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := s.process(req, resp)
|
|
|
|
if r < 0 {
|
|
|
|
return
|
|
|
|
} else if r == 0 {
|
|
|
|
resp.Options.Update(dhcpv4.OptMessageType(dhcpv4.MessageTypeNak))
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: sending: %s", resp.Summary())
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
_, err = conn.WriteTo(resp.ToBytes(), peer)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Error("dhcpv4: conn.Write to %s failed: %s", peer, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
// Start starts the IPv4 DHCP server.
|
2021-04-21 14:42:19 +03:00
|
|
|
func (s *v4Server) Start() (err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-04-21 14:42:19 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
if !s.conf.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
ifaceName := s.conf.InterfaceName
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
2021-04-21 14:42:19 +03:00
|
|
|
return fmt.Errorf("finding interface %s by name: %w", ifaceName, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: starting...")
|
2020-11-09 19:27:04 +03:00
|
|
|
|
2020-11-27 14:39:43 +03:00
|
|
|
dnsIPAddrs, err := ifaceDNSIPAddrs(iface, ipVersion4, defaultMaxAttempts, defaultBackoff)
|
2020-11-09 19:27:04 +03:00
|
|
|
if err != nil {
|
2021-04-21 14:42:19 +03:00
|
|
|
return fmt.Errorf("interface %s: %w", ifaceName, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 14:42:59 +03:00
|
|
|
if len(dnsIPAddrs) == 0 {
|
|
|
|
// No available IP addresses which may appear later.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
s.conf.dnsIPAddrs = dnsIPAddrs
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
laddr := &net.UDPAddr{
|
2021-01-13 16:56:05 +03:00
|
|
|
IP: net.IP{0, 0, 0, 0},
|
2020-07-03 18:20:01 +03:00
|
|
|
Port: dhcpv4.ServerPort,
|
|
|
|
}
|
|
|
|
s.srv, err = server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Info("dhcpv4: listening")
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
go func() {
|
2021-05-21 14:55:42 +03:00
|
|
|
if serr := s.srv.Serve(); errors.Is(serr, net.ErrClosed) {
|
|
|
|
log.Info("dhcpv4: server is closed")
|
|
|
|
|
|
|
|
return
|
|
|
|
} else if serr != nil {
|
2021-04-21 14:42:19 +03:00
|
|
|
log.Error("dhcpv4: srv.Serve: %s", serr)
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
}()
|
2020-11-09 19:27:04 +03:00
|
|
|
|
2021-04-15 17:52:53 +03:00
|
|
|
// Signal to the clients containers in packages home and dnsforward that
|
|
|
|
// it should reload the DHCP clients.
|
|
|
|
s.conf.notify(LeaseChangedAdded)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop - stop server
|
2021-06-16 16:48:46 +03:00
|
|
|
func (s *v4Server) Stop() (err error) {
|
2020-07-03 18:20:01 +03:00
|
|
|
if s.srv == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:20:57 +03:00
|
|
|
log.Debug("dhcpv4: stopping")
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.srv.Close()
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
2021-06-16 16:48:46 +03:00
|
|
|
return fmt.Errorf("closing dhcpv4 srv: %w", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-04-15 17:52:53 +03:00
|
|
|
|
|
|
|
// Signal to the clients containers in packages home and dnsforward that
|
|
|
|
// it should remove all DHCP clients.
|
|
|
|
s.conf.notify(LeaseChangedRemovedAll)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
s.srv = nil
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return nil
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create DHCPv4 server
|
2021-03-15 19:24:26 +03:00
|
|
|
func v4Create(conf V4ServerConf) (srv DHCPServer, err error) {
|
2020-07-03 18:20:01 +03:00
|
|
|
s := &v4Server{}
|
|
|
|
s.conf = conf
|
2021-04-20 15:07:57 +03:00
|
|
|
s.leaseHosts = aghstrings.NewSet()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-03-17 15:02:17 +03:00
|
|
|
// TODO(a.garipov): Don't use a disabled server in other places or just
|
|
|
|
// use an interface.
|
2020-07-03 18:20:01 +03:00
|
|
|
if !conf.Enabled {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:07:57 +03:00
|
|
|
var routerIP net.IP
|
|
|
|
routerIP, err = tryTo4(s.conf.GatewayIP)
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 16:56:05 +03:00
|
|
|
if s.conf.SubnetMask == nil {
|
|
|
|
return s, fmt.Errorf("dhcpv4: invalid subnet mask: %v", s.conf.SubnetMask)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-04-20 15:07:57 +03:00
|
|
|
|
|
|
|
subnetMask := make([]byte, 4)
|
|
|
|
copy(subnetMask, s.conf.SubnetMask.To4())
|
|
|
|
|
|
|
|
s.conf.subnet = &net.IPNet{
|
|
|
|
IP: routerIP,
|
|
|
|
Mask: subnetMask,
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-03-16 19:11:32 +03:00
|
|
|
s.conf.ipRange, err = newIPRange(conf.RangeStart, conf.RangeEnd)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:07:13 +03:00
|
|
|
s.leasedOffsets = newBitSet()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
if conf.LeaseDuration == 0 {
|
|
|
|
s.conf.leaseTime = time.Hour * 24
|
|
|
|
s.conf.LeaseDuration = uint32(s.conf.leaseTime.Seconds())
|
|
|
|
} else {
|
|
|
|
s.conf.leaseTime = time.Second * time.Duration(conf.LeaseDuration)
|
|
|
|
}
|
|
|
|
|
2021-03-15 19:24:26 +03:00
|
|
|
p := newDHCPOptionParser()
|
|
|
|
|
|
|
|
for i, o := range conf.Options {
|
|
|
|
var code uint8
|
|
|
|
var data []byte
|
|
|
|
code, data, err = p.parse(o)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: bad option string at index %d: %s", i, err)
|
|
|
|
|
2020-08-25 14:07:11 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := dhcpOption{
|
|
|
|
code: code,
|
2021-03-15 19:24:26 +03:00
|
|
|
data: data,
|
2020-08-25 14:07:11 +03:00
|
|
|
}
|
2021-03-15 19:24:26 +03:00
|
|
|
|
2020-08-25 14:07:11 +03:00
|
|
|
s.conf.options = append(s.conf.options, opt)
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return s, nil
|
|
|
|
}
|