mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-01-09 23:47:23 +03:00
ea8d634f65
Merge in DNS/adguard-home from dhcpd-imp-code to master Squashed commit of the following: commit 413403c169bd3f6b5f2ed63b783d078dbb15e054 Merge: eed1838500fec990bc
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 26 12:46:25 2023 +0300 Merge remote-tracking branch 'origin/master' into dhcpd-imp-code commit eed1838502add1e16e5d3ada03778f21913fd5e5 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 26 12:16:46 2023 +0300 dhcpd: imp docs commit fa4fe036f7b1f2b49201bf0b5b904f99989082f0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu May 25 11:32:34 2023 +0300 all: lint script commit a4022b3d4bbfa709e5096397bbe64ea406c8a366 Merge: e08ff3a26cbc7985e7
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu May 25 11:29:57 2023 +0300 Merge remote-tracking branch 'origin/master' into dhcpd-imp-code # Conflicts: # scripts/make/go-lint.sh commit e08ff3a26414e201d6e75608363db941fa2f5b39 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 15:43:11 2023 +0300 dhcpd: imp code commit 970b538f8ea94d3732d77bfb648e402a1d28ab06 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 15:40:36 2023 +0300 dhcpd: imp code commit 0e5916ddd7514af983e8557080d55d6aeb6fbbc0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 15:37:17 2023 +0300 dhcpd: imp code commit e06a6c6031b232e76ae2be3e3b8fe1a2ffa715e0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 16:40:09 2023 +0300 dhcpd: imp code commit eed4ff10ff1b29c71d70fb7978706efde89afee1 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 15:45:06 2023 +0300 all: lint script commit 87f84ace5f6f34dbc28befa8257d1d2492c5e0a4 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 15:44:23 2023 +0300 dhcpd: imp code commit a54c9929d51de1f1e6807d650fd08dd80ddbf147 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 14:29:42 2023 +0300 dhcpd: imp code commit 1bbea342f7f55587724aa9a29d9657e5ce75f5d8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 14:12:09 2023 +0300 dhcpd: imp code commit 48fb4eff73683e799ddb3076afdcf7b067ca24b6 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 13:57:59 2023 +0300 dhcpd: imp code commit f6cd7fcb8d4c1c815a20875d777ea1eca2c8ea89 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 13:17:54 2023 +0300 dhcpd: imp code commit 2b91dc25bbaa16dba6d9389a4e2224cf91eb4554 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue May 23 12:57:46 2023 +0300 dhcpd: imp code commit 34f5dd58764080f6202fc9a1abd751a15dbf7090 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 15:31:39 2023 +0300 dhcpd: imp code commit 12ef0d225064a1b78adf7b2cfca21a8dba92ca8e Merge: 6b62a766524b41100c
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 13:03:41 2023 +0300 Merge remote-tracking branch 'origin/master' into dhcpd-imp-code commit 6b62a7665720b85398d65a1926518a63e6bb6403 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 12:55:43 2023 +0300 dhcpd: imp code commit 18c5cdf0480fac7711282027a64d58704c75af5f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 12:48:30 2023 +0300 dhcpd: imp code commit e7c1f4324cba3fe86cf56df6b971791a5a8790de Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 12:37:15 2023 +0300 dhcpd: imp code ... and 1 more commit
411 lines
9.8 KiB
Go
411 lines
9.8 KiB
Go
// Package dhcpd provides a DHCP server.
|
|
package dhcpd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
const (
|
|
// DefaultDHCPLeaseTTL is the default time-to-live for leases.
|
|
DefaultDHCPLeaseTTL = uint32(timeutil.Day / time.Second)
|
|
|
|
// DefaultDHCPTimeoutICMP is the default timeout for waiting ICMP responses.
|
|
DefaultDHCPTimeoutICMP = 1000
|
|
)
|
|
|
|
// Currently used defaults for ifaceDNSAddrs.
|
|
const (
|
|
defaultMaxAttempts int = 10
|
|
defaultBackoff time.Duration = 500 * time.Millisecond
|
|
)
|
|
|
|
// Lease contains the necessary information about a DHCP lease. It's used in
|
|
// various places. So don't change it without good reason.
|
|
type Lease struct {
|
|
// Expiry is the expiration time of the lease.
|
|
Expiry time.Time `json:"expires"`
|
|
|
|
// Hostname of the client.
|
|
Hostname string `json:"hostname"`
|
|
|
|
// HWAddr is the physical hardware address (MAC address).
|
|
HWAddr net.HardwareAddr `json:"mac"`
|
|
|
|
// IP is the IP address leased to the client.
|
|
//
|
|
// TODO(a.garipov): Migrate leases.db.
|
|
IP netip.Addr `json:"ip"`
|
|
|
|
// IsStatic defines if the lease is static.
|
|
IsStatic bool `json:"static"`
|
|
}
|
|
|
|
// Clone returns a deep copy of l.
|
|
func (l *Lease) Clone() (clone *Lease) {
|
|
if l == nil {
|
|
return nil
|
|
}
|
|
|
|
return &Lease{
|
|
Expiry: l.Expiry,
|
|
Hostname: l.Hostname,
|
|
HWAddr: slices.Clone(l.HWAddr),
|
|
IP: l.IP,
|
|
IsStatic: l.IsStatic,
|
|
}
|
|
}
|
|
|
|
// IsBlocklisted returns true if the lease is blocklisted.
|
|
//
|
|
// TODO(a.garipov): Just make it a boolean field.
|
|
func (l *Lease) IsBlocklisted() (ok bool) {
|
|
if len(l.HWAddr) == 0 {
|
|
return false
|
|
}
|
|
|
|
for _, b := range l.HWAddr {
|
|
if b != 0 {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for Lease.
|
|
func (l Lease) MarshalJSON() ([]byte, error) {
|
|
var expiryStr string
|
|
if !l.IsStatic {
|
|
// The front-end is waiting for RFC 3999 format of the time
|
|
// value. It also shouldn't got an Expiry field for static
|
|
// leases.
|
|
//
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/2692.
|
|
expiryStr = l.Expiry.Format(time.RFC3339)
|
|
}
|
|
|
|
type lease Lease
|
|
return json.Marshal(&struct {
|
|
HWAddr string `json:"mac"`
|
|
Expiry string `json:"expires,omitempty"`
|
|
lease
|
|
}{
|
|
HWAddr: l.HWAddr.String(),
|
|
Expiry: expiryStr,
|
|
lease: lease(l),
|
|
})
|
|
}
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for *Lease.
|
|
func (l *Lease) UnmarshalJSON(data []byte) (err error) {
|
|
type lease Lease
|
|
aux := struct {
|
|
*lease
|
|
HWAddr string `json:"mac"`
|
|
}{
|
|
lease: (*lease)(l),
|
|
}
|
|
if err = json.Unmarshal(data, &aux); err != nil {
|
|
return err
|
|
}
|
|
|
|
l.HWAddr, err = net.ParseMAC(aux.HWAddr)
|
|
if err != nil {
|
|
return fmt.Errorf("couldn't parse MAC address: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// OnLeaseChangedT is a callback for lease changes.
|
|
type OnLeaseChangedT func(flags int)
|
|
|
|
// flags for onLeaseChanged()
|
|
const (
|
|
LeaseChangedAdded = iota
|
|
LeaseChangedAddedStatic
|
|
LeaseChangedRemovedStatic
|
|
LeaseChangedRemovedAll
|
|
|
|
LeaseChangedDBStore
|
|
)
|
|
|
|
// GetLeasesFlags are the flags for GetLeases.
|
|
type GetLeasesFlags uint8
|
|
|
|
// GetLeasesFlags values
|
|
const (
|
|
LeasesDynamic GetLeasesFlags = 0b01
|
|
LeasesStatic GetLeasesFlags = 0b10
|
|
|
|
LeasesAll = LeasesDynamic | LeasesStatic
|
|
)
|
|
|
|
// Interface is the DHCP server that deals with both IP address families.
|
|
type Interface interface {
|
|
Start() (err error)
|
|
Stop() (err error)
|
|
Enabled() (ok bool)
|
|
|
|
Leases(flags GetLeasesFlags) (leases []*Lease)
|
|
SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)
|
|
FindMACbyIP(ip netip.Addr) (mac net.HardwareAddr)
|
|
|
|
WriteDiskConfig(c *ServerConfig)
|
|
}
|
|
|
|
// MockInterface is a mock Interface implementation.
|
|
//
|
|
// TODO(e.burkov): Move to aghtest when the API stabilized.
|
|
type MockInterface struct {
|
|
OnStart func() (err error)
|
|
OnStop func() (err error)
|
|
OnEnabled func() (ok bool)
|
|
OnLeases func(flags GetLeasesFlags) (leases []*Lease)
|
|
OnSetOnLeaseChanged func(f OnLeaseChangedT)
|
|
OnFindMACbyIP func(ip netip.Addr) (mac net.HardwareAddr)
|
|
OnWriteDiskConfig func(c *ServerConfig)
|
|
}
|
|
|
|
var _ Interface = (*MockInterface)(nil)
|
|
|
|
// Start implements the Interface for *MockInterface.
|
|
func (s *MockInterface) Start() (err error) { return s.OnStart() }
|
|
|
|
// Stop implements the Interface for *MockInterface.
|
|
func (s *MockInterface) Stop() (err error) { return s.OnStop() }
|
|
|
|
// Enabled implements the Interface for *MockInterface.
|
|
func (s *MockInterface) Enabled() (ok bool) { return s.OnEnabled() }
|
|
|
|
// Leases implements the Interface for *MockInterface.
|
|
func (s *MockInterface) Leases(flags GetLeasesFlags) (ls []*Lease) { return s.OnLeases(flags) }
|
|
|
|
// SetOnLeaseChanged implements the Interface for *MockInterface.
|
|
func (s *MockInterface) SetOnLeaseChanged(f OnLeaseChangedT) { s.OnSetOnLeaseChanged(f) }
|
|
|
|
// FindMACbyIP implements the [Interface] for *MockInterface.
|
|
func (s *MockInterface) FindMACbyIP(ip netip.Addr) (mac net.HardwareAddr) {
|
|
return s.OnFindMACbyIP(ip)
|
|
}
|
|
|
|
// WriteDiskConfig implements the Interface for *MockInterface.
|
|
func (s *MockInterface) WriteDiskConfig(c *ServerConfig) { s.OnWriteDiskConfig(c) }
|
|
|
|
// server is the DHCP service that handles DHCPv4, DHCPv6, and HTTP API.
|
|
type server struct {
|
|
srv4 DHCPServer
|
|
srv6 DHCPServer
|
|
|
|
// TODO(a.garipov): Either create a separate type for the internal config or
|
|
// just put the config values into Server.
|
|
conf *ServerConfig
|
|
|
|
// Called when the leases DB is modified
|
|
onLeaseChanged []OnLeaseChangedT
|
|
}
|
|
|
|
// type check
|
|
var _ Interface = (*server)(nil)
|
|
|
|
// Create initializes and returns the DHCP server handling both address
|
|
// families. It also registers the corresponding HTTP API endpoints.
|
|
func Create(conf *ServerConfig) (s *server, err error) {
|
|
s = &server{
|
|
conf: &ServerConfig{
|
|
ConfigModified: conf.ConfigModified,
|
|
|
|
HTTPRegister: conf.HTTPRegister,
|
|
|
|
Enabled: conf.Enabled,
|
|
InterfaceName: conf.InterfaceName,
|
|
|
|
LocalDomainName: conf.LocalDomainName,
|
|
|
|
dbFilePath: filepath.Join(conf.DataDir, dataFilename),
|
|
},
|
|
}
|
|
|
|
// TODO(e.burkov): Don't register handlers, see TODO on
|
|
// [aghhttp.RegisterFunc].
|
|
s.registerHandlers()
|
|
|
|
v4Enabled, v6Enabled, err := s.setServers(conf)
|
|
if err != nil {
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
return nil, err
|
|
}
|
|
|
|
s.conf.Conf4 = conf.Conf4
|
|
s.conf.Conf6 = conf.Conf6
|
|
|
|
if s.conf.Enabled && !v4Enabled && !v6Enabled {
|
|
return nil, fmt.Errorf("neither dhcpv4 nor dhcpv6 srv is configured")
|
|
}
|
|
|
|
// Migrate leases db if needed.
|
|
err = migrateDB(conf)
|
|
if err != nil {
|
|
// Don't wrap the error since it's informative enough as is.
|
|
return nil, err
|
|
}
|
|
|
|
// Don't delay database loading until the DHCP server is started,
|
|
// because we need static leases functionality available beforehand.
|
|
err = s.dbLoad()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("loading db: %w", err)
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
// setServers updates DHCPv4 and DHCPv6 servers created from the provided
|
|
// configuration conf.
|
|
func (s *server) setServers(conf *ServerConfig) (v4Enabled, v6Enabled bool, err error) {
|
|
v4conf := conf.Conf4
|
|
v4conf.InterfaceName = s.conf.InterfaceName
|
|
v4conf.notify = s.onNotify
|
|
v4conf.Enabled = s.conf.Enabled && v4conf.RangeStart.IsValid()
|
|
|
|
s.srv4, err = v4Create(&v4conf)
|
|
if err != nil {
|
|
if v4conf.Enabled {
|
|
return true, false, fmt.Errorf("creating dhcpv4 srv: %w", err)
|
|
}
|
|
|
|
log.Debug("dhcpd: warning: creating dhcpv4 srv: %s", err)
|
|
}
|
|
|
|
v6conf := conf.Conf6
|
|
v6conf.InterfaceName = s.conf.InterfaceName
|
|
v6conf.notify = s.onNotify
|
|
v6conf.Enabled = s.conf.Enabled
|
|
if len(v6conf.RangeStart) == 0 {
|
|
v6conf.Enabled = false
|
|
}
|
|
|
|
s.srv6, err = v6Create(v6conf)
|
|
if err != nil {
|
|
return v4conf.Enabled, v6conf.Enabled, fmt.Errorf("creating dhcpv6 srv: %w", err)
|
|
}
|
|
|
|
return v4conf.Enabled, v6conf.Enabled, nil
|
|
}
|
|
|
|
// Enabled returns true when the server is enabled.
|
|
func (s *server) Enabled() (ok bool) {
|
|
return s.conf.Enabled
|
|
}
|
|
|
|
// resetLeases resets all leases in the lease database.
|
|
func (s *server) resetLeases() (err error) {
|
|
err = s.srv4.ResetLeases(nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if s.srv6 != nil {
|
|
err = s.srv6.ResetLeases(nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return s.dbStore()
|
|
}
|
|
|
|
// server calls this function after DB is updated
|
|
func (s *server) onNotify(flags uint32) {
|
|
if flags == LeaseChangedDBStore {
|
|
err := s.dbStore()
|
|
if err != nil {
|
|
log.Error("updating db: %s", err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
s.notify(int(flags))
|
|
}
|
|
|
|
// SetOnLeaseChanged - set callback
|
|
func (s *server) SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT) {
|
|
s.onLeaseChanged = append(s.onLeaseChanged, onLeaseChanged)
|
|
}
|
|
|
|
func (s *server) notify(flags int) {
|
|
for _, f := range s.onLeaseChanged {
|
|
f(flags)
|
|
}
|
|
}
|
|
|
|
// WriteDiskConfig - write configuration
|
|
func (s *server) WriteDiskConfig(c *ServerConfig) {
|
|
c.Enabled = s.conf.Enabled
|
|
c.InterfaceName = s.conf.InterfaceName
|
|
c.LocalDomainName = s.conf.LocalDomainName
|
|
|
|
s.srv4.WriteDiskConfig4(&c.Conf4)
|
|
s.srv6.WriteDiskConfig6(&c.Conf6)
|
|
}
|
|
|
|
// Start will listen on port 67 and serve DHCP requests.
|
|
func (s *server) Start() (err error) {
|
|
err = s.srv4.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.srv6.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Stop closes the listening UDP socket
|
|
func (s *server) Stop() (err error) {
|
|
err = s.srv4.Stop()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.srv6.Stop()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Leases returns the list of active IPv4 and IPv6 DHCP leases. It's safe for
|
|
// concurrent use.
|
|
func (s *server) Leases(flags GetLeasesFlags) (leases []*Lease) {
|
|
return append(s.srv4.GetLeases(flags), s.srv6.GetLeases(flags)...)
|
|
}
|
|
|
|
// FindMACbyIP returns a MAC address by the IP address of its lease, if there is
|
|
// one.
|
|
func (s *server) FindMACbyIP(ip netip.Addr) (mac net.HardwareAddr) {
|
|
if ip.Is4() {
|
|
return s.srv4.FindMACbyIP(ip)
|
|
}
|
|
|
|
return s.srv6.FindMACbyIP(ip)
|
|
}
|
|
|
|
// AddStaticLease - add static v4 lease
|
|
func (s *server) AddStaticLease(l *Lease) error {
|
|
return s.srv4.AddStaticLease(l)
|
|
}
|