2017-03-13 07:46:46 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/log"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
2017-03-13 07:46:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const userChain = "DOCKER-USER"
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
var ctrl *Controller
|
2019-10-13 04:54:49 +00:00
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
func setupArrangeUserFilterRule(c *Controller) {
|
2019-10-13 04:54:49 +00:00
|
|
|
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() {
|
2022-12-28 12:21:07 +00:00
|
|
|
if ctrl == nil {
|
2023-01-14 22:27:52 +00:00
|
|
|
return
|
2017-03-13 07:46:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 12:21:07 +00:00
|
|
|
conds := []struct {
|
|
|
|
ipVer iptables.IPVersion
|
|
|
|
cond bool
|
|
|
|
}{
|
|
|
|
{ipVer: iptables.IPv4, cond: ctrl.iptablesEnabled()},
|
|
|
|
{ipVer: iptables.IPv6, cond: ctrl.ip6tablesEnabled()},
|
2023-01-14 22:27:52 +00:00
|
|
|
}
|
2022-12-28 12:21:07 +00:00
|
|
|
|
2022-12-28 12:21:07 +00:00
|
|
|
for _, ipVerCond := range conds {
|
|
|
|
cond := ipVerCond.cond
|
|
|
|
if !cond {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ipVer := ipVerCond.ipVer
|
|
|
|
iptable := iptables.GetIptable(ipVer)
|
|
|
|
_, err := iptable.NewChain(userChain, iptables.Filter, false)
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Warnf("Failed to create %s %v chain", userChain, ipVer)
|
2022-12-28 12:21:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = iptable.AddReturnRule(userChain); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Warnf("Failed to add the RETURN rule for %s %v", userChain, ipVer)
|
2022-12-28 12:21:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iptable.EnsureJumpRule("FORWARD", userChain)
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Warnf("Failed to ensure the jump rule for %s %v", userChain, ipVer)
|
2022-12-28 12:21:07 +00:00
|
|
|
}
|
2017-03-13 07:46:46 +00:00
|
|
|
}
|
|
|
|
}
|