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