2017-03-13 07:46:46 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
2017-07-26 21:18:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-03-13 07:46:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const userChain = "DOCKER-USER"
|
|
|
|
|
2019-10-13 04:54:49 +00:00
|
|
|
var (
|
|
|
|
ctrl *controller = nil
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupArrangeUserFilterRule(c *controller) {
|
|
|
|
ctrl = c
|
|
|
|
iptables.OnReloaded(arrangeUserFilterRule)
|
2018-01-12 15:29:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 07:46:46 +00:00
|
|
|
// This chain allow users to configure firewall policies in a way that persists
|
|
|
|
// docker operations/restarts. Docker will not delete or modify any pre-existing
|
|
|
|
// rules from the DOCKER-USER filter chain.
|
2019-10-13 04:54:49 +00:00
|
|
|
// Note once DOCKER-USER chain is created, docker engine does not remove it when
|
|
|
|
// IPTableForwarding is disabled, because it contains rules configured by user that
|
|
|
|
// are beyond docker engine's control.
|
2017-03-13 07:46:46 +00:00
|
|
|
func arrangeUserFilterRule() {
|
2019-10-13 04:54:49 +00:00
|
|
|
if ctrl == nil || !ctrl.iptablesEnabled() {
|
|
|
|
return
|
|
|
|
}
|
2020-07-23 14:52:40 +00:00
|
|
|
// TODO IPv6 support
|
2017-11-28 21:15:55 +00:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
_, err := iptable.NewChain(userChain, iptables.Filter, false)
|
2017-03-13 07:46:46 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to create %s chain: %v", userChain, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:15:55 +00:00
|
|
|
if err = iptable.AddReturnRule(userChain); err != nil {
|
2017-03-13 07:46:46 +00:00
|
|
|
logrus.Warnf("Failed to add the RETURN rule for %s: %v", userChain, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:15:55 +00:00
|
|
|
err = iptable.EnsureJumpRule("FORWARD", userChain)
|
2017-03-13 07:46:46 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to ensure the jump rule for %s: %v", userChain, err)
|
|
|
|
}
|
|
|
|
}
|