]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/firewall
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / src / initscripts / system / firewall
1 #!/bin/sh
2
3 . /etc/sysconfig/rc
4 . ${rc_functions}
5
6 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
8 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
9 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10 if [ -z $IFACE ]; then
11 IFACE="red0"
12 fi
13
14 if [ -f /var/ipfire/red/device ]; then
15 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
16 fi
17
18 NAT_MASK="0x0f000000"
19
20 IPS_REPEAT_MARK="0x80000000"
21 IPS_REPEAT_MASK="0x80000000"
22 IPS_BYPASS_MARK="0x40000000"
23 IPS_BYPASS_MASK="0x40000000"
24
25 function iptables() {
26 /sbin/iptables --wait "$@"
27 }
28
29 iptables_init() {
30 # Flush all rules and delete all custom chains
31 iptables -F
32 iptables -t nat -F
33 iptables -t mangle -F
34 iptables -t raw -F
35 iptables -X
36 iptables -t nat -X
37 iptables -t mangle -X
38 iptables -t raw -X
39
40 # Set up policies
41 iptables -P INPUT DROP
42 iptables -P FORWARD DROP
43 iptables -P OUTPUT ACCEPT
44
45 # Enable TRACE logging to syslog
46 modprobe nf_log_ipv4
47 sysctl -q -w net.netfilter.nf_log.2=nf_log_ipv4
48
49 # IPS Bypass Chain which stores the BYPASS bit in connection tracking
50 iptables -N IPSBYPASS
51 iptables -A IPSBYPASS -j CONNMARK --save-mark --mask "$(( ~IPS_REPEAT_MASK & 0xffffffff ))"
52
53 # Jump into bypass chain when the BYPASS bit is set
54 for chain in INPUT FORWARD OUTPUT; do
55 iptables -A "${chain}" -m mark \
56 --mark "$(( IPS_REPEAT_MARK | IPS_BYPASS_MARK ))/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j IPSBYPASS
57 done
58
59 # Empty LOG_DROP and LOG_REJECT chains
60 iptables -N LOG_DROP
61 iptables -A LOG_DROP -m limit --limit 10/second -j LOG
62 iptables -A LOG_DROP -j DROP
63 iptables -N LOG_REJECT
64 iptables -A LOG_REJECT -m limit --limit 10/second -j LOG
65 iptables -A LOG_REJECT -j REJECT
66
67 # This chain will log, then DROPs packets with certain bad combinations
68 # of flags might indicate a port-scan attempt (xmas, null, etc.)
69 iptables -N PSCAN
70 if [ "$DROPPORTSCAN" == "on" ]; then
71 iptables -A PSCAN -p tcp -m limit --limit 10/second -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
72 iptables -A PSCAN -p udp -m limit --limit 10/second -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
73 iptables -A PSCAN -p icmp -m limit --limit 10/second -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
74 iptables -A PSCAN -f -m limit --limit 10/second -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
75 fi
76 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
77
78 # New tcp packets without SYN set - could well be an obscure type of port scan
79 # that's not covered above, may just be a broken Windows machine
80 iptables -N NEWNOTSYN
81 if [ "$DROPNEWNOTSYN" == "on" ]; then
82 iptables -A NEWNOTSYN -m limit --limit 10/second -j LOG --log-prefix "DROP_NEWNOTSYN "
83 fi
84 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
85
86 # Log and subsequently drop spoofed packets or "martians", arriving from sources
87 # on interfaces where we don't expect them
88 iptables -N SPOOFED_MARTIAN
89 if [ "$DROPSPOOFEDMARTIAN" == "on" ]; then
90 iptables -A SPOOFED_MARTIAN -m limit --limit 10/second -j LOG --log-prefix "DROP_SPOOFED_MARTIAN "
91 fi
92 iptables -A SPOOFED_MARTIAN -j DROP -m comment --comment "DROP_SPOOFED_MARTIAN"
93
94 # Chain to contain all the rules relating to bad TCP flags
95 iptables -N BADTCP
96
97 # Don't check loopback
98 iptables -A BADTCP -i lo -j RETURN
99
100 # Disallow packets frequently used by port-scanners
101 # NMAP FIN/URG/PSH (XMAS scan)
102 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
103 # SYN/RST/ACK/FIN/URG
104 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
105 # ALL/ALL
106 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
107 # FIN Stealth
108 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
109 # SYN/RST (also catches xmas variants that set SYN+RST+...)
110 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
111 # SYN/FIN (QueSO or nmap OS probe)
112 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
113 # Null
114 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
115 # NEW TCP without SYN
116 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
117
118 iptables -A INPUT -p tcp -j BADTCP
119 iptables -A FORWARD -p tcp -j BADTCP
120
121 # Connection tracking chains
122 iptables -N CTINVALID
123 if [ "$LOGDROPCTINVALID" == "on" ]; then
124 iptables -A CTINVALID -m limit --limit 10/second -j LOG --log-prefix "DROP_CTINVALID "
125 fi
126 iptables -A CTINVALID -j DROP -m comment --comment "DROP_CTINVALID"
127
128 iptables -N CONNTRACK
129 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED -j ACCEPT
130 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j CTINVALID
131 iptables -A CONNTRACK -p icmp -m conntrack --ctstate RELATED -j ACCEPT
132
133 # Restore any connection marks
134 iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
135
136 # Fix for braindead ISPs
137 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
138
139 # CUSTOM chains, can be used by the users themselves
140 iptables -N CUSTOMINPUT
141 iptables -A INPUT -j CUSTOMINPUT
142 iptables -N CUSTOMFORWARD
143 iptables -A FORWARD -j CUSTOMFORWARD
144 iptables -N CUSTOMOUTPUT
145 iptables -A OUTPUT -j CUSTOMOUTPUT
146 iptables -t nat -N CUSTOMPREROUTING
147 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
148 iptables -t nat -N CUSTOMPOSTROUTING
149 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
150
151 # Log and drop any traffic from and to networks known as being hostile, posing
152 # a technical threat to our users (i. e. listed at Spamhaus DROP et al.)
153 iptables -N HOSTILE
154 if [ "$DROPHOSTILE" == "on" ]; then
155 iptables -A HOSTILE -m limit --limit 10/second -j LOG --log-prefix "DROP_HOSTILE "
156 iptables -A INPUT -i $IFACE -m geoip --src-cc XD -j HOSTILE
157 iptables -A FORWARD -i $IFACE -m geoip --src-cc XD -j HOSTILE
158 iptables -A FORWARD -o $IFACE -m geoip --dst-cc XD -j HOSTILE
159 iptables -A OUTPUT -o $IFACE -m geoip --src-cc XD -j HOSTILE
160 fi
161 iptables -A HOSTILE -j DROP -m comment --comment "DROP_HOSTILE"
162
163 # P2PBLOCK
164 iptables -N P2PBLOCK
165 iptables -A INPUT -j P2PBLOCK
166 iptables -A FORWARD -j P2PBLOCK
167 iptables -A OUTPUT -j P2PBLOCK
168
169 # IPS (Guardian) chains
170 iptables -N GUARDIAN
171 iptables -A INPUT -j GUARDIAN
172 iptables -A FORWARD -j GUARDIAN
173
174 # Block non-established IPsec networks
175 iptables -N IPSECBLOCK
176 iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
177 iptables -A OUTPUT -m policy --dir out --pol none -j IPSECBLOCK
178
179 # Block OpenVPN transfer networks
180 iptables -N OVPNBLOCK
181 iptables -A INPUT -i tun+ -j OVPNBLOCK
182 iptables -A FORWARD -i tun+ -j OVPNBLOCK
183 iptables -A FORWARD -o tun+ -j OVPNBLOCK
184
185 # IPS (Suricata) chains
186 iptables -N IPS_INPUT
187 iptables -N IPS_FORWARD
188 iptables -N IPS_OUTPUT
189
190 for chain in INPUT FORWARD OUTPUT; do
191 iptables -A "${chain}" -m mark --mark "0x0/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j "IPS_${chain}"
192 done
193
194 # OpenVPN transfer network translation
195 iptables -t nat -N OVPNNAT
196 iptables -t nat -A POSTROUTING -j OVPNNAT
197
198 # IPTV chains for IGMPPROXY
199 iptables -N IPTVINPUT
200 iptables -A INPUT -j IPTVINPUT
201 iptables -N IPTVFORWARD
202 iptables -A FORWARD -j IPTVFORWARD
203
204 # Allow to ping the firewall.
205 iptables -N ICMPINPUT
206 iptables -A INPUT -j ICMPINPUT
207 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
208
209 # Accept everything on loopback
210 iptables -N LOOPBACK
211 iptables -A LOOPBACK -i lo -j ACCEPT
212 iptables -A LOOPBACK -o lo -j ACCEPT
213
214 # Filter all packets with loopback addresses on non-loopback interfaces (spoofed)
215 iptables -A LOOPBACK -s 127.0.0.0/8 -j SPOOFED_MARTIAN
216 iptables -A LOOPBACK -d 127.0.0.0/8 -j SPOOFED_MARTIAN
217
218 for i in INPUT FORWARD OUTPUT; do
219 iptables -A ${i} -j LOOPBACK
220 done
221
222 # Captive portal
223 iptables -N CAPTIVE_PORTAL
224 iptables -N CAPTIVE_PORTAL_CLIENTS
225 for i in INPUT FORWARD; do
226 iptables -A ${i} -j CAPTIVE_PORTAL
227 done
228
229 # Accept everything connected
230 for i in INPUT FORWARD OUTPUT; do
231 iptables -A ${i} -j CONNTRACK
232 done
233
234 # Allow DHCP
235 iptables -N DHCPINPUT
236 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
237 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
238
239 iptables -N DHCPOUTPUT
240 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
241 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
242
243 # Allow DHCP on GREEN
244 iptables -N DHCPGREENINPUT
245 iptables -N DHCPGREENOUTPUT
246 if [ -n "${GREEN_DEV}" ]; then
247 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
248 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
249 fi
250
251 # Allow DHCP on BLUE to be turned on/off
252 iptables -N DHCPBLUEINPUT
253 iptables -N DHCPBLUEOUTPUT
254 if [ -n "${BLUE_DEV}" ]; then
255 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
256 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
257 fi
258
259 # Tor (inbound)
260 iptables -N TOR_INPUT
261 iptables -A INPUT -j TOR_INPUT
262
263 # Location Block
264 iptables -N LOCATIONBLOCK
265 iptables -A INPUT -j LOCATIONBLOCK
266 iptables -A FORWARD -j LOCATIONBLOCK
267
268 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
269 iptables -N IPSECINPUT
270 iptables -N IPSECFORWARD
271 iptables -N IPSECOUTPUT
272 iptables -A INPUT -j IPSECINPUT
273 iptables -A FORWARD -j IPSECFORWARD
274 iptables -A OUTPUT -j IPSECOUTPUT
275 iptables -t nat -N IPSECNAT
276 iptables -t nat -A POSTROUTING -j IPSECNAT
277
278 # localhost and ethernet.
279 # Always allow accessing the web GUI from GREEN.
280 iptables -N GUIINPUT
281 iptables -A INPUT -j GUIINPUT
282 if [ -n "${GREEN_DEV}" ]; then
283 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
284 fi
285
286 # WIRELESS chains
287 iptables -N WIRELESSINPUT
288 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
289 iptables -N WIRELESSFORWARD
290 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
291
292 # OpenVPN
293 iptables -N OVPNINPUT
294 iptables -A INPUT -j OVPNINPUT
295
296 # Tor (outbound)
297 iptables -N TOR_OUTPUT
298 iptables -A OUTPUT -j TOR_OUTPUT
299
300 # Jump into the actual firewall ruleset.
301 iptables -N INPUTFW
302 iptables -A INPUT -j INPUTFW
303
304 iptables -N OUTGOINGFW
305 iptables -A OUTPUT -j OUTGOINGFW
306
307 iptables -N FORWARDFW
308 iptables -A FORWARD -j FORWARDFW
309
310 # SNAT rules
311 iptables -t nat -N NAT_SOURCE
312 iptables -t nat -A POSTROUTING -j NAT_SOURCE
313
314 # Captive Portal
315 iptables -t nat -N CAPTIVE_PORTAL
316 iptables -t nat -A PREROUTING -j CAPTIVE_PORTAL
317
318 # Custom prerouting chains (for transparent proxy)
319 iptables -t nat -N SQUID
320 iptables -t nat -A PREROUTING -j SQUID
321
322 # DNAT rules
323 iptables -t nat -N NAT_DESTINATION
324 iptables -t nat -A PREROUTING -j NAT_DESTINATION
325 iptables -t nat -A OUTPUT -j NAT_DESTINATION
326
327 iptables -t mangle -N NAT_DESTINATION
328 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
329
330 iptables -t nat -N NAT_DESTINATION_FIX
331 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
332
333 if [ -n "${GREEN_ADDRESS}" ]; then
334 iptables -t nat -A NAT_DESTINATION_FIX \
335 -m mark --mark "0x01000000/${NAT_MASK}" -j SNAT --to-source "${GREEN_ADDRESS}"
336 fi
337
338 if [ -n "${BLUE_ADDRESS}" ]; then
339 iptables -t nat -A NAT_DESTINATION_FIX \
340 -m mark --mark "0x02000000/${NAT_MASK}" -j SNAT --to-source "${BLUE_ADDRESS}"
341 fi
342
343 if [ -n "${ORANGE_ADDRESS}" ]; then
344 iptables -t nat -A NAT_DESTINATION_FIX \
345 -m mark --mark "0x04000000/${NAT_MASK}" -j SNAT --to-source "${ORANGE_ADDRESS}"
346 fi
347
348 # RED chain, used for the red interface
349 iptables -N REDINPUT
350 iptables -A INPUT -j REDINPUT
351 iptables -N REDFORWARD
352 iptables -A FORWARD -j REDFORWARD
353 iptables -t nat -N REDNAT
354 iptables -t nat -A POSTROUTING -j REDNAT
355
356 # Populate IPsec chains
357 /usr/lib/firewall/ipsec-policy
358
359 # Apply OpenVPN firewall rules
360 /usr/local/bin/openvpnctrl --firewall-rules
361
362 # run wirelessctrl
363 /usr/local/bin/wirelessctrl
364
365 # run captivectrl
366 /usr/local/bin/captivectrl
367
368 # POLICY CHAIN
369 iptables -N POLICYIN
370 iptables -A INPUT -j POLICYIN
371 iptables -N POLICYFWD
372 iptables -A FORWARD -j POLICYFWD
373 iptables -N POLICYOUT
374 iptables -A OUTPUT -j POLICYOUT
375
376 # Initialize firewall policies.
377 /usr/sbin/firewall-policy
378
379 # Install firewall rules for the red interface.
380 iptables_red_up
381
382 # If red has not been brought up yet, we will
383 # add the blocking rules for MASQUERADE
384 if [ ! -e "/var/ipfire/red/active" ]; then
385 iptables_red_down
386 fi
387 }
388
389 iptables_red_up() {
390 iptables -F REDINPUT
391 iptables -F REDFORWARD
392 iptables -t nat -F REDNAT
393
394 # Prohibit spoofing our own IP address on RED
395 if [ -f /var/ipfire/red/active ]; then
396 REDIP="$( cat /var/ipfire/red/local-ipaddress )";
397
398 if [ "$IFACE" != "" ]; then
399 iptables -A REDINPUT -s $REDIP -i $IFACE -j SPOOFED_MARTIAN
400 elif [ "$DEVICE" != "" ]; then
401 iptables -A REDINPUT -s $REDIP -i $DEVICE -j SPOOFED_MARTIAN
402 fi
403 fi
404
405 # PPPoE / PPTP Device
406 if [ "$IFACE" != "" ]; then
407 # PPPoE / PPTP
408 if [ "$DEVICE" != "" ]; then
409 iptables -A REDINPUT -i $DEVICE -j ACCEPT
410 fi
411 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
412 if [ "$RED_DEV" != "" ]; then
413 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
414 fi
415 fi
416 fi
417
418 # PPTP over DHCP
419 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
420 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
421 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
422 fi
423
424 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
425 # DHCP
426 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
427 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
428 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
429 fi
430 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
431 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
432 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
433 fi
434
435 # Outgoing masquerading (don't masqerade IPsec)
436 iptables -t nat -A REDNAT -m policy --pol ipsec --dir=out -o "${IFACE}" -j RETURN
437
438 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
439 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
440 fi
441
442 local NO_MASQ_NETWORKS
443
444 if [ "${MASQUERADE_GREEN}" = "off" ]; then
445 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
446 fi
447
448 if [ "${MASQUERADE_BLUE}" = "off" ]; then
449 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
450 fi
451
452 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
453 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
454 fi
455
456 local network
457 for network in ${NO_MASQ_NETWORKS}; do
458 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
459 done
460
461 # Masquerade everything else
462 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
463 fi
464
465 # Reload all rules.
466 /usr/local/bin/firewallctrl
467 }
468
469 iptables_red_down() {
470 # Prohibit packets to reach the masquerading rule
471 # while the WAN interface is down - this is required to
472 # circumvent udp related NAT issues
473 # http://forum.ipfire.org/index.php?topic=11127.0
474 if [ -n "${IFACE}" ]; then
475 iptables -F REDFORWARD
476 iptables -A REDFORWARD -o "${IFACE}" -j DROP
477 fi
478
479 # Reload all rules.
480 /usr/local/bin/firewallctrl
481 }
482
483 # See how we were called.
484 case "$1" in
485 start)
486 boot_mesg "Setting up firewall"
487 iptables_init
488 evaluate_retval
489 ;;
490 reload|up)
491 boot_mesg "Reloading firewall"
492 iptables_red_up
493 evaluate_retval
494 ;;
495 down)
496 boot_mesg "Disabling firewall access to RED"
497 iptables_red_down
498 evaluate_retval
499 ;;
500 restart)
501 $0 start
502 ;;
503 *)
504 echo "Usage: $0 {start|reload|restart}"
505 exit 1
506 ;;
507 esac
508
509 exit 0