2014-01-28 15:42:46 -08:00
|
|
|
package portmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2014-06-19 23:33:51 +02:00
|
|
|
|
2015-03-26 23:22:04 +01:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/networkdriver/portallocator"
|
|
|
|
"github.com/docker/docker/pkg/iptables"
|
2014-01-28 15:42:46 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type mapping struct {
|
|
|
|
proto string
|
2014-08-12 15:04:00 -07:00
|
|
|
userlandProxy UserlandProxy
|
2014-01-28 15:42:46 -08:00
|
|
|
host net.Addr
|
|
|
|
container net.Addr
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:31:21 -07:00
|
|
|
var NewProxy = NewProxyCommand
|
2014-01-28 15:42:46 -08:00
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnknownBackendAddressType = errors.New("unknown container address type not supported")
|
|
|
|
ErrPortMappedForIP = errors.New("port is already mapped to ip")
|
|
|
|
ErrPortNotMapped = errors.New("port is not mapped")
|
|
|
|
)
|
|
|
|
|
2015-03-24 10:29:30 +00:00
|
|
|
type PortMapper struct {
|
|
|
|
chain *iptables.Chain
|
|
|
|
|
|
|
|
// udp:ip:port
|
|
|
|
currentMappings map[string]*mapping
|
|
|
|
lock sync.Mutex
|
|
|
|
|
2015-03-30 18:06:16 -07:00
|
|
|
Allocator *portallocator.PortAllocator
|
2015-03-24 10:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *PortMapper {
|
|
|
|
return NewWithPortAllocator(portallocator.New())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWithPortAllocator(allocator *portallocator.PortAllocator) *PortMapper {
|
|
|
|
return &PortMapper{
|
|
|
|
currentMappings: make(map[string]*mapping),
|
2015-03-30 18:06:16 -07:00
|
|
|
Allocator: allocator,
|
2015-03-24 10:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *PortMapper) SetIptablesChain(c *iptables.Chain) {
|
|
|
|
pm.chain = c
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:19:16 -08:00
|
|
|
func (pm *PortMapper) Map(container net.Addr, hostIP net.IP, hostPort int, useProxy bool) (host net.Addr, err error) {
|
2015-03-24 10:29:30 +00:00
|
|
|
pm.lock.Lock()
|
|
|
|
defer pm.lock.Unlock()
|
2014-01-28 15:42:46 -08:00
|
|
|
|
2014-06-19 23:33:51 +02:00
|
|
|
var (
|
|
|
|
m *mapping
|
|
|
|
proto string
|
|
|
|
allocatedHostPort int
|
|
|
|
)
|
|
|
|
|
2014-01-28 15:42:46 -08:00
|
|
|
switch container.(type) {
|
|
|
|
case *net.TCPAddr:
|
2014-06-19 23:33:51 +02:00
|
|
|
proto = "tcp"
|
2015-03-30 18:06:16 -07:00
|
|
|
if allocatedHostPort, err = pm.Allocator.RequestPort(hostIP, proto, hostPort); err != nil {
|
2014-06-25 13:17:20 -07:00
|
|
|
return nil, err
|
2014-06-19 23:33:51 +02:00
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2014-01-28 15:42:46 -08:00
|
|
|
m = &mapping{
|
2014-06-19 23:33:51 +02:00
|
|
|
proto: proto,
|
|
|
|
host: &net.TCPAddr{IP: hostIP, Port: allocatedHostPort},
|
2014-01-28 15:42:46 -08:00
|
|
|
container: container,
|
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2014-11-10 16:19:16 -08:00
|
|
|
if useProxy {
|
|
|
|
m.userlandProxy = NewProxy(proto, hostIP, allocatedHostPort, container.(*net.TCPAddr).IP, container.(*net.TCPAddr).Port)
|
|
|
|
}
|
2014-01-28 15:42:46 -08:00
|
|
|
case *net.UDPAddr:
|
2014-06-19 23:33:51 +02:00
|
|
|
proto = "udp"
|
2015-03-30 18:06:16 -07:00
|
|
|
if allocatedHostPort, err = pm.Allocator.RequestPort(hostIP, proto, hostPort); err != nil {
|
2014-06-25 13:17:20 -07:00
|
|
|
return nil, err
|
2014-06-19 23:33:51 +02:00
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2014-01-28 15:42:46 -08:00
|
|
|
m = &mapping{
|
2014-06-19 23:33:51 +02:00
|
|
|
proto: proto,
|
|
|
|
host: &net.UDPAddr{IP: hostIP, Port: allocatedHostPort},
|
2014-01-28 15:42:46 -08:00
|
|
|
container: container,
|
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2014-11-10 16:19:16 -08:00
|
|
|
if useProxy {
|
|
|
|
m.userlandProxy = NewProxy(proto, hostIP, allocatedHostPort, container.(*net.UDPAddr).IP, container.(*net.UDPAddr).Port)
|
|
|
|
}
|
2014-01-28 15:42:46 -08:00
|
|
|
default:
|
2014-07-18 16:57:32 +02:00
|
|
|
return nil, ErrUnknownBackendAddressType
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
|
|
|
|
2014-07-18 16:57:32 +02:00
|
|
|
// release the allocated port on any further error during return.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-03-30 18:06:16 -07:00
|
|
|
pm.Allocator.ReleasePort(hostIP, proto, allocatedHostPort)
|
2014-07-18 16:57:32 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-01-28 15:42:46 -08:00
|
|
|
key := getKey(m.host)
|
2015-03-24 10:29:30 +00:00
|
|
|
if _, exists := pm.currentMappings[key]; exists {
|
2014-07-18 16:57:32 +02:00
|
|
|
return nil, ErrPortMappedForIP
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
containerIP, containerPort := getIPAndPort(m.container)
|
2015-03-24 10:29:30 +00:00
|
|
|
if err := pm.forward(iptables.Append, m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort); err != nil {
|
2014-06-26 00:09:19 -07:00
|
|
|
return nil, err
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
|
|
|
|
2014-09-07 12:33:51 -07:00
|
|
|
cleanup := func() error {
|
2014-06-25 13:17:20 -07:00
|
|
|
// need to undo the iptables rules before we return
|
2014-11-10 16:19:16 -08:00
|
|
|
if m.userlandProxy != nil {
|
|
|
|
m.userlandProxy.Stop()
|
|
|
|
}
|
2015-03-24 10:29:30 +00:00
|
|
|
pm.forward(iptables.Delete, m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort)
|
2015-03-30 18:06:16 -07:00
|
|
|
if err := pm.Allocator.ReleasePort(hostIP, m.proto, allocatedHostPort); err != nil {
|
2014-09-07 12:33:51 -07:00
|
|
|
return err
|
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2014-09-07 12:33:51 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:19:16 -08:00
|
|
|
if m.userlandProxy != nil {
|
|
|
|
if err := m.userlandProxy.Start(); err != nil {
|
|
|
|
if err := cleanup(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error during port allocation cleanup: %v", err)
|
|
|
|
}
|
|
|
|
return nil, err
|
2014-09-07 12:33:51 -07:00
|
|
|
}
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
2014-11-10 16:19:16 -08:00
|
|
|
|
2015-03-24 10:29:30 +00:00
|
|
|
pm.currentMappings[key] = m
|
2014-06-19 23:33:51 +02:00
|
|
|
return m.host, nil
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
|
|
|
|
React to firewalld's reload/restart
When firewalld (or iptables service) restarts/reloads,
all previously added docker firewall rules are flushed.
With firewalld we can react to its Reloaded() [1]
D-Bus signal and recreate the firewall rules.
Also when firewalld gets restarted (stopped & started)
we can catch the NameOwnerChanged signal [2].
To specify which signals we want to react to we use AddMatch [3].
Libvirt has been doing this for quite a long time now.
Docker changes firewall rules on basically 3 places.
1) daemon/networkdriver/portmapper/mapper.go - port mappings
Portmapper fortunatelly keeps list of mapped ports,
so we can easily recreate firewall rules on firewalld restart/reload
New ReMapAll() function does that
2) daemon/networkdriver/bridge/driver.go
When setting a bridge, basic firewall rules are created.
This is done at once during start, it's parametrized and nowhere
tracked so how can one know what and how to set it again when
there's been firewalld restart/reload ?
The only solution that came to my mind is using of closures [4],
i.e. I keep list of references to closures (anonymous functions
together with a referencing environment) and when there's firewalld
restart/reload I re-call them in the same order.
3) links/links.go - linking containers
Link is added in Enable() and removed in Disable().
In Enable() we add a callback function, which creates the link,
that's OK so far.
It'd be ideal if we could remove the same function from
the list in Disable(). Unfortunatelly that's not possible AFAICT,
because we don't know the reference to that function
at that moment, so we can only add a reference to function,
which removes the link. That means that after creating and
removing a link there are 2 functions in the list,
one adding and one removing the link and after
firewalld restart/reload both are called.
It works, but it's far from ideal.
[1] https://jpopelka.fedorapeople.org/firewalld/doc/firewalld.dbus.html#FirewallD1.Signals.Reloaded
[2] http://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-name-owner-changed
[3] http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
[4] https://en.wikipedia.org/wiki/Closure_%28computer_programming%29
Signed-off-by: Jiri Popelka <jpopelka@redhat.com>
2014-11-26 19:10:35 +01:00
|
|
|
// re-apply all port mappings
|
|
|
|
func (pm *PortMapper) ReMapAll() {
|
|
|
|
logrus.Debugln("Re-applying all port mappings.")
|
|
|
|
for _, data := range pm.currentMappings {
|
|
|
|
containerIP, containerPort := getIPAndPort(data.container)
|
|
|
|
hostIP, hostPort := getIPAndPort(data.host)
|
|
|
|
if err := pm.forward(iptables.Append, data.proto, hostIP, hostPort, containerIP.String(), containerPort); err != nil {
|
|
|
|
logrus.Errorf("Error on iptables add: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 10:29:30 +00:00
|
|
|
func (pm *PortMapper) Unmap(host net.Addr) error {
|
|
|
|
pm.lock.Lock()
|
|
|
|
defer pm.lock.Unlock()
|
2014-01-28 15:42:46 -08:00
|
|
|
|
|
|
|
key := getKey(host)
|
2015-03-24 10:29:30 +00:00
|
|
|
data, exists := pm.currentMappings[key]
|
2014-01-28 15:42:46 -08:00
|
|
|
if !exists {
|
|
|
|
return ErrPortNotMapped
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:19:16 -08:00
|
|
|
if data.userlandProxy != nil {
|
|
|
|
data.userlandProxy.Stop()
|
|
|
|
}
|
2014-08-12 15:04:00 -07:00
|
|
|
|
2015-03-24 10:29:30 +00:00
|
|
|
delete(pm.currentMappings, key)
|
2014-01-28 15:42:46 -08:00
|
|
|
|
|
|
|
containerIP, containerPort := getIPAndPort(data.container)
|
|
|
|
hostIP, hostPort := getIPAndPort(data.host)
|
2015-03-24 10:29:30 +00:00
|
|
|
if err := pm.forward(iptables.Delete, data.proto, hostIP, hostPort, containerIP.String(), containerPort); err != nil {
|
2015-03-26 23:22:04 +01:00
|
|
|
logrus.Errorf("Error on iptables delete: %s", err)
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|
2014-06-19 23:33:51 +02:00
|
|
|
|
|
|
|
switch a := host.(type) {
|
|
|
|
case *net.TCPAddr:
|
2015-03-30 18:06:16 -07:00
|
|
|
return pm.Allocator.ReleasePort(a.IP, "tcp", a.Port)
|
2014-06-19 23:33:51 +02:00
|
|
|
case *net.UDPAddr:
|
2015-03-30 18:06:16 -07:00
|
|
|
return pm.Allocator.ReleasePort(a.IP, "udp", a.Port)
|
2014-06-19 23:33:51 +02:00
|
|
|
}
|
2014-01-28 15:42:46 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
|
2015-03-24 10:29:30 +00:00
|
|
|
func (pm *PortMapper) forward(action iptables.Action, proto string, sourceIP net.IP, sourcePort int, containerIP string, containerPort int) error {
|
|
|
|
if pm.chain == nil {
|
2014-01-28 15:42:46 -08:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-24 10:29:30 +00:00
|
|
|
return pm.chain.Forward(action, sourceIP, sourcePort, proto, containerIP, containerPort)
|
2014-01-28 15:42:46 -08:00
|
|
|
}
|