]> git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blob - src/initscripts/system/firewall
Merge branch 'master' into next
[people/mfischer/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 # Location Block
280 iptables -N LOCATIONBLOCK
281 iptables -A INPUT -j LOCATIONBLOCK
282 iptables -A FORWARD -j LOCATIONBLOCK
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 # RED chain, used for the red interface
367 iptables -N REDINPUT
368 iptables -A INPUT -j REDINPUT
369 iptables -N REDFORWARD
370 iptables -A FORWARD -j REDFORWARD
371 iptables -t nat -N REDNAT
372 iptables -t nat -A POSTROUTING -j REDNAT
373
374 # Populate IPsec chains
375 /usr/lib/firewall/ipsec-policy
376
377 # Apply OpenVPN firewall rules
378 /usr/local/bin/openvpnctrl --firewall-rules
379
380 # run wirelessctrl
381 /usr/local/bin/wirelessctrl
382
383 # run captivectrl
384 /usr/local/bin/captivectrl
385
386 # POLICY CHAIN
387 iptables -N POLICYIN
388 iptables -A INPUT -j POLICYIN
389 iptables -N POLICYFWD
390 iptables -A FORWARD -j POLICYFWD
391 iptables -N POLICYOUT
392 iptables -A OUTPUT -j POLICYOUT
393
394 # Initialize firewall policies.
395 /usr/sbin/firewall-policy
396
397 # Install firewall rules for the red interface.
398 iptables_red_up
399
400 # If red has not been brought up yet, we will
401 # add the blocking rules for MASQUERADE
402 if [ ! -e "/var/ipfire/red/active" ]; then
403 iptables_red_down
404 fi
405 }
406
407 iptables_red_up() {
408 iptables -F REDINPUT
409 iptables -F REDFORWARD
410 iptables -t nat -F REDNAT
411
412 # PPPoE / PPTP Device
413 if [ "$IFACE" != "" ]; then
414 # PPPoE / PPTP
415 if [ "$DEVICE" != "" ]; then
416 iptables -A REDINPUT -i $DEVICE -j ACCEPT
417 fi
418 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
419 if [ "$RED_DEV" != "" ]; then
420 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
421 fi
422 fi
423 fi
424
425 # PPTP over DHCP
426 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
427 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
428 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
429 fi
430
431 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
432 # DHCP
433 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
434 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
435 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
436 fi
437 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
438 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
439 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
440 fi
441
442 # Outgoing masquerading (don't masqerade IPsec (mark 50))
443 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
444
445 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
446 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
447 fi
448
449 local NO_MASQ_NETWORKS
450
451 if [ "${MASQUERADE_GREEN}" = "off" ]; then
452 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
453 fi
454
455 if [ "${MASQUERADE_BLUE}" = "off" ]; then
456 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
457 fi
458
459 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
460 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
461 fi
462
463 local network
464 for network in ${NO_MASQ_NETWORKS}; do
465 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
466 done
467
468 # Masquerade everything else
469 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
470 fi
471
472 # Reload all rules.
473 /usr/local/bin/firewallctrl
474 }
475
476 iptables_red_down() {
477 # Prohibit packets to reach the masquerading rule
478 # while the WAN interface is down - this is required to
479 # circumvent udp related NAT issues
480 # http://forum.ipfire.org/index.php?topic=11127.0
481 if [ -n "${IFACE}" ]; then
482 iptables -F REDFORWARD
483 iptables -A REDFORWARD -o "${IFACE}" -j DROP
484 fi
485
486 # Reload all rules.
487 /usr/local/bin/firewallctrl
488 }
489
490 # See how we were called.
491 case "$1" in
492 start)
493 boot_mesg "Setting up firewall"
494 iptables_init
495 evaluate_retval
496 ;;
497 reload|up)
498 boot_mesg "Reloading firewall"
499 iptables_red_up
500 evaluate_retval
501 ;;
502 down)
503 boot_mesg "Disabling firewall access to RED"
504 iptables_red_down
505 evaluate_retval
506 ;;
507 restart)
508 $0 start
509 ;;
510 *)
511 echo "Usage: $0 {start|reload|restart}"
512 exit 1
513 ;;
514 esac
515
516 exit 0