nftables-mk.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. main() {
  3. local NFTABLES_SAVE=${2:-'/var/lib/nftables/rules-save'}
  4. case "$1" in
  5. "check")
  6. nft -c -f "${NFTABLES_SAVE}"
  7. ;;
  8. "clear")
  9. nft flush ruleset
  10. ;;
  11. "list")
  12. nft ${SAVE_OPTIONS} list ruleset
  13. ;;
  14. "load")
  15. # We use an include because cat fails with long rulesets see #675188
  16. printf 'flush ruleset\ninclude "%s"\n' "${NFTABLES_SAVE}" | nft -f -
  17. ;;
  18. "panic")
  19. panic hard | nft -f -
  20. ;;
  21. "soft_panic")
  22. panic soft | nft -f -
  23. ;;
  24. "store")
  25. local tmp_save="${NFTABLES_SAVE}.tmp"
  26. umask 177
  27. (
  28. printf '#!/sbin/nft -f\nflush ruleset\n'
  29. nft ${SAVE_OPTIONS} list ruleset
  30. ) > "$tmp_save" && mv ${tmp_save} ${NFTABLES_SAVE}
  31. ;;
  32. esac
  33. }
  34. panic() {
  35. local erule;
  36. [ "$1" = soft ] && erule="ct state established,related accept;" || erule="";
  37. cat <<EOF
  38. flush ruleset
  39. table inet filter {
  40. chain input {
  41. type filter hook input priority 0;
  42. $erule
  43. drop
  44. }
  45. chain forward {
  46. type filter hook forward priority 0;
  47. drop
  48. }
  49. chain output {
  50. type filter hook output priority 0;
  51. $erule
  52. drop
  53. }
  54. }
  55. EOF
  56. }
  57. main "$@"