1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
4 ## Please make changes in /etc/firewall.user
9 config_load /var/state/network
11 config_get WAN wan ifname
12 config_get WANDEV wan device
13 config_get LAN lan ifname
16 for T in filter nat; do
21 iptables -N input_rule
23 iptables -N output_rule
24 iptables -N forwarding_rule
25 iptables -N forwarding_wan
27 iptables -t nat -N NEW
28 iptables -t nat -N prerouting_rule
29 iptables -t nat -N prerouting_wan
30 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
38 ### (connections with the router as destination)
41 iptables -P INPUT DROP
42 iptables -A INPUT -m state --state INVALID -j DROP
43 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
44 iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j DROP
47 # insert accept rule or to jump to new accept-check table here
49 iptables -A INPUT -j input_rule
50 [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
53 iptables -A INPUT -j LAN_ACCEPT # allow from lan/wifi interfaces
54 iptables -A INPUT -p icmp -j ACCEPT # allow ICMP
55 iptables -A INPUT -p gre -j ACCEPT # allow GRE
57 # reject (what to do with anything not allowed earlier)
58 iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
59 iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
62 ### (connections with the router as source)
65 iptables -P OUTPUT DROP
66 iptables -A OUTPUT -m state --state INVALID -j DROP
67 iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
70 # insert accept rule or to jump to new accept-check table here
72 iptables -A OUTPUT -j output_rule
75 iptables -A OUTPUT -j ACCEPT #allow everything out
77 # reject (what to do with anything not allowed earlier)
78 iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
79 iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
82 ### (connections routed through the router)
85 iptables -P FORWARD DROP
86 iptables -A FORWARD -m state --state INVALID -j DROP
87 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
88 iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
91 # insert accept rule or to jump to new accept-check table here
93 iptables -A FORWARD -j forwarding_rule
94 [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
97 iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
98 [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
100 # reject (what to do with anything not allowed earlier)
101 # uses the default -P DROP
104 iptables -t nat -A PREROUTING -m state --state NEW -p tcp -j NEW
105 iptables -t nat -A PREROUTING -j prerouting_rule
106 [ -z "$WAN" ] || iptables -t nat -A PREROUTING -i "$WAN" -j prerouting_wan
107 iptables -t nat -A POSTROUTING -j postrouting_rule
108 [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
110 iptables -t nat -A NEW -m limit --limit 50 --limit-burst 100 -j RETURN && \
111 iptables -t nat -A NEW -j DROP
114 [ -f /etc/firewall.user ] && . /etc/firewall.user
115 [ -n "$WAN" -a -e /etc/config/firewall ] && {
117 awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
122 iptables -P INPUT ACCEPT
123 iptables -P OUTPUT ACCEPT
124 iptables -P FORWARD ACCEPT
127 iptables -t nat -P PREROUTING ACCEPT
128 iptables -t nat -P POSTROUTING ACCEPT
129 iptables -t nat -P OUTPUT ACCEPT