]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/firewall
Merge remote-tracking branch 'origin/master' into next
[ipfire-2.x.git] / src / initscripts / system / firewall
1 #!/bin/sh
2
3 . /etc/sysconfig/rc
4 . ${rc_functions}
5
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'`
10
11 if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13 fi
14
15 function iptables() {
16 /sbin/iptables --wait "$@"
17 }
18
19 iptables_init() {
20 # Flush all rules and delete all custom chains
21 iptables -F
22 iptables -t nat -F
23 iptables -t mangle -F
24 iptables -t raw -F
25 iptables -X
26 iptables -t nat -X
27 iptables -t mangle -X
28 iptables -t raw -X
29
30 # Set up policies
31 iptables -P INPUT DROP
32 iptables -P FORWARD DROP
33 iptables -P OUTPUT ACCEPT
34
35 # Enable TRACE logging to syslog
36 modprobe nf_log_ipv4
37 sysctl -q -w net.netfilter.nf_log.2=nf_log_ipv4
38
39 # Empty LOG_DROP and LOG_REJECT chains
40 iptables -N LOG_DROP
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
46
47 # This chain will log, then DROPs packets with certain bad combinations
48 # of flags might indicate a port-scan attempt (xmas, null, etc.)
49 iptables -N PSCAN
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"
55 fi
56 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
57
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
60 iptables -N NEWNOTSYN
61 if [ "$DROPNEWNOTSYN" == "on" ]; then
62 iptables -A NEWNOTSYN -m limit --limit 10/second -j LOG --log-prefix "DROP_NEWNOTSYN "
63 fi
64 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
65
66 # Chain to contain all the rules relating to bad TCP flags
67 iptables -N BADTCP
68
69 # Don't check loopback
70 iptables -A BADTCP -i lo -j RETURN
71
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
75 # SYN/RST/ACK/FIN/URG
76 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
77 # ALL/ALL
78 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
79 # FIN Stealth
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
85 # Null
86 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
87 # NEW TCP without SYN
88 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
89
90 iptables -A INPUT -p tcp -j BADTCP
91 iptables -A FORWARD -p tcp -j BADTCP
92
93 # Connection tracking chains
94 iptables -N CONNTRACK
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
100
101 # Fix for braindead ISPs
102 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
103
104 # CUSTOM chains, can be used by the users themselves
105 iptables -N CUSTOMINPUT
106 iptables -A INPUT -j CUSTOMINPUT
107 iptables -N CUSTOMFORWARD
108 iptables -A FORWARD -j CUSTOMFORWARD
109 iptables -N CUSTOMOUTPUT
110 iptables -A OUTPUT -j CUSTOMOUTPUT
111 iptables -t nat -N CUSTOMPREROUTING
112 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
113 iptables -t nat -N CUSTOMPOSTROUTING
114 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
115
116 # P2PBLOCK
117 iptables -N P2PBLOCK
118 iptables -A INPUT -j P2PBLOCK
119 iptables -A FORWARD -j P2PBLOCK
120 iptables -A OUTPUT -j P2PBLOCK
121
122 # IPS (Guardian) chains
123 iptables -N GUARDIAN
124 iptables -A INPUT -j GUARDIAN
125 iptables -A FORWARD -j GUARDIAN
126
127 # Block non-established IPsec networks
128 iptables -N IPSECBLOCK
129 iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
130 iptables -A OUTPUT -m policy --dir out --pol none -j IPSECBLOCK
131
132 # Block OpenVPN transfer networks
133 iptables -N OVPNBLOCK
134 iptables -A INPUT -i tun+ -j OVPNBLOCK
135 iptables -A FORWARD -i tun+ -j OVPNBLOCK
136 iptables -A FORWARD -o tun+ -j OVPNBLOCK
137
138 # IPS (Suricata) chains
139 iptables -N IPS_INPUT
140 iptables -N IPS_FORWARD
141 iptables -N IPS_OUTPUT
142 iptables -A INPUT -j IPS_INPUT
143 iptables -A FORWARD -j IPS_FORWARD
144 iptables -A OUTPUT -j IPS_OUTPUT
145
146 # OpenVPN transfer network translation
147 iptables -t nat -N OVPNNAT
148 iptables -t nat -A POSTROUTING -j OVPNNAT
149
150 # IPTV chains for IGMPPROXY
151 iptables -N IPTVINPUT
152 iptables -A INPUT -j IPTVINPUT
153 iptables -N IPTVFORWARD
154 iptables -A FORWARD -j IPTVFORWARD
155
156 # Allow to ping the firewall.
157 iptables -N ICMPINPUT
158 iptables -A INPUT -j ICMPINPUT
159 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
160
161 # Accept everything on loopback
162 iptables -N LOOPBACK
163 iptables -A LOOPBACK -i lo -j ACCEPT
164 iptables -A LOOPBACK -o lo -j ACCEPT
165
166 # Filter all packets with loopback addresses on non-loopback interfaces.
167 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
168 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
169
170 for i in INPUT FORWARD OUTPUT; do
171 iptables -A ${i} -j LOOPBACK
172 done
173
174 # Captive portal
175 iptables -N CAPTIVE_PORTAL
176 iptables -N CAPTIVE_PORTAL_CLIENTS
177 for i in INPUT FORWARD; do
178 iptables -A ${i} -j CAPTIVE_PORTAL
179 done
180
181 # Accept everything connected
182 for i in INPUT FORWARD OUTPUT; do
183 iptables -A ${i} -j CONNTRACK
184 done
185
186 # Allow DHCP
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
190
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
194
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
201 fi
202
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
209 fi
210
211 # Location Block
212 iptables -N LOCATIONBLOCK
213 iptables -A INPUT -j LOCATIONBLOCK
214 iptables -A FORWARD -j LOCATIONBLOCK
215
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
225
226 # localhost and ethernet.
227 # Always allow accessing the web GUI from GREEN.
228 iptables -N GUIINPUT
229 iptables -A INPUT -j GUIINPUT
230 if [ -n "${GREEN_DEV}" ]; then
231 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
232 fi
233
234 # WIRELESS chains
235 iptables -N WIRELESSINPUT
236 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
237 iptables -N WIRELESSFORWARD
238 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
239
240 # OpenVPN
241 iptables -N OVPNINPUT
242 iptables -A INPUT -j OVPNINPUT
243
244 # Tor (inbound and outbound)
245 iptables -N TOR_INPUT
246 iptables -A INPUT -j TOR_INPUT
247 iptables -N TOR_OUTPUT
248 iptables -A OUTPUT -j TOR_OUTPUT
249
250 # Jump into the actual firewall ruleset.
251 iptables -N INPUTFW
252 iptables -A INPUT -j INPUTFW
253
254 iptables -N OUTGOINGFW
255 iptables -A OUTPUT -j OUTGOINGFW
256
257 iptables -N FORWARDFW
258 iptables -A FORWARD -j FORWARDFW
259
260 # SNAT rules
261 iptables -t nat -N NAT_SOURCE
262 iptables -t nat -A POSTROUTING -j NAT_SOURCE
263
264 # Captive Portal
265 iptables -t nat -N CAPTIVE_PORTAL
266 iptables -t nat -A PREROUTING -j CAPTIVE_PORTAL
267
268 # Custom prerouting chains (for transparent proxy)
269 iptables -t nat -N SQUID
270 iptables -t nat -A PREROUTING -j SQUID
271
272 # DNAT rules
273 iptables -t nat -N NAT_DESTINATION
274 iptables -t nat -A PREROUTING -j NAT_DESTINATION
275 iptables -t nat -A OUTPUT -j NAT_DESTINATION
276
277 iptables -t mangle -N NAT_DESTINATION
278 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
279
280 iptables -t nat -N NAT_DESTINATION_FIX
281 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
282
283 if [ -n "${GREEN_ADDRESS}" ]; then
284 iptables -t nat -A NAT_DESTINATION_FIX \
285 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
286 fi
287
288 if [ -n "${BLUE_ADDRESS}" ]; then
289 iptables -t nat -A NAT_DESTINATION_FIX \
290 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
291 fi
292
293 if [ -n "${ORANGE_ADDRESS}" ]; then
294 iptables -t nat -A NAT_DESTINATION_FIX \
295 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
296 fi
297
298 # RED chain, used for the red interface
299 iptables -N REDINPUT
300 iptables -A INPUT -j REDINPUT
301 iptables -N REDFORWARD
302 iptables -A FORWARD -j REDFORWARD
303 iptables -t nat -N REDNAT
304 iptables -t nat -A POSTROUTING -j REDNAT
305
306 # Populate IPsec chains
307 /usr/lib/firewall/ipsec-policy
308
309 # Apply OpenVPN firewall rules
310 /usr/local/bin/openvpnctrl --firewall-rules
311
312 # run wirelessctrl
313 /usr/local/bin/wirelessctrl
314
315 # run captivectrl
316 /usr/local/bin/captivectrl
317
318 # POLICY CHAIN
319 iptables -N POLICYIN
320 iptables -A INPUT -j POLICYIN
321 iptables -N POLICYFWD
322 iptables -A FORWARD -j POLICYFWD
323 iptables -N POLICYOUT
324 iptables -A OUTPUT -j POLICYOUT
325
326 # Initialize firewall policies.
327 /usr/sbin/firewall-policy
328
329 # Install firewall rules for the red interface.
330 iptables_red_up
331
332 # If red has not been brought up yet, we will
333 # add the blocking rules for MASQUERADE
334 if [ ! -e "/var/ipfire/red/active" ]; then
335 iptables_red_down
336 fi
337 }
338
339 iptables_red_up() {
340 iptables -F REDINPUT
341 iptables -F REDFORWARD
342 iptables -t nat -F REDNAT
343
344 # PPPoE / PPTP Device
345 if [ "$IFACE" != "" ]; then
346 # PPPoE / PPTP
347 if [ "$DEVICE" != "" ]; then
348 iptables -A REDINPUT -i $DEVICE -j ACCEPT
349 fi
350 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
351 if [ "$RED_DEV" != "" ]; then
352 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
353 fi
354 fi
355 fi
356
357 # PPTP over DHCP
358 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
359 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
360 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
361 fi
362
363 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
364 # DHCP
365 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
366 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
367 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
368 fi
369 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
370 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
371 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
372 fi
373
374 # Outgoing masquerading (don't masqerade IPsec (mark 50))
375 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
376
377 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
378 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
379 fi
380
381 local NO_MASQ_NETWORKS
382
383 if [ "${MASQUERADE_GREEN}" = "off" ]; then
384 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
385 fi
386
387 if [ "${MASQUERADE_BLUE}" = "off" ]; then
388 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
389 fi
390
391 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
392 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
393 fi
394
395 local network
396 for network in ${NO_MASQ_NETWORKS}; do
397 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
398 done
399
400 # Masquerade everything else
401 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
402 fi
403
404 # Reload all rules.
405 /usr/local/bin/firewallctrl
406 }
407
408 iptables_red_down() {
409 # Prohibit packets to reach the masquerading rule
410 # while the WAN interface is down - this is required to
411 # circumvent udp related NAT issues
412 # http://forum.ipfire.org/index.php?topic=11127.0
413 if [ -n "${IFACE}" ]; then
414 iptables -F REDFORWARD
415 iptables -A REDFORWARD -o "${IFACE}" -j DROP
416 fi
417
418 # Reload all rules.
419 /usr/local/bin/firewallctrl
420 }
421
422 # See how we were called.
423 case "$1" in
424 start)
425 boot_mesg "Setting up firewall"
426 iptables_init
427 evaluate_retval
428 ;;
429 reload|up)
430 boot_mesg "Reloading firewall"
431 iptables_red_up
432 evaluate_retval
433 ;;
434 down)
435 boot_mesg "Disabling firewall access to RED"
436 iptables_red_down
437 evaluate_retval
438 ;;
439 restart)
440 $0 start
441 ;;
442 *)
443 echo "Usage: $0 {start|reload|restart}"
444 exit 1
445 ;;
446 esac
447
448 exit 0