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