Fix missing string in proxy.cgi (Cache-Digest creation).
[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
c0359d6d
MT
73 /sbin/iptables -A INPUT -p tcp -j BADTCP
74 /sbin/iptables -A FORWARD -p tcp -j BADTCP
75
b85d2a98
MT
76 # Connection tracking chain
77 /sbin/iptables -N CONNTRACK
78 /sbin/iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
3a1019f6 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
86 /sbin/iptables -N CUSTOMFORWARD
87 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
88 /sbin/iptables -N CUSTOMOUTPUT
89 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
90 /sbin/iptables -t nat -N CUSTOMPREROUTING
91 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
92 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
93 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
94
815eaff4
MT
95 # Guardian (IPS) chains
96 /sbin/iptables -N GUARDIAN
97 /sbin/iptables -A INPUT -j GUARDIAN
98 /sbin/iptables -A FORWARD -j GUARDIAN
99
1e555330
MT
100 # Block OpenVPN transfer networks
101 /sbin/iptables -N OVPNBLOCK
c0f99754 102 for i in INPUT FORWARD; do
1e555330
MT
103 /sbin/iptables -A ${i} -j OVPNBLOCK
104 done
105
51ab1de1
MT
106 # OpenVPN transfer network translation
107 /sbin/iptables -t nat -N OVPNNAT
108 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
109
daa1ceba
AF
110 # IPTV chains for IGMPPROXY
111 /sbin/iptables -N IPTVINPUT
112 /sbin/iptables -A INPUT -j IPTVINPUT
113 /sbin/iptables -N IPTVFORWARD
114 /sbin/iptables -A FORWARD -j IPTVFORWARD
115
3a1019f6
MT
116 # filtering from GUI
117 /sbin/iptables -N GUIINPUT
118 /sbin/iptables -A INPUT -j GUIINPUT
905fbf3e 119 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6 120
afc611d4
MT
121 # Accept everything on loopback
122 /sbin/iptables -N LOOPBACK
123 /sbin/iptables -A LOOPBACK -i lo -j ACCEPT
124 /sbin/iptables -A LOOPBACK -o lo -j ACCEPT
125
3b9a23ce
MT
126 # Filter all packets with loopback addresses on non-loopback interfaces.
127 /sbin/iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
128 /sbin/iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
129
130 for i in INPUT FORWARD OUTPUT; do
131 /sbin/iptables -A ${i} -j LOOPBACK
132 done
afc611d4 133
3a1019f6 134 # Accept everything connected
b85d2a98
MT
135 for i in INPUT FORWARD OUTPUT; do
136 /sbin/iptables -A ${i} -j CONNTRACK
137 done
138
5fd30232 139 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
6652626c
AF
140 /sbin/iptables -N IPSECINPUT
141 /sbin/iptables -N IPSECFORWARD
142 /sbin/iptables -N IPSECOUTPUT
6652626c 143 /sbin/iptables -A INPUT -j IPSECINPUT
6652626c 144 /sbin/iptables -A FORWARD -j IPSECFORWARD
6652626c 145 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
c4cd0f7b
AF
146 /sbin/iptables -t nat -N IPSECNAT
147 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 148
3a1019f6 149 # localhost and ethernet.
b85d2a98 150 /sbin/iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp
218b3341 151
3a1019f6
MT
152 # allow DHCP on BLUE to be turned on/off
153 /sbin/iptables -N DHCPBLUEINPUT
154 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
81393987
AM
155
156 # WIRELESS chains
157 /sbin/iptables -N WIRELESSINPUT
b85d2a98 158 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
81393987 159 /sbin/iptables -N WIRELESSFORWARD
b85d2a98 160 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
987b75bc 161
ab4876ad
MT
162 # OpenVPN
163 /sbin/iptables -N OVPNINPUT
164 /sbin/iptables -A INPUT -j OVPNINPUT
165
987b75bc
MT
166 # TOR
167 /sbin/iptables -N TOR_INPUT
168 /sbin/iptables -A INPUT -j TOR_INPUT
12dcfbbd 169
d5f1422d
MT
170 # Jump into the actual firewall ruleset.
171 /sbin/iptables -N INPUTFW
172 /sbin/iptables -A INPUT -j INPUTFW
173
174 /sbin/iptables -N OUTGOINGFW
175 /sbin/iptables -A OUTPUT -j OUTGOINGFW
176
93b75f31
AM
177 /sbin/iptables -N FORWARDFW
178 /sbin/iptables -A FORWARD -j FORWARDFW
d5f1422d 179
fac38614
AM
180 # SNAT rules
181 /sbin/iptables -t nat -N NAT_SOURCE
182 /sbin/iptables -t nat -A POSTROUTING -j NAT_SOURCE
183
3a1019f6
MT
184 # RED chain, used for the red interface
185 /sbin/iptables -N REDINPUT
186 /sbin/iptables -A INPUT -j REDINPUT
187 /sbin/iptables -N REDFORWARD
188 /sbin/iptables -A FORWARD -j REDFORWARD
189 /sbin/iptables -t nat -N REDNAT
190 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
191
192 iptables_red
bb12dd7b
MT
193
194 # Custom prerouting chains (for transparent proxy)
3a1019f6
MT
195 /sbin/iptables -t nat -N SQUID
196 /sbin/iptables -t nat -A PREROUTING -j SQUID
bb12dd7b
MT
197
198 # DNAT rules
9efd8d1c 199 /sbin/iptables -t nat -N NAT_DESTINATION
9efd8d1c 200 /sbin/iptables -t nat -A PREROUTING -j NAT_DESTINATION
bb12dd7b 201
7e7495b3
MT
202 # upnp chain for our upnp daemon
203 /sbin/iptables -t nat -N UPNPFW
204 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
54194ba4 205 /sbin/iptables -N UPNPFW
b85d2a98 206 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
3a1019f6 207
3a1019f6
MT
208 # run local firewall configuration, if present
209 if [ -x /etc/sysconfig/firewall.local ]; then
210 /etc/sysconfig/firewall.local start
211 fi
690b0bd7 212
ab4876ad
MT
213 # Apply OpenVPN firewall rules
214 /usr/local/bin/openvpnctrl --firewall-rules
ff4770c7
AM
215
216 # run wirelessctrl
217 /usr/local/bin/wirelessctrl
218
62fc8511 219 #POLICY CHAIN
a9b3ae26
AM
220 /sbin/iptables -N POLICYIN
221 /sbin/iptables -A INPUT -j POLICYIN
5d7faa45
AM
222 /sbin/iptables -N POLICYFWD
223 /sbin/iptables -A FORWARD -j POLICYFWD
224 /sbin/iptables -N POLICYOUT
225 /sbin/iptables -A OUTPUT -j POLICYOUT
b324de14 226
5d7faa45 227 /usr/sbin/firewall-policy
690b0bd7 228
ff4770c7 229 # read new firewall
8039a710 230 /usr/local/bin/firewallctrl
ff4770c7 231
690b0bd7
AM
232 if [ "$DROPINPUT" == "on" ]; then
233 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT"
234 fi
e41b651b 235 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
690b0bd7
AM
236 if [ "$DROPFORWARD" == "on" ]; then
237 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD"
238 fi
239 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_FORWARD"
ff4770c7 240}
3a1019f6 241
ff4770c7
AM
242iptables_red() {
243 /sbin/iptables -F REDINPUT
244 /sbin/iptables -F REDFORWARD
245 /sbin/iptables -t nat -F REDNAT
3a1019f6 246
ff4770c7
AM
247 # PPPoE / PPTP Device
248 if [ "$IFACE" != "" ]; then
249 # PPPoE / PPTP
250 if [ "$DEVICE" != "" ]; then
251 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
252 fi
253 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
254 if [ "$RED_DEV" != "" ]; then
255 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
256 fi
257 fi
3a1019f6 258 fi
ff4770c7
AM
259
260 # PPTP over DHCP
261 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
262 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
263 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
264 fi
265
ff4770c7
AM
266 # Orange pinholes
267 if [ "$ORANGE_DEV" != "" ]; then
268 # This rule enables a host on ORANGE network to connect to the outside
269 # (only if we have a red connection)
270 if [ "$IFACE" != "" ]; then
271 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
272 fi
3a1019f6 273 fi
c400fe4c 274
ff4770c7
AM
275 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
276 # DHCP
277 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
278 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
279 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
280 fi
281 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
282 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
283 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
284 fi
285
286 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
287 /sbin/iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
288 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
c400fe4c 289
6be0579b 290 fi
ff4770c7
AM
291}
292
293# See how we were called.
294case "$1" in
295 start)
296 iptables_init
6be0579b 297 ;;
3a1019f6
MT
298 reload)
299 iptables_red
3a1019f6 300 # run local firewall configuration, if present
ff4770c7 301 if [ -x /etc/sysconfig/firewall.local ]; then
3a1019f6
MT
302 /etc/sysconfig/firewall.local reload
303 fi
304 ;;
305 restart)
ff4770c7
AM
306 # run local firewall configuration, if present
307 if [ -x /etc/sysconfig/firewall.local ]; then
308 /etc/sysconfig/firewall.local stop
309 fi
3a1019f6
MT
310 $0 start
311 ;;
312 *)
ff4770c7 313 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
314 exit 1
315 ;;
316esac
317
318exit 0