iptables: Replace state module by conntrack module.
[people/teissler/ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
3eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
4eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 5eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
6IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
7
8if [ -f /var/ipfire/red/device ]; then
9 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
10fi
11
12iptables_init() {
13 # Flush all rules and delete all custom chains
14 /sbin/iptables -F
15 /sbin/iptables -t nat -F
16 /sbin/iptables -t mangle -F
17 /sbin/iptables -X
18 /sbin/iptables -t nat -X
19 /sbin/iptables -t mangle -X
20
21 # Set up policies
22 /sbin/iptables -P INPUT DROP
23 /sbin/iptables -P FORWARD DROP
24 /sbin/iptables -P OUTPUT ACCEPT
25
26 # Empty LOG_DROP and LOG_REJECT chains
27 /sbin/iptables -N LOG_DROP
28 /sbin/iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
29 /sbin/iptables -A LOG_DROP -j DROP
30 /sbin/iptables -N LOG_REJECT
31 /sbin/iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
32 /sbin/iptables -A LOG_REJECT -j REJECT
33
34 # This chain will log, then DROPs packets with certain bad combinations
35 # of flags might indicate a port-scan attempt (xmas, null, etc)
36 /sbin/iptables -N PSCAN
5595bc03 37 if [ "$DROPPORTSCAN" == "on" ]; then
97fe1741 38 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
fe0cd647 39 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
97fe1741
CS
40 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
41 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 42 fi
97fe1741 43 /sbin/iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
44
45 # New tcp packets without SYN set - could well be an obscure type of port scan
46 # that's not covered above, may just be a broken windows machine
47 /sbin/iptables -N NEWNOTSYN
5595bc03 48 if [ "$DROPNEWNOTSYN" == "on" ]; then
97fe1741 49 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 50 fi
97fe1741 51 /sbin/iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
52
53 # Chain to contain all the rules relating to bad TCP flags
54 /sbin/iptables -N BADTCP
55
d8158ca6 56 #Don't check loopback
a56df4ee 57 /sbin/iptables -A BADTCP -i lo -j RETURN
d8158ca6 58
3a1019f6
MT
59 # Disallow packets frequently used by port-scanners
60 # nmap xmas
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
62 # Null
63 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
64 # FIN
65 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
66 # SYN/RST (also catches xmas variants that set SYN+RST+...)
67 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
68 # SYN/FIN (QueSO or nmap OS probe)
69 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
70 # NEW TCP without SYN
b85d2a98
MT
71 /sbin/iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
72
73 # Connection tracking chain
74 /sbin/iptables -N CONNTRACK
75 /sbin/iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
3a1019f6
MT
76
77 /sbin/iptables -A INPUT -j BADTCP
78 /sbin/iptables -A FORWARD -j BADTCP
79
3a1019f6
MT
80 # Fix for braindead ISP's
81 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
82
83 # CUSTOM chains, can be used by the users themselves
84 /sbin/iptables -N CUSTOMINPUT
85 /sbin/iptables -A INPUT -j CUSTOMINPUT
057249ba
CS
86 /sbin/iptables -N GUARDIAN
87 /sbin/iptables -A INPUT -j GUARDIAN
690b0bd7
AM
88 /sbin/iptables -N OVPNBLOCK
89 /sbin/iptables -A FORWARD -j OVPNBLOCK
057249ba 90 /sbin/iptables -A FORWARD -j GUARDIAN
3a1019f6
MT
91 /sbin/iptables -N CUSTOMFORWARD
92 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
93 /sbin/iptables -N CUSTOMOUTPUT
690b0bd7 94 /sbin/iptables -A OUTPUT -j OVPNBLOCK
3a1019f6 95 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
4cb74dce
MT
96 /sbin/iptables -N OUTGOINGFW
97 /sbin/iptables -A OUTPUT -j OUTGOINGFW
3a1019f6 98 /sbin/iptables -t nat -N CUSTOMPREROUTING
690b0bd7 99 /sbin/iptables -t nat -N OVPNNAT
3a1019f6
MT
100 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
101 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
102 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
690b0bd7 103 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
3a1019f6 104
daa1ceba
AF
105 # IPTV chains for IGMPPROXY
106 /sbin/iptables -N IPTVINPUT
107 /sbin/iptables -A INPUT -j IPTVINPUT
108 /sbin/iptables -N IPTVFORWARD
109 /sbin/iptables -A FORWARD -j IPTVFORWARD
110
690b0bd7
AM
111 # Filtering ovpn networks INPUT
112 /sbin/iptables -A INPUT -j OVPNBLOCK
113
3a1019f6
MT
114 # filtering from GUI
115 /sbin/iptables -N GUIINPUT
116 /sbin/iptables -A INPUT -j GUIINPUT
905fbf3e 117 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6
MT
118
119 # Accept everything connected
b85d2a98
MT
120 for i in INPUT FORWARD OUTPUT; do
121 /sbin/iptables -A ${i} -j CONNTRACK
122 done
123
ef6f983b 124 # Accept everything on lo
b85d2a98
MT
125 iptables -A INPUT -i lo -m conntrack --ctstate NEW -j ACCEPT
126 iptables -A OUTPUT -o lo -m conntrack --ctstate NEW -j ACCEPT
ef6f983b 127
5fd30232 128 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
6652626c
AF
129 /sbin/iptables -N IPSECINPUT
130 /sbin/iptables -N IPSECFORWARD
131 /sbin/iptables -N IPSECOUTPUT
5fd30232 132 /sbin/iptables -N OPENSSLVIRTUAL
6652626c 133 /sbin/iptables -A INPUT -j IPSECINPUT
5595bc03 134 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL INPUT"
6652626c 135 /sbin/iptables -A FORWARD -j IPSECFORWARD
5595bc03 136 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL FORWARD"
6652626c 137 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
c4cd0f7b
AF
138 /sbin/iptables -t nat -N IPSECNAT
139 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 140
231499fc
AM
141 # Input Firewall
142 /sbin/iptables -N INPUTFW
b85d2a98 143 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j INPUTFW
231499fc 144
3a1019f6 145 # localhost and ethernet.
b85d2a98
MT
146 /sbin/iptables -A INPUT -i lo -m conntrack --ctstate NEW -j ACCEPT
147 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP # Loopback not on lo
148 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
149 /sbin/iptables -A FORWARD -i lo -m conntrack --ctstate NEW -j ACCEPT
150 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
151 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
152 /sbin/iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp
218b3341 153
3a1019f6
MT
154 # allow DHCP on BLUE to be turned on/off
155 /sbin/iptables -N DHCPBLUEINPUT
156 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
81393987
AM
157
158 # WIRELESS chains
159 /sbin/iptables -N WIRELESSINPUT
b85d2a98 160 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
81393987 161 /sbin/iptables -N WIRELESSFORWARD
b85d2a98 162 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
12dcfbbd 163
93b75f31
AM
164 # Forward Firewall
165 /sbin/iptables -N FORWARDFW
166 /sbin/iptables -A FORWARD -j FORWARDFW
167
5fd30232
MT
168 # OPenSSL
169 /sbin/iptables -N OPENSSLPHYSICAL
170 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
3a1019f6 171
3a1019f6
MT
172 # RED chain, used for the red interface
173 /sbin/iptables -N REDINPUT
174 /sbin/iptables -A INPUT -j REDINPUT
175 /sbin/iptables -N REDFORWARD
176 /sbin/iptables -A FORWARD -j REDFORWARD
177 /sbin/iptables -t nat -N REDNAT
178 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
179
180 iptables_red
218b3341 181
3a1019f6
MT
182 # Custom prerouting chains (for transparent proxy and port forwarding)
183 /sbin/iptables -t nat -N SQUID
184 /sbin/iptables -t nat -A PREROUTING -j SQUID
9efd8d1c
AM
185 /sbin/iptables -t nat -N NAT_DESTINATION
186 /sbin/iptables -t nat -N NAT_SOURCE
187 /sbin/iptables -t nat -A PREROUTING -j NAT_DESTINATION
690b0bd7
AM
188 /sbin/iptables -t nat -I POSTROUTING 3 -j NAT_SOURCE
189
9efd8d1c
AM
190
191
7e7495b3
MT
192 # upnp chain for our upnp daemon
193 /sbin/iptables -t nat -N UPNPFW
194 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
54194ba4 195 /sbin/iptables -N UPNPFW
b85d2a98 196 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
3a1019f6 197
3a1019f6 198 # Postrouting rules (for port forwarding)
690b0bd7 199 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT --to-source $GREEN_ADDRESS
3a1019f6
MT
200 if [ "$BLUE_DEV" != "" ]; then
201 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
202 fi
203 if [ "$ORANGE_DEV" != "" ]; then
204 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
205 fi
206
3a1019f6
MT
207 # run local firewall configuration, if present
208 if [ -x /etc/sysconfig/firewall.local ]; then
209 /etc/sysconfig/firewall.local start
210 fi
690b0bd7 211
ff4770c7
AM
212 # run openvpn
213 /usr/local/bin/openvpnctrl --create-chains-and-rules
214
215 # run wirelessctrl
216 /usr/local/bin/wirelessctrl
217
62fc8511 218 #POLICY CHAIN
a9b3ae26
AM
219 /sbin/iptables -N POLICYIN
220 /sbin/iptables -A INPUT -j POLICYIN
5d7faa45
AM
221 /sbin/iptables -N POLICYFWD
222 /sbin/iptables -A FORWARD -j POLICYFWD
223 /sbin/iptables -N POLICYOUT
224 /sbin/iptables -A OUTPUT -j POLICYOUT
b324de14 225
5d7faa45 226 /usr/sbin/firewall-policy
690b0bd7 227
ff4770c7
AM
228 # read new firewall
229 /usr/local/bin/forwardfwctrl
230
690b0bd7
AM
231 if [ "$DROPINPUT" == "on" ]; then
232 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT"
233 fi
e41b651b 234 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
690b0bd7
AM
235 if [ "$DROPFORWARD" == "on" ]; then
236 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD"
237 fi
238 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_FORWARD"
ff4770c7 239}
3a1019f6 240
ff4770c7
AM
241iptables_red() {
242 /sbin/iptables -F REDINPUT
243 /sbin/iptables -F REDFORWARD
244 /sbin/iptables -t nat -F REDNAT
3a1019f6 245
ff4770c7
AM
246 # PPPoE / PPTP Device
247 if [ "$IFACE" != "" ]; then
248 # PPPoE / PPTP
249 if [ "$DEVICE" != "" ]; then
250 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
251 fi
252 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
253 if [ "$RED_DEV" != "" ]; then
254 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
255 fi
256 fi
3a1019f6 257 fi
ff4770c7
AM
258
259 # PPTP over DHCP
260 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
261 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
262 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
263 fi
264
ff4770c7
AM
265 # Orange pinholes
266 if [ "$ORANGE_DEV" != "" ]; then
267 # This rule enables a host on ORANGE network to connect to the outside
268 # (only if we have a red connection)
269 if [ "$IFACE" != "" ]; then
270 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
271 fi
3a1019f6 272 fi
c400fe4c 273
ff4770c7
AM
274 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
275 # DHCP
276 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
277 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
278 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
279 fi
280 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
281 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
282 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
283 fi
284
285 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
286 /sbin/iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
287 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
c400fe4c 288
6be0579b 289 fi
ff4770c7
AM
290}
291
292# See how we were called.
293case "$1" in
294 start)
295 iptables_init
6be0579b 296 ;;
3a1019f6
MT
297 reload)
298 iptables_red
3a1019f6 299 # run local firewall configuration, if present
ff4770c7 300 if [ -x /etc/sysconfig/firewall.local ]; then
3a1019f6
MT
301 /etc/sysconfig/firewall.local reload
302 fi
303 ;;
304 restart)
ff4770c7
AM
305 # run local firewall configuration, if present
306 if [ -x /etc/sysconfig/firewall.local ]; then
307 /etc/sysconfig/firewall.local stop
308 fi
3a1019f6
MT
309 $0 start
310 ;;
311 *)
ff4770c7 312 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
313 exit 1
314 ;;
315esac
316
317exit 0