]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/firewall
Merge remote-tracking branch 'stevee/core-90-geoip' into next
[ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
0f5c5ce7
MT
3. /etc/sysconfig/rc
4. ${rc_functions}
5
3a1019f6
MT
6eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 8eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
9IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10
11if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13fi
14
c581b670
MT
15function iptables() {
16 /sbin/iptables --wait "$@"
17}
18
3a1019f6
MT
19iptables_init() {
20 # Flush all rules and delete all custom chains
c581b670
MT
21 iptables -F
22 iptables -t nat -F
23 iptables -t mangle -F
24 iptables -X
25 iptables -t nat -X
26 iptables -t mangle -X
3a1019f6
MT
27
28 # Set up policies
c581b670
MT
29 iptables -P INPUT DROP
30 iptables -P FORWARD DROP
31 iptables -P OUTPUT ACCEPT
3a1019f6
MT
32
33 # Empty LOG_DROP and LOG_REJECT chains
c581b670
MT
34 iptables -N LOG_DROP
35 iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
36 iptables -A LOG_DROP -j DROP
37 iptables -N LOG_REJECT
38 iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
39 iptables -A LOG_REJECT -j REJECT
3a1019f6
MT
40
41 # This chain will log, then DROPs packets with certain bad combinations
42 # of flags might indicate a port-scan attempt (xmas, null, etc)
c581b670 43 iptables -N PSCAN
5595bc03 44 if [ "$DROPPORTSCAN" == "on" ]; then
c581b670
MT
45 iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
46 iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
47 iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
48 iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 49 fi
c581b670 50 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
51
52 # New tcp packets without SYN set - could well be an obscure type of port scan
53 # that's not covered above, may just be a broken windows machine
c581b670 54 iptables -N NEWNOTSYN
5595bc03 55 if [ "$DROPNEWNOTSYN" == "on" ]; then
c581b670 56 iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 57 fi
c581b670 58 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
59
60 # Chain to contain all the rules relating to bad TCP flags
c581b670 61 iptables -N BADTCP
3a1019f6 62
c581b670
MT
63 # Don't check loopback
64 iptables -A BADTCP -i lo -j RETURN
d8158ca6 65
3a1019f6 66 # Disallow packets frequently used by port-scanners
dccbf1bf
AF
67 # NMAP FIN/URG/PSH (XMAS scan)
68 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
69 # SYN/RST/ACK/FIN/URG
70 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
71 # ALL/ALL
72 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
73 # FIN Stealth
c581b670 74 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
3a1019f6 75 # SYN/RST (also catches xmas variants that set SYN+RST+...)
c581b670 76 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
3a1019f6 77 # SYN/FIN (QueSO or nmap OS probe)
c581b670 78 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
dccbf1bf
AF
79 # Null
80 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
3a1019f6 81 # NEW TCP without SYN
c581b670 82 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
b85d2a98 83
c581b670
MT
84 iptables -A INPUT -p tcp -j BADTCP
85 iptables -A FORWARD -p tcp -j BADTCP
c0359d6d 86
b85d2a98 87 # Connection tracking chain
c581b670
MT
88 iptables -N CONNTRACK
89 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
dccbf1bf 90 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j DROP
3a1019f6 91
3a1019f6 92 # Fix for braindead ISP's
c581b670 93 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
3a1019f6
MT
94
95 # CUSTOM chains, can be used by the users themselves
c581b670
MT
96 iptables -N CUSTOMINPUT
97 iptables -A INPUT -j CUSTOMINPUT
98 iptables -N CUSTOMFORWARD
99 iptables -A FORWARD -j CUSTOMFORWARD
100 iptables -N CUSTOMOUTPUT
101 iptables -A OUTPUT -j CUSTOMOUTPUT
102 iptables -t nat -N CUSTOMPREROUTING
103 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
104 iptables -t nat -N CUSTOMPOSTROUTING
105 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
3a1019f6 106
2a5b19c5
AF
107 # P2PBLOCK
108 iptables -N P2PBLOCK
109 iptables -A INPUT -j P2PBLOCK
110 iptables -A FORWARD -j P2PBLOCK
111 iptables -A OUTPUT -j P2PBLOCK
112
815eaff4 113 # Guardian (IPS) chains
c581b670
MT
114 iptables -N GUARDIAN
115 iptables -A INPUT -j GUARDIAN
116 iptables -A FORWARD -j GUARDIAN
815eaff4 117
1e555330 118 # Block OpenVPN transfer networks
c581b670 119 iptables -N OVPNBLOCK
a0a5c14f 120 iptables -A INPUT -i tun+ -j OVPNBLOCK
a0a5c14f
MT
121 iptables -A FORWARD -i tun+ -j OVPNBLOCK
122 iptables -A FORWARD -o tun+ -j OVPNBLOCK
1e555330 123
51ab1de1 124 # OpenVPN transfer network translation
c581b670
MT
125 iptables -t nat -N OVPNNAT
126 iptables -t nat -A POSTROUTING -j OVPNNAT
51ab1de1 127
daa1ceba 128 # IPTV chains for IGMPPROXY
c581b670
MT
129 iptables -N IPTVINPUT
130 iptables -A INPUT -j IPTVINPUT
131 iptables -N IPTVFORWARD
132 iptables -A FORWARD -j IPTVFORWARD
daa1ceba 133
8e59a602
MT
134 # Allow to ping the firewall.
135 iptables -N ICMPINPUT
136 iptables -A INPUT -j ICMPINPUT
137 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6 138
afc611d4 139 # Accept everything on loopback
c581b670
MT
140 iptables -N LOOPBACK
141 iptables -A LOOPBACK -i lo -j ACCEPT
142 iptables -A LOOPBACK -o lo -j ACCEPT
afc611d4 143
3b9a23ce 144 # Filter all packets with loopback addresses on non-loopback interfaces.
c581b670
MT
145 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
146 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
3b9a23ce
MT
147
148 for i in INPUT FORWARD OUTPUT; do
c581b670 149 iptables -A ${i} -j LOOPBACK
3b9a23ce 150 done
afc611d4 151
3a1019f6 152 # Accept everything connected
b85d2a98 153 for i in INPUT FORWARD OUTPUT; do
c581b670 154 iptables -A ${i} -j CONNTRACK
b85d2a98
MT
155 done
156
8490e496
MT
157 # Allow DHCP
158 iptables -N DHCPINPUT
159 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
160 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
161
162 iptables -N DHCPOUTPUT
163 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
164 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
165
166 # Allow DHCP on GREEN
167 iptables -N DHCPGREENINPUT
168 iptables -N DHCPGREENOUTPUT
169 if [ -n "${GREEN_DEV}" ]; then
170 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
171 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
172 fi
173
174 # allow DHCP on BLUE to be turned on/off
175 iptables -N DHCPBLUEINPUT
176 iptables -N DHCPBLUEOUTPUT
177 if [ -n "${BLUE_DEV}" ]; then
178 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
179 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
180 fi
181
cab02e2a
SS
182 # GeoIP block
183 iptables -N GEOIPBLOCK
184 iptables -A INPUT -j GEOIPBLOCK
185 iptables -A FORWARD -j GEOIPBLOCK
186
5fd30232 187 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
c581b670
MT
188 iptables -N IPSECINPUT
189 iptables -N IPSECFORWARD
190 iptables -N IPSECOUTPUT
191 iptables -A INPUT -j IPSECINPUT
192 iptables -A FORWARD -j IPSECFORWARD
193 iptables -A OUTPUT -j IPSECOUTPUT
194 iptables -t nat -N IPSECNAT
195 iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 196
3a1019f6 197 # localhost and ethernet.
c0e0848f
MT
198 # Always allow accessing the web GUI from GREEN.
199 iptables -N GUIINPUT
200 iptables -A INPUT -j GUIINPUT
201 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
8490e496 202
81393987 203 # WIRELESS chains
c581b670
MT
204 iptables -N WIRELESSINPUT
205 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
206 iptables -N WIRELESSFORWARD
207 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
987b75bc 208
ab4876ad 209 # OpenVPN
c581b670
MT
210 iptables -N OVPNINPUT
211 iptables -A INPUT -j OVPNINPUT
ab4876ad 212
987b75bc 213 # TOR
c581b670
MT
214 iptables -N TOR_INPUT
215 iptables -A INPUT -j TOR_INPUT
12dcfbbd 216
d5f1422d 217 # Jump into the actual firewall ruleset.
c581b670
MT
218 iptables -N INPUTFW
219 iptables -A INPUT -j INPUTFW
d5f1422d 220
c581b670
MT
221 iptables -N OUTGOINGFW
222 iptables -A OUTPUT -j OUTGOINGFW
d5f1422d 223
c581b670
MT
224 iptables -N FORWARDFW
225 iptables -A FORWARD -j FORWARDFW
d5f1422d 226
fac38614 227 # SNAT rules
c581b670
MT
228 iptables -t nat -N NAT_SOURCE
229 iptables -t nat -A POSTROUTING -j NAT_SOURCE
fac38614 230
bb12dd7b 231 # Custom prerouting chains (for transparent proxy)
c581b670
MT
232 iptables -t nat -N SQUID
233 iptables -t nat -A PREROUTING -j SQUID
bb12dd7b
MT
234
235 # DNAT rules
c581b670
MT
236 iptables -t nat -N NAT_DESTINATION
237 iptables -t nat -A PREROUTING -j NAT_DESTINATION
99f11a16 238 iptables -t nat -A OUTPUT -j NAT_DESTINATION
bb12dd7b 239
6e87f0aa
MT
240 iptables -t mangle -N NAT_DESTINATION
241 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
242
243 iptables -t nat -N NAT_DESTINATION_FIX
244 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
245
246 iptables -t nat -A NAT_DESTINATION_FIX \
247 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
248
249 if [ -n "${BLUE_ADDRESS}" ]; then
250 iptables -t nat -A NAT_DESTINATION_FIX \
251 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
252 fi
253
254 if [ -n "${ORANGE_ADDRESS}" ]; then
255 iptables -t nat -A NAT_DESTINATION_FIX \
256 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
257 fi
258
7e7495b3 259 # upnp chain for our upnp daemon
c581b670
MT
260 iptables -t nat -N UPNPFW
261 iptables -t nat -A PREROUTING -j UPNPFW
262 iptables -N UPNPFW
263 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
3a1019f6 264
6e87f0aa
MT
265 # RED chain, used for the red interface
266 iptables -N REDINPUT
267 iptables -A INPUT -j REDINPUT
268 iptables -N REDFORWARD
269 iptables -A FORWARD -j REDFORWARD
270 iptables -t nat -N REDNAT
271 iptables -t nat -A POSTROUTING -j REDNAT
272
ab4876ad
MT
273 # Apply OpenVPN firewall rules
274 /usr/local/bin/openvpnctrl --firewall-rules
ff4770c7
AM
275
276 # run wirelessctrl
277 /usr/local/bin/wirelessctrl
278
c581b670
MT
279 # POLICY CHAIN
280 iptables -N POLICYIN
281 iptables -A INPUT -j POLICYIN
282 iptables -N POLICYFWD
283 iptables -A FORWARD -j POLICYFWD
284 iptables -N POLICYOUT
285 iptables -A OUTPUT -j POLICYOUT
b324de14 286
bb383423 287 # Initialize firewall policies.
5d7faa45 288 /usr/sbin/firewall-policy
690b0bd7 289
bb383423 290 # Install firewall rules for the red interface.
4b12aa41
TE
291 iptables_red_up
292
293 # If red has not been brought up yet, we will
294 # add the blocking rules for MASQUERADE
295 if [ ! -e "/var/ipfire/red/active" ]; then
296 iptables_red_down
297 fi
ff4770c7 298}
3a1019f6 299
4b12aa41 300iptables_red_up() {
c581b670
MT
301 iptables -F REDINPUT
302 iptables -F REDFORWARD
303 iptables -t nat -F REDNAT
3a1019f6 304
ff4770c7
AM
305 # PPPoE / PPTP Device
306 if [ "$IFACE" != "" ]; then
307 # PPPoE / PPTP
308 if [ "$DEVICE" != "" ]; then
c581b670 309 iptables -A REDINPUT -i $DEVICE -j ACCEPT
ff4770c7
AM
310 fi
311 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
312 if [ "$RED_DEV" != "" ]; then
c581b670 313 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
ff4770c7
AM
314 fi
315 fi
3a1019f6 316 fi
ff4770c7
AM
317
318 # PPTP over DHCP
319 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
c581b670
MT
320 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
321 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
322 fi
323
ff4770c7
AM
324 # Orange pinholes
325 if [ "$ORANGE_DEV" != "" ]; then
326 # This rule enables a host on ORANGE network to connect to the outside
327 # (only if we have a red connection)
328 if [ "$IFACE" != "" ]; then
c581b670 329 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
ff4770c7 330 fi
3a1019f6 331 fi
c400fe4c 332
ff4770c7
AM
333 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
334 # DHCP
335 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
c581b670
MT
336 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
337 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
338 fi
339 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
c581b670
MT
340 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
341 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
342 fi
343
344 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
c581b670 345 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
c926c637 346
83ef9c40
MT
347 if [ "$IFACE" = "$GREEN_DEV" ]; then
348 MASQUERADE_GREEN="off"
c926c637 349 fi
c400fe4c 350
983d471f 351 local NO_MASQ_NETWORKS
83ef9c40
MT
352
353 if [ "${MASQUERADE_GREEN}" = "off" ]; then
983d471f 354 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
83ef9c40
MT
355 fi
356
357 if [ "${MASQUERADE_BLUE}" = "off" ]; then
983d471f 358 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
83ef9c40
MT
359 fi
360
361 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
983d471f 362 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
83ef9c40
MT
363 fi
364
983d471f
MT
365 local network
366 for network in ${NO_MASQ_NETWORKS}; do
367 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
83ef9c40
MT
368 done
369
370 # Masquerade everything else
371 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
6be0579b 372 fi
66f6b279
MT
373
374 # Reload all rules.
55a5bcae 375 /usr/local/bin/firewallctrl
ff4770c7
AM
376}
377
4b12aa41
TE
378iptables_red_down() {
379 # Prohibit packets to reach the masquerading rule
380 # while the wan interface is down - this is required to
381 # circumvent udp related NAT issues
382 # http://forum.ipfire.org/index.php?topic=11127.0
e7204c2d
MT
383 if [ -n "${IFACE}" ]; then
384 iptables -F REDFORWARD
385 iptables -A REDFORWARD -o "${IFACE}" -j DROP
386 fi
4b12aa41
TE
387
388 # Reload all rules.
389 /usr/local/bin/firewallctrl
390}
391
ff4770c7
AM
392# See how we were called.
393case "$1" in
394 start)
cdb725da
MT
395 boot_mesg "Loading firewall modules into the kernel"
396 modprobe iptable_nat || failed=1
397 for i in $(find /lib/modules/$(uname -r) -name nf_conntrack*); do
398 modprobe $(basename $i | cut -d. -f1) || failed=1
399 done
400 for i in $(find /lib/modules/$(uname -r) -name nf_nat*); do
401 modprobe $(basename $i | cut -d. -f1) || failed=1
402 done
403 (exit ${failed})
404 evaluate_retval
405
406 if [ -e /var/ipfire/main/disable_nf_sip ]; then
407 rmmod nf_nat_sip
408 rmmod nf_conntrack_sip
409 rmmod nf_nat_h323
410 rmmod nf_conntrack_h323
411 fi
412
7d7740a4 413 boot_mesg "Setting up firewall"
ff4770c7 414 iptables_init
7d7740a4 415 evaluate_retval
6be0579b 416 ;;
4b12aa41 417 reload|up)
7d7740a4 418 boot_mesg "Reloading firewall"
4b12aa41 419 iptables_red_up
7d7740a4 420 evaluate_retval
3a1019f6 421 ;;
4b12aa41
TE
422 down)
423 boot_mesg "Disabling firewall access to RED"
424 iptables_red_down
425 evaluate_retval
426 ;;
3a1019f6 427 restart)
3a1019f6
MT
428 $0 start
429 ;;
430 *)
ff4770c7 431 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
432 exit 1
433 ;;
434esac
435
436exit 0