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