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/second
-j LOG
38 iptables
-A LOG_DROP
-j DROP
39 iptables
-N LOG_REJECT
40 iptables
-A LOG_REJECT
-m limit
--limit 10/second
-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/second
-j LOG
--log-prefix "DROP_TCP Scan " -m comment
--comment "DROP_TCP PScan"
48 iptables
-A PSCAN
-p udp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_UDP Scan " -m comment
--comment "DROP_UDP PScan"
49 iptables
-A PSCAN
-p icmp
-m limit
--limit 10/second
-j LOG
--log-prefix "DROP_ICMP Scan " -m comment
--comment "DROP_ICMP PScan"
50 iptables
-A PSCAN
-f -m limit
--limit 10/second
-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/second
-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/)
99 # GRE (always enabled)
100 modprobe nf_conntrack_proto_gre
103 if [ "${CONNTRACK_SIP}" = "on" ]; then
105 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
106 -m helper
--helper sip
-j ACCEPT
107 for proto
in udp tcp
; do
108 iptables
-t raw
-A CONNTRACK
-p "${proto}" --dport 5060 -j CT
--helper sip
113 if [ "${CONNTRACK_H323}" = "on" ]; then
115 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
116 -m helper
--helper h323
-j ACCEPT
119 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1719 -j CT
--helper RAS
122 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1720 -j CT
--helper Q
.931
126 if [ "${CONNTRACK_FTP}" = "on" ]; then
128 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
129 -m helper
--helper ftp -p tcp
--dport 1024: -j ACCEPT
130 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 21 -j CT
--helper ftp
134 if [ "${CONNTRACK_PPTP}" = "on" ]; then
136 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
137 -m helper
--helper pptp
-j ACCEPT
138 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1723 -j CT
--helper pptp
142 if [ "${CONNTRACK_TFTP}" = "on" ]; then
144 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
145 -m helper
--helper tftp
-j ACCEPT
146 iptables
-t raw
-A CONNTRACK
-p udp
--dport 69 -j CT
--helper tftp
150 if [ "${CONNTRACK_IRC}" = "on" ]; then
152 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
153 -m helper
--helper irc
-j ACCEPT
154 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 6667 -j CT
--helper irc
158 if [ "${CONNTRACK_AMANDA}" = "on" ]; then
159 modprobe nf_nat_amanda
160 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
161 -m helper
--helper amanda
-j ACCEPT
162 iptables
-t raw
-A CONNTRACK
-p tcp
-j CT
--helper amanda
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
182 iptables
-A INPUT
-j P2PBLOCK
183 iptables
-A FORWARD
-j P2PBLOCK
184 iptables
-A OUTPUT
-j P2PBLOCK
186 # IPS (Guardian) chains
188 iptables
-A INPUT
-j GUARDIAN
189 iptables
-A FORWARD
-j GUARDIAN
191 # Block non-established IPsec networks
192 iptables
-N IPSECBLOCK
193 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
194 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
196 # Block OpenVPN transfer networks
197 iptables
-N OVPNBLOCK
198 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
199 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
200 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
202 # IPS (Suricata) chains
203 iptables
-N IPS_INPUT
204 iptables
-N IPS_FORWARD
205 iptables
-N IPS_OUTPUT
206 iptables
-A INPUT
-j IPS_INPUT
207 iptables
-A FORWARD
-j IPS_FORWARD
208 iptables
-A OUTPUT
-j IPS_OUTPUT
210 # OpenVPN transfer network translation
211 iptables
-t nat
-N OVPNNAT
212 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
214 # IPTV chains for IGMPPROXY
215 iptables
-N IPTVINPUT
216 iptables
-A INPUT
-j IPTVINPUT
217 iptables
-N IPTVFORWARD
218 iptables
-A FORWARD
-j IPTVFORWARD
220 # Allow to ping the firewall.
221 iptables
-N ICMPINPUT
222 iptables
-A INPUT
-j ICMPINPUT
223 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
225 # Accept everything on loopback
227 iptables
-A LOOPBACK
-i lo
-j ACCEPT
228 iptables
-A LOOPBACK
-o lo
-j ACCEPT
230 # Filter all packets with loopback addresses on non-loopback interfaces.
231 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
232 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
234 for i
in INPUT FORWARD OUTPUT
; do
235 iptables
-A ${i} -j LOOPBACK
239 iptables
-N CAPTIVE_PORTAL
240 iptables
-N CAPTIVE_PORTAL_CLIENTS
241 for i
in INPUT FORWARD
; do
242 iptables
-A ${i} -j CAPTIVE_PORTAL
245 # Accept everything connected
246 for i
in INPUT FORWARD OUTPUT
; do
247 iptables
-A ${i} -j CONNTRACK
251 iptables
-N DHCPINPUT
252 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
253 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
255 iptables
-N DHCPOUTPUT
256 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
257 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
259 # Allow DHCP on GREEN
260 iptables
-N DHCPGREENINPUT
261 iptables
-N DHCPGREENOUTPUT
262 if [ -n "${GREEN_DEV}" ]; then
263 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
264 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
267 # Allow DHCP on BLUE to be turned on/off
268 iptables
-N DHCPBLUEINPUT
269 iptables
-N DHCPBLUEOUTPUT
270 if [ -n "${BLUE_DEV}" ]; then
271 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
272 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
276 iptables
-N GEOIPBLOCK
277 iptables
-A INPUT
-j GEOIPBLOCK
278 iptables
-A FORWARD
-j GEOIPBLOCK
280 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
281 iptables
-N IPSECINPUT
282 iptables
-N IPSECFORWARD
283 iptables
-N IPSECOUTPUT
284 iptables
-A INPUT
-j IPSECINPUT
285 iptables
-A FORWARD
-j IPSECFORWARD
286 iptables
-A OUTPUT
-j IPSECOUTPUT
287 iptables
-t nat
-N IPSECNAT
288 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
290 # localhost and ethernet.
291 # Always allow accessing the web GUI from GREEN.
293 iptables
-A INPUT
-j GUIINPUT
294 if [ -n "${GREEN_DEV}" ]; then
295 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
299 iptables
-N WIRELESSINPUT
300 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
301 iptables
-N WIRELESSFORWARD
302 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
305 iptables
-N OVPNINPUT
306 iptables
-A INPUT
-j OVPNINPUT
308 # Tor (inbound and outbound)
309 iptables
-N TOR_INPUT
310 iptables
-A INPUT
-j TOR_INPUT
311 iptables
-N TOR_OUTPUT
312 iptables
-A OUTPUT
-j TOR_OUTPUT
314 # Jump into the actual firewall ruleset.
316 iptables
-A INPUT
-j INPUTFW
318 iptables
-N OUTGOINGFW
319 iptables
-A OUTPUT
-j OUTGOINGFW
321 iptables
-N FORWARDFW
322 iptables
-A FORWARD
-j FORWARDFW
325 iptables
-t nat
-N NAT_SOURCE
326 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
329 iptables
-t nat
-N CAPTIVE_PORTAL
330 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
332 # Custom prerouting chains (for transparent proxy)
333 iptables
-t nat
-N SQUID
334 iptables
-t nat
-A PREROUTING
-j SQUID
337 iptables
-t nat
-N NAT_DESTINATION
338 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
339 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
341 iptables
-t mangle
-N NAT_DESTINATION
342 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
344 iptables
-t nat
-N NAT_DESTINATION_FIX
345 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
347 if [ -n "${GREEN_ADDRESS}" ]; then
348 iptables
-t nat
-A NAT_DESTINATION_FIX \
349 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
352 if [ -n "${BLUE_ADDRESS}" ]; then
353 iptables
-t nat
-A NAT_DESTINATION_FIX \
354 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
357 if [ -n "${ORANGE_ADDRESS}" ]; then
358 iptables
-t nat
-A NAT_DESTINATION_FIX \
359 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
362 # upnp chain for our upnp daemon
363 iptables
-t nat
-N UPNPFW
364 iptables
-t nat
-A PREROUTING
-j UPNPFW
366 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
368 # RED chain, used for the red interface
370 iptables
-A INPUT
-j REDINPUT
371 iptables
-N REDFORWARD
372 iptables
-A FORWARD
-j REDFORWARD
373 iptables
-t nat
-N REDNAT
374 iptables
-t nat
-A POSTROUTING
-j REDNAT
376 # Populate IPsec chains
377 /usr
/lib
/firewall
/ipsec-policy
379 # Apply OpenVPN firewall rules
380 /usr
/local
/bin
/openvpnctrl
--firewall-rules
383 /usr
/local
/bin
/wirelessctrl
386 /usr
/local
/bin
/captivectrl
390 iptables
-A INPUT
-j POLICYIN
391 iptables
-N POLICYFWD
392 iptables
-A FORWARD
-j POLICYFWD
393 iptables
-N POLICYOUT
394 iptables
-A OUTPUT
-j POLICYOUT
396 # Initialize firewall policies.
397 /usr
/sbin
/firewall-policy
399 # Install firewall rules for the red interface.
402 # If red has not been brought up yet, we will
403 # add the blocking rules for MASQUERADE
404 if [ ! -e "/var/ipfire/red/active" ]; then
411 iptables
-F REDFORWARD
412 iptables
-t nat
-F REDNAT
414 # PPPoE / PPTP Device
415 if [ "$IFACE" != "" ]; then
417 if [ "$DEVICE" != "" ]; then
418 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
420 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
421 if [ "$RED_DEV" != "" ]; then
422 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
428 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
429 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
430 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
433 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
435 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
436 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
437 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
439 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
440 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
441 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
444 # Outgoing masquerading (don't masqerade IPsec (mark 50))
445 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
447 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
448 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
451 local NO_MASQ_NETWORKS
453 if [ "${MASQUERADE_GREEN}" = "off" ]; then
454 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
457 if [ "${MASQUERADE_BLUE}" = "off" ]; then
458 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
461 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
462 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
466 for network
in ${NO_MASQ_NETWORKS}; do
467 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
470 # Masquerade everything else
471 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
475 /usr
/local
/bin
/firewallctrl
478 iptables_red_down
() {
479 # Prohibit packets to reach the masquerading rule
480 # while the WAN interface is down - this is required to
481 # circumvent udp related NAT issues
482 # http://forum.ipfire.org/index.php?topic=11127.0
483 if [ -n "${IFACE}" ]; then
484 iptables
-F REDFORWARD
485 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
489 /usr
/local
/bin
/firewallctrl
492 # See how we were called.
495 boot_mesg
"Setting up firewall"
500 boot_mesg
"Reloading firewall"
505 boot_mesg
"Disabling firewall access to RED"
513 echo "Usage: $0 {start|reload|restart}"