2020-11-16 16:01:12 +00:00
|
|
|
// Package dhcpd provides a DHCP server.
|
2018-12-28 14:17:51 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2021-01-20 12:59:24 +00:00
|
|
|
"fmt"
|
2018-12-28 14:17:51 +00:00
|
|
|
"net"
|
2023-03-01 10:27:24 +00:00
|
|
|
"net/netip"
|
2019-08-19 12:27:02 +01:00
|
|
|
"path/filepath"
|
2018-12-28 14:17:51 +00:00
|
|
|
"time"
|
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2022-09-13 21:45:35 +01:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2018-12-28 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
const (
|
2022-09-13 21:45:35 +01:00
|
|
|
// 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
|
2020-11-16 16:01:12 +00:00
|
|
|
)
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
// Currently used defaults for ifaceDNSAddrs.
|
|
|
|
const (
|
|
|
|
defaultMaxAttempts int = 10
|
|
|
|
defaultBackoff time.Duration = 500 * time.Millisecond
|
|
|
|
)
|
2020-02-18 16:27:09 +00:00
|
|
|
|
2020-12-07 12:38:05 +00:00
|
|
|
// OnLeaseChangedT is a callback for lease changes.
|
2020-07-03 16:20:01 +01:00
|
|
|
type OnLeaseChangedT func(flags int)
|
2020-01-30 07:25:02 +00:00
|
|
|
|
|
|
|
// flags for onLeaseChanged()
|
|
|
|
const (
|
|
|
|
LeaseChangedAdded = iota
|
|
|
|
LeaseChangedAddedStatic
|
|
|
|
LeaseChangedRemovedStatic
|
2021-04-15 15:52:53 +01:00
|
|
|
LeaseChangedRemovedAll
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
LeaseChangedDBStore
|
2020-01-30 07:25:02 +00:00
|
|
|
)
|
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
// GetLeasesFlags are the flags for GetLeases.
|
|
|
|
type GetLeasesFlags uint8
|
|
|
|
|
|
|
|
// GetLeasesFlags values
|
|
|
|
const (
|
2022-09-13 21:45:35 +01:00
|
|
|
LeasesDynamic GetLeasesFlags = 0b01
|
|
|
|
LeasesStatic GetLeasesFlags = 0b10
|
2021-06-16 14:48:46 +01:00
|
|
|
|
|
|
|
LeasesAll = LeasesDynamic | LeasesStatic
|
|
|
|
)
|
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
// Interface is the DHCP server that deals with both IP address families.
|
|
|
|
type Interface interface {
|
|
|
|
Start() (err error)
|
|
|
|
Stop() (err error)
|
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// Enabled returns true if the DHCP server is running.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Currently, we need this method to determine whether the
|
|
|
|
// local domain suffix should be considered while resolving A/AAAA requests.
|
|
|
|
// This is because other parts of the code aren't aware of the DNS suffixes
|
|
|
|
// in DHCP clients names and caller is responsible for trimming it. This
|
|
|
|
// behavior should be changed in the future.
|
|
|
|
Enabled() (ok bool)
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// Leases returns all the leases in the database.
|
|
|
|
Leases() (leases []*dhcpsvc.Lease)
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// MacByIP returns the MAC address of a client with ip. It returns nil if
|
|
|
|
// there is no such client, due to an assumption that a DHCP client must
|
|
|
|
// always have a HardwareAddr.
|
|
|
|
MACByIP(ip netip.Addr) (mac net.HardwareAddr)
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// HostByIP returns the hostname of the DHCP client with the given IP
|
|
|
|
// address. The address will be netip.Addr{} if there is no such client,
|
|
|
|
// due to an assumption that a DHCP client must always have an IP address.
|
|
|
|
HostByIP(ip netip.Addr) (host string)
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// IPByHost returns the IP address of the DHCP client with the given
|
|
|
|
// hostname. The address will be netip.Addr{} if there is no such client,
|
|
|
|
// due to an assumption that a DHCP client must always have an IP address.
|
|
|
|
IPByHost(host string) (ip netip.Addr)
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
WriteDiskConfig(c *ServerConfig)
|
2023-03-01 10:27:24 +00:00
|
|
|
}
|
2022-09-13 21:45:35 +01:00
|
|
|
|
|
|
|
// 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
|
2019-03-06 12:13:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
// 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{
|
2022-01-25 16:47:02 +00:00
|
|
|
conf: &ServerConfig{
|
|
|
|
ConfigModified: conf.ConfigModified,
|
2020-11-16 16:01:12 +00:00
|
|
|
|
2022-01-25 16:47:02 +00:00
|
|
|
HTTPRegister: conf.HTTPRegister,
|
|
|
|
|
|
|
|
Enabled: conf.Enabled,
|
|
|
|
InterfaceName: conf.InterfaceName,
|
|
|
|
|
|
|
|
LocalDomainName: conf.LocalDomainName,
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
dbFilePath: filepath.Join(conf.DataDir, dataFilename),
|
2022-01-25 16:47:02 +00:00
|
|
|
},
|
|
|
|
}
|
2019-11-06 10:04:01 +00:00
|
|
|
|
2022-11-25 15:21:25 +00:00
|
|
|
// TODO(e.burkov): Don't register handlers, see TODO on
|
|
|
|
// [aghhttp.RegisterFunc].
|
2022-09-13 21:45:35 +01:00
|
|
|
s.registerHandlers()
|
2020-04-07 09:48:03 +01:00
|
|
|
|
2023-05-26 10:50:03 +01:00
|
|
|
v4Enabled, v6Enabled, err := s.setServers(conf)
|
2021-06-16 14:48:46 +01:00
|
|
|
if err != nil {
|
2023-05-26 10:50:03 +01:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return nil, err
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:49:03 +00:00
|
|
|
s.conf.Conf4 = conf.Conf4
|
|
|
|
s.conf.Conf6 = conf.Conf6
|
|
|
|
|
2023-05-26 10:50:03 +01:00
|
|
|
if s.conf.Enabled && !v4Enabled && !v6Enabled {
|
2021-06-16 14:48:46 +01:00
|
|
|
return nil, fmt.Errorf("neither dhcpv4 nor dhcpv6 srv is configured")
|
2020-08-04 12:18:35 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
// 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
|
2019-11-22 11:21:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:50:03 +01:00
|
|
|
// setServers updates DHCPv4 and DHCPv6 servers created from the provided
|
2023-08-30 12:02:12 +01:00
|
|
|
// configuration conf. It returns the status of both the DHCPv4 and the DHCPv6
|
|
|
|
// servers, which is always false for corresponding server on any error.
|
2023-05-26 10:50:03 +01:00
|
|
|
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 {
|
2023-08-30 12:02:12 +01:00
|
|
|
return false, false, fmt.Errorf("creating dhcpv4 srv: %w", err)
|
2023-05-26 10:50:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("dhcpd: warning: creating dhcpv4 srv: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v6conf := conf.Conf6
|
|
|
|
v6conf.InterfaceName = s.conf.InterfaceName
|
|
|
|
v6conf.notify = s.onNotify
|
2023-08-30 12:02:12 +01:00
|
|
|
v6conf.Enabled = s.conf.Enabled && len(v6conf.RangeStart) != 0
|
2023-05-26 10:50:03 +01:00
|
|
|
|
|
|
|
s.srv6, err = v6Create(v6conf)
|
|
|
|
if err != nil {
|
2023-08-30 12:02:12 +01:00
|
|
|
return v4conf.Enabled, false, fmt.Errorf("creating dhcpv6 srv: %w", err)
|
2023-05-26 10:50:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return v4conf.Enabled, v6conf.Enabled, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:00:07 +01:00
|
|
|
// Enabled returns true when the server is enabled.
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) Enabled() (ok bool) {
|
2021-04-29 14:00:07 +01:00
|
|
|
return s.conf.Enabled
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
// resetLeases resets all leases in the lease database.
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) resetLeases() (err error) {
|
2021-06-16 14:48:46 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
// server calls this function after DB is updated
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) onNotify(flags uint32) {
|
2020-07-03 16:20:01 +01:00
|
|
|
if flags == LeaseChangedDBStore {
|
2021-06-16 14:48:46 +01:00
|
|
|
err := s.dbStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("updating db: %s", err)
|
|
|
|
}
|
2021-03-18 14:07:13 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
s.notify(int(flags))
|
2019-05-14 13:49:53 +01:00
|
|
|
}
|
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) notify(flags int) {
|
2020-06-23 10:13:13 +01:00
|
|
|
for _, f := range s.onLeaseChanged {
|
|
|
|
f(flags)
|
|
|
|
}
|
2020-01-30 07:25:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:21:08 +00:00
|
|
|
// WriteDiskConfig - write configuration
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) WriteDiskConfig(c *ServerConfig) {
|
2020-07-03 16:20:01 +01:00
|
|
|
c.Enabled = s.conf.Enabled
|
|
|
|
c.InterfaceName = s.conf.InterfaceName
|
2022-01-25 16:47:02 +00:00
|
|
|
c.LocalDomainName = s.conf.LocalDomainName
|
2022-09-13 21:45:35 +01:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
s.srv4.WriteDiskConfig4(&c.Conf4)
|
|
|
|
s.srv6.WriteDiskConfig6(&c.Conf6)
|
2019-05-14 13:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start will listen on port 67 and serve DHCP requests.
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) Start() (err error) {
|
2021-04-21 12:42:19 +01:00
|
|
|
err = s.srv4.Start()
|
2019-05-14 13:49:53 +01:00
|
|
|
if err != nil {
|
2020-07-03 16:20:01 +01:00
|
|
|
return err
|
2019-05-14 13:49:53 +01:00
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
err = s.srv6.Start()
|
2018-12-28 14:17:51 +00:00
|
|
|
if err != nil {
|
2020-07-03 16:20:01 +01:00
|
|
|
return err
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// Stop closes the listening UDP socket
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) Stop() (err error) {
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.srv4.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-14 11:02:04 +01:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.srv6.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-30 07:25:02 +00:00
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// Leases returns the list of active DHCP leases.
|
|
|
|
func (s *server) Leases() (leases []*dhcpsvc.Lease) {
|
2023-11-16 11:14:40 +00:00
|
|
|
return append(s.srv4.GetLeases(LeasesAll), s.srv6.GetLeases(LeasesAll)...)
|
2019-05-28 12:11:47 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// MACByIP returns a MAC address by the IP address of its lease, if there is
|
2023-03-01 10:27:24 +00:00
|
|
|
// one.
|
2023-08-30 12:02:12 +01:00
|
|
|
func (s *server) MACByIP(ip netip.Addr) (mac net.HardwareAddr) {
|
2023-03-01 10:27:24 +00:00
|
|
|
if ip.Is4() {
|
2020-07-03 16:20:01 +01:00
|
|
|
return s.srv4.FindMACbyIP(ip)
|
2019-12-23 13:59:02 +00:00
|
|
|
}
|
2023-03-01 10:27:24 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
return s.srv6.FindMACbyIP(ip)
|
2019-09-26 14:40:52 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 12:02:12 +01:00
|
|
|
// HostByIP implements the [Interface] interface for *server.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Implement this method for DHCPv6.
|
|
|
|
func (s *server) HostByIP(ip netip.Addr) (host string) {
|
|
|
|
if ip.Is4() {
|
|
|
|
return s.srv4.HostByIP(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPByHost implements the [Interface] interface for *server.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Implement this method for DHCPv6.
|
|
|
|
func (s *server) IPByHost(host string) (ip netip.Addr) {
|
|
|
|
return s.srv4.IPByHost(host)
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
// AddStaticLease - add static v4 lease
|
2023-11-16 11:14:40 +00:00
|
|
|
func (s *server) AddStaticLease(l *dhcpsvc.Lease) error {
|
2021-06-16 14:48:46 +01:00
|
|
|
return s.srv4.AddStaticLease(l)
|
2019-03-01 17:03:22 +00:00
|
|
|
}
|