]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/system/firewall
adb2240bbedbc8feecb4ef0b0fa73db3d6f5fd7c
[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 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 CONNTRACK
123 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED -j ACCEPT
124 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j LOG_DROP
125 iptables -A CONNTRACK -p icmp -m conntrack --ctstate RELATED -j ACCEPT
126
127 # Restore any connection marks
128 iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
129
130 # Fix for braindead ISPs
131 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
132
133 # CUSTOM chains, can be used by the users themselves
134 iptables -N CUSTOMINPUT
135 iptables -A INPUT -j CUSTOMINPUT
136 iptables -N CUSTOMFORWARD
137 iptables -A FORWARD -j CUSTOMFORWARD
138 iptables -N CUSTOMOUTPUT
139 iptables -A OUTPUT -j CUSTOMOUTPUT
140 iptables -t nat -N CUSTOMPREROUTING
141 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
142 iptables -t nat -N CUSTOMPOSTROUTING
143 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
144
145 # Log and drop any traffic from and to networks known as being hostile, posing
146 # a technical threat to our users (i. e. listed at Spamhaus DROP et al.)
147 iptables -N HOSTILE
148 if [ "$DROPHOSTILE" == "on" ]; then
149 iptables -A HOSTILE -m limit --limit 10/second -j LOG --log-prefix "DROP_HOSTILE "
150 iptables -A INPUT -i $IFACE -m set --match-set CC_XD src -j HOSTILE
151 iptables -A FORWARD -i $IFACE -m set --match-set CC_XD src -j HOSTILE
152 iptables -A FORWARD -o $IFACE -m set --match-set CC_XD dst -j HOSTILE
153 iptables -A OUTPUT -o $IFACE -m set --match-set CC_XD src -j HOSTILE
154 fi
155 iptables -A HOSTILE -j DROP -m comment --comment "DROP_HOSTILE"
156
157 # IPS (Guardian) chains
158 iptables -N GUARDIAN
159 iptables -A INPUT -j GUARDIAN
160 iptables -A FORWARD -j GUARDIAN
161
162 # Block non-established IPsec networks
163 iptables -N IPSECBLOCK
164 iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
165 iptables -A OUTPUT -m policy --dir out --pol none -j IPSECBLOCK
166
167 # Block OpenVPN transfer networks
168 iptables -N OVPNBLOCK
169 iptables -A INPUT -i tun+ -j OVPNBLOCK
170 iptables -A FORWARD -i tun+ -j OVPNBLOCK
171 iptables -A FORWARD -o tun+ -j OVPNBLOCK
172
173 # IPS (Suricata) chains
174 iptables -N IPS_INPUT
175 iptables -N IPS_FORWARD
176 iptables -N IPS_OUTPUT
177
178 for chain in INPUT FORWARD OUTPUT; do
179 iptables -A "${chain}" -m mark --mark "0x0/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j "IPS_${chain}"
180 done
181
182 # OpenVPN transfer network translation
183 iptables -t nat -N OVPNNAT
184 iptables -t nat -A POSTROUTING -j OVPNNAT
185
186 # IPTV chains for IGMPPROXY
187 iptables -N IPTVINPUT
188 iptables -A INPUT -j IPTVINPUT
189 iptables -N IPTVFORWARD
190 iptables -A FORWARD -j IPTVFORWARD
191
192 # Allow to ping the firewall.
193 iptables -N ICMPINPUT
194 iptables -A INPUT -j ICMPINPUT
195 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
196
197 # Accept everything on loopback
198 iptables -N LOOPBACK
199 iptables -A LOOPBACK -i lo -j ACCEPT
200 iptables -A LOOPBACK -o lo -j ACCEPT
201
202 # Filter all packets with loopback addresses on non-loopback interfaces (spoofed)
203 iptables -A LOOPBACK -s 127.0.0.0/8 -j SPOOFED_MARTIAN
204 iptables -A LOOPBACK -d 127.0.0.0/8 -j SPOOFED_MARTIAN
205
206 for i in INPUT FORWARD OUTPUT; do
207 iptables -A ${i} -j LOOPBACK
208 done
209
210 # Captive portal
211 iptables -N CAPTIVE_PORTAL
212 iptables -N CAPTIVE_PORTAL_CLIENTS
213 for i in INPUT FORWARD; do
214 iptables -A ${i} -j CAPTIVE_PORTAL
215 done
216
217 # Accept everything connected
218 for i in INPUT FORWARD OUTPUT; do
219 iptables -A ${i} -j CONNTRACK
220 done
221
222 # Allow DHCP
223 iptables -N DHCPINPUT
224 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
225 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
226
227 iptables -N DHCPOUTPUT
228 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
229 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
230
231 # Allow DHCP on GREEN
232 iptables -N DHCPGREENINPUT
233 iptables -N DHCPGREENOUTPUT
234 if [ -n "${GREEN_DEV}" ]; then
235 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
236 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
237 fi
238
239 # Allow DHCP on BLUE to be turned on/off
240 iptables -N DHCPBLUEINPUT
241 iptables -N DHCPBLUEOUTPUT
242 if [ -n "${BLUE_DEV}" ]; then
243 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
244 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
245 fi
246
247 # Tor (inbound)
248 iptables -N TOR_INPUT
249 iptables -A INPUT -j TOR_INPUT
250
251 # Location Block
252 iptables -N LOCATIONBLOCK
253 iptables -A INPUT -j LOCATIONBLOCK
254 iptables -A FORWARD -j LOCATIONBLOCK
255
256 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
257 iptables -N IPSECINPUT
258 iptables -N IPSECFORWARD
259 iptables -N IPSECOUTPUT
260 iptables -A INPUT -j IPSECINPUT
261 iptables -A FORWARD -j IPSECFORWARD
262 iptables -A OUTPUT -j IPSECOUTPUT
263 iptables -t nat -N IPSECNAT
264 iptables -t nat -A POSTROUTING -j IPSECNAT
265
266 # localhost and ethernet.
267 # Always allow accessing the web GUI from GREEN.
268 iptables -N GUIINPUT
269 iptables -A INPUT -j GUIINPUT
270 if [ -n "${GREEN_DEV}" ]; then
271 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
272 fi
273
274 # WIRELESS chains
275 iptables -N WIRELESSINPUT
276 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
277 iptables -N WIRELESSFORWARD
278 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
279
280 # OpenVPN
281 iptables -N OVPNINPUT
282 iptables -A INPUT -j OVPNINPUT
283
284 # Tor (outbound)
285 iptables -N TOR_OUTPUT
286 iptables -A OUTPUT -j TOR_OUTPUT
287
288 # Jump into the actual firewall ruleset.
289 iptables -N INPUTFW
290 iptables -A INPUT -j INPUTFW
291
292 iptables -N OUTGOINGFW
293 iptables -A OUTPUT -j OUTGOINGFW
294
295 iptables -N FORWARDFW
296 iptables -A FORWARD -j FORWARDFW
297
298 # SNAT rules
299 iptables -t nat -N NAT_SOURCE
300 iptables -t nat -A POSTROUTING -j NAT_SOURCE
301
302 # Captive Portal
303 iptables -t nat -N CAPTIVE_PORTAL
304 iptables -t nat -A PREROUTING -j CAPTIVE_PORTAL
305
306 # Custom prerouting chains (for transparent proxy)
307 iptables -t nat -N SQUID
308 iptables -t nat -A PREROUTING -j SQUID
309
310 # DNAT rules
311 iptables -t nat -N NAT_DESTINATION
312 iptables -t nat -A PREROUTING -j NAT_DESTINATION
313 iptables -t nat -A OUTPUT -j NAT_DESTINATION
314
315 iptables -t mangle -N NAT_DESTINATION
316 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
317
318 iptables -t nat -N NAT_DESTINATION_FIX
319 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
320
321 if [ -n "${GREEN_ADDRESS}" ]; then
322 iptables -t nat -A NAT_DESTINATION_FIX \
323 -m mark --mark "0x01000000/${NAT_MASK}" -j SNAT --to-source "${GREEN_ADDRESS}"
324 fi
325
326 if [ -n "${BLUE_ADDRESS}" ]; then
327 iptables -t nat -A NAT_DESTINATION_FIX \
328 -m mark --mark "0x02000000/${NAT_MASK}" -j SNAT --to-source "${BLUE_ADDRESS}"
329 fi
330
331 if [ -n "${ORANGE_ADDRESS}" ]; then
332 iptables -t nat -A NAT_DESTINATION_FIX \
333 -m mark --mark "0x04000000/${NAT_MASK}" -j SNAT --to-source "${ORANGE_ADDRESS}"
334 fi
335
336 # RED chain, used for the red interface
337 iptables -N REDINPUT
338 iptables -A INPUT -j REDINPUT
339 iptables -N REDFORWARD
340 iptables -A FORWARD -j REDFORWARD
341 iptables -t nat -N REDNAT
342 iptables -t nat -A POSTROUTING -j REDNAT
343
344 # Populate IPsec chains
345 /usr/lib/firewall/ipsec-policy
346
347 # Apply OpenVPN firewall rules
348 /usr/local/bin/openvpnctrl --firewall-rules
349
350 # run wirelessctrl
351 /usr/local/bin/wirelessctrl
352
353 # run captivectrl
354 /usr/local/bin/captivectrl
355
356 # POLICY CHAIN
357 iptables -N POLICYIN
358 iptables -A INPUT -j POLICYIN
359 iptables -N POLICYFWD
360 iptables -A FORWARD -j POLICYFWD
361 iptables -N POLICYOUT
362 iptables -A OUTPUT -j POLICYOUT
363
364 # Initialize firewall policies.
365 /usr/sbin/firewall-policy
366
367 # Install firewall rules for the red interface.
368 iptables_red_up
369
370 # If red has not been brought up yet, we will
371 # add the blocking rules for MASQUERADE
372 if [ ! -e "/var/ipfire/red/active" ]; then
373 iptables_red_down
374 fi
375 }
376
377 iptables_red_up() {
378 iptables -F REDINPUT
379 iptables -F REDFORWARD
380 iptables -t nat -F REDNAT
381
382 # Prohibit spoofing our own IP address on RED
383 if [ -f /var/ipfire/red/active ]; then
384 REDIP="$( cat /var/ipfire/red/local-ipaddress )";
385
386 if [ "$IFACE" != "" ]; then
387 iptables -A REDINPUT -s $REDIP -i $IFACE -j SPOOFED_MARTIAN
388 elif [ "$DEVICE" != "" ]; then
389 iptables -A REDINPUT -s $REDIP -i $DEVICE -j SPOOFED_MARTIAN
390 fi
391 fi
392
393 # PPPoE / PPTP Device
394 if [ "$IFACE" != "" ]; then
395 # PPPoE / PPTP
396 if [ "$DEVICE" != "" ]; then
397 iptables -A REDINPUT -i $DEVICE -j ACCEPT
398 fi
399 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
400 if [ "$RED_DEV" != "" ]; then
401 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
402 fi
403 fi
404 fi
405
406 # PPTP over DHCP
407 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
408 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
409 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
410 fi
411
412 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
413 # DHCP
414 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
415 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
416 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
417 fi
418 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
419 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
420 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
421 fi
422
423 # Outgoing masquerading (don't masqerade IPsec)
424 iptables -t nat -A REDNAT -m policy --pol ipsec --dir=out -o "${IFACE}" -j RETURN
425
426 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
427 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
428 fi
429
430 local NO_MASQ_NETWORKS
431
432 if [ "${MASQUERADE_GREEN}" = "off" ]; then
433 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
434 fi
435
436 if [ "${MASQUERADE_BLUE}" = "off" ]; then
437 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
438 fi
439
440 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
441 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
442 fi
443
444 local network
445 for network in ${NO_MASQ_NETWORKS}; do
446 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
447 done
448
449 # Masquerade everything else
450 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
451 fi
452
453 # Reload all rules.
454 /usr/local/bin/firewallctrl
455 }
456
457 iptables_red_down() {
458 # Prohibit packets to reach the masquerading rule
459 # while the WAN interface is down - this is required to
460 # circumvent udp related NAT issues
461 # http://forum.ipfire.org/index.php?topic=11127.0
462 if [ -n "${IFACE}" ]; then
463 iptables -F REDFORWARD
464 iptables -A REDFORWARD -o "${IFACE}" -j DROP
465 fi
466
467 # Reload all rules.
468 /usr/local/bin/firewallctrl
469 }
470
471 # See how we were called.
472 case "$1" in
473 start)
474 boot_mesg "Setting up firewall"
475 iptables_init
476 evaluate_retval
477 ;;
478 reload|up)
479 boot_mesg "Reloading firewall"
480 iptables_red_up
481 evaluate_retval
482 ;;
483 down)
484 boot_mesg "Disabling firewall access to RED"
485 iptables_red_down
486 evaluate_retval
487 ;;
488 restart)
489 $0 start
490 ;;
491 *)
492 echo "Usage: $0 {start|reload|restart}"
493 exit 1
494 ;;
495 esac
496
497 exit 0