]>
Commit | Line | Data |
---|---|---|
3a1019f6 MT |
1 | #!/bin/sh |
2 | ||
0f5c5ce7 MT |
3 | . /etc/sysconfig/rc |
4 | . ${rc_functions} | |
5 | ||
3a1019f6 MT |
6 | eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings) |
7 | eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) | |
fe0cd647 | 8 | eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings) |
3a1019f6 MT |
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 | ||
c581b670 MT |
15 | function iptables() { |
16 | /sbin/iptables --wait "$@" | |
17 | } | |
18 | ||
3a1019f6 MT |
19 | iptables_init() { |
20 | # Flush all rules and delete all custom chains | |
c581b670 MT |
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 | |
3a1019f6 MT |
27 | |
28 | # Set up policies | |
c581b670 MT |
29 | iptables -P INPUT DROP |
30 | iptables -P FORWARD DROP | |
31 | iptables -P OUTPUT ACCEPT | |
3a1019f6 MT |
32 | |
33 | # Empty LOG_DROP and LOG_REJECT chains | |
c581b670 MT |
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 | |
3a1019f6 MT |
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) | |
c581b670 | 43 | iptables -N PSCAN |
5595bc03 | 44 | if [ "$DROPPORTSCAN" == "on" ]; then |
c581b670 MT |
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" | |
5595bc03 | 49 | fi |
c581b670 | 50 | iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan" |
3a1019f6 MT |
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 | |
c581b670 | 54 | iptables -N NEWNOTSYN |
5595bc03 | 55 | if [ "$DROPNEWNOTSYN" == "on" ]; then |
c581b670 | 56 | iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN " |
5595bc03 | 57 | fi |
c581b670 | 58 | iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN" |
3a1019f6 MT |
59 | |
60 | # Chain to contain all the rules relating to bad TCP flags | |
c581b670 | 61 | iptables -N BADTCP |
3a1019f6 | 62 | |
c581b670 MT |
63 | # Don't check loopback |
64 | iptables -A BADTCP -i lo -j RETURN | |
d8158ca6 | 65 | |
3a1019f6 MT |
66 | # Disallow packets frequently used by port-scanners |
67 | # nmap xmas | |
c581b670 | 68 | iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN |
3a1019f6 | 69 | # Null |
c581b670 | 70 | iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN |
3a1019f6 | 71 | # FIN |
c581b670 | 72 | iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN |
3a1019f6 | 73 | # SYN/RST (also catches xmas variants that set SYN+RST+...) |
c581b670 | 74 | iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN |
3a1019f6 | 75 | # SYN/FIN (QueSO or nmap OS probe) |
c581b670 | 76 | iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN |
3a1019f6 | 77 | # NEW TCP without SYN |
c581b670 | 78 | iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN |
b85d2a98 | 79 | |
c581b670 MT |
80 | iptables -A INPUT -p tcp -j BADTCP |
81 | iptables -A FORWARD -p tcp -j BADTCP | |
c0359d6d | 82 | |
b85d2a98 | 83 | # Connection tracking chain |
c581b670 MT |
84 | iptables -N CONNTRACK |
85 | iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT | |
3a1019f6 | 86 | |
3a1019f6 | 87 | # Fix for braindead ISP's |
c581b670 | 88 | iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
3a1019f6 MT |
89 | |
90 | # CUSTOM chains, can be used by the users themselves | |
c581b670 MT |
91 | iptables -N CUSTOMINPUT |
92 | iptables -A INPUT -j CUSTOMINPUT | |
93 | iptables -N CUSTOMFORWARD | |
94 | iptables -A FORWARD -j CUSTOMFORWARD | |
95 | iptables -N CUSTOMOUTPUT | |
96 | iptables -A OUTPUT -j CUSTOMOUTPUT | |
97 | iptables -t nat -N CUSTOMPREROUTING | |
98 | iptables -t nat -A PREROUTING -j CUSTOMPREROUTING | |
99 | iptables -t nat -N CUSTOMPOSTROUTING | |
100 | iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING | |
3a1019f6 | 101 | |
815eaff4 | 102 | # Guardian (IPS) chains |
c581b670 MT |
103 | iptables -N GUARDIAN |
104 | iptables -A INPUT -j GUARDIAN | |
105 | iptables -A FORWARD -j GUARDIAN | |
815eaff4 | 106 | |
1e555330 | 107 | # Block OpenVPN transfer networks |
c581b670 | 108 | iptables -N OVPNBLOCK |
a0a5c14f MT |
109 | iptables -A INPUT -i tun+ -j OVPNBLOCK |
110 | iptables -A OUTPUT -o tun+ -j OVPNBLOCK | |
111 | iptables -A FORWARD -i tun+ -j OVPNBLOCK | |
112 | iptables -A FORWARD -o tun+ -j OVPNBLOCK | |
1e555330 | 113 | |
51ab1de1 | 114 | # OpenVPN transfer network translation |
c581b670 MT |
115 | iptables -t nat -N OVPNNAT |
116 | iptables -t nat -A POSTROUTING -j OVPNNAT | |
51ab1de1 | 117 | |
daa1ceba | 118 | # IPTV chains for IGMPPROXY |
c581b670 MT |
119 | iptables -N IPTVINPUT |
120 | iptables -A INPUT -j IPTVINPUT | |
121 | iptables -N IPTVFORWARD | |
122 | iptables -A FORWARD -j IPTVFORWARD | |
daa1ceba | 123 | |
3a1019f6 | 124 | # filtering from GUI |
c581b670 MT |
125 | iptables -N GUIINPUT |
126 | iptables -A INPUT -j GUIINPUT | |
127 | iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT | |
3a1019f6 | 128 | |
afc611d4 | 129 | # Accept everything on loopback |
c581b670 MT |
130 | iptables -N LOOPBACK |
131 | iptables -A LOOPBACK -i lo -j ACCEPT | |
132 | iptables -A LOOPBACK -o lo -j ACCEPT | |
afc611d4 | 133 | |
3b9a23ce | 134 | # Filter all packets with loopback addresses on non-loopback interfaces. |
c581b670 MT |
135 | iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP |
136 | iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP | |
3b9a23ce MT |
137 | |
138 | for i in INPUT FORWARD OUTPUT; do | |
c581b670 | 139 | iptables -A ${i} -j LOOPBACK |
3b9a23ce | 140 | done |
afc611d4 | 141 | |
3a1019f6 | 142 | # Accept everything connected |
b85d2a98 | 143 | for i in INPUT FORWARD OUTPUT; do |
c581b670 | 144 | iptables -A ${i} -j CONNTRACK |
b85d2a98 MT |
145 | done |
146 | ||
5fd30232 | 147 | # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything |
c581b670 MT |
148 | iptables -N IPSECINPUT |
149 | iptables -N IPSECFORWARD | |
150 | iptables -N IPSECOUTPUT | |
151 | iptables -A INPUT -j IPSECINPUT | |
152 | iptables -A FORWARD -j IPSECFORWARD | |
153 | iptables -A OUTPUT -j IPSECOUTPUT | |
154 | iptables -t nat -N IPSECNAT | |
155 | iptables -t nat -A POSTROUTING -j IPSECNAT | |
b68e5c14 | 156 | |
3a1019f6 | 157 | # localhost and ethernet. |
c581b670 | 158 | iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp |
218b3341 | 159 | |
3a1019f6 | 160 | # allow DHCP on BLUE to be turned on/off |
c581b670 MT |
161 | iptables -N DHCPBLUEINPUT |
162 | iptables -A INPUT -j DHCPBLUEINPUT | |
81393987 AM |
163 | |
164 | # WIRELESS chains | |
c581b670 MT |
165 | iptables -N WIRELESSINPUT |
166 | iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT | |
167 | iptables -N WIRELESSFORWARD | |
168 | iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD | |
987b75bc | 169 | |
ab4876ad | 170 | # OpenVPN |
c581b670 MT |
171 | iptables -N OVPNINPUT |
172 | iptables -A INPUT -j OVPNINPUT | |
ab4876ad | 173 | |
987b75bc | 174 | # TOR |
c581b670 MT |
175 | iptables -N TOR_INPUT |
176 | iptables -A INPUT -j TOR_INPUT | |
12dcfbbd | 177 | |
d5f1422d | 178 | # Jump into the actual firewall ruleset. |
c581b670 MT |
179 | iptables -N INPUTFW |
180 | iptables -A INPUT -j INPUTFW | |
d5f1422d | 181 | |
c581b670 MT |
182 | iptables -N OUTGOINGFW |
183 | iptables -A OUTPUT -j OUTGOINGFW | |
d5f1422d | 184 | |
c581b670 MT |
185 | iptables -N FORWARDFW |
186 | iptables -A FORWARD -j FORWARDFW | |
d5f1422d | 187 | |
fac38614 | 188 | # SNAT rules |
c581b670 MT |
189 | iptables -t nat -N NAT_SOURCE |
190 | iptables -t nat -A POSTROUTING -j NAT_SOURCE | |
fac38614 | 191 | |
bb12dd7b | 192 | # Custom prerouting chains (for transparent proxy) |
c581b670 MT |
193 | iptables -t nat -N SQUID |
194 | iptables -t nat -A PREROUTING -j SQUID | |
bb12dd7b MT |
195 | |
196 | # DNAT rules | |
c581b670 MT |
197 | iptables -t nat -N NAT_DESTINATION |
198 | iptables -t nat -A PREROUTING -j NAT_DESTINATION | |
bb12dd7b | 199 | |
6e87f0aa MT |
200 | iptables -t mangle -N NAT_DESTINATION |
201 | iptables -t mangle -A PREROUTING -j NAT_DESTINATION | |
202 | ||
203 | iptables -t nat -N NAT_DESTINATION_FIX | |
204 | iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX | |
205 | ||
206 | iptables -t nat -A NAT_DESTINATION_FIX \ | |
207 | -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}" | |
208 | ||
209 | if [ -n "${BLUE_ADDRESS}" ]; then | |
210 | iptables -t nat -A NAT_DESTINATION_FIX \ | |
211 | -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}" | |
212 | fi | |
213 | ||
214 | if [ -n "${ORANGE_ADDRESS}" ]; then | |
215 | iptables -t nat -A NAT_DESTINATION_FIX \ | |
216 | -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}" | |
217 | fi | |
218 | ||
7e7495b3 | 219 | # upnp chain for our upnp daemon |
c581b670 MT |
220 | iptables -t nat -N UPNPFW |
221 | iptables -t nat -A PREROUTING -j UPNPFW | |
222 | iptables -N UPNPFW | |
223 | iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW | |
3a1019f6 | 224 | |
6e87f0aa MT |
225 | # RED chain, used for the red interface |
226 | iptables -N REDINPUT | |
227 | iptables -A INPUT -j REDINPUT | |
228 | iptables -N REDFORWARD | |
229 | iptables -A FORWARD -j REDFORWARD | |
230 | iptables -t nat -N REDNAT | |
231 | iptables -t nat -A POSTROUTING -j REDNAT | |
232 | ||
63f2fb7f MT |
233 | # Filter logging of incoming broadcasts. |
234 | iptables -N BROADCAST_FILTER | |
235 | iptables -A INPUT -j BROADCAST_FILTER | |
236 | ||
237 | iptables -A BROADCAST_FILTER -i "${GREEN_DEV}" -d "${GREEN_BROADCAST}" -j DROP | |
238 | ||
239 | if [ -n "${BLUE_DEV}" -a -n "${BLUE_BROADCAST}" ]; then | |
240 | iptables -A BROADCAST_FILTER -i "${BLUE_DEV}" -d "${BLUE_BROADCAST}" -j DROP | |
241 | fi | |
242 | ||
243 | if [ -n "${ORANGE_DEV}" -a -n "${ORANGE_BROADCAST}" ]; then | |
244 | iptables -A BROADCAST_FILTER -i "${ORANGE_DEV}" -d "${ORANGE_BROADCAST}" -j DROP | |
245 | fi | |
246 | ||
ab4876ad MT |
247 | # Apply OpenVPN firewall rules |
248 | /usr/local/bin/openvpnctrl --firewall-rules | |
ff4770c7 AM |
249 | |
250 | # run wirelessctrl | |
251 | /usr/local/bin/wirelessctrl | |
252 | ||
c581b670 MT |
253 | # POLICY CHAIN |
254 | iptables -N POLICYIN | |
255 | iptables -A INPUT -j POLICYIN | |
256 | iptables -N POLICYFWD | |
257 | iptables -A FORWARD -j POLICYFWD | |
258 | iptables -N POLICYOUT | |
259 | iptables -A OUTPUT -j POLICYOUT | |
b324de14 | 260 | |
bb383423 | 261 | # Initialize firewall policies. |
5d7faa45 | 262 | /usr/sbin/firewall-policy |
690b0bd7 | 263 | |
bb383423 MT |
264 | # Install firewall rules for the red interface. |
265 | iptables_red | |
ff4770c7 | 266 | } |
3a1019f6 | 267 | |
ff4770c7 | 268 | iptables_red() { |
c581b670 MT |
269 | iptables -F REDINPUT |
270 | iptables -F REDFORWARD | |
271 | iptables -t nat -F REDNAT | |
3a1019f6 | 272 | |
ff4770c7 AM |
273 | # PPPoE / PPTP Device |
274 | if [ "$IFACE" != "" ]; then | |
275 | # PPPoE / PPTP | |
276 | if [ "$DEVICE" != "" ]; then | |
c581b670 | 277 | iptables -A REDINPUT -i $DEVICE -j ACCEPT |
ff4770c7 AM |
278 | fi |
279 | if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then | |
280 | if [ "$RED_DEV" != "" ]; then | |
c581b670 | 281 | iptables -A REDINPUT -i $RED_DEV -j ACCEPT |
ff4770c7 AM |
282 | fi |
283 | fi | |
3a1019f6 | 284 | fi |
ff4770c7 AM |
285 | |
286 | # PPTP over DHCP | |
287 | if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then | |
c581b670 MT |
288 | iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT |
289 | iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT | |
3a1019f6 MT |
290 | fi |
291 | ||
ff4770c7 AM |
292 | # Orange pinholes |
293 | if [ "$ORANGE_DEV" != "" ]; then | |
294 | # This rule enables a host on ORANGE network to connect to the outside | |
295 | # (only if we have a red connection) | |
296 | if [ "$IFACE" != "" ]; then | |
c581b670 | 297 | iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT |
ff4770c7 | 298 | fi |
3a1019f6 | 299 | fi |
c400fe4c | 300 | |
ff4770c7 AM |
301 | if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then |
302 | # DHCP | |
303 | if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then | |
c581b670 MT |
304 | iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT |
305 | iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT | |
ff4770c7 AM |
306 | fi |
307 | if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then | |
c581b670 MT |
308 | iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT |
309 | iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT | |
ff4770c7 AM |
310 | fi |
311 | ||
312 | # Outgoing masquerading (don't masqerade IPSEC (mark 50)) | |
c581b670 MT |
313 | iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN |
314 | iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE | |
c400fe4c | 315 | |
6be0579b | 316 | fi |
66f6b279 MT |
317 | |
318 | # Reload all rules. | |
55a5bcae | 319 | /usr/local/bin/firewallctrl |
ff4770c7 AM |
320 | } |
321 | ||
322 | # See how we were called. | |
323 | case "$1" in | |
324 | start) | |
cdb725da MT |
325 | boot_mesg "Loading firewall modules into the kernel" |
326 | modprobe iptable_nat || failed=1 | |
327 | for i in $(find /lib/modules/$(uname -r) -name nf_conntrack*); do | |
328 | modprobe $(basename $i | cut -d. -f1) || failed=1 | |
329 | done | |
330 | for i in $(find /lib/modules/$(uname -r) -name nf_nat*); do | |
331 | modprobe $(basename $i | cut -d. -f1) || failed=1 | |
332 | done | |
333 | (exit ${failed}) | |
334 | evaluate_retval | |
335 | ||
336 | if [ -e /var/ipfire/main/disable_nf_sip ]; then | |
337 | rmmod nf_nat_sip | |
338 | rmmod nf_conntrack_sip | |
339 | rmmod nf_nat_h323 | |
340 | rmmod nf_conntrack_h323 | |
341 | fi | |
342 | ||
7d7740a4 | 343 | boot_mesg "Setting up firewall" |
ff4770c7 | 344 | iptables_init |
7d7740a4 MT |
345 | evaluate_retval |
346 | ||
159c55c5 MT |
347 | # run local firewall configuration, if present |
348 | if [ -x /etc/sysconfig/firewall.local ]; then | |
349 | /etc/sysconfig/firewall.local start | |
350 | fi | |
6be0579b | 351 | ;; |
3a1019f6 | 352 | reload) |
7d7740a4 | 353 | boot_mesg "Reloading firewall" |
3a1019f6 | 354 | iptables_red |
7d7740a4 MT |
355 | evaluate_retval |
356 | ||
3a1019f6 | 357 | # run local firewall configuration, if present |
ff4770c7 | 358 | if [ -x /etc/sysconfig/firewall.local ]; then |
3a1019f6 MT |
359 | /etc/sysconfig/firewall.local reload |
360 | fi | |
361 | ;; | |
362 | restart) | |
ff4770c7 AM |
363 | # run local firewall configuration, if present |
364 | if [ -x /etc/sysconfig/firewall.local ]; then | |
365 | /etc/sysconfig/firewall.local stop | |
366 | fi | |
3a1019f6 MT |
367 | $0 start |
368 | ;; | |
369 | *) | |
ff4770c7 | 370 | echo "Usage: $0 {start|reload|restart}" |
3a1019f6 MT |
371 | exit 1 |
372 | ;; | |
373 | esac | |
374 | ||
375 | exit 0 |