2022-09-14 16:36:29 +03:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
package aghnet
|
2018-12-28 17:17:51 +03:00
|
|
|
|
|
|
|
import (
|
2019-04-16 17:36:34 +03:00
|
|
|
"bytes"
|
2018-12-28 17:17:51 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2018-12-28 17:17:51 +03:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
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
|
|
|
)
|
|
|
|
|
2022-01-21 19:21:38 +03:00
|
|
|
// defaultDiscoverTime is the default timeout of checking another DHCP server
|
|
|
|
// response.
|
2021-08-12 17:33:53 +03:00
|
|
|
const defaultDiscoverTime = 3 * time.Second
|
|
|
|
|
|
|
|
func checkOtherDHCP(ifaceName string) (ok4, ok6 bool, err4, err6 error) {
|
2018-12-28 17:17:51 +03:00
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
2021-08-12 17:33:53 +03:00
|
|
|
err = fmt.Errorf("couldn't find interface by name %s: %w", ifaceName, err)
|
|
|
|
err4, err6 = err, err
|
2018-12-28 17:17:51 +03:00
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
return false, false, err4, err6
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
ok4, err4 = checkOtherDHCPv4(iface)
|
|
|
|
ok6, err6 = checkOtherDHCPv6(iface)
|
|
|
|
|
|
|
|
return ok4, ok6, err4, err6
|
|
|
|
}
|
|
|
|
|
|
|
|
// ifaceIPv4Subnet returns the first suitable IPv4 subnetwork iface has.
|
2022-11-02 16:18:02 +03:00
|
|
|
func ifaceIPv4Subnet(iface *net.Interface) (subnet netip.Prefix, err error) {
|
2021-08-12 17:33:53 +03:00
|
|
|
var addrs []net.Addr
|
|
|
|
if addrs, err = iface.Addrs(); err != nil {
|
2022-11-02 16:18:02 +03:00
|
|
|
return netip.Prefix{}, err
|
2020-08-12 18:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
for _, a := range addrs {
|
2022-11-02 16:18:02 +03:00
|
|
|
var ip net.IP
|
|
|
|
var maskLen int
|
2021-08-12 17:33:53 +03:00
|
|
|
switch a := a.(type) {
|
|
|
|
case *net.IPAddr:
|
2022-11-02 16:18:02 +03:00
|
|
|
ip = a.IP
|
|
|
|
maskLen, _ = ip.DefaultMask().Size()
|
2021-08-12 17:33:53 +03:00
|
|
|
case *net.IPNet:
|
2022-11-02 16:18:02 +03:00
|
|
|
ip = a.IP
|
|
|
|
maskLen, _ = a.Mask.Size()
|
2021-08-12 17:33:53 +03:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2018-12-28 17:17:51 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
if ip = ip.To4(); ip != nil {
|
|
|
|
return netip.PrefixFrom(netip.AddrFrom4(*(*[4]byte)(ip)), maskLen), nil
|
2021-08-12 17:33:53 +03:00
|
|
|
}
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
return netip.Prefix{}, fmt.Errorf("interface %s has no ipv4 addresses", iface.Name)
|
2021-08-12 17:33:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-11-02 16:18:02 +03:00
|
|
|
var subnet netip.Prefix
|
2021-08-12 17:33:53 +03:00
|
|
|
if subnet, err = ifaceIPv4Subnet(iface); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve broadcast addr.
|
2022-11-02 16:18:02 +03:00
|
|
|
dst := netip.AddrPortFrom(BroadcastFromPref(subnet), 67).String()
|
2021-08-12 17:33:53 +03:00
|
|
|
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 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03:00
|
|
|
var hostname string
|
|
|
|
if hostname, err = os.Hostname(); err != nil {
|
|
|
|
return false, fmt.Errorf("couldn't get hostname: %w", err)
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03: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 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03: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-28 17:44:10 +03:00
|
|
|
// ignores broadcast packets when reading.
|
2021-08-12 17:33:53 +03:00
|
|
|
var c net.PacketConn
|
2021-08-16 15:53:33 +03:00
|
|
|
if c, err = listenPacketReusable(iface.Name, "udp4", ":68"); 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
|
|
|
}
|
2021-08-12 17:33:53 +03:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2018-12-28 17:17:51 +03:00
|
|
|
|
2021-08-16 15:53:33 +03:00
|
|
|
// Send to broadcast.
|
2021-08-12 17:33:53 +03: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 17:17:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-16 17:36:34 +03:00
|
|
|
for {
|
2021-08-12 17:33:53 +03:00
|
|
|
if err = c.SetDeadline(time.Now().Add(defaultDiscoverTime)); err != nil {
|
|
|
|
return false, fmt.Errorf("setting deadline: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-03-11 21:30:52 +03:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn4(req, c, iface)
|
2020-11-09 19:27:04 +03:00
|
|
|
if next {
|
2020-11-13 14:40:22 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 21:30:52 +03:00
|
|
|
|
2019-04-16 17:36:34 +03:00
|
|
|
if err != nil {
|
2020-11-13 14:40:22 +03:00
|
|
|
return false, err
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
2020-11-25 14:26:26 +03:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-09 19:27:04 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-16 19:01:12 +03: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 19:27:04 +03:00
|
|
|
// TODO: replicate dhclient's behavior of retrying several times with
|
|
|
|
// progressively longer timeouts.
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Tracef("dhcpv4: waiting %v for an answer", defaultDiscoverTime)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
b := make([]byte, 1500)
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
2022-03-29 16:21:22 +03:00
|
|
|
if errors.Is(err, os.ErrDeadlineExceeded) {
|
2020-11-09 19:27:04 +03:00
|
|
|
log.Debug("dhcpv4: didn't receive dhcp response")
|
|
|
|
|
|
|
|
return false, false, nil
|
2019-04-16 17:36:34 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Tracef("dhcpv4: received packet, %d bytes", n)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
response, err := dhcpv4.FromBytes(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv4: encoding: %s", err)
|
2019-04-16 17:36:34 +03:00
|
|
|
|
2020-11-09 19:27:04 +03:00
|
|
|
return false, true, err
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-11-09 19:27:04 +03:00
|
|
|
|
|
|
|
log.Debug("dhcpv4: received message from server: %s", response.Summary())
|
|
|
|
|
2022-03-29 16:21:22 +03:00
|
|
|
switch {
|
|
|
|
case
|
|
|
|
response.OpCode != dhcpv4.OpcodeBootReply,
|
|
|
|
response.HWType != iana.HWTypeEthernet,
|
|
|
|
!bytes.Equal(response.ClientHWAddr, iface.HardwareAddr),
|
|
|
|
response.TransactionID != req.TransactionID,
|
|
|
|
!response.Options.Has(dhcpv4.OptionDHCPMessageType):
|
|
|
|
log.Debug("dhcpv4: received response doesn't match the request")
|
2020-11-09 19:27:04 +03:00
|
|
|
|
|
|
|
return false, true, nil
|
2022-03-29 16:21:22 +03:00
|
|
|
default:
|
|
|
|
log.Tracef("dhcpv4: the packet is from an active dhcp server")
|
2020-11-09 19:27:04 +03:00
|
|
|
|
2022-03-29 16:21:22 +03:00
|
|
|
return true, false, nil
|
|
|
|
}
|
2018-12-28 17:17:51 +03:00
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2021-08-12 17:33:53 +03: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 18:45:27 +03:00
|
|
|
if err != nil {
|
2021-08-12 17:33:53 +03:00
|
|
|
return false, fmt.Errorf("getting ipv6 addrs for iface %s: %w", iface.Name, err)
|
2020-11-09 19:27:04 +03:00
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
if len(ifaceIPNet) == 0 {
|
2021-08-12 17:33:53 +03:00
|
|
|
return false, fmt.Errorf("interface %s has no ipv6 addresses", iface.Name)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
srcIP := ifaceIPNet[0]
|
2021-08-09 16:03:37 +03:00
|
|
|
src := netutil.JoinHostPort(srcIP.String(), 546)
|
2020-08-13 18:45:27 +03:00
|
|
|
dst := "[ff02::1:2]:547"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-12 17:33:53 +03: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 18:45:27 +03:00
|
|
|
log.Debug("DHCPv6: Listening to udp6 %+v", udpAddr)
|
2021-08-12 17:33:53 +03:00
|
|
|
c, err := nclient6.NewIPv6UDPConn(iface.Name, dhcpv6.DefaultClientPort)
|
2020-08-13 18:45:27 +03:00
|
|
|
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
|
|
|
}
|
2021-08-12 17:33:53 +03:00
|
|
|
defer func() { err = errors.WithDeferred(err, c.Close()) }()
|
2020-08-13 18:45:27 +03:00
|
|
|
|
|
|
|
_, err = c.WriteTo(req.ToBytes(), dstAddr)
|
|
|
|
if err != nil {
|
2021-08-12 17:33:53 +03:00
|
|
|
return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: %w", dstAddr, err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2021-03-11 21:30:52 +03:00
|
|
|
var next bool
|
|
|
|
ok, next, err = tryConn6(req, c)
|
2020-11-16 19:01:12 +03:00
|
|
|
if next {
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("dhcpv6: trying a connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-03-11 21:30:52 +03:00
|
|
|
|
2020-08-13 18:45:27 +03:00
|
|
|
if err != nil {
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, err
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2020-11-25 14:26:26 +03:00
|
|
|
|
|
|
|
return ok, nil
|
2020-11-16 19:01:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03: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 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03: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 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
if err != nil {
|
2022-03-29 16:21:22 +03:00
|
|
|
if errors.Is(err, os.ErrDeadlineExceeded) {
|
2020-11-16 19:01:12 +03:00
|
|
|
log.Debug("dhcpv6: didn't receive dhcp response")
|
2020-08-13 18:45:27 +03:00
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, false, nil
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 19:01:12 +03:00
|
|
|
return false, false, fmt.Errorf("receiving packet: %w", err)
|
2020-08-13 18:45:27 +03:00
|
|
|
}
|
2020-11-16 19:01:12 +03: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 &&
|
2023-06-07 20:04:01 +03:00
|
|
|
cid.Equal(rcid)) {
|
2020-11-16 19:01:12 +03:00
|
|
|
|
|
|
|
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 18:45:27 +03:00
|
|
|
}
|