mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 14:35:48 +03:00
bf4c256c72
Merge in DNS/adguard-home from 2295-dhcp-windows to master Updates #2295. Squashed commit of the following: commit 3b00a90c3d9bc33e9af478e4062c0f938d4f327d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Nov 16 16:45:43 2020 +0300 all: use the 501 handlers instead of the real ones, revert other changes commit 0a3b37736a21abd6181e0d28c32069e8d7a576d0 Merge: 45feba7556358240e9
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Nov 16 15:59:15 2020 +0300 Merge branch 'master' into 2295-dhcp-windows and update commit 45feba755dde37e43cc8075b896e1576157341e6 Merge: cd987d8bca19523b25
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Nov 16 15:51:16 2020 +0300 Merge branch 'master' into 2295-dhcp-windows commit cd987d8bc2cd524b7454d9037b595069714645f9 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 13 15:55:23 2020 +0300 all: improve tests and refactor dhcp checking code even more commit 3aad675443f325b5909523bcc1c987aa04ac61d9 Merge: 70c477e6109196118e
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 13 14:44:43 2020 +0300 Merge branch 'master' into 2295-dhcp-windows commit 70c477e61cdc1237603918f1c44470c1549f1136 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 13 14:34:06 2020 +0300 home: fix dhcpd test on windows commit e59597d783fb9304e63f94eee2b5a5d67a5b2169 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 13 13:38:25 2020 +0300 all: mention the feature in the changelog commit 5555c8d881b1c20b5b0a0cb096a17cf56e209c06 Merge: c3b6a5a93e802e6645
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 13 13:35:35 2020 +0300 Merge branch 'master' into 2295-dhcp-windows commit c3b6a5a930693090838eb1ef9f75a09b5b223ba6 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Nov 12 20:37:09 2020 +0300 util: fix comment commit ed92dfdb5d3a6c4ba5d032cbe781e7fd87882813 Author: ArtemBaskal <asbaskal@miem.hse.ru> Date: Thu Nov 12 20:24:14 2020 +0300 Adapt client commit e6f0494c20a4ad5388492af9091568eea5c6e2d6 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Nov 12 13:35:25 2020 +0300 return 501 when we don't support features
284 lines
7.6 KiB
Go
284 lines
7.6 KiB
Go
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd/nclient4"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
"github.com/insomniacslk/dhcp/dhcpv6/nclient6"
|
|
"github.com/insomniacslk/dhcp/iana"
|
|
)
|
|
|
|
// CheckIfOtherDHCPServersPresentV4 sends a DHCP request to the specified network interface,
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
|
func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error) {
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
if err != nil {
|
|
return false, fmt.Errorf("couldn't find interface by name %s: %w", ifaceName, err)
|
|
}
|
|
|
|
ifaceIPNet, err := ifaceIPv4Addrs(iface)
|
|
if err != nil {
|
|
return false, fmt.Errorf("getting ipv4 addrs for iface %s: %w", ifaceName, err)
|
|
}
|
|
if len(ifaceIPNet) == 0 {
|
|
return false, fmt.Errorf("interface %s has no ipv4 addresses", ifaceName)
|
|
}
|
|
|
|
// TODO(a.garipov): Find out what this is about. Perhaps this
|
|
// information is outdated or at least incomplete.
|
|
if runtime.GOOS == "darwin" {
|
|
return false, fmt.Errorf("can't find DHCP server: not supported on macOS")
|
|
}
|
|
|
|
srcIP := ifaceIPNet[0]
|
|
src := net.JoinHostPort(srcIP.String(), "68")
|
|
dst := "255.255.255.255:67"
|
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
req, err := dhcpv4.NewDiscovery(iface.HardwareAddr)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv4.NewDiscovery: %w", err)
|
|
}
|
|
req.Options.Update(dhcpv4.OptClientIdentifier(iface.HardwareAddr))
|
|
req.Options.Update(dhcpv4.OptHostName(hostname))
|
|
|
|
// resolve 0.0.0.0:68
|
|
udpAddr, err := net.ResolveUDPAddr("udp4", src)
|
|
if err != nil {
|
|
return false, fmt.Errorf("couldn't resolve UDP address %s: %w", src, err)
|
|
}
|
|
|
|
if !udpAddr.IP.To4().Equal(srcIP) {
|
|
return false, fmt.Errorf("resolved UDP address is not %s: %w", src, err)
|
|
}
|
|
|
|
// resolve 255.255.255.255:67
|
|
dstAddr, err := net.ResolveUDPAddr("udp4", dst)
|
|
if err != nil {
|
|
return false, fmt.Errorf("couldn't resolve UDP address %s: %w", dst, err)
|
|
}
|
|
|
|
// bind to 0.0.0.0:68
|
|
log.Tracef("Listening to udp4 %+v", udpAddr)
|
|
c, err := nclient4.NewRawUDPConn(ifaceName, 68)
|
|
if err != nil {
|
|
return false, fmt.Errorf("couldn't listen on :68: %w", err)
|
|
}
|
|
if c != nil {
|
|
defer c.Close()
|
|
}
|
|
|
|
// send to 255.255.255.255:67
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
|
if err != nil {
|
|
return false, fmt.Errorf("couldn't send a packet to %s: %w", dst, err)
|
|
}
|
|
|
|
for {
|
|
ok, next, err := tryConn4(req, c, iface)
|
|
if next {
|
|
if err != nil {
|
|
log.Debug("dhcpv4: trying a connection: %s", err)
|
|
}
|
|
|
|
continue
|
|
}
|
|
if ok {
|
|
return true, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
// TODO: replicate dhclient's behavior of retrying several times with
|
|
// progressively longer timeouts.
|
|
log.Tracef("dhcpv4: waiting %v for an answer", defaultDiscoverTime)
|
|
|
|
b := make([]byte, 1500)
|
|
err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
|
|
if err != nil {
|
|
return false, false, fmt.Errorf("setting deadline: %w", err)
|
|
}
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
if err != nil {
|
|
if isTimeout(err) {
|
|
log.Debug("dhcpv4: didn't receive dhcp response")
|
|
|
|
return false, false, nil
|
|
}
|
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
|
}
|
|
|
|
log.Tracef("dhcpv4: received packet, %d bytes", n)
|
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
if err != nil {
|
|
log.Debug("dhcpv4: encoding: %s", err)
|
|
|
|
return false, true, err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
log.Tracef("dhcpv4: the packet is from an active dhcp server")
|
|
|
|
return true, false, nil
|
|
}
|
|
|
|
// CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified network interface,
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
|
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: net.InterfaceByName: %s: %w", ifaceName, err)
|
|
}
|
|
|
|
ifaceIPNet, err := ifaceIPv6Addrs(iface)
|
|
if err != nil {
|
|
return false, fmt.Errorf("getting ipv6 addrs for iface %s: %w", ifaceName, err)
|
|
}
|
|
if len(ifaceIPNet) == 0 {
|
|
return false, fmt.Errorf("interface %s has no ipv6 addresses", ifaceName)
|
|
}
|
|
|
|
srcIP := ifaceIPNet[0]
|
|
src := net.JoinHostPort(srcIP.String(), "546")
|
|
dst := "[ff02::1:2]:547"
|
|
|
|
req, err := dhcpv6.NewSolicit(iface.HardwareAddr)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: dhcpv6.NewSolicit: %w", err)
|
|
}
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp6", src)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", src, err)
|
|
}
|
|
|
|
if !udpAddr.IP.To16().Equal(srcIP) {
|
|
return false, fmt.Errorf("dhcpv6: Resolved UDP address is not %s: %w", src, err)
|
|
}
|
|
|
|
dstAddr, err := net.ResolveUDPAddr("udp6", dst)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", dst, err)
|
|
}
|
|
|
|
log.Debug("DHCPv6: Listening to udp6 %+v", udpAddr)
|
|
c, err := nclient6.NewIPv6UDPConn(ifaceName, dhcpv6.DefaultClientPort)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: Couldn't listen on :546: %w", err)
|
|
}
|
|
if c != nil {
|
|
defer c.Close()
|
|
}
|
|
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
|
if err != nil {
|
|
return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: %w", dst, err)
|
|
}
|
|
|
|
for {
|
|
ok, next, err := tryConn6(req, c)
|
|
if next {
|
|
if err != nil {
|
|
log.Debug("dhcpv6: trying a connection: %s", err)
|
|
}
|
|
|
|
continue
|
|
}
|
|
if ok {
|
|
return true, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
|
|
b := make([]byte, 4096)
|
|
err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
|
|
if err != nil {
|
|
return false, false, fmt.Errorf("setting deadline: %w", err)
|
|
}
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
if err != nil {
|
|
if isTimeout(err) {
|
|
log.Debug("dhcpv6: didn't receive dhcp response")
|
|
|
|
return false, false, nil
|
|
}
|
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
|
}
|
|
|
|
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
|
|
}
|