nftables.initd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/openrc-run
  2. # Copyright 2014-2017 Nicholas Vinson
  3. # Copyright 1999-2017 Gentoo Foundation
  4. # Distributed under the terms of the GNU General Public License v2
  5. extra_commands="clear list panic save"
  6. extra_started_commands="reload"
  7. depend() {
  8. need localmount #434774
  9. before net
  10. }
  11. start_pre() {
  12. checkkernel || return 1
  13. checkconfig || return 1
  14. return 0
  15. }
  16. clear() {
  17. /usr/lib/nftables/nftables.sh clear || return 1
  18. return 0
  19. }
  20. list() {
  21. /usr/lib/nftables/nftables.sh list || return 1
  22. return 0
  23. }
  24. panic() {
  25. checkkernel || return 1
  26. if service_started ${RC_SVCNAME}; then
  27. rc-service ${RC_SVCNAME} stop
  28. fi
  29. ebegin "Dropping all packets"
  30. clear
  31. if nft create table ip filter >/dev/null 2>&1; then
  32. nft -f /dev/stdin <<-EOF
  33. table ip filter {
  34. chain input {
  35. type filter hook input priority 0;
  36. drop
  37. }
  38. chain forward {
  39. type filter hook forward priority 0;
  40. drop
  41. }
  42. chain output {
  43. type filter hook output priority 0;
  44. drop
  45. }
  46. }
  47. EOF
  48. fi
  49. if nft create table ip6 filter >/dev/null 2>&1; then
  50. nft -f /dev/stdin <<-EOF
  51. table ip6 filter {
  52. chain input {
  53. type filter hook input priority 0;
  54. drop
  55. }
  56. chain forward {
  57. type filter hook forward priority 0;
  58. drop
  59. }
  60. chain output {
  61. type filter hook output priority 0;
  62. drop
  63. }
  64. }
  65. EOF
  66. fi
  67. }
  68. reload() {
  69. checkkernel || return 1
  70. ebegin "Flushing firewall"
  71. clear
  72. start
  73. }
  74. save() {
  75. ebegin "Saving nftables state"
  76. checkpath -q -d "$(dirname "${NFTABLES_SAVE}")"
  77. checkpath -q -m 0600 -f "${NFTABLES_SAVE}"
  78. export SAVE_OPTIONS
  79. /usr/lib/nftables/nftables.sh store ${NFTABLES_SAVE}
  80. return $?
  81. }
  82. start() {
  83. ebegin "Loading nftables state and starting firewall"
  84. clear
  85. /usr/lib/nftables/nftables.sh load ${NFTABLES_SAVE}
  86. eend $?
  87. }
  88. stop() {
  89. if yesno ${SAVE_ON_STOP:-yes}; then
  90. save || return 1
  91. fi
  92. ebegin "Stopping firewall"
  93. clear
  94. eend $?
  95. }
  96. checkconfig() {
  97. if [ ! -f ${NFTABLES_SAVE} ]; then
  98. eerror "Not starting nftables. First create some rules then run:"
  99. eerror "rc-service nftables save"
  100. return 1
  101. fi
  102. return 0
  103. }
  104. checkkernel() {
  105. if ! nft list tables >/dev/null 2>&1; then
  106. eerror "Your kernel lacks nftables support, please load"
  107. eerror "appropriate modules and try again."
  108. return 1
  109. fi
  110. return 0
  111. }