]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/firewall
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'`
11 if [ -f /var
/ipfire
/red
/device
]; then
12 DEVICE
=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
16 /sbin
/iptables
--wait "$@"
20 # Flush all rules and delete all custom chains
31 iptables
-P INPUT DROP
32 iptables
-P FORWARD DROP
33 iptables
-P OUTPUT ACCEPT
35 # Enable TRACE logging to syslog
37 sysctl
-q -w net.netfilter.nf_log
.2=nf_log_ipv4
39 # Empty LOG_DROP and LOG_REJECT chains
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
47 # This chain will log, then DROPs packets with certain bad combinations
48 # of flags might indicate a port-scan attempt (xmas, null, etc.)
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"
56 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
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
61 if [ "$DROPNEWNOTSYN" == "on" ]; then
62 iptables
-A NEWNOTSYN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_NEWNOTSYN "
64 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
66 # Chain to contain all the rules relating to bad TCP flags
69 # Don't check loopback
70 iptables
-A BADTCP
-i lo
-j RETURN
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
76 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
78 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
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
86 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
88 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
90 iptables
-A INPUT
-p tcp
-j BADTCP
91 iptables
-A FORWARD
-p tcp
-j BADTCP
93 # Connection tracking chains
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
101 # Fix for braindead ISPs
102 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
104 # CUSTOM chains, can be used by the users themselves
105 iptables
-N CUSTOMINPUT
106 iptables
-A INPUT
-j CUSTOMINPUT
107 iptables
-N CUSTOMFORWARD
108 iptables
-A FORWARD
-j CUSTOMFORWARD
109 iptables
-N CUSTOMOUTPUT
110 iptables
-A OUTPUT
-j CUSTOMOUTPUT
111 iptables
-t nat
-N CUSTOMPREROUTING
112 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
113 iptables
-t nat
-N CUSTOMPOSTROUTING
114 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
118 iptables
-A INPUT
-j P2PBLOCK
119 iptables
-A FORWARD
-j P2PBLOCK
120 iptables
-A OUTPUT
-j P2PBLOCK
122 # IPS (Guardian) chains
124 iptables
-A INPUT
-j GUARDIAN
125 iptables
-A FORWARD
-j GUARDIAN
127 # Block non-established IPsec networks
128 iptables
-N IPSECBLOCK
129 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
130 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
132 # Block OpenVPN transfer networks
133 iptables
-N OVPNBLOCK
134 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
135 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
136 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
138 # IPS (Suricata) chains
139 iptables
-N IPS_INPUT
140 iptables
-N IPS_FORWARD
141 iptables
-N IPS_OUTPUT
142 iptables
-A INPUT
-j IPS_INPUT
143 iptables
-A FORWARD
-j IPS_FORWARD
144 iptables
-A OUTPUT
-j IPS_OUTPUT
146 # OpenVPN transfer network translation
147 iptables
-t nat
-N OVPNNAT
148 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
150 # IPTV chains for IGMPPROXY
151 iptables
-N IPTVINPUT
152 iptables
-A INPUT
-j IPTVINPUT
153 iptables
-N IPTVFORWARD
154 iptables
-A FORWARD
-j IPTVFORWARD
156 # Allow to ping the firewall.
157 iptables
-N ICMPINPUT
158 iptables
-A INPUT
-j ICMPINPUT
159 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
161 # Accept everything on loopback
163 iptables
-A LOOPBACK
-i lo
-j ACCEPT
164 iptables
-A LOOPBACK
-o lo
-j ACCEPT
166 # Filter all packets with loopback addresses on non-loopback interfaces.
167 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
168 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
170 for i
in INPUT FORWARD OUTPUT
; do
171 iptables
-A ${i} -j LOOPBACK
175 iptables
-N CAPTIVE_PORTAL
176 iptables
-N CAPTIVE_PORTAL_CLIENTS
177 for i
in INPUT FORWARD
; do
178 iptables
-A ${i} -j CAPTIVE_PORTAL
181 # Accept everything connected
182 for i
in INPUT FORWARD OUTPUT
; do
183 iptables
-A ${i} -j CONNTRACK
187 iptables
-N DHCPINPUT
188 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
189 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
191 iptables
-N DHCPOUTPUT
192 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
193 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
195 # Allow DHCP on GREEN
196 iptables
-N DHCPGREENINPUT
197 iptables
-N DHCPGREENOUTPUT
198 if [ -n "${GREEN_DEV}" ]; then
199 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
200 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
203 # Allow DHCP on BLUE to be turned on/off
204 iptables
-N DHCPBLUEINPUT
205 iptables
-N DHCPBLUEOUTPUT
206 if [ -n "${BLUE_DEV}" ]; then
207 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
208 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
212 iptables
-N LOCATIONBLOCK
213 iptables
-A INPUT
-j LOCATIONBLOCK
214 iptables
-A FORWARD
-j LOCATIONBLOCK
216 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
217 iptables
-N IPSECINPUT
218 iptables
-N IPSECFORWARD
219 iptables
-N IPSECOUTPUT
220 iptables
-A INPUT
-j IPSECINPUT
221 iptables
-A FORWARD
-j IPSECFORWARD
222 iptables
-A OUTPUT
-j IPSECOUTPUT
223 iptables
-t nat
-N IPSECNAT
224 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
226 # localhost and ethernet.
227 # Always allow accessing the web GUI from GREEN.
229 iptables
-A INPUT
-j GUIINPUT
230 if [ -n "${GREEN_DEV}" ]; then
231 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
235 iptables
-N WIRELESSINPUT
236 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
237 iptables
-N WIRELESSFORWARD
238 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
241 iptables
-N OVPNINPUT
242 iptables
-A INPUT
-j OVPNINPUT
244 # Tor (inbound and outbound)
245 iptables
-N TOR_INPUT
246 iptables
-A INPUT
-j TOR_INPUT
247 iptables
-N TOR_OUTPUT
248 iptables
-A OUTPUT
-j TOR_OUTPUT
250 # Jump into the actual firewall ruleset.
252 iptables
-A INPUT
-j INPUTFW
254 iptables
-N OUTGOINGFW
255 iptables
-A OUTPUT
-j OUTGOINGFW
257 iptables
-N FORWARDFW
258 iptables
-A FORWARD
-j FORWARDFW
261 iptables
-t nat
-N NAT_SOURCE
262 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
265 iptables
-t nat
-N CAPTIVE_PORTAL
266 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
268 # Custom prerouting chains (for transparent proxy)
269 iptables
-t nat
-N SQUID
270 iptables
-t nat
-A PREROUTING
-j SQUID
273 iptables
-t nat
-N NAT_DESTINATION
274 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
275 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
277 iptables
-t mangle
-N NAT_DESTINATION
278 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
280 iptables
-t nat
-N NAT_DESTINATION_FIX
281 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
283 if [ -n "${GREEN_ADDRESS}" ]; then
284 iptables
-t nat
-A NAT_DESTINATION_FIX \
285 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
288 if [ -n "${BLUE_ADDRESS}" ]; then
289 iptables
-t nat
-A NAT_DESTINATION_FIX \
290 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
293 if [ -n "${ORANGE_ADDRESS}" ]; then
294 iptables
-t nat
-A NAT_DESTINATION_FIX \
295 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
298 # RED chain, used for the red interface
300 iptables
-A INPUT
-j REDINPUT
301 iptables
-N REDFORWARD
302 iptables
-A FORWARD
-j REDFORWARD
303 iptables
-t nat
-N REDNAT
304 iptables
-t nat
-A POSTROUTING
-j REDNAT
306 # Populate IPsec chains
307 /usr
/lib
/firewall
/ipsec-policy
309 # Apply OpenVPN firewall rules
310 /usr
/local
/bin
/openvpnctrl
--firewall-rules
313 /usr
/local
/bin
/wirelessctrl
316 /usr
/local
/bin
/captivectrl
320 iptables
-A INPUT
-j POLICYIN
321 iptables
-N POLICYFWD
322 iptables
-A FORWARD
-j POLICYFWD
323 iptables
-N POLICYOUT
324 iptables
-A OUTPUT
-j POLICYOUT
326 # Initialize firewall policies.
327 /usr
/sbin
/firewall-policy
329 # Install firewall rules for the red interface.
332 # If red has not been brought up yet, we will
333 # add the blocking rules for MASQUERADE
334 if [ ! -e "/var/ipfire/red/active" ]; then
341 iptables
-F REDFORWARD
342 iptables
-t nat
-F REDNAT
344 # PPPoE / PPTP Device
345 if [ "$IFACE" != "" ]; then
347 if [ "$DEVICE" != "" ]; then
348 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
350 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
351 if [ "$RED_DEV" != "" ]; then
352 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
358 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
359 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
360 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
363 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
365 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
366 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
367 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
369 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
370 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
371 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
374 # Outgoing masquerading (don't masqerade IPsec (mark 50))
375 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
377 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
378 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
381 local NO_MASQ_NETWORKS
383 if [ "${MASQUERADE_GREEN}" = "off" ]; then
384 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
387 if [ "${MASQUERADE_BLUE}" = "off" ]; then
388 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
391 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
392 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
396 for network
in ${NO_MASQ_NETWORKS}; do
397 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
400 # Masquerade everything else
401 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
405 /usr
/local
/bin
/firewallctrl
408 iptables_red_down
() {
409 # Prohibit packets to reach the masquerading rule
410 # while the WAN interface is down - this is required to
411 # circumvent udp related NAT issues
412 # http://forum.ipfire.org/index.php?topic=11127.0
413 if [ -n "${IFACE}" ]; then
414 iptables
-F REDFORWARD
415 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
419 /usr
/local
/bin
/firewallctrl
422 # See how we were called.
425 boot_mesg
"Setting up firewall"
430 boot_mesg
"Reloading firewall"
435 boot_mesg
"Disabling firewall access to RED"
443 echo "Usage: $0 {start|reload|restart}"