github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/drivers/bridge/port_mapping.go (about) 1 package bridge 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "net" 8 "sync" 9 10 "github.com/docker/libnetwork/types" 11 "github.com/ishidawataru/sctp" 12 "github.com/sirupsen/logrus" 13 ) 14 15 func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) { 16 if ep.extConnConfig == nil || ep.extConnConfig.PortBindings == nil { 17 return nil, nil 18 } 19 20 defHostIP := net.IPv4zero // 0.0.0.0 21 if reqDefBindIP != nil { 22 defHostIP = reqDefBindIP 23 } 24 25 var containerIPv6 net.IP 26 if ep.addrv6 != nil { 27 containerIPv6 = ep.addrv6.IP 28 } 29 30 pb, err := n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addr.IP, containerIPv6, defHostIP, ulPxyEnabled) 31 if err != nil { 32 return nil, err 33 } 34 return pb, nil 35 } 36 37 func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, containerIPv4, containerIPv6, defHostIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) { 38 bs := make([]types.PortBinding, 0, len(bindings)) 39 for _, c := range bindings { 40 bIPv4 := c.GetCopy() 41 bIPv6 := c.GetCopy() 42 // Allocate IPv4 Port mappings 43 if ok := n.validatePortBindingIPv4(&bIPv4, containerIPv4, defHostIP); ok { 44 if err := n.allocatePort(&bIPv4, ulPxyEnabled); err != nil { 45 // On allocation failure, release previously allocated ports. On cleanup error, just log a warning message 46 if cuErr := n.releasePortsInternal(bs); cuErr != nil { 47 logrus.Warnf("allocation failure for %v, failed to clear previously allocated ipv4 port bindings: %v", bIPv4, cuErr) 48 } 49 return nil, err 50 } 51 bs = append(bs, bIPv4) 52 } 53 54 // skip adding implicit v6 addr, when the kernel was booted with `ipv6.disable=1` 55 // https://github.com/moby/moby/issues/42288 56 isV6Binding := c.HostIP != nil && c.HostIP.To4() == nil 57 if !isV6Binding && !IsV6Listenable() { 58 continue 59 } 60 61 // Allocate IPv6 Port mappings 62 // If the container has no IPv6 address, allow proxying host IPv6 traffic to it 63 // by setting up the binding with the IPv4 interface if the userland proxy is enabled 64 // This change was added to keep backward compatibility 65 containerIP := containerIPv6 66 if ulPxyEnabled && (containerIPv6 == nil) { 67 containerIP = containerIPv4 68 } 69 if ok := n.validatePortBindingIPv6(&bIPv6, containerIP, defHostIP); ok { 70 if err := n.allocatePort(&bIPv6, ulPxyEnabled); err != nil { 71 // On allocation failure, release previously allocated ports. On cleanup error, just log a warning message 72 if cuErr := n.releasePortsInternal(bs); cuErr != nil { 73 logrus.Warnf("allocation failure for %v, failed to clear previously allocated ipv6 port bindings: %v", bIPv6, cuErr) 74 } 75 return nil, err 76 } 77 bs = append(bs, bIPv6) 78 } 79 } 80 return bs, nil 81 } 82 83 // validatePortBindingIPv4 validates the port binding, populates the missing Host IP field and returns true 84 // if this is a valid IPv4 binding, else returns false 85 func (n *bridgeNetwork) validatePortBindingIPv4(bnd *types.PortBinding, containerIPv4, defHostIP net.IP) bool { 86 //Return early if there is a valid Host IP, but its not a IPv4 address 87 if len(bnd.HostIP) > 0 && bnd.HostIP.To4() == nil { 88 return false 89 } 90 // Adjust the host address in the operational binding 91 if len(bnd.HostIP) == 0 { 92 // Return early if the default binding address is an IPv6 address 93 if defHostIP.To4() == nil { 94 return false 95 } 96 bnd.HostIP = defHostIP 97 } 98 bnd.IP = containerIPv4 99 return true 100 101 } 102 103 // validatePortBindingIPv6 validates the port binding, populates the missing Host IP field and returns true 104 // if this is a valid IPv6 binding, else returns false 105 func (n *bridgeNetwork) validatePortBindingIPv6(bnd *types.PortBinding, containerIP, defHostIP net.IP) bool { 106 // Return early if there is no container endpoint 107 if containerIP == nil { 108 return false 109 } 110 // Return early if there is a valid Host IP, which is a IPv4 address 111 if len(bnd.HostIP) > 0 && bnd.HostIP.To4() != nil { 112 return false 113 } 114 115 // Setup a binding to "::" if Host IP is empty and the default binding IP is 0.0.0.0 116 if len(bnd.HostIP) == 0 { 117 if defHostIP.Equal(net.IPv4zero) { 118 bnd.HostIP = net.IPv6zero 119 // If the default binding IP is an IPv6 address, use it 120 } else if defHostIP.To4() == nil { 121 bnd.HostIP = defHostIP 122 // Return false if default binding ip is an IPv4 address 123 } else { 124 return false 125 } 126 } 127 bnd.IP = containerIP 128 return true 129 } 130 131 func (n *bridgeNetwork) allocatePort(bnd *types.PortBinding, ulPxyEnabled bool) error { 132 var ( 133 host net.Addr 134 err error 135 ) 136 137 // Adjust HostPortEnd if this is not a range. 138 if bnd.HostPortEnd == 0 { 139 bnd.HostPortEnd = bnd.HostPort 140 } 141 142 // Construct the container side transport address 143 container, err := bnd.ContainerAddr() 144 if err != nil { 145 return err 146 } 147 148 portmapper := n.portMapper 149 150 if bnd.HostIP.To4() == nil { 151 portmapper = n.portMapperV6 152 } 153 154 // Try up to maxAllocatePortAttempts times to get a port that's not already allocated. 155 for i := 0; i < maxAllocatePortAttempts; i++ { 156 if host, err = portmapper.MapRange(container, bnd.HostIP, int(bnd.HostPort), int(bnd.HostPortEnd), ulPxyEnabled); err == nil { 157 break 158 } 159 // There is no point in immediately retrying to map an explicitly chosen port. 160 if bnd.HostPort != 0 { 161 logrus.Warnf("Failed to allocate and map port %d-%d: %s", bnd.HostPort, bnd.HostPortEnd, err) 162 break 163 } 164 logrus.Warnf("Failed to allocate and map port: %s, retry: %d", err, i+1) 165 } 166 if err != nil { 167 return err 168 } 169 170 // Save the host port (regardless it was or not specified in the binding) 171 switch netAddr := host.(type) { 172 case *net.TCPAddr: 173 bnd.HostPort = uint16(host.(*net.TCPAddr).Port) 174 return nil 175 case *net.UDPAddr: 176 bnd.HostPort = uint16(host.(*net.UDPAddr).Port) 177 return nil 178 case *sctp.SCTPAddr: 179 bnd.HostPort = uint16(host.(*sctp.SCTPAddr).Port) 180 return nil 181 default: 182 // For completeness 183 return ErrUnsupportedAddressType(fmt.Sprintf("%T", netAddr)) 184 } 185 } 186 187 func (n *bridgeNetwork) releasePorts(ep *bridgeEndpoint) error { 188 return n.releasePortsInternal(ep.portMapping) 189 } 190 191 func (n *bridgeNetwork) releasePortsInternal(bindings []types.PortBinding) error { 192 var errorBuf bytes.Buffer 193 194 // Attempt to release all port bindings, do not stop on failure 195 for _, m := range bindings { 196 if err := n.releasePort(m); err != nil { 197 errorBuf.WriteString(fmt.Sprintf("\ncould not release %v because of %v", m, err)) 198 } 199 } 200 201 if errorBuf.Len() != 0 { 202 return errors.New(errorBuf.String()) 203 } 204 return nil 205 } 206 207 func (n *bridgeNetwork) releasePort(bnd types.PortBinding) error { 208 // Construct the host side transport address 209 host, err := bnd.HostAddr() 210 if err != nil { 211 return err 212 } 213 214 portmapper := n.portMapper 215 216 if bnd.HostIP.To4() == nil { 217 portmapper = n.portMapperV6 218 } 219 220 return portmapper.Unmap(host) 221 } 222 223 var ( 224 v6ListenableCached bool 225 v6ListenableOnce sync.Once 226 ) 227 228 // IsV6Listenable returns true when `[::1]:0` is listenable. 229 // IsV6Listenable returns false mostly when the kernel was booted with `ipv6.disable=1` option. 230 func IsV6Listenable() bool { 231 v6ListenableOnce.Do(func() { 232 ln, err := net.Listen("tcp6", "[::1]:0") 233 if err != nil { 234 // When the kernel was booted with `ipv6.disable=1`, 235 // we get err "listen tcp6 [::1]:0: socket: address family not supported by protocol" 236 // https://github.com/moby/moby/issues/42288 237 logrus.Debugf("port_mapping: v6Listenable=false (%v)", err) 238 } else { 239 v6ListenableCached = true 240 ln.Close() 241 } 242 }) 243 return v6ListenableCached 244 }