firewall_linux.go 1.5 KB

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