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
29 iptables
-P INPUT DROP
30 iptables
-P FORWARD DROP
31 iptables
-P OUTPUT ACCEPT
33 # Empty LOG_DROP and LOG_REJECT chains
35 iptables
-A LOG_DROP
-m limit
--limit 10/minute
-j LOG
36 iptables
-A LOG_DROP
-j DROP
37 iptables
-N LOG_REJECT
38 iptables
-A LOG_REJECT
-m limit
--limit 10/minute
-j LOG
39 iptables
-A LOG_REJECT
-j REJECT
41 # This chain will log, then DROPs packets with certain bad combinations
42 # of flags might indicate a port-scan attempt (xmas, null, etc)
44 if [ "$DROPPORTSCAN" == "on" ]; then
45 iptables
-A PSCAN
-p tcp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_TCP Scan " -m comment
--comment "DROP_TCP PScan"
46 iptables
-A PSCAN
-p udp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_UDP Scan " -m comment
--comment "DROP_UDP PScan"
47 iptables
-A PSCAN
-p icmp
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_ICMP Scan " -m comment
--comment "DROP_ICMP PScan"
48 iptables
-A PSCAN
-f -m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_FRAG Scan " -m comment
--comment "DROP_FRAG PScan"
50 iptables
-A PSCAN
-j DROP
-m comment
--comment "DROP_PScan"
52 # New tcp packets without SYN set - could well be an obscure type of port scan
53 # that's not covered above, may just be a broken windows machine
55 if [ "$DROPNEWNOTSYN" == "on" ]; then
56 iptables
-A NEWNOTSYN
-m limit
--limit 10/minute
-j LOG
--log-prefix "DROP_NEWNOTSYN "
58 iptables
-A NEWNOTSYN
-j DROP
-m comment
--comment "DROP_NEWNOTSYN"
60 # Chain to contain all the rules relating to bad TCP flags
63 # Don't check loopback
64 iptables
-A BADTCP
-i lo
-j RETURN
66 # Disallow packets frequently used by port-scanners
67 # NMAP FIN/URG/PSH (XMAS scan)
68 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
,URG
,PSH
-j PSCAN
70 iptables
-A BADTCP
-p tcp
--tcp-flags ALL SYN
,RST
,ACK
,FIN
,URG
-j PSCAN
72 iptables
-A BADTCP
-p tcp
--tcp-flags ALL ALL
-j PSCAN
74 iptables
-A BADTCP
-p tcp
--tcp-flags ALL FIN
-j PSCAN
75 # SYN/RST (also catches xmas variants that set SYN+RST+...)
76 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,RST SYN
,RST
-j PSCAN
77 # SYN/FIN (QueSO or nmap OS probe)
78 iptables
-A BADTCP
-p tcp
--tcp-flags SYN
,FIN SYN
,FIN
-j PSCAN
80 iptables
-A BADTCP
-p tcp
--tcp-flags ALL NONE
-j PSCAN
82 iptables
-A BADTCP
-p tcp
! --syn -m conntrack
--ctstate NEW
-j NEWNOTSYN
84 iptables
-A INPUT
-p tcp
-j BADTCP
85 iptables
-A FORWARD
-p tcp
-j BADTCP
87 # Connection tracking chain
89 iptables
-A CONNTRACK
-m conntrack
--ctstate ESTABLISHED
,RELATED
-j ACCEPT
90 iptables
-A CONNTRACK
-m conntrack
--ctstate INVALID
-j DROP
92 # Fix for braindead ISP's
93 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
95 # CUSTOM chains, can be used by the users themselves
96 iptables
-N CUSTOMINPUT
97 iptables
-A INPUT
-j CUSTOMINPUT
98 iptables
-N CUSTOMFORWARD
99 iptables
-A FORWARD
-j CUSTOMFORWARD
100 iptables
-N CUSTOMOUTPUT
101 iptables
-A OUTPUT
-j CUSTOMOUTPUT
102 iptables
-t nat
-N CUSTOMPREROUTING
103 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
104 iptables
-t nat
-N CUSTOMPOSTROUTING
105 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
109 iptables
-A INPUT
-j P2PBLOCK
110 iptables
-A FORWARD
-j P2PBLOCK
111 iptables
-A OUTPUT
-j P2PBLOCK
113 # Guardian (IPS) chains
115 iptables
-A INPUT
-j GUARDIAN
116 iptables
-A FORWARD
-j GUARDIAN
118 # Block OpenVPN transfer networks
119 iptables
-N OVPNBLOCK
120 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
121 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
122 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
124 # OpenVPN transfer network translation
125 iptables
-t nat
-N OVPNNAT
126 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
128 # IPTV chains for IGMPPROXY
129 iptables
-N IPTVINPUT
130 iptables
-A INPUT
-j IPTVINPUT
131 iptables
-N IPTVFORWARD
132 iptables
-A FORWARD
-j IPTVFORWARD
134 # Allow to ping the firewall.
135 iptables
-N ICMPINPUT
136 iptables
-A INPUT
-j ICMPINPUT
137 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
139 # Accept everything on loopback
141 iptables
-A LOOPBACK
-i lo
-j ACCEPT
142 iptables
-A LOOPBACK
-o lo
-j ACCEPT
144 # Filter all packets with loopback addresses on non-loopback interfaces.
145 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
146 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
148 for i
in INPUT FORWARD OUTPUT
; do
149 iptables
-A ${i} -j LOOPBACK
152 # Accept everything connected
153 for i
in INPUT FORWARD OUTPUT
; do
154 iptables
-A ${i} -j CONNTRACK
158 iptables
-N DHCPINPUT
159 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
160 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
162 iptables
-N DHCPOUTPUT
163 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
164 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
166 # Allow DHCP on GREEN
167 iptables
-N DHCPGREENINPUT
168 iptables
-N DHCPGREENOUTPUT
169 if [ -n "${GREEN_DEV}" ]; then
170 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
171 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
174 # allow DHCP on BLUE to be turned on/off
175 iptables
-N DHCPBLUEINPUT
176 iptables
-N DHCPBLUEOUTPUT
177 if [ -n "${BLUE_DEV}" ]; then
178 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
179 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
182 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
183 iptables
-N IPSECINPUT
184 iptables
-N IPSECFORWARD
185 iptables
-N IPSECOUTPUT
186 iptables
-A INPUT
-j IPSECINPUT
187 iptables
-A FORWARD
-j IPSECFORWARD
188 iptables
-A OUTPUT
-j IPSECOUTPUT
189 iptables
-t nat
-N IPSECNAT
190 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
192 # localhost and ethernet.
193 # Always allow accessing the web GUI from GREEN.
195 iptables
-A INPUT
-j GUIINPUT
196 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
199 iptables
-N WIRELESSINPUT
200 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
201 iptables
-N WIRELESSFORWARD
202 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
205 iptables
-N OVPNINPUT
206 iptables
-A INPUT
-j OVPNINPUT
209 iptables
-N TOR_INPUT
210 iptables
-A INPUT
-j TOR_INPUT
212 # Jump into the actual firewall ruleset.
214 iptables
-A INPUT
-j INPUTFW
216 iptables
-N OUTGOINGFW
217 iptables
-A OUTPUT
-j OUTGOINGFW
219 iptables
-N FORWARDFW
220 iptables
-A FORWARD
-j FORWARDFW
223 iptables
-t nat
-N NAT_SOURCE
224 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
226 # Custom prerouting chains (for transparent proxy)
227 iptables
-t nat
-N SQUID
228 iptables
-t nat
-A PREROUTING
-j SQUID
231 iptables
-t nat
-N NAT_DESTINATION
232 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
233 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
235 iptables
-t mangle
-N NAT_DESTINATION
236 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
238 iptables
-t nat
-N NAT_DESTINATION_FIX
239 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
241 iptables
-t nat
-A NAT_DESTINATION_FIX \
242 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
244 if [ -n "${BLUE_ADDRESS}" ]; then
245 iptables
-t nat
-A NAT_DESTINATION_FIX \
246 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
249 if [ -n "${ORANGE_ADDRESS}" ]; then
250 iptables
-t nat
-A NAT_DESTINATION_FIX \
251 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
254 # upnp chain for our upnp daemon
255 iptables
-t nat
-N UPNPFW
256 iptables
-t nat
-A PREROUTING
-j UPNPFW
258 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
260 # RED chain, used for the red interface
262 iptables
-A INPUT
-j REDINPUT
263 iptables
-N REDFORWARD
264 iptables
-A FORWARD
-j REDFORWARD
265 iptables
-t nat
-N REDNAT
266 iptables
-t nat
-A POSTROUTING
-j REDNAT
268 # Apply OpenVPN firewall rules
269 /usr
/local
/bin
/openvpnctrl
--firewall-rules
272 /usr
/local
/bin
/wirelessctrl
276 iptables
-A INPUT
-j POLICYIN
277 iptables
-N POLICYFWD
278 iptables
-A FORWARD
-j POLICYFWD
279 iptables
-N POLICYOUT
280 iptables
-A OUTPUT
-j POLICYOUT
282 # Initialize firewall policies.
283 /usr
/sbin
/firewall-policy
285 # Install firewall rules for the red interface.
288 # If red has not been brought up yet, we will
289 # add the blocking rules for MASQUERADE
290 if [ ! -e "/var/ipfire/red/active" ]; then
297 iptables
-F REDFORWARD
298 iptables
-t nat
-F REDNAT
300 # PPPoE / PPTP Device
301 if [ "$IFACE" != "" ]; then
303 if [ "$DEVICE" != "" ]; then
304 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
306 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
307 if [ "$RED_DEV" != "" ]; then
308 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
314 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
315 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
316 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
320 if [ "$ORANGE_DEV" != "" ]; then
321 # This rule enables a host on ORANGE network to connect to the outside
322 # (only if we have a red connection)
323 if [ "$IFACE" != "" ]; then
324 iptables
-A REDFORWARD
-i $ORANGE_DEV -o $IFACE -j ACCEPT
328 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
330 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
331 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
332 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
334 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
335 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
336 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
339 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
340 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
342 if [ "$IFACE" = "$GREEN_DEV" ]; then
343 MASQUERADE_GREEN
="off"
346 local NO_MASQ_NETWORKS
348 if [ "${MASQUERADE_GREEN}" = "off" ]; then
349 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
352 if [ "${MASQUERADE_BLUE}" = "off" ]; then
353 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
356 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
357 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
361 for network
in ${NO_MASQ_NETWORKS}; do
362 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
365 # Masquerade everything else
366 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
370 /usr
/local
/bin
/firewallctrl
373 iptables_red_down
() {
374 # Prohibit packets to reach the masquerading rule
375 # while the wan interface is down - this is required to
376 # circumvent udp related NAT issues
377 # http://forum.ipfire.org/index.php?topic=11127.0
378 if [ -n "${IFACE}" ]; then
379 iptables
-F REDFORWARD
380 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
384 /usr
/local
/bin
/firewallctrl
387 # See how we were called.
390 boot_mesg
"Loading firewall modules into the kernel"
391 modprobe iptable_nat || failed
=1
392 for i
in $
(find /lib
/modules
/$
(uname
-r) -name nf_conntrack
*); do
393 modprobe $
(basename $i | cut
-d.
-f1) || failed
=1
395 for i
in $
(find /lib
/modules
/$
(uname
-r) -name nf_nat
*); do
396 modprobe $
(basename $i | cut
-d.
-f1) || failed
=1
401 if [ -e /var
/ipfire
/main
/disable_nf_sip
]; then
403 rmmod nf_conntrack_sip
405 rmmod nf_conntrack_h323
408 boot_mesg
"Setting up firewall"
413 boot_mesg
"Reloading firewall"
418 boot_mesg
"Disabling firewall access to RED"
426 echo "Usage: $0 {start|reload|restart}"