]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/Firewall.pm
parse_protocol_file: support lines without end comments
[pve-firewall.git] / src / PVE / Firewall.pm
index 8d36175b7a6391f255b663ad59ea532ad1be9bc4..ac4850744a83571a5aec1340ace98997e0dfb48d 100644 (file)
@@ -876,12 +876,8 @@ sub get_etc_services {
     return $etc_services;
 }
 
-my $etc_protocols;
-
-sub get_etc_protocols {
-    return $etc_protocols if $etc_protocols;
-
-    my $filename = "/etc/protocols";
+sub parse_protocol_file {
+    my ($filename) = @_;
 
     my $fh = IO::File->new($filename, O_RDONLY);
     if (!$fh) {
@@ -896,7 +892,7 @@ sub get_etc_protocols {
        next if $line =~m/^#/;
        next if ($line =~m/^\s*$/);
 
-       if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
+       if ($line =~ m!^(\S+)\s+(\d+)(?:\s+.*)?$!) {
            $protocols->{byid}->{$2}->{name} = $1;
            $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
        }
@@ -904,6 +900,16 @@ sub get_etc_protocols {
 
     close($fh);
 
+    return $protocols;
+}
+
+my $etc_protocols;
+
+sub get_etc_protocols {
+    return $etc_protocols if $etc_protocols;
+
+    my $protocols = parse_protocol_file('/etc/protocols');
+
     # add special case for ICMP v6
     $protocols->{byid}->{icmpv6}->{name} = "icmpv6";
     $protocols->{byname}->{icmpv6} = $protocols->{byid}->{icmpv6};
@@ -1035,12 +1041,13 @@ sub parse_port_name_number_or_range {
     my @elements = split(/,/, $str);
     die "extraneous commas in list\n" if $str ne join(',', @elements);
     foreach my $item (@elements) {
-       $count++;
        if ($item =~ m/^(\d+):(\d+)$/) {
+           $count += 2;
            my ($port1, $port2) = ($1, $2);
            die "invalid port '$port1'\n" if $port1 > 65535;
            die "invalid port '$port2'\n" if $port2 > 65535;
        } elsif ($item =~ m/^(\d+)$/) {
+           $count += 1;
            my $port = $1;
            die "invalid port '$port'\n" if $port > 65535;
        } else {
@@ -1054,9 +1061,15 @@ sub parse_port_name_number_or_range {
        }
     }
 
-    die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
+    die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 0;
 
-    return $count;
+    # I really don't like to use the word number here, but it's the only thing
+    # that makes sense in a literal way. The range 1:100 counts as 2, not as
+    # one and not as 100...
+    die "too many entries in port list (> 15 numbers)\n"
+       if $count > 15;
+
+    return (scalar(@elements) > 1);
 }
 
 PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec);
@@ -1878,19 +1891,12 @@ sub ipt_rule_to_cmds {
        if (my $proto = $rule->{proto}) {
            push @match, "-p $proto";
 
-           my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}, 1) : 0;
-           my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}, 0) : 0;
-
-           my $multiport = 0;
-           $multiport++ if $nbdport > 1;
-           $multiport++ if $nbsport > 1;
+           my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, 1);
+           my $multisport = defined($rule->{sport}) && parse_port_name_number_or_range($rule->{sport}, 0);
 
-           push @match, "--match multiport" if $multiport;
+           my $add_dport = sub {
+               return if !$rule->{dport};
 
-           die "multiport: option '--sports' cannot be used together with '--dports'\n"
-               if ($multiport == 2) && ($rule->{dport} ne $rule->{sport});
-
-           if ($rule->{dport}) {
                if ($proto eq 'icmp') {
                    # Note: we use dport to store --icmp-type
                    die "unknown icmp-type '$rule->{dport}'\n"
@@ -1903,28 +1909,29 @@ sub ipt_rule_to_cmds {
                    push @match, "-m icmpv6 --icmpv6-type $rule->{dport}";
                } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) {
                    die "protocol $proto does not have ports\n";
+               } elsif ($multidport) {
+                   push @match, "--match multiport", "--dports $rule->{dport}";
                } else {
-                   if ($nbdport > 1) {
-                       if ($multiport == 2) {
-                           push @match,  "--ports $rule->{dport}";
-                       } else {
-                           push @match, "--dports $rule->{dport}";
-                       }
-                   } else {
-                       push @match, "--dport $rule->{dport}";
-                   }
+                   push @match, "--dport $rule->{dport}";
                }
-           }
+           };
+
+           my $add_sport = sub {
+               return if !$rule->{sport};
 
-           if ($rule->{sport}) {
                die "protocol $proto does not have ports\n"
                    if !$PROTOCOLS_WITH_PORTS->{$proto};
-               if ($nbsport > 1) {
-                   push @match, "--sports $rule->{sport}" if $multiport != 2;
+               if ($multisport) {
+                   push @match, "--match multiport", "--sports $rule->{sport}";
                } else {
                    push @match, "--sport $rule->{sport}";
                }
-           }
+           };
+
+           # order matters - single port before multiport!
+           $add_dport->() if $multisport;
+           $add_sport->();
+           $add_dport->() if !$multisport;
        } elsif ($rule->{dport} || $rule->{sport}) {
            die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport};
            die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport};
@@ -1952,103 +1959,6 @@ sub ipt_rule_to_cmds {
     return @iptcmds;
 }
 
-sub ruleset_generate_match {
-    my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
-
-    return if defined($rule->{enable}) && !$rule->{enable};
-    return if $rule->{errors};
-
-    return $rule->{match} if defined $rule->{match};
-
-    die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
-
-    my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}, 1) : 0;
-    my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}, 0) : 0;
-
-    my @cmd = ();
-
-    push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in};
-    push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out};
-
-    my $source = $rule->{source};
-    my $dest = $rule->{dest};
-
-    push @cmd, ipt_gen_src_or_dst_match($source, 's', $ipversion, $cluster_conf, $fw_conf) if $source;
-    push @cmd, ipt_gen_src_or_dst_match($dest, 'd', $ipversion, $cluster_conf, $fw_conf) if $dest;
-
-    if (my $proto = $rule->{proto}) {
-       push @cmd, "-p $proto";
-
-       my $multiport = 0;
-       $multiport++ if $nbdport > 1;
-       $multiport++ if $nbsport > 1;
-
-       push @cmd, "--match multiport" if $multiport;
-
-       die "multiport: option '--sports' cannot be used together with '--dports'\n"
-           if ($multiport == 2) && ($rule->{dport} ne $rule->{sport});
-
-       if ($rule->{dport}) {
-           if ($proto eq 'icmp') {
-               # Note: we use dport to store --icmp-type
-               die "unknown icmp-type '$rule->{dport}'\n"
-                   if $rule->{dport} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport}});
-               push @cmd, "-m icmp --icmp-type $rule->{dport}";
-           } elsif ($proto eq 'icmpv6') {
-               # Note: we use dport to store --icmpv6-type
-               die "unknown icmpv6-type '$rule->{dport}'\n"
-                   if $rule->{dport} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport}});
-               push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
-           } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) {
-               die "protocol $proto does not have ports\n";
-           } else {
-               if ($nbdport > 1) {
-                   if ($multiport == 2) {
-                       push @cmd,  "--ports $rule->{dport}";
-                   } else {
-                       push @cmd, "--dports $rule->{dport}";
-                   }
-               } else {
-                   push @cmd, "--dport $rule->{dport}";
-               }
-           }
-       }
-
-       if ($rule->{sport}) {
-           die "protocol $proto does not have ports\n"
-                if !$PROTOCOLS_WITH_PORTS->{$proto};
-           if ($nbsport > 1) {
-               push @cmd, "--sports $rule->{sport}" if $multiport != 2;
-           } else {
-               push @cmd, "--sport $rule->{sport}";
-           }
-       }
-    } elsif ($rule->{dport} || $rule->{sport}) {
-       die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport};
-       die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport};
-    }
-
-    push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
-
-    return scalar(@cmd) ? join(' ', @cmd) : undef;
-}
-
-sub ruleset_generate_action {
-    my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
-
-    return $rule->{target} if defined $rule->{target};
-
-    my @cmd = ();
-
-    if (my $action = $rule->{action}) {
-       $action = $actions->{$action} if defined($actions->{$action});
-       $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
-       push @cmd, $goto ? "-g $action" : "-j $action";
-    }
-
-    return scalar(@cmd) ? join(' ', @cmd) : undef;
-}
-
 sub ruleset_generate_rule {
     my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf) = @_;
 
@@ -2514,6 +2424,7 @@ sub generate_group_rules {
 
     foreach my $rule (@$rules) {
        next if $rule->{type} ne 'in';
+       next if !$rule->{enable} || $rule->{errors};
        next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
        rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
        ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf);
@@ -2526,6 +2437,7 @@ sub generate_group_rules {
 
     foreach my $rule (@$rules) {
        next if $rule->{type} ne 'out';
+       next if !$rule->{enable} || $rule->{errors};
        next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
        # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
        # check also other tap rules later