ip6tables.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/sh -e
  2. #
  3. # You may redistribute this program and/or modify it under the terms of
  4. # the GNU General Public License as published by the Free Software Foundation,
  5. # either version 3 of the License, or (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. #
  15. # Simple example IPv6 Firewall configuration.
  16. # Derived from http://www.exp-networks.be/blog/ipv6-firewall/
  17. #
  18. # permits only outbound, connected and core ICMP messages on tun0
  19. # - does not filter other interfaces at all.
  20. # - Adds one rule to the INPUT chain
  21. # - Adds a new chain for tun0
  22. # - edit and change the INPUT_PORTS rules to run services.
  23. # - there is no stop facility - if you can't figure out how to reset an
  24. # ip6tables firewall, you shouldn't do so!
  25. #
  26. # Error if ip6tables is not in the path.
  27. which ip6tables
  28. # Inbound TCP ports
  29. TCP_INPUT_PORTS=""
  30. # Inbound UDP ports
  31. UDP_INPUT_PORTS=""
  32. # Allowed ICMP messages
  33. ALLOWED_ICMP="\
  34. echo-request \
  35. echo-reply \
  36. "
  37. # There is no 'assert a chain exists.
  38. ip6tables -N CJD || ip6tables -F CJD
  39. # Link the new table into the master INPUT table.
  40. ip6tables -C INPUT -i tun0 -j CJD || ip6tables -I INPUT -i tun0 -j CJD
  41. # Allow related and established connection.
  42. ip6tables -A CJD -m state --state RELATED,ESTABLISHED -j ACCEPT
  43. # Allow ICMP as defined in ALLOWED_ICMP
  44. if [ -n "$ALLOWED_ICMP" ] ; then
  45. for ICMP_TYPE in $ALLOWED_ICMP; do
  46. ip6tables -A CJD -p icmpv6 --icmpv6-type ${ICMP_TYPE} -j ACCEPT
  47. done
  48. fi
  49. # Open allowed TCP ports if any
  50. if [ -n "$TCP_INPUT_PORTS" ] ; then
  51. for PORT in $TCP_INPUT_PORTS; do
  52. ip6tables -A CJD -m state --state NEW -p tcp --dport ${PORT} \
  53. -j ACCEPT
  54. done
  55. fi
  56. # Open allowed UDP ports if any
  57. if [ -n "$UDP_INPUT_PORTS" ] ; then
  58. for PORT in $UDP_INPUT_PORTS; do
  59. ip6tables -A CJD -m state --state NEW -p udp --dport ${PORT} \
  60. -j ACCEPT
  61. done
  62. fi
  63. # Deny all other traffic on tun0
  64. ip6tables -A CJD -j LOG
  65. ip6tables -A CJD -j DROP