]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/rules.pl
d2991b897c6f64a4447266991358ee9ff616bb66
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
24 require '/var/ipfire/general-functions.pl';
25 require "${General::swroot}/lang.pl";
26 require "/usr/lib/firewall/firewall-lib.pl";
28 # Set to one to enable debugging mode.
31 my $IPTABLES = "iptables --wait";
34 my $CHAIN_INPUT = "INPUTFW";
35 my $CHAIN_FORWARD = "FORWARDFW";
36 my $CHAIN_OUTPUT = "OUTPUTFW";
37 my $CHAIN = $CHAIN_FORWARD;
38 my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
39 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
40 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
42 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
43 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
45 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
48 my %defaultNetworks=();
52 my %configoutgoingfw=();
57 my $configfwdfw = "${General::swroot}/firewall/config";
58 my $configinput = "${General::swroot}/firewall/input";
59 my $configoutgoing = "${General::swroot}/firewall/outgoing";
60 my $p2pfile = "${General::swroot}/firewall/p2protocols";
61 my $configgrp = "${General::swroot}/fwhosts/customgroups";
62 my $netsettings = "${General::swroot}/ethernet/settings";
64 &General
::readhash
("${General::swroot}/firewall/settings", \
%fwdfwsettings);
65 &General
::readhash
("$netsettings", \
%defaultNetworks);
66 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
67 &General
::readhasharray
($configinput, \
%configinputfw);
68 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
69 &General
::readhasharray
($configgrp, \
%customgrp);
70 &General
::get_aliases
(\
%aliases);
79 # Reload firewall rules.
82 # Load P2P block rules.
85 # Reload firewall policy.
86 run
("/usr/sbin/firewall-policy");
90 # Executes or prints the given shell command.
103 print STDERR
"$message\n";
113 printf(" %2d: %s", $i++, $_);
119 run
("$IPTABLES -F FORWARDFW");
120 run
("$IPTABLES -F INPUTFW");
121 run
("$IPTABLES -F OUTGOINGFW");
122 run
("$IPTABLES -t nat -F NAT_DESTINATION");
123 run
("$IPTABLES -t nat -F NAT_SOURCE");
127 if (! -z
"${General::swroot}/firewall/config"){
128 &buildrules
(\
%configfwdfw);
130 if (! -z
"${General::swroot}/firewall/input"){
131 &buildrules
(\
%configinputfw);
133 if (! -z
"${General::swroot}/firewall/outgoing"){
134 &buildrules
(\
%configoutgoingfw);
141 foreach my $key (sort {$a <=> $b} keys %$hash) {
142 # Skip disabled rules.
143 next unless ($$hash{$key}[2] eq 'ON');
146 print_rule
($$hash{$key});
149 # Check if the target is valid.
150 my $target = $$hash{$key}[0];
151 if (!$target ~~ @VALID_TARGETS) {
152 print_error
("Invalid target '$target' for rule $key");
156 # Check if the chain is valid.
157 my $chain = $$hash{$key}[1];
158 if (!$chain ~~ @VALID_CHAINS) {
159 print_error
("Invalid chain '$chain' in rule $key");
163 # Collect all sources.
164 my @sources = &get_addresses
($hash, $key, "src");
166 # Collect all destinations.
167 my @destinations = &get_addresses
($hash, $key, "tgt");
169 my $time_constraints = "";
171 # Check if logging should be enabled.
172 my $LOG = ($$hash{$key}[17] eq 'ON');
174 # Check if NAT is enabled and initialize variables, that we use for that.
175 my $NAT = ($$hash{$key}[28] eq 'ON');
178 $NAT_MODE = uc($$hash{$key}[31]);
181 # Set up time constraints.
182 my @time_options = ();
183 if ($$hash{$key}[18] eq 'ON') {
184 push(@time_options, ("-m", "time"));
186 # Select all days of the week this match is active.
188 if ($$hash{$key}[19] ne '') {
189 push (@weekdays, "Mon");
191 if ($$hash{$key}[20] ne '') {
192 push (@weekdays, "Tue");
194 if ($$hash{$key}[21] ne '') {
195 push (@weekdays, "Wed");
197 if ($$hash{$key}[22] ne '') {
198 push (@weekdays, "Thu");
200 if ($$hash{$key}[23] ne '') {
201 push (@weekdays, "Fri");
203 if ($$hash{$key}[24] ne '') {
204 push (@weekdays, "Sat");
206 if ($$hash{$key}[25] ne '') {
207 push (@weekdays, "Sun");
210 push(@time_options, ("--weekdays", join(",", @weekdays)));
213 # Convert start time.
214 my $time_start = &format_time
($$hash{$key}[26]);
216 push(@time_options, ("--timestart", $time_start));
220 my $time_stop = &format_time
($$hash{$key}[27]);
222 push(@time_options, ("--timestop", $time_stop));
226 # Check which protocols are used in this rule and so that we can
227 # later group rules by protocols.
228 my @protocols = &get_protocols
($hash, $key);
230 print_error
("Invalid protocol configuration for rule $key");
234 foreach my $protocol (@protocols) {
235 # Check if the given protocol is supported.
236 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
237 print_error
("Protocol $protocol is not supported (rule $key)");
241 # Prepare protocol options (like ICMP types, ports, etc...).
242 my @protocol_options = &get_protocol_options
($hash, $key, $protocol);
244 # Check if this protocol knows ports.
245 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
247 foreach my $source (@sources) {
248 foreach my $destination (@destinations) {
249 # Skip invalid rules.
250 next if (!$source || !$destination || ($destination eq "none"));
252 # Array with iptables arguments.
256 if ($protocol ne "all") {
257 push(@options, ("-p", $protocol));
258 push(@options, @protocol_options);
262 if ($source =~ /mac/) {
263 push(@options, $source);
265 push(@options, ("-s", $source));
268 # Append destination for non-DNAT rules.
269 unless ($NAT && ($NAT_MODE eq "DNAT")) {
270 push(@options, ("-d", $destination));
273 # Add time constraint options.
274 push(@options, @time_options);
278 my $nat_address = &get_nat_address
($$hash{$key}[29]);
280 # Skip NAT rules if the NAT address is unknown
281 # (i.e. no internet connection has been established, yet).
282 next unless ($nat_address);
285 if ($NAT_MODE eq "DNAT") {
286 my ($dnat_address, $dnat_mask) = split("/", $destination);
288 my @nat_options = @options;
289 push(@nat_options, ("-d", $nat_address));
290 push(@options, ("-d", $dnat_address));
292 if ($protocol_has_ports) {
293 my $dnat_port = &get_dnat_target_port
($hash, $key);
296 $dnat_address .= ":$dnat_port";
298 # Replace --dport with the translated one.
299 my @new_options = ();
301 foreach my $option (@options) {
302 next if ($skip_count-- > 0);
304 if ($option eq "--dport") {
305 push(@new_options, ("--dport", $dnat_port));
310 push(@new_options, $option);
312 @options = @new_options;
317 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j LOG --log-prefix 'DNAT'");
319 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
322 } elsif ($NAT_MODE eq "SNAT") {
324 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @options -j LOG --log-prefix 'SNAT'");
326 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @options -j SNAT --to-source $nat_address");
330 # Insert firewall rule.
332 run
("$IPTABLES -A $chain @options -j LOG");
334 run
("$IPTABLES -A $chain @options -j $target");
341 sub get_external_interface
() {
342 open(IFACE
, "/var/ipfire/red/iface") or return "";
349 sub get_external_address
() {
350 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
351 my $address = <ADDR
>;
360 foreach my $alias (sort keys %aliases) {
362 return $aliases{$alias};
367 sub get_nat_address
{
370 # Any static address of any zone.
371 if ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
372 return $defaultNetworks{$zone . "_ADDRESS"};
374 } elsif ($zone eq "Default IP") {
375 return &get_external_address
();
378 return &get_alias
($zone);
381 print_error
("Could not find NAT address");
384 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
388 # Convert the given time into minutes.
389 my $minutes = &time_convert_to_minutes
($val);
391 # Move the timestamp into UTC.
392 $minutes += &time_utc_offset
();
394 # Make sure $minutes is between 00:00 and 23:59.
399 if ($minutes > 1440) {
404 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
407 # Calculates the offsets in minutes from the local timezone to UTC.
408 sub time_utc_offset
{
409 my @localtime = localtime(time);
410 my @gmtime = gmtime(time);
412 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
415 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
416 sub time_convert_to_minutes
{
417 my ($hrs, $min) = split(":", shift);
419 return ($hrs * 60) + $min;
425 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
428 my $CMD = "-m ipp2p";
429 foreach my $p2pentry (sort @p2ps) {
430 my @p2pline = split( /\;/, $p2pentry );
431 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
433 if ("$p2pline[2]" eq "on") {
434 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
438 if ("$p2pline[2]" eq "off") {
439 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
445 run
("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
459 if ($type eq "src") {
460 $addr_type = $$hash{$key}[3];
461 $value = $$hash{$key}[4];
463 } elsif ($type eq "tgt") {
464 $addr_type = $$hash{$key}[5];
465 $value = $$hash{$key}[6];
468 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
469 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
470 if ($customgrp{$grp}[0] eq $value) {
471 my @address = &get_address
($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
474 push(@addresses, @address);
479 my @address = &get_address
($addr_type, $value, $type);
482 push(@addresses, @address);
496 # If the user manually typed an address, we just check if it is a MAC
497 # address. Otherwise, we assume that it is an IP address.
498 if ($key ~~ ["src_addr", "tgt_addr"]) {
499 if (&General
::validmac
($value)) {
500 push(@ret, "-m mac --mac-source $value");
505 # If a default network interface (GREEN, BLUE, etc.) is selected, we
506 # try to get the corresponding address of the network.
507 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
508 my $external_interface = &get_external_interface
();
510 my $network_address = &fwlib
::get_std_net_ip
($value, $external_interface);
511 if ($network_address) {
512 push(@ret, $network_address);
516 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
517 my $network_address = &fwlib
::get_net_ip
($value);
518 if ($network_address) {
519 push(@ret, $network_address);
523 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
524 my $host_address = &fwlib
::get_host_ip
($value, $type);
526 push(@ret, $host_address);
530 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
531 my $network_address = &fwlib
::get_ovpn_net_ip
($value, 1);
532 if ($network_address) {
533 push(@ret, $network_address);
537 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
538 my $host_address = &fwlib
::get_ovpn_host_ip
($value, 33);
540 push(@ret, $host_address);
544 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
545 my $network_address = &fwlib
::get_ovpn_n2n_ip
($value, 11);
546 if ($network_address) {
547 push(@ret, $network_address);
551 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
552 my $network_address = &fwlib
::get_ipsec_net_ip
($value, 11);
553 if ($network_address) {
554 push(@ret, $network_address);
557 # The firewall's own IP addresses.
558 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
560 if ($value eq "ALL") {
564 } elsif ($value eq "GREEN") {
565 push(@ret, $defaultNetworks{"GREEN_ADDRESS"});
568 } elsif ($value eq "BLUE") {
569 push(@ret, $defaultNetworks{"BLUE_ADDRESS"});
572 } elsif ($value eq "ORANGE") {
573 push(@ret, $defaultNetworks{"ORANGE_ADDRESS"});
576 } elsif ($value ~~ ["RED", "RED1"]) {
577 my $address = &get_external_address
();
579 push(@ret, $address);
584 my %alias = &get_alias
($value);
586 push(@ret, $alias{"IPT"});
590 # If nothing was selected, we assume "any".
602 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
603 my $uses_services = ($$hash{$key}[11] eq "ON");
607 # Rules which don't have source ports or services (like ICMP, ESP, ...).
608 if (!$uses_source_ports && !$uses_services) {
609 push(@protocols, $$hash{$key}[8]);
611 # Rules which either use ports or services.
612 } elsif ($uses_source_ports || $uses_services) {
613 # Check if service group or service
614 if ($$hash{$key}[14] eq 'cust_srv') {
615 push(@protocols, &fwlib
::get_srv_prot
($$hash{$key}[15]));
617 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
618 my $protos = &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
619 push(@protocols, split(",", $protos));
622 # Fetch the protocol for this rule.
623 my $protocol = lc($$hash{$key}[8]);
625 # Fetch source and destination ports for this rule.
626 my $source_ports = $$hash{$key}[10];
627 my $destination_ports = $$hash{$key}[15];
629 # Check if ports are set for protocols which do not support ports.
630 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
631 print_error
("$protocol does not support ports");
635 push(@protocols, $protocol);
639 # Remove all empty elements
640 @protocols = map { $_ ?
$_ : () } @protocols;
642 # If no protocol has been defined, we assume "all".
644 push(@protocols, "all");
647 # Make all protocol names lowercase.
648 @protocols = map { lc } @protocols;
653 sub get_protocol_options
{
656 my $protocol = shift;
659 # Process source ports.
660 my $use_src_ports = ($$hash{$key}[7] eq "ON");
661 my $src_ports = $$hash{$key}[10];
663 if ($use_src_ports && $src_ports) {
664 push(@options, &format_ports
($src_ports, "src"));
667 # Process destination ports.
668 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
669 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
671 if ($use_dst_ports) {
672 my $dst_ports_mode = $$hash{$key}[14];
673 my $dst_ports = $$hash{$key}[15];
674 if ($use_dnat && $$hash{$key}[30]) {
675 $dst_ports = $$hash{$key}[30];
678 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
679 push(@options, &format_ports
($dst_ports, "dst"));
681 } elsif ($dst_ports_mode eq "cust_srv") {
682 if ($protocol eq "ICMP") {
683 push(@options, ("--icmp-type", &fwlib
::get_srv_port
($dst_ports, 3, "ICMP")));
685 $dst_ports = &fwlib
::get_srv_port
($dst_ports, 1, uc($protocol));
686 push(@options, &format_ports
($dst_ports, "dst"));
689 } elsif ($dst_ports_mode eq "cust_srvgrp") {
690 push(@options, &fwlib
::get_srvgrp_port
($dst_ports, uc($protocol)));
694 # Check if a single ICMP type is selected.
695 if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") {
696 my $icmp_type = $$hash{$key}[9];
698 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
699 push(@options, ("--icmp-type", $icmp_type));
711 if ($type eq "src") {
713 } elsif ($type eq "dst") {
719 if ($ports =~ /\|/) {
721 push(@options, ("-m", "multiport"));
724 push(@options, ($arg, $ports));
729 sub get_dnat_target_port
{
733 if ($$hash{$key}[14] eq "TGT_PORT") {
734 return $$hash{$key}[15];