2021-05-21 11:55:42 +00:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 16:42:41 +00:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
package aghnet
|
2018-12-28 14:17:51 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-16 14:36:34 +00:00
|
|
|
"bytes"
|
2018-12-28 14:17:51 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2021-05-24 14:28:11 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 13:03:37 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-07-03 15:20:01 +00:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
2020-08-13 15:45:27 +00:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6/nclient6"
|
2020-07-03 15:20:01 +00:00
|
|
|
"github.com/insomniacslk/dhcp/iana"
|
2018-12-28 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2022-01-21 16:21:38 +00:00
|
|
|
// defaultDiscoverTime is the default timeout of checking another DHCP server
|
|
|
|
// response.
|
2021-08-12 14:33:53 +00:00
|
|
|
const defaultDiscoverTime = 3 * time.Second
|
|
|
|
|
|
|
|
func checkOtherDHCP(ifaceName string) (ok4, ok6 bool, err4, err6 error) {
|
2018-12-28 14:17:51 +00:00
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
2021-08-12 14:33:53 +00:00
|
|
|
err = fmt.Errorf("couldn't find interface by name %s: %w", ifaceName, err)
|
|
|
|
err4, err6 = err, err
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
return false, false, err4, err6
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
ok4, err4 = checkOtherDHCPv4(iface)
|
|
|
|
ok6, err6 = checkOtherDHCPv6(iface)
|
|
|
|
|
|
|
|
return ok4, ok6, err4, err6
|
|
|
|
}
|
|
|
|
|
|
|
|
// ifaceIPv4Subnet returns the first suitable IPv4 subnetwork iface has.
|
|
|
|
func ifaceIPv4Subnet(iface *net.Interface) (subnet *net.IPNet, err error) {
|
|
|
|
var addrs []net.Addr
|
|
|
|
if addrs, err = iface.Addrs(); err != nil {
|
|
|
|
return nil, err
|
2020-08-12 15:13:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
for _, a := range addrs {
|
|
|
|
switch a := a.(type) {
|
|
|
|
case *net.IPAddr:
|
|
|
|
subnet = &net.IPNet{
|
|
|
|
IP: a.IP,
|
|
|
|
Mask: a.IP.DefaultMask(),
|
|
|
|
}
|
|
|
|
case *net.IPNet:
|
|
|
|
subnet = a
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
if ip4 := subnet.IP.To4(); ip4 != nil {
|
|
|
|
subnet.IP = ip4
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
return subnet, nil
|
|
|
|
}
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
return nil, fmt.Errorf("interface %s has no ipv4 addresses", iface.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkOtherDHCPv4 sends a DHCP request to the specified network interface, and
|
|
|
|
// waits for a response for a period defined by defaultDiscoverTime.
|
|
|
|
func checkOtherDHCPv4(iface *net.Interface) (ok bool, err error) {
|
|
|
|
var subnet *net.IPNet
|
|
|
|
if subnet, err = ifaceIPv4Subnet(iface); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve broadcast addr.
|
|
|
|
dst := netutil.IPPort{
|
|
|
|
IP: BroadcastFromIPNet(subnet),
|
|
|
|
Port: 67,
|
|
|
|
}.String()
|
|
|
|
var dstAddr *net.UDPAddr
|
|
|
|
if dstAddr, err = net.ResolveUDPAddr("udp4", dst); err != nil {
|
|
|
|
return false, fmt.Errorf("couldn't resolve UDP address %s: %w", dst, err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
var hostname string
|
|
|
|
if hostname, err = os.Hostname(); err != nil {
|
|
|
|
return false, fmt.Errorf("couldn't get hostname: %w", err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
return discover4(iface, dstAddr, hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func discover4(iface *net.Interface, dstAddr *net.UDPAddr, hostname string) (ok bool, err error) {
|
|
|
|
var req *dhcpv4.DHCPv4
|
|
|
|
if req, err = dhcpv4.NewDiscovery(iface.HardwareAddr); err != nil {
|
|
|
|
return false, fmt.Errorf("dhcpv4.NewDiscovery: %w", err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
req.Options.Update(dhcpv4.OptClientIdentifier(iface.HardwareAddr))
|
|
|
|
req.Options.Update(dhcpv4.OptHostName(hostname))
|
|
|
|
req.SetBroadcast()
|
|
|
|
|
|
|
|
// Bind to 0.0.0.0:68.
|
|
|
|
//
|
|
|
|
// On OpenBSD binding to the port 68 competes with dhclient's binding,
|
|
|
|
// so that all incoming packets are ignored and the discovering process
|
|
|
|
// is spoiled.
|
|
|
|
//
|
|
|
|
// It's also known that listening on the specified interface's address
|
2022-01-25 16:47:02 +00:00
|
|
|
// ignores broadcast packets when reading.
|
2021-08-12 14:33:53 +00:00
|
|
|
var c net.PacketConn
|
2021-08-16 12:53:33 +00:00
|
|
|
if c, err = listenPacketReusable(iface.Name, "udp4", ":68"); err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return false, fmt.Errorf("couldn't listen on :68: %w", err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2021-08-12 14:33:53 +00:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2018-12-28 14:17:51 +00:00
|
|
|
|
2021-08-16 12:53:33 +00:00
|
|
|
// Send to broadcast.
|
2021-08-12 14:33:53 +00:00
|
|
|
if _, err = c.WriteTo(req.ToBytes(), dstAddr); err != nil {
|
|
|
|
return false, fmt.Errorf("couldn't send a packet to %s: %w", dstAddr, err)
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 14:36:34 +00:00
|
|
|
for {
|
2021-08-12 14:33:53 +00:00
|
|
|
if err = c.SetDeadline(time.Now().Add(defaultDiscoverTime)); err != nil {
|
|
|
|
return false, fmt.Errorf("setting deadline: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:30:52 +00:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn4(req, c, iface)
|
2020-11-09 16:27:04 +00:00
|
|
|
if next {
|
2020-11-13 11:40:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 18:30:52 +00:00
|
|
|
|
2019-04-16 14:36:34 +00:00
|
|
|
if err != nil {
|
2020-11-13 11:40:22 +00:00
|
|
|
return false, err
|
2019-04-16 14:36:34 +00:00
|
|
|
}
|
2020-11-25 11:26:26 +00:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-09 16:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 14:36:34 +00:00
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
// 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) {
|
2020-11-09 16:27:04 +00:00
|
|
|
// TODO: replicate dhclient's behavior of retrying several times with
|
|
|
|
// progressively longer timeouts.
|
2020-11-16 16:01:12 +00:00
|
|
|
log.Tracef("dhcpv4: waiting %v for an answer", defaultDiscoverTime)
|
2019-04-16 14:36:34 +00:00
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
b := make([]byte, 1500)
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
if isTimeout(err) {
|
|
|
|
log.Debug("dhcpv4: didn't receive dhcp response")
|
|
|
|
|
|
|
|
return false, false, nil
|
2019-04-16 14:36:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
log.Tracef("dhcpv4: received packet, %d bytes", n)
|
2019-04-16 14:36:34 +00:00
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: encoding: %s", err)
|
2019-04-16 14:36:34 +00:00
|
|
|
|
2020-11-09 16:27:04 +00:00
|
|
|
return false, true, err
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2020-11-09 16:27:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
log.Tracef("dhcpv4: the packet is from an active dhcp server")
|
2020-11-09 16:27:04 +00:00
|
|
|
|
|
|
|
return true, false, nil
|
2018-12-28 14:17:51 +00:00
|
|
|
}
|
2020-08-13 15:45:27 +00:00
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
// checkOtherDHCPv6 sends a DHCP request to the specified network interface, and
|
|
|
|
// waits for a response for a period defined by defaultDiscoverTime.
|
|
|
|
func checkOtherDHCPv6(iface *net.Interface) (ok bool, err error) {
|
|
|
|
ifaceIPNet, err := IfaceIPAddrs(iface, IPVersion6)
|
2020-08-13 15:45:27 +00:00
|
|
|
if err != nil {
|
2021-08-12 14:33:53 +00:00
|
|
|
return false, fmt.Errorf("getting ipv6 addrs for iface %s: %w", iface.Name, err)
|
2020-11-09 16:27:04 +00:00
|
|
|
}
|
2020-08-13 15:45:27 +00:00
|
|
|
if len(ifaceIPNet) == 0 {
|
2021-08-12 14:33:53 +00:00
|
|
|
return false, fmt.Errorf("interface %s has no ipv6 addresses", iface.Name)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
srcIP := ifaceIPNet[0]
|
2021-08-09 13:03:37 +00:00
|
|
|
src := netutil.JoinHostPort(srcIP.String(), 546)
|
2020-08-13 15:45:27 +00:00
|
|
|
dst := "[ff02::1:2]:547"
|
|
|
|
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp6", src)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", src, err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !udpAddr.IP.To16().Equal(srcIP) {
|
2020-11-05 12:20:57 +00:00
|
|
|
return false, fmt.Errorf("dhcpv6: Resolved UDP address is not %s: %w", src, err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dstAddr, err := net.ResolveUDPAddr("udp6", dst)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't resolve UDP address %s: %w", dst, err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
return discover6(iface, udpAddr, dstAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func discover6(iface *net.Interface, udpAddr, dstAddr *net.UDPAddr) (ok bool, err error) {
|
|
|
|
req, err := dhcpv6.NewSolicit(iface.HardwareAddr)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("dhcpv6: dhcpv6.NewSolicit: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-13 15:45:27 +00:00
|
|
|
log.Debug("DHCPv6: Listening to udp6 %+v", udpAddr)
|
2021-08-12 14:33:53 +00:00
|
|
|
c, err := nclient6.NewIPv6UDPConn(iface.Name, dhcpv6.DefaultClientPort)
|
2020-08-13 15:45:27 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't listen on :546: %w", err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
2021-08-12 14:33:53 +00:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2020-08-13 15:45:27 +00:00
|
|
|
|
|
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
|
|
|
if err != nil {
|
2021-08-12 14:33:53 +00:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: %w", dstAddr, err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2021-03-11 18:30:52 +00:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn6(req, c)
|
2020-11-16 16:01:12 +00:00
|
|
|
if next {
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv6: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-03-11 18:30:52 +00:00
|
|
|
|
2020-08-13 15:45:27 +00:00
|
|
|
if err != nil {
|
2020-11-16 16:01:12 +00:00
|
|
|
return false, err
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
2020-11-25 11:26:26 +00:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-16 16:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 15:45:27 +00:00
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
// 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)
|
2020-08-13 15:45:27 +00:00
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
b := make([]byte, 4096)
|
|
|
|
err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
|
|
|
|
if err != nil {
|
|
|
|
return false, false, fmt.Errorf("setting deadline: %w", err)
|
|
|
|
}
|
2020-08-13 15:45:27 +00:00
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
if isTimeout(err) {
|
|
|
|
log.Debug("dhcpv6: didn't receive dhcp response")
|
2020-08-13 15:45:27 +00:00
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
return false, false, nil
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:01:12 +00:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
2020-11-16 16:01:12 +00:00
|
|
|
|
|
|
|
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
|
2020-08-13 15:45:27 +00:00
|
|
|
}
|
2021-08-12 14:33:53 +00:00
|
|
|
|
|
|
|
// isTimeout returns true if err is an operation timeout error from net package.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Consider moving into netutil.
|
|
|
|
func isTimeout(err error) (ok bool) {
|
|
|
|
var operr *net.OpError
|
|
|
|
if errors.As(err, &operr) {
|
|
|
|
return operr.Timeout()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|