nftables.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #! /bin/sh
  2. main() {
  3. local NFTABLES_SAVE=${2:-'/var/lib/nftables/rules-save'}
  4. local retval
  5. case "$1" in
  6. "clear")
  7. if ! use_legacy; then
  8. nft flush ruleset
  9. else
  10. clear_legacy
  11. fi
  12. retval=$?
  13. ;;
  14. "list")
  15. if ! use_legacy; then
  16. nft list ruleset
  17. else
  18. list_legacy
  19. fi
  20. retval=$?
  21. ;;
  22. "load")
  23. nft -f ${NFTABLES_SAVE}
  24. retval=$?
  25. ;;
  26. "store")
  27. umask 177
  28. local tmp_save="${NFTABLES_SAVE}.tmp"
  29. if ! use_legacy; then
  30. nft ${SAVE_OPTIONS} list ruleset > ${tmp_save}
  31. else
  32. save_legacy ${tmp_save}
  33. fi
  34. retval=$?
  35. if [ ${retval} ]; then
  36. mv ${tmp_save} ${NFTABLES_SAVE}
  37. fi
  38. ;;
  39. esac
  40. return ${retval}
  41. }
  42. clear_legacy() {
  43. local l3f line table chain first_line
  44. first_line=1
  45. if manualwalk; then
  46. for l3f in $(getfamilies); do
  47. nft list tables ${l3f} | while read line; do
  48. table=$(echo ${line} | sed "s/table[ \t]*//")
  49. deletetable ${l3f} ${table}
  50. done
  51. done
  52. else
  53. nft list tables | while read line; do
  54. l3f=$(echo ${line} | cut -d ' ' -f2)
  55. table=$(echo ${line} | cut -d ' ' -f3)
  56. deletetable ${l3f} ${table}
  57. done
  58. fi
  59. }
  60. list_legacy() {
  61. local l3f
  62. if manualwalk; then
  63. for l3f in $(getfamilies); do
  64. nft list tables ${l3f} | while read line; do
  65. line=$(echo ${line} | sed "s/table/table ${l3f}/")
  66. echo "$(nft list ${line})"
  67. done
  68. done
  69. else
  70. nft list tables | while read line; do
  71. echo "$(nft list ${line})"
  72. done
  73. fi
  74. }
  75. save_legacy() {
  76. tmp_save=$1
  77. touch "${tmp_save}"
  78. if manualwalk; then
  79. for l3f in $(getfamilies); do
  80. nft list tables ${l3f} | while read line; do
  81. line=$(echo ${line} | sed "s/table/table ${l3f}/")
  82. nft ${SAVE_OPTIONS} list ${line} >> ${tmp_save}
  83. done
  84. done
  85. else
  86. nft list tables | while read line; do
  87. nft ${SAVE_OPTIONS} list ${line} >> "${tmp_save}"
  88. done
  89. fi
  90. }
  91. use_legacy() {
  92. local major_ver minor_ver
  93. major_ver=$(uname -r | cut -d '.' -f1)
  94. minor_ver=$(uname -r | cut -d '.' -f2)
  95. [ $major_ver -ge 4 -o $major_ver -eq 3 -a $minor_ver -ge 18 ] && return 1
  96. return 0
  97. }
  98. CHECK_TABLE_NAME="GENTOO_CHECK_TABLE"
  99. getfamilies() {
  100. local l3f families
  101. for l3f in ip arp ip6 bridge inet; do
  102. if nft create table ${l3f} ${CHECK_TABLE_NAME} > /dev/null 2>&1; then
  103. families="${families}${l3f} "
  104. nft delete table ${l3f} ${CHECK_TABLE_NAME}
  105. fi
  106. done
  107. echo ${families}
  108. }
  109. manualwalk() {
  110. local result l3f=`getfamilies | cut -d ' ' -f1`
  111. nft create table ${l3f} ${CHECK_TABLE_NAME}
  112. nft list tables | read line
  113. if [ $(echo $line | wc -w) -lt 3 ]; then
  114. result=0
  115. fi
  116. result=1
  117. nft delete table ${l3f} ${CHECK_TABLE_NAME}
  118. return $result
  119. }
  120. deletetable() {
  121. # family is $1
  122. # table name is $2
  123. nft flush table $1 $2
  124. nft list table $1 $2 | while read l; do
  125. chain=$(echo $l | grep -o 'chain [^[:space:]]\+' | cut -d ' ' -f2)
  126. if [ -n "${chain}" ]; then
  127. nft flush chain $1 $2 ${chain}
  128. nft delete chain $1 $2 ${chain}
  129. fi
  130. done
  131. nft delete table $1 $2
  132. }
  133. main "$@"