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