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