2014-11-26 11:14:50 +00:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
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 18:10:35 +00:00
|
|
|
"fmt"
|
2015-05-01 20:35:54 +00:00
|
|
|
"strings"
|
|
|
|
|
2014-11-26 11:14:50 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/godbus/dbus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPV string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Iptables IPV = "ipv4"
|
|
|
|
Ip6tables IPV = "ipv6"
|
|
|
|
Ebtables IPV = "eb"
|
|
|
|
)
|
|
|
|
const (
|
|
|
|
dbusInterface = "org.fedoraproject.FirewallD1"
|
|
|
|
dbusPath = "/org/fedoraproject/FirewallD1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Conn is a connection to firewalld dbus endpoint.
|
|
|
|
type Conn struct {
|
|
|
|
sysconn *dbus.Conn
|
|
|
|
sysobj *dbus.Object
|
|
|
|
signal chan *dbus.Signal
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
connection *Conn
|
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 18:10:35 +00:00
|
|
|
firewalldRunning bool // is Firewalld service running
|
|
|
|
onReloaded []*func() // callbacks when Firewalld has been reloaded
|
2014-11-26 11:14:50 +00:00
|
|
|
)
|
|
|
|
|
2015-04-28 12:43:04 +00:00
|
|
|
func FirewalldInit() error {
|
2014-11-26 11:14:50 +00:00
|
|
|
var err error
|
|
|
|
|
2015-04-28 12:43:04 +00:00
|
|
|
if connection, err = newConnection(); err != nil {
|
|
|
|
return fmt.Errorf("Failed to connect to D-Bus system bus: %v", err)
|
2014-11-26 11:14:50 +00:00
|
|
|
}
|
2015-05-01 20:35:54 +00:00
|
|
|
if connection != nil {
|
|
|
|
go signalHandler()
|
|
|
|
}
|
2014-11-26 11:14:50 +00:00
|
|
|
|
|
|
|
firewalldRunning = checkRunning()
|
2015-04-28 12:43:04 +00:00
|
|
|
return nil
|
2014-11-26 11:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New() establishes a connection to the system bus.
|
|
|
|
func newConnection() (*Conn, error) {
|
|
|
|
c := new(Conn)
|
|
|
|
if err := c.initConnection(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Innitialize D-Bus connection.
|
|
|
|
func (c *Conn) initConnection() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c.sysconn, err = dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This never fails, even if the service is not running atm.
|
|
|
|
c.sysobj = c.sysconn.Object(dbusInterface, dbus.ObjectPath(dbusPath))
|
|
|
|
|
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 18:10:35 +00:00
|
|
|
rule := fmt.Sprintf("type='signal',path='%s',interface='%s',sender='%s',member='Reloaded'",
|
|
|
|
dbusPath, dbusInterface, dbusInterface)
|
|
|
|
c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
|
|
|
|
|
|
|
|
rule = fmt.Sprintf("type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus',arg0='%s'",
|
|
|
|
dbusInterface)
|
|
|
|
c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
|
|
|
|
|
|
|
|
c.signal = make(chan *dbus.Signal, 10)
|
|
|
|
c.sysconn.Signal(c.signal)
|
|
|
|
|
2014-11-26 11:14:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 18:10:35 +00:00
|
|
|
func signalHandler() {
|
2015-05-01 20:35:54 +00:00
|
|
|
for signal := range connection.signal {
|
|
|
|
if strings.Contains(signal.Name, "NameOwnerChanged") {
|
|
|
|
firewalldRunning = checkRunning()
|
|
|
|
dbusConnectionChanged(signal.Body)
|
|
|
|
} else if strings.Contains(signal.Name, "Reloaded") {
|
|
|
|
reloaded()
|
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 18:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dbusConnectionChanged(args []interface{}) {
|
|
|
|
name := args[0].(string)
|
|
|
|
old_owner := args[1].(string)
|
|
|
|
new_owner := args[2].(string)
|
|
|
|
|
|
|
|
if name != dbusInterface {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(new_owner) > 0 {
|
|
|
|
connectionEstablished()
|
|
|
|
} else if len(old_owner) > 0 {
|
|
|
|
connectionLost()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionEstablished() {
|
|
|
|
reloaded()
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionLost() {
|
|
|
|
// Doesn't do anything for now. Libvirt also doesn't react to this.
|
|
|
|
}
|
|
|
|
|
|
|
|
// call all callbacks
|
|
|
|
func reloaded() {
|
|
|
|
for _, pf := range onReloaded {
|
|
|
|
(*pf)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add callback
|
|
|
|
func OnReloaded(callback func()) {
|
|
|
|
for _, pf := range onReloaded {
|
|
|
|
if pf == &callback {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onReloaded = append(onReloaded, &callback)
|
|
|
|
}
|
|
|
|
|
2014-11-26 11:14:50 +00:00
|
|
|
// Call some remote method to see whether the service is actually running.
|
|
|
|
func checkRunning() bool {
|
|
|
|
var zone string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if connection != nil {
|
|
|
|
err = connection.sysobj.Call(dbusInterface+".getDefaultZone", 0).Store(&zone)
|
|
|
|
logrus.Infof("Firewalld running: %t", err == nil)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Firewalld's passthrough method simply passes args through to iptables/ip6tables
|
|
|
|
func Passthrough(ipv IPV, args ...string) ([]byte, error) {
|
|
|
|
var output string
|
|
|
|
logrus.Debugf("Firewalld passthrough: %s, %s", ipv, args)
|
2015-04-28 12:43:04 +00:00
|
|
|
if err := connection.sysobj.Call(dbusInterface+".direct.passthrough", 0, ipv, args).Store(&output); err != nil {
|
|
|
|
return nil, err
|
2014-11-26 11:14:50 +00:00
|
|
|
}
|
2015-04-28 12:43:04 +00:00
|
|
|
return []byte(output), nil
|
2014-11-26 11:14:50 +00:00
|
|
|
}
|