]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/rules.pl
182c9482aa52f0303756d5896f2f126ac7100650
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 = "OUTGOINGFW";
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 $CHAIN_INPUT");
125 run
("$IPTABLES -F $CHAIN_FORWARD");
126 run
("$IPTABLES -F $CHAIN_OUTPUT");
127 run
("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
128 run
("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
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 # Check if logging should be enabled.
176 my $LOG = ($$hash{$key}[17] eq 'ON');
178 # Check if NAT is enabled and initialize variables, that we use for that.
179 my $NAT = ($$hash{$key}[28] eq 'ON');
182 $NAT_MODE = uc($$hash{$key}[31]);
185 # Set up time constraints.
186 my @time_options = ();
187 if ($$hash{$key}[18] eq 'ON') {
188 push(@time_options, ("-m", "time"));
190 # Select all days of the week this match is active.
192 if ($$hash{$key}[19] ne '') {
193 push (@weekdays, "Mon");
195 if ($$hash{$key}[20] ne '') {
196 push (@weekdays, "Tue");
198 if ($$hash{$key}[21] ne '') {
199 push (@weekdays, "Wed");
201 if ($$hash{$key}[22] ne '') {
202 push (@weekdays, "Thu");
204 if ($$hash{$key}[23] ne '') {
205 push (@weekdays, "Fri");
207 if ($$hash{$key}[24] ne '') {
208 push (@weekdays, "Sat");
210 if ($$hash{$key}[25] ne '') {
211 push (@weekdays, "Sun");
214 push(@time_options, ("--weekdays", join(",", @weekdays)));
217 # Convert start time.
218 my $time_start = &format_time
($$hash{$key}[26]);
220 push(@time_options, ("--timestart", $time_start));
224 my $time_stop = &format_time
($$hash{$key}[27]);
226 push(@time_options, ("--timestop", $time_stop));
230 # Check which protocols are used in this rule and so that we can
231 # later group rules by protocols.
232 my @protocols = &get_protocols
($hash, $key);
234 print_error
("Invalid protocol configuration for rule $key");
238 foreach my $protocol (@protocols) {
239 # Check if the given protocol is supported.
240 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
241 print_error
("Protocol $protocol is not supported (rule $key)");
245 # Prepare protocol options (like ICMP types, ports, etc...).
246 my @protocol_options = &get_protocol_options
($hash, $key, $protocol);
248 # Check if this protocol knows ports.
249 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
251 foreach my $source (@sources) {
252 foreach my $destination (@destinations) {
253 # Skip invalid rules.
254 next if (!$source || !$destination || ($destination eq "none"));
256 # Array with iptables arguments.
260 if ($protocol ne "all") {
261 push(@options, ("-p", $protocol));
262 push(@options, @protocol_options);
265 # Prepare source options.
266 my @source_options = ();
267 if ($source =~ /mac/) {
268 push(@source_options, $source);
270 push(@source_options, ("-s", $source));
273 # Prepare destination options.
274 my @destination_options = ("-d", $destination);
276 # Add time constraint options.
277 push(@options, @time_options);
281 my $nat_address = &get_nat_address
($$hash{$key}[29]);
283 # Skip NAT rules if the NAT address is unknown
284 # (i.e. no internet connection has been established, yet).
285 next unless ($nat_address);
288 if ($NAT_MODE eq "DNAT") {
289 # Make port-forwardings useable from the internal networks.
290 &add_dnat_mangle_rules
($nat_address, @options);
292 my @nat_options = @options;
293 push(@nat_options, @source_options);
294 push(@nat_options, ("-d", $nat_address));
296 my ($dnat_address, $dnat_mask) = split("/", $destination);
297 @destination_options = ("-d", $dnat_address);
299 if ($protocol_has_ports) {
300 my $dnat_port = &get_dnat_target_port
($hash, $key);
303 $dnat_address .= ":$dnat_port";
308 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j LOG --log-prefix 'DNAT '");
310 run
("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
313 } elsif ($NAT_MODE eq "SNAT") {
314 my @nat_options = @options;
316 push(@nat_options, @source_options);
317 push(@nat_options, @destination_options);
320 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j LOG --log-prefix 'SNAT '");
322 run
("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
326 push(@options, @source_options);
327 push(@options, @destination_options);
329 # Insert firewall rule.
331 run
("$IPTABLES -A $chain @options -j LOG");
333 run
("$IPTABLES -A $chain @options -j $target");
340 sub get_external_interface
() {
341 open(IFACE
, "/var/ipfire/red/iface") or return "";
348 sub get_external_address
() {
349 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
350 my $address = <ADDR
>;
359 foreach my $alias (sort keys %aliases) {
361 return $aliases{$alias};
366 sub get_nat_address
{
369 # Any static address of any zone.
370 if ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
371 return $defaultNetworks{$zone . "_ADDRESS"};
373 } elsif ($zone eq "Default IP") {
374 return &get_external_address
();
377 return &get_alias
($zone);
380 print_error
("Could not find NAT address");
383 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
387 # Convert the given time into minutes.
388 my $minutes = &time_convert_to_minutes
($val);
390 # Move the timestamp into UTC.
391 $minutes += &time_utc_offset
();
393 # Make sure $minutes is between 00:00 and 23:59.
398 if ($minutes > 1440) {
403 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
406 # Calculates the offsets in minutes from the local timezone to UTC.
407 sub time_utc_offset
{
408 my @localtime = localtime(time);
409 my @gmtime = gmtime(time);
411 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
414 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
415 sub time_convert_to_minutes
{
416 my ($hrs, $min) = split(":", shift);
418 return ($hrs * 60) + $min;
424 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
427 my $CMD = "-m ipp2p";
428 foreach my $p2pentry (sort @p2ps) {
429 my @p2pline = split( /\;/, $p2pentry );
430 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
432 if ("$p2pline[2]" eq "on") {
433 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
437 if ("$p2pline[2]" eq "off") {
438 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
444 run
("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
458 if ($type eq "src") {
459 $addr_type = $$hash{$key}[3];
460 $value = $$hash{$key}[4];
462 } elsif ($type eq "tgt") {
463 $addr_type = $$hash{$key}[5];
464 $value = $$hash{$key}[6];
467 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
468 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
469 if ($customgrp{$grp}[0] eq $value) {
470 my @address = &get_address
($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
473 push(@addresses, @address);
478 my @address = &get_address
($addr_type, $value, $type);
481 push(@addresses, @address);
495 # If the user manually typed an address, we just check if it is a MAC
496 # address. Otherwise, we assume that it is an IP address.
497 if ($key ~~ ["src_addr", "tgt_addr"]) {
498 if (&General
::validmac
($value)) {
499 push(@ret, "-m mac --mac-source $value");
504 # If a default network interface (GREEN, BLUE, etc.) is selected, we
505 # try to get the corresponding address of the network.
506 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
507 my $external_interface = &get_external_interface
();
509 my $network_address = &fwlib
::get_std_net_ip
($value, $external_interface);
510 if ($network_address) {
511 push(@ret, $network_address);
515 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
516 my $network_address = &fwlib
::get_net_ip
($value);
517 if ($network_address) {
518 push(@ret, $network_address);
522 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
523 my $host_address = &fwlib
::get_host_ip
($value, $type);
525 push(@ret, $host_address);
529 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
530 my $network_address = &fwlib
::get_ovpn_net_ip
($value, 1);
531 if ($network_address) {
532 push(@ret, $network_address);
536 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
537 my $host_address = &fwlib
::get_ovpn_host_ip
($value, 33);
539 push(@ret, $host_address);
543 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
544 my $network_address = &fwlib
::get_ovpn_n2n_ip
($value, 11);
545 if ($network_address) {
546 push(@ret, $network_address);
550 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
551 my $network_address = &fwlib
::get_ipsec_net_ip
($value, 11);
552 if ($network_address) {
553 push(@ret, $network_address);
556 # The firewall's own IP addresses.
557 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
559 if ($value eq "ALL") {
563 } elsif ($value eq "GREEN") {
564 push(@ret, $defaultNetworks{"GREEN_ADDRESS"});
567 } elsif ($value eq "BLUE") {
568 push(@ret, $defaultNetworks{"BLUE_ADDRESS"});
571 } elsif ($value eq "ORANGE") {
572 push(@ret, $defaultNetworks{"ORANGE_ADDRESS"});
575 } elsif ($value ~~ ["RED", "RED1"]) {
576 my $address = &get_external_address
();
578 push(@ret, $address);
583 my %alias = &get_alias
($value);
585 push(@ret, $alias{"IPT"});
589 # If nothing was selected, we assume "any".
601 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
602 my $uses_services = ($$hash{$key}[11] eq "ON");
606 # Rules which don't have source ports or services (like ICMP, ESP, ...).
607 if (!$uses_source_ports && !$uses_services) {
608 push(@protocols, $$hash{$key}[8]);
610 # Rules which either use ports or services.
611 } elsif ($uses_source_ports || $uses_services) {
612 # Check if service group or service
613 if ($$hash{$key}[14] eq 'cust_srv') {
614 push(@protocols, &fwlib
::get_srv_prot
($$hash{$key}[15]));
616 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
617 my $protos = &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
618 push(@protocols, split(",", $protos));
621 # Fetch the protocol for this rule.
622 my $protocol = lc($$hash{$key}[8]);
624 # Fetch source and destination ports for this rule.
625 my $source_ports = $$hash{$key}[10];
626 my $destination_ports = $$hash{$key}[15];
628 # Check if ports are set for protocols which do not support ports.
629 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
630 print_error
("$protocol does not support ports");
634 push(@protocols, $protocol);
638 # Remove all empty elements
639 @protocols = map { $_ ?
$_ : () } @protocols;
641 # If no protocol has been defined, we assume "all".
643 push(@protocols, "all");
646 # Make all protocol names lowercase.
647 @protocols = map { lc } @protocols;
652 sub get_protocol_options
{
655 my $protocol = shift;
658 # Process source ports.
659 my $use_src_ports = ($$hash{$key}[7] eq "ON");
660 my $src_ports = $$hash{$key}[10];
662 if ($use_src_ports && $src_ports) {
663 push(@options, &format_ports
($src_ports, "src"));
666 # Process destination ports.
667 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
668 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
670 if ($use_dst_ports) {
671 my $dst_ports_mode = $$hash{$key}[14];
672 my $dst_ports = $$hash{$key}[15];
674 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
675 if ($use_dnat && $$hash{$key}[30]) {
676 $dst_ports = $$hash{$key}[30];
678 push(@options, &format_ports
($dst_ports, "dst"));
680 } elsif ($dst_ports_mode eq "cust_srv") {
681 if ($protocol eq "ICMP") {
682 push(@options, ("--icmp-type", &fwlib
::get_srv_port
($dst_ports, 3, "ICMP")));
684 $dst_ports = &fwlib
::get_srv_port
($dst_ports, 1, uc($protocol));
685 push(@options, &format_ports
($dst_ports, "dst"));
688 } elsif ($dst_ports_mode eq "cust_srvgrp") {
689 push(@options, &fwlib
::get_srvgrp_port
($dst_ports, uc($protocol)));
693 # Check if a single ICMP type is selected.
694 if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") {
695 my $icmp_type = $$hash{$key}[9];
697 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
698 push(@options, ("--icmp-type", $icmp_type));
710 if ($type eq "src") {
712 } elsif ($type eq "dst") {
718 if ($ports =~ /\|/) {
720 push(@options, ("-m", "multiport"));
724 push(@options, ($arg, $ports));
730 sub get_dnat_target_port
{
734 if ($$hash{$key}[14] eq "TGT_PORT") {
735 my $port = $$hash{$key}[15];
736 my $external_port = $$hash{$key}[30];
738 if ($external_port && ($port ne $external_port)) {
739 return $$hash{$key}[15];
744 sub add_dnat_mangle_rules
{
745 my $nat_address = shift;
749 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
752 # Skip rule if not all required information exists.
753 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
754 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
756 my @mangle_options = @options;
758 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
759 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
761 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
762 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
764 run
("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");