diff --git a/Shorewall/Perl/Shorewall/Chains.pm b/Shorewall/Perl/Shorewall/Chains.pm index f16b53fca..8f59e697e 100644 --- a/Shorewall/Perl/Shorewall/Chains.pm +++ b/Shorewall/Perl/Shorewall/Chains.pm @@ -1419,9 +1419,20 @@ sub delete_chain_and_references( $ ) { # We're going to delete this chain but first, we must delete all references to it. # my $tableref = $chain_table{$chainref->{table}}; - my $name1 = $chainref->{name}; - for ( @{$chainref->{rules}} ) { - decrement_reference_count( $tableref->{$_->{target}}, $name1 ) if $_->{target}; + my $name = $chainref->{name}; + + while ( my ( $chain, $references ) = each %{$chainref->{references}} ) { + # + # Delete all rules from $chain that have $name as their target + # + my $chain1ref = $tableref->{$chain}; + $chain1ref->{rules} = [ grep ( ( $_->{target} || '' ) ne $name, @{$chain1ref->{rules}} ) ]; + } + # + # Now decrement the reference count of all targets of this chain's rules + # + for ( grep $_, ( map( $_->{target}, @{$chainref->{rules}} ) ) ) { + decrement_reference_count( $tableref->{$_}, $name ); } delete_chain $chainref;