2018-12-28 14:17:51 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-10-11 16:56:18 +00:00
|
|
|
"net/http"
|
2019-08-19 11:27:02 +00:00
|
|
|
"path/filepath"
|
2018-12-28 14:17:51 +00:00
|
|
|
"time"
|
|
|
|
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-28 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultDiscoverTime = time.Second * 3
|
2019-05-14 10:02:04 +00:00
|
|
|
const leaseExpireStatic = 1
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2020-02-18 16:27:09 +00:00
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// Lease contains the necessary information about a DHCP lease
|
2018-12-28 14:17:51 +00:00
|
|
|
type Lease struct {
|
2020-07-03 15:20:01 +00:00
|
|
|
HWAddr net.HardwareAddr `json:"mac"`
|
2018-12-29 13:44:07 +00:00
|
|
|
IP net.IP `json:"ip"`
|
|
|
|
Hostname string `json:"hostname"`
|
2019-05-14 10:02:04 +00:00
|
|
|
|
|
|
|
// Lease expiration time
|
|
|
|
// 1: static lease
|
|
|
|
Expiry time.Time `json:"expires"`
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// ServerConfig - DHCP server configuration
|
2018-12-28 14:17:51 +00:00
|
|
|
// field ordering is important -- yaml fields will mirror ordering from here
|
|
|
|
type ServerConfig struct {
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
InterfaceName string `yaml:"interface_name"`
|
2019-03-07 13:48:55 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
Conf4 V4ServerConf `yaml:"dhcpv4"`
|
|
|
|
Conf6 V6ServerConf `yaml:"dhcpv6"`
|
2019-10-14 09:12:06 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
WorkDir string `yaml:"-"`
|
|
|
|
DBFilePath string `yaml:"-"` // path to DB file
|
2019-10-11 16:56:18 +00:00
|
|
|
|
|
|
|
// Called when the configuration is changed by HTTP request
|
2020-07-03 15:20:01 +00:00
|
|
|
ConfigModified func() `yaml:"-"`
|
2019-10-11 16:56:18 +00:00
|
|
|
|
|
|
|
// Register an HTTP handler
|
2020-07-03 15:20:01 +00:00
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
type OnLeaseChangedT func(flags int)
|
2020-01-30 07:25:02 +00:00
|
|
|
|
|
|
|
// flags for onLeaseChanged()
|
|
|
|
const (
|
|
|
|
LeaseChangedAdded = iota
|
|
|
|
LeaseChangedAddedStatic
|
|
|
|
LeaseChangedRemovedStatic
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
LeaseChangedDBStore
|
2020-01-30 07:25:02 +00:00
|
|
|
)
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// Server - the current state of the DHCP server
|
2018-12-28 14:17:51 +00:00
|
|
|
type Server struct {
|
2020-07-03 15:20:01 +00:00
|
|
|
srv4 DHCPServer
|
|
|
|
srv6 DHCPServer
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2019-05-14 14:49:45 +00:00
|
|
|
conf ServerConfig
|
2020-01-30 07:25:02 +00:00
|
|
|
|
|
|
|
// Called when the leases DB is modified
|
2020-07-03 15:20:01 +00:00
|
|
|
onLeaseChanged []OnLeaseChangedT
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
type ServerInterface interface {
|
|
|
|
Leases(flags int) []Lease
|
|
|
|
SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)
|
2019-03-06 12:13:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 12:50:07 +00:00
|
|
|
// CheckConfig checks the configuration
|
|
|
|
func (s *Server) CheckConfig(config ServerConfig) error {
|
2020-07-03 15:20:01 +00:00
|
|
|
return nil
|
2019-05-14 12:50:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:21:08 +00:00
|
|
|
// Create - create object
|
|
|
|
func Create(config ServerConfig) *Server {
|
|
|
|
s := Server{}
|
2020-07-03 15:20:01 +00:00
|
|
|
s.conf.Enabled = config.Enabled
|
|
|
|
s.conf.InterfaceName = config.InterfaceName
|
|
|
|
s.conf.HTTPRegister = config.HTTPRegister
|
|
|
|
s.conf.ConfigModified = config.ConfigModified
|
2019-11-06 10:04:01 +00:00
|
|
|
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
|
|
|
|
|
2020-04-07 08:48:03 +00:00
|
|
|
if !webHandlersRegistered && s.conf.HTTPRegister != nil {
|
|
|
|
webHandlersRegistered = true
|
|
|
|
s.registerHandlers()
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
var err4, err6 error
|
|
|
|
v4conf := config.Conf4
|
|
|
|
v4conf.Enabled = s.conf.Enabled
|
|
|
|
if len(v4conf.RangeStart) == 0 {
|
|
|
|
v4conf.Enabled = false
|
|
|
|
}
|
|
|
|
v4conf.InterfaceName = s.conf.InterfaceName
|
|
|
|
v4conf.notify = s.onNotify
|
|
|
|
s.srv4, err4 = v4Create(v4conf)
|
|
|
|
|
|
|
|
v6conf := config.Conf6
|
|
|
|
v6conf.Enabled = s.conf.Enabled
|
|
|
|
if len(v6conf.RangeStart) == 0 {
|
|
|
|
v6conf.Enabled = false
|
|
|
|
}
|
|
|
|
v6conf.InterfaceName = s.conf.InterfaceName
|
|
|
|
v6conf.notify = s.onNotify
|
|
|
|
s.srv6, err6 = v6Create(v6conf)
|
|
|
|
|
|
|
|
if err4 != nil {
|
|
|
|
log.Error("%s", err4)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err6 != nil {
|
|
|
|
log.Error("%s", err6)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:18:35 +00:00
|
|
|
if s.conf.Enabled && !v4conf.Enabled && !v6conf.Enabled {
|
|
|
|
log.Error("Can't enable DHCP server because neither DHCPv4 nor DHCPv6 servers are configured")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 10:04:01 +00:00
|
|
|
// we can't delay database loading until DHCP server is started,
|
|
|
|
// because we need static leases functionality available beforehand
|
|
|
|
s.dbLoad()
|
2019-11-22 11:21:08 +00:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
// server calls this function after DB is updated
|
|
|
|
func (s *Server) onNotify(flags uint32) {
|
|
|
|
if flags == LeaseChangedDBStore {
|
|
|
|
s.dbStore()
|
|
|
|
return
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
s.notify(int(flags))
|
2019-05-14 12:49:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:25:02 +00:00
|
|
|
// SetOnLeaseChanged - set callback
|
2020-07-03 15:20:01 +00:00
|
|
|
func (s *Server) SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT) {
|
2020-06-23 09:13:13 +00:00
|
|
|
s.onLeaseChanged = append(s.onLeaseChanged, onLeaseChanged)
|
2020-01-30 07:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) notify(flags int) {
|
2020-06-23 09:13:13 +00:00
|
|
|
if len(s.onLeaseChanged) == 0 {
|
2020-01-30 07:25:02 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-23 09:13:13 +00: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
|
|
|
|
func (s *Server) WriteDiskConfig(c *ServerConfig) {
|
2020-07-03 15:20:01 +00:00
|
|
|
c.Enabled = s.conf.Enabled
|
|
|
|
c.InterfaceName = s.conf.InterfaceName
|
|
|
|
s.srv4.WriteDiskConfig4(&c.Conf4)
|
|
|
|
s.srv6.WriteDiskConfig6(&c.Conf6)
|
2019-05-14 12:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start will listen on port 67 and serve DHCP requests.
|
|
|
|
func (s *Server) Start() error {
|
2020-07-03 15:20:01 +00:00
|
|
|
err := s.srv4.Start()
|
2019-05-14 12:49:53 +00:00
|
|
|
if err != nil {
|
2020-07-03 15:20:01 +00:00
|
|
|
log.Error("DHCPv4: start: %s", err)
|
|
|
|
return err
|
2019-05-14 12:49:53 +00:00
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
err = s.srv6.Start()
|
2018-12-28 14:17:51 +00:00
|
|
|
if err != nil {
|
2020-07-03 15:20:01 +00:00
|
|
|
log.Error("DHCPv6: start: %s", err)
|
|
|
|
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
|
2020-07-03 15:20:01 +00:00
|
|
|
func (s *Server) Stop() {
|
|
|
|
s.srv4.Stop()
|
|
|
|
s.srv6.Stop()
|
2019-05-14 10:02:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:25:02 +00:00
|
|
|
// flags for Leases() function
|
|
|
|
const (
|
|
|
|
LeasesDynamic = 1
|
|
|
|
LeasesStatic = 2
|
|
|
|
LeasesAll = LeasesDynamic | LeasesStatic
|
|
|
|
)
|
|
|
|
|
2019-02-28 15:24:01 +00:00
|
|
|
// Leases returns the list of current DHCP leases (thread-safe)
|
2020-01-30 07:25:02 +00:00
|
|
|
func (s *Server) Leases(flags int) []Lease {
|
2020-07-03 15:20:01 +00:00
|
|
|
result := s.srv4.GetLeases(flags)
|
2019-03-01 17:03:22 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
v6leases := s.srv6.GetLeases(flags)
|
|
|
|
result = append(result, v6leases...)
|
2019-03-06 12:13:27 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
return result
|
2019-05-28 11:11:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 13:40:52 +00:00
|
|
|
// FindMACbyIP - find a MAC address by IP address in the currently active DHCP leases
|
|
|
|
func (s *Server) FindMACbyIP(ip net.IP) net.HardwareAddr {
|
2020-07-03 15:20:01 +00:00
|
|
|
if ip.To4() != nil {
|
|
|
|
return s.srv4.FindMACbyIP(ip)
|
2019-12-23 13:59:02 +00:00
|
|
|
}
|
2020-07-03 15:20:01 +00:00
|
|
|
return s.srv6.FindMACbyIP(ip)
|
2019-09-26 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
// AddStaticLease - add static v4 lease
|
|
|
|
func (s *Server) AddStaticLease(lease Lease) error {
|
|
|
|
return s.srv4.AddStaticLease(lease)
|
2019-03-01 17:03:22 +00:00
|
|
|
}
|