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