firewall.init 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006 OpenWrt.org
  3. ## Please make changes in /etc/firewall.user
  4. START=45
  5. start() {
  6. include /lib/network
  7. scan_interfaces
  8. config_load /var/state/network
  9. config_get WAN wan ifname
  10. config_get WANDEV wan device
  11. config_get LAN lan ifname
  12. ## CLEAR TABLES
  13. for T in filter nat; do
  14. iptables -t $T -F
  15. iptables -t $T -X
  16. done
  17. iptables -N input_rule
  18. iptables -N input_wan
  19. iptables -N output_rule
  20. iptables -N forwarding_rule
  21. iptables -N forwarding_wan
  22. iptables -t nat -N NEW
  23. iptables -t nat -N prerouting_rule
  24. iptables -t nat -N prerouting_wan
  25. iptables -t nat -N postrouting_rule
  26. iptables -N LAN_ACCEPT
  27. [ -z "$WAN" ] || iptables -A LAN_ACCEPT -i "$WAN" -j RETURN
  28. [ -z "$WANDEV" -o "$WANDEV" = "$WAN" ] || iptables -A LAN_ACCEPT -i "$WANDEV" -j RETURN
  29. iptables -A LAN_ACCEPT -j ACCEPT
  30. ### INPUT
  31. ### (connections with the router as destination)
  32. # base case
  33. iptables -P INPUT DROP
  34. iptables -A INPUT -m state --state INVALID -j DROP
  35. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  36. iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j DROP
  37. #
  38. # insert accept rule or to jump to new accept-check table here
  39. #
  40. iptables -A INPUT -j input_rule
  41. [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
  42. # allow
  43. iptables -A INPUT -j LAN_ACCEPT # allow from lan/wifi interfaces
  44. iptables -A INPUT -p icmp -j ACCEPT # allow ICMP
  45. iptables -A INPUT -p gre -j ACCEPT # allow GRE
  46. # reject (what to do with anything not allowed earlier)
  47. iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
  48. iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
  49. ### OUTPUT
  50. ### (connections with the router as source)
  51. # base case
  52. iptables -P OUTPUT DROP
  53. iptables -A OUTPUT -m state --state INVALID -j DROP
  54. iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  55. #
  56. # insert accept rule or to jump to new accept-check table here
  57. #
  58. iptables -A OUTPUT -j output_rule
  59. # allow
  60. iptables -A OUTPUT -j ACCEPT #allow everything out
  61. # reject (what to do with anything not allowed earlier)
  62. iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
  63. iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
  64. ### FORWARDING
  65. ### (connections routed through the router)
  66. # base case
  67. iptables -P FORWARD DROP
  68. iptables -A FORWARD -m state --state INVALID -j DROP
  69. iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  70. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  71. #
  72. # insert accept rule or to jump to new accept-check table here
  73. #
  74. iptables -A FORWARD -j forwarding_rule
  75. [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
  76. # allow
  77. iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
  78. [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
  79. # reject (what to do with anything not allowed earlier)
  80. # uses the default -P DROP
  81. ### MASQ
  82. iptables -t nat -A PREROUTING -m state --state NEW -p tcp -j NEW
  83. iptables -t nat -A PREROUTING -j prerouting_rule
  84. [ -z "$WAN" ] || iptables -t nat -A PREROUTING -i "$WAN" -j prerouting_wan
  85. iptables -t nat -A POSTROUTING -j postrouting_rule
  86. [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
  87. iptables -t nat -A NEW -m limit --limit 50 --limit-burst 100 -j RETURN && \
  88. iptables -t nat -A NEW -j DROP
  89. ## USER RULES
  90. [ -f /etc/firewall.user ] && . /etc/firewall.user
  91. [ -n "$WAN" -a -e /etc/config/firewall ] && {
  92. export WAN
  93. awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
  94. }
  95. }
  96. stop() {
  97. iptables -P INPUT ACCEPT
  98. iptables -P OUTPUT ACCEPT
  99. iptables -P FORWARD ACCEPT
  100. iptables -F
  101. iptables -X
  102. iptables -t nat -P PREROUTING ACCEPT
  103. iptables -t nat -P POSTROUTING ACCEPT
  104. iptables -t nat -P OUTPUT ACCEPT
  105. iptables -t nat -F
  106. iptables -t nat -X
  107. }