Farly/Rule/Optimizer.pm

filter: Statementstmt 84.4% 179 / 212 Branchbran 61.5% 48 / 78 Conditioncond 51.8% 14 / 27 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 90.0% 36 / 40 Podpod 69.2% 9 / 13 totaltotal 75.5% 291 / 385 CC 58 SCAR 19.4
CC
58
Cov
76%
_can_remove
29.6
_inconsistent
15.4
set_p_action
12.0
CRAP 7.0
« Farly/ASA/Rewriter.pm ↑ Summary Farly/ASA/Filter.pm »
1 package Farly::Rule::Optimizer;
2
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 ▶use Farly::Template::Cisco;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 our $VERSION = '0.26';
12
13 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
14 5 my ( $class, $rules ) = @_;
15
16 5 ▶ confess "Farly::Object::List object required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 0 5 5
17 unless ( defined($rules) );
18
19 5 ▶ confess "Farly::Object::List object required"
Branch: 1 branch
BranchTrueFalseTotal
unless $rules->isa("Farly::Object::List") 0 5 5
20 unless ( $rules->isa('Farly::Object::List') );
21
22 5 my $self = {
23 RULES => $rules,
24 OPTIMIZED => Farly::Object::List->new(),
25 REMOVED => Farly::Object::List->new(),
26 P_ACTION => 'permit',
27 D_ACTION => 'deny',
28 MODE => 'L4',
29 PROTOCOLS => [ 0, 6, 17 ],
30 PROPERTIES => [ 'PROTOCOL', 'SRC_IP', 'SRC_PORT', 'DST_IP', 'DST_PORT' ],
31 VERBOSE => undef,
32 TEMPLATE => Farly::Template::Cisco->new('ASA'),
33 };
34
35 5 bless $self, $class;
36
37
38 5 $log->info("$self NEW");
39 5 $log->info( "$self RULES " . $self->{RULES} );
40
41 #validate input rule set
42 5 $self->_is_valid_rule_set();
43 3 $self->_is_expanded();
44
45 3 return $self;
46 }
47
48 34 ▶sub rules { return $_[0]->{RULES}; }
Subroutine rules: called CC=1 SCAR=0.0
Pod undocumented
49 5 ▶sub optimized { return $_[0]->{OPTIMIZED}; }
Subroutine optimized: called CC=1 SCAR=0.0
Pod documented
50 1 ▶sub removed { return $_[0]->{REMOVED}; }
Subroutine removed: called CC=1 SCAR=0.0
Pod documented
51 3 ▶sub p_action { return $_[0]->{P_ACTION}; }
Subroutine p_action: called CC=1 SCAR=0.0
Pod undocumented
52 3 ▶sub d_action { return $_[0]->{D_ACTION}; }
Subroutine d_action: called CC=1 SCAR=0.0
Pod undocumented
53 18 ▶sub _mode { return $_[0]->{MODE}; }
Subroutine _mode: called CC=1 SCAR=0.0
54 6 ▶sub _protocols { return @{ $_[0]->{PROTOCOLS} }; }
Subroutine _protocols: called CC=1 SCAR=0.0
55 17 ▶sub _properties { return @{ $_[0]->{PROPERTIES} }; }
Subroutine _properties: called CC=1 SCAR=0.0
56 5 ▶sub _is_verbose { return $_[0]->{VERBOSE}; }
Subroutine _is_verbose: called CC=1 SCAR=0.0
57 0 ▶sub _template { return $_[0]->{TEMPLATE}; }
Subroutine _template: not called CC=1 SCAR=6.9
58
59 5 ▶sub _is_valid_rule_set {
Subroutine _is_valid_rule_set: called CC=4 SCAR=14.0
60 5 my ($self) = @_;
61
62 5 my $id = $self->rules->[0]->get('ID');
63
64 5 my $search = Farly::Object->new();
65 5 $search->set( 'ENTRY', Farly::Value::String->new('RULE') );
66 5 $search->set( 'ID', $id );
67
68 5 foreach my $rule ( $self->rules->iter() ) {
69 83 ▶ if ( $rule->has_defined('REMOVE') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->has_defined("REMOVE")) 0 83 83
70 0 die "found REMOVE in firewall ruleset ", $rule->dump();
71 }
72 83 ▶ if ( !$rule->matches($search) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule->matches($search)) 2 81 83
73 2 die "found invalid object in firewall ruleset ", $rule->dump();
74 }
75 }
76 }
77
78 3 ▶sub _is_expanded {
Subroutine _is_expanded: called CC=4 SCAR=14.8
79 3 my ($self) = @_;
80 3 foreach my $rule ( $self->rules->iter() ) {
81 60 foreach my $key ( $rule->get_keys() ) {
82 557 ▶ if ( $rule->get($key)->isa('Farly::Object::Ref') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->get($key)->isa("Farly::Object::Ref")) 0 557 557
83 0 die "an expanded firewall ruleset is required";
84 }
85 }
86 }
87 }
88
89 0 ▶sub verbose {
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
90 0 my ( $self, $flag ) = @_;
91 0 $self->{VERBOSE} = $flag;
92 }
93
94 0 ▶sub set_p_action {
Subroutine set_p_action: not called CC=3 SCAR=24.8
Pod documented
95 0 my ( $self, $action ) = @_;
96 0 ▶ confess "invalid action" unless ( defined($action) && length($action) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and length $action 0 0 0
Condition0%0/3
defined $action and length $action
!ll&&!rl&&r
000
MC/DC0%0/2
defined $action and length $action
defined $action length $action
Truth table0%0/3
A and B
A= defined $actionB= length $action
ABresult
0X0
100
111
97 0 $self->{P_ACTION} = $action;
98
99 0 $log->debug("set permit action to $action");
100 }
101
102 0 ▶sub set_d_action {
Subroutine set_d_action: not called CC=3 SCAR=24.8
Pod documented
103 0 my ( $self, $action ) = @_;
104 0 ▶ confess "invalid action" unless ( defined($action) && length($action) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and length $action 0 0 0
Condition0%0/3
defined $action and length $action
!ll&&!rl&&r
000
MC/DC0%0/2
defined $action and length $action
defined $action length $action
Truth table0%0/3
A and B
A= defined $actionB= length $action
ABresult
0X0
100
111
105 0 $self->{D_ACTION} = $action;
106
107 0 $log->debug("set deny action to $action");
108 }
109
110 # sort rules in ascending order by line number
111 49 ▶sub _ascending_LINE {
Subroutine _ascending_LINE: called CC=1 SCAR=0.0
112 49 $a->get('LINE')->compare( $b->get('LINE') );
113 }
114
115 1 ▶sub set_l4 {
Subroutine set_l4: called CC=1 SCAR=0.0
Pod undocumented
116 1 my ($self) = @_;
117 1 $self->{MODE} = 'L4';
118 1 $self->{PROTOCOLS} = [ 0, 6, 17 ];
119 1 $self->{PROPERTIES} = [ 'PROTOCOL', 'SRC_IP', 'SRC_PORT', 'DST_IP', 'DST_PORT' ];
120 }
121
122 # sort rules in ascending order so that current can contain next
123 # but next can't contain current
124 60 ▶sub _ascending_l4 {
Subroutine _ascending_l4: called CC=5 SCAR=16.1
125 60 ▶ $a->get('DST_IP')->compare( $b->get('DST_IP') )
Branch: 1 branch
BranchTrueFalseTotal
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("DST_PORT")->compare($b->get("DST_PORT")) or $a->get("SRC_PORT")->compare($b->get("SRC_PORT")) or $a->get("PROTOCOL")->compare($b->get("PROTOCOL")) 55 5 60
Condition88%8/9
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("DST_PORT")->compare($b->get("DST_PORT")) or $a->get("SRC_PORT")->compare($b->get("SRC_PORT"))
l!l&&r!l&&!r
5505
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("DST_PORT")->compare($b->get("DST_PORT"))
l!l&&r!l&&!r
5055
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP"))
l!l&&r!l&&!r
47310
MC/DC75%3/4
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("DST_PORT")->compare($b->get("DST_PORT")) or $a->get("SRC_PORT")->compare($b->get("SRC_PORT"))
$a->get("DST_IP")->compare($b->get("DST_IP")) $a->get("SRC_IP")->compare($b->get("SRC_IP")) $a->get("DST_PORT")->compare($b->get("DST_PORT")) $a->get("SRC_PORT")->compare($b->get("SRC_PORT"))
Truth table80%4/5
A or B or C or D
A= $a->get("DST_IP")->compare($b->get("DST_IP"))B= $a->get("SRC_IP")->compare($b->get("SRC_IP"))C= $a->get("DST_PORT")->compare($b->get("DST_PORT"))D= $a->get("SRC_PORT")->compare($b->get("SRC_PORT"))
ABCDresult
1XXX1
01XX1
001X1
00011
00000
126 || $a->get('SRC_IP')->compare( $b->get('SRC_IP') )
127 || $a->get('DST_PORT')->compare( $b->get('DST_PORT') )
128 || $a->get('SRC_PORT')->compare( $b->get('SRC_PORT') )
129 || $a->get('PROTOCOL')->compare( $b->get('PROTOCOL') );
130 }
131
132 1 ▶sub set_icmp {
Subroutine set_icmp: called CC=1 SCAR=0.0
Pod documented
133 1 my ($self) = @_;
134
135
136 1 $log->info("set_icmp mode");
137
138 1 $self->{MODE} = 'ICMP';
139 1 $self->{PROTOCOLS} = [ 0, 1 ];
140 1 $self->{PROPERTIES} = [ 'PROTOCOL', 'SRC_IP', 'DST_IP', 'ICMP_TYPE' ];
141 }
142
143 13 ▶sub _ascending_icmp {
Subroutine _ascending_icmp: called CC=4 SCAR=13.9
144 13 ▶ $a->get('DST_IP')->compare( $b->get('DST_IP') )
Branch: 1 branch
BranchTrueFalseTotal
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("ICMP_TYPE")->compare($b->get("ICMP_TYPE")) or $a->get("PROTOCOL")->compare($b->get("PROTOCOL")) 10 3 13
Condition83%5/6
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("ICMP_TYPE")->compare($b->get("ICMP_TYPE"))
l!l&&r!l&&!r
643
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP"))
l!l&&r!l&&!r
607
MC/DC66%2/3
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("ICMP_TYPE")->compare($b->get("ICMP_TYPE"))
$a->get("DST_IP")->compare($b->get("DST_IP")) $a->get("SRC_IP")->compare($b->get("SRC_IP")) $a->get("ICMP_TYPE")->compare($b->get("ICMP_TYPE"))
Truth table75%3/4
A or B or C
A= $a->get("DST_IP")->compare($b->get("DST_IP"))B= $a->get("SRC_IP")->compare($b->get("SRC_IP"))C= $a->get("ICMP_TYPE")->compare($b->get("ICMP_TYPE"))
ABCresult
1XX1
01X1
0011
0000
145 || $a->get('SRC_IP')->compare( $b->get('SRC_IP') )
146 || $a->get('ICMP_TYPE')->compare( $b->get('ICMP_TYPE') )
147 || $a->get('PROTOCOL')->compare( $b->get('PROTOCOL') );
148 }
149
150 1 ▶sub set_l3 {
Subroutine set_l3: called CC=6 SCAR=17.9
Pod documented
151 1 my ($self) = @_;
152
153
154
155 1 $log->info("set_l3 mode");
156
157 1 my $ICMP = Farly::Object->new();
158 1 $ICMP->set( 'PROTOCOL', Farly::Transport::Protocol->new(1) );
159
160 1 my $TCP = Farly::Object->new();
161 1 $TCP->set( 'PROTOCOL', Farly::Transport::Protocol->new(6) );
162
163 1 my $UDP = Farly::Object->new();
164 1 $UDP->set( 'PROTOCOL', Farly::Transport::Protocol->new(17) );
165
166 1 my %protocols;
167
168 1 foreach my $rule ( $self->rules->iter() ) {
169
170 20 ▶ next if $rule->matches($ICMP);
Branch: 1 branch
BranchTrueFalseTotal
if $rule->matches($ICMP) 5 15 20
171 15 ▶ next if $rule->matches($TCP);
Branch: 1 branch
BranchTrueFalseTotal
if $rule->matches($TCP) 8 7 15
172 7 ▶ next if $rule->matches($UDP);
Branch: 1 branch
BranchTrueFalseTotal
if $rule->matches($UDP) 3 4 7
173
174 4 ▶ if ( $rule->has_defined('PROTOCOL') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->has_defined('PROTOCOL')) { } 2 2 4
175 2 $protocols{ $rule->get('PROTOCOL')->as_string() }++;
176 }
177 else {
178 2 $log->info( "set_l3 skipped:\n" . $rule->dump() );
179 }
180 }
181
182 1 my @p = keys %protocols;
183
184 1 $self->{MODE} = 'L3';
185 1 $self->{PROTOCOLS} = \@p;
186 1 $self->{PROPERTIES} = [ 'PROTOCOL', 'SRC_IP', 'DST_IP' ];
187 }
188
189 1 ▶sub _ascending_l3 {
Subroutine _ascending_l3: called CC=3 SCAR=14.2
190 1 ▶ $a->get('DST_IP')->compare( $b->get('DST_IP') )
Branch: 1 branch
BranchTrueFalseTotal
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP")) or $a->get("PROTOCOL")->compare($b->get("PROTOCOL")) 0 1 1
Condition33%1/3
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP"))
l!l&&r!l&&!r
001
MC/DC0%0/2
$a->get("DST_IP")->compare($b->get("DST_IP")) or $a->get("SRC_IP")->compare($b->get("SRC_IP"))
$a->get("DST_IP")->compare($b->get("DST_IP")) $a->get("SRC_IP")->compare($b->get("SRC_IP"))
Truth table33%1/3
A or B
A= $a->get("DST_IP")->compare($b->get("DST_IP"))B= $a->get("SRC_IP")->compare($b->get("SRC_IP"))
ABresult
1X1
011
000
191 || $a->get('SRC_IP')->compare( $b->get('SRC_IP') )
192 || $a->get('PROTOCOL')->compare( $b->get('PROTOCOL') );
193 }
194
195 3 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
196 3 my ($self) = @_;
197
198 3 $self->_optimize();
199
200 3 $self->{OPTIMIZED} = $self->_keep( $self->rules );
201 3 $self->{REMOVED} = $self->_remove( $self->rules );
202 }
203
204 6 ▶sub _do_search {
Subroutine _do_search: called CC=2 SCAR=6.9
205 6 my ( $self, $action ) = @_;
206
207
208
209 6 my $search = Farly::Object->new();
210 6 my $result = Farly::Object::List->new();
211
212 6 foreach my $protocol ( $self->_protocols ) {
213
214 14 $log->info("searching for $action $protocol");
215
216 14 $search->set( 'PROTOCOL', Farly::Transport::Protocol->new($protocol) );
217 14 $search->set( 'ACTION', Farly::Value::String->new($action) );
218
219 14 $self->rules->matches( $search, $result );
220 }
221
222 6 return $result;
223 }
224
225 17 ▶sub _tuple {
Subroutine _tuple: called CC=3 SCAR=11.2
226 17 my ( $self, $rule ) = @_;
227
228
229
230 17 my $r = Farly::Object->new();
231
232 17 my @rule_properties = $self->_properties();
233
234 17 foreach my $property (@rule_properties) {
235 79 ▶ if ( $rule->has_defined($property) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->has_defined($property)) { } 79 0 79
236 79 $r->set( $property, $rule->get($property) );
237 }
238 else {
239 0 $log->warn( "property $property not defined in " . $rule->dump() );
240 }
241 }
242
243 17 return $r;
244 }
245
246 # Given rule X, Y, where X precedes Y in the ACL
247 # X and Y are inconsistent if:
248 # Xp contains Yd
249 # Xd contains Yp
250
251 6 ▶sub _inconsistent {
Subroutine _inconsistent: called CC=7 SCAR=27.3
252 6 my ( $self, $s_a, $s_an ) = @_;
253
254 # $s_a = ARRAY ref of rules of action a
255 # $s_an = ARRAY ref of rules of action !a
256 # $s_a and $s_an are sorted by line number and must be readonly
257
258 6 my $rule_x;
259 my $rule_y;
260
261 # iterate over rules of action a
262 6 ▶ for ( my $x = 0 ; $x != scalar( @{$s_a} ) ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != scalar @{$s_a;}) 21 6 27
263
264 21 $rule_x = $s_a->[$x];
265
266 21 ▶ confess "error : rule_x defined remove"
Branch: 1 branch
BranchTrueFalseTotal
if $rule_x->has_defined("REMOVE") 0 21 21
267 if ( $rule_x->has_defined('REMOVE') );
268
269 # iterate over rules of action !a
270 21 ▶ for ( my $y = 0 ; $y != scalar( @{$s_an} ) ; $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y != scalar @{$s_an;}) 0 21 21
271
272 0 $rule_y = $s_an->[$y];
273
274 #skip check if rule_y is already removed
275 0 ▶ next if $rule_y->has_defined('REMOVE');
Branch: 1 branch
BranchTrueFalseTotal
if $rule_y->has_defined("REMOVE") 0 0 0
276
277 # if $rule_x comes before $rule_y in the rule set
278 # then check if $rule_x contains $rule_y
279
280 0 ▶ if ( $rule_x->get('LINE')->number() <= $rule_y->get('LINE')->number() )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_x->get("LINE")->number <= $rule_y->get("LINE")->number) 0 0 0
281 {
282
283 # $rule_x1 is rule_x with layer 3 and 4 properties only
284 0 my $rule_x1 = $self->_tuple($rule_x);
285
286 0 ▶ if ( $rule_y->contained_by($rule_x1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_y->contained_by($rule_x1)) 0 0 0
287
288 # note removal of rule_y and the
289 # rule_x which caused the inconsistency
290 0 $rule_y->set( 'REMOVE', Farly::Value::String->new('RULE') );
291 0 $self->_log_remove( $rule_x, $rule_y );
292 }
293 }
294 }
295 }
296 }
297
298 # Given rule X, Y, where X precedes Y in the ACL
299 # if Yp contains Xp and there does not exist rule Zd between
300 # Xp and Yp such that Zd intersect Xp and Xp !contains Zd
301
302 1 ▶sub _can_remove {
Subroutine _can_remove: called CC=7 SCAR=33.9
303 1 my ( $self, $rule_x, $rule_y, $s_an ) = @_;
304
305 # $rule_x = the rule contained by $rule_y
306 # $s_an = rules of action !a sorted by ascending DST_IP
307
308 # $rule_x1 is rule_x with layer 3 and 4 properties only
309 1 my $rule_x1 = $self->_tuple($rule_x);
310
311 1 foreach my $rule_z ( @{$s_an} ) {
312
313 0 ▶ if ( !$rule_z->get('DST_IP')->gt( $rule_x1->get('DST_IP') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rule_z->get('DST_IP')->gt($rule_x1->get('DST_IP'))) { } 0 0 0
314
315 #is Z between X and Y?
316 0 ▶ if ( ( $rule_z->get('LINE')->number() >= $rule_x->get('LINE')->number() )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_z->get("LINE")->number >= $rule_x->get("LINE")->number and $rule_z->get("LINE")->number <= $rule_y->get("LINE")->number) 0 0 0
Condition0%0/3
$rule_z->get("LINE")->number >= $rule_x->get("LINE")->number and $rule_z->get("LINE")->number <= $rule_y->get("LINE")->number
!ll&&!rl&&r
000
MC/DC0%0/2
$rule_z->get("LINE")->number >= $rule_x->get("LINE")->number and $rule_z->get("LINE")->number <= $rule_y->get("LINE")->number
$rule_z->get("LINE")->number >= $rule_x->get("LINE")->number $rule_z->get("LINE")->number <= $rule_y->get("LINE")->number
Truth table0%0/3
A and B
A= $rule_z->get("LINE")->number >= $rule_x->get("LINE")->numberB= $rule_z->get("LINE")->number <= $rule_y->get("LINE")->number
ABresult
0X0
100
111
317 && ( $rule_z->get('LINE')->number() <= $rule_y->get('LINE')->number() ) )
318 {
319
320 # Zd intersect Xp?
321 0 ▶ if ( $rule_z->intersects($rule_x1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_z->intersects($rule_x1)) 0 0 0
322
323 # Xp ! contain Zd
324 0 ▶ if ( !$rule_z->contained_by($rule_x1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_z->contained_by($rule_x1)) 0 0 0
325 0 return undef;
326 }
327 }
328 }
329 }
330 else {
331
332 # $rule_z is greater than $rule_x1 therefore rule_x and rule_z are disjoint
333 0 last;
334 }
335 }
336
337 1 return 1;
338 }
339
340 # Given rule X, Y, where X precedes Y in the ACL
341 # a is the action type of the rule
342 # if X contains Y then Y can be removed
343 # if Y contains X then X can be removed if there are no rules Z
344 # in $s_an that intersect X and exist between X and Y in the ACL
345
346 6 ▶sub _redundant {
Subroutine _redundant: called CC=9 SCAR=22.0
347 6 my ( $self, $s_a, $s_an ) = @_;
348
349 # $s_a = ARRAY ref of rules of action a to be validated
350 # $s_an = ARRAY ref of rules of action !a
351 # $s_a and $s_an are sorted by ascending and must be readonly
352
353 # iterate over rules of action a
354 6 ▶ for ( my $x = 0 ; $x != scalar( @{$s_a} ) ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != scalar @{$s_a;}) 21 6 27
355
356 # $rule_x1 is rule_x with layer 3 and 4 properties only
357 21 my $rule_x = $s_a->[$x];
358
359 #skip check if rule_x is already being removed
360 21 ▶ next if $rule_x->has_defined('REMOVE');
Branch: 1 branch
BranchTrueFalseTotal
if $rule_x->has_defined("REMOVE") 5 16 21
361
362 # remove non layer 3/4 rule properties
363 16 my $rule_x1 = $self->_tuple( $s_a->[$x] );
364
365 16 ▶ for ( my $y = $x + 1 ; $y != scalar( @{$s_a} ) ; $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y != scalar @{$s_a;}) 54 8 62
366
367 54 my $rule_y = $s_a->[$y];
368
369 #skip check if a rule_x made more than one rule_y redundant
370 54 ▶ next if $rule_y->has_defined('REMOVE');
Branch: 1 branch
BranchTrueFalseTotal
if $rule_y->has_defined("REMOVE") 0 54 54
371
372 54 ▶ if ( !$rule_y->get('DST_IP')->gt( $rule_x->get('DST_IP') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rule_y->get('DST_IP')->gt($rule_x->get('DST_IP'))) { } 46 8 54
373
374 # $rule_x comes before rule_y in the rule array
375 # therefore x might contain y
376
377 46 ▶ if ( $rule_y->contained_by($rule_x1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_y->contained_by($rule_x1)) 5 41 46
378
379 # rule_x is before rule_y in the rule set so remove rule_y
380 5 ▶ if ( $rule_x->get('LINE')->number() <= $rule_y->get('LINE')->number() )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_x->get('LINE')->number <= $rule_y->get('LINE')->number) { } 4 1 5
381 {
382 4 $rule_y->set( 'REMOVE', Farly::Value::String->new('RULE') );
383 4 $self->_log_remove( $rule_x, $rule_y );
384 }
385 else {
386
387 # rule_y is actually after rule_x in the rule set
388 1 ▶ if ( $self->_can_remove( $rule_y, $rule_x, $s_an ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can_remove($rule_y, $rule_x, $s_an)) 1 0 1
389 1 $rule_y->set( 'REMOVE', Farly::Value::String->new('RULE') );
390 1 $self->_log_remove( $rule_x, $rule_y );
391 }
392 }
393 }
394 }
395 else {
396
397 # rule_y DST_IP is greater than rule_x DST_IP therefore rule_x can't
398 # contain rule_y or any rules after rule_y (they are disjoint)
399 8 last;
400 }
401 }
402 }
403 }
404
405 3 ▶sub _remove {
Subroutine _remove: called CC=3 SCAR=11.0
406 3 my ( $self, $a_ref ) = @_;
407
408 3 my $remove = Farly::Object::List->new();
409
410 3 foreach my $rule (@$a_ref) {
411 60 ▶ if ( $rule->has_defined('REMOVE') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->has_defined("REMOVE")) 5 55 60
412 5 $remove->add($rule);
413 }
414 }
415
416 3 return $remove;
417 }
418
419 12 ▶sub _keep {
Subroutine _keep: called CC=3 SCAR=11.0
420 12 my ( $self, $a_ref ) = @_;
421
422 12 my $keep = Farly::Object::List->new();
423
424 12 foreach my $rule (@$a_ref) {
425 102 ▶ if ( !$rule->has_defined('REMOVE') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule->has_defined("REMOVE")) 92 10 102
426 92 $keep->add($rule);
427 }
428 }
429
430 12 return $keep;
431 }
432
433 5 ▶sub _log_remove {
Subroutine _log_remove: called CC=2 SCAR=11.6
434 5 my ( $self, $keep, $remove ) = @_;
435
436 5 ▶ if ( $self->_is_verbose() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_verbose) 0 5 5
437 0 print " ! ";
438 0 $self->_template->as_string($keep);
439 0 print "\n";
440 0 $self->_template->as_string($remove);
441 0 print "\n";
442 }
443 }
444
445 9 ▶sub _do_sort {
Subroutine _do_sort: called CC=4 SCAR=14.0
446 9 my ( $self, $list ) = @_;
447
448 9 my @sorted;
449
450 9 ▶ if ( $self->_mode eq 'L4' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_mode eq 'L4') { } 3 6 9
elsif ($self->_mode eq 'L3') { } 3 3 6
elsif ($self->_mode eq 'ICMP') { } 3 0 3
451 3 @sorted = sort _ascending_l4 $list->iter();
452 }
453 elsif ( $self->_mode eq 'L3' ) {
454 3 @sorted = sort _ascending_l3 $list->iter();
455 }
456 elsif ( $self->_mode eq 'ICMP' ) {
457 3 @sorted = sort _ascending_icmp $list->iter();
458 }
459 else {
460 0 confess "mode error";
461 }
462
463 9 return \@sorted;
464 }
465
466 3 ▶sub _optimize {
Subroutine _optimize: called CC=1 SCAR=0.0
467 3 my ($self) = @_;
468
469
470
471 3 my $permits = $self->_do_search( $self->p_action );
472 3 my $denies = $self->_do_search( $self->d_action );
473
474 3 my @arr_permits = sort _ascending_LINE $permits->iter();
475 3 my @arr_denys = sort _ascending_LINE $denies->iter();
476
477 # find permit rules that contain deny rules
478 # which are defined further down in the rule set
479 3 $log->info("Checking for deny rule inconsistencies...");
480 3 $self->_inconsistent( \@arr_permits, \@arr_denys );
481
482 # create a new list of deny rules which are being kept
483 3 $denies = $self->_keep( \@arr_denys );
484
485 # the consistent deny list sorted by LINE again
486 3 @arr_denys = sort _ascending_LINE $denies->iter();
487
488 # find deny rules which contain permit
489 # rules further down in the rule set
490 3 $log->info("Checking for permit rule inconsistencies...");
491 3 $self->_inconsistent( \@arr_denys, \@arr_permits );
492
493 # create the list of permit rules which are being kept
494 3 $permits = $self->_keep( \@arr_permits );
495
496 # sort the rule in ascending order
497 3 my $aref_permits = $self->_do_sort($permits);
498 3 my $aref_denys = $self->_do_sort($denies);
499
500 3 $log->info("Checking for permit rule redundancies...");
501 3 $self->_redundant( $aref_permits, $aref_denys );
502
503 3 $permits = $self->_keep($aref_permits);
504
505 # sort the permits again
506 3 $aref_permits = $self->_do_sort($permits);
507
508 3 $log->info("Checking for deny rule redundancies...");
509 3 $self->_redundant( $aref_denys, $aref_permits );
510
511 }
512
513 1 1;
514 __END__
« Farly/ASA/Rewriter.pm ↑ Summary Farly/ASA/Filter.pm »