]>
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 # Empty LOG_DROP and LOG_REJECT chains
37 iptables
-A LOG_DROP
-m limit
--limit 10/minute
-j LOG
38 iptables
-A LOG_DROP
-j DROP
39 iptables
-N LOG_REJECT
40 iptables
-A LOG_REJECT
-m limit
--limit 10/minute
-j LOG
41 iptables
-A LOG_REJECT
-j REJECT
43 # This chain will log, then DROPs packets with certain bad combinations
44 # of flags might indicate a port-scan attempt (xmas, null, etc)
46 if [ "$DROPPORTSCAN" == "on" ]; then
47 iptables
-A PSCAN
-p tcp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_TCP Scan " -m comment
--comment "DROP_TCP PScan"
48 iptables
-A PSCAN
-p udp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_UDP Scan " -m comment
--comment "DROP_UDP PScan"
49 iptables
-A PSCAN
-p icmp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_ICMP Scan " -m comment
--comment "DROP_ICMP PScan"
50 iptables
-A PSCAN
-f -m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_FRAG Scan " -m comment
--comment "DROP_FRAG PScan"
52 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
54 # New tcp packets without SYN set - could well be an obscure type of port scan
55 # that's not covered above, may just be a broken windows machine
57 if [ "$DROPNEWNOTSYN" == "on" ]; then
58 iptables
-A NEWNOTSYN
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_NEWNOTSYN "
60 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
62 # Chain to contain all the rules relating to bad TCP flags
65 # Don't check loopback
66 iptables
-A BADTCP
-i lo
-j RETURN
68 # Disallow packets frequently used by port-scanners
69 # NMAP FIN/URG/PSH (XMAS scan)
70 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
,URG
,PSH
-j PSCAN
72 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
74 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
76 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
-j PSCAN
77 # SYN/RST (also catches xmas variants that set SYN+RST+...)
78 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,RST SYN
,RST
-j PSCAN
79 # SYN/FIN (QueSO or nmap OS probe)
80 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,FIN SYN
,FIN
-j PSCAN
82 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
84 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
86 iptables
-A INPUT
-p tcp
-j BADTCP
87 iptables
-A FORWARD
-p tcp
-j BADTCP
89 # Connection tracking chains
91 iptables
-A CONNTRACK
-m conntrack
--ctstate ESTABLISHED
-j ACCEPT
92 iptables
-A CONNTRACK
-m conntrack
--ctstate INVALID
-j DROP
93 iptables
-A CONNTRACK
-p icmp
-m conntrack
--ctstate RELATED
-j ACCEPT
94 iptables
-t raw
-N CONNTRACK
95 iptables
-t raw
-A PREROUTING
-j CONNTRACK
97 # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
100 if [ "${CONNTRACK_SIP}" = "on" ]; then
102 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
103 -m helper
--helper sip
-j ACCEPT
104 for proto
in udp tcp
; do
105 iptables
-t raw
-A CONNTRACK
-p "${proto}" --dport 5060 -j CT
--helper sip
110 if [ "${CONNTRACK_H323}" = "on" ]; then
112 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
113 -m helper
--helper h323
-j ACCEPT
116 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1719 -j CT
--helper RAS
119 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1720 -j CT
--helper Q
.931
123 if [ "${CONNTRACK_FTP}" = "on" ]; then
125 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
126 -m helper
--helper ftp -p tcp
--dport 1024: -j ACCEPT
127 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 21 -j CT
--helper ftp
131 if [ "${CONNTRACK_PPTP}" = "on" ]; then
133 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
134 -m helper
--helper pptp
-j ACCEPT
135 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1723 -j CT
--helper pptp
139 if [ "${CONNTRACK_TFTP}" = "on" ]; then
141 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
142 -m helper
--helper tftp
-j ACCEPT
143 iptables
-t raw
-A CONNTRACK
-p udp
--dport 69 -j CT
--helper tftp
147 if [ "${CONNTRACK_IRC}" = "on" ]; then
149 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
150 -m helper
--helper irc
-j ACCEPT
151 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 6667 -j CT
--helper irc
155 if [ "${CONNTRACK_AMANDA}" = "on" ]; then
156 modprobe nf_nat_amanda
157 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
158 -m helper
--helper amanda
-j ACCEPT
159 iptables
-t raw
-A CONNTRACK
-p tcp
-j CT
--helper amanda
162 # Fix for braindead ISP's
163 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
165 # CUSTOM chains, can be used by the users themselves
166 iptables
-N CUSTOMINPUT
167 iptables
-A INPUT
-j CUSTOMINPUT
168 iptables
-N CUSTOMFORWARD
169 iptables
-A FORWARD
-j CUSTOMFORWARD
170 iptables
-N CUSTOMOUTPUT
171 iptables
-A OUTPUT
-j CUSTOMOUTPUT
172 iptables
-t nat
-N CUSTOMPREROUTING
173 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
174 iptables
-t nat
-N CUSTOMPOSTROUTING
175 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
179 iptables
-A INPUT
-j P2PBLOCK
180 iptables
-A FORWARD
-j P2PBLOCK
181 iptables
-A OUTPUT
-j P2PBLOCK
183 # Guardian (IPS) chains
185 iptables
-A INPUT
-j GUARDIAN
186 iptables
-A FORWARD
-j GUARDIAN
188 # Block non-established IPsec networks
189 iptables
-N IPSECBLOCK
190 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
191 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
193 # Block OpenVPN transfer networks
194 iptables
-N OVPNBLOCK
195 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
196 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
197 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
199 # OpenVPN transfer network translation
200 iptables
-t nat
-N OVPNNAT
201 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
203 # IPTV chains for IGMPPROXY
204 iptables
-N IPTVINPUT
205 iptables
-A INPUT
-j IPTVINPUT
206 iptables
-N IPTVFORWARD
207 iptables
-A FORWARD
-j IPTVFORWARD
209 # Allow to ping the firewall.
210 iptables
-N ICMPINPUT
211 iptables
-A INPUT
-j ICMPINPUT
212 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
214 # Accept everything on loopback
216 iptables
-A LOOPBACK
-i lo
-j ACCEPT
217 iptables
-A LOOPBACK
-o lo
-j ACCEPT
219 # Filter all packets with loopback addresses on non-loopback interfaces.
220 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
221 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
223 for i
in INPUT FORWARD OUTPUT
; do
224 iptables
-A ${i} -j LOOPBACK
227 # Accept everything connected
228 for i
in INPUT FORWARD OUTPUT
; do
229 iptables
-A ${i} -j CONNTRACK
233 iptables
-N DHCPINPUT
234 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
235 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
237 iptables
-N DHCPOUTPUT
238 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
239 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
241 # Allow DHCP on GREEN
242 iptables
-N DHCPGREENINPUT
243 iptables
-N DHCPGREENOUTPUT
244 if [ -n "${GREEN_DEV}" ]; then
245 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
246 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
249 # allow DHCP on BLUE to be turned on/off
250 iptables
-N DHCPBLUEINPUT
251 iptables
-N DHCPBLUEOUTPUT
252 if [ -n "${BLUE_DEV}" ]; then
253 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
254 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
258 iptables
-N GEOIPBLOCK
259 iptables
-A INPUT
-j GEOIPBLOCK
260 iptables
-A FORWARD
-j GEOIPBLOCK
262 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
263 iptables
-N IPSECINPUT
264 iptables
-N IPSECFORWARD
265 iptables
-N IPSECOUTPUT
266 iptables
-A INPUT
-j IPSECINPUT
267 iptables
-A FORWARD
-j IPSECFORWARD
268 iptables
-A OUTPUT
-j IPSECOUTPUT
269 iptables
-t nat
-N IPSECNAT
270 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
272 # localhost and ethernet.
273 # Always allow accessing the web GUI from GREEN.
275 iptables
-A INPUT
-j GUIINPUT
276 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
279 iptables
-N WIRELESSINPUT
280 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
281 iptables
-N WIRELESSFORWARD
282 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
285 iptables
-N OVPNINPUT
286 iptables
-A INPUT
-j OVPNINPUT
289 iptables
-N TOR_INPUT
290 iptables
-A INPUT
-j TOR_INPUT
292 # Jump into the actual firewall ruleset.
294 iptables
-A INPUT
-j INPUTFW
296 iptables
-N OUTGOINGFW
297 iptables
-A OUTPUT
-j OUTGOINGFW
299 iptables
-N FORWARDFW
300 iptables
-A FORWARD
-j FORWARDFW
303 iptables
-t nat
-N NAT_SOURCE
304 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
306 # Custom prerouting chains (for transparent proxy)
307 iptables
-t nat
-N SQUID
308 iptables
-t nat
-A PREROUTING
-j SQUID
311 iptables
-t nat
-N NAT_DESTINATION
312 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
313 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
315 iptables
-t mangle
-N NAT_DESTINATION
316 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
318 iptables
-t nat
-N NAT_DESTINATION_FIX
319 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
321 iptables
-t nat
-A NAT_DESTINATION_FIX \
322 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
324 if [ -n "${BLUE_ADDRESS}" ]; then
325 iptables
-t nat
-A NAT_DESTINATION_FIX \
326 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
329 if [ -n "${ORANGE_ADDRESS}" ]; then
330 iptables
-t nat
-A NAT_DESTINATION_FIX \
331 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
334 # upnp chain for our upnp daemon
335 iptables
-t nat
-N UPNPFW
336 iptables
-t nat
-A PREROUTING
-j UPNPFW
338 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
340 # RED chain, used for the red interface
342 iptables
-A INPUT
-j REDINPUT
343 iptables
-N REDFORWARD
344 iptables
-A FORWARD
-j REDFORWARD
345 iptables
-t nat
-N REDNAT
346 iptables
-t nat
-A POSTROUTING
-j REDNAT
348 # Populate IPsec block chain
349 /usr
/lib
/firewall
/ipsec-block
351 # Apply OpenVPN firewall rules
352 /usr
/local
/bin
/openvpnctrl
--firewall-rules
355 /usr
/local
/bin
/wirelessctrl
359 iptables
-A INPUT
-j POLICYIN
360 iptables
-N POLICYFWD
361 iptables
-A FORWARD
-j POLICYFWD
362 iptables
-N POLICYOUT
363 iptables
-A OUTPUT
-j POLICYOUT
365 # Initialize firewall policies.
366 /usr
/sbin
/firewall-policy
368 # Install firewall rules for the red interface.
371 # If red has not been brought up yet, we will
372 # add the blocking rules for MASQUERADE
373 if [ ! -e "/var/ipfire/red/active" ]; then
380 iptables
-F REDFORWARD
381 iptables
-t nat
-F REDNAT
383 # PPPoE / PPTP Device
384 if [ "$IFACE" != "" ]; then
386 if [ "$DEVICE" != "" ]; then
387 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
389 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
390 if [ "$RED_DEV" != "" ]; then
391 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
397 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
398 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
399 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
403 if [ "$ORANGE_DEV" != "" ]; then
404 # This rule enables a host on ORANGE network to connect to the outside
405 # (only if we have a red connection)
406 if [ "$IFACE" != "" ]; then
407 iptables
-A REDFORWARD
-i $ORANGE_DEV -o $IFACE -j ACCEPT
411 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
413 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
414 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
415 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
417 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
418 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
419 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
422 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
423 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
425 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
426 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
429 local NO_MASQ_NETWORKS
431 if [ "${MASQUERADE_GREEN}" = "off" ]; then
432 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
435 if [ "${MASQUERADE_BLUE}" = "off" ]; then
436 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
439 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
440 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
444 for network
in ${NO_MASQ_NETWORKS}; do
445 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
448 # Masquerade everything else
449 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
453 /usr
/local
/bin
/firewallctrl
456 iptables_red_down
() {
457 # Prohibit packets to reach the masquerading rule
458 # while the wan interface is down - this is required to
459 # circumvent udp related NAT issues
460 # http://forum.ipfire.org/index.php?topic=11127.0
461 if [ -n "${IFACE}" ]; then
462 iptables
-F REDFORWARD
463 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
467 /usr
/local
/bin
/firewallctrl
470 # See how we were called.
473 boot_mesg
"Setting up firewall"
478 boot_mesg
"Reloading firewall"
483 boot_mesg
"Disabling firewall access to RED"
491 echo "Usage: $0 {start|reload|restart}"