2018-12-28 07:17:51 -07:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2019-04-16 07:36:34 -07:00
|
|
|
"bytes"
|
2018-12-28 07:17:51 -07:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-02-25 06:44:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-28 07:17:51 -07:00
|
|
|
"github.com/krolaw/dhcp4"
|
2019-04-16 07:36:34 -07:00
|
|
|
"golang.org/x/net/ipv4"
|
2018-12-28 07:17:51 -07:00
|
|
|
)
|
|
|
|
|
2019-01-24 10:11:01 -07:00
|
|
|
// CheckIfOtherDHCPServersPresent sends a DHCP request to the specified network interface,
|
|
|
|
// and waits for a response for a period defined by defaultDiscoverTime
|
2019-08-19 14:55:32 -07:00
|
|
|
// nolint
|
2018-12-28 07:17:51 -07: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
|
|
|
|
ifaceIPNet := getIfaceIPv4(iface)
|
|
|
|
if ifaceIPNet == nil {
|
|
|
|
return false, fmt.Errorf("Couldn't find IPv4 address of interface %s %+v", ifaceName, iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
srcIP := ifaceIPNet.IP
|
|
|
|
src := net.JoinHostPort(srcIP.String(), "68")
|
|
|
|
dst := "255.255.255.255:67"
|
|
|
|
|
|
|
|
// form a DHCP request packet, try to emulate existing client as much as possible
|
2019-04-16 07:36:34 -07:00
|
|
|
xID := make([]byte, 4)
|
2019-01-24 10:11:01 -07:00
|
|
|
n, err := rand.Read(xID)
|
2019-04-16 07:36:34 -07:00
|
|
|
if n != 4 && err == nil {
|
|
|
|
err = fmt.Errorf("Generated less than 4 bytes")
|
2018-12-28 07:17:51 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-16 07:36:34 -07:00
|
|
|
return false, wrapErrPrint(err, "Couldn't generate random bytes")
|
2018-12-28 07:17:51 -07:00
|
|
|
}
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't get hostname")
|
|
|
|
}
|
|
|
|
requestList := []byte{
|
|
|
|
byte(dhcp4.OptionSubnetMask),
|
|
|
|
byte(dhcp4.OptionClasslessRouteFormat),
|
|
|
|
byte(dhcp4.OptionRouter),
|
|
|
|
byte(dhcp4.OptionDomainNameServer),
|
|
|
|
byte(dhcp4.OptionDomainName),
|
|
|
|
byte(dhcp4.OptionDomainSearch),
|
|
|
|
252, // private/proxy autodiscovery
|
|
|
|
95, // LDAP
|
|
|
|
byte(dhcp4.OptionNetBIOSOverTCPIPNameServer),
|
|
|
|
byte(dhcp4.OptionNetBIOSOverTCPIPNodeType),
|
|
|
|
}
|
|
|
|
maxUDPsizeRaw := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(maxUDPsizeRaw, 1500)
|
|
|
|
leaseTimeRaw := make([]byte, 4)
|
2019-01-25 06:01:27 -07:00
|
|
|
leaseTime := uint32(math.RoundToEven((time.Hour * 24 * 90).Seconds()))
|
2018-12-28 07:17:51 -07:00
|
|
|
binary.BigEndian.PutUint32(leaseTimeRaw, leaseTime)
|
|
|
|
options := []dhcp4.Option{
|
2019-01-24 10:11:01 -07:00
|
|
|
{Code: dhcp4.OptionParameterRequestList, Value: requestList},
|
|
|
|
{Code: dhcp4.OptionMaximumDHCPMessageSize, Value: maxUDPsizeRaw},
|
|
|
|
{Code: dhcp4.OptionClientIdentifier, Value: append([]byte{0x01}, iface.HardwareAddr...)},
|
|
|
|
{Code: dhcp4.OptionIPAddressLeaseTime, Value: leaseTimeRaw},
|
|
|
|
{Code: dhcp4.OptionHostName, Value: []byte(hostname)},
|
2018-12-28 07:17:51 -07:00
|
|
|
}
|
2019-01-24 10:11:01 -07:00
|
|
|
packet := dhcp4.RequestPacket(dhcp4.Discover, iface.HardwareAddr, nil, xID, false, options)
|
2018-12-28 07:17:51 -07: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)
|
|
|
|
}
|
|
|
|
// spew.Dump(udpAddr, err)
|
|
|
|
|
|
|
|
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 07:23:42 -07:00
|
|
|
log.Tracef("Listening to udp4 %+v", udpAddr)
|
2019-04-16 07:36:34 -07:00
|
|
|
c, err := newBroadcastPacketConn(net.IPv4(0, 0, 0, 0), 68, ifaceName)
|
2018-12-28 07:17:51 -07:00
|
|
|
if c != nil {
|
|
|
|
defer c.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-16 07:36:34 -07:00
|
|
|
return false, wrapErrPrint(err, "Couldn't listen on :68")
|
2018-12-28 07:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// send to 255.255.255.255:67
|
2019-04-16 07:36:34 -07:00
|
|
|
cm := ipv4.ControlMessage{}
|
|
|
|
_, err = c.WriteTo(packet, &cm, dstAddr)
|
2018-12-28 07:17:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't send a packet to %s", dst)
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:36:34 -07: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))
|
|
|
|
n, _, _, err = c.ReadFrom(b)
|
|
|
|
if isTimeout(err) {
|
|
|
|
// timed out -- no DHCP servers
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't receive packet")
|
|
|
|
}
|
|
|
|
// spew.Dump(n, fromAddr, err, b)
|
|
|
|
|
|
|
|
log.Tracef("Received packet (%v bytes)", n)
|
|
|
|
|
|
|
|
if n < 240 {
|
|
|
|
// packet too small for dhcp
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
response := dhcp4.Packet(b[:n])
|
|
|
|
if response.OpCode() != dhcp4.BootReply ||
|
|
|
|
response.HType() != 1 /*Ethernet*/ ||
|
|
|
|
response.HLen() > 16 ||
|
|
|
|
!bytes.Equal(response.CHAddr(), iface.HardwareAddr) ||
|
|
|
|
!bytes.Equal(response.XId(), xID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedOptions := response.ParseOptions()
|
|
|
|
if t := parsedOptions[dhcp4.OptionDHCPMessageType]; len(t) != 1 {
|
|
|
|
continue //packet without DHCP message type
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("The packet is from an active DHCP server")
|
|
|
|
// that's a DHCP server there
|
|
|
|
return true, nil
|
2018-12-28 07:17:51 -07:00
|
|
|
}
|
|
|
|
}
|