2015-03-24 16:56:52 -04:00
|
|
|
// Network utility functions.
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
package netutils
|
2015-03-24 16:56:52 -04:00
|
|
|
|
|
|
|
import (
|
2015-04-14 00:12:40 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2015-03-24 16:56:52 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-04-14 00:12:40 -04:00
|
|
|
"io"
|
2015-03-24 16:56:52 -04:00
|
|
|
"net"
|
2015-06-03 12:36:47 -04:00
|
|
|
"strings"
|
2015-03-24 16:56:52 -04:00
|
|
|
|
2015-06-03 12:36:47 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-03-24 16:56:52 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNetworkOverlapsWithNameservers preformatted error
|
|
|
|
ErrNetworkOverlapsWithNameservers = errors.New("requested network overlaps with nameserver")
|
|
|
|
// ErrNetworkOverlaps preformatted error
|
|
|
|
ErrNetworkOverlaps = errors.New("requested network overlaps with existing network")
|
|
|
|
// ErrNoDefaultRoute preformatted error
|
|
|
|
ErrNoDefaultRoute = errors.New("no default route")
|
|
|
|
|
|
|
|
networkGetRoutesFct = netlink.RouteList
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckNameserverOverlaps checks whether the passed network overlaps with any of the nameservers
|
|
|
|
func CheckNameserverOverlaps(nameservers []string, toCheck *net.IPNet) error {
|
|
|
|
if len(nameservers) > 0 {
|
|
|
|
for _, ns := range nameservers {
|
|
|
|
_, nsNetwork, err := net.ParseCIDR(ns)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if NetworkOverlaps(toCheck, nsNetwork) {
|
|
|
|
return ErrNetworkOverlapsWithNameservers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRouteOverlaps checks whether the passed network overlaps with any existing routes
|
|
|
|
func CheckRouteOverlaps(toCheck *net.IPNet) error {
|
|
|
|
networks, err := networkGetRoutesFct(nil, netlink.FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range networks {
|
|
|
|
if network.Dst != nil && NetworkOverlaps(toCheck, network.Dst) {
|
|
|
|
return ErrNetworkOverlaps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkOverlaps detects overlap between one IPNet and another
|
|
|
|
func NetworkOverlaps(netX *net.IPNet, netY *net.IPNet) bool {
|
2015-08-06 13:04:01 -04:00
|
|
|
return netX.Contains(netY.IP) || netY.Contains(netX.IP)
|
2015-03-24 16:56:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkRange calculates the first and last IP addresses in an IPNet
|
|
|
|
func NetworkRange(network *net.IPNet) (net.IP, net.IP) {
|
2015-07-27 19:48:30 -04:00
|
|
|
if network == nil {
|
2015-03-24 16:56:52 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:48:30 -04:00
|
|
|
firstIP := network.IP.Mask(network.Mask)
|
|
|
|
lastIP := types.GetIPCopy(firstIP)
|
|
|
|
for i := 0; i < len(firstIP); i++ {
|
|
|
|
lastIP[i] = firstIP[i] | ^network.Mask[i]
|
2015-03-24 16:56:52 -04:00
|
|
|
}
|
2015-07-27 19:48:30 -04:00
|
|
|
|
|
|
|
if network.IP.To4() != nil {
|
|
|
|
firstIP = firstIP.To4()
|
|
|
|
lastIP = lastIP.To4()
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstIP, lastIP
|
2015-03-24 16:56:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIfaceAddr returns the first IPv4 address and slice of IPv6 addresses for the specified network interface
|
|
|
|
func GetIfaceAddr(name string) (net.Addr, []net.Addr, error) {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
var addrs4 []net.Addr
|
|
|
|
var addrs6 []net.Addr
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip := (addr.(*net.IPNet)).IP
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
addrs4 = append(addrs4, addr)
|
|
|
|
} else if ip6 := ip.To16(); len(ip6) == net.IPv6len {
|
|
|
|
addrs6 = append(addrs6, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case len(addrs4) == 0:
|
|
|
|
return nil, nil, fmt.Errorf("Interface %v has no IPv4 addresses", name)
|
|
|
|
case len(addrs4) > 1:
|
|
|
|
fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n",
|
|
|
|
name, (addrs4[0].(*net.IPNet)).IP)
|
|
|
|
}
|
|
|
|
return addrs4[0], addrs6, nil
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:33:45 -04:00
|
|
|
func genMAC(ip net.IP) net.HardwareAddr {
|
2015-03-24 16:56:52 -04:00
|
|
|
hw := make(net.HardwareAddr, 6)
|
2015-04-14 00:12:40 -04:00
|
|
|
// The first byte of the MAC address has to comply with these rules:
|
|
|
|
// 1. Unicast: Set the least-significant bit to 0.
|
|
|
|
// 2. Address is locally administered: Set the second-least-significant bit (U/L) to 1.
|
|
|
|
hw[0] = 0x02
|
|
|
|
// The first 24 bits of the MAC represent the Organizationally Unique Identifier (OUI).
|
|
|
|
// Since this address is locally administered, we can do whatever we want as long as
|
|
|
|
// it doesn't conflict with other addresses.
|
|
|
|
hw[1] = 0x42
|
2015-07-29 18:33:45 -04:00
|
|
|
// Fill the remaining 4 bytes based on the input
|
|
|
|
if ip == nil {
|
|
|
|
rand.Read(hw[2:])
|
|
|
|
} else {
|
|
|
|
copy(hw[2:], ip.To4())
|
2015-03-24 16:56:52 -04:00
|
|
|
}
|
|
|
|
return hw
|
|
|
|
}
|
2015-04-14 00:12:40 -04:00
|
|
|
|
2015-07-29 18:33:45 -04:00
|
|
|
// GenerateRandomMAC returns a new 6-byte(48-bit) hardware address (MAC)
|
|
|
|
func GenerateRandomMAC() net.HardwareAddr {
|
|
|
|
return genMAC(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateMACFromIP returns a locally administered MAC address where the 4 least
|
|
|
|
// significant bytes are derived from the IPv4 address.
|
|
|
|
func GenerateMACFromIP(ip net.IP) net.HardwareAddr {
|
|
|
|
return genMAC(ip)
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:12:40 -04:00
|
|
|
// GenerateRandomName returns a new name joined with a prefix. This size
|
|
|
|
// specified is used to truncate the randomly generated value
|
|
|
|
func GenerateRandomName(prefix string, size int) (string, error) {
|
|
|
|
id := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return prefix + hex.EncodeToString(id)[:size], nil
|
|
|
|
}
|
2015-06-03 12:36:47 -04:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
func GenerateIfaceName(prefix string, len int) (string, error) {
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
name, err := GenerateRandomName(prefix, len)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := net.InterfaceByName(name); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "no such") {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", types.InternalErrorf("could not generate interface name")
|
|
|
|
}
|