2017-03-13 07:46:46 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2023-07-16 14:21:23 +00:00
|
|
|
"fmt"
|
2023-06-23 00:33:17 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/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
|
|
|
}
|
|
|
|
|
2023-07-16 12:16:26 +00:00
|
|
|
// arrangeUserFilterRule sets up the DOCKER-USER chain for each iptables version
|
|
|
|
// (IPv4, IPv6) that's enabled in the controller's configuration.
|
2023-07-16 14:21:23 +00:00
|
|
|
func arrangeUserFilterRule() {
|
|
|
|
if ctrl == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, ipVersion := range ctrl.enabledIptablesVersions() {
|
|
|
|
if err := setupUserChain(ipVersion); err != nil {
|
|
|
|
log.G(context.TODO()).WithError(err).Warn("arrangeUserFilterRule")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupUserChain sets up the DOCKER-USER chain for the given [iptables.IPVersion].
|
2023-07-16 12:16:26 +00:00
|
|
|
//
|
|
|
|
// This chain allows users to configure firewall policies in a way that
|
|
|
|
// persist daemon operations/restarts. The daemon does not delete or modify
|
|
|
|
// any pre-existing rules from the DOCKER-USER filter chain.
|
|
|
|
//
|
|
|
|
// Once the DOCKER-USER chain is created, the daemon does not remove it when
|
|
|
|
// IPTableForwarding is disabled, because it contains rules configured by user
|
|
|
|
// that are beyond the daemon's control.
|
2023-07-16 14:21:23 +00:00
|
|
|
func setupUserChain(ipVersion iptables.IPVersion) error {
|
|
|
|
ipt := iptables.GetIptable(ipVersion)
|
|
|
|
if _, err := ipt.NewChain(userChain, iptables.Filter, false); err != nil {
|
|
|
|
return fmt.Errorf("failed to create %s %v chain: %v", userChain, ipVersion, err)
|
2017-03-13 07:46:46 +00:00
|
|
|
}
|
2023-07-16 14:21:23 +00:00
|
|
|
if err := ipt.AddReturnRule(userChain); err != nil {
|
|
|
|
return fmt.Errorf("failed to add the RETURN rule for %s %v: %w", userChain, ipVersion, err)
|
|
|
|
}
|
|
|
|
if err := ipt.EnsureJumpRule("FORWARD", userChain); err != nil {
|
|
|
|
return fmt.Errorf("failed to ensure the jump rule for %s %v: %w", userChain, ipVersion, err)
|
2017-03-13 07:46:46 +00:00
|
|
|
}
|
2023-07-16 14:21:23 +00:00
|
|
|
return nil
|
2017-03-13 07:46:46 +00:00
|
|
|
}
|