2021-05-21 14:55:42 +03:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 19:42:41 +03:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2018-12-28 17:17:51 +03:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2019-04-16 17:36:34 +03:00
|
|
|
"bytes"
|
2018-12-28 17:17:51 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-08-12 18:13:07 +03:00
|
|
|
"runtime"
|
2018-12-28 17:17:51 +03:00
|
|
|
"time"
|
|
|
|
|
2021-06-01 21:06:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-06-04 16:35:34 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-07-03 18:20:01 +03:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
2021-04-21 13:41:18 +03:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4/nclient4"
|
2020-08-13 18:45:27 +03:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6/nclient6"
|
2020-07-03 18:20:01 +03:00
|
|
|
"github.com/insomniacslk/dhcp/iana"
|
2018-12-28 17:17:51 +03:00
|
|
|
)
|
|
|
|
|
2020-08-13 18:45:27 +03:00
|
|
|
// CheckIfOtherDHCPServersPresentV4 sends a DHCP request to the specified network interface,
|
2019-01-24 20:11:01 +03:00
|
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
2021-03-11 21:30:52 +03:00
|
|
|
func CheckIfOtherDHCPServersPresentV4(ifaceName string) (ok bool, err error) {
|
2018-12-28 17:17:51 +03:00
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't find interface by name %s: %w", ifaceName, err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:39:43 +03:00
|
|
|
ifaceIPNet, err := ifaceIPAddrs(iface, ipVersion4)
|
2020-11-09 19:27:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("getting ipv4 addrs for iface %s: %w", ifaceName, err)
|
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
if len(ifaceIPNet) == 0 {
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, fmt.Errorf("interface %s has no ipv4 addresses", ifaceName)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
// TODO(a.garipov): Find out what this is about. Perhaps this
|
|
|
|
// information is outdated or at least incomplete.
|
2020-08-12 18:13:07 +03:00
|
|
|
if runtime.GOOS == "darwin" {
|
2021-06-04 16:35:34 +03:00
|
|
|
return false, aghos.Unsupported("CheckIfOtherDHCPServersPresentV4")
|
2020-08-12 18:13:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
srcIP := ifaceIPNet[0]
|
2021-06-01 21:06:55 +03:00
|
|
|
src := aghnet.JoinHostPort(srcIP.String(), 68)
|
2018-12-28 17:17:51 +03:00
|
|
|
dst := "255.255.255.255:67"
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
|
|
|
|
req, err := dhcpv4.NewDiscovery(iface.HardwareAddr)
|
2018-12-28 17:17:51 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv4.NewDiscovery: %w", err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
req.Options.Update(dhcpv4.OptClientIdentifier(iface.HardwareAddr))
|
|
|
|
req.Options.Update(dhcpv4.OptHostName(hostname))
|
2018-12-28 17:17:51 +03:00
|
|
|
|
|
|
|
// resolve 0.0.0.0:68
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp4", src)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't resolve UDP address %s: %w", src, err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !udpAddr.IP.To4().Equal(srcIP) {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("resolved UDP address is not %s: %w", src, err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve 255.255.255.255:67
|
|
|
|
dstAddr, err := net.ResolveUDPAddr("udp4", dst)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't resolve UDP address %s: %w", dst, err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// bind to 0.0.0.0:68
|
2018-12-29 17:23:42 +03:00
|
|
|
log.Tracef("Listening to udp4 %+v", udpAddr)
|
2020-08-12 18:13:07 +03:00
|
|
|
c, err := nclient4.NewRawUDPConn(ifaceName, 68)
|
2018-12-28 17:17:51 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't listen on :68: %w", err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
if c != nil {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2018-12-28 17:17:51 +03:00
|
|
|
|
|
|
|
// send to 255.255.255.255:67
|
2020-08-12 18:13:07 +03:00
|
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
2018-12-28 17:17:51 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't send a packet to %s: %w", dst, err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-16 17:36:34 +03:00
|
|
|
for {
|
2021-03-11 21:30:52 +03:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn4(req, c, iface)
|
2020-11-09 19:27:04 +03:00
|
|
|
if next {
|
2020-11-13 14:40:22 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 21:30:52 +03:00
|
|
|
|
2019-04-16 17:36:34 +03:00
|
|
|
if err != nil {
|
2020-11-13 14:40:22 +03:00
|
|
|
return false, err
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
2020-11-25 14:26:26 +03:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-09 19:27:04 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
// TODO(a.garipov): Refactor further. Inspect error handling, remove parameter
|
|
|
|
// next, address the TODO, merge with tryConn6, etc.
|
|
|
|
func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, next bool, err error) {
|
2020-11-09 19:27:04 +03:00
|
|
|
// TODO: replicate dhclient's behavior of retrying several times with
|
|
|
|
// progressively longer timeouts.
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Tracef("dhcpv4: waiting %v for an answer", defaultDiscoverTime)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
b := make([]byte, 1500)
|
2020-11-13 14:40:22 +03:00
|
|
|
err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
|
|
|
|
if err != nil {
|
|
|
|
return false, false, fmt.Errorf("setting deadline: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
if isTimeout(err) {
|
|
|
|
log.Debug("dhcpv4: didn't receive dhcp response")
|
|
|
|
|
|
|
|
return false, false, nil
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Tracef("dhcpv4: received packet, %d bytes", n)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: encoding: %s", err)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, true, err
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-11-09 19:27:04 +03:00
|
|
|
|
|
|
|
log.Debug("dhcpv4: received message from server: %s", response.Summary())
|
|
|
|
|
|
|
|
if !(response.OpCode == dhcpv4.OpcodeBootReply &&
|
|
|
|
response.HWType == iana.HWTypeEthernet &&
|
|
|
|
bytes.Equal(response.ClientHWAddr, iface.HardwareAddr) &&
|
|
|
|
bytes.Equal(response.TransactionID[:], req.TransactionID[:]) &&
|
|
|
|
response.Options.Has(dhcpv4.OptionDHCPMessageType)) {
|
|
|
|
|
|
|
|
log.Debug("dhcpv4: received message from server doesn't match our request")
|
|
|
|
|
|
|
|
return false, true, nil
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Tracef("dhcpv4: the packet is from an active dhcp server")
|
2020-11-09 19:27:04 +03:00
|
|
|
|
|
|
|
return true, false, nil
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
|
|
|
// CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified network interface,
|
|
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
2021-03-11 21:30:52 +03:00
|
|
|
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (ok bool, err error) {
|
2020-08-13 18:45:27 +03:00
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: net.InterfaceByName: %s: %w", ifaceName, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:39:43 +03:00
|
|
|
ifaceIPNet, err := ifaceIPAddrs(iface, ipVersion6)
|
2020-11-09 19:27:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("getting ipv6 addrs for iface %s: %w", ifaceName, err)
|
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
if len(ifaceIPNet) == 0 {
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, fmt.Errorf("interface %s has no ipv6 addresses", ifaceName)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
srcIP := ifaceIPNet[0]
|
2021-06-01 21:06:55 +03:00
|
|
|
src := aghnet.JoinHostPort(srcIP.String(), 546)
|
2020-08-13 18:45:27 +03:00
|
|
|
dst := "[ff02::1:2]:547"
|
|
|
|
|
|
|
|
req, err := dhcpv6.NewSolicit(iface.HardwareAddr)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: dhcpv6.NewSolicit: %w", err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp6", src)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", src, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !udpAddr.IP.To16().Equal(srcIP) {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Resolved UDP address is not %s: %w", src, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dstAddr, err := net.ResolveUDPAddr("udp6", dst)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", dst, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("DHCPv6: Listening to udp6 %+v", udpAddr)
|
|
|
|
c, err := nclient6.NewIPv6UDPConn(ifaceName, dhcpv6.DefaultClientPort)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't listen on :546: %w", err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
if c != nil {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: %w", dst, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2021-03-11 21:30:52 +03:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn6(req, c)
|
2020-11-16 19:01:12 +03:00
|
|
|
if next {
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv6: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-03-11 21:30:52 +03:00
|
|
|
|
2020-08-13 18:45:27 +03:00
|
|
|
if err != nil {
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, err
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2020-11-25 14:26:26 +03:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-16 19:01:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
// TODO(a.garipov): See the comment on tryConn4. Sigh…
|
|
|
|
func tryConn6(req *dhcpv6.Message, c net.PacketConn) (ok, next bool, err error) {
|
|
|
|
// TODO: replicate dhclient's behavior of retrying several times with
|
|
|
|
// progressively longer timeouts.
|
|
|
|
log.Tracef("dhcpv6: waiting %v for an answer", defaultDiscoverTime)
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
b := make([]byte, 4096)
|
|
|
|
err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
|
|
|
|
if err != nil {
|
|
|
|
return false, false, fmt.Errorf("setting deadline: %w", err)
|
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
if isTimeout(err) {
|
|
|
|
log.Debug("dhcpv6: didn't receive dhcp response")
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, false, nil
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2020-11-16 19:01:12 +03:00
|
|
|
|
|
|
|
log.Tracef("dhcpv6: received packet, %d bytes", n)
|
|
|
|
|
|
|
|
response, err := dhcpv6.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv6: encoding: %s", err)
|
|
|
|
|
|
|
|
return false, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("dhcpv6: received message from server: %s", response.Summary())
|
|
|
|
|
|
|
|
cid := req.Options.ClientID()
|
|
|
|
msg, err := response.GetInnerMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv6: resp.GetInnerMessage(): %s", err)
|
|
|
|
|
|
|
|
return false, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rcid := msg.Options.ClientID()
|
|
|
|
if !(response.Type() == dhcpv6.MessageTypeAdvertise &&
|
|
|
|
msg.TransactionID == req.TransactionID &&
|
|
|
|
rcid != nil &&
|
|
|
|
cid.Equal(*rcid)) {
|
|
|
|
|
|
|
|
log.Debug("dhcpv6: received message from server doesn't match our request")
|
|
|
|
|
|
|
|
return false, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("dhcpv6: the packet is from an active dhcp server")
|
|
|
|
|
|
|
|
return true, false, nil
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|