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