]> git.proxmox.com Git - pve-firewall.git/blobdiff - PVE/Firewall.pm
ignoreZ source/destination port if no protocol specified
[pve-firewall.git] / PVE / Firewall.pm
index 5583ec061127d5d9fab31e89f474994c5db33f63..6309b81e0223aa3ad674d20ce926ad813e59ff94 100644 (file)
@@ -224,7 +224,7 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'tcp', dport => '1723' },
     ],
     'Ping' => [
-       { action => 'PARAM', proto => 'icmp', dport => '8' },
+       { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
     ],
     'PostgreSQL' => [
        { action => 'PARAM', proto => 'tcp', dport => '5432' },
@@ -322,7 +322,7 @@ my $pve_fw_macros = {
     ],
     'Trcrt' => [
        { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
-       { action => 'PARAM', proto => 'icmp', dport => '8' },
+       { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
     ],
     'VNC' => [
        { action => 'PARAM', proto => 'tcp', dport => '5900:5909' },
@@ -714,39 +714,55 @@ sub iptables_rule_exist {
 sub ruleset_generate_rule {
     my ($ruleset, $chain, $rule, $actions, $goto) = @_;
 
+    return if $rule->{disable};
+
     my $cmd = '';
 
     $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
     $cmd .= " -s $rule->{source}" if $rule->{source};
     $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
     $cmd .= " -d $rule->{dest}" if $rule->{dest};
-    $cmd .= " -p $rule->{proto}" if $rule->{proto};
 
-    if (($rule->{nbdport} && $rule->{nbdport} > 1) ||
-       ($rule->{nbsport} && $rule->{nbsport} > 1)) {
-       $cmd .= " --match multiport" 
-    }
+    if ($rule->{proto}) {
+       $cmd .= " -p $rule->{proto}";
 
-    if ($rule->{dport}) {
-       if ($rule->{proto} && $rule->{proto} eq 'icmp') {
-           # Note: we use dport to store --icmp-type
-           die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport}};
-           $cmd .= " -m icmp --icmp-type $rule->{dport}";
-       } else {
-           if ($rule->{nbdport} && $rule->{nbdport} > 1) {
-               $cmd .= " --dports $rule->{dport}";
+       my $multiport = 0;
+       $multiport++ if $rule->{nbdport} && ($rule->{nbdport} > 1);
+       $multiport++ if $rule->{nbsport} && ($rule->{nbsport} > 1);
+
+       $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 ($rule->{proto} && $rule->{proto} eq 'icmp') {
+               # Note: we use dport to store --icmp-type
+               die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}});
+               $cmd .= " -m icmp --icmp-type $rule->{dport}";
            } else {
-               $cmd .= " --dport $rule->{dport}";
+               if ($rule->{nbdport} && $rule->{nbdport} > 1) {
+                   if ($multiport == 2) {
+                       $cmd .= " --ports $rule->{dport}";
+                   } else {
+                       $cmd .= " --dports $rule->{dport}";
+                   }
+               } else {
+                   $cmd .= " --dport $rule->{dport}";
+               }
            }
        }
-    }
 
-    if ($rule->{sport}) {
-       if ($rule->{nbsport} && $rule->{nbsport} > 1) {
-           $cmd .= " --sports $rule->{sport}";
-       } else {
-           $cmd .= " --sport $rule->{sport}";
+       if ($rule->{sport}) {
+           if ($rule->{nbsport} && $rule->{nbsport} > 1) {
+               $cmd .= " --sports $rule->{sport}" if $multiport != 2;
+           } else {
+               $cmd .= " --sport $rule->{sport}";
+           }
        }
+    } elsif ($rule->{dport} || $rule->{sport}) {
+       warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport};
+       warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport};
     }
 
     $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
@@ -755,7 +771,7 @@ sub ruleset_generate_rule {
        $action = $actions->{$action} if defined($actions->{$action}); 
        $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
        $cmd .= $goto ? " -g $action" : " -j $action";
-    };
+    }
 
     ruleset_addrule($ruleset, $chain, $cmd) if $cmd;
 }
@@ -1013,7 +1029,11 @@ sub parse_fw_rule {
 
     my ($action, $iface, $source, $dest, $proto, $dport, $sport);
 
-    $line =~ s/#.*$//;
+    # we can add single line comments to the end of the rule
+    my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
+
+    # we can disable a rule when prefixed with '|'
+    my $disable = 1 if  $line =~ s/^\|//;
 
     my @data = split(/\s+/, $line);
     my $expected_elements = $need_iface ? 7 : 6;
@@ -1072,6 +1092,8 @@ sub parse_fw_rule {
     my $rules = [];
 
     my $param = {
+       disable => $disable,
+       comment => $comment,
        action => $action,
        iface => $iface,
        source => $source,