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
-t raw
-N CONNTRACK
94 iptables
-t raw
-A PREROUTING
-j CONNTRACK
96 # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
99 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
100 -m helper
--helper sip
-j ACCEPT
101 for proto
in udp tcp
; do
102 iptables
-t raw
-A CONNTRACK
-p "${proto}" --dport 5060 -j CT
--helper sip
106 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
107 -m helper
--helper ftp -p tcp
--dport 1024: -j ACCEPT
108 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 21 -j CT
--helper ftp
111 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
112 -m helper
--helper pptp
-j ACCEPT
113 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1723 -j CT
--helper pptp
116 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
117 -m helper
--helper tftp
-j ACCEPT
118 iptables
-t raw
-A CONNTRACK
-p udp
--dport 69 -j CT
--helper tftp
120 # Fix for braindead ISP's
121 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
123 # CUSTOM chains, can be used by the users themselves
124 iptables
-N CUSTOMINPUT
125 iptables
-A INPUT
-j CUSTOMINPUT
126 iptables
-N CUSTOMFORWARD
127 iptables
-A FORWARD
-j CUSTOMFORWARD
128 iptables
-N CUSTOMOUTPUT
129 iptables
-A OUTPUT
-j CUSTOMOUTPUT
130 iptables
-t nat
-N CUSTOMPREROUTING
131 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
132 iptables
-t nat
-N CUSTOMPOSTROUTING
133 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
137 iptables
-A INPUT
-j P2PBLOCK
138 iptables
-A FORWARD
-j P2PBLOCK
139 iptables
-A OUTPUT
-j P2PBLOCK
141 # Guardian (IPS) chains
143 iptables
-A INPUT
-j GUARDIAN
144 iptables
-A FORWARD
-j GUARDIAN
146 # Block OpenVPN transfer networks
147 iptables
-N OVPNBLOCK
148 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
149 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
150 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
152 # OpenVPN transfer network translation
153 iptables
-t nat
-N OVPNNAT
154 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
156 # IPTV chains for IGMPPROXY
157 iptables
-N IPTVINPUT
158 iptables
-A INPUT
-j IPTVINPUT
159 iptables
-N IPTVFORWARD
160 iptables
-A FORWARD
-j IPTVFORWARD
162 # Allow to ping the firewall.
163 iptables
-N ICMPINPUT
164 iptables
-A INPUT
-j ICMPINPUT
165 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
167 # Accept everything on loopback
169 iptables
-A LOOPBACK
-i lo
-j ACCEPT
170 iptables
-A LOOPBACK
-o lo
-j ACCEPT
172 # Filter all packets with loopback addresses on non-loopback interfaces.
173 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
174 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
176 for i
in INPUT FORWARD OUTPUT
; do
177 iptables
-A ${i} -j LOOPBACK
180 # Accept everything connected
181 for i
in INPUT FORWARD OUTPUT
; do
182 iptables
-A ${i} -j CONNTRACK
186 iptables
-N DHCPINPUT
187 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
188 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
190 iptables
-N DHCPOUTPUT
191 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
192 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
194 # Allow DHCP on GREEN
195 iptables
-N DHCPGREENINPUT
196 iptables
-N DHCPGREENOUTPUT
197 if [ -n "${GREEN_DEV}" ]; then
198 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
199 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
202 # allow DHCP on BLUE to be turned on/off
203 iptables
-N DHCPBLUEINPUT
204 iptables
-N DHCPBLUEOUTPUT
205 if [ -n "${BLUE_DEV}" ]; then
206 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
207 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
211 iptables
-N GEOIPBLOCK
212 iptables
-A INPUT
-j GEOIPBLOCK
213 iptables
-A FORWARD
-j GEOIPBLOCK
215 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
216 iptables
-N IPSECINPUT
217 iptables
-N IPSECFORWARD
218 iptables
-N IPSECOUTPUT
219 iptables
-A INPUT
-j IPSECINPUT
220 iptables
-A FORWARD
-j IPSECFORWARD
221 iptables
-A OUTPUT
-j IPSECOUTPUT
222 iptables
-t nat
-N IPSECNAT
223 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
225 # localhost and ethernet.
226 # Always allow accessing the web GUI from GREEN.
228 iptables
-A INPUT
-j GUIINPUT
229 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
232 iptables
-N WIRELESSINPUT
233 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
234 iptables
-N WIRELESSFORWARD
235 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
238 iptables
-N OVPNINPUT
239 iptables
-A INPUT
-j OVPNINPUT
242 iptables
-N TOR_INPUT
243 iptables
-A INPUT
-j TOR_INPUT
245 # Jump into the actual firewall ruleset.
247 iptables
-A INPUT
-j INPUTFW
249 iptables
-N OUTGOINGFW
250 iptables
-A OUTPUT
-j OUTGOINGFW
252 iptables
-N FORWARDFW
253 iptables
-A FORWARD
-j FORWARDFW
256 iptables
-t nat
-N NAT_SOURCE
257 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
259 # Custom prerouting chains (for transparent proxy)
260 iptables
-t nat
-N SQUID
261 iptables
-t nat
-A PREROUTING
-j SQUID
264 iptables
-t nat
-N NAT_DESTINATION
265 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
266 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
268 iptables
-t mangle
-N NAT_DESTINATION
269 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
271 iptables
-t nat
-N NAT_DESTINATION_FIX
272 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
274 iptables
-t nat
-A NAT_DESTINATION_FIX \
275 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
277 if [ -n "${BLUE_ADDRESS}" ]; then
278 iptables
-t nat
-A NAT_DESTINATION_FIX \
279 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
282 if [ -n "${ORANGE_ADDRESS}" ]; then
283 iptables
-t nat
-A NAT_DESTINATION_FIX \
284 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
287 # upnp chain for our upnp daemon
288 iptables
-t nat
-N UPNPFW
289 iptables
-t nat
-A PREROUTING
-j UPNPFW
291 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
293 # RED chain, used for the red interface
295 iptables
-A INPUT
-j REDINPUT
296 iptables
-N REDFORWARD
297 iptables
-A FORWARD
-j REDFORWARD
298 iptables
-t nat
-N REDNAT
299 iptables
-t nat
-A POSTROUTING
-j REDNAT
301 # Apply OpenVPN firewall rules
302 /usr
/local
/bin
/openvpnctrl
--firewall-rules
305 /usr
/local
/bin
/wirelessctrl
309 iptables
-A INPUT
-j POLICYIN
310 iptables
-N POLICYFWD
311 iptables
-A FORWARD
-j POLICYFWD
312 iptables
-N POLICYOUT
313 iptables
-A OUTPUT
-j POLICYOUT
315 # Initialize firewall policies.
316 /usr
/sbin
/firewall-policy
318 # Install firewall rules for the red interface.
321 # If red has not been brought up yet, we will
322 # add the blocking rules for MASQUERADE
323 if [ ! -e "/var/ipfire/red/active" ]; then
330 iptables
-F REDFORWARD
331 iptables
-t nat
-F REDNAT
333 # PPPoE / PPTP Device
334 if [ "$IFACE" != "" ]; then
336 if [ "$DEVICE" != "" ]; then
337 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
339 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
340 if [ "$RED_DEV" != "" ]; then
341 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
347 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
348 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
349 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
353 if [ "$ORANGE_DEV" != "" ]; then
354 # This rule enables a host on ORANGE network to connect to the outside
355 # (only if we have a red connection)
356 if [ "$IFACE" != "" ]; then
357 iptables
-A REDFORWARD
-i $ORANGE_DEV -o $IFACE -j ACCEPT
361 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
363 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
364 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
365 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
367 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
368 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
369 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
372 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
373 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
375 if [ "$IFACE" = "$GREEN_DEV" ]; then
376 MASQUERADE_GREEN
="off"
379 local NO_MASQ_NETWORKS
381 if [ "${MASQUERADE_GREEN}" = "off" ]; then
382 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
385 if [ "${MASQUERADE_BLUE}" = "off" ]; then
386 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
389 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
390 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
394 for network
in ${NO_MASQ_NETWORKS}; do
395 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
398 # Masquerade everything else
399 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
403 /usr
/local
/bin
/firewallctrl
406 iptables_red_down
() {
407 # Prohibit packets to reach the masquerading rule
408 # while the wan interface is down - this is required to
409 # circumvent udp related NAT issues
410 # http://forum.ipfire.org/index.php?topic=11127.0
411 if [ -n "${IFACE}" ]; then
412 iptables
-F REDFORWARD
413 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
417 /usr
/local
/bin
/firewallctrl
420 # See how we were called.
423 boot_mesg
"Loading firewall modules into the kernel"
424 modprobe iptable_nat || failed
=1
425 for i
in $
(find /lib
/modules
/$
(uname
-r) -name nf_conntrack
*); do
426 modprobe $
(basename $i | cut
-d.
-f1) || failed
=1
428 for i
in $
(find /lib
/modules
/$
(uname
-r) -name nf_nat
*); do
429 modprobe $
(basename $i | cut
-d.
-f1) || failed
=1
434 if [ -e /var
/ipfire
/main
/disable_nf_sip
]; then
436 rmmod nf_conntrack_sip
438 rmmod nf_conntrack_h323
441 boot_mesg
"Setting up firewall"
446 boot_mesg
"Reloading firewall"
451 boot_mesg
"Disabling firewall access to RED"
459 echo "Usage: $0 {start|reload|restart}"