nftables-mk.initd 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2019 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="check clear list panic save soft_panic"
  5. extra_started_commands="reload"
  6. depend() {
  7. need localmount #434774
  8. before net
  9. }
  10. checkkernel() {
  11. if ! /sbin/nft list ruleset >/dev/null 2>/dev/null ; then
  12. eerror "Your kernel lacks nftables support, please load"
  13. eerror "appropriate modules and try again."
  14. return 1
  15. fi
  16. return 0
  17. }
  18. checkconfig() {
  19. if [ -z "${NFTABLES_SAVE}" -o ! -f "${NFTABLES_SAVE}" ] ; then
  20. eerror "Not starting nftables. First create some rules then run:"
  21. eerror "/etc/init.d/${SVCNAME} save"
  22. return 1
  23. fi
  24. return 0
  25. }
  26. start_pre() {
  27. checkconfig || return 1
  28. checkkernel || return 1
  29. check || return 1
  30. }
  31. start() {
  32. ebegin "Loading ${SVCNAME} state and starting firewall"
  33. /usr/lib/nftables/nftables.sh load "${NFTABLES_SAVE}"
  34. eend $?
  35. }
  36. stop() {
  37. if [ "${SAVE_ON_STOP}" = "yes" ] ; then
  38. save || return 1
  39. fi
  40. ebegin "Stopping firewall"
  41. if [ "${PANIC_ON_STOP}" = "hard" ]; then
  42. /usr/lib/nftables/nftables.sh panic
  43. elif [ "${PANIC_ON_STOP}" = "soft" ]; then
  44. /usr/lib/nftables/nftables.sh soft_panic
  45. else
  46. /usr/lib/nftables/nftables.sh clear
  47. fi
  48. eend $?
  49. }
  50. reload() {
  51. start_pre || return 1
  52. start
  53. }
  54. clear() {
  55. ebegin "Clearing rules"
  56. /usr/lib/nftables/nftables.sh clear
  57. eend $?
  58. }
  59. list() {
  60. /usr/lib/nftables/nftables.sh list
  61. }
  62. check() {
  63. ebegin "Checking rules"
  64. /usr/lib/nftables/nftables.sh check "${NFTABLES_SAVE}"
  65. eend $?
  66. }
  67. save() {
  68. ebegin "Saving ${SVCNAME} state"
  69. checkpath -q -d "$(dirname "${NFTABLES_SAVE}")"
  70. checkpath -q -m 0600 -f "${NFTABLES_SAVE}"
  71. /usr/lib/nftables/nftables.sh store "${NFTABLES_SAVE}"
  72. eend $?
  73. }
  74. panic() {
  75. if service_started ${SVCNAME}; then
  76. rc-service ${SVCNAME} zap
  77. fi
  78. ebegin "Dropping all packets"
  79. /usr/lib/nftables/nftables.sh panic
  80. eend $?
  81. }
  82. soft_panic() {
  83. if service_started ${SVCNAME}; then
  84. rc-service ${SVCNAME} zap
  85. fi
  86. ebegin "Dropping new connections"
  87. /usr/lib/nftables/nftables.sh soft_panic
  88. eend $?
  89. }