]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/system/firewall
602bd6c5b4ae8df8a68350fe6c1e79955db0189e
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'`
12 if [ -f /var
/ipfire
/red
/device
]; then
13 DEVICE
=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
17 /sbin
/iptables
--wait "$@"
21 # Flush all rules and delete all custom chains
32 iptables
-P INPUT DROP
33 iptables
-P FORWARD DROP
34 iptables
-P OUTPUT ACCEPT
36 # Empty LOG_DROP and LOG_REJECT chains
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
44 # This chain will log, then DROPs packets with certain bad combinations
45 # of flags might indicate a port-scan attempt (xmas, null, etc)
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"
53 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
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
58 if [ "$DROPNEWNOTSYN" == "on" ]; then
59 iptables
-A NEWNOTSYN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_NEWNOTSYN "
61 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
63 # Chain to contain all the rules relating to bad TCP flags
66 # Don't check loopback
67 iptables
-A BADTCP
-i lo
-j RETURN
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
73 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
75 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
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
83 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
85 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
87 iptables
-A INPUT
-p tcp
-j BADTCP
88 iptables
-A FORWARD
-p tcp
-j BADTCP
90 # Connection tracking chains
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
98 # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
101 if [ "${CONNTRACK_SIP}" = "on" ]; then
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
111 if [ "${CONNTRACK_H323}" = "on" ]; then
113 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
114 -m helper
--helper h323
-j ACCEPT
117 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1719 -j CT
--helper RAS
120 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1720 -j CT
--helper Q
.931
124 if [ "${CONNTRACK_FTP}" = "on" ]; then
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
132 if [ "${CONNTRACK_PPTP}" = "on" ]; then
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
140 if [ "${CONNTRACK_TFTP}" = "on" ]; then
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
148 if [ "${CONNTRACK_IRC}" = "on" ]; then
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
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
163 # Fix for braindead ISP's
164 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
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
180 iptables
-A INPUT
-j P2PBLOCK
181 iptables
-A FORWARD
-j P2PBLOCK
182 iptables
-A OUTPUT
-j P2PBLOCK
184 # Guardian (IPS) chains
186 iptables
-A INPUT
-j GUARDIAN
187 iptables
-A FORWARD
-j GUARDIAN
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
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
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
208 # OpenVPN transfer network translation
209 iptables
-t nat
-N OVPNNAT
210 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
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
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
223 # Accept everything on loopback
225 iptables
-A LOOPBACK
-i lo
-j ACCEPT
226 iptables
-A LOOPBACK
-o lo
-j ACCEPT
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
232 for i
in INPUT FORWARD OUTPUT
; do
233 iptables
-A ${i} -j LOOPBACK
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
243 # Accept everything connected
244 for i
in INPUT FORWARD OUTPUT
; do
245 iptables
-A ${i} -j CONNTRACK
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
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
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
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
274 iptables
-N GEOIPBLOCK
275 iptables
-A INPUT
-j GEOIPBLOCK
276 iptables
-A FORWARD
-j GEOIPBLOCK
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
288 # localhost and ethernet.
289 # Always allow accessing the web GUI from GREEN.
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
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
303 iptables
-N OVPNINPUT
304 iptables
-A INPUT
-j OVPNINPUT
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
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
316 for ip
in "${rootserverips[@]}"; do
317 ipset add root-servers
$ip
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
323 # Jump into the actual firewall ruleset.
325 iptables
-A INPUT
-j INPUTFW
327 iptables
-N OUTGOINGFW
328 iptables
-A OUTPUT
-j OUTGOINGFW
330 iptables
-N FORWARDFW
331 iptables
-A FORWARD
-j FORWARDFW
334 iptables
-t nat
-N NAT_SOURCE
335 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
338 iptables
-t nat
-N CAPTIVE_PORTAL
339 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
341 # Custom prerouting chains (for transparent proxy)
342 iptables
-t nat
-N SQUID
343 iptables
-t nat
-A PREROUTING
-j SQUID
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
350 iptables
-t mangle
-N NAT_DESTINATION
351 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
353 iptables
-t nat
-N NAT_DESTINATION_FIX
354 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
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}"
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}"
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}"
371 # upnp chain for our upnp daemon
372 iptables
-t nat
-N UPNPFW
373 iptables
-t nat
-A PREROUTING
-j UPNPFW
375 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
377 # RED chain, used for the red interface
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
385 # Populate IPsec chains
386 /usr
/lib
/firewall
/ipsec-policy
388 # Apply OpenVPN firewall rules
389 /usr
/local
/bin
/openvpnctrl
--firewall-rules
392 /usr
/local
/bin
/wirelessctrl
395 /usr
/local
/bin
/captivectrl
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
405 # Initialize firewall policies.
406 /usr
/sbin
/firewall-policy
408 # Install firewall rules for the red interface.
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
420 iptables
-F REDFORWARD
421 iptables
-t nat
-F REDNAT
423 # PPPoE / PPTP Device
424 if [ "$IFACE" != "" ]; then
426 if [ "$DEVICE" != "" ]; then
427 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
429 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
430 if [ "$RED_DEV" != "" ]; then
431 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
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
442 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
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
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
453 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
454 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
456 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
457 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
460 local NO_MASQ_NETWORKS
462 if [ "${MASQUERADE_GREEN}" = "off" ]; then
463 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
466 if [ "${MASQUERADE_BLUE}" = "off" ]; then
467 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
470 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
471 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
475 for network
in ${NO_MASQ_NETWORKS}; do
476 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
479 # Masquerade everything else
480 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
484 /usr
/local
/bin
/firewallctrl
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
498 /usr
/local
/bin
/firewallctrl
501 # See how we were called.
504 boot_mesg
"Setting up firewall"
509 boot_mesg
"Reloading firewall"
514 boot_mesg
"Disabling firewall access to RED"
522 echo "Usage: $0 {start|reload|restart}"