]>
git.proxmox.com Git - pve-firewall.git/blob - test/fwtester.pl
9aa3c480627b88471012f615ccee9a44abdd9f70
14 my $outside_iface = 'eth0';
15 my $outside_bridge = 'vmbr0';
19 sub print_usage_and_exit
{
20 die "usage: $0 [--debug] [testfile [testid]]\n";
23 if (!GetOptions
('debug' => \
$debug)) {
24 print_usage_and_exit
();
27 my $testfilename = shift;
41 my ($chain, $rule, $pkg) = @_;
43 $rule =~ s/^-A $chain // || die "got strange rule: $rule";
45 if ($rule =~ s/^-m conntrack\s*//) {
46 return undef; # simply ignore
49 if ($rule =~ s/^-m addrtype\s*//) {
50 return undef; # simply ignore
53 if ($rule =~ s/^-i (\S+)\s*//) {
54 die "missing iface_in" if !$pkg->{iface_in
};
55 return undef if $pkg->{iface_in
} ne $1; # no match
57 if ($rule =~ s/^-o (\S+)\s*//) {
58 die "missing iface_out" if !$pkg->{iface_out
};
59 return undef if $pkg->{iface_out
} ne $1; # no match
62 if ($rule =~ s/^-p (tcp|udp)\s*//) {
63 die "missing proto" if !$pkg->{proto
};
64 return undef if $pkg->{proto
} ne $1; # no match
67 if ($rule =~ s/^--dport (\d+):(\d+)\s*//) {
68 die "missing dport" if !$pkg->{dport
};
69 return undef if ($pkg->{dport
} < $1) || ($pkg->{dport
} > $2); # no match
72 if ($rule =~ s/^--dport (\d+)\s*//) {
73 die "missing dport" if !$pkg->{dport
};
74 return undef if $pkg->{dport
} != $1; # no match
77 if ($rule =~ s/^-s (\S+)\s*//) {
78 die "missing source" if !$pkg->{source
};
79 return undef if $pkg->{source
} ne $1; # no match
82 if ($rule =~ s/^-d (\S+)\s*//) {
83 die "missing destination" if !$pkg->{dest
};
84 return undef if $pkg->{dest
} ne $1; # no match
87 if ($rule =~ s/^-m mac ! --mac-source (\S+)\s*//) {
88 die "missing source mac" if !$pkg->{mac_source
};
89 return undef if $pkg->{mac_source
} eq $1; # no match
92 if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-in (\S+)\s*//) {
95 return undef if !$pkg->{physdev_in
};
96 return undef if $pkg->{physdev_in
} !~ m/^${devre}$/;
99 if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-out (\S+)\s*//) {
101 $devre =~ s/\+/\.\*/;
102 return undef if !$pkg->{physdev_out
};
103 return undef if $pkg->{physdev_out
} !~ m/^${devre}$/;
106 if ($rule =~ s/^-j MARK --set-mark (\d+)\s*$//) {
111 if ($rule =~ s/^-j (\S+)\s*$//) {
115 if ($rule =~ s/^-g (\S+)\s*$//) {
119 die "unable to parse rule: $rule";
122 sub ruleset_simulate_chain
{
123 my ($ruleset, $chain, $pkg) = @_;
125 add_trace
("ENTER chain $chain\n");
127 my $counter = 2; # ENTER + LEAVE = 2
129 if ($chain eq 'PVEFW-Drop') {
130 add_trace
("LEAVE chain $chain\n");
131 return ('DROP', $counter);
133 if ($chain eq 'PVEFW-reject') {
134 add_trace
("LEAVE chain $chain\n");
135 return ('REJECT', $counter);
138 if ($chain eq 'PVEFW-tcpflags') {
139 add_trace
("LEAVE chain $chain\n");
140 return (undef, $counter);
143 my $rules = $ruleset->{$chain} ||
144 die "no such chain '$chain'";
146 foreach my $rule (@$rules) {
148 my ($goto, $action) = rule_match
($chain, $rule, $pkg);
149 if (!defined($action)) {
150 add_trace
("SKIP: $rule\n");
153 add_trace
("MATCH: $rule\n");
155 if ($action eq 'ACCEPT' || $action eq 'DROP' || $action eq 'REJECT') {
156 add_trace
("TERMINATE chain $chain: $action\n");
157 return ($action, $counter);
158 } elsif ($action eq 'RETURN') {
159 add_trace
("RETURN FROM chain $chain\n");
163 add_trace
("LEAVE chain $chain - goto $action\n");
164 return ruleset_simulate_chain
($ruleset, $action, $pkg)
166 #$rules = $ruleset->{$chain} || die "no such chain '$chain'";
168 my ($act, $ctr) = ruleset_simulate_chain
($ruleset, $action, $pkg);
170 return ($act, $counter) if $act;
171 add_trace
("CONTINUE chain $chain\n");
176 add_trace
("LEAVE chain $chain\n");
177 if ($chain =~ m/^PVEFW-(INPUT|OUTPUT|FORWARD)$/) {
178 return ('ACCEPT', $counter); # default policy
181 return (undef, $counter);
189 while (my ($k,$v) = each %$pkg) {
196 # Try to simulate packet traversal inside kernel. This invokes iptable
197 # checks several times.
199 my ($ruleset, $ipset_ruleset, $pkg, $from_info, $target, $start_state) = @_;
201 my $route_state = $start_state;
205 my $ipt_invocation_counter = 0;
206 my $rule_check_counter = 0;
208 while ($route_state ne $target->{iface
}) {
211 my $next_route_state;
214 $pkg->{iface_in
} = $pkg->{iface_out
} = undef;
215 $pkg->{physdev_in
} = $pkg->{physdev_out
} = undef;
217 if ($route_state eq 'from-outside') {
218 $next_route_state = $outside_bridge || die 'internal error';
219 $next_physdev_in = $outside_iface || die 'internal error';
220 } elsif ($route_state eq 'host') {
222 if ($target->{type
} eq 'outside') {
223 $pkg->{iface_in
} = 'lo';
224 $pkg->{iface_out
} = $outside_bridge;
225 $chain = 'PVEFW-OUTPUT';
226 $next_route_state = $outside_iface
227 } elsif ($target->{type
} eq 'ct') {
228 $pkg->{iface_in
} = 'lo';
229 $pkg->{iface_out
} = 'venet0';
230 $chain = 'PVEFW-OUTPUT';
231 $next_route_state = 'venet-in';
232 } elsif ($target->{type
} eq 'vm') {
233 $pkg->{iface_in
} = 'lo';
234 $pkg->{iface_out
} = $target->{bridge
} || die 'internal error';
235 $chain = 'PVEFW-OUTPUT';
236 $next_route_state = 'fwbr-in';
241 } elsif ($route_state eq 'venet-out') {
243 if ($target->{type
} eq 'host') {
245 $chain = 'PVEFW-INPUT';
246 $pkg->{iface_in
} = 'venet0';
247 $pkg->{iface_out
} = 'lo';
248 $next_route_state = 'host';
250 } elsif ($target->{type
} eq 'outside') {
252 $chain = 'PVEFW-FORWARD';
253 $pkg->{iface_in
} = 'venet0';
254 $pkg->{iface_out
} = $outside_bridge;
255 $next_route_state = $outside_iface;
257 } elsif ($target->{type
} eq 'vm') {
259 $chain = 'PVEFW-FORWARD';
260 $pkg->{iface_in
} = 'venet0';
261 $pkg->{iface_out
} = $target->{bridge
} || die 'internal error';
262 $next_route_state = 'fwbr-in';
264 } elsif ($target->{type
} eq 'ct') {
266 $chain = 'PVEFW-FORWARD';
267 $pkg->{iface_in
} = 'venet0';
268 $pkg->{iface_out
} = 'venet0';
269 $next_route_state = 'venet-in';
275 } elsif ($route_state eq 'fwbr-out') {
277 $chain = 'PVEFW-FORWARD';
278 $next_route_state = $from_info->{bridge
} || die 'internal error';
279 $next_physdev_in = $from_info->{fwpr
} || die 'internal error';
280 $pkg->{iface_in
} = $from_info->{fwbr
} || die 'internal error';
281 $pkg->{iface_out
} = $from_info->{fwbr
} || die 'internal error';
282 $pkg->{physdev_in
} = $from_info->{tapdev
} || die 'internal error';
283 $pkg->{physdev_out
} = $from_info->{fwln
} || die 'internal error';
285 } elsif ($route_state eq 'fwbr-in') {
287 $chain = 'PVEFW-FORWARD';
288 $next_route_state = $target->{tapdev
};
289 $pkg->{iface_in
} = $target->{fwbr
} || die 'internal error';
290 $pkg->{iface_out
} = $target->{fwbr
} || die 'internal error';
291 $pkg->{physdev_in
} = $target->{fwln
} || die 'internal error';
292 $pkg->{physdev_out
} = $target->{tapdev
} || die 'internal error';
294 } elsif ($route_state =~ m/^vmbr\d+$/) {
296 die "missing physdev_in - internal error?" if !$physdev_in;
298 if ($target->{type
} eq 'host') {
300 $chain = 'PVEFW-INPUT';
301 $pkg->{iface_in
} = $route_state;
302 $pkg->{iface_out
} = 'lo';
303 $next_route_state = 'host';
305 } elsif ($target->{type
} eq 'outside') {
307 $chain = 'PVEFW-FORWARD';
308 $pkg->{iface_in
} = $route_state;
309 $pkg->{iface_out
} = $outside_bridge;
310 $pkg->{physdev_in
} = $physdev_in;
311 # conditionally set physdev_out (same behavior as kernel)
312 if ($route_state eq $outside_bridge) {
313 $pkg->{physdev_out
} = $outside_iface || die 'internal error';
315 $next_route_state = $outside_iface;
317 } elsif ($target->{type
} eq 'ct') {
319 $chain = 'PVEFW-FORWARD';
320 $pkg->{iface_in
} = $route_state;
321 $pkg->{iface_out
} = 'venet0';
322 $next_route_state = 'venet-in';
324 } elsif ($target->{type
} eq 'vm') {
326 $chain = 'PVEFW-FORWARD';
327 $pkg->{iface_in
} = $route_state;
328 $pkg->{iface_out
} = $target->{bridge
};
329 $pkg->{physdev_in
} = $physdev_in;
330 # conditionally set physdev_out (same behavior as kernel)
331 if ($route_state eq $target->{bridge
}) {
332 $pkg->{physdev_out
} = $target->{fwpr
} || die 'internal error';
334 $next_route_state = 'fwbr-in';
341 die "implement me $route_state";
344 die "internal error" if !defined($next_route_state);
347 add_trace
("IPT check at $route_state (chain $chain)\n");
348 add_trace
(Dumper
($pkg));
349 $ipt_invocation_counter++;
350 my ($res, $ctr) = ruleset_simulate_chain
($ruleset, $chain, $pkg);
351 $rule_check_counter += $ctr;
352 return ($res, $ipt_invocation_counter, $rule_check_counter) if $res ne 'ACCEPT';
355 $route_state = $next_route_state;
357 $physdev_in = $next_physdev_in;
360 return ('ACCEPT', $ipt_invocation_counter, $rule_check_counter);
363 sub extract_ct_info
{
364 my ($vmdata, $vmid) = @_;
366 my $info = { type
=> 'ct', vmid
=> $vmid };
368 my $conf = $vmdata->{openvz
}->{$vmid} || die "no such CT '$vmid'";
369 if ($conf->{ip_address
}) {
370 $info->{ip_address
} = $conf->{ip_address
}->{value
};
377 sub extract_vm_info
{
378 my ($vmdata, $vmid) = @_;
380 my $info = { type
=> 'vm', vmid
=> $vmid };
382 my $conf = $vmdata->{qemu
}->{$vmid} || die "no such VM '$vmid'";
383 my $net = PVE
::QemuServer
::parse_net
($conf->{net0
});
384 $info->{macaddr
} = $net->{macaddr
} || die "unable to get mac address";
385 $info->{bridge
} = $net->{bridge
} || die "unable to get bridge";
386 $info->{fwbr
} = "fwbr${vmid}i0";
387 $info->{tapdev
} = "tap${vmid}i0";
388 $info->{fwln
} = "fwln${vmid}i0";
389 $info->{fwpr
} = "fwpr${vmid}p0";
394 sub simulate_firewall
{
395 my ($ruleset, $ipset_ruleset, $vmdata, $test) = @_;
397 my $from = delete $test->{from
} || die "missing 'from' field";
398 my $to = delete $test->{to
} || die "missing 'to' field";
399 my $action = delete $test->{action
} || die "missing 'action'";
401 my $testid = delete $test->{id
};
403 die "from/to needs to be different" if $from eq $to;
413 while (my ($k,$v) = each %$test) {
414 die "unknown attribute '$k'\n" if !exists($pkg->{$k});
422 if ($from eq 'host') {
423 $from_info->{type
} = 'host';
424 $start_state = 'host';
425 } elsif ($from eq 'outside') {
426 $from_info->{type
} = 'outside';
427 $start_state = 'from-outside';
428 } elsif ($from =~ m/^ct(\d+)$/) {
430 $from_info = extract_ct_info
($vmdata, $vmid);
431 if ($from_info->{ip_address
}) {
432 $pkg->{source
} = $from_info->{ip_address
};
433 $start_state = 'venet-out';
437 } elsif ($from =~ m/^vm(\d+)$/) {
439 $from_info = extract_vm_info
($vmdata, $vmid);
440 $start_state = 'fwbr-out';
441 $pkg->{mac_source
} = $from_info->{macaddr
};
449 $target->{type
} = 'host';
450 $target->{iface
} = 'host';
451 } elsif ($to eq 'outside') {
452 $target->{type
} = 'outside';
453 $target->{iface
} = $outside_iface;
454 } elsif ($to =~ m/^ct(\d+)$/) {
456 $target = extract_ct_info
($vmdata, $vmid);
457 $target->{iface
} = 'venet-in';
459 if ($target->{ip_address
}) {
460 $pkg->{dest
} = $target->{ip_address
};
464 } elsif ($to =~ m/^vm(\d+)$/) {
466 $target = extract_vm_info
($vmdata, $vmid);
467 $target->{iface
} = $target->{tapdev
};
472 my ($res, $ic, $rc) = route_packet
($ruleset, $ipset_ruleset, $pkg,
473 $from_info, $target, $start_state);
475 add_trace
("IPT statistics: invocation = $ic, checks = $rc\n");
477 die "test failed ($res != $action)\n" if $action ne $res;
483 my ($vmdata, $testdir, $testfile, $testid) = @_;
485 $testfile = 'tests' if !$testfile;
487 $vmdata->{testdir
} = $testdir;
489 my ($ruleset, $ipset_ruleset) =
490 PVE
::Firewall
::compile
(undef, undef, $vmdata);
492 my $filename = "$testdir/$testfile";
493 my $fh = IO
::File-
>new($filename) ||
494 die "unable to open '$filename' - $!\n";
497 while (defined(my $line = <$fh>)) {
498 next if $line =~ m/^\s*$/;
499 next if $line =~ m/^#.*$/;
500 if ($line =~ m/^\{.*\}\s*$/) {
501 my $test = eval $line;
503 next if defined($testid) && (!defined($test->{id
}) || ($testid ne $test->{id
}));
505 print Dumper
($ruleset) if $debug;
507 eval { simulate_firewall
($ruleset, $ipset_ruleset, $vmdata, $test); };
510 print Dumper
($ruleset) if !$debug;
512 print "$trace\n" if !$debug;
514 print "$filename line $.: $line";
516 print "test failed: $err\n";
525 die "no tests found\n" if $testcount <= 0;
527 print "PASS: $filename\n";
535 net0
=> "e1000=0E:0B:38:B8:B3:21,bridge=vmbr0",
538 net0
=> "e1000=0E:0B:38:B8:B3:22,bridge=vmbr0",
542 net0
=> "e1000=0E:0B:38:B8:B4:21,bridge=vmbr1",
547 ip_address
=> { value
=> '10.0.200.1' },
550 ip_address
=> { value
=> '10.0.200.2' },
559 if (-d
$testfilename) {
560 $dir = $testfilename;
561 } elsif (-f
$testfilename) {
562 $dir = dirname
($testfilename);
563 $testfile = basename
($testfilename);
565 die "no such file/dir '$testfilename'\n";
568 run_tests
($vmdata, $dir, $testfile, $testid);
571 foreach my $dir (<test-
*>) {
573 run_tests
($vmdata, $dir);
577 print "OK - all tests passed\n";