iptables-rules 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/bin/sh
  2. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  3. NAME=iptables-rules
  4. DESC="iptables filter rules"
  5. iptables_bin="/usr/sbin/iptables"
  6. ip6tables_bin="/usr/sbin/ip6tables"
  7. die()
  8. {
  9. echo "$*"
  10. exit 1
  11. }
  12. iptables()
  13. {
  14. $iptables_bin "$@" || die "FAILED: $iptables_bin $@"
  15. }
  16. ip6tables()
  17. {
  18. $ip6tables_bin "$@" || die "FAILED: $ip6tables_bin $@"
  19. }
  20. clear_ip4tables()
  21. {
  22. # clear and delete all chains
  23. iptables -F
  24. iptables -X
  25. iptables -Z
  26. # delete `nat' and `mangle' chains
  27. iptables -t mangle -F
  28. iptables -t nat -F
  29. }
  30. clear_ip6tables()
  31. {
  32. # clear and delete all chains
  33. ip6tables -F INPUT
  34. ip6tables -F OUTPUT
  35. ip6tables -F FORWARD
  36. ip6tables -F
  37. ip6tables -X
  38. ip6tables -Z
  39. # delete `mangle' chain
  40. ip6tables -t mangle -F
  41. }
  42. clear_all_ipt_rules()
  43. {
  44. clear_ip4tables
  45. clear_ip6tables
  46. }
  47. setup_ip4tables()
  48. {
  49. # default policy
  50. iptables -P INPUT DROP
  51. iptables -P FORWARD DROP
  52. iptables -P OUTPUT DROP
  53. # loopback
  54. iptables -A INPUT -i lo -j ACCEPT
  55. iptables -A OUTPUT -o lo -j ACCEPT
  56. # UDP
  57. iptables -A INPUT -p udp -j ACCEPT # Accept everything
  58. # Established connections
  59. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  60. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  61. if true; then
  62. # forwarding and masquerading
  63. echo 1 >/proc/sys/net/ipv4/ip_forward
  64. for inif in eth+ usb+; do
  65. for outif in wlan+ ppp+ tun+; do
  66. iptables -t nat -A POSTROUTING -o $outif -j MASQUERADE
  67. iptables -A FORWARD -i $inif -o $outif -j ACCEPT
  68. iptables -A FORWARD -i $outif -o $inif -j ACCEPT
  69. done
  70. done
  71. fi
  72. # Output
  73. iptables -A OUTPUT -j ACCEPT
  74. # ICMP
  75. iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
  76. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  77. iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
  78. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  79. iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
  80. iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
  81. iptables -A INPUT -p icmp --icmp-type source-quench -j ACCEPT
  82. iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
  83. iptables -A INPUT -p icmp --icmp-type parameter-problem -j ACCEPT
  84. # logging
  85. # iptables -A INPUT -j LOG --log-level info --log-prefix "reject_ipv4_input:"
  86. # iptables -A OUTPUT -j LOG --log-level info --log-prefix "reject_ipv4_output:"
  87. # iptables -A FORWARD -j LOG --log-level info --log-prefix "reject_ipv4_forward:"
  88. # REJECT the rest
  89. iptables -A INPUT -j REJECT
  90. iptables -A OUTPUT -j REJECT
  91. iptables -A FORWARD -j REJECT
  92. }
  93. setup_ip6tables()
  94. {
  95. # default policy
  96. ip6tables -P INPUT DROP
  97. ip6tables -P FORWARD DROP
  98. ip6tables -P OUTPUT DROP
  99. # Disable processing of any RH0 packet
  100. # Which could allow a ping-pong of packets
  101. ip6tables -A INPUT -m rt --rt-type 0 -j DROP
  102. ip6tables -A OUTPUT -m rt --rt-type 0 -j DROP
  103. ip6tables -A FORWARD -m rt --rt-type 0 -j DROP
  104. # loopback
  105. ip6tables -A INPUT -i lo -j ACCEPT
  106. ip6tables -A OUTPUT -o lo -j ACCEPT
  107. # Allow Link-Local addresses
  108. # ip6tables -A INPUT -s fe80::/10 -j ACCEPT
  109. # ip6tables -A OUTPUT -s fe80::/10 -j ACCEPT
  110. # Allow multicast
  111. # ip6tables -A INPUT -s ff00::/8 -j ACCEPT
  112. # ip6tables -A OUTPUT -s ff00::/8 -j ACCEPT
  113. # Established connections
  114. ip6tables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  115. ip6tables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  116. # Output
  117. ip6tables -A OUTPUT -j ACCEPT
  118. # ICMP
  119. ip6tables -A INPUT -p icmpv6 -j ACCEPT
  120. ip6tables -A OUTPUT -p icmpv6 -j ACCEPT
  121. ip6tables -A FORWARD -p icmpv6 -j ACCEPT
  122. # logging
  123. # ip6tables -A INPUT -j LOG --log-level info --log-prefix "reject_ipv6_input:"
  124. # ip6tables -A OUTPUT -j LOG --log-level info --log-prefix "reject_ipv6_output:"
  125. # ip6tables -A FORWARD -j LOG --log-level info --log-prefix "reject_ipv6_forward:"
  126. # REJECT the rest
  127. ip6tables -A INPUT -j REJECT
  128. ip6tables -A OUTPUT -j REJECT
  129. ip6tables -A FORWARD -j REJECT
  130. }
  131. setup_iptables()
  132. {
  133. clear_all_ipt_rules
  134. setup_ip4tables
  135. setup_ip6tables
  136. }
  137. reset_iptables()
  138. {
  139. clear_all_ipt_rules
  140. iptables -P INPUT ACCEPT
  141. iptables -P FORWARD ACCEPT
  142. iptables -P OUTPUT ACCEPT
  143. ip6tables -P INPUT ACCEPT
  144. ip6tables -P FORWARD ACCEPT
  145. ip6tables -P OUTPUT ACCEPT
  146. }
  147. sanity_checks()
  148. {
  149. [ $(id -u) -eq 0 ] || die "Permission denied"
  150. [ -x "$iptables_bin" ] || die "Can not execute iptables binary \"$iptables_bin\""
  151. [ -x "$ip6tables_bin" ] || die "Can not execute ip6tables binary \"$ip6tables_bin\""
  152. }
  153. sanity_checks
  154. case "$1" in
  155. start|restart|reload)
  156. echo "Starting $DESC: $NAME"
  157. setup_iptables
  158. ;;
  159. stop)
  160. echo "Stopping $DESC: $NAME"
  161. reset_iptables
  162. ;;
  163. *)
  164. echo "Usage: $0 {start|stop|restart|reload}"
  165. exit 1
  166. ;;
  167. esac
  168. exit 0