]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/system/firewall
7a7d52d577b9f55ae2a6e004083d2794b3a05cb0
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'`
18 /sbin
/iptables
--wait "$@"
22 # Flush all rules and delete all custom chains
33 iptables
-P INPUT DROP
34 iptables
-P FORWARD DROP
35 iptables
-P OUTPUT ACCEPT
37 # Enable TRACE logging to syslog
39 sysctl
-q -w net.netfilter.nf_log
.2=nf_log_ipv4
41 # Empty LOG_DROP and LOG_REJECT chains
43 iptables
-A LOG_DROP
-m limit
--limit 10/second
-j LOG
44 iptables
-A LOG_DROP
-j DROP
45 iptables
-N LOG_REJECT
46 iptables
-A LOG_REJECT
-m limit
--limit 10/second
-j LOG
47 iptables
-A LOG_REJECT
-j REJECT
49 # This chain will log, then DROPs packets with certain bad combinations
50 # of flags might indicate a port-scan attempt (xmas, null, etc.)
52 if [ "$DROPPORTSCAN" == "on" ]; then
53 iptables
-A PSCAN
-p tcp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_TCP Scan " -m comment
--comment "DROP_TCP PScan"
54 iptables
-A PSCAN
-p udp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_UDP Scan " -m comment
--comment "DROP_UDP PScan"
55 iptables
-A PSCAN
-p icmp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_ICMP Scan " -m comment
--comment "DROP_ICMP PScan"
56 iptables
-A PSCAN
-f -m limit
--limit 10/second
-j LOG
--log-prefix "DROP_FRAG Scan " -m comment
--comment "DROP_FRAG PScan"
58 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
60 # New tcp packets without SYN set - could well be an obscure type of port scan
61 # that's not covered above, may just be a broken Windows machine
63 if [ "$DROPNEWNOTSYN" == "on" ]; then
64 iptables
-A NEWNOTSYN
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_NEWNOTSYN "
66 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
68 # Chain to contain all the rules relating to bad TCP flags
71 # Don't check loopback
72 iptables
-A BADTCP
-i lo
-j RETURN
74 # Disallow packets frequently used by port-scanners
75 # NMAP FIN/URG/PSH (XMAS scan)
76 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
,URG
,PSH
-j PSCAN
78 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
80 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
82 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
-j PSCAN
83 # SYN/RST (also catches xmas variants that set SYN+RST+...)
84 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,RST SYN
,RST
-j PSCAN
85 # SYN/FIN (QueSO or nmap OS probe)
86 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,FIN SYN
,FIN
-j PSCAN
88 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
90 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
92 iptables
-A INPUT
-p tcp
-j BADTCP
93 iptables
-A FORWARD
-p tcp
-j BADTCP
95 # Connection tracking chains
97 iptables
-A CONNTRACK
-m conntrack
--ctstate ESTABLISHED
-j ACCEPT
98 iptables
-A CONNTRACK
-m conntrack
--ctstate INVALID
-j DROP
99 iptables
-A CONNTRACK
-p icmp
-m conntrack
--ctstate RELATED
-j ACCEPT
100 iptables
-t raw
-N CONNTRACK
101 iptables
-t raw
-A PREROUTING
-j CONNTRACK
103 # Restore any connection marks
104 iptables
-t mangle
-A PREROUTING
-j CONNMARK
--restore-mark
106 # Fix for braindead ISPs
107 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
109 # CUSTOM chains, can be used by the users themselves
110 iptables
-N CUSTOMINPUT
111 iptables
-A INPUT
-j CUSTOMINPUT
112 iptables
-N CUSTOMFORWARD
113 iptables
-A FORWARD
-j CUSTOMFORWARD
114 iptables
-N CUSTOMOUTPUT
115 iptables
-A OUTPUT
-j CUSTOMOUTPUT
116 iptables
-t nat
-N CUSTOMPREROUTING
117 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
118 iptables
-t nat
-N CUSTOMPOSTROUTING
119 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
123 iptables
-A INPUT
-j P2PBLOCK
124 iptables
-A FORWARD
-j P2PBLOCK
125 iptables
-A OUTPUT
-j P2PBLOCK
127 # IPS (Guardian) chains
129 iptables
-A INPUT
-j GUARDIAN
130 iptables
-A FORWARD
-j GUARDIAN
132 # Block non-established IPsec networks
133 iptables
-N IPSECBLOCK
134 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
135 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
137 # Block OpenVPN transfer networks
138 iptables
-N OVPNBLOCK
139 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
140 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
141 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
143 # IPS (Suricata) chains
144 iptables
-N IPS_INPUT
145 iptables
-N IPS_FORWARD
146 iptables
-N IPS_OUTPUT
147 iptables
-A INPUT
-j IPS_INPUT
148 iptables
-A FORWARD
-j IPS_FORWARD
149 iptables
-A OUTPUT
-j IPS_OUTPUT
151 # OpenVPN transfer network translation
152 iptables
-t nat
-N OVPNNAT
153 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
155 # IPTV chains for IGMPPROXY
156 iptables
-N IPTVINPUT
157 iptables
-A INPUT
-j IPTVINPUT
158 iptables
-N IPTVFORWARD
159 iptables
-A FORWARD
-j IPTVFORWARD
161 # Allow to ping the firewall.
162 iptables
-N ICMPINPUT
163 iptables
-A INPUT
-j ICMPINPUT
164 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
166 # Accept everything on loopback
168 iptables
-A LOOPBACK
-i lo
-j ACCEPT
169 iptables
-A LOOPBACK
-o lo
-j ACCEPT
171 # Filter all packets with loopback addresses on non-loopback interfaces.
172 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
173 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
175 for i
in INPUT FORWARD OUTPUT
; do
176 iptables
-A ${i} -j LOOPBACK
180 iptables
-N CAPTIVE_PORTAL
181 iptables
-N CAPTIVE_PORTAL_CLIENTS
182 for i
in INPUT FORWARD
; do
183 iptables
-A ${i} -j CAPTIVE_PORTAL
186 # Accept everything connected
187 for i
in INPUT FORWARD OUTPUT
; do
188 iptables
-A ${i} -j CONNTRACK
192 iptables
-N DHCPINPUT
193 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
194 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
196 iptables
-N DHCPOUTPUT
197 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
198 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
200 # Allow DHCP on GREEN
201 iptables
-N DHCPGREENINPUT
202 iptables
-N DHCPGREENOUTPUT
203 if [ -n "${GREEN_DEV}" ]; then
204 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
205 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
208 # Allow DHCP on BLUE to be turned on/off
209 iptables
-N DHCPBLUEINPUT
210 iptables
-N DHCPBLUEOUTPUT
211 if [ -n "${BLUE_DEV}" ]; then
212 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
213 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
217 iptables
-N LOCATIONBLOCK
218 iptables
-A INPUT
-j LOCATIONBLOCK
219 iptables
-A FORWARD
-j LOCATIONBLOCK
221 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
222 iptables
-N IPSECINPUT
223 iptables
-N IPSECFORWARD
224 iptables
-N IPSECOUTPUT
225 iptables
-A INPUT
-j IPSECINPUT
226 iptables
-A FORWARD
-j IPSECFORWARD
227 iptables
-A OUTPUT
-j IPSECOUTPUT
228 iptables
-t nat
-N IPSECNAT
229 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
231 # localhost and ethernet.
232 # Always allow accessing the web GUI from GREEN.
234 iptables
-A INPUT
-j GUIINPUT
235 if [ -n "${GREEN_DEV}" ]; then
236 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
240 iptables
-N WIRELESSINPUT
241 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
242 iptables
-N WIRELESSFORWARD
243 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
246 iptables
-N OVPNINPUT
247 iptables
-A INPUT
-j OVPNINPUT
249 # Tor (inbound and outbound)
250 iptables
-N TOR_INPUT
251 iptables
-A INPUT
-j TOR_INPUT
252 iptables
-N TOR_OUTPUT
253 iptables
-A OUTPUT
-j TOR_OUTPUT
255 # Jump into the actual firewall ruleset.
257 iptables
-A INPUT
-j INPUTFW
259 iptables
-N OUTGOINGFW
260 iptables
-A OUTPUT
-j OUTGOINGFW
262 iptables
-N FORWARDFW
263 iptables
-A FORWARD
-j FORWARDFW
266 iptables
-t nat
-N NAT_SOURCE
267 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
270 iptables
-t nat
-N CAPTIVE_PORTAL
271 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
273 # Custom prerouting chains (for transparent proxy)
274 iptables
-t nat
-N SQUID
275 iptables
-t nat
-A PREROUTING
-j SQUID
278 iptables
-t nat
-N NAT_DESTINATION
279 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
280 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
282 iptables
-t mangle
-N NAT_DESTINATION
283 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
285 iptables
-t nat
-N NAT_DESTINATION_FIX
286 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
288 if [ -n "${GREEN_ADDRESS}" ]; then
289 iptables
-t nat
-A NAT_DESTINATION_FIX \
290 -m mark
--mark "0x01000000/${NAT_MASK}" -j SNAT
--to-source "${GREEN_ADDRESS}"
293 if [ -n "${BLUE_ADDRESS}" ]; then
294 iptables
-t nat
-A NAT_DESTINATION_FIX \
295 -m mark
--mark "0x02000000/${NAT_MASK}" -j SNAT
--to-source "${BLUE_ADDRESS}"
298 if [ -n "${ORANGE_ADDRESS}" ]; then
299 iptables
-t nat
-A NAT_DESTINATION_FIX \
300 -m mark
--mark "0x04000000/${NAT_MASK}" -j SNAT
--to-source "${ORANGE_ADDRESS}"
303 # RED chain, used for the red interface
305 iptables
-A INPUT
-j REDINPUT
306 iptables
-N REDFORWARD
307 iptables
-A FORWARD
-j REDFORWARD
308 iptables
-t nat
-N REDNAT
309 iptables
-t nat
-A POSTROUTING
-j REDNAT
311 # Populate IPsec chains
312 /usr
/lib
/firewall
/ipsec-policy
314 # Apply OpenVPN firewall rules
315 /usr
/local
/bin
/openvpnctrl
--firewall-rules
318 /usr
/local
/bin
/wirelessctrl
321 /usr
/local
/bin
/captivectrl
325 iptables
-A INPUT
-j POLICYIN
326 iptables
-N POLICYFWD
327 iptables
-A FORWARD
-j POLICYFWD
328 iptables
-N POLICYOUT
329 iptables
-A OUTPUT
-j POLICYOUT
331 # Initialize firewall policies.
332 /usr
/sbin
/firewall-policy
334 # Install firewall rules for the red interface.
337 # If red has not been brought up yet, we will
338 # add the blocking rules for MASQUERADE
339 if [ ! -e "/var/ipfire/red/active" ]; then
346 iptables
-F REDFORWARD
347 iptables
-t nat
-F REDNAT
349 # PPPoE / PPTP Device
350 if [ "$IFACE" != "" ]; then
352 if [ "$DEVICE" != "" ]; then
353 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
355 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
356 if [ "$RED_DEV" != "" ]; then
357 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
363 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
364 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
365 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
368 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
370 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
371 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
372 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
374 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
375 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
376 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
379 # Outgoing masquerading (don't masqerade IPsec (mark 50))
380 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
382 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
383 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
386 local NO_MASQ_NETWORKS
388 if [ "${MASQUERADE_GREEN}" = "off" ]; then
389 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
392 if [ "${MASQUERADE_BLUE}" = "off" ]; then
393 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
396 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
397 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
401 for network
in ${NO_MASQ_NETWORKS}; do
402 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
405 # Masquerade everything else
406 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
410 /usr
/local
/bin
/firewallctrl
413 iptables_red_down
() {
414 # Prohibit packets to reach the masquerading rule
415 # while the WAN interface is down - this is required to
416 # circumvent udp related NAT issues
417 # http://forum.ipfire.org/index.php?topic=11127.0
418 if [ -n "${IFACE}" ]; then
419 iptables
-F REDFORWARD
420 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
424 /usr
/local
/bin
/firewallctrl
427 # See how we were called.
430 boot_mesg
"Setting up firewall"
435 boot_mesg
"Reloading firewall"
440 boot_mesg
"Disabling firewall access to RED"
448 echo "Usage: $0 {start|reload|restart}"