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 # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
103 # GRE (always enabled)
104 modprobe nf_conntrack_proto_gre
107 if [ "${CONNTRACK_SIP}" = "on" ]; then
109 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
110 -m helper
--helper sip
-j ACCEPT
111 for proto
in udp tcp
; do
112 iptables
-t raw
-A CONNTRACK
-p "${proto}" --dport 5060 -j CT
--helper sip
117 if [ "${CONNTRACK_H323}" = "on" ]; then
119 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
120 -m helper
--helper h323
-j ACCEPT
123 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1719 -j CT
--helper RAS
126 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1720 -j CT
--helper Q
.931
130 if [ "${CONNTRACK_FTP}" = "on" ]; then
132 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
133 -m helper
--helper ftp -p tcp
--dport 1024: -j ACCEPT
134 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 21 -j CT
--helper ftp
138 if [ "${CONNTRACK_PPTP}" = "on" ]; then
140 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
141 -m helper
--helper pptp
-j ACCEPT
142 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 1723 -j CT
--helper pptp
146 if [ "${CONNTRACK_TFTP}" = "on" ]; then
148 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
149 -m helper
--helper tftp
-j ACCEPT
150 iptables
-t raw
-A CONNTRACK
-p udp
--dport 69 -j CT
--helper tftp
154 if [ "${CONNTRACK_IRC}" = "on" ]; then
156 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
157 -m helper
--helper irc
-j ACCEPT
158 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 6667 -j CT
--helper irc
162 if [ "${CONNTRACK_AMANDA}" = "on" ]; then
163 modprobe nf_nat_amanda
164 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
165 -m helper
--helper amanda
-j ACCEPT
166 iptables
-t raw
-A CONNTRACK
-p tcp
-j CT
--helper amanda
169 # Fix for braindead ISPs
170 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
172 # CUSTOM chains, can be used by the users themselves
173 iptables
-N CUSTOMINPUT
174 iptables
-A INPUT
-j CUSTOMINPUT
175 iptables
-N CUSTOMFORWARD
176 iptables
-A FORWARD
-j CUSTOMFORWARD
177 iptables
-N CUSTOMOUTPUT
178 iptables
-A OUTPUT
-j CUSTOMOUTPUT
179 iptables
-t nat
-N CUSTOMPREROUTING
180 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
181 iptables
-t nat
-N CUSTOMPOSTROUTING
182 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
186 iptables
-A INPUT
-j P2PBLOCK
187 iptables
-A FORWARD
-j P2PBLOCK
188 iptables
-A OUTPUT
-j P2PBLOCK
190 # IPS (Guardian) chains
192 iptables
-A INPUT
-j GUARDIAN
193 iptables
-A FORWARD
-j GUARDIAN
195 # Block non-established IPsec networks
196 iptables
-N IPSECBLOCK
197 iptables
-A FORWARD
-m policy
--dir out
--pol none
-j IPSECBLOCK
198 iptables
-A OUTPUT
-m policy
--dir out
--pol none
-j IPSECBLOCK
200 # Block OpenVPN transfer networks
201 iptables
-N OVPNBLOCK
202 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
203 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
204 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
206 # IPS (Suricata) chains
207 iptables
-N IPS_INPUT
208 iptables
-N IPS_FORWARD
209 iptables
-N IPS_OUTPUT
210 iptables
-A INPUT
-j IPS_INPUT
211 iptables
-A FORWARD
-j IPS_FORWARD
212 iptables
-A OUTPUT
-j IPS_OUTPUT
214 # OpenVPN transfer network translation
215 iptables
-t nat
-N OVPNNAT
216 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
218 # IPTV chains for IGMPPROXY
219 iptables
-N IPTVINPUT
220 iptables
-A INPUT
-j IPTVINPUT
221 iptables
-N IPTVFORWARD
222 iptables
-A FORWARD
-j IPTVFORWARD
224 # Allow to ping the firewall.
225 iptables
-N ICMPINPUT
226 iptables
-A INPUT
-j ICMPINPUT
227 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
229 # Accept everything on loopback
231 iptables
-A LOOPBACK
-i lo
-j ACCEPT
232 iptables
-A LOOPBACK
-o lo
-j ACCEPT
234 # Filter all packets with loopback addresses on non-loopback interfaces.
235 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
236 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
238 for i
in INPUT FORWARD OUTPUT
; do
239 iptables
-A ${i} -j LOOPBACK
243 iptables
-N CAPTIVE_PORTAL
244 iptables
-N CAPTIVE_PORTAL_CLIENTS
245 for i
in INPUT FORWARD
; do
246 iptables
-A ${i} -j CAPTIVE_PORTAL
249 # Accept everything connected
250 for i
in INPUT FORWARD OUTPUT
; do
251 iptables
-A ${i} -j CONNTRACK
255 iptables
-N DHCPINPUT
256 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
257 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
259 iptables
-N DHCPOUTPUT
260 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
261 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
263 # Allow DHCP on GREEN
264 iptables
-N DHCPGREENINPUT
265 iptables
-N DHCPGREENOUTPUT
266 if [ -n "${GREEN_DEV}" ]; then
267 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
268 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
271 # Allow DHCP on BLUE to be turned on/off
272 iptables
-N DHCPBLUEINPUT
273 iptables
-N DHCPBLUEOUTPUT
274 if [ -n "${BLUE_DEV}" ]; then
275 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
276 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
280 iptables
-N GEOIPBLOCK
281 iptables
-A INPUT
-j GEOIPBLOCK
282 iptables
-A FORWARD
-j GEOIPBLOCK
284 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
285 iptables
-N IPSECINPUT
286 iptables
-N IPSECFORWARD
287 iptables
-N IPSECOUTPUT
288 iptables
-A INPUT
-j IPSECINPUT
289 iptables
-A FORWARD
-j IPSECFORWARD
290 iptables
-A OUTPUT
-j IPSECOUTPUT
291 iptables
-t nat
-N IPSECNAT
292 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
294 # localhost and ethernet.
295 # Always allow accessing the web GUI from GREEN.
297 iptables
-A INPUT
-j GUIINPUT
298 if [ -n "${GREEN_DEV}" ]; then
299 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
303 iptables
-N WIRELESSINPUT
304 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
305 iptables
-N WIRELESSFORWARD
306 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
309 iptables
-N OVPNINPUT
310 iptables
-A INPUT
-j OVPNINPUT
312 # Tor (inbound and outbound)
313 iptables
-N TOR_INPUT
314 iptables
-A INPUT
-j TOR_INPUT
315 iptables
-N TOR_OUTPUT
316 iptables
-A OUTPUT
-j TOR_OUTPUT
318 # Jump into the actual firewall ruleset.
320 iptables
-A INPUT
-j INPUTFW
322 iptables
-N OUTGOINGFW
323 iptables
-A OUTPUT
-j OUTGOINGFW
325 iptables
-N FORWARDFW
326 iptables
-A FORWARD
-j FORWARDFW
329 iptables
-t nat
-N NAT_SOURCE
330 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
333 iptables
-t nat
-N CAPTIVE_PORTAL
334 iptables
-t nat
-A PREROUTING
-j CAPTIVE_PORTAL
336 # Custom prerouting chains (for transparent proxy)
337 iptables
-t nat
-N SQUID
338 iptables
-t nat
-A PREROUTING
-j SQUID
341 iptables
-t nat
-N NAT_DESTINATION
342 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
343 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
345 iptables
-t mangle
-N NAT_DESTINATION
346 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
348 iptables
-t nat
-N NAT_DESTINATION_FIX
349 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
351 if [ -n "${GREEN_ADDRESS}" ]; then
352 iptables
-t nat
-A NAT_DESTINATION_FIX \
353 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
356 if [ -n "${BLUE_ADDRESS}" ]; then
357 iptables
-t nat
-A NAT_DESTINATION_FIX \
358 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
361 if [ -n "${ORANGE_ADDRESS}" ]; then
362 iptables
-t nat
-A NAT_DESTINATION_FIX \
363 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
366 # upnp chain for our upnp daemon
367 iptables
-t nat
-N UPNPFW
368 iptables
-t nat
-A PREROUTING
-j UPNPFW
370 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
372 # RED chain, used for the red interface
374 iptables
-A INPUT
-j REDINPUT
375 iptables
-N REDFORWARD
376 iptables
-A FORWARD
-j REDFORWARD
377 iptables
-t nat
-N REDNAT
378 iptables
-t nat
-A POSTROUTING
-j REDNAT
380 # Populate IPsec chains
381 /usr
/lib
/firewall
/ipsec-policy
383 # Apply OpenVPN firewall rules
384 /usr
/local
/bin
/openvpnctrl
--firewall-rules
387 /usr
/local
/bin
/wirelessctrl
390 /usr
/local
/bin
/captivectrl
394 iptables
-A INPUT
-j POLICYIN
395 iptables
-N POLICYFWD
396 iptables
-A FORWARD
-j POLICYFWD
397 iptables
-N POLICYOUT
398 iptables
-A OUTPUT
-j POLICYOUT
400 # Initialize firewall policies.
401 /usr
/sbin
/firewall-policy
403 # Install firewall rules for the red interface.
406 # If red has not been brought up yet, we will
407 # add the blocking rules for MASQUERADE
408 if [ ! -e "/var/ipfire/red/active" ]; then
415 iptables
-F REDFORWARD
416 iptables
-t nat
-F REDNAT
418 # PPPoE / PPTP Device
419 if [ "$IFACE" != "" ]; then
421 if [ "$DEVICE" != "" ]; then
422 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
424 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
425 if [ "$RED_DEV" != "" ]; then
426 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
432 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
433 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
434 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
437 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
439 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; 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
443 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
444 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
445 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
448 # Outgoing masquerading (don't masqerade IPsec (mark 50))
449 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
451 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
452 iptables
-t nat
-A REDNAT
-i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
455 local NO_MASQ_NETWORKS
457 if [ "${MASQUERADE_GREEN}" = "off" ]; then
458 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
461 if [ "${MASQUERADE_BLUE}" = "off" ]; then
462 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
465 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
466 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
470 for network
in ${NO_MASQ_NETWORKS}; do
471 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
474 # Masquerade everything else
475 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
479 /usr
/local
/bin
/firewallctrl
482 iptables_red_down
() {
483 # Prohibit packets to reach the masquerading rule
484 # while the WAN interface is down - this is required to
485 # circumvent udp related NAT issues
486 # http://forum.ipfire.org/index.php?topic=11127.0
487 if [ -n "${IFACE}" ]; then
488 iptables
-F REDFORWARD
489 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
493 /usr
/local
/bin
/firewallctrl
496 # See how we were called.
499 boot_mesg
"Setting up firewall"
504 boot_mesg
"Reloading firewall"
509 boot_mesg
"Disabling firewall access to RED"
517 echo "Usage: $0 {start|reload|restart}"