ipset_atomic.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package firewall
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "net"
  6. "os/exec"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. )
  11. var setApplyMu sync.Mutex
  12. var setDigests = map[string][32]byte{}
  13. func runIPSet(args ...string) error {
  14. out, err := exec.Command("ipset", args...).CombinedOutput()
  15. if err != nil {
  16. return fmt.Errorf("ipset %v: %s: %w", args, out, err)
  17. }
  18. return nil
  19. }
  20. func replaceSet(name, family string, addresses []string) error {
  21. setApplyMu.Lock()
  22. defer setApplyMu.Unlock()
  23. if len(name) > 31 {
  24. return fmt.Errorf("address list name too long")
  25. }
  26. unique := map[string]bool{}
  27. var clean []string
  28. for _, address := range addresses {
  29. ip := net.ParseIP(address)
  30. if ip != nil {
  31. address = ip.String()
  32. }
  33. if ip == nil {
  34. var network *net.IPNet
  35. var err error
  36. ip, network, err = net.ParseCIDR(address)
  37. if err == nil {
  38. address = network.String()
  39. }
  40. }
  41. if ip == nil || (ip.To4() != nil) != (family == "inet") {
  42. return fmt.Errorf("invalid %s address %q", family, address)
  43. }
  44. if !unique[address] {
  45. unique[address] = true
  46. clean = append(clean, address)
  47. }
  48. }
  49. digest := sha256.Sum256([]byte(family + "\n" + strings.Join(clean, "\n")))
  50. header, inspectErr := exec.Command("ipset", "list", name, "-terse").CombinedOutput()
  51. exists := inspectErr == nil
  52. if inspectErr != nil && !strings.Contains(string(header), "does not exist") {
  53. return fmt.Errorf("cannot inspect ipset %s: %s: %w", name, header, inspectErr)
  54. }
  55. if exists && (!strings.Contains(string(header), "Type: hash:net\n") || !strings.Contains(string(header), "family "+family+" ")) {
  56. return fmt.Errorf("set %s has an incompatible type/family; existing data was preserved", name)
  57. }
  58. if exists && setDigests[name] == digest && strings.Contains(string(header), "Number of entries: "+strconv.Itoa(len(clean))+"\n") {
  59. return nil
  60. }
  61. h := sha256.Sum256([]byte(name))
  62. temp := fmt.Sprintf("dt_tmp_%x", h[:8])
  63. // A previous version may have left a temporary set with different limits.
  64. _ = exec.Command("ipset", "destroy", temp).Run()
  65. var restore strings.Builder
  66. fmt.Fprintf(&restore, "create %s hash:net family %s maxelem 1048576\n", temp, family)
  67. for _, address := range clean {
  68. fmt.Fprintf(&restore, "add %s %s -exist\n", temp, address)
  69. }
  70. if !exists {
  71. fmt.Fprintf(&restore, "create %s hash:net family %s maxelem 1048576\n", name, family)
  72. }
  73. fmt.Fprintf(&restore, "swap %s %s\ndestroy %s\n", temp, name, temp)
  74. cmd := exec.Command("ipset", "restore")
  75. cmd.Stdin = strings.NewReader(restore.String())
  76. if out, err := cmd.CombinedOutput(); err != nil {
  77. _ = exec.Command("ipset", "destroy", temp).Run()
  78. return fmt.Errorf("ipset bulk restore %s: %s: %w", name, out, err)
  79. }
  80. setDigests[name] = digest
  81. return nil
  82. }