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