2021-03-16 19:42:15 +03:00
|
|
|
// Package aghnet contains some utilities for networking.
|
|
|
|
package aghnet
|
2020-02-13 18:42:07 +03:00
|
|
|
|
|
|
|
import (
|
2022-03-30 15:11:57 +03:00
|
|
|
"bytes"
|
2023-07-20 14:26:35 +03:00
|
|
|
"context"
|
2021-01-21 19:55:41 +03:00
|
|
|
"encoding/json"
|
2020-02-13 18:42:07 +03:00
|
|
|
"fmt"
|
2021-12-16 20:54:59 +03:00
|
|
|
"io"
|
2020-02-13 18:42:07 +03:00
|
|
|
"net"
|
2022-10-14 15:29:44 +03:00
|
|
|
"net/netip"
|
2023-09-28 19:11:11 +03:00
|
|
|
"net/url"
|
2023-11-16 14:05:10 +03:00
|
|
|
"strings"
|
2020-02-13 18:42:07 +03:00
|
|
|
"syscall"
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-11-16 14:05:10 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-02-13 18:42:07 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-02-13 13:19:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2020-02-13 18:42:07 +03:00
|
|
|
)
|
|
|
|
|
2023-07-20 14:26:35 +03:00
|
|
|
// DialContextFunc is the semantic alias for dialing functions, such as
|
|
|
|
// [http.Transport.DialContext].
|
|
|
|
type DialContextFunc = func(ctx context.Context, network, addr string) (conn net.Conn, err error)
|
|
|
|
|
2022-03-31 19:56:50 +03:00
|
|
|
// Variables and functions to substitute in tests.
|
|
|
|
var (
|
|
|
|
// aghosRunCommand is the function to run shell commands.
|
|
|
|
aghosRunCommand = aghos.RunCommand
|
2022-03-30 15:11:57 +03:00
|
|
|
|
2022-03-31 19:56:50 +03:00
|
|
|
// netInterfaces is the function to get the available network interfaces.
|
|
|
|
netInterfaceAddrs = net.InterfaceAddrs
|
|
|
|
|
|
|
|
// rootDirFS is the filesystem pointing to the root directory.
|
2024-02-13 13:19:22 +03:00
|
|
|
rootDirFS = osutil.RootDirFS()
|
2022-03-31 19:56:50 +03:00
|
|
|
)
|
2022-03-30 15:11:57 +03:00
|
|
|
|
2021-03-16 19:42:15 +03:00
|
|
|
// ErrNoStaticIPInfo is returned by IfaceHasStaticIP when no information about
|
|
|
|
// the IP being static is available.
|
2021-05-24 17:28:11 +03:00
|
|
|
const ErrNoStaticIPInfo errors.Error = "no information about static ip"
|
2021-03-16 19:42:15 +03:00
|
|
|
|
|
|
|
// IfaceHasStaticIP checks if interface is configured to have static IP address.
|
|
|
|
// If it can't give a definitive answer, it returns false and an error for which
|
|
|
|
// errors.Is(err, ErrNoStaticIPInfo) is true.
|
|
|
|
func IfaceHasStaticIP(ifaceName string) (has bool, err error) {
|
|
|
|
return ifaceHasStaticIP(ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IfaceSetStaticIP sets static IP address for network interface.
|
|
|
|
func IfaceSetStaticIP(ifaceName string) (err error) {
|
|
|
|
return ifaceSetStaticIP(ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GatewayIP returns IP address of interface's gateway.
|
2022-03-30 15:11:57 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Investigate if the gateway address may be fetched in another
|
|
|
|
// way since not every machine has the software installed.
|
2022-10-14 15:29:44 +03:00
|
|
|
func GatewayIP(ifaceName string) (ip netip.Addr) {
|
2022-03-30 15:11:57 +03:00
|
|
|
code, out, err := aghosRunCommand("ip", "route", "show", "dev", ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("%s", err)
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
return netip.Addr{}
|
2022-03-30 15:11:57 +03:00
|
|
|
} else if code != 0 {
|
|
|
|
log.Debug("fetching gateway ip: unexpected exit code: %d", code)
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
return netip.Addr{}
|
2021-03-16 19:42:15 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
fields := bytes.Fields(out)
|
2021-03-16 19:42:15 +03:00
|
|
|
// The meaningful "ip route" command output should contain the word
|
2022-01-21 19:21:38 +03:00
|
|
|
// "default" at first field and default gateway IP address at third field.
|
2022-03-30 15:11:57 +03:00
|
|
|
if len(fields) < 3 || string(fields[0]) != "default" {
|
2022-10-14 15:29:44 +03:00
|
|
|
return netip.Addr{}
|
2021-03-16 19:42:15 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ip, err = netip.ParseAddr(string(fields[2]))
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
2021-03-16 19:42:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 17:02:45 +03:00
|
|
|
// CanBindPrivilegedPorts checks if current process can bind to privileged
|
|
|
|
// ports.
|
|
|
|
func CanBindPrivilegedPorts() (can bool, err error) {
|
|
|
|
return canBindPrivilegedPorts()
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
// NetInterface represents an entry of network interfaces map.
|
2020-02-13 18:42:07 +03:00
|
|
|
type NetInterface struct {
|
2021-08-09 16:03:37 +03:00
|
|
|
// Addresses are the network interface addresses.
|
2022-10-14 15:29:44 +03:00
|
|
|
Addresses []netip.Addr `json:"ip_addresses,omitempty"`
|
2021-08-09 16:03:37 +03:00
|
|
|
// Subnets are the IP networks for this network interface.
|
2022-10-14 15:29:44 +03:00
|
|
|
Subnets []netip.Prefix `json:"-"`
|
2021-01-21 19:55:41 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
HardwareAddr net.HardwareAddr `json:"hardware_address"`
|
|
|
|
Flags net.Flags `json:"flags"`
|
2021-08-09 16:03:37 +03:00
|
|
|
MTU int `json:"mtu"`
|
2021-01-21 19:55:41 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 19:36:13 +03:00
|
|
|
// MarshalJSON implements the json.Marshaler interface for NetInterface.
|
|
|
|
func (iface NetInterface) MarshalJSON() ([]byte, error) {
|
2021-01-21 19:55:41 +03:00
|
|
|
type netInterface NetInterface
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
HardwareAddr string `json:"hardware_address"`
|
|
|
|
Flags string `json:"flags"`
|
2021-05-14 19:41:45 +03:00
|
|
|
netInterface
|
2021-01-21 19:55:41 +03:00
|
|
|
}{
|
|
|
|
HardwareAddr: iface.HardwareAddr.String(),
|
|
|
|
Flags: iface.Flags.String(),
|
2021-05-14 19:41:45 +03:00
|
|
|
netInterface: netInterface(iface),
|
2021-01-21 19:55:41 +03:00
|
|
|
})
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
func NetInterfaceFrom(iface *net.Interface) (niface *NetInterface, err error) {
|
|
|
|
niface = &NetInterface{
|
|
|
|
Name: iface.Name,
|
|
|
|
HardwareAddr: iface.HardwareAddr,
|
|
|
|
Flags: iface.Flags,
|
|
|
|
MTU: iface.MTU,
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := iface.Addrs()
|
2020-02-13 18:42:07 +03:00
|
|
|
if err != nil {
|
2022-10-14 15:29:44 +03:00
|
|
|
return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
// Collect network interface addresses.
|
|
|
|
for _, addr := range addrs {
|
|
|
|
n, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
// Should be *net.IPNet, this is weird.
|
|
|
|
return nil, fmt.Errorf("expected %[2]s to be %[1]T, got %[2]T", n, addr)
|
|
|
|
} else if ip4 := n.IP.To4(); ip4 != nil {
|
|
|
|
n.IP = ip4
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ip, ok := netip.AddrFromSlice(n.IP)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("bad address %s", n.IP)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ip = ip.Unmap()
|
|
|
|
if ip.IsLinkLocalUnicast() {
|
|
|
|
// Ignore link-local IPv4.
|
|
|
|
if ip.Is4() {
|
2020-02-13 18:42:07 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-03-30 15:11:57 +03:00
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ip = ip.WithZone(iface.Name)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ones, _ := n.Mask.Size()
|
|
|
|
p := netip.PrefixFrom(ip, ones)
|
|
|
|
|
|
|
|
niface.Addresses = append(niface.Addresses, ip)
|
|
|
|
niface.Subnets = append(niface.Subnets, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return niface, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidNetInterfacesForWeb returns interfaces that are eligible for DNS and
|
|
|
|
// WEB only we do not return link-local addresses here.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Can't properly test the function since it's nontrivial to
|
|
|
|
// substitute net.Interface.Addrs and the net.InterfaceAddrs can't be used.
|
|
|
|
func GetValidNetInterfacesForWeb() (nifaces []*NetInterface, err error) {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting interfaces: %w", err)
|
|
|
|
} else if len(ifaces) == 0 {
|
|
|
|
return nil, errors.Error("no legible interfaces")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ifaces {
|
|
|
|
var niface *NetInterface
|
|
|
|
niface, err = NetInterfaceFrom(&ifaces[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(niface.Addresses) != 0 {
|
|
|
|
// Discard interfaces with no addresses.
|
|
|
|
nifaces = append(nifaces, niface)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
return nifaces, nil
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:39:28 +03:00
|
|
|
// InterfaceByIP returns the name of the interface bound to ip.
|
2022-03-31 19:56:50 +03:00
|
|
|
//
|
2022-08-10 13:39:28 +03:00
|
|
|
// TODO(a.garipov, e.burkov): This function is technically incorrect, since one
|
|
|
|
// IP address can be shared by multiple interfaces in some configurations.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): See TODO on GetValidNetInterfacesForWeb.
|
2022-10-14 15:29:44 +03:00
|
|
|
func InterfaceByIP(ip netip.Addr) (ifaceName string) {
|
2020-02-13 18:42:07 +03:00
|
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
for _, addr := range iface.Addresses {
|
2022-10-14 15:29:44 +03:00
|
|
|
if ip == addr {
|
2020-02-13 18:42:07 +03:00
|
|
|
return iface.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
// GetSubnet returns the subnet corresponding to the interface of zero prefix if
|
2021-01-20 17:27:53 +03:00
|
|
|
// the search fails.
|
2022-03-31 19:56:50 +03:00
|
|
|
//
|
2022-08-10 13:39:28 +03:00
|
|
|
// TODO(e.burkov): See TODO on GetValidNetInterfacesForWeb.
|
2022-10-14 15:29:44 +03:00
|
|
|
func GetSubnet(ifaceName string) (p netip.Prefix) {
|
2020-02-13 18:42:07 +03:00
|
|
|
netIfaces, err := GetValidNetInterfacesForWeb()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not get network interfaces info: %v", err)
|
2022-10-14 15:29:44 +03:00
|
|
|
|
|
|
|
return p
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, netIface := range netIfaces {
|
|
|
|
if netIface.Name == ifaceName && len(netIface.Subnets) > 0 {
|
|
|
|
return netIface.Subnets[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
return p
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:16:08 +03:00
|
|
|
// CheckPort checks if the port is available for binding. network is expected
|
|
|
|
// to be one of "udp" and "tcp".
|
2022-10-14 15:29:44 +03:00
|
|
|
func CheckPort(network string, ipp netip.AddrPort) (err error) {
|
2021-12-16 20:54:59 +03:00
|
|
|
var c io.Closer
|
2022-10-14 15:29:44 +03:00
|
|
|
addr := ipp.String()
|
2021-12-16 20:54:59 +03:00
|
|
|
switch network {
|
|
|
|
case "tcp":
|
|
|
|
c, err = net.Listen(network, addr)
|
|
|
|
case "udp":
|
|
|
|
c, err = net.ListenPacket(network, addr)
|
|
|
|
default:
|
|
|
|
return nil
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:16:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return closePortChecker(c)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
// IsAddrInUse checks if err is about unsuccessful address binding.
|
|
|
|
func IsAddrInUse(err error) (ok bool) {
|
|
|
|
var sysErr syscall.Errno
|
|
|
|
if !errors.As(err, &sysErr) {
|
2020-02-13 18:42:07 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
return isAddrInUse(sysErr)
|
2020-02-13 18:42:07 +03:00
|
|
|
}
|
2021-03-22 16:46:36 +03:00
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// CollectAllIfacesAddrs returns the slice of all network interfaces IP
|
|
|
|
// addresses without port number.
|
2023-09-28 19:11:11 +03:00
|
|
|
func CollectAllIfacesAddrs() (addrs []netip.Addr, err error) {
|
2022-03-31 19:56:50 +03:00
|
|
|
var ifaceAddrs []net.Addr
|
|
|
|
ifaceAddrs, err = netInterfaceAddrs()
|
2021-04-07 20:16:06 +03:00
|
|
|
if err != nil {
|
2022-03-31 19:56:50 +03:00
|
|
|
return nil, fmt.Errorf("getting interfaces addresses: %w", err)
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:56:50 +03:00
|
|
|
for _, addr := range ifaceAddrs {
|
2023-09-28 19:11:11 +03:00
|
|
|
var p netip.Prefix
|
|
|
|
p, err = netip.ParsePrefix(addr.String())
|
2021-04-07 20:16:06 +03:00
|
|
|
if err != nil {
|
2023-09-28 19:11:11 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
2023-09-28 19:11:11 +03:00
|
|
|
addrs = append(addrs, p.Addr())
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
2021-08-12 17:33:53 +03:00
|
|
|
|
2023-09-28 19:11:11 +03:00
|
|
|
// ParseAddrPort parses an [netip.AddrPort] from s, which should be either a
|
|
|
|
// valid IP, optionally with port, or a valid URL with plain IP address. The
|
|
|
|
// defaultPort is used if s doesn't contain port number.
|
|
|
|
func ParseAddrPort(s string, defaultPort uint16) (ipp netip.AddrPort, err error) {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err == nil && u.Host != "" {
|
|
|
|
s = u.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
ipp, err = netip.ParseAddrPort(s)
|
|
|
|
if err != nil {
|
|
|
|
ip, parseErr := netip.ParseAddr(s)
|
|
|
|
if parseErr != nil {
|
|
|
|
return ipp, errors.Join(err, parseErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return netip.AddrPortFrom(ip, defaultPort), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipp, nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
// ParseSubnet parses s either as a CIDR prefix itself, or as an IP address,
|
|
|
|
// returning the corresponding single-IP CIDR prefix.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Taken from dnsproxy, move to golibs.
|
|
|
|
func ParseSubnet(s string) (p netip.Prefix, err error) {
|
|
|
|
if strings.Contains(s, "/") {
|
|
|
|
p, err = netip.ParsePrefix(s)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Prefix{}, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var ip netip.Addr
|
|
|
|
ip, err = netip.ParseAddr(s)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Prefix{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p = netip.PrefixFrom(ip, ip.BitLen())
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseBootstraps returns the slice of upstream resolvers parsed from addrs.
|
|
|
|
// It additionally returns the closers for each resolver, that should be closed
|
|
|
|
// after use.
|
|
|
|
func ParseBootstraps(
|
|
|
|
addrs []string,
|
|
|
|
opts *upstream.Options,
|
|
|
|
) (boots []*upstream.UpstreamResolver, err error) {
|
|
|
|
boots = make([]*upstream.UpstreamResolver, 0, len(boots))
|
|
|
|
for i, b := range addrs {
|
|
|
|
var r *upstream.UpstreamResolver
|
|
|
|
r, err = upstream.NewUpstreamResolver(b, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bootstrap at index %d: %w", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
boots = append(boots, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return boots, nil
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
// BroadcastFromPref calculates the broadcast IP address for p.
|
|
|
|
func BroadcastFromPref(p netip.Prefix) (bc netip.Addr) {
|
|
|
|
bc = p.Addr().Unmap()
|
|
|
|
if !bc.IsValid() {
|
|
|
|
return netip.Addr{}
|
|
|
|
}
|
2021-08-12 17:33:53 +03:00
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
maskLen, addrLen := p.Bits(), bc.BitLen()
|
|
|
|
if maskLen == addrLen {
|
|
|
|
return bc
|
2021-08-12 17:33:53 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
ipBytes := bc.AsSlice()
|
|
|
|
for i := maskLen; i < addrLen; i++ {
|
|
|
|
ipBytes[i/8] |= 1 << (7 - (i % 8))
|
2021-08-12 17:33:53 +03:00
|
|
|
}
|
2022-10-14 15:29:44 +03:00
|
|
|
bc, _ = netip.AddrFromSlice(ipBytes)
|
2021-08-12 17:33:53 +03:00
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
return bc
|
2021-08-12 17:33:53 +03:00
|
|
|
}
|