firewall_linux.go 1.6 KB

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