X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=test%2Ffwtester.pl;h=35729eecb73fc02596fdf9e5289fa55afa8d9947;hp=afbc7f682bbc03d71094257d0995ba108e5eb550;hb=680d56eeb067910b25282765782278ced2bc6beb;hpb=ec2e28f6d07625b1d59298a7de949a716f6537d8 diff --git a/test/fwtester.pl b/test/fwtester.pl index afbc7f6..35729ee 100755 --- a/test/fwtester.pl +++ b/test/fwtester.pl @@ -7,13 +7,11 @@ use Data::Dumper; use PVE::Firewall; use Getopt::Long; use File::Basename; +use Net::IP; my $mark; my $trace; -my $outside_iface = 'eth0'; -my $outside_bridge = 'vmbr0'; - my $debug = 0; sub print_usage_and_exit { @@ -37,83 +35,114 @@ sub add_trace { } } +sub nf_dev_match { + my ($devre, $dev) = @_; + + $devre =~ s/\+$/\.\*/; + return ($dev =~ m/^${devre}$/) ? 1 : 0; +} + sub rule_match { my ($chain, $rule, $pkg) = @_; $rule =~ s/^-A $chain // || die "got strange rule: $rule"; - if ($rule =~ s/^-m conntrack\s*//) { - return undef; # simply ignore - } + while (length($rule)) { - if ($rule =~ s/^-m addrtype\s*//) { - return undef; # simply ignore - } + if ($rule =~ s/^-m conntrack\s*//) { + return undef; # simply ignore + } - if ($rule =~ s/^-i (\S+)\s*//) { - die "missing iface_in" if !$pkg->{iface_in}; - return undef if $pkg->{iface_in} ne $1; # no match - } - if ($rule =~ s/^-o (\S+)\s*//) { - die "missing iface_out" if !$pkg->{iface_out}; - return undef if $pkg->{iface_out} ne $1; # no match - } + if ($rule =~ s/^-m addrtype\s*//) { + return undef; # simply ignore + } - if ($rule =~ s/^-p (tcp|udp)\s*//) { - die "missing proto" if !$pkg->{proto}; - return undef if $pkg->{proto} ne $1; # no match - } + if ($rule =~ s/^-i (\S+)\s*//) { + my $devre = $1; + die "missing iface_in" if !$pkg->{iface_in}; + return undef if !nf_dev_match($devre, $pkg->{iface_in}); + next; + } - if ($rule =~ s/^--dport (\d+):(\d+)\s*//) { - die "missing dport" if !$pkg->{dport}; - return undef if ($pkg->{dport} < $1) || ($pkg->{dport} > $2); # no match - } + if ($rule =~ s/^-o (\S+)\s*//) { + my $devre = $1; + die "missing iface_out" if !$pkg->{iface_out}; + return undef if !nf_dev_match($devre, $pkg->{iface_out}); + next; + } - if ($rule =~ s/^--dport (\d+)\s*//) { - die "missing dport" if !$pkg->{dport}; - return undef if $pkg->{dport} != $1; # no match - } + if ($rule =~ s/^-p (tcp|udp)\s*//) { + die "missing proto" if !$pkg->{proto}; + return undef if $pkg->{proto} ne $1; # no match + next; + } - if ($rule =~ s/^-s (\S+)\s*//) { - die "missing source" if !$pkg->{source}; - return undef if $pkg->{source} ne $1; # no match - } + if ($rule =~ s/^--dport (\d+):(\d+)\s*//) { + die "missing dport" if !$pkg->{dport}; + return undef if ($pkg->{dport} < $1) || ($pkg->{dport} > $2); # no match + next; + } + + if ($rule =~ s/^--dport (\d+)\s*//) { + die "missing dport" if !$pkg->{dport}; + return undef if $pkg->{dport} != $1; # no match + next; + } + + if ($rule =~ s/^-s (\S+)\s*//) { + die "missing source" if !$pkg->{source}; + my $ip = Net::IP->new($1); + return undef if !$ip->overlaps(Net::IP->new($pkg->{source})); # no match + next; + } - if ($rule =~ s/^-d (\S+)\s*//) { - die "missing destination" if !$pkg->{dest}; - return undef if $pkg->{dest} ne $1; # no match - } + if ($rule =~ s/^-d (\S+)\s*//) { + die "missing destination" if !$pkg->{dest}; + my $ip = Net::IP->new($1); + return undef if !$ip->overlaps(Net::IP->new($pkg->{dest})); # no match + next; + } - if ($rule =~ s/^-m mac ! --mac-source (\S+)\s*//) { - die "missing source mac" if !$pkg->{mac_source}; - return undef if $pkg->{mac_source} eq $1; # no match - } + if ($rule =~ s/^-m mac ! --mac-source (\S+)\s*//) { + die "missing source mac" if !$pkg->{mac_source}; + return undef if $pkg->{mac_source} eq $1; # no match + next; + } - if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-in (\S+)\s*//) { - my $devre = $1; - $devre =~ s/\+/\.\*/; - return undef if !$pkg->{physdev_in}; - return undef if $pkg->{physdev_in} !~ m/^${devre}$/; - } + if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-in (\S+)\s*//) { + my $devre = $1; + return undef if !$pkg->{physdev_in}; + return undef if !nf_dev_match($devre, $pkg->{physdev_in}); + next; + } - if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-out (\S+)\s*//) { - my $devre = $1; - $devre =~ s/\+/\.\*/; - return undef if !$pkg->{physdev_out}; - return undef if $pkg->{physdev_out} !~ m/^${devre}$/; - } + if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-out (\S+)\s*//) { + my $devre = $1; + return undef if !$pkg->{physdev_out}; + return undef if !nf_dev_match($devre, $pkg->{physdev_out}); + next; + } - if ($rule =~ s/^-j MARK --set-mark (\d+)\s*$//) { - $mark = $1; - return undef; - } + if ($rule =~ s/^-m mark --mark (\d+)\s*//) { + return undef if !defined($mark) || $mark != $1; + next; + } - if ($rule =~ s/^-j (\S+)\s*$//) { - return (0, $1); - } + # final actions + if ($rule =~ s/^-j MARK --set-mark (\d+)\s*$//) { + $mark = $1; + return undef; + } + + if ($rule =~ s/^-j (\S+)\s*$//) { + return (0, $1); + } - if ($rule =~ s/^-g (\S+)\s*$//) { - return (1, $1); + if ($rule =~ s/^-g (\S+)\s*$//) { + return (1, $1); + } + + last; } die "unable to parse rule: $rule"; @@ -124,24 +153,27 @@ sub ruleset_simulate_chain { add_trace("ENTER chain $chain\n"); + my $counter = 0; + if ($chain eq 'PVEFW-Drop') { add_trace("LEAVE chain $chain\n"); - return 'DROP'; + return ('DROP', $counter); } if ($chain eq 'PVEFW-reject') { add_trace("LEAVE chain $chain\n"); - return 'REJECT'; + return ('REJECT', $counter); } if ($chain eq 'PVEFW-tcpflags') { add_trace("LEAVE chain $chain\n"); - return undef; + return (undef, $counter); } my $rules = $ruleset->{$chain} || die "no such chain '$chain'"; foreach my $rule (@$rules) { + $counter++; my ($goto, $action) = rule_match($chain, $rule, $pkg); if (!defined($action)) { add_trace("SKIP: $rule\n"); @@ -151,7 +183,7 @@ sub ruleset_simulate_chain { if ($action eq 'ACCEPT' || $action eq 'DROP' || $action eq 'REJECT') { add_trace("TERMINATE chain $chain: $action\n"); - return $action; + return ($action, $counter); } elsif ($action eq 'RETURN') { add_trace("RETURN FROM chain $chain\n"); last; @@ -162,9 +194,9 @@ sub ruleset_simulate_chain { #$chain = $action; #$rules = $ruleset->{$chain} || die "no such chain '$chain'"; } else { - if ($action = ruleset_simulate_chain($ruleset, $action, $pkg)) { - return $action; - } + my ($act, $ctr) = ruleset_simulate_chain($ruleset, $action, $pkg); + $counter += $ctr; + return ($act, $counter) if $act; add_trace("CONTINUE chain $chain\n"); } } @@ -172,10 +204,10 @@ sub ruleset_simulate_chain { add_trace("LEAVE chain $chain\n"); if ($chain =~ m/^PVEFW-(INPUT|OUTPUT|FORWARD)$/) { - return 'ACCEPT'; # default policy + return ('ACCEPT', $counter); # default policy } - return undef; + return (undef, $counter); } sub copy_packet { @@ -199,6 +231,9 @@ sub route_packet { my $physdev_in; + my $ipt_invocation_counter = 0; + my $rule_check_counter = 0; + while ($route_state ne $target->{iface}) { my $chain; @@ -208,16 +243,16 @@ sub route_packet { $pkg->{iface_in} = $pkg->{iface_out} = undef; $pkg->{physdev_in} = $pkg->{physdev_out} = undef; - if ($route_state eq 'from-outside') { - $next_route_state = $outside_bridge || die 'internal error'; - $next_physdev_in = $outside_iface || die 'internal error'; + if ($route_state eq 'from-bport') { + $next_route_state = $from_info->{bridge} || die 'internal error'; + $next_physdev_in = $from_info->{iface} || die 'internal error'; } elsif ($route_state eq 'host') { - if ($target->{type} eq 'outside') { + if ($target->{type} eq 'bport') { $pkg->{iface_in} = 'lo'; - $pkg->{iface_out} = $outside_bridge; + $pkg->{iface_out} = $target->{bridge} || die 'internal error'; $chain = 'PVEFW-OUTPUT'; - $next_route_state = $outside_iface + $next_route_state = $target->{iface} || die 'internal error'; } elsif ($target->{type} eq 'ct') { $pkg->{iface_in} = 'lo'; $pkg->{iface_out} = 'venet0'; @@ -241,12 +276,12 @@ sub route_packet { $pkg->{iface_out} = 'lo'; $next_route_state = 'host'; - } elsif ($target->{type} eq 'outside') { + } elsif ($target->{type} eq 'bport') { $chain = 'PVEFW-FORWARD'; $pkg->{iface_in} = 'venet0'; - $pkg->{iface_out} = $outside_bridge; - $next_route_state = $outside_iface; + $pkg->{iface_out} = $target->{bridge} || die 'internal error'; + $next_route_state = $target->{iface} || die 'internal error'; } elsif ($target->{type} eq 'vm') { @@ -288,6 +323,7 @@ sub route_packet { } elsif ($route_state =~ m/^vmbr\d+$/) { die "missing physdev_in - internal error?" if !$physdev_in; + $pkg->{physdev_in} = $physdev_in; if ($target->{type} eq 'host') { @@ -296,23 +332,16 @@ sub route_packet { $pkg->{iface_out} = 'lo'; $next_route_state = 'host'; - if ($route_state eq $outside_bridge) { - - } else { - - } - - } elsif ($target->{type} eq 'outside') { + } elsif ($target->{type} eq 'bport') { $chain = 'PVEFW-FORWARD'; $pkg->{iface_in} = $route_state; - $pkg->{iface_out} = $outside_bridge; - $pkg->{physdev_in} = $physdev_in; + $pkg->{iface_out} = $target->{bridge} || die 'internal error'; # conditionally set physdev_out (same behavior as kernel) - if ($route_state eq $outside_bridge) { - $pkg->{physdev_out} = $outside_iface || die 'internal error'; + if ($route_state eq $target->{bridge}) { + $pkg->{physdev_out} = $target->{iface} || die 'internal error'; } - $next_route_state = $outside_iface; + $next_route_state = $target->{iface}; } elsif ($target->{type} eq 'ct') { @@ -326,7 +355,6 @@ sub route_packet { $chain = 'PVEFW-FORWARD'; $pkg->{iface_in} = $route_state; $pkg->{iface_out} = $target->{bridge}; - $pkg->{physdev_in} = $physdev_in; # conditionally set physdev_out (same behavior as kernel) if ($route_state eq $target->{bridge}) { $pkg->{physdev_out} = $target->{fwpr} || die 'internal error'; @@ -346,8 +374,10 @@ sub route_packet { if ($chain) { add_trace("IPT check at $route_state (chain $chain)\n"); add_trace(Dumper($pkg)); - my $res = ruleset_simulate_chain($ruleset, $chain, $pkg); - return $res if $res ne 'ACCEPT'; + $ipt_invocation_counter++; + my ($res, $ctr) = ruleset_simulate_chain($ruleset, $chain, $pkg); + $rule_check_counter += $ctr; + return ($res, $ipt_invocation_counter, $rule_check_counter) if $res ne 'ACCEPT'; } $route_state = $next_route_state; @@ -355,7 +385,7 @@ sub route_packet { $physdev_in = $next_physdev_in; } - return 'ACCEPT'; + return ('ACCEPT', $ipt_invocation_counter, $rule_check_counter); } sub extract_ct_info { @@ -395,18 +425,21 @@ sub simulate_firewall { my $from = delete $test->{from} || die "missing 'from' field"; my $to = delete $test->{to} || die "missing 'to' field"; my $action = delete $test->{action} || die "missing 'action'"; - + + my $testid = delete $test->{id}; + die "from/to needs to be different" if $from eq $to; my $pkg = { proto => 'tcp', - sport => '1234', - dport => '4321', - source => '10.11.12.13', - dest => '10.11.12.14', + sport => undef, + dport => undef, + source => undef, + dest => undef, }; while (my ($k,$v) = each %$test) { + die "unknown attribute '$k'\n" if !exists($pkg->{$k}); $pkg->{$k} = $v; } @@ -417,9 +450,21 @@ sub simulate_firewall { if ($from eq 'host') { $from_info->{type} = 'host'; $start_state = 'host'; + } elsif ($from =~ m|^(vmbr\d+)/(\S+)$|) { + $from_info->{type} = 'bport'; + $from_info->{bridge} = $1; + $from_info->{iface} = $2; + $start_state = 'from-bport'; } elsif ($from eq 'outside') { - $from_info->{type} = 'outside'; - $start_state = 'from-outside'; + $from_info->{type} = 'bport'; + $from_info->{bridge} = 'vmbr0'; + $from_info->{iface} = 'eth0'; + $start_state = 'from-bport'; + } elsif ($from eq 'nfvm') { + $from_info->{type} = 'bport'; + $from_info->{bridge} = 'vmbr0'; + $from_info->{iface} = 'tapXYZ'; + $start_state = 'from-bport'; } elsif ($from =~ m/^ct(\d+)$/) { my $vmid = $1; $from_info = extract_ct_info($vmdata, $vmid); @@ -435,7 +480,7 @@ sub simulate_firewall { $start_state = 'fwbr-out'; $pkg->{mac_source} = $from_info->{macaddr}; } else { - die "implement me"; + die "unable to parse \"from => '$from'\"\n"; } my $target; @@ -443,9 +488,18 @@ sub simulate_firewall { if ($to eq 'host') { $target->{type} = 'host'; $target->{iface} = 'host'; + } elsif ($to =~ m|^(vmbr\d+)/(\S+)$|) { + $target->{type} = 'bport'; + $target->{bridge} = $1; + $target->{iface} = $2; } elsif ($to eq 'outside') { - $target->{type} = 'outside'; - $target->{iface} = $outside_iface; + $target->{type} = 'bport'; + $target->{bridge} = 'vmbr0'; + $target->{iface} = 'eth0'; + } elsif ($to eq 'nfvm') { + $target->{type} = 'bport'; + $target->{bridge} = 'vmbr0'; + $target->{iface} = 'tapXYZ'; } elsif ($to =~ m/^ct(\d+)$/) { my $vmid = $1; $target = extract_ct_info($vmdata, $vmid); @@ -461,11 +515,14 @@ sub simulate_firewall { $target = extract_vm_info($vmdata, $vmid); $target->{iface} = $target->{tapdev}; } else { - die "implement me"; + die "unable to parse \"to => '$to'\"\n"; } - my $res = route_packet($ruleset, $ipset_ruleset, $pkg, $from_info, $target, $start_state); + my ($res, $ic, $rc) = route_packet($ruleset, $ipset_ruleset, $pkg, + $from_info, $target, $start_state); + add_trace("IPT statistics: invocation = $ic, checks = $rc\n"); + die "test failed ($res != $action)\n" if $action ne $res; return undef; @@ -524,14 +581,14 @@ sub run_tests { my $vmdata = { qemu => { 100 => { - net0 => "e1000=0E:0B:38:B8:B3:21,bridge=vmbr0", + net0 => "e1000=0E:0B:38:B8:B3:21,bridge=vmbr0,firewall=1", }, 101 => { - net0 => "e1000=0E:0B:38:B8:B3:22,bridge=vmbr0", + net0 => "e1000=0E:0B:38:B8:B3:22,bridge=vmbr0,firewall=1", }, # on bridge vmbr1 110 => { - net0 => "e1000=0E:0B:38:B8:B4:21,bridge=vmbr1", + net0 => "e1000=0E:0B:38:B8:B4:21,bridge=vmbr1,firewall=1", }, }, openvz => {