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