ad30c3d1d4523ddfce5a56223023bd54613d6e7b
[people/pmueller/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 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
6
7 if [ -f /var/ipfire/red/device ]; then
8 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
9 fi
10
11 iptables_init() {
12 # Flush all rules and delete all custom chains
13 /sbin/iptables -F
14 /sbin/iptables -t nat -F
15 /sbin/iptables -t mangle -F
16 /sbin/iptables -X
17 /sbin/iptables -t nat -X
18 /sbin/iptables -t mangle -X
19
20 # Set up policies
21 /sbin/iptables -P INPUT DROP
22 /sbin/iptables -P FORWARD DROP
23 /sbin/iptables -P OUTPUT ACCEPT
24
25 # Empty LOG_DROP and LOG_REJECT chains
26 /sbin/iptables -N LOG_DROP
27 /sbin/iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
28 /sbin/iptables -A LOG_DROP -j DROP
29 /sbin/iptables -N LOG_REJECT
30 /sbin/iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
31 /sbin/iptables -A LOG_REJECT -j REJECT
32
33 # This chain will log, then DROPs packets with certain bad combinations
34 # of flags might indicate a port-scan attempt (xmas, null, etc)
35 /sbin/iptables -N PSCAN
36 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "TCP Scan? "
37 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "UDP Scan? "
38 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "ICMP Scan? "
39 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "FRAG Scan? "
40 /sbin/iptables -A PSCAN -j DROP
41
42 # New tcp packets without SYN set - could well be an obscure type of port scan
43 # that's not covered above, may just be a broken windows machine
44 /sbin/iptables -N NEWNOTSYN
45 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "NEW not SYN? "
46 /sbin/iptables -A NEWNOTSYN -j DROP
47
48 # Chain to contain all the rules relating to bad TCP flags
49 /sbin/iptables -N BADTCP
50
51 # Disallow packets frequently used by port-scanners
52 # nmap xmas
53 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
54 # Null
55 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
56 # FIN
57 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
58 # SYN/RST (also catches xmas variants that set SYN+RST+...)
59 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
60 # SYN/FIN (QueSO or nmap OS probe)
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
62 # NEW TCP without SYN
63 /sbin/iptables -A BADTCP -p tcp ! --syn -m state --state NEW -j NEWNOTSYN
64
65 /sbin/iptables -A INPUT -j BADTCP
66 /sbin/iptables -A FORWARD -j BADTCP
67
68 }
69
70 iptables_red() {
71 /sbin/iptables -F REDINPUT
72 /sbin/iptables -F REDFORWARD
73 /sbin/iptables -t nat -F REDNAT
74
75 # PPPoE / PPTP Device
76 if [ "$IFACE" != "" ]; then
77 # PPPoE / PPTP
78 if [ "$DEVICE" != "" ]; then
79 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
80 fi
81 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
82 if [ "$RED_DEV" != "" ]; then
83 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
84 fi
85 fi
86 fi
87
88 # PPTP over DHCP
89 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
90 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
91 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
92 fi
93
94 # Orange pinholes
95 if [ "$ORANGE_DEV" != "" ]; then
96 # This rule enables a host on ORANGE network to connect to the outside
97 # (only if we have a red connection)
98 if [ "$IFACE" != "" ]; then
99 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p tcp -o $IFACE -j ACCEPT
100 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p udp -o $IFACE -j ACCEPT
101 fi
102 fi
103
104 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
105 # DHCP
106 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
107 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
108 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
109 fi
110 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
111 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
112 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
113 fi
114
115 # Outgoing masquerading
116 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
117
118 fi
119 }
120
121 # See how we were called.
122 case "$1" in
123 start)
124 iptables_init
125
126 # Limit Packets- helps reduce dos/syn attacks
127 # original do nothing line
128 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 10/sec
129 # the correct one, but the negative '!' do nothing...
130 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit ! --limit 10/sec -j DROP
131
132 # Fix for braindead ISP's
133 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
134
135 # CUSTOM chains, can be used by the users themselves
136 /sbin/iptables -N CUSTOMINPUT
137 /sbin/iptables -A INPUT -j CUSTOMINPUT
138 /sbin/iptables -N CUSTOMFORWARD
139 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
140 /sbin/iptables -N CUSTOMOUTPUT
141 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
142 /sbin/iptables -N OUTGOINGFW
143 /sbin/iptables -A OUTPUT -j OUTGOINGFW
144 /sbin/iptables -t nat -N CUSTOMPREROUTING
145 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
146 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
147 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
148
149 # filtering from GUI
150 /sbin/iptables -N GUIINPUT
151 /sbin/iptables -A INPUT -j GUIINPUT
152 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
153
154 # Accept everything connected
155 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
156 /sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
157
158 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
159 /sbin/iptables -N IPSECVIRTUAL
160 /sbin/iptables -N OPENSSLVIRTUAL
161 /sbin/iptables -A INPUT -j IPSECVIRTUAL
162 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL
163 /sbin/iptables -A FORWARD -j IPSECVIRTUAL
164 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL
165
166 # Outgoing Firewall
167 /sbin/iptables -A FORWARD -j OUTGOINGFW
168
169 # localhost and ethernet.
170 /sbin/iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
171 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m state --state NEW -j DROP # Loopback not on lo
172 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m state --state NEW -j DROP
173 /sbin/iptables -A FORWARD -i lo -m state --state NEW -j ACCEPT
174 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m state --state NEW -j DROP
175 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m state --state NEW -j DROP
176 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT -p ! icmp
177 /sbin/iptables -A FORWARD -i $GREEN_DEV -m state --state NEW -j ACCEPT
178
179 # If a host on orange tries to initiate a connection to IPFire's red IP and
180 # the connection gets DNATed back through a port forward to a server on orange
181 # we end up with orange -> orange traffic passing through IPFire
182 [ "$ORANGE_DEV" != "" ] && /sbin/iptables -A FORWARD -i $ORANGE_DEV -o $ORANGE_DEV -m state --state NEW -j ACCEPT
183
184 # allow DHCP on BLUE to be turned on/off
185 /sbin/iptables -N DHCPBLUEINPUT
186 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
187
188 # IPSec
189 /sbin/iptables -N IPSECPHYSICAL
190 /sbin/iptables -A INPUT -j IPSECPHYSICAL
191
192 # OPenSSL
193 /sbin/iptables -N OPENSSLPHYSICAL
194 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
195
196 # WIRELESS chains
197 /sbin/iptables -N WIRELESSINPUT
198 /sbin/iptables -A INPUT -m state --state NEW -j WIRELESSINPUT
199 /sbin/iptables -N WIRELESSFORWARD
200 /sbin/iptables -A FORWARD -m state --state NEW -j WIRELESSFORWARD
201
202 # RED chain, used for the red interface
203 /sbin/iptables -N REDINPUT
204 /sbin/iptables -A INPUT -j REDINPUT
205 /sbin/iptables -N REDFORWARD
206 /sbin/iptables -A FORWARD -j REDFORWARD
207 /sbin/iptables -t nat -N REDNAT
208 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
209
210 iptables_red
211
212 # DMZ pinhole chain. setdmzholes setuid prog adds rules here to allow
213 # ORANGE to talk to GREEN / BLUE.
214 /sbin/iptables -N DMZHOLES
215 if [ "$ORANGE_DEV" != "" ]; then
216 /sbin/iptables -A FORWARD -i $ORANGE_DEV -m state --state NEW -j DMZHOLES
217 fi
218
219 # XTACCESS chain, used for external access
220 /sbin/iptables -N XTACCESS
221 /sbin/iptables -A INPUT -m state --state NEW -j XTACCESS
222
223 # PORTFWACCESS chain, used for portforwarding
224 /sbin/iptables -N PORTFWACCESS
225 /sbin/iptables -A FORWARD -m state --state NEW -j PORTFWACCESS
226
227 # Custom prerouting chains (for transparent proxy and port forwarding)
228 /sbin/iptables -t nat -N SQUID
229 /sbin/iptables -t nat -A PREROUTING -j SQUID
230 /sbin/iptables -t nat -N PORTFW
231 /sbin/iptables -t nat -A PREROUTING -j PORTFW
232
233 # upnp chain for our upnp daemon
234 /sbin/iptables -t nat -N UPNPFW
235 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
236
237
238 # Custom mangle chain (for port fowarding)
239 /sbin/iptables -t mangle -N PORTFWMANGLE
240 /sbin/iptables -t mangle -A PREROUTING -j PORTFWMANGLE
241
242 # Postrouting rules (for port forwarding)
243 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT \
244 --to-source $GREEN_ADDRESS
245 if [ "$BLUE_DEV" != "" ]; then
246 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
247 fi
248 if [ "$ORANGE_DEV" != "" ]; then
249 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
250 fi
251
252 # run local firewall configuration, if present
253 if [ -x /etc/sysconfig/firewall.local ]; then
254 /etc/sysconfig/firewall.local start
255 fi
256
257 # last rule in input and forward chain is for logging.
258 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "INPUT "
259 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "OUTPUT "
260 ;;
261 startovpn)
262 # run openvpn
263 /usr/local/bin/openvpnctrl --create-chains-and-rules
264 ;;
265 stop)
266 iptables_init
267 # Accept everyting connected
268 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
269
270 # localhost and ethernet.
271 /sbin/iptables -A INPUT -i lo -j ACCEPT
272 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT
273
274 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
275 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
276 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
277 fi
278 if [ "$PROTOCOL" == "RFC1483" -a "$METHOD" == "DHCP" ]; then
279 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
280 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
281 fi
282
283 # run local firewall configuration, if present
284 if [ -x /etc/sysconfig/firewall.local ]; then
285 /etc/sysconfig/firewall.local stop
286 fi
287
288 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "INPUT "
289 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "OUTPUT "
290 ;;
291 stopovpn)
292 # stop openvpn
293 /usr/local/bin/openvpnctrl --delete-chains-and-rules
294 ;;
295 reload)
296 iptables_red
297
298 # run local firewall configuration, if present
299 if [ -x /etc/sysconfig/firewall.local ]; then
300 /etc/sysconfig/firewall.local reload
301 fi
302 ;;
303 restart)
304 $0 stop
305 $0 start
306 ;;
307 *)
308 echo "Usage: $0 {start|stop|reload|restart}"
309 exit 1
310 ;;
311 esac
312
313 exit 0