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