2018-12-28 14:17:51 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2019-04-16 15:36:34 +01:00
|
|
|
"bytes"
|
2018-12-28 14:17:51 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-28 14:17:51 +00:00
|
|
|
"github.com/krolaw/dhcp4"
|
2019-04-16 15:36:34 +01: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
|
|
|
|
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 15:36:34 +01:00
|
|
|
xID := make([]byte, 4)
|
2019-01-24 17:11:01 +00:00
|
|
|
n, err := rand.Read(xID)
|
2019-04-16 15:36:34 +01:00
|
|
|
if n != 4 && err == nil {
|
|
|
|
err = fmt.Errorf("Generated less than 4 bytes")
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-16 15:36:34 +01:00
|
|
|
return false, wrapErrPrint(err, "Couldn't generate random bytes")
|
2018-12-28 14:17:51 +00: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 13:01:27 +00:00
|
|
|
leaseTime := uint32(math.RoundToEven((time.Hour * 24 * 90).Seconds()))
|
2018-12-28 14:17:51 +00:00
|
|
|
binary.BigEndian.PutUint32(leaseTimeRaw, leaseTime)
|
|
|
|
options := []dhcp4.Option{
|
2019-01-24 17:11:01 +00: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 14:17:51 +00:00
|
|
|
}
|
2019-01-24 17:11:01 +00:00
|
|
|
packet := dhcp4.RequestPacket(dhcp4.Discover, iface.HardwareAddr, nil, xID, false, options)
|
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)
|
|
|
|
}
|
|
|
|
// 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 14:23:42 +00:00
|
|
|
log.Tracef("Listening to udp4 %+v", udpAddr)
|
2019-04-16 15:36:34 +01: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()
|
|
|
|
}
|
|
|
|
// spew.Dump(c, err)
|
|
|
|
// spew.Printf("net.ListenUDP returned %v, %v\n", c, err)
|
|
|
|
if err != nil {
|
2019-04-16 15:36:34 +01: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 15:36:34 +01:00
|
|
|
cm := ipv4.ControlMessage{}
|
|
|
|
_, err = c.WriteTo(packet, &cm, dstAddr)
|
2018-12-28 14:17:51 +00:00
|
|
|
// spew.Dump(n, err)
|
|
|
|
if err != nil {
|
|
|
|
return false, wrapErrPrint(err, "Couldn't send a packet to %s", dst)
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:36:34 +01: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 14:17:51 +00:00
|
|
|
}
|
|
|
|
}
|