]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/rules.pl
2ce31c9a256d6ec3a0fc63da87cc23221a10cf37
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 $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
41 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
43 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
44 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
46 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
49 my %defaultNetworks=();
53 my %configoutgoingfw=();
58 my $configfwdfw = "${General::swroot}/firewall/config";
59 my $configinput = "${General::swroot}/firewall/input";
60 my $configoutgoing = "${General::swroot}/firewall/outgoing";
61 my $p2pfile = "${General::swroot}/firewall/p2protocols";
62 my $configgrp = "${General::swroot}/fwhosts/customgroups";
63 my $netsettings = "${General::swroot}/ethernet/settings";
65 &General
::readhash
("${General::swroot}/firewall/settings", \
%fwdfwsettings);
66 &General
::readhash
("$netsettings", \
%defaultNetworks);
67 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
68 &General
::readhasharray
($configinput, \
%configinputfw);
69 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
70 &General
::readhasharray
($configgrp, \
%customgrp);
71 &General
::get_aliases
(\
%aliases);
80 # Reload firewall rules.
83 # Load P2P block rules.
86 # Reload firewall policy.
87 run
("/usr/sbin/firewall-policy");
91 # Executes or prints the given shell command.
100 print_error
("ERROR: $command");
108 print STDERR
"$message\n";
118 printf(" %2d: %s", $i++, $_);
124 run
("$IPTABLES -F FORWARDFW");
125 run
("$IPTABLES -F INPUTFW");
126 run
("$IPTABLES -F OUTGOINGFW");
127 run
("$IPTABLES -t nat -F NAT_DESTINATION");
128 run
("$IPTABLES -t nat -F NAT_SOURCE");
129 run
("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
133 if (! -z
"${General::swroot}/firewall/config"){
134 &buildrules
(\
%configfwdfw);
136 if (! -z
"${General::swroot}/firewall/input"){
137 &buildrules
(\
%configinputfw);
139 if (! -z
"${General::swroot}/firewall/outgoing"){
140 &buildrules
(\
%configoutgoingfw);
147 foreach my $key (sort {$a <=> $b} keys %$hash) {
148 # Skip disabled rules.
149 next unless ($$hash{$key}[2] eq 'ON');
152 print_rule
($$hash{$key});
155 # Check if the target is valid.
156 my $target = $$hash{$key}[0];
157 if (!$target ~~ @VALID_TARGETS) {
158 print_error
("Invalid target '$target' for rule $key");
162 # Check if the chain is valid.
163 my $chain = $$hash{$key}[1];
164 if (!$chain ~~ @VALID_CHAINS) {
165 print_error
("Invalid chain '$chain' in rule $key");
169 # Collect all sources.
170 my @sources = &get_addresses
($hash, $key, "src");
172 # Collect all destinations.
173 my @destinations = &get_addresses
($hash, $key, "tgt");
175 my $time_constraints = "";
177 # Check if logging should be enabled.
178 my $LOG = ($$hash{$key}[17] eq 'ON');
180 # Check if NAT is enabled and initialize variables, that we use for that.
181 my $NAT = ($$hash{$key}[28] eq 'ON');
184 $NAT_MODE = uc($$hash{$key}[31]);
187 # Set up time constraints.
188 my @time_options = ();
189 if ($$hash{$key}[18] eq 'ON') {
190 push(@time_options, ("-m", "time"));
192 # Select all days of the week this match is active.
194 if ($$hash{$key}[19] ne '') {
195 push (@weekdays, "Mon");
197 if ($$hash{$key}[20] ne '') {
198 push (@weekdays, "Tue");
200 if ($$hash{$key}[21] ne '') {
201 push (@weekdays, "Wed");
203 if ($$hash{$key}[22] ne '') {
204 push (@weekdays, "Thu");
206 if ($$hash{$key}[23] ne '') {
207 push (@weekdays, "Fri");
209 if ($$hash{$key}[24] ne '') {
210 push (@weekdays, "Sat");
212 if ($$hash{$key}[25] ne '') {
213 push (@weekdays, "Sun");
216 push(@time_options, ("--weekdays", join(",", @weekdays)));
219 # Convert start time.
220 my $time_start = &format_time
($$hash{$key}[26]);
222 push(@time_options, ("--timestart", $time_start));
226 my $time_stop = &format_time
($$hash{$key}[27]);
228 push(@time_options, ("--timestop", $time_stop));
232 # Check which protocols are used in this rule and so that we can
233 # later group rules by protocols.
234 my @protocols = &get_protocols
($hash, $key);
236 print_error
("Invalid protocol configuration for rule $key");
240 foreach my $protocol (@protocols) {
241 # Check if the given protocol is supported.
242 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
243 print_error
("Protocol $protocol is not supported (rule $key)");
247 # Prepare protocol options (like ICMP types, ports, etc...).
248 my @protocol_options = &get_protocol_options
($hash, $key, $protocol);
250 # Check if this protocol knows ports.
251 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
253 foreach my $source (@sources) {
254 foreach my $destination (@destinations) {
255 # Skip invalid rules.
256 next if (!$source || !$destination || ($destination eq "none"));
258 # Array with iptables arguments.
262 if ($protocol ne "all") {
263 push(@options, ("-p", $protocol));
264 push(@options, @protocol_options);
267 # Prepare source options.
268 my @source_options = ();
269 if ($source =~ /mac/) {
270 push(@source_options, $source);
272 push(@source_options, ("-s", $source));
275 # Prepare destination options.
276 my @destination_options = ("-d", $destination);
278 # Add time constraint options.
279 push(@options, @time_options);
283 my $nat_address = &get_nat_address
($$hash{$key}[29]);
285 # Skip NAT rules if the NAT address is unknown
286 # (i.e. no internet connection has been established, yet).
287 next unless ($nat_address);
290 if ($NAT_MODE eq "DNAT") {
291 # Make port-forwardings useable from the internal networks.
292 &add_dnat_mangle_rules
($nat_address, @options);
294 my @nat_options = @options;
295 push(@nat_options, @source_options);
296 push(@nat_options, ("-d", $nat_address));
298 my ($dnat_address, $dnat_mask) = split("/", $destination);
299 @destination_options = ("-d", $dnat_address);
301 if ($protocol_has_ports) {
302 my $dnat_port = &get_dnat_target_port
($hash, $key);
305 $dnat_address .= ":$dnat_port";
307 # Replace --dport with the translated one.
308 my @new_nat_options = ();
310 foreach my $option (@nat_options) {
311 next if ($skip_count-- > 0);
313 if ($option eq "--dport") {
314 push(@new_nat_options, ("--dport", $dnat_port));
319 push(@new_nat_options, $option);
321 @nat_options = @new_nat_options;
326 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j LOG --log-prefix 'DNAT'");
328 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
331 } elsif ($NAT_MODE eq "SNAT") {
332 my @nat_options = @options;
334 push(@nat_options, @source_options);
335 push(@nat_options, @destination_options);
338 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j LOG --log-prefix 'SNAT'");
340 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
344 push(@options, @source_options);
345 push(@options, @destination_options);
347 # Insert firewall rule.
349 run
("$IPTABLES -A $chain @options -j LOG");
351 run
("$IPTABLES -A $chain @options -j $target");
358 sub get_external_interface
() {
359 open(IFACE
, "/var/ipfire/red/iface") or return "";
366 sub get_external_address
() {
367 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
368 my $address = <ADDR
>;
377 foreach my $alias (sort keys %aliases) {
379 return $aliases{$alias};
384 sub get_nat_address
{
387 # Any static address of any zone.
388 if ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
389 return $defaultNetworks{$zone . "_ADDRESS"};
391 } elsif ($zone eq "Default IP") {
392 return &get_external_address
();
395 return &get_alias
($zone);
398 print_error
("Could not find NAT address");
401 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
405 # Convert the given time into minutes.
406 my $minutes = &time_convert_to_minutes
($val);
408 # Move the timestamp into UTC.
409 $minutes += &time_utc_offset
();
411 # Make sure $minutes is between 00:00 and 23:59.
416 if ($minutes > 1440) {
421 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
424 # Calculates the offsets in minutes from the local timezone to UTC.
425 sub time_utc_offset
{
426 my @localtime = localtime(time);
427 my @gmtime = gmtime(time);
429 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
432 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
433 sub time_convert_to_minutes
{
434 my ($hrs, $min) = split(":", shift);
436 return ($hrs * 60) + $min;
442 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
445 my $CMD = "-m ipp2p";
446 foreach my $p2pentry (sort @p2ps) {
447 my @p2pline = split( /\;/, $p2pentry );
448 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
450 if ("$p2pline[2]" eq "on") {
451 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
455 if ("$p2pline[2]" eq "off") {
456 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
462 run
("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
476 if ($type eq "src") {
477 $addr_type = $$hash{$key}[3];
478 $value = $$hash{$key}[4];
480 } elsif ($type eq "tgt") {
481 $addr_type = $$hash{$key}[5];
482 $value = $$hash{$key}[6];
485 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
486 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
487 if ($customgrp{$grp}[0] eq $value) {
488 my @address = &get_address
($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
491 push(@addresses, @address);
496 my @address = &get_address
($addr_type, $value, $type);
499 push(@addresses, @address);
513 # If the user manually typed an address, we just check if it is a MAC
514 # address. Otherwise, we assume that it is an IP address.
515 if ($key ~~ ["src_addr", "tgt_addr"]) {
516 if (&General
::validmac
($value)) {
517 push(@ret, "-m mac --mac-source $value");
522 # If a default network interface (GREEN, BLUE, etc.) is selected, we
523 # try to get the corresponding address of the network.
524 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
525 my $external_interface = &get_external_interface
();
527 my $network_address = &fwlib
::get_std_net_ip
($value, $external_interface);
528 if ($network_address) {
529 push(@ret, $network_address);
533 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
534 my $network_address = &fwlib
::get_net_ip
($value);
535 if ($network_address) {
536 push(@ret, $network_address);
540 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
541 my $host_address = &fwlib
::get_host_ip
($value, $type);
543 push(@ret, $host_address);
547 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
548 my $network_address = &fwlib
::get_ovpn_net_ip
($value, 1);
549 if ($network_address) {
550 push(@ret, $network_address);
554 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
555 my $host_address = &fwlib
::get_ovpn_host_ip
($value, 33);
557 push(@ret, $host_address);
561 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
562 my $network_address = &fwlib
::get_ovpn_n2n_ip
($value, 11);
563 if ($network_address) {
564 push(@ret, $network_address);
568 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
569 my $network_address = &fwlib
::get_ipsec_net_ip
($value, 11);
570 if ($network_address) {
571 push(@ret, $network_address);
574 # The firewall's own IP addresses.
575 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
577 if ($value eq "ALL") {
581 } elsif ($value eq "GREEN") {
582 push(@ret, $defaultNetworks{"GREEN_ADDRESS"});
585 } elsif ($value eq "BLUE") {
586 push(@ret, $defaultNetworks{"BLUE_ADDRESS"});
589 } elsif ($value eq "ORANGE") {
590 push(@ret, $defaultNetworks{"ORANGE_ADDRESS"});
593 } elsif ($value ~~ ["RED", "RED1"]) {
594 my $address = &get_external_address
();
596 push(@ret, $address);
601 my %alias = &get_alias
($value);
603 push(@ret, $alias{"IPT"});
607 # If nothing was selected, we assume "any".
619 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
620 my $uses_services = ($$hash{$key}[11] eq "ON");
624 # Rules which don't have source ports or services (like ICMP, ESP, ...).
625 if (!$uses_source_ports && !$uses_services) {
626 push(@protocols, $$hash{$key}[8]);
628 # Rules which either use ports or services.
629 } elsif ($uses_source_ports || $uses_services) {
630 # Check if service group or service
631 if ($$hash{$key}[14] eq 'cust_srv') {
632 push(@protocols, &fwlib
::get_srv_prot
($$hash{$key}[15]));
634 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
635 my $protos = &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
636 push(@protocols, split(",", $protos));
639 # Fetch the protocol for this rule.
640 my $protocol = lc($$hash{$key}[8]);
642 # Fetch source and destination ports for this rule.
643 my $source_ports = $$hash{$key}[10];
644 my $destination_ports = $$hash{$key}[15];
646 # Check if ports are set for protocols which do not support ports.
647 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
648 print_error
("$protocol does not support ports");
652 push(@protocols, $protocol);
656 # Remove all empty elements
657 @protocols = map { $_ ?
$_ : () } @protocols;
659 # If no protocol has been defined, we assume "all".
661 push(@protocols, "all");
664 # Make all protocol names lowercase.
665 @protocols = map { lc } @protocols;
670 sub get_protocol_options
{
673 my $protocol = shift;
676 # Process source ports.
677 my $use_src_ports = ($$hash{$key}[7] eq "ON");
678 my $src_ports = $$hash{$key}[10];
680 if ($use_src_ports && $src_ports) {
681 push(@options, &format_ports
($src_ports, "src"));
684 # Process destination ports.
685 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
686 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
688 if ($use_dst_ports) {
689 my $dst_ports_mode = $$hash{$key}[14];
690 my $dst_ports = $$hash{$key}[15];
691 if ($use_dnat && $$hash{$key}[30]) {
692 $dst_ports = $$hash{$key}[30];
695 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
696 push(@options, &format_ports
($dst_ports, "dst"));
698 } elsif ($dst_ports_mode eq "cust_srv") {
699 if ($protocol eq "ICMP") {
700 push(@options, ("--icmp-type", &fwlib
::get_srv_port
($dst_ports, 3, "ICMP")));
702 $dst_ports = &fwlib
::get_srv_port
($dst_ports, 1, uc($protocol));
703 push(@options, &format_ports
($dst_ports, "dst"));
706 } elsif ($dst_ports_mode eq "cust_srvgrp") {
707 push(@options, &fwlib
::get_srvgrp_port
($dst_ports, uc($protocol)));
711 # Check if a single ICMP type is selected.
712 if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") {
713 my $icmp_type = $$hash{$key}[9];
715 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
716 push(@options, ("--icmp-type", $icmp_type));
728 if ($type eq "src") {
730 } elsif ($type eq "dst") {
736 if ($ports =~ /\|/) {
738 push(@options, ("-m", "multiport"));
741 push(@options, ($arg, $ports));
746 sub get_dnat_target_port
{
750 if ($$hash{$key}[14] eq "TGT_PORT") {
751 return $$hash{$key}[15];
755 sub add_dnat_mangle_rules
{
756 my $nat_address = shift;
760 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
763 # Skip rule if not all required information exists.
764 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
765 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
767 my @mangle_options = @options;
769 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
770 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
772 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
773 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
775 run
("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");