]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/init.d/firewall
5d6ac3a295f200441b554db737022cc747883f1c
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 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
101 -m helper
--helper sip
-j ACCEPT
102 for proto
in udp tcp
; do
103 iptables
-t raw
-A CONNTRACK
-p "${proto}" --dport 5060 -j CT
--helper sip
107 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
108 -m helper
--helper ftp -p tcp
--dport 1024: -j ACCEPT
109 iptables
-t raw
-A CONNTRACK
-p tcp
--dport 21 -j CT
--helper ftp
112 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
113 -m helper
--helper pptp
-j ACCEPT
114 iptables
-t raw
-A CONNTRACK
-p udp
--dport 1723 -j CT
--helper pptp
117 iptables
-A CONNTRACK
-m conntrack
--ctstate RELATED \
118 -m helper
--helper tftp
-j ACCEPT
119 iptables
-t raw
-A CONNTRACK
-p udp
--dport 69 -j CT
--helper tftp
121 # Fix for braindead ISP's
122 iptables
-A FORWARD
-p tcp
--tcp-flags SYN
,RST SYN
-j TCPMSS
--clamp-mss-to-pmtu
124 # CUSTOM chains, can be used by the users themselves
125 iptables
-N CUSTOMINPUT
126 iptables
-A INPUT
-j CUSTOMINPUT
127 iptables
-N CUSTOMFORWARD
128 iptables
-A FORWARD
-j CUSTOMFORWARD
129 iptables
-N CUSTOMOUTPUT
130 iptables
-A OUTPUT
-j CUSTOMOUTPUT
131 iptables
-t nat
-N CUSTOMPREROUTING
132 iptables
-t nat
-A PREROUTING
-j CUSTOMPREROUTING
133 iptables
-t nat
-N CUSTOMPOSTROUTING
134 iptables
-t nat
-A POSTROUTING
-j CUSTOMPOSTROUTING
138 iptables
-A INPUT
-j P2PBLOCK
139 iptables
-A FORWARD
-j P2PBLOCK
140 iptables
-A OUTPUT
-j P2PBLOCK
142 # Guardian (IPS) chains
144 iptables
-A INPUT
-j GUARDIAN
145 iptables
-A FORWARD
-j GUARDIAN
147 # Block OpenVPN transfer networks
148 iptables
-N OVPNBLOCK
149 iptables
-A INPUT
-i tun
+ -j OVPNBLOCK
150 iptables
-A FORWARD
-i tun
+ -j OVPNBLOCK
151 iptables
-A FORWARD
-o tun
+ -j OVPNBLOCK
153 # OpenVPN transfer network translation
154 iptables
-t nat
-N OVPNNAT
155 iptables
-t nat
-A POSTROUTING
-j OVPNNAT
157 # IPTV chains for IGMPPROXY
158 iptables
-N IPTVINPUT
159 iptables
-A INPUT
-j IPTVINPUT
160 iptables
-N IPTVFORWARD
161 iptables
-A FORWARD
-j IPTVFORWARD
163 # Allow to ping the firewall.
164 iptables
-N ICMPINPUT
165 iptables
-A INPUT
-j ICMPINPUT
166 iptables
-A ICMPINPUT
-p icmp
--icmp-type 8 -j ACCEPT
168 # Accept everything on loopback
170 iptables
-A LOOPBACK
-i lo
-j ACCEPT
171 iptables
-A LOOPBACK
-o lo
-j ACCEPT
173 # Filter all packets with loopback addresses on non-loopback interfaces.
174 iptables
-A LOOPBACK
-s 127.0.0.0/8 -j DROP
175 iptables
-A LOOPBACK
-d 127.0.0.0/8 -j DROP
177 for i
in INPUT FORWARD OUTPUT
; do
178 iptables
-A ${i} -j LOOPBACK
181 # Accept everything connected
182 for i
in INPUT FORWARD OUTPUT
; do
183 iptables
-A ${i} -j CONNTRACK
187 iptables
-N DHCPINPUT
188 iptables
-A DHCPINPUT
-p udp
--sport 68 --dport 67 -j ACCEPT
189 iptables
-A DHCPINPUT
-p tcp
--sport 68 --dport 67 -j ACCEPT
191 iptables
-N DHCPOUTPUT
192 iptables
-A DHCPOUTPUT
-p udp
--sport 67 --dport 68 -j ACCEPT
193 iptables
-A DHCPOUTPUT
-p tcp
--sport 67 --dport 68 -j ACCEPT
195 # Allow DHCP on GREEN
196 iptables
-N DHCPGREENINPUT
197 iptables
-N DHCPGREENOUTPUT
198 if [ -n "${GREEN_DEV}" ]; then
199 iptables
-A INPUT
-i "${GREEN_DEV}" -j DHCPGREENINPUT
200 iptables
-A OUTPUT
-o "${GREEN_DEV}" -j DHCPGREENOUTPUT
203 # allow DHCP on BLUE to be turned on/off
204 iptables
-N DHCPBLUEINPUT
205 iptables
-N DHCPBLUEOUTPUT
206 if [ -n "${BLUE_DEV}" ]; then
207 iptables
-A INPUT
-i "${BLUE_DEV}" -j DHCPBLUEINPUT
208 iptables
-A OUTPUT
-o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
212 iptables
-N GEOIPBLOCK
213 iptables
-A INPUT
-j GEOIPBLOCK
214 iptables
-A FORWARD
-j GEOIPBLOCK
216 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
217 iptables
-N IPSECINPUT
218 iptables
-N IPSECFORWARD
219 iptables
-N IPSECOUTPUT
220 iptables
-A INPUT
-j IPSECINPUT
221 iptables
-A FORWARD
-j IPSECFORWARD
222 iptables
-A OUTPUT
-j IPSECOUTPUT
223 iptables
-t nat
-N IPSECNAT
224 iptables
-t nat
-A POSTROUTING
-j IPSECNAT
226 # localhost and ethernet.
227 # Always allow accessing the web GUI from GREEN.
229 iptables
-A INPUT
-j GUIINPUT
230 iptables
-A GUIINPUT
-i "${GREEN_DEV}" -p tcp
--dport 444 -j ACCEPT
233 iptables
-N WIRELESSINPUT
234 iptables
-A INPUT
-m conntrack
--ctstate NEW
-j WIRELESSINPUT
235 iptables
-N WIRELESSFORWARD
236 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j WIRELESSFORWARD
239 iptables
-N OVPNINPUT
240 iptables
-A INPUT
-j OVPNINPUT
243 iptables
-N TOR_INPUT
244 iptables
-A INPUT
-j TOR_INPUT
246 # Jump into the actual firewall ruleset.
248 iptables
-A INPUT
-j INPUTFW
250 iptables
-N OUTGOINGFW
251 iptables
-A OUTPUT
-j OUTGOINGFW
253 iptables
-N FORWARDFW
254 iptables
-A FORWARD
-j FORWARDFW
257 iptables
-t nat
-N NAT_SOURCE
258 iptables
-t nat
-A POSTROUTING
-j NAT_SOURCE
260 # Custom prerouting chains (for transparent proxy)
261 iptables
-t nat
-N SQUID
262 iptables
-t nat
-A PREROUTING
-j SQUID
265 iptables
-t nat
-N NAT_DESTINATION
266 iptables
-t nat
-A PREROUTING
-j NAT_DESTINATION
267 iptables
-t nat
-A OUTPUT
-j NAT_DESTINATION
269 iptables
-t mangle
-N NAT_DESTINATION
270 iptables
-t mangle
-A PREROUTING
-j NAT_DESTINATION
272 iptables
-t nat
-N NAT_DESTINATION_FIX
273 iptables
-t nat
-A POSTROUTING
-j NAT_DESTINATION_FIX
275 iptables
-t nat
-A NAT_DESTINATION_FIX \
276 -m mark
--mark 1 -j SNAT
--to-source "${GREEN_ADDRESS}"
278 if [ -n "${BLUE_ADDRESS}" ]; then
279 iptables
-t nat
-A NAT_DESTINATION_FIX \
280 -m mark
--mark 2 -j SNAT
--to-source "${BLUE_ADDRESS}"
283 if [ -n "${ORANGE_ADDRESS}" ]; then
284 iptables
-t nat
-A NAT_DESTINATION_FIX \
285 -m mark
--mark 3 -j SNAT
--to-source "${ORANGE_ADDRESS}"
288 # upnp chain for our upnp daemon
289 iptables
-t nat
-N UPNPFW
290 iptables
-t nat
-A PREROUTING
-j UPNPFW
292 iptables
-A FORWARD
-m conntrack
--ctstate NEW
-j UPNPFW
294 # RED chain, used for the red interface
296 iptables
-A INPUT
-j REDINPUT
297 iptables
-N REDFORWARD
298 iptables
-A FORWARD
-j REDFORWARD
299 iptables
-t nat
-N REDNAT
300 iptables
-t nat
-A POSTROUTING
-j REDNAT
302 # Apply OpenVPN firewall rules
303 /usr
/local
/bin
/openvpnctrl
--firewall-rules
306 /usr
/local
/bin
/wirelessctrl
310 iptables
-A INPUT
-j POLICYIN
311 iptables
-N POLICYFWD
312 iptables
-A FORWARD
-j POLICYFWD
313 iptables
-N POLICYOUT
314 iptables
-A OUTPUT
-j POLICYOUT
316 # Initialize firewall policies.
317 /usr
/sbin
/firewall-policy
319 # Install firewall rules for the red interface.
322 # If red has not been brought up yet, we will
323 # add the blocking rules for MASQUERADE
324 if [ ! -e "/var/ipfire/red/active" ]; then
331 iptables
-F REDFORWARD
332 iptables
-t nat
-F REDNAT
334 # PPPoE / PPTP Device
335 if [ "$IFACE" != "" ]; then
337 if [ "$DEVICE" != "" ]; then
338 iptables
-A REDINPUT
-i $DEVICE -j ACCEPT
340 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
341 if [ "$RED_DEV" != "" ]; then
342 iptables
-A REDINPUT
-i $RED_DEV -j ACCEPT
348 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
349 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
350 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
354 if [ "$ORANGE_DEV" != "" ]; then
355 # This rule enables a host on ORANGE network to connect to the outside
356 # (only if we have a red connection)
357 if [ "$IFACE" != "" ]; then
358 iptables
-A REDFORWARD
-i $ORANGE_DEV -o $IFACE -j ACCEPT
362 if [ "$IFACE" != "" -a -f /var
/ipfire
/red
/active
]; then
364 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
365 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
366 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
368 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
369 iptables
-A REDINPUT
-p tcp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
370 iptables
-A REDINPUT
-p udp
--source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
373 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
374 iptables
-t nat
-A REDNAT
-m mark
--mark 50 -o $IFACE -j RETURN
376 if [ "$IFACE" = "$GREEN_DEV" ]; then
377 MASQUERADE_GREEN
="off"
380 local NO_MASQ_NETWORKS
382 if [ "${MASQUERADE_GREEN}" = "off" ]; then
383 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
386 if [ "${MASQUERADE_BLUE}" = "off" ]; then
387 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
390 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
391 NO_MASQ_NETWORKS
="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
395 for network
in ${NO_MASQ_NETWORKS}; do
396 iptables
-t nat
-A REDNAT
-s "${network}" -o "${IFACE}" -j RETURN
399 # Masquerade everything else
400 iptables
-t nat
-A REDNAT
-o $IFACE -j MASQUERADE
404 /usr
/local
/bin
/firewallctrl
407 iptables_red_down
() {
408 # Prohibit packets to reach the masquerading rule
409 # while the wan interface is down - this is required to
410 # circumvent udp related NAT issues
411 # http://forum.ipfire.org/index.php?topic=11127.0
412 if [ -n "${IFACE}" ]; then
413 iptables
-F REDFORWARD
414 iptables
-A REDFORWARD
-o "${IFACE}" -j DROP
418 /usr
/local
/bin
/firewallctrl
421 # See how we were called.
424 boot_mesg
"Setting up firewall"
429 boot_mesg
"Reloading firewall"
434 boot_mesg
"Disabling firewall access to RED"
442 echo "Usage: $0 {start|reload|restart}"