2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
|
|
|
|
2015-05-20 19:28:58 +00:00
|
|
|
// Network utility functions.
|
|
|
|
|
|
|
|
package netutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-10-01 19:37:21 +00:00
|
|
|
"os"
|
2015-05-20 19:28:58 +00:00
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
|
|
|
"github.com/docker/docker/libnetwork/resolvconf"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2019-04-01 18:14:54 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-05-20 19:28:58 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2022-10-01 19:37:21 +00:00
|
|
|
var networkGetRoutesFct func(netlink.Link, int) ([]netlink.Route, error)
|
2015-05-20 19:28:58 +00:00
|
|
|
|
|
|
|
// CheckRouteOverlaps checks whether the passed network overlaps with any existing routes
|
|
|
|
func CheckRouteOverlaps(toCheck *net.IPNet) error {
|
2022-11-07 22:32:33 +00:00
|
|
|
networkGetRoutesFct := networkGetRoutesFct
|
2016-05-16 18:51:40 +00:00
|
|
|
if networkGetRoutesFct == nil {
|
|
|
|
networkGetRoutesFct = ns.NlHandle().RouteList
|
|
|
|
}
|
2015-05-20 19:28:58 +00:00
|
|
|
networks, err := networkGetRoutesFct(nil, netlink.FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
2021-07-05 14:54:13 +00:00
|
|
|
if network.Dst != nil && network.Scope == netlink.SCOPE_LINK && NetworkOverlaps(toCheck, network.Dst) {
|
2015-05-20 19:28:58 +00:00
|
|
|
return ErrNetworkOverlaps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateIfaceName returns an interface name using the passed in
|
|
|
|
// prefix and the length of random bytes. The api ensures that the
|
|
|
|
// there are is no interface which exists with that name.
|
2016-05-16 18:51:40 +00:00
|
|
|
func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, error) {
|
|
|
|
linkByName := netlink.LinkByName
|
|
|
|
if nlh != nil {
|
|
|
|
linkByName = nlh.LinkByName
|
|
|
|
}
|
2015-05-20 19:28:58 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
name, err := GenerateRandomName(prefix, len)
|
|
|
|
if err != nil {
|
2023-01-21 23:23:07 +00:00
|
|
|
return "", err
|
2015-05-20 19:28:58 +00:00
|
|
|
}
|
2016-05-16 18:51:40 +00:00
|
|
|
_, err = linkByName(name)
|
|
|
|
if err != nil {
|
2023-01-21 23:23:07 +00:00
|
|
|
if errors.As(err, &netlink.LinkNotFoundError{}) {
|
2015-05-20 19:28:58 +00:00
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", types.InternalErrorf("could not generate interface name")
|
|
|
|
}
|
2016-02-26 22:58:11 +00:00
|
|
|
|
|
|
|
// FindAvailableNetwork returns a network from the passed list which does not
|
|
|
|
// overlap with existing interfaces in the system
|
|
|
|
func FindAvailableNetwork(list []*net.IPNet) (*net.IPNet, error) {
|
|
|
|
// We don't check for an error here, because we don't really care if we
|
|
|
|
// can't read /etc/resolv.conf. So instead we skip the append if resolvConf
|
|
|
|
// is nil. It either doesn't exist, or we can't read it for some reason.
|
|
|
|
var nameservers []string
|
2022-10-01 19:37:21 +00:00
|
|
|
if rc, err := os.ReadFile(resolvconf.Path()); err == nil {
|
|
|
|
nameservers = resolvconf.GetNameserversAsCIDR(rc)
|
2016-02-26 22:58:11 +00:00
|
|
|
}
|
|
|
|
for _, nw := range list {
|
|
|
|
if err := CheckNameserverOverlaps(nameservers, nw); err == nil {
|
|
|
|
if err := CheckRouteOverlaps(nw); err == nil {
|
|
|
|
return nw, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 19:37:21 +00:00
|
|
|
return nil, errors.New("no available network")
|
2016-02-26 22:58:11 +00:00
|
|
|
}
|