firewall.init 3.7 KB

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