2015-04-08 02:20:04 -04:00
|
|
|
package portmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/portallocator"
|
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-04-08 02:20:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type mapping struct {
|
|
|
|
proto string
|
|
|
|
userlandProxy userlandProxy
|
|
|
|
host net.Addr
|
|
|
|
container net.Addr
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:34:55 -04:00
|
|
|
// newProxy is used to mock out the proxy server in tests
|
2015-04-10 14:59:05 -04:00
|
|
|
var newProxy = newProxyCommand
|
2015-04-08 02:20:04 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrUnknownBackendAddressType refers to an unknown container or unsupported address type
|
|
|
|
ErrUnknownBackendAddressType = errors.New("unknown container address type not supported")
|
|
|
|
// ErrPortMappedForIP refers to a port already mapped to an ip address
|
|
|
|
ErrPortMappedForIP = errors.New("port is already mapped to ip")
|
2015-04-20 00:04:08 -04:00
|
|
|
// ErrPortNotMapped refers to an unmapped port
|
2015-04-08 02:20:04 -04:00
|
|
|
ErrPortNotMapped = errors.New("port is not mapped")
|
2017-06-13 01:29:56 -04:00
|
|
|
// ErrSCTPAddrNoIP refers to a SCTP address without IP address.
|
|
|
|
ErrSCTPAddrNoIP = errors.New("sctp address does not contain any IP address")
|
2015-04-08 02:20:04 -04:00
|
|
|
)
|
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
// New returns a new instance of PortMapper
|
2016-09-25 11:43:27 -04:00
|
|
|
func New(proxyPath string) *PortMapper {
|
|
|
|
return NewWithPortAllocator(portallocator.Get(), proxyPath)
|
2015-04-10 14:59:05 -04:00
|
|
|
}
|
|
|
|
|
2015-04-20 00:04:08 -04:00
|
|
|
// NewWithPortAllocator returns a new instance of PortMapper which will use the specified PortAllocator
|
2016-09-25 11:43:27 -04:00
|
|
|
func NewWithPortAllocator(allocator *portallocator.PortAllocator, proxyPath string) *PortMapper {
|
2015-04-10 14:59:05 -04:00
|
|
|
return &PortMapper{
|
|
|
|
currentMappings: make(map[string]*mapping),
|
|
|
|
Allocator: allocator,
|
2016-09-25 11:43:27 -04:00
|
|
|
proxyPath: proxyPath,
|
2015-04-10 14:59:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:20:04 -04:00
|
|
|
// Map maps the specified container transport address to the host's network address and transport port
|
2015-05-18 19:49:12 -04:00
|
|
|
func (pm *PortMapper) Map(container net.Addr, hostIP net.IP, hostPort int, useProxy bool) (host net.Addr, err error) {
|
2015-06-11 21:19:42 -04:00
|
|
|
return pm.MapRange(container, hostIP, hostPort, hostPort, useProxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapRange maps the specified container transport address to the host's network address and transport port range
|
|
|
|
func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart, hostPortEnd int, useProxy bool) (host net.Addr, err error) {
|
2015-04-10 14:59:05 -04:00
|
|
|
pm.lock.Lock()
|
|
|
|
defer pm.lock.Unlock()
|
2015-04-08 02:20:04 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
m *mapping
|
|
|
|
proto string
|
|
|
|
allocatedHostPort int
|
|
|
|
)
|
|
|
|
|
2021-05-27 20:15:56 -04:00
|
|
|
switch t := container.(type) {
|
2015-04-08 02:20:04 -04:00
|
|
|
case *net.TCPAddr:
|
|
|
|
proto = "tcp"
|
2015-06-11 21:19:42 -04:00
|
|
|
if allocatedHostPort, err = pm.Allocator.RequestPortInRange(hostIP, proto, hostPortStart, hostPortEnd); err != nil {
|
2015-04-08 02:20:04 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = &mapping{
|
|
|
|
proto: proto,
|
|
|
|
host: &net.TCPAddr{IP: hostIP, Port: allocatedHostPort},
|
|
|
|
container: container,
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04:00
|
|
|
if useProxy {
|
2021-05-27 20:15:56 -04:00
|
|
|
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, t.IP, t.Port, pm.proxyPath)
|
2016-06-09 07:31:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-21 15:20:48 -04:00
|
|
|
} else {
|
2017-06-13 01:29:56 -04:00
|
|
|
m.userlandProxy, err = newDummyProxy(proto, hostIP, allocatedHostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-18 19:49:12 -04:00
|
|
|
}
|
2015-04-08 02:20:04 -04:00
|
|
|
case *net.UDPAddr:
|
|
|
|
proto = "udp"
|
2015-06-11 21:19:42 -04:00
|
|
|
if allocatedHostPort, err = pm.Allocator.RequestPortInRange(hostIP, proto, hostPortStart, hostPortEnd); err != nil {
|
2015-04-08 02:20:04 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = &mapping{
|
|
|
|
proto: proto,
|
|
|
|
host: &net.UDPAddr{IP: hostIP, Port: allocatedHostPort},
|
|
|
|
container: container,
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04:00
|
|
|
if useProxy {
|
2021-05-27 20:15:56 -04:00
|
|
|
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, t.IP, t.Port, pm.proxyPath)
|
2016-06-09 07:31:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-21 15:20:48 -04:00
|
|
|
} else {
|
2017-06-13 01:29:56 -04:00
|
|
|
m.userlandProxy, err = newDummyProxy(proto, hostIP, allocatedHostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *sctp.SCTPAddr:
|
|
|
|
proto = "sctp"
|
|
|
|
if allocatedHostPort, err = pm.Allocator.RequestPortInRange(hostIP, proto, hostPortStart, hostPortEnd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = &mapping{
|
|
|
|
proto: proto,
|
2019-05-02 07:23:31 -04:00
|
|
|
host: &sctp.SCTPAddr{IPAddrs: []net.IPAddr{{IP: hostIP}}, Port: allocatedHostPort},
|
2017-06-13 01:29:56 -04:00
|
|
|
container: container,
|
|
|
|
}
|
|
|
|
|
|
|
|
if useProxy {
|
|
|
|
sctpAddr := container.(*sctp.SCTPAddr)
|
2019-05-02 07:23:31 -04:00
|
|
|
if len(sctpAddr.IPAddrs) == 0 {
|
2017-06-13 01:29:56 -04:00
|
|
|
return nil, ErrSCTPAddrNoIP
|
|
|
|
}
|
2019-05-02 07:23:31 -04:00
|
|
|
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, sctpAddr.IPAddrs[0].IP, sctpAddr.Port, pm.proxyPath)
|
2017-06-13 01:29:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m.userlandProxy, err = newDummyProxy(proto, hostIP, allocatedHostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-18 19:49:12 -04:00
|
|
|
}
|
2015-04-08 02:20:04 -04:00
|
|
|
default:
|
|
|
|
return nil, ErrUnknownBackendAddressType
|
|
|
|
}
|
|
|
|
|
|
|
|
// release the allocated port on any further error during return.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-04-10 14:59:05 -04:00
|
|
|
pm.Allocator.ReleasePort(hostIP, proto, allocatedHostPort)
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
key := getKey(m.host)
|
2015-04-10 14:59:05 -04:00
|
|
|
if _, exists := pm.currentMappings[key]; exists {
|
2015-04-08 02:20:04 -04:00
|
|
|
return nil, ErrPortMappedForIP
|
|
|
|
}
|
|
|
|
|
|
|
|
containerIP, containerPort := getIPAndPort(m.container)
|
2020-12-13 18:56:30 -05:00
|
|
|
if err := pm.AppendForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort); err != nil {
|
|
|
|
return nil, err
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() error {
|
|
|
|
// need to undo the iptables rules before we return
|
2015-05-21 15:20:48 -04:00
|
|
|
m.userlandProxy.Stop()
|
2020-12-13 18:56:30 -05:00
|
|
|
pm.DeleteForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort)
|
|
|
|
if err := pm.Allocator.ReleasePort(hostIP, m.proto, allocatedHostPort); err != nil {
|
|
|
|
return err
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:43:02 -04:00
|
|
|
if err := m.userlandProxy.Start(); err != nil {
|
|
|
|
if err := cleanup(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error during port allocation cleanup: %v", err)
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
2020-07-22 09:43:02 -04:00
|
|
|
return nil, err
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
2015-05-18 19:49:12 -04:00
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
pm.currentMappings[key] = m
|
2015-04-08 02:20:04 -04:00
|
|
|
return m.host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap removes stored mapping for the specified host transport address
|
2015-04-10 14:59:05 -04:00
|
|
|
func (pm *PortMapper) Unmap(host net.Addr) error {
|
|
|
|
pm.lock.Lock()
|
|
|
|
defer pm.lock.Unlock()
|
2015-04-08 02:20:04 -04:00
|
|
|
|
|
|
|
key := getKey(host)
|
2015-04-10 14:59:05 -04:00
|
|
|
data, exists := pm.currentMappings[key]
|
2015-04-08 02:20:04 -04:00
|
|
|
if !exists {
|
|
|
|
return ErrPortNotMapped
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:49:12 -04:00
|
|
|
if data.userlandProxy != nil {
|
|
|
|
data.userlandProxy.Stop()
|
|
|
|
}
|
2015-04-08 02:20:04 -04:00
|
|
|
|
2015-04-10 14:59:05 -04:00
|
|
|
delete(pm.currentMappings, key)
|
2015-04-08 02:20:04 -04:00
|
|
|
|
|
|
|
containerIP, containerPort := getIPAndPort(data.container)
|
|
|
|
hostIP, hostPort := getIPAndPort(data.host)
|
2019-04-17 13:54:10 -04:00
|
|
|
if err := pm.DeleteForwardingTableEntry(data.proto, hostIP, hostPort, containerIP.String(), containerPort); err != nil {
|
2015-04-10 14:59:05 -04:00
|
|
|
logrus.Errorf("Error on iptables delete: %s", err)
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch a := host.(type) {
|
|
|
|
case *net.TCPAddr:
|
2015-04-10 14:59:05 -04:00
|
|
|
return pm.Allocator.ReleasePort(a.IP, "tcp", a.Port)
|
2015-04-08 02:20:04 -04:00
|
|
|
case *net.UDPAddr:
|
2015-04-10 14:59:05 -04:00
|
|
|
return pm.Allocator.ReleasePort(a.IP, "udp", a.Port)
|
2017-06-13 01:29:56 -04:00
|
|
|
case *sctp.SCTPAddr:
|
2019-05-02 07:23:31 -04:00
|
|
|
if len(a.IPAddrs) == 0 {
|
2017-06-13 01:29:56 -04:00
|
|
|
return ErrSCTPAddrNoIP
|
|
|
|
}
|
2019-05-02 07:23:31 -04:00
|
|
|
return pm.Allocator.ReleasePort(a.IPAddrs[0].IP, "sctp", a.Port)
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
2017-06-13 01:29:56 -04:00
|
|
|
return ErrUnknownBackendAddressType
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 11:34:55 -04:00
|
|
|
// ReMapAll re-applies all port mappings
|
2015-07-24 13:20:48 -04:00
|
|
|
func (pm *PortMapper) ReMapAll() {
|
2015-08-24 10:47:44 -04:00
|
|
|
pm.lock.Lock()
|
|
|
|
defer pm.lock.Unlock()
|
2015-07-24 13:20:48 -04:00
|
|
|
logrus.Debugln("Re-applying all port mappings.")
|
|
|
|
for _, data := range pm.currentMappings {
|
|
|
|
containerIP, containerPort := getIPAndPort(data.container)
|
|
|
|
hostIP, hostPort := getIPAndPort(data.host)
|
2019-04-17 13:54:10 -04:00
|
|
|
if err := pm.AppendForwardingTableEntry(data.proto, hostIP, hostPort, containerIP.String(), containerPort); err != nil {
|
2015-07-24 13:20:48 -04:00
|
|
|
logrus.Errorf("Error on iptables add: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:20:04 -04:00
|
|
|
func getKey(a net.Addr) string {
|
|
|
|
switch t := a.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return fmt.Sprintf("%s:%d/%s", t.IP.String(), t.Port, "tcp")
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return fmt.Sprintf("%s:%d/%s", t.IP.String(), t.Port, "udp")
|
2017-06-13 01:29:56 -04:00
|
|
|
case *sctp.SCTPAddr:
|
2019-05-02 07:23:31 -04:00
|
|
|
if len(t.IPAddrs) == 0 {
|
2017-06-13 01:29:56 -04:00
|
|
|
logrus.Error(ErrSCTPAddrNoIP)
|
|
|
|
return ""
|
|
|
|
}
|
2019-05-02 07:23:31 -04:00
|
|
|
return fmt.Sprintf("%s:%d/%s", t.IPAddrs[0].IP.String(), t.Port, "sctp")
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIPAndPort(a net.Addr) (net.IP, int) {
|
|
|
|
switch t := a.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return t.IP, t.Port
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return t.IP, t.Port
|
2017-06-13 01:29:56 -04:00
|
|
|
case *sctp.SCTPAddr:
|
2019-05-02 07:23:31 -04:00
|
|
|
if len(t.IPAddrs) == 0 {
|
2017-06-13 01:29:56 -04:00
|
|
|
logrus.Error(ErrSCTPAddrNoIP)
|
|
|
|
return nil, 0
|
|
|
|
}
|
2019-05-02 07:23:31 -04:00
|
|
|
return t.IPAddrs[0].IP, t.Port
|
2015-04-08 02:20:04 -04:00
|
|
|
}
|
|
|
|
return nil, 0
|
|
|
|
}
|