-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
- }
-
- if ($rule =~ s/^-m addrtype\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/^-p (tcp|udp)\s*//) {
- die "missing proto" if !$pkg->{proto};
- return undef if $pkg->{proto} 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
- }
-
- if ($rule =~ s/^--dport (\d+)\s*//) {
- die "missing dport" if !$pkg->{dport};
- return undef if $pkg->{dport} != $1; # no match
- }
-
- if ($rule =~ s/^-s (\S+)\s*//) {
- die "missing source" if !$pkg->{source};
- return undef if $pkg->{source} ne $1; # no match
- }
-
- if ($rule =~ s/^-d (\S+)\s*//) {
- die "missing destination" if !$pkg->{dest};
- return undef if $pkg->{dest} ne $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
- }
-
- 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-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/^-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);
- }
-
- die "unable to parse rule: $rule";
-}
-
-sub ruleset_simulate_chain {
- my ($ruleset, $chain, $pkg) = @_;
-
- $trace .= "ENTER chain $chain\n";
-
- if ($chain eq 'PVEFW-Drop') {
- $trace .= "LEAVE chain $chain\n";
- return 'DROP';
- }
- if ($chain eq 'PVEFW-reject') {
- $trace .= "LEAVE chain $chain\n";
- return 'REJECT';
- }
-
- if ($chain eq 'PVEFW-tcpflags') {
- $trace .= "LEAVE chain $chain\n";
- return undef;
- }
-
- my $rules = $ruleset->{$chain} ||
- die "no such chain '$chain'";
-
- foreach my $rule (@$rules) {
- my ($goto, $action) = rule_match($chain, $rule, $pkg);
- if (!defined($action)) {
- $trace .= "SKIP: $rule\n";
- next;
- }
- $trace .= "MATCH: $rule\n";
-
- if ($action eq 'ACCEPT' || $action eq 'DROP' || $action eq 'REJECT') {
- $trace .= "TERMINATE chain $chain: $action\n";
- return $action;
- } elsif ($action eq 'RETURN') {
- $trace .= "RETURN FROM chain $chain\n";
- last;
- } else {
- if ($goto) {
- $trace .= "LEAVE chain $chain - goto $action\n";
- return ruleset_simulate_chain($ruleset, $action, $pkg)
- #$chain = $action;
- #$rules = $ruleset->{$chain} || die "no such chain '$chain'";
- } else {
- if ($action = ruleset_simulate_chain($ruleset, $action, $pkg)) {
- return $action;
- }
- $trace .= "CONTINUE chain $chain\n";
- }
- }
- }
-
- $trace .= "LEAVE chain $chain\n";
- if ($chain =~ m/^PVEFW-(INPUT|OUTPUT|FORWARD)$/) {
- return 'ACCEPT'; # default policy
- }
-
- return undef;