2020-07-03 18:20:01 +03:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
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"
|
|
|
|
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd/nclient4"
|
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"
|
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
|
2020-08-13 18:45:27 +03:00
|
|
|
func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, 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
|
|
|
}
|
|
|
|
|
|
|
|
// get ipv4 address of an interface
|
2020-07-03 18:20:01 +03:00
|
|
|
ifaceIPNet := getIfaceIPv4(*iface)
|
|
|
|
if len(ifaceIPNet) == 0 {
|
|
|
|
return false, fmt.Errorf("couldn't find IPv4 address of interface %s %+v", ifaceName, iface)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2020-08-12 18:13:07 +03:00
|
|
|
if runtime.GOOS == "darwin" {
|
2020-08-20 15:41:25 +03:00
|
|
|
return false, fmt.Errorf("can't find DHCP server: not supported on macOS")
|
2020-08-12 18:13:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
srcIP := ifaceIPNet[0]
|
2018-12-28 17:17:51 +03:00
|
|
|
src := net.JoinHostPort(srcIP.String(), "68")
|
|
|
|
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 {
|
|
|
|
defer c.Close()
|
|
|
|
}
|
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 {
|
|
|
|
// wait for answer
|
|
|
|
log.Tracef("Waiting %v for an answer", defaultDiscoverTime)
|
|
|
|
// TODO: replicate dhclient's behaviour of retrying several times with progressively bigger timeouts
|
|
|
|
b := make([]byte, 1500)
|
|
|
|
_ = c.SetReadDeadline(time.Now().Add(defaultDiscoverTime))
|
2020-08-12 18:13:07 +03:00
|
|
|
n, _, err := c.ReadFrom(b)
|
2019-04-16 17:36:34 +03:00
|
|
|
if isTimeout(err) {
|
|
|
|
// timed out -- no DHCP servers
|
2020-08-12 18:13:07 +03:00
|
|
|
log.Debug("DHCPv4: didn't receive DHCP response")
|
2019-04-16 17:36:34 +03:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't receive packet: %w", err)
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("Received packet (%v bytes)", n)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("DHCPv4: dhcpv4.FromBytes: %s", err)
|
2019-04-16 17:36:34 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
log.Debug("DHCPv4: received message from server: %s", response.Summary())
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
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")
|
|
|
|
continue
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("The packet is from an active DHCP server")
|
|
|
|
// that's a DHCP server there
|
|
|
|
return true, 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
|
|
|
|
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ifaceIPNet := getIfaceIPv6(*iface)
|
|
|
|
if len(ifaceIPNet) == 0 {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: couldn't find IPv6 address of interface %s %+v", ifaceName, iface)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
srcIP := ifaceIPNet[0]
|
|
|
|
src := net.JoinHostPort(srcIP.String(), "546")
|
|
|
|
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 {
|
|
|
|
defer c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
log.Debug("DHCPv6: Waiting %v for an answer", defaultDiscoverTime)
|
|
|
|
b := make([]byte, 4096)
|
|
|
|
_ = c.SetReadDeadline(time.Now().Add(defaultDiscoverTime))
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if isTimeout(err) {
|
|
|
|
log.Debug("DHCPv6: didn't receive DHCP response")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return false, fmt.Errorf("couldn't receive packet: %w", err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("DHCPv6: Received packet (%v bytes)", n)
|
|
|
|
|
|
|
|
resp, err := dhcpv6.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("DHCPv6: dhcpv6.FromBytes: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("DHCPv6: received message from server: %s", resp.Summary())
|
|
|
|
|
|
|
|
cid := req.Options.ClientID()
|
|
|
|
msg, err := resp.GetInnerMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("DHCPv6: resp.GetInnerMessage: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rcid := msg.Options.ClientID()
|
|
|
|
if resp.Type() == dhcpv6.MessageTypeAdvertise &&
|
|
|
|
msg.TransactionID == req.TransactionID &&
|
|
|
|
rcid != nil &&
|
|
|
|
cid.Equal(*rcid) {
|
|
|
|
log.Debug("DHCPv6: The packet is from an active DHCP server")
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("DHCPv6: received message from server doesn't match our request")
|
|
|
|
}
|
|
|
|
}
|