BlockerHelper/backends/ipfw.pm

filter: Statementstmt 30.0% 130 / 432 Branchbran 34.4% 64 / 186 Conditioncond 36.6% 44 / 120 MC/DCmcdc 3.3% 2 / 59 Subroutinesub 52.6% 10 / 19 Podpod 100% 13 / 13 totaltotal 31.7% 263 / 829 CC 144 SCAR 61.8
CC
144
Cov
32%
new
1062.9
unban
110.0
ban_cidr
90.0
CRAP 481.9
↑ Summary BlockerHelper/backends/nftables.pm »
1 package Net::Firewall::BlockerHelper::backends::ipfw;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base qw( Error::Helper Net::Firewall::BlockerHelper::Util );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::ipfw - IPFW backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 4 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 The block rules this backend creates are added straight into the main
25 ruleset at the configured rule number, the C<rule> option, default 150, so
26 no additional wiring is needed for them to be evaluated. What does need
27 minding is placement, as ipfw is first match wins. The rule number needs to
28 be lower than that of any rule that would pass the traffic in question,
29 such as a C<check-state> or an allow rule, otherwise the bans will never
30 match anything.
31  
32 use Net::Firewall::BlockerHelper::backends::ipfw;
33  
34 my $backend1;
35 my $backend2;
36 eval {
37 $backend1 = Net::Firewall::BlockerHelper::backends::ipfw->new(
38 backend => 'ipfw',
39 name => 'all',
40 options=>{ rule=>150, kill=>1 },
41 );
42 $backend2 = Net::Firewall::BlockerHelper::backends::ipfw->new(
43 backend => 'ipfw',
44 ports => ['143'],
45 protocols => ['tcp'],
46 name => 'imap',
47 options=>{ rule=>151 },
48 );
49 };
50 if ($@) {
51 print 'Error: '
52 . $Error::Helper::error
53 . "\nError String: "
54 . $Error::Helper::errorString
55 . "\nError Flag: "
56 . $Error::Helper::errorFlag . "\n";
57 }
58  
59 print `ipfw list`
60  
61 $backend1->init;
62 $backend2->init;
63  
64 print `ipfw list`
65  
66 $backend1->ban(ban=>'1.2.3.4');
67 $backend1->ban(ban=>'4.3.2.1');
68 $backend2->ban(ban=>'4.3.2.1');
69  
70 use Data::Dumper;
71 print Dumper($backend1->list);
72 print Dumper($backend2->list);
73  
74 $backend1->unban(ban=>'4.3.2.1');
75  
76 use Data::Dumper;
77 print Dumper($backend1->list);
78 print Dumper($backend2->list);
79  
80 $backend1->teardown;
81  
82 print `ipfw list`
83  
84 $backend2->teardown;
85  
86 print `ipfw list`
87  
88 =head1 METHODS
89  
90 =head2 new
91  
92 Initiates the object. Takes the arguments below.
93  
94 - options :: Backend specific options that will be passed to the backend unchecked
95 outside of making sure it is a hash ref if defined. See below for furhter info.
96 - Default :: {}
97  
98 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
99 service name via getservbyname. All ports will be blocked if non are specified. If
100 duplicates are removed.
101 - Default :: []
102  
103 - protocols :: A array of protocols to block. This is checked against
104 /etc/protocols via the function getprotobyname. Duplicates will be
105 discarded. If no protocols are given, all IPv4 and IPv6 traffic is
106 blocked ('ip4' and 'ip6'), unless ports are given, in which case it
107 defaults to tcp and udp. Ports are only attached to port-capable
108 protocols (tcp/udp/sctp); other protocols are blocked without a
109 port. As the ipfw 'me' keyword only matches IPv4 addresses and
110 'me6' only IPv6 ones, family neutral protocols such as tcp/udp
111 get one rule per family while family specific ones such as icmp or
112 ip6 get a single rule for their family.
113 - Default :: ['ip4','ip6'], or ['tcp','udp'] when ports are given
114  
115 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
116 - default :: kur
117  
118 - name :: Name of this specific instance. This must be specified.
119 - default :: undef
120  
121 The options hash accepts the following.
122  
123 - rule :: The rule number to use for the IPFW rules. This should not
124 be re-used or it will result in the other rules at that number
125 being removed when init is called.
126 - Default :: 150
127  
128 - type :: The drop method to use. 'deny' silently drops. 'unreach' and
129 'unreach6' both mean reject, and are treated as synonyms: the
130 family of each generated rule decides whether an IPv4 unreach or
131 an IPv6 unreach6 is sent, using the unreach and unreach6 codes
132 below. See ipfw(8) for more info.
133 - Default :: deny
134  
135 - unreach :: The IPv4 unreach code to use when type is a reject. Applied
136 to IPv4 protocols. See ipfw(8) for more info.
137 - Default :: port
138  
139 - unreach6 :: The IPv6 unreach6 code to use when type is a reject. Applied
140 to IPv6 protocols. See ipfw(8) for more info.
141 - Default :: port
142  
143 - kill :: Use tcpdrop to kill TCP connections for that IP. Handles both
144 IPv4 and IPv6, picking the sockstat family and parsing per the
145 family of the banned IP. TCP only, as FreeBSD has no tcpdrop
146 equivalent for UDP; since the generated rules are stateless, UDP
147 from the banned IP is blocked immediately regardless, so there is
148 no lingering flow state to clear. If protocols are configured and
149 tcp is not among them, nothing is killed, so blocking only udp
150 never drops TCP connections.
151 - Default :: 0
152  
153 All errors are considered fatal, meaning if new fails it will die.
154  
155 my $backend;
156 eval {
157 $backend = Net::Firewall::BlockerHelper::backends::ipfw->new(
158 backend => 'ipfw',
159 ports => ['22'],
160 protocols => ['tcp'],
161 name => 'ssh',
162 );
163 };
164 if ($@) {
165 print 'Error: '
166 . $Error::Helper::error
167 . "\nError String: "
168 . $Error::Helper::errorString
169 . "\nError Flag: "
170 . $Error::Helper::errorFlag . "\n";
171 }
172  
173 =cut
174  
175 4 ▶sub new {
Subroutine new: called CC=69 SCAR=69.7
Pod documented
176 4 my ( $blank, %opts ) = @_;
177  
178 4 my $self = {
179 perror => undef,
180 error => undef,
181 errorLine => undef,
182 errorFilename => undef,
183 errorString => "",
184 errorExtra => {
185 all_errors_fatal => 1,
186 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
187 # is kept for the name documented in its POD
188 all_fatal => 1,
189 flags => {
190 1 => 'notInited',
191 2 => 'invalidPortSpecified',
192 3 => 'portsNotArray',
193 4 => 'protocolsNotArray',
194 5 => 'invalidPortSpecified',
195 6 => 'invalidPrefixSpecified',
196 7 => 'invalidName',
197 8 => 'optionsNotHash',
198 9 => 'noBanItem',
199 10 => 'banItemNotIP',
200 11 => 'invalidBackend',
201 12 => 'backendInitError',
202 13 => 'banFailed',
203 14 => 'unbanFailed',
204 15 => 'listFailed',
205 16 => 'reInitFailed',
206 17 => 'teardownFailed',
207 18 => 'alreadyInited',
208 19 => 'ruleInvalid',
209 20 => 'typeInvalid',
210 21 => 'unreachInvalid',
211 22 => 'unreach6Invalid',
212 23 => 'initFailed',
213 26 => 'nameTooLong',
214 24 => 'checkFailed',
215 25 => 'flushFailed',
216 27 => 'banCidrFailed',
217 28 => 'unbanCidrFailed',
218 29 => 'cidrItemNotCidr',
219 30 => 'cidrNotSupported',
220 31 => 'listCidrFailed',
221 },
222 fatal_flags => {},
223 perror_not_fatal => 0,
224 },
225 options => {
226 rule => 150,
227 type => 'deny',
228 unreach => 'port',
229 unreach6 => 'port',
230 kill => 0,
231 },
232 ports => [],
233 protocols => [],
234 testing => undef,
235 test_data => undef,
236 prefix => 'kur',
237 frontend_obj => undef,
238 inited => 0,
239 banned => {},
240 banned_cidr => {},
241 cidr_supported => 1,
242 };
243 4 bless $self;
244  
245 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 4 4
elsif (defined $opts{'ports'}) { } 2 2 4
Condition66%2/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
220
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
246 0 $self->{perror} = 1;
247 0 $self->{error} = 3;
248 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
249 0 $self->warn;
250 } elsif ( defined( $opts{ports} ) ) {
251 2 my %ports;
252 2 foreach my $item ( @{ $opts{ports} } ) {
253 1 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 1 0 1
elsif ($item =~ /^[0-9]+$/) { } 0 0 0
Condition33%2/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
001
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
001
MC/DC0%0/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table25%1/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
254 1 $ports{$item} = 1;
255 } elsif ( $item =~ /^[0-9]+$/ ) {
256 0 $self->{perror} = 1;
257 0 $self->{error} = 2;
258 $self->{errorString}
259 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
260 0 $self->warn;
261 } else {
262 # just using tcp here as protocol must be specified
263 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
264 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
265 0 $self->{perror} = 1;
266 0 $self->{error} = 2;
267 $self->{errorString}
268 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
269 0 $self->warn;
270 }
271 0 $ports{$port} = 1;
272 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
273 } ## end foreach my $item ( @{ $opts{ports} } )
274 2 my @port_keys = keys(%ports);
275 0 @port_keys = sort { $a <=> $b } @port_keys;
276 2 push( @{ $self->{ports} }, @port_keys );
277 } ## end elsif ( defined( $opts{ports} ) )
278  
279 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 4 4
elsif (defined $opts{'protocols'}) { } 2 2 4
Condition66%2/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
220
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
280 0 $self->{perror} = 1;
281 0 $self->{error} = 4;
282 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
283 0 $self->warn;
284 } elsif ( defined( $opts{protocols} ) ) {
285 2 my %protocols;
286 2 foreach my $item ( @{ $opts{protocols} } ) {
287 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
288 # if this is undef, it means it is not a known protocol
289 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
290 0 $self->{perror} = 1;
291 0 $self->{error} = 5;
292 $self->{errorString}
293 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
294 0 $self->warn;
295 }
296 0 $protocols{$item} = 1;
297 } ## end foreach my $item ( @{ $opts{protocols} } )
298 2 my @protocols_keys = keys(%protocols);
299 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
300 2 push( @{ $self->{protocols} }, @protocols_keys );
301 } ## end elsif ( defined( $opts{protocols} ) )
302  
303 # make sure prefix is sane if defiend
304 4 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
305 0 $self->{perror} = 1;
306 0 $self->{error} = 6;
307 $self->{errorString}
308 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
309 0 $self->warn;
310 } elsif ( defined( $opts{prefix} ) ) {
311 4 $self->{prefix} = $opts{prefix};
312 }
313  
314 # make sure we have a name and that it is valid
315 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
316 0 $self->{perror} = 1;
317 0 $self->{error} = 7;
318 0 $self->{errorString} = 'name is undef';
319 0 $self->warn;
320 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
321 0 $self->{perror} = 1;
322 0 $self->{error} = 7;
323 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
324 0 $self->warn;
325 }
326 4 $self->{name} = $opts{name};
327  
328 # ipfw limits table names to 63 characters and the table is
329 # <prefix>_<name>, so catch over-long combos here rather than as a
330 # confusing error at init
331 4 ▶ if ( defined( $self->{name} ) && length( $self->{prefix} . '_' . $self->{name} ) > 63 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63) 1 3 4
Condition66%2/3
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63
!ll&&!rl&&r
031
MC/DC50%1/2
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63
defined $self->{"name"} length $self->{"prefix"} . '_' . $self->{"name"} > 63
Truth table66%2/3
A and B
A= defined $self->{"name"}B= length $self->{"prefix"} . '_' . $self->{"name"} > 63
ABresult
0X0
100
111
332 1 $self->{perror} = 1;
333 1 $self->{error} = 26;
334 $self->{errorString}
335 = 'the combined prefix and name, "'
336 . $self->{prefix} . '_'
337 . $self->{name}
338 1 . '", is longer than 63 characters, the max ipfw table name length';
339 1 $self->warn;
340 }
341  
342 # used internally for testing
343 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 1 3
344 2 $self->{testing} = $opts{testing};
345 }
346 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 1 3
347 2 $self->{frontend_obj} = $opts{frontend_obj};
348 }
349  
350 3 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 2 1 3
351 2 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 2 2
352 0 $self->{perror} = 1;
353 0 $self->{error} = 8;
354 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
355 0 $self->warn;
356 }
357 2 $self->{options} = $opts{options};
358  
359 2 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 1 1 2
360 1 $self->{options}{kill} = 0;
361 }
362  
363 2 ▶ if ( defined( $opts{options}{rule} ) && ref( $opts{options}{rule} ) ne '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'rule'} and ref $opts{'options'}{'rule'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'rule'} and not $opts{'options'}{'rule'} =~ /^[0-9]+$/) { } 0 2 2
elsif (defined $opts{'options'}{'rule'} and $opts{'options'}{'rule'} < 1) { } 0 2 2
elsif (not defined $opts{'options'}{'rule'}) { } 0 2 2
Condition33%1/3
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
defined $opts{"options"}{"rule"} ref $opts{"options"}{"rule"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"rule"}B= ref $opts{"options"}{"rule"} ne ""
ABresult
0X0
100
111
364 0 $self->{perror} = 1;
365 0 $self->{error} = 19;
366 0 $self->{errorString} = 'ref for $opts{options}{rule} is "' . ref( $opts{options}{rule} ) . '" and not ""';
367 0 $self->warn;
368 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} !~ /^[0-9]+$/ ) {
Condition33%1/3
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
defined $opts{"options"}{"rule"} not $opts{"options"}{"rule"} =~ /^[0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"options"}{"rule"}B= not $opts{"options"}{"rule"} =~ /^[0-9]+$/
ABresult
0X0
100
111
369 0 $self->{perror} = 1;
370 0 $self->{error} = 19;
371 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" and not "150"';
372 0 $self->warn;
373 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} < 1 ) {
Condition33%1/3
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
defined $opts{"options"}{"rule"} $opts{"options"}{"rule"} < 1
Truth table33%1/3
A and B
A= defined $opts{"options"}{"rule"}B= $opts{"options"}{"rule"} < 1
ABresult
0X0
100
111
374 0 $self->{perror} = 1;
375 0 $self->{error} = 19;
376 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" is less than 1';
377 0 $self->warn;
378 } elsif ( !defined( $opts{options}{rule} ) ) {
379 0 $self->{options}{rule} = 150;
380 }
381  
382 2 ▶ if ( defined( $opts{options}{type} ) && ref( $opts{options}{type} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'type'} and ref $opts{'options'}{'type'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'type'} and $opts{'options'}{'type'} ne 'unreach' and $opts{'options'}{'type'} ne 'deny' and $opts{'options'}{'type'} ne 'unreach6') { } 0 2 2
elsif (not defined $opts{'options'}{'type'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
defined $opts{"options"}{"type"} ref $opts{"options"}{"type"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"type"}B= ref $opts{"options"}{"type"} ne ""
ABresult
0X0
100
111
383 0 $self->{perror} = 1;
384 0 $self->{error} = 20;
385 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
386 0 $self->warn;
387 ▶ } elsif ( defined( $opts{options}{type} )
Condition33%3/9
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny" and $opts{"options"}{"type"} ne "unreach6"
!ll&&!rl&&r
200
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny"
!ll&&!rl&&r
200
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach"
!ll&&!rl&&r
200
MC/DC0%0/4
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny" and $opts{"options"}{"type"} ne "unreach6"
defined $opts{"options"}{"type"} $opts{"options"}{"type"} ne "unreach" $opts{"options"}{"type"} ne "deny" $opts{"options"}{"type"} ne "unreach6"
Truth table20%1/5
A and B and C and D
A= defined $opts{"options"}{"type"}B= $opts{"options"}{"type"} ne "unreach"C= $opts{"options"}{"type"} ne "deny"D= $opts{"options"}{"type"} ne "unreach6"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
388 && $opts{options}{type} ne 'unreach'
389 && $opts{options}{type} ne 'deny'
390 && $opts{options}{type} ne 'unreach6' )
391 {
392 0 $self->{perror} = 1;
393 0 $self->{error} = 20;
394 $self->{errorString}
395 0 = '$opts{options}{type} is "' . $opts{options}{type} . '" and not "deny", "unreach", or "unreach6"';
396 0 $self->warn;
397 } elsif ( !defined( $opts{options}{type} ) ) {
398 2 $self->{options}{type} = 'deny';
399 }
400  
401 2 ▶ if ( defined( $opts{options}{unreach} ) && ref( $opts{options}{unreach} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'unreach'} and ref $opts{'options'}{'unreach'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'unreach'} and $opts{'options'}{'unreach'} ne 'net' and $opts{'options'}{'unreach'} ne 'host' and $opts{'options'}{'unreach'} ne 'protocol' and $opts{'options'}{'unreach'} ne 'port' and $opts{'options'}{'unreach'} ne 'needfrag' and $opts{'options'}{'unreach'} ne 'srcfail' and $opts{'options'}{'unreach'} ne 'net-unknown' and $opts{'options'}{'unreach'} ne 'host-unknown' and $opts{'options'}{'unreach'} ne 'isolated' and $opts{'options'}{'unreach'} ne 'net-prohib' and $opts{'options'}{'unreach'} ne 'host-prohib' and $opts{'options'}{'unreach'} ne 'tosnet' and $opts{'options'}{'unreach'} ne 'toshost' and $opts{'options'}{'unreach'} ne 'filter-prohib' and $opts{'options'}{'unreach'} ne 'host-precedence' and $opts{'options'}{'unreach'} ne 'precedence-cutoff') { } 0 2 2
elsif (not defined $opts{'options'}{'unreach'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opts{"options"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
defined $opts{"options"}{"unreach"} ref $opts{"options"}{"unreach"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"unreach"}B= ref $opts{"options"}{"unreach"} ne ""
ABresult
0X0
100
111
402 0 $self->{perror} = 1;
403 0 $self->{error} = 21;
404 $self->{errorString}
405 0 = 'ref for $opts{options}{unreach} is "' . ref( $opts{options}{unreach} ) . '" and not ""';
406 0 $self->warn;
407 ▶ } elsif ( defined( $opts{options}{unreach} )
Condition33%16/48
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence" and $opts{"options"}{"unreach"} ne "precedence-cutoff"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net"
!ll&&!rl&&r
200
MC/DC0%0/17
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence" and $opts{"options"}{"unreach"} ne "precedence-cutoff"
too many conditions
408 && $opts{options}{unreach} ne 'net'
409 && $opts{options}{unreach} ne 'host'
410 && $opts{options}{unreach} ne 'protocol'
411 && $opts{options}{unreach} ne 'port'
412 && $opts{options}{unreach} ne 'needfrag'
413 && $opts{options}{unreach} ne 'srcfail'
414 && $opts{options}{unreach} ne 'net-unknown'
415 && $opts{options}{unreach} ne 'host-unknown'
416 && $opts{options}{unreach} ne 'isolated'
417 && $opts{options}{unreach} ne 'net-prohib'
418 && $opts{options}{unreach} ne 'host-prohib'
419 && $opts{options}{unreach} ne 'tosnet'
420 && $opts{options}{unreach} ne 'toshost'
421 && $opts{options}{unreach} ne 'filter-prohib'
422 && $opts{options}{unreach} ne 'host-precedence'
423 && $opts{options}{unreach} ne 'precedence-cutoff' )
424 {
425 0 $self->{perror} = 1;
426 0 $self->{error} = 21;
427 $self->{errorString}
428 = '$opts{options}{unreach} is "'
429 . $opts{options}{unreach}
430 0 . '" and a value understood by ipfw(8) for unreach';
431 0 $self->warn;
432 } elsif ( !defined( $opts{options}{unreach} ) ) {
433 2 $self->{options}{unreach} = 'port';
434 }
435  
436 2 ▶ if ( defined( $opts{options}{unreach6} ) && ref( $opts{options}{unreach6} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'unreach6'} and ref $opts{'options'}{'unreach6'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'unreach6'} and $opts{'options'}{'unreach6'} ne 'no-route' and $opts{'options'}{'unreach6'} ne 'admin-prohib' and $opts{'options'}{'unreach6'} ne 'address' and $opts{'options'}{'unreach6'} ne 'port') { } 0 2 2
elsif (not defined $opts{'options'}{'unreach6'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opts{"options"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
defined $opts{"options"}{"unreach6"} ref $opts{"options"}{"unreach6"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"unreach6"}B= ref $opts{"options"}{"unreach6"} ne ""
ABresult
0X0
100
111
437 0 $self->{perror} = 1;
438 0 $self->{error} = 22;
439 $self->{errorString}
440 0 = 'ref for $opts{options}{unreach6} is "' . ref( $opts{options}{unreach6} ) . '" and not ""';
441 0 $self->warn;
442 ▶ } elsif ( defined( $opts{options}{unreach6} )
Condition33%4/12
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address" and $opts{"options"}{"unreach6"} ne "port"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route"
!ll&&!rl&&r
200
MC/DC0%0/5
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address" and $opts{"options"}{"unreach6"} ne "port"
defined $opts{"options"}{"unreach6"} $opts{"options"}{"unreach6"} ne "no-route" $opts{"options"}{"unreach6"} ne "admin-prohib" $opts{"options"}{"unreach6"} ne "address" $opts{"options"}{"unreach6"} ne "port"
Truth table16%1/6
A and B and C and D and E
A= defined $opts{"options"}{"unreach6"}B= $opts{"options"}{"unreach6"} ne "no-route"C= $opts{"options"}{"unreach6"} ne "admin-prohib"D= $opts{"options"}{"unreach6"} ne "address"E= $opts{"options"}{"unreach6"} ne "port"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
443 && $opts{options}{unreach6} ne 'no-route'
444 && $opts{options}{unreach6} ne 'admin-prohib'
445 && $opts{options}{unreach6} ne 'address'
446 && $opts{options}{unreach6} ne 'port' )
447 {
448 0 $self->{perror} = 1;
449 0 $self->{error} = 22;
450 $self->{errorString}
451 = '$opts{options}{unreach6} is "'
452 . $opts{options}{unreach6}
453 0 . '" and a value understood by ipfw(8) for unreach6';
454 0 $self->warn;
455 } elsif ( !defined( $opts{options}{unreach6} ) ) {
456 2 $self->{options}{unreach6} = 'port';
457 }
458  
459 } ## end if ( defined( $opts{options} ) )
460  
461 3 return $self;
462 } ## end sub new
463  
464 =head2 init
465  
466 Initiates the backend. First any old remnants are removed on a best effort
467 basis, deleting the configured rule number and then destroying the table
468 C<E<lt>prefixE<gt>_E<lt>nameE<gt>>, with failures ignored. The rule goes
469 first as ipfw will not destroy a table a rule still references. The table is then created
470 via C<ipfw table ... create> and a block rule is added at the configured rule
471 number for each configured protocol, matching from C<table(...)> to C<me> or
472 C<me6> depending on the family, with the configured ports appended for
473 port-capable protocols (tcp/udp/sctp). The action is C<deny> or, for reject
474 types, C<unreach>/C<unreach6> per the family of the rule.
475  
476 Note that as ipfw is first match wins, the configured rule number needs to
477 come before any rules that would pass the traffic. See L</SYNOPSIS>.
478  
479 No arguments are taken.
480  
481 If called a second time, it will error.
482  
483 $backend->init;
484  
485 =cut
486  
487 2 ▶sub init {
Subroutine init: called CC=20 SCAR=34.4
Pod documented
488 2 my ( $self, %opts ) = @_;
489  
490 2 $self->errorblank;
491  
492 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
493 0 $self->{error} = 18;
494 0 $self->{errorString} = 'backend has already been inited';
495 0 $self->warn;
496 }
497  
498 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
499 2 $self->{frontend_obj}->{test_data} = {};
500 }
501  
502 2 my $ports;
503 2 ▶ if ( defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ports"}[0]) 1 1 2
504 1 $ports = join( ',', @{ $self->{ports} } );
505 }
506  
507 2 my @protocols;
508 2 ▶ if ( defined( $self->{protocols}[0] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'protocols'}[0]) { } 0 2 2
elsif (defined $ports) { } 1 1 2
509 0 push( @protocols, @{ $self->{protocols} } );
510 } elsif ( defined($ports) ) {
511 # ports need a port-capable protocol, default to tcp and udp
512 1 push( @protocols, 'tcp', 'udp' );
513 } else {
514 # block all, both IPv4 and IPv6; ip4/ip6 are used rather than ip as
515 # in ipfw 'ip' means any packet, not IPv4, per ipfw(8)
516 1 push( @protocols, 'ip4', 'ip6' );
517 }
518  
519 # protocols that accept a port specification
520 2 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
521  
522 # in ipfw 'me' matches only IPv4 addresses on the system and 'me6' only
523 # IPv6 ones, so each rule must be emitted for the right family; family
524 # neutral protocols such as tcp/udp get one rule per family
525 2 my %is_v6 = ( ip6 => 1, ipv6 => 1, icmp6 => 1, 'ipv6-icmp' => 1, icmpv6 => 1 );
526 2 my %is_v4 = ( ip4 => 1, ipv4 => 1, icmp => 1, igmp => 1 );
527  
528 # the rule has to go before the table it references, as ipfw refuses to
529 # destroy a table that is still in use by a rule; destroying first leaves
530 # the table behind and the create below then fails with "File exists"
531 2 my @fail_okay_commands;
532 2 push( @fail_okay_commands, 'ipfw delete ' . $self->{options}{rule} );
533 2 push( @fail_okay_commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
534  
535 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
536 2 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
537 } else {
538 0 foreach my $item (@fail_okay_commands) {
539 0 my $output = `$item 2>&1`;
540 }
541 }
542  
543 2 my @commands;
544 # add the table to create the command
545 2 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' create' );
546  
547 # generates the block rules
548 2 foreach my $item (@protocols) {
549 # family specific protocols get one rule for their family; family
550 # neutral protocols such as tcp/udp get a rule for each family
551 4 my @families;
552 4 ▶ if ( $is_v6{$item} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_v6{$item}) { } 1 3 4
elsif ($is_v4{$item}) { } 1 2 3
553 1 @families = (6);
554 } elsif ( $is_v4{$item} ) {
555 1 @families = (4);
556 } else {
557 2 @families = ( 4, 6 );
558 }
559  
560 4 foreach my $family (@families) {
561 # type is deny vs reject; for a reject the family of the rule
562 # decides between unreach (IPv4) and unreach6 (IPv6)
563 6 my $action;
564 6 ▶ if ( $self->{options}{type} eq 'deny' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'options'}{'type'} eq 'deny') { } 6 0 6
elsif ($family == 6) { } 0 0 0
565 6 $action = 'deny';
566 } elsif ( $family == 6 ) {
567 0 $action = 'unreach6 ' . $self->{options}{unreach6};
568 } else {
569 0 $action = 'unreach ' . $self->{options}{unreach};
570 }
571  
572 my $command
573 = 'ipfw add '
574 . $self->{options}{rule} . ' '
575 . $action . ' '
576 . $item
577 . ' from "table('
578 . $self->{prefix} . '_'
579 . $self->{name}
580 6 ▶ . ')" to '
Branch: 1 branch
BranchTrueFalseTotal
$family == 6 ? : 3 3 6
581 . ( $family == 6 ? 'me6' : 'me' );
582 6 ▶ if ( defined($ports) && $port_ok{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ports and $port_ok{$item}) 4 2 6
Condition66%2/3
defined $ports and $port_ok{$item}
!ll&&!rl&&r
204
MC/DC50%1/2
defined $ports and $port_ok{$item}
defined $ports $port_ok{$item}
Truth table66%2/3
A and B
A= defined $portsB= $port_ok{$item}
ABresult
0X0
100
111
583 4 $command = $command . ' ' . $ports;
584 }
585 6 push( @commands, $command );
586 } ## end foreach my $family (@families)
587 } ## end foreach my $item (@protocols)
588  
589 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
590 2 $self->{frontend_obj}->{test_data}{commands} = \@commands;
591 } else {
592 0 foreach my $item (@commands) {
593 0 my $output = `$item 2>&1`;
594 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
595 0 $self->{error} = 23;
596 $self->{errorString}
597 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
598 0 $self->warn;
599 }
600 }
601 } ## end else [ if ( $self->{testing} ) ]
602  
603 2 $self->{inited} = 1;
604 } ## end sub init
605  
606 =head2 ban
607  
608 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
609 and lowercased, then added to the IPFW table via
610 C<ipfw table E<lt>prefixE<gt>_E<lt>nameE<gt> add>. If the kill option is set
611 and tcp is among the blocked protocols, established TCP connections from the
612 IP are found via sockstat and killed via tcpdrop. Banning an already banned
613 IP is a noop.
614  
615 $backend->ban(ban => $ip);
616  
617 =cut
618  
619 2 ▶sub ban {
Subroutine ban: called CC=15 SCAR=40.7
Pod documented
620 2 my ( $self, %opts ) = @_;
621  
622 2 $self->errorblank;
623  
624 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
625 0 $self->{error} = 1;
626 0 $self->{errorString} = 'backend has not been inited';
627 0 $self->warn;
628 0 return;
629 }
630  
631 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/) { } 0 2 2
632 0 $self->{error} = 9;
633 0 $self->{errorString} = 'Nothing specified for the value ban';
634 0 $self->warn;
635 0 return;
636 } elsif ( ref( $opts{ban} ) ne '' ) {
637 0 $self->{error} = 10;
638 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
639 0 $self->warn;
640 0 return;
641 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
ABresult
1X1
011
000
642 && $opts{ban} !~ /\A$IPv6_re\z/ )
643 {
644 0 $self->{error} = 10;
645 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
646 0 $self->warn;
647 0 return;
648 }
649  
650 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
651 2 $opts{ban} = lc( $opts{ban} );
652  
653 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
654 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
655 0 $self->{frontend_obj}->{test_data} = 'already banned';
656 }
657 0 return;
658 }
659  
660 2 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $opts{ban};
661  
662 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
663 2 $self->{frontend_obj}->{test_data} = [$command];
664 } else {
665 0 my $output = `$command 2>&1`;
666 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
667 0 $self->{error} = 13;
668 $self->{errorString}
669 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
670 0 $self->warn;
671 }
672 }
673  
674 # tcpdrop only handles TCP, so only kill when tcp is among the blocked
675 # protocols; no protocols configured means everything is being blocked,
676 # tcp included
677 0 ▶ my $kill_applicable = !defined( $self->{protocols}[0] ) || ( grep { $_ eq 'tcp' } @{ $self->{protocols} } );
Condition33%1/3
!defined($self->{'protocols'}[0]) || grep({$_ eq 'tcp';} @{$$self{'protocols'};})
l!l&&r!l&&!r
200
MC/DC0%0/2
!defined($self->{'protocols'}[0]) || grep({$_ eq 'tcp';} @{$$self{'protocols'};})
!defined($self->{'protocols'}[0]) grep({$_ eq 'tcp';} @{$$self{'protocols'};})
Truth table33%1/3
not(A) || B
A= !defined($self->{'protocols'}[0])B= grep({$_ eq 'tcp';} @{$$self{'protocols'};})
ABresult
1X1
011
000
678  
679 2 ▶ if ( $self->{options}{kill} && $kill_applicable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"} and $kill_applicable) 2 0 2
Condition33%1/3
$self->{"options"}{"kill"} and $kill_applicable
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"options"}{"kill"} and $kill_applicable
$self->{"options"}{"kill"} $kill_applicable
Truth table33%1/3
A and B
A= $self->{"options"}{"kill"}B= $kill_applicable
ABresult
0X0
100
111
680 2 ▶ if ( $opts{ban} =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/) { } 1 1 2
681 # for IPv4 every colon is a address/port separator, so a simple
682 # split works; -wF so the IP is matched as a fixed string and not
683 # as a regexp, preventing 1.2.3.4 from also matching 11.2.3.45
684 $command
685 = 'sockstat -nc4 -P tcp |sed "s/.*tcp[46] *//" | sed "s/:/ /g" | grep -wF '
686 . $opts{ban}
687 1 . ' | xargs -n 4 tcpdrop';
688 } else {
689 # for IPv6 the address itself contains colons, so only the last
690 # colon of each field, the port, is turned into a space, and any
691 # scope ID is stripped as tcpdrop does not take them
692 $command
693 = 'sockstat -nc6 -P tcp |sed "s/.*tcp[46] *//" | grep -E "(^|[[:space:]])'
694 . $opts{ban}
695 1 . ':[0-9]+([[:space:]]|\$)" | sed "s/%[a-zA-Z0-9]*//g" | sed -E "s/:([0-9]+)([[:space:]]|\$)/ \\1\\2/g" | xargs -n 4 tcpdrop';
696 }
697 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
698 2 push( @{ $self->{frontend_obj}->{test_data} }, $command );
699 } else {
700 0 my $output = `$command 2>&1`;
701 }
702 } ## end if ( $self->{options}{kill} )
703  
704 2 $self->{banned}{ $opts{ban} } = 1;
705 } ## end sub ban
706  
707 =head2 unban
708  
709 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
710 and lowercased, then removed from the IPFW table via
711 C<ipfw table E<lt>prefixE<gt>_E<lt>nameE<gt> delete>. Unbanning an IP that
712 is not banned is a noop.
713  
714 $backend->unban(ban => $ip);
715  
716 =cut
717  
718 0 ▶sub unban {
Subroutine unban: not called CC=10 SCAR=47.0
Pod documented
719 0 my ( $self, %opts ) = @_;
720  
721 0 $self->errorblank;
722  
723 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
724 0 $self->{error} = 1;
725 0 $self->{errorString} = 'backend has not been inited';
726 0 $self->warn;
727 0 return;
728 }
729  
730 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/) { } 0 0 0
731 0 $self->{error} = 9;
732 0 $self->{errorString} = 'Nothing specified for the value ban';
733 0 $self->warn;
734 0 return;
735 } elsif ( ref( $opts{ban} ) ne '' ) {
736 0 $self->{error} = 10;
737 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
738 0 $self->warn;
739 0 return;
740 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
ABresult
1X1
011
000
741 && $opts{ban} !~ /\A$IPv6_re\z/ )
742 {
743 0 $self->{error} = 10;
744 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
745 0 $self->warn;
746 0 return;
747 }
748  
749 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
750 0 $opts{ban} = lc( $opts{ban} );
751  
752 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
753 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
754 0 $self->{frontend_obj}->{test_data} = 'not banned';
755 }
756 0 return;
757 }
758  
759 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' delete ' . $opts{ban};
760  
761 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
762 0 $self->{frontend_obj}->{test_data} = $command;
763 } else {
764 0 my $output = `$command 2>&1`;
765 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
766 0 $self->{error} = 14;
767 $self->{errorString}
768 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
769 0 $self->warn;
770 }
771 }
772  
773 0 delete( $self->{banned}{ $opts{ban} } );
774 } ## end sub unban
775  
776 =head2 ban_cidr
777  
778 Bans a CIDR range by adding it to the IPFW table. IPFW tables accept a
779 network prefix in the same manner as a single address.
780  
781 $backend->ban_cidr(ban => '1.2.3.0/24');
782  
783 =cut
784  
785 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=9 SCAR=45.0
Pod documented
786 0 my ( $self, %opts ) = @_;
787  
788 0 $self->errorblank;
789  
790 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
791 0 $self->{error} = 1;
792 0 $self->{errorString} = 'backend has not been inited';
793 0 $self->warn;
794 0 return;
795 }
796  
797 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 0 0
798 0 $self->{error} = 9;
799 0 $self->{errorString} = 'Nothing specified for the value ban';
800 0 $self->warn;
801 0 return;
802 } elsif ( ref( $opts{ban} ) ne '' ) {
803 0 $self->{error} = 29;
804 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
805 0 $self->warn;
806 0 return;
807 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
808 0 $self->{error} = 29;
809 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
810 0 $self->warn;
811 0 return;
812 }
813  
814 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
815 0 $opts{ban} = lc( $opts{ban} );
816  
817 0 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 0 0
818 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
819 0 $self->{frontend_obj}->{test_data} = 'already banned';
820 }
821 0 return;
822 }
823  
824 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $opts{ban};
825  
826 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
827 0 $self->{frontend_obj}->{test_data} = [$command];
828 } else {
829 0 my $output = `$command 2>&1`;
830 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
831 0 $self->{error} = 27;
832 $self->{errorString}
833 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
834 0 $self->warn;
835 }
836 }
837  
838 0 $self->{banned_cidr}{ $opts{ban} } = 1;
839 } ## end sub ban_cidr
840  
841 =head2 unban_cidr
842  
843 Unbans a CIDR range by deleting it from the IPFW table.
844  
845 $backend->unban_cidr(ban => '1.2.3.0/24');
846  
847 =cut
848  
849 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=9 SCAR=45.0
Pod documented
850 0 my ( $self, %opts ) = @_;
851  
852 0 $self->errorblank;
853  
854 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
855 0 $self->{error} = 1;
856 0 $self->{errorString} = 'backend has not been inited';
857 0 $self->warn;
858 0 return;
859 }
860  
861 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 0 0
862 0 $self->{error} = 9;
863 0 $self->{errorString} = 'Nothing specified for the value ban';
864 0 $self->warn;
865 0 return;
866 } elsif ( ref( $opts{ban} ) ne '' ) {
867 0 $self->{error} = 29;
868 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
869 0 $self->warn;
870 0 return;
871 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
872 0 $self->{error} = 29;
873 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
874 0 $self->warn;
875 0 return;
876 }
877  
878 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
879 0 $opts{ban} = lc( $opts{ban} );
880  
881 0 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 0 0
882 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
883 0 $self->{frontend_obj}->{test_data} = 'not banned';
884 }
885 0 return;
886 }
887  
888 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' delete ' . $opts{ban};
889  
890 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
891 0 $self->{frontend_obj}->{test_data} = $command;
892 } else {
893 0 my $output = `$command 2>&1`;
894 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
895 0 $self->{error} = 28;
896 $self->{errorString}
897 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
898 0 $self->warn;
899 }
900 }
901  
902 0 delete( $self->{banned_cidr}{ $opts{ban} } );
903 } ## end sub unban_cidr
904  
905 =head2 list_cidr
906  
907 List banned CIDR ranges.
908  
909 my @banned_cidrs = $backend->list_cidr;
910  
911 =cut
912  
913 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=2 SCAR=17.9
Pod documented
914 0 my ( $self, %opts ) = @_;
915  
916 0 $self->errorblank;
917  
918 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
919 0 $self->{frontend_obj}->{test_data} = 'list_cidr';
920 }
921  
922 0 return keys( %{ $self->{banned_cidr} } );
923 }
924  
925 =head2 list
926  
927 List banned IPs. Returns an array of the currently banned single IPs. CIDR
928 ranges are not included; for those see L</list_cidr>.
929  
930 my @banned = $backend->list;
931  
932 =cut
933  
934 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
935 0 my ( $self, %opts ) = @_;
936  
937 0 $self->errorblank;
938  
939 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
940 0 $self->{frontend_obj}->{test_data} = 'list';
941 }
942  
943 0 return keys( %{ $self->{banned} } );
944 }
945  
946 =head2 re_init
947  
948 Tells the backend to re-init itself.
949  
950 Calls teardown, with errors ignored as a partially wiped setup is what
951 re_init recovers from, then init to recreate the table and rules. All
952 previously banned IPs and CIDR ranges are then re-added to the table via
953 C<ipfw table ... add>.
954  
955 $backend->re_init;
956  
957 =cut
958  
959 0 ▶sub re_init {
Subroutine re_init: not called CC=5 SCAR=34.0
Pod documented
960 0 my ( $self, %opts ) = @_;
961  
962 0 $self->errorblank;
963  
964 # teardown is best effort here as a partially or fully wiped setup is
965 # exactly what re_init needs to recover from; init cleans up any remnants
966 {
967 0 local $@;
968 0 eval { $self->teardown; };
969 }
970 0 $self->init;
971  
972 0 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
973  
974 0 my @re_init_test_data;
975 0 foreach my $item (@to_ban) {
976 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $item;
977  
978 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
979 0 push( @re_init_test_data, $command );
980 } else {
981 0 my $output = `$command 2>&1`;
982 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
983 0 $self->{error} = 13;
984 $self->{errorString}
985 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
986 0 $self->warn;
987 }
988 }
989 } ## end foreach my $item (@to_ban)
990  
991 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
992 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
993 }
994  
995 0 $self->{inited} = 1;
996 } ## end sub re_init
997  
998 =head2 teardown
999  
1000 Tears down the setup for the backend, deleting the configured rule number and
1001 then destroying the table C<E<lt>prefixE<gt>_E<lt>nameE<gt>>.
1002  
1003 The ordering matters. ipfw will not destroy a table that is still in use by a
1004 rule, so the rule is removed first; doing it the other way round leaves the
1005 table behind.
1006  
1007 Both commands are always attempted, and a failure of either is raised only
1008 once both have run, so a rule that has already been removed does not stop the
1009 table from being destroyed.
1010  
1011 It does not check if it has been inited or not, so if called prior to
1012 calling init, it will error as the commands will fail.
1013  
1014 $backend->teardown;
1015  
1016 =cut
1017  
1018 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
1019 0 my ( $self, %opts ) = @_;
1020  
1021 0 $self->errorblank;
1022  
1023 0 $self->{inited} = 0;
1024  
1025 # the rule has to go before the table it references, as ipfw refuses to
1026 # destroy a table that is still in use by a rule
1027 0 my @commands;
1028 0 push( @commands, 'ipfw delete ' . $self->{options}{rule} );
1029 0 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
1030  
1031 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1032 0 $self->{frontend_obj}->{test_data} = \@commands;
1033 } else {
1034 # every command is attempted before anything is raised. errors are
1035 # fatal, so warning inside the loop would abort it and leave the rest
1036 # of the setup in place; a rule that is already gone must not stop the
1037 # table from being destroyed
1038 0 my @failures;
1039 0 foreach my $item (@commands) {
1040 0 my $output = `$item 2>&1`;
1041 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1042 0 push( @failures, 'command... "' . $item . '"... output... ' . $output );
1043 }
1044 }
1045  
1046 0 ▶ if (@failures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failures) 0 0 0
1047 0 $self->{error} = 17;
1048 0 $self->{errorString} = 'teardown failed. non-zero exit code for the ' . join( ' and the ', @failures );
1049 0 $self->warn;
1050 }
1051 } ## end else [ if ( $self->{testing} ) ]
1052 } ## end sub teardown
1053  
1054 =head2 stop
1055  
1056 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
1057 concept.
1058  
1059 $backend->stop;
1060  
1061 =cut
1062  
1063 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1064 0 my ( $self, %opts ) = @_;
1065  
1066 0 return $self->teardown(%opts);
1067 }
1068  
1069 =head2 check
1070  
1071 Verifies that the IPFW table is still present and that the rule number still
1072 exists. Returns a true value if the setup is intact and a false value if any
1073 part of it appears to have been removed. This is the equivalent of fail2ban's
1074 C<actioncheck>.
1075  
1076 if ( !$backend->check ) {
1077 $backend->re_init;
1078 }
1079  
1080 =cut
1081  
1082 2 ▶sub check {
Subroutine check: called CC=4 SCAR=18.6
Pod documented
1083 2 my ( $self, %opts ) = @_;
1084  
1085 2 $self->errorblank;
1086  
1087 my @commands = (
1088 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' info',
1089 'ipfw list ' . $self->{options}{rule},
1090 2 );
1091  
1092 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
1093 2 $self->{frontend_obj}->{test_data} = \@commands;
1094 2 return 1;
1095 }
1096  
1097 0 foreach my $item (@commands) {
1098 0 my $output = `$item 2>&1`;
1099 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1100 0 return 0;
1101 }
1102 }
1103  
1104 0 return 1;
1105 } ## end sub check
1106  
1107 =head2 flush
1108  
1109 Removes all currently banned IPs and CIDR ranges at once by flushing the
1110 IPFW table, leaving the table and rule in place. This is the equivalent of
1111 fail2ban's C<actionflush>.
1112  
1113 $backend->flush;
1114  
1115 =cut
1116  
1117 0 ▶sub flush {
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
1118 0 my ( $self, %opts ) = @_;
1119  
1120 0 $self->errorblank;
1121  
1122 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1123 0 $self->{error} = 1;
1124 0 $self->{errorString} = 'backend has not been inited';
1125 0 $self->warn;
1126 0 return;
1127 }
1128  
1129 0 my @commands = ( 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' flush' );
1130  
1131 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1132 0 $self->{frontend_obj}->{test_data} = \@commands;
1133 } else {
1134 0 foreach my $item (@commands) {
1135 0 my $output = `$item 2>&1`;
1136 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1137 0 $self->{error} = 25;
1138 $self->{errorString}
1139 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1140 0 $self->warn;
1141 }
1142 }
1143 } ## end else [ if ( $self->{testing} ) ]
1144  
1145 # the table flush above removes both single IP and CIDR entries at once
1146 0 $self->{banned} = {};
1147 0 $self->{banned_cidr} = {};
1148 } ## end sub flush
1149  
1150 =head1 ERROR CODES / FLAGS
1151  
1152 Error handling is provided by L<Error::Helper>. All
1153 errors are considered fatal.
1154  
1155 =head2 1, notInited
1156  
1157 The backend has not been inited yet.
1158  
1159 =head2 2, invalidPortSpecified
1160  
1161 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1162  
1163 =head2 3, portsNotArray
1164  
1165 The data passed to new for ports is not an array.
1166  
1167 =head2 4, protocolsNotArray
1168  
1169 The data passed to new for protocols is not an array.
1170  
1171 =head2 5, invalidPortSpecified
1172  
1173 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1174  
1175 =head2 6, invalidPrefixSpecified
1176  
1177 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1178  
1179 =head2 7, invalidName
1180  
1181 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1182  
1183 =head2 8, optionsNotHash
1184  
1185 The item passed to new for options is not a hash.
1186  
1187 =head2 9, noBanItem
1188  
1189 No IP or CIDR range specified to ban or unban.
1190  
1191 =head2 10, banItemNotIP
1192  
1193 The item to ban is not an IP. Either wrong ref type or regexp
1194 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1195  
1196 =head2 11, invalidBackend
1197  
1198 The specified backend failed to pass a basic sanity check of making sure it
1199 matches the regexp /^[a-zA-Z0-9\_]+$/.
1200  
1201 =head2 12, backendInitError
1202  
1203 Failed to init the backend.
1204  
1205 =head2 13, banFailed
1206  
1207 Failed to ban the item.
1208  
1209 =head2 14, unbanFailed
1210  
1211 Failed to unban the item.
1212  
1213 =head2 15, listFailed
1214  
1215 Failed to get a list of bans.
1216  
1217 =head2 16, reInitFailed
1218  
1219 Failed to re_init the backend.
1220  
1221 =head2 17, teardownFailed
1222  
1223 Failed to teardown the backend.
1224  
1225 =head2 18, alreadyInited
1226  
1227 init called, but the backend has already been inited.
1228  
1229 =head2 19, ruleInvalid
1230  
1231 The option rule is not an int of 1 or greater.
1232  
1233 =head2 20, typeInvalid
1234  
1235 The option type is not "deny", "unreach", or "unreach6".
1236  
1237 =head2 21, unreachInvalid
1238  
1239 The option unreach is not a value understood by ipfw(8) for unreach.
1240  
1241 =head2 22, unreach6Invalid
1242  
1243 The option unreach6 is not a value understood by ipfw(8) for unreach6.
1244  
1245 =head2 23, initFailed
1246  
1247 One of the required commands for init failed.
1248  
1249 =head2 24, checkFailed
1250  
1251 The backend check raised an error.
1252  
1253 =head2 25, flushFailed
1254  
1255 Failed to flush the bans.
1256  
1257 =head2 26, nameTooLong
1258  
1259 The combined prefix and name is longer than 63 characters, the max ipfw
1260 table name length.
1261  
1262 =head2 27, banCidrFailed
1263  
1264 Failed to ban the CIDR range.
1265  
1266 =head2 28, unbanCidrFailed
1267  
1268 Failed to unban the CIDR range.
1269  
1270 =head2 29, cidrItemNotCidr
1271  
1272 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1273 IPv4 or IPv6 address followed by a prefix length valid for its family.
1274  
1275 =head2 30, cidrNotSupported
1276  
1277 The backend does not support CIDR bans.
1278  
1279 =head2 31, listCidrFailed
1280  
1281 Failed to get a list of CIDR bans.
1282  
1283 =head1 AUTHOR
1284  
1285 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1286  
1287 =head1 BUGS
1288  
1289 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1290 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
1291 automatically be notified of progress on your bug as I make changes.
1292  
1293  
1294  
1295  
1296 =head1 SUPPORT
1297  
1298 You can find documentation for this module with the perldoc command.
1299  
1300 perldoc Net::Firewall::BlockerHelper
1301  
1302  
1303 You can also look for information at:
1304  
1305 =over 4
1306  
1307 =item * RT: CPAN's request tracker (report bugs here)
1308  
1309 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
1310  
1311 =item * Search CPAN
1312  
1313 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
1314  
1315 =back
1316  
1317  
1318 =head1 ACKNOWLEDGEMENTS
1319  
1320  
1321 =head1 LICENSE AND COPYRIGHT
1322  
1323 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1324  
1325 This is free software, licensed under:
1326  
1327 The GNU Lesser General Public License, Version 2.1, February 1999
1328  
1329  
1330 =cut
1331  
1332 4 1; # End of Net::Firewall::BlockerHelper
↑ Summary BlockerHelper/backends/nftables.pm »