Fix outgoing firewall problems with MAC rules.
[ipfire-2.x.git] / config / outgoingfw / outgoingfw.pl
CommitLineData
dba3aa2b 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
e4e42008 5# Copyright (C) 2007-2011 IPFire Team #
70df8302
MT
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
ebb9187c
MT
22
23use strict;
24# enable only the following on debugging purpose
25#use warnings;
26
27require '/var/ipfire/general-functions.pl';
e4e42008 28require "${General::swroot}/lang.pl";
ebb9187c
MT
29
30my %outfwsettings = ();
31my %checked = ();
32my %selected= () ;
33my %netsettings = ();
34my $errormessage = "";
35my $configentry = "";
36my @configs = ();
37my @configline = ();
38my $p2pentry = "";
39my @p2ps = ();
40my @p2pline = ();
ebb9187c 41my $CMD = "";
b4f8d26c
MT
42my $P2PSTRING = "";
43
ebb9187c
MT
44my $DEBUG = 0;
45
46my $configfile = "/var/ipfire/outgoing/rules";
47my $p2pfile = "/var/ipfire/outgoing/p2protocols";
48
ebb9187c
MT
49### Values that have to be initialized
50$outfwsettings{'ACTION'} = '';
51$outfwsettings{'VALID'} = 'yes';
52$outfwsettings{'EDIT'} = 'no';
53$outfwsettings{'NAME'} = '';
54$outfwsettings{'SNET'} = '';
55$outfwsettings{'SIP'} = '';
56$outfwsettings{'SPORT'} = '';
57$outfwsettings{'SMAC'} = '';
58$outfwsettings{'DIP'} = '';
59$outfwsettings{'DPORT'} = '';
60$outfwsettings{'PROT'} = '';
61$outfwsettings{'STATE'} = '';
62$outfwsettings{'DISPLAY_DIP'} = '';
63$outfwsettings{'DISPLAY_DPORT'} = '';
64$outfwsettings{'DISPLAY_SMAC'} = '';
65$outfwsettings{'DISPLAY_SIP'} = '';
66$outfwsettings{'POLICY'} = 'MODE0';
39008af7 67
fdeaa057 68my @SOURCE = "";
ebb9187c
MT
69my $SOURCE = "";
70my $DESTINATION = "";
fdeaa057 71my @PROTO = "";
ebb9187c
MT
72my $PROTO = "";
73my $DPORT = "";
74my $DEV = "";
75my $MAC = "";
ebb9187c 76my $DO = "";
39008af7 77my $DAY = "";
ebb9187c
MT
78
79# read files
80&General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
81&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
82
ed828642 83$netsettings{'RED_DEV'}=`cat /var/ipfire/red/iface`;
39008af7 84$netsettings{'RED_IP'}=`cat /var/ipfire/red/local-ipaddress`;
ed828642 85
ebb9187c
MT
86open( FILE, "< $configfile" ) or die "Unable to read $configfile";
87@configs = <FILE>;
88close FILE;
89
b4f8d26c 90if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
ebb9187c 91 $outfwsettings{'STATE'} = "ALLOW";
78a14abf 92 $DO = "RETURN";
ebb9187c
MT
93} elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
94 $outfwsettings{'STATE'} = "DENY";
d9716b06 95 $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW '";
ebb9187c
MT
96}
97
98### Initialize IPTables
99system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
100system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
101system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
102
d9716b06
CS
103system("/sbin/iptables --flush OUTGOINGFWMAC >/dev/null 2>&1");
104system("/sbin/iptables --delete-chain OUTGOINGFWMAC >/dev/null 2>&1");
105system("/sbin/iptables -N OUTGOINGFWMAC >/dev/null 2>&1");
106
b4f8d26c
MT
107if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
108 exit 0
109}
110
111if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
78a14abf 112 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j RETURN";
d9716b06 113 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
78a14abf 114 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m state --state ESTABLISHED,RELATED -j RETURN";
b4f8d26c 115 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
78a14abf 116 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j RETURN";
b4f8d26c 117 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
78a14abf 118 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -p icmp -j RETURN";
d9716b06 119 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
b4f8d26c
MT
120}
121
ebb9187c
MT
122foreach $configentry (sort @configs)
123{
fdeaa057 124 @SOURCE = "";
ebb9187c
MT
125 $DESTINATION = "";
126 $PROTO = "";
127 $DPORT = "";
128 $DEV = "";
129 $MAC = "";
130 @configline = split( /\;/, $configentry );
fdeaa057 131
ebb9187c
MT
132 if ($outfwsettings{'STATE'} eq $configline[0]) {
133 if ($configline[2] eq 'green') {
fdeaa057 134 @SOURCE = ("$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}");
ebb9187c 135 $DEV = $netsettings{'GREEN_DEV'};
39008af7 136 } elsif ($configline[2] eq 'red') {
fdeaa057 137 @SOURCE = ("$netsettings{'RED_IP'}");
39008af7 138 $DEV = "";
ebb9187c 139 } elsif ($configline[2] eq 'blue') {
fdeaa057 140 @SOURCE = ("$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}");
ebb9187c
MT
141 $DEV = $netsettings{'BLUE_DEV'};
142 } elsif ($configline[2] eq 'orange') {
fdeaa057 143 @SOURCE = ("$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}");
ebb9187c 144 $DEV = $netsettings{'ORANGE_DEV'};
fdeaa057
CS
145 } elsif ($configline[2] eq 'ipsec') {
146 @SOURCE = "";
147 $DEV = "ipsec+";
148 } elsif ($configline[2] eq 'ovpn') {
149 @SOURCE = "";
150 $DEV = "tun+";
ebb9187c 151 } elsif ($configline[2] eq 'ip') {
fdeaa057
CS
152 @SOURCE = ("$configline[5]");
153 $DEV = "";
d9716b06
CS
154 } elsif ($configline[2] eq 'mac') {
155 @SOURCE = ("$configline[6]");
156 $DEV = "";
70248550
CS
157 } elsif ($configline[2] eq 'all') {
158 @SOURCE = ("0/0");
159 $DEV = "";
fdeaa057 160 } else {
d9716b06 161 if ( -e "/var/ipfire/outgoing/groups/ipgroups/$configline[2]" ) {
fdeaa057 162 @SOURCE = `cat /var/ipfire/outgoing/groups/ipgroups/$configline[2]`;
d9716b06
CS
163 } elsif ( -e "/var/ipfire/outgoing/groups/macgroups/$configline[2]" ) {
164 @SOURCE = `cat /var/ipfire/outgoing/groups/macgroups/$configline[2]`;
bd4ea3c2 165 $configline[2] = "mac";
fdeaa057 166 }
ebb9187c
MT
167 $DEV = "";
168 }
169
170 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
d9716b06 171
b4f8d26c 172 if ($configline[3] eq 'tcp') {
fdeaa057 173 @PROTO = ("tcp");
b4f8d26c 174 } elsif ($configline[3] eq 'udp') {
fdeaa057 175 @PROTO = ("udp");
c791bb2a 176 } elsif ($configline[3] eq 'esp') {
fdeaa057 177 @PROTO = ("esp");
c791bb2a 178 } elsif ($configline[3] eq 'gre') {
fdeaa057 179 @PROTO = ("gre");
b4f8d26c 180 } else {
fdeaa057 181 @PROTO = ("tcp","udp");
b4f8d26c 182 }
4cb74dce 183
ab74c839 184 my $macrule = 0;
fdeaa057
CS
185 foreach $PROTO (@PROTO){
186 foreach $SOURCE (@SOURCE) {
187 $SOURCE =~ s/\s//gi;
188
68dd6726 189 if ( $SOURCE eq "" || $configline[1] eq "" ){next;}
fdeaa057 190
70248550 191 if ( ( $configline[6] ne "" || $configline[2] eq 'mac' ) && $configline[2] ne 'all'){
d9716b06 192 $SOURCE =~ s/[^a-zA-Z0-9]/:/gi;
ab74c839
MT
193 $CMD = "-m mac --mac-source $SOURCE -d $DESTINATION -p $PROTO";
194 $macrule = 1;
d9716b06 195 } else {
ab74c839 196 $CMD = "-s $SOURCE -d $DESTINATION -p $PROTO";
d9716b06 197 }
fdeaa057
CS
198
199 if ($configline[8] && ( $configline[3] ne 'esp' || $configline[3] ne 'gre') ) {
200 $DPORT = "$configline[8]";
201 $CMD = "$CMD -m multiport --destination-port $DPORT";
202 }
203
204 if ($DEV) {
205 $CMD = "$CMD -i $DEV";
206 }
207
fdeaa057 208 if ($configline[17] && $configline[18]) {
d89eab6d 209 $DAY = "";
fdeaa057
CS
210 if ($configline[10]){$DAY = "Mon,"}
211 if ($configline[11]){$DAY .= "Tue,"}
212 if ($configline[12]){$DAY .= "Wed,"}
213 if ($configline[13]){$DAY .= "Thu,"}
214 if ($configline[14]){$DAY .= "Fri,"}
215 if ($configline[15]){$DAY .= "Sat,"}
216 if ($configline[16]){$DAY .= "Sun"}
217 $CMD = "$CMD -m time --timestart $configline[17] --timestop $configline[18] --weekdays $DAY";
218 }
219
220 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
221
e4e42008 222 if ( $configline[9] eq $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE1' ) {
ab74c839 223 applyrule("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '", $macrule);
e4e42008 224 } elsif ( $configline[9] eq $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE2' ) {
ab74c839 225 applyrule("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '", $macrule);
fdeaa057
CS
226 }
227
ab74c839 228 applyrule("$CMD -j $DO", $macrule);
4cb74dce 229 }
fdeaa057 230 }
b4f8d26c
MT
231 }
232}
ebb9187c 233
b4f8d26c
MT
234### Do the P2P-Stuff here
235open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
236@p2ps = <FILE>;
237close FILE;
ebb9187c 238
ab74c839 239$CMD = "-m ipp2p";
ebb9187c 240
ab74c839 241foreach $p2pentry (sort @p2ps) {
b4f8d26c
MT
242 @p2pline = split( /\;/, $p2pentry );
243 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
244 $DO = "DROP";
245 if ("$p2pline[2]" eq "off") {
246 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
247 }
248 } else {
78a14abf 249 $DO = "RETURN";
b4f8d26c
MT
250 if ("$p2pline[2]" eq "on") {
251 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
ebb9187c 252 }
b4f8d26c
MT
253 }
254}
255if ($P2PSTRING) {
ab74c839 256 applyrule("$CMD $P2PSTRING -j $DO", 0);
b4f8d26c 257}
ebb9187c 258
b4f8d26c 259if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
ab74c839
MT
260 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
261 applyrule("-o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '", 0);
262 }
52802b5f 263
ab74c839
MT
264 applyrule("-o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW '", 0);
265}
266
267sub applyrule($$) {
268 my $cmd = shift;
269 my $macrule = shift;
270
271 system("/sbin/iptables -A OUTGOINGFWMAC $cmd");
272 if ($macrule == 0) {
273 system("/sbin/iptables -A OUTGOINGFW $cmd");
ebb9187c 274 }
78a14abf 275}