prepare for moving part of the firewall to hotplug. created new chains {input,forward...
[librecmc/librecmc.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 prerouting_rule
27         iptables -t nat -N prerouting_wan
28         iptables -t nat -N postrouting_rule
29         
30         iptables -N LAN_ACCEPT
31         [ -z "$WAN" ] || iptables -A LAN_ACCEPT -i "$WAN" -j RETURN
32         [ -z "$WANDEV" -o "$WANDEV" = "$WAN" ] || iptables -A LAN_ACCEPT -i "$WANDEV" -j RETURN
33         iptables -A LAN_ACCEPT -j ACCEPT
34         
35         ### INPUT
36         ###  (connections with the router as destination)
37         
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         #
45         # insert accept rule or to jump to new accept-check table here
46         #
47         iptables -A INPUT -j input_rule
48         [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
49         
50         # allow
51         iptables -A INPUT -j LAN_ACCEPT # allow from lan/wifi interfaces 
52         iptables -A INPUT -p icmp       -j ACCEPT       # allow ICMP
53         iptables -A INPUT -p gre        -j ACCEPT       # allow GRE
54         
55         # reject (what to do with anything not allowed earlier)
56         iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
57         iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
58         
59         ### OUTPUT
60         ### (connections with the router as source)
61         
62         # base case
63         iptables -P OUTPUT DROP
64         iptables -A OUTPUT -m state --state INVALID -j DROP
65         iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
66         
67         #
68         # insert accept rule or to jump to new accept-check table here
69         #
70         iptables -A OUTPUT -j output_rule
71         
72         # allow
73         iptables -A OUTPUT -j ACCEPT            #allow everything out
74         
75         # reject (what to do with anything not allowed earlier)
76         iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
77         iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
78         
79         ### FORWARDING
80         ### (connections routed through the router)
81         
82         # base case
83         iptables -P FORWARD DROP 
84         iptables -A FORWARD -m state --state INVALID -j DROP
85         iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
86         iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
87         
88         #
89         # insert accept rule or to jump to new accept-check table here
90         #
91         iptables -A FORWARD -j forwarding_rule
92         [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
93         
94         # allow
95         iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
96         [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
97         
98         # reject (what to do with anything not allowed earlier)
99         # uses the default -P DROP
100         
101         ### MASQ
102         iptables -t nat -A PREROUTING -j prerouting_rule
103         [ -z "$WAN" ] || iptables -t nat -A PREROUTING -i "$WAN" -j prerouting_wan
104         iptables -t nat -A POSTROUTING -j postrouting_rule
105         [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
106         
107         ## USER RULES
108         [ -f /etc/firewall.user ] && . /etc/firewall.user
109         [ -n "$WAN" -a -e /etc/config/firewall ] && {
110                 export WAN
111                 awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
112         }
113 }
114
115 stop() {
116         iptables -P INPUT ACCEPT
117         iptables -P OUTPUT ACCEPT
118         iptables -P FORWARD ACCEPT
119         iptables -F
120         iptables -X
121         iptables -t nat -P PREROUTING ACCEPT
122         iptables -t nat -P POSTROUTING ACCEPT
123         iptables -t nat -P OUTPUT ACCEPT
124         iptables -t nat -F
125         iptables -t nat -X
126 }