2017-03-13 03:46:46 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-03-13 03:46:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const userChain = "DOCKER-USER"
|
|
|
|
|
2022-07-04 04:12:20 -04:00
|
|
|
var ctrl *controller
|
2019-10-13 00:54:49 -04:00
|
|
|
|
|
|
|
func setupArrangeUserFilterRule(c *controller) {
|
|
|
|
ctrl = c
|
|
|
|
iptables.OnReloaded(arrangeUserFilterRule)
|
2018-01-12 10:29:09 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 03:46:46 -04: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 00:54:49 -04: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 03:46:46 -04:00
|
|
|
func arrangeUserFilterRule() {
|
2019-10-13 00:54:49 -04:00
|
|
|
if ctrl == nil || !ctrl.iptablesEnabled() {
|
|
|
|
return
|
|
|
|
}
|
2020-07-23 10:52:40 -04:00
|
|
|
// TODO IPv6 support
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
_, err := iptable.NewChain(userChain, iptables.Filter, false)
|
2017-03-13 03:46:46 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to create %s chain: %v", userChain, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
if err = iptable.AddReturnRule(userChain); err != nil {
|
2017-03-13 03:46:46 -04:00
|
|
|
logrus.Warnf("Failed to add the RETURN rule for %s: %v", userChain, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
err = iptable.EnsureJumpRule("FORWARD", userChain)
|
2017-03-13 03:46:46 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to ensure the jump rule for %s: %v", userChain, err)
|
|
|
|
}
|
|
|
|
}
|