]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/firewall
0d5bb0061514428aa2a428e89a223f2acabeb9ca
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
25 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ppp
/settings
)
26 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
27 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/optionsfw
/settings
)
28 if [ -r "/var/ipfire/tor/settings" ]; then
29 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/tor
/settings
)
31 IFACE
=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
32 if [ -z $IFACE ]; then
36 if [ -f /var
/ipfire
/red
/device
]; then
37 DEVICE
=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
42 IPSET_DB_DIR
="/var/lib/location/ipset"
45 # Allow clients to use Selective ACKs
48 # Allow TCP Timestamps
54 # Maximum Segment Size
59 /sbin
/iptables
--wait "$@"
63 # Flush all rules and delete all custom chains
74 iptables
-P INPUT DROP
75 iptables
-P FORWARD DROP
76 iptables
-P OUTPUT ACCEPT
78 # Enable TRACE logging to syslog
80 sysctl
-q -w net.netfilter.nf_log
.2=nf_log_ipv4
82 # Empty LOG_DROP and LOG_REJECT chains
84 iptables
-A LOG_DROP
-m limit
--limit 10/second
-j LOG
85 iptables
-A LOG_DROP
-j DROP
86 iptables
-N LOG_REJECT
87 iptables
-A LOG_REJECT
-m limit
--limit 10/second
-j LOG
88 iptables
-A LOG_REJECT
-j REJECT
90 # This chain will log, then DROPs packets with certain bad combinations
91 # of flags might indicate a port-scan attempt (xmas, null, etc.)
93 if [ "$DROPPORTSCAN" == "on" ]; then
94 iptables
-A PSCAN
-p tcp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_TCP Scan " -m comment
--comment "DROP_TCP PScan"
95 iptables
-A PSCAN
-p udp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_UDP Scan " -m comment
--comment "DROP_UDP PScan"
96 iptables
-A PSCAN
-p icmp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_ICMP Scan " -m comment
--comment "DROP_ICMP PScan"
97 iptables
-A PSCAN
-f -m limit
--limit 10/second
-j LOG
--log-prefix "DROP_FRAG Scan " -m comment
--comment "DROP_FRAG PScan"
99 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
101 # New tcp packets without SYN set - could well be an obscure type of port scan
102 # that's not covered above, may just be a broken Windows machine
103 iptables
-N NEWNOTSYN
104 if [ "$DROPNEWNOTSYN" == "on" ]; then
105 iptables
-A NEWNOTSYN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_NEWNOTSYN "
107 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
109 # Log and subsequently drop spoofed packets or "martians", arriving from sources
110 # on interfaces where we don't expect them
111 iptables
-N SPOOFED_MARTIAN
112 if [ "$DROPSPOOFEDMARTIAN" == "on" ]; then
113 iptables
-A SPOOFED_MARTIAN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_SPOOFED_MARTIAN "
115 iptables
-A SPOOFED_MARTIAN
-j DROP
-m comment
--comment "DROP_SPOOFED_MARTIAN"
117 # Chain to contain all the rules relating to bad TCP flags
120 # Don't check loopback
121 iptables
-A BADTCP
-i lo
-j RETURN
123 # Disallow packets frequently used by port-scanners
124 # NMAP FIN/URG/PSH (XMAS scan)
125 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
,URG
,PSH
-j PSCAN
126 # SYN/RST/ACK/FIN/URG
127 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
129 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
131 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
-j PSCAN
132 # SYN/RST (also catches xmas variants that set SYN+RST+...)
133 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,RST SYN
,RST
-j PSCAN
134 # SYN/FIN (QueSO or nmap OS probe)
135 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,FIN SYN
,FIN
-j PSCAN
137 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
138 # NEW TCP without SYN
139 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
141 iptables
-A INPUT
-p tcp
-j BADTCP
142 iptables
-A FORWARD
-p tcp
-j BADTCP
144 # Connection tracking chains
145 iptables
-N CTINVALID
146 if [ "$LOGDROPCTINVALID" == "on" ]; then
147 iptables
-A CTINVALID
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_CTINVALID "
149 iptables
-A CTINVALID
-j DROP
-m comment
--comment "DROP_CTINVALID"
152 iptables
-A CTINPUT
-m conntrack
--ctstate ESTABLISHED
-j ACCEPT
153 iptables
-A CTINPUT
-m conntrack
--ctstate INVALID
,UNTRACKED \
154 -p tcp
-j SYNPROXY
"${SYNPROXY_OPTIONS[@]}"
155 iptables
-A CTINPUT
-m conntrack
--ctstate INVALID
-j CTINVALID
156 iptables
-A CTINPUT
-p icmp
-m conntrack
--ctstate RELATED
-j ACCEPT
159 iptables
-A CTOUTPUT
-m conntrack
--ctstate ESTABLISHED
-j ACCEPT
160 iptables
-A CTOUTPUT
-p icmp
-m conntrack
--ctstate RELATED
-j ACCEPT
162 # Restore any connection marks
163 iptables
-t mangle
-A PREROUTING
-m mark
--mark 0 -j CONNMARK
--restore-mark
165 # Fix for braindead ISPs
166 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
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
180 # Chains for networks known as being hostile, posing a technical threat to our users
181 # (i. e. listed at Spamhaus DROP et al.)
183 iptables
-A INPUT
-j HOSTILE
184 iptables
-A FORWARD
-j HOSTILE
185 iptables
-A OUTPUT
-j HOSTILE
187 iptables
-N HOSTILE_DROP_IN
188 if [ "$LOGDROPHOSTILEIN" == "on" ]; then
189 iptables
-A HOSTILE_DROP_IN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_HOSTILE "
191 iptables
-A HOSTILE_DROP_IN
-j DROP
-m comment
--comment "DROP_HOSTILE"
193 iptables
-N HOSTILE_DROP_OUT
194 if [ "$LOGDROPHOSTILEOUT" == "on" ]; then
195 iptables
-A HOSTILE_DROP_OUT
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_HOSTILE "
197 iptables
-A HOSTILE_DROP_OUT
-j DROP
-m comment
--comment "DROP_HOSTILE"
200 # IP Address Blocklist chains
201 iptables
-N BLOCKLISTIN
202 iptables
-N BLOCKLISTOUT
203 iptables
-A INPUT
! -p icmp
-j BLOCKLISTIN
204 iptables
-A FORWARD
! -p icmp
-j BLOCKLISTIN
205 iptables
-A FORWARD
! -p icmp
-j BLOCKLISTOUT
206 iptables
-A OUTPUT
! -p icmp
-j BLOCKLISTOUT
208 # IPS (Guardian) chains
210 iptables
-A INPUT
-j GUARDIAN
211 iptables
-A FORWARD
-j GUARDIAN
213 # Block non-established IPsec networks
214 iptables
-N IPSECBLOCK
215 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
216 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
218 # Block unauthorized WireGuard traffic
220 iptables
-A INPUT
-i wg
+ -j WGBLOCK
221 iptables
-A FORWARD
-i wg
+ -j WGBLOCK
223 # NAT for WireGuard peers
224 iptables
-t nat
-N WGNAT
225 iptables
-t nat
-A POSTROUTING
-j WGNAT
227 # Block OpenVPN transfer networks
228 iptables
-N OVPNBLOCK
229 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
230 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
231 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
233 # OpenVPN transfer network translation
234 iptables
-t nat
-N OVPNNAT
235 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
237 # IPTV chains for IGMPPROXY
238 iptables
-N IPTVINPUT
239 iptables
-A INPUT
-j IPTVINPUT
240 iptables
-N IPTVFORWARD
241 iptables
-A FORWARD
-j IPTVFORWARD
243 # Allow to ping the firewall.
244 iptables
-N ICMPINPUT
245 iptables
-A INPUT
-j ICMPINPUT
246 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
248 # Accept everything on loopback
250 iptables
-A LOOPBACK
-i lo
-j ACCEPT
251 iptables
-A LOOPBACK
-o lo
-j ACCEPT
253 # Filter all packets with loopback addresses on non-loopback interfaces (spoofed)
254 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j SPOOFED_MARTIAN
255 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j SPOOFED_MARTIAN
257 for i
in INPUT FORWARD OUTPUT
; do
258 iptables
-A ${i} -j LOOPBACK
262 iptables
-N CAPTIVE_PORTAL
263 iptables
-N CAPTIVE_PORTAL_CLIENTS
264 for i
in INPUT FORWARD
; do
265 iptables
-A ${i} -j CAPTIVE_PORTAL
268 # Accept everything connected
269 iptables
-A INPUT
-j CTINPUT
270 iptables
-A FORWARD
-j CTINPUT
271 iptables
-A OUTPUT
-j CTOUTPUT
274 iptables
-N DHCPINPUT
275 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
276 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
278 iptables
-N DHCPOUTPUT
279 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
280 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
282 # Allow DHCP on GREEN
283 iptables
-N DHCPGREENINPUT
284 iptables
-N DHCPGREENOUTPUT
285 if [ -n "${GREEN_DEV}" ]; then
286 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
287 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
290 # Allow DHCP on BLUE to be turned on/off
291 iptables
-N DHCPBLUEINPUT
292 iptables
-N DHCPBLUEOUTPUT
293 if [ -n "${BLUE_DEV}" ]; then
294 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
295 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
299 iptables
-N TOR_INPUT
300 iptables
-A INPUT
-j TOR_INPUT
303 iptables
-N LOCATIONBLOCK
304 iptables
-A INPUT
-j LOCATIONBLOCK
305 iptables
-A FORWARD
-j LOCATIONBLOCK
307 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
308 iptables
-N IPSECINPUT
309 iptables
-N IPSECFORWARD
310 iptables
-N IPSECOUTPUT
311 iptables
-A INPUT
-j IPSECINPUT
312 iptables
-A FORWARD
-j IPSECFORWARD
313 iptables
-A OUTPUT
-j IPSECOUTPUT
314 iptables
-t nat
-N IPSECNAT
315 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
317 # localhost and ethernet.
318 # Always allow accessing the web GUI from GREEN.
320 iptables
-A INPUT
-j GUIINPUT
321 if [ -n "${GREEN_DEV}" ]; then
322 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
326 iptables
-N WIRELESSINPUT
327 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
328 iptables
-N WIRELESSFORWARD
329 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
333 iptables
-A INPUT
-j WGINPUT
336 iptables
-N OVPNINPUT
337 iptables
-A INPUT
-j OVPNINPUT
340 iptables
-N TOR_OUTPUT
341 iptables
-A OUTPUT
-j TOR_OUTPUT
343 # Jump into the actual firewall ruleset.
345 iptables
-A INPUT
-j INPUTFW
347 iptables
-N OUTGOINGFW
348 iptables
-A OUTPUT
-j OUTGOINGFW
350 iptables
-N FORWARDFW
351 iptables
-A FORWARD
-j FORWARDFW
354 iptables
-t nat
-N NAT_SOURCE
355 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
358 iptables
-t nat
-N CAPTIVE_PORTAL
359 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
361 # Custom prerouting chains (for transparent proxy)
362 iptables
-t nat
-N SQUID
363 iptables
-t nat
-A PREROUTING
-j SQUID
366 iptables
-t nat
-N NAT_DESTINATION
367 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
368 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
370 iptables
-t mangle
-N NAT_DESTINATION
371 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
373 iptables
-t nat
-N NAT_DESTINATION_FIX
374 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
376 if [ -n "${GREEN_ADDRESS}" ]; then
377 iptables
-t nat
-A NAT_DESTINATION_FIX \
378 -m mark
--mark "0x01000000/${NAT_MASK}" -j SNAT
--to-source "${GREEN_ADDRESS}"
381 if [ -n "${BLUE_ADDRESS}" ]; then
382 iptables
-t nat
-A NAT_DESTINATION_FIX \
383 -m mark
--mark "0x02000000/${NAT_MASK}" -j SNAT
--to-source "${BLUE_ADDRESS}"
386 if [ -n "${ORANGE_ADDRESS}" ]; then
387 iptables
-t nat
-A NAT_DESTINATION_FIX \
388 -m mark
--mark "0x04000000/${NAT_MASK}" -j SNAT
--to-source "${ORANGE_ADDRESS}"
391 # IPS (Suricata) chains
392 iptables
-t mangle
-N IPS
393 iptables
-t mangle
-N IPS_CLEAR
394 iptables
-t mangle
-N IPS_SCAN_IN
395 iptables
-t mangle
-N IPS_SCAN_OUT
397 iptables
-t mangle
-A INPUT
-j IPS_SCAN_IN
398 iptables
-t mangle
-A FORWARD
-j IPS_SCAN_IN
399 iptables
-t mangle
-A FORWARD
-j IPS_SCAN_OUT
400 iptables
-t mangle
-A OUTPUT
-j IPS_SCAN_OUT
402 for chain
in INPUT FORWARD OUTPUT
; do
403 iptables
-t mangle
-A "${chain}" -j IPS
404 iptables
-t mangle
-A "${chain}" -j IPS_CLEAR
407 # RED chain, used for the red interface
409 iptables
-A INPUT
-j REDINPUT
410 iptables
-N REDFORWARD
411 iptables
-A FORWARD
-j REDFORWARD
412 iptables
-t nat
-N REDNAT
413 iptables
-t nat
-A POSTROUTING
-j REDNAT
415 # SYN Flood Protection
416 iptables
-t raw
-N SYN_FLOOD_PROTECT
417 iptables
-t raw
-A PREROUTING
-p tcp
--syn -j SYN_FLOOD_PROTECT
419 # Populate IPsec chains
420 /usr
/lib
/firewall
/ipsec-policy
422 # Apply OpenVPN firewall rules
423 /usr
/local
/bin
/openvpnctrl
--firewall-rules
426 /usr
/local
/bin
/wirelessctrl
429 /usr
/local
/bin
/captivectrl
431 # If a Tor relay is enabled apply firewall rules
432 if [ "${TOR_RELAY_ENABLED}" = "on" -a -n "${TOR_RELAY_PORT}" ]; then
433 /usr
/local
/bin
/torctrl restart
1> /dev
/null
438 iptables
-A INPUT
-j POLICYIN
439 iptables
-N POLICYFWD
440 iptables
-A FORWARD
-j POLICYFWD
441 iptables
-N POLICYOUT
442 iptables
-A OUTPUT
-j POLICYOUT
444 # Initialize firewall policies.
445 /usr
/sbin
/firewall-policy
447 # Install firewall rules for the red interface.
450 # If red has not been brought up yet, we will
451 # add the blocking rules for MASQUERADE
452 if [ ! -e "/var/ipfire/red/active" ]; then
459 iptables
-F REDFORWARD
460 iptables
-t nat
-F REDNAT
462 # Prohibit spoofing our own IP address on RED
463 if [ -f /var
/ipfire
/red
/active
]; then
464 REDIP
="$( cat /var/ipfire/red/local-ipaddress )";
466 if [ "$IFACE" != "" ]; then
467 iptables
-A REDINPUT
-s $REDIP -i $IFACE -j SPOOFED_MARTIAN
468 elif [ "$DEVICE" != "" ]; then
469 iptables
-A REDINPUT
-s $REDIP -i $DEVICE -j SPOOFED_MARTIAN
474 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
475 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
476 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
479 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
481 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
482 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
483 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
485 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
486 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
487 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
490 # Outgoing masquerading (don't masqerade IPsec)
491 iptables
-t nat
-A REDNAT
-m policy
--pol ipsec
--dir=out
-o "${IFACE}" -j RETURN
493 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
494 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
497 local NO_MASQ_NETWORKS
=()
499 if [ "${MASQUERADE_GREEN}" = "off" ]; then
500 NO_MASQ_NETWORKS
+=( "${GREEN_NETADDRESS}/${GREEN_NETMASK}" )
503 if [ "${MASQUERADE_BLUE}" = "off" ]; then
504 NO_MASQ_NETWORKS
+=( "${BLUE_NETADDRESS}/${BLUE_NETMASK}" )
507 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
508 NO_MASQ_NETWORKS
+=( "${ORANGE_NETADDRESS}/${ORANGE_NETMASK}" )
512 for alias in $
(get_aliases
); do
513 NO_MASQ_NETWORKS
+=( "${alias}" )
517 for network
in ${NO_MASQ_NETWORKS[@]}; do
518 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
521 # Masquerade everything else
522 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
526 /usr
/local
/bin
/firewallctrl
529 iptables_red_down
() {
530 # Prohibit packets to reach the masquerading rule
531 # while the WAN interface is down - this is required to
532 # circumvent udp related NAT issues
533 # http://forum.ipfire.org/index.php?topic=11127.0
534 if [ -n "${IFACE}" ]; then
535 iptables
-F REDFORWARD
536 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
540 /usr
/local
/bin
/firewallctrl
543 # See how we were called.
546 boot_mesg
"Setting up firewall"
551 boot_mesg
"Reloading firewall"
556 boot_mesg
"Disabling firewall access to RED"
564 echo "Usage: $0 {start|reload|restart}"