firewall.init 4.0 KB

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