firewall_linux.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package libnetwork
  2. import (
  3. "github.com/docker/libnetwork/iptables"
  4. "github.com/sirupsen/logrus"
  5. )
  6. const userChain = "DOCKER-USER"
  7. var (
  8. ctrl *controller = nil
  9. )
  10. func setupArrangeUserFilterRule(c *controller) {
  11. ctrl = c
  12. iptables.OnReloaded(arrangeUserFilterRule)
  13. }
  14. // This chain allow users to configure firewall policies in a way that persists
  15. // docker operations/restarts. Docker will not delete or modify any pre-existing
  16. // rules from the DOCKER-USER filter chain.
  17. // Note once DOCKER-USER chain is created, docker engine does not remove it when
  18. // IPTableForwarding is disabled, because it contains rules configured by user that
  19. // are beyond docker engine's control.
  20. func arrangeUserFilterRule() {
  21. if ctrl == nil || !ctrl.iptablesEnabled() {
  22. return
  23. }
  24. // TODO IPv6 support
  25. iptable := iptables.GetIptable(iptables.IPv4)
  26. _, err := iptable.NewChain(userChain, iptables.Filter, false)
  27. if err != nil {
  28. logrus.Warnf("Failed to create %s chain: %v", userChain, err)
  29. return
  30. }
  31. if err = iptable.AddReturnRule(userChain); err != nil {
  32. logrus.Warnf("Failed to add the RETURN rule for %s: %v", userChain, err)
  33. return
  34. }
  35. err = iptable.EnsureJumpRule("FORWARD", userChain)
  36. if err != nil {
  37. logrus.Warnf("Failed to ensure the jump rule for %s: %v", userChain, err)
  38. }
  39. }