16e98a5d4bb4b73f95e3bf1a38d9a6140eba9451
[people/teissler/ipfire-2.x.git] / config / firewall / firewall-policy
1 #!/bin/sh
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
23 eval $(/usr/local/bin/readhash /var/ipfire/firewall/settings)
24 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
25
26 function iptables() {
27 /sbin/iptables --wait "$@"
28 }
29
30 iptables -F POLICYFWD
31 iptables -F POLICYOUT
32 iptables -F POLICYIN
33
34 if [ -f "/var/ipfire/red/iface" ]; then
35 IFACE="$(</var/ipfire/red/iface)"
36 fi
37
38 # Figure out what devices are configured.
39 HAVE_BLUE="false"
40 HAVE_ORANGE="false"
41
42 case "${CONFIG_TYPE}" in
43 2)
44 HAVE_ORANGE="true"
45 ;;
46 3)
47 HAVE_BLUE="true"
48 ;;
49 4)
50 HAVE_BLUE="true"
51 HAVE_ORANGE="true"
52 ;;
53 esac
54
55 HAVE_IPSEC="true"
56 HAVE_OPENVPN="true"
57
58 # INPUT
59
60 # IPsec INPUT
61 case "${HAVE_IPSEC},${POLICY}" in
62 true,MODE1) ;;
63 true,*)
64 iptables -A POLICYIN -m policy --pol ipsec --dir in -j ACCEPT
65 ;;
66 esac
67
68 # OpenVPN INPUT
69 # Allow direct access to the internal IP addresses of the firewall
70 # from remote subnets if forward policy is allowed.
71 case "${HAVE_OPENVPN},${POLICY}" in
72 true,MODE1) ;;
73 true,*)
74 iptables -A POLICYIN -i tun+ -j ACCEPT
75 ;;
76 esac
77
78 case "${FWPOLICY2}" in
79 REJECT)
80 if [ "${DROPINPUT}" = "on" ]; then
81 iptables -A POLICYIN -m limit --limit 10/minute -j LOG --log-prefix "REJECT_INPUT "
82 fi
83 iptables -A POLICYIN -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_INPUT"
84 ;;
85 *) # DROP
86 if [ "${DROPINPUT}" = "on" ]; then
87 iptables -A POLICYIN -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
88 fi
89 iptables -A POLICYIN -j DROP -m comment --comment "DROP_INPUT"
90 ;;
91 esac
92
93 # FORWARD
94 case "${POLICY}" in
95 MODE1)
96 case "${FWPOLICY}" in
97 REJECT)
98 if [ "${DROPFORWARD}" = "on" ]; then
99 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "REJECT_FORWARD "
100 fi
101 iptables -A POLICYFWD -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_FORWARD"
102 ;;
103 *) # DROP
104 if [ "${DROPFORWARD}" = "on" ]; then
105 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD "
106 fi
107 iptables -A POLICYFWD -j DROP -m comment --comment "DROP_FORWARD"
108 ;;
109 esac
110 ;;
111
112 *)
113
114 # Access from GREEN is granted to everywhere
115 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
116 # internet via green
117 # don't check source IP/NET if IFACE is GREEN
118 iptables -A POLICYFWD -i "${GREEN_DEV}" -j ACCEPT
119 else
120 iptables -A POLICYFWD -i "${GREEN_DEV}" -s "${GREEN_NETADDRESS}/${GREEN_NETMASK}" -j ACCEPT
121 fi
122
123 # Grant access for IPsec VPN connections
124 iptables -A POLICYFWD -m policy --pol ipsec --dir in -j ACCEPT
125
126 # Grant access for OpenVPN connections
127 iptables -A POLICYFWD -i tun+ -j ACCEPT
128
129 if [ -n "${IFACE}" ]; then
130 if [ "${HAVE_BLUE}" = "true" ] && [ -n "${BLUE_DEV}" ]; then
131 iptables -A POLICYFWD -i "${BLUE_DEV}" -s "${BLUE_NETADDRESS}/${BLUE_NETMASK}" -o "${IFACE}" -j ACCEPT
132 fi
133
134 if [ "${HAVE_ORANGE}" = "true" ] && [ -n "${ORANGE_DEV}" ]; then
135 iptables -A POLICYFWD -i "${ORANGE_DEV}" -s "${ORANGE_NETADDRESS}/${ORANGE_NETMASK}" -o "${IFACE}" -j ACCEPT
136 fi
137 fi
138
139 if [ "${DROPFORWARD}" = "on" ]; then
140 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD "
141 fi
142 iptables -A POLICYFWD -m comment --comment "DROP_FORWARD" -j DROP
143 ;;
144 esac
145
146 # OUTGOING
147 case "${POLICY1}" in
148 MODE1)
149 case "${FWPOLICY1}" in
150 REJECT)
151 if [ "${DROPOUTGOING}" = "on" ]; then
152 iptables -A POLICYOUT -m limit --limit 10/minute -j LOG --log-prefix "REJECT_OUTPUT "
153 fi
154 iptables -A POLICYOUT -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_OUTPUT"
155 ;;
156 *) # DROP
157 if [ "${DROPOUTGOING}" == "on" ]; then
158 iptables -A POLICYOUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
159 fi
160 iptables -A POLICYOUT -j DROP -m comment --comment "DROP_OUTPUT"
161 ;;
162 esac
163 ;;
164 *)
165 iptables -A POLICYOUT -j ACCEPT
166 iptables -A POLICYOUT -m comment --comment "DROP_OUTPUT" -j DROP
167 ;;
168 esac
169
170 exit 0