iptables.initd 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/sbin/openrc-run
  2. # Copyright 1999-2018 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="check save panic"
  5. extra_started_commands="reload"
  6. iptables_lock_wait_time=${IPTABLES_LOCK_WAIT_TIME:-"60"}
  7. iptables_lock_wait_interval=${IPTABLES_LOCK_WAIT_INTERVAL:-"1000"}
  8. iptables_name=${SVCNAME}
  9. case ${iptables_name} in
  10. iptables|ip6tables) ;;
  11. *) iptables_name="iptables" ;;
  12. esac
  13. iptables_bin="/sbin/${iptables_name}"
  14. case ${iptables_name} in
  15. iptables) iptables_proc="/proc/net/ip_tables_names"
  16. iptables_save=${IPTABLES_SAVE};;
  17. ip6tables) iptables_proc="/proc/net/ip6_tables_names"
  18. iptables_save=${IP6TABLES_SAVE};;
  19. esac
  20. depend() {
  21. need localmount #434774
  22. before net
  23. }
  24. set_table_policy() {
  25. local has_errors=0 chains table=$1 policy=$2
  26. case ${table} in
  27. nat) chains="PREROUTING POSTROUTING OUTPUT";;
  28. mangle) chains="PREROUTING INPUT FORWARD OUTPUT POSTROUTING";;
  29. filter) chains="INPUT FORWARD OUTPUT";;
  30. *) chains="";;
  31. esac
  32. local chain
  33. for chain in ${chains} ; do
  34. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -t ${table} -P ${chain} ${policy}
  35. [ $? -ne 0 ] && has_errors=1
  36. done
  37. return ${has_errors}
  38. }
  39. checkkernel() {
  40. if [ ! -e ${iptables_proc} ] ; then
  41. eerror "Your kernel lacks ${iptables_name} support, please load"
  42. eerror "appropriate modules and try again."
  43. return 1
  44. fi
  45. return 0
  46. }
  47. checkconfig() {
  48. if [ -z "${iptables_save}" -o ! -f "${iptables_save}" ] ; then
  49. eerror "Not starting ${iptables_name}. First create some rules then run:"
  50. eerror "/etc/init.d/${iptables_name} save"
  51. return 1
  52. fi
  53. return 0
  54. }
  55. start_pre() {
  56. checkconfig || return 1
  57. }
  58. start() {
  59. ebegin "Loading ${iptables_name} state and starting firewall"
  60. ${iptables_bin}-restore --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
  61. eend $?
  62. }
  63. stop_pre() {
  64. checkkernel || return 1
  65. }
  66. stop() {
  67. if [ "${SAVE_ON_STOP}" = "yes" ] ; then
  68. save || return 1
  69. fi
  70. ebegin "Stopping firewall"
  71. local has_errors=0 a
  72. for a in $(cat ${iptables_proc}) ; do
  73. set_table_policy $a ACCEPT
  74. [ $? -ne 0 ] && has_errors=1
  75. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
  76. [ $? -ne 0 ] && has_errors=1
  77. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
  78. [ $? -ne 0 ] && has_errors=1
  79. done
  80. eend ${has_errors}
  81. }
  82. reload() {
  83. checkkernel || return 1
  84. checkrules || return 1
  85. ebegin "Flushing firewall"
  86. local has_errors=0 a
  87. for a in $(cat ${iptables_proc}) ; do
  88. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
  89. [ $? -ne 0 ] && has_errors=1
  90. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
  91. [ $? -ne 0 ] && has_errors=1
  92. done
  93. eend ${has_errors}
  94. start
  95. }
  96. checkrules() {
  97. ebegin "Checking rules"
  98. ${iptables_bin}-restore --test ${SAVE_RESTORE_OPTIONS} < "${iptables_save}"
  99. eend $?
  100. }
  101. check() {
  102. # Short name for users of init.d script.
  103. checkrules
  104. }
  105. save() {
  106. ebegin "Saving ${iptables_name} state"
  107. checkpath -q -d "$(dirname "${iptables_save}")"
  108. checkpath -q -m 0600 -f "${iptables_save}"
  109. ${iptables_bin}-save ${SAVE_RESTORE_OPTIONS} > "${iptables_save}"
  110. eend $?
  111. }
  112. panic() {
  113. # use iptables autoload capability to load at least all required
  114. # modules and filter table
  115. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -S >/dev/null
  116. if [ $? -ne 0 ] ; then
  117. eerror "${iptables_bin} failed to load"
  118. return 1
  119. fi
  120. if service_started ${iptables_name}; then
  121. rc-service ${iptables_name} stop
  122. fi
  123. local has_errors=0 a
  124. ebegin "Dropping all packets"
  125. for a in $(cat ${iptables_proc}) ; do
  126. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -F -t $a
  127. [ $? -ne 0 ] && has_errors=1
  128. ${iptables_bin} --wait ${iptables_lock_wait_time} --wait-interval ${iptables_lock_wait_interval} -X -t $a
  129. [ $? -ne 0 ] && has_errors=1
  130. if [ "${a}" != "nat" ]; then
  131. # The "nat" table is not intended for filtering, the use of DROP is therefore inhibited.
  132. set_table_policy $a DROP
  133. [ $? -ne 0 ] && has_errors=1
  134. fi
  135. done
  136. eend ${has_errors}
  137. }