firewall: Don't require to enable the RW server for N2N networks.
[people/teissler/ipfire-2.x.git] / src / initscripts / init.d / firewall
1 #!/bin/sh
2
3 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
4 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
5 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
6 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
7
8 if [ -f /var/ipfire/red/device ]; then
9 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
10 fi
11
12 iptables_init() {
13 # Flush all rules and delete all custom chains
14 /sbin/iptables -F
15 /sbin/iptables -t nat -F
16 /sbin/iptables -t mangle -F
17 /sbin/iptables -X
18 /sbin/iptables -t nat -X
19 /sbin/iptables -t mangle -X
20
21 # Set up policies
22 /sbin/iptables -P INPUT DROP
23 /sbin/iptables -P FORWARD DROP
24 /sbin/iptables -P OUTPUT ACCEPT
25
26 # Empty LOG_DROP and LOG_REJECT chains
27 /sbin/iptables -N LOG_DROP
28 /sbin/iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
29 /sbin/iptables -A LOG_DROP -j DROP
30 /sbin/iptables -N LOG_REJECT
31 /sbin/iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
32 /sbin/iptables -A LOG_REJECT -j REJECT
33
34 # This chain will log, then DROPs packets with certain bad combinations
35 # of flags might indicate a port-scan attempt (xmas, null, etc)
36 /sbin/iptables -N PSCAN
37 if [ "$DROPPORTSCAN" == "on" ]; then
38 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
39 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
40 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
41 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
42 fi
43 /sbin/iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
44
45 # New tcp packets without SYN set - could well be an obscure type of port scan
46 # that's not covered above, may just be a broken windows machine
47 /sbin/iptables -N NEWNOTSYN
48 if [ "$DROPNEWNOTSYN" == "on" ]; then
49 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
50 fi
51 /sbin/iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
52
53 # Chain to contain all the rules relating to bad TCP flags
54 /sbin/iptables -N BADTCP
55
56 #Don't check loopback
57 /sbin/iptables -A BADTCP -i lo -j RETURN
58
59 # Disallow packets frequently used by port-scanners
60 # nmap xmas
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
62 # Null
63 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
64 # FIN
65 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
66 # SYN/RST (also catches xmas variants that set SYN+RST+...)
67 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
68 # SYN/FIN (QueSO or nmap OS probe)
69 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
70 # NEW TCP without SYN
71 /sbin/iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
72
73 /sbin/iptables -A INPUT -p tcp -j BADTCP
74 /sbin/iptables -A FORWARD -p tcp -j BADTCP
75
76 # Connection tracking chain
77 /sbin/iptables -N CONNTRACK
78 /sbin/iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
79
80 # Fix for braindead ISP's
81 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
82
83 # CUSTOM chains, can be used by the users themselves
84 /sbin/iptables -N CUSTOMINPUT
85 /sbin/iptables -A INPUT -j CUSTOMINPUT
86 /sbin/iptables -N CUSTOMFORWARD
87 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
88 /sbin/iptables -N CUSTOMOUTPUT
89 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
90 /sbin/iptables -t nat -N CUSTOMPREROUTING
91 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
92 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
93 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
94
95 # Guardian (IPS) chains
96 /sbin/iptables -N GUARDIAN
97 /sbin/iptables -A INPUT -j GUARDIAN
98 /sbin/iptables -A FORWARD -j GUARDIAN
99
100 # Block OpenVPN transfer networks
101 /sbin/iptables -N OVPNBLOCK
102 for i in INPUT FORWARD OUTPUT; do
103 /sbin/iptables -A ${i} -j OVPNBLOCK
104 done
105
106 # OpenVPN transfer network translation
107 /sbin/iptables -t nat -N OVPNNAT
108 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
109
110 # IPTV chains for IGMPPROXY
111 /sbin/iptables -N IPTVINPUT
112 /sbin/iptables -A INPUT -j IPTVINPUT
113 /sbin/iptables -N IPTVFORWARD
114 /sbin/iptables -A FORWARD -j IPTVFORWARD
115
116 # filtering from GUI
117 /sbin/iptables -N GUIINPUT
118 /sbin/iptables -A INPUT -j GUIINPUT
119 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
120
121 # Accept everything on loopback
122 /sbin/iptables -N LOOPBACK
123 /sbin/iptables -A LOOPBACK -i lo -j ACCEPT
124 /sbin/iptables -A LOOPBACK -o lo -j ACCEPT
125
126 # Filter all packets with loopback addresses on non-loopback interfaces.
127 /sbin/iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
128 /sbin/iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
129
130 for i in INPUT FORWARD OUTPUT; do
131 /sbin/iptables -A ${i} -j LOOPBACK
132 done
133
134 # Accept everything connected
135 for i in INPUT FORWARD OUTPUT; do
136 /sbin/iptables -A ${i} -j CONNTRACK
137 done
138
139 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
140 /sbin/iptables -N IPSECINPUT
141 /sbin/iptables -N IPSECFORWARD
142 /sbin/iptables -N IPSECOUTPUT
143 /sbin/iptables -A INPUT -j IPSECINPUT
144 /sbin/iptables -A FORWARD -j IPSECFORWARD
145 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
146 /sbin/iptables -t nat -N IPSECNAT
147 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
148
149 # localhost and ethernet.
150 /sbin/iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp
151
152 # allow DHCP on BLUE to be turned on/off
153 /sbin/iptables -N DHCPBLUEINPUT
154 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
155
156 # WIRELESS chains
157 /sbin/iptables -N WIRELESSINPUT
158 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
159 /sbin/iptables -N WIRELESSFORWARD
160 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
161
162 # OpenVPN
163 /sbin/iptables -N OVPNINPUT
164 /sbin/iptables -A INPUT -j OVPNINPUT
165
166 # TOR
167 /sbin/iptables -N TOR_INPUT
168 /sbin/iptables -A INPUT -j TOR_INPUT
169
170 # Jump into the actual firewall ruleset.
171 /sbin/iptables -N INPUTFW
172 /sbin/iptables -A INPUT -j INPUTFW
173
174 /sbin/iptables -N OUTGOINGFW
175 /sbin/iptables -A OUTPUT -j OUTGOINGFW
176
177 /sbin/iptables -N FORWARDFW
178 /sbin/iptables -A FORWARD -j FORWARDFW
179
180 # RED chain, used for the red interface
181 /sbin/iptables -N REDINPUT
182 /sbin/iptables -A INPUT -j REDINPUT
183 /sbin/iptables -N REDFORWARD
184 /sbin/iptables -A FORWARD -j REDFORWARD
185 /sbin/iptables -t nat -N REDNAT
186 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
187
188 iptables_red
189
190 # Custom prerouting chains (for transparent proxy)
191 /sbin/iptables -t nat -N SQUID
192 /sbin/iptables -t nat -A PREROUTING -j SQUID
193
194 # DNAT rules
195 /sbin/iptables -t nat -N NAT_DESTINATION
196 /sbin/iptables -t nat -A PREROUTING -j NAT_DESTINATION
197
198 # SNAT rules
199 /sbin/iptables -t nat -N NAT_SOURCE
200 /sbin/iptables -t nat -A POSTROUTING -j NAT_SOURCE
201
202 # upnp chain for our upnp daemon
203 /sbin/iptables -t nat -N UPNPFW
204 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
205 /sbin/iptables -N UPNPFW
206 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
207
208 # run local firewall configuration, if present
209 if [ -x /etc/sysconfig/firewall.local ]; then
210 /etc/sysconfig/firewall.local start
211 fi
212
213 # Apply OpenVPN firewall rules
214 /usr/local/bin/openvpnctrl --firewall-rules
215
216 # run wirelessctrl
217 /usr/local/bin/wirelessctrl
218
219 #POLICY CHAIN
220 /sbin/iptables -N POLICYIN
221 /sbin/iptables -A INPUT -j POLICYIN
222 /sbin/iptables -N POLICYFWD
223 /sbin/iptables -A FORWARD -j POLICYFWD
224 /sbin/iptables -N POLICYOUT
225 /sbin/iptables -A OUTPUT -j POLICYOUT
226
227 /usr/sbin/firewall-policy
228
229 # read new firewall
230 /usr/local/bin/firewallctrl
231
232 if [ "$DROPINPUT" == "on" ]; then
233 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT"
234 fi
235 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
236 if [ "$DROPFORWARD" == "on" ]; then
237 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD"
238 fi
239 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_FORWARD"
240 }
241
242 iptables_red() {
243 /sbin/iptables -F REDINPUT
244 /sbin/iptables -F REDFORWARD
245 /sbin/iptables -t nat -F REDNAT
246
247 # PPPoE / PPTP Device
248 if [ "$IFACE" != "" ]; then
249 # PPPoE / PPTP
250 if [ "$DEVICE" != "" ]; then
251 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
252 fi
253 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
254 if [ "$RED_DEV" != "" ]; then
255 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
256 fi
257 fi
258 fi
259
260 # PPTP over DHCP
261 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
262 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
263 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
264 fi
265
266 # Orange pinholes
267 if [ "$ORANGE_DEV" != "" ]; then
268 # This rule enables a host on ORANGE network to connect to the outside
269 # (only if we have a red connection)
270 if [ "$IFACE" != "" ]; then
271 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
272 fi
273 fi
274
275 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
276 # DHCP
277 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
278 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
279 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
280 fi
281 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
282 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
283 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
284 fi
285
286 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
287 /sbin/iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
288 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
289
290 fi
291 }
292
293 # See how we were called.
294 case "$1" in
295 start)
296 iptables_init
297 ;;
298 reload)
299 iptables_red
300 # run local firewall configuration, if present
301 if [ -x /etc/sysconfig/firewall.local ]; then
302 /etc/sysconfig/firewall.local reload
303 fi
304 ;;
305 restart)
306 # run local firewall configuration, if present
307 if [ -x /etc/sysconfig/firewall.local ]; then
308 /etc/sysconfig/firewall.local stop
309 fi
310 $0 start
311 ;;
312 *)
313 echo "Usage: $0 {start|reload|restart}"
314 exit 1
315 ;;
316 esac
317
318 exit 0