2020-07-03 15:20:01 +00:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
2018-12-28 14:17:51 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2019-04-16 14:36:34 +00:00
|
|
|
"bytes"
|
2018-12-28 14:17:51 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-07-03 15:20:01 +00:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/insomniacslk/dhcp/iana"
|
2019-04-16 14:36:34 +00:00
|
|
|
"golang.org/x/net/ipv4"
|
2018-12-28 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// CheckIfOtherDHCPServersPresent sends a DHCP request to the specified network interface,
|
|
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
2018-12-28 14:17:51 +00:00
|
|
|
func CheckIfOtherDHCPServersPresent(ifaceName string) (bool, error) {
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't find interface by name %s", ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ipv4 address of an interface
|
2020-07-03 15:20:01 +00: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 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
srcIP := ifaceIPNet[0]
|
2018-12-28 14:17:51 +00:00
|
|
|
src := net.JoinHostPort(srcIP.String(), "68")
|
|
|
|
dst := "255.255.255.255:67"
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
|
|
|
|
req, err := dhcpv4.NewDiscovery(iface.HardwareAddr)
|
2018-12-28 14:17:51 +00:00
|
|
|
if err != nil {
|
2020-07-03 15:20:01 +00:00
|
|
|
return false, fmt.Errorf("dhcpv4.NewDiscovery: %s", err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2020-07-03 15:20:01 +00:00
|
|
|
req.Options.Update(dhcpv4.OptClientIdentifier(iface.HardwareAddr))
|
|
|
|
req.Options.Update(dhcpv4.OptHostName(hostname))
|
2018-12-28 14:17:51 +00:00
|
|
|
|
|
|
|
// resolve 0.0.0.0:68
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp4", src)
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't resolve UDP address %s", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !udpAddr.IP.To4().Equal(srcIP) {
|
|
|
|
return false, wrapErrPrint(err, "Resolved UDP address is not %s", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve 255.255.255.255:67
|
|
|
|
dstAddr, err := net.ResolveUDPAddr("udp4", dst)
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't resolve UDP address %s", dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind to 0.0.0.0:68
|
2018-12-29 14:23:42 +00:00
|
|
|
log.Tracef("Listening to udp4 %+v", udpAddr)
|
2019-04-16 14:36:34 +00:00
|
|
|
c, err := newBroadcastPacketConn(net.IPv4(0, 0, 0, 0), 68, ifaceName)
|
2018-12-28 14:17:51 +00:00
|
|
|
if c != nil {
|
|
|
|
defer c.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-16 14:36:34 +00:00
|
|
|
return false, wrapErrPrint(err, "Couldn't listen on :68")
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send to 255.255.255.255:67
|
2019-04-16 14:36:34 +00:00
|
|
|
cm := ipv4.ControlMessage{}
|
2020-07-03 15:20:01 +00:00
|
|
|
_, err = c.WriteTo(req.ToBytes(), &cm, dstAddr)
|
2018-12-28 14:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't send a packet to %s", dst)
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:36:34 +00: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-07-03 15:20:01 +00:00
|
|
|
n, _, _, err := c.ReadFrom(b)
|
2019-04-16 14:36:34 +00:00
|
|
|
if isTimeout(err) {
|
|
|
|
// timed out -- no DHCP servers
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't receive packet")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("Received packet (%v bytes)", n)
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("DHCPv4: dhcpv4.FromBytes: %s", err)
|
2019-04-16 14:36:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
log.Debug("DHCPv4: received message from server: %s", response.Summary())
|
2019-04-16 14:36:34 +00:00
|
|
|
|
2020-07-03 15:20:01 +00: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 14:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("The packet is from an active DHCP server")
|
|
|
|
// that's a DHCP server there
|
|
|
|
return true, nil
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
}
|