2022-09-13 23:45:35 +03:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-10-14 12:12:06 +03:00
|
|
|
"os"
|
2019-10-11 19:56:18 +03:00
|
|
|
|
2022-06-09 17:47:05 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-16 20:54:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2021-03-16 19:42:15 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-10-11 19:56:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
type v4ServerConfJSON struct {
|
2021-01-13 16:56:05 +03:00
|
|
|
GatewayIP net.IP `json:"gateway_ip"`
|
|
|
|
SubnetMask net.IP `json:"subnet_mask"`
|
|
|
|
RangeStart net.IP `json:"range_start"`
|
|
|
|
RangeEnd net.IP `json:"range_end"`
|
2020-07-03 18:20:01 +03:00
|
|
|
LeaseDuration uint32 `json:"lease_duration"`
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (j *v4ServerConfJSON) toServerConf() *V4ServerConf {
|
2021-02-11 20:49:03 +03:00
|
|
|
if j == nil {
|
2022-09-13 23:45:35 +03:00
|
|
|
return &V4ServerConf{}
|
2021-02-11 20:49:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
return &V4ServerConf{
|
2021-01-20 15:59:24 +03:00
|
|
|
GatewayIP: j.GatewayIP,
|
|
|
|
SubnetMask: j.SubnetMask,
|
|
|
|
RangeStart: j.RangeStart,
|
|
|
|
RangeEnd: j.RangeEnd,
|
2020-07-03 18:20:01 +03:00
|
|
|
LeaseDuration: j.LeaseDuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type v6ServerConfJSON struct {
|
2021-01-20 17:27:53 +03:00
|
|
|
RangeStart net.IP `json:"range_start"`
|
2020-07-03 18:20:01 +03:00
|
|
|
LeaseDuration uint32 `json:"lease_duration"`
|
|
|
|
}
|
|
|
|
|
2021-02-11 20:49:03 +03:00
|
|
|
func v6JSONToServerConf(j *v6ServerConfJSON) V6ServerConf {
|
|
|
|
if j == nil {
|
|
|
|
return V6ServerConf{}
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
return V6ServerConf{
|
|
|
|
RangeStart: j.RangeStart,
|
|
|
|
LeaseDuration: j.LeaseDuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:59:24 +03:00
|
|
|
// dhcpStatusResponse is the response for /control/dhcp/status endpoint.
|
|
|
|
type dhcpStatusResponse struct {
|
|
|
|
IfaceName string `json:"interface_name"`
|
|
|
|
V4 V4ServerConf `json:"v4"`
|
|
|
|
V6 V6ServerConf `json:"v6"`
|
2021-06-16 16:48:46 +03:00
|
|
|
Leases []*Lease `json:"leases"`
|
|
|
|
StaticLeases []*Lease `json:"static_leases"`
|
|
|
|
Enabled bool `json:"enabled"`
|
2021-01-20 15:59:24 +03:00
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
|
2021-01-20 15:59:24 +03:00
|
|
|
status := &dhcpStatusResponse{
|
|
|
|
Enabled: s.conf.Enabled,
|
|
|
|
IfaceName: s.conf.InterfaceName,
|
|
|
|
V4: V4ServerConf{},
|
|
|
|
V6: V6ServerConf{},
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-01-20 15:59:24 +03:00
|
|
|
s.srv4.WriteDiskConfig4(&status.V4)
|
|
|
|
s.srv6.WriteDiskConfig6(&status.V6)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-01-20 15:59:24 +03:00
|
|
|
status.Leases = s.Leases(LeasesDynamic)
|
|
|
|
status.StaticLeases = s.Leases(LeasesStatic)
|
2019-10-11 19:56:18 +03:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-09-13 23:45:35 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
err := json.NewEncoder(w).Encode(status)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"Unable to marshal DHCP status json: %s",
|
|
|
|
err,
|
|
|
|
)
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) enableDHCP(ifaceName string) (code int, err error) {
|
2021-02-05 15:17:18 +03:00
|
|
|
var hasStaticIP bool
|
2021-03-16 19:42:15 +03:00
|
|
|
hasStaticIP, err = aghnet.IfaceHasStaticIP(ifaceName)
|
2021-02-05 15:17:18 +03:00
|
|
|
if err != nil {
|
2021-02-12 16:40:34 +03:00
|
|
|
if errors.Is(err, os.ErrPermission) {
|
2022-09-13 23:45:35 +03:00
|
|
|
// ErrPermission may happen here on Linux systems where AdGuard Home
|
|
|
|
// is installed using Snap. That doesn't necessarily mean that the
|
|
|
|
// machine doesn't have a static IP, so we can assume that it has
|
|
|
|
// and go on. If the machine doesn't, we'll get an error later.
|
2021-02-15 19:07:08 +03:00
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/2667.
|
|
|
|
//
|
2022-09-13 23:45:35 +03:00
|
|
|
// TODO(a.garipov): I was thinking about moving this into
|
|
|
|
// IfaceHasStaticIP, but then we wouldn't be able to log it. Think
|
|
|
|
// about it more.
|
2021-02-12 16:40:34 +03:00
|
|
|
log.Info("error while checking static ip: %s; "+
|
|
|
|
"assuming machine has static ip and going on", err)
|
|
|
|
hasStaticIP = true
|
2021-03-16 19:42:15 +03:00
|
|
|
} else if errors.Is(err, aghnet.ErrNoStaticIPInfo) {
|
2022-09-13 23:45:35 +03:00
|
|
|
// Couldn't obtain a definitive answer. Assume static IP an go on.
|
2021-02-15 19:07:08 +03:00
|
|
|
log.Info("can't check for static ip; " +
|
|
|
|
"assuming machine has static ip and going on")
|
|
|
|
hasStaticIP = true
|
2021-02-12 16:40:34 +03:00
|
|
|
} else {
|
2021-02-15 19:07:08 +03:00
|
|
|
err = fmt.Errorf("checking static ip: %w", err)
|
|
|
|
|
|
|
|
return http.StatusInternalServerError, err
|
2021-02-12 16:40:34 +03:00
|
|
|
}
|
2021-02-05 15:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hasStaticIP {
|
2021-03-16 19:42:15 +03:00
|
|
|
err = aghnet.IfaceSetStaticIP(ifaceName)
|
2021-02-05 15:17:18 +03:00
|
|
|
if err != nil {
|
2021-02-15 19:07:08 +03:00
|
|
|
err = fmt.Errorf("setting static ip: %w", err)
|
|
|
|
|
|
|
|
return http.StatusInternalServerError, err
|
2021-02-05 15:17:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Start()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, fmt.Errorf("starting dhcp server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 20:49:03 +03:00
|
|
|
type dhcpServerConfigJSON struct {
|
|
|
|
V4 *v4ServerConfJSON `json:"v4"`
|
|
|
|
V6 *v6ServerConfJSON `json:"v6"`
|
|
|
|
InterfaceName string `json:"interface_name"`
|
2022-06-09 17:47:05 +03:00
|
|
|
Enabled aghalg.NullBool `json:"enabled"`
|
2021-02-11 20:49:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPSetConfigV4(
|
2021-06-16 16:48:46 +03:00
|
|
|
conf *dhcpServerConfigJSON,
|
2022-09-13 23:45:35 +03:00
|
|
|
) (srv DHCPServer, enabled bool, err error) {
|
2021-06-16 16:48:46 +03:00
|
|
|
if conf.V4 == nil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
v4Conf := conf.V4.toServerConf()
|
2022-06-09 17:47:05 +03:00
|
|
|
v4Conf.Enabled = conf.Enabled == aghalg.NBTrue
|
2021-06-16 16:48:46 +03:00
|
|
|
if len(v4Conf.RangeStart) == 0 {
|
|
|
|
v4Conf.Enabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
v4Conf.InterfaceName = conf.InterfaceName
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
// Set the default values for the fields not configurable via web API.
|
|
|
|
c4 := &V4ServerConf{
|
|
|
|
notify: s.onNotify,
|
|
|
|
ICMPTimeout: s.conf.Conf4.ICMPTimeout,
|
|
|
|
Options: s.conf.Conf4.Options,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.srv4.WriteDiskConfig4(c4)
|
2021-06-16 16:48:46 +03:00
|
|
|
v4Conf.notify = c4.notify
|
|
|
|
v4Conf.ICMPTimeout = c4.ICMPTimeout
|
|
|
|
v4Conf.Options = c4.Options
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
srv4, err := v4Create(v4Conf)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
return srv4, srv4.enabled(), err
|
2021-06-16 16:48:46 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPSetConfigV6(
|
2021-06-16 16:48:46 +03:00
|
|
|
conf *dhcpServerConfigJSON,
|
|
|
|
) (srv6 DHCPServer, enabled bool, err error) {
|
|
|
|
if conf.V6 == nil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v6Conf := v6JSONToServerConf(conf.V6)
|
2022-06-09 17:47:05 +03:00
|
|
|
v6Conf.Enabled = conf.Enabled == aghalg.NBTrue
|
2021-06-16 16:48:46 +03:00
|
|
|
if len(v6Conf.RangeStart) == 0 {
|
|
|
|
v6Conf.Enabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't overwrite the RA/SLAAC settings from the config file.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Perhaps include them into the request to allow
|
|
|
|
// changing them from the HTTP API?
|
|
|
|
v6Conf.RASLAACOnly = s.conf.Conf6.RASLAACOnly
|
|
|
|
v6Conf.RAAllowSLAAC = s.conf.Conf6.RAAllowSLAAC
|
|
|
|
|
|
|
|
enabled = v6Conf.Enabled
|
|
|
|
v6Conf.InterfaceName = conf.InterfaceName
|
|
|
|
v6Conf.notify = s.onNotify
|
|
|
|
|
|
|
|
srv6, err = v6Create(v6Conf)
|
|
|
|
|
|
|
|
return srv6, enabled, err
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
|
2021-06-16 16:48:46 +03:00
|
|
|
conf := &dhcpServerConfigJSON{}
|
2022-06-09 17:47:05 +03:00
|
|
|
conf.Enabled = aghalg.BoolToNullBool(s.conf.Enabled)
|
2021-02-11 20:49:03 +03:00
|
|
|
conf.InterfaceName = s.conf.InterfaceName
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err := json.NewDecoder(r.Body).Decode(conf)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "failed to parse new dhcp config json: %s", err)
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
srv4, v4Enabled, err := s.handleDHCPSetConfigV4(conf)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "bad dhcpv4 configuration: %s", err)
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
return
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
srv6, v6Enabled, err := s.handleDHCPSetConfigV6(conf)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "bad dhcpv6 configuration: %s", err)
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
return
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:47:05 +03:00
|
|
|
if conf.Enabled == aghalg.NBTrue && !v4Enabled && !v6Enabled {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "dhcpv4 or dhcpv6 configuration must be complete")
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.Stop()
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "stopping dhcp: %s", err)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2022-06-09 17:47:05 +03:00
|
|
|
if conf.Enabled != aghalg.NBNull {
|
|
|
|
s.conf.Enabled = conf.Enabled == aghalg.NBTrue
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 20:49:03 +03:00
|
|
|
if conf.InterfaceName != "" {
|
|
|
|
s.conf.InterfaceName = conf.InterfaceName
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
if srv4 != nil {
|
|
|
|
s.srv4 = srv4
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
if srv6 != nil {
|
|
|
|
s.srv6 = srv6
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2021-02-05 15:17:18 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
s.conf.ConfigModified()
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
err = s.dbLoad()
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "loading leases db: %s", err)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-10-11 19:56:18 +03:00
|
|
|
|
2020-08-13 18:45:27 +03:00
|
|
|
if s.conf.Enabled {
|
2021-02-05 15:17:18 +03:00
|
|
|
var code int
|
2021-02-11 20:49:03 +03:00
|
|
|
code, err = s.enableDHCP(conf.InterfaceName)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, code, "enabling dhcp: %s", err)
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:42:07 +03:00
|
|
|
type netInterfaceJSON struct {
|
2019-10-11 19:56:18 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
HardwareAddr string `json:"hardware_address"`
|
2022-06-09 17:47:05 +03:00
|
|
|
Flags string `json:"flags"`
|
|
|
|
GatewayIP net.IP `json:"gateway_ip"`
|
2021-01-13 16:56:05 +03:00
|
|
|
Addrs4 []net.IP `json:"ipv4_addresses"`
|
|
|
|
Addrs6 []net.IP `json:"ipv6_addresses"`
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
|
2021-01-20 15:59:24 +03:00
|
|
|
response := map[string]netInterfaceJSON{}
|
2019-10-11 19:56:18 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
ifaces, err := net.Interfaces()
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
|
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
|
|
// it's a loopback, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if iface.Flags&net.FlagBroadcast == 0 {
|
|
|
|
// this interface doesn't support broadcast, skip it
|
|
|
|
continue
|
|
|
|
}
|
2021-03-11 21:30:52 +03:00
|
|
|
|
|
|
|
var addrs []net.Addr
|
|
|
|
addrs, err = iface.Addrs()
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"Failed to get addresses for interface %s: %s",
|
|
|
|
iface.Name,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:42:07 +03:00
|
|
|
jsonIface := netInterfaceJSON{
|
2019-10-11 19:56:18 +03:00
|
|
|
Name: iface.Name,
|
|
|
|
HardwareAddr: iface.HardwareAddr.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.Flags != 0 {
|
|
|
|
jsonIface.Flags = iface.Flags.String()
|
|
|
|
}
|
|
|
|
// we don't want link-local addresses in json, so skip them
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ipnet, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
// not an IPNet, should not happen
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"got iface.Addrs() element %[1]s that is not net.IPNet, it is %[1]T",
|
|
|
|
addr)
|
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// ignore link-local
|
|
|
|
if ipnet.IP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-07 11:47:24 +03:00
|
|
|
if ipnet.IP.To4() != nil {
|
2021-01-13 16:56:05 +03:00
|
|
|
jsonIface.Addrs4 = append(jsonIface.Addrs4, ipnet.IP)
|
2020-08-07 11:47:24 +03:00
|
|
|
} else {
|
2021-01-13 16:56:05 +03:00
|
|
|
jsonIface.Addrs6 = append(jsonIface.Addrs6, ipnet.IP)
|
2020-08-07 11:47:24 +03:00
|
|
|
}
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
2020-08-07 11:47:24 +03:00
|
|
|
if len(jsonIface.Addrs4)+len(jsonIface.Addrs6) != 0 {
|
2021-03-16 19:42:15 +03:00
|
|
|
jsonIface.GatewayIP = aghnet.GatewayIP(iface.Name)
|
2019-10-11 19:56:18 +03:00
|
|
|
response[iface.Name] = jsonIface
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"Failed to marshal json with available interfaces: %s",
|
|
|
|
err,
|
|
|
|
)
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:59:24 +03:00
|
|
|
// dhcpSearchOtherResult contains information about other DHCP server for
|
|
|
|
// specific network interface.
|
|
|
|
type dhcpSearchOtherResult struct {
|
|
|
|
Found string `json:"found,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// dhcpStaticIPStatus contains information about static IP address for DHCP
|
|
|
|
// server.
|
|
|
|
type dhcpStaticIPStatus struct {
|
|
|
|
Static string `json:"static"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// dhcpSearchV4Result contains information about DHCPv4 server for specific
|
|
|
|
// network interface.
|
|
|
|
type dhcpSearchV4Result struct {
|
|
|
|
OtherServer dhcpSearchOtherResult `json:"other_server"`
|
|
|
|
StaticIP dhcpStaticIPStatus `json:"static_ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// dhcpSearchV6Result contains information about DHCPv6 server for specific
|
|
|
|
// network interface.
|
|
|
|
type dhcpSearchV6Result struct {
|
|
|
|
OtherServer dhcpSearchOtherResult `json:"other_server"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// dhcpSearchResult is a response for /control/dhcp/find_active_dhcp endpoint.
|
|
|
|
type dhcpSearchResult struct {
|
|
|
|
V4 dhcpSearchV4Result `json:"v4"`
|
|
|
|
V6 dhcpSearchV6Result `json:"v6"`
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
// findActiveServerReq is the JSON structure for the request to find active DHCP
|
|
|
|
// servers.
|
|
|
|
type findActiveServerReq struct {
|
|
|
|
Interface string `json:"interface"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDHCPFindActiveServer performs the following tasks:
|
|
|
|
// 1. searches for another DHCP server in the network;
|
|
|
|
// 2. check if a static IP is configured for the network interface;
|
|
|
|
// 3. responds with the results.
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
|
2022-09-29 19:04:26 +03:00
|
|
|
if aghhttp.WriteTextPlainDeprecated(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &findActiveServerReq{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(req)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2022-09-29 19:04:26 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "reading req: %s", err)
|
2021-08-12 17:33:53 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
ifaceName := req.Interface
|
2021-08-12 17:33:53 +03:00
|
|
|
if ifaceName == "" {
|
2022-09-29 19:04:26 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "empty interface name")
|
2021-08-12 17:33:53 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
result := &dhcpSearchResult{
|
2021-01-20 15:59:24 +03:00
|
|
|
V4: dhcpSearchV4Result{
|
2021-08-12 17:33:53 +03:00
|
|
|
OtherServer: dhcpSearchOtherResult{
|
|
|
|
Found: "no",
|
|
|
|
},
|
2021-07-30 15:27:24 +03:00
|
|
|
StaticIP: dhcpStaticIPStatus{
|
|
|
|
Static: "yes",
|
|
|
|
},
|
2021-01-20 15:59:24 +03:00
|
|
|
},
|
|
|
|
V6: dhcpSearchV6Result{
|
2021-08-12 17:33:53 +03:00
|
|
|
OtherServer: dhcpSearchOtherResult{
|
|
|
|
Found: "no",
|
|
|
|
},
|
2021-01-20 15:59:24 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
if isStaticIP, serr := aghnet.IfaceHasStaticIP(ifaceName); serr != nil {
|
2021-01-20 15:59:24 +03:00
|
|
|
result.V4.StaticIP.Static = "error"
|
2021-08-12 17:33:53 +03:00
|
|
|
result.V4.StaticIP.Error = serr.Error()
|
2019-10-11 19:56:18 +03:00
|
|
|
} else if !isStaticIP {
|
2021-01-20 15:59:24 +03:00
|
|
|
result.V4.StaticIP.Static = "no"
|
2021-08-12 17:33:53 +03:00
|
|
|
// TODO(e.burkov): The returned IP should only be of version 4.
|
|
|
|
result.V4.StaticIP.IP = aghnet.GetSubnet(ifaceName).String()
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
setOtherDHCPResult(ifaceName, result)
|
|
|
|
|
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setOtherDHCPResult sets the results of the check for another DHCP server in
|
|
|
|
// result.
|
|
|
|
func setOtherDHCPResult(ifaceName string, result *dhcpSearchResult) {
|
2021-08-12 17:33:53 +03:00
|
|
|
found4, found6, err4, err6 := aghnet.CheckOtherDHCP(ifaceName)
|
|
|
|
if err4 != nil {
|
2021-01-20 15:59:24 +03:00
|
|
|
result.V4.OtherServer.Found = "error"
|
|
|
|
result.V4.OtherServer.Error = err4.Error()
|
2021-08-12 17:33:53 +03:00
|
|
|
} else if found4 {
|
|
|
|
result.V4.OtherServer.Found = "yes"
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2022-09-29 19:04:26 +03:00
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
if err6 != nil {
|
2021-01-20 15:59:24 +03:00
|
|
|
result.V6.OtherServer.Found = "error"
|
|
|
|
result.V6.OtherServer.Error = err6.Error()
|
2021-08-12 17:33:53 +03:00
|
|
|
} else if found6 {
|
|
|
|
result.V6.OtherServer.Found = "yes"
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2019-10-11 19:56:18 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request) {
|
2021-06-16 16:48:46 +03:00
|
|
|
l := &Lease{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(l)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
2021-01-13 16:56:05 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
if l.IP == nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "invalid IP")
|
2021-01-13 16:56:05 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
var srv DHCPServer
|
|
|
|
if ip4 := l.IP.To4(); ip4 != nil {
|
|
|
|
l.IP = ip4
|
|
|
|
srv = s.srv4
|
|
|
|
} else {
|
2021-06-16 16:48:46 +03:00
|
|
|
l.IP = l.IP.To16()
|
2022-09-13 23:45:35 +03:00
|
|
|
srv = s.srv6
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2019-10-11 19:56:18 +03:00
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
err = srv.AddStaticLease(l)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
2021-01-13 16:56:05 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Request) {
|
2021-06-16 16:48:46 +03:00
|
|
|
l := &Lease{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(l)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
2021-01-13 16:56:05 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
if l.IP == nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "invalid IP")
|
2021-01-13 16:56:05 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
ip4 := l.IP.To4()
|
2021-01-13 16:56:05 +03:00
|
|
|
|
|
|
|
if ip4 == nil {
|
2021-06-16 16:48:46 +03:00
|
|
|
l.IP = l.IP.To16()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.srv6.RemoveStaticLease(l)
|
2020-07-03 18:20:01 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
l.IP = ip4
|
|
|
|
err = s.srv4.RemoveStaticLease(l)
|
2019-10-11 19:56:18 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
2021-01-13 16:56:05 +03:00
|
|
|
|
2019-10-11 19:56:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleReset(w http.ResponseWriter, r *http.Request) {
|
2021-06-16 16:48:46 +03:00
|
|
|
err := s.Stop()
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "stopping dhcp: %s", err)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-10-14 12:12:06 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = os.Remove(s.conf.DBFilePath)
|
2021-03-15 14:19:04 +03:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2021-06-16 16:48:46 +03:00
|
|
|
log.Error("dhcp: removing db: %s", err)
|
2019-10-14 12:12:06 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 19:47:02 +03:00
|
|
|
s.conf = &ServerConfig{
|
|
|
|
ConfigModified: s.conf.ConfigModified,
|
|
|
|
|
|
|
|
HTTPRegister: s.conf.HTTPRegister,
|
|
|
|
|
|
|
|
LocalDomainName: s.conf.LocalDomainName,
|
|
|
|
|
|
|
|
WorkDir: s.conf.WorkDir,
|
|
|
|
DBFilePath: s.conf.DBFilePath,
|
2021-11-24 13:57:50 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
v4conf := &V4ServerConf{
|
2021-11-24 13:57:50 +03:00
|
|
|
LeaseDuration: DefaultDHCPLeaseTTL,
|
|
|
|
ICMPTimeout: DefaultDHCPTimeoutICMP,
|
|
|
|
notify: s.onNotify,
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
s.srv4, _ = v4Create(v4conf)
|
|
|
|
|
2021-11-24 13:57:50 +03:00
|
|
|
v6conf := V6ServerConf{
|
|
|
|
LeaseDuration: DefaultDHCPLeaseTTL,
|
|
|
|
notify: s.onNotify,
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
s.srv6, _ = v6Create(v6conf)
|
|
|
|
|
2019-10-14 12:12:06 +03:00
|
|
|
s.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) handleResetLeases(w http.ResponseWriter, r *http.Request) {
|
2021-06-16 16:48:46 +03:00
|
|
|
err := s.resetLeases()
|
|
|
|
if err != nil {
|
|
|
|
msg := "resetting leases: %s"
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, msg, err)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:45:35 +03:00
|
|
|
func (s *server) registerHandlers() {
|
|
|
|
if s.conf.HTTPRegister == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/status", s.handleDHCPStatus)
|
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/interfaces", s.handleDHCPInterfaces)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/set_config", s.handleDHCPSetConfig)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset", s.handleReset)
|
2021-06-16 16:48:46 +03:00
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset_leases", s.handleResetLeases)
|
2020-11-16 19:01:12 +03:00
|
|
|
}
|