9d8a405ab0ff338b95fc8afeeb69bdf6925dabab
[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         config_load /var/state/network
10         
11         config_get WAN wan ifname
12         config_get WANDEV wan device
13         config_get LAN lan ifname
14         
15         ## CLEAR TABLES
16         for T in filter nat; do
17                 iptables -t $T -F
18                 iptables -t $T -X
19         done
20         
21         iptables -N input_rule
22         iptables -N input_wan
23         iptables -N output_rule
24         iptables -N forwarding_rule
25         iptables -N forwarding_wan
26
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
31         
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         
37         ### INPUT
38         ###  (connections with the router as destination)
39         
40         # base case
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
45         
46         #
47         # insert accept rule or to jump to new accept-check table here
48         #
49         iptables -A INPUT -j input_rule
50         [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
51         
52         # allow
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
56         
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
60         
61         ### OUTPUT
62         ### (connections with the router as source)
63         
64         # base case
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
68         
69         #
70         # insert accept rule or to jump to new accept-check table here
71         #
72         iptables -A OUTPUT -j output_rule
73         
74         # allow
75         iptables -A OUTPUT -j ACCEPT            #allow everything out
76         
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
80         
81         ### FORWARDING
82         ### (connections routed through the router)
83         
84         # base case
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
89         
90         #
91         # insert accept rule or to jump to new accept-check table here
92         #
93         iptables -A FORWARD -j forwarding_rule
94         [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
95         
96         # allow
97         iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
98         [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
99         
100         # reject (what to do with anything not allowed earlier)
101         # uses the default -P DROP
102         
103         ### MASQ
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         ### Only RFC1918 addresses
109         [ -z "$WAN" ] || iptables -t nat -A POSTROUTING --src 192.168.0.0/16 -o $WAN -j MASQUERADE
110         [ -z "$WAN" ] || iptables -t nat -A POSTROUTING --src 172.16.0.0/12 -o $WAN -j MASQUERADE
111         [ -z "$WAN" ] || iptables -t nat -A POSTROUTING --src 10.0.0.0/8 -o $WAN -j MASQUERADE
112
113         iptables -t nat -A NEW -m limit --limit 50 --limit-burst 100 -j RETURN && \
114                 iptables -t nat -A NEW -j DROP
115
116         ## USER RULES
117         [ -f /etc/firewall.user ] && . /etc/firewall.user
118         [ -n "$WAN" -a -e /etc/config/firewall ] && {
119                 export WAN
120                 awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
121         }
122 }
123
124 stop() {
125         iptables -P INPUT ACCEPT
126         iptables -P OUTPUT ACCEPT
127         iptables -P FORWARD ACCEPT
128         iptables -F
129         iptables -X
130         iptables -t nat -P PREROUTING ACCEPT
131         iptables -t nat -P POSTROUTING ACCEPT
132         iptables -t nat -P OUTPUT ACCEPT
133         iptables -t nat -F
134         iptables -t nat -X
135 }