2020-07-03 16:20:01 +01:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
2021-03-31 10:36:57 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2020-07-03 16:20:01 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-11-20 10:44:21 +00:00
|
|
|
"github.com/go-ping/ping"
|
2020-07-03 16:20:01 +01:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4/server4"
|
|
|
|
)
|
|
|
|
|
2020-11-27 11:39:43 +00:00
|
|
|
// v4Server is a DHCPv4 server.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Think about unifying this and v6Server.
|
2020-07-03 16:20:01 +01:00
|
|
|
type v4Server struct {
|
|
|
|
conf V4ServerConf
|
2021-03-16 16:11:32 +00:00
|
|
|
srv *server4.Server
|
|
|
|
|
|
|
|
// leasedOffsets contains offsets from conf.ipRange.start that have been
|
|
|
|
// leased.
|
2021-03-18 14:07:13 +00:00
|
|
|
leasedOffsets *bitSet
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
// leases contains all dynamic and static leases.
|
|
|
|
leases []*Lease
|
|
|
|
|
|
|
|
// leasesLock protects leases and leasedOffsets.
|
|
|
|
leasesLock sync.Mutex
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig4 - write configuration
|
|
|
|
func (s *v4Server) WriteDiskConfig4(c *V4ServerConf) {
|
|
|
|
*c = s.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig6 - write configuration
|
|
|
|
func (s *v4Server) WriteDiskConfig6(c *V6ServerConf) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetLeases - reset leases
|
|
|
|
func (s *v4Server) ResetLeases(leases []*Lease) {
|
|
|
|
s.leases = nil
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
r := s.conf.ipRange
|
2020-07-03 16:20:01 +01:00
|
|
|
for _, l := range leases {
|
2021-03-18 14:07:13 +00:00
|
|
|
if !l.IsStatic() && !r.contains(l.IP) {
|
|
|
|
log.Debug(
|
|
|
|
"dhcpv4: skipping lease %s (%s): not within current ip range",
|
|
|
|
l.IP,
|
|
|
|
l.HWAddr,
|
|
|
|
)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
err := s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(a.garipov): Better error handling.
|
|
|
|
log.Error("dhcpv4: adding a lease for %s (%s): %s", l.IP, l.HWAddr, err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
// getLeasesRef returns the actual leases slice. For internal use only.
|
|
|
|
func (s *v4Server) getLeasesRef() []*Lease {
|
2020-07-03 16:20:01 +01:00
|
|
|
return s.leases
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00: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 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
// GetLeases returns the list of current DHCP leases. It is safe for concurrent
|
|
|
|
// use.
|
|
|
|
func (s *v4Server) GetLeases(flags int) (res []Lease) {
|
|
|
|
// 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.
|
|
|
|
res = []Lease{}
|
|
|
|
|
|
|
|
// TODO(a.garipov): Remove the silly bit twiddling and make GetLeases
|
|
|
|
// accept booleans. Seriously, this doesn't even save stack space.
|
|
|
|
getDynamic := flags&LeasesDynamic != 0
|
|
|
|
getStatic := flags&LeasesStatic != 0
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
2021-03-18 14:07:13 +00:00
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if getDynamic && l.Expiry.After(now) && !s.isBlocklisted(l) {
|
|
|
|
res = append(res, *l)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if getStatic && l.IsStatic() {
|
|
|
|
res = append(res, *l)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return res
|
2020-07-03 16:20:01 +01: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 14:07:13 +00:00
|
|
|
now := time.Now()
|
2020-07-03 16:20:01 +01: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 14:07:13 +00:00
|
|
|
if l.Expiry.After(now) || l.IsStatic() {
|
2020-07-03 16:20:01 +01:00
|
|
|
return l.HWAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 12:02:17 +00:00
|
|
|
// defaultHwAddrLen is the default length of a hardware (MAC) address.
|
|
|
|
const defaultHwAddrLen = 6
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
// Add the specified IP to the black list for a time period
|
2021-03-17 12:02:17 +00: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 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
// rmLeaseByIndex removes a lease by its index in the leases slice.
|
|
|
|
func (s *v4Server) rmLeaseByIndex(i int) {
|
2021-03-17 12:02:17 +00: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 16:11:32 +00:00
|
|
|
l := s.leases[i]
|
|
|
|
s.leases = append(s.leases[:i], s.leases[i+1:]...)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-03-17 12:02:17 +00:00
|
|
|
n = len(s.leases)
|
|
|
|
if n > 0 {
|
|
|
|
s.leases = s.leases[:n-1]
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
r := s.conf.ipRange
|
|
|
|
offset, ok := r.offset(l.IP)
|
|
|
|
if ok {
|
2021-03-18 14:07:13 +00:00
|
|
|
s.leasedOffsets.set(offset, false)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
log.Debug("dhcpv4: removed lease %s (%s)", l.IP, l.HWAddr)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a dynamic lease with the same properties
|
|
|
|
// Return error if a static lease is found
|
2021-03-18 14:07:13 +00:00
|
|
|
func (s *v4Server) rmDynamicLease(lease *Lease) (err error) {
|
2020-07-03 16:20:01 +01:00
|
|
|
for i := 0; i < len(s.leases); i++ {
|
|
|
|
l := s.leases[i]
|
|
|
|
|
|
|
|
if bytes.Equal(l.HWAddr, lease.HWAddr) {
|
2021-03-18 14:07:13 +00:00
|
|
|
if l.IsStatic() {
|
2020-07-03 16:20:01 +01:00
|
|
|
return fmt.Errorf("static lease already exists")
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
s.rmLeaseByIndex(i)
|
2020-08-25 11:38:52 +01:00
|
|
|
if i == len(s.leases) {
|
|
|
|
break
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
l = s.leases[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
if net.IP.Equal(l.IP, lease.IP) {
|
2021-03-18 14:07:13 +00:00
|
|
|
if l.IsStatic() {
|
2020-07-03 16:20:01 +01:00
|
|
|
return fmt.Errorf("static lease already exists")
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
s.rmLeaseByIndex(i)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
func (s *v4Server) addStaticLease(l *Lease) (err error) {
|
|
|
|
subnet := &net.IPNet{
|
|
|
|
IP: s.conf.routerIP,
|
|
|
|
Mask: s.conf.subnetMask,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !subnet.Contains(l.IP) {
|
|
|
|
return fmt.Errorf("subnet %s does not contain the ip %q", subnet, l.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.leases = append(s.leases, l)
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
r := s.conf.ipRange
|
|
|
|
offset, ok := r.offset(l.IP)
|
2021-03-18 14:07:13 +00:00
|
|
|
if ok {
|
|
|
|
s.leasedOffsets.set(offset, true)
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v4Server) addDynamicLease(l *Lease) (err error) {
|
|
|
|
r := s.conf.ipRange
|
|
|
|
offset, ok := r.offset(l.IP)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("lease %s (%s) out of range, not adding", l.IP, l.HWAddr)
|
2021-03-16 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
s.leases = append(s.leases, l)
|
2021-03-18 14:07:13 +00:00
|
|
|
s.leasedOffsets.set(offset, true)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addLease adds a dynamic or static lease.
|
|
|
|
func (s *v4Server) addLease(l *Lease) (err error) {
|
|
|
|
if l.IsStatic() {
|
|
|
|
return s.addStaticLease(l)
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return s.addDynamicLease(l)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a lease with the same properties
|
|
|
|
func (s *v4Server) rmLease(lease Lease) error {
|
2021-03-18 14:07:13 +00:00
|
|
|
if len(s.leases) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
for i, l := range s.leases {
|
2021-03-16 16:11:32 +00:00
|
|
|
if l.IP.Equal(lease.IP) {
|
2021-03-18 14:07:13 +00: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 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
s.rmLeaseByIndex(i)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return agherr.Error("lease not found")
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
// AddStaticLease adds a static lease. It is safe for concurrent use.
|
|
|
|
func (s *v4Server) AddStaticLease(l Lease) (err error) {
|
|
|
|
defer agherr.Annotate("dhcpv4: %w", &err)
|
|
|
|
|
|
|
|
if ip4 := l.IP.To4(); ip4 == nil {
|
|
|
|
return fmt.Errorf("invalid ip %q, only ipv4 is supported", l.IP)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2021-03-31 10:36:57 +01:00
|
|
|
err = aghnet.ValidateHardwareAddress(l.HWAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating lease: %w", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
l.Expiry = time.Unix(leaseExpireStatic, 0)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
err = s.rmDynamicLease(&l)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"removing dynamic leases for %s (%s): %w",
|
|
|
|
l.IP,
|
|
|
|
l.HWAddr,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.addLease(&l)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("adding static lease for %s (%s): %w", l.IP, l.HWAddr, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2020-07-03 16:20:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2020-07-03 16:20:01 +01:00
|
|
|
s.conf.notify(LeaseChangedAddedStatic)
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:36:57 +01:00
|
|
|
// RemoveStaticLease removes a static lease. It is safe for concurrent use.
|
|
|
|
func (s *v4Server) RemoveStaticLease(l Lease) (err error) {
|
|
|
|
defer agherr.Annotate("dhcpv4: %w", &err)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
if len(l.IP) != 4 {
|
|
|
|
return fmt.Errorf("invalid IP")
|
|
|
|
}
|
2021-03-31 10:36:57 +01:00
|
|
|
|
|
|
|
err = aghnet.ValidateHardwareAddress(l.HWAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating lease: %w", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
2021-03-31 10:36:57 +01:00
|
|
|
err = s.rmLease(l)
|
2020-07-03 16:20:01 +01:00
|
|
|
if err != nil {
|
|
|
|
s.leasesLock.Unlock()
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.leasesLock.Unlock()
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2020-07-03 16:20:01 +01:00
|
|
|
s.conf.notify(LeaseChangedRemovedStatic)
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send ICMP to the specified machine
|
|
|
|
// Return TRUE if it doesn't reply, which probably means that the IP is available
|
|
|
|
func (s *v4Server) addrAvailable(target net.IP) bool {
|
|
|
|
if s.conf.ICMPTimeout == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger, err := ping.NewPinger(target.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ping.NewPinger(): %v", err)
|
2020-11-20 10:44:21 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger.SetPrivileged(true)
|
|
|
|
pinger.Timeout = time.Duration(s.conf.ICMPTimeout) * time.Millisecond
|
|
|
|
pinger.Count = 1
|
|
|
|
reply := false
|
2021-03-16 16:11:32 +00:00
|
|
|
pinger.OnRecv = func(_ *ping.Packet) {
|
2020-07-03 16:20:01 +01:00
|
|
|
reply = true
|
|
|
|
}
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: Sending ICMP Echo to %v", target)
|
2020-11-20 10:44:21 +00:00
|
|
|
|
|
|
|
err = pinger.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("pinger.Run(): %v", err)
|
|
|
|
return true
|
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
if reply {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Info("dhcpv4: IP conflict: %v is already used by another device", target)
|
2020-07-03 16:20:01 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: ICMP procedure is complete: %v", target)
|
2020-07-03 16:20:01 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00: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 16:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00: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 16:20:01 +01:00
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return !s.leasedOffsets.isSet(offset)
|
2021-03-16 16:11:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return ip.To4()
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find an expired lease and return its index or -1
|
|
|
|
func (s *v4Server) findExpiredLease() int {
|
2021-03-18 14:07:13 +00:00
|
|
|
now := time.Now()
|
2020-07-03 16:20:01 +01:00
|
|
|
for i, lease := range s.leases {
|
2021-03-18 14:07:13 +00:00
|
|
|
if !lease.IsStatic() && lease.Expiry.Before(now) {
|
2020-07-03 16:20:01 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:32 +00: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 14:07:13 +00:00
|
|
|
func (s *v4Server) reserveLease(mac net.HardwareAddr) (l *Lease, err error) {
|
2021-03-16 16:11:32 +00:00
|
|
|
l = &Lease{
|
2021-03-17 12:02:17 +00:00
|
|
|
HWAddr: make([]byte, len(mac)),
|
2021-03-16 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
copy(l.HWAddr, mac)
|
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
l.IP = s.nextIP()
|
2020-07-03 16:20:01 +01:00
|
|
|
if l.IP == nil {
|
|
|
|
i := s.findExpiredLease()
|
|
|
|
if i < 0 {
|
2021-03-18 14:07:13 +00:00
|
|
|
return nil, nil
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
copy(s.leases[i].HWAddr, mac)
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return s.leases[i], nil
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
err = s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
return l, nil
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v4Server) commitLease(l *Lease) {
|
|
|
|
l.Expiry = time.Now().Add(s.conf.leaseTime)
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
s.conf.notify(LeaseChangedAdded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Discover request and return lease
|
2021-03-18 14:07:13 +00:00
|
|
|
func (s *v4Server) processDiscover(req, resp *dhcpv4.DHCPv4) (l *Lease, err error) {
|
2020-07-03 16:20:01 +01:00
|
|
|
mac := req.ClientHWAddr
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
// TODO(a.garipov): Refactor this mess.
|
|
|
|
l = s.findLease(mac)
|
|
|
|
if l == nil {
|
2020-07-03 16:20:01 +01:00
|
|
|
toStore := false
|
2021-03-18 14:07:13 +00:00
|
|
|
for l == nil {
|
|
|
|
l, err = s.reserveLease(mac)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reserving a lease: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l == nil {
|
|
|
|
log.Debug("dhcpv4: no more ip addresses")
|
2020-07-03 16:20:01 +01:00
|
|
|
if toStore {
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
|
|
|
// TODO(a.garipov): Return a special error?
|
|
|
|
return nil, nil
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toStore = true
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
if !s.addrAvailable(l.IP) {
|
|
|
|
s.blocklistLease(l)
|
|
|
|
l = nil
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
} else {
|
2021-03-12 10:34:43 +00:00
|
|
|
reqIP := req.RequestedIPAddress()
|
2021-03-18 14:07:13 +00:00
|
|
|
if len(reqIP) != 0 && !reqIP.Equal(l.IP) {
|
|
|
|
log.Debug("dhcpv4: different RequestedIP: %s != %s", reqIP, l.IP)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
|
2021-03-18 14:07:13 +00:00
|
|
|
|
|
|
|
return l, nil
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 11:01:55 +01: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
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
// Process Request request and return lease
|
|
|
|
// Return false if we don't need to reply
|
2020-11-09 16:27:04 +00:00
|
|
|
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
|
2020-07-03 16:20:01 +01:00
|
|
|
var lease *Lease
|
|
|
|
mac := req.ClientHWAddr
|
2021-03-12 10:34:43 +00:00
|
|
|
reqIP := req.RequestedIPAddress()
|
2020-07-03 16:20:01 +01:00
|
|
|
if reqIP == nil {
|
|
|
|
reqIP = req.ClientIPAddr
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:34:43 +00:00
|
|
|
sid := req.ServerIdentifier()
|
|
|
|
if len(sid) != 0 && !sid.Equal(s.conf.dnsIPAddrs[0]) {
|
|
|
|
log.Debug("dhcpv4: bad OptionServerIdentifier in request message for %s", mac)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:34:43 +00:00
|
|
|
if ip4 := reqIP.To4(); ip4 == nil {
|
|
|
|
log.Debug("dhcpv4: bad OptionRequestedIPAddress in request message for %s", mac)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
for _, l := range s.leases {
|
|
|
|
if bytes.Equal(l.HWAddr, mac) {
|
2021-03-12 10:34:43 +00:00
|
|
|
if !l.IP.Equal(reqIP) {
|
2020-07-03 16:20:01 +01:00
|
|
|
s.leasesLock.Unlock()
|
2021-03-12 10:34:43 +00:00
|
|
|
log.Debug("dhcpv4: mismatched OptionRequestedIPAddress in request message for %s", mac)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
lease = l
|
2021-03-12 10:34:43 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
if lease == nil {
|
2021-03-12 10:34:43 +00:00
|
|
|
log.Debug("dhcpv4: no lease for %s", mac)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
if !lease.IsStatic() {
|
2021-03-12 10:34:43 +00:00
|
|
|
lease.Hostname = req.HostName()
|
2020-07-03 16:20:01 +01:00
|
|
|
s.commitLease(lease)
|
2020-08-24 11:01:55 +01:00
|
|
|
} else if len(lease.Hostname) != 0 {
|
|
|
|
o := &optFQDN{
|
2020-08-25 15:44:30 +01:00
|
|
|
name: lease.Hostname,
|
2020-08-24 11:01:55 +01:00
|
|
|
}
|
|
|
|
fqdn := dhcpv4.Option{
|
|
|
|
Code: dhcpv4.OptionFQDN,
|
|
|
|
Value: o,
|
|
|
|
}
|
2021-03-12 10:34:43 +00:00
|
|
|
|
2020-08-24 11:01:55 +01:00
|
|
|
resp.UpdateOption(fqdn)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
2021-03-12 10:34:43 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return lease, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:27:04 +00:00
|
|
|
func (s *v4Server) process(req, resp *dhcpv4.DHCPv4) int {
|
2021-03-18 14:07:13 +00:00
|
|
|
var err error
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptServerIdentifier(s.conf.dnsIPAddrs[0]))
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
var l *Lease
|
2020-07-03 16:20:01 +01:00
|
|
|
switch req.MessageType() {
|
|
|
|
case dhcpv4.MessageTypeDiscover:
|
2021-03-18 14:07:13 +00:00
|
|
|
l, err = s.processDiscover(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: processing discover: %s", err)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
if l == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
case dhcpv4.MessageTypeRequest:
|
|
|
|
var toReply bool
|
2021-03-18 14:07:13 +00:00
|
|
|
l, toReply = s.processRequest(req, resp)
|
|
|
|
if l == nil {
|
2020-07-03 16:20:01 +01:00
|
|
|
if toReply {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return -1 // drop packet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.YourIPAddr = make([]byte, 4)
|
2021-03-18 14:07:13 +00:00
|
|
|
copy(resp.YourIPAddr, l.IP)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptIPAddressLeaseTime(s.conf.leaseTime))
|
|
|
|
resp.UpdateOption(dhcpv4.OptRouter(s.conf.routerIP))
|
|
|
|
resp.UpdateOption(dhcpv4.OptSubnetMask(s.conf.subnetMask))
|
|
|
|
resp.UpdateOption(dhcpv4.OptDNS(s.conf.dnsIPAddrs...))
|
2020-08-25 12:07:11 +01:00
|
|
|
|
|
|
|
for _, opt := range s.conf.options {
|
2021-03-15 16:24:26 +00:00
|
|
|
resp.Options[opt.code] = opt.data
|
2020-08-25 12:07:11 +01:00
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01: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 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: received message: %s", req.Summary())
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
switch req.MessageType() {
|
|
|
|
case dhcpv4.MessageTypeDiscover,
|
|
|
|
dhcpv4.MessageTypeRequest:
|
|
|
|
//
|
|
|
|
|
|
|
|
default:
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: unsupported message type %d", req.MessageType())
|
2020-07-03 16:20:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := dhcpv4.NewReplyFromRequest(req)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: dhcpv4.New: %s", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:36:57 +01:00
|
|
|
err = aghnet.ValidateHardwareAddress(req.ClientHWAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: invalid ClientHWAddr: %s", err)
|
|
|
|
|
2020-07-03 16:20:01 +01: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 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: sending: %s", resp.Summary())
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
_, err = conn.WriteTo(resp.ToBytes(), peer)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Error("dhcpv4: conn.Write to %s failed: %s", peer, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
// Start starts the IPv4 DHCP server.
|
2020-07-03 16:20:01 +01:00
|
|
|
func (s *v4Server) Start() error {
|
|
|
|
if !s.conf.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
ifaceName := s.conf.InterfaceName
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
2020-07-03 16:20:01 +01:00
|
|
|
if err != nil {
|
2020-11-09 16:27:04 +00:00
|
|
|
return fmt.Errorf("dhcpv4: finding interface %s by name: %w", ifaceName, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: starting...")
|
2020-11-09 16:27:04 +00:00
|
|
|
|
2020-11-27 11:39:43 +00:00
|
|
|
dnsIPAddrs, err := ifaceDNSIPAddrs(iface, ipVersion4, defaultMaxAttempts, defaultBackoff)
|
2020-11-09 16:27:04 +00:00
|
|
|
if err != nil {
|
2020-11-27 11:39:43 +00:00
|
|
|
return fmt.Errorf("dhcpv4: interface %s: %w", ifaceName, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 11:42:59 +00:00
|
|
|
if len(dnsIPAddrs) == 0 {
|
|
|
|
// No available IP addresses which may appear later.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
s.conf.dnsIPAddrs = dnsIPAddrs
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
laddr := &net.UDPAddr{
|
2021-01-13 13:56:05 +00:00
|
|
|
IP: net.IP{0, 0, 0, 0},
|
2020-07-03 16:20:01 +01:00
|
|
|
Port: dhcpv4.ServerPort,
|
|
|
|
}
|
|
|
|
s.srv, err = server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Info("dhcpv4: listening")
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
err = s.srv.Serve()
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: srv.Serve: %s", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}()
|
2020-11-09 16:27:04 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop - stop server
|
|
|
|
func (s *v4Server) Stop() {
|
|
|
|
if s.srv == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("dhcpv4: stopping")
|
2020-07-03 16:20:01 +01:00
|
|
|
err := s.srv.Close()
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Error("dhcpv4: srv.Close: %s", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
// now s.srv.Serve() will return
|
|
|
|
s.srv = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create DHCPv4 server
|
2021-03-15 16:24:26 +00:00
|
|
|
func v4Create(conf V4ServerConf) (srv DHCPServer, err error) {
|
2020-07-03 16:20:01 +01:00
|
|
|
s := &v4Server{}
|
|
|
|
s.conf = conf
|
|
|
|
|
2021-03-17 12:02:17 +00:00
|
|
|
// TODO(a.garipov): Don't use a disabled server in other places or just
|
|
|
|
// use an interface.
|
2020-07-03 16:20:01 +01:00
|
|
|
if !conf.Enabled {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
s.conf.routerIP, err = tryTo4(s.conf.GatewayIP)
|
2020-07-03 16:20:01 +01:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
if s.conf.SubnetMask == nil {
|
|
|
|
return s, fmt.Errorf("dhcpv4: invalid subnet mask: %v", s.conf.SubnetMask)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
s.conf.subnetMask = make([]byte, 4)
|
2021-01-13 13:56:05 +00:00
|
|
|
copy(s.conf.subnetMask, s.conf.SubnetMask.To4())
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
s.conf.ipRange, err = newIPRange(conf.RangeStart, conf.RangeEnd)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:07:13 +00:00
|
|
|
s.leasedOffsets = newBitSet()
|
2020-07-03 16:20:01 +01: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 16:24:26 +00: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 12:07:11 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := dhcpOption{
|
|
|
|
code: code,
|
2021-03-15 16:24:26 +00:00
|
|
|
data: data,
|
2020-08-25 12:07:11 +01:00
|
|
|
}
|
2021-03-15 16:24:26 +00:00
|
|
|
|
2020-08-25 12:07:11 +01:00
|
|
|
s.conf.options = append(s.conf.options, opt)
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return s, nil
|
|
|
|
}
|