]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/firewall/firewall-policy
Merge remote-tracking branch 'origin/master' into next
[people/pmueller/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 # Allow access from GREEN
61 iptables -A POLICYIN -i "${GREEN_DEV}" -j ACCEPT
62
63 # Allow access from BLUE
64 if [ "${HAVE_BLUE}" = "true" ] && [ -n "${BLUE_DEV}" ]; then
65 iptables -A POLICYIN -i "${BLUE_DEV}" -j ACCEPT
66 fi
67
68 # IPsec INPUT
69 case "${HAVE_IPSEC},${POLICY}" in
70 true,MODE1) ;;
71 true,*)
72 iptables -A POLICYIN -m policy --pol ipsec --dir in -j ACCEPT
73 ;;
74 esac
75
76 # OpenVPN INPUT
77 # Allow direct access to the internal IP addresses of the firewall
78 # from remote subnets if forward policy is allowed.
79 case "${HAVE_OPENVPN},${POLICY}" in
80 true,MODE1) ;;
81 true,*)
82 iptables -A POLICYIN -i tun+ -j ACCEPT
83 ;;
84 esac
85
86 case "${FWPOLICY2}" in
87 REJECT)
88 if [ "${DROPINPUT}" = "on" ]; then
89 iptables -A POLICYIN -m limit --limit 10/minute -j LOG --log-prefix "REJECT_INPUT "
90 fi
91 iptables -A POLICYIN -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_INPUT"
92 ;;
93 *) # DROP
94 if [ "${DROPINPUT}" = "on" ]; then
95 iptables -A POLICYIN -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
96 fi
97 iptables -A POLICYIN -j DROP -m comment --comment "DROP_INPUT"
98 ;;
99 esac
100
101 # FORWARD
102 case "${POLICY}" in
103 MODE1)
104 case "${FWPOLICY}" in
105 REJECT)
106 if [ "${DROPFORWARD}" = "on" ]; then
107 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "REJECT_FORWARD "
108 fi
109 iptables -A POLICYFWD -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_FORWARD"
110 ;;
111 *) # DROP
112 if [ "${DROPFORWARD}" = "on" ]; then
113 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD "
114 fi
115 iptables -A POLICYFWD -j DROP -m comment --comment "DROP_FORWARD"
116 ;;
117 esac
118 ;;
119
120 *)
121 # Access from GREEN is granted to everywhere
122 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
123 # internet via green
124 # don't check source IP/NET if IFACE is GREEN
125 iptables -A POLICYFWD -i "${GREEN_DEV}" -j ACCEPT
126 else
127 iptables -A POLICYFWD -i "${GREEN_DEV}" -s "${GREEN_NETADDRESS}/${GREEN_NETMASK}" -j ACCEPT
128 fi
129
130 # Grant access for IPsec VPN connections
131 iptables -A POLICYFWD -m policy --pol ipsec --dir in -j ACCEPT
132
133 # Grant access for OpenVPN connections
134 iptables -A POLICYFWD -i tun+ -j ACCEPT
135
136 if [ -n "${IFACE}" ]; then
137 if [ "${HAVE_BLUE}" = "true" ] && [ -n "${BLUE_DEV}" ]; then
138 iptables -A POLICYFWD -i "${BLUE_DEV}" -s "${BLUE_NETADDRESS}/${BLUE_NETMASK}" -o "${IFACE}" -j ACCEPT
139 fi
140
141 if [ "${HAVE_ORANGE}" = "true" ] && [ -n "${ORANGE_DEV}" ]; then
142 iptables -A POLICYFWD -i "${ORANGE_DEV}" -s "${ORANGE_NETADDRESS}/${ORANGE_NETMASK}" -o "${IFACE}" -j ACCEPT
143 fi
144 fi
145
146 if [ "${DROPFORWARD}" = "on" ]; then
147 iptables -A POLICYFWD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD "
148 fi
149 iptables -A POLICYFWD -m comment --comment "DROP_FORWARD" -j DROP
150 ;;
151 esac
152
153 # OUTGOING
154 case "${POLICY1}" in
155 MODE1)
156 case "${FWPOLICY1}" in
157 REJECT)
158 if [ "${DROPOUTGOING}" = "on" ]; then
159 iptables -A POLICYOUT -m limit --limit 10/minute -j LOG --log-prefix "REJECT_OUTPUT "
160 fi
161 iptables -A POLICYOUT -j REJECT --reject-with icmp-host-unreachable -m comment --comment "DROP_OUTPUT"
162 ;;
163 *) # DROP
164 if [ "${DROPOUTGOING}" == "on" ]; then
165 iptables -A POLICYOUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
166 fi
167 iptables -A POLICYOUT -j DROP -m comment --comment "DROP_OUTPUT"
168 ;;
169 esac
170 ;;
171 *)
172 iptables -A POLICYOUT -j ACCEPT
173 iptables -A POLICYOUT -m comment --comment "DROP_OUTPUT" -j DROP
174 ;;
175 esac
176
177 exit 0