ebtables.initd 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/sbin/openrc-run
  2. # Copyright 1999-2020 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="save panic"
  5. extra_started_commands="reload"
  6. ebtables_bin="/sbin/ebtables"
  7. ebtables_save=${EBTABLES_SAVE}
  8. depend() {
  9. before net
  10. use logger
  11. }
  12. ebtables_tables() {
  13. for table in filter nat broute; do
  14. if ${ebtables_bin} -t ${table} -L > /dev/null 2>&1; then
  15. printf '%s' "${table} "
  16. fi
  17. done
  18. }
  19. set_table_policy() {
  20. local chains table=$1 policy=$2
  21. case ${table} in
  22. nat) chains="PREROUTING POSTROUTING OUTPUT";;
  23. broute) chains="BROUTING";;
  24. filter) chains="INPUT FORWARD OUTPUT";;
  25. *) chains="";;
  26. esac
  27. local chain
  28. for chain in ${chains} ; do
  29. ${ebtables_bin} -t ${table} -P ${chain} ${policy}
  30. done
  31. }
  32. checkconfig() {
  33. if [ ! -f ${ebtables_save} ] ; then
  34. eerror "Not starting ebtables. First create some rules then run:"
  35. eerror "/etc/init.d/ebtables save"
  36. return 1
  37. fi
  38. return 0
  39. }
  40. start() {
  41. checkconfig || return 1
  42. ebegin "Loading ebtables state and starting bridge firewall"
  43. ${ebtables_bin}-restore ${SAVE_RESTORE_OPTIONS} < "${ebtables_save}"
  44. eend $?
  45. }
  46. stop() {
  47. if [ "${SAVE_ON_STOP}" = "yes" ] ; then
  48. save || return 1
  49. fi
  50. ebegin "Stopping bridge firewall"
  51. local a
  52. for a in $(ebtables_tables); do
  53. set_table_policy $a ACCEPT
  54. ${ebtables_bin} -t $a -F
  55. ${ebtables_bin} -t $a -X
  56. done
  57. eend $?
  58. }
  59. reload() {
  60. ebegin "Flushing bridge firewall"
  61. local a
  62. for a in $(ebtables_tables); do
  63. ${ebtables_bin} -t $a -F
  64. ${ebtables_bin} -t $a -X
  65. done
  66. eend $?
  67. start
  68. }
  69. save() {
  70. ebegin "Saving ebtables state"
  71. touch "${ebtables_save}"
  72. chmod 0600 "${ebtables_save}"
  73. ${ebtables_bin}-save $(ebtables_tables) ${SAVE_RESTORE_OPTIONS} > "${ebtables_save}"
  74. eend $?
  75. }
  76. panic() {
  77. service_started ebtables && svc_stop
  78. local a
  79. ebegin "Dropping all packets forwarded on bridges"
  80. for a in $(ebtables_tables); do
  81. ${ebtables_bin} -t $a -F
  82. ${ebtables_bin} -t $a -X
  83. set_table_policy $a DROP
  84. done
  85. eend $?
  86. }