firewall_linux.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // This chain allow users to configure firewall policies in a way that persists
  14. // docker operations/restarts. Docker will not delete or modify any pre-existing
  15. // rules from the DOCKER-USER filter chain.
  16. // Note once DOCKER-USER chain is created, docker engine does not remove it when
  17. // IPTableForwarding is disabled, because it contains rules configured by user that
  18. // are beyond docker engine's control.
  19. func arrangeUserFilterRule() {
  20. if ctrl == nil {
  21. return
  22. }
  23. conds := []struct {
  24. ipVer iptables.IPVersion
  25. cond bool
  26. }{
  27. {ipVer: iptables.IPv4, cond: ctrl.iptablesEnabled()},
  28. {ipVer: iptables.IPv6, cond: ctrl.ip6tablesEnabled()},
  29. }
  30. for _, ipVerCond := range conds {
  31. cond := ipVerCond.cond
  32. if !cond {
  33. continue
  34. }
  35. ipVer := ipVerCond.ipVer
  36. iptable := iptables.GetIptable(ipVer)
  37. _, err := iptable.NewChain(userChain, iptables.Filter, false)
  38. if err != nil {
  39. log.G(context.TODO()).WithError(err).Warnf("Failed to create %s %v chain", userChain, ipVer)
  40. return
  41. }
  42. if err = iptable.AddReturnRule(userChain); err != nil {
  43. log.G(context.TODO()).WithError(err).Warnf("Failed to add the RETURN rule for %s %v", userChain, ipVer)
  44. return
  45. }
  46. err = iptable.EnsureJumpRule("FORWARD", userChain)
  47. if err != nil {
  48. log.G(context.TODO()).WithError(err).Warnf("Failed to ensure the jump rule for %s %v", userChain, ipVer)
  49. }
  50. }
  51. }