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