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