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