network.go
5cecd548
 package docker
 
 import (
e0e49b9a
 	"encoding/binary"
797bb6e7
 	"errors"
5039d4a2
 	"fmt"
2e69e172
 	"github.com/dotcloud/docker/utils"
799ffa17
 	"log"
5cecd548
 	"net"
799ffa17
 	"os/exec"
 	"strconv"
 	"strings"
a5fb1d6c
 	"sync"
5cecd548
 )
 
f39af7e0
 var NetworkBridgeIface string
 
5cecd548
 const (
8cf30395
 	DefaultNetworkBridge = "docker0"
49673fc4
 	DisableNetworkBridge = "none"
1b370f9d
 	portRangeStart       = 49153
 	portRangeEnd         = 65535
5cecd548
 )
 
799ffa17
 // Calculates the first and last IP addresses in an IPNet
e0e49b9a
 func networkRange(network *net.IPNet) (net.IP, net.IP) {
 	netIP := network.IP.To4()
 	firstIP := netIP.Mask(network.Mask)
 	lastIP := net.IPv4(0, 0, 0, 0).To4()
 	for i := 0; i < len(lastIP); i++ {
 		lastIP[i] = netIP[i] | ^network.Mask[i]
 	}
 	return firstIP, lastIP
 }
 
90a6e310
 // Detects overlap between one IPNet and another
 func networkOverlaps(netX *net.IPNet, netY *net.IPNet) bool {
 	firstIP, _ := networkRange(netX)
 	if netY.Contains(firstIP) {
 		return true
 	}
 	firstIP, _ = networkRange(netY)
 	if netX.Contains(firstIP) {
 		return true
 	}
 	return false
 }
 
799ffa17
 // Converts a 4 bytes IP into a 32 bit integer
6f9a67a7
 func ipToInt(ip net.IP) int32 {
 	return int32(binary.BigEndian.Uint32(ip.To4()))
e0e49b9a
 }
 
799ffa17
 // Converts 32 bit integer into a 4 bytes IP address
fd224ee5
 func intToIP(n int32) net.IP {
6f9a67a7
 	b := make([]byte, 4)
 	binary.BigEndian.PutUint32(b, uint32(n))
 	return net.IP(b)
e0e49b9a
 }
 
799ffa17
 // Given a netmask, calculates the number of available hosts
6f9a67a7
 func networkSize(mask net.IPMask) int32 {
c08f5b2b
 	m := net.IPv4Mask(0, 0, 0, 0)
e0e49b9a
 	for i := 0; i < net.IPv4len; i++ {
c08f5b2b
 		m[i] = ^mask[i]
e0e49b9a
 	}
6f9a67a7
 
 	return int32(binary.BigEndian.Uint32(m)) + 1
e0e49b9a
 }
 
aa4bf428
 //Wrapper around the ip command
 func ip(args ...string) (string, error) {
 	path, err := exec.LookPath("ip")
 	if err != nil {
 		return "", fmt.Errorf("command not found: ip")
 	}
 	output, err := exec.Command(path, args...).CombinedOutput()
 	if err != nil {
 		return "", fmt.Errorf("ip failed: ip %v", strings.Join(args, " "))
 	}
 	return string(output), nil
 }
 
799ffa17
 // Wrapper around the iptables command
 func iptables(args ...string) error {
dfc3904f
 	path, err := exec.LookPath("iptables")
 	if err != nil {
c66d2b6a
 		return fmt.Errorf("command not found: iptables")
dfc3904f
 	}
 	if err := exec.Command(path, args...).Run(); err != nil {
799ffa17
 		return fmt.Errorf("iptables failed: iptables %v", strings.Join(args, " "))
 	}
 	return nil
 }
 
aa4bf428
 func checkRouteOverlaps(dockerNetwork *net.IPNet) error {
 	output, err := ip("route")
 	if err != nil {
 		return err
 	}
2e69e172
 	utils.Debugf("Routes:\n\n%s", output)
aa4bf428
 	for _, line := range strings.Split(output, "\n") {
 		if strings.Trim(line, "\r\n\t ") == "" || strings.Contains(line, "default") {
 			continue
 		}
 		if _, network, err := net.ParseCIDR(strings.Split(line, " ")[0]); err != nil {
 			return fmt.Errorf("Unexpected ip route output: %s (%s)", err, line)
 		} else if networkOverlaps(dockerNetwork, network) {
 			return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork.String(), line)
 		}
 	}
 	return nil
 }
 
4714f102
 // CreateBridgeIface creates a network bridge interface on the host system with the name `ifaceName`,
 // and attempts to configure it with an address which doesn't conflict with any other interface on the host.
 // If it can't find an address which doesn't conflict, it will return an error.
aa4bf428
 func CreateBridgeIface(ifaceName string) error {
4714f102
 	addrs := []string{
 		// Here we don't follow the convention of using the 1st IP of the range for the gateway.
 		// This is to use the same gateway IPs as the /24 ranges, which predate the /16 ranges.
 		// In theory this shouldn't matter - in practice there's bound to be a few scripts relying
 		// on the internal addressing or other stupid things like that.
 		// The shouldn't, but hey, let's not break them unless we really have to.
 		"172.16.42.1/16",
 		"10.0.42.1/16", // Don't even try using the entire /8, that's too intrusive
 		"10.1.42.1/16",
 		"10.42.42.1/16",
 		"172.16.42.1/24",
 		"172.16.43.1/24",
 		"172.16.44.1/24",
 		"10.0.42.1/24",
 		"10.0.43.1/24",
 		"192.168.42.1/24",
 		"192.168.43.1/24",
 		"192.168.44.1/24",
 	}
aa4bf428
 
 	var ifaceAddr string
 	for _, addr := range addrs {
 		_, dockerNetwork, err := net.ParseCIDR(addr)
 		if err != nil {
 			return err
 		}
 		if err := checkRouteOverlaps(dockerNetwork); err == nil {
 			ifaceAddr = addr
 			break
 		} else {
2e69e172
 			utils.Debugf("%s: %s", addr, err)
aa4bf428
 		}
 	}
 	if ifaceAddr == "" {
1601366c
 		return fmt.Errorf("Could not find a free IP address range for interface '%s'. Please configure its address manually and run 'docker -b %s'", ifaceName, ifaceName)
aa4bf428
 	}
86ada2fa
 	utils.Debugf("Creating bridge %s with network %s", ifaceName, ifaceAddr)
aa4bf428
 
 	if output, err := ip("link", "add", ifaceName, "type", "bridge"); err != nil {
 		return fmt.Errorf("Error creating bridge: %s (output: %s)", err, output)
 	}
 
 	if output, err := ip("addr", "add", ifaceAddr, "dev", ifaceName); err != nil {
 		return fmt.Errorf("Unable to add private network: %s (%s)", err, output)
 	}
 	if output, err := ip("link", "set", ifaceName, "up"); err != nil {
 		return fmt.Errorf("Unable to start network bridge: %s (%s)", err, output)
 	}
 	if err := iptables("-t", "nat", "-A", "POSTROUTING", "-s", ifaceAddr,
 		"!", "-d", ifaceAddr, "-j", "MASQUERADE"); err != nil {
 		return fmt.Errorf("Unable to enable network bridge NAT: %s", err)
 	}
 	return nil
 }
 
799ffa17
 // Return the IPv4 address of a network interface
c08f5b2b
 func getIfaceAddr(name string) (net.Addr, error) {
5039d4a2
 	iface, err := net.InterfaceByName(name)
 	if err != nil {
 		return nil, err
 	}
 	addrs, err := iface.Addrs()
 	if err != nil {
 		return nil, err
 	}
 	var addrs4 []net.Addr
 	for _, addr := range addrs {
 		ip := (addr.(*net.IPNet)).IP
 		if ip4 := ip.To4(); len(ip4) == net.IPv4len {
 			addrs4 = append(addrs4, addr)
 		}
 	}
 	switch {
 	case len(addrs4) == 0:
799ffa17
 		return nil, fmt.Errorf("Interface %v has no IP addresses", name)
5039d4a2
 	case len(addrs4) > 1:
3aefed2d
 		fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n",
 			name, (addrs4[0].(*net.IPNet)).IP)
5039d4a2
 	}
 	return addrs4[0], nil
 }
 
799ffa17
 // Port mapper takes care of mapping external ports to containers by setting
 // up iptables rules.
 // It keeps track of all mappings and is able to unmap at will
 type PortMapper struct {
fac0d87d
 	tcpMapping map[int]*net.TCPAddr
 	tcpProxies map[int]Proxy
 	udpMapping map[int]*net.UDPAddr
 	udpProxies map[int]Proxy
799ffa17
 }
 
 func (mapper *PortMapper) cleanup() error {
 	// Ignore errors - This could mean the chains were never set up
3b65be91
 	iptables("-t", "nat", "-D", "PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "DOCKER")
61259ab4
 	iptables("-t", "nat", "-D", "OUTPUT", "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8", "-j", "DOCKER")
 	iptables("-t", "nat", "-D", "OUTPUT", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "DOCKER") // Created in versions <= 0.1.6
ebc83795
 	// Also cleanup rules created by older versions, or -X might fail.
 	iptables("-t", "nat", "-D", "PREROUTING", "-j", "DOCKER")
 	iptables("-t", "nat", "-D", "OUTPUT", "-j", "DOCKER")
799ffa17
 	iptables("-t", "nat", "-F", "DOCKER")
 	iptables("-t", "nat", "-X", "DOCKER")
fac0d87d
 	mapper.tcpMapping = make(map[int]*net.TCPAddr)
 	mapper.tcpProxies = make(map[int]Proxy)
 	mapper.udpMapping = make(map[int]*net.UDPAddr)
 	mapper.udpProxies = make(map[int]Proxy)
799ffa17
 	return nil
 }
 
 func (mapper *PortMapper) setup() error {
 	if err := iptables("-t", "nat", "-N", "DOCKER"); err != nil {
523803d6
 		return fmt.Errorf("Failed to create DOCKER chain: %s", err)
5039d4a2
 	}
3b65be91
 	if err := iptables("-t", "nat", "-A", "PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "DOCKER"); err != nil {
523803d6
 		return fmt.Errorf("Failed to inject docker in PREROUTING chain: %s", err)
799ffa17
 	}
61259ab4
 	if err := iptables("-t", "nat", "-A", "OUTPUT", "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8", "-j", "DOCKER"); err != nil {
523803d6
 		return fmt.Errorf("Failed to inject docker in OUTPUT chain: %s", err)
3c6b8bb8
 	}
799ffa17
 	return nil
 }
 
fac0d87d
 func (mapper *PortMapper) iptablesForward(rule string, port int, proto string, dest_addr string, dest_port int) error {
 	return iptables("-t", "nat", rule, "DOCKER", "-p", proto, "--dport", strconv.Itoa(port),
 		"-j", "DNAT", "--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port)))
799ffa17
 }
 
fac0d87d
 func (mapper *PortMapper) Map(port int, backendAddr net.Addr) error {
 	if _, isTCP := backendAddr.(*net.TCPAddr); isTCP {
 		backendPort := backendAddr.(*net.TCPAddr).Port
 		backendIP := backendAddr.(*net.TCPAddr).IP
 		if err := mapper.iptablesForward("-A", port, "tcp", backendIP.String(), backendPort); err != nil {
 			return err
 		}
 		mapper.tcpMapping[port] = backendAddr.(*net.TCPAddr)
 		proxy, err := NewProxy(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port}, backendAddr)
930e9a7e
 		if err != nil {
fac0d87d
 			mapper.Unmap(port, "tcp")
930e9a7e
 			return err
 		}
fac0d87d
 		mapper.tcpProxies[port] = proxy
 		go proxy.Run()
 	} else {
 		backendPort := backendAddr.(*net.UDPAddr).Port
 		backendIP := backendAddr.(*net.UDPAddr).IP
 		if err := mapper.iptablesForward("-A", port, "udp", backendIP.String(), backendPort); err != nil {
 			return err
 		}
 		mapper.udpMapping[port] = backendAddr.(*net.UDPAddr)
 		proxy, err := NewProxy(&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port}, backendAddr)
930e9a7e
 		if err != nil {
fac0d87d
 			mapper.Unmap(port, "udp")
 			return err
930e9a7e
 		}
fac0d87d
 		mapper.udpProxies[port] = proxy
 		go proxy.Run()
930e9a7e
 	}
fac0d87d
 	return nil
930e9a7e
 }
 
fac0d87d
 func (mapper *PortMapper) Unmap(port int, proto string) error {
 	if proto == "tcp" {
 		backendAddr, ok := mapper.tcpMapping[port]
 		if !ok {
 			return fmt.Errorf("Port tcp/%v is not mapped", port)
 		}
 		if proxy, exists := mapper.tcpProxies[port]; exists {
 			proxy.Close()
 			delete(mapper.tcpProxies, port)
 		}
 		if err := mapper.iptablesForward("-D", port, proto, backendAddr.IP.String(), backendAddr.Port); err != nil {
 			return err
 		}
 		delete(mapper.tcpMapping, port)
 	} else {
 		backendAddr, ok := mapper.udpMapping[port]
 		if !ok {
 			return fmt.Errorf("Port udp/%v is not mapped", port)
 		}
 		if proxy, exists := mapper.udpProxies[port]; exists {
 			proxy.Close()
 			delete(mapper.udpProxies, port)
 		}
 		if err := mapper.iptablesForward("-D", port, proto, backendAddr.IP.String(), backendAddr.Port); err != nil {
 			return err
 		}
 		delete(mapper.udpMapping, port)
e0e49b9a
 	}
799ffa17
 	return nil
 }
 
 func newPortMapper() (*PortMapper, error) {
 	mapper := &PortMapper{}
 	if err := mapper.cleanup(); err != nil {
c08f5b2b
 		return nil, err
5cecd548
 	}
799ffa17
 	if err := mapper.setup(); err != nil {
 		return nil, err
 	}
 	return mapper, nil
5cecd548
 }
797bb6e7
 
799ffa17
 // Port allocator: Atomatically allocate and release networking ports
 type PortAllocator struct {
1cf9c80e
 	sync.Mutex
2aad4a34
 	inUse    map[int]struct{}
a5fb1d6c
 	fountain chan (int)
797bb6e7
 }
 
a5fb1d6c
 func (alloc *PortAllocator) runFountain() {
 	for {
 		for port := portRangeStart; port < portRangeEnd; port++ {
 			alloc.fountain <- port
 		}
797bb6e7
 	}
 }
 
a5fb1d6c
 // FIXME: Release can no longer fail, change its prototype to reflect that.
799ffa17
 func (alloc *PortAllocator) Release(port int) error {
2e69e172
 	utils.Debugf("Releasing %d", port)
1cf9c80e
 	alloc.Lock()
a5fb1d6c
 	delete(alloc.inUse, port)
1cf9c80e
 	alloc.Unlock()
a5fb1d6c
 	return nil
 }
 
 func (alloc *PortAllocator) Acquire(port int) (int, error) {
2e69e172
 	utils.Debugf("Acquiring %d", port)
a5fb1d6c
 	if port == 0 {
 		// Allocate a port from the fountain
 		for port := range alloc.fountain {
 			if _, err := alloc.Acquire(port); err == nil {
 				return port, nil
 			}
 		}
 		return -1, fmt.Errorf("Port generator ended unexpectedly")
797bb6e7
 	}
1cf9c80e
 	alloc.Lock()
 	defer alloc.Unlock()
a5fb1d6c
 	if _, inUse := alloc.inUse[port]; inUse {
 		return -1, fmt.Errorf("Port already in use: %d", port)
 	}
 	alloc.inUse[port] = struct{}{}
 	return port, nil
797bb6e7
 }
 
a5fb1d6c
 func newPortAllocator() (*PortAllocator, error) {
 	allocator := &PortAllocator{
22893429
 		inUse:    make(map[int]struct{}),
d32f1846
 		fountain: make(chan int),
a5fb1d6c
 	}
 	go allocator.runFountain()
799ffa17
 	return allocator, nil
 }
 
 // IP allocator: Atomatically allocate and release networking ports
 type IPAllocator struct {
6f9a67a7
 	network       *net.IPNet
 	queueAlloc    chan allocatedIP
 	queueReleased chan net.IP
 	inUse         map[int32]struct{}
 }
 
 type allocatedIP struct {
 	ip  net.IP
 	err error
799ffa17
 }
 
6f9a67a7
 func (alloc *IPAllocator) run() {
799ffa17
 	firstIP, _ := networkRange(alloc.network)
6f9a67a7
 	ipNum := ipToInt(firstIP)
 	ownIP := ipToInt(alloc.network.IP)
 	size := networkSize(alloc.network.Mask)
 
 	pos := int32(1)
 	max := size - 2 // -1 for the broadcast address, -1 for the gateway address
 	for {
 		var (
 			newNum int32
 			inUse  bool
 		)
 
 		// Find first unused IP, give up after one whole round
 		for attempt := int32(0); attempt < max; attempt++ {
 			newNum = ipNum + pos
 
 			pos = pos%max + 1
 
 			// The network's IP is never okay to use
 			if newNum == ownIP {
 				continue
 			}
 
 			if _, inUse = alloc.inUse[newNum]; !inUse {
 				// We found an unused IP
 				break
 			}
797bb6e7
 		}
6f9a67a7
 
fd224ee5
 		ip := allocatedIP{ip: intToIP(newNum)}
6f9a67a7
 		if inUse {
 			ip.err = errors.New("No unallocated IP available")
797bb6e7
 		}
6f9a67a7
 
 		select {
 		case alloc.queueAlloc <- ip:
 			alloc.inUse[newNum] = struct{}{}
 		case released := <-alloc.queueReleased:
 			r := ipToInt(released)
 			delete(alloc.inUse, r)
 
 			if inUse {
 				// If we couldn't allocate a new IP, the released one
 				// will be the only free one now, so instantly use it
 				// next time
 				pos = r - ipNum
 			} else {
 				// Use same IP as last time
 				if pos == 1 {
 					pos = max
 				} else {
 					pos--
 				}
 			}
797bb6e7
 		}
799ffa17
 	}
 }
 
 func (alloc *IPAllocator) Acquire() (net.IP, error) {
6f9a67a7
 	ip := <-alloc.queueAlloc
 	return ip.ip, ip.err
799ffa17
 }
 
6f9a67a7
 func (alloc *IPAllocator) Release(ip net.IP) {
 	alloc.queueReleased <- ip
797bb6e7
 }
 
6f9a67a7
 func newIPAllocator(network *net.IPNet) *IPAllocator {
799ffa17
 	alloc := &IPAllocator{
6f9a67a7
 		network:       network,
 		queueAlloc:    make(chan allocatedIP),
 		queueReleased: make(chan net.IP),
 		inUse:         make(map[int32]struct{}),
799ffa17
 	}
6f9a67a7
 
 	go alloc.run()
 
 	return alloc
799ffa17
 }
 
 // Network interface represents the networking stack of a container
 type NetworkInterface struct {
 	IPNet   net.IPNet
 	Gateway net.IP
 
 	manager  *NetworkManager
fac0d87d
 	extPorts []*Nat
49673fc4
 	disabled bool
799ffa17
 }
 
 // Allocate an external TCP port and map it to the interface
2aad4a34
 func (iface *NetworkInterface) AllocatePort(spec string) (*Nat, error) {
49673fc4
 
 	if iface.disabled {
 		return nil, fmt.Errorf("Trying to allocate port for interface %v, which is disabled", iface) // FIXME
 	}
 
2aad4a34
 	nat, err := parseNat(spec)
799ffa17
 	if err != nil {
2aad4a34
 		return nil, err
 	}
fac0d87d
 
 	if nat.Proto == "tcp" {
 		extPort, err := iface.manager.tcpPortAllocator.Acquire(nat.Frontend)
 		if err != nil {
 			return nil, err
 		}
 		backend := &net.TCPAddr{IP: iface.IPNet.IP, Port: nat.Backend}
 		if err := iface.manager.portMapper.Map(extPort, backend); err != nil {
 			iface.manager.tcpPortAllocator.Release(extPort)
 			return nil, err
 		}
 		nat.Frontend = extPort
 	} else {
 		extPort, err := iface.manager.udpPortAllocator.Acquire(nat.Frontend)
 		if err != nil {
 			return nil, err
 		}
 		backend := &net.UDPAddr{IP: iface.IPNet.IP, Port: nat.Backend}
 		if err := iface.manager.portMapper.Map(extPort, backend); err != nil {
 			iface.manager.udpPortAllocator.Release(extPort)
 			return nil, err
 		}
 		nat.Frontend = extPort
2aad4a34
 	}
fac0d87d
 	iface.extPorts = append(iface.extPorts, nat)
 
2aad4a34
 	return nat, nil
 }
 
 type Nat struct {
 	Proto    string
 	Frontend int
 	Backend  int
 }
 
 func parseNat(spec string) (*Nat, error) {
 	var nat Nat
95d66ebc
 
fac0d87d
 	if strings.Contains(spec, "/") {
 		specParts := strings.Split(spec, "/")
 		if len(specParts) != 2 {
 			return nil, fmt.Errorf("Invalid port format.")
 		}
 		proto := specParts[1]
 		spec = specParts[0]
 		if proto != "tcp" && proto != "udp" {
 			return nil, fmt.Errorf("Invalid port format: unknown protocol %v.", proto)
 		}
 		nat.Proto = proto
 	} else {
 		nat.Proto = "tcp"
 	}
 
95d66ebc
 	if strings.Contains(spec, ":") {
 		specParts := strings.Split(spec, ":")
 		if len(specParts) != 2 {
 			return nil, fmt.Errorf("Invalid port format.")
 		}
 		// If spec starts with ':', external and internal ports must be the same.
 		// This might fail if the requested external port is not available.
 		var sameFrontend bool
 		if len(specParts[0]) == 0 {
 			sameFrontend = true
 		} else {
 			front, err := strconv.ParseUint(specParts[0], 10, 16)
 			if err != nil {
 				return nil, err
 			}
 			nat.Frontend = int(front)
 		}
 		back, err := strconv.ParseUint(specParts[1], 10, 16)
 		if err != nil {
 			return nil, err
 		}
 		nat.Backend = int(back)
 		if sameFrontend {
 			nat.Frontend = nat.Backend
 		}
 	} else {
 		port, err := strconv.ParseUint(spec, 10, 16)
 		if err != nil {
 			return nil, err
 		}
 		nat.Backend = int(port)
799ffa17
 	}
fac0d87d
 
2aad4a34
 	return &nat, nil
799ffa17
 }
 
 // Release: Network cleanup - release all resources
6f9a67a7
 func (iface *NetworkInterface) Release() {
49673fc4
 
 	if iface.disabled {
 		return
 	}
 
fac0d87d
 	for _, nat := range iface.extPorts {
 		utils.Debugf("Unmaping %v/%v", nat.Proto, nat.Frontend)
 		if err := iface.manager.portMapper.Unmap(nat.Frontend, nat.Proto); err != nil {
 			log.Printf("Unable to unmap port %v/%v: %v", nat.Proto, nat.Frontend, err)
799ffa17
 		}
fac0d87d
 		if nat.Proto == "tcp" {
 			if err := iface.manager.tcpPortAllocator.Release(nat.Frontend); err != nil {
 				log.Printf("Unable to release port tcp/%v: %v", nat.Frontend, err)
 			}
 		} else if err := iface.manager.udpPortAllocator.Release(nat.Frontend); err != nil {
 			log.Printf("Unable to release port udp/%v: %v", nat.Frontend, err)
799ffa17
 		}
 	}
6f9a67a7
 
 	iface.manager.ipAllocator.Release(iface.IPNet.IP)
799ffa17
 }
 
 // Network Manager manages a set of network interfaces
 // Only *one* manager per host machine should be used
 type NetworkManager struct {
 	bridgeIface   string
 	bridgeNetwork *net.IPNet
 
fac0d87d
 	ipAllocator      *IPAllocator
 	tcpPortAllocator *PortAllocator
 	udpPortAllocator *PortAllocator
 	portMapper       *PortMapper
49673fc4
 
 	disabled bool
799ffa17
 }
 
 // Allocate a network interface
 func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
49673fc4
 
 	if manager.disabled {
 		return &NetworkInterface{disabled: true}, nil
 	}
 
799ffa17
 	ip, err := manager.ipAllocator.Acquire()
797bb6e7
 	if err != nil {
c08f5b2b
 		return nil, err
797bb6e7
 	}
c08f5b2b
 	iface := &NetworkInterface{
ab99e925
 		IPNet:   net.IPNet{IP: ip, Mask: manager.bridgeNetwork.Mask},
799ffa17
 		Gateway: manager.bridgeNetwork.IP,
 		manager: manager,
c08f5b2b
 	}
 	return iface, nil
 }
 
799ffa17
 func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
49673fc4
 
 	if bridgeIface == DisableNetworkBridge {
 		manager := &NetworkManager{
 			disabled: true,
 		}
 		return manager, nil
 	}
 
799ffa17
 	addr, err := getIfaceAddr(bridgeIface)
 	if err != nil {
aa4bf428
 		// If the iface is not found, try to create it
 		if err := CreateBridgeIface(bridgeIface); err != nil {
 			return nil, err
 		}
 		addr, err = getIfaceAddr(bridgeIface)
 		if err != nil {
 			return nil, err
 		}
799ffa17
 	}
 	network := addr.(*net.IPNet)
 
6f9a67a7
 	ipAllocator := newIPAllocator(network)
799ffa17
 
fac0d87d
 	tcpPortAllocator, err := newPortAllocator()
 	if err != nil {
 		return nil, err
 	}
 	udpPortAllocator, err := newPortAllocator()
799ffa17
 	if err != nil {
 		return nil, err
 	}
 
 	portMapper, err := newPortMapper()
37122552
 	if err != nil {
 		return nil, err
 	}
799ffa17
 
 	manager := &NetworkManager{
fac0d87d
 		bridgeIface:      bridgeIface,
 		bridgeNetwork:    network,
 		ipAllocator:      ipAllocator,
 		tcpPortAllocator: tcpPortAllocator,
 		udpPortAllocator: udpPortAllocator,
 		portMapper:       portMapper,
799ffa17
 	}
 	return manager, nil
797bb6e7
 }