2021-05-21 04:55:42 -07:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 09:42:41 -07:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 04:55:42 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-13 06:00:09 -07:00
|
|
|
"strings"
|
2020-07-03 08:20:01 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-03-31 02:36:57 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-24 07:28:11 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-07-03 08:20:01 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 06:03:37 -07:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 07:40:31 -07:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-09-30 11:17:54 -07:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2020-11-20 03:44:21 -07:00
|
|
|
"github.com/go-ping/ping"
|
2020-07-03 08:20:01 -07:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4/server4"
|
2022-08-23 08:22:49 -07:00
|
|
|
"golang.org/x/exp/slices"
|
2022-08-15 08:31:32 -07:00
|
|
|
|
|
|
|
//lint:ignore SA1019 See the TODO in go.mod.
|
|
|
|
"github.com/mdlayher/raw"
|
2020-07-03 08:20:01 -07:00
|
|
|
)
|
|
|
|
|
2020-11-27 04:39:43 -07:00
|
|
|
// v4Server is a DHCPv4 server.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Think about unifying this and v6Server.
|
2020-07-03 08:20:01 -07:00
|
|
|
type v4Server struct {
|
|
|
|
conf V4ServerConf
|
2021-03-16 09:11:32 -07:00
|
|
|
srv *server4.Server
|
|
|
|
|
|
|
|
// leasedOffsets contains offsets from conf.ipRange.start that have been
|
|
|
|
// leased.
|
2021-03-18 07:07:13 -07:00
|
|
|
leasedOffsets *bitSet
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-04-20 05:07:57 -07:00
|
|
|
// leaseHosts is the set of all hostnames of all known DHCP clients.
|
2021-07-29 07:40:31 -07:00
|
|
|
leaseHosts *stringutil.Set
|
2021-04-20 05:07:57 -07:00
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
// leases contains all dynamic and static leases.
|
|
|
|
leases []*Lease
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
// leasesLock protects leases, leaseHosts, and leasedOffsets.
|
2021-03-16 09:11:32 -07:00
|
|
|
leasesLock sync.Mutex
|
2021-09-13 10:05:41 -07:00
|
|
|
|
|
|
|
// options holds predefined DHCP options to return to clients.
|
|
|
|
options dhcpv4.Options
|
2020-07-03 08:20:01 -07: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 03:02:48 -07: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 07:28:11 -07:00
|
|
|
defer func() { err = errors.Annotate(err, "normalizing %q: %w", hostname) }()
|
2021-05-06 03:02:48 -07:00
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
norm = strings.ToLower(hostname)
|
|
|
|
parts := strings.FieldsFunc(norm, func(c rune) (ok bool) {
|
2021-08-09 06:03:37 -07:00
|
|
|
return c != '.' && !netutil.IsValidHostOuterRune(c)
|
2021-05-06 03:02:48 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return "", fmt.Errorf("no valid parts")
|
|
|
|
}
|
|
|
|
|
|
|
|
norm = strings.Join(parts, "-")
|
|
|
|
norm = strings.TrimSuffix(norm, "-")
|
|
|
|
|
|
|
|
return norm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 05:17:42 -07: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 03:02:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = aghnet.GenerateHostname(ip)
|
|
|
|
}
|
|
|
|
|
2021-08-09 06:03:37 -07:00
|
|
|
err = netutil.ValidateDomainName(hostname)
|
2021-05-13 05:17:42 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Info("dhcpv4: %s", err)
|
|
|
|
hostname = ""
|
2021-05-06 03:02:48 -07:00
|
|
|
}
|
|
|
|
|
2021-05-13 05:17:42 -07:00
|
|
|
return hostname
|
2021-05-06 03:02:48 -07:00
|
|
|
}
|
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
// ResetLeases resets leases.
|
|
|
|
func (s *v4Server) ResetLeases(leases []*Lease) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-04-29 04:52:22 -07:00
|
|
|
|
2021-04-21 04:42:19 -07:00
|
|
|
if !s.conf.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.leasedOffsets = newBitSet()
|
2021-07-29 07:40:31 -07:00
|
|
|
s.leaseHosts = stringutil.NewSet()
|
2020-07-03 08:20:01 -07:00
|
|
|
s.leases = nil
|
|
|
|
|
|
|
|
for _, l := range leases {
|
2021-06-10 04:54:47 -07:00
|
|
|
if !l.IsStatic() {
|
|
|
|
l.Hostname = s.validHostnameForClient(l.Hostname, l.IP)
|
|
|
|
}
|
2021-04-29 04:52:22 -07:00
|
|
|
err = s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(a.garipov): Wrap and bubble up the error.
|
2021-04-20 05:07:57 -07:00
|
|
|
log.Error(
|
|
|
|
"dhcpv4: reset: re-adding a lease for %s (%s): %s",
|
|
|
|
l.IP,
|
|
|
|
l.HWAddr,
|
|
|
|
err,
|
|
|
|
)
|
2021-03-18 07:07:13 -07:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-06-16 06:48:46 -07:00
|
|
|
|
|
|
|
return nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
// getLeasesRef returns the actual leases slice. For internal use only.
|
|
|
|
func (s *v4Server) getLeasesRef() []*Lease {
|
2020-07-03 08:20:01 -07:00
|
|
|
return s.leases
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07: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 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
// GetLeases returns the list of current DHCP leases. It is safe for concurrent
|
|
|
|
// use.
|
2021-06-16 06:48:46 -07:00
|
|
|
func (s *v4Server) GetLeases(flags GetLeasesFlags) (leases []*Lease) {
|
2021-03-18 07:07:13 -07: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 06:48:46 -07:00
|
|
|
leases = []*Lease{}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
|
|
|
getDynamic := flags&LeasesDynamic != 0
|
|
|
|
getStatic := flags&LeasesStatic != 0
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
2021-03-18 07:07:13 -07: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 06:48:46 -07:00
|
|
|
leases = append(leases, l.Clone())
|
2021-03-18 07:07:13 -07:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if getStatic && l.IsStatic() {
|
2021-06-16 06:48:46 -07:00
|
|
|
leases = append(leases, l.Clone())
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
return leases
|
2020-07-03 08:20:01 -07: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 07:07:13 -07:00
|
|
|
now := time.Now()
|
2020-07-03 08:20:01 -07: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 07:07:13 -07:00
|
|
|
if l.Expiry.After(now) || l.IsStatic() {
|
2020-07-03 08:20:01 -07:00
|
|
|
return l.HWAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:02:17 -07:00
|
|
|
// defaultHwAddrLen is the default length of a hardware (MAC) address.
|
|
|
|
const defaultHwAddrLen = 6
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
// Add the specified IP to the black list for a time period
|
2021-03-17 05:02:17 -07: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 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
// rmLeaseByIndex removes a lease by its index in the leases slice.
|
|
|
|
func (s *v4Server) rmLeaseByIndex(i int) {
|
2021-03-17 05:02:17 -07: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 09:11:32 -07:00
|
|
|
l := s.leases[i]
|
|
|
|
s.leases = append(s.leases[:i], s.leases[i+1:]...)
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
r := s.conf.ipRange
|
|
|
|
offset, ok := r.offset(l.IP)
|
|
|
|
if ok {
|
2021-03-18 07:07:13 -07:00
|
|
|
s.leasedOffsets.set(offset, false)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-04-20 05:07:57 -07:00
|
|
|
s.leaseHosts.Del(l.Hostname)
|
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
log.Debug("dhcpv4: removed lease %s (%s)", l.IP, l.HWAddr)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a dynamic lease with the same properties
|
|
|
|
// Return error if a static lease is found
|
2021-03-18 07:07:13 -07:00
|
|
|
func (s *v4Server) rmDynamicLease(lease *Lease) (err error) {
|
2022-08-23 08:22:49 -07:00
|
|
|
for i, l := range s.leases {
|
|
|
|
isStatic := l.IsStatic()
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
if bytes.Equal(l.HWAddr, lease.HWAddr) || l.IP.Equal(lease.IP) {
|
|
|
|
if isStatic {
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("static lease already exists")
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
s.rmLeaseByIndex(i)
|
2021-05-13 05:17:42 -07:00
|
|
|
if i == len(s.leases) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
l = s.leases[i]
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
if !isStatic && l.Hostname == lease.Hostname {
|
2021-05-13 05:17:42 -07:00
|
|
|
l.Hostname = ""
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
// ErrDupHostname is returned by addLease when the added lease has a not empty
|
|
|
|
// non-unique hostname.
|
|
|
|
const ErrDupHostname = errors.Error("hostname is not unique")
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
// addLease adds a dynamic or static lease.
|
|
|
|
func (s *v4Server) addLease(l *Lease) (err error) {
|
2021-03-16 09:11:32 -07:00
|
|
|
r := s.conf.ipRange
|
2021-05-06 03:02:48 -07:00
|
|
|
offset, inOffset := r.offset(l.IP)
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
if l.IsStatic() {
|
2021-10-19 09:28:18 -07:00
|
|
|
// TODO(a.garipov, d.seregin): Subnet can be nil when dhcp server is
|
|
|
|
// disabled.
|
2021-05-06 03:02:48 -07:00
|
|
|
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 07:07:13 -07:00
|
|
|
return fmt.Errorf("lease %s (%s) out of range, not adding", l.IP, l.HWAddr)
|
2021-03-16 09:11:32 -07:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
if l.Hostname != "" {
|
2022-08-23 08:22:49 -07:00
|
|
|
if s.leaseHosts.Has(l.Hostname) {
|
|
|
|
return ErrDupHostname
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
s.leaseHosts.Add(l.Hostname)
|
2021-03-18 07:07:13 -07:00
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
s.leases = append(s.leases, l)
|
|
|
|
s.leasedOffsets.set(offset, true)
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
return nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
// rmLease removes a lease with the same properties.
|
2021-06-16 06:48:46 -07:00
|
|
|
func (s *v4Server) rmLease(lease *Lease) (err error) {
|
2021-03-18 07:07:13 -07:00
|
|
|
if len(s.leases) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
for i, l := range s.leases {
|
2021-03-16 09:11:32 -07:00
|
|
|
if l.IP.Equal(lease.IP) {
|
2021-03-18 07:07:13 -07: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 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
s.rmLeaseByIndex(i)
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("lease not found")
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-07-08 05:17:47 -07:00
|
|
|
// AddStaticLease implements the DHCPServer interface for *v4Server. It is safe
|
|
|
|
// for concurrent use.
|
2021-06-16 06:48:46 -07:00
|
|
|
func (s *v4Server) AddStaticLease(l *Lease) (err error) {
|
2021-05-24 07:28:11 -07:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: adding static lease: %w") }()
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2022-07-08 05:17:47 -07:00
|
|
|
ip := l.IP.To4()
|
|
|
|
if ip == nil {
|
2021-03-18 07:07:13 -07:00
|
|
|
return fmt.Errorf("invalid ip %q, only ipv4 is supported", l.IP)
|
2022-07-08 05:17:47 -07:00
|
|
|
} else if gwIP := s.conf.GatewayIP; gwIP.Equal(ip) {
|
|
|
|
return fmt.Errorf("can't assign the gateway IP %s to the lease", gwIP)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2021-04-20 05:07:57 -07:00
|
|
|
l.Expiry = time.Unix(leaseExpireStatic, 0)
|
|
|
|
|
2021-08-09 06:03:37 -07:00
|
|
|
err = netutil.ValidateMAC(l.HWAddr)
|
2021-05-06 03:02:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:54:47 -07:00
|
|
|
if hostname := l.Hostname; hostname != "" {
|
|
|
|
hostname, err = normalizeHostname(hostname)
|
2021-05-13 05:17:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-08-09 06:03:37 -07:00
|
|
|
err = netutil.ValidateDomainName(hostname)
|
2021-05-13 05:17:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating hostname: %w", err)
|
|
|
|
}
|
2021-05-06 03:02:48 -07:00
|
|
|
|
2022-08-23 08:22:49 -07: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. In case a static
|
|
|
|
// lease with the same name already exists, addLease will return an
|
|
|
|
// error and the lease won't be added.
|
2021-05-06 03:02:48 -07:00
|
|
|
|
2021-05-13 05:17:42 -07:00
|
|
|
l.Hostname = hostname
|
|
|
|
}
|
2021-05-06 03:02:48 -07:00
|
|
|
|
2021-03-18 07:07:13 -07: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 06:48:46 -07:00
|
|
|
err = s.rmDynamicLease(l)
|
2021-03-18 07:07:13 -07:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"removing dynamic leases for %s (%s): %w",
|
2022-07-08 05:17:47 -07:00
|
|
|
ip,
|
2021-03-18 07:07:13 -07:00
|
|
|
l.HWAddr,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
err = s.addLease(l)
|
2021-03-18 07:07:13 -07:00
|
|
|
if err != nil {
|
2022-07-08 05:17:47 -07:00
|
|
|
err = fmt.Errorf("adding static lease for %s (%s): %w", ip, l.HWAddr, err)
|
2021-03-18 07:07:13 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2020-07-03 08:20:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
2020-07-03 08:20:01 -07:00
|
|
|
s.conf.notify(LeaseChangedAddedStatic)
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 02:36:57 -07:00
|
|
|
// RemoveStaticLease removes a static lease. It is safe for concurrent use.
|
2021-06-16 06:48:46 -07:00
|
|
|
func (s *v4Server) RemoveStaticLease(l *Lease) (err error) {
|
2021-05-24 07:28:11 -07:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-03-31 02:36:57 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
if len(l.IP) != 4 {
|
|
|
|
return fmt.Errorf("invalid IP")
|
|
|
|
}
|
2021-03-31 02:36:57 -07:00
|
|
|
|
2021-08-09 06:03:37 -07:00
|
|
|
err = netutil.ValidateMAC(l.HWAddr)
|
2021-03-31 02:36:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating lease: %w", err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
s.conf.notify(LeaseChangedRemovedStatic)
|
|
|
|
}()
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
return s.rmLease(l)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:02:48 -07: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 08:20:01 -07:00
|
|
|
if s.conf.ICMPTimeout == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger, err := ping.NewPinger(target.String())
|
|
|
|
if err != nil {
|
2021-05-06 03:02:48 -07:00
|
|
|
log.Error("dhcpv4: ping.NewPinger(): %s", err)
|
2020-11-20 03:44:21 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pinger.SetPrivileged(true)
|
|
|
|
pinger.Timeout = time.Duration(s.conf.ICMPTimeout) * time.Millisecond
|
|
|
|
pinger.Count = 1
|
|
|
|
reply := false
|
2021-03-16 09:11:32 -07:00
|
|
|
pinger.OnRecv = func(_ *ping.Packet) {
|
2020-07-03 08:20:01 -07:00
|
|
|
reply = true
|
|
|
|
}
|
2021-05-06 03:02:48 -07:00
|
|
|
|
|
|
|
log.Debug("dhcpv4: sending icmp echo to %s", target)
|
2020-11-20 03:44:21 -07:00
|
|
|
|
|
|
|
err = pinger.Run()
|
|
|
|
if err != nil {
|
2021-05-06 03:02:48 -07:00
|
|
|
log.Error("dhcpv4: pinger.Run(): %s", err)
|
|
|
|
|
2020-11-20 03:44:21 -07:00
|
|
|
return true
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
if reply {
|
2021-05-06 03:02:48 -07:00
|
|
|
log.Info("dhcpv4: ip conflict: %s is already used by another device", target)
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
log.Debug("dhcpv4: icmp procedure is complete: %q", target)
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07: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 08:20:01 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07: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 08:20:01 -07:00
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
return !s.leasedOffsets.isSet(offset)
|
2021-03-16 09:11:32 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
return ip.To4()
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find an expired lease and return its index or -1
|
|
|
|
func (s *v4Server) findExpiredLease() int {
|
2021-03-18 07:07:13 -07:00
|
|
|
now := time.Now()
|
2020-07-03 08:20:01 -07:00
|
|
|
for i, lease := range s.leases {
|
2021-03-18 07:07:13 -07:00
|
|
|
if !lease.IsStatic() && lease.Expiry.Before(now) {
|
2020-07-03 08:20:01 -07:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:11:32 -07: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 07:07:13 -07:00
|
|
|
func (s *v4Server) reserveLease(mac net.HardwareAddr) (l *Lease, err error) {
|
2022-08-23 08:22:49 -07:00
|
|
|
l = &Lease{HWAddr: slices.Clone(mac)}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
l.IP = s.nextIP()
|
2020-07-03 08:20:01 -07:00
|
|
|
if l.IP == nil {
|
|
|
|
i := s.findExpiredLease()
|
|
|
|
if i < 0 {
|
2021-03-18 07:07:13 -07:00
|
|
|
return nil, nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
copy(s.leases[i].HWAddr, mac)
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
return s.leases[i], nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
err = s.addLease(l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-16 09:11:32 -07:00
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
return l, nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// commitLease refreshes l's values. It takes the desired hostname into account
|
|
|
|
// when setting it into the lease, but generates a unique one if the provided
|
|
|
|
// can't be used.
|
|
|
|
func (s *v4Server) commitLease(l *Lease, hostname string) {
|
|
|
|
prev := l.Hostname
|
|
|
|
hostname = s.validHostnameForClient(hostname, l.IP)
|
2021-05-06 03:02:48 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
if s.leaseHosts.Has(hostname) {
|
|
|
|
log.Info("dhcpv4: hostname %q already exists", hostname)
|
2021-05-13 05:17:42 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
if prev == "" {
|
|
|
|
// The lease is just allocated due to DHCPDISCOVER.
|
|
|
|
hostname = aghnet.GenerateHostname(l.IP)
|
|
|
|
} else {
|
|
|
|
hostname = prev
|
2021-05-13 05:17:42 -07:00
|
|
|
}
|
2022-08-30 10:39:34 -07:00
|
|
|
}
|
|
|
|
if l.Hostname != hostname {
|
|
|
|
l.Hostname = hostname
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
l.Expiry = time.Now().Add(s.conf.leaseTime)
|
|
|
|
if prev != "" && prev != l.Hostname {
|
|
|
|
s.leaseHosts.Del(prev)
|
|
|
|
}
|
|
|
|
if l.Hostname != "" {
|
|
|
|
s.leaseHosts.Add(l.Hostname)
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-05-14 09:16:07 -07: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// handleDiscover is the handler for the DHCP Discover request.
|
|
|
|
func (s *v4Server) handleDiscover(req, resp *dhcpv4.DHCPv4) (l *Lease, err error) {
|
2020-07-03 08:20:01 -07:00
|
|
|
mac := req.ClientHWAddr
|
|
|
|
|
2021-05-14 09:16:07 -07:00
|
|
|
defer s.conf.notify(LeaseChangedDBStore)
|
2021-05-06 03:02:48 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
l = s.findLease(mac)
|
2021-05-06 03:02:48 -07: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 07:07:13 -07:00
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2021-05-06 03:02:48 -07:00
|
|
|
return l, nil
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-05-14 09:16:07 -07: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 03:02:48 -07:00
|
|
|
|
2021-05-14 09:16:07 -07:00
|
|
|
return nil, nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
|
2021-03-18 07:07:13 -07:00
|
|
|
|
|
|
|
return l, nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
// OptionFQDN returns a DHCPv4 option for sending the FQDN to the client
|
|
|
|
// requested another hostname.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc4702.
|
|
|
|
func OptionFQDN(fqdn string) (opt dhcpv4.Option) {
|
|
|
|
optData := []byte{
|
|
|
|
// Set only S and O DHCP client FQDN option flags.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc4702#section-2.1.
|
|
|
|
1<<0 | 1<<1,
|
|
|
|
// The RCODE fields should be set to 0xFF in the server responses.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc4702#section-2.2.
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
}
|
|
|
|
optData = append(optData, fqdn...)
|
2020-08-24 03:01:55 -07:00
|
|
|
|
2022-08-23 08:22:49 -07:00
|
|
|
return dhcpv4.OptGeneric(dhcpv4.OptionFQDN, optData)
|
2020-08-24 03:01:55 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 04:54:47 -07: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
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// handleSelecting handles the DHCPREQUEST generated during SELECTING state.
|
|
|
|
func (s *v4Server) handleSelecting(
|
|
|
|
req *dhcpv4.DHCPv4,
|
|
|
|
reqIP net.IP,
|
|
|
|
sid net.IP,
|
|
|
|
) (l *Lease, needsReply bool) {
|
|
|
|
// Client inserts the address of the selected server in server identifier,
|
|
|
|
// ciaddr MUST be zero.
|
2021-05-06 03:02:48 -07:00
|
|
|
mac := req.ClientHWAddr
|
2022-08-30 10:39:34 -07:00
|
|
|
if !sid.Equal(s.conf.dnsIPAddrs[0]) {
|
|
|
|
log.Debug("dhcpv4: bad server identifier in req msg for %s: %s", mac, sid)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
} else if ciaddr := req.ClientIPAddr; ciaddr != nil && !ciaddr.IsUnspecified() {
|
|
|
|
log.Debug("dhcpv4: non-zero ciaddr in selecting req msg for %s", mac)
|
|
|
|
|
|
|
|
return nil, false
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// Requested IP address MUST be filled in with the yiaddr value from the
|
|
|
|
// chosen DHCPOFFER.
|
|
|
|
if ip4 := reqIP.To4(); ip4 == nil {
|
|
|
|
log.Debug("dhcpv4: bad requested address in req msg for %s: %s", mac, reqIP)
|
2021-03-12 03:34:43 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
var mismatch bool
|
|
|
|
if l, mismatch = s.checkLease(mac, reqIP); mismatch {
|
|
|
|
return nil, true
|
|
|
|
} else if l == nil {
|
|
|
|
log.Debug("dhcpv4: no reserved lease for %s", mac)
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleInitReboot handles the DHCPREQUEST generated during INIT-REBOOT state.
|
|
|
|
func (s *v4Server) handleInitReboot(req *dhcpv4.DHCPv4, reqIP net.IP) (l *Lease, needsReply bool) {
|
|
|
|
mac := req.ClientHWAddr
|
|
|
|
|
2021-03-12 03:34:43 -07:00
|
|
|
if ip4 := reqIP.To4(); ip4 == nil {
|
2022-08-30 10:39:34 -07:00
|
|
|
log.Debug("dhcpv4: bad requested address in req msg for %s: %s", mac, reqIP)
|
2021-03-12 03:34:43 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// ciaddr MUST be zero. The client is seeking to verify a previously
|
|
|
|
// allocated, cached configuration.
|
|
|
|
if ciaddr := req.ClientIPAddr; ciaddr != nil && !ciaddr.IsUnspecified() {
|
|
|
|
log.Debug("dhcpv4: non-zero ciaddr in init-reboot req msg for %s", mac)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.conf.subnet.Contains(reqIP) {
|
|
|
|
// If the DHCP server detects that the client is on the wrong net then
|
|
|
|
// the server SHOULD send a DHCPNAK message to the client.
|
|
|
|
log.Debug("dhcpv4: wrong subnet in init-reboot req msg for %s: %s", mac, reqIP)
|
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:54:47 -07:00
|
|
|
var mismatch bool
|
2022-08-30 10:39:34 -07:00
|
|
|
if l, mismatch = s.checkLease(mac, reqIP); mismatch {
|
2021-05-06 03:02:48 -07:00
|
|
|
return nil, true
|
2022-08-30 10:39:34 -07:00
|
|
|
} else if l == nil {
|
|
|
|
// If the DHCP server has no record of this client, then it MUST remain
|
|
|
|
// silent, and MAY output a warning to the network administrator.
|
|
|
|
log.Info("dhcpv4: warning: no existing lease for %s", mac)
|
|
|
|
|
|
|
|
return nil, false
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
return l, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleRenew handles the DHCPREQUEST generated during RENEWING or REBINDING
|
|
|
|
// state.
|
|
|
|
func (s *v4Server) handleRenew(req *dhcpv4.DHCPv4) (l *Lease, needsReply bool) {
|
|
|
|
mac := req.ClientHWAddr
|
|
|
|
|
|
|
|
// ciaddr MUST be filled in with client's IP address.
|
|
|
|
ciaddr := req.ClientIPAddr
|
|
|
|
if ciaddr == nil || ciaddr.IsUnspecified() || ciaddr.To4() == nil {
|
|
|
|
log.Debug("dhcpv4: bad ciaddr in renew req msg for %s: %s", mac, ciaddr)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-03-12 03:34:43 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
var mismatch bool
|
|
|
|
if l, mismatch = s.checkLease(mac, ciaddr); mismatch {
|
2020-07-03 08:20:01 -07:00
|
|
|
return nil, true
|
2022-08-30 10:39:34 -07:00
|
|
|
} else if l == nil {
|
|
|
|
// If the DHCP server has no record of this client, then it MUST remain
|
|
|
|
// silent, and MAY output a warning to the network administrator.
|
|
|
|
log.Info("dhcpv4: warning: no existing lease for %s", mac)
|
|
|
|
|
|
|
|
return nil, false
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
return l, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleByRequestType handles the DHCPREQUEST according to the state during
|
|
|
|
// which it's generated by client.
|
|
|
|
func (s *v4Server) handleByRequestType(req *dhcpv4.DHCPv4) (lease *Lease, needsReply bool) {
|
|
|
|
reqIP, sid := req.RequestedIPAddress(), req.ServerIdentifier()
|
|
|
|
|
|
|
|
if sid != nil && !sid.IsUnspecified() {
|
|
|
|
// If the DHCPREQUEST message contains a server identifier option, the
|
|
|
|
// message is in response to a DHCPOFFER message. Otherwise, the
|
|
|
|
// message is a request to verify or extend an existing lease.
|
|
|
|
return s.handleSelecting(req, reqIP, sid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqIP != nil && !reqIP.IsUnspecified() {
|
|
|
|
// Requested IP address option MUST be filled in with client's notion of
|
|
|
|
// its previously assigned address.
|
|
|
|
return s.handleInitReboot(req, reqIP)
|
|
|
|
}
|
2021-04-15 06:59:05 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// Server identifier MUST NOT be filled in, requested IP address option MUST
|
|
|
|
// NOT be filled in.
|
|
|
|
return s.handleRenew(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleRequest is the handler for a DHCPREQUEST message.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc2131#section-4.3.2.
|
|
|
|
func (s *v4Server) handleRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needsReply bool) {
|
|
|
|
lease, needsReply = s.handleByRequestType(req)
|
|
|
|
if lease == nil {
|
|
|
|
return nil, needsReply
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
|
2021-03-12 03:34:43 -07:00
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
hostname := req.HostName()
|
|
|
|
isRequested := hostname != "" || req.ParameterRequestList().Has(dhcpv4.OptionHostName)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
s.conf.notify(LeaseChangedAdded)
|
|
|
|
s.conf.notify(LeaseChangedDBStore)
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.leasesLock.Lock()
|
|
|
|
defer s.leasesLock.Unlock()
|
|
|
|
|
|
|
|
if lease.IsStatic() {
|
|
|
|
if lease.Hostname != "" {
|
|
|
|
// TODO(e.burkov): This option is used to update the server's DNS
|
|
|
|
// mapping. The option should only be answered when it has been
|
|
|
|
// requested.
|
|
|
|
resp.UpdateOption(OptionFQDN(lease.Hostname))
|
|
|
|
}
|
|
|
|
|
|
|
|
return lease, needsReply
|
|
|
|
}
|
|
|
|
|
|
|
|
s.commitLease(lease, hostname)
|
|
|
|
|
|
|
|
if isRequested {
|
|
|
|
resp.UpdateOption(dhcpv4.OptHostName(lease.Hostname))
|
|
|
|
}
|
|
|
|
|
|
|
|
return lease, needsReply
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// handleDecline is the handler for the DHCP Decline request.
|
|
|
|
func (s *v4Server) handleDecline(req, resp *dhcpv4.DHCPv4) (err error) {
|
2021-05-14 09:16:07 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
// handleRelease is the handler for the DHCP Release request.
|
|
|
|
func (s *v4Server) handleRelease(req, resp *dhcpv4.DHCPv4) (err error) {
|
2021-05-14 09:16:07 -07:00
|
|
|
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 08:20:01 -07: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
|
2022-08-30 10:39:34 -07:00
|
|
|
func (s *v4Server) handle(req, resp *dhcpv4.DHCPv4) int {
|
2021-03-18 07:07:13 -07:00
|
|
|
var err error
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-09-13 10:05:41 -07:00
|
|
|
// Include server's identifier option since any reply should contain it.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc2131#page-29.
|
2020-07-03 08:20:01 -07:00
|
|
|
resp.UpdateOption(dhcpv4.OptServerIdentifier(s.conf.dnsIPAddrs[0]))
|
|
|
|
|
2021-05-14 09:16:07 -07:00
|
|
|
// TODO(a.garipov): Refactor this into handlers.
|
2021-03-18 07:07:13 -07:00
|
|
|
var l *Lease
|
2022-08-23 08:22:49 -07:00
|
|
|
switch mt := req.MessageType(); mt {
|
2020-07-03 08:20:01 -07:00
|
|
|
case dhcpv4.MessageTypeDiscover:
|
2022-08-30 10:39:34 -07:00
|
|
|
l, err = s.handleDiscover(req, resp)
|
2021-03-18 07:07:13 -07:00
|
|
|
if err != nil {
|
2022-08-30 10:39:34 -07:00
|
|
|
log.Error("dhcpv4: handling discover: %s", err)
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:13 -07:00
|
|
|
if l == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
case dhcpv4.MessageTypeRequest:
|
|
|
|
var toReply bool
|
2022-08-30 10:39:34 -07:00
|
|
|
l, toReply = s.handleRequest(req, resp)
|
2021-03-18 07:07:13 -07:00
|
|
|
if l == nil {
|
2020-07-03 08:20:01 -07:00
|
|
|
if toReply {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return -1 // drop packet
|
|
|
|
}
|
2021-05-14 09:16:07 -07:00
|
|
|
case dhcpv4.MessageTypeDecline:
|
2022-08-30 10:39:34 -07:00
|
|
|
err = s.handleDecline(req, resp)
|
2021-05-14 09:16:07 -07:00
|
|
|
if err != nil {
|
2022-08-30 10:39:34 -07:00
|
|
|
log.Error("dhcpv4: handling decline: %s", err)
|
2021-05-14 09:16:07 -07:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
case dhcpv4.MessageTypeRelease:
|
2022-08-30 10:39:34 -07:00
|
|
|
err = s.handleRelease(req, resp)
|
2021-05-14 09:16:07 -07:00
|
|
|
if err != nil {
|
2022-08-30 10:39:34 -07:00
|
|
|
log.Error("dhcpv4: handling release: %s", err)
|
2021-05-14 09:16:07 -07:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-05-14 09:16:07 -07:00
|
|
|
if l != nil {
|
2021-09-13 10:05:41 -07:00
|
|
|
resp.YourIPAddr = netutil.CloneIP(l.IP)
|
2021-05-14 09:16:07 -07:00
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2022-08-26 04:30:31 -07:00
|
|
|
// Set IP address lease time for all DHCPOFFER messages and DHCPACK messages
|
|
|
|
// replied for DHCPREQUEST.
|
2021-09-13 10:05:41 -07:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Inspect why this is always set to configured value.
|
2020-07-03 08:20:01 -07:00
|
|
|
resp.UpdateOption(dhcpv4.OptIPAddressLeaseTime(s.conf.leaseTime))
|
2020-08-25 04:07:11 -07:00
|
|
|
|
2022-08-26 04:30:31 -07:00
|
|
|
// Delete options explicitly configured to be removed.
|
|
|
|
for code := range resp.Options {
|
|
|
|
if val, ok := s.options[code]; ok && val == nil {
|
|
|
|
delete(resp.Options, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:05:41 -07:00
|
|
|
// Update values for each explicitly configured parameter requested by
|
|
|
|
// client.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc2131#section-4.3.1.
|
|
|
|
requested := req.ParameterRequestList()
|
|
|
|
for _, code := range requested {
|
2022-08-26 04:30:31 -07:00
|
|
|
if val := s.options.Get(code); val != nil {
|
|
|
|
resp.UpdateOption(dhcpv4.Option{
|
|
|
|
Code: code,
|
|
|
|
Value: dhcpv4.OptionGeneric{Data: s.options.Get(code)},
|
|
|
|
})
|
2021-09-13 10:05:41 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:13 -07:00
|
|
|
|
2020-07-03 08:20:01 -07: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 05:20:57 -07:00
|
|
|
log.Debug("dhcpv4: received message: %s", req.Summary())
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
switch req.MessageType() {
|
2021-09-13 10:05:41 -07:00
|
|
|
case
|
|
|
|
dhcpv4.MessageTypeDiscover,
|
2021-05-14 09:16:07 -07:00
|
|
|
dhcpv4.MessageTypeRequest,
|
|
|
|
dhcpv4.MessageTypeDecline,
|
|
|
|
dhcpv4.MessageTypeRelease:
|
|
|
|
// Go on.
|
2020-07-03 08:20:01 -07:00
|
|
|
default:
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("dhcpv4: unsupported message type %d", req.MessageType())
|
2021-05-14 09:16:07 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := dhcpv4.NewReplyFromRequest(req)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("dhcpv4: dhcpv4.New: %s", err)
|
2022-08-26 04:30:31 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-09 06:03:37 -07:00
|
|
|
err = netutil.ValidateMAC(req.ClientHWAddr)
|
2021-03-31 02:36:57 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error("dhcpv4: invalid ClientHWAddr: %s", err)
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:39:34 -07:00
|
|
|
r := s.handle(req, resp)
|
2020-07-03 08:20:01 -07:00
|
|
|
if r < 0 {
|
|
|
|
return
|
|
|
|
} else if r == 0 {
|
|
|
|
resp.Options.Update(dhcpv4.OptMessageType(dhcpv4.MessageTypeNak))
|
|
|
|
}
|
|
|
|
|
2021-09-07 08:33:23 -07:00
|
|
|
s.send(peer, conn, req, resp)
|
|
|
|
}
|
2021-07-30 05:27:24 -07:00
|
|
|
|
2021-09-13 10:05:41 -07:00
|
|
|
// send writes resp for peer to conn considering the req's parameters according
|
|
|
|
// to RFC-2131.
|
2021-09-07 08:33:23 -07:00
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc2131#section-4.1.
|
|
|
|
func (s *v4Server) send(peer net.Addr, conn net.PacketConn, req, resp *dhcpv4.DHCPv4) {
|
2021-09-30 08:28:19 -07:00
|
|
|
switch giaddr, ciaddr, mtype := req.GatewayIPAddr, req.ClientIPAddr, resp.MessageType(); {
|
|
|
|
case giaddr != nil && !giaddr.IsUnspecified():
|
2021-09-07 08:33:23 -07:00
|
|
|
// Send any return messages to the server port on the BOOTP
|
|
|
|
// relay agent whose address appears in giaddr.
|
|
|
|
peer = &net.UDPAddr{
|
|
|
|
IP: giaddr,
|
|
|
|
Port: dhcpv4.ServerPort,
|
|
|
|
}
|
2021-09-30 08:28:19 -07:00
|
|
|
if mtype == dhcpv4.MessageTypeNak {
|
2022-01-25 09:47:02 -07:00
|
|
|
// Set the broadcast bit in the DHCPNAK, so that the relay agent
|
|
|
|
// broadcasts it to the client, because the client may not have
|
|
|
|
// a correct network address or subnet mask, and the client may not
|
|
|
|
// be answering ARP requests.
|
2021-09-30 08:28:19 -07:00
|
|
|
resp.SetBroadcast()
|
|
|
|
}
|
|
|
|
case mtype == dhcpv4.MessageTypeNak:
|
2021-09-07 08:33:23 -07:00
|
|
|
// Broadcast any DHCPNAK messages to 0xffffffff.
|
2021-09-30 08:28:19 -07:00
|
|
|
case ciaddr != nil && !ciaddr.IsUnspecified():
|
2021-09-07 08:33:23 -07:00
|
|
|
// Unicast DHCPOFFER and DHCPACK messages to the address in
|
|
|
|
// ciaddr.
|
|
|
|
peer = &net.UDPAddr{
|
|
|
|
IP: ciaddr,
|
|
|
|
Port: dhcpv4.ClientPort,
|
|
|
|
}
|
2021-09-30 08:28:19 -07:00
|
|
|
case !req.IsBroadcast() && req.ClientHWAddr != nil:
|
|
|
|
// Unicast DHCPOFFER and DHCPACK messages to the client's
|
|
|
|
// hardware address and yiaddr.
|
|
|
|
peer = &dhcpUnicastAddr{
|
2022-08-15 08:31:32 -07:00
|
|
|
Addr: raw.Addr{HardwareAddr: req.ClientHWAddr},
|
2021-09-30 08:28:19 -07:00
|
|
|
yiaddr: resp.YourIPAddr,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on since peer is already set to broadcast.
|
2021-07-30 05:27:24 -07:00
|
|
|
}
|
|
|
|
|
2021-09-07 08:33:23 -07:00
|
|
|
log.Debug("dhcpv4: sending to %s: %s", peer, resp.Summary())
|
2021-09-30 08:28:19 -07:00
|
|
|
if _, err := conn.WriteTo(resp.ToBytes(), peer); err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Error("dhcpv4: conn.Write to %s failed: %s", peer, err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:27:04 -07:00
|
|
|
// Start starts the IPv4 DHCP server.
|
2021-04-21 04:42:19 -07:00
|
|
|
func (s *v4Server) Start() (err error) {
|
2021-05-24 07:28:11 -07:00
|
|
|
defer func() { err = errors.Annotate(err, "dhcpv4: %w") }()
|
2021-04-21 04:42:19 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
if !s.conf.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:27:04 -07:00
|
|
|
ifaceName := s.conf.InterfaceName
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
2020-07-03 08:20:01 -07:00
|
|
|
if err != nil {
|
2021-04-21 04:42:19 -07:00
|
|
|
return fmt.Errorf("finding interface %s by name: %w", ifaceName, err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("dhcpv4: starting...")
|
2020-11-09 09:27:04 -07:00
|
|
|
|
2021-08-12 07:33:53 -07:00
|
|
|
dnsIPAddrs, err := aghnet.IfaceDNSIPAddrs(
|
|
|
|
iface,
|
|
|
|
aghnet.IPVersion4,
|
|
|
|
defaultMaxAttempts,
|
|
|
|
defaultBackoff,
|
|
|
|
)
|
2020-11-09 09:27:04 -07:00
|
|
|
if err != nil {
|
2021-04-21 04:42:19 -07:00
|
|
|
return fmt.Errorf("interface %s: %w", ifaceName, err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2020-12-02 04:42:59 -07:00
|
|
|
if len(dnsIPAddrs) == 0 {
|
|
|
|
// No available IP addresses which may appear later.
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-26 04:30:31 -07:00
|
|
|
// Update the value of Domain Name Server option separately from others if
|
|
|
|
// not assigned yet since its value is available only at server's start.
|
|
|
|
if !s.options.Has(dhcpv4.OptionDomainNameServer) {
|
|
|
|
s.options.Update(dhcpv4.OptDNS(dnsIPAddrs...))
|
|
|
|
}
|
2020-12-02 04:42:59 -07:00
|
|
|
|
2020-11-09 09:27:04 -07:00
|
|
|
s.conf.dnsIPAddrs = dnsIPAddrs
|
|
|
|
|
2021-09-30 08:28:19 -07:00
|
|
|
var c net.PacketConn
|
|
|
|
if c, err = s.newDHCPConn(iface); err != nil {
|
|
|
|
return err
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-09-30 08:28:19 -07:00
|
|
|
|
|
|
|
s.srv, err = server4.NewServer(
|
|
|
|
iface.Name,
|
|
|
|
nil,
|
|
|
|
s.packetHandler,
|
|
|
|
server4.WithConn(c),
|
|
|
|
server4.WithDebugLogger(),
|
|
|
|
)
|
2020-07-03 08:20:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Info("dhcpv4: listening")
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
go func() {
|
2021-05-21 04:55:42 -07:00
|
|
|
if serr := s.srv.Serve(); errors.Is(serr, net.ErrClosed) {
|
|
|
|
log.Info("dhcpv4: server is closed")
|
|
|
|
} else if serr != nil {
|
2021-04-21 04:42:19 -07:00
|
|
|
log.Error("dhcpv4: srv.Serve: %s", serr)
|
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
}()
|
2020-11-09 09:27:04 -07:00
|
|
|
|
2021-04-15 07:52:53 -07: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 08:20:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop - stop server
|
2021-06-16 06:48:46 -07:00
|
|
|
func (s *v4Server) Stop() (err error) {
|
2020-07-03 08:20:01 -07:00
|
|
|
if s.srv == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("dhcpv4: stopping")
|
2021-06-16 06:48:46 -07:00
|
|
|
err = s.srv.Close()
|
2020-07-03 08:20:01 -07:00
|
|
|
if err != nil {
|
2021-06-16 06:48:46 -07:00
|
|
|
return fmt.Errorf("closing dhcpv4 srv: %w", err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-04-15 07:52:53 -07: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 08:20:01 -07:00
|
|
|
s.srv = nil
|
2021-06-16 06:48:46 -07:00
|
|
|
|
|
|
|
return nil
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create DHCPv4 server
|
2021-03-15 09:24:26 -07:00
|
|
|
func v4Create(conf V4ServerConf) (srv DHCPServer, err error) {
|
2020-07-03 08:20:01 -07:00
|
|
|
s := &v4Server{}
|
|
|
|
s.conf = conf
|
2021-07-29 07:40:31 -07:00
|
|
|
s.leaseHosts = stringutil.NewSet()
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-03-17 05:02:17 -07:00
|
|
|
// TODO(a.garipov): Don't use a disabled server in other places or just
|
|
|
|
// use an interface.
|
2020-07-03 08:20:01 -07:00
|
|
|
if !conf.Enabled {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-04-20 05:07:57 -07:00
|
|
|
var routerIP net.IP
|
|
|
|
routerIP, err = tryTo4(s.conf.GatewayIP)
|
2020-07-03 08:20:01 -07:00
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
if s.conf.SubnetMask == nil {
|
|
|
|
return s, fmt.Errorf("dhcpv4: invalid subnet mask: %v", s.conf.SubnetMask)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
2021-04-20 05:07:57 -07:00
|
|
|
|
|
|
|
subnetMask := make([]byte, 4)
|
|
|
|
copy(subnetMask, s.conf.SubnetMask.To4())
|
|
|
|
|
|
|
|
s.conf.subnet = &net.IPNet{
|
|
|
|
IP: routerIP,
|
|
|
|
Mask: subnetMask,
|
|
|
|
}
|
2021-08-12 07:33:53 -07:00
|
|
|
s.conf.broadcastIP = aghnet.BroadcastFromIPNet(s.conf.subnet)
|
2021-07-30 05:27:24 -07:00
|
|
|
|
2021-03-16 09:11:32 -07:00
|
|
|
s.conf.ipRange, err = newIPRange(conf.RangeStart, conf.RangeEnd)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return s, fmt.Errorf("dhcpv4: %w", err)
|
2020-07-03 08:20:01 -07:00
|
|
|
}
|
|
|
|
|
2021-10-19 09:28:18 -07:00
|
|
|
if s.conf.ipRange.contains(routerIP) {
|
|
|
|
return s, fmt.Errorf("dhcpv4: gateway ip %v in the ip range: %v-%v",
|
|
|
|
routerIP,
|
|
|
|
conf.RangeStart,
|
|
|
|
conf.RangeEnd,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.conf.subnet.Contains(conf.RangeStart) {
|
|
|
|
return s, fmt.Errorf("dhcpv4: range start %v is outside network %v",
|
|
|
|
conf.RangeStart,
|
|
|
|
s.conf.subnet,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.conf.subnet.Contains(conf.RangeEnd) {
|
|
|
|
return s, fmt.Errorf("dhcpv4: range end %v is outside network %v",
|
|
|
|
conf.RangeEnd,
|
|
|
|
s.conf.subnet,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(a.garipov, d.seregin): Check that every lease is inside the IPRange.
|
2021-03-18 07:07:13 -07:00
|
|
|
s.leasedOffsets = newBitSet()
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
if conf.LeaseDuration == 0 {
|
2021-09-30 11:17:54 -07:00
|
|
|
s.conf.leaseTime = timeutil.Day
|
2020-07-03 08:20:01 -07:00
|
|
|
s.conf.LeaseDuration = uint32(s.conf.leaseTime.Seconds())
|
|
|
|
} else {
|
|
|
|
s.conf.leaseTime = time.Second * time.Duration(conf.LeaseDuration)
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:05:41 -07:00
|
|
|
s.options = prepareOptions(s.conf)
|
2020-08-25 04:07:11 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
return s, nil
|
|
|
|
}
|