2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-26 02:28:40 -04:00
|
|
|
"sync"
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2017-06-13 01:29:56 -04:00
|
|
|
"github.com/ishidawataru/sctp"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-01 20:01:21 -04:00
|
|
|
)
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
|
|
|
if ep.extConnConfig == nil || ep.extConnConfig.PortBindings == nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
defHostIP := net.IPv4zero // 0.0.0.0
|
2015-05-01 20:01:21 -04:00
|
|
|
if reqDefBindIP != nil {
|
|
|
|
defHostIP = reqDefBindIP
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
var containerIPv6 net.IP
|
|
|
|
if ep.addrv6 != nil {
|
|
|
|
containerIPv6 = ep.addrv6.IP
|
2017-11-28 16:15:55 -05:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
pb, err := n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addr.IP, containerIPv6, defHostIP, ulPxyEnabled)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-28 16:15:55 -05:00
|
|
|
}
|
2020-07-22 09:43:02 -04:00
|
|
|
return pb, nil
|
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, containerIPv4, containerIPv6, defHostIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
2020-07-22 09:43:02 -04:00
|
|
|
bs := make([]types.PortBinding, 0, len(bindings))
|
2015-05-01 20:01:21 -04:00
|
|
|
for _, c := range bindings {
|
2020-12-13 18:56:30 -05:00
|
|
|
bIPv4 := c.GetCopy()
|
|
|
|
bIPv6 := c.GetCopy()
|
|
|
|
// Allocate IPv4 Port mappings
|
|
|
|
if ok := n.validatePortBindingIPv4(&bIPv4, containerIPv4, defHostIP); ok {
|
|
|
|
if err := n.allocatePort(&bIPv4, ulPxyEnabled); err != nil {
|
|
|
|
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
|
|
|
|
if cuErr := n.releasePortsInternal(bs); cuErr != nil {
|
|
|
|
logrus.Warnf("allocation failure for %v, failed to clear previously allocated ipv4 port bindings: %v", bIPv4, cuErr)
|
|
|
|
}
|
|
|
|
return nil, err
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
2020-12-13 18:56:30 -05:00
|
|
|
bs = append(bs, bIPv4)
|
|
|
|
}
|
2021-01-06 19:33:59 -05:00
|
|
|
|
2021-04-26 02:28:40 -04:00
|
|
|
// skip adding implicit v6 addr, when the kernel was booted with `ipv6.disable=1`
|
|
|
|
// https://github.com/moby/moby/issues/42288
|
|
|
|
isV6Binding := c.HostIP != nil && c.HostIP.To4() == nil
|
|
|
|
if !isV6Binding && !IsV6Listenable() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
// Allocate IPv6 Port mappings
|
2021-01-06 19:33:59 -05:00
|
|
|
// If the container has no IPv6 address, allow proxying host IPv6 traffic to it
|
|
|
|
// by setting up the binding with the IPv4 interface if the userland proxy is enabled
|
|
|
|
// This change was added to keep backward compatibility
|
|
|
|
containerIP := containerIPv6
|
|
|
|
if ulPxyEnabled && (containerIPv6 == nil) {
|
|
|
|
containerIP = containerIPv4
|
|
|
|
}
|
|
|
|
if ok := n.validatePortBindingIPv6(&bIPv6, containerIP, defHostIP); ok {
|
2020-12-13 18:56:30 -05:00
|
|
|
if err := n.allocatePort(&bIPv6, ulPxyEnabled); err != nil {
|
|
|
|
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
|
|
|
|
if cuErr := n.releasePortsInternal(bs); cuErr != nil {
|
|
|
|
logrus.Warnf("allocation failure for %v, failed to clear previously allocated ipv6 port bindings: %v", bIPv6, cuErr)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bs = append(bs, bIPv6)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
// validatePortBindingIPv4 validates the port binding, populates the missing Host IP field and returns true
|
|
|
|
// if this is a valid IPv4 binding, else returns false
|
|
|
|
func (n *bridgeNetwork) validatePortBindingIPv4(bnd *types.PortBinding, containerIPv4, defHostIP net.IP) bool {
|
2021-01-06 19:33:59 -05:00
|
|
|
//Return early if there is a valid Host IP, but its not a IPv4 address
|
2020-12-13 18:56:30 -05:00
|
|
|
if len(bnd.HostIP) > 0 && bnd.HostIP.To4() == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-05-01 20:01:21 -04:00
|
|
|
// Adjust the host address in the operational binding
|
|
|
|
if len(bnd.HostIP) == 0 {
|
2020-12-13 18:56:30 -05:00
|
|
|
// Return early if the default binding address is an IPv6 address
|
|
|
|
if defHostIP.To4() == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-05-01 20:01:21 -04:00
|
|
|
bnd.HostIP = defHostIP
|
|
|
|
}
|
2020-12-13 18:56:30 -05:00
|
|
|
bnd.IP = containerIPv4
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// validatePortBindingIPv6 validates the port binding, populates the missing Host IP field and returns true
|
2021-01-06 19:33:59 -05:00
|
|
|
// if this is a valid IPv6 binding, else returns false
|
|
|
|
func (n *bridgeNetwork) validatePortBindingIPv6(bnd *types.PortBinding, containerIP, defHostIP net.IP) bool {
|
|
|
|
// Return early if there is no container endpoint
|
|
|
|
if containerIP == nil {
|
2020-12-13 18:56:30 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Return early if there is a valid Host IP, which is a IPv4 address
|
|
|
|
if len(bnd.HostIP) > 0 && bnd.HostIP.To4() != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a binding to "::" if Host IP is empty and the default binding IP is 0.0.0.0
|
|
|
|
if len(bnd.HostIP) == 0 {
|
|
|
|
if defHostIP.Equal(net.IPv4zero) {
|
|
|
|
bnd.HostIP = net.IPv6zero
|
|
|
|
// If the default binding IP is an IPv6 address, use it
|
|
|
|
} else if defHostIP.To4() == nil {
|
|
|
|
bnd.HostIP = defHostIP
|
|
|
|
// Return false if default binding ip is an IPv4 address
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-01-06 19:33:59 -05:00
|
|
|
bnd.IP = containerIP
|
2020-12-13 18:56:30 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *bridgeNetwork) allocatePort(bnd *types.PortBinding, ulPxyEnabled bool) error {
|
|
|
|
var (
|
|
|
|
host net.Addr
|
|
|
|
err error
|
|
|
|
)
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2015-06-11 21:19:42 -04:00
|
|
|
// Adjust HostPortEnd if this is not a range.
|
|
|
|
if bnd.HostPortEnd == 0 {
|
|
|
|
bnd.HostPortEnd = bnd.HostPort
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
// Construct the container side transport address
|
|
|
|
container, err := bnd.ContainerAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
portmapper := n.portMapper
|
|
|
|
|
2021-01-23 15:00:46 -05:00
|
|
|
if bnd.HostIP.To4() == nil {
|
2017-11-28 16:15:55 -05:00
|
|
|
portmapper = n.portMapperV6
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
// Try up to maxAllocatePortAttempts times to get a port that's not already allocated.
|
|
|
|
for i := 0; i < maxAllocatePortAttempts; i++ {
|
2017-11-28 16:15:55 -05:00
|
|
|
if host, err = portmapper.MapRange(container, bnd.HostIP, int(bnd.HostPort), int(bnd.HostPortEnd), ulPxyEnabled); err == nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// There is no point in immediately retrying to map an explicitly chosen port.
|
|
|
|
if bnd.HostPort != 0 {
|
2015-06-11 21:19:42 -04:00
|
|
|
logrus.Warnf("Failed to allocate and map port %d-%d: %s", bnd.HostPort, bnd.HostPortEnd, err)
|
2015-05-01 20:01:21 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
logrus.Warnf("Failed to allocate and map port: %s, retry: %d", err, i+1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the host port (regardless it was or not specified in the binding)
|
|
|
|
switch netAddr := host.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
bnd.HostPort = uint16(host.(*net.TCPAddr).Port)
|
|
|
|
return nil
|
|
|
|
case *net.UDPAddr:
|
|
|
|
bnd.HostPort = uint16(host.(*net.UDPAddr).Port)
|
|
|
|
return nil
|
2017-06-13 01:29:56 -04:00
|
|
|
case *sctp.SCTPAddr:
|
|
|
|
bnd.HostPort = uint16(host.(*sctp.SCTPAddr).Port)
|
|
|
|
return nil
|
2015-05-01 20:01:21 -04:00
|
|
|
default:
|
|
|
|
// For completeness
|
|
|
|
return ErrUnsupportedAddressType(fmt.Sprintf("%T", netAddr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePorts(ep *bridgeEndpoint) error {
|
|
|
|
return n.releasePortsInternal(ep.portMapping)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePortsInternal(bindings []types.PortBinding) error {
|
2015-05-01 20:01:21 -04:00
|
|
|
var errorBuf bytes.Buffer
|
|
|
|
|
|
|
|
// Attempt to release all port bindings, do not stop on failure
|
|
|
|
for _, m := range bindings {
|
2015-06-01 16:14:29 -04:00
|
|
|
if err := n.releasePort(m); err != nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
errorBuf.WriteString(fmt.Sprintf("\ncould not release %v because of %v", m, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorBuf.Len() != 0 {
|
|
|
|
return errors.New(errorBuf.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePort(bnd types.PortBinding) error {
|
2015-05-01 20:01:21 -04:00
|
|
|
// Construct the host side transport address
|
|
|
|
host, err := bnd.HostAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
|
|
|
|
portmapper := n.portMapper
|
|
|
|
|
2020-07-20 13:53:43 -04:00
|
|
|
if bnd.HostIP.To4() == nil {
|
2017-11-28 16:15:55 -05:00
|
|
|
portmapper = n.portMapperV6
|
|
|
|
}
|
|
|
|
|
|
|
|
return portmapper.Unmap(host)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
2021-04-26 02:28:40 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
v6ListenableCached bool
|
|
|
|
v6ListenableOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsV6Listenable returns true when `[::1]:0` is listenable.
|
|
|
|
// IsV6Listenable returns false mostly when the kernel was booted with `ipv6.disable=1` option.
|
|
|
|
func IsV6Listenable() bool {
|
|
|
|
v6ListenableOnce.Do(func() {
|
|
|
|
ln, err := net.Listen("tcp6", "[::1]:0")
|
|
|
|
if err != nil {
|
|
|
|
// When the kernel was booted with `ipv6.disable=1`,
|
|
|
|
// we get err "listen tcp6 [::1]:0: socket: address family not supported by protocol"
|
|
|
|
// https://github.com/moby/moby/issues/42288
|
|
|
|
logrus.Debugf("port_mapping: v6Listenable=false (%v)", err)
|
|
|
|
} else {
|
|
|
|
v6ListenableCached = true
|
|
|
|
ln.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return v6ListenableCached
|
|
|
|
}
|