firewall_linux.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package libnetwork
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/containerd/log"
  6. "github.com/docker/docker/libnetwork/iptables"
  7. )
  8. const userChain = "DOCKER-USER"
  9. var ctrl *Controller
  10. func setupArrangeUserFilterRule(c *Controller) {
  11. ctrl = c
  12. iptables.OnReloaded(arrangeUserFilterRule)
  13. }
  14. // arrangeUserFilterRule sets up the DOCKER-USER chain for each iptables version
  15. // (IPv4, IPv6) that's enabled in the controller's configuration.
  16. func arrangeUserFilterRule() {
  17. if ctrl == nil {
  18. return
  19. }
  20. for _, ipVersion := range ctrl.enabledIptablesVersions() {
  21. if err := setupUserChain(ipVersion); err != nil {
  22. log.G(context.TODO()).WithError(err).Warn("arrangeUserFilterRule")
  23. }
  24. }
  25. }
  26. // setupUserChain sets up the DOCKER-USER chain for the given [iptables.IPVersion].
  27. //
  28. // This chain allows users to configure firewall policies in a way that
  29. // persist daemon operations/restarts. The daemon does not delete or modify
  30. // any pre-existing rules from the DOCKER-USER filter chain.
  31. //
  32. // Once the DOCKER-USER chain is created, the daemon does not remove it when
  33. // IPTableForwarding is disabled, because it contains rules configured by user
  34. // that are beyond the daemon's control.
  35. func setupUserChain(ipVersion iptables.IPVersion) error {
  36. ipt := iptables.GetIptable(ipVersion)
  37. if _, err := ipt.NewChain(userChain, iptables.Filter, false); err != nil {
  38. return fmt.Errorf("failed to create %s %v chain: %v", userChain, ipVersion, err)
  39. }
  40. if err := ipt.AddReturnRule(userChain); err != nil {
  41. return fmt.Errorf("failed to add the RETURN rule for %s %v: %w", userChain, ipVersion, err)
  42. }
  43. if err := ipt.EnsureJumpRule("FORWARD", userChain); err != nil {
  44. return fmt.Errorf("failed to ensure the jump rule for %s %v: %w", userChain, ipVersion, err)
  45. }
  46. return nil
  47. }